usbtest.c 70 KB

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