f_sourcesink.c 16 KB

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