f_loopback.c 14 KB

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