f_sourcesink.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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/utsname.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. struct timer_list resume;
  57. };
  58. static inline struct f_sourcesink *func_to_ss(struct usb_function *f)
  59. {
  60. return container_of(f, struct f_sourcesink, function);
  61. }
  62. static unsigned autoresume;
  63. module_param(autoresume, uint, 0);
  64. MODULE_PARM_DESC(autoresume, "zero, or seconds before remote wakeup");
  65. static unsigned pattern;
  66. module_param(pattern, uint, 0);
  67. MODULE_PARM_DESC(pattern, "0 = all zeroes, 1 = mod63 ");
  68. /*-------------------------------------------------------------------------*/
  69. static struct usb_interface_descriptor source_sink_intf = {
  70. .bLength = sizeof source_sink_intf,
  71. .bDescriptorType = USB_DT_INTERFACE,
  72. .bNumEndpoints = 2,
  73. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  74. /* .iInterface = DYNAMIC */
  75. };
  76. /* full speed support: */
  77. static struct usb_endpoint_descriptor fs_source_desc = {
  78. .bLength = USB_DT_ENDPOINT_SIZE,
  79. .bDescriptorType = USB_DT_ENDPOINT,
  80. .bEndpointAddress = USB_DIR_IN,
  81. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  82. };
  83. static struct usb_endpoint_descriptor fs_sink_desc = {
  84. .bLength = USB_DT_ENDPOINT_SIZE,
  85. .bDescriptorType = USB_DT_ENDPOINT,
  86. .bEndpointAddress = USB_DIR_OUT,
  87. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  88. };
  89. static struct usb_descriptor_header *fs_source_sink_descs[] = {
  90. (struct usb_descriptor_header *) &source_sink_intf,
  91. (struct usb_descriptor_header *) &fs_sink_desc,
  92. (struct usb_descriptor_header *) &fs_source_desc,
  93. NULL,
  94. };
  95. /* high speed support: */
  96. static struct usb_endpoint_descriptor hs_source_desc = {
  97. .bLength = USB_DT_ENDPOINT_SIZE,
  98. .bDescriptorType = USB_DT_ENDPOINT,
  99. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  100. .wMaxPacketSize = __constant_cpu_to_le16(512),
  101. };
  102. static struct usb_endpoint_descriptor hs_sink_desc = {
  103. .bLength = USB_DT_ENDPOINT_SIZE,
  104. .bDescriptorType = USB_DT_ENDPOINT,
  105. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  106. .wMaxPacketSize = __constant_cpu_to_le16(512),
  107. };
  108. static struct usb_descriptor_header *hs_source_sink_descs[] = {
  109. (struct usb_descriptor_header *) &source_sink_intf,
  110. (struct usb_descriptor_header *) &hs_source_desc,
  111. (struct usb_descriptor_header *) &hs_sink_desc,
  112. NULL,
  113. };
  114. /* function-specific strings: */
  115. static struct usb_string strings_sourcesink[] = {
  116. [0].s = "source and sink data",
  117. { } /* end of list */
  118. };
  119. static struct usb_gadget_strings stringtab_sourcesink = {
  120. .language = 0x0409, /* en-us */
  121. .strings = strings_sourcesink,
  122. };
  123. static struct usb_gadget_strings *sourcesink_strings[] = {
  124. &stringtab_sourcesink,
  125. NULL,
  126. };
  127. /*-------------------------------------------------------------------------*/
  128. static void sourcesink_autoresume(unsigned long _c)
  129. {
  130. struct usb_composite_dev *cdev = (void *)_c;
  131. struct usb_gadget *g = cdev->gadget;
  132. /* Normally the host would be woken up for something
  133. * more significant than just a timer firing; likely
  134. * because of some direct user request.
  135. */
  136. if (g->speed != USB_SPEED_UNKNOWN) {
  137. int status = usb_gadget_wakeup(g);
  138. DBG(cdev, "%s --> %d\n", __func__, status);
  139. }
  140. }
  141. static int __init
  142. sourcesink_bind(struct usb_configuration *c, struct usb_function *f)
  143. {
  144. struct usb_composite_dev *cdev = c->cdev;
  145. struct f_sourcesink *ss = func_to_ss(f);
  146. int id;
  147. /* allocate interface ID(s) */
  148. id = usb_interface_id(c, f);
  149. if (id < 0)
  150. return id;
  151. source_sink_intf.bInterfaceNumber = id;
  152. /* allocate endpoints */
  153. ss->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc);
  154. if (!ss->in_ep) {
  155. autoconf_fail:
  156. ERROR(cdev, "%s: can't autoconfigure on %s\n",
  157. f->name, cdev->gadget->name);
  158. return -ENODEV;
  159. }
  160. ss->in_ep->driver_data = cdev; /* claim */
  161. ss->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc);
  162. if (!ss->out_ep)
  163. goto autoconf_fail;
  164. ss->out_ep->driver_data = cdev; /* claim */
  165. setup_timer(&ss->resume, sourcesink_autoresume,
  166. (unsigned long) c->cdev);
  167. /* support high speed hardware */
  168. if (gadget_is_dualspeed(c->cdev->gadget)) {
  169. hs_source_desc.bEndpointAddress =
  170. fs_source_desc.bEndpointAddress;
  171. hs_sink_desc.bEndpointAddress =
  172. fs_sink_desc.bEndpointAddress;
  173. f->hs_descriptors = hs_source_sink_descs;
  174. }
  175. DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
  176. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  177. f->name, ss->in_ep->name, ss->out_ep->name);
  178. return 0;
  179. }
  180. static void
  181. sourcesink_unbind(struct usb_configuration *c, struct usb_function *f)
  182. {
  183. kfree(func_to_ss(f));
  184. }
  185. /* optionally require specific source/sink data patterns */
  186. static int check_read_data(struct f_sourcesink *ss, struct usb_request *req)
  187. {
  188. unsigned i;
  189. u8 *buf = req->buf;
  190. struct usb_composite_dev *cdev = ss->function.config->cdev;
  191. for (i = 0; i < req->actual; i++, buf++) {
  192. switch (pattern) {
  193. /* all-zeroes has no synchronization issues */
  194. case 0:
  195. if (*buf == 0)
  196. continue;
  197. break;
  198. /* "mod63" stays in sync with short-terminated transfers,
  199. * OR otherwise when host and gadget agree on how large
  200. * each usb transfer request should be. Resync is done
  201. * with set_interface or set_config. (We *WANT* it to
  202. * get quickly out of sync if controllers or their drivers
  203. * stutter for any reason, including buffer duplcation...)
  204. */
  205. case 1:
  206. if (*buf == (u8)(i % 63))
  207. continue;
  208. break;
  209. }
  210. ERROR(cdev, "bad OUT byte, buf[%d] = %d\n", i, *buf);
  211. usb_ep_set_halt(ss->out_ep);
  212. return -EINVAL;
  213. }
  214. return 0;
  215. }
  216. static void reinit_write_data(struct usb_ep *ep, struct usb_request *req)
  217. {
  218. unsigned i;
  219. u8 *buf = req->buf;
  220. switch (pattern) {
  221. case 0:
  222. memset(req->buf, 0, req->length);
  223. break;
  224. case 1:
  225. for (i = 0; i < req->length; i++)
  226. *buf++ = (u8) (i % 63);
  227. break;
  228. }
  229. }
  230. static void source_sink_complete(struct usb_ep *ep, struct usb_request *req)
  231. {
  232. struct f_sourcesink *ss = ep->driver_data;
  233. struct usb_composite_dev *cdev = ss->function.config->cdev;
  234. int status = req->status;
  235. switch (status) {
  236. case 0: /* normal completion? */
  237. if (ep == ss->out_ep) {
  238. check_read_data(ss, req);
  239. memset(req->buf, 0x55, req->length);
  240. } else
  241. reinit_write_data(ep, req);
  242. break;
  243. /* this endpoint is normally active while we're configured */
  244. case -ECONNABORTED: /* hardware forced ep reset */
  245. case -ECONNRESET: /* request dequeued */
  246. case -ESHUTDOWN: /* disconnect from host */
  247. VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
  248. req->actual, req->length);
  249. if (ep == ss->out_ep)
  250. check_read_data(ss, req);
  251. free_ep_req(ep, req);
  252. return;
  253. case -EOVERFLOW: /* buffer overrun on read means that
  254. * we didn't provide a big enough
  255. * buffer.
  256. */
  257. default:
  258. #if 1
  259. DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
  260. status, req->actual, req->length);
  261. #endif
  262. case -EREMOTEIO: /* short read */
  263. break;
  264. }
  265. status = usb_ep_queue(ep, req, GFP_ATOMIC);
  266. if (status) {
  267. ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n",
  268. ep->name, req->length, status);
  269. usb_ep_set_halt(ep);
  270. /* FIXME recover later ... somehow */
  271. }
  272. }
  273. static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in)
  274. {
  275. struct usb_ep *ep;
  276. struct usb_request *req;
  277. int status;
  278. ep = is_in ? ss->in_ep : ss->out_ep;
  279. req = alloc_ep_req(ep);
  280. if (!req)
  281. return -ENOMEM;
  282. req->complete = source_sink_complete;
  283. if (is_in)
  284. reinit_write_data(ep, req);
  285. else
  286. memset(req->buf, 0x55, req->length);
  287. status = usb_ep_queue(ep, req, GFP_ATOMIC);
  288. if (status) {
  289. struct usb_composite_dev *cdev;
  290. cdev = ss->function.config->cdev;
  291. ERROR(cdev, "start %s %s --> %d\n",
  292. is_in ? "IN" : "OUT",
  293. ep->name, status);
  294. free_ep_req(ep, req);
  295. }
  296. return status;
  297. }
  298. static void disable_source_sink(struct f_sourcesink *ss)
  299. {
  300. struct usb_composite_dev *cdev;
  301. cdev = ss->function.config->cdev;
  302. disable_endpoints(cdev, ss->in_ep, ss->out_ep);
  303. del_timer(&ss->resume);
  304. VDBG(cdev, "%s disabled\n", ss->function.name);
  305. }
  306. static int
  307. enable_source_sink(struct usb_composite_dev *cdev, struct f_sourcesink *ss)
  308. {
  309. int result = 0;
  310. const struct usb_endpoint_descriptor *src, *sink;
  311. struct usb_ep *ep;
  312. src = ep_choose(cdev->gadget, &hs_source_desc, &fs_source_desc);
  313. sink = ep_choose(cdev->gadget, &hs_sink_desc, &fs_sink_desc);
  314. /* one endpoint writes (sources) zeroes IN (to the host) */
  315. ep = ss->in_ep;
  316. result = usb_ep_enable(ep, src);
  317. if (result < 0)
  318. return result;
  319. ep->driver_data = ss;
  320. result = source_sink_start_ep(ss, true);
  321. if (result < 0) {
  322. fail:
  323. ep = ss->in_ep;
  324. usb_ep_disable(ep);
  325. ep->driver_data = NULL;
  326. return result;
  327. }
  328. /* one endpoint reads (sinks) anything OUT (from the host) */
  329. ep = ss->out_ep;
  330. result = usb_ep_enable(ep, sink);
  331. if (result < 0)
  332. goto fail;
  333. ep->driver_data = ss;
  334. result = source_sink_start_ep(ss, false);
  335. if (result < 0) {
  336. usb_ep_disable(ep);
  337. ep->driver_data = NULL;
  338. goto fail;
  339. }
  340. DBG(cdev, "%s enabled\n", ss->function.name);
  341. return result;
  342. }
  343. static int sourcesink_set_alt(struct usb_function *f,
  344. unsigned intf, unsigned alt)
  345. {
  346. struct f_sourcesink *ss = func_to_ss(f);
  347. struct usb_composite_dev *cdev = f->config->cdev;
  348. /* we know alt is zero */
  349. if (ss->in_ep->driver_data)
  350. disable_source_sink(ss);
  351. return enable_source_sink(cdev, ss);
  352. }
  353. static void sourcesink_disable(struct usb_function *f)
  354. {
  355. struct f_sourcesink *ss = func_to_ss(f);
  356. disable_source_sink(ss);
  357. }
  358. static void sourcesink_suspend(struct usb_function *f)
  359. {
  360. struct f_sourcesink *ss = func_to_ss(f);
  361. struct usb_composite_dev *cdev = f->config->cdev;
  362. if (cdev->gadget->speed == USB_SPEED_UNKNOWN)
  363. return;
  364. if (autoresume) {
  365. mod_timer(&ss->resume, jiffies + (HZ * autoresume));
  366. DBG(cdev, "suspend, wakeup in %d seconds\n", autoresume);
  367. } else
  368. DBG(cdev, "%s\n", __func__);
  369. }
  370. static void sourcesink_resume(struct usb_function *f)
  371. {
  372. struct f_sourcesink *ss = func_to_ss(f);
  373. struct usb_composite_dev *cdev = f->config->cdev;
  374. DBG(cdev, "%s\n", __func__);
  375. del_timer(&ss->resume);
  376. }
  377. /*-------------------------------------------------------------------------*/
  378. static int __init sourcesink_bind_config(struct usb_configuration *c)
  379. {
  380. struct f_sourcesink *ss;
  381. int status;
  382. ss = kzalloc(sizeof *ss, GFP_KERNEL);
  383. if (!ss)
  384. return -ENOMEM;
  385. ss->function.name = "source/sink";
  386. ss->function.descriptors = fs_source_sink_descs;
  387. ss->function.bind = sourcesink_bind;
  388. ss->function.unbind = sourcesink_unbind;
  389. ss->function.set_alt = sourcesink_set_alt;
  390. ss->function.disable = sourcesink_disable;
  391. ss->function.suspend = sourcesink_suspend;
  392. ss->function.resume = sourcesink_resume;
  393. status = usb_add_function(c, &ss->function);
  394. if (status)
  395. kfree(ss);
  396. return status;
  397. }
  398. static int sourcesink_setup(struct usb_configuration *c,
  399. const struct usb_ctrlrequest *ctrl)
  400. {
  401. struct usb_request *req = c->cdev->req;
  402. int value = -EOPNOTSUPP;
  403. u16 w_index = le16_to_cpu(ctrl->wIndex);
  404. u16 w_value = le16_to_cpu(ctrl->wValue);
  405. u16 w_length = le16_to_cpu(ctrl->wLength);
  406. /* composite driver infrastructure handles everything except
  407. * the two control test requests.
  408. */
  409. switch (ctrl->bRequest) {
  410. /*
  411. * These are the same vendor-specific requests supported by
  412. * Intel's USB 2.0 compliance test devices. We exceed that
  413. * device spec by allowing multiple-packet requests.
  414. *
  415. * NOTE: the Control-OUT data stays in req->buf ... better
  416. * would be copying it into a scratch buffer, so that other
  417. * requests may safely intervene.
  418. */
  419. case 0x5b: /* control WRITE test -- fill the buffer */
  420. if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
  421. goto unknown;
  422. if (w_value || w_index)
  423. break;
  424. /* just read that many bytes into the buffer */
  425. if (w_length > req->length)
  426. break;
  427. value = w_length;
  428. break;
  429. case 0x5c: /* control READ test -- return the buffer */
  430. if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
  431. goto unknown;
  432. if (w_value || w_index)
  433. break;
  434. /* expect those bytes are still in the buffer; send back */
  435. if (w_length > req->length)
  436. break;
  437. value = w_length;
  438. break;
  439. default:
  440. unknown:
  441. VDBG(c->cdev,
  442. "unknown control req%02x.%02x v%04x i%04x l%d\n",
  443. ctrl->bRequestType, ctrl->bRequest,
  444. w_value, w_index, w_length);
  445. }
  446. /* respond with data transfer or status phase? */
  447. if (value >= 0) {
  448. VDBG(c->cdev, "source/sink req%02x.%02x v%04x i%04x l%d\n",
  449. ctrl->bRequestType, ctrl->bRequest,
  450. w_value, w_index, w_length);
  451. req->zero = 0;
  452. req->length = value;
  453. value = usb_ep_queue(c->cdev->gadget->ep0, req, GFP_ATOMIC);
  454. if (value < 0)
  455. ERROR(c->cdev, "source/sinkc response, err %d\n",
  456. value);
  457. }
  458. /* device either stalls (value < 0) or reports success */
  459. return value;
  460. }
  461. static struct usb_configuration sourcesink_driver = {
  462. .label = "source/sink",
  463. .strings = sourcesink_strings,
  464. .bind = sourcesink_bind_config,
  465. .setup = sourcesink_setup,
  466. .bConfigurationValue = 3,
  467. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  468. .bMaxPower = 1, /* 2 mA, minimal */
  469. /* .iConfiguration = DYNAMIC */
  470. };
  471. /**
  472. * sourcesink_add - add a source/sink testing configuration to a device
  473. * @cdev: the device to support the configuration
  474. */
  475. int __init sourcesink_add(struct usb_composite_dev *cdev)
  476. {
  477. int id;
  478. /* allocate string ID(s) */
  479. id = usb_string_id(cdev);
  480. if (id < 0)
  481. return id;
  482. strings_sourcesink[0].id = id;
  483. source_sink_intf.iInterface = id;
  484. sourcesink_driver.iConfiguration = id;
  485. /* support autoresume for remote wakeup testing */
  486. if (autoresume)
  487. sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  488. /* support OTG systems */
  489. if (gadget_is_otg(cdev->gadget)) {
  490. sourcesink_driver.descriptors = otg_desc;
  491. sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  492. }
  493. return usb_add_config(cdev, &sourcesink_driver);
  494. }