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> 25c85d70efSVitaly Kuzmichev #include <linux/netdevice.h> 2623cd1385SRemy Bohmer #include <linux/usb/ch9.h> 2723cd1385SRemy Bohmer #include <linux/usb/cdc.h> 2823cd1385SRemy Bohmer #include <linux/usb/gadget.h> 2923cd1385SRemy Bohmer #include <net.h> 307612a43dSVitaly Kuzmichev #include <malloc.h> 3123cd1385SRemy Bohmer #include <linux/ctype.h> 3223cd1385SRemy Bohmer 3323cd1385SRemy Bohmer #include "gadget_chips.h" 347612a43dSVitaly Kuzmichev #include "rndis.h" 3523cd1385SRemy Bohmer 368f7aa831SVitaly Kuzmichev #define USB_NET_NAME "usb_ether" 377de73185SVitaly Kuzmichev 3823cd1385SRemy Bohmer #define atomic_read 3923cd1385SRemy Bohmer extern struct platform_data brd; 4023cd1385SRemy Bohmer #define spin_lock(x) 4123cd1385SRemy Bohmer #define spin_unlock(x) 4223cd1385SRemy Bohmer 4323cd1385SRemy Bohmer 4423cd1385SRemy Bohmer unsigned packet_received, packet_sent; 4523cd1385SRemy Bohmer 4623cd1385SRemy Bohmer #define DEV_CONFIG_CDC 1 4723cd1385SRemy Bohmer #define GFP_ATOMIC ((gfp_t) 0) 4823cd1385SRemy Bohmer #define GFP_KERNEL ((gfp_t) 0) 4923cd1385SRemy Bohmer 5023cd1385SRemy Bohmer /* 5123cd1385SRemy Bohmer * Ethernet gadget driver -- with CDC and non-CDC options 5223cd1385SRemy Bohmer * Builds on hardware support for a full duplex link. 5323cd1385SRemy Bohmer * 5423cd1385SRemy Bohmer * CDC Ethernet is the standard USB solution for sending Ethernet frames 5523cd1385SRemy Bohmer * using USB. Real hardware tends to use the same framing protocol but look 5623cd1385SRemy Bohmer * different for control features. This driver strongly prefers to use 5723cd1385SRemy Bohmer * this USB-IF standard as its open-systems interoperability solution; 5823cd1385SRemy Bohmer * most host side USB stacks (except from Microsoft) support it. 5923cd1385SRemy Bohmer * 6023cd1385SRemy Bohmer * This is sometimes called "CDC ECM" (Ethernet Control Model) to support 6123cd1385SRemy Bohmer * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new 6223cd1385SRemy Bohmer * "CDC EEM" (Ethernet Emulation Model) is starting to spread. 6323cd1385SRemy Bohmer * 6423cd1385SRemy Bohmer * There's some hardware that can't talk CDC ECM. We make that hardware 6523cd1385SRemy Bohmer * implement a "minimalist" vendor-agnostic CDC core: same framing, but 6623cd1385SRemy Bohmer * link-level setup only requires activating the configuration. Only the 6723cd1385SRemy Bohmer * endpoint descriptors, and product/vendor IDs, are relevant; no control 6823cd1385SRemy Bohmer * operations are available. Linux supports it, but other host operating 6923cd1385SRemy Bohmer * systems may not. (This is a subset of CDC Ethernet.) 7023cd1385SRemy Bohmer * 7123cd1385SRemy Bohmer * It turns out that if you add a few descriptors to that "CDC Subset", 7223cd1385SRemy Bohmer * (Windows) host side drivers from MCCI can treat it as one submode of 7323cd1385SRemy Bohmer * a proprietary scheme called "SAFE" ... without needing to know about 7423cd1385SRemy Bohmer * specific product/vendor IDs. So we do that, making it easier to use 7523cd1385SRemy Bohmer * those MS-Windows drivers. Those added descriptors make it resemble a 7623cd1385SRemy Bohmer * CDC MDLM device, but they don't change device behavior at all. (See 7723cd1385SRemy Bohmer * MCCI Engineering report 950198 "SAFE Networking Functions".) 7823cd1385SRemy Bohmer * 7923cd1385SRemy Bohmer * A third option is also in use. Rather than CDC Ethernet, or something 8023cd1385SRemy Bohmer * simpler, Microsoft pushes their own approach: RNDIS. The published 8123cd1385SRemy Bohmer * RNDIS specs are ambiguous and appear to be incomplete, and are also 8223cd1385SRemy Bohmer * needlessly complex. They borrow more from CDC ACM than CDC ECM. 8323cd1385SRemy Bohmer */ 8423cd1385SRemy Bohmer #define ETH_ALEN 6 /* Octets in one ethernet addr */ 8523cd1385SRemy Bohmer #define ETH_HLEN 14 /* Total octets in header. */ 8623cd1385SRemy Bohmer #define ETH_ZLEN 60 /* Min. octets in frame sans FCS */ 8723cd1385SRemy Bohmer #define ETH_DATA_LEN 1500 /* Max. octets in payload */ 8823cd1385SRemy Bohmer #define ETH_FRAME_LEN PKTSIZE_ALIGN /* Max. octets in frame sans FCS */ 8923cd1385SRemy Bohmer #define ETH_FCS_LEN 4 /* Octets in the FCS */ 9023cd1385SRemy Bohmer 9123cd1385SRemy Bohmer #define DRIVER_DESC "Ethernet Gadget" 9223cd1385SRemy Bohmer /* Based on linux 2.6.27 version */ 9323cd1385SRemy Bohmer #define DRIVER_VERSION "May Day 2005" 9423cd1385SRemy Bohmer 9523cd1385SRemy Bohmer static const char shortname[] = "ether"; 9623cd1385SRemy Bohmer static const char driver_desc[] = DRIVER_DESC; 9723cd1385SRemy Bohmer 9823cd1385SRemy Bohmer #define RX_EXTRA 20 /* guard against rx overflows */ 9923cd1385SRemy Bohmer 1007612a43dSVitaly Kuzmichev #ifndef CONFIG_USB_ETH_RNDIS 1017612a43dSVitaly Kuzmichev #define rndis_uninit(x) do {} while (0) 1027612a43dSVitaly Kuzmichev #define rndis_deregister(c) do {} while (0) 1037612a43dSVitaly Kuzmichev #define rndis_exit() do {} while (0) 1047612a43dSVitaly Kuzmichev #endif 1057612a43dSVitaly Kuzmichev 1067612a43dSVitaly Kuzmichev /* CDC and RNDIS support the same host-chosen outgoing packet filters. */ 10723cd1385SRemy Bohmer #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \ 10823cd1385SRemy Bohmer |USB_CDC_PACKET_TYPE_ALL_MULTICAST \ 10923cd1385SRemy Bohmer |USB_CDC_PACKET_TYPE_PROMISCUOUS \ 11023cd1385SRemy Bohmer |USB_CDC_PACKET_TYPE_DIRECTED) 11123cd1385SRemy Bohmer 11223cd1385SRemy Bohmer #define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ) 11323cd1385SRemy Bohmer 11423cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 1158b6b66b4SVitaly Kuzmichev 1168b6b66b4SVitaly Kuzmichev struct eth_dev { 1178b6b66b4SVitaly Kuzmichev struct usb_gadget *gadget; 1188b6b66b4SVitaly Kuzmichev struct usb_request *req; /* for control responses */ 1197612a43dSVitaly Kuzmichev struct usb_request *stat_req; /* for cdc & rndis status */ 1208b6b66b4SVitaly Kuzmichev 1218b6b66b4SVitaly Kuzmichev u8 config; 1228b6b66b4SVitaly Kuzmichev struct usb_ep *in_ep, *out_ep, *status_ep; 1238b6b66b4SVitaly Kuzmichev const struct usb_endpoint_descriptor 1248b6b66b4SVitaly Kuzmichev *in, *out, *status; 1258b6b66b4SVitaly Kuzmichev 1268b6b66b4SVitaly Kuzmichev struct usb_request *tx_req, *rx_req; 1278b6b66b4SVitaly Kuzmichev 1288b6b66b4SVitaly Kuzmichev struct eth_device *net; 1298b6b66b4SVitaly Kuzmichev struct net_device_stats stats; 1308b6b66b4SVitaly Kuzmichev unsigned int tx_qlen; 1318b6b66b4SVitaly Kuzmichev 1328b6b66b4SVitaly Kuzmichev unsigned zlp:1; 1338b6b66b4SVitaly Kuzmichev unsigned cdc:1; 1347612a43dSVitaly Kuzmichev unsigned rndis:1; 1358b6b66b4SVitaly Kuzmichev unsigned suspended:1; 1368b6b66b4SVitaly Kuzmichev unsigned network_started:1; 1378b6b66b4SVitaly Kuzmichev u16 cdc_filter; 1388b6b66b4SVitaly Kuzmichev unsigned long todo; 1398b6b66b4SVitaly Kuzmichev int mtu; 1408b6b66b4SVitaly Kuzmichev #define WORK_RX_MEMORY 0 1417612a43dSVitaly Kuzmichev int rndis_config; 1428b6b66b4SVitaly Kuzmichev u8 host_mac[ETH_ALEN]; 1438b6b66b4SVitaly Kuzmichev }; 1448b6b66b4SVitaly Kuzmichev 1458b6b66b4SVitaly Kuzmichev /* 1468b6b66b4SVitaly Kuzmichev * This version autoconfigures as much as possible at run-time. 1478b6b66b4SVitaly Kuzmichev * 1488b6b66b4SVitaly Kuzmichev * It also ASSUMES a self-powered device, without remote wakeup, 1498b6b66b4SVitaly Kuzmichev * although remote wakeup support would make sense. 1508b6b66b4SVitaly Kuzmichev */ 1518b6b66b4SVitaly Kuzmichev 1528b6b66b4SVitaly Kuzmichev /*-------------------------------------------------------------------------*/ 15323cd1385SRemy Bohmer static struct eth_dev l_ethdev; 15423cd1385SRemy Bohmer static struct eth_device l_netdev; 15523cd1385SRemy Bohmer static struct usb_gadget_driver eth_driver; 15623cd1385SRemy Bohmer 15723cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 15823cd1385SRemy Bohmer 15923cd1385SRemy Bohmer /* "main" config is either CDC, or its simple subset */ 16023cd1385SRemy Bohmer static inline int is_cdc(struct eth_dev *dev) 16123cd1385SRemy Bohmer { 16223cd1385SRemy Bohmer #if !defined(DEV_CONFIG_SUBSET) 16323cd1385SRemy Bohmer return 1; /* only cdc possible */ 16423cd1385SRemy Bohmer #elif !defined(DEV_CONFIG_CDC) 16523cd1385SRemy Bohmer return 0; /* only subset possible */ 16623cd1385SRemy Bohmer #else 16723cd1385SRemy Bohmer return dev->cdc; /* depends on what hardware we found */ 16823cd1385SRemy Bohmer #endif 16923cd1385SRemy Bohmer } 17023cd1385SRemy Bohmer 1717612a43dSVitaly Kuzmichev /* "secondary" RNDIS config may sometimes be activated */ 1727612a43dSVitaly Kuzmichev static inline int rndis_active(struct eth_dev *dev) 1737612a43dSVitaly Kuzmichev { 1747612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS 1757612a43dSVitaly Kuzmichev return dev->rndis; 1767612a43dSVitaly Kuzmichev #else 1777612a43dSVitaly Kuzmichev return 0; 1787612a43dSVitaly Kuzmichev #endif 1797612a43dSVitaly Kuzmichev } 1807612a43dSVitaly Kuzmichev 1817612a43dSVitaly Kuzmichev #define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev)) 1827612a43dSVitaly Kuzmichev #define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev)) 18323cd1385SRemy Bohmer 18423cd1385SRemy Bohmer #define DEFAULT_QLEN 2 /* double buffering by default */ 18523cd1385SRemy Bohmer 18623cd1385SRemy Bohmer /* peak bulk transfer bits-per-second */ 18723cd1385SRemy Bohmer #define HS_BPS (13 * 512 * 8 * 1000 * 8) 18823cd1385SRemy Bohmer #define FS_BPS (19 * 64 * 1 * 1000 * 8) 18923cd1385SRemy Bohmer 19023cd1385SRemy Bohmer #ifdef CONFIG_USB_GADGET_DUALSPEED 19123cd1385SRemy Bohmer #define DEVSPEED USB_SPEED_HIGH 19223cd1385SRemy Bohmer 1932721dbf1SVitaly Kuzmichev #ifdef CONFIG_USB_ETH_QMULT 1942721dbf1SVitaly Kuzmichev #define qmult CONFIG_USB_ETH_QMULT 1952721dbf1SVitaly Kuzmichev #else 1962721dbf1SVitaly Kuzmichev #define qmult 5 1972721dbf1SVitaly Kuzmichev #endif 1982721dbf1SVitaly Kuzmichev 19923cd1385SRemy Bohmer /* for dual-speed hardware, use deeper queues at highspeed */ 20023cd1385SRemy Bohmer #define qlen(gadget) \ 20123cd1385SRemy Bohmer (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1)) 20223cd1385SRemy Bohmer 20323cd1385SRemy Bohmer static inline int BITRATE(struct usb_gadget *g) 20423cd1385SRemy Bohmer { 20523cd1385SRemy Bohmer return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS; 20623cd1385SRemy Bohmer } 20723cd1385SRemy Bohmer 20823cd1385SRemy Bohmer #else /* full speed (low speed doesn't do bulk) */ 20923cd1385SRemy Bohmer 21023cd1385SRemy Bohmer #define qmult 1 21123cd1385SRemy Bohmer 21223cd1385SRemy Bohmer #define DEVSPEED USB_SPEED_FULL 21323cd1385SRemy Bohmer 21423cd1385SRemy Bohmer #define qlen(gadget) DEFAULT_QLEN 21523cd1385SRemy Bohmer 21623cd1385SRemy Bohmer static inline int BITRATE(struct usb_gadget *g) 21723cd1385SRemy Bohmer { 21823cd1385SRemy Bohmer return FS_BPS; 21923cd1385SRemy Bohmer } 22023cd1385SRemy Bohmer #endif 22123cd1385SRemy Bohmer 22223cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 22323cd1385SRemy Bohmer 2246142e0aeSVitaly Kuzmichev /* 2256142e0aeSVitaly Kuzmichev * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! 22623cd1385SRemy Bohmer * Instead: allocate your own, using normal USB-IF procedures. 22723cd1385SRemy Bohmer */ 22823cd1385SRemy Bohmer 2296142e0aeSVitaly Kuzmichev /* 2306142e0aeSVitaly Kuzmichev * Thanks to NetChip Technologies for donating this product ID. 23123cd1385SRemy Bohmer * It's for devices with only CDC Ethernet configurations. 23223cd1385SRemy Bohmer */ 23323cd1385SRemy Bohmer #define CDC_VENDOR_NUM 0x0525 /* NetChip */ 23423cd1385SRemy Bohmer #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */ 23523cd1385SRemy Bohmer 2366142e0aeSVitaly Kuzmichev /* 2376142e0aeSVitaly Kuzmichev * For hardware that can't talk CDC, we use the same vendor ID that 23823cd1385SRemy Bohmer * ARM Linux has used for ethernet-over-usb, both with sa1100 and 23923cd1385SRemy Bohmer * with pxa250. We're protocol-compatible, if the host-side drivers 24023cd1385SRemy Bohmer * use the endpoint descriptors. bcdDevice (version) is nonzero, so 24123cd1385SRemy Bohmer * drivers that need to hard-wire endpoint numbers have a hook. 24223cd1385SRemy Bohmer * 24323cd1385SRemy Bohmer * The protocol is a minimal subset of CDC Ether, which works on any bulk 24423cd1385SRemy Bohmer * hardware that's not deeply broken ... even on hardware that can't talk 24523cd1385SRemy Bohmer * RNDIS (like SA-1100, with no interrupt endpoint, or anything that 24623cd1385SRemy Bohmer * doesn't handle control-OUT). 24723cd1385SRemy Bohmer */ 2487612a43dSVitaly Kuzmichev #define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */ 2497612a43dSVitaly Kuzmichev #define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */ 2507612a43dSVitaly Kuzmichev 2517612a43dSVitaly Kuzmichev /* 2527612a43dSVitaly Kuzmichev * For hardware that can talk RNDIS and either of the above protocols, 2537612a43dSVitaly Kuzmichev * use this ID ... the windows INF files will know it. Unless it's 2547612a43dSVitaly Kuzmichev * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose 2557612a43dSVitaly Kuzmichev * the non-RNDIS configuration. 2567612a43dSVitaly Kuzmichev */ 2577612a43dSVitaly Kuzmichev #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */ 2587612a43dSVitaly Kuzmichev #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */ 25923cd1385SRemy Bohmer 2606142e0aeSVitaly Kuzmichev /* 2616142e0aeSVitaly Kuzmichev * Some systems will want different product identifers published in the 26223cd1385SRemy Bohmer * device descriptor, either numbers or strings or both. These string 26323cd1385SRemy Bohmer * parameters are in UTF-8 (superset of ASCII's 7 bit characters). 26423cd1385SRemy Bohmer */ 26523cd1385SRemy Bohmer 2667612a43dSVitaly Kuzmichev /* 2677612a43dSVitaly Kuzmichev * Emulating them in eth_bind: 2687612a43dSVitaly Kuzmichev * static ushort idVendor; 2697612a43dSVitaly Kuzmichev * static ushort idProduct; 2707612a43dSVitaly Kuzmichev */ 2717612a43dSVitaly Kuzmichev 27223cd1385SRemy Bohmer #if defined(CONFIG_USBNET_MANUFACTURER) 27323cd1385SRemy Bohmer static char *iManufacturer = CONFIG_USBNET_MANUFACTURER; 27423cd1385SRemy Bohmer #else 27523cd1385SRemy Bohmer static char *iManufacturer = "U-boot"; 27623cd1385SRemy Bohmer #endif 2777612a43dSVitaly Kuzmichev 2787612a43dSVitaly Kuzmichev /* These probably need to be configurable. */ 2797612a43dSVitaly Kuzmichev static ushort bcdDevice; 28023cd1385SRemy Bohmer static char *iProduct; 28123cd1385SRemy Bohmer static char *iSerialNumber; 2827612a43dSVitaly Kuzmichev 28323cd1385SRemy Bohmer static char dev_addr[18]; 2847612a43dSVitaly Kuzmichev 28523cd1385SRemy Bohmer static char host_addr[18]; 28623cd1385SRemy Bohmer 2877612a43dSVitaly Kuzmichev 28823cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 28923cd1385SRemy Bohmer 2906142e0aeSVitaly Kuzmichev /* 2916142e0aeSVitaly Kuzmichev * USB DRIVER HOOKUP (to the hardware driver, below us), mostly 29223cd1385SRemy Bohmer * ep0 implementation: descriptors, config management, setup(). 29323cd1385SRemy Bohmer * also optional class-specific notification interrupt transfer. 29423cd1385SRemy Bohmer */ 29523cd1385SRemy Bohmer 29623cd1385SRemy Bohmer /* 29723cd1385SRemy Bohmer * DESCRIPTORS ... most are static, but strings and (full) configuration 29823cd1385SRemy Bohmer * descriptors are built on demand. For now we do either full CDC, or 2997612a43dSVitaly Kuzmichev * our simple subset, with RNDIS as an optional second configuration. 3007612a43dSVitaly Kuzmichev * 3017612a43dSVitaly Kuzmichev * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But 3027612a43dSVitaly Kuzmichev * the class descriptors match a modem (they're ignored; it's really just 3037612a43dSVitaly Kuzmichev * Ethernet functionality), they don't need the NOP altsetting, and the 3047612a43dSVitaly Kuzmichev * status transfer endpoint isn't optional. 30523cd1385SRemy Bohmer */ 30623cd1385SRemy Bohmer 30723cd1385SRemy Bohmer #define STRING_MANUFACTURER 1 30823cd1385SRemy Bohmer #define STRING_PRODUCT 2 30923cd1385SRemy Bohmer #define STRING_ETHADDR 3 31023cd1385SRemy Bohmer #define STRING_DATA 4 31123cd1385SRemy Bohmer #define STRING_CONTROL 5 3127612a43dSVitaly Kuzmichev #define STRING_RNDIS_CONTROL 6 31323cd1385SRemy Bohmer #define STRING_CDC 7 31423cd1385SRemy Bohmer #define STRING_SUBSET 8 3157612a43dSVitaly Kuzmichev #define STRING_RNDIS 9 31623cd1385SRemy Bohmer #define STRING_SERIALNUMBER 10 31723cd1385SRemy Bohmer 3187612a43dSVitaly Kuzmichev /* holds our biggest descriptor (or RNDIS response) */ 31923cd1385SRemy Bohmer #define USB_BUFSIZ 256 32023cd1385SRemy Bohmer 32123cd1385SRemy Bohmer /* 3227612a43dSVitaly Kuzmichev * This device advertises one configuration, eth_config, unless RNDIS 3237612a43dSVitaly Kuzmichev * is enabled (rndis_config) on hardware supporting at least two configs. 3247612a43dSVitaly Kuzmichev * 3257612a43dSVitaly Kuzmichev * NOTE: Controllers like superh_udc should probably be able to use 3267612a43dSVitaly Kuzmichev * an RNDIS-only configuration. 32723cd1385SRemy Bohmer * 32823cd1385SRemy Bohmer * FIXME define some higher-powered configurations to make it easier 32923cd1385SRemy Bohmer * to recharge batteries ... 33023cd1385SRemy Bohmer */ 33123cd1385SRemy Bohmer 33223cd1385SRemy Bohmer #define DEV_CONFIG_VALUE 1 /* cdc or subset */ 3337612a43dSVitaly Kuzmichev #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */ 33423cd1385SRemy Bohmer 33523cd1385SRemy Bohmer static struct usb_device_descriptor 33623cd1385SRemy Bohmer device_desc = { 33723cd1385SRemy Bohmer .bLength = sizeof device_desc, 33823cd1385SRemy Bohmer .bDescriptorType = USB_DT_DEVICE, 33923cd1385SRemy Bohmer 34023cd1385SRemy Bohmer .bcdUSB = __constant_cpu_to_le16(0x0200), 34123cd1385SRemy Bohmer 34223cd1385SRemy Bohmer .bDeviceClass = USB_CLASS_COMM, 34323cd1385SRemy Bohmer .bDeviceSubClass = 0, 34423cd1385SRemy Bohmer .bDeviceProtocol = 0, 34523cd1385SRemy Bohmer 34623cd1385SRemy Bohmer .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM), 34723cd1385SRemy Bohmer .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM), 34823cd1385SRemy Bohmer .iManufacturer = STRING_MANUFACTURER, 34923cd1385SRemy Bohmer .iProduct = STRING_PRODUCT, 35023cd1385SRemy Bohmer .bNumConfigurations = 1, 35123cd1385SRemy Bohmer }; 35223cd1385SRemy Bohmer 35323cd1385SRemy Bohmer static struct usb_otg_descriptor 35423cd1385SRemy Bohmer otg_descriptor = { 35523cd1385SRemy Bohmer .bLength = sizeof otg_descriptor, 35623cd1385SRemy Bohmer .bDescriptorType = USB_DT_OTG, 35723cd1385SRemy Bohmer 35823cd1385SRemy Bohmer .bmAttributes = USB_OTG_SRP, 35923cd1385SRemy Bohmer }; 36023cd1385SRemy Bohmer 36123cd1385SRemy Bohmer static struct usb_config_descriptor 36223cd1385SRemy Bohmer eth_config = { 36323cd1385SRemy Bohmer .bLength = sizeof eth_config, 36423cd1385SRemy Bohmer .bDescriptorType = USB_DT_CONFIG, 36523cd1385SRemy Bohmer 36623cd1385SRemy Bohmer /* compute wTotalLength on the fly */ 36723cd1385SRemy Bohmer .bNumInterfaces = 2, 36823cd1385SRemy Bohmer .bConfigurationValue = DEV_CONFIG_VALUE, 36923cd1385SRemy Bohmer .iConfiguration = STRING_CDC, 37023cd1385SRemy Bohmer .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, 37123cd1385SRemy Bohmer .bMaxPower = 1, 37223cd1385SRemy Bohmer }; 37323cd1385SRemy Bohmer 3747612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS 3757612a43dSVitaly Kuzmichev static struct usb_config_descriptor 3767612a43dSVitaly Kuzmichev rndis_config = { 3777612a43dSVitaly Kuzmichev .bLength = sizeof rndis_config, 3787612a43dSVitaly Kuzmichev .bDescriptorType = USB_DT_CONFIG, 3797612a43dSVitaly Kuzmichev 3807612a43dSVitaly Kuzmichev /* compute wTotalLength on the fly */ 3817612a43dSVitaly Kuzmichev .bNumInterfaces = 2, 3827612a43dSVitaly Kuzmichev .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE, 3837612a43dSVitaly Kuzmichev .iConfiguration = STRING_RNDIS, 3847612a43dSVitaly Kuzmichev .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, 3857612a43dSVitaly Kuzmichev .bMaxPower = 1, 3867612a43dSVitaly Kuzmichev }; 3877612a43dSVitaly Kuzmichev #endif 3887612a43dSVitaly Kuzmichev 38923cd1385SRemy Bohmer /* 39023cd1385SRemy Bohmer * Compared to the simple CDC subset, the full CDC Ethernet model adds 39123cd1385SRemy Bohmer * three class descriptors, two interface descriptors, optional status 39223cd1385SRemy Bohmer * endpoint. Both have a "data" interface and two bulk endpoints. 39323cd1385SRemy Bohmer * There are also differences in how control requests are handled. 3947612a43dSVitaly Kuzmichev * 3957612a43dSVitaly Kuzmichev * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the 3967612a43dSVitaly Kuzmichev * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it 3977612a43dSVitaly Kuzmichev * may hang or oops. Since bugfixes (or accurate specs, letting Linux 3987612a43dSVitaly Kuzmichev * work around those bugs) are unlikely to ever come from MSFT, you may 3997612a43dSVitaly Kuzmichev * wish to avoid using RNDIS. 4007612a43dSVitaly Kuzmichev * 4017612a43dSVitaly Kuzmichev * MCCI offers an alternative to RNDIS if you need to connect to Windows 4027612a43dSVitaly Kuzmichev * but have hardware that can't support CDC Ethernet. We add descriptors 4037612a43dSVitaly Kuzmichev * to present the CDC Subset as a (nonconformant) CDC MDLM variant called 4047612a43dSVitaly Kuzmichev * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can 4057612a43dSVitaly Kuzmichev * get those drivers from MCCI, or bundled with various products. 40623cd1385SRemy Bohmer */ 40723cd1385SRemy Bohmer 40823cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC 40923cd1385SRemy Bohmer static struct usb_interface_descriptor 41023cd1385SRemy Bohmer control_intf = { 41123cd1385SRemy Bohmer .bLength = sizeof control_intf, 41223cd1385SRemy Bohmer .bDescriptorType = USB_DT_INTERFACE, 41323cd1385SRemy Bohmer 41423cd1385SRemy Bohmer .bInterfaceNumber = 0, 41523cd1385SRemy Bohmer /* status endpoint is optional; this may be patched later */ 41623cd1385SRemy Bohmer .bNumEndpoints = 1, 41723cd1385SRemy Bohmer .bInterfaceClass = USB_CLASS_COMM, 41823cd1385SRemy Bohmer .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, 41923cd1385SRemy Bohmer .bInterfaceProtocol = USB_CDC_PROTO_NONE, 42023cd1385SRemy Bohmer .iInterface = STRING_CONTROL, 42123cd1385SRemy Bohmer }; 42223cd1385SRemy Bohmer #endif 42323cd1385SRemy Bohmer 4247612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS 4257612a43dSVitaly Kuzmichev static const struct usb_interface_descriptor 4267612a43dSVitaly Kuzmichev rndis_control_intf = { 4277612a43dSVitaly Kuzmichev .bLength = sizeof rndis_control_intf, 4287612a43dSVitaly Kuzmichev .bDescriptorType = USB_DT_INTERFACE, 4297612a43dSVitaly Kuzmichev 4307612a43dSVitaly Kuzmichev .bInterfaceNumber = 0, 4317612a43dSVitaly Kuzmichev .bNumEndpoints = 1, 4327612a43dSVitaly Kuzmichev .bInterfaceClass = USB_CLASS_COMM, 4337612a43dSVitaly Kuzmichev .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM, 4347612a43dSVitaly Kuzmichev .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR, 4357612a43dSVitaly Kuzmichev .iInterface = STRING_RNDIS_CONTROL, 4367612a43dSVitaly Kuzmichev }; 4377612a43dSVitaly Kuzmichev #endif 4387612a43dSVitaly Kuzmichev 43923cd1385SRemy Bohmer static const struct usb_cdc_header_desc header_desc = { 44023cd1385SRemy Bohmer .bLength = sizeof header_desc, 44123cd1385SRemy Bohmer .bDescriptorType = USB_DT_CS_INTERFACE, 44223cd1385SRemy Bohmer .bDescriptorSubType = USB_CDC_HEADER_TYPE, 44323cd1385SRemy Bohmer 44423cd1385SRemy Bohmer .bcdCDC = __constant_cpu_to_le16(0x0110), 44523cd1385SRemy Bohmer }; 44623cd1385SRemy Bohmer 4477612a43dSVitaly Kuzmichev #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS) 44823cd1385SRemy Bohmer 44923cd1385SRemy Bohmer static const struct usb_cdc_union_desc union_desc = { 45023cd1385SRemy Bohmer .bLength = sizeof union_desc, 45123cd1385SRemy Bohmer .bDescriptorType = USB_DT_CS_INTERFACE, 45223cd1385SRemy Bohmer .bDescriptorSubType = USB_CDC_UNION_TYPE, 45323cd1385SRemy Bohmer 45423cd1385SRemy Bohmer .bMasterInterface0 = 0, /* index of control interface */ 45523cd1385SRemy Bohmer .bSlaveInterface0 = 1, /* index of DATA interface */ 45623cd1385SRemy Bohmer }; 45723cd1385SRemy Bohmer 4587612a43dSVitaly Kuzmichev #endif /* CDC || RNDIS */ 4597612a43dSVitaly Kuzmichev 4607612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS 4617612a43dSVitaly Kuzmichev 4627612a43dSVitaly Kuzmichev static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = { 4637612a43dSVitaly Kuzmichev .bLength = sizeof call_mgmt_descriptor, 4647612a43dSVitaly Kuzmichev .bDescriptorType = USB_DT_CS_INTERFACE, 4657612a43dSVitaly Kuzmichev .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE, 4667612a43dSVitaly Kuzmichev 4677612a43dSVitaly Kuzmichev .bmCapabilities = 0x00, 4687612a43dSVitaly Kuzmichev .bDataInterface = 0x01, 4697612a43dSVitaly Kuzmichev }; 4707612a43dSVitaly Kuzmichev 4717612a43dSVitaly Kuzmichev static const struct usb_cdc_acm_descriptor acm_descriptor = { 4727612a43dSVitaly Kuzmichev .bLength = sizeof acm_descriptor, 4737612a43dSVitaly Kuzmichev .bDescriptorType = USB_DT_CS_INTERFACE, 4747612a43dSVitaly Kuzmichev .bDescriptorSubType = USB_CDC_ACM_TYPE, 4757612a43dSVitaly Kuzmichev 4767612a43dSVitaly Kuzmichev .bmCapabilities = 0x00, 4777612a43dSVitaly Kuzmichev }; 4787612a43dSVitaly Kuzmichev 4797612a43dSVitaly Kuzmichev #endif 48023cd1385SRemy Bohmer 48123cd1385SRemy Bohmer #ifndef DEV_CONFIG_CDC 48223cd1385SRemy Bohmer 4836142e0aeSVitaly Kuzmichev /* 4846142e0aeSVitaly Kuzmichev * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various 48523cd1385SRemy Bohmer * ways: data endpoints live in the control interface, there's no data 48623cd1385SRemy Bohmer * interface, and it's not used to talk to a cell phone radio. 48723cd1385SRemy Bohmer */ 48823cd1385SRemy Bohmer 48923cd1385SRemy Bohmer static const struct usb_cdc_mdlm_desc mdlm_desc = { 49023cd1385SRemy Bohmer .bLength = sizeof mdlm_desc, 49123cd1385SRemy Bohmer .bDescriptorType = USB_DT_CS_INTERFACE, 49223cd1385SRemy Bohmer .bDescriptorSubType = USB_CDC_MDLM_TYPE, 49323cd1385SRemy Bohmer 49423cd1385SRemy Bohmer .bcdVersion = __constant_cpu_to_le16(0x0100), 49523cd1385SRemy Bohmer .bGUID = { 49623cd1385SRemy Bohmer 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6, 49723cd1385SRemy Bohmer 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f, 49823cd1385SRemy Bohmer }, 49923cd1385SRemy Bohmer }; 50023cd1385SRemy Bohmer 5016142e0aeSVitaly Kuzmichev /* 5026142e0aeSVitaly Kuzmichev * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we 50323cd1385SRemy Bohmer * can't really use its struct. All we do here is say that we're using 50423cd1385SRemy Bohmer * the submode of "SAFE" which directly matches the CDC Subset. 50523cd1385SRemy Bohmer */ 50623cd1385SRemy Bohmer static const u8 mdlm_detail_desc[] = { 50723cd1385SRemy Bohmer 6, 50823cd1385SRemy Bohmer USB_DT_CS_INTERFACE, 50923cd1385SRemy Bohmer USB_CDC_MDLM_DETAIL_TYPE, 51023cd1385SRemy Bohmer 51123cd1385SRemy Bohmer 0, /* "SAFE" */ 51223cd1385SRemy Bohmer 0, /* network control capabilities (none) */ 51323cd1385SRemy Bohmer 0, /* network data capabilities ("raw" encapsulation) */ 51423cd1385SRemy Bohmer }; 51523cd1385SRemy Bohmer 51623cd1385SRemy Bohmer #endif 51723cd1385SRemy Bohmer 51823cd1385SRemy Bohmer static const struct usb_cdc_ether_desc ether_desc = { 51923cd1385SRemy Bohmer .bLength = sizeof(ether_desc), 52023cd1385SRemy Bohmer .bDescriptorType = USB_DT_CS_INTERFACE, 52123cd1385SRemy Bohmer .bDescriptorSubType = USB_CDC_ETHERNET_TYPE, 52223cd1385SRemy Bohmer 52323cd1385SRemy Bohmer /* this descriptor actually adds value, surprise! */ 52423cd1385SRemy Bohmer .iMACAddress = STRING_ETHADDR, 52523cd1385SRemy Bohmer .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */ 52623cd1385SRemy Bohmer .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN), 52723cd1385SRemy Bohmer .wNumberMCFilters = __constant_cpu_to_le16(0), 52823cd1385SRemy Bohmer .bNumberPowerFilters = 0, 52923cd1385SRemy Bohmer }; 53023cd1385SRemy Bohmer 5317612a43dSVitaly Kuzmichev #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS) 53223cd1385SRemy Bohmer 5336142e0aeSVitaly Kuzmichev /* 5346142e0aeSVitaly Kuzmichev * include the status endpoint if we can, even where it's optional. 53523cd1385SRemy Bohmer * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one 53623cd1385SRemy Bohmer * packet, to simplify cancellation; and a big transfer interval, to 53723cd1385SRemy Bohmer * waste less bandwidth. 53823cd1385SRemy Bohmer * 53923cd1385SRemy Bohmer * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even 54023cd1385SRemy Bohmer * if they ignore the connect/disconnect notifications that real aether 54123cd1385SRemy Bohmer * can provide. more advanced cdc configurations might want to support 54223cd1385SRemy Bohmer * encapsulated commands (vendor-specific, using control-OUT). 5437612a43dSVitaly Kuzmichev * 5447612a43dSVitaly Kuzmichev * RNDIS requires the status endpoint, since it uses that encapsulation 5457612a43dSVitaly Kuzmichev * mechanism for its funky RPC scheme. 54623cd1385SRemy Bohmer */ 54723cd1385SRemy Bohmer 54823cd1385SRemy Bohmer #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */ 54923cd1385SRemy Bohmer #define STATUS_BYTECOUNT 16 /* 8 byte header + data */ 55023cd1385SRemy Bohmer 55123cd1385SRemy Bohmer static struct usb_endpoint_descriptor 55223cd1385SRemy Bohmer fs_status_desc = { 55323cd1385SRemy Bohmer .bLength = USB_DT_ENDPOINT_SIZE, 55423cd1385SRemy Bohmer .bDescriptorType = USB_DT_ENDPOINT, 55523cd1385SRemy Bohmer 55623cd1385SRemy Bohmer .bEndpointAddress = USB_DIR_IN, 55723cd1385SRemy Bohmer .bmAttributes = USB_ENDPOINT_XFER_INT, 55823cd1385SRemy Bohmer .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT), 55923cd1385SRemy Bohmer .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC, 56023cd1385SRemy Bohmer }; 56123cd1385SRemy Bohmer #endif 56223cd1385SRemy Bohmer 56323cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC 56423cd1385SRemy Bohmer 56523cd1385SRemy Bohmer /* the default data interface has no endpoints ... */ 56623cd1385SRemy Bohmer 56723cd1385SRemy Bohmer static const struct usb_interface_descriptor 56823cd1385SRemy Bohmer data_nop_intf = { 56923cd1385SRemy Bohmer .bLength = sizeof data_nop_intf, 57023cd1385SRemy Bohmer .bDescriptorType = USB_DT_INTERFACE, 57123cd1385SRemy Bohmer 57223cd1385SRemy Bohmer .bInterfaceNumber = 1, 57323cd1385SRemy Bohmer .bAlternateSetting = 0, 57423cd1385SRemy Bohmer .bNumEndpoints = 0, 57523cd1385SRemy Bohmer .bInterfaceClass = USB_CLASS_CDC_DATA, 57623cd1385SRemy Bohmer .bInterfaceSubClass = 0, 57723cd1385SRemy Bohmer .bInterfaceProtocol = 0, 57823cd1385SRemy Bohmer }; 57923cd1385SRemy Bohmer 58023cd1385SRemy Bohmer /* ... but the "real" data interface has two bulk endpoints */ 58123cd1385SRemy Bohmer 58223cd1385SRemy Bohmer static const struct usb_interface_descriptor 58323cd1385SRemy Bohmer data_intf = { 58423cd1385SRemy Bohmer .bLength = sizeof data_intf, 58523cd1385SRemy Bohmer .bDescriptorType = USB_DT_INTERFACE, 58623cd1385SRemy Bohmer 58723cd1385SRemy Bohmer .bInterfaceNumber = 1, 58823cd1385SRemy Bohmer .bAlternateSetting = 1, 58923cd1385SRemy Bohmer .bNumEndpoints = 2, 59023cd1385SRemy Bohmer .bInterfaceClass = USB_CLASS_CDC_DATA, 59123cd1385SRemy Bohmer .bInterfaceSubClass = 0, 59223cd1385SRemy Bohmer .bInterfaceProtocol = 0, 59323cd1385SRemy Bohmer .iInterface = STRING_DATA, 59423cd1385SRemy Bohmer }; 59523cd1385SRemy Bohmer 59623cd1385SRemy Bohmer #endif 59723cd1385SRemy Bohmer 5987612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS 5997612a43dSVitaly Kuzmichev 6007612a43dSVitaly Kuzmichev /* RNDIS doesn't activate by changing to the "real" altsetting */ 6017612a43dSVitaly Kuzmichev 6027612a43dSVitaly Kuzmichev static const struct usb_interface_descriptor 6037612a43dSVitaly Kuzmichev rndis_data_intf = { 6047612a43dSVitaly Kuzmichev .bLength = sizeof rndis_data_intf, 6057612a43dSVitaly Kuzmichev .bDescriptorType = USB_DT_INTERFACE, 6067612a43dSVitaly Kuzmichev 6077612a43dSVitaly Kuzmichev .bInterfaceNumber = 1, 6087612a43dSVitaly Kuzmichev .bAlternateSetting = 0, 6097612a43dSVitaly Kuzmichev .bNumEndpoints = 2, 6107612a43dSVitaly Kuzmichev .bInterfaceClass = USB_CLASS_CDC_DATA, 6117612a43dSVitaly Kuzmichev .bInterfaceSubClass = 0, 6127612a43dSVitaly Kuzmichev .bInterfaceProtocol = 0, 6137612a43dSVitaly Kuzmichev .iInterface = STRING_DATA, 6147612a43dSVitaly Kuzmichev }; 6157612a43dSVitaly Kuzmichev 6167612a43dSVitaly Kuzmichev #endif 6177612a43dSVitaly Kuzmichev 61823cd1385SRemy Bohmer #ifdef DEV_CONFIG_SUBSET 61923cd1385SRemy Bohmer 62023cd1385SRemy Bohmer /* 62123cd1385SRemy Bohmer * "Simple" CDC-subset option is a simple vendor-neutral model that most 62223cd1385SRemy Bohmer * full speed controllers can handle: one interface, two bulk endpoints. 62323cd1385SRemy Bohmer * 62423cd1385SRemy Bohmer * To assist host side drivers, we fancy it up a bit, and add descriptors 62523cd1385SRemy Bohmer * so some host side drivers will understand it as a "SAFE" variant. 62623cd1385SRemy Bohmer */ 62723cd1385SRemy Bohmer 62823cd1385SRemy Bohmer static const struct usb_interface_descriptor 62923cd1385SRemy Bohmer subset_data_intf = { 63023cd1385SRemy Bohmer .bLength = sizeof subset_data_intf, 63123cd1385SRemy Bohmer .bDescriptorType = USB_DT_INTERFACE, 63223cd1385SRemy Bohmer 63323cd1385SRemy Bohmer .bInterfaceNumber = 0, 63423cd1385SRemy Bohmer .bAlternateSetting = 0, 63523cd1385SRemy Bohmer .bNumEndpoints = 2, 63623cd1385SRemy Bohmer .bInterfaceClass = USB_CLASS_COMM, 63723cd1385SRemy Bohmer .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM, 63823cd1385SRemy Bohmer .bInterfaceProtocol = 0, 63923cd1385SRemy Bohmer .iInterface = STRING_DATA, 64023cd1385SRemy Bohmer }; 64123cd1385SRemy Bohmer 64223cd1385SRemy Bohmer #endif /* SUBSET */ 64323cd1385SRemy Bohmer 64423cd1385SRemy Bohmer static struct usb_endpoint_descriptor 64523cd1385SRemy Bohmer fs_source_desc = { 64623cd1385SRemy Bohmer .bLength = USB_DT_ENDPOINT_SIZE, 64723cd1385SRemy Bohmer .bDescriptorType = USB_DT_ENDPOINT, 64823cd1385SRemy Bohmer 64923cd1385SRemy Bohmer .bEndpointAddress = USB_DIR_IN, 65023cd1385SRemy Bohmer .bmAttributes = USB_ENDPOINT_XFER_BULK, 65123cd1385SRemy Bohmer }; 65223cd1385SRemy Bohmer 65323cd1385SRemy Bohmer static struct usb_endpoint_descriptor 65423cd1385SRemy Bohmer fs_sink_desc = { 65523cd1385SRemy Bohmer .bLength = USB_DT_ENDPOINT_SIZE, 65623cd1385SRemy Bohmer .bDescriptorType = USB_DT_ENDPOINT, 65723cd1385SRemy Bohmer 65823cd1385SRemy Bohmer .bEndpointAddress = USB_DIR_OUT, 65923cd1385SRemy Bohmer .bmAttributes = USB_ENDPOINT_XFER_BULK, 66023cd1385SRemy Bohmer }; 66123cd1385SRemy Bohmer 66223cd1385SRemy Bohmer static const struct usb_descriptor_header *fs_eth_function[11] = { 66323cd1385SRemy Bohmer (struct usb_descriptor_header *) &otg_descriptor, 66423cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC 66523cd1385SRemy Bohmer /* "cdc" mode descriptors */ 66623cd1385SRemy Bohmer (struct usb_descriptor_header *) &control_intf, 66723cd1385SRemy Bohmer (struct usb_descriptor_header *) &header_desc, 66823cd1385SRemy Bohmer (struct usb_descriptor_header *) &union_desc, 66923cd1385SRemy Bohmer (struct usb_descriptor_header *) ðer_desc, 67023cd1385SRemy Bohmer /* NOTE: status endpoint may need to be removed */ 67123cd1385SRemy Bohmer (struct usb_descriptor_header *) &fs_status_desc, 67223cd1385SRemy Bohmer /* data interface, with altsetting */ 67323cd1385SRemy Bohmer (struct usb_descriptor_header *) &data_nop_intf, 67423cd1385SRemy Bohmer (struct usb_descriptor_header *) &data_intf, 67523cd1385SRemy Bohmer (struct usb_descriptor_header *) &fs_source_desc, 67623cd1385SRemy Bohmer (struct usb_descriptor_header *) &fs_sink_desc, 67723cd1385SRemy Bohmer NULL, 67823cd1385SRemy Bohmer #endif /* DEV_CONFIG_CDC */ 67923cd1385SRemy Bohmer }; 68023cd1385SRemy Bohmer 68123cd1385SRemy Bohmer static inline void fs_subset_descriptors(void) 68223cd1385SRemy Bohmer { 68323cd1385SRemy Bohmer #ifdef DEV_CONFIG_SUBSET 68423cd1385SRemy Bohmer /* behavior is "CDC Subset"; extra descriptors say "SAFE" */ 68523cd1385SRemy Bohmer fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf; 68623cd1385SRemy Bohmer fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc; 68723cd1385SRemy Bohmer fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc; 68823cd1385SRemy Bohmer fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc; 68923cd1385SRemy Bohmer fs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc; 69023cd1385SRemy Bohmer fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc; 69123cd1385SRemy Bohmer fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc; 69223cd1385SRemy Bohmer fs_eth_function[8] = NULL; 69323cd1385SRemy Bohmer #else 69423cd1385SRemy Bohmer fs_eth_function[1] = NULL; 69523cd1385SRemy Bohmer #endif 69623cd1385SRemy Bohmer } 69723cd1385SRemy Bohmer 6987612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS 6997612a43dSVitaly Kuzmichev static const struct usb_descriptor_header *fs_rndis_function[] = { 7007612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &otg_descriptor, 7017612a43dSVitaly Kuzmichev /* control interface matches ACM, not Ethernet */ 7027612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &rndis_control_intf, 7037612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &header_desc, 7047612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &call_mgmt_descriptor, 7057612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &acm_descriptor, 7067612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &union_desc, 7077612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &fs_status_desc, 7087612a43dSVitaly Kuzmichev /* data interface has no altsetting */ 7097612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &rndis_data_intf, 7107612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &fs_source_desc, 7117612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &fs_sink_desc, 7127612a43dSVitaly Kuzmichev NULL, 7137612a43dSVitaly Kuzmichev }; 7147612a43dSVitaly Kuzmichev #endif 7157612a43dSVitaly Kuzmichev 71623cd1385SRemy Bohmer /* 71723cd1385SRemy Bohmer * usb 2.0 devices need to expose both high speed and full speed 71823cd1385SRemy Bohmer * descriptors, unless they only run at full speed. 71923cd1385SRemy Bohmer */ 72023cd1385SRemy Bohmer 7217612a43dSVitaly Kuzmichev #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS) 72223cd1385SRemy Bohmer static struct usb_endpoint_descriptor 72323cd1385SRemy Bohmer hs_status_desc = { 72423cd1385SRemy Bohmer .bLength = USB_DT_ENDPOINT_SIZE, 72523cd1385SRemy Bohmer .bDescriptorType = USB_DT_ENDPOINT, 72623cd1385SRemy Bohmer 72723cd1385SRemy Bohmer .bmAttributes = USB_ENDPOINT_XFER_INT, 72823cd1385SRemy Bohmer .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT), 72923cd1385SRemy Bohmer .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4, 73023cd1385SRemy Bohmer }; 73123cd1385SRemy Bohmer #endif /* DEV_CONFIG_CDC */ 73223cd1385SRemy Bohmer 73323cd1385SRemy Bohmer static struct usb_endpoint_descriptor 73423cd1385SRemy Bohmer hs_source_desc = { 73523cd1385SRemy Bohmer .bLength = USB_DT_ENDPOINT_SIZE, 73623cd1385SRemy Bohmer .bDescriptorType = USB_DT_ENDPOINT, 73723cd1385SRemy Bohmer 73823cd1385SRemy Bohmer .bmAttributes = USB_ENDPOINT_XFER_BULK, 73923cd1385SRemy Bohmer .wMaxPacketSize = __constant_cpu_to_le16(512), 74023cd1385SRemy Bohmer }; 74123cd1385SRemy Bohmer 74223cd1385SRemy Bohmer static struct usb_endpoint_descriptor 74323cd1385SRemy Bohmer hs_sink_desc = { 74423cd1385SRemy Bohmer .bLength = USB_DT_ENDPOINT_SIZE, 74523cd1385SRemy Bohmer .bDescriptorType = USB_DT_ENDPOINT, 74623cd1385SRemy Bohmer 74723cd1385SRemy Bohmer .bmAttributes = USB_ENDPOINT_XFER_BULK, 74823cd1385SRemy Bohmer .wMaxPacketSize = __constant_cpu_to_le16(512), 74923cd1385SRemy Bohmer }; 75023cd1385SRemy Bohmer 75123cd1385SRemy Bohmer static struct usb_qualifier_descriptor 75223cd1385SRemy Bohmer dev_qualifier = { 75323cd1385SRemy Bohmer .bLength = sizeof dev_qualifier, 75423cd1385SRemy Bohmer .bDescriptorType = USB_DT_DEVICE_QUALIFIER, 75523cd1385SRemy Bohmer 75623cd1385SRemy Bohmer .bcdUSB = __constant_cpu_to_le16(0x0200), 75723cd1385SRemy Bohmer .bDeviceClass = USB_CLASS_COMM, 75823cd1385SRemy Bohmer 75923cd1385SRemy Bohmer .bNumConfigurations = 1, 76023cd1385SRemy Bohmer }; 76123cd1385SRemy Bohmer 76223cd1385SRemy Bohmer static const struct usb_descriptor_header *hs_eth_function[11] = { 76323cd1385SRemy Bohmer (struct usb_descriptor_header *) &otg_descriptor, 76423cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC 76523cd1385SRemy Bohmer /* "cdc" mode descriptors */ 76623cd1385SRemy Bohmer (struct usb_descriptor_header *) &control_intf, 76723cd1385SRemy Bohmer (struct usb_descriptor_header *) &header_desc, 76823cd1385SRemy Bohmer (struct usb_descriptor_header *) &union_desc, 76923cd1385SRemy Bohmer (struct usb_descriptor_header *) ðer_desc, 77023cd1385SRemy Bohmer /* NOTE: status endpoint may need to be removed */ 77123cd1385SRemy Bohmer (struct usb_descriptor_header *) &hs_status_desc, 77223cd1385SRemy Bohmer /* data interface, with altsetting */ 77323cd1385SRemy Bohmer (struct usb_descriptor_header *) &data_nop_intf, 77423cd1385SRemy Bohmer (struct usb_descriptor_header *) &data_intf, 77523cd1385SRemy Bohmer (struct usb_descriptor_header *) &hs_source_desc, 77623cd1385SRemy Bohmer (struct usb_descriptor_header *) &hs_sink_desc, 77723cd1385SRemy Bohmer NULL, 77823cd1385SRemy Bohmer #endif /* DEV_CONFIG_CDC */ 77923cd1385SRemy Bohmer }; 78023cd1385SRemy Bohmer 78123cd1385SRemy Bohmer static inline void hs_subset_descriptors(void) 78223cd1385SRemy Bohmer { 78323cd1385SRemy Bohmer #ifdef DEV_CONFIG_SUBSET 78423cd1385SRemy Bohmer /* behavior is "CDC Subset"; extra descriptors say "SAFE" */ 78523cd1385SRemy Bohmer hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf; 78623cd1385SRemy Bohmer hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc; 78723cd1385SRemy Bohmer hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc; 78823cd1385SRemy Bohmer hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc; 78923cd1385SRemy Bohmer hs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc; 79023cd1385SRemy Bohmer hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc; 79123cd1385SRemy Bohmer hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc; 79223cd1385SRemy Bohmer hs_eth_function[8] = NULL; 79323cd1385SRemy Bohmer #else 79423cd1385SRemy Bohmer hs_eth_function[1] = NULL; 79523cd1385SRemy Bohmer #endif 79623cd1385SRemy Bohmer } 79723cd1385SRemy Bohmer 7987612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS 7997612a43dSVitaly Kuzmichev static const struct usb_descriptor_header *hs_rndis_function[] = { 8007612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &otg_descriptor, 8017612a43dSVitaly Kuzmichev /* control interface matches ACM, not Ethernet */ 8027612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &rndis_control_intf, 8037612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &header_desc, 8047612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &call_mgmt_descriptor, 8057612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &acm_descriptor, 8067612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &union_desc, 8077612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &hs_status_desc, 8087612a43dSVitaly Kuzmichev /* data interface has no altsetting */ 8097612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &rndis_data_intf, 8107612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &hs_source_desc, 8117612a43dSVitaly Kuzmichev (struct usb_descriptor_header *) &hs_sink_desc, 8127612a43dSVitaly Kuzmichev NULL, 8137612a43dSVitaly Kuzmichev }; 8147612a43dSVitaly Kuzmichev #endif 8157612a43dSVitaly Kuzmichev 8167612a43dSVitaly Kuzmichev 81723cd1385SRemy Bohmer /* maxpacket and other transfer characteristics vary by speed. */ 81823cd1385SRemy Bohmer static inline struct usb_endpoint_descriptor * 81923cd1385SRemy Bohmer ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs, 82023cd1385SRemy Bohmer struct usb_endpoint_descriptor *fs) 82123cd1385SRemy Bohmer { 82223cd1385SRemy Bohmer if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) 82323cd1385SRemy Bohmer return hs; 82423cd1385SRemy Bohmer return fs; 82523cd1385SRemy Bohmer } 82623cd1385SRemy Bohmer 82723cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 82823cd1385SRemy Bohmer 82923cd1385SRemy Bohmer /* descriptors that are built on-demand */ 83023cd1385SRemy Bohmer 83123cd1385SRemy Bohmer static char manufacturer[50]; 83223cd1385SRemy Bohmer static char product_desc[40] = DRIVER_DESC; 83323cd1385SRemy Bohmer static char serial_number[20]; 83423cd1385SRemy Bohmer 83523cd1385SRemy Bohmer /* address that the host will use ... usually assigned at random */ 83623cd1385SRemy Bohmer static char ethaddr[2 * ETH_ALEN + 1]; 83723cd1385SRemy Bohmer 83823cd1385SRemy Bohmer /* static strings, in UTF-8 */ 83923cd1385SRemy Bohmer static struct usb_string strings[] = { 84023cd1385SRemy Bohmer { STRING_MANUFACTURER, manufacturer, }, 84123cd1385SRemy Bohmer { STRING_PRODUCT, product_desc, }, 84223cd1385SRemy Bohmer { STRING_SERIALNUMBER, serial_number, }, 84323cd1385SRemy Bohmer { STRING_DATA, "Ethernet Data", }, 84423cd1385SRemy Bohmer { STRING_ETHADDR, ethaddr, }, 84523cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC 84623cd1385SRemy Bohmer { STRING_CDC, "CDC Ethernet", }, 84723cd1385SRemy Bohmer { STRING_CONTROL, "CDC Communications Control", }, 84823cd1385SRemy Bohmer #endif 84923cd1385SRemy Bohmer #ifdef DEV_CONFIG_SUBSET 85023cd1385SRemy Bohmer { STRING_SUBSET, "CDC Ethernet Subset", }, 85123cd1385SRemy Bohmer #endif 8527612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS 8537612a43dSVitaly Kuzmichev { STRING_RNDIS, "RNDIS", }, 8547612a43dSVitaly Kuzmichev { STRING_RNDIS_CONTROL, "RNDIS Communications Control", }, 8557612a43dSVitaly Kuzmichev #endif 85623cd1385SRemy Bohmer { } /* end of list */ 85723cd1385SRemy Bohmer }; 85823cd1385SRemy Bohmer 85923cd1385SRemy Bohmer static struct usb_gadget_strings stringtab = { 86023cd1385SRemy Bohmer .language = 0x0409, /* en-us */ 86123cd1385SRemy Bohmer .strings = strings, 86223cd1385SRemy Bohmer }; 86323cd1385SRemy Bohmer 86423cd1385SRemy Bohmer /*============================================================================*/ 86523cd1385SRemy Bohmer static u8 control_req[USB_BUFSIZ]; 86680460772SStefano Babic static u8 status_req[STATUS_BYTECOUNT] __attribute__ ((aligned(4))); 86723cd1385SRemy Bohmer 86823cd1385SRemy Bohmer 86923cd1385SRemy Bohmer /** 87023cd1385SRemy Bohmer * strlcpy - Copy a %NUL terminated string into a sized buffer 87123cd1385SRemy Bohmer * @dest: Where to copy the string to 87223cd1385SRemy Bohmer * @src: Where to copy the string from 87323cd1385SRemy Bohmer * @size: size of destination buffer 87423cd1385SRemy Bohmer * 87523cd1385SRemy Bohmer * Compatible with *BSD: the result is always a valid 87623cd1385SRemy Bohmer * NUL-terminated string that fits in the buffer (unless, 87723cd1385SRemy Bohmer * of course, the buffer size is zero). It does not pad 87823cd1385SRemy Bohmer * out the result like strncpy() does. 87923cd1385SRemy Bohmer */ 88023cd1385SRemy Bohmer size_t strlcpy(char *dest, const char *src, size_t size) 88123cd1385SRemy Bohmer { 88223cd1385SRemy Bohmer size_t ret = strlen(src); 88323cd1385SRemy Bohmer 88423cd1385SRemy Bohmer if (size) { 88523cd1385SRemy Bohmer size_t len = (ret >= size) ? size - 1 : ret; 88623cd1385SRemy Bohmer memcpy(dest, src, len); 88723cd1385SRemy Bohmer dest[len] = '\0'; 88823cd1385SRemy Bohmer } 88923cd1385SRemy Bohmer return ret; 89023cd1385SRemy Bohmer } 89123cd1385SRemy Bohmer 89223cd1385SRemy Bohmer /*============================================================================*/ 89323cd1385SRemy Bohmer 89423cd1385SRemy Bohmer /* 89523cd1385SRemy Bohmer * one config, two interfaces: control, data. 89623cd1385SRemy Bohmer * complications: class descriptors, and an altsetting. 89723cd1385SRemy Bohmer */ 89823cd1385SRemy Bohmer static int 89923cd1385SRemy Bohmer config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg) 90023cd1385SRemy Bohmer { 90123cd1385SRemy Bohmer int len; 90223cd1385SRemy Bohmer const struct usb_config_descriptor *config; 90323cd1385SRemy Bohmer const struct usb_descriptor_header **function; 90423cd1385SRemy Bohmer int hs = 0; 90523cd1385SRemy Bohmer 90623cd1385SRemy Bohmer if (gadget_is_dualspeed(g)) { 90723cd1385SRemy Bohmer hs = (g->speed == USB_SPEED_HIGH); 90823cd1385SRemy Bohmer if (type == USB_DT_OTHER_SPEED_CONFIG) 90923cd1385SRemy Bohmer hs = !hs; 91023cd1385SRemy Bohmer } 91123cd1385SRemy Bohmer #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function) 91223cd1385SRemy Bohmer 91323cd1385SRemy Bohmer if (index >= device_desc.bNumConfigurations) 91423cd1385SRemy Bohmer return -EINVAL; 91523cd1385SRemy Bohmer 9167612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS 9177612a43dSVitaly Kuzmichev /* 9187612a43dSVitaly Kuzmichev * list the RNDIS config first, to make Microsoft's drivers 9197612a43dSVitaly Kuzmichev * happy. DOCSIS 1.0 needs this too. 9207612a43dSVitaly Kuzmichev */ 9217612a43dSVitaly Kuzmichev if (device_desc.bNumConfigurations == 2 && index == 0) { 9227612a43dSVitaly Kuzmichev config = &rndis_config; 9237612a43dSVitaly Kuzmichev function = which_fn(rndis); 9247612a43dSVitaly Kuzmichev } else 9257612a43dSVitaly Kuzmichev #endif 9267612a43dSVitaly Kuzmichev { 92723cd1385SRemy Bohmer config = ð_config; 92823cd1385SRemy Bohmer function = which_fn(eth); 9297612a43dSVitaly Kuzmichev } 93023cd1385SRemy Bohmer 93123cd1385SRemy Bohmer /* for now, don't advertise srp-only devices */ 93223cd1385SRemy Bohmer if (!is_otg) 93323cd1385SRemy Bohmer function++; 93423cd1385SRemy Bohmer 93523cd1385SRemy Bohmer len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function); 93623cd1385SRemy Bohmer if (len < 0) 93723cd1385SRemy Bohmer return len; 93823cd1385SRemy Bohmer ((struct usb_config_descriptor *) buf)->bDescriptorType = type; 93923cd1385SRemy Bohmer return len; 94023cd1385SRemy Bohmer } 94123cd1385SRemy Bohmer 94223cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 94323cd1385SRemy Bohmer 9447612a43dSVitaly Kuzmichev static void eth_start(struct eth_dev *dev, gfp_t gfp_flags); 94523cd1385SRemy Bohmer static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags); 94623cd1385SRemy Bohmer 94723cd1385SRemy Bohmer static int 94823cd1385SRemy Bohmer set_ether_config(struct eth_dev *dev, gfp_t gfp_flags) 94923cd1385SRemy Bohmer { 95023cd1385SRemy Bohmer int result = 0; 95123cd1385SRemy Bohmer struct usb_gadget *gadget = dev->gadget; 95223cd1385SRemy Bohmer 9537612a43dSVitaly Kuzmichev #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS) 9547612a43dSVitaly Kuzmichev /* status endpoint used for RNDIS and (optionally) CDC */ 95523cd1385SRemy Bohmer if (!subset_active(dev) && dev->status_ep) { 95623cd1385SRemy Bohmer dev->status = ep_desc(gadget, &hs_status_desc, 95723cd1385SRemy Bohmer &fs_status_desc); 95823cd1385SRemy Bohmer dev->status_ep->driver_data = dev; 95923cd1385SRemy Bohmer 96023cd1385SRemy Bohmer result = usb_ep_enable(dev->status_ep, dev->status); 96123cd1385SRemy Bohmer if (result != 0) { 9627de73185SVitaly Kuzmichev debug("enable %s --> %d\n", 96323cd1385SRemy Bohmer dev->status_ep->name, result); 96423cd1385SRemy Bohmer goto done; 96523cd1385SRemy Bohmer } 96623cd1385SRemy Bohmer } 96723cd1385SRemy Bohmer #endif 96823cd1385SRemy Bohmer 96923cd1385SRemy Bohmer dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc); 97023cd1385SRemy Bohmer dev->in_ep->driver_data = dev; 97123cd1385SRemy Bohmer 97223cd1385SRemy Bohmer dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc); 97323cd1385SRemy Bohmer dev->out_ep->driver_data = dev; 97423cd1385SRemy Bohmer 9756142e0aeSVitaly Kuzmichev /* 9766142e0aeSVitaly Kuzmichev * With CDC, the host isn't allowed to use these two data 97723cd1385SRemy Bohmer * endpoints in the default altsetting for the interface. 97823cd1385SRemy Bohmer * so we don't activate them yet. Reset from SET_INTERFACE. 9797612a43dSVitaly Kuzmichev * 9807612a43dSVitaly Kuzmichev * Strictly speaking RNDIS should work the same: activation is 9817612a43dSVitaly Kuzmichev * a side effect of setting a packet filter. Deactivation is 9827612a43dSVitaly Kuzmichev * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG. 98323cd1385SRemy Bohmer */ 98423cd1385SRemy Bohmer if (!cdc_active(dev)) { 98523cd1385SRemy Bohmer result = usb_ep_enable(dev->in_ep, dev->in); 98623cd1385SRemy Bohmer if (result != 0) { 9877de73185SVitaly Kuzmichev debug("enable %s --> %d\n", 98823cd1385SRemy Bohmer dev->in_ep->name, result); 98923cd1385SRemy Bohmer goto done; 99023cd1385SRemy Bohmer } 99123cd1385SRemy Bohmer 99223cd1385SRemy Bohmer result = usb_ep_enable(dev->out_ep, dev->out); 99323cd1385SRemy Bohmer if (result != 0) { 9947de73185SVitaly Kuzmichev debug("enable %s --> %d\n", 99523cd1385SRemy Bohmer dev->out_ep->name, result); 99623cd1385SRemy Bohmer goto done; 99723cd1385SRemy Bohmer } 99823cd1385SRemy Bohmer } 99923cd1385SRemy Bohmer 100023cd1385SRemy Bohmer done: 100123cd1385SRemy Bohmer if (result == 0) 100223cd1385SRemy Bohmer result = alloc_requests(dev, qlen(gadget), gfp_flags); 100323cd1385SRemy Bohmer 100423cd1385SRemy Bohmer /* on error, disable any endpoints */ 100523cd1385SRemy Bohmer if (result < 0) { 1006d5292c16SVitaly Kuzmichev if (!subset_active(dev) && dev->status_ep) 100723cd1385SRemy Bohmer (void) usb_ep_disable(dev->status_ep); 100823cd1385SRemy Bohmer dev->status = NULL; 100923cd1385SRemy Bohmer (void) usb_ep_disable(dev->in_ep); 101023cd1385SRemy Bohmer (void) usb_ep_disable(dev->out_ep); 101123cd1385SRemy Bohmer dev->in = NULL; 101223cd1385SRemy Bohmer dev->out = NULL; 10137612a43dSVitaly Kuzmichev } else if (!cdc_active(dev)) { 10147612a43dSVitaly Kuzmichev /* 10157612a43dSVitaly Kuzmichev * activate non-CDC configs right away 10167612a43dSVitaly Kuzmichev * this isn't strictly according to the RNDIS spec 10177612a43dSVitaly Kuzmichev */ 10187612a43dSVitaly Kuzmichev eth_start(dev, GFP_ATOMIC); 101923cd1385SRemy Bohmer } 102023cd1385SRemy Bohmer 102123cd1385SRemy Bohmer /* caller is responsible for cleanup on error */ 102223cd1385SRemy Bohmer return result; 102323cd1385SRemy Bohmer } 102423cd1385SRemy Bohmer 102523cd1385SRemy Bohmer static void eth_reset_config(struct eth_dev *dev) 102623cd1385SRemy Bohmer { 102723cd1385SRemy Bohmer if (dev->config == 0) 102823cd1385SRemy Bohmer return; 102923cd1385SRemy Bohmer 10307de73185SVitaly Kuzmichev debug("%s\n", __func__); 10317de73185SVitaly Kuzmichev 10327612a43dSVitaly Kuzmichev rndis_uninit(dev->rndis_config); 10337612a43dSVitaly Kuzmichev 10346142e0aeSVitaly Kuzmichev /* 10356142e0aeSVitaly Kuzmichev * disable endpoints, forcing (synchronous) completion of 103623cd1385SRemy Bohmer * pending i/o. then free the requests. 103723cd1385SRemy Bohmer */ 103823cd1385SRemy Bohmer 103923cd1385SRemy Bohmer if (dev->in) { 104023cd1385SRemy Bohmer usb_ep_disable(dev->in_ep); 104123cd1385SRemy Bohmer if (dev->tx_req) { 104223cd1385SRemy Bohmer usb_ep_free_request(dev->in_ep, dev->tx_req); 104323cd1385SRemy Bohmer dev->tx_req = NULL; 104423cd1385SRemy Bohmer } 104523cd1385SRemy Bohmer } 104623cd1385SRemy Bohmer if (dev->out) { 104723cd1385SRemy Bohmer usb_ep_disable(dev->out_ep); 104823cd1385SRemy Bohmer if (dev->rx_req) { 10490129e327SVitaly Kuzmichev usb_ep_free_request(dev->out_ep, dev->rx_req); 105023cd1385SRemy Bohmer dev->rx_req = NULL; 105123cd1385SRemy Bohmer } 105223cd1385SRemy Bohmer } 10536142e0aeSVitaly Kuzmichev if (dev->status) 105423cd1385SRemy Bohmer usb_ep_disable(dev->status_ep); 10556142e0aeSVitaly Kuzmichev 10567612a43dSVitaly Kuzmichev dev->rndis = 0; 105723cd1385SRemy Bohmer dev->cdc_filter = 0; 105823cd1385SRemy Bohmer dev->config = 0; 105923cd1385SRemy Bohmer } 106023cd1385SRemy Bohmer 10616142e0aeSVitaly Kuzmichev /* 10626142e0aeSVitaly Kuzmichev * change our operational config. must agree with the code 106323cd1385SRemy Bohmer * that returns config descriptors, and altsetting code. 106423cd1385SRemy Bohmer */ 10656142e0aeSVitaly Kuzmichev static int eth_set_config(struct eth_dev *dev, unsigned number, 10666142e0aeSVitaly Kuzmichev gfp_t gfp_flags) 106723cd1385SRemy Bohmer { 106823cd1385SRemy Bohmer int result = 0; 106923cd1385SRemy Bohmer struct usb_gadget *gadget = dev->gadget; 107023cd1385SRemy Bohmer 107123cd1385SRemy Bohmer if (gadget_is_sa1100(gadget) 107223cd1385SRemy Bohmer && dev->config 107323cd1385SRemy Bohmer && dev->tx_qlen != 0) { 107423cd1385SRemy Bohmer /* tx fifo is full, but we can't clear it...*/ 10757de73185SVitaly Kuzmichev error("can't change configurations"); 107623cd1385SRemy Bohmer return -ESPIPE; 107723cd1385SRemy Bohmer } 107823cd1385SRemy Bohmer eth_reset_config(dev); 107923cd1385SRemy Bohmer 108023cd1385SRemy Bohmer switch (number) { 108123cd1385SRemy Bohmer case DEV_CONFIG_VALUE: 108223cd1385SRemy Bohmer result = set_ether_config(dev, gfp_flags); 108323cd1385SRemy Bohmer break; 10847612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS 10857612a43dSVitaly Kuzmichev case DEV_RNDIS_CONFIG_VALUE: 10867612a43dSVitaly Kuzmichev dev->rndis = 1; 10877612a43dSVitaly Kuzmichev result = set_ether_config(dev, gfp_flags); 10887612a43dSVitaly Kuzmichev break; 10897612a43dSVitaly Kuzmichev #endif 109023cd1385SRemy Bohmer default: 109123cd1385SRemy Bohmer result = -EINVAL; 109223cd1385SRemy Bohmer /* FALL THROUGH */ 109323cd1385SRemy Bohmer case 0: 109423cd1385SRemy Bohmer break; 109523cd1385SRemy Bohmer } 109623cd1385SRemy Bohmer 109723cd1385SRemy Bohmer if (result) { 109823cd1385SRemy Bohmer if (number) 109923cd1385SRemy Bohmer eth_reset_config(dev); 110023cd1385SRemy Bohmer usb_gadget_vbus_draw(dev->gadget, 110123cd1385SRemy Bohmer gadget_is_otg(dev->gadget) ? 8 : 100); 110223cd1385SRemy Bohmer } else { 110323cd1385SRemy Bohmer char *speed; 110423cd1385SRemy Bohmer unsigned power; 110523cd1385SRemy Bohmer 110623cd1385SRemy Bohmer power = 2 * eth_config.bMaxPower; 110723cd1385SRemy Bohmer usb_gadget_vbus_draw(dev->gadget, power); 110823cd1385SRemy Bohmer 110923cd1385SRemy Bohmer switch (gadget->speed) { 11106142e0aeSVitaly Kuzmichev case USB_SPEED_FULL: 11116142e0aeSVitaly Kuzmichev speed = "full"; break; 111223cd1385SRemy Bohmer #ifdef CONFIG_USB_GADGET_DUALSPEED 11136142e0aeSVitaly Kuzmichev case USB_SPEED_HIGH: 11146142e0aeSVitaly Kuzmichev speed = "high"; break; 111523cd1385SRemy Bohmer #endif 11166142e0aeSVitaly Kuzmichev default: 11176142e0aeSVitaly Kuzmichev speed = "?"; break; 111823cd1385SRemy Bohmer } 111923cd1385SRemy Bohmer 112023cd1385SRemy Bohmer dev->config = number; 11217de73185SVitaly Kuzmichev printf("%s speed config #%d: %d mA, %s, using %s\n", 112223cd1385SRemy Bohmer speed, number, power, driver_desc, 11237612a43dSVitaly Kuzmichev rndis_active(dev) 11247612a43dSVitaly Kuzmichev ? "RNDIS" 11257612a43dSVitaly Kuzmichev : (cdc_active(dev) 11267612a43dSVitaly Kuzmichev ? "CDC Ethernet" 112723cd1385SRemy Bohmer : "CDC Ethernet Subset")); 112823cd1385SRemy Bohmer } 112923cd1385SRemy Bohmer return result; 113023cd1385SRemy Bohmer } 113123cd1385SRemy Bohmer 113223cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 113323cd1385SRemy Bohmer 113423cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC 113523cd1385SRemy Bohmer 11366142e0aeSVitaly Kuzmichev /* 11376142e0aeSVitaly Kuzmichev * The interrupt endpoint is used in CDC networking models (Ethernet, ATM) 113823cd1385SRemy Bohmer * only to notify the host about link status changes (which we support) or 11397612a43dSVitaly Kuzmichev * report completion of some encapsulated command (as used in RNDIS). Since 114023cd1385SRemy Bohmer * we want this CDC Ethernet code to be vendor-neutral, we don't use that 114123cd1385SRemy Bohmer * command mechanism; and only one status request is ever queued. 114223cd1385SRemy Bohmer */ 114323cd1385SRemy Bohmer static void eth_status_complete(struct usb_ep *ep, struct usb_request *req) 114423cd1385SRemy Bohmer { 114523cd1385SRemy Bohmer struct usb_cdc_notification *event = req->buf; 114623cd1385SRemy Bohmer int value = req->status; 114723cd1385SRemy Bohmer struct eth_dev *dev = ep->driver_data; 114823cd1385SRemy Bohmer 114923cd1385SRemy Bohmer /* issue the second notification if host reads the first */ 115023cd1385SRemy Bohmer if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION 115123cd1385SRemy Bohmer && value == 0) { 115223cd1385SRemy Bohmer __le32 *data = req->buf + sizeof *event; 115323cd1385SRemy Bohmer 115423cd1385SRemy Bohmer event->bmRequestType = 0xA1; 115523cd1385SRemy Bohmer event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE; 115623cd1385SRemy Bohmer event->wValue = __constant_cpu_to_le16(0); 115723cd1385SRemy Bohmer event->wIndex = __constant_cpu_to_le16(1); 115823cd1385SRemy Bohmer event->wLength = __constant_cpu_to_le16(8); 115923cd1385SRemy Bohmer 116023cd1385SRemy Bohmer /* SPEED_CHANGE data is up/down speeds in bits/sec */ 116123cd1385SRemy Bohmer data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget)); 116223cd1385SRemy Bohmer 116323cd1385SRemy Bohmer req->length = STATUS_BYTECOUNT; 116423cd1385SRemy Bohmer value = usb_ep_queue(ep, req, GFP_ATOMIC); 11657de73185SVitaly Kuzmichev debug("send SPEED_CHANGE --> %d\n", value); 116623cd1385SRemy Bohmer if (value == 0) 116723cd1385SRemy Bohmer return; 116823cd1385SRemy Bohmer } else if (value != -ECONNRESET) { 11697de73185SVitaly Kuzmichev debug("event %02x --> %d\n", 117023cd1385SRemy Bohmer event->bNotificationType, value); 117123cd1385SRemy Bohmer if (event->bNotificationType == 11726142e0aeSVitaly Kuzmichev USB_CDC_NOTIFY_SPEED_CHANGE) { 117323cd1385SRemy Bohmer l_ethdev.network_started = 1; 117423cd1385SRemy Bohmer printf("USB network up!\n"); 117523cd1385SRemy Bohmer } 117623cd1385SRemy Bohmer } 117723cd1385SRemy Bohmer req->context = NULL; 117823cd1385SRemy Bohmer } 117923cd1385SRemy Bohmer 118023cd1385SRemy Bohmer static void issue_start_status(struct eth_dev *dev) 118123cd1385SRemy Bohmer { 118223cd1385SRemy Bohmer struct usb_request *req = dev->stat_req; 118323cd1385SRemy Bohmer struct usb_cdc_notification *event; 118423cd1385SRemy Bohmer int value; 118523cd1385SRemy Bohmer 11866142e0aeSVitaly Kuzmichev /* 11876142e0aeSVitaly Kuzmichev * flush old status 118823cd1385SRemy Bohmer * 118923cd1385SRemy Bohmer * FIXME ugly idiom, maybe we'd be better with just 119023cd1385SRemy Bohmer * a "cancel the whole queue" primitive since any 119123cd1385SRemy Bohmer * unlink-one primitive has way too many error modes. 119223cd1385SRemy Bohmer * here, we "know" toggle is already clear... 119323cd1385SRemy Bohmer * 119423cd1385SRemy Bohmer * FIXME iff req->context != null just dequeue it 119523cd1385SRemy Bohmer */ 119623cd1385SRemy Bohmer usb_ep_disable(dev->status_ep); 119723cd1385SRemy Bohmer usb_ep_enable(dev->status_ep, dev->status); 119823cd1385SRemy Bohmer 11996142e0aeSVitaly Kuzmichev /* 12006142e0aeSVitaly Kuzmichev * 3.8.1 says to issue first NETWORK_CONNECTION, then 120123cd1385SRemy Bohmer * a SPEED_CHANGE. could be useful in some configs. 120223cd1385SRemy Bohmer */ 120323cd1385SRemy Bohmer event = req->buf; 120423cd1385SRemy Bohmer event->bmRequestType = 0xA1; 120523cd1385SRemy Bohmer event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION; 120623cd1385SRemy Bohmer event->wValue = __constant_cpu_to_le16(1); /* connected */ 120723cd1385SRemy Bohmer event->wIndex = __constant_cpu_to_le16(1); 120823cd1385SRemy Bohmer event->wLength = 0; 120923cd1385SRemy Bohmer 121023cd1385SRemy Bohmer req->length = sizeof *event; 121123cd1385SRemy Bohmer req->complete = eth_status_complete; 121223cd1385SRemy Bohmer req->context = dev; 121323cd1385SRemy Bohmer 121423cd1385SRemy Bohmer value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC); 121523cd1385SRemy Bohmer if (value < 0) 12167de73185SVitaly Kuzmichev debug("status buf queue --> %d\n", value); 121723cd1385SRemy Bohmer } 121823cd1385SRemy Bohmer 121923cd1385SRemy Bohmer #endif 122023cd1385SRemy Bohmer 122123cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 122223cd1385SRemy Bohmer 122323cd1385SRemy Bohmer static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req) 122423cd1385SRemy Bohmer { 122523cd1385SRemy Bohmer if (req->status || req->actual != req->length) 12267de73185SVitaly Kuzmichev debug("setup complete --> %d, %d/%d\n", 122723cd1385SRemy Bohmer req->status, req->actual, req->length); 122823cd1385SRemy Bohmer } 122923cd1385SRemy Bohmer 12307612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS 12317612a43dSVitaly Kuzmichev 12327612a43dSVitaly Kuzmichev static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req) 12337612a43dSVitaly Kuzmichev { 12347612a43dSVitaly Kuzmichev if (req->status || req->actual != req->length) 12357612a43dSVitaly Kuzmichev debug("rndis response complete --> %d, %d/%d\n", 12367612a43dSVitaly Kuzmichev req->status, req->actual, req->length); 12377612a43dSVitaly Kuzmichev 12387612a43dSVitaly Kuzmichev /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */ 12397612a43dSVitaly Kuzmichev } 12407612a43dSVitaly Kuzmichev 12417612a43dSVitaly Kuzmichev static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req) 12427612a43dSVitaly Kuzmichev { 12437612a43dSVitaly Kuzmichev struct eth_dev *dev = ep->driver_data; 12447612a43dSVitaly Kuzmichev int status; 12457612a43dSVitaly Kuzmichev 12467612a43dSVitaly Kuzmichev /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */ 12477612a43dSVitaly Kuzmichev status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf); 12487612a43dSVitaly Kuzmichev if (status < 0) 12497612a43dSVitaly Kuzmichev error("%s: rndis parse error %d", __func__, status); 12507612a43dSVitaly Kuzmichev } 12517612a43dSVitaly Kuzmichev 12527612a43dSVitaly Kuzmichev #endif /* RNDIS */ 12537612a43dSVitaly Kuzmichev 125423cd1385SRemy Bohmer /* 125523cd1385SRemy Bohmer * The setup() callback implements all the ep0 functionality that's not 125623cd1385SRemy Bohmer * handled lower down. CDC has a number of less-common features: 125723cd1385SRemy Bohmer * 125823cd1385SRemy Bohmer * - two interfaces: control, and ethernet data 125923cd1385SRemy Bohmer * - Ethernet data interface has two altsettings: default, and active 126023cd1385SRemy Bohmer * - class-specific descriptors for the control interface 126123cd1385SRemy Bohmer * - class-specific control requests 126223cd1385SRemy Bohmer */ 126323cd1385SRemy Bohmer static int 126423cd1385SRemy Bohmer eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) 126523cd1385SRemy Bohmer { 126623cd1385SRemy Bohmer struct eth_dev *dev = get_gadget_data(gadget); 126723cd1385SRemy Bohmer struct usb_request *req = dev->req; 126823cd1385SRemy Bohmer int value = -EOPNOTSUPP; 126923cd1385SRemy Bohmer u16 wIndex = le16_to_cpu(ctrl->wIndex); 127023cd1385SRemy Bohmer u16 wValue = le16_to_cpu(ctrl->wValue); 127123cd1385SRemy Bohmer u16 wLength = le16_to_cpu(ctrl->wLength); 127223cd1385SRemy Bohmer 12736142e0aeSVitaly Kuzmichev /* 12746142e0aeSVitaly Kuzmichev * descriptors just go into the pre-allocated ep0 buffer, 127523cd1385SRemy Bohmer * while config change events may enable network traffic. 127623cd1385SRemy Bohmer */ 127723cd1385SRemy Bohmer 12787de73185SVitaly Kuzmichev debug("%s\n", __func__); 127923cd1385SRemy Bohmer 128023cd1385SRemy Bohmer req->complete = eth_setup_complete; 128123cd1385SRemy Bohmer switch (ctrl->bRequest) { 128223cd1385SRemy Bohmer 128323cd1385SRemy Bohmer case USB_REQ_GET_DESCRIPTOR: 128423cd1385SRemy Bohmer if (ctrl->bRequestType != USB_DIR_IN) 128523cd1385SRemy Bohmer break; 128623cd1385SRemy Bohmer switch (wValue >> 8) { 128723cd1385SRemy Bohmer 128823cd1385SRemy Bohmer case USB_DT_DEVICE: 128923cd1385SRemy Bohmer value = min(wLength, (u16) sizeof device_desc); 129023cd1385SRemy Bohmer memcpy(req->buf, &device_desc, value); 129123cd1385SRemy Bohmer break; 129223cd1385SRemy Bohmer case USB_DT_DEVICE_QUALIFIER: 129323cd1385SRemy Bohmer if (!gadget_is_dualspeed(gadget)) 129423cd1385SRemy Bohmer break; 129523cd1385SRemy Bohmer value = min(wLength, (u16) sizeof dev_qualifier); 129623cd1385SRemy Bohmer memcpy(req->buf, &dev_qualifier, value); 129723cd1385SRemy Bohmer break; 129823cd1385SRemy Bohmer 129923cd1385SRemy Bohmer case USB_DT_OTHER_SPEED_CONFIG: 130023cd1385SRemy Bohmer if (!gadget_is_dualspeed(gadget)) 130123cd1385SRemy Bohmer break; 130223cd1385SRemy Bohmer /* FALLTHROUGH */ 130323cd1385SRemy Bohmer case USB_DT_CONFIG: 130423cd1385SRemy Bohmer value = config_buf(gadget, req->buf, 130523cd1385SRemy Bohmer wValue >> 8, 130623cd1385SRemy Bohmer wValue & 0xff, 130723cd1385SRemy Bohmer gadget_is_otg(gadget)); 130823cd1385SRemy Bohmer if (value >= 0) 130923cd1385SRemy Bohmer value = min(wLength, (u16) value); 131023cd1385SRemy Bohmer break; 131123cd1385SRemy Bohmer 131223cd1385SRemy Bohmer case USB_DT_STRING: 131323cd1385SRemy Bohmer value = usb_gadget_get_string(&stringtab, 131423cd1385SRemy Bohmer wValue & 0xff, req->buf); 131523cd1385SRemy Bohmer 131623cd1385SRemy Bohmer if (value >= 0) 131723cd1385SRemy Bohmer value = min(wLength, (u16) value); 131823cd1385SRemy Bohmer 131923cd1385SRemy Bohmer break; 132023cd1385SRemy Bohmer } 132123cd1385SRemy Bohmer break; 132223cd1385SRemy Bohmer 132323cd1385SRemy Bohmer case USB_REQ_SET_CONFIGURATION: 132423cd1385SRemy Bohmer if (ctrl->bRequestType != 0) 132523cd1385SRemy Bohmer break; 132623cd1385SRemy Bohmer if (gadget->a_hnp_support) 13277de73185SVitaly Kuzmichev debug("HNP available\n"); 132823cd1385SRemy Bohmer else if (gadget->a_alt_hnp_support) 13297de73185SVitaly Kuzmichev debug("HNP needs a different root port\n"); 133023cd1385SRemy Bohmer value = eth_set_config(dev, wValue, GFP_ATOMIC); 133123cd1385SRemy Bohmer break; 133223cd1385SRemy Bohmer case USB_REQ_GET_CONFIGURATION: 133323cd1385SRemy Bohmer if (ctrl->bRequestType != USB_DIR_IN) 133423cd1385SRemy Bohmer break; 133523cd1385SRemy Bohmer *(u8 *)req->buf = dev->config; 133623cd1385SRemy Bohmer value = min(wLength, (u16) 1); 133723cd1385SRemy Bohmer break; 133823cd1385SRemy Bohmer 133923cd1385SRemy Bohmer case USB_REQ_SET_INTERFACE: 134023cd1385SRemy Bohmer if (ctrl->bRequestType != USB_RECIP_INTERFACE 134123cd1385SRemy Bohmer || !dev->config 134223cd1385SRemy Bohmer || wIndex > 1) 134323cd1385SRemy Bohmer break; 134423cd1385SRemy Bohmer if (!cdc_active(dev) && wIndex != 0) 134523cd1385SRemy Bohmer break; 134623cd1385SRemy Bohmer 13476142e0aeSVitaly Kuzmichev /* 13486142e0aeSVitaly Kuzmichev * PXA hardware partially handles SET_INTERFACE; 134923cd1385SRemy Bohmer * we need to kluge around that interference. 135023cd1385SRemy Bohmer */ 135123cd1385SRemy Bohmer if (gadget_is_pxa(gadget)) { 135223cd1385SRemy Bohmer value = eth_set_config(dev, DEV_CONFIG_VALUE, 135323cd1385SRemy Bohmer GFP_ATOMIC); 135423cd1385SRemy Bohmer goto done_set_intf; 135523cd1385SRemy Bohmer } 135623cd1385SRemy Bohmer 135723cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC 135823cd1385SRemy Bohmer switch (wIndex) { 135923cd1385SRemy Bohmer case 0: /* control/master intf */ 136023cd1385SRemy Bohmer if (wValue != 0) 136123cd1385SRemy Bohmer break; 136223cd1385SRemy Bohmer if (dev->status) { 136323cd1385SRemy Bohmer usb_ep_disable(dev->status_ep); 136423cd1385SRemy Bohmer usb_ep_enable(dev->status_ep, dev->status); 136523cd1385SRemy Bohmer } 13667612a43dSVitaly Kuzmichev 136723cd1385SRemy Bohmer value = 0; 136823cd1385SRemy Bohmer break; 136923cd1385SRemy Bohmer case 1: /* data intf */ 137023cd1385SRemy Bohmer if (wValue > 1) 137123cd1385SRemy Bohmer break; 137223cd1385SRemy Bohmer usb_ep_disable(dev->in_ep); 137323cd1385SRemy Bohmer usb_ep_disable(dev->out_ep); 137423cd1385SRemy Bohmer 13756142e0aeSVitaly Kuzmichev /* 13766142e0aeSVitaly Kuzmichev * CDC requires the data transfers not be done from 137723cd1385SRemy Bohmer * the default interface setting ... also, setting 137823cd1385SRemy Bohmer * the non-default interface resets filters etc. 137923cd1385SRemy Bohmer */ 138023cd1385SRemy Bohmer if (wValue == 1) { 138123cd1385SRemy Bohmer if (!cdc_active(dev)) 138223cd1385SRemy Bohmer break; 138323cd1385SRemy Bohmer usb_ep_enable(dev->in_ep, dev->in); 138423cd1385SRemy Bohmer usb_ep_enable(dev->out_ep, dev->out); 138523cd1385SRemy Bohmer dev->cdc_filter = DEFAULT_FILTER; 138623cd1385SRemy Bohmer if (dev->status) 138723cd1385SRemy Bohmer issue_start_status(dev); 13887612a43dSVitaly Kuzmichev eth_start(dev, GFP_ATOMIC); 138923cd1385SRemy Bohmer } 139023cd1385SRemy Bohmer value = 0; 139123cd1385SRemy Bohmer break; 139223cd1385SRemy Bohmer } 139323cd1385SRemy Bohmer #else 13946142e0aeSVitaly Kuzmichev /* 13956142e0aeSVitaly Kuzmichev * FIXME this is wrong, as is the assumption that 139623cd1385SRemy Bohmer * all non-PXA hardware talks real CDC ... 139723cd1385SRemy Bohmer */ 13987de73185SVitaly Kuzmichev debug("set_interface ignored!\n"); 139923cd1385SRemy Bohmer #endif /* DEV_CONFIG_CDC */ 140023cd1385SRemy Bohmer 140123cd1385SRemy Bohmer done_set_intf: 140223cd1385SRemy Bohmer break; 140323cd1385SRemy Bohmer case USB_REQ_GET_INTERFACE: 140423cd1385SRemy Bohmer if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE) 140523cd1385SRemy Bohmer || !dev->config 140623cd1385SRemy Bohmer || wIndex > 1) 140723cd1385SRemy Bohmer break; 14087612a43dSVitaly Kuzmichev if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0) 140923cd1385SRemy Bohmer break; 141023cd1385SRemy Bohmer 141123cd1385SRemy Bohmer /* for CDC, iff carrier is on, data interface is active. */ 14127612a43dSVitaly Kuzmichev if (rndis_active(dev) || wIndex != 1) 141323cd1385SRemy Bohmer *(u8 *)req->buf = 0; 141423cd1385SRemy Bohmer else { 141523cd1385SRemy Bohmer /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */ 141623cd1385SRemy Bohmer /* carrier always ok ...*/ 141723cd1385SRemy Bohmer *(u8 *)req->buf = 1 ; 141823cd1385SRemy Bohmer } 141923cd1385SRemy Bohmer value = min(wLength, (u16) 1); 142023cd1385SRemy Bohmer break; 142123cd1385SRemy Bohmer 142223cd1385SRemy Bohmer #ifdef DEV_CONFIG_CDC 142323cd1385SRemy Bohmer case USB_CDC_SET_ETHERNET_PACKET_FILTER: 14246142e0aeSVitaly Kuzmichev /* 14256142e0aeSVitaly Kuzmichev * see 6.2.30: no data, wIndex = interface, 142623cd1385SRemy Bohmer * wValue = packet filter bitmap 142723cd1385SRemy Bohmer */ 142823cd1385SRemy Bohmer if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE) 142923cd1385SRemy Bohmer || !cdc_active(dev) 143023cd1385SRemy Bohmer || wLength != 0 143123cd1385SRemy Bohmer || wIndex > 1) 143223cd1385SRemy Bohmer break; 14337de73185SVitaly Kuzmichev debug("packet filter %02x\n", wValue); 143423cd1385SRemy Bohmer dev->cdc_filter = wValue; 143523cd1385SRemy Bohmer value = 0; 143623cd1385SRemy Bohmer break; 143723cd1385SRemy Bohmer 14386142e0aeSVitaly Kuzmichev /* 14396142e0aeSVitaly Kuzmichev * and potentially: 144023cd1385SRemy Bohmer * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS: 144123cd1385SRemy Bohmer * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER: 144223cd1385SRemy Bohmer * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER: 144323cd1385SRemy Bohmer * case USB_CDC_GET_ETHERNET_STATISTIC: 144423cd1385SRemy Bohmer */ 144523cd1385SRemy Bohmer 144623cd1385SRemy Bohmer #endif /* DEV_CONFIG_CDC */ 144723cd1385SRemy Bohmer 14487612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS 14497612a43dSVitaly Kuzmichev /* 14507612a43dSVitaly Kuzmichev * RNDIS uses the CDC command encapsulation mechanism to implement 14517612a43dSVitaly Kuzmichev * an RPC scheme, with much getting/setting of attributes by OID. 14527612a43dSVitaly Kuzmichev */ 14537612a43dSVitaly Kuzmichev case USB_CDC_SEND_ENCAPSULATED_COMMAND: 14547612a43dSVitaly Kuzmichev if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE) 14557612a43dSVitaly Kuzmichev || !rndis_active(dev) 14567612a43dSVitaly Kuzmichev || wLength > USB_BUFSIZ 14577612a43dSVitaly Kuzmichev || wValue 14587612a43dSVitaly Kuzmichev || rndis_control_intf.bInterfaceNumber 14597612a43dSVitaly Kuzmichev != wIndex) 14607612a43dSVitaly Kuzmichev break; 14617612a43dSVitaly Kuzmichev /* read the request, then process it */ 14627612a43dSVitaly Kuzmichev value = wLength; 14637612a43dSVitaly Kuzmichev req->complete = rndis_command_complete; 14647612a43dSVitaly Kuzmichev /* later, rndis_control_ack () sends a notification */ 14657612a43dSVitaly Kuzmichev break; 14667612a43dSVitaly Kuzmichev 14677612a43dSVitaly Kuzmichev case USB_CDC_GET_ENCAPSULATED_RESPONSE: 14687612a43dSVitaly Kuzmichev if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE) 14697612a43dSVitaly Kuzmichev == ctrl->bRequestType 14707612a43dSVitaly Kuzmichev && rndis_active(dev) 14717612a43dSVitaly Kuzmichev /* && wLength >= 0x0400 */ 14727612a43dSVitaly Kuzmichev && !wValue 14737612a43dSVitaly Kuzmichev && rndis_control_intf.bInterfaceNumber 14747612a43dSVitaly Kuzmichev == wIndex) { 14757612a43dSVitaly Kuzmichev u8 *buf; 14767612a43dSVitaly Kuzmichev u32 n; 14777612a43dSVitaly Kuzmichev 14787612a43dSVitaly Kuzmichev /* return the result */ 14797612a43dSVitaly Kuzmichev buf = rndis_get_next_response(dev->rndis_config, &n); 14807612a43dSVitaly Kuzmichev if (buf) { 14817612a43dSVitaly Kuzmichev memcpy(req->buf, buf, n); 14827612a43dSVitaly Kuzmichev req->complete = rndis_response_complete; 14837612a43dSVitaly Kuzmichev rndis_free_response(dev->rndis_config, buf); 14847612a43dSVitaly Kuzmichev value = n; 14857612a43dSVitaly Kuzmichev } 14867612a43dSVitaly Kuzmichev /* else stalls ... spec says to avoid that */ 14877612a43dSVitaly Kuzmichev } 14887612a43dSVitaly Kuzmichev break; 14897612a43dSVitaly Kuzmichev #endif /* RNDIS */ 14907612a43dSVitaly Kuzmichev 149123cd1385SRemy Bohmer default: 14927de73185SVitaly Kuzmichev debug("unknown control req%02x.%02x v%04x i%04x l%d\n", 149323cd1385SRemy Bohmer ctrl->bRequestType, ctrl->bRequest, 149423cd1385SRemy Bohmer wValue, wIndex, wLength); 149523cd1385SRemy Bohmer } 149623cd1385SRemy Bohmer 149723cd1385SRemy Bohmer /* respond with data transfer before status phase? */ 149823cd1385SRemy Bohmer if (value >= 0) { 14997de73185SVitaly Kuzmichev debug("respond with data transfer before status phase\n"); 150023cd1385SRemy Bohmer req->length = value; 150123cd1385SRemy Bohmer req->zero = value < wLength 150223cd1385SRemy Bohmer && (value % gadget->ep0->maxpacket) == 0; 150323cd1385SRemy Bohmer value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC); 150423cd1385SRemy Bohmer if (value < 0) { 15057de73185SVitaly Kuzmichev debug("ep_queue --> %d\n", value); 150623cd1385SRemy Bohmer req->status = 0; 150723cd1385SRemy Bohmer eth_setup_complete(gadget->ep0, req); 150823cd1385SRemy Bohmer } 150923cd1385SRemy Bohmer } 151023cd1385SRemy Bohmer 151123cd1385SRemy Bohmer /* host either stalls (value < 0) or reports success */ 151223cd1385SRemy Bohmer return value; 151323cd1385SRemy Bohmer } 151423cd1385SRemy Bohmer 151523cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 151623cd1385SRemy Bohmer 151723cd1385SRemy Bohmer static void rx_complete(struct usb_ep *ep, struct usb_request *req); 151823cd1385SRemy Bohmer 15196142e0aeSVitaly Kuzmichev static int rx_submit(struct eth_dev *dev, struct usb_request *req, 152023cd1385SRemy Bohmer gfp_t gfp_flags) 152123cd1385SRemy Bohmer { 152223cd1385SRemy Bohmer int retval = -ENOMEM; 152323cd1385SRemy Bohmer size_t size; 152423cd1385SRemy Bohmer 15256142e0aeSVitaly Kuzmichev /* 15266142e0aeSVitaly Kuzmichev * Padding up to RX_EXTRA handles minor disagreements with host. 152723cd1385SRemy Bohmer * Normally we use the USB "terminate on short read" convention; 152823cd1385SRemy Bohmer * so allow up to (N*maxpacket), since that memory is normally 152923cd1385SRemy Bohmer * already allocated. Some hardware doesn't deal well with short 153023cd1385SRemy Bohmer * reads (e.g. DMA must be N*maxpacket), so for now don't trim a 153123cd1385SRemy Bohmer * byte off the end (to force hardware errors on overflow). 15327612a43dSVitaly Kuzmichev * 15337612a43dSVitaly Kuzmichev * RNDIS uses internal framing, and explicitly allows senders to 15347612a43dSVitaly Kuzmichev * pad to end-of-packet. That's potentially nice for speed, 15357612a43dSVitaly Kuzmichev * but means receivers can't recover synch on their own. 153623cd1385SRemy Bohmer */ 153723cd1385SRemy Bohmer 15387de73185SVitaly Kuzmichev debug("%s\n", __func__); 153923cd1385SRemy Bohmer 154023cd1385SRemy Bohmer size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA); 154123cd1385SRemy Bohmer size += dev->out_ep->maxpacket - 1; 15427612a43dSVitaly Kuzmichev if (rndis_active(dev)) 15437612a43dSVitaly Kuzmichev size += sizeof(struct rndis_packet_msg_type); 154423cd1385SRemy Bohmer size -= size % dev->out_ep->maxpacket; 154523cd1385SRemy Bohmer 15466142e0aeSVitaly Kuzmichev /* 15476142e0aeSVitaly Kuzmichev * Some platforms perform better when IP packets are aligned, 15487612a43dSVitaly Kuzmichev * but on at least one, checksumming fails otherwise. Note: 15497612a43dSVitaly Kuzmichev * RNDIS headers involve variable numbers of LE32 values. 155023cd1385SRemy Bohmer */ 155123cd1385SRemy Bohmer 155223cd1385SRemy Bohmer req->buf = (u8 *) NetRxPackets[0]; 155323cd1385SRemy Bohmer req->length = size; 155423cd1385SRemy Bohmer req->complete = rx_complete; 155523cd1385SRemy Bohmer 155623cd1385SRemy Bohmer retval = usb_ep_queue(dev->out_ep, req, gfp_flags); 155723cd1385SRemy Bohmer 15586142e0aeSVitaly Kuzmichev if (retval) 15597de73185SVitaly Kuzmichev error("rx submit --> %d", retval); 15606142e0aeSVitaly Kuzmichev 156123cd1385SRemy Bohmer return retval; 156223cd1385SRemy Bohmer } 156323cd1385SRemy Bohmer 156423cd1385SRemy Bohmer static void rx_complete(struct usb_ep *ep, struct usb_request *req) 156523cd1385SRemy Bohmer { 156623cd1385SRemy Bohmer struct eth_dev *dev = ep->driver_data; 156723cd1385SRemy Bohmer 15687de73185SVitaly Kuzmichev debug("%s: status %d\n", __func__, req->status); 1569c85d70efSVitaly Kuzmichev switch (req->status) { 1570c85d70efSVitaly Kuzmichev /* normal completion */ 1571c85d70efSVitaly Kuzmichev case 0: 15727612a43dSVitaly Kuzmichev if (rndis_active(dev)) { 15737612a43dSVitaly Kuzmichev /* we know MaxPacketsPerTransfer == 1 here */ 15747612a43dSVitaly Kuzmichev int length = rndis_rm_hdr(req->buf, req->actual); 15757612a43dSVitaly Kuzmichev if (length < 0) 15767612a43dSVitaly Kuzmichev goto length_err; 15777612a43dSVitaly Kuzmichev req->length -= length; 15787612a43dSVitaly Kuzmichev req->actual -= length; 15797612a43dSVitaly Kuzmichev } 15807612a43dSVitaly Kuzmichev if (req->actual < ETH_HLEN || ETH_FRAME_LEN < req->actual) { 15817612a43dSVitaly Kuzmichev length_err: 15827612a43dSVitaly Kuzmichev dev->stats.rx_errors++; 15837612a43dSVitaly Kuzmichev dev->stats.rx_length_errors++; 15847612a43dSVitaly Kuzmichev debug("rx length %d\n", req->length); 15857612a43dSVitaly Kuzmichev break; 15867612a43dSVitaly Kuzmichev } 15877612a43dSVitaly Kuzmichev 1588c85d70efSVitaly Kuzmichev dev->stats.rx_packets++; 1589c85d70efSVitaly Kuzmichev dev->stats.rx_bytes += req->length; 1590c85d70efSVitaly Kuzmichev break; 1591c85d70efSVitaly Kuzmichev 1592c85d70efSVitaly Kuzmichev /* software-driven interface shutdown */ 1593c85d70efSVitaly Kuzmichev case -ECONNRESET: /* unlink */ 1594c85d70efSVitaly Kuzmichev case -ESHUTDOWN: /* disconnect etc */ 1595c85d70efSVitaly Kuzmichev /* for hardware automagic (such as pxa) */ 1596c85d70efSVitaly Kuzmichev case -ECONNABORTED: /* endpoint reset */ 1597c85d70efSVitaly Kuzmichev break; 1598c85d70efSVitaly Kuzmichev 1599c85d70efSVitaly Kuzmichev /* data overrun */ 1600c85d70efSVitaly Kuzmichev case -EOVERFLOW: 1601c85d70efSVitaly Kuzmichev dev->stats.rx_over_errors++; 1602c85d70efSVitaly Kuzmichev /* FALLTHROUGH */ 1603c85d70efSVitaly Kuzmichev default: 1604c85d70efSVitaly Kuzmichev dev->stats.rx_errors++; 1605c85d70efSVitaly Kuzmichev break; 1606c85d70efSVitaly Kuzmichev } 160723cd1385SRemy Bohmer 160823cd1385SRemy Bohmer packet_received = 1; 160923cd1385SRemy Bohmer } 161023cd1385SRemy Bohmer 161123cd1385SRemy Bohmer static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags) 161223cd1385SRemy Bohmer { 161323cd1385SRemy Bohmer 161423cd1385SRemy Bohmer dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0); 161523cd1385SRemy Bohmer 161623cd1385SRemy Bohmer if (!dev->tx_req) 1617ac5d32d1SVitaly Kuzmichev goto fail1; 161823cd1385SRemy Bohmer 161923cd1385SRemy Bohmer dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0); 162023cd1385SRemy Bohmer 162123cd1385SRemy Bohmer if (!dev->rx_req) 1622ac5d32d1SVitaly Kuzmichev goto fail2; 162323cd1385SRemy Bohmer 162423cd1385SRemy Bohmer return 0; 162523cd1385SRemy Bohmer 1626ac5d32d1SVitaly Kuzmichev fail2: 1627ac5d32d1SVitaly Kuzmichev usb_ep_free_request(dev->in_ep, dev->tx_req); 1628ac5d32d1SVitaly Kuzmichev fail1: 16297de73185SVitaly Kuzmichev error("can't alloc requests"); 163023cd1385SRemy Bohmer return -1; 163123cd1385SRemy Bohmer } 163223cd1385SRemy Bohmer 163323cd1385SRemy Bohmer static void tx_complete(struct usb_ep *ep, struct usb_request *req) 163423cd1385SRemy Bohmer { 1635c85d70efSVitaly Kuzmichev struct eth_dev *dev = ep->driver_data; 1636c85d70efSVitaly Kuzmichev 16377de73185SVitaly Kuzmichev debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok"); 1638c85d70efSVitaly Kuzmichev switch (req->status) { 1639c85d70efSVitaly Kuzmichev default: 1640c85d70efSVitaly Kuzmichev dev->stats.tx_errors++; 1641c85d70efSVitaly Kuzmichev debug("tx err %d\n", req->status); 1642c85d70efSVitaly Kuzmichev /* FALLTHROUGH */ 1643c85d70efSVitaly Kuzmichev case -ECONNRESET: /* unlink */ 1644c85d70efSVitaly Kuzmichev case -ESHUTDOWN: /* disconnect etc */ 1645c85d70efSVitaly Kuzmichev break; 1646c85d70efSVitaly Kuzmichev case 0: 1647c85d70efSVitaly Kuzmichev dev->stats.tx_bytes += req->length; 1648c85d70efSVitaly Kuzmichev } 1649c85d70efSVitaly Kuzmichev dev->stats.tx_packets++; 1650c85d70efSVitaly Kuzmichev 165123cd1385SRemy Bohmer packet_sent = 1; 165223cd1385SRemy Bohmer } 165323cd1385SRemy Bohmer 165423cd1385SRemy Bohmer static inline int eth_is_promisc(struct eth_dev *dev) 165523cd1385SRemy Bohmer { 165623cd1385SRemy Bohmer /* no filters for the CDC subset; always promisc */ 165723cd1385SRemy Bohmer if (subset_active(dev)) 165823cd1385SRemy Bohmer return 1; 165923cd1385SRemy Bohmer return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS; 166023cd1385SRemy Bohmer } 166123cd1385SRemy Bohmer 166223cd1385SRemy Bohmer #if 0 166323cd1385SRemy Bohmer static int eth_start_xmit (struct sk_buff *skb, struct net_device *net) 166423cd1385SRemy Bohmer { 166523cd1385SRemy Bohmer struct eth_dev *dev = netdev_priv(net); 166623cd1385SRemy Bohmer int length = skb->len; 166723cd1385SRemy Bohmer int retval; 166823cd1385SRemy Bohmer struct usb_request *req = NULL; 166923cd1385SRemy Bohmer unsigned long flags; 167023cd1385SRemy Bohmer 167123cd1385SRemy Bohmer /* apply outgoing CDC or RNDIS filters */ 167223cd1385SRemy Bohmer if (!eth_is_promisc (dev)) { 167323cd1385SRemy Bohmer u8 *dest = skb->data; 167423cd1385SRemy Bohmer 167523cd1385SRemy Bohmer if (is_multicast_ether_addr(dest)) { 167623cd1385SRemy Bohmer u16 type; 167723cd1385SRemy Bohmer 167823cd1385SRemy Bohmer /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host 167923cd1385SRemy Bohmer * SET_ETHERNET_MULTICAST_FILTERS requests 168023cd1385SRemy Bohmer */ 168123cd1385SRemy Bohmer if (is_broadcast_ether_addr(dest)) 168223cd1385SRemy Bohmer type = USB_CDC_PACKET_TYPE_BROADCAST; 168323cd1385SRemy Bohmer else 168423cd1385SRemy Bohmer type = USB_CDC_PACKET_TYPE_ALL_MULTICAST; 168523cd1385SRemy Bohmer if (!(dev->cdc_filter & type)) { 168623cd1385SRemy Bohmer dev_kfree_skb_any (skb); 168723cd1385SRemy Bohmer return 0; 168823cd1385SRemy Bohmer } 168923cd1385SRemy Bohmer } 169023cd1385SRemy Bohmer /* ignores USB_CDC_PACKET_TYPE_DIRECTED */ 169123cd1385SRemy Bohmer } 169223cd1385SRemy Bohmer 169323cd1385SRemy Bohmer spin_lock_irqsave(&dev->req_lock, flags); 169423cd1385SRemy Bohmer /* 169523cd1385SRemy Bohmer * this freelist can be empty if an interrupt triggered disconnect() 169623cd1385SRemy Bohmer * and reconfigured the gadget (shutting down this queue) after the 169723cd1385SRemy Bohmer * network stack decided to xmit but before we got the spinlock. 169823cd1385SRemy Bohmer */ 169923cd1385SRemy Bohmer if (list_empty(&dev->tx_reqs)) { 170023cd1385SRemy Bohmer spin_unlock_irqrestore(&dev->req_lock, flags); 170123cd1385SRemy Bohmer return 1; 170223cd1385SRemy Bohmer } 170323cd1385SRemy Bohmer 170423cd1385SRemy Bohmer req = container_of (dev->tx_reqs.next, struct usb_request, list); 170523cd1385SRemy Bohmer list_del (&req->list); 170623cd1385SRemy Bohmer 170723cd1385SRemy Bohmer /* temporarily stop TX queue when the freelist empties */ 170823cd1385SRemy Bohmer if (list_empty (&dev->tx_reqs)) 170923cd1385SRemy Bohmer netif_stop_queue (net); 171023cd1385SRemy Bohmer spin_unlock_irqrestore(&dev->req_lock, flags); 171123cd1385SRemy Bohmer 171223cd1385SRemy Bohmer /* no buffer copies needed, unless the network stack did it 171323cd1385SRemy Bohmer * or the hardware can't use skb buffers. 171423cd1385SRemy Bohmer * or there's not enough space for any RNDIS headers we need 171523cd1385SRemy Bohmer */ 171623cd1385SRemy Bohmer if (rndis_active(dev)) { 171723cd1385SRemy Bohmer struct sk_buff *skb_rndis; 171823cd1385SRemy Bohmer 171923cd1385SRemy Bohmer skb_rndis = skb_realloc_headroom (skb, 172023cd1385SRemy Bohmer sizeof (struct rndis_packet_msg_type)); 172123cd1385SRemy Bohmer if (!skb_rndis) 172223cd1385SRemy Bohmer goto drop; 172323cd1385SRemy Bohmer 172423cd1385SRemy Bohmer dev_kfree_skb_any (skb); 172523cd1385SRemy Bohmer skb = skb_rndis; 172623cd1385SRemy Bohmer rndis_add_hdr (skb); 172723cd1385SRemy Bohmer length = skb->len; 172823cd1385SRemy Bohmer } 172923cd1385SRemy Bohmer req->buf = skb->data; 173023cd1385SRemy Bohmer req->context = skb; 173123cd1385SRemy Bohmer req->complete = tx_complete; 173223cd1385SRemy Bohmer 173323cd1385SRemy Bohmer /* use zlp framing on tx for strict CDC-Ether conformance, 173423cd1385SRemy Bohmer * though any robust network rx path ignores extra padding. 173523cd1385SRemy Bohmer * and some hardware doesn't like to write zlps. 173623cd1385SRemy Bohmer */ 173723cd1385SRemy Bohmer req->zero = 1; 173823cd1385SRemy Bohmer if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0) 173923cd1385SRemy Bohmer length++; 174023cd1385SRemy Bohmer 174123cd1385SRemy Bohmer req->length = length; 174223cd1385SRemy Bohmer 174323cd1385SRemy Bohmer /* throttle highspeed IRQ rate back slightly */ 174423cd1385SRemy Bohmer if (gadget_is_dualspeed(dev->gadget)) 174523cd1385SRemy Bohmer req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH) 174623cd1385SRemy Bohmer ? ((atomic_read(&dev->tx_qlen) % qmult) != 0) 174723cd1385SRemy Bohmer : 0; 174823cd1385SRemy Bohmer 174923cd1385SRemy Bohmer retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC); 175023cd1385SRemy Bohmer switch (retval) { 175123cd1385SRemy Bohmer default: 175223cd1385SRemy Bohmer DEBUG (dev, "tx queue err %d\n", retval); 175323cd1385SRemy Bohmer break; 175423cd1385SRemy Bohmer case 0: 175523cd1385SRemy Bohmer net->trans_start = jiffies; 175623cd1385SRemy Bohmer atomic_inc (&dev->tx_qlen); 175723cd1385SRemy Bohmer } 175823cd1385SRemy Bohmer 175923cd1385SRemy Bohmer if (retval) { 176023cd1385SRemy Bohmer drop: 176123cd1385SRemy Bohmer dev->stats.tx_dropped++; 176223cd1385SRemy Bohmer dev_kfree_skb_any (skb); 176323cd1385SRemy Bohmer spin_lock_irqsave(&dev->req_lock, flags); 176423cd1385SRemy Bohmer if (list_empty (&dev->tx_reqs)) 176523cd1385SRemy Bohmer netif_start_queue (net); 176623cd1385SRemy Bohmer list_add (&req->list, &dev->tx_reqs); 176723cd1385SRemy Bohmer spin_unlock_irqrestore(&dev->req_lock, flags); 176823cd1385SRemy Bohmer } 176923cd1385SRemy Bohmer return 0; 177023cd1385SRemy Bohmer } 177123cd1385SRemy Bohmer 177223cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 177323cd1385SRemy Bohmer #endif 177423cd1385SRemy Bohmer 177523cd1385SRemy Bohmer static void eth_unbind(struct usb_gadget *gadget) 177623cd1385SRemy Bohmer { 177723cd1385SRemy Bohmer struct eth_dev *dev = get_gadget_data(gadget); 177823cd1385SRemy Bohmer 17797de73185SVitaly Kuzmichev debug("%s...\n", __func__); 17807612a43dSVitaly Kuzmichev rndis_deregister(dev->rndis_config); 17817612a43dSVitaly Kuzmichev rndis_exit(); 178223cd1385SRemy Bohmer 17830129e327SVitaly Kuzmichev /* we've already been disconnected ... no i/o is active */ 17840129e327SVitaly Kuzmichev if (dev->req) { 17850129e327SVitaly Kuzmichev usb_ep_free_request(gadget->ep0, dev->req); 17860129e327SVitaly Kuzmichev dev->req = NULL; 17870129e327SVitaly Kuzmichev } 178823cd1385SRemy Bohmer if (dev->stat_req) { 178923cd1385SRemy Bohmer usb_ep_free_request(dev->status_ep, dev->stat_req); 179023cd1385SRemy Bohmer dev->stat_req = NULL; 179123cd1385SRemy Bohmer } 179223cd1385SRemy Bohmer 179323cd1385SRemy Bohmer if (dev->tx_req) { 179423cd1385SRemy Bohmer usb_ep_free_request(dev->in_ep, dev->tx_req); 179523cd1385SRemy Bohmer dev->tx_req = NULL; 179623cd1385SRemy Bohmer } 179723cd1385SRemy Bohmer 179823cd1385SRemy Bohmer if (dev->rx_req) { 17990129e327SVitaly Kuzmichev usb_ep_free_request(dev->out_ep, dev->rx_req); 180023cd1385SRemy Bohmer dev->rx_req = NULL; 180123cd1385SRemy Bohmer } 180223cd1385SRemy Bohmer 180323cd1385SRemy Bohmer /* unregister_netdev (dev->net);*/ 180423cd1385SRemy Bohmer /* free_netdev(dev->net);*/ 180523cd1385SRemy Bohmer 1806988ee3e3SLei Wen dev->gadget = NULL; 180723cd1385SRemy Bohmer set_gadget_data(gadget, NULL); 180823cd1385SRemy Bohmer } 180923cd1385SRemy Bohmer 181023cd1385SRemy Bohmer static void eth_disconnect(struct usb_gadget *gadget) 181123cd1385SRemy Bohmer { 181223cd1385SRemy Bohmer eth_reset_config(get_gadget_data(gadget)); 18137612a43dSVitaly Kuzmichev /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */ 181423cd1385SRemy Bohmer } 181523cd1385SRemy Bohmer 181623cd1385SRemy Bohmer static void eth_suspend(struct usb_gadget *gadget) 181723cd1385SRemy Bohmer { 181823cd1385SRemy Bohmer /* Not used */ 181923cd1385SRemy Bohmer } 182023cd1385SRemy Bohmer 182123cd1385SRemy Bohmer static void eth_resume(struct usb_gadget *gadget) 182223cd1385SRemy Bohmer { 182323cd1385SRemy Bohmer /* Not used */ 182423cd1385SRemy Bohmer } 182523cd1385SRemy Bohmer 182623cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 182723cd1385SRemy Bohmer 18287612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS 18297612a43dSVitaly Kuzmichev 18307612a43dSVitaly Kuzmichev /* 18317612a43dSVitaly Kuzmichev * The interrupt endpoint is used in RNDIS to notify the host when messages 18327612a43dSVitaly Kuzmichev * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT 18337612a43dSVitaly Kuzmichev * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even 18347612a43dSVitaly Kuzmichev * REMOTE_NDIS_KEEPALIVE_MSG. 18357612a43dSVitaly Kuzmichev * 18367612a43dSVitaly Kuzmichev * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and 18377612a43dSVitaly Kuzmichev * normally just one notification will be queued. 18387612a43dSVitaly Kuzmichev */ 18397612a43dSVitaly Kuzmichev 18407612a43dSVitaly Kuzmichev static void rndis_control_ack_complete(struct usb_ep *ep, 18417612a43dSVitaly Kuzmichev struct usb_request *req) 18427612a43dSVitaly Kuzmichev { 18437612a43dSVitaly Kuzmichev struct eth_dev *dev = ep->driver_data; 18447612a43dSVitaly Kuzmichev 18457612a43dSVitaly Kuzmichev debug("%s...\n", __func__); 18467612a43dSVitaly Kuzmichev if (req->status || req->actual != req->length) 18477612a43dSVitaly Kuzmichev debug("rndis control ack complete --> %d, %d/%d\n", 18487612a43dSVitaly Kuzmichev req->status, req->actual, req->length); 18497612a43dSVitaly Kuzmichev 18507612a43dSVitaly Kuzmichev if (!l_ethdev.network_started) { 18517612a43dSVitaly Kuzmichev if (rndis_get_state(dev->rndis_config) 18527612a43dSVitaly Kuzmichev == RNDIS_DATA_INITIALIZED) { 18537612a43dSVitaly Kuzmichev l_ethdev.network_started = 1; 18547612a43dSVitaly Kuzmichev printf("USB RNDIS network up!\n"); 18557612a43dSVitaly Kuzmichev } 18567612a43dSVitaly Kuzmichev } 18577612a43dSVitaly Kuzmichev 18587612a43dSVitaly Kuzmichev req->context = NULL; 18597612a43dSVitaly Kuzmichev 18607612a43dSVitaly Kuzmichev if (req != dev->stat_req) 18617612a43dSVitaly Kuzmichev usb_ep_free_request(ep, req); 18627612a43dSVitaly Kuzmichev } 18637612a43dSVitaly Kuzmichev 18647612a43dSVitaly Kuzmichev static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32)))); 18657612a43dSVitaly Kuzmichev 18667612a43dSVitaly Kuzmichev static int rndis_control_ack(struct eth_device *net) 18677612a43dSVitaly Kuzmichev { 18687612a43dSVitaly Kuzmichev struct eth_dev *dev = &l_ethdev; 18697612a43dSVitaly Kuzmichev int length; 18707612a43dSVitaly Kuzmichev struct usb_request *resp = dev->stat_req; 18717612a43dSVitaly Kuzmichev 18727612a43dSVitaly Kuzmichev /* in case RNDIS calls this after disconnect */ 18737612a43dSVitaly Kuzmichev if (!dev->status) { 18747612a43dSVitaly Kuzmichev debug("status ENODEV\n"); 18757612a43dSVitaly Kuzmichev return -ENODEV; 18767612a43dSVitaly Kuzmichev } 18777612a43dSVitaly Kuzmichev 18787612a43dSVitaly Kuzmichev /* in case queue length > 1 */ 18797612a43dSVitaly Kuzmichev if (resp->context) { 18807612a43dSVitaly Kuzmichev resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC); 18817612a43dSVitaly Kuzmichev if (!resp) 18827612a43dSVitaly Kuzmichev return -ENOMEM; 18837612a43dSVitaly Kuzmichev resp->buf = rndis_resp_buf; 18847612a43dSVitaly Kuzmichev } 18857612a43dSVitaly Kuzmichev 18867612a43dSVitaly Kuzmichev /* 18877612a43dSVitaly Kuzmichev * Send RNDIS RESPONSE_AVAILABLE notification; 18887612a43dSVitaly Kuzmichev * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too 18897612a43dSVitaly Kuzmichev */ 18907612a43dSVitaly Kuzmichev resp->length = 8; 18917612a43dSVitaly Kuzmichev resp->complete = rndis_control_ack_complete; 18927612a43dSVitaly Kuzmichev resp->context = dev; 18937612a43dSVitaly Kuzmichev 18947612a43dSVitaly Kuzmichev *((__le32 *) resp->buf) = __constant_cpu_to_le32(1); 18957612a43dSVitaly Kuzmichev *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0); 18967612a43dSVitaly Kuzmichev 18977612a43dSVitaly Kuzmichev length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC); 18987612a43dSVitaly Kuzmichev if (length < 0) { 18997612a43dSVitaly Kuzmichev resp->status = 0; 19007612a43dSVitaly Kuzmichev rndis_control_ack_complete(dev->status_ep, resp); 19017612a43dSVitaly Kuzmichev } 19027612a43dSVitaly Kuzmichev 19037612a43dSVitaly Kuzmichev return 0; 19047612a43dSVitaly Kuzmichev } 19057612a43dSVitaly Kuzmichev 19067612a43dSVitaly Kuzmichev #else 19077612a43dSVitaly Kuzmichev 19087612a43dSVitaly Kuzmichev #define rndis_control_ack NULL 19097612a43dSVitaly Kuzmichev 19107612a43dSVitaly Kuzmichev #endif /* RNDIS */ 19117612a43dSVitaly Kuzmichev 19127612a43dSVitaly Kuzmichev static void eth_start(struct eth_dev *dev, gfp_t gfp_flags) 19137612a43dSVitaly Kuzmichev { 19147612a43dSVitaly Kuzmichev if (rndis_active(dev)) { 19157612a43dSVitaly Kuzmichev rndis_set_param_medium(dev->rndis_config, 19167612a43dSVitaly Kuzmichev NDIS_MEDIUM_802_3, 19177612a43dSVitaly Kuzmichev BITRATE(dev->gadget)/100); 19187612a43dSVitaly Kuzmichev rndis_signal_connect(dev->rndis_config); 19197612a43dSVitaly Kuzmichev } 19207612a43dSVitaly Kuzmichev } 19217612a43dSVitaly Kuzmichev 19227612a43dSVitaly Kuzmichev static int eth_stop(struct eth_dev *dev) 19237612a43dSVitaly Kuzmichev { 1924*e4ae6660SVitaly Kuzmichev #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT 1925*e4ae6660SVitaly Kuzmichev unsigned long ts; 1926*e4ae6660SVitaly Kuzmichev unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */ 1927*e4ae6660SVitaly Kuzmichev #endif 1928*e4ae6660SVitaly Kuzmichev 19297612a43dSVitaly Kuzmichev if (rndis_active(dev)) { 19307612a43dSVitaly Kuzmichev rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0); 19317612a43dSVitaly Kuzmichev rndis_signal_disconnect(dev->rndis_config); 19327612a43dSVitaly Kuzmichev 1933*e4ae6660SVitaly Kuzmichev #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT 1934*e4ae6660SVitaly Kuzmichev /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */ 1935*e4ae6660SVitaly Kuzmichev ts = get_timer(0); 1936*e4ae6660SVitaly Kuzmichev while (get_timer(ts) < timeout) 1937*e4ae6660SVitaly Kuzmichev usb_gadget_handle_interrupts(); 1938*e4ae6660SVitaly Kuzmichev #endif 1939*e4ae6660SVitaly Kuzmichev 19407612a43dSVitaly Kuzmichev rndis_uninit(dev->rndis_config); 19417612a43dSVitaly Kuzmichev dev->rndis = 0; 19427612a43dSVitaly Kuzmichev } 19437612a43dSVitaly Kuzmichev 19447612a43dSVitaly Kuzmichev return 0; 19457612a43dSVitaly Kuzmichev } 19467612a43dSVitaly Kuzmichev 19477612a43dSVitaly Kuzmichev /*-------------------------------------------------------------------------*/ 19487612a43dSVitaly Kuzmichev 194923cd1385SRemy Bohmer static int is_eth_addr_valid(char *str) 195023cd1385SRemy Bohmer { 195123cd1385SRemy Bohmer if (strlen(str) == 17) { 195223cd1385SRemy Bohmer int i; 195323cd1385SRemy Bohmer char *p, *q; 195423cd1385SRemy Bohmer uchar ea[6]; 195523cd1385SRemy Bohmer 195623cd1385SRemy Bohmer /* see if it looks like an ethernet address */ 195723cd1385SRemy Bohmer 195823cd1385SRemy Bohmer p = str; 195923cd1385SRemy Bohmer 196023cd1385SRemy Bohmer for (i = 0; i < 6; i++) { 196123cd1385SRemy Bohmer char term = (i == 5 ? '\0' : ':'); 196223cd1385SRemy Bohmer 196323cd1385SRemy Bohmer ea[i] = simple_strtol(p, &q, 16); 196423cd1385SRemy Bohmer 196523cd1385SRemy Bohmer if ((q - p) != 2 || *q++ != term) 196623cd1385SRemy Bohmer break; 196723cd1385SRemy Bohmer 196823cd1385SRemy Bohmer p = q; 196923cd1385SRemy Bohmer } 197023cd1385SRemy Bohmer 197123cd1385SRemy Bohmer if (i == 6) /* it looks ok */ 197223cd1385SRemy Bohmer return 1; 197323cd1385SRemy Bohmer } 197423cd1385SRemy Bohmer return 0; 197523cd1385SRemy Bohmer } 197623cd1385SRemy Bohmer 197723cd1385SRemy Bohmer static u8 nibble(unsigned char c) 197823cd1385SRemy Bohmer { 197923cd1385SRemy Bohmer if (likely(isdigit(c))) 198023cd1385SRemy Bohmer return c - '0'; 198123cd1385SRemy Bohmer c = toupper(c); 198223cd1385SRemy Bohmer if (likely(isxdigit(c))) 198323cd1385SRemy Bohmer return 10 + c - 'A'; 198423cd1385SRemy Bohmer return 0; 198523cd1385SRemy Bohmer } 198623cd1385SRemy Bohmer 198723cd1385SRemy Bohmer static int get_ether_addr(const char *str, u8 *dev_addr) 198823cd1385SRemy Bohmer { 198923cd1385SRemy Bohmer if (str) { 199023cd1385SRemy Bohmer unsigned i; 199123cd1385SRemy Bohmer 199223cd1385SRemy Bohmer for (i = 0; i < 6; i++) { 199323cd1385SRemy Bohmer unsigned char num; 199423cd1385SRemy Bohmer 199523cd1385SRemy Bohmer if ((*str == '.') || (*str == ':')) 199623cd1385SRemy Bohmer str++; 199723cd1385SRemy Bohmer num = nibble(*str++) << 4; 199823cd1385SRemy Bohmer num |= (nibble(*str++)); 199923cd1385SRemy Bohmer dev_addr[i] = num; 200023cd1385SRemy Bohmer } 200123cd1385SRemy Bohmer if (is_valid_ether_addr(dev_addr)) 200223cd1385SRemy Bohmer return 0; 200323cd1385SRemy Bohmer } 200423cd1385SRemy Bohmer return 1; 200523cd1385SRemy Bohmer } 200623cd1385SRemy Bohmer 200723cd1385SRemy Bohmer static int eth_bind(struct usb_gadget *gadget) 200823cd1385SRemy Bohmer { 200923cd1385SRemy Bohmer struct eth_dev *dev = &l_ethdev; 20107612a43dSVitaly Kuzmichev u8 cdc = 1, zlp = 1, rndis = 1; 201123cd1385SRemy Bohmer struct usb_ep *in_ep, *out_ep, *status_ep = NULL; 20127612a43dSVitaly Kuzmichev int status = -ENOMEM; 201323cd1385SRemy Bohmer int gcnum; 201423cd1385SRemy Bohmer u8 tmp[7]; 201523cd1385SRemy Bohmer 201623cd1385SRemy Bohmer /* these flags are only ever cleared; compiler take note */ 201723cd1385SRemy Bohmer #ifndef DEV_CONFIG_CDC 201823cd1385SRemy Bohmer cdc = 0; 201923cd1385SRemy Bohmer #endif 20207612a43dSVitaly Kuzmichev #ifndef CONFIG_USB_ETH_RNDIS 20217612a43dSVitaly Kuzmichev rndis = 0; 20227612a43dSVitaly Kuzmichev #endif 20236142e0aeSVitaly Kuzmichev /* 20246142e0aeSVitaly Kuzmichev * Because most host side USB stacks handle CDC Ethernet, that 202523cd1385SRemy Bohmer * standard protocol is _strongly_ preferred for interop purposes. 202623cd1385SRemy Bohmer * (By everyone except Microsoft.) 202723cd1385SRemy Bohmer */ 202823cd1385SRemy Bohmer if (gadget_is_pxa(gadget)) { 202923cd1385SRemy Bohmer /* pxa doesn't support altsettings */ 203023cd1385SRemy Bohmer cdc = 0; 203123cd1385SRemy Bohmer } else if (gadget_is_musbhdrc(gadget)) { 203223cd1385SRemy Bohmer /* reduce tx dma overhead by avoiding special cases */ 203323cd1385SRemy Bohmer zlp = 0; 203423cd1385SRemy Bohmer } else if (gadget_is_sh(gadget)) { 203523cd1385SRemy Bohmer /* sh doesn't support multiple interfaces or configs */ 203623cd1385SRemy Bohmer cdc = 0; 20377612a43dSVitaly Kuzmichev rndis = 0; 203823cd1385SRemy Bohmer } else if (gadget_is_sa1100(gadget)) { 203923cd1385SRemy Bohmer /* hardware can't write zlps */ 204023cd1385SRemy Bohmer zlp = 0; 20416142e0aeSVitaly Kuzmichev /* 20426142e0aeSVitaly Kuzmichev * sa1100 CAN do CDC, without status endpoint ... we use 204323cd1385SRemy Bohmer * non-CDC to be compatible with ARM Linux-2.4 "usb-eth". 204423cd1385SRemy Bohmer */ 204523cd1385SRemy Bohmer cdc = 0; 204623cd1385SRemy Bohmer } 204723cd1385SRemy Bohmer 204823cd1385SRemy Bohmer gcnum = usb_gadget_controller_number(gadget); 204923cd1385SRemy Bohmer if (gcnum >= 0) 205023cd1385SRemy Bohmer device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum); 205123cd1385SRemy Bohmer else { 20526142e0aeSVitaly Kuzmichev /* 20536142e0aeSVitaly Kuzmichev * can't assume CDC works. don't want to default to 205423cd1385SRemy Bohmer * anything less functional on CDC-capable hardware, 205523cd1385SRemy Bohmer * so we fail in this case. 205623cd1385SRemy Bohmer */ 20577de73185SVitaly Kuzmichev error("controller '%s' not recognized", 205823cd1385SRemy Bohmer gadget->name); 205923cd1385SRemy Bohmer return -ENODEV; 206023cd1385SRemy Bohmer } 206123cd1385SRemy Bohmer 20626142e0aeSVitaly Kuzmichev /* 20637612a43dSVitaly Kuzmichev * If there's an RNDIS configuration, that's what Windows wants to 20647612a43dSVitaly Kuzmichev * be using ... so use these product IDs here and in the "linux.inf" 20657612a43dSVitaly Kuzmichev * needed to install MSFT drivers. Current Linux kernels will use 20667612a43dSVitaly Kuzmichev * the second configuration if it's CDC Ethernet, and need some help 20677612a43dSVitaly Kuzmichev * to choose the right configuration otherwise. 20687612a43dSVitaly Kuzmichev */ 20697612a43dSVitaly Kuzmichev if (rndis) { 20707612a43dSVitaly Kuzmichev #if defined(CONFIG_USB_RNDIS_VENDOR_ID) && defined(CONFIG_USB_RNDIS_PRODUCT_ID) 20717612a43dSVitaly Kuzmichev device_desc.idVendor = 20727612a43dSVitaly Kuzmichev __constant_cpu_to_le16(CONFIG_USB_RNDIS_VENDOR_ID); 20737612a43dSVitaly Kuzmichev device_desc.idProduct = 20747612a43dSVitaly Kuzmichev __constant_cpu_to_le16(CONFIG_USB_RNDIS_PRODUCT_ID); 20757612a43dSVitaly Kuzmichev #else 20767612a43dSVitaly Kuzmichev device_desc.idVendor = 20777612a43dSVitaly Kuzmichev __constant_cpu_to_le16(RNDIS_VENDOR_NUM); 20787612a43dSVitaly Kuzmichev device_desc.idProduct = 20797612a43dSVitaly Kuzmichev __constant_cpu_to_le16(RNDIS_PRODUCT_NUM); 20807612a43dSVitaly Kuzmichev #endif 20817612a43dSVitaly Kuzmichev sprintf(product_desc, "RNDIS/%s", driver_desc); 20827612a43dSVitaly Kuzmichev 20837612a43dSVitaly Kuzmichev /* 20846142e0aeSVitaly Kuzmichev * CDC subset ... recognized by Linux since 2.4.10, but Windows 208523cd1385SRemy Bohmer * drivers aren't widely available. (That may be improved by 208623cd1385SRemy Bohmer * supporting one submode of the "SAFE" variant of MDLM.) 208723cd1385SRemy Bohmer */ 20887612a43dSVitaly Kuzmichev } else { 20897612a43dSVitaly Kuzmichev #if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID) 20907612a43dSVitaly Kuzmichev device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID); 20917612a43dSVitaly Kuzmichev device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID); 20927612a43dSVitaly Kuzmichev #else 209323cd1385SRemy Bohmer if (!cdc) { 209423cd1385SRemy Bohmer device_desc.idVendor = 209523cd1385SRemy Bohmer __constant_cpu_to_le16(SIMPLE_VENDOR_NUM); 209623cd1385SRemy Bohmer device_desc.idProduct = 209723cd1385SRemy Bohmer __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM); 209823cd1385SRemy Bohmer } 209923cd1385SRemy Bohmer #endif 21007612a43dSVitaly Kuzmichev } 21017612a43dSVitaly Kuzmichev /* support optional vendor/distro customization */ 210223cd1385SRemy Bohmer if (bcdDevice) 210323cd1385SRemy Bohmer device_desc.bcdDevice = cpu_to_le16(bcdDevice); 210423cd1385SRemy Bohmer if (iManufacturer) 21052e12abe6SVitaly Kuzmichev strlcpy(manufacturer, iManufacturer, sizeof manufacturer); 210623cd1385SRemy Bohmer if (iProduct) 21072e12abe6SVitaly Kuzmichev strlcpy(product_desc, iProduct, sizeof product_desc); 210823cd1385SRemy Bohmer if (iSerialNumber) { 210923cd1385SRemy Bohmer device_desc.iSerialNumber = STRING_SERIALNUMBER, 21102e12abe6SVitaly Kuzmichev strlcpy(serial_number, iSerialNumber, sizeof serial_number); 211123cd1385SRemy Bohmer } 211223cd1385SRemy Bohmer 211323cd1385SRemy Bohmer /* all we really need is bulk IN/OUT */ 211423cd1385SRemy Bohmer usb_ep_autoconfig_reset(gadget); 211523cd1385SRemy Bohmer in_ep = usb_ep_autoconfig(gadget, &fs_source_desc); 211623cd1385SRemy Bohmer if (!in_ep) { 211723cd1385SRemy Bohmer autoconf_fail: 21187de73185SVitaly Kuzmichev error("can't autoconfigure on %s\n", 211923cd1385SRemy Bohmer gadget->name); 212023cd1385SRemy Bohmer return -ENODEV; 212123cd1385SRemy Bohmer } 212223cd1385SRemy Bohmer in_ep->driver_data = in_ep; /* claim */ 212323cd1385SRemy Bohmer 212423cd1385SRemy Bohmer out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc); 212523cd1385SRemy Bohmer if (!out_ep) 212623cd1385SRemy Bohmer goto autoconf_fail; 212723cd1385SRemy Bohmer out_ep->driver_data = out_ep; /* claim */ 212823cd1385SRemy Bohmer 21297612a43dSVitaly Kuzmichev #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS) 21306142e0aeSVitaly Kuzmichev /* 21316142e0aeSVitaly Kuzmichev * CDC Ethernet control interface doesn't require a status endpoint. 213223cd1385SRemy Bohmer * Since some hosts expect one, try to allocate one anyway. 213323cd1385SRemy Bohmer */ 21347612a43dSVitaly Kuzmichev if (cdc || rndis) { 213523cd1385SRemy Bohmer status_ep = usb_ep_autoconfig(gadget, &fs_status_desc); 213623cd1385SRemy Bohmer if (status_ep) { 213723cd1385SRemy Bohmer status_ep->driver_data = status_ep; /* claim */ 21387612a43dSVitaly Kuzmichev } else if (rndis) { 21397612a43dSVitaly Kuzmichev error("can't run RNDIS on %s", gadget->name); 21407612a43dSVitaly Kuzmichev return -ENODEV; 21417612a43dSVitaly Kuzmichev #ifdef DEV_CONFIG_CDC 214223cd1385SRemy Bohmer } else if (cdc) { 214323cd1385SRemy Bohmer control_intf.bNumEndpoints = 0; 214423cd1385SRemy Bohmer /* FIXME remove endpoint from descriptor list */ 21457612a43dSVitaly Kuzmichev #endif 214623cd1385SRemy Bohmer } 214723cd1385SRemy Bohmer } 214823cd1385SRemy Bohmer #endif 214923cd1385SRemy Bohmer 215023cd1385SRemy Bohmer /* one config: cdc, else minimal subset */ 215123cd1385SRemy Bohmer if (!cdc) { 215223cd1385SRemy Bohmer eth_config.bNumInterfaces = 1; 215323cd1385SRemy Bohmer eth_config.iConfiguration = STRING_SUBSET; 215423cd1385SRemy Bohmer 21556142e0aeSVitaly Kuzmichev /* 21566142e0aeSVitaly Kuzmichev * use functions to set these up, in case we're built to work 215723cd1385SRemy Bohmer * with multiple controllers and must override CDC Ethernet. 215823cd1385SRemy Bohmer */ 215923cd1385SRemy Bohmer fs_subset_descriptors(); 216023cd1385SRemy Bohmer hs_subset_descriptors(); 216123cd1385SRemy Bohmer } 216223cd1385SRemy Bohmer 216323cd1385SRemy Bohmer device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket; 216423cd1385SRemy Bohmer usb_gadget_set_selfpowered(gadget); 216523cd1385SRemy Bohmer 21667612a43dSVitaly Kuzmichev /* For now RNDIS is always a second config */ 21677612a43dSVitaly Kuzmichev if (rndis) 21687612a43dSVitaly Kuzmichev device_desc.bNumConfigurations = 2; 21697612a43dSVitaly Kuzmichev 217023cd1385SRemy Bohmer if (gadget_is_dualspeed(gadget)) { 21717612a43dSVitaly Kuzmichev if (rndis) 21727612a43dSVitaly Kuzmichev dev_qualifier.bNumConfigurations = 2; 21737612a43dSVitaly Kuzmichev else if (!cdc) 217423cd1385SRemy Bohmer dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC; 217523cd1385SRemy Bohmer 217623cd1385SRemy Bohmer /* assumes ep0 uses the same value for both speeds ... */ 217723cd1385SRemy Bohmer dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0; 217823cd1385SRemy Bohmer 217923cd1385SRemy Bohmer /* and that all endpoints are dual-speed */ 218023cd1385SRemy Bohmer hs_source_desc.bEndpointAddress = 218123cd1385SRemy Bohmer fs_source_desc.bEndpointAddress; 218223cd1385SRemy Bohmer hs_sink_desc.bEndpointAddress = 218323cd1385SRemy Bohmer fs_sink_desc.bEndpointAddress; 21847612a43dSVitaly Kuzmichev #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS) 218523cd1385SRemy Bohmer if (status_ep) 218623cd1385SRemy Bohmer hs_status_desc.bEndpointAddress = 218723cd1385SRemy Bohmer fs_status_desc.bEndpointAddress; 218823cd1385SRemy Bohmer #endif 218923cd1385SRemy Bohmer } 219023cd1385SRemy Bohmer 219123cd1385SRemy Bohmer if (gadget_is_otg(gadget)) { 219223cd1385SRemy Bohmer otg_descriptor.bmAttributes |= USB_OTG_HNP, 219323cd1385SRemy Bohmer eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP; 219423cd1385SRemy Bohmer eth_config.bMaxPower = 4; 21957612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS 21967612a43dSVitaly Kuzmichev rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP; 21977612a43dSVitaly Kuzmichev rndis_config.bMaxPower = 4; 21987612a43dSVitaly Kuzmichev #endif 219923cd1385SRemy Bohmer } 220023cd1385SRemy Bohmer 22017612a43dSVitaly Kuzmichev 22027612a43dSVitaly Kuzmichev /* network device setup */ 220323cd1385SRemy Bohmer dev->net = &l_netdev; 220423cd1385SRemy Bohmer 220523cd1385SRemy Bohmer dev->cdc = cdc; 220623cd1385SRemy Bohmer dev->zlp = zlp; 220723cd1385SRemy Bohmer 220823cd1385SRemy Bohmer dev->in_ep = in_ep; 220923cd1385SRemy Bohmer dev->out_ep = out_ep; 221023cd1385SRemy Bohmer dev->status_ep = status_ep; 221123cd1385SRemy Bohmer 22126142e0aeSVitaly Kuzmichev /* 22136142e0aeSVitaly Kuzmichev * Module params for these addresses should come from ID proms. 22147612a43dSVitaly Kuzmichev * The host side address is used with CDC and RNDIS, and commonly 221523cd1385SRemy Bohmer * ends up in a persistent config database. It's not clear if 221623cd1385SRemy Bohmer * host side code for the SAFE thing cares -- its original BLAN 221723cd1385SRemy Bohmer * thing didn't, Sharp never assigned those addresses on Zaurii. 221823cd1385SRemy Bohmer */ 221923cd1385SRemy Bohmer get_ether_addr(dev_addr, dev->net->enetaddr); 222023cd1385SRemy Bohmer 222123cd1385SRemy Bohmer memset(tmp, 0, sizeof(tmp)); 222223cd1385SRemy Bohmer memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr)); 222323cd1385SRemy Bohmer 222423cd1385SRemy Bohmer get_ether_addr(host_addr, dev->host_mac); 222523cd1385SRemy Bohmer 222623cd1385SRemy Bohmer sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X", 222723cd1385SRemy Bohmer dev->host_mac[0], dev->host_mac[1], 222823cd1385SRemy Bohmer dev->host_mac[2], dev->host_mac[3], 222923cd1385SRemy Bohmer dev->host_mac[4], dev->host_mac[5]); 223023cd1385SRemy Bohmer 22317612a43dSVitaly Kuzmichev if (rndis) { 22327612a43dSVitaly Kuzmichev status = rndis_init(); 22337612a43dSVitaly Kuzmichev if (status < 0) { 22347612a43dSVitaly Kuzmichev error("can't init RNDIS, %d", status); 22357612a43dSVitaly Kuzmichev goto fail; 22367612a43dSVitaly Kuzmichev } 223723cd1385SRemy Bohmer } 223823cd1385SRemy Bohmer 22396142e0aeSVitaly Kuzmichev /* 22406142e0aeSVitaly Kuzmichev * use PKTSIZE (or aligned... from u-boot) and set 22416142e0aeSVitaly Kuzmichev * wMaxSegmentSize accordingly 22426142e0aeSVitaly Kuzmichev */ 224323cd1385SRemy Bohmer dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/ 224423cd1385SRemy Bohmer 224523cd1385SRemy Bohmer /* preallocate control message data and buffer */ 224623cd1385SRemy Bohmer dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL); 224723cd1385SRemy Bohmer if (!dev->req) 224823cd1385SRemy Bohmer goto fail; 224923cd1385SRemy Bohmer dev->req->buf = control_req; 225023cd1385SRemy Bohmer dev->req->complete = eth_setup_complete; 225123cd1385SRemy Bohmer 225223cd1385SRemy Bohmer /* ... and maybe likewise for status transfer */ 22537612a43dSVitaly Kuzmichev #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS) 225423cd1385SRemy Bohmer if (dev->status_ep) { 22556142e0aeSVitaly Kuzmichev dev->stat_req = usb_ep_alloc_request(dev->status_ep, 22566142e0aeSVitaly Kuzmichev GFP_KERNEL); 225723cd1385SRemy Bohmer if (!dev->stat_req) { 2258df559c1dSVitaly Kuzmichev usb_ep_free_request(dev->status_ep, dev->req); 225923cd1385SRemy Bohmer 226023cd1385SRemy Bohmer goto fail; 226123cd1385SRemy Bohmer } 2262df559c1dSVitaly Kuzmichev dev->stat_req->buf = status_req; 226323cd1385SRemy Bohmer dev->stat_req->context = NULL; 226423cd1385SRemy Bohmer } 226523cd1385SRemy Bohmer #endif 226623cd1385SRemy Bohmer 226723cd1385SRemy Bohmer /* finish hookup to lower layer ... */ 226823cd1385SRemy Bohmer dev->gadget = gadget; 226923cd1385SRemy Bohmer set_gadget_data(gadget, dev); 227023cd1385SRemy Bohmer gadget->ep0->driver_data = dev; 227123cd1385SRemy Bohmer 22726142e0aeSVitaly Kuzmichev /* 22736142e0aeSVitaly Kuzmichev * two kinds of host-initiated state changes: 227423cd1385SRemy Bohmer * - iff DATA transfer is active, carrier is "on" 227523cd1385SRemy Bohmer * - tx queueing enabled if open *and* carrier is "on" 227623cd1385SRemy Bohmer */ 22777612a43dSVitaly Kuzmichev 22787612a43dSVitaly Kuzmichev printf("using %s, OUT %s IN %s%s%s\n", gadget->name, 22797612a43dSVitaly Kuzmichev out_ep->name, in_ep->name, 22807612a43dSVitaly Kuzmichev status_ep ? " STATUS " : "", 22817612a43dSVitaly Kuzmichev status_ep ? status_ep->name : "" 22827612a43dSVitaly Kuzmichev ); 22837612a43dSVitaly Kuzmichev printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n", 22847612a43dSVitaly Kuzmichev dev->net->enetaddr[0], dev->net->enetaddr[1], 22857612a43dSVitaly Kuzmichev dev->net->enetaddr[2], dev->net->enetaddr[3], 22867612a43dSVitaly Kuzmichev dev->net->enetaddr[4], dev->net->enetaddr[5]); 22877612a43dSVitaly Kuzmichev 22887612a43dSVitaly Kuzmichev if (cdc || rndis) 22897612a43dSVitaly Kuzmichev printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n", 22907612a43dSVitaly Kuzmichev dev->host_mac[0], dev->host_mac[1], 22917612a43dSVitaly Kuzmichev dev->host_mac[2], dev->host_mac[3], 22927612a43dSVitaly Kuzmichev dev->host_mac[4], dev->host_mac[5]); 22937612a43dSVitaly Kuzmichev 22947612a43dSVitaly Kuzmichev if (rndis) { 22957612a43dSVitaly Kuzmichev u32 vendorID = 0; 22967612a43dSVitaly Kuzmichev 22977612a43dSVitaly Kuzmichev /* FIXME RNDIS vendor id == "vendor NIC code" == ? */ 22987612a43dSVitaly Kuzmichev 22997612a43dSVitaly Kuzmichev dev->rndis_config = rndis_register(rndis_control_ack); 23007612a43dSVitaly Kuzmichev if (dev->rndis_config < 0) { 23017612a43dSVitaly Kuzmichev fail0: 23027612a43dSVitaly Kuzmichev eth_unbind(gadget); 23037612a43dSVitaly Kuzmichev debug("RNDIS setup failed\n"); 23047612a43dSVitaly Kuzmichev status = -ENODEV; 23057612a43dSVitaly Kuzmichev goto fail; 23067612a43dSVitaly Kuzmichev } 23077612a43dSVitaly Kuzmichev 23087612a43dSVitaly Kuzmichev /* these set up a lot of the OIDs that RNDIS needs */ 23097612a43dSVitaly Kuzmichev rndis_set_host_mac(dev->rndis_config, dev->host_mac); 23107612a43dSVitaly Kuzmichev if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu, 23117612a43dSVitaly Kuzmichev &dev->stats, &dev->cdc_filter)) 23127612a43dSVitaly Kuzmichev goto fail0; 23137612a43dSVitaly Kuzmichev if (rndis_set_param_vendor(dev->rndis_config, vendorID, 23147612a43dSVitaly Kuzmichev manufacturer)) 23157612a43dSVitaly Kuzmichev goto fail0; 23167612a43dSVitaly Kuzmichev if (rndis_set_param_medium(dev->rndis_config, 23177612a43dSVitaly Kuzmichev NDIS_MEDIUM_802_3, 0)) 23187612a43dSVitaly Kuzmichev goto fail0; 23197612a43dSVitaly Kuzmichev printf("RNDIS ready\n"); 23207612a43dSVitaly Kuzmichev } 232123cd1385SRemy Bohmer return 0; 232223cd1385SRemy Bohmer 232323cd1385SRemy Bohmer fail: 23247612a43dSVitaly Kuzmichev error("%s failed, status = %d", __func__, status); 232523cd1385SRemy Bohmer eth_unbind(gadget); 23267612a43dSVitaly Kuzmichev return status; 232723cd1385SRemy Bohmer } 232823cd1385SRemy Bohmer 23297612a43dSVitaly Kuzmichev /*-------------------------------------------------------------------------*/ 23307612a43dSVitaly Kuzmichev 233123cd1385SRemy Bohmer static int usb_eth_init(struct eth_device *netdev, bd_t *bd) 233223cd1385SRemy Bohmer { 233323cd1385SRemy Bohmer struct eth_dev *dev = &l_ethdev; 233423cd1385SRemy Bohmer struct usb_gadget *gadget; 233523cd1385SRemy Bohmer unsigned long ts; 233623cd1385SRemy Bohmer unsigned long timeout = USB_CONNECT_TIMEOUT; 233723cd1385SRemy Bohmer 233823cd1385SRemy Bohmer if (!netdev) { 23397de73185SVitaly Kuzmichev error("received NULL ptr"); 234023cd1385SRemy Bohmer goto fail; 234123cd1385SRemy Bohmer } 234258939fccSVitaly Kuzmichev 234358939fccSVitaly Kuzmichev /* Configure default mac-addresses for the USB ethernet device */ 234458939fccSVitaly Kuzmichev #ifdef CONFIG_USBNET_DEV_ADDR 234558939fccSVitaly Kuzmichev strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr)); 234658939fccSVitaly Kuzmichev #endif 234758939fccSVitaly Kuzmichev #ifdef CONFIG_USBNET_HOST_ADDR 234858939fccSVitaly Kuzmichev strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr)); 234958939fccSVitaly Kuzmichev #endif 235058939fccSVitaly Kuzmichev /* Check if the user overruled the MAC addresses */ 235158939fccSVitaly Kuzmichev if (getenv("usbnet_devaddr")) 235258939fccSVitaly Kuzmichev strlcpy(dev_addr, getenv("usbnet_devaddr"), 235358939fccSVitaly Kuzmichev sizeof(dev_addr)); 235458939fccSVitaly Kuzmichev 235558939fccSVitaly Kuzmichev if (getenv("usbnet_hostaddr")) 235658939fccSVitaly Kuzmichev strlcpy(host_addr, getenv("usbnet_hostaddr"), 235758939fccSVitaly Kuzmichev sizeof(host_addr)); 235858939fccSVitaly Kuzmichev 235958939fccSVitaly Kuzmichev if (!is_eth_addr_valid(dev_addr)) { 236058939fccSVitaly Kuzmichev error("Need valid 'usbnet_devaddr' to be set"); 236158939fccSVitaly Kuzmichev goto fail; 236258939fccSVitaly Kuzmichev } 236358939fccSVitaly Kuzmichev if (!is_eth_addr_valid(host_addr)) { 236458939fccSVitaly Kuzmichev error("Need valid 'usbnet_hostaddr' to be set"); 236558939fccSVitaly Kuzmichev goto fail; 236658939fccSVitaly Kuzmichev } 236758939fccSVitaly Kuzmichev 2368988ee3e3SLei Wen if (usb_gadget_register_driver(ð_driver) < 0) 2369988ee3e3SLei Wen goto fail; 237023cd1385SRemy Bohmer 237123cd1385SRemy Bohmer dev->network_started = 0; 237223cd1385SRemy Bohmer 237323cd1385SRemy Bohmer packet_received = 0; 237423cd1385SRemy Bohmer packet_sent = 0; 237523cd1385SRemy Bohmer 237623cd1385SRemy Bohmer gadget = dev->gadget; 237723cd1385SRemy Bohmer usb_gadget_connect(gadget); 237823cd1385SRemy Bohmer 237923cd1385SRemy Bohmer if (getenv("cdc_connect_timeout")) 238023cd1385SRemy Bohmer timeout = simple_strtoul(getenv("cdc_connect_timeout"), 238123cd1385SRemy Bohmer NULL, 10) * CONFIG_SYS_HZ; 238223cd1385SRemy Bohmer ts = get_timer(0); 23836142e0aeSVitaly Kuzmichev while (!l_ethdev.network_started) { 238423cd1385SRemy Bohmer /* Handle control-c and timeouts */ 238523cd1385SRemy Bohmer if (ctrlc() || (get_timer(ts) > timeout)) { 23867de73185SVitaly Kuzmichev error("The remote end did not respond in time."); 238723cd1385SRemy Bohmer goto fail; 238823cd1385SRemy Bohmer } 238923cd1385SRemy Bohmer usb_gadget_handle_interrupts(); 239023cd1385SRemy Bohmer } 239123cd1385SRemy Bohmer 239298fae970SVitaly Kuzmichev packet_received = 0; 239323cd1385SRemy Bohmer rx_submit(dev, dev->rx_req, 0); 239423cd1385SRemy Bohmer return 0; 239523cd1385SRemy Bohmer fail: 239623cd1385SRemy Bohmer return -1; 239723cd1385SRemy Bohmer } 239823cd1385SRemy Bohmer 23996142e0aeSVitaly Kuzmichev static int usb_eth_send(struct eth_device *netdev, 24006142e0aeSVitaly Kuzmichev volatile void *packet, int length) 240123cd1385SRemy Bohmer { 240223cd1385SRemy Bohmer int retval; 24037612a43dSVitaly Kuzmichev void *rndis_pkt = NULL; 240423cd1385SRemy Bohmer struct eth_dev *dev = &l_ethdev; 2405ac5d32d1SVitaly Kuzmichev struct usb_request *req = dev->tx_req; 2406a170f2c7SStefano Babic unsigned long ts; 2407a170f2c7SStefano Babic unsigned long timeout = USB_CONNECT_TIMEOUT; 24087de73185SVitaly Kuzmichev 24097de73185SVitaly Kuzmichev debug("%s:...\n", __func__); 241023cd1385SRemy Bohmer 24117612a43dSVitaly Kuzmichev /* new buffer is needed to include RNDIS header */ 24127612a43dSVitaly Kuzmichev if (rndis_active(dev)) { 24137612a43dSVitaly Kuzmichev rndis_pkt = malloc(length + 24147612a43dSVitaly Kuzmichev sizeof(struct rndis_packet_msg_type)); 24157612a43dSVitaly Kuzmichev if (!rndis_pkt) { 24167612a43dSVitaly Kuzmichev error("No memory to alloc RNDIS packet"); 24177612a43dSVitaly Kuzmichev goto drop; 24187612a43dSVitaly Kuzmichev } 24197612a43dSVitaly Kuzmichev rndis_add_hdr(rndis_pkt, length); 24207612a43dSVitaly Kuzmichev memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type), 24217612a43dSVitaly Kuzmichev (void *)packet, length); 24227612a43dSVitaly Kuzmichev packet = rndis_pkt; 24237612a43dSVitaly Kuzmichev length += sizeof(struct rndis_packet_msg_type); 24247612a43dSVitaly Kuzmichev } 242523cd1385SRemy Bohmer req->buf = (void *)packet; 242623cd1385SRemy Bohmer req->context = NULL; 242723cd1385SRemy Bohmer req->complete = tx_complete; 242823cd1385SRemy Bohmer 24296142e0aeSVitaly Kuzmichev /* 24306142e0aeSVitaly Kuzmichev * use zlp framing on tx for strict CDC-Ether conformance, 243123cd1385SRemy Bohmer * though any robust network rx path ignores extra padding. 243223cd1385SRemy Bohmer * and some hardware doesn't like to write zlps. 243323cd1385SRemy Bohmer */ 243423cd1385SRemy Bohmer req->zero = 1; 243523cd1385SRemy Bohmer if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0) 243623cd1385SRemy Bohmer length++; 243723cd1385SRemy Bohmer 243823cd1385SRemy Bohmer req->length = length; 243923cd1385SRemy Bohmer #if 0 244023cd1385SRemy Bohmer /* throttle highspeed IRQ rate back slightly */ 244123cd1385SRemy Bohmer if (gadget_is_dualspeed(dev->gadget)) 244223cd1385SRemy Bohmer req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH) 244323cd1385SRemy Bohmer ? ((dev->tx_qlen % qmult) != 0) : 0; 244423cd1385SRemy Bohmer #endif 244523cd1385SRemy Bohmer dev->tx_qlen = 1; 2446a170f2c7SStefano Babic ts = get_timer(0); 2447a170f2c7SStefano Babic packet_sent = 0; 244823cd1385SRemy Bohmer 244923cd1385SRemy Bohmer retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC); 245023cd1385SRemy Bohmer 245123cd1385SRemy Bohmer if (!retval) 24527de73185SVitaly Kuzmichev debug("%s: packet queued\n", __func__); 24536142e0aeSVitaly Kuzmichev while (!packet_sent) { 2454a170f2c7SStefano Babic if (get_timer(ts) > timeout) { 2455a170f2c7SStefano Babic printf("timeout sending packets to usb ethernet\n"); 2456a170f2c7SStefano Babic return -1; 2457a170f2c7SStefano Babic } 2458a170f2c7SStefano Babic usb_gadget_handle_interrupts(); 245923cd1385SRemy Bohmer } 24607612a43dSVitaly Kuzmichev if (rndis_pkt) 24617612a43dSVitaly Kuzmichev free(rndis_pkt); 246223cd1385SRemy Bohmer 246323cd1385SRemy Bohmer return 0; 24647612a43dSVitaly Kuzmichev drop: 24657612a43dSVitaly Kuzmichev dev->stats.tx_dropped++; 24667612a43dSVitaly Kuzmichev return -ENOMEM; 246723cd1385SRemy Bohmer } 246823cd1385SRemy Bohmer 246923cd1385SRemy Bohmer static int usb_eth_recv(struct eth_device *netdev) 247023cd1385SRemy Bohmer { 247123cd1385SRemy Bohmer struct eth_dev *dev = &l_ethdev; 247223cd1385SRemy Bohmer 247323cd1385SRemy Bohmer usb_gadget_handle_interrupts(); 247423cd1385SRemy Bohmer 24756142e0aeSVitaly Kuzmichev if (packet_received) { 24767de73185SVitaly Kuzmichev debug("%s: packet received\n", __func__); 24776142e0aeSVitaly Kuzmichev if (dev->rx_req) { 247823cd1385SRemy Bohmer NetReceive(NetRxPackets[0], dev->rx_req->length); 247923cd1385SRemy Bohmer packet_received = 0; 248023cd1385SRemy Bohmer 248123cd1385SRemy Bohmer rx_submit(dev, dev->rx_req, 0); 24826142e0aeSVitaly Kuzmichev } else 24836142e0aeSVitaly Kuzmichev error("dev->rx_req invalid"); 248423cd1385SRemy Bohmer } 248523cd1385SRemy Bohmer return 0; 248623cd1385SRemy Bohmer } 248723cd1385SRemy Bohmer 248823cd1385SRemy Bohmer void usb_eth_halt(struct eth_device *netdev) 248923cd1385SRemy Bohmer { 249023cd1385SRemy Bohmer struct eth_dev *dev = &l_ethdev; 249123cd1385SRemy Bohmer 24926142e0aeSVitaly Kuzmichev if (!netdev) { 24937de73185SVitaly Kuzmichev error("received NULL ptr"); 249423cd1385SRemy Bohmer return; 249523cd1385SRemy Bohmer } 249623cd1385SRemy Bohmer 2497988ee3e3SLei Wen /* If the gadget not registered, simple return */ 2498988ee3e3SLei Wen if (!dev->gadget) 2499988ee3e3SLei Wen return; 2500988ee3e3SLei Wen 2501*e4ae6660SVitaly Kuzmichev /* 2502*e4ae6660SVitaly Kuzmichev * Some USB controllers may need additional deinitialization here 2503*e4ae6660SVitaly Kuzmichev * before dropping pull-up (also due to hardware issues). 2504*e4ae6660SVitaly Kuzmichev * For example: unhandled interrupt with status stage started may 2505*e4ae6660SVitaly Kuzmichev * bring the controller to fully broken state (until board reset). 2506*e4ae6660SVitaly Kuzmichev * There are some variants to debug and fix such cases: 2507*e4ae6660SVitaly Kuzmichev * 1) In the case of RNDIS connection eth_stop can perform additional 2508*e4ae6660SVitaly Kuzmichev * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition. 2509*e4ae6660SVitaly Kuzmichev * 2) 'pullup' callback in your UDC driver can be improved to perform 2510*e4ae6660SVitaly Kuzmichev * this deinitialization. 2511*e4ae6660SVitaly Kuzmichev */ 25127612a43dSVitaly Kuzmichev eth_stop(dev); 25137612a43dSVitaly Kuzmichev 251423cd1385SRemy Bohmer usb_gadget_disconnect(dev->gadget); 2515b3649f3bSVitaly Kuzmichev 2516b3649f3bSVitaly Kuzmichev /* Clear pending interrupt */ 2517b3649f3bSVitaly Kuzmichev if (dev->network_started) { 2518b3649f3bSVitaly Kuzmichev usb_gadget_handle_interrupts(); 2519b3649f3bSVitaly Kuzmichev dev->network_started = 0; 2520b3649f3bSVitaly Kuzmichev } 2521b3649f3bSVitaly Kuzmichev 2522988ee3e3SLei Wen usb_gadget_unregister_driver(ð_driver); 252323cd1385SRemy Bohmer } 252423cd1385SRemy Bohmer 252523cd1385SRemy Bohmer static struct usb_gadget_driver eth_driver = { 252623cd1385SRemy Bohmer .speed = DEVSPEED, 252723cd1385SRemy Bohmer 252823cd1385SRemy Bohmer .bind = eth_bind, 252923cd1385SRemy Bohmer .unbind = eth_unbind, 253023cd1385SRemy Bohmer 253123cd1385SRemy Bohmer .setup = eth_setup, 253223cd1385SRemy Bohmer .disconnect = eth_disconnect, 253323cd1385SRemy Bohmer 253423cd1385SRemy Bohmer .suspend = eth_suspend, 253523cd1385SRemy Bohmer .resume = eth_resume, 253623cd1385SRemy Bohmer }; 253723cd1385SRemy Bohmer 253823cd1385SRemy Bohmer int usb_eth_initialize(bd_t *bi) 253923cd1385SRemy Bohmer { 254023cd1385SRemy Bohmer struct eth_device *netdev = &l_netdev; 254123cd1385SRemy Bohmer 25428f7aa831SVitaly Kuzmichev strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name)); 254323cd1385SRemy Bohmer 254423cd1385SRemy Bohmer netdev->init = usb_eth_init; 254523cd1385SRemy Bohmer netdev->send = usb_eth_send; 254623cd1385SRemy Bohmer netdev->recv = usb_eth_recv; 254723cd1385SRemy Bohmer netdev->halt = usb_eth_halt; 254823cd1385SRemy Bohmer 254923cd1385SRemy Bohmer #ifdef CONFIG_MCAST_TFTP 255023cd1385SRemy Bohmer #error not supported 255123cd1385SRemy Bohmer #endif 255223cd1385SRemy Bohmer eth_register(netdev); 255323cd1385SRemy Bohmer return 0; 255423cd1385SRemy Bohmer } 2555