usbtest.c 76 KB

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