f_loopback.c 10 KB

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