usbtest.c 57 KB

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