f_sourcesink.c 15 KB

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