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