f_loopback.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. /* allocate endpoints */
  150. loop->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_loop_source_desc);
  151. if (!loop->in_ep) {
  152. autoconf_fail:
  153. ERROR(cdev, "%s: can't autoconfigure on %s\n",
  154. f->name, cdev->gadget->name);
  155. return -ENODEV;
  156. }
  157. loop->in_ep->driver_data = cdev; /* claim */
  158. loop->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_loop_sink_desc);
  159. if (!loop->out_ep)
  160. goto autoconf_fail;
  161. loop->out_ep->driver_data = cdev; /* claim */
  162. /* support high speed hardware */
  163. hs_loop_source_desc.bEndpointAddress =
  164. fs_loop_source_desc.bEndpointAddress;
  165. hs_loop_sink_desc.bEndpointAddress = fs_loop_sink_desc.bEndpointAddress;
  166. /* support super speed hardware */
  167. ss_loop_source_desc.bEndpointAddress =
  168. fs_loop_source_desc.bEndpointAddress;
  169. ss_loop_sink_desc.bEndpointAddress = fs_loop_sink_desc.bEndpointAddress;
  170. ret = usb_assign_descriptors(f, fs_loopback_descs, hs_loopback_descs,
  171. ss_loopback_descs);
  172. if (ret)
  173. return ret;
  174. DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
  175. (gadget_is_superspeed(c->cdev->gadget) ? "super" :
  176. (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")),
  177. f->name, loop->in_ep->name, loop->out_ep->name);
  178. return 0;
  179. }
  180. static void
  181. loopback_unbind(struct usb_configuration *c, struct usb_function *f)
  182. {
  183. usb_free_all_descriptors(f);
  184. kfree(func_to_loop(f));
  185. }
  186. static void loopback_complete(struct usb_ep *ep, struct usb_request *req)
  187. {
  188. struct f_loopback *loop = ep->driver_data;
  189. struct usb_composite_dev *cdev = loop->function.config->cdev;
  190. int status = req->status;
  191. switch (status) {
  192. case 0: /* normal completion? */
  193. if (ep == loop->out_ep) {
  194. /* loop this OUT packet back IN to the host */
  195. req->zero = (req->actual < req->length);
  196. req->length = req->actual;
  197. status = usb_ep_queue(loop->in_ep, req, GFP_ATOMIC);
  198. if (status == 0)
  199. return;
  200. /* "should never get here" */
  201. ERROR(cdev, "can't loop %s to %s: %d\n",
  202. ep->name, loop->in_ep->name,
  203. status);
  204. }
  205. /* queue the buffer for some later OUT packet */
  206. req->length = buflen;
  207. status = usb_ep_queue(loop->out_ep, req, GFP_ATOMIC);
  208. if (status == 0)
  209. return;
  210. /* "should never get here" */
  211. /* FALLTHROUGH */
  212. default:
  213. ERROR(cdev, "%s loop complete --> %d, %d/%d\n", ep->name,
  214. status, req->actual, req->length);
  215. /* FALLTHROUGH */
  216. /* NOTE: since this driver doesn't maintain an explicit record
  217. * of requests it submitted (just maintains qlen count), we
  218. * rely on the hardware driver to clean up on disconnect or
  219. * endpoint disable.
  220. */
  221. case -ECONNABORTED: /* hardware forced ep reset */
  222. case -ECONNRESET: /* request dequeued */
  223. case -ESHUTDOWN: /* disconnect from host */
  224. free_ep_req(ep, req);
  225. return;
  226. }
  227. }
  228. static void disable_loopback(struct f_loopback *loop)
  229. {
  230. struct usb_composite_dev *cdev;
  231. cdev = loop->function.config->cdev;
  232. disable_endpoints(cdev, loop->in_ep, loop->out_ep);
  233. VDBG(cdev, "%s disabled\n", loop->function.name);
  234. }
  235. static int
  236. enable_loopback(struct usb_composite_dev *cdev, struct f_loopback *loop)
  237. {
  238. int result = 0;
  239. struct usb_ep *ep;
  240. struct usb_request *req;
  241. unsigned i;
  242. /* one endpoint writes data back IN to the host */
  243. ep = loop->in_ep;
  244. result = config_ep_by_speed(cdev->gadget, &(loop->function), ep);
  245. if (result)
  246. return result;
  247. result = usb_ep_enable(ep);
  248. if (result < 0)
  249. return result;
  250. ep->driver_data = loop;
  251. /* one endpoint just reads OUT packets */
  252. ep = loop->out_ep;
  253. result = config_ep_by_speed(cdev->gadget, &(loop->function), ep);
  254. if (result)
  255. goto fail0;
  256. result = usb_ep_enable(ep);
  257. if (result < 0) {
  258. fail0:
  259. ep = loop->in_ep;
  260. usb_ep_disable(ep);
  261. ep->driver_data = NULL;
  262. return result;
  263. }
  264. ep->driver_data = loop;
  265. /* allocate a bunch of read buffers and queue them all at once.
  266. * we buffer at most 'qlen' transfers; fewer if any need more
  267. * than 'buflen' bytes each.
  268. */
  269. for (i = 0; i < qlen && result == 0; i++) {
  270. req = alloc_ep_req(ep);
  271. if (req) {
  272. req->complete = loopback_complete;
  273. result = usb_ep_queue(ep, req, GFP_ATOMIC);
  274. if (result)
  275. ERROR(cdev, "%s queue req --> %d\n",
  276. ep->name, result);
  277. } else {
  278. usb_ep_disable(ep);
  279. ep->driver_data = NULL;
  280. result = -ENOMEM;
  281. goto fail0;
  282. }
  283. }
  284. DBG(cdev, "%s enabled\n", loop->function.name);
  285. return result;
  286. }
  287. static int loopback_set_alt(struct usb_function *f,
  288. unsigned intf, unsigned alt)
  289. {
  290. struct f_loopback *loop = func_to_loop(f);
  291. struct usb_composite_dev *cdev = f->config->cdev;
  292. /* we know alt is zero */
  293. if (loop->in_ep->driver_data)
  294. disable_loopback(loop);
  295. return enable_loopback(cdev, loop);
  296. }
  297. static void loopback_disable(struct usb_function *f)
  298. {
  299. struct f_loopback *loop = func_to_loop(f);
  300. disable_loopback(loop);
  301. }
  302. /*-------------------------------------------------------------------------*/
  303. static int __init loopback_bind_config(struct usb_configuration *c)
  304. {
  305. struct f_loopback *loop;
  306. int status;
  307. loop = kzalloc(sizeof *loop, GFP_KERNEL);
  308. if (!loop)
  309. return -ENOMEM;
  310. loop->function.name = "loopback";
  311. loop->function.bind = loopback_bind;
  312. loop->function.unbind = loopback_unbind;
  313. loop->function.set_alt = loopback_set_alt;
  314. loop->function.disable = loopback_disable;
  315. status = usb_add_function(c, &loop->function);
  316. if (status)
  317. kfree(loop);
  318. return status;
  319. }
  320. static struct usb_configuration loopback_driver = {
  321. .label = "loopback",
  322. .strings = loopback_strings,
  323. .bConfigurationValue = 2,
  324. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  325. /* .iConfiguration = DYNAMIC */
  326. };
  327. /**
  328. * loopback_add - add a loopback testing configuration to a device
  329. * @cdev: the device to support the loopback configuration
  330. */
  331. int __init loopback_add(struct usb_composite_dev *cdev, bool autoresume)
  332. {
  333. int id;
  334. /* allocate string ID(s) */
  335. id = usb_string_id(cdev);
  336. if (id < 0)
  337. return id;
  338. strings_loopback[0].id = id;
  339. loopback_intf.iInterface = id;
  340. loopback_driver.iConfiguration = id;
  341. /* support autoresume for remote wakeup testing */
  342. if (autoresume)
  343. loopback_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  344. /* support OTG systems */
  345. if (gadget_is_otg(cdev->gadget)) {
  346. loopback_driver.descriptors = otg_desc;
  347. loopback_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  348. }
  349. return usb_add_config(cdev, &loopback_driver, loopback_bind_config);
  350. }