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> 2523cd1385SRemy Bohmer #include <linux/usb/ch9.h> 2623cd1385SRemy Bohmer #include <linux/usb/cdc.h> 2723cd1385SRemy Bohmer #include <linux/usb/gadget.h> 2823cd1385SRemy Bohmer #include <net.h> 2923cd1385SRemy Bohmer #include <linux/ctype.h> 3023cd1385SRemy Bohmer 3123cd1385SRemy Bohmer #include "gadget_chips.h" 3223cd1385SRemy Bohmer 3323cd1385SRemy Bohmer #define USB_NET_NAME "usb0" 347de73185SVitaly Kuzmichev 3523cd1385SRemy Bohmer #define atomic_read 3623cd1385SRemy Bohmer extern struct platform_data brd; 3723cd1385SRemy Bohmer #define spin_lock(x) 3823cd1385SRemy Bohmer #define spin_unlock(x) 3923cd1385SRemy Bohmer 4023cd1385SRemy Bohmer 4123cd1385SRemy Bohmer unsigned packet_received, packet_sent; 4223cd1385SRemy Bohmer 4323cd1385SRemy Bohmer #define DEV_CONFIG_CDC 1 4423cd1385SRemy Bohmer #define GFP_ATOMIC ((gfp_t) 0) 4523cd1385SRemy Bohmer #define GFP_KERNEL ((gfp_t) 0) 4623cd1385SRemy Bohmer 4723cd1385SRemy Bohmer /* 4823cd1385SRemy Bohmer * Ethernet gadget driver -- with CDC and non-CDC options 4923cd1385SRemy Bohmer * Builds on hardware support for a full duplex link. 5023cd1385SRemy Bohmer * 5123cd1385SRemy Bohmer * CDC Ethernet is the standard USB solution for sending Ethernet frames 5223cd1385SRemy Bohmer * using USB. Real hardware tends to use the same framing protocol but look 5323cd1385SRemy Bohmer * different for control features. This driver strongly prefers to use 5423cd1385SRemy Bohmer * this USB-IF standard as its open-systems interoperability solution; 5523cd1385SRemy Bohmer * most host side USB stacks (except from Microsoft) support it. 5623cd1385SRemy Bohmer * 5723cd1385SRemy Bohmer * This is sometimes called "CDC ECM" (Ethernet Control Model) to support 5823cd1385SRemy Bohmer * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new 5923cd1385SRemy Bohmer * "CDC EEM" (Ethernet Emulation Model) is starting to spread. 6023cd1385SRemy Bohmer * 6123cd1385SRemy Bohmer * There's some hardware that can't talk CDC ECM. We make that hardware 6223cd1385SRemy Bohmer * implement a "minimalist" vendor-agnostic CDC core: same framing, but 6323cd1385SRemy Bohmer * link-level setup only requires activating the configuration. Only the 6423cd1385SRemy Bohmer * endpoint descriptors, and product/vendor IDs, are relevant; no control 6523cd1385SRemy Bohmer * operations are available. Linux supports it, but other host operating 6623cd1385SRemy Bohmer * systems may not. (This is a subset of CDC Ethernet.) 6723cd1385SRemy Bohmer * 6823cd1385SRemy Bohmer * It turns out that if you add a few descriptors to that "CDC Subset", 6923cd1385SRemy Bohmer * (Windows) host side drivers from MCCI can treat it as one submode of 7023cd1385SRemy Bohmer * a proprietary scheme called "SAFE" ... without needing to know about 7123cd1385SRemy Bohmer * specific product/vendor IDs. So we do that, making it easier to use 7223cd1385SRemy Bohmer * those MS-Windows drivers. Those added descriptors make it resemble a 7323cd1385SRemy Bohmer * CDC MDLM device, but they don't change device behavior at all. (See 7423cd1385SRemy Bohmer * MCCI Engineering report 950198 "SAFE Networking Functions".) 7523cd1385SRemy Bohmer * 7623cd1385SRemy Bohmer * A third option is also in use. Rather than CDC Ethernet, or something 7723cd1385SRemy Bohmer * simpler, Microsoft pushes their own approach: RNDIS. The published 7823cd1385SRemy Bohmer * RNDIS specs are ambiguous and appear to be incomplete, and are also 7923cd1385SRemy Bohmer * needlessly complex. They borrow more from CDC ACM than CDC ECM. 8023cd1385SRemy Bohmer */ 8123cd1385SRemy Bohmer #define ETH_ALEN 6 /* Octets in one ethernet addr */ 8223cd1385SRemy Bohmer #define ETH_HLEN 14 /* Total octets in header. */ 8323cd1385SRemy Bohmer #define ETH_ZLEN 60 /* Min. octets in frame sans FCS */ 8423cd1385SRemy Bohmer #define ETH_DATA_LEN 1500 /* Max. octets in payload */ 8523cd1385SRemy Bohmer #define ETH_FRAME_LEN PKTSIZE_ALIGN /* Max. octets in frame sans FCS */ 8623cd1385SRemy Bohmer #define ETH_FCS_LEN 4 /* Octets in the FCS */ 8723cd1385SRemy Bohmer 8823cd1385SRemy Bohmer #define DRIVER_DESC "Ethernet Gadget" 8923cd1385SRemy Bohmer /* Based on linux 2.6.27 version */ 9023cd1385SRemy Bohmer #define DRIVER_VERSION "May Day 2005" 9123cd1385SRemy Bohmer 9223cd1385SRemy Bohmer static const char shortname[] = "ether"; 9323cd1385SRemy Bohmer static const char driver_desc[] = DRIVER_DESC; 9423cd1385SRemy Bohmer 9523cd1385SRemy Bohmer #define RX_EXTRA 20 /* guard against rx overflows */ 9623cd1385SRemy Bohmer 9723cd1385SRemy Bohmer /* CDC support the same host-chosen outgoing packet filters. */ 9823cd1385SRemy Bohmer #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \ 9923cd1385SRemy Bohmer |USB_CDC_PACKET_TYPE_ALL_MULTICAST \ 10023cd1385SRemy Bohmer |USB_CDC_PACKET_TYPE_PROMISCUOUS \ 10123cd1385SRemy Bohmer |USB_CDC_PACKET_TYPE_DIRECTED) 10223cd1385SRemy Bohmer 10323cd1385SRemy Bohmer #define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ) 10423cd1385SRemy Bohmer 10523cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 10623cd1385SRemy Bohmer static struct eth_dev l_ethdev; 10723cd1385SRemy Bohmer static struct eth_device l_netdev; 10823cd1385SRemy Bohmer static struct usb_gadget_driver eth_driver; 10923cd1385SRemy Bohmer 11023cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 11123cd1385SRemy Bohmer 11223cd1385SRemy Bohmer /* "main" config is either CDC, or its simple subset */ 11323cd1385SRemy Bohmer static inline int is_cdc(struct eth_dev *dev) 11423cd1385SRemy Bohmer { 11523cd1385SRemy Bohmer #if !defined(DEV_CONFIG_SUBSET) 11623cd1385SRemy Bohmer return 1; /* only cdc possible */ 11723cd1385SRemy Bohmer #elif !defined(DEV_CONFIG_CDC) 11823cd1385SRemy Bohmer return 0; /* only subset possible */ 11923cd1385SRemy Bohmer #else 12023cd1385SRemy Bohmer return dev->cdc; /* depends on what hardware we found */ 12123cd1385SRemy Bohmer #endif 12223cd1385SRemy Bohmer } 12323cd1385SRemy Bohmer 12423cd1385SRemy Bohmer #define subset_active(dev) (!is_cdc(dev)) 12523cd1385SRemy Bohmer #define cdc_active(dev) (is_cdc(dev)) 12623cd1385SRemy Bohmer 12723cd1385SRemy Bohmer #define DEFAULT_QLEN 2 /* double buffering by default */ 12823cd1385SRemy Bohmer 12923cd1385SRemy Bohmer /* peak bulk transfer bits-per-second */ 13023cd1385SRemy Bohmer #define HS_BPS (13 * 512 * 8 * 1000 * 8) 13123cd1385SRemy Bohmer #define FS_BPS (19 * 64 * 1 * 1000 * 8) 13223cd1385SRemy Bohmer 13323cd1385SRemy Bohmer #ifdef CONFIG_USB_GADGET_DUALSPEED 13423cd1385SRemy Bohmer #define DEVSPEED USB_SPEED_HIGH 13523cd1385SRemy Bohmer 1362721dbf1SVitaly Kuzmichev #ifdef CONFIG_USB_ETH_QMULT 1372721dbf1SVitaly Kuzmichev #define qmult CONFIG_USB_ETH_QMULT 1382721dbf1SVitaly Kuzmichev #else 1392721dbf1SVitaly Kuzmichev #define qmult 5 1402721dbf1SVitaly Kuzmichev #endif 1412721dbf1SVitaly Kuzmichev 14223cd1385SRemy Bohmer /* for dual-speed hardware, use deeper queues at highspeed */ 14323cd1385SRemy Bohmer #define qlen(gadget) \ 14423cd1385SRemy Bohmer (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1)) 14523cd1385SRemy Bohmer 14623cd1385SRemy Bohmer static inline int BITRATE(struct usb_gadget *g) 14723cd1385SRemy Bohmer { 14823cd1385SRemy Bohmer return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS; 14923cd1385SRemy Bohmer } 15023cd1385SRemy Bohmer 15123cd1385SRemy Bohmer #else /* full speed (low speed doesn't do bulk) */ 15223cd1385SRemy Bohmer 15323cd1385SRemy Bohmer #define qmult 1 15423cd1385SRemy Bohmer 15523cd1385SRemy Bohmer #define DEVSPEED USB_SPEED_FULL 15623cd1385SRemy Bohmer 15723cd1385SRemy Bohmer #define qlen(gadget) DEFAULT_QLEN 15823cd1385SRemy Bohmer 15923cd1385SRemy Bohmer static inline int BITRATE(struct usb_gadget *g) 16023cd1385SRemy Bohmer { 16123cd1385SRemy Bohmer return FS_BPS; 16223cd1385SRemy Bohmer } 16323cd1385SRemy Bohmer #endif 16423cd1385SRemy Bohmer 16523cd1385SRemy Bohmer struct eth_dev { 16623cd1385SRemy Bohmer struct usb_gadget *gadget; 16723cd1385SRemy Bohmer struct usb_request *req; /* for control responses */ 16823cd1385SRemy Bohmer struct usb_request *stat_req; /* for cdc status */ 16923cd1385SRemy Bohmer 17023cd1385SRemy Bohmer u8 config; 17123cd1385SRemy Bohmer struct usb_ep *in_ep, *out_ep, *status_ep; 17223cd1385SRemy Bohmer const struct usb_endpoint_descriptor 17323cd1385SRemy Bohmer *in, *out, *status; 17423cd1385SRemy Bohmer 17523cd1385SRemy Bohmer struct usb_request *tx_req, *rx_req; 17623cd1385SRemy Bohmer 17723cd1385SRemy Bohmer struct eth_device *net; 17823cd1385SRemy Bohmer unsigned int tx_qlen; 17923cd1385SRemy Bohmer 18023cd1385SRemy Bohmer unsigned zlp:1; 18123cd1385SRemy Bohmer unsigned cdc:1; 18223cd1385SRemy Bohmer unsigned suspended:1; 18323cd1385SRemy Bohmer unsigned network_started:1; 18423cd1385SRemy Bohmer u16 cdc_filter; 18523cd1385SRemy Bohmer unsigned long todo; 18623cd1385SRemy Bohmer int mtu; 18723cd1385SRemy Bohmer #define WORK_RX_MEMORY 0 18823cd1385SRemy Bohmer u8 host_mac[ETH_ALEN]; 18923cd1385SRemy Bohmer }; 19023cd1385SRemy Bohmer 191*6142e0aeSVitaly Kuzmichev /* 192*6142e0aeSVitaly Kuzmichev * This version autoconfigures as much as possible at run-time. 19323cd1385SRemy Bohmer * 19423cd1385SRemy Bohmer * It also ASSUMES a self-powered device, without remote wakeup, 19523cd1385SRemy Bohmer * although remote wakeup support would make sense. 19623cd1385SRemy Bohmer */ 19723cd1385SRemy Bohmer 19823cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 19923cd1385SRemy Bohmer 200*6142e0aeSVitaly Kuzmichev /* 201*6142e0aeSVitaly Kuzmichev * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! 20223cd1385SRemy Bohmer * Instead: allocate your own, using normal USB-IF procedures. 20323cd1385SRemy Bohmer */ 20423cd1385SRemy Bohmer 205*6142e0aeSVitaly Kuzmichev /* 206*6142e0aeSVitaly Kuzmichev * Thanks to NetChip Technologies for donating this product ID. 20723cd1385SRemy Bohmer * It's for devices with only CDC Ethernet configurations. 20823cd1385SRemy Bohmer */ 20923cd1385SRemy Bohmer #define CDC_VENDOR_NUM 0x0525 /* NetChip */ 21023cd1385SRemy Bohmer #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */ 21123cd1385SRemy Bohmer 212*6142e0aeSVitaly Kuzmichev /* 213*6142e0aeSVitaly Kuzmichev * For hardware that can't talk CDC, we use the same vendor ID that 21423cd1385SRemy Bohmer * ARM Linux has used for ethernet-over-usb, both with sa1100 and 21523cd1385SRemy Bohmer * with pxa250. We're protocol-compatible, if the host-side drivers 21623cd1385SRemy Bohmer * use the endpoint descriptors. bcdDevice (version) is nonzero, so 21723cd1385SRemy Bohmer * drivers that need to hard-wire endpoint numbers have a hook. 21823cd1385SRemy Bohmer * 21923cd1385SRemy Bohmer * The protocol is a minimal subset of CDC Ether, which works on any bulk 22023cd1385SRemy Bohmer * hardware that's not deeply broken ... even on hardware that can't talk 22123cd1385SRemy Bohmer * RNDIS (like SA-1100, with no interrupt endpoint, or anything that 22223cd1385SRemy Bohmer * doesn't handle control-OUT). 22323cd1385SRemy Bohmer */ 22423cd1385SRemy Bohmer #define SIMPLE_VENDOR_NUM 0x049f 22523cd1385SRemy Bohmer #define SIMPLE_PRODUCT_NUM 0x505a 22623cd1385SRemy Bohmer 227*6142e0aeSVitaly Kuzmichev /* 228*6142e0aeSVitaly Kuzmichev * Some systems will want different product identifers published in the 22923cd1385SRemy Bohmer * device descriptor, either numbers or strings or both. These string 23023cd1385SRemy Bohmer * parameters are in UTF-8 (superset of ASCII's 7 bit characters). 23123cd1385SRemy Bohmer */ 23223cd1385SRemy Bohmer 23323cd1385SRemy Bohmer static ushort bcdDevice; 23423cd1385SRemy Bohmer #if defined(CONFIG_USBNET_MANUFACTURER) 23523cd1385SRemy Bohmer static char *iManufacturer = CONFIG_USBNET_MANUFACTURER; 23623cd1385SRemy Bohmer #else 23723cd1385SRemy Bohmer static char *iManufacturer = "U-boot"; 23823cd1385SRemy Bohmer #endif 23923cd1385SRemy Bohmer static char *iProduct; 24023cd1385SRemy Bohmer static char *iSerialNumber; 24123cd1385SRemy Bohmer static char dev_addr[18]; 24223cd1385SRemy Bohmer static char host_addr[18]; 24323cd1385SRemy Bohmer 24423cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 24523cd1385SRemy Bohmer 246*6142e0aeSVitaly Kuzmichev /* 247*6142e0aeSVitaly Kuzmichev * USB DRIVER HOOKUP (to the hardware driver, below us), mostly 24823cd1385SRemy Bohmer * ep0 implementation: descriptors, config management, setup(). 24923cd1385SRemy Bohmer * also optional class-specific notification interrupt transfer. 25023cd1385SRemy Bohmer */ 25123cd1385SRemy Bohmer 25223cd1385SRemy Bohmer /* 25323cd1385SRemy Bohmer * DESCRIPTORS ... most are static, but strings and (full) configuration 25423cd1385SRemy Bohmer * descriptors are built on demand. For now we do either full CDC, or 25523cd1385SRemy Bohmer * our simple subset. 25623cd1385SRemy Bohmer */ 25723cd1385SRemy Bohmer 25823cd1385SRemy Bohmer #define STRING_MANUFACTURER 1 25923cd1385SRemy Bohmer #define STRING_PRODUCT 2 26023cd1385SRemy Bohmer #define STRING_ETHADDR 3 26123cd1385SRemy Bohmer #define STRING_DATA 4 26223cd1385SRemy Bohmer #define STRING_CONTROL 5 26323cd1385SRemy Bohmer #define STRING_CDC 7 26423cd1385SRemy Bohmer #define STRING_SUBSET 8 26523cd1385SRemy Bohmer #define STRING_SERIALNUMBER 10 26623cd1385SRemy Bohmer 26723cd1385SRemy Bohmer /* holds our biggest descriptor */ 26823cd1385SRemy Bohmer #define USB_BUFSIZ 256 26923cd1385SRemy Bohmer 27023cd1385SRemy Bohmer /* 27123cd1385SRemy Bohmer * This device advertises one configuration, eth_config, 27223cd1385SRemy Bohmer * on hardware supporting at least two configs. 27323cd1385SRemy Bohmer * 27423cd1385SRemy Bohmer * FIXME define some higher-powered configurations to make it easier 27523cd1385SRemy Bohmer * to recharge batteries ... 27623cd1385SRemy Bohmer */ 27723cd1385SRemy Bohmer 27823cd1385SRemy Bohmer #define DEV_CONFIG_VALUE 1 /* cdc or subset */ 27923cd1385SRemy Bohmer 28023cd1385SRemy Bohmer static struct usb_device_descriptor 28123cd1385SRemy Bohmer device_desc = { 28223cd1385SRemy Bohmer .bLength = sizeof device_desc, 28323cd1385SRemy Bohmer .bDescriptorType = USB_DT_DEVICE, 28423cd1385SRemy Bohmer 28523cd1385SRemy Bohmer .bcdUSB = __constant_cpu_to_le16(0x0200), 28623cd1385SRemy Bohmer 28723cd1385SRemy Bohmer .bDeviceClass = USB_CLASS_COMM, 28823cd1385SRemy Bohmer .bDeviceSubClass = 0, 28923cd1385SRemy Bohmer .bDeviceProtocol = 0, 29023cd1385SRemy Bohmer 29123cd1385SRemy Bohmer .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM), 29223cd1385SRemy Bohmer .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM), 29323cd1385SRemy Bohmer .iManufacturer = STRING_MANUFACTURER, 29423cd1385SRemy Bohmer .iProduct = STRING_PRODUCT, 29523cd1385SRemy Bohmer .bNumConfigurations = 1, 29623cd1385SRemy Bohmer }; 29723cd1385SRemy Bohmer 29823cd1385SRemy Bohmer static struct usb_otg_descriptor 29923cd1385SRemy Bohmer otg_descriptor = { 30023cd1385SRemy Bohmer .bLength = sizeof otg_descriptor, 30123cd1385SRemy Bohmer .bDescriptorType = USB_DT_OTG, 30223cd1385SRemy Bohmer 30323cd1385SRemy Bohmer .bmAttributes = USB_OTG_SRP, 30423cd1385SRemy Bohmer }; 30523cd1385SRemy Bohmer 30623cd1385SRemy Bohmer static struct usb_config_descriptor 30723cd1385SRemy Bohmer eth_config = { 30823cd1385SRemy Bohmer .bLength = sizeof eth_config, 30923cd1385SRemy Bohmer .bDescriptorType = USB_DT_CONFIG, 31023cd1385SRemy Bohmer 31123cd1385SRemy Bohmer /* compute wTotalLength on the fly */ 31223cd1385SRemy Bohmer .bNumInterfaces = 2, 31323cd1385SRemy Bohmer .bConfigurationValue = DEV_CONFIG_VALUE, 31423cd1385SRemy Bohmer .iConfiguration = STRING_CDC, 31523cd1385SRemy Bohmer .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, 31623cd1385SRemy Bohmer .bMaxPower = 1, 31723cd1385SRemy Bohmer }; 31823cd1385SRemy Bohmer 31923cd1385SRemy Bohmer /* 32023cd1385SRemy Bohmer * Compared to the simple CDC subset, the full CDC Ethernet model adds 32123cd1385SRemy Bohmer * three class descriptors, two interface descriptors, optional status 32223cd1385SRemy Bohmer * endpoint. Both have a "data" interface and two bulk endpoints. 32323cd1385SRemy Bohmer * There are also differences in how control requests are handled. 32423cd1385SRemy Bohmer */ 32523cd1385SRemy Bohmer 32623cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC 32723cd1385SRemy Bohmer static struct usb_interface_descriptor 32823cd1385SRemy Bohmer control_intf = { 32923cd1385SRemy Bohmer .bLength = sizeof control_intf, 33023cd1385SRemy Bohmer .bDescriptorType = USB_DT_INTERFACE, 33123cd1385SRemy Bohmer 33223cd1385SRemy Bohmer .bInterfaceNumber = 0, 33323cd1385SRemy Bohmer /* status endpoint is optional; this may be patched later */ 33423cd1385SRemy Bohmer .bNumEndpoints = 1, 33523cd1385SRemy Bohmer .bInterfaceClass = USB_CLASS_COMM, 33623cd1385SRemy Bohmer .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, 33723cd1385SRemy Bohmer .bInterfaceProtocol = USB_CDC_PROTO_NONE, 33823cd1385SRemy Bohmer .iInterface = STRING_CONTROL, 33923cd1385SRemy Bohmer }; 34023cd1385SRemy Bohmer #endif 34123cd1385SRemy Bohmer 34223cd1385SRemy Bohmer static const struct usb_cdc_header_desc header_desc = { 34323cd1385SRemy Bohmer .bLength = sizeof header_desc, 34423cd1385SRemy Bohmer .bDescriptorType = USB_DT_CS_INTERFACE, 34523cd1385SRemy Bohmer .bDescriptorSubType = USB_CDC_HEADER_TYPE, 34623cd1385SRemy Bohmer 34723cd1385SRemy Bohmer .bcdCDC = __constant_cpu_to_le16(0x0110), 34823cd1385SRemy Bohmer }; 34923cd1385SRemy Bohmer 35023cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC) 35123cd1385SRemy Bohmer 35223cd1385SRemy Bohmer static const struct usb_cdc_union_desc union_desc = { 35323cd1385SRemy Bohmer .bLength = sizeof union_desc, 35423cd1385SRemy Bohmer .bDescriptorType = USB_DT_CS_INTERFACE, 35523cd1385SRemy Bohmer .bDescriptorSubType = USB_CDC_UNION_TYPE, 35623cd1385SRemy Bohmer 35723cd1385SRemy Bohmer .bMasterInterface0 = 0, /* index of control interface */ 35823cd1385SRemy Bohmer .bSlaveInterface0 = 1, /* index of DATA interface */ 35923cd1385SRemy Bohmer }; 36023cd1385SRemy Bohmer 36123cd1385SRemy Bohmer #endif /* CDC */ 36223cd1385SRemy Bohmer 36323cd1385SRemy Bohmer #ifndef DEV_CONFIG_CDC 36423cd1385SRemy Bohmer 365*6142e0aeSVitaly Kuzmichev /* 366*6142e0aeSVitaly Kuzmichev * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various 36723cd1385SRemy Bohmer * ways: data endpoints live in the control interface, there's no data 36823cd1385SRemy Bohmer * interface, and it's not used to talk to a cell phone radio. 36923cd1385SRemy Bohmer */ 37023cd1385SRemy Bohmer 37123cd1385SRemy Bohmer static const struct usb_cdc_mdlm_desc mdlm_desc = { 37223cd1385SRemy Bohmer .bLength = sizeof mdlm_desc, 37323cd1385SRemy Bohmer .bDescriptorType = USB_DT_CS_INTERFACE, 37423cd1385SRemy Bohmer .bDescriptorSubType = USB_CDC_MDLM_TYPE, 37523cd1385SRemy Bohmer 37623cd1385SRemy Bohmer .bcdVersion = __constant_cpu_to_le16(0x0100), 37723cd1385SRemy Bohmer .bGUID = { 37823cd1385SRemy Bohmer 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6, 37923cd1385SRemy Bohmer 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f, 38023cd1385SRemy Bohmer }, 38123cd1385SRemy Bohmer }; 38223cd1385SRemy Bohmer 383*6142e0aeSVitaly Kuzmichev /* 384*6142e0aeSVitaly Kuzmichev * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we 38523cd1385SRemy Bohmer * can't really use its struct. All we do here is say that we're using 38623cd1385SRemy Bohmer * the submode of "SAFE" which directly matches the CDC Subset. 38723cd1385SRemy Bohmer */ 38823cd1385SRemy Bohmer static const u8 mdlm_detail_desc[] = { 38923cd1385SRemy Bohmer 6, 39023cd1385SRemy Bohmer USB_DT_CS_INTERFACE, 39123cd1385SRemy Bohmer USB_CDC_MDLM_DETAIL_TYPE, 39223cd1385SRemy Bohmer 39323cd1385SRemy Bohmer 0, /* "SAFE" */ 39423cd1385SRemy Bohmer 0, /* network control capabilities (none) */ 39523cd1385SRemy Bohmer 0, /* network data capabilities ("raw" encapsulation) */ 39623cd1385SRemy Bohmer }; 39723cd1385SRemy Bohmer 39823cd1385SRemy Bohmer #endif 39923cd1385SRemy Bohmer 40023cd1385SRemy Bohmer static const struct usb_cdc_ether_desc ether_desc = { 40123cd1385SRemy Bohmer .bLength = sizeof(ether_desc), 40223cd1385SRemy Bohmer .bDescriptorType = USB_DT_CS_INTERFACE, 40323cd1385SRemy Bohmer .bDescriptorSubType = USB_CDC_ETHERNET_TYPE, 40423cd1385SRemy Bohmer 40523cd1385SRemy Bohmer /* this descriptor actually adds value, surprise! */ 40623cd1385SRemy Bohmer .iMACAddress = STRING_ETHADDR, 40723cd1385SRemy Bohmer .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */ 40823cd1385SRemy Bohmer .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN), 40923cd1385SRemy Bohmer .wNumberMCFilters = __constant_cpu_to_le16(0), 41023cd1385SRemy Bohmer .bNumberPowerFilters = 0, 41123cd1385SRemy Bohmer }; 41223cd1385SRemy Bohmer 41323cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC) 41423cd1385SRemy Bohmer 415*6142e0aeSVitaly Kuzmichev /* 416*6142e0aeSVitaly Kuzmichev * include the status endpoint if we can, even where it's optional. 41723cd1385SRemy Bohmer * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one 41823cd1385SRemy Bohmer * packet, to simplify cancellation; and a big transfer interval, to 41923cd1385SRemy Bohmer * waste less bandwidth. 42023cd1385SRemy Bohmer * 42123cd1385SRemy Bohmer * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even 42223cd1385SRemy Bohmer * if they ignore the connect/disconnect notifications that real aether 42323cd1385SRemy Bohmer * can provide. more advanced cdc configurations might want to support 42423cd1385SRemy Bohmer * encapsulated commands (vendor-specific, using control-OUT). 42523cd1385SRemy Bohmer */ 42623cd1385SRemy Bohmer 42723cd1385SRemy Bohmer #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */ 42823cd1385SRemy Bohmer #define STATUS_BYTECOUNT 16 /* 8 byte header + data */ 42923cd1385SRemy Bohmer 43023cd1385SRemy Bohmer static struct usb_endpoint_descriptor 43123cd1385SRemy Bohmer fs_status_desc = { 43223cd1385SRemy Bohmer .bLength = USB_DT_ENDPOINT_SIZE, 43323cd1385SRemy Bohmer .bDescriptorType = USB_DT_ENDPOINT, 43423cd1385SRemy Bohmer 43523cd1385SRemy Bohmer .bEndpointAddress = USB_DIR_IN, 43623cd1385SRemy Bohmer .bmAttributes = USB_ENDPOINT_XFER_INT, 43723cd1385SRemy Bohmer .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT), 43823cd1385SRemy Bohmer .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC, 43923cd1385SRemy Bohmer }; 44023cd1385SRemy Bohmer #endif 44123cd1385SRemy Bohmer 44223cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC 44323cd1385SRemy Bohmer 44423cd1385SRemy Bohmer /* the default data interface has no endpoints ... */ 44523cd1385SRemy Bohmer 44623cd1385SRemy Bohmer static const struct usb_interface_descriptor 44723cd1385SRemy Bohmer data_nop_intf = { 44823cd1385SRemy Bohmer .bLength = sizeof data_nop_intf, 44923cd1385SRemy Bohmer .bDescriptorType = USB_DT_INTERFACE, 45023cd1385SRemy Bohmer 45123cd1385SRemy Bohmer .bInterfaceNumber = 1, 45223cd1385SRemy Bohmer .bAlternateSetting = 0, 45323cd1385SRemy Bohmer .bNumEndpoints = 0, 45423cd1385SRemy Bohmer .bInterfaceClass = USB_CLASS_CDC_DATA, 45523cd1385SRemy Bohmer .bInterfaceSubClass = 0, 45623cd1385SRemy Bohmer .bInterfaceProtocol = 0, 45723cd1385SRemy Bohmer }; 45823cd1385SRemy Bohmer 45923cd1385SRemy Bohmer /* ... but the "real" data interface has two bulk endpoints */ 46023cd1385SRemy Bohmer 46123cd1385SRemy Bohmer static const struct usb_interface_descriptor 46223cd1385SRemy Bohmer data_intf = { 46323cd1385SRemy Bohmer .bLength = sizeof data_intf, 46423cd1385SRemy Bohmer .bDescriptorType = USB_DT_INTERFACE, 46523cd1385SRemy Bohmer 46623cd1385SRemy Bohmer .bInterfaceNumber = 1, 46723cd1385SRemy Bohmer .bAlternateSetting = 1, 46823cd1385SRemy Bohmer .bNumEndpoints = 2, 46923cd1385SRemy Bohmer .bInterfaceClass = USB_CLASS_CDC_DATA, 47023cd1385SRemy Bohmer .bInterfaceSubClass = 0, 47123cd1385SRemy Bohmer .bInterfaceProtocol = 0, 47223cd1385SRemy Bohmer .iInterface = STRING_DATA, 47323cd1385SRemy Bohmer }; 47423cd1385SRemy Bohmer 47523cd1385SRemy Bohmer #endif 47623cd1385SRemy Bohmer 47723cd1385SRemy Bohmer #ifdef DEV_CONFIG_SUBSET 47823cd1385SRemy Bohmer 47923cd1385SRemy Bohmer /* 48023cd1385SRemy Bohmer * "Simple" CDC-subset option is a simple vendor-neutral model that most 48123cd1385SRemy Bohmer * full speed controllers can handle: one interface, two bulk endpoints. 48223cd1385SRemy Bohmer * 48323cd1385SRemy Bohmer * To assist host side drivers, we fancy it up a bit, and add descriptors 48423cd1385SRemy Bohmer * so some host side drivers will understand it as a "SAFE" variant. 48523cd1385SRemy Bohmer */ 48623cd1385SRemy Bohmer 48723cd1385SRemy Bohmer static const struct usb_interface_descriptor 48823cd1385SRemy Bohmer subset_data_intf = { 48923cd1385SRemy Bohmer .bLength = sizeof subset_data_intf, 49023cd1385SRemy Bohmer .bDescriptorType = USB_DT_INTERFACE, 49123cd1385SRemy Bohmer 49223cd1385SRemy Bohmer .bInterfaceNumber = 0, 49323cd1385SRemy Bohmer .bAlternateSetting = 0, 49423cd1385SRemy Bohmer .bNumEndpoints = 2, 49523cd1385SRemy Bohmer .bInterfaceClass = USB_CLASS_COMM, 49623cd1385SRemy Bohmer .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM, 49723cd1385SRemy Bohmer .bInterfaceProtocol = 0, 49823cd1385SRemy Bohmer .iInterface = STRING_DATA, 49923cd1385SRemy Bohmer }; 50023cd1385SRemy Bohmer 50123cd1385SRemy Bohmer #endif /* SUBSET */ 50223cd1385SRemy Bohmer 50323cd1385SRemy Bohmer static struct usb_endpoint_descriptor 50423cd1385SRemy Bohmer fs_source_desc = { 50523cd1385SRemy Bohmer .bLength = USB_DT_ENDPOINT_SIZE, 50623cd1385SRemy Bohmer .bDescriptorType = USB_DT_ENDPOINT, 50723cd1385SRemy Bohmer 50823cd1385SRemy Bohmer .bEndpointAddress = USB_DIR_IN, 50923cd1385SRemy Bohmer .bmAttributes = USB_ENDPOINT_XFER_BULK, 51023cd1385SRemy Bohmer }; 51123cd1385SRemy Bohmer 51223cd1385SRemy Bohmer static struct usb_endpoint_descriptor 51323cd1385SRemy Bohmer fs_sink_desc = { 51423cd1385SRemy Bohmer .bLength = USB_DT_ENDPOINT_SIZE, 51523cd1385SRemy Bohmer .bDescriptorType = USB_DT_ENDPOINT, 51623cd1385SRemy Bohmer 51723cd1385SRemy Bohmer .bEndpointAddress = USB_DIR_OUT, 51823cd1385SRemy Bohmer .bmAttributes = USB_ENDPOINT_XFER_BULK, 51923cd1385SRemy Bohmer }; 52023cd1385SRemy Bohmer 52123cd1385SRemy Bohmer static const struct usb_descriptor_header *fs_eth_function[11] = { 52223cd1385SRemy Bohmer (struct usb_descriptor_header *) &otg_descriptor, 52323cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC 52423cd1385SRemy Bohmer /* "cdc" mode descriptors */ 52523cd1385SRemy Bohmer (struct usb_descriptor_header *) &control_intf, 52623cd1385SRemy Bohmer (struct usb_descriptor_header *) &header_desc, 52723cd1385SRemy Bohmer (struct usb_descriptor_header *) &union_desc, 52823cd1385SRemy Bohmer (struct usb_descriptor_header *) ðer_desc, 52923cd1385SRemy Bohmer /* NOTE: status endpoint may need to be removed */ 53023cd1385SRemy Bohmer (struct usb_descriptor_header *) &fs_status_desc, 53123cd1385SRemy Bohmer /* data interface, with altsetting */ 53223cd1385SRemy Bohmer (struct usb_descriptor_header *) &data_nop_intf, 53323cd1385SRemy Bohmer (struct usb_descriptor_header *) &data_intf, 53423cd1385SRemy Bohmer (struct usb_descriptor_header *) &fs_source_desc, 53523cd1385SRemy Bohmer (struct usb_descriptor_header *) &fs_sink_desc, 53623cd1385SRemy Bohmer NULL, 53723cd1385SRemy Bohmer #endif /* DEV_CONFIG_CDC */ 53823cd1385SRemy Bohmer }; 53923cd1385SRemy Bohmer 54023cd1385SRemy Bohmer static inline void fs_subset_descriptors(void) 54123cd1385SRemy Bohmer { 54223cd1385SRemy Bohmer #ifdef DEV_CONFIG_SUBSET 54323cd1385SRemy Bohmer /* behavior is "CDC Subset"; extra descriptors say "SAFE" */ 54423cd1385SRemy Bohmer fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf; 54523cd1385SRemy Bohmer fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc; 54623cd1385SRemy Bohmer fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc; 54723cd1385SRemy Bohmer fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc; 54823cd1385SRemy Bohmer fs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc; 54923cd1385SRemy Bohmer fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc; 55023cd1385SRemy Bohmer fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc; 55123cd1385SRemy Bohmer fs_eth_function[8] = NULL; 55223cd1385SRemy Bohmer #else 55323cd1385SRemy Bohmer fs_eth_function[1] = NULL; 55423cd1385SRemy Bohmer #endif 55523cd1385SRemy Bohmer } 55623cd1385SRemy Bohmer 55723cd1385SRemy Bohmer /* 55823cd1385SRemy Bohmer * usb 2.0 devices need to expose both high speed and full speed 55923cd1385SRemy Bohmer * descriptors, unless they only run at full speed. 56023cd1385SRemy Bohmer */ 56123cd1385SRemy Bohmer 56223cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC) 56323cd1385SRemy Bohmer static struct usb_endpoint_descriptor 56423cd1385SRemy Bohmer hs_status_desc = { 56523cd1385SRemy Bohmer .bLength = USB_DT_ENDPOINT_SIZE, 56623cd1385SRemy Bohmer .bDescriptorType = USB_DT_ENDPOINT, 56723cd1385SRemy Bohmer 56823cd1385SRemy Bohmer .bmAttributes = USB_ENDPOINT_XFER_INT, 56923cd1385SRemy Bohmer .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT), 57023cd1385SRemy Bohmer .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4, 57123cd1385SRemy Bohmer }; 57223cd1385SRemy Bohmer #endif /* DEV_CONFIG_CDC */ 57323cd1385SRemy Bohmer 57423cd1385SRemy Bohmer static struct usb_endpoint_descriptor 57523cd1385SRemy Bohmer hs_source_desc = { 57623cd1385SRemy Bohmer .bLength = USB_DT_ENDPOINT_SIZE, 57723cd1385SRemy Bohmer .bDescriptorType = USB_DT_ENDPOINT, 57823cd1385SRemy Bohmer 57923cd1385SRemy Bohmer .bmAttributes = USB_ENDPOINT_XFER_BULK, 58023cd1385SRemy Bohmer .wMaxPacketSize = __constant_cpu_to_le16(512), 58123cd1385SRemy Bohmer }; 58223cd1385SRemy Bohmer 58323cd1385SRemy Bohmer static struct usb_endpoint_descriptor 58423cd1385SRemy Bohmer hs_sink_desc = { 58523cd1385SRemy Bohmer .bLength = USB_DT_ENDPOINT_SIZE, 58623cd1385SRemy Bohmer .bDescriptorType = USB_DT_ENDPOINT, 58723cd1385SRemy Bohmer 58823cd1385SRemy Bohmer .bmAttributes = USB_ENDPOINT_XFER_BULK, 58923cd1385SRemy Bohmer .wMaxPacketSize = __constant_cpu_to_le16(512), 59023cd1385SRemy Bohmer }; 59123cd1385SRemy Bohmer 59223cd1385SRemy Bohmer static struct usb_qualifier_descriptor 59323cd1385SRemy Bohmer dev_qualifier = { 59423cd1385SRemy Bohmer .bLength = sizeof dev_qualifier, 59523cd1385SRemy Bohmer .bDescriptorType = USB_DT_DEVICE_QUALIFIER, 59623cd1385SRemy Bohmer 59723cd1385SRemy Bohmer .bcdUSB = __constant_cpu_to_le16(0x0200), 59823cd1385SRemy Bohmer .bDeviceClass = USB_CLASS_COMM, 59923cd1385SRemy Bohmer 60023cd1385SRemy Bohmer .bNumConfigurations = 1, 60123cd1385SRemy Bohmer }; 60223cd1385SRemy Bohmer 60323cd1385SRemy Bohmer static const struct usb_descriptor_header *hs_eth_function[11] = { 60423cd1385SRemy Bohmer (struct usb_descriptor_header *) &otg_descriptor, 60523cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC 60623cd1385SRemy Bohmer /* "cdc" mode descriptors */ 60723cd1385SRemy Bohmer (struct usb_descriptor_header *) &control_intf, 60823cd1385SRemy Bohmer (struct usb_descriptor_header *) &header_desc, 60923cd1385SRemy Bohmer (struct usb_descriptor_header *) &union_desc, 61023cd1385SRemy Bohmer (struct usb_descriptor_header *) ðer_desc, 61123cd1385SRemy Bohmer /* NOTE: status endpoint may need to be removed */ 61223cd1385SRemy Bohmer (struct usb_descriptor_header *) &hs_status_desc, 61323cd1385SRemy Bohmer /* data interface, with altsetting */ 61423cd1385SRemy Bohmer (struct usb_descriptor_header *) &data_nop_intf, 61523cd1385SRemy Bohmer (struct usb_descriptor_header *) &data_intf, 61623cd1385SRemy Bohmer (struct usb_descriptor_header *) &hs_source_desc, 61723cd1385SRemy Bohmer (struct usb_descriptor_header *) &hs_sink_desc, 61823cd1385SRemy Bohmer NULL, 61923cd1385SRemy Bohmer #endif /* DEV_CONFIG_CDC */ 62023cd1385SRemy Bohmer }; 62123cd1385SRemy Bohmer 62223cd1385SRemy Bohmer static inline void hs_subset_descriptors(void) 62323cd1385SRemy Bohmer { 62423cd1385SRemy Bohmer #ifdef DEV_CONFIG_SUBSET 62523cd1385SRemy Bohmer /* behavior is "CDC Subset"; extra descriptors say "SAFE" */ 62623cd1385SRemy Bohmer hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf; 62723cd1385SRemy Bohmer hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc; 62823cd1385SRemy Bohmer hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc; 62923cd1385SRemy Bohmer hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc; 63023cd1385SRemy Bohmer hs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc; 63123cd1385SRemy Bohmer hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc; 63223cd1385SRemy Bohmer hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc; 63323cd1385SRemy Bohmer hs_eth_function[8] = NULL; 63423cd1385SRemy Bohmer #else 63523cd1385SRemy Bohmer hs_eth_function[1] = NULL; 63623cd1385SRemy Bohmer #endif 63723cd1385SRemy Bohmer } 63823cd1385SRemy Bohmer 63923cd1385SRemy Bohmer /* maxpacket and other transfer characteristics vary by speed. */ 64023cd1385SRemy Bohmer static inline struct usb_endpoint_descriptor * 64123cd1385SRemy Bohmer ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs, 64223cd1385SRemy Bohmer struct usb_endpoint_descriptor *fs) 64323cd1385SRemy Bohmer { 64423cd1385SRemy Bohmer if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) 64523cd1385SRemy Bohmer return hs; 64623cd1385SRemy Bohmer return fs; 64723cd1385SRemy Bohmer } 64823cd1385SRemy Bohmer 64923cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 65023cd1385SRemy Bohmer 65123cd1385SRemy Bohmer /* descriptors that are built on-demand */ 65223cd1385SRemy Bohmer 65323cd1385SRemy Bohmer static char manufacturer[50]; 65423cd1385SRemy Bohmer static char product_desc[40] = DRIVER_DESC; 65523cd1385SRemy Bohmer static char serial_number[20]; 65623cd1385SRemy Bohmer 65723cd1385SRemy Bohmer /* address that the host will use ... usually assigned at random */ 65823cd1385SRemy Bohmer static char ethaddr[2 * ETH_ALEN + 1]; 65923cd1385SRemy Bohmer 66023cd1385SRemy Bohmer /* static strings, in UTF-8 */ 66123cd1385SRemy Bohmer static struct usb_string strings[] = { 66223cd1385SRemy Bohmer { STRING_MANUFACTURER, manufacturer, }, 66323cd1385SRemy Bohmer { STRING_PRODUCT, product_desc, }, 66423cd1385SRemy Bohmer { STRING_SERIALNUMBER, serial_number, }, 66523cd1385SRemy Bohmer { STRING_DATA, "Ethernet Data", }, 66623cd1385SRemy Bohmer { STRING_ETHADDR, ethaddr, }, 66723cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC 66823cd1385SRemy Bohmer { STRING_CDC, "CDC Ethernet", }, 66923cd1385SRemy Bohmer { STRING_CONTROL, "CDC Communications Control", }, 67023cd1385SRemy Bohmer #endif 67123cd1385SRemy Bohmer #ifdef DEV_CONFIG_SUBSET 67223cd1385SRemy Bohmer { STRING_SUBSET, "CDC Ethernet Subset", }, 67323cd1385SRemy Bohmer #endif 67423cd1385SRemy Bohmer { } /* end of list */ 67523cd1385SRemy Bohmer }; 67623cd1385SRemy Bohmer 67723cd1385SRemy Bohmer static struct usb_gadget_strings stringtab = { 67823cd1385SRemy Bohmer .language = 0x0409, /* en-us */ 67923cd1385SRemy Bohmer .strings = strings, 68023cd1385SRemy Bohmer }; 68123cd1385SRemy Bohmer 68223cd1385SRemy Bohmer /*============================================================================*/ 68323cd1385SRemy Bohmer static u8 control_req[USB_BUFSIZ]; 68480460772SStefano Babic static u8 status_req[STATUS_BYTECOUNT] __attribute__ ((aligned(4))); 68523cd1385SRemy Bohmer 68623cd1385SRemy Bohmer 68723cd1385SRemy Bohmer /** 68823cd1385SRemy Bohmer * strlcpy - Copy a %NUL terminated string into a sized buffer 68923cd1385SRemy Bohmer * @dest: Where to copy the string to 69023cd1385SRemy Bohmer * @src: Where to copy the string from 69123cd1385SRemy Bohmer * @size: size of destination buffer 69223cd1385SRemy Bohmer * 69323cd1385SRemy Bohmer * Compatible with *BSD: the result is always a valid 69423cd1385SRemy Bohmer * NUL-terminated string that fits in the buffer (unless, 69523cd1385SRemy Bohmer * of course, the buffer size is zero). It does not pad 69623cd1385SRemy Bohmer * out the result like strncpy() does. 69723cd1385SRemy Bohmer */ 69823cd1385SRemy Bohmer size_t strlcpy(char *dest, const char *src, size_t size) 69923cd1385SRemy Bohmer { 70023cd1385SRemy Bohmer size_t ret = strlen(src); 70123cd1385SRemy Bohmer 70223cd1385SRemy Bohmer if (size) { 70323cd1385SRemy Bohmer size_t len = (ret >= size) ? size - 1 : ret; 70423cd1385SRemy Bohmer memcpy(dest, src, len); 70523cd1385SRemy Bohmer dest[len] = '\0'; 70623cd1385SRemy Bohmer } 70723cd1385SRemy Bohmer return ret; 70823cd1385SRemy Bohmer } 70923cd1385SRemy Bohmer 71023cd1385SRemy Bohmer /*============================================================================*/ 71123cd1385SRemy Bohmer 71223cd1385SRemy Bohmer /* 71323cd1385SRemy Bohmer * one config, two interfaces: control, data. 71423cd1385SRemy Bohmer * complications: class descriptors, and an altsetting. 71523cd1385SRemy Bohmer */ 71623cd1385SRemy Bohmer static int 71723cd1385SRemy Bohmer config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg) 71823cd1385SRemy Bohmer { 71923cd1385SRemy Bohmer int len; 72023cd1385SRemy Bohmer const struct usb_config_descriptor *config; 72123cd1385SRemy Bohmer const struct usb_descriptor_header **function; 72223cd1385SRemy Bohmer int hs = 0; 72323cd1385SRemy Bohmer 72423cd1385SRemy Bohmer if (gadget_is_dualspeed(g)) { 72523cd1385SRemy Bohmer hs = (g->speed == USB_SPEED_HIGH); 72623cd1385SRemy Bohmer if (type == USB_DT_OTHER_SPEED_CONFIG) 72723cd1385SRemy Bohmer hs = !hs; 72823cd1385SRemy Bohmer } 72923cd1385SRemy Bohmer #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function) 73023cd1385SRemy Bohmer 73123cd1385SRemy Bohmer if (index >= device_desc.bNumConfigurations) 73223cd1385SRemy Bohmer return -EINVAL; 73323cd1385SRemy Bohmer 73423cd1385SRemy Bohmer config = ð_config; 73523cd1385SRemy Bohmer function = which_fn(eth); 73623cd1385SRemy Bohmer 73723cd1385SRemy Bohmer /* for now, don't advertise srp-only devices */ 73823cd1385SRemy Bohmer if (!is_otg) 73923cd1385SRemy Bohmer function++; 74023cd1385SRemy Bohmer 74123cd1385SRemy Bohmer len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function); 74223cd1385SRemy Bohmer if (len < 0) 74323cd1385SRemy Bohmer return len; 74423cd1385SRemy Bohmer ((struct usb_config_descriptor *) buf)->bDescriptorType = type; 74523cd1385SRemy Bohmer return len; 74623cd1385SRemy Bohmer } 74723cd1385SRemy Bohmer 74823cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 74923cd1385SRemy Bohmer 75023cd1385SRemy Bohmer static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags); 75123cd1385SRemy Bohmer 75223cd1385SRemy Bohmer static int 75323cd1385SRemy Bohmer set_ether_config(struct eth_dev *dev, gfp_t gfp_flags) 75423cd1385SRemy Bohmer { 75523cd1385SRemy Bohmer int result = 0; 75623cd1385SRemy Bohmer struct usb_gadget *gadget = dev->gadget; 75723cd1385SRemy Bohmer 75823cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC) 75923cd1385SRemy Bohmer /* status endpoint used for (optionally) CDC */ 76023cd1385SRemy Bohmer if (!subset_active(dev) && dev->status_ep) { 76123cd1385SRemy Bohmer dev->status = ep_desc(gadget, &hs_status_desc, 76223cd1385SRemy Bohmer &fs_status_desc); 76323cd1385SRemy Bohmer dev->status_ep->driver_data = dev; 76423cd1385SRemy Bohmer 76523cd1385SRemy Bohmer result = usb_ep_enable(dev->status_ep, dev->status); 76623cd1385SRemy Bohmer if (result != 0) { 7677de73185SVitaly Kuzmichev debug("enable %s --> %d\n", 76823cd1385SRemy Bohmer dev->status_ep->name, result); 76923cd1385SRemy Bohmer goto done; 77023cd1385SRemy Bohmer } 77123cd1385SRemy Bohmer } 77223cd1385SRemy Bohmer #endif 77323cd1385SRemy Bohmer 77423cd1385SRemy Bohmer dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc); 77523cd1385SRemy Bohmer dev->in_ep->driver_data = dev; 77623cd1385SRemy Bohmer 77723cd1385SRemy Bohmer dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc); 77823cd1385SRemy Bohmer dev->out_ep->driver_data = dev; 77923cd1385SRemy Bohmer 780*6142e0aeSVitaly Kuzmichev /* 781*6142e0aeSVitaly Kuzmichev * With CDC, the host isn't allowed to use these two data 78223cd1385SRemy Bohmer * endpoints in the default altsetting for the interface. 78323cd1385SRemy Bohmer * so we don't activate them yet. Reset from SET_INTERFACE. 78423cd1385SRemy Bohmer */ 78523cd1385SRemy Bohmer if (!cdc_active(dev)) { 78623cd1385SRemy Bohmer result = usb_ep_enable(dev->in_ep, dev->in); 78723cd1385SRemy Bohmer if (result != 0) { 7887de73185SVitaly Kuzmichev debug("enable %s --> %d\n", 78923cd1385SRemy Bohmer dev->in_ep->name, result); 79023cd1385SRemy Bohmer goto done; 79123cd1385SRemy Bohmer } 79223cd1385SRemy Bohmer 79323cd1385SRemy Bohmer result = usb_ep_enable(dev->out_ep, dev->out); 79423cd1385SRemy Bohmer if (result != 0) { 7957de73185SVitaly Kuzmichev debug("enable %s --> %d\n", 79623cd1385SRemy Bohmer dev->out_ep->name, result); 79723cd1385SRemy Bohmer goto done; 79823cd1385SRemy Bohmer } 79923cd1385SRemy Bohmer } 80023cd1385SRemy Bohmer 80123cd1385SRemy Bohmer done: 80223cd1385SRemy Bohmer if (result == 0) 80323cd1385SRemy Bohmer result = alloc_requests(dev, qlen(gadget), gfp_flags); 80423cd1385SRemy Bohmer 80523cd1385SRemy Bohmer /* on error, disable any endpoints */ 80623cd1385SRemy Bohmer if (result < 0) { 807d5292c16SVitaly Kuzmichev if (!subset_active(dev) && dev->status_ep) 80823cd1385SRemy Bohmer (void) usb_ep_disable(dev->status_ep); 80923cd1385SRemy Bohmer dev->status = NULL; 81023cd1385SRemy Bohmer (void) usb_ep_disable(dev->in_ep); 81123cd1385SRemy Bohmer (void) usb_ep_disable(dev->out_ep); 81223cd1385SRemy Bohmer dev->in = NULL; 81323cd1385SRemy Bohmer dev->out = NULL; 81423cd1385SRemy Bohmer } 81523cd1385SRemy Bohmer 81623cd1385SRemy Bohmer /* caller is responsible for cleanup on error */ 81723cd1385SRemy Bohmer return result; 81823cd1385SRemy Bohmer } 81923cd1385SRemy Bohmer 82023cd1385SRemy Bohmer static void eth_reset_config(struct eth_dev *dev) 82123cd1385SRemy Bohmer { 82223cd1385SRemy Bohmer if (dev->config == 0) 82323cd1385SRemy Bohmer return; 82423cd1385SRemy Bohmer 8257de73185SVitaly Kuzmichev debug("%s\n", __func__); 8267de73185SVitaly Kuzmichev 827*6142e0aeSVitaly Kuzmichev /* 828*6142e0aeSVitaly Kuzmichev * disable endpoints, forcing (synchronous) completion of 82923cd1385SRemy Bohmer * pending i/o. then free the requests. 83023cd1385SRemy Bohmer */ 83123cd1385SRemy Bohmer 83223cd1385SRemy Bohmer if (dev->in) { 83323cd1385SRemy Bohmer usb_ep_disable(dev->in_ep); 83423cd1385SRemy Bohmer if (dev->tx_req) { 83523cd1385SRemy Bohmer usb_ep_free_request(dev->in_ep, dev->tx_req); 83623cd1385SRemy Bohmer dev->tx_req = NULL; 83723cd1385SRemy Bohmer } 83823cd1385SRemy Bohmer } 83923cd1385SRemy Bohmer if (dev->out) { 84023cd1385SRemy Bohmer usb_ep_disable(dev->out_ep); 84123cd1385SRemy Bohmer if (dev->rx_req) { 8420129e327SVitaly Kuzmichev usb_ep_free_request(dev->out_ep, dev->rx_req); 84323cd1385SRemy Bohmer dev->rx_req = NULL; 84423cd1385SRemy Bohmer } 84523cd1385SRemy Bohmer } 846*6142e0aeSVitaly Kuzmichev if (dev->status) 84723cd1385SRemy Bohmer usb_ep_disable(dev->status_ep); 848*6142e0aeSVitaly Kuzmichev 84923cd1385SRemy Bohmer dev->cdc_filter = 0; 85023cd1385SRemy Bohmer dev->config = 0; 85123cd1385SRemy Bohmer } 85223cd1385SRemy Bohmer 853*6142e0aeSVitaly Kuzmichev /* 854*6142e0aeSVitaly Kuzmichev * change our operational config. must agree with the code 85523cd1385SRemy Bohmer * that returns config descriptors, and altsetting code. 85623cd1385SRemy Bohmer */ 857*6142e0aeSVitaly Kuzmichev static int eth_set_config(struct eth_dev *dev, unsigned number, 858*6142e0aeSVitaly Kuzmichev gfp_t gfp_flags) 85923cd1385SRemy Bohmer { 86023cd1385SRemy Bohmer int result = 0; 86123cd1385SRemy Bohmer struct usb_gadget *gadget = dev->gadget; 86223cd1385SRemy Bohmer 86323cd1385SRemy Bohmer if (gadget_is_sa1100(gadget) 86423cd1385SRemy Bohmer && dev->config 86523cd1385SRemy Bohmer && dev->tx_qlen != 0) { 86623cd1385SRemy Bohmer /* tx fifo is full, but we can't clear it...*/ 8677de73185SVitaly Kuzmichev error("can't change configurations"); 86823cd1385SRemy Bohmer return -ESPIPE; 86923cd1385SRemy Bohmer } 87023cd1385SRemy Bohmer eth_reset_config(dev); 87123cd1385SRemy Bohmer 87223cd1385SRemy Bohmer switch (number) { 87323cd1385SRemy Bohmer case DEV_CONFIG_VALUE: 87423cd1385SRemy Bohmer result = set_ether_config(dev, gfp_flags); 87523cd1385SRemy Bohmer break; 87623cd1385SRemy Bohmer default: 87723cd1385SRemy Bohmer result = -EINVAL; 87823cd1385SRemy Bohmer /* FALL THROUGH */ 87923cd1385SRemy Bohmer case 0: 88023cd1385SRemy Bohmer break; 88123cd1385SRemy Bohmer } 88223cd1385SRemy Bohmer 88323cd1385SRemy Bohmer if (result) { 88423cd1385SRemy Bohmer if (number) 88523cd1385SRemy Bohmer eth_reset_config(dev); 88623cd1385SRemy Bohmer usb_gadget_vbus_draw(dev->gadget, 88723cd1385SRemy Bohmer gadget_is_otg(dev->gadget) ? 8 : 100); 88823cd1385SRemy Bohmer } else { 88923cd1385SRemy Bohmer char *speed; 89023cd1385SRemy Bohmer unsigned power; 89123cd1385SRemy Bohmer 89223cd1385SRemy Bohmer power = 2 * eth_config.bMaxPower; 89323cd1385SRemy Bohmer usb_gadget_vbus_draw(dev->gadget, power); 89423cd1385SRemy Bohmer 89523cd1385SRemy Bohmer switch (gadget->speed) { 896*6142e0aeSVitaly Kuzmichev case USB_SPEED_FULL: 897*6142e0aeSVitaly Kuzmichev speed = "full"; break; 89823cd1385SRemy Bohmer #ifdef CONFIG_USB_GADGET_DUALSPEED 899*6142e0aeSVitaly Kuzmichev case USB_SPEED_HIGH: 900*6142e0aeSVitaly Kuzmichev speed = "high"; break; 90123cd1385SRemy Bohmer #endif 902*6142e0aeSVitaly Kuzmichev default: 903*6142e0aeSVitaly Kuzmichev speed = "?"; break; 90423cd1385SRemy Bohmer } 90523cd1385SRemy Bohmer 90623cd1385SRemy Bohmer dev->config = number; 9077de73185SVitaly Kuzmichev printf("%s speed config #%d: %d mA, %s, using %s\n", 90823cd1385SRemy Bohmer speed, number, power, driver_desc, 90923cd1385SRemy Bohmer (cdc_active(dev) ? "CDC Ethernet" 91023cd1385SRemy Bohmer : "CDC Ethernet Subset")); 91123cd1385SRemy Bohmer } 91223cd1385SRemy Bohmer return result; 91323cd1385SRemy Bohmer } 91423cd1385SRemy Bohmer 91523cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 91623cd1385SRemy Bohmer 91723cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC 91823cd1385SRemy Bohmer 919*6142e0aeSVitaly Kuzmichev /* 920*6142e0aeSVitaly Kuzmichev * The interrupt endpoint is used in CDC networking models (Ethernet, ATM) 92123cd1385SRemy Bohmer * only to notify the host about link status changes (which we support) or 92223cd1385SRemy Bohmer * report completion of some encapsulated command. Since 92323cd1385SRemy Bohmer * we want this CDC Ethernet code to be vendor-neutral, we don't use that 92423cd1385SRemy Bohmer * command mechanism; and only one status request is ever queued. 92523cd1385SRemy Bohmer */ 92623cd1385SRemy Bohmer static void eth_status_complete(struct usb_ep *ep, struct usb_request *req) 92723cd1385SRemy Bohmer { 92823cd1385SRemy Bohmer struct usb_cdc_notification *event = req->buf; 92923cd1385SRemy Bohmer int value = req->status; 93023cd1385SRemy Bohmer struct eth_dev *dev = ep->driver_data; 93123cd1385SRemy Bohmer 93223cd1385SRemy Bohmer /* issue the second notification if host reads the first */ 93323cd1385SRemy Bohmer if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION 93423cd1385SRemy Bohmer && value == 0) { 93523cd1385SRemy Bohmer __le32 *data = req->buf + sizeof *event; 93623cd1385SRemy Bohmer 93723cd1385SRemy Bohmer event->bmRequestType = 0xA1; 93823cd1385SRemy Bohmer event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE; 93923cd1385SRemy Bohmer event->wValue = __constant_cpu_to_le16(0); 94023cd1385SRemy Bohmer event->wIndex = __constant_cpu_to_le16(1); 94123cd1385SRemy Bohmer event->wLength = __constant_cpu_to_le16(8); 94223cd1385SRemy Bohmer 94323cd1385SRemy Bohmer /* SPEED_CHANGE data is up/down speeds in bits/sec */ 94423cd1385SRemy Bohmer data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget)); 94523cd1385SRemy Bohmer 94623cd1385SRemy Bohmer req->length = STATUS_BYTECOUNT; 94723cd1385SRemy Bohmer value = usb_ep_queue(ep, req, GFP_ATOMIC); 9487de73185SVitaly Kuzmichev debug("send SPEED_CHANGE --> %d\n", value); 94923cd1385SRemy Bohmer if (value == 0) 95023cd1385SRemy Bohmer return; 95123cd1385SRemy Bohmer } else if (value != -ECONNRESET) { 9527de73185SVitaly Kuzmichev debug("event %02x --> %d\n", 95323cd1385SRemy Bohmer event->bNotificationType, value); 95423cd1385SRemy Bohmer if (event->bNotificationType == 955*6142e0aeSVitaly Kuzmichev USB_CDC_NOTIFY_SPEED_CHANGE) { 95623cd1385SRemy Bohmer l_ethdev.network_started = 1; 95723cd1385SRemy Bohmer printf("USB network up!\n"); 95823cd1385SRemy Bohmer } 95923cd1385SRemy Bohmer } 96023cd1385SRemy Bohmer req->context = NULL; 96123cd1385SRemy Bohmer } 96223cd1385SRemy Bohmer 96323cd1385SRemy Bohmer static void issue_start_status(struct eth_dev *dev) 96423cd1385SRemy Bohmer { 96523cd1385SRemy Bohmer struct usb_request *req = dev->stat_req; 96623cd1385SRemy Bohmer struct usb_cdc_notification *event; 96723cd1385SRemy Bohmer int value; 96823cd1385SRemy Bohmer 969*6142e0aeSVitaly Kuzmichev /* 970*6142e0aeSVitaly Kuzmichev * flush old status 97123cd1385SRemy Bohmer * 97223cd1385SRemy Bohmer * FIXME ugly idiom, maybe we'd be better with just 97323cd1385SRemy Bohmer * a "cancel the whole queue" primitive since any 97423cd1385SRemy Bohmer * unlink-one primitive has way too many error modes. 97523cd1385SRemy Bohmer * here, we "know" toggle is already clear... 97623cd1385SRemy Bohmer * 97723cd1385SRemy Bohmer * FIXME iff req->context != null just dequeue it 97823cd1385SRemy Bohmer */ 97923cd1385SRemy Bohmer usb_ep_disable(dev->status_ep); 98023cd1385SRemy Bohmer usb_ep_enable(dev->status_ep, dev->status); 98123cd1385SRemy Bohmer 982*6142e0aeSVitaly Kuzmichev /* 983*6142e0aeSVitaly Kuzmichev * 3.8.1 says to issue first NETWORK_CONNECTION, then 98423cd1385SRemy Bohmer * a SPEED_CHANGE. could be useful in some configs. 98523cd1385SRemy Bohmer */ 98623cd1385SRemy Bohmer event = req->buf; 98723cd1385SRemy Bohmer event->bmRequestType = 0xA1; 98823cd1385SRemy Bohmer event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION; 98923cd1385SRemy Bohmer event->wValue = __constant_cpu_to_le16(1); /* connected */ 99023cd1385SRemy Bohmer event->wIndex = __constant_cpu_to_le16(1); 99123cd1385SRemy Bohmer event->wLength = 0; 99223cd1385SRemy Bohmer 99323cd1385SRemy Bohmer req->length = sizeof *event; 99423cd1385SRemy Bohmer req->complete = eth_status_complete; 99523cd1385SRemy Bohmer req->context = dev; 99623cd1385SRemy Bohmer 99723cd1385SRemy Bohmer value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC); 99823cd1385SRemy Bohmer if (value < 0) 9997de73185SVitaly Kuzmichev debug("status buf queue --> %d\n", value); 100023cd1385SRemy Bohmer } 100123cd1385SRemy Bohmer 100223cd1385SRemy Bohmer #endif 100323cd1385SRemy Bohmer 100423cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 100523cd1385SRemy Bohmer 100623cd1385SRemy Bohmer static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req) 100723cd1385SRemy Bohmer { 100823cd1385SRemy Bohmer if (req->status || req->actual != req->length) 10097de73185SVitaly Kuzmichev debug("setup complete --> %d, %d/%d\n", 101023cd1385SRemy Bohmer req->status, req->actual, req->length); 101123cd1385SRemy Bohmer } 101223cd1385SRemy Bohmer 101323cd1385SRemy Bohmer /* 101423cd1385SRemy Bohmer * The setup() callback implements all the ep0 functionality that's not 101523cd1385SRemy Bohmer * handled lower down. CDC has a number of less-common features: 101623cd1385SRemy Bohmer * 101723cd1385SRemy Bohmer * - two interfaces: control, and ethernet data 101823cd1385SRemy Bohmer * - Ethernet data interface has two altsettings: default, and active 101923cd1385SRemy Bohmer * - class-specific descriptors for the control interface 102023cd1385SRemy Bohmer * - class-specific control requests 102123cd1385SRemy Bohmer */ 102223cd1385SRemy Bohmer static int 102323cd1385SRemy Bohmer eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) 102423cd1385SRemy Bohmer { 102523cd1385SRemy Bohmer struct eth_dev *dev = get_gadget_data(gadget); 102623cd1385SRemy Bohmer struct usb_request *req = dev->req; 102723cd1385SRemy Bohmer int value = -EOPNOTSUPP; 102823cd1385SRemy Bohmer u16 wIndex = le16_to_cpu(ctrl->wIndex); 102923cd1385SRemy Bohmer u16 wValue = le16_to_cpu(ctrl->wValue); 103023cd1385SRemy Bohmer u16 wLength = le16_to_cpu(ctrl->wLength); 103123cd1385SRemy Bohmer 1032*6142e0aeSVitaly Kuzmichev /* 1033*6142e0aeSVitaly Kuzmichev * descriptors just go into the pre-allocated ep0 buffer, 103423cd1385SRemy Bohmer * while config change events may enable network traffic. 103523cd1385SRemy Bohmer */ 103623cd1385SRemy Bohmer 10377de73185SVitaly Kuzmichev debug("%s\n", __func__); 103823cd1385SRemy Bohmer 103923cd1385SRemy Bohmer req->complete = eth_setup_complete; 104023cd1385SRemy Bohmer switch (ctrl->bRequest) { 104123cd1385SRemy Bohmer 104223cd1385SRemy Bohmer case USB_REQ_GET_DESCRIPTOR: 104323cd1385SRemy Bohmer if (ctrl->bRequestType != USB_DIR_IN) 104423cd1385SRemy Bohmer break; 104523cd1385SRemy Bohmer switch (wValue >> 8) { 104623cd1385SRemy Bohmer 104723cd1385SRemy Bohmer case USB_DT_DEVICE: 104823cd1385SRemy Bohmer value = min(wLength, (u16) sizeof device_desc); 104923cd1385SRemy Bohmer memcpy(req->buf, &device_desc, value); 105023cd1385SRemy Bohmer break; 105123cd1385SRemy Bohmer case USB_DT_DEVICE_QUALIFIER: 105223cd1385SRemy Bohmer if (!gadget_is_dualspeed(gadget)) 105323cd1385SRemy Bohmer break; 105423cd1385SRemy Bohmer value = min(wLength, (u16) sizeof dev_qualifier); 105523cd1385SRemy Bohmer memcpy(req->buf, &dev_qualifier, value); 105623cd1385SRemy Bohmer break; 105723cd1385SRemy Bohmer 105823cd1385SRemy Bohmer case USB_DT_OTHER_SPEED_CONFIG: 105923cd1385SRemy Bohmer if (!gadget_is_dualspeed(gadget)) 106023cd1385SRemy Bohmer break; 106123cd1385SRemy Bohmer /* FALLTHROUGH */ 106223cd1385SRemy Bohmer case USB_DT_CONFIG: 106323cd1385SRemy Bohmer value = config_buf(gadget, req->buf, 106423cd1385SRemy Bohmer wValue >> 8, 106523cd1385SRemy Bohmer wValue & 0xff, 106623cd1385SRemy Bohmer gadget_is_otg(gadget)); 106723cd1385SRemy Bohmer if (value >= 0) 106823cd1385SRemy Bohmer value = min(wLength, (u16) value); 106923cd1385SRemy Bohmer break; 107023cd1385SRemy Bohmer 107123cd1385SRemy Bohmer case USB_DT_STRING: 107223cd1385SRemy Bohmer value = usb_gadget_get_string(&stringtab, 107323cd1385SRemy Bohmer wValue & 0xff, req->buf); 107423cd1385SRemy Bohmer 107523cd1385SRemy Bohmer if (value >= 0) 107623cd1385SRemy Bohmer value = min(wLength, (u16) value); 107723cd1385SRemy Bohmer 107823cd1385SRemy Bohmer break; 107923cd1385SRemy Bohmer } 108023cd1385SRemy Bohmer break; 108123cd1385SRemy Bohmer 108223cd1385SRemy Bohmer case USB_REQ_SET_CONFIGURATION: 108323cd1385SRemy Bohmer if (ctrl->bRequestType != 0) 108423cd1385SRemy Bohmer break; 108523cd1385SRemy Bohmer if (gadget->a_hnp_support) 10867de73185SVitaly Kuzmichev debug("HNP available\n"); 108723cd1385SRemy Bohmer else if (gadget->a_alt_hnp_support) 10887de73185SVitaly Kuzmichev debug("HNP needs a different root port\n"); 108923cd1385SRemy Bohmer value = eth_set_config(dev, wValue, GFP_ATOMIC); 109023cd1385SRemy Bohmer break; 109123cd1385SRemy Bohmer case USB_REQ_GET_CONFIGURATION: 109223cd1385SRemy Bohmer if (ctrl->bRequestType != USB_DIR_IN) 109323cd1385SRemy Bohmer break; 109423cd1385SRemy Bohmer *(u8 *)req->buf = dev->config; 109523cd1385SRemy Bohmer value = min(wLength, (u16) 1); 109623cd1385SRemy Bohmer break; 109723cd1385SRemy Bohmer 109823cd1385SRemy Bohmer case USB_REQ_SET_INTERFACE: 109923cd1385SRemy Bohmer if (ctrl->bRequestType != USB_RECIP_INTERFACE 110023cd1385SRemy Bohmer || !dev->config 110123cd1385SRemy Bohmer || wIndex > 1) 110223cd1385SRemy Bohmer break; 110323cd1385SRemy Bohmer if (!cdc_active(dev) && wIndex != 0) 110423cd1385SRemy Bohmer break; 110523cd1385SRemy Bohmer 1106*6142e0aeSVitaly Kuzmichev /* 1107*6142e0aeSVitaly Kuzmichev * PXA hardware partially handles SET_INTERFACE; 110823cd1385SRemy Bohmer * we need to kluge around that interference. 110923cd1385SRemy Bohmer */ 111023cd1385SRemy Bohmer if (gadget_is_pxa(gadget)) { 111123cd1385SRemy Bohmer value = eth_set_config(dev, DEV_CONFIG_VALUE, 111223cd1385SRemy Bohmer GFP_ATOMIC); 111323cd1385SRemy Bohmer goto done_set_intf; 111423cd1385SRemy Bohmer } 111523cd1385SRemy Bohmer 111623cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC 111723cd1385SRemy Bohmer switch (wIndex) { 111823cd1385SRemy Bohmer case 0: /* control/master intf */ 111923cd1385SRemy Bohmer if (wValue != 0) 112023cd1385SRemy Bohmer break; 112123cd1385SRemy Bohmer if (dev->status) { 112223cd1385SRemy Bohmer usb_ep_disable(dev->status_ep); 112323cd1385SRemy Bohmer usb_ep_enable(dev->status_ep, dev->status); 112423cd1385SRemy Bohmer } 112523cd1385SRemy Bohmer value = 0; 112623cd1385SRemy Bohmer break; 112723cd1385SRemy Bohmer case 1: /* data intf */ 112823cd1385SRemy Bohmer if (wValue > 1) 112923cd1385SRemy Bohmer break; 113023cd1385SRemy Bohmer usb_ep_disable(dev->in_ep); 113123cd1385SRemy Bohmer usb_ep_disable(dev->out_ep); 113223cd1385SRemy Bohmer 1133*6142e0aeSVitaly Kuzmichev /* 1134*6142e0aeSVitaly Kuzmichev * CDC requires the data transfers not be done from 113523cd1385SRemy Bohmer * the default interface setting ... also, setting 113623cd1385SRemy Bohmer * the non-default interface resets filters etc. 113723cd1385SRemy Bohmer */ 113823cd1385SRemy Bohmer if (wValue == 1) { 113923cd1385SRemy Bohmer if (!cdc_active(dev)) 114023cd1385SRemy Bohmer break; 114123cd1385SRemy Bohmer usb_ep_enable(dev->in_ep, dev->in); 114223cd1385SRemy Bohmer usb_ep_enable(dev->out_ep, dev->out); 114323cd1385SRemy Bohmer dev->cdc_filter = DEFAULT_FILTER; 114423cd1385SRemy Bohmer if (dev->status) 114523cd1385SRemy Bohmer issue_start_status(dev); 114623cd1385SRemy Bohmer } 114723cd1385SRemy Bohmer 114823cd1385SRemy Bohmer value = 0; 114923cd1385SRemy Bohmer break; 115023cd1385SRemy Bohmer } 115123cd1385SRemy Bohmer #else 1152*6142e0aeSVitaly Kuzmichev /* 1153*6142e0aeSVitaly Kuzmichev * FIXME this is wrong, as is the assumption that 115423cd1385SRemy Bohmer * all non-PXA hardware talks real CDC ... 115523cd1385SRemy Bohmer */ 11567de73185SVitaly Kuzmichev debug("set_interface ignored!\n"); 115723cd1385SRemy Bohmer #endif /* DEV_CONFIG_CDC */ 115823cd1385SRemy Bohmer 115923cd1385SRemy Bohmer done_set_intf: 116023cd1385SRemy Bohmer break; 116123cd1385SRemy Bohmer case USB_REQ_GET_INTERFACE: 116223cd1385SRemy Bohmer if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE) 116323cd1385SRemy Bohmer || !dev->config 116423cd1385SRemy Bohmer || wIndex > 1) 116523cd1385SRemy Bohmer break; 116623cd1385SRemy Bohmer if (!(cdc_active(dev)) && wIndex != 0) 116723cd1385SRemy Bohmer break; 116823cd1385SRemy Bohmer 116923cd1385SRemy Bohmer /* for CDC, iff carrier is on, data interface is active. */ 117023cd1385SRemy Bohmer if (wIndex != 1) 117123cd1385SRemy Bohmer *(u8 *)req->buf = 0; 117223cd1385SRemy Bohmer else { 117323cd1385SRemy Bohmer /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */ 117423cd1385SRemy Bohmer /* carrier always ok ...*/ 117523cd1385SRemy Bohmer *(u8 *)req->buf = 1 ; 117623cd1385SRemy Bohmer } 117723cd1385SRemy Bohmer value = min(wLength, (u16) 1); 117823cd1385SRemy Bohmer break; 117923cd1385SRemy Bohmer 118023cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC 118123cd1385SRemy Bohmer case USB_CDC_SET_ETHERNET_PACKET_FILTER: 1182*6142e0aeSVitaly Kuzmichev /* 1183*6142e0aeSVitaly Kuzmichev * see 6.2.30: no data, wIndex = interface, 118423cd1385SRemy Bohmer * wValue = packet filter bitmap 118523cd1385SRemy Bohmer */ 118623cd1385SRemy Bohmer if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE) 118723cd1385SRemy Bohmer || !cdc_active(dev) 118823cd1385SRemy Bohmer || wLength != 0 118923cd1385SRemy Bohmer || wIndex > 1) 119023cd1385SRemy Bohmer break; 11917de73185SVitaly Kuzmichev debug("packet filter %02x\n", wValue); 119223cd1385SRemy Bohmer dev->cdc_filter = wValue; 119323cd1385SRemy Bohmer value = 0; 119423cd1385SRemy Bohmer break; 119523cd1385SRemy Bohmer 1196*6142e0aeSVitaly Kuzmichev /* 1197*6142e0aeSVitaly Kuzmichev * and potentially: 119823cd1385SRemy Bohmer * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS: 119923cd1385SRemy Bohmer * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER: 120023cd1385SRemy Bohmer * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER: 120123cd1385SRemy Bohmer * case USB_CDC_GET_ETHERNET_STATISTIC: 120223cd1385SRemy Bohmer */ 120323cd1385SRemy Bohmer 120423cd1385SRemy Bohmer #endif /* DEV_CONFIG_CDC */ 120523cd1385SRemy Bohmer 120623cd1385SRemy Bohmer default: 12077de73185SVitaly Kuzmichev debug("unknown control req%02x.%02x v%04x i%04x l%d\n", 120823cd1385SRemy Bohmer ctrl->bRequestType, ctrl->bRequest, 120923cd1385SRemy Bohmer wValue, wIndex, wLength); 121023cd1385SRemy Bohmer } 121123cd1385SRemy Bohmer 121223cd1385SRemy Bohmer /* respond with data transfer before status phase? */ 121323cd1385SRemy Bohmer if (value >= 0) { 12147de73185SVitaly Kuzmichev debug("respond with data transfer before status phase\n"); 121523cd1385SRemy Bohmer req->length = value; 121623cd1385SRemy Bohmer req->zero = value < wLength 121723cd1385SRemy Bohmer && (value % gadget->ep0->maxpacket) == 0; 121823cd1385SRemy Bohmer value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC); 121923cd1385SRemy Bohmer if (value < 0) { 12207de73185SVitaly Kuzmichev debug("ep_queue --> %d\n", value); 122123cd1385SRemy Bohmer req->status = 0; 122223cd1385SRemy Bohmer eth_setup_complete(gadget->ep0, req); 122323cd1385SRemy Bohmer } 122423cd1385SRemy Bohmer } 122523cd1385SRemy Bohmer 122623cd1385SRemy Bohmer /* host either stalls (value < 0) or reports success */ 122723cd1385SRemy Bohmer return value; 122823cd1385SRemy Bohmer } 122923cd1385SRemy Bohmer 123023cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 123123cd1385SRemy Bohmer 123223cd1385SRemy Bohmer static void rx_complete(struct usb_ep *ep, struct usb_request *req); 123323cd1385SRemy Bohmer 1234*6142e0aeSVitaly Kuzmichev static int rx_submit(struct eth_dev *dev, struct usb_request *req, 123523cd1385SRemy Bohmer gfp_t gfp_flags) 123623cd1385SRemy Bohmer { 123723cd1385SRemy Bohmer int retval = -ENOMEM; 123823cd1385SRemy Bohmer size_t size; 123923cd1385SRemy Bohmer 1240*6142e0aeSVitaly Kuzmichev /* 1241*6142e0aeSVitaly Kuzmichev * Padding up to RX_EXTRA handles minor disagreements with host. 124223cd1385SRemy Bohmer * Normally we use the USB "terminate on short read" convention; 124323cd1385SRemy Bohmer * so allow up to (N*maxpacket), since that memory is normally 124423cd1385SRemy Bohmer * already allocated. Some hardware doesn't deal well with short 124523cd1385SRemy Bohmer * reads (e.g. DMA must be N*maxpacket), so for now don't trim a 124623cd1385SRemy Bohmer * byte off the end (to force hardware errors on overflow). 124723cd1385SRemy Bohmer */ 124823cd1385SRemy Bohmer 12497de73185SVitaly Kuzmichev debug("%s\n", __func__); 125023cd1385SRemy Bohmer 125123cd1385SRemy Bohmer size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA); 125223cd1385SRemy Bohmer size += dev->out_ep->maxpacket - 1; 125323cd1385SRemy Bohmer size -= size % dev->out_ep->maxpacket; 125423cd1385SRemy Bohmer 1255*6142e0aeSVitaly Kuzmichev /* 1256*6142e0aeSVitaly Kuzmichev * Some platforms perform better when IP packets are aligned, 125723cd1385SRemy Bohmer * but on at least one, checksumming fails otherwise. 125823cd1385SRemy Bohmer */ 125923cd1385SRemy Bohmer 126023cd1385SRemy Bohmer req->buf = (u8 *) NetRxPackets[0]; 126123cd1385SRemy Bohmer req->length = size; 126223cd1385SRemy Bohmer req->complete = rx_complete; 126323cd1385SRemy Bohmer 126423cd1385SRemy Bohmer retval = usb_ep_queue(dev->out_ep, req, gfp_flags); 126523cd1385SRemy Bohmer 1266*6142e0aeSVitaly Kuzmichev if (retval) 12677de73185SVitaly Kuzmichev error("rx submit --> %d", retval); 1268*6142e0aeSVitaly Kuzmichev 126923cd1385SRemy Bohmer return retval; 127023cd1385SRemy Bohmer } 127123cd1385SRemy Bohmer 127223cd1385SRemy Bohmer static void rx_complete(struct usb_ep *ep, struct usb_request *req) 127323cd1385SRemy Bohmer { 127423cd1385SRemy Bohmer struct eth_dev *dev = ep->driver_data; 127523cd1385SRemy Bohmer 12767de73185SVitaly Kuzmichev debug("%s: status %d\n", __func__, req->status); 127723cd1385SRemy Bohmer 127823cd1385SRemy Bohmer packet_received = 1; 127923cd1385SRemy Bohmer 128023cd1385SRemy Bohmer if (req) 128123cd1385SRemy Bohmer dev->rx_req = req; 128223cd1385SRemy Bohmer } 128323cd1385SRemy Bohmer 128423cd1385SRemy Bohmer static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags) 128523cd1385SRemy Bohmer { 128623cd1385SRemy Bohmer 128723cd1385SRemy Bohmer dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0); 128823cd1385SRemy Bohmer 128923cd1385SRemy Bohmer if (!dev->tx_req) 129023cd1385SRemy Bohmer goto fail; 129123cd1385SRemy Bohmer 129223cd1385SRemy Bohmer dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0); 129323cd1385SRemy Bohmer 129423cd1385SRemy Bohmer if (!dev->rx_req) 129523cd1385SRemy Bohmer goto fail; 129623cd1385SRemy Bohmer 129723cd1385SRemy Bohmer return 0; 129823cd1385SRemy Bohmer 129923cd1385SRemy Bohmer fail: 13007de73185SVitaly Kuzmichev error("can't alloc requests"); 130123cd1385SRemy Bohmer return -1; 130223cd1385SRemy Bohmer } 130323cd1385SRemy Bohmer 130423cd1385SRemy Bohmer static void tx_complete(struct usb_ep *ep, struct usb_request *req) 130523cd1385SRemy Bohmer { 13067de73185SVitaly Kuzmichev debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok"); 130723cd1385SRemy Bohmer packet_sent = 1; 130823cd1385SRemy Bohmer } 130923cd1385SRemy Bohmer 131023cd1385SRemy Bohmer static inline int eth_is_promisc(struct eth_dev *dev) 131123cd1385SRemy Bohmer { 131223cd1385SRemy Bohmer /* no filters for the CDC subset; always promisc */ 131323cd1385SRemy Bohmer if (subset_active(dev)) 131423cd1385SRemy Bohmer return 1; 131523cd1385SRemy Bohmer return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS; 131623cd1385SRemy Bohmer } 131723cd1385SRemy Bohmer 131823cd1385SRemy Bohmer #if 0 131923cd1385SRemy Bohmer static int eth_start_xmit (struct sk_buff *skb, struct net_device *net) 132023cd1385SRemy Bohmer { 132123cd1385SRemy Bohmer struct eth_dev *dev = netdev_priv(net); 132223cd1385SRemy Bohmer int length = skb->len; 132323cd1385SRemy Bohmer int retval; 132423cd1385SRemy Bohmer struct usb_request *req = NULL; 132523cd1385SRemy Bohmer unsigned long flags; 132623cd1385SRemy Bohmer 132723cd1385SRemy Bohmer /* apply outgoing CDC or RNDIS filters */ 132823cd1385SRemy Bohmer if (!eth_is_promisc (dev)) { 132923cd1385SRemy Bohmer u8 *dest = skb->data; 133023cd1385SRemy Bohmer 133123cd1385SRemy Bohmer if (is_multicast_ether_addr(dest)) { 133223cd1385SRemy Bohmer u16 type; 133323cd1385SRemy Bohmer 133423cd1385SRemy Bohmer /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host 133523cd1385SRemy Bohmer * SET_ETHERNET_MULTICAST_FILTERS requests 133623cd1385SRemy Bohmer */ 133723cd1385SRemy Bohmer if (is_broadcast_ether_addr(dest)) 133823cd1385SRemy Bohmer type = USB_CDC_PACKET_TYPE_BROADCAST; 133923cd1385SRemy Bohmer else 134023cd1385SRemy Bohmer type = USB_CDC_PACKET_TYPE_ALL_MULTICAST; 134123cd1385SRemy Bohmer if (!(dev->cdc_filter & type)) { 134223cd1385SRemy Bohmer dev_kfree_skb_any (skb); 134323cd1385SRemy Bohmer return 0; 134423cd1385SRemy Bohmer } 134523cd1385SRemy Bohmer } 134623cd1385SRemy Bohmer /* ignores USB_CDC_PACKET_TYPE_DIRECTED */ 134723cd1385SRemy Bohmer } 134823cd1385SRemy Bohmer 134923cd1385SRemy Bohmer spin_lock_irqsave(&dev->req_lock, flags); 135023cd1385SRemy Bohmer /* 135123cd1385SRemy Bohmer * this freelist can be empty if an interrupt triggered disconnect() 135223cd1385SRemy Bohmer * and reconfigured the gadget (shutting down this queue) after the 135323cd1385SRemy Bohmer * network stack decided to xmit but before we got the spinlock. 135423cd1385SRemy Bohmer */ 135523cd1385SRemy Bohmer if (list_empty(&dev->tx_reqs)) { 135623cd1385SRemy Bohmer spin_unlock_irqrestore(&dev->req_lock, flags); 135723cd1385SRemy Bohmer return 1; 135823cd1385SRemy Bohmer } 135923cd1385SRemy Bohmer 136023cd1385SRemy Bohmer req = container_of (dev->tx_reqs.next, struct usb_request, list); 136123cd1385SRemy Bohmer list_del (&req->list); 136223cd1385SRemy Bohmer 136323cd1385SRemy Bohmer /* temporarily stop TX queue when the freelist empties */ 136423cd1385SRemy Bohmer if (list_empty (&dev->tx_reqs)) 136523cd1385SRemy Bohmer netif_stop_queue (net); 136623cd1385SRemy Bohmer spin_unlock_irqrestore(&dev->req_lock, flags); 136723cd1385SRemy Bohmer 136823cd1385SRemy Bohmer /* no buffer copies needed, unless the network stack did it 136923cd1385SRemy Bohmer * or the hardware can't use skb buffers. 137023cd1385SRemy Bohmer * or there's not enough space for any RNDIS headers we need 137123cd1385SRemy Bohmer */ 137223cd1385SRemy Bohmer if (rndis_active(dev)) { 137323cd1385SRemy Bohmer struct sk_buff *skb_rndis; 137423cd1385SRemy Bohmer 137523cd1385SRemy Bohmer skb_rndis = skb_realloc_headroom (skb, 137623cd1385SRemy Bohmer sizeof (struct rndis_packet_msg_type)); 137723cd1385SRemy Bohmer if (!skb_rndis) 137823cd1385SRemy Bohmer goto drop; 137923cd1385SRemy Bohmer 138023cd1385SRemy Bohmer dev_kfree_skb_any (skb); 138123cd1385SRemy Bohmer skb = skb_rndis; 138223cd1385SRemy Bohmer rndis_add_hdr (skb); 138323cd1385SRemy Bohmer length = skb->len; 138423cd1385SRemy Bohmer } 138523cd1385SRemy Bohmer req->buf = skb->data; 138623cd1385SRemy Bohmer req->context = skb; 138723cd1385SRemy Bohmer req->complete = tx_complete; 138823cd1385SRemy Bohmer 138923cd1385SRemy Bohmer /* use zlp framing on tx for strict CDC-Ether conformance, 139023cd1385SRemy Bohmer * though any robust network rx path ignores extra padding. 139123cd1385SRemy Bohmer * and some hardware doesn't like to write zlps. 139223cd1385SRemy Bohmer */ 139323cd1385SRemy Bohmer req->zero = 1; 139423cd1385SRemy Bohmer if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0) 139523cd1385SRemy Bohmer length++; 139623cd1385SRemy Bohmer 139723cd1385SRemy Bohmer req->length = length; 139823cd1385SRemy Bohmer 139923cd1385SRemy Bohmer /* throttle highspeed IRQ rate back slightly */ 140023cd1385SRemy Bohmer if (gadget_is_dualspeed(dev->gadget)) 140123cd1385SRemy Bohmer req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH) 140223cd1385SRemy Bohmer ? ((atomic_read(&dev->tx_qlen) % qmult) != 0) 140323cd1385SRemy Bohmer : 0; 140423cd1385SRemy Bohmer 140523cd1385SRemy Bohmer retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC); 140623cd1385SRemy Bohmer switch (retval) { 140723cd1385SRemy Bohmer default: 140823cd1385SRemy Bohmer DEBUG (dev, "tx queue err %d\n", retval); 140923cd1385SRemy Bohmer break; 141023cd1385SRemy Bohmer case 0: 141123cd1385SRemy Bohmer net->trans_start = jiffies; 141223cd1385SRemy Bohmer atomic_inc (&dev->tx_qlen); 141323cd1385SRemy Bohmer } 141423cd1385SRemy Bohmer 141523cd1385SRemy Bohmer if (retval) { 141623cd1385SRemy Bohmer drop: 141723cd1385SRemy Bohmer dev->stats.tx_dropped++; 141823cd1385SRemy Bohmer dev_kfree_skb_any (skb); 141923cd1385SRemy Bohmer spin_lock_irqsave(&dev->req_lock, flags); 142023cd1385SRemy Bohmer if (list_empty (&dev->tx_reqs)) 142123cd1385SRemy Bohmer netif_start_queue (net); 142223cd1385SRemy Bohmer list_add (&req->list, &dev->tx_reqs); 142323cd1385SRemy Bohmer spin_unlock_irqrestore(&dev->req_lock, flags); 142423cd1385SRemy Bohmer } 142523cd1385SRemy Bohmer return 0; 142623cd1385SRemy Bohmer } 142723cd1385SRemy Bohmer 142823cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 142923cd1385SRemy Bohmer #endif 143023cd1385SRemy Bohmer 143123cd1385SRemy Bohmer static void eth_unbind(struct usb_gadget *gadget) 143223cd1385SRemy Bohmer { 143323cd1385SRemy Bohmer struct eth_dev *dev = get_gadget_data(gadget); 143423cd1385SRemy Bohmer 14357de73185SVitaly Kuzmichev debug("%s...\n", __func__); 143623cd1385SRemy Bohmer 14370129e327SVitaly Kuzmichev /* we've already been disconnected ... no i/o is active */ 14380129e327SVitaly Kuzmichev if (dev->req) { 14390129e327SVitaly Kuzmichev usb_ep_free_request(gadget->ep0, dev->req); 14400129e327SVitaly Kuzmichev dev->req = NULL; 14410129e327SVitaly Kuzmichev } 144223cd1385SRemy Bohmer if (dev->stat_req) { 144323cd1385SRemy Bohmer usb_ep_free_request(dev->status_ep, dev->stat_req); 144423cd1385SRemy Bohmer dev->stat_req = NULL; 144523cd1385SRemy Bohmer } 144623cd1385SRemy Bohmer 144723cd1385SRemy Bohmer if (dev->tx_req) { 144823cd1385SRemy Bohmer usb_ep_free_request(dev->in_ep, dev->tx_req); 144923cd1385SRemy Bohmer dev->tx_req = NULL; 145023cd1385SRemy Bohmer } 145123cd1385SRemy Bohmer 145223cd1385SRemy Bohmer if (dev->rx_req) { 14530129e327SVitaly Kuzmichev usb_ep_free_request(dev->out_ep, dev->rx_req); 145423cd1385SRemy Bohmer dev->rx_req = NULL; 145523cd1385SRemy Bohmer } 145623cd1385SRemy Bohmer 145723cd1385SRemy Bohmer /* unregister_netdev (dev->net);*/ 145823cd1385SRemy Bohmer /* free_netdev(dev->net);*/ 145923cd1385SRemy Bohmer 146023cd1385SRemy Bohmer set_gadget_data(gadget, NULL); 146123cd1385SRemy Bohmer } 146223cd1385SRemy Bohmer 146323cd1385SRemy Bohmer static void eth_disconnect(struct usb_gadget *gadget) 146423cd1385SRemy Bohmer { 146523cd1385SRemy Bohmer eth_reset_config(get_gadget_data(gadget)); 146623cd1385SRemy Bohmer } 146723cd1385SRemy Bohmer 146823cd1385SRemy Bohmer static void eth_suspend(struct usb_gadget *gadget) 146923cd1385SRemy Bohmer { 147023cd1385SRemy Bohmer /* Not used */ 147123cd1385SRemy Bohmer } 147223cd1385SRemy Bohmer 147323cd1385SRemy Bohmer static void eth_resume(struct usb_gadget *gadget) 147423cd1385SRemy Bohmer { 147523cd1385SRemy Bohmer /* Not used */ 147623cd1385SRemy Bohmer } 147723cd1385SRemy Bohmer 147823cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 147923cd1385SRemy Bohmer 148023cd1385SRemy Bohmer static int is_eth_addr_valid(char *str) 148123cd1385SRemy Bohmer { 148223cd1385SRemy Bohmer if (strlen(str) == 17) { 148323cd1385SRemy Bohmer int i; 148423cd1385SRemy Bohmer char *p, *q; 148523cd1385SRemy Bohmer uchar ea[6]; 148623cd1385SRemy Bohmer 148723cd1385SRemy Bohmer /* see if it looks like an ethernet address */ 148823cd1385SRemy Bohmer 148923cd1385SRemy Bohmer p = str; 149023cd1385SRemy Bohmer 149123cd1385SRemy Bohmer for (i = 0; i < 6; i++) { 149223cd1385SRemy Bohmer char term = (i == 5 ? '\0' : ':'); 149323cd1385SRemy Bohmer 149423cd1385SRemy Bohmer ea[i] = simple_strtol(p, &q, 16); 149523cd1385SRemy Bohmer 149623cd1385SRemy Bohmer if ((q - p) != 2 || *q++ != term) 149723cd1385SRemy Bohmer break; 149823cd1385SRemy Bohmer 149923cd1385SRemy Bohmer p = q; 150023cd1385SRemy Bohmer } 150123cd1385SRemy Bohmer 150223cd1385SRemy Bohmer if (i == 6) /* it looks ok */ 150323cd1385SRemy Bohmer return 1; 150423cd1385SRemy Bohmer } 150523cd1385SRemy Bohmer return 0; 150623cd1385SRemy Bohmer } 150723cd1385SRemy Bohmer 150823cd1385SRemy Bohmer static u8 nibble(unsigned char c) 150923cd1385SRemy Bohmer { 151023cd1385SRemy Bohmer if (likely(isdigit(c))) 151123cd1385SRemy Bohmer return c - '0'; 151223cd1385SRemy Bohmer c = toupper(c); 151323cd1385SRemy Bohmer if (likely(isxdigit(c))) 151423cd1385SRemy Bohmer return 10 + c - 'A'; 151523cd1385SRemy Bohmer return 0; 151623cd1385SRemy Bohmer } 151723cd1385SRemy Bohmer 151823cd1385SRemy Bohmer static int get_ether_addr(const char *str, u8 *dev_addr) 151923cd1385SRemy Bohmer { 152023cd1385SRemy Bohmer if (str) { 152123cd1385SRemy Bohmer unsigned i; 152223cd1385SRemy Bohmer 152323cd1385SRemy Bohmer for (i = 0; i < 6; i++) { 152423cd1385SRemy Bohmer unsigned char num; 152523cd1385SRemy Bohmer 152623cd1385SRemy Bohmer if ((*str == '.') || (*str == ':')) 152723cd1385SRemy Bohmer str++; 152823cd1385SRemy Bohmer num = nibble(*str++) << 4; 152923cd1385SRemy Bohmer num |= (nibble(*str++)); 153023cd1385SRemy Bohmer dev_addr[i] = num; 153123cd1385SRemy Bohmer } 153223cd1385SRemy Bohmer if (is_valid_ether_addr(dev_addr)) 153323cd1385SRemy Bohmer return 0; 153423cd1385SRemy Bohmer } 153523cd1385SRemy Bohmer return 1; 153623cd1385SRemy Bohmer } 153723cd1385SRemy Bohmer 153823cd1385SRemy Bohmer static int eth_bind(struct usb_gadget *gadget) 153923cd1385SRemy Bohmer { 154023cd1385SRemy Bohmer struct eth_dev *dev = &l_ethdev; 154123cd1385SRemy Bohmer u8 cdc = 1, zlp = 1; 154223cd1385SRemy Bohmer struct usb_ep *in_ep, *out_ep, *status_ep = NULL; 154323cd1385SRemy Bohmer int gcnum; 154423cd1385SRemy Bohmer u8 tmp[7]; 154523cd1385SRemy Bohmer 154623cd1385SRemy Bohmer /* these flags are only ever cleared; compiler take note */ 154723cd1385SRemy Bohmer #ifndef DEV_CONFIG_CDC 154823cd1385SRemy Bohmer cdc = 0; 154923cd1385SRemy Bohmer #endif 1550*6142e0aeSVitaly Kuzmichev /* 1551*6142e0aeSVitaly Kuzmichev * Because most host side USB stacks handle CDC Ethernet, that 155223cd1385SRemy Bohmer * standard protocol is _strongly_ preferred for interop purposes. 155323cd1385SRemy Bohmer * (By everyone except Microsoft.) 155423cd1385SRemy Bohmer */ 155523cd1385SRemy Bohmer if (gadget_is_pxa(gadget)) { 155623cd1385SRemy Bohmer /* pxa doesn't support altsettings */ 155723cd1385SRemy Bohmer cdc = 0; 155823cd1385SRemy Bohmer } else if (gadget_is_musbhdrc(gadget)) { 155923cd1385SRemy Bohmer /* reduce tx dma overhead by avoiding special cases */ 156023cd1385SRemy Bohmer zlp = 0; 156123cd1385SRemy Bohmer } else if (gadget_is_sh(gadget)) { 156223cd1385SRemy Bohmer /* sh doesn't support multiple interfaces or configs */ 156323cd1385SRemy Bohmer cdc = 0; 156423cd1385SRemy Bohmer } else if (gadget_is_sa1100(gadget)) { 156523cd1385SRemy Bohmer /* hardware can't write zlps */ 156623cd1385SRemy Bohmer zlp = 0; 1567*6142e0aeSVitaly Kuzmichev /* 1568*6142e0aeSVitaly Kuzmichev * sa1100 CAN do CDC, without status endpoint ... we use 156923cd1385SRemy Bohmer * non-CDC to be compatible with ARM Linux-2.4 "usb-eth". 157023cd1385SRemy Bohmer */ 157123cd1385SRemy Bohmer cdc = 0; 157223cd1385SRemy Bohmer } 157323cd1385SRemy Bohmer 157423cd1385SRemy Bohmer gcnum = usb_gadget_controller_number(gadget); 157523cd1385SRemy Bohmer if (gcnum >= 0) 157623cd1385SRemy Bohmer device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum); 157723cd1385SRemy Bohmer else { 1578*6142e0aeSVitaly Kuzmichev /* 1579*6142e0aeSVitaly Kuzmichev * can't assume CDC works. don't want to default to 158023cd1385SRemy Bohmer * anything less functional on CDC-capable hardware, 158123cd1385SRemy Bohmer * so we fail in this case. 158223cd1385SRemy Bohmer */ 15837de73185SVitaly Kuzmichev error("controller '%s' not recognized", 158423cd1385SRemy Bohmer gadget->name); 158523cd1385SRemy Bohmer return -ENODEV; 158623cd1385SRemy Bohmer } 158723cd1385SRemy Bohmer 1588*6142e0aeSVitaly Kuzmichev /* 1589*6142e0aeSVitaly Kuzmichev * CDC subset ... recognized by Linux since 2.4.10, but Windows 159023cd1385SRemy Bohmer * drivers aren't widely available. (That may be improved by 159123cd1385SRemy Bohmer * supporting one submode of the "SAFE" variant of MDLM.) 159223cd1385SRemy Bohmer */ 159323cd1385SRemy Bohmer if (!cdc) { 159423cd1385SRemy Bohmer device_desc.idVendor = 159523cd1385SRemy Bohmer __constant_cpu_to_le16(SIMPLE_VENDOR_NUM); 159623cd1385SRemy Bohmer device_desc.idProduct = 159723cd1385SRemy Bohmer __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM); 159823cd1385SRemy Bohmer } 159923cd1385SRemy Bohmer 160023cd1385SRemy Bohmer /* support optional vendor/distro customization */ 160123cd1385SRemy Bohmer #if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID) 160223cd1385SRemy Bohmer device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID); 160323cd1385SRemy Bohmer device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID); 160423cd1385SRemy Bohmer #endif 160523cd1385SRemy Bohmer if (bcdDevice) 160623cd1385SRemy Bohmer device_desc.bcdDevice = cpu_to_le16(bcdDevice); 160723cd1385SRemy Bohmer if (iManufacturer) 16082e12abe6SVitaly Kuzmichev strlcpy(manufacturer, iManufacturer, sizeof manufacturer); 160923cd1385SRemy Bohmer if (iProduct) 16102e12abe6SVitaly Kuzmichev strlcpy(product_desc, iProduct, sizeof product_desc); 161123cd1385SRemy Bohmer if (iSerialNumber) { 161223cd1385SRemy Bohmer device_desc.iSerialNumber = STRING_SERIALNUMBER, 16132e12abe6SVitaly Kuzmichev strlcpy(serial_number, iSerialNumber, sizeof serial_number); 161423cd1385SRemy Bohmer } 161523cd1385SRemy Bohmer 161623cd1385SRemy Bohmer /* all we really need is bulk IN/OUT */ 161723cd1385SRemy Bohmer usb_ep_autoconfig_reset(gadget); 161823cd1385SRemy Bohmer in_ep = usb_ep_autoconfig(gadget, &fs_source_desc); 161923cd1385SRemy Bohmer if (!in_ep) { 162023cd1385SRemy Bohmer autoconf_fail: 16217de73185SVitaly Kuzmichev error("can't autoconfigure on %s\n", 162223cd1385SRemy Bohmer gadget->name); 162323cd1385SRemy Bohmer return -ENODEV; 162423cd1385SRemy Bohmer } 162523cd1385SRemy Bohmer in_ep->driver_data = in_ep; /* claim */ 162623cd1385SRemy Bohmer 162723cd1385SRemy Bohmer out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc); 162823cd1385SRemy Bohmer if (!out_ep) 162923cd1385SRemy Bohmer goto autoconf_fail; 163023cd1385SRemy Bohmer out_ep->driver_data = out_ep; /* claim */ 163123cd1385SRemy Bohmer 163223cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC) 1633*6142e0aeSVitaly Kuzmichev /* 1634*6142e0aeSVitaly Kuzmichev * CDC Ethernet control interface doesn't require a status endpoint. 163523cd1385SRemy Bohmer * Since some hosts expect one, try to allocate one anyway. 163623cd1385SRemy Bohmer */ 163723cd1385SRemy Bohmer if (cdc) { 163823cd1385SRemy Bohmer status_ep = usb_ep_autoconfig(gadget, &fs_status_desc); 163923cd1385SRemy Bohmer if (status_ep) { 164023cd1385SRemy Bohmer status_ep->driver_data = status_ep; /* claim */ 164123cd1385SRemy Bohmer } else if (cdc) { 164223cd1385SRemy Bohmer control_intf.bNumEndpoints = 0; 164323cd1385SRemy Bohmer /* FIXME remove endpoint from descriptor list */ 164423cd1385SRemy Bohmer } 164523cd1385SRemy Bohmer } 164623cd1385SRemy Bohmer #endif 164723cd1385SRemy Bohmer 164823cd1385SRemy Bohmer /* one config: cdc, else minimal subset */ 164923cd1385SRemy Bohmer if (!cdc) { 165023cd1385SRemy Bohmer eth_config.bNumInterfaces = 1; 165123cd1385SRemy Bohmer eth_config.iConfiguration = STRING_SUBSET; 165223cd1385SRemy Bohmer 1653*6142e0aeSVitaly Kuzmichev /* 1654*6142e0aeSVitaly Kuzmichev * use functions to set these up, in case we're built to work 165523cd1385SRemy Bohmer * with multiple controllers and must override CDC Ethernet. 165623cd1385SRemy Bohmer */ 165723cd1385SRemy Bohmer fs_subset_descriptors(); 165823cd1385SRemy Bohmer hs_subset_descriptors(); 165923cd1385SRemy Bohmer } 166023cd1385SRemy Bohmer 166123cd1385SRemy Bohmer device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket; 166223cd1385SRemy Bohmer usb_gadget_set_selfpowered(gadget); 166323cd1385SRemy Bohmer 166423cd1385SRemy Bohmer if (gadget_is_dualspeed(gadget)) { 166523cd1385SRemy Bohmer if (!cdc) 166623cd1385SRemy Bohmer dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC; 166723cd1385SRemy Bohmer 166823cd1385SRemy Bohmer /* assumes ep0 uses the same value for both speeds ... */ 166923cd1385SRemy Bohmer dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0; 167023cd1385SRemy Bohmer 167123cd1385SRemy Bohmer /* and that all endpoints are dual-speed */ 167223cd1385SRemy Bohmer hs_source_desc.bEndpointAddress = 167323cd1385SRemy Bohmer fs_source_desc.bEndpointAddress; 167423cd1385SRemy Bohmer hs_sink_desc.bEndpointAddress = 167523cd1385SRemy Bohmer fs_sink_desc.bEndpointAddress; 167623cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC) 167723cd1385SRemy Bohmer if (status_ep) 167823cd1385SRemy Bohmer hs_status_desc.bEndpointAddress = 167923cd1385SRemy Bohmer fs_status_desc.bEndpointAddress; 168023cd1385SRemy Bohmer #endif 168123cd1385SRemy Bohmer } 168223cd1385SRemy Bohmer 168323cd1385SRemy Bohmer if (gadget_is_otg(gadget)) { 168423cd1385SRemy Bohmer otg_descriptor.bmAttributes |= USB_OTG_HNP, 168523cd1385SRemy Bohmer eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP; 168623cd1385SRemy Bohmer eth_config.bMaxPower = 4; 168723cd1385SRemy Bohmer } 168823cd1385SRemy Bohmer 168923cd1385SRemy Bohmer dev->net = &l_netdev; 169023cd1385SRemy Bohmer strcpy(dev->net->name, USB_NET_NAME); 169123cd1385SRemy Bohmer 169223cd1385SRemy Bohmer dev->cdc = cdc; 169323cd1385SRemy Bohmer dev->zlp = zlp; 169423cd1385SRemy Bohmer 169523cd1385SRemy Bohmer dev->in_ep = in_ep; 169623cd1385SRemy Bohmer dev->out_ep = out_ep; 169723cd1385SRemy Bohmer dev->status_ep = status_ep; 169823cd1385SRemy Bohmer 1699*6142e0aeSVitaly Kuzmichev /* 1700*6142e0aeSVitaly Kuzmichev * Module params for these addresses should come from ID proms. 170123cd1385SRemy Bohmer * The host side address is used with CDC, and commonly 170223cd1385SRemy Bohmer * ends up in a persistent config database. It's not clear if 170323cd1385SRemy Bohmer * host side code for the SAFE thing cares -- its original BLAN 170423cd1385SRemy Bohmer * thing didn't, Sharp never assigned those addresses on Zaurii. 170523cd1385SRemy Bohmer */ 170623cd1385SRemy Bohmer get_ether_addr(dev_addr, dev->net->enetaddr); 170723cd1385SRemy Bohmer 170823cd1385SRemy Bohmer memset(tmp, 0, sizeof(tmp)); 170923cd1385SRemy Bohmer memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr)); 171023cd1385SRemy Bohmer 171123cd1385SRemy Bohmer get_ether_addr(host_addr, dev->host_mac); 171223cd1385SRemy Bohmer 171323cd1385SRemy Bohmer sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X", 171423cd1385SRemy Bohmer dev->host_mac[0], dev->host_mac[1], 171523cd1385SRemy Bohmer dev->host_mac[2], dev->host_mac[3], 171623cd1385SRemy Bohmer dev->host_mac[4], dev->host_mac[5]); 171723cd1385SRemy Bohmer 17187de73185SVitaly Kuzmichev printf("using %s, OUT %s IN %s%s%s\n", gadget->name, 171923cd1385SRemy Bohmer out_ep->name, in_ep->name, 172023cd1385SRemy Bohmer status_ep ? " STATUS " : "", 172123cd1385SRemy Bohmer status_ep ? status_ep->name : "" 172223cd1385SRemy Bohmer ); 17237de73185SVitaly Kuzmichev printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n", 172423cd1385SRemy Bohmer dev->net->enetaddr[0], dev->net->enetaddr[1], 172523cd1385SRemy Bohmer dev->net->enetaddr[2], dev->net->enetaddr[3], 172623cd1385SRemy Bohmer dev->net->enetaddr[4], dev->net->enetaddr[5]); 172723cd1385SRemy Bohmer 172823cd1385SRemy Bohmer if (cdc) { 17297de73185SVitaly Kuzmichev printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n", 173023cd1385SRemy Bohmer dev->host_mac[0], dev->host_mac[1], 173123cd1385SRemy Bohmer dev->host_mac[2], dev->host_mac[3], 173223cd1385SRemy Bohmer dev->host_mac[4], dev->host_mac[5]); 173323cd1385SRemy Bohmer } 173423cd1385SRemy Bohmer 1735*6142e0aeSVitaly Kuzmichev /* 1736*6142e0aeSVitaly Kuzmichev * use PKTSIZE (or aligned... from u-boot) and set 1737*6142e0aeSVitaly Kuzmichev * wMaxSegmentSize accordingly 1738*6142e0aeSVitaly Kuzmichev */ 173923cd1385SRemy Bohmer dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/ 174023cd1385SRemy Bohmer 174123cd1385SRemy Bohmer /* preallocate control message data and buffer */ 174223cd1385SRemy Bohmer dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL); 174323cd1385SRemy Bohmer if (!dev->req) 174423cd1385SRemy Bohmer goto fail; 174523cd1385SRemy Bohmer dev->req->buf = control_req; 174623cd1385SRemy Bohmer dev->req->complete = eth_setup_complete; 174723cd1385SRemy Bohmer 174823cd1385SRemy Bohmer /* ... and maybe likewise for status transfer */ 174923cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC) 175023cd1385SRemy Bohmer if (dev->status_ep) { 1751*6142e0aeSVitaly Kuzmichev dev->stat_req = usb_ep_alloc_request(dev->status_ep, 1752*6142e0aeSVitaly Kuzmichev GFP_KERNEL); 175323cd1385SRemy Bohmer if (!dev->stat_req) { 1754df559c1dSVitaly Kuzmichev usb_ep_free_request(dev->status_ep, dev->req); 175523cd1385SRemy Bohmer 175623cd1385SRemy Bohmer goto fail; 175723cd1385SRemy Bohmer } 1758df559c1dSVitaly Kuzmichev dev->stat_req->buf = status_req; 175923cd1385SRemy Bohmer dev->stat_req->context = NULL; 176023cd1385SRemy Bohmer } 176123cd1385SRemy Bohmer #endif 176223cd1385SRemy Bohmer 176323cd1385SRemy Bohmer /* finish hookup to lower layer ... */ 176423cd1385SRemy Bohmer dev->gadget = gadget; 176523cd1385SRemy Bohmer set_gadget_data(gadget, dev); 176623cd1385SRemy Bohmer gadget->ep0->driver_data = dev; 176723cd1385SRemy Bohmer 1768*6142e0aeSVitaly Kuzmichev /* 1769*6142e0aeSVitaly Kuzmichev * two kinds of host-initiated state changes: 177023cd1385SRemy Bohmer * - iff DATA transfer is active, carrier is "on" 177123cd1385SRemy Bohmer * - tx queueing enabled if open *and* carrier is "on" 177223cd1385SRemy Bohmer */ 177323cd1385SRemy Bohmer return 0; 177423cd1385SRemy Bohmer 177523cd1385SRemy Bohmer fail: 17767de73185SVitaly Kuzmichev error("%s failed", __func__); 177723cd1385SRemy Bohmer eth_unbind(gadget); 177823cd1385SRemy Bohmer return -ENOMEM; 177923cd1385SRemy Bohmer } 178023cd1385SRemy Bohmer 178123cd1385SRemy Bohmer static int usb_eth_init(struct eth_device *netdev, bd_t *bd) 178223cd1385SRemy Bohmer { 178323cd1385SRemy Bohmer struct eth_dev *dev = &l_ethdev; 178423cd1385SRemy Bohmer struct usb_gadget *gadget; 178523cd1385SRemy Bohmer unsigned long ts; 178623cd1385SRemy Bohmer unsigned long timeout = USB_CONNECT_TIMEOUT; 178723cd1385SRemy Bohmer 178823cd1385SRemy Bohmer if (!netdev) { 17897de73185SVitaly Kuzmichev error("received NULL ptr"); 179023cd1385SRemy Bohmer goto fail; 179123cd1385SRemy Bohmer } 179223cd1385SRemy Bohmer 179323cd1385SRemy Bohmer dev->network_started = 0; 179423cd1385SRemy Bohmer dev->tx_req = NULL; 179523cd1385SRemy Bohmer dev->rx_req = NULL; 179623cd1385SRemy Bohmer 179723cd1385SRemy Bohmer packet_received = 0; 179823cd1385SRemy Bohmer packet_sent = 0; 179923cd1385SRemy Bohmer 180023cd1385SRemy Bohmer gadget = dev->gadget; 180123cd1385SRemy Bohmer usb_gadget_connect(gadget); 180223cd1385SRemy Bohmer 180323cd1385SRemy Bohmer if (getenv("cdc_connect_timeout")) 180423cd1385SRemy Bohmer timeout = simple_strtoul(getenv("cdc_connect_timeout"), 180523cd1385SRemy Bohmer NULL, 10) * CONFIG_SYS_HZ; 180623cd1385SRemy Bohmer ts = get_timer(0); 1807*6142e0aeSVitaly Kuzmichev while (!l_ethdev.network_started) { 180823cd1385SRemy Bohmer /* Handle control-c and timeouts */ 180923cd1385SRemy Bohmer if (ctrlc() || (get_timer(ts) > timeout)) { 18107de73185SVitaly Kuzmichev error("The remote end did not respond in time."); 181123cd1385SRemy Bohmer goto fail; 181223cd1385SRemy Bohmer } 181323cd1385SRemy Bohmer usb_gadget_handle_interrupts(); 181423cd1385SRemy Bohmer } 181523cd1385SRemy Bohmer 181623cd1385SRemy Bohmer rx_submit(dev, dev->rx_req, 0); 181723cd1385SRemy Bohmer return 0; 181823cd1385SRemy Bohmer fail: 181923cd1385SRemy Bohmer return -1; 182023cd1385SRemy Bohmer } 182123cd1385SRemy Bohmer 1822*6142e0aeSVitaly Kuzmichev static int usb_eth_send(struct eth_device *netdev, 1823*6142e0aeSVitaly Kuzmichev volatile void *packet, int length) 182423cd1385SRemy Bohmer { 182523cd1385SRemy Bohmer int retval; 182623cd1385SRemy Bohmer struct usb_request *req = NULL; 182723cd1385SRemy Bohmer struct eth_dev *dev = &l_ethdev; 1828a170f2c7SStefano Babic unsigned long ts; 1829a170f2c7SStefano Babic unsigned long timeout = USB_CONNECT_TIMEOUT; 18307de73185SVitaly Kuzmichev 18317de73185SVitaly Kuzmichev debug("%s:...\n", __func__); 183223cd1385SRemy Bohmer 183323cd1385SRemy Bohmer req = dev->tx_req; 183423cd1385SRemy Bohmer 183523cd1385SRemy Bohmer req->buf = (void *)packet; 183623cd1385SRemy Bohmer req->context = NULL; 183723cd1385SRemy Bohmer req->complete = tx_complete; 183823cd1385SRemy Bohmer 1839*6142e0aeSVitaly Kuzmichev /* 1840*6142e0aeSVitaly Kuzmichev * use zlp framing on tx for strict CDC-Ether conformance, 184123cd1385SRemy Bohmer * though any robust network rx path ignores extra padding. 184223cd1385SRemy Bohmer * and some hardware doesn't like to write zlps. 184323cd1385SRemy Bohmer */ 184423cd1385SRemy Bohmer req->zero = 1; 184523cd1385SRemy Bohmer if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0) 184623cd1385SRemy Bohmer length++; 184723cd1385SRemy Bohmer 184823cd1385SRemy Bohmer req->length = length; 184923cd1385SRemy Bohmer #if 0 185023cd1385SRemy Bohmer /* throttle highspeed IRQ rate back slightly */ 185123cd1385SRemy Bohmer if (gadget_is_dualspeed(dev->gadget)) 185223cd1385SRemy Bohmer req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH) 185323cd1385SRemy Bohmer ? ((dev->tx_qlen % qmult) != 0) : 0; 185423cd1385SRemy Bohmer #endif 185523cd1385SRemy Bohmer dev->tx_qlen = 1; 1856a170f2c7SStefano Babic ts = get_timer(0); 1857a170f2c7SStefano Babic packet_sent = 0; 185823cd1385SRemy Bohmer 185923cd1385SRemy Bohmer retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC); 186023cd1385SRemy Bohmer 186123cd1385SRemy Bohmer if (!retval) 18627de73185SVitaly Kuzmichev debug("%s: packet queued\n", __func__); 1863*6142e0aeSVitaly Kuzmichev while (!packet_sent) { 1864a170f2c7SStefano Babic if (get_timer(ts) > timeout) { 1865a170f2c7SStefano Babic printf("timeout sending packets to usb ethernet\n"); 1866a170f2c7SStefano Babic return -1; 1867a170f2c7SStefano Babic } 1868a170f2c7SStefano Babic usb_gadget_handle_interrupts(); 186923cd1385SRemy Bohmer } 187023cd1385SRemy Bohmer 187123cd1385SRemy Bohmer return 0; 187223cd1385SRemy Bohmer } 187323cd1385SRemy Bohmer 187423cd1385SRemy Bohmer static int usb_eth_recv(struct eth_device *netdev) 187523cd1385SRemy Bohmer { 187623cd1385SRemy Bohmer struct eth_dev *dev = &l_ethdev; 187723cd1385SRemy Bohmer 187823cd1385SRemy Bohmer usb_gadget_handle_interrupts(); 187923cd1385SRemy Bohmer 1880*6142e0aeSVitaly Kuzmichev if (packet_received) { 18817de73185SVitaly Kuzmichev debug("%s: packet received\n", __func__); 1882*6142e0aeSVitaly Kuzmichev if (dev->rx_req) { 188323cd1385SRemy Bohmer NetReceive(NetRxPackets[0], dev->rx_req->length); 188423cd1385SRemy Bohmer packet_received = 0; 188523cd1385SRemy Bohmer 188623cd1385SRemy Bohmer if (dev->rx_req) 188723cd1385SRemy Bohmer rx_submit(dev, dev->rx_req, 0); 1888*6142e0aeSVitaly Kuzmichev } else 1889*6142e0aeSVitaly Kuzmichev error("dev->rx_req invalid"); 189023cd1385SRemy Bohmer } 189123cd1385SRemy Bohmer return 0; 189223cd1385SRemy Bohmer } 189323cd1385SRemy Bohmer 189423cd1385SRemy Bohmer void usb_eth_halt(struct eth_device *netdev) 189523cd1385SRemy Bohmer { 189623cd1385SRemy Bohmer struct eth_dev *dev = &l_ethdev; 189723cd1385SRemy Bohmer 1898*6142e0aeSVitaly Kuzmichev if (!netdev) { 18997de73185SVitaly Kuzmichev error("received NULL ptr"); 190023cd1385SRemy Bohmer return; 190123cd1385SRemy Bohmer } 190223cd1385SRemy Bohmer 190323cd1385SRemy Bohmer usb_gadget_disconnect(dev->gadget); 190423cd1385SRemy Bohmer } 190523cd1385SRemy Bohmer 190623cd1385SRemy Bohmer static struct usb_gadget_driver eth_driver = { 190723cd1385SRemy Bohmer .speed = DEVSPEED, 190823cd1385SRemy Bohmer 190923cd1385SRemy Bohmer .bind = eth_bind, 191023cd1385SRemy Bohmer .unbind = eth_unbind, 191123cd1385SRemy Bohmer 191223cd1385SRemy Bohmer .setup = eth_setup, 191323cd1385SRemy Bohmer .disconnect = eth_disconnect, 191423cd1385SRemy Bohmer 191523cd1385SRemy Bohmer .suspend = eth_suspend, 191623cd1385SRemy Bohmer .resume = eth_resume, 191723cd1385SRemy Bohmer }; 191823cd1385SRemy Bohmer 191923cd1385SRemy Bohmer int usb_eth_initialize(bd_t *bi) 192023cd1385SRemy Bohmer { 192123cd1385SRemy Bohmer int status = 0; 192223cd1385SRemy Bohmer struct eth_device *netdev = &l_netdev; 192323cd1385SRemy Bohmer 192423cd1385SRemy Bohmer sprintf(netdev->name, "usb_ether"); 192523cd1385SRemy Bohmer 192623cd1385SRemy Bohmer netdev->init = usb_eth_init; 192723cd1385SRemy Bohmer netdev->send = usb_eth_send; 192823cd1385SRemy Bohmer netdev->recv = usb_eth_recv; 192923cd1385SRemy Bohmer netdev->halt = usb_eth_halt; 193023cd1385SRemy Bohmer 193123cd1385SRemy Bohmer #ifdef CONFIG_MCAST_TFTP 193223cd1385SRemy Bohmer #error not supported 193323cd1385SRemy Bohmer #endif 193423cd1385SRemy Bohmer /* Configure default mac-addresses for the USB ethernet device */ 193523cd1385SRemy Bohmer #ifdef CONFIG_USBNET_DEV_ADDR 193623cd1385SRemy Bohmer strncpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr)); 193723cd1385SRemy Bohmer #endif 193823cd1385SRemy Bohmer #ifdef CONFIG_USBNET_HOST_ADDR 193923cd1385SRemy Bohmer strncpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr)); 194023cd1385SRemy Bohmer #endif 194123cd1385SRemy Bohmer /* Check if the user overruled the MAC addresses */ 194223cd1385SRemy Bohmer if (getenv("usbnet_devaddr")) 194323cd1385SRemy Bohmer strncpy(dev_addr, getenv("usbnet_devaddr"), 194423cd1385SRemy Bohmer sizeof(dev_addr)); 194523cd1385SRemy Bohmer 194623cd1385SRemy Bohmer if (getenv("usbnet_hostaddr")) 194723cd1385SRemy Bohmer strncpy(host_addr, getenv("usbnet_hostaddr"), 194823cd1385SRemy Bohmer sizeof(host_addr)); 194923cd1385SRemy Bohmer 195023cd1385SRemy Bohmer /* Make sure both strings are terminated */ 195123cd1385SRemy Bohmer dev_addr[sizeof(dev_addr)-1] = '\0'; 195223cd1385SRemy Bohmer host_addr[sizeof(host_addr)-1] = '\0'; 195323cd1385SRemy Bohmer 195423cd1385SRemy Bohmer if (!is_eth_addr_valid(dev_addr)) { 19557de73185SVitaly Kuzmichev error("Need valid 'usbnet_devaddr' to be set"); 195623cd1385SRemy Bohmer status = -1; 195723cd1385SRemy Bohmer } 195823cd1385SRemy Bohmer if (!is_eth_addr_valid(host_addr)) { 19597de73185SVitaly Kuzmichev error("Need valid 'usbnet_hostaddr' to be set"); 196023cd1385SRemy Bohmer status = -1; 196123cd1385SRemy Bohmer } 196223cd1385SRemy Bohmer if (status) 196323cd1385SRemy Bohmer goto fail; 196423cd1385SRemy Bohmer 196523cd1385SRemy Bohmer status = usb_gadget_register_driver(ð_driver); 196623cd1385SRemy Bohmer if (status < 0) 196723cd1385SRemy Bohmer goto fail; 196823cd1385SRemy Bohmer 196923cd1385SRemy Bohmer eth_register(netdev); 197023cd1385SRemy Bohmer return 0; 197123cd1385SRemy Bohmer 197223cd1385SRemy Bohmer fail: 19737de73185SVitaly Kuzmichev error("%s failed. error = %d", __func__, status); 197423cd1385SRemy Bohmer return status; 197523cd1385SRemy Bohmer } 197623cd1385SRemy Bohmer 1977