usbtest.c 57 KB

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