usbtest.c 74 KB

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