f_loopback.c 14 KB

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