usbtest.c 56 KB

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