usbtest.c 57 KB

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