usbtest.c 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/kernel.h>
  3. #include <linux/errno.h>
  4. #include <linux/init.h>
  5. #include <linux/slab.h>
  6. #include <linux/mm.h>
  7. #include <linux/module.h>
  8. #include <linux/moduleparam.h>
  9. #include <linux/scatterlist.h>
  10. #include <linux/mutex.h>
  11. #include <linux/timer.h>
  12. #include <linux/usb.h>
  13. #define SIMPLE_IO_TIMEOUT 10000 /* in milliseconds */
  14. /*-------------------------------------------------------------------------*/
  15. static int override_alt = -1;
  16. module_param_named(alt, override_alt, int, 0644);
  17. MODULE_PARM_DESC(alt, ">= 0 to override altsetting selection");
  18. static void complicated_callback(struct urb *urb);
  19. /*-------------------------------------------------------------------------*/
  20. /* FIXME make these public somewhere; usbdevfs.h? */
  21. /* Parameter for usbtest driver. */
  22. struct usbtest_param_32 {
  23. /* inputs */
  24. __u32 test_num; /* 0..(TEST_CASES-1) */
  25. __u32 iterations;
  26. __u32 length;
  27. __u32 vary;
  28. __u32 sglen;
  29. /* outputs */
  30. __s32 duration_sec;
  31. __s32 duration_usec;
  32. };
  33. /*
  34. * Compat parameter to the usbtest driver.
  35. * This supports older user space binaries compiled with 64 bit compiler.
  36. */
  37. struct usbtest_param_64 {
  38. /* inputs */
  39. __u32 test_num; /* 0..(TEST_CASES-1) */
  40. __u32 iterations;
  41. __u32 length;
  42. __u32 vary;
  43. __u32 sglen;
  44. /* outputs */
  45. __s64 duration_sec;
  46. __s64 duration_usec;
  47. };
  48. /* IOCTL interface to the driver. */
  49. #define USBTEST_REQUEST_32 _IOWR('U', 100, struct usbtest_param_32)
  50. /* COMPAT IOCTL interface to the driver. */
  51. #define USBTEST_REQUEST_64 _IOWR('U', 100, struct usbtest_param_64)
  52. /*-------------------------------------------------------------------------*/
  53. #define GENERIC /* let probe() bind using module params */
  54. /* Some devices that can be used for testing will have "real" drivers.
  55. * Entries for those need to be enabled here by hand, after disabling
  56. * that "real" driver.
  57. */
  58. //#define IBOT2 /* grab iBOT2 webcams */
  59. //#define KEYSPAN_19Qi /* grab un-renumerated serial adapter */
  60. /*-------------------------------------------------------------------------*/
  61. struct usbtest_info {
  62. const char *name;
  63. u8 ep_in; /* bulk/intr source */
  64. u8 ep_out; /* bulk/intr sink */
  65. unsigned autoconf:1;
  66. unsigned ctrl_out:1;
  67. unsigned iso:1; /* try iso in/out */
  68. unsigned intr:1; /* try interrupt in/out */
  69. int alt;
  70. };
  71. /* this is accessed only through usbfs ioctl calls.
  72. * one ioctl to issue a test ... one lock per device.
  73. * tests create other threads if they need them.
  74. * urbs and buffers are allocated dynamically,
  75. * and data generated deterministically.
  76. */
  77. struct usbtest_dev {
  78. struct usb_interface *intf;
  79. struct usbtest_info *info;
  80. int in_pipe;
  81. int out_pipe;
  82. int in_iso_pipe;
  83. int out_iso_pipe;
  84. int in_int_pipe;
  85. int out_int_pipe;
  86. struct usb_endpoint_descriptor *iso_in, *iso_out;
  87. struct usb_endpoint_descriptor *int_in, *int_out;
  88. struct mutex lock;
  89. #define TBUF_SIZE 256
  90. u8 *buf;
  91. };
  92. static struct usb_device *testdev_to_usbdev(struct usbtest_dev *test)
  93. {
  94. return interface_to_usbdev(test->intf);
  95. }
  96. /* set up all urbs so they can be used with either bulk or interrupt */
  97. #define INTERRUPT_RATE 1 /* msec/transfer */
  98. #define ERROR(tdev, fmt, args...) \
  99. dev_err(&(tdev)->intf->dev , fmt , ## args)
  100. #define WARNING(tdev, fmt, args...) \
  101. dev_warn(&(tdev)->intf->dev , fmt , ## args)
  102. #define GUARD_BYTE 0xA5
  103. #define MAX_SGLEN 128
  104. /*-------------------------------------------------------------------------*/
  105. static inline void endpoint_update(int edi,
  106. struct usb_host_endpoint **in,
  107. struct usb_host_endpoint **out,
  108. struct usb_host_endpoint *e)
  109. {
  110. if (edi) {
  111. if (!*in)
  112. *in = e;
  113. } else {
  114. if (!*out)
  115. *out = e;
  116. }
  117. }
  118. static int
  119. get_endpoints(struct usbtest_dev *dev, struct usb_interface *intf)
  120. {
  121. int tmp;
  122. struct usb_host_interface *alt;
  123. struct usb_host_endpoint *in, *out;
  124. struct usb_host_endpoint *iso_in, *iso_out;
  125. struct usb_host_endpoint *int_in, *int_out;
  126. struct usb_device *udev;
  127. for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
  128. unsigned ep;
  129. in = out = NULL;
  130. iso_in = iso_out = NULL;
  131. int_in = int_out = NULL;
  132. alt = intf->altsetting + tmp;
  133. if (override_alt >= 0 &&
  134. override_alt != alt->desc.bAlternateSetting)
  135. continue;
  136. /* take the first altsetting with in-bulk + out-bulk;
  137. * ignore other endpoints and altsettings.
  138. */
  139. for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
  140. struct usb_host_endpoint *e;
  141. int edi;
  142. e = alt->endpoint + ep;
  143. edi = usb_endpoint_dir_in(&e->desc);
  144. switch (usb_endpoint_type(&e->desc)) {
  145. case USB_ENDPOINT_XFER_BULK:
  146. endpoint_update(edi, &in, &out, e);
  147. continue;
  148. case USB_ENDPOINT_XFER_INT:
  149. if (dev->info->intr)
  150. endpoint_update(edi, &int_in, &int_out, e);
  151. continue;
  152. case USB_ENDPOINT_XFER_ISOC:
  153. if (dev->info->iso)
  154. endpoint_update(edi, &iso_in, &iso_out, e);
  155. fallthrough;
  156. default:
  157. continue;
  158. }
  159. }
  160. if ((in && out) || iso_in || iso_out || int_in || int_out)
  161. goto found;
  162. }
  163. return -EINVAL;
  164. found:
  165. udev = testdev_to_usbdev(dev);
  166. dev->info->alt = alt->desc.bAlternateSetting;
  167. if (alt->desc.bAlternateSetting != 0) {
  168. tmp = usb_set_interface(udev,
  169. alt->desc.bInterfaceNumber,
  170. alt->desc.bAlternateSetting);
  171. if (tmp < 0)
  172. return tmp;
  173. }
  174. if (in)
  175. dev->in_pipe = usb_rcvbulkpipe(udev,
  176. in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  177. if (out)
  178. dev->out_pipe = usb_sndbulkpipe(udev,
  179. out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  180. if (iso_in) {
  181. dev->iso_in = &iso_in->desc;
  182. dev->in_iso_pipe = usb_rcvisocpipe(udev,
  183. iso_in->desc.bEndpointAddress
  184. & USB_ENDPOINT_NUMBER_MASK);
  185. }
  186. if (iso_out) {
  187. dev->iso_out = &iso_out->desc;
  188. dev->out_iso_pipe = usb_sndisocpipe(udev,
  189. iso_out->desc.bEndpointAddress
  190. & USB_ENDPOINT_NUMBER_MASK);
  191. }
  192. if (int_in) {
  193. dev->int_in = &int_in->desc;
  194. dev->in_int_pipe = usb_rcvintpipe(udev,
  195. int_in->desc.bEndpointAddress
  196. & USB_ENDPOINT_NUMBER_MASK);
  197. }
  198. if (int_out) {
  199. dev->int_out = &int_out->desc;
  200. dev->out_int_pipe = usb_sndintpipe(udev,
  201. int_out->desc.bEndpointAddress
  202. & USB_ENDPOINT_NUMBER_MASK);
  203. }
  204. return 0;
  205. }
  206. /*-------------------------------------------------------------------------*/
  207. /* Support for testing basic non-queued I/O streams.
  208. *
  209. * These just package urbs as requests that can be easily canceled.
  210. * Each urb's data buffer is dynamically allocated; callers can fill
  211. * them with non-zero test data (or test for it) when appropriate.
  212. */
  213. static void simple_callback(struct urb *urb)
  214. {
  215. complete(urb->context);
  216. }
  217. static struct urb *usbtest_alloc_urb(
  218. struct usb_device *udev,
  219. int pipe,
  220. unsigned long bytes,
  221. unsigned transfer_flags,
  222. unsigned offset,
  223. u8 bInterval,
  224. usb_complete_t complete_fn)
  225. {
  226. struct urb *urb;
  227. urb = usb_alloc_urb(0, GFP_KERNEL);
  228. if (!urb)
  229. return urb;
  230. if (bInterval)
  231. usb_fill_int_urb(urb, udev, pipe, NULL, bytes, complete_fn,
  232. NULL, bInterval);
  233. else
  234. usb_fill_bulk_urb(urb, udev, pipe, NULL, bytes, complete_fn,
  235. NULL);
  236. urb->interval = (udev->speed == USB_SPEED_HIGH)
  237. ? (INTERRUPT_RATE << 3)
  238. : INTERRUPT_RATE;
  239. urb->transfer_flags = transfer_flags;
  240. if (usb_pipein(pipe))
  241. urb->transfer_flags |= URB_SHORT_NOT_OK;
  242. if ((bytes + offset) == 0)
  243. return urb;
  244. if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
  245. urb->transfer_buffer = usb_alloc_coherent(udev, bytes + offset,
  246. GFP_KERNEL, &urb->transfer_dma);
  247. else
  248. urb->transfer_buffer = kmalloc(bytes + offset, GFP_KERNEL);
  249. if (!urb->transfer_buffer) {
  250. usb_free_urb(urb);
  251. return NULL;
  252. }
  253. /* To test unaligned transfers add an offset and fill the
  254. unused memory with a guard value */
  255. if (offset) {
  256. memset(urb->transfer_buffer, GUARD_BYTE, offset);
  257. urb->transfer_buffer += offset;
  258. if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
  259. urb->transfer_dma += offset;
  260. }
  261. /* For inbound transfers use guard byte so that test fails if
  262. data not correctly copied */
  263. memset(urb->transfer_buffer,
  264. usb_pipein(urb->pipe) ? GUARD_BYTE : 0,
  265. bytes);
  266. return urb;
  267. }
  268. static struct urb *simple_alloc_urb(
  269. struct usb_device *udev,
  270. int pipe,
  271. unsigned long bytes,
  272. u8 bInterval)
  273. {
  274. return usbtest_alloc_urb(udev, pipe, bytes, URB_NO_TRANSFER_DMA_MAP, 0,
  275. bInterval, simple_callback);
  276. }
  277. static struct urb *complicated_alloc_urb(
  278. struct usb_device *udev,
  279. int pipe,
  280. unsigned long bytes,
  281. u8 bInterval)
  282. {
  283. return usbtest_alloc_urb(udev, pipe, bytes, URB_NO_TRANSFER_DMA_MAP, 0,
  284. bInterval, complicated_callback);
  285. }
  286. static unsigned pattern;
  287. static unsigned mod_pattern;
  288. module_param_named(pattern, mod_pattern, uint, S_IRUGO | S_IWUSR);
  289. MODULE_PARM_DESC(mod_pattern, "i/o pattern (0 == zeroes)");
  290. static unsigned get_maxpacket(struct usb_device *udev, int pipe)
  291. {
  292. struct usb_host_endpoint *ep;
  293. ep = usb_pipe_endpoint(udev, pipe);
  294. return le16_to_cpup(&ep->desc.wMaxPacketSize);
  295. }
  296. static int ss_isoc_get_packet_num(struct usb_device *udev, int pipe)
  297. {
  298. struct usb_host_endpoint *ep = usb_pipe_endpoint(udev, pipe);
  299. return USB_SS_MULT(ep->ss_ep_comp.bmAttributes)
  300. * (1 + ep->ss_ep_comp.bMaxBurst);
  301. }
  302. static void simple_fill_buf(struct urb *urb)
  303. {
  304. unsigned i;
  305. u8 *buf = urb->transfer_buffer;
  306. unsigned len = urb->transfer_buffer_length;
  307. unsigned maxpacket;
  308. switch (pattern) {
  309. default:
  310. fallthrough;
  311. case 0:
  312. memset(buf, 0, len);
  313. break;
  314. case 1: /* mod63 */
  315. maxpacket = get_maxpacket(urb->dev, urb->pipe);
  316. for (i = 0; i < len; i++)
  317. *buf++ = (u8) ((i % maxpacket) % 63);
  318. break;
  319. }
  320. }
  321. static inline unsigned long buffer_offset(void *buf)
  322. {
  323. return (unsigned long)buf & (ARCH_KMALLOC_MINALIGN - 1);
  324. }
  325. static int check_guard_bytes(struct usbtest_dev *tdev, struct urb *urb)
  326. {
  327. u8 *buf = urb->transfer_buffer;
  328. u8 *guard = buf - buffer_offset(buf);
  329. unsigned i;
  330. for (i = 0; guard < buf; i++, guard++) {
  331. if (*guard != GUARD_BYTE) {
  332. ERROR(tdev, "guard byte[%d] %d (not %d)\n",
  333. i, *guard, GUARD_BYTE);
  334. return -EINVAL;
  335. }
  336. }
  337. return 0;
  338. }
  339. static int simple_check_buf(struct usbtest_dev *tdev, struct urb *urb)
  340. {
  341. unsigned i;
  342. u8 expected;
  343. u8 *buf = urb->transfer_buffer;
  344. unsigned len = urb->actual_length;
  345. unsigned maxpacket = get_maxpacket(urb->dev, urb->pipe);
  346. int ret = check_guard_bytes(tdev, urb);
  347. if (ret)
  348. return ret;
  349. for (i = 0; i < len; i++, buf++) {
  350. switch (pattern) {
  351. /* all-zeroes has no synchronization issues */
  352. case 0:
  353. expected = 0;
  354. break;
  355. /* mod63 stays in sync with short-terminated transfers,
  356. * or otherwise when host and gadget agree on how large
  357. * each usb transfer request should be. resync is done
  358. * with set_interface or set_config.
  359. */
  360. case 1: /* mod63 */
  361. expected = (i % maxpacket) % 63;
  362. break;
  363. /* always fail unsupported patterns */
  364. default:
  365. expected = !*buf;
  366. break;
  367. }
  368. if (*buf == expected)
  369. continue;
  370. ERROR(tdev, "buf[%d] = %d (not %d)\n", i, *buf, expected);
  371. return -EINVAL;
  372. }
  373. return 0;
  374. }
  375. static void simple_free_urb(struct urb *urb)
  376. {
  377. unsigned long offset = buffer_offset(urb->transfer_buffer);
  378. if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
  379. usb_free_coherent(
  380. urb->dev,
  381. urb->transfer_buffer_length + offset,
  382. urb->transfer_buffer - offset,
  383. urb->transfer_dma - offset);
  384. else
  385. kfree(urb->transfer_buffer - offset);
  386. usb_free_urb(urb);
  387. }
  388. static int simple_io(
  389. struct usbtest_dev *tdev,
  390. struct urb *urb,
  391. int iterations,
  392. int vary,
  393. int expected,
  394. const char *label
  395. )
  396. {
  397. struct usb_device *udev = urb->dev;
  398. int max = urb->transfer_buffer_length;
  399. struct completion completion;
  400. int retval = 0;
  401. unsigned long expire;
  402. urb->context = &completion;
  403. while (retval == 0 && iterations-- > 0) {
  404. init_completion(&completion);
  405. if (usb_pipeout(urb->pipe)) {
  406. simple_fill_buf(urb);
  407. urb->transfer_flags |= URB_ZERO_PACKET;
  408. }
  409. retval = usb_submit_urb(urb, GFP_KERNEL);
  410. if (retval != 0)
  411. break;
  412. expire = msecs_to_jiffies(SIMPLE_IO_TIMEOUT);
  413. if (!wait_for_completion_timeout(&completion, expire)) {
  414. usb_kill_urb(urb);
  415. retval = (urb->status == -ENOENT ?
  416. -ETIMEDOUT : urb->status);
  417. } else {
  418. retval = urb->status;
  419. }
  420. urb->dev = udev;
  421. if (retval == 0 && usb_pipein(urb->pipe))
  422. retval = simple_check_buf(tdev, urb);
  423. if (vary) {
  424. int len = urb->transfer_buffer_length;
  425. len += vary;
  426. len %= max;
  427. if (len == 0)
  428. len = (vary < max) ? vary : max;
  429. urb->transfer_buffer_length = len;
  430. }
  431. /* FIXME if endpoint halted, clear halt (and log) */
  432. }
  433. urb->transfer_buffer_length = max;
  434. if (expected != retval)
  435. dev_err(&udev->dev,
  436. "%s failed, iterations left %d, status %d (not %d)\n",
  437. label, iterations, retval, expected);
  438. return retval;
  439. }
  440. /*-------------------------------------------------------------------------*/
  441. /* We use scatterlist primitives to test queued I/O.
  442. * Yes, this also tests the scatterlist primitives.
  443. */
  444. static void free_sglist(struct scatterlist *sg, int nents)
  445. {
  446. unsigned i;
  447. if (!sg)
  448. return;
  449. for (i = 0; i < nents; i++) {
  450. if (!sg_page(&sg[i]))
  451. continue;
  452. kfree(sg_virt(&sg[i]));
  453. }
  454. kfree(sg);
  455. }
  456. static struct scatterlist *
  457. alloc_sglist(int nents, int max, int vary, struct usbtest_dev *dev, int pipe)
  458. {
  459. struct scatterlist *sg;
  460. unsigned int n_size = 0;
  461. unsigned i;
  462. unsigned size = max;
  463. unsigned maxpacket =
  464. get_maxpacket(interface_to_usbdev(dev->intf), pipe);
  465. if (max == 0)
  466. return NULL;
  467. sg = kmalloc_array(nents, sizeof(*sg), GFP_KERNEL);
  468. if (!sg)
  469. return NULL;
  470. sg_init_table(sg, nents);
  471. for (i = 0; i < nents; i++) {
  472. char *buf;
  473. unsigned j;
  474. buf = kzalloc(size, GFP_KERNEL);
  475. if (!buf) {
  476. free_sglist(sg, i);
  477. return NULL;
  478. }
  479. /* kmalloc pages are always physically contiguous! */
  480. sg_set_buf(&sg[i], buf, size);
  481. switch (pattern) {
  482. case 0:
  483. /* already zeroed */
  484. break;
  485. case 1:
  486. for (j = 0; j < size; j++)
  487. *buf++ = (u8) (((j + n_size) % maxpacket) % 63);
  488. n_size += size;
  489. break;
  490. }
  491. if (vary) {
  492. size += vary;
  493. size %= max;
  494. if (size == 0)
  495. size = (vary < max) ? vary : max;
  496. }
  497. }
  498. return sg;
  499. }
  500. struct sg_timeout {
  501. struct timer_list timer;
  502. struct usb_sg_request *req;
  503. };
  504. static void sg_timeout(struct timer_list *t)
  505. {
  506. struct sg_timeout *timeout = from_timer(timeout, t, timer);
  507. usb_sg_cancel(timeout->req);
  508. }
  509. static int perform_sglist(
  510. struct usbtest_dev *tdev,
  511. unsigned iterations,
  512. int pipe,
  513. struct usb_sg_request *req,
  514. struct scatterlist *sg,
  515. int nents
  516. )
  517. {
  518. struct usb_device *udev = testdev_to_usbdev(tdev);
  519. int retval = 0;
  520. struct sg_timeout timeout = {
  521. .req = req,
  522. };
  523. timer_setup_on_stack(&timeout.timer, sg_timeout, 0);
  524. while (retval == 0 && iterations-- > 0) {
  525. retval = usb_sg_init(req, udev, pipe,
  526. (udev->speed == USB_SPEED_HIGH)
  527. ? (INTERRUPT_RATE << 3)
  528. : INTERRUPT_RATE,
  529. sg, nents, 0, GFP_KERNEL);
  530. if (retval)
  531. break;
  532. mod_timer(&timeout.timer, jiffies +
  533. msecs_to_jiffies(SIMPLE_IO_TIMEOUT));
  534. usb_sg_wait(req);
  535. if (!del_timer_sync(&timeout.timer))
  536. retval = -ETIMEDOUT;
  537. else
  538. retval = req->status;
  539. destroy_timer_on_stack(&timeout.timer);
  540. /* FIXME check resulting data pattern */
  541. /* FIXME if endpoint halted, clear halt (and log) */
  542. }
  543. /* FIXME for unlink or fault handling tests, don't report
  544. * failure if retval is as we expected ...
  545. */
  546. if (retval)
  547. ERROR(tdev, "perform_sglist failed, "
  548. "iterations left %d, status %d\n",
  549. iterations, retval);
  550. return retval;
  551. }
  552. /*-------------------------------------------------------------------------*/
  553. /* unqueued control message testing
  554. *
  555. * there's a nice set of device functional requirements in chapter 9 of the
  556. * usb 2.0 spec, which we can apply to ANY device, even ones that don't use
  557. * special test firmware.
  558. *
  559. * we know the device is configured (or suspended) by the time it's visible
  560. * through usbfs. we can't change that, so we won't test enumeration (which
  561. * worked 'well enough' to get here, this time), power management (ditto),
  562. * or remote wakeup (which needs human interaction).
  563. */
  564. static unsigned realworld = 1;
  565. module_param(realworld, uint, 0);
  566. MODULE_PARM_DESC(realworld, "clear to demand stricter spec compliance");
  567. static int get_altsetting(struct usbtest_dev *dev)
  568. {
  569. struct usb_interface *iface = dev->intf;
  570. struct usb_device *udev = interface_to_usbdev(iface);
  571. int retval;
  572. retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  573. USB_REQ_GET_INTERFACE, USB_DIR_IN|USB_RECIP_INTERFACE,
  574. 0, iface->altsetting[0].desc.bInterfaceNumber,
  575. dev->buf, 1, USB_CTRL_GET_TIMEOUT);
  576. switch (retval) {
  577. case 1:
  578. return dev->buf[0];
  579. case 0:
  580. retval = -ERANGE;
  581. fallthrough;
  582. default:
  583. return retval;
  584. }
  585. }
  586. static int set_altsetting(struct usbtest_dev *dev, int alternate)
  587. {
  588. struct usb_interface *iface = dev->intf;
  589. struct usb_device *udev;
  590. if (alternate < 0 || alternate >= 256)
  591. return -EINVAL;
  592. udev = interface_to_usbdev(iface);
  593. return usb_set_interface(udev,
  594. iface->altsetting[0].desc.bInterfaceNumber,
  595. alternate);
  596. }
  597. static int is_good_config(struct usbtest_dev *tdev, int len)
  598. {
  599. struct usb_config_descriptor *config;
  600. if (len < sizeof(*config))
  601. return 0;
  602. config = (struct usb_config_descriptor *) tdev->buf;
  603. switch (config->bDescriptorType) {
  604. case USB_DT_CONFIG:
  605. case USB_DT_OTHER_SPEED_CONFIG:
  606. if (config->bLength != 9) {
  607. ERROR(tdev, "bogus config descriptor length\n");
  608. return 0;
  609. }
  610. /* this bit 'must be 1' but often isn't */
  611. if (!realworld && !(config->bmAttributes & 0x80)) {
  612. ERROR(tdev, "high bit of config attributes not set\n");
  613. return 0;
  614. }
  615. if (config->bmAttributes & 0x1f) { /* reserved == 0 */
  616. ERROR(tdev, "reserved config bits set\n");
  617. return 0;
  618. }
  619. break;
  620. default:
  621. return 0;
  622. }
  623. if (le16_to_cpu(config->wTotalLength) == len) /* read it all */
  624. return 1;
  625. if (le16_to_cpu(config->wTotalLength) >= TBUF_SIZE) /* max partial read */
  626. return 1;
  627. ERROR(tdev, "bogus config descriptor read size\n");
  628. return 0;
  629. }
  630. static int is_good_ext(struct usbtest_dev *tdev, u8 *buf)
  631. {
  632. struct usb_ext_cap_descriptor *ext;
  633. u32 attr;
  634. ext = (struct usb_ext_cap_descriptor *) buf;
  635. if (ext->bLength != USB_DT_USB_EXT_CAP_SIZE) {
  636. ERROR(tdev, "bogus usb 2.0 extension descriptor length\n");
  637. return 0;
  638. }
  639. attr = le32_to_cpu(ext->bmAttributes);
  640. /* bits[1:15] is used and others are reserved */
  641. if (attr & ~0xfffe) { /* reserved == 0 */
  642. ERROR(tdev, "reserved bits set\n");
  643. return 0;
  644. }
  645. return 1;
  646. }
  647. static int is_good_ss_cap(struct usbtest_dev *tdev, u8 *buf)
  648. {
  649. struct usb_ss_cap_descriptor *ss;
  650. ss = (struct usb_ss_cap_descriptor *) buf;
  651. if (ss->bLength != USB_DT_USB_SS_CAP_SIZE) {
  652. ERROR(tdev, "bogus superspeed device capability descriptor length\n");
  653. return 0;
  654. }
  655. /*
  656. * only bit[1] of bmAttributes is used for LTM and others are
  657. * reserved
  658. */
  659. if (ss->bmAttributes & ~0x02) { /* reserved == 0 */
  660. ERROR(tdev, "reserved bits set in bmAttributes\n");
  661. return 0;
  662. }
  663. /* bits[0:3] of wSpeedSupported is used and others are reserved */
  664. if (le16_to_cpu(ss->wSpeedSupported) & ~0x0f) { /* reserved == 0 */
  665. ERROR(tdev, "reserved bits set in wSpeedSupported\n");
  666. return 0;
  667. }
  668. return 1;
  669. }
  670. static int is_good_con_id(struct usbtest_dev *tdev, u8 *buf)
  671. {
  672. struct usb_ss_container_id_descriptor *con_id;
  673. con_id = (struct usb_ss_container_id_descriptor *) buf;
  674. if (con_id->bLength != USB_DT_USB_SS_CONTN_ID_SIZE) {
  675. ERROR(tdev, "bogus container id descriptor length\n");
  676. return 0;
  677. }
  678. if (con_id->bReserved) { /* reserved == 0 */
  679. ERROR(tdev, "reserved bits set\n");
  680. return 0;
  681. }
  682. return 1;
  683. }
  684. /* sanity test for standard requests working with usb_control_mesg() and some
  685. * of the utility functions which use it.
  686. *
  687. * this doesn't test how endpoint halts behave or data toggles get set, since
  688. * we won't do I/O to bulk/interrupt endpoints here (which is how to change
  689. * halt or toggle). toggle testing is impractical without support from hcds.
  690. *
  691. * this avoids failing devices linux would normally work with, by not testing
  692. * config/altsetting operations for devices that only support their defaults.
  693. * such devices rarely support those needless operations.
  694. *
  695. * NOTE that since this is a sanity test, it's not examining boundary cases
  696. * to see if usbcore, hcd, and device all behave right. such testing would
  697. * involve varied read sizes and other operation sequences.
  698. */
  699. static int ch9_postconfig(struct usbtest_dev *dev)
  700. {
  701. struct usb_interface *iface = dev->intf;
  702. struct usb_device *udev = interface_to_usbdev(iface);
  703. int i, alt, retval;
  704. /* [9.2.3] if there's more than one altsetting, we need to be able to
  705. * set and get each one. mostly trusts the descriptors from usbcore.
  706. */
  707. for (i = 0; i < iface->num_altsetting; i++) {
  708. /* 9.2.3 constrains the range here */
  709. alt = iface->altsetting[i].desc.bAlternateSetting;
  710. if (alt < 0 || alt >= iface->num_altsetting) {
  711. dev_err(&iface->dev,
  712. "invalid alt [%d].bAltSetting = %d\n",
  713. i, alt);
  714. }
  715. /* [real world] get/set unimplemented if there's only one */
  716. if (realworld && iface->num_altsetting == 1)
  717. continue;
  718. /* [9.4.10] set_interface */
  719. retval = set_altsetting(dev, alt);
  720. if (retval) {
  721. dev_err(&iface->dev, "can't set_interface = %d, %d\n",
  722. alt, retval);
  723. return retval;
  724. }
  725. /* [9.4.4] get_interface always works */
  726. retval = get_altsetting(dev);
  727. if (retval != alt) {
  728. dev_err(&iface->dev, "get alt should be %d, was %d\n",
  729. alt, retval);
  730. return (retval < 0) ? retval : -EDOM;
  731. }
  732. }
  733. /* [real world] get_config unimplemented if there's only one */
  734. if (!realworld || udev->descriptor.bNumConfigurations != 1) {
  735. int expected = udev->actconfig->desc.bConfigurationValue;
  736. /* [9.4.2] get_configuration always works
  737. * ... although some cheap devices (like one TI Hub I've got)
  738. * won't return config descriptors except before set_config.
  739. */
  740. retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  741. USB_REQ_GET_CONFIGURATION,
  742. USB_DIR_IN | USB_RECIP_DEVICE,
  743. 0, 0, dev->buf, 1, USB_CTRL_GET_TIMEOUT);
  744. if (retval != 1 || dev->buf[0] != expected) {
  745. dev_err(&iface->dev, "get config --> %d %d (1 %d)\n",
  746. retval, dev->buf[0], expected);
  747. return (retval < 0) ? retval : -EDOM;
  748. }
  749. }
  750. /* there's always [9.4.3] a device descriptor [9.6.1] */
  751. retval = usb_get_descriptor(udev, USB_DT_DEVICE, 0,
  752. dev->buf, sizeof(udev->descriptor));
  753. if (retval != sizeof(udev->descriptor)) {
  754. dev_err(&iface->dev, "dev descriptor --> %d\n", retval);
  755. return (retval < 0) ? retval : -EDOM;
  756. }
  757. /*
  758. * there's always [9.4.3] a bos device descriptor [9.6.2] in USB
  759. * 3.0 spec
  760. */
  761. if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0210) {
  762. struct usb_bos_descriptor *bos = NULL;
  763. struct usb_dev_cap_header *header = NULL;
  764. unsigned total, num, length;
  765. u8 *buf;
  766. retval = usb_get_descriptor(udev, USB_DT_BOS, 0, dev->buf,
  767. sizeof(*udev->bos->desc));
  768. if (retval != sizeof(*udev->bos->desc)) {
  769. dev_err(&iface->dev, "bos descriptor --> %d\n", retval);
  770. return (retval < 0) ? retval : -EDOM;
  771. }
  772. bos = (struct usb_bos_descriptor *)dev->buf;
  773. total = le16_to_cpu(bos->wTotalLength);
  774. num = bos->bNumDeviceCaps;
  775. if (total > TBUF_SIZE)
  776. total = TBUF_SIZE;
  777. /*
  778. * get generic device-level capability descriptors [9.6.2]
  779. * in USB 3.0 spec
  780. */
  781. retval = usb_get_descriptor(udev, USB_DT_BOS, 0, dev->buf,
  782. total);
  783. if (retval != total) {
  784. dev_err(&iface->dev, "bos descriptor set --> %d\n",
  785. retval);
  786. return (retval < 0) ? retval : -EDOM;
  787. }
  788. length = sizeof(*udev->bos->desc);
  789. buf = dev->buf;
  790. for (i = 0; i < num; i++) {
  791. buf += length;
  792. if (buf + sizeof(struct usb_dev_cap_header) >
  793. dev->buf + total)
  794. break;
  795. header = (struct usb_dev_cap_header *)buf;
  796. length = header->bLength;
  797. if (header->bDescriptorType !=
  798. USB_DT_DEVICE_CAPABILITY) {
  799. dev_warn(&udev->dev, "not device capability descriptor, skip\n");
  800. continue;
  801. }
  802. switch (header->bDevCapabilityType) {
  803. case USB_CAP_TYPE_EXT:
  804. if (buf + USB_DT_USB_EXT_CAP_SIZE >
  805. dev->buf + total ||
  806. !is_good_ext(dev, buf)) {
  807. dev_err(&iface->dev, "bogus usb 2.0 extension descriptor\n");
  808. return -EDOM;
  809. }
  810. break;
  811. case USB_SS_CAP_TYPE:
  812. if (buf + USB_DT_USB_SS_CAP_SIZE >
  813. dev->buf + total ||
  814. !is_good_ss_cap(dev, buf)) {
  815. dev_err(&iface->dev, "bogus superspeed device capability descriptor\n");
  816. return -EDOM;
  817. }
  818. break;
  819. case CONTAINER_ID_TYPE:
  820. if (buf + USB_DT_USB_SS_CONTN_ID_SIZE >
  821. dev->buf + total ||
  822. !is_good_con_id(dev, buf)) {
  823. dev_err(&iface->dev, "bogus container id descriptor\n");
  824. return -EDOM;
  825. }
  826. break;
  827. default:
  828. break;
  829. }
  830. }
  831. }
  832. /* there's always [9.4.3] at least one config descriptor [9.6.3] */
  833. for (i = 0; i < udev->descriptor.bNumConfigurations; i++) {
  834. retval = usb_get_descriptor(udev, USB_DT_CONFIG, i,
  835. dev->buf, TBUF_SIZE);
  836. if (!is_good_config(dev, retval)) {
  837. dev_err(&iface->dev,
  838. "config [%d] descriptor --> %d\n",
  839. i, retval);
  840. return (retval < 0) ? retval : -EDOM;
  841. }
  842. /* FIXME cross-checking udev->config[i] to make sure usbcore
  843. * parsed it right (etc) would be good testing paranoia
  844. */
  845. }
  846. /* and sometimes [9.2.6.6] speed dependent descriptors */
  847. if (le16_to_cpu(udev->descriptor.bcdUSB) == 0x0200) {
  848. struct usb_qualifier_descriptor *d = NULL;
  849. /* device qualifier [9.6.2] */
  850. retval = usb_get_descriptor(udev,
  851. USB_DT_DEVICE_QUALIFIER, 0, dev->buf,
  852. sizeof(struct usb_qualifier_descriptor));
  853. if (retval == -EPIPE) {
  854. if (udev->speed == USB_SPEED_HIGH) {
  855. dev_err(&iface->dev,
  856. "hs dev qualifier --> %d\n",
  857. retval);
  858. return retval;
  859. }
  860. /* usb2.0 but not high-speed capable; fine */
  861. } else if (retval != sizeof(struct usb_qualifier_descriptor)) {
  862. dev_err(&iface->dev, "dev qualifier --> %d\n", retval);
  863. return (retval < 0) ? retval : -EDOM;
  864. } else
  865. d = (struct usb_qualifier_descriptor *) dev->buf;
  866. /* might not have [9.6.2] any other-speed configs [9.6.4] */
  867. if (d) {
  868. unsigned max = d->bNumConfigurations;
  869. for (i = 0; i < max; i++) {
  870. retval = usb_get_descriptor(udev,
  871. USB_DT_OTHER_SPEED_CONFIG, i,
  872. dev->buf, TBUF_SIZE);
  873. if (!is_good_config(dev, retval)) {
  874. dev_err(&iface->dev,
  875. "other speed config --> %d\n",
  876. retval);
  877. return (retval < 0) ? retval : -EDOM;
  878. }
  879. }
  880. }
  881. }
  882. /* FIXME fetch strings from at least the device descriptor */
  883. /* [9.4.5] get_status always works */
  884. retval = usb_get_std_status(udev, USB_RECIP_DEVICE, 0, dev->buf);
  885. if (retval) {
  886. dev_err(&iface->dev, "get dev status --> %d\n", retval);
  887. return retval;
  888. }
  889. /* FIXME configuration.bmAttributes says if we could try to set/clear
  890. * the device's remote wakeup feature ... if we can, test that here
  891. */
  892. retval = usb_get_std_status(udev, USB_RECIP_INTERFACE,
  893. iface->altsetting[0].desc.bInterfaceNumber, dev->buf);
  894. if (retval) {
  895. dev_err(&iface->dev, "get interface status --> %d\n", retval);
  896. return retval;
  897. }
  898. /* FIXME get status for each endpoint in the interface */
  899. return 0;
  900. }
  901. /*-------------------------------------------------------------------------*/
  902. /* use ch9 requests to test whether:
  903. * (a) queues work for control, keeping N subtests queued and
  904. * active (auto-resubmit) for M loops through the queue.
  905. * (b) protocol stalls (control-only) will autorecover.
  906. * it's not like bulk/intr; no halt clearing.
  907. * (c) short control reads are reported and handled.
  908. * (d) queues are always processed in-order
  909. */
  910. struct ctrl_ctx {
  911. spinlock_t lock;
  912. struct usbtest_dev *dev;
  913. struct completion complete;
  914. unsigned count;
  915. unsigned pending;
  916. int status;
  917. struct urb **urb;
  918. struct usbtest_param_32 *param;
  919. int last;
  920. };
  921. #define NUM_SUBCASES 16 /* how many test subcases here? */
  922. struct subcase {
  923. struct usb_ctrlrequest setup;
  924. int number;
  925. int expected;
  926. };
  927. static void ctrl_complete(struct urb *urb)
  928. {
  929. struct ctrl_ctx *ctx = urb->context;
  930. struct usb_ctrlrequest *reqp;
  931. struct subcase *subcase;
  932. int status = urb->status;
  933. unsigned long flags;
  934. reqp = (struct usb_ctrlrequest *)urb->setup_packet;
  935. subcase = container_of(reqp, struct subcase, setup);
  936. spin_lock_irqsave(&ctx->lock, flags);
  937. ctx->count--;
  938. ctx->pending--;
  939. /* queue must transfer and complete in fifo order, unless
  940. * usb_unlink_urb() is used to unlink something not at the
  941. * physical queue head (not tested).
  942. */
  943. if (subcase->number > 0) {
  944. if ((subcase->number - ctx->last) != 1) {
  945. ERROR(ctx->dev,
  946. "subcase %d completed out of order, last %d\n",
  947. subcase->number, ctx->last);
  948. status = -EDOM;
  949. ctx->last = subcase->number;
  950. goto error;
  951. }
  952. }
  953. ctx->last = subcase->number;
  954. /* succeed or fault in only one way? */
  955. if (status == subcase->expected)
  956. status = 0;
  957. /* async unlink for cleanup? */
  958. else if (status != -ECONNRESET) {
  959. /* some faults are allowed, not required */
  960. if (subcase->expected > 0 && (
  961. ((status == -subcase->expected /* happened */
  962. || status == 0)))) /* didn't */
  963. status = 0;
  964. /* sometimes more than one fault is allowed */
  965. else if (subcase->number == 12 && status == -EPIPE)
  966. status = 0;
  967. else
  968. ERROR(ctx->dev, "subtest %d error, status %d\n",
  969. subcase->number, status);
  970. }
  971. /* unexpected status codes mean errors; ideally, in hardware */
  972. if (status) {
  973. error:
  974. if (ctx->status == 0) {
  975. int i;
  976. ctx->status = status;
  977. ERROR(ctx->dev, "control queue %02x.%02x, err %d, "
  978. "%d left, subcase %d, len %d/%d\n",
  979. reqp->bRequestType, reqp->bRequest,
  980. status, ctx->count, subcase->number,
  981. urb->actual_length,
  982. urb->transfer_buffer_length);
  983. /* FIXME this "unlink everything" exit route should
  984. * be a separate test case.
  985. */
  986. /* unlink whatever's still pending */
  987. for (i = 1; i < ctx->param->sglen; i++) {
  988. struct urb *u = ctx->urb[
  989. (i + subcase->number)
  990. % ctx->param->sglen];
  991. if (u == urb || !u->dev)
  992. continue;
  993. spin_unlock(&ctx->lock);
  994. status = usb_unlink_urb(u);
  995. spin_lock(&ctx->lock);
  996. switch (status) {
  997. case -EINPROGRESS:
  998. case -EBUSY:
  999. case -EIDRM:
  1000. continue;
  1001. default:
  1002. ERROR(ctx->dev, "urb unlink --> %d\n",
  1003. status);
  1004. }
  1005. }
  1006. status = ctx->status;
  1007. }
  1008. }
  1009. /* resubmit if we need to, else mark this as done */
  1010. if ((status == 0) && (ctx->pending < ctx->count)) {
  1011. status = usb_submit_urb(urb, GFP_ATOMIC);
  1012. if (status != 0) {
  1013. ERROR(ctx->dev,
  1014. "can't resubmit ctrl %02x.%02x, err %d\n",
  1015. reqp->bRequestType, reqp->bRequest, status);
  1016. urb->dev = NULL;
  1017. } else
  1018. ctx->pending++;
  1019. } else
  1020. urb->dev = NULL;
  1021. /* signal completion when nothing's queued */
  1022. if (ctx->pending == 0)
  1023. complete(&ctx->complete);
  1024. spin_unlock_irqrestore(&ctx->lock, flags);
  1025. }
  1026. static int
  1027. test_ctrl_queue(struct usbtest_dev *dev, struct usbtest_param_32 *param)
  1028. {
  1029. struct usb_device *udev = testdev_to_usbdev(dev);
  1030. struct urb **urb;
  1031. struct ctrl_ctx context;
  1032. int i;
  1033. if (param->sglen == 0 || param->iterations > UINT_MAX / param->sglen)
  1034. return -EOPNOTSUPP;
  1035. spin_lock_init(&context.lock);
  1036. context.dev = dev;
  1037. init_completion(&context.complete);
  1038. context.count = param->sglen * param->iterations;
  1039. context.pending = 0;
  1040. context.status = -ENOMEM;
  1041. context.param = param;
  1042. context.last = -1;
  1043. /* allocate and init the urbs we'll queue.
  1044. * as with bulk/intr sglists, sglen is the queue depth; it also
  1045. * controls which subtests run (more tests than sglen) or rerun.
  1046. */
  1047. urb = kcalloc(param->sglen, sizeof(struct urb *), GFP_KERNEL);
  1048. if (!urb)
  1049. return -ENOMEM;
  1050. for (i = 0; i < param->sglen; i++) {
  1051. int pipe = usb_rcvctrlpipe(udev, 0);
  1052. unsigned len;
  1053. struct urb *u;
  1054. struct usb_ctrlrequest req;
  1055. struct subcase *reqp;
  1056. /* sign of this variable means:
  1057. * -: tested code must return this (negative) error code
  1058. * +: tested code may return this (negative too) error code
  1059. */
  1060. int expected = 0;
  1061. /* requests here are mostly expected to succeed on any
  1062. * device, but some are chosen to trigger protocol stalls
  1063. * or short reads.
  1064. */
  1065. memset(&req, 0, sizeof(req));
  1066. req.bRequest = USB_REQ_GET_DESCRIPTOR;
  1067. req.bRequestType = USB_DIR_IN|USB_RECIP_DEVICE;
  1068. switch (i % NUM_SUBCASES) {
  1069. case 0: /* get device descriptor */
  1070. req.wValue = cpu_to_le16(USB_DT_DEVICE << 8);
  1071. len = sizeof(struct usb_device_descriptor);
  1072. break;
  1073. case 1: /* get first config descriptor (only) */
  1074. req.wValue = cpu_to_le16((USB_DT_CONFIG << 8) | 0);
  1075. len = sizeof(struct usb_config_descriptor);
  1076. break;
  1077. case 2: /* get altsetting (OFTEN STALLS) */
  1078. req.bRequest = USB_REQ_GET_INTERFACE;
  1079. req.bRequestType = USB_DIR_IN|USB_RECIP_INTERFACE;
  1080. /* index = 0 means first interface */
  1081. len = 1;
  1082. expected = EPIPE;
  1083. break;
  1084. case 3: /* get interface status */
  1085. req.bRequest = USB_REQ_GET_STATUS;
  1086. req.bRequestType = USB_DIR_IN|USB_RECIP_INTERFACE;
  1087. /* interface 0 */
  1088. len = 2;
  1089. break;
  1090. case 4: /* get device status */
  1091. req.bRequest = USB_REQ_GET_STATUS;
  1092. req.bRequestType = USB_DIR_IN|USB_RECIP_DEVICE;
  1093. len = 2;
  1094. break;
  1095. case 5: /* get device qualifier (MAY STALL) */
  1096. req.wValue = cpu_to_le16 (USB_DT_DEVICE_QUALIFIER << 8);
  1097. len = sizeof(struct usb_qualifier_descriptor);
  1098. if (udev->speed != USB_SPEED_HIGH)
  1099. expected = EPIPE;
  1100. break;
  1101. case 6: /* get first config descriptor, plus interface */
  1102. req.wValue = cpu_to_le16((USB_DT_CONFIG << 8) | 0);
  1103. len = sizeof(struct usb_config_descriptor);
  1104. len += sizeof(struct usb_interface_descriptor);
  1105. break;
  1106. case 7: /* get interface descriptor (ALWAYS STALLS) */
  1107. req.wValue = cpu_to_le16 (USB_DT_INTERFACE << 8);
  1108. /* interface == 0 */
  1109. len = sizeof(struct usb_interface_descriptor);
  1110. expected = -EPIPE;
  1111. break;
  1112. /* NOTE: two consecutive stalls in the queue here.
  1113. * that tests fault recovery a bit more aggressively. */
  1114. case 8: /* clear endpoint halt (MAY STALL) */
  1115. req.bRequest = USB_REQ_CLEAR_FEATURE;
  1116. req.bRequestType = USB_RECIP_ENDPOINT;
  1117. /* wValue 0 == ep halt */
  1118. /* wIndex 0 == ep0 (shouldn't halt!) */
  1119. len = 0;
  1120. pipe = usb_sndctrlpipe(udev, 0);
  1121. expected = EPIPE;
  1122. break;
  1123. case 9: /* get endpoint status */
  1124. req.bRequest = USB_REQ_GET_STATUS;
  1125. req.bRequestType = USB_DIR_IN|USB_RECIP_ENDPOINT;
  1126. /* endpoint 0 */
  1127. len = 2;
  1128. break;
  1129. case 10: /* trigger short read (EREMOTEIO) */
  1130. req.wValue = cpu_to_le16((USB_DT_CONFIG << 8) | 0);
  1131. len = 1024;
  1132. expected = -EREMOTEIO;
  1133. break;
  1134. /* NOTE: two consecutive _different_ faults in the queue. */
  1135. case 11: /* get endpoint descriptor (ALWAYS STALLS) */
  1136. req.wValue = cpu_to_le16(USB_DT_ENDPOINT << 8);
  1137. /* endpoint == 0 */
  1138. len = sizeof(struct usb_interface_descriptor);
  1139. expected = EPIPE;
  1140. break;
  1141. /* NOTE: sometimes even a third fault in the queue! */
  1142. case 12: /* get string 0 descriptor (MAY STALL) */
  1143. req.wValue = cpu_to_le16(USB_DT_STRING << 8);
  1144. /* string == 0, for language IDs */
  1145. len = sizeof(struct usb_interface_descriptor);
  1146. /* may succeed when > 4 languages */
  1147. expected = EREMOTEIO; /* or EPIPE, if no strings */
  1148. break;
  1149. case 13: /* short read, resembling case 10 */
  1150. req.wValue = cpu_to_le16((USB_DT_CONFIG << 8) | 0);
  1151. /* last data packet "should" be DATA1, not DATA0 */
  1152. if (udev->speed == USB_SPEED_SUPER)
  1153. len = 1024 - 512;
  1154. else
  1155. len = 1024 - udev->descriptor.bMaxPacketSize0;
  1156. expected = -EREMOTEIO;
  1157. break;
  1158. case 14: /* short read; try to fill the last packet */
  1159. req.wValue = cpu_to_le16((USB_DT_DEVICE << 8) | 0);
  1160. /* device descriptor size == 18 bytes */
  1161. len = udev->descriptor.bMaxPacketSize0;
  1162. if (udev->speed == USB_SPEED_SUPER)
  1163. len = 512;
  1164. switch (len) {
  1165. case 8:
  1166. len = 24;
  1167. break;
  1168. case 16:
  1169. len = 32;
  1170. break;
  1171. }
  1172. expected = -EREMOTEIO;
  1173. break;
  1174. case 15:
  1175. req.wValue = cpu_to_le16(USB_DT_BOS << 8);
  1176. if (udev->bos)
  1177. len = le16_to_cpu(udev->bos->desc->wTotalLength);
  1178. else
  1179. len = sizeof(struct usb_bos_descriptor);
  1180. if (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0201)
  1181. expected = -EPIPE;
  1182. break;
  1183. default:
  1184. ERROR(dev, "bogus number of ctrl queue testcases!\n");
  1185. context.status = -EINVAL;
  1186. goto cleanup;
  1187. }
  1188. req.wLength = cpu_to_le16(len);
  1189. urb[i] = u = simple_alloc_urb(udev, pipe, len, 0);
  1190. if (!u)
  1191. goto cleanup;
  1192. reqp = kmalloc(sizeof(*reqp), GFP_KERNEL);
  1193. if (!reqp)
  1194. goto cleanup;
  1195. reqp->setup = req;
  1196. reqp->number = i % NUM_SUBCASES;
  1197. reqp->expected = expected;
  1198. u->setup_packet = (char *) &reqp->setup;
  1199. u->context = &context;
  1200. u->complete = ctrl_complete;
  1201. }
  1202. /* queue the urbs */
  1203. context.urb = urb;
  1204. spin_lock_irq(&context.lock);
  1205. for (i = 0; i < param->sglen; i++) {
  1206. context.status = usb_submit_urb(urb[i], GFP_ATOMIC);
  1207. if (context.status != 0) {
  1208. ERROR(dev, "can't submit urb[%d], status %d\n",
  1209. i, context.status);
  1210. context.count = context.pending;
  1211. break;
  1212. }
  1213. context.pending++;
  1214. }
  1215. spin_unlock_irq(&context.lock);
  1216. /* FIXME set timer and time out; provide a disconnect hook */
  1217. /* wait for the last one to complete */
  1218. if (context.pending > 0)
  1219. wait_for_completion(&context.complete);
  1220. cleanup:
  1221. for (i = 0; i < param->sglen; i++) {
  1222. if (!urb[i])
  1223. continue;
  1224. urb[i]->dev = udev;
  1225. kfree(urb[i]->setup_packet);
  1226. simple_free_urb(urb[i]);
  1227. }
  1228. kfree(urb);
  1229. return context.status;
  1230. }
  1231. #undef NUM_SUBCASES
  1232. /*-------------------------------------------------------------------------*/
  1233. static void unlink1_callback(struct urb *urb)
  1234. {
  1235. int status = urb->status;
  1236. /* we "know" -EPIPE (stall) never happens */
  1237. if (!status)
  1238. status = usb_submit_urb(urb, GFP_ATOMIC);
  1239. if (status) {
  1240. urb->status = status;
  1241. complete(urb->context);
  1242. }
  1243. }
  1244. static int unlink1(struct usbtest_dev *dev, int pipe, int size, int async)
  1245. {
  1246. struct urb *urb;
  1247. struct completion completion;
  1248. int retval = 0;
  1249. init_completion(&completion);
  1250. urb = simple_alloc_urb(testdev_to_usbdev(dev), pipe, size, 0);
  1251. if (!urb)
  1252. return -ENOMEM;
  1253. urb->context = &completion;
  1254. urb->complete = unlink1_callback;
  1255. if (usb_pipeout(urb->pipe)) {
  1256. simple_fill_buf(urb);
  1257. urb->transfer_flags |= URB_ZERO_PACKET;
  1258. }
  1259. /* keep the endpoint busy. there are lots of hc/hcd-internal
  1260. * states, and testing should get to all of them over time.
  1261. *
  1262. * FIXME want additional tests for when endpoint is STALLing
  1263. * due to errors, or is just NAKing requests.
  1264. */
  1265. retval = usb_submit_urb(urb, GFP_KERNEL);
  1266. if (retval != 0) {
  1267. dev_err(&dev->intf->dev, "submit fail %d\n", retval);
  1268. return retval;
  1269. }
  1270. /* unlinking that should always work. variable delay tests more
  1271. * hcd states and code paths, even with little other system load.
  1272. */
  1273. msleep(jiffies % (2 * INTERRUPT_RATE));
  1274. if (async) {
  1275. while (!completion_done(&completion)) {
  1276. retval = usb_unlink_urb(urb);
  1277. if (retval == 0 && usb_pipein(urb->pipe))
  1278. retval = simple_check_buf(dev, urb);
  1279. switch (retval) {
  1280. case -EBUSY:
  1281. case -EIDRM:
  1282. /* we can't unlink urbs while they're completing
  1283. * or if they've completed, and we haven't
  1284. * resubmitted. "normal" drivers would prevent
  1285. * resubmission, but since we're testing unlink
  1286. * paths, we can't.
  1287. */
  1288. ERROR(dev, "unlink retry\n");
  1289. continue;
  1290. case 0:
  1291. case -EINPROGRESS:
  1292. break;
  1293. default:
  1294. dev_err(&dev->intf->dev,
  1295. "unlink fail %d\n", retval);
  1296. return retval;
  1297. }
  1298. break;
  1299. }
  1300. } else
  1301. usb_kill_urb(urb);
  1302. wait_for_completion(&completion);
  1303. retval = urb->status;
  1304. simple_free_urb(urb);
  1305. if (async)
  1306. return (retval == -ECONNRESET) ? 0 : retval - 1000;
  1307. else
  1308. return (retval == -ENOENT || retval == -EPERM) ?
  1309. 0 : retval - 2000;
  1310. }
  1311. static int unlink_simple(struct usbtest_dev *dev, int pipe, int len)
  1312. {
  1313. int retval = 0;
  1314. /* test sync and async paths */
  1315. retval = unlink1(dev, pipe, len, 1);
  1316. if (!retval)
  1317. retval = unlink1(dev, pipe, len, 0);
  1318. return retval;
  1319. }
  1320. /*-------------------------------------------------------------------------*/
  1321. struct queued_ctx {
  1322. struct completion complete;
  1323. atomic_t pending;
  1324. unsigned num;
  1325. int status;
  1326. struct urb **urbs;
  1327. };
  1328. static void unlink_queued_callback(struct urb *urb)
  1329. {
  1330. int status = urb->status;
  1331. struct queued_ctx *ctx = urb->context;
  1332. if (ctx->status)
  1333. goto done;
  1334. if (urb == ctx->urbs[ctx->num - 4] || urb == ctx->urbs[ctx->num - 2]) {
  1335. if (status == -ECONNRESET)
  1336. goto done;
  1337. /* What error should we report if the URB completed normally? */
  1338. }
  1339. if (status != 0)
  1340. ctx->status = status;
  1341. done:
  1342. if (atomic_dec_and_test(&ctx->pending))
  1343. complete(&ctx->complete);
  1344. }
  1345. static int unlink_queued(struct usbtest_dev *dev, int pipe, unsigned num,
  1346. unsigned size)
  1347. {
  1348. struct queued_ctx ctx;
  1349. struct usb_device *udev = testdev_to_usbdev(dev);
  1350. void *buf;
  1351. dma_addr_t buf_dma;
  1352. int i;
  1353. int retval = -ENOMEM;
  1354. init_completion(&ctx.complete);
  1355. atomic_set(&ctx.pending, 1); /* One more than the actual value */
  1356. ctx.num = num;
  1357. ctx.status = 0;
  1358. buf = usb_alloc_coherent(udev, size, GFP_KERNEL, &buf_dma);
  1359. if (!buf)
  1360. return retval;
  1361. memset(buf, 0, size);
  1362. /* Allocate and init the urbs we'll queue */
  1363. ctx.urbs = kcalloc(num, sizeof(struct urb *), GFP_KERNEL);
  1364. if (!ctx.urbs)
  1365. goto free_buf;
  1366. for (i = 0; i < num; i++) {
  1367. ctx.urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
  1368. if (!ctx.urbs[i])
  1369. goto free_urbs;
  1370. usb_fill_bulk_urb(ctx.urbs[i], udev, pipe, buf, size,
  1371. unlink_queued_callback, &ctx);
  1372. ctx.urbs[i]->transfer_dma = buf_dma;
  1373. ctx.urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  1374. if (usb_pipeout(ctx.urbs[i]->pipe)) {
  1375. simple_fill_buf(ctx.urbs[i]);
  1376. ctx.urbs[i]->transfer_flags |= URB_ZERO_PACKET;
  1377. }
  1378. }
  1379. /* Submit all the URBs and then unlink URBs num - 4 and num - 2. */
  1380. for (i = 0; i < num; i++) {
  1381. atomic_inc(&ctx.pending);
  1382. retval = usb_submit_urb(ctx.urbs[i], GFP_KERNEL);
  1383. if (retval != 0) {
  1384. dev_err(&dev->intf->dev, "submit urbs[%d] fail %d\n",
  1385. i, retval);
  1386. atomic_dec(&ctx.pending);
  1387. ctx.status = retval;
  1388. break;
  1389. }
  1390. }
  1391. if (i == num) {
  1392. usb_unlink_urb(ctx.urbs[num - 4]);
  1393. usb_unlink_urb(ctx.urbs[num - 2]);
  1394. } else {
  1395. while (--i >= 0)
  1396. usb_unlink_urb(ctx.urbs[i]);
  1397. }
  1398. if (atomic_dec_and_test(&ctx.pending)) /* The extra count */
  1399. complete(&ctx.complete);
  1400. wait_for_completion(&ctx.complete);
  1401. retval = ctx.status;
  1402. free_urbs:
  1403. for (i = 0; i < num; i++)
  1404. usb_free_urb(ctx.urbs[i]);
  1405. kfree(ctx.urbs);
  1406. free_buf:
  1407. usb_free_coherent(udev, size, buf, buf_dma);
  1408. return retval;
  1409. }
  1410. /*-------------------------------------------------------------------------*/
  1411. static int verify_not_halted(struct usbtest_dev *tdev, int ep, struct urb *urb)
  1412. {
  1413. int retval;
  1414. u16 status;
  1415. /* shouldn't look or act halted */
  1416. retval = usb_get_std_status(urb->dev, USB_RECIP_ENDPOINT, ep, &status);
  1417. if (retval < 0) {
  1418. ERROR(tdev, "ep %02x couldn't get no-halt status, %d\n",
  1419. ep, retval);
  1420. return retval;
  1421. }
  1422. if (status != 0) {
  1423. ERROR(tdev, "ep %02x bogus status: %04x != 0\n", ep, status);
  1424. return -EINVAL;
  1425. }
  1426. retval = simple_io(tdev, urb, 1, 0, 0, __func__);
  1427. if (retval != 0)
  1428. return -EINVAL;
  1429. return 0;
  1430. }
  1431. static int verify_halted(struct usbtest_dev *tdev, int ep, struct urb *urb)
  1432. {
  1433. int retval;
  1434. u16 status;
  1435. /* should look and act halted */
  1436. retval = usb_get_std_status(urb->dev, USB_RECIP_ENDPOINT, ep, &status);
  1437. if (retval < 0) {
  1438. ERROR(tdev, "ep %02x couldn't get halt status, %d\n",
  1439. ep, retval);
  1440. return retval;
  1441. }
  1442. if (status != 1) {
  1443. ERROR(tdev, "ep %02x bogus status: %04x != 1\n", ep, status);
  1444. return -EINVAL;
  1445. }
  1446. retval = simple_io(tdev, urb, 1, 0, -EPIPE, __func__);
  1447. if (retval != -EPIPE)
  1448. return -EINVAL;
  1449. retval = simple_io(tdev, urb, 1, 0, -EPIPE, "verify_still_halted");
  1450. if (retval != -EPIPE)
  1451. return -EINVAL;
  1452. return 0;
  1453. }
  1454. static int test_halt(struct usbtest_dev *tdev, int ep, struct urb *urb)
  1455. {
  1456. int retval;
  1457. /* shouldn't look or act halted now */
  1458. retval = verify_not_halted(tdev, ep, urb);
  1459. if (retval < 0)
  1460. return retval;
  1461. /* set halt (protocol test only), verify it worked */
  1462. retval = usb_control_msg(urb->dev, usb_sndctrlpipe(urb->dev, 0),
  1463. USB_REQ_SET_FEATURE, USB_RECIP_ENDPOINT,
  1464. USB_ENDPOINT_HALT, ep,
  1465. NULL, 0, USB_CTRL_SET_TIMEOUT);
  1466. if (retval < 0) {
  1467. ERROR(tdev, "ep %02x couldn't set halt, %d\n", ep, retval);
  1468. return retval;
  1469. }
  1470. retval = verify_halted(tdev, ep, urb);
  1471. if (retval < 0) {
  1472. int ret;
  1473. /* clear halt anyways, else further tests will fail */
  1474. ret = usb_clear_halt(urb->dev, urb->pipe);
  1475. if (ret)
  1476. ERROR(tdev, "ep %02x couldn't clear halt, %d\n",
  1477. ep, ret);
  1478. return retval;
  1479. }
  1480. /* clear halt (tests API + protocol), verify it worked */
  1481. retval = usb_clear_halt(urb->dev, urb->pipe);
  1482. if (retval < 0) {
  1483. ERROR(tdev, "ep %02x couldn't clear halt, %d\n", ep, retval);
  1484. return retval;
  1485. }
  1486. retval = verify_not_halted(tdev, ep, urb);
  1487. if (retval < 0)
  1488. return retval;
  1489. /* NOTE: could also verify SET_INTERFACE clear halts ... */
  1490. return 0;
  1491. }
  1492. static int test_toggle_sync(struct usbtest_dev *tdev, int ep, struct urb *urb)
  1493. {
  1494. int retval;
  1495. /* clear initial data toggle to DATA0 */
  1496. retval = usb_clear_halt(urb->dev, urb->pipe);
  1497. if (retval < 0) {
  1498. ERROR(tdev, "ep %02x couldn't clear halt, %d\n", ep, retval);
  1499. return retval;
  1500. }
  1501. /* transfer 3 data packets, should be DATA0, DATA1, DATA0 */
  1502. retval = simple_io(tdev, urb, 1, 0, 0, __func__);
  1503. if (retval != 0)
  1504. return -EINVAL;
  1505. /* clear halt resets device side data toggle, host should react to it */
  1506. retval = usb_clear_halt(urb->dev, urb->pipe);
  1507. if (retval < 0) {
  1508. ERROR(tdev, "ep %02x couldn't clear halt, %d\n", ep, retval);
  1509. return retval;
  1510. }
  1511. /* host should use DATA0 again after clear halt */
  1512. retval = simple_io(tdev, urb, 1, 0, 0, __func__);
  1513. return retval;
  1514. }
  1515. static int halt_simple(struct usbtest_dev *dev)
  1516. {
  1517. int ep;
  1518. int retval = 0;
  1519. struct urb *urb;
  1520. struct usb_device *udev = testdev_to_usbdev(dev);
  1521. if (udev->speed == USB_SPEED_SUPER)
  1522. urb = simple_alloc_urb(udev, 0, 1024, 0);
  1523. else
  1524. urb = simple_alloc_urb(udev, 0, 512, 0);
  1525. if (urb == NULL)
  1526. return -ENOMEM;
  1527. if (dev->in_pipe) {
  1528. ep = usb_pipeendpoint(dev->in_pipe) | USB_DIR_IN;
  1529. urb->pipe = dev->in_pipe;
  1530. retval = test_halt(dev, ep, urb);
  1531. if (retval < 0)
  1532. goto done;
  1533. }
  1534. if (dev->out_pipe) {
  1535. ep = usb_pipeendpoint(dev->out_pipe);
  1536. urb->pipe = dev->out_pipe;
  1537. retval = test_halt(dev, ep, urb);
  1538. }
  1539. done:
  1540. simple_free_urb(urb);
  1541. return retval;
  1542. }
  1543. static int toggle_sync_simple(struct usbtest_dev *dev)
  1544. {
  1545. int ep;
  1546. int retval = 0;
  1547. struct urb *urb;
  1548. struct usb_device *udev = testdev_to_usbdev(dev);
  1549. unsigned maxp = get_maxpacket(udev, dev->out_pipe);
  1550. /*
  1551. * Create a URB that causes a transfer of uneven amount of data packets
  1552. * This way the clear toggle has an impact on the data toggle sequence.
  1553. * Use 2 maxpacket length packets and one zero packet.
  1554. */
  1555. urb = simple_alloc_urb(udev, 0, 2 * maxp, 0);
  1556. if (urb == NULL)
  1557. return -ENOMEM;
  1558. urb->transfer_flags |= URB_ZERO_PACKET;
  1559. ep = usb_pipeendpoint(dev->out_pipe);
  1560. urb->pipe = dev->out_pipe;
  1561. retval = test_toggle_sync(dev, ep, urb);
  1562. simple_free_urb(urb);
  1563. return retval;
  1564. }
  1565. /*-------------------------------------------------------------------------*/
  1566. /* Control OUT tests use the vendor control requests from Intel's
  1567. * USB 2.0 compliance test device: write a buffer, read it back.
  1568. *
  1569. * Intel's spec only _requires_ that it work for one packet, which
  1570. * is pretty weak. Some HCDs place limits here; most devices will
  1571. * need to be able to handle more than one OUT data packet. We'll
  1572. * try whatever we're told to try.
  1573. */
  1574. static int ctrl_out(struct usbtest_dev *dev,
  1575. unsigned count, unsigned length, unsigned vary, unsigned offset)
  1576. {
  1577. unsigned i, j, len;
  1578. int retval;
  1579. u8 *buf;
  1580. char *what = "?";
  1581. struct usb_device *udev;
  1582. if (length < 1 || length > 0xffff || vary >= length)
  1583. return -EINVAL;
  1584. buf = kmalloc(length + offset, GFP_KERNEL);
  1585. if (!buf)
  1586. return -ENOMEM;
  1587. buf += offset;
  1588. udev = testdev_to_usbdev(dev);
  1589. len = length;
  1590. retval = 0;
  1591. /* NOTE: hardware might well act differently if we pushed it
  1592. * with lots back-to-back queued requests.
  1593. */
  1594. for (i = 0; i < count; i++) {
  1595. /* write patterned data */
  1596. for (j = 0; j < len; j++)
  1597. buf[j] = (u8)(i + j);
  1598. retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  1599. 0x5b, USB_DIR_OUT|USB_TYPE_VENDOR,
  1600. 0, 0, buf, len, USB_CTRL_SET_TIMEOUT);
  1601. if (retval != len) {
  1602. what = "write";
  1603. if (retval >= 0) {
  1604. ERROR(dev, "ctrl_out, wlen %d (expected %d)\n",
  1605. retval, len);
  1606. retval = -EBADMSG;
  1607. }
  1608. break;
  1609. }
  1610. /* read it back -- assuming nothing intervened!! */
  1611. retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  1612. 0x5c, USB_DIR_IN|USB_TYPE_VENDOR,
  1613. 0, 0, buf, len, USB_CTRL_GET_TIMEOUT);
  1614. if (retval != len) {
  1615. what = "read";
  1616. if (retval >= 0) {
  1617. ERROR(dev, "ctrl_out, rlen %d (expected %d)\n",
  1618. retval, len);
  1619. retval = -EBADMSG;
  1620. }
  1621. break;
  1622. }
  1623. /* fail if we can't verify */
  1624. for (j = 0; j < len; j++) {
  1625. if (buf[j] != (u8)(i + j)) {
  1626. ERROR(dev, "ctrl_out, byte %d is %d not %d\n",
  1627. j, buf[j], (u8)(i + j));
  1628. retval = -EBADMSG;
  1629. break;
  1630. }
  1631. }
  1632. if (retval < 0) {
  1633. what = "verify";
  1634. break;
  1635. }
  1636. len += vary;
  1637. /* [real world] the "zero bytes IN" case isn't really used.
  1638. * hardware can easily trip up in this weird case, since its
  1639. * status stage is IN, not OUT like other ep0in transfers.
  1640. */
  1641. if (len > length)
  1642. len = realworld ? 1 : 0;
  1643. }
  1644. if (retval < 0)
  1645. ERROR(dev, "ctrl_out %s failed, code %d, count %d\n",
  1646. what, retval, i);
  1647. kfree(buf - offset);
  1648. return retval;
  1649. }
  1650. /*-------------------------------------------------------------------------*/
  1651. /* ISO/BULK tests ... mimics common usage
  1652. * - buffer length is split into N packets (mostly maxpacket sized)
  1653. * - multi-buffers according to sglen
  1654. */
  1655. struct transfer_context {
  1656. unsigned count;
  1657. unsigned pending;
  1658. spinlock_t lock;
  1659. struct completion done;
  1660. int submit_error;
  1661. unsigned long errors;
  1662. unsigned long packet_count;
  1663. struct usbtest_dev *dev;
  1664. bool is_iso;
  1665. };
  1666. static void complicated_callback(struct urb *urb)
  1667. {
  1668. struct transfer_context *ctx = urb->context;
  1669. unsigned long flags;
  1670. spin_lock_irqsave(&ctx->lock, flags);
  1671. ctx->count--;
  1672. ctx->packet_count += urb->number_of_packets;
  1673. if (urb->error_count > 0)
  1674. ctx->errors += urb->error_count;
  1675. else if (urb->status != 0)
  1676. ctx->errors += (ctx->is_iso ? urb->number_of_packets : 1);
  1677. else if (urb->actual_length != urb->transfer_buffer_length)
  1678. ctx->errors++;
  1679. else if (check_guard_bytes(ctx->dev, urb) != 0)
  1680. ctx->errors++;
  1681. if (urb->status == 0 && ctx->count > (ctx->pending - 1)
  1682. && !ctx->submit_error) {
  1683. int status = usb_submit_urb(urb, GFP_ATOMIC);
  1684. switch (status) {
  1685. case 0:
  1686. goto done;
  1687. default:
  1688. dev_err(&ctx->dev->intf->dev,
  1689. "resubmit err %d\n",
  1690. status);
  1691. fallthrough;
  1692. case -ENODEV: /* disconnected */
  1693. case -ESHUTDOWN: /* endpoint disabled */
  1694. ctx->submit_error = 1;
  1695. break;
  1696. }
  1697. }
  1698. ctx->pending--;
  1699. if (ctx->pending == 0) {
  1700. if (ctx->errors)
  1701. dev_err(&ctx->dev->intf->dev,
  1702. "during the test, %lu errors out of %lu\n",
  1703. ctx->errors, ctx->packet_count);
  1704. complete(&ctx->done);
  1705. }
  1706. done:
  1707. spin_unlock_irqrestore(&ctx->lock, flags);
  1708. }
  1709. static struct urb *iso_alloc_urb(
  1710. struct usb_device *udev,
  1711. int pipe,
  1712. struct usb_endpoint_descriptor *desc,
  1713. long bytes,
  1714. unsigned offset
  1715. )
  1716. {
  1717. struct urb *urb;
  1718. unsigned i, maxp, packets;
  1719. if (bytes < 0 || !desc)
  1720. return NULL;
  1721. maxp = usb_endpoint_maxp(desc);
  1722. if (udev->speed >= USB_SPEED_SUPER)
  1723. maxp *= ss_isoc_get_packet_num(udev, pipe);
  1724. else
  1725. maxp *= usb_endpoint_maxp_mult(desc);
  1726. packets = DIV_ROUND_UP(bytes, maxp);
  1727. urb = usb_alloc_urb(packets, GFP_KERNEL);
  1728. if (!urb)
  1729. return urb;
  1730. urb->dev = udev;
  1731. urb->pipe = pipe;
  1732. urb->number_of_packets = packets;
  1733. urb->transfer_buffer_length = bytes;
  1734. urb->transfer_buffer = usb_alloc_coherent(udev, bytes + offset,
  1735. GFP_KERNEL,
  1736. &urb->transfer_dma);
  1737. if (!urb->transfer_buffer) {
  1738. usb_free_urb(urb);
  1739. return NULL;
  1740. }
  1741. if (offset) {
  1742. memset(urb->transfer_buffer, GUARD_BYTE, offset);
  1743. urb->transfer_buffer += offset;
  1744. urb->transfer_dma += offset;
  1745. }
  1746. /* For inbound transfers use guard byte so that test fails if
  1747. data not correctly copied */
  1748. memset(urb->transfer_buffer,
  1749. usb_pipein(urb->pipe) ? GUARD_BYTE : 0,
  1750. bytes);
  1751. for (i = 0; i < packets; i++) {
  1752. /* here, only the last packet will be short */
  1753. urb->iso_frame_desc[i].length = min((unsigned) bytes, maxp);
  1754. bytes -= urb->iso_frame_desc[i].length;
  1755. urb->iso_frame_desc[i].offset = maxp * i;
  1756. }
  1757. urb->complete = complicated_callback;
  1758. /* urb->context = SET BY CALLER */
  1759. urb->interval = 1 << (desc->bInterval - 1);
  1760. urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
  1761. return urb;
  1762. }
  1763. static int
  1764. test_queue(struct usbtest_dev *dev, struct usbtest_param_32 *param,
  1765. int pipe, struct usb_endpoint_descriptor *desc, unsigned offset)
  1766. {
  1767. struct transfer_context context;
  1768. struct usb_device *udev;
  1769. unsigned i;
  1770. unsigned long packets = 0;
  1771. int status = 0;
  1772. struct urb **urbs;
  1773. if (!param->sglen || param->iterations > UINT_MAX / param->sglen)
  1774. return -EINVAL;
  1775. if (param->sglen > MAX_SGLEN)
  1776. return -EINVAL;
  1777. urbs = kcalloc(param->sglen, sizeof(*urbs), GFP_KERNEL);
  1778. if (!urbs)
  1779. return -ENOMEM;
  1780. memset(&context, 0, sizeof(context));
  1781. context.count = param->iterations * param->sglen;
  1782. context.dev = dev;
  1783. context.is_iso = !!desc;
  1784. init_completion(&context.done);
  1785. spin_lock_init(&context.lock);
  1786. udev = testdev_to_usbdev(dev);
  1787. for (i = 0; i < param->sglen; i++) {
  1788. if (context.is_iso)
  1789. urbs[i] = iso_alloc_urb(udev, pipe, desc,
  1790. param->length, offset);
  1791. else
  1792. urbs[i] = complicated_alloc_urb(udev, pipe,
  1793. param->length, 0);
  1794. if (!urbs[i]) {
  1795. status = -ENOMEM;
  1796. goto fail;
  1797. }
  1798. packets += urbs[i]->number_of_packets;
  1799. urbs[i]->context = &context;
  1800. }
  1801. packets *= param->iterations;
  1802. if (context.is_iso) {
  1803. int transaction_num;
  1804. if (udev->speed >= USB_SPEED_SUPER)
  1805. transaction_num = ss_isoc_get_packet_num(udev, pipe);
  1806. else
  1807. transaction_num = usb_endpoint_maxp_mult(desc);
  1808. dev_info(&dev->intf->dev,
  1809. "iso period %d %sframes, wMaxPacket %d, transactions: %d\n",
  1810. 1 << (desc->bInterval - 1),
  1811. (udev->speed >= USB_SPEED_HIGH) ? "micro" : "",
  1812. usb_endpoint_maxp(desc),
  1813. transaction_num);
  1814. dev_info(&dev->intf->dev,
  1815. "total %lu msec (%lu packets)\n",
  1816. (packets * (1 << (desc->bInterval - 1)))
  1817. / ((udev->speed >= USB_SPEED_HIGH) ? 8 : 1),
  1818. packets);
  1819. }
  1820. spin_lock_irq(&context.lock);
  1821. for (i = 0; i < param->sglen; i++) {
  1822. ++context.pending;
  1823. status = usb_submit_urb(urbs[i], GFP_ATOMIC);
  1824. if (status < 0) {
  1825. ERROR(dev, "submit iso[%d], error %d\n", i, status);
  1826. if (i == 0) {
  1827. spin_unlock_irq(&context.lock);
  1828. goto fail;
  1829. }
  1830. simple_free_urb(urbs[i]);
  1831. urbs[i] = NULL;
  1832. context.pending--;
  1833. context.submit_error = 1;
  1834. break;
  1835. }
  1836. }
  1837. spin_unlock_irq(&context.lock);
  1838. wait_for_completion(&context.done);
  1839. for (i = 0; i < param->sglen; i++) {
  1840. if (urbs[i])
  1841. simple_free_urb(urbs[i]);
  1842. }
  1843. /*
  1844. * Isochronous transfers are expected to fail sometimes. As an
  1845. * arbitrary limit, we will report an error if any submissions
  1846. * fail or if the transfer failure rate is > 10%.
  1847. */
  1848. if (status != 0)
  1849. ;
  1850. else if (context.submit_error)
  1851. status = -EACCES;
  1852. else if (context.errors >
  1853. (context.is_iso ? context.packet_count / 10 : 0))
  1854. status = -EIO;
  1855. kfree(urbs);
  1856. return status;
  1857. fail:
  1858. for (i = 0; i < param->sglen; i++) {
  1859. if (urbs[i])
  1860. simple_free_urb(urbs[i]);
  1861. }
  1862. kfree(urbs);
  1863. return status;
  1864. }
  1865. static int test_unaligned_bulk(
  1866. struct usbtest_dev *tdev,
  1867. int pipe,
  1868. unsigned length,
  1869. int iterations,
  1870. unsigned transfer_flags,
  1871. const char *label)
  1872. {
  1873. int retval;
  1874. struct urb *urb = usbtest_alloc_urb(testdev_to_usbdev(tdev),
  1875. pipe, length, transfer_flags, 1, 0, simple_callback);
  1876. if (!urb)
  1877. return -ENOMEM;
  1878. retval = simple_io(tdev, urb, iterations, 0, 0, label);
  1879. simple_free_urb(urb);
  1880. return retval;
  1881. }
  1882. /* Run tests. */
  1883. static int
  1884. usbtest_do_ioctl(struct usb_interface *intf, struct usbtest_param_32 *param)
  1885. {
  1886. struct usbtest_dev *dev = usb_get_intfdata(intf);
  1887. struct usb_device *udev = testdev_to_usbdev(dev);
  1888. struct urb *urb;
  1889. struct scatterlist *sg;
  1890. struct usb_sg_request req;
  1891. unsigned i;
  1892. int retval = -EOPNOTSUPP;
  1893. if (param->iterations <= 0)
  1894. return -EINVAL;
  1895. if (param->sglen > MAX_SGLEN)
  1896. return -EINVAL;
  1897. /*
  1898. * Just a bunch of test cases that every HCD is expected to handle.
  1899. *
  1900. * Some may need specific firmware, though it'd be good to have
  1901. * one firmware image to handle all the test cases.
  1902. *
  1903. * FIXME add more tests! cancel requests, verify the data, control
  1904. * queueing, concurrent read+write threads, and so on.
  1905. */
  1906. switch (param->test_num) {
  1907. case 0:
  1908. dev_info(&intf->dev, "TEST 0: NOP\n");
  1909. retval = 0;
  1910. break;
  1911. /* Simple non-queued bulk I/O tests */
  1912. case 1:
  1913. if (dev->out_pipe == 0)
  1914. break;
  1915. dev_info(&intf->dev,
  1916. "TEST 1: write %d bytes %u times\n",
  1917. param->length, param->iterations);
  1918. urb = simple_alloc_urb(udev, dev->out_pipe, param->length, 0);
  1919. if (!urb) {
  1920. retval = -ENOMEM;
  1921. break;
  1922. }
  1923. /* FIRMWARE: bulk sink (maybe accepts short writes) */
  1924. retval = simple_io(dev, urb, param->iterations, 0, 0, "test1");
  1925. simple_free_urb(urb);
  1926. break;
  1927. case 2:
  1928. if (dev->in_pipe == 0)
  1929. break;
  1930. dev_info(&intf->dev,
  1931. "TEST 2: read %d bytes %u times\n",
  1932. param->length, param->iterations);
  1933. urb = simple_alloc_urb(udev, dev->in_pipe, param->length, 0);
  1934. if (!urb) {
  1935. retval = -ENOMEM;
  1936. break;
  1937. }
  1938. /* FIRMWARE: bulk source (maybe generates short writes) */
  1939. retval = simple_io(dev, urb, param->iterations, 0, 0, "test2");
  1940. simple_free_urb(urb);
  1941. break;
  1942. case 3:
  1943. if (dev->out_pipe == 0 || param->vary == 0)
  1944. break;
  1945. dev_info(&intf->dev,
  1946. "TEST 3: write/%d 0..%d bytes %u times\n",
  1947. param->vary, param->length, param->iterations);
  1948. urb = simple_alloc_urb(udev, dev->out_pipe, param->length, 0);
  1949. if (!urb) {
  1950. retval = -ENOMEM;
  1951. break;
  1952. }
  1953. /* FIRMWARE: bulk sink (maybe accepts short writes) */
  1954. retval = simple_io(dev, urb, param->iterations, param->vary,
  1955. 0, "test3");
  1956. simple_free_urb(urb);
  1957. break;
  1958. case 4:
  1959. if (dev->in_pipe == 0 || param->vary == 0)
  1960. break;
  1961. dev_info(&intf->dev,
  1962. "TEST 4: read/%d 0..%d bytes %u times\n",
  1963. param->vary, param->length, param->iterations);
  1964. urb = simple_alloc_urb(udev, dev->in_pipe, param->length, 0);
  1965. if (!urb) {
  1966. retval = -ENOMEM;
  1967. break;
  1968. }
  1969. /* FIRMWARE: bulk source (maybe generates short writes) */
  1970. retval = simple_io(dev, urb, param->iterations, param->vary,
  1971. 0, "test4");
  1972. simple_free_urb(urb);
  1973. break;
  1974. /* Queued bulk I/O tests */
  1975. case 5:
  1976. if (dev->out_pipe == 0 || param->sglen == 0)
  1977. break;
  1978. dev_info(&intf->dev,
  1979. "TEST 5: write %d sglists %d entries of %d bytes\n",
  1980. param->iterations,
  1981. param->sglen, param->length);
  1982. sg = alloc_sglist(param->sglen, param->length,
  1983. 0, dev, dev->out_pipe);
  1984. if (!sg) {
  1985. retval = -ENOMEM;
  1986. break;
  1987. }
  1988. /* FIRMWARE: bulk sink (maybe accepts short writes) */
  1989. retval = perform_sglist(dev, param->iterations, dev->out_pipe,
  1990. &req, sg, param->sglen);
  1991. free_sglist(sg, param->sglen);
  1992. break;
  1993. case 6:
  1994. if (dev->in_pipe == 0 || param->sglen == 0)
  1995. break;
  1996. dev_info(&intf->dev,
  1997. "TEST 6: read %d sglists %d entries of %d bytes\n",
  1998. param->iterations,
  1999. param->sglen, param->length);
  2000. sg = alloc_sglist(param->sglen, param->length,
  2001. 0, dev, dev->in_pipe);
  2002. if (!sg) {
  2003. retval = -ENOMEM;
  2004. break;
  2005. }
  2006. /* FIRMWARE: bulk source (maybe generates short writes) */
  2007. retval = perform_sglist(dev, param->iterations, dev->in_pipe,
  2008. &req, sg, param->sglen);
  2009. free_sglist(sg, param->sglen);
  2010. break;
  2011. case 7:
  2012. if (dev->out_pipe == 0 || param->sglen == 0 || param->vary == 0)
  2013. break;
  2014. dev_info(&intf->dev,
  2015. "TEST 7: write/%d %d sglists %d entries 0..%d bytes\n",
  2016. param->vary, param->iterations,
  2017. param->sglen, param->length);
  2018. sg = alloc_sglist(param->sglen, param->length,
  2019. param->vary, dev, dev->out_pipe);
  2020. if (!sg) {
  2021. retval = -ENOMEM;
  2022. break;
  2023. }
  2024. /* FIRMWARE: bulk sink (maybe accepts short writes) */
  2025. retval = perform_sglist(dev, param->iterations, dev->out_pipe,
  2026. &req, sg, param->sglen);
  2027. free_sglist(sg, param->sglen);
  2028. break;
  2029. case 8:
  2030. if (dev->in_pipe == 0 || param->sglen == 0 || param->vary == 0)
  2031. break;
  2032. dev_info(&intf->dev,
  2033. "TEST 8: read/%d %d sglists %d entries 0..%d bytes\n",
  2034. param->vary, param->iterations,
  2035. param->sglen, param->length);
  2036. sg = alloc_sglist(param->sglen, param->length,
  2037. param->vary, dev, dev->in_pipe);
  2038. if (!sg) {
  2039. retval = -ENOMEM;
  2040. break;
  2041. }
  2042. /* FIRMWARE: bulk source (maybe generates short writes) */
  2043. retval = perform_sglist(dev, param->iterations, dev->in_pipe,
  2044. &req, sg, param->sglen);
  2045. free_sglist(sg, param->sglen);
  2046. break;
  2047. /* non-queued sanity tests for control (chapter 9 subset) */
  2048. case 9:
  2049. retval = 0;
  2050. dev_info(&intf->dev,
  2051. "TEST 9: ch9 (subset) control tests, %d times\n",
  2052. param->iterations);
  2053. for (i = param->iterations; retval == 0 && i--; /* NOP */)
  2054. retval = ch9_postconfig(dev);
  2055. if (retval)
  2056. dev_err(&intf->dev, "ch9 subset failed, "
  2057. "iterations left %d\n", i);
  2058. break;
  2059. /* queued control messaging */
  2060. case 10:
  2061. retval = 0;
  2062. dev_info(&intf->dev,
  2063. "TEST 10: queue %d control calls, %d times\n",
  2064. param->sglen,
  2065. param->iterations);
  2066. retval = test_ctrl_queue(dev, param);
  2067. break;
  2068. /* simple non-queued unlinks (ring with one urb) */
  2069. case 11:
  2070. if (dev->in_pipe == 0 || !param->length)
  2071. break;
  2072. retval = 0;
  2073. dev_info(&intf->dev, "TEST 11: unlink %d reads of %d\n",
  2074. param->iterations, param->length);
  2075. for (i = param->iterations; retval == 0 && i--; /* NOP */)
  2076. retval = unlink_simple(dev, dev->in_pipe,
  2077. param->length);
  2078. if (retval)
  2079. dev_err(&intf->dev, "unlink reads failed %d, "
  2080. "iterations left %d\n", retval, i);
  2081. break;
  2082. case 12:
  2083. if (dev->out_pipe == 0 || !param->length)
  2084. break;
  2085. retval = 0;
  2086. dev_info(&intf->dev, "TEST 12: unlink %d writes of %d\n",
  2087. param->iterations, param->length);
  2088. for (i = param->iterations; retval == 0 && i--; /* NOP */)
  2089. retval = unlink_simple(dev, dev->out_pipe,
  2090. param->length);
  2091. if (retval)
  2092. dev_err(&intf->dev, "unlink writes failed %d, "
  2093. "iterations left %d\n", retval, i);
  2094. break;
  2095. /* ep halt tests */
  2096. case 13:
  2097. if (dev->out_pipe == 0 && dev->in_pipe == 0)
  2098. break;
  2099. retval = 0;
  2100. dev_info(&intf->dev, "TEST 13: set/clear %d halts\n",
  2101. param->iterations);
  2102. for (i = param->iterations; retval == 0 && i--; /* NOP */)
  2103. retval = halt_simple(dev);
  2104. if (retval)
  2105. ERROR(dev, "halts failed, iterations left %d\n", i);
  2106. break;
  2107. /* control write tests */
  2108. case 14:
  2109. if (!dev->info->ctrl_out)
  2110. break;
  2111. dev_info(&intf->dev, "TEST 14: %d ep0out, %d..%d vary %d\n",
  2112. param->iterations,
  2113. realworld ? 1 : 0, param->length,
  2114. param->vary);
  2115. retval = ctrl_out(dev, param->iterations,
  2116. param->length, param->vary, 0);
  2117. break;
  2118. /* iso write tests */
  2119. case 15:
  2120. if (dev->out_iso_pipe == 0 || param->sglen == 0)
  2121. break;
  2122. dev_info(&intf->dev,
  2123. "TEST 15: write %d iso, %d entries of %d bytes\n",
  2124. param->iterations,
  2125. param->sglen, param->length);
  2126. /* FIRMWARE: iso sink */
  2127. retval = test_queue(dev, param,
  2128. dev->out_iso_pipe, dev->iso_out, 0);
  2129. break;
  2130. /* iso read tests */
  2131. case 16:
  2132. if (dev->in_iso_pipe == 0 || param->sglen == 0)
  2133. break;
  2134. dev_info(&intf->dev,
  2135. "TEST 16: read %d iso, %d entries of %d bytes\n",
  2136. param->iterations,
  2137. param->sglen, param->length);
  2138. /* FIRMWARE: iso source */
  2139. retval = test_queue(dev, param,
  2140. dev->in_iso_pipe, dev->iso_in, 0);
  2141. break;
  2142. /* FIXME scatterlist cancel (needs helper thread) */
  2143. /* Tests for bulk I/O using DMA mapping by core and odd address */
  2144. case 17:
  2145. if (dev->out_pipe == 0)
  2146. break;
  2147. dev_info(&intf->dev,
  2148. "TEST 17: write odd addr %d bytes %u times core map\n",
  2149. param->length, param->iterations);
  2150. retval = test_unaligned_bulk(
  2151. dev, dev->out_pipe,
  2152. param->length, param->iterations,
  2153. 0, "test17");
  2154. break;
  2155. case 18:
  2156. if (dev->in_pipe == 0)
  2157. break;
  2158. dev_info(&intf->dev,
  2159. "TEST 18: read odd addr %d bytes %u times core map\n",
  2160. param->length, param->iterations);
  2161. retval = test_unaligned_bulk(
  2162. dev, dev->in_pipe,
  2163. param->length, param->iterations,
  2164. 0, "test18");
  2165. break;
  2166. /* Tests for bulk I/O using premapped coherent buffer and odd address */
  2167. case 19:
  2168. if (dev->out_pipe == 0)
  2169. break;
  2170. dev_info(&intf->dev,
  2171. "TEST 19: write odd addr %d bytes %u times premapped\n",
  2172. param->length, param->iterations);
  2173. retval = test_unaligned_bulk(
  2174. dev, dev->out_pipe,
  2175. param->length, param->iterations,
  2176. URB_NO_TRANSFER_DMA_MAP, "test19");
  2177. break;
  2178. case 20:
  2179. if (dev->in_pipe == 0)
  2180. break;
  2181. dev_info(&intf->dev,
  2182. "TEST 20: read odd addr %d bytes %u times premapped\n",
  2183. param->length, param->iterations);
  2184. retval = test_unaligned_bulk(
  2185. dev, dev->in_pipe,
  2186. param->length, param->iterations,
  2187. URB_NO_TRANSFER_DMA_MAP, "test20");
  2188. break;
  2189. /* control write tests with unaligned buffer */
  2190. case 21:
  2191. if (!dev->info->ctrl_out)
  2192. break;
  2193. dev_info(&intf->dev,
  2194. "TEST 21: %d ep0out odd addr, %d..%d vary %d\n",
  2195. param->iterations,
  2196. realworld ? 1 : 0, param->length,
  2197. param->vary);
  2198. retval = ctrl_out(dev, param->iterations,
  2199. param->length, param->vary, 1);
  2200. break;
  2201. /* unaligned iso tests */
  2202. case 22:
  2203. if (dev->out_iso_pipe == 0 || param->sglen == 0)
  2204. break;
  2205. dev_info(&intf->dev,
  2206. "TEST 22: write %d iso odd, %d entries of %d bytes\n",
  2207. param->iterations,
  2208. param->sglen, param->length);
  2209. retval = test_queue(dev, param,
  2210. dev->out_iso_pipe, dev->iso_out, 1);
  2211. break;
  2212. case 23:
  2213. if (dev->in_iso_pipe == 0 || param->sglen == 0)
  2214. break;
  2215. dev_info(&intf->dev,
  2216. "TEST 23: read %d iso odd, %d entries of %d bytes\n",
  2217. param->iterations,
  2218. param->sglen, param->length);
  2219. retval = test_queue(dev, param,
  2220. dev->in_iso_pipe, dev->iso_in, 1);
  2221. break;
  2222. /* unlink URBs from a bulk-OUT queue */
  2223. case 24:
  2224. if (dev->out_pipe == 0 || !param->length || param->sglen < 4)
  2225. break;
  2226. retval = 0;
  2227. dev_info(&intf->dev, "TEST 24: unlink from %d queues of "
  2228. "%d %d-byte writes\n",
  2229. param->iterations, param->sglen, param->length);
  2230. for (i = param->iterations; retval == 0 && i > 0; --i) {
  2231. retval = unlink_queued(dev, dev->out_pipe,
  2232. param->sglen, param->length);
  2233. if (retval) {
  2234. dev_err(&intf->dev,
  2235. "unlink queued writes failed %d, "
  2236. "iterations left %d\n", retval, i);
  2237. break;
  2238. }
  2239. }
  2240. break;
  2241. /* Simple non-queued interrupt I/O tests */
  2242. case 25:
  2243. if (dev->out_int_pipe == 0)
  2244. break;
  2245. dev_info(&intf->dev,
  2246. "TEST 25: write %d bytes %u times\n",
  2247. param->length, param->iterations);
  2248. urb = simple_alloc_urb(udev, dev->out_int_pipe, param->length,
  2249. dev->int_out->bInterval);
  2250. if (!urb) {
  2251. retval = -ENOMEM;
  2252. break;
  2253. }
  2254. /* FIRMWARE: interrupt sink (maybe accepts short writes) */
  2255. retval = simple_io(dev, urb, param->iterations, 0, 0, "test25");
  2256. simple_free_urb(urb);
  2257. break;
  2258. case 26:
  2259. if (dev->in_int_pipe == 0)
  2260. break;
  2261. dev_info(&intf->dev,
  2262. "TEST 26: read %d bytes %u times\n",
  2263. param->length, param->iterations);
  2264. urb = simple_alloc_urb(udev, dev->in_int_pipe, param->length,
  2265. dev->int_in->bInterval);
  2266. if (!urb) {
  2267. retval = -ENOMEM;
  2268. break;
  2269. }
  2270. /* FIRMWARE: interrupt source (maybe generates short writes) */
  2271. retval = simple_io(dev, urb, param->iterations, 0, 0, "test26");
  2272. simple_free_urb(urb);
  2273. break;
  2274. case 27:
  2275. /* We do performance test, so ignore data compare */
  2276. if (dev->out_pipe == 0 || param->sglen == 0 || pattern != 0)
  2277. break;
  2278. dev_info(&intf->dev,
  2279. "TEST 27: bulk write %dMbytes\n", (param->iterations *
  2280. param->sglen * param->length) / (1024 * 1024));
  2281. retval = test_queue(dev, param,
  2282. dev->out_pipe, NULL, 0);
  2283. break;
  2284. case 28:
  2285. if (dev->in_pipe == 0 || param->sglen == 0 || pattern != 0)
  2286. break;
  2287. dev_info(&intf->dev,
  2288. "TEST 28: bulk read %dMbytes\n", (param->iterations *
  2289. param->sglen * param->length) / (1024 * 1024));
  2290. retval = test_queue(dev, param,
  2291. dev->in_pipe, NULL, 0);
  2292. break;
  2293. /* Test data Toggle/seq_nr clear between bulk out transfers */
  2294. case 29:
  2295. if (dev->out_pipe == 0)
  2296. break;
  2297. retval = 0;
  2298. dev_info(&intf->dev, "TEST 29: Clear toggle between bulk writes %d times\n",
  2299. param->iterations);
  2300. for (i = param->iterations; retval == 0 && i > 0; --i)
  2301. retval = toggle_sync_simple(dev);
  2302. if (retval)
  2303. ERROR(dev, "toggle sync failed, iterations left %d\n",
  2304. i);
  2305. break;
  2306. }
  2307. return retval;
  2308. }
  2309. /*-------------------------------------------------------------------------*/
  2310. /* We only have this one interface to user space, through usbfs.
  2311. * User mode code can scan usbfs to find N different devices (maybe on
  2312. * different busses) to use when testing, and allocate one thread per
  2313. * test. So discovery is simplified, and we have no device naming issues.
  2314. *
  2315. * Don't use these only as stress/load tests. Use them along with
  2316. * other USB bus activity: plugging, unplugging, mousing, mp3 playback,
  2317. * video capture, and so on. Run different tests at different times, in
  2318. * different sequences. Nothing here should interact with other devices,
  2319. * except indirectly by consuming USB bandwidth and CPU resources for test
  2320. * threads and request completion. But the only way to know that for sure
  2321. * is to test when HC queues are in use by many devices.
  2322. *
  2323. * WARNING: Because usbfs grabs udev->dev.sem before calling this ioctl(),
  2324. * it locks out usbcore in certain code paths. Notably, if you disconnect
  2325. * the device-under-test, hub_wq will wait block forever waiting for the
  2326. * ioctl to complete ... so that usb_disconnect() can abort the pending
  2327. * urbs and then call usbtest_disconnect(). To abort a test, you're best
  2328. * off just killing the userspace task and waiting for it to exit.
  2329. */
  2330. static int
  2331. usbtest_ioctl(struct usb_interface *intf, unsigned int code, void *buf)
  2332. {
  2333. struct usbtest_dev *dev = usb_get_intfdata(intf);
  2334. struct usbtest_param_64 *param_64 = buf;
  2335. struct usbtest_param_32 temp;
  2336. struct usbtest_param_32 *param_32 = buf;
  2337. struct timespec64 start;
  2338. struct timespec64 end;
  2339. struct timespec64 duration;
  2340. int retval = -EOPNOTSUPP;
  2341. /* FIXME USBDEVFS_CONNECTINFO doesn't say how fast the device is. */
  2342. pattern = mod_pattern;
  2343. if (mutex_lock_interruptible(&dev->lock))
  2344. return -ERESTARTSYS;
  2345. /* FIXME: What if a system sleep starts while a test is running? */
  2346. /* some devices, like ez-usb default devices, need a non-default
  2347. * altsetting to have any active endpoints. some tests change
  2348. * altsettings; force a default so most tests don't need to check.
  2349. */
  2350. if (dev->info->alt >= 0) {
  2351. if (intf->altsetting->desc.bInterfaceNumber) {
  2352. retval = -ENODEV;
  2353. goto free_mutex;
  2354. }
  2355. retval = set_altsetting(dev, dev->info->alt);
  2356. if (retval) {
  2357. dev_err(&intf->dev,
  2358. "set altsetting to %d failed, %d\n",
  2359. dev->info->alt, retval);
  2360. goto free_mutex;
  2361. }
  2362. }
  2363. switch (code) {
  2364. case USBTEST_REQUEST_64:
  2365. temp.test_num = param_64->test_num;
  2366. temp.iterations = param_64->iterations;
  2367. temp.length = param_64->length;
  2368. temp.sglen = param_64->sglen;
  2369. temp.vary = param_64->vary;
  2370. param_32 = &temp;
  2371. break;
  2372. case USBTEST_REQUEST_32:
  2373. break;
  2374. default:
  2375. retval = -EOPNOTSUPP;
  2376. goto free_mutex;
  2377. }
  2378. ktime_get_ts64(&start);
  2379. retval = usbtest_do_ioctl(intf, param_32);
  2380. if (retval < 0)
  2381. goto free_mutex;
  2382. ktime_get_ts64(&end);
  2383. duration = timespec64_sub(end, start);
  2384. temp.duration_sec = duration.tv_sec;
  2385. temp.duration_usec = duration.tv_nsec/NSEC_PER_USEC;
  2386. switch (code) {
  2387. case USBTEST_REQUEST_32:
  2388. param_32->duration_sec = temp.duration_sec;
  2389. param_32->duration_usec = temp.duration_usec;
  2390. break;
  2391. case USBTEST_REQUEST_64:
  2392. param_64->duration_sec = temp.duration_sec;
  2393. param_64->duration_usec = temp.duration_usec;
  2394. break;
  2395. }
  2396. free_mutex:
  2397. mutex_unlock(&dev->lock);
  2398. return retval;
  2399. }
  2400. /*-------------------------------------------------------------------------*/
  2401. static unsigned force_interrupt;
  2402. module_param(force_interrupt, uint, 0);
  2403. MODULE_PARM_DESC(force_interrupt, "0 = test default; else interrupt");
  2404. #ifdef GENERIC
  2405. static unsigned short vendor;
  2406. module_param(vendor, ushort, 0);
  2407. MODULE_PARM_DESC(vendor, "vendor code (from usb-if)");
  2408. static unsigned short product;
  2409. module_param(product, ushort, 0);
  2410. MODULE_PARM_DESC(product, "product code (from vendor)");
  2411. #endif
  2412. static int
  2413. usbtest_probe(struct usb_interface *intf, const struct usb_device_id *id)
  2414. {
  2415. struct usb_device *udev;
  2416. struct usbtest_dev *dev;
  2417. struct usbtest_info *info;
  2418. char *rtest, *wtest;
  2419. char *irtest, *iwtest;
  2420. char *intrtest, *intwtest;
  2421. udev = interface_to_usbdev(intf);
  2422. #ifdef GENERIC
  2423. /* specify devices by module parameters? */
  2424. if (id->match_flags == 0) {
  2425. /* vendor match required, product match optional */
  2426. if (!vendor || le16_to_cpu(udev->descriptor.idVendor) != (u16)vendor)
  2427. return -ENODEV;
  2428. if (product && le16_to_cpu(udev->descriptor.idProduct) != (u16)product)
  2429. return -ENODEV;
  2430. dev_info(&intf->dev, "matched module params, "
  2431. "vend=0x%04x prod=0x%04x\n",
  2432. le16_to_cpu(udev->descriptor.idVendor),
  2433. le16_to_cpu(udev->descriptor.idProduct));
  2434. }
  2435. #endif
  2436. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  2437. if (!dev)
  2438. return -ENOMEM;
  2439. info = (struct usbtest_info *) id->driver_info;
  2440. dev->info = info;
  2441. mutex_init(&dev->lock);
  2442. dev->intf = intf;
  2443. /* cacheline-aligned scratch for i/o */
  2444. dev->buf = kmalloc(TBUF_SIZE, GFP_KERNEL);
  2445. if (dev->buf == NULL) {
  2446. kfree(dev);
  2447. return -ENOMEM;
  2448. }
  2449. /* NOTE this doesn't yet test the handful of difference that are
  2450. * visible with high speed interrupts: bigger maxpacket (1K) and
  2451. * "high bandwidth" modes (up to 3 packets/uframe).
  2452. */
  2453. rtest = wtest = "";
  2454. irtest = iwtest = "";
  2455. intrtest = intwtest = "";
  2456. if (force_interrupt || udev->speed == USB_SPEED_LOW) {
  2457. if (info->ep_in) {
  2458. dev->in_pipe = usb_rcvintpipe(udev, info->ep_in);
  2459. rtest = " intr-in";
  2460. }
  2461. if (info->ep_out) {
  2462. dev->out_pipe = usb_sndintpipe(udev, info->ep_out);
  2463. wtest = " intr-out";
  2464. }
  2465. } else {
  2466. if (override_alt >= 0 || info->autoconf) {
  2467. int status;
  2468. status = get_endpoints(dev, intf);
  2469. if (status < 0) {
  2470. WARNING(dev, "couldn't get endpoints, %d\n",
  2471. status);
  2472. kfree(dev->buf);
  2473. kfree(dev);
  2474. return status;
  2475. }
  2476. /* may find bulk or ISO pipes */
  2477. } else {
  2478. if (info->ep_in)
  2479. dev->in_pipe = usb_rcvbulkpipe(udev,
  2480. info->ep_in);
  2481. if (info->ep_out)
  2482. dev->out_pipe = usb_sndbulkpipe(udev,
  2483. info->ep_out);
  2484. }
  2485. if (dev->in_pipe)
  2486. rtest = " bulk-in";
  2487. if (dev->out_pipe)
  2488. wtest = " bulk-out";
  2489. if (dev->in_iso_pipe)
  2490. irtest = " iso-in";
  2491. if (dev->out_iso_pipe)
  2492. iwtest = " iso-out";
  2493. if (dev->in_int_pipe)
  2494. intrtest = " int-in";
  2495. if (dev->out_int_pipe)
  2496. intwtest = " int-out";
  2497. }
  2498. usb_set_intfdata(intf, dev);
  2499. dev_info(&intf->dev, "%s\n", info->name);
  2500. dev_info(&intf->dev, "%s {control%s%s%s%s%s%s%s} tests%s\n",
  2501. usb_speed_string(udev->speed),
  2502. info->ctrl_out ? " in/out" : "",
  2503. rtest, wtest,
  2504. irtest, iwtest,
  2505. intrtest, intwtest,
  2506. info->alt >= 0 ? " (+alt)" : "");
  2507. return 0;
  2508. }
  2509. static int usbtest_suspend(struct usb_interface *intf, pm_message_t message)
  2510. {
  2511. return 0;
  2512. }
  2513. static int usbtest_resume(struct usb_interface *intf)
  2514. {
  2515. return 0;
  2516. }
  2517. static void usbtest_disconnect(struct usb_interface *intf)
  2518. {
  2519. struct usbtest_dev *dev = usb_get_intfdata(intf);
  2520. usb_set_intfdata(intf, NULL);
  2521. dev_dbg(&intf->dev, "disconnect\n");
  2522. kfree(dev->buf);
  2523. kfree(dev);
  2524. }
  2525. /* Basic testing only needs a device that can source or sink bulk traffic.
  2526. * Any device can test control transfers (default with GENERIC binding).
  2527. *
  2528. * Several entries work with the default EP0 implementation that's built
  2529. * into EZ-USB chips. There's a default vendor ID which can be overridden
  2530. * by (very) small config EEPROMS, but otherwise all these devices act
  2531. * identically until firmware is loaded: only EP0 works. It turns out
  2532. * to be easy to make other endpoints work, without modifying that EP0
  2533. * behavior. For now, we expect that kind of firmware.
  2534. */
  2535. /* an21xx or fx versions of ez-usb */
  2536. static struct usbtest_info ez1_info = {
  2537. .name = "EZ-USB device",
  2538. .ep_in = 2,
  2539. .ep_out = 2,
  2540. .alt = 1,
  2541. };
  2542. /* fx2 version of ez-usb */
  2543. static struct usbtest_info ez2_info = {
  2544. .name = "FX2 device",
  2545. .ep_in = 6,
  2546. .ep_out = 2,
  2547. .alt = 1,
  2548. };
  2549. /* ezusb family device with dedicated usb test firmware,
  2550. */
  2551. static struct usbtest_info fw_info = {
  2552. .name = "usb test device",
  2553. .ep_in = 2,
  2554. .ep_out = 2,
  2555. .alt = 1,
  2556. .autoconf = 1, /* iso and ctrl_out need autoconf */
  2557. .ctrl_out = 1,
  2558. .iso = 1, /* iso_ep's are #8 in/out */
  2559. };
  2560. /* peripheral running Linux and 'zero.c' test firmware, or
  2561. * its user-mode cousin. different versions of this use
  2562. * different hardware with the same vendor/product codes.
  2563. * host side MUST rely on the endpoint descriptors.
  2564. */
  2565. static struct usbtest_info gz_info = {
  2566. .name = "Linux gadget zero",
  2567. .autoconf = 1,
  2568. .ctrl_out = 1,
  2569. .iso = 1,
  2570. .intr = 1,
  2571. .alt = 0,
  2572. };
  2573. static struct usbtest_info um_info = {
  2574. .name = "Linux user mode test driver",
  2575. .autoconf = 1,
  2576. .alt = -1,
  2577. };
  2578. static struct usbtest_info um2_info = {
  2579. .name = "Linux user mode ISO test driver",
  2580. .autoconf = 1,
  2581. .iso = 1,
  2582. .alt = -1,
  2583. };
  2584. #ifdef IBOT2
  2585. /* this is a nice source of high speed bulk data;
  2586. * uses an FX2, with firmware provided in the device
  2587. */
  2588. static struct usbtest_info ibot2_info = {
  2589. .name = "iBOT2 webcam",
  2590. .ep_in = 2,
  2591. .alt = -1,
  2592. };
  2593. #endif
  2594. #ifdef GENERIC
  2595. /* we can use any device to test control traffic */
  2596. static struct usbtest_info generic_info = {
  2597. .name = "Generic USB device",
  2598. .alt = -1,
  2599. };
  2600. #endif
  2601. static const struct usb_device_id id_table[] = {
  2602. /*-------------------------------------------------------------*/
  2603. /* EZ-USB devices which download firmware to replace (or in our
  2604. * case augment) the default device implementation.
  2605. */
  2606. /* generic EZ-USB FX controller */
  2607. { USB_DEVICE(0x0547, 0x2235),
  2608. .driver_info = (unsigned long) &ez1_info,
  2609. },
  2610. /* CY3671 development board with EZ-USB FX */
  2611. { USB_DEVICE(0x0547, 0x0080),
  2612. .driver_info = (unsigned long) &ez1_info,
  2613. },
  2614. /* generic EZ-USB FX2 controller (or development board) */
  2615. { USB_DEVICE(0x04b4, 0x8613),
  2616. .driver_info = (unsigned long) &ez2_info,
  2617. },
  2618. /* re-enumerated usb test device firmware */
  2619. { USB_DEVICE(0xfff0, 0xfff0),
  2620. .driver_info = (unsigned long) &fw_info,
  2621. },
  2622. /* "Gadget Zero" firmware runs under Linux */
  2623. { USB_DEVICE(0x0525, 0xa4a0),
  2624. .driver_info = (unsigned long) &gz_info,
  2625. },
  2626. /* so does a user-mode variant */
  2627. { USB_DEVICE(0x0525, 0xa4a4),
  2628. .driver_info = (unsigned long) &um_info,
  2629. },
  2630. /* ... and a user-mode variant that talks iso */
  2631. { USB_DEVICE(0x0525, 0xa4a3),
  2632. .driver_info = (unsigned long) &um2_info,
  2633. },
  2634. #ifdef KEYSPAN_19Qi
  2635. /* Keyspan 19qi uses an21xx (original EZ-USB) */
  2636. /* this does not coexist with the real Keyspan 19qi driver! */
  2637. { USB_DEVICE(0x06cd, 0x010b),
  2638. .driver_info = (unsigned long) &ez1_info,
  2639. },
  2640. #endif
  2641. /*-------------------------------------------------------------*/
  2642. #ifdef IBOT2
  2643. /* iBOT2 makes a nice source of high speed bulk-in data */
  2644. /* this does not coexist with a real iBOT2 driver! */
  2645. { USB_DEVICE(0x0b62, 0x0059),
  2646. .driver_info = (unsigned long) &ibot2_info,
  2647. },
  2648. #endif
  2649. /*-------------------------------------------------------------*/
  2650. #ifdef GENERIC
  2651. /* module params can specify devices to use for control tests */
  2652. { .driver_info = (unsigned long) &generic_info, },
  2653. #endif
  2654. /*-------------------------------------------------------------*/
  2655. { }
  2656. };
  2657. MODULE_DEVICE_TABLE(usb, id_table);
  2658. static struct usb_driver usbtest_driver = {
  2659. .name = "usbtest",
  2660. .id_table = id_table,
  2661. .probe = usbtest_probe,
  2662. .unlocked_ioctl = usbtest_ioctl,
  2663. .disconnect = usbtest_disconnect,
  2664. .suspend = usbtest_suspend,
  2665. .resume = usbtest_resume,
  2666. };
  2667. /*-------------------------------------------------------------------------*/
  2668. static int __init usbtest_init(void)
  2669. {
  2670. #ifdef GENERIC
  2671. if (vendor)
  2672. pr_debug("params: vend=0x%04x prod=0x%04x\n", vendor, product);
  2673. #endif
  2674. return usb_register(&usbtest_driver);
  2675. }
  2676. module_init(usbtest_init);
  2677. static void __exit usbtest_exit(void)
  2678. {
  2679. usb_deregister(&usbtest_driver);
  2680. }
  2681. module_exit(usbtest_exit);
  2682. MODULE_DESCRIPTION("USB Core/HCD Testing Driver");
  2683. MODULE_LICENSE("GPL");