Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1 | /* |
| 2 | * ether.c -- Ethernet gadget driver, with CDC and non-CDC options |
| 3 | * |
| 4 | * Copyright (C) 2003-2005,2008 David Brownell |
| 5 | * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger |
| 6 | * Copyright (C) 2008 Nokia Corporation |
| 7 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <asm/errno.h> |
Vitaly Kuzmichev | c85d70e | 2011-02-11 18:18:32 +0300 | [diff] [blame] | 13 | #include <linux/netdevice.h> |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 14 | #include <linux/usb/ch9.h> |
| 15 | #include <linux/usb/cdc.h> |
| 16 | #include <linux/usb/gadget.h> |
| 17 | #include <net.h> |
Kishon Vijay Abraham I | 8bfc288 | 2015-08-19 13:49:46 +0530 | [diff] [blame] | 18 | #include <usb.h> |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 19 | #include <malloc.h> |
Simon Glass | cf92e05 | 2015-09-02 17:24:58 -0600 | [diff] [blame] | 20 | #include <memalign.h> |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 21 | #include <linux/ctype.h> |
| 22 | |
| 23 | #include "gadget_chips.h" |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 24 | #include "rndis.h" |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 25 | |
Vitaly Kuzmichev | 8f7aa83 | 2010-12-28 16:59:31 +0300 | [diff] [blame] | 26 | #define USB_NET_NAME "usb_ether" |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 27 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 28 | #define atomic_read |
| 29 | extern struct platform_data brd; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 30 | |
| 31 | |
| 32 | unsigned packet_received, packet_sent; |
| 33 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 34 | /* |
| 35 | * Ethernet gadget driver -- with CDC and non-CDC options |
| 36 | * Builds on hardware support for a full duplex link. |
| 37 | * |
| 38 | * CDC Ethernet is the standard USB solution for sending Ethernet frames |
| 39 | * using USB. Real hardware tends to use the same framing protocol but look |
| 40 | * different for control features. This driver strongly prefers to use |
| 41 | * this USB-IF standard as its open-systems interoperability solution; |
| 42 | * most host side USB stacks (except from Microsoft) support it. |
| 43 | * |
| 44 | * This is sometimes called "CDC ECM" (Ethernet Control Model) to support |
| 45 | * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new |
| 46 | * "CDC EEM" (Ethernet Emulation Model) is starting to spread. |
| 47 | * |
| 48 | * There's some hardware that can't talk CDC ECM. We make that hardware |
| 49 | * implement a "minimalist" vendor-agnostic CDC core: same framing, but |
| 50 | * link-level setup only requires activating the configuration. Only the |
| 51 | * endpoint descriptors, and product/vendor IDs, are relevant; no control |
| 52 | * operations are available. Linux supports it, but other host operating |
| 53 | * systems may not. (This is a subset of CDC Ethernet.) |
| 54 | * |
| 55 | * It turns out that if you add a few descriptors to that "CDC Subset", |
| 56 | * (Windows) host side drivers from MCCI can treat it as one submode of |
| 57 | * a proprietary scheme called "SAFE" ... without needing to know about |
| 58 | * specific product/vendor IDs. So we do that, making it easier to use |
| 59 | * those MS-Windows drivers. Those added descriptors make it resemble a |
| 60 | * CDC MDLM device, but they don't change device behavior at all. (See |
| 61 | * MCCI Engineering report 950198 "SAFE Networking Functions".) |
| 62 | * |
| 63 | * A third option is also in use. Rather than CDC Ethernet, or something |
| 64 | * simpler, Microsoft pushes their own approach: RNDIS. The published |
| 65 | * RNDIS specs are ambiguous and appear to be incomplete, and are also |
| 66 | * needlessly complex. They borrow more from CDC ACM than CDC ECM. |
| 67 | */ |
| 68 | #define ETH_ALEN 6 /* Octets in one ethernet addr */ |
| 69 | #define ETH_HLEN 14 /* Total octets in header. */ |
| 70 | #define ETH_ZLEN 60 /* Min. octets in frame sans FCS */ |
| 71 | #define ETH_DATA_LEN 1500 /* Max. octets in payload */ |
| 72 | #define ETH_FRAME_LEN PKTSIZE_ALIGN /* Max. octets in frame sans FCS */ |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 73 | |
| 74 | #define DRIVER_DESC "Ethernet Gadget" |
| 75 | /* Based on linux 2.6.27 version */ |
| 76 | #define DRIVER_VERSION "May Day 2005" |
| 77 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 78 | static const char shortname[] = "ether"; |
| 79 | static const char driver_desc[] = DRIVER_DESC; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 80 | |
| 81 | #define RX_EXTRA 20 /* guard against rx overflows */ |
| 82 | |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 83 | #ifndef CONFIG_USB_ETH_RNDIS |
| 84 | #define rndis_uninit(x) do {} while (0) |
| 85 | #define rndis_deregister(c) do {} while (0) |
| 86 | #define rndis_exit() do {} while (0) |
| 87 | #endif |
| 88 | |
| 89 | /* CDC and RNDIS support the same host-chosen outgoing packet filters. */ |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 90 | #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \ |
| 91 | |USB_CDC_PACKET_TYPE_ALL_MULTICAST \ |
| 92 | |USB_CDC_PACKET_TYPE_PROMISCUOUS \ |
| 93 | |USB_CDC_PACKET_TYPE_DIRECTED) |
| 94 | |
| 95 | #define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ) |
| 96 | |
| 97 | /*-------------------------------------------------------------------------*/ |
Vitaly Kuzmichev | 8b6b66b | 2011-02-11 18:18:33 +0300 | [diff] [blame] | 98 | |
| 99 | struct eth_dev { |
| 100 | struct usb_gadget *gadget; |
| 101 | struct usb_request *req; /* for control responses */ |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 102 | struct usb_request *stat_req; /* for cdc & rndis status */ |
Vitaly Kuzmichev | 8b6b66b | 2011-02-11 18:18:33 +0300 | [diff] [blame] | 103 | |
| 104 | u8 config; |
| 105 | struct usb_ep *in_ep, *out_ep, *status_ep; |
| 106 | const struct usb_endpoint_descriptor |
| 107 | *in, *out, *status; |
| 108 | |
| 109 | struct usb_request *tx_req, *rx_req; |
| 110 | |
| 111 | struct eth_device *net; |
| 112 | struct net_device_stats stats; |
| 113 | unsigned int tx_qlen; |
| 114 | |
| 115 | unsigned zlp:1; |
| 116 | unsigned cdc:1; |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 117 | unsigned rndis:1; |
Vitaly Kuzmichev | 8b6b66b | 2011-02-11 18:18:33 +0300 | [diff] [blame] | 118 | unsigned suspended:1; |
| 119 | unsigned network_started:1; |
| 120 | u16 cdc_filter; |
| 121 | unsigned long todo; |
| 122 | int mtu; |
| 123 | #define WORK_RX_MEMORY 0 |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 124 | int rndis_config; |
Vitaly Kuzmichev | 8b6b66b | 2011-02-11 18:18:33 +0300 | [diff] [blame] | 125 | u8 host_mac[ETH_ALEN]; |
| 126 | }; |
| 127 | |
| 128 | /* |
| 129 | * This version autoconfigures as much as possible at run-time. |
| 130 | * |
| 131 | * It also ASSUMES a self-powered device, without remote wakeup, |
| 132 | * although remote wakeup support would make sense. |
| 133 | */ |
| 134 | |
| 135 | /*-------------------------------------------------------------------------*/ |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 136 | static struct eth_dev l_ethdev; |
| 137 | static struct eth_device l_netdev; |
| 138 | static struct usb_gadget_driver eth_driver; |
| 139 | |
| 140 | /*-------------------------------------------------------------------------*/ |
| 141 | |
| 142 | /* "main" config is either CDC, or its simple subset */ |
| 143 | static inline int is_cdc(struct eth_dev *dev) |
| 144 | { |
Lukasz Dalek | 2bb3788 | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 145 | #if !defined(CONFIG_USB_ETH_SUBSET) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 146 | return 1; /* only cdc possible */ |
Lukasz Dalek | 2bb3788 | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 147 | #elif !defined(CONFIG_USB_ETH_CDC) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 148 | return 0; /* only subset possible */ |
| 149 | #else |
| 150 | return dev->cdc; /* depends on what hardware we found */ |
| 151 | #endif |
| 152 | } |
| 153 | |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 154 | /* "secondary" RNDIS config may sometimes be activated */ |
| 155 | static inline int rndis_active(struct eth_dev *dev) |
| 156 | { |
| 157 | #ifdef CONFIG_USB_ETH_RNDIS |
| 158 | return dev->rndis; |
| 159 | #else |
| 160 | return 0; |
| 161 | #endif |
| 162 | } |
| 163 | |
| 164 | #define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev)) |
| 165 | #define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev)) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 166 | |
| 167 | #define DEFAULT_QLEN 2 /* double buffering by default */ |
| 168 | |
| 169 | /* peak bulk transfer bits-per-second */ |
| 170 | #define HS_BPS (13 * 512 * 8 * 1000 * 8) |
| 171 | #define FS_BPS (19 * 64 * 1 * 1000 * 8) |
| 172 | |
| 173 | #ifdef CONFIG_USB_GADGET_DUALSPEED |
| 174 | #define DEVSPEED USB_SPEED_HIGH |
| 175 | |
Vitaly Kuzmichev | 2721dbf | 2010-08-12 16:44:40 +0400 | [diff] [blame] | 176 | #ifdef CONFIG_USB_ETH_QMULT |
| 177 | #define qmult CONFIG_USB_ETH_QMULT |
| 178 | #else |
| 179 | #define qmult 5 |
| 180 | #endif |
| 181 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 182 | /* for dual-speed hardware, use deeper queues at highspeed */ |
| 183 | #define qlen(gadget) \ |
| 184 | (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1)) |
| 185 | |
| 186 | static inline int BITRATE(struct usb_gadget *g) |
| 187 | { |
| 188 | return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS; |
| 189 | } |
| 190 | |
| 191 | #else /* full speed (low speed doesn't do bulk) */ |
| 192 | |
| 193 | #define qmult 1 |
| 194 | |
| 195 | #define DEVSPEED USB_SPEED_FULL |
| 196 | |
| 197 | #define qlen(gadget) DEFAULT_QLEN |
| 198 | |
| 199 | static inline int BITRATE(struct usb_gadget *g) |
| 200 | { |
| 201 | return FS_BPS; |
| 202 | } |
| 203 | #endif |
| 204 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 205 | /*-------------------------------------------------------------------------*/ |
| 206 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 207 | /* |
| 208 | * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 209 | * Instead: allocate your own, using normal USB-IF procedures. |
| 210 | */ |
| 211 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 212 | /* |
| 213 | * Thanks to NetChip Technologies for donating this product ID. |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 214 | * It's for devices with only CDC Ethernet configurations. |
| 215 | */ |
| 216 | #define CDC_VENDOR_NUM 0x0525 /* NetChip */ |
| 217 | #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */ |
| 218 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 219 | /* |
| 220 | * For hardware that can't talk CDC, we use the same vendor ID that |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 221 | * ARM Linux has used for ethernet-over-usb, both with sa1100 and |
| 222 | * with pxa250. We're protocol-compatible, if the host-side drivers |
| 223 | * use the endpoint descriptors. bcdDevice (version) is nonzero, so |
| 224 | * drivers that need to hard-wire endpoint numbers have a hook. |
| 225 | * |
| 226 | * The protocol is a minimal subset of CDC Ether, which works on any bulk |
| 227 | * hardware that's not deeply broken ... even on hardware that can't talk |
| 228 | * RNDIS (like SA-1100, with no interrupt endpoint, or anything that |
| 229 | * doesn't handle control-OUT). |
| 230 | */ |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 231 | #define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */ |
| 232 | #define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */ |
| 233 | |
| 234 | /* |
| 235 | * For hardware that can talk RNDIS and either of the above protocols, |
| 236 | * use this ID ... the windows INF files will know it. Unless it's |
| 237 | * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose |
| 238 | * the non-RNDIS configuration. |
| 239 | */ |
| 240 | #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */ |
| 241 | #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */ |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 242 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 243 | /* |
| 244 | * Some systems will want different product identifers published in the |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 245 | * device descriptor, either numbers or strings or both. These string |
| 246 | * parameters are in UTF-8 (superset of ASCII's 7 bit characters). |
| 247 | */ |
| 248 | |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 249 | /* |
| 250 | * Emulating them in eth_bind: |
| 251 | * static ushort idVendor; |
| 252 | * static ushort idProduct; |
| 253 | */ |
| 254 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 255 | #if defined(CONFIG_USBNET_MANUFACTURER) |
| 256 | static char *iManufacturer = CONFIG_USBNET_MANUFACTURER; |
| 257 | #else |
| 258 | static char *iManufacturer = "U-boot"; |
| 259 | #endif |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 260 | |
| 261 | /* These probably need to be configurable. */ |
| 262 | static ushort bcdDevice; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 263 | static char *iProduct; |
| 264 | static char *iSerialNumber; |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 265 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 266 | static char dev_addr[18]; |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 267 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 268 | static char host_addr[18]; |
| 269 | |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 270 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 271 | /*-------------------------------------------------------------------------*/ |
| 272 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 273 | /* |
| 274 | * USB DRIVER HOOKUP (to the hardware driver, below us), mostly |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 275 | * ep0 implementation: descriptors, config management, setup(). |
| 276 | * also optional class-specific notification interrupt transfer. |
| 277 | */ |
| 278 | |
| 279 | /* |
| 280 | * DESCRIPTORS ... most are static, but strings and (full) configuration |
| 281 | * descriptors are built on demand. For now we do either full CDC, or |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 282 | * our simple subset, with RNDIS as an optional second configuration. |
| 283 | * |
| 284 | * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But |
| 285 | * the class descriptors match a modem (they're ignored; it's really just |
| 286 | * Ethernet functionality), they don't need the NOP altsetting, and the |
| 287 | * status transfer endpoint isn't optional. |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 288 | */ |
| 289 | |
| 290 | #define STRING_MANUFACTURER 1 |
| 291 | #define STRING_PRODUCT 2 |
| 292 | #define STRING_ETHADDR 3 |
| 293 | #define STRING_DATA 4 |
| 294 | #define STRING_CONTROL 5 |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 295 | #define STRING_RNDIS_CONTROL 6 |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 296 | #define STRING_CDC 7 |
| 297 | #define STRING_SUBSET 8 |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 298 | #define STRING_RNDIS 9 |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 299 | #define STRING_SERIALNUMBER 10 |
| 300 | |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 301 | /* holds our biggest descriptor (or RNDIS response) */ |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 302 | #define USB_BUFSIZ 256 |
| 303 | |
| 304 | /* |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 305 | * This device advertises one configuration, eth_config, unless RNDIS |
| 306 | * is enabled (rndis_config) on hardware supporting at least two configs. |
| 307 | * |
| 308 | * NOTE: Controllers like superh_udc should probably be able to use |
| 309 | * an RNDIS-only configuration. |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 310 | * |
| 311 | * FIXME define some higher-powered configurations to make it easier |
| 312 | * to recharge batteries ... |
| 313 | */ |
| 314 | |
| 315 | #define DEV_CONFIG_VALUE 1 /* cdc or subset */ |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 316 | #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */ |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 317 | |
| 318 | static struct usb_device_descriptor |
| 319 | device_desc = { |
| 320 | .bLength = sizeof device_desc, |
| 321 | .bDescriptorType = USB_DT_DEVICE, |
| 322 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 323 | .bcdUSB = __constant_cpu_to_le16(0x0200), |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 324 | |
| 325 | .bDeviceClass = USB_CLASS_COMM, |
| 326 | .bDeviceSubClass = 0, |
| 327 | .bDeviceProtocol = 0, |
| 328 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 329 | .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM), |
| 330 | .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM), |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 331 | .iManufacturer = STRING_MANUFACTURER, |
| 332 | .iProduct = STRING_PRODUCT, |
| 333 | .bNumConfigurations = 1, |
| 334 | }; |
| 335 | |
| 336 | static struct usb_otg_descriptor |
| 337 | otg_descriptor = { |
| 338 | .bLength = sizeof otg_descriptor, |
| 339 | .bDescriptorType = USB_DT_OTG, |
| 340 | |
| 341 | .bmAttributes = USB_OTG_SRP, |
| 342 | }; |
| 343 | |
| 344 | static struct usb_config_descriptor |
| 345 | eth_config = { |
| 346 | .bLength = sizeof eth_config, |
| 347 | .bDescriptorType = USB_DT_CONFIG, |
| 348 | |
| 349 | /* compute wTotalLength on the fly */ |
| 350 | .bNumInterfaces = 2, |
| 351 | .bConfigurationValue = DEV_CONFIG_VALUE, |
| 352 | .iConfiguration = STRING_CDC, |
| 353 | .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, |
| 354 | .bMaxPower = 1, |
| 355 | }; |
| 356 | |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 357 | #ifdef CONFIG_USB_ETH_RNDIS |
| 358 | static struct usb_config_descriptor |
| 359 | rndis_config = { |
| 360 | .bLength = sizeof rndis_config, |
| 361 | .bDescriptorType = USB_DT_CONFIG, |
| 362 | |
| 363 | /* compute wTotalLength on the fly */ |
| 364 | .bNumInterfaces = 2, |
| 365 | .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE, |
| 366 | .iConfiguration = STRING_RNDIS, |
| 367 | .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, |
| 368 | .bMaxPower = 1, |
| 369 | }; |
| 370 | #endif |
| 371 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 372 | /* |
| 373 | * Compared to the simple CDC subset, the full CDC Ethernet model adds |
| 374 | * three class descriptors, two interface descriptors, optional status |
| 375 | * endpoint. Both have a "data" interface and two bulk endpoints. |
| 376 | * There are also differences in how control requests are handled. |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 377 | * |
| 378 | * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the |
| 379 | * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it |
| 380 | * may hang or oops. Since bugfixes (or accurate specs, letting Linux |
| 381 | * work around those bugs) are unlikely to ever come from MSFT, you may |
| 382 | * wish to avoid using RNDIS. |
| 383 | * |
| 384 | * MCCI offers an alternative to RNDIS if you need to connect to Windows |
| 385 | * but have hardware that can't support CDC Ethernet. We add descriptors |
| 386 | * to present the CDC Subset as a (nonconformant) CDC MDLM variant called |
| 387 | * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can |
| 388 | * get those drivers from MCCI, or bundled with various products. |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 389 | */ |
| 390 | |
Lukasz Dalek | 2bb3788 | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 391 | #ifdef CONFIG_USB_ETH_CDC |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 392 | static struct usb_interface_descriptor |
| 393 | control_intf = { |
| 394 | .bLength = sizeof control_intf, |
| 395 | .bDescriptorType = USB_DT_INTERFACE, |
| 396 | |
| 397 | .bInterfaceNumber = 0, |
| 398 | /* status endpoint is optional; this may be patched later */ |
| 399 | .bNumEndpoints = 1, |
| 400 | .bInterfaceClass = USB_CLASS_COMM, |
| 401 | .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, |
| 402 | .bInterfaceProtocol = USB_CDC_PROTO_NONE, |
| 403 | .iInterface = STRING_CONTROL, |
| 404 | }; |
| 405 | #endif |
| 406 | |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 407 | #ifdef CONFIG_USB_ETH_RNDIS |
| 408 | static const struct usb_interface_descriptor |
| 409 | rndis_control_intf = { |
| 410 | .bLength = sizeof rndis_control_intf, |
| 411 | .bDescriptorType = USB_DT_INTERFACE, |
| 412 | |
| 413 | .bInterfaceNumber = 0, |
| 414 | .bNumEndpoints = 1, |
| 415 | .bInterfaceClass = USB_CLASS_COMM, |
| 416 | .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM, |
| 417 | .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR, |
| 418 | .iInterface = STRING_RNDIS_CONTROL, |
| 419 | }; |
| 420 | #endif |
| 421 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 422 | static const struct usb_cdc_header_desc header_desc = { |
| 423 | .bLength = sizeof header_desc, |
| 424 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 425 | .bDescriptorSubType = USB_CDC_HEADER_TYPE, |
| 426 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 427 | .bcdCDC = __constant_cpu_to_le16(0x0110), |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 428 | }; |
| 429 | |
Lukasz Dalek | 2bb3788 | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 430 | #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 431 | |
| 432 | static const struct usb_cdc_union_desc union_desc = { |
| 433 | .bLength = sizeof union_desc, |
| 434 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 435 | .bDescriptorSubType = USB_CDC_UNION_TYPE, |
| 436 | |
| 437 | .bMasterInterface0 = 0, /* index of control interface */ |
| 438 | .bSlaveInterface0 = 1, /* index of DATA interface */ |
| 439 | }; |
| 440 | |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 441 | #endif /* CDC || RNDIS */ |
| 442 | |
| 443 | #ifdef CONFIG_USB_ETH_RNDIS |
| 444 | |
| 445 | static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = { |
| 446 | .bLength = sizeof call_mgmt_descriptor, |
| 447 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 448 | .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE, |
| 449 | |
| 450 | .bmCapabilities = 0x00, |
| 451 | .bDataInterface = 0x01, |
| 452 | }; |
| 453 | |
| 454 | static const struct usb_cdc_acm_descriptor acm_descriptor = { |
| 455 | .bLength = sizeof acm_descriptor, |
| 456 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 457 | .bDescriptorSubType = USB_CDC_ACM_TYPE, |
| 458 | |
| 459 | .bmCapabilities = 0x00, |
| 460 | }; |
| 461 | |
| 462 | #endif |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 463 | |
Lukasz Dalek | 2bb3788 | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 464 | #ifndef CONFIG_USB_ETH_CDC |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 465 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 466 | /* |
| 467 | * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 468 | * ways: data endpoints live in the control interface, there's no data |
| 469 | * interface, and it's not used to talk to a cell phone radio. |
| 470 | */ |
| 471 | |
| 472 | static const struct usb_cdc_mdlm_desc mdlm_desc = { |
| 473 | .bLength = sizeof mdlm_desc, |
| 474 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 475 | .bDescriptorSubType = USB_CDC_MDLM_TYPE, |
| 476 | |
| 477 | .bcdVersion = __constant_cpu_to_le16(0x0100), |
| 478 | .bGUID = { |
| 479 | 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6, |
| 480 | 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f, |
| 481 | }, |
| 482 | }; |
| 483 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 484 | /* |
| 485 | * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 486 | * can't really use its struct. All we do here is say that we're using |
| 487 | * the submode of "SAFE" which directly matches the CDC Subset. |
| 488 | */ |
| 489 | static const u8 mdlm_detail_desc[] = { |
| 490 | 6, |
| 491 | USB_DT_CS_INTERFACE, |
| 492 | USB_CDC_MDLM_DETAIL_TYPE, |
| 493 | |
| 494 | 0, /* "SAFE" */ |
| 495 | 0, /* network control capabilities (none) */ |
| 496 | 0, /* network data capabilities ("raw" encapsulation) */ |
| 497 | }; |
| 498 | |
| 499 | #endif |
| 500 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 501 | static const struct usb_cdc_ether_desc ether_desc = { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 502 | .bLength = sizeof(ether_desc), |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 503 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 504 | .bDescriptorSubType = USB_CDC_ETHERNET_TYPE, |
| 505 | |
| 506 | /* this descriptor actually adds value, surprise! */ |
| 507 | .iMACAddress = STRING_ETHADDR, |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 508 | .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */ |
| 509 | .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN), |
| 510 | .wNumberMCFilters = __constant_cpu_to_le16(0), |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 511 | .bNumberPowerFilters = 0, |
| 512 | }; |
| 513 | |
Lukasz Dalek | 2bb3788 | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 514 | #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 515 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 516 | /* |
| 517 | * include the status endpoint if we can, even where it's optional. |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 518 | * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one |
| 519 | * packet, to simplify cancellation; and a big transfer interval, to |
| 520 | * waste less bandwidth. |
| 521 | * |
| 522 | * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even |
| 523 | * if they ignore the connect/disconnect notifications that real aether |
| 524 | * can provide. more advanced cdc configurations might want to support |
| 525 | * encapsulated commands (vendor-specific, using control-OUT). |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 526 | * |
| 527 | * RNDIS requires the status endpoint, since it uses that encapsulation |
| 528 | * mechanism for its funky RPC scheme. |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 529 | */ |
| 530 | |
| 531 | #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */ |
| 532 | #define STATUS_BYTECOUNT 16 /* 8 byte header + data */ |
| 533 | |
| 534 | static struct usb_endpoint_descriptor |
| 535 | fs_status_desc = { |
| 536 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 537 | .bDescriptorType = USB_DT_ENDPOINT, |
| 538 | |
| 539 | .bEndpointAddress = USB_DIR_IN, |
| 540 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 541 | .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT), |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 542 | .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC, |
| 543 | }; |
| 544 | #endif |
| 545 | |
Lukasz Dalek | 2bb3788 | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 546 | #ifdef CONFIG_USB_ETH_CDC |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 547 | |
| 548 | /* the default data interface has no endpoints ... */ |
| 549 | |
| 550 | static const struct usb_interface_descriptor |
| 551 | data_nop_intf = { |
| 552 | .bLength = sizeof data_nop_intf, |
| 553 | .bDescriptorType = USB_DT_INTERFACE, |
| 554 | |
| 555 | .bInterfaceNumber = 1, |
| 556 | .bAlternateSetting = 0, |
| 557 | .bNumEndpoints = 0, |
| 558 | .bInterfaceClass = USB_CLASS_CDC_DATA, |
| 559 | .bInterfaceSubClass = 0, |
| 560 | .bInterfaceProtocol = 0, |
| 561 | }; |
| 562 | |
| 563 | /* ... but the "real" data interface has two bulk endpoints */ |
| 564 | |
| 565 | static const struct usb_interface_descriptor |
| 566 | data_intf = { |
| 567 | .bLength = sizeof data_intf, |
| 568 | .bDescriptorType = USB_DT_INTERFACE, |
| 569 | |
| 570 | .bInterfaceNumber = 1, |
| 571 | .bAlternateSetting = 1, |
| 572 | .bNumEndpoints = 2, |
| 573 | .bInterfaceClass = USB_CLASS_CDC_DATA, |
| 574 | .bInterfaceSubClass = 0, |
| 575 | .bInterfaceProtocol = 0, |
| 576 | .iInterface = STRING_DATA, |
| 577 | }; |
| 578 | |
| 579 | #endif |
| 580 | |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 581 | #ifdef CONFIG_USB_ETH_RNDIS |
| 582 | |
| 583 | /* RNDIS doesn't activate by changing to the "real" altsetting */ |
| 584 | |
| 585 | static const struct usb_interface_descriptor |
| 586 | rndis_data_intf = { |
| 587 | .bLength = sizeof rndis_data_intf, |
| 588 | .bDescriptorType = USB_DT_INTERFACE, |
| 589 | |
| 590 | .bInterfaceNumber = 1, |
| 591 | .bAlternateSetting = 0, |
| 592 | .bNumEndpoints = 2, |
| 593 | .bInterfaceClass = USB_CLASS_CDC_DATA, |
| 594 | .bInterfaceSubClass = 0, |
| 595 | .bInterfaceProtocol = 0, |
| 596 | .iInterface = STRING_DATA, |
| 597 | }; |
| 598 | |
| 599 | #endif |
| 600 | |
Lukasz Dalek | 2bb3788 | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 601 | #ifdef CONFIG_USB_ETH_SUBSET |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 602 | |
| 603 | /* |
| 604 | * "Simple" CDC-subset option is a simple vendor-neutral model that most |
| 605 | * full speed controllers can handle: one interface, two bulk endpoints. |
| 606 | * |
| 607 | * To assist host side drivers, we fancy it up a bit, and add descriptors |
| 608 | * so some host side drivers will understand it as a "SAFE" variant. |
| 609 | */ |
| 610 | |
| 611 | static const struct usb_interface_descriptor |
| 612 | subset_data_intf = { |
| 613 | .bLength = sizeof subset_data_intf, |
| 614 | .bDescriptorType = USB_DT_INTERFACE, |
| 615 | |
| 616 | .bInterfaceNumber = 0, |
| 617 | .bAlternateSetting = 0, |
| 618 | .bNumEndpoints = 2, |
| 619 | .bInterfaceClass = USB_CLASS_COMM, |
| 620 | .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM, |
| 621 | .bInterfaceProtocol = 0, |
| 622 | .iInterface = STRING_DATA, |
| 623 | }; |
| 624 | |
| 625 | #endif /* SUBSET */ |
| 626 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 627 | static struct usb_endpoint_descriptor |
| 628 | fs_source_desc = { |
| 629 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 630 | .bDescriptorType = USB_DT_ENDPOINT, |
| 631 | |
| 632 | .bEndpointAddress = USB_DIR_IN, |
| 633 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Troy Kisky | 43880ce | 2013-09-25 18:41:04 -0700 | [diff] [blame] | 634 | .wMaxPacketSize = __constant_cpu_to_le16(64), |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 635 | }; |
| 636 | |
| 637 | static struct usb_endpoint_descriptor |
| 638 | fs_sink_desc = { |
| 639 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 640 | .bDescriptorType = USB_DT_ENDPOINT, |
| 641 | |
| 642 | .bEndpointAddress = USB_DIR_OUT, |
| 643 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Troy Kisky | 43880ce | 2013-09-25 18:41:04 -0700 | [diff] [blame] | 644 | .wMaxPacketSize = __constant_cpu_to_le16(64), |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 645 | }; |
| 646 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 647 | static const struct usb_descriptor_header *fs_eth_function[11] = { |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 648 | (struct usb_descriptor_header *) &otg_descriptor, |
Lukasz Dalek | 2bb3788 | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 649 | #ifdef CONFIG_USB_ETH_CDC |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 650 | /* "cdc" mode descriptors */ |
| 651 | (struct usb_descriptor_header *) &control_intf, |
| 652 | (struct usb_descriptor_header *) &header_desc, |
| 653 | (struct usb_descriptor_header *) &union_desc, |
| 654 | (struct usb_descriptor_header *) ðer_desc, |
| 655 | /* NOTE: status endpoint may need to be removed */ |
| 656 | (struct usb_descriptor_header *) &fs_status_desc, |
| 657 | /* data interface, with altsetting */ |
| 658 | (struct usb_descriptor_header *) &data_nop_intf, |
| 659 | (struct usb_descriptor_header *) &data_intf, |
| 660 | (struct usb_descriptor_header *) &fs_source_desc, |
| 661 | (struct usb_descriptor_header *) &fs_sink_desc, |
| 662 | NULL, |
Lukasz Dalek | 2bb3788 | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 663 | #endif /* CONFIG_USB_ETH_CDC */ |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 664 | }; |
| 665 | |
| 666 | static inline void fs_subset_descriptors(void) |
| 667 | { |
Lukasz Dalek | 2bb3788 | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 668 | #ifdef CONFIG_USB_ETH_SUBSET |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 669 | /* behavior is "CDC Subset"; extra descriptors say "SAFE" */ |
| 670 | fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf; |
| 671 | fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc; |
| 672 | fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc; |
| 673 | fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc; |
| 674 | fs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc; |
| 675 | fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc; |
| 676 | fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc; |
| 677 | fs_eth_function[8] = NULL; |
| 678 | #else |
| 679 | fs_eth_function[1] = NULL; |
| 680 | #endif |
| 681 | } |
| 682 | |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 683 | #ifdef CONFIG_USB_ETH_RNDIS |
| 684 | static const struct usb_descriptor_header *fs_rndis_function[] = { |
| 685 | (struct usb_descriptor_header *) &otg_descriptor, |
| 686 | /* control interface matches ACM, not Ethernet */ |
| 687 | (struct usb_descriptor_header *) &rndis_control_intf, |
| 688 | (struct usb_descriptor_header *) &header_desc, |
| 689 | (struct usb_descriptor_header *) &call_mgmt_descriptor, |
| 690 | (struct usb_descriptor_header *) &acm_descriptor, |
| 691 | (struct usb_descriptor_header *) &union_desc, |
| 692 | (struct usb_descriptor_header *) &fs_status_desc, |
| 693 | /* data interface has no altsetting */ |
| 694 | (struct usb_descriptor_header *) &rndis_data_intf, |
| 695 | (struct usb_descriptor_header *) &fs_source_desc, |
| 696 | (struct usb_descriptor_header *) &fs_sink_desc, |
| 697 | NULL, |
| 698 | }; |
| 699 | #endif |
| 700 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 701 | /* |
| 702 | * usb 2.0 devices need to expose both high speed and full speed |
| 703 | * descriptors, unless they only run at full speed. |
| 704 | */ |
| 705 | |
Lukasz Dalek | 2bb3788 | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 706 | #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 707 | static struct usb_endpoint_descriptor |
| 708 | hs_status_desc = { |
| 709 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 710 | .bDescriptorType = USB_DT_ENDPOINT, |
| 711 | |
| 712 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 713 | .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT), |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 714 | .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4, |
| 715 | }; |
Lukasz Dalek | 2bb3788 | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 716 | #endif /* CONFIG_USB_ETH_CDC */ |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 717 | |
| 718 | static struct usb_endpoint_descriptor |
| 719 | hs_source_desc = { |
| 720 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 721 | .bDescriptorType = USB_DT_ENDPOINT, |
| 722 | |
| 723 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 724 | .wMaxPacketSize = __constant_cpu_to_le16(512), |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 725 | }; |
| 726 | |
| 727 | static struct usb_endpoint_descriptor |
| 728 | hs_sink_desc = { |
| 729 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 730 | .bDescriptorType = USB_DT_ENDPOINT, |
| 731 | |
| 732 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 733 | .wMaxPacketSize = __constant_cpu_to_le16(512), |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 734 | }; |
| 735 | |
| 736 | static struct usb_qualifier_descriptor |
| 737 | dev_qualifier = { |
| 738 | .bLength = sizeof dev_qualifier, |
| 739 | .bDescriptorType = USB_DT_DEVICE_QUALIFIER, |
| 740 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 741 | .bcdUSB = __constant_cpu_to_le16(0x0200), |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 742 | .bDeviceClass = USB_CLASS_COMM, |
| 743 | |
| 744 | .bNumConfigurations = 1, |
| 745 | }; |
| 746 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 747 | static const struct usb_descriptor_header *hs_eth_function[11] = { |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 748 | (struct usb_descriptor_header *) &otg_descriptor, |
Lukasz Dalek | 2bb3788 | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 749 | #ifdef CONFIG_USB_ETH_CDC |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 750 | /* "cdc" mode descriptors */ |
| 751 | (struct usb_descriptor_header *) &control_intf, |
| 752 | (struct usb_descriptor_header *) &header_desc, |
| 753 | (struct usb_descriptor_header *) &union_desc, |
| 754 | (struct usb_descriptor_header *) ðer_desc, |
| 755 | /* NOTE: status endpoint may need to be removed */ |
| 756 | (struct usb_descriptor_header *) &hs_status_desc, |
| 757 | /* data interface, with altsetting */ |
| 758 | (struct usb_descriptor_header *) &data_nop_intf, |
| 759 | (struct usb_descriptor_header *) &data_intf, |
| 760 | (struct usb_descriptor_header *) &hs_source_desc, |
| 761 | (struct usb_descriptor_header *) &hs_sink_desc, |
| 762 | NULL, |
Lukasz Dalek | 2bb3788 | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 763 | #endif /* CONFIG_USB_ETH_CDC */ |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 764 | }; |
| 765 | |
| 766 | static inline void hs_subset_descriptors(void) |
| 767 | { |
Lukasz Dalek | 2bb3788 | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 768 | #ifdef CONFIG_USB_ETH_SUBSET |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 769 | /* behavior is "CDC Subset"; extra descriptors say "SAFE" */ |
| 770 | hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf; |
| 771 | hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc; |
| 772 | hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc; |
| 773 | hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc; |
| 774 | hs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc; |
| 775 | hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc; |
| 776 | hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc; |
| 777 | hs_eth_function[8] = NULL; |
| 778 | #else |
| 779 | hs_eth_function[1] = NULL; |
| 780 | #endif |
| 781 | } |
| 782 | |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 783 | #ifdef CONFIG_USB_ETH_RNDIS |
| 784 | static const struct usb_descriptor_header *hs_rndis_function[] = { |
| 785 | (struct usb_descriptor_header *) &otg_descriptor, |
| 786 | /* control interface matches ACM, not Ethernet */ |
| 787 | (struct usb_descriptor_header *) &rndis_control_intf, |
| 788 | (struct usb_descriptor_header *) &header_desc, |
| 789 | (struct usb_descriptor_header *) &call_mgmt_descriptor, |
| 790 | (struct usb_descriptor_header *) &acm_descriptor, |
| 791 | (struct usb_descriptor_header *) &union_desc, |
| 792 | (struct usb_descriptor_header *) &hs_status_desc, |
| 793 | /* data interface has no altsetting */ |
| 794 | (struct usb_descriptor_header *) &rndis_data_intf, |
| 795 | (struct usb_descriptor_header *) &hs_source_desc, |
| 796 | (struct usb_descriptor_header *) &hs_sink_desc, |
| 797 | NULL, |
| 798 | }; |
| 799 | #endif |
| 800 | |
| 801 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 802 | /* maxpacket and other transfer characteristics vary by speed. */ |
| 803 | static inline struct usb_endpoint_descriptor * |
| 804 | ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs, |
| 805 | struct usb_endpoint_descriptor *fs) |
| 806 | { |
| 807 | if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) |
| 808 | return hs; |
| 809 | return fs; |
| 810 | } |
| 811 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 812 | /*-------------------------------------------------------------------------*/ |
| 813 | |
| 814 | /* descriptors that are built on-demand */ |
| 815 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 816 | static char manufacturer[50]; |
| 817 | static char product_desc[40] = DRIVER_DESC; |
| 818 | static char serial_number[20]; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 819 | |
| 820 | /* address that the host will use ... usually assigned at random */ |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 821 | static char ethaddr[2 * ETH_ALEN + 1]; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 822 | |
| 823 | /* static strings, in UTF-8 */ |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 824 | static struct usb_string strings[] = { |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 825 | { STRING_MANUFACTURER, manufacturer, }, |
| 826 | { STRING_PRODUCT, product_desc, }, |
| 827 | { STRING_SERIALNUMBER, serial_number, }, |
| 828 | { STRING_DATA, "Ethernet Data", }, |
| 829 | { STRING_ETHADDR, ethaddr, }, |
Lukasz Dalek | 2bb3788 | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 830 | #ifdef CONFIG_USB_ETH_CDC |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 831 | { STRING_CDC, "CDC Ethernet", }, |
| 832 | { STRING_CONTROL, "CDC Communications Control", }, |
| 833 | #endif |
Lukasz Dalek | 2bb3788 | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 834 | #ifdef CONFIG_USB_ETH_SUBSET |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 835 | { STRING_SUBSET, "CDC Ethernet Subset", }, |
| 836 | #endif |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 837 | #ifdef CONFIG_USB_ETH_RNDIS |
| 838 | { STRING_RNDIS, "RNDIS", }, |
| 839 | { STRING_RNDIS_CONTROL, "RNDIS Communications Control", }, |
| 840 | #endif |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 841 | { } /* end of list */ |
| 842 | }; |
| 843 | |
| 844 | static struct usb_gadget_strings stringtab = { |
| 845 | .language = 0x0409, /* en-us */ |
| 846 | .strings = strings, |
| 847 | }; |
| 848 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 849 | /*============================================================================*/ |
Joel Fernandes | 5290759 | 2013-09-04 18:55:14 -0500 | [diff] [blame] | 850 | DEFINE_CACHE_ALIGN_BUFFER(u8, control_req, USB_BUFSIZ); |
| 851 | |
Lukasz Dalek | 2bb3788 | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 852 | #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS) |
Joel Fernandes | 5290759 | 2013-09-04 18:55:14 -0500 | [diff] [blame] | 853 | DEFINE_CACHE_ALIGN_BUFFER(u8, status_req, STATUS_BYTECOUNT); |
Lukasz Dalek | 563aed2 | 2012-10-02 17:04:29 +0200 | [diff] [blame] | 854 | #endif |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 855 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 856 | /*============================================================================*/ |
| 857 | |
| 858 | /* |
| 859 | * one config, two interfaces: control, data. |
| 860 | * complications: class descriptors, and an altsetting. |
| 861 | */ |
| 862 | static int |
| 863 | config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg) |
| 864 | { |
| 865 | int len; |
| 866 | const struct usb_config_descriptor *config; |
| 867 | const struct usb_descriptor_header **function; |
| 868 | int hs = 0; |
| 869 | |
| 870 | if (gadget_is_dualspeed(g)) { |
| 871 | hs = (g->speed == USB_SPEED_HIGH); |
| 872 | if (type == USB_DT_OTHER_SPEED_CONFIG) |
| 873 | hs = !hs; |
| 874 | } |
| 875 | #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function) |
| 876 | |
| 877 | if (index >= device_desc.bNumConfigurations) |
| 878 | return -EINVAL; |
| 879 | |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 880 | #ifdef CONFIG_USB_ETH_RNDIS |
| 881 | /* |
| 882 | * list the RNDIS config first, to make Microsoft's drivers |
| 883 | * happy. DOCSIS 1.0 needs this too. |
| 884 | */ |
| 885 | if (device_desc.bNumConfigurations == 2 && index == 0) { |
| 886 | config = &rndis_config; |
| 887 | function = which_fn(rndis); |
| 888 | } else |
| 889 | #endif |
| 890 | { |
| 891 | config = ð_config; |
| 892 | function = which_fn(eth); |
| 893 | } |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 894 | |
| 895 | /* for now, don't advertise srp-only devices */ |
| 896 | if (!is_otg) |
| 897 | function++; |
| 898 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 899 | len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 900 | if (len < 0) |
| 901 | return len; |
| 902 | ((struct usb_config_descriptor *) buf)->bDescriptorType = type; |
| 903 | return len; |
| 904 | } |
| 905 | |
| 906 | /*-------------------------------------------------------------------------*/ |
| 907 | |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 908 | static void eth_start(struct eth_dev *dev, gfp_t gfp_flags); |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 909 | static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 910 | |
| 911 | static int |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 912 | set_ether_config(struct eth_dev *dev, gfp_t gfp_flags) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 913 | { |
| 914 | int result = 0; |
| 915 | struct usb_gadget *gadget = dev->gadget; |
| 916 | |
Lukasz Dalek | 2bb3788 | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 917 | #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS) |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 918 | /* status endpoint used for RNDIS and (optionally) CDC */ |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 919 | if (!subset_active(dev) && dev->status_ep) { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 920 | dev->status = ep_desc(gadget, &hs_status_desc, |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 921 | &fs_status_desc); |
| 922 | dev->status_ep->driver_data = dev; |
| 923 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 924 | result = usb_ep_enable(dev->status_ep, dev->status); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 925 | if (result != 0) { |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 926 | debug("enable %s --> %d\n", |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 927 | dev->status_ep->name, result); |
| 928 | goto done; |
| 929 | } |
| 930 | } |
| 931 | #endif |
| 932 | |
| 933 | dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc); |
| 934 | dev->in_ep->driver_data = dev; |
| 935 | |
| 936 | dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc); |
| 937 | dev->out_ep->driver_data = dev; |
| 938 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 939 | /* |
| 940 | * With CDC, the host isn't allowed to use these two data |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 941 | * endpoints in the default altsetting for the interface. |
| 942 | * so we don't activate them yet. Reset from SET_INTERFACE. |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 943 | * |
| 944 | * Strictly speaking RNDIS should work the same: activation is |
| 945 | * a side effect of setting a packet filter. Deactivation is |
| 946 | * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG. |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 947 | */ |
| 948 | if (!cdc_active(dev)) { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 949 | result = usb_ep_enable(dev->in_ep, dev->in); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 950 | if (result != 0) { |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 951 | debug("enable %s --> %d\n", |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 952 | dev->in_ep->name, result); |
| 953 | goto done; |
| 954 | } |
| 955 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 956 | result = usb_ep_enable(dev->out_ep, dev->out); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 957 | if (result != 0) { |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 958 | debug("enable %s --> %d\n", |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 959 | dev->out_ep->name, result); |
| 960 | goto done; |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | done: |
| 965 | if (result == 0) |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 966 | result = alloc_requests(dev, qlen(gadget), gfp_flags); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 967 | |
| 968 | /* on error, disable any endpoints */ |
| 969 | if (result < 0) { |
Vitaly Kuzmichev | d5292c1 | 2010-08-13 17:02:29 +0400 | [diff] [blame] | 970 | if (!subset_active(dev) && dev->status_ep) |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 971 | (void) usb_ep_disable(dev->status_ep); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 972 | dev->status = NULL; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 973 | (void) usb_ep_disable(dev->in_ep); |
| 974 | (void) usb_ep_disable(dev->out_ep); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 975 | dev->in = NULL; |
| 976 | dev->out = NULL; |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 977 | } else if (!cdc_active(dev)) { |
| 978 | /* |
| 979 | * activate non-CDC configs right away |
| 980 | * this isn't strictly according to the RNDIS spec |
| 981 | */ |
| 982 | eth_start(dev, GFP_ATOMIC); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | /* caller is responsible for cleanup on error */ |
| 986 | return result; |
| 987 | } |
| 988 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 989 | static void eth_reset_config(struct eth_dev *dev) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 990 | { |
| 991 | if (dev->config == 0) |
| 992 | return; |
| 993 | |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 994 | debug("%s\n", __func__); |
| 995 | |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 996 | rndis_uninit(dev->rndis_config); |
| 997 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 998 | /* |
| 999 | * disable endpoints, forcing (synchronous) completion of |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1000 | * pending i/o. then free the requests. |
| 1001 | */ |
| 1002 | |
| 1003 | if (dev->in) { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1004 | usb_ep_disable(dev->in_ep); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1005 | if (dev->tx_req) { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1006 | usb_ep_free_request(dev->in_ep, dev->tx_req); |
| 1007 | dev->tx_req = NULL; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1008 | } |
| 1009 | } |
| 1010 | if (dev->out) { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1011 | usb_ep_disable(dev->out_ep); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1012 | if (dev->rx_req) { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1013 | usb_ep_free_request(dev->out_ep, dev->rx_req); |
| 1014 | dev->rx_req = NULL; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1015 | } |
| 1016 | } |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1017 | if (dev->status) |
| 1018 | usb_ep_disable(dev->status_ep); |
| 1019 | |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1020 | dev->rndis = 0; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1021 | dev->cdc_filter = 0; |
| 1022 | dev->config = 0; |
| 1023 | } |
| 1024 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1025 | /* |
| 1026 | * change our operational config. must agree with the code |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1027 | * that returns config descriptors, and altsetting code. |
| 1028 | */ |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1029 | static int eth_set_config(struct eth_dev *dev, unsigned number, |
| 1030 | gfp_t gfp_flags) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1031 | { |
| 1032 | int result = 0; |
| 1033 | struct usb_gadget *gadget = dev->gadget; |
| 1034 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1035 | if (gadget_is_sa1100(gadget) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1036 | && dev->config |
| 1037 | && dev->tx_qlen != 0) { |
| 1038 | /* tx fifo is full, but we can't clear it...*/ |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1039 | error("can't change configurations"); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1040 | return -ESPIPE; |
| 1041 | } |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1042 | eth_reset_config(dev); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1043 | |
| 1044 | switch (number) { |
| 1045 | case DEV_CONFIG_VALUE: |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1046 | result = set_ether_config(dev, gfp_flags); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1047 | break; |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1048 | #ifdef CONFIG_USB_ETH_RNDIS |
| 1049 | case DEV_RNDIS_CONFIG_VALUE: |
| 1050 | dev->rndis = 1; |
| 1051 | result = set_ether_config(dev, gfp_flags); |
| 1052 | break; |
| 1053 | #endif |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1054 | default: |
| 1055 | result = -EINVAL; |
| 1056 | /* FALL THROUGH */ |
| 1057 | case 0: |
| 1058 | break; |
| 1059 | } |
| 1060 | |
| 1061 | if (result) { |
| 1062 | if (number) |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1063 | eth_reset_config(dev); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1064 | usb_gadget_vbus_draw(dev->gadget, |
| 1065 | gadget_is_otg(dev->gadget) ? 8 : 100); |
| 1066 | } else { |
| 1067 | char *speed; |
| 1068 | unsigned power; |
| 1069 | |
| 1070 | power = 2 * eth_config.bMaxPower; |
| 1071 | usb_gadget_vbus_draw(dev->gadget, power); |
| 1072 | |
| 1073 | switch (gadget->speed) { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1074 | case USB_SPEED_FULL: |
| 1075 | speed = "full"; break; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1076 | #ifdef CONFIG_USB_GADGET_DUALSPEED |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1077 | case USB_SPEED_HIGH: |
| 1078 | speed = "high"; break; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1079 | #endif |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1080 | default: |
| 1081 | speed = "?"; break; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | dev->config = number; |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1085 | printf("%s speed config #%d: %d mA, %s, using %s\n", |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1086 | speed, number, power, driver_desc, |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1087 | rndis_active(dev) |
| 1088 | ? "RNDIS" |
| 1089 | : (cdc_active(dev) |
| 1090 | ? "CDC Ethernet" |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1091 | : "CDC Ethernet Subset")); |
| 1092 | } |
| 1093 | return result; |
| 1094 | } |
| 1095 | |
| 1096 | /*-------------------------------------------------------------------------*/ |
| 1097 | |
Lukasz Dalek | 2bb3788 | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 1098 | #ifdef CONFIG_USB_ETH_CDC |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1099 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1100 | /* |
| 1101 | * The interrupt endpoint is used in CDC networking models (Ethernet, ATM) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1102 | * only to notify the host about link status changes (which we support) or |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1103 | * report completion of some encapsulated command (as used in RNDIS). Since |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1104 | * we want this CDC Ethernet code to be vendor-neutral, we don't use that |
| 1105 | * command mechanism; and only one status request is ever queued. |
| 1106 | */ |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1107 | static void eth_status_complete(struct usb_ep *ep, struct usb_request *req) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1108 | { |
| 1109 | struct usb_cdc_notification *event = req->buf; |
| 1110 | int value = req->status; |
| 1111 | struct eth_dev *dev = ep->driver_data; |
| 1112 | |
| 1113 | /* issue the second notification if host reads the first */ |
| 1114 | if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION |
| 1115 | && value == 0) { |
| 1116 | __le32 *data = req->buf + sizeof *event; |
| 1117 | |
| 1118 | event->bmRequestType = 0xA1; |
| 1119 | event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1120 | event->wValue = __constant_cpu_to_le16(0); |
| 1121 | event->wIndex = __constant_cpu_to_le16(1); |
| 1122 | event->wLength = __constant_cpu_to_le16(8); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1123 | |
| 1124 | /* SPEED_CHANGE data is up/down speeds in bits/sec */ |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1125 | data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget)); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1126 | |
| 1127 | req->length = STATUS_BYTECOUNT; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1128 | value = usb_ep_queue(ep, req, GFP_ATOMIC); |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1129 | debug("send SPEED_CHANGE --> %d\n", value); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1130 | if (value == 0) |
| 1131 | return; |
| 1132 | } else if (value != -ECONNRESET) { |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1133 | debug("event %02x --> %d\n", |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1134 | event->bNotificationType, value); |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1135 | if (event->bNotificationType == |
| 1136 | USB_CDC_NOTIFY_SPEED_CHANGE) { |
| 1137 | l_ethdev.network_started = 1; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1138 | printf("USB network up!\n"); |
| 1139 | } |
| 1140 | } |
| 1141 | req->context = NULL; |
| 1142 | } |
| 1143 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1144 | static void issue_start_status(struct eth_dev *dev) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1145 | { |
| 1146 | struct usb_request *req = dev->stat_req; |
| 1147 | struct usb_cdc_notification *event; |
| 1148 | int value; |
| 1149 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1150 | /* |
| 1151 | * flush old status |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1152 | * |
| 1153 | * FIXME ugly idiom, maybe we'd be better with just |
| 1154 | * a "cancel the whole queue" primitive since any |
| 1155 | * unlink-one primitive has way too many error modes. |
| 1156 | * here, we "know" toggle is already clear... |
| 1157 | * |
| 1158 | * FIXME iff req->context != null just dequeue it |
| 1159 | */ |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1160 | usb_ep_disable(dev->status_ep); |
| 1161 | usb_ep_enable(dev->status_ep, dev->status); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1162 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1163 | /* |
| 1164 | * 3.8.1 says to issue first NETWORK_CONNECTION, then |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1165 | * a SPEED_CHANGE. could be useful in some configs. |
| 1166 | */ |
| 1167 | event = req->buf; |
| 1168 | event->bmRequestType = 0xA1; |
| 1169 | event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1170 | event->wValue = __constant_cpu_to_le16(1); /* connected */ |
| 1171 | event->wIndex = __constant_cpu_to_le16(1); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1172 | event->wLength = 0; |
| 1173 | |
| 1174 | req->length = sizeof *event; |
| 1175 | req->complete = eth_status_complete; |
| 1176 | req->context = dev; |
| 1177 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1178 | value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1179 | if (value < 0) |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1180 | debug("status buf queue --> %d\n", value); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1181 | } |
| 1182 | |
| 1183 | #endif |
| 1184 | |
| 1185 | /*-------------------------------------------------------------------------*/ |
| 1186 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1187 | static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1188 | { |
| 1189 | if (req->status || req->actual != req->length) |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1190 | debug("setup complete --> %d, %d/%d\n", |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1191 | req->status, req->actual, req->length); |
| 1192 | } |
| 1193 | |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1194 | #ifdef CONFIG_USB_ETH_RNDIS |
| 1195 | |
| 1196 | static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req) |
| 1197 | { |
| 1198 | if (req->status || req->actual != req->length) |
| 1199 | debug("rndis response complete --> %d, %d/%d\n", |
| 1200 | req->status, req->actual, req->length); |
| 1201 | |
| 1202 | /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */ |
| 1203 | } |
| 1204 | |
| 1205 | static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req) |
| 1206 | { |
| 1207 | struct eth_dev *dev = ep->driver_data; |
| 1208 | int status; |
| 1209 | |
| 1210 | /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */ |
| 1211 | status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf); |
| 1212 | if (status < 0) |
| 1213 | error("%s: rndis parse error %d", __func__, status); |
| 1214 | } |
| 1215 | |
| 1216 | #endif /* RNDIS */ |
| 1217 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1218 | /* |
| 1219 | * The setup() callback implements all the ep0 functionality that's not |
| 1220 | * handled lower down. CDC has a number of less-common features: |
| 1221 | * |
| 1222 | * - two interfaces: control, and ethernet data |
| 1223 | * - Ethernet data interface has two altsettings: default, and active |
| 1224 | * - class-specific descriptors for the control interface |
| 1225 | * - class-specific control requests |
| 1226 | */ |
| 1227 | static int |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1228 | eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1229 | { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1230 | struct eth_dev *dev = get_gadget_data(gadget); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1231 | struct usb_request *req = dev->req; |
| 1232 | int value = -EOPNOTSUPP; |
| 1233 | u16 wIndex = le16_to_cpu(ctrl->wIndex); |
| 1234 | u16 wValue = le16_to_cpu(ctrl->wValue); |
| 1235 | u16 wLength = le16_to_cpu(ctrl->wLength); |
| 1236 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1237 | /* |
| 1238 | * descriptors just go into the pre-allocated ep0 buffer, |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1239 | * while config change events may enable network traffic. |
| 1240 | */ |
| 1241 | |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1242 | debug("%s\n", __func__); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1243 | |
| 1244 | req->complete = eth_setup_complete; |
| 1245 | switch (ctrl->bRequest) { |
| 1246 | |
| 1247 | case USB_REQ_GET_DESCRIPTOR: |
| 1248 | if (ctrl->bRequestType != USB_DIR_IN) |
| 1249 | break; |
| 1250 | switch (wValue >> 8) { |
| 1251 | |
| 1252 | case USB_DT_DEVICE: |
Kishon Vijay Abraham I | 04afd5b | 2015-02-23 18:40:17 +0530 | [diff] [blame] | 1253 | device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1254 | value = min(wLength, (u16) sizeof device_desc); |
| 1255 | memcpy(req->buf, &device_desc, value); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1256 | break; |
| 1257 | case USB_DT_DEVICE_QUALIFIER: |
| 1258 | if (!gadget_is_dualspeed(gadget)) |
| 1259 | break; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1260 | value = min(wLength, (u16) sizeof dev_qualifier); |
| 1261 | memcpy(req->buf, &dev_qualifier, value); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1262 | break; |
| 1263 | |
| 1264 | case USB_DT_OTHER_SPEED_CONFIG: |
| 1265 | if (!gadget_is_dualspeed(gadget)) |
| 1266 | break; |
| 1267 | /* FALLTHROUGH */ |
| 1268 | case USB_DT_CONFIG: |
| 1269 | value = config_buf(gadget, req->buf, |
| 1270 | wValue >> 8, |
| 1271 | wValue & 0xff, |
| 1272 | gadget_is_otg(gadget)); |
| 1273 | if (value >= 0) |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1274 | value = min(wLength, (u16) value); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1275 | break; |
| 1276 | |
| 1277 | case USB_DT_STRING: |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1278 | value = usb_gadget_get_string(&stringtab, |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1279 | wValue & 0xff, req->buf); |
| 1280 | |
| 1281 | if (value >= 0) |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1282 | value = min(wLength, (u16) value); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1283 | |
| 1284 | break; |
| 1285 | } |
| 1286 | break; |
| 1287 | |
| 1288 | case USB_REQ_SET_CONFIGURATION: |
| 1289 | if (ctrl->bRequestType != 0) |
| 1290 | break; |
| 1291 | if (gadget->a_hnp_support) |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1292 | debug("HNP available\n"); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1293 | else if (gadget->a_alt_hnp_support) |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1294 | debug("HNP needs a different root port\n"); |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1295 | value = eth_set_config(dev, wValue, GFP_ATOMIC); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1296 | break; |
| 1297 | case USB_REQ_GET_CONFIGURATION: |
| 1298 | if (ctrl->bRequestType != USB_DIR_IN) |
| 1299 | break; |
| 1300 | *(u8 *)req->buf = dev->config; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1301 | value = min(wLength, (u16) 1); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1302 | break; |
| 1303 | |
| 1304 | case USB_REQ_SET_INTERFACE: |
| 1305 | if (ctrl->bRequestType != USB_RECIP_INTERFACE |
| 1306 | || !dev->config |
| 1307 | || wIndex > 1) |
| 1308 | break; |
| 1309 | if (!cdc_active(dev) && wIndex != 0) |
| 1310 | break; |
| 1311 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1312 | /* |
| 1313 | * PXA hardware partially handles SET_INTERFACE; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1314 | * we need to kluge around that interference. |
| 1315 | */ |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1316 | if (gadget_is_pxa(gadget)) { |
| 1317 | value = eth_set_config(dev, DEV_CONFIG_VALUE, |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1318 | GFP_ATOMIC); |
Lukasz Dalek | 563aed2 | 2012-10-02 17:04:29 +0200 | [diff] [blame] | 1319 | /* |
| 1320 | * PXA25x driver use non-CDC ethernet gadget. |
| 1321 | * But only _CDC and _RNDIS code can signalize |
| 1322 | * that network is working. So we signalize it |
| 1323 | * here. |
| 1324 | */ |
| 1325 | l_ethdev.network_started = 1; |
| 1326 | debug("USB network up!\n"); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1327 | goto done_set_intf; |
| 1328 | } |
| 1329 | |
Lukasz Dalek | 2bb3788 | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 1330 | #ifdef CONFIG_USB_ETH_CDC |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1331 | switch (wIndex) { |
| 1332 | case 0: /* control/master intf */ |
| 1333 | if (wValue != 0) |
| 1334 | break; |
| 1335 | if (dev->status) { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1336 | usb_ep_disable(dev->status_ep); |
| 1337 | usb_ep_enable(dev->status_ep, dev->status); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1338 | } |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1339 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1340 | value = 0; |
| 1341 | break; |
| 1342 | case 1: /* data intf */ |
| 1343 | if (wValue > 1) |
| 1344 | break; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1345 | usb_ep_disable(dev->in_ep); |
| 1346 | usb_ep_disable(dev->out_ep); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1347 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1348 | /* |
| 1349 | * CDC requires the data transfers not be done from |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1350 | * the default interface setting ... also, setting |
| 1351 | * the non-default interface resets filters etc. |
| 1352 | */ |
| 1353 | if (wValue == 1) { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1354 | if (!cdc_active(dev)) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1355 | break; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1356 | usb_ep_enable(dev->in_ep, dev->in); |
| 1357 | usb_ep_enable(dev->out_ep, dev->out); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1358 | dev->cdc_filter = DEFAULT_FILTER; |
| 1359 | if (dev->status) |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1360 | issue_start_status(dev); |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1361 | eth_start(dev, GFP_ATOMIC); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1362 | } |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1363 | value = 0; |
| 1364 | break; |
| 1365 | } |
| 1366 | #else |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1367 | /* |
| 1368 | * FIXME this is wrong, as is the assumption that |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1369 | * all non-PXA hardware talks real CDC ... |
| 1370 | */ |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1371 | debug("set_interface ignored!\n"); |
Lukasz Dalek | 2bb3788 | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 1372 | #endif /* CONFIG_USB_ETH_CDC */ |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1373 | |
| 1374 | done_set_intf: |
| 1375 | break; |
| 1376 | case USB_REQ_GET_INTERFACE: |
| 1377 | if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE) |
| 1378 | || !dev->config |
| 1379 | || wIndex > 1) |
| 1380 | break; |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1381 | if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1382 | break; |
| 1383 | |
| 1384 | /* for CDC, iff carrier is on, data interface is active. */ |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1385 | if (rndis_active(dev) || wIndex != 1) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1386 | *(u8 *)req->buf = 0; |
| 1387 | else { |
| 1388 | /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */ |
| 1389 | /* carrier always ok ...*/ |
| 1390 | *(u8 *)req->buf = 1 ; |
| 1391 | } |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1392 | value = min(wLength, (u16) 1); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1393 | break; |
| 1394 | |
Lukasz Dalek | 2bb3788 | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 1395 | #ifdef CONFIG_USB_ETH_CDC |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1396 | case USB_CDC_SET_ETHERNET_PACKET_FILTER: |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1397 | /* |
| 1398 | * see 6.2.30: no data, wIndex = interface, |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1399 | * wValue = packet filter bitmap |
| 1400 | */ |
| 1401 | if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE) |
| 1402 | || !cdc_active(dev) |
| 1403 | || wLength != 0 |
| 1404 | || wIndex > 1) |
| 1405 | break; |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1406 | debug("packet filter %02x\n", wValue); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1407 | dev->cdc_filter = wValue; |
| 1408 | value = 0; |
| 1409 | break; |
| 1410 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1411 | /* |
| 1412 | * and potentially: |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1413 | * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS: |
| 1414 | * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER: |
| 1415 | * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER: |
| 1416 | * case USB_CDC_GET_ETHERNET_STATISTIC: |
| 1417 | */ |
| 1418 | |
Lukasz Dalek | 2bb3788 | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 1419 | #endif /* CONFIG_USB_ETH_CDC */ |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1420 | |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1421 | #ifdef CONFIG_USB_ETH_RNDIS |
| 1422 | /* |
| 1423 | * RNDIS uses the CDC command encapsulation mechanism to implement |
| 1424 | * an RPC scheme, with much getting/setting of attributes by OID. |
| 1425 | */ |
| 1426 | case USB_CDC_SEND_ENCAPSULATED_COMMAND: |
| 1427 | if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE) |
| 1428 | || !rndis_active(dev) |
| 1429 | || wLength > USB_BUFSIZ |
| 1430 | || wValue |
| 1431 | || rndis_control_intf.bInterfaceNumber |
| 1432 | != wIndex) |
| 1433 | break; |
| 1434 | /* read the request, then process it */ |
| 1435 | value = wLength; |
| 1436 | req->complete = rndis_command_complete; |
| 1437 | /* later, rndis_control_ack () sends a notification */ |
| 1438 | break; |
| 1439 | |
| 1440 | case USB_CDC_GET_ENCAPSULATED_RESPONSE: |
| 1441 | if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE) |
| 1442 | == ctrl->bRequestType |
| 1443 | && rndis_active(dev) |
| 1444 | /* && wLength >= 0x0400 */ |
| 1445 | && !wValue |
| 1446 | && rndis_control_intf.bInterfaceNumber |
| 1447 | == wIndex) { |
| 1448 | u8 *buf; |
| 1449 | u32 n; |
| 1450 | |
| 1451 | /* return the result */ |
| 1452 | buf = rndis_get_next_response(dev->rndis_config, &n); |
| 1453 | if (buf) { |
| 1454 | memcpy(req->buf, buf, n); |
| 1455 | req->complete = rndis_response_complete; |
| 1456 | rndis_free_response(dev->rndis_config, buf); |
| 1457 | value = n; |
| 1458 | } |
| 1459 | /* else stalls ... spec says to avoid that */ |
| 1460 | } |
| 1461 | break; |
| 1462 | #endif /* RNDIS */ |
| 1463 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1464 | default: |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1465 | debug("unknown control req%02x.%02x v%04x i%04x l%d\n", |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1466 | ctrl->bRequestType, ctrl->bRequest, |
| 1467 | wValue, wIndex, wLength); |
| 1468 | } |
| 1469 | |
| 1470 | /* respond with data transfer before status phase? */ |
| 1471 | if (value >= 0) { |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1472 | debug("respond with data transfer before status phase\n"); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1473 | req->length = value; |
| 1474 | req->zero = value < wLength |
| 1475 | && (value % gadget->ep0->maxpacket) == 0; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1476 | value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1477 | if (value < 0) { |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1478 | debug("ep_queue --> %d\n", value); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1479 | req->status = 0; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1480 | eth_setup_complete(gadget->ep0, req); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1481 | } |
| 1482 | } |
| 1483 | |
| 1484 | /* host either stalls (value < 0) or reports success */ |
| 1485 | return value; |
| 1486 | } |
| 1487 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1488 | /*-------------------------------------------------------------------------*/ |
| 1489 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1490 | static void rx_complete(struct usb_ep *ep, struct usb_request *req); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1491 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1492 | static int rx_submit(struct eth_dev *dev, struct usb_request *req, |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1493 | gfp_t gfp_flags) |
| 1494 | { |
| 1495 | int retval = -ENOMEM; |
| 1496 | size_t size; |
| 1497 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1498 | /* |
| 1499 | * Padding up to RX_EXTRA handles minor disagreements with host. |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1500 | * Normally we use the USB "terminate on short read" convention; |
| 1501 | * so allow up to (N*maxpacket), since that memory is normally |
| 1502 | * already allocated. Some hardware doesn't deal well with short |
| 1503 | * reads (e.g. DMA must be N*maxpacket), so for now don't trim a |
| 1504 | * byte off the end (to force hardware errors on overflow). |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1505 | * |
| 1506 | * RNDIS uses internal framing, and explicitly allows senders to |
| 1507 | * pad to end-of-packet. That's potentially nice for speed, |
| 1508 | * but means receivers can't recover synch on their own. |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1509 | */ |
| 1510 | |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1511 | debug("%s\n", __func__); |
Troy Kisky | 71fc5f9 | 2013-09-25 18:41:05 -0700 | [diff] [blame] | 1512 | if (!req) |
| 1513 | return -EINVAL; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1514 | |
| 1515 | size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA); |
| 1516 | size += dev->out_ep->maxpacket - 1; |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1517 | if (rndis_active(dev)) |
| 1518 | size += sizeof(struct rndis_packet_msg_type); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1519 | size -= size % dev->out_ep->maxpacket; |
| 1520 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1521 | /* |
| 1522 | * Some platforms perform better when IP packets are aligned, |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1523 | * but on at least one, checksumming fails otherwise. Note: |
| 1524 | * RNDIS headers involve variable numbers of LE32 values. |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1525 | */ |
| 1526 | |
Joe Hershberger | 1fd92db | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 1527 | req->buf = (u8 *)net_rx_packets[0]; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1528 | req->length = size; |
| 1529 | req->complete = rx_complete; |
| 1530 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1531 | retval = usb_ep_queue(dev->out_ep, req, gfp_flags); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1532 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1533 | if (retval) |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1534 | error("rx submit --> %d", retval); |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1535 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1536 | return retval; |
| 1537 | } |
| 1538 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1539 | static void rx_complete(struct usb_ep *ep, struct usb_request *req) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1540 | { |
| 1541 | struct eth_dev *dev = ep->driver_data; |
| 1542 | |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1543 | debug("%s: status %d\n", __func__, req->status); |
Vitaly Kuzmichev | c85d70e | 2011-02-11 18:18:32 +0300 | [diff] [blame] | 1544 | switch (req->status) { |
| 1545 | /* normal completion */ |
| 1546 | case 0: |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1547 | if (rndis_active(dev)) { |
| 1548 | /* we know MaxPacketsPerTransfer == 1 here */ |
| 1549 | int length = rndis_rm_hdr(req->buf, req->actual); |
| 1550 | if (length < 0) |
| 1551 | goto length_err; |
| 1552 | req->length -= length; |
| 1553 | req->actual -= length; |
| 1554 | } |
| 1555 | if (req->actual < ETH_HLEN || ETH_FRAME_LEN < req->actual) { |
| 1556 | length_err: |
| 1557 | dev->stats.rx_errors++; |
| 1558 | dev->stats.rx_length_errors++; |
| 1559 | debug("rx length %d\n", req->length); |
| 1560 | break; |
| 1561 | } |
| 1562 | |
Vitaly Kuzmichev | c85d70e | 2011-02-11 18:18:32 +0300 | [diff] [blame] | 1563 | dev->stats.rx_packets++; |
| 1564 | dev->stats.rx_bytes += req->length; |
| 1565 | break; |
| 1566 | |
| 1567 | /* software-driven interface shutdown */ |
| 1568 | case -ECONNRESET: /* unlink */ |
| 1569 | case -ESHUTDOWN: /* disconnect etc */ |
| 1570 | /* for hardware automagic (such as pxa) */ |
| 1571 | case -ECONNABORTED: /* endpoint reset */ |
| 1572 | break; |
| 1573 | |
| 1574 | /* data overrun */ |
| 1575 | case -EOVERFLOW: |
| 1576 | dev->stats.rx_over_errors++; |
| 1577 | /* FALLTHROUGH */ |
| 1578 | default: |
| 1579 | dev->stats.rx_errors++; |
| 1580 | break; |
| 1581 | } |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1582 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1583 | packet_received = 1; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1584 | } |
| 1585 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1586 | static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1587 | { |
| 1588 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1589 | dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1590 | |
| 1591 | if (!dev->tx_req) |
Vitaly Kuzmichev | ac5d32d | 2010-09-22 13:13:55 +0400 | [diff] [blame] | 1592 | goto fail1; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1593 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1594 | dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1595 | |
| 1596 | if (!dev->rx_req) |
Vitaly Kuzmichev | ac5d32d | 2010-09-22 13:13:55 +0400 | [diff] [blame] | 1597 | goto fail2; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1598 | |
| 1599 | return 0; |
| 1600 | |
Vitaly Kuzmichev | ac5d32d | 2010-09-22 13:13:55 +0400 | [diff] [blame] | 1601 | fail2: |
| 1602 | usb_ep_free_request(dev->in_ep, dev->tx_req); |
| 1603 | fail1: |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1604 | error("can't alloc requests"); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1605 | return -1; |
| 1606 | } |
| 1607 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1608 | static void tx_complete(struct usb_ep *ep, struct usb_request *req) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1609 | { |
Vitaly Kuzmichev | c85d70e | 2011-02-11 18:18:32 +0300 | [diff] [blame] | 1610 | struct eth_dev *dev = ep->driver_data; |
| 1611 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1612 | debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok"); |
Vitaly Kuzmichev | c85d70e | 2011-02-11 18:18:32 +0300 | [diff] [blame] | 1613 | switch (req->status) { |
| 1614 | default: |
| 1615 | dev->stats.tx_errors++; |
| 1616 | debug("tx err %d\n", req->status); |
| 1617 | /* FALLTHROUGH */ |
| 1618 | case -ECONNRESET: /* unlink */ |
| 1619 | case -ESHUTDOWN: /* disconnect etc */ |
| 1620 | break; |
| 1621 | case 0: |
| 1622 | dev->stats.tx_bytes += req->length; |
| 1623 | } |
| 1624 | dev->stats.tx_packets++; |
| 1625 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1626 | packet_sent = 1; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1627 | } |
| 1628 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1629 | static inline int eth_is_promisc(struct eth_dev *dev) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1630 | { |
| 1631 | /* no filters for the CDC subset; always promisc */ |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1632 | if (subset_active(dev)) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1633 | return 1; |
| 1634 | return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS; |
| 1635 | } |
| 1636 | |
| 1637 | #if 0 |
| 1638 | static int eth_start_xmit (struct sk_buff *skb, struct net_device *net) |
| 1639 | { |
| 1640 | struct eth_dev *dev = netdev_priv(net); |
| 1641 | int length = skb->len; |
| 1642 | int retval; |
| 1643 | struct usb_request *req = NULL; |
| 1644 | unsigned long flags; |
| 1645 | |
| 1646 | /* apply outgoing CDC or RNDIS filters */ |
| 1647 | if (!eth_is_promisc (dev)) { |
| 1648 | u8 *dest = skb->data; |
| 1649 | |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 1650 | if (is_multicast_ethaddr(dest)) { |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1651 | u16 type; |
| 1652 | |
| 1653 | /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host |
| 1654 | * SET_ETHERNET_MULTICAST_FILTERS requests |
| 1655 | */ |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 1656 | if (is_broadcast_ethaddr(dest)) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1657 | type = USB_CDC_PACKET_TYPE_BROADCAST; |
| 1658 | else |
| 1659 | type = USB_CDC_PACKET_TYPE_ALL_MULTICAST; |
| 1660 | if (!(dev->cdc_filter & type)) { |
| 1661 | dev_kfree_skb_any (skb); |
| 1662 | return 0; |
| 1663 | } |
| 1664 | } |
| 1665 | /* ignores USB_CDC_PACKET_TYPE_DIRECTED */ |
| 1666 | } |
| 1667 | |
| 1668 | spin_lock_irqsave(&dev->req_lock, flags); |
| 1669 | /* |
| 1670 | * this freelist can be empty if an interrupt triggered disconnect() |
| 1671 | * and reconfigured the gadget (shutting down this queue) after the |
| 1672 | * network stack decided to xmit but before we got the spinlock. |
| 1673 | */ |
| 1674 | if (list_empty(&dev->tx_reqs)) { |
| 1675 | spin_unlock_irqrestore(&dev->req_lock, flags); |
| 1676 | return 1; |
| 1677 | } |
| 1678 | |
| 1679 | req = container_of (dev->tx_reqs.next, struct usb_request, list); |
| 1680 | list_del (&req->list); |
| 1681 | |
| 1682 | /* temporarily stop TX queue when the freelist empties */ |
| 1683 | if (list_empty (&dev->tx_reqs)) |
| 1684 | netif_stop_queue (net); |
| 1685 | spin_unlock_irqrestore(&dev->req_lock, flags); |
| 1686 | |
| 1687 | /* no buffer copies needed, unless the network stack did it |
| 1688 | * or the hardware can't use skb buffers. |
| 1689 | * or there's not enough space for any RNDIS headers we need |
| 1690 | */ |
| 1691 | if (rndis_active(dev)) { |
| 1692 | struct sk_buff *skb_rndis; |
| 1693 | |
| 1694 | skb_rndis = skb_realloc_headroom (skb, |
| 1695 | sizeof (struct rndis_packet_msg_type)); |
| 1696 | if (!skb_rndis) |
| 1697 | goto drop; |
| 1698 | |
| 1699 | dev_kfree_skb_any (skb); |
| 1700 | skb = skb_rndis; |
| 1701 | rndis_add_hdr (skb); |
| 1702 | length = skb->len; |
| 1703 | } |
| 1704 | req->buf = skb->data; |
| 1705 | req->context = skb; |
| 1706 | req->complete = tx_complete; |
| 1707 | |
| 1708 | /* use zlp framing on tx for strict CDC-Ether conformance, |
| 1709 | * though any robust network rx path ignores extra padding. |
| 1710 | * and some hardware doesn't like to write zlps. |
| 1711 | */ |
| 1712 | req->zero = 1; |
| 1713 | if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0) |
| 1714 | length++; |
| 1715 | |
| 1716 | req->length = length; |
| 1717 | |
| 1718 | /* throttle highspeed IRQ rate back slightly */ |
| 1719 | if (gadget_is_dualspeed(dev->gadget)) |
| 1720 | req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH) |
| 1721 | ? ((atomic_read(&dev->tx_qlen) % qmult) != 0) |
| 1722 | : 0; |
| 1723 | |
| 1724 | retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC); |
| 1725 | switch (retval) { |
| 1726 | default: |
| 1727 | DEBUG (dev, "tx queue err %d\n", retval); |
| 1728 | break; |
| 1729 | case 0: |
| 1730 | net->trans_start = jiffies; |
| 1731 | atomic_inc (&dev->tx_qlen); |
| 1732 | } |
| 1733 | |
| 1734 | if (retval) { |
| 1735 | drop: |
| 1736 | dev->stats.tx_dropped++; |
| 1737 | dev_kfree_skb_any (skb); |
| 1738 | spin_lock_irqsave(&dev->req_lock, flags); |
| 1739 | if (list_empty (&dev->tx_reqs)) |
| 1740 | netif_start_queue (net); |
| 1741 | list_add (&req->list, &dev->tx_reqs); |
| 1742 | spin_unlock_irqrestore(&dev->req_lock, flags); |
| 1743 | } |
| 1744 | return 0; |
| 1745 | } |
| 1746 | |
| 1747 | /*-------------------------------------------------------------------------*/ |
| 1748 | #endif |
| 1749 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1750 | static void eth_unbind(struct usb_gadget *gadget) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1751 | { |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1752 | struct eth_dev *dev = get_gadget_data(gadget); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1753 | |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1754 | debug("%s...\n", __func__); |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1755 | rndis_deregister(dev->rndis_config); |
| 1756 | rndis_exit(); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1757 | |
Vitaly Kuzmichev | 0129e32 | 2010-08-13 17:00:16 +0400 | [diff] [blame] | 1758 | /* we've already been disconnected ... no i/o is active */ |
| 1759 | if (dev->req) { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1760 | usb_ep_free_request(gadget->ep0, dev->req); |
Vitaly Kuzmichev | 0129e32 | 2010-08-13 17:00:16 +0400 | [diff] [blame] | 1761 | dev->req = NULL; |
| 1762 | } |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1763 | if (dev->stat_req) { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1764 | usb_ep_free_request(dev->status_ep, dev->stat_req); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1765 | dev->stat_req = NULL; |
| 1766 | } |
| 1767 | |
| 1768 | if (dev->tx_req) { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1769 | usb_ep_free_request(dev->in_ep, dev->tx_req); |
| 1770 | dev->tx_req = NULL; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1771 | } |
| 1772 | |
| 1773 | if (dev->rx_req) { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1774 | usb_ep_free_request(dev->out_ep, dev->rx_req); |
| 1775 | dev->rx_req = NULL; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1776 | } |
| 1777 | |
| 1778 | /* unregister_netdev (dev->net);*/ |
| 1779 | /* free_netdev(dev->net);*/ |
| 1780 | |
Lei Wen | 988ee3e | 2010-12-01 23:43:43 +0800 | [diff] [blame] | 1781 | dev->gadget = NULL; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1782 | set_gadget_data(gadget, NULL); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1783 | } |
| 1784 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1785 | static void eth_disconnect(struct usb_gadget *gadget) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1786 | { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1787 | eth_reset_config(get_gadget_data(gadget)); |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1788 | /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */ |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1789 | } |
| 1790 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1791 | static void eth_suspend(struct usb_gadget *gadget) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1792 | { |
| 1793 | /* Not used */ |
| 1794 | } |
| 1795 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1796 | static void eth_resume(struct usb_gadget *gadget) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1797 | { |
| 1798 | /* Not used */ |
| 1799 | } |
| 1800 | |
| 1801 | /*-------------------------------------------------------------------------*/ |
| 1802 | |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1803 | #ifdef CONFIG_USB_ETH_RNDIS |
| 1804 | |
| 1805 | /* |
| 1806 | * The interrupt endpoint is used in RNDIS to notify the host when messages |
| 1807 | * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT |
| 1808 | * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even |
| 1809 | * REMOTE_NDIS_KEEPALIVE_MSG. |
| 1810 | * |
| 1811 | * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and |
| 1812 | * normally just one notification will be queued. |
| 1813 | */ |
| 1814 | |
| 1815 | static void rndis_control_ack_complete(struct usb_ep *ep, |
| 1816 | struct usb_request *req) |
| 1817 | { |
| 1818 | struct eth_dev *dev = ep->driver_data; |
| 1819 | |
| 1820 | debug("%s...\n", __func__); |
| 1821 | if (req->status || req->actual != req->length) |
| 1822 | debug("rndis control ack complete --> %d, %d/%d\n", |
| 1823 | req->status, req->actual, req->length); |
| 1824 | |
| 1825 | if (!l_ethdev.network_started) { |
| 1826 | if (rndis_get_state(dev->rndis_config) |
| 1827 | == RNDIS_DATA_INITIALIZED) { |
| 1828 | l_ethdev.network_started = 1; |
| 1829 | printf("USB RNDIS network up!\n"); |
| 1830 | } |
| 1831 | } |
| 1832 | |
| 1833 | req->context = NULL; |
| 1834 | |
| 1835 | if (req != dev->stat_req) |
| 1836 | usb_ep_free_request(ep, req); |
| 1837 | } |
| 1838 | |
| 1839 | static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32)))); |
| 1840 | |
| 1841 | static int rndis_control_ack(struct eth_device *net) |
| 1842 | { |
| 1843 | struct eth_dev *dev = &l_ethdev; |
| 1844 | int length; |
| 1845 | struct usb_request *resp = dev->stat_req; |
| 1846 | |
| 1847 | /* in case RNDIS calls this after disconnect */ |
| 1848 | if (!dev->status) { |
| 1849 | debug("status ENODEV\n"); |
| 1850 | return -ENODEV; |
| 1851 | } |
| 1852 | |
| 1853 | /* in case queue length > 1 */ |
| 1854 | if (resp->context) { |
| 1855 | resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC); |
| 1856 | if (!resp) |
| 1857 | return -ENOMEM; |
| 1858 | resp->buf = rndis_resp_buf; |
| 1859 | } |
| 1860 | |
| 1861 | /* |
| 1862 | * Send RNDIS RESPONSE_AVAILABLE notification; |
| 1863 | * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too |
| 1864 | */ |
| 1865 | resp->length = 8; |
| 1866 | resp->complete = rndis_control_ack_complete; |
| 1867 | resp->context = dev; |
| 1868 | |
| 1869 | *((__le32 *) resp->buf) = __constant_cpu_to_le32(1); |
| 1870 | *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0); |
| 1871 | |
| 1872 | length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC); |
| 1873 | if (length < 0) { |
| 1874 | resp->status = 0; |
| 1875 | rndis_control_ack_complete(dev->status_ep, resp); |
| 1876 | } |
| 1877 | |
| 1878 | return 0; |
| 1879 | } |
| 1880 | |
| 1881 | #else |
| 1882 | |
| 1883 | #define rndis_control_ack NULL |
| 1884 | |
| 1885 | #endif /* RNDIS */ |
| 1886 | |
| 1887 | static void eth_start(struct eth_dev *dev, gfp_t gfp_flags) |
| 1888 | { |
| 1889 | if (rndis_active(dev)) { |
| 1890 | rndis_set_param_medium(dev->rndis_config, |
| 1891 | NDIS_MEDIUM_802_3, |
| 1892 | BITRATE(dev->gadget)/100); |
| 1893 | rndis_signal_connect(dev->rndis_config); |
| 1894 | } |
| 1895 | } |
| 1896 | |
| 1897 | static int eth_stop(struct eth_dev *dev) |
| 1898 | { |
Vitaly Kuzmichev | e4ae666 | 2011-02-11 18:18:35 +0300 | [diff] [blame] | 1899 | #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT |
| 1900 | unsigned long ts; |
| 1901 | unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */ |
| 1902 | #endif |
| 1903 | |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1904 | if (rndis_active(dev)) { |
| 1905 | rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0); |
| 1906 | rndis_signal_disconnect(dev->rndis_config); |
| 1907 | |
Vitaly Kuzmichev | e4ae666 | 2011-02-11 18:18:35 +0300 | [diff] [blame] | 1908 | #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT |
| 1909 | /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */ |
| 1910 | ts = get_timer(0); |
| 1911 | while (get_timer(ts) < timeout) |
Kishon Vijay Abraham I | 2d48aa6 | 2015-02-23 18:40:23 +0530 | [diff] [blame] | 1912 | usb_gadget_handle_interrupts(0); |
Vitaly Kuzmichev | e4ae666 | 2011-02-11 18:18:35 +0300 | [diff] [blame] | 1913 | #endif |
| 1914 | |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1915 | rndis_uninit(dev->rndis_config); |
| 1916 | dev->rndis = 0; |
| 1917 | } |
| 1918 | |
| 1919 | return 0; |
| 1920 | } |
| 1921 | |
| 1922 | /*-------------------------------------------------------------------------*/ |
| 1923 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1924 | static int is_eth_addr_valid(char *str) |
| 1925 | { |
| 1926 | if (strlen(str) == 17) { |
| 1927 | int i; |
| 1928 | char *p, *q; |
| 1929 | uchar ea[6]; |
| 1930 | |
| 1931 | /* see if it looks like an ethernet address */ |
| 1932 | |
| 1933 | p = str; |
| 1934 | |
| 1935 | for (i = 0; i < 6; i++) { |
| 1936 | char term = (i == 5 ? '\0' : ':'); |
| 1937 | |
| 1938 | ea[i] = simple_strtol(p, &q, 16); |
| 1939 | |
| 1940 | if ((q - p) != 2 || *q++ != term) |
| 1941 | break; |
| 1942 | |
| 1943 | p = q; |
| 1944 | } |
| 1945 | |
Tom Rini | 57a87a2 | 2012-10-31 13:30:41 +0000 | [diff] [blame] | 1946 | /* Now check the contents. */ |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 1947 | return is_valid_ethaddr(ea); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1948 | } |
| 1949 | return 0; |
| 1950 | } |
| 1951 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1952 | static u8 nibble(unsigned char c) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1953 | { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1954 | if (likely(isdigit(c))) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1955 | return c - '0'; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1956 | c = toupper(c); |
| 1957 | if (likely(isxdigit(c))) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1958 | return 10 + c - 'A'; |
| 1959 | return 0; |
| 1960 | } |
| 1961 | |
| 1962 | static int get_ether_addr(const char *str, u8 *dev_addr) |
| 1963 | { |
| 1964 | if (str) { |
| 1965 | unsigned i; |
| 1966 | |
| 1967 | for (i = 0; i < 6; i++) { |
| 1968 | unsigned char num; |
| 1969 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1970 | if ((*str == '.') || (*str == ':')) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1971 | str++; |
| 1972 | num = nibble(*str++) << 4; |
| 1973 | num |= (nibble(*str++)); |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1974 | dev_addr[i] = num; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1975 | } |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 1976 | if (is_valid_ethaddr(dev_addr)) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1977 | return 0; |
| 1978 | } |
| 1979 | return 1; |
| 1980 | } |
| 1981 | |
| 1982 | static int eth_bind(struct usb_gadget *gadget) |
| 1983 | { |
| 1984 | struct eth_dev *dev = &l_ethdev; |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1985 | u8 cdc = 1, zlp = 1, rndis = 1; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1986 | struct usb_ep *in_ep, *out_ep, *status_ep = NULL; |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1987 | int status = -ENOMEM; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1988 | int gcnum; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1989 | u8 tmp[7]; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1990 | |
| 1991 | /* these flags are only ever cleared; compiler take note */ |
Lukasz Dalek | 2bb3788 | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 1992 | #ifndef CONFIG_USB_ETH_CDC |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1993 | cdc = 0; |
| 1994 | #endif |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1995 | #ifndef CONFIG_USB_ETH_RNDIS |
| 1996 | rndis = 0; |
| 1997 | #endif |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1998 | /* |
| 1999 | * Because most host side USB stacks handle CDC Ethernet, that |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2000 | * standard protocol is _strongly_ preferred for interop purposes. |
| 2001 | * (By everyone except Microsoft.) |
| 2002 | */ |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2003 | if (gadget_is_pxa(gadget)) { |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2004 | /* pxa doesn't support altsettings */ |
| 2005 | cdc = 0; |
| 2006 | } else if (gadget_is_musbhdrc(gadget)) { |
| 2007 | /* reduce tx dma overhead by avoiding special cases */ |
| 2008 | zlp = 0; |
| 2009 | } else if (gadget_is_sh(gadget)) { |
| 2010 | /* sh doesn't support multiple interfaces or configs */ |
| 2011 | cdc = 0; |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2012 | rndis = 0; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2013 | } else if (gadget_is_sa1100(gadget)) { |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2014 | /* hardware can't write zlps */ |
| 2015 | zlp = 0; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2016 | /* |
| 2017 | * sa1100 CAN do CDC, without status endpoint ... we use |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2018 | * non-CDC to be compatible with ARM Linux-2.4 "usb-eth". |
| 2019 | */ |
| 2020 | cdc = 0; |
| 2021 | } |
| 2022 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2023 | gcnum = usb_gadget_controller_number(gadget); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2024 | if (gcnum >= 0) |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2025 | device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2026 | else { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2027 | /* |
| 2028 | * can't assume CDC works. don't want to default to |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2029 | * anything less functional on CDC-capable hardware, |
| 2030 | * so we fail in this case. |
| 2031 | */ |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 2032 | error("controller '%s' not recognized", |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2033 | gadget->name); |
| 2034 | return -ENODEV; |
| 2035 | } |
| 2036 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2037 | /* |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2038 | * If there's an RNDIS configuration, that's what Windows wants to |
| 2039 | * be using ... so use these product IDs here and in the "linux.inf" |
| 2040 | * needed to install MSFT drivers. Current Linux kernels will use |
| 2041 | * the second configuration if it's CDC Ethernet, and need some help |
| 2042 | * to choose the right configuration otherwise. |
| 2043 | */ |
| 2044 | if (rndis) { |
| 2045 | #if defined(CONFIG_USB_RNDIS_VENDOR_ID) && defined(CONFIG_USB_RNDIS_PRODUCT_ID) |
| 2046 | device_desc.idVendor = |
| 2047 | __constant_cpu_to_le16(CONFIG_USB_RNDIS_VENDOR_ID); |
| 2048 | device_desc.idProduct = |
| 2049 | __constant_cpu_to_le16(CONFIG_USB_RNDIS_PRODUCT_ID); |
| 2050 | #else |
| 2051 | device_desc.idVendor = |
| 2052 | __constant_cpu_to_le16(RNDIS_VENDOR_NUM); |
| 2053 | device_desc.idProduct = |
| 2054 | __constant_cpu_to_le16(RNDIS_PRODUCT_NUM); |
| 2055 | #endif |
| 2056 | sprintf(product_desc, "RNDIS/%s", driver_desc); |
| 2057 | |
| 2058 | /* |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2059 | * CDC subset ... recognized by Linux since 2.4.10, but Windows |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2060 | * drivers aren't widely available. (That may be improved by |
| 2061 | * supporting one submode of the "SAFE" variant of MDLM.) |
| 2062 | */ |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2063 | } else { |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2064 | #if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID) |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2065 | device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID); |
| 2066 | device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID); |
| 2067 | #else |
| 2068 | if (!cdc) { |
| 2069 | device_desc.idVendor = |
| 2070 | __constant_cpu_to_le16(SIMPLE_VENDOR_NUM); |
| 2071 | device_desc.idProduct = |
| 2072 | __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM); |
| 2073 | } |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2074 | #endif |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2075 | } |
| 2076 | /* support optional vendor/distro customization */ |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2077 | if (bcdDevice) |
| 2078 | device_desc.bcdDevice = cpu_to_le16(bcdDevice); |
| 2079 | if (iManufacturer) |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2080 | strlcpy(manufacturer, iManufacturer, sizeof manufacturer); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2081 | if (iProduct) |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2082 | strlcpy(product_desc, iProduct, sizeof product_desc); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2083 | if (iSerialNumber) { |
| 2084 | device_desc.iSerialNumber = STRING_SERIALNUMBER, |
Vitaly Kuzmichev | 2e12abe | 2010-08-13 17:00:45 +0400 | [diff] [blame] | 2085 | strlcpy(serial_number, iSerialNumber, sizeof serial_number); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2086 | } |
| 2087 | |
| 2088 | /* all we really need is bulk IN/OUT */ |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2089 | usb_ep_autoconfig_reset(gadget); |
| 2090 | in_ep = usb_ep_autoconfig(gadget, &fs_source_desc); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2091 | if (!in_ep) { |
| 2092 | autoconf_fail: |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 2093 | error("can't autoconfigure on %s\n", |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2094 | gadget->name); |
| 2095 | return -ENODEV; |
| 2096 | } |
| 2097 | in_ep->driver_data = in_ep; /* claim */ |
| 2098 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2099 | out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2100 | if (!out_ep) |
| 2101 | goto autoconf_fail; |
| 2102 | out_ep->driver_data = out_ep; /* claim */ |
| 2103 | |
Lukasz Dalek | 2bb3788 | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 2104 | #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS) |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2105 | /* |
| 2106 | * CDC Ethernet control interface doesn't require a status endpoint. |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2107 | * Since some hosts expect one, try to allocate one anyway. |
| 2108 | */ |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2109 | if (cdc || rndis) { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2110 | status_ep = usb_ep_autoconfig(gadget, &fs_status_desc); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2111 | if (status_ep) { |
| 2112 | status_ep->driver_data = status_ep; /* claim */ |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2113 | } else if (rndis) { |
| 2114 | error("can't run RNDIS on %s", gadget->name); |
| 2115 | return -ENODEV; |
Lukasz Dalek | 2bb3788 | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 2116 | #ifdef CONFIG_USB_ETH_CDC |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2117 | } else if (cdc) { |
| 2118 | control_intf.bNumEndpoints = 0; |
| 2119 | /* FIXME remove endpoint from descriptor list */ |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2120 | #endif |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2121 | } |
| 2122 | } |
| 2123 | #endif |
| 2124 | |
| 2125 | /* one config: cdc, else minimal subset */ |
| 2126 | if (!cdc) { |
| 2127 | eth_config.bNumInterfaces = 1; |
| 2128 | eth_config.iConfiguration = STRING_SUBSET; |
| 2129 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2130 | /* |
| 2131 | * use functions to set these up, in case we're built to work |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2132 | * with multiple controllers and must override CDC Ethernet. |
| 2133 | */ |
| 2134 | fs_subset_descriptors(); |
| 2135 | hs_subset_descriptors(); |
| 2136 | } |
| 2137 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2138 | usb_gadget_set_selfpowered(gadget); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2139 | |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2140 | /* For now RNDIS is always a second config */ |
| 2141 | if (rndis) |
| 2142 | device_desc.bNumConfigurations = 2; |
| 2143 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2144 | if (gadget_is_dualspeed(gadget)) { |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2145 | if (rndis) |
| 2146 | dev_qualifier.bNumConfigurations = 2; |
| 2147 | else if (!cdc) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2148 | dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC; |
| 2149 | |
| 2150 | /* assumes ep0 uses the same value for both speeds ... */ |
| 2151 | dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0; |
| 2152 | |
| 2153 | /* and that all endpoints are dual-speed */ |
| 2154 | hs_source_desc.bEndpointAddress = |
| 2155 | fs_source_desc.bEndpointAddress; |
| 2156 | hs_sink_desc.bEndpointAddress = |
| 2157 | fs_sink_desc.bEndpointAddress; |
Lukasz Dalek | 2bb3788 | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 2158 | #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2159 | if (status_ep) |
| 2160 | hs_status_desc.bEndpointAddress = |
| 2161 | fs_status_desc.bEndpointAddress; |
| 2162 | #endif |
| 2163 | } |
| 2164 | |
| 2165 | if (gadget_is_otg(gadget)) { |
| 2166 | otg_descriptor.bmAttributes |= USB_OTG_HNP, |
| 2167 | eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
| 2168 | eth_config.bMaxPower = 4; |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2169 | #ifdef CONFIG_USB_ETH_RNDIS |
| 2170 | rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
| 2171 | rndis_config.bMaxPower = 4; |
| 2172 | #endif |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2173 | } |
| 2174 | |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2175 | |
| 2176 | /* network device setup */ |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2177 | dev->net = &l_netdev; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2178 | |
| 2179 | dev->cdc = cdc; |
| 2180 | dev->zlp = zlp; |
| 2181 | |
| 2182 | dev->in_ep = in_ep; |
| 2183 | dev->out_ep = out_ep; |
| 2184 | dev->status_ep = status_ep; |
| 2185 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2186 | /* |
| 2187 | * Module params for these addresses should come from ID proms. |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2188 | * The host side address is used with CDC and RNDIS, and commonly |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2189 | * ends up in a persistent config database. It's not clear if |
| 2190 | * host side code for the SAFE thing cares -- its original BLAN |
| 2191 | * thing didn't, Sharp never assigned those addresses on Zaurii. |
| 2192 | */ |
| 2193 | get_ether_addr(dev_addr, dev->net->enetaddr); |
| 2194 | |
| 2195 | memset(tmp, 0, sizeof(tmp)); |
| 2196 | memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr)); |
| 2197 | |
| 2198 | get_ether_addr(host_addr, dev->host_mac); |
| 2199 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2200 | sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X", |
| 2201 | dev->host_mac[0], dev->host_mac[1], |
| 2202 | dev->host_mac[2], dev->host_mac[3], |
| 2203 | dev->host_mac[4], dev->host_mac[5]); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2204 | |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2205 | if (rndis) { |
| 2206 | status = rndis_init(); |
| 2207 | if (status < 0) { |
| 2208 | error("can't init RNDIS, %d", status); |
| 2209 | goto fail; |
| 2210 | } |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2211 | } |
| 2212 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2213 | /* |
| 2214 | * use PKTSIZE (or aligned... from u-boot) and set |
| 2215 | * wMaxSegmentSize accordingly |
| 2216 | */ |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2217 | dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/ |
| 2218 | |
| 2219 | /* preallocate control message data and buffer */ |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2220 | dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2221 | if (!dev->req) |
| 2222 | goto fail; |
| 2223 | dev->req->buf = control_req; |
| 2224 | dev->req->complete = eth_setup_complete; |
| 2225 | |
| 2226 | /* ... and maybe likewise for status transfer */ |
Lukasz Dalek | 2bb3788 | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 2227 | #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2228 | if (dev->status_ep) { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2229 | dev->stat_req = usb_ep_alloc_request(dev->status_ep, |
| 2230 | GFP_KERNEL); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2231 | if (!dev->stat_req) { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2232 | usb_ep_free_request(dev->status_ep, dev->req); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2233 | |
| 2234 | goto fail; |
| 2235 | } |
Vitaly Kuzmichev | df559c1 | 2010-08-13 17:01:06 +0400 | [diff] [blame] | 2236 | dev->stat_req->buf = status_req; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2237 | dev->stat_req->context = NULL; |
| 2238 | } |
| 2239 | #endif |
| 2240 | |
| 2241 | /* finish hookup to lower layer ... */ |
| 2242 | dev->gadget = gadget; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2243 | set_gadget_data(gadget, dev); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2244 | gadget->ep0->driver_data = dev; |
| 2245 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2246 | /* |
| 2247 | * two kinds of host-initiated state changes: |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2248 | * - iff DATA transfer is active, carrier is "on" |
| 2249 | * - tx queueing enabled if open *and* carrier is "on" |
| 2250 | */ |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2251 | |
| 2252 | printf("using %s, OUT %s IN %s%s%s\n", gadget->name, |
| 2253 | out_ep->name, in_ep->name, |
| 2254 | status_ep ? " STATUS " : "", |
| 2255 | status_ep ? status_ep->name : "" |
| 2256 | ); |
| 2257 | printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n", |
| 2258 | dev->net->enetaddr[0], dev->net->enetaddr[1], |
| 2259 | dev->net->enetaddr[2], dev->net->enetaddr[3], |
| 2260 | dev->net->enetaddr[4], dev->net->enetaddr[5]); |
| 2261 | |
| 2262 | if (cdc || rndis) |
| 2263 | printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n", |
| 2264 | dev->host_mac[0], dev->host_mac[1], |
| 2265 | dev->host_mac[2], dev->host_mac[3], |
| 2266 | dev->host_mac[4], dev->host_mac[5]); |
| 2267 | |
| 2268 | if (rndis) { |
| 2269 | u32 vendorID = 0; |
| 2270 | |
| 2271 | /* FIXME RNDIS vendor id == "vendor NIC code" == ? */ |
| 2272 | |
| 2273 | dev->rndis_config = rndis_register(rndis_control_ack); |
| 2274 | if (dev->rndis_config < 0) { |
| 2275 | fail0: |
| 2276 | eth_unbind(gadget); |
| 2277 | debug("RNDIS setup failed\n"); |
| 2278 | status = -ENODEV; |
| 2279 | goto fail; |
| 2280 | } |
| 2281 | |
| 2282 | /* these set up a lot of the OIDs that RNDIS needs */ |
| 2283 | rndis_set_host_mac(dev->rndis_config, dev->host_mac); |
| 2284 | if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu, |
| 2285 | &dev->stats, &dev->cdc_filter)) |
| 2286 | goto fail0; |
| 2287 | if (rndis_set_param_vendor(dev->rndis_config, vendorID, |
| 2288 | manufacturer)) |
| 2289 | goto fail0; |
| 2290 | if (rndis_set_param_medium(dev->rndis_config, |
| 2291 | NDIS_MEDIUM_802_3, 0)) |
| 2292 | goto fail0; |
| 2293 | printf("RNDIS ready\n"); |
| 2294 | } |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2295 | return 0; |
| 2296 | |
| 2297 | fail: |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2298 | error("%s failed, status = %d", __func__, status); |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2299 | eth_unbind(gadget); |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2300 | return status; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2301 | } |
| 2302 | |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2303 | /*-------------------------------------------------------------------------*/ |
| 2304 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2305 | static int usb_eth_init(struct eth_device *netdev, bd_t *bd) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2306 | { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2307 | struct eth_dev *dev = &l_ethdev; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2308 | struct usb_gadget *gadget; |
| 2309 | unsigned long ts; |
| 2310 | unsigned long timeout = USB_CONNECT_TIMEOUT; |
| 2311 | |
| 2312 | if (!netdev) { |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 2313 | error("received NULL ptr"); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2314 | goto fail; |
| 2315 | } |
Vitaly Kuzmichev | 58939fc | 2010-12-28 16:59:32 +0300 | [diff] [blame] | 2316 | |
Kishon Vijay Abraham I | 8bfc288 | 2015-08-19 13:49:46 +0530 | [diff] [blame] | 2317 | board_usb_init(0, USB_INIT_DEVICE); |
| 2318 | |
Vitaly Kuzmichev | 58939fc | 2010-12-28 16:59:32 +0300 | [diff] [blame] | 2319 | /* Configure default mac-addresses for the USB ethernet device */ |
| 2320 | #ifdef CONFIG_USBNET_DEV_ADDR |
| 2321 | strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr)); |
| 2322 | #endif |
| 2323 | #ifdef CONFIG_USBNET_HOST_ADDR |
| 2324 | strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr)); |
| 2325 | #endif |
| 2326 | /* Check if the user overruled the MAC addresses */ |
| 2327 | if (getenv("usbnet_devaddr")) |
| 2328 | strlcpy(dev_addr, getenv("usbnet_devaddr"), |
| 2329 | sizeof(dev_addr)); |
| 2330 | |
| 2331 | if (getenv("usbnet_hostaddr")) |
| 2332 | strlcpy(host_addr, getenv("usbnet_hostaddr"), |
| 2333 | sizeof(host_addr)); |
| 2334 | |
| 2335 | if (!is_eth_addr_valid(dev_addr)) { |
| 2336 | error("Need valid 'usbnet_devaddr' to be set"); |
| 2337 | goto fail; |
| 2338 | } |
| 2339 | if (!is_eth_addr_valid(host_addr)) { |
| 2340 | error("Need valid 'usbnet_hostaddr' to be set"); |
| 2341 | goto fail; |
| 2342 | } |
| 2343 | |
Lei Wen | 988ee3e | 2010-12-01 23:43:43 +0800 | [diff] [blame] | 2344 | if (usb_gadget_register_driver(ð_driver) < 0) |
| 2345 | goto fail; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2346 | |
| 2347 | dev->network_started = 0; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2348 | |
| 2349 | packet_received = 0; |
| 2350 | packet_sent = 0; |
| 2351 | |
| 2352 | gadget = dev->gadget; |
| 2353 | usb_gadget_connect(gadget); |
| 2354 | |
| 2355 | if (getenv("cdc_connect_timeout")) |
| 2356 | timeout = simple_strtoul(getenv("cdc_connect_timeout"), |
| 2357 | NULL, 10) * CONFIG_SYS_HZ; |
| 2358 | ts = get_timer(0); |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2359 | while (!l_ethdev.network_started) { |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2360 | /* Handle control-c and timeouts */ |
| 2361 | if (ctrlc() || (get_timer(ts) > timeout)) { |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 2362 | error("The remote end did not respond in time."); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2363 | goto fail; |
| 2364 | } |
Kishon Vijay Abraham I | 2d48aa6 | 2015-02-23 18:40:23 +0530 | [diff] [blame] | 2365 | usb_gadget_handle_interrupts(0); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2366 | } |
| 2367 | |
Vitaly Kuzmichev | 98fae97 | 2010-09-22 13:13:56 +0400 | [diff] [blame] | 2368 | packet_received = 0; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2369 | rx_submit(dev, dev->rx_req, 0); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2370 | return 0; |
| 2371 | fail: |
| 2372 | return -1; |
| 2373 | } |
| 2374 | |
Joe Hershberger | 10cbe3b | 2012-05-22 18:36:19 +0000 | [diff] [blame] | 2375 | static int usb_eth_send(struct eth_device *netdev, void *packet, int length) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2376 | { |
| 2377 | int retval; |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2378 | void *rndis_pkt = NULL; |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 2379 | struct eth_dev *dev = &l_ethdev; |
Vitaly Kuzmichev | ac5d32d | 2010-09-22 13:13:55 +0400 | [diff] [blame] | 2380 | struct usb_request *req = dev->tx_req; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2381 | unsigned long ts; |
| 2382 | unsigned long timeout = USB_CONNECT_TIMEOUT; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2383 | |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 2384 | debug("%s:...\n", __func__); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2385 | |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2386 | /* new buffer is needed to include RNDIS header */ |
| 2387 | if (rndis_active(dev)) { |
| 2388 | rndis_pkt = malloc(length + |
| 2389 | sizeof(struct rndis_packet_msg_type)); |
| 2390 | if (!rndis_pkt) { |
| 2391 | error("No memory to alloc RNDIS packet"); |
| 2392 | goto drop; |
| 2393 | } |
| 2394 | rndis_add_hdr(rndis_pkt, length); |
| 2395 | memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type), |
Joe Hershberger | 10cbe3b | 2012-05-22 18:36:19 +0000 | [diff] [blame] | 2396 | packet, length); |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2397 | packet = rndis_pkt; |
| 2398 | length += sizeof(struct rndis_packet_msg_type); |
| 2399 | } |
Joe Hershberger | 10cbe3b | 2012-05-22 18:36:19 +0000 | [diff] [blame] | 2400 | req->buf = packet; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2401 | req->context = NULL; |
| 2402 | req->complete = tx_complete; |
| 2403 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2404 | /* |
| 2405 | * use zlp framing on tx for strict CDC-Ether conformance, |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2406 | * though any robust network rx path ignores extra padding. |
| 2407 | * and some hardware doesn't like to write zlps. |
| 2408 | */ |
| 2409 | req->zero = 1; |
| 2410 | if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0) |
| 2411 | length++; |
| 2412 | |
| 2413 | req->length = length; |
| 2414 | #if 0 |
| 2415 | /* throttle highspeed IRQ rate back slightly */ |
| 2416 | if (gadget_is_dualspeed(dev->gadget)) |
| 2417 | req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH) |
| 2418 | ? ((dev->tx_qlen % qmult) != 0) : 0; |
| 2419 | #endif |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2420 | dev->tx_qlen = 1; |
| 2421 | ts = get_timer(0); |
| 2422 | packet_sent = 0; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2423 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2424 | retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2425 | |
| 2426 | if (!retval) |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 2427 | debug("%s: packet queued\n", __func__); |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2428 | while (!packet_sent) { |
| 2429 | if (get_timer(ts) > timeout) { |
| 2430 | printf("timeout sending packets to usb ethernet\n"); |
| 2431 | return -1; |
| 2432 | } |
Kishon Vijay Abraham I | 2d48aa6 | 2015-02-23 18:40:23 +0530 | [diff] [blame] | 2433 | usb_gadget_handle_interrupts(0); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2434 | } |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2435 | if (rndis_pkt) |
| 2436 | free(rndis_pkt); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2437 | |
| 2438 | return 0; |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2439 | drop: |
| 2440 | dev->stats.tx_dropped++; |
| 2441 | return -ENOMEM; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2442 | } |
| 2443 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2444 | static int usb_eth_recv(struct eth_device *netdev) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2445 | { |
| 2446 | struct eth_dev *dev = &l_ethdev; |
| 2447 | |
Kishon Vijay Abraham I | 2d48aa6 | 2015-02-23 18:40:23 +0530 | [diff] [blame] | 2448 | usb_gadget_handle_interrupts(0); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2449 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2450 | if (packet_received) { |
| 2451 | debug("%s: packet received\n", __func__); |
| 2452 | if (dev->rx_req) { |
Joe Hershberger | 1fd92db | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 2453 | net_process_received_packet(net_rx_packets[0], |
| 2454 | dev->rx_req->length); |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2455 | packet_received = 0; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2456 | |
Vitaly Kuzmichev | ac5d32d | 2010-09-22 13:13:55 +0400 | [diff] [blame] | 2457 | rx_submit(dev, dev->rx_req, 0); |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2458 | } else |
| 2459 | error("dev->rx_req invalid"); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2460 | } |
| 2461 | return 0; |
| 2462 | } |
| 2463 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2464 | void usb_eth_halt(struct eth_device *netdev) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2465 | { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2466 | struct eth_dev *dev = &l_ethdev; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2467 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2468 | if (!netdev) { |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 2469 | error("received NULL ptr"); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2470 | return; |
| 2471 | } |
| 2472 | |
Lei Wen | 988ee3e | 2010-12-01 23:43:43 +0800 | [diff] [blame] | 2473 | /* If the gadget not registered, simple return */ |
| 2474 | if (!dev->gadget) |
| 2475 | return; |
| 2476 | |
Vitaly Kuzmichev | e4ae666 | 2011-02-11 18:18:35 +0300 | [diff] [blame] | 2477 | /* |
| 2478 | * Some USB controllers may need additional deinitialization here |
| 2479 | * before dropping pull-up (also due to hardware issues). |
| 2480 | * For example: unhandled interrupt with status stage started may |
| 2481 | * bring the controller to fully broken state (until board reset). |
| 2482 | * There are some variants to debug and fix such cases: |
| 2483 | * 1) In the case of RNDIS connection eth_stop can perform additional |
| 2484 | * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition. |
| 2485 | * 2) 'pullup' callback in your UDC driver can be improved to perform |
| 2486 | * this deinitialization. |
| 2487 | */ |
Vitaly Kuzmichev | 7612a43 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2488 | eth_stop(dev); |
| 2489 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2490 | usb_gadget_disconnect(dev->gadget); |
Vitaly Kuzmichev | b3649f3 | 2011-02-11 18:18:31 +0300 | [diff] [blame] | 2491 | |
| 2492 | /* Clear pending interrupt */ |
| 2493 | if (dev->network_started) { |
Kishon Vijay Abraham I | 2d48aa6 | 2015-02-23 18:40:23 +0530 | [diff] [blame] | 2494 | usb_gadget_handle_interrupts(0); |
Vitaly Kuzmichev | b3649f3 | 2011-02-11 18:18:31 +0300 | [diff] [blame] | 2495 | dev->network_started = 0; |
| 2496 | } |
| 2497 | |
Lei Wen | 988ee3e | 2010-12-01 23:43:43 +0800 | [diff] [blame] | 2498 | usb_gadget_unregister_driver(ð_driver); |
Kishon Vijay Abraham I | 8bfc288 | 2015-08-19 13:49:46 +0530 | [diff] [blame] | 2499 | board_usb_cleanup(0, USB_INIT_DEVICE); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2500 | } |
| 2501 | |
| 2502 | static struct usb_gadget_driver eth_driver = { |
| 2503 | .speed = DEVSPEED, |
| 2504 | |
| 2505 | .bind = eth_bind, |
| 2506 | .unbind = eth_unbind, |
| 2507 | |
| 2508 | .setup = eth_setup, |
Kishon Vijay Abraham I | 5df1315 | 2015-08-19 13:49:48 +0530 | [diff] [blame] | 2509 | .reset = eth_disconnect, |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2510 | .disconnect = eth_disconnect, |
| 2511 | |
| 2512 | .suspend = eth_suspend, |
| 2513 | .resume = eth_resume, |
| 2514 | }; |
| 2515 | |
| 2516 | int usb_eth_initialize(bd_t *bi) |
| 2517 | { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2518 | struct eth_device *netdev = &l_netdev; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2519 | |
Vitaly Kuzmichev | 8f7aa83 | 2010-12-28 16:59:31 +0300 | [diff] [blame] | 2520 | strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name)); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2521 | |
| 2522 | netdev->init = usb_eth_init; |
| 2523 | netdev->send = usb_eth_send; |
| 2524 | netdev->recv = usb_eth_recv; |
| 2525 | netdev->halt = usb_eth_halt; |
| 2526 | |
| 2527 | #ifdef CONFIG_MCAST_TFTP |
| 2528 | #error not supported |
| 2529 | #endif |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2530 | eth_register(netdev); |
| 2531 | return 0; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2532 | } |