usbtest.c 72 KB

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