usbtest.c 56 KB

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