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