usbtest.c 57 KB

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