usbtest.c 70 KB

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