2 * f_loopback.c - USB peripheral loopback configuration driver
4 * Copyright (C) 2003-2008 David Brownell
5 * Copyright (C) 2008 by Nokia Corporation
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.
13 /* #define VERBOSE_DEBUG */
15 #include <linux/slab.h>
16 #include <linux/kernel.h>
17 #include <linux/device.h>
18 #include <linux/module.h>
19 #include <linux/err.h>
20 #include <linux/usb/composite.h>
26 * LOOPBACK FUNCTION ... a testing vehicle for USB peripherals,
28 * This takes messages of various sizes written OUT to a device, and loops
29 * them back so they can be read IN from it. It has been used by certain
30 * test applications. It supports limited testing of data queueing logic.
33 struct usb_function function;
36 struct usb_ep *out_ep;
39 static inline struct f_loopback *func_to_loop(struct usb_function *f)
41 return container_of(f, struct f_loopback, function);
45 static unsigned buflen;
47 /*-------------------------------------------------------------------------*/
49 static struct usb_interface_descriptor loopback_intf = {
50 .bLength = sizeof loopback_intf,
51 .bDescriptorType = USB_DT_INTERFACE,
54 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
55 /* .iInterface = DYNAMIC */
58 /* full speed support: */
60 static struct usb_endpoint_descriptor fs_loop_source_desc = {
61 .bLength = USB_DT_ENDPOINT_SIZE,
62 .bDescriptorType = USB_DT_ENDPOINT,
64 .bEndpointAddress = USB_DIR_IN,
65 .bmAttributes = USB_ENDPOINT_XFER_BULK,
68 static struct usb_endpoint_descriptor fs_loop_sink_desc = {
69 .bLength = USB_DT_ENDPOINT_SIZE,
70 .bDescriptorType = USB_DT_ENDPOINT,
72 .bEndpointAddress = USB_DIR_OUT,
73 .bmAttributes = USB_ENDPOINT_XFER_BULK,
76 static struct usb_descriptor_header *fs_loopback_descs[] = {
77 (struct usb_descriptor_header *) &loopback_intf,
78 (struct usb_descriptor_header *) &fs_loop_sink_desc,
79 (struct usb_descriptor_header *) &fs_loop_source_desc,
83 /* high speed support: */
85 static struct usb_endpoint_descriptor hs_loop_source_desc = {
86 .bLength = USB_DT_ENDPOINT_SIZE,
87 .bDescriptorType = USB_DT_ENDPOINT,
89 .bmAttributes = USB_ENDPOINT_XFER_BULK,
90 .wMaxPacketSize = cpu_to_le16(512),
93 static struct usb_endpoint_descriptor hs_loop_sink_desc = {
94 .bLength = USB_DT_ENDPOINT_SIZE,
95 .bDescriptorType = USB_DT_ENDPOINT,
97 .bmAttributes = USB_ENDPOINT_XFER_BULK,
98 .wMaxPacketSize = cpu_to_le16(512),
101 static struct usb_descriptor_header *hs_loopback_descs[] = {
102 (struct usb_descriptor_header *) &loopback_intf,
103 (struct usb_descriptor_header *) &hs_loop_source_desc,
104 (struct usb_descriptor_header *) &hs_loop_sink_desc,
108 /* super speed support: */
110 static struct usb_endpoint_descriptor ss_loop_source_desc = {
111 .bLength = USB_DT_ENDPOINT_SIZE,
112 .bDescriptorType = USB_DT_ENDPOINT,
114 .bmAttributes = USB_ENDPOINT_XFER_BULK,
115 .wMaxPacketSize = cpu_to_le16(1024),
118 static struct usb_ss_ep_comp_descriptor ss_loop_source_comp_desc = {
119 .bLength = USB_DT_SS_EP_COMP_SIZE,
120 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
123 .wBytesPerInterval = 0,
126 static struct usb_endpoint_descriptor ss_loop_sink_desc = {
127 .bLength = USB_DT_ENDPOINT_SIZE,
128 .bDescriptorType = USB_DT_ENDPOINT,
130 .bmAttributes = USB_ENDPOINT_XFER_BULK,
131 .wMaxPacketSize = cpu_to_le16(1024),
134 static struct usb_ss_ep_comp_descriptor ss_loop_sink_comp_desc = {
135 .bLength = USB_DT_SS_EP_COMP_SIZE,
136 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
139 .wBytesPerInterval = 0,
142 static struct usb_descriptor_header *ss_loopback_descs[] = {
143 (struct usb_descriptor_header *) &loopback_intf,
144 (struct usb_descriptor_header *) &ss_loop_source_desc,
145 (struct usb_descriptor_header *) &ss_loop_source_comp_desc,
146 (struct usb_descriptor_header *) &ss_loop_sink_desc,
147 (struct usb_descriptor_header *) &ss_loop_sink_comp_desc,
151 /* function-specific strings: */
153 static struct usb_string strings_loopback[] = {
154 [0].s = "loop input to output",
155 { } /* end of list */
158 static struct usb_gadget_strings stringtab_loop = {
159 .language = 0x0409, /* en-us */
160 .strings = strings_loopback,
163 static struct usb_gadget_strings *loopback_strings[] = {
168 /*-------------------------------------------------------------------------*/
170 static int loopback_bind(struct usb_configuration *c, struct usb_function *f)
172 struct usb_composite_dev *cdev = c->cdev;
173 struct f_loopback *loop = func_to_loop(f);
177 /* allocate interface ID(s) */
178 id = usb_interface_id(c, f);
181 loopback_intf.bInterfaceNumber = id;
183 id = usb_string_id(cdev);
186 strings_loopback[0].id = id;
187 loopback_intf.iInterface = id;
189 /* allocate endpoints */
191 loop->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_loop_source_desc);
194 ERROR(cdev, "%s: can't autoconfigure on %s\n",
195 f->name, cdev->gadget->name);
198 loop->in_ep->driver_data = cdev; /* claim */
200 loop->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_loop_sink_desc);
203 loop->out_ep->driver_data = cdev; /* claim */
205 /* support high speed hardware */
206 hs_loop_source_desc.bEndpointAddress =
207 fs_loop_source_desc.bEndpointAddress;
208 hs_loop_sink_desc.bEndpointAddress = fs_loop_sink_desc.bEndpointAddress;
210 /* support super speed hardware */
211 ss_loop_source_desc.bEndpointAddress =
212 fs_loop_source_desc.bEndpointAddress;
213 ss_loop_sink_desc.bEndpointAddress = fs_loop_sink_desc.bEndpointAddress;
215 ret = usb_assign_descriptors(f, fs_loopback_descs, hs_loopback_descs,
220 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
221 (gadget_is_superspeed(c->cdev->gadget) ? "super" :
222 (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")),
223 f->name, loop->in_ep->name, loop->out_ep->name);
227 static void lb_free_func(struct usb_function *f)
229 struct f_lb_opts *opts;
231 opts = container_of(f->fi, struct f_lb_opts, func_inst);
233 mutex_lock(&opts->lock);
235 mutex_unlock(&opts->lock);
237 usb_free_all_descriptors(f);
238 kfree(func_to_loop(f));
241 static void loopback_complete(struct usb_ep *ep, struct usb_request *req)
243 struct f_loopback *loop = ep->driver_data;
244 struct usb_composite_dev *cdev = loop->function.config->cdev;
245 int status = req->status;
249 case 0: /* normal completion? */
250 if (ep == loop->out_ep) {
251 req->zero = (req->actual < req->length);
252 req->length = req->actual;
255 /* queue the buffer for some later OUT packet */
256 req->length = buflen;
257 status = usb_ep_queue(ep, req, GFP_ATOMIC);
261 /* "should never get here" */
265 ERROR(cdev, "%s loop complete --> %d, %d/%d\n", ep->name,
266 status, req->actual, req->length);
269 /* NOTE: since this driver doesn't maintain an explicit record
270 * of requests it submitted (just maintains qlen count), we
271 * rely on the hardware driver to clean up on disconnect or
274 case -ECONNABORTED: /* hardware forced ep reset */
275 case -ECONNRESET: /* request dequeued */
276 case -ESHUTDOWN: /* disconnect from host */
277 free_ep_req(ep, req);
282 static void disable_loopback(struct f_loopback *loop)
284 struct usb_composite_dev *cdev;
286 cdev = loop->function.config->cdev;
287 disable_endpoints(cdev, loop->in_ep, loop->out_ep, NULL, NULL);
288 VDBG(cdev, "%s disabled\n", loop->function.name);
291 static inline struct usb_request *lb_alloc_ep_req(struct usb_ep *ep, int len)
293 return alloc_ep_req(ep, len, buflen);
296 static int enable_endpoint(struct usb_composite_dev *cdev, struct f_loopback *loop,
299 struct usb_request *req;
304 * one endpoint writes data back IN to the host while another endpoint
305 * just reads OUT packets
307 result = config_ep_by_speed(cdev->gadget, &(loop->function), ep);
310 result = usb_ep_enable(ep);
313 ep->driver_data = loop;
316 * allocate a bunch of read buffers and queue them all at once.
317 * we buffer at most 'qlen' transfers; fewer if any need more
318 * than 'buflen' bytes each.
320 for (i = 0; i < qlen && result == 0; i++) {
321 req = lb_alloc_ep_req(ep, 0);
325 req->complete = loopback_complete;
326 result = usb_ep_queue(ep, req, GFP_ATOMIC);
328 ERROR(cdev, "%s queue req --> %d\n",
344 enable_loopback(struct usb_composite_dev *cdev, struct f_loopback *loop)
348 result = enable_endpoint(cdev, loop, loop->in_ep);
352 result = enable_endpoint(cdev, loop, loop->out_ep);
356 DBG(cdev, "%s enabled\n", loop->function.name);
360 static int loopback_set_alt(struct usb_function *f,
361 unsigned intf, unsigned alt)
363 struct f_loopback *loop = func_to_loop(f);
364 struct usb_composite_dev *cdev = f->config->cdev;
366 /* we know alt is zero */
367 if (loop->in_ep->driver_data)
368 disable_loopback(loop);
369 return enable_loopback(cdev, loop);
372 static void loopback_disable(struct usb_function *f)
374 struct f_loopback *loop = func_to_loop(f);
376 disable_loopback(loop);
379 static struct usb_function *loopback_alloc(struct usb_function_instance *fi)
381 struct f_loopback *loop;
382 struct f_lb_opts *lb_opts;
384 loop = kzalloc(sizeof *loop, GFP_KERNEL);
386 return ERR_PTR(-ENOMEM);
388 lb_opts = container_of(fi, struct f_lb_opts, func_inst);
390 mutex_lock(&lb_opts->lock);
392 mutex_unlock(&lb_opts->lock);
394 buflen = lb_opts->bulk_buflen;
395 qlen = lb_opts->qlen;
399 loop->function.name = "loopback";
400 loop->function.bind = loopback_bind;
401 loop->function.set_alt = loopback_set_alt;
402 loop->function.disable = loopback_disable;
403 loop->function.strings = loopback_strings;
405 loop->function.free_func = lb_free_func;
407 return &loop->function;
410 static inline struct f_lb_opts *to_f_lb_opts(struct config_item *item)
412 return container_of(to_config_group(item), struct f_lb_opts,
416 CONFIGFS_ATTR_STRUCT(f_lb_opts);
417 CONFIGFS_ATTR_OPS(f_lb_opts);
419 static void lb_attr_release(struct config_item *item)
421 struct f_lb_opts *lb_opts = to_f_lb_opts(item);
423 usb_put_function_instance(&lb_opts->func_inst);
426 static struct configfs_item_operations lb_item_ops = {
427 .release = lb_attr_release,
428 .show_attribute = f_lb_opts_attr_show,
429 .store_attribute = f_lb_opts_attr_store,
432 static ssize_t f_lb_opts_qlen_show(struct f_lb_opts *opts, char *page)
436 mutex_lock(&opts->lock);
437 result = sprintf(page, "%d", opts->qlen);
438 mutex_unlock(&opts->lock);
443 static ssize_t f_lb_opts_qlen_store(struct f_lb_opts *opts,
444 const char *page, size_t len)
449 mutex_lock(&opts->lock);
455 ret = kstrtou32(page, 0, &num);
462 mutex_unlock(&opts->lock);
466 static struct f_lb_opts_attribute f_lb_opts_qlen =
467 __CONFIGFS_ATTR(qlen, S_IRUGO | S_IWUSR,
469 f_lb_opts_qlen_store);
471 static ssize_t f_lb_opts_bulk_buflen_show(struct f_lb_opts *opts, char *page)
475 mutex_lock(&opts->lock);
476 result = sprintf(page, "%d", opts->bulk_buflen);
477 mutex_unlock(&opts->lock);
482 static ssize_t f_lb_opts_bulk_buflen_store(struct f_lb_opts *opts,
483 const char *page, size_t len)
488 mutex_lock(&opts->lock);
494 ret = kstrtou32(page, 0, &num);
498 opts->bulk_buflen = num;
501 mutex_unlock(&opts->lock);
505 static struct f_lb_opts_attribute f_lb_opts_bulk_buflen =
506 __CONFIGFS_ATTR(buflen, S_IRUGO | S_IWUSR,
507 f_lb_opts_bulk_buflen_show,
508 f_lb_opts_bulk_buflen_store);
510 static struct configfs_attribute *lb_attrs[] = {
511 &f_lb_opts_qlen.attr,
512 &f_lb_opts_bulk_buflen.attr,
516 static struct config_item_type lb_func_type = {
517 .ct_item_ops = &lb_item_ops,
518 .ct_attrs = lb_attrs,
519 .ct_owner = THIS_MODULE,
522 static void lb_free_instance(struct usb_function_instance *fi)
524 struct f_lb_opts *lb_opts;
526 lb_opts = container_of(fi, struct f_lb_opts, func_inst);
530 static struct usb_function_instance *loopback_alloc_instance(void)
532 struct f_lb_opts *lb_opts;
534 lb_opts = kzalloc(sizeof(*lb_opts), GFP_KERNEL);
536 return ERR_PTR(-ENOMEM);
537 mutex_init(&lb_opts->lock);
538 lb_opts->func_inst.free_func_inst = lb_free_instance;
539 lb_opts->bulk_buflen = GZERO_BULK_BUFLEN;
540 lb_opts->qlen = GZERO_QLEN;
542 config_group_init_type_name(&lb_opts->func_inst.group, "",
545 return &lb_opts->func_inst;
547 DECLARE_USB_FUNCTION(Loopback, loopback_alloc_instance, loopback_alloc);
549 int __init lb_modinit(void)
553 ret = usb_function_register(&Loopbackusb_func);
558 void __exit lb_modexit(void)
560 usb_function_unregister(&Loopbackusb_func);
563 MODULE_LICENSE("GPL");