f_loopback.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * f_loopback.c - USB peripheral loopback 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. * LOOPBACK FUNCTION ... a testing vehicle for USB peripherals,
  29. *
  30. * This takes messages of various sizes written OUT to a device, and loops
  31. * them back so they can be read IN from it. It has been used by certain
  32. * test applications. It supports limited testing of data queueing logic.
  33. *
  34. *
  35. * This is currently packaged as a configuration driver, which can't be
  36. * combined with other functions to make composite devices. However, it
  37. * can be combined with other independent configurations.
  38. */
  39. struct f_loopback {
  40. struct usb_function function;
  41. struct usb_ep *in_ep;
  42. struct usb_ep *out_ep;
  43. };
  44. static inline struct f_loopback *func_to_loop(struct usb_function *f)
  45. {
  46. return container_of(f, struct f_loopback, function);
  47. }
  48. static unsigned qlen = 32;
  49. module_param(qlen, uint, 0);
  50. MODULE_PARM_DESC(qlenn, "depth of loopback queue");
  51. /*-------------------------------------------------------------------------*/
  52. static struct usb_interface_descriptor loopback_intf = {
  53. .bLength = sizeof loopback_intf,
  54. .bDescriptorType = USB_DT_INTERFACE,
  55. .bNumEndpoints = 2,
  56. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  57. /* .iInterface = DYNAMIC */
  58. };
  59. /* full speed support: */
  60. static struct usb_endpoint_descriptor fs_loop_source_desc = {
  61. .bLength = USB_DT_ENDPOINT_SIZE,
  62. .bDescriptorType = USB_DT_ENDPOINT,
  63. .bEndpointAddress = USB_DIR_IN,
  64. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  65. };
  66. static struct usb_endpoint_descriptor fs_loop_sink_desc = {
  67. .bLength = USB_DT_ENDPOINT_SIZE,
  68. .bDescriptorType = USB_DT_ENDPOINT,
  69. .bEndpointAddress = USB_DIR_OUT,
  70. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  71. };
  72. static struct usb_descriptor_header *fs_loopback_descs[] = {
  73. (struct usb_descriptor_header *) &loopback_intf,
  74. (struct usb_descriptor_header *) &fs_loop_sink_desc,
  75. (struct usb_descriptor_header *) &fs_loop_source_desc,
  76. NULL,
  77. };
  78. /* high speed support: */
  79. static struct usb_endpoint_descriptor hs_loop_source_desc = {
  80. .bLength = USB_DT_ENDPOINT_SIZE,
  81. .bDescriptorType = USB_DT_ENDPOINT,
  82. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  83. .wMaxPacketSize = cpu_to_le16(512),
  84. };
  85. static struct usb_endpoint_descriptor hs_loop_sink_desc = {
  86. .bLength = USB_DT_ENDPOINT_SIZE,
  87. .bDescriptorType = USB_DT_ENDPOINT,
  88. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  89. .wMaxPacketSize = cpu_to_le16(512),
  90. };
  91. static struct usb_descriptor_header *hs_loopback_descs[] = {
  92. (struct usb_descriptor_header *) &loopback_intf,
  93. (struct usb_descriptor_header *) &hs_loop_source_desc,
  94. (struct usb_descriptor_header *) &hs_loop_sink_desc,
  95. NULL,
  96. };
  97. /* function-specific strings: */
  98. static struct usb_string strings_loopback[] = {
  99. [0].s = "loop input to output",
  100. { } /* end of list */
  101. };
  102. static struct usb_gadget_strings stringtab_loop = {
  103. .language = 0x0409, /* en-us */
  104. .strings = strings_loopback,
  105. };
  106. static struct usb_gadget_strings *loopback_strings[] = {
  107. &stringtab_loop,
  108. NULL,
  109. };
  110. /*-------------------------------------------------------------------------*/
  111. static int __init
  112. loopback_bind(struct usb_configuration *c, struct usb_function *f)
  113. {
  114. struct usb_composite_dev *cdev = c->cdev;
  115. struct f_loopback *loop = func_to_loop(f);
  116. int id;
  117. /* allocate interface ID(s) */
  118. id = usb_interface_id(c, f);
  119. if (id < 0)
  120. return id;
  121. loopback_intf.bInterfaceNumber = id;
  122. /* allocate endpoints */
  123. loop->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_loop_source_desc);
  124. if (!loop->in_ep) {
  125. autoconf_fail:
  126. ERROR(cdev, "%s: can't autoconfigure on %s\n",
  127. f->name, cdev->gadget->name);
  128. return -ENODEV;
  129. }
  130. loop->in_ep->driver_data = cdev; /* claim */
  131. loop->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_loop_sink_desc);
  132. if (!loop->out_ep)
  133. goto autoconf_fail;
  134. loop->out_ep->driver_data = cdev; /* claim */
  135. /* support high speed hardware */
  136. if (gadget_is_dualspeed(c->cdev->gadget)) {
  137. hs_loop_source_desc.bEndpointAddress =
  138. fs_loop_source_desc.bEndpointAddress;
  139. hs_loop_sink_desc.bEndpointAddress =
  140. fs_loop_sink_desc.bEndpointAddress;
  141. f->hs_descriptors = hs_loopback_descs;
  142. }
  143. DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
  144. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  145. f->name, loop->in_ep->name, loop->out_ep->name);
  146. return 0;
  147. }
  148. static void
  149. loopback_unbind(struct usb_configuration *c, struct usb_function *f)
  150. {
  151. kfree(func_to_loop(f));
  152. }
  153. static void loopback_complete(struct usb_ep *ep, struct usb_request *req)
  154. {
  155. struct f_loopback *loop = ep->driver_data;
  156. struct usb_composite_dev *cdev = loop->function.config->cdev;
  157. int status = req->status;
  158. switch (status) {
  159. case 0: /* normal completion? */
  160. if (ep == loop->out_ep) {
  161. /* loop this OUT packet back IN to the host */
  162. req->zero = (req->actual < req->length);
  163. req->length = req->actual;
  164. status = usb_ep_queue(loop->in_ep, req, GFP_ATOMIC);
  165. if (status == 0)
  166. return;
  167. /* "should never get here" */
  168. ERROR(cdev, "can't loop %s to %s: %d\n",
  169. ep->name, loop->in_ep->name,
  170. status);
  171. }
  172. /* queue the buffer for some later OUT packet */
  173. req->length = buflen;
  174. status = usb_ep_queue(loop->out_ep, req, GFP_ATOMIC);
  175. if (status == 0)
  176. return;
  177. /* "should never get here" */
  178. /* FALLTHROUGH */
  179. default:
  180. ERROR(cdev, "%s loop complete --> %d, %d/%d\n", ep->name,
  181. status, req->actual, req->length);
  182. /* FALLTHROUGH */
  183. /* NOTE: since this driver doesn't maintain an explicit record
  184. * of requests it submitted (just maintains qlen count), we
  185. * rely on the hardware driver to clean up on disconnect or
  186. * endpoint disable.
  187. */
  188. case -ECONNABORTED: /* hardware forced ep reset */
  189. case -ECONNRESET: /* request dequeued */
  190. case -ESHUTDOWN: /* disconnect from host */
  191. free_ep_req(ep, req);
  192. return;
  193. }
  194. }
  195. static void disable_loopback(struct f_loopback *loop)
  196. {
  197. struct usb_composite_dev *cdev;
  198. cdev = loop->function.config->cdev;
  199. disable_endpoints(cdev, loop->in_ep, loop->out_ep);
  200. VDBG(cdev, "%s disabled\n", loop->function.name);
  201. }
  202. static int
  203. enable_loopback(struct usb_composite_dev *cdev, struct f_loopback *loop)
  204. {
  205. int result = 0;
  206. const struct usb_endpoint_descriptor *src, *sink;
  207. struct usb_ep *ep;
  208. struct usb_request *req;
  209. unsigned i;
  210. src = ep_choose(cdev->gadget,
  211. &hs_loop_source_desc, &fs_loop_source_desc);
  212. sink = ep_choose(cdev->gadget,
  213. &hs_loop_sink_desc, &fs_loop_sink_desc);
  214. /* one endpoint writes data back IN to the host */
  215. ep = loop->in_ep;
  216. result = usb_ep_enable(ep, src);
  217. if (result < 0)
  218. return result;
  219. ep->driver_data = loop;
  220. /* one endpoint just reads OUT packets */
  221. ep = loop->out_ep;
  222. result = usb_ep_enable(ep, sink);
  223. if (result < 0) {
  224. fail0:
  225. ep = loop->in_ep;
  226. usb_ep_disable(ep);
  227. ep->driver_data = NULL;
  228. return result;
  229. }
  230. ep->driver_data = loop;
  231. /* allocate a bunch of read buffers and queue them all at once.
  232. * we buffer at most 'qlen' transfers; fewer if any need more
  233. * than 'buflen' bytes each.
  234. */
  235. for (i = 0; i < qlen && result == 0; i++) {
  236. req = alloc_ep_req(ep);
  237. if (req) {
  238. req->complete = loopback_complete;
  239. result = usb_ep_queue(ep, req, GFP_ATOMIC);
  240. if (result)
  241. ERROR(cdev, "%s queue req --> %d\n",
  242. ep->name, result);
  243. } else {
  244. usb_ep_disable(ep);
  245. ep->driver_data = NULL;
  246. result = -ENOMEM;
  247. goto fail0;
  248. }
  249. }
  250. DBG(cdev, "%s enabled\n", loop->function.name);
  251. return result;
  252. }
  253. static int loopback_set_alt(struct usb_function *f,
  254. unsigned intf, unsigned alt)
  255. {
  256. struct f_loopback *loop = func_to_loop(f);
  257. struct usb_composite_dev *cdev = f->config->cdev;
  258. /* we know alt is zero */
  259. if (loop->in_ep->driver_data)
  260. disable_loopback(loop);
  261. return enable_loopback(cdev, loop);
  262. }
  263. static void loopback_disable(struct usb_function *f)
  264. {
  265. struct f_loopback *loop = func_to_loop(f);
  266. disable_loopback(loop);
  267. }
  268. /*-------------------------------------------------------------------------*/
  269. static int __init loopback_bind_config(struct usb_configuration *c)
  270. {
  271. struct f_loopback *loop;
  272. int status;
  273. loop = kzalloc(sizeof *loop, GFP_KERNEL);
  274. if (!loop)
  275. return -ENOMEM;
  276. loop->function.name = "loopback";
  277. loop->function.descriptors = fs_loopback_descs;
  278. loop->function.bind = loopback_bind;
  279. loop->function.unbind = loopback_unbind;
  280. loop->function.set_alt = loopback_set_alt;
  281. loop->function.disable = loopback_disable;
  282. status = usb_add_function(c, &loop->function);
  283. if (status)
  284. kfree(loop);
  285. return status;
  286. }
  287. static struct usb_configuration loopback_driver = {
  288. .label = "loopback",
  289. .strings = loopback_strings,
  290. .bConfigurationValue = 2,
  291. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  292. /* .iConfiguration = DYNAMIC */
  293. };
  294. /**
  295. * loopback_add - add a loopback testing configuration to a device
  296. * @cdev: the device to support the loopback configuration
  297. */
  298. int __init loopback_add(struct usb_composite_dev *cdev, bool autoresume)
  299. {
  300. int id;
  301. /* allocate string ID(s) */
  302. id = usb_string_id(cdev);
  303. if (id < 0)
  304. return id;
  305. strings_loopback[0].id = id;
  306. loopback_intf.iInterface = id;
  307. loopback_driver.iConfiguration = id;
  308. /* support autoresume for remote wakeup testing */
  309. if (autoresume)
  310. loopback_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  311. /* support OTG systems */
  312. if (gadget_is_otg(cdev->gadget)) {
  313. loopback_driver.descriptors = otg_desc;
  314. loopback_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  315. }
  316. return usb_add_config(cdev, &loopback_driver, loopback_bind_config);
  317. }