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. /*-------------------------------------------------------------------------*/
  82. static int
  83. get_endpoints(struct usbtest_dev *dev, struct usb_interface *intf)
  84. {
  85. int tmp;
  86. struct usb_host_interface *alt;
  87. struct usb_host_endpoint *in, *out;
  88. struct usb_host_endpoint *iso_in, *iso_out;
  89. struct usb_host_endpoint *int_in, *int_out;
  90. struct usb_device *udev;
  91. for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
  92. unsigned ep;
  93. in = out = NULL;
  94. iso_in = iso_out = NULL;
  95. int_in = int_out = NULL;
  96. alt = intf->altsetting + tmp;
  97. if (override_alt >= 0 &&
  98. override_alt != alt->desc.bAlternateSetting)
  99. continue;
  100. /* take the first altsetting with in-bulk + out-bulk;
  101. * ignore other endpoints and altsettings.
  102. */
  103. for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
  104. struct usb_host_endpoint *e;
  105. e = alt->endpoint + ep;
  106. switch (usb_endpoint_type(&e->desc)) {
  107. case USB_ENDPOINT_XFER_BULK:
  108. break;
  109. case USB_ENDPOINT_XFER_INT:
  110. if (dev->info->intr)
  111. goto try_intr;
  112. case USB_ENDPOINT_XFER_ISOC:
  113. if (dev->info->iso)
  114. goto try_iso;
  115. /* FALLTHROUGH */
  116. default:
  117. continue;
  118. }
  119. if (usb_endpoint_dir_in(&e->desc)) {
  120. if (!in)
  121. in = e;
  122. } else {
  123. if (!out)
  124. out = e;
  125. }
  126. continue;
  127. try_intr:
  128. if (usb_endpoint_dir_in(&e->desc)) {
  129. if (!int_in)
  130. int_in = e;
  131. } else {
  132. if (!int_out)
  133. int_out = e;
  134. }
  135. continue;
  136. try_iso:
  137. if (usb_endpoint_dir_in(&e->desc)) {
  138. if (!iso_in)
  139. iso_in = e;
  140. } else {
  141. if (!iso_out)
  142. iso_out = e;
  143. }
  144. }
  145. if ((in && out) || iso_in || iso_out || int_in || int_out)
  146. goto found;
  147. }
  148. return -EINVAL;
  149. found:
  150. udev = testdev_to_usbdev(dev);
  151. dev->info->alt = alt->desc.bAlternateSetting;
  152. if (alt->desc.bAlternateSetting != 0) {
  153. tmp = usb_set_interface(udev,
  154. alt->desc.bInterfaceNumber,
  155. alt->desc.bAlternateSetting);
  156. if (tmp < 0)
  157. return tmp;
  158. }
  159. if (in) {
  160. dev->in_pipe = usb_rcvbulkpipe(udev,
  161. in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  162. dev->out_pipe = usb_sndbulkpipe(udev,
  163. out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  164. }
  165. if (iso_in) {
  166. dev->iso_in = &iso_in->desc;
  167. dev->in_iso_pipe = usb_rcvisocpipe(udev,
  168. iso_in->desc.bEndpointAddress
  169. & USB_ENDPOINT_NUMBER_MASK);
  170. }
  171. if (iso_out) {
  172. dev->iso_out = &iso_out->desc;
  173. dev->out_iso_pipe = usb_sndisocpipe(udev,
  174. iso_out->desc.bEndpointAddress
  175. & USB_ENDPOINT_NUMBER_MASK);
  176. }
  177. if (int_in) {
  178. dev->int_in = &int_in->desc;
  179. dev->in_int_pipe = usb_rcvintpipe(udev,
  180. int_in->desc.bEndpointAddress
  181. & USB_ENDPOINT_NUMBER_MASK);
  182. }
  183. if (int_out) {
  184. dev->int_out = &int_out->desc;
  185. dev->out_int_pipe = usb_sndintpipe(udev,
  186. int_out->desc.bEndpointAddress
  187. & USB_ENDPOINT_NUMBER_MASK);
  188. }
  189. return 0;
  190. }
  191. /*-------------------------------------------------------------------------*/
  192. /* Support for testing basic non-queued I/O streams.
  193. *
  194. * These just package urbs as requests that can be easily canceled.
  195. * Each urb's data buffer is dynamically allocated; callers can fill
  196. * them with non-zero test data (or test for it) when appropriate.
  197. */
  198. static void simple_callback(struct urb *urb)
  199. {
  200. complete(urb->context);
  201. }
  202. static struct urb *usbtest_alloc_urb(
  203. struct usb_device *udev,
  204. int pipe,
  205. unsigned long bytes,
  206. unsigned transfer_flags,
  207. unsigned offset,
  208. u8 bInterval)
  209. {
  210. struct urb *urb;
  211. urb = usb_alloc_urb(0, GFP_KERNEL);
  212. if (!urb)
  213. return urb;
  214. if (bInterval)
  215. usb_fill_int_urb(urb, udev, pipe, NULL, bytes, simple_callback,
  216. NULL, bInterval);
  217. else
  218. usb_fill_bulk_urb(urb, udev, pipe, NULL, bytes, simple_callback,
  219. NULL);
  220. urb->interval = (udev->speed == USB_SPEED_HIGH)
  221. ? (INTERRUPT_RATE << 3)
  222. : INTERRUPT_RATE;
  223. urb->transfer_flags = transfer_flags;
  224. if (usb_pipein(pipe))
  225. urb->transfer_flags |= URB_SHORT_NOT_OK;
  226. if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
  227. urb->transfer_buffer = usb_alloc_coherent(udev, bytes + offset,
  228. GFP_KERNEL, &urb->transfer_dma);
  229. else
  230. urb->transfer_buffer = kmalloc(bytes + offset, GFP_KERNEL);
  231. if (!urb->transfer_buffer) {
  232. usb_free_urb(urb);
  233. return NULL;
  234. }
  235. /* To test unaligned transfers add an offset and fill the
  236. unused memory with a guard value */
  237. if (offset) {
  238. memset(urb->transfer_buffer, GUARD_BYTE, offset);
  239. urb->transfer_buffer += offset;
  240. if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
  241. urb->transfer_dma += offset;
  242. }
  243. /* For inbound transfers use guard byte so that test fails if
  244. data not correctly copied */
  245. memset(urb->transfer_buffer,
  246. usb_pipein(urb->pipe) ? GUARD_BYTE : 0,
  247. bytes);
  248. return urb;
  249. }
  250. static struct urb *simple_alloc_urb(
  251. struct usb_device *udev,
  252. int pipe,
  253. unsigned long bytes,
  254. u8 bInterval)
  255. {
  256. return usbtest_alloc_urb(udev, pipe, bytes, URB_NO_TRANSFER_DMA_MAP, 0,
  257. bInterval);
  258. }
  259. static unsigned pattern;
  260. static unsigned mod_pattern;
  261. module_param_named(pattern, mod_pattern, uint, S_IRUGO | S_IWUSR);
  262. MODULE_PARM_DESC(mod_pattern, "i/o pattern (0 == zeroes)");
  263. static inline void simple_fill_buf(struct urb *urb)
  264. {
  265. unsigned i;
  266. u8 *buf = urb->transfer_buffer;
  267. unsigned len = urb->transfer_buffer_length;
  268. switch (pattern) {
  269. default:
  270. /* FALLTHROUGH */
  271. case 0:
  272. memset(buf, 0, len);
  273. break;
  274. case 1: /* mod63 */
  275. for (i = 0; i < len; i++)
  276. *buf++ = (u8) (i % 63);
  277. break;
  278. }
  279. }
  280. static inline unsigned long buffer_offset(void *buf)
  281. {
  282. return (unsigned long)buf & (ARCH_KMALLOC_MINALIGN - 1);
  283. }
  284. static int check_guard_bytes(struct usbtest_dev *tdev, struct urb *urb)
  285. {
  286. u8 *buf = urb->transfer_buffer;
  287. u8 *guard = buf - buffer_offset(buf);
  288. unsigned i;
  289. for (i = 0; guard < buf; i++, guard++) {
  290. if (*guard != GUARD_BYTE) {
  291. ERROR(tdev, "guard byte[%d] %d (not %d)\n",
  292. i, *guard, GUARD_BYTE);
  293. return -EINVAL;
  294. }
  295. }
  296. return 0;
  297. }
  298. static int simple_check_buf(struct usbtest_dev *tdev, struct urb *urb)
  299. {
  300. unsigned i;
  301. u8 expected;
  302. u8 *buf = urb->transfer_buffer;
  303. unsigned len = urb->actual_length;
  304. int ret = check_guard_bytes(tdev, urb);
  305. if (ret)
  306. return ret;
  307. for (i = 0; i < len; i++, buf++) {
  308. switch (pattern) {
  309. /* all-zeroes has no synchronization issues */
  310. case 0:
  311. expected = 0;
  312. break;
  313. /* mod63 stays in sync with short-terminated transfers,
  314. * or otherwise when host and gadget agree on how large
  315. * each usb transfer request should be. resync is done
  316. * with set_interface or set_config.
  317. */
  318. case 1: /* mod63 */
  319. expected = i % 63;
  320. break;
  321. /* always fail unsupported patterns */
  322. default:
  323. expected = !*buf;
  324. break;
  325. }
  326. if (*buf == expected)
  327. continue;
  328. ERROR(tdev, "buf[%d] = %d (not %d)\n", i, *buf, expected);
  329. return -EINVAL;
  330. }
  331. return 0;
  332. }
  333. static void simple_free_urb(struct urb *urb)
  334. {
  335. unsigned long offset = buffer_offset(urb->transfer_buffer);
  336. if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
  337. usb_free_coherent(
  338. urb->dev,
  339. urb->transfer_buffer_length + offset,
  340. urb->transfer_buffer - offset,
  341. urb->transfer_dma - offset);
  342. else
  343. kfree(urb->transfer_buffer - offset);
  344. usb_free_urb(urb);
  345. }
  346. static int simple_io(
  347. struct usbtest_dev *tdev,
  348. struct urb *urb,
  349. int iterations,
  350. int vary,
  351. int expected,
  352. const char *label
  353. )
  354. {
  355. struct usb_device *udev = urb->dev;
  356. int max = urb->transfer_buffer_length;
  357. struct completion completion;
  358. int retval = 0;
  359. unsigned long expire;
  360. urb->context = &completion;
  361. while (retval == 0 && iterations-- > 0) {
  362. init_completion(&completion);
  363. if (usb_pipeout(urb->pipe)) {
  364. simple_fill_buf(urb);
  365. urb->transfer_flags |= URB_ZERO_PACKET;
  366. }
  367. retval = usb_submit_urb(urb, GFP_KERNEL);
  368. if (retval != 0)
  369. break;
  370. expire = msecs_to_jiffies(SIMPLE_IO_TIMEOUT);
  371. if (!wait_for_completion_timeout(&completion, expire)) {
  372. usb_kill_urb(urb);
  373. retval = (urb->status == -ENOENT ?
  374. -ETIMEDOUT : urb->status);
  375. } else {
  376. retval = urb->status;
  377. }
  378. urb->dev = udev;
  379. if (retval == 0 && usb_pipein(urb->pipe))
  380. retval = simple_check_buf(tdev, urb);
  381. if (vary) {
  382. int len = urb->transfer_buffer_length;
  383. len += vary;
  384. len %= max;
  385. if (len == 0)
  386. len = (vary < max) ? vary : max;
  387. urb->transfer_buffer_length = len;
  388. }
  389. /* FIXME if endpoint halted, clear halt (and log) */
  390. }
  391. urb->transfer_buffer_length = max;
  392. if (expected != retval)
  393. dev_err(&udev->dev,
  394. "%s failed, iterations left %d, status %d (not %d)\n",
  395. label, iterations, retval, expected);
  396. return retval;
  397. }
  398. /*-------------------------------------------------------------------------*/
  399. /* We use scatterlist primitives to test queued I/O.
  400. * Yes, this also tests the scatterlist primitives.
  401. */
  402. static void free_sglist(struct scatterlist *sg, int nents)
  403. {
  404. unsigned i;
  405. if (!sg)
  406. return;
  407. for (i = 0; i < nents; i++) {
  408. if (!sg_page(&sg[i]))
  409. continue;
  410. kfree(sg_virt(&sg[i]));
  411. }
  412. kfree(sg);
  413. }
  414. static struct scatterlist *
  415. alloc_sglist(int nents, int max, int vary)
  416. {
  417. struct scatterlist *sg;
  418. unsigned i;
  419. unsigned size = max;
  420. if (max == 0)
  421. return NULL;
  422. sg = kmalloc_array(nents, sizeof(*sg), GFP_KERNEL);
  423. if (!sg)
  424. return NULL;
  425. sg_init_table(sg, nents);
  426. for (i = 0; i < nents; i++) {
  427. char *buf;
  428. unsigned j;
  429. buf = kzalloc(size, GFP_KERNEL);
  430. if (!buf) {
  431. free_sglist(sg, i);
  432. return NULL;
  433. }
  434. /* kmalloc pages are always physically contiguous! */
  435. sg_set_buf(&sg[i], buf, size);
  436. switch (pattern) {
  437. case 0:
  438. /* already zeroed */
  439. break;
  440. case 1:
  441. for (j = 0; j < size; j++)
  442. *buf++ = (u8) (j % 63);
  443. break;
  444. }
  445. if (vary) {
  446. size += vary;
  447. size %= max;
  448. if (size == 0)
  449. size = (vary < max) ? vary : max;
  450. }
  451. }
  452. return sg;
  453. }
  454. static void sg_timeout(unsigned long _req)
  455. {
  456. struct usb_sg_request *req = (struct usb_sg_request *) _req;
  457. req->status = -ETIMEDOUT;
  458. usb_sg_cancel(req);
  459. }
  460. static int perform_sglist(
  461. struct usbtest_dev *tdev,
  462. unsigned iterations,
  463. int pipe,
  464. struct usb_sg_request *req,
  465. struct scatterlist *sg,
  466. int nents
  467. )
  468. {
  469. struct usb_device *udev = testdev_to_usbdev(tdev);
  470. int retval = 0;
  471. struct timer_list sg_timer;
  472. setup_timer_on_stack(&sg_timer, sg_timeout, (unsigned long) req);
  473. while (retval == 0 && iterations-- > 0) {
  474. retval = usb_sg_init(req, udev, pipe,
  475. (udev->speed == USB_SPEED_HIGH)
  476. ? (INTERRUPT_RATE << 3)
  477. : INTERRUPT_RATE,
  478. sg, nents, 0, GFP_KERNEL);
  479. if (retval)
  480. break;
  481. mod_timer(&sg_timer, jiffies +
  482. msecs_to_jiffies(SIMPLE_IO_TIMEOUT));
  483. usb_sg_wait(req);
  484. del_timer_sync(&sg_timer);
  485. retval = req->status;
  486. /* FIXME check resulting data pattern */
  487. /* FIXME if endpoint halted, clear halt (and log) */
  488. }
  489. /* FIXME for unlink or fault handling tests, don't report
  490. * failure if retval is as we expected ...
  491. */
  492. if (retval)
  493. ERROR(tdev, "perform_sglist failed, "
  494. "iterations left %d, status %d\n",
  495. iterations, retval);
  496. return retval;
  497. }
  498. /*-------------------------------------------------------------------------*/
  499. /* unqueued control message testing
  500. *
  501. * there's a nice set of device functional requirements in chapter 9 of the
  502. * usb 2.0 spec, which we can apply to ANY device, even ones that don't use
  503. * special test firmware.
  504. *
  505. * we know the device is configured (or suspended) by the time it's visible
  506. * through usbfs. we can't change that, so we won't test enumeration (which
  507. * worked 'well enough' to get here, this time), power management (ditto),
  508. * or remote wakeup (which needs human interaction).
  509. */
  510. static unsigned realworld = 1;
  511. module_param(realworld, uint, 0);
  512. MODULE_PARM_DESC(realworld, "clear to demand stricter spec compliance");
  513. static int get_altsetting(struct usbtest_dev *dev)
  514. {
  515. struct usb_interface *iface = dev->intf;
  516. struct usb_device *udev = interface_to_usbdev(iface);
  517. int retval;
  518. retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  519. USB_REQ_GET_INTERFACE, USB_DIR_IN|USB_RECIP_INTERFACE,
  520. 0, iface->altsetting[0].desc.bInterfaceNumber,
  521. dev->buf, 1, USB_CTRL_GET_TIMEOUT);
  522. switch (retval) {
  523. case 1:
  524. return dev->buf[0];
  525. case 0:
  526. retval = -ERANGE;
  527. /* FALLTHROUGH */
  528. default:
  529. return retval;
  530. }
  531. }
  532. static int set_altsetting(struct usbtest_dev *dev, int alternate)
  533. {
  534. struct usb_interface *iface = dev->intf;
  535. struct usb_device *udev;
  536. if (alternate < 0 || alternate >= 256)
  537. return -EINVAL;
  538. udev = interface_to_usbdev(iface);
  539. return usb_set_interface(udev,
  540. iface->altsetting[0].desc.bInterfaceNumber,
  541. alternate);
  542. }
  543. static int is_good_config(struct usbtest_dev *tdev, int len)
  544. {
  545. struct usb_config_descriptor *config;
  546. if (len < sizeof(*config))
  547. return 0;
  548. config = (struct usb_config_descriptor *) tdev->buf;
  549. switch (config->bDescriptorType) {
  550. case USB_DT_CONFIG:
  551. case USB_DT_OTHER_SPEED_CONFIG:
  552. if (config->bLength != 9) {
  553. ERROR(tdev, "bogus config descriptor length\n");
  554. return 0;
  555. }
  556. /* this bit 'must be 1' but often isn't */
  557. if (!realworld && !(config->bmAttributes & 0x80)) {
  558. ERROR(tdev, "high bit of config attributes not set\n");
  559. return 0;
  560. }
  561. if (config->bmAttributes & 0x1f) { /* reserved == 0 */
  562. ERROR(tdev, "reserved config bits set\n");
  563. return 0;
  564. }
  565. break;
  566. default:
  567. return 0;
  568. }
  569. if (le16_to_cpu(config->wTotalLength) == len) /* read it all */
  570. return 1;
  571. if (le16_to_cpu(config->wTotalLength) >= TBUF_SIZE) /* max partial read */
  572. return 1;
  573. ERROR(tdev, "bogus config descriptor read size\n");
  574. return 0;
  575. }
  576. static int is_good_ext(struct usbtest_dev *tdev, u8 *buf)
  577. {
  578. struct usb_ext_cap_descriptor *ext;
  579. u32 attr;
  580. ext = (struct usb_ext_cap_descriptor *) buf;
  581. if (ext->bLength != USB_DT_USB_EXT_CAP_SIZE) {
  582. ERROR(tdev, "bogus usb 2.0 extension descriptor length\n");
  583. return 0;
  584. }
  585. attr = le32_to_cpu(ext->bmAttributes);
  586. /* bits[1:15] is used and others are reserved */
  587. if (attr & ~0xfffe) { /* reserved == 0 */
  588. ERROR(tdev, "reserved bits set\n");
  589. return 0;
  590. }
  591. return 1;
  592. }
  593. static int is_good_ss_cap(struct usbtest_dev *tdev, u8 *buf)
  594. {
  595. struct usb_ss_cap_descriptor *ss;
  596. ss = (struct usb_ss_cap_descriptor *) buf;
  597. if (ss->bLength != USB_DT_USB_SS_CAP_SIZE) {
  598. ERROR(tdev, "bogus superspeed device capability descriptor length\n");
  599. return 0;
  600. }
  601. /*
  602. * only bit[1] of bmAttributes is used for LTM and others are
  603. * reserved
  604. */
  605. if (ss->bmAttributes & ~0x02) { /* reserved == 0 */
  606. ERROR(tdev, "reserved bits set in bmAttributes\n");
  607. return 0;
  608. }
  609. /* bits[0:3] of wSpeedSupported is used and others are reserved */
  610. if (le16_to_cpu(ss->wSpeedSupported) & ~0x0f) { /* reserved == 0 */
  611. ERROR(tdev, "reserved bits set in wSpeedSupported\n");
  612. return 0;
  613. }
  614. return 1;
  615. }
  616. static int is_good_con_id(struct usbtest_dev *tdev, u8 *buf)
  617. {
  618. struct usb_ss_container_id_descriptor *con_id;
  619. con_id = (struct usb_ss_container_id_descriptor *) buf;
  620. if (con_id->bLength != USB_DT_USB_SS_CONTN_ID_SIZE) {
  621. ERROR(tdev, "bogus container id descriptor length\n");
  622. return 0;
  623. }
  624. if (con_id->bReserved) { /* reserved == 0 */
  625. ERROR(tdev, "reserved bits set\n");
  626. return 0;
  627. }
  628. return 1;
  629. }
  630. /* sanity test for standard requests working with usb_control_mesg() and some
  631. * of the utility functions which use it.
  632. *
  633. * this doesn't test how endpoint halts behave or data toggles get set, since
  634. * we won't do I/O to bulk/interrupt endpoints here (which is how to change
  635. * halt or toggle). toggle testing is impractical without support from hcds.
  636. *
  637. * this avoids failing devices linux would normally work with, by not testing
  638. * config/altsetting operations for devices that only support their defaults.
  639. * such devices rarely support those needless operations.
  640. *
  641. * NOTE that since this is a sanity test, it's not examining boundary cases
  642. * to see if usbcore, hcd, and device all behave right. such testing would
  643. * involve varied read sizes and other operation sequences.
  644. */
  645. static int ch9_postconfig(struct usbtest_dev *dev)
  646. {
  647. struct usb_interface *iface = dev->intf;
  648. struct usb_device *udev = interface_to_usbdev(iface);
  649. int i, alt, retval;
  650. /* [9.2.3] if there's more than one altsetting, we need to be able to
  651. * set and get each one. mostly trusts the descriptors from usbcore.
  652. */
  653. for (i = 0; i < iface->num_altsetting; i++) {
  654. /* 9.2.3 constrains the range here */
  655. alt = iface->altsetting[i].desc.bAlternateSetting;
  656. if (alt < 0 || alt >= iface->num_altsetting) {
  657. dev_err(&iface->dev,
  658. "invalid alt [%d].bAltSetting = %d\n",
  659. i, alt);
  660. }
  661. /* [real world] get/set unimplemented if there's only one */
  662. if (realworld && iface->num_altsetting == 1)
  663. continue;
  664. /* [9.4.10] set_interface */
  665. retval = set_altsetting(dev, alt);
  666. if (retval) {
  667. dev_err(&iface->dev, "can't set_interface = %d, %d\n",
  668. alt, retval);
  669. return retval;
  670. }
  671. /* [9.4.4] get_interface always works */
  672. retval = get_altsetting(dev);
  673. if (retval != alt) {
  674. dev_err(&iface->dev, "get alt should be %d, was %d\n",
  675. alt, retval);
  676. return (retval < 0) ? retval : -EDOM;
  677. }
  678. }
  679. /* [real world] get_config unimplemented if there's only one */
  680. if (!realworld || udev->descriptor.bNumConfigurations != 1) {
  681. int expected = udev->actconfig->desc.bConfigurationValue;
  682. /* [9.4.2] get_configuration always works
  683. * ... although some cheap devices (like one TI Hub I've got)
  684. * won't return config descriptors except before set_config.
  685. */
  686. retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  687. USB_REQ_GET_CONFIGURATION,
  688. USB_DIR_IN | USB_RECIP_DEVICE,
  689. 0, 0, dev->buf, 1, USB_CTRL_GET_TIMEOUT);
  690. if (retval != 1 || dev->buf[0] != expected) {
  691. dev_err(&iface->dev, "get config --> %d %d (1 %d)\n",
  692. retval, dev->buf[0], expected);
  693. return (retval < 0) ? retval : -EDOM;
  694. }
  695. }
  696. /* there's always [9.4.3] a device descriptor [9.6.1] */
  697. retval = usb_get_descriptor(udev, USB_DT_DEVICE, 0,
  698. dev->buf, sizeof(udev->descriptor));
  699. if (retval != sizeof(udev->descriptor)) {
  700. dev_err(&iface->dev, "dev descriptor --> %d\n", retval);
  701. return (retval < 0) ? retval : -EDOM;
  702. }
  703. /*
  704. * there's always [9.4.3] a bos device descriptor [9.6.2] in USB
  705. * 3.0 spec
  706. */
  707. if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0210) {
  708. struct usb_bos_descriptor *bos = NULL;
  709. struct usb_dev_cap_header *header = NULL;
  710. unsigned total, num, length;
  711. u8 *buf;
  712. retval = usb_get_descriptor(udev, USB_DT_BOS, 0, dev->buf,
  713. sizeof(*udev->bos->desc));
  714. if (retval != sizeof(*udev->bos->desc)) {
  715. dev_err(&iface->dev, "bos descriptor --> %d\n", retval);
  716. return (retval < 0) ? retval : -EDOM;
  717. }
  718. bos = (struct usb_bos_descriptor *)dev->buf;
  719. total = le16_to_cpu(bos->wTotalLength);
  720. num = bos->bNumDeviceCaps;
  721. if (total > TBUF_SIZE)
  722. total = TBUF_SIZE;
  723. /*
  724. * get generic device-level capability descriptors [9.6.2]
  725. * in USB 3.0 spec
  726. */
  727. retval = usb_get_descriptor(udev, USB_DT_BOS, 0, dev->buf,
  728. total);
  729. if (retval != total) {
  730. dev_err(&iface->dev, "bos descriptor set --> %d\n",
  731. retval);
  732. return (retval < 0) ? retval : -EDOM;
  733. }
  734. length = sizeof(*udev->bos->desc);
  735. buf = dev->buf;
  736. for (i = 0; i < num; i++) {
  737. buf += length;
  738. if (buf + sizeof(struct usb_dev_cap_header) >
  739. dev->buf + total)
  740. break;
  741. header = (struct usb_dev_cap_header *)buf;
  742. length = header->bLength;
  743. if (header->bDescriptorType !=
  744. USB_DT_DEVICE_CAPABILITY) {
  745. dev_warn(&udev->dev, "not device capability descriptor, skip\n");
  746. continue;
  747. }
  748. switch (header->bDevCapabilityType) {
  749. case USB_CAP_TYPE_EXT:
  750. if (buf + USB_DT_USB_EXT_CAP_SIZE >
  751. dev->buf + total ||
  752. !is_good_ext(dev, buf)) {
  753. dev_err(&iface->dev, "bogus usb 2.0 extension descriptor\n");
  754. return -EDOM;
  755. }
  756. break;
  757. case USB_SS_CAP_TYPE:
  758. if (buf + USB_DT_USB_SS_CAP_SIZE >
  759. dev->buf + total ||
  760. !is_good_ss_cap(dev, buf)) {
  761. dev_err(&iface->dev, "bogus superspeed device capability descriptor\n");
  762. return -EDOM;
  763. }
  764. break;
  765. case CONTAINER_ID_TYPE:
  766. if (buf + USB_DT_USB_SS_CONTN_ID_SIZE >
  767. dev->buf + total ||
  768. !is_good_con_id(dev, buf)) {
  769. dev_err(&iface->dev, "bogus container id descriptor\n");
  770. return -EDOM;
  771. }
  772. break;
  773. default:
  774. break;
  775. }
  776. }
  777. }
  778. /* there's always [9.4.3] at least one config descriptor [9.6.3] */
  779. for (i = 0; i < udev->descriptor.bNumConfigurations; i++) {
  780. retval = usb_get_descriptor(udev, USB_DT_CONFIG, i,
  781. dev->buf, TBUF_SIZE);
  782. if (!is_good_config(dev, retval)) {
  783. dev_err(&iface->dev,
  784. "config [%d] descriptor --> %d\n",
  785. i, retval);
  786. return (retval < 0) ? retval : -EDOM;
  787. }
  788. /* FIXME cross-checking udev->config[i] to make sure usbcore
  789. * parsed it right (etc) would be good testing paranoia
  790. */
  791. }
  792. /* and sometimes [9.2.6.6] speed dependent descriptors */
  793. if (le16_to_cpu(udev->descriptor.bcdUSB) == 0x0200) {
  794. struct usb_qualifier_descriptor *d = NULL;
  795. /* device qualifier [9.6.2] */
  796. retval = usb_get_descriptor(udev,
  797. USB_DT_DEVICE_QUALIFIER, 0, dev->buf,
  798. sizeof(struct usb_qualifier_descriptor));
  799. if (retval == -EPIPE) {
  800. if (udev->speed == USB_SPEED_HIGH) {
  801. dev_err(&iface->dev,
  802. "hs dev qualifier --> %d\n",
  803. retval);
  804. return (retval < 0) ? retval : -EDOM;
  805. }
  806. /* usb2.0 but not high-speed capable; fine */
  807. } else if (retval != sizeof(struct usb_qualifier_descriptor)) {
  808. dev_err(&iface->dev, "dev qualifier --> %d\n", retval);
  809. return (retval < 0) ? retval : -EDOM;
  810. } else
  811. d = (struct usb_qualifier_descriptor *) dev->buf;
  812. /* might not have [9.6.2] any other-speed configs [9.6.4] */
  813. if (d) {
  814. unsigned max = d->bNumConfigurations;
  815. for (i = 0; i < max; i++) {
  816. retval = usb_get_descriptor(udev,
  817. USB_DT_OTHER_SPEED_CONFIG, i,
  818. dev->buf, TBUF_SIZE);
  819. if (!is_good_config(dev, retval)) {
  820. dev_err(&iface->dev,
  821. "other speed config --> %d\n",
  822. retval);
  823. return (retval < 0) ? retval : -EDOM;
  824. }
  825. }
  826. }
  827. }
  828. /* FIXME fetch strings from at least the device descriptor */
  829. /* [9.4.5] get_status always works */
  830. retval = usb_get_status(udev, USB_RECIP_DEVICE, 0, dev->buf);
  831. if (retval) {
  832. dev_err(&iface->dev, "get dev status --> %d\n", retval);
  833. return retval;
  834. }
  835. /* FIXME configuration.bmAttributes says if we could try to set/clear
  836. * the device's remote wakeup feature ... if we can, test that here
  837. */
  838. retval = usb_get_status(udev, USB_RECIP_INTERFACE,
  839. iface->altsetting[0].desc.bInterfaceNumber, dev->buf);
  840. if (retval) {
  841. dev_err(&iface->dev, "get interface status --> %d\n", retval);
  842. return retval;
  843. }
  844. /* FIXME get status for each endpoint in the interface */
  845. return 0;
  846. }
  847. /*-------------------------------------------------------------------------*/
  848. /* use ch9 requests to test whether:
  849. * (a) queues work for control, keeping N subtests queued and
  850. * active (auto-resubmit) for M loops through the queue.
  851. * (b) protocol stalls (control-only) will autorecover.
  852. * it's not like bulk/intr; no halt clearing.
  853. * (c) short control reads are reported and handled.
  854. * (d) queues are always processed in-order
  855. */
  856. struct ctrl_ctx {
  857. spinlock_t lock;
  858. struct usbtest_dev *dev;
  859. struct completion complete;
  860. unsigned count;
  861. unsigned pending;
  862. int status;
  863. struct urb **urb;
  864. struct usbtest_param *param;
  865. int last;
  866. };
  867. #define NUM_SUBCASES 16 /* how many test subcases here? */
  868. struct subcase {
  869. struct usb_ctrlrequest setup;
  870. int number;
  871. int expected;
  872. };
  873. static void ctrl_complete(struct urb *urb)
  874. {
  875. struct ctrl_ctx *ctx = urb->context;
  876. struct usb_ctrlrequest *reqp;
  877. struct subcase *subcase;
  878. int status = urb->status;
  879. reqp = (struct usb_ctrlrequest *)urb->setup_packet;
  880. subcase = container_of(reqp, struct subcase, setup);
  881. spin_lock(&ctx->lock);
  882. ctx->count--;
  883. ctx->pending--;
  884. /* queue must transfer and complete in fifo order, unless
  885. * usb_unlink_urb() is used to unlink something not at the
  886. * physical queue head (not tested).
  887. */
  888. if (subcase->number > 0) {
  889. if ((subcase->number - ctx->last) != 1) {
  890. ERROR(ctx->dev,
  891. "subcase %d completed out of order, last %d\n",
  892. subcase->number, ctx->last);
  893. status = -EDOM;
  894. ctx->last = subcase->number;
  895. goto error;
  896. }
  897. }
  898. ctx->last = subcase->number;
  899. /* succeed or fault in only one way? */
  900. if (status == subcase->expected)
  901. status = 0;
  902. /* async unlink for cleanup? */
  903. else if (status != -ECONNRESET) {
  904. /* some faults are allowed, not required */
  905. if (subcase->expected > 0 && (
  906. ((status == -subcase->expected /* happened */
  907. || status == 0)))) /* didn't */
  908. status = 0;
  909. /* sometimes more than one fault is allowed */
  910. else if (subcase->number == 12 && status == -EPIPE)
  911. status = 0;
  912. else
  913. ERROR(ctx->dev, "subtest %d error, status %d\n",
  914. subcase->number, status);
  915. }
  916. /* unexpected status codes mean errors; ideally, in hardware */
  917. if (status) {
  918. error:
  919. if (ctx->status == 0) {
  920. int i;
  921. ctx->status = status;
  922. ERROR(ctx->dev, "control queue %02x.%02x, err %d, "
  923. "%d left, subcase %d, len %d/%d\n",
  924. reqp->bRequestType, reqp->bRequest,
  925. status, ctx->count, subcase->number,
  926. urb->actual_length,
  927. urb->transfer_buffer_length);
  928. /* FIXME this "unlink everything" exit route should
  929. * be a separate test case.
  930. */
  931. /* unlink whatever's still pending */
  932. for (i = 1; i < ctx->param->sglen; i++) {
  933. struct urb *u = ctx->urb[
  934. (i + subcase->number)
  935. % ctx->param->sglen];
  936. if (u == urb || !u->dev)
  937. continue;
  938. spin_unlock(&ctx->lock);
  939. status = usb_unlink_urb(u);
  940. spin_lock(&ctx->lock);
  941. switch (status) {
  942. case -EINPROGRESS:
  943. case -EBUSY:
  944. case -EIDRM:
  945. continue;
  946. default:
  947. ERROR(ctx->dev, "urb unlink --> %d\n",
  948. status);
  949. }
  950. }
  951. status = ctx->status;
  952. }
  953. }
  954. /* resubmit if we need to, else mark this as done */
  955. if ((status == 0) && (ctx->pending < ctx->count)) {
  956. status = usb_submit_urb(urb, GFP_ATOMIC);
  957. if (status != 0) {
  958. ERROR(ctx->dev,
  959. "can't resubmit ctrl %02x.%02x, err %d\n",
  960. reqp->bRequestType, reqp->bRequest, status);
  961. urb->dev = NULL;
  962. } else
  963. ctx->pending++;
  964. } else
  965. urb->dev = NULL;
  966. /* signal completion when nothing's queued */
  967. if (ctx->pending == 0)
  968. complete(&ctx->complete);
  969. spin_unlock(&ctx->lock);
  970. }
  971. static int
  972. test_ctrl_queue(struct usbtest_dev *dev, struct usbtest_param *param)
  973. {
  974. struct usb_device *udev = testdev_to_usbdev(dev);
  975. struct urb **urb;
  976. struct ctrl_ctx context;
  977. int i;
  978. if (param->sglen == 0 || param->iterations > UINT_MAX / param->sglen)
  979. return -EOPNOTSUPP;
  980. spin_lock_init(&context.lock);
  981. context.dev = dev;
  982. init_completion(&context.complete);
  983. context.count = param->sglen * param->iterations;
  984. context.pending = 0;
  985. context.status = -ENOMEM;
  986. context.param = param;
  987. context.last = -1;
  988. /* allocate and init the urbs we'll queue.
  989. * as with bulk/intr sglists, sglen is the queue depth; it also
  990. * controls which subtests run (more tests than sglen) or rerun.
  991. */
  992. urb = kcalloc(param->sglen, sizeof(struct urb *), GFP_KERNEL);
  993. if (!urb)
  994. return -ENOMEM;
  995. for (i = 0; i < param->sglen; i++) {
  996. int pipe = usb_rcvctrlpipe(udev, 0);
  997. unsigned len;
  998. struct urb *u;
  999. struct usb_ctrlrequest req;
  1000. struct subcase *reqp;
  1001. /* sign of this variable means:
  1002. * -: tested code must return this (negative) error code
  1003. * +: tested code may return this (negative too) error code
  1004. */
  1005. int expected = 0;
  1006. /* requests here are mostly expected to succeed on any
  1007. * device, but some are chosen to trigger protocol stalls
  1008. * or short reads.
  1009. */
  1010. memset(&req, 0, sizeof(req));
  1011. req.bRequest = USB_REQ_GET_DESCRIPTOR;
  1012. req.bRequestType = USB_DIR_IN|USB_RECIP_DEVICE;
  1013. switch (i % NUM_SUBCASES) {
  1014. case 0: /* get device descriptor */
  1015. req.wValue = cpu_to_le16(USB_DT_DEVICE << 8);
  1016. len = sizeof(struct usb_device_descriptor);
  1017. break;
  1018. case 1: /* get first config descriptor (only) */
  1019. req.wValue = cpu_to_le16((USB_DT_CONFIG << 8) | 0);
  1020. len = sizeof(struct usb_config_descriptor);
  1021. break;
  1022. case 2: /* get altsetting (OFTEN STALLS) */
  1023. req.bRequest = USB_REQ_GET_INTERFACE;
  1024. req.bRequestType = USB_DIR_IN|USB_RECIP_INTERFACE;
  1025. /* index = 0 means first interface */
  1026. len = 1;
  1027. expected = EPIPE;
  1028. break;
  1029. case 3: /* get interface status */
  1030. req.bRequest = USB_REQ_GET_STATUS;
  1031. req.bRequestType = USB_DIR_IN|USB_RECIP_INTERFACE;
  1032. /* interface 0 */
  1033. len = 2;
  1034. break;
  1035. case 4: /* get device status */
  1036. req.bRequest = USB_REQ_GET_STATUS;
  1037. req.bRequestType = USB_DIR_IN|USB_RECIP_DEVICE;
  1038. len = 2;
  1039. break;
  1040. case 5: /* get device qualifier (MAY STALL) */
  1041. req.wValue = cpu_to_le16 (USB_DT_DEVICE_QUALIFIER << 8);
  1042. len = sizeof(struct usb_qualifier_descriptor);
  1043. if (udev->speed != USB_SPEED_HIGH)
  1044. expected = EPIPE;
  1045. break;
  1046. case 6: /* get first config descriptor, plus interface */
  1047. req.wValue = cpu_to_le16((USB_DT_CONFIG << 8) | 0);
  1048. len = sizeof(struct usb_config_descriptor);
  1049. len += sizeof(struct usb_interface_descriptor);
  1050. break;
  1051. case 7: /* get interface descriptor (ALWAYS STALLS) */
  1052. req.wValue = cpu_to_le16 (USB_DT_INTERFACE << 8);
  1053. /* interface == 0 */
  1054. len = sizeof(struct usb_interface_descriptor);
  1055. expected = -EPIPE;
  1056. break;
  1057. /* NOTE: two consecutive stalls in the queue here.
  1058. * that tests fault recovery a bit more aggressively. */
  1059. case 8: /* clear endpoint halt (MAY STALL) */
  1060. req.bRequest = USB_REQ_CLEAR_FEATURE;
  1061. req.bRequestType = USB_RECIP_ENDPOINT;
  1062. /* wValue 0 == ep halt */
  1063. /* wIndex 0 == ep0 (shouldn't halt!) */
  1064. len = 0;
  1065. pipe = usb_sndctrlpipe(udev, 0);
  1066. expected = EPIPE;
  1067. break;
  1068. case 9: /* get endpoint status */
  1069. req.bRequest = USB_REQ_GET_STATUS;
  1070. req.bRequestType = USB_DIR_IN|USB_RECIP_ENDPOINT;
  1071. /* endpoint 0 */
  1072. len = 2;
  1073. break;
  1074. case 10: /* trigger short read (EREMOTEIO) */
  1075. req.wValue = cpu_to_le16((USB_DT_CONFIG << 8) | 0);
  1076. len = 1024;
  1077. expected = -EREMOTEIO;
  1078. break;
  1079. /* NOTE: two consecutive _different_ faults in the queue. */
  1080. case 11: /* get endpoint descriptor (ALWAYS STALLS) */
  1081. req.wValue = cpu_to_le16(USB_DT_ENDPOINT << 8);
  1082. /* endpoint == 0 */
  1083. len = sizeof(struct usb_interface_descriptor);
  1084. expected = EPIPE;
  1085. break;
  1086. /* NOTE: sometimes even a third fault in the queue! */
  1087. case 12: /* get string 0 descriptor (MAY STALL) */
  1088. req.wValue = cpu_to_le16(USB_DT_STRING << 8);
  1089. /* string == 0, for language IDs */
  1090. len = sizeof(struct usb_interface_descriptor);
  1091. /* may succeed when > 4 languages */
  1092. expected = EREMOTEIO; /* or EPIPE, if no strings */
  1093. break;
  1094. case 13: /* short read, resembling case 10 */
  1095. req.wValue = cpu_to_le16((USB_DT_CONFIG << 8) | 0);
  1096. /* last data packet "should" be DATA1, not DATA0 */
  1097. if (udev->speed == USB_SPEED_SUPER)
  1098. len = 1024 - 512;
  1099. else
  1100. len = 1024 - udev->descriptor.bMaxPacketSize0;
  1101. expected = -EREMOTEIO;
  1102. break;
  1103. case 14: /* short read; try to fill the last packet */
  1104. req.wValue = cpu_to_le16((USB_DT_DEVICE << 8) | 0);
  1105. /* device descriptor size == 18 bytes */
  1106. len = udev->descriptor.bMaxPacketSize0;
  1107. if (udev->speed == USB_SPEED_SUPER)
  1108. len = 512;
  1109. switch (len) {
  1110. case 8:
  1111. len = 24;
  1112. break;
  1113. case 16:
  1114. len = 32;
  1115. break;
  1116. }
  1117. expected = -EREMOTEIO;
  1118. break;
  1119. case 15:
  1120. req.wValue = cpu_to_le16(USB_DT_BOS << 8);
  1121. if (udev->bos)
  1122. len = le16_to_cpu(udev->bos->desc->wTotalLength);
  1123. else
  1124. len = sizeof(struct usb_bos_descriptor);
  1125. if (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0201)
  1126. expected = -EPIPE;
  1127. break;
  1128. default:
  1129. ERROR(dev, "bogus number of ctrl queue testcases!\n");
  1130. context.status = -EINVAL;
  1131. goto cleanup;
  1132. }
  1133. req.wLength = cpu_to_le16(len);
  1134. urb[i] = u = simple_alloc_urb(udev, pipe, len, 0);
  1135. if (!u)
  1136. goto cleanup;
  1137. reqp = kmalloc(sizeof(*reqp), GFP_KERNEL);
  1138. if (!reqp)
  1139. goto cleanup;
  1140. reqp->setup = req;
  1141. reqp->number = i % NUM_SUBCASES;
  1142. reqp->expected = expected;
  1143. u->setup_packet = (char *) &reqp->setup;
  1144. u->context = &context;
  1145. u->complete = ctrl_complete;
  1146. }
  1147. /* queue the urbs */
  1148. context.urb = urb;
  1149. spin_lock_irq(&context.lock);
  1150. for (i = 0; i < param->sglen; i++) {
  1151. context.status = usb_submit_urb(urb[i], GFP_ATOMIC);
  1152. if (context.status != 0) {
  1153. ERROR(dev, "can't submit urb[%d], status %d\n",
  1154. i, context.status);
  1155. context.count = context.pending;
  1156. break;
  1157. }
  1158. context.pending++;
  1159. }
  1160. spin_unlock_irq(&context.lock);
  1161. /* FIXME set timer and time out; provide a disconnect hook */
  1162. /* wait for the last one to complete */
  1163. if (context.pending > 0)
  1164. wait_for_completion(&context.complete);
  1165. cleanup:
  1166. for (i = 0; i < param->sglen; i++) {
  1167. if (!urb[i])
  1168. continue;
  1169. urb[i]->dev = udev;
  1170. kfree(urb[i]->setup_packet);
  1171. simple_free_urb(urb[i]);
  1172. }
  1173. kfree(urb);
  1174. return context.status;
  1175. }
  1176. #undef NUM_SUBCASES
  1177. /*-------------------------------------------------------------------------*/
  1178. static void unlink1_callback(struct urb *urb)
  1179. {
  1180. int status = urb->status;
  1181. /* we "know" -EPIPE (stall) never happens */
  1182. if (!status)
  1183. status = usb_submit_urb(urb, GFP_ATOMIC);
  1184. if (status) {
  1185. urb->status = status;
  1186. complete(urb->context);
  1187. }
  1188. }
  1189. static int unlink1(struct usbtest_dev *dev, int pipe, int size, int async)
  1190. {
  1191. struct urb *urb;
  1192. struct completion completion;
  1193. int retval = 0;
  1194. init_completion(&completion);
  1195. urb = simple_alloc_urb(testdev_to_usbdev(dev), pipe, size, 0);
  1196. if (!urb)
  1197. return -ENOMEM;
  1198. urb->context = &completion;
  1199. urb->complete = unlink1_callback;
  1200. if (usb_pipeout(urb->pipe)) {
  1201. simple_fill_buf(urb);
  1202. urb->transfer_flags |= URB_ZERO_PACKET;
  1203. }
  1204. /* keep the endpoint busy. there are lots of hc/hcd-internal
  1205. * states, and testing should get to all of them over time.
  1206. *
  1207. * FIXME want additional tests for when endpoint is STALLing
  1208. * due to errors, or is just NAKing requests.
  1209. */
  1210. retval = usb_submit_urb(urb, GFP_KERNEL);
  1211. if (retval != 0) {
  1212. dev_err(&dev->intf->dev, "submit fail %d\n", retval);
  1213. return retval;
  1214. }
  1215. /* unlinking that should always work. variable delay tests more
  1216. * hcd states and code paths, even with little other system load.
  1217. */
  1218. msleep(jiffies % (2 * INTERRUPT_RATE));
  1219. if (async) {
  1220. while (!completion_done(&completion)) {
  1221. retval = usb_unlink_urb(urb);
  1222. if (retval == 0 && usb_pipein(urb->pipe))
  1223. retval = simple_check_buf(dev, urb);
  1224. switch (retval) {
  1225. case -EBUSY:
  1226. case -EIDRM:
  1227. /* we can't unlink urbs while they're completing
  1228. * or if they've completed, and we haven't
  1229. * resubmitted. "normal" drivers would prevent
  1230. * resubmission, but since we're testing unlink
  1231. * paths, we can't.
  1232. */
  1233. ERROR(dev, "unlink retry\n");
  1234. continue;
  1235. case 0:
  1236. case -EINPROGRESS:
  1237. break;
  1238. default:
  1239. dev_err(&dev->intf->dev,
  1240. "unlink fail %d\n", retval);
  1241. return retval;
  1242. }
  1243. break;
  1244. }
  1245. } else
  1246. usb_kill_urb(urb);
  1247. wait_for_completion(&completion);
  1248. retval = urb->status;
  1249. simple_free_urb(urb);
  1250. if (async)
  1251. return (retval == -ECONNRESET) ? 0 : retval - 1000;
  1252. else
  1253. return (retval == -ENOENT || retval == -EPERM) ?
  1254. 0 : retval - 2000;
  1255. }
  1256. static int unlink_simple(struct usbtest_dev *dev, int pipe, int len)
  1257. {
  1258. int retval = 0;
  1259. /* test sync and async paths */
  1260. retval = unlink1(dev, pipe, len, 1);
  1261. if (!retval)
  1262. retval = unlink1(dev, pipe, len, 0);
  1263. return retval;
  1264. }
  1265. /*-------------------------------------------------------------------------*/
  1266. struct queued_ctx {
  1267. struct completion complete;
  1268. atomic_t pending;
  1269. unsigned num;
  1270. int status;
  1271. struct urb **urbs;
  1272. };
  1273. static void unlink_queued_callback(struct urb *urb)
  1274. {
  1275. int status = urb->status;
  1276. struct queued_ctx *ctx = urb->context;
  1277. if (ctx->status)
  1278. goto done;
  1279. if (urb == ctx->urbs[ctx->num - 4] || urb == ctx->urbs[ctx->num - 2]) {
  1280. if (status == -ECONNRESET)
  1281. goto done;
  1282. /* What error should we report if the URB completed normally? */
  1283. }
  1284. if (status != 0)
  1285. ctx->status = status;
  1286. done:
  1287. if (atomic_dec_and_test(&ctx->pending))
  1288. complete(&ctx->complete);
  1289. }
  1290. static int unlink_queued(struct usbtest_dev *dev, int pipe, unsigned num,
  1291. unsigned size)
  1292. {
  1293. struct queued_ctx ctx;
  1294. struct usb_device *udev = testdev_to_usbdev(dev);
  1295. void *buf;
  1296. dma_addr_t buf_dma;
  1297. int i;
  1298. int retval = -ENOMEM;
  1299. init_completion(&ctx.complete);
  1300. atomic_set(&ctx.pending, 1); /* One more than the actual value */
  1301. ctx.num = num;
  1302. ctx.status = 0;
  1303. buf = usb_alloc_coherent(udev, size, GFP_KERNEL, &buf_dma);
  1304. if (!buf)
  1305. return retval;
  1306. memset(buf, 0, size);
  1307. /* Allocate and init the urbs we'll queue */
  1308. ctx.urbs = kcalloc(num, sizeof(struct urb *), GFP_KERNEL);
  1309. if (!ctx.urbs)
  1310. goto free_buf;
  1311. for (i = 0; i < num; i++) {
  1312. ctx.urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
  1313. if (!ctx.urbs[i])
  1314. goto free_urbs;
  1315. usb_fill_bulk_urb(ctx.urbs[i], udev, pipe, buf, size,
  1316. unlink_queued_callback, &ctx);
  1317. ctx.urbs[i]->transfer_dma = buf_dma;
  1318. ctx.urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  1319. if (usb_pipeout(ctx.urbs[i]->pipe)) {
  1320. simple_fill_buf(ctx.urbs[i]);
  1321. ctx.urbs[i]->transfer_flags |= URB_ZERO_PACKET;
  1322. }
  1323. }
  1324. /* Submit all the URBs and then unlink URBs num - 4 and num - 2. */
  1325. for (i = 0; i < num; i++) {
  1326. atomic_inc(&ctx.pending);
  1327. retval = usb_submit_urb(ctx.urbs[i], GFP_KERNEL);
  1328. if (retval != 0) {
  1329. dev_err(&dev->intf->dev, "submit urbs[%d] fail %d\n",
  1330. i, retval);
  1331. atomic_dec(&ctx.pending);
  1332. ctx.status = retval;
  1333. break;
  1334. }
  1335. }
  1336. if (i == num) {
  1337. usb_unlink_urb(ctx.urbs[num - 4]);
  1338. usb_unlink_urb(ctx.urbs[num - 2]);
  1339. } else {
  1340. while (--i >= 0)
  1341. usb_unlink_urb(ctx.urbs[i]);
  1342. }
  1343. if (atomic_dec_and_test(&ctx.pending)) /* The extra count */
  1344. complete(&ctx.complete);
  1345. wait_for_completion(&ctx.complete);
  1346. retval = ctx.status;
  1347. free_urbs:
  1348. for (i = 0; i < num; i++)
  1349. usb_free_urb(ctx.urbs[i]);
  1350. kfree(ctx.urbs);
  1351. free_buf:
  1352. usb_free_coherent(udev, size, buf, buf_dma);
  1353. return retval;
  1354. }
  1355. /*-------------------------------------------------------------------------*/
  1356. static int verify_not_halted(struct usbtest_dev *tdev, int ep, struct urb *urb)
  1357. {
  1358. int retval;
  1359. u16 status;
  1360. /* shouldn't look or act halted */
  1361. retval = usb_get_status(urb->dev, USB_RECIP_ENDPOINT, ep, &status);
  1362. if (retval < 0) {
  1363. ERROR(tdev, "ep %02x couldn't get no-halt status, %d\n",
  1364. ep, retval);
  1365. return retval;
  1366. }
  1367. if (status != 0) {
  1368. ERROR(tdev, "ep %02x bogus status: %04x != 0\n", ep, status);
  1369. return -EINVAL;
  1370. }
  1371. retval = simple_io(tdev, urb, 1, 0, 0, __func__);
  1372. if (retval != 0)
  1373. return -EINVAL;
  1374. return 0;
  1375. }
  1376. static int verify_halted(struct usbtest_dev *tdev, int ep, struct urb *urb)
  1377. {
  1378. int retval;
  1379. u16 status;
  1380. /* should look and act halted */
  1381. retval = usb_get_status(urb->dev, USB_RECIP_ENDPOINT, ep, &status);
  1382. if (retval < 0) {
  1383. ERROR(tdev, "ep %02x couldn't get halt status, %d\n",
  1384. ep, retval);
  1385. return retval;
  1386. }
  1387. if (status != 1) {
  1388. ERROR(tdev, "ep %02x bogus status: %04x != 1\n", ep, status);
  1389. return -EINVAL;
  1390. }
  1391. retval = simple_io(tdev, urb, 1, 0, -EPIPE, __func__);
  1392. if (retval != -EPIPE)
  1393. return -EINVAL;
  1394. retval = simple_io(tdev, urb, 1, 0, -EPIPE, "verify_still_halted");
  1395. if (retval != -EPIPE)
  1396. return -EINVAL;
  1397. return 0;
  1398. }
  1399. static int test_halt(struct usbtest_dev *tdev, int ep, struct urb *urb)
  1400. {
  1401. int retval;
  1402. /* shouldn't look or act halted now */
  1403. retval = verify_not_halted(tdev, ep, urb);
  1404. if (retval < 0)
  1405. return retval;
  1406. /* set halt (protocol test only), verify it worked */
  1407. retval = usb_control_msg(urb->dev, usb_sndctrlpipe(urb->dev, 0),
  1408. USB_REQ_SET_FEATURE, USB_RECIP_ENDPOINT,
  1409. USB_ENDPOINT_HALT, ep,
  1410. NULL, 0, USB_CTRL_SET_TIMEOUT);
  1411. if (retval < 0) {
  1412. ERROR(tdev, "ep %02x couldn't set halt, %d\n", ep, retval);
  1413. return retval;
  1414. }
  1415. retval = verify_halted(tdev, ep, urb);
  1416. if (retval < 0) {
  1417. int ret;
  1418. /* clear halt anyways, else further tests will fail */
  1419. ret = usb_clear_halt(urb->dev, urb->pipe);
  1420. if (ret)
  1421. ERROR(tdev, "ep %02x couldn't clear halt, %d\n",
  1422. ep, ret);
  1423. return retval;
  1424. }
  1425. /* clear halt (tests API + protocol), verify it worked */
  1426. retval = usb_clear_halt(urb->dev, urb->pipe);
  1427. if (retval < 0) {
  1428. ERROR(tdev, "ep %02x couldn't clear halt, %d\n", ep, retval);
  1429. return retval;
  1430. }
  1431. retval = verify_not_halted(tdev, ep, urb);
  1432. if (retval < 0)
  1433. return retval;
  1434. /* NOTE: could also verify SET_INTERFACE clear halts ... */
  1435. return 0;
  1436. }
  1437. static int halt_simple(struct usbtest_dev *dev)
  1438. {
  1439. int ep;
  1440. int retval = 0;
  1441. struct urb *urb;
  1442. struct usb_device *udev = testdev_to_usbdev(dev);
  1443. if (udev->speed == USB_SPEED_SUPER)
  1444. urb = simple_alloc_urb(udev, 0, 1024, 0);
  1445. else
  1446. urb = simple_alloc_urb(udev, 0, 512, 0);
  1447. if (urb == NULL)
  1448. return -ENOMEM;
  1449. if (dev->in_pipe) {
  1450. ep = usb_pipeendpoint(dev->in_pipe) | USB_DIR_IN;
  1451. urb->pipe = dev->in_pipe;
  1452. retval = test_halt(dev, ep, urb);
  1453. if (retval < 0)
  1454. goto done;
  1455. }
  1456. if (dev->out_pipe) {
  1457. ep = usb_pipeendpoint(dev->out_pipe);
  1458. urb->pipe = dev->out_pipe;
  1459. retval = test_halt(dev, ep, urb);
  1460. }
  1461. done:
  1462. simple_free_urb(urb);
  1463. return retval;
  1464. }
  1465. /*-------------------------------------------------------------------------*/
  1466. /* Control OUT tests use the vendor control requests from Intel's
  1467. * USB 2.0 compliance test device: write a buffer, read it back.
  1468. *
  1469. * Intel's spec only _requires_ that it work for one packet, which
  1470. * is pretty weak. Some HCDs place limits here; most devices will
  1471. * need to be able to handle more than one OUT data packet. We'll
  1472. * try whatever we're told to try.
  1473. */
  1474. static int ctrl_out(struct usbtest_dev *dev,
  1475. unsigned count, unsigned length, unsigned vary, unsigned offset)
  1476. {
  1477. unsigned i, j, len;
  1478. int retval;
  1479. u8 *buf;
  1480. char *what = "?";
  1481. struct usb_device *udev;
  1482. if (length < 1 || length > 0xffff || vary >= length)
  1483. return -EINVAL;
  1484. buf = kmalloc(length + offset, GFP_KERNEL);
  1485. if (!buf)
  1486. return -ENOMEM;
  1487. buf += offset;
  1488. udev = testdev_to_usbdev(dev);
  1489. len = length;
  1490. retval = 0;
  1491. /* NOTE: hardware might well act differently if we pushed it
  1492. * with lots back-to-back queued requests.
  1493. */
  1494. for (i = 0; i < count; i++) {
  1495. /* write patterned data */
  1496. for (j = 0; j < len; j++)
  1497. buf[j] = i + j;
  1498. retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  1499. 0x5b, USB_DIR_OUT|USB_TYPE_VENDOR,
  1500. 0, 0, buf, len, USB_CTRL_SET_TIMEOUT);
  1501. if (retval != len) {
  1502. what = "write";
  1503. if (retval >= 0) {
  1504. ERROR(dev, "ctrl_out, wlen %d (expected %d)\n",
  1505. retval, len);
  1506. retval = -EBADMSG;
  1507. }
  1508. break;
  1509. }
  1510. /* read it back -- assuming nothing intervened!! */
  1511. retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  1512. 0x5c, USB_DIR_IN|USB_TYPE_VENDOR,
  1513. 0, 0, buf, len, USB_CTRL_GET_TIMEOUT);
  1514. if (retval != len) {
  1515. what = "read";
  1516. if (retval >= 0) {
  1517. ERROR(dev, "ctrl_out, rlen %d (expected %d)\n",
  1518. retval, len);
  1519. retval = -EBADMSG;
  1520. }
  1521. break;
  1522. }
  1523. /* fail if we can't verify */
  1524. for (j = 0; j < len; j++) {
  1525. if (buf[j] != (u8) (i + j)) {
  1526. ERROR(dev, "ctrl_out, byte %d is %d not %d\n",
  1527. j, buf[j], (u8) i + j);
  1528. retval = -EBADMSG;
  1529. break;
  1530. }
  1531. }
  1532. if (retval < 0) {
  1533. what = "verify";
  1534. break;
  1535. }
  1536. len += vary;
  1537. /* [real world] the "zero bytes IN" case isn't really used.
  1538. * hardware can easily trip up in this weird case, since its
  1539. * status stage is IN, not OUT like other ep0in transfers.
  1540. */
  1541. if (len > length)
  1542. len = realworld ? 1 : 0;
  1543. }
  1544. if (retval < 0)
  1545. ERROR(dev, "ctrl_out %s failed, code %d, count %d\n",
  1546. what, retval, i);
  1547. kfree(buf - offset);
  1548. return retval;
  1549. }
  1550. /*-------------------------------------------------------------------------*/
  1551. /* ISO tests ... mimics common usage
  1552. * - buffer length is split into N packets (mostly maxpacket sized)
  1553. * - multi-buffers according to sglen
  1554. */
  1555. struct iso_context {
  1556. unsigned count;
  1557. unsigned pending;
  1558. spinlock_t lock;
  1559. struct completion done;
  1560. int submit_error;
  1561. unsigned long errors;
  1562. unsigned long packet_count;
  1563. struct usbtest_dev *dev;
  1564. };
  1565. static void iso_callback(struct urb *urb)
  1566. {
  1567. struct iso_context *ctx = urb->context;
  1568. spin_lock(&ctx->lock);
  1569. ctx->count--;
  1570. ctx->packet_count += urb->number_of_packets;
  1571. if (urb->error_count > 0)
  1572. ctx->errors += urb->error_count;
  1573. else if (urb->status != 0)
  1574. ctx->errors += urb->number_of_packets;
  1575. else if (urb->actual_length != urb->transfer_buffer_length)
  1576. ctx->errors++;
  1577. else if (check_guard_bytes(ctx->dev, urb) != 0)
  1578. ctx->errors++;
  1579. if (urb->status == 0 && ctx->count > (ctx->pending - 1)
  1580. && !ctx->submit_error) {
  1581. int status = usb_submit_urb(urb, GFP_ATOMIC);
  1582. switch (status) {
  1583. case 0:
  1584. goto done;
  1585. default:
  1586. dev_err(&ctx->dev->intf->dev,
  1587. "iso resubmit err %d\n",
  1588. status);
  1589. /* FALLTHROUGH */
  1590. case -ENODEV: /* disconnected */
  1591. case -ESHUTDOWN: /* endpoint disabled */
  1592. ctx->submit_error = 1;
  1593. break;
  1594. }
  1595. }
  1596. ctx->pending--;
  1597. if (ctx->pending == 0) {
  1598. if (ctx->errors)
  1599. dev_err(&ctx->dev->intf->dev,
  1600. "iso test, %lu errors out of %lu\n",
  1601. ctx->errors, ctx->packet_count);
  1602. complete(&ctx->done);
  1603. }
  1604. done:
  1605. spin_unlock(&ctx->lock);
  1606. }
  1607. static struct urb *iso_alloc_urb(
  1608. struct usb_device *udev,
  1609. int pipe,
  1610. struct usb_endpoint_descriptor *desc,
  1611. long bytes,
  1612. unsigned offset
  1613. )
  1614. {
  1615. struct urb *urb;
  1616. unsigned i, maxp, packets;
  1617. if (bytes < 0 || !desc)
  1618. return NULL;
  1619. maxp = 0x7ff & usb_endpoint_maxp(desc);
  1620. maxp *= 1 + (0x3 & (usb_endpoint_maxp(desc) >> 11));
  1621. packets = DIV_ROUND_UP(bytes, maxp);
  1622. urb = usb_alloc_urb(packets, GFP_KERNEL);
  1623. if (!urb)
  1624. return urb;
  1625. urb->dev = udev;
  1626. urb->pipe = pipe;
  1627. urb->number_of_packets = packets;
  1628. urb->transfer_buffer_length = bytes;
  1629. urb->transfer_buffer = usb_alloc_coherent(udev, bytes + offset,
  1630. GFP_KERNEL,
  1631. &urb->transfer_dma);
  1632. if (!urb->transfer_buffer) {
  1633. usb_free_urb(urb);
  1634. return NULL;
  1635. }
  1636. if (offset) {
  1637. memset(urb->transfer_buffer, GUARD_BYTE, offset);
  1638. urb->transfer_buffer += offset;
  1639. urb->transfer_dma += offset;
  1640. }
  1641. /* For inbound transfers use guard byte so that test fails if
  1642. data not correctly copied */
  1643. memset(urb->transfer_buffer,
  1644. usb_pipein(urb->pipe) ? GUARD_BYTE : 0,
  1645. bytes);
  1646. for (i = 0; i < packets; i++) {
  1647. /* here, only the last packet will be short */
  1648. urb->iso_frame_desc[i].length = min((unsigned) bytes, maxp);
  1649. bytes -= urb->iso_frame_desc[i].length;
  1650. urb->iso_frame_desc[i].offset = maxp * i;
  1651. }
  1652. urb->complete = iso_callback;
  1653. /* urb->context = SET BY CALLER */
  1654. urb->interval = 1 << (desc->bInterval - 1);
  1655. urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
  1656. return urb;
  1657. }
  1658. static int
  1659. test_iso_queue(struct usbtest_dev *dev, struct usbtest_param *param,
  1660. int pipe, struct usb_endpoint_descriptor *desc, unsigned offset)
  1661. {
  1662. struct iso_context context;
  1663. struct usb_device *udev;
  1664. unsigned i;
  1665. unsigned long packets = 0;
  1666. int status = 0;
  1667. struct urb *urbs[10]; /* FIXME no limit */
  1668. if (param->sglen > 10)
  1669. return -EDOM;
  1670. memset(&context, 0, sizeof(context));
  1671. context.count = param->iterations * param->sglen;
  1672. context.dev = dev;
  1673. init_completion(&context.done);
  1674. spin_lock_init(&context.lock);
  1675. memset(urbs, 0, sizeof(urbs));
  1676. udev = testdev_to_usbdev(dev);
  1677. dev_info(&dev->intf->dev,
  1678. "iso period %d %sframes, wMaxPacket %d, transactions: %d\n",
  1679. 1 << (desc->bInterval - 1),
  1680. (udev->speed == USB_SPEED_HIGH) ? "micro" : "",
  1681. usb_endpoint_maxp(desc) & 0x7ff,
  1682. 1 + (0x3 & (usb_endpoint_maxp(desc) >> 11)));
  1683. for (i = 0; i < param->sglen; i++) {
  1684. urbs[i] = iso_alloc_urb(udev, pipe, desc,
  1685. param->length, offset);
  1686. if (!urbs[i]) {
  1687. status = -ENOMEM;
  1688. goto fail;
  1689. }
  1690. packets += urbs[i]->number_of_packets;
  1691. urbs[i]->context = &context;
  1692. }
  1693. packets *= param->iterations;
  1694. dev_info(&dev->intf->dev,
  1695. "total %lu msec (%lu packets)\n",
  1696. (packets * (1 << (desc->bInterval - 1)))
  1697. / ((udev->speed == USB_SPEED_HIGH) ? 8 : 1),
  1698. packets);
  1699. spin_lock_irq(&context.lock);
  1700. for (i = 0; i < param->sglen; i++) {
  1701. ++context.pending;
  1702. status = usb_submit_urb(urbs[i], GFP_ATOMIC);
  1703. if (status < 0) {
  1704. ERROR(dev, "submit iso[%d], error %d\n", i, status);
  1705. if (i == 0) {
  1706. spin_unlock_irq(&context.lock);
  1707. goto fail;
  1708. }
  1709. simple_free_urb(urbs[i]);
  1710. urbs[i] = NULL;
  1711. context.pending--;
  1712. context.submit_error = 1;
  1713. break;
  1714. }
  1715. }
  1716. spin_unlock_irq(&context.lock);
  1717. wait_for_completion(&context.done);
  1718. for (i = 0; i < param->sglen; i++) {
  1719. if (urbs[i])
  1720. simple_free_urb(urbs[i]);
  1721. }
  1722. /*
  1723. * Isochronous transfers are expected to fail sometimes. As an
  1724. * arbitrary limit, we will report an error if any submissions
  1725. * fail or if the transfer failure rate is > 10%.
  1726. */
  1727. if (status != 0)
  1728. ;
  1729. else if (context.submit_error)
  1730. status = -EACCES;
  1731. else if (context.errors > context.packet_count / 10)
  1732. status = -EIO;
  1733. return status;
  1734. fail:
  1735. for (i = 0; i < param->sglen; i++) {
  1736. if (urbs[i])
  1737. simple_free_urb(urbs[i]);
  1738. }
  1739. return status;
  1740. }
  1741. static int test_unaligned_bulk(
  1742. struct usbtest_dev *tdev,
  1743. int pipe,
  1744. unsigned length,
  1745. int iterations,
  1746. unsigned transfer_flags,
  1747. const char *label)
  1748. {
  1749. int retval;
  1750. struct urb *urb = usbtest_alloc_urb(
  1751. testdev_to_usbdev(tdev), pipe, length, transfer_flags, 1, 0);
  1752. if (!urb)
  1753. return -ENOMEM;
  1754. retval = simple_io(tdev, urb, iterations, 0, 0, label);
  1755. simple_free_urb(urb);
  1756. return retval;
  1757. }
  1758. /*-------------------------------------------------------------------------*/
  1759. /* We only have this one interface to user space, through usbfs.
  1760. * User mode code can scan usbfs to find N different devices (maybe on
  1761. * different busses) to use when testing, and allocate one thread per
  1762. * test. So discovery is simplified, and we have no device naming issues.
  1763. *
  1764. * Don't use these only as stress/load tests. Use them along with with
  1765. * other USB bus activity: plugging, unplugging, mousing, mp3 playback,
  1766. * video capture, and so on. Run different tests at different times, in
  1767. * different sequences. Nothing here should interact with other devices,
  1768. * except indirectly by consuming USB bandwidth and CPU resources for test
  1769. * threads and request completion. But the only way to know that for sure
  1770. * is to test when HC queues are in use by many devices.
  1771. *
  1772. * WARNING: Because usbfs grabs udev->dev.sem before calling this ioctl(),
  1773. * it locks out usbcore in certain code paths. Notably, if you disconnect
  1774. * the device-under-test, hub_wq will wait block forever waiting for the
  1775. * ioctl to complete ... so that usb_disconnect() can abort the pending
  1776. * urbs and then call usbtest_disconnect(). To abort a test, you're best
  1777. * off just killing the userspace task and waiting for it to exit.
  1778. */
  1779. static int
  1780. usbtest_ioctl(struct usb_interface *intf, unsigned int code, void *buf)
  1781. {
  1782. struct usbtest_dev *dev = usb_get_intfdata(intf);
  1783. struct usb_device *udev = testdev_to_usbdev(dev);
  1784. struct usbtest_param *param = buf;
  1785. int retval = -EOPNOTSUPP;
  1786. struct urb *urb;
  1787. struct scatterlist *sg;
  1788. struct usb_sg_request req;
  1789. struct timeval start;
  1790. unsigned i;
  1791. /* FIXME USBDEVFS_CONNECTINFO doesn't say how fast the device is. */
  1792. pattern = mod_pattern;
  1793. if (code != USBTEST_REQUEST)
  1794. return -EOPNOTSUPP;
  1795. if (param->iterations <= 0)
  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");