usbtest.c 66 KB

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