usbtest.c 56 KB

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