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