usbtest.c 56 KB

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