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