f_loopback.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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. /*
  198. * We received some data from the host so let's
  199. * queue it so host can read the from our in ep
  200. */
  201. struct usb_request *in_req = req->context;
  202. in_req->zero = (req->actual < req->length);
  203. in_req->length = req->actual;
  204. ep = loop->in_ep;
  205. req = in_req;
  206. } else {
  207. /*
  208. * We have just looped back a bunch of data
  209. * to host. Now let's wait for some more data.
  210. */
  211. req = req->context;
  212. ep = loop->out_ep;
  213. }
  214. /* queue the buffer back to host or for next bunch of data */
  215. status = usb_ep_queue(ep, req, GFP_ATOMIC);
  216. if (status == 0) {
  217. return;
  218. } else {
  219. ERROR(cdev, "Unable to loop back buffer to %s: %d\n",
  220. ep->name, status);
  221. goto free_req;
  222. }
  223. /* "should never get here" */
  224. default:
  225. ERROR(cdev, "%s loop complete --> %d, %d/%d\n", ep->name,
  226. status, req->actual, req->length);
  227. /* FALLTHROUGH */
  228. /* NOTE: since this driver doesn't maintain an explicit record
  229. * of requests it submitted (just maintains qlen count), we
  230. * rely on the hardware driver to clean up on disconnect or
  231. * endpoint disable.
  232. */
  233. case -ECONNABORTED: /* hardware forced ep reset */
  234. case -ECONNRESET: /* request dequeued */
  235. case -ESHUTDOWN: /* disconnect from host */
  236. free_req:
  237. usb_ep_free_request(ep == loop->in_ep ?
  238. loop->out_ep : loop->in_ep,
  239. req->context);
  240. free_ep_req(ep, req);
  241. return;
  242. }
  243. }
  244. static void disable_loopback(struct f_loopback *loop)
  245. {
  246. struct usb_composite_dev *cdev;
  247. cdev = loop->function.config->cdev;
  248. disable_endpoints(cdev, loop->in_ep, loop->out_ep, NULL, NULL);
  249. VDBG(cdev, "%s disabled\n", loop->function.name);
  250. }
  251. static inline struct usb_request *lb_alloc_ep_req(struct usb_ep *ep, int len)
  252. {
  253. struct f_loopback *loop = ep->driver_data;
  254. return alloc_ep_req(ep, len, loop->buflen);
  255. }
  256. static int alloc_requests(struct usb_composite_dev *cdev,
  257. struct f_loopback *loop)
  258. {
  259. struct usb_request *in_req, *out_req;
  260. int i;
  261. int result = 0;
  262. /*
  263. * allocate a bunch of read buffers and queue them all at once.
  264. * we buffer at most 'qlen' transfers; We allocate buffers only
  265. * for out transfer and reuse them in IN transfers to implement
  266. * our loopback functionality
  267. */
  268. for (i = 0; i < loop->qlen && result == 0; i++) {
  269. result = -ENOMEM;
  270. in_req = usb_ep_alloc_request(loop->in_ep, GFP_ATOMIC);
  271. if (!in_req)
  272. goto fail;
  273. out_req = lb_alloc_ep_req(loop->out_ep, 0);
  274. if (!out_req)
  275. goto fail_in;
  276. in_req->complete = loopback_complete;
  277. out_req->complete = loopback_complete;
  278. in_req->buf = out_req->buf;
  279. /* length will be set in complete routine */
  280. in_req->context = out_req;
  281. out_req->context = in_req;
  282. result = usb_ep_queue(loop->out_ep, out_req, GFP_ATOMIC);
  283. if (result) {
  284. ERROR(cdev, "%s queue req --> %d\n",
  285. loop->out_ep->name, result);
  286. goto fail_out;
  287. }
  288. }
  289. return 0;
  290. fail_out:
  291. free_ep_req(loop->out_ep, out_req);
  292. fail_in:
  293. usb_ep_free_request(loop->in_ep, in_req);
  294. fail:
  295. return result;
  296. }
  297. static int enable_endpoint(struct usb_composite_dev *cdev,
  298. struct f_loopback *loop, struct usb_ep *ep)
  299. {
  300. int result;
  301. result = config_ep_by_speed(cdev->gadget, &(loop->function), ep);
  302. if (result)
  303. goto out;
  304. result = usb_ep_enable(ep);
  305. if (result < 0)
  306. goto out;
  307. ep->driver_data = loop;
  308. result = 0;
  309. out:
  310. return result;
  311. }
  312. static int
  313. enable_loopback(struct usb_composite_dev *cdev, struct f_loopback *loop)
  314. {
  315. int result = 0;
  316. result = enable_endpoint(cdev, loop, loop->in_ep);
  317. if (result)
  318. goto out;
  319. result = enable_endpoint(cdev, loop, loop->out_ep);
  320. if (result)
  321. goto disable_in;
  322. result = alloc_requests(cdev, loop);
  323. if (result)
  324. goto disable_out;
  325. DBG(cdev, "%s enabled\n", loop->function.name);
  326. return 0;
  327. disable_out:
  328. usb_ep_disable(loop->out_ep);
  329. disable_in:
  330. usb_ep_disable(loop->in_ep);
  331. out:
  332. return result;
  333. }
  334. static int loopback_set_alt(struct usb_function *f,
  335. unsigned intf, unsigned alt)
  336. {
  337. struct f_loopback *loop = func_to_loop(f);
  338. struct usb_composite_dev *cdev = f->config->cdev;
  339. /* we know alt is zero */
  340. disable_loopback(loop);
  341. return enable_loopback(cdev, loop);
  342. }
  343. static void loopback_disable(struct usb_function *f)
  344. {
  345. struct f_loopback *loop = func_to_loop(f);
  346. disable_loopback(loop);
  347. }
  348. static struct usb_function *loopback_alloc(struct usb_function_instance *fi)
  349. {
  350. struct f_loopback *loop;
  351. struct f_lb_opts *lb_opts;
  352. loop = kzalloc(sizeof *loop, GFP_KERNEL);
  353. if (!loop)
  354. return ERR_PTR(-ENOMEM);
  355. lb_opts = container_of(fi, struct f_lb_opts, func_inst);
  356. mutex_lock(&lb_opts->lock);
  357. lb_opts->refcnt++;
  358. mutex_unlock(&lb_opts->lock);
  359. loop->buflen = lb_opts->bulk_buflen;
  360. loop->qlen = lb_opts->qlen;
  361. if (!loop->qlen)
  362. loop->qlen = 32;
  363. loop->function.name = "loopback";
  364. loop->function.bind = loopback_bind;
  365. loop->function.set_alt = loopback_set_alt;
  366. loop->function.disable = loopback_disable;
  367. loop->function.strings = loopback_strings;
  368. loop->function.free_func = lb_free_func;
  369. return &loop->function;
  370. }
  371. static inline struct f_lb_opts *to_f_lb_opts(struct config_item *item)
  372. {
  373. return container_of(to_config_group(item), struct f_lb_opts,
  374. func_inst.group);
  375. }
  376. static void lb_attr_release(struct config_item *item)
  377. {
  378. struct f_lb_opts *lb_opts = to_f_lb_opts(item);
  379. usb_put_function_instance(&lb_opts->func_inst);
  380. }
  381. static struct configfs_item_operations lb_item_ops = {
  382. .release = lb_attr_release,
  383. };
  384. static ssize_t f_lb_opts_qlen_show(struct config_item *item, char *page)
  385. {
  386. struct f_lb_opts *opts = to_f_lb_opts(item);
  387. int result;
  388. mutex_lock(&opts->lock);
  389. result = sprintf(page, "%d\n", opts->qlen);
  390. mutex_unlock(&opts->lock);
  391. return result;
  392. }
  393. static ssize_t f_lb_opts_qlen_store(struct config_item *item,
  394. const char *page, size_t len)
  395. {
  396. struct f_lb_opts *opts = to_f_lb_opts(item);
  397. int ret;
  398. u32 num;
  399. mutex_lock(&opts->lock);
  400. if (opts->refcnt) {
  401. ret = -EBUSY;
  402. goto end;
  403. }
  404. ret = kstrtou32(page, 0, &num);
  405. if (ret)
  406. goto end;
  407. opts->qlen = num;
  408. ret = len;
  409. end:
  410. mutex_unlock(&opts->lock);
  411. return ret;
  412. }
  413. CONFIGFS_ATTR(f_lb_opts_, qlen);
  414. static ssize_t f_lb_opts_bulk_buflen_show(struct config_item *item, char *page)
  415. {
  416. struct f_lb_opts *opts = to_f_lb_opts(item);
  417. int result;
  418. mutex_lock(&opts->lock);
  419. result = sprintf(page, "%d\n", opts->bulk_buflen);
  420. mutex_unlock(&opts->lock);
  421. return result;
  422. }
  423. static ssize_t f_lb_opts_bulk_buflen_store(struct config_item *item,
  424. const char *page, size_t len)
  425. {
  426. struct f_lb_opts *opts = to_f_lb_opts(item);
  427. int ret;
  428. u32 num;
  429. mutex_lock(&opts->lock);
  430. if (opts->refcnt) {
  431. ret = -EBUSY;
  432. goto end;
  433. }
  434. ret = kstrtou32(page, 0, &num);
  435. if (ret)
  436. goto end;
  437. opts->bulk_buflen = num;
  438. ret = len;
  439. end:
  440. mutex_unlock(&opts->lock);
  441. return ret;
  442. }
  443. CONFIGFS_ATTR(f_lb_opts_, bulk_buflen);
  444. static struct configfs_attribute *lb_attrs[] = {
  445. &f_lb_opts_attr_qlen,
  446. &f_lb_opts_attr_bulk_buflen,
  447. NULL,
  448. };
  449. static struct config_item_type lb_func_type = {
  450. .ct_item_ops = &lb_item_ops,
  451. .ct_attrs = lb_attrs,
  452. .ct_owner = THIS_MODULE,
  453. };
  454. static void lb_free_instance(struct usb_function_instance *fi)
  455. {
  456. struct f_lb_opts *lb_opts;
  457. lb_opts = container_of(fi, struct f_lb_opts, func_inst);
  458. kfree(lb_opts);
  459. }
  460. static struct usb_function_instance *loopback_alloc_instance(void)
  461. {
  462. struct f_lb_opts *lb_opts;
  463. lb_opts = kzalloc(sizeof(*lb_opts), GFP_KERNEL);
  464. if (!lb_opts)
  465. return ERR_PTR(-ENOMEM);
  466. mutex_init(&lb_opts->lock);
  467. lb_opts->func_inst.free_func_inst = lb_free_instance;
  468. lb_opts->bulk_buflen = GZERO_BULK_BUFLEN;
  469. lb_opts->qlen = GZERO_QLEN;
  470. config_group_init_type_name(&lb_opts->func_inst.group, "",
  471. &lb_func_type);
  472. return &lb_opts->func_inst;
  473. }
  474. DECLARE_USB_FUNCTION(Loopback, loopback_alloc_instance, loopback_alloc);
  475. int __init lb_modinit(void)
  476. {
  477. int ret;
  478. ret = usb_function_register(&Loopbackusb_func);
  479. if (ret)
  480. return ret;
  481. return ret;
  482. }
  483. void __exit lb_modexit(void)
  484. {
  485. usb_function_unregister(&Loopbackusb_func);
  486. }
  487. MODULE_LICENSE("GPL");