f_sourcesink.c 14 KB

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