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