f_loopback.c 11 KB

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