From 70fccb3f2469f5cfd75ad17c6e452a382fbabbcf Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Wed, 11 Aug 2010 17:54:00 -0400 Subject: usb: musb: stub out MUSB_TXCSR_MODE for Blackfin parts The MUSB_TXCSR_MODE register setting isn't supported on Blackfin musb parts, so stub it out to 0. This matches Linux behavior. Signed-off-by: Mike Frysinger --- drivers/usb/musb/musb_core.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'drivers') diff --git a/drivers/usb/musb/musb_core.h b/drivers/usb/musb/musb_core.h index 4771876..8f73876 100644 --- a/drivers/usb/musb/musb_core.h +++ b/drivers/usb/musb/musb_core.h @@ -369,6 +369,8 @@ extern void read_fifo(u8 ep, u32 length, void *fifo_data); # define readb(addr) (u8)bfin_read16(addr) # undef writeb # define writeb(b, addr) bfin_write16(addr, b) +# undef MUSB_TXCSR_MODE /* not supported */ +# define MUSB_TXCSR_MODE 0 /* * The USB PHY on current Blackfin processors is a UTMI+ level 2 PHY. * However, it has no ULPI support - so there are no registers at all. -- cgit v1.1 From 23cd138503f90ff6af109c0096727ba641942614 Mon Sep 17 00:00:00 2001 From: Remy Bohmer Date: Wed, 29 Jul 2009 18:18:43 +0200 Subject: Integrate USB gadget layer and USB CDC driver layer Derived from Linux kernel 2.6.27 Signed-off-by: Thomas Smits Signed-off-by: Remy Bohmer --- drivers/usb/gadget/Makefile | 3 + drivers/usb/gadget/config.c | 119 +++ drivers/usb/gadget/epautoconf.c | 306 ++++++ drivers/usb/gadget/ether.c | 1947 +++++++++++++++++++++++++++++++++++++ drivers/usb/gadget/gadget_chips.h | 219 +++++ drivers/usb/gadget/usbstring.c | 134 +++ 6 files changed, 2728 insertions(+) create mode 100644 drivers/usb/gadget/config.c create mode 100644 drivers/usb/gadget/epautoconf.c create mode 100644 drivers/usb/gadget/ether.c create mode 100644 drivers/usb/gadget/gadget_chips.h create mode 100644 drivers/usb/gadget/usbstring.c (limited to 'drivers') diff --git a/drivers/usb/gadget/Makefile b/drivers/usb/gadget/Makefile index 1d7362d..9b1b55b 100644 --- a/drivers/usb/gadget/Makefile +++ b/drivers/usb/gadget/Makefile @@ -25,6 +25,7 @@ include $(TOPDIR)/config.mk LIB := $(obj)libusb_gadget.a +# Devices not related to the new gadget layer depend on CONFIG_USB_DEVICE ifdef CONFIG_USB_DEVICE COBJS-y += core.o COBJS-y += ep0.o @@ -34,6 +35,8 @@ COBJS-$(CONFIG_MPC885_FAMILY) += mpc8xx_udc.o COBJS-$(CONFIG_PXA27X) += pxa27x_udc.o COBJS-$(CONFIG_SPEARUDC) += spr_udc.o endif +# new USB gadget layer dependencies +COBJS-$(CONFIG_USB_ETHER) += ether.o epautoconf.o config.o usbstring.o COBJS := $(COBJS-y) SRCS := $(COBJS:.o=.c) diff --git a/drivers/usb/gadget/config.c b/drivers/usb/gadget/config.c new file mode 100644 index 0000000..84c90f0 --- /dev/null +++ b/drivers/usb/gadget/config.c @@ -0,0 +1,119 @@ +/* + * usb/gadget/config.c -- simplify building config descriptors + * + * Copyright (C) 2003 David Brownell + * + * 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 + * + * Ported to U-boot by: Thomas Smits and + * Remy Bohmer + */ + +#include +#include +#include +#include + +#include +#include + + +/** + * usb_descriptor_fillbuf - fill buffer with descriptors + * @buf: Buffer to be filled + * @buflen: Size of buf + * @src: Array of descriptor pointers, terminated by null pointer. + * + * Copies descriptors into the buffer, returning the length or a + * negative error code if they can't all be copied. Useful when + * assembling descriptors for an associated set of interfaces used + * as part of configuring a composite device; or in other cases where + * sets of descriptors need to be marshaled. + */ +int +usb_descriptor_fillbuf(void *buf, unsigned buflen, + const struct usb_descriptor_header **src) +{ + u8 *dest = buf; + + if (!src) + return -EINVAL; + + /* fill buffer from src[] until null descriptor ptr */ + for (; NULL != *src; src++) { + unsigned len = (*src)->bLength; + + if (len > buflen) + return -EINVAL; + memcpy(dest, *src, len); + buflen -= len; + dest += len; + } + return dest - (u8 *)buf; +} + + +/** + * usb_gadget_config_buf - builts a complete configuration descriptor + * @config: Header for the descriptor, including characteristics such + * as power requirements and number of interfaces. + * @desc: Null-terminated vector of pointers to the descriptors (interface, + * endpoint, etc) defining all functions in this device configuration. + * @buf: Buffer for the resulting configuration descriptor. + * @length: Length of buffer. If this is not big enough to hold the + * entire configuration descriptor, an error code will be returned. + * + * This copies descriptors into the response buffer, building a descriptor + * for that configuration. It returns the buffer length or a negative + * status code. The config.wTotalLength field is set to match the length + * of the result, but other descriptor fields (including power usage and + * interface count) must be set by the caller. + * + * Gadget drivers could use this when constructing a config descriptor + * in response to USB_REQ_GET_DESCRIPTOR. They will need to patch the + * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed. + */ +int usb_gadget_config_buf( + const struct usb_config_descriptor *config, + void *buf, + unsigned length, + const struct usb_descriptor_header **desc +) +{ + struct usb_config_descriptor *cp = buf; + int len; + + /* config descriptor first */ + if (length < USB_DT_CONFIG_SIZE || !desc) + return -EINVAL; + *cp = *config; + + /* then interface/endpoint/class/vendor/... */ + len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8*)buf, + length - USB_DT_CONFIG_SIZE, desc); + if (len < 0) + return len; + len += USB_DT_CONFIG_SIZE; + if (len > 0xffff) + return -EINVAL; + + /* patch up the config descriptor */ + cp->bLength = USB_DT_CONFIG_SIZE; + cp->bDescriptorType = USB_DT_CONFIG; + cp->wTotalLength = cpu_to_le16(len); + cp->bmAttributes |= USB_CONFIG_ATT_ONE; + return len; +} + diff --git a/drivers/usb/gadget/epautoconf.c b/drivers/usb/gadget/epautoconf.c new file mode 100644 index 0000000..c7fad39 --- /dev/null +++ b/drivers/usb/gadget/epautoconf.c @@ -0,0 +1,306 @@ +/* + * epautoconf.c -- endpoint autoconfiguration for usb gadget drivers + * + * Copyright (C) 2004 David Brownell + * + * 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 + * + * Ported to U-boot by: Thomas Smits and + * Remy Bohmer + */ + +#include +#include +#include +#include +#include "gadget_chips.h" + +#define isdigit(c) ('0' <= (c) && (c) <= '9') + +/* we must assign addresses for configurable endpoints (like net2280) */ +static unsigned epnum; + +// #define MANY_ENDPOINTS +#ifdef MANY_ENDPOINTS +/* more than 15 configurable endpoints */ +static unsigned in_epnum; +#endif + + +/* + * This should work with endpoints from controller drivers sharing the + * same endpoint naming convention. By example: + * + * - ep1, ep2, ... address is fixed, not direction or type + * - ep1in, ep2out, ... address and direction are fixed, not type + * - ep1-bulk, ep2-bulk, ... address and type are fixed, not direction + * - ep1in-bulk, ep2out-iso, ... all three are fixed + * - ep-* ... no functionality restrictions + * + * Type suffixes are "-bulk", "-iso", or "-int". Numbers are decimal. + * Less common restrictions are implied by gadget_is_*(). + * + * NOTE: each endpoint is unidirectional, as specified by its USB + * descriptor; and isn't specific to a configuration or altsetting. + */ +static int +ep_matches ( + struct usb_gadget *gadget, + struct usb_ep *ep, + struct usb_endpoint_descriptor *desc +) +{ + u8 type; + const char *tmp; + u16 max; + + /* endpoint already claimed? */ + if (NULL != ep->driver_data) + return 0; + + /* only support ep0 for portable CONTROL traffic */ + type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK; + if (USB_ENDPOINT_XFER_CONTROL == type) + return 0; + + /* some other naming convention */ + if ('e' != ep->name[0]) + return 0; + + /* type-restriction: "-iso", "-bulk", or "-int". + * direction-restriction: "in", "out". + */ + if ('-' != ep->name[2]) { + tmp = strrchr (ep->name, '-'); + if (tmp) { + switch (type) { + case USB_ENDPOINT_XFER_INT: + /* bulk endpoints handle interrupt transfers, + * except the toggle-quirky iso-synch kind + */ + if ('s' == tmp[2]) // == "-iso" + return 0; + /* for now, avoid PXA "interrupt-in"; + * it's documented as never using DATA1. + */ + if (gadget_is_pxa (gadget) + && 'i' == tmp [1]) + return 0; + break; + case USB_ENDPOINT_XFER_BULK: + if ('b' != tmp[1]) // != "-bulk" + return 0; + break; + case USB_ENDPOINT_XFER_ISOC: + if ('s' != tmp[2]) // != "-iso" + return 0; + } + } else { + tmp = ep->name + strlen (ep->name); + } + + /* direction-restriction: "..in-..", "out-.." */ + tmp--; + if (!isdigit (*tmp)) { + if (desc->bEndpointAddress & USB_DIR_IN) { + if ('n' != *tmp) + return 0; + } else { + if ('t' != *tmp) + return 0; + } + } + } + + /* endpoint maxpacket size is an input parameter, except for bulk + * where it's an output parameter representing the full speed limit. + * the usb spec fixes high speed bulk maxpacket at 512 bytes. + */ + max = 0x7ff & le16_to_cpu(desc->wMaxPacketSize); + switch (type) { + case USB_ENDPOINT_XFER_INT: + /* INT: limit 64 bytes full speed, 1024 high speed */ + if (!gadget->is_dualspeed && max > 64) + return 0; + /* FALLTHROUGH */ + + case USB_ENDPOINT_XFER_ISOC: + /* ISO: limit 1023 bytes full speed, 1024 high speed */ + if (ep->maxpacket < max) + return 0; + if (!gadget->is_dualspeed && max > 1023) + return 0; + + /* BOTH: "high bandwidth" works only at high speed */ + if ((desc->wMaxPacketSize & __constant_cpu_to_le16(3<<11))) { + if (!gadget->is_dualspeed) + return 0; + /* configure your hardware with enough buffering!! */ + } + break; + } + + /* MATCH!! */ + + /* report address */ + if (isdigit (ep->name [2])) { + u8 num = simple_strtol (&ep->name [2], NULL, 10); + desc->bEndpointAddress |= num; +#ifdef MANY_ENDPOINTS + } else if (desc->bEndpointAddress & USB_DIR_IN) { + if (++in_epnum > 15) + return 0; + desc->bEndpointAddress = USB_DIR_IN | in_epnum; +#endif + } else { + if (++epnum > 15) + return 0; + desc->bEndpointAddress |= epnum; + } + + /* report (variable) full speed bulk maxpacket */ + if (USB_ENDPOINT_XFER_BULK == type) { + int size = ep->maxpacket; + + /* min() doesn't work on bitfields with gcc-3.5 */ + if (size > 64) + size = 64; + desc->wMaxPacketSize = cpu_to_le16(size); + } + return 1; +} + +static struct usb_ep * +find_ep (struct usb_gadget *gadget, const char *name) +{ + struct usb_ep *ep; + + list_for_each_entry (ep, &gadget->ep_list, ep_list) { + if (0 == strcmp (ep->name, name)) + return ep; + } + return NULL; +} + +/** + * usb_ep_autoconfig - choose an endpoint matching the descriptor + * @gadget: The device to which the endpoint must belong. + * @desc: Endpoint descriptor, with endpoint direction and transfer mode + * initialized. For periodic transfers, the maximum packet + * size must also be initialized. This is modified on success. + * + * By choosing an endpoint to use with the specified descriptor, this + * routine simplifies writing gadget drivers that work with multiple + * USB device controllers. The endpoint would be passed later to + * usb_ep_enable(), along with some descriptor. + * + * That second descriptor won't always be the same as the first one. + * For example, isochronous endpoints can be autoconfigured for high + * bandwidth, and then used in several lower bandwidth altsettings. + * Also, high and full speed descriptors will be different. + * + * Be sure to examine and test the results of autoconfiguration on your + * hardware. This code may not make the best choices about how to use the + * USB controller, and it can't know all the restrictions that may apply. + * Some combinations of driver and hardware won't be able to autoconfigure. + * + * On success, this returns an un-claimed usb_ep, and modifies the endpoint + * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value + * is initialized as if the endpoint were used at full speed. To prevent + * the endpoint from being returned by a later autoconfig call, claim it + * by assigning ep->driver_data to some non-null value. + * + * On failure, this returns a null endpoint descriptor. + */ +struct usb_ep * usb_ep_autoconfig ( + struct usb_gadget *gadget, + struct usb_endpoint_descriptor *desc +) +{ + struct usb_ep *ep; + u8 type; + + type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK; + + /* First, apply chip-specific "best usage" knowledge. + * This might make a good usb_gadget_ops hook ... + */ + if (gadget_is_net2280 (gadget) && type == USB_ENDPOINT_XFER_INT) { + /* ep-e, ep-f are PIO with only 64 byte fifos */ + ep = find_ep (gadget, "ep-e"); + if (ep && ep_matches (gadget, ep, desc)) + return ep; + ep = find_ep (gadget, "ep-f"); + if (ep && ep_matches (gadget, ep, desc)) + return ep; + + } else if (gadget_is_goku (gadget)) { + if (USB_ENDPOINT_XFER_INT == type) { + /* single buffering is enough */ + ep = find_ep (gadget, "ep3-bulk"); + if (ep && ep_matches (gadget, ep, desc)) + return ep; + } else if (USB_ENDPOINT_XFER_BULK == type + && (USB_DIR_IN & desc->bEndpointAddress)) { + /* DMA may be available */ + ep = find_ep (gadget, "ep2-bulk"); + if (ep && ep_matches (gadget, ep, desc)) + return ep; + } + + } else if (gadget_is_sh (gadget) && USB_ENDPOINT_XFER_INT == type) { + /* single buffering is enough; maybe 8 byte fifo is too */ + ep = find_ep (gadget, "ep3in-bulk"); + if (ep && ep_matches (gadget, ep, desc)) + return ep; + + } else if (gadget_is_mq11xx (gadget) && USB_ENDPOINT_XFER_INT == type) { + ep = find_ep (gadget, "ep1-bulk"); + if (ep && ep_matches (gadget, ep, desc)) + return ep; + } + + /* Second, look at endpoints until an unclaimed one looks usable */ + list_for_each_entry (ep, &gadget->ep_list, ep_list) { + if (ep_matches (gadget, ep, desc)) + return ep; + } + + /* Fail */ + return NULL; +} + +/** + * usb_ep_autoconfig_reset - reset endpoint autoconfig state + * @gadget: device for which autoconfig state will be reset + * + * Use this for devices where one configuration may need to assign + * endpoint resources very differently from the next one. It clears + * state such as ep->driver_data and the record of assigned endpoints + * used by usb_ep_autoconfig(). + */ +void usb_ep_autoconfig_reset (struct usb_gadget *gadget) +{ + struct usb_ep *ep; + + list_for_each_entry (ep, &gadget->ep_list, ep_list) { + ep->driver_data = NULL; + } +#ifdef MANY_ENDPOINTS + in_epnum = 0; +#endif + epnum = 0; +} + diff --git a/drivers/usb/gadget/ether.c b/drivers/usb/gadget/ether.c new file mode 100644 index 0000000..3d871fa --- /dev/null +++ b/drivers/usb/gadget/ether.c @@ -0,0 +1,1947 @@ +/* + * ether.c -- Ethernet gadget driver, with CDC and non-CDC options + * + * Copyright (C) 2003-2005,2008 David Brownell + * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger + * Copyright (C) 2008 Nokia Corporation + * + * 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 + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "gadget_chips.h" + +#define USB_NET_NAME "usb0" +#define dprintf(x, ...) +#undef INFO +#define INFO(x, s...) printf(s) +#define dev_err(x, stuff...) printf(stuff) +#define dev_dbg dev_err +#define dev_warn dev_err +#define DEBUG dev_err +#define VDEBUG DEBUG +#define atomic_read +extern struct platform_data brd; +#define spin_lock(x) +#define spin_unlock(x) + + +unsigned packet_received, packet_sent; + +#define DEV_CONFIG_CDC 1 +#define GFP_ATOMIC ((gfp_t) 0) +#define GFP_KERNEL ((gfp_t) 0) + +/* + * Ethernet gadget driver -- with CDC and non-CDC options + * Builds on hardware support for a full duplex link. + * + * CDC Ethernet is the standard USB solution for sending Ethernet frames + * using USB. Real hardware tends to use the same framing protocol but look + * different for control features. This driver strongly prefers to use + * this USB-IF standard as its open-systems interoperability solution; + * most host side USB stacks (except from Microsoft) support it. + * + * This is sometimes called "CDC ECM" (Ethernet Control Model) to support + * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new + * "CDC EEM" (Ethernet Emulation Model) is starting to spread. + * + * There's some hardware that can't talk CDC ECM. We make that hardware + * implement a "minimalist" vendor-agnostic CDC core: same framing, but + * link-level setup only requires activating the configuration. Only the + * endpoint descriptors, and product/vendor IDs, are relevant; no control + * operations are available. Linux supports it, but other host operating + * systems may not. (This is a subset of CDC Ethernet.) + * + * It turns out that if you add a few descriptors to that "CDC Subset", + * (Windows) host side drivers from MCCI can treat it as one submode of + * a proprietary scheme called "SAFE" ... without needing to know about + * specific product/vendor IDs. So we do that, making it easier to use + * those MS-Windows drivers. Those added descriptors make it resemble a + * CDC MDLM device, but they don't change device behavior at all. (See + * MCCI Engineering report 950198 "SAFE Networking Functions".) + * + * A third option is also in use. Rather than CDC Ethernet, or something + * simpler, Microsoft pushes their own approach: RNDIS. The published + * RNDIS specs are ambiguous and appear to be incomplete, and are also + * needlessly complex. They borrow more from CDC ACM than CDC ECM. + */ +#define ETH_ALEN 6 /* Octets in one ethernet addr */ +#define ETH_HLEN 14 /* Total octets in header. */ +#define ETH_ZLEN 60 /* Min. octets in frame sans FCS */ +#define ETH_DATA_LEN 1500 /* Max. octets in payload */ +#define ETH_FRAME_LEN PKTSIZE_ALIGN /* Max. octets in frame sans FCS */ +#define ETH_FCS_LEN 4 /* Octets in the FCS */ + +#define DRIVER_DESC "Ethernet Gadget" +/* Based on linux 2.6.27 version */ +#define DRIVER_VERSION "May Day 2005" + +static const char shortname [] = "ether"; +static const char driver_desc [] = DRIVER_DESC; + +#define RX_EXTRA 20 /* guard against rx overflows */ + +/* CDC support the same host-chosen outgoing packet filters. */ +#define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \ + |USB_CDC_PACKET_TYPE_ALL_MULTICAST \ + |USB_CDC_PACKET_TYPE_PROMISCUOUS \ + |USB_CDC_PACKET_TYPE_DIRECTED) + +#define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ) + +/*-------------------------------------------------------------------------*/ +static struct eth_dev l_ethdev; +static struct eth_device l_netdev; +static struct usb_gadget_driver eth_driver; + +/*-------------------------------------------------------------------------*/ + +/* "main" config is either CDC, or its simple subset */ +static inline int is_cdc(struct eth_dev *dev) +{ +#if !defined(DEV_CONFIG_SUBSET) + return 1; /* only cdc possible */ +#elif !defined (DEV_CONFIG_CDC) + return 0; /* only subset possible */ +#else + return dev->cdc; /* depends on what hardware we found */ +#endif +} + +#define subset_active(dev) (!is_cdc(dev)) +#define cdc_active(dev) ( is_cdc(dev)) + +#define DEFAULT_QLEN 2 /* double buffering by default */ + +/* peak bulk transfer bits-per-second */ +#define HS_BPS (13 * 512 * 8 * 1000 * 8) +#define FS_BPS (19 * 64 * 1 * 1000 * 8) + +#ifdef CONFIG_USB_GADGET_DUALSPEED +#define DEVSPEED USB_SPEED_HIGH + +/* for dual-speed hardware, use deeper queues at highspeed */ +#define qlen(gadget) \ + (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1)) + +static inline int BITRATE(struct usb_gadget *g) +{ + return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS; +} + +#else /* full speed (low speed doesn't do bulk) */ + +#define qmult 1 + +#define DEVSPEED USB_SPEED_FULL + +#define qlen(gadget) DEFAULT_QLEN + +static inline int BITRATE(struct usb_gadget *g) +{ + return FS_BPS; +} +#endif + +struct eth_dev { + struct usb_gadget *gadget; + struct usb_request *req; /* for control responses */ + struct usb_request *stat_req; /* for cdc status */ + + u8 config; + struct usb_ep *in_ep, *out_ep, *status_ep; + const struct usb_endpoint_descriptor + *in, *out, *status; + + struct usb_request *tx_req, *rx_req; + + struct eth_device *net; + unsigned int tx_qlen; + + unsigned zlp:1; + unsigned cdc:1; + unsigned suspended:1; + unsigned network_started:1; + u16 cdc_filter; + unsigned long todo; + int mtu; +#define WORK_RX_MEMORY 0 + u8 host_mac [ETH_ALEN]; +}; + +/* This version autoconfigures as much as possible at run-time. + * + * It also ASSUMES a self-powered device, without remote wakeup, + * although remote wakeup support would make sense. + */ + +/*-------------------------------------------------------------------------*/ + +/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! + * Instead: allocate your own, using normal USB-IF procedures. + */ + +/* Thanks to NetChip Technologies for donating this product ID. + * It's for devices with only CDC Ethernet configurations. + */ +#define CDC_VENDOR_NUM 0x0525 /* NetChip */ +#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */ + +/* For hardware that can't talk CDC, we use the same vendor ID that + * ARM Linux has used for ethernet-over-usb, both with sa1100 and + * with pxa250. We're protocol-compatible, if the host-side drivers + * use the endpoint descriptors. bcdDevice (version) is nonzero, so + * drivers that need to hard-wire endpoint numbers have a hook. + * + * The protocol is a minimal subset of CDC Ether, which works on any bulk + * hardware that's not deeply broken ... even on hardware that can't talk + * RNDIS (like SA-1100, with no interrupt endpoint, or anything that + * doesn't handle control-OUT). + */ +#define SIMPLE_VENDOR_NUM 0x049f +#define SIMPLE_PRODUCT_NUM 0x505a + +/* Some systems will want different product identifers published in the + * device descriptor, either numbers or strings or both. These string + * parameters are in UTF-8 (superset of ASCII's 7 bit characters). + */ + +static ushort bcdDevice; +#if defined(CONFIG_USBNET_MANUFACTURER) +static char *iManufacturer = CONFIG_USBNET_MANUFACTURER; +#else +static char *iManufacturer = "U-boot"; +#endif +static char *iProduct; +static char *iSerialNumber; +static char dev_addr[18]; +static char host_addr[18]; + +/*-------------------------------------------------------------------------*/ + +/* USB DRIVER HOOKUP (to the hardware driver, below us), mostly + * ep0 implementation: descriptors, config management, setup(). + * also optional class-specific notification interrupt transfer. + */ + +/* + * DESCRIPTORS ... most are static, but strings and (full) configuration + * descriptors are built on demand. For now we do either full CDC, or + * our simple subset. + */ + +#define STRING_MANUFACTURER 1 +#define STRING_PRODUCT 2 +#define STRING_ETHADDR 3 +#define STRING_DATA 4 +#define STRING_CONTROL 5 +#define STRING_CDC 7 +#define STRING_SUBSET 8 +#define STRING_SERIALNUMBER 10 + +/* holds our biggest descriptor */ +#define USB_BUFSIZ 256 + +/* + * This device advertises one configuration, eth_config, + * on hardware supporting at least two configs. + * + * FIXME define some higher-powered configurations to make it easier + * to recharge batteries ... + */ + +#define DEV_CONFIG_VALUE 1 /* cdc or subset */ + +static struct usb_device_descriptor +device_desc = { + .bLength = sizeof device_desc, + .bDescriptorType = USB_DT_DEVICE, + + .bcdUSB = __constant_cpu_to_le16 (0x0200), + + .bDeviceClass = USB_CLASS_COMM, + .bDeviceSubClass = 0, + .bDeviceProtocol = 0, + + .idVendor = __constant_cpu_to_le16 (CDC_VENDOR_NUM), + .idProduct = __constant_cpu_to_le16 (CDC_PRODUCT_NUM), + .iManufacturer = STRING_MANUFACTURER, + .iProduct = STRING_PRODUCT, + .bNumConfigurations = 1, +}; + +static struct usb_otg_descriptor +otg_descriptor = { + .bLength = sizeof otg_descriptor, + .bDescriptorType = USB_DT_OTG, + + .bmAttributes = USB_OTG_SRP, +}; + +static struct usb_config_descriptor +eth_config = { + .bLength = sizeof eth_config, + .bDescriptorType = USB_DT_CONFIG, + + /* compute wTotalLength on the fly */ + .bNumInterfaces = 2, + .bConfigurationValue = DEV_CONFIG_VALUE, + .iConfiguration = STRING_CDC, + .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, + .bMaxPower = 1, +}; + +/* + * Compared to the simple CDC subset, the full CDC Ethernet model adds + * three class descriptors, two interface descriptors, optional status + * endpoint. Both have a "data" interface and two bulk endpoints. + * There are also differences in how control requests are handled. + */ + +#ifdef DEV_CONFIG_CDC +static struct usb_interface_descriptor +control_intf = { + .bLength = sizeof control_intf, + .bDescriptorType = USB_DT_INTERFACE, + + .bInterfaceNumber = 0, + /* status endpoint is optional; this may be patched later */ + .bNumEndpoints = 1, + .bInterfaceClass = USB_CLASS_COMM, + .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, + .bInterfaceProtocol = USB_CDC_PROTO_NONE, + .iInterface = STRING_CONTROL, +}; +#endif + +static const struct usb_cdc_header_desc header_desc = { + .bLength = sizeof header_desc, + .bDescriptorType = USB_DT_CS_INTERFACE, + .bDescriptorSubType = USB_CDC_HEADER_TYPE, + + .bcdCDC = __constant_cpu_to_le16 (0x0110), +}; + +#if defined(DEV_CONFIG_CDC) + +static const struct usb_cdc_union_desc union_desc = { + .bLength = sizeof union_desc, + .bDescriptorType = USB_DT_CS_INTERFACE, + .bDescriptorSubType = USB_CDC_UNION_TYPE, + + .bMasterInterface0 = 0, /* index of control interface */ + .bSlaveInterface0 = 1, /* index of DATA interface */ +}; + +#endif /* CDC */ + +#ifndef DEV_CONFIG_CDC + +/* "SAFE" loosely follows CDC WMC MDLM, violating the spec in various + * ways: data endpoints live in the control interface, there's no data + * interface, and it's not used to talk to a cell phone radio. + */ + +static const struct usb_cdc_mdlm_desc mdlm_desc = { + .bLength = sizeof mdlm_desc, + .bDescriptorType = USB_DT_CS_INTERFACE, + .bDescriptorSubType = USB_CDC_MDLM_TYPE, + + .bcdVersion = __constant_cpu_to_le16(0x0100), + .bGUID = { + 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6, + 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f, + }, +}; + +/* since "usb_cdc_mdlm_detail_desc" is a variable length structure, we + * can't really use its struct. All we do here is say that we're using + * the submode of "SAFE" which directly matches the CDC Subset. + */ +static const u8 mdlm_detail_desc[] = { + 6, + USB_DT_CS_INTERFACE, + USB_CDC_MDLM_DETAIL_TYPE, + + 0, /* "SAFE" */ + 0, /* network control capabilities (none) */ + 0, /* network data capabilities ("raw" encapsulation) */ +}; + +#endif + + +static const struct usb_cdc_ether_desc ether_desc = { + .bLength = sizeof (ether_desc), + .bDescriptorType = USB_DT_CS_INTERFACE, + .bDescriptorSubType = USB_CDC_ETHERNET_TYPE, + + /* this descriptor actually adds value, surprise! */ + .iMACAddress = STRING_ETHADDR, + .bmEthernetStatistics = __constant_cpu_to_le32 (0), /* no statistics */ + .wMaxSegmentSize = __constant_cpu_to_le16 (ETH_FRAME_LEN), + .wNumberMCFilters = __constant_cpu_to_le16 (0), + .bNumberPowerFilters = 0, +}; + + +#if defined(DEV_CONFIG_CDC) + +/* include the status endpoint if we can, even where it's optional. + * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one + * packet, to simplify cancellation; and a big transfer interval, to + * waste less bandwidth. + * + * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even + * if they ignore the connect/disconnect notifications that real aether + * can provide. more advanced cdc configurations might want to support + * encapsulated commands (vendor-specific, using control-OUT). + */ + +#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */ +#define STATUS_BYTECOUNT 16 /* 8 byte header + data */ + +static struct usb_endpoint_descriptor +fs_status_desc = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + + .bEndpointAddress = USB_DIR_IN, + .bmAttributes = USB_ENDPOINT_XFER_INT, + .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT), + .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC, +}; +#endif + +#ifdef DEV_CONFIG_CDC + +/* the default data interface has no endpoints ... */ + +static const struct usb_interface_descriptor +data_nop_intf = { + .bLength = sizeof data_nop_intf, + .bDescriptorType = USB_DT_INTERFACE, + + .bInterfaceNumber = 1, + .bAlternateSetting = 0, + .bNumEndpoints = 0, + .bInterfaceClass = USB_CLASS_CDC_DATA, + .bInterfaceSubClass = 0, + .bInterfaceProtocol = 0, +}; + +/* ... but the "real" data interface has two bulk endpoints */ + +static const struct usb_interface_descriptor +data_intf = { + .bLength = sizeof data_intf, + .bDescriptorType = USB_DT_INTERFACE, + + .bInterfaceNumber = 1, + .bAlternateSetting = 1, + .bNumEndpoints = 2, + .bInterfaceClass = USB_CLASS_CDC_DATA, + .bInterfaceSubClass = 0, + .bInterfaceProtocol = 0, + .iInterface = STRING_DATA, +}; + +#endif + +#ifdef DEV_CONFIG_SUBSET + +/* + * "Simple" CDC-subset option is a simple vendor-neutral model that most + * full speed controllers can handle: one interface, two bulk endpoints. + * + * To assist host side drivers, we fancy it up a bit, and add descriptors + * so some host side drivers will understand it as a "SAFE" variant. + */ + +static const struct usb_interface_descriptor +subset_data_intf = { + .bLength = sizeof subset_data_intf, + .bDescriptorType = USB_DT_INTERFACE, + + .bInterfaceNumber = 0, + .bAlternateSetting = 0, + .bNumEndpoints = 2, + .bInterfaceClass = USB_CLASS_COMM, + .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM, + .bInterfaceProtocol = 0, + .iInterface = STRING_DATA, +}; + +#endif /* SUBSET */ + + +static struct usb_endpoint_descriptor +fs_source_desc = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + + .bEndpointAddress = USB_DIR_IN, + .bmAttributes = USB_ENDPOINT_XFER_BULK, +}; + +static struct usb_endpoint_descriptor +fs_sink_desc = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + + .bEndpointAddress = USB_DIR_OUT, + .bmAttributes = USB_ENDPOINT_XFER_BULK, +}; + +static const struct usb_descriptor_header *fs_eth_function [11] = { + (struct usb_descriptor_header *) &otg_descriptor, +#ifdef DEV_CONFIG_CDC + /* "cdc" mode descriptors */ + (struct usb_descriptor_header *) &control_intf, + (struct usb_descriptor_header *) &header_desc, + (struct usb_descriptor_header *) &union_desc, + (struct usb_descriptor_header *) ðer_desc, + /* NOTE: status endpoint may need to be removed */ + (struct usb_descriptor_header *) &fs_status_desc, + /* data interface, with altsetting */ + (struct usb_descriptor_header *) &data_nop_intf, + (struct usb_descriptor_header *) &data_intf, + (struct usb_descriptor_header *) &fs_source_desc, + (struct usb_descriptor_header *) &fs_sink_desc, + NULL, +#endif /* DEV_CONFIG_CDC */ +}; + +static inline void fs_subset_descriptors(void) +{ +#ifdef DEV_CONFIG_SUBSET + /* behavior is "CDC Subset"; extra descriptors say "SAFE" */ + fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf; + fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc; + fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc; + fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc; + fs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc; + fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc; + fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc; + fs_eth_function[8] = NULL; +#else + fs_eth_function[1] = NULL; +#endif +} + +/* + * usb 2.0 devices need to expose both high speed and full speed + * descriptors, unless they only run at full speed. + */ + +#if defined(DEV_CONFIG_CDC) +static struct usb_endpoint_descriptor +hs_status_desc = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + + .bmAttributes = USB_ENDPOINT_XFER_INT, + .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT), + .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4, +}; +#endif /* DEV_CONFIG_CDC */ + +static struct usb_endpoint_descriptor +hs_source_desc = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + + .bmAttributes = USB_ENDPOINT_XFER_BULK, + .wMaxPacketSize = __constant_cpu_to_le16 (512), +}; + +static struct usb_endpoint_descriptor +hs_sink_desc = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + + .bmAttributes = USB_ENDPOINT_XFER_BULK, + .wMaxPacketSize = __constant_cpu_to_le16 (512), +}; + +static struct usb_qualifier_descriptor +dev_qualifier = { + .bLength = sizeof dev_qualifier, + .bDescriptorType = USB_DT_DEVICE_QUALIFIER, + + .bcdUSB = __constant_cpu_to_le16 (0x0200), + .bDeviceClass = USB_CLASS_COMM, + + .bNumConfigurations = 1, +}; + +static const struct usb_descriptor_header *hs_eth_function [11] = { + (struct usb_descriptor_header *) &otg_descriptor, +#ifdef DEV_CONFIG_CDC + /* "cdc" mode descriptors */ + (struct usb_descriptor_header *) &control_intf, + (struct usb_descriptor_header *) &header_desc, + (struct usb_descriptor_header *) &union_desc, + (struct usb_descriptor_header *) ðer_desc, + /* NOTE: status endpoint may need to be removed */ + (struct usb_descriptor_header *) &hs_status_desc, + /* data interface, with altsetting */ + (struct usb_descriptor_header *) &data_nop_intf, + (struct usb_descriptor_header *) &data_intf, + (struct usb_descriptor_header *) &hs_source_desc, + (struct usb_descriptor_header *) &hs_sink_desc, + NULL, +#endif /* DEV_CONFIG_CDC */ +}; + +static inline void hs_subset_descriptors(void) +{ +#ifdef DEV_CONFIG_SUBSET + /* behavior is "CDC Subset"; extra descriptors say "SAFE" */ + hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf; + hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc; + hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc; + hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc; + hs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc; + hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc; + hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc; + hs_eth_function[8] = NULL; +#else + hs_eth_function[1] = NULL; +#endif +} + +/* maxpacket and other transfer characteristics vary by speed. */ +static inline struct usb_endpoint_descriptor * +ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs, + struct usb_endpoint_descriptor *fs) +{ + if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) + return hs; + return fs; +} + + +/*-------------------------------------------------------------------------*/ + +/* descriptors that are built on-demand */ + +static char manufacturer [50]; +static char product_desc [40] = DRIVER_DESC; +static char serial_number [20]; + +/* address that the host will use ... usually assigned at random */ +static char ethaddr [2 * ETH_ALEN + 1]; + +/* static strings, in UTF-8 */ +static struct usb_string strings [] = { + { STRING_MANUFACTURER, manufacturer, }, + { STRING_PRODUCT, product_desc, }, + { STRING_SERIALNUMBER, serial_number, }, + { STRING_DATA, "Ethernet Data", }, + { STRING_ETHADDR, ethaddr, }, +#ifdef DEV_CONFIG_CDC + { STRING_CDC, "CDC Ethernet", }, + { STRING_CONTROL, "CDC Communications Control", }, +#endif +#ifdef DEV_CONFIG_SUBSET + { STRING_SUBSET, "CDC Ethernet Subset", }, +#endif + { } /* end of list */ +}; + +static struct usb_gadget_strings stringtab = { + .language = 0x0409, /* en-us */ + .strings = strings, +}; + + +/*============================================================================*/ +static u8 control_req[USB_BUFSIZ]; +static u8 status_req[STATUS_BYTECOUNT]; + + + +/** + * strlcpy - Copy a %NUL terminated string into a sized buffer + * @dest: Where to copy the string to + * @src: Where to copy the string from + * @size: size of destination buffer + * + * Compatible with *BSD: the result is always a valid + * NUL-terminated string that fits in the buffer (unless, + * of course, the buffer size is zero). It does not pad + * out the result like strncpy() does. + */ +size_t strlcpy(char *dest, const char *src, size_t size) +{ + size_t ret = strlen(src); + + if (size) { + size_t len = (ret >= size) ? size - 1 : ret; + memcpy(dest, src, len); + dest[len] = '\0'; + } + return ret; +} + + +/*============================================================================*/ + +/* + * one config, two interfaces: control, data. + * complications: class descriptors, and an altsetting. + */ +static int +config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg) +{ + int len; + const struct usb_config_descriptor *config; + const struct usb_descriptor_header **function; + int hs = 0; + + if (gadget_is_dualspeed(g)) { + hs = (g->speed == USB_SPEED_HIGH); + if (type == USB_DT_OTHER_SPEED_CONFIG) + hs = !hs; + } +#define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function) + + if (index >= device_desc.bNumConfigurations) + return -EINVAL; + + config = ð_config; + function = which_fn (eth); + + /* for now, don't advertise srp-only devices */ + if (!is_otg) + function++; + + len = usb_gadget_config_buf (config, buf, USB_BUFSIZ, function); + if (len < 0) + return len; + ((struct usb_config_descriptor *) buf)->bDescriptorType = type; + return len; +} + +/*-------------------------------------------------------------------------*/ + +static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags); + +static int +set_ether_config (struct eth_dev *dev, gfp_t gfp_flags) +{ + int result = 0; + struct usb_gadget *gadget = dev->gadget; + +#if defined(DEV_CONFIG_CDC) + /* status endpoint used for (optionally) CDC */ + if (!subset_active(dev) && dev->status_ep) { + dev->status = ep_desc (gadget, &hs_status_desc, + &fs_status_desc); + dev->status_ep->driver_data = dev; + + result = usb_ep_enable (dev->status_ep, dev->status); + if (result != 0) { + printf ("enable %s --> %d\n", + dev->status_ep->name, result); + goto done; + } + } +#endif + + dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc); + dev->in_ep->driver_data = dev; + + dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc); + dev->out_ep->driver_data = dev; + + /* With CDC, the host isn't allowed to use these two data + * endpoints in the default altsetting for the interface. + * so we don't activate them yet. Reset from SET_INTERFACE. + */ + if (!cdc_active(dev)) { + result = usb_ep_enable (dev->in_ep, dev->in); + if (result != 0) { + printf ("enable %s --> %d\n", + dev->in_ep->name, result); + goto done; + } + + result = usb_ep_enable (dev->out_ep, dev->out); + if (result != 0) { + printf ("enable %s --> %d\n", + dev->out_ep->name, result); + goto done; + } + } + +done: + if (result == 0) + result = alloc_requests (dev, qlen (gadget), gfp_flags); + + /* on error, disable any endpoints */ + if (result < 0) { + if (!subset_active(dev)) + (void) usb_ep_disable (dev->status_ep); + dev->status = NULL; + (void) usb_ep_disable (dev->in_ep); + (void) usb_ep_disable (dev->out_ep); + dev->in = NULL; + dev->out = NULL; + } + + /* caller is responsible for cleanup on error */ + return result; +} + + +static void eth_reset_config (struct eth_dev *dev) +{ + if (dev->config == 0) + return; + + /* disable endpoints, forcing (synchronous) completion of + * pending i/o. then free the requests. + */ + + if (dev->in) { + usb_ep_disable (dev->in_ep); + if (dev->tx_req) { + usb_ep_free_request (dev->in_ep, dev->tx_req); + dev->tx_req=NULL; + } + } + if (dev->out) { + usb_ep_disable (dev->out_ep); + if (dev->rx_req) { + usb_ep_free_request (dev->in_ep, dev->rx_req); + dev->rx_req=NULL; + } + } + if (dev->status) { + usb_ep_disable (dev->status_ep); + } + dev->cdc_filter = 0; + dev->config = 0; +} + +/* change our operational config. must agree with the code + * that returns config descriptors, and altsetting code. + */ +static int eth_set_config (struct eth_dev *dev, unsigned number, gfp_t gfp_flags) +{ + int result = 0; + struct usb_gadget *gadget = dev->gadget; + + if (gadget_is_sa1100 (gadget) + && dev->config + && dev->tx_qlen != 0) { + /* tx fifo is full, but we can't clear it...*/ + INFO (dev, "can't change configurations\n"); + return -ESPIPE; + } + eth_reset_config (dev); + + switch (number) { + case DEV_CONFIG_VALUE: + result = set_ether_config (dev, gfp_flags); + break; + default: + result = -EINVAL; + /* FALL THROUGH */ + case 0: + break; + } + + if (result) { + if (number) + eth_reset_config (dev); + usb_gadget_vbus_draw(dev->gadget, + gadget_is_otg(dev->gadget) ? 8 : 100); + } else { + char *speed; + unsigned power; + + power = 2 * eth_config.bMaxPower; + usb_gadget_vbus_draw(dev->gadget, power); + + switch (gadget->speed) { + case USB_SPEED_FULL: speed = "full"; break; +#ifdef CONFIG_USB_GADGET_DUALSPEED + case USB_SPEED_HIGH: speed = "high"; break; +#endif + default: speed = "?"; break; + } + + dev->config = number; + INFO (dev, "%s speed config #%d: %d mA, %s, using %s\n", + speed, number, power, driver_desc, + (cdc_active(dev)? "CDC Ethernet" + : "CDC Ethernet Subset")); + } + return result; +} + +/*-------------------------------------------------------------------------*/ + +#ifdef DEV_CONFIG_CDC + +/* The interrupt endpoint is used in CDC networking models (Ethernet, ATM) + * only to notify the host about link status changes (which we support) or + * report completion of some encapsulated command. Since + * we want this CDC Ethernet code to be vendor-neutral, we don't use that + * command mechanism; and only one status request is ever queued. + */ +static void eth_status_complete (struct usb_ep *ep, struct usb_request *req) +{ + struct usb_cdc_notification *event = req->buf; + int value = req->status; + struct eth_dev *dev = ep->driver_data; + + /* issue the second notification if host reads the first */ + if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION + && value == 0) { + __le32 *data = req->buf + sizeof *event; + + event->bmRequestType = 0xA1; + event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE; + event->wValue = __constant_cpu_to_le16 (0); + event->wIndex = __constant_cpu_to_le16 (1); + event->wLength = __constant_cpu_to_le16 (8); + + /* SPEED_CHANGE data is up/down speeds in bits/sec */ + data [0] = data [1] = cpu_to_le32 (BITRATE (dev->gadget)); + + req->length = STATUS_BYTECOUNT; + value = usb_ep_queue (ep, req, GFP_ATOMIC); + dprintf ("send SPEED_CHANGE --> %d\n", value); + if (value == 0) + return; + } else if (value != -ECONNRESET) { + dprintf("event %02x --> %d\n", + event->bNotificationType, value); + if (event->bNotificationType== + USB_CDC_NOTIFY_SPEED_CHANGE) + { + l_ethdev.network_started=1; + printf("USB network up!\n"); + } + } + req->context = NULL; +} + +static void issue_start_status (struct eth_dev *dev) +{ + struct usb_request *req = dev->stat_req; + struct usb_cdc_notification *event; + int value; + + /* flush old status + * + * FIXME ugly idiom, maybe we'd be better with just + * a "cancel the whole queue" primitive since any + * unlink-one primitive has way too many error modes. + * here, we "know" toggle is already clear... + * + * FIXME iff req->context != null just dequeue it + */ + usb_ep_disable (dev->status_ep); + usb_ep_enable (dev->status_ep, dev->status); + + /* 3.8.1 says to issue first NETWORK_CONNECTION, then + * a SPEED_CHANGE. could be useful in some configs. + */ + event = req->buf; + event->bmRequestType = 0xA1; + event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION; + event->wValue = __constant_cpu_to_le16 (1); /* connected */ + event->wIndex = __constant_cpu_to_le16 (1); + event->wLength = 0; + + req->length = sizeof *event; + req->complete = eth_status_complete; + req->context = dev; + + value = usb_ep_queue (dev->status_ep, req, GFP_ATOMIC); + if (value < 0) + printf ("status buf queue --> %d\n", value); +} + +#endif + +/*-------------------------------------------------------------------------*/ + +static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req) +{ + if (req->status || req->actual != req->length) + dprintf (/*(struct eth_dev *) ep->driver_data*/ + "setup complete --> %d, %d/%d\n", + req->status, req->actual, req->length); +} + +/* + * The setup() callback implements all the ep0 functionality that's not + * handled lower down. CDC has a number of less-common features: + * + * - two interfaces: control, and ethernet data + * - Ethernet data interface has two altsettings: default, and active + * - class-specific descriptors for the control interface + * - class-specific control requests + */ +static int +eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) +{ + struct eth_dev *dev = get_gadget_data (gadget); + struct usb_request *req = dev->req; + int value = -EOPNOTSUPP; + u16 wIndex = le16_to_cpu(ctrl->wIndex); + u16 wValue = le16_to_cpu(ctrl->wValue); + u16 wLength = le16_to_cpu(ctrl->wLength); + + /* descriptors just go into the pre-allocated ep0 buffer, + * while config change events may enable network traffic. + */ + + dprintf("eth_setup:...\n"); + + req->complete = eth_setup_complete; + switch (ctrl->bRequest) { + + case USB_REQ_GET_DESCRIPTOR: + if (ctrl->bRequestType != USB_DIR_IN) + break; + switch (wValue >> 8) { + + case USB_DT_DEVICE: + value = min (wLength, (u16) sizeof device_desc); + memcpy (req->buf, &device_desc, value); + break; + case USB_DT_DEVICE_QUALIFIER: + if (!gadget_is_dualspeed(gadget)) + break; + value = min (wLength, (u16) sizeof dev_qualifier); + memcpy (req->buf, &dev_qualifier, value); + break; + + case USB_DT_OTHER_SPEED_CONFIG: + if (!gadget_is_dualspeed(gadget)) + break; + /* FALLTHROUGH */ + case USB_DT_CONFIG: + value = config_buf(gadget, req->buf, + wValue >> 8, + wValue & 0xff, + gadget_is_otg(gadget)); + if (value >= 0) + value = min (wLength, (u16) value); + break; + + case USB_DT_STRING: + value = usb_gadget_get_string (&stringtab, + wValue & 0xff, req->buf); + + if (value >= 0) + value = min (wLength, (u16) value); + + break; + } + break; + + case USB_REQ_SET_CONFIGURATION: + if (ctrl->bRequestType != 0) + break; + if (gadget->a_hnp_support) + DEBUG (dev, "HNP available\n"); + else if (gadget->a_alt_hnp_support) + DEBUG (dev, "HNP needs a different root port\n"); + value = eth_set_config (dev, wValue, GFP_ATOMIC); + break; + case USB_REQ_GET_CONFIGURATION: + if (ctrl->bRequestType != USB_DIR_IN) + break; + *(u8 *)req->buf = dev->config; + value = min (wLength, (u16) 1); + break; + + case USB_REQ_SET_INTERFACE: + if (ctrl->bRequestType != USB_RECIP_INTERFACE + || !dev->config + || wIndex > 1) + break; + if (!cdc_active(dev) && wIndex != 0) + break; + + /* PXA hardware partially handles SET_INTERFACE; + * we need to kluge around that interference. + */ + if (gadget_is_pxa (gadget)) { + value = eth_set_config (dev, DEV_CONFIG_VALUE, + GFP_ATOMIC); + goto done_set_intf; + } + +#ifdef DEV_CONFIG_CDC + switch (wIndex) { + case 0: /* control/master intf */ + if (wValue != 0) + break; + if (dev->status) { + usb_ep_disable (dev->status_ep); + usb_ep_enable (dev->status_ep, dev->status); + } + value = 0; + break; + case 1: /* data intf */ + if (wValue > 1) + break; + usb_ep_disable (dev->in_ep); + usb_ep_disable (dev->out_ep); + + /* CDC requires the data transfers not be done from + * the default interface setting ... also, setting + * the non-default interface resets filters etc. + */ + if (wValue == 1) { + if (!cdc_active (dev)) + break; + usb_ep_enable (dev->in_ep, dev->in); + usb_ep_enable (dev->out_ep, dev->out); + dev->cdc_filter = DEFAULT_FILTER; + if (dev->status) + issue_start_status (dev); + } + + value = 0; + break; + } +#else + /* FIXME this is wrong, as is the assumption that + * all non-PXA hardware talks real CDC ... + */ + dev_warn (&gadget->dev, "set_interface ignored!\n"); +#endif /* DEV_CONFIG_CDC */ + +done_set_intf: + break; + case USB_REQ_GET_INTERFACE: + if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE) + || !dev->config + || wIndex > 1) + break; + if (!(cdc_active(dev)) && wIndex != 0) + break; + + /* for CDC, iff carrier is on, data interface is active. */ + if (wIndex != 1) + *(u8 *)req->buf = 0; + else { + /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */ + /* carrier always ok ...*/ + *(u8 *)req->buf = 1 ; + } + value = min (wLength, (u16) 1); + break; + +#ifdef DEV_CONFIG_CDC + case USB_CDC_SET_ETHERNET_PACKET_FILTER: + /* see 6.2.30: no data, wIndex = interface, + * wValue = packet filter bitmap + */ + if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE) + || !cdc_active(dev) + || wLength != 0 + || wIndex > 1) + break; + printf ("packet filter %02x\n", wValue); + dev->cdc_filter = wValue; + value = 0; + break; + + /* and potentially: + * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS: + * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER: + * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER: + * case USB_CDC_GET_ETHERNET_STATISTIC: + */ + +#endif /* DEV_CONFIG_CDC */ + + default: + printf ( + "unknown control req%02x.%02x v%04x i%04x l%d\n", + ctrl->bRequestType, ctrl->bRequest, + wValue, wIndex, wLength); + } + + /* respond with data transfer before status phase? */ + if (value >= 0) { + dprintf("respond with data transfer before status phase\n"); + req->length = value; + req->zero = value < wLength + && (value % gadget->ep0->maxpacket) == 0; + value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC); + if (value < 0) { + DEBUG (dev, "ep_queue --> %d\n", value); + req->status = 0; + eth_setup_complete (gadget->ep0, req); + } + } + + /* host either stalls (value < 0) or reports success */ + return value; +} + + +/*-------------------------------------------------------------------------*/ + +static void rx_complete (struct usb_ep *ep, struct usb_request *req); + +static int rx_submit ( struct eth_dev *dev, struct usb_request *req, \ + gfp_t gfp_flags) +{ + int retval = -ENOMEM; + size_t size; + + /* Padding up to RX_EXTRA handles minor disagreements with host. + * Normally we use the USB "terminate on short read" convention; + * so allow up to (N*maxpacket), since that memory is normally + * already allocated. Some hardware doesn't deal well with short + * reads (e.g. DMA must be N*maxpacket), so for now don't trim a + * byte off the end (to force hardware errors on overflow). + */ + + dprintf("%s\n", __func__); + + size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA); + size += dev->out_ep->maxpacket - 1; + size -= size % dev->out_ep->maxpacket; + + + /* Some platforms perform better when IP packets are aligned, + * but on at least one, checksumming fails otherwise. + */ + + req->buf = (u8 *) NetRxPackets[0]; + req->length = size; + req->complete = rx_complete; + + retval = usb_ep_queue (dev->out_ep, req, gfp_flags); + + if (retval) { + dprintf ("rx submit --> %d\n", retval); + } + return retval; +} + + +static void rx_complete (struct usb_ep *ep, struct usb_request *req) +{ + struct eth_dev *dev = ep->driver_data; + + dprintf("%s\n", __func__); + dprintf("rx status %d\n", req->status); + + packet_received=1; + + if (req) + dev->rx_req=req; +} + + +static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags) +{ + + dev->tx_req = usb_ep_alloc_request (dev->in_ep, 0); + + if (!dev->tx_req) + goto fail; + + dev->rx_req = usb_ep_alloc_request (dev->out_ep, 0); + + if (!dev->rx_req) + goto fail; + + return 0; + +fail: + DEBUG (dev, "can't alloc requests\n"); + return -1; +} + + +static void tx_complete (struct usb_ep *ep, struct usb_request *req) +{ + dprintf("%s, status: %s\n", __func__,(req->status) ? "failed":"ok"); + packet_sent=1; +} + +static inline int eth_is_promisc (struct eth_dev *dev) +{ + /* no filters for the CDC subset; always promisc */ + if (subset_active (dev)) + return 1; + return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS; +} + +#if 0 +static int eth_start_xmit (struct sk_buff *skb, struct net_device *net) +{ + struct eth_dev *dev = netdev_priv(net); + int length = skb->len; + int retval; + struct usb_request *req = NULL; + unsigned long flags; + + /* apply outgoing CDC or RNDIS filters */ + if (!eth_is_promisc (dev)) { + u8 *dest = skb->data; + + if (is_multicast_ether_addr(dest)) { + u16 type; + + /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host + * SET_ETHERNET_MULTICAST_FILTERS requests + */ + if (is_broadcast_ether_addr(dest)) + type = USB_CDC_PACKET_TYPE_BROADCAST; + else + type = USB_CDC_PACKET_TYPE_ALL_MULTICAST; + if (!(dev->cdc_filter & type)) { + dev_kfree_skb_any (skb); + return 0; + } + } + /* ignores USB_CDC_PACKET_TYPE_DIRECTED */ + } + + spin_lock_irqsave(&dev->req_lock, flags); + /* + * this freelist can be empty if an interrupt triggered disconnect() + * and reconfigured the gadget (shutting down this queue) after the + * network stack decided to xmit but before we got the spinlock. + */ + if (list_empty(&dev->tx_reqs)) { + spin_unlock_irqrestore(&dev->req_lock, flags); + return 1; + } + + req = container_of (dev->tx_reqs.next, struct usb_request, list); + list_del (&req->list); + + /* temporarily stop TX queue when the freelist empties */ + if (list_empty (&dev->tx_reqs)) + netif_stop_queue (net); + spin_unlock_irqrestore(&dev->req_lock, flags); + + /* no buffer copies needed, unless the network stack did it + * or the hardware can't use skb buffers. + * or there's not enough space for any RNDIS headers we need + */ + if (rndis_active(dev)) { + struct sk_buff *skb_rndis; + + skb_rndis = skb_realloc_headroom (skb, + sizeof (struct rndis_packet_msg_type)); + if (!skb_rndis) + goto drop; + + dev_kfree_skb_any (skb); + skb = skb_rndis; + rndis_add_hdr (skb); + length = skb->len; + } + req->buf = skb->data; + req->context = skb; + req->complete = tx_complete; + + /* use zlp framing on tx for strict CDC-Ether conformance, + * though any robust network rx path ignores extra padding. + * and some hardware doesn't like to write zlps. + */ + req->zero = 1; + if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0) + length++; + + req->length = length; + + /* throttle highspeed IRQ rate back slightly */ + if (gadget_is_dualspeed(dev->gadget)) + req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH) + ? ((atomic_read(&dev->tx_qlen) % qmult) != 0) + : 0; + + retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC); + switch (retval) { + default: + DEBUG (dev, "tx queue err %d\n", retval); + break; + case 0: + net->trans_start = jiffies; + atomic_inc (&dev->tx_qlen); + } + + if (retval) { +drop: + dev->stats.tx_dropped++; + dev_kfree_skb_any (skb); + spin_lock_irqsave(&dev->req_lock, flags); + if (list_empty (&dev->tx_reqs)) + netif_start_queue (net); + list_add (&req->list, &dev->tx_reqs); + spin_unlock_irqrestore(&dev->req_lock, flags); + } + return 0; +} + +/*-------------------------------------------------------------------------*/ +#endif + +static void eth_unbind (struct usb_gadget *gadget) +{ + struct eth_dev *dev = get_gadget_data (gadget); + + printf("eth_unbind:...\n"); + + if (dev->stat_req) { + usb_ep_free_request (dev->status_ep, dev->stat_req); + dev->stat_req = NULL; + } + + if (dev->tx_req) { + usb_ep_free_request (dev->in_ep, dev->tx_req); + dev->tx_req=NULL; + } + + if (dev->rx_req) { + usb_ep_free_request (dev->in_ep, dev->rx_req); + dev->rx_req=NULL; + } + +/* unregister_netdev (dev->net);*/ +/* free_netdev(dev->net);*/ + + set_gadget_data (gadget, NULL); +} + +static void eth_disconnect (struct usb_gadget *gadget) +{ + eth_reset_config (get_gadget_data (gadget)); +} + +static void eth_suspend (struct usb_gadget *gadget) +{ + /* Not used */ +} + +static void eth_resume (struct usb_gadget *gadget) +{ + /* Not used */ +} + +/*-------------------------------------------------------------------------*/ + +static int is_eth_addr_valid(char *str) +{ + if (strlen(str) == 17) { + int i; + char *p, *q; + uchar ea[6]; + + /* see if it looks like an ethernet address */ + + p = str; + + for (i = 0; i < 6; i++) { + char term = (i == 5 ? '\0' : ':'); + + ea[i] = simple_strtol(p, &q, 16); + + if ((q - p) != 2 || *q++ != term) + break; + + p = q; + } + + if (i == 6) /* it looks ok */ + return 1; + } + return 0; +} + +static u8 nibble (unsigned char c) +{ + if (likely (isdigit (c))) + return c - '0'; + c = toupper (c); + if (likely (isxdigit (c))) + return 10 + c - 'A'; + return 0; +} + +static int get_ether_addr(const char *str, u8 *dev_addr) +{ + if (str) { + unsigned i; + + for (i = 0; i < 6; i++) { + unsigned char num; + + if((*str == '.') || (*str == ':')) + str++; + num = nibble(*str++) << 4; + num |= (nibble(*str++)); + dev_addr [i] = num; + } + if (is_valid_ether_addr (dev_addr)) + return 0; + } + return 1; +} + +static int eth_bind(struct usb_gadget *gadget) +{ + struct eth_dev *dev = &l_ethdev; + u8 cdc = 1, zlp = 1; + struct usb_ep *in_ep, *out_ep, *status_ep = NULL; + int gcnum; + u8 tmp[7]; + + /* these flags are only ever cleared; compiler take note */ +#ifndef DEV_CONFIG_CDC + cdc = 0; +#endif + /* Because most host side USB stacks handle CDC Ethernet, that + * standard protocol is _strongly_ preferred for interop purposes. + * (By everyone except Microsoft.) + */ + if (gadget_is_pxa (gadget)) { + /* pxa doesn't support altsettings */ + cdc = 0; + } else if (gadget_is_musbhdrc(gadget)) { + /* reduce tx dma overhead by avoiding special cases */ + zlp = 0; + } else if (gadget_is_sh(gadget)) { + /* sh doesn't support multiple interfaces or configs */ + cdc = 0; + } else if (gadget_is_sa1100 (gadget)) { + /* hardware can't write zlps */ + zlp = 0; + /* sa1100 CAN do CDC, without status endpoint ... we use + * non-CDC to be compatible with ARM Linux-2.4 "usb-eth". + */ + cdc = 0; + } + + gcnum = usb_gadget_controller_number (gadget); + if (gcnum >= 0) + device_desc.bcdDevice = cpu_to_le16 (0x0300 + gcnum); + else { + /* can't assume CDC works. don't want to default to + * anything less functional on CDC-capable hardware, + * so we fail in this case. + */ + dev_err (&gadget->dev, + "controller '%s' not recognized\n", + gadget->name); + return -ENODEV; + } + + /* CDC subset ... recognized by Linux since 2.4.10, but Windows + * drivers aren't widely available. (That may be improved by + * supporting one submode of the "SAFE" variant of MDLM.) + */ + if (!cdc) { + device_desc.idVendor = + __constant_cpu_to_le16(SIMPLE_VENDOR_NUM); + device_desc.idProduct = + __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM); + } + + /* support optional vendor/distro customization */ +#if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID) + device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID); + device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID); +#endif + if (bcdDevice) + device_desc.bcdDevice = cpu_to_le16(bcdDevice); + if (iManufacturer) + strcpy (manufacturer, iManufacturer); + if (iProduct) + strcpy (product_desc, iProduct); + if (iSerialNumber) { + device_desc.iSerialNumber = STRING_SERIALNUMBER, + strcpy(serial_number, iSerialNumber); + } + + /* all we really need is bulk IN/OUT */ + usb_ep_autoconfig_reset (gadget); + in_ep = usb_ep_autoconfig (gadget, &fs_source_desc); + if (!in_ep) { +autoconf_fail: + dev_err (&gadget->dev, + "can't autoconfigure on %s\n", + gadget->name); + return -ENODEV; + } + in_ep->driver_data = in_ep; /* claim */ + + out_ep = usb_ep_autoconfig (gadget, &fs_sink_desc); + if (!out_ep) + goto autoconf_fail; + out_ep->driver_data = out_ep; /* claim */ + +#if defined(DEV_CONFIG_CDC) + /* CDC Ethernet control interface doesn't require a status endpoint. + * Since some hosts expect one, try to allocate one anyway. + */ + if (cdc) { + status_ep = usb_ep_autoconfig (gadget, &fs_status_desc); + if (status_ep) { + status_ep->driver_data = status_ep; /* claim */ + } else if (cdc) { + control_intf.bNumEndpoints = 0; + /* FIXME remove endpoint from descriptor list */ + } + } +#endif + + /* one config: cdc, else minimal subset */ + if (!cdc) { + eth_config.bNumInterfaces = 1; + eth_config.iConfiguration = STRING_SUBSET; + + /* use functions to set these up, in case we're built to work + * with multiple controllers and must override CDC Ethernet. + */ + fs_subset_descriptors(); + hs_subset_descriptors(); + } + + device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket; + usb_gadget_set_selfpowered (gadget); + + if (gadget_is_dualspeed(gadget)) { + if (!cdc) + dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC; + + /* assumes ep0 uses the same value for both speeds ... */ + dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0; + + /* and that all endpoints are dual-speed */ + hs_source_desc.bEndpointAddress = + fs_source_desc.bEndpointAddress; + hs_sink_desc.bEndpointAddress = + fs_sink_desc.bEndpointAddress; +#if defined(DEV_CONFIG_CDC) + if (status_ep) + hs_status_desc.bEndpointAddress = + fs_status_desc.bEndpointAddress; +#endif + } + + if (gadget_is_otg(gadget)) { + otg_descriptor.bmAttributes |= USB_OTG_HNP, + eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP; + eth_config.bMaxPower = 4; + } + + dev->net = &l_netdev; + strcpy (dev->net->name, USB_NET_NAME); + + dev->cdc = cdc; + dev->zlp = zlp; + + dev->in_ep = in_ep; + dev->out_ep = out_ep; + dev->status_ep = status_ep; + + /* Module params for these addresses should come from ID proms. + * The host side address is used with CDC, and commonly + * ends up in a persistent config database. It's not clear if + * host side code for the SAFE thing cares -- its original BLAN + * thing didn't, Sharp never assigned those addresses on Zaurii. + */ + get_ether_addr(dev_addr, dev->net->enetaddr); + + memset(tmp, 0, sizeof(tmp)); + memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr)); + + get_ether_addr(host_addr, dev->host_mac); + + sprintf (ethaddr, "%02X%02X%02X%02X%02X%02X", + dev->host_mac [0], dev->host_mac [1], + dev->host_mac [2], dev->host_mac [3], + dev->host_mac [4], dev->host_mac [5]); + + INFO (dev, "using %s, OUT %s IN %s%s%s\n", gadget->name, + out_ep->name, in_ep->name, + status_ep ? " STATUS " : "", + status_ep ? status_ep->name : "" + ); + INFO (dev, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n", + dev->net->enetaddr [0], dev->net->enetaddr [1], + dev->net->enetaddr [2], dev->net->enetaddr [3], + dev->net->enetaddr [4], dev->net->enetaddr [5]); + + if (cdc) { + INFO (dev, "HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n", + dev->host_mac [0], dev->host_mac [1], + dev->host_mac [2], dev->host_mac [3], + dev->host_mac [4], dev->host_mac [5]); + } + + /* use PKTSIZE (or aligned... from u-boot) and set + * wMaxSegmentSize accordingly*/ + dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/ + + /* preallocate control message data and buffer */ + dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL); + if (!dev->req) + goto fail; + dev->req->buf = control_req; + dev->req->complete = eth_setup_complete; + + /* ... and maybe likewise for status transfer */ +#if defined(DEV_CONFIG_CDC) + if (dev->status_ep) { + dev->stat_req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL); + dev->stat_req->buf = status_req; + if (!dev->stat_req) { + dev->stat_req->buf=NULL; + usb_ep_free_request (gadget->ep0, dev->req); + + goto fail; + } + dev->stat_req->context = NULL; + } +#endif + + /* finish hookup to lower layer ... */ + dev->gadget = gadget; + set_gadget_data (gadget, dev); + gadget->ep0->driver_data = dev; + + /* two kinds of host-initiated state changes: + * - iff DATA transfer is active, carrier is "on" + * - tx queueing enabled if open *and* carrier is "on" + */ + return 0; + +fail: + dev_dbg(&gadget->dev, "register_netdev failed\n"); + eth_unbind (gadget); + return -ENOMEM; +} + +static int usb_eth_init(struct eth_device* netdev, bd_t* bd) +{ + struct eth_dev *dev=&l_ethdev; + struct usb_gadget *gadget; + unsigned long ts; + unsigned long timeout = USB_CONNECT_TIMEOUT; + + if (!netdev) { + printf("ERROR: received NULL ptr\n"); + goto fail; + } + + dev->network_started = 0; + dev->tx_req = NULL; + dev->rx_req = NULL; + + packet_received = 0; + packet_sent = 0; + + gadget = dev->gadget; + usb_gadget_connect(gadget); + + if (getenv("cdc_connect_timeout")) + timeout = simple_strtoul(getenv("cdc_connect_timeout"), + NULL, 10) * CONFIG_SYS_HZ; + ts = get_timer(0); + while (!l_ethdev.network_started) + { + /* Handle control-c and timeouts */ + if (ctrlc() || (get_timer(ts) > timeout)) { + printf("The remote end did not respond in time.\n"); + goto fail; + } + usb_gadget_handle_interrupts(); + } + + rx_submit (dev, dev->rx_req, 0); + return 0; +fail: + return -1; +} + +static int usb_eth_send(struct eth_device* netdev, volatile void* packet, int length) +{ + int retval; + struct usb_request *req = NULL; + + struct eth_dev *dev = &l_ethdev; + dprintf("%s:...\n",__func__); + + req = dev->tx_req; + + req->buf = (void *)packet; + req->context = NULL; + req->complete = tx_complete; + + /* use zlp framing on tx for strict CDC-Ether conformance, + * though any robust network rx path ignores extra padding. + * and some hardware doesn't like to write zlps. + */ + req->zero = 1; + if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0) + length++; + + req->length = length; +#if 0 + /* throttle highspeed IRQ rate back slightly */ + if (gadget_is_dualspeed(dev->gadget)) + req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH) + ? ((dev->tx_qlen % qmult) != 0) : 0; +#endif + dev->tx_qlen=1; + + retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC); + + if (!retval) + dprintf("%s: packet queued\n",__func__); + while(!packet_sent) + { + packet_sent=0; + } + + return 0; +} + +static int usb_eth_recv(struct eth_device* netdev) +{ + struct eth_dev *dev = &l_ethdev; + + usb_gadget_handle_interrupts(); + + if (packet_received) + { + dprintf("%s: packet received \n",__func__); + if (dev->rx_req) + { + NetReceive(NetRxPackets[0],dev->rx_req->length); + packet_received=0; + + if (dev->rx_req) + rx_submit (dev, dev->rx_req, 0); + } + else printf("dev->rx_req invalid\n"); + } + return 0; +} + +void usb_eth_halt(struct eth_device* netdev) +{ + struct eth_dev *dev =&l_ethdev; + + if (!netdev) + { + printf("ERROR: received NULL ptr\n"); + return; + } + + usb_gadget_disconnect(dev->gadget); +} + +static struct usb_gadget_driver eth_driver = { + .speed = DEVSPEED, + + .bind = eth_bind, + .unbind = eth_unbind, + + .setup = eth_setup, + .disconnect = eth_disconnect, + + .suspend = eth_suspend, + .resume = eth_resume, +}; + +int usb_eth_initialize(bd_t *bi) +{ + int status = 0; + struct eth_device *netdev=&l_netdev; + + sprintf(netdev->name,"usb_ether"); + + netdev->init = usb_eth_init; + netdev->send = usb_eth_send; + netdev->recv = usb_eth_recv; + netdev->halt = usb_eth_halt; + +#ifdef CONFIG_MCAST_TFTP + #error not supported +#endif + /* Configure default mac-addresses for the USB ethernet device */ +#ifdef CONFIG_USBNET_DEV_ADDR + strncpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr)); +#endif +#ifdef CONFIG_USBNET_HOST_ADDR + strncpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr)); +#endif + /* Check if the user overruled the MAC addresses */ + if (getenv("usbnet_devaddr")) + strncpy(dev_addr, getenv("usbnet_devaddr"), + sizeof(dev_addr)); + + if (getenv("usbnet_hostaddr")) + strncpy(host_addr, getenv("usbnet_hostaddr"), + sizeof(host_addr)); + + /* Make sure both strings are terminated */ + dev_addr[sizeof(dev_addr)-1] = '\0'; + host_addr[sizeof(host_addr)-1] = '\0'; + + if (!is_eth_addr_valid(dev_addr)) { + printf("ERROR: Need valid 'usbnet_devaddr' to be set\n"); + status = -1; + } + if (!is_eth_addr_valid(host_addr)) { + printf("ERROR: Need valid 'usbnet_hostaddr' to be set\n"); + status = -1; + } + if (status) + goto fail; + + status = usb_gadget_register_driver(ð_driver); + if (status < 0) + goto fail; + + eth_register(netdev); + return 0; + +fail: + printf("%s failed\n", __func__ ); + return status; +} + diff --git a/drivers/usb/gadget/gadget_chips.h b/drivers/usb/gadget/gadget_chips.h new file mode 100644 index 0000000..480bc87 --- /dev/null +++ b/drivers/usb/gadget/gadget_chips.h @@ -0,0 +1,219 @@ +/* + * USB device controllers have lots of quirks. Use these macros in + * gadget drivers or other code that needs to deal with them, and which + * autoconfigures instead of using early binding to the hardware. + * + * This SHOULD eventually work like the ARM mach_is_*() stuff, driven by + * some config file that gets updated as new hardware is supported. + * (And avoiding all runtime comparisons in typical one-choice configs!) + * + * NOTE: some of these controller drivers may not be available yet. + * Some are available on 2.4 kernels; several are available, but not + * yet pushed in the 2.6 mainline tree. + * + * Ported to U-boot by: Thomas Smits and + * Remy Bohmer + */ +#ifdef CONFIG_USB_GADGET_NET2280 +#define gadget_is_net2280(g) !strcmp("net2280", (g)->name) +#else +#define gadget_is_net2280(g) 0 +#endif + +#ifdef CONFIG_USB_GADGET_AMD5536UDC +#define gadget_is_amd5536udc(g) !strcmp("amd5536udc", (g)->name) +#else +#define gadget_is_amd5536udc(g) 0 +#endif + +#ifdef CONFIG_USB_GADGET_DUMMY_HCD +#define gadget_is_dummy(g) !strcmp("dummy_udc", (g)->name) +#else +#define gadget_is_dummy(g) 0 +#endif + +#ifdef CONFIG_USB_GADGET_PXA2XX +#define gadget_is_pxa(g) !strcmp("pxa2xx_udc", (g)->name) +#else +#define gadget_is_pxa(g) 0 +#endif + +#ifdef CONFIG_USB_GADGET_GOKU +#define gadget_is_goku(g) !strcmp("goku_udc", (g)->name) +#else +#define gadget_is_goku(g) 0 +#endif + +/* SH3 UDC -- not yet ported 2.4 --> 2.6 */ +#ifdef CONFIG_USB_GADGET_SUPERH +#define gadget_is_sh(g) !strcmp("sh_udc", (g)->name) +#else +#define gadget_is_sh(g) 0 +#endif + +/* not yet stable on 2.6 (would help "original Zaurus") */ +#ifdef CONFIG_USB_GADGET_SA1100 +#define gadget_is_sa1100(g) !strcmp("sa1100_udc", (g)->name) +#else +#define gadget_is_sa1100(g) 0 +#endif + +#ifdef CONFIG_USB_GADGET_LH7A40X +#define gadget_is_lh7a40x(g) !strcmp("lh7a40x_udc", (g)->name) +#else +#define gadget_is_lh7a40x(g) 0 +#endif + +/* handhelds.org tree (?) */ +#ifdef CONFIG_USB_GADGET_MQ11XX +#define gadget_is_mq11xx(g) !strcmp("mq11xx_udc", (g)->name) +#else +#define gadget_is_mq11xx(g) 0 +#endif + +#ifdef CONFIG_USB_GADGET_OMAP +#define gadget_is_omap(g) !strcmp("omap_udc", (g)->name) +#else +#define gadget_is_omap(g) 0 +#endif + +/* not yet ported 2.4 --> 2.6 */ +#ifdef CONFIG_USB_GADGET_N9604 +#define gadget_is_n9604(g) !strcmp("n9604_udc", (g)->name) +#else +#define gadget_is_n9604(g) 0 +#endif + +/* various unstable versions available */ +#ifdef CONFIG_USB_GADGET_PXA27X +#define gadget_is_pxa27x(g) !strcmp("pxa27x_udc", (g)->name) +#else +#define gadget_is_pxa27x(g) 0 +#endif + +#ifdef CONFIG_USB_GADGET_ATMEL_USBA +#define gadget_is_atmel_usba(g) !strcmp("atmel_usba_udc", (g)->name) +#else +#define gadget_is_atmel_usba(g) 0 +#endif + +#ifdef CONFIG_USB_GADGET_S3C2410 +#define gadget_is_s3c2410(g) !strcmp("s3c2410_udc", (g)->name) +#else +#define gadget_is_s3c2410(g) 0 +#endif + +#ifdef CONFIG_USB_GADGET_AT91 +#define gadget_is_at91(g) !strcmp("at91_udc", (g)->name) +#else +#define gadget_is_at91(g) 0 +#endif + +/* status unclear */ +#ifdef CONFIG_USB_GADGET_IMX +#define gadget_is_imx(g) !strcmp("imx_udc", (g)->name) +#else +#define gadget_is_imx(g) 0 +#endif + +#ifdef CONFIG_USB_GADGET_FSL_USB2 +#define gadget_is_fsl_usb2(g) !strcmp("fsl-usb2-udc", (g)->name) +#else +#define gadget_is_fsl_usb2(g) 0 +#endif + +/* Mentor high speed function controller */ +/* from Montavista kernel (?) */ +#ifdef CONFIG_USB_GADGET_MUSBHSFC +#define gadget_is_musbhsfc(g) !strcmp("musbhsfc_udc", (g)->name) +#else +#define gadget_is_musbhsfc(g) 0 +#endif + +/* Mentor high speed "dual role" controller, in peripheral role */ +#ifdef CONFIG_USB_GADGET_MUSB_HDRC +#define gadget_is_musbhdrc(g) !strcmp("musb_hdrc", (g)->name) +#else +#define gadget_is_musbhdrc(g) 0 +#endif + +/* from Montavista kernel (?) */ +#ifdef CONFIG_USB_GADGET_MPC8272 +#define gadget_is_mpc8272(g) !strcmp("mpc8272_udc", (g)->name) +#else +#define gadget_is_mpc8272(g) 0 +#endif + +#ifdef CONFIG_USB_GADGET_M66592 +#define gadget_is_m66592(g) !strcmp("m66592_udc", (g)->name) +#else +#define gadget_is_m66592(g) 0 +#endif + + +// CONFIG_USB_GADGET_SX2 +// CONFIG_USB_GADGET_AU1X00 +// ... + + +/** + * usb_gadget_controller_number - support bcdDevice id convention + * @gadget: the controller being driven + * + * Return a 2-digit BCD value associated with the peripheral controller, + * suitable for use as part of a bcdDevice value, or a negative error code. + * + * NOTE: this convention is purely optional, and has no meaning in terms of + * any USB specification. If you want to use a different convention in your + * gadget driver firmware -- maybe a more formal revision ID -- feel free. + * + * Hosts see these bcdDevice numbers, and are allowed (but not encouraged!) + * to change their behavior accordingly. For example it might help avoiding + * some chip bug. + */ +static inline int usb_gadget_controller_number(struct usb_gadget *gadget) +{ + if (gadget_is_net2280(gadget)) + return 0x01; + else if (gadget_is_dummy(gadget)) + return 0x02; + else if (gadget_is_pxa(gadget)) + return 0x03; + else if (gadget_is_sh(gadget)) + return 0x04; + else if (gadget_is_sa1100(gadget)) + return 0x05; + else if (gadget_is_goku(gadget)) + return 0x06; + else if (gadget_is_mq11xx(gadget)) + return 0x07; + else if (gadget_is_omap(gadget)) + return 0x08; + else if (gadget_is_lh7a40x(gadget)) + return 0x09; + else if (gadget_is_n9604(gadget)) + return 0x10; + else if (gadget_is_pxa27x(gadget)) + return 0x11; + else if (gadget_is_s3c2410(gadget)) + return 0x12; + else if (gadget_is_at91(gadget)) + return 0x13; + else if (gadget_is_imx(gadget)) + return 0x14; + else if (gadget_is_musbhsfc(gadget)) + return 0x15; + else if (gadget_is_musbhdrc(gadget)) + return 0x16; + else if (gadget_is_mpc8272(gadget)) + return 0x17; + else if (gadget_is_atmel_usba(gadget)) + return 0x18; + else if (gadget_is_fsl_usb2(gadget)) + return 0x19; + else if (gadget_is_amd5536udc(gadget)) + return 0x20; + else if (gadget_is_m66592(gadget)) + return 0x21; + return -ENOENT; +} diff --git a/drivers/usb/gadget/usbstring.c b/drivers/usb/gadget/usbstring.c new file mode 100644 index 0000000..168f75f --- /dev/null +++ b/drivers/usb/gadget/usbstring.c @@ -0,0 +1,134 @@ +/* + * Copyright (C) 2003 David Brownell + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation; either version 2.1 of the License, or + * (at your option) any later version. + * + * Ported to U-boot by: Thomas Smits and + * Remy Bohmer + */ + +#include +#include +#include +#include + +#include + + +static int utf8_to_utf16le(const char *s, __le16 *cp, unsigned len) +{ + int count = 0; + u8 c; + u16 uchar; + + /* this insists on correct encodings, though not minimal ones. + * BUT it currently rejects legit 4-byte UTF-8 code points, + * which need surrogate pairs. (Unicode 3.1 can use them.) + */ + while (len != 0 && (c = (u8) *s++) != 0) { + if ((c & 0x80)) { + // 2-byte sequence: + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx + if ((c & 0xe0) == 0xc0) { + uchar = (c & 0x1f) << 6; + + c = (u8) *s++; + if ((c & 0xc0) != 0x80) + goto fail; + c &= 0x3f; + uchar |= c; + + // 3-byte sequence (most CJKV characters): + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx + } else if ((c & 0xf0) == 0xe0) { + uchar = (c & 0x0f) << 12; + + c = (u8) *s++; + if ((c & 0xc0) != 0x80) + goto fail; + c &= 0x3f; + uchar |= c << 6; + + c = (u8) *s++; + if ((c & 0xc0) != 0x80) + goto fail; + c &= 0x3f; + uchar |= c; + + /* no bogus surrogates */ + if (0xd800 <= uchar && uchar <= 0xdfff) + goto fail; + + // 4-byte sequence (surrogate pairs, currently rare): + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx + // (uuuuu = wwww + 1) + // FIXME accept the surrogate code points (only) + + } else + goto fail; + } else + uchar = c; + put_unaligned_le16(uchar, cp++); + count++; + len--; + } + return count; +fail: + return -1; +} + + +/** + * usb_gadget_get_string - fill out a string descriptor + * @table: of c strings encoded using UTF-8 + * @id: string id, from low byte of wValue in get string descriptor + * @buf: at least 256 bytes + * + * Finds the UTF-8 string matching the ID, and converts it into a + * string descriptor in utf16-le. + * Returns length of descriptor (always even) or negative errno + * + * If your driver needs stings in multiple languages, you'll probably + * "switch (wIndex) { ... }" in your ep0 string descriptor logic, + * using this routine after choosing which set of UTF-8 strings to use. + * Note that US-ASCII is a strict subset of UTF-8; any string bytes with + * the eighth bit set will be multibyte UTF-8 characters, not ISO-8859/1 + * characters (which are also widely used in C strings). + */ +int +usb_gadget_get_string (struct usb_gadget_strings *table, int id, u8 *buf) +{ + struct usb_string *s; + int len; + + /* descriptor 0 has the language id */ + if (id == 0) { + buf [0] = 4; + buf [1] = USB_DT_STRING; + buf [2] = (u8) table->language; + buf [3] = (u8) (table->language >> 8); + return 4; + } + for (s = table->strings; s && s->s; s++) + if (s->id == id) + break; + + /* unrecognized: stall. */ + if (!s || !s->s) + return -EINVAL; + + /* string descriptors have length, tag, then UTF16-LE text */ + len = min ((size_t) 126, strlen (s->s)); + memset (buf + 2, 0, 2 * len); /* zero all the bytes */ + len = utf8_to_utf16le(s->s, (__le16 *)&buf[2], len); + if (len < 0) + return -EINVAL; + buf [0] = (len + 1) * 2; + buf [1] = USB_DT_STRING; + return buf [0]; +} + -- cgit v1.1 From 7b2f3906113c5bee6e0bfc0e9ee92c2be3e99edb Mon Sep 17 00:00:00 2001 From: Vitaly Kuzmichev Date: Thu, 12 Aug 2010 16:44:39 +0400 Subject: USB-CDC: Restuct USB gadget Makefile Prohibit simultaneous usage of both old and new gadget stacks and allow UDC drivers to be dependent on CONFIG_USB_ETHER. Signed-off-by: Vitaly Kuzmichev --- drivers/usb/gadget/Makefile | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'drivers') diff --git a/drivers/usb/gadget/Makefile b/drivers/usb/gadget/Makefile index 9b1b55b..8e6b26e 100644 --- a/drivers/usb/gadget/Makefile +++ b/drivers/usb/gadget/Makefile @@ -25,6 +25,10 @@ include $(TOPDIR)/config.mk LIB := $(obj)libusb_gadget.a +# new USB gadget layer dependencies +ifdef CONFIG_USB_ETHER +COBJS-y += ether.o epautoconf.o config.o usbstring.o +else # Devices not related to the new gadget layer depend on CONFIG_USB_DEVICE ifdef CONFIG_USB_DEVICE COBJS-y += core.o @@ -35,8 +39,7 @@ COBJS-$(CONFIG_MPC885_FAMILY) += mpc8xx_udc.o COBJS-$(CONFIG_PXA27X) += pxa27x_udc.o COBJS-$(CONFIG_SPEARUDC) += spr_udc.o endif -# new USB gadget layer dependencies -COBJS-$(CONFIG_USB_ETHER) += ether.o epautoconf.o config.o usbstring.o +endif COBJS := $(COBJS-y) SRCS := $(COBJS:.o=.c) -- cgit v1.1 From 2721dbf1ddeb32df07b64314da86d32a8157e331 Mon Sep 17 00:00:00 2001 From: Vitaly Kuzmichev Date: Thu, 12 Aug 2010 16:44:40 +0400 Subject: USB-CDC: Add lost 'qmult' definition Add lost 'qmult' definition for High Speed devices and make it configurable through CONFIG_USB_ETH_QMULT. Signed-off-by: Vitaly Kuzmichev --- drivers/usb/gadget/ether.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'drivers') diff --git a/drivers/usb/gadget/ether.c b/drivers/usb/gadget/ether.c index 3d871fa..a07738f 100644 --- a/drivers/usb/gadget/ether.c +++ b/drivers/usb/gadget/ether.c @@ -140,6 +140,12 @@ static inline int is_cdc(struct eth_dev *dev) #ifdef CONFIG_USB_GADGET_DUALSPEED #define DEVSPEED USB_SPEED_HIGH +#ifdef CONFIG_USB_ETH_QMULT +#define qmult CONFIG_USB_ETH_QMULT +#else +#define qmult 5 +#endif + /* for dual-speed hardware, use deeper queues at highspeed */ #define qlen(gadget) \ (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1)) -- cgit v1.1 From 7de731859158144cf2abf1de4e79327c8a44baf8 Mon Sep 17 00:00:00 2001 From: Vitaly Kuzmichev Date: Fri, 13 Aug 2010 16:57:51 +0400 Subject: USB-CDC: Use native debug printout macros Replace Linux-like debug printout macros by native ones. Signed-off-by: Vitaly Kuzmichev Applied after removing dead code like '#define DEBUG/#undef DEBUG' --- drivers/usb/gadget/ether.c | 96 +++++++++++++++++++++------------------------- 1 file changed, 43 insertions(+), 53 deletions(-) (limited to 'drivers') diff --git a/drivers/usb/gadget/ether.c b/drivers/usb/gadget/ether.c index a07738f..5b2f6dd 100644 --- a/drivers/usb/gadget/ether.c +++ b/drivers/usb/gadget/ether.c @@ -31,14 +31,7 @@ #include "gadget_chips.h" #define USB_NET_NAME "usb0" -#define dprintf(x, ...) -#undef INFO -#define INFO(x, s...) printf(s) -#define dev_err(x, stuff...) printf(stuff) -#define dev_dbg dev_err -#define dev_warn dev_err -#define DEBUG dev_err -#define VDEBUG DEBUG + #define atomic_read extern struct platform_data brd; #define spin_lock(x) @@ -769,7 +762,7 @@ set_ether_config (struct eth_dev *dev, gfp_t gfp_flags) result = usb_ep_enable (dev->status_ep, dev->status); if (result != 0) { - printf ("enable %s --> %d\n", + debug("enable %s --> %d\n", dev->status_ep->name, result); goto done; } @@ -789,14 +782,14 @@ set_ether_config (struct eth_dev *dev, gfp_t gfp_flags) if (!cdc_active(dev)) { result = usb_ep_enable (dev->in_ep, dev->in); if (result != 0) { - printf ("enable %s --> %d\n", + debug("enable %s --> %d\n", dev->in_ep->name, result); goto done; } result = usb_ep_enable (dev->out_ep, dev->out); if (result != 0) { - printf ("enable %s --> %d\n", + debug("enable %s --> %d\n", dev->out_ep->name, result); goto done; } @@ -827,6 +820,8 @@ static void eth_reset_config (struct eth_dev *dev) if (dev->config == 0) return; + debug("%s\n", __func__); + /* disable endpoints, forcing (synchronous) completion of * pending i/o. then free the requests. */ @@ -864,7 +859,7 @@ static int eth_set_config (struct eth_dev *dev, unsigned number, gfp_t gfp_flags && dev->config && dev->tx_qlen != 0) { /* tx fifo is full, but we can't clear it...*/ - INFO (dev, "can't change configurations\n"); + error("can't change configurations"); return -ESPIPE; } eth_reset_config (dev); @@ -901,7 +896,7 @@ static int eth_set_config (struct eth_dev *dev, unsigned number, gfp_t gfp_flags } dev->config = number; - INFO (dev, "%s speed config #%d: %d mA, %s, using %s\n", + printf("%s speed config #%d: %d mA, %s, using %s\n", speed, number, power, driver_desc, (cdc_active(dev)? "CDC Ethernet" : "CDC Ethernet Subset")); @@ -941,11 +936,11 @@ static void eth_status_complete (struct usb_ep *ep, struct usb_request *req) req->length = STATUS_BYTECOUNT; value = usb_ep_queue (ep, req, GFP_ATOMIC); - dprintf ("send SPEED_CHANGE --> %d\n", value); + debug("send SPEED_CHANGE --> %d\n", value); if (value == 0) return; } else if (value != -ECONNRESET) { - dprintf("event %02x --> %d\n", + debug("event %02x --> %d\n", event->bNotificationType, value); if (event->bNotificationType== USB_CDC_NOTIFY_SPEED_CHANGE) @@ -991,7 +986,7 @@ static void issue_start_status (struct eth_dev *dev) value = usb_ep_queue (dev->status_ep, req, GFP_ATOMIC); if (value < 0) - printf ("status buf queue --> %d\n", value); + debug("status buf queue --> %d\n", value); } #endif @@ -1001,8 +996,7 @@ static void issue_start_status (struct eth_dev *dev) static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req) { if (req->status || req->actual != req->length) - dprintf (/*(struct eth_dev *) ep->driver_data*/ - "setup complete --> %d, %d/%d\n", + debug("setup complete --> %d, %d/%d\n", req->status, req->actual, req->length); } @@ -1029,7 +1023,7 @@ eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) * while config change events may enable network traffic. */ - dprintf("eth_setup:...\n"); + debug("%s\n", __func__); req->complete = eth_setup_complete; switch (ctrl->bRequest) { @@ -1078,9 +1072,9 @@ eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) if (ctrl->bRequestType != 0) break; if (gadget->a_hnp_support) - DEBUG (dev, "HNP available\n"); + debug("HNP available\n"); else if (gadget->a_alt_hnp_support) - DEBUG (dev, "HNP needs a different root port\n"); + debug("HNP needs a different root port\n"); value = eth_set_config (dev, wValue, GFP_ATOMIC); break; case USB_REQ_GET_CONFIGURATION: @@ -1145,7 +1139,7 @@ eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) /* FIXME this is wrong, as is the assumption that * all non-PXA hardware talks real CDC ... */ - dev_warn (&gadget->dev, "set_interface ignored!\n"); + debug("set_interface ignored!\n"); #endif /* DEV_CONFIG_CDC */ done_set_intf: @@ -1179,7 +1173,7 @@ done_set_intf: || wLength != 0 || wIndex > 1) break; - printf ("packet filter %02x\n", wValue); + debug("packet filter %02x\n", wValue); dev->cdc_filter = wValue; value = 0; break; @@ -1194,21 +1188,20 @@ done_set_intf: #endif /* DEV_CONFIG_CDC */ default: - printf ( - "unknown control req%02x.%02x v%04x i%04x l%d\n", + debug("unknown control req%02x.%02x v%04x i%04x l%d\n", ctrl->bRequestType, ctrl->bRequest, wValue, wIndex, wLength); } /* respond with data transfer before status phase? */ if (value >= 0) { - dprintf("respond with data transfer before status phase\n"); + debug("respond with data transfer before status phase\n"); req->length = value; req->zero = value < wLength && (value % gadget->ep0->maxpacket) == 0; value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC); if (value < 0) { - DEBUG (dev, "ep_queue --> %d\n", value); + debug("ep_queue --> %d\n", value); req->status = 0; eth_setup_complete (gadget->ep0, req); } @@ -1237,7 +1230,7 @@ static int rx_submit ( struct eth_dev *dev, struct usb_request *req, \ * byte off the end (to force hardware errors on overflow). */ - dprintf("%s\n", __func__); + debug("%s\n", __func__); size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA); size += dev->out_ep->maxpacket - 1; @@ -1255,7 +1248,7 @@ static int rx_submit ( struct eth_dev *dev, struct usb_request *req, \ retval = usb_ep_queue (dev->out_ep, req, gfp_flags); if (retval) { - dprintf ("rx submit --> %d\n", retval); + error("rx submit --> %d", retval); } return retval; } @@ -1265,8 +1258,7 @@ static void rx_complete (struct usb_ep *ep, struct usb_request *req) { struct eth_dev *dev = ep->driver_data; - dprintf("%s\n", __func__); - dprintf("rx status %d\n", req->status); + debug("%s: status %d\n", __func__, req->status); packet_received=1; @@ -1291,14 +1283,14 @@ static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags) return 0; fail: - DEBUG (dev, "can't alloc requests\n"); + error("can't alloc requests"); return -1; } static void tx_complete (struct usb_ep *ep, struct usb_request *req) { - dprintf("%s, status: %s\n", __func__,(req->status) ? "failed":"ok"); + debug("%s: status %s\n", __func__, (req->status)?"failed":"ok"); packet_sent=1; } @@ -1427,7 +1419,7 @@ static void eth_unbind (struct usb_gadget *gadget) { struct eth_dev *dev = get_gadget_data (gadget); - printf("eth_unbind:...\n"); + debug("%s...\n", __func__); if (dev->stat_req) { usb_ep_free_request (dev->status_ep, dev->stat_req); @@ -1567,8 +1559,7 @@ static int eth_bind(struct usb_gadget *gadget) * anything less functional on CDC-capable hardware, * so we fail in this case. */ - dev_err (&gadget->dev, - "controller '%s' not recognized\n", + error("controller '%s' not recognized", gadget->name); return -ENODEV; } @@ -1605,8 +1596,7 @@ static int eth_bind(struct usb_gadget *gadget) in_ep = usb_ep_autoconfig (gadget, &fs_source_desc); if (!in_ep) { autoconf_fail: - dev_err (&gadget->dev, - "can't autoconfigure on %s\n", + error("can't autoconfigure on %s\n", gadget->name); return -ENODEV; } @@ -1700,18 +1690,18 @@ autoconf_fail: dev->host_mac [2], dev->host_mac [3], dev->host_mac [4], dev->host_mac [5]); - INFO (dev, "using %s, OUT %s IN %s%s%s\n", gadget->name, + printf("using %s, OUT %s IN %s%s%s\n", gadget->name, out_ep->name, in_ep->name, status_ep ? " STATUS " : "", status_ep ? status_ep->name : "" ); - INFO (dev, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n", + printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n", dev->net->enetaddr [0], dev->net->enetaddr [1], dev->net->enetaddr [2], dev->net->enetaddr [3], dev->net->enetaddr [4], dev->net->enetaddr [5]); if (cdc) { - INFO (dev, "HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n", + printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n", dev->host_mac [0], dev->host_mac [1], dev->host_mac [2], dev->host_mac [3], dev->host_mac [4], dev->host_mac [5]); @@ -1755,7 +1745,7 @@ autoconf_fail: return 0; fail: - dev_dbg(&gadget->dev, "register_netdev failed\n"); + error("%s failed", __func__); eth_unbind (gadget); return -ENOMEM; } @@ -1768,7 +1758,7 @@ static int usb_eth_init(struct eth_device* netdev, bd_t* bd) unsigned long timeout = USB_CONNECT_TIMEOUT; if (!netdev) { - printf("ERROR: received NULL ptr\n"); + error("received NULL ptr"); goto fail; } @@ -1790,7 +1780,7 @@ static int usb_eth_init(struct eth_device* netdev, bd_t* bd) { /* Handle control-c and timeouts */ if (ctrlc() || (get_timer(ts) > timeout)) { - printf("The remote end did not respond in time.\n"); + error("The remote end did not respond in time."); goto fail; } usb_gadget_handle_interrupts(); @@ -1806,9 +1796,9 @@ static int usb_eth_send(struct eth_device* netdev, volatile void* packet, int le { int retval; struct usb_request *req = NULL; + struct eth_dev *dev = &l_ethdev; - struct eth_dev *dev = &l_ethdev; - dprintf("%s:...\n",__func__); + debug("%s:...\n", __func__); req = dev->tx_req; @@ -1836,7 +1826,7 @@ static int usb_eth_send(struct eth_device* netdev, volatile void* packet, int le retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC); if (!retval) - dprintf("%s: packet queued\n",__func__); + debug("%s: packet queued\n", __func__); while(!packet_sent) { packet_sent=0; @@ -1853,7 +1843,7 @@ static int usb_eth_recv(struct eth_device* netdev) if (packet_received) { - dprintf("%s: packet received \n",__func__); + debug("%s: packet received \n", __func__); if (dev->rx_req) { NetReceive(NetRxPackets[0],dev->rx_req->length); @@ -1862,7 +1852,7 @@ static int usb_eth_recv(struct eth_device* netdev) if (dev->rx_req) rx_submit (dev, dev->rx_req, 0); } - else printf("dev->rx_req invalid\n"); + else error("dev->rx_req invalid"); } return 0; } @@ -1873,7 +1863,7 @@ void usb_eth_halt(struct eth_device* netdev) if (!netdev) { - printf("ERROR: received NULL ptr\n"); + error("received NULL ptr"); return; } @@ -1929,11 +1919,11 @@ int usb_eth_initialize(bd_t *bi) host_addr[sizeof(host_addr)-1] = '\0'; if (!is_eth_addr_valid(dev_addr)) { - printf("ERROR: Need valid 'usbnet_devaddr' to be set\n"); + error("Need valid 'usbnet_devaddr' to be set"); status = -1; } if (!is_eth_addr_valid(host_addr)) { - printf("ERROR: Need valid 'usbnet_hostaddr' to be set\n"); + error("Need valid 'usbnet_hostaddr' to be set"); status = -1; } if (status) @@ -1947,7 +1937,7 @@ int usb_eth_initialize(bd_t *bi) return 0; fail: - printf("%s failed\n", __func__ ); + error("%s failed. error = %d", __func__, status); return status; } -- cgit v1.1 From 0129e327f424f61a9123de44c73fe1082adb3672 Mon Sep 17 00:00:00 2001 From: Vitaly Kuzmichev Date: Fri, 13 Aug 2010 17:00:16 +0400 Subject: USB-CDC: Correct freeing usb requests Fix in_ep and out_ep confusion (rx_req was allocated from out_ep, not from in_ep) and add lost dev->req freeing. Signed-off-by: Vitaly Kuzmichev --- drivers/usb/gadget/ether.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'drivers') diff --git a/drivers/usb/gadget/ether.c b/drivers/usb/gadget/ether.c index 5b2f6dd..51f5006 100644 --- a/drivers/usb/gadget/ether.c +++ b/drivers/usb/gadget/ether.c @@ -836,7 +836,7 @@ static void eth_reset_config (struct eth_dev *dev) if (dev->out) { usb_ep_disable (dev->out_ep); if (dev->rx_req) { - usb_ep_free_request (dev->in_ep, dev->rx_req); + usb_ep_free_request (dev->out_ep, dev->rx_req); dev->rx_req=NULL; } } @@ -1421,6 +1421,11 @@ static void eth_unbind (struct usb_gadget *gadget) debug("%s...\n", __func__); + /* we've already been disconnected ... no i/o is active */ + if (dev->req) { + usb_ep_free_request (gadget->ep0, dev->req); + dev->req = NULL; + } if (dev->stat_req) { usb_ep_free_request (dev->status_ep, dev->stat_req); dev->stat_req = NULL; @@ -1432,7 +1437,7 @@ static void eth_unbind (struct usb_gadget *gadget) } if (dev->rx_req) { - usb_ep_free_request (dev->in_ep, dev->rx_req); + usb_ep_free_request (dev->out_ep, dev->rx_req); dev->rx_req=NULL; } -- cgit v1.1 From 2e12abe65410bcfa4c6058a8b81231d5d62a323c Mon Sep 17 00:00:00 2001 From: Vitaly Kuzmichev Date: Fri, 13 Aug 2010 17:00:45 +0400 Subject: USB-CDC: Replace 'strcpy' by 'strlcpy' Replace 'strcpy' by more safe 'strlcpy' that is implemented in ether.c Signed-off-by: Vitaly Kuzmichev --- drivers/usb/gadget/ether.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'drivers') diff --git a/drivers/usb/gadget/ether.c b/drivers/usb/gadget/ether.c index 51f5006..283049e 100644 --- a/drivers/usb/gadget/ether.c +++ b/drivers/usb/gadget/ether.c @@ -1588,12 +1588,12 @@ static int eth_bind(struct usb_gadget *gadget) if (bcdDevice) device_desc.bcdDevice = cpu_to_le16(bcdDevice); if (iManufacturer) - strcpy (manufacturer, iManufacturer); + strlcpy (manufacturer, iManufacturer, sizeof manufacturer); if (iProduct) - strcpy (product_desc, iProduct); + strlcpy (product_desc, iProduct, sizeof product_desc); if (iSerialNumber) { device_desc.iSerialNumber = STRING_SERIALNUMBER, - strcpy(serial_number, iSerialNumber); + strlcpy(serial_number, iSerialNumber, sizeof serial_number); } /* all we really need is bulk IN/OUT */ -- cgit v1.1 From df559c1d21ba1a9ee0dcec57567907888484d877 Mon Sep 17 00:00:00 2001 From: Vitaly Kuzmichev Date: Fri, 13 Aug 2010 17:01:06 +0400 Subject: USB-CDC: Correct stat_req initialization Fix possible oops on stat_req->buf initialization and fix ep0 and status_ep confusion (last one is just intended for stat_req keeping). Signed-off-by: Vitaly Kuzmichev Signed-off-by: Stefano Babic --- drivers/usb/gadget/ether.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'drivers') diff --git a/drivers/usb/gadget/ether.c b/drivers/usb/gadget/ether.c index 283049e..8126f76 100644 --- a/drivers/usb/gadget/ether.c +++ b/drivers/usb/gadget/ether.c @@ -1726,14 +1726,13 @@ autoconf_fail: /* ... and maybe likewise for status transfer */ #if defined(DEV_CONFIG_CDC) if (dev->status_ep) { - dev->stat_req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL); - dev->stat_req->buf = status_req; + dev->stat_req = usb_ep_alloc_request(dev->status_ep, GFP_KERNEL); if (!dev->stat_req) { - dev->stat_req->buf=NULL; - usb_ep_free_request (gadget->ep0, dev->req); + usb_ep_free_request (dev->status_ep, dev->req); goto fail; } + dev->stat_req->buf = status_req; dev->stat_req->context = NULL; } #endif -- cgit v1.1 From d5292c1647d3a711a05ac99ab2077698b3b4a8a0 Mon Sep 17 00:00:00 2001 From: Vitaly Kuzmichev Date: Fri, 13 Aug 2010 17:02:29 +0400 Subject: USB-CDC: ethernet error path potential oops fix Fix potential oops on rare error path. The patch is based on commit e7b13ec9235b9fded90f826ceeb8c34548631351 (done by David Brownell ) from linux-2.6.git. Description of the issue taken from linux kernel bugzilla: (https://bugzilla.kernel.org/show_bug.cgi?id=9594) The potential error can be tracked down as follows: (1) line 807: let the second conjunct on the "if" statment be false meaning "dev->status_ep" is null. This means the "if" evaluates to false. follow thru the code until... (2) line 808: usb_ep_disable(dev->status_ep) passes in a null argument, however "usb_ep_disable" cannot handle that: (from include/linux/usb/gadget.h) 191 static inline int 192 usb_ep_disable (struct usb_ep *ep) 193 { 194 return ep->ops->disable (ep); 195 } -- Signed-off-by: Vitaly Kuzmichev --- drivers/usb/gadget/ether.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'drivers') diff --git a/drivers/usb/gadget/ether.c b/drivers/usb/gadget/ether.c index 8126f76..c601d4a 100644 --- a/drivers/usb/gadget/ether.c +++ b/drivers/usb/gadget/ether.c @@ -801,7 +801,7 @@ done: /* on error, disable any endpoints */ if (result < 0) { - if (!subset_active(dev)) + if (!subset_active(dev) && dev->status_ep) (void) usb_ep_disable (dev->status_ep); dev->status = NULL; (void) usb_ep_disable (dev->in_ep); -- cgit v1.1 From c0ef51318813c062052f91ef7c15b75bbbf15767 Mon Sep 17 00:00:00 2001 From: Vitaly Kuzmichev Date: Fri, 13 Aug 2010 17:02:41 +0400 Subject: USB-CDC: change simple_strtol to simple_strtoul The patch is based on commit bb9496c6f7e853e5d4edd5397c9d45f1968d623c (done by Julia Lawall ) from linux-2.6.git. Since num is unsigned, it would seem better to use simple_strtoul that simple_strtol. Signed-off-by: Vitaly Kuzmichev --- drivers/usb/gadget/epautoconf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'drivers') diff --git a/drivers/usb/gadget/epautoconf.c b/drivers/usb/gadget/epautoconf.c index c7fad39..e11cc20 100644 --- a/drivers/usb/gadget/epautoconf.c +++ b/drivers/usb/gadget/epautoconf.c @@ -156,7 +156,7 @@ ep_matches ( /* report address */ if (isdigit (ep->name [2])) { - u8 num = simple_strtol (&ep->name [2], NULL, 10); + u8 num = simple_strtoul (&ep->name [2], NULL, 10); desc->bEndpointAddress |= num; #ifdef MANY_ENDPOINTS } else if (desc->bEndpointAddress & USB_DIR_IN) { -- cgit v1.1 From 80460772197ea33d9e0023b1c359f5916a760fa1 Mon Sep 17 00:00:00 2001 From: Stefano Babic Date: Sun, 15 Aug 2010 14:18:59 +0200 Subject: USB-CDC: correct wrong alignment in ether.c The buffer for the status request must be word aligned because it is accessed with 32 bit pointer in the eth_status_complete function. Signed-off-by: Stefano Babic --- drivers/usb/gadget/ether.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'drivers') diff --git a/drivers/usb/gadget/ether.c b/drivers/usb/gadget/ether.c index c601d4a..9fc6a36 100644 --- a/drivers/usb/gadget/ether.c +++ b/drivers/usb/gadget/ether.c @@ -677,7 +677,7 @@ static struct usb_gadget_strings stringtab = { /*============================================================================*/ static u8 control_req[USB_BUFSIZ]; -static u8 status_req[STATUS_BYTECOUNT]; +static u8 status_req[STATUS_BYTECOUNT] __attribute__ ((aligned(4))); -- cgit v1.1 From a170f2c7973728f2e9e1b66214b0c14062c78398 Mon Sep 17 00:00:00 2001 From: Stefano Babic Date: Sun, 15 Aug 2010 14:19:00 +0200 Subject: USB-CDC: called handle_interrupts inside usb_eth_send The patch removes an endless loop in the usb_eth_send if the tx_complete is not called before going in the loop. The driver interrupt routine is called allowing the driver to check if the TX is completed. Signed-off-by: Stefano Babic --- drivers/usb/gadget/ether.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'drivers') diff --git a/drivers/usb/gadget/ether.c b/drivers/usb/gadget/ether.c index 9fc6a36..2965299 100644 --- a/drivers/usb/gadget/ether.c +++ b/drivers/usb/gadget/ether.c @@ -1801,6 +1801,8 @@ static int usb_eth_send(struct eth_device* netdev, volatile void* packet, int le int retval; struct usb_request *req = NULL; struct eth_dev *dev = &l_ethdev; + unsigned long ts; + unsigned long timeout = USB_CONNECT_TIMEOUT; debug("%s:...\n", __func__); @@ -1826,6 +1828,8 @@ static int usb_eth_send(struct eth_device* netdev, volatile void* packet, int le ? ((dev->tx_qlen % qmult) != 0) : 0; #endif dev->tx_qlen=1; + ts = get_timer(0); + packet_sent = 0; retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC); @@ -1833,7 +1837,11 @@ static int usb_eth_send(struct eth_device* netdev, volatile void* packet, int le debug("%s: packet queued\n", __func__); while(!packet_sent) { - packet_sent=0; + if (get_timer(ts) > timeout) { + printf("timeout sending packets to usb ethernet\n"); + return -1; + } + usb_gadget_handle_interrupts(); } return 0; -- cgit v1.1 From 6142e0ae0fcf8bf5a7a8d785061197ace8955cb6 Mon Sep 17 00:00:00 2001 From: Vitaly Kuzmichev Date: Mon, 13 Sep 2010 18:37:11 +0400 Subject: USB-CDC: Fix coding style issues Fixes most of checkpatch warnings and errors in USB gadget stack. The most frequently encountered problems are: 1) "(foo*)", "foo * bar", "foo* bar" 2) C99 // comments 3) No spaces before/after/around '?', ':', '=', '==', ',', '&', '(' 4) Spaces before '[' 5) Spaces between function names and '(' 6) Block braces in wrong places 7) Spaces before tabs 8) Macros with complex values not enclosed in parenthesis 9) Multiline comments start just after /* Signed-off-by: Vitaly Kuzmichev --- drivers/usb/gadget/config.c | 2 +- drivers/usb/gadget/epautoconf.c | 73 +++--- drivers/usb/gadget/ether.c | 513 ++++++++++++++++++++------------------ drivers/usb/gadget/gadget_chips.h | 51 ++-- drivers/usb/gadget/usbstring.c | 48 ++-- 5 files changed, 357 insertions(+), 330 deletions(-) (limited to 'drivers') diff --git a/drivers/usb/gadget/config.c b/drivers/usb/gadget/config.c index 84c90f0..cf22629 100644 --- a/drivers/usb/gadget/config.c +++ b/drivers/usb/gadget/config.c @@ -101,7 +101,7 @@ int usb_gadget_config_buf( *cp = *config; /* then interface/endpoint/class/vendor/... */ - len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8*)buf, + len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8 *)buf, length - USB_DT_CONFIG_SIZE, desc); if (len < 0) return len; diff --git a/drivers/usb/gadget/epautoconf.c b/drivers/usb/gadget/epautoconf.c index e11cc20..7cf3c67 100644 --- a/drivers/usb/gadget/epautoconf.c +++ b/drivers/usb/gadget/epautoconf.c @@ -32,7 +32,7 @@ /* we must assign addresses for configurable endpoints (like net2280) */ static unsigned epnum; -// #define MANY_ENDPOINTS +/* #define MANY_ENDPOINTS */ #ifdef MANY_ENDPOINTS /* more than 15 configurable endpoints */ static unsigned in_epnum; @@ -55,8 +55,7 @@ static unsigned in_epnum; * NOTE: each endpoint is unidirectional, as specified by its USB * descriptor; and isn't specific to a configuration or altsetting. */ -static int -ep_matches ( +static int ep_matches( struct usb_gadget *gadget, struct usb_ep *ep, struct usb_endpoint_descriptor *desc @@ -83,37 +82,37 @@ ep_matches ( * direction-restriction: "in", "out". */ if ('-' != ep->name[2]) { - tmp = strrchr (ep->name, '-'); + tmp = strrchr(ep->name, '-'); if (tmp) { switch (type) { case USB_ENDPOINT_XFER_INT: /* bulk endpoints handle interrupt transfers, * except the toggle-quirky iso-synch kind */ - if ('s' == tmp[2]) // == "-iso" + if ('s' == tmp[2]) /* == "-iso" */ return 0; /* for now, avoid PXA "interrupt-in"; * it's documented as never using DATA1. */ - if (gadget_is_pxa (gadget) - && 'i' == tmp [1]) + if (gadget_is_pxa(gadget) + && 'i' == tmp[1]) return 0; break; case USB_ENDPOINT_XFER_BULK: - if ('b' != tmp[1]) // != "-bulk" + if ('b' != tmp[1]) /* != "-bulk" */ return 0; break; case USB_ENDPOINT_XFER_ISOC: - if ('s' != tmp[2]) // != "-iso" + if ('s' != tmp[2]) /* != "-iso" */ return 0; } } else { - tmp = ep->name + strlen (ep->name); + tmp = ep->name + strlen(ep->name); } /* direction-restriction: "..in-..", "out-.." */ tmp--; - if (!isdigit (*tmp)) { + if (!isdigit(*tmp)) { if (desc->bEndpointAddress & USB_DIR_IN) { if ('n' != *tmp) return 0; @@ -155,8 +154,8 @@ ep_matches ( /* MATCH!! */ /* report address */ - if (isdigit (ep->name [2])) { - u8 num = simple_strtoul (&ep->name [2], NULL, 10); + if (isdigit(ep->name[2])) { + u8 num = simple_strtoul(&ep->name[2], NULL, 10); desc->bEndpointAddress |= num; #ifdef MANY_ENDPOINTS } else if (desc->bEndpointAddress & USB_DIR_IN) { @@ -183,12 +182,12 @@ ep_matches ( } static struct usb_ep * -find_ep (struct usb_gadget *gadget, const char *name) +find_ep(struct usb_gadget *gadget, const char *name) { struct usb_ep *ep; - list_for_each_entry (ep, &gadget->ep_list, ep_list) { - if (0 == strcmp (ep->name, name)) + list_for_each_entry(ep, &gadget->ep_list, ep_list) { + if (0 == strcmp(ep->name, name)) return ep; } return NULL; @@ -224,7 +223,7 @@ find_ep (struct usb_gadget *gadget, const char *name) * * On failure, this returns a null endpoint descriptor. */ -struct usb_ep * usb_ep_autoconfig ( +struct usb_ep *usb_ep_autoconfig( struct usb_gadget *gadget, struct usb_endpoint_descriptor *desc ) @@ -237,44 +236,44 @@ struct usb_ep * usb_ep_autoconfig ( /* First, apply chip-specific "best usage" knowledge. * This might make a good usb_gadget_ops hook ... */ - if (gadget_is_net2280 (gadget) && type == USB_ENDPOINT_XFER_INT) { + if (gadget_is_net2280(gadget) && type == USB_ENDPOINT_XFER_INT) { /* ep-e, ep-f are PIO with only 64 byte fifos */ - ep = find_ep (gadget, "ep-e"); - if (ep && ep_matches (gadget, ep, desc)) + ep = find_ep(gadget, "ep-e"); + if (ep && ep_matches(gadget, ep, desc)) return ep; - ep = find_ep (gadget, "ep-f"); - if (ep && ep_matches (gadget, ep, desc)) + ep = find_ep(gadget, "ep-f"); + if (ep && ep_matches(gadget, ep, desc)) return ep; - } else if (gadget_is_goku (gadget)) { + } else if (gadget_is_goku(gadget)) { if (USB_ENDPOINT_XFER_INT == type) { /* single buffering is enough */ - ep = find_ep (gadget, "ep3-bulk"); - if (ep && ep_matches (gadget, ep, desc)) + ep = find_ep(gadget, "ep3-bulk"); + if (ep && ep_matches(gadget, ep, desc)) return ep; } else if (USB_ENDPOINT_XFER_BULK == type && (USB_DIR_IN & desc->bEndpointAddress)) { /* DMA may be available */ - ep = find_ep (gadget, "ep2-bulk"); - if (ep && ep_matches (gadget, ep, desc)) + ep = find_ep(gadget, "ep2-bulk"); + if (ep && ep_matches(gadget, ep, desc)) return ep; } - } else if (gadget_is_sh (gadget) && USB_ENDPOINT_XFER_INT == type) { + } else if (gadget_is_sh(gadget) && USB_ENDPOINT_XFER_INT == type) { /* single buffering is enough; maybe 8 byte fifo is too */ - ep = find_ep (gadget, "ep3in-bulk"); - if (ep && ep_matches (gadget, ep, desc)) + ep = find_ep(gadget, "ep3in-bulk"); + if (ep && ep_matches(gadget, ep, desc)) return ep; - } else if (gadget_is_mq11xx (gadget) && USB_ENDPOINT_XFER_INT == type) { - ep = find_ep (gadget, "ep1-bulk"); - if (ep && ep_matches (gadget, ep, desc)) + } else if (gadget_is_mq11xx(gadget) && USB_ENDPOINT_XFER_INT == type) { + ep = find_ep(gadget, "ep1-bulk"); + if (ep && ep_matches(gadget, ep, desc)) return ep; } /* Second, look at endpoints until an unclaimed one looks usable */ - list_for_each_entry (ep, &gadget->ep_list, ep_list) { - if (ep_matches (gadget, ep, desc)) + list_for_each_entry(ep, &gadget->ep_list, ep_list) { + if (ep_matches(gadget, ep, desc)) return ep; } @@ -291,11 +290,11 @@ struct usb_ep * usb_ep_autoconfig ( * state such as ep->driver_data and the record of assigned endpoints * used by usb_ep_autoconfig(). */ -void usb_ep_autoconfig_reset (struct usb_gadget *gadget) +void usb_ep_autoconfig_reset(struct usb_gadget *gadget) { struct usb_ep *ep; - list_for_each_entry (ep, &gadget->ep_list, ep_list) { + list_for_each_entry(ep, &gadget->ep_list, ep_list) { ep->driver_data = NULL; } #ifdef MANY_ENDPOINTS diff --git a/drivers/usb/gadget/ether.c b/drivers/usb/gadget/ether.c index 2965299..bc6480c 100644 --- a/drivers/usb/gadget/ether.c +++ b/drivers/usb/gadget/ether.c @@ -89,8 +89,8 @@ unsigned packet_received, packet_sent; /* Based on linux 2.6.27 version */ #define DRIVER_VERSION "May Day 2005" -static const char shortname [] = "ether"; -static const char driver_desc [] = DRIVER_DESC; +static const char shortname[] = "ether"; +static const char driver_desc[] = DRIVER_DESC; #define RX_EXTRA 20 /* guard against rx overflows */ @@ -114,7 +114,7 @@ static inline int is_cdc(struct eth_dev *dev) { #if !defined(DEV_CONFIG_SUBSET) return 1; /* only cdc possible */ -#elif !defined (DEV_CONFIG_CDC) +#elif !defined(DEV_CONFIG_CDC) return 0; /* only subset possible */ #else return dev->cdc; /* depends on what hardware we found */ @@ -122,7 +122,7 @@ static inline int is_cdc(struct eth_dev *dev) } #define subset_active(dev) (!is_cdc(dev)) -#define cdc_active(dev) ( is_cdc(dev)) +#define cdc_active(dev) (is_cdc(dev)) #define DEFAULT_QLEN 2 /* double buffering by default */ @@ -180,15 +180,16 @@ struct eth_dev { unsigned zlp:1; unsigned cdc:1; unsigned suspended:1; - unsigned network_started:1; + unsigned network_started:1; u16 cdc_filter; unsigned long todo; - int mtu; + int mtu; #define WORK_RX_MEMORY 0 - u8 host_mac [ETH_ALEN]; + u8 host_mac[ETH_ALEN]; }; -/* This version autoconfigures as much as possible at run-time. +/* + * This version autoconfigures as much as possible at run-time. * * It also ASSUMES a self-powered device, without remote wakeup, * although remote wakeup support would make sense. @@ -196,17 +197,20 @@ struct eth_dev { /*-------------------------------------------------------------------------*/ -/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! +/* + * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! * Instead: allocate your own, using normal USB-IF procedures. */ -/* Thanks to NetChip Technologies for donating this product ID. +/* + * Thanks to NetChip Technologies for donating this product ID. * It's for devices with only CDC Ethernet configurations. */ #define CDC_VENDOR_NUM 0x0525 /* NetChip */ #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */ -/* For hardware that can't talk CDC, we use the same vendor ID that +/* + * For hardware that can't talk CDC, we use the same vendor ID that * ARM Linux has used for ethernet-over-usb, both with sa1100 and * with pxa250. We're protocol-compatible, if the host-side drivers * use the endpoint descriptors. bcdDevice (version) is nonzero, so @@ -220,7 +224,8 @@ struct eth_dev { #define SIMPLE_VENDOR_NUM 0x049f #define SIMPLE_PRODUCT_NUM 0x505a -/* Some systems will want different product identifers published in the +/* + * Some systems will want different product identifers published in the * device descriptor, either numbers or strings or both. These string * parameters are in UTF-8 (superset of ASCII's 7 bit characters). */ @@ -238,7 +243,8 @@ static char host_addr[18]; /*-------------------------------------------------------------------------*/ -/* USB DRIVER HOOKUP (to the hardware driver, below us), mostly +/* + * USB DRIVER HOOKUP (to the hardware driver, below us), mostly * ep0 implementation: descriptors, config management, setup(). * also optional class-specific notification interrupt transfer. */ @@ -276,14 +282,14 @@ device_desc = { .bLength = sizeof device_desc, .bDescriptorType = USB_DT_DEVICE, - .bcdUSB = __constant_cpu_to_le16 (0x0200), + .bcdUSB = __constant_cpu_to_le16(0x0200), .bDeviceClass = USB_CLASS_COMM, .bDeviceSubClass = 0, .bDeviceProtocol = 0, - .idVendor = __constant_cpu_to_le16 (CDC_VENDOR_NUM), - .idProduct = __constant_cpu_to_le16 (CDC_PRODUCT_NUM), + .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM), + .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM), .iManufacturer = STRING_MANUFACTURER, .iProduct = STRING_PRODUCT, .bNumConfigurations = 1, @@ -338,7 +344,7 @@ static const struct usb_cdc_header_desc header_desc = { .bDescriptorType = USB_DT_CS_INTERFACE, .bDescriptorSubType = USB_CDC_HEADER_TYPE, - .bcdCDC = __constant_cpu_to_le16 (0x0110), + .bcdCDC = __constant_cpu_to_le16(0x0110), }; #if defined(DEV_CONFIG_CDC) @@ -356,7 +362,8 @@ static const struct usb_cdc_union_desc union_desc = { #ifndef DEV_CONFIG_CDC -/* "SAFE" loosely follows CDC WMC MDLM, violating the spec in various +/* + * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various * ways: data endpoints live in the control interface, there's no data * interface, and it's not used to talk to a cell phone radio. */ @@ -373,7 +380,8 @@ static const struct usb_cdc_mdlm_desc mdlm_desc = { }, }; -/* since "usb_cdc_mdlm_detail_desc" is a variable length structure, we +/* + * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we * can't really use its struct. All we do here is say that we're using * the submode of "SAFE" which directly matches the CDC Subset. */ @@ -389,24 +397,23 @@ static const u8 mdlm_detail_desc[] = { #endif - static const struct usb_cdc_ether_desc ether_desc = { - .bLength = sizeof (ether_desc), + .bLength = sizeof(ether_desc), .bDescriptorType = USB_DT_CS_INTERFACE, .bDescriptorSubType = USB_CDC_ETHERNET_TYPE, /* this descriptor actually adds value, surprise! */ .iMACAddress = STRING_ETHADDR, - .bmEthernetStatistics = __constant_cpu_to_le32 (0), /* no statistics */ - .wMaxSegmentSize = __constant_cpu_to_le16 (ETH_FRAME_LEN), - .wNumberMCFilters = __constant_cpu_to_le16 (0), + .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */ + .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN), + .wNumberMCFilters = __constant_cpu_to_le16(0), .bNumberPowerFilters = 0, }; - #if defined(DEV_CONFIG_CDC) -/* include the status endpoint if we can, even where it's optional. +/* + * include the status endpoint if we can, even where it's optional. * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one * packet, to simplify cancellation; and a big transfer interval, to * waste less bandwidth. @@ -427,7 +434,7 @@ fs_status_desc = { .bEndpointAddress = USB_DIR_IN, .bmAttributes = USB_ENDPOINT_XFER_INT, - .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT), + .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT), .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC, }; #endif @@ -493,7 +500,6 @@ subset_data_intf = { #endif /* SUBSET */ - static struct usb_endpoint_descriptor fs_source_desc = { .bLength = USB_DT_ENDPOINT_SIZE, @@ -512,7 +518,7 @@ fs_sink_desc = { .bmAttributes = USB_ENDPOINT_XFER_BULK, }; -static const struct usb_descriptor_header *fs_eth_function [11] = { +static const struct usb_descriptor_header *fs_eth_function[11] = { (struct usb_descriptor_header *) &otg_descriptor, #ifdef DEV_CONFIG_CDC /* "cdc" mode descriptors */ @@ -560,7 +566,7 @@ hs_status_desc = { .bDescriptorType = USB_DT_ENDPOINT, .bmAttributes = USB_ENDPOINT_XFER_INT, - .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT), + .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT), .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4, }; #endif /* DEV_CONFIG_CDC */ @@ -571,7 +577,7 @@ hs_source_desc = { .bDescriptorType = USB_DT_ENDPOINT, .bmAttributes = USB_ENDPOINT_XFER_BULK, - .wMaxPacketSize = __constant_cpu_to_le16 (512), + .wMaxPacketSize = __constant_cpu_to_le16(512), }; static struct usb_endpoint_descriptor @@ -580,7 +586,7 @@ hs_sink_desc = { .bDescriptorType = USB_DT_ENDPOINT, .bmAttributes = USB_ENDPOINT_XFER_BULK, - .wMaxPacketSize = __constant_cpu_to_le16 (512), + .wMaxPacketSize = __constant_cpu_to_le16(512), }; static struct usb_qualifier_descriptor @@ -588,13 +594,13 @@ dev_qualifier = { .bLength = sizeof dev_qualifier, .bDescriptorType = USB_DT_DEVICE_QUALIFIER, - .bcdUSB = __constant_cpu_to_le16 (0x0200), + .bcdUSB = __constant_cpu_to_le16(0x0200), .bDeviceClass = USB_CLASS_COMM, .bNumConfigurations = 1, }; -static const struct usb_descriptor_header *hs_eth_function [11] = { +static const struct usb_descriptor_header *hs_eth_function[11] = { (struct usb_descriptor_header *) &otg_descriptor, #ifdef DEV_CONFIG_CDC /* "cdc" mode descriptors */ @@ -640,20 +646,19 @@ ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs, return fs; } - /*-------------------------------------------------------------------------*/ /* descriptors that are built on-demand */ -static char manufacturer [50]; -static char product_desc [40] = DRIVER_DESC; -static char serial_number [20]; +static char manufacturer[50]; +static char product_desc[40] = DRIVER_DESC; +static char serial_number[20]; /* address that the host will use ... usually assigned at random */ -static char ethaddr [2 * ETH_ALEN + 1]; +static char ethaddr[2 * ETH_ALEN + 1]; /* static strings, in UTF-8 */ -static struct usb_string strings [] = { +static struct usb_string strings[] = { { STRING_MANUFACTURER, manufacturer, }, { STRING_PRODUCT, product_desc, }, { STRING_SERIALNUMBER, serial_number, }, @@ -674,13 +679,11 @@ static struct usb_gadget_strings stringtab = { .strings = strings, }; - /*============================================================================*/ static u8 control_req[USB_BUFSIZ]; static u8 status_req[STATUS_BYTECOUNT] __attribute__ ((aligned(4))); - /** * strlcpy - Copy a %NUL terminated string into a sized buffer * @dest: Where to copy the string to @@ -704,7 +707,6 @@ size_t strlcpy(char *dest, const char *src, size_t size) return ret; } - /*============================================================================*/ /* @@ -730,13 +732,13 @@ config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg) return -EINVAL; config = ð_config; - function = which_fn (eth); + function = which_fn(eth); /* for now, don't advertise srp-only devices */ if (!is_otg) function++; - len = usb_gadget_config_buf (config, buf, USB_BUFSIZ, function); + len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function); if (len < 0) return len; ((struct usb_config_descriptor *) buf)->bDescriptorType = type; @@ -745,10 +747,10 @@ config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg) /*-------------------------------------------------------------------------*/ -static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags); +static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags); static int -set_ether_config (struct eth_dev *dev, gfp_t gfp_flags) +set_ether_config(struct eth_dev *dev, gfp_t gfp_flags) { int result = 0; struct usb_gadget *gadget = dev->gadget; @@ -756,11 +758,11 @@ set_ether_config (struct eth_dev *dev, gfp_t gfp_flags) #if defined(DEV_CONFIG_CDC) /* status endpoint used for (optionally) CDC */ if (!subset_active(dev) && dev->status_ep) { - dev->status = ep_desc (gadget, &hs_status_desc, + dev->status = ep_desc(gadget, &hs_status_desc, &fs_status_desc); dev->status_ep->driver_data = dev; - result = usb_ep_enable (dev->status_ep, dev->status); + result = usb_ep_enable(dev->status_ep, dev->status); if (result != 0) { debug("enable %s --> %d\n", dev->status_ep->name, result); @@ -775,19 +777,20 @@ set_ether_config (struct eth_dev *dev, gfp_t gfp_flags) dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc); dev->out_ep->driver_data = dev; - /* With CDC, the host isn't allowed to use these two data + /* + * With CDC, the host isn't allowed to use these two data * endpoints in the default altsetting for the interface. * so we don't activate them yet. Reset from SET_INTERFACE. */ if (!cdc_active(dev)) { - result = usb_ep_enable (dev->in_ep, dev->in); + result = usb_ep_enable(dev->in_ep, dev->in); if (result != 0) { debug("enable %s --> %d\n", dev->in_ep->name, result); goto done; } - result = usb_ep_enable (dev->out_ep, dev->out); + result = usb_ep_enable(dev->out_ep, dev->out); if (result != 0) { debug("enable %s --> %d\n", dev->out_ep->name, result); @@ -797,15 +800,15 @@ set_ether_config (struct eth_dev *dev, gfp_t gfp_flags) done: if (result == 0) - result = alloc_requests (dev, qlen (gadget), gfp_flags); + result = alloc_requests(dev, qlen(gadget), gfp_flags); /* on error, disable any endpoints */ if (result < 0) { if (!subset_active(dev) && dev->status_ep) - (void) usb_ep_disable (dev->status_ep); + (void) usb_ep_disable(dev->status_ep); dev->status = NULL; - (void) usb_ep_disable (dev->in_ep); - (void) usb_ep_disable (dev->out_ep); + (void) usb_ep_disable(dev->in_ep); + (void) usb_ep_disable(dev->out_ep); dev->in = NULL; dev->out = NULL; } @@ -814,59 +817,61 @@ done: return result; } - -static void eth_reset_config (struct eth_dev *dev) +static void eth_reset_config(struct eth_dev *dev) { if (dev->config == 0) return; debug("%s\n", __func__); - /* disable endpoints, forcing (synchronous) completion of + /* + * disable endpoints, forcing (synchronous) completion of * pending i/o. then free the requests. */ if (dev->in) { - usb_ep_disable (dev->in_ep); + usb_ep_disable(dev->in_ep); if (dev->tx_req) { - usb_ep_free_request (dev->in_ep, dev->tx_req); - dev->tx_req=NULL; + usb_ep_free_request(dev->in_ep, dev->tx_req); + dev->tx_req = NULL; } } if (dev->out) { - usb_ep_disable (dev->out_ep); + usb_ep_disable(dev->out_ep); if (dev->rx_req) { - usb_ep_free_request (dev->out_ep, dev->rx_req); - dev->rx_req=NULL; + usb_ep_free_request(dev->out_ep, dev->rx_req); + dev->rx_req = NULL; } } - if (dev->status) { - usb_ep_disable (dev->status_ep); - } + if (dev->status) + usb_ep_disable(dev->status_ep); + dev->cdc_filter = 0; dev->config = 0; } -/* change our operational config. must agree with the code +/* + * change our operational config. must agree with the code * that returns config descriptors, and altsetting code. */ -static int eth_set_config (struct eth_dev *dev, unsigned number, gfp_t gfp_flags) +static int eth_set_config(struct eth_dev *dev, unsigned number, + gfp_t gfp_flags) { int result = 0; struct usb_gadget *gadget = dev->gadget; - if (gadget_is_sa1100 (gadget) + if (gadget_is_sa1100(gadget) && dev->config && dev->tx_qlen != 0) { /* tx fifo is full, but we can't clear it...*/ error("can't change configurations"); return -ESPIPE; } - eth_reset_config (dev); + eth_reset_config(dev); switch (number) { case DEV_CONFIG_VALUE: - result = set_ether_config (dev, gfp_flags); + result = set_ether_config(dev, gfp_flags); break; default: result = -EINVAL; @@ -877,7 +882,7 @@ static int eth_set_config (struct eth_dev *dev, unsigned number, gfp_t gfp_flags if (result) { if (number) - eth_reset_config (dev); + eth_reset_config(dev); usb_gadget_vbus_draw(dev->gadget, gadget_is_otg(dev->gadget) ? 8 : 100); } else { @@ -888,17 +893,20 @@ static int eth_set_config (struct eth_dev *dev, unsigned number, gfp_t gfp_flags usb_gadget_vbus_draw(dev->gadget, power); switch (gadget->speed) { - case USB_SPEED_FULL: speed = "full"; break; + case USB_SPEED_FULL: + speed = "full"; break; #ifdef CONFIG_USB_GADGET_DUALSPEED - case USB_SPEED_HIGH: speed = "high"; break; + case USB_SPEED_HIGH: + speed = "high"; break; #endif - default: speed = "?"; break; + default: + speed = "?"; break; } dev->config = number; printf("%s speed config #%d: %d mA, %s, using %s\n", speed, number, power, driver_desc, - (cdc_active(dev)? "CDC Ethernet" + (cdc_active(dev) ? "CDC Ethernet" : "CDC Ethernet Subset")); } return result; @@ -908,13 +916,14 @@ static int eth_set_config (struct eth_dev *dev, unsigned number, gfp_t gfp_flags #ifdef DEV_CONFIG_CDC -/* The interrupt endpoint is used in CDC networking models (Ethernet, ATM) +/* + * The interrupt endpoint is used in CDC networking models (Ethernet, ATM) * only to notify the host about link status changes (which we support) or * report completion of some encapsulated command. Since * we want this CDC Ethernet code to be vendor-neutral, we don't use that * command mechanism; and only one status request is ever queued. */ -static void eth_status_complete (struct usb_ep *ep, struct usb_request *req) +static void eth_status_complete(struct usb_ep *ep, struct usb_request *req) { struct usb_cdc_notification *event = req->buf; int value = req->status; @@ -927,38 +936,38 @@ static void eth_status_complete (struct usb_ep *ep, struct usb_request *req) event->bmRequestType = 0xA1; event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE; - event->wValue = __constant_cpu_to_le16 (0); - event->wIndex = __constant_cpu_to_le16 (1); - event->wLength = __constant_cpu_to_le16 (8); + event->wValue = __constant_cpu_to_le16(0); + event->wIndex = __constant_cpu_to_le16(1); + event->wLength = __constant_cpu_to_le16(8); /* SPEED_CHANGE data is up/down speeds in bits/sec */ - data [0] = data [1] = cpu_to_le32 (BITRATE (dev->gadget)); + data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget)); req->length = STATUS_BYTECOUNT; - value = usb_ep_queue (ep, req, GFP_ATOMIC); + value = usb_ep_queue(ep, req, GFP_ATOMIC); debug("send SPEED_CHANGE --> %d\n", value); if (value == 0) return; } else if (value != -ECONNRESET) { debug("event %02x --> %d\n", event->bNotificationType, value); - if (event->bNotificationType== - USB_CDC_NOTIFY_SPEED_CHANGE) - { - l_ethdev.network_started=1; + if (event->bNotificationType == + USB_CDC_NOTIFY_SPEED_CHANGE) { + l_ethdev.network_started = 1; printf("USB network up!\n"); } } req->context = NULL; } -static void issue_start_status (struct eth_dev *dev) +static void issue_start_status(struct eth_dev *dev) { struct usb_request *req = dev->stat_req; struct usb_cdc_notification *event; int value; - /* flush old status + /* + * flush old status * * FIXME ugly idiom, maybe we'd be better with just * a "cancel the whole queue" primitive since any @@ -967,24 +976,25 @@ static void issue_start_status (struct eth_dev *dev) * * FIXME iff req->context != null just dequeue it */ - usb_ep_disable (dev->status_ep); - usb_ep_enable (dev->status_ep, dev->status); + usb_ep_disable(dev->status_ep); + usb_ep_enable(dev->status_ep, dev->status); - /* 3.8.1 says to issue first NETWORK_CONNECTION, then + /* + * 3.8.1 says to issue first NETWORK_CONNECTION, then * a SPEED_CHANGE. could be useful in some configs. */ event = req->buf; event->bmRequestType = 0xA1; event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION; - event->wValue = __constant_cpu_to_le16 (1); /* connected */ - event->wIndex = __constant_cpu_to_le16 (1); + event->wValue = __constant_cpu_to_le16(1); /* connected */ + event->wIndex = __constant_cpu_to_le16(1); event->wLength = 0; req->length = sizeof *event; req->complete = eth_status_complete; req->context = dev; - value = usb_ep_queue (dev->status_ep, req, GFP_ATOMIC); + value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC); if (value < 0) debug("status buf queue --> %d\n", value); } @@ -993,7 +1003,7 @@ static void issue_start_status (struct eth_dev *dev) /*-------------------------------------------------------------------------*/ -static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req) +static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req) { if (req->status || req->actual != req->length) debug("setup complete --> %d, %d/%d\n", @@ -1010,16 +1020,17 @@ static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req) * - class-specific control requests */ static int -eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) +eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) { - struct eth_dev *dev = get_gadget_data (gadget); + struct eth_dev *dev = get_gadget_data(gadget); struct usb_request *req = dev->req; int value = -EOPNOTSUPP; u16 wIndex = le16_to_cpu(ctrl->wIndex); u16 wValue = le16_to_cpu(ctrl->wValue); u16 wLength = le16_to_cpu(ctrl->wLength); - /* descriptors just go into the pre-allocated ep0 buffer, + /* + * descriptors just go into the pre-allocated ep0 buffer, * while config change events may enable network traffic. */ @@ -1034,14 +1045,14 @@ eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) switch (wValue >> 8) { case USB_DT_DEVICE: - value = min (wLength, (u16) sizeof device_desc); - memcpy (req->buf, &device_desc, value); + value = min(wLength, (u16) sizeof device_desc); + memcpy(req->buf, &device_desc, value); break; case USB_DT_DEVICE_QUALIFIER: if (!gadget_is_dualspeed(gadget)) break; - value = min (wLength, (u16) sizeof dev_qualifier); - memcpy (req->buf, &dev_qualifier, value); + value = min(wLength, (u16) sizeof dev_qualifier); + memcpy(req->buf, &dev_qualifier, value); break; case USB_DT_OTHER_SPEED_CONFIG: @@ -1054,15 +1065,15 @@ eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) wValue & 0xff, gadget_is_otg(gadget)); if (value >= 0) - value = min (wLength, (u16) value); + value = min(wLength, (u16) value); break; case USB_DT_STRING: - value = usb_gadget_get_string (&stringtab, + value = usb_gadget_get_string(&stringtab, wValue & 0xff, req->buf); if (value >= 0) - value = min (wLength, (u16) value); + value = min(wLength, (u16) value); break; } @@ -1075,13 +1086,13 @@ eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) debug("HNP available\n"); else if (gadget->a_alt_hnp_support) debug("HNP needs a different root port\n"); - value = eth_set_config (dev, wValue, GFP_ATOMIC); + value = eth_set_config(dev, wValue, GFP_ATOMIC); break; case USB_REQ_GET_CONFIGURATION: if (ctrl->bRequestType != USB_DIR_IN) break; *(u8 *)req->buf = dev->config; - value = min (wLength, (u16) 1); + value = min(wLength, (u16) 1); break; case USB_REQ_SET_INTERFACE: @@ -1092,11 +1103,12 @@ eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) if (!cdc_active(dev) && wIndex != 0) break; - /* PXA hardware partially handles SET_INTERFACE; + /* + * PXA hardware partially handles SET_INTERFACE; * we need to kluge around that interference. */ - if (gadget_is_pxa (gadget)) { - value = eth_set_config (dev, DEV_CONFIG_VALUE, + if (gadget_is_pxa(gadget)) { + value = eth_set_config(dev, DEV_CONFIG_VALUE, GFP_ATOMIC); goto done_set_intf; } @@ -1107,36 +1119,38 @@ eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) if (wValue != 0) break; if (dev->status) { - usb_ep_disable (dev->status_ep); - usb_ep_enable (dev->status_ep, dev->status); + usb_ep_disable(dev->status_ep); + usb_ep_enable(dev->status_ep, dev->status); } value = 0; break; case 1: /* data intf */ if (wValue > 1) break; - usb_ep_disable (dev->in_ep); - usb_ep_disable (dev->out_ep); + usb_ep_disable(dev->in_ep); + usb_ep_disable(dev->out_ep); - /* CDC requires the data transfers not be done from + /* + * CDC requires the data transfers not be done from * the default interface setting ... also, setting * the non-default interface resets filters etc. */ if (wValue == 1) { - if (!cdc_active (dev)) + if (!cdc_active(dev)) break; - usb_ep_enable (dev->in_ep, dev->in); - usb_ep_enable (dev->out_ep, dev->out); + usb_ep_enable(dev->in_ep, dev->in); + usb_ep_enable(dev->out_ep, dev->out); dev->cdc_filter = DEFAULT_FILTER; if (dev->status) - issue_start_status (dev); + issue_start_status(dev); } value = 0; break; } #else - /* FIXME this is wrong, as is the assumption that + /* + * FIXME this is wrong, as is the assumption that * all non-PXA hardware talks real CDC ... */ debug("set_interface ignored!\n"); @@ -1160,12 +1174,13 @@ done_set_intf: /* carrier always ok ...*/ *(u8 *)req->buf = 1 ; } - value = min (wLength, (u16) 1); + value = min(wLength, (u16) 1); break; #ifdef DEV_CONFIG_CDC case USB_CDC_SET_ETHERNET_PACKET_FILTER: - /* see 6.2.30: no data, wIndex = interface, + /* + * see 6.2.30: no data, wIndex = interface, * wValue = packet filter bitmap */ if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE) @@ -1178,7 +1193,8 @@ done_set_intf: value = 0; break; - /* and potentially: + /* + * and potentially: * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS: * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER: * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER: @@ -1199,11 +1215,11 @@ done_set_intf: req->length = value; req->zero = value < wLength && (value % gadget->ep0->maxpacket) == 0; - value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC); + value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC); if (value < 0) { debug("ep_queue --> %d\n", value); req->status = 0; - eth_setup_complete (gadget->ep0, req); + eth_setup_complete(gadget->ep0, req); } } @@ -1211,18 +1227,18 @@ done_set_intf: return value; } - /*-------------------------------------------------------------------------*/ -static void rx_complete (struct usb_ep *ep, struct usb_request *req); +static void rx_complete(struct usb_ep *ep, struct usb_request *req); -static int rx_submit ( struct eth_dev *dev, struct usb_request *req, \ +static int rx_submit(struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags) { int retval = -ENOMEM; size_t size; - /* Padding up to RX_EXTRA handles minor disagreements with host. + /* + * Padding up to RX_EXTRA handles minor disagreements with host. * Normally we use the USB "terminate on short read" convention; * so allow up to (N*maxpacket), since that memory is normally * already allocated. Some hardware doesn't deal well with short @@ -1236,8 +1252,8 @@ static int rx_submit ( struct eth_dev *dev, struct usb_request *req, \ size += dev->out_ep->maxpacket - 1; size -= size % dev->out_ep->maxpacket; - - /* Some platforms perform better when IP packets are aligned, + /* + * Some platforms perform better when IP packets are aligned, * but on at least one, checksumming fails otherwise. */ @@ -1245,37 +1261,35 @@ static int rx_submit ( struct eth_dev *dev, struct usb_request *req, \ req->length = size; req->complete = rx_complete; - retval = usb_ep_queue (dev->out_ep, req, gfp_flags); + retval = usb_ep_queue(dev->out_ep, req, gfp_flags); - if (retval) { + if (retval) error("rx submit --> %d", retval); - } + return retval; } - -static void rx_complete (struct usb_ep *ep, struct usb_request *req) +static void rx_complete(struct usb_ep *ep, struct usb_request *req) { struct eth_dev *dev = ep->driver_data; debug("%s: status %d\n", __func__, req->status); - packet_received=1; + packet_received = 1; if (req) - dev->rx_req=req; + dev->rx_req = req; } - -static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags) +static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags) { - dev->tx_req = usb_ep_alloc_request (dev->in_ep, 0); + dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0); if (!dev->tx_req) goto fail; - dev->rx_req = usb_ep_alloc_request (dev->out_ep, 0); + dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0); if (!dev->rx_req) goto fail; @@ -1287,17 +1301,16 @@ fail: return -1; } - -static void tx_complete (struct usb_ep *ep, struct usb_request *req) +static void tx_complete(struct usb_ep *ep, struct usb_request *req) { - debug("%s: status %s\n", __func__, (req->status)?"failed":"ok"); - packet_sent=1; + debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok"); + packet_sent = 1; } -static inline int eth_is_promisc (struct eth_dev *dev) +static inline int eth_is_promisc(struct eth_dev *dev) { /* no filters for the CDC subset; always promisc */ - if (subset_active (dev)) + if (subset_active(dev)) return 1; return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS; } @@ -1415,49 +1428,49 @@ drop: /*-------------------------------------------------------------------------*/ #endif -static void eth_unbind (struct usb_gadget *gadget) +static void eth_unbind(struct usb_gadget *gadget) { - struct eth_dev *dev = get_gadget_data (gadget); + struct eth_dev *dev = get_gadget_data(gadget); debug("%s...\n", __func__); /* we've already been disconnected ... no i/o is active */ if (dev->req) { - usb_ep_free_request (gadget->ep0, dev->req); + usb_ep_free_request(gadget->ep0, dev->req); dev->req = NULL; } if (dev->stat_req) { - usb_ep_free_request (dev->status_ep, dev->stat_req); + usb_ep_free_request(dev->status_ep, dev->stat_req); dev->stat_req = NULL; } if (dev->tx_req) { - usb_ep_free_request (dev->in_ep, dev->tx_req); - dev->tx_req=NULL; + usb_ep_free_request(dev->in_ep, dev->tx_req); + dev->tx_req = NULL; } if (dev->rx_req) { - usb_ep_free_request (dev->out_ep, dev->rx_req); - dev->rx_req=NULL; + usb_ep_free_request(dev->out_ep, dev->rx_req); + dev->rx_req = NULL; } /* unregister_netdev (dev->net);*/ /* free_netdev(dev->net);*/ - set_gadget_data (gadget, NULL); + set_gadget_data(gadget, NULL); } -static void eth_disconnect (struct usb_gadget *gadget) +static void eth_disconnect(struct usb_gadget *gadget) { - eth_reset_config (get_gadget_data (gadget)); + eth_reset_config(get_gadget_data(gadget)); } -static void eth_suspend (struct usb_gadget *gadget) +static void eth_suspend(struct usb_gadget *gadget) { /* Not used */ } -static void eth_resume (struct usb_gadget *gadget) +static void eth_resume(struct usb_gadget *gadget) { /* Not used */ } @@ -1492,12 +1505,12 @@ static int is_eth_addr_valid(char *str) return 0; } -static u8 nibble (unsigned char c) +static u8 nibble(unsigned char c) { - if (likely (isdigit (c))) + if (likely(isdigit(c))) return c - '0'; - c = toupper (c); - if (likely (isxdigit (c))) + c = toupper(c); + if (likely(isxdigit(c))) return 10 + c - 'A'; return 0; } @@ -1510,13 +1523,13 @@ static int get_ether_addr(const char *str, u8 *dev_addr) for (i = 0; i < 6; i++) { unsigned char num; - if((*str == '.') || (*str == ':')) + if ((*str == '.') || (*str == ':')) str++; num = nibble(*str++) << 4; num |= (nibble(*str++)); - dev_addr [i] = num; + dev_addr[i] = num; } - if (is_valid_ether_addr (dev_addr)) + if (is_valid_ether_addr(dev_addr)) return 0; } return 1; @@ -1528,17 +1541,18 @@ static int eth_bind(struct usb_gadget *gadget) u8 cdc = 1, zlp = 1; struct usb_ep *in_ep, *out_ep, *status_ep = NULL; int gcnum; - u8 tmp[7]; + u8 tmp[7]; /* these flags are only ever cleared; compiler take note */ #ifndef DEV_CONFIG_CDC cdc = 0; #endif - /* Because most host side USB stacks handle CDC Ethernet, that + /* + * Because most host side USB stacks handle CDC Ethernet, that * standard protocol is _strongly_ preferred for interop purposes. * (By everyone except Microsoft.) */ - if (gadget_is_pxa (gadget)) { + if (gadget_is_pxa(gadget)) { /* pxa doesn't support altsettings */ cdc = 0; } else if (gadget_is_musbhdrc(gadget)) { @@ -1547,20 +1561,22 @@ static int eth_bind(struct usb_gadget *gadget) } else if (gadget_is_sh(gadget)) { /* sh doesn't support multiple interfaces or configs */ cdc = 0; - } else if (gadget_is_sa1100 (gadget)) { + } else if (gadget_is_sa1100(gadget)) { /* hardware can't write zlps */ zlp = 0; - /* sa1100 CAN do CDC, without status endpoint ... we use + /* + * sa1100 CAN do CDC, without status endpoint ... we use * non-CDC to be compatible with ARM Linux-2.4 "usb-eth". */ cdc = 0; } - gcnum = usb_gadget_controller_number (gadget); + gcnum = usb_gadget_controller_number(gadget); if (gcnum >= 0) - device_desc.bcdDevice = cpu_to_le16 (0x0300 + gcnum); + device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum); else { - /* can't assume CDC works. don't want to default to + /* + * can't assume CDC works. don't want to default to * anything less functional on CDC-capable hardware, * so we fail in this case. */ @@ -1569,7 +1585,8 @@ static int eth_bind(struct usb_gadget *gadget) return -ENODEV; } - /* CDC subset ... recognized by Linux since 2.4.10, but Windows + /* + * CDC subset ... recognized by Linux since 2.4.10, but Windows * drivers aren't widely available. (That may be improved by * supporting one submode of the "SAFE" variant of MDLM.) */ @@ -1588,17 +1605,17 @@ static int eth_bind(struct usb_gadget *gadget) if (bcdDevice) device_desc.bcdDevice = cpu_to_le16(bcdDevice); if (iManufacturer) - strlcpy (manufacturer, iManufacturer, sizeof manufacturer); + strlcpy(manufacturer, iManufacturer, sizeof manufacturer); if (iProduct) - strlcpy (product_desc, iProduct, sizeof product_desc); + strlcpy(product_desc, iProduct, sizeof product_desc); if (iSerialNumber) { device_desc.iSerialNumber = STRING_SERIALNUMBER, strlcpy(serial_number, iSerialNumber, sizeof serial_number); } /* all we really need is bulk IN/OUT */ - usb_ep_autoconfig_reset (gadget); - in_ep = usb_ep_autoconfig (gadget, &fs_source_desc); + usb_ep_autoconfig_reset(gadget); + in_ep = usb_ep_autoconfig(gadget, &fs_source_desc); if (!in_ep) { autoconf_fail: error("can't autoconfigure on %s\n", @@ -1607,17 +1624,18 @@ autoconf_fail: } in_ep->driver_data = in_ep; /* claim */ - out_ep = usb_ep_autoconfig (gadget, &fs_sink_desc); + out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc); if (!out_ep) goto autoconf_fail; out_ep->driver_data = out_ep; /* claim */ #if defined(DEV_CONFIG_CDC) - /* CDC Ethernet control interface doesn't require a status endpoint. + /* + * CDC Ethernet control interface doesn't require a status endpoint. * Since some hosts expect one, try to allocate one anyway. */ if (cdc) { - status_ep = usb_ep_autoconfig (gadget, &fs_status_desc); + status_ep = usb_ep_autoconfig(gadget, &fs_status_desc); if (status_ep) { status_ep->driver_data = status_ep; /* claim */ } else if (cdc) { @@ -1632,7 +1650,8 @@ autoconf_fail: eth_config.bNumInterfaces = 1; eth_config.iConfiguration = STRING_SUBSET; - /* use functions to set these up, in case we're built to work + /* + * use functions to set these up, in case we're built to work * with multiple controllers and must override CDC Ethernet. */ fs_subset_descriptors(); @@ -1640,7 +1659,7 @@ autoconf_fail: } device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket; - usb_gadget_set_selfpowered (gadget); + usb_gadget_set_selfpowered(gadget); if (gadget_is_dualspeed(gadget)) { if (!cdc) @@ -1668,7 +1687,7 @@ autoconf_fail: } dev->net = &l_netdev; - strcpy (dev->net->name, USB_NET_NAME); + strcpy(dev->net->name, USB_NET_NAME); dev->cdc = cdc; dev->zlp = zlp; @@ -1677,7 +1696,8 @@ autoconf_fail: dev->out_ep = out_ep; dev->status_ep = status_ep; - /* Module params for these addresses should come from ID proms. + /* + * Module params for these addresses should come from ID proms. * The host side address is used with CDC, and commonly * ends up in a persistent config database. It's not clear if * host side code for the SAFE thing cares -- its original BLAN @@ -1690,10 +1710,10 @@ autoconf_fail: get_ether_addr(host_addr, dev->host_mac); - sprintf (ethaddr, "%02X%02X%02X%02X%02X%02X", - dev->host_mac [0], dev->host_mac [1], - dev->host_mac [2], dev->host_mac [3], - dev->host_mac [4], dev->host_mac [5]); + sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X", + dev->host_mac[0], dev->host_mac[1], + dev->host_mac[2], dev->host_mac[3], + dev->host_mac[4], dev->host_mac[5]); printf("using %s, OUT %s IN %s%s%s\n", gadget->name, out_ep->name, in_ep->name, @@ -1701,23 +1721,25 @@ autoconf_fail: status_ep ? status_ep->name : "" ); printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n", - dev->net->enetaddr [0], dev->net->enetaddr [1], - dev->net->enetaddr [2], dev->net->enetaddr [3], - dev->net->enetaddr [4], dev->net->enetaddr [5]); + dev->net->enetaddr[0], dev->net->enetaddr[1], + dev->net->enetaddr[2], dev->net->enetaddr[3], + dev->net->enetaddr[4], dev->net->enetaddr[5]); if (cdc) { printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n", - dev->host_mac [0], dev->host_mac [1], - dev->host_mac [2], dev->host_mac [3], - dev->host_mac [4], dev->host_mac [5]); + dev->host_mac[0], dev->host_mac[1], + dev->host_mac[2], dev->host_mac[3], + dev->host_mac[4], dev->host_mac[5]); } - /* use PKTSIZE (or aligned... from u-boot) and set - * wMaxSegmentSize accordingly*/ + /* + * use PKTSIZE (or aligned... from u-boot) and set + * wMaxSegmentSize accordingly + */ dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/ /* preallocate control message data and buffer */ - dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL); + dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL); if (!dev->req) goto fail; dev->req->buf = control_req; @@ -1726,9 +1748,10 @@ autoconf_fail: /* ... and maybe likewise for status transfer */ #if defined(DEV_CONFIG_CDC) if (dev->status_ep) { - dev->stat_req = usb_ep_alloc_request(dev->status_ep, GFP_KERNEL); + dev->stat_req = usb_ep_alloc_request(dev->status_ep, + GFP_KERNEL); if (!dev->stat_req) { - usb_ep_free_request (dev->status_ep, dev->req); + usb_ep_free_request(dev->status_ep, dev->req); goto fail; } @@ -1739,10 +1762,11 @@ autoconf_fail: /* finish hookup to lower layer ... */ dev->gadget = gadget; - set_gadget_data (gadget, dev); + set_gadget_data(gadget, dev); gadget->ep0->driver_data = dev; - /* two kinds of host-initiated state changes: + /* + * two kinds of host-initiated state changes: * - iff DATA transfer is active, carrier is "on" * - tx queueing enabled if open *and* carrier is "on" */ @@ -1750,13 +1774,13 @@ autoconf_fail: fail: error("%s failed", __func__); - eth_unbind (gadget); + eth_unbind(gadget); return -ENOMEM; } -static int usb_eth_init(struct eth_device* netdev, bd_t* bd) +static int usb_eth_init(struct eth_device *netdev, bd_t *bd) { - struct eth_dev *dev=&l_ethdev; + struct eth_dev *dev = &l_ethdev; struct usb_gadget *gadget; unsigned long ts; unsigned long timeout = USB_CONNECT_TIMEOUT; @@ -1780,8 +1804,7 @@ static int usb_eth_init(struct eth_device* netdev, bd_t* bd) timeout = simple_strtoul(getenv("cdc_connect_timeout"), NULL, 10) * CONFIG_SYS_HZ; ts = get_timer(0); - while (!l_ethdev.network_started) - { + while (!l_ethdev.network_started) { /* Handle control-c and timeouts */ if (ctrlc() || (get_timer(ts) > timeout)) { error("The remote end did not respond in time."); @@ -1790,19 +1813,20 @@ static int usb_eth_init(struct eth_device* netdev, bd_t* bd) usb_gadget_handle_interrupts(); } - rx_submit (dev, dev->rx_req, 0); + rx_submit(dev, dev->rx_req, 0); return 0; fail: return -1; } -static int usb_eth_send(struct eth_device* netdev, volatile void* packet, int length) +static int usb_eth_send(struct eth_device *netdev, + volatile void *packet, int length) { int retval; struct usb_request *req = NULL; struct eth_dev *dev = &l_ethdev; - unsigned long ts; - unsigned long timeout = USB_CONNECT_TIMEOUT; + unsigned long ts; + unsigned long timeout = USB_CONNECT_TIMEOUT; debug("%s:...\n", __func__); @@ -1812,7 +1836,8 @@ static int usb_eth_send(struct eth_device* netdev, volatile void* packet, int le req->context = NULL; req->complete = tx_complete; - /* use zlp framing on tx for strict CDC-Ether conformance, + /* + * use zlp framing on tx for strict CDC-Ether conformance, * though any robust network rx path ignores extra padding. * and some hardware doesn't like to write zlps. */ @@ -1827,54 +1852,50 @@ static int usb_eth_send(struct eth_device* netdev, volatile void* packet, int le req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH) ? ((dev->tx_qlen % qmult) != 0) : 0; #endif - dev->tx_qlen=1; - ts = get_timer(0); - packet_sent = 0; + dev->tx_qlen = 1; + ts = get_timer(0); + packet_sent = 0; - retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC); + retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC); if (!retval) debug("%s: packet queued\n", __func__); - while(!packet_sent) - { - if (get_timer(ts) > timeout) { - printf("timeout sending packets to usb ethernet\n"); - return -1; - } - usb_gadget_handle_interrupts(); + while (!packet_sent) { + if (get_timer(ts) > timeout) { + printf("timeout sending packets to usb ethernet\n"); + return -1; + } + usb_gadget_handle_interrupts(); } return 0; } -static int usb_eth_recv(struct eth_device* netdev) +static int usb_eth_recv(struct eth_device *netdev) { struct eth_dev *dev = &l_ethdev; usb_gadget_handle_interrupts(); - if (packet_received) - { - debug("%s: packet received \n", __func__); - if (dev->rx_req) - { - NetReceive(NetRxPackets[0],dev->rx_req->length); - packet_received=0; + if (packet_received) { + debug("%s: packet received\n", __func__); + if (dev->rx_req) { + NetReceive(NetRxPackets[0], dev->rx_req->length); + packet_received = 0; if (dev->rx_req) - rx_submit (dev, dev->rx_req, 0); - } - else error("dev->rx_req invalid"); + rx_submit(dev, dev->rx_req, 0); + } else + error("dev->rx_req invalid"); } return 0; } -void usb_eth_halt(struct eth_device* netdev) +void usb_eth_halt(struct eth_device *netdev) { - struct eth_dev *dev =&l_ethdev; + struct eth_dev *dev = &l_ethdev; - if (!netdev) - { + if (!netdev) { error("received NULL ptr"); return; } @@ -1898,9 +1919,9 @@ static struct usb_gadget_driver eth_driver = { int usb_eth_initialize(bd_t *bi) { int status = 0; - struct eth_device *netdev=&l_netdev; + struct eth_device *netdev = &l_netdev; - sprintf(netdev->name,"usb_ether"); + sprintf(netdev->name, "usb_ether"); netdev->init = usb_eth_init; netdev->send = usb_eth_send; diff --git a/drivers/usb/gadget/gadget_chips.h b/drivers/usb/gadget/gadget_chips.h index 480bc87..9bb7e2e 100644 --- a/drivers/usb/gadget/gadget_chips.h +++ b/drivers/usb/gadget/gadget_chips.h @@ -15,109 +15,109 @@ * Remy Bohmer */ #ifdef CONFIG_USB_GADGET_NET2280 -#define gadget_is_net2280(g) !strcmp("net2280", (g)->name) +#define gadget_is_net2280(g) (!strcmp("net2280", (g)->name)) #else #define gadget_is_net2280(g) 0 #endif #ifdef CONFIG_USB_GADGET_AMD5536UDC -#define gadget_is_amd5536udc(g) !strcmp("amd5536udc", (g)->name) +#define gadget_is_amd5536udc(g) (!strcmp("amd5536udc", (g)->name)) #else #define gadget_is_amd5536udc(g) 0 #endif #ifdef CONFIG_USB_GADGET_DUMMY_HCD -#define gadget_is_dummy(g) !strcmp("dummy_udc", (g)->name) +#define gadget_is_dummy(g) (!strcmp("dummy_udc", (g)->name)) #else #define gadget_is_dummy(g) 0 #endif #ifdef CONFIG_USB_GADGET_PXA2XX -#define gadget_is_pxa(g) !strcmp("pxa2xx_udc", (g)->name) +#define gadget_is_pxa(g) (!strcmp("pxa2xx_udc", (g)->name)) #else #define gadget_is_pxa(g) 0 #endif #ifdef CONFIG_USB_GADGET_GOKU -#define gadget_is_goku(g) !strcmp("goku_udc", (g)->name) +#define gadget_is_goku(g) (!strcmp("goku_udc", (g)->name)) #else #define gadget_is_goku(g) 0 #endif /* SH3 UDC -- not yet ported 2.4 --> 2.6 */ #ifdef CONFIG_USB_GADGET_SUPERH -#define gadget_is_sh(g) !strcmp("sh_udc", (g)->name) +#define gadget_is_sh(g) (!strcmp("sh_udc", (g)->name)) #else #define gadget_is_sh(g) 0 #endif /* not yet stable on 2.6 (would help "original Zaurus") */ #ifdef CONFIG_USB_GADGET_SA1100 -#define gadget_is_sa1100(g) !strcmp("sa1100_udc", (g)->name) +#define gadget_is_sa1100(g) (!strcmp("sa1100_udc", (g)->name)) #else #define gadget_is_sa1100(g) 0 #endif #ifdef CONFIG_USB_GADGET_LH7A40X -#define gadget_is_lh7a40x(g) !strcmp("lh7a40x_udc", (g)->name) +#define gadget_is_lh7a40x(g) (!strcmp("lh7a40x_udc", (g)->name)) #else #define gadget_is_lh7a40x(g) 0 #endif /* handhelds.org tree (?) */ #ifdef CONFIG_USB_GADGET_MQ11XX -#define gadget_is_mq11xx(g) !strcmp("mq11xx_udc", (g)->name) +#define gadget_is_mq11xx(g) (!strcmp("mq11xx_udc", (g)->name)) #else #define gadget_is_mq11xx(g) 0 #endif #ifdef CONFIG_USB_GADGET_OMAP -#define gadget_is_omap(g) !strcmp("omap_udc", (g)->name) +#define gadget_is_omap(g) (!strcmp("omap_udc", (g)->name)) #else #define gadget_is_omap(g) 0 #endif /* not yet ported 2.4 --> 2.6 */ #ifdef CONFIG_USB_GADGET_N9604 -#define gadget_is_n9604(g) !strcmp("n9604_udc", (g)->name) +#define gadget_is_n9604(g) (!strcmp("n9604_udc", (g)->name)) #else #define gadget_is_n9604(g) 0 #endif /* various unstable versions available */ #ifdef CONFIG_USB_GADGET_PXA27X -#define gadget_is_pxa27x(g) !strcmp("pxa27x_udc", (g)->name) +#define gadget_is_pxa27x(g) (!strcmp("pxa27x_udc", (g)->name)) #else #define gadget_is_pxa27x(g) 0 #endif #ifdef CONFIG_USB_GADGET_ATMEL_USBA -#define gadget_is_atmel_usba(g) !strcmp("atmel_usba_udc", (g)->name) +#define gadget_is_atmel_usba(g) (!strcmp("atmel_usba_udc", (g)->name)) #else #define gadget_is_atmel_usba(g) 0 #endif #ifdef CONFIG_USB_GADGET_S3C2410 -#define gadget_is_s3c2410(g) !strcmp("s3c2410_udc", (g)->name) +#define gadget_is_s3c2410(g) (!strcmp("s3c2410_udc", (g)->name)) #else #define gadget_is_s3c2410(g) 0 #endif #ifdef CONFIG_USB_GADGET_AT91 -#define gadget_is_at91(g) !strcmp("at91_udc", (g)->name) +#define gadget_is_at91(g) (!strcmp("at91_udc", (g)->name)) #else #define gadget_is_at91(g) 0 #endif /* status unclear */ #ifdef CONFIG_USB_GADGET_IMX -#define gadget_is_imx(g) !strcmp("imx_udc", (g)->name) +#define gadget_is_imx(g) (!strcmp("imx_udc", (g)->name)) #else #define gadget_is_imx(g) 0 #endif #ifdef CONFIG_USB_GADGET_FSL_USB2 -#define gadget_is_fsl_usb2(g) !strcmp("fsl-usb2-udc", (g)->name) +#define gadget_is_fsl_usb2(g) (!strcmp("fsl-usb2-udc", (g)->name)) #else #define gadget_is_fsl_usb2(g) 0 #endif @@ -125,36 +125,37 @@ /* Mentor high speed function controller */ /* from Montavista kernel (?) */ #ifdef CONFIG_USB_GADGET_MUSBHSFC -#define gadget_is_musbhsfc(g) !strcmp("musbhsfc_udc", (g)->name) +#define gadget_is_musbhsfc(g) (!strcmp("musbhsfc_udc", (g)->name)) #else #define gadget_is_musbhsfc(g) 0 #endif /* Mentor high speed "dual role" controller, in peripheral role */ #ifdef CONFIG_USB_GADGET_MUSB_HDRC -#define gadget_is_musbhdrc(g) !strcmp("musb_hdrc", (g)->name) +#define gadget_is_musbhdrc(g) (!strcmp("musb_hdrc", (g)->name)) #else #define gadget_is_musbhdrc(g) 0 #endif /* from Montavista kernel (?) */ #ifdef CONFIG_USB_GADGET_MPC8272 -#define gadget_is_mpc8272(g) !strcmp("mpc8272_udc", (g)->name) +#define gadget_is_mpc8272(g) (!strcmp("mpc8272_udc", (g)->name)) #else #define gadget_is_mpc8272(g) 0 #endif #ifdef CONFIG_USB_GADGET_M66592 -#define gadget_is_m66592(g) !strcmp("m66592_udc", (g)->name) +#define gadget_is_m66592(g) (!strcmp("m66592_udc", (g)->name)) #else #define gadget_is_m66592(g) 0 #endif -// CONFIG_USB_GADGET_SX2 -// CONFIG_USB_GADGET_AU1X00 -// ... - +/* + * CONFIG_USB_GADGET_SX2 + * CONFIG_USB_GADGET_AU1X00 + * ... + */ /** * usb_gadget_controller_number - support bcdDevice id convention diff --git a/drivers/usb/gadget/usbstring.c b/drivers/usb/gadget/usbstring.c index 168f75f..6e9d1bf 100644 --- a/drivers/usb/gadget/usbstring.c +++ b/drivers/usb/gadget/usbstring.c @@ -24,14 +24,17 @@ static int utf8_to_utf16le(const char *s, __le16 *cp, unsigned len) u8 c; u16 uchar; - /* this insists on correct encodings, though not minimal ones. + /* + * this insists on correct encodings, though not minimal ones. * BUT it currently rejects legit 4-byte UTF-8 code points, * which need surrogate pairs. (Unicode 3.1 can use them.) */ while (len != 0 && (c = (u8) *s++) != 0) { if ((c & 0x80)) { - // 2-byte sequence: - // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx + /* + * 2-byte sequence: + * 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx + */ if ((c & 0xe0) == 0xc0) { uchar = (c & 0x1f) << 6; @@ -41,8 +44,10 @@ static int utf8_to_utf16le(const char *s, __le16 *cp, unsigned len) c &= 0x3f; uchar |= c; - // 3-byte sequence (most CJKV characters): - // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx + /* + * 3-byte sequence (most CJKV characters): + * zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx + */ } else if ((c & 0xf0) == 0xe0) { uchar = (c & 0x0f) << 12; @@ -62,12 +67,13 @@ static int utf8_to_utf16le(const char *s, __le16 *cp, unsigned len) if (0xd800 <= uchar && uchar <= 0xdfff) goto fail; - // 4-byte sequence (surrogate pairs, currently rare): - // 11101110wwwwzzzzyy + 110111yyyyxxxxxx - // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx - // (uuuuu = wwww + 1) - // FIXME accept the surrogate code points (only) - + /* + * 4-byte sequence (surrogate pairs, currently rare): + * 11101110wwwwzzzzyy + 110111yyyyxxxxxx + * = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx + * (uuuuu = wwww + 1) + * FIXME accept the surrogate code points (only) + */ } else goto fail; } else @@ -100,17 +106,17 @@ fail: * characters (which are also widely used in C strings). */ int -usb_gadget_get_string (struct usb_gadget_strings *table, int id, u8 *buf) +usb_gadget_get_string(struct usb_gadget_strings *table, int id, u8 *buf) { struct usb_string *s; int len; /* descriptor 0 has the language id */ if (id == 0) { - buf [0] = 4; - buf [1] = USB_DT_STRING; - buf [2] = (u8) table->language; - buf [3] = (u8) (table->language >> 8); + buf[0] = 4; + buf[1] = USB_DT_STRING; + buf[2] = (u8) table->language; + buf[3] = (u8) (table->language >> 8); return 4; } for (s = table->strings; s && s->s; s++) @@ -122,13 +128,13 @@ usb_gadget_get_string (struct usb_gadget_strings *table, int id, u8 *buf) return -EINVAL; /* string descriptors have length, tag, then UTF16-LE text */ - len = min ((size_t) 126, strlen (s->s)); - memset (buf + 2, 0, 2 * len); /* zero all the bytes */ + len = min((size_t) 126, strlen(s->s)); + memset(buf + 2, 0, 2 * len); /* zero all the bytes */ len = utf8_to_utf16le(s->s, (__le16 *)&buf[2], len); if (len < 0) return -EINVAL; - buf [0] = (len + 1) * 2; - buf [1] = USB_DT_STRING; - return buf [0]; + buf[0] = (len + 1) * 2; + buf[1] = USB_DT_STRING; + return buf[0]; } -- cgit v1.1