f_loopback.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 <linux/module.h>
  17. #include <linux/err.h>
  18. #include <linux/usb/composite.h>
  19. #include "g_zero.h"
  20. /*
  21. * LOOPBACK FUNCTION ... a testing vehicle for USB peripherals,
  22. *
  23. * This takes messages of various sizes written OUT to a device, and loops
  24. * them back so they can be read IN from it. It has been used by certain
  25. * test applications. It supports limited testing of data queueing logic.
  26. *
  27. *
  28. * This is currently packaged as a configuration driver, which can't be
  29. * combined with other functions to make composite devices. However, it
  30. * can be combined with other independent configurations.
  31. */
  32. struct f_loopback {
  33. struct usb_function function;
  34. struct usb_ep *in_ep;
  35. struct usb_ep *out_ep;
  36. };
  37. static inline struct f_loopback *func_to_loop(struct usb_function *f)
  38. {
  39. return container_of(f, struct f_loopback, function);
  40. }
  41. static unsigned qlen;
  42. static unsigned buflen;
  43. /*-------------------------------------------------------------------------*/
  44. static struct usb_interface_descriptor loopback_intf = {
  45. .bLength = sizeof loopback_intf,
  46. .bDescriptorType = USB_DT_INTERFACE,
  47. .bNumEndpoints = 2,
  48. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  49. /* .iInterface = DYNAMIC */
  50. };
  51. /* full speed support: */
  52. static struct usb_endpoint_descriptor fs_loop_source_desc = {
  53. .bLength = USB_DT_ENDPOINT_SIZE,
  54. .bDescriptorType = USB_DT_ENDPOINT,
  55. .bEndpointAddress = USB_DIR_IN,
  56. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  57. };
  58. static struct usb_endpoint_descriptor fs_loop_sink_desc = {
  59. .bLength = USB_DT_ENDPOINT_SIZE,
  60. .bDescriptorType = USB_DT_ENDPOINT,
  61. .bEndpointAddress = USB_DIR_OUT,
  62. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  63. };
  64. static struct usb_descriptor_header *fs_loopback_descs[] = {
  65. (struct usb_descriptor_header *) &loopback_intf,
  66. (struct usb_descriptor_header *) &fs_loop_sink_desc,
  67. (struct usb_descriptor_header *) &fs_loop_source_desc,
  68. NULL,
  69. };
  70. /* high speed support: */
  71. static struct usb_endpoint_descriptor hs_loop_source_desc = {
  72. .bLength = USB_DT_ENDPOINT_SIZE,
  73. .bDescriptorType = USB_DT_ENDPOINT,
  74. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  75. .wMaxPacketSize = cpu_to_le16(512),
  76. };
  77. static struct usb_endpoint_descriptor hs_loop_sink_desc = {
  78. .bLength = USB_DT_ENDPOINT_SIZE,
  79. .bDescriptorType = USB_DT_ENDPOINT,
  80. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  81. .wMaxPacketSize = cpu_to_le16(512),
  82. };
  83. static struct usb_descriptor_header *hs_loopback_descs[] = {
  84. (struct usb_descriptor_header *) &loopback_intf,
  85. (struct usb_descriptor_header *) &hs_loop_source_desc,
  86. (struct usb_descriptor_header *) &hs_loop_sink_desc,
  87. NULL,
  88. };
  89. /* super speed support: */
  90. static struct usb_endpoint_descriptor ss_loop_source_desc = {
  91. .bLength = USB_DT_ENDPOINT_SIZE,
  92. .bDescriptorType = USB_DT_ENDPOINT,
  93. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  94. .wMaxPacketSize = cpu_to_le16(1024),
  95. };
  96. struct usb_ss_ep_comp_descriptor ss_loop_source_comp_desc = {
  97. .bLength = USB_DT_SS_EP_COMP_SIZE,
  98. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  99. .bMaxBurst = 0,
  100. .bmAttributes = 0,
  101. .wBytesPerInterval = 0,
  102. };
  103. static struct usb_endpoint_descriptor ss_loop_sink_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_loop_sink_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_descriptor_header *ss_loopback_descs[] = {
  117. (struct usb_descriptor_header *) &loopback_intf,
  118. (struct usb_descriptor_header *) &ss_loop_source_desc,
  119. (struct usb_descriptor_header *) &ss_loop_source_comp_desc,
  120. (struct usb_descriptor_header *) &ss_loop_sink_desc,
  121. (struct usb_descriptor_header *) &ss_loop_sink_comp_desc,
  122. NULL,
  123. };
  124. /* function-specific strings: */
  125. static struct usb_string strings_loopback[] = {
  126. [0].s = "loop input to output",
  127. { } /* end of list */
  128. };
  129. static struct usb_gadget_strings stringtab_loop = {
  130. .language = 0x0409, /* en-us */
  131. .strings = strings_loopback,
  132. };
  133. static struct usb_gadget_strings *loopback_strings[] = {
  134. &stringtab_loop,
  135. NULL,
  136. };
  137. /*-------------------------------------------------------------------------*/
  138. static int 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 lb_free_func(struct usb_function *f)
  186. {
  187. usb_free_all_descriptors(f);
  188. kfree(func_to_loop(f));
  189. }
  190. static void loopback_complete(struct usb_ep *ep, struct usb_request *req)
  191. {
  192. struct f_loopback *loop = ep->driver_data;
  193. struct usb_composite_dev *cdev = loop->function.config->cdev;
  194. int status = req->status;
  195. switch (status) {
  196. case 0: /* normal completion? */
  197. if (ep == loop->out_ep) {
  198. /* loop this OUT packet back IN to the host */
  199. req->zero = (req->actual < req->length);
  200. req->length = req->actual;
  201. status = usb_ep_queue(loop->in_ep, req, GFP_ATOMIC);
  202. if (status == 0)
  203. return;
  204. /* "should never get here" */
  205. ERROR(cdev, "can't loop %s to %s: %d\n",
  206. ep->name, loop->in_ep->name,
  207. status);
  208. }
  209. /* queue the buffer for some later OUT packet */
  210. req->length = buflen;
  211. status = usb_ep_queue(loop->out_ep, req, GFP_ATOMIC);
  212. if (status == 0)
  213. return;
  214. /* "should never get here" */
  215. /* FALLTHROUGH */
  216. default:
  217. ERROR(cdev, "%s loop complete --> %d, %d/%d\n", ep->name,
  218. status, req->actual, req->length);
  219. /* FALLTHROUGH */
  220. /* NOTE: since this driver doesn't maintain an explicit record
  221. * of requests it submitted (just maintains qlen count), we
  222. * rely on the hardware driver to clean up on disconnect or
  223. * endpoint disable.
  224. */
  225. case -ECONNABORTED: /* hardware forced ep reset */
  226. case -ECONNRESET: /* request dequeued */
  227. case -ESHUTDOWN: /* disconnect from host */
  228. free_ep_req(ep, req);
  229. return;
  230. }
  231. }
  232. static void disable_loopback(struct f_loopback *loop)
  233. {
  234. struct usb_composite_dev *cdev;
  235. cdev = loop->function.config->cdev;
  236. disable_endpoints(cdev, loop->in_ep, loop->out_ep, NULL, NULL);
  237. VDBG(cdev, "%s disabled\n", loop->function.name);
  238. }
  239. static int
  240. enable_loopback(struct usb_composite_dev *cdev, struct f_loopback *loop)
  241. {
  242. int result = 0;
  243. struct usb_ep *ep;
  244. struct usb_request *req;
  245. unsigned i;
  246. /* one endpoint writes data back IN to the host */
  247. ep = loop->in_ep;
  248. result = config_ep_by_speed(cdev->gadget, &(loop->function), ep);
  249. if (result)
  250. return result;
  251. result = usb_ep_enable(ep);
  252. if (result < 0)
  253. return result;
  254. ep->driver_data = loop;
  255. /* one endpoint just reads OUT packets */
  256. ep = loop->out_ep;
  257. result = config_ep_by_speed(cdev->gadget, &(loop->function), ep);
  258. if (result)
  259. goto fail0;
  260. result = usb_ep_enable(ep);
  261. if (result < 0) {
  262. fail0:
  263. ep = loop->in_ep;
  264. usb_ep_disable(ep);
  265. ep->driver_data = NULL;
  266. return result;
  267. }
  268. ep->driver_data = loop;
  269. /* allocate a bunch of read buffers and queue them all at once.
  270. * we buffer at most 'qlen' transfers; fewer if any need more
  271. * than 'buflen' bytes each.
  272. */
  273. for (i = 0; i < qlen && result == 0; i++) {
  274. req = alloc_ep_req(ep, 0);
  275. if (req) {
  276. req->complete = loopback_complete;
  277. result = usb_ep_queue(ep, req, GFP_ATOMIC);
  278. if (result)
  279. ERROR(cdev, "%s queue req --> %d\n",
  280. ep->name, result);
  281. } else {
  282. usb_ep_disable(ep);
  283. ep->driver_data = NULL;
  284. result = -ENOMEM;
  285. goto fail0;
  286. }
  287. }
  288. DBG(cdev, "%s enabled\n", loop->function.name);
  289. return result;
  290. }
  291. static int loopback_set_alt(struct usb_function *f,
  292. unsigned intf, unsigned alt)
  293. {
  294. struct f_loopback *loop = func_to_loop(f);
  295. struct usb_composite_dev *cdev = f->config->cdev;
  296. /* we know alt is zero */
  297. if (loop->in_ep->driver_data)
  298. disable_loopback(loop);
  299. return enable_loopback(cdev, loop);
  300. }
  301. static void loopback_disable(struct usb_function *f)
  302. {
  303. struct f_loopback *loop = func_to_loop(f);
  304. disable_loopback(loop);
  305. }
  306. static struct usb_function *loopback_alloc(struct usb_function_instance *fi)
  307. {
  308. struct f_loopback *loop;
  309. struct f_lb_opts *lb_opts;
  310. loop = kzalloc(sizeof *loop, GFP_KERNEL);
  311. if (!loop)
  312. return ERR_PTR(-ENOMEM);
  313. lb_opts = container_of(fi, struct f_lb_opts, func_inst);
  314. buflen = lb_opts->bulk_buflen;
  315. qlen = lb_opts->qlen;
  316. if (!qlen)
  317. qlen = 32;
  318. loop->function.name = "loopback";
  319. loop->function.bind = loopback_bind;
  320. loop->function.set_alt = loopback_set_alt;
  321. loop->function.disable = loopback_disable;
  322. loop->function.strings = loopback_strings;
  323. loop->function.free_func = lb_free_func;
  324. return &loop->function;
  325. }
  326. static void lb_free_instance(struct usb_function_instance *fi)
  327. {
  328. struct f_lb_opts *lb_opts;
  329. lb_opts = container_of(fi, struct f_lb_opts, func_inst);
  330. kfree(lb_opts);
  331. }
  332. static struct usb_function_instance *loopback_alloc_instance(void)
  333. {
  334. struct f_lb_opts *lb_opts;
  335. lb_opts = kzalloc(sizeof(*lb_opts), GFP_KERNEL);
  336. if (!lb_opts)
  337. return ERR_PTR(-ENOMEM);
  338. lb_opts->func_inst.free_func_inst = lb_free_instance;
  339. return &lb_opts->func_inst;
  340. }
  341. DECLARE_USB_FUNCTION(Loopback, loopback_alloc_instance, loopback_alloc);
  342. int __init lb_modinit(void)
  343. {
  344. int ret;
  345. ret = usb_function_register(&Loopbackusb_func);
  346. if (ret)
  347. return ret;
  348. return ret;
  349. }
  350. void __exit lb_modexit(void)
  351. {
  352. usb_function_unregister(&Loopbackusb_func);
  353. }
  354. MODULE_LICENSE("GPL");