f_loopback.c 13 KB

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