f_sourcesink.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. /*
  2. * f_sourcesink.c - USB peripheral source/sink configuration driver
  3. *
  4. * Copyright (C) 2003-2008 David Brownell
  5. * Copyright (C) 2008 by Nokia Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. /* #define VERBOSE_DEBUG */
  13. #include <linux/slab.h>
  14. #include <linux/kernel.h>
  15. #include <linux/device.h>
  16. #include <linux/module.h>
  17. #include "g_zero.h"
  18. #include "gadget_chips.h"
  19. /*
  20. * SOURCE/SINK FUNCTION ... a primary testing vehicle for USB peripheral
  21. * controller drivers.
  22. *
  23. * This just sinks bulk packets OUT to the peripheral and sources them IN
  24. * to the host, optionally with specific data patterns for integrity tests.
  25. * As such it supports basic functionality and load tests.
  26. *
  27. * In terms of control messaging, this supports all the standard requests
  28. * plus two that support control-OUT tests. If the optional "autoresume"
  29. * mode is enabled, it provides good functional coverage for the "USBCV"
  30. * test harness from USB-IF.
  31. *
  32. * Note that because this doesn't queue more than one request at a time,
  33. * some other function must be used to test queueing logic. The network
  34. * link (g_ether) is the best overall option for that, since its TX and RX
  35. * queues are relatively independent, will receive a range of packet sizes,
  36. * and can often be made to run out completely. Those issues are important
  37. * when stress testing peripheral controller drivers.
  38. *
  39. *
  40. * This is currently packaged as a configuration driver, which can't be
  41. * combined with other functions to make composite devices. However, it
  42. * can be combined with other independent configurations.
  43. */
  44. struct f_sourcesink {
  45. struct usb_function function;
  46. struct usb_ep *in_ep;
  47. struct usb_ep *out_ep;
  48. struct usb_ep *iso_in_ep;
  49. struct usb_ep *iso_out_ep;
  50. int cur_alt;
  51. };
  52. static inline struct f_sourcesink *func_to_ss(struct usb_function *f)
  53. {
  54. return container_of(f, struct f_sourcesink, function);
  55. }
  56. static unsigned pattern;
  57. module_param(pattern, uint, S_IRUGO|S_IWUSR);
  58. MODULE_PARM_DESC(pattern, "0 = all zeroes, 1 = mod63, 2 = none");
  59. static unsigned isoc_interval = 4;
  60. module_param(isoc_interval, uint, S_IRUGO|S_IWUSR);
  61. MODULE_PARM_DESC(isoc_interval, "1 - 16");
  62. static unsigned isoc_maxpacket = 1024;
  63. module_param(isoc_maxpacket, uint, S_IRUGO|S_IWUSR);
  64. MODULE_PARM_DESC(isoc_maxpacket, "0 - 1023 (fs), 0 - 1024 (hs/ss)");
  65. static unsigned isoc_mult;
  66. module_param(isoc_mult, uint, S_IRUGO|S_IWUSR);
  67. MODULE_PARM_DESC(isoc_mult, "0 - 2 (hs/ss only)");
  68. static unsigned isoc_maxburst;
  69. module_param(isoc_maxburst, uint, S_IRUGO|S_IWUSR);
  70. MODULE_PARM_DESC(isoc_maxburst, "0 - 15 (ss only)");
  71. /*-------------------------------------------------------------------------*/
  72. static struct usb_interface_descriptor source_sink_intf_alt0 = {
  73. .bLength = USB_DT_INTERFACE_SIZE,
  74. .bDescriptorType = USB_DT_INTERFACE,
  75. .bAlternateSetting = 0,
  76. .bNumEndpoints = 2,
  77. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  78. /* .iInterface = DYNAMIC */
  79. };
  80. static struct usb_interface_descriptor source_sink_intf_alt1 = {
  81. .bLength = USB_DT_INTERFACE_SIZE,
  82. .bDescriptorType = USB_DT_INTERFACE,
  83. .bAlternateSetting = 1,
  84. .bNumEndpoints = 4,
  85. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  86. /* .iInterface = DYNAMIC */
  87. };
  88. /* full speed support: */
  89. static struct usb_endpoint_descriptor fs_source_desc = {
  90. .bLength = USB_DT_ENDPOINT_SIZE,
  91. .bDescriptorType = USB_DT_ENDPOINT,
  92. .bEndpointAddress = USB_DIR_IN,
  93. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  94. };
  95. static struct usb_endpoint_descriptor fs_sink_desc = {
  96. .bLength = USB_DT_ENDPOINT_SIZE,
  97. .bDescriptorType = USB_DT_ENDPOINT,
  98. .bEndpointAddress = USB_DIR_OUT,
  99. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  100. };
  101. static struct usb_endpoint_descriptor fs_iso_source_desc = {
  102. .bLength = USB_DT_ENDPOINT_SIZE,
  103. .bDescriptorType = USB_DT_ENDPOINT,
  104. .bEndpointAddress = USB_DIR_IN,
  105. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  106. .wMaxPacketSize = cpu_to_le16(1023),
  107. .bInterval = 4,
  108. };
  109. static struct usb_endpoint_descriptor fs_iso_sink_desc = {
  110. .bLength = USB_DT_ENDPOINT_SIZE,
  111. .bDescriptorType = USB_DT_ENDPOINT,
  112. .bEndpointAddress = USB_DIR_OUT,
  113. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  114. .wMaxPacketSize = cpu_to_le16(1023),
  115. .bInterval = 4,
  116. };
  117. static struct usb_descriptor_header *fs_source_sink_descs[] = {
  118. (struct usb_descriptor_header *) &source_sink_intf_alt0,
  119. (struct usb_descriptor_header *) &fs_sink_desc,
  120. (struct usb_descriptor_header *) &fs_source_desc,
  121. (struct usb_descriptor_header *) &source_sink_intf_alt1,
  122. #define FS_ALT_IFC_1_OFFSET 3
  123. (struct usb_descriptor_header *) &fs_sink_desc,
  124. (struct usb_descriptor_header *) &fs_source_desc,
  125. (struct usb_descriptor_header *) &fs_iso_sink_desc,
  126. (struct usb_descriptor_header *) &fs_iso_source_desc,
  127. NULL,
  128. };
  129. /* high speed support: */
  130. static struct usb_endpoint_descriptor hs_source_desc = {
  131. .bLength = USB_DT_ENDPOINT_SIZE,
  132. .bDescriptorType = USB_DT_ENDPOINT,
  133. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  134. .wMaxPacketSize = cpu_to_le16(512),
  135. };
  136. static struct usb_endpoint_descriptor hs_sink_desc = {
  137. .bLength = USB_DT_ENDPOINT_SIZE,
  138. .bDescriptorType = USB_DT_ENDPOINT,
  139. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  140. .wMaxPacketSize = cpu_to_le16(512),
  141. };
  142. static struct usb_endpoint_descriptor hs_iso_source_desc = {
  143. .bLength = USB_DT_ENDPOINT_SIZE,
  144. .bDescriptorType = USB_DT_ENDPOINT,
  145. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  146. .wMaxPacketSize = cpu_to_le16(1024),
  147. .bInterval = 4,
  148. };
  149. static struct usb_endpoint_descriptor hs_iso_sink_desc = {
  150. .bLength = USB_DT_ENDPOINT_SIZE,
  151. .bDescriptorType = USB_DT_ENDPOINT,
  152. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  153. .wMaxPacketSize = cpu_to_le16(1024),
  154. .bInterval = 4,
  155. };
  156. static struct usb_descriptor_header *hs_source_sink_descs[] = {
  157. (struct usb_descriptor_header *) &source_sink_intf_alt0,
  158. (struct usb_descriptor_header *) &hs_source_desc,
  159. (struct usb_descriptor_header *) &hs_sink_desc,
  160. (struct usb_descriptor_header *) &source_sink_intf_alt1,
  161. #define HS_ALT_IFC_1_OFFSET 3
  162. (struct usb_descriptor_header *) &hs_source_desc,
  163. (struct usb_descriptor_header *) &hs_sink_desc,
  164. (struct usb_descriptor_header *) &hs_iso_source_desc,
  165. (struct usb_descriptor_header *) &hs_iso_sink_desc,
  166. NULL,
  167. };
  168. /* super speed support: */
  169. static struct usb_endpoint_descriptor ss_source_desc = {
  170. .bLength = USB_DT_ENDPOINT_SIZE,
  171. .bDescriptorType = USB_DT_ENDPOINT,
  172. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  173. .wMaxPacketSize = cpu_to_le16(1024),
  174. };
  175. struct usb_ss_ep_comp_descriptor ss_source_comp_desc = {
  176. .bLength = USB_DT_SS_EP_COMP_SIZE,
  177. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  178. .bMaxBurst = 0,
  179. .bmAttributes = 0,
  180. .wBytesPerInterval = 0,
  181. };
  182. static struct usb_endpoint_descriptor ss_sink_desc = {
  183. .bLength = USB_DT_ENDPOINT_SIZE,
  184. .bDescriptorType = USB_DT_ENDPOINT,
  185. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  186. .wMaxPacketSize = cpu_to_le16(1024),
  187. };
  188. struct usb_ss_ep_comp_descriptor ss_sink_comp_desc = {
  189. .bLength = USB_DT_SS_EP_COMP_SIZE,
  190. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  191. .bMaxBurst = 0,
  192. .bmAttributes = 0,
  193. .wBytesPerInterval = 0,
  194. };
  195. static struct usb_endpoint_descriptor ss_iso_source_desc = {
  196. .bLength = USB_DT_ENDPOINT_SIZE,
  197. .bDescriptorType = USB_DT_ENDPOINT,
  198. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  199. .wMaxPacketSize = cpu_to_le16(1024),
  200. .bInterval = 4,
  201. };
  202. struct usb_ss_ep_comp_descriptor ss_iso_source_comp_desc = {
  203. .bLength = USB_DT_SS_EP_COMP_SIZE,
  204. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  205. .bMaxBurst = 0,
  206. .bmAttributes = 0,
  207. .wBytesPerInterval = cpu_to_le16(1024),
  208. };
  209. static struct usb_endpoint_descriptor ss_iso_sink_desc = {
  210. .bLength = USB_DT_ENDPOINT_SIZE,
  211. .bDescriptorType = USB_DT_ENDPOINT,
  212. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  213. .wMaxPacketSize = cpu_to_le16(1024),
  214. .bInterval = 4,
  215. };
  216. struct usb_ss_ep_comp_descriptor ss_iso_sink_comp_desc = {
  217. .bLength = USB_DT_SS_EP_COMP_SIZE,
  218. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  219. .bMaxBurst = 0,
  220. .bmAttributes = 0,
  221. .wBytesPerInterval = cpu_to_le16(1024),
  222. };
  223. static struct usb_descriptor_header *ss_source_sink_descs[] = {
  224. (struct usb_descriptor_header *) &source_sink_intf_alt0,
  225. (struct usb_descriptor_header *) &ss_source_desc,
  226. (struct usb_descriptor_header *) &ss_source_comp_desc,
  227. (struct usb_descriptor_header *) &ss_sink_desc,
  228. (struct usb_descriptor_header *) &ss_sink_comp_desc,
  229. (struct usb_descriptor_header *) &source_sink_intf_alt1,
  230. #define SS_ALT_IFC_1_OFFSET 5
  231. (struct usb_descriptor_header *) &ss_source_desc,
  232. (struct usb_descriptor_header *) &ss_source_comp_desc,
  233. (struct usb_descriptor_header *) &ss_sink_desc,
  234. (struct usb_descriptor_header *) &ss_sink_comp_desc,
  235. (struct usb_descriptor_header *) &ss_iso_source_desc,
  236. (struct usb_descriptor_header *) &ss_iso_source_comp_desc,
  237. (struct usb_descriptor_header *) &ss_iso_sink_desc,
  238. (struct usb_descriptor_header *) &ss_iso_sink_comp_desc,
  239. NULL,
  240. };
  241. /* function-specific strings: */
  242. static struct usb_string strings_sourcesink[] = {
  243. [0].s = "source and sink data",
  244. { } /* end of list */
  245. };
  246. static struct usb_gadget_strings stringtab_sourcesink = {
  247. .language = 0x0409, /* en-us */
  248. .strings = strings_sourcesink,
  249. };
  250. static struct usb_gadget_strings *sourcesink_strings[] = {
  251. &stringtab_sourcesink,
  252. NULL,
  253. };
  254. /*-------------------------------------------------------------------------*/
  255. static int __init
  256. sourcesink_bind(struct usb_configuration *c, struct usb_function *f)
  257. {
  258. struct usb_composite_dev *cdev = c->cdev;
  259. struct f_sourcesink *ss = func_to_ss(f);
  260. int id;
  261. int ret;
  262. /* allocate interface ID(s) */
  263. id = usb_interface_id(c, f);
  264. if (id < 0)
  265. return id;
  266. source_sink_intf_alt0.bInterfaceNumber = id;
  267. source_sink_intf_alt1.bInterfaceNumber = id;
  268. /* allocate bulk endpoints */
  269. ss->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc);
  270. if (!ss->in_ep) {
  271. autoconf_fail:
  272. ERROR(cdev, "%s: can't autoconfigure on %s\n",
  273. f->name, cdev->gadget->name);
  274. return -ENODEV;
  275. }
  276. ss->in_ep->driver_data = cdev; /* claim */
  277. ss->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc);
  278. if (!ss->out_ep)
  279. goto autoconf_fail;
  280. ss->out_ep->driver_data = cdev; /* claim */
  281. /* sanity check the isoc module parameters */
  282. if (isoc_interval < 1)
  283. isoc_interval = 1;
  284. if (isoc_interval > 16)
  285. isoc_interval = 16;
  286. if (isoc_mult > 2)
  287. isoc_mult = 2;
  288. if (isoc_maxburst > 15)
  289. isoc_maxburst = 15;
  290. /* fill in the FS isoc descriptors from the module parameters */
  291. fs_iso_source_desc.wMaxPacketSize = isoc_maxpacket > 1023 ?
  292. 1023 : isoc_maxpacket;
  293. fs_iso_source_desc.bInterval = isoc_interval;
  294. fs_iso_sink_desc.wMaxPacketSize = isoc_maxpacket > 1023 ?
  295. 1023 : isoc_maxpacket;
  296. fs_iso_sink_desc.bInterval = isoc_interval;
  297. /* allocate iso endpoints */
  298. ss->iso_in_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_source_desc);
  299. if (!ss->iso_in_ep)
  300. goto no_iso;
  301. ss->iso_in_ep->driver_data = cdev; /* claim */
  302. ss->iso_out_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_sink_desc);
  303. if (ss->iso_out_ep) {
  304. ss->iso_out_ep->driver_data = cdev; /* claim */
  305. } else {
  306. ss->iso_in_ep->driver_data = NULL;
  307. ss->iso_in_ep = NULL;
  308. no_iso:
  309. /*
  310. * We still want to work even if the UDC doesn't have isoc
  311. * endpoints, so null out the alt interface that contains
  312. * them and continue.
  313. */
  314. fs_source_sink_descs[FS_ALT_IFC_1_OFFSET] = NULL;
  315. hs_source_sink_descs[HS_ALT_IFC_1_OFFSET] = NULL;
  316. ss_source_sink_descs[SS_ALT_IFC_1_OFFSET] = NULL;
  317. }
  318. if (isoc_maxpacket > 1024)
  319. isoc_maxpacket = 1024;
  320. /* support high speed hardware */
  321. hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
  322. hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
  323. /*
  324. * Fill in the HS isoc descriptors from the module parameters.
  325. * We assume that the user knows what they are doing and won't
  326. * give parameters that their UDC doesn't support.
  327. */
  328. hs_iso_source_desc.wMaxPacketSize = isoc_maxpacket;
  329. hs_iso_source_desc.wMaxPacketSize |= isoc_mult << 11;
  330. hs_iso_source_desc.bInterval = isoc_interval;
  331. hs_iso_source_desc.bEndpointAddress =
  332. fs_iso_source_desc.bEndpointAddress;
  333. hs_iso_sink_desc.wMaxPacketSize = isoc_maxpacket;
  334. hs_iso_sink_desc.wMaxPacketSize |= isoc_mult << 11;
  335. hs_iso_sink_desc.bInterval = isoc_interval;
  336. hs_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress;
  337. /* support super speed hardware */
  338. ss_source_desc.bEndpointAddress =
  339. fs_source_desc.bEndpointAddress;
  340. ss_sink_desc.bEndpointAddress =
  341. fs_sink_desc.bEndpointAddress;
  342. /*
  343. * Fill in the SS isoc descriptors from the module parameters.
  344. * We assume that the user knows what they are doing and won't
  345. * give parameters that their UDC doesn't support.
  346. */
  347. ss_iso_source_desc.wMaxPacketSize = isoc_maxpacket;
  348. ss_iso_source_desc.bInterval = isoc_interval;
  349. ss_iso_source_comp_desc.bmAttributes = isoc_mult;
  350. ss_iso_source_comp_desc.bMaxBurst = isoc_maxburst;
  351. ss_iso_source_comp_desc.wBytesPerInterval =
  352. isoc_maxpacket * (isoc_mult + 1) * (isoc_maxburst + 1);
  353. ss_iso_source_desc.bEndpointAddress =
  354. fs_iso_source_desc.bEndpointAddress;
  355. ss_iso_sink_desc.wMaxPacketSize = isoc_maxpacket;
  356. ss_iso_sink_desc.bInterval = isoc_interval;
  357. ss_iso_sink_comp_desc.bmAttributes = isoc_mult;
  358. ss_iso_sink_comp_desc.bMaxBurst = isoc_maxburst;
  359. ss_iso_sink_comp_desc.wBytesPerInterval =
  360. isoc_maxpacket * (isoc_mult + 1) * (isoc_maxburst + 1);
  361. ss_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress;
  362. ret = usb_assign_descriptors(f, fs_source_sink_descs,
  363. hs_source_sink_descs, ss_source_sink_descs);
  364. if (ret)
  365. return ret;
  366. DBG(cdev, "%s speed %s: IN/%s, OUT/%s, ISO-IN/%s, ISO-OUT/%s\n",
  367. (gadget_is_superspeed(c->cdev->gadget) ? "super" :
  368. (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")),
  369. f->name, ss->in_ep->name, ss->out_ep->name,
  370. ss->iso_in_ep ? ss->iso_in_ep->name : "<none>",
  371. ss->iso_out_ep ? ss->iso_out_ep->name : "<none>");
  372. return 0;
  373. }
  374. static void
  375. sourcesink_unbind(struct usb_configuration *c, struct usb_function *f)
  376. {
  377. usb_free_all_descriptors(f);
  378. kfree(func_to_ss(f));
  379. }
  380. /* optionally require specific source/sink data patterns */
  381. static int check_read_data(struct f_sourcesink *ss, struct usb_request *req)
  382. {
  383. unsigned i;
  384. u8 *buf = req->buf;
  385. struct usb_composite_dev *cdev = ss->function.config->cdev;
  386. if (pattern == 2)
  387. return 0;
  388. for (i = 0; i < req->actual; i++, buf++) {
  389. switch (pattern) {
  390. /* all-zeroes has no synchronization issues */
  391. case 0:
  392. if (*buf == 0)
  393. continue;
  394. break;
  395. /* "mod63" stays in sync with short-terminated transfers,
  396. * OR otherwise when host and gadget agree on how large
  397. * each usb transfer request should be. Resync is done
  398. * with set_interface or set_config. (We *WANT* it to
  399. * get quickly out of sync if controllers or their drivers
  400. * stutter for any reason, including buffer duplication...)
  401. */
  402. case 1:
  403. if (*buf == (u8)(i % 63))
  404. continue;
  405. break;
  406. }
  407. ERROR(cdev, "bad OUT byte, buf[%d] = %d\n", i, *buf);
  408. usb_ep_set_halt(ss->out_ep);
  409. return -EINVAL;
  410. }
  411. return 0;
  412. }
  413. static void reinit_write_data(struct usb_ep *ep, struct usb_request *req)
  414. {
  415. unsigned i;
  416. u8 *buf = req->buf;
  417. switch (pattern) {
  418. case 0:
  419. memset(req->buf, 0, req->length);
  420. break;
  421. case 1:
  422. for (i = 0; i < req->length; i++)
  423. *buf++ = (u8) (i % 63);
  424. break;
  425. case 2:
  426. break;
  427. }
  428. }
  429. static void source_sink_complete(struct usb_ep *ep, struct usb_request *req)
  430. {
  431. struct usb_composite_dev *cdev;
  432. struct f_sourcesink *ss = ep->driver_data;
  433. int status = req->status;
  434. /* driver_data will be null if ep has been disabled */
  435. if (!ss)
  436. return;
  437. cdev = ss->function.config->cdev;
  438. switch (status) {
  439. case 0: /* normal completion? */
  440. if (ep == ss->out_ep) {
  441. check_read_data(ss, req);
  442. if (pattern != 2)
  443. memset(req->buf, 0x55, req->length);
  444. } else
  445. reinit_write_data(ep, req);
  446. break;
  447. /* this endpoint is normally active while we're configured */
  448. case -ECONNABORTED: /* hardware forced ep reset */
  449. case -ECONNRESET: /* request dequeued */
  450. case -ESHUTDOWN: /* disconnect from host */
  451. VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
  452. req->actual, req->length);
  453. if (ep == ss->out_ep)
  454. check_read_data(ss, req);
  455. free_ep_req(ep, req);
  456. return;
  457. case -EOVERFLOW: /* buffer overrun on read means that
  458. * we didn't provide a big enough
  459. * buffer.
  460. */
  461. default:
  462. #if 1
  463. DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
  464. status, req->actual, req->length);
  465. #endif
  466. case -EREMOTEIO: /* short read */
  467. break;
  468. }
  469. status = usb_ep_queue(ep, req, GFP_ATOMIC);
  470. if (status) {
  471. ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n",
  472. ep->name, req->length, status);
  473. usb_ep_set_halt(ep);
  474. /* FIXME recover later ... somehow */
  475. }
  476. }
  477. static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in,
  478. bool is_iso, int speed)
  479. {
  480. struct usb_ep *ep;
  481. struct usb_request *req;
  482. int i, size, status;
  483. for (i = 0; i < 8; i++) {
  484. if (is_iso) {
  485. switch (speed) {
  486. case USB_SPEED_SUPER:
  487. size = isoc_maxpacket * (isoc_mult + 1) *
  488. (isoc_maxburst + 1);
  489. break;
  490. case USB_SPEED_HIGH:
  491. size = isoc_maxpacket * (isoc_mult + 1);
  492. break;
  493. default:
  494. size = isoc_maxpacket > 1023 ?
  495. 1023 : isoc_maxpacket;
  496. break;
  497. }
  498. ep = is_in ? ss->iso_in_ep : ss->iso_out_ep;
  499. req = alloc_ep_req(ep, size);
  500. } else {
  501. ep = is_in ? ss->in_ep : ss->out_ep;
  502. req = alloc_ep_req(ep, 0);
  503. }
  504. if (!req)
  505. return -ENOMEM;
  506. req->complete = source_sink_complete;
  507. if (is_in)
  508. reinit_write_data(ep, req);
  509. else if (pattern != 2)
  510. memset(req->buf, 0x55, req->length);
  511. status = usb_ep_queue(ep, req, GFP_ATOMIC);
  512. if (status) {
  513. struct usb_composite_dev *cdev;
  514. cdev = ss->function.config->cdev;
  515. ERROR(cdev, "start %s%s %s --> %d\n",
  516. is_iso ? "ISO-" : "", is_in ? "IN" : "OUT",
  517. ep->name, status);
  518. free_ep_req(ep, req);
  519. }
  520. if (!is_iso)
  521. break;
  522. }
  523. return status;
  524. }
  525. static void disable_source_sink(struct f_sourcesink *ss)
  526. {
  527. struct usb_composite_dev *cdev;
  528. cdev = ss->function.config->cdev;
  529. disable_endpoints(cdev, ss->in_ep, ss->out_ep, ss->iso_in_ep,
  530. ss->iso_out_ep);
  531. VDBG(cdev, "%s disabled\n", ss->function.name);
  532. }
  533. static int
  534. enable_source_sink(struct usb_composite_dev *cdev, struct f_sourcesink *ss,
  535. int alt)
  536. {
  537. int result = 0;
  538. int speed = cdev->gadget->speed;
  539. struct usb_ep *ep;
  540. /* one bulk endpoint writes (sources) zeroes IN (to the host) */
  541. ep = ss->in_ep;
  542. result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
  543. if (result)
  544. return result;
  545. result = usb_ep_enable(ep);
  546. if (result < 0)
  547. return result;
  548. ep->driver_data = ss;
  549. result = source_sink_start_ep(ss, true, false, speed);
  550. if (result < 0) {
  551. fail:
  552. ep = ss->in_ep;
  553. usb_ep_disable(ep);
  554. ep->driver_data = NULL;
  555. return result;
  556. }
  557. /* one bulk endpoint reads (sinks) anything OUT (from the host) */
  558. ep = ss->out_ep;
  559. result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
  560. if (result)
  561. goto fail;
  562. result = usb_ep_enable(ep);
  563. if (result < 0)
  564. goto fail;
  565. ep->driver_data = ss;
  566. result = source_sink_start_ep(ss, false, false, speed);
  567. if (result < 0) {
  568. fail2:
  569. ep = ss->out_ep;
  570. usb_ep_disable(ep);
  571. ep->driver_data = NULL;
  572. goto fail;
  573. }
  574. if (alt == 0)
  575. goto out;
  576. /* one iso endpoint writes (sources) zeroes IN (to the host) */
  577. ep = ss->iso_in_ep;
  578. if (ep) {
  579. result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
  580. if (result)
  581. goto fail2;
  582. result = usb_ep_enable(ep);
  583. if (result < 0)
  584. goto fail2;
  585. ep->driver_data = ss;
  586. result = source_sink_start_ep(ss, true, true, speed);
  587. if (result < 0) {
  588. fail3:
  589. ep = ss->iso_in_ep;
  590. if (ep) {
  591. usb_ep_disable(ep);
  592. ep->driver_data = NULL;
  593. }
  594. goto fail2;
  595. }
  596. }
  597. /* one iso endpoint reads (sinks) anything OUT (from the host) */
  598. ep = ss->iso_out_ep;
  599. if (ep) {
  600. result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
  601. if (result)
  602. goto fail3;
  603. result = usb_ep_enable(ep);
  604. if (result < 0)
  605. goto fail3;
  606. ep->driver_data = ss;
  607. result = source_sink_start_ep(ss, false, true, speed);
  608. if (result < 0) {
  609. usb_ep_disable(ep);
  610. ep->driver_data = NULL;
  611. goto fail3;
  612. }
  613. }
  614. out:
  615. ss->cur_alt = alt;
  616. DBG(cdev, "%s enabled, alt intf %d\n", ss->function.name, alt);
  617. return result;
  618. }
  619. static int sourcesink_set_alt(struct usb_function *f,
  620. unsigned intf, unsigned alt)
  621. {
  622. struct f_sourcesink *ss = func_to_ss(f);
  623. struct usb_composite_dev *cdev = f->config->cdev;
  624. if (ss->in_ep->driver_data)
  625. disable_source_sink(ss);
  626. return enable_source_sink(cdev, ss, alt);
  627. }
  628. static int sourcesink_get_alt(struct usb_function *f, unsigned intf)
  629. {
  630. struct f_sourcesink *ss = func_to_ss(f);
  631. return ss->cur_alt;
  632. }
  633. static void sourcesink_disable(struct usb_function *f)
  634. {
  635. struct f_sourcesink *ss = func_to_ss(f);
  636. disable_source_sink(ss);
  637. }
  638. /*-------------------------------------------------------------------------*/
  639. static int __init sourcesink_bind_config(struct usb_configuration *c)
  640. {
  641. struct f_sourcesink *ss;
  642. int status;
  643. ss = kzalloc(sizeof *ss, GFP_KERNEL);
  644. if (!ss)
  645. return -ENOMEM;
  646. ss->function.name = "source/sink";
  647. ss->function.bind = sourcesink_bind;
  648. ss->function.unbind = sourcesink_unbind;
  649. ss->function.set_alt = sourcesink_set_alt;
  650. ss->function.get_alt = sourcesink_get_alt;
  651. ss->function.disable = sourcesink_disable;
  652. status = usb_add_function(c, &ss->function);
  653. if (status)
  654. kfree(ss);
  655. return status;
  656. }
  657. static int sourcesink_setup(struct usb_configuration *c,
  658. const struct usb_ctrlrequest *ctrl)
  659. {
  660. struct usb_request *req = c->cdev->req;
  661. int value = -EOPNOTSUPP;
  662. u16 w_index = le16_to_cpu(ctrl->wIndex);
  663. u16 w_value = le16_to_cpu(ctrl->wValue);
  664. u16 w_length = le16_to_cpu(ctrl->wLength);
  665. req->length = USB_COMP_EP0_BUFSIZ;
  666. /* composite driver infrastructure handles everything except
  667. * the two control test requests.
  668. */
  669. switch (ctrl->bRequest) {
  670. /*
  671. * These are the same vendor-specific requests supported by
  672. * Intel's USB 2.0 compliance test devices. We exceed that
  673. * device spec by allowing multiple-packet requests.
  674. *
  675. * NOTE: the Control-OUT data stays in req->buf ... better
  676. * would be copying it into a scratch buffer, so that other
  677. * requests may safely intervene.
  678. */
  679. case 0x5b: /* control WRITE test -- fill the buffer */
  680. if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
  681. goto unknown;
  682. if (w_value || w_index)
  683. break;
  684. /* just read that many bytes into the buffer */
  685. if (w_length > req->length)
  686. break;
  687. value = w_length;
  688. break;
  689. case 0x5c: /* control READ test -- return the buffer */
  690. if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
  691. goto unknown;
  692. if (w_value || w_index)
  693. break;
  694. /* expect those bytes are still in the buffer; send back */
  695. if (w_length > req->length)
  696. break;
  697. value = w_length;
  698. break;
  699. default:
  700. unknown:
  701. VDBG(c->cdev,
  702. "unknown control req%02x.%02x v%04x i%04x l%d\n",
  703. ctrl->bRequestType, ctrl->bRequest,
  704. w_value, w_index, w_length);
  705. }
  706. /* respond with data transfer or status phase? */
  707. if (value >= 0) {
  708. VDBG(c->cdev, "source/sink req%02x.%02x v%04x i%04x l%d\n",
  709. ctrl->bRequestType, ctrl->bRequest,
  710. w_value, w_index, w_length);
  711. req->zero = 0;
  712. req->length = value;
  713. value = usb_ep_queue(c->cdev->gadget->ep0, req, GFP_ATOMIC);
  714. if (value < 0)
  715. ERROR(c->cdev, "source/sink response, err %d\n",
  716. value);
  717. }
  718. /* device either stalls (value < 0) or reports success */
  719. return value;
  720. }
  721. static struct usb_configuration sourcesink_driver = {
  722. .label = "source/sink",
  723. .strings = sourcesink_strings,
  724. .setup = sourcesink_setup,
  725. .bConfigurationValue = 3,
  726. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  727. /* .iConfiguration = DYNAMIC */
  728. };
  729. /**
  730. * sourcesink_add - add a source/sink testing configuration to a device
  731. * @cdev: the device to support the configuration
  732. */
  733. int __init sourcesink_add(struct usb_composite_dev *cdev, bool autoresume)
  734. {
  735. int id;
  736. /* allocate string ID(s) */
  737. id = usb_string_id(cdev);
  738. if (id < 0)
  739. return id;
  740. strings_sourcesink[0].id = id;
  741. source_sink_intf_alt0.iInterface = id;
  742. source_sink_intf_alt1.iInterface = id;
  743. sourcesink_driver.iConfiguration = id;
  744. /* support autoresume for remote wakeup testing */
  745. if (autoresume)
  746. sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  747. /* support OTG systems */
  748. if (gadget_is_otg(cdev->gadget)) {
  749. sourcesink_driver.descriptors = otg_desc;
  750. sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  751. }
  752. return usb_add_config(cdev, &sourcesink_driver, sourcesink_bind_config);
  753. }