f_loopback.c 13 KB

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