usbtest.c 56 KB

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