From: Ming Lei <tom.leiming@xxxxxxxxx> This patch introduces isochronous source sink function driver into zero composite driver. The driver is based on David Brownell's f_sourcesink.c It can be used to evalute isochronous transfer performance of usb host controller or usb host controller driver, or can be used to troubleshoot usb host controller driver(usually some embedded hcd) or usb gadget controller driver handily, or can be used to test gadget driver or hardware. It has been verified OK on omap3-based beagle board, and Martin has tested it OK on other usb gadget controller. Basic features supported now: -multiple interface altsetting -customization of interface altsetting via module parameter -single iso-in or iso-out only endpoint -double buffering or more -high bandwidth endpoint Tested-by: Martin Fuzzey <mfuzzey@xxxxxxxxx> Tested-by: Gadiyar, Anand <gadiyar@xxxxxx> Cc: David Brownell <david-b@xxxxxxxxxxx> Signed-off-by: Ming Lei <tom.leiming@xxxxxxxxx> --- V3: Fix compile failure caused by the patch titled with: usb gadget: don't save bind callback in struct usb_configuration V2: Fix two problems below reported by Martin Fuzzey: - oops in enable_iso_source_sink if max packet size of highspeed endpoint is not stronger than that of fullspeed endpoint - not queue request in iso_source_sink_start_ep V1: This version takes suggestions from David Brownell: - add modules parameter to change interval/multi/max packet size of endpoint - support double buffering or more - support multiple altsetting --- drivers/usb/gadget/f_iso.c | 934 ++++++++++++++++++++++++++++++++++++++++++++ drivers/usb/gadget/zero.c | 28 ++- 2 files changed, 959 insertions(+), 3 deletions(-) create mode 100644 drivers/usb/gadget/f_iso.c diff --git a/drivers/usb/gadget/f_iso.c b/drivers/usb/gadget/f_iso.c new file mode 100644 index 0000000..f81fc97 --- /dev/null +++ b/drivers/usb/gadget/f_iso.c @@ -0,0 +1,934 @@ +/* + * f_iso.c - USB peripheral isochronous source/sink function driver + * This driver is based on David Brownell's f_sourcesink.c. + * + * Copyright (C) 2010, Ming Lei <tom.leiming@xxxxxxxxx> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/*#define VERBOSE_DEBUG*/ + +#include <linux/kernel.h> +#include <linux/utsname.h> +#include <linux/device.h> + +#include "g_zero.h" +#include "gadget_chips.h" + +/*Maximum altsettings supported for one interface*/ +#define MAX_ISO_ALTSETTINGS 5 +#define MAX_ISO_DESC_HEADERS (MAX_ISO_ALTSETTINGS*3 + 1) + +/* + * ISOCHRONOUS SOURCE/SINK FUNCTION ... a primary testing vehicle for + * USB peripheral controller drivers, it can also be used to evalute + * performance of usb host controller or usb host controller diver, + * and can be used to troubleshoot usb host controller driver handily. + * + * This just sinks isochronous packets OUT to the peripheral and sources + * them IN to the host. As such it supports basic functionality and + * load tests. + * + * In terms of control messaging, this supports all the standard requests + * + * This is currently packaged as a configuration driver, which can't be + * combined with other functions to make composite devices. However, it + * can be combined with other independent configurations. + */ + +/* Descriptions for one altsetting, at most one iso-in and + * one iso-out endpoint can be supported now at the same time + * for one altsetting. + * */ +struct iso_alt { + unsigned char has_in; /*there is one iso-in endpoint*/ + unsigned char has_out; /*there is one iso-out endpoint*/ + unsigned char in_multi; /*multi attribute for iso-in ep*/ + unsigned char out_multi; /*multi attribute for iso-out ep*/ + unsigned short in_interval; /*interval attribute for iso-in ep*/ + unsigned short out_interval; /*interval attribute for iso-out ep*/ + unsigned short in_maxp; /*max packet size for iso-in ep*/ + unsigned short out_maxp; /*max packet size for iso-out ep*/ + + /*the altsetting related descriptor*/ + struct usb_interface_descriptor *intf_desc; + struct usb_endpoint_descriptor *source_ep_desc; + struct usb_endpoint_descriptor *sink_ep_desc; +}; + +struct f_iso_sourcesink { + struct usb_function function; + + struct usb_ep *in_ep; + struct usb_ep *out_ep; + + int curr_alt; /*current altsetting*/ + + int fs_alt_num; /*how many altsettings for fs gadget*/ + int hs_alt_num; /*how many altsettings for hs gadget*/ + + /* the altsetting with the strongest capability(max packet size + * and multi), which is used for selecting hardware endpoint. + * */ + char fs_in_maxp_alt; + char fs_out_maxp_alt; + char hs_in_maxp_alt; + char hs_out_maxp_alt; + + struct iso_alt fs_alt[MAX_ISO_ALTSETTINGS]; + struct iso_alt hs_alt[MAX_ISO_ALTSETTINGS]; +}; + +static inline struct f_iso_sourcesink *func_to_iso_ss(struct usb_function *f) +{ + return container_of(f, struct f_iso_sourcesink, function); +} + +/*-------------------------------------------------------------------------*/ + +static struct usb_interface_descriptor iso_source_sink_intf_alt0 = { + .bLength = sizeof iso_source_sink_intf_alt0, + .bDescriptorType = USB_DT_INTERFACE, + .bAlternateSetting = 0, + .bNumEndpoints = 0, + .bInterfaceClass = USB_CLASS_VENDOR_SPEC, + /* .iInterface = DYNAMIC */ +}; + +static struct usb_descriptor_header *fs_iso_ss_descs[MAX_ISO_DESC_HEADERS]; +static struct usb_descriptor_header *hs_iso_ss_descs[MAX_ISO_DESC_HEADERS]; + +static char *iso_intf_desc = + "fs:alt0:alt1,in/1023/1:alt2,in/512/1:hs:alt0:alt1,in/1024*3/1:alt2,in/1024/1"; + +/* From @iso_intf_desc, we can see how many altsettings there are for + * the interface, and the endpoint direction, max packet size, + * multi(only for highspeed device) and interval information for each + * altsetting, then the whole interface decriptors can be built + * dynamically instead of using static descriptors stored in array. + * */ +module_param(iso_intf_desc, charp, S_IRUGO); +MODULE_PARM_DESC(iso_intf_desc, "ISO function description, format: fs:alt0:alt1,in/maxp/interval,out/maxp/interval:alt2,...:hs:alt0:alt1,in/maxp*multi/interval,out/maxp*multi/interval:...:"); + +/* function-specific strings: */ +static struct usb_string strings_iso_sourcesink[] = { + [0].s = "iso source and sink data", + { } /* end of list */ +}; + +static struct usb_gadget_strings stringtab_iso_sourcesink = { + .language = 0x0409, /* en-us */ + .strings = strings_iso_sourcesink, +}; + +static struct usb_gadget_strings *iso_sourcesink_strings[] = { + &stringtab_iso_sourcesink, + NULL, +}; + +#define is_valid(str, end) ((str) && ((str) <= (end))) + +static int parse_iso_altsetting(char *str, int *alt, + int *in_maxp, int *in_multi, + int *in_interval, + int *out_maxp, int *out_multi, + int *out_interval) +{ + char *end; + char *start; + int count = -1; + int out = 0; + int maxp, multi, interval; + + if (strncmp(str, "alt", 3)) + goto exit; + + start = str; + end = strstr(start, ":"); + if (!end) + end = start + strlen(start); + + /*parse altsetting number*/ + start += 3; + if (sscanf(start, "%d", alt) <= 0) + goto exit; + + count++; + + /*each altsetting, at most one in and one out endpoint are supported now*/ + while (start < end && count < 2) { + str = strstr(start, ",in/"); + if (!is_valid(str, end)) { + str = strstr(start, ",out/"); + if (!is_valid(str, end)) + goto exit; + out = 1; + } else + out = 0; + + /*skip ',in/' or ',out/'*/ + if (out) + start = str + 5; + else + start = str + 4; + + /*parse max packet size*/ + if (sscanf(start, "%d", &maxp) <= 0) + goto exit; + + /*parse multi*/ + str = strstr(start, "*"); + if (is_valid(str, end)) { + if (sscanf(str+1, "%d", &multi) <= 0) + goto exit; + } else + multi = 0; + + /*parse interval*/ + str = strstr(start, "/"); + if (is_valid(str, end)) { + if (sscanf(str+1, "%d", &interval) <= 0) + goto exit; + } else + goto exit; + + if (out) { + *out_maxp = maxp; + *out_multi = multi; + *out_interval = interval; + } else { + *in_maxp = maxp; + *in_multi = multi; + *in_interval = interval; + } + count++; + } +exit: + return count; +} + +static int check_iso_alt(struct usb_function *f, int hs, int alt, + int in_maxp, int in_multi, int in_interval, + int out_maxp, int out_multi, int out_interval) +{ + struct f_iso_sourcesink *ss = func_to_iso_ss(f); + struct usb_composite_dev *cdev = ss->function.config->cdev; + int ret = -EINVAL; + + if (!hs) { + if (in_multi || out_multi) { + ERROR(cdev, "%s:alt%d multi is not avaliable for fs dev\n", + __func__, alt); + return ret; + } + + if (in_maxp > 1023 || out_maxp > 1023) { + ERROR(cdev, "%s:alt%d too much max packet size for fs dev\n", + __func__, alt); + return ret; + } + } else { + if (in_multi > 3 || out_multi > 3) { + ERROR(cdev, "%s:alt%d incorrect multi setting\n", + __func__, alt); + return ret; + } + + if (in_maxp > 1024 || out_maxp > 1024) { + ERROR(cdev, "%s:alt%d too much max packet size\n", + __func__, alt); + return ret; + } + } + + return 0; +} + +static void __free_iso_intf_desc(struct usb_function *f) +{ + int idx; + + for (idx = 0; idx < MAX_ISO_DESC_HEADERS; idx++) + if (fs_iso_ss_descs[idx]) { + kfree(fs_iso_ss_descs[idx]); + fs_iso_ss_descs[idx] = NULL; + } + + for (idx = 0; idx < MAX_ISO_DESC_HEADERS; idx++) + if (hs_iso_ss_descs[idx]) { + kfree(hs_iso_ss_descs[idx]); + hs_iso_ss_descs[idx] = NULL; + } +} + +static int __build_iso_intf_desc(struct usb_function *f, int hs) +{ + struct f_iso_sourcesink *ss = func_to_iso_ss(f); + struct usb_composite_dev *cdev = ss->function.config->cdev; + struct iso_alt *ialt; + int alt = 0; + int idx = 0; + struct usb_interface_descriptor *intf; + struct usb_endpoint_descriptor *endp; + int ret = -ENOMEM; + struct usb_descriptor_header **header; + int alt_num; + int in_maxp, in_maxp_alt; + int out_maxp, out_maxp_alt; + + in_maxp = out_maxp = 0; + in_maxp_alt = out_maxp_alt = 0; + + if (hs) { + header = hs_iso_ss_descs; + alt_num = ss->hs_alt_num; + ialt = ss->hs_alt; + } else { + header = fs_iso_ss_descs; + alt_num = ss->fs_alt_num; + ialt = ss->fs_alt; + } + + while (alt < alt_num) { + int multi; + + intf = kmalloc(sizeof(struct usb_interface_descriptor), + GFP_KERNEL); + if (!intf) + goto failed; + + memcpy(intf, &iso_source_sink_intf_alt0, + sizeof(struct usb_interface_descriptor)); + intf->bAlternateSetting = alt; + intf->bNumEndpoints = ialt[alt].has_in + ialt[alt].has_out; + ialt[alt].intf_desc = intf; + header[idx++] = (struct usb_descriptor_header *)intf; + + if (ialt[alt].has_in) { + endp = kmalloc(sizeof(struct usb_endpoint_descriptor), + GFP_KERNEL); + if (!endp) + goto failed; + multi = ialt[alt].in_multi; + if (multi) + multi -= 1; + endp->bLength = USB_DT_ENDPOINT_SIZE; + endp->bDescriptorType = USB_DT_ENDPOINT; + endp->bEndpointAddress = USB_DIR_IN; + endp->bmAttributes = USB_ENDPOINT_XFER_ISOC; + endp->wMaxPacketSize = + __constant_cpu_to_le16(ialt[alt].in_maxp | (multi << 11)); + endp->bInterval = ialt[alt].in_interval; + ialt[alt].source_ep_desc = endp; + header[idx++] = (struct usb_descriptor_header *)endp; + + if (in_maxp < (ialt[alt].in_maxp * (multi + 1))) { + in_maxp = ialt[alt].in_maxp * (multi + 1); + in_maxp_alt = alt; + } + } + + if (ialt[alt].has_out) { + endp = kmalloc(sizeof(struct usb_endpoint_descriptor), + GFP_KERNEL); + if (!endp) + goto failed; + multi = ialt[alt].out_multi; + if (multi) + multi -= 1; + endp->bLength = USB_DT_ENDPOINT_SIZE; + endp->bDescriptorType = USB_DT_ENDPOINT; + endp->bEndpointAddress = USB_DIR_OUT; + endp->bmAttributes = USB_ENDPOINT_XFER_ISOC; + endp->wMaxPacketSize = + __constant_cpu_to_le16(ialt[alt].out_maxp|(multi << 11)); + endp->bInterval = ialt[alt].out_interval; + ialt[alt].sink_ep_desc = endp; + header[idx++] = (struct usb_descriptor_header *)endp; + if (out_maxp < (ialt[alt].out_maxp * (multi + 1))) { + out_maxp = ialt[alt].out_maxp * (multi + 1); + out_maxp_alt = alt; + } + } + alt++; + } + + if (hs) { + ss->hs_in_maxp_alt = in_maxp_alt; + ss->hs_out_maxp_alt = out_maxp_alt; + VDBG(cdev, "%s: hs altsettingnum=%d max_in_alt=%d max_out_alt=%d\n", + __func__, alt, ss->hs_in_maxp_alt, ss->hs_out_maxp_alt); + } else { + ss->fs_in_maxp_alt = in_maxp_alt; + ss->fs_out_maxp_alt = out_maxp_alt; + VDBG(cdev, "%s: fs altsettingnum=%d max_in_alt=%d max_out_alt=%d\n", + __func__, alt, ss->fs_in_maxp_alt, ss->fs_out_maxp_alt); + } + return 0; +failed: + __free_iso_intf_desc(f); + return ret; +} + +/* Scaning and parsing the string pointed by @para, we build + * whole interface descriptors(may include multiple altsetting) + * for function @f. + * */ +static int build_iso_desc(char *para, struct usb_function *f) +{ + struct f_iso_sourcesink *ss = func_to_iso_ss(f); + struct usb_composite_dev *cdev = ss->function.config->cdev; + int hs = 0; + int alt = 0; + int in_multi = 0, out_multi = 0; + int in_maxp = 0, out_maxp = 0; + int in_interval = 0, out_interval = 0; + int ret = 0; + char *scan = para; + struct iso_alt *ialt; + + scan = strstr(para, "fs:"); + + while (scan && *scan) { + scan = strstr(scan, ":"); + if (!scan || *(scan+1) == '\0') + break; + scan++; + if (!strncmp(scan, "hs:", 3)) { + hs = 1; + scan += 3; + } + + DBG(cdev, "str=%s\n", scan); + + alt = 0; + in_maxp = in_multi = in_interval = 0; + out_maxp = out_multi = out_interval = 0; + ret = parse_iso_altsetting(scan, &alt, &in_maxp, &in_multi, + &in_interval, &out_maxp, + &out_multi, &out_interval); + + DBG(cdev, "alt=%d return=%d hs=%d\n", alt, ret, hs); + DBG(cdev, "in_maxp =%4d in_mulit =%4d in_interval =%4d\n", + in_maxp, in_multi, in_interval); + DBG(cdev, "out_maxp=%4d out_mulit=%4d out_interval=%4d\n\n", + out_maxp, out_multi, out_interval); + if (ret < 0) { + ERROR(cdev, "%s: pasrse altsetting failed, ret=%d\n", + __func__, ret); + goto failed; + } + + ret = check_iso_alt(f, hs, alt, in_maxp, in_multi, in_interval, + out_maxp, out_multi, out_interval); + if (ret) + goto failed; + + if (hs) { + ialt = ss->hs_alt; + ss->hs_alt_num++; + } else { + ialt = ss->fs_alt; + ss->fs_alt_num++; + } + + ialt[alt].has_in = in_maxp ? 1 : 0; + ialt[alt].has_out = out_maxp ? 1 : 0; + ialt[alt].in_maxp = (unsigned short)in_maxp; + ialt[alt].in_multi = (unsigned char)in_multi; + ialt[alt].in_interval = (unsigned short)in_interval; + ialt[alt].out_maxp = (unsigned short)out_maxp; + ialt[alt].out_multi = (unsigned char)out_multi; + ialt[alt].out_interval = (unsigned short)out_interval; + + VDBG(cdev, "%s:alt%d: in_maxp=%d,in_multi=%d,in_interval=%d\n", + hs ? "hs" : "fs", alt, ialt[alt].in_maxp, + ialt[alt].in_multi, + ialt[alt].in_interval); + VDBG(cdev, " out_maxp=%d,out_multi=%d,out_interval=%d\n", + ialt[alt].out_maxp, ialt[alt].out_multi, + ialt[alt].out_interval); + } + + ret = __build_iso_intf_desc(f, 0); + if (ret) + goto failed; + + ret = __build_iso_intf_desc(f, 1); + if (ret) + goto failed; + + ss->curr_alt = 0; + + return 0; +failed: + return ret; +} + +static void iso_update_ep_addr(struct usb_function *f, + struct usb_endpoint_descriptor *in, + struct usb_endpoint_descriptor *out, + int hs) +{ + struct f_iso_sourcesink *ss = func_to_iso_ss(f); + int idx, num; + struct iso_alt *ialt; + struct usb_endpoint_descriptor *ep; + + if (hs) { + ialt = ss->hs_alt; + num = ss->hs_alt_num; + } else { + ialt = ss->fs_alt; + num = ss->fs_alt_num; + } + + if (in) + for (idx = 0; idx < num; idx++) { + ep = ialt[idx].source_ep_desc; + if (ialt[idx].has_in && ep) + ep->bEndpointAddress = in->bEndpointAddress; + } + if (out) + for (idx = 0; idx < num; idx++) { + ep = ialt[idx].sink_ep_desc; + if (ialt[idx].has_out && ep) + ep->bEndpointAddress = out->bEndpointAddress; + } +} + +/*-------------------------------------------------------------------------*/ +static int __init +iso_sourcesink_bind(struct usb_configuration *c, struct usb_function *f) +{ + struct usb_composite_dev *cdev = c->cdev; + struct f_iso_sourcesink *ss = func_to_iso_ss(f); + int id; + int ret, hs; + struct iso_alt *ialt; + struct usb_endpoint_descriptor *max_in; + struct usb_endpoint_descriptor *max_out; + struct usb_endpoint_descriptor *fs_max_in; + struct usb_endpoint_descriptor *fs_max_out; + const char *in_name, *out_name; + + /* allocate interface ID(s) */ + id = usb_interface_id(c, f); + if (id < 0) { + ERROR(cdev, "%s: can't obtain intf id on %s\n", + f->name, cdev->gadget->name); + return id; + } + iso_source_sink_intf_alt0.bInterfaceNumber = id; + + ret = build_iso_desc(iso_intf_desc, f); + if (ret < 0) { + ERROR(cdev, "%s: can't build iso desc on %s\n", + f->name, cdev->gadget->name); + return -ENODEV; + } + + if (gadget_is_dualspeed(c->cdev->gadget)) + hs = 1; + else + hs = 0; + + /*use the ep desc with the strongest capability to configure hw ep*/ + ialt = ss->hs_alt; + max_in = ialt[(int)ss->hs_in_maxp_alt].source_ep_desc; + max_out = ialt[(int)ss->hs_out_maxp_alt].sink_ep_desc; + + ialt = ss->fs_alt; + fs_max_in = ialt[(int)ss->fs_in_maxp_alt].source_ep_desc; + fs_max_out = ialt[(int)ss->fs_out_maxp_alt].sink_ep_desc; + + max_in = max_in ? : fs_max_in; + max_out = max_out ? : fs_max_out; + + if (!max_in) + goto out; + + /* allocate endpoints */ + ss->in_ep = usb_ep_autoconfig(cdev->gadget, max_in); + if (!ss->in_ep) { +autoconf_fail: + ERROR(cdev, "%s: can't autoconfigure on %s\n", + f->name, cdev->gadget->name); + return -ENODEV; + } + ss->in_ep->driver_data = cdev; /* claim */ + +out: + if (!max_out) + goto update_addr; + ss->out_ep = usb_ep_autoconfig(cdev->gadget, max_out); + if (!ss->out_ep) + goto autoconf_fail; + ss->out_ep->driver_data = cdev; /* claim */ + +update_addr: + iso_update_ep_addr(f, max_in, max_out, 0); + if (hs) { + iso_update_ep_addr(f, max_in, max_out, 1); + f->hs_descriptors = hs_iso_ss_descs; + } + + in_name = ss->in_ep ? ss->in_ep->name : "none"; + out_name = ss->out_ep ? ss->out_ep->name : "none"; + DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n", + gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full", + f->name, in_name, out_name); + return 0; +} + +static void +iso_sourcesink_unbind(struct usb_configuration *c, struct usb_function *f) +{ + __free_iso_intf_desc(f); + kfree(func_to_iso_ss(f)); +} + +static unsigned char iso_count; + +static void iso_source_sink_complete(struct usb_ep *ep, struct usb_request *req) +{ + struct f_iso_sourcesink *ss = ep->driver_data; + struct usb_composite_dev *cdev = ss->function.config->cdev; + int status = req->status; + + switch (status) { + + case 0: /* normal completion? */ + memset(req->buf, iso_count++, req->length); + break; + + /* this endpoint is normally active while we're configured */ + case -ECONNABORTED: /* hardware forced ep reset */ + case -ECONNRESET: /* request dequeued */ + case -ESHUTDOWN: /* disconnect from host */ + VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status, + req->actual, req->length); + free_ep_req(ep, req); + return; + + case -EOVERFLOW: /* buffer overrun on read means that + * we didn't provide a big enough + * buffer. + */ + default: + DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name, + status, req->actual, req->length); + case -EREMOTEIO: /* short read */ + break; + } + + status = usb_ep_queue(ep, req, GFP_ATOMIC); + if (status) { + ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n", + ep->name, req->length, status); + usb_ep_set_halt(ep); + /* FIXME recover later ... somehow */ + } +} + +static int iso_source_sink_start_ep(struct f_iso_sourcesink *ss, bool is_in) +{ + struct usb_ep *ep; + struct usb_request *req; + int status; + + ep = is_in ? ss->in_ep : ss->out_ep; + if (!ep) + return 0; + + req = alloc_ep_req(ep); + if (!req) + return -ENOMEM; + + req->complete = iso_source_sink_complete; + memset(req->buf, iso_count++, req->length); + + status = usb_ep_queue(ep, req, GFP_ATOMIC); + if (status) { + struct usb_composite_dev *cdev; + + cdev = ss->function.config->cdev; + ERROR(cdev, "start %s %s --> %d\n", + is_in ? "IN" : "OUT", + ep->name, status); + free_ep_req(ep, req); + } + + return status; +} + +static void disable_iso_source_sink(struct f_iso_sourcesink *ss) +{ + struct usb_composite_dev *cdev; + + cdev = ss->function.config->cdev; + disable_endpoints(cdev, ss->in_ep, ss->out_ep); + VDBG(cdev, "%s disabled\n", ss->function.name); +} + +static int iso_qlen = 8; +static int +enable_iso_source_sink(struct usb_composite_dev *cdev, + struct f_iso_sourcesink *ss) +{ + int result = 0; + const struct usb_endpoint_descriptor *src, *sink; + struct usb_ep *ep; + struct usb_endpoint_descriptor *hs, *fs; + int idx = 0; + + hs = ss->hs_alt[ss->curr_alt].source_ep_desc; + fs = ss->fs_alt[ss->curr_alt].source_ep_desc; + src = ep_choose(cdev->gadget, hs, fs); + + hs = ss->hs_alt[ss->curr_alt].sink_ep_desc; + fs = ss->fs_alt[ss->curr_alt].sink_ep_desc; + sink = ep_choose(cdev->gadget, hs, fs); + + if (!src) + goto out; + + /* one endpoint writes (sources) zeroes IN (to the host) */ + ep = ss->in_ep; + result = usb_ep_enable(ep, src); + if (result < 0) + return result; + + ep->driver_data = ss; + + for (idx = 0; idx < iso_qlen; idx++) { + result = iso_source_sink_start_ep(ss, true); + if (result < 0) { +fail: + ep = ss->in_ep; + usb_ep_disable(ep); + ep->driver_data = NULL; + return result; + } + } + +out: + if (!sink) + goto exit; + + /* one endpoint reads (sinks) anything OUT (from the host) */ + ep = ss->out_ep; + result = usb_ep_enable(ep, sink); + if (result < 0) + goto fail; + ep->driver_data = ss; + + for (idx = 0; idx < iso_qlen; idx++) { + result = iso_source_sink_start_ep(ss, false); + if (result < 0) { + usb_ep_disable(ep); + ep->driver_data = NULL; + goto fail; + } + } +exit: + DBG(cdev, "%s enabled\n", ss->function.name); + return result; +} + +static int iso_sourcesink_set_alt(struct usb_function *f, + unsigned intf, unsigned alt) +{ + struct f_iso_sourcesink *ss = func_to_iso_ss(f); + struct usb_composite_dev *cdev = f->config->cdev; + int last_alt, num; + struct usb_gadget *g; + int ret; + + g = cdev->gadget; + if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) + num = ss->hs_alt_num; + else + num = ss->fs_alt_num; + + DBG(cdev, "%s: alt=%d last_alt=%d num=%d\n", __func__, alt, + ss->curr_alt, num); + + if (alt < 0 || alt >= num) { + ret = -EINVAL; + goto exit; + } + + last_alt = ss->curr_alt; + ss->curr_alt = alt; + + if (last_alt) + disable_iso_source_sink(ss); + + if (!alt) { + ret = 0; + goto exit; + } + ret = enable_iso_source_sink(cdev, ss); +exit: + if (ret) + ERROR(cdev, "%s: ret=%d\n", __func__, ret); + return ret; +} + +static void iso_sourcesink_disable(struct usb_function *f) +{ + struct f_iso_sourcesink *ss = func_to_iso_ss(f); + + disable_iso_source_sink(ss); +} + + +/*-------------------------------------------------------------------------*/ + +static int __init iso_sourcesink_bind_config(struct usb_configuration *c) +{ + struct f_iso_sourcesink *ss; + int status; + + ss = kzalloc(sizeof *ss, GFP_KERNEL); + if (!ss) + return -ENOMEM; + + ss->function.name = "iso source/sink"; + ss->function.descriptors = fs_iso_ss_descs; + ss->function.bind = iso_sourcesink_bind; + ss->function.unbind = iso_sourcesink_unbind; + ss->function.set_alt = iso_sourcesink_set_alt; + ss->function.disable = iso_sourcesink_disable; + + status = usb_add_function(c, &ss->function); + if (status) + kfree(ss); + return status; +} + +static int iso_sourcesink_setup(struct usb_configuration *c, + const struct usb_ctrlrequest *ctrl) +{ + struct usb_request *req = c->cdev->req; + int value = -EOPNOTSUPP; + u16 w_index = le16_to_cpu(ctrl->wIndex); + u16 w_value = le16_to_cpu(ctrl->wValue); + u16 w_length = le16_to_cpu(ctrl->wLength); + + /* composite driver infrastructure handles everything except + * the two control test requests. + */ + switch (ctrl->bRequest) { + + /* + * These are the same vendor-specific requests supported by + * Intel's USB 2.0 compliance test devices. We exceed that + * device spec by allowing multiple-packet requests. + * + * NOTE: the Control-OUT data stays in req->buf ... better + * would be copying it into a scratch buffer, so that other + * requests may safely intervene. + */ + case 0x5b: /* control WRITE test -- fill the buffer */ + if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR)) + goto unknown; + if (w_value || w_index) + break; + /* just read that many bytes into the buffer */ + if (w_length > req->length) + break; + value = w_length; + break; + case 0x5c: /* control READ test -- return the buffer */ + if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR)) + goto unknown; + if (w_value || w_index) + break; + /* expect those bytes are still in the buffer; send back */ + if (w_length > req->length) + break; + value = w_length; + break; + + default: +unknown: + VDBG(c->cdev, + "unknown control req%02x.%02x v%04x i%04x l%d\n", + ctrl->bRequestType, ctrl->bRequest, + w_value, w_index, w_length); + } + + /* respond with data transfer or status phase? */ + if (value >= 0) { + VDBG(c->cdev, "source/sink req%02x.%02x v%04x i%04x l%d\n", + ctrl->bRequestType, ctrl->bRequest, + w_value, w_index, w_length); + req->zero = 0; + req->length = value; + value = usb_ep_queue(c->cdev->gadget->ep0, req, GFP_ATOMIC); + if (value < 0) + ERROR(c->cdev, "source/sinkc response, err %d\n", + value); + } + + /* device either stalls (value < 0) or reports success */ + return value; +} + +static struct usb_configuration iso_sourcesink_driver = { + .label = "iso source/sink", + .strings = iso_sourcesink_strings, + .setup = iso_sourcesink_setup, + .bConfigurationValue = 4, + .bmAttributes = USB_CONFIG_ATT_SELFPOWER, + /* .iConfiguration = DYNAMIC */ +}; + + + +/** + * sourcesink_add - add a source/sink testing configuration to a device + * @cdev: the device to support the configuration + */ +int __init iso_sourcesink_add(struct usb_composite_dev *cdev, + bool autoresume) +{ + int id; + + /* allocate string ID(s) */ + id = usb_string_id(cdev); + if (id < 0) + return id; + strings_iso_sourcesink[0].id = id; + + iso_source_sink_intf_alt0.iInterface = id; + iso_sourcesink_driver.iConfiguration = id; + + /* support autoresume for remote wakeup testing */ + if (autoresume) + iso_sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP; + + /* support OTG systems */ + if (gadget_is_otg(cdev->gadget)) { + iso_sourcesink_driver.descriptors = otg_desc; + iso_sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP; + } + + return usb_add_config(cdev, &iso_sourcesink_driver, + iso_sourcesink_bind_config); +} diff --git a/drivers/usb/gadget/zero.c b/drivers/usb/gadget/zero.c index 6d16db9..134ded6 100644 --- a/drivers/usb/gadget/zero.c +++ b/drivers/usb/gadget/zero.c @@ -74,6 +74,7 @@ #include "f_sourcesink.c" #include "f_loopback.c" +#include "f_iso.c" /*-------------------------------------------------------------------------*/ @@ -93,6 +94,17 @@ module_param(buflen, uint, 0); static int loopdefault = 0; module_param(loopdefault, bool, S_IRUGO|S_IWUSR); +/* + * Normally the "iso" configuration is third (index 2) so + * it's not the default. Here's where to change that order, to + * work better with hosts where config changes are problematic or + * controllers (like original superh) that only support one config. + */ +static int isodefault; +module_param(isodefault, bool, S_IRUGO|S_IWUSR); + + + /*-------------------------------------------------------------------------*/ /* Thanks to NetChip Technologies for donating this product ID. @@ -129,7 +141,7 @@ static struct usb_device_descriptor device_desc = { .idVendor = cpu_to_le16(DRIVER_VENDOR_NUM), .idProduct = cpu_to_le16(DRIVER_PRODUCT_NUM), - .bNumConfigurations = 2, + .bNumConfigurations = 3, }; #ifdef CONFIG_USB_OTG @@ -217,8 +229,10 @@ static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep) void disable_endpoints(struct usb_composite_dev *cdev, struct usb_ep *in, struct usb_ep *out) { - disable_ep(cdev, in); - disable_ep(cdev, out); + if (in) + disable_ep(cdev, in); + if (out) + disable_ep(cdev, out); } /*-------------------------------------------------------------------------*/ @@ -269,6 +283,7 @@ static int __init zero_bind(struct usb_composite_dev *cdev) int gcnum; struct usb_gadget *gadget = cdev->gadget; int id; + int ret; /* Allocate string descriptor numbers ... note that string * contents can be overridden by the composite_dev glue. @@ -299,10 +314,17 @@ static int __init zero_bind(struct usb_composite_dev *cdev) if (loopdefault) { loopback_add(cdev, autoresume != 0); sourcesink_add(cdev, autoresume != 0); + ret = iso_sourcesink_add(cdev, autoresume != 0); + } else if (isodefault) { + ret = iso_sourcesink_add(cdev, autoresume != 0); + sourcesink_add(cdev, autoresume != 0); + loopback_add(cdev, autoresume != 0); } else { sourcesink_add(cdev, autoresume != 0); + ret = iso_sourcesink_add(cdev, autoresume != 0); loopback_add(cdev, autoresume != 0); } + INFO(cdev, "ISO configuration: %s\n", ret ? "not support" : "support"); gcnum = usb_gadget_controller_number(gadget); if (gcnum >= 0) -- 1.7.3 -- To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html