usbtest.c 57 KB

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