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> 27b9300531SLukasz Majewski #include <usbdescriptors.h> 2823cd1385SRemy Bohmer #include <linux/usb/cdc.h> 2923cd1385SRemy Bohmer #include <linux/usb/gadget.h> 3023cd1385SRemy Bohmer #include <net.h> 317612a43dSVitaly Kuzmichev #include <malloc.h> 3223cd1385SRemy Bohmer #include <linux/ctype.h> 3323cd1385SRemy Bohmer 3423cd1385SRemy Bohmer #include "gadget_chips.h" 357612a43dSVitaly Kuzmichev #include "rndis.h" 3623cd1385SRemy Bohmer 378f7aa831SVitaly Kuzmichev #define USB_NET_NAME "usb_ether" 387de73185SVitaly Kuzmichev 3923cd1385SRemy Bohmer #define atomic_read 4023cd1385SRemy Bohmer extern struct platform_data brd; 4123cd1385SRemy Bohmer #define spin_lock(x) 4223cd1385SRemy Bohmer #define spin_unlock(x) 4323cd1385SRemy Bohmer 4423cd1385SRemy Bohmer 4523cd1385SRemy Bohmer unsigned packet_received, packet_sent; 4623cd1385SRemy Bohmer 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 { 1622bb37884SLukasz Dalek #if !defined(CONFIG_USB_ETH_SUBSET) 16323cd1385SRemy Bohmer return 1; /* only cdc possible */ 1642bb37884SLukasz Dalek #elif !defined(CONFIG_USB_ETH_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 4082bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_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 4472bb37884SLukasz Dalek #if defined(CONFIG_USB_ETH_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 4812bb37884SLukasz Dalek #ifndef CONFIG_USB_ETH_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 5312bb37884SLukasz Dalek #if defined(CONFIG_USB_ETH_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 5632bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_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 6182bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_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, 6642bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_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, 6782bb37884SLukasz Dalek #endif /* CONFIG_USB_ETH_CDC */ 67923cd1385SRemy Bohmer }; 68023cd1385SRemy Bohmer 68123cd1385SRemy Bohmer static inline void fs_subset_descriptors(void) 68223cd1385SRemy Bohmer { 6832bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_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 7212bb37884SLukasz Dalek #if defined(CONFIG_USB_ETH_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 }; 7312bb37884SLukasz Dalek #endif /* CONFIG_USB_ETH_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, 7642bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_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, 7782bb37884SLukasz Dalek #endif /* CONFIG_USB_ETH_CDC */ 77923cd1385SRemy Bohmer }; 78023cd1385SRemy Bohmer 78123cd1385SRemy Bohmer static inline void hs_subset_descriptors(void) 78223cd1385SRemy Bohmer { 7832bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_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, }, 8452bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_CDC 84623cd1385SRemy Bohmer { STRING_CDC, "CDC Ethernet", }, 84723cd1385SRemy Bohmer { STRING_CONTROL, "CDC Communications Control", }, 84823cd1385SRemy Bohmer #endif 8492bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_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]; 8662bb37884SLukasz Dalek #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS) 86780460772SStefano Babic static u8 status_req[STATUS_BYTECOUNT] __attribute__ ((aligned(4))); 868563aed25SLukasz Dalek #endif 86923cd1385SRemy Bohmer 87023cd1385SRemy Bohmer 87123cd1385SRemy Bohmer /** 87223cd1385SRemy Bohmer * strlcpy - Copy a %NUL terminated string into a sized buffer 87323cd1385SRemy Bohmer * @dest: Where to copy the string to 87423cd1385SRemy Bohmer * @src: Where to copy the string from 87523cd1385SRemy Bohmer * @size: size of destination buffer 87623cd1385SRemy Bohmer * 87723cd1385SRemy Bohmer * Compatible with *BSD: the result is always a valid 87823cd1385SRemy Bohmer * NUL-terminated string that fits in the buffer (unless, 87923cd1385SRemy Bohmer * of course, the buffer size is zero). It does not pad 88023cd1385SRemy Bohmer * out the result like strncpy() does. 88123cd1385SRemy Bohmer */ 88223cd1385SRemy Bohmer size_t strlcpy(char *dest, const char *src, size_t size) 88323cd1385SRemy Bohmer { 88423cd1385SRemy Bohmer size_t ret = strlen(src); 88523cd1385SRemy Bohmer 88623cd1385SRemy Bohmer if (size) { 88723cd1385SRemy Bohmer size_t len = (ret >= size) ? size - 1 : ret; 88823cd1385SRemy Bohmer memcpy(dest, src, len); 88923cd1385SRemy Bohmer dest[len] = '\0'; 89023cd1385SRemy Bohmer } 89123cd1385SRemy Bohmer return ret; 89223cd1385SRemy Bohmer } 89323cd1385SRemy Bohmer 89423cd1385SRemy Bohmer /*============================================================================*/ 89523cd1385SRemy Bohmer 89623cd1385SRemy Bohmer /* 89723cd1385SRemy Bohmer * one config, two interfaces: control, data. 89823cd1385SRemy Bohmer * complications: class descriptors, and an altsetting. 89923cd1385SRemy Bohmer */ 90023cd1385SRemy Bohmer static int 90123cd1385SRemy Bohmer config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg) 90223cd1385SRemy Bohmer { 90323cd1385SRemy Bohmer int len; 90423cd1385SRemy Bohmer const struct usb_config_descriptor *config; 90523cd1385SRemy Bohmer const struct usb_descriptor_header **function; 90623cd1385SRemy Bohmer int hs = 0; 90723cd1385SRemy Bohmer 90823cd1385SRemy Bohmer if (gadget_is_dualspeed(g)) { 90923cd1385SRemy Bohmer hs = (g->speed == USB_SPEED_HIGH); 91023cd1385SRemy Bohmer if (type == USB_DT_OTHER_SPEED_CONFIG) 91123cd1385SRemy Bohmer hs = !hs; 91223cd1385SRemy Bohmer } 91323cd1385SRemy Bohmer #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function) 91423cd1385SRemy Bohmer 91523cd1385SRemy Bohmer if (index >= device_desc.bNumConfigurations) 91623cd1385SRemy Bohmer return -EINVAL; 91723cd1385SRemy Bohmer 9187612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS 9197612a43dSVitaly Kuzmichev /* 9207612a43dSVitaly Kuzmichev * list the RNDIS config first, to make Microsoft's drivers 9217612a43dSVitaly Kuzmichev * happy. DOCSIS 1.0 needs this too. 9227612a43dSVitaly Kuzmichev */ 9237612a43dSVitaly Kuzmichev if (device_desc.bNumConfigurations == 2 && index == 0) { 9247612a43dSVitaly Kuzmichev config = &rndis_config; 9257612a43dSVitaly Kuzmichev function = which_fn(rndis); 9267612a43dSVitaly Kuzmichev } else 9277612a43dSVitaly Kuzmichev #endif 9287612a43dSVitaly Kuzmichev { 92923cd1385SRemy Bohmer config = ð_config; 93023cd1385SRemy Bohmer function = which_fn(eth); 9317612a43dSVitaly Kuzmichev } 93223cd1385SRemy Bohmer 93323cd1385SRemy Bohmer /* for now, don't advertise srp-only devices */ 93423cd1385SRemy Bohmer if (!is_otg) 93523cd1385SRemy Bohmer function++; 93623cd1385SRemy Bohmer 93723cd1385SRemy Bohmer len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function); 93823cd1385SRemy Bohmer if (len < 0) 93923cd1385SRemy Bohmer return len; 94023cd1385SRemy Bohmer ((struct usb_config_descriptor *) buf)->bDescriptorType = type; 94123cd1385SRemy Bohmer return len; 94223cd1385SRemy Bohmer } 94323cd1385SRemy Bohmer 94423cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 94523cd1385SRemy Bohmer 9467612a43dSVitaly Kuzmichev static void eth_start(struct eth_dev *dev, gfp_t gfp_flags); 94723cd1385SRemy Bohmer static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags); 94823cd1385SRemy Bohmer 94923cd1385SRemy Bohmer static int 95023cd1385SRemy Bohmer set_ether_config(struct eth_dev *dev, gfp_t gfp_flags) 95123cd1385SRemy Bohmer { 95223cd1385SRemy Bohmer int result = 0; 95323cd1385SRemy Bohmer struct usb_gadget *gadget = dev->gadget; 95423cd1385SRemy Bohmer 9552bb37884SLukasz Dalek #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS) 9567612a43dSVitaly Kuzmichev /* status endpoint used for RNDIS and (optionally) CDC */ 95723cd1385SRemy Bohmer if (!subset_active(dev) && dev->status_ep) { 95823cd1385SRemy Bohmer dev->status = ep_desc(gadget, &hs_status_desc, 95923cd1385SRemy Bohmer &fs_status_desc); 96023cd1385SRemy Bohmer dev->status_ep->driver_data = dev; 96123cd1385SRemy Bohmer 96223cd1385SRemy Bohmer result = usb_ep_enable(dev->status_ep, dev->status); 96323cd1385SRemy Bohmer if (result != 0) { 9647de73185SVitaly Kuzmichev debug("enable %s --> %d\n", 96523cd1385SRemy Bohmer dev->status_ep->name, result); 96623cd1385SRemy Bohmer goto done; 96723cd1385SRemy Bohmer } 96823cd1385SRemy Bohmer } 96923cd1385SRemy Bohmer #endif 97023cd1385SRemy Bohmer 97123cd1385SRemy Bohmer dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc); 97223cd1385SRemy Bohmer dev->in_ep->driver_data = dev; 97323cd1385SRemy Bohmer 97423cd1385SRemy Bohmer dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc); 97523cd1385SRemy Bohmer dev->out_ep->driver_data = dev; 97623cd1385SRemy Bohmer 9776142e0aeSVitaly Kuzmichev /* 9786142e0aeSVitaly Kuzmichev * With CDC, the host isn't allowed to use these two data 97923cd1385SRemy Bohmer * endpoints in the default altsetting for the interface. 98023cd1385SRemy Bohmer * so we don't activate them yet. Reset from SET_INTERFACE. 9817612a43dSVitaly Kuzmichev * 9827612a43dSVitaly Kuzmichev * Strictly speaking RNDIS should work the same: activation is 9837612a43dSVitaly Kuzmichev * a side effect of setting a packet filter. Deactivation is 9847612a43dSVitaly Kuzmichev * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG. 98523cd1385SRemy Bohmer */ 98623cd1385SRemy Bohmer if (!cdc_active(dev)) { 98723cd1385SRemy Bohmer result = usb_ep_enable(dev->in_ep, dev->in); 98823cd1385SRemy Bohmer if (result != 0) { 9897de73185SVitaly Kuzmichev debug("enable %s --> %d\n", 99023cd1385SRemy Bohmer dev->in_ep->name, result); 99123cd1385SRemy Bohmer goto done; 99223cd1385SRemy Bohmer } 99323cd1385SRemy Bohmer 99423cd1385SRemy Bohmer result = usb_ep_enable(dev->out_ep, dev->out); 99523cd1385SRemy Bohmer if (result != 0) { 9967de73185SVitaly Kuzmichev debug("enable %s --> %d\n", 99723cd1385SRemy Bohmer dev->out_ep->name, result); 99823cd1385SRemy Bohmer goto done; 99923cd1385SRemy Bohmer } 100023cd1385SRemy Bohmer } 100123cd1385SRemy Bohmer 100223cd1385SRemy Bohmer done: 100323cd1385SRemy Bohmer if (result == 0) 100423cd1385SRemy Bohmer result = alloc_requests(dev, qlen(gadget), gfp_flags); 100523cd1385SRemy Bohmer 100623cd1385SRemy Bohmer /* on error, disable any endpoints */ 100723cd1385SRemy Bohmer if (result < 0) { 1008d5292c16SVitaly Kuzmichev if (!subset_active(dev) && dev->status_ep) 100923cd1385SRemy Bohmer (void) usb_ep_disable(dev->status_ep); 101023cd1385SRemy Bohmer dev->status = NULL; 101123cd1385SRemy Bohmer (void) usb_ep_disable(dev->in_ep); 101223cd1385SRemy Bohmer (void) usb_ep_disable(dev->out_ep); 101323cd1385SRemy Bohmer dev->in = NULL; 101423cd1385SRemy Bohmer dev->out = NULL; 10157612a43dSVitaly Kuzmichev } else if (!cdc_active(dev)) { 10167612a43dSVitaly Kuzmichev /* 10177612a43dSVitaly Kuzmichev * activate non-CDC configs right away 10187612a43dSVitaly Kuzmichev * this isn't strictly according to the RNDIS spec 10197612a43dSVitaly Kuzmichev */ 10207612a43dSVitaly Kuzmichev eth_start(dev, GFP_ATOMIC); 102123cd1385SRemy Bohmer } 102223cd1385SRemy Bohmer 102323cd1385SRemy Bohmer /* caller is responsible for cleanup on error */ 102423cd1385SRemy Bohmer return result; 102523cd1385SRemy Bohmer } 102623cd1385SRemy Bohmer 102723cd1385SRemy Bohmer static void eth_reset_config(struct eth_dev *dev) 102823cd1385SRemy Bohmer { 102923cd1385SRemy Bohmer if (dev->config == 0) 103023cd1385SRemy Bohmer return; 103123cd1385SRemy Bohmer 10327de73185SVitaly Kuzmichev debug("%s\n", __func__); 10337de73185SVitaly Kuzmichev 10347612a43dSVitaly Kuzmichev rndis_uninit(dev->rndis_config); 10357612a43dSVitaly Kuzmichev 10366142e0aeSVitaly Kuzmichev /* 10376142e0aeSVitaly Kuzmichev * disable endpoints, forcing (synchronous) completion of 103823cd1385SRemy Bohmer * pending i/o. then free the requests. 103923cd1385SRemy Bohmer */ 104023cd1385SRemy Bohmer 104123cd1385SRemy Bohmer if (dev->in) { 104223cd1385SRemy Bohmer usb_ep_disable(dev->in_ep); 104323cd1385SRemy Bohmer if (dev->tx_req) { 104423cd1385SRemy Bohmer usb_ep_free_request(dev->in_ep, dev->tx_req); 104523cd1385SRemy Bohmer dev->tx_req = NULL; 104623cd1385SRemy Bohmer } 104723cd1385SRemy Bohmer } 104823cd1385SRemy Bohmer if (dev->out) { 104923cd1385SRemy Bohmer usb_ep_disable(dev->out_ep); 105023cd1385SRemy Bohmer if (dev->rx_req) { 10510129e327SVitaly Kuzmichev usb_ep_free_request(dev->out_ep, dev->rx_req); 105223cd1385SRemy Bohmer dev->rx_req = NULL; 105323cd1385SRemy Bohmer } 105423cd1385SRemy Bohmer } 10556142e0aeSVitaly Kuzmichev if (dev->status) 105623cd1385SRemy Bohmer usb_ep_disable(dev->status_ep); 10576142e0aeSVitaly Kuzmichev 10587612a43dSVitaly Kuzmichev dev->rndis = 0; 105923cd1385SRemy Bohmer dev->cdc_filter = 0; 106023cd1385SRemy Bohmer dev->config = 0; 106123cd1385SRemy Bohmer } 106223cd1385SRemy Bohmer 10636142e0aeSVitaly Kuzmichev /* 10646142e0aeSVitaly Kuzmichev * change our operational config. must agree with the code 106523cd1385SRemy Bohmer * that returns config descriptors, and altsetting code. 106623cd1385SRemy Bohmer */ 10676142e0aeSVitaly Kuzmichev static int eth_set_config(struct eth_dev *dev, unsigned number, 10686142e0aeSVitaly Kuzmichev gfp_t gfp_flags) 106923cd1385SRemy Bohmer { 107023cd1385SRemy Bohmer int result = 0; 107123cd1385SRemy Bohmer struct usb_gadget *gadget = dev->gadget; 107223cd1385SRemy Bohmer 107323cd1385SRemy Bohmer if (gadget_is_sa1100(gadget) 107423cd1385SRemy Bohmer && dev->config 107523cd1385SRemy Bohmer && dev->tx_qlen != 0) { 107623cd1385SRemy Bohmer /* tx fifo is full, but we can't clear it...*/ 10777de73185SVitaly Kuzmichev error("can't change configurations"); 107823cd1385SRemy Bohmer return -ESPIPE; 107923cd1385SRemy Bohmer } 108023cd1385SRemy Bohmer eth_reset_config(dev); 108123cd1385SRemy Bohmer 108223cd1385SRemy Bohmer switch (number) { 108323cd1385SRemy Bohmer case DEV_CONFIG_VALUE: 108423cd1385SRemy Bohmer result = set_ether_config(dev, gfp_flags); 108523cd1385SRemy Bohmer break; 10867612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS 10877612a43dSVitaly Kuzmichev case DEV_RNDIS_CONFIG_VALUE: 10887612a43dSVitaly Kuzmichev dev->rndis = 1; 10897612a43dSVitaly Kuzmichev result = set_ether_config(dev, gfp_flags); 10907612a43dSVitaly Kuzmichev break; 10917612a43dSVitaly Kuzmichev #endif 109223cd1385SRemy Bohmer default: 109323cd1385SRemy Bohmer result = -EINVAL; 109423cd1385SRemy Bohmer /* FALL THROUGH */ 109523cd1385SRemy Bohmer case 0: 109623cd1385SRemy Bohmer break; 109723cd1385SRemy Bohmer } 109823cd1385SRemy Bohmer 109923cd1385SRemy Bohmer if (result) { 110023cd1385SRemy Bohmer if (number) 110123cd1385SRemy Bohmer eth_reset_config(dev); 110223cd1385SRemy Bohmer usb_gadget_vbus_draw(dev->gadget, 110323cd1385SRemy Bohmer gadget_is_otg(dev->gadget) ? 8 : 100); 110423cd1385SRemy Bohmer } else { 110523cd1385SRemy Bohmer char *speed; 110623cd1385SRemy Bohmer unsigned power; 110723cd1385SRemy Bohmer 110823cd1385SRemy Bohmer power = 2 * eth_config.bMaxPower; 110923cd1385SRemy Bohmer usb_gadget_vbus_draw(dev->gadget, power); 111023cd1385SRemy Bohmer 111123cd1385SRemy Bohmer switch (gadget->speed) { 11126142e0aeSVitaly Kuzmichev case USB_SPEED_FULL: 11136142e0aeSVitaly Kuzmichev speed = "full"; break; 111423cd1385SRemy Bohmer #ifdef CONFIG_USB_GADGET_DUALSPEED 11156142e0aeSVitaly Kuzmichev case USB_SPEED_HIGH: 11166142e0aeSVitaly Kuzmichev speed = "high"; break; 111723cd1385SRemy Bohmer #endif 11186142e0aeSVitaly Kuzmichev default: 11196142e0aeSVitaly Kuzmichev speed = "?"; break; 112023cd1385SRemy Bohmer } 112123cd1385SRemy Bohmer 112223cd1385SRemy Bohmer dev->config = number; 11237de73185SVitaly Kuzmichev printf("%s speed config #%d: %d mA, %s, using %s\n", 112423cd1385SRemy Bohmer speed, number, power, driver_desc, 11257612a43dSVitaly Kuzmichev rndis_active(dev) 11267612a43dSVitaly Kuzmichev ? "RNDIS" 11277612a43dSVitaly Kuzmichev : (cdc_active(dev) 11287612a43dSVitaly Kuzmichev ? "CDC Ethernet" 112923cd1385SRemy Bohmer : "CDC Ethernet Subset")); 113023cd1385SRemy Bohmer } 113123cd1385SRemy Bohmer return result; 113223cd1385SRemy Bohmer } 113323cd1385SRemy Bohmer 113423cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 113523cd1385SRemy Bohmer 11362bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_CDC 113723cd1385SRemy Bohmer 11386142e0aeSVitaly Kuzmichev /* 11396142e0aeSVitaly Kuzmichev * The interrupt endpoint is used in CDC networking models (Ethernet, ATM) 114023cd1385SRemy Bohmer * only to notify the host about link status changes (which we support) or 11417612a43dSVitaly Kuzmichev * report completion of some encapsulated command (as used in RNDIS). Since 114223cd1385SRemy Bohmer * we want this CDC Ethernet code to be vendor-neutral, we don't use that 114323cd1385SRemy Bohmer * command mechanism; and only one status request is ever queued. 114423cd1385SRemy Bohmer */ 114523cd1385SRemy Bohmer static void eth_status_complete(struct usb_ep *ep, struct usb_request *req) 114623cd1385SRemy Bohmer { 114723cd1385SRemy Bohmer struct usb_cdc_notification *event = req->buf; 114823cd1385SRemy Bohmer int value = req->status; 114923cd1385SRemy Bohmer struct eth_dev *dev = ep->driver_data; 115023cd1385SRemy Bohmer 115123cd1385SRemy Bohmer /* issue the second notification if host reads the first */ 115223cd1385SRemy Bohmer if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION 115323cd1385SRemy Bohmer && value == 0) { 115423cd1385SRemy Bohmer __le32 *data = req->buf + sizeof *event; 115523cd1385SRemy Bohmer 115623cd1385SRemy Bohmer event->bmRequestType = 0xA1; 115723cd1385SRemy Bohmer event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE; 115823cd1385SRemy Bohmer event->wValue = __constant_cpu_to_le16(0); 115923cd1385SRemy Bohmer event->wIndex = __constant_cpu_to_le16(1); 116023cd1385SRemy Bohmer event->wLength = __constant_cpu_to_le16(8); 116123cd1385SRemy Bohmer 116223cd1385SRemy Bohmer /* SPEED_CHANGE data is up/down speeds in bits/sec */ 116323cd1385SRemy Bohmer data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget)); 116423cd1385SRemy Bohmer 116523cd1385SRemy Bohmer req->length = STATUS_BYTECOUNT; 116623cd1385SRemy Bohmer value = usb_ep_queue(ep, req, GFP_ATOMIC); 11677de73185SVitaly Kuzmichev debug("send SPEED_CHANGE --> %d\n", value); 116823cd1385SRemy Bohmer if (value == 0) 116923cd1385SRemy Bohmer return; 117023cd1385SRemy Bohmer } else if (value != -ECONNRESET) { 11717de73185SVitaly Kuzmichev debug("event %02x --> %d\n", 117223cd1385SRemy Bohmer event->bNotificationType, value); 117323cd1385SRemy Bohmer if (event->bNotificationType == 11746142e0aeSVitaly Kuzmichev USB_CDC_NOTIFY_SPEED_CHANGE) { 117523cd1385SRemy Bohmer l_ethdev.network_started = 1; 117623cd1385SRemy Bohmer printf("USB network up!\n"); 117723cd1385SRemy Bohmer } 117823cd1385SRemy Bohmer } 117923cd1385SRemy Bohmer req->context = NULL; 118023cd1385SRemy Bohmer } 118123cd1385SRemy Bohmer 118223cd1385SRemy Bohmer static void issue_start_status(struct eth_dev *dev) 118323cd1385SRemy Bohmer { 118423cd1385SRemy Bohmer struct usb_request *req = dev->stat_req; 118523cd1385SRemy Bohmer struct usb_cdc_notification *event; 118623cd1385SRemy Bohmer int value; 118723cd1385SRemy Bohmer 11886142e0aeSVitaly Kuzmichev /* 11896142e0aeSVitaly Kuzmichev * flush old status 119023cd1385SRemy Bohmer * 119123cd1385SRemy Bohmer * FIXME ugly idiom, maybe we'd be better with just 119223cd1385SRemy Bohmer * a "cancel the whole queue" primitive since any 119323cd1385SRemy Bohmer * unlink-one primitive has way too many error modes. 119423cd1385SRemy Bohmer * here, we "know" toggle is already clear... 119523cd1385SRemy Bohmer * 119623cd1385SRemy Bohmer * FIXME iff req->context != null just dequeue it 119723cd1385SRemy Bohmer */ 119823cd1385SRemy Bohmer usb_ep_disable(dev->status_ep); 119923cd1385SRemy Bohmer usb_ep_enable(dev->status_ep, dev->status); 120023cd1385SRemy Bohmer 12016142e0aeSVitaly Kuzmichev /* 12026142e0aeSVitaly Kuzmichev * 3.8.1 says to issue first NETWORK_CONNECTION, then 120323cd1385SRemy Bohmer * a SPEED_CHANGE. could be useful in some configs. 120423cd1385SRemy Bohmer */ 120523cd1385SRemy Bohmer event = req->buf; 120623cd1385SRemy Bohmer event->bmRequestType = 0xA1; 120723cd1385SRemy Bohmer event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION; 120823cd1385SRemy Bohmer event->wValue = __constant_cpu_to_le16(1); /* connected */ 120923cd1385SRemy Bohmer event->wIndex = __constant_cpu_to_le16(1); 121023cd1385SRemy Bohmer event->wLength = 0; 121123cd1385SRemy Bohmer 121223cd1385SRemy Bohmer req->length = sizeof *event; 121323cd1385SRemy Bohmer req->complete = eth_status_complete; 121423cd1385SRemy Bohmer req->context = dev; 121523cd1385SRemy Bohmer 121623cd1385SRemy Bohmer value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC); 121723cd1385SRemy Bohmer if (value < 0) 12187de73185SVitaly Kuzmichev debug("status buf queue --> %d\n", value); 121923cd1385SRemy Bohmer } 122023cd1385SRemy Bohmer 122123cd1385SRemy Bohmer #endif 122223cd1385SRemy Bohmer 122323cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 122423cd1385SRemy Bohmer 122523cd1385SRemy Bohmer static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req) 122623cd1385SRemy Bohmer { 122723cd1385SRemy Bohmer if (req->status || req->actual != req->length) 12287de73185SVitaly Kuzmichev debug("setup complete --> %d, %d/%d\n", 122923cd1385SRemy Bohmer req->status, req->actual, req->length); 123023cd1385SRemy Bohmer } 123123cd1385SRemy Bohmer 12327612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS 12337612a43dSVitaly Kuzmichev 12347612a43dSVitaly Kuzmichev static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req) 12357612a43dSVitaly Kuzmichev { 12367612a43dSVitaly Kuzmichev if (req->status || req->actual != req->length) 12377612a43dSVitaly Kuzmichev debug("rndis response complete --> %d, %d/%d\n", 12387612a43dSVitaly Kuzmichev req->status, req->actual, req->length); 12397612a43dSVitaly Kuzmichev 12407612a43dSVitaly Kuzmichev /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */ 12417612a43dSVitaly Kuzmichev } 12427612a43dSVitaly Kuzmichev 12437612a43dSVitaly Kuzmichev static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req) 12447612a43dSVitaly Kuzmichev { 12457612a43dSVitaly Kuzmichev struct eth_dev *dev = ep->driver_data; 12467612a43dSVitaly Kuzmichev int status; 12477612a43dSVitaly Kuzmichev 12487612a43dSVitaly Kuzmichev /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */ 12497612a43dSVitaly Kuzmichev status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf); 12507612a43dSVitaly Kuzmichev if (status < 0) 12517612a43dSVitaly Kuzmichev error("%s: rndis parse error %d", __func__, status); 12527612a43dSVitaly Kuzmichev } 12537612a43dSVitaly Kuzmichev 12547612a43dSVitaly Kuzmichev #endif /* RNDIS */ 12557612a43dSVitaly Kuzmichev 125623cd1385SRemy Bohmer /* 125723cd1385SRemy Bohmer * The setup() callback implements all the ep0 functionality that's not 125823cd1385SRemy Bohmer * handled lower down. CDC has a number of less-common features: 125923cd1385SRemy Bohmer * 126023cd1385SRemy Bohmer * - two interfaces: control, and ethernet data 126123cd1385SRemy Bohmer * - Ethernet data interface has two altsettings: default, and active 126223cd1385SRemy Bohmer * - class-specific descriptors for the control interface 126323cd1385SRemy Bohmer * - class-specific control requests 126423cd1385SRemy Bohmer */ 126523cd1385SRemy Bohmer static int 126623cd1385SRemy Bohmer eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) 126723cd1385SRemy Bohmer { 126823cd1385SRemy Bohmer struct eth_dev *dev = get_gadget_data(gadget); 126923cd1385SRemy Bohmer struct usb_request *req = dev->req; 127023cd1385SRemy Bohmer int value = -EOPNOTSUPP; 127123cd1385SRemy Bohmer u16 wIndex = le16_to_cpu(ctrl->wIndex); 127223cd1385SRemy Bohmer u16 wValue = le16_to_cpu(ctrl->wValue); 127323cd1385SRemy Bohmer u16 wLength = le16_to_cpu(ctrl->wLength); 127423cd1385SRemy Bohmer 12756142e0aeSVitaly Kuzmichev /* 12766142e0aeSVitaly Kuzmichev * descriptors just go into the pre-allocated ep0 buffer, 127723cd1385SRemy Bohmer * while config change events may enable network traffic. 127823cd1385SRemy Bohmer */ 127923cd1385SRemy Bohmer 12807de73185SVitaly Kuzmichev debug("%s\n", __func__); 128123cd1385SRemy Bohmer 128223cd1385SRemy Bohmer req->complete = eth_setup_complete; 128323cd1385SRemy Bohmer switch (ctrl->bRequest) { 128423cd1385SRemy Bohmer 128523cd1385SRemy Bohmer case USB_REQ_GET_DESCRIPTOR: 128623cd1385SRemy Bohmer if (ctrl->bRequestType != USB_DIR_IN) 128723cd1385SRemy Bohmer break; 128823cd1385SRemy Bohmer switch (wValue >> 8) { 128923cd1385SRemy Bohmer 129023cd1385SRemy Bohmer case USB_DT_DEVICE: 129123cd1385SRemy Bohmer value = min(wLength, (u16) sizeof device_desc); 129223cd1385SRemy Bohmer memcpy(req->buf, &device_desc, value); 129323cd1385SRemy Bohmer break; 129423cd1385SRemy Bohmer case USB_DT_DEVICE_QUALIFIER: 129523cd1385SRemy Bohmer if (!gadget_is_dualspeed(gadget)) 129623cd1385SRemy Bohmer break; 129723cd1385SRemy Bohmer value = min(wLength, (u16) sizeof dev_qualifier); 129823cd1385SRemy Bohmer memcpy(req->buf, &dev_qualifier, value); 129923cd1385SRemy Bohmer break; 130023cd1385SRemy Bohmer 130123cd1385SRemy Bohmer case USB_DT_OTHER_SPEED_CONFIG: 130223cd1385SRemy Bohmer if (!gadget_is_dualspeed(gadget)) 130323cd1385SRemy Bohmer break; 130423cd1385SRemy Bohmer /* FALLTHROUGH */ 130523cd1385SRemy Bohmer case USB_DT_CONFIG: 130623cd1385SRemy Bohmer value = config_buf(gadget, req->buf, 130723cd1385SRemy Bohmer wValue >> 8, 130823cd1385SRemy Bohmer wValue & 0xff, 130923cd1385SRemy Bohmer gadget_is_otg(gadget)); 131023cd1385SRemy Bohmer if (value >= 0) 131123cd1385SRemy Bohmer value = min(wLength, (u16) value); 131223cd1385SRemy Bohmer break; 131323cd1385SRemy Bohmer 131423cd1385SRemy Bohmer case USB_DT_STRING: 131523cd1385SRemy Bohmer value = usb_gadget_get_string(&stringtab, 131623cd1385SRemy Bohmer wValue & 0xff, req->buf); 131723cd1385SRemy Bohmer 131823cd1385SRemy Bohmer if (value >= 0) 131923cd1385SRemy Bohmer value = min(wLength, (u16) value); 132023cd1385SRemy Bohmer 132123cd1385SRemy Bohmer break; 132223cd1385SRemy Bohmer } 132323cd1385SRemy Bohmer break; 132423cd1385SRemy Bohmer 132523cd1385SRemy Bohmer case USB_REQ_SET_CONFIGURATION: 132623cd1385SRemy Bohmer if (ctrl->bRequestType != 0) 132723cd1385SRemy Bohmer break; 132823cd1385SRemy Bohmer if (gadget->a_hnp_support) 13297de73185SVitaly Kuzmichev debug("HNP available\n"); 133023cd1385SRemy Bohmer else if (gadget->a_alt_hnp_support) 13317de73185SVitaly Kuzmichev debug("HNP needs a different root port\n"); 133223cd1385SRemy Bohmer value = eth_set_config(dev, wValue, GFP_ATOMIC); 133323cd1385SRemy Bohmer break; 133423cd1385SRemy Bohmer case USB_REQ_GET_CONFIGURATION: 133523cd1385SRemy Bohmer if (ctrl->bRequestType != USB_DIR_IN) 133623cd1385SRemy Bohmer break; 133723cd1385SRemy Bohmer *(u8 *)req->buf = dev->config; 133823cd1385SRemy Bohmer value = min(wLength, (u16) 1); 133923cd1385SRemy Bohmer break; 134023cd1385SRemy Bohmer 134123cd1385SRemy Bohmer case USB_REQ_SET_INTERFACE: 134223cd1385SRemy Bohmer if (ctrl->bRequestType != USB_RECIP_INTERFACE 134323cd1385SRemy Bohmer || !dev->config 134423cd1385SRemy Bohmer || wIndex > 1) 134523cd1385SRemy Bohmer break; 134623cd1385SRemy Bohmer if (!cdc_active(dev) && wIndex != 0) 134723cd1385SRemy Bohmer break; 134823cd1385SRemy Bohmer 13496142e0aeSVitaly Kuzmichev /* 13506142e0aeSVitaly Kuzmichev * PXA hardware partially handles SET_INTERFACE; 135123cd1385SRemy Bohmer * we need to kluge around that interference. 135223cd1385SRemy Bohmer */ 135323cd1385SRemy Bohmer if (gadget_is_pxa(gadget)) { 135423cd1385SRemy Bohmer value = eth_set_config(dev, DEV_CONFIG_VALUE, 135523cd1385SRemy Bohmer GFP_ATOMIC); 1356563aed25SLukasz Dalek /* 1357563aed25SLukasz Dalek * PXA25x driver use non-CDC ethernet gadget. 1358563aed25SLukasz Dalek * But only _CDC and _RNDIS code can signalize 1359563aed25SLukasz Dalek * that network is working. So we signalize it 1360563aed25SLukasz Dalek * here. 1361563aed25SLukasz Dalek */ 1362563aed25SLukasz Dalek l_ethdev.network_started = 1; 1363563aed25SLukasz Dalek debug("USB network up!\n"); 136423cd1385SRemy Bohmer goto done_set_intf; 136523cd1385SRemy Bohmer } 136623cd1385SRemy Bohmer 13672bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_CDC 136823cd1385SRemy Bohmer switch (wIndex) { 136923cd1385SRemy Bohmer case 0: /* control/master intf */ 137023cd1385SRemy Bohmer if (wValue != 0) 137123cd1385SRemy Bohmer break; 137223cd1385SRemy Bohmer if (dev->status) { 137323cd1385SRemy Bohmer usb_ep_disable(dev->status_ep); 137423cd1385SRemy Bohmer usb_ep_enable(dev->status_ep, dev->status); 137523cd1385SRemy Bohmer } 13767612a43dSVitaly Kuzmichev 137723cd1385SRemy Bohmer value = 0; 137823cd1385SRemy Bohmer break; 137923cd1385SRemy Bohmer case 1: /* data intf */ 138023cd1385SRemy Bohmer if (wValue > 1) 138123cd1385SRemy Bohmer break; 138223cd1385SRemy Bohmer usb_ep_disable(dev->in_ep); 138323cd1385SRemy Bohmer usb_ep_disable(dev->out_ep); 138423cd1385SRemy Bohmer 13856142e0aeSVitaly Kuzmichev /* 13866142e0aeSVitaly Kuzmichev * CDC requires the data transfers not be done from 138723cd1385SRemy Bohmer * the default interface setting ... also, setting 138823cd1385SRemy Bohmer * the non-default interface resets filters etc. 138923cd1385SRemy Bohmer */ 139023cd1385SRemy Bohmer if (wValue == 1) { 139123cd1385SRemy Bohmer if (!cdc_active(dev)) 139223cd1385SRemy Bohmer break; 139323cd1385SRemy Bohmer usb_ep_enable(dev->in_ep, dev->in); 139423cd1385SRemy Bohmer usb_ep_enable(dev->out_ep, dev->out); 139523cd1385SRemy Bohmer dev->cdc_filter = DEFAULT_FILTER; 139623cd1385SRemy Bohmer if (dev->status) 139723cd1385SRemy Bohmer issue_start_status(dev); 13987612a43dSVitaly Kuzmichev eth_start(dev, GFP_ATOMIC); 139923cd1385SRemy Bohmer } 140023cd1385SRemy Bohmer value = 0; 140123cd1385SRemy Bohmer break; 140223cd1385SRemy Bohmer } 140323cd1385SRemy Bohmer #else 14046142e0aeSVitaly Kuzmichev /* 14056142e0aeSVitaly Kuzmichev * FIXME this is wrong, as is the assumption that 140623cd1385SRemy Bohmer * all non-PXA hardware talks real CDC ... 140723cd1385SRemy Bohmer */ 14087de73185SVitaly Kuzmichev debug("set_interface ignored!\n"); 14092bb37884SLukasz Dalek #endif /* CONFIG_USB_ETH_CDC */ 141023cd1385SRemy Bohmer 141123cd1385SRemy Bohmer done_set_intf: 141223cd1385SRemy Bohmer break; 141323cd1385SRemy Bohmer case USB_REQ_GET_INTERFACE: 141423cd1385SRemy Bohmer if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE) 141523cd1385SRemy Bohmer || !dev->config 141623cd1385SRemy Bohmer || wIndex > 1) 141723cd1385SRemy Bohmer break; 14187612a43dSVitaly Kuzmichev if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0) 141923cd1385SRemy Bohmer break; 142023cd1385SRemy Bohmer 142123cd1385SRemy Bohmer /* for CDC, iff carrier is on, data interface is active. */ 14227612a43dSVitaly Kuzmichev if (rndis_active(dev) || wIndex != 1) 142323cd1385SRemy Bohmer *(u8 *)req->buf = 0; 142423cd1385SRemy Bohmer else { 142523cd1385SRemy Bohmer /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */ 142623cd1385SRemy Bohmer /* carrier always ok ...*/ 142723cd1385SRemy Bohmer *(u8 *)req->buf = 1 ; 142823cd1385SRemy Bohmer } 142923cd1385SRemy Bohmer value = min(wLength, (u16) 1); 143023cd1385SRemy Bohmer break; 143123cd1385SRemy Bohmer 14322bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_CDC 143323cd1385SRemy Bohmer case USB_CDC_SET_ETHERNET_PACKET_FILTER: 14346142e0aeSVitaly Kuzmichev /* 14356142e0aeSVitaly Kuzmichev * see 6.2.30: no data, wIndex = interface, 143623cd1385SRemy Bohmer * wValue = packet filter bitmap 143723cd1385SRemy Bohmer */ 143823cd1385SRemy Bohmer if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE) 143923cd1385SRemy Bohmer || !cdc_active(dev) 144023cd1385SRemy Bohmer || wLength != 0 144123cd1385SRemy Bohmer || wIndex > 1) 144223cd1385SRemy Bohmer break; 14437de73185SVitaly Kuzmichev debug("packet filter %02x\n", wValue); 144423cd1385SRemy Bohmer dev->cdc_filter = wValue; 144523cd1385SRemy Bohmer value = 0; 144623cd1385SRemy Bohmer break; 144723cd1385SRemy Bohmer 14486142e0aeSVitaly Kuzmichev /* 14496142e0aeSVitaly Kuzmichev * and potentially: 145023cd1385SRemy Bohmer * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS: 145123cd1385SRemy Bohmer * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER: 145223cd1385SRemy Bohmer * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER: 145323cd1385SRemy Bohmer * case USB_CDC_GET_ETHERNET_STATISTIC: 145423cd1385SRemy Bohmer */ 145523cd1385SRemy Bohmer 14562bb37884SLukasz Dalek #endif /* CONFIG_USB_ETH_CDC */ 145723cd1385SRemy Bohmer 14587612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS 14597612a43dSVitaly Kuzmichev /* 14607612a43dSVitaly Kuzmichev * RNDIS uses the CDC command encapsulation mechanism to implement 14617612a43dSVitaly Kuzmichev * an RPC scheme, with much getting/setting of attributes by OID. 14627612a43dSVitaly Kuzmichev */ 14637612a43dSVitaly Kuzmichev case USB_CDC_SEND_ENCAPSULATED_COMMAND: 14647612a43dSVitaly Kuzmichev if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE) 14657612a43dSVitaly Kuzmichev || !rndis_active(dev) 14667612a43dSVitaly Kuzmichev || wLength > USB_BUFSIZ 14677612a43dSVitaly Kuzmichev || wValue 14687612a43dSVitaly Kuzmichev || rndis_control_intf.bInterfaceNumber 14697612a43dSVitaly Kuzmichev != wIndex) 14707612a43dSVitaly Kuzmichev break; 14717612a43dSVitaly Kuzmichev /* read the request, then process it */ 14727612a43dSVitaly Kuzmichev value = wLength; 14737612a43dSVitaly Kuzmichev req->complete = rndis_command_complete; 14747612a43dSVitaly Kuzmichev /* later, rndis_control_ack () sends a notification */ 14757612a43dSVitaly Kuzmichev break; 14767612a43dSVitaly Kuzmichev 14777612a43dSVitaly Kuzmichev case USB_CDC_GET_ENCAPSULATED_RESPONSE: 14787612a43dSVitaly Kuzmichev if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE) 14797612a43dSVitaly Kuzmichev == ctrl->bRequestType 14807612a43dSVitaly Kuzmichev && rndis_active(dev) 14817612a43dSVitaly Kuzmichev /* && wLength >= 0x0400 */ 14827612a43dSVitaly Kuzmichev && !wValue 14837612a43dSVitaly Kuzmichev && rndis_control_intf.bInterfaceNumber 14847612a43dSVitaly Kuzmichev == wIndex) { 14857612a43dSVitaly Kuzmichev u8 *buf; 14867612a43dSVitaly Kuzmichev u32 n; 14877612a43dSVitaly Kuzmichev 14887612a43dSVitaly Kuzmichev /* return the result */ 14897612a43dSVitaly Kuzmichev buf = rndis_get_next_response(dev->rndis_config, &n); 14907612a43dSVitaly Kuzmichev if (buf) { 14917612a43dSVitaly Kuzmichev memcpy(req->buf, buf, n); 14927612a43dSVitaly Kuzmichev req->complete = rndis_response_complete; 14937612a43dSVitaly Kuzmichev rndis_free_response(dev->rndis_config, buf); 14947612a43dSVitaly Kuzmichev value = n; 14957612a43dSVitaly Kuzmichev } 14967612a43dSVitaly Kuzmichev /* else stalls ... spec says to avoid that */ 14977612a43dSVitaly Kuzmichev } 14987612a43dSVitaly Kuzmichev break; 14997612a43dSVitaly Kuzmichev #endif /* RNDIS */ 15007612a43dSVitaly Kuzmichev 150123cd1385SRemy Bohmer default: 15027de73185SVitaly Kuzmichev debug("unknown control req%02x.%02x v%04x i%04x l%d\n", 150323cd1385SRemy Bohmer ctrl->bRequestType, ctrl->bRequest, 150423cd1385SRemy Bohmer wValue, wIndex, wLength); 150523cd1385SRemy Bohmer } 150623cd1385SRemy Bohmer 150723cd1385SRemy Bohmer /* respond with data transfer before status phase? */ 150823cd1385SRemy Bohmer if (value >= 0) { 15097de73185SVitaly Kuzmichev debug("respond with data transfer before status phase\n"); 151023cd1385SRemy Bohmer req->length = value; 151123cd1385SRemy Bohmer req->zero = value < wLength 151223cd1385SRemy Bohmer && (value % gadget->ep0->maxpacket) == 0; 151323cd1385SRemy Bohmer value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC); 151423cd1385SRemy Bohmer if (value < 0) { 15157de73185SVitaly Kuzmichev debug("ep_queue --> %d\n", value); 151623cd1385SRemy Bohmer req->status = 0; 151723cd1385SRemy Bohmer eth_setup_complete(gadget->ep0, req); 151823cd1385SRemy Bohmer } 151923cd1385SRemy Bohmer } 152023cd1385SRemy Bohmer 152123cd1385SRemy Bohmer /* host either stalls (value < 0) or reports success */ 152223cd1385SRemy Bohmer return value; 152323cd1385SRemy Bohmer } 152423cd1385SRemy Bohmer 152523cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 152623cd1385SRemy Bohmer 152723cd1385SRemy Bohmer static void rx_complete(struct usb_ep *ep, struct usb_request *req); 152823cd1385SRemy Bohmer 15296142e0aeSVitaly Kuzmichev static int rx_submit(struct eth_dev *dev, struct usb_request *req, 153023cd1385SRemy Bohmer gfp_t gfp_flags) 153123cd1385SRemy Bohmer { 153223cd1385SRemy Bohmer int retval = -ENOMEM; 153323cd1385SRemy Bohmer size_t size; 153423cd1385SRemy Bohmer 15356142e0aeSVitaly Kuzmichev /* 15366142e0aeSVitaly Kuzmichev * Padding up to RX_EXTRA handles minor disagreements with host. 153723cd1385SRemy Bohmer * Normally we use the USB "terminate on short read" convention; 153823cd1385SRemy Bohmer * so allow up to (N*maxpacket), since that memory is normally 153923cd1385SRemy Bohmer * already allocated. Some hardware doesn't deal well with short 154023cd1385SRemy Bohmer * reads (e.g. DMA must be N*maxpacket), so for now don't trim a 154123cd1385SRemy Bohmer * byte off the end (to force hardware errors on overflow). 15427612a43dSVitaly Kuzmichev * 15437612a43dSVitaly Kuzmichev * RNDIS uses internal framing, and explicitly allows senders to 15447612a43dSVitaly Kuzmichev * pad to end-of-packet. That's potentially nice for speed, 15457612a43dSVitaly Kuzmichev * but means receivers can't recover synch on their own. 154623cd1385SRemy Bohmer */ 154723cd1385SRemy Bohmer 15487de73185SVitaly Kuzmichev debug("%s\n", __func__); 154923cd1385SRemy Bohmer 155023cd1385SRemy Bohmer size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA); 155123cd1385SRemy Bohmer size += dev->out_ep->maxpacket - 1; 15527612a43dSVitaly Kuzmichev if (rndis_active(dev)) 15537612a43dSVitaly Kuzmichev size += sizeof(struct rndis_packet_msg_type); 155423cd1385SRemy Bohmer size -= size % dev->out_ep->maxpacket; 155523cd1385SRemy Bohmer 15566142e0aeSVitaly Kuzmichev /* 15576142e0aeSVitaly Kuzmichev * Some platforms perform better when IP packets are aligned, 15587612a43dSVitaly Kuzmichev * but on at least one, checksumming fails otherwise. Note: 15597612a43dSVitaly Kuzmichev * RNDIS headers involve variable numbers of LE32 values. 156023cd1385SRemy Bohmer */ 156123cd1385SRemy Bohmer 156223cd1385SRemy Bohmer req->buf = (u8 *) NetRxPackets[0]; 156323cd1385SRemy Bohmer req->length = size; 156423cd1385SRemy Bohmer req->complete = rx_complete; 156523cd1385SRemy Bohmer 156623cd1385SRemy Bohmer retval = usb_ep_queue(dev->out_ep, req, gfp_flags); 156723cd1385SRemy Bohmer 15686142e0aeSVitaly Kuzmichev if (retval) 15697de73185SVitaly Kuzmichev error("rx submit --> %d", retval); 15706142e0aeSVitaly Kuzmichev 157123cd1385SRemy Bohmer return retval; 157223cd1385SRemy Bohmer } 157323cd1385SRemy Bohmer 157423cd1385SRemy Bohmer static void rx_complete(struct usb_ep *ep, struct usb_request *req) 157523cd1385SRemy Bohmer { 157623cd1385SRemy Bohmer struct eth_dev *dev = ep->driver_data; 157723cd1385SRemy Bohmer 15787de73185SVitaly Kuzmichev debug("%s: status %d\n", __func__, req->status); 1579c85d70efSVitaly Kuzmichev switch (req->status) { 1580c85d70efSVitaly Kuzmichev /* normal completion */ 1581c85d70efSVitaly Kuzmichev case 0: 15827612a43dSVitaly Kuzmichev if (rndis_active(dev)) { 15837612a43dSVitaly Kuzmichev /* we know MaxPacketsPerTransfer == 1 here */ 15847612a43dSVitaly Kuzmichev int length = rndis_rm_hdr(req->buf, req->actual); 15857612a43dSVitaly Kuzmichev if (length < 0) 15867612a43dSVitaly Kuzmichev goto length_err; 15877612a43dSVitaly Kuzmichev req->length -= length; 15887612a43dSVitaly Kuzmichev req->actual -= length; 15897612a43dSVitaly Kuzmichev } 15907612a43dSVitaly Kuzmichev if (req->actual < ETH_HLEN || ETH_FRAME_LEN < req->actual) { 15917612a43dSVitaly Kuzmichev length_err: 15927612a43dSVitaly Kuzmichev dev->stats.rx_errors++; 15937612a43dSVitaly Kuzmichev dev->stats.rx_length_errors++; 15947612a43dSVitaly Kuzmichev debug("rx length %d\n", req->length); 15957612a43dSVitaly Kuzmichev break; 15967612a43dSVitaly Kuzmichev } 15977612a43dSVitaly Kuzmichev 1598c85d70efSVitaly Kuzmichev dev->stats.rx_packets++; 1599c85d70efSVitaly Kuzmichev dev->stats.rx_bytes += req->length; 1600c85d70efSVitaly Kuzmichev break; 1601c85d70efSVitaly Kuzmichev 1602c85d70efSVitaly Kuzmichev /* software-driven interface shutdown */ 1603c85d70efSVitaly Kuzmichev case -ECONNRESET: /* unlink */ 1604c85d70efSVitaly Kuzmichev case -ESHUTDOWN: /* disconnect etc */ 1605c85d70efSVitaly Kuzmichev /* for hardware automagic (such as pxa) */ 1606c85d70efSVitaly Kuzmichev case -ECONNABORTED: /* endpoint reset */ 1607c85d70efSVitaly Kuzmichev break; 1608c85d70efSVitaly Kuzmichev 1609c85d70efSVitaly Kuzmichev /* data overrun */ 1610c85d70efSVitaly Kuzmichev case -EOVERFLOW: 1611c85d70efSVitaly Kuzmichev dev->stats.rx_over_errors++; 1612c85d70efSVitaly Kuzmichev /* FALLTHROUGH */ 1613c85d70efSVitaly Kuzmichev default: 1614c85d70efSVitaly Kuzmichev dev->stats.rx_errors++; 1615c85d70efSVitaly Kuzmichev break; 1616c85d70efSVitaly Kuzmichev } 161723cd1385SRemy Bohmer 161823cd1385SRemy Bohmer packet_received = 1; 161923cd1385SRemy Bohmer } 162023cd1385SRemy Bohmer 162123cd1385SRemy Bohmer static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags) 162223cd1385SRemy Bohmer { 162323cd1385SRemy Bohmer 162423cd1385SRemy Bohmer dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0); 162523cd1385SRemy Bohmer 162623cd1385SRemy Bohmer if (!dev->tx_req) 1627ac5d32d1SVitaly Kuzmichev goto fail1; 162823cd1385SRemy Bohmer 162923cd1385SRemy Bohmer dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0); 163023cd1385SRemy Bohmer 163123cd1385SRemy Bohmer if (!dev->rx_req) 1632ac5d32d1SVitaly Kuzmichev goto fail2; 163323cd1385SRemy Bohmer 163423cd1385SRemy Bohmer return 0; 163523cd1385SRemy Bohmer 1636ac5d32d1SVitaly Kuzmichev fail2: 1637ac5d32d1SVitaly Kuzmichev usb_ep_free_request(dev->in_ep, dev->tx_req); 1638ac5d32d1SVitaly Kuzmichev fail1: 16397de73185SVitaly Kuzmichev error("can't alloc requests"); 164023cd1385SRemy Bohmer return -1; 164123cd1385SRemy Bohmer } 164223cd1385SRemy Bohmer 164323cd1385SRemy Bohmer static void tx_complete(struct usb_ep *ep, struct usb_request *req) 164423cd1385SRemy Bohmer { 1645c85d70efSVitaly Kuzmichev struct eth_dev *dev = ep->driver_data; 1646c85d70efSVitaly Kuzmichev 16477de73185SVitaly Kuzmichev debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok"); 1648c85d70efSVitaly Kuzmichev switch (req->status) { 1649c85d70efSVitaly Kuzmichev default: 1650c85d70efSVitaly Kuzmichev dev->stats.tx_errors++; 1651c85d70efSVitaly Kuzmichev debug("tx err %d\n", req->status); 1652c85d70efSVitaly Kuzmichev /* FALLTHROUGH */ 1653c85d70efSVitaly Kuzmichev case -ECONNRESET: /* unlink */ 1654c85d70efSVitaly Kuzmichev case -ESHUTDOWN: /* disconnect etc */ 1655c85d70efSVitaly Kuzmichev break; 1656c85d70efSVitaly Kuzmichev case 0: 1657c85d70efSVitaly Kuzmichev dev->stats.tx_bytes += req->length; 1658c85d70efSVitaly Kuzmichev } 1659c85d70efSVitaly Kuzmichev dev->stats.tx_packets++; 1660c85d70efSVitaly Kuzmichev 166123cd1385SRemy Bohmer packet_sent = 1; 166223cd1385SRemy Bohmer } 166323cd1385SRemy Bohmer 166423cd1385SRemy Bohmer static inline int eth_is_promisc(struct eth_dev *dev) 166523cd1385SRemy Bohmer { 166623cd1385SRemy Bohmer /* no filters for the CDC subset; always promisc */ 166723cd1385SRemy Bohmer if (subset_active(dev)) 166823cd1385SRemy Bohmer return 1; 166923cd1385SRemy Bohmer return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS; 167023cd1385SRemy Bohmer } 167123cd1385SRemy Bohmer 167223cd1385SRemy Bohmer #if 0 167323cd1385SRemy Bohmer static int eth_start_xmit (struct sk_buff *skb, struct net_device *net) 167423cd1385SRemy Bohmer { 167523cd1385SRemy Bohmer struct eth_dev *dev = netdev_priv(net); 167623cd1385SRemy Bohmer int length = skb->len; 167723cd1385SRemy Bohmer int retval; 167823cd1385SRemy Bohmer struct usb_request *req = NULL; 167923cd1385SRemy Bohmer unsigned long flags; 168023cd1385SRemy Bohmer 168123cd1385SRemy Bohmer /* apply outgoing CDC or RNDIS filters */ 168223cd1385SRemy Bohmer if (!eth_is_promisc (dev)) { 168323cd1385SRemy Bohmer u8 *dest = skb->data; 168423cd1385SRemy Bohmer 168523cd1385SRemy Bohmer if (is_multicast_ether_addr(dest)) { 168623cd1385SRemy Bohmer u16 type; 168723cd1385SRemy Bohmer 168823cd1385SRemy Bohmer /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host 168923cd1385SRemy Bohmer * SET_ETHERNET_MULTICAST_FILTERS requests 169023cd1385SRemy Bohmer */ 169123cd1385SRemy Bohmer if (is_broadcast_ether_addr(dest)) 169223cd1385SRemy Bohmer type = USB_CDC_PACKET_TYPE_BROADCAST; 169323cd1385SRemy Bohmer else 169423cd1385SRemy Bohmer type = USB_CDC_PACKET_TYPE_ALL_MULTICAST; 169523cd1385SRemy Bohmer if (!(dev->cdc_filter & type)) { 169623cd1385SRemy Bohmer dev_kfree_skb_any (skb); 169723cd1385SRemy Bohmer return 0; 169823cd1385SRemy Bohmer } 169923cd1385SRemy Bohmer } 170023cd1385SRemy Bohmer /* ignores USB_CDC_PACKET_TYPE_DIRECTED */ 170123cd1385SRemy Bohmer } 170223cd1385SRemy Bohmer 170323cd1385SRemy Bohmer spin_lock_irqsave(&dev->req_lock, flags); 170423cd1385SRemy Bohmer /* 170523cd1385SRemy Bohmer * this freelist can be empty if an interrupt triggered disconnect() 170623cd1385SRemy Bohmer * and reconfigured the gadget (shutting down this queue) after the 170723cd1385SRemy Bohmer * network stack decided to xmit but before we got the spinlock. 170823cd1385SRemy Bohmer */ 170923cd1385SRemy Bohmer if (list_empty(&dev->tx_reqs)) { 171023cd1385SRemy Bohmer spin_unlock_irqrestore(&dev->req_lock, flags); 171123cd1385SRemy Bohmer return 1; 171223cd1385SRemy Bohmer } 171323cd1385SRemy Bohmer 171423cd1385SRemy Bohmer req = container_of (dev->tx_reqs.next, struct usb_request, list); 171523cd1385SRemy Bohmer list_del (&req->list); 171623cd1385SRemy Bohmer 171723cd1385SRemy Bohmer /* temporarily stop TX queue when the freelist empties */ 171823cd1385SRemy Bohmer if (list_empty (&dev->tx_reqs)) 171923cd1385SRemy Bohmer netif_stop_queue (net); 172023cd1385SRemy Bohmer spin_unlock_irqrestore(&dev->req_lock, flags); 172123cd1385SRemy Bohmer 172223cd1385SRemy Bohmer /* no buffer copies needed, unless the network stack did it 172323cd1385SRemy Bohmer * or the hardware can't use skb buffers. 172423cd1385SRemy Bohmer * or there's not enough space for any RNDIS headers we need 172523cd1385SRemy Bohmer */ 172623cd1385SRemy Bohmer if (rndis_active(dev)) { 172723cd1385SRemy Bohmer struct sk_buff *skb_rndis; 172823cd1385SRemy Bohmer 172923cd1385SRemy Bohmer skb_rndis = skb_realloc_headroom (skb, 173023cd1385SRemy Bohmer sizeof (struct rndis_packet_msg_type)); 173123cd1385SRemy Bohmer if (!skb_rndis) 173223cd1385SRemy Bohmer goto drop; 173323cd1385SRemy Bohmer 173423cd1385SRemy Bohmer dev_kfree_skb_any (skb); 173523cd1385SRemy Bohmer skb = skb_rndis; 173623cd1385SRemy Bohmer rndis_add_hdr (skb); 173723cd1385SRemy Bohmer length = skb->len; 173823cd1385SRemy Bohmer } 173923cd1385SRemy Bohmer req->buf = skb->data; 174023cd1385SRemy Bohmer req->context = skb; 174123cd1385SRemy Bohmer req->complete = tx_complete; 174223cd1385SRemy Bohmer 174323cd1385SRemy Bohmer /* use zlp framing on tx for strict CDC-Ether conformance, 174423cd1385SRemy Bohmer * though any robust network rx path ignores extra padding. 174523cd1385SRemy Bohmer * and some hardware doesn't like to write zlps. 174623cd1385SRemy Bohmer */ 174723cd1385SRemy Bohmer req->zero = 1; 174823cd1385SRemy Bohmer if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0) 174923cd1385SRemy Bohmer length++; 175023cd1385SRemy Bohmer 175123cd1385SRemy Bohmer req->length = length; 175223cd1385SRemy Bohmer 175323cd1385SRemy Bohmer /* throttle highspeed IRQ rate back slightly */ 175423cd1385SRemy Bohmer if (gadget_is_dualspeed(dev->gadget)) 175523cd1385SRemy Bohmer req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH) 175623cd1385SRemy Bohmer ? ((atomic_read(&dev->tx_qlen) % qmult) != 0) 175723cd1385SRemy Bohmer : 0; 175823cd1385SRemy Bohmer 175923cd1385SRemy Bohmer retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC); 176023cd1385SRemy Bohmer switch (retval) { 176123cd1385SRemy Bohmer default: 176223cd1385SRemy Bohmer DEBUG (dev, "tx queue err %d\n", retval); 176323cd1385SRemy Bohmer break; 176423cd1385SRemy Bohmer case 0: 176523cd1385SRemy Bohmer net->trans_start = jiffies; 176623cd1385SRemy Bohmer atomic_inc (&dev->tx_qlen); 176723cd1385SRemy Bohmer } 176823cd1385SRemy Bohmer 176923cd1385SRemy Bohmer if (retval) { 177023cd1385SRemy Bohmer drop: 177123cd1385SRemy Bohmer dev->stats.tx_dropped++; 177223cd1385SRemy Bohmer dev_kfree_skb_any (skb); 177323cd1385SRemy Bohmer spin_lock_irqsave(&dev->req_lock, flags); 177423cd1385SRemy Bohmer if (list_empty (&dev->tx_reqs)) 177523cd1385SRemy Bohmer netif_start_queue (net); 177623cd1385SRemy Bohmer list_add (&req->list, &dev->tx_reqs); 177723cd1385SRemy Bohmer spin_unlock_irqrestore(&dev->req_lock, flags); 177823cd1385SRemy Bohmer } 177923cd1385SRemy Bohmer return 0; 178023cd1385SRemy Bohmer } 178123cd1385SRemy Bohmer 178223cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 178323cd1385SRemy Bohmer #endif 178423cd1385SRemy Bohmer 178523cd1385SRemy Bohmer static void eth_unbind(struct usb_gadget *gadget) 178623cd1385SRemy Bohmer { 178723cd1385SRemy Bohmer struct eth_dev *dev = get_gadget_data(gadget); 178823cd1385SRemy Bohmer 17897de73185SVitaly Kuzmichev debug("%s...\n", __func__); 17907612a43dSVitaly Kuzmichev rndis_deregister(dev->rndis_config); 17917612a43dSVitaly Kuzmichev rndis_exit(); 179223cd1385SRemy Bohmer 17930129e327SVitaly Kuzmichev /* we've already been disconnected ... no i/o is active */ 17940129e327SVitaly Kuzmichev if (dev->req) { 17950129e327SVitaly Kuzmichev usb_ep_free_request(gadget->ep0, dev->req); 17960129e327SVitaly Kuzmichev dev->req = NULL; 17970129e327SVitaly Kuzmichev } 179823cd1385SRemy Bohmer if (dev->stat_req) { 179923cd1385SRemy Bohmer usb_ep_free_request(dev->status_ep, dev->stat_req); 180023cd1385SRemy Bohmer dev->stat_req = NULL; 180123cd1385SRemy Bohmer } 180223cd1385SRemy Bohmer 180323cd1385SRemy Bohmer if (dev->tx_req) { 180423cd1385SRemy Bohmer usb_ep_free_request(dev->in_ep, dev->tx_req); 180523cd1385SRemy Bohmer dev->tx_req = NULL; 180623cd1385SRemy Bohmer } 180723cd1385SRemy Bohmer 180823cd1385SRemy Bohmer if (dev->rx_req) { 18090129e327SVitaly Kuzmichev usb_ep_free_request(dev->out_ep, dev->rx_req); 181023cd1385SRemy Bohmer dev->rx_req = NULL; 181123cd1385SRemy Bohmer } 181223cd1385SRemy Bohmer 181323cd1385SRemy Bohmer /* unregister_netdev (dev->net);*/ 181423cd1385SRemy Bohmer /* free_netdev(dev->net);*/ 181523cd1385SRemy Bohmer 1816988ee3e3SLei Wen dev->gadget = NULL; 181723cd1385SRemy Bohmer set_gadget_data(gadget, NULL); 181823cd1385SRemy Bohmer } 181923cd1385SRemy Bohmer 182023cd1385SRemy Bohmer static void eth_disconnect(struct usb_gadget *gadget) 182123cd1385SRemy Bohmer { 182223cd1385SRemy Bohmer eth_reset_config(get_gadget_data(gadget)); 18237612a43dSVitaly Kuzmichev /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */ 182423cd1385SRemy Bohmer } 182523cd1385SRemy Bohmer 182623cd1385SRemy Bohmer static void eth_suspend(struct usb_gadget *gadget) 182723cd1385SRemy Bohmer { 182823cd1385SRemy Bohmer /* Not used */ 182923cd1385SRemy Bohmer } 183023cd1385SRemy Bohmer 183123cd1385SRemy Bohmer static void eth_resume(struct usb_gadget *gadget) 183223cd1385SRemy Bohmer { 183323cd1385SRemy Bohmer /* Not used */ 183423cd1385SRemy Bohmer } 183523cd1385SRemy Bohmer 183623cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 183723cd1385SRemy Bohmer 18387612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS 18397612a43dSVitaly Kuzmichev 18407612a43dSVitaly Kuzmichev /* 18417612a43dSVitaly Kuzmichev * The interrupt endpoint is used in RNDIS to notify the host when messages 18427612a43dSVitaly Kuzmichev * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT 18437612a43dSVitaly Kuzmichev * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even 18447612a43dSVitaly Kuzmichev * REMOTE_NDIS_KEEPALIVE_MSG. 18457612a43dSVitaly Kuzmichev * 18467612a43dSVitaly Kuzmichev * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and 18477612a43dSVitaly Kuzmichev * normally just one notification will be queued. 18487612a43dSVitaly Kuzmichev */ 18497612a43dSVitaly Kuzmichev 18507612a43dSVitaly Kuzmichev static void rndis_control_ack_complete(struct usb_ep *ep, 18517612a43dSVitaly Kuzmichev struct usb_request *req) 18527612a43dSVitaly Kuzmichev { 18537612a43dSVitaly Kuzmichev struct eth_dev *dev = ep->driver_data; 18547612a43dSVitaly Kuzmichev 18557612a43dSVitaly Kuzmichev debug("%s...\n", __func__); 18567612a43dSVitaly Kuzmichev if (req->status || req->actual != req->length) 18577612a43dSVitaly Kuzmichev debug("rndis control ack complete --> %d, %d/%d\n", 18587612a43dSVitaly Kuzmichev req->status, req->actual, req->length); 18597612a43dSVitaly Kuzmichev 18607612a43dSVitaly Kuzmichev if (!l_ethdev.network_started) { 18617612a43dSVitaly Kuzmichev if (rndis_get_state(dev->rndis_config) 18627612a43dSVitaly Kuzmichev == RNDIS_DATA_INITIALIZED) { 18637612a43dSVitaly Kuzmichev l_ethdev.network_started = 1; 18647612a43dSVitaly Kuzmichev printf("USB RNDIS network up!\n"); 18657612a43dSVitaly Kuzmichev } 18667612a43dSVitaly Kuzmichev } 18677612a43dSVitaly Kuzmichev 18687612a43dSVitaly Kuzmichev req->context = NULL; 18697612a43dSVitaly Kuzmichev 18707612a43dSVitaly Kuzmichev if (req != dev->stat_req) 18717612a43dSVitaly Kuzmichev usb_ep_free_request(ep, req); 18727612a43dSVitaly Kuzmichev } 18737612a43dSVitaly Kuzmichev 18747612a43dSVitaly Kuzmichev static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32)))); 18757612a43dSVitaly Kuzmichev 18767612a43dSVitaly Kuzmichev static int rndis_control_ack(struct eth_device *net) 18777612a43dSVitaly Kuzmichev { 18787612a43dSVitaly Kuzmichev struct eth_dev *dev = &l_ethdev; 18797612a43dSVitaly Kuzmichev int length; 18807612a43dSVitaly Kuzmichev struct usb_request *resp = dev->stat_req; 18817612a43dSVitaly Kuzmichev 18827612a43dSVitaly Kuzmichev /* in case RNDIS calls this after disconnect */ 18837612a43dSVitaly Kuzmichev if (!dev->status) { 18847612a43dSVitaly Kuzmichev debug("status ENODEV\n"); 18857612a43dSVitaly Kuzmichev return -ENODEV; 18867612a43dSVitaly Kuzmichev } 18877612a43dSVitaly Kuzmichev 18887612a43dSVitaly Kuzmichev /* in case queue length > 1 */ 18897612a43dSVitaly Kuzmichev if (resp->context) { 18907612a43dSVitaly Kuzmichev resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC); 18917612a43dSVitaly Kuzmichev if (!resp) 18927612a43dSVitaly Kuzmichev return -ENOMEM; 18937612a43dSVitaly Kuzmichev resp->buf = rndis_resp_buf; 18947612a43dSVitaly Kuzmichev } 18957612a43dSVitaly Kuzmichev 18967612a43dSVitaly Kuzmichev /* 18977612a43dSVitaly Kuzmichev * Send RNDIS RESPONSE_AVAILABLE notification; 18987612a43dSVitaly Kuzmichev * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too 18997612a43dSVitaly Kuzmichev */ 19007612a43dSVitaly Kuzmichev resp->length = 8; 19017612a43dSVitaly Kuzmichev resp->complete = rndis_control_ack_complete; 19027612a43dSVitaly Kuzmichev resp->context = dev; 19037612a43dSVitaly Kuzmichev 19047612a43dSVitaly Kuzmichev *((__le32 *) resp->buf) = __constant_cpu_to_le32(1); 19057612a43dSVitaly Kuzmichev *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0); 19067612a43dSVitaly Kuzmichev 19077612a43dSVitaly Kuzmichev length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC); 19087612a43dSVitaly Kuzmichev if (length < 0) { 19097612a43dSVitaly Kuzmichev resp->status = 0; 19107612a43dSVitaly Kuzmichev rndis_control_ack_complete(dev->status_ep, resp); 19117612a43dSVitaly Kuzmichev } 19127612a43dSVitaly Kuzmichev 19137612a43dSVitaly Kuzmichev return 0; 19147612a43dSVitaly Kuzmichev } 19157612a43dSVitaly Kuzmichev 19167612a43dSVitaly Kuzmichev #else 19177612a43dSVitaly Kuzmichev 19187612a43dSVitaly Kuzmichev #define rndis_control_ack NULL 19197612a43dSVitaly Kuzmichev 19207612a43dSVitaly Kuzmichev #endif /* RNDIS */ 19217612a43dSVitaly Kuzmichev 19227612a43dSVitaly Kuzmichev static void eth_start(struct eth_dev *dev, gfp_t gfp_flags) 19237612a43dSVitaly Kuzmichev { 19247612a43dSVitaly Kuzmichev if (rndis_active(dev)) { 19257612a43dSVitaly Kuzmichev rndis_set_param_medium(dev->rndis_config, 19267612a43dSVitaly Kuzmichev NDIS_MEDIUM_802_3, 19277612a43dSVitaly Kuzmichev BITRATE(dev->gadget)/100); 19287612a43dSVitaly Kuzmichev rndis_signal_connect(dev->rndis_config); 19297612a43dSVitaly Kuzmichev } 19307612a43dSVitaly Kuzmichev } 19317612a43dSVitaly Kuzmichev 19327612a43dSVitaly Kuzmichev static int eth_stop(struct eth_dev *dev) 19337612a43dSVitaly Kuzmichev { 1934e4ae6660SVitaly Kuzmichev #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT 1935e4ae6660SVitaly Kuzmichev unsigned long ts; 1936e4ae6660SVitaly Kuzmichev unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */ 1937e4ae6660SVitaly Kuzmichev #endif 1938e4ae6660SVitaly Kuzmichev 19397612a43dSVitaly Kuzmichev if (rndis_active(dev)) { 19407612a43dSVitaly Kuzmichev rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0); 19417612a43dSVitaly Kuzmichev rndis_signal_disconnect(dev->rndis_config); 19427612a43dSVitaly Kuzmichev 1943e4ae6660SVitaly Kuzmichev #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT 1944e4ae6660SVitaly Kuzmichev /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */ 1945e4ae6660SVitaly Kuzmichev ts = get_timer(0); 1946e4ae6660SVitaly Kuzmichev while (get_timer(ts) < timeout) 1947e4ae6660SVitaly Kuzmichev usb_gadget_handle_interrupts(); 1948e4ae6660SVitaly Kuzmichev #endif 1949e4ae6660SVitaly Kuzmichev 19507612a43dSVitaly Kuzmichev rndis_uninit(dev->rndis_config); 19517612a43dSVitaly Kuzmichev dev->rndis = 0; 19527612a43dSVitaly Kuzmichev } 19537612a43dSVitaly Kuzmichev 19547612a43dSVitaly Kuzmichev return 0; 19557612a43dSVitaly Kuzmichev } 19567612a43dSVitaly Kuzmichev 19577612a43dSVitaly Kuzmichev /*-------------------------------------------------------------------------*/ 19587612a43dSVitaly Kuzmichev 195923cd1385SRemy Bohmer static int is_eth_addr_valid(char *str) 196023cd1385SRemy Bohmer { 196123cd1385SRemy Bohmer if (strlen(str) == 17) { 196223cd1385SRemy Bohmer int i; 196323cd1385SRemy Bohmer char *p, *q; 196423cd1385SRemy Bohmer uchar ea[6]; 196523cd1385SRemy Bohmer 196623cd1385SRemy Bohmer /* see if it looks like an ethernet address */ 196723cd1385SRemy Bohmer 196823cd1385SRemy Bohmer p = str; 196923cd1385SRemy Bohmer 197023cd1385SRemy Bohmer for (i = 0; i < 6; i++) { 197123cd1385SRemy Bohmer char term = (i == 5 ? '\0' : ':'); 197223cd1385SRemy Bohmer 197323cd1385SRemy Bohmer ea[i] = simple_strtol(p, &q, 16); 197423cd1385SRemy Bohmer 197523cd1385SRemy Bohmer if ((q - p) != 2 || *q++ != term) 197623cd1385SRemy Bohmer break; 197723cd1385SRemy Bohmer 197823cd1385SRemy Bohmer p = q; 197923cd1385SRemy Bohmer } 198023cd1385SRemy Bohmer 1981*57a87a25STom Rini /* Now check the contents. */ 1982*57a87a25STom Rini return is_valid_ether_addr(ea); 198323cd1385SRemy Bohmer } 198423cd1385SRemy Bohmer return 0; 198523cd1385SRemy Bohmer } 198623cd1385SRemy Bohmer 198723cd1385SRemy Bohmer static u8 nibble(unsigned char c) 198823cd1385SRemy Bohmer { 198923cd1385SRemy Bohmer if (likely(isdigit(c))) 199023cd1385SRemy Bohmer return c - '0'; 199123cd1385SRemy Bohmer c = toupper(c); 199223cd1385SRemy Bohmer if (likely(isxdigit(c))) 199323cd1385SRemy Bohmer return 10 + c - 'A'; 199423cd1385SRemy Bohmer return 0; 199523cd1385SRemy Bohmer } 199623cd1385SRemy Bohmer 199723cd1385SRemy Bohmer static int get_ether_addr(const char *str, u8 *dev_addr) 199823cd1385SRemy Bohmer { 199923cd1385SRemy Bohmer if (str) { 200023cd1385SRemy Bohmer unsigned i; 200123cd1385SRemy Bohmer 200223cd1385SRemy Bohmer for (i = 0; i < 6; i++) { 200323cd1385SRemy Bohmer unsigned char num; 200423cd1385SRemy Bohmer 200523cd1385SRemy Bohmer if ((*str == '.') || (*str == ':')) 200623cd1385SRemy Bohmer str++; 200723cd1385SRemy Bohmer num = nibble(*str++) << 4; 200823cd1385SRemy Bohmer num |= (nibble(*str++)); 200923cd1385SRemy Bohmer dev_addr[i] = num; 201023cd1385SRemy Bohmer } 201123cd1385SRemy Bohmer if (is_valid_ether_addr(dev_addr)) 201223cd1385SRemy Bohmer return 0; 201323cd1385SRemy Bohmer } 201423cd1385SRemy Bohmer return 1; 201523cd1385SRemy Bohmer } 201623cd1385SRemy Bohmer 201723cd1385SRemy Bohmer static int eth_bind(struct usb_gadget *gadget) 201823cd1385SRemy Bohmer { 201923cd1385SRemy Bohmer struct eth_dev *dev = &l_ethdev; 20207612a43dSVitaly Kuzmichev u8 cdc = 1, zlp = 1, rndis = 1; 202123cd1385SRemy Bohmer struct usb_ep *in_ep, *out_ep, *status_ep = NULL; 20227612a43dSVitaly Kuzmichev int status = -ENOMEM; 202323cd1385SRemy Bohmer int gcnum; 202423cd1385SRemy Bohmer u8 tmp[7]; 202523cd1385SRemy Bohmer 202623cd1385SRemy Bohmer /* these flags are only ever cleared; compiler take note */ 20272bb37884SLukasz Dalek #ifndef CONFIG_USB_ETH_CDC 202823cd1385SRemy Bohmer cdc = 0; 202923cd1385SRemy Bohmer #endif 20307612a43dSVitaly Kuzmichev #ifndef CONFIG_USB_ETH_RNDIS 20317612a43dSVitaly Kuzmichev rndis = 0; 20327612a43dSVitaly Kuzmichev #endif 20336142e0aeSVitaly Kuzmichev /* 20346142e0aeSVitaly Kuzmichev * Because most host side USB stacks handle CDC Ethernet, that 203523cd1385SRemy Bohmer * standard protocol is _strongly_ preferred for interop purposes. 203623cd1385SRemy Bohmer * (By everyone except Microsoft.) 203723cd1385SRemy Bohmer */ 203823cd1385SRemy Bohmer if (gadget_is_pxa(gadget)) { 203923cd1385SRemy Bohmer /* pxa doesn't support altsettings */ 204023cd1385SRemy Bohmer cdc = 0; 204123cd1385SRemy Bohmer } else if (gadget_is_musbhdrc(gadget)) { 204223cd1385SRemy Bohmer /* reduce tx dma overhead by avoiding special cases */ 204323cd1385SRemy Bohmer zlp = 0; 204423cd1385SRemy Bohmer } else if (gadget_is_sh(gadget)) { 204523cd1385SRemy Bohmer /* sh doesn't support multiple interfaces or configs */ 204623cd1385SRemy Bohmer cdc = 0; 20477612a43dSVitaly Kuzmichev rndis = 0; 204823cd1385SRemy Bohmer } else if (gadget_is_sa1100(gadget)) { 204923cd1385SRemy Bohmer /* hardware can't write zlps */ 205023cd1385SRemy Bohmer zlp = 0; 20516142e0aeSVitaly Kuzmichev /* 20526142e0aeSVitaly Kuzmichev * sa1100 CAN do CDC, without status endpoint ... we use 205323cd1385SRemy Bohmer * non-CDC to be compatible with ARM Linux-2.4 "usb-eth". 205423cd1385SRemy Bohmer */ 205523cd1385SRemy Bohmer cdc = 0; 205623cd1385SRemy Bohmer } 205723cd1385SRemy Bohmer 205823cd1385SRemy Bohmer gcnum = usb_gadget_controller_number(gadget); 205923cd1385SRemy Bohmer if (gcnum >= 0) 206023cd1385SRemy Bohmer device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum); 206123cd1385SRemy Bohmer else { 20626142e0aeSVitaly Kuzmichev /* 20636142e0aeSVitaly Kuzmichev * can't assume CDC works. don't want to default to 206423cd1385SRemy Bohmer * anything less functional on CDC-capable hardware, 206523cd1385SRemy Bohmer * so we fail in this case. 206623cd1385SRemy Bohmer */ 20677de73185SVitaly Kuzmichev error("controller '%s' not recognized", 206823cd1385SRemy Bohmer gadget->name); 206923cd1385SRemy Bohmer return -ENODEV; 207023cd1385SRemy Bohmer } 207123cd1385SRemy Bohmer 20726142e0aeSVitaly Kuzmichev /* 20737612a43dSVitaly Kuzmichev * If there's an RNDIS configuration, that's what Windows wants to 20747612a43dSVitaly Kuzmichev * be using ... so use these product IDs here and in the "linux.inf" 20757612a43dSVitaly Kuzmichev * needed to install MSFT drivers. Current Linux kernels will use 20767612a43dSVitaly Kuzmichev * the second configuration if it's CDC Ethernet, and need some help 20777612a43dSVitaly Kuzmichev * to choose the right configuration otherwise. 20787612a43dSVitaly Kuzmichev */ 20797612a43dSVitaly Kuzmichev if (rndis) { 20807612a43dSVitaly Kuzmichev #if defined(CONFIG_USB_RNDIS_VENDOR_ID) && defined(CONFIG_USB_RNDIS_PRODUCT_ID) 20817612a43dSVitaly Kuzmichev device_desc.idVendor = 20827612a43dSVitaly Kuzmichev __constant_cpu_to_le16(CONFIG_USB_RNDIS_VENDOR_ID); 20837612a43dSVitaly Kuzmichev device_desc.idProduct = 20847612a43dSVitaly Kuzmichev __constant_cpu_to_le16(CONFIG_USB_RNDIS_PRODUCT_ID); 20857612a43dSVitaly Kuzmichev #else 20867612a43dSVitaly Kuzmichev device_desc.idVendor = 20877612a43dSVitaly Kuzmichev __constant_cpu_to_le16(RNDIS_VENDOR_NUM); 20887612a43dSVitaly Kuzmichev device_desc.idProduct = 20897612a43dSVitaly Kuzmichev __constant_cpu_to_le16(RNDIS_PRODUCT_NUM); 20907612a43dSVitaly Kuzmichev #endif 20917612a43dSVitaly Kuzmichev sprintf(product_desc, "RNDIS/%s", driver_desc); 20927612a43dSVitaly Kuzmichev 20937612a43dSVitaly Kuzmichev /* 20946142e0aeSVitaly Kuzmichev * CDC subset ... recognized by Linux since 2.4.10, but Windows 209523cd1385SRemy Bohmer * drivers aren't widely available. (That may be improved by 209623cd1385SRemy Bohmer * supporting one submode of the "SAFE" variant of MDLM.) 209723cd1385SRemy Bohmer */ 20987612a43dSVitaly Kuzmichev } else { 20997612a43dSVitaly Kuzmichev #if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID) 21007612a43dSVitaly Kuzmichev device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID); 21017612a43dSVitaly Kuzmichev device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID); 21027612a43dSVitaly Kuzmichev #else 210323cd1385SRemy Bohmer if (!cdc) { 210423cd1385SRemy Bohmer device_desc.idVendor = 210523cd1385SRemy Bohmer __constant_cpu_to_le16(SIMPLE_VENDOR_NUM); 210623cd1385SRemy Bohmer device_desc.idProduct = 210723cd1385SRemy Bohmer __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM); 210823cd1385SRemy Bohmer } 210923cd1385SRemy Bohmer #endif 21107612a43dSVitaly Kuzmichev } 21117612a43dSVitaly Kuzmichev /* support optional vendor/distro customization */ 211223cd1385SRemy Bohmer if (bcdDevice) 211323cd1385SRemy Bohmer device_desc.bcdDevice = cpu_to_le16(bcdDevice); 211423cd1385SRemy Bohmer if (iManufacturer) 21152e12abe6SVitaly Kuzmichev strlcpy(manufacturer, iManufacturer, sizeof manufacturer); 211623cd1385SRemy Bohmer if (iProduct) 21172e12abe6SVitaly Kuzmichev strlcpy(product_desc, iProduct, sizeof product_desc); 211823cd1385SRemy Bohmer if (iSerialNumber) { 211923cd1385SRemy Bohmer device_desc.iSerialNumber = STRING_SERIALNUMBER, 21202e12abe6SVitaly Kuzmichev strlcpy(serial_number, iSerialNumber, sizeof serial_number); 212123cd1385SRemy Bohmer } 212223cd1385SRemy Bohmer 212323cd1385SRemy Bohmer /* all we really need is bulk IN/OUT */ 212423cd1385SRemy Bohmer usb_ep_autoconfig_reset(gadget); 212523cd1385SRemy Bohmer in_ep = usb_ep_autoconfig(gadget, &fs_source_desc); 212623cd1385SRemy Bohmer if (!in_ep) { 212723cd1385SRemy Bohmer autoconf_fail: 21287de73185SVitaly Kuzmichev error("can't autoconfigure on %s\n", 212923cd1385SRemy Bohmer gadget->name); 213023cd1385SRemy Bohmer return -ENODEV; 213123cd1385SRemy Bohmer } 213223cd1385SRemy Bohmer in_ep->driver_data = in_ep; /* claim */ 213323cd1385SRemy Bohmer 213423cd1385SRemy Bohmer out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc); 213523cd1385SRemy Bohmer if (!out_ep) 213623cd1385SRemy Bohmer goto autoconf_fail; 213723cd1385SRemy Bohmer out_ep->driver_data = out_ep; /* claim */ 213823cd1385SRemy Bohmer 21392bb37884SLukasz Dalek #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS) 21406142e0aeSVitaly Kuzmichev /* 21416142e0aeSVitaly Kuzmichev * CDC Ethernet control interface doesn't require a status endpoint. 214223cd1385SRemy Bohmer * Since some hosts expect one, try to allocate one anyway. 214323cd1385SRemy Bohmer */ 21447612a43dSVitaly Kuzmichev if (cdc || rndis) { 214523cd1385SRemy Bohmer status_ep = usb_ep_autoconfig(gadget, &fs_status_desc); 214623cd1385SRemy Bohmer if (status_ep) { 214723cd1385SRemy Bohmer status_ep->driver_data = status_ep; /* claim */ 21487612a43dSVitaly Kuzmichev } else if (rndis) { 21497612a43dSVitaly Kuzmichev error("can't run RNDIS on %s", gadget->name); 21507612a43dSVitaly Kuzmichev return -ENODEV; 21512bb37884SLukasz Dalek #ifdef CONFIG_USB_ETH_CDC 215223cd1385SRemy Bohmer } else if (cdc) { 215323cd1385SRemy Bohmer control_intf.bNumEndpoints = 0; 215423cd1385SRemy Bohmer /* FIXME remove endpoint from descriptor list */ 21557612a43dSVitaly Kuzmichev #endif 215623cd1385SRemy Bohmer } 215723cd1385SRemy Bohmer } 215823cd1385SRemy Bohmer #endif 215923cd1385SRemy Bohmer 216023cd1385SRemy Bohmer /* one config: cdc, else minimal subset */ 216123cd1385SRemy Bohmer if (!cdc) { 216223cd1385SRemy Bohmer eth_config.bNumInterfaces = 1; 216323cd1385SRemy Bohmer eth_config.iConfiguration = STRING_SUBSET; 216423cd1385SRemy Bohmer 21656142e0aeSVitaly Kuzmichev /* 21666142e0aeSVitaly Kuzmichev * use functions to set these up, in case we're built to work 216723cd1385SRemy Bohmer * with multiple controllers and must override CDC Ethernet. 216823cd1385SRemy Bohmer */ 216923cd1385SRemy Bohmer fs_subset_descriptors(); 217023cd1385SRemy Bohmer hs_subset_descriptors(); 217123cd1385SRemy Bohmer } 217223cd1385SRemy Bohmer 217323cd1385SRemy Bohmer device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket; 217423cd1385SRemy Bohmer usb_gadget_set_selfpowered(gadget); 217523cd1385SRemy Bohmer 21767612a43dSVitaly Kuzmichev /* For now RNDIS is always a second config */ 21777612a43dSVitaly Kuzmichev if (rndis) 21787612a43dSVitaly Kuzmichev device_desc.bNumConfigurations = 2; 21797612a43dSVitaly Kuzmichev 218023cd1385SRemy Bohmer if (gadget_is_dualspeed(gadget)) { 21817612a43dSVitaly Kuzmichev if (rndis) 21827612a43dSVitaly Kuzmichev dev_qualifier.bNumConfigurations = 2; 21837612a43dSVitaly Kuzmichev else if (!cdc) 218423cd1385SRemy Bohmer dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC; 218523cd1385SRemy Bohmer 218623cd1385SRemy Bohmer /* assumes ep0 uses the same value for both speeds ... */ 218723cd1385SRemy Bohmer dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0; 218823cd1385SRemy Bohmer 218923cd1385SRemy Bohmer /* and that all endpoints are dual-speed */ 219023cd1385SRemy Bohmer hs_source_desc.bEndpointAddress = 219123cd1385SRemy Bohmer fs_source_desc.bEndpointAddress; 219223cd1385SRemy Bohmer hs_sink_desc.bEndpointAddress = 219323cd1385SRemy Bohmer fs_sink_desc.bEndpointAddress; 21942bb37884SLukasz Dalek #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS) 219523cd1385SRemy Bohmer if (status_ep) 219623cd1385SRemy Bohmer hs_status_desc.bEndpointAddress = 219723cd1385SRemy Bohmer fs_status_desc.bEndpointAddress; 219823cd1385SRemy Bohmer #endif 219923cd1385SRemy Bohmer } 220023cd1385SRemy Bohmer 220123cd1385SRemy Bohmer if (gadget_is_otg(gadget)) { 220223cd1385SRemy Bohmer otg_descriptor.bmAttributes |= USB_OTG_HNP, 220323cd1385SRemy Bohmer eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP; 220423cd1385SRemy Bohmer eth_config.bMaxPower = 4; 22057612a43dSVitaly Kuzmichev #ifdef CONFIG_USB_ETH_RNDIS 22067612a43dSVitaly Kuzmichev rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP; 22077612a43dSVitaly Kuzmichev rndis_config.bMaxPower = 4; 22087612a43dSVitaly Kuzmichev #endif 220923cd1385SRemy Bohmer } 221023cd1385SRemy Bohmer 22117612a43dSVitaly Kuzmichev 22127612a43dSVitaly Kuzmichev /* network device setup */ 221323cd1385SRemy Bohmer dev->net = &l_netdev; 221423cd1385SRemy Bohmer 221523cd1385SRemy Bohmer dev->cdc = cdc; 221623cd1385SRemy Bohmer dev->zlp = zlp; 221723cd1385SRemy Bohmer 221823cd1385SRemy Bohmer dev->in_ep = in_ep; 221923cd1385SRemy Bohmer dev->out_ep = out_ep; 222023cd1385SRemy Bohmer dev->status_ep = status_ep; 222123cd1385SRemy Bohmer 22226142e0aeSVitaly Kuzmichev /* 22236142e0aeSVitaly Kuzmichev * Module params for these addresses should come from ID proms. 22247612a43dSVitaly Kuzmichev * The host side address is used with CDC and RNDIS, and commonly 222523cd1385SRemy Bohmer * ends up in a persistent config database. It's not clear if 222623cd1385SRemy Bohmer * host side code for the SAFE thing cares -- its original BLAN 222723cd1385SRemy Bohmer * thing didn't, Sharp never assigned those addresses on Zaurii. 222823cd1385SRemy Bohmer */ 222923cd1385SRemy Bohmer get_ether_addr(dev_addr, dev->net->enetaddr); 223023cd1385SRemy Bohmer 223123cd1385SRemy Bohmer memset(tmp, 0, sizeof(tmp)); 223223cd1385SRemy Bohmer memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr)); 223323cd1385SRemy Bohmer 223423cd1385SRemy Bohmer get_ether_addr(host_addr, dev->host_mac); 223523cd1385SRemy Bohmer 223623cd1385SRemy Bohmer sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X", 223723cd1385SRemy Bohmer dev->host_mac[0], dev->host_mac[1], 223823cd1385SRemy Bohmer dev->host_mac[2], dev->host_mac[3], 223923cd1385SRemy Bohmer dev->host_mac[4], dev->host_mac[5]); 224023cd1385SRemy Bohmer 22417612a43dSVitaly Kuzmichev if (rndis) { 22427612a43dSVitaly Kuzmichev status = rndis_init(); 22437612a43dSVitaly Kuzmichev if (status < 0) { 22447612a43dSVitaly Kuzmichev error("can't init RNDIS, %d", status); 22457612a43dSVitaly Kuzmichev goto fail; 22467612a43dSVitaly Kuzmichev } 224723cd1385SRemy Bohmer } 224823cd1385SRemy Bohmer 22496142e0aeSVitaly Kuzmichev /* 22506142e0aeSVitaly Kuzmichev * use PKTSIZE (or aligned... from u-boot) and set 22516142e0aeSVitaly Kuzmichev * wMaxSegmentSize accordingly 22526142e0aeSVitaly Kuzmichev */ 225323cd1385SRemy Bohmer dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/ 225423cd1385SRemy Bohmer 225523cd1385SRemy Bohmer /* preallocate control message data and buffer */ 225623cd1385SRemy Bohmer dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL); 225723cd1385SRemy Bohmer if (!dev->req) 225823cd1385SRemy Bohmer goto fail; 225923cd1385SRemy Bohmer dev->req->buf = control_req; 226023cd1385SRemy Bohmer dev->req->complete = eth_setup_complete; 226123cd1385SRemy Bohmer 226223cd1385SRemy Bohmer /* ... and maybe likewise for status transfer */ 22632bb37884SLukasz Dalek #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS) 226423cd1385SRemy Bohmer if (dev->status_ep) { 22656142e0aeSVitaly Kuzmichev dev->stat_req = usb_ep_alloc_request(dev->status_ep, 22666142e0aeSVitaly Kuzmichev GFP_KERNEL); 226723cd1385SRemy Bohmer if (!dev->stat_req) { 2268df559c1dSVitaly Kuzmichev usb_ep_free_request(dev->status_ep, dev->req); 226923cd1385SRemy Bohmer 227023cd1385SRemy Bohmer goto fail; 227123cd1385SRemy Bohmer } 2272df559c1dSVitaly Kuzmichev dev->stat_req->buf = status_req; 227323cd1385SRemy Bohmer dev->stat_req->context = NULL; 227423cd1385SRemy Bohmer } 227523cd1385SRemy Bohmer #endif 227623cd1385SRemy Bohmer 227723cd1385SRemy Bohmer /* finish hookup to lower layer ... */ 227823cd1385SRemy Bohmer dev->gadget = gadget; 227923cd1385SRemy Bohmer set_gadget_data(gadget, dev); 228023cd1385SRemy Bohmer gadget->ep0->driver_data = dev; 228123cd1385SRemy Bohmer 22826142e0aeSVitaly Kuzmichev /* 22836142e0aeSVitaly Kuzmichev * two kinds of host-initiated state changes: 228423cd1385SRemy Bohmer * - iff DATA transfer is active, carrier is "on" 228523cd1385SRemy Bohmer * - tx queueing enabled if open *and* carrier is "on" 228623cd1385SRemy Bohmer */ 22877612a43dSVitaly Kuzmichev 22887612a43dSVitaly Kuzmichev printf("using %s, OUT %s IN %s%s%s\n", gadget->name, 22897612a43dSVitaly Kuzmichev out_ep->name, in_ep->name, 22907612a43dSVitaly Kuzmichev status_ep ? " STATUS " : "", 22917612a43dSVitaly Kuzmichev status_ep ? status_ep->name : "" 22927612a43dSVitaly Kuzmichev ); 22937612a43dSVitaly Kuzmichev printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n", 22947612a43dSVitaly Kuzmichev dev->net->enetaddr[0], dev->net->enetaddr[1], 22957612a43dSVitaly Kuzmichev dev->net->enetaddr[2], dev->net->enetaddr[3], 22967612a43dSVitaly Kuzmichev dev->net->enetaddr[4], dev->net->enetaddr[5]); 22977612a43dSVitaly Kuzmichev 22987612a43dSVitaly Kuzmichev if (cdc || rndis) 22997612a43dSVitaly Kuzmichev printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n", 23007612a43dSVitaly Kuzmichev dev->host_mac[0], dev->host_mac[1], 23017612a43dSVitaly Kuzmichev dev->host_mac[2], dev->host_mac[3], 23027612a43dSVitaly Kuzmichev dev->host_mac[4], dev->host_mac[5]); 23037612a43dSVitaly Kuzmichev 23047612a43dSVitaly Kuzmichev if (rndis) { 23057612a43dSVitaly Kuzmichev u32 vendorID = 0; 23067612a43dSVitaly Kuzmichev 23077612a43dSVitaly Kuzmichev /* FIXME RNDIS vendor id == "vendor NIC code" == ? */ 23087612a43dSVitaly Kuzmichev 23097612a43dSVitaly Kuzmichev dev->rndis_config = rndis_register(rndis_control_ack); 23107612a43dSVitaly Kuzmichev if (dev->rndis_config < 0) { 23117612a43dSVitaly Kuzmichev fail0: 23127612a43dSVitaly Kuzmichev eth_unbind(gadget); 23137612a43dSVitaly Kuzmichev debug("RNDIS setup failed\n"); 23147612a43dSVitaly Kuzmichev status = -ENODEV; 23157612a43dSVitaly Kuzmichev goto fail; 23167612a43dSVitaly Kuzmichev } 23177612a43dSVitaly Kuzmichev 23187612a43dSVitaly Kuzmichev /* these set up a lot of the OIDs that RNDIS needs */ 23197612a43dSVitaly Kuzmichev rndis_set_host_mac(dev->rndis_config, dev->host_mac); 23207612a43dSVitaly Kuzmichev if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu, 23217612a43dSVitaly Kuzmichev &dev->stats, &dev->cdc_filter)) 23227612a43dSVitaly Kuzmichev goto fail0; 23237612a43dSVitaly Kuzmichev if (rndis_set_param_vendor(dev->rndis_config, vendorID, 23247612a43dSVitaly Kuzmichev manufacturer)) 23257612a43dSVitaly Kuzmichev goto fail0; 23267612a43dSVitaly Kuzmichev if (rndis_set_param_medium(dev->rndis_config, 23277612a43dSVitaly Kuzmichev NDIS_MEDIUM_802_3, 0)) 23287612a43dSVitaly Kuzmichev goto fail0; 23297612a43dSVitaly Kuzmichev printf("RNDIS ready\n"); 23307612a43dSVitaly Kuzmichev } 233123cd1385SRemy Bohmer return 0; 233223cd1385SRemy Bohmer 233323cd1385SRemy Bohmer fail: 23347612a43dSVitaly Kuzmichev error("%s failed, status = %d", __func__, status); 233523cd1385SRemy Bohmer eth_unbind(gadget); 23367612a43dSVitaly Kuzmichev return status; 233723cd1385SRemy Bohmer } 233823cd1385SRemy Bohmer 23397612a43dSVitaly Kuzmichev /*-------------------------------------------------------------------------*/ 23407612a43dSVitaly Kuzmichev 234123cd1385SRemy Bohmer static int usb_eth_init(struct eth_device *netdev, bd_t *bd) 234223cd1385SRemy Bohmer { 234323cd1385SRemy Bohmer struct eth_dev *dev = &l_ethdev; 234423cd1385SRemy Bohmer struct usb_gadget *gadget; 234523cd1385SRemy Bohmer unsigned long ts; 234623cd1385SRemy Bohmer unsigned long timeout = USB_CONNECT_TIMEOUT; 234723cd1385SRemy Bohmer 234823cd1385SRemy Bohmer if (!netdev) { 23497de73185SVitaly Kuzmichev error("received NULL ptr"); 235023cd1385SRemy Bohmer goto fail; 235123cd1385SRemy Bohmer } 235258939fccSVitaly Kuzmichev 235358939fccSVitaly Kuzmichev /* Configure default mac-addresses for the USB ethernet device */ 235458939fccSVitaly Kuzmichev #ifdef CONFIG_USBNET_DEV_ADDR 235558939fccSVitaly Kuzmichev strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr)); 235658939fccSVitaly Kuzmichev #endif 235758939fccSVitaly Kuzmichev #ifdef CONFIG_USBNET_HOST_ADDR 235858939fccSVitaly Kuzmichev strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr)); 235958939fccSVitaly Kuzmichev #endif 236058939fccSVitaly Kuzmichev /* Check if the user overruled the MAC addresses */ 236158939fccSVitaly Kuzmichev if (getenv("usbnet_devaddr")) 236258939fccSVitaly Kuzmichev strlcpy(dev_addr, getenv("usbnet_devaddr"), 236358939fccSVitaly Kuzmichev sizeof(dev_addr)); 236458939fccSVitaly Kuzmichev 236558939fccSVitaly Kuzmichev if (getenv("usbnet_hostaddr")) 236658939fccSVitaly Kuzmichev strlcpy(host_addr, getenv("usbnet_hostaddr"), 236758939fccSVitaly Kuzmichev sizeof(host_addr)); 236858939fccSVitaly Kuzmichev 236958939fccSVitaly Kuzmichev if (!is_eth_addr_valid(dev_addr)) { 237058939fccSVitaly Kuzmichev error("Need valid 'usbnet_devaddr' to be set"); 237158939fccSVitaly Kuzmichev goto fail; 237258939fccSVitaly Kuzmichev } 237358939fccSVitaly Kuzmichev if (!is_eth_addr_valid(host_addr)) { 237458939fccSVitaly Kuzmichev error("Need valid 'usbnet_hostaddr' to be set"); 237558939fccSVitaly Kuzmichev goto fail; 237658939fccSVitaly Kuzmichev } 237758939fccSVitaly Kuzmichev 2378988ee3e3SLei Wen if (usb_gadget_register_driver(ð_driver) < 0) 2379988ee3e3SLei Wen goto fail; 238023cd1385SRemy Bohmer 238123cd1385SRemy Bohmer dev->network_started = 0; 238223cd1385SRemy Bohmer 238323cd1385SRemy Bohmer packet_received = 0; 238423cd1385SRemy Bohmer packet_sent = 0; 238523cd1385SRemy Bohmer 238623cd1385SRemy Bohmer gadget = dev->gadget; 238723cd1385SRemy Bohmer usb_gadget_connect(gadget); 238823cd1385SRemy Bohmer 238923cd1385SRemy Bohmer if (getenv("cdc_connect_timeout")) 239023cd1385SRemy Bohmer timeout = simple_strtoul(getenv("cdc_connect_timeout"), 239123cd1385SRemy Bohmer NULL, 10) * CONFIG_SYS_HZ; 239223cd1385SRemy Bohmer ts = get_timer(0); 23936142e0aeSVitaly Kuzmichev while (!l_ethdev.network_started) { 239423cd1385SRemy Bohmer /* Handle control-c and timeouts */ 239523cd1385SRemy Bohmer if (ctrlc() || (get_timer(ts) > timeout)) { 23967de73185SVitaly Kuzmichev error("The remote end did not respond in time."); 239723cd1385SRemy Bohmer goto fail; 239823cd1385SRemy Bohmer } 239923cd1385SRemy Bohmer usb_gadget_handle_interrupts(); 240023cd1385SRemy Bohmer } 240123cd1385SRemy Bohmer 240298fae970SVitaly Kuzmichev packet_received = 0; 240323cd1385SRemy Bohmer rx_submit(dev, dev->rx_req, 0); 240423cd1385SRemy Bohmer return 0; 240523cd1385SRemy Bohmer fail: 240623cd1385SRemy Bohmer return -1; 240723cd1385SRemy Bohmer } 240823cd1385SRemy Bohmer 240910cbe3b6SJoe Hershberger static int usb_eth_send(struct eth_device *netdev, void *packet, int length) 241023cd1385SRemy Bohmer { 241123cd1385SRemy Bohmer int retval; 24127612a43dSVitaly Kuzmichev void *rndis_pkt = NULL; 241323cd1385SRemy Bohmer struct eth_dev *dev = &l_ethdev; 2414ac5d32d1SVitaly Kuzmichev struct usb_request *req = dev->tx_req; 2415a170f2c7SStefano Babic unsigned long ts; 2416a170f2c7SStefano Babic unsigned long timeout = USB_CONNECT_TIMEOUT; 24177de73185SVitaly Kuzmichev 24187de73185SVitaly Kuzmichev debug("%s:...\n", __func__); 241923cd1385SRemy Bohmer 24207612a43dSVitaly Kuzmichev /* new buffer is needed to include RNDIS header */ 24217612a43dSVitaly Kuzmichev if (rndis_active(dev)) { 24227612a43dSVitaly Kuzmichev rndis_pkt = malloc(length + 24237612a43dSVitaly Kuzmichev sizeof(struct rndis_packet_msg_type)); 24247612a43dSVitaly Kuzmichev if (!rndis_pkt) { 24257612a43dSVitaly Kuzmichev error("No memory to alloc RNDIS packet"); 24267612a43dSVitaly Kuzmichev goto drop; 24277612a43dSVitaly Kuzmichev } 24287612a43dSVitaly Kuzmichev rndis_add_hdr(rndis_pkt, length); 24297612a43dSVitaly Kuzmichev memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type), 243010cbe3b6SJoe Hershberger packet, length); 24317612a43dSVitaly Kuzmichev packet = rndis_pkt; 24327612a43dSVitaly Kuzmichev length += sizeof(struct rndis_packet_msg_type); 24337612a43dSVitaly Kuzmichev } 243410cbe3b6SJoe Hershberger req->buf = packet; 243523cd1385SRemy Bohmer req->context = NULL; 243623cd1385SRemy Bohmer req->complete = tx_complete; 243723cd1385SRemy Bohmer 24386142e0aeSVitaly Kuzmichev /* 24396142e0aeSVitaly Kuzmichev * use zlp framing on tx for strict CDC-Ether conformance, 244023cd1385SRemy Bohmer * though any robust network rx path ignores extra padding. 244123cd1385SRemy Bohmer * and some hardware doesn't like to write zlps. 244223cd1385SRemy Bohmer */ 244323cd1385SRemy Bohmer req->zero = 1; 244423cd1385SRemy Bohmer if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0) 244523cd1385SRemy Bohmer length++; 244623cd1385SRemy Bohmer 244723cd1385SRemy Bohmer req->length = length; 244823cd1385SRemy Bohmer #if 0 244923cd1385SRemy Bohmer /* throttle highspeed IRQ rate back slightly */ 245023cd1385SRemy Bohmer if (gadget_is_dualspeed(dev->gadget)) 245123cd1385SRemy Bohmer req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH) 245223cd1385SRemy Bohmer ? ((dev->tx_qlen % qmult) != 0) : 0; 245323cd1385SRemy Bohmer #endif 245423cd1385SRemy Bohmer dev->tx_qlen = 1; 2455a170f2c7SStefano Babic ts = get_timer(0); 2456a170f2c7SStefano Babic packet_sent = 0; 245723cd1385SRemy Bohmer 245823cd1385SRemy Bohmer retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC); 245923cd1385SRemy Bohmer 246023cd1385SRemy Bohmer if (!retval) 24617de73185SVitaly Kuzmichev debug("%s: packet queued\n", __func__); 24626142e0aeSVitaly Kuzmichev while (!packet_sent) { 2463a170f2c7SStefano Babic if (get_timer(ts) > timeout) { 2464a170f2c7SStefano Babic printf("timeout sending packets to usb ethernet\n"); 2465a170f2c7SStefano Babic return -1; 2466a170f2c7SStefano Babic } 2467a170f2c7SStefano Babic usb_gadget_handle_interrupts(); 246823cd1385SRemy Bohmer } 24697612a43dSVitaly Kuzmichev if (rndis_pkt) 24707612a43dSVitaly Kuzmichev free(rndis_pkt); 247123cd1385SRemy Bohmer 247223cd1385SRemy Bohmer return 0; 24737612a43dSVitaly Kuzmichev drop: 24747612a43dSVitaly Kuzmichev dev->stats.tx_dropped++; 24757612a43dSVitaly Kuzmichev return -ENOMEM; 247623cd1385SRemy Bohmer } 247723cd1385SRemy Bohmer 247823cd1385SRemy Bohmer static int usb_eth_recv(struct eth_device *netdev) 247923cd1385SRemy Bohmer { 248023cd1385SRemy Bohmer struct eth_dev *dev = &l_ethdev; 248123cd1385SRemy Bohmer 248223cd1385SRemy Bohmer usb_gadget_handle_interrupts(); 248323cd1385SRemy Bohmer 24846142e0aeSVitaly Kuzmichev if (packet_received) { 24857de73185SVitaly Kuzmichev debug("%s: packet received\n", __func__); 24866142e0aeSVitaly Kuzmichev if (dev->rx_req) { 248723cd1385SRemy Bohmer NetReceive(NetRxPackets[0], dev->rx_req->length); 248823cd1385SRemy Bohmer packet_received = 0; 248923cd1385SRemy Bohmer 249023cd1385SRemy Bohmer rx_submit(dev, dev->rx_req, 0); 24916142e0aeSVitaly Kuzmichev } else 24926142e0aeSVitaly Kuzmichev error("dev->rx_req invalid"); 249323cd1385SRemy Bohmer } 249423cd1385SRemy Bohmer return 0; 249523cd1385SRemy Bohmer } 249623cd1385SRemy Bohmer 249723cd1385SRemy Bohmer void usb_eth_halt(struct eth_device *netdev) 249823cd1385SRemy Bohmer { 249923cd1385SRemy Bohmer struct eth_dev *dev = &l_ethdev; 250023cd1385SRemy Bohmer 25016142e0aeSVitaly Kuzmichev if (!netdev) { 25027de73185SVitaly Kuzmichev error("received NULL ptr"); 250323cd1385SRemy Bohmer return; 250423cd1385SRemy Bohmer } 250523cd1385SRemy Bohmer 2506988ee3e3SLei Wen /* If the gadget not registered, simple return */ 2507988ee3e3SLei Wen if (!dev->gadget) 2508988ee3e3SLei Wen return; 2509988ee3e3SLei Wen 2510e4ae6660SVitaly Kuzmichev /* 2511e4ae6660SVitaly Kuzmichev * Some USB controllers may need additional deinitialization here 2512e4ae6660SVitaly Kuzmichev * before dropping pull-up (also due to hardware issues). 2513e4ae6660SVitaly Kuzmichev * For example: unhandled interrupt with status stage started may 2514e4ae6660SVitaly Kuzmichev * bring the controller to fully broken state (until board reset). 2515e4ae6660SVitaly Kuzmichev * There are some variants to debug and fix such cases: 2516e4ae6660SVitaly Kuzmichev * 1) In the case of RNDIS connection eth_stop can perform additional 2517e4ae6660SVitaly Kuzmichev * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition. 2518e4ae6660SVitaly Kuzmichev * 2) 'pullup' callback in your UDC driver can be improved to perform 2519e4ae6660SVitaly Kuzmichev * this deinitialization. 2520e4ae6660SVitaly Kuzmichev */ 25217612a43dSVitaly Kuzmichev eth_stop(dev); 25227612a43dSVitaly Kuzmichev 252323cd1385SRemy Bohmer usb_gadget_disconnect(dev->gadget); 2524b3649f3bSVitaly Kuzmichev 2525b3649f3bSVitaly Kuzmichev /* Clear pending interrupt */ 2526b3649f3bSVitaly Kuzmichev if (dev->network_started) { 2527b3649f3bSVitaly Kuzmichev usb_gadget_handle_interrupts(); 2528b3649f3bSVitaly Kuzmichev dev->network_started = 0; 2529b3649f3bSVitaly Kuzmichev } 2530b3649f3bSVitaly Kuzmichev 2531988ee3e3SLei Wen usb_gadget_unregister_driver(ð_driver); 253223cd1385SRemy Bohmer } 253323cd1385SRemy Bohmer 253423cd1385SRemy Bohmer static struct usb_gadget_driver eth_driver = { 253523cd1385SRemy Bohmer .speed = DEVSPEED, 253623cd1385SRemy Bohmer 253723cd1385SRemy Bohmer .bind = eth_bind, 253823cd1385SRemy Bohmer .unbind = eth_unbind, 253923cd1385SRemy Bohmer 254023cd1385SRemy Bohmer .setup = eth_setup, 254123cd1385SRemy Bohmer .disconnect = eth_disconnect, 254223cd1385SRemy Bohmer 254323cd1385SRemy Bohmer .suspend = eth_suspend, 254423cd1385SRemy Bohmer .resume = eth_resume, 254523cd1385SRemy Bohmer }; 254623cd1385SRemy Bohmer 254723cd1385SRemy Bohmer int usb_eth_initialize(bd_t *bi) 254823cd1385SRemy Bohmer { 254923cd1385SRemy Bohmer struct eth_device *netdev = &l_netdev; 255023cd1385SRemy Bohmer 25518f7aa831SVitaly Kuzmichev strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name)); 255223cd1385SRemy Bohmer 255323cd1385SRemy Bohmer netdev->init = usb_eth_init; 255423cd1385SRemy Bohmer netdev->send = usb_eth_send; 255523cd1385SRemy Bohmer netdev->recv = usb_eth_recv; 255623cd1385SRemy Bohmer netdev->halt = usb_eth_halt; 255723cd1385SRemy Bohmer 255823cd1385SRemy Bohmer #ifdef CONFIG_MCAST_TFTP 255923cd1385SRemy Bohmer #error not supported 256023cd1385SRemy Bohmer #endif 256123cd1385SRemy Bohmer eth_register(netdev); 256223cd1385SRemy Bohmer return 0; 256323cd1385SRemy Bohmer } 2564