usbtest.c 72 KB

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