usbtest.c 57 KB

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