blob: 3d103a2f998f13d246492f0a058fddb1e26b7cc9 [file] [log] [blame]
David Brownelle5760fd2008-06-19 17:55:35 -07001/*
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.
David Brownelle5760fd2008-06-19 17:55:35 -070011 */
12
13/* #define VERBOSE_DEBUG */
14
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
David Brownelle5760fd2008-06-19 17:55:35 -070016#include <linux/kernel.h>
David Brownelle5760fd2008-06-19 17:55:35 -070017#include <linux/device.h>
18
19#include "g_zero.h"
20#include "gadget_chips.h"
21
22
23/*
24 * LOOPBACK FUNCTION ... a testing vehicle for USB peripherals,
25 *
26 * This takes messages of various sizes written OUT to a device, and loops
27 * them back so they can be read IN from it. It has been used by certain
28 * test applications. It supports limited testing of data queueing logic.
29 *
30 *
31 * This is currently packaged as a configuration driver, which can't be
32 * combined with other functions to make composite devices. However, it
33 * can be combined with other independent configurations.
34 */
35struct f_loopback {
36 struct usb_function function;
37
38 struct usb_ep *in_ep;
39 struct usb_ep *out_ep;
40};
41
42static inline struct f_loopback *func_to_loop(struct usb_function *f)
43{
44 return container_of(f, struct f_loopback, function);
45}
46
47static unsigned qlen = 32;
48module_param(qlen, uint, 0);
49MODULE_PARM_DESC(qlenn, "depth of loopback queue");
50
51/*-------------------------------------------------------------------------*/
52
53static struct usb_interface_descriptor loopback_intf = {
54 .bLength = sizeof loopback_intf,
55 .bDescriptorType = USB_DT_INTERFACE,
56
57 .bNumEndpoints = 2,
58 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
59 /* .iInterface = DYNAMIC */
60};
61
62/* full speed support: */
63
David Brownell7e75bc02008-08-18 17:41:31 -070064static struct usb_endpoint_descriptor fs_loop_source_desc = {
David Brownelle5760fd2008-06-19 17:55:35 -070065 .bLength = USB_DT_ENDPOINT_SIZE,
66 .bDescriptorType = USB_DT_ENDPOINT,
67
68 .bEndpointAddress = USB_DIR_IN,
69 .bmAttributes = USB_ENDPOINT_XFER_BULK,
70};
71
David Brownell7e75bc02008-08-18 17:41:31 -070072static struct usb_endpoint_descriptor fs_loop_sink_desc = {
David Brownelle5760fd2008-06-19 17:55:35 -070073 .bLength = USB_DT_ENDPOINT_SIZE,
74 .bDescriptorType = USB_DT_ENDPOINT,
75
76 .bEndpointAddress = USB_DIR_OUT,
77 .bmAttributes = USB_ENDPOINT_XFER_BULK,
78};
79
80static struct usb_descriptor_header *fs_loopback_descs[] = {
81 (struct usb_descriptor_header *) &loopback_intf,
David Brownell7e75bc02008-08-18 17:41:31 -070082 (struct usb_descriptor_header *) &fs_loop_sink_desc,
83 (struct usb_descriptor_header *) &fs_loop_source_desc,
David Brownelle5760fd2008-06-19 17:55:35 -070084 NULL,
85};
86
87/* high speed support: */
88
David Brownell7e75bc02008-08-18 17:41:31 -070089static struct usb_endpoint_descriptor hs_loop_source_desc = {
David Brownelle5760fd2008-06-19 17:55:35 -070090 .bLength = USB_DT_ENDPOINT_SIZE,
91 .bDescriptorType = USB_DT_ENDPOINT,
92
93 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -080094 .wMaxPacketSize = cpu_to_le16(512),
David Brownelle5760fd2008-06-19 17:55:35 -070095};
96
David Brownell7e75bc02008-08-18 17:41:31 -070097static struct usb_endpoint_descriptor hs_loop_sink_desc = {
David Brownelle5760fd2008-06-19 17:55:35 -070098 .bLength = USB_DT_ENDPOINT_SIZE,
99 .bDescriptorType = USB_DT_ENDPOINT,
100
101 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800102 .wMaxPacketSize = cpu_to_le16(512),
David Brownelle5760fd2008-06-19 17:55:35 -0700103};
104
105static struct usb_descriptor_header *hs_loopback_descs[] = {
106 (struct usb_descriptor_header *) &loopback_intf,
David Brownell7e75bc02008-08-18 17:41:31 -0700107 (struct usb_descriptor_header *) &hs_loop_source_desc,
108 (struct usb_descriptor_header *) &hs_loop_sink_desc,
David Brownelle5760fd2008-06-19 17:55:35 -0700109 NULL,
110};
111
Amit Blay57c97c02011-07-03 17:29:31 +0300112/* super speed support: */
113
114static struct usb_endpoint_descriptor ss_loop_source_desc = {
115 .bLength = USB_DT_ENDPOINT_SIZE,
116 .bDescriptorType = USB_DT_ENDPOINT,
117
118 .bmAttributes = USB_ENDPOINT_XFER_BULK,
119 .wMaxPacketSize = cpu_to_le16(1024),
120};
121
122struct usb_ss_ep_comp_descriptor ss_loop_source_comp_desc = {
123 .bLength = USB_DT_SS_EP_COMP_SIZE,
124 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
125 .bMaxBurst = 0,
126 .bmAttributes = 0,
127 .wBytesPerInterval = 0,
128};
129
130static struct usb_endpoint_descriptor ss_loop_sink_desc = {
131 .bLength = USB_DT_ENDPOINT_SIZE,
132 .bDescriptorType = USB_DT_ENDPOINT,
133
134 .bmAttributes = USB_ENDPOINT_XFER_BULK,
135 .wMaxPacketSize = cpu_to_le16(1024),
136};
137
138struct usb_ss_ep_comp_descriptor ss_loop_sink_comp_desc = {
139 .bLength = USB_DT_SS_EP_COMP_SIZE,
140 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
141 .bMaxBurst = 0,
142 .bmAttributes = 0,
143 .wBytesPerInterval = 0,
144};
145
146static struct usb_descriptor_header *ss_loopback_descs[] = {
147 (struct usb_descriptor_header *) &loopback_intf,
148 (struct usb_descriptor_header *) &ss_loop_source_desc,
149 (struct usb_descriptor_header *) &ss_loop_source_comp_desc,
150 (struct usb_descriptor_header *) &ss_loop_sink_desc,
151 (struct usb_descriptor_header *) &ss_loop_sink_comp_desc,
152 NULL,
153};
154
David Brownelle5760fd2008-06-19 17:55:35 -0700155/* function-specific strings: */
156
157static struct usb_string strings_loopback[] = {
158 [0].s = "loop input to output",
159 { } /* end of list */
160};
161
162static struct usb_gadget_strings stringtab_loop = {
163 .language = 0x0409, /* en-us */
164 .strings = strings_loopback,
165};
166
167static struct usb_gadget_strings *loopback_strings[] = {
168 &stringtab_loop,
169 NULL,
170};
171
172/*-------------------------------------------------------------------------*/
173
174static int __init
175loopback_bind(struct usb_configuration *c, struct usb_function *f)
176{
177 struct usb_composite_dev *cdev = c->cdev;
178 struct f_loopback *loop = func_to_loop(f);
179 int id;
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200180 int ret;
David Brownelle5760fd2008-06-19 17:55:35 -0700181
182 /* allocate interface ID(s) */
183 id = usb_interface_id(c, f);
184 if (id < 0)
185 return id;
186 loopback_intf.bInterfaceNumber = id;
187
Sebastian Andrzej Siewior78f46f02012-12-23 21:09:59 +0100188 id = usb_string_id(cdev);
189 if (id < 0)
190 return id;
191 strings_loopback[0].id = id;
192 loopback_intf.iInterface = id;
193
David Brownelle5760fd2008-06-19 17:55:35 -0700194 /* allocate endpoints */
195
David Brownell7e75bc02008-08-18 17:41:31 -0700196 loop->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_loop_source_desc);
David Brownelle5760fd2008-06-19 17:55:35 -0700197 if (!loop->in_ep) {
198autoconf_fail:
199 ERROR(cdev, "%s: can't autoconfigure on %s\n",
200 f->name, cdev->gadget->name);
201 return -ENODEV;
202 }
203 loop->in_ep->driver_data = cdev; /* claim */
204
David Brownell7e75bc02008-08-18 17:41:31 -0700205 loop->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_loop_sink_desc);
David Brownelle5760fd2008-06-19 17:55:35 -0700206 if (!loop->out_ep)
207 goto autoconf_fail;
208 loop->out_ep->driver_data = cdev; /* claim */
209
210 /* support high speed hardware */
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200211 hs_loop_source_desc.bEndpointAddress =
212 fs_loop_source_desc.bEndpointAddress;
213 hs_loop_sink_desc.bEndpointAddress = fs_loop_sink_desc.bEndpointAddress;
David Brownelle5760fd2008-06-19 17:55:35 -0700214
Amit Blay57c97c02011-07-03 17:29:31 +0300215 /* support super speed hardware */
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200216 ss_loop_source_desc.bEndpointAddress =
217 fs_loop_source_desc.bEndpointAddress;
218 ss_loop_sink_desc.bEndpointAddress = fs_loop_sink_desc.bEndpointAddress;
219
220 ret = usb_assign_descriptors(f, fs_loopback_descs, hs_loopback_descs,
221 ss_loopback_descs);
222 if (ret)
223 return ret;
Amit Blay57c97c02011-07-03 17:29:31 +0300224
David Brownelle5760fd2008-06-19 17:55:35 -0700225 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
Amit Blay57c97c02011-07-03 17:29:31 +0300226 (gadget_is_superspeed(c->cdev->gadget) ? "super" :
227 (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")),
David Brownelle5760fd2008-06-19 17:55:35 -0700228 f->name, loop->in_ep->name, loop->out_ep->name);
229 return 0;
230}
231
232static void
233loopback_unbind(struct usb_configuration *c, struct usb_function *f)
234{
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200235 usb_free_all_descriptors(f);
David Brownelle5760fd2008-06-19 17:55:35 -0700236 kfree(func_to_loop(f));
237}
238
239static void loopback_complete(struct usb_ep *ep, struct usb_request *req)
240{
241 struct f_loopback *loop = ep->driver_data;
242 struct usb_composite_dev *cdev = loop->function.config->cdev;
243 int status = req->status;
244
245 switch (status) {
246
247 case 0: /* normal completion? */
248 if (ep == loop->out_ep) {
249 /* loop this OUT packet back IN to the host */
250 req->zero = (req->actual < req->length);
251 req->length = req->actual;
252 status = usb_ep_queue(loop->in_ep, req, GFP_ATOMIC);
253 if (status == 0)
254 return;
255
256 /* "should never get here" */
257 ERROR(cdev, "can't loop %s to %s: %d\n",
258 ep->name, loop->in_ep->name,
259 status);
260 }
261
262 /* queue the buffer for some later OUT packet */
263 req->length = buflen;
264 status = usb_ep_queue(loop->out_ep, req, GFP_ATOMIC);
265 if (status == 0)
266 return;
267
268 /* "should never get here" */
269 /* FALLTHROUGH */
270
271 default:
272 ERROR(cdev, "%s loop complete --> %d, %d/%d\n", ep->name,
273 status, req->actual, req->length);
274 /* FALLTHROUGH */
275
276 /* NOTE: since this driver doesn't maintain an explicit record
277 * of requests it submitted (just maintains qlen count), we
278 * rely on the hardware driver to clean up on disconnect or
279 * endpoint disable.
280 */
281 case -ECONNABORTED: /* hardware forced ep reset */
282 case -ECONNRESET: /* request dequeued */
283 case -ESHUTDOWN: /* disconnect from host */
284 free_ep_req(ep, req);
285 return;
286 }
287}
288
289static void disable_loopback(struct f_loopback *loop)
290{
291 struct usb_composite_dev *cdev;
292
293 cdev = loop->function.config->cdev;
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700294 disable_endpoints(cdev, loop->in_ep, loop->out_ep, NULL, NULL);
David Brownelle5760fd2008-06-19 17:55:35 -0700295 VDBG(cdev, "%s disabled\n", loop->function.name);
296}
297
298static int
299enable_loopback(struct usb_composite_dev *cdev, struct f_loopback *loop)
300{
301 int result = 0;
David Brownelle5760fd2008-06-19 17:55:35 -0700302 struct usb_ep *ep;
303 struct usb_request *req;
304 unsigned i;
305
David Brownelle5760fd2008-06-19 17:55:35 -0700306 /* one endpoint writes data back IN to the host */
307 ep = loop->in_ep;
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300308 result = config_ep_by_speed(cdev->gadget, &(loop->function), ep);
309 if (result)
310 return result;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300311 result = usb_ep_enable(ep);
David Brownelle5760fd2008-06-19 17:55:35 -0700312 if (result < 0)
313 return result;
314 ep->driver_data = loop;
315
316 /* one endpoint just reads OUT packets */
317 ep = loop->out_ep;
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300318 result = config_ep_by_speed(cdev->gadget, &(loop->function), ep);
319 if (result)
320 goto fail0;
321
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300322 result = usb_ep_enable(ep);
David Brownelle5760fd2008-06-19 17:55:35 -0700323 if (result < 0) {
324fail0:
325 ep = loop->in_ep;
326 usb_ep_disable(ep);
327 ep->driver_data = NULL;
328 return result;
329 }
330 ep->driver_data = loop;
331
332 /* allocate a bunch of read buffers and queue them all at once.
333 * we buffer at most 'qlen' transfers; fewer if any need more
334 * than 'buflen' bytes each.
335 */
336 for (i = 0; i < qlen && result == 0; i++) {
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700337 req = alloc_ep_req(ep, 0);
David Brownelle5760fd2008-06-19 17:55:35 -0700338 if (req) {
339 req->complete = loopback_complete;
340 result = usb_ep_queue(ep, req, GFP_ATOMIC);
341 if (result)
342 ERROR(cdev, "%s queue req --> %d\n",
343 ep->name, result);
344 } else {
345 usb_ep_disable(ep);
346 ep->driver_data = NULL;
347 result = -ENOMEM;
348 goto fail0;
349 }
350 }
351
352 DBG(cdev, "%s enabled\n", loop->function.name);
353 return result;
354}
355
356static int loopback_set_alt(struct usb_function *f,
357 unsigned intf, unsigned alt)
358{
359 struct f_loopback *loop = func_to_loop(f);
360 struct usb_composite_dev *cdev = f->config->cdev;
361
362 /* we know alt is zero */
363 if (loop->in_ep->driver_data)
364 disable_loopback(loop);
365 return enable_loopback(cdev, loop);
366}
367
368static void loopback_disable(struct usb_function *f)
369{
370 struct f_loopback *loop = func_to_loop(f);
371
372 disable_loopback(loop);
373}
374
375/*-------------------------------------------------------------------------*/
376
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200377static int __init loopback_bind_config(struct usb_configuration *c)
David Brownelle5760fd2008-06-19 17:55:35 -0700378{
379 struct f_loopback *loop;
380 int status;
381
382 loop = kzalloc(sizeof *loop, GFP_KERNEL);
383 if (!loop)
384 return -ENOMEM;
385
386 loop->function.name = "loopback";
David Brownelle5760fd2008-06-19 17:55:35 -0700387 loop->function.bind = loopback_bind;
388 loop->function.unbind = loopback_unbind;
389 loop->function.set_alt = loopback_set_alt;
390 loop->function.disable = loopback_disable;
391
392 status = usb_add_function(c, &loop->function);
393 if (status)
394 kfree(loop);
395 return status;
396}