usbtest.c 65 KB

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