usbtest.c 72 KB

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