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" 34*7de73185SVitaly 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 19123cd1385SRemy Bohmer /* This version autoconfigures as much as possible at run-time. 19223cd1385SRemy Bohmer * 19323cd1385SRemy Bohmer * It also ASSUMES a self-powered device, without remote wakeup, 19423cd1385SRemy Bohmer * although remote wakeup support would make sense. 19523cd1385SRemy Bohmer */ 19623cd1385SRemy Bohmer 19723cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 19823cd1385SRemy Bohmer 19923cd1385SRemy Bohmer /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! 20023cd1385SRemy Bohmer * Instead: allocate your own, using normal USB-IF procedures. 20123cd1385SRemy Bohmer */ 20223cd1385SRemy Bohmer 20323cd1385SRemy Bohmer /* Thanks to NetChip Technologies for donating this product ID. 20423cd1385SRemy Bohmer * It's for devices with only CDC Ethernet configurations. 20523cd1385SRemy Bohmer */ 20623cd1385SRemy Bohmer #define CDC_VENDOR_NUM 0x0525 /* NetChip */ 20723cd1385SRemy Bohmer #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */ 20823cd1385SRemy Bohmer 20923cd1385SRemy Bohmer /* For hardware that can't talk CDC, we use the same vendor ID that 21023cd1385SRemy Bohmer * ARM Linux has used for ethernet-over-usb, both with sa1100 and 21123cd1385SRemy Bohmer * with pxa250. We're protocol-compatible, if the host-side drivers 21223cd1385SRemy Bohmer * use the endpoint descriptors. bcdDevice (version) is nonzero, so 21323cd1385SRemy Bohmer * drivers that need to hard-wire endpoint numbers have a hook. 21423cd1385SRemy Bohmer * 21523cd1385SRemy Bohmer * The protocol is a minimal subset of CDC Ether, which works on any bulk 21623cd1385SRemy Bohmer * hardware that's not deeply broken ... even on hardware that can't talk 21723cd1385SRemy Bohmer * RNDIS (like SA-1100, with no interrupt endpoint, or anything that 21823cd1385SRemy Bohmer * doesn't handle control-OUT). 21923cd1385SRemy Bohmer */ 22023cd1385SRemy Bohmer #define SIMPLE_VENDOR_NUM 0x049f 22123cd1385SRemy Bohmer #define SIMPLE_PRODUCT_NUM 0x505a 22223cd1385SRemy Bohmer 22323cd1385SRemy Bohmer /* Some systems will want different product identifers published in the 22423cd1385SRemy Bohmer * device descriptor, either numbers or strings or both. These string 22523cd1385SRemy Bohmer * parameters are in UTF-8 (superset of ASCII's 7 bit characters). 22623cd1385SRemy Bohmer */ 22723cd1385SRemy Bohmer 22823cd1385SRemy Bohmer static ushort bcdDevice; 22923cd1385SRemy Bohmer #if defined(CONFIG_USBNET_MANUFACTURER) 23023cd1385SRemy Bohmer static char *iManufacturer = CONFIG_USBNET_MANUFACTURER; 23123cd1385SRemy Bohmer #else 23223cd1385SRemy Bohmer static char *iManufacturer = "U-boot"; 23323cd1385SRemy Bohmer #endif 23423cd1385SRemy Bohmer static char *iProduct; 23523cd1385SRemy Bohmer static char *iSerialNumber; 23623cd1385SRemy Bohmer static char dev_addr[18]; 23723cd1385SRemy Bohmer static char host_addr[18]; 23823cd1385SRemy Bohmer 23923cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 24023cd1385SRemy Bohmer 24123cd1385SRemy Bohmer /* USB DRIVER HOOKUP (to the hardware driver, below us), mostly 24223cd1385SRemy Bohmer * ep0 implementation: descriptors, config management, setup(). 24323cd1385SRemy Bohmer * also optional class-specific notification interrupt transfer. 24423cd1385SRemy Bohmer */ 24523cd1385SRemy Bohmer 24623cd1385SRemy Bohmer /* 24723cd1385SRemy Bohmer * DESCRIPTORS ... most are static, but strings and (full) configuration 24823cd1385SRemy Bohmer * descriptors are built on demand. For now we do either full CDC, or 24923cd1385SRemy Bohmer * our simple subset. 25023cd1385SRemy Bohmer */ 25123cd1385SRemy Bohmer 25223cd1385SRemy Bohmer #define STRING_MANUFACTURER 1 25323cd1385SRemy Bohmer #define STRING_PRODUCT 2 25423cd1385SRemy Bohmer #define STRING_ETHADDR 3 25523cd1385SRemy Bohmer #define STRING_DATA 4 25623cd1385SRemy Bohmer #define STRING_CONTROL 5 25723cd1385SRemy Bohmer #define STRING_CDC 7 25823cd1385SRemy Bohmer #define STRING_SUBSET 8 25923cd1385SRemy Bohmer #define STRING_SERIALNUMBER 10 26023cd1385SRemy Bohmer 26123cd1385SRemy Bohmer /* holds our biggest descriptor */ 26223cd1385SRemy Bohmer #define USB_BUFSIZ 256 26323cd1385SRemy Bohmer 26423cd1385SRemy Bohmer /* 26523cd1385SRemy Bohmer * This device advertises one configuration, eth_config, 26623cd1385SRemy Bohmer * on hardware supporting at least two configs. 26723cd1385SRemy Bohmer * 26823cd1385SRemy Bohmer * FIXME define some higher-powered configurations to make it easier 26923cd1385SRemy Bohmer * to recharge batteries ... 27023cd1385SRemy Bohmer */ 27123cd1385SRemy Bohmer 27223cd1385SRemy Bohmer #define DEV_CONFIG_VALUE 1 /* cdc or subset */ 27323cd1385SRemy Bohmer 27423cd1385SRemy Bohmer static struct usb_device_descriptor 27523cd1385SRemy Bohmer device_desc = { 27623cd1385SRemy Bohmer .bLength = sizeof device_desc, 27723cd1385SRemy Bohmer .bDescriptorType = USB_DT_DEVICE, 27823cd1385SRemy Bohmer 27923cd1385SRemy Bohmer .bcdUSB = __constant_cpu_to_le16 (0x0200), 28023cd1385SRemy Bohmer 28123cd1385SRemy Bohmer .bDeviceClass = USB_CLASS_COMM, 28223cd1385SRemy Bohmer .bDeviceSubClass = 0, 28323cd1385SRemy Bohmer .bDeviceProtocol = 0, 28423cd1385SRemy Bohmer 28523cd1385SRemy Bohmer .idVendor = __constant_cpu_to_le16 (CDC_VENDOR_NUM), 28623cd1385SRemy Bohmer .idProduct = __constant_cpu_to_le16 (CDC_PRODUCT_NUM), 28723cd1385SRemy Bohmer .iManufacturer = STRING_MANUFACTURER, 28823cd1385SRemy Bohmer .iProduct = STRING_PRODUCT, 28923cd1385SRemy Bohmer .bNumConfigurations = 1, 29023cd1385SRemy Bohmer }; 29123cd1385SRemy Bohmer 29223cd1385SRemy Bohmer static struct usb_otg_descriptor 29323cd1385SRemy Bohmer otg_descriptor = { 29423cd1385SRemy Bohmer .bLength = sizeof otg_descriptor, 29523cd1385SRemy Bohmer .bDescriptorType = USB_DT_OTG, 29623cd1385SRemy Bohmer 29723cd1385SRemy Bohmer .bmAttributes = USB_OTG_SRP, 29823cd1385SRemy Bohmer }; 29923cd1385SRemy Bohmer 30023cd1385SRemy Bohmer static struct usb_config_descriptor 30123cd1385SRemy Bohmer eth_config = { 30223cd1385SRemy Bohmer .bLength = sizeof eth_config, 30323cd1385SRemy Bohmer .bDescriptorType = USB_DT_CONFIG, 30423cd1385SRemy Bohmer 30523cd1385SRemy Bohmer /* compute wTotalLength on the fly */ 30623cd1385SRemy Bohmer .bNumInterfaces = 2, 30723cd1385SRemy Bohmer .bConfigurationValue = DEV_CONFIG_VALUE, 30823cd1385SRemy Bohmer .iConfiguration = STRING_CDC, 30923cd1385SRemy Bohmer .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, 31023cd1385SRemy Bohmer .bMaxPower = 1, 31123cd1385SRemy Bohmer }; 31223cd1385SRemy Bohmer 31323cd1385SRemy Bohmer /* 31423cd1385SRemy Bohmer * Compared to the simple CDC subset, the full CDC Ethernet model adds 31523cd1385SRemy Bohmer * three class descriptors, two interface descriptors, optional status 31623cd1385SRemy Bohmer * endpoint. Both have a "data" interface and two bulk endpoints. 31723cd1385SRemy Bohmer * There are also differences in how control requests are handled. 31823cd1385SRemy Bohmer */ 31923cd1385SRemy Bohmer 32023cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC 32123cd1385SRemy Bohmer static struct usb_interface_descriptor 32223cd1385SRemy Bohmer control_intf = { 32323cd1385SRemy Bohmer .bLength = sizeof control_intf, 32423cd1385SRemy Bohmer .bDescriptorType = USB_DT_INTERFACE, 32523cd1385SRemy Bohmer 32623cd1385SRemy Bohmer .bInterfaceNumber = 0, 32723cd1385SRemy Bohmer /* status endpoint is optional; this may be patched later */ 32823cd1385SRemy Bohmer .bNumEndpoints = 1, 32923cd1385SRemy Bohmer .bInterfaceClass = USB_CLASS_COMM, 33023cd1385SRemy Bohmer .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, 33123cd1385SRemy Bohmer .bInterfaceProtocol = USB_CDC_PROTO_NONE, 33223cd1385SRemy Bohmer .iInterface = STRING_CONTROL, 33323cd1385SRemy Bohmer }; 33423cd1385SRemy Bohmer #endif 33523cd1385SRemy Bohmer 33623cd1385SRemy Bohmer static const struct usb_cdc_header_desc header_desc = { 33723cd1385SRemy Bohmer .bLength = sizeof header_desc, 33823cd1385SRemy Bohmer .bDescriptorType = USB_DT_CS_INTERFACE, 33923cd1385SRemy Bohmer .bDescriptorSubType = USB_CDC_HEADER_TYPE, 34023cd1385SRemy Bohmer 34123cd1385SRemy Bohmer .bcdCDC = __constant_cpu_to_le16 (0x0110), 34223cd1385SRemy Bohmer }; 34323cd1385SRemy Bohmer 34423cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC) 34523cd1385SRemy Bohmer 34623cd1385SRemy Bohmer static const struct usb_cdc_union_desc union_desc = { 34723cd1385SRemy Bohmer .bLength = sizeof union_desc, 34823cd1385SRemy Bohmer .bDescriptorType = USB_DT_CS_INTERFACE, 34923cd1385SRemy Bohmer .bDescriptorSubType = USB_CDC_UNION_TYPE, 35023cd1385SRemy Bohmer 35123cd1385SRemy Bohmer .bMasterInterface0 = 0, /* index of control interface */ 35223cd1385SRemy Bohmer .bSlaveInterface0 = 1, /* index of DATA interface */ 35323cd1385SRemy Bohmer }; 35423cd1385SRemy Bohmer 35523cd1385SRemy Bohmer #endif /* CDC */ 35623cd1385SRemy Bohmer 35723cd1385SRemy Bohmer #ifndef DEV_CONFIG_CDC 35823cd1385SRemy Bohmer 35923cd1385SRemy Bohmer /* "SAFE" loosely follows CDC WMC MDLM, violating the spec in various 36023cd1385SRemy Bohmer * ways: data endpoints live in the control interface, there's no data 36123cd1385SRemy Bohmer * interface, and it's not used to talk to a cell phone radio. 36223cd1385SRemy Bohmer */ 36323cd1385SRemy Bohmer 36423cd1385SRemy Bohmer static const struct usb_cdc_mdlm_desc mdlm_desc = { 36523cd1385SRemy Bohmer .bLength = sizeof mdlm_desc, 36623cd1385SRemy Bohmer .bDescriptorType = USB_DT_CS_INTERFACE, 36723cd1385SRemy Bohmer .bDescriptorSubType = USB_CDC_MDLM_TYPE, 36823cd1385SRemy Bohmer 36923cd1385SRemy Bohmer .bcdVersion = __constant_cpu_to_le16(0x0100), 37023cd1385SRemy Bohmer .bGUID = { 37123cd1385SRemy Bohmer 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6, 37223cd1385SRemy Bohmer 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f, 37323cd1385SRemy Bohmer }, 37423cd1385SRemy Bohmer }; 37523cd1385SRemy Bohmer 37623cd1385SRemy Bohmer /* since "usb_cdc_mdlm_detail_desc" is a variable length structure, we 37723cd1385SRemy Bohmer * can't really use its struct. All we do here is say that we're using 37823cd1385SRemy Bohmer * the submode of "SAFE" which directly matches the CDC Subset. 37923cd1385SRemy Bohmer */ 38023cd1385SRemy Bohmer static const u8 mdlm_detail_desc[] = { 38123cd1385SRemy Bohmer 6, 38223cd1385SRemy Bohmer USB_DT_CS_INTERFACE, 38323cd1385SRemy Bohmer USB_CDC_MDLM_DETAIL_TYPE, 38423cd1385SRemy Bohmer 38523cd1385SRemy Bohmer 0, /* "SAFE" */ 38623cd1385SRemy Bohmer 0, /* network control capabilities (none) */ 38723cd1385SRemy Bohmer 0, /* network data capabilities ("raw" encapsulation) */ 38823cd1385SRemy Bohmer }; 38923cd1385SRemy Bohmer 39023cd1385SRemy Bohmer #endif 39123cd1385SRemy Bohmer 39223cd1385SRemy Bohmer 39323cd1385SRemy Bohmer static const struct usb_cdc_ether_desc ether_desc = { 39423cd1385SRemy Bohmer .bLength = sizeof (ether_desc), 39523cd1385SRemy Bohmer .bDescriptorType = USB_DT_CS_INTERFACE, 39623cd1385SRemy Bohmer .bDescriptorSubType = USB_CDC_ETHERNET_TYPE, 39723cd1385SRemy Bohmer 39823cd1385SRemy Bohmer /* this descriptor actually adds value, surprise! */ 39923cd1385SRemy Bohmer .iMACAddress = STRING_ETHADDR, 40023cd1385SRemy Bohmer .bmEthernetStatistics = __constant_cpu_to_le32 (0), /* no statistics */ 40123cd1385SRemy Bohmer .wMaxSegmentSize = __constant_cpu_to_le16 (ETH_FRAME_LEN), 40223cd1385SRemy Bohmer .wNumberMCFilters = __constant_cpu_to_le16 (0), 40323cd1385SRemy Bohmer .bNumberPowerFilters = 0, 40423cd1385SRemy Bohmer }; 40523cd1385SRemy Bohmer 40623cd1385SRemy Bohmer 40723cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC) 40823cd1385SRemy Bohmer 40923cd1385SRemy Bohmer /* include the status endpoint if we can, even where it's optional. 41023cd1385SRemy Bohmer * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one 41123cd1385SRemy Bohmer * packet, to simplify cancellation; and a big transfer interval, to 41223cd1385SRemy Bohmer * waste less bandwidth. 41323cd1385SRemy Bohmer * 41423cd1385SRemy Bohmer * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even 41523cd1385SRemy Bohmer * if they ignore the connect/disconnect notifications that real aether 41623cd1385SRemy Bohmer * can provide. more advanced cdc configurations might want to support 41723cd1385SRemy Bohmer * encapsulated commands (vendor-specific, using control-OUT). 41823cd1385SRemy Bohmer */ 41923cd1385SRemy Bohmer 42023cd1385SRemy Bohmer #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */ 42123cd1385SRemy Bohmer #define STATUS_BYTECOUNT 16 /* 8 byte header + data */ 42223cd1385SRemy Bohmer 42323cd1385SRemy Bohmer static struct usb_endpoint_descriptor 42423cd1385SRemy Bohmer fs_status_desc = { 42523cd1385SRemy Bohmer .bLength = USB_DT_ENDPOINT_SIZE, 42623cd1385SRemy Bohmer .bDescriptorType = USB_DT_ENDPOINT, 42723cd1385SRemy Bohmer 42823cd1385SRemy Bohmer .bEndpointAddress = USB_DIR_IN, 42923cd1385SRemy Bohmer .bmAttributes = USB_ENDPOINT_XFER_INT, 43023cd1385SRemy Bohmer .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT), 43123cd1385SRemy Bohmer .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC, 43223cd1385SRemy Bohmer }; 43323cd1385SRemy Bohmer #endif 43423cd1385SRemy Bohmer 43523cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC 43623cd1385SRemy Bohmer 43723cd1385SRemy Bohmer /* the default data interface has no endpoints ... */ 43823cd1385SRemy Bohmer 43923cd1385SRemy Bohmer static const struct usb_interface_descriptor 44023cd1385SRemy Bohmer data_nop_intf = { 44123cd1385SRemy Bohmer .bLength = sizeof data_nop_intf, 44223cd1385SRemy Bohmer .bDescriptorType = USB_DT_INTERFACE, 44323cd1385SRemy Bohmer 44423cd1385SRemy Bohmer .bInterfaceNumber = 1, 44523cd1385SRemy Bohmer .bAlternateSetting = 0, 44623cd1385SRemy Bohmer .bNumEndpoints = 0, 44723cd1385SRemy Bohmer .bInterfaceClass = USB_CLASS_CDC_DATA, 44823cd1385SRemy Bohmer .bInterfaceSubClass = 0, 44923cd1385SRemy Bohmer .bInterfaceProtocol = 0, 45023cd1385SRemy Bohmer }; 45123cd1385SRemy Bohmer 45223cd1385SRemy Bohmer /* ... but the "real" data interface has two bulk endpoints */ 45323cd1385SRemy Bohmer 45423cd1385SRemy Bohmer static const struct usb_interface_descriptor 45523cd1385SRemy Bohmer data_intf = { 45623cd1385SRemy Bohmer .bLength = sizeof data_intf, 45723cd1385SRemy Bohmer .bDescriptorType = USB_DT_INTERFACE, 45823cd1385SRemy Bohmer 45923cd1385SRemy Bohmer .bInterfaceNumber = 1, 46023cd1385SRemy Bohmer .bAlternateSetting = 1, 46123cd1385SRemy Bohmer .bNumEndpoints = 2, 46223cd1385SRemy Bohmer .bInterfaceClass = USB_CLASS_CDC_DATA, 46323cd1385SRemy Bohmer .bInterfaceSubClass = 0, 46423cd1385SRemy Bohmer .bInterfaceProtocol = 0, 46523cd1385SRemy Bohmer .iInterface = STRING_DATA, 46623cd1385SRemy Bohmer }; 46723cd1385SRemy Bohmer 46823cd1385SRemy Bohmer #endif 46923cd1385SRemy Bohmer 47023cd1385SRemy Bohmer #ifdef DEV_CONFIG_SUBSET 47123cd1385SRemy Bohmer 47223cd1385SRemy Bohmer /* 47323cd1385SRemy Bohmer * "Simple" CDC-subset option is a simple vendor-neutral model that most 47423cd1385SRemy Bohmer * full speed controllers can handle: one interface, two bulk endpoints. 47523cd1385SRemy Bohmer * 47623cd1385SRemy Bohmer * To assist host side drivers, we fancy it up a bit, and add descriptors 47723cd1385SRemy Bohmer * so some host side drivers will understand it as a "SAFE" variant. 47823cd1385SRemy Bohmer */ 47923cd1385SRemy Bohmer 48023cd1385SRemy Bohmer static const struct usb_interface_descriptor 48123cd1385SRemy Bohmer subset_data_intf = { 48223cd1385SRemy Bohmer .bLength = sizeof subset_data_intf, 48323cd1385SRemy Bohmer .bDescriptorType = USB_DT_INTERFACE, 48423cd1385SRemy Bohmer 48523cd1385SRemy Bohmer .bInterfaceNumber = 0, 48623cd1385SRemy Bohmer .bAlternateSetting = 0, 48723cd1385SRemy Bohmer .bNumEndpoints = 2, 48823cd1385SRemy Bohmer .bInterfaceClass = USB_CLASS_COMM, 48923cd1385SRemy Bohmer .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM, 49023cd1385SRemy Bohmer .bInterfaceProtocol = 0, 49123cd1385SRemy Bohmer .iInterface = STRING_DATA, 49223cd1385SRemy Bohmer }; 49323cd1385SRemy Bohmer 49423cd1385SRemy Bohmer #endif /* SUBSET */ 49523cd1385SRemy Bohmer 49623cd1385SRemy Bohmer 49723cd1385SRemy Bohmer static struct usb_endpoint_descriptor 49823cd1385SRemy Bohmer fs_source_desc = { 49923cd1385SRemy Bohmer .bLength = USB_DT_ENDPOINT_SIZE, 50023cd1385SRemy Bohmer .bDescriptorType = USB_DT_ENDPOINT, 50123cd1385SRemy Bohmer 50223cd1385SRemy Bohmer .bEndpointAddress = USB_DIR_IN, 50323cd1385SRemy Bohmer .bmAttributes = USB_ENDPOINT_XFER_BULK, 50423cd1385SRemy Bohmer }; 50523cd1385SRemy Bohmer 50623cd1385SRemy Bohmer static struct usb_endpoint_descriptor 50723cd1385SRemy Bohmer fs_sink_desc = { 50823cd1385SRemy Bohmer .bLength = USB_DT_ENDPOINT_SIZE, 50923cd1385SRemy Bohmer .bDescriptorType = USB_DT_ENDPOINT, 51023cd1385SRemy Bohmer 51123cd1385SRemy Bohmer .bEndpointAddress = USB_DIR_OUT, 51223cd1385SRemy Bohmer .bmAttributes = USB_ENDPOINT_XFER_BULK, 51323cd1385SRemy Bohmer }; 51423cd1385SRemy Bohmer 51523cd1385SRemy Bohmer static const struct usb_descriptor_header *fs_eth_function [11] = { 51623cd1385SRemy Bohmer (struct usb_descriptor_header *) &otg_descriptor, 51723cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC 51823cd1385SRemy Bohmer /* "cdc" mode descriptors */ 51923cd1385SRemy Bohmer (struct usb_descriptor_header *) &control_intf, 52023cd1385SRemy Bohmer (struct usb_descriptor_header *) &header_desc, 52123cd1385SRemy Bohmer (struct usb_descriptor_header *) &union_desc, 52223cd1385SRemy Bohmer (struct usb_descriptor_header *) ðer_desc, 52323cd1385SRemy Bohmer /* NOTE: status endpoint may need to be removed */ 52423cd1385SRemy Bohmer (struct usb_descriptor_header *) &fs_status_desc, 52523cd1385SRemy Bohmer /* data interface, with altsetting */ 52623cd1385SRemy Bohmer (struct usb_descriptor_header *) &data_nop_intf, 52723cd1385SRemy Bohmer (struct usb_descriptor_header *) &data_intf, 52823cd1385SRemy Bohmer (struct usb_descriptor_header *) &fs_source_desc, 52923cd1385SRemy Bohmer (struct usb_descriptor_header *) &fs_sink_desc, 53023cd1385SRemy Bohmer NULL, 53123cd1385SRemy Bohmer #endif /* DEV_CONFIG_CDC */ 53223cd1385SRemy Bohmer }; 53323cd1385SRemy Bohmer 53423cd1385SRemy Bohmer static inline void fs_subset_descriptors(void) 53523cd1385SRemy Bohmer { 53623cd1385SRemy Bohmer #ifdef DEV_CONFIG_SUBSET 53723cd1385SRemy Bohmer /* behavior is "CDC Subset"; extra descriptors say "SAFE" */ 53823cd1385SRemy Bohmer fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf; 53923cd1385SRemy Bohmer fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc; 54023cd1385SRemy Bohmer fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc; 54123cd1385SRemy Bohmer fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc; 54223cd1385SRemy Bohmer fs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc; 54323cd1385SRemy Bohmer fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc; 54423cd1385SRemy Bohmer fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc; 54523cd1385SRemy Bohmer fs_eth_function[8] = NULL; 54623cd1385SRemy Bohmer #else 54723cd1385SRemy Bohmer fs_eth_function[1] = NULL; 54823cd1385SRemy Bohmer #endif 54923cd1385SRemy Bohmer } 55023cd1385SRemy Bohmer 55123cd1385SRemy Bohmer /* 55223cd1385SRemy Bohmer * usb 2.0 devices need to expose both high speed and full speed 55323cd1385SRemy Bohmer * descriptors, unless they only run at full speed. 55423cd1385SRemy Bohmer */ 55523cd1385SRemy Bohmer 55623cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC) 55723cd1385SRemy Bohmer static struct usb_endpoint_descriptor 55823cd1385SRemy Bohmer hs_status_desc = { 55923cd1385SRemy Bohmer .bLength = USB_DT_ENDPOINT_SIZE, 56023cd1385SRemy Bohmer .bDescriptorType = USB_DT_ENDPOINT, 56123cd1385SRemy Bohmer 56223cd1385SRemy Bohmer .bmAttributes = USB_ENDPOINT_XFER_INT, 56323cd1385SRemy Bohmer .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT), 56423cd1385SRemy Bohmer .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4, 56523cd1385SRemy Bohmer }; 56623cd1385SRemy Bohmer #endif /* DEV_CONFIG_CDC */ 56723cd1385SRemy Bohmer 56823cd1385SRemy Bohmer static struct usb_endpoint_descriptor 56923cd1385SRemy Bohmer hs_source_desc = { 57023cd1385SRemy Bohmer .bLength = USB_DT_ENDPOINT_SIZE, 57123cd1385SRemy Bohmer .bDescriptorType = USB_DT_ENDPOINT, 57223cd1385SRemy Bohmer 57323cd1385SRemy Bohmer .bmAttributes = USB_ENDPOINT_XFER_BULK, 57423cd1385SRemy Bohmer .wMaxPacketSize = __constant_cpu_to_le16 (512), 57523cd1385SRemy Bohmer }; 57623cd1385SRemy Bohmer 57723cd1385SRemy Bohmer static struct usb_endpoint_descriptor 57823cd1385SRemy Bohmer hs_sink_desc = { 57923cd1385SRemy Bohmer .bLength = USB_DT_ENDPOINT_SIZE, 58023cd1385SRemy Bohmer .bDescriptorType = USB_DT_ENDPOINT, 58123cd1385SRemy Bohmer 58223cd1385SRemy Bohmer .bmAttributes = USB_ENDPOINT_XFER_BULK, 58323cd1385SRemy Bohmer .wMaxPacketSize = __constant_cpu_to_le16 (512), 58423cd1385SRemy Bohmer }; 58523cd1385SRemy Bohmer 58623cd1385SRemy Bohmer static struct usb_qualifier_descriptor 58723cd1385SRemy Bohmer dev_qualifier = { 58823cd1385SRemy Bohmer .bLength = sizeof dev_qualifier, 58923cd1385SRemy Bohmer .bDescriptorType = USB_DT_DEVICE_QUALIFIER, 59023cd1385SRemy Bohmer 59123cd1385SRemy Bohmer .bcdUSB = __constant_cpu_to_le16 (0x0200), 59223cd1385SRemy Bohmer .bDeviceClass = USB_CLASS_COMM, 59323cd1385SRemy Bohmer 59423cd1385SRemy Bohmer .bNumConfigurations = 1, 59523cd1385SRemy Bohmer }; 59623cd1385SRemy Bohmer 59723cd1385SRemy Bohmer static const struct usb_descriptor_header *hs_eth_function [11] = { 59823cd1385SRemy Bohmer (struct usb_descriptor_header *) &otg_descriptor, 59923cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC 60023cd1385SRemy Bohmer /* "cdc" mode descriptors */ 60123cd1385SRemy Bohmer (struct usb_descriptor_header *) &control_intf, 60223cd1385SRemy Bohmer (struct usb_descriptor_header *) &header_desc, 60323cd1385SRemy Bohmer (struct usb_descriptor_header *) &union_desc, 60423cd1385SRemy Bohmer (struct usb_descriptor_header *) ðer_desc, 60523cd1385SRemy Bohmer /* NOTE: status endpoint may need to be removed */ 60623cd1385SRemy Bohmer (struct usb_descriptor_header *) &hs_status_desc, 60723cd1385SRemy Bohmer /* data interface, with altsetting */ 60823cd1385SRemy Bohmer (struct usb_descriptor_header *) &data_nop_intf, 60923cd1385SRemy Bohmer (struct usb_descriptor_header *) &data_intf, 61023cd1385SRemy Bohmer (struct usb_descriptor_header *) &hs_source_desc, 61123cd1385SRemy Bohmer (struct usb_descriptor_header *) &hs_sink_desc, 61223cd1385SRemy Bohmer NULL, 61323cd1385SRemy Bohmer #endif /* DEV_CONFIG_CDC */ 61423cd1385SRemy Bohmer }; 61523cd1385SRemy Bohmer 61623cd1385SRemy Bohmer static inline void hs_subset_descriptors(void) 61723cd1385SRemy Bohmer { 61823cd1385SRemy Bohmer #ifdef DEV_CONFIG_SUBSET 61923cd1385SRemy Bohmer /* behavior is "CDC Subset"; extra descriptors say "SAFE" */ 62023cd1385SRemy Bohmer hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf; 62123cd1385SRemy Bohmer hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc; 62223cd1385SRemy Bohmer hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc; 62323cd1385SRemy Bohmer hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc; 62423cd1385SRemy Bohmer hs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc; 62523cd1385SRemy Bohmer hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc; 62623cd1385SRemy Bohmer hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc; 62723cd1385SRemy Bohmer hs_eth_function[8] = NULL; 62823cd1385SRemy Bohmer #else 62923cd1385SRemy Bohmer hs_eth_function[1] = NULL; 63023cd1385SRemy Bohmer #endif 63123cd1385SRemy Bohmer } 63223cd1385SRemy Bohmer 63323cd1385SRemy Bohmer /* maxpacket and other transfer characteristics vary by speed. */ 63423cd1385SRemy Bohmer static inline struct usb_endpoint_descriptor * 63523cd1385SRemy Bohmer ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs, 63623cd1385SRemy Bohmer struct usb_endpoint_descriptor *fs) 63723cd1385SRemy Bohmer { 63823cd1385SRemy Bohmer if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) 63923cd1385SRemy Bohmer return hs; 64023cd1385SRemy Bohmer return fs; 64123cd1385SRemy Bohmer } 64223cd1385SRemy Bohmer 64323cd1385SRemy Bohmer 64423cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 64523cd1385SRemy Bohmer 64623cd1385SRemy Bohmer /* descriptors that are built on-demand */ 64723cd1385SRemy Bohmer 64823cd1385SRemy Bohmer static char manufacturer [50]; 64923cd1385SRemy Bohmer static char product_desc [40] = DRIVER_DESC; 65023cd1385SRemy Bohmer static char serial_number [20]; 65123cd1385SRemy Bohmer 65223cd1385SRemy Bohmer /* address that the host will use ... usually assigned at random */ 65323cd1385SRemy Bohmer static char ethaddr [2 * ETH_ALEN + 1]; 65423cd1385SRemy Bohmer 65523cd1385SRemy Bohmer /* static strings, in UTF-8 */ 65623cd1385SRemy Bohmer static struct usb_string strings [] = { 65723cd1385SRemy Bohmer { STRING_MANUFACTURER, manufacturer, }, 65823cd1385SRemy Bohmer { STRING_PRODUCT, product_desc, }, 65923cd1385SRemy Bohmer { STRING_SERIALNUMBER, serial_number, }, 66023cd1385SRemy Bohmer { STRING_DATA, "Ethernet Data", }, 66123cd1385SRemy Bohmer { STRING_ETHADDR, ethaddr, }, 66223cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC 66323cd1385SRemy Bohmer { STRING_CDC, "CDC Ethernet", }, 66423cd1385SRemy Bohmer { STRING_CONTROL, "CDC Communications Control", }, 66523cd1385SRemy Bohmer #endif 66623cd1385SRemy Bohmer #ifdef DEV_CONFIG_SUBSET 66723cd1385SRemy Bohmer { STRING_SUBSET, "CDC Ethernet Subset", }, 66823cd1385SRemy Bohmer #endif 66923cd1385SRemy Bohmer { } /* end of list */ 67023cd1385SRemy Bohmer }; 67123cd1385SRemy Bohmer 67223cd1385SRemy Bohmer static struct usb_gadget_strings stringtab = { 67323cd1385SRemy Bohmer .language = 0x0409, /* en-us */ 67423cd1385SRemy Bohmer .strings = strings, 67523cd1385SRemy Bohmer }; 67623cd1385SRemy Bohmer 67723cd1385SRemy Bohmer 67823cd1385SRemy Bohmer /*============================================================================*/ 67923cd1385SRemy Bohmer static u8 control_req[USB_BUFSIZ]; 68023cd1385SRemy Bohmer static u8 status_req[STATUS_BYTECOUNT]; 68123cd1385SRemy Bohmer 68223cd1385SRemy Bohmer 68323cd1385SRemy Bohmer 68423cd1385SRemy Bohmer /** 68523cd1385SRemy Bohmer * strlcpy - Copy a %NUL terminated string into a sized buffer 68623cd1385SRemy Bohmer * @dest: Where to copy the string to 68723cd1385SRemy Bohmer * @src: Where to copy the string from 68823cd1385SRemy Bohmer * @size: size of destination buffer 68923cd1385SRemy Bohmer * 69023cd1385SRemy Bohmer * Compatible with *BSD: the result is always a valid 69123cd1385SRemy Bohmer * NUL-terminated string that fits in the buffer (unless, 69223cd1385SRemy Bohmer * of course, the buffer size is zero). It does not pad 69323cd1385SRemy Bohmer * out the result like strncpy() does. 69423cd1385SRemy Bohmer */ 69523cd1385SRemy Bohmer size_t strlcpy(char *dest, const char *src, size_t size) 69623cd1385SRemy Bohmer { 69723cd1385SRemy Bohmer size_t ret = strlen(src); 69823cd1385SRemy Bohmer 69923cd1385SRemy Bohmer if (size) { 70023cd1385SRemy Bohmer size_t len = (ret >= size) ? size - 1 : ret; 70123cd1385SRemy Bohmer memcpy(dest, src, len); 70223cd1385SRemy Bohmer dest[len] = '\0'; 70323cd1385SRemy Bohmer } 70423cd1385SRemy Bohmer return ret; 70523cd1385SRemy Bohmer } 70623cd1385SRemy Bohmer 70723cd1385SRemy Bohmer 70823cd1385SRemy Bohmer /*============================================================================*/ 70923cd1385SRemy Bohmer 71023cd1385SRemy Bohmer /* 71123cd1385SRemy Bohmer * one config, two interfaces: control, data. 71223cd1385SRemy Bohmer * complications: class descriptors, and an altsetting. 71323cd1385SRemy Bohmer */ 71423cd1385SRemy Bohmer static int 71523cd1385SRemy Bohmer config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg) 71623cd1385SRemy Bohmer { 71723cd1385SRemy Bohmer int len; 71823cd1385SRemy Bohmer const struct usb_config_descriptor *config; 71923cd1385SRemy Bohmer const struct usb_descriptor_header **function; 72023cd1385SRemy Bohmer int hs = 0; 72123cd1385SRemy Bohmer 72223cd1385SRemy Bohmer if (gadget_is_dualspeed(g)) { 72323cd1385SRemy Bohmer hs = (g->speed == USB_SPEED_HIGH); 72423cd1385SRemy Bohmer if (type == USB_DT_OTHER_SPEED_CONFIG) 72523cd1385SRemy Bohmer hs = !hs; 72623cd1385SRemy Bohmer } 72723cd1385SRemy Bohmer #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function) 72823cd1385SRemy Bohmer 72923cd1385SRemy Bohmer if (index >= device_desc.bNumConfigurations) 73023cd1385SRemy Bohmer return -EINVAL; 73123cd1385SRemy Bohmer 73223cd1385SRemy Bohmer config = ð_config; 73323cd1385SRemy Bohmer function = which_fn (eth); 73423cd1385SRemy Bohmer 73523cd1385SRemy Bohmer /* for now, don't advertise srp-only devices */ 73623cd1385SRemy Bohmer if (!is_otg) 73723cd1385SRemy Bohmer function++; 73823cd1385SRemy Bohmer 73923cd1385SRemy Bohmer len = usb_gadget_config_buf (config, buf, USB_BUFSIZ, function); 74023cd1385SRemy Bohmer if (len < 0) 74123cd1385SRemy Bohmer return len; 74223cd1385SRemy Bohmer ((struct usb_config_descriptor *) buf)->bDescriptorType = type; 74323cd1385SRemy Bohmer return len; 74423cd1385SRemy Bohmer } 74523cd1385SRemy Bohmer 74623cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 74723cd1385SRemy Bohmer 74823cd1385SRemy Bohmer static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags); 74923cd1385SRemy Bohmer 75023cd1385SRemy Bohmer static int 75123cd1385SRemy Bohmer set_ether_config (struct eth_dev *dev, gfp_t gfp_flags) 75223cd1385SRemy Bohmer { 75323cd1385SRemy Bohmer int result = 0; 75423cd1385SRemy Bohmer struct usb_gadget *gadget = dev->gadget; 75523cd1385SRemy Bohmer 75623cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC) 75723cd1385SRemy Bohmer /* status endpoint used for (optionally) CDC */ 75823cd1385SRemy Bohmer if (!subset_active(dev) && dev->status_ep) { 75923cd1385SRemy Bohmer dev->status = ep_desc (gadget, &hs_status_desc, 76023cd1385SRemy Bohmer &fs_status_desc); 76123cd1385SRemy Bohmer dev->status_ep->driver_data = dev; 76223cd1385SRemy Bohmer 76323cd1385SRemy Bohmer result = usb_ep_enable (dev->status_ep, dev->status); 76423cd1385SRemy Bohmer if (result != 0) { 765*7de73185SVitaly Kuzmichev debug("enable %s --> %d\n", 76623cd1385SRemy Bohmer dev->status_ep->name, result); 76723cd1385SRemy Bohmer goto done; 76823cd1385SRemy Bohmer } 76923cd1385SRemy Bohmer } 77023cd1385SRemy Bohmer #endif 77123cd1385SRemy Bohmer 77223cd1385SRemy Bohmer dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc); 77323cd1385SRemy Bohmer dev->in_ep->driver_data = dev; 77423cd1385SRemy Bohmer 77523cd1385SRemy Bohmer dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc); 77623cd1385SRemy Bohmer dev->out_ep->driver_data = dev; 77723cd1385SRemy Bohmer 77823cd1385SRemy Bohmer /* With CDC, the host isn't allowed to use these two data 77923cd1385SRemy Bohmer * endpoints in the default altsetting for the interface. 78023cd1385SRemy Bohmer * so we don't activate them yet. Reset from SET_INTERFACE. 78123cd1385SRemy Bohmer */ 78223cd1385SRemy Bohmer if (!cdc_active(dev)) { 78323cd1385SRemy Bohmer result = usb_ep_enable (dev->in_ep, dev->in); 78423cd1385SRemy Bohmer if (result != 0) { 785*7de73185SVitaly Kuzmichev debug("enable %s --> %d\n", 78623cd1385SRemy Bohmer dev->in_ep->name, result); 78723cd1385SRemy Bohmer goto done; 78823cd1385SRemy Bohmer } 78923cd1385SRemy Bohmer 79023cd1385SRemy Bohmer result = usb_ep_enable (dev->out_ep, dev->out); 79123cd1385SRemy Bohmer if (result != 0) { 792*7de73185SVitaly Kuzmichev debug("enable %s --> %d\n", 79323cd1385SRemy Bohmer dev->out_ep->name, result); 79423cd1385SRemy Bohmer goto done; 79523cd1385SRemy Bohmer } 79623cd1385SRemy Bohmer } 79723cd1385SRemy Bohmer 79823cd1385SRemy Bohmer done: 79923cd1385SRemy Bohmer if (result == 0) 80023cd1385SRemy Bohmer result = alloc_requests (dev, qlen (gadget), gfp_flags); 80123cd1385SRemy Bohmer 80223cd1385SRemy Bohmer /* on error, disable any endpoints */ 80323cd1385SRemy Bohmer if (result < 0) { 80423cd1385SRemy Bohmer if (!subset_active(dev)) 80523cd1385SRemy Bohmer (void) usb_ep_disable (dev->status_ep); 80623cd1385SRemy Bohmer dev->status = NULL; 80723cd1385SRemy Bohmer (void) usb_ep_disable (dev->in_ep); 80823cd1385SRemy Bohmer (void) usb_ep_disable (dev->out_ep); 80923cd1385SRemy Bohmer dev->in = NULL; 81023cd1385SRemy Bohmer dev->out = NULL; 81123cd1385SRemy Bohmer } 81223cd1385SRemy Bohmer 81323cd1385SRemy Bohmer /* caller is responsible for cleanup on error */ 81423cd1385SRemy Bohmer return result; 81523cd1385SRemy Bohmer } 81623cd1385SRemy Bohmer 81723cd1385SRemy Bohmer 81823cd1385SRemy Bohmer static void eth_reset_config (struct eth_dev *dev) 81923cd1385SRemy Bohmer { 82023cd1385SRemy Bohmer if (dev->config == 0) 82123cd1385SRemy Bohmer return; 82223cd1385SRemy Bohmer 823*7de73185SVitaly Kuzmichev debug("%s\n", __func__); 824*7de73185SVitaly Kuzmichev 82523cd1385SRemy Bohmer /* disable endpoints, forcing (synchronous) completion of 82623cd1385SRemy Bohmer * pending i/o. then free the requests. 82723cd1385SRemy Bohmer */ 82823cd1385SRemy Bohmer 82923cd1385SRemy Bohmer if (dev->in) { 83023cd1385SRemy Bohmer usb_ep_disable (dev->in_ep); 83123cd1385SRemy Bohmer if (dev->tx_req) { 83223cd1385SRemy Bohmer usb_ep_free_request (dev->in_ep, dev->tx_req); 83323cd1385SRemy Bohmer dev->tx_req=NULL; 83423cd1385SRemy Bohmer } 83523cd1385SRemy Bohmer } 83623cd1385SRemy Bohmer if (dev->out) { 83723cd1385SRemy Bohmer usb_ep_disable (dev->out_ep); 83823cd1385SRemy Bohmer if (dev->rx_req) { 83923cd1385SRemy Bohmer usb_ep_free_request (dev->in_ep, dev->rx_req); 84023cd1385SRemy Bohmer dev->rx_req=NULL; 84123cd1385SRemy Bohmer } 84223cd1385SRemy Bohmer } 84323cd1385SRemy Bohmer if (dev->status) { 84423cd1385SRemy Bohmer usb_ep_disable (dev->status_ep); 84523cd1385SRemy Bohmer } 84623cd1385SRemy Bohmer dev->cdc_filter = 0; 84723cd1385SRemy Bohmer dev->config = 0; 84823cd1385SRemy Bohmer } 84923cd1385SRemy Bohmer 85023cd1385SRemy Bohmer /* change our operational config. must agree with the code 85123cd1385SRemy Bohmer * that returns config descriptors, and altsetting code. 85223cd1385SRemy Bohmer */ 85323cd1385SRemy Bohmer static int eth_set_config (struct eth_dev *dev, unsigned number, gfp_t gfp_flags) 85423cd1385SRemy Bohmer { 85523cd1385SRemy Bohmer int result = 0; 85623cd1385SRemy Bohmer struct usb_gadget *gadget = dev->gadget; 85723cd1385SRemy Bohmer 85823cd1385SRemy Bohmer if (gadget_is_sa1100 (gadget) 85923cd1385SRemy Bohmer && dev->config 86023cd1385SRemy Bohmer && dev->tx_qlen != 0) { 86123cd1385SRemy Bohmer /* tx fifo is full, but we can't clear it...*/ 862*7de73185SVitaly Kuzmichev error("can't change configurations"); 86323cd1385SRemy Bohmer return -ESPIPE; 86423cd1385SRemy Bohmer } 86523cd1385SRemy Bohmer eth_reset_config (dev); 86623cd1385SRemy Bohmer 86723cd1385SRemy Bohmer switch (number) { 86823cd1385SRemy Bohmer case DEV_CONFIG_VALUE: 86923cd1385SRemy Bohmer result = set_ether_config (dev, gfp_flags); 87023cd1385SRemy Bohmer break; 87123cd1385SRemy Bohmer default: 87223cd1385SRemy Bohmer result = -EINVAL; 87323cd1385SRemy Bohmer /* FALL THROUGH */ 87423cd1385SRemy Bohmer case 0: 87523cd1385SRemy Bohmer break; 87623cd1385SRemy Bohmer } 87723cd1385SRemy Bohmer 87823cd1385SRemy Bohmer if (result) { 87923cd1385SRemy Bohmer if (number) 88023cd1385SRemy Bohmer eth_reset_config (dev); 88123cd1385SRemy Bohmer usb_gadget_vbus_draw(dev->gadget, 88223cd1385SRemy Bohmer gadget_is_otg(dev->gadget) ? 8 : 100); 88323cd1385SRemy Bohmer } else { 88423cd1385SRemy Bohmer char *speed; 88523cd1385SRemy Bohmer unsigned power; 88623cd1385SRemy Bohmer 88723cd1385SRemy Bohmer power = 2 * eth_config.bMaxPower; 88823cd1385SRemy Bohmer usb_gadget_vbus_draw(dev->gadget, power); 88923cd1385SRemy Bohmer 89023cd1385SRemy Bohmer switch (gadget->speed) { 89123cd1385SRemy Bohmer case USB_SPEED_FULL: speed = "full"; break; 89223cd1385SRemy Bohmer #ifdef CONFIG_USB_GADGET_DUALSPEED 89323cd1385SRemy Bohmer case USB_SPEED_HIGH: speed = "high"; break; 89423cd1385SRemy Bohmer #endif 89523cd1385SRemy Bohmer default: speed = "?"; break; 89623cd1385SRemy Bohmer } 89723cd1385SRemy Bohmer 89823cd1385SRemy Bohmer dev->config = number; 899*7de73185SVitaly Kuzmichev printf("%s speed config #%d: %d mA, %s, using %s\n", 90023cd1385SRemy Bohmer speed, number, power, driver_desc, 90123cd1385SRemy Bohmer (cdc_active(dev)? "CDC Ethernet" 90223cd1385SRemy Bohmer : "CDC Ethernet Subset")); 90323cd1385SRemy Bohmer } 90423cd1385SRemy Bohmer return result; 90523cd1385SRemy Bohmer } 90623cd1385SRemy Bohmer 90723cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 90823cd1385SRemy Bohmer 90923cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC 91023cd1385SRemy Bohmer 91123cd1385SRemy Bohmer /* The interrupt endpoint is used in CDC networking models (Ethernet, ATM) 91223cd1385SRemy Bohmer * only to notify the host about link status changes (which we support) or 91323cd1385SRemy Bohmer * report completion of some encapsulated command. Since 91423cd1385SRemy Bohmer * we want this CDC Ethernet code to be vendor-neutral, we don't use that 91523cd1385SRemy Bohmer * command mechanism; and only one status request is ever queued. 91623cd1385SRemy Bohmer */ 91723cd1385SRemy Bohmer static void eth_status_complete (struct usb_ep *ep, struct usb_request *req) 91823cd1385SRemy Bohmer { 91923cd1385SRemy Bohmer struct usb_cdc_notification *event = req->buf; 92023cd1385SRemy Bohmer int value = req->status; 92123cd1385SRemy Bohmer struct eth_dev *dev = ep->driver_data; 92223cd1385SRemy Bohmer 92323cd1385SRemy Bohmer /* issue the second notification if host reads the first */ 92423cd1385SRemy Bohmer if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION 92523cd1385SRemy Bohmer && value == 0) { 92623cd1385SRemy Bohmer __le32 *data = req->buf + sizeof *event; 92723cd1385SRemy Bohmer 92823cd1385SRemy Bohmer event->bmRequestType = 0xA1; 92923cd1385SRemy Bohmer event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE; 93023cd1385SRemy Bohmer event->wValue = __constant_cpu_to_le16 (0); 93123cd1385SRemy Bohmer event->wIndex = __constant_cpu_to_le16 (1); 93223cd1385SRemy Bohmer event->wLength = __constant_cpu_to_le16 (8); 93323cd1385SRemy Bohmer 93423cd1385SRemy Bohmer /* SPEED_CHANGE data is up/down speeds in bits/sec */ 93523cd1385SRemy Bohmer data [0] = data [1] = cpu_to_le32 (BITRATE (dev->gadget)); 93623cd1385SRemy Bohmer 93723cd1385SRemy Bohmer req->length = STATUS_BYTECOUNT; 93823cd1385SRemy Bohmer value = usb_ep_queue (ep, req, GFP_ATOMIC); 939*7de73185SVitaly Kuzmichev debug("send SPEED_CHANGE --> %d\n", value); 94023cd1385SRemy Bohmer if (value == 0) 94123cd1385SRemy Bohmer return; 94223cd1385SRemy Bohmer } else if (value != -ECONNRESET) { 943*7de73185SVitaly Kuzmichev debug("event %02x --> %d\n", 94423cd1385SRemy Bohmer event->bNotificationType, value); 94523cd1385SRemy Bohmer if (event->bNotificationType== 94623cd1385SRemy Bohmer USB_CDC_NOTIFY_SPEED_CHANGE) 94723cd1385SRemy Bohmer { 94823cd1385SRemy Bohmer l_ethdev.network_started=1; 94923cd1385SRemy Bohmer printf("USB network up!\n"); 95023cd1385SRemy Bohmer } 95123cd1385SRemy Bohmer } 95223cd1385SRemy Bohmer req->context = NULL; 95323cd1385SRemy Bohmer } 95423cd1385SRemy Bohmer 95523cd1385SRemy Bohmer static void issue_start_status (struct eth_dev *dev) 95623cd1385SRemy Bohmer { 95723cd1385SRemy Bohmer struct usb_request *req = dev->stat_req; 95823cd1385SRemy Bohmer struct usb_cdc_notification *event; 95923cd1385SRemy Bohmer int value; 96023cd1385SRemy Bohmer 96123cd1385SRemy Bohmer /* flush old status 96223cd1385SRemy Bohmer * 96323cd1385SRemy Bohmer * FIXME ugly idiom, maybe we'd be better with just 96423cd1385SRemy Bohmer * a "cancel the whole queue" primitive since any 96523cd1385SRemy Bohmer * unlink-one primitive has way too many error modes. 96623cd1385SRemy Bohmer * here, we "know" toggle is already clear... 96723cd1385SRemy Bohmer * 96823cd1385SRemy Bohmer * FIXME iff req->context != null just dequeue it 96923cd1385SRemy Bohmer */ 97023cd1385SRemy Bohmer usb_ep_disable (dev->status_ep); 97123cd1385SRemy Bohmer usb_ep_enable (dev->status_ep, dev->status); 97223cd1385SRemy Bohmer 97323cd1385SRemy Bohmer /* 3.8.1 says to issue first NETWORK_CONNECTION, then 97423cd1385SRemy Bohmer * a SPEED_CHANGE. could be useful in some configs. 97523cd1385SRemy Bohmer */ 97623cd1385SRemy Bohmer event = req->buf; 97723cd1385SRemy Bohmer event->bmRequestType = 0xA1; 97823cd1385SRemy Bohmer event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION; 97923cd1385SRemy Bohmer event->wValue = __constant_cpu_to_le16 (1); /* connected */ 98023cd1385SRemy Bohmer event->wIndex = __constant_cpu_to_le16 (1); 98123cd1385SRemy Bohmer event->wLength = 0; 98223cd1385SRemy Bohmer 98323cd1385SRemy Bohmer req->length = sizeof *event; 98423cd1385SRemy Bohmer req->complete = eth_status_complete; 98523cd1385SRemy Bohmer req->context = dev; 98623cd1385SRemy Bohmer 98723cd1385SRemy Bohmer value = usb_ep_queue (dev->status_ep, req, GFP_ATOMIC); 98823cd1385SRemy Bohmer if (value < 0) 989*7de73185SVitaly Kuzmichev debug("status buf queue --> %d\n", value); 99023cd1385SRemy Bohmer } 99123cd1385SRemy Bohmer 99223cd1385SRemy Bohmer #endif 99323cd1385SRemy Bohmer 99423cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 99523cd1385SRemy Bohmer 99623cd1385SRemy Bohmer static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req) 99723cd1385SRemy Bohmer { 99823cd1385SRemy Bohmer if (req->status || req->actual != req->length) 999*7de73185SVitaly Kuzmichev debug("setup complete --> %d, %d/%d\n", 100023cd1385SRemy Bohmer req->status, req->actual, req->length); 100123cd1385SRemy Bohmer } 100223cd1385SRemy Bohmer 100323cd1385SRemy Bohmer /* 100423cd1385SRemy Bohmer * The setup() callback implements all the ep0 functionality that's not 100523cd1385SRemy Bohmer * handled lower down. CDC has a number of less-common features: 100623cd1385SRemy Bohmer * 100723cd1385SRemy Bohmer * - two interfaces: control, and ethernet data 100823cd1385SRemy Bohmer * - Ethernet data interface has two altsettings: default, and active 100923cd1385SRemy Bohmer * - class-specific descriptors for the control interface 101023cd1385SRemy Bohmer * - class-specific control requests 101123cd1385SRemy Bohmer */ 101223cd1385SRemy Bohmer static int 101323cd1385SRemy Bohmer eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) 101423cd1385SRemy Bohmer { 101523cd1385SRemy Bohmer struct eth_dev *dev = get_gadget_data (gadget); 101623cd1385SRemy Bohmer struct usb_request *req = dev->req; 101723cd1385SRemy Bohmer int value = -EOPNOTSUPP; 101823cd1385SRemy Bohmer u16 wIndex = le16_to_cpu(ctrl->wIndex); 101923cd1385SRemy Bohmer u16 wValue = le16_to_cpu(ctrl->wValue); 102023cd1385SRemy Bohmer u16 wLength = le16_to_cpu(ctrl->wLength); 102123cd1385SRemy Bohmer 102223cd1385SRemy Bohmer /* descriptors just go into the pre-allocated ep0 buffer, 102323cd1385SRemy Bohmer * while config change events may enable network traffic. 102423cd1385SRemy Bohmer */ 102523cd1385SRemy Bohmer 1026*7de73185SVitaly Kuzmichev debug("%s\n", __func__); 102723cd1385SRemy Bohmer 102823cd1385SRemy Bohmer req->complete = eth_setup_complete; 102923cd1385SRemy Bohmer switch (ctrl->bRequest) { 103023cd1385SRemy Bohmer 103123cd1385SRemy Bohmer case USB_REQ_GET_DESCRIPTOR: 103223cd1385SRemy Bohmer if (ctrl->bRequestType != USB_DIR_IN) 103323cd1385SRemy Bohmer break; 103423cd1385SRemy Bohmer switch (wValue >> 8) { 103523cd1385SRemy Bohmer 103623cd1385SRemy Bohmer case USB_DT_DEVICE: 103723cd1385SRemy Bohmer value = min (wLength, (u16) sizeof device_desc); 103823cd1385SRemy Bohmer memcpy (req->buf, &device_desc, value); 103923cd1385SRemy Bohmer break; 104023cd1385SRemy Bohmer case USB_DT_DEVICE_QUALIFIER: 104123cd1385SRemy Bohmer if (!gadget_is_dualspeed(gadget)) 104223cd1385SRemy Bohmer break; 104323cd1385SRemy Bohmer value = min (wLength, (u16) sizeof dev_qualifier); 104423cd1385SRemy Bohmer memcpy (req->buf, &dev_qualifier, value); 104523cd1385SRemy Bohmer break; 104623cd1385SRemy Bohmer 104723cd1385SRemy Bohmer case USB_DT_OTHER_SPEED_CONFIG: 104823cd1385SRemy Bohmer if (!gadget_is_dualspeed(gadget)) 104923cd1385SRemy Bohmer break; 105023cd1385SRemy Bohmer /* FALLTHROUGH */ 105123cd1385SRemy Bohmer case USB_DT_CONFIG: 105223cd1385SRemy Bohmer value = config_buf(gadget, req->buf, 105323cd1385SRemy Bohmer wValue >> 8, 105423cd1385SRemy Bohmer wValue & 0xff, 105523cd1385SRemy Bohmer gadget_is_otg(gadget)); 105623cd1385SRemy Bohmer if (value >= 0) 105723cd1385SRemy Bohmer value = min (wLength, (u16) value); 105823cd1385SRemy Bohmer break; 105923cd1385SRemy Bohmer 106023cd1385SRemy Bohmer case USB_DT_STRING: 106123cd1385SRemy Bohmer value = usb_gadget_get_string (&stringtab, 106223cd1385SRemy Bohmer wValue & 0xff, req->buf); 106323cd1385SRemy Bohmer 106423cd1385SRemy Bohmer if (value >= 0) 106523cd1385SRemy Bohmer value = min (wLength, (u16) value); 106623cd1385SRemy Bohmer 106723cd1385SRemy Bohmer break; 106823cd1385SRemy Bohmer } 106923cd1385SRemy Bohmer break; 107023cd1385SRemy Bohmer 107123cd1385SRemy Bohmer case USB_REQ_SET_CONFIGURATION: 107223cd1385SRemy Bohmer if (ctrl->bRequestType != 0) 107323cd1385SRemy Bohmer break; 107423cd1385SRemy Bohmer if (gadget->a_hnp_support) 1075*7de73185SVitaly Kuzmichev debug("HNP available\n"); 107623cd1385SRemy Bohmer else if (gadget->a_alt_hnp_support) 1077*7de73185SVitaly Kuzmichev debug("HNP needs a different root port\n"); 107823cd1385SRemy Bohmer value = eth_set_config (dev, wValue, GFP_ATOMIC); 107923cd1385SRemy Bohmer break; 108023cd1385SRemy Bohmer case USB_REQ_GET_CONFIGURATION: 108123cd1385SRemy Bohmer if (ctrl->bRequestType != USB_DIR_IN) 108223cd1385SRemy Bohmer break; 108323cd1385SRemy Bohmer *(u8 *)req->buf = dev->config; 108423cd1385SRemy Bohmer value = min (wLength, (u16) 1); 108523cd1385SRemy Bohmer break; 108623cd1385SRemy Bohmer 108723cd1385SRemy Bohmer case USB_REQ_SET_INTERFACE: 108823cd1385SRemy Bohmer if (ctrl->bRequestType != USB_RECIP_INTERFACE 108923cd1385SRemy Bohmer || !dev->config 109023cd1385SRemy Bohmer || wIndex > 1) 109123cd1385SRemy Bohmer break; 109223cd1385SRemy Bohmer if (!cdc_active(dev) && wIndex != 0) 109323cd1385SRemy Bohmer break; 109423cd1385SRemy Bohmer 109523cd1385SRemy Bohmer /* PXA hardware partially handles SET_INTERFACE; 109623cd1385SRemy Bohmer * we need to kluge around that interference. 109723cd1385SRemy Bohmer */ 109823cd1385SRemy Bohmer if (gadget_is_pxa (gadget)) { 109923cd1385SRemy Bohmer value = eth_set_config (dev, DEV_CONFIG_VALUE, 110023cd1385SRemy Bohmer GFP_ATOMIC); 110123cd1385SRemy Bohmer goto done_set_intf; 110223cd1385SRemy Bohmer } 110323cd1385SRemy Bohmer 110423cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC 110523cd1385SRemy Bohmer switch (wIndex) { 110623cd1385SRemy Bohmer case 0: /* control/master intf */ 110723cd1385SRemy Bohmer if (wValue != 0) 110823cd1385SRemy Bohmer break; 110923cd1385SRemy Bohmer if (dev->status) { 111023cd1385SRemy Bohmer usb_ep_disable (dev->status_ep); 111123cd1385SRemy Bohmer usb_ep_enable (dev->status_ep, dev->status); 111223cd1385SRemy Bohmer } 111323cd1385SRemy Bohmer value = 0; 111423cd1385SRemy Bohmer break; 111523cd1385SRemy Bohmer case 1: /* data intf */ 111623cd1385SRemy Bohmer if (wValue > 1) 111723cd1385SRemy Bohmer break; 111823cd1385SRemy Bohmer usb_ep_disable (dev->in_ep); 111923cd1385SRemy Bohmer usb_ep_disable (dev->out_ep); 112023cd1385SRemy Bohmer 112123cd1385SRemy Bohmer /* CDC requires the data transfers not be done from 112223cd1385SRemy Bohmer * the default interface setting ... also, setting 112323cd1385SRemy Bohmer * the non-default interface resets filters etc. 112423cd1385SRemy Bohmer */ 112523cd1385SRemy Bohmer if (wValue == 1) { 112623cd1385SRemy Bohmer if (!cdc_active (dev)) 112723cd1385SRemy Bohmer break; 112823cd1385SRemy Bohmer usb_ep_enable (dev->in_ep, dev->in); 112923cd1385SRemy Bohmer usb_ep_enable (dev->out_ep, dev->out); 113023cd1385SRemy Bohmer dev->cdc_filter = DEFAULT_FILTER; 113123cd1385SRemy Bohmer if (dev->status) 113223cd1385SRemy Bohmer issue_start_status (dev); 113323cd1385SRemy Bohmer } 113423cd1385SRemy Bohmer 113523cd1385SRemy Bohmer value = 0; 113623cd1385SRemy Bohmer break; 113723cd1385SRemy Bohmer } 113823cd1385SRemy Bohmer #else 113923cd1385SRemy Bohmer /* FIXME this is wrong, as is the assumption that 114023cd1385SRemy Bohmer * all non-PXA hardware talks real CDC ... 114123cd1385SRemy Bohmer */ 1142*7de73185SVitaly Kuzmichev debug("set_interface ignored!\n"); 114323cd1385SRemy Bohmer #endif /* DEV_CONFIG_CDC */ 114423cd1385SRemy Bohmer 114523cd1385SRemy Bohmer done_set_intf: 114623cd1385SRemy Bohmer break; 114723cd1385SRemy Bohmer case USB_REQ_GET_INTERFACE: 114823cd1385SRemy Bohmer if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE) 114923cd1385SRemy Bohmer || !dev->config 115023cd1385SRemy Bohmer || wIndex > 1) 115123cd1385SRemy Bohmer break; 115223cd1385SRemy Bohmer if (!(cdc_active(dev)) && wIndex != 0) 115323cd1385SRemy Bohmer break; 115423cd1385SRemy Bohmer 115523cd1385SRemy Bohmer /* for CDC, iff carrier is on, data interface is active. */ 115623cd1385SRemy Bohmer if (wIndex != 1) 115723cd1385SRemy Bohmer *(u8 *)req->buf = 0; 115823cd1385SRemy Bohmer else { 115923cd1385SRemy Bohmer /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */ 116023cd1385SRemy Bohmer /* carrier always ok ...*/ 116123cd1385SRemy Bohmer *(u8 *)req->buf = 1 ; 116223cd1385SRemy Bohmer } 116323cd1385SRemy Bohmer value = min (wLength, (u16) 1); 116423cd1385SRemy Bohmer break; 116523cd1385SRemy Bohmer 116623cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC 116723cd1385SRemy Bohmer case USB_CDC_SET_ETHERNET_PACKET_FILTER: 116823cd1385SRemy Bohmer /* see 6.2.30: no data, wIndex = interface, 116923cd1385SRemy Bohmer * wValue = packet filter bitmap 117023cd1385SRemy Bohmer */ 117123cd1385SRemy Bohmer if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE) 117223cd1385SRemy Bohmer || !cdc_active(dev) 117323cd1385SRemy Bohmer || wLength != 0 117423cd1385SRemy Bohmer || wIndex > 1) 117523cd1385SRemy Bohmer break; 1176*7de73185SVitaly Kuzmichev debug("packet filter %02x\n", wValue); 117723cd1385SRemy Bohmer dev->cdc_filter = wValue; 117823cd1385SRemy Bohmer value = 0; 117923cd1385SRemy Bohmer break; 118023cd1385SRemy Bohmer 118123cd1385SRemy Bohmer /* and potentially: 118223cd1385SRemy Bohmer * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS: 118323cd1385SRemy Bohmer * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER: 118423cd1385SRemy Bohmer * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER: 118523cd1385SRemy Bohmer * case USB_CDC_GET_ETHERNET_STATISTIC: 118623cd1385SRemy Bohmer */ 118723cd1385SRemy Bohmer 118823cd1385SRemy Bohmer #endif /* DEV_CONFIG_CDC */ 118923cd1385SRemy Bohmer 119023cd1385SRemy Bohmer default: 1191*7de73185SVitaly Kuzmichev debug("unknown control req%02x.%02x v%04x i%04x l%d\n", 119223cd1385SRemy Bohmer ctrl->bRequestType, ctrl->bRequest, 119323cd1385SRemy Bohmer wValue, wIndex, wLength); 119423cd1385SRemy Bohmer } 119523cd1385SRemy Bohmer 119623cd1385SRemy Bohmer /* respond with data transfer before status phase? */ 119723cd1385SRemy Bohmer if (value >= 0) { 1198*7de73185SVitaly Kuzmichev debug("respond with data transfer before status phase\n"); 119923cd1385SRemy Bohmer req->length = value; 120023cd1385SRemy Bohmer req->zero = value < wLength 120123cd1385SRemy Bohmer && (value % gadget->ep0->maxpacket) == 0; 120223cd1385SRemy Bohmer value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC); 120323cd1385SRemy Bohmer if (value < 0) { 1204*7de73185SVitaly Kuzmichev debug("ep_queue --> %d\n", value); 120523cd1385SRemy Bohmer req->status = 0; 120623cd1385SRemy Bohmer eth_setup_complete (gadget->ep0, req); 120723cd1385SRemy Bohmer } 120823cd1385SRemy Bohmer } 120923cd1385SRemy Bohmer 121023cd1385SRemy Bohmer /* host either stalls (value < 0) or reports success */ 121123cd1385SRemy Bohmer return value; 121223cd1385SRemy Bohmer } 121323cd1385SRemy Bohmer 121423cd1385SRemy Bohmer 121523cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 121623cd1385SRemy Bohmer 121723cd1385SRemy Bohmer static void rx_complete (struct usb_ep *ep, struct usb_request *req); 121823cd1385SRemy Bohmer 121923cd1385SRemy Bohmer static int rx_submit ( struct eth_dev *dev, struct usb_request *req, \ 122023cd1385SRemy Bohmer gfp_t gfp_flags) 122123cd1385SRemy Bohmer { 122223cd1385SRemy Bohmer int retval = -ENOMEM; 122323cd1385SRemy Bohmer size_t size; 122423cd1385SRemy Bohmer 122523cd1385SRemy Bohmer /* Padding up to RX_EXTRA handles minor disagreements with host. 122623cd1385SRemy Bohmer * Normally we use the USB "terminate on short read" convention; 122723cd1385SRemy Bohmer * so allow up to (N*maxpacket), since that memory is normally 122823cd1385SRemy Bohmer * already allocated. Some hardware doesn't deal well with short 122923cd1385SRemy Bohmer * reads (e.g. DMA must be N*maxpacket), so for now don't trim a 123023cd1385SRemy Bohmer * byte off the end (to force hardware errors on overflow). 123123cd1385SRemy Bohmer */ 123223cd1385SRemy Bohmer 1233*7de73185SVitaly Kuzmichev debug("%s\n", __func__); 123423cd1385SRemy Bohmer 123523cd1385SRemy Bohmer size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA); 123623cd1385SRemy Bohmer size += dev->out_ep->maxpacket - 1; 123723cd1385SRemy Bohmer size -= size % dev->out_ep->maxpacket; 123823cd1385SRemy Bohmer 123923cd1385SRemy Bohmer 124023cd1385SRemy Bohmer /* Some platforms perform better when IP packets are aligned, 124123cd1385SRemy Bohmer * but on at least one, checksumming fails otherwise. 124223cd1385SRemy Bohmer */ 124323cd1385SRemy Bohmer 124423cd1385SRemy Bohmer req->buf = (u8 *) NetRxPackets[0]; 124523cd1385SRemy Bohmer req->length = size; 124623cd1385SRemy Bohmer req->complete = rx_complete; 124723cd1385SRemy Bohmer 124823cd1385SRemy Bohmer retval = usb_ep_queue (dev->out_ep, req, gfp_flags); 124923cd1385SRemy Bohmer 125023cd1385SRemy Bohmer if (retval) { 1251*7de73185SVitaly Kuzmichev error("rx submit --> %d", retval); 125223cd1385SRemy Bohmer } 125323cd1385SRemy Bohmer return retval; 125423cd1385SRemy Bohmer } 125523cd1385SRemy Bohmer 125623cd1385SRemy Bohmer 125723cd1385SRemy Bohmer static void rx_complete (struct usb_ep *ep, struct usb_request *req) 125823cd1385SRemy Bohmer { 125923cd1385SRemy Bohmer struct eth_dev *dev = ep->driver_data; 126023cd1385SRemy Bohmer 1261*7de73185SVitaly Kuzmichev debug("%s: status %d\n", __func__, req->status); 126223cd1385SRemy Bohmer 126323cd1385SRemy Bohmer packet_received=1; 126423cd1385SRemy Bohmer 126523cd1385SRemy Bohmer if (req) 126623cd1385SRemy Bohmer dev->rx_req=req; 126723cd1385SRemy Bohmer } 126823cd1385SRemy Bohmer 126923cd1385SRemy Bohmer 127023cd1385SRemy Bohmer static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags) 127123cd1385SRemy Bohmer { 127223cd1385SRemy Bohmer 127323cd1385SRemy Bohmer dev->tx_req = usb_ep_alloc_request (dev->in_ep, 0); 127423cd1385SRemy Bohmer 127523cd1385SRemy Bohmer if (!dev->tx_req) 127623cd1385SRemy Bohmer goto fail; 127723cd1385SRemy Bohmer 127823cd1385SRemy Bohmer dev->rx_req = usb_ep_alloc_request (dev->out_ep, 0); 127923cd1385SRemy Bohmer 128023cd1385SRemy Bohmer if (!dev->rx_req) 128123cd1385SRemy Bohmer goto fail; 128223cd1385SRemy Bohmer 128323cd1385SRemy Bohmer return 0; 128423cd1385SRemy Bohmer 128523cd1385SRemy Bohmer fail: 1286*7de73185SVitaly Kuzmichev error("can't alloc requests"); 128723cd1385SRemy Bohmer return -1; 128823cd1385SRemy Bohmer } 128923cd1385SRemy Bohmer 129023cd1385SRemy Bohmer 129123cd1385SRemy Bohmer static void tx_complete (struct usb_ep *ep, struct usb_request *req) 129223cd1385SRemy Bohmer { 1293*7de73185SVitaly Kuzmichev debug("%s: status %s\n", __func__, (req->status)?"failed":"ok"); 129423cd1385SRemy Bohmer packet_sent=1; 129523cd1385SRemy Bohmer } 129623cd1385SRemy Bohmer 129723cd1385SRemy Bohmer static inline int eth_is_promisc (struct eth_dev *dev) 129823cd1385SRemy Bohmer { 129923cd1385SRemy Bohmer /* no filters for the CDC subset; always promisc */ 130023cd1385SRemy Bohmer if (subset_active (dev)) 130123cd1385SRemy Bohmer return 1; 130223cd1385SRemy Bohmer return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS; 130323cd1385SRemy Bohmer } 130423cd1385SRemy Bohmer 130523cd1385SRemy Bohmer #if 0 130623cd1385SRemy Bohmer static int eth_start_xmit (struct sk_buff *skb, struct net_device *net) 130723cd1385SRemy Bohmer { 130823cd1385SRemy Bohmer struct eth_dev *dev = netdev_priv(net); 130923cd1385SRemy Bohmer int length = skb->len; 131023cd1385SRemy Bohmer int retval; 131123cd1385SRemy Bohmer struct usb_request *req = NULL; 131223cd1385SRemy Bohmer unsigned long flags; 131323cd1385SRemy Bohmer 131423cd1385SRemy Bohmer /* apply outgoing CDC or RNDIS filters */ 131523cd1385SRemy Bohmer if (!eth_is_promisc (dev)) { 131623cd1385SRemy Bohmer u8 *dest = skb->data; 131723cd1385SRemy Bohmer 131823cd1385SRemy Bohmer if (is_multicast_ether_addr(dest)) { 131923cd1385SRemy Bohmer u16 type; 132023cd1385SRemy Bohmer 132123cd1385SRemy Bohmer /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host 132223cd1385SRemy Bohmer * SET_ETHERNET_MULTICAST_FILTERS requests 132323cd1385SRemy Bohmer */ 132423cd1385SRemy Bohmer if (is_broadcast_ether_addr(dest)) 132523cd1385SRemy Bohmer type = USB_CDC_PACKET_TYPE_BROADCAST; 132623cd1385SRemy Bohmer else 132723cd1385SRemy Bohmer type = USB_CDC_PACKET_TYPE_ALL_MULTICAST; 132823cd1385SRemy Bohmer if (!(dev->cdc_filter & type)) { 132923cd1385SRemy Bohmer dev_kfree_skb_any (skb); 133023cd1385SRemy Bohmer return 0; 133123cd1385SRemy Bohmer } 133223cd1385SRemy Bohmer } 133323cd1385SRemy Bohmer /* ignores USB_CDC_PACKET_TYPE_DIRECTED */ 133423cd1385SRemy Bohmer } 133523cd1385SRemy Bohmer 133623cd1385SRemy Bohmer spin_lock_irqsave(&dev->req_lock, flags); 133723cd1385SRemy Bohmer /* 133823cd1385SRemy Bohmer * this freelist can be empty if an interrupt triggered disconnect() 133923cd1385SRemy Bohmer * and reconfigured the gadget (shutting down this queue) after the 134023cd1385SRemy Bohmer * network stack decided to xmit but before we got the spinlock. 134123cd1385SRemy Bohmer */ 134223cd1385SRemy Bohmer if (list_empty(&dev->tx_reqs)) { 134323cd1385SRemy Bohmer spin_unlock_irqrestore(&dev->req_lock, flags); 134423cd1385SRemy Bohmer return 1; 134523cd1385SRemy Bohmer } 134623cd1385SRemy Bohmer 134723cd1385SRemy Bohmer req = container_of (dev->tx_reqs.next, struct usb_request, list); 134823cd1385SRemy Bohmer list_del (&req->list); 134923cd1385SRemy Bohmer 135023cd1385SRemy Bohmer /* temporarily stop TX queue when the freelist empties */ 135123cd1385SRemy Bohmer if (list_empty (&dev->tx_reqs)) 135223cd1385SRemy Bohmer netif_stop_queue (net); 135323cd1385SRemy Bohmer spin_unlock_irqrestore(&dev->req_lock, flags); 135423cd1385SRemy Bohmer 135523cd1385SRemy Bohmer /* no buffer copies needed, unless the network stack did it 135623cd1385SRemy Bohmer * or the hardware can't use skb buffers. 135723cd1385SRemy Bohmer * or there's not enough space for any RNDIS headers we need 135823cd1385SRemy Bohmer */ 135923cd1385SRemy Bohmer if (rndis_active(dev)) { 136023cd1385SRemy Bohmer struct sk_buff *skb_rndis; 136123cd1385SRemy Bohmer 136223cd1385SRemy Bohmer skb_rndis = skb_realloc_headroom (skb, 136323cd1385SRemy Bohmer sizeof (struct rndis_packet_msg_type)); 136423cd1385SRemy Bohmer if (!skb_rndis) 136523cd1385SRemy Bohmer goto drop; 136623cd1385SRemy Bohmer 136723cd1385SRemy Bohmer dev_kfree_skb_any (skb); 136823cd1385SRemy Bohmer skb = skb_rndis; 136923cd1385SRemy Bohmer rndis_add_hdr (skb); 137023cd1385SRemy Bohmer length = skb->len; 137123cd1385SRemy Bohmer } 137223cd1385SRemy Bohmer req->buf = skb->data; 137323cd1385SRemy Bohmer req->context = skb; 137423cd1385SRemy Bohmer req->complete = tx_complete; 137523cd1385SRemy Bohmer 137623cd1385SRemy Bohmer /* use zlp framing on tx for strict CDC-Ether conformance, 137723cd1385SRemy Bohmer * though any robust network rx path ignores extra padding. 137823cd1385SRemy Bohmer * and some hardware doesn't like to write zlps. 137923cd1385SRemy Bohmer */ 138023cd1385SRemy Bohmer req->zero = 1; 138123cd1385SRemy Bohmer if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0) 138223cd1385SRemy Bohmer length++; 138323cd1385SRemy Bohmer 138423cd1385SRemy Bohmer req->length = length; 138523cd1385SRemy Bohmer 138623cd1385SRemy Bohmer /* throttle highspeed IRQ rate back slightly */ 138723cd1385SRemy Bohmer if (gadget_is_dualspeed(dev->gadget)) 138823cd1385SRemy Bohmer req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH) 138923cd1385SRemy Bohmer ? ((atomic_read(&dev->tx_qlen) % qmult) != 0) 139023cd1385SRemy Bohmer : 0; 139123cd1385SRemy Bohmer 139223cd1385SRemy Bohmer retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC); 139323cd1385SRemy Bohmer switch (retval) { 139423cd1385SRemy Bohmer default: 139523cd1385SRemy Bohmer DEBUG (dev, "tx queue err %d\n", retval); 139623cd1385SRemy Bohmer break; 139723cd1385SRemy Bohmer case 0: 139823cd1385SRemy Bohmer net->trans_start = jiffies; 139923cd1385SRemy Bohmer atomic_inc (&dev->tx_qlen); 140023cd1385SRemy Bohmer } 140123cd1385SRemy Bohmer 140223cd1385SRemy Bohmer if (retval) { 140323cd1385SRemy Bohmer drop: 140423cd1385SRemy Bohmer dev->stats.tx_dropped++; 140523cd1385SRemy Bohmer dev_kfree_skb_any (skb); 140623cd1385SRemy Bohmer spin_lock_irqsave(&dev->req_lock, flags); 140723cd1385SRemy Bohmer if (list_empty (&dev->tx_reqs)) 140823cd1385SRemy Bohmer netif_start_queue (net); 140923cd1385SRemy Bohmer list_add (&req->list, &dev->tx_reqs); 141023cd1385SRemy Bohmer spin_unlock_irqrestore(&dev->req_lock, flags); 141123cd1385SRemy Bohmer } 141223cd1385SRemy Bohmer return 0; 141323cd1385SRemy Bohmer } 141423cd1385SRemy Bohmer 141523cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 141623cd1385SRemy Bohmer #endif 141723cd1385SRemy Bohmer 141823cd1385SRemy Bohmer static void eth_unbind (struct usb_gadget *gadget) 141923cd1385SRemy Bohmer { 142023cd1385SRemy Bohmer struct eth_dev *dev = get_gadget_data (gadget); 142123cd1385SRemy Bohmer 1422*7de73185SVitaly Kuzmichev debug("%s...\n", __func__); 142323cd1385SRemy Bohmer 142423cd1385SRemy Bohmer if (dev->stat_req) { 142523cd1385SRemy Bohmer usb_ep_free_request (dev->status_ep, dev->stat_req); 142623cd1385SRemy Bohmer dev->stat_req = NULL; 142723cd1385SRemy Bohmer } 142823cd1385SRemy Bohmer 142923cd1385SRemy Bohmer if (dev->tx_req) { 143023cd1385SRemy Bohmer usb_ep_free_request (dev->in_ep, dev->tx_req); 143123cd1385SRemy Bohmer dev->tx_req=NULL; 143223cd1385SRemy Bohmer } 143323cd1385SRemy Bohmer 143423cd1385SRemy Bohmer if (dev->rx_req) { 143523cd1385SRemy Bohmer usb_ep_free_request (dev->in_ep, dev->rx_req); 143623cd1385SRemy Bohmer dev->rx_req=NULL; 143723cd1385SRemy Bohmer } 143823cd1385SRemy Bohmer 143923cd1385SRemy Bohmer /* unregister_netdev (dev->net);*/ 144023cd1385SRemy Bohmer /* free_netdev(dev->net);*/ 144123cd1385SRemy Bohmer 144223cd1385SRemy Bohmer set_gadget_data (gadget, NULL); 144323cd1385SRemy Bohmer } 144423cd1385SRemy Bohmer 144523cd1385SRemy Bohmer static void eth_disconnect (struct usb_gadget *gadget) 144623cd1385SRemy Bohmer { 144723cd1385SRemy Bohmer eth_reset_config (get_gadget_data (gadget)); 144823cd1385SRemy Bohmer } 144923cd1385SRemy Bohmer 145023cd1385SRemy Bohmer static void eth_suspend (struct usb_gadget *gadget) 145123cd1385SRemy Bohmer { 145223cd1385SRemy Bohmer /* Not used */ 145323cd1385SRemy Bohmer } 145423cd1385SRemy Bohmer 145523cd1385SRemy Bohmer static void eth_resume (struct usb_gadget *gadget) 145623cd1385SRemy Bohmer { 145723cd1385SRemy Bohmer /* Not used */ 145823cd1385SRemy Bohmer } 145923cd1385SRemy Bohmer 146023cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 146123cd1385SRemy Bohmer 146223cd1385SRemy Bohmer static int is_eth_addr_valid(char *str) 146323cd1385SRemy Bohmer { 146423cd1385SRemy Bohmer if (strlen(str) == 17) { 146523cd1385SRemy Bohmer int i; 146623cd1385SRemy Bohmer char *p, *q; 146723cd1385SRemy Bohmer uchar ea[6]; 146823cd1385SRemy Bohmer 146923cd1385SRemy Bohmer /* see if it looks like an ethernet address */ 147023cd1385SRemy Bohmer 147123cd1385SRemy Bohmer p = str; 147223cd1385SRemy Bohmer 147323cd1385SRemy Bohmer for (i = 0; i < 6; i++) { 147423cd1385SRemy Bohmer char term = (i == 5 ? '\0' : ':'); 147523cd1385SRemy Bohmer 147623cd1385SRemy Bohmer ea[i] = simple_strtol(p, &q, 16); 147723cd1385SRemy Bohmer 147823cd1385SRemy Bohmer if ((q - p) != 2 || *q++ != term) 147923cd1385SRemy Bohmer break; 148023cd1385SRemy Bohmer 148123cd1385SRemy Bohmer p = q; 148223cd1385SRemy Bohmer } 148323cd1385SRemy Bohmer 148423cd1385SRemy Bohmer if (i == 6) /* it looks ok */ 148523cd1385SRemy Bohmer return 1; 148623cd1385SRemy Bohmer } 148723cd1385SRemy Bohmer return 0; 148823cd1385SRemy Bohmer } 148923cd1385SRemy Bohmer 149023cd1385SRemy Bohmer static u8 nibble (unsigned char c) 149123cd1385SRemy Bohmer { 149223cd1385SRemy Bohmer if (likely (isdigit (c))) 149323cd1385SRemy Bohmer return c - '0'; 149423cd1385SRemy Bohmer c = toupper (c); 149523cd1385SRemy Bohmer if (likely (isxdigit (c))) 149623cd1385SRemy Bohmer return 10 + c - 'A'; 149723cd1385SRemy Bohmer return 0; 149823cd1385SRemy Bohmer } 149923cd1385SRemy Bohmer 150023cd1385SRemy Bohmer static int get_ether_addr(const char *str, u8 *dev_addr) 150123cd1385SRemy Bohmer { 150223cd1385SRemy Bohmer if (str) { 150323cd1385SRemy Bohmer unsigned i; 150423cd1385SRemy Bohmer 150523cd1385SRemy Bohmer for (i = 0; i < 6; i++) { 150623cd1385SRemy Bohmer unsigned char num; 150723cd1385SRemy Bohmer 150823cd1385SRemy Bohmer if((*str == '.') || (*str == ':')) 150923cd1385SRemy Bohmer str++; 151023cd1385SRemy Bohmer num = nibble(*str++) << 4; 151123cd1385SRemy Bohmer num |= (nibble(*str++)); 151223cd1385SRemy Bohmer dev_addr [i] = num; 151323cd1385SRemy Bohmer } 151423cd1385SRemy Bohmer if (is_valid_ether_addr (dev_addr)) 151523cd1385SRemy Bohmer return 0; 151623cd1385SRemy Bohmer } 151723cd1385SRemy Bohmer return 1; 151823cd1385SRemy Bohmer } 151923cd1385SRemy Bohmer 152023cd1385SRemy Bohmer static int eth_bind(struct usb_gadget *gadget) 152123cd1385SRemy Bohmer { 152223cd1385SRemy Bohmer struct eth_dev *dev = &l_ethdev; 152323cd1385SRemy Bohmer u8 cdc = 1, zlp = 1; 152423cd1385SRemy Bohmer struct usb_ep *in_ep, *out_ep, *status_ep = NULL; 152523cd1385SRemy Bohmer int gcnum; 152623cd1385SRemy Bohmer u8 tmp[7]; 152723cd1385SRemy Bohmer 152823cd1385SRemy Bohmer /* these flags are only ever cleared; compiler take note */ 152923cd1385SRemy Bohmer #ifndef DEV_CONFIG_CDC 153023cd1385SRemy Bohmer cdc = 0; 153123cd1385SRemy Bohmer #endif 153223cd1385SRemy Bohmer /* Because most host side USB stacks handle CDC Ethernet, that 153323cd1385SRemy Bohmer * standard protocol is _strongly_ preferred for interop purposes. 153423cd1385SRemy Bohmer * (By everyone except Microsoft.) 153523cd1385SRemy Bohmer */ 153623cd1385SRemy Bohmer if (gadget_is_pxa (gadget)) { 153723cd1385SRemy Bohmer /* pxa doesn't support altsettings */ 153823cd1385SRemy Bohmer cdc = 0; 153923cd1385SRemy Bohmer } else if (gadget_is_musbhdrc(gadget)) { 154023cd1385SRemy Bohmer /* reduce tx dma overhead by avoiding special cases */ 154123cd1385SRemy Bohmer zlp = 0; 154223cd1385SRemy Bohmer } else if (gadget_is_sh(gadget)) { 154323cd1385SRemy Bohmer /* sh doesn't support multiple interfaces or configs */ 154423cd1385SRemy Bohmer cdc = 0; 154523cd1385SRemy Bohmer } else if (gadget_is_sa1100 (gadget)) { 154623cd1385SRemy Bohmer /* hardware can't write zlps */ 154723cd1385SRemy Bohmer zlp = 0; 154823cd1385SRemy Bohmer /* sa1100 CAN do CDC, without status endpoint ... we use 154923cd1385SRemy Bohmer * non-CDC to be compatible with ARM Linux-2.4 "usb-eth". 155023cd1385SRemy Bohmer */ 155123cd1385SRemy Bohmer cdc = 0; 155223cd1385SRemy Bohmer } 155323cd1385SRemy Bohmer 155423cd1385SRemy Bohmer gcnum = usb_gadget_controller_number (gadget); 155523cd1385SRemy Bohmer if (gcnum >= 0) 155623cd1385SRemy Bohmer device_desc.bcdDevice = cpu_to_le16 (0x0300 + gcnum); 155723cd1385SRemy Bohmer else { 155823cd1385SRemy Bohmer /* can't assume CDC works. don't want to default to 155923cd1385SRemy Bohmer * anything less functional on CDC-capable hardware, 156023cd1385SRemy Bohmer * so we fail in this case. 156123cd1385SRemy Bohmer */ 1562*7de73185SVitaly Kuzmichev error("controller '%s' not recognized", 156323cd1385SRemy Bohmer gadget->name); 156423cd1385SRemy Bohmer return -ENODEV; 156523cd1385SRemy Bohmer } 156623cd1385SRemy Bohmer 156723cd1385SRemy Bohmer /* CDC subset ... recognized by Linux since 2.4.10, but Windows 156823cd1385SRemy Bohmer * drivers aren't widely available. (That may be improved by 156923cd1385SRemy Bohmer * supporting one submode of the "SAFE" variant of MDLM.) 157023cd1385SRemy Bohmer */ 157123cd1385SRemy Bohmer if (!cdc) { 157223cd1385SRemy Bohmer device_desc.idVendor = 157323cd1385SRemy Bohmer __constant_cpu_to_le16(SIMPLE_VENDOR_NUM); 157423cd1385SRemy Bohmer device_desc.idProduct = 157523cd1385SRemy Bohmer __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM); 157623cd1385SRemy Bohmer } 157723cd1385SRemy Bohmer 157823cd1385SRemy Bohmer /* support optional vendor/distro customization */ 157923cd1385SRemy Bohmer #if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID) 158023cd1385SRemy Bohmer device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID); 158123cd1385SRemy Bohmer device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID); 158223cd1385SRemy Bohmer #endif 158323cd1385SRemy Bohmer if (bcdDevice) 158423cd1385SRemy Bohmer device_desc.bcdDevice = cpu_to_le16(bcdDevice); 158523cd1385SRemy Bohmer if (iManufacturer) 158623cd1385SRemy Bohmer strcpy (manufacturer, iManufacturer); 158723cd1385SRemy Bohmer if (iProduct) 158823cd1385SRemy Bohmer strcpy (product_desc, iProduct); 158923cd1385SRemy Bohmer if (iSerialNumber) { 159023cd1385SRemy Bohmer device_desc.iSerialNumber = STRING_SERIALNUMBER, 159123cd1385SRemy Bohmer strcpy(serial_number, iSerialNumber); 159223cd1385SRemy Bohmer } 159323cd1385SRemy Bohmer 159423cd1385SRemy Bohmer /* all we really need is bulk IN/OUT */ 159523cd1385SRemy Bohmer usb_ep_autoconfig_reset (gadget); 159623cd1385SRemy Bohmer in_ep = usb_ep_autoconfig (gadget, &fs_source_desc); 159723cd1385SRemy Bohmer if (!in_ep) { 159823cd1385SRemy Bohmer autoconf_fail: 1599*7de73185SVitaly Kuzmichev error("can't autoconfigure on %s\n", 160023cd1385SRemy Bohmer gadget->name); 160123cd1385SRemy Bohmer return -ENODEV; 160223cd1385SRemy Bohmer } 160323cd1385SRemy Bohmer in_ep->driver_data = in_ep; /* claim */ 160423cd1385SRemy Bohmer 160523cd1385SRemy Bohmer out_ep = usb_ep_autoconfig (gadget, &fs_sink_desc); 160623cd1385SRemy Bohmer if (!out_ep) 160723cd1385SRemy Bohmer goto autoconf_fail; 160823cd1385SRemy Bohmer out_ep->driver_data = out_ep; /* claim */ 160923cd1385SRemy Bohmer 161023cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC) 161123cd1385SRemy Bohmer /* CDC Ethernet control interface doesn't require a status endpoint. 161223cd1385SRemy Bohmer * Since some hosts expect one, try to allocate one anyway. 161323cd1385SRemy Bohmer */ 161423cd1385SRemy Bohmer if (cdc) { 161523cd1385SRemy Bohmer status_ep = usb_ep_autoconfig (gadget, &fs_status_desc); 161623cd1385SRemy Bohmer if (status_ep) { 161723cd1385SRemy Bohmer status_ep->driver_data = status_ep; /* claim */ 161823cd1385SRemy Bohmer } else if (cdc) { 161923cd1385SRemy Bohmer control_intf.bNumEndpoints = 0; 162023cd1385SRemy Bohmer /* FIXME remove endpoint from descriptor list */ 162123cd1385SRemy Bohmer } 162223cd1385SRemy Bohmer } 162323cd1385SRemy Bohmer #endif 162423cd1385SRemy Bohmer 162523cd1385SRemy Bohmer /* one config: cdc, else minimal subset */ 162623cd1385SRemy Bohmer if (!cdc) { 162723cd1385SRemy Bohmer eth_config.bNumInterfaces = 1; 162823cd1385SRemy Bohmer eth_config.iConfiguration = STRING_SUBSET; 162923cd1385SRemy Bohmer 163023cd1385SRemy Bohmer /* use functions to set these up, in case we're built to work 163123cd1385SRemy Bohmer * with multiple controllers and must override CDC Ethernet. 163223cd1385SRemy Bohmer */ 163323cd1385SRemy Bohmer fs_subset_descriptors(); 163423cd1385SRemy Bohmer hs_subset_descriptors(); 163523cd1385SRemy Bohmer } 163623cd1385SRemy Bohmer 163723cd1385SRemy Bohmer device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket; 163823cd1385SRemy Bohmer usb_gadget_set_selfpowered (gadget); 163923cd1385SRemy Bohmer 164023cd1385SRemy Bohmer if (gadget_is_dualspeed(gadget)) { 164123cd1385SRemy Bohmer if (!cdc) 164223cd1385SRemy Bohmer dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC; 164323cd1385SRemy Bohmer 164423cd1385SRemy Bohmer /* assumes ep0 uses the same value for both speeds ... */ 164523cd1385SRemy Bohmer dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0; 164623cd1385SRemy Bohmer 164723cd1385SRemy Bohmer /* and that all endpoints are dual-speed */ 164823cd1385SRemy Bohmer hs_source_desc.bEndpointAddress = 164923cd1385SRemy Bohmer fs_source_desc.bEndpointAddress; 165023cd1385SRemy Bohmer hs_sink_desc.bEndpointAddress = 165123cd1385SRemy Bohmer fs_sink_desc.bEndpointAddress; 165223cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC) 165323cd1385SRemy Bohmer if (status_ep) 165423cd1385SRemy Bohmer hs_status_desc.bEndpointAddress = 165523cd1385SRemy Bohmer fs_status_desc.bEndpointAddress; 165623cd1385SRemy Bohmer #endif 165723cd1385SRemy Bohmer } 165823cd1385SRemy Bohmer 165923cd1385SRemy Bohmer if (gadget_is_otg(gadget)) { 166023cd1385SRemy Bohmer otg_descriptor.bmAttributes |= USB_OTG_HNP, 166123cd1385SRemy Bohmer eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP; 166223cd1385SRemy Bohmer eth_config.bMaxPower = 4; 166323cd1385SRemy Bohmer } 166423cd1385SRemy Bohmer 166523cd1385SRemy Bohmer dev->net = &l_netdev; 166623cd1385SRemy Bohmer strcpy (dev->net->name, USB_NET_NAME); 166723cd1385SRemy Bohmer 166823cd1385SRemy Bohmer dev->cdc = cdc; 166923cd1385SRemy Bohmer dev->zlp = zlp; 167023cd1385SRemy Bohmer 167123cd1385SRemy Bohmer dev->in_ep = in_ep; 167223cd1385SRemy Bohmer dev->out_ep = out_ep; 167323cd1385SRemy Bohmer dev->status_ep = status_ep; 167423cd1385SRemy Bohmer 167523cd1385SRemy Bohmer /* Module params for these addresses should come from ID proms. 167623cd1385SRemy Bohmer * The host side address is used with CDC, and commonly 167723cd1385SRemy Bohmer * ends up in a persistent config database. It's not clear if 167823cd1385SRemy Bohmer * host side code for the SAFE thing cares -- its original BLAN 167923cd1385SRemy Bohmer * thing didn't, Sharp never assigned those addresses on Zaurii. 168023cd1385SRemy Bohmer */ 168123cd1385SRemy Bohmer get_ether_addr(dev_addr, dev->net->enetaddr); 168223cd1385SRemy Bohmer 168323cd1385SRemy Bohmer memset(tmp, 0, sizeof(tmp)); 168423cd1385SRemy Bohmer memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr)); 168523cd1385SRemy Bohmer 168623cd1385SRemy Bohmer get_ether_addr(host_addr, dev->host_mac); 168723cd1385SRemy Bohmer 168823cd1385SRemy Bohmer sprintf (ethaddr, "%02X%02X%02X%02X%02X%02X", 168923cd1385SRemy Bohmer dev->host_mac [0], dev->host_mac [1], 169023cd1385SRemy Bohmer dev->host_mac [2], dev->host_mac [3], 169123cd1385SRemy Bohmer dev->host_mac [4], dev->host_mac [5]); 169223cd1385SRemy Bohmer 1693*7de73185SVitaly Kuzmichev printf("using %s, OUT %s IN %s%s%s\n", gadget->name, 169423cd1385SRemy Bohmer out_ep->name, in_ep->name, 169523cd1385SRemy Bohmer status_ep ? " STATUS " : "", 169623cd1385SRemy Bohmer status_ep ? status_ep->name : "" 169723cd1385SRemy Bohmer ); 1698*7de73185SVitaly Kuzmichev printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n", 169923cd1385SRemy Bohmer dev->net->enetaddr [0], dev->net->enetaddr [1], 170023cd1385SRemy Bohmer dev->net->enetaddr [2], dev->net->enetaddr [3], 170123cd1385SRemy Bohmer dev->net->enetaddr [4], dev->net->enetaddr [5]); 170223cd1385SRemy Bohmer 170323cd1385SRemy Bohmer if (cdc) { 1704*7de73185SVitaly Kuzmichev printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n", 170523cd1385SRemy Bohmer dev->host_mac [0], dev->host_mac [1], 170623cd1385SRemy Bohmer dev->host_mac [2], dev->host_mac [3], 170723cd1385SRemy Bohmer dev->host_mac [4], dev->host_mac [5]); 170823cd1385SRemy Bohmer } 170923cd1385SRemy Bohmer 171023cd1385SRemy Bohmer /* use PKTSIZE (or aligned... from u-boot) and set 171123cd1385SRemy Bohmer * wMaxSegmentSize accordingly*/ 171223cd1385SRemy Bohmer dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/ 171323cd1385SRemy Bohmer 171423cd1385SRemy Bohmer /* preallocate control message data and buffer */ 171523cd1385SRemy Bohmer dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL); 171623cd1385SRemy Bohmer if (!dev->req) 171723cd1385SRemy Bohmer goto fail; 171823cd1385SRemy Bohmer dev->req->buf = control_req; 171923cd1385SRemy Bohmer dev->req->complete = eth_setup_complete; 172023cd1385SRemy Bohmer 172123cd1385SRemy Bohmer /* ... and maybe likewise for status transfer */ 172223cd1385SRemy Bohmer #if defined(DEV_CONFIG_CDC) 172323cd1385SRemy Bohmer if (dev->status_ep) { 172423cd1385SRemy Bohmer dev->stat_req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL); 172523cd1385SRemy Bohmer dev->stat_req->buf = status_req; 172623cd1385SRemy Bohmer if (!dev->stat_req) { 172723cd1385SRemy Bohmer dev->stat_req->buf=NULL; 172823cd1385SRemy Bohmer usb_ep_free_request (gadget->ep0, dev->req); 172923cd1385SRemy Bohmer 173023cd1385SRemy Bohmer goto fail; 173123cd1385SRemy Bohmer } 173223cd1385SRemy Bohmer dev->stat_req->context = NULL; 173323cd1385SRemy Bohmer } 173423cd1385SRemy Bohmer #endif 173523cd1385SRemy Bohmer 173623cd1385SRemy Bohmer /* finish hookup to lower layer ... */ 173723cd1385SRemy Bohmer dev->gadget = gadget; 173823cd1385SRemy Bohmer set_gadget_data (gadget, dev); 173923cd1385SRemy Bohmer gadget->ep0->driver_data = dev; 174023cd1385SRemy Bohmer 174123cd1385SRemy Bohmer /* two kinds of host-initiated state changes: 174223cd1385SRemy Bohmer * - iff DATA transfer is active, carrier is "on" 174323cd1385SRemy Bohmer * - tx queueing enabled if open *and* carrier is "on" 174423cd1385SRemy Bohmer */ 174523cd1385SRemy Bohmer return 0; 174623cd1385SRemy Bohmer 174723cd1385SRemy Bohmer fail: 1748*7de73185SVitaly Kuzmichev error("%s failed", __func__); 174923cd1385SRemy Bohmer eth_unbind (gadget); 175023cd1385SRemy Bohmer return -ENOMEM; 175123cd1385SRemy Bohmer } 175223cd1385SRemy Bohmer 175323cd1385SRemy Bohmer static int usb_eth_init(struct eth_device* netdev, bd_t* bd) 175423cd1385SRemy Bohmer { 175523cd1385SRemy Bohmer struct eth_dev *dev=&l_ethdev; 175623cd1385SRemy Bohmer struct usb_gadget *gadget; 175723cd1385SRemy Bohmer unsigned long ts; 175823cd1385SRemy Bohmer unsigned long timeout = USB_CONNECT_TIMEOUT; 175923cd1385SRemy Bohmer 176023cd1385SRemy Bohmer if (!netdev) { 1761*7de73185SVitaly Kuzmichev error("received NULL ptr"); 176223cd1385SRemy Bohmer goto fail; 176323cd1385SRemy Bohmer } 176423cd1385SRemy Bohmer 176523cd1385SRemy Bohmer dev->network_started = 0; 176623cd1385SRemy Bohmer dev->tx_req = NULL; 176723cd1385SRemy Bohmer dev->rx_req = NULL; 176823cd1385SRemy Bohmer 176923cd1385SRemy Bohmer packet_received = 0; 177023cd1385SRemy Bohmer packet_sent = 0; 177123cd1385SRemy Bohmer 177223cd1385SRemy Bohmer gadget = dev->gadget; 177323cd1385SRemy Bohmer usb_gadget_connect(gadget); 177423cd1385SRemy Bohmer 177523cd1385SRemy Bohmer if (getenv("cdc_connect_timeout")) 177623cd1385SRemy Bohmer timeout = simple_strtoul(getenv("cdc_connect_timeout"), 177723cd1385SRemy Bohmer NULL, 10) * CONFIG_SYS_HZ; 177823cd1385SRemy Bohmer ts = get_timer(0); 177923cd1385SRemy Bohmer while (!l_ethdev.network_started) 178023cd1385SRemy Bohmer { 178123cd1385SRemy Bohmer /* Handle control-c and timeouts */ 178223cd1385SRemy Bohmer if (ctrlc() || (get_timer(ts) > timeout)) { 1783*7de73185SVitaly Kuzmichev error("The remote end did not respond in time."); 178423cd1385SRemy Bohmer goto fail; 178523cd1385SRemy Bohmer } 178623cd1385SRemy Bohmer usb_gadget_handle_interrupts(); 178723cd1385SRemy Bohmer } 178823cd1385SRemy Bohmer 178923cd1385SRemy Bohmer rx_submit (dev, dev->rx_req, 0); 179023cd1385SRemy Bohmer return 0; 179123cd1385SRemy Bohmer fail: 179223cd1385SRemy Bohmer return -1; 179323cd1385SRemy Bohmer } 179423cd1385SRemy Bohmer 179523cd1385SRemy Bohmer static int usb_eth_send(struct eth_device* netdev, volatile void* packet, int length) 179623cd1385SRemy Bohmer { 179723cd1385SRemy Bohmer int retval; 179823cd1385SRemy Bohmer struct usb_request *req = NULL; 179923cd1385SRemy Bohmer struct eth_dev *dev = &l_ethdev; 1800*7de73185SVitaly Kuzmichev 1801*7de73185SVitaly Kuzmichev debug("%s:...\n", __func__); 180223cd1385SRemy Bohmer 180323cd1385SRemy Bohmer req = dev->tx_req; 180423cd1385SRemy Bohmer 180523cd1385SRemy Bohmer req->buf = (void *)packet; 180623cd1385SRemy Bohmer req->context = NULL; 180723cd1385SRemy Bohmer req->complete = tx_complete; 180823cd1385SRemy Bohmer 180923cd1385SRemy Bohmer /* use zlp framing on tx for strict CDC-Ether conformance, 181023cd1385SRemy Bohmer * though any robust network rx path ignores extra padding. 181123cd1385SRemy Bohmer * and some hardware doesn't like to write zlps. 181223cd1385SRemy Bohmer */ 181323cd1385SRemy Bohmer req->zero = 1; 181423cd1385SRemy Bohmer if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0) 181523cd1385SRemy Bohmer length++; 181623cd1385SRemy Bohmer 181723cd1385SRemy Bohmer req->length = length; 181823cd1385SRemy Bohmer #if 0 181923cd1385SRemy Bohmer /* throttle highspeed IRQ rate back slightly */ 182023cd1385SRemy Bohmer if (gadget_is_dualspeed(dev->gadget)) 182123cd1385SRemy Bohmer req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH) 182223cd1385SRemy Bohmer ? ((dev->tx_qlen % qmult) != 0) : 0; 182323cd1385SRemy Bohmer #endif 182423cd1385SRemy Bohmer dev->tx_qlen=1; 182523cd1385SRemy Bohmer 182623cd1385SRemy Bohmer retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC); 182723cd1385SRemy Bohmer 182823cd1385SRemy Bohmer if (!retval) 1829*7de73185SVitaly Kuzmichev debug("%s: packet queued\n", __func__); 183023cd1385SRemy Bohmer while(!packet_sent) 183123cd1385SRemy Bohmer { 183223cd1385SRemy Bohmer packet_sent=0; 183323cd1385SRemy Bohmer } 183423cd1385SRemy Bohmer 183523cd1385SRemy Bohmer return 0; 183623cd1385SRemy Bohmer } 183723cd1385SRemy Bohmer 183823cd1385SRemy Bohmer static int usb_eth_recv(struct eth_device* netdev) 183923cd1385SRemy Bohmer { 184023cd1385SRemy Bohmer struct eth_dev *dev = &l_ethdev; 184123cd1385SRemy Bohmer 184223cd1385SRemy Bohmer usb_gadget_handle_interrupts(); 184323cd1385SRemy Bohmer 184423cd1385SRemy Bohmer if (packet_received) 184523cd1385SRemy Bohmer { 1846*7de73185SVitaly Kuzmichev debug("%s: packet received \n", __func__); 184723cd1385SRemy Bohmer if (dev->rx_req) 184823cd1385SRemy Bohmer { 184923cd1385SRemy Bohmer NetReceive(NetRxPackets[0],dev->rx_req->length); 185023cd1385SRemy Bohmer packet_received=0; 185123cd1385SRemy Bohmer 185223cd1385SRemy Bohmer if (dev->rx_req) 185323cd1385SRemy Bohmer rx_submit (dev, dev->rx_req, 0); 185423cd1385SRemy Bohmer } 1855*7de73185SVitaly Kuzmichev else error("dev->rx_req invalid"); 185623cd1385SRemy Bohmer } 185723cd1385SRemy Bohmer return 0; 185823cd1385SRemy Bohmer } 185923cd1385SRemy Bohmer 186023cd1385SRemy Bohmer void usb_eth_halt(struct eth_device* netdev) 186123cd1385SRemy Bohmer { 186223cd1385SRemy Bohmer struct eth_dev *dev =&l_ethdev; 186323cd1385SRemy Bohmer 186423cd1385SRemy Bohmer if (!netdev) 186523cd1385SRemy Bohmer { 1866*7de73185SVitaly Kuzmichev error("received NULL ptr"); 186723cd1385SRemy Bohmer return; 186823cd1385SRemy Bohmer } 186923cd1385SRemy Bohmer 187023cd1385SRemy Bohmer usb_gadget_disconnect(dev->gadget); 187123cd1385SRemy Bohmer } 187223cd1385SRemy Bohmer 187323cd1385SRemy Bohmer static struct usb_gadget_driver eth_driver = { 187423cd1385SRemy Bohmer .speed = DEVSPEED, 187523cd1385SRemy Bohmer 187623cd1385SRemy Bohmer .bind = eth_bind, 187723cd1385SRemy Bohmer .unbind = eth_unbind, 187823cd1385SRemy Bohmer 187923cd1385SRemy Bohmer .setup = eth_setup, 188023cd1385SRemy Bohmer .disconnect = eth_disconnect, 188123cd1385SRemy Bohmer 188223cd1385SRemy Bohmer .suspend = eth_suspend, 188323cd1385SRemy Bohmer .resume = eth_resume, 188423cd1385SRemy Bohmer }; 188523cd1385SRemy Bohmer 188623cd1385SRemy Bohmer int usb_eth_initialize(bd_t *bi) 188723cd1385SRemy Bohmer { 188823cd1385SRemy Bohmer int status = 0; 188923cd1385SRemy Bohmer struct eth_device *netdev=&l_netdev; 189023cd1385SRemy Bohmer 189123cd1385SRemy Bohmer sprintf(netdev->name,"usb_ether"); 189223cd1385SRemy Bohmer 189323cd1385SRemy Bohmer netdev->init = usb_eth_init; 189423cd1385SRemy Bohmer netdev->send = usb_eth_send; 189523cd1385SRemy Bohmer netdev->recv = usb_eth_recv; 189623cd1385SRemy Bohmer netdev->halt = usb_eth_halt; 189723cd1385SRemy Bohmer 189823cd1385SRemy Bohmer #ifdef CONFIG_MCAST_TFTP 189923cd1385SRemy Bohmer #error not supported 190023cd1385SRemy Bohmer #endif 190123cd1385SRemy Bohmer /* Configure default mac-addresses for the USB ethernet device */ 190223cd1385SRemy Bohmer #ifdef CONFIG_USBNET_DEV_ADDR 190323cd1385SRemy Bohmer strncpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr)); 190423cd1385SRemy Bohmer #endif 190523cd1385SRemy Bohmer #ifdef CONFIG_USBNET_HOST_ADDR 190623cd1385SRemy Bohmer strncpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr)); 190723cd1385SRemy Bohmer #endif 190823cd1385SRemy Bohmer /* Check if the user overruled the MAC addresses */ 190923cd1385SRemy Bohmer if (getenv("usbnet_devaddr")) 191023cd1385SRemy Bohmer strncpy(dev_addr, getenv("usbnet_devaddr"), 191123cd1385SRemy Bohmer sizeof(dev_addr)); 191223cd1385SRemy Bohmer 191323cd1385SRemy Bohmer if (getenv("usbnet_hostaddr")) 191423cd1385SRemy Bohmer strncpy(host_addr, getenv("usbnet_hostaddr"), 191523cd1385SRemy Bohmer sizeof(host_addr)); 191623cd1385SRemy Bohmer 191723cd1385SRemy Bohmer /* Make sure both strings are terminated */ 191823cd1385SRemy Bohmer dev_addr[sizeof(dev_addr)-1] = '\0'; 191923cd1385SRemy Bohmer host_addr[sizeof(host_addr)-1] = '\0'; 192023cd1385SRemy Bohmer 192123cd1385SRemy Bohmer if (!is_eth_addr_valid(dev_addr)) { 1922*7de73185SVitaly Kuzmichev error("Need valid 'usbnet_devaddr' to be set"); 192323cd1385SRemy Bohmer status = -1; 192423cd1385SRemy Bohmer } 192523cd1385SRemy Bohmer if (!is_eth_addr_valid(host_addr)) { 1926*7de73185SVitaly Kuzmichev error("Need valid 'usbnet_hostaddr' to be set"); 192723cd1385SRemy Bohmer status = -1; 192823cd1385SRemy Bohmer } 192923cd1385SRemy Bohmer if (status) 193023cd1385SRemy Bohmer goto fail; 193123cd1385SRemy Bohmer 193223cd1385SRemy Bohmer status = usb_gadget_register_driver(ð_driver); 193323cd1385SRemy Bohmer if (status < 0) 193423cd1385SRemy Bohmer goto fail; 193523cd1385SRemy Bohmer 193623cd1385SRemy Bohmer eth_register(netdev); 193723cd1385SRemy Bohmer return 0; 193823cd1385SRemy Bohmer 193923cd1385SRemy Bohmer fail: 1940*7de73185SVitaly Kuzmichev error("%s failed. error = %d", __func__, status); 194123cd1385SRemy Bohmer return status; 194223cd1385SRemy Bohmer } 194323cd1385SRemy Bohmer 1944