f_loopback.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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. req->zero = (req->actual < req->length);
  205. req->length = req->actual;
  206. }
  207. /* queue the buffer for some later OUT packet */
  208. req->length = buflen;
  209. status = usb_ep_queue(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, NULL, NULL);
  235. VDBG(cdev, "%s disabled\n", loop->function.name);
  236. }
  237. static inline struct usb_request *lb_alloc_ep_req(struct usb_ep *ep, int len)
  238. {
  239. return alloc_ep_req(ep, len, buflen);
  240. }
  241. static int enable_endpoint(struct usb_composite_dev *cdev, struct f_loopback *loop,
  242. struct usb_ep *ep)
  243. {
  244. struct usb_request *req;
  245. unsigned i;
  246. int result;
  247. /*
  248. * one endpoint writes data back IN to the host while another endpoint
  249. * just reads OUT packets
  250. */
  251. result = config_ep_by_speed(cdev->gadget, &(loop->function), ep);
  252. if (result)
  253. goto fail0;
  254. result = usb_ep_enable(ep);
  255. if (result < 0)
  256. goto fail0;
  257. ep->driver_data = loop;
  258. /*
  259. * allocate a bunch of read buffers and queue them all at once.
  260. * we buffer at most 'qlen' transfers; fewer if any need more
  261. * than 'buflen' bytes each.
  262. */
  263. for (i = 0; i < qlen && result == 0; i++) {
  264. req = lb_alloc_ep_req(ep, 0);
  265. if (!req)
  266. goto fail1;
  267. req->complete = loopback_complete;
  268. result = usb_ep_queue(ep, req, GFP_ATOMIC);
  269. if (result) {
  270. ERROR(cdev, "%s queue req --> %d\n",
  271. ep->name, result);
  272. goto fail1;
  273. }
  274. }
  275. return 0;
  276. fail1:
  277. usb_ep_disable(ep);
  278. fail0:
  279. return result;
  280. }
  281. static int
  282. enable_loopback(struct usb_composite_dev *cdev, struct f_loopback *loop)
  283. {
  284. int result = 0;
  285. result = enable_endpoint(cdev, loop, loop->in_ep);
  286. if (result)
  287. return result;
  288. result = enable_endpoint(cdev, loop, loop->out_ep);
  289. if (result)
  290. return result;
  291. DBG(cdev, "%s enabled\n", loop->function.name);
  292. return result;
  293. }
  294. static int loopback_set_alt(struct usb_function *f,
  295. unsigned intf, unsigned alt)
  296. {
  297. struct f_loopback *loop = func_to_loop(f);
  298. struct usb_composite_dev *cdev = f->config->cdev;
  299. /* we know alt is zero */
  300. if (loop->in_ep->driver_data)
  301. disable_loopback(loop);
  302. return enable_loopback(cdev, loop);
  303. }
  304. static void loopback_disable(struct usb_function *f)
  305. {
  306. struct f_loopback *loop = func_to_loop(f);
  307. disable_loopback(loop);
  308. }
  309. static struct usb_function *loopback_alloc(struct usb_function_instance *fi)
  310. {
  311. struct f_loopback *loop;
  312. struct f_lb_opts *lb_opts;
  313. loop = kzalloc(sizeof *loop, GFP_KERNEL);
  314. if (!loop)
  315. return ERR_PTR(-ENOMEM);
  316. lb_opts = container_of(fi, struct f_lb_opts, func_inst);
  317. mutex_lock(&lb_opts->lock);
  318. lb_opts->refcnt++;
  319. mutex_unlock(&lb_opts->lock);
  320. buflen = lb_opts->bulk_buflen;
  321. qlen = lb_opts->qlen;
  322. if (!qlen)
  323. qlen = 32;
  324. loop->function.name = "loopback";
  325. loop->function.bind = loopback_bind;
  326. loop->function.set_alt = loopback_set_alt;
  327. loop->function.disable = loopback_disable;
  328. loop->function.strings = loopback_strings;
  329. loop->function.free_func = lb_free_func;
  330. return &loop->function;
  331. }
  332. static inline struct f_lb_opts *to_f_lb_opts(struct config_item *item)
  333. {
  334. return container_of(to_config_group(item), struct f_lb_opts,
  335. func_inst.group);
  336. }
  337. CONFIGFS_ATTR_STRUCT(f_lb_opts);
  338. CONFIGFS_ATTR_OPS(f_lb_opts);
  339. static void lb_attr_release(struct config_item *item)
  340. {
  341. struct f_lb_opts *lb_opts = to_f_lb_opts(item);
  342. usb_put_function_instance(&lb_opts->func_inst);
  343. }
  344. static struct configfs_item_operations lb_item_ops = {
  345. .release = lb_attr_release,
  346. .show_attribute = f_lb_opts_attr_show,
  347. .store_attribute = f_lb_opts_attr_store,
  348. };
  349. static ssize_t f_lb_opts_qlen_show(struct f_lb_opts *opts, char *page)
  350. {
  351. int result;
  352. mutex_lock(&opts->lock);
  353. result = sprintf(page, "%d", opts->qlen);
  354. mutex_unlock(&opts->lock);
  355. return result;
  356. }
  357. static ssize_t f_lb_opts_qlen_store(struct f_lb_opts *opts,
  358. const char *page, size_t len)
  359. {
  360. int ret;
  361. u32 num;
  362. mutex_lock(&opts->lock);
  363. if (opts->refcnt) {
  364. ret = -EBUSY;
  365. goto end;
  366. }
  367. ret = kstrtou32(page, 0, &num);
  368. if (ret)
  369. goto end;
  370. opts->qlen = num;
  371. ret = len;
  372. end:
  373. mutex_unlock(&opts->lock);
  374. return ret;
  375. }
  376. static struct f_lb_opts_attribute f_lb_opts_qlen =
  377. __CONFIGFS_ATTR(qlen, S_IRUGO | S_IWUSR,
  378. f_lb_opts_qlen_show,
  379. f_lb_opts_qlen_store);
  380. static ssize_t f_lb_opts_bulk_buflen_show(struct f_lb_opts *opts, char *page)
  381. {
  382. int result;
  383. mutex_lock(&opts->lock);
  384. result = sprintf(page, "%d", opts->bulk_buflen);
  385. mutex_unlock(&opts->lock);
  386. return result;
  387. }
  388. static ssize_t f_lb_opts_bulk_buflen_store(struct f_lb_opts *opts,
  389. const char *page, size_t len)
  390. {
  391. int ret;
  392. u32 num;
  393. mutex_lock(&opts->lock);
  394. if (opts->refcnt) {
  395. ret = -EBUSY;
  396. goto end;
  397. }
  398. ret = kstrtou32(page, 0, &num);
  399. if (ret)
  400. goto end;
  401. opts->bulk_buflen = num;
  402. ret = len;
  403. end:
  404. mutex_unlock(&opts->lock);
  405. return ret;
  406. }
  407. static struct f_lb_opts_attribute f_lb_opts_bulk_buflen =
  408. __CONFIGFS_ATTR(buflen, S_IRUGO | S_IWUSR,
  409. f_lb_opts_bulk_buflen_show,
  410. f_lb_opts_bulk_buflen_store);
  411. static struct configfs_attribute *lb_attrs[] = {
  412. &f_lb_opts_qlen.attr,
  413. &f_lb_opts_bulk_buflen.attr,
  414. NULL,
  415. };
  416. static struct config_item_type lb_func_type = {
  417. .ct_item_ops = &lb_item_ops,
  418. .ct_attrs = lb_attrs,
  419. .ct_owner = THIS_MODULE,
  420. };
  421. static void lb_free_instance(struct usb_function_instance *fi)
  422. {
  423. struct f_lb_opts *lb_opts;
  424. lb_opts = container_of(fi, struct f_lb_opts, func_inst);
  425. kfree(lb_opts);
  426. }
  427. static struct usb_function_instance *loopback_alloc_instance(void)
  428. {
  429. struct f_lb_opts *lb_opts;
  430. lb_opts = kzalloc(sizeof(*lb_opts), GFP_KERNEL);
  431. if (!lb_opts)
  432. return ERR_PTR(-ENOMEM);
  433. mutex_init(&lb_opts->lock);
  434. lb_opts->func_inst.free_func_inst = lb_free_instance;
  435. lb_opts->bulk_buflen = GZERO_BULK_BUFLEN;
  436. lb_opts->qlen = GZERO_QLEN;
  437. config_group_init_type_name(&lb_opts->func_inst.group, "",
  438. &lb_func_type);
  439. return &lb_opts->func_inst;
  440. }
  441. DECLARE_USB_FUNCTION(Loopback, loopback_alloc_instance, loopback_alloc);
  442. int __init lb_modinit(void)
  443. {
  444. int ret;
  445. ret = usb_function_register(&Loopbackusb_func);
  446. if (ret)
  447. return ret;
  448. return ret;
  449. }
  450. void __exit lb_modexit(void)
  451. {
  452. usb_function_unregister(&Loopbackusb_func);
  453. }
  454. MODULE_LICENSE("GPL");