usbtest.c 57 KB

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