usbtest.c 57 KB

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