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