2015-11-27 10:59:51

by Robert Baldyga

[permalink] [raw]
Subject: [PATCH v2 00/29] usb: gadget: composite: introduce new function API

Hi Felipe,

Here is my new patch series doing some changes in composite framework
and modifying USB Function API. Some of concepts changed significantly,
for example bind process is done automatically inside composite framework
after collecting descriptors from all Functions. Hence bind() operation
of USB Function has been replaced with prep_descs(). Besides the other
benefits, such as simple implementation of gadget-level autoconfig solver,
changes in API allowed to simplify code of USB Functions, which contain
lots of boilerplate code.

First five patches of this series are fixes or improvements, being
preparation for further changes. Patches from 6 to 19 add implementation
of new features. Some code allowing coexistence of both old and new API
is added. This code will be removed after converting all USB Functions
present in kernel to new API.
Last ten patches converts Functions: loopback, sourcesink, ecm, rndis,
hid, acm, eem, ncm, printer and serial to new API. Conversion of another
Functions will be done soon.

*** What has changed? ***

The main changes are listed below:
A. Introduce new descriptors format. It makes descriptors creation process
simpler plus creates good place to contain additional information such
as result of automatic bind or actually selected altsetting.
B. Split descriptors creation process into two stages, implemented by
two new operations:
- prep_descs() provide entity descriptors (interfaces, altsettings
and endpoints)
- prep_vendor_descs() provide class and vendor specific descriptors.
The first one is called before binding funciton to UDC and it's
mandatory, because it provides information needed during bind process.
The second one is optional and a Function can implement it if it wants
to attach some class or vendor specific descriptors. It's called after
bind process, so from it's context all information about interface
numbers and endpoint addresses is accessible.
C. Perform bind automatically inside composite framework after collecting
descriptors from all USB Functions. Besides removing lots of repetitive
code from USB Functions, it gives us two main advantages:
- We can have gadget-level autoconfig solver providing better endpoint
resources usage. We can choose best endpoint configuration for all
Functions in all configurations.
- We have composite driver structure creation process separated from
bind process which allows to modify configfs to operate directly
on composite driver state - both legacy gadgets and configfs can
use common composite driver creation process.
Function allowing to obtain endpoints after bind process is provided,
and it should be called in set_alt().
D. Replace disable() operation with more powerful clear_alt(). It is
called when Function is being disabled or when altsetting being
selected on interface which already has active altsetting. It makes
API more symmetric, which greatly simplifies resource management.
E. Handle endpoint enable/disable automatically, which means, that in
set_alt() we obtain set of already enabled endpoints for current
altsetting. Likewise in clear_alt() endpoints are already disabled.
F. Change meaning of second parameter of set_alt() operation. Now it
contains index of interface within desctiptors array of given USB
Function instead of bInterfaceNumber of this interface, which
simplifies altsetting handling (so far it was necessary to compare
this value with bInterfaceNumber of each interface to find out which
altsetting of which interface is being selected).
G. Handle get_alt() automatically. Currently selected altsetting number
is stored for each interface.

*** How did it work before? ***

So far USB Functions had to handle bind process manually and deal with
endpoints state explicitly, which has been making code lengthy and
bug-prone. USB Functions contained lots of repetitive code which was
usually copied while creating new USB Function module. This resulted
with lots of boilerplate code scattered across all Functions present
in Linux kernel.

BIND:

During bind process we had to obtain interface id manually and assign
it to each interface descriptor (altsetting) of given interface. We also
had to obtain endpoints manually using usb_ep_autoconfig(). Beside its
verbosity, this solution resulted with suboptimal endpoints distribution,
because autoconfig algorithm was aware of requirements of only single
endpoint at a time.

udc_bind_to_driver() {
composite_bind() {
configuration1->bind() {
function1->bind() {
intf1_id = usb_interface_id(); // Obtain intf id manually
ep1 = usb_ep_autoconfig(); // Endpoint-level autoconfig
ep2 = usb_ep_autoconfig();
intf2_id = usb_interface_id();
ep3 = usb_ep_autoconfig();
ep4 = usb_ep_autoconfig();
}
function2->bind() {
intf1_id = usb_interface_id();
ep1 = usb_ep_autoconfig();
ep2 = usb_ep_autoconfig();
ep3 = usb_ep_autoconfig();
}
function3->bind() {
intf1_id = usb_interface_id();
ep1 = usb_ep_autoconfig();
intf2_id = usb_interface_id();
ep2 = usb_ep_autoconfig();
}
}
configuration2->bind() {
function1->bind() {
intf1_id = usb_interface_id();
ep1 = usb_ep_autoconfig();
}
function2->bind() {
intf1_id = usb_interface_id();
ep1 = usb_ep_autoconfig();
}
}
}
}

SET_ALT:

In set_alt() we had to guess if any altsetting for given interface had
been selected before or not. In fact many functions have been doing this
by storing some information in driver_data field of endpoints or simply
by doing interface reset regardless of previous state. We also needed
to guess for which interface set_alt() has been called, because interface
number passed to this callback was the interface index in configuration,
not index in function descriptors. It has been making set_alt() handling
quite problematic.

function1->set_alt() {
id = detect_intf_id();
if (id == intf1_id) {
intf_specific_disable(); // Disable everything just in case
usb_ep_disable(ep1); // Disable endpoint manually
usb_ep_disable(ep2);
config_ep_by_speed(ep1); // Configure endpoint manually
config_ep_by_speed(ep2);
usb_ep_enable(ep1); // Enable endpoint manually
usb_ep_enable(ep2);
intf_specific_enable();
} else {
intf_specific_disable();
usb_ep_disable(ep3);
usb_ep_disable(ep4);
config_ep_by_speed(ep3);
config_ep_by_speed(ep4);
usb_ep_enable(ep3);
usb_ep_enable(ep4);
intf_specific_enable();
}
}
function2->set_alt() {
function_specific_disable();
usb_ep_disable(ep1);
usb_ep_disable(ep2);
config_ep_by_speed(ep1);
config_ep_by_speed(ep2);
usb_ep_enable(ep1);
usb_ep_enable(ep2);
function_specific_enable();
}

DISABLE:

The disable() callback has been called only when entire Function had
being disabled. In this function we had to deactivate all functionality
and disable all endpoints manually.

function1->disable() {
function_specific_disable();
usb_ep_disable(ep1); // Disable endpoint manually
usb_ep_disable(ep2);
usb_ep_disable(ep3);
usb_ep_disable(ep4);
}
function2->disable() {
function_specific_disable();
usb_ep_disable(ep1);
usb_ep_disable(ep2);
}

*** How does it work now? ***

BIND:

Bind process is done entirely by composite framework internals. To
achieve this, composite needs to have a knowledge about interfaces and
endpoints which the Function is comprised of. This knowledge is provided
by prep_descs() callback which assigns descriptors needed for bind process
to Function. Having all descriptors collected allows to implement
configuration-level or even gadget-level autoconfig solver. This solver
could, basing on information from descriptors and endpoint capabilities
of UDC hardware, which gadget driver is being bound to, distribute
endpoints over interfaces in much more optimal way than it has been done
so far. At the end, after binding gadget to UDC hardware,
prep_vendor_descs() callback is invoked for each Function to allow it
to provide some class and vendor specific descriptors.

udc_bind_to_driver() {
composite_bind() {
configuration1->bind() {
function1->prep_descs() { // Gather descriptors
usb_function_set_descs(); // Set descs to Function
}
function2->prep_descs() {
usb_function_set_descs();
}
function3->prep_descs() {
usb_function_set_descs();
}
}
usb_config_do_bind(); // Bind entire configuration

configuration2->bind() {
function1->prep_descs() {
usb_function_set_descs();
}
function2->prep_descs() {
usb_function_set_descs();
}
}
usb_config_do_bind();

composite_prep_vendor_descs(); // Gather class and vendor descs
}
}

SET_ALT:

In set_alt() function we have to obtain endpoints for currently selected
altsetting. Endpoints are already configured and enabled. Interface number
passed to set_alt() is its index within Function descriptors array, which
simplifies set_alt() handling. We also don't need to remember if any
altsetting has been previously selected, because in such situation
clear_alt() is called before set_alt(), to allow function to cleanup
interface state.

function1->set_alt() {
if (intf == 0) {
ep1 = usb_function_get_ep();
ep2 = usb_function_get_ep();
intf_specific_enable();
} else {
ep3 = usb_function_get_ep();
ep4 = usb_function_get_ep();
intf_specific_enable();
}
}
function2->set_alt() {
ep1 = usb_function_get_ep();
ep2 = usb_function_get_ep();
function_specific_enable();
}

CLEAR_ALT:

In clear_alt() callback function can clear interface state and free
resources allocated in set_alt(). It's called before selecting altsetting
in interface which has already selected active altsetting, or when
function is being disabled. Thanks to this clear_alt() can be used as
an enhanced replacement of disable() operation.

function1->clear_alt() {
if (intf == 0) {
intf_specific_disable();
} else {
intf_specific_disable();
}
}
function2->clear_alt() {
function_specific_disable();
}

*** What's next? ***

The next step is to convert all Functions to new API and cleanup composite
code. Then it will be possible to implement intelligent configuration-level
autoconfig solver. We can also try to implement gadget-level autoconfig
solver which could be capable to reconfigure UDC hardware according to
requirements of specific gadget driver.

Thanks to separation of bind process from composite driver creation
process (adding function to configuration doesn't involve its bind, so
it can be done before hardware is available), we can also simplify
configfs gadget implementation. We can make legacy gadgets and configfs
using common composite driver creation process.

I believe it's also possible to make descriptors creation process less
verbose by providing set of macros/functions dedicated for this purpose
(now descriptors take at average over 200 lines of code per Function).

I have some WIP version of patches in which I'm doing part of changes
mentioned above. I can send them as RFC to show what is final result
which I want to achieve.

Best regards,
Robert Baldyga

Changelog:

v2:
- Addressed comments from Sergei
- Added 6 new patches converting Functions to new API

v1: https://lkml.org/lkml/2015/11/3/288

Robert Baldyga (29):
usb: gadget: f_sourcesink: make ISO altset user-selectable
usb: gadget: f_sourcesink: free requests in sourcesink_disable()
usb: gadget: f_loopback: free requests in loopback_disable()
usb: gadget: configfs: fix error path
usb: gadget: composite: introduce new descriptors format
usb: gadget: composite: add functions for descriptors handling
usb: gadget: composite: introduce new USB function ops
usb: gadget: composite: handle function bind
usb: gadget: composite: handle vendor descs
usb: gadget: composite: generate old descs for compatibility
usb: gadget: composite: disable eps before calling disable() callback
usb: gadget: composite: enable eps before calling set_alt() callback
usb: gadget: composite: introduce clear_alt() operation
usb: gadget: composite: handle get_alt() automatically
usb: gadget: composite: add usb_function_get_ep() function
usb: gadget: composite: add usb_get_interface_id() function
usb: gadget: composite: enable adding USB functions using new API
usb: gadget: configfs: add new composite API support
usb: gadget: f_loopback: convert to new API
usb: gadget: f_sourcesink: convert to new API
usb: gadget: f_ecm: conversion to new API
usb: gadget: f_rndis: conversion to new API
usb: gadget: f_hid: handle requests lifetime properly
usb: gadget: f_hid: conversion to new API
usb: gadget: f_acm: conversion to new API
usb: gadget: f_eem: conversion to new API
usb: gadget: f_ncm: conversion to new API
usb: gadget: f_printer: conversion to new API
usb: gadget: f_serial: conversion to new API

drivers/usb/gadget/composite.c | 947 ++++++++++++++++++++++++++++-
drivers/usb/gadget/configfs.c | 24 +-
drivers/usb/gadget/function/f_acm.c | 248 +++-----
drivers/usb/gadget/function/f_ecm.c | 321 +++-------
drivers/usb/gadget/function/f_eem.c | 154 +----
drivers/usb/gadget/function/f_hid.c | 307 ++++------
drivers/usb/gadget/function/f_loopback.c | 218 ++-----
drivers/usb/gadget/function/f_ncm.c | 320 ++++------
drivers/usb/gadget/function/f_printer.c | 300 +++------
drivers/usb/gadget/function/f_rndis.c | 321 ++++------
drivers/usb/gadget/function/f_serial.c | 122 +---
drivers/usb/gadget/function/f_sourcesink.c | 415 +++++--------
drivers/usb/gadget/function/g_zero.h | 6 +-
drivers/usb/gadget/legacy/zero.c | 12 +
include/linux/usb/composite.h | 194 ++++++
15 files changed, 2025 insertions(+), 1884 deletions(-)

--
1.9.1


2015-11-27 11:00:31

by Robert Baldyga

[permalink] [raw]
Subject: [PATCH v2 01/29] usb: gadget: f_sourcesink: make ISO altset user-selectable

So far it was decided during the bind process whether is iso altsetting
included to f_sourcesink function or not. This decision was based on
availability of isochronous endpoints.

Since we can assemble gadget driver using composite framework and configfs
from many different functions, availability of given type of endpoint
can depend on selected components or even on their order in given
configuration.

This can result with non-obvious behavior - even small, seemingly unrelated
change in gadget configuration can decide if we have second altsetting with
iso endpoints in given sourcesink function instance or not.

Because of this it's way better to have additional parameter allowing user
to decide if he/she wants to have iso altsetting, and if iso altsetting is
included, and there are no iso endpoints available, function bind will fail
instead of silently allowing to have non-complete function bound.

Signed-off-by: Robert Baldyga <[email protected]>
---
drivers/usb/gadget/function/f_sourcesink.c | 98 ++++++++++++++++++++----------
drivers/usb/gadget/function/g_zero.h | 3 +
drivers/usb/gadget/legacy/zero.c | 6 ++
3 files changed, 76 insertions(+), 31 deletions(-)

diff --git a/drivers/usb/gadget/function/f_sourcesink.c b/drivers/usb/gadget/function/f_sourcesink.c
index 242ba5c..e950031 100644
--- a/drivers/usb/gadget/function/f_sourcesink.c
+++ b/drivers/usb/gadget/function/f_sourcesink.c
@@ -49,6 +49,7 @@ struct f_sourcesink {
unsigned isoc_maxpacket;
unsigned isoc_mult;
unsigned isoc_maxburst;
+ unsigned isoc_enabled;
unsigned buflen;
unsigned bulk_qlen;
unsigned iso_qlen;
@@ -336,17 +337,28 @@ sourcesink_bind(struct usb_configuration *c, struct usb_function *f)

/* allocate bulk endpoints */
ss->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc);
- if (!ss->in_ep) {
-autoconf_fail:
- ERROR(cdev, "%s: can't autoconfigure on %s\n",
- f->name, cdev->gadget->name);
- return -ENODEV;
- }
+ if (!ss->in_ep)
+ goto autoconf_fail;

ss->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc);
if (!ss->out_ep)
goto autoconf_fail;

+ /* support high speed hardware */
+ hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
+ hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
+
+ /* support super speed hardware */
+ ss_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
+ ss_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
+
+ if (!ss->isoc_enabled) {
+ fs_source_sink_descs[FS_ALT_IFC_1_OFFSET] = NULL;
+ hs_source_sink_descs[HS_ALT_IFC_1_OFFSET] = NULL;
+ ss_source_sink_descs[SS_ALT_IFC_1_OFFSET] = NULL;
+ goto no_iso;
+ }
+
/* sanity check the isoc module parameters */
if (ss->isoc_interval < 1)
ss->isoc_interval = 1;
@@ -368,30 +380,14 @@ autoconf_fail:
/* allocate iso endpoints */
ss->iso_in_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_source_desc);
if (!ss->iso_in_ep)
- goto no_iso;
+ goto autoconf_fail;

ss->iso_out_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_sink_desc);
- if (!ss->iso_out_ep) {
- usb_ep_autoconfig_release(ss->iso_in_ep);
- ss->iso_in_ep = NULL;
-no_iso:
- /*
- * We still want to work even if the UDC doesn't have isoc
- * endpoints, so null out the alt interface that contains
- * them and continue.
- */
- fs_source_sink_descs[FS_ALT_IFC_1_OFFSET] = NULL;
- hs_source_sink_descs[HS_ALT_IFC_1_OFFSET] = NULL;
- ss_source_sink_descs[SS_ALT_IFC_1_OFFSET] = NULL;
- }
+ if (!ss->iso_out_ep)
+ goto autoconf_fail;

if (ss->isoc_maxpacket > 1024)
ss->isoc_maxpacket = 1024;
-
- /* support high speed hardware */
- hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
- hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
-
/*
* Fill in the HS isoc descriptors from the module parameters.
* We assume that the user knows what they are doing and won't
@@ -408,12 +404,6 @@ no_iso:
hs_iso_sink_desc.bInterval = ss->isoc_interval;
hs_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress;

- /* support super speed hardware */
- ss_source_desc.bEndpointAddress =
- fs_source_desc.bEndpointAddress;
- ss_sink_desc.bEndpointAddress =
- fs_sink_desc.bEndpointAddress;
-
/*
* Fill in the SS isoc descriptors from the module parameters.
* We assume that the user knows what they are doing and won't
@@ -436,6 +426,7 @@ no_iso:
(ss->isoc_mult + 1) * (ss->isoc_maxburst + 1);
ss_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress;

+no_iso:
ret = usb_assign_descriptors(f, fs_source_sink_descs,
hs_source_sink_descs, ss_source_sink_descs);
if (ret)
@@ -448,6 +439,11 @@ no_iso:
ss->iso_in_ep ? ss->iso_in_ep->name : "<none>",
ss->iso_out_ep ? ss->iso_out_ep->name : "<none>");
return 0;
+
+autoconf_fail:
+ ERROR(cdev, "%s: can't autoconfigure on %s\n",
+ f->name, cdev->gadget->name);
+ return -ENODEV;
}

static void
@@ -857,6 +853,7 @@ static struct usb_function *source_sink_alloc_func(
ss->isoc_maxpacket = ss_opts->isoc_maxpacket;
ss->isoc_mult = ss_opts->isoc_mult;
ss->isoc_maxburst = ss_opts->isoc_maxburst;
+ ss->isoc_enabled = ss_opts->isoc_enabled;
ss->buflen = ss_opts->bulk_buflen;
ss->bulk_qlen = ss_opts->bulk_qlen;
ss->iso_qlen = ss_opts->iso_qlen;
@@ -1106,6 +1103,44 @@ end:

CONFIGFS_ATTR(f_ss_opts_, isoc_maxburst);

+static ssize_t f_ss_opts_isoc_enabled_show(struct config_item *item, char *page)
+{
+ struct f_ss_opts *opts = to_f_ss_opts(item);
+ int result;
+
+ mutex_lock(&opts->lock);
+ result = sprintf(page, "%u\n", opts->isoc_enabled);
+ mutex_unlock(&opts->lock);
+
+ return result;
+}
+
+static ssize_t f_ss_opts_isoc_enabled_store(struct config_item *item,
+ const char *page, size_t len)
+{
+ struct f_ss_opts *opts = to_f_ss_opts(item);
+ int ret;
+ bool enabled;
+
+ mutex_lock(&opts->lock);
+ if (opts->refcnt) {
+ ret = -EBUSY;
+ goto end;
+ }
+
+ ret = strtobool(page, &enabled);
+ if (ret)
+ goto end;
+
+ opts->isoc_enabled = enabled;
+ ret = len;
+end:
+ mutex_unlock(&opts->lock);
+ return ret;
+}
+
+CONFIGFS_ATTR(f_ss_opts_, isoc_enabled);
+
static ssize_t f_ss_opts_bulk_buflen_show(struct config_item *item, char *page)
{
struct f_ss_opts *opts = to_f_ss_opts(item);
@@ -1226,6 +1261,7 @@ static struct configfs_attribute *ss_attrs[] = {
&f_ss_opts_attr_isoc_maxpacket,
&f_ss_opts_attr_isoc_mult,
&f_ss_opts_attr_isoc_maxburst,
+ &f_ss_opts_attr_isoc_enabled,
&f_ss_opts_attr_bulk_buflen,
&f_ss_opts_attr_bulk_qlen,
&f_ss_opts_attr_iso_qlen,
diff --git a/drivers/usb/gadget/function/g_zero.h b/drivers/usb/gadget/function/g_zero.h
index 492924d0..ae03278 100644
--- a/drivers/usb/gadget/function/g_zero.h
+++ b/drivers/usb/gadget/function/g_zero.h
@@ -10,6 +10,7 @@
#define GZERO_QLEN 32
#define GZERO_ISOC_INTERVAL 4
#define GZERO_ISOC_MAXPACKET 1024
+#define GZERO_ISOC_ENABLED 1
#define GZERO_SS_BULK_QLEN 1
#define GZERO_SS_ISO_QLEN 8

@@ -19,6 +20,7 @@ struct usb_zero_options {
unsigned isoc_maxpacket;
unsigned isoc_mult;
unsigned isoc_maxburst;
+ unsigned isoc_enabled;
unsigned bulk_buflen;
unsigned qlen;
unsigned ss_bulk_qlen;
@@ -32,6 +34,7 @@ struct f_ss_opts {
unsigned isoc_maxpacket;
unsigned isoc_mult;
unsigned isoc_maxburst;
+ unsigned isoc_enabled;
unsigned bulk_buflen;
unsigned bulk_qlen;
unsigned iso_qlen;
diff --git a/drivers/usb/gadget/legacy/zero.c b/drivers/usb/gadget/legacy/zero.c
index d02e2ce..6adfc88 100644
--- a/drivers/usb/gadget/legacy/zero.c
+++ b/drivers/usb/gadget/legacy/zero.c
@@ -66,6 +66,7 @@ module_param(loopdefault, bool, S_IRUGO|S_IWUSR);
static struct usb_zero_options gzero_options = {
.isoc_interval = GZERO_ISOC_INTERVAL,
.isoc_maxpacket = GZERO_ISOC_MAXPACKET,
+ .isoc_enabled = GZERO_ISOC_ENABLED,
.bulk_buflen = GZERO_BULK_BUFLEN,
.qlen = GZERO_QLEN,
.ss_bulk_qlen = GZERO_SS_BULK_QLEN,
@@ -251,6 +252,10 @@ module_param_named(isoc_maxburst, gzero_options.isoc_maxburst, uint,
S_IRUGO|S_IWUSR);
MODULE_PARM_DESC(isoc_maxburst, "0 - 15 (ss only)");

+module_param_named(isoc_enabled, gzero_options.isoc_enabled, uint,
+ S_IRUGO|S_IWUSR);
+MODULE_PARM_DESC(isoc_enabled, "0 - disabled, 1 - enabled");
+
static struct usb_function *func_lb;
static struct usb_function_instance *func_inst_lb;

@@ -294,6 +299,7 @@ static int zero_bind(struct usb_composite_dev *cdev)
ss_opts->isoc_maxpacket = gzero_options.isoc_maxpacket;
ss_opts->isoc_mult = gzero_options.isoc_mult;
ss_opts->isoc_maxburst = gzero_options.isoc_maxburst;
+ ss_opts->isoc_enabled = gzero_options.isoc_enabled;
ss_opts->bulk_buflen = gzero_options.bulk_buflen;
ss_opts->bulk_qlen = gzero_options.ss_bulk_qlen;
ss_opts->iso_qlen = gzero_options.ss_iso_qlen;
--
1.9.1

2015-11-27 11:00:24

by Robert Baldyga

[permalink] [raw]
Subject: [PATCH v2 02/29] usb: gadget: f_sourcesink: free requests in sourcesink_disable()

USB requests in SourceSink function are allocated in sourcesink_get_alt()
function, so we prefer to free them rather in sourcesink_disable() than
in source_sink_complete() when request is completed with error. It provides
better symetry in resource management and improves code readability.

Signed-off-by: Robert Baldyga <[email protected]>
---
drivers/usb/gadget/function/f_sourcesink.c | 63 ++++++++++++++++++++++++------
1 file changed, 51 insertions(+), 12 deletions(-)

diff --git a/drivers/usb/gadget/function/f_sourcesink.c b/drivers/usb/gadget/function/f_sourcesink.c
index e950031..6193b47 100644
--- a/drivers/usb/gadget/function/f_sourcesink.c
+++ b/drivers/usb/gadget/function/f_sourcesink.c
@@ -44,6 +44,11 @@ struct f_sourcesink {
struct usb_ep *iso_out_ep;
int cur_alt;

+ struct usb_request **in_reqs;
+ struct usb_request **out_reqs;
+ struct usb_request **iso_in_reqs;
+ struct usb_request **iso_out_reqs;
+
unsigned pattern;
unsigned isoc_interval;
unsigned isoc_maxpacket;
@@ -550,7 +555,6 @@ static void source_sink_complete(struct usb_ep *ep, struct usb_request *req)
req->actual, req->length);
if (ep == ss->out_ep)
check_read_data(ss, req);
- free_ep_req(ep, req);
return;

case -EOVERFLOW: /* buffer overrun on read means that
@@ -579,7 +583,7 @@ static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in,
bool is_iso, int speed)
{
struct usb_ep *ep;
- struct usb_request *req;
+ struct usb_request **reqs;
int i, size, qlen, status = 0;

if (is_iso) {
@@ -604,19 +608,23 @@ static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in,
qlen = ss->bulk_qlen;
size = 0;
}
-
+
+ reqs = kzalloc(qlen * sizeof(*reqs), GFP_ATOMIC);
+
for (i = 0; i < qlen; i++) {
- req = ss_alloc_ep_req(ep, size);
- if (!req)
- return -ENOMEM;
+ reqs[i] = ss_alloc_ep_req(ep, size);
+ if (!reqs[i]) {
+ status = -ENOMEM;
+ goto err;
+ }

- req->complete = source_sink_complete;
+ reqs[i]->complete = source_sink_complete;
if (is_in)
- reinit_write_data(ep, req);
+ reinit_write_data(ep, reqs[i]);
else if (ss->pattern != 2)
- memset(req->buf, 0x55, req->length);
+ memset(reqs[i]->buf, 0x55, reqs[i]->length);

- status = usb_ep_queue(ep, req, GFP_ATOMIC);
+ status = usb_ep_queue(ep, reqs[i], GFP_ATOMIC);
if (status) {
struct usb_composite_dev *cdev;

@@ -624,12 +632,30 @@ static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in,
ERROR(cdev, "start %s%s %s --> %d\n",
is_iso ? "ISO-" : "", is_in ? "IN" : "OUT",
ep->name, status);
- free_ep_req(ep, req);
- return status;
+ free_ep_req(ep, reqs[i]);
+ goto err;
+ }
+
+ if (is_iso) {
+ if (is_in)
+ ss->iso_in_reqs = reqs;
+ else
+ ss->iso_out_reqs = reqs;
+ } else {
+ if (is_in)
+ ss->in_reqs = reqs;
+ else
+ ss->out_reqs = reqs;
}
}

return status;
+
+err:
+ while (--i)
+ free_ep_req(ep, reqs[i]);
+ kfree(reqs);
+ return status;
}

static void disable_source_sink(struct f_sourcesink *ss)
@@ -754,8 +780,21 @@ static int sourcesink_get_alt(struct usb_function *f, unsigned intf)
static void sourcesink_disable(struct usb_function *f)
{
struct f_sourcesink *ss = func_to_ss(f);
+ int i;

disable_source_sink(ss);
+
+ for (i = 0; i < ss->bulk_qlen; ++i) {
+ free_ep_req(ss->in_ep, ss->in_reqs[i]);
+ free_ep_req(ss->out_ep, ss->out_reqs[i]);
+ }
+
+ if (ss->iso_in_ep) {
+ for (i = 0; i < ss->iso_qlen; ++i) {
+ free_ep_req(ss->iso_in_ep, ss->iso_in_reqs[i]);
+ free_ep_req(ss->iso_out_ep, ss->iso_out_reqs[i]);
+ }
+ }
}

/*-------------------------------------------------------------------------*/
--
1.9.1

2015-11-27 11:00:27

by Robert Baldyga

[permalink] [raw]
Subject: [PATCH v2 03/29] usb: gadget: f_loopback: free requests in loopback_disable()

USB requests in Loopback function are allocated in loopback_get_alt()
function, so we prefer to free them rather in loopback_disable() than
in loopback_complete() when request is completed with error. It provides
better symetry in resource management and improves code readability.

Signed-off-by: Robert Baldyga <[email protected]>
---
drivers/usb/gadget/function/f_loopback.c | 58 +++++++++++++-------------------
1 file changed, 23 insertions(+), 35 deletions(-)

diff --git a/drivers/usb/gadget/function/f_loopback.c b/drivers/usb/gadget/function/f_loopback.c
index 23933bd..7d1fa10 100644
--- a/drivers/usb/gadget/function/f_loopback.c
+++ b/drivers/usb/gadget/function/f_loopback.c
@@ -35,6 +35,9 @@ struct f_loopback {
struct usb_ep *in_ep;
struct usb_ep *out_ep;

+ struct usb_request *in_req;
+ struct usb_request *out_req;
+
unsigned qlen;
unsigned buflen;
};
@@ -249,30 +252,25 @@ static void loopback_complete(struct usb_ep *ep, struct usb_request *req)
* 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;
+ loop->in_req->zero = (req->actual < req->length);
+ loop->in_req->length = req->actual;
+ req = loop->in_req;
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;
+ req = loop->out_req;
ep = loop->out_ep;
}

/* queue the buffer back to host or for next bunch of data */
status = usb_ep_queue(ep, req, GFP_ATOMIC);
- if (status == 0) {
- return;
- } else {
+ if (status < 0)
ERROR(cdev, "Unable to loop back buffer to %s: %d\n",
ep->name, status);
- goto free_req;
- }
+ break;

/* "should never get here" */
default:
@@ -280,20 +278,10 @@ static void loopback_complete(struct usb_ep *ep, struct usb_request *req)
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 */
-free_req:
- usb_ep_free_request(ep == loop->in_ep ?
- loop->out_ep : loop->in_ep,
- req->context);
- free_ep_req(ep, req);
- return;
+ break;
}
}

