usbtest.c 76 KB

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