usbtest.c 78 KB

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