usbtest.c 75 KB

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