f_loopback.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. #include "u_f.h"
  21. /*
  22. * LOOPBACK FUNCTION ... a testing vehicle for USB peripherals,
  23. *
  24. * This takes messages of various sizes written OUT to a device, and loops
  25. * them back so they can be read IN from it. It has been used by certain
  26. * test applications. It supports limited testing of data queueing logic.
  27. *
  28. *
  29. * This is currently packaged as a configuration driver, which can't be
  30. * combined with other functions to make composite devices. However, it
  31. * can be combined with other independent configurations.
  32. */
  33. struct f_loopback {
  34. struct usb_function function;
  35. struct usb_ep *in_ep;
  36. struct usb_ep *out_ep;
  37. };
  38. static inline struct f_loopback *func_to_loop(struct usb_function *f)
  39. {
  40. return container_of(f, struct f_loopback, function);
  41. }
  42. static unsigned qlen;
  43. static unsigned buflen;
  44. /*-------------------------------------------------------------------------*/
  45. static struct usb_interface_descriptor loopback_intf = {
  46. .bLength = sizeof loopback_intf,
  47. .bDescriptorType = USB_DT_INTERFACE,
  48. .bNumEndpoints = 2,
  49. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  50. /* .iInterface = DYNAMIC */
  51. };
  52. /* full speed support: */
  53. static struct usb_endpoint_descriptor fs_loop_source_desc = {
  54. .bLength = USB_DT_ENDPOINT_SIZE,
  55. .bDescriptorType = USB_DT_ENDPOINT,
  56. .bEndpointAddress = USB_DIR_IN,
  57. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  58. };
  59. static struct usb_endpoint_descriptor fs_loop_sink_desc = {
  60. .bLength = USB_DT_ENDPOINT_SIZE,
  61. .bDescriptorType = USB_DT_ENDPOINT,
  62. .bEndpointAddress = USB_DIR_OUT,
  63. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  64. };
  65. static struct usb_descriptor_header *fs_loopback_descs[] = {
  66. (struct usb_descriptor_header *) &loopback_intf,
  67. (struct usb_descriptor_header *) &fs_loop_sink_desc,
  68. (struct usb_descriptor_header *) &fs_loop_source_desc,
  69. NULL,
  70. };
  71. /* high speed support: */
  72. static struct usb_endpoint_descriptor hs_loop_source_desc = {
  73. .bLength = USB_DT_ENDPOINT_SIZE,
  74. .bDescriptorType = USB_DT_ENDPOINT,
  75. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  76. .wMaxPacketSize = cpu_to_le16(512),
  77. };
  78. static struct usb_endpoint_descriptor hs_loop_sink_desc = {
  79. .bLength = USB_DT_ENDPOINT_SIZE,
  80. .bDescriptorType = USB_DT_ENDPOINT,
  81. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  82. .wMaxPacketSize = cpu_to_le16(512),
  83. };
  84. static struct usb_descriptor_header *hs_loopback_descs[] = {
  85. (struct usb_descriptor_header *) &loopback_intf,
  86. (struct usb_descriptor_header *) &hs_loop_source_desc,
  87. (struct usb_descriptor_header *) &hs_loop_sink_desc,
  88. NULL,
  89. };
  90. /* super speed support: */
  91. static struct usb_endpoint_descriptor ss_loop_source_desc = {
  92. .bLength = USB_DT_ENDPOINT_SIZE,
  93. .bDescriptorType = USB_DT_ENDPOINT,
  94. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  95. .wMaxPacketSize = cpu_to_le16(1024),
  96. };
  97. static struct usb_ss_ep_comp_descriptor ss_loop_source_comp_desc = {
  98. .bLength = USB_DT_SS_EP_COMP_SIZE,
  99. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  100. .bMaxBurst = 0,
  101. .bmAttributes = 0,
  102. .wBytesPerInterval = 0,
  103. };
  104. static struct usb_endpoint_descriptor ss_loop_sink_desc = {
  105. .bLength = USB_DT_ENDPOINT_SIZE,
  106. .bDescriptorType = USB_DT_ENDPOINT,
  107. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  108. .wMaxPacketSize = cpu_to_le16(1024),
  109. };
  110. static struct usb_ss_ep_comp_descriptor ss_loop_sink_comp_desc = {
  111. .bLength = USB_DT_SS_EP_COMP_SIZE,
  112. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  113. .bMaxBurst = 0,
  114. .bmAttributes = 0,
  115. .wBytesPerInterval = 0,
  116. };
  117. static struct usb_descriptor_header *ss_loopback_descs[] = {
  118. (struct usb_descriptor_header *) &loopback_intf,
  119. (struct usb_descriptor_header *) &ss_loop_source_desc,
  120. (struct usb_descriptor_header *) &ss_loop_source_comp_desc,
  121. (struct usb_descriptor_header *) &ss_loop_sink_desc,
  122. (struct usb_descriptor_header *) &ss_loop_sink_comp_desc,
  123. NULL,
  124. };
  125. /* function-specific strings: */
  126. static struct usb_string strings_loopback[] = {
  127. [0].s = "loop input to output",
  128. { } /* end of list */
  129. };
  130. static struct usb_gadget_strings stringtab_loop = {
  131. .language = 0x0409, /* en-us */
  132. .strings = strings_loopback,
  133. };
  134. static struct usb_gadget_strings *loopback_strings[] = {
  135. &stringtab_loop,
  136. NULL,
  137. };
  138. /*-------------------------------------------------------------------------*/
  139. static int loopback_bind(struct usb_configuration *c, struct usb_function *f)
  140. {
  141. struct usb_composite_dev *cdev = c->cdev;
  142. struct f_loopback *loop = func_to_loop(f);
  143. int id;
  144. int ret;
  145. /* allocate interface ID(s) */
  146. id = usb_interface_id(c, f);
  147. if (id < 0)
  148. return id;
  149. loopback_intf.bInterfaceNumber = id;
  150. id = usb_string_id(cdev);
  151. if (id < 0)
  152. return id;
  153. strings_loopback[0].id = id;
  154. loopback_intf.iInterface = id;
  155. /* allocate endpoints */
  156. loop->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_loop_source_desc);
  157. if (!loop->in_ep) {
  158. autoconf_fail:
  159. ERROR(cdev, "%s: can't autoconfigure on %s\n",
  160. f->name, cdev->gadget->name);
  161. return -ENODEV;
  162. }
  163. loop->in_ep->driver_data = cdev; /* claim */
  164. loop->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_loop_sink_desc);
  165. if (!loop->out_ep)
  166. goto autoconf_fail;
  167. loop->out_ep->driver_data = cdev; /* claim */
  168. /* support high speed hardware */
  169. hs_loop_source_desc.bEndpointAddress =
  170. fs_loop_source_desc.bEndpointAddress;
  171. hs_loop_sink_desc.bEndpointAddress = fs_loop_sink_desc.bEndpointAddress;
  172. /* support super speed hardware */
  173. ss_loop_source_desc.bEndpointAddress =
  174. fs_loop_source_desc.bEndpointAddress;
  175. ss_loop_sink_desc.bEndpointAddress = fs_loop_sink_desc.bEndpointAddress;
  176. ret = usb_assign_descriptors(f, fs_loopback_descs, hs_loopback_descs,
  177. ss_loopback_descs);
  178. if (ret)
  179. return ret;
  180. DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
  181. (gadget_is_superspeed(c->cdev->gadget) ? "super" :
  182. (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")),
  183. f->name, loop->in_ep->name, loop->out_ep->name);
  184. return 0;
  185. }
  186. static void lb_free_func(struct usb_function *f)
  187. {
  188. struct f_lb_opts *opts;
  189. opts = container_of(f->fi, struct f_lb_opts, func_inst);
  190. mutex_lock(&opts->lock);
  191. opts->refcnt--;
  192. mutex_unlock(&opts->lock);
  193. usb_free_all_descriptors(f);
  194. kfree(func_to_loop(f));
  195. }
  196. static void loopback_complete(struct usb_ep *ep, struct usb_request *req)
  197. {
  198. struct f_loopback *loop = ep->driver_data;
  199. struct usb_composite_dev *cdev = loop->function.config->cdev;
  200. int status = req->status;
  201. switch (status) {
  202. case 0: /* normal completion? */
  203. if (ep == loop->out_ep) {
  204. /* loop this OUT packet back IN to the host */
  205. req->zero = (req->actual < req->length);
  206. req->length = req->actual;
  207. status = usb_ep_queue(loop->in_ep, req, GFP_ATOMIC);
  208. if (status == 0)
  209. return;
  210. /* "should never get here" */
  211. ERROR(cdev, "can't loop %s to %s: %d\n",
  212. ep->name, loop->in_ep->name,
  213. status);
  214. }
  215. /* queue the buffer for some later OUT packet */
  216. req->length = buflen;
  217. status = usb_ep_queue(loop->out_ep, req, GFP_ATOMIC);
  218. if (status == 0)
  219. return;
  220. /* "should never get here" */
  221. /* FALLTHROUGH */
  222. default:
  223. ERROR(cdev, "%s loop complete --> %d, %d/%d\n", ep->name,
  224. status, req->actual, req->length);
  225. /* FALLTHROUGH */
  226. /* NOTE: since this driver doesn't maintain an explicit record
  227. * of requests it submitted (just maintains qlen count), we
  228. * rely on the hardware driver to clean up on disconnect or
  229. * endpoint disable.
  230. */
  231. case -ECONNABORTED: /* hardware forced ep reset */
  232. case -ECONNRESET: /* request dequeued */
  233. case -ESHUTDOWN: /* disconnect from host */
  234. free_ep_req(ep, req);
  235. return;
  236. }
  237. }
  238. static void disable_loopback(struct f_loopback *loop)
  239. {
  240. struct usb_composite_dev *cdev;
  241. cdev = loop->function.config->cdev;
  242. disable_endpoints(cdev, loop->in_ep, loop->out_ep, NULL, NULL);
  243. VDBG(cdev, "%s disabled\n", loop->function.name);
  244. }
  245. static inline struct usb_request *lb_alloc_ep_req(struct usb_ep *ep, int len)
  246. {
  247. return alloc_ep_req(ep, len, buflen);
  248. }
  249. static int
  250. enable_loopback(struct usb_composite_dev *cdev, struct f_loopback *loop)
  251. {
  252. int result = 0;
  253. struct usb_ep *ep;
  254. struct usb_request *req;
  255. unsigned i;
  256. /* one endpoint writes data back IN to the host */
  257. ep = loop->in_ep;
  258. result = config_ep_by_speed(cdev->gadget, &(loop->function), ep);
  259. if (result)
  260. return result;
  261. result = usb_ep_enable(ep);
  262. if (result < 0)
  263. return result;
  264. ep->driver_data = loop;
  265. /* one endpoint just reads OUT packets */
  266. ep = loop->out_ep;
  267. result = config_ep_by_speed(cdev->gadget, &(loop->function), ep);
  268. if (result)
  269. goto fail0;
  270. result = usb_ep_enable(ep);
  271. if (result < 0) {
  272. fail0:
  273. ep = loop->in_ep;
  274. usb_ep_disable(ep);
  275. ep->driver_data = NULL;
  276. return result;
  277. }
  278. ep->driver_data = loop;
  279. /* allocate a bunch of read buffers and queue them all at once.
  280. * we buffer at most 'qlen' transfers; fewer if any need more
  281. * than 'buflen' bytes each.
  282. */
  283. for (i = 0; i < qlen && result == 0; i++) {
  284. req = lb_alloc_ep_req(ep, 0);
  285. if (req) {
  286. req->complete = loopback_complete;
  287. result = usb_ep_queue(ep, req, GFP_ATOMIC);
  288. if (result)
  289. ERROR(cdev, "%s queue req --> %d\n",
  290. ep->name, result);
  291. } else {
  292. usb_ep_disable(ep);
  293. ep->driver_data = NULL;
  294. result = -ENOMEM;
  295. goto fail0;
  296. }
  297. }
  298. DBG(cdev, "%s enabled\n", loop->function.name);
  299. return result;
  300. }
  301. static int loopback_set_alt(struct usb_function *f,
  302. unsigned intf, unsigned alt)
  303. {
  304. struct f_loopback *loop = func_to_loop(f);
  305. struct usb_composite_dev *cdev = f->config->cdev;
  306. /* we know alt is zero */
  307. if (loop->in_ep->driver_data)
  308. disable_loopback(loop);
  309. return enable_loopback(cdev, loop);
  310. }
  311. static void loopback_disable(struct usb_function *f)
  312. {
  313. struct f_loopback *loop = func_to_loop(f);
  314. disable_loopback(loop);
  315. }
  316. static struct usb_function *loopback_alloc(struct usb_function_instance *fi)
  317. {
  318. struct f_loopback *loop;
  319. struct f_lb_opts *lb_opts;
  320. loop = kzalloc(sizeof *loop, GFP_KERNEL);
  321. if (!loop)
  322. return ERR_PTR(-ENOMEM);
  323. lb_opts = container_of(fi, struct f_lb_opts, func_inst);
  324. mutex_lock(&lb_opts->lock);
  325. lb_opts->refcnt++;
  326. mutex_unlock(&lb_opts->lock);
  327. buflen = lb_opts->bulk_buflen;
  328. qlen = lb_opts->qlen;
  329. if (!qlen)
  330. qlen = 32;
  331. loop->function.name = "loopback";
  332. loop->function.bind = loopback_bind;
  333. loop->function.set_alt = loopback_set_alt;
  334. loop->function.disable = loopback_disable;
  335. loop->function.strings = loopback_strings;
  336. loop->function.free_func = lb_free_func;
  337. return &loop->function;
  338. }
  339. static inline struct f_lb_opts *to_f_lb_opts(struct config_item *item)
  340. {
  341. return container_of(to_config_group(item), struct f_lb_opts,
  342. func_inst.group);
  343. }
  344. CONFIGFS_ATTR_STRUCT(f_lb_opts);
  345. CONFIGFS_ATTR_OPS(f_lb_opts);
  346. static void lb_attr_release(struct config_item *item)
  347. {
  348. struct f_lb_opts *lb_opts = to_f_lb_opts(item);
  349. usb_put_function_instance(&lb_opts->func_inst);
  350. }
  351. static struct configfs_item_operations lb_item_ops = {
  352. .release = lb_attr_release,
  353. .show_attribute = f_lb_opts_attr_show,
  354. .store_attribute = f_lb_opts_attr_store,
  355. };
  356. static ssize_t f_lb_opts_qlen_show(struct f_lb_opts *opts, char *page)
  357. {
  358. int result;
  359. mutex_lock(&opts->lock);
  360. result = sprintf(page, "%d", opts->qlen);
  361. mutex_unlock(&opts->lock);
  362. return result;
  363. }
  364. static ssize_t f_lb_opts_qlen_store(struct f_lb_opts *opts,
  365. const char *page, size_t len)
  366. {
  367. int ret;
  368. u32 num;
  369. mutex_lock(&opts->lock);
  370. if (opts->refcnt) {
  371. ret = -EBUSY;
  372. goto end;
  373. }
  374. ret = kstrtou32(page, 0, &num);
  375. if (ret)
  376. goto end;
  377. opts->qlen = num;
  378. ret = len;
  379. end:
  380. mutex_unlock(&opts->lock);
  381. return ret;
  382. }
  383. static struct f_lb_opts_attribute f_lb_opts_qlen =
  384. __CONFIGFS_ATTR(qlen, S_IRUGO | S_IWUSR,
  385. f_lb_opts_qlen_show,
  386. f_lb_opts_qlen_store);
  387. static ssize_t f_lb_opts_bulk_buflen_show(struct f_lb_opts *opts, char *page)
  388. {
  389. int result;
  390. mutex_lock(&opts->lock);
  391. result = sprintf(page, "%d", opts->bulk_buflen);
  392. mutex_unlock(&opts->lock);
  393. return result;
  394. }
  395. static ssize_t f_lb_opts_bulk_buflen_store(struct f_lb_opts *opts,
  396. const char *page, size_t len)
  397. {
  398. int ret;
  399. u32 num;
  400. mutex_lock(&opts->lock);
  401. if (opts->refcnt) {
  402. ret = -EBUSY;
  403. goto end;
  404. }
  405. ret = kstrtou32(page, 0, &num);
  406. if (ret)
  407. goto end;
  408. opts->bulk_buflen = num;
  409. ret = len;
  410. end:
  411. mutex_unlock(&opts->lock);
  412. return ret;
  413. }
  414. static struct f_lb_opts_attribute f_lb_opts_bulk_buflen =
  415. __CONFIGFS_ATTR(buflen, S_IRUGO | S_IWUSR,
  416. f_lb_opts_bulk_buflen_show,
  417. f_lb_opts_bulk_buflen_store);
  418. static struct configfs_attribute *lb_attrs[] = {
  419. &f_lb_opts_qlen.attr,
  420. &f_lb_opts_bulk_buflen.attr,
  421. NULL,
  422. };
  423. static struct config_item_type lb_func_type = {
  424. .ct_item_ops = &lb_item_ops,
  425. .ct_attrs = lb_attrs,
  426. .ct_owner = THIS_MODULE,
  427. };
  428. static void lb_free_instance(struct usb_function_instance *fi)
  429. {
  430. struct f_lb_opts *lb_opts;
  431. lb_opts = container_of(fi, struct f_lb_opts, func_inst);
  432. kfree(lb_opts);
  433. }
  434. static struct usb_function_instance *loopback_alloc_instance(void)
  435. {
  436. struct f_lb_opts *lb_opts;
  437. lb_opts = kzalloc(sizeof(*lb_opts), GFP_KERNEL);
  438. if (!lb_opts)
  439. return ERR_PTR(-ENOMEM);
  440. mutex_init(&lb_opts->lock);
  441. lb_opts->func_inst.free_func_inst = lb_free_instance;
  442. lb_opts->bulk_buflen = GZERO_BULK_BUFLEN;
  443. lb_opts->qlen = GZERO_QLEN;
  444. config_group_init_type_name(&lb_opts->func_inst.group, "",
  445. &lb_func_type);
  446. return &lb_opts->func_inst;
  447. }
  448. DECLARE_USB_FUNCTION(Loopback, loopback_alloc_instance, loopback_alloc);
  449. int __init lb_modinit(void)
  450. {
  451. int ret;
  452. ret = usb_function_register(&Loopbackusb_func);
  453. if (ret)
  454. return ret;
  455. return ret;
  456. }
  457. void __exit lb_modexit(void)
  458. {
  459. usb_function_unregister(&Loopbackusb_func);
  460. }
  461. MODULE_LICENSE("GPL");