usbtest.c 56 KB

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