f_sourcesink.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* #define VERBOSE_DEBUG */
  22. #include <linux/slab.h>
  23. #include <linux/kernel.h>
  24. #include <linux/device.h>
  25. #include "g_zero.h"
  26. #include "gadget_chips.h"
  27. /*
  28. * SOURCE/SINK FUNCTION ... a primary testing vehicle for USB peripheral
  29. * controller drivers.
  30. *
  31. * This just sinks bulk packets OUT to the peripheral and sources them IN
  32. * to the host, optionally with specific data patterns for integrity tests.
  33. * As such it supports basic functionality and load tests.
  34. *
  35. * In terms of control messaging, this supports all the standard requests
  36. * plus two that support control-OUT tests. If the optional "autoresume"
  37. * mode is enabled, it provides good functional coverage for the "USBCV"
  38. * test harness from USB-IF.
  39. *
  40. * Note that because this doesn't queue more than one request at a time,
  41. * some other function must be used to test queueing logic. The network
  42. * link (g_ether) is the best overall option for that, since its TX and RX
  43. * queues are relatively independent, will receive a range of packet sizes,
  44. * and can often be made to run out completely. Those issues are important
  45. * when stress testing peripheral controller drivers.
  46. *
  47. *
  48. * This is currently packaged as a configuration driver, which can't be
  49. * combined with other functions to make composite devices. However, it
  50. * can be combined with other independent configurations.
  51. */
  52. struct f_sourcesink {
  53. struct usb_function function;
  54. struct usb_ep *in_ep;
  55. struct usb_ep *out_ep;
  56. };
  57. static inline struct f_sourcesink *func_to_ss(struct usb_function *f)
  58. {
  59. return container_of(f, struct f_sourcesink, function);
  60. }
  61. static unsigned pattern;
  62. module_param(pattern, uint, 0);
  63. MODULE_PARM_DESC(pattern, "0 = all zeroes, 1 = mod63 ");
  64. /*-------------------------------------------------------------------------*/
  65. static struct usb_interface_descriptor source_sink_intf = {
  66. .bLength = sizeof source_sink_intf,
  67. .bDescriptorType = USB_DT_INTERFACE,
  68. .bNumEndpoints = 2,
  69. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  70. /* .iInterface = DYNAMIC */
  71. };
  72. /* full speed support: */
  73. static struct usb_endpoint_descriptor fs_source_desc = {
  74. .bLength = USB_DT_ENDPOINT_SIZE,
  75. .bDescriptorType = USB_DT_ENDPOINT,
  76. .bEndpointAddress = USB_DIR_IN,
  77. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  78. };
  79. static struct usb_endpoint_descriptor fs_sink_desc = {
  80. .bLength = USB_DT_ENDPOINT_SIZE,
  81. .bDescriptorType = USB_DT_ENDPOINT,
  82. .bEndpointAddress = USB_DIR_OUT,
  83. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  84. };
  85. static struct usb_descriptor_header *fs_source_sink_descs[] = {
  86. (struct usb_descriptor_header *) &source_sink_intf,
  87. (struct usb_descriptor_header *) &fs_sink_desc,
  88. (struct usb_descriptor_header *) &fs_source_desc,
  89. NULL,
  90. };
  91. /* high speed support: */
  92. static struct usb_endpoint_descriptor hs_source_desc = {
  93. .bLength = USB_DT_ENDPOINT_SIZE,
  94. .bDescriptorType = USB_DT_ENDPOINT,
  95. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  96. .wMaxPacketSize = cpu_to_le16(512),
  97. };
  98. static struct usb_endpoint_descriptor hs_sink_desc = {
  99. .bLength = USB_DT_ENDPOINT_SIZE,
  100. .bDescriptorType = USB_DT_ENDPOINT,
  101. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  102. .wMaxPacketSize = cpu_to_le16(512),
  103. };
  104. static struct usb_descriptor_header *hs_source_sink_descs[] = {
  105. (struct usb_descriptor_header *) &source_sink_intf,
  106. (struct usb_descriptor_header *) &hs_source_desc,
  107. (struct usb_descriptor_header *) &hs_sink_desc,
  108. NULL,
  109. };
  110. /* function-specific strings: */
  111. static struct usb_string strings_sourcesink[] = {
  112. [0].s = "source and sink data",
  113. { } /* end of list */
  114. };
  115. static struct usb_gadget_strings stringtab_sourcesink = {
  116. .language = 0x0409, /* en-us */
  117. .strings = strings_sourcesink,
  118. };
  119. static struct usb_gadget_strings *sourcesink_strings[] = {
  120. &stringtab_sourcesink,
  121. NULL,
  122. };
  123. /*-------------------------------------------------------------------------*/
  124. static int __init
  125. sourcesink_bind(struct usb_configuration *c, struct usb_function *f)
  126. {
  127. struct usb_composite_dev *cdev = c->cdev;
  128. struct f_sourcesink *ss = func_to_ss(f);
  129. int id;
  130. /* allocate interface ID(s) */
  131. id = usb_interface_id(c, f);
  132. if (id < 0)
  133. return id;
  134. source_sink_intf.bInterfaceNumber = id;
  135. /* allocate endpoints */
  136. ss->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc);
  137. if (!ss->in_ep) {
  138. autoconf_fail:
  139. ERROR(cdev, "%s: can't autoconfigure on %s\n",
  140. f->name, cdev->gadget->name);
  141. return -ENODEV;
  142. }
  143. ss->in_ep->driver_data = cdev; /* claim */
  144. ss->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc);
  145. if (!ss->out_ep)
  146. goto autoconf_fail;
  147. ss->out_ep->driver_data = cdev; /* claim */
  148. /* support high speed hardware */
  149. if (gadget_is_dualspeed(c->cdev->gadget)) {
  150. hs_source_desc.bEndpointAddress =
  151. fs_source_desc.bEndpointAddress;
  152. hs_sink_desc.bEndpointAddress =
  153. fs_sink_desc.bEndpointAddress;
  154. f->hs_descriptors = hs_source_sink_descs;
  155. }
  156. DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
  157. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  158. f->name, ss->in_ep->name, ss->out_ep->name);
  159. return 0;
  160. }
  161. static void
  162. sourcesink_unbind(struct usb_configuration *c, struct usb_function *f)
  163. {
  164. kfree(func_to_ss(f));
  165. }
  166. /* optionally require specific source/sink data patterns */
  167. static int check_read_data(struct f_sourcesink *ss, struct usb_request *req)
  168. {
  169. unsigned i;
  170. u8 *buf = req->buf;
  171. struct usb_composite_dev *cdev = ss->function.config->cdev;
  172. for (i = 0; i < req->actual; i++, buf++) {
  173. switch (pattern) {
  174. /* all-zeroes has no synchronization issues */
  175. case 0:
  176. if (*buf == 0)
  177. continue;
  178. break;
  179. /* "mod63" stays in sync with short-terminated transfers,
  180. * OR otherwise when host and gadget agree on how large
  181. * each usb transfer request should be. Resync is done
  182. * with set_interface or set_config. (We *WANT* it to
  183. * get quickly out of sync if controllers or their drivers
  184. * stutter for any reason, including buffer duplcation...)
  185. */
  186. case 1:
  187. if (*buf == (u8)(i % 63))
  188. continue;
  189. break;
  190. }
  191. ERROR(cdev, "bad OUT byte, buf[%d] = %d\n", i, *buf);
  192. usb_ep_set_halt(ss->out_ep);
  193. return -EINVAL;
  194. }
  195. return 0;
  196. }
  197. static void reinit_write_data(struct usb_ep *ep, struct usb_request *req)
  198. {
  199. unsigned i;
  200. u8 *buf = req->buf;
  201. switch (pattern) {
  202. case 0:
  203. memset(req->buf, 0, req->length);
  204. break;
  205. case 1:
  206. for (i = 0; i < req->length; i++)
  207. *buf++ = (u8) (i % 63);
  208. break;
  209. }
  210. }
  211. static void source_sink_complete(struct usb_ep *ep, struct usb_request *req)
  212. {
  213. struct f_sourcesink *ss = ep->driver_data;
  214. struct usb_composite_dev *cdev = ss->function.config->cdev;
  215. int status = req->status;
  216. switch (status) {
  217. case 0: /* normal completion? */
  218. if (ep == ss->out_ep) {
  219. check_read_data(ss, req);
  220. memset(req->buf, 0x55, req->length);
  221. } else
  222. reinit_write_data(ep, req);
  223. break;
  224. /* this endpoint is normally active while we're configured */
  225. case -ECONNABORTED: /* hardware forced ep reset */
  226. case -ECONNRESET: /* request dequeued */
  227. case -ESHUTDOWN: /* disconnect from host */
  228. VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
  229. req->actual, req->length);
  230. if (ep == ss->out_ep)
  231. check_read_data(ss, req);
  232. free_ep_req(ep, req);
  233. return;
  234. case -EOVERFLOW: /* buffer overrun on read means that
  235. * we didn't provide a big enough
  236. * buffer.
  237. */
  238. default:
  239. #if 1
  240. DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
  241. status, req->actual, req->length);
  242. #endif
  243. case -EREMOTEIO: /* short read */
  244. break;
  245. }
  246. status = usb_ep_queue(ep, req, GFP_ATOMIC);
  247. if (status) {
  248. ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n",
  249. ep->name, req->length, status);
  250. usb_ep_set_halt(ep);
  251. /* FIXME recover later ... somehow */
  252. }
  253. }
  254. static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in)
  255. {
  256. struct usb_ep *ep;
  257. struct usb_request *req;
  258. int status;
  259. ep = is_in ? ss->in_ep : ss->out_ep;
  260. req = alloc_ep_req(ep);
  261. if (!req)
  262. return -ENOMEM;
  263. req->complete = source_sink_complete;
  264. if (is_in)
  265. reinit_write_data(ep, req);
  266. else
  267. memset(req->buf, 0x55, req->length);
  268. status = usb_ep_queue(ep, req, GFP_ATOMIC);
  269. if (status) {
  270. struct usb_composite_dev *cdev;
  271. cdev = ss->function.config->cdev;
  272. ERROR(cdev, "start %s %s --> %d\n",
  273. is_in ? "IN" : "OUT",
  274. ep->name, status);
  275. free_ep_req(ep, req);
  276. }
  277. return status;
  278. }
  279. static void disable_source_sink(struct f_sourcesink *ss)
  280. {
  281. struct usb_composite_dev *cdev;
  282. cdev = ss->function.config->cdev;
  283. disable_endpoints(cdev, ss->in_ep, ss->out_ep);
  284. VDBG(cdev, "%s disabled\n", ss->function.name);
  285. }
  286. static int
  287. enable_source_sink(struct usb_composite_dev *cdev, struct f_sourcesink *ss)
  288. {
  289. int result = 0;
  290. const struct usb_endpoint_descriptor *src, *sink;
  291. struct usb_ep *ep;
  292. src = ep_choose(cdev->gadget, &hs_source_desc, &fs_source_desc);
  293. sink = ep_choose(cdev->gadget, &hs_sink_desc, &fs_sink_desc);
  294. /* one endpoint writes (sources) zeroes IN (to the host) */
  295. ep = ss->in_ep;
  296. result = usb_ep_enable(ep, src);
  297. if (result < 0)
  298. return result;
  299. ep->driver_data = ss;
  300. result = source_sink_start_ep(ss, true);
  301. if (result < 0) {
  302. fail:
  303. ep = ss->in_ep;
  304. usb_ep_disable(ep);
  305. ep->driver_data = NULL;
  306. return result;
  307. }
  308. /* one endpoint reads (sinks) anything OUT (from the host) */
  309. ep = ss->out_ep;
  310. result = usb_ep_enable(ep, sink);
  311. if (result < 0)
  312. goto fail;
  313. ep->driver_data = ss;
  314. result = source_sink_start_ep(ss, false);
  315. if (result < 0) {
  316. usb_ep_disable(ep);
  317. ep->driver_data = NULL;
  318. goto fail;
  319. }
  320. DBG(cdev, "%s enabled\n", ss->function.name);
  321. return result;
  322. }
  323. static int sourcesink_set_alt(struct usb_function *f,
  324. unsigned intf, unsigned alt)
  325. {
  326. struct f_sourcesink *ss = func_to_ss(f);
  327. struct usb_composite_dev *cdev = f->config->cdev;
  328. /* we know alt is zero */
  329. if (ss->in_ep->driver_data)
  330. disable_source_sink(ss);
  331. return enable_source_sink(cdev, ss);
  332. }
  333. static void sourcesink_disable(struct usb_function *f)
  334. {
  335. struct f_sourcesink *ss = func_to_ss(f);
  336. disable_source_sink(ss);
  337. }
  338. /*-------------------------------------------------------------------------*/
  339. static int __init sourcesink_bind_config(struct usb_configuration *c)
  340. {
  341. struct f_sourcesink *ss;
  342. int status;
  343. ss = kzalloc(sizeof *ss, GFP_KERNEL);
  344. if (!ss)
  345. return -ENOMEM;
  346. ss->function.name = "source/sink";
  347. ss->function.descriptors = fs_source_sink_descs;
  348. ss->function.bind = sourcesink_bind;
  349. ss->function.unbind = sourcesink_unbind;
  350. ss->function.set_alt = sourcesink_set_alt;
  351. ss->function.disable = sourcesink_disable;
  352. status = usb_add_function(c, &ss->function);
  353. if (status)
  354. kfree(ss);
  355. return status;
  356. }
  357. static int sourcesink_setup(struct usb_configuration *c,
  358. const struct usb_ctrlrequest *ctrl)
  359. {
  360. struct usb_request *req = c->cdev->req;
  361. int value = -EOPNOTSUPP;
  362. u16 w_index = le16_to_cpu(ctrl->wIndex);
  363. u16 w_value = le16_to_cpu(ctrl->wValue);
  364. u16 w_length = le16_to_cpu(ctrl->wLength);
  365. /* composite driver infrastructure handles everything except
  366. * the two control test requests.
  367. */
  368. switch (ctrl->bRequest) {
  369. /*
  370. * These are the same vendor-specific requests supported by
  371. * Intel's USB 2.0 compliance test devices. We exceed that
  372. * device spec by allowing multiple-packet requests.
  373. *
  374. * NOTE: the Control-OUT data stays in req->buf ... better
  375. * would be copying it into a scratch buffer, so that other
  376. * requests may safely intervene.
  377. */
  378. case 0x5b: /* control WRITE test -- fill the buffer */
  379. if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
  380. goto unknown;
  381. if (w_value || w_index)
  382. break;
  383. /* just read that many bytes into the buffer */
  384. if (w_length > req->length)
  385. break;
  386. value = w_length;
  387. break;
  388. case 0x5c: /* control READ test -- return the buffer */
  389. if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
  390. goto unknown;
  391. if (w_value || w_index)
  392. break;
  393. /* expect those bytes are still in the buffer; send back */
  394. if (w_length > req->length)
  395. break;
  396. value = w_length;
  397. break;
  398. default:
  399. unknown:
  400. VDBG(c->cdev,
  401. "unknown control req%02x.%02x v%04x i%04x l%d\n",
  402. ctrl->bRequestType, ctrl->bRequest,
  403. w_value, w_index, w_length);
  404. }
  405. /* respond with data transfer or status phase? */
  406. if (value >= 0) {
  407. VDBG(c->cdev, "source/sink req%02x.%02x v%04x i%04x l%d\n",
  408. ctrl->bRequestType, ctrl->bRequest,
  409. w_value, w_index, w_length);
  410. req->zero = 0;
  411. req->length = value;
  412. value = usb_ep_queue(c->cdev->gadget->ep0, req, GFP_ATOMIC);
  413. if (value < 0)
  414. ERROR(c->cdev, "source/sinkc response, err %d\n",
  415. value);
  416. }
  417. /* device either stalls (value < 0) or reports success */
  418. return value;
  419. }
  420. static struct usb_configuration sourcesink_driver = {
  421. .label = "source/sink",
  422. .strings = sourcesink_strings,
  423. .setup = sourcesink_setup,
  424. .bConfigurationValue = 3,
  425. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  426. /* .iConfiguration = DYNAMIC */
  427. };
  428. /**
  429. * sourcesink_add - add a source/sink testing configuration to a device
  430. * @cdev: the device to support the configuration
  431. */
  432. int __init sourcesink_add(struct usb_composite_dev *cdev, bool autoresume)
  433. {
  434. int id;
  435. /* allocate string ID(s) */
  436. id = usb_string_id(cdev);
  437. if (id < 0)
  438. return id;
  439. strings_sourcesink[0].id = id;
  440. source_sink_intf.iInterface = id;
  441. sourcesink_driver.iConfiguration = id;
  442. /* support autoresume for remote wakeup testing */
  443. if (autoresume)
  444. sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  445. /* support OTG systems */
  446. if (gadget_is_otg(cdev->gadget)) {
  447. sourcesink_driver.descriptors = otg_desc;
  448. sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  449. }
  450. return usb_add_config(cdev, &sourcesink_driver, sourcesink_bind_config);
  451. }