usbtest.c 57 KB

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