usbtest.c 69 KB

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