123cd1385SRemy Bohmer /* 223cd1385SRemy Bohmer * ether.c -- Ethernet gadget driver, with CDC and non-CDC options 323cd1385SRemy Bohmer * 423cd1385SRemy Bohmer * Copyright (C) 2003-2005,2008 David Brownell 523cd1385SRemy Bohmer * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger 623cd1385SRemy Bohmer * Copyright (C) 2008 Nokia Corporation 723cd1385SRemy Bohmer * 823cd1385SRemy Bohmer * This program is free software; you can redistribute it and/or modify 923cd1385SRemy Bohmer * it under the terms of the GNU General Public License as published by 1023cd1385SRemy Bohmer * the Free Software Foundation; either version 2 of the License, or 1123cd1385SRemy Bohmer * (at your option) any later version. 1223cd1385SRemy Bohmer * 1323cd1385SRemy Bohmer * This program is distributed in the hope that it will be useful, 1423cd1385SRemy Bohmer * but WITHOUT ANY WARRANTY; without even the implied warranty of 1523cd1385SRemy Bohmer * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1623cd1385SRemy Bohmer * GNU General Public License for more details. 1723cd1385SRemy Bohmer * 1823cd1385SRemy Bohmer * You should have received a copy of the GNU General Public License 1923cd1385SRemy Bohmer * along with this program; if not, write to the Free Software 2023cd1385SRemy Bohmer * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 2123cd1385SRemy Bohmer */ 2223cd1385SRemy Bohmer 2323cd1385SRemy Bohmer #include <common.h> 2423cd1385SRemy Bohmer #include <asm/errno.h> 25*c85d70efSVitaly Kuzmichev #include <linux/netdevice.h> 2623cd1385SRemy Bohmer #include <linux/usb/ch9.h> 2723cd1385SRemy Bohmer #include <linux/usb/cdc.h> 2823cd1385SRemy Bohmer #include <linux/usb/gadget.h> 2923cd1385SRemy Bohmer #include <net.h> 3023cd1385SRemy Bohmer #include <linux/ctype.h> 3123cd1385SRemy Bohmer 3223cd1385SRemy Bohmer #include "gadget_chips.h" 3323cd1385SRemy Bohmer 348f7aa831SVitaly Kuzmichev #define USB_NET_NAME "usb_ether" 357de73185SVitaly Kuzmichev 3623cd1385SRemy Bohmer #define atomic_read 3723cd1385SRemy Bohmer extern struct platform_data brd; 3823cd1385SRemy Bohmer #define spin_lock(x) 3923cd1385SRemy Bohmer #define spin_unlock(x) 4023cd1385SRemy Bohmer 4123cd1385SRemy Bohmer 4223cd1385SRemy Bohmer unsigned packet_received, packet_sent; 4323cd1385SRemy Bohmer 4423cd1385SRemy Bohmer #define DEV_CONFIG_CDC 1 4523cd1385SRemy Bohmer #define GFP_ATOMIC ((gfp_t) 0) 4623cd1385SRemy Bohmer #define GFP_KERNEL ((gfp_t) 0) 4723cd1385SRemy Bohmer 4823cd1385SRemy Bohmer /* 4923cd1385SRemy Bohmer * Ethernet gadget driver -- with CDC and non-CDC options 5023cd1385SRemy Bohmer * Builds on hardware support for a full duplex link. 5123cd1385SRemy Bohmer * 5223cd1385SRemy Bohmer * CDC Ethernet is the standard USB solution for sending Ethernet frames 5323cd1385SRemy Bohmer * using USB. Real hardware tends to use the same framing protocol but look 5423cd1385SRemy Bohmer * different for control features. This driver strongly prefers to use 5523cd1385SRemy Bohmer * this USB-IF standard as its open-systems interoperability solution; 5623cd1385SRemy Bohmer * most host side USB stacks (except from Microsoft) support it. 5723cd1385SRemy Bohmer * 5823cd1385SRemy Bohmer * This is sometimes called "CDC ECM" (Ethernet Control Model) to support 5923cd1385SRemy Bohmer * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new 6023cd1385SRemy Bohmer * "CDC EEM" (Ethernet Emulation Model) is starting to spread. 6123cd1385SRemy Bohmer * 6223cd1385SRemy Bohmer * There's some hardware that can't talk CDC ECM. We make that hardware 6323cd1385SRemy Bohmer * implement a "minimalist" vendor-agnostic CDC core: same framing, but 6423cd1385SRemy Bohmer * link-level setup only requires activating the configuration. Only the 6523cd1385SRemy Bohmer * endpoint descriptors, and product/vendor IDs, are relevant; no control 6623cd1385SRemy Bohmer * operations are available. Linux supports it, but other host operating 6723cd1385SRemy Bohmer * systems may not. (This is a subset of CDC Ethernet.) 6823cd1385SRemy Bohmer * 6923cd1385SRemy Bohmer * It turns out that if you add a few descriptors to that "CDC Subset", 7023cd1385SRemy Bohmer * (Windows) host side drivers from MCCI can treat it as one submode of 7123cd1385SRemy Bohmer * a proprietary scheme called "SAFE" ... without needing to know about 7223cd1385SRemy Bohmer * specific product/vendor IDs. So we do that, making it easier to use 7323cd1385SRemy Bohmer * those MS-Windows drivers. Those added descriptors make it resemble a 7423cd1385SRemy Bohmer * CDC MDLM device, but they don't change device behavior at all. (See 7523cd1385SRemy Bohmer * MCCI Engineering report 950198 "SAFE Networking Functions".) 7623cd1385SRemy Bohmer * 7723cd1385SRemy Bohmer * A third option is also in use. Rather than CDC Ethernet, or something 7823cd1385SRemy Bohmer * simpler, Microsoft pushes their own approach: RNDIS. The published 7923cd1385SRemy Bohmer * RNDIS specs are ambiguous and appear to be incomplete, and are also 8023cd1385SRemy Bohmer * needlessly complex. They borrow more from CDC ACM than CDC ECM. 8123cd1385SRemy Bohmer */ 8223cd1385SRemy Bohmer #define ETH_ALEN 6 /* Octets in one ethernet addr */ 8323cd1385SRemy Bohmer #define ETH_HLEN 14 /* Total octets in header. */ 8423cd1385SRemy Bohmer #define ETH_ZLEN 60 /* Min. octets in frame sans FCS */ 8523cd1385SRemy Bohmer #define ETH_DATA_LEN 1500 /* Max. octets in payload */ 8623cd1385SRemy Bohmer #define ETH_FRAME_LEN PKTSIZE_ALIGN /* Max. octets in frame sans FCS */ 8723cd1385SRemy Bohmer #define ETH_FCS_LEN 4 /* Octets in the FCS */ 8823cd1385SRemy Bohmer 8923cd1385SRemy Bohmer #define DRIVER_DESC "Ethernet Gadget" 9023cd1385SRemy Bohmer /* Based on linux 2.6.27 version */ 9123cd1385SRemy Bohmer #define DRIVER_VERSION "May Day 2005" 9223cd1385SRemy Bohmer 9323cd1385SRemy Bohmer static const char shortname[] = "ether"; 9423cd1385SRemy Bohmer static const char driver_desc[] = DRIVER_DESC; 9523cd1385SRemy Bohmer 9623cd1385SRemy Bohmer #define RX_EXTRA 20 /* guard against rx overflows */ 9723cd1385SRemy Bohmer 9823cd1385SRemy Bohmer /* CDC support the same host-chosen outgoing packet filters. */ 9923cd1385SRemy Bohmer #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \ 10023cd1385SRemy Bohmer |USB_CDC_PACKET_TYPE_ALL_MULTICAST \ 10123cd1385SRemy Bohmer |USB_CDC_PACKET_TYPE_PROMISCUOUS \ 10223cd1385SRemy Bohmer |USB_CDC_PACKET_TYPE_DIRECTED) 10323cd1385SRemy Bohmer 10423cd1385SRemy Bohmer #define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ) 10523cd1385SRemy Bohmer 10623cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 10723cd1385SRemy Bohmer static struct eth_dev l_ethdev; 10823cd1385SRemy Bohmer static struct eth_device l_netdev; 10923cd1385SRemy Bohmer static struct usb_gadget_driver eth_driver; 11023cd1385SRemy Bohmer 11123cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 11223cd1385SRemy Bohmer 11323cd1385SRemy Bohmer /* "main" config is either CDC, or its simple subset */ 11423cd1385SRemy Bohmer static inline int is_cdc(struct eth_dev *dev) 11523cd1385SRemy Bohmer { 11623cd1385SRemy Bohmer #if !defined(DEV_CONFIG_SUBSET) 11723cd1385SRemy Bohmer return 1; /* only cdc possible */ 11823cd1385SRemy Bohmer #elif !defined(DEV_CONFIG_CDC) 11923cd1385SRemy Bohmer return 0; /* only subset possible */ 12023cd1385SRemy Bohmer #else 12123cd1385SRemy Bohmer return dev->cdc; /* depends on what hardware we found */ 12223cd1385SRemy Bohmer #endif 12323cd1385SRemy Bohmer } 12423cd1385SRemy Bohmer 12523cd1385SRemy Bohmer #define subset_active(dev) (!is_cdc(dev)) 12623cd1385SRemy Bohmer #define cdc_active(dev) (is_cdc(dev)) 12723cd1385SRemy Bohmer 12823cd1385SRemy Bohmer #define DEFAULT_QLEN 2 /* double buffering by default */ 12923cd1385SRemy Bohmer 13023cd1385SRemy Bohmer /* peak bulk transfer bits-per-second */ 13123cd1385SRemy Bohmer #define HS_BPS (13 * 512 * 8 * 1000 * 8) 13223cd1385SRemy Bohmer #define FS_BPS (19 * 64 * 1 * 1000 * 8) 13323cd1385SRemy Bohmer 13423cd1385SRemy Bohmer #ifdef CONFIG_USB_GADGET_DUALSPEED 13523cd1385SRemy Bohmer #define DEVSPEED USB_SPEED_HIGH 13623cd1385SRemy Bohmer 1372721dbf1SVitaly Kuzmichev #ifdef CONFIG_USB_ETH_QMULT 1382721dbf1SVitaly Kuzmichev #define qmult CONFIG_USB_ETH_QMULT 1392721dbf1SVitaly Kuzmichev #else 1402721dbf1SVitaly Kuzmichev #define qmult 5 1412721dbf1SVitaly Kuzmichev #endif 1422721dbf1SVitaly Kuzmichev 14323cd1385SRemy Bohmer /* for dual-speed hardware, use deeper queues at highspeed */ 14423cd1385SRemy Bohmer #define qlen(gadget) \ 14523cd1385SRemy Bohmer (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1)) 14623cd1385SRemy Bohmer 14723cd1385SRemy Bohmer static inline int BITRATE(struct usb_gadget *g) 14823cd1385SRemy Bohmer { 14923cd1385SRemy Bohmer return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS; 15023cd1385SRemy Bohmer } 15123cd1385SRemy Bohmer 15223cd1385SRemy Bohmer #else /* full speed (low speed doesn't do bulk) */ 15323cd1385SRemy Bohmer 15423cd1385SRemy Bohmer #define qmult 1 15523cd1385SRemy Bohmer 15623cd1385SRemy Bohmer #define DEVSPEED USB_SPEED_FULL 15723cd1385SRemy Bohmer 15823cd1385SRemy Bohmer #define qlen(gadget) DEFAULT_QLEN 15923cd1385SRemy Bohmer 16023cd1385SRemy Bohmer static inline int BITRATE(struct usb_gadget *g) 16123cd1385SRemy Bohmer { 16223cd1385SRemy Bohmer return FS_BPS; 16323cd1385SRemy Bohmer } 16423cd1385SRemy Bohmer #endif 16523cd1385SRemy Bohmer 16623cd1385SRemy Bohmer struct eth_dev { 16723cd1385SRemy Bohmer struct usb_gadget *gadget; 16823cd1385SRemy Bohmer struct usb_request *req; /* for control responses */ 16923cd1385SRemy Bohmer struct usb_request *stat_req; /* for cdc status */ 17023cd1385SRemy Bohmer 17123cd1385SRemy Bohmer u8 config; 17223cd1385SRemy Bohmer struct usb_ep *in_ep, *out_ep, *status_ep; 17323cd1385SRemy Bohmer const struct usb_endpoint_descriptor 17423cd1385SRemy Bohmer *in, *out, *status; 17523cd1385SRemy Bohmer 17623cd1385SRemy Bohmer struct usb_request *tx_req, *rx_req; 17723cd1385SRemy Bohmer 17823cd1385SRemy Bohmer struct eth_device *net; 179*c85d70efSVitaly Kuzmichev struct net_device_stats stats; 18023cd1385SRemy Bohmer unsigned int tx_qlen; 18123cd1385SRemy Bohmer 18223cd1385SRemy Bohmer unsigned zlp:1; 18323cd1385SRemy Bohmer unsigned cdc:1; 18423cd1385SRemy Bohmer unsigned suspended:1; 18523cd1385SRemy Bohmer unsigned network_started:1; 18623cd1385SRemy Bohmer u16 cdc_filter; 18723cd1385SRemy Bohmer unsigned long todo; 18823cd1385SRemy Bohmer int mtu; 18923cd1385SRemy Bohmer #define WORK_RX_MEMORY 0 19023cd1385SRemy Bohmer u8 host_mac[ETH_ALEN]; 19123cd1385SRemy Bohmer }; 19223cd1385SRemy Bohmer 1936142e0aeSVitaly Kuzmichev /* 1946142e0aeSVitaly Kuzmichev * This version autoconfigures as much as possible at run-time. 19523cd1385SRemy Bohmer * 19623cd1385SRemy Bohmer * It also ASSUMES a self-powered device, without remote wakeup, 19723cd1385SRemy Bohmer * although remote wakeup support would make sense. 19823cd1385SRemy Bohmer */ 19923cd1385SRemy Bohmer 20023cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 20123cd1385SRemy Bohmer 2026142e0aeSVitaly Kuzmichev /* 2036142e0aeSVitaly Kuzmichev * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! 20423cd1385SRemy Bohmer * Instead: allocate your own, using normal USB-IF procedures. 20523cd1385SRemy Bohmer */ 20623cd1385SRemy Bohmer 2076142e0aeSVitaly Kuzmichev /* 2086142e0aeSVitaly Kuzmichev * Thanks to NetChip Technologies for donating this product ID. 20923cd1385SRemy Bohmer * It's for devices with only CDC Ethernet configurations. 21023cd1385SRemy Bohmer */ 21123cd1385SRemy Bohmer #define CDC_VENDOR_NUM 0x0525 /* NetChip */ 21223cd1385SRemy Bohmer #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */ 21323cd1385SRemy Bohmer 2146142e0aeSVitaly Kuzmichev /* 2156142e0aeSVitaly Kuzmichev * For hardware that can't talk CDC, we use the same vendor ID that 21623cd1385SRemy Bohmer * ARM Linux has used for ethernet-over-usb, both with sa1100 and 21723cd1385SRemy Bohmer * with pxa250. We're protocol-compatible, if the host-side drivers 21823cd1385SRemy Bohmer * use the endpoint descriptors. bcdDevice (version) is nonzero, so 21923cd1385SRemy Bohmer * drivers that need to hard-wire endpoint numbers have a hook. 22023cd1385SRemy Bohmer * 22123cd1385SRemy Bohmer * The protocol is a minimal subset of CDC Ether, which works on any bulk 22223cd1385SRemy Bohmer * hardware that's not deeply broken ... even on hardware that can't talk 22323cd1385SRemy Bohmer * RNDIS (like SA-1100, with no interrupt endpoint, or anything that 22423cd1385SRemy Bohmer * doesn't handle control-OUT). 22523cd1385SRemy Bohmer */ 22623cd1385SRemy Bohmer #define SIMPLE_VENDOR_NUM 0x049f 22723cd1385SRemy Bohmer #define SIMPLE_PRODUCT_NUM 0x505a 22823cd1385SRemy Bohmer 2296142e0aeSVitaly Kuzmichev /* 2306142e0aeSVitaly Kuzmichev * Some systems will want different product identifers published in the 23123cd1385SRemy Bohmer * device descriptor, either numbers or strings or both. These string 23223cd1385SRemy Bohmer * parameters are in UTF-8 (superset of ASCII's 7 bit characters). 23323cd1385SRemy Bohmer */ 23423cd1385SRemy Bohmer 23523cd1385SRemy Bohmer static ushort bcdDevice; 23623cd1385SRemy Bohmer #if defined(CONFIG_USBNET_MANUFACTURER) 23723cd1385SRemy Bohmer static char *iManufacturer = CONFIG_USBNET_MANUFACTURER; 23823cd1385SRemy Bohmer #else 23923cd1385SRemy Bohmer static char *iManufacturer = "U-boot"; 24023cd1385SRemy Bohmer #endif 24123cd1385SRemy Bohmer static char *iProduct; 24223cd1385SRemy Bohmer static char *iSerialNumber; 24323cd1385SRemy Bohmer static char dev_addr[18]; 24423cd1385SRemy Bohmer static char host_addr[18]; 24523cd1385SRemy Bohmer 24623cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 24723cd1385SRemy Bohmer 2486142e0aeSVitaly Kuzmichev /* 2496142e0aeSVitaly Kuzmichev * USB DRIVER HOOKUP (to the hardware driver, below us), mostly 25023cd1385SRemy Bohmer * ep0 implementation: descriptors, config management, setup(). 25123cd1385SRemy Bohmer * also optional class-specific notification interrupt transfer. 25223cd1385SRemy Bohmer */ 25323cd1385SRemy Bohmer 25423cd1385SRemy Bohmer /* 25523cd1385SRemy Bohmer * DESCRIPTORS ... most are static, but strings and (full) configuration 25623cd1385SRemy Bohmer * descriptors are built on demand. For now we do either full CDC, or 25723cd1385SRemy Bohmer * our simple subset. 25823cd1385SRemy Bohmer */ 25923cd1385SRemy Bohmer 26023cd1385SRemy Bohmer #define STRING_MANUFACTURER 1 26123cd1385SRemy Bohmer #define STRING_PRODUCT 2 26223cd1385SRemy Bohmer #define STRING_ETHADDR 3 26323cd1385SRemy Bohmer #define STRING_DATA 4 26423cd1385SRemy Bohmer #define STRING_CONTROL 5 26523cd1385SRemy Bohmer #define STRING_CDC 7 26623cd1385SRemy Bohmer #define STRING_SUBSET 8 26723cd1385SRemy Bohmer #define STRING_SERIALNUMBER 10 26823cd1385SRemy Bohmer 26923cd1385SRemy Bohmer /* holds our biggest descriptor */ 27023cd1385SRemy Bohmer #define USB_BUFSIZ 256 27123cd1385SRemy Bohmer 27223cd1385SRemy Bohmer /* 27323cd1385SRemy Bohmer * This device advertises one configuration, eth_config, 27423cd1385SRemy Bohmer * on hardware supporting at least two configs. 27523cd1385SRemy Bohmer * 27623cd1385SRemy Bohmer * FIXME define some higher-powered configurations to make it easier 27723cd1385SRemy Bohmer * to recharge batteries ... 27823cd1385SRemy Bohmer */ 27923cd1385SRemy Bohmer 28023cd1385SRemy Bohmer #define DEV_CONFIG_VALUE 1 /* cdc or subset */ 28123cd1385SRemy Bohmer 28223cd1385SRemy Bohmer static struct usb_device_descriptor 28323cd1385SRemy Bohmer device_desc = { 28423cd1385SRemy Bohmer .bLength = sizeof device_desc, 28523cd1385SRemy Bohmer .bDescriptorType = USB_DT_DEVICE, 28623cd1385SRemy Bohmer 28723cd1385SRemy Bohmer .bcdUSB = __constant_cpu_to_le16(0x0200), 28823cd1385SRemy Bohmer 28923cd1385SRemy Bohmer .bDeviceClass = USB_CLASS_COMM, 29023cd1385SRemy Bohmer .bDeviceSubClass = 0, 29123cd1385SRemy Bohmer .bDeviceProtocol = 0, 29223cd1385SRemy Bohmer 29323cd1385SRemy Bohmer .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM), 29423cd1385SRemy Bohmer .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM), 29523cd1385SRemy Bohmer .iManufacturer = STRING_MANUFACTURER, 29623cd1385SRemy Bohmer .iProduct = STRING_PRODUCT, 29723cd1385SRemy Bohmer .bNumConfigurations = 1, 29823cd1385SRemy Bohmer }; 29923cd1385SRemy Bohmer 30023cd1385SRemy Bohmer static struct usb_otg_descriptor 30123cd1385SRemy Bohmer otg_descriptor = { 30223cd1385SRemy Bohmer .bLength = sizeof otg_descriptor, 30323cd1385SRemy Bohmer .bDescriptorType = USB_DT_OTG, 30423cd1385SRemy Bohmer 30523cd1385SRemy Bohmer .bmAttributes = USB_OTG_SRP, 30623cd1385SRemy Bohmer }; 30723cd1385SRemy Bohmer 30823cd1385SRemy Bohmer static struct usb_config_descriptor 30923cd1385SRemy Bohmer eth_config = { 31023cd1385SRemy Bohmer .bLength = sizeof eth_config, 31123cd1385SRemy Bohmer .bDescriptorType = USB_DT_CONFIG, 31223cd1385SRemy Bohmer 31323cd1385SRemy Bohmer /* compute wTotalLength on the fly */ 31423cd1385SRemy Bohmer .bNumInterfaces = 2, 31523cd1385SRemy Bohmer .bConfigurationValue = DEV_CONFIG_VALUE, 31623cd1385SRemy Bohmer .iConfiguration = STRING_CDC, 31723cd1385SRemy Bohmer .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, 31823cd1385SRemy Bohmer .bMaxPower = 1, 31923cd1385SRemy Bohmer }; 32023cd1385SRemy Bohmer 32123cd1385SRemy Bohmer /* 32223cd1385SRemy Bohmer * Compared to the simple CDC subset, the full CDC Ethernet model adds 32323cd1385SRemy Bohmer * three class descriptors, two interface descriptors, optional status 32423cd1385SRemy Bohmer * endpoint. Both have a "data" interface and two bulk endpoints. 32523cd1385SRemy Bohmer * There are also differences in how control requests are handled. 32623cd1385SRemy Bohmer */ 32723cd1385SRemy Bohmer 32823cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC 32923cd1385SRemy Bohmer static struct usb_interface_descriptor 33023cd1385SRemy Bohmer control_intf = { 33123cd1385SRemy Bohmer .bLength = sizeof control_intf, 33223cd1385SRemy Bohmer .bDescriptorType = USB_DT_INTERFACE, 33323cd1385SRemy Bohmer 33423cd1385SRemy Bohmer .bInterfaceNumber = 0, 33523cd1385SRemy Bohmer /* status endpoint is optional; this may be patched later */ 33623cd1385SRemy Bohmer .bNumEndpoints = 1, 33723cd1385SRemy Bohmer .bInterfaceClass = USB_CLASS_COMM, 33823cd1385SRemy Bohmer .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, 33923cd1385SRemy Bohmer .bInterfaceProtocol = USB_CDC_PROTO_NONE, 34023cd1385SRemy Bohmer .iInterface = STRING_CONTROL, 34123cd1385SRemy Bohmer }; 34223cd1385SRemy Bohmer #endif 34323cd1385SRemy Bohmer 34423cd1385SRemy Bohmer static const struct usb_cdc_header_desc header_desc = { 34523cd1385SRemy Bohmer .bLength = sizeof header_desc, 34623cd1385SRemy Bohmer .bDescriptorType = USB_DT_CS_INTERFACE, 34723cd1385SRemy Bohmer .bDescriptorSubType = USB_CDC_HEADER_TYPE, 34823cd1385SRemy Bohmer 34923cd1385SRemy Bohmer .bcdCDC = __constant_cpu_to_le16(0x0110), 35023cd1385SRemy Bohmer }; 35123cd1385SRemy Bohmer 35223cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC) 35323cd1385SRemy Bohmer 35423cd1385SRemy Bohmer static const struct usb_cdc_union_desc union_desc = { 35523cd1385SRemy Bohmer .bLength = sizeof union_desc, 35623cd1385SRemy Bohmer .bDescriptorType = USB_DT_CS_INTERFACE, 35723cd1385SRemy Bohmer .bDescriptorSubType = USB_CDC_UNION_TYPE, 35823cd1385SRemy Bohmer 35923cd1385SRemy Bohmer .bMasterInterface0 = 0, /* index of control interface */ 36023cd1385SRemy Bohmer .bSlaveInterface0 = 1, /* index of DATA interface */ 36123cd1385SRemy Bohmer }; 36223cd1385SRemy Bohmer 36323cd1385SRemy Bohmer #endif /* CDC */ 36423cd1385SRemy Bohmer 36523cd1385SRemy Bohmer #ifndef DEV_CONFIG_CDC 36623cd1385SRemy Bohmer 3676142e0aeSVitaly Kuzmichev /* 3686142e0aeSVitaly Kuzmichev * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various 36923cd1385SRemy Bohmer * ways: data endpoints live in the control interface, there's no data 37023cd1385SRemy Bohmer * interface, and it's not used to talk to a cell phone radio. 37123cd1385SRemy Bohmer */ 37223cd1385SRemy Bohmer 37323cd1385SRemy Bohmer static const struct usb_cdc_mdlm_desc mdlm_desc = { 37423cd1385SRemy Bohmer .bLength = sizeof mdlm_desc, 37523cd1385SRemy Bohmer .bDescriptorType = USB_DT_CS_INTERFACE, 37623cd1385SRemy Bohmer .bDescriptorSubType = USB_CDC_MDLM_TYPE, 37723cd1385SRemy Bohmer 37823cd1385SRemy Bohmer .bcdVersion = __constant_cpu_to_le16(0x0100), 37923cd1385SRemy Bohmer .bGUID = { 38023cd1385SRemy Bohmer 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6, 38123cd1385SRemy Bohmer 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f, 38223cd1385SRemy Bohmer }, 38323cd1385SRemy Bohmer }; 38423cd1385SRemy Bohmer 3856142e0aeSVitaly Kuzmichev /* 3866142e0aeSVitaly Kuzmichev * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we 38723cd1385SRemy Bohmer * can't really use its struct. All we do here is say that we're using 38823cd1385SRemy Bohmer * the submode of "SAFE" which directly matches the CDC Subset. 38923cd1385SRemy Bohmer */ 39023cd1385SRemy Bohmer static const u8 mdlm_detail_desc[] = { 39123cd1385SRemy Bohmer 6, 39223cd1385SRemy Bohmer USB_DT_CS_INTERFACE, 39323cd1385SRemy Bohmer USB_CDC_MDLM_DETAIL_TYPE, 39423cd1385SRemy Bohmer 39523cd1385SRemy Bohmer 0, /* "SAFE" */ 39623cd1385SRemy Bohmer 0, /* network control capabilities (none) */ 39723cd1385SRemy Bohmer 0, /* network data capabilities ("raw" encapsulation) */ 39823cd1385SRemy Bohmer }; 39923cd1385SRemy Bohmer 40023cd1385SRemy Bohmer #endif 40123cd1385SRemy Bohmer 40223cd1385SRemy Bohmer static const struct usb_cdc_ether_desc ether_desc = { 40323cd1385SRemy Bohmer .bLength = sizeof(ether_desc), 40423cd1385SRemy Bohmer .bDescriptorType = USB_DT_CS_INTERFACE, 40523cd1385SRemy Bohmer .bDescriptorSubType = USB_CDC_ETHERNET_TYPE, 40623cd1385SRemy Bohmer 40723cd1385SRemy Bohmer /* this descriptor actually adds value, surprise! */ 40823cd1385SRemy Bohmer .iMACAddress = STRING_ETHADDR, 40923cd1385SRemy Bohmer .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */ 41023cd1385SRemy Bohmer .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN), 41123cd1385SRemy Bohmer .wNumberMCFilters = __constant_cpu_to_le16(0), 41223cd1385SRemy Bohmer .bNumberPowerFilters = 0, 41323cd1385SRemy Bohmer }; 41423cd1385SRemy Bohmer 41523cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC) 41623cd1385SRemy Bohmer 4176142e0aeSVitaly Kuzmichev /* 4186142e0aeSVitaly Kuzmichev * include the status endpoint if we can, even where it's optional. 41923cd1385SRemy Bohmer * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one 42023cd1385SRemy Bohmer * packet, to simplify cancellation; and a big transfer interval, to 42123cd1385SRemy Bohmer * waste less bandwidth. 42223cd1385SRemy Bohmer * 42323cd1385SRemy Bohmer * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even 42423cd1385SRemy Bohmer * if they ignore the connect/disconnect notifications that real aether 42523cd1385SRemy Bohmer * can provide. more advanced cdc configurations might want to support 42623cd1385SRemy Bohmer * encapsulated commands (vendor-specific, using control-OUT). 42723cd1385SRemy Bohmer */ 42823cd1385SRemy Bohmer 42923cd1385SRemy Bohmer #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */ 43023cd1385SRemy Bohmer #define STATUS_BYTECOUNT 16 /* 8 byte header + data */ 43123cd1385SRemy Bohmer 43223cd1385SRemy Bohmer static struct usb_endpoint_descriptor 43323cd1385SRemy Bohmer fs_status_desc = { 43423cd1385SRemy Bohmer .bLength = USB_DT_ENDPOINT_SIZE, 43523cd1385SRemy Bohmer .bDescriptorType = USB_DT_ENDPOINT, 43623cd1385SRemy Bohmer 43723cd1385SRemy Bohmer .bEndpointAddress = USB_DIR_IN, 43823cd1385SRemy Bohmer .bmAttributes = USB_ENDPOINT_XFER_INT, 43923cd1385SRemy Bohmer .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT), 44023cd1385SRemy Bohmer .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC, 44123cd1385SRemy Bohmer }; 44223cd1385SRemy Bohmer #endif 44323cd1385SRemy Bohmer 44423cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC 44523cd1385SRemy Bohmer 44623cd1385SRemy Bohmer /* the default data interface has no endpoints ... */ 44723cd1385SRemy Bohmer 44823cd1385SRemy Bohmer static const struct usb_interface_descriptor 44923cd1385SRemy Bohmer data_nop_intf = { 45023cd1385SRemy Bohmer .bLength = sizeof data_nop_intf, 45123cd1385SRemy Bohmer .bDescriptorType = USB_DT_INTERFACE, 45223cd1385SRemy Bohmer 45323cd1385SRemy Bohmer .bInterfaceNumber = 1, 45423cd1385SRemy Bohmer .bAlternateSetting = 0, 45523cd1385SRemy Bohmer .bNumEndpoints = 0, 45623cd1385SRemy Bohmer .bInterfaceClass = USB_CLASS_CDC_DATA, 45723cd1385SRemy Bohmer .bInterfaceSubClass = 0, 45823cd1385SRemy Bohmer .bInterfaceProtocol = 0, 45923cd1385SRemy Bohmer }; 46023cd1385SRemy Bohmer 46123cd1385SRemy Bohmer /* ... but the "real" data interface has two bulk endpoints */ 46223cd1385SRemy Bohmer 46323cd1385SRemy Bohmer static const struct usb_interface_descriptor 46423cd1385SRemy Bohmer data_intf = { 46523cd1385SRemy Bohmer .bLength = sizeof data_intf, 46623cd1385SRemy Bohmer .bDescriptorType = USB_DT_INTERFACE, 46723cd1385SRemy Bohmer 46823cd1385SRemy Bohmer .bInterfaceNumber = 1, 46923cd1385SRemy Bohmer .bAlternateSetting = 1, 47023cd1385SRemy Bohmer .bNumEndpoints = 2, 47123cd1385SRemy Bohmer .bInterfaceClass = USB_CLASS_CDC_DATA, 47223cd1385SRemy Bohmer .bInterfaceSubClass = 0, 47323cd1385SRemy Bohmer .bInterfaceProtocol = 0, 47423cd1385SRemy Bohmer .iInterface = STRING_DATA, 47523cd1385SRemy Bohmer }; 47623cd1385SRemy Bohmer 47723cd1385SRemy Bohmer #endif 47823cd1385SRemy Bohmer 47923cd1385SRemy Bohmer #ifdef DEV_CONFIG_SUBSET 48023cd1385SRemy Bohmer 48123cd1385SRemy Bohmer /* 48223cd1385SRemy Bohmer * "Simple" CDC-subset option is a simple vendor-neutral model that most 48323cd1385SRemy Bohmer * full speed controllers can handle: one interface, two bulk endpoints. 48423cd1385SRemy Bohmer * 48523cd1385SRemy Bohmer * To assist host side drivers, we fancy it up a bit, and add descriptors 48623cd1385SRemy Bohmer * so some host side drivers will understand it as a "SAFE" variant. 48723cd1385SRemy Bohmer */ 48823cd1385SRemy Bohmer 48923cd1385SRemy Bohmer static const struct usb_interface_descriptor 49023cd1385SRemy Bohmer subset_data_intf = { 49123cd1385SRemy Bohmer .bLength = sizeof subset_data_intf, 49223cd1385SRemy Bohmer .bDescriptorType = USB_DT_INTERFACE, 49323cd1385SRemy Bohmer 49423cd1385SRemy Bohmer .bInterfaceNumber = 0, 49523cd1385SRemy Bohmer .bAlternateSetting = 0, 49623cd1385SRemy Bohmer .bNumEndpoints = 2, 49723cd1385SRemy Bohmer .bInterfaceClass = USB_CLASS_COMM, 49823cd1385SRemy Bohmer .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM, 49923cd1385SRemy Bohmer .bInterfaceProtocol = 0, 50023cd1385SRemy Bohmer .iInterface = STRING_DATA, 50123cd1385SRemy Bohmer }; 50223cd1385SRemy Bohmer 50323cd1385SRemy Bohmer #endif /* SUBSET */ 50423cd1385SRemy Bohmer 50523cd1385SRemy Bohmer static struct usb_endpoint_descriptor 50623cd1385SRemy Bohmer fs_source_desc = { 50723cd1385SRemy Bohmer .bLength = USB_DT_ENDPOINT_SIZE, 50823cd1385SRemy Bohmer .bDescriptorType = USB_DT_ENDPOINT, 50923cd1385SRemy Bohmer 51023cd1385SRemy Bohmer .bEndpointAddress = USB_DIR_IN, 51123cd1385SRemy Bohmer .bmAttributes = USB_ENDPOINT_XFER_BULK, 51223cd1385SRemy Bohmer }; 51323cd1385SRemy Bohmer 51423cd1385SRemy Bohmer static struct usb_endpoint_descriptor 51523cd1385SRemy Bohmer fs_sink_desc = { 51623cd1385SRemy Bohmer .bLength = USB_DT_ENDPOINT_SIZE, 51723cd1385SRemy Bohmer .bDescriptorType = USB_DT_ENDPOINT, 51823cd1385SRemy Bohmer 51923cd1385SRemy Bohmer .bEndpointAddress = USB_DIR_OUT, 52023cd1385SRemy Bohmer .bmAttributes = USB_ENDPOINT_XFER_BULK, 52123cd1385SRemy Bohmer }; 52223cd1385SRemy Bohmer 52323cd1385SRemy Bohmer static const struct usb_descriptor_header *fs_eth_function[11] = { 52423cd1385SRemy Bohmer (struct usb_descriptor_header *) &otg_descriptor, 52523cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC 52623cd1385SRemy Bohmer /* "cdc" mode descriptors */ 52723cd1385SRemy Bohmer (struct usb_descriptor_header *) &control_intf, 52823cd1385SRemy Bohmer (struct usb_descriptor_header *) &header_desc, 52923cd1385SRemy Bohmer (struct usb_descriptor_header *) &union_desc, 53023cd1385SRemy Bohmer (struct usb_descriptor_header *) ðer_desc, 53123cd1385SRemy Bohmer /* NOTE: status endpoint may need to be removed */ 53223cd1385SRemy Bohmer (struct usb_descriptor_header *) &fs_status_desc, 53323cd1385SRemy Bohmer /* data interface, with altsetting */ 53423cd1385SRemy Bohmer (struct usb_descriptor_header *) &data_nop_intf, 53523cd1385SRemy Bohmer (struct usb_descriptor_header *) &data_intf, 53623cd1385SRemy Bohmer (struct usb_descriptor_header *) &fs_source_desc, 53723cd1385SRemy Bohmer (struct usb_descriptor_header *) &fs_sink_desc, 53823cd1385SRemy Bohmer NULL, 53923cd1385SRemy Bohmer #endif /* DEV_CONFIG_CDC */ 54023cd1385SRemy Bohmer }; 54123cd1385SRemy Bohmer 54223cd1385SRemy Bohmer static inline void fs_subset_descriptors(void) 54323cd1385SRemy Bohmer { 54423cd1385SRemy Bohmer #ifdef DEV_CONFIG_SUBSET 54523cd1385SRemy Bohmer /* behavior is "CDC Subset"; extra descriptors say "SAFE" */ 54623cd1385SRemy Bohmer fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf; 54723cd1385SRemy Bohmer fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc; 54823cd1385SRemy Bohmer fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc; 54923cd1385SRemy Bohmer fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc; 55023cd1385SRemy Bohmer fs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc; 55123cd1385SRemy Bohmer fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc; 55223cd1385SRemy Bohmer fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc; 55323cd1385SRemy Bohmer fs_eth_function[8] = NULL; 55423cd1385SRemy Bohmer #else 55523cd1385SRemy Bohmer fs_eth_function[1] = NULL; 55623cd1385SRemy Bohmer #endif 55723cd1385SRemy Bohmer } 55823cd1385SRemy Bohmer 55923cd1385SRemy Bohmer /* 56023cd1385SRemy Bohmer * usb 2.0 devices need to expose both high speed and full speed 56123cd1385SRemy Bohmer * descriptors, unless they only run at full speed. 56223cd1385SRemy Bohmer */ 56323cd1385SRemy Bohmer 56423cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC) 56523cd1385SRemy Bohmer static struct usb_endpoint_descriptor 56623cd1385SRemy Bohmer hs_status_desc = { 56723cd1385SRemy Bohmer .bLength = USB_DT_ENDPOINT_SIZE, 56823cd1385SRemy Bohmer .bDescriptorType = USB_DT_ENDPOINT, 56923cd1385SRemy Bohmer 57023cd1385SRemy Bohmer .bmAttributes = USB_ENDPOINT_XFER_INT, 57123cd1385SRemy Bohmer .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT), 57223cd1385SRemy Bohmer .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4, 57323cd1385SRemy Bohmer }; 57423cd1385SRemy Bohmer #endif /* DEV_CONFIG_CDC */ 57523cd1385SRemy Bohmer 57623cd1385SRemy Bohmer static struct usb_endpoint_descriptor 57723cd1385SRemy Bohmer hs_source_desc = { 57823cd1385SRemy Bohmer .bLength = USB_DT_ENDPOINT_SIZE, 57923cd1385SRemy Bohmer .bDescriptorType = USB_DT_ENDPOINT, 58023cd1385SRemy Bohmer 58123cd1385SRemy Bohmer .bmAttributes = USB_ENDPOINT_XFER_BULK, 58223cd1385SRemy Bohmer .wMaxPacketSize = __constant_cpu_to_le16(512), 58323cd1385SRemy Bohmer }; 58423cd1385SRemy Bohmer 58523cd1385SRemy Bohmer static struct usb_endpoint_descriptor 58623cd1385SRemy Bohmer hs_sink_desc = { 58723cd1385SRemy Bohmer .bLength = USB_DT_ENDPOINT_SIZE, 58823cd1385SRemy Bohmer .bDescriptorType = USB_DT_ENDPOINT, 58923cd1385SRemy Bohmer 59023cd1385SRemy Bohmer .bmAttributes = USB_ENDPOINT_XFER_BULK, 59123cd1385SRemy Bohmer .wMaxPacketSize = __constant_cpu_to_le16(512), 59223cd1385SRemy Bohmer }; 59323cd1385SRemy Bohmer 59423cd1385SRemy Bohmer static struct usb_qualifier_descriptor 59523cd1385SRemy Bohmer dev_qualifier = { 59623cd1385SRemy Bohmer .bLength = sizeof dev_qualifier, 59723cd1385SRemy Bohmer .bDescriptorType = USB_DT_DEVICE_QUALIFIER, 59823cd1385SRemy Bohmer 59923cd1385SRemy Bohmer .bcdUSB = __constant_cpu_to_le16(0x0200), 60023cd1385SRemy Bohmer .bDeviceClass = USB_CLASS_COMM, 60123cd1385SRemy Bohmer 60223cd1385SRemy Bohmer .bNumConfigurations = 1, 60323cd1385SRemy Bohmer }; 60423cd1385SRemy Bohmer 60523cd1385SRemy Bohmer static const struct usb_descriptor_header *hs_eth_function[11] = { 60623cd1385SRemy Bohmer (struct usb_descriptor_header *) &otg_descriptor, 60723cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC 60823cd1385SRemy Bohmer /* "cdc" mode descriptors */ 60923cd1385SRemy Bohmer (struct usb_descriptor_header *) &control_intf, 61023cd1385SRemy Bohmer (struct usb_descriptor_header *) &header_desc, 61123cd1385SRemy Bohmer (struct usb_descriptor_header *) &union_desc, 61223cd1385SRemy Bohmer (struct usb_descriptor_header *) ðer_desc, 61323cd1385SRemy Bohmer /* NOTE: status endpoint may need to be removed */ 61423cd1385SRemy Bohmer (struct usb_descriptor_header *) &hs_status_desc, 61523cd1385SRemy Bohmer /* data interface, with altsetting */ 61623cd1385SRemy Bohmer (struct usb_descriptor_header *) &data_nop_intf, 61723cd1385SRemy Bohmer (struct usb_descriptor_header *) &data_intf, 61823cd1385SRemy Bohmer (struct usb_descriptor_header *) &hs_source_desc, 61923cd1385SRemy Bohmer (struct usb_descriptor_header *) &hs_sink_desc, 62023cd1385SRemy Bohmer NULL, 62123cd1385SRemy Bohmer #endif /* DEV_CONFIG_CDC */ 62223cd1385SRemy Bohmer }; 62323cd1385SRemy Bohmer 62423cd1385SRemy Bohmer static inline void hs_subset_descriptors(void) 62523cd1385SRemy Bohmer { 62623cd1385SRemy Bohmer #ifdef DEV_CONFIG_SUBSET 62723cd1385SRemy Bohmer /* behavior is "CDC Subset"; extra descriptors say "SAFE" */ 62823cd1385SRemy Bohmer hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf; 62923cd1385SRemy Bohmer hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc; 63023cd1385SRemy Bohmer hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc; 63123cd1385SRemy Bohmer hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc; 63223cd1385SRemy Bohmer hs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc; 63323cd1385SRemy Bohmer hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc; 63423cd1385SRemy Bohmer hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc; 63523cd1385SRemy Bohmer hs_eth_function[8] = NULL; 63623cd1385SRemy Bohmer #else 63723cd1385SRemy Bohmer hs_eth_function[1] = NULL; 63823cd1385SRemy Bohmer #endif 63923cd1385SRemy Bohmer } 64023cd1385SRemy Bohmer 64123cd1385SRemy Bohmer /* maxpacket and other transfer characteristics vary by speed. */ 64223cd1385SRemy Bohmer static inline struct usb_endpoint_descriptor * 64323cd1385SRemy Bohmer ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs, 64423cd1385SRemy Bohmer struct usb_endpoint_descriptor *fs) 64523cd1385SRemy Bohmer { 64623cd1385SRemy Bohmer if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) 64723cd1385SRemy Bohmer return hs; 64823cd1385SRemy Bohmer return fs; 64923cd1385SRemy Bohmer } 65023cd1385SRemy Bohmer 65123cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 65223cd1385SRemy Bohmer 65323cd1385SRemy Bohmer /* descriptors that are built on-demand */ 65423cd1385SRemy Bohmer 65523cd1385SRemy Bohmer static char manufacturer[50]; 65623cd1385SRemy Bohmer static char product_desc[40] = DRIVER_DESC; 65723cd1385SRemy Bohmer static char serial_number[20]; 65823cd1385SRemy Bohmer 65923cd1385SRemy Bohmer /* address that the host will use ... usually assigned at random */ 66023cd1385SRemy Bohmer static char ethaddr[2 * ETH_ALEN + 1]; 66123cd1385SRemy Bohmer 66223cd1385SRemy Bohmer /* static strings, in UTF-8 */ 66323cd1385SRemy Bohmer static struct usb_string strings[] = { 66423cd1385SRemy Bohmer { STRING_MANUFACTURER, manufacturer, }, 66523cd1385SRemy Bohmer { STRING_PRODUCT, product_desc, }, 66623cd1385SRemy Bohmer { STRING_SERIALNUMBER, serial_number, }, 66723cd1385SRemy Bohmer { STRING_DATA, "Ethernet Data", }, 66823cd1385SRemy Bohmer { STRING_ETHADDR, ethaddr, }, 66923cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC 67023cd1385SRemy Bohmer { STRING_CDC, "CDC Ethernet", }, 67123cd1385SRemy Bohmer { STRING_CONTROL, "CDC Communications Control", }, 67223cd1385SRemy Bohmer #endif 67323cd1385SRemy Bohmer #ifdef DEV_CONFIG_SUBSET 67423cd1385SRemy Bohmer { STRING_SUBSET, "CDC Ethernet Subset", }, 67523cd1385SRemy Bohmer #endif 67623cd1385SRemy Bohmer { } /* end of list */ 67723cd1385SRemy Bohmer }; 67823cd1385SRemy Bohmer 67923cd1385SRemy Bohmer static struct usb_gadget_strings stringtab = { 68023cd1385SRemy Bohmer .language = 0x0409, /* en-us */ 68123cd1385SRemy Bohmer .strings = strings, 68223cd1385SRemy Bohmer }; 68323cd1385SRemy Bohmer 68423cd1385SRemy Bohmer /*============================================================================*/ 68523cd1385SRemy Bohmer static u8 control_req[USB_BUFSIZ]; 68680460772SStefano Babic static u8 status_req[STATUS_BYTECOUNT] __attribute__ ((aligned(4))); 68723cd1385SRemy Bohmer 68823cd1385SRemy Bohmer 68923cd1385SRemy Bohmer /** 69023cd1385SRemy Bohmer * strlcpy - Copy a %NUL terminated string into a sized buffer 69123cd1385SRemy Bohmer * @dest: Where to copy the string to 69223cd1385SRemy Bohmer * @src: Where to copy the string from 69323cd1385SRemy Bohmer * @size: size of destination buffer 69423cd1385SRemy Bohmer * 69523cd1385SRemy Bohmer * Compatible with *BSD: the result is always a valid 69623cd1385SRemy Bohmer * NUL-terminated string that fits in the buffer (unless, 69723cd1385SRemy Bohmer * of course, the buffer size is zero). It does not pad 69823cd1385SRemy Bohmer * out the result like strncpy() does. 69923cd1385SRemy Bohmer */ 70023cd1385SRemy Bohmer size_t strlcpy(char *dest, const char *src, size_t size) 70123cd1385SRemy Bohmer { 70223cd1385SRemy Bohmer size_t ret = strlen(src); 70323cd1385SRemy Bohmer 70423cd1385SRemy Bohmer if (size) { 70523cd1385SRemy Bohmer size_t len = (ret >= size) ? size - 1 : ret; 70623cd1385SRemy Bohmer memcpy(dest, src, len); 70723cd1385SRemy Bohmer dest[len] = '\0'; 70823cd1385SRemy Bohmer } 70923cd1385SRemy Bohmer return ret; 71023cd1385SRemy Bohmer } 71123cd1385SRemy Bohmer 71223cd1385SRemy Bohmer /*============================================================================*/ 71323cd1385SRemy Bohmer 71423cd1385SRemy Bohmer /* 71523cd1385SRemy Bohmer * one config, two interfaces: control, data. 71623cd1385SRemy Bohmer * complications: class descriptors, and an altsetting. 71723cd1385SRemy Bohmer */ 71823cd1385SRemy Bohmer static int 71923cd1385SRemy Bohmer config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg) 72023cd1385SRemy Bohmer { 72123cd1385SRemy Bohmer int len; 72223cd1385SRemy Bohmer const struct usb_config_descriptor *config; 72323cd1385SRemy Bohmer const struct usb_descriptor_header **function; 72423cd1385SRemy Bohmer int hs = 0; 72523cd1385SRemy Bohmer 72623cd1385SRemy Bohmer if (gadget_is_dualspeed(g)) { 72723cd1385SRemy Bohmer hs = (g->speed == USB_SPEED_HIGH); 72823cd1385SRemy Bohmer if (type == USB_DT_OTHER_SPEED_CONFIG) 72923cd1385SRemy Bohmer hs = !hs; 73023cd1385SRemy Bohmer } 73123cd1385SRemy Bohmer #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function) 73223cd1385SRemy Bohmer 73323cd1385SRemy Bohmer if (index >= device_desc.bNumConfigurations) 73423cd1385SRemy Bohmer return -EINVAL; 73523cd1385SRemy Bohmer 73623cd1385SRemy Bohmer config = ð_config; 73723cd1385SRemy Bohmer function = which_fn(eth); 73823cd1385SRemy Bohmer 73923cd1385SRemy Bohmer /* for now, don't advertise srp-only devices */ 74023cd1385SRemy Bohmer if (!is_otg) 74123cd1385SRemy Bohmer function++; 74223cd1385SRemy Bohmer 74323cd1385SRemy Bohmer len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function); 74423cd1385SRemy Bohmer if (len < 0) 74523cd1385SRemy Bohmer return len; 74623cd1385SRemy Bohmer ((struct usb_config_descriptor *) buf)->bDescriptorType = type; 74723cd1385SRemy Bohmer return len; 74823cd1385SRemy Bohmer } 74923cd1385SRemy Bohmer 75023cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 75123cd1385SRemy Bohmer 75223cd1385SRemy Bohmer static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags); 75323cd1385SRemy Bohmer 75423cd1385SRemy Bohmer static int 75523cd1385SRemy Bohmer set_ether_config(struct eth_dev *dev, gfp_t gfp_flags) 75623cd1385SRemy Bohmer { 75723cd1385SRemy Bohmer int result = 0; 75823cd1385SRemy Bohmer struct usb_gadget *gadget = dev->gadget; 75923cd1385SRemy Bohmer 76023cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC) 76123cd1385SRemy Bohmer /* status endpoint used for (optionally) CDC */ 76223cd1385SRemy Bohmer if (!subset_active(dev) && dev->status_ep) { 76323cd1385SRemy Bohmer dev->status = ep_desc(gadget, &hs_status_desc, 76423cd1385SRemy Bohmer &fs_status_desc); 76523cd1385SRemy Bohmer dev->status_ep->driver_data = dev; 76623cd1385SRemy Bohmer 76723cd1385SRemy Bohmer result = usb_ep_enable(dev->status_ep, dev->status); 76823cd1385SRemy Bohmer if (result != 0) { 7697de73185SVitaly Kuzmichev debug("enable %s --> %d\n", 77023cd1385SRemy Bohmer dev->status_ep->name, result); 77123cd1385SRemy Bohmer goto done; 77223cd1385SRemy Bohmer } 77323cd1385SRemy Bohmer } 77423cd1385SRemy Bohmer #endif 77523cd1385SRemy Bohmer 77623cd1385SRemy Bohmer dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc); 77723cd1385SRemy Bohmer dev->in_ep->driver_data = dev; 77823cd1385SRemy Bohmer 77923cd1385SRemy Bohmer dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc); 78023cd1385SRemy Bohmer dev->out_ep->driver_data = dev; 78123cd1385SRemy Bohmer 7826142e0aeSVitaly Kuzmichev /* 7836142e0aeSVitaly Kuzmichev * With CDC, the host isn't allowed to use these two data 78423cd1385SRemy Bohmer * endpoints in the default altsetting for the interface. 78523cd1385SRemy Bohmer * so we don't activate them yet. Reset from SET_INTERFACE. 78623cd1385SRemy Bohmer */ 78723cd1385SRemy Bohmer if (!cdc_active(dev)) { 78823cd1385SRemy Bohmer result = usb_ep_enable(dev->in_ep, dev->in); 78923cd1385SRemy Bohmer if (result != 0) { 7907de73185SVitaly Kuzmichev debug("enable %s --> %d\n", 79123cd1385SRemy Bohmer dev->in_ep->name, result); 79223cd1385SRemy Bohmer goto done; 79323cd1385SRemy Bohmer } 79423cd1385SRemy Bohmer 79523cd1385SRemy Bohmer result = usb_ep_enable(dev->out_ep, dev->out); 79623cd1385SRemy Bohmer if (result != 0) { 7977de73185SVitaly Kuzmichev debug("enable %s --> %d\n", 79823cd1385SRemy Bohmer dev->out_ep->name, result); 79923cd1385SRemy Bohmer goto done; 80023cd1385SRemy Bohmer } 80123cd1385SRemy Bohmer } 80223cd1385SRemy Bohmer 80323cd1385SRemy Bohmer done: 80423cd1385SRemy Bohmer if (result == 0) 80523cd1385SRemy Bohmer result = alloc_requests(dev, qlen(gadget), gfp_flags); 80623cd1385SRemy Bohmer 80723cd1385SRemy Bohmer /* on error, disable any endpoints */ 80823cd1385SRemy Bohmer if (result < 0) { 809d5292c16SVitaly Kuzmichev if (!subset_active(dev) && dev->status_ep) 81023cd1385SRemy Bohmer (void) usb_ep_disable(dev->status_ep); 81123cd1385SRemy Bohmer dev->status = NULL; 81223cd1385SRemy Bohmer (void) usb_ep_disable(dev->in_ep); 81323cd1385SRemy Bohmer (void) usb_ep_disable(dev->out_ep); 81423cd1385SRemy Bohmer dev->in = NULL; 81523cd1385SRemy Bohmer dev->out = NULL; 81623cd1385SRemy Bohmer } 81723cd1385SRemy Bohmer 81823cd1385SRemy Bohmer /* caller is responsible for cleanup on error */ 81923cd1385SRemy Bohmer return result; 82023cd1385SRemy Bohmer } 82123cd1385SRemy Bohmer 82223cd1385SRemy Bohmer static void eth_reset_config(struct eth_dev *dev) 82323cd1385SRemy Bohmer { 82423cd1385SRemy Bohmer if (dev->config == 0) 82523cd1385SRemy Bohmer return; 82623cd1385SRemy Bohmer 8277de73185SVitaly Kuzmichev debug("%s\n", __func__); 8287de73185SVitaly Kuzmichev 8296142e0aeSVitaly Kuzmichev /* 8306142e0aeSVitaly Kuzmichev * disable endpoints, forcing (synchronous) completion of 83123cd1385SRemy Bohmer * pending i/o. then free the requests. 83223cd1385SRemy Bohmer */ 83323cd1385SRemy Bohmer 83423cd1385SRemy Bohmer if (dev->in) { 83523cd1385SRemy Bohmer usb_ep_disable(dev->in_ep); 83623cd1385SRemy Bohmer if (dev->tx_req) { 83723cd1385SRemy Bohmer usb_ep_free_request(dev->in_ep, dev->tx_req); 83823cd1385SRemy Bohmer dev->tx_req = NULL; 83923cd1385SRemy Bohmer } 84023cd1385SRemy Bohmer } 84123cd1385SRemy Bohmer if (dev->out) { 84223cd1385SRemy Bohmer usb_ep_disable(dev->out_ep); 84323cd1385SRemy Bohmer if (dev->rx_req) { 8440129e327SVitaly Kuzmichev usb_ep_free_request(dev->out_ep, dev->rx_req); 84523cd1385SRemy Bohmer dev->rx_req = NULL; 84623cd1385SRemy Bohmer } 84723cd1385SRemy Bohmer } 8486142e0aeSVitaly Kuzmichev if (dev->status) 84923cd1385SRemy Bohmer usb_ep_disable(dev->status_ep); 8506142e0aeSVitaly Kuzmichev 85123cd1385SRemy Bohmer dev->cdc_filter = 0; 85223cd1385SRemy Bohmer dev->config = 0; 85323cd1385SRemy Bohmer } 85423cd1385SRemy Bohmer 8556142e0aeSVitaly Kuzmichev /* 8566142e0aeSVitaly Kuzmichev * change our operational config. must agree with the code 85723cd1385SRemy Bohmer * that returns config descriptors, and altsetting code. 85823cd1385SRemy Bohmer */ 8596142e0aeSVitaly Kuzmichev static int eth_set_config(struct eth_dev *dev, unsigned number, 8606142e0aeSVitaly Kuzmichev gfp_t gfp_flags) 86123cd1385SRemy Bohmer { 86223cd1385SRemy Bohmer int result = 0; 86323cd1385SRemy Bohmer struct usb_gadget *gadget = dev->gadget; 86423cd1385SRemy Bohmer 86523cd1385SRemy Bohmer if (gadget_is_sa1100(gadget) 86623cd1385SRemy Bohmer && dev->config 86723cd1385SRemy Bohmer && dev->tx_qlen != 0) { 86823cd1385SRemy Bohmer /* tx fifo is full, but we can't clear it...*/ 8697de73185SVitaly Kuzmichev error("can't change configurations"); 87023cd1385SRemy Bohmer return -ESPIPE; 87123cd1385SRemy Bohmer } 87223cd1385SRemy Bohmer eth_reset_config(dev); 87323cd1385SRemy Bohmer 87423cd1385SRemy Bohmer switch (number) { 87523cd1385SRemy Bohmer case DEV_CONFIG_VALUE: 87623cd1385SRemy Bohmer result = set_ether_config(dev, gfp_flags); 87723cd1385SRemy Bohmer break; 87823cd1385SRemy Bohmer default: 87923cd1385SRemy Bohmer result = -EINVAL; 88023cd1385SRemy Bohmer /* FALL THROUGH */ 88123cd1385SRemy Bohmer case 0: 88223cd1385SRemy Bohmer break; 88323cd1385SRemy Bohmer } 88423cd1385SRemy Bohmer 88523cd1385SRemy Bohmer if (result) { 88623cd1385SRemy Bohmer if (number) 88723cd1385SRemy Bohmer eth_reset_config(dev); 88823cd1385SRemy Bohmer usb_gadget_vbus_draw(dev->gadget, 88923cd1385SRemy Bohmer gadget_is_otg(dev->gadget) ? 8 : 100); 89023cd1385SRemy Bohmer } else { 89123cd1385SRemy Bohmer char *speed; 89223cd1385SRemy Bohmer unsigned power; 89323cd1385SRemy Bohmer 89423cd1385SRemy Bohmer power = 2 * eth_config.bMaxPower; 89523cd1385SRemy Bohmer usb_gadget_vbus_draw(dev->gadget, power); 89623cd1385SRemy Bohmer 89723cd1385SRemy Bohmer switch (gadget->speed) { 8986142e0aeSVitaly Kuzmichev case USB_SPEED_FULL: 8996142e0aeSVitaly Kuzmichev speed = "full"; break; 90023cd1385SRemy Bohmer #ifdef CONFIG_USB_GADGET_DUALSPEED 9016142e0aeSVitaly Kuzmichev case USB_SPEED_HIGH: 9026142e0aeSVitaly Kuzmichev speed = "high"; break; 90323cd1385SRemy Bohmer #endif 9046142e0aeSVitaly Kuzmichev default: 9056142e0aeSVitaly Kuzmichev speed = "?"; break; 90623cd1385SRemy Bohmer } 90723cd1385SRemy Bohmer 90823cd1385SRemy Bohmer dev->config = number; 9097de73185SVitaly Kuzmichev printf("%s speed config #%d: %d mA, %s, using %s\n", 91023cd1385SRemy Bohmer speed, number, power, driver_desc, 91123cd1385SRemy Bohmer (cdc_active(dev) ? "CDC Ethernet" 91223cd1385SRemy Bohmer : "CDC Ethernet Subset")); 91323cd1385SRemy Bohmer } 91423cd1385SRemy Bohmer return result; 91523cd1385SRemy Bohmer } 91623cd1385SRemy Bohmer 91723cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 91823cd1385SRemy Bohmer 91923cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC 92023cd1385SRemy Bohmer 9216142e0aeSVitaly Kuzmichev /* 9226142e0aeSVitaly Kuzmichev * The interrupt endpoint is used in CDC networking models (Ethernet, ATM) 92323cd1385SRemy Bohmer * only to notify the host about link status changes (which we support) or 92423cd1385SRemy Bohmer * report completion of some encapsulated command. Since 92523cd1385SRemy Bohmer * we want this CDC Ethernet code to be vendor-neutral, we don't use that 92623cd1385SRemy Bohmer * command mechanism; and only one status request is ever queued. 92723cd1385SRemy Bohmer */ 92823cd1385SRemy Bohmer static void eth_status_complete(struct usb_ep *ep, struct usb_request *req) 92923cd1385SRemy Bohmer { 93023cd1385SRemy Bohmer struct usb_cdc_notification *event = req->buf; 93123cd1385SRemy Bohmer int value = req->status; 93223cd1385SRemy Bohmer struct eth_dev *dev = ep->driver_data; 93323cd1385SRemy Bohmer 93423cd1385SRemy Bohmer /* issue the second notification if host reads the first */ 93523cd1385SRemy Bohmer if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION 93623cd1385SRemy Bohmer && value == 0) { 93723cd1385SRemy Bohmer __le32 *data = req->buf + sizeof *event; 93823cd1385SRemy Bohmer 93923cd1385SRemy Bohmer event->bmRequestType = 0xA1; 94023cd1385SRemy Bohmer event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE; 94123cd1385SRemy Bohmer event->wValue = __constant_cpu_to_le16(0); 94223cd1385SRemy Bohmer event->wIndex = __constant_cpu_to_le16(1); 94323cd1385SRemy Bohmer event->wLength = __constant_cpu_to_le16(8); 94423cd1385SRemy Bohmer 94523cd1385SRemy Bohmer /* SPEED_CHANGE data is up/down speeds in bits/sec */ 94623cd1385SRemy Bohmer data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget)); 94723cd1385SRemy Bohmer 94823cd1385SRemy Bohmer req->length = STATUS_BYTECOUNT; 94923cd1385SRemy Bohmer value = usb_ep_queue(ep, req, GFP_ATOMIC); 9507de73185SVitaly Kuzmichev debug("send SPEED_CHANGE --> %d\n", value); 95123cd1385SRemy Bohmer if (value == 0) 95223cd1385SRemy Bohmer return; 95323cd1385SRemy Bohmer } else if (value != -ECONNRESET) { 9547de73185SVitaly Kuzmichev debug("event %02x --> %d\n", 95523cd1385SRemy Bohmer event->bNotificationType, value); 95623cd1385SRemy Bohmer if (event->bNotificationType == 9576142e0aeSVitaly Kuzmichev USB_CDC_NOTIFY_SPEED_CHANGE) { 95823cd1385SRemy Bohmer l_ethdev.network_started = 1; 95923cd1385SRemy Bohmer printf("USB network up!\n"); 96023cd1385SRemy Bohmer } 96123cd1385SRemy Bohmer } 96223cd1385SRemy Bohmer req->context = NULL; 96323cd1385SRemy Bohmer } 96423cd1385SRemy Bohmer 96523cd1385SRemy Bohmer static void issue_start_status(struct eth_dev *dev) 96623cd1385SRemy Bohmer { 96723cd1385SRemy Bohmer struct usb_request *req = dev->stat_req; 96823cd1385SRemy Bohmer struct usb_cdc_notification *event; 96923cd1385SRemy Bohmer int value; 97023cd1385SRemy Bohmer 9716142e0aeSVitaly Kuzmichev /* 9726142e0aeSVitaly Kuzmichev * flush old status 97323cd1385SRemy Bohmer * 97423cd1385SRemy Bohmer * FIXME ugly idiom, maybe we'd be better with just 97523cd1385SRemy Bohmer * a "cancel the whole queue" primitive since any 97623cd1385SRemy Bohmer * unlink-one primitive has way too many error modes. 97723cd1385SRemy Bohmer * here, we "know" toggle is already clear... 97823cd1385SRemy Bohmer * 97923cd1385SRemy Bohmer * FIXME iff req->context != null just dequeue it 98023cd1385SRemy Bohmer */ 98123cd1385SRemy Bohmer usb_ep_disable(dev->status_ep); 98223cd1385SRemy Bohmer usb_ep_enable(dev->status_ep, dev->status); 98323cd1385SRemy Bohmer 9846142e0aeSVitaly Kuzmichev /* 9856142e0aeSVitaly Kuzmichev * 3.8.1 says to issue first NETWORK_CONNECTION, then 98623cd1385SRemy Bohmer * a SPEED_CHANGE. could be useful in some configs. 98723cd1385SRemy Bohmer */ 98823cd1385SRemy Bohmer event = req->buf; 98923cd1385SRemy Bohmer event->bmRequestType = 0xA1; 99023cd1385SRemy Bohmer event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION; 99123cd1385SRemy Bohmer event->wValue = __constant_cpu_to_le16(1); /* connected */ 99223cd1385SRemy Bohmer event->wIndex = __constant_cpu_to_le16(1); 99323cd1385SRemy Bohmer event->wLength = 0; 99423cd1385SRemy Bohmer 99523cd1385SRemy Bohmer req->length = sizeof *event; 99623cd1385SRemy Bohmer req->complete = eth_status_complete; 99723cd1385SRemy Bohmer req->context = dev; 99823cd1385SRemy Bohmer 99923cd1385SRemy Bohmer value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC); 100023cd1385SRemy Bohmer if (value < 0) 10017de73185SVitaly Kuzmichev debug("status buf queue --> %d\n", value); 100223cd1385SRemy Bohmer } 100323cd1385SRemy Bohmer 100423cd1385SRemy Bohmer #endif 100523cd1385SRemy Bohmer 100623cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 100723cd1385SRemy Bohmer 100823cd1385SRemy Bohmer static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req) 100923cd1385SRemy Bohmer { 101023cd1385SRemy Bohmer if (req->status || req->actual != req->length) 10117de73185SVitaly Kuzmichev debug("setup complete --> %d, %d/%d\n", 101223cd1385SRemy Bohmer req->status, req->actual, req->length); 101323cd1385SRemy Bohmer } 101423cd1385SRemy Bohmer 101523cd1385SRemy Bohmer /* 101623cd1385SRemy Bohmer * The setup() callback implements all the ep0 functionality that's not 101723cd1385SRemy Bohmer * handled lower down. CDC has a number of less-common features: 101823cd1385SRemy Bohmer * 101923cd1385SRemy Bohmer * - two interfaces: control, and ethernet data 102023cd1385SRemy Bohmer * - Ethernet data interface has two altsettings: default, and active 102123cd1385SRemy Bohmer * - class-specific descriptors for the control interface 102223cd1385SRemy Bohmer * - class-specific control requests 102323cd1385SRemy Bohmer */ 102423cd1385SRemy Bohmer static int 102523cd1385SRemy Bohmer eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) 102623cd1385SRemy Bohmer { 102723cd1385SRemy Bohmer struct eth_dev *dev = get_gadget_data(gadget); 102823cd1385SRemy Bohmer struct usb_request *req = dev->req; 102923cd1385SRemy Bohmer int value = -EOPNOTSUPP; 103023cd1385SRemy Bohmer u16 wIndex = le16_to_cpu(ctrl->wIndex); 103123cd1385SRemy Bohmer u16 wValue = le16_to_cpu(ctrl->wValue); 103223cd1385SRemy Bohmer u16 wLength = le16_to_cpu(ctrl->wLength); 103323cd1385SRemy Bohmer 10346142e0aeSVitaly Kuzmichev /* 10356142e0aeSVitaly Kuzmichev * descriptors just go into the pre-allocated ep0 buffer, 103623cd1385SRemy Bohmer * while config change events may enable network traffic. 103723cd1385SRemy Bohmer */ 103823cd1385SRemy Bohmer 10397de73185SVitaly Kuzmichev debug("%s\n", __func__); 104023cd1385SRemy Bohmer 104123cd1385SRemy Bohmer req->complete = eth_setup_complete; 104223cd1385SRemy Bohmer switch (ctrl->bRequest) { 104323cd1385SRemy Bohmer 104423cd1385SRemy Bohmer case USB_REQ_GET_DESCRIPTOR: 104523cd1385SRemy Bohmer if (ctrl->bRequestType != USB_DIR_IN) 104623cd1385SRemy Bohmer break; 104723cd1385SRemy Bohmer switch (wValue >> 8) { 104823cd1385SRemy Bohmer 104923cd1385SRemy Bohmer case USB_DT_DEVICE: 105023cd1385SRemy Bohmer value = min(wLength, (u16) sizeof device_desc); 105123cd1385SRemy Bohmer memcpy(req->buf, &device_desc, value); 105223cd1385SRemy Bohmer break; 105323cd1385SRemy Bohmer case USB_DT_DEVICE_QUALIFIER: 105423cd1385SRemy Bohmer if (!gadget_is_dualspeed(gadget)) 105523cd1385SRemy Bohmer break; 105623cd1385SRemy Bohmer value = min(wLength, (u16) sizeof dev_qualifier); 105723cd1385SRemy Bohmer memcpy(req->buf, &dev_qualifier, value); 105823cd1385SRemy Bohmer break; 105923cd1385SRemy Bohmer 106023cd1385SRemy Bohmer case USB_DT_OTHER_SPEED_CONFIG: 106123cd1385SRemy Bohmer if (!gadget_is_dualspeed(gadget)) 106223cd1385SRemy Bohmer break; 106323cd1385SRemy Bohmer /* FALLTHROUGH */ 106423cd1385SRemy Bohmer case USB_DT_CONFIG: 106523cd1385SRemy Bohmer value = config_buf(gadget, req->buf, 106623cd1385SRemy Bohmer wValue >> 8, 106723cd1385SRemy Bohmer wValue & 0xff, 106823cd1385SRemy Bohmer gadget_is_otg(gadget)); 106923cd1385SRemy Bohmer if (value >= 0) 107023cd1385SRemy Bohmer value = min(wLength, (u16) value); 107123cd1385SRemy Bohmer break; 107223cd1385SRemy Bohmer 107323cd1385SRemy Bohmer case USB_DT_STRING: 107423cd1385SRemy Bohmer value = usb_gadget_get_string(&stringtab, 107523cd1385SRemy Bohmer wValue & 0xff, req->buf); 107623cd1385SRemy Bohmer 107723cd1385SRemy Bohmer if (value >= 0) 107823cd1385SRemy Bohmer value = min(wLength, (u16) value); 107923cd1385SRemy Bohmer 108023cd1385SRemy Bohmer break; 108123cd1385SRemy Bohmer } 108223cd1385SRemy Bohmer break; 108323cd1385SRemy Bohmer 108423cd1385SRemy Bohmer case USB_REQ_SET_CONFIGURATION: 108523cd1385SRemy Bohmer if (ctrl->bRequestType != 0) 108623cd1385SRemy Bohmer break; 108723cd1385SRemy Bohmer if (gadget->a_hnp_support) 10887de73185SVitaly Kuzmichev debug("HNP available\n"); 108923cd1385SRemy Bohmer else if (gadget->a_alt_hnp_support) 10907de73185SVitaly Kuzmichev debug("HNP needs a different root port\n"); 109123cd1385SRemy Bohmer value = eth_set_config(dev, wValue, GFP_ATOMIC); 109223cd1385SRemy Bohmer break; 109323cd1385SRemy Bohmer case USB_REQ_GET_CONFIGURATION: 109423cd1385SRemy Bohmer if (ctrl->bRequestType != USB_DIR_IN) 109523cd1385SRemy Bohmer break; 109623cd1385SRemy Bohmer *(u8 *)req->buf = dev->config; 109723cd1385SRemy Bohmer value = min(wLength, (u16) 1); 109823cd1385SRemy Bohmer break; 109923cd1385SRemy Bohmer 110023cd1385SRemy Bohmer case USB_REQ_SET_INTERFACE: 110123cd1385SRemy Bohmer if (ctrl->bRequestType != USB_RECIP_INTERFACE 110223cd1385SRemy Bohmer || !dev->config 110323cd1385SRemy Bohmer || wIndex > 1) 110423cd1385SRemy Bohmer break; 110523cd1385SRemy Bohmer if (!cdc_active(dev) && wIndex != 0) 110623cd1385SRemy Bohmer break; 110723cd1385SRemy Bohmer 11086142e0aeSVitaly Kuzmichev /* 11096142e0aeSVitaly Kuzmichev * PXA hardware partially handles SET_INTERFACE; 111023cd1385SRemy Bohmer * we need to kluge around that interference. 111123cd1385SRemy Bohmer */ 111223cd1385SRemy Bohmer if (gadget_is_pxa(gadget)) { 111323cd1385SRemy Bohmer value = eth_set_config(dev, DEV_CONFIG_VALUE, 111423cd1385SRemy Bohmer GFP_ATOMIC); 111523cd1385SRemy Bohmer goto done_set_intf; 111623cd1385SRemy Bohmer } 111723cd1385SRemy Bohmer 111823cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC 111923cd1385SRemy Bohmer switch (wIndex) { 112023cd1385SRemy Bohmer case 0: /* control/master intf */ 112123cd1385SRemy Bohmer if (wValue != 0) 112223cd1385SRemy Bohmer break; 112323cd1385SRemy Bohmer if (dev->status) { 112423cd1385SRemy Bohmer usb_ep_disable(dev->status_ep); 112523cd1385SRemy Bohmer usb_ep_enable(dev->status_ep, dev->status); 112623cd1385SRemy Bohmer } 112723cd1385SRemy Bohmer value = 0; 112823cd1385SRemy Bohmer break; 112923cd1385SRemy Bohmer case 1: /* data intf */ 113023cd1385SRemy Bohmer if (wValue > 1) 113123cd1385SRemy Bohmer break; 113223cd1385SRemy Bohmer usb_ep_disable(dev->in_ep); 113323cd1385SRemy Bohmer usb_ep_disable(dev->out_ep); 113423cd1385SRemy Bohmer 11356142e0aeSVitaly Kuzmichev /* 11366142e0aeSVitaly Kuzmichev * CDC requires the data transfers not be done from 113723cd1385SRemy Bohmer * the default interface setting ... also, setting 113823cd1385SRemy Bohmer * the non-default interface resets filters etc. 113923cd1385SRemy Bohmer */ 114023cd1385SRemy Bohmer if (wValue == 1) { 114123cd1385SRemy Bohmer if (!cdc_active(dev)) 114223cd1385SRemy Bohmer break; 114323cd1385SRemy Bohmer usb_ep_enable(dev->in_ep, dev->in); 114423cd1385SRemy Bohmer usb_ep_enable(dev->out_ep, dev->out); 114523cd1385SRemy Bohmer dev->cdc_filter = DEFAULT_FILTER; 114623cd1385SRemy Bohmer if (dev->status) 114723cd1385SRemy Bohmer issue_start_status(dev); 114823cd1385SRemy Bohmer } 114923cd1385SRemy Bohmer 115023cd1385SRemy Bohmer value = 0; 115123cd1385SRemy Bohmer break; 115223cd1385SRemy Bohmer } 115323cd1385SRemy Bohmer #else 11546142e0aeSVitaly Kuzmichev /* 11556142e0aeSVitaly Kuzmichev * FIXME this is wrong, as is the assumption that 115623cd1385SRemy Bohmer * all non-PXA hardware talks real CDC ... 115723cd1385SRemy Bohmer */ 11587de73185SVitaly Kuzmichev debug("set_interface ignored!\n"); 115923cd1385SRemy Bohmer #endif /* DEV_CONFIG_CDC */ 116023cd1385SRemy Bohmer 116123cd1385SRemy Bohmer done_set_intf: 116223cd1385SRemy Bohmer break; 116323cd1385SRemy Bohmer case USB_REQ_GET_INTERFACE: 116423cd1385SRemy Bohmer if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE) 116523cd1385SRemy Bohmer || !dev->config 116623cd1385SRemy Bohmer || wIndex > 1) 116723cd1385SRemy Bohmer break; 116823cd1385SRemy Bohmer if (!(cdc_active(dev)) && wIndex != 0) 116923cd1385SRemy Bohmer break; 117023cd1385SRemy Bohmer 117123cd1385SRemy Bohmer /* for CDC, iff carrier is on, data interface is active. */ 117223cd1385SRemy Bohmer if (wIndex != 1) 117323cd1385SRemy Bohmer *(u8 *)req->buf = 0; 117423cd1385SRemy Bohmer else { 117523cd1385SRemy Bohmer /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */ 117623cd1385SRemy Bohmer /* carrier always ok ...*/ 117723cd1385SRemy Bohmer *(u8 *)req->buf = 1 ; 117823cd1385SRemy Bohmer } 117923cd1385SRemy Bohmer value = min(wLength, (u16) 1); 118023cd1385SRemy Bohmer break; 118123cd1385SRemy Bohmer 118223cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC 118323cd1385SRemy Bohmer case USB_CDC_SET_ETHERNET_PACKET_FILTER: 11846142e0aeSVitaly Kuzmichev /* 11856142e0aeSVitaly Kuzmichev * see 6.2.30: no data, wIndex = interface, 118623cd1385SRemy Bohmer * wValue = packet filter bitmap 118723cd1385SRemy Bohmer */ 118823cd1385SRemy Bohmer if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE) 118923cd1385SRemy Bohmer || !cdc_active(dev) 119023cd1385SRemy Bohmer || wLength != 0 119123cd1385SRemy Bohmer || wIndex > 1) 119223cd1385SRemy Bohmer break; 11937de73185SVitaly Kuzmichev debug("packet filter %02x\n", wValue); 119423cd1385SRemy Bohmer dev->cdc_filter = wValue; 119523cd1385SRemy Bohmer value = 0; 119623cd1385SRemy Bohmer break; 119723cd1385SRemy Bohmer 11986142e0aeSVitaly Kuzmichev /* 11996142e0aeSVitaly Kuzmichev * and potentially: 120023cd1385SRemy Bohmer * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS: 120123cd1385SRemy Bohmer * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER: 120223cd1385SRemy Bohmer * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER: 120323cd1385SRemy Bohmer * case USB_CDC_GET_ETHERNET_STATISTIC: 120423cd1385SRemy Bohmer */ 120523cd1385SRemy Bohmer 120623cd1385SRemy Bohmer #endif /* DEV_CONFIG_CDC */ 120723cd1385SRemy Bohmer 120823cd1385SRemy Bohmer default: 12097de73185SVitaly Kuzmichev debug("unknown control req%02x.%02x v%04x i%04x l%d\n", 121023cd1385SRemy Bohmer ctrl->bRequestType, ctrl->bRequest, 121123cd1385SRemy Bohmer wValue, wIndex, wLength); 121223cd1385SRemy Bohmer } 121323cd1385SRemy Bohmer 121423cd1385SRemy Bohmer /* respond with data transfer before status phase? */ 121523cd1385SRemy Bohmer if (value >= 0) { 12167de73185SVitaly Kuzmichev debug("respond with data transfer before status phase\n"); 121723cd1385SRemy Bohmer req->length = value; 121823cd1385SRemy Bohmer req->zero = value < wLength 121923cd1385SRemy Bohmer && (value % gadget->ep0->maxpacket) == 0; 122023cd1385SRemy Bohmer value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC); 122123cd1385SRemy Bohmer if (value < 0) { 12227de73185SVitaly Kuzmichev debug("ep_queue --> %d\n", value); 122323cd1385SRemy Bohmer req->status = 0; 122423cd1385SRemy Bohmer eth_setup_complete(gadget->ep0, req); 122523cd1385SRemy Bohmer } 122623cd1385SRemy Bohmer } 122723cd1385SRemy Bohmer 122823cd1385SRemy Bohmer /* host either stalls (value < 0) or reports success */ 122923cd1385SRemy Bohmer return value; 123023cd1385SRemy Bohmer } 123123cd1385SRemy Bohmer 123223cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 123323cd1385SRemy Bohmer 123423cd1385SRemy Bohmer static void rx_complete(struct usb_ep *ep, struct usb_request *req); 123523cd1385SRemy Bohmer 12366142e0aeSVitaly Kuzmichev static int rx_submit(struct eth_dev *dev, struct usb_request *req, 123723cd1385SRemy Bohmer gfp_t gfp_flags) 123823cd1385SRemy Bohmer { 123923cd1385SRemy Bohmer int retval = -ENOMEM; 124023cd1385SRemy Bohmer size_t size; 124123cd1385SRemy Bohmer 12426142e0aeSVitaly Kuzmichev /* 12436142e0aeSVitaly Kuzmichev * Padding up to RX_EXTRA handles minor disagreements with host. 124423cd1385SRemy Bohmer * Normally we use the USB "terminate on short read" convention; 124523cd1385SRemy Bohmer * so allow up to (N*maxpacket), since that memory is normally 124623cd1385SRemy Bohmer * already allocated. Some hardware doesn't deal well with short 124723cd1385SRemy Bohmer * reads (e.g. DMA must be N*maxpacket), so for now don't trim a 124823cd1385SRemy Bohmer * byte off the end (to force hardware errors on overflow). 124923cd1385SRemy Bohmer */ 125023cd1385SRemy Bohmer 12517de73185SVitaly Kuzmichev debug("%s\n", __func__); 125223cd1385SRemy Bohmer 125323cd1385SRemy Bohmer size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA); 125423cd1385SRemy Bohmer size += dev->out_ep->maxpacket - 1; 125523cd1385SRemy Bohmer size -= size % dev->out_ep->maxpacket; 125623cd1385SRemy Bohmer 12576142e0aeSVitaly Kuzmichev /* 12586142e0aeSVitaly Kuzmichev * Some platforms perform better when IP packets are aligned, 125923cd1385SRemy Bohmer * but on at least one, checksumming fails otherwise. 126023cd1385SRemy Bohmer */ 126123cd1385SRemy Bohmer 126223cd1385SRemy Bohmer req->buf = (u8 *) NetRxPackets[0]; 126323cd1385SRemy Bohmer req->length = size; 126423cd1385SRemy Bohmer req->complete = rx_complete; 126523cd1385SRemy Bohmer 126623cd1385SRemy Bohmer retval = usb_ep_queue(dev->out_ep, req, gfp_flags); 126723cd1385SRemy Bohmer 12686142e0aeSVitaly Kuzmichev if (retval) 12697de73185SVitaly Kuzmichev error("rx submit --> %d", retval); 12706142e0aeSVitaly Kuzmichev 127123cd1385SRemy Bohmer return retval; 127223cd1385SRemy Bohmer } 127323cd1385SRemy Bohmer 127423cd1385SRemy Bohmer static void rx_complete(struct usb_ep *ep, struct usb_request *req) 127523cd1385SRemy Bohmer { 127623cd1385SRemy Bohmer struct eth_dev *dev = ep->driver_data; 127723cd1385SRemy Bohmer 12787de73185SVitaly Kuzmichev debug("%s: status %d\n", __func__, req->status); 1279*c85d70efSVitaly Kuzmichev switch (req->status) { 1280*c85d70efSVitaly Kuzmichev /* normal completion */ 1281*c85d70efSVitaly Kuzmichev case 0: 1282*c85d70efSVitaly Kuzmichev dev->stats.rx_packets++; 1283*c85d70efSVitaly Kuzmichev dev->stats.rx_bytes += req->length; 1284*c85d70efSVitaly Kuzmichev break; 1285*c85d70efSVitaly Kuzmichev 1286*c85d70efSVitaly Kuzmichev /* software-driven interface shutdown */ 1287*c85d70efSVitaly Kuzmichev case -ECONNRESET: /* unlink */ 1288*c85d70efSVitaly Kuzmichev case -ESHUTDOWN: /* disconnect etc */ 1289*c85d70efSVitaly Kuzmichev /* for hardware automagic (such as pxa) */ 1290*c85d70efSVitaly Kuzmichev case -ECONNABORTED: /* endpoint reset */ 1291*c85d70efSVitaly Kuzmichev break; 1292*c85d70efSVitaly Kuzmichev 1293*c85d70efSVitaly Kuzmichev /* data overrun */ 1294*c85d70efSVitaly Kuzmichev case -EOVERFLOW: 1295*c85d70efSVitaly Kuzmichev dev->stats.rx_over_errors++; 1296*c85d70efSVitaly Kuzmichev /* FALLTHROUGH */ 1297*c85d70efSVitaly Kuzmichev default: 1298*c85d70efSVitaly Kuzmichev dev->stats.rx_errors++; 1299*c85d70efSVitaly Kuzmichev break; 1300*c85d70efSVitaly Kuzmichev } 130123cd1385SRemy Bohmer 130223cd1385SRemy Bohmer packet_received = 1; 130323cd1385SRemy Bohmer } 130423cd1385SRemy Bohmer 130523cd1385SRemy Bohmer static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags) 130623cd1385SRemy Bohmer { 130723cd1385SRemy Bohmer 130823cd1385SRemy Bohmer dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0); 130923cd1385SRemy Bohmer 131023cd1385SRemy Bohmer if (!dev->tx_req) 1311ac5d32d1SVitaly Kuzmichev goto fail1; 131223cd1385SRemy Bohmer 131323cd1385SRemy Bohmer dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0); 131423cd1385SRemy Bohmer 131523cd1385SRemy Bohmer if (!dev->rx_req) 1316ac5d32d1SVitaly Kuzmichev goto fail2; 131723cd1385SRemy Bohmer 131823cd1385SRemy Bohmer return 0; 131923cd1385SRemy Bohmer 1320ac5d32d1SVitaly Kuzmichev fail2: 1321ac5d32d1SVitaly Kuzmichev usb_ep_free_request(dev->in_ep, dev->tx_req); 1322ac5d32d1SVitaly Kuzmichev fail1: 13237de73185SVitaly Kuzmichev error("can't alloc requests"); 132423cd1385SRemy Bohmer return -1; 132523cd1385SRemy Bohmer } 132623cd1385SRemy Bohmer 132723cd1385SRemy Bohmer static void tx_complete(struct usb_ep *ep, struct usb_request *req) 132823cd1385SRemy Bohmer { 1329*c85d70efSVitaly Kuzmichev struct eth_dev *dev = ep->driver_data; 1330*c85d70efSVitaly Kuzmichev 13317de73185SVitaly Kuzmichev debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok"); 1332*c85d70efSVitaly Kuzmichev switch (req->status) { 1333*c85d70efSVitaly Kuzmichev default: 1334*c85d70efSVitaly Kuzmichev dev->stats.tx_errors++; 1335*c85d70efSVitaly Kuzmichev debug("tx err %d\n", req->status); 1336*c85d70efSVitaly Kuzmichev /* FALLTHROUGH */ 1337*c85d70efSVitaly Kuzmichev case -ECONNRESET: /* unlink */ 1338*c85d70efSVitaly Kuzmichev case -ESHUTDOWN: /* disconnect etc */ 1339*c85d70efSVitaly Kuzmichev break; 1340*c85d70efSVitaly Kuzmichev case 0: 1341*c85d70efSVitaly Kuzmichev dev->stats.tx_bytes += req->length; 1342*c85d70efSVitaly Kuzmichev } 1343*c85d70efSVitaly Kuzmichev dev->stats.tx_packets++; 1344*c85d70efSVitaly Kuzmichev 134523cd1385SRemy Bohmer packet_sent = 1; 134623cd1385SRemy Bohmer } 134723cd1385SRemy Bohmer 134823cd1385SRemy Bohmer static inline int eth_is_promisc(struct eth_dev *dev) 134923cd1385SRemy Bohmer { 135023cd1385SRemy Bohmer /* no filters for the CDC subset; always promisc */ 135123cd1385SRemy Bohmer if (subset_active(dev)) 135223cd1385SRemy Bohmer return 1; 135323cd1385SRemy Bohmer return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS; 135423cd1385SRemy Bohmer } 135523cd1385SRemy Bohmer 135623cd1385SRemy Bohmer #if 0 135723cd1385SRemy Bohmer static int eth_start_xmit (struct sk_buff *skb, struct net_device *net) 135823cd1385SRemy Bohmer { 135923cd1385SRemy Bohmer struct eth_dev *dev = netdev_priv(net); 136023cd1385SRemy Bohmer int length = skb->len; 136123cd1385SRemy Bohmer int retval; 136223cd1385SRemy Bohmer struct usb_request *req = NULL; 136323cd1385SRemy Bohmer unsigned long flags; 136423cd1385SRemy Bohmer 136523cd1385SRemy Bohmer /* apply outgoing CDC or RNDIS filters */ 136623cd1385SRemy Bohmer if (!eth_is_promisc (dev)) { 136723cd1385SRemy Bohmer u8 *dest = skb->data; 136823cd1385SRemy Bohmer 136923cd1385SRemy Bohmer if (is_multicast_ether_addr(dest)) { 137023cd1385SRemy Bohmer u16 type; 137123cd1385SRemy Bohmer 137223cd1385SRemy Bohmer /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host 137323cd1385SRemy Bohmer * SET_ETHERNET_MULTICAST_FILTERS requests 137423cd1385SRemy Bohmer */ 137523cd1385SRemy Bohmer if (is_broadcast_ether_addr(dest)) 137623cd1385SRemy Bohmer type = USB_CDC_PACKET_TYPE_BROADCAST; 137723cd1385SRemy Bohmer else 137823cd1385SRemy Bohmer type = USB_CDC_PACKET_TYPE_ALL_MULTICAST; 137923cd1385SRemy Bohmer if (!(dev->cdc_filter & type)) { 138023cd1385SRemy Bohmer dev_kfree_skb_any (skb); 138123cd1385SRemy Bohmer return 0; 138223cd1385SRemy Bohmer } 138323cd1385SRemy Bohmer } 138423cd1385SRemy Bohmer /* ignores USB_CDC_PACKET_TYPE_DIRECTED */ 138523cd1385SRemy Bohmer } 138623cd1385SRemy Bohmer 138723cd1385SRemy Bohmer spin_lock_irqsave(&dev->req_lock, flags); 138823cd1385SRemy Bohmer /* 138923cd1385SRemy Bohmer * this freelist can be empty if an interrupt triggered disconnect() 139023cd1385SRemy Bohmer * and reconfigured the gadget (shutting down this queue) after the 139123cd1385SRemy Bohmer * network stack decided to xmit but before we got the spinlock. 139223cd1385SRemy Bohmer */ 139323cd1385SRemy Bohmer if (list_empty(&dev->tx_reqs)) { 139423cd1385SRemy Bohmer spin_unlock_irqrestore(&dev->req_lock, flags); 139523cd1385SRemy Bohmer return 1; 139623cd1385SRemy Bohmer } 139723cd1385SRemy Bohmer 139823cd1385SRemy Bohmer req = container_of (dev->tx_reqs.next, struct usb_request, list); 139923cd1385SRemy Bohmer list_del (&req->list); 140023cd1385SRemy Bohmer 140123cd1385SRemy Bohmer /* temporarily stop TX queue when the freelist empties */ 140223cd1385SRemy Bohmer if (list_empty (&dev->tx_reqs)) 140323cd1385SRemy Bohmer netif_stop_queue (net); 140423cd1385SRemy Bohmer spin_unlock_irqrestore(&dev->req_lock, flags); 140523cd1385SRemy Bohmer 140623cd1385SRemy Bohmer /* no buffer copies needed, unless the network stack did it 140723cd1385SRemy Bohmer * or the hardware can't use skb buffers. 140823cd1385SRemy Bohmer * or there's not enough space for any RNDIS headers we need 140923cd1385SRemy Bohmer */ 141023cd1385SRemy Bohmer if (rndis_active(dev)) { 141123cd1385SRemy Bohmer struct sk_buff *skb_rndis; 141223cd1385SRemy Bohmer 141323cd1385SRemy Bohmer skb_rndis = skb_realloc_headroom (skb, 141423cd1385SRemy Bohmer sizeof (struct rndis_packet_msg_type)); 141523cd1385SRemy Bohmer if (!skb_rndis) 141623cd1385SRemy Bohmer goto drop; 141723cd1385SRemy Bohmer 141823cd1385SRemy Bohmer dev_kfree_skb_any (skb); 141923cd1385SRemy Bohmer skb = skb_rndis; 142023cd1385SRemy Bohmer rndis_add_hdr (skb); 142123cd1385SRemy Bohmer length = skb->len; 142223cd1385SRemy Bohmer } 142323cd1385SRemy Bohmer req->buf = skb->data; 142423cd1385SRemy Bohmer req->context = skb; 142523cd1385SRemy Bohmer req->complete = tx_complete; 142623cd1385SRemy Bohmer 142723cd1385SRemy Bohmer /* use zlp framing on tx for strict CDC-Ether conformance, 142823cd1385SRemy Bohmer * though any robust network rx path ignores extra padding. 142923cd1385SRemy Bohmer * and some hardware doesn't like to write zlps. 143023cd1385SRemy Bohmer */ 143123cd1385SRemy Bohmer req->zero = 1; 143223cd1385SRemy Bohmer if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0) 143323cd1385SRemy Bohmer length++; 143423cd1385SRemy Bohmer 143523cd1385SRemy Bohmer req->length = length; 143623cd1385SRemy Bohmer 143723cd1385SRemy Bohmer /* throttle highspeed IRQ rate back slightly */ 143823cd1385SRemy Bohmer if (gadget_is_dualspeed(dev->gadget)) 143923cd1385SRemy Bohmer req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH) 144023cd1385SRemy Bohmer ? ((atomic_read(&dev->tx_qlen) % qmult) != 0) 144123cd1385SRemy Bohmer : 0; 144223cd1385SRemy Bohmer 144323cd1385SRemy Bohmer retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC); 144423cd1385SRemy Bohmer switch (retval) { 144523cd1385SRemy Bohmer default: 144623cd1385SRemy Bohmer DEBUG (dev, "tx queue err %d\n", retval); 144723cd1385SRemy Bohmer break; 144823cd1385SRemy Bohmer case 0: 144923cd1385SRemy Bohmer net->trans_start = jiffies; 145023cd1385SRemy Bohmer atomic_inc (&dev->tx_qlen); 145123cd1385SRemy Bohmer } 145223cd1385SRemy Bohmer 145323cd1385SRemy Bohmer if (retval) { 145423cd1385SRemy Bohmer drop: 145523cd1385SRemy Bohmer dev->stats.tx_dropped++; 145623cd1385SRemy Bohmer dev_kfree_skb_any (skb); 145723cd1385SRemy Bohmer spin_lock_irqsave(&dev->req_lock, flags); 145823cd1385SRemy Bohmer if (list_empty (&dev->tx_reqs)) 145923cd1385SRemy Bohmer netif_start_queue (net); 146023cd1385SRemy Bohmer list_add (&req->list, &dev->tx_reqs); 146123cd1385SRemy Bohmer spin_unlock_irqrestore(&dev->req_lock, flags); 146223cd1385SRemy Bohmer } 146323cd1385SRemy Bohmer return 0; 146423cd1385SRemy Bohmer } 146523cd1385SRemy Bohmer 146623cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 146723cd1385SRemy Bohmer #endif 146823cd1385SRemy Bohmer 146923cd1385SRemy Bohmer static void eth_unbind(struct usb_gadget *gadget) 147023cd1385SRemy Bohmer { 147123cd1385SRemy Bohmer struct eth_dev *dev = get_gadget_data(gadget); 147223cd1385SRemy Bohmer 14737de73185SVitaly Kuzmichev debug("%s...\n", __func__); 147423cd1385SRemy Bohmer 14750129e327SVitaly Kuzmichev /* we've already been disconnected ... no i/o is active */ 14760129e327SVitaly Kuzmichev if (dev->req) { 14770129e327SVitaly Kuzmichev usb_ep_free_request(gadget->ep0, dev->req); 14780129e327SVitaly Kuzmichev dev->req = NULL; 14790129e327SVitaly Kuzmichev } 148023cd1385SRemy Bohmer if (dev->stat_req) { 148123cd1385SRemy Bohmer usb_ep_free_request(dev->status_ep, dev->stat_req); 148223cd1385SRemy Bohmer dev->stat_req = NULL; 148323cd1385SRemy Bohmer } 148423cd1385SRemy Bohmer 148523cd1385SRemy Bohmer if (dev->tx_req) { 148623cd1385SRemy Bohmer usb_ep_free_request(dev->in_ep, dev->tx_req); 148723cd1385SRemy Bohmer dev->tx_req = NULL; 148823cd1385SRemy Bohmer } 148923cd1385SRemy Bohmer 149023cd1385SRemy Bohmer if (dev->rx_req) { 14910129e327SVitaly Kuzmichev usb_ep_free_request(dev->out_ep, dev->rx_req); 149223cd1385SRemy Bohmer dev->rx_req = NULL; 149323cd1385SRemy Bohmer } 149423cd1385SRemy Bohmer 149523cd1385SRemy Bohmer /* unregister_netdev (dev->net);*/ 149623cd1385SRemy Bohmer /* free_netdev(dev->net);*/ 149723cd1385SRemy Bohmer 1498988ee3e3SLei Wen dev->gadget = NULL; 149923cd1385SRemy Bohmer set_gadget_data(gadget, NULL); 150023cd1385SRemy Bohmer } 150123cd1385SRemy Bohmer 150223cd1385SRemy Bohmer static void eth_disconnect(struct usb_gadget *gadget) 150323cd1385SRemy Bohmer { 150423cd1385SRemy Bohmer eth_reset_config(get_gadget_data(gadget)); 150523cd1385SRemy Bohmer } 150623cd1385SRemy Bohmer 150723cd1385SRemy Bohmer static void eth_suspend(struct usb_gadget *gadget) 150823cd1385SRemy Bohmer { 150923cd1385SRemy Bohmer /* Not used */ 151023cd1385SRemy Bohmer } 151123cd1385SRemy Bohmer 151223cd1385SRemy Bohmer static void eth_resume(struct usb_gadget *gadget) 151323cd1385SRemy Bohmer { 151423cd1385SRemy Bohmer /* Not used */ 151523cd1385SRemy Bohmer } 151623cd1385SRemy Bohmer 151723cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 151823cd1385SRemy Bohmer 151923cd1385SRemy Bohmer static int is_eth_addr_valid(char *str) 152023cd1385SRemy Bohmer { 152123cd1385SRemy Bohmer if (strlen(str) == 17) { 152223cd1385SRemy Bohmer int i; 152323cd1385SRemy Bohmer char *p, *q; 152423cd1385SRemy Bohmer uchar ea[6]; 152523cd1385SRemy Bohmer 152623cd1385SRemy Bohmer /* see if it looks like an ethernet address */ 152723cd1385SRemy Bohmer 152823cd1385SRemy Bohmer p = str; 152923cd1385SRemy Bohmer 153023cd1385SRemy Bohmer for (i = 0; i < 6; i++) { 153123cd1385SRemy Bohmer char term = (i == 5 ? '\0' : ':'); 153223cd1385SRemy Bohmer 153323cd1385SRemy Bohmer ea[i] = simple_strtol(p, &q, 16); 153423cd1385SRemy Bohmer 153523cd1385SRemy Bohmer if ((q - p) != 2 || *q++ != term) 153623cd1385SRemy Bohmer break; 153723cd1385SRemy Bohmer 153823cd1385SRemy Bohmer p = q; 153923cd1385SRemy Bohmer } 154023cd1385SRemy Bohmer 154123cd1385SRemy Bohmer if (i == 6) /* it looks ok */ 154223cd1385SRemy Bohmer return 1; 154323cd1385SRemy Bohmer } 154423cd1385SRemy Bohmer return 0; 154523cd1385SRemy Bohmer } 154623cd1385SRemy Bohmer 154723cd1385SRemy Bohmer static u8 nibble(unsigned char c) 154823cd1385SRemy Bohmer { 154923cd1385SRemy Bohmer if (likely(isdigit(c))) 155023cd1385SRemy Bohmer return c - '0'; 155123cd1385SRemy Bohmer c = toupper(c); 155223cd1385SRemy Bohmer if (likely(isxdigit(c))) 155323cd1385SRemy Bohmer return 10 + c - 'A'; 155423cd1385SRemy Bohmer return 0; 155523cd1385SRemy Bohmer } 155623cd1385SRemy Bohmer 155723cd1385SRemy Bohmer static int get_ether_addr(const char *str, u8 *dev_addr) 155823cd1385SRemy Bohmer { 155923cd1385SRemy Bohmer if (str) { 156023cd1385SRemy Bohmer unsigned i; 156123cd1385SRemy Bohmer 156223cd1385SRemy Bohmer for (i = 0; i < 6; i++) { 156323cd1385SRemy Bohmer unsigned char num; 156423cd1385SRemy Bohmer 156523cd1385SRemy Bohmer if ((*str == '.') || (*str == ':')) 156623cd1385SRemy Bohmer str++; 156723cd1385SRemy Bohmer num = nibble(*str++) << 4; 156823cd1385SRemy Bohmer num |= (nibble(*str++)); 156923cd1385SRemy Bohmer dev_addr[i] = num; 157023cd1385SRemy Bohmer } 157123cd1385SRemy Bohmer if (is_valid_ether_addr(dev_addr)) 157223cd1385SRemy Bohmer return 0; 157323cd1385SRemy Bohmer } 157423cd1385SRemy Bohmer return 1; 157523cd1385SRemy Bohmer } 157623cd1385SRemy Bohmer 157723cd1385SRemy Bohmer static int eth_bind(struct usb_gadget *gadget) 157823cd1385SRemy Bohmer { 157923cd1385SRemy Bohmer struct eth_dev *dev = &l_ethdev; 158023cd1385SRemy Bohmer u8 cdc = 1, zlp = 1; 158123cd1385SRemy Bohmer struct usb_ep *in_ep, *out_ep, *status_ep = NULL; 158223cd1385SRemy Bohmer int gcnum; 158323cd1385SRemy Bohmer u8 tmp[7]; 158423cd1385SRemy Bohmer 158523cd1385SRemy Bohmer /* these flags are only ever cleared; compiler take note */ 158623cd1385SRemy Bohmer #ifndef DEV_CONFIG_CDC 158723cd1385SRemy Bohmer cdc = 0; 158823cd1385SRemy Bohmer #endif 15896142e0aeSVitaly Kuzmichev /* 15906142e0aeSVitaly Kuzmichev * Because most host side USB stacks handle CDC Ethernet, that 159123cd1385SRemy Bohmer * standard protocol is _strongly_ preferred for interop purposes. 159223cd1385SRemy Bohmer * (By everyone except Microsoft.) 159323cd1385SRemy Bohmer */ 159423cd1385SRemy Bohmer if (gadget_is_pxa(gadget)) { 159523cd1385SRemy Bohmer /* pxa doesn't support altsettings */ 159623cd1385SRemy Bohmer cdc = 0; 159723cd1385SRemy Bohmer } else if (gadget_is_musbhdrc(gadget)) { 159823cd1385SRemy Bohmer /* reduce tx dma overhead by avoiding special cases */ 159923cd1385SRemy Bohmer zlp = 0; 160023cd1385SRemy Bohmer } else if (gadget_is_sh(gadget)) { 160123cd1385SRemy Bohmer /* sh doesn't support multiple interfaces or configs */ 160223cd1385SRemy Bohmer cdc = 0; 160323cd1385SRemy Bohmer } else if (gadget_is_sa1100(gadget)) { 160423cd1385SRemy Bohmer /* hardware can't write zlps */ 160523cd1385SRemy Bohmer zlp = 0; 16066142e0aeSVitaly Kuzmichev /* 16076142e0aeSVitaly Kuzmichev * sa1100 CAN do CDC, without status endpoint ... we use 160823cd1385SRemy Bohmer * non-CDC to be compatible with ARM Linux-2.4 "usb-eth". 160923cd1385SRemy Bohmer */ 161023cd1385SRemy Bohmer cdc = 0; 161123cd1385SRemy Bohmer } 161223cd1385SRemy Bohmer 161323cd1385SRemy Bohmer gcnum = usb_gadget_controller_number(gadget); 161423cd1385SRemy Bohmer if (gcnum >= 0) 161523cd1385SRemy Bohmer device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum); 161623cd1385SRemy Bohmer else { 16176142e0aeSVitaly Kuzmichev /* 16186142e0aeSVitaly Kuzmichev * can't assume CDC works. don't want to default to 161923cd1385SRemy Bohmer * anything less functional on CDC-capable hardware, 162023cd1385SRemy Bohmer * so we fail in this case. 162123cd1385SRemy Bohmer */ 16227de73185SVitaly Kuzmichev error("controller '%s' not recognized", 162323cd1385SRemy Bohmer gadget->name); 162423cd1385SRemy Bohmer return -ENODEV; 162523cd1385SRemy Bohmer } 162623cd1385SRemy Bohmer 16276142e0aeSVitaly Kuzmichev /* 16286142e0aeSVitaly Kuzmichev * CDC subset ... recognized by Linux since 2.4.10, but Windows 162923cd1385SRemy Bohmer * drivers aren't widely available. (That may be improved by 163023cd1385SRemy Bohmer * supporting one submode of the "SAFE" variant of MDLM.) 163123cd1385SRemy Bohmer */ 163223cd1385SRemy Bohmer if (!cdc) { 163323cd1385SRemy Bohmer device_desc.idVendor = 163423cd1385SRemy Bohmer __constant_cpu_to_le16(SIMPLE_VENDOR_NUM); 163523cd1385SRemy Bohmer device_desc.idProduct = 163623cd1385SRemy Bohmer __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM); 163723cd1385SRemy Bohmer } 163823cd1385SRemy Bohmer 163923cd1385SRemy Bohmer /* support optional vendor/distro customization */ 164023cd1385SRemy Bohmer #if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID) 164123cd1385SRemy Bohmer device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID); 164223cd1385SRemy Bohmer device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID); 164323cd1385SRemy Bohmer #endif 164423cd1385SRemy Bohmer if (bcdDevice) 164523cd1385SRemy Bohmer device_desc.bcdDevice = cpu_to_le16(bcdDevice); 164623cd1385SRemy Bohmer if (iManufacturer) 16472e12abe6SVitaly Kuzmichev strlcpy(manufacturer, iManufacturer, sizeof manufacturer); 164823cd1385SRemy Bohmer if (iProduct) 16492e12abe6SVitaly Kuzmichev strlcpy(product_desc, iProduct, sizeof product_desc); 165023cd1385SRemy Bohmer if (iSerialNumber) { 165123cd1385SRemy Bohmer device_desc.iSerialNumber = STRING_SERIALNUMBER, 16522e12abe6SVitaly Kuzmichev strlcpy(serial_number, iSerialNumber, sizeof serial_number); 165323cd1385SRemy Bohmer } 165423cd1385SRemy Bohmer 165523cd1385SRemy Bohmer /* all we really need is bulk IN/OUT */ 165623cd1385SRemy Bohmer usb_ep_autoconfig_reset(gadget); 165723cd1385SRemy Bohmer in_ep = usb_ep_autoconfig(gadget, &fs_source_desc); 165823cd1385SRemy Bohmer if (!in_ep) { 165923cd1385SRemy Bohmer autoconf_fail: 16607de73185SVitaly Kuzmichev error("can't autoconfigure on %s\n", 166123cd1385SRemy Bohmer gadget->name); 166223cd1385SRemy Bohmer return -ENODEV; 166323cd1385SRemy Bohmer } 166423cd1385SRemy Bohmer in_ep->driver_data = in_ep; /* claim */ 166523cd1385SRemy Bohmer 166623cd1385SRemy Bohmer out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc); 166723cd1385SRemy Bohmer if (!out_ep) 166823cd1385SRemy Bohmer goto autoconf_fail; 166923cd1385SRemy Bohmer out_ep->driver_data = out_ep; /* claim */ 167023cd1385SRemy Bohmer 167123cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC) 16726142e0aeSVitaly Kuzmichev /* 16736142e0aeSVitaly Kuzmichev * CDC Ethernet control interface doesn't require a status endpoint. 167423cd1385SRemy Bohmer * Since some hosts expect one, try to allocate one anyway. 167523cd1385SRemy Bohmer */ 167623cd1385SRemy Bohmer if (cdc) { 167723cd1385SRemy Bohmer status_ep = usb_ep_autoconfig(gadget, &fs_status_desc); 167823cd1385SRemy Bohmer if (status_ep) { 167923cd1385SRemy Bohmer status_ep->driver_data = status_ep; /* claim */ 168023cd1385SRemy Bohmer } else if (cdc) { 168123cd1385SRemy Bohmer control_intf.bNumEndpoints = 0; 168223cd1385SRemy Bohmer /* FIXME remove endpoint from descriptor list */ 168323cd1385SRemy Bohmer } 168423cd1385SRemy Bohmer } 168523cd1385SRemy Bohmer #endif 168623cd1385SRemy Bohmer 168723cd1385SRemy Bohmer /* one config: cdc, else minimal subset */ 168823cd1385SRemy Bohmer if (!cdc) { 168923cd1385SRemy Bohmer eth_config.bNumInterfaces = 1; 169023cd1385SRemy Bohmer eth_config.iConfiguration = STRING_SUBSET; 169123cd1385SRemy Bohmer 16926142e0aeSVitaly Kuzmichev /* 16936142e0aeSVitaly Kuzmichev * use functions to set these up, in case we're built to work 169423cd1385SRemy Bohmer * with multiple controllers and must override CDC Ethernet. 169523cd1385SRemy Bohmer */ 169623cd1385SRemy Bohmer fs_subset_descriptors(); 169723cd1385SRemy Bohmer hs_subset_descriptors(); 169823cd1385SRemy Bohmer } 169923cd1385SRemy Bohmer 170023cd1385SRemy Bohmer device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket; 170123cd1385SRemy Bohmer usb_gadget_set_selfpowered(gadget); 170223cd1385SRemy Bohmer 170323cd1385SRemy Bohmer if (gadget_is_dualspeed(gadget)) { 170423cd1385SRemy Bohmer if (!cdc) 170523cd1385SRemy Bohmer dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC; 170623cd1385SRemy Bohmer 170723cd1385SRemy Bohmer /* assumes ep0 uses the same value for both speeds ... */ 170823cd1385SRemy Bohmer dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0; 170923cd1385SRemy Bohmer 171023cd1385SRemy Bohmer /* and that all endpoints are dual-speed */ 171123cd1385SRemy Bohmer hs_source_desc.bEndpointAddress = 171223cd1385SRemy Bohmer fs_source_desc.bEndpointAddress; 171323cd1385SRemy Bohmer hs_sink_desc.bEndpointAddress = 171423cd1385SRemy Bohmer fs_sink_desc.bEndpointAddress; 171523cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC) 171623cd1385SRemy Bohmer if (status_ep) 171723cd1385SRemy Bohmer hs_status_desc.bEndpointAddress = 171823cd1385SRemy Bohmer fs_status_desc.bEndpointAddress; 171923cd1385SRemy Bohmer #endif 172023cd1385SRemy Bohmer } 172123cd1385SRemy Bohmer 172223cd1385SRemy Bohmer if (gadget_is_otg(gadget)) { 172323cd1385SRemy Bohmer otg_descriptor.bmAttributes |= USB_OTG_HNP, 172423cd1385SRemy Bohmer eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP; 172523cd1385SRemy Bohmer eth_config.bMaxPower = 4; 172623cd1385SRemy Bohmer } 172723cd1385SRemy Bohmer 172823cd1385SRemy Bohmer dev->net = &l_netdev; 172923cd1385SRemy Bohmer 173023cd1385SRemy Bohmer dev->cdc = cdc; 173123cd1385SRemy Bohmer dev->zlp = zlp; 173223cd1385SRemy Bohmer 173323cd1385SRemy Bohmer dev->in_ep = in_ep; 173423cd1385SRemy Bohmer dev->out_ep = out_ep; 173523cd1385SRemy Bohmer dev->status_ep = status_ep; 173623cd1385SRemy Bohmer 17376142e0aeSVitaly Kuzmichev /* 17386142e0aeSVitaly Kuzmichev * Module params for these addresses should come from ID proms. 173923cd1385SRemy Bohmer * The host side address is used with CDC, and commonly 174023cd1385SRemy Bohmer * ends up in a persistent config database. It's not clear if 174123cd1385SRemy Bohmer * host side code for the SAFE thing cares -- its original BLAN 174223cd1385SRemy Bohmer * thing didn't, Sharp never assigned those addresses on Zaurii. 174323cd1385SRemy Bohmer */ 174423cd1385SRemy Bohmer get_ether_addr(dev_addr, dev->net->enetaddr); 174523cd1385SRemy Bohmer 174623cd1385SRemy Bohmer memset(tmp, 0, sizeof(tmp)); 174723cd1385SRemy Bohmer memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr)); 174823cd1385SRemy Bohmer 174923cd1385SRemy Bohmer get_ether_addr(host_addr, dev->host_mac); 175023cd1385SRemy Bohmer 175123cd1385SRemy Bohmer sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X", 175223cd1385SRemy Bohmer dev->host_mac[0], dev->host_mac[1], 175323cd1385SRemy Bohmer dev->host_mac[2], dev->host_mac[3], 175423cd1385SRemy Bohmer dev->host_mac[4], dev->host_mac[5]); 175523cd1385SRemy Bohmer 17567de73185SVitaly Kuzmichev printf("using %s, OUT %s IN %s%s%s\n", gadget->name, 175723cd1385SRemy Bohmer out_ep->name, in_ep->name, 175823cd1385SRemy Bohmer status_ep ? " STATUS " : "", 175923cd1385SRemy Bohmer status_ep ? status_ep->name : "" 176023cd1385SRemy Bohmer ); 17617de73185SVitaly Kuzmichev printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n", 176223cd1385SRemy Bohmer dev->net->enetaddr[0], dev->net->enetaddr[1], 176323cd1385SRemy Bohmer dev->net->enetaddr[2], dev->net->enetaddr[3], 176423cd1385SRemy Bohmer dev->net->enetaddr[4], dev->net->enetaddr[5]); 176523cd1385SRemy Bohmer 176623cd1385SRemy Bohmer if (cdc) { 17677de73185SVitaly Kuzmichev printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n", 176823cd1385SRemy Bohmer dev->host_mac[0], dev->host_mac[1], 176923cd1385SRemy Bohmer dev->host_mac[2], dev->host_mac[3], 177023cd1385SRemy Bohmer dev->host_mac[4], dev->host_mac[5]); 177123cd1385SRemy Bohmer } 177223cd1385SRemy Bohmer 17736142e0aeSVitaly Kuzmichev /* 17746142e0aeSVitaly Kuzmichev * use PKTSIZE (or aligned... from u-boot) and set 17756142e0aeSVitaly Kuzmichev * wMaxSegmentSize accordingly 17766142e0aeSVitaly Kuzmichev */ 177723cd1385SRemy Bohmer dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/ 177823cd1385SRemy Bohmer 177923cd1385SRemy Bohmer /* preallocate control message data and buffer */ 178023cd1385SRemy Bohmer dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL); 178123cd1385SRemy Bohmer if (!dev->req) 178223cd1385SRemy Bohmer goto fail; 178323cd1385SRemy Bohmer dev->req->buf = control_req; 178423cd1385SRemy Bohmer dev->req->complete = eth_setup_complete; 178523cd1385SRemy Bohmer 178623cd1385SRemy Bohmer /* ... and maybe likewise for status transfer */ 178723cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC) 178823cd1385SRemy Bohmer if (dev->status_ep) { 17896142e0aeSVitaly Kuzmichev dev->stat_req = usb_ep_alloc_request(dev->status_ep, 17906142e0aeSVitaly Kuzmichev GFP_KERNEL); 179123cd1385SRemy Bohmer if (!dev->stat_req) { 1792df559c1dSVitaly Kuzmichev usb_ep_free_request(dev->status_ep, dev->req); 179323cd1385SRemy Bohmer 179423cd1385SRemy Bohmer goto fail; 179523cd1385SRemy Bohmer } 1796df559c1dSVitaly Kuzmichev dev->stat_req->buf = status_req; 179723cd1385SRemy Bohmer dev->stat_req->context = NULL; 179823cd1385SRemy Bohmer } 179923cd1385SRemy Bohmer #endif 180023cd1385SRemy Bohmer 180123cd1385SRemy Bohmer /* finish hookup to lower layer ... */ 180223cd1385SRemy Bohmer dev->gadget = gadget; 180323cd1385SRemy Bohmer set_gadget_data(gadget, dev); 180423cd1385SRemy Bohmer gadget->ep0->driver_data = dev; 180523cd1385SRemy Bohmer 18066142e0aeSVitaly Kuzmichev /* 18076142e0aeSVitaly Kuzmichev * two kinds of host-initiated state changes: 180823cd1385SRemy Bohmer * - iff DATA transfer is active, carrier is "on" 180923cd1385SRemy Bohmer * - tx queueing enabled if open *and* carrier is "on" 181023cd1385SRemy Bohmer */ 181123cd1385SRemy Bohmer return 0; 181223cd1385SRemy Bohmer 181323cd1385SRemy Bohmer fail: 18147de73185SVitaly Kuzmichev error("%s failed", __func__); 181523cd1385SRemy Bohmer eth_unbind(gadget); 181623cd1385SRemy Bohmer return -ENOMEM; 181723cd1385SRemy Bohmer } 181823cd1385SRemy Bohmer 181923cd1385SRemy Bohmer static int usb_eth_init(struct eth_device *netdev, bd_t *bd) 182023cd1385SRemy Bohmer { 182123cd1385SRemy Bohmer struct eth_dev *dev = &l_ethdev; 182223cd1385SRemy Bohmer struct usb_gadget *gadget; 182323cd1385SRemy Bohmer unsigned long ts; 182423cd1385SRemy Bohmer unsigned long timeout = USB_CONNECT_TIMEOUT; 182523cd1385SRemy Bohmer 182623cd1385SRemy Bohmer if (!netdev) { 18277de73185SVitaly Kuzmichev error("received NULL ptr"); 182823cd1385SRemy Bohmer goto fail; 182923cd1385SRemy Bohmer } 183058939fccSVitaly Kuzmichev 183158939fccSVitaly Kuzmichev /* Configure default mac-addresses for the USB ethernet device */ 183258939fccSVitaly Kuzmichev #ifdef CONFIG_USBNET_DEV_ADDR 183358939fccSVitaly Kuzmichev strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr)); 183458939fccSVitaly Kuzmichev #endif 183558939fccSVitaly Kuzmichev #ifdef CONFIG_USBNET_HOST_ADDR 183658939fccSVitaly Kuzmichev strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr)); 183758939fccSVitaly Kuzmichev #endif 183858939fccSVitaly Kuzmichev /* Check if the user overruled the MAC addresses */ 183958939fccSVitaly Kuzmichev if (getenv("usbnet_devaddr")) 184058939fccSVitaly Kuzmichev strlcpy(dev_addr, getenv("usbnet_devaddr"), 184158939fccSVitaly Kuzmichev sizeof(dev_addr)); 184258939fccSVitaly Kuzmichev 184358939fccSVitaly Kuzmichev if (getenv("usbnet_hostaddr")) 184458939fccSVitaly Kuzmichev strlcpy(host_addr, getenv("usbnet_hostaddr"), 184558939fccSVitaly Kuzmichev sizeof(host_addr)); 184658939fccSVitaly Kuzmichev 184758939fccSVitaly Kuzmichev if (!is_eth_addr_valid(dev_addr)) { 184858939fccSVitaly Kuzmichev error("Need valid 'usbnet_devaddr' to be set"); 184958939fccSVitaly Kuzmichev goto fail; 185058939fccSVitaly Kuzmichev } 185158939fccSVitaly Kuzmichev if (!is_eth_addr_valid(host_addr)) { 185258939fccSVitaly Kuzmichev error("Need valid 'usbnet_hostaddr' to be set"); 185358939fccSVitaly Kuzmichev goto fail; 185458939fccSVitaly Kuzmichev } 185558939fccSVitaly Kuzmichev 1856988ee3e3SLei Wen if (usb_gadget_register_driver(ð_driver) < 0) 1857988ee3e3SLei Wen goto fail; 185823cd1385SRemy Bohmer 185923cd1385SRemy Bohmer dev->network_started = 0; 186023cd1385SRemy Bohmer 186123cd1385SRemy Bohmer packet_received = 0; 186223cd1385SRemy Bohmer packet_sent = 0; 186323cd1385SRemy Bohmer 186423cd1385SRemy Bohmer gadget = dev->gadget; 186523cd1385SRemy Bohmer usb_gadget_connect(gadget); 186623cd1385SRemy Bohmer 186723cd1385SRemy Bohmer if (getenv("cdc_connect_timeout")) 186823cd1385SRemy Bohmer timeout = simple_strtoul(getenv("cdc_connect_timeout"), 186923cd1385SRemy Bohmer NULL, 10) * CONFIG_SYS_HZ; 187023cd1385SRemy Bohmer ts = get_timer(0); 18716142e0aeSVitaly Kuzmichev while (!l_ethdev.network_started) { 187223cd1385SRemy Bohmer /* Handle control-c and timeouts */ 187323cd1385SRemy Bohmer if (ctrlc() || (get_timer(ts) > timeout)) { 18747de73185SVitaly Kuzmichev error("The remote end did not respond in time."); 187523cd1385SRemy Bohmer goto fail; 187623cd1385SRemy Bohmer } 187723cd1385SRemy Bohmer usb_gadget_handle_interrupts(); 187823cd1385SRemy Bohmer } 187923cd1385SRemy Bohmer 188098fae970SVitaly Kuzmichev packet_received = 0; 188123cd1385SRemy Bohmer rx_submit(dev, dev->rx_req, 0); 188223cd1385SRemy Bohmer return 0; 188323cd1385SRemy Bohmer fail: 188423cd1385SRemy Bohmer return -1; 188523cd1385SRemy Bohmer } 188623cd1385SRemy Bohmer 18876142e0aeSVitaly Kuzmichev static int usb_eth_send(struct eth_device *netdev, 18886142e0aeSVitaly Kuzmichev volatile void *packet, int length) 188923cd1385SRemy Bohmer { 189023cd1385SRemy Bohmer int retval; 189123cd1385SRemy Bohmer struct eth_dev *dev = &l_ethdev; 1892ac5d32d1SVitaly Kuzmichev struct usb_request *req = dev->tx_req; 1893a170f2c7SStefano Babic unsigned long ts; 1894a170f2c7SStefano Babic unsigned long timeout = USB_CONNECT_TIMEOUT; 18957de73185SVitaly Kuzmichev 18967de73185SVitaly Kuzmichev debug("%s:...\n", __func__); 189723cd1385SRemy Bohmer 189823cd1385SRemy Bohmer req->buf = (void *)packet; 189923cd1385SRemy Bohmer req->context = NULL; 190023cd1385SRemy Bohmer req->complete = tx_complete; 190123cd1385SRemy Bohmer 19026142e0aeSVitaly Kuzmichev /* 19036142e0aeSVitaly Kuzmichev * use zlp framing on tx for strict CDC-Ether conformance, 190423cd1385SRemy Bohmer * though any robust network rx path ignores extra padding. 190523cd1385SRemy Bohmer * and some hardware doesn't like to write zlps. 190623cd1385SRemy Bohmer */ 190723cd1385SRemy Bohmer req->zero = 1; 190823cd1385SRemy Bohmer if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0) 190923cd1385SRemy Bohmer length++; 191023cd1385SRemy Bohmer 191123cd1385SRemy Bohmer req->length = length; 191223cd1385SRemy Bohmer #if 0 191323cd1385SRemy Bohmer /* throttle highspeed IRQ rate back slightly */ 191423cd1385SRemy Bohmer if (gadget_is_dualspeed(dev->gadget)) 191523cd1385SRemy Bohmer req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH) 191623cd1385SRemy Bohmer ? ((dev->tx_qlen % qmult) != 0) : 0; 191723cd1385SRemy Bohmer #endif 191823cd1385SRemy Bohmer dev->tx_qlen = 1; 1919a170f2c7SStefano Babic ts = get_timer(0); 1920a170f2c7SStefano Babic packet_sent = 0; 192123cd1385SRemy Bohmer 192223cd1385SRemy Bohmer retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC); 192323cd1385SRemy Bohmer 192423cd1385SRemy Bohmer if (!retval) 19257de73185SVitaly Kuzmichev debug("%s: packet queued\n", __func__); 19266142e0aeSVitaly Kuzmichev while (!packet_sent) { 1927a170f2c7SStefano Babic if (get_timer(ts) > timeout) { 1928a170f2c7SStefano Babic printf("timeout sending packets to usb ethernet\n"); 1929a170f2c7SStefano Babic return -1; 1930a170f2c7SStefano Babic } 1931a170f2c7SStefano Babic usb_gadget_handle_interrupts(); 193223cd1385SRemy Bohmer } 193323cd1385SRemy Bohmer 193423cd1385SRemy Bohmer return 0; 193523cd1385SRemy Bohmer } 193623cd1385SRemy Bohmer 193723cd1385SRemy Bohmer static int usb_eth_recv(struct eth_device *netdev) 193823cd1385SRemy Bohmer { 193923cd1385SRemy Bohmer struct eth_dev *dev = &l_ethdev; 194023cd1385SRemy Bohmer 194123cd1385SRemy Bohmer usb_gadget_handle_interrupts(); 194223cd1385SRemy Bohmer 19436142e0aeSVitaly Kuzmichev if (packet_received) { 19447de73185SVitaly Kuzmichev debug("%s: packet received\n", __func__); 19456142e0aeSVitaly Kuzmichev if (dev->rx_req) { 194623cd1385SRemy Bohmer NetReceive(NetRxPackets[0], dev->rx_req->length); 194723cd1385SRemy Bohmer packet_received = 0; 194823cd1385SRemy Bohmer 194923cd1385SRemy Bohmer rx_submit(dev, dev->rx_req, 0); 19506142e0aeSVitaly Kuzmichev } else 19516142e0aeSVitaly Kuzmichev error("dev->rx_req invalid"); 195223cd1385SRemy Bohmer } 195323cd1385SRemy Bohmer return 0; 195423cd1385SRemy Bohmer } 195523cd1385SRemy Bohmer 195623cd1385SRemy Bohmer void usb_eth_halt(struct eth_device *netdev) 195723cd1385SRemy Bohmer { 195823cd1385SRemy Bohmer struct eth_dev *dev = &l_ethdev; 195923cd1385SRemy Bohmer 19606142e0aeSVitaly Kuzmichev if (!netdev) { 19617de73185SVitaly Kuzmichev error("received NULL ptr"); 196223cd1385SRemy Bohmer return; 196323cd1385SRemy Bohmer } 196423cd1385SRemy Bohmer 1965988ee3e3SLei Wen /* If the gadget not registered, simple return */ 1966988ee3e3SLei Wen if (!dev->gadget) 1967988ee3e3SLei Wen return; 1968988ee3e3SLei Wen 196923cd1385SRemy Bohmer usb_gadget_disconnect(dev->gadget); 1970b3649f3bSVitaly Kuzmichev 1971b3649f3bSVitaly Kuzmichev /* Clear pending interrupt */ 1972b3649f3bSVitaly Kuzmichev if (dev->network_started) { 1973b3649f3bSVitaly Kuzmichev usb_gadget_handle_interrupts(); 1974b3649f3bSVitaly Kuzmichev dev->network_started = 0; 1975b3649f3bSVitaly Kuzmichev } 1976b3649f3bSVitaly Kuzmichev 1977988ee3e3SLei Wen usb_gadget_unregister_driver(ð_driver); 197823cd1385SRemy Bohmer } 197923cd1385SRemy Bohmer 198023cd1385SRemy Bohmer static struct usb_gadget_driver eth_driver = { 198123cd1385SRemy Bohmer .speed = DEVSPEED, 198223cd1385SRemy Bohmer 198323cd1385SRemy Bohmer .bind = eth_bind, 198423cd1385SRemy Bohmer .unbind = eth_unbind, 198523cd1385SRemy Bohmer 198623cd1385SRemy Bohmer .setup = eth_setup, 198723cd1385SRemy Bohmer .disconnect = eth_disconnect, 198823cd1385SRemy Bohmer 198923cd1385SRemy Bohmer .suspend = eth_suspend, 199023cd1385SRemy Bohmer .resume = eth_resume, 199123cd1385SRemy Bohmer }; 199223cd1385SRemy Bohmer 199323cd1385SRemy Bohmer int usb_eth_initialize(bd_t *bi) 199423cd1385SRemy Bohmer { 199523cd1385SRemy Bohmer struct eth_device *netdev = &l_netdev; 199623cd1385SRemy Bohmer 19978f7aa831SVitaly Kuzmichev strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name)); 199823cd1385SRemy Bohmer 199923cd1385SRemy Bohmer netdev->init = usb_eth_init; 200023cd1385SRemy Bohmer netdev->send = usb_eth_send; 200123cd1385SRemy Bohmer netdev->recv = usb_eth_recv; 200223cd1385SRemy Bohmer netdev->halt = usb_eth_halt; 200323cd1385SRemy Bohmer 200423cd1385SRemy Bohmer #ifdef CONFIG_MCAST_TFTP 200523cd1385SRemy Bohmer #error not supported 200623cd1385SRemy Bohmer #endif 200723cd1385SRemy Bohmer eth_register(netdev); 200823cd1385SRemy Bohmer return 0; 200923cd1385SRemy Bohmer } 2010