usbtest.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208
  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_buffer_alloc (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_buffer_free (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 = usb_buffer_alloc (udev, sizeof *reqp, GFP_KERNEL,
  862. &u->setup_dma);
  863. if (!reqp)
  864. goto cleanup;
  865. reqp->setup = req;
  866. reqp->number = i % NUM_SUBCASES;
  867. reqp->expected = expected;
  868. u->setup_packet = (char *) &reqp->setup;
  869. u->transfer_flags |= URB_NO_SETUP_DMA_MAP;
  870. u->context = &context;
  871. u->complete = ctrl_complete;
  872. }
  873. /* queue the urbs */
  874. context.urb = urb;
  875. spin_lock_irq (&context.lock);
  876. for (i = 0; i < param->sglen; i++) {
  877. context.status = usb_submit_urb (urb [i], GFP_ATOMIC);
  878. if (context.status != 0) {
  879. ERROR(dev, "can't submit urb[%d], status %d\n",
  880. i, context.status);
  881. context.count = context.pending;
  882. break;
  883. }
  884. context.pending++;
  885. }
  886. spin_unlock_irq (&context.lock);
  887. /* FIXME set timer and time out; provide a disconnect hook */
  888. /* wait for the last one to complete */
  889. if (context.pending > 0)
  890. wait_for_completion (&context.complete);
  891. cleanup:
  892. for (i = 0; i < param->sglen; i++) {
  893. if (!urb [i])
  894. continue;
  895. urb [i]->dev = udev;
  896. if (urb [i]->setup_packet)
  897. usb_buffer_free (udev, sizeof (struct usb_ctrlrequest),
  898. urb [i]->setup_packet,
  899. urb [i]->setup_dma);
  900. simple_free_urb (urb [i]);
  901. }
  902. kfree (urb);
  903. return context.status;
  904. }
  905. #undef NUM_SUBCASES
  906. /*-------------------------------------------------------------------------*/
  907. static void unlink1_callback (struct urb *urb)
  908. {
  909. int status = urb->status;
  910. // we "know" -EPIPE (stall) never happens
  911. if (!status)
  912. status = usb_submit_urb (urb, GFP_ATOMIC);
  913. if (status) {
  914. urb->status = status;
  915. complete(urb->context);
  916. }
  917. }
  918. static int unlink1 (struct usbtest_dev *dev, int pipe, int size, int async)
  919. {
  920. struct urb *urb;
  921. struct completion completion;
  922. int retval = 0;
  923. init_completion (&completion);
  924. urb = simple_alloc_urb (testdev_to_usbdev (dev), pipe, size);
  925. if (!urb)
  926. return -ENOMEM;
  927. urb->context = &completion;
  928. urb->complete = unlink1_callback;
  929. /* keep the endpoint busy. there are lots of hc/hcd-internal
  930. * states, and testing should get to all of them over time.
  931. *
  932. * FIXME want additional tests for when endpoint is STALLing
  933. * due to errors, or is just NAKing requests.
  934. */
  935. if ((retval = usb_submit_urb (urb, GFP_KERNEL)) != 0) {
  936. dev_err(&dev->intf->dev, "submit fail %d\n", retval);
  937. return retval;
  938. }
  939. /* unlinking that should always work. variable delay tests more
  940. * hcd states and code paths, even with little other system load.
  941. */
  942. msleep (jiffies % (2 * INTERRUPT_RATE));
  943. if (async) {
  944. while (!completion_done(&completion)) {
  945. retval = usb_unlink_urb(urb);
  946. switch (retval) {
  947. case -EBUSY:
  948. case -EIDRM:
  949. /* we can't unlink urbs while they're completing
  950. * or if they've completed, and we haven't
  951. * resubmitted. "normal" drivers would prevent
  952. * resubmission, but since we're testing unlink
  953. * paths, we can't.
  954. */
  955. ERROR(dev, "unlink retry\n");
  956. continue;
  957. case 0:
  958. case -EINPROGRESS:
  959. break;
  960. default:
  961. dev_err(&dev->intf->dev,
  962. "unlink fail %d\n", retval);
  963. return retval;
  964. }
  965. break;
  966. }
  967. } else
  968. usb_kill_urb (urb);
  969. wait_for_completion (&completion);
  970. retval = urb->status;
  971. simple_free_urb (urb);
  972. if (async)
  973. return (retval == -ECONNRESET) ? 0 : retval - 1000;
  974. else
  975. return (retval == -ENOENT || retval == -EPERM) ?
  976. 0 : retval - 2000;
  977. }
  978. static int unlink_simple (struct usbtest_dev *dev, int pipe, int len)
  979. {
  980. int retval = 0;
  981. /* test sync and async paths */
  982. retval = unlink1 (dev, pipe, len, 1);
  983. if (!retval)
  984. retval = unlink1 (dev, pipe, len, 0);
  985. return retval;
  986. }
  987. /*-------------------------------------------------------------------------*/
  988. static int verify_not_halted(struct usbtest_dev *tdev, int ep, struct urb *urb)
  989. {
  990. int retval;
  991. u16 status;
  992. /* shouldn't look or act halted */
  993. retval = usb_get_status (urb->dev, USB_RECIP_ENDPOINT, ep, &status);
  994. if (retval < 0) {
  995. ERROR(tdev, "ep %02x couldn't get no-halt status, %d\n",
  996. ep, retval);
  997. return retval;
  998. }
  999. if (status != 0) {
  1000. ERROR(tdev, "ep %02x bogus status: %04x != 0\n", ep, status);
  1001. return -EINVAL;
  1002. }
  1003. retval = simple_io(tdev, urb, 1, 0, 0, __func__);
  1004. if (retval != 0)
  1005. return -EINVAL;
  1006. return 0;
  1007. }
  1008. static int verify_halted(struct usbtest_dev *tdev, int ep, struct urb *urb)
  1009. {
  1010. int retval;
  1011. u16 status;
  1012. /* should look and act halted */
  1013. retval = usb_get_status (urb->dev, USB_RECIP_ENDPOINT, ep, &status);
  1014. if (retval < 0) {
  1015. ERROR(tdev, "ep %02x couldn't get halt status, %d\n",
  1016. ep, retval);
  1017. return retval;
  1018. }
  1019. le16_to_cpus(&status);
  1020. if (status != 1) {
  1021. ERROR(tdev, "ep %02x bogus status: %04x != 1\n", ep, status);
  1022. return -EINVAL;
  1023. }
  1024. retval = simple_io(tdev, urb, 1, 0, -EPIPE, __func__);
  1025. if (retval != -EPIPE)
  1026. return -EINVAL;
  1027. retval = simple_io(tdev, urb, 1, 0, -EPIPE, "verify_still_halted");
  1028. if (retval != -EPIPE)
  1029. return -EINVAL;
  1030. return 0;
  1031. }
  1032. static int test_halt(struct usbtest_dev *tdev, int ep, struct urb *urb)
  1033. {
  1034. int retval;
  1035. /* shouldn't look or act halted now */
  1036. retval = verify_not_halted(tdev, ep, urb);
  1037. if (retval < 0)
  1038. return retval;
  1039. /* set halt (protocol test only), verify it worked */
  1040. retval = usb_control_msg (urb->dev, usb_sndctrlpipe (urb->dev, 0),
  1041. USB_REQ_SET_FEATURE, USB_RECIP_ENDPOINT,
  1042. USB_ENDPOINT_HALT, ep,
  1043. NULL, 0, USB_CTRL_SET_TIMEOUT);
  1044. if (retval < 0) {
  1045. ERROR(tdev, "ep %02x couldn't set halt, %d\n", ep, retval);
  1046. return retval;
  1047. }
  1048. retval = verify_halted(tdev, ep, urb);
  1049. if (retval < 0)
  1050. return retval;
  1051. /* clear halt (tests API + protocol), verify it worked */
  1052. retval = usb_clear_halt (urb->dev, urb->pipe);
  1053. if (retval < 0) {
  1054. ERROR(tdev, "ep %02x couldn't clear halt, %d\n", ep, retval);
  1055. return retval;
  1056. }
  1057. retval = verify_not_halted(tdev, ep, urb);
  1058. if (retval < 0)
  1059. return retval;
  1060. /* NOTE: could also verify SET_INTERFACE clear halts ... */
  1061. return 0;
  1062. }
  1063. static int halt_simple (struct usbtest_dev *dev)
  1064. {
  1065. int ep;
  1066. int retval = 0;
  1067. struct urb *urb;
  1068. urb = simple_alloc_urb (testdev_to_usbdev (dev), 0, 512);
  1069. if (urb == NULL)
  1070. return -ENOMEM;
  1071. if (dev->in_pipe) {
  1072. ep = usb_pipeendpoint (dev->in_pipe) | USB_DIR_IN;
  1073. urb->pipe = dev->in_pipe;
  1074. retval = test_halt(dev, ep, urb);
  1075. if (retval < 0)
  1076. goto done;
  1077. }
  1078. if (dev->out_pipe) {
  1079. ep = usb_pipeendpoint (dev->out_pipe);
  1080. urb->pipe = dev->out_pipe;
  1081. retval = test_halt(dev, ep, urb);
  1082. }
  1083. done:
  1084. simple_free_urb (urb);
  1085. return retval;
  1086. }
  1087. /*-------------------------------------------------------------------------*/
  1088. /* Control OUT tests use the vendor control requests from Intel's
  1089. * USB 2.0 compliance test device: write a buffer, read it back.
  1090. *
  1091. * Intel's spec only _requires_ that it work for one packet, which
  1092. * is pretty weak. Some HCDs place limits here; most devices will
  1093. * need to be able to handle more than one OUT data packet. We'll
  1094. * try whatever we're told to try.
  1095. */
  1096. static int ctrl_out (struct usbtest_dev *dev,
  1097. unsigned count, unsigned length, unsigned vary)
  1098. {
  1099. unsigned i, j, len;
  1100. int retval;
  1101. u8 *buf;
  1102. char *what = "?";
  1103. struct usb_device *udev;
  1104. if (length < 1 || length > 0xffff || vary >= length)
  1105. return -EINVAL;
  1106. buf = kmalloc(length, GFP_KERNEL);
  1107. if (!buf)
  1108. return -ENOMEM;
  1109. udev = testdev_to_usbdev (dev);
  1110. len = length;
  1111. retval = 0;
  1112. /* NOTE: hardware might well act differently if we pushed it
  1113. * with lots back-to-back queued requests.
  1114. */
  1115. for (i = 0; i < count; i++) {
  1116. /* write patterned data */
  1117. for (j = 0; j < len; j++)
  1118. buf [j] = i + j;
  1119. retval = usb_control_msg (udev, usb_sndctrlpipe (udev,0),
  1120. 0x5b, USB_DIR_OUT|USB_TYPE_VENDOR,
  1121. 0, 0, buf, len, USB_CTRL_SET_TIMEOUT);
  1122. if (retval != len) {
  1123. what = "write";
  1124. if (retval >= 0) {
  1125. ERROR(dev, "ctrl_out, wlen %d (expected %d)\n",
  1126. retval, len);
  1127. retval = -EBADMSG;
  1128. }
  1129. break;
  1130. }
  1131. /* read it back -- assuming nothing intervened!! */
  1132. retval = usb_control_msg (udev, usb_rcvctrlpipe (udev,0),
  1133. 0x5c, USB_DIR_IN|USB_TYPE_VENDOR,
  1134. 0, 0, buf, len, USB_CTRL_GET_TIMEOUT);
  1135. if (retval != len) {
  1136. what = "read";
  1137. if (retval >= 0) {
  1138. ERROR(dev, "ctrl_out, rlen %d (expected %d)\n",
  1139. retval, len);
  1140. retval = -EBADMSG;
  1141. }
  1142. break;
  1143. }
  1144. /* fail if we can't verify */
  1145. for (j = 0; j < len; j++) {
  1146. if (buf [j] != (u8) (i + j)) {
  1147. ERROR(dev, "ctrl_out, byte %d is %d not %d\n",
  1148. j, buf [j], (u8) i + j);
  1149. retval = -EBADMSG;
  1150. break;
  1151. }
  1152. }
  1153. if (retval < 0) {
  1154. what = "verify";
  1155. break;
  1156. }
  1157. len += vary;
  1158. /* [real world] the "zero bytes IN" case isn't really used.
  1159. * hardware can easily trip up in this weird case, since its
  1160. * status stage is IN, not OUT like other ep0in transfers.
  1161. */
  1162. if (len > length)
  1163. len = realworld ? 1 : 0;
  1164. }
  1165. if (retval < 0)
  1166. ERROR (dev, "ctrl_out %s failed, code %d, count %d\n",
  1167. what, retval, i);
  1168. kfree (buf);
  1169. return retval;
  1170. }
  1171. /*-------------------------------------------------------------------------*/
  1172. /* ISO tests ... mimics common usage
  1173. * - buffer length is split into N packets (mostly maxpacket sized)
  1174. * - multi-buffers according to sglen
  1175. */
  1176. struct iso_context {
  1177. unsigned count;
  1178. unsigned pending;
  1179. spinlock_t lock;
  1180. struct completion done;
  1181. int submit_error;
  1182. unsigned long errors;
  1183. unsigned long packet_count;
  1184. struct usbtest_dev *dev;
  1185. };
  1186. static void iso_callback (struct urb *urb)
  1187. {
  1188. struct iso_context *ctx = urb->context;
  1189. spin_lock(&ctx->lock);
  1190. ctx->count--;
  1191. ctx->packet_count += urb->number_of_packets;
  1192. if (urb->error_count > 0)
  1193. ctx->errors += urb->error_count;
  1194. else if (urb->status != 0)
  1195. ctx->errors += urb->number_of_packets;
  1196. if (urb->status == 0 && ctx->count > (ctx->pending - 1)
  1197. && !ctx->submit_error) {
  1198. int status = usb_submit_urb (urb, GFP_ATOMIC);
  1199. switch (status) {
  1200. case 0:
  1201. goto done;
  1202. default:
  1203. dev_err(&ctx->dev->intf->dev,
  1204. "iso resubmit err %d\n",
  1205. status);
  1206. /* FALLTHROUGH */
  1207. case -ENODEV: /* disconnected */
  1208. case -ESHUTDOWN: /* endpoint disabled */
  1209. ctx->submit_error = 1;
  1210. break;
  1211. }
  1212. }
  1213. simple_free_urb (urb);
  1214. ctx->pending--;
  1215. if (ctx->pending == 0) {
  1216. if (ctx->errors)
  1217. dev_err(&ctx->dev->intf->dev,
  1218. "iso test, %lu errors out of %lu\n",
  1219. ctx->errors, ctx->packet_count);
  1220. complete (&ctx->done);
  1221. }
  1222. done:
  1223. spin_unlock(&ctx->lock);
  1224. }
  1225. static struct urb *iso_alloc_urb (
  1226. struct usb_device *udev,
  1227. int pipe,
  1228. struct usb_endpoint_descriptor *desc,
  1229. long bytes
  1230. )
  1231. {
  1232. struct urb *urb;
  1233. unsigned i, maxp, packets;
  1234. if (bytes < 0 || !desc)
  1235. return NULL;
  1236. maxp = 0x7ff & le16_to_cpu(desc->wMaxPacketSize);
  1237. maxp *= 1 + (0x3 & (le16_to_cpu(desc->wMaxPacketSize) >> 11));
  1238. packets = DIV_ROUND_UP(bytes, maxp);
  1239. urb = usb_alloc_urb (packets, GFP_KERNEL);
  1240. if (!urb)
  1241. return urb;
  1242. urb->dev = udev;
  1243. urb->pipe = pipe;
  1244. urb->number_of_packets = packets;
  1245. urb->transfer_buffer_length = bytes;
  1246. urb->transfer_buffer = usb_buffer_alloc (udev, bytes, GFP_KERNEL,
  1247. &urb->transfer_dma);
  1248. if (!urb->transfer_buffer) {
  1249. usb_free_urb (urb);
  1250. return NULL;
  1251. }
  1252. memset (urb->transfer_buffer, 0, bytes);
  1253. for (i = 0; i < packets; i++) {
  1254. /* here, only the last packet will be short */
  1255. urb->iso_frame_desc[i].length = min ((unsigned) bytes, maxp);
  1256. bytes -= urb->iso_frame_desc[i].length;
  1257. urb->iso_frame_desc[i].offset = maxp * i;
  1258. }
  1259. urb->complete = iso_callback;
  1260. // urb->context = SET BY CALLER
  1261. urb->interval = 1 << (desc->bInterval - 1);
  1262. urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
  1263. return urb;
  1264. }
  1265. static int
  1266. test_iso_queue (struct usbtest_dev *dev, struct usbtest_param *param,
  1267. int pipe, struct usb_endpoint_descriptor *desc)
  1268. {
  1269. struct iso_context context;
  1270. struct usb_device *udev;
  1271. unsigned i;
  1272. unsigned long packets = 0;
  1273. int status = 0;
  1274. struct urb *urbs[10]; /* FIXME no limit */
  1275. if (param->sglen > 10)
  1276. return -EDOM;
  1277. memset(&context, 0, sizeof context);
  1278. context.count = param->iterations * param->sglen;
  1279. context.dev = dev;
  1280. init_completion (&context.done);
  1281. spin_lock_init (&context.lock);
  1282. memset (urbs, 0, sizeof urbs);
  1283. udev = testdev_to_usbdev (dev);
  1284. dev_info(&dev->intf->dev,
  1285. "... iso period %d %sframes, wMaxPacket %04x\n",
  1286. 1 << (desc->bInterval - 1),
  1287. (udev->speed == USB_SPEED_HIGH) ? "micro" : "",
  1288. le16_to_cpu(desc->wMaxPacketSize));
  1289. for (i = 0; i < param->sglen; i++) {
  1290. urbs [i] = iso_alloc_urb (udev, pipe, desc,
  1291. param->length);
  1292. if (!urbs [i]) {
  1293. status = -ENOMEM;
  1294. goto fail;
  1295. }
  1296. packets += urbs[i]->number_of_packets;
  1297. urbs [i]->context = &context;
  1298. }
  1299. packets *= param->iterations;
  1300. dev_info(&dev->intf->dev,
  1301. "... total %lu msec (%lu packets)\n",
  1302. (packets * (1 << (desc->bInterval - 1)))
  1303. / ((udev->speed == USB_SPEED_HIGH) ? 8 : 1),
  1304. packets);
  1305. spin_lock_irq (&context.lock);
  1306. for (i = 0; i < param->sglen; i++) {
  1307. ++context.pending;
  1308. status = usb_submit_urb (urbs [i], GFP_ATOMIC);
  1309. if (status < 0) {
  1310. ERROR (dev, "submit iso[%d], error %d\n", i, status);
  1311. if (i == 0) {
  1312. spin_unlock_irq (&context.lock);
  1313. goto fail;
  1314. }
  1315. simple_free_urb (urbs [i]);
  1316. context.pending--;
  1317. context.submit_error = 1;
  1318. break;
  1319. }
  1320. }
  1321. spin_unlock_irq (&context.lock);
  1322. wait_for_completion (&context.done);
  1323. /*
  1324. * Isochronous transfers are expected to fail sometimes. As an
  1325. * arbitrary limit, we will report an error if any submissions
  1326. * fail or if the transfer failure rate is > 10%.
  1327. */
  1328. if (status != 0)
  1329. ;
  1330. else if (context.submit_error)
  1331. status = -EACCES;
  1332. else if (context.errors > context.packet_count / 10)
  1333. status = -EIO;
  1334. return status;
  1335. fail:
  1336. for (i = 0; i < param->sglen; i++) {
  1337. if (urbs [i])
  1338. simple_free_urb (urbs [i]);
  1339. }
  1340. return status;
  1341. }
  1342. /*-------------------------------------------------------------------------*/
  1343. /* We only have this one interface to user space, through usbfs.
  1344. * User mode code can scan usbfs to find N different devices (maybe on
  1345. * different busses) to use when testing, and allocate one thread per
  1346. * test. So discovery is simplified, and we have no device naming issues.
  1347. *
  1348. * Don't use these only as stress/load tests. Use them along with with
  1349. * other USB bus activity: plugging, unplugging, mousing, mp3 playback,
  1350. * video capture, and so on. Run different tests at different times, in
  1351. * different sequences. Nothing here should interact with other devices,
  1352. * except indirectly by consuming USB bandwidth and CPU resources for test
  1353. * threads and request completion. But the only way to know that for sure
  1354. * is to test when HC queues are in use by many devices.
  1355. *
  1356. * WARNING: Because usbfs grabs udev->dev.sem before calling this ioctl(),
  1357. * it locks out usbcore in certain code paths. Notably, if you disconnect
  1358. * the device-under-test, khubd will wait block forever waiting for the
  1359. * ioctl to complete ... so that usb_disconnect() can abort the pending
  1360. * urbs and then call usbtest_disconnect(). To abort a test, you're best
  1361. * off just killing the userspace task and waiting for it to exit.
  1362. */
  1363. static int
  1364. usbtest_ioctl (struct usb_interface *intf, unsigned int code, void *buf)
  1365. {
  1366. struct usbtest_dev *dev = usb_get_intfdata (intf);
  1367. struct usb_device *udev = testdev_to_usbdev (dev);
  1368. struct usbtest_param *param = buf;
  1369. int retval = -EOPNOTSUPP;
  1370. struct urb *urb;
  1371. struct scatterlist *sg;
  1372. struct usb_sg_request req;
  1373. struct timeval start;
  1374. unsigned i;
  1375. // FIXME USBDEVFS_CONNECTINFO doesn't say how fast the device is.
  1376. pattern = mod_pattern;
  1377. if (code != USBTEST_REQUEST)
  1378. return -EOPNOTSUPP;
  1379. if (param->iterations <= 0)
  1380. return -EINVAL;
  1381. if (mutex_lock_interruptible(&dev->lock))
  1382. return -ERESTARTSYS;
  1383. /* FIXME: What if a system sleep starts while a test is running? */
  1384. if (!intf->is_active) {
  1385. mutex_unlock(&dev->lock);
  1386. return -EHOSTUNREACH;
  1387. }
  1388. /* some devices, like ez-usb default devices, need a non-default
  1389. * altsetting to have any active endpoints. some tests change
  1390. * altsettings; force a default so most tests don't need to check.
  1391. */
  1392. if (dev->info->alt >= 0) {
  1393. int res;
  1394. if (intf->altsetting->desc.bInterfaceNumber) {
  1395. mutex_unlock(&dev->lock);
  1396. return -ENODEV;
  1397. }
  1398. res = set_altsetting (dev, dev->info->alt);
  1399. if (res) {
  1400. dev_err (&intf->dev,
  1401. "set altsetting to %d failed, %d\n",
  1402. dev->info->alt, res);
  1403. mutex_unlock(&dev->lock);
  1404. return res;
  1405. }
  1406. }
  1407. /*
  1408. * Just a bunch of test cases that every HCD is expected to handle.
  1409. *
  1410. * Some may need specific firmware, though it'd be good to have
  1411. * one firmware image to handle all the test cases.
  1412. *
  1413. * FIXME add more tests! cancel requests, verify the data, control
  1414. * queueing, concurrent read+write threads, and so on.
  1415. */
  1416. do_gettimeofday (&start);
  1417. switch (param->test_num) {
  1418. case 0:
  1419. dev_info(&intf->dev, "TEST 0: NOP\n");
  1420. retval = 0;
  1421. break;
  1422. /* Simple non-queued bulk I/O tests */
  1423. case 1:
  1424. if (dev->out_pipe == 0)
  1425. break;
  1426. dev_info(&intf->dev,
  1427. "TEST 1: write %d bytes %u times\n",
  1428. param->length, param->iterations);
  1429. urb = simple_alloc_urb (udev, dev->out_pipe, param->length);
  1430. if (!urb) {
  1431. retval = -ENOMEM;
  1432. break;
  1433. }
  1434. // FIRMWARE: bulk sink (maybe accepts short writes)
  1435. retval = simple_io(dev, urb, param->iterations, 0, 0, "test1");
  1436. simple_free_urb (urb);
  1437. break;
  1438. case 2:
  1439. if (dev->in_pipe == 0)
  1440. break;
  1441. dev_info(&intf->dev,
  1442. "TEST 2: read %d bytes %u times\n",
  1443. param->length, param->iterations);
  1444. urb = simple_alloc_urb (udev, dev->in_pipe, param->length);
  1445. if (!urb) {
  1446. retval = -ENOMEM;
  1447. break;
  1448. }
  1449. // FIRMWARE: bulk source (maybe generates short writes)
  1450. retval = simple_io(dev, urb, param->iterations, 0, 0, "test2");
  1451. simple_free_urb (urb);
  1452. break;
  1453. case 3:
  1454. if (dev->out_pipe == 0 || param->vary == 0)
  1455. break;
  1456. dev_info(&intf->dev,
  1457. "TEST 3: write/%d 0..%d bytes %u times\n",
  1458. param->vary, param->length, param->iterations);
  1459. urb = simple_alloc_urb (udev, dev->out_pipe, param->length);
  1460. if (!urb) {
  1461. retval = -ENOMEM;
  1462. break;
  1463. }
  1464. // FIRMWARE: bulk sink (maybe accepts short writes)
  1465. retval = simple_io(dev, urb, param->iterations, param->vary,
  1466. 0, "test3");
  1467. simple_free_urb (urb);
  1468. break;
  1469. case 4:
  1470. if (dev->in_pipe == 0 || param->vary == 0)
  1471. break;
  1472. dev_info(&intf->dev,
  1473. "TEST 4: read/%d 0..%d bytes %u times\n",
  1474. param->vary, param->length, param->iterations);
  1475. urb = simple_alloc_urb (udev, dev->in_pipe, param->length);
  1476. if (!urb) {
  1477. retval = -ENOMEM;
  1478. break;
  1479. }
  1480. // FIRMWARE: bulk source (maybe generates short writes)
  1481. retval = simple_io(dev, urb, param->iterations, param->vary,
  1482. 0, "test4");
  1483. simple_free_urb (urb);
  1484. break;
  1485. /* Queued bulk I/O tests */
  1486. case 5:
  1487. if (dev->out_pipe == 0 || param->sglen == 0)
  1488. break;
  1489. dev_info(&intf->dev,
  1490. "TEST 5: write %d sglists %d entries of %d bytes\n",
  1491. param->iterations,
  1492. param->sglen, param->length);
  1493. sg = alloc_sglist (param->sglen, param->length, 0);
  1494. if (!sg) {
  1495. retval = -ENOMEM;
  1496. break;
  1497. }
  1498. // FIRMWARE: bulk sink (maybe accepts short writes)
  1499. retval = perform_sglist(dev, param->iterations, dev->out_pipe,
  1500. &req, sg, param->sglen);
  1501. free_sglist (sg, param->sglen);
  1502. break;
  1503. case 6:
  1504. if (dev->in_pipe == 0 || param->sglen == 0)
  1505. break;
  1506. dev_info(&intf->dev,
  1507. "TEST 6: read %d sglists %d entries of %d bytes\n",
  1508. param->iterations,
  1509. param->sglen, param->length);
  1510. sg = alloc_sglist (param->sglen, param->length, 0);
  1511. if (!sg) {
  1512. retval = -ENOMEM;
  1513. break;
  1514. }
  1515. // FIRMWARE: bulk source (maybe generates short writes)
  1516. retval = perform_sglist(dev, param->iterations, dev->in_pipe,
  1517. &req, sg, param->sglen);
  1518. free_sglist (sg, param->sglen);
  1519. break;
  1520. case 7:
  1521. if (dev->out_pipe == 0 || param->sglen == 0 || param->vary == 0)
  1522. break;
  1523. dev_info(&intf->dev,
  1524. "TEST 7: write/%d %d sglists %d entries 0..%d bytes\n",
  1525. param->vary, param->iterations,
  1526. param->sglen, param->length);
  1527. sg = alloc_sglist (param->sglen, param->length, param->vary);
  1528. if (!sg) {
  1529. retval = -ENOMEM;
  1530. break;
  1531. }
  1532. // FIRMWARE: bulk sink (maybe accepts short writes)
  1533. retval = perform_sglist(dev, param->iterations, dev->out_pipe,
  1534. &req, sg, param->sglen);
  1535. free_sglist (sg, param->sglen);
  1536. break;
  1537. case 8:
  1538. if (dev->in_pipe == 0 || param->sglen == 0 || param->vary == 0)
  1539. break;
  1540. dev_info(&intf->dev,
  1541. "TEST 8: read/%d %d sglists %d entries 0..%d bytes\n",
  1542. param->vary, param->iterations,
  1543. param->sglen, param->length);
  1544. sg = alloc_sglist (param->sglen, param->length, param->vary);
  1545. if (!sg) {
  1546. retval = -ENOMEM;
  1547. break;
  1548. }
  1549. // FIRMWARE: bulk source (maybe generates short writes)
  1550. retval = perform_sglist(dev, param->iterations, dev->in_pipe,
  1551. &req, sg, param->sglen);
  1552. free_sglist (sg, param->sglen);
  1553. break;
  1554. /* non-queued sanity tests for control (chapter 9 subset) */
  1555. case 9:
  1556. retval = 0;
  1557. dev_info(&intf->dev,
  1558. "TEST 9: ch9 (subset) control tests, %d times\n",
  1559. param->iterations);
  1560. for (i = param->iterations; retval == 0 && i--; /* NOP */)
  1561. retval = ch9_postconfig (dev);
  1562. if (retval)
  1563. dev_err(&intf->dev, "ch9 subset failed, "
  1564. "iterations left %d\n", i);
  1565. break;
  1566. /* queued control messaging */
  1567. case 10:
  1568. if (param->sglen == 0)
  1569. break;
  1570. retval = 0;
  1571. dev_info(&intf->dev,
  1572. "TEST 10: queue %d control calls, %d times\n",
  1573. param->sglen,
  1574. param->iterations);
  1575. retval = test_ctrl_queue (dev, param);
  1576. break;
  1577. /* simple non-queued unlinks (ring with one urb) */
  1578. case 11:
  1579. if (dev->in_pipe == 0 || !param->length)
  1580. break;
  1581. retval = 0;
  1582. dev_info(&intf->dev, "TEST 11: unlink %d reads of %d\n",
  1583. param->iterations, param->length);
  1584. for (i = param->iterations; retval == 0 && i--; /* NOP */)
  1585. retval = unlink_simple (dev, dev->in_pipe,
  1586. param->length);
  1587. if (retval)
  1588. dev_err(&intf->dev, "unlink reads failed %d, "
  1589. "iterations left %d\n", retval, i);
  1590. break;
  1591. case 12:
  1592. if (dev->out_pipe == 0 || !param->length)
  1593. break;
  1594. retval = 0;
  1595. dev_info(&intf->dev, "TEST 12: unlink %d writes of %d\n",
  1596. param->iterations, param->length);
  1597. for (i = param->iterations; retval == 0 && i--; /* NOP */)
  1598. retval = unlink_simple (dev, dev->out_pipe,
  1599. param->length);
  1600. if (retval)
  1601. dev_err(&intf->dev, "unlink writes failed %d, "
  1602. "iterations left %d\n", retval, i);
  1603. break;
  1604. /* ep halt tests */
  1605. case 13:
  1606. if (dev->out_pipe == 0 && dev->in_pipe == 0)
  1607. break;
  1608. retval = 0;
  1609. dev_info(&intf->dev, "TEST 13: set/clear %d halts\n",
  1610. param->iterations);
  1611. for (i = param->iterations; retval == 0 && i--; /* NOP */)
  1612. retval = halt_simple (dev);
  1613. if (retval)
  1614. ERROR(dev, "halts failed, iterations left %d\n", i);
  1615. break;
  1616. /* control write tests */
  1617. case 14:
  1618. if (!dev->info->ctrl_out)
  1619. break;
  1620. dev_info(&intf->dev, "TEST 14: %d ep0out, %d..%d vary %d\n",
  1621. param->iterations,
  1622. realworld ? 1 : 0, param->length,
  1623. param->vary);
  1624. retval = ctrl_out(dev, param->iterations,
  1625. param->length, param->vary);
  1626. break;
  1627. /* iso write tests */
  1628. case 15:
  1629. if (dev->out_iso_pipe == 0 || param->sglen == 0)
  1630. break;
  1631. dev_info(&intf->dev,
  1632. "TEST 15: write %d iso, %d entries of %d bytes\n",
  1633. param->iterations,
  1634. param->sglen, param->length);
  1635. // FIRMWARE: iso sink
  1636. retval = test_iso_queue (dev, param,
  1637. dev->out_iso_pipe, dev->iso_out);
  1638. break;
  1639. /* iso read tests */
  1640. case 16:
  1641. if (dev->in_iso_pipe == 0 || param->sglen == 0)
  1642. break;
  1643. dev_info(&intf->dev,
  1644. "TEST 16: read %d iso, %d entries of %d bytes\n",
  1645. param->iterations,
  1646. param->sglen, param->length);
  1647. // FIRMWARE: iso source
  1648. retval = test_iso_queue (dev, param,
  1649. dev->in_iso_pipe, dev->iso_in);
  1650. break;
  1651. // FIXME unlink from queue (ring with N urbs)
  1652. // FIXME scatterlist cancel (needs helper thread)
  1653. }
  1654. do_gettimeofday (&param->duration);
  1655. param->duration.tv_sec -= start.tv_sec;
  1656. param->duration.tv_usec -= start.tv_usec;
  1657. if (param->duration.tv_usec < 0) {
  1658. param->duration.tv_usec += 1000 * 1000;
  1659. param->duration.tv_sec -= 1;
  1660. }
  1661. mutex_unlock(&dev->lock);
  1662. return retval;
  1663. }
  1664. /*-------------------------------------------------------------------------*/
  1665. static unsigned force_interrupt = 0;
  1666. module_param (force_interrupt, uint, 0);
  1667. MODULE_PARM_DESC (force_interrupt, "0 = test default; else interrupt");
  1668. #ifdef GENERIC
  1669. static unsigned short vendor;
  1670. module_param(vendor, ushort, 0);
  1671. MODULE_PARM_DESC (vendor, "vendor code (from usb-if)");
  1672. static unsigned short product;
  1673. module_param(product, ushort, 0);
  1674. MODULE_PARM_DESC (product, "product code (from vendor)");
  1675. #endif
  1676. static int
  1677. usbtest_probe (struct usb_interface *intf, const struct usb_device_id *id)
  1678. {
  1679. struct usb_device *udev;
  1680. struct usbtest_dev *dev;
  1681. struct usbtest_info *info;
  1682. char *rtest, *wtest;
  1683. char *irtest, *iwtest;
  1684. udev = interface_to_usbdev (intf);
  1685. #ifdef GENERIC
  1686. /* specify devices by module parameters? */
  1687. if (id->match_flags == 0) {
  1688. /* vendor match required, product match optional */
  1689. if (!vendor || le16_to_cpu(udev->descriptor.idVendor) != (u16)vendor)
  1690. return -ENODEV;
  1691. if (product && le16_to_cpu(udev->descriptor.idProduct) != (u16)product)
  1692. return -ENODEV;
  1693. dev_info(&intf->dev, "matched module params, "
  1694. "vend=0x%04x prod=0x%04x\n",
  1695. le16_to_cpu(udev->descriptor.idVendor),
  1696. le16_to_cpu(udev->descriptor.idProduct));
  1697. }
  1698. #endif
  1699. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1700. if (!dev)
  1701. return -ENOMEM;
  1702. info = (struct usbtest_info *) id->driver_info;
  1703. dev->info = info;
  1704. mutex_init(&dev->lock);
  1705. dev->intf = intf;
  1706. /* cacheline-aligned scratch for i/o */
  1707. if ((dev->buf = kmalloc (TBUF_SIZE, GFP_KERNEL)) == NULL) {
  1708. kfree (dev);
  1709. return -ENOMEM;
  1710. }
  1711. /* NOTE this doesn't yet test the handful of difference that are
  1712. * visible with high speed interrupts: bigger maxpacket (1K) and
  1713. * "high bandwidth" modes (up to 3 packets/uframe).
  1714. */
  1715. rtest = wtest = "";
  1716. irtest = iwtest = "";
  1717. if (force_interrupt || udev->speed == USB_SPEED_LOW) {
  1718. if (info->ep_in) {
  1719. dev->in_pipe = usb_rcvintpipe (udev, info->ep_in);
  1720. rtest = " intr-in";
  1721. }
  1722. if (info->ep_out) {
  1723. dev->out_pipe = usb_sndintpipe (udev, info->ep_out);
  1724. wtest = " intr-out";
  1725. }
  1726. } else {
  1727. if (info->autoconf) {
  1728. int status;
  1729. status = get_endpoints (dev, intf);
  1730. if (status < 0) {
  1731. WARNING(dev, "couldn't get endpoints, %d\n",
  1732. status);
  1733. return status;
  1734. }
  1735. /* may find bulk or ISO pipes */
  1736. } else {
  1737. if (info->ep_in)
  1738. dev->in_pipe = usb_rcvbulkpipe (udev,
  1739. info->ep_in);
  1740. if (info->ep_out)
  1741. dev->out_pipe = usb_sndbulkpipe (udev,
  1742. info->ep_out);
  1743. }
  1744. if (dev->in_pipe)
  1745. rtest = " bulk-in";
  1746. if (dev->out_pipe)
  1747. wtest = " bulk-out";
  1748. if (dev->in_iso_pipe)
  1749. irtest = " iso-in";
  1750. if (dev->out_iso_pipe)
  1751. iwtest = " iso-out";
  1752. }
  1753. usb_set_intfdata (intf, dev);
  1754. dev_info (&intf->dev, "%s\n", info->name);
  1755. dev_info (&intf->dev, "%s speed {control%s%s%s%s%s} tests%s\n",
  1756. ({ char *tmp;
  1757. switch (udev->speed) {
  1758. case USB_SPEED_LOW: tmp = "low"; break;
  1759. case USB_SPEED_FULL: tmp = "full"; break;
  1760. case USB_SPEED_HIGH: tmp = "high"; break;
  1761. default: tmp = "unknown"; break;
  1762. }; tmp; }),
  1763. info->ctrl_out ? " in/out" : "",
  1764. rtest, wtest,
  1765. irtest, iwtest,
  1766. info->alt >= 0 ? " (+alt)" : "");
  1767. return 0;
  1768. }
  1769. static int usbtest_suspend (struct usb_interface *intf, pm_message_t message)
  1770. {
  1771. return 0;
  1772. }
  1773. static int usbtest_resume (struct usb_interface *intf)
  1774. {
  1775. return 0;
  1776. }
  1777. static void usbtest_disconnect (struct usb_interface *intf)
  1778. {
  1779. struct usbtest_dev *dev = usb_get_intfdata (intf);
  1780. usb_set_intfdata (intf, NULL);
  1781. dev_dbg (&intf->dev, "disconnect\n");
  1782. kfree (dev);
  1783. }
  1784. /* Basic testing only needs a device that can source or sink bulk traffic.
  1785. * Any device can test control transfers (default with GENERIC binding).
  1786. *
  1787. * Several entries work with the default EP0 implementation that's built
  1788. * into EZ-USB chips. There's a default vendor ID which can be overridden
  1789. * by (very) small config EEPROMS, but otherwise all these devices act
  1790. * identically until firmware is loaded: only EP0 works. It turns out
  1791. * to be easy to make other endpoints work, without modifying that EP0
  1792. * behavior. For now, we expect that kind of firmware.
  1793. */
  1794. /* an21xx or fx versions of ez-usb */
  1795. static struct usbtest_info ez1_info = {
  1796. .name = "EZ-USB device",
  1797. .ep_in = 2,
  1798. .ep_out = 2,
  1799. .alt = 1,
  1800. };
  1801. /* fx2 version of ez-usb */
  1802. static struct usbtest_info ez2_info = {
  1803. .name = "FX2 device",
  1804. .ep_in = 6,
  1805. .ep_out = 2,
  1806. .alt = 1,
  1807. };
  1808. /* ezusb family device with dedicated usb test firmware,
  1809. */
  1810. static struct usbtest_info fw_info = {
  1811. .name = "usb test device",
  1812. .ep_in = 2,
  1813. .ep_out = 2,
  1814. .alt = 1,
  1815. .autoconf = 1, // iso and ctrl_out need autoconf
  1816. .ctrl_out = 1,
  1817. .iso = 1, // iso_ep's are #8 in/out
  1818. };
  1819. /* peripheral running Linux and 'zero.c' test firmware, or
  1820. * its user-mode cousin. different versions of this use
  1821. * different hardware with the same vendor/product codes.
  1822. * host side MUST rely on the endpoint descriptors.
  1823. */
  1824. static struct usbtest_info gz_info = {
  1825. .name = "Linux gadget zero",
  1826. .autoconf = 1,
  1827. .ctrl_out = 1,
  1828. .alt = 0,
  1829. };
  1830. static struct usbtest_info um_info = {
  1831. .name = "Linux user mode test driver",
  1832. .autoconf = 1,
  1833. .alt = -1,
  1834. };
  1835. static struct usbtest_info um2_info = {
  1836. .name = "Linux user mode ISO test driver",
  1837. .autoconf = 1,
  1838. .iso = 1,
  1839. .alt = -1,
  1840. };
  1841. #ifdef IBOT2
  1842. /* this is a nice source of high speed bulk data;
  1843. * uses an FX2, with firmware provided in the device
  1844. */
  1845. static struct usbtest_info ibot2_info = {
  1846. .name = "iBOT2 webcam",
  1847. .ep_in = 2,
  1848. .alt = -1,
  1849. };
  1850. #endif
  1851. #ifdef GENERIC
  1852. /* we can use any device to test control traffic */
  1853. static struct usbtest_info generic_info = {
  1854. .name = "Generic USB device",
  1855. .alt = -1,
  1856. };
  1857. #endif
  1858. static struct usb_device_id id_table [] = {
  1859. /*-------------------------------------------------------------*/
  1860. /* EZ-USB devices which download firmware to replace (or in our
  1861. * case augment) the default device implementation.
  1862. */
  1863. /* generic EZ-USB FX controller */
  1864. { USB_DEVICE (0x0547, 0x2235),
  1865. .driver_info = (unsigned long) &ez1_info,
  1866. },
  1867. /* CY3671 development board with EZ-USB FX */
  1868. { USB_DEVICE (0x0547, 0x0080),
  1869. .driver_info = (unsigned long) &ez1_info,
  1870. },
  1871. /* generic EZ-USB FX2 controller (or development board) */
  1872. { USB_DEVICE (0x04b4, 0x8613),
  1873. .driver_info = (unsigned long) &ez2_info,
  1874. },
  1875. /* re-enumerated usb test device firmware */
  1876. { USB_DEVICE (0xfff0, 0xfff0),
  1877. .driver_info = (unsigned long) &fw_info,
  1878. },
  1879. /* "Gadget Zero" firmware runs under Linux */
  1880. { USB_DEVICE (0x0525, 0xa4a0),
  1881. .driver_info = (unsigned long) &gz_info,
  1882. },
  1883. /* so does a user-mode variant */
  1884. { USB_DEVICE (0x0525, 0xa4a4),
  1885. .driver_info = (unsigned long) &um_info,
  1886. },
  1887. /* ... and a user-mode variant that talks iso */
  1888. { USB_DEVICE (0x0525, 0xa4a3),
  1889. .driver_info = (unsigned long) &um2_info,
  1890. },
  1891. #ifdef KEYSPAN_19Qi
  1892. /* Keyspan 19qi uses an21xx (original EZ-USB) */
  1893. // this does not coexist with the real Keyspan 19qi driver!
  1894. { USB_DEVICE (0x06cd, 0x010b),
  1895. .driver_info = (unsigned long) &ez1_info,
  1896. },
  1897. #endif
  1898. /*-------------------------------------------------------------*/
  1899. #ifdef IBOT2
  1900. /* iBOT2 makes a nice source of high speed bulk-in data */
  1901. // this does not coexist with a real iBOT2 driver!
  1902. { USB_DEVICE (0x0b62, 0x0059),
  1903. .driver_info = (unsigned long) &ibot2_info,
  1904. },
  1905. #endif
  1906. /*-------------------------------------------------------------*/
  1907. #ifdef GENERIC
  1908. /* module params can specify devices to use for control tests */
  1909. { .driver_info = (unsigned long) &generic_info, },
  1910. #endif
  1911. /*-------------------------------------------------------------*/
  1912. { }
  1913. };
  1914. MODULE_DEVICE_TABLE (usb, id_table);
  1915. static struct usb_driver usbtest_driver = {
  1916. .name = "usbtest",
  1917. .id_table = id_table,
  1918. .probe = usbtest_probe,
  1919. .ioctl = usbtest_ioctl,
  1920. .disconnect = usbtest_disconnect,
  1921. .suspend = usbtest_suspend,
  1922. .resume = usbtest_resume,
  1923. };
  1924. /*-------------------------------------------------------------------------*/
  1925. static int __init usbtest_init (void)
  1926. {
  1927. #ifdef GENERIC
  1928. if (vendor)
  1929. pr_debug("params: vend=0x%04x prod=0x%04x\n", vendor, product);
  1930. #endif
  1931. return usb_register (&usbtest_driver);
  1932. }
  1933. module_init (usbtest_init);
  1934. static void __exit usbtest_exit (void)
  1935. {
  1936. usb_deregister (&usbtest_driver);
  1937. }
  1938. module_exit (usbtest_exit);
  1939. MODULE_DESCRIPTION ("USB Core/HCD Testing Driver");
  1940. MODULE_LICENSE ("GPL");