usbtest.c 57 KB

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