f_loopback.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. struct usb_request *alloc_ep_req(struct usb_ep *ep, int len)
  139. {
  140. struct usb_request *req;
  141. req = usb_ep_alloc_request(ep, GFP_ATOMIC);
  142. if (req) {
  143. if (len)
  144. req->length = len;
  145. else
  146. req->length = buflen;
  147. req->buf = kmalloc(req->length, GFP_ATOMIC);
  148. if (!req->buf) {
  149. usb_ep_free_request(ep, req);
  150. req = NULL;
  151. }
  152. }
  153. return req;
  154. }
  155. void free_ep_req(struct usb_ep *ep, struct usb_request *req)
  156. {
  157. kfree(req->buf);
  158. usb_ep_free_request(ep, req);
  159. }
  160. static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
  161. {
  162. int value;
  163. if (ep->driver_data) {
  164. value = usb_ep_disable(ep);
  165. if (value < 0)
  166. DBG(cdev, "disable %s --> %d\n",
  167. ep->name, value);
  168. ep->driver_data = NULL;
  169. }
  170. }
  171. void disable_endpoints(struct usb_composite_dev *cdev,
  172. struct usb_ep *in, struct usb_ep *out,
  173. struct usb_ep *iso_in, struct usb_ep *iso_out)
  174. {
  175. disable_ep(cdev, in);
  176. disable_ep(cdev, out);
  177. if (iso_in)
  178. disable_ep(cdev, iso_in);
  179. if (iso_out)
  180. disable_ep(cdev, iso_out);
  181. }
  182. static int loopback_bind(struct usb_configuration *c, struct usb_function *f)
  183. {
  184. struct usb_composite_dev *cdev = c->cdev;
  185. struct f_loopback *loop = func_to_loop(f);
  186. int id;
  187. int ret;
  188. /* allocate interface ID(s) */
  189. id = usb_interface_id(c, f);
  190. if (id < 0)
  191. return id;
  192. loopback_intf.bInterfaceNumber = id;
  193. id = usb_string_id(cdev);
  194. if (id < 0)
  195. return id;
  196. strings_loopback[0].id = id;
  197. loopback_intf.iInterface = id;
  198. /* allocate endpoints */
  199. loop->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_loop_source_desc);
  200. if (!loop->in_ep) {
  201. autoconf_fail:
  202. ERROR(cdev, "%s: can't autoconfigure on %s\n",
  203. f->name, cdev->gadget->name);
  204. return -ENODEV;
  205. }
  206. loop->in_ep->driver_data = cdev; /* claim */
  207. loop->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_loop_sink_desc);
  208. if (!loop->out_ep)
  209. goto autoconf_fail;
  210. loop->out_ep->driver_data = cdev; /* claim */
  211. /* support high speed hardware */
  212. hs_loop_source_desc.bEndpointAddress =
  213. fs_loop_source_desc.bEndpointAddress;
  214. hs_loop_sink_desc.bEndpointAddress = fs_loop_sink_desc.bEndpointAddress;
  215. /* support super speed hardware */
  216. ss_loop_source_desc.bEndpointAddress =
  217. fs_loop_source_desc.bEndpointAddress;
  218. ss_loop_sink_desc.bEndpointAddress = fs_loop_sink_desc.bEndpointAddress;
  219. ret = usb_assign_descriptors(f, fs_loopback_descs, hs_loopback_descs,
  220. ss_loopback_descs);
  221. if (ret)
  222. return ret;
  223. DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
  224. (gadget_is_superspeed(c->cdev->gadget) ? "super" :
  225. (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")),
  226. f->name, loop->in_ep->name, loop->out_ep->name);
  227. return 0;
  228. }
  229. static void lb_free_func(struct usb_function *f)
  230. {
  231. usb_free_all_descriptors(f);
  232. kfree(func_to_loop(f));
  233. }
  234. static void loopback_complete(struct usb_ep *ep, struct usb_request *req)
  235. {
  236. struct f_loopback *loop = ep->driver_data;
  237. struct usb_composite_dev *cdev = loop->function.config->cdev;
  238. int status = req->status;
  239. switch (status) {
  240. case 0: /* normal completion? */
  241. if (ep == loop->out_ep) {
  242. /* loop this OUT packet back IN to the host */
  243. struct usb_request *in_req = req->context;
  244. in_req->zero = (req->actual % ep->maxpacket == 0)?1:0;
  245. in_req->length = req->actual;
  246. in_req->actual = 0;
  247. memcpy(in_req->buf, req->buf, req->length);
  248. status = usb_ep_queue(loop->in_ep, in_req, GFP_ATOMIC);
  249. if (status == 0)
  250. return;
  251. /* "should never get here" */
  252. ERROR(cdev, "can't loop %s to %s: %d\n",
  253. ep->name, loop->in_ep->name,
  254. status);
  255. }
  256. if (ep == loop->in_ep) {
  257. struct usb_request *out_req = req->context;
  258. /* queue the buffer for some later OUT packet */
  259. out_req->length = buflen;
  260. status = usb_ep_queue(loop->out_ep, out_req, GFP_ATOMIC);
  261. if (status == 0)
  262. return;
  263. }
  264. /* "should never get here" */
  265. /* FALLTHROUGH */
  266. default:
  267. ERROR(cdev, "%s loop complete --> %d, %d/%d\n", ep->name,
  268. status, req->actual, req->length);
  269. /* FALLTHROUGH */
  270. /* NOTE: since this driver doesn't maintain an explicit record
  271. * of requests it submitted (just maintains qlen count), we
  272. * rely on the hardware driver to clean up on disconnect or
  273. * endpoint disable.
  274. */
  275. case -ECONNABORTED: /* hardware forced ep reset */
  276. case -ECONNRESET: /* request dequeued */
  277. case -ESHUTDOWN: /* disconnect from host */
  278. free_ep_req(ep, req);
  279. return;
  280. }
  281. }
  282. static void disable_loopback(struct f_loopback *loop)
  283. {
  284. struct usb_composite_dev *cdev;
  285. cdev = loop->function.config->cdev;
  286. disable_endpoints(cdev, loop->in_ep, loop->out_ep, NULL, NULL);
  287. VDBG(cdev, "%s disabled\n", loop->function.name);
  288. }
  289. static int
  290. enable_loopback(struct usb_composite_dev *cdev, struct f_loopback *loop)
  291. {
  292. int result = 0;
  293. struct usb_ep *ep;
  294. unsigned i;
  295. /* one endpoint writes data back IN to the host */
  296. ep = loop->in_ep;
  297. result = config_ep_by_speed(cdev->gadget, &(loop->function), ep);
  298. if (result)
  299. return result;
  300. result = usb_ep_enable(ep);
  301. if (result < 0)
  302. return result;
  303. ep->driver_data = loop;
  304. /* one endpoint just reads OUT packets */
  305. ep = loop->out_ep;
  306. result = config_ep_by_speed(cdev->gadget, &(loop->function), ep);
  307. if (result)
  308. goto fail0;
  309. result = usb_ep_enable(ep);
  310. if (result < 0) {
  311. fail0:
  312. ep = loop->in_ep;
  313. usb_ep_disable(ep);
  314. ep->driver_data = NULL;
  315. return result;
  316. }
  317. ep->driver_data = loop;
  318. /* allocate a bunch of read buffers and queue them all at once.
  319. * we buffer at most 'qlen' transfers; fewer if any need more
  320. * than 'buflen' bytes each.
  321. */
  322. for (i = 0; i < qlen && result == 0; i++) {
  323. struct usb_request *out_req;
  324. struct usb_request *in_req;
  325. out_req = alloc_ep_req(loop->out_ep, 0);
  326. if (out_req) {
  327. out_req->complete = loopback_complete;
  328. result = usb_ep_queue(loop->out_ep, out_req, GFP_ATOMIC);
  329. if (result)
  330. ERROR(cdev, "%s queue req --> %d\n",
  331. loop->out_ep->name, result);
  332. } else {
  333. usb_ep_disable(loop->out_ep);
  334. loop->out_ep->driver_data = NULL;
  335. result = -ENOMEM;
  336. goto fail0;
  337. }
  338. in_req = alloc_ep_req(loop->in_ep, 0);
  339. if (in_req) {
  340. in_req->complete = loopback_complete;
  341. } else {
  342. usb_ep_disable(loop->in_ep);
  343. loop->in_ep->driver_data = NULL;
  344. result = -ENOMEM;
  345. goto fail0;
  346. }
  347. in_req->context = out_req;
  348. out_req->context = in_req;
  349. }
  350. DBG(cdev, "%s enabled\n", loop->function.name);
  351. return result;
  352. }
  353. static int loopback_set_alt(struct usb_function *f,
  354. unsigned intf, unsigned alt)
  355. {
  356. struct f_loopback *loop = func_to_loop(f);
  357. struct usb_composite_dev *cdev = f->config->cdev;
  358. /* we know alt is zero */
  359. if (loop->in_ep->driver_data)
  360. disable_loopback(loop);
  361. return enable_loopback(cdev, loop);
  362. }
  363. static void loopback_disable(struct usb_function *f)
  364. {
  365. struct f_loopback *loop = func_to_loop(f);
  366. disable_loopback(loop);
  367. }
  368. static struct usb_function *loopback_alloc(struct usb_function_instance *fi)
  369. {
  370. struct f_loopback *loop;
  371. struct f_lb_opts *lb_opts;
  372. loop = kzalloc(sizeof *loop, GFP_KERNEL);
  373. if (!loop)
  374. return ERR_PTR(-ENOMEM);
  375. lb_opts = container_of(fi, struct f_lb_opts, func_inst);
  376. buflen = lb_opts->bulk_buflen;
  377. if (!buflen)
  378. buflen = 512;
  379. qlen = lb_opts->qlen;
  380. if (!qlen)
  381. qlen = 8;
  382. loop->function.name = "loopback";
  383. loop->function.bind = loopback_bind;
  384. loop->function.set_alt = loopback_set_alt;
  385. loop->function.disable = loopback_disable;
  386. loop->function.strings = loopback_strings;
  387. loop->function.free_func = lb_free_func;
  388. return &loop->function;
  389. }
  390. static void lb_free_instance(struct usb_function_instance *fi)
  391. {
  392. struct f_lb_opts *lb_opts;
  393. lb_opts = container_of(fi, struct f_lb_opts, func_inst);
  394. kfree(lb_opts);
  395. }
  396. static struct usb_function_instance *loopback_alloc_instance(void)
  397. {
  398. struct f_lb_opts *lb_opts;
  399. lb_opts = kzalloc(sizeof(*lb_opts), GFP_KERNEL);
  400. if (!lb_opts)
  401. return ERR_PTR(-ENOMEM);
  402. lb_opts->func_inst.free_func_inst = lb_free_instance;
  403. return &lb_opts->func_inst;
  404. }
  405. DECLARE_USB_FUNCTION_INIT(loopback, loopback_alloc_instance, loopback_alloc);
  406. #if 0
  407. int __init lb_modinit(void)
  408. {
  409. int ret;
  410. ret = usb_function_register(&Loopbackusb_func);
  411. if (ret)
  412. return ret;
  413. return ret;
  414. }
  415. void __exit lb_modexit(void)
  416. {
  417. usb_function_unregister(&Loopbackusb_func);
  418. }
  419. #endif
  420. MODULE_LICENSE("GPL");