usbtest.c 62 KB

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