blob: b79065376a16db8e9f0335fda2ea7b7be4b81810 [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>
Sebastian Andrzej Siewiorcf9a08a2012-12-23 21:10:01 +010018#include <linux/module.h>
19#include <linux/err.h>
20#include <linux/usb/composite.h>
David Brownelle5760fd2008-06-19 17:55:35 -070021
22#include "g_zero.h"
Andrzej Pietrasiewicz1efd54e2013-11-07 08:41:26 +010023#include "u_f.h"
David Brownelle5760fd2008-06-19 17:55:35 -070024
25/*
26 * LOOPBACK FUNCTION ... a testing vehicle for USB peripherals,
27 *
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.
31 *
32 *
33 * This is currently packaged as a configuration driver, which can't be
34 * combined with other functions to make composite devices. However, it
35 * can be combined with other independent configurations.
36 */
37struct f_loopback {
38 struct usb_function function;
39
40 struct usb_ep *in_ep;
41 struct usb_ep *out_ep;
42};
43
44static inline struct f_loopback *func_to_loop(struct usb_function *f)
45{
46 return container_of(f, struct f_loopback, function);
47}
48
Sebastian Andrzej Siewiorcf9a08a2012-12-23 21:10:01 +010049static unsigned qlen;
50static unsigned buflen;
David Brownelle5760fd2008-06-19 17:55:35 -070051
52/*-------------------------------------------------------------------------*/
53
54static struct usb_interface_descriptor loopback_intf = {
55 .bLength = sizeof loopback_intf,
56 .bDescriptorType = USB_DT_INTERFACE,
57
58 .bNumEndpoints = 2,
59 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
60 /* .iInterface = DYNAMIC */
61};
62
63/* full speed support: */
64
David Brownell7e75bc02008-08-18 17:41:31 -070065static struct usb_endpoint_descriptor fs_loop_source_desc = {
David Brownelle5760fd2008-06-19 17:55:35 -070066 .bLength = USB_DT_ENDPOINT_SIZE,
67 .bDescriptorType = USB_DT_ENDPOINT,
68
69 .bEndpointAddress = USB_DIR_IN,
70 .bmAttributes = USB_ENDPOINT_XFER_BULK,
71};
72
David Brownell7e75bc02008-08-18 17:41:31 -070073static struct usb_endpoint_descriptor fs_loop_sink_desc = {
David Brownelle5760fd2008-06-19 17:55:35 -070074 .bLength = USB_DT_ENDPOINT_SIZE,
75 .bDescriptorType = USB_DT_ENDPOINT,
76
77 .bEndpointAddress = USB_DIR_OUT,
78 .bmAttributes = USB_ENDPOINT_XFER_BULK,
79};
80
81static struct usb_descriptor_header *fs_loopback_descs[] = {
82 (struct usb_descriptor_header *) &loopback_intf,
David Brownell7e75bc02008-08-18 17:41:31 -070083 (struct usb_descriptor_header *) &fs_loop_sink_desc,
84 (struct usb_descriptor_header *) &fs_loop_source_desc,
David Brownelle5760fd2008-06-19 17:55:35 -070085 NULL,
86};
87
88/* high speed support: */
89
David Brownell7e75bc02008-08-18 17:41:31 -070090static struct usb_endpoint_descriptor hs_loop_source_desc = {
David Brownelle5760fd2008-06-19 17:55:35 -070091 .bLength = USB_DT_ENDPOINT_SIZE,
92 .bDescriptorType = USB_DT_ENDPOINT,
93
94 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -080095 .wMaxPacketSize = cpu_to_le16(512),
David Brownelle5760fd2008-06-19 17:55:35 -070096};
97
David Brownell7e75bc02008-08-18 17:41:31 -070098static struct usb_endpoint_descriptor hs_loop_sink_desc = {
David Brownelle5760fd2008-06-19 17:55:35 -070099 .bLength = USB_DT_ENDPOINT_SIZE,
100 .bDescriptorType = USB_DT_ENDPOINT,
101
102 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800103 .wMaxPacketSize = cpu_to_le16(512),
David Brownelle5760fd2008-06-19 17:55:35 -0700104};
105
106static struct usb_descriptor_header *hs_loopback_descs[] = {
107 (struct usb_descriptor_header *) &loopback_intf,
David Brownell7e75bc02008-08-18 17:41:31 -0700108 (struct usb_descriptor_header *) &hs_loop_source_desc,
109 (struct usb_descriptor_header *) &hs_loop_sink_desc,
David Brownelle5760fd2008-06-19 17:55:35 -0700110 NULL,
111};
112
Amit Blay57c97c02011-07-03 17:29:31 +0300113/* super speed support: */
114
115static struct usb_endpoint_descriptor ss_loop_source_desc = {
116 .bLength = USB_DT_ENDPOINT_SIZE,
117 .bDescriptorType = USB_DT_ENDPOINT,
118
119 .bmAttributes = USB_ENDPOINT_XFER_BULK,
120 .wMaxPacketSize = cpu_to_le16(1024),
121};
122
123struct usb_ss_ep_comp_descriptor ss_loop_source_comp_desc = {
124 .bLength = USB_DT_SS_EP_COMP_SIZE,
125 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
126 .bMaxBurst = 0,
127 .bmAttributes = 0,
128 .wBytesPerInterval = 0,
129};
130
131static struct usb_endpoint_descriptor ss_loop_sink_desc = {
132 .bLength = USB_DT_ENDPOINT_SIZE,
133 .bDescriptorType = USB_DT_ENDPOINT,
134
135 .bmAttributes = USB_ENDPOINT_XFER_BULK,
136 .wMaxPacketSize = cpu_to_le16(1024),
137};
138
139struct usb_ss_ep_comp_descriptor ss_loop_sink_comp_desc = {
140 .bLength = USB_DT_SS_EP_COMP_SIZE,
141 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
142 .bMaxBurst = 0,
143 .bmAttributes = 0,
144 .wBytesPerInterval = 0,
145};
146
147static struct usb_descriptor_header *ss_loopback_descs[] = {
148 (struct usb_descriptor_header *) &loopback_intf,
149 (struct usb_descriptor_header *) &ss_loop_source_desc,
150 (struct usb_descriptor_header *) &ss_loop_source_comp_desc,
151 (struct usb_descriptor_header *) &ss_loop_sink_desc,
152 (struct usb_descriptor_header *) &ss_loop_sink_comp_desc,
153 NULL,
154};
155
David Brownelle5760fd2008-06-19 17:55:35 -0700156/* function-specific strings: */
157
158static struct usb_string strings_loopback[] = {
159 [0].s = "loop input to output",
160 { } /* end of list */
161};
162
163static struct usb_gadget_strings stringtab_loop = {
164 .language = 0x0409, /* en-us */
165 .strings = strings_loopback,
166};
167
168static struct usb_gadget_strings *loopback_strings[] = {
169 &stringtab_loop,
170 NULL,
171};
172
173/*-------------------------------------------------------------------------*/
174
Sebastian Andrzej Siewiorcf9a08a2012-12-23 21:10:01 +0100175static int loopback_bind(struct usb_configuration *c, struct usb_function *f)
David Brownelle5760fd2008-06-19 17:55:35 -0700176{
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
Sebastian Andrzej Siewiorcf9a08a2012-12-23 21:10:01 +0100232static void lb_free_func(struct usb_function *f)
David Brownelle5760fd2008-06-19 17:55:35 -0700233{
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200234 usb_free_all_descriptors(f);
David Brownelle5760fd2008-06-19 17:55:35 -0700235 kfree(func_to_loop(f));
236}
237
238static void loopback_complete(struct usb_ep *ep, struct usb_request *req)
239{
240 struct f_loopback *loop = ep->driver_data;
241 struct usb_composite_dev *cdev = loop->function.config->cdev;
242 int status = req->status;
243
244 switch (status) {
245
246 case 0: /* normal completion? */
247 if (ep == loop->out_ep) {
248 /* loop this OUT packet back IN to the host */
249 req->zero = (req->actual < req->length);
250 req->length = req->actual;
251 status = usb_ep_queue(loop->in_ep, req, GFP_ATOMIC);
252 if (status == 0)
253 return;
254
255 /* "should never get here" */
256 ERROR(cdev, "can't loop %s to %s: %d\n",
257 ep->name, loop->in_ep->name,
258 status);
259 }
260
261 /* queue the buffer for some later OUT packet */
262 req->length = buflen;
263 status = usb_ep_queue(loop->out_ep, req, GFP_ATOMIC);
264 if (status == 0)
265 return;
266
267 /* "should never get here" */
268 /* FALLTHROUGH */
269
270 default:
271 ERROR(cdev, "%s loop complete --> %d, %d/%d\n", ep->name,
272 status, req->actual, req->length);
273 /* FALLTHROUGH */
274
275 /* NOTE: since this driver doesn't maintain an explicit record
276 * of requests it submitted (just maintains qlen count), we
277 * rely on the hardware driver to clean up on disconnect or
278 * endpoint disable.
279 */
280 case -ECONNABORTED: /* hardware forced ep reset */
281 case -ECONNRESET: /* request dequeued */
282 case -ESHUTDOWN: /* disconnect from host */
283 free_ep_req(ep, req);
284 return;
285 }
286}
287
288static void disable_loopback(struct f_loopback *loop)
289{
290 struct usb_composite_dev *cdev;
291
292 cdev = loop->function.config->cdev;
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700293 disable_endpoints(cdev, loop->in_ep, loop->out_ep, NULL, NULL);
David Brownelle5760fd2008-06-19 17:55:35 -0700294 VDBG(cdev, "%s disabled\n", loop->function.name);
295}
296
Andrzej Pietrasiewicz1efd54e2013-11-07 08:41:26 +0100297static inline struct usb_request *lb_alloc_ep_req(struct usb_ep *ep, int len)
298{
299 return alloc_ep_req(ep, len, buflen);
300}
301
David Brownelle5760fd2008-06-19 17:55:35 -0700302static int
303enable_loopback(struct usb_composite_dev *cdev, struct f_loopback *loop)
304{
305 int result = 0;
David Brownelle5760fd2008-06-19 17:55:35 -0700306 struct usb_ep *ep;
307 struct usb_request *req;
308 unsigned i;
309
David Brownelle5760fd2008-06-19 17:55:35 -0700310 /* one endpoint writes data back IN to the host */
311 ep = loop->in_ep;
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300312 result = config_ep_by_speed(cdev->gadget, &(loop->function), ep);
313 if (result)
314 return result;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300315 result = usb_ep_enable(ep);
David Brownelle5760fd2008-06-19 17:55:35 -0700316 if (result < 0)
317 return result;
318 ep->driver_data = loop;
319
320 /* one endpoint just reads OUT packets */
321 ep = loop->out_ep;
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300322 result = config_ep_by_speed(cdev->gadget, &(loop->function), ep);
323 if (result)
324 goto fail0;
325
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300326 result = usb_ep_enable(ep);
David Brownelle5760fd2008-06-19 17:55:35 -0700327 if (result < 0) {
328fail0:
329 ep = loop->in_ep;
330 usb_ep_disable(ep);
331 ep->driver_data = NULL;
332 return result;
333 }
334 ep->driver_data = loop;
335
336 /* allocate a bunch of read buffers and queue them all at once.
337 * we buffer at most 'qlen' transfers; fewer if any need more
338 * than 'buflen' bytes each.
339 */
340 for (i = 0; i < qlen && result == 0; i++) {
Andrzej Pietrasiewicz1efd54e2013-11-07 08:41:26 +0100341 req = lb_alloc_ep_req(ep, 0);
David Brownelle5760fd2008-06-19 17:55:35 -0700342 if (req) {
343 req->complete = loopback_complete;
344 result = usb_ep_queue(ep, req, GFP_ATOMIC);
345 if (result)
346 ERROR(cdev, "%s queue req --> %d\n",
347 ep->name, result);
348 } else {
349 usb_ep_disable(ep);
350 ep->driver_data = NULL;
351 result = -ENOMEM;
352 goto fail0;
353 }
354 }
355
356 DBG(cdev, "%s enabled\n", loop->function.name);
357 return result;
358}
359
360static int loopback_set_alt(struct usb_function *f,
361 unsigned intf, unsigned alt)
362{
363 struct f_loopback *loop = func_to_loop(f);
364 struct usb_composite_dev *cdev = f->config->cdev;
365
366 /* we know alt is zero */
367 if (loop->in_ep->driver_data)
368 disable_loopback(loop);
369 return enable_loopback(cdev, loop);
370}
371
372static void loopback_disable(struct usb_function *f)
373{
374 struct f_loopback *loop = func_to_loop(f);
375
376 disable_loopback(loop);
377}
378
Sebastian Andrzej Siewiorcf9a08a2012-12-23 21:10:01 +0100379static struct usb_function *loopback_alloc(struct usb_function_instance *fi)
David Brownelle5760fd2008-06-19 17:55:35 -0700380{
381 struct f_loopback *loop;
Sebastian Andrzej Siewiorcf9a08a2012-12-23 21:10:01 +0100382 struct f_lb_opts *lb_opts;
David Brownelle5760fd2008-06-19 17:55:35 -0700383
384 loop = kzalloc(sizeof *loop, GFP_KERNEL);
385 if (!loop)
Sebastian Andrzej Siewiorcf9a08a2012-12-23 21:10:01 +0100386 return ERR_PTR(-ENOMEM);
387
388 lb_opts = container_of(fi, struct f_lb_opts, func_inst);
389 buflen = lb_opts->bulk_buflen;
390 qlen = lb_opts->qlen;
391 if (!qlen)
392 qlen = 32;
David Brownelle5760fd2008-06-19 17:55:35 -0700393
394 loop->function.name = "loopback";
David Brownelle5760fd2008-06-19 17:55:35 -0700395 loop->function.bind = loopback_bind;
David Brownelle5760fd2008-06-19 17:55:35 -0700396 loop->function.set_alt = loopback_set_alt;
397 loop->function.disable = loopback_disable;
Sebastian Andrzej Siewiorcf9a08a2012-12-23 21:10:01 +0100398 loop->function.strings = loopback_strings;
David Brownelle5760fd2008-06-19 17:55:35 -0700399
Sebastian Andrzej Siewiorcf9a08a2012-12-23 21:10:01 +0100400 loop->function.free_func = lb_free_func;
401
402 return &loop->function;
David Brownelle5760fd2008-06-19 17:55:35 -0700403}
Sebastian Andrzej Siewiorcf9a08a2012-12-23 21:10:01 +0100404
405static void lb_free_instance(struct usb_function_instance *fi)
406{
407 struct f_lb_opts *lb_opts;
408
409 lb_opts = container_of(fi, struct f_lb_opts, func_inst);
410 kfree(lb_opts);
411}
412
413static struct usb_function_instance *loopback_alloc_instance(void)
414{
415 struct f_lb_opts *lb_opts;
416
417 lb_opts = kzalloc(sizeof(*lb_opts), GFP_KERNEL);
418 if (!lb_opts)
419 return ERR_PTR(-ENOMEM);
420 lb_opts->func_inst.free_func_inst = lb_free_instance;
421 return &lb_opts->func_inst;
422}
423DECLARE_USB_FUNCTION(Loopback, loopback_alloc_instance, loopback_alloc);
424
425int __init lb_modinit(void)
426{
427 int ret;
428
429 ret = usb_function_register(&Loopbackusb_func);
430 if (ret)
431 return ret;
432 return ret;
433}
434void __exit lb_modexit(void)
435{
436 usb_function_unregister(&Loopbackusb_func);
437}
438
439MODULE_LICENSE("GPL");