usbtest.c 76 KB

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