usbtest.c 65 KB

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