usbtest.c 57 KB

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