@@ -316,7 +304,6 @@ static inline struct usb_request *lb_alloc_ep_req(struct usb_ep *ep, int len)
static int alloc_requests(struct usb_composite_dev *cdev,
struct f_loopback *loop)
{
- struct usb_request *in_req, *out_req;
int i;
int result = 0;

@@ -329,23 +316,21 @@ static int alloc_requests(struct usb_composite_dev *cdev,
for (i = 0; i < loop->qlen && result == 0; i++) {
result = -ENOMEM;

- in_req = usb_ep_alloc_request(loop->in_ep, GFP_KERNEL);
- if (!in_req)
+ loop->in_req = usb_ep_alloc_request(loop->in_ep, GFP_KERNEL);
+ if (!loop->in_req)
goto fail;

- out_req = lb_alloc_ep_req(loop->out_ep, 0);
- if (!out_req)
+ loop->out_req = lb_alloc_ep_req(loop->out_ep, 0);
+ if (!loop->out_req)
goto fail_in;

- in_req->complete = loopback_complete;
- out_req->complete = loopback_complete;
+ loop->in_req->complete = loopback_complete;
+ loop->out_req->complete = loopback_complete;

- in_req->buf = out_req->buf;
+ loop->in_req->buf = loop->out_req->buf;
/* length will be set in complete routine */
- in_req->context = out_req;
- out_req->context = in_req;

- result = usb_ep_queue(loop->out_ep, out_req, GFP_ATOMIC);
+ result = usb_ep_queue(loop->out_ep, loop->out_req, GFP_ATOMIC);
if (result) {
ERROR(cdev, "%s queue req --> %d\n",
loop->out_ep->name, result);
@@ -356,9 +341,9 @@ static int alloc_requests(struct usb_composite_dev *cdev,
return 0;

fail_out:
- free_ep_req(loop->out_ep, out_req);
+ free_ep_req(loop->out_ep, loop->out_req);
fail_in:
- usb_ep_free_request(loop->in_ep, in_req);
+ usb_ep_free_request(loop->in_ep, loop->in_req);
fail:
return result;
}
@@ -426,6 +411,9 @@ static void loopback_disable(struct usb_function *f)
struct f_loopback *loop = func_to_loop(f);

disable_loopback(loop);
+
+ free_ep_req(loop->out_ep, loop->out_req);
+ usb_ep_free_request(loop->in_ep, loop->in_req);
}

static struct usb_function *loopback_alloc(struct usb_function_instance *fi)
--
1.9.1

2015-11-27 11:00:36

by Robert Baldyga

[permalink] [raw]
Subject: [PATCH v2 04/29] usb: gadget: configfs: fix error path

As usb_gstrings_attach() failure can happen when some USB functions are
are already added to some configurations (in previous loop iterations),
we should always call purge_configs_funcs() to be sure that failure is
be handled properly.

Signed-off-by: Robert Baldyga <[email protected]>
---
drivers/usb/gadget/configfs.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/usb/gadget/configfs.c b/drivers/usb/gadget/configfs.c
index 163d305..0557f80 100644
--- a/drivers/usb/gadget/configfs.c
+++ b/drivers/usb/gadget/configfs.c
@@ -1342,7 +1342,7 @@ static int configfs_composite_bind(struct usb_gadget *gadget,
s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1);
if (IS_ERR(s)) {
ret = PTR_ERR(s);
- goto err_comp_cleanup;
+ goto err_purge_funcs;
}
c->iConfiguration = s[0].id;
}
--
1.9.1

2015-11-27 11:00:39

by Robert Baldyga

[permalink] [raw]
Subject: [PATCH v2 05/29] usb: gadget: composite: introduce new descriptors format

Introduce new structures designed to contain information about
descriptors. It splits descriptors in two categories:
1. Entity descs - interface and endpoint descriptors
2. Vendor descs - all other vendor and class specific descriptors

Entity descriptors are embedded in hierarchy of structures while vendor
descriptors are contained in linked lists. This distinction is caused
by fact, that entity descriptors are needed during gadget bind procedure,
while vendor descriptors can be supplied later, which is usually desired,
as these descriptors may need to be filled with interface numbers and
endpoint addresses which are assigned during gadget bind.

In result we can split descriptors creation process in two steps - first
collecs entity descriptors, perform the bind and then update and attach
all other descriptors. This process can be done this way not only for
each function separately, but also for entire gadget at once, which means
we can first gather descriptors from all functions in gadget, next perform
bind procedure, and then allow functions to supply additional descriptors.

It allows us to have autoconfig solver capable to better distibute ep
resources, and additionally, because we now store information about
endpoints, allows us to handle endpoint state inside composite framework,
and in result remove lots of boilerplate code from USB functions.

Signed-off-by: Robert Baldyga <[email protected]>
---
include/linux/usb/composite.h | 119 ++++++++++++++++++++++++++++++++++++++++++
1 file changed, 119 insertions(+)

diff --git a/include/linux/usb/composite.h b/include/linux/usb/composite.h
index 1074b89..686c5f7 100644
--- a/include/linux/usb/composite.h
+++ b/include/linux/usb/composite.h
@@ -57,6 +57,121 @@
struct usb_configuration;

/**
+ * struct usb_composite_vendor_desc - vendor specific descriptor
+ * @desc: pointer to vendor specific descriptor
+ * @list: descriptor list element
+ *
+ * It's designed to be element of vendor specific descriptor list,
+ * which can be attached to function, interface (per altsetting) or
+ * endpoint.
+ */
+struct usb_composite_vendor_desc {
+ struct usb_descriptor_header *desc;
+ struct list_head list;
+};
+
+/**
+ * struct usb_composite_ep - representation of USB endpoint
+ * @fs.desc: FullSpeed descriptor
+ * @hs.desc: HighSpeed descriptor
+ * @ss.desc: SuperSpeed descriptor
+ * @ss_comp.desc: SuperSpeed Companion descriptor
+ * @vendor_descs: list of vendor specific descriptors
+ * @vendor_descs_num: count of vendor specific descriptors
+ * @ep: pointer to endpoint obtained during bind process
+ *
+ * We have pointer to each descriptor in union with pointer to descriptor
+ * header in order to avoid casting in many places in code, because in
+ * some situations we want to have access to fields of particular type
+ * of descriptor, while in other situations we want to treat all types
+ * of descriptors in the same way.
+ */
+struct usb_composite_ep {
+ union {
+ struct usb_descriptor_header *header;
+ struct usb_endpoint_descriptor *desc;
+ } fs;
+
+ union {
+ struct usb_descriptor_header *header;
+ struct usb_endpoint_descriptor *desc;
+ } hs;
+
+ union {
+ struct usb_descriptor_header *header;
+ struct usb_endpoint_descriptor *desc;
+ } ss;
+
+ union {
+ struct usb_descriptor_header *header;
+ struct usb_ss_ep_comp_descriptor *desc;
+ } ss_comp;
+
+ struct list_head vendor_descs;
+ int vendor_descs_num;
+
+ struct usb_ep *ep;
+};
+
+/**
+ * struct usb_composite_altset - representation of USB altsetting
+ * @alt.desc: interface (altsetting) descriptor
+ * @eps: array of endpoints in altsetting
+ * @eps_num: number of endpoints
+ * @vendor_descs: list of vendor specific descriptors
+ * @vendor_descs_num: count of vendor specific descriptors
+ *
+ * We have pointer to alt descriptor in union with pointer to descriptor
+ * header in order to avoid casting in many places in code, because in
+ * some situations we want to have access to fields of particular type
+ * of descriptor, while in other situations we want to treat all types
+ * of descriptors in the same way.
+ */
+struct usb_composite_altset {
+ union {
+ struct usb_descriptor_header *header;
+ struct usb_interface_descriptor *desc;
+ } alt;
+
+ struct usb_composite_ep **eps;
+ int eps_num;
+
+ struct list_head vendor_descs;
+ int vendor_descs_num;
+};
+
+/**
+ * struct usb_composite_intf - representation of USB interface
+ * @altsets: array of altsettings in interface
+ * @altsets_num: number of altsettings
+ * @cur_altset: number of currently selected altsetting
+ * @id: id number of interface in configuraion (value of
+ * bInterfaceNumber in interface descriptor)
+ */
+struct usb_composite_intf {
+ struct usb_composite_altset **altsets;
+ int altsets_num;
+
+ int cur_altset;
+ u8 id;
+};
+
+/**
+ * struct usb_composite_descs - representation of USB descriptors
+ * @intfs: array of interfaces in function
+ * @intfs_num: number of interfaces
+ * @vendor_descs: list of vendor specific descriptors
+ * @vendor_descs_num: count of vendor specific descriptors
+ */
+struct usb_composite_descs {
+ struct usb_composite_intf **intfs;
+ int intfs_num;
+
+ struct list_head vendor_descs;
+ int vendor_descs_num;
+};
+
+/**
* struct usb_os_desc_ext_prop - describes one "Extended Property"
* @entry: used to keep a list of extended properties
* @type: Extended Property type
@@ -126,6 +241,8 @@ struct usb_os_desc_table {
* string identifiers assigned during @bind(). If this
* pointer is null after initiation, the function will not
* be available at super speed.
+ * @descs: structure containing information about descriptors and endpoints
+ * assigned during gadget bind.
* @config: assigned when @usb_add_function() is called; this is the
* configuration with which this function is associated.
* @os_desc_table: Table of (interface id, os descriptors) pairs. The function
@@ -187,6 +304,8 @@ struct usb_function {
struct usb_descriptor_header **hs_descriptors;
struct usb_descriptor_header **ss_descriptors;

+ struct usb_composite_descs *descs;
+
struct usb_configuration *config;

struct usb_os_desc_table *os_desc_table;
--
1.9.1

2015-11-27 11:00:44

by Robert Baldyga

[permalink] [raw]
Subject: [PATCH v2 06/29] usb: gadget: composite: add functions for descriptors handling

Introduce functions and macros allowing to create and assign descriptors
to function easily. Macros build structure hierarchy using pointers to
USB descriptors, while functions assigning them to gadget make a deep
copy. It allows for easy conversion of USB functions to make them using
new descriptors format.

Signed-off-by: Robert Baldyga <[email protected]>
---
drivers/usb/gadget/composite.c | 344 +++++++++++++++++++++++++++++++++++++++++
include/linux/usb/composite.h | 52 +++++++
2 files changed, 396 insertions(+)

diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c
index 8b14c2a..3ecfaca 100644
--- a/drivers/usb/gadget/composite.c
+++ b/drivers/usb/gadget/composite.c
@@ -327,6 +327,315 @@ int usb_function_activate(struct usb_function *function)
EXPORT_SYMBOL_GPL(usb_function_activate);

/**
+ * usb_function_set_descs - assing descriptors to USB function
+ * @f: USB function
+ * @descs: USB descriptors to be assigned to function
+ *
+ * This function is to be called from prep_desc() callback to provide
+ * descriptors needed during bind process. It does a deep copy of
+ * descriptors hierarchy.
+ *
+ * Returns zero on success, else negative errno.
+ */
+int usb_function_set_descs(struct usb_function *f,
+ struct usb_composite_descs *descs)
+{
+ struct usb_composite_descs *descs_c;
+ struct usb_composite_intf *intf, *intf_c;
+ struct usb_composite_altset *altset, *altset_c;
+ struct usb_composite_ep *ep, *ep_c;
+ int i, a, e;
+ size_t size;
+ void *mem;
+
+ size = sizeof(*descs);
+
+ if (!descs->intfs_num)
+ return -EINVAL;
+
+ if (!f->config)
+ return -ENODEV;
+
+ size += descs->intfs_num *
+ (sizeof(*descs->intfs) + sizeof(**descs->intfs));
+ for (i = 0; i < descs->intfs_num; ++i) {
+ intf = descs->intfs[i];
+ if (!intf->altsets_num)
+ return -EINVAL;
+ size += intf->altsets_num *
+ (sizeof(*intf->altsets) + sizeof(**intf->altsets));
+ for (a = 0; a < intf->altsets_num; ++a) {
+ altset = intf->altsets[a];
+ size += sizeof(*altset->alt.desc);
+ size += altset->eps_num *
+ (sizeof(*altset->eps) + sizeof(**altset->eps));
+ for (e = 0; e < altset->eps_num; ++e) {
+ ep = altset->eps[e];
+ if (ep->fs.desc) {
+ size += sizeof(*ep->fs.desc);
+ f->config->fullspeed = true;
+ }
+ if (ep->hs.desc) {
+ size += sizeof(*ep->hs.desc);
+ f->config->highspeed = true;
+ }
+ if (ep->ss.desc) {
+ size += sizeof(*ep->ss.desc);
+ f->config->superspeed = true;
+ }
+ if (ep->ss_comp.desc)
+ size += sizeof(*ep->ss_comp.desc);
+ }
+ }
+ }
+
+ mem = kzalloc(size, GFP_KERNEL);
+ if (!mem)
+ return -ENOMEM;
+
+ f->descs = descs_c = mem;
+ mem += sizeof(*descs_c);
+ INIT_LIST_HEAD(&descs_c->vendor_descs);
+ descs_c->intfs_num = descs->intfs_num;
+ descs_c->intfs = mem;
+ mem += descs_c->intfs_num * sizeof(*descs_c->intfs);
+
+ for (i = 0; i < f->descs->intfs_num; ++i) {
+ intf = descs->intfs[i];
+ descs_c->intfs[i] = intf_c = mem;
+ mem += sizeof(*intf_c);
+ intf_c->altsets_num = intf->altsets_num;
+ intf_c->altsets = mem;
+ mem += intf_c->altsets_num * sizeof(*intf_c->altsets);
+
+ for (a = 0; a < intf->altsets_num; ++a) {
+ altset = intf->altsets[a];
+ intf_c->altsets[a] = altset_c = mem;
+ mem += sizeof(*altset_c);
+ INIT_LIST_HEAD(&altset_c->vendor_descs);
+ altset_c->alt.desc = mem;
+ mem += sizeof(*altset->alt.desc);
+ memcpy(altset_c->alt.desc, altset->alt.desc,
+ sizeof(*altset->alt.desc));
+ altset_c->eps_num = altset->eps_num;
+ altset_c->eps = mem;
+ mem += altset_c->eps_num * sizeof(*altset_c->eps);
+
+ for (e = 0; e < altset->eps_num; ++e) {
+ ep = altset->eps[e];
+ altset_c->eps[e] = ep_c = mem;
+ mem += sizeof(*ep_c);
+ INIT_LIST_HEAD(&ep_c->vendor_descs);
+ if (ep->fs.desc) {
+ ep_c->fs.desc = mem;
+ mem += sizeof(*ep_c->fs.desc);
+ memcpy(ep_c->fs.desc, ep->fs.desc,
+ sizeof(*ep_c->fs.desc));
+ }
+ if (ep->hs.desc) {
+ ep_c->hs.desc = mem;
+ mem += sizeof(*ep_c->hs.desc);
+ memcpy(ep_c->hs.desc, ep->hs.desc,
+ sizeof(*ep_c->hs.desc));
+ }
+ if (ep->ss.desc) {
+ ep_c->ss.desc = mem;
+ mem += sizeof(*ep_c->ss.desc);
+ memcpy(ep_c->ss.desc, ep->ss.desc,
+ sizeof(*ep_c->ss.desc));
+ }
+ if (ep->ss_comp.desc) {
+ ep_c->ss_comp.desc = mem;
+ mem += sizeof(*ep_c->ss_comp.desc);
+ memcpy(ep_c->ss_comp.desc,
+ ep->ss_comp.desc,
+ sizeof(*ep_c->ss_comp.desc));
+ }
+ }
+ }
+ }
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(usb_function_set_descs);
+
+/**
+ * usb_function_free_descs - frees descriptors assinged to function
+ * @f: USB function
+ */
+static inline void usb_function_free_descs(struct usb_function *f)
+{
+ kfree(f->descs);
+ f->descs = NULL;
+}
+
+/**
+ * usb_function_add_vendor_desc - add vendor specific descriptor to USB
+ * function
+ * @f: USB function
+ * @desc: descriptor to be attached
+ *
+ * Descriptor is copied and attached at the end of linked list.
+ *
+ * Returns zero on success, else negative errno.
+ */
+int usb_function_add_vendor_desc(struct usb_function *f,
+ struct usb_descriptor_header *desc)
+{
+ struct usb_composite_vendor_desc *vd;
+ void *mem;
+
+ if (!f->descs)
+ return -ENODEV;
+
+ mem = kmalloc(sizeof(*vd) + desc->bLength, GFP_KERNEL);
+ if (!mem)
+ return -ENOMEM;
+
+ vd = mem;
+ vd->desc = mem + sizeof(*vd);
+
+ memcpy(vd->desc, desc, desc->bLength);
+
+ list_add_tail(&vd->list, &f->descs->vendor_descs);
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(usb_function_add_vendor_desc);
+
+/**
+ * usb_ep_add_vendor_desc - add vendor specific descriptor to altsetting
+ * @f: USB function
+ * @i: index of interface in function
+ * @a: index of altsetting in interface
+ * @desc: descriptor to be attached
+ *
+ * Descriptor is copied and attached at the end of linked list.
+ *
+ * Returns zero on success, else negative errno.
+ */
+int usb_altset_add_vendor_desc(struct usb_function *f, int i, int a,
+ struct usb_descriptor_header *desc)
+{
+ struct usb_composite_vendor_desc *vd;
+ struct usb_composite_altset *alt;
+ void *mem;
+
+ if (!f->descs)
+ return -ENODEV;
+ if (f->descs->intfs_num <= i)
+ return -ENODEV;
+ if (f->descs->intfs[i]->altsets_num <= a)
+ return -ENODEV;
+
+ mem = kmalloc(sizeof(*vd) + desc->bLength, GFP_KERNEL);
+ if (!mem)
+ return -ENOMEM;
+
+ vd = mem;
+ vd->desc = mem + sizeof(*vd);
+
+ memcpy(vd->desc, desc, desc->bLength);
+
+ alt = f->descs->intfs[i]->altsets[a];
+ list_add_tail(&vd->list, &alt->vendor_descs);
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(usb_altset_add_vendor_desc);
+
+/**
+ * usb_ep_add_vendor_desc - add vendor specific descriptor to endpoint
+ * @f: USB function
+ * @i: index of interface in function
+ * @a: index of altsetting in interface
+ * @e: index of endpoint in altsetting
+ * @desc: descriptor to be attached
+ *
+ * Descriptor is copied and attached at the end of linked list.
+ *
+ * Returns zero on success, else negative errno.
+ */
+int usb_ep_add_vendor_desc(struct usb_function *f, int i, int a, int e,
+ struct usb_descriptor_header *desc)
+{
+ struct usb_composite_vendor_desc *vd;
+ struct usb_composite_ep *ep;
+ void *mem;
+
+ if (!f->descs)
+ return -ENODEV;
+ if (f->descs->intfs_num <= i)
+ return -ENODEV;
+ if (f->descs->intfs[i]->altsets_num <= a)
+ return -ENODEV;
+ if (f->descs->intfs[i]->altsets[e]->eps_num <= e)
+ return -ENODEV;
+
+ mem = kmalloc(sizeof(*vd) + desc->bLength, GFP_KERNEL);
+ if (!mem)
+ return -ENOMEM;
+
+ vd = mem;
+ vd->desc = mem + sizeof(*vd);
+
+ memcpy(vd->desc, desc, desc->bLength);
+
+ ep = f->descs->intfs[i]->altsets[a]->eps[e];
+ list_add_tail(&vd->list, &ep->vendor_descs);
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(usb_ep_add_vendor_desc);
+
+/**
+ * free_vendor_descs - removes and frees all vendor descriptors from
+ * given list
+ * @vendor_descs: handle to the list of descriptors
+ */
+static inline void free_vendor_descs(struct list_head *vendor_descs)
+{
+ while (!list_empty(vendor_descs)) {
+ struct usb_composite_vendor_desc *d;
+
+ d = list_first_entry(vendor_descs,
+ struct usb_composite_vendor_desc, list);
+ list_del(&d->list);
+ kfree(d);
+ }
+}
+
+/**
+ * usb_function_free_vendor_descs - frees vendor specific descriptors
+ * assinged to function
+ * @f: USB function
+ */
+static void usb_function_free_vendor_descs(struct usb_function *f)
+{
+ struct usb_composite_intf *intf;
+ struct usb_composite_altset *alt;
+ int i, a, e;
+
+ if (!f->descs)
+ return;
+
+ free_vendor_descs(&f->descs->vendor_descs);
+ f->descs->vendor_descs_num = 0;
+ for (i = 0; i < f->descs->intfs_num; ++i) {
+ intf = f->descs->intfs[i];
+ for (a = 0; a < intf->altsets_num; ++a) {
+ alt = intf->altsets[a];
+ free_vendor_descs(&alt->vendor_descs);
+ alt->vendor_descs_num = 0;
+ for (e = 0; e < alt->eps_num; ++e) {
+ free_vendor_descs(&alt->eps[e]->vendor_descs);
+ alt->eps[e]->vendor_descs_num = 0;
+ }
+ }
+ }
+}
+
+/**
* usb_interface_id() - allocate an unused interface ID
* @config: configuration associated with the interface
* @function: function handling the interface
@@ -1893,6 +2202,38 @@ static ssize_t suspended_show(struct device *dev, struct device_attribute *attr,
}
static DEVICE_ATTR_RO(suspended);

+/**
+ * composite_free_descs - free entity descriptors for all functions in all
+ * configurations, allocated with usb_function_set_descs()
+ * @cdev: composite device
+ */
+void composite_free_descs(struct usb_composite_dev *cdev)
+{
+ struct usb_configuration *c;
+ struct usb_function *f;
+
+ list_for_each_entry(c, &cdev->configs, list)
+ list_for_each_entry(f, &c->functions, list)
+ usb_function_free_descs(f);
+}
+EXPORT_SYMBOL_GPL(composite_free_descs);
+
+/**
+ * composite_free_descs - free vendor and class specific descriptors for all
+ * functions in all configurations.
+ * @cdev: composite device
+ */
+void composite_free_vendor_descs(struct usb_composite_dev *cdev)
+{
+ struct usb_configuration *c;
+ struct usb_function *f;
+
+ list_for_each_entry(c, &cdev->configs, list)
+ list_for_each_entry(f, &c->functions, list)
+ usb_function_free_vendor_descs(f);
+}
+EXPORT_SYMBOL_GPL(composite_free_vendor_descs);
+
static void __composite_unbind(struct usb_gadget *gadget, bool unbind_driver)
{
struct usb_composite_dev *cdev = get_gadget_data(gadget);
@@ -1904,6 +2245,9 @@ static void __composite_unbind(struct usb_gadget *gadget, bool unbind_driver)
*/
WARN_ON(cdev->config);

+ composite_free_vendor_descs(cdev);
+ composite_free_descs(cdev);
+
while (!list_empty(&cdev->configs)) {
struct usb_configuration *c;
c = list_first_entry(&cdev->configs,
diff --git a/include/linux/usb/composite.h b/include/linux/usb/composite.h
index 686c5f7..b778d4d 100644
--- a/include/linux/usb/composite.h
+++ b/include/linux/usb/composite.h
@@ -171,6 +171,43 @@ struct usb_composite_descs {
int vendor_descs_num;
};

+/*
+ * Macros to be used to create USB descriptors hierarchy.
+ */
+
+#define USB_COMPOSITE_ENDPOINT(_name, _fs_desc, _hs_desc, _ss_desc, _ss_comp) \
+ static struct usb_composite_ep _name = { \
+ .fs = { .desc = _fs_desc, }, \
+ .hs = { .desc = _hs_desc, }, \
+ .ss = { .desc = _ss_desc, }, \
+ .ss_comp = { .desc = _ss_comp, }, \
+ .vendor_descs = LIST_HEAD_INIT(_name.vendor_descs), \
+ }
+
+#define __EP_ARRAY(...) ((struct usb_composite_ep*[]){ __VA_ARGS__ })
+#define USB_COMPOSITE_ALTSETTING(_name, _desc, ...) \
+ static struct usb_composite_altset _name = { \
+ .alt = { .desc = _desc, }, \
+ .eps = __EP_ARRAY(__VA_ARGS__), \
+ .eps_num = ARRAY_SIZE(__EP_ARRAY(__VA_ARGS__)), \
+ .vendor_descs = LIST_HEAD_INIT(_name.vendor_descs), \
+ }
+
+#define __ALTSET_ARRAY(...) ((struct usb_composite_altset*[]){ __VA_ARGS__ })
+#define USB_COMPOSITE_INTERFACE(_name, ...) \
+ static struct usb_composite_intf _name = { \
+ .altsets = __ALTSET_ARRAY(__VA_ARGS__), \
+ .altsets_num = ARRAY_SIZE(__ALTSET_ARRAY(__VA_ARGS__)), \
+ }
+
+#define __INTF_ARRAY(...) ((struct usb_composite_intf*[]){ __VA_ARGS__ })
+#define USB_COMPOSITE_DESCRIPTORS(_name, ...) \
+ static struct usb_composite_descs _name = { \
+ .intfs = __INTF_ARRAY(__VA_ARGS__), \
+ .intfs_num = ARRAY_SIZE(__INTF_ARRAY(__VA_ARGS__)), \
+ .vendor_descs = LIST_HEAD_INIT(_name.vendor_descs), \
+ }
+
/**
* struct usb_os_desc_ext_prop - describes one "Extended Property"
* @entry: used to keep a list of extended properties
@@ -356,6 +393,18 @@ int usb_add_function(struct usb_configuration *, struct usb_function *);
int usb_function_deactivate(struct usb_function *);
int usb_function_activate(struct usb_function *);

+int usb_function_set_descs(struct usb_function *f,
+ struct usb_composite_descs *descs);
+
+int usb_function_add_vendor_desc(struct usb_function *f,
+ struct usb_descriptor_header *desc);
+
+int usb_altset_add_vendor_desc(struct usb_function *f, int i, int a,
+ struct usb_descriptor_header *desc);
+
+int usb_ep_add_vendor_desc(struct usb_function *f, int i, int a, int e,
+ struct usb_descriptor_header *desc);
+
int usb_interface_id(struct usb_configuration *, struct usb_function *);

int config_ep_by_speed(struct usb_gadget *g, struct usb_function *f,
@@ -532,6 +581,9 @@ extern int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
struct usb_ep *ep0);
void composite_dev_cleanup(struct usb_composite_dev *cdev);

+void composite_free_descs(struct usb_composite_dev *cdev);
+void composite_free_vendor_descs(struct usb_composite_dev *cdev);
+
static inline struct usb_composite_driver *to_cdriver(
struct usb_gadget_driver *gdrv)
{
--
1.9.1

2015-11-27 11:00:48

by Robert Baldyga

[permalink] [raw]
Subject: [PATCH v2 07/29] usb: gadget: composite: introduce new USB function ops

Introduce two new USB function operations:

1. prep_descs() prepares and assigns entity (interface and endpoint)
descriptors to USB function. It's mandatory, in the new function API,
as each USB function should have at least minimalistic set of entity
descriptors. The minimum is single inferface with one altsetting with
no endpoins (ep0 only). Descriptors assigned to function in prep_descs()
callback are used during bind procedure.

2. prep_vendor_descs() - prepares and assigns class and vendor specific
descriptors to function. This function is called after binding function
to UDC hardware, which means that interface numbers and endpoint addresses
are already assigned so that function can use these values to prepare
class or vendor specific descriptors and attach them to function.

Signed-off-by: Robert Baldyga <[email protected]>
---
include/linux/usb/composite.h | 8 ++++++++
1 file changed, 8 insertions(+)

diff --git a/include/linux/usb/composite.h b/include/linux/usb/composite.h
index b778d4d..58d2929 100644
--- a/include/linux/usb/composite.h
+++ b/include/linux/usb/composite.h
@@ -286,6 +286,10 @@ struct usb_os_desc_table {
* can expose more than one interface. If an interface is a member of
* an IAD, only the first interface of IAD has its entry in the table.
* @os_desc_n: Number of entries in os_desc_table
+ * @prep_descs: Returns standard function descriptors (interface and endpoint
+ * descritptors).
+ * @prep_vendor_descs: Attaches vendor or class specific descriptors to
+ * standard descriptors.
* @bind: Before the gadget can register, all of its functions bind() to the
* available resources including string and interface identifiers used
* in interface or class descriptors; endpoints; I/O buffers; and so on.
@@ -354,6 +358,10 @@ struct usb_function {
* Related: unbind() may kfree() but bind() won't...
*/

+ /* new function API*/
+ int (*prep_descs)(struct usb_function *);
+ int (*prep_vendor_descs)(struct usb_function *);
+
/* configuration management: bind/unbind */
int (*bind)(struct usb_configuration *,
struct usb_function *);
--
1.9.1

2015-11-27 11:00:50

by Robert Baldyga

[permalink] [raw]
Subject: [PATCH v2 08/29] usb: gadget: composite: handle function bind

As now USB function supplies entity descriptors to composite in
prep_descs() callback, we can perform bind inside composite framework
without involving bind() callback (which now is unused and will be
removed after converting all functions in kernel to new API).

For now we bind each configuration when it's added, because we have
to support functions based on old API, but after completing conversion
of functions, we will be able to do bind after adding all configurations.
Also more sophisticated autoconfig solver will be provided to improve
utilization of available hardware endpoints.

Signed-off-by: Robert Baldyga <[email protected]>
---
drivers/usb/gadget/composite.c | 162 +++++++++++++++++++++++++++++++++++++++++
include/linux/usb/composite.h | 3 +
2 files changed, 165 insertions(+)

diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c
index 3ecfaca..f4189b1 100644
--- a/drivers/usb/gadget/composite.c
+++ b/drivers/usb/gadget/composite.c
@@ -327,6 +327,21 @@ int usb_function_activate(struct usb_function *function)
EXPORT_SYMBOL_GPL(usb_function_activate);

/**
+ * usb_function_is_new_api - checks if USB function uses new API
+ * @f: USB function
+ *
+ * This function is added temporarily to allow both old and new function API
+ * to coexist. It function will be removed after converting all USB functions
+ * in kernel to new API.
+ *
+ * Returns true if function uses new API.
+ */
+static inline bool usb_function_is_new_api(struct usb_function *f)
+{
+ return !!f->prep_descs;
+}
+
+/**
* usb_function_set_descs - assing descriptors to USB function
* @f: USB function
* @descs: USB descriptors to be assigned to function
@@ -1109,6 +1124,12 @@ int usb_add_config(struct usb_composite_dev *cdev,
goto done;

status = bind(config);
+ if (status < 0)
+ goto out;
+
+ status = usb_config_do_bind(config);
+
+out:
if (status < 0) {
while (!list_empty(&config->functions)) {
struct usb_function *f;
@@ -2404,6 +2425,147 @@ void composite_dev_cleanup(struct usb_composite_dev *cdev)
device_remove_file(&cdev->gadget->dev, &dev_attr_suspended);
}

+/**
+ * usb_cmp_ep_descs - compare descriptors of two endpoints
+ *
+ * As currently during autoconfig procedure we take into consideration only
+ * FullSpeed and SuperSpeed Companion descriptors, we need to compare only
+ * these descriptors. It they are the same, endpoints are identical from
+ * autoconfig point of view.
+ */
+static int usb_cmp_ep_descs(struct usb_composite_ep *ep1,
+ struct usb_composite_ep *ep2)
+{
+ if (ep1->fs.desc->bLength != ep2->fs.desc->bLength)
+ return 0;
+ if (usb_endpoint_dir_in(ep1->fs.desc) ^
+ usb_endpoint_dir_in(ep2->fs.desc))
+ return 0;
+ if (ep1->fs.desc->bmAttributes != ep2->fs.desc->bmAttributes)
+ return 0;
+ if (ep1->fs.desc->wMaxPacketSize != ep2->fs.desc->wMaxPacketSize)
+ return 0;
+ if (ep1->fs.desc->bInterval != ep2->fs.desc->bInterval)
+ return 0;
+
+ if (ep1->fs.desc->bLength != USB_DT_ENDPOINT_AUDIO_SIZE)
+ goto ss_comp;
+
+ if (ep1->fs.desc->bRefresh != ep2->fs.desc->bRefresh)
+ return 0;
+ if (ep1->fs.desc->bSynchAddress != ep2->fs.desc->bSynchAddress)
+ return 0;
+
+ss_comp:
+ if (!ep1->ss_comp.desc ^ !ep2->ss_comp.desc)
+ return 0;
+ if (!ep1->ss_comp.desc)
+ return 1;
+
+ if (ep1->ss_comp.desc->bMaxBurst != ep2->ss_comp.desc->bMaxBurst)
+ return 0;
+ if (ep1->ss_comp.desc->bmAttributes != ep2->ss_comp.desc->bmAttributes)
+ return 0;
+ if (ep1->ss_comp.desc->wBytesPerInterval !=
+ ep2->ss_comp.desc->wBytesPerInterval)
+ return 0;
+
+ return 1;
+}
+
+/**
+ * ep_update_address() - update endpoint address in descriptors
+ * @ep: composite endpoint with assigned hardware ep
+ *
+ * This function should be called after setting ep->ep to endpoint obtained
+ * from usb_ep_autoconfig_ss(), to update endpoint address in descriptors for
+ * all supported speeds.
+ */
+static inline void ep_update_address(struct usb_composite_ep *ep)
+{
+ if (ep->fs.desc)
+ ep->hs.desc->bEndpointAddress = ep->ep->address;
+ if (ep->hs.desc)
+ ep->hs.desc->bEndpointAddress = ep->ep->address;
+ if (ep->ss.desc)
+ ep->ss.desc->bEndpointAddress = ep->ep->address;
+}
+
+/**
+ * interface_do_bind() - bind interface to UDC
+ * @c: USB configuration
+ * @f: USB function in configuration c
+ * @intf: USB interface in function f
+ *
+ * For now we use only simple interface-level ep aucoconfig solver.
+ * We share endpoints between altsettings where it's possible.
+ */
+static int interface_do_bind(struct usb_configuration *c,
+ struct usb_function *f, struct usb_composite_intf *intf)
+{
+ struct usb_composite_altset *alt, *altx;
+ struct usb_composite_ep *ep, *epx;
+ int a, e, ax, ex;
+
+ intf->id = usb_interface_id(c, f);
+ intf->cur_altset = -1;
+
+ for (a = 0; a < intf->altsets_num; ++a) {
+ alt = intf->altsets[a];
+ alt->alt.desc->bInterfaceNumber = intf->id;
+ for (e = 0; e < alt->eps_num; ++e) {
+ ep = alt->eps[e];
+ if (ep->ep)
+ continue;
+ ep->ep = usb_ep_autoconfig_ss(c->cdev->gadget,
+ ep->fs.desc, ep->ss_comp.desc);
+ if (!ep->ep)
+ return -ENODEV;
+ ep_update_address(ep);
+ /* Try endpoint for other altsets */
+ for (ax = a + 1; ax < intf->altsets_num; ++ax) {
+ altx = intf->altsets[ax];
+ for (ex = 0; ex < altx->eps_num; ++ex) {
+ epx = altx->eps[ex];
+ if (usb_cmp_ep_descs(ep, epx)) {
+ epx->ep = ep->ep;
+ ep_update_address(epx);
+ }
+ }
+ }
+ }
+ }
+
+ return 0;
+}
+
+/**
+ * config_do_bind() - bind configuration to UDC
+ * @c: USB configuration
+ *
+ * Bind the all functions in configuration to UDC.
+ */
+int usb_config_do_bind(struct usb_configuration *c)
+{
+ struct usb_function *f;
+ struct usb_composite_intf *intf;
+ int i, ret;
+
+ list_for_each_entry(f, &c->functions, list) {
+ if (!usb_function_is_new_api(f))
+ continue;
+ for (i = 0; i < f->descs->intfs_num; ++i) {
+ intf = f->descs->intfs[i];
+ ret = interface_do_bind(c, f, intf);
+ if (ret)
+ return ret;
+ }
+ }
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(usb_config_do_bind);
+
static int composite_bind(struct usb_gadget *gadget,
struct usb_gadget_driver *gdriver)
{
diff --git a/include/linux/usb/composite.h b/include/linux/usb/composite.h
index 58d2929..a92da38 100644
--- a/include/linux/usb/composite.h
+++ b/include/linux/usb/composite.h
@@ -413,6 +413,7 @@ int usb_altset_add_vendor_desc(struct usb_function *f, int i, int a,
int usb_ep_add_vendor_desc(struct usb_function *f, int i, int a, int e,
struct usb_descriptor_header *desc);

+
int usb_interface_id(struct usb_configuration *, struct usb_function *);

int config_ep_by_speed(struct usb_gadget *g, struct usb_function *f,
@@ -503,6 +504,8 @@ int usb_add_config(struct usb_composite_dev *,
void usb_remove_config(struct usb_composite_dev *,
struct usb_configuration *);

+int usb_config_do_bind(struct usb_configuration *c);
+
/* predefined index for usb_composite_driver */
enum {
USB_GADGET_MANUFACTURER_IDX = 0,
--
1.9.1

2015-11-27 11:00:55

by Robert Baldyga

[permalink] [raw]
Subject: [PATCH v2 09/29] usb: gadget: composite: handle vendor descs

After binding all configurations in gadget, call prep_vendor_descs()
for each function which uses new API and implements this callback.

Signed-off-by: Robert Baldyga <[email protected]>
---
drivers/usb/gadget/composite.c | 31 +++++++++++++++++++++++++++++++
include/linux/usb/composite.h | 2 ++
2 files changed, 33 insertions(+)

diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c
index f4189b1..9b4fcfe 100644
--- a/drivers/usb/gadget/composite.c
+++ b/drivers/usb/gadget/composite.c
@@ -2566,6 +2566,33 @@ int usb_config_do_bind(struct usb_configuration *c)
}
EXPORT_SYMBOL_GPL(usb_config_do_bind);

+/**
+ * composite_prep_vendor_descs - for each function in each configuration call
+ * prep_vendor_descs() callback.
+ * @cdev: composite device
+ */
+int composite_prep_vendor_descs(struct usb_composite_dev *cdev)
+{
+ struct usb_configuration *c;
+ struct usb_function *f;
+ int ret;
+
+ list_for_each_entry(c, &cdev->configs, list)
+ list_for_each_entry(f, &c->functions, list) {
+ if (!usb_function_is_new_api(f))
+ continue;
+ if (f->prep_vendor_descs) {
+ ret = f->prep_vendor_descs(f);
+ if (ret)
+ return ret;
+ }
+
+ }
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(composite_prep_vendor_descs);
+
static int composite_bind(struct usb_gadget *gadget,
struct usb_gadget_driver *gdriver)
{
@@ -2595,6 +2622,10 @@ static int composite_bind(struct usb_gadget *gadget,
if (status < 0)
goto fail;

+ status = composite_prep_vendor_descs(cdev);
+ if (status < 0)
+ goto fail;
+
if (cdev->use_os_string) {
status = composite_os_desc_req_prepare(cdev, gadget->ep0);
if (status)
diff --git a/include/linux/usb/composite.h b/include/linux/usb/composite.h
index a92da38..dc0ac28c 100644
--- a/include/linux/usb/composite.h
+++ b/include/linux/usb/composite.h
@@ -592,6 +592,8 @@ extern int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
struct usb_ep *ep0);
void composite_dev_cleanup(struct usb_composite_dev *cdev);

+int composite_prep_vendor_descs(struct usb_composite_dev *cdev);
+
void composite_free_descs(struct usb_composite_dev *cdev);
void composite_free_vendor_descs(struct usb_composite_dev *cdev);

--
1.9.1

2015-11-27 11:00:59

by Robert Baldyga

[permalink] [raw]
Subject: [PATCH v2 10/29] usb: gadget: composite: generate old descs for compatibility

For now we generate descriptor arrays for each speed as it is done by old
API functions, to allow use mixed new and old API based functions in single
configurations.

This will be removed after complete switch to new API.

Signed-off-by: Robert Baldyga <[email protected]>
---
drivers/usb/gadget/composite.c | 175 +++++++++++++++++++++++++++++++++++++++++
include/linux/usb/composite.h | 2 +
2 files changed, 177 insertions(+)

diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c
index 9b4fcfe..b50ebdb 100644
--- a/drivers/usb/gadget/composite.c
+++ b/drivers/usb/gadget/composite.c
@@ -2255,6 +2255,30 @@ void composite_free_vendor_descs(struct usb_composite_dev *cdev)
}
EXPORT_SYMBOL_GPL(composite_free_vendor_descs);

+/**
+ * composite_free_old_descs - for all functions implementing new API free old
+ * descriptors arrays.
+ * @cdev: composite device
+ */
+void composite_free_old_descs(struct usb_composite_dev *cdev)
+{
+ struct usb_configuration *c;
+ struct usb_function *f;
+
+ list_for_each_entry(c, &cdev->configs, list)
+ list_for_each_entry(f, &c->functions, list) {
+ if (!usb_function_is_new_api(f))
+ continue;
+ kfree(f->fs_descriptors);
+ kfree(f->hs_descriptors);
+ kfree(f->ss_descriptors);
+ f->fs_descriptors = NULL;
+ f->hs_descriptors = NULL;
+ f->ss_descriptors = NULL;
+ }
+}
+EXPORT_SYMBOL_GPL(composite_free_old_descs);
+
static void __composite_unbind(struct usb_gadget *gadget, bool unbind_driver)
{
struct usb_composite_dev *cdev = get_gadget_data(gadget);
@@ -2266,6 +2290,7 @@ static void __composite_unbind(struct usb_gadget *gadget, bool unbind_driver)
*/
WARN_ON(cdev->config);

+ composite_free_old_descs(cdev);
composite_free_vendor_descs(cdev);
composite_free_descs(cdev);

@@ -2593,6 +2618,152 @@ int composite_prep_vendor_descs(struct usb_composite_dev *cdev)
}
EXPORT_SYMBOL_GPL(composite_prep_vendor_descs);

+/*
+ * function_add_desc() - Add given descriptor to descriptor arrays for
+ * each supported speed, in proper version for each speed.
+ *
+ * @f - USB function
+ * @idx - pointer to index of descriptor in fs and hs array
+ * @idx_ss - pointer to index of descriptor in ss array
+ * @fs - descriptor for FS
+ * @hs - descriptor for HS
+ * @ss - descriptor for SS
+ *
+ * Indexes are automatically incremented.
+ *
+ * This function will be removed after converting all USB functions
+ * in kernel to new API.
+ */
+static inline void function_add_desc(struct usb_function *f,
+ int *idx, int *idx_ss,
+ struct usb_descriptor_header *fs,
+ struct usb_descriptor_header *hs,
+ struct usb_descriptor_header *ss) {
+ if (f->config->fullspeed)
+ f->fs_descriptors[*idx] = fs;
+ if (f->config->highspeed)
+ f->hs_descriptors[*idx] = hs;
+ if (f->config->superspeed)
+ f->ss_descriptors[*idx_ss] = ss;
+ ++(*idx);
+ ++(*idx_ss);
+}
+
+/*
+ * function_generate_old_descs() - generate descriptors array for each speed
+ *
+ * Allocate arrays of needed size and assign to them USB descriptors.
+ *
+ * This is temporary solution allowing coexistence to both old and new function
+ * API. It will be removed after converting all functions in kernel to new API.
+ */
+static int function_generate_old_descs(struct usb_function *f)
+{
+ struct usb_composite_intf *intf;
+ struct usb_composite_altset *alt;
+ struct usb_composite_ep *ep;
+ struct usb_composite_vendor_desc *vd;
+ int cnt, eps, i, a, e, idx, idx_ss;
+
+ cnt = f->descs->vendor_descs_num;
+ eps = 0;
+
+ for (i = 0; i < f->descs->intfs_num; ++i) {
+ intf = f->descs->intfs[i];
+ for (a = 0; a < intf->altsets_num; ++a) {
+ alt = intf->altsets[a];
+ cnt += alt->vendor_descs_num + 1;
+ eps += alt->eps_num;
+ for (e = 0; e < alt->eps_num; ++e)
+ cnt += alt->eps[e]->vendor_descs_num + 1;
+ }
+ }
+
+ if (f->config->fullspeed) {
+ f->fs_descriptors = kzalloc((cnt + 1) *
+ sizeof(*f->fs_descriptors), GFP_KERNEL);
+ if (!f->fs_descriptors)
+ return -ENOMEM;
+ }
+ if (f->config->highspeed) {
+ f->hs_descriptors = kzalloc((cnt + 1) *
+ sizeof(*f->hs_descriptors), GFP_KERNEL);
+ if (!f->hs_descriptors)
+ goto err;
+ }
+ if (f->config->superspeed) {
+ f->ss_descriptors = kzalloc((cnt + eps + 1) *
+ sizeof(*f->ss_descriptors), GFP_KERNEL);
+ if (!f->ss_descriptors)
+ goto err;
+ }
+
+ idx = 0;
+ idx_ss = 0;
+ list_for_each_entry(vd, &f->descs->vendor_descs, list)
+ function_add_desc(f, &idx, &idx_ss,
+ vd->desc, vd->desc, vd->desc);
+ for (i = 0; i < f->descs->intfs_num; ++i) {
+ intf = f->descs->intfs[i];
+ for (a = 0; a < intf->altsets_num; ++a) {
+ alt = intf->altsets[a];
+ function_add_desc(f, &idx, &idx_ss, alt->alt.header,
+ alt->alt.header, alt->alt.header);
+ list_for_each_entry(vd, &alt->vendor_descs, list)
+ function_add_desc(f, &idx, &idx_ss,
+ vd->desc, vd->desc, vd->desc);
+ for (e = 0; e < alt->eps_num; ++e) {
+ ep = alt->eps[e];
+ function_add_desc(f, &idx, &idx_ss,
+ ep->fs.header, ep->hs.header,
+ ep->ss.header);
+ if (f->config->superspeed)
+ f->ss_descriptors[idx_ss++] =
+ ep->ss_comp.header;
+ list_for_each_entry(vd,
+ &ep->vendor_descs, list)
+ function_add_desc(f, &idx, &idx_ss,
+ vd->desc, vd->desc, vd->desc);
+ }
+ }
+ }
+
+ return 0;
+
+err:
+ kfree(f->ss_descriptors);
+ f->ss_descriptors = NULL;
+ kfree(f->hs_descriptors);
+ f->hs_descriptors = NULL;
+ kfree(f->fs_descriptors);
+ f->fs_descriptors = NULL;
+
+ return -ENOMEM;
+}
+
+/*
+ * composite_generate_old_descs() - generate descriptors arrays for each
+ * function in each configuraion
+ */
+int composite_generate_old_descs(struct usb_composite_dev *cdev)
+{
+ struct usb_configuration *c;
+ struct usb_function *f;
+ int ret;
+
+ list_for_each_entry(c, &cdev->configs, list)
+ list_for_each_entry(f, &c->functions, list) {
+ if (!usb_function_is_new_api(f))
+ continue;
+ ret = function_generate_old_descs(f);
+ if (ret)
+ return ret;
+ }
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(composite_generate_old_descs);
+
static int composite_bind(struct usb_gadget *gadget,
struct usb_gadget_driver *gdriver)
{
@@ -2626,6 +2797,10 @@ static int composite_bind(struct usb_gadget *gadget,
if (status < 0)
goto fail;

+ status = composite_generate_old_descs(cdev);
+ if (status < 0)
+ goto fail;
+
if (cdev->use_os_string) {
status = composite_os_desc_req_prepare(cdev, gadget->ep0);
if (status)
diff --git a/include/linux/usb/composite.h b/include/linux/usb/composite.h
index dc0ac28c..7cef8c0 100644
--- a/include/linux/usb/composite.h
+++ b/include/linux/usb/composite.h
@@ -593,9 +593,11 @@ extern int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
void composite_dev_cleanup(struct usb_composite_dev *cdev);

int composite_prep_vendor_descs(struct usb_composite_dev *cdev);
+int composite_generate_old_descs(struct usb_composite_dev *cdev);

void composite_free_descs(struct usb_composite_dev *cdev);
void composite_free_vendor_descs(struct usb_composite_dev *cdev);
+void composite_free_old_descs(struct usb_composite_dev *cdev);

static inline struct usb_composite_driver *to_cdriver(
struct usb_gadget_driver *gdrv)
--
1.9.1

2015-11-27 11:01:05

by Robert Baldyga

[permalink] [raw]
Subject: [PATCH v2 11/29] usb: gadget: composite: disable eps before calling disable() callback

Changes meaning of disable() operation for functions using new API.
Before calling disable() callback composite automatically disables
endpoints of active altsettings of given USB function. This reduces
amount of boilerplate code in USB functions.

Signed-off-by: Robert Baldyga <[email protected]>
---
drivers/usb/gadget/composite.c | 51 ++++++++++++++++++++++++++++++++++++------
1 file changed, 44 insertions(+), 7 deletions(-)

diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c
index b50ebdb..a965fbc 100644
--- a/drivers/usb/gadget/composite.c
+++ b/drivers/usb/gadget/composite.c
@@ -246,12 +246,12 @@ done:
}
EXPORT_SYMBOL_GPL(usb_add_function);

+static void disable_function(struct usb_function *f);
+
void usb_remove_function(struct usb_configuration *c, struct usb_function *f)
{
- if (f->disable)
- f->disable(f);
+ disable_function(f);

- bitmap_zero(f->endpoints, 32);
list_del(&f->list);
if (f->unbind)
f->unbind(c, f);
@@ -943,6 +943,46 @@ static void device_qual(struct usb_composite_dev *cdev)

/*-------------------------------------------------------------------------*/

+/**
+ * disable_interface - disable all endpoints in given interface
+ * @f: USB function
+ * @i: interface index in function
+ */
+static void disable_interface(struct usb_function *f, unsigned i)
+{
+ struct usb_composite_intf *intf;
+ struct usb_composite_altset *alt;
+ int e;
+
+ intf = f->descs->intfs[i];
+ if (intf->cur_altset < 0)
+ return;
+
+ alt = intf->altsets[intf->cur_altset];
+ for (e = 0; e < alt->eps_num; ++e)
+ usb_ep_disable(alt->eps[e]->ep);
+
+ intf->cur_altset = -1;
+}
+
+/**
+ * disable_function - disable all endpoints in given function
+ * @f: USB function
+ */
+static void disable_function(struct usb_function *f)
+{
+ int i;
+
+ if (usb_function_is_new_api(f))
+ for (i = 0; i < f->descs->intfs_num; ++i)
+ disable_interface(f, i);
+
+ if (f->disable)
+ f->disable(f);
+
+ bitmap_zero(f->endpoints, 32);
+}
+
static void reset_config(struct usb_composite_dev *cdev)
{
struct usb_function *f;
@@ -950,10 +990,7 @@ static void reset_config(struct usb_composite_dev *cdev)
DBG(cdev, "reset config\n");

list_for_each_entry(f, &cdev->config->functions, list) {
- if (f->disable)
- f->disable(f);
-
- bitmap_zero(f->endpoints, 32);
+ disable_function(f);
}
cdev->config = NULL;
cdev->delayed_status = 0;
--
1.9.1

2015-11-27 11:09:10

by Robert Baldyga

[permalink] [raw]
Subject: [PATCH v2 12/29] usb: gadget: composite: enable eps before calling set_alt() callback

Change set_alt() behavior for functions using new API. Before we call
set_alt() callback, we disable endpoints of previously selected altsetting,
and enable endpoints of currently selected altsetting, which reduces
amount of boilerplate code in USB functions.

We also calculate index of interface in function and pass it to set_alt()
callback instead of passing index of interface in configuration which has
to be obtained from interface descriptor. This simplifies altsetting
changes handling in code of USB functions.

Signed-off-by: Robert Baldyga <[email protected]>
---
drivers/usb/gadget/composite.c | 80 ++++++++++++++++++++++++++++++++++++++++--
1 file changed, 78 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c
index a965fbc..439beca 100644
--- a/drivers/usb/gadget/composite.c
+++ b/drivers/usb/gadget/composite.c
@@ -651,6 +651,26 @@ static void usb_function_free_vendor_descs(struct usb_function *f)
}

/**
+ * usb_interface_id_to_index - if interface with a specified id belongs
+ * to given USB function, return its index within descriptors array
+ * of this function
+ * @f: USB function
+ * @id: id number of interface
+ *
+ * Returns interface index on success, else negative errno.
+ */
+static int usb_interface_id_to_index(struct usb_function *f, u8 id)
+{
+ int i;
+
+ for (i = 0; i < f->descs->intfs_num; ++i)
+ if (f->descs->intfs[i]->id == id)
+ return i;
+
+ return -EINVAL;
+}
+
+/**
* usb_interface_id() - allocate an unused interface ID
* @config: configuration associated with the interface
* @function: function handling the interface
@@ -996,6 +1016,62 @@ static void reset_config(struct usb_composite_dev *cdev)
cdev->delayed_status = 0;
}

+/**
+ * set_alt() - select specified altsetting in given interface
+ * @f: USB function
+ * @i: interface id number
+ * @a: altsetting number
+ *
+ * This function has different behavior depending on which API is used by
+ * given USB function. For functions using old API behavior stays unchanged,
+ * while for functions using new API index of interface in function is
+ * calculated and endpoints are configured and enabled before calling
+ * set_alt() callback.
+ */
+static int set_alt(struct usb_function *f, unsigned i, unsigned a)
+{
+ struct usb_composite_dev *cdev = f->config->cdev;
+ struct usb_composite_altset *alt;
+ struct usb_composite_ep *ep;
+ int e, ret = -EINVAL;
+
+ /* To be removed after switch to new API */
+ if (!usb_function_is_new_api(f))
+ return f->set_alt(f, i, a);
+
+ i = usb_interface_id_to_index(f, i);
+ if (i < 0)
+ return i;
+
+ disable_interface(f, i);
+
+ if (a >= f->descs->intfs[i]->altsets_num)
+ return -EINVAL;
+
+ alt = f->descs->intfs[i]->altsets[a];
+ for (e = 0; e < alt->eps_num; ++e) {
+ ep = alt->eps[e];
+ ret = config_ep_by_speed(cdev->gadget, f, ep->ep);
+ if (ret)
+ goto err;
+ ret = usb_ep_enable(ep->ep);
+ if (ret)
+ goto err;
+ }
+
+ f->descs->intfs[i]->cur_altset = a;
+ ret = f->set_alt(f, i, a);
+ if (ret)
+ goto err;
+
+ return 0;
+err:
+ for (e = 0; e < alt->eps_num; ++e)
+ usb_ep_disable(alt->eps[e]->ep);
+ f->descs->intfs[i]->cur_altset = -1;
+ return ret;
+}
+
static int set_config(struct usb_composite_dev *cdev,
const struct usb_ctrlrequest *ctrl, unsigned number)
{
@@ -1075,7 +1151,7 @@ static int set_config(struct usb_composite_dev *cdev,
set_bit(addr, f->endpoints);
}

- result = f->set_alt(f, tmp, 0);
+ result = set_alt(f, tmp, 0);
if (result < 0) {
DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
tmp, f->name, f, result);
@@ -1976,7 +2052,7 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
break;
if (w_value && !f->set_alt)
break;
- value = f->set_alt(f, w_index, w_value);
+ value = set_alt(f, w_index, w_value);
if (value == USB_GADGET_DELAYED_STATUS) {
DBG(cdev,
"%s: interface %d (%s) requested delayed status\n",
--
1.9.1

2015-11-27 11:08:40

by Robert Baldyga

[permalink] [raw]
Subject: [PATCH v2 13/29] usb: gadget: composite: introduce clear_alt() operation

Introduce clear_alt() callback, which is called when prevoiusly set
altsetting is cleared. This can take place in two situations:
- when another altsetting is selected,
- during function disable.

Thanks to symetry to set_alt(), clear_alt() simplifies managing of
resources allocated in set_alt(). It also takes over the function of
disable() opetarion, so it can be removed after converting all USB
functions to new API.

Signed-off-by: Robert Baldyga <[email protected]>
---
drivers/usb/gadget/composite.c | 12 ++++++++----
include/linux/usb/composite.h | 4 ++++
2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c
index 439beca..5ce95d2 100644
--- a/drivers/usb/gadget/composite.c
+++ b/drivers/usb/gadget/composite.c
@@ -982,6 +982,9 @@ static void disable_interface(struct usb_function *f, unsigned i)
for (e = 0; e < alt->eps_num; ++e)
usb_ep_disable(alt->eps[e]->ep);

+ if (f->clear_alt)
+ f->clear_alt(f, i, intf->cur_altset);
+
intf->cur_altset = -1;
}

@@ -993,12 +996,13 @@ static void disable_function(struct usb_function *f)
{
int i;

- if (usb_function_is_new_api(f))
+ if (usb_function_is_new_api(f)) {
for (i = 0; i < f->descs->intfs_num; ++i)
disable_interface(f, i);
-
- if (f->disable)
- f->disable(f);
+ } else {
+ if (f->disable)
+ f->disable(f);
+ }

bitmap_zero(f->endpoints, 32);
}
diff --git a/include/linux/usb/composite.h b/include/linux/usb/composite.h
index 7cef8c0..3838eb6 100644
--- a/include/linux/usb/composite.h
+++ b/include/linux/usb/composite.h
@@ -301,6 +301,8 @@ struct usb_os_desc_table {
* initialize usb_ep.driver data at this time (when it is used).
* Note that setting an interface to its current altsetting resets
* interface state, and that all interfaces have a disabled state.
+ * @clear_alt: (REQUIRED) Clears altsetting, frees all ep requiests and other
+ * resources allocated by set_alt.
* @get_alt: Returns the active altsetting. If this is not provided,
* then only altsetting zero is supported.
* @disable: (REQUIRED) Indicates the function should be disabled. Reasons
@@ -373,6 +375,8 @@ struct usb_function {
/* runtime state management */
int (*set_alt)(struct usb_function *,
unsigned interface, unsigned alt);
+ void (*clear_alt)(struct usb_function *,
+ unsigned interface, unsigned alt);
int (*get_alt)(struct usb_function *,
unsigned interface);
void (*disable)(struct usb_function *);
--
1.9.1

2015-11-27 11:08:18

by Robert Baldyga

[permalink] [raw]
Subject: [PATCH v2 14/29] usb: gadget: composite: handle get_alt() automatically

As now we store current altsetting number for each interface, we can
handle USB_REQ_GET_INTERFACE automatically.

Signed-off-by: Robert Baldyga <[email protected]>
---
drivers/usb/gadget/composite.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c
index 5ce95d2..21f8c51 100644
--- a/drivers/usb/gadget/composite.c
+++ b/drivers/usb/gadget/composite.c
@@ -2075,7 +2075,13 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
if (!f)
break;
/* lots of interfaces only need altsetting zero... */
- value = f->get_alt ? f->get_alt(f, w_index) : 0;
+ if (usb_function_is_new_api(f)) {
+ value = usb_interface_id_to_index(f, intf);
+ if (value >= 0)
+ value = f->descs->intfs[value]->cur_altset;
+ } else {
+ value = f->get_alt ? f->get_alt(f, w_index) : 0;
+ }
if (value < 0)
break;
*((u8 *)req->buf) = value;
--
1.9.1

2015-11-27 11:01:08

by Robert Baldyga

[permalink] [raw]
Subject: [PATCH v2 15/29] usb: gadget: composite: add usb_function_get_ep() function

Introduce function returning endpoint of given index in active altsetting
of specified interface. It's intended to be used in set_alt() callback
to obtain endpoints of currently selected altsetting.

Signed-off-by: Robert Baldyga <[email protected]>
---
drivers/usb/gadget/composite.c | 33 +++++++++++++++++++++++++++++++++
include/linux/usb/composite.h | 2 ++
2 files changed, 35 insertions(+)

diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c
index 21f8c51..0e264c5 100644
--- a/drivers/usb/gadget/composite.c
+++ b/drivers/usb/gadget/composite.c
@@ -671,6 +671,39 @@ static int usb_interface_id_to_index(struct usb_function *f, u8 id)
}

/**
+ * usb_function_get_ep - obtains endpoint of given index from active
+ * altsetting of given interface
+ * @f: USB function
+ * @i: index of interface in given function
+ * @e: index of endpoint in active altsetting of given interface
+ *
+ * This function is designed to be used in set_alt() callback, when
+ * new altsetting is selected. It allows to obtain endpoints assigned
+ * to altsetting during autoconfig process.
+ *
+ * Returns pointer to endpoint on success or NULL on failure.
+ */
+struct usb_ep *usb_function_get_ep(struct usb_function *f, int i, int e)
+{
+ struct usb_composite_altset *altset;
+ int selected_altset;
+
+ if (!f->descs)
+ return NULL;
+ if (i >= f->descs->intfs_num)
+ return NULL;
+
+ selected_altset = f->descs->intfs[i]->cur_altset;
+ altset = f->descs->intfs[i]->altsets[selected_altset];
+
+ if (e >= altset->eps_num)
+ return NULL;
+
+ return altset->eps[e]->ep;
+}
+EXPORT_SYMBOL_GPL(usb_function_get_ep);
+
+/**
* usb_interface_id() - allocate an unused interface ID
* @config: configuration associated with the interface
* @function: function handling the interface
diff --git a/include/linux/usb/composite.h b/include/linux/usb/composite.h
index 3838eb6..e12921c 100644
--- a/include/linux/usb/composite.h
+++ b/include/linux/usb/composite.h
@@ -418,6 +418,8 @@ int usb_ep_add_vendor_desc(struct usb_function *f, int i, int a, int e,
struct usb_descriptor_header *desc);


+struct usb_ep *usb_function_get_ep(struct usb_function *f, int intf, int ep);
+
int usb_interface_id(struct usb_configuration *, struct usb_function *);

int config_ep_by_speed(struct usb_gadget *g, struct usb_function *f,
--
1.9.1

2015-11-27 11:07:08

by Robert Baldyga

[permalink] [raw]
Subject: [PATCH v2 16/29] usb: gadget: composite: add usb_get_interface_id() function

Introduce function returning id of interface at given index in function.
The id value is equal bInterfaceNumber field in interface descriptor.
This value can be useful during preparation of class or vendor specific
descriptors in prep_vendor_descs() callback. It can be also necessary
to handle some class or vendor specific setup requests.

Signed-off-by: Robert Baldyga <[email protected]>
---
drivers/usb/gadget/composite.c | 19 +++++++++++++++++++
include/linux/usb/composite.h | 2 ++
2 files changed, 21 insertions(+)

diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c
index 0e264c5..9db8924 100644
--- a/drivers/usb/gadget/composite.c
+++ b/drivers/usb/gadget/composite.c
@@ -671,6 +671,25 @@ static int usb_interface_id_to_index(struct usb_function *f, u8 id)
}

/**
+ * usb_get_interface_id - get id number of interface at given index in
+ * USB function
+ * @f: USB function
+ * @i: index of interface in function
+ *
+ * Returns interface id on success, else negative errno.
+ */
+int usb_get_interface_id(struct usb_function *f, int i)
+{
+ if (!f->descs)
+ return -ENODEV;
+ if (f->descs->intfs_num <= i)
+ return -ENODEV;
+
+ return f->descs->intfs[i]->id;
+}
+EXPORT_SYMBOL_GPL(usb_get_interface_id);
+
+/**
* usb_function_get_ep - obtains endpoint of given index from active
* altsetting of given interface
* @f: USB function
diff --git a/include/linux/usb/composite.h b/include/linux/usb/composite.h
index e12921c..b6f5447 100644
--- a/include/linux/usb/composite.h
+++ b/include/linux/usb/composite.h
@@ -418,6 +418,8 @@ int usb_ep_add_vendor_desc(struct usb_function *f, int i, int a, int e,
struct usb_descriptor_header *desc);


+int usb_get_interface_id(struct usb_function *f, int i);
+
struct usb_ep *usb_function_get_ep(struct usb_function *f, int intf, int ep);

int usb_interface_id(struct usb_configuration *, struct usb_function *);
--
1.9.1

2015-11-27 11:06:35

by Robert Baldyga

[permalink] [raw]
Subject: [PATCH v2 17/29] usb: gadget: composite: enable adding USB functions using new API

Enable adding USB functions which use new API. Check if all necessary
function ops are supplied and call prep_descs() to allow function register
it's entity descriptors. Notice that bind() function is not called for
USB functions using new API, as now bind procedure is handled for them
in composite framework.

Signed-off-by: Robert Baldyga <[email protected]>
---
drivers/usb/gadget/composite.c | 40 +++++++++++++++++++++++++++++++---------
1 file changed, 31 insertions(+), 9 deletions(-)

diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c
index 9db8924..91ed45d 100644
--- a/drivers/usb/gadget/composite.c
+++ b/drivers/usb/gadget/composite.c
@@ -181,6 +181,8 @@ ep_found:
}
EXPORT_SYMBOL_GPL(config_ep_by_speed);

+static inline bool usb_function_is_new_api(struct usb_function *f);
+
/**
* usb_add_function() - add a function to a configuration
* @config: the configuration
@@ -198,15 +200,12 @@ EXPORT_SYMBOL_GPL(config_ep_by_speed);
int usb_add_function(struct usb_configuration *config,
struct usb_function *function)
{
- int value = -EINVAL;
+ int value;

DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
function->name, function,
config->label, config);

- if (!function->set_alt || !function->disable)
- goto done;
-
function->config = config;
list_add_tail(&function->list, &config->functions);

@@ -216,13 +215,22 @@ int usb_add_function(struct usb_configuration *config,
goto done;
}

+ value = -EINVAL;
+
+ if (!function->set_alt)
+ goto done;
+
+ if (usb_function_is_new_api(function))
+ goto new_api;
+
+ if (!function->disable)
+ goto done;
+
/* REVISIT *require* function->bind? */
if (function->bind) {
value = function->bind(config, function);
- if (value < 0) {
- list_del(&function->list);
- function->config = NULL;
- }
+ if (value < 0)
+ goto done;
} else
value = 0;

@@ -238,10 +246,24 @@ int usb_add_function(struct usb_configuration *config,
if (!config->superspeed && function->ss_descriptors)
config->superspeed = true;

+ goto done;
+
+new_api:
+ if (!function->prep_descs)
+ goto done;
+
+ if (!function->clear_alt)
+ goto done;
+
+ value = function->prep_descs(function);
+
done:
- if (value)
+ if (value) {
+ list_del(&function->list);
+ function->config = NULL;
DBG(config->cdev, "adding '%s'/%p --> %d\n",
function->name, function, value);
+ }
return value;
}
EXPORT_SYMBOL_GPL(usb_add_function);
--
1.9.1

2015-11-27 11:01:13

by Robert Baldyga

[permalink] [raw]
Subject: [PATCH v2 18/29] usb: gadget: configfs: add new composite API support

Handle functions using new API properly.

Signed-off-by: Robert Baldyga <[email protected]>
---
drivers/usb/gadget/configfs.c | 22 ++++++++++++++++++++++
1 file changed, 22 insertions(+)

diff --git a/drivers/usb/gadget/configfs.c b/drivers/usb/gadget/configfs.c
index 0557f80..153adf7 100644
--- a/drivers/usb/gadget/configfs.c
+++ b/drivers/usb/gadget/configfs.c
@@ -1355,6 +1355,11 @@ static int configfs_composite_bind(struct usb_gadget *gadget,
goto err_purge_funcs;
}
}
+
+ ret = usb_config_do_bind(c);
+ if (ret)
+ goto err_purge_funcs;
+
usb_ep_autoconfig_reset(cdev->gadget);
}
if (cdev->use_os_string) {
@@ -1363,10 +1368,23 @@ static int configfs_composite_bind(struct usb_gadget *gadget,
goto err_purge_funcs;
}

+ ret = composite_prep_vendor_descs(cdev);
+ if (ret < 0)
+ goto err_free_vendor_descs;
+
+ ret = composite_generate_old_descs(cdev);
+ if (ret < 0)
+ goto err_free_old_descs;
+
usb_ep_autoconfig_reset(cdev->gadget);
return 0;

+err_free_old_descs:
+ composite_free_old_descs(cdev);
+err_free_vendor_descs:
+ composite_free_vendor_descs(cdev);
err_purge_funcs:
+ composite_free_descs(cdev);
purge_configs_funcs(gi);
err_comp_cleanup:
composite_dev_cleanup(cdev);
@@ -1383,6 +1401,10 @@ static void configfs_composite_unbind(struct usb_gadget *gadget)
cdev = get_gadget_data(gadget);
gi = container_of(cdev, struct gadget_info, cdev);

+ composite_free_old_descs(cdev);
+ composite_free_vendor_descs(cdev);
+ composite_free_descs(cdev);
+
kfree(otg_desc[0]);
otg_desc[0] = NULL;
purge_configs_funcs(gi);
--
1.9.1

2015-11-27 11:03:57

by Robert Baldyga

[permalink] [raw]
Subject: [PATCH v2 19/29] usb: gadget: f_loopback: convert to new API

Generate descriptors in new format and attach them to USB function in
prep_descs(). Change set_alt() implementation and implement clear_alt()
operation. Remove unnecessary boilerplate code.

Call usb_config_do_bind() in legacy gadget zero, because it uses
usb_add_config_only() instead of usb_add_config() and prepares
configuration manually.

Signed-off-by: Robert Baldyga <[email protected]>
---
drivers/usb/gadget/function/f_loopback.c | 162 ++++++-------------------------
drivers/usb/gadget/legacy/zero.c | 3 +
2 files changed, 33 insertions(+), 132 deletions(-)

diff --git a/drivers/usb/gadget/function/f_loopback.c b/drivers/usb/gadget/function/f_loopback.c
index 7d1fa10..14ba8a9 100644
--- a/drivers/usb/gadget/function/f_loopback.c
+++ b/drivers/usb/gadget/function/f_loopback.c
@@ -76,13 +76,6 @@ static struct usb_endpoint_descriptor fs_loop_sink_desc = {
.bmAttributes = USB_ENDPOINT_XFER_BULK,
};

-static struct usb_descriptor_header *fs_loopback_descs[] = {
- (struct usb_descriptor_header *) &loopback_intf,
- (struct usb_descriptor_header *) &fs_loop_sink_desc,
- (struct usb_descriptor_header *) &fs_loop_source_desc,
- NULL,
-};
-
/* high speed support: */

static struct usb_endpoint_descriptor hs_loop_source_desc = {
@@ -101,13 +94,6 @@ static struct usb_endpoint_descriptor hs_loop_sink_desc = {
.wMaxPacketSize = cpu_to_le16(512),
};

-static struct usb_descriptor_header *hs_loopback_descs[] = {
- (struct usb_descriptor_header *) &loopback_intf,
- (struct usb_descriptor_header *) &hs_loop_source_desc,
- (struct usb_descriptor_header *) &hs_loop_sink_desc,
- NULL,
-};
-
/* super speed support: */

static struct usb_endpoint_descriptor ss_loop_source_desc = {
@@ -142,14 +128,17 @@ static struct usb_ss_ep_comp_descriptor ss_loop_sink_comp_desc = {
.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,
-};
+USB_COMPOSITE_ENDPOINT(ep_source, &fs_loop_source_desc, &hs_loop_source_desc,
+ &ss_loop_source_desc, &ss_loop_source_comp_desc);
+USB_COMPOSITE_ENDPOINT(ep_sink, &fs_loop_sink_desc, &hs_loop_sink_desc,
+ &ss_loop_sink_desc, &ss_loop_sink_comp_desc);
+
+USB_COMPOSITE_ALTSETTING(altset0, &loopback_intf, &ep_source, &ep_sink);
+
+USB_COMPOSITE_INTERFACE(intf0, &altset0);
+
+USB_COMPOSITE_DESCRIPTORS(loopback_descs, &intf0);
+

/* function-specific strings: */

@@ -170,18 +159,10 @@ static struct usb_gadget_strings *loopback_strings[] = {

/*-------------------------------------------------------------------------*/

-static int loopback_bind(struct usb_configuration *c, struct usb_function *f)
+static int loopback_prep_descs(struct usb_function *f)
{
- struct usb_composite_dev *cdev = c->cdev;
- struct f_loopback *loop = func_to_loop(f);
- int id;
- int ret;
-
- /* allocate interface ID(s) */
- id = usb_interface_id(c, f);
- if (id < 0)
- return id;
- loopback_intf.bInterfaceNumber = id;
+ struct usb_composite_dev *cdev = f->config->cdev;
+ int id;

id = usb_string_id(cdev);
if (id < 0)
@@ -189,40 +170,7 @@ static int loopback_bind(struct usb_configuration *c, struct usb_function *f)
strings_loopback[0].id = id;
loopback_intf.iInterface = id;

- /* allocate endpoints */
-
- loop->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_loop_source_desc);
- if (!loop->in_ep) {
-autoconf_fail:
- ERROR(cdev, "%s: can't autoconfigure on %s\n",
- f->name, cdev->gadget->name);
- return -ENODEV;
- }
-
- loop->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_loop_sink_desc);
- if (!loop->out_ep)
- goto autoconf_fail;
-
- /* support high speed hardware */
- hs_loop_source_desc.bEndpointAddress =
- fs_loop_source_desc.bEndpointAddress;
- hs_loop_sink_desc.bEndpointAddress = fs_loop_sink_desc.bEndpointAddress;
-
- /* support super speed hardware */
- 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,
- ss_loopback_descs);
- if (ret)
- return ret;
-
- DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
- (gadget_is_superspeed(c->cdev->gadget) ? "super" :
- (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")),
- f->name, loop->in_ep->name, loop->out_ep->name);
- return 0;
+ return usb_function_set_descs(f, &loopback_descs);
}

static void lb_free_func(struct usb_function *f)
@@ -235,7 +183,6 @@ static void lb_free_func(struct usb_function *f)
opts->refcnt--;
mutex_unlock(&opts->lock);

- usb_free_all_descriptors(f);
kfree(func_to_loop(f));
}

@@ -285,15 +232,6 @@ static void loopback_complete(struct usb_ep *ep, struct usb_request *req)
}
}

-static void disable_loopback(struct f_loopback *loop)
-{
- struct usb_composite_dev *cdev;
-
- cdev = loop->function.config->cdev;
- disable_endpoints(cdev, loop->in_ep, loop->out_ep, NULL, NULL);
- VDBG(cdev, "%s disabled\n", loop->function.name);
-}
-
static inline struct usb_request *lb_alloc_ep_req(struct usb_ep *ep, int len)
{
struct f_loopback *loop = ep->driver_data;
@@ -348,70 +286,30 @@ 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;
-
- result = usb_ep_enable(ep);
- if (result < 0)
- goto out;
- ep->driver_data = loop;
- result = 0;
-
-out:
- 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)
- goto out;
-
- result = enable_endpoint(cdev, loop, loop->out_ep);
- if (result)
- goto disable_in;
-
- result = alloc_requests(cdev, loop);
- if (result)
- goto disable_out;
-
- DBG(cdev, "%s enabled\n", loop->function.name);
- return 0;
-
-disable_out:
- usb_ep_disable(loop->out_ep);
-disable_in:
- usb_ep_disable(loop->in_ep);
-out:
- 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 */
- disable_loopback(loop);
- return enable_loopback(cdev, loop);
+ loop->in_ep = usb_function_get_ep(f, intf, 0);
+ if (!loop->in_ep)
+ return -ENODEV;
+ loop->in_ep->driver_data = loop;
+
+ loop->out_ep = usb_function_get_ep(f, intf, 1);
+ if (!loop->out_ep)
+ return -ENODEV;
+ loop->out_ep->driver_data = loop;
+
+ return alloc_requests(cdev, loop);
}

-static void loopback_disable(struct usb_function *f)
+static void loopback_clear_alt(struct usb_function *f,
+ unsigned intf, unsigned alt)
{
struct f_loopback *loop = func_to_loop(f);

- disable_loopback(loop);
-
free_ep_req(loop->out_ep, loop->out_req);
usb_ep_free_request(loop->in_ep, loop->in_req);
}
@@ -437,9 +335,9 @@ static struct usb_function *loopback_alloc(struct usb_function_instance *fi)
loop->qlen = 32;

loop->function.name = "loopback";
- loop->function.bind = loopback_bind;
+ loop->function.prep_descs = loopback_prep_descs;
loop->function.set_alt = loopback_set_alt;
- loop->function.disable = loopback_disable;
+ loop->function.clear_alt = loopback_clear_alt;
loop->function.strings = loopback_strings;

loop->function.free_func = lb_free_func;
diff --git a/drivers/usb/gadget/legacy/zero.c b/drivers/usb/gadget/legacy/zero.c
index 6adfc88..781ca94 100644
--- a/drivers/usb/gadget/legacy/zero.c
+++ b/drivers/usb/gadget/legacy/zero.c
@@ -378,6 +378,9 @@ static int zero_bind(struct usb_composite_dev *cdev)
status = usb_add_function(&loopback_driver, func_lb);
if (status)
goto err_free_otg_desc;
+ status = usb_config_do_bind(&loopback_driver);
+ if (status)
+ goto err_free_otg_desc;

usb_ep_autoconfig_reset(cdev->gadget);
usb_composite_overwrite_options(cdev, &coverwrite);
--
1.9.1

2015-11-27 11:01:17

by Robert Baldyga

[permalink] [raw]
Subject: [PATCH v2 20/29] usb: gadget: f_sourcesink: convert to new API

Generate descriptors in new format and attach them to USB function in
prep_descs(). Change set_alt() implementation and implement clear_alt()
operation. Get rid of get_alt() callback, as now USB_REQ_GET_INTERFACE
is handled automatically by composite framwework. Remove unnecessary
boilerplate code.

Call usb_config_do_bind() in legacy gadget zero, because it uses
usb_add_config_only() instead of usb_add_config() and prepares
configuration manually.

Signed-off-by: Robert Baldyga <[email protected]>
---
drivers/usb/gadget/function/f_sourcesink.c | 314 ++++++-----------------------
drivers/usb/gadget/function/g_zero.h | 3 -
drivers/usb/gadget/legacy/zero.c | 3 +
3 files changed, 65 insertions(+), 255 deletions(-)

diff --git a/drivers/usb/gadget/function/f_sourcesink.c b/drivers/usb/gadget/function/f_sourcesink.c
index 6193b47..262dae8 100644
--- a/drivers/usb/gadget/function/f_sourcesink.c
+++ b/drivers/usb/gadget/function/f_sourcesink.c
@@ -42,7 +42,6 @@ struct f_sourcesink {
struct usb_ep *out_ep;
struct usb_ep *iso_in_ep;
struct usb_ep *iso_out_ep;
- int cur_alt;

struct usb_request **in_reqs;
struct usb_request **out_reqs;
@@ -125,19 +124,6 @@ static struct usb_endpoint_descriptor fs_iso_sink_desc = {
.bInterval = 4,
};

-static struct usb_descriptor_header *fs_source_sink_descs[] = {
- (struct usb_descriptor_header *) &source_sink_intf_alt0,
- (struct usb_descriptor_header *) &fs_sink_desc,
- (struct usb_descriptor_header *) &fs_source_desc,
- (struct usb_descriptor_header *) &source_sink_intf_alt1,
-#define FS_ALT_IFC_1_OFFSET 3
- (struct usb_descriptor_header *) &fs_sink_desc,
- (struct usb_descriptor_header *) &fs_source_desc,
- (struct usb_descriptor_header *) &fs_iso_sink_desc,
- (struct usb_descriptor_header *) &fs_iso_source_desc,
- NULL,
-};
-
/* high speed support: */

static struct usb_endpoint_descriptor hs_source_desc = {
@@ -174,19 +160,6 @@ static struct usb_endpoint_descriptor hs_iso_sink_desc = {
.bInterval = 4,
};

-static struct usb_descriptor_header *hs_source_sink_descs[] = {
- (struct usb_descriptor_header *) &source_sink_intf_alt0,
- (struct usb_descriptor_header *) &hs_source_desc,
- (struct usb_descriptor_header *) &hs_sink_desc,
- (struct usb_descriptor_header *) &source_sink_intf_alt1,
-#define HS_ALT_IFC_1_OFFSET 3
- (struct usb_descriptor_header *) &hs_source_desc,
- (struct usb_descriptor_header *) &hs_sink_desc,
- (struct usb_descriptor_header *) &hs_iso_source_desc,
- (struct usb_descriptor_header *) &hs_iso_sink_desc,
- NULL,
-};
-
/* super speed support: */

static struct usb_endpoint_descriptor ss_source_desc = {
@@ -259,24 +232,24 @@ static struct usb_ss_ep_comp_descriptor ss_iso_sink_comp_desc = {
.wBytesPerInterval = cpu_to_le16(1024),
};

-static struct usb_descriptor_header *ss_source_sink_descs[] = {
- (struct usb_descriptor_header *) &source_sink_intf_alt0,
- (struct usb_descriptor_header *) &ss_source_desc,
- (struct usb_descriptor_header *) &ss_source_comp_desc,
- (struct usb_descriptor_header *) &ss_sink_desc,
- (struct usb_descriptor_header *) &ss_sink_comp_desc,
- (struct usb_descriptor_header *) &source_sink_intf_alt1,
-#define SS_ALT_IFC_1_OFFSET 5
- (struct usb_descriptor_header *) &ss_source_desc,
- (struct usb_descriptor_header *) &ss_source_comp_desc,
- (struct usb_descriptor_header *) &ss_sink_desc,
- (struct usb_descriptor_header *) &ss_sink_comp_desc,
- (struct usb_descriptor_header *) &ss_iso_source_desc,
- (struct usb_descriptor_header *) &ss_iso_source_comp_desc,
- (struct usb_descriptor_header *) &ss_iso_sink_desc,
- (struct usb_descriptor_header *) &ss_iso_sink_comp_desc,
- NULL,
-};
+USB_COMPOSITE_ENDPOINT(ep_source, &fs_source_desc, &hs_source_desc,
+ &ss_source_desc, &ss_source_comp_desc);
+USB_COMPOSITE_ENDPOINT(ep_sink, &fs_sink_desc, &hs_sink_desc,
+ &ss_sink_desc, &ss_sink_comp_desc);
+USB_COMPOSITE_ENDPOINT(ep_iso_source, &fs_iso_source_desc, &hs_iso_source_desc,
+ &ss_iso_source_desc, &ss_iso_source_comp_desc);
+USB_COMPOSITE_ENDPOINT(ep_iso_sink, &fs_iso_sink_desc, &hs_iso_sink_desc,
+ &ss_iso_sink_desc, &ss_iso_sink_comp_desc);
+
+USB_COMPOSITE_ALTSETTING(altset0, &source_sink_intf_alt0, &ep_source, &ep_sink);
+USB_COMPOSITE_ALTSETTING(altset1, &source_sink_intf_alt1, &ep_source, &ep_sink,
+ &ep_iso_source, &ep_iso_sink);
+
+USB_COMPOSITE_INTERFACE(intf0, &altset0, &altset1);
+USB_COMPOSITE_INTERFACE(intf0_no_iso, &altset0);
+
+USB_COMPOSITE_DESCRIPTORS(source_sink_descs, &intf0);
+USB_COMPOSITE_DESCRIPTORS(source_sink_descs_no_iso, &intf0_no_iso);

/* function-specific strings: */

@@ -304,65 +277,12 @@ static inline struct usb_request *ss_alloc_ep_req(struct usb_ep *ep, int len)
return alloc_ep_req(ep, len, ss->buflen);
}

-static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
-{
- int value;
-
- value = usb_ep_disable(ep);
- if (value < 0)
- DBG(cdev, "disable %s --> %d\n", ep->name, value);
-}
-
-void disable_endpoints(struct usb_composite_dev *cdev,
- struct usb_ep *in, struct usb_ep *out,
- struct usb_ep *iso_in, struct usb_ep *iso_out)
-{
- disable_ep(cdev, in);
- disable_ep(cdev, out);
- if (iso_in)
- disable_ep(cdev, iso_in);
- if (iso_out)
- disable_ep(cdev, iso_out);
-}
-
-static int
-sourcesink_bind(struct usb_configuration *c, struct usb_function *f)
+static int sourcesink_prep_descs(struct usb_function *f)
{
- struct usb_composite_dev *cdev = c->cdev;
struct f_sourcesink *ss = func_to_ss(f);
- int id;
- int ret;

- /* allocate interface ID(s) */
- id = usb_interface_id(c, f);
- if (id < 0)
- return id;
- source_sink_intf_alt0.bInterfaceNumber = id;
- source_sink_intf_alt1.bInterfaceNumber = id;
-
- /* allocate bulk endpoints */
- ss->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc);
- if (!ss->in_ep)
- goto autoconf_fail;
-
- ss->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc);
- if (!ss->out_ep)
- goto autoconf_fail;
-
- /* support high speed hardware */
- hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
- hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
-
- /* support super speed hardware */
- ss_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
- ss_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
-
- if (!ss->isoc_enabled) {
- fs_source_sink_descs[FS_ALT_IFC_1_OFFSET] = NULL;
- hs_source_sink_descs[HS_ALT_IFC_1_OFFSET] = NULL;
- ss_source_sink_descs[SS_ALT_IFC_1_OFFSET] = NULL;
- goto no_iso;
- }
+ if (!ss->isoc_enabled)
+ return usb_function_set_descs(f, &source_sink_descs_no_iso);

/* sanity check the isoc module parameters */
if (ss->isoc_interval < 1)
@@ -382,15 +302,6 @@ sourcesink_bind(struct usb_configuration *c, struct usb_function *f)
1023 : ss->isoc_maxpacket;
fs_iso_sink_desc.bInterval = ss->isoc_interval;

- /* allocate iso endpoints */
- ss->iso_in_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_source_desc);
- if (!ss->iso_in_ep)
- goto autoconf_fail;
-
- ss->iso_out_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_sink_desc);
- if (!ss->iso_out_ep)
- goto autoconf_fail;
-
if (ss->isoc_maxpacket > 1024)
ss->isoc_maxpacket = 1024;
/*
@@ -401,14 +312,10 @@ sourcesink_bind(struct usb_configuration *c, struct usb_function *f)
hs_iso_source_desc.wMaxPacketSize = ss->isoc_maxpacket;
hs_iso_source_desc.wMaxPacketSize |= ss->isoc_mult << 11;
hs_iso_source_desc.bInterval = ss->isoc_interval;
- hs_iso_source_desc.bEndpointAddress =
- fs_iso_source_desc.bEndpointAddress;

hs_iso_sink_desc.wMaxPacketSize = ss->isoc_maxpacket;
hs_iso_sink_desc.wMaxPacketSize |= ss->isoc_mult << 11;
hs_iso_sink_desc.bInterval = ss->isoc_interval;
- hs_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress;
-
/*
* Fill in the SS isoc descriptors from the module parameters.
* We assume that the user knows what they are doing and won't
@@ -420,8 +327,6 @@ sourcesink_bind(struct usb_configuration *c, struct usb_function *f)
ss_iso_source_comp_desc.bMaxBurst = ss->isoc_maxburst;
ss_iso_source_comp_desc.wBytesPerInterval = ss->isoc_maxpacket *
(ss->isoc_mult + 1) * (ss->isoc_maxburst + 1);
- ss_iso_source_desc.bEndpointAddress =
- fs_iso_source_desc.bEndpointAddress;

ss_iso_sink_desc.wMaxPacketSize = ss->isoc_maxpacket;
ss_iso_sink_desc.bInterval = ss->isoc_interval;
@@ -429,26 +334,8 @@ sourcesink_bind(struct usb_configuration *c, struct usb_function *f)
ss_iso_sink_comp_desc.bMaxBurst = ss->isoc_maxburst;
ss_iso_sink_comp_desc.wBytesPerInterval = ss->isoc_maxpacket *
(ss->isoc_mult + 1) * (ss->isoc_maxburst + 1);
- ss_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress;

-no_iso:
- ret = usb_assign_descriptors(f, fs_source_sink_descs,
- hs_source_sink_descs, ss_source_sink_descs);
- if (ret)
- return ret;
-
- DBG(cdev, "%s speed %s: IN/%s, OUT/%s, ISO-IN/%s, ISO-OUT/%s\n",
- (gadget_is_superspeed(c->cdev->gadget) ? "super" :
- (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")),
- f->name, ss->in_ep->name, ss->out_ep->name,
- ss->iso_in_ep ? ss->iso_in_ep->name : "<none>",
- ss->iso_out_ep ? ss->iso_out_ep->name : "<none>");
- return 0;
-
-autoconf_fail:
- ERROR(cdev, "%s: can't autoconfigure on %s\n",
- f->name, cdev->gadget->name);
- return -ENODEV;
+ return usb_function_set_descs(f, &source_sink_descs);
}

static void
@@ -462,7 +349,6 @@ sourcesink_free_func(struct usb_function *f)
opts->refcnt--;
mutex_unlock(&opts->lock);

- usb_free_all_descriptors(f);
kfree(func_to_ss(f));
}

@@ -658,138 +544,63 @@ err:
return status;
}

-static void disable_source_sink(struct f_sourcesink *ss)
-{
- struct usb_composite_dev *cdev;
-
- cdev = ss->function.config->cdev;
- disable_endpoints(cdev, ss->in_ep, ss->out_ep, ss->iso_in_ep,
- ss->iso_out_ep);
- VDBG(cdev, "%s disabled\n", ss->function.name);
-}
-
-static int
-enable_source_sink(struct usb_composite_dev *cdev, struct f_sourcesink *ss,
- int alt)
-{
- int result = 0;
- int speed = cdev->gadget->speed;
- struct usb_ep *ep;
-
- /* one bulk endpoint writes (sources) zeroes IN (to the host) */
- ep = ss->in_ep;
- result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
- if (result)
- return result;
- result = usb_ep_enable(ep);
- if (result < 0)
- return result;
- ep->driver_data = ss;
-
- result = source_sink_start_ep(ss, true, false, speed);
- if (result < 0) {
-fail:
- ep = ss->in_ep;
- usb_ep_disable(ep);
- return result;
- }
-
- /* one bulk endpoint reads (sinks) anything OUT (from the host) */
- ep = ss->out_ep;
- result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
- if (result)
- goto fail;
- result = usb_ep_enable(ep);
- if (result < 0)
- goto fail;
- ep->driver_data = ss;
-
- result = source_sink_start_ep(ss, false, false, speed);
- if (result < 0) {
-fail2:
- ep = ss->out_ep;
- usb_ep_disable(ep);
- goto fail;
- }
-
- if (alt == 0)
- goto out;
-
- /* one iso endpoint writes (sources) zeroes IN (to the host) */
- ep = ss->iso_in_ep;
- if (ep) {
- result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
- if (result)
- goto fail2;
- result = usb_ep_enable(ep);
- if (result < 0)
- goto fail2;
- ep->driver_data = ss;
-
- result = source_sink_start_ep(ss, true, true, speed);
- if (result < 0) {
-fail3:
- ep = ss->iso_in_ep;
- if (ep)
- usb_ep_disable(ep);
- goto fail2;
- }
- }
-
- /* one iso endpoint reads (sinks) anything OUT (from the host) */
- ep = ss->iso_out_ep;
- if (ep) {
- result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
- if (result)
- goto fail3;
- result = usb_ep_enable(ep);
- if (result < 0)
- goto fail3;
- ep->driver_data = ss;
-
- result = source_sink_start_ep(ss, false, true, speed);
- if (result < 0) {
- usb_ep_disable(ep);
- goto fail3;
- }
- }
-out:
- ss->cur_alt = alt;
-
- DBG(cdev, "%s enabled, alt intf %d\n", ss->function.name, alt);
- return result;
-}
-
static int sourcesink_set_alt(struct usb_function *f,
unsigned intf, unsigned alt)
{
struct f_sourcesink *ss = func_to_ss(f);
struct usb_composite_dev *cdev = f->config->cdev;
+ int speed = cdev->gadget->speed;
+ int ret;

- disable_source_sink(ss);
- return enable_source_sink(cdev, ss, alt);
-}
+ ss->in_ep = usb_function_get_ep(f, intf, 0);
+ if (!ss->in_ep)
+ return -ENODEV;
+ ss->in_ep->driver_data = ss;
+ ret = source_sink_start_ep(ss, true, false, speed);
+ if (ret < 0)
+ return ret;

-static int sourcesink_get_alt(struct usb_function *f, unsigned intf)
-{
- struct f_sourcesink *ss = func_to_ss(f);
+ ss->out_ep = usb_function_get_ep(f, intf, 1);
+ if (!ss->out_ep)
+ return -ENODEV;
+ ss->out_ep->driver_data = ss;
+ ret = source_sink_start_ep(ss, false, false, speed);
+ if (ret < 0)
+ return ret;
+
+ if (alt == 1) {
+ ss->iso_in_ep = usb_function_get_ep(f, intf, 2);
+ if (!ss->iso_in_ep)
+ return -ENODEV;
+ ss->iso_in_ep->driver_data = ss;
+ ret = source_sink_start_ep(ss, true, true, speed);
+ if (ret < 0)
+ return ret;
+
+ ss->iso_out_ep = usb_function_get_ep(f, intf, 3);
+ if (!ss->iso_out_ep)
+ return -ENODEV;
+ ss->iso_out_ep->driver_data = ss;
+ ret = source_sink_start_ep(ss, false, true, speed);
+ if (ret < 0)
+ return ret;
+ }

- return ss->cur_alt;
+ return 0;
}

-static void sourcesink_disable(struct usb_function *f)
+static void sourcesink_clear_alt(struct usb_function *f,
+ unsigned intf, unsigned alt)
{
struct f_sourcesink *ss = func_to_ss(f);
int i;

- disable_source_sink(ss);
-
for (i = 0; i < ss->bulk_qlen; ++i) {
free_ep_req(ss->in_ep, ss->in_reqs[i]);
free_ep_req(ss->out_ep, ss->out_reqs[i]);
}

- if (ss->iso_in_ep) {
+ if (alt == 1) {
for (i = 0; i < ss->iso_qlen; ++i) {
free_ep_req(ss->iso_in_ep, ss->iso_in_reqs[i]);
free_ep_req(ss->iso_out_ep, ss->iso_out_reqs[i]);
@@ -898,10 +709,9 @@ static struct usb_function *source_sink_alloc_func(
ss->iso_qlen = ss_opts->iso_qlen;

ss->function.name = "source/sink";
- ss->function.bind = sourcesink_bind;
+ ss->function.prep_descs = sourcesink_prep_descs;
ss->function.set_alt = sourcesink_set_alt;
- ss->function.get_alt = sourcesink_get_alt;
- ss->function.disable = sourcesink_disable;
+ ss->function.clear_alt = sourcesink_clear_alt;
ss->function.setup = sourcesink_setup;
ss->function.strings = sourcesink_strings;

diff --git a/drivers/usb/gadget/function/g_zero.h b/drivers/usb/gadget/function/g_zero.h
index ae03278..e8d832d 100644
--- a/drivers/usb/gadget/function/g_zero.h
+++ b/drivers/usb/gadget/function/g_zero.h
@@ -68,8 +68,5 @@ void lb_modexit(void);
int lb_modinit(void);

/* common utilities */
-void disable_endpoints(struct usb_composite_dev *cdev,
- struct usb_ep *in, struct usb_ep *out,
- struct usb_ep *iso_in, struct usb_ep *iso_out);

#endif /* __G_ZERO_H */
diff --git a/drivers/usb/gadget/legacy/zero.c b/drivers/usb/gadget/legacy/zero.c
index 781ca94..4c52b4a 100644
--- a/drivers/usb/gadget/legacy/zero.c
+++ b/drivers/usb/gadget/legacy/zero.c
@@ -373,6 +373,9 @@ static int zero_bind(struct usb_composite_dev *cdev)
status = usb_add_function(&sourcesink_driver, func_ss);
if (status)
goto err_free_otg_desc;
+ status = usb_config_do_bind(&sourcesink_driver);
+ if (status)
+ goto err_free_otg_desc;

usb_ep_autoconfig_reset(cdev->gadget);
status = usb_add_function(&loopback_driver, func_lb);
--
1.9.1

2015-11-27 11:04:57

by Robert Baldyga

[permalink] [raw]
Subject: [PATCH v2 21/29] usb: gadget: f_ecm: conversion to new API

Generate descriptors in new format and attach them to USB function in
prep_descs(). Implement prep_vendor_descs() to supply class specific
descriptors. Change set_alt() implementation and implement clear_alt()
operation. Get rid of get_alt() which now is handled automatically.
Remove boilerplate code. Change USB request lifetime management - now
it's allocated in set_alt() and freed in clear_alt().

Signed-off-by: Robert Baldyga <[email protected]>
---
drivers/usb/gadget/function/f_ecm.c | 321 +++++++++++-------------------------
1 file changed, 92 insertions(+), 229 deletions(-)

diff --git a/drivers/usb/gadget/function/f_ecm.c b/drivers/usb/gadget/function/f_ecm.c
index 7ad60ee..4627f20 100644
--- a/drivers/usb/gadget/function/f_ecm.c
+++ b/drivers/usb/gadget/function/f_ecm.c
@@ -214,25 +214,6 @@ static struct usb_endpoint_descriptor fs_ecm_out_desc = {
.bmAttributes = USB_ENDPOINT_XFER_BULK,
};

-static struct usb_descriptor_header *ecm_fs_function[] = {
- /* CDC ECM control descriptors */
- (struct usb_descriptor_header *) &ecm_iad_descriptor,
- (struct usb_descriptor_header *) &ecm_control_intf,
- (struct usb_descriptor_header *) &ecm_header_desc,
- (struct usb_descriptor_header *) &ecm_union_desc,
- (struct usb_descriptor_header *) &ecm_desc,
-
- /* NOTE: status endpoint might need to be removed */
- (struct usb_descriptor_header *) &fs_ecm_notify_desc,
-
- /* data interface, altsettings 0 and 1 */
- (struct usb_descriptor_header *) &ecm_data_nop_intf,
- (struct usb_descriptor_header *) &ecm_data_intf,
- (struct usb_descriptor_header *) &fs_ecm_in_desc,
- (struct usb_descriptor_header *) &fs_ecm_out_desc,
- NULL,
-};
-
/* high speed support: */

static struct usb_endpoint_descriptor hs_ecm_notify_desc = {
@@ -263,25 +244,6 @@ static struct usb_endpoint_descriptor hs_ecm_out_desc = {
.wMaxPacketSize = cpu_to_le16(512),
};

-static struct usb_descriptor_header *ecm_hs_function[] = {
- /* CDC ECM control descriptors */
- (struct usb_descriptor_header *) &ecm_iad_descriptor,
- (struct usb_descriptor_header *) &ecm_control_intf,
- (struct usb_descriptor_header *) &ecm_header_desc,
- (struct usb_descriptor_header *) &ecm_union_desc,
- (struct usb_descriptor_header *) &ecm_desc,
-
- /* NOTE: status endpoint might need to be removed */
- (struct usb_descriptor_header *) &hs_ecm_notify_desc,
-
- /* data interface, altsettings 0 and 1 */
- (struct usb_descriptor_header *) &ecm_data_nop_intf,
- (struct usb_descriptor_header *) &ecm_data_intf,
- (struct usb_descriptor_header *) &hs_ecm_in_desc,
- (struct usb_descriptor_header *) &hs_ecm_out_desc,
- NULL,
-};
-
/* super speed support: */

static struct usb_endpoint_descriptor ss_ecm_notify_desc = {
@@ -331,27 +293,21 @@ static struct usb_ss_ep_comp_descriptor ss_ecm_bulk_comp_desc = {
/* .bmAttributes = 0, */
};

-static struct usb_descriptor_header *ecm_ss_function[] = {
- /* CDC ECM control descriptors */
- (struct usb_descriptor_header *) &ecm_iad_descriptor,
- (struct usb_descriptor_header *) &ecm_control_intf,
- (struct usb_descriptor_header *) &ecm_header_desc,
- (struct usb_descriptor_header *) &ecm_union_desc,
- (struct usb_descriptor_header *) &ecm_desc,
-
- /* NOTE: status endpoint might need to be removed */
- (struct usb_descriptor_header *) &ss_ecm_notify_desc,
- (struct usb_descriptor_header *) &ss_ecm_intr_comp_desc,
-
- /* data interface, altsettings 0 and 1 */
- (struct usb_descriptor_header *) &ecm_data_nop_intf,
- (struct usb_descriptor_header *) &ecm_data_intf,
- (struct usb_descriptor_header *) &ss_ecm_in_desc,
- (struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc,
- (struct usb_descriptor_header *) &ss_ecm_out_desc,
- (struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc,
- NULL,
-};
+USB_COMPOSITE_ENDPOINT(ep_notify, &fs_ecm_notify_desc, &hs_ecm_notify_desc,
+ &ss_ecm_notify_desc, &ss_ecm_intr_comp_desc);
+USB_COMPOSITE_ENDPOINT(ep_in, &fs_ecm_in_desc, &hs_ecm_in_desc,
+ &ss_ecm_in_desc, &ss_ecm_bulk_comp_desc);
+USB_COMPOSITE_ENDPOINT(ep_out, &fs_ecm_out_desc, &hs_ecm_out_desc,
+ &ss_ecm_out_desc, &ss_ecm_bulk_comp_desc);
+
+USB_COMPOSITE_ALTSETTING(intf0alt0, &ecm_control_intf, &ep_notify);
+USB_COMPOSITE_ALTSETTING(intf1alt0, &ecm_data_nop_intf);
+USB_COMPOSITE_ALTSETTING(intf1alt1, &ecm_data_intf, &ep_in, &ep_out);
+
+USB_COMPOSITE_INTERFACE(intf0, &intf0alt0);
+USB_COMPOSITE_INTERFACE(intf1, &intf1alt0, &intf1alt1);
+
+USB_COMPOSITE_DESCRIPTORS(ecm_descs, &intf0, &intf1);

/* string descriptors: */

@@ -537,47 +493,43 @@ static int ecm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
struct usb_composite_dev *cdev = f->config->cdev;

/* Control interface has only altsetting 0 */
- if (intf == ecm->ctrl_id) {
- if (alt != 0)
- goto fail;
+ if (intf == 0) {
+ /* NOTE: a status/notification endpoint is *OPTIONAL* but we
+ * don't treat it that way. It's simpler, and some newer CDC
+ * profiles (wireless handsets) no longer treat it as optional.
+ */

VDBG(cdev, "reset ecm control %d\n", intf);
- usb_ep_disable(ecm->notify);
- if (!(ecm->notify->desc)) {
- VDBG(cdev, "init ecm ctrl %d\n", intf);
- if (config_ep_by_speed(cdev->gadget, f, ecm->notify))
- goto fail;
- }
- usb_ep_enable(ecm->notify);
-
- /* Data interface has two altsettings, 0 and 1 */
- } else if (intf == ecm->data_id) {
- if (alt > 1)
- goto fail;

- if (ecm->port.in_ep->enabled) {
- DBG(cdev, "reset ecm\n");
- gether_disconnect(&ecm->port);
+ ecm->notify = usb_function_get_ep(f, intf, 0);
+ if (!ecm->notify)
+ return -ENODEV;
+
+ /* allocate notification request and buffer */
+ ecm->notify_req = usb_ep_alloc_request(ecm->notify, GFP_KERNEL);
+ if (!ecm->notify_req)
+ return -ENOMEM;
+ ecm->notify_req->buf = kmalloc(ECM_STATUS_BYTECOUNT,
+ GFP_KERNEL);
+ if (!ecm->notify_req->buf) {
+ usb_ep_free_request(ecm->notify, ecm->notify_req);
+ return -ENOMEM;
}
-
- if (!ecm->port.in_ep->desc ||
- !ecm->port.out_ep->desc) {
- DBG(cdev, "init ecm\n");
- if (config_ep_by_speed(cdev->gadget, f,
- ecm->port.in_ep) ||
- config_ep_by_speed(cdev->gadget, f,
- ecm->port.out_ep)) {
- ecm->port.in_ep->desc = NULL;
- ecm->port.out_ep->desc = NULL;
- goto fail;
- }
- }
-
+ ecm->notify_req->context = ecm;
+ ecm->notify_req->complete = ecm_notify_complete;
+ /* Data interface has two altsettings, 0 and 1 */
+ } else if (intf == 1) {
/* CDC Ethernet only sends data in non-default altsettings.
* Changing altsettings resets filters, statistics, etc.
*/
if (alt == 1) {
struct net_device *net;
+ ecm->port.in_ep = usb_function_get_ep(f, intf, 0);
+ if (!ecm->port.in_ep)
+ return -ENODEV;
+ ecm->port.out_ep = usb_function_get_ep(f, intf, 1);
+ if (!ecm->port.out_ep)
+ return -ENODEV;

/* Enable zlps by default for ECM conformance;
* override for musb_hdrc (avoids txdma ovhead).
@@ -598,38 +550,24 @@ static int ecm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
* just guarantee that a speed notification is always sent.
*/
ecm_notify(ecm);
- } else
- goto fail;
+ }

return 0;
-fail:
- return -EINVAL;
}

-/* Because the data interface supports multiple altsettings,
- * this ECM function *MUST* implement a get_alt() method.
- */
-static int ecm_get_alt(struct usb_function *f, unsigned intf)
+static void ecm_clear_alt(struct usb_function *f, unsigned intf, unsigned alt)
{
- struct f_ecm *ecm = func_to_ecm(f);
-
- if (intf == ecm->ctrl_id)
- return 0;
- return ecm->port.in_ep->enabled ? 1 : 0;
-}
-
-static void ecm_disable(struct usb_function *f)
-{
- struct f_ecm *ecm = func_to_ecm(f);
+ struct f_ecm *ecm = func_to_ecm(f);
struct usb_composite_dev *cdev = f->config->cdev;

DBG(cdev, "ecm deactivated\n");

- if (ecm->port.in_ep->enabled)
+ if (intf == 0) {
+ kfree(ecm->notify_req->buf);
+ usb_ep_free_request(ecm->notify, ecm->notify_req);
+ } else if (intf == 1 && alt == 1) {
gether_disconnect(&ecm->port);
-
- usb_ep_disable(ecm->notify);
- ecm->notify->desc = NULL;
+ }
}

/*-------------------------------------------------------------------------*/
@@ -676,20 +614,33 @@ static void ecm_close(struct gether *geth)

/* ethernet function driver setup/binding */

-static int
-ecm_bind(struct usb_configuration *c, struct usb_function *f)
+static int ecm_prep_descs(struct usb_function *f)
{
- struct usb_composite_dev *cdev = c->cdev;
- struct f_ecm *ecm = func_to_ecm(f);
+ struct usb_composite_dev *cdev = f->config->cdev;
struct usb_string *us;
- int status;
- struct usb_ep *ep;
-
- struct f_ecm_opts *ecm_opts;

if (!can_support_ecm(cdev->gadget))
return -EINVAL;

+ us = usb_gstrings_attach(cdev, ecm_strings,
+ ARRAY_SIZE(ecm_string_defs));
+ if (IS_ERR(us))
+ return PTR_ERR(us);
+ ecm_control_intf.iInterface = us[0].id;
+ ecm_data_intf.iInterface = us[2].id;
+ ecm_desc.iMACAddress = us[1].id;
+ ecm_iad_descriptor.iFunction = us[3].id;
+
+ return usb_function_set_descs(f, &ecm_descs);
+}
+
+static int ecm_prep_vendor_descs(struct usb_function *f)
+{
+ struct f_ecm *ecm = func_to_ecm(f);
+ struct f_ecm_opts *ecm_opts;
+ struct usb_composite_dev *cdev = f->config->cdev;
+ int status, intf0_id, intf1_id;
+
ecm_opts = container_of(f->fi, struct f_ecm_opts, func_inst);

/*
@@ -709,86 +660,26 @@ ecm_bind(struct usb_configuration *c, struct usb_function *f)
ecm_opts->bound = true;
}

- us = usb_gstrings_attach(cdev, ecm_strings,
- ARRAY_SIZE(ecm_string_defs));
- if (IS_ERR(us))
- return PTR_ERR(us);
- ecm_control_intf.iInterface = us[0].id;
- ecm_data_intf.iInterface = us[2].id;
- ecm_desc.iMACAddress = us[1].id;
- ecm_iad_descriptor.iFunction = us[3].id;
+ intf0_id = usb_get_interface_id(f, 0);
+ intf1_id = usb_get_interface_id(f, 1);

- /* allocate instance-specific interface IDs */
- status = usb_interface_id(c, f);
- if (status < 0)
- goto fail;
- ecm->ctrl_id = status;
- ecm_iad_descriptor.bFirstInterface = status;
-
- ecm_control_intf.bInterfaceNumber = status;
- ecm_union_desc.bMasterInterface0 = status;
-
- status = usb_interface_id(c, f);
- if (status < 0)
- goto fail;
- ecm->data_id = status;
-
- ecm_data_nop_intf.bInterfaceNumber = status;
- ecm_data_intf.bInterfaceNumber = status;
- ecm_union_desc.bSlaveInterface0 = status;
-
- status = -ENODEV;
-
- /* allocate instance-specific endpoints */
- ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_in_desc);
- if (!ep)
- goto fail;
- ecm->port.in_ep = ep;
-
- ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_out_desc);
- if (!ep)
- goto fail;
- ecm->port.out_ep = ep;
-
- /* NOTE: a status/notification endpoint is *OPTIONAL* but we
- * don't treat it that way. It's simpler, and some newer CDC
- * profiles (wireless handsets) no longer treat it as optional.
- */
- ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_notify_desc);
- if (!ep)
- goto fail;
- ecm->notify = ep;
-
- status = -ENOMEM;
-
- /* allocate notification request and buffer */
- ecm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
- if (!ecm->notify_req)
- goto fail;
- ecm->notify_req->buf = kmalloc(ECM_STATUS_BYTECOUNT, GFP_KERNEL);
- if (!ecm->notify_req->buf)
- goto fail;
- ecm->notify_req->context = ecm;
- ecm->notify_req->complete = ecm_notify_complete;
-
- /* support all relevant hardware speeds... we expect that when
- * hardware is dual speed, all bulk-capable endpoints work at
- * both speeds
- */
- hs_ecm_in_desc.bEndpointAddress = fs_ecm_in_desc.bEndpointAddress;
- hs_ecm_out_desc.bEndpointAddress = fs_ecm_out_desc.bEndpointAddress;
- hs_ecm_notify_desc.bEndpointAddress =
- fs_ecm_notify_desc.bEndpointAddress;
+ ecm->ctrl_id = intf0_id;
+ ecm->data_id = intf1_id;
+
+ ecm_iad_descriptor.bFirstInterface = intf0_id;
+
+ ecm_union_desc.bMasterInterface0 = intf0_id;
+ ecm_union_desc.bSlaveInterface0 = intf1_id;

- ss_ecm_in_desc.bEndpointAddress = fs_ecm_in_desc.bEndpointAddress;
- ss_ecm_out_desc.bEndpointAddress = fs_ecm_out_desc.bEndpointAddress;
- ss_ecm_notify_desc.bEndpointAddress =
- fs_ecm_notify_desc.bEndpointAddress;
+ usb_function_add_vendor_desc(f,
+ (struct usb_descriptor_header *)&ecm_iad_descriptor);

- status = usb_assign_descriptors(f, ecm_fs_function, ecm_hs_function,
- ecm_ss_function);
- if (status)
- goto fail;
+ usb_altset_add_vendor_desc(f, 0, 0,
+ (struct usb_descriptor_header *)&ecm_header_desc);
+ usb_altset_add_vendor_desc(f, 0, 0,
+ (struct usb_descriptor_header *)&ecm_union_desc);
+ usb_altset_add_vendor_desc(f, 0, 0,
+ (struct usb_descriptor_header *)&ecm_desc);

/* NOTE: all that is done without knowing or caring about
* the network link ... which is unavailable to this code
@@ -798,22 +689,7 @@ ecm_bind(struct usb_configuration *c, struct usb_function *f)
ecm->port.open = ecm_open;
ecm->port.close = ecm_close;

- DBG(cdev, "CDC Ethernet: %s speed IN/%s OUT/%s NOTIFY/%s\n",
- gadget_is_superspeed(c->cdev->gadget) ? "super" :
- gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
- ecm->port.in_ep->name, ecm->port.out_ep->name,
- ecm->notify->name);
return 0;
-
-fail:
- if (ecm->notify_req) {
- kfree(ecm->notify_req->buf);
- usb_ep_free_request(ecm->notify, ecm->notify_req);
- }
-
- ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
-
- return status;
}

static inline struct f_ecm_opts *to_f_ecm_opts(struct config_item *item)
@@ -897,18 +773,6 @@ static void ecm_free(struct usb_function *f)
mutex_unlock(&opts->lock);
}

-static void ecm_unbind(struct usb_configuration *c, struct usb_function *f)
-{
- struct f_ecm *ecm = func_to_ecm(f);
-
- DBG(c->cdev, "ecm unbind\n");
-
- usb_free_all_descriptors(f);
-
- kfree(ecm->notify_req->buf);
- usb_ep_free_request(ecm->notify, ecm->notify_req);
-}
-
static struct usb_function *ecm_alloc(struct usb_function_instance *fi)
{
struct f_ecm *ecm;
@@ -940,12 +804,11 @@ static struct usb_function *ecm_alloc(struct usb_function_instance *fi)

ecm->port.func.name = "cdc_ethernet";
/* descriptors are per-instance copies */
- ecm->port.func.bind = ecm_bind;
- ecm->port.func.unbind = ecm_unbind;
+ ecm->port.func.prep_descs = ecm_prep_descs;
+ ecm->port.func.prep_vendor_descs = ecm_prep_vendor_descs;
ecm->port.func.set_alt = ecm_set_alt;
- ecm->port.func.get_alt = ecm_get_alt;
+ ecm->port.func.clear_alt = ecm_clear_alt;
ecm->port.func.setup = ecm_setup;
- ecm->port.func.disable = ecm_disable;
ecm->port.func.free_func = ecm_free;

return &ecm->port.func;
--
1.9.1

2015-11-27 11:04:30

by Robert Baldyga

[permalink] [raw]
Subject: [PATCH v2 22/29] usb: gadget: f_rndis: conversion to new API

Generate descriptors in new format and attach them to USB function in
prep_descs(). Implement prep_vendor_descs() to supply class specific
descriptors. Change set_alt() implementation and implement clear_alt()
operation. Remove boilerplate code. Change USB request lifetime
management - now it's allocated in set_alt() and freed in clear_alt().

Signed-off-by: Robert Baldyga <[email protected]>
---
drivers/usb/gadget/function/f_rndis.c | 321 ++++++++++++----------------------
1 file changed, 112 insertions(+), 209 deletions(-)

diff --git a/drivers/usb/gadget/function/f_rndis.c b/drivers/usb/gadget/function/f_rndis.c
index e587767..abe11a7 100644
--- a/drivers/usb/gadget/function/f_rndis.c
+++ b/drivers/usb/gadget/function/f_rndis.c
@@ -212,24 +212,6 @@ static struct usb_endpoint_descriptor fs_out_desc = {
.bmAttributes = USB_ENDPOINT_XFER_BULK,
};

-static struct usb_descriptor_header *eth_fs_function[] = {
- (struct usb_descriptor_header *) &rndis_iad_descriptor,
-
- /* control interface matches ACM, not Ethernet */
- (struct usb_descriptor_header *) &rndis_control_intf,
- (struct usb_descriptor_header *) &header_desc,
- (struct usb_descriptor_header *) &call_mgmt_descriptor,
- (struct usb_descriptor_header *) &rndis_acm_descriptor,
- (struct usb_descriptor_header *) &rndis_union_desc,
- (struct usb_descriptor_header *) &fs_notify_desc,
-
- /* data interface has no altsetting */
- (struct usb_descriptor_header *) &rndis_data_intf,
- (struct usb_descriptor_header *) &fs_in_desc,
- (struct usb_descriptor_header *) &fs_out_desc,
- NULL,
-};
-
/* high speed support: */

static struct usb_endpoint_descriptor hs_notify_desc = {
@@ -260,24 +242,6 @@ static struct usb_endpoint_descriptor hs_out_desc = {
.wMaxPacketSize = cpu_to_le16(512),
};

-static struct usb_descriptor_header *eth_hs_function[] = {
- (struct usb_descriptor_header *) &rndis_iad_descriptor,
-
- /* control interface matches ACM, not Ethernet */
- (struct usb_descriptor_header *) &rndis_control_intf,
- (struct usb_descriptor_header *) &header_desc,
- (struct usb_descriptor_header *) &call_mgmt_descriptor,
- (struct usb_descriptor_header *) &rndis_acm_descriptor,
- (struct usb_descriptor_header *) &rndis_union_desc,
- (struct usb_descriptor_header *) &hs_notify_desc,
-
- /* data interface has no altsetting */
- (struct usb_descriptor_header *) &rndis_data_intf,
- (struct usb_descriptor_header *) &hs_in_desc,
- (struct usb_descriptor_header *) &hs_out_desc,
- NULL,
-};
-
/* super speed support: */

static struct usb_endpoint_descriptor ss_notify_desc = {
@@ -327,26 +291,20 @@ static struct usb_ss_ep_comp_descriptor ss_bulk_comp_desc = {
/* .bmAttributes = 0, */
};

-static struct usb_descriptor_header *eth_ss_function[] = {
- (struct usb_descriptor_header *) &rndis_iad_descriptor,
-
- /* control interface matches ACM, not Ethernet */
- (struct usb_descriptor_header *) &rndis_control_intf,
- (struct usb_descriptor_header *) &header_desc,
- (struct usb_descriptor_header *) &call_mgmt_descriptor,
- (struct usb_descriptor_header *) &rndis_acm_descriptor,
- (struct usb_descriptor_header *) &rndis_union_desc,
- (struct usb_descriptor_header *) &ss_notify_desc,
- (struct usb_descriptor_header *) &ss_intr_comp_desc,
-
- /* data interface has no altsetting */
- (struct usb_descriptor_header *) &rndis_data_intf,
- (struct usb_descriptor_header *) &ss_in_desc,
- (struct usb_descriptor_header *) &ss_bulk_comp_desc,
- (struct usb_descriptor_header *) &ss_out_desc,
- (struct usb_descriptor_header *) &ss_bulk_comp_desc,
- NULL,
-};
+USB_COMPOSITE_ENDPOINT(ep_notify, &fs_notify_desc, &hs_notify_desc,
+ &ss_notify_desc, &ss_intr_comp_desc);
+USB_COMPOSITE_ENDPOINT(ep_in, &fs_in_desc, &hs_in_desc,
+ &ss_in_desc, &ss_bulk_comp_desc);
+USB_COMPOSITE_ENDPOINT(ep_out, &fs_out_desc, &hs_out_desc,
+ &ss_out_desc, &ss_bulk_comp_desc);
+
+USB_COMPOSITE_ALTSETTING(intf0alt0, &rndis_control_intf, &ep_notify);
+USB_COMPOSITE_ALTSETTING(intf1alt0, &rndis_data_intf, &ep_in, &ep_out);
+
+USB_COMPOSITE_INTERFACE(intf0, &intf0alt0);
+USB_COMPOSITE_INTERFACE(intf1, &intf1alt0);
+
+USB_COMPOSITE_DESCRIPTORS(rndis_descs, &intf0, &intf1);

/* string descriptors: */

@@ -542,36 +500,35 @@ static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt)

/* we know alt == 0 */

- if (intf == rndis->ctrl_id) {
+ if (intf == 0) {
VDBG(cdev, "reset rndis control %d\n", intf);
- usb_ep_disable(rndis->notify);

- if (!rndis->notify->desc) {
- VDBG(cdev, "init rndis ctrl %d\n", intf);
- if (config_ep_by_speed(cdev->gadget, f, rndis->notify))
- goto fail;
- }
- usb_ep_enable(rndis->notify);
-
- } else if (intf == rndis->data_id) {
- struct net_device *net;
+ rndis->notify = usb_function_get_ep(f, intf, 0);
+ if (!rndis->notify)
+ return -ENODEV;

- if (rndis->port.in_ep->enabled) {
- DBG(cdev, "reset rndis\n");
- gether_disconnect(&rndis->port);
+ /* allocate notification request and buffer */
+ rndis->notify_req = usb_ep_alloc_request(rndis->notify,
+ GFP_KERNEL);
+ if (!rndis->notify_req)
+ return -ENOMEM;
+ rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL);
+ if (!rndis->notify_req->buf) {
+ usb_ep_free_request(rndis->notify, rndis->notify_req);
+ return -ENOMEM;
}
+ rndis->notify_req->length = STATUS_BYTECOUNT;
+ rndis->notify_req->context = rndis;
+ rndis->notify_req->complete = rndis_response_complete;
+ } else if (intf == 1) {
+ struct net_device *net;

- if (!rndis->port.in_ep->desc || !rndis->port.out_ep->desc) {
- DBG(cdev, "init rndis\n");
- if (config_ep_by_speed(cdev->gadget, f,
- rndis->port.in_ep) ||
- config_ep_by_speed(cdev->gadget, f,
- rndis->port.out_ep)) {
- rndis->port.in_ep->desc = NULL;
- rndis->port.out_ep->desc = NULL;
- goto fail;
- }
- }
+ rndis->port.in_ep = usb_function_get_ep(f, intf, 0);
+ if (!rndis->port.in_ep)
+ return -ENODEV;
+ rndis->port.out_ep = usb_function_get_ep(f, intf, 1);
+ if (!rndis->port.out_ep)
+ return -ENODEV;

/* Avoid ZLPs; they can be troublesome. */
rndis->port.is_zlp_ok = false;
@@ -597,28 +554,25 @@ static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt)

rndis_set_param_dev(rndis->params, net,
&rndis->port.cdc_filter);
- } else
- goto fail;
+ }

return 0;
-fail:
- return -EINVAL;
}

-static void rndis_disable(struct usb_function *f)
+static void rndis_clear_alt(struct usb_function *f,
+ unsigned intf, unsigned alt)
{
struct f_rndis *rndis = func_to_rndis(f);
struct usb_composite_dev *cdev = f->config->cdev;

- if (!rndis->notify->enabled)
- return;
-
- DBG(cdev, "rndis deactivated\n");
-
- rndis_uninit(rndis->params);
- gether_disconnect(&rndis->port);
-
- usb_ep_disable(rndis->notify);
+ if (intf == 0) {
+ kfree(rndis->notify_req->buf);
+ usb_ep_free_request(rndis->notify, rndis->notify_req);
+ } else if (intf == 1) {
+ DBG(cdev, "rndis deactivated\n");
+ rndis_uninit(rndis->params);
+ gether_disconnect(&rndis->port);
+ }
}

/*-------------------------------------------------------------------------*/
@@ -663,22 +617,19 @@ static inline bool can_support_rndis(struct usb_configuration *c)

/* ethernet function driver setup/binding */

-static int
-rndis_bind(struct usb_configuration *c, struct usb_function *f)
+static int rndis_prep_descs(struct usb_function *f)
{
- struct usb_composite_dev *cdev = c->cdev;
struct f_rndis *rndis = func_to_rndis(f);
- struct usb_string *us;
+ struct f_rndis_opts *rndis_opts;
+ struct usb_composite_dev *cdev = f->config->cdev;
+ struct usb_string *us;
int status;
- struct usb_ep *ep;

- struct f_rndis_opts *rndis_opts;
+ rndis_opts = container_of(f->fi, struct f_rndis_opts, func_inst);

- if (!can_support_rndis(c))
+ if (!can_support_rndis(f->config))
return -EINVAL;

- rndis_opts = container_of(f->fi, struct f_rndis_opts, func_inst);
-
if (cdev->use_os_string) {
f->os_desc_table = kzalloc(sizeof(*f->os_desc_table),
GFP_KERNEL);
@@ -688,21 +639,6 @@ rndis_bind(struct usb_configuration *c, struct usb_function *f)
f->os_desc_table[0].os_desc = &rndis_opts->rndis_os_desc;
}

- /*
- * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
- * configurations are bound in sequence with list_for_each_entry,
- * in each configuration its functions are bound in sequence
- * with list_for_each_entry, so we assume no race condition
- * with regard to rndis_opts->bound access
- */
- if (!rndis_opts->bound) {
- gether_set_gadget(rndis_opts->net, cdev->gadget);
- status = gether_register_netdev(rndis_opts->net);
- if (status)
- goto fail;
- rndis_opts->bound = true;
- }
-
us = usb_gstrings_attach(cdev, rndis_strings,
ARRAY_SIZE(rndis_string_defs));
if (IS_ERR(us)) {
@@ -713,79 +649,72 @@ rndis_bind(struct usb_configuration *c, struct usb_function *f)
rndis_data_intf.iInterface = us[1].id;
rndis_iad_descriptor.iFunction = us[2].id;

- /* allocate instance-specific interface IDs */
- status = usb_interface_id(c, f);
- if (status < 0)
- goto fail;
- rndis->ctrl_id = status;
- rndis_iad_descriptor.bFirstInterface = status;
-
- rndis_control_intf.bInterfaceNumber = status;
- rndis_union_desc.bMasterInterface0 = status;
+ return usb_function_set_descs(f, &rndis_descs);

- if (cdev->use_os_string)
- f->os_desc_table[0].if_id =
- rndis_iad_descriptor.bFirstInterface;
+fail:
+ kfree(f->os_desc_table);
+ f->os_desc_n = 0;

- status = usb_interface_id(c, f);
- if (status < 0)
- goto fail;
- rndis->data_id = status;
+ if (rndis->notify_req) {
+ kfree(rndis->notify_req->buf);
+ usb_ep_free_request(rndis->notify, rndis->notify_req);
+ }

- rndis_data_intf.bInterfaceNumber = status;
- rndis_union_desc.bSlaveInterface0 = status;
+ ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);

- status = -ENODEV;
+ return status;
+}

- /* allocate instance-specific endpoints */
- ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc);
- if (!ep)
- goto fail;
- rndis->port.in_ep = ep;
+static int rndis_prep_vendor_descs(struct usb_function *f)
+{
+ struct f_rndis *rndis = func_to_rndis(f);
+ struct f_rndis_opts *rndis_opts;
+ struct usb_composite_dev *cdev = f->config->cdev;
+ int status, intf0_id, intf1_id;

- ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
- if (!ep)
- goto fail;
- rndis->port.out_ep = ep;
+ rndis_opts = container_of(f->fi, struct f_rndis_opts, func_inst);

- /* NOTE: a status/notification endpoint is, strictly speaking,
- * optional. We don't treat it that way though! It's simpler,
- * and some newer profiles don't treat it as optional.
+ /*
+ * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
+ * configurations are bound in sequence with list_for_each_entry,
+ * in each configuration its functions are bound in sequence
+ * with list_for_each_entry, so we assume no race condition
+ * with regard to rndis_opts->bound access
*/
- ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc);
- if (!ep)
- goto fail;
- rndis->notify = ep;
+ if (!rndis_opts->bound) {
+ gether_set_gadget(rndis_opts->net, cdev->gadget);
+ status = gether_register_netdev(rndis_opts->net);
+ if (status)
+ return status;
+ rndis_opts->bound = true;
+ }

- status = -ENOMEM;
+ intf0_id = usb_get_interface_id(f, 0);
+ intf1_id = usb_get_interface_id(f, 1);

- /* allocate notification request and buffer */
- rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
- if (!rndis->notify_req)
- goto fail;
- rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL);
- if (!rndis->notify_req->buf)
- goto fail;
- rndis->notify_req->length = STATUS_BYTECOUNT;
- rndis->notify_req->context = rndis;
- rndis->notify_req->complete = rndis_response_complete;
+ rndis->ctrl_id = intf0_id;
+ rndis->data_id = intf1_id;

- /* support all relevant hardware speeds... we expect that when
- * hardware is dual speed, all bulk-capable endpoints work at
- * both speeds
- */
- hs_in_desc.bEndpointAddress = fs_in_desc.bEndpointAddress;
- hs_out_desc.bEndpointAddress = fs_out_desc.bEndpointAddress;
- hs_notify_desc.bEndpointAddress = fs_notify_desc.bEndpointAddress;
+ rndis_iad_descriptor.bFirstInterface = intf0_id;

- ss_in_desc.bEndpointAddress = fs_in_desc.bEndpointAddress;
- ss_out_desc.bEndpointAddress = fs_out_desc.bEndpointAddress;
- ss_notify_desc.bEndpointAddress = fs_notify_desc.bEndpointAddress;
+ rndis_union_desc.bMasterInterface0 = intf0_id;
+ rndis_union_desc.bSlaveInterface0 = intf1_id;

- status = usb_assign_descriptors(f, eth_fs_function, eth_hs_function,
- eth_ss_function);
- if (status)
- goto fail;
+ if (cdev->use_os_string)
+ f->os_desc_table[0].if_id =
+ rndis_iad_descriptor.bFirstInterface;
+
+ usb_function_add_vendor_desc(f,
+ (struct usb_descriptor_header *)&rndis_iad_descriptor);
+
+ usb_altset_add_vendor_desc(f, 0, 0,
+ (struct usb_descriptor_header *)&header_desc);
+ usb_altset_add_vendor_desc(f, 0, 0,
+ (struct usb_descriptor_header *)&call_mgmt_descriptor);
+ usb_altset_add_vendor_desc(f, 0, 0,
+ (struct usb_descriptor_header *)&rndis_acm_descriptor);
+ usb_altset_add_vendor_desc(f, 0, 0,
+ (struct usb_descriptor_header *)&rndis_union_desc);

rndis->port.open = rndis_open;
rndis->port.close = rndis_close;
@@ -796,8 +725,7 @@ rndis_bind(struct usb_configuration *c, struct usb_function *f)
if (rndis->manufacturer && rndis->vendorID &&
rndis_set_param_vendor(rndis->params, rndis->vendorID,
rndis->manufacturer)) {
- status = -EINVAL;
- goto fail_free_descs;
+ return -EINVAL;
}

/* NOTE: all that is done without knowing or caring about
@@ -805,27 +733,7 @@ rndis_bind(struct usb_configuration *c, struct usb_function *f)
* until we're activated via set_alt().
*/

- DBG(cdev, "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n",
- gadget_is_superspeed(c->cdev->gadget) ? "super" :
- gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
- rndis->port.in_ep->name, rndis->port.out_ep->name,
- rndis->notify->name);
return 0;
-
-fail_free_descs:
- usb_free_all_descriptors(f);
-fail:
- kfree(f->os_desc_table);
- f->os_desc_n = 0;
-
- if (rndis->notify_req) {
- kfree(rndis->notify_req->buf);
- usb_ep_free_request(rndis->notify, rndis->notify_req);
- }
-
- ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
-
- return status;
}

void rndis_borrow_net(struct usb_function_instance *f, struct net_device *net)
@@ -940,14 +848,8 @@ static void rndis_free(struct usb_function *f)

static void rndis_unbind(struct usb_configuration *c, struct usb_function *f)
{
- struct f_rndis *rndis = func_to_rndis(f);
-
kfree(f->os_desc_table);
f->os_desc_n = 0;
- usb_free_all_descriptors(f);
-
- kfree(rndis->notify_req->buf);
- usb_ep_free_request(rndis->notify, rndis->notify_req);
}

static struct usb_function *rndis_alloc(struct usb_function_instance *fi)
@@ -981,11 +883,12 @@ static struct usb_function *rndis_alloc(struct usb_function_instance *fi)

rndis->port.func.name = "rndis";
/* descriptors are per-instance copies */
- rndis->port.func.bind = rndis_bind;
+ rndis->port.func.prep_descs = rndis_prep_descs;
+ rndis->port.func.prep_vendor_descs = rndis_prep_vendor_descs;
rndis->port.func.unbind = rndis_unbind;
rndis->port.func.set_alt = rndis_set_alt;
+ rndis->port.func.clear_alt = rndis_clear_alt;
rndis->port.func.setup = rndis_setup;
- rndis->port.func.disable = rndis_disable;
rndis->port.func.free_func = rndis_free;

params = rndis_register(rndis_response_available, rndis);
--
1.9.1

2015-11-27 11:03:54

by Robert Baldyga

[permalink] [raw]
Subject: [PATCH v2 23/29] usb: gadget: f_hid: handle requests lifetime properly

So far USB requests allocated in hidg_set_alt() were not freed. Now we
free them in case of hidg_set_alt() failure (when we are not able to
allocate and enqueue all the requests) or in hidg_disable() function.

Signed-off-by: Robert Baldyga <[email protected]>
---
drivers/usb/gadget/function/f_hid.c | 22 +++++++++++++++++++++-
1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/gadget/function/f_hid.c b/drivers/usb/gadget/function/f_hid.c
index 99285b4..0456a53 100644
--- a/drivers/usb/gadget/function/f_hid.c
+++ b/drivers/usb/gadget/function/f_hid.c
@@ -59,6 +59,7 @@ struct f_hidg {
bool write_pending;
wait_queue_head_t write_queue;
struct usb_request *req;
+ struct usb_request **out_reqs;

int minor;
struct cdev cdev;
@@ -490,6 +491,7 @@ static void hidg_disable(struct usb_function *f)
{
struct f_hidg *hidg = func_to_hidg(f);
struct f_hidg_req_list *list, *next;
+ int i;

usb_ep_disable(hidg->in_ep);
usb_ep_disable(hidg->out_ep);
@@ -498,6 +500,12 @@ static void hidg_disable(struct usb_function *f)
list_del(&list->list);
kfree(list);
}
+
+ for (i = 0; i < hidg->qlen; ++i) {
+ kfree(hidg->out_reqs[i]->buf);
+ kfree(hidg->out_reqs[i]);
+ }
+ kfree(hidg->out_reqs);
}

static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
@@ -547,11 +555,14 @@ static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
/*
* allocate a bunch of read buffers and queue them all at once.
*/
+ hidg->out_reqs = kzalloc(hidg->qlen *
+ sizeof(*hidg->out_reqs), GFP_KERNEL);
for (i = 0; i < hidg->qlen && status == 0; i++) {
struct usb_request *req =
hidg_alloc_ep_req(hidg->out_ep,
hidg->report_length);
if (req) {
+ hidg->out_reqs[i] = req;
req->complete = hidg_set_report_complete;
req->context = hidg;
status = usb_ep_queue(hidg->out_ep, req,
@@ -562,11 +573,20 @@ static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
} else {
usb_ep_disable(hidg->out_ep);
status = -ENOMEM;
- goto fail;
+ goto free_req;
}
}
}

+free_req:
+ if (status < 0) {
+ while (i--) {
+ kfree(hidg->out_reqs[i]->buf);
+ kfree(hidg->out_reqs[i]);
+ }
+ kfree(hidg->out_reqs);
+ }
+
fail:
return status;
}
--
1.9.1

2015-11-27 11:03:25

by Robert Baldyga

[permalink] [raw]
Subject: [PATCH v2 24/29] usb: gadget: f_hid: conversion to new API

Generate descriptors in new format and attach them to USB function in
prep_descs(). Implement prep_vendor_descs() to supply class specific
descriptors. Change set_alt() implementation and implement clear_alt()
operation. Move cdev initialization to hidg_alloc(). Remove boilerplate
code.

Signed-off-by: Robert Baldyga <[email protected]>
---
drivers/usb/gadget/function/f_hid.c | 305 ++++++++++++++----------------------
1 file changed, 119 insertions(+), 186 deletions(-)

diff --git a/drivers/usb/gadget/function/f_hid.c b/drivers/usb/gadget/function/f_hid.c
index 0456a53..8770289 100644
--- a/drivers/usb/gadget/function/f_hid.c
+++ b/drivers/usb/gadget/function/f_hid.c
@@ -125,14 +125,6 @@ static struct usb_endpoint_descriptor hidg_hs_out_ep_desc = {
*/
};

-static struct usb_descriptor_header *hidg_hs_descriptors[] = {
- (struct usb_descriptor_header *)&hidg_interface_desc,
- (struct usb_descriptor_header *)&hidg_desc,
- (struct usb_descriptor_header *)&hidg_hs_in_ep_desc,
- (struct usb_descriptor_header *)&hidg_hs_out_ep_desc,
- NULL,
-};
-
/* Full-Speed Support */

static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = {
@@ -159,13 +151,16 @@ static struct usb_endpoint_descriptor hidg_fs_out_ep_desc = {
*/
};

-static struct usb_descriptor_header *hidg_fs_descriptors[] = {
- (struct usb_descriptor_header *)&hidg_interface_desc,
- (struct usb_descriptor_header *)&hidg_desc,
- (struct usb_descriptor_header *)&hidg_fs_in_ep_desc,
- (struct usb_descriptor_header *)&hidg_fs_out_ep_desc,
- NULL,
-};
+USB_COMPOSITE_ENDPOINT(ep_in, &hidg_fs_in_ep_desc,
+ &hidg_hs_in_ep_desc, NULL, NULL);
+USB_COMPOSITE_ENDPOINT(ep_out, &hidg_fs_out_ep_desc,
+ &hidg_hs_out_ep_desc, NULL, NULL);
+
+USB_COMPOSITE_ALTSETTING(intf0alt0, &hidg_interface_desc, &ep_in, &ep_out);
+
+USB_COMPOSITE_INTERFACE(intf0, &intf0alt0);
+
+USB_COMPOSITE_DESCRIPTORS(hidg_descs, &intf0);

/*-------------------------------------------------------------------------*/
/* Strings */
@@ -487,27 +482,6 @@ respond:
return status;
}

-static void hidg_disable(struct usb_function *f)
-{
- struct f_hidg *hidg = func_to_hidg(f);
- struct f_hidg_req_list *list, *next;
- int i;
-
- usb_ep_disable(hidg->in_ep);
- usb_ep_disable(hidg->out_ep);
-
- list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) {
- list_del(&list->list);
- kfree(list);
- }
-
- for (i = 0; i < hidg->qlen; ++i) {
- kfree(hidg->out_reqs[i]->buf);
- kfree(hidg->out_reqs[i]);
- }
- kfree(hidg->out_reqs);
-}
-
static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
{
struct usb_composite_dev *cdev = f->config->cdev;
@@ -516,65 +490,46 @@ static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)

VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);

- if (hidg->in_ep != NULL) {
- /* restart endpoint */
- usb_ep_disable(hidg->in_ep);
+ hidg->in_ep = usb_function_get_ep(f, intf, 0);
+ if (!hidg->in_ep)
+ return -ENODEV;
+ hidg->in_ep->driver_data = hidg;

- status = config_ep_by_speed(f->config->cdev->gadget, f,
- hidg->in_ep);
- if (status) {
- ERROR(cdev, "config_ep_by_speed FAILED!\n");
- goto fail;
- }
- status = usb_ep_enable(hidg->in_ep);
- if (status < 0) {
- ERROR(cdev, "Enable IN endpoint FAILED!\n");
- goto fail;
- }
- hidg->in_ep->driver_data = hidg;
- }
+ hidg->out_ep = usb_function_get_ep(f, intf, 1);
+ if (!hidg->out_ep)
+ return -ENODEV;
+ hidg->out_ep->driver_data = hidg;

+ /* preallocate request and buffer */
+ hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL);
+ if (!hidg->req)
+ return -ENOMEM;

- if (hidg->out_ep != NULL) {
- /* restart endpoint */
- usb_ep_disable(hidg->out_ep);
+ hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL);
+ if (!hidg->req->buf)
+ return -ENOMEM;

- status = config_ep_by_speed(f->config->cdev->gadget, f,
- hidg->out_ep);
- if (status) {
- ERROR(cdev, "config_ep_by_speed FAILED!\n");
- goto fail;
- }
- status = usb_ep_enable(hidg->out_ep);
- if (status < 0) {
- ERROR(cdev, "Enable IN endpoint FAILED!\n");
- goto fail;
- }
- hidg->out_ep->driver_data = hidg;
-
- /*
- * allocate a bunch of read buffers and queue them all at once.
- */
- hidg->out_reqs = kzalloc(hidg->qlen *
- sizeof(*hidg->out_reqs), GFP_KERNEL);
- for (i = 0; i < hidg->qlen && status == 0; i++) {
- struct usb_request *req =
- hidg_alloc_ep_req(hidg->out_ep,
- hidg->report_length);
- if (req) {
+ /*
+ * allocate a bunch of read buffers and queue them all at once.
+ */
+ hidg->out_reqs = kzalloc(hidg->qlen *
+ sizeof(*hidg->out_reqs), GFP_KERNEL);
+ for (i = 0; i < hidg->qlen && status == 0; i++) {
+ struct usb_request *req =
+ hidg_alloc_ep_req(hidg->out_ep,
+ hidg->report_length);
+ if (req) {
hidg->out_reqs[i] = req;
- req->complete = hidg_set_report_complete;
- req->context = hidg;
- status = usb_ep_queue(hidg->out_ep, req,
- GFP_ATOMIC);
- if (status)
- ERROR(cdev, "%s queue req --> %d\n",
+ req->complete = hidg_set_report_complete;
+ req->context = hidg;
+ status = usb_ep_queue(hidg->out_ep, req,
+ GFP_ATOMIC);
+ if (status)
+ ERROR(cdev, "%s queue req --> %d\n",
hidg->out_ep->name, status);
- } else {
- usb_ep_disable(hidg->out_ep);
- status = -ENOMEM;
- goto free_req;
- }
+ } else {
+ status = -ENOMEM;
+ goto free_req;
}
}

@@ -587,10 +542,31 @@ free_req:
kfree(hidg->out_reqs);
}

-fail:
return status;
}

+static void hidg_clear_alt(struct usb_function *f, unsigned intf, unsigned alt)
+{
+ struct f_hidg *hidg = func_to_hidg(f);
+ struct f_hidg_req_list *list, *next;
+ int i;
+
+ list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) {
+ list_del(&list->list);
+ kfree(list);
+ }
+
+ for (i = 0; i < hidg->qlen; ++i) {
+ kfree(hidg->out_reqs[i]->buf);
+ kfree(hidg->out_reqs[i]);
+ }
+ kfree(hidg->out_reqs);
+
+ /* disable/free request and end point */
+ kfree(hidg->req->buf);
+ usb_ep_free_request(hidg->in_ep, hidg->req);
+}
+
static const struct file_operations f_hidg_fops = {
.owner = THIS_MODULE,
.open = f_hidg_open,
@@ -601,50 +577,19 @@ static const struct file_operations f_hidg_fops = {
.llseek = noop_llseek,
};

-static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
+static int hidg_prep_descs(struct usb_function *f)
{
- struct usb_ep *ep;
+ struct usb_composite_dev *cdev = f->config->cdev;
struct f_hidg *hidg = func_to_hidg(f);
struct usb_string *us;
- struct device *device;
- int status;
- dev_t dev;

/* maybe allocate device-global string IDs, and patch descriptors */
- us = usb_gstrings_attach(c->cdev, ct_func_strings,
+ us = usb_gstrings_attach(cdev, ct_func_strings,
ARRAY_SIZE(ct_func_string_defs));
if (IS_ERR(us))
return PTR_ERR(us);
hidg_interface_desc.iInterface = us[CT_FUNC_HID_IDX].id;

- /* allocate instance-specific interface IDs, and patch descriptors */
- status = usb_interface_id(c, f);
- if (status < 0)
- goto fail;
- hidg_interface_desc.bInterfaceNumber = status;
-
- /* allocate instance-specific endpoints */
- status = -ENODEV;
- ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
- if (!ep)
- goto fail;
- hidg->in_ep = ep;
-
- ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc);
- if (!ep)
- goto fail;
- hidg->out_ep = ep;
-
- /* preallocate request and buffer */
- status = -ENOMEM;
- hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL);
- if (!hidg->req)
- goto fail;
-
- hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL);
- if (!hidg->req->buf)
- goto fail;
-
/* set descriptor dynamic values */
hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
@@ -652,6 +597,14 @@ static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
+
+ return usb_function_set_descs(f, &hidg_descs);
+}
+
+static int hidg_prep_vendor_descs(struct usb_function *f)
+{
+ struct f_hidg *hidg = func_to_hidg(f);
+
/*
* We can use hidg_desc struct here but we should not relay
* that its content won't change after returning from this function.
@@ -660,50 +613,10 @@ static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
hidg_desc.desc[0].wDescriptorLength =
cpu_to_le16(hidg->report_desc_length);

- hidg_hs_in_ep_desc.bEndpointAddress =
- hidg_fs_in_ep_desc.bEndpointAddress;
- hidg_hs_out_ep_desc.bEndpointAddress =
- hidg_fs_out_ep_desc.bEndpointAddress;
-
- status = usb_assign_descriptors(f, hidg_fs_descriptors,
- hidg_hs_descriptors, NULL);
- if (status)
- goto fail;
-
- mutex_init(&hidg->lock);
- spin_lock_init(&hidg->spinlock);
- init_waitqueue_head(&hidg->write_queue);
- init_waitqueue_head(&hidg->read_queue);
- INIT_LIST_HEAD(&hidg->completed_out_req);
-
- /* create char device */
- cdev_init(&hidg->cdev, &f_hidg_fops);
- dev = MKDEV(major, hidg->minor);
- status = cdev_add(&hidg->cdev, dev, 1);
- if (status)
- goto fail_free_descs;
-
- device = device_create(hidg_class, NULL, dev, NULL,
- "%s%d", "hidg", hidg->minor);
- if (IS_ERR(device)) {
- status = PTR_ERR(device);
- goto del;
- }
+ usb_altset_add_vendor_desc(f, 0, 0,
+ (struct usb_descriptor_header *)&hidg_desc);

return 0;
-del:
- cdev_del(&hidg->cdev);
-fail_free_descs:
- usb_free_all_descriptors(f);
-fail:
- ERROR(f->config->cdev, "hidg_bind FAILED\n");
- if (hidg->req != NULL) {
- kfree(hidg->req->buf);
- if (hidg->in_ep != NULL)
- usb_ep_free_request(hidg->in_ep, hidg->req);
- }
-
- return status;
}

static inline int hidg_get_minor(void)
@@ -914,6 +827,10 @@ static void hidg_free(struct usb_function *f)

hidg = func_to_hidg(f);
opts = container_of(f->fi, struct f_hid_opts, func_inst);
+
+ device_destroy(hidg_class, MKDEV(major, hidg->minor));
+ cdev_del(&hidg->cdev);
+
kfree(hidg->report_desc);
kfree(hidg);
mutex_lock(&opts->lock);
@@ -921,31 +838,25 @@ static void hidg_free(struct usb_function *f)
mutex_unlock(&opts->lock);
}

-static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
-{
- struct f_hidg *hidg = func_to_hidg(f);
-
- device_destroy(hidg_class, MKDEV(major, hidg->minor));
- cdev_del(&hidg->cdev);
-
- /* disable/free request and end point */
- usb_ep_disable(hidg->in_ep);
- kfree(hidg->req->buf);
- usb_ep_free_request(hidg->in_ep, hidg->req);
-
- usb_free_all_descriptors(f);
-}
-
static struct usb_function *hidg_alloc(struct usb_function_instance *fi)
{
struct f_hidg *hidg;
struct f_hid_opts *opts;
+ struct device *device;
+ dev_t dev;
+ int ret;

/* allocate and initialize one new instance */
hidg = kzalloc(sizeof(*hidg), GFP_KERNEL);
if (!hidg)
return ERR_PTR(-ENOMEM);

+ mutex_init(&hidg->lock);
+ spin_lock_init(&hidg->spinlock);
+ init_waitqueue_head(&hidg->write_queue);
+ init_waitqueue_head(&hidg->read_queue);
+ INIT_LIST_HEAD(&hidg->completed_out_req);
+
opts = container_of(fi, struct f_hid_opts, func_inst);

mutex_lock(&opts->lock);
@@ -961,26 +872,48 @@ static struct usb_function *hidg_alloc(struct usb_function_instance *fi)
opts->report_desc_length,
GFP_KERNEL);
if (!hidg->report_desc) {
- kfree(hidg);
mutex_unlock(&opts->lock);
- return ERR_PTR(-ENOMEM);
+ ret = -ENOMEM;
+ goto err_hidg;
}
}

mutex_unlock(&opts->lock);

hidg->func.name = "hid";
- hidg->func.bind = hidg_bind;
- hidg->func.unbind = hidg_unbind;
+ hidg->func.prep_descs = hidg_prep_descs;
+ hidg->func.prep_vendor_descs = hidg_prep_vendor_descs;
hidg->func.set_alt = hidg_set_alt;
- hidg->func.disable = hidg_disable;
+ hidg->func.clear_alt = hidg_clear_alt;
hidg->func.setup = hidg_setup;
hidg->func.free_func = hidg_free;

/* this could me made configurable at some point */
hidg->qlen = 4;

+ /* create char device */
+ cdev_init(&hidg->cdev, &f_hidg_fops);
+ dev = MKDEV(major, hidg->minor);
+ ret = cdev_add(&hidg->cdev, dev, 1);
+ if (ret < 0)
+ goto err_report;
+
+ device = device_create(hidg_class, NULL, dev, NULL,
+ "%s%d", "hidg", hidg->minor);
+ if (IS_ERR(device)) {
+ ret = PTR_ERR(device);
+ goto err_cdev;
+ }
+
return &hidg->func;
+
+err_cdev:
+ cdev_del(&hidg->cdev);
+err_report:
+ kfree(hidg->report_desc);
+err_hidg:
+ kfree(hidg);
+ return ERR_PTR(ret);
}

DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc);
--
1.9.1

2015-11-27 11:03:03

by Robert Baldyga

[permalink] [raw]
Subject: [PATCH v2 25/29] usb: gadget: f_acm: conversion to new API

Generate descriptors in new format and attach them to USB function in
prep_descs(). Implement prep_vendor_descs() to supply class specific
descriptors. Change set_alt() implementation and implement clear_alt()
operation. Remove boilerplate code.

Signed-off-by: Robert Baldyga <[email protected]>
---
drivers/usb/gadget/function/f_acm.c | 248 ++++++++++++------------------------
1 file changed, 78 insertions(+), 170 deletions(-)

diff --git a/drivers/usb/gadget/function/f_acm.c b/drivers/usb/gadget/function/f_acm.c
index 2fa1e80..0d3fe1a 100644
--- a/drivers/usb/gadget/function/f_acm.c
+++ b/drivers/usb/gadget/function/f_acm.c
@@ -185,20 +185,6 @@ static struct usb_endpoint_descriptor acm_fs_out_desc = {
.bmAttributes = USB_ENDPOINT_XFER_BULK,
};

-static struct usb_descriptor_header *acm_fs_function[] = {
- (struct usb_descriptor_header *) &acm_iad_descriptor,
- (struct usb_descriptor_header *) &acm_control_interface_desc,
- (struct usb_descriptor_header *) &acm_header_desc,
- (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
- (struct usb_descriptor_header *) &acm_descriptor,
- (struct usb_descriptor_header *) &acm_union_desc,
- (struct usb_descriptor_header *) &acm_fs_notify_desc,
- (struct usb_descriptor_header *) &acm_data_interface_desc,
- (struct usb_descriptor_header *) &acm_fs_in_desc,
- (struct usb_descriptor_header *) &acm_fs_out_desc,
- NULL,
-};
-
/* high speed support: */
static struct usb_endpoint_descriptor acm_hs_notify_desc = {
.bLength = USB_DT_ENDPOINT_SIZE,
@@ -223,20 +209,6 @@ static struct usb_endpoint_descriptor acm_hs_out_desc = {
.wMaxPacketSize = cpu_to_le16(512),
};

-static struct usb_descriptor_header *acm_hs_function[] = {
- (struct usb_descriptor_header *) &acm_iad_descriptor,
- (struct usb_descriptor_header *) &acm_control_interface_desc,
- (struct usb_descriptor_header *) &acm_header_desc,
- (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
- (struct usb_descriptor_header *) &acm_descriptor,
- (struct usb_descriptor_header *) &acm_union_desc,
- (struct usb_descriptor_header *) &acm_hs_notify_desc,
- (struct usb_descriptor_header *) &acm_data_interface_desc,
- (struct usb_descriptor_header *) &acm_hs_in_desc,
- (struct usb_descriptor_header *) &acm_hs_out_desc,
- NULL,
-};
-
static struct usb_endpoint_descriptor acm_ss_in_desc = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
@@ -256,22 +228,20 @@ static struct usb_ss_ep_comp_descriptor acm_ss_bulk_comp_desc = {
.bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
};

-static struct usb_descriptor_header *acm_ss_function[] = {
- (struct usb_descriptor_header *) &acm_iad_descriptor,
- (struct usb_descriptor_header *) &acm_control_interface_desc,
- (struct usb_descriptor_header *) &acm_header_desc,
- (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
- (struct usb_descriptor_header *) &acm_descriptor,
- (struct usb_descriptor_header *) &acm_union_desc,
- (struct usb_descriptor_header *) &acm_hs_notify_desc,
- (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
- (struct usb_descriptor_header *) &acm_data_interface_desc,
- (struct usb_descriptor_header *) &acm_ss_in_desc,
- (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
- (struct usb_descriptor_header *) &acm_ss_out_desc,
- (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
- NULL,
-};
+USB_COMPOSITE_ENDPOINT(ep_notify, &acm_fs_notify_desc, &acm_hs_notify_desc,
+ &acm_hs_notify_desc, &acm_ss_bulk_comp_desc);
+USB_COMPOSITE_ENDPOINT(ep_in, &acm_fs_in_desc, &acm_hs_in_desc,
+ &acm_ss_in_desc, &acm_ss_bulk_comp_desc);
+USB_COMPOSITE_ENDPOINT(ep_out, &acm_fs_out_desc, &acm_hs_out_desc,
+ &acm_ss_out_desc, &acm_ss_bulk_comp_desc);
+
+USB_COMPOSITE_ALTSETTING(intf0alt0, &acm_control_interface_desc, &ep_notify);
+USB_COMPOSITE_ALTSETTING(intf1alt0, &acm_data_interface_desc, &ep_in, &ep_out);
+
+USB_COMPOSITE_INTERFACE(intf0, &intf0alt0);
+USB_COMPOSITE_INTERFACE(intf1, &intf1alt0);
+
+USB_COMPOSITE_DESCRIPTORS(acm_descs, &intf0, &intf1);

/* string descriptors: */

@@ -420,6 +390,8 @@ invalid:
return value;
}

+static void acm_cdc_notify_complete(struct usb_ep *ep, struct usb_request *req);
+
static int acm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
{
struct f_acm *acm = func_to_acm(f);
@@ -427,51 +399,49 @@ static int acm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)

/* we know alt == 0, so this is an activation or a reset */

- if (intf == acm->ctrl_id) {
+ if (intf == 0) {
dev_vdbg(&cdev->gadget->dev,
"reset acm control interface %d\n", intf);
- usb_ep_disable(acm->notify);
-
- if (!acm->notify->desc)
- if (config_ep_by_speed(cdev->gadget, f, acm->notify))
- return -EINVAL;
-
- usb_ep_enable(acm->notify);
-
- } else if (intf == acm->data_id) {
- if (acm->notify->enabled) {
- dev_dbg(&cdev->gadget->dev,
- "reset acm ttyGS%d\n", acm->port_num);
- gserial_disconnect(&acm->port);
- }
- if (!acm->port.in->desc || !acm->port.out->desc) {
- dev_dbg(&cdev->gadget->dev,
+
+ acm->notify = usb_function_get_ep(f, intf, 0);
+ if (!acm->notify)
+ return -ENODEV;
+
+ /* allocate notification */
+ acm->notify_req = gs_alloc_req(acm->notify,
+ sizeof(struct usb_cdc_notification) + 2,
+ GFP_KERNEL);
+ if (!acm->notify_req)
+ return -ENOMEM;
+
+ acm->notify_req->complete = acm_cdc_notify_complete;
+ acm->notify_req->context = acm;
+ } else if (intf == 1) {
+ dev_dbg(&cdev->gadget->dev,
"activate acm ttyGS%d\n", acm->port_num);
- if (config_ep_by_speed(cdev->gadget, f,
- acm->port.in) ||
- config_ep_by_speed(cdev->gadget, f,
- acm->port.out)) {
- acm->port.in->desc = NULL;
- acm->port.out->desc = NULL;
- return -EINVAL;
- }
- }
- gserial_connect(&acm->port, acm->port_num);

- } else
- return -EINVAL;
+ acm->port.in = usb_function_get_ep(f, intf, 0);
+ if (!acm->port.in)
+ return -ENODEV;
+ acm->port.out = usb_function_get_ep(f, intf, 1);
+ if (!acm->port.out)
+ return -ENODEV;
+
+ gserial_connect(&acm->port, acm->port_num);
+ }

return 0;
}

-static void acm_disable(struct usb_function *f)
+static void acm_clear_alt(struct usb_function *f, unsigned intf, unsigned alt)
{
struct f_acm *acm = func_to_acm(f);
struct usb_composite_dev *cdev = f->config->cdev;

dev_dbg(&cdev->gadget->dev, "acm ttyGS%d deactivated\n", acm->port_num);
gserial_disconnect(&acm->port);
- usb_ep_disable(acm->notify);
+
+ gs_free_req(acm->notify, acm->notify_req);
}

/*-------------------------------------------------------------------------*/
@@ -602,15 +572,10 @@ static int acm_send_break(struct gserial *port, int duration)

/*-------------------------------------------------------------------------*/

-/* ACM function driver setup/binding */
-static int
-acm_bind(struct usb_configuration *c, struct usb_function *f)
+static int acm_prep_descs(struct usb_function *f)
{
- struct usb_composite_dev *cdev = c->cdev;
- struct f_acm *acm = func_to_acm(f);
+ struct usb_composite_dev *cdev = f->config->cdev;
struct usb_string *us;
- int status;
- struct usb_ep *ep;

/* REVISIT might want instance-specific strings to help
* distinguish instances ...
@@ -625,96 +590,39 @@ acm_bind(struct usb_configuration *c, struct usb_function *f)
acm_data_interface_desc.iInterface = us[ACM_DATA_IDX].id;
acm_iad_descriptor.iFunction = us[ACM_IAD_IDX].id;

- /* allocate instance-specific interface IDs, and patch descriptors */
- status = usb_interface_id(c, f);
- if (status < 0)
- goto fail;
- acm->ctrl_id = status;
- acm_iad_descriptor.bFirstInterface = status;
-
- acm_control_interface_desc.bInterfaceNumber = status;
- acm_union_desc .bMasterInterface0 = status;
-
- status = usb_interface_id(c, f);
- if (status < 0)
- goto fail;
- acm->data_id = status;
-
- acm_data_interface_desc.bInterfaceNumber = status;
- acm_union_desc.bSlaveInterface0 = status;
- acm_call_mgmt_descriptor.bDataInterface = status;
-
- status = -ENODEV;
-
- /* allocate instance-specific endpoints */
- ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_in_desc);
- if (!ep)
- goto fail;
- acm->port.in = ep;
-
- ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_out_desc);
- if (!ep)
- goto fail;
- acm->port.out = ep;
-
- ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_notify_desc);
- if (!ep)
- goto fail;
- acm->notify = ep;
-
- /* allocate notification */
- acm->notify_req = gs_alloc_req(ep,
- sizeof(struct usb_cdc_notification) + 2,
- GFP_KERNEL);
- if (!acm->notify_req)
- goto fail;
-
- acm->notify_req->complete = acm_cdc_notify_complete;
- acm->notify_req->context = acm;
-
- /* support all relevant hardware speeds... we expect that when
- * hardware is dual speed, all bulk-capable endpoints work at
- * both speeds
- */
- acm_hs_in_desc.bEndpointAddress = acm_fs_in_desc.bEndpointAddress;
- acm_hs_out_desc.bEndpointAddress = acm_fs_out_desc.bEndpointAddress;
- acm_hs_notify_desc.bEndpointAddress =
- acm_fs_notify_desc.bEndpointAddress;
-
- acm_ss_in_desc.bEndpointAddress = acm_fs_in_desc.bEndpointAddress;
- acm_ss_out_desc.bEndpointAddress = acm_fs_out_desc.bEndpointAddress;
-
- status = usb_assign_descriptors(f, acm_fs_function, acm_hs_function,
- acm_ss_function);
- if (status)
- goto fail;
-
- dev_dbg(&cdev->gadget->dev,
- "acm ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n",
- acm->port_num,
- gadget_is_superspeed(c->cdev->gadget) ? "super" :
- gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
- acm->port.in->name, acm->port.out->name,
- acm->notify->name);
- return 0;
-
-fail:
- if (acm->notify_req)
- gs_free_req(acm->notify, acm->notify_req);
-
- ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
-
- return status;
+ return usb_function_set_descs(f, &acm_descs);
}

-static void acm_unbind(struct usb_configuration *c, struct usb_function *f)
+static int acm_prep_vendor_descs(struct usb_function *f)
{
struct f_acm *acm = func_to_acm(f);
+ int intf0_id, intf1_id;
+
+ intf0_id = usb_get_interface_id(f, 0);
+ intf1_id = usb_get_interface_id(f, 1);
+
+ acm->ctrl_id = intf0_id;
+ acm->data_id = intf1_id;
+
+ acm_iad_descriptor.bFirstInterface = intf0_id;

- acm_string_defs[0].id = 0;
- usb_free_all_descriptors(f);
- if (acm->notify_req)
- gs_free_req(acm->notify, acm->notify_req);
+ acm_call_mgmt_descriptor.bDataInterface = intf1_id;
+ acm_union_desc.bMasterInterface0 = intf0_id;
+ acm_union_desc.bSlaveInterface0 = intf1_id;
+
+ usb_function_add_vendor_desc(f,
+ (struct usb_descriptor_header *)&acm_iad_descriptor);
+
+ usb_altset_add_vendor_desc(f, 0, 0,
+ (struct usb_descriptor_header *)&acm_header_desc);
+ usb_altset_add_vendor_desc(f, 0, 0,
+ (struct usb_descriptor_header *) &acm_call_mgmt_descriptor);
+ usb_altset_add_vendor_desc(f, 0, 0,
+ (struct usb_descriptor_header *)&acm_descriptor);
+ usb_altset_add_vendor_desc(f, 0, 0,
+ (struct usb_descriptor_header *)&acm_union_desc);
+
+ return 0;
}

static void acm_free_func(struct usb_function *f)
@@ -742,14 +650,14 @@ static struct usb_function *acm_alloc_func(struct usb_function_instance *fi)
acm->port.func.name = "acm";
acm->port.func.strings = acm_strings;
/* descriptors are per-instance copies */
- acm->port.func.bind = acm_bind;
+ acm->port.func.prep_descs = acm_prep_descs;
+ acm->port.func.prep_vendor_descs = acm_prep_vendor_descs;
acm->port.func.set_alt = acm_set_alt;
+ acm->port.func.clear_alt = acm_clear_alt;
acm->port.func.setup = acm_setup;
- acm->port.func.disable = acm_disable;

opts = container_of(fi, struct f_serial_opts, func_inst);
acm->port_num = opts->port_num;
- acm->port.func.unbind = acm_unbind;
acm->port.func.free_func = acm_free_func;

return &acm->port.func;
--
1.9.1

2015-11-27 11:02:42

by Robert Baldyga

[permalink] [raw]
Subject: [PATCH v2 26/29] usb: gadget: f_eem: conversion to new API

Generate descriptors in new format and attach them to USB function in
prep_descs(). Implement prep_vendor_descs() to supply class specific
descriptors. Change set_alt() implementation and implement clear_alt()
operation. Remove boilerplate code.

Signed-off-by: Robert Baldyga <[email protected]>
---
drivers/usb/gadget/function/f_eem.c | 154 ++++++++----------------------------
1 file changed, 33 insertions(+), 121 deletions(-)

diff --git a/drivers/usb/gadget/function/f_eem.c b/drivers/usb/gadget/function/f_eem.c
index cad35a5..8896419 100644
--- a/drivers/usb/gadget/function/f_eem.c
+++ b/drivers/usb/gadget/function/f_eem.c
@@ -73,14 +73,6 @@ static struct usb_endpoint_descriptor eem_fs_out_desc = {
.bmAttributes = USB_ENDPOINT_XFER_BULK,
};

-static struct usb_descriptor_header *eem_fs_function[] = {
- /* CDC EEM control descriptors */
- (struct usb_descriptor_header *) &eem_intf,
- (struct usb_descriptor_header *) &eem_fs_in_desc,
- (struct usb_descriptor_header *) &eem_fs_out_desc,
- NULL,
-};
-
/* high speed support: */

static struct usb_endpoint_descriptor eem_hs_in_desc = {
@@ -101,14 +93,6 @@ static struct usb_endpoint_descriptor eem_hs_out_desc = {
.wMaxPacketSize = cpu_to_le16(512),
};

-static struct usb_descriptor_header *eem_hs_function[] = {
- /* CDC EEM control descriptors */
- (struct usb_descriptor_header *) &eem_intf,
- (struct usb_descriptor_header *) &eem_hs_in_desc,
- (struct usb_descriptor_header *) &eem_hs_out_desc,
- NULL,
-};
-
/* super speed support: */

static struct usb_endpoint_descriptor eem_ss_in_desc = {
@@ -138,15 +122,16 @@ static struct usb_ss_ep_comp_descriptor eem_ss_bulk_comp_desc = {
/* .bmAttributes = 0, */
};

-static struct usb_descriptor_header *eem_ss_function[] = {
- /* CDC EEM control descriptors */
- (struct usb_descriptor_header *) &eem_intf,
- (struct usb_descriptor_header *) &eem_ss_in_desc,
- (struct usb_descriptor_header *) &eem_ss_bulk_comp_desc,
- (struct usb_descriptor_header *) &eem_ss_out_desc,
- (struct usb_descriptor_header *) &eem_ss_bulk_comp_desc,
- NULL,
-};
+USB_COMPOSITE_ENDPOINT(ep_in, &eem_fs_in_desc, &eem_hs_in_desc,
+ &eem_ss_in_desc, &eem_ss_bulk_comp_desc);
+USB_COMPOSITE_ENDPOINT(ep_out, &eem_fs_out_desc, &eem_hs_out_desc,
+ &eem_ss_out_desc, &eem_ss_bulk_comp_desc);
+
+USB_COMPOSITE_ALTSETTING(intf0alt0, &eem_intf, &ep_in, &ep_out);
+
+USB_COMPOSITE_INTERFACE(intf0, &intf0alt0);
+
+USB_COMPOSITE_DESCRIPTORS(eem_descs, &intf0);

/* string descriptors: */

@@ -190,65 +175,46 @@ static int eem_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
struct usb_composite_dev *cdev = f->config->cdev;
struct net_device *net;

- /* we know alt == 0, so this is an activation or a reset */
- if (alt != 0)
- goto fail;
-
- if (intf == eem->ctrl_id) {
- DBG(cdev, "reset eem\n");
- gether_disconnect(&eem->port);
-
- if (!eem->port.in_ep->desc || !eem->port.out_ep->desc) {
- DBG(cdev, "init eem\n");
- if (config_ep_by_speed(cdev->gadget, f,
- eem->port.in_ep) ||
- config_ep_by_speed(cdev->gadget, f,
- eem->port.out_ep)) {
- eem->port.in_ep->desc = NULL;
- eem->port.out_ep->desc = NULL;
- goto fail;
- }
- }
+ eem->port.in_ep = usb_function_get_ep(f, intf, 0);
+ if (!eem->port.in_ep)
+ return -ENODEV;

- /* zlps should not occur because zero-length EEM packets
- * will be inserted in those cases where they would occur
- */
- eem->port.is_zlp_ok = 1;
- eem->port.cdc_filter = DEFAULT_FILTER;
- DBG(cdev, "activate eem\n");
- net = gether_connect(&eem->port);
- if (IS_ERR(net))
- return PTR_ERR(net);
- } else
- goto fail;
+ eem->port.out_ep = usb_function_get_ep(f, intf, 1);
+ if (!eem->port.out_ep)
+ return -ENODEV;
+
+ /* zlps should not occur because zero-length EEM packets
+ * will be inserted in those cases where they would occur
+ */
+ eem->port.is_zlp_ok = 1;
+ eem->port.cdc_filter = DEFAULT_FILTER;
+ DBG(cdev, "activate eem\n");
+ net = gether_connect(&eem->port);
+ if (IS_ERR(net))
+ return PTR_ERR(net);

return 0;
-fail:
- return -EINVAL;
}

-static void eem_disable(struct usb_function *f)
+static void eem_clear_alt(struct usb_function *f, unsigned intf, unsigned alt)
{
struct f_eem *eem = func_to_eem(f);
struct usb_composite_dev *cdev = f->config->cdev;

DBG(cdev, "eem deactivated\n");

- if (eem->port.in_ep->enabled)
- gether_disconnect(&eem->port);
+ gether_disconnect(&eem->port);
}

/*-------------------------------------------------------------------------*/

/* EEM function driver setup/binding */

-static int eem_bind(struct usb_configuration *c, struct usb_function *f)
+static int eem_prep_descs(struct usb_function *f)
{
- struct usb_composite_dev *cdev = c->cdev;
- struct f_eem *eem = func_to_eem(f);
+ struct usb_composite_dev *cdev = f->config->cdev;
struct usb_string *us;
int status;
- struct usb_ep *ep;

struct f_eem_opts *eem_opts;

@@ -276,53 +242,7 @@ static int eem_bind(struct usb_configuration *c, struct usb_function *f)
return PTR_ERR(us);
eem_intf.iInterface = us[0].id;

- /* allocate instance-specific interface IDs */
- status = usb_interface_id(c, f);
- if (status < 0)
- goto fail;
- eem->ctrl_id = status;
- eem_intf.bInterfaceNumber = status;
-
- status = -ENODEV;
-
- /* allocate instance-specific endpoints */
- ep = usb_ep_autoconfig(cdev->gadget, &eem_fs_in_desc);
- if (!ep)
- goto fail;
- eem->port.in_ep = ep;
-
- ep = usb_ep_autoconfig(cdev->gadget, &eem_fs_out_desc);
- if (!ep)
- goto fail;
- eem->port.out_ep = ep;
-
- status = -ENOMEM;
-
- /* support all relevant hardware speeds... we expect that when
- * hardware is dual speed, all bulk-capable endpoints work at
- * both speeds
- */
- eem_hs_in_desc.bEndpointAddress = eem_fs_in_desc.bEndpointAddress;
- eem_hs_out_desc.bEndpointAddress = eem_fs_out_desc.bEndpointAddress;
-
- eem_ss_in_desc.bEndpointAddress = eem_fs_in_desc.bEndpointAddress;
- eem_ss_out_desc.bEndpointAddress = eem_fs_out_desc.bEndpointAddress;
-
- status = usb_assign_descriptors(f, eem_fs_function, eem_hs_function,
- eem_ss_function);
- if (status)
- goto fail;
-
- DBG(cdev, "CDC Ethernet (EEM): %s speed IN/%s OUT/%s\n",
- gadget_is_superspeed(c->cdev->gadget) ? "super" :
- gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
- eem->port.in_ep->name, eem->port.out_ep->name);
- return 0;
-
-fail:
- ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
-
- return status;
+ return usb_function_set_descs(f, &eem_descs);
}

static void eem_cmd_complete(struct usb_ep *ep, struct usb_request *req)
@@ -604,13 +524,6 @@ static void eem_free(struct usb_function *f)
mutex_unlock(&opts->lock);
}

-static void eem_unbind(struct usb_configuration *c, struct usb_function *f)
-{
- DBG(c->cdev, "eem unbind\n");
-
- usb_free_all_descriptors(f);
-}
-
static struct usb_function *eem_alloc(struct usb_function_instance *fi)
{
struct f_eem *eem;
@@ -631,11 +544,10 @@ static struct usb_function *eem_alloc(struct usb_function_instance *fi)

eem->port.func.name = "cdc_eem";
/* descriptors are per-instance copies */
- eem->port.func.bind = eem_bind;
- eem->port.func.unbind = eem_unbind;
+ eem->port.func.prep_descs = eem_prep_descs;
eem->port.func.set_alt = eem_set_alt;
+ eem->port.func.clear_alt = eem_clear_alt;
eem->port.func.setup = eem_setup;
- eem->port.func.disable = eem_disable;
eem->port.func.free_func = eem_free;
eem->port.wrap = eem_wrap;
eem->port.unwrap = eem_unwrap;
--
1.9.1

2015-11-27 11:01:21

by Robert Baldyga

[permalink] [raw]
Subject: [PATCH v2 27/29] usb: gadget: f_ncm: conversion to new API

Generate descriptors in new format and attach them to USB function in
prep_descs(). Implement prep_vendor_descs() to supply class specific
descriptors. Change set_alt() implementation and implement clear_alt()
operation. Remove boilerplate code.

Signed-off-by: Robert Baldyga <[email protected]>
---
drivers/usb/gadget/function/f_ncm.c | 320 ++++++++++++------------------------
1 file changed, 105 insertions(+), 215 deletions(-)

diff --git a/drivers/usb/gadget/function/f_ncm.c b/drivers/usb/gadget/function/f_ncm.c
index 7ad798a..a681895 100644
--- a/drivers/usb/gadget/function/f_ncm.c
+++ b/drivers/usb/gadget/function/f_ncm.c
@@ -32,8 +32,7 @@
* NCM is intended to be used with high-speed network attachments.
*
* Note that NCM requires the use of "alternate settings" for its data
- * interface. This means that the set_alt() method has real work to do,
- * and also means that a get_alt() method is required.
+ * interface.
*/

/* to trigger crc/non-crc ndp signature */
@@ -270,23 +269,6 @@ static struct usb_endpoint_descriptor fs_ncm_out_desc = {
.bmAttributes = USB_ENDPOINT_XFER_BULK,
};

-static struct usb_descriptor_header *ncm_fs_function[] = {
- (struct usb_descriptor_header *) &ncm_iad_desc,
- /* CDC NCM control descriptors */
- (struct usb_descriptor_header *) &ncm_control_intf,
- (struct usb_descriptor_header *) &ncm_header_desc,
- (struct usb_descriptor_header *) &ncm_union_desc,
- (struct usb_descriptor_header *) &ecm_desc,
- (struct usb_descriptor_header *) &ncm_desc,
- (struct usb_descriptor_header *) &fs_ncm_notify_desc,
- /* data interface, altsettings 0 and 1 */
- (struct usb_descriptor_header *) &ncm_data_nop_intf,
- (struct usb_descriptor_header *) &ncm_data_intf,
- (struct usb_descriptor_header *) &fs_ncm_in_desc,
- (struct usb_descriptor_header *) &fs_ncm_out_desc,
- NULL,
-};
-
/* high speed support: */

static struct usb_endpoint_descriptor hs_ncm_notify_desc = {
@@ -316,22 +298,21 @@ static struct usb_endpoint_descriptor hs_ncm_out_desc = {
.wMaxPacketSize = cpu_to_le16(512),
};

-static struct usb_descriptor_header *ncm_hs_function[] = {
- (struct usb_descriptor_header *) &ncm_iad_desc,
- /* CDC NCM control descriptors */
- (struct usb_descriptor_header *) &ncm_control_intf,
- (struct usb_descriptor_header *) &ncm_header_desc,
- (struct usb_descriptor_header *) &ncm_union_desc,
- (struct usb_descriptor_header *) &ecm_desc,
- (struct usb_descriptor_header *) &ncm_desc,
- (struct usb_descriptor_header *) &hs_ncm_notify_desc,
- /* data interface, altsettings 0 and 1 */
- (struct usb_descriptor_header *) &ncm_data_nop_intf,
- (struct usb_descriptor_header *) &ncm_data_intf,
- (struct usb_descriptor_header *) &hs_ncm_in_desc,
- (struct usb_descriptor_header *) &hs_ncm_out_desc,
- NULL,
-};
+USB_COMPOSITE_ENDPOINT(ep_notify, &fs_ncm_notify_desc,
+ &hs_ncm_notify_desc, NULL, NULL);
+USB_COMPOSITE_ENDPOINT(ep_in, &fs_ncm_in_desc,
+ &hs_ncm_in_desc, NULL, NULL);
+USB_COMPOSITE_ENDPOINT(ep_out, &fs_ncm_out_desc,
+ &hs_ncm_out_desc, NULL, NULL);
+
+USB_COMPOSITE_ALTSETTING(intf0alt0, &ncm_control_intf, &ep_notify);
+USB_COMPOSITE_ALTSETTING(intf1alt0, &ncm_data_nop_intf);
+USB_COMPOSITE_ALTSETTING(intf1alt1, &ncm_data_intf, &ep_in, &ep_out);
+
+USB_COMPOSITE_INTERFACE(intf0, &intf0alt0);
+USB_COMPOSITE_INTERFACE(intf1, &intf1alt0, &intf1alt1);
+
+USB_COMPOSITE_DESCRIPTORS(ncm_descs, &intf0, &intf1);

/* string descriptors: */

@@ -792,6 +773,8 @@ invalid:
return value;
}

+static void ncm_tx_tasklet(unsigned long data);
+static enum hrtimer_restart ncm_tx_timeout(struct hrtimer *data);

static int ncm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
{
@@ -799,52 +782,44 @@ static int ncm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
struct usb_composite_dev *cdev = f->config->cdev;

/* Control interface has only altsetting 0 */
- if (intf == ncm->ctrl_id) {
- if (alt != 0)
- goto fail;
-
+ if (intf == 0) {
DBG(cdev, "reset ncm control %d\n", intf);
- usb_ep_disable(ncm->notify);

- if (!(ncm->notify->desc)) {
- DBG(cdev, "init ncm ctrl %d\n", intf);
- if (config_ep_by_speed(cdev->gadget, f, ncm->notify))
- goto fail;
+ ncm->notify = usb_function_get_ep(f, intf, 0);
+ if (!ncm->notify)
+ return -ENODEV;
+
+ /* allocate notification request and buffer */
+ ncm->notify_req = usb_ep_alloc_request(ncm->notify, GFP_KERNEL);
+ if (!ncm->notify_req)
+ return -ENOMEM;
+ ncm->notify_req->buf = kmalloc(NCM_STATUS_BYTECOUNT, GFP_KERNEL);
+ if (!ncm->notify_req->buf) {
+ usb_ep_free_request(ncm->notify, ncm->notify_req);
+ return -ENOMEM;
}
- usb_ep_enable(ncm->notify);

- /* Data interface has two altsettings, 0 and 1 */
- } else if (intf == ncm->data_id) {
- if (alt > 1)
- goto fail;
-
- if (ncm->port.in_ep->enabled) {
- DBG(cdev, "reset ncm\n");
- ncm->timer_stopping = true;
- ncm->netdev = NULL;
- gether_disconnect(&ncm->port);
- ncm_reset_values(ncm);
- }
+ ncm->notify_req->context = ncm;
+ ncm->notify_req->complete = ncm_notify_complete;

+ tasklet_init(&ncm->tx_tasklet, ncm_tx_tasklet, (unsigned long) ncm);
+ hrtimer_init(&ncm->task_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+ ncm->task_timer.function = ncm_tx_timeout;
+
+ /* Data interface has two altsettings, 0 and 1 */
+ } else if (intf == 1) {
/*
* CDC Network only sends data in non-default altsettings.
* Changing altsettings resets filters, statistics, etc.
*/
if (alt == 1) {
struct net_device *net;
-
- if (!ncm->port.in_ep->desc ||
- !ncm->port.out_ep->desc) {
- DBG(cdev, "init ncm\n");
- if (config_ep_by_speed(cdev->gadget, f,
- ncm->port.in_ep) ||
- config_ep_by_speed(cdev->gadget, f,
- ncm->port.out_ep)) {
- ncm->port.in_ep->desc = NULL;
- ncm->port.out_ep->desc = NULL;
- goto fail;
- }
- }
+ ncm->port.in_ep = usb_function_get_ep(f, intf, 0);
+ if (!ncm->port.in_ep)
+ return -ENODEV;
+ ncm->port.out_ep = usb_function_get_ep(f, intf, 1);
+ if (!ncm->port.out_ep)
+ return -ENODEV;

/* TODO */
/* Enable zlps by default for NCM conformance;
@@ -864,25 +839,28 @@ static int ncm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
spin_lock(&ncm->lock);
ncm_notify(ncm);
spin_unlock(&ncm->lock);
- } else
- goto fail;
+ }

return 0;
-fail:
- return -EINVAL;
}

-/*
- * Because the data interface supports multiple altsettings,
- * this NCM function *MUST* implement a get_alt() method.
- */
-static int ncm_get_alt(struct usb_function *f, unsigned intf)
+static void ncm_clear_alt(struct usb_function *f, unsigned intf, unsigned alt)
{
struct f_ncm *ncm = func_to_ncm(f);
+ struct usb_composite_dev *cdev = f->config->cdev;

- if (intf == ncm->ctrl_id)
- return 0;
- return ncm->port.in_ep->enabled ? 1 : 0;
+ if (intf == 0) {
+ hrtimer_cancel(&ncm->task_timer);
+ tasklet_kill(&ncm->tx_tasklet);
+ kfree(ncm->notify_req->buf);
+ usb_ep_free_request(ncm->notify, ncm->notify_req);
+ } else if (intf == 1 && alt == 1) {
+ DBG(cdev, "reset ncm\n");
+ ncm->timer_stopping = true;
+ ncm->netdev = NULL;
+ gether_disconnect(&ncm->port);
+ ncm_reset_values(ncm);
+ }
}

static struct sk_buff *package_for_tx(struct f_ncm *ncm)
@@ -1266,25 +1244,6 @@ err:
return ret;
}

-static void ncm_disable(struct usb_function *f)
-{
- struct f_ncm *ncm = func_to_ncm(f);
- struct usb_composite_dev *cdev = f->config->cdev;
-
- DBG(cdev, "ncm deactivated\n");
-
- if (ncm->port.in_ep->enabled) {
- ncm->timer_stopping = true;
- ncm->netdev = NULL;
- gether_disconnect(&ncm->port);
- }
-
- if (ncm->notify->enabled) {
- usb_ep_disable(ncm->notify);
- ncm->notify->desc = NULL;
- }
-}
-
/*-------------------------------------------------------------------------*/

/*
@@ -1333,18 +1292,34 @@ static void ncm_close(struct gether *geth)

/* ethernet function driver setup/binding */

-static int ncm_bind(struct usb_configuration *c, struct usb_function *f)
+static int ncm_prep_descs(struct usb_function *f)
{
- struct usb_composite_dev *cdev = c->cdev;
- struct f_ncm *ncm = func_to_ncm(f);
+ struct usb_composite_dev *cdev = f->config->cdev;
struct usb_string *us;
- int status;
- struct usb_ep *ep;
- struct f_ncm_opts *ncm_opts;

if (!can_support_ecm(cdev->gadget))
return -EINVAL;

+ us = usb_gstrings_attach(cdev, ncm_strings,
+ ARRAY_SIZE(ncm_string_defs));
+ if (IS_ERR(us))
+ return PTR_ERR(us);
+ ncm_control_intf.iInterface = us[STRING_CTRL_IDX].id;
+ ncm_data_nop_intf.iInterface = us[STRING_DATA_IDX].id;
+ ncm_data_intf.iInterface = us[STRING_DATA_IDX].id;
+ ecm_desc.iMACAddress = us[STRING_MAC_IDX].id;
+ ncm_iad_desc.iFunction = us[STRING_IAD_IDX].id;
+
+ return usb_function_set_descs(f, &ncm_descs);
+}
+
+static int ncm_prep_vendor_descs(struct usb_function *f)
+{
+ struct usb_composite_dev *cdev = f->config->cdev;
+ struct f_ncm *ncm = func_to_ncm(f);
+ struct f_ncm_opts *ncm_opts;
+ int status, intf0_id, intf1_id;
+
ncm_opts = container_of(f->fi, struct f_ncm_opts, func_inst);
/*
* in drivers/usb/gadget/configfs.c:configfs_composite_bind()
@@ -1362,79 +1337,29 @@ static int ncm_bind(struct usb_configuration *c, struct usb_function *f)
return status;
ncm_opts->bound = true;
}
- us = usb_gstrings_attach(cdev, ncm_strings,
- ARRAY_SIZE(ncm_string_defs));
- if (IS_ERR(us))
- return PTR_ERR(us);
- ncm_control_intf.iInterface = us[STRING_CTRL_IDX].id;
- ncm_data_nop_intf.iInterface = us[STRING_DATA_IDX].id;
- ncm_data_intf.iInterface = us[STRING_DATA_IDX].id;
- ecm_desc.iMACAddress = us[STRING_MAC_IDX].id;
- ncm_iad_desc.iFunction = us[STRING_IAD_IDX].id;

- /* allocate instance-specific interface IDs */
- status = usb_interface_id(c, f);
- if (status < 0)
- goto fail;
- ncm->ctrl_id = status;
- ncm_iad_desc.bFirstInterface = status;
-
- ncm_control_intf.bInterfaceNumber = status;
- ncm_union_desc.bMasterInterface0 = status;
-
- status = usb_interface_id(c, f);
- if (status < 0)
- goto fail;
- ncm->data_id = status;
-
- ncm_data_nop_intf.bInterfaceNumber = status;
- ncm_data_intf.bInterfaceNumber = status;
- ncm_union_desc.bSlaveInterface0 = status;
-
- status = -ENODEV;
-
- /* allocate instance-specific endpoints */
- ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_in_desc);
- if (!ep)
- goto fail;
- ncm->port.in_ep = ep;
-
- ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_out_desc);
- if (!ep)
- goto fail;
- ncm->port.out_ep = ep;
-
- ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_notify_desc);
- if (!ep)
- goto fail;
- ncm->notify = ep;
-
- status = -ENOMEM;
-
- /* allocate notification request and buffer */
- ncm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
- if (!ncm->notify_req)
- goto fail;
- ncm->notify_req->buf = kmalloc(NCM_STATUS_BYTECOUNT, GFP_KERNEL);
- if (!ncm->notify_req->buf)
- goto fail;
- ncm->notify_req->context = ncm;
- ncm->notify_req->complete = ncm_notify_complete;
+ intf0_id = usb_get_interface_id(f, 0);
+ intf1_id = usb_get_interface_id(f, 1);

- /*
- * support all relevant hardware speeds... we expect that when
- * hardware is dual speed, all bulk-capable endpoints work at
- * both speeds
- */
- hs_ncm_in_desc.bEndpointAddress = fs_ncm_in_desc.bEndpointAddress;
- hs_ncm_out_desc.bEndpointAddress = fs_ncm_out_desc.bEndpointAddress;
- hs_ncm_notify_desc.bEndpointAddress =
- fs_ncm_notify_desc.bEndpointAddress;
+ ncm->ctrl_id = intf0_id;
+ ncm->data_id = intf1_id;
+
+ ncm_iad_desc.bFirstInterface = intf0_id;
+
+ ncm_union_desc.bMasterInterface0 = intf0_id;
+ ncm_union_desc.bSlaveInterface0 = intf1_id;

- status = usb_assign_descriptors(f, ncm_fs_function, ncm_hs_function,
- NULL);
- if (status)
- goto fail;
+ usb_function_add_vendor_desc(f,
+ (struct usb_descriptor_header *)&ncm_iad_desc);
+
+ usb_altset_add_vendor_desc(f, 0, 0,
+ (struct usb_descriptor_header *)&ncm_header_desc);
+ usb_altset_add_vendor_desc(f, 0, 0,
+ (struct usb_descriptor_header *)&ncm_union_desc);
+ usb_altset_add_vendor_desc(f, 0, 0,
+ (struct usb_descriptor_header *)&ecm_desc);
+ usb_altset_add_vendor_desc(f, 0, 0,
+ (struct usb_descriptor_header *)&ncm_desc);

/*
* NOTE: all that is done without knowing or caring about
@@ -1445,25 +1370,7 @@ static int ncm_bind(struct usb_configuration *c, struct usb_function *f)
ncm->port.open = ncm_open;
ncm->port.close = ncm_close;

- tasklet_init(&ncm->tx_tasklet, ncm_tx_tasklet, (unsigned long) ncm);
- hrtimer_init(&ncm->task_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
- ncm->task_timer.function = ncm_tx_timeout;
-
- DBG(cdev, "CDC Network: %s speed IN/%s OUT/%s NOTIFY/%s\n",
- gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
- ncm->port.in_ep->name, ncm->port.out_ep->name,
- ncm->notify->name);
return 0;
-
-fail:
- if (ncm->notify_req) {
- kfree(ncm->notify_req->buf);
- usb_ep_free_request(ncm->notify, ncm->notify_req);
- }
-
- ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
-
- return status;
}

static inline struct f_ncm_opts *to_f_ncm_opts(struct config_item *item)
@@ -1547,22 +1454,6 @@ static void ncm_free(struct usb_function *f)
mutex_unlock(&opts->lock);
}

-static void ncm_unbind(struct usb_configuration *c, struct usb_function *f)
-{
- struct f_ncm *ncm = func_to_ncm(f);
-
- DBG(c->cdev, "ncm unbind\n");
-
- hrtimer_cancel(&ncm->task_timer);
- tasklet_kill(&ncm->tx_tasklet);
-
- ncm_string_defs[0].id = 0;
- usb_free_all_descriptors(f);
-
- kfree(ncm->notify_req->buf);
- usb_ep_free_request(ncm->notify, ncm->notify_req);
-}
-
static struct usb_function *ncm_alloc(struct usb_function_instance *fi)
{
struct f_ncm *ncm;
@@ -1597,12 +1488,11 @@ static struct usb_function *ncm_alloc(struct usb_function_instance *fi)

ncm->port.func.name = "cdc_network";
/* descriptors are per-instance copies */
- ncm->port.func.bind = ncm_bind;
- ncm->port.func.unbind = ncm_unbind;
+ ncm->port.func.prep_descs = ncm_prep_descs;
+ ncm->port.func.prep_vendor_descs = ncm_prep_vendor_descs;
ncm->port.func.set_alt = ncm_set_alt;
- ncm->port.func.get_alt = ncm_get_alt;
+ ncm->port.func.clear_alt = ncm_clear_alt;
ncm->port.func.setup = ncm_setup;
- ncm->port.func.disable = ncm_disable;
ncm->port.func.free_func = ncm_free;

ncm->port.wrap = ncm_wrap_ntb;
--
1.9.1

2015-11-27 11:01:23

by Robert Baldyga

[permalink] [raw]
Subject: [PATCH v2 28/29] usb: gadget: f_printer: conversion to new API

Generate descriptors in new format and attach them to USB function in
prep_descs(). Implement prep_vendor_descs() to supply class specific
descriptors. Change set_alt() implementation and implement clear_alt()
operation. Remove boilerplate code.

Signed-off-by: Robert Baldyga <[email protected]>
---
drivers/usb/gadget/function/f_printer.c | 300 ++++++++++----------------------
1 file changed, 88 insertions(+), 212 deletions(-)

diff --git a/drivers/usb/gadget/function/f_printer.c b/drivers/usb/gadget/function/f_printer.c
index 0fbfb2b..f5cfea3 100644
--- a/drivers/usb/gadget/function/f_printer.c
+++ b/drivers/usb/gadget/function/f_printer.c
@@ -135,13 +135,6 @@ static struct usb_endpoint_descriptor fs_ep_out_desc = {
.bmAttributes = USB_ENDPOINT_XFER_BULK
};

-static struct usb_descriptor_header *fs_printer_function[] = {
- (struct usb_descriptor_header *) &intf_desc,
- (struct usb_descriptor_header *) &fs_ep_in_desc,
- (struct usb_descriptor_header *) &fs_ep_out_desc,
- NULL
-};
-
/*
* usb 2.0 devices need to expose both high speed and full speed
* descriptors, unless they only run at full speed.
@@ -169,13 +162,6 @@ static struct usb_qualifier_descriptor dev_qualifier = {
.bNumConfigurations = 1
};

-static struct usb_descriptor_header *hs_printer_function[] = {
- (struct usb_descriptor_header *) &intf_desc,
- (struct usb_descriptor_header *) &hs_ep_in_desc,
- (struct usb_descriptor_header *) &hs_ep_out_desc,
- NULL
-};
-
/*
* Added endpoint descriptors for 3.0 devices
*/
@@ -204,14 +190,16 @@ static struct usb_ss_ep_comp_descriptor ss_ep_out_comp_desc = {
.bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
};

-static struct usb_descriptor_header *ss_printer_function[] = {
- (struct usb_descriptor_header *) &intf_desc,
- (struct usb_descriptor_header *) &ss_ep_in_desc,
- (struct usb_descriptor_header *) &ss_ep_in_comp_desc,
- (struct usb_descriptor_header *) &ss_ep_out_desc,
- (struct usb_descriptor_header *) &ss_ep_out_comp_desc,
- NULL
-};
+USB_COMPOSITE_ENDPOINT(ep_in, &fs_ep_in_desc, &hs_ep_in_desc,
+ &ss_ep_in_desc, &ss_ep_in_comp_desc);
+USB_COMPOSITE_ENDPOINT(ep_out, &fs_ep_out_desc, &hs_ep_out_desc,
+ &ss_ep_out_desc, &ss_ep_out_comp_desc);
+
+USB_COMPOSITE_ALTSETTING(intf0alt0, &intf_desc, &ep_in, &ep_out);
+
+USB_COMPOSITE_INTERFACE(intf0, &intf0alt0);
+
+USB_COMPOSITE_DESCRIPTORS(printer_descs, &intf0);

/* maxpacket and other transfer characteristics vary by speed. */
static inline struct usb_endpoint_descriptor *ep_desc(struct usb_gadget *gadget,
@@ -764,86 +752,6 @@ static const struct file_operations printer_io_operations = {

/*-------------------------------------------------------------------------*/

-static int
-set_printer_interface(struct printer_dev *dev)
-{
- int result = 0;
-
- dev->in_ep->desc = ep_desc(dev->gadget, &fs_ep_in_desc, &hs_ep_in_desc,
- &ss_ep_in_desc);
- dev->in_ep->driver_data = dev;
-
- dev->out_ep->desc = ep_desc(dev->gadget, &fs_ep_out_desc,
- &hs_ep_out_desc, &ss_ep_out_desc);
- dev->out_ep->driver_data = dev;
-
- result = usb_ep_enable(dev->in_ep);
- if (result != 0) {
- DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
- goto done;
- }
-
- result = usb_ep_enable(dev->out_ep);
- if (result != 0) {
- DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
- goto done;
- }
-
-done:
- /* on error, disable any endpoints */
- if (result != 0) {
- (void) usb_ep_disable(dev->in_ep);
- (void) usb_ep_disable(dev->out_ep);
- dev->in_ep->desc = NULL;
- dev->out_ep->desc = NULL;
- }
-
- /* caller is responsible for cleanup on error */
- return result;
-}
-
-static void printer_reset_interface(struct printer_dev *dev)
-{
- unsigned long flags;
-
- if (dev->interface < 0)
- return;
-
- DBG(dev, "%s\n", __func__);
-
- if (dev->in_ep->desc)
- usb_ep_disable(dev->in_ep);
-
- if (dev->out_ep->desc)
- usb_ep_disable(dev->out_ep);
-
- spin_lock_irqsave(&dev->lock, flags);
- dev->in_ep->desc = NULL;
- dev->out_ep->desc = NULL;
- dev->interface = -1;
- spin_unlock_irqrestore(&dev->lock, flags);
-}
-
-/* Change our operational Interface. */
-static int set_interface(struct printer_dev *dev, unsigned number)
-{
- int result = 0;
-
- /* Free the current interface */
- printer_reset_interface(dev);
-
- result = set_printer_interface(dev);
- if (result)
- printer_reset_interface(dev);
- else
- dev->interface = number;
-
- if (!result)
- INFO(dev, "Using interface %x\n", number);
-
- return result;
-}
-
static void printer_soft_reset(struct printer_dev *dev)
{
struct usb_request *req;
@@ -1008,55 +916,64 @@ unknown:
return value;
}

-static int printer_func_bind(struct usb_configuration *c,
- struct usb_function *f)
+static int printer_func_prep_descs(struct usb_function *f)
+{
+ return usb_function_set_descs(f, &printer_descs);
+}
+
+static int printer_func_prep_vendor_descs(struct usb_function *f)
{
- struct usb_gadget *gadget = c->cdev->gadget;
struct printer_dev *dev = func_to_printer(f);
struct device *pdev;
- struct usb_composite_dev *cdev = c->cdev;
- struct usb_ep *in_ep;
- struct usb_ep *out_ep = NULL;
- struct usb_request *req;
dev_t devt;
- int id;
int ret;
- u32 i;

- id = usb_interface_id(c, f);
- if (id < 0)
- return id;
- intf_desc.bInterfaceNumber = id;
+ dev->interface = usb_get_interface_id(f, 0);

- /* finish hookup to lower layer ... */
- dev->gadget = gadget;
-
- /* all we really need is bulk IN/OUT */
- in_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_in_desc);
- if (!in_ep) {
-autoconf_fail:
- dev_err(&cdev->gadget->dev, "can't autoconfigure on %s\n",
- cdev->gadget->name);
- return -ENODEV;
+ /* Setup the sysfs files for the printer gadget. */
+ devt = MKDEV(major, dev->minor);
+ pdev = device_create(usb_gadget_class, NULL, devt,
+ NULL, "g_printer%d", dev->minor);
+ if (IS_ERR(pdev)) {
+ ERROR(dev, "Failed to create device: g_printer\n");
+ return PTR_ERR(pdev);
+ }
+
+ /*
+ * Register a character device as an interface to a user mode
+ * program that handles the printer specific functionality.
+ */
+ cdev_init(&dev->printer_cdev, &printer_io_operations);
+ dev->printer_cdev.owner = THIS_MODULE;
+ ret = cdev_add(&dev->printer_cdev, devt, 1);
+ if (ret) {
+ ERROR(dev, "Failed to open char device\n");
+ device_destroy(usb_gadget_class, devt);
}

- out_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_out_desc);
- if (!out_ep)
- goto autoconf_fail;
+ return ret;
+}

- /* assumes that all endpoints are dual-speed */
- hs_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
- hs_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
- ss_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
- ss_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
+static int printer_func_set_alt(struct usb_function *f,
+ unsigned intf, unsigned alt)
+{
+ struct printer_dev *dev = func_to_printer(f);
+ struct usb_composite_dev *cdev = f->config->cdev;
+ struct usb_request *req;
+ int i, ret = -ENOTSUPP;

- ret = usb_assign_descriptors(f, fs_printer_function,
- hs_printer_function, ss_printer_function);
- if (ret)
- return ret;
+ /* finish hookup to lower layer ... */
+ dev->gadget = cdev->gadget;
+
+ dev->in_ep = usb_function_get_ep(f, intf, 0);
+ if (!dev->in_ep)
+ return -ENODEV;
+ dev->out_ep = usb_function_get_ep(f, intf, 1);
+ if (!dev->out_ep)
+ return -ENODEV;

- dev->in_ep = in_ep;
- dev->out_ep = out_ep;
+ dev->in_ep->driver_data = dev;
+ dev->out_ep->driver_data = dev;

ret = -ENOMEM;
for (i = 0; i < dev->q_len; i++) {
@@ -1073,33 +990,8 @@ autoconf_fail:
list_add(&req->list, &dev->rx_reqs);
}

- /* Setup the sysfs files for the printer gadget. */
- devt = MKDEV(major, dev->minor);
- pdev = device_create(usb_gadget_class, NULL, devt,
- NULL, "g_printer%d", dev->minor);
- if (IS_ERR(pdev)) {
- ERROR(dev, "Failed to create device: g_printer\n");
- ret = PTR_ERR(pdev);
- goto fail_rx_reqs;
- }
-
- /*
- * Register a character device as an interface to a user mode
- * program that handles the printer specific functionality.
- */
- cdev_init(&dev->printer_cdev, &printer_io_operations);
- dev->printer_cdev.owner = THIS_MODULE;
- ret = cdev_add(&dev->printer_cdev, devt, 1);
- if (ret) {
- ERROR(dev, "Failed to open char device\n");
- goto fail_cdev_add;
- }
-
return 0;

-fail_cdev_add:
- device_destroy(usb_gadget_class, devt);
-
fail_rx_reqs:
while (!list_empty(&dev->rx_reqs)) {
req = container_of(dev->rx_reqs.next, struct usb_request, list);
@@ -1115,28 +1007,44 @@ fail_tx_reqs:
}

return ret;
-
}

-static int printer_func_set_alt(struct usb_function *f,
+static void printer_func_clear_alt(struct usb_function *f,
unsigned intf, unsigned alt)
{
struct printer_dev *dev = func_to_printer(f);
- int ret = -ENOTSUPP;
+ struct usb_request *req;
+
+ DBG(dev, "%s\n", __func__);

- if (!alt)
- ret = set_interface(dev, intf);
+ /* we must already have been disconnected ... no i/o may be active */
+ WARN_ON(!list_empty(&dev->tx_reqs_active));
+ WARN_ON(!list_empty(&dev->rx_reqs_active));

- return ret;
-}
+ /* Free all memory for this driver. */
+ while (!list_empty(&dev->tx_reqs)) {
+ req = container_of(dev->tx_reqs.next, struct usb_request,
+ list);
+ list_del(&req->list);
+ printer_req_free(dev->in_ep, req);
+ }

-static void printer_func_disable(struct usb_function *f)
-{
- struct printer_dev *dev = func_to_printer(f);
+ if (dev->current_rx_req != NULL)
+ printer_req_free(dev->out_ep, dev->current_rx_req);

- DBG(dev, "%s\n", __func__);
+ while (!list_empty(&dev->rx_reqs)) {
+ req = container_of(dev->rx_reqs.next,
+ struct usb_request, list);
+ list_del(&req->list);
+ printer_req_free(dev->out_ep, req);
+ }

- printer_reset_interface(dev);
+ while (!list_empty(&dev->rx_buffers)) {
+ req = container_of(dev->rx_buffers.next,
+ struct usb_request, list);
+ list_del(&req->list);
+ printer_req_free(dev->out_ep, req);
+ }
}

static inline struct f_printer_opts
@@ -1333,45 +1241,12 @@ static void gprinter_free(struct usb_function *f)
static void printer_func_unbind(struct usb_configuration *c,
struct usb_function *f)
{
- struct printer_dev *dev;
- struct usb_request *req;
-
- dev = func_to_printer(f);
+ struct printer_dev *dev = func_to_printer(f);

device_destroy(usb_gadget_class, MKDEV(major, dev->minor));

/* Remove Character Device */
cdev_del(&dev->printer_cdev);
-
- /* we must already have been disconnected ... no i/o may be active */
- WARN_ON(!list_empty(&dev->tx_reqs_active));
- WARN_ON(!list_empty(&dev->rx_reqs_active));
-
- /* Free all memory for this driver. */
- while (!list_empty(&dev->tx_reqs)) {
- req = container_of(dev->tx_reqs.next, struct usb_request,
- list);
- list_del(&req->list);
- printer_req_free(dev->in_ep, req);
- }
-
- if (dev->current_rx_req != NULL)
- printer_req_free(dev->out_ep, dev->current_rx_req);
-
- while (!list_empty(&dev->rx_reqs)) {
- req = container_of(dev->rx_reqs.next,
- struct usb_request, list);
- list_del(&req->list);
- printer_req_free(dev->out_ep, req);
- }
-
- while (!list_empty(&dev->rx_buffers)) {
- req = container_of(dev->rx_buffers.next,
- struct usb_request, list);
- list_del(&req->list);
- printer_req_free(dev->out_ep, req);
- }
- usb_free_all_descriptors(f);
}

static struct usb_function *gprinter_alloc(struct usb_function_instance *fi)
@@ -1400,11 +1275,12 @@ static struct usb_function *gprinter_alloc(struct usb_function_instance *fi)
mutex_unlock(&opts->lock);

dev->function.name = "printer";
- dev->function.bind = printer_func_bind;
+ dev->function.prep_descs = printer_func_prep_descs;
+ dev->function.prep_vendor_descs = printer_func_prep_vendor_descs;
dev->function.setup = printer_func_setup;
dev->function.unbind = printer_func_unbind;
dev->function.set_alt = printer_func_set_alt;
- dev->function.disable = printer_func_disable;
+ dev->function.clear_alt = printer_func_clear_alt;
dev->function.req_match = gprinter_req_match;
dev->function.free_func = gprinter_free;

--
1.9.1

2015-11-27 11:01:50

by Robert Baldyga

[permalink] [raw]
Subject: [PATCH v2 29/29] usb: gadget: f_serial: conversion to new API

Generate descriptors in new format and attach them to USB function in
prep_descs(). Change set_alt() implementation and implement clear_alt()
operation. Remove boilerplate code.

Signed-off-by: Robert Baldyga <[email protected]>
---
drivers/usb/gadget/function/f_serial.c | 122 +++++++--------------------------
1 file changed, 25 insertions(+), 97 deletions(-)

diff --git a/drivers/usb/gadget/function/f_serial.c b/drivers/usb/gadget/function/f_serial.c
index 6bb44d61..526e664 100644
--- a/drivers/usb/gadget/function/f_serial.c
+++ b/drivers/usb/gadget/function/f_serial.c
@@ -69,13 +69,6 @@ static struct usb_endpoint_descriptor gser_fs_out_desc = {
.bmAttributes = USB_ENDPOINT_XFER_BULK,
};

-static struct usb_descriptor_header *gser_fs_function[] = {
- (struct usb_descriptor_header *) &gser_interface_desc,
- (struct usb_descriptor_header *) &gser_fs_in_desc,
- (struct usb_descriptor_header *) &gser_fs_out_desc,
- NULL,
-};
-
/* high speed support: */

static struct usb_endpoint_descriptor gser_hs_in_desc = {
@@ -92,13 +85,6 @@ static struct usb_endpoint_descriptor gser_hs_out_desc = {
.wMaxPacketSize = cpu_to_le16(512),
};

-static struct usb_descriptor_header *gser_hs_function[] = {
- (struct usb_descriptor_header *) &gser_interface_desc,
- (struct usb_descriptor_header *) &gser_hs_in_desc,
- (struct usb_descriptor_header *) &gser_hs_out_desc,
- NULL,
-};
-
static struct usb_endpoint_descriptor gser_ss_in_desc = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
@@ -118,14 +104,16 @@ static struct usb_ss_ep_comp_descriptor gser_ss_bulk_comp_desc = {
.bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
};

-static struct usb_descriptor_header *gser_ss_function[] = {
- (struct usb_descriptor_header *) &gser_interface_desc,
- (struct usb_descriptor_header *) &gser_ss_in_desc,
- (struct usb_descriptor_header *) &gser_ss_bulk_comp_desc,
- (struct usb_descriptor_header *) &gser_ss_out_desc,
- (struct usb_descriptor_header *) &gser_ss_bulk_comp_desc,
- NULL,
-};
+USB_COMPOSITE_ENDPOINT(ep_in, &gser_fs_in_desc, &gser_hs_in_desc,
+ &gser_ss_in_desc, &gser_ss_bulk_comp_desc);
+USB_COMPOSITE_ENDPOINT(ep_out, &gser_fs_out_desc, &gser_hs_out_desc,
+ &gser_ss_out_desc, &gser_ss_bulk_comp_desc);
+
+USB_COMPOSITE_ALTSETTING(intf0alt0, &gser_interface_desc, &ep_in, &ep_out);
+
+USB_COMPOSITE_INTERFACE(intf0, &intf0alt0);
+
+USB_COMPOSITE_DESCRIPTORS(serial_descs, &intf0);

/* string descriptors: */

@@ -151,28 +139,21 @@ static int gser_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
struct f_gser *gser = func_to_gser(f);
struct usb_composite_dev *cdev = f->config->cdev;

- /* we know alt == 0, so this is an activation or a reset */
-
- if (gser->port.in->enabled) {
- dev_dbg(&cdev->gadget->dev,
- "reset generic ttyGS%d\n", gser->port_num);
- gserial_disconnect(&gser->port);
- }
- if (!gser->port.in->desc || !gser->port.out->desc) {
- dev_dbg(&cdev->gadget->dev,
+ dev_dbg(&cdev->gadget->dev,
"activate generic ttyGS%d\n", gser->port_num);
- if (config_ep_by_speed(cdev->gadget, f, gser->port.in) ||
- config_ep_by_speed(cdev->gadget, f, gser->port.out)) {
- gser->port.in->desc = NULL;
- gser->port.out->desc = NULL;
- return -EINVAL;
- }
- }
+
+ gser->port.in = usb_function_get_ep(f, intf, 0);
+ if (!gser->port.in)
+ return -ENODEV;
+ gser->port.out = usb_function_get_ep(f, intf, 0);
+ if (!gser->port.out)
+ return -ENODEV;
+
gserial_connect(&gser->port, gser->port_num);
return 0;
}

-static void gser_disable(struct usb_function *f)
+static void gser_clear_alt(struct usb_function *f, unsigned intf, unsigned alt)
{
struct f_gser *gser = func_to_gser(f);
struct usb_composite_dev *cdev = f->config->cdev;
@@ -186,12 +167,9 @@ static void gser_disable(struct usb_function *f)

/* serial function driver setup/binding */

-static int gser_bind(struct usb_configuration *c, struct usb_function *f)
+static int gser_prep_descs(struct usb_function *f)
{
- struct usb_composite_dev *cdev = c->cdev;
- struct f_gser *gser = func_to_gser(f);
int status;
- struct usb_ep *ep;

/* REVISIT might want instance-specific strings to help
* distinguish instances ...
@@ -199,57 +177,13 @@ static int gser_bind(struct usb_configuration *c, struct usb_function *f)

/* maybe allocate device-global string ID */
if (gser_string_defs[0].id == 0) {
- status = usb_string_id(c->cdev);
+ status = usb_string_id(f->config->cdev);
if (status < 0)
return status;
gser_string_defs[0].id = status;
}

- /* allocate instance-specific interface IDs */
- status = usb_interface_id(c, f);
- if (status < 0)
- goto fail;
- gser->data_id = status;
- gser_interface_desc.bInterfaceNumber = status;
-
- status = -ENODEV;
-
- /* allocate instance-specific endpoints */
- ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_in_desc);
- if (!ep)
- goto fail;
- gser->port.in = ep;
-
- ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_out_desc);
- if (!ep)
- goto fail;
- gser->port.out = ep;
-
- /* support all relevant hardware speeds... we expect that when
- * hardware is dual speed, all bulk-capable endpoints work at
- * both speeds
- */
- gser_hs_in_desc.bEndpointAddress = gser_fs_in_desc.bEndpointAddress;
- gser_hs_out_desc.bEndpointAddress = gser_fs_out_desc.bEndpointAddress;
-
- gser_ss_in_desc.bEndpointAddress = gser_fs_in_desc.bEndpointAddress;
- gser_ss_out_desc.bEndpointAddress = gser_fs_out_desc.bEndpointAddress;
-
- status = usb_assign_descriptors(f, gser_fs_function, gser_hs_function,
- gser_ss_function);
- if (status)
- goto fail;
- dev_dbg(&cdev->gadget->dev, "generic ttyGS%d: %s speed IN/%s OUT/%s\n",
- gser->port_num,
- gadget_is_superspeed(c->cdev->gadget) ? "super" :
- gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
- gser->port.in->name, gser->port.out->name);
- return 0;
-
-fail:
- ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
-
- return status;
+ return usb_function_set_descs(f, &serial_descs);
}

static inline struct f_serial_opts *to_f_serial_opts(struct config_item *item)
@@ -325,11 +259,6 @@ static void gser_free(struct usb_function *f)
kfree(serial);
}

-static void gser_unbind(struct usb_configuration *c, struct usb_function *f)
-{
- usb_free_all_descriptors(f);
-}
-
static struct usb_function *gser_alloc(struct usb_function_instance *fi)
{
struct f_gser *gser;
@@ -346,10 +275,9 @@ static struct usb_function *gser_alloc(struct usb_function_instance *fi)

gser->port.func.name = "gser";
gser->port.func.strings = gser_strings;
- gser->port.func.bind = gser_bind;
- gser->port.func.unbind = gser_unbind;
+ gser->port.func.prep_descs = gser_prep_descs;
gser->port.func.set_alt = gser_set_alt;
- gser->port.func.disable = gser_disable;
+ gser->port.func.clear_alt = gser_clear_alt;
gser->port.func.free_func = gser_free;

return &gser->port.func;
--
1.9.1