usbtest.c 74 KB

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