f_loopback.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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, NULL);
  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. return alloc_ep_req(ep, len);
  254. }
  255. static int alloc_requests(struct usb_composite_dev *cdev,
  256. struct f_loopback *loop)
  257. {
  258. struct usb_request *in_req, *out_req;
  259. int i;
  260. int result = 0;
  261. /*
  262. * allocate a bunch of read buffers and queue them all at once.
  263. * we buffer at most 'qlen' transfers; We allocate buffers only
  264. * for out transfer and reuse them in IN transfers to implement
  265. * our loopback functionality
  266. */
  267. for (i = 0; i < loop->qlen && result == 0; i++) {
  268. result = -ENOMEM;
  269. in_req = usb_ep_alloc_request(loop->in_ep, GFP_ATOMIC);
  270. if (!in_req)
  271. goto fail;
  272. out_req = lb_alloc_ep_req(loop->out_ep, loop->buflen);
  273. if (!out_req)
  274. goto fail_in;
  275. in_req->complete = loopback_complete;
  276. out_req->complete = loopback_complete;
  277. in_req->buf = out_req->buf;
  278. /* length will be set in complete routine */
  279. in_req->context = out_req;
  280. out_req->context = in_req;
  281. result = usb_ep_queue(loop->out_ep, out_req, GFP_ATOMIC);
  282. if (result) {
  283. ERROR(cdev, "%s queue req --> %d\n",
  284. loop->out_ep->name, result);
  285. goto fail_out;
  286. }
  287. }
  288. return 0;
  289. fail_out:
  290. free_ep_req(loop->out_ep, out_req);
  291. fail_in:
  292. usb_ep_free_request(loop->in_ep, in_req);
  293. fail:
  294. return result;
  295. }
  296. static int enable_endpoint(struct usb_composite_dev *cdev,
  297. struct f_loopback *loop, struct usb_ep *ep)
  298. {
  299. int result;
  300. result = config_ep_by_speed(cdev->gadget, &(loop->function), ep);
  301. if (result)
  302. goto out;
  303. result = usb_ep_enable(ep);
  304. if (result < 0)
  305. goto out;
  306. ep->driver_data = loop;
  307. result = 0;
  308. out:
  309. return result;
  310. }
  311. static int
  312. enable_loopback(struct usb_composite_dev *cdev, struct f_loopback *loop)
  313. {
  314. int result = 0;
  315. result = enable_endpoint(cdev, loop, loop->in_ep);
  316. if (result)
  317. goto out;
  318. result = enable_endpoint(cdev, loop, loop->out_ep);
  319. if (result)
  320. goto disable_in;
  321. result = alloc_requests(cdev, loop);
  322. if (result)
  323. goto disable_out;
  324. DBG(cdev, "%s enabled\n", loop->function.name);
  325. return 0;
  326. disable_out:
  327. usb_ep_disable(loop->out_ep);
  328. disable_in:
  329. usb_ep_disable(loop->in_ep);
  330. out:
  331. return result;
  332. }
  333. static int loopback_set_alt(struct usb_function *f,
  334. unsigned intf, unsigned alt)
  335. {
  336. struct f_loopback *loop = func_to_loop(f);
  337. struct usb_composite_dev *cdev = f->config->cdev;
  338. /* we know alt is zero */
  339. disable_loopback(loop);
  340. return enable_loopback(cdev, loop);
  341. }
  342. static void loopback_disable(struct usb_function *f)
  343. {
  344. struct f_loopback *loop = func_to_loop(f);
  345. disable_loopback(loop);
  346. }
  347. static struct usb_function *loopback_alloc(struct usb_function_instance *fi)
  348. {
  349. struct f_loopback *loop;
  350. struct f_lb_opts *lb_opts;
  351. loop = kzalloc(sizeof *loop, GFP_KERNEL);
  352. if (!loop)
  353. return ERR_PTR(-ENOMEM);
  354. lb_opts = container_of(fi, struct f_lb_opts, func_inst);
  355. mutex_lock(&lb_opts->lock);
  356. lb_opts->refcnt++;
  357. mutex_unlock(&lb_opts->lock);
  358. loop->buflen = lb_opts->bulk_buflen;
  359. loop->qlen = lb_opts->qlen;
  360. if (!loop->qlen)
  361. loop->qlen = 32;
  362. loop->function.name = "loopback";
  363. loop->function.bind = loopback_bind;
  364. loop->function.set_alt = loopback_set_alt;
  365. loop->function.disable = loopback_disable;
  366. loop->function.strings = loopback_strings;
  367. loop->function.free_func = lb_free_func;
  368. return &loop->function;
  369. }
  370. static inline struct f_lb_opts *to_f_lb_opts(struct config_item *item)
  371. {
  372. return container_of(to_config_group(item), struct f_lb_opts,
  373. func_inst.group);
  374. }
  375. static void lb_attr_release(struct config_item *item)
  376. {
  377. struct f_lb_opts *lb_opts = to_f_lb_opts(item);
  378. usb_put_function_instance(&lb_opts->func_inst);
  379. }
  380. static struct configfs_item_operations lb_item_ops = {
  381. .release = lb_attr_release,
  382. };
  383. static ssize_t f_lb_opts_qlen_show(struct config_item *item, char *page)
  384. {
  385. struct f_lb_opts *opts = to_f_lb_opts(item);
  386. int result;
  387. mutex_lock(&opts->lock);
  388. result = sprintf(page, "%d\n", opts->qlen);
  389. mutex_unlock(&opts->lock);
  390. return result;
  391. }
  392. static ssize_t f_lb_opts_qlen_store(struct config_item *item,
  393. const char *page, size_t len)
  394. {
  395. struct f_lb_opts *opts = to_f_lb_opts(item);
  396. int ret;
  397. u32 num;
  398. mutex_lock(&opts->lock);
  399. if (opts->refcnt) {
  400. ret = -EBUSY;
  401. goto end;
  402. }
  403. ret = kstrtou32(page, 0, &num);
  404. if (ret)
  405. goto end;
  406. opts->qlen = num;
  407. ret = len;
  408. end:
  409. mutex_unlock(&opts->lock);
  410. return ret;
  411. }
  412. CONFIGFS_ATTR(f_lb_opts_, qlen);
  413. static ssize_t f_lb_opts_bulk_buflen_show(struct config_item *item, char *page)
  414. {
  415. struct f_lb_opts *opts = to_f_lb_opts(item);
  416. int result;
  417. mutex_lock(&opts->lock);
  418. result = sprintf(page, "%d\n", opts->bulk_buflen);
  419. mutex_unlock(&opts->lock);
  420. return result;
  421. }
  422. static ssize_t f_lb_opts_bulk_buflen_store(struct config_item *item,
  423. const char *page, size_t len)
  424. {
  425. struct f_lb_opts *opts = to_f_lb_opts(item);
  426. int ret;
  427. u32 num;
  428. mutex_lock(&opts->lock);
  429. if (opts->refcnt) {
  430. ret = -EBUSY;
  431. goto end;
  432. }
  433. ret = kstrtou32(page, 0, &num);
  434. if (ret)
  435. goto end;
  436. opts->bulk_buflen = num;
  437. ret = len;
  438. end:
  439. mutex_unlock(&opts->lock);
  440. return ret;
  441. }
  442. CONFIGFS_ATTR(f_lb_opts_, bulk_buflen);
  443. static struct configfs_attribute *lb_attrs[] = {
  444. &f_lb_opts_attr_qlen,
  445. &f_lb_opts_attr_bulk_buflen,
  446. NULL,
  447. };
  448. static struct config_item_type lb_func_type = {
  449. .ct_item_ops = &lb_item_ops,
  450. .ct_attrs = lb_attrs,
  451. .ct_owner = THIS_MODULE,
  452. };
  453. static void lb_free_instance(struct usb_function_instance *fi)
  454. {
  455. struct f_lb_opts *lb_opts;
  456. lb_opts = container_of(fi, struct f_lb_opts, func_inst);
  457. kfree(lb_opts);
  458. }
  459. static struct usb_function_instance *loopback_alloc_instance(void)
  460. {
  461. struct f_lb_opts *lb_opts;
  462. lb_opts = kzalloc(sizeof(*lb_opts), GFP_KERNEL);
  463. if (!lb_opts)
  464. return ERR_PTR(-ENOMEM);
  465. mutex_init(&lb_opts->lock);
  466. lb_opts->func_inst.free_func_inst = lb_free_instance;
  467. lb_opts->bulk_buflen = GZERO_BULK_BUFLEN;
  468. lb_opts->qlen = GZERO_QLEN;
  469. config_group_init_type_name(&lb_opts->func_inst.group, "",
  470. &lb_func_type);
  471. return &lb_opts->func_inst;
  472. }
  473. DECLARE_USB_FUNCTION(Loopback, loopback_alloc_instance, loopback_alloc);
  474. int __init lb_modinit(void)
  475. {
  476. return usb_function_register(&Loopbackusb_func);
  477. }
  478. void __exit lb_modexit(void)
  479. {
  480. usb_function_unregister(&Loopbackusb_func);
  481. }
  482. MODULE_LICENSE("GPL");