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.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 /* #define VERBOSE_DEBUG */
24 #include <linux/kernel.h>
25 #include <linux/device.h>
28 #include "gadget_chips.h"
32 * LOOPBACK FUNCTION ... a testing vehicle for USB peripherals,
34 * This takes messages of various sizes written OUT to a device, and loops
35 * them back so they can be read IN from it. It has been used by certain
36 * test applications. It supports limited testing of data queueing logic.
39 * This is currently packaged as a configuration driver, which can't be
40 * combined with other functions to make composite devices. However, it
41 * can be combined with other independent configurations.
44 struct usb_function function;
47 struct usb_ep *out_ep;
50 static inline struct f_loopback *func_to_loop(struct usb_function *f)
52 return container_of(f, struct f_loopback, function);
55 static unsigned qlen = 32;
56 module_param(qlen, uint, 0);
57 MODULE_PARM_DESC(qlenn, "depth of loopback queue");
59 /*-------------------------------------------------------------------------*/
61 static struct usb_interface_descriptor loopback_intf = {
62 .bLength = sizeof loopback_intf,
63 .bDescriptorType = USB_DT_INTERFACE,
66 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
67 /* .iInterface = DYNAMIC */
70 /* full speed support: */
72 static struct usb_endpoint_descriptor fs_loop_source_desc = {
73 .bLength = USB_DT_ENDPOINT_SIZE,
74 .bDescriptorType = USB_DT_ENDPOINT,
76 .bEndpointAddress = USB_DIR_IN,
77 .bmAttributes = USB_ENDPOINT_XFER_BULK,
80 static struct usb_endpoint_descriptor fs_loop_sink_desc = {
81 .bLength = USB_DT_ENDPOINT_SIZE,
82 .bDescriptorType = USB_DT_ENDPOINT,
84 .bEndpointAddress = USB_DIR_OUT,
85 .bmAttributes = USB_ENDPOINT_XFER_BULK,
88 static struct usb_descriptor_header *fs_loopback_descs[] = {
89 (struct usb_descriptor_header *) &loopback_intf,
90 (struct usb_descriptor_header *) &fs_loop_sink_desc,
91 (struct usb_descriptor_header *) &fs_loop_source_desc,
95 /* high speed support: */
97 static struct usb_endpoint_descriptor hs_loop_source_desc = {
98 .bLength = USB_DT_ENDPOINT_SIZE,
99 .bDescriptorType = USB_DT_ENDPOINT,
101 .bmAttributes = USB_ENDPOINT_XFER_BULK,
102 .wMaxPacketSize = cpu_to_le16(512),
105 static struct usb_endpoint_descriptor hs_loop_sink_desc = {
106 .bLength = USB_DT_ENDPOINT_SIZE,
107 .bDescriptorType = USB_DT_ENDPOINT,
109 .bmAttributes = USB_ENDPOINT_XFER_BULK,
110 .wMaxPacketSize = cpu_to_le16(512),
113 static struct usb_descriptor_header *hs_loopback_descs[] = {
114 (struct usb_descriptor_header *) &loopback_intf,
115 (struct usb_descriptor_header *) &hs_loop_source_desc,
116 (struct usb_descriptor_header *) &hs_loop_sink_desc,
120 /* function-specific strings: */
122 static struct usb_string strings_loopback[] = {
123 [0].s = "loop input to output",
124 { } /* end of list */
127 static struct usb_gadget_strings stringtab_loop = {
128 .language = 0x0409, /* en-us */
129 .strings = strings_loopback,
132 static struct usb_gadget_strings *loopback_strings[] = {
137 /*-------------------------------------------------------------------------*/
140 loopback_bind(struct usb_configuration *c, struct usb_function *f)
142 struct usb_composite_dev *cdev = c->cdev;
143 struct f_loopback *loop = func_to_loop(f);
146 /* allocate interface ID(s) */
147 id = usb_interface_id(c, f);
150 loopback_intf.bInterfaceNumber = id;
152 /* allocate endpoints */
154 loop->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_loop_source_desc);
157 ERROR(cdev, "%s: can't autoconfigure on %s\n",
158 f->name, cdev->gadget->name);
161 loop->in_ep->driver_data = cdev; /* claim */
163 loop->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_loop_sink_desc);
166 loop->out_ep->driver_data = cdev; /* claim */
168 /* support high speed hardware */
169 if (gadget_is_dualspeed(c->cdev->gadget)) {
170 hs_loop_source_desc.bEndpointAddress =
171 fs_loop_source_desc.bEndpointAddress;
172 hs_loop_sink_desc.bEndpointAddress =
173 fs_loop_sink_desc.bEndpointAddress;
174 f->hs_descriptors = hs_loopback_descs;
177 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
178 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
179 f->name, loop->in_ep->name, loop->out_ep->name);
184 loopback_unbind(struct usb_configuration *c, struct usb_function *f)
186 kfree(func_to_loop(f));
189 static void loopback_complete(struct usb_ep *ep, struct usb_request *req)
191 struct f_loopback *loop = ep->driver_data;
192 struct usb_composite_dev *cdev = loop->function.config->cdev;
193 int status = req->status;
197 case 0: /* normal completion? */
198 if (ep == loop->out_ep) {
199 /* loop this OUT packet back IN to the host */
200 req->zero = (req->actual < req->length);
201 req->length = req->actual;
202 status = usb_ep_queue(loop->in_ep, req, GFP_ATOMIC);
206 /* "should never get here" */
207 ERROR(cdev, "can't loop %s to %s: %d\n",
208 ep->name, loop->in_ep->name,
212 /* queue the buffer for some later OUT packet */
213 req->length = buflen;
214 status = usb_ep_queue(loop->out_ep, req, GFP_ATOMIC);
218 /* "should never get here" */
222 ERROR(cdev, "%s loop complete --> %d, %d/%d\n", ep->name,
223 status, req->actual, req->length);
226 /* NOTE: since this driver doesn't maintain an explicit record
227 * of requests it submitted (just maintains qlen count), we
228 * rely on the hardware driver to clean up on disconnect or
231 case -ECONNABORTED: /* hardware forced ep reset */
232 case -ECONNRESET: /* request dequeued */
233 case -ESHUTDOWN: /* disconnect from host */
234 free_ep_req(ep, req);
239 static void disable_loopback(struct f_loopback *loop)
241 struct usb_composite_dev *cdev;
243 cdev = loop->function.config->cdev;
244 disable_endpoints(cdev, loop->in_ep, loop->out_ep);
245 VDBG(cdev, "%s disabled\n", loop->function.name);
249 enable_loopback(struct usb_composite_dev *cdev, struct f_loopback *loop)
252 const struct usb_endpoint_descriptor *src, *sink;
254 struct usb_request *req;
257 src = ep_choose(cdev->gadget,
258 &hs_loop_source_desc, &fs_loop_source_desc);
259 sink = ep_choose(cdev->gadget,
260 &hs_loop_sink_desc, &fs_loop_sink_desc);
262 /* one endpoint writes data back IN to the host */
264 result = usb_ep_enable(ep, src);
267 ep->driver_data = loop;
269 /* one endpoint just reads OUT packets */
271 result = usb_ep_enable(ep, sink);
276 ep->driver_data = NULL;
279 ep->driver_data = loop;
281 /* allocate a bunch of read buffers and queue them all at once.
282 * we buffer at most 'qlen' transfers; fewer if any need more
283 * than 'buflen' bytes each.
285 for (i = 0; i < qlen && result == 0; i++) {
286 req = alloc_ep_req(ep);
288 req->complete = loopback_complete;
289 result = usb_ep_queue(ep, req, GFP_ATOMIC);
291 ERROR(cdev, "%s queue req --> %d\n",
295 ep->driver_data = NULL;
301 DBG(cdev, "%s enabled\n", loop->function.name);
305 static int loopback_set_alt(struct usb_function *f,
306 unsigned intf, unsigned alt)
308 struct f_loopback *loop = func_to_loop(f);
309 struct usb_composite_dev *cdev = f->config->cdev;
311 /* we know alt is zero */
312 if (loop->in_ep->driver_data)
313 disable_loopback(loop);
314 return enable_loopback(cdev, loop);
317 static void loopback_disable(struct usb_function *f)
319 struct f_loopback *loop = func_to_loop(f);
321 disable_loopback(loop);
324 /*-------------------------------------------------------------------------*/
326 static int __init loopback_bind_config(struct usb_configuration *c)
328 struct f_loopback *loop;
331 loop = kzalloc(sizeof *loop, GFP_KERNEL);
335 loop->function.name = "loopback";
336 loop->function.descriptors = fs_loopback_descs;
337 loop->function.bind = loopback_bind;
338 loop->function.unbind = loopback_unbind;
339 loop->function.set_alt = loopback_set_alt;
340 loop->function.disable = loopback_disable;
342 status = usb_add_function(c, &loop->function);
348 static struct usb_configuration loopback_driver = {
350 .strings = loopback_strings,
351 .bind = loopback_bind_config,
352 .bConfigurationValue = 2,
353 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
354 /* .iConfiguration = DYNAMIC */
358 * loopback_add - add a loopback testing configuration to a device
359 * @cdev: the device to support the loopback configuration
361 int __init loopback_add(struct usb_composite_dev *cdev, bool autoresume)
365 /* allocate string ID(s) */
366 id = usb_string_id(cdev);
369 strings_loopback[0].id = id;
371 loopback_intf.iInterface = id;
372 loopback_driver.iConfiguration = id;
374 /* support autoresume for remote wakeup testing */
376 sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
378 /* support OTG systems */
379 if (gadget_is_otg(cdev->gadget)) {
380 loopback_driver.descriptors = otg_desc;
381 loopback_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
384 return usb_add_config(cdev, &loopback_driver);