]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/usb/gadget/function/f_loopback.c
usb: gadget: loopback: don't queue requests to bogus endpoints
[mirror_ubuntu-bionic-kernel.git] / drivers / usb / gadget / function / f_loopback.c
CommitLineData
e5760fda
DB
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.
e5760fda
DB
11 */
12
13/* #define VERBOSE_DEBUG */
14
5a0e3ad6 15#include <linux/slab.h>
e5760fda 16#include <linux/kernel.h>
e5760fda 17#include <linux/device.h>
cf9a08ae
SAS
18#include <linux/module.h>
19#include <linux/err.h>
20#include <linux/usb/composite.h>
e5760fda
DB
21
22#include "g_zero.h"
1efd54ea 23#include "u_f.h"
e5760fda
DB
24
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
cf9a08ae
SAS
49static unsigned qlen;
50static unsigned buflen;
e5760fda
DB
51
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
7e75bc0f 65static struct usb_endpoint_descriptor fs_loop_source_desc = {
e5760fda
DB
66 .bLength = USB_DT_ENDPOINT_SIZE,
67 .bDescriptorType = USB_DT_ENDPOINT,
68
69 .bEndpointAddress = USB_DIR_IN,
70 .bmAttributes = USB_ENDPOINT_XFER_BULK,
71};
72
7e75bc0f 73static struct usb_endpoint_descriptor fs_loop_sink_desc = {
e5760fda
DB
74 .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,
7e75bc0f
DB
83 (struct usb_descriptor_header *) &fs_loop_sink_desc,
84 (struct usb_descriptor_header *) &fs_loop_source_desc,
e5760fda
DB
85 NULL,
86};
87
88/* high speed support: */
89
7e75bc0f 90static struct usb_endpoint_descriptor hs_loop_source_desc = {
e5760fda
DB
91 .bLength = USB_DT_ENDPOINT_SIZE,
92 .bDescriptorType = USB_DT_ENDPOINT,
93
94 .bmAttributes = USB_ENDPOINT_XFER_BULK,
551509d2 95 .wMaxPacketSize = cpu_to_le16(512),
e5760fda
DB
96};
97
7e75bc0f 98static struct usb_endpoint_descriptor hs_loop_sink_desc = {
e5760fda
DB
99 .bLength = USB_DT_ENDPOINT_SIZE,
100 .bDescriptorType = USB_DT_ENDPOINT,
101
102 .bmAttributes = USB_ENDPOINT_XFER_BULK,
551509d2 103 .wMaxPacketSize = cpu_to_le16(512),
e5760fda
DB
104};
105
106static struct usb_descriptor_header *hs_loopback_descs[] = {
107 (struct usb_descriptor_header *) &loopback_intf,
7e75bc0f
DB
108 (struct usb_descriptor_header *) &hs_loop_source_desc,
109 (struct usb_descriptor_header *) &hs_loop_sink_desc,
e5760fda
DB
110 NULL,
111};
112
57c97c02
AB
113/* 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
8002418d 123static struct usb_ss_ep_comp_descriptor ss_loop_source_comp_desc = {
57c97c02
AB
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
8002418d 139static struct usb_ss_ep_comp_descriptor ss_loop_sink_comp_desc = {
57c97c02
AB
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
e5760fda
DB
156/* 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
cf9a08ae 175static int loopback_bind(struct usb_configuration *c, struct usb_function *f)
e5760fda
DB
176{
177 struct usb_composite_dev *cdev = c->cdev;
178 struct f_loopback *loop = func_to_loop(f);
179 int id;
10287bae 180 int ret;
e5760fda
DB
181
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
78f46f09
SAS
188 id = usb_string_id(cdev);
189 if (id < 0)
190 return id;
191 strings_loopback[0].id = id;
192 loopback_intf.iInterface = id;
193
e5760fda
DB
194 /* allocate endpoints */
195
7e75bc0f 196 loop->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_loop_source_desc);
e5760fda
DB
197 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
7e75bc0f 205 loop->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_loop_sink_desc);
e5760fda
DB
206 if (!loop->out_ep)
207 goto autoconf_fail;
208 loop->out_ep->driver_data = cdev; /* claim */
209
210 /* support high speed hardware */
10287bae
SAS
211 hs_loop_source_desc.bEndpointAddress =
212 fs_loop_source_desc.bEndpointAddress;
213 hs_loop_sink_desc.bEndpointAddress = fs_loop_sink_desc.bEndpointAddress;
e5760fda 214
57c97c02 215 /* support super speed hardware */
10287bae
SAS
216 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;
57c97c02 224
e5760fda 225 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
57c97c02
AB
226 (gadget_is_superspeed(c->cdev->gadget) ? "super" :
227 (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")),
e5760fda
DB
228 f->name, loop->in_ep->name, loop->out_ep->name);
229 return 0;
230}
231
cf9a08ae 232static void lb_free_func(struct usb_function *f)
e5760fda 233{
c0501f47
AP
234 struct f_lb_opts *opts;
235
236 opts = container_of(f->fi, struct f_lb_opts, func_inst);
237
238 mutex_lock(&opts->lock);
239 opts->refcnt--;
240 mutex_unlock(&opts->lock);
241
10287bae 242 usb_free_all_descriptors(f);
e5760fda
DB
243 kfree(func_to_loop(f));
244}
245
246static void loopback_complete(struct usb_ep *ep, struct usb_request *req)
247{
248 struct f_loopback *loop = ep->driver_data;
249 struct usb_composite_dev *cdev = loop->function.config->cdev;
250 int status = req->status;
251
252 switch (status) {
253
254 case 0: /* normal completion? */
255 if (ep == loop->out_ep) {
e5760fda
DB
256 req->zero = (req->actual < req->length);
257 req->length = req->actual;
e5760fda
DB
258 }
259
260 /* queue the buffer for some later OUT packet */
261 req->length = buflen;
e0857ce5 262 status = usb_ep_queue(ep, req, GFP_ATOMIC);
e5760fda
DB
263 if (status == 0)
264 return;
265
266 /* "should never get here" */
267 /* FALLTHROUGH */
268
269 default:
270 ERROR(cdev, "%s loop complete --> %d, %d/%d\n", ep->name,
271 status, req->actual, req->length);
272 /* FALLTHROUGH */
273
274 /* NOTE: since this driver doesn't maintain an explicit record
275 * of requests it submitted (just maintains qlen count), we
276 * rely on the hardware driver to clean up on disconnect or
277 * endpoint disable.
278 */
279 case -ECONNABORTED: /* hardware forced ep reset */
280 case -ECONNRESET: /* request dequeued */
281 case -ESHUTDOWN: /* disconnect from host */
282 free_ep_req(ep, req);
283 return;
284 }
285}
286
287static void disable_loopback(struct f_loopback *loop)
288{
289 struct usb_composite_dev *cdev;
290
291 cdev = loop->function.config->cdev;
ef11982d
AV
292 disable_endpoints(cdev, loop->in_ep, loop->out_ep, NULL, NULL, NULL,
293 NULL);
e5760fda
DB
294 VDBG(cdev, "%s disabled\n", loop->function.name);
295}
296
1efd54ea
AP
297static 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
e0857ce5
FB
302static int enable_endpoint(struct usb_composite_dev *cdev, struct f_loopback *loop,
303 struct usb_ep *ep)
e5760fda 304{
e5760fda
DB
305 struct usb_request *req;
306 unsigned i;
e0857ce5 307 int result;
e5760fda 308
e0857ce5
FB
309 /*
310 * one endpoint writes data back IN to the host while another endpoint
311 * just reads OUT packets
312 */
ea2a1df7
TB
313 result = config_ep_by_speed(cdev->gadget, &(loop->function), ep);
314 if (result)
e0857ce5 315 goto fail0;
72c973dd 316 result = usb_ep_enable(ep);
e5760fda 317 if (result < 0)
ea2a1df7 318 goto fail0;
e5760fda
DB
319 ep->driver_data = loop;
320
e0857ce5
FB
321 /*
322 * allocate a bunch of read buffers and queue them all at once.
e5760fda
DB
323 * we buffer at most 'qlen' transfers; fewer if any need more
324 * than 'buflen' bytes each.
325 */
326 for (i = 0; i < qlen && result == 0; i++) {
1efd54ea 327 req = lb_alloc_ep_req(ep, 0);
e0857ce5
FB
328 if (!req)
329 goto fail1;
330
331 req->complete = loopback_complete;
332 result = usb_ep_queue(ep, req, GFP_ATOMIC);
333 if (result) {
334 ERROR(cdev, "%s queue req --> %d\n",
335 ep->name, result);
336 goto fail1;
e5760fda
DB
337 }
338 }
339
e0857ce5
FB
340 return 0;
341
342fail1:
343 usb_ep_disable(ep);
344
345fail0:
346 return result;
347}
348
349static int
350enable_loopback(struct usb_composite_dev *cdev, struct f_loopback *loop)
351{
352 int result = 0;
353
354 result = enable_endpoint(cdev, loop, loop->in_ep);
355 if (result)
356 return result;
357
358 result = enable_endpoint(cdev, loop, loop->out_ep);
359 if (result)
360 return result;
361
e5760fda
DB
362 DBG(cdev, "%s enabled\n", loop->function.name);
363 return result;
364}
365
366static int loopback_set_alt(struct usb_function *f,
367 unsigned intf, unsigned alt)
368{
369 struct f_loopback *loop = func_to_loop(f);
370 struct usb_composite_dev *cdev = f->config->cdev;
371
372 /* we know alt is zero */
373 if (loop->in_ep->driver_data)
374 disable_loopback(loop);
375 return enable_loopback(cdev, loop);
376}
377
378static void loopback_disable(struct usb_function *f)
379{
380 struct f_loopback *loop = func_to_loop(f);
381
382 disable_loopback(loop);
383}
384
cf9a08ae 385static struct usb_function *loopback_alloc(struct usb_function_instance *fi)
e5760fda
DB
386{
387 struct f_loopback *loop;
cf9a08ae 388 struct f_lb_opts *lb_opts;
e5760fda
DB
389
390 loop = kzalloc(sizeof *loop, GFP_KERNEL);
391 if (!loop)
cf9a08ae
SAS
392 return ERR_PTR(-ENOMEM);
393
394 lb_opts = container_of(fi, struct f_lb_opts, func_inst);
c0501f47
AP
395
396 mutex_lock(&lb_opts->lock);
397 lb_opts->refcnt++;
398 mutex_unlock(&lb_opts->lock);
399
cf9a08ae
SAS
400 buflen = lb_opts->bulk_buflen;
401 qlen = lb_opts->qlen;
402 if (!qlen)
403 qlen = 32;
e5760fda
DB
404
405 loop->function.name = "loopback";
e5760fda 406 loop->function.bind = loopback_bind;
e5760fda
DB
407 loop->function.set_alt = loopback_set_alt;
408 loop->function.disable = loopback_disable;
cf9a08ae
SAS
409 loop->function.strings = loopback_strings;
410
411 loop->function.free_func = lb_free_func;
412
413 return &loop->function;
414}
415
c0501f47
AP
416static inline struct f_lb_opts *to_f_lb_opts(struct config_item *item)
417{
418 return container_of(to_config_group(item), struct f_lb_opts,
419 func_inst.group);
420}
421
422CONFIGFS_ATTR_STRUCT(f_lb_opts);
423CONFIGFS_ATTR_OPS(f_lb_opts);
424
425static void lb_attr_release(struct config_item *item)
426{
427 struct f_lb_opts *lb_opts = to_f_lb_opts(item);
428
429 usb_put_function_instance(&lb_opts->func_inst);
430}
431
432static struct configfs_item_operations lb_item_ops = {
433 .release = lb_attr_release,
434 .show_attribute = f_lb_opts_attr_show,
435 .store_attribute = f_lb_opts_attr_store,
436};
437
438static ssize_t f_lb_opts_qlen_show(struct f_lb_opts *opts, char *page)
439{
440 int result;
441
442 mutex_lock(&opts->lock);
443 result = sprintf(page, "%d", opts->qlen);
444 mutex_unlock(&opts->lock);
445
446 return result;
447}
448
449static ssize_t f_lb_opts_qlen_store(struct f_lb_opts *opts,
450 const char *page, size_t len)
451{
452 int ret;
453 u32 num;
454
455 mutex_lock(&opts->lock);
456 if (opts->refcnt) {
457 ret = -EBUSY;
458 goto end;
459 }
460
461 ret = kstrtou32(page, 0, &num);
462 if (ret)
463 goto end;
464
465 opts->qlen = num;
466 ret = len;
467end:
468 mutex_unlock(&opts->lock);
469 return ret;
470}
471
472static struct f_lb_opts_attribute f_lb_opts_qlen =
473 __CONFIGFS_ATTR(qlen, S_IRUGO | S_IWUSR,
474 f_lb_opts_qlen_show,
475 f_lb_opts_qlen_store);
476
477static ssize_t f_lb_opts_bulk_buflen_show(struct f_lb_opts *opts, char *page)
478{
479 int result;
480
481 mutex_lock(&opts->lock);
482 result = sprintf(page, "%d", opts->bulk_buflen);
483 mutex_unlock(&opts->lock);
484
485 return result;
486}
487
488static ssize_t f_lb_opts_bulk_buflen_store(struct f_lb_opts *opts,
489 const char *page, size_t len)
490{
491 int ret;
492 u32 num;
493
494 mutex_lock(&opts->lock);
495 if (opts->refcnt) {
496 ret = -EBUSY;
497 goto end;
498 }
499
500 ret = kstrtou32(page, 0, &num);
501 if (ret)
502 goto end;
503
504 opts->bulk_buflen = num;
505 ret = len;
506end:
507 mutex_unlock(&opts->lock);
508 return ret;
509}
510
511static struct f_lb_opts_attribute f_lb_opts_bulk_buflen =
512 __CONFIGFS_ATTR(buflen, S_IRUGO | S_IWUSR,
513 f_lb_opts_bulk_buflen_show,
514 f_lb_opts_bulk_buflen_store);
515
516static struct configfs_attribute *lb_attrs[] = {
517 &f_lb_opts_qlen.attr,
518 &f_lb_opts_bulk_buflen.attr,
519 NULL,
520};
521
522static struct config_item_type lb_func_type = {
523 .ct_item_ops = &lb_item_ops,
524 .ct_attrs = lb_attrs,
525 .ct_owner = THIS_MODULE,
526};
527
cf9a08ae
SAS
528static void lb_free_instance(struct usb_function_instance *fi)
529{
530 struct f_lb_opts *lb_opts;
e5760fda 531
cf9a08ae
SAS
532 lb_opts = container_of(fi, struct f_lb_opts, func_inst);
533 kfree(lb_opts);
e5760fda 534}
cf9a08ae
SAS
535
536static struct usb_function_instance *loopback_alloc_instance(void)
537{
538 struct f_lb_opts *lb_opts;
539
540 lb_opts = kzalloc(sizeof(*lb_opts), GFP_KERNEL);
541 if (!lb_opts)
542 return ERR_PTR(-ENOMEM);
c0501f47 543 mutex_init(&lb_opts->lock);
cf9a08ae 544 lb_opts->func_inst.free_func_inst = lb_free_instance;
c0501f47
AP
545 lb_opts->bulk_buflen = GZERO_BULK_BUFLEN;
546 lb_opts->qlen = GZERO_QLEN;
547
548 config_group_init_type_name(&lb_opts->func_inst.group, "",
549 &lb_func_type);
550
cf9a08ae
SAS
551 return &lb_opts->func_inst;
552}
553DECLARE_USB_FUNCTION(Loopback, loopback_alloc_instance, loopback_alloc);
554
555int __init lb_modinit(void)
556{
557 int ret;
558
559 ret = usb_function_register(&Loopbackusb_func);
560 if (ret)
561 return ret;
562 return ret;
563}
564void __exit lb_modexit(void)
565{
566 usb_function_unregister(&Loopbackusb_func);
567}
568
569MODULE_LICENSE("GPL");