2017-11-03 18:28:30 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2008-06-20 08:55:35 +08:00
|
|
|
/*
|
|
|
|
* f_loopback.c - USB peripheral loopback configuration driver
|
|
|
|
*
|
|
|
|
* Copyright (C) 2003-2008 David Brownell
|
|
|
|
* Copyright (C) 2008 by Nokia Corporation
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* #define VERBOSE_DEBUG */
|
|
|
|
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 16:04:11 +08:00
|
|
|
#include <linux/slab.h>
|
2008-06-20 08:55:35 +08:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/device.h>
|
2012-12-24 04:10:01 +08:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/usb/composite.h>
|
2008-06-20 08:55:35 +08:00
|
|
|
|
|
|
|
#include "g_zero.h"
|
2013-11-07 15:41:26 +08:00
|
|
|
#include "u_f.h"
|
2008-06-20 08:55:35 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* LOOPBACK FUNCTION ... a testing vehicle for USB peripherals,
|
|
|
|
*
|
|
|
|
* This takes messages of various sizes written OUT to a device, and loops
|
|
|
|
* them back so they can be read IN from it. It has been used by certain
|
|
|
|
* test applications. It supports limited testing of data queueing logic.
|
|
|
|
*/
|
|
|
|
struct f_loopback {
|
|
|
|
struct usb_function function;
|
|
|
|
|
|
|
|
struct usb_ep *in_ep;
|
|
|
|
struct usb_ep *out_ep;
|
2015-10-15 05:03:34 +08:00
|
|
|
|
|
|
|
unsigned qlen;
|
|
|
|
unsigned buflen;
|
2008-06-20 08:55:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static inline struct f_loopback *func_to_loop(struct usb_function *f)
|
|
|
|
{
|
|
|
|
return container_of(f, struct f_loopback, function);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
static struct usb_interface_descriptor loopback_intf = {
|
2015-10-15 05:03:34 +08:00
|
|
|
.bLength = sizeof(loopback_intf),
|
2008-06-20 08:55:35 +08:00
|
|
|
.bDescriptorType = USB_DT_INTERFACE,
|
|
|
|
|
|
|
|
.bNumEndpoints = 2,
|
|
|
|
.bInterfaceClass = USB_CLASS_VENDOR_SPEC,
|
|
|
|
/* .iInterface = DYNAMIC */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* full speed support: */
|
|
|
|
|
2008-08-19 08:41:31 +08:00
|
|
|
static struct usb_endpoint_descriptor fs_loop_source_desc = {
|
2008-06-20 08:55:35 +08:00
|
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
|
|
|
|
.bEndpointAddress = USB_DIR_IN,
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
|
|
};
|
|
|
|
|
2008-08-19 08:41:31 +08:00
|
|
|
static struct usb_endpoint_descriptor fs_loop_sink_desc = {
|
2008-06-20 08:55:35 +08:00
|
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
|
|
|
|
.bEndpointAddress = USB_DIR_OUT,
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct usb_descriptor_header *fs_loopback_descs[] = {
|
|
|
|
(struct usb_descriptor_header *) &loopback_intf,
|
2008-08-19 08:41:31 +08:00
|
|
|
(struct usb_descriptor_header *) &fs_loop_sink_desc,
|
|
|
|
(struct usb_descriptor_header *) &fs_loop_source_desc,
|
2008-06-20 08:55:35 +08:00
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* high speed support: */
|
|
|
|
|
2008-08-19 08:41:31 +08:00
|
|
|
static struct usb_endpoint_descriptor hs_loop_source_desc = {
|
2008-06-20 08:55:35 +08:00
|
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
2009-02-12 06:11:36 +08:00
|
|
|
.wMaxPacketSize = cpu_to_le16(512),
|
2008-06-20 08:55:35 +08:00
|
|
|
};
|
|
|
|
|
2008-08-19 08:41:31 +08:00
|
|
|
static struct usb_endpoint_descriptor hs_loop_sink_desc = {
|
2008-06-20 08:55:35 +08:00
|
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
2009-02-12 06:11:36 +08:00
|
|
|
.wMaxPacketSize = cpu_to_le16(512),
|
2008-06-20 08:55:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct usb_descriptor_header *hs_loopback_descs[] = {
|
|
|
|
(struct usb_descriptor_header *) &loopback_intf,
|
2008-08-19 08:41:31 +08:00
|
|
|
(struct usb_descriptor_header *) &hs_loop_source_desc,
|
|
|
|
(struct usb_descriptor_header *) &hs_loop_sink_desc,
|
2008-06-20 08:55:35 +08:00
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2011-07-03 22:29:31 +08:00
|
|
|
/* super speed support: */
|
|
|
|
|
|
|
|
static struct usb_endpoint_descriptor ss_loop_source_desc = {
|
|
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
|
|
.wMaxPacketSize = cpu_to_le16(1024),
|
|
|
|
};
|
|
|
|
|
2013-12-16 17:40:49 +08:00
|
|
|
static struct usb_ss_ep_comp_descriptor ss_loop_source_comp_desc = {
|
2011-07-03 22:29:31 +08:00
|
|
|
.bLength = USB_DT_SS_EP_COMP_SIZE,
|
|
|
|
.bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
|
|
|
|
.bMaxBurst = 0,
|
|
|
|
.bmAttributes = 0,
|
|
|
|
.wBytesPerInterval = 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct usb_endpoint_descriptor ss_loop_sink_desc = {
|
|
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
|
|
.wMaxPacketSize = cpu_to_le16(1024),
|
|
|
|
};
|
|
|
|
|
2013-12-16 17:40:49 +08:00
|
|
|
static struct usb_ss_ep_comp_descriptor ss_loop_sink_comp_desc = {
|
2011-07-03 22:29:31 +08:00
|
|
|
.bLength = USB_DT_SS_EP_COMP_SIZE,
|
|
|
|
.bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
|
|
|
|
.bMaxBurst = 0,
|
|
|
|
.bmAttributes = 0,
|
|
|
|
.wBytesPerInterval = 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct usb_descriptor_header *ss_loopback_descs[] = {
|
|
|
|
(struct usb_descriptor_header *) &loopback_intf,
|
|
|
|
(struct usb_descriptor_header *) &ss_loop_source_desc,
|
|
|
|
(struct usb_descriptor_header *) &ss_loop_source_comp_desc,
|
|
|
|
(struct usb_descriptor_header *) &ss_loop_sink_desc,
|
|
|
|
(struct usb_descriptor_header *) &ss_loop_sink_comp_desc,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2008-06-20 08:55:35 +08:00
|
|
|
/* function-specific strings: */
|
|
|
|
|
|
|
|
static struct usb_string strings_loopback[] = {
|
|
|
|
[0].s = "loop input to output",
|
|
|
|
{ } /* end of list */
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct usb_gadget_strings stringtab_loop = {
|
|
|
|
.language = 0x0409, /* en-us */
|
|
|
|
.strings = strings_loopback,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct usb_gadget_strings *loopback_strings[] = {
|
|
|
|
&stringtab_loop,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
2012-12-24 04:10:01 +08:00
|
|
|
static int loopback_bind(struct usb_configuration *c, struct usb_function *f)
|
2008-06-20 08:55:35 +08:00
|
|
|
{
|
|
|
|
struct usb_composite_dev *cdev = c->cdev;
|
|
|
|
struct f_loopback *loop = func_to_loop(f);
|
|
|
|
int id;
|
2012-10-23 04:15:06 +08:00
|
|
|
int ret;
|
2008-06-20 08:55:35 +08:00
|
|
|
|
|
|
|
/* allocate interface ID(s) */
|
|
|
|
id = usb_interface_id(c, f);
|
|
|
|
if (id < 0)
|
|
|
|
return id;
|
|
|
|
loopback_intf.bInterfaceNumber = id;
|
|
|
|
|
2012-12-24 04:09:59 +08:00
|
|
|
id = usb_string_id(cdev);
|
|
|
|
if (id < 0)
|
|
|
|
return id;
|
|
|
|
strings_loopback[0].id = id;
|
|
|
|
loopback_intf.iInterface = id;
|
|
|
|
|
2008-06-20 08:55:35 +08:00
|
|
|
/* allocate endpoints */
|
|
|
|
|
2008-08-19 08:41:31 +08:00
|
|
|
loop->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_loop_source_desc);
|
2008-06-20 08:55:35 +08:00
|
|
|
if (!loop->in_ep) {
|
|
|
|
autoconf_fail:
|
|
|
|
ERROR(cdev, "%s: can't autoconfigure on %s\n",
|
|
|
|
f->name, cdev->gadget->name);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2008-08-19 08:41:31 +08:00
|
|
|
loop->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_loop_sink_desc);
|
2008-06-20 08:55:35 +08:00
|
|
|
if (!loop->out_ep)
|
|
|
|
goto autoconf_fail;
|
|
|
|
|
|
|
|
/* support high speed hardware */
|
2012-10-23 04:15:06 +08:00
|
|
|
hs_loop_source_desc.bEndpointAddress =
|
|
|
|
fs_loop_source_desc.bEndpointAddress;
|
|
|
|
hs_loop_sink_desc.bEndpointAddress = fs_loop_sink_desc.bEndpointAddress;
|
2008-06-20 08:55:35 +08:00
|
|
|
|
2011-07-03 22:29:31 +08:00
|
|
|
/* support super speed hardware */
|
2012-10-23 04:15:06 +08:00
|
|
|
ss_loop_source_desc.bEndpointAddress =
|
|
|
|
fs_loop_source_desc.bEndpointAddress;
|
|
|
|
ss_loop_sink_desc.bEndpointAddress = fs_loop_sink_desc.bEndpointAddress;
|
|
|
|
|
|
|
|
ret = usb_assign_descriptors(f, fs_loopback_descs, hs_loopback_descs,
|
2016-02-06 09:06:07 +08:00
|
|
|
ss_loopback_descs, NULL);
|
2012-10-23 04:15:06 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2011-07-03 22:29:31 +08:00
|
|
|
|
2008-06-20 08:55:35 +08:00
|
|
|
DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
|
2011-07-03 22:29:31 +08:00
|
|
|
(gadget_is_superspeed(c->cdev->gadget) ? "super" :
|
|
|
|
(gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")),
|
2008-06-20 08:55:35 +08:00
|
|
|
f->name, loop->in_ep->name, loop->out_ep->name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-24 04:10:01 +08:00
|
|
|
static void lb_free_func(struct usb_function *f)
|
2008-06-20 08:55:35 +08:00
|
|
|
{
|
2013-11-07 15:41:27 +08:00
|
|
|
struct f_lb_opts *opts;
|
|
|
|
|
|
|
|
opts = container_of(f->fi, struct f_lb_opts, func_inst);
|
|
|
|
|
|
|
|
mutex_lock(&opts->lock);
|
|
|
|
opts->refcnt--;
|
|
|
|
mutex_unlock(&opts->lock);
|
|
|
|
|
2012-10-23 04:15:06 +08:00
|
|
|
usb_free_all_descriptors(f);
|
2008-06-20 08:55:35 +08:00
|
|
|
kfree(func_to_loop(f));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void loopback_complete(struct usb_ep *ep, struct usb_request *req)
|
|
|
|
{
|
|
|
|
struct f_loopback *loop = ep->driver_data;
|
|
|
|
struct usb_composite_dev *cdev = loop->function.config->cdev;
|
|
|
|
int status = req->status;
|
|
|
|
|
|
|
|
switch (status) {
|
|
|
|
case 0: /* normal completion? */
|
|
|
|
if (ep == loop->out_ep) {
|
2015-10-15 04:49:40 +08:00
|
|
|
/*
|
|
|
|
* We received some data from the host so let's
|
|
|
|
* queue it so host can read the from our in ep
|
|
|
|
*/
|
|
|
|
struct usb_request *in_req = req->context;
|
|
|
|
|
|
|
|
in_req->zero = (req->actual < req->length);
|
|
|
|
in_req->length = req->actual;
|
|
|
|
ep = loop->in_ep;
|
|
|
|
req = in_req;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* We have just looped back a bunch of data
|
|
|
|
* to host. Now let's wait for some more data.
|
|
|
|
*/
|
|
|
|
req = req->context;
|
|
|
|
ep = loop->out_ep;
|
2008-06-20 08:55:35 +08:00
|
|
|
}
|
|
|
|
|
2015-10-15 04:49:40 +08:00
|
|
|
/* queue the buffer back to host or for next bunch of data */
|
2014-10-14 04:30:52 +08:00
|
|
|
status = usb_ep_queue(ep, req, GFP_ATOMIC);
|
2015-10-15 04:49:40 +08:00
|
|
|
if (status == 0) {
|
2008-06-20 08:55:35 +08:00
|
|
|
return;
|
2015-10-15 04:49:40 +08:00
|
|
|
} else {
|
|
|
|
ERROR(cdev, "Unable to loop back buffer to %s: %d\n",
|
|
|
|
ep->name, status);
|
|
|
|
goto free_req;
|
|
|
|
}
|
2008-06-20 08:55:35 +08:00
|
|
|
|
|
|
|
/* "should never get here" */
|
|
|
|
default:
|
|
|
|
ERROR(cdev, "%s loop complete --> %d, %d/%d\n", ep->name,
|
|
|
|
status, req->actual, req->length);
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
|
|
|
|
/* NOTE: since this driver doesn't maintain an explicit record
|
|
|
|
* of requests it submitted (just maintains qlen count), we
|
|
|
|
* rely on the hardware driver to clean up on disconnect or
|
|
|
|
* endpoint disable.
|
|
|
|
*/
|
|
|
|
case -ECONNABORTED: /* hardware forced ep reset */
|
|
|
|
case -ECONNRESET: /* request dequeued */
|
|
|
|
case -ESHUTDOWN: /* disconnect from host */
|
2015-10-15 04:49:40 +08:00
|
|
|
free_req:
|
|
|
|
usb_ep_free_request(ep == loop->in_ep ?
|
|
|
|
loop->out_ep : loop->in_ep,
|
|
|
|
req->context);
|
2008-06-20 08:55:35 +08:00
|
|
|
free_ep_req(ep, req);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void disable_loopback(struct f_loopback *loop)
|
|
|
|
{
|
|
|
|
struct usb_composite_dev *cdev;
|
|
|
|
|
|
|
|
cdev = loop->function.config->cdev;
|
2015-03-11 23:00:05 +08:00
|
|
|
disable_endpoints(cdev, loop->in_ep, loop->out_ep, NULL, NULL);
|
2008-06-20 08:55:35 +08:00
|
|
|
VDBG(cdev, "%s disabled\n", loop->function.name);
|
|
|
|
}
|
|
|
|
|
2013-11-07 15:41:26 +08:00
|
|
|
static inline struct usb_request *lb_alloc_ep_req(struct usb_ep *ep, int len)
|
|
|
|
{
|
2016-08-24 01:24:49 +08:00
|
|
|
return alloc_ep_req(ep, len);
|
2013-11-07 15:41:26 +08:00
|
|
|
}
|
|
|
|
|
2015-10-15 04:49:40 +08:00
|
|
|
static int alloc_requests(struct usb_composite_dev *cdev,
|
|
|
|
struct f_loopback *loop)
|
2008-06-20 08:55:35 +08:00
|
|
|
{
|
2015-10-15 04:49:40 +08:00
|
|
|
struct usb_request *in_req, *out_req;
|
|
|
|
int i;
|
|
|
|
int result = 0;
|
2008-06-20 08:55:35 +08:00
|
|
|
|
2014-10-14 04:30:52 +08:00
|
|
|
/*
|
|
|
|
* allocate a bunch of read buffers and queue them all at once.
|
2015-10-15 04:49:40 +08:00
|
|
|
* we buffer at most 'qlen' transfers; We allocate buffers only
|
|
|
|
* for out transfer and reuse them in IN transfers to implement
|
|
|
|
* our loopback functionality
|
2008-06-20 08:55:35 +08:00
|
|
|
*/
|
2015-10-15 05:03:34 +08:00
|
|
|
for (i = 0; i < loop->qlen && result == 0; i++) {
|
2015-10-15 04:49:40 +08:00
|
|
|
result = -ENOMEM;
|
|
|
|
|
2015-11-03 15:57:38 +08:00
|
|
|
in_req = usb_ep_alloc_request(loop->in_ep, GFP_ATOMIC);
|
2015-10-15 04:49:40 +08:00
|
|
|
if (!in_req)
|
|
|
|
goto fail;
|
|
|
|
|
2016-08-24 01:24:49 +08:00
|
|
|
out_req = lb_alloc_ep_req(loop->out_ep, loop->buflen);
|
2015-10-15 04:49:40 +08:00
|
|
|
if (!out_req)
|
|
|
|
goto fail_in;
|
|
|
|
|
|
|
|
in_req->complete = loopback_complete;
|
|
|
|
out_req->complete = loopback_complete;
|
|
|
|
|
|
|
|
in_req->buf = out_req->buf;
|
|
|
|
/* length will be set in complete routine */
|
|
|
|
in_req->context = out_req;
|
|
|
|
out_req->context = in_req;
|
2014-10-14 04:30:52 +08:00
|
|
|
|
2015-10-15 04:49:40 +08:00
|
|
|
result = usb_ep_queue(loop->out_ep, out_req, GFP_ATOMIC);
|
2014-10-14 04:30:52 +08:00
|
|
|
if (result) {
|
|
|
|
ERROR(cdev, "%s queue req --> %d\n",
|
2015-10-15 04:49:40 +08:00
|
|
|
loop->out_ep->name, result);
|
|
|
|
goto fail_out;
|
2008-06-20 08:55:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-14 04:30:52 +08:00
|
|
|
return 0;
|
|
|
|
|
2015-10-15 04:49:40 +08:00
|
|
|
fail_out:
|
|
|
|
free_ep_req(loop->out_ep, out_req);
|
|
|
|
fail_in:
|
|
|
|
usb_ep_free_request(loop->in_ep, in_req);
|
|
|
|
fail:
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int enable_endpoint(struct usb_composite_dev *cdev,
|
|
|
|
struct f_loopback *loop, struct usb_ep *ep)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
|
|
|
|
result = config_ep_by_speed(cdev->gadget, &(loop->function), ep);
|
|
|
|
if (result)
|
|
|
|
goto out;
|
2014-10-14 04:30:52 +08:00
|
|
|
|
2015-10-15 04:49:40 +08:00
|
|
|
result = usb_ep_enable(ep);
|
|
|
|
if (result < 0)
|
|
|
|
goto out;
|
|
|
|
ep->driver_data = loop;
|
|
|
|
result = 0;
|
|
|
|
|
|
|
|
out:
|
2014-10-14 04:30:52 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
enable_loopback(struct usb_composite_dev *cdev, struct f_loopback *loop)
|
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
result = enable_endpoint(cdev, loop, loop->in_ep);
|
|
|
|
if (result)
|
2015-10-15 04:49:40 +08:00
|
|
|
goto out;
|
2014-10-14 04:30:52 +08:00
|
|
|
|
|
|
|
result = enable_endpoint(cdev, loop, loop->out_ep);
|
|
|
|
if (result)
|
2015-10-15 04:49:40 +08:00
|
|
|
goto disable_in;
|
|
|
|
|
|
|
|
result = alloc_requests(cdev, loop);
|
|
|
|
if (result)
|
|
|
|
goto disable_out;
|
2014-10-14 04:30:52 +08:00
|
|
|
|
2008-06-20 08:55:35 +08:00
|
|
|
DBG(cdev, "%s enabled\n", loop->function.name);
|
2015-10-15 04:49:40 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
disable_out:
|
|
|
|
usb_ep_disable(loop->out_ep);
|
|
|
|
disable_in:
|
|
|
|
usb_ep_disable(loop->in_ep);
|
|
|
|
out:
|
2008-06-20 08:55:35 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int loopback_set_alt(struct usb_function *f,
|
|
|
|
unsigned intf, unsigned alt)
|
|
|
|
{
|
|
|
|
struct f_loopback *loop = func_to_loop(f);
|
|
|
|
struct usb_composite_dev *cdev = f->config->cdev;
|
|
|
|
|
|
|
|
/* we know alt is zero */
|
2015-09-16 18:10:47 +08:00
|
|
|
disable_loopback(loop);
|
2008-06-20 08:55:35 +08:00
|
|
|
return enable_loopback(cdev, loop);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void loopback_disable(struct usb_function *f)
|
|
|
|
{
|
|
|
|
struct f_loopback *loop = func_to_loop(f);
|
|
|
|
|
|
|
|
disable_loopback(loop);
|
|
|
|
}
|
|
|
|
|
2012-12-24 04:10:01 +08:00
|
|
|
static struct usb_function *loopback_alloc(struct usb_function_instance *fi)
|
2008-06-20 08:55:35 +08:00
|
|
|
{
|
|
|
|
struct f_loopback *loop;
|
2012-12-24 04:10:01 +08:00
|
|
|
struct f_lb_opts *lb_opts;
|
2008-06-20 08:55:35 +08:00
|
|
|
|
|
|
|
loop = kzalloc(sizeof *loop, GFP_KERNEL);
|
|
|
|
if (!loop)
|
2012-12-24 04:10:01 +08:00
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
lb_opts = container_of(fi, struct f_lb_opts, func_inst);
|
2013-11-07 15:41:27 +08:00
|
|
|
|
|
|
|
mutex_lock(&lb_opts->lock);
|
|
|
|
lb_opts->refcnt++;
|
|
|
|
mutex_unlock(&lb_opts->lock);
|
|
|
|
|
2015-10-15 05:03:34 +08:00
|
|
|
loop->buflen = lb_opts->bulk_buflen;
|
|
|
|
loop->qlen = lb_opts->qlen;
|
|
|
|
if (!loop->qlen)
|
|
|
|
loop->qlen = 32;
|
2008-06-20 08:55:35 +08:00
|
|
|
|
|
|
|
loop->function.name = "loopback";
|
|
|
|
loop->function.bind = loopback_bind;
|
|
|
|
loop->function.set_alt = loopback_set_alt;
|
|
|
|
loop->function.disable = loopback_disable;
|
2012-12-24 04:10:01 +08:00
|
|
|
loop->function.strings = loopback_strings;
|
|
|
|
|
|
|
|
loop->function.free_func = lb_free_func;
|
|
|
|
|
|
|
|
return &loop->function;
|
|
|
|
}
|
|
|
|
|
2013-11-07 15:41:27 +08:00
|
|
|
static inline struct f_lb_opts *to_f_lb_opts(struct config_item *item)
|
|
|
|
{
|
|
|
|
return container_of(to_config_group(item), struct f_lb_opts,
|
|
|
|
func_inst.group);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void lb_attr_release(struct config_item *item)
|
|
|
|
{
|
|
|
|
struct f_lb_opts *lb_opts = to_f_lb_opts(item);
|
|
|
|
|
|
|
|
usb_put_function_instance(&lb_opts->func_inst);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct configfs_item_operations lb_item_ops = {
|
|
|
|
.release = lb_attr_release,
|
|
|
|
};
|
|
|
|
|
2015-10-03 21:32:43 +08:00
|
|
|
static ssize_t f_lb_opts_qlen_show(struct config_item *item, char *page)
|
2013-11-07 15:41:27 +08:00
|
|
|
{
|
2015-10-03 21:32:43 +08:00
|
|
|
struct f_lb_opts *opts = to_f_lb_opts(item);
|
2013-11-07 15:41:27 +08:00
|
|
|
int result;
|
|
|
|
|
|
|
|
mutex_lock(&opts->lock);
|
2015-09-22 03:51:13 +08:00
|
|
|
result = sprintf(page, "%d\n", opts->qlen);
|
2013-11-07 15:41:27 +08:00
|
|
|
mutex_unlock(&opts->lock);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-10-03 21:32:43 +08:00
|
|
|
static ssize_t f_lb_opts_qlen_store(struct config_item *item,
|
2013-11-07 15:41:27 +08:00
|
|
|
const char *page, size_t len)
|
|
|
|
{
|
2015-10-03 21:32:43 +08:00
|
|
|
struct f_lb_opts *opts = to_f_lb_opts(item);
|
2013-11-07 15:41:27 +08:00
|
|
|
int ret;
|
|
|
|
u32 num;
|
|
|
|
|
|
|
|
mutex_lock(&opts->lock);
|
|
|
|
if (opts->refcnt) {
|
|
|
|
ret = -EBUSY;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = kstrtou32(page, 0, &num);
|
|
|
|
if (ret)
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
opts->qlen = num;
|
|
|
|
ret = len;
|
|
|
|
end:
|
|
|
|
mutex_unlock(&opts->lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-03 21:32:43 +08:00
|
|
|
CONFIGFS_ATTR(f_lb_opts_, qlen);
|
2013-11-07 15:41:27 +08:00
|
|
|
|
2015-10-03 21:32:43 +08:00
|
|
|
static ssize_t f_lb_opts_bulk_buflen_show(struct config_item *item, char *page)
|
2013-11-07 15:41:27 +08:00
|
|
|
{
|
2015-10-03 21:32:43 +08:00
|
|
|
struct f_lb_opts *opts = to_f_lb_opts(item);
|
2013-11-07 15:41:27 +08:00
|
|
|
int result;
|
|
|
|
|
|
|
|
mutex_lock(&opts->lock);
|
2015-09-22 03:51:13 +08:00
|
|
|
result = sprintf(page, "%d\n", opts->bulk_buflen);
|
2013-11-07 15:41:27 +08:00
|
|
|
mutex_unlock(&opts->lock);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-10-03 21:32:43 +08:00
|
|
|
static ssize_t f_lb_opts_bulk_buflen_store(struct config_item *item,
|
2013-11-07 15:41:27 +08:00
|
|
|
const char *page, size_t len)
|
|
|
|
{
|
2015-10-03 21:32:43 +08:00
|
|
|
struct f_lb_opts *opts = to_f_lb_opts(item);
|
2013-11-07 15:41:27 +08:00
|
|
|
int ret;
|
|
|
|
u32 num;
|
|
|
|
|
|
|
|
mutex_lock(&opts->lock);
|
|
|
|
if (opts->refcnt) {
|
|
|
|
ret = -EBUSY;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = kstrtou32(page, 0, &num);
|
|
|
|
if (ret)
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
opts->bulk_buflen = num;
|
|
|
|
ret = len;
|
|
|
|
end:
|
|
|
|
mutex_unlock(&opts->lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-03 21:32:43 +08:00
|
|
|
CONFIGFS_ATTR(f_lb_opts_, bulk_buflen);
|
2013-11-07 15:41:27 +08:00
|
|
|
|
|
|
|
static struct configfs_attribute *lb_attrs[] = {
|
2015-10-03 21:32:43 +08:00
|
|
|
&f_lb_opts_attr_qlen,
|
|
|
|
&f_lb_opts_attr_bulk_buflen,
|
2013-11-07 15:41:27 +08:00
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2017-10-16 23:18:41 +08:00
|
|
|
static const struct config_item_type lb_func_type = {
|
2013-11-07 15:41:27 +08:00
|
|
|
.ct_item_ops = &lb_item_ops,
|
|
|
|
.ct_attrs = lb_attrs,
|
|
|
|
.ct_owner = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
2012-12-24 04:10:01 +08:00
|
|
|
static void lb_free_instance(struct usb_function_instance *fi)
|
|
|
|
{
|
|
|
|
struct f_lb_opts *lb_opts;
|
2008-06-20 08:55:35 +08:00
|
|
|
|
2012-12-24 04:10:01 +08:00
|
|
|
lb_opts = container_of(fi, struct f_lb_opts, func_inst);
|
|
|
|
kfree(lb_opts);
|
2008-06-20 08:55:35 +08:00
|
|
|
}
|
2012-12-24 04:10:01 +08:00
|
|
|
|
|
|
|
static struct usb_function_instance *loopback_alloc_instance(void)
|
|
|
|
{
|
|
|
|
struct f_lb_opts *lb_opts;
|
|
|
|
|
|
|
|
lb_opts = kzalloc(sizeof(*lb_opts), GFP_KERNEL);
|
|
|
|
if (!lb_opts)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
2013-11-07 15:41:27 +08:00
|
|
|
mutex_init(&lb_opts->lock);
|
2012-12-24 04:10:01 +08:00
|
|
|
lb_opts->func_inst.free_func_inst = lb_free_instance;
|
2013-11-07 15:41:27 +08:00
|
|
|
lb_opts->bulk_buflen = GZERO_BULK_BUFLEN;
|
|
|
|
lb_opts->qlen = GZERO_QLEN;
|
|
|
|
|
|
|
|
config_group_init_type_name(&lb_opts->func_inst.group, "",
|
|
|
|
&lb_func_type);
|
|
|
|
|
2012-12-24 04:10:01 +08:00
|
|
|
return &lb_opts->func_inst;
|
|
|
|
}
|
|
|
|
DECLARE_USB_FUNCTION(Loopback, loopback_alloc_instance, loopback_alloc);
|
|
|
|
|
|
|
|
int __init lb_modinit(void)
|
|
|
|
{
|
2016-09-09 04:09:30 +08:00
|
|
|
return usb_function_register(&Loopbackusb_func);
|
2012-12-24 04:10:01 +08:00
|
|
|
}
|
2016-09-09 04:09:30 +08:00
|
|
|
|
2012-12-24 04:10:01 +08:00
|
|
|
void __exit lb_modexit(void)
|
|
|
|
{
|
|
|
|
usb_function_unregister(&Loopbackusb_func);
|
|
|
|
}
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|