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> |
| 25 | #include <linux/usb/ch9.h> |
| 26 | #include <linux/usb/cdc.h> |
| 27 | #include <linux/usb/gadget.h> |
| 28 | #include <net.h> |
| 29 | #include <linux/ctype.h> |
| 30 | |
| 31 | #include "gadget_chips.h" |
| 32 | |
| 33 | #define USB_NET_NAME "usb0" |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 34 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 35 | #define atomic_read |
| 36 | extern struct platform_data brd; |
| 37 | #define spin_lock(x) |
| 38 | #define spin_unlock(x) |
| 39 | |
| 40 | |
| 41 | unsigned packet_received, packet_sent; |
| 42 | |
| 43 | #define DEV_CONFIG_CDC 1 |
| 44 | #define GFP_ATOMIC ((gfp_t) 0) |
| 45 | #define GFP_KERNEL ((gfp_t) 0) |
| 46 | |
| 47 | /* |
| 48 | * Ethernet gadget driver -- with CDC and non-CDC options |
| 49 | * Builds on hardware support for a full duplex link. |
| 50 | * |
| 51 | * CDC Ethernet is the standard USB solution for sending Ethernet frames |
| 52 | * using USB. Real hardware tends to use the same framing protocol but look |
| 53 | * different for control features. This driver strongly prefers to use |
| 54 | * this USB-IF standard as its open-systems interoperability solution; |
| 55 | * most host side USB stacks (except from Microsoft) support it. |
| 56 | * |
| 57 | * This is sometimes called "CDC ECM" (Ethernet Control Model) to support |
| 58 | * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new |
| 59 | * "CDC EEM" (Ethernet Emulation Model) is starting to spread. |
| 60 | * |
| 61 | * There's some hardware that can't talk CDC ECM. We make that hardware |
| 62 | * implement a "minimalist" vendor-agnostic CDC core: same framing, but |
| 63 | * link-level setup only requires activating the configuration. Only the |
| 64 | * endpoint descriptors, and product/vendor IDs, are relevant; no control |
| 65 | * operations are available. Linux supports it, but other host operating |
| 66 | * systems may not. (This is a subset of CDC Ethernet.) |
| 67 | * |
| 68 | * It turns out that if you add a few descriptors to that "CDC Subset", |
| 69 | * (Windows) host side drivers from MCCI can treat it as one submode of |
| 70 | * a proprietary scheme called "SAFE" ... without needing to know about |
| 71 | * specific product/vendor IDs. So we do that, making it easier to use |
| 72 | * those MS-Windows drivers. Those added descriptors make it resemble a |
| 73 | * CDC MDLM device, but they don't change device behavior at all. (See |
| 74 | * MCCI Engineering report 950198 "SAFE Networking Functions".) |
| 75 | * |
| 76 | * A third option is also in use. Rather than CDC Ethernet, or something |
| 77 | * simpler, Microsoft pushes their own approach: RNDIS. The published |
| 78 | * RNDIS specs are ambiguous and appear to be incomplete, and are also |
| 79 | * needlessly complex. They borrow more from CDC ACM than CDC ECM. |
| 80 | */ |
| 81 | #define ETH_ALEN 6 /* Octets in one ethernet addr */ |
| 82 | #define ETH_HLEN 14 /* Total octets in header. */ |
| 83 | #define ETH_ZLEN 60 /* Min. octets in frame sans FCS */ |
| 84 | #define ETH_DATA_LEN 1500 /* Max. octets in payload */ |
| 85 | #define ETH_FRAME_LEN PKTSIZE_ALIGN /* Max. octets in frame sans FCS */ |
| 86 | #define ETH_FCS_LEN 4 /* Octets in the FCS */ |
| 87 | |
| 88 | #define DRIVER_DESC "Ethernet Gadget" |
| 89 | /* Based on linux 2.6.27 version */ |
| 90 | #define DRIVER_VERSION "May Day 2005" |
| 91 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 92 | static const char shortname[] = "ether"; |
| 93 | static const char driver_desc[] = DRIVER_DESC; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 94 | |
| 95 | #define RX_EXTRA 20 /* guard against rx overflows */ |
| 96 | |
| 97 | /* CDC support the same host-chosen outgoing packet filters. */ |
| 98 | #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \ |
| 99 | |USB_CDC_PACKET_TYPE_ALL_MULTICAST \ |
| 100 | |USB_CDC_PACKET_TYPE_PROMISCUOUS \ |
| 101 | |USB_CDC_PACKET_TYPE_DIRECTED) |
| 102 | |
| 103 | #define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ) |
| 104 | |
| 105 | /*-------------------------------------------------------------------------*/ |
| 106 | static struct eth_dev l_ethdev; |
| 107 | static struct eth_device l_netdev; |
| 108 | static struct usb_gadget_driver eth_driver; |
| 109 | |
| 110 | /*-------------------------------------------------------------------------*/ |
| 111 | |
| 112 | /* "main" config is either CDC, or its simple subset */ |
| 113 | static inline int is_cdc(struct eth_dev *dev) |
| 114 | { |
| 115 | #if !defined(DEV_CONFIG_SUBSET) |
| 116 | return 1; /* only cdc possible */ |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 117 | #elif !defined(DEV_CONFIG_CDC) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 118 | return 0; /* only subset possible */ |
| 119 | #else |
| 120 | return dev->cdc; /* depends on what hardware we found */ |
| 121 | #endif |
| 122 | } |
| 123 | |
| 124 | #define subset_active(dev) (!is_cdc(dev)) |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 125 | #define cdc_active(dev) (is_cdc(dev)) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 126 | |
| 127 | #define DEFAULT_QLEN 2 /* double buffering by default */ |
| 128 | |
| 129 | /* peak bulk transfer bits-per-second */ |
| 130 | #define HS_BPS (13 * 512 * 8 * 1000 * 8) |
| 131 | #define FS_BPS (19 * 64 * 1 * 1000 * 8) |
| 132 | |
| 133 | #ifdef CONFIG_USB_GADGET_DUALSPEED |
| 134 | #define DEVSPEED USB_SPEED_HIGH |
| 135 | |
Vitaly Kuzmichev | 2721dbf | 2010-08-12 16:44:40 +0400 | [diff] [blame] | 136 | #ifdef CONFIG_USB_ETH_QMULT |
| 137 | #define qmult CONFIG_USB_ETH_QMULT |
| 138 | #else |
| 139 | #define qmult 5 |
| 140 | #endif |
| 141 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 142 | /* for dual-speed hardware, use deeper queues at highspeed */ |
| 143 | #define qlen(gadget) \ |
| 144 | (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1)) |
| 145 | |
| 146 | static inline int BITRATE(struct usb_gadget *g) |
| 147 | { |
| 148 | return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS; |
| 149 | } |
| 150 | |
| 151 | #else /* full speed (low speed doesn't do bulk) */ |
| 152 | |
| 153 | #define qmult 1 |
| 154 | |
| 155 | #define DEVSPEED USB_SPEED_FULL |
| 156 | |
| 157 | #define qlen(gadget) DEFAULT_QLEN |
| 158 | |
| 159 | static inline int BITRATE(struct usb_gadget *g) |
| 160 | { |
| 161 | return FS_BPS; |
| 162 | } |
| 163 | #endif |
| 164 | |
| 165 | struct eth_dev { |
| 166 | struct usb_gadget *gadget; |
| 167 | struct usb_request *req; /* for control responses */ |
| 168 | struct usb_request *stat_req; /* for cdc status */ |
| 169 | |
| 170 | u8 config; |
| 171 | struct usb_ep *in_ep, *out_ep, *status_ep; |
| 172 | const struct usb_endpoint_descriptor |
| 173 | *in, *out, *status; |
| 174 | |
| 175 | struct usb_request *tx_req, *rx_req; |
| 176 | |
| 177 | struct eth_device *net; |
| 178 | unsigned int tx_qlen; |
| 179 | |
| 180 | unsigned zlp:1; |
| 181 | unsigned cdc:1; |
| 182 | unsigned suspended:1; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 183 | unsigned network_started:1; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 184 | u16 cdc_filter; |
| 185 | unsigned long todo; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 186 | int mtu; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 187 | #define WORK_RX_MEMORY 0 |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 188 | u8 host_mac[ETH_ALEN]; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 189 | }; |
| 190 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 191 | /* |
| 192 | * This version autoconfigures as much as possible at run-time. |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 193 | * |
| 194 | * It also ASSUMES a self-powered device, without remote wakeup, |
| 195 | * although remote wakeup support would make sense. |
| 196 | */ |
| 197 | |
| 198 | /*-------------------------------------------------------------------------*/ |
| 199 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 200 | /* |
| 201 | * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 202 | * Instead: allocate your own, using normal USB-IF procedures. |
| 203 | */ |
| 204 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 205 | /* |
| 206 | * Thanks to NetChip Technologies for donating this product ID. |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 207 | * It's for devices with only CDC Ethernet configurations. |
| 208 | */ |
| 209 | #define CDC_VENDOR_NUM 0x0525 /* NetChip */ |
| 210 | #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */ |
| 211 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 212 | /* |
| 213 | * 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] | 214 | * ARM Linux has used for ethernet-over-usb, both with sa1100 and |
| 215 | * with pxa250. We're protocol-compatible, if the host-side drivers |
| 216 | * use the endpoint descriptors. bcdDevice (version) is nonzero, so |
| 217 | * drivers that need to hard-wire endpoint numbers have a hook. |
| 218 | * |
| 219 | * The protocol is a minimal subset of CDC Ether, which works on any bulk |
| 220 | * hardware that's not deeply broken ... even on hardware that can't talk |
| 221 | * RNDIS (like SA-1100, with no interrupt endpoint, or anything that |
| 222 | * doesn't handle control-OUT). |
| 223 | */ |
| 224 | #define SIMPLE_VENDOR_NUM 0x049f |
| 225 | #define SIMPLE_PRODUCT_NUM 0x505a |
| 226 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 227 | /* |
| 228 | * Some systems will want different product identifers published in the |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 229 | * device descriptor, either numbers or strings or both. These string |
| 230 | * parameters are in UTF-8 (superset of ASCII's 7 bit characters). |
| 231 | */ |
| 232 | |
| 233 | static ushort bcdDevice; |
| 234 | #if defined(CONFIG_USBNET_MANUFACTURER) |
| 235 | static char *iManufacturer = CONFIG_USBNET_MANUFACTURER; |
| 236 | #else |
| 237 | static char *iManufacturer = "U-boot"; |
| 238 | #endif |
| 239 | static char *iProduct; |
| 240 | static char *iSerialNumber; |
| 241 | static char dev_addr[18]; |
| 242 | static char host_addr[18]; |
| 243 | |
| 244 | /*-------------------------------------------------------------------------*/ |
| 245 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 246 | /* |
| 247 | * USB DRIVER HOOKUP (to the hardware driver, below us), mostly |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 248 | * ep0 implementation: descriptors, config management, setup(). |
| 249 | * also optional class-specific notification interrupt transfer. |
| 250 | */ |
| 251 | |
| 252 | /* |
| 253 | * DESCRIPTORS ... most are static, but strings and (full) configuration |
| 254 | * descriptors are built on demand. For now we do either full CDC, or |
| 255 | * our simple subset. |
| 256 | */ |
| 257 | |
| 258 | #define STRING_MANUFACTURER 1 |
| 259 | #define STRING_PRODUCT 2 |
| 260 | #define STRING_ETHADDR 3 |
| 261 | #define STRING_DATA 4 |
| 262 | #define STRING_CONTROL 5 |
| 263 | #define STRING_CDC 7 |
| 264 | #define STRING_SUBSET 8 |
| 265 | #define STRING_SERIALNUMBER 10 |
| 266 | |
| 267 | /* holds our biggest descriptor */ |
| 268 | #define USB_BUFSIZ 256 |
| 269 | |
| 270 | /* |
| 271 | * This device advertises one configuration, eth_config, |
| 272 | * on hardware supporting at least two configs. |
| 273 | * |
| 274 | * FIXME define some higher-powered configurations to make it easier |
| 275 | * to recharge batteries ... |
| 276 | */ |
| 277 | |
| 278 | #define DEV_CONFIG_VALUE 1 /* cdc or subset */ |
| 279 | |
| 280 | static struct usb_device_descriptor |
| 281 | device_desc = { |
| 282 | .bLength = sizeof device_desc, |
| 283 | .bDescriptorType = USB_DT_DEVICE, |
| 284 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 285 | .bcdUSB = __constant_cpu_to_le16(0x0200), |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 286 | |
| 287 | .bDeviceClass = USB_CLASS_COMM, |
| 288 | .bDeviceSubClass = 0, |
| 289 | .bDeviceProtocol = 0, |
| 290 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 291 | .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM), |
| 292 | .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM), |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 293 | .iManufacturer = STRING_MANUFACTURER, |
| 294 | .iProduct = STRING_PRODUCT, |
| 295 | .bNumConfigurations = 1, |
| 296 | }; |
| 297 | |
| 298 | static struct usb_otg_descriptor |
| 299 | otg_descriptor = { |
| 300 | .bLength = sizeof otg_descriptor, |
| 301 | .bDescriptorType = USB_DT_OTG, |
| 302 | |
| 303 | .bmAttributes = USB_OTG_SRP, |
| 304 | }; |
| 305 | |
| 306 | static struct usb_config_descriptor |
| 307 | eth_config = { |
| 308 | .bLength = sizeof eth_config, |
| 309 | .bDescriptorType = USB_DT_CONFIG, |
| 310 | |
| 311 | /* compute wTotalLength on the fly */ |
| 312 | .bNumInterfaces = 2, |
| 313 | .bConfigurationValue = DEV_CONFIG_VALUE, |
| 314 | .iConfiguration = STRING_CDC, |
| 315 | .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, |
| 316 | .bMaxPower = 1, |
| 317 | }; |
| 318 | |
| 319 | /* |
| 320 | * Compared to the simple CDC subset, the full CDC Ethernet model adds |
| 321 | * three class descriptors, two interface descriptors, optional status |
| 322 | * endpoint. Both have a "data" interface and two bulk endpoints. |
| 323 | * There are also differences in how control requests are handled. |
| 324 | */ |
| 325 | |
| 326 | #ifdef DEV_CONFIG_CDC |
| 327 | static struct usb_interface_descriptor |
| 328 | control_intf = { |
| 329 | .bLength = sizeof control_intf, |
| 330 | .bDescriptorType = USB_DT_INTERFACE, |
| 331 | |
| 332 | .bInterfaceNumber = 0, |
| 333 | /* status endpoint is optional; this may be patched later */ |
| 334 | .bNumEndpoints = 1, |
| 335 | .bInterfaceClass = USB_CLASS_COMM, |
| 336 | .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, |
| 337 | .bInterfaceProtocol = USB_CDC_PROTO_NONE, |
| 338 | .iInterface = STRING_CONTROL, |
| 339 | }; |
| 340 | #endif |
| 341 | |
| 342 | static const struct usb_cdc_header_desc header_desc = { |
| 343 | .bLength = sizeof header_desc, |
| 344 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 345 | .bDescriptorSubType = USB_CDC_HEADER_TYPE, |
| 346 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 347 | .bcdCDC = __constant_cpu_to_le16(0x0110), |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 348 | }; |
| 349 | |
| 350 | #if defined(DEV_CONFIG_CDC) |
| 351 | |
| 352 | static const struct usb_cdc_union_desc union_desc = { |
| 353 | .bLength = sizeof union_desc, |
| 354 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 355 | .bDescriptorSubType = USB_CDC_UNION_TYPE, |
| 356 | |
| 357 | .bMasterInterface0 = 0, /* index of control interface */ |
| 358 | .bSlaveInterface0 = 1, /* index of DATA interface */ |
| 359 | }; |
| 360 | |
| 361 | #endif /* CDC */ |
| 362 | |
| 363 | #ifndef DEV_CONFIG_CDC |
| 364 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 365 | /* |
| 366 | * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 367 | * ways: data endpoints live in the control interface, there's no data |
| 368 | * interface, and it's not used to talk to a cell phone radio. |
| 369 | */ |
| 370 | |
| 371 | static const struct usb_cdc_mdlm_desc mdlm_desc = { |
| 372 | .bLength = sizeof mdlm_desc, |
| 373 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 374 | .bDescriptorSubType = USB_CDC_MDLM_TYPE, |
| 375 | |
| 376 | .bcdVersion = __constant_cpu_to_le16(0x0100), |
| 377 | .bGUID = { |
| 378 | 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6, |
| 379 | 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f, |
| 380 | }, |
| 381 | }; |
| 382 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 383 | /* |
| 384 | * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 385 | * can't really use its struct. All we do here is say that we're using |
| 386 | * the submode of "SAFE" which directly matches the CDC Subset. |
| 387 | */ |
| 388 | static const u8 mdlm_detail_desc[] = { |
| 389 | 6, |
| 390 | USB_DT_CS_INTERFACE, |
| 391 | USB_CDC_MDLM_DETAIL_TYPE, |
| 392 | |
| 393 | 0, /* "SAFE" */ |
| 394 | 0, /* network control capabilities (none) */ |
| 395 | 0, /* network data capabilities ("raw" encapsulation) */ |
| 396 | }; |
| 397 | |
| 398 | #endif |
| 399 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 400 | static const struct usb_cdc_ether_desc ether_desc = { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 401 | .bLength = sizeof(ether_desc), |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 402 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 403 | .bDescriptorSubType = USB_CDC_ETHERNET_TYPE, |
| 404 | |
| 405 | /* this descriptor actually adds value, surprise! */ |
| 406 | .iMACAddress = STRING_ETHADDR, |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 407 | .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */ |
| 408 | .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN), |
| 409 | .wNumberMCFilters = __constant_cpu_to_le16(0), |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 410 | .bNumberPowerFilters = 0, |
| 411 | }; |
| 412 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 413 | #if defined(DEV_CONFIG_CDC) |
| 414 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 415 | /* |
| 416 | * include the status endpoint if we can, even where it's optional. |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 417 | * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one |
| 418 | * packet, to simplify cancellation; and a big transfer interval, to |
| 419 | * waste less bandwidth. |
| 420 | * |
| 421 | * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even |
| 422 | * if they ignore the connect/disconnect notifications that real aether |
| 423 | * can provide. more advanced cdc configurations might want to support |
| 424 | * encapsulated commands (vendor-specific, using control-OUT). |
| 425 | */ |
| 426 | |
| 427 | #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */ |
| 428 | #define STATUS_BYTECOUNT 16 /* 8 byte header + data */ |
| 429 | |
| 430 | static struct usb_endpoint_descriptor |
| 431 | fs_status_desc = { |
| 432 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 433 | .bDescriptorType = USB_DT_ENDPOINT, |
| 434 | |
| 435 | .bEndpointAddress = USB_DIR_IN, |
| 436 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 437 | .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT), |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 438 | .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC, |
| 439 | }; |
| 440 | #endif |
| 441 | |
| 442 | #ifdef DEV_CONFIG_CDC |
| 443 | |
| 444 | /* the default data interface has no endpoints ... */ |
| 445 | |
| 446 | static const struct usb_interface_descriptor |
| 447 | data_nop_intf = { |
| 448 | .bLength = sizeof data_nop_intf, |
| 449 | .bDescriptorType = USB_DT_INTERFACE, |
| 450 | |
| 451 | .bInterfaceNumber = 1, |
| 452 | .bAlternateSetting = 0, |
| 453 | .bNumEndpoints = 0, |
| 454 | .bInterfaceClass = USB_CLASS_CDC_DATA, |
| 455 | .bInterfaceSubClass = 0, |
| 456 | .bInterfaceProtocol = 0, |
| 457 | }; |
| 458 | |
| 459 | /* ... but the "real" data interface has two bulk endpoints */ |
| 460 | |
| 461 | static const struct usb_interface_descriptor |
| 462 | data_intf = { |
| 463 | .bLength = sizeof data_intf, |
| 464 | .bDescriptorType = USB_DT_INTERFACE, |
| 465 | |
| 466 | .bInterfaceNumber = 1, |
| 467 | .bAlternateSetting = 1, |
| 468 | .bNumEndpoints = 2, |
| 469 | .bInterfaceClass = USB_CLASS_CDC_DATA, |
| 470 | .bInterfaceSubClass = 0, |
| 471 | .bInterfaceProtocol = 0, |
| 472 | .iInterface = STRING_DATA, |
| 473 | }; |
| 474 | |
| 475 | #endif |
| 476 | |
| 477 | #ifdef DEV_CONFIG_SUBSET |
| 478 | |
| 479 | /* |
| 480 | * "Simple" CDC-subset option is a simple vendor-neutral model that most |
| 481 | * full speed controllers can handle: one interface, two bulk endpoints. |
| 482 | * |
| 483 | * To assist host side drivers, we fancy it up a bit, and add descriptors |
| 484 | * so some host side drivers will understand it as a "SAFE" variant. |
| 485 | */ |
| 486 | |
| 487 | static const struct usb_interface_descriptor |
| 488 | subset_data_intf = { |
| 489 | .bLength = sizeof subset_data_intf, |
| 490 | .bDescriptorType = USB_DT_INTERFACE, |
| 491 | |
| 492 | .bInterfaceNumber = 0, |
| 493 | .bAlternateSetting = 0, |
| 494 | .bNumEndpoints = 2, |
| 495 | .bInterfaceClass = USB_CLASS_COMM, |
| 496 | .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM, |
| 497 | .bInterfaceProtocol = 0, |
| 498 | .iInterface = STRING_DATA, |
| 499 | }; |
| 500 | |
| 501 | #endif /* SUBSET */ |
| 502 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 503 | static struct usb_endpoint_descriptor |
| 504 | fs_source_desc = { |
| 505 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 506 | .bDescriptorType = USB_DT_ENDPOINT, |
| 507 | |
| 508 | .bEndpointAddress = USB_DIR_IN, |
| 509 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 510 | }; |
| 511 | |
| 512 | static struct usb_endpoint_descriptor |
| 513 | fs_sink_desc = { |
| 514 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 515 | .bDescriptorType = USB_DT_ENDPOINT, |
| 516 | |
| 517 | .bEndpointAddress = USB_DIR_OUT, |
| 518 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 519 | }; |
| 520 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 521 | static const struct usb_descriptor_header *fs_eth_function[11] = { |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 522 | (struct usb_descriptor_header *) &otg_descriptor, |
| 523 | #ifdef DEV_CONFIG_CDC |
| 524 | /* "cdc" mode descriptors */ |
| 525 | (struct usb_descriptor_header *) &control_intf, |
| 526 | (struct usb_descriptor_header *) &header_desc, |
| 527 | (struct usb_descriptor_header *) &union_desc, |
| 528 | (struct usb_descriptor_header *) ðer_desc, |
| 529 | /* NOTE: status endpoint may need to be removed */ |
| 530 | (struct usb_descriptor_header *) &fs_status_desc, |
| 531 | /* data interface, with altsetting */ |
| 532 | (struct usb_descriptor_header *) &data_nop_intf, |
| 533 | (struct usb_descriptor_header *) &data_intf, |
| 534 | (struct usb_descriptor_header *) &fs_source_desc, |
| 535 | (struct usb_descriptor_header *) &fs_sink_desc, |
| 536 | NULL, |
| 537 | #endif /* DEV_CONFIG_CDC */ |
| 538 | }; |
| 539 | |
| 540 | static inline void fs_subset_descriptors(void) |
| 541 | { |
| 542 | #ifdef DEV_CONFIG_SUBSET |
| 543 | /* behavior is "CDC Subset"; extra descriptors say "SAFE" */ |
| 544 | fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf; |
| 545 | fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc; |
| 546 | fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc; |
| 547 | fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc; |
| 548 | fs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc; |
| 549 | fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc; |
| 550 | fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc; |
| 551 | fs_eth_function[8] = NULL; |
| 552 | #else |
| 553 | fs_eth_function[1] = NULL; |
| 554 | #endif |
| 555 | } |
| 556 | |
| 557 | /* |
| 558 | * usb 2.0 devices need to expose both high speed and full speed |
| 559 | * descriptors, unless they only run at full speed. |
| 560 | */ |
| 561 | |
| 562 | #if defined(DEV_CONFIG_CDC) |
| 563 | static struct usb_endpoint_descriptor |
| 564 | hs_status_desc = { |
| 565 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 566 | .bDescriptorType = USB_DT_ENDPOINT, |
| 567 | |
| 568 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 569 | .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT), |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 570 | .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4, |
| 571 | }; |
| 572 | #endif /* DEV_CONFIG_CDC */ |
| 573 | |
| 574 | static struct usb_endpoint_descriptor |
| 575 | hs_source_desc = { |
| 576 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 577 | .bDescriptorType = USB_DT_ENDPOINT, |
| 578 | |
| 579 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 580 | .wMaxPacketSize = __constant_cpu_to_le16(512), |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 581 | }; |
| 582 | |
| 583 | static struct usb_endpoint_descriptor |
| 584 | hs_sink_desc = { |
| 585 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 586 | .bDescriptorType = USB_DT_ENDPOINT, |
| 587 | |
| 588 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 589 | .wMaxPacketSize = __constant_cpu_to_le16(512), |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 590 | }; |
| 591 | |
| 592 | static struct usb_qualifier_descriptor |
| 593 | dev_qualifier = { |
| 594 | .bLength = sizeof dev_qualifier, |
| 595 | .bDescriptorType = USB_DT_DEVICE_QUALIFIER, |
| 596 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 597 | .bcdUSB = __constant_cpu_to_le16(0x0200), |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 598 | .bDeviceClass = USB_CLASS_COMM, |
| 599 | |
| 600 | .bNumConfigurations = 1, |
| 601 | }; |
| 602 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 603 | static const struct usb_descriptor_header *hs_eth_function[11] = { |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 604 | (struct usb_descriptor_header *) &otg_descriptor, |
| 605 | #ifdef DEV_CONFIG_CDC |
| 606 | /* "cdc" mode descriptors */ |
| 607 | (struct usb_descriptor_header *) &control_intf, |
| 608 | (struct usb_descriptor_header *) &header_desc, |
| 609 | (struct usb_descriptor_header *) &union_desc, |
| 610 | (struct usb_descriptor_header *) ðer_desc, |
| 611 | /* NOTE: status endpoint may need to be removed */ |
| 612 | (struct usb_descriptor_header *) &hs_status_desc, |
| 613 | /* data interface, with altsetting */ |
| 614 | (struct usb_descriptor_header *) &data_nop_intf, |
| 615 | (struct usb_descriptor_header *) &data_intf, |
| 616 | (struct usb_descriptor_header *) &hs_source_desc, |
| 617 | (struct usb_descriptor_header *) &hs_sink_desc, |
| 618 | NULL, |
| 619 | #endif /* DEV_CONFIG_CDC */ |
| 620 | }; |
| 621 | |
| 622 | static inline void hs_subset_descriptors(void) |
| 623 | { |
| 624 | #ifdef DEV_CONFIG_SUBSET |
| 625 | /* behavior is "CDC Subset"; extra descriptors say "SAFE" */ |
| 626 | hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf; |
| 627 | hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc; |
| 628 | hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc; |
| 629 | hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc; |
| 630 | hs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc; |
| 631 | hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc; |
| 632 | hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc; |
| 633 | hs_eth_function[8] = NULL; |
| 634 | #else |
| 635 | hs_eth_function[1] = NULL; |
| 636 | #endif |
| 637 | } |
| 638 | |
| 639 | /* maxpacket and other transfer characteristics vary by speed. */ |
| 640 | static inline struct usb_endpoint_descriptor * |
| 641 | ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs, |
| 642 | struct usb_endpoint_descriptor *fs) |
| 643 | { |
| 644 | if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) |
| 645 | return hs; |
| 646 | return fs; |
| 647 | } |
| 648 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 649 | /*-------------------------------------------------------------------------*/ |
| 650 | |
| 651 | /* descriptors that are built on-demand */ |
| 652 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 653 | static char manufacturer[50]; |
| 654 | static char product_desc[40] = DRIVER_DESC; |
| 655 | static char serial_number[20]; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 656 | |
| 657 | /* address that the host will use ... usually assigned at random */ |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 658 | static char ethaddr[2 * ETH_ALEN + 1]; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 659 | |
| 660 | /* static strings, in UTF-8 */ |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 661 | static struct usb_string strings[] = { |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 662 | { STRING_MANUFACTURER, manufacturer, }, |
| 663 | { STRING_PRODUCT, product_desc, }, |
| 664 | { STRING_SERIALNUMBER, serial_number, }, |
| 665 | { STRING_DATA, "Ethernet Data", }, |
| 666 | { STRING_ETHADDR, ethaddr, }, |
| 667 | #ifdef DEV_CONFIG_CDC |
| 668 | { STRING_CDC, "CDC Ethernet", }, |
| 669 | { STRING_CONTROL, "CDC Communications Control", }, |
| 670 | #endif |
| 671 | #ifdef DEV_CONFIG_SUBSET |
| 672 | { STRING_SUBSET, "CDC Ethernet Subset", }, |
| 673 | #endif |
| 674 | { } /* end of list */ |
| 675 | }; |
| 676 | |
| 677 | static struct usb_gadget_strings stringtab = { |
| 678 | .language = 0x0409, /* en-us */ |
| 679 | .strings = strings, |
| 680 | }; |
| 681 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 682 | /*============================================================================*/ |
| 683 | static u8 control_req[USB_BUFSIZ]; |
Stefano Babic | 8046077 | 2010-08-15 14:18:59 +0200 | [diff] [blame] | 684 | static u8 status_req[STATUS_BYTECOUNT] __attribute__ ((aligned(4))); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 685 | |
| 686 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 687 | /** |
| 688 | * strlcpy - Copy a %NUL terminated string into a sized buffer |
| 689 | * @dest: Where to copy the string to |
| 690 | * @src: Where to copy the string from |
| 691 | * @size: size of destination buffer |
| 692 | * |
| 693 | * Compatible with *BSD: the result is always a valid |
| 694 | * NUL-terminated string that fits in the buffer (unless, |
| 695 | * of course, the buffer size is zero). It does not pad |
| 696 | * out the result like strncpy() does. |
| 697 | */ |
| 698 | size_t strlcpy(char *dest, const char *src, size_t size) |
| 699 | { |
| 700 | size_t ret = strlen(src); |
| 701 | |
| 702 | if (size) { |
| 703 | size_t len = (ret >= size) ? size - 1 : ret; |
| 704 | memcpy(dest, src, len); |
| 705 | dest[len] = '\0'; |
| 706 | } |
| 707 | return ret; |
| 708 | } |
| 709 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 710 | /*============================================================================*/ |
| 711 | |
| 712 | /* |
| 713 | * one config, two interfaces: control, data. |
| 714 | * complications: class descriptors, and an altsetting. |
| 715 | */ |
| 716 | static int |
| 717 | config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg) |
| 718 | { |
| 719 | int len; |
| 720 | const struct usb_config_descriptor *config; |
| 721 | const struct usb_descriptor_header **function; |
| 722 | int hs = 0; |
| 723 | |
| 724 | if (gadget_is_dualspeed(g)) { |
| 725 | hs = (g->speed == USB_SPEED_HIGH); |
| 726 | if (type == USB_DT_OTHER_SPEED_CONFIG) |
| 727 | hs = !hs; |
| 728 | } |
| 729 | #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function) |
| 730 | |
| 731 | if (index >= device_desc.bNumConfigurations) |
| 732 | return -EINVAL; |
| 733 | |
| 734 | config = ð_config; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 735 | function = which_fn(eth); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 736 | |
| 737 | /* for now, don't advertise srp-only devices */ |
| 738 | if (!is_otg) |
| 739 | function++; |
| 740 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 741 | len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 742 | if (len < 0) |
| 743 | return len; |
| 744 | ((struct usb_config_descriptor *) buf)->bDescriptorType = type; |
| 745 | return len; |
| 746 | } |
| 747 | |
| 748 | /*-------------------------------------------------------------------------*/ |
| 749 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 750 | 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] | 751 | |
| 752 | static int |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 753 | set_ether_config(struct eth_dev *dev, gfp_t gfp_flags) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 754 | { |
| 755 | int result = 0; |
| 756 | struct usb_gadget *gadget = dev->gadget; |
| 757 | |
| 758 | #if defined(DEV_CONFIG_CDC) |
| 759 | /* status endpoint used for (optionally) CDC */ |
| 760 | if (!subset_active(dev) && dev->status_ep) { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 761 | dev->status = ep_desc(gadget, &hs_status_desc, |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 762 | &fs_status_desc); |
| 763 | dev->status_ep->driver_data = dev; |
| 764 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 765 | result = usb_ep_enable(dev->status_ep, dev->status); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 766 | if (result != 0) { |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 767 | debug("enable %s --> %d\n", |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 768 | dev->status_ep->name, result); |
| 769 | goto done; |
| 770 | } |
| 771 | } |
| 772 | #endif |
| 773 | |
| 774 | dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc); |
| 775 | dev->in_ep->driver_data = dev; |
| 776 | |
| 777 | dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc); |
| 778 | dev->out_ep->driver_data = dev; |
| 779 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 780 | /* |
| 781 | * With CDC, the host isn't allowed to use these two data |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 782 | * endpoints in the default altsetting for the interface. |
| 783 | * so we don't activate them yet. Reset from SET_INTERFACE. |
| 784 | */ |
| 785 | if (!cdc_active(dev)) { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 786 | result = usb_ep_enable(dev->in_ep, dev->in); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 787 | if (result != 0) { |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 788 | debug("enable %s --> %d\n", |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 789 | dev->in_ep->name, result); |
| 790 | goto done; |
| 791 | } |
| 792 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 793 | result = usb_ep_enable(dev->out_ep, dev->out); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 794 | if (result != 0) { |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 795 | debug("enable %s --> %d\n", |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 796 | dev->out_ep->name, result); |
| 797 | goto done; |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | done: |
| 802 | if (result == 0) |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 803 | result = alloc_requests(dev, qlen(gadget), gfp_flags); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 804 | |
| 805 | /* on error, disable any endpoints */ |
| 806 | if (result < 0) { |
Vitaly Kuzmichev | d5292c1 | 2010-08-13 17:02:29 +0400 | [diff] [blame] | 807 | if (!subset_active(dev) && dev->status_ep) |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 808 | (void) usb_ep_disable(dev->status_ep); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 809 | dev->status = NULL; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 810 | (void) usb_ep_disable(dev->in_ep); |
| 811 | (void) usb_ep_disable(dev->out_ep); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 812 | dev->in = NULL; |
| 813 | dev->out = NULL; |
| 814 | } |
| 815 | |
| 816 | /* caller is responsible for cleanup on error */ |
| 817 | return result; |
| 818 | } |
| 819 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 820 | static void eth_reset_config(struct eth_dev *dev) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 821 | { |
| 822 | if (dev->config == 0) |
| 823 | return; |
| 824 | |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 825 | debug("%s\n", __func__); |
| 826 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 827 | /* |
| 828 | * disable endpoints, forcing (synchronous) completion of |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 829 | * pending i/o. then free the requests. |
| 830 | */ |
| 831 | |
| 832 | if (dev->in) { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 833 | usb_ep_disable(dev->in_ep); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 834 | if (dev->tx_req) { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 835 | usb_ep_free_request(dev->in_ep, dev->tx_req); |
| 836 | dev->tx_req = NULL; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 837 | } |
| 838 | } |
| 839 | if (dev->out) { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 840 | usb_ep_disable(dev->out_ep); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 841 | if (dev->rx_req) { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 842 | usb_ep_free_request(dev->out_ep, dev->rx_req); |
| 843 | dev->rx_req = NULL; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 844 | } |
| 845 | } |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 846 | if (dev->status) |
| 847 | usb_ep_disable(dev->status_ep); |
| 848 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 849 | dev->cdc_filter = 0; |
| 850 | dev->config = 0; |
| 851 | } |
| 852 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 853 | /* |
| 854 | * change our operational config. must agree with the code |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 855 | * that returns config descriptors, and altsetting code. |
| 856 | */ |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 857 | static int eth_set_config(struct eth_dev *dev, unsigned number, |
| 858 | gfp_t gfp_flags) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 859 | { |
| 860 | int result = 0; |
| 861 | struct usb_gadget *gadget = dev->gadget; |
| 862 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 863 | if (gadget_is_sa1100(gadget) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 864 | && dev->config |
| 865 | && dev->tx_qlen != 0) { |
| 866 | /* tx fifo is full, but we can't clear it...*/ |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 867 | error("can't change configurations"); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 868 | return -ESPIPE; |
| 869 | } |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 870 | eth_reset_config(dev); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 871 | |
| 872 | switch (number) { |
| 873 | case DEV_CONFIG_VALUE: |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 874 | result = set_ether_config(dev, gfp_flags); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 875 | break; |
| 876 | default: |
| 877 | result = -EINVAL; |
| 878 | /* FALL THROUGH */ |
| 879 | case 0: |
| 880 | break; |
| 881 | } |
| 882 | |
| 883 | if (result) { |
| 884 | if (number) |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 885 | eth_reset_config(dev); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 886 | usb_gadget_vbus_draw(dev->gadget, |
| 887 | gadget_is_otg(dev->gadget) ? 8 : 100); |
| 888 | } else { |
| 889 | char *speed; |
| 890 | unsigned power; |
| 891 | |
| 892 | power = 2 * eth_config.bMaxPower; |
| 893 | usb_gadget_vbus_draw(dev->gadget, power); |
| 894 | |
| 895 | switch (gadget->speed) { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 896 | case USB_SPEED_FULL: |
| 897 | speed = "full"; break; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 898 | #ifdef CONFIG_USB_GADGET_DUALSPEED |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 899 | case USB_SPEED_HIGH: |
| 900 | speed = "high"; break; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 901 | #endif |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 902 | default: |
| 903 | speed = "?"; break; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | dev->config = number; |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 907 | printf("%s speed config #%d: %d mA, %s, using %s\n", |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 908 | speed, number, power, driver_desc, |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 909 | (cdc_active(dev) ? "CDC Ethernet" |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 910 | : "CDC Ethernet Subset")); |
| 911 | } |
| 912 | return result; |
| 913 | } |
| 914 | |
| 915 | /*-------------------------------------------------------------------------*/ |
| 916 | |
| 917 | #ifdef DEV_CONFIG_CDC |
| 918 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 919 | /* |
| 920 | * The interrupt endpoint is used in CDC networking models (Ethernet, ATM) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 921 | * only to notify the host about link status changes (which we support) or |
| 922 | * report completion of some encapsulated command. Since |
| 923 | * we want this CDC Ethernet code to be vendor-neutral, we don't use that |
| 924 | * command mechanism; and only one status request is ever queued. |
| 925 | */ |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 926 | 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] | 927 | { |
| 928 | struct usb_cdc_notification *event = req->buf; |
| 929 | int value = req->status; |
| 930 | struct eth_dev *dev = ep->driver_data; |
| 931 | |
| 932 | /* issue the second notification if host reads the first */ |
| 933 | if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION |
| 934 | && value == 0) { |
| 935 | __le32 *data = req->buf + sizeof *event; |
| 936 | |
| 937 | event->bmRequestType = 0xA1; |
| 938 | event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 939 | event->wValue = __constant_cpu_to_le16(0); |
| 940 | event->wIndex = __constant_cpu_to_le16(1); |
| 941 | event->wLength = __constant_cpu_to_le16(8); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 942 | |
| 943 | /* SPEED_CHANGE data is up/down speeds in bits/sec */ |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 944 | data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget)); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 945 | |
| 946 | req->length = STATUS_BYTECOUNT; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 947 | value = usb_ep_queue(ep, req, GFP_ATOMIC); |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 948 | debug("send SPEED_CHANGE --> %d\n", value); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 949 | if (value == 0) |
| 950 | return; |
| 951 | } else if (value != -ECONNRESET) { |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 952 | debug("event %02x --> %d\n", |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 953 | event->bNotificationType, value); |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 954 | if (event->bNotificationType == |
| 955 | USB_CDC_NOTIFY_SPEED_CHANGE) { |
| 956 | l_ethdev.network_started = 1; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 957 | printf("USB network up!\n"); |
| 958 | } |
| 959 | } |
| 960 | req->context = NULL; |
| 961 | } |
| 962 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 963 | static void issue_start_status(struct eth_dev *dev) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 964 | { |
| 965 | struct usb_request *req = dev->stat_req; |
| 966 | struct usb_cdc_notification *event; |
| 967 | int value; |
| 968 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 969 | /* |
| 970 | * flush old status |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 971 | * |
| 972 | * FIXME ugly idiom, maybe we'd be better with just |
| 973 | * a "cancel the whole queue" primitive since any |
| 974 | * unlink-one primitive has way too many error modes. |
| 975 | * here, we "know" toggle is already clear... |
| 976 | * |
| 977 | * FIXME iff req->context != null just dequeue it |
| 978 | */ |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 979 | usb_ep_disable(dev->status_ep); |
| 980 | usb_ep_enable(dev->status_ep, dev->status); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 981 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 982 | /* |
| 983 | * 3.8.1 says to issue first NETWORK_CONNECTION, then |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 984 | * a SPEED_CHANGE. could be useful in some configs. |
| 985 | */ |
| 986 | event = req->buf; |
| 987 | event->bmRequestType = 0xA1; |
| 988 | event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 989 | event->wValue = __constant_cpu_to_le16(1); /* connected */ |
| 990 | event->wIndex = __constant_cpu_to_le16(1); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 991 | event->wLength = 0; |
| 992 | |
| 993 | req->length = sizeof *event; |
| 994 | req->complete = eth_status_complete; |
| 995 | req->context = dev; |
| 996 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 997 | value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 998 | if (value < 0) |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 999 | debug("status buf queue --> %d\n", value); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | #endif |
| 1003 | |
| 1004 | /*-------------------------------------------------------------------------*/ |
| 1005 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1006 | 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] | 1007 | { |
| 1008 | if (req->status || req->actual != req->length) |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1009 | debug("setup complete --> %d, %d/%d\n", |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1010 | req->status, req->actual, req->length); |
| 1011 | } |
| 1012 | |
| 1013 | /* |
| 1014 | * The setup() callback implements all the ep0 functionality that's not |
| 1015 | * handled lower down. CDC has a number of less-common features: |
| 1016 | * |
| 1017 | * - two interfaces: control, and ethernet data |
| 1018 | * - Ethernet data interface has two altsettings: default, and active |
| 1019 | * - class-specific descriptors for the control interface |
| 1020 | * - class-specific control requests |
| 1021 | */ |
| 1022 | static int |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1023 | eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1024 | { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1025 | struct eth_dev *dev = get_gadget_data(gadget); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1026 | struct usb_request *req = dev->req; |
| 1027 | int value = -EOPNOTSUPP; |
| 1028 | u16 wIndex = le16_to_cpu(ctrl->wIndex); |
| 1029 | u16 wValue = le16_to_cpu(ctrl->wValue); |
| 1030 | u16 wLength = le16_to_cpu(ctrl->wLength); |
| 1031 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1032 | /* |
| 1033 | * descriptors just go into the pre-allocated ep0 buffer, |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1034 | * while config change events may enable network traffic. |
| 1035 | */ |
| 1036 | |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1037 | debug("%s\n", __func__); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1038 | |
| 1039 | req->complete = eth_setup_complete; |
| 1040 | switch (ctrl->bRequest) { |
| 1041 | |
| 1042 | case USB_REQ_GET_DESCRIPTOR: |
| 1043 | if (ctrl->bRequestType != USB_DIR_IN) |
| 1044 | break; |
| 1045 | switch (wValue >> 8) { |
| 1046 | |
| 1047 | case USB_DT_DEVICE: |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1048 | value = min(wLength, (u16) sizeof device_desc); |
| 1049 | memcpy(req->buf, &device_desc, value); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1050 | break; |
| 1051 | case USB_DT_DEVICE_QUALIFIER: |
| 1052 | if (!gadget_is_dualspeed(gadget)) |
| 1053 | break; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1054 | value = min(wLength, (u16) sizeof dev_qualifier); |
| 1055 | memcpy(req->buf, &dev_qualifier, value); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1056 | break; |
| 1057 | |
| 1058 | case USB_DT_OTHER_SPEED_CONFIG: |
| 1059 | if (!gadget_is_dualspeed(gadget)) |
| 1060 | break; |
| 1061 | /* FALLTHROUGH */ |
| 1062 | case USB_DT_CONFIG: |
| 1063 | value = config_buf(gadget, req->buf, |
| 1064 | wValue >> 8, |
| 1065 | wValue & 0xff, |
| 1066 | gadget_is_otg(gadget)); |
| 1067 | if (value >= 0) |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1068 | value = min(wLength, (u16) value); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1069 | break; |
| 1070 | |
| 1071 | case USB_DT_STRING: |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1072 | value = usb_gadget_get_string(&stringtab, |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1073 | wValue & 0xff, req->buf); |
| 1074 | |
| 1075 | if (value >= 0) |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1076 | value = min(wLength, (u16) value); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1077 | |
| 1078 | break; |
| 1079 | } |
| 1080 | break; |
| 1081 | |
| 1082 | case USB_REQ_SET_CONFIGURATION: |
| 1083 | if (ctrl->bRequestType != 0) |
| 1084 | break; |
| 1085 | if (gadget->a_hnp_support) |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1086 | debug("HNP available\n"); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1087 | else if (gadget->a_alt_hnp_support) |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1088 | debug("HNP needs a different root port\n"); |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1089 | value = eth_set_config(dev, wValue, GFP_ATOMIC); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1090 | break; |
| 1091 | case USB_REQ_GET_CONFIGURATION: |
| 1092 | if (ctrl->bRequestType != USB_DIR_IN) |
| 1093 | break; |
| 1094 | *(u8 *)req->buf = dev->config; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1095 | value = min(wLength, (u16) 1); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1096 | break; |
| 1097 | |
| 1098 | case USB_REQ_SET_INTERFACE: |
| 1099 | if (ctrl->bRequestType != USB_RECIP_INTERFACE |
| 1100 | || !dev->config |
| 1101 | || wIndex > 1) |
| 1102 | break; |
| 1103 | if (!cdc_active(dev) && wIndex != 0) |
| 1104 | break; |
| 1105 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1106 | /* |
| 1107 | * PXA hardware partially handles SET_INTERFACE; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1108 | * we need to kluge around that interference. |
| 1109 | */ |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1110 | if (gadget_is_pxa(gadget)) { |
| 1111 | value = eth_set_config(dev, DEV_CONFIG_VALUE, |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1112 | GFP_ATOMIC); |
| 1113 | goto done_set_intf; |
| 1114 | } |
| 1115 | |
| 1116 | #ifdef DEV_CONFIG_CDC |
| 1117 | switch (wIndex) { |
| 1118 | case 0: /* control/master intf */ |
| 1119 | if (wValue != 0) |
| 1120 | break; |
| 1121 | if (dev->status) { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1122 | usb_ep_disable(dev->status_ep); |
| 1123 | usb_ep_enable(dev->status_ep, dev->status); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1124 | } |
| 1125 | value = 0; |
| 1126 | break; |
| 1127 | case 1: /* data intf */ |
| 1128 | if (wValue > 1) |
| 1129 | break; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1130 | usb_ep_disable(dev->in_ep); |
| 1131 | usb_ep_disable(dev->out_ep); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1132 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1133 | /* |
| 1134 | * CDC requires the data transfers not be done from |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1135 | * the default interface setting ... also, setting |
| 1136 | * the non-default interface resets filters etc. |
| 1137 | */ |
| 1138 | if (wValue == 1) { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1139 | if (!cdc_active(dev)) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1140 | break; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1141 | usb_ep_enable(dev->in_ep, dev->in); |
| 1142 | usb_ep_enable(dev->out_ep, dev->out); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1143 | dev->cdc_filter = DEFAULT_FILTER; |
| 1144 | if (dev->status) |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1145 | issue_start_status(dev); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | value = 0; |
| 1149 | break; |
| 1150 | } |
| 1151 | #else |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1152 | /* |
| 1153 | * FIXME this is wrong, as is the assumption that |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1154 | * all non-PXA hardware talks real CDC ... |
| 1155 | */ |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1156 | debug("set_interface ignored!\n"); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1157 | #endif /* DEV_CONFIG_CDC */ |
| 1158 | |
| 1159 | done_set_intf: |
| 1160 | break; |
| 1161 | case USB_REQ_GET_INTERFACE: |
| 1162 | if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE) |
| 1163 | || !dev->config |
| 1164 | || wIndex > 1) |
| 1165 | break; |
| 1166 | if (!(cdc_active(dev)) && wIndex != 0) |
| 1167 | break; |
| 1168 | |
| 1169 | /* for CDC, iff carrier is on, data interface is active. */ |
| 1170 | if (wIndex != 1) |
| 1171 | *(u8 *)req->buf = 0; |
| 1172 | else { |
| 1173 | /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */ |
| 1174 | /* carrier always ok ...*/ |
| 1175 | *(u8 *)req->buf = 1 ; |
| 1176 | } |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1177 | value = min(wLength, (u16) 1); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1178 | break; |
| 1179 | |
| 1180 | #ifdef DEV_CONFIG_CDC |
| 1181 | case USB_CDC_SET_ETHERNET_PACKET_FILTER: |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1182 | /* |
| 1183 | * see 6.2.30: no data, wIndex = interface, |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1184 | * wValue = packet filter bitmap |
| 1185 | */ |
| 1186 | if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE) |
| 1187 | || !cdc_active(dev) |
| 1188 | || wLength != 0 |
| 1189 | || wIndex > 1) |
| 1190 | break; |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1191 | debug("packet filter %02x\n", wValue); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1192 | dev->cdc_filter = wValue; |
| 1193 | value = 0; |
| 1194 | break; |
| 1195 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1196 | /* |
| 1197 | * and potentially: |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1198 | * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS: |
| 1199 | * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER: |
| 1200 | * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER: |
| 1201 | * case USB_CDC_GET_ETHERNET_STATISTIC: |
| 1202 | */ |
| 1203 | |
| 1204 | #endif /* DEV_CONFIG_CDC */ |
| 1205 | |
| 1206 | default: |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1207 | 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] | 1208 | ctrl->bRequestType, ctrl->bRequest, |
| 1209 | wValue, wIndex, wLength); |
| 1210 | } |
| 1211 | |
| 1212 | /* respond with data transfer before status phase? */ |
| 1213 | if (value >= 0) { |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1214 | debug("respond with data transfer before status phase\n"); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1215 | req->length = value; |
| 1216 | req->zero = value < wLength |
| 1217 | && (value % gadget->ep0->maxpacket) == 0; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1218 | value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1219 | if (value < 0) { |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1220 | debug("ep_queue --> %d\n", value); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1221 | req->status = 0; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1222 | eth_setup_complete(gadget->ep0, req); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1223 | } |
| 1224 | } |
| 1225 | |
| 1226 | /* host either stalls (value < 0) or reports success */ |
| 1227 | return value; |
| 1228 | } |
| 1229 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1230 | /*-------------------------------------------------------------------------*/ |
| 1231 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1232 | static void rx_complete(struct usb_ep *ep, struct usb_request *req); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1233 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1234 | static int rx_submit(struct eth_dev *dev, struct usb_request *req, |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1235 | gfp_t gfp_flags) |
| 1236 | { |
| 1237 | int retval = -ENOMEM; |
| 1238 | size_t size; |
| 1239 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1240 | /* |
| 1241 | * Padding up to RX_EXTRA handles minor disagreements with host. |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1242 | * Normally we use the USB "terminate on short read" convention; |
| 1243 | * so allow up to (N*maxpacket), since that memory is normally |
| 1244 | * already allocated. Some hardware doesn't deal well with short |
| 1245 | * reads (e.g. DMA must be N*maxpacket), so for now don't trim a |
| 1246 | * byte off the end (to force hardware errors on overflow). |
| 1247 | */ |
| 1248 | |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1249 | debug("%s\n", __func__); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1250 | |
| 1251 | size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA); |
| 1252 | size += dev->out_ep->maxpacket - 1; |
| 1253 | size -= size % dev->out_ep->maxpacket; |
| 1254 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1255 | /* |
| 1256 | * Some platforms perform better when IP packets are aligned, |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1257 | * but on at least one, checksumming fails otherwise. |
| 1258 | */ |
| 1259 | |
| 1260 | req->buf = (u8 *) NetRxPackets[0]; |
| 1261 | req->length = size; |
| 1262 | req->complete = rx_complete; |
| 1263 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1264 | retval = usb_ep_queue(dev->out_ep, req, gfp_flags); |
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 | if (retval) |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1267 | error("rx submit --> %d", retval); |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1268 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1269 | return retval; |
| 1270 | } |
| 1271 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1272 | static void rx_complete(struct usb_ep *ep, struct usb_request *req) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1273 | { |
| 1274 | struct eth_dev *dev = ep->driver_data; |
| 1275 | |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1276 | debug("%s: status %d\n", __func__, req->status); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1277 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1278 | packet_received = 1; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1279 | } |
| 1280 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1281 | 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] | 1282 | { |
| 1283 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1284 | dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1285 | |
| 1286 | if (!dev->tx_req) |
Vitaly Kuzmichev | ac5d32d | 2010-09-22 13:13:55 +0400 | [diff] [blame] | 1287 | goto fail1; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1288 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1289 | dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1290 | |
| 1291 | if (!dev->rx_req) |
Vitaly Kuzmichev | ac5d32d | 2010-09-22 13:13:55 +0400 | [diff] [blame] | 1292 | goto fail2; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1293 | |
| 1294 | return 0; |
| 1295 | |
Vitaly Kuzmichev | ac5d32d | 2010-09-22 13:13:55 +0400 | [diff] [blame] | 1296 | fail2: |
| 1297 | usb_ep_free_request(dev->in_ep, dev->tx_req); |
| 1298 | fail1: |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1299 | error("can't alloc requests"); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1300 | return -1; |
| 1301 | } |
| 1302 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1303 | static void tx_complete(struct usb_ep *ep, struct usb_request *req) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1304 | { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1305 | debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok"); |
| 1306 | packet_sent = 1; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1307 | } |
| 1308 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1309 | static inline int eth_is_promisc(struct eth_dev *dev) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1310 | { |
| 1311 | /* no filters for the CDC subset; always promisc */ |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1312 | if (subset_active(dev)) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1313 | return 1; |
| 1314 | return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS; |
| 1315 | } |
| 1316 | |
| 1317 | #if 0 |
| 1318 | static int eth_start_xmit (struct sk_buff *skb, struct net_device *net) |
| 1319 | { |
| 1320 | struct eth_dev *dev = netdev_priv(net); |
| 1321 | int length = skb->len; |
| 1322 | int retval; |
| 1323 | struct usb_request *req = NULL; |
| 1324 | unsigned long flags; |
| 1325 | |
| 1326 | /* apply outgoing CDC or RNDIS filters */ |
| 1327 | if (!eth_is_promisc (dev)) { |
| 1328 | u8 *dest = skb->data; |
| 1329 | |
| 1330 | if (is_multicast_ether_addr(dest)) { |
| 1331 | u16 type; |
| 1332 | |
| 1333 | /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host |
| 1334 | * SET_ETHERNET_MULTICAST_FILTERS requests |
| 1335 | */ |
| 1336 | if (is_broadcast_ether_addr(dest)) |
| 1337 | type = USB_CDC_PACKET_TYPE_BROADCAST; |
| 1338 | else |
| 1339 | type = USB_CDC_PACKET_TYPE_ALL_MULTICAST; |
| 1340 | if (!(dev->cdc_filter & type)) { |
| 1341 | dev_kfree_skb_any (skb); |
| 1342 | return 0; |
| 1343 | } |
| 1344 | } |
| 1345 | /* ignores USB_CDC_PACKET_TYPE_DIRECTED */ |
| 1346 | } |
| 1347 | |
| 1348 | spin_lock_irqsave(&dev->req_lock, flags); |
| 1349 | /* |
| 1350 | * this freelist can be empty if an interrupt triggered disconnect() |
| 1351 | * and reconfigured the gadget (shutting down this queue) after the |
| 1352 | * network stack decided to xmit but before we got the spinlock. |
| 1353 | */ |
| 1354 | if (list_empty(&dev->tx_reqs)) { |
| 1355 | spin_unlock_irqrestore(&dev->req_lock, flags); |
| 1356 | return 1; |
| 1357 | } |
| 1358 | |
| 1359 | req = container_of (dev->tx_reqs.next, struct usb_request, list); |
| 1360 | list_del (&req->list); |
| 1361 | |
| 1362 | /* temporarily stop TX queue when the freelist empties */ |
| 1363 | if (list_empty (&dev->tx_reqs)) |
| 1364 | netif_stop_queue (net); |
| 1365 | spin_unlock_irqrestore(&dev->req_lock, flags); |
| 1366 | |
| 1367 | /* no buffer copies needed, unless the network stack did it |
| 1368 | * or the hardware can't use skb buffers. |
| 1369 | * or there's not enough space for any RNDIS headers we need |
| 1370 | */ |
| 1371 | if (rndis_active(dev)) { |
| 1372 | struct sk_buff *skb_rndis; |
| 1373 | |
| 1374 | skb_rndis = skb_realloc_headroom (skb, |
| 1375 | sizeof (struct rndis_packet_msg_type)); |
| 1376 | if (!skb_rndis) |
| 1377 | goto drop; |
| 1378 | |
| 1379 | dev_kfree_skb_any (skb); |
| 1380 | skb = skb_rndis; |
| 1381 | rndis_add_hdr (skb); |
| 1382 | length = skb->len; |
| 1383 | } |
| 1384 | req->buf = skb->data; |
| 1385 | req->context = skb; |
| 1386 | req->complete = tx_complete; |
| 1387 | |
| 1388 | /* use zlp framing on tx for strict CDC-Ether conformance, |
| 1389 | * though any robust network rx path ignores extra padding. |
| 1390 | * and some hardware doesn't like to write zlps. |
| 1391 | */ |
| 1392 | req->zero = 1; |
| 1393 | if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0) |
| 1394 | length++; |
| 1395 | |
| 1396 | req->length = length; |
| 1397 | |
| 1398 | /* throttle highspeed IRQ rate back slightly */ |
| 1399 | if (gadget_is_dualspeed(dev->gadget)) |
| 1400 | req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH) |
| 1401 | ? ((atomic_read(&dev->tx_qlen) % qmult) != 0) |
| 1402 | : 0; |
| 1403 | |
| 1404 | retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC); |
| 1405 | switch (retval) { |
| 1406 | default: |
| 1407 | DEBUG (dev, "tx queue err %d\n", retval); |
| 1408 | break; |
| 1409 | case 0: |
| 1410 | net->trans_start = jiffies; |
| 1411 | atomic_inc (&dev->tx_qlen); |
| 1412 | } |
| 1413 | |
| 1414 | if (retval) { |
| 1415 | drop: |
| 1416 | dev->stats.tx_dropped++; |
| 1417 | dev_kfree_skb_any (skb); |
| 1418 | spin_lock_irqsave(&dev->req_lock, flags); |
| 1419 | if (list_empty (&dev->tx_reqs)) |
| 1420 | netif_start_queue (net); |
| 1421 | list_add (&req->list, &dev->tx_reqs); |
| 1422 | spin_unlock_irqrestore(&dev->req_lock, flags); |
| 1423 | } |
| 1424 | return 0; |
| 1425 | } |
| 1426 | |
| 1427 | /*-------------------------------------------------------------------------*/ |
| 1428 | #endif |
| 1429 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1430 | static void eth_unbind(struct usb_gadget *gadget) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1431 | { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1432 | struct eth_dev *dev = get_gadget_data(gadget); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1433 | |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1434 | debug("%s...\n", __func__); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1435 | |
Vitaly Kuzmichev | 0129e32 | 2010-08-13 17:00:16 +0400 | [diff] [blame] | 1436 | /* we've already been disconnected ... no i/o is active */ |
| 1437 | if (dev->req) { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1438 | usb_ep_free_request(gadget->ep0, dev->req); |
Vitaly Kuzmichev | 0129e32 | 2010-08-13 17:00:16 +0400 | [diff] [blame] | 1439 | dev->req = NULL; |
| 1440 | } |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1441 | if (dev->stat_req) { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1442 | usb_ep_free_request(dev->status_ep, dev->stat_req); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1443 | dev->stat_req = NULL; |
| 1444 | } |
| 1445 | |
| 1446 | if (dev->tx_req) { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1447 | usb_ep_free_request(dev->in_ep, dev->tx_req); |
| 1448 | dev->tx_req = NULL; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | if (dev->rx_req) { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1452 | usb_ep_free_request(dev->out_ep, dev->rx_req); |
| 1453 | dev->rx_req = NULL; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | /* unregister_netdev (dev->net);*/ |
| 1457 | /* free_netdev(dev->net);*/ |
| 1458 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1459 | set_gadget_data(gadget, NULL); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1460 | } |
| 1461 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1462 | static void eth_disconnect(struct usb_gadget *gadget) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1463 | { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1464 | eth_reset_config(get_gadget_data(gadget)); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1465 | } |
| 1466 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1467 | static void eth_suspend(struct usb_gadget *gadget) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1468 | { |
| 1469 | /* Not used */ |
| 1470 | } |
| 1471 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1472 | static void eth_resume(struct usb_gadget *gadget) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1473 | { |
| 1474 | /* Not used */ |
| 1475 | } |
| 1476 | |
| 1477 | /*-------------------------------------------------------------------------*/ |
| 1478 | |
| 1479 | static int is_eth_addr_valid(char *str) |
| 1480 | { |
| 1481 | if (strlen(str) == 17) { |
| 1482 | int i; |
| 1483 | char *p, *q; |
| 1484 | uchar ea[6]; |
| 1485 | |
| 1486 | /* see if it looks like an ethernet address */ |
| 1487 | |
| 1488 | p = str; |
| 1489 | |
| 1490 | for (i = 0; i < 6; i++) { |
| 1491 | char term = (i == 5 ? '\0' : ':'); |
| 1492 | |
| 1493 | ea[i] = simple_strtol(p, &q, 16); |
| 1494 | |
| 1495 | if ((q - p) != 2 || *q++ != term) |
| 1496 | break; |
| 1497 | |
| 1498 | p = q; |
| 1499 | } |
| 1500 | |
| 1501 | if (i == 6) /* it looks ok */ |
| 1502 | return 1; |
| 1503 | } |
| 1504 | return 0; |
| 1505 | } |
| 1506 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1507 | static u8 nibble(unsigned char c) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1508 | { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1509 | if (likely(isdigit(c))) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1510 | return c - '0'; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1511 | c = toupper(c); |
| 1512 | if (likely(isxdigit(c))) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1513 | return 10 + c - 'A'; |
| 1514 | return 0; |
| 1515 | } |
| 1516 | |
| 1517 | static int get_ether_addr(const char *str, u8 *dev_addr) |
| 1518 | { |
| 1519 | if (str) { |
| 1520 | unsigned i; |
| 1521 | |
| 1522 | for (i = 0; i < 6; i++) { |
| 1523 | unsigned char num; |
| 1524 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1525 | if ((*str == '.') || (*str == ':')) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1526 | str++; |
| 1527 | num = nibble(*str++) << 4; |
| 1528 | num |= (nibble(*str++)); |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1529 | dev_addr[i] = num; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1530 | } |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1531 | if (is_valid_ether_addr(dev_addr)) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1532 | return 0; |
| 1533 | } |
| 1534 | return 1; |
| 1535 | } |
| 1536 | |
| 1537 | static int eth_bind(struct usb_gadget *gadget) |
| 1538 | { |
| 1539 | struct eth_dev *dev = &l_ethdev; |
| 1540 | u8 cdc = 1, zlp = 1; |
| 1541 | struct usb_ep *in_ep, *out_ep, *status_ep = NULL; |
| 1542 | int gcnum; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1543 | u8 tmp[7]; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1544 | |
| 1545 | /* these flags are only ever cleared; compiler take note */ |
| 1546 | #ifndef DEV_CONFIG_CDC |
| 1547 | cdc = 0; |
| 1548 | #endif |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1549 | /* |
| 1550 | * Because most host side USB stacks handle CDC Ethernet, that |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1551 | * standard protocol is _strongly_ preferred for interop purposes. |
| 1552 | * (By everyone except Microsoft.) |
| 1553 | */ |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1554 | if (gadget_is_pxa(gadget)) { |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1555 | /* pxa doesn't support altsettings */ |
| 1556 | cdc = 0; |
| 1557 | } else if (gadget_is_musbhdrc(gadget)) { |
| 1558 | /* reduce tx dma overhead by avoiding special cases */ |
| 1559 | zlp = 0; |
| 1560 | } else if (gadget_is_sh(gadget)) { |
| 1561 | /* sh doesn't support multiple interfaces or configs */ |
| 1562 | cdc = 0; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1563 | } else if (gadget_is_sa1100(gadget)) { |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1564 | /* hardware can't write zlps */ |
| 1565 | zlp = 0; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1566 | /* |
| 1567 | * sa1100 CAN do CDC, without status endpoint ... we use |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1568 | * non-CDC to be compatible with ARM Linux-2.4 "usb-eth". |
| 1569 | */ |
| 1570 | cdc = 0; |
| 1571 | } |
| 1572 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1573 | gcnum = usb_gadget_controller_number(gadget); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1574 | if (gcnum >= 0) |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1575 | device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1576 | else { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1577 | /* |
| 1578 | * can't assume CDC works. don't want to default to |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1579 | * anything less functional on CDC-capable hardware, |
| 1580 | * so we fail in this case. |
| 1581 | */ |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1582 | error("controller '%s' not recognized", |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1583 | gadget->name); |
| 1584 | return -ENODEV; |
| 1585 | } |
| 1586 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1587 | /* |
| 1588 | * CDC subset ... recognized by Linux since 2.4.10, but Windows |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1589 | * drivers aren't widely available. (That may be improved by |
| 1590 | * supporting one submode of the "SAFE" variant of MDLM.) |
| 1591 | */ |
| 1592 | if (!cdc) { |
| 1593 | device_desc.idVendor = |
| 1594 | __constant_cpu_to_le16(SIMPLE_VENDOR_NUM); |
| 1595 | device_desc.idProduct = |
| 1596 | __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM); |
| 1597 | } |
| 1598 | |
| 1599 | /* support optional vendor/distro customization */ |
| 1600 | #if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID) |
| 1601 | device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID); |
| 1602 | device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID); |
| 1603 | #endif |
| 1604 | if (bcdDevice) |
| 1605 | device_desc.bcdDevice = cpu_to_le16(bcdDevice); |
| 1606 | if (iManufacturer) |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1607 | strlcpy(manufacturer, iManufacturer, sizeof manufacturer); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1608 | if (iProduct) |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1609 | strlcpy(product_desc, iProduct, sizeof product_desc); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1610 | if (iSerialNumber) { |
| 1611 | device_desc.iSerialNumber = STRING_SERIALNUMBER, |
Vitaly Kuzmichev | 2e12abe | 2010-08-13 17:00:45 +0400 | [diff] [blame] | 1612 | strlcpy(serial_number, iSerialNumber, sizeof serial_number); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | /* all we really need is bulk IN/OUT */ |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1616 | usb_ep_autoconfig_reset(gadget); |
| 1617 | in_ep = usb_ep_autoconfig(gadget, &fs_source_desc); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1618 | if (!in_ep) { |
| 1619 | autoconf_fail: |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1620 | error("can't autoconfigure on %s\n", |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1621 | gadget->name); |
| 1622 | return -ENODEV; |
| 1623 | } |
| 1624 | in_ep->driver_data = in_ep; /* claim */ |
| 1625 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1626 | out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1627 | if (!out_ep) |
| 1628 | goto autoconf_fail; |
| 1629 | out_ep->driver_data = out_ep; /* claim */ |
| 1630 | |
| 1631 | #if defined(DEV_CONFIG_CDC) |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1632 | /* |
| 1633 | * CDC Ethernet control interface doesn't require a status endpoint. |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1634 | * Since some hosts expect one, try to allocate one anyway. |
| 1635 | */ |
| 1636 | if (cdc) { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1637 | status_ep = usb_ep_autoconfig(gadget, &fs_status_desc); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1638 | if (status_ep) { |
| 1639 | status_ep->driver_data = status_ep; /* claim */ |
| 1640 | } else if (cdc) { |
| 1641 | control_intf.bNumEndpoints = 0; |
| 1642 | /* FIXME remove endpoint from descriptor list */ |
| 1643 | } |
| 1644 | } |
| 1645 | #endif |
| 1646 | |
| 1647 | /* one config: cdc, else minimal subset */ |
| 1648 | if (!cdc) { |
| 1649 | eth_config.bNumInterfaces = 1; |
| 1650 | eth_config.iConfiguration = STRING_SUBSET; |
| 1651 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1652 | /* |
| 1653 | * 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] | 1654 | * with multiple controllers and must override CDC Ethernet. |
| 1655 | */ |
| 1656 | fs_subset_descriptors(); |
| 1657 | hs_subset_descriptors(); |
| 1658 | } |
| 1659 | |
| 1660 | device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1661 | usb_gadget_set_selfpowered(gadget); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1662 | |
| 1663 | if (gadget_is_dualspeed(gadget)) { |
| 1664 | if (!cdc) |
| 1665 | dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC; |
| 1666 | |
| 1667 | /* assumes ep0 uses the same value for both speeds ... */ |
| 1668 | dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0; |
| 1669 | |
| 1670 | /* and that all endpoints are dual-speed */ |
| 1671 | hs_source_desc.bEndpointAddress = |
| 1672 | fs_source_desc.bEndpointAddress; |
| 1673 | hs_sink_desc.bEndpointAddress = |
| 1674 | fs_sink_desc.bEndpointAddress; |
| 1675 | #if defined(DEV_CONFIG_CDC) |
| 1676 | if (status_ep) |
| 1677 | hs_status_desc.bEndpointAddress = |
| 1678 | fs_status_desc.bEndpointAddress; |
| 1679 | #endif |
| 1680 | } |
| 1681 | |
| 1682 | if (gadget_is_otg(gadget)) { |
| 1683 | otg_descriptor.bmAttributes |= USB_OTG_HNP, |
| 1684 | eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
| 1685 | eth_config.bMaxPower = 4; |
| 1686 | } |
| 1687 | |
| 1688 | dev->net = &l_netdev; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1689 | strcpy(dev->net->name, USB_NET_NAME); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1690 | |
| 1691 | dev->cdc = cdc; |
| 1692 | dev->zlp = zlp; |
| 1693 | |
| 1694 | dev->in_ep = in_ep; |
| 1695 | dev->out_ep = out_ep; |
| 1696 | dev->status_ep = status_ep; |
| 1697 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1698 | /* |
| 1699 | * Module params for these addresses should come from ID proms. |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1700 | * The host side address is used with CDC, and commonly |
| 1701 | * ends up in a persistent config database. It's not clear if |
| 1702 | * host side code for the SAFE thing cares -- its original BLAN |
| 1703 | * thing didn't, Sharp never assigned those addresses on Zaurii. |
| 1704 | */ |
| 1705 | get_ether_addr(dev_addr, dev->net->enetaddr); |
| 1706 | |
| 1707 | memset(tmp, 0, sizeof(tmp)); |
| 1708 | memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr)); |
| 1709 | |
| 1710 | get_ether_addr(host_addr, dev->host_mac); |
| 1711 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1712 | sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X", |
| 1713 | dev->host_mac[0], dev->host_mac[1], |
| 1714 | dev->host_mac[2], dev->host_mac[3], |
| 1715 | dev->host_mac[4], dev->host_mac[5]); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1716 | |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1717 | printf("using %s, OUT %s IN %s%s%s\n", gadget->name, |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1718 | out_ep->name, in_ep->name, |
| 1719 | status_ep ? " STATUS " : "", |
| 1720 | status_ep ? status_ep->name : "" |
| 1721 | ); |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1722 | printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n", |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1723 | dev->net->enetaddr[0], dev->net->enetaddr[1], |
| 1724 | dev->net->enetaddr[2], dev->net->enetaddr[3], |
| 1725 | dev->net->enetaddr[4], dev->net->enetaddr[5]); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1726 | |
| 1727 | if (cdc) { |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1728 | printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n", |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1729 | dev->host_mac[0], dev->host_mac[1], |
| 1730 | dev->host_mac[2], dev->host_mac[3], |
| 1731 | dev->host_mac[4], dev->host_mac[5]); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1732 | } |
| 1733 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1734 | /* |
| 1735 | * use PKTSIZE (or aligned... from u-boot) and set |
| 1736 | * wMaxSegmentSize accordingly |
| 1737 | */ |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1738 | dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/ |
| 1739 | |
| 1740 | /* preallocate control message data and buffer */ |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1741 | dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1742 | if (!dev->req) |
| 1743 | goto fail; |
| 1744 | dev->req->buf = control_req; |
| 1745 | dev->req->complete = eth_setup_complete; |
| 1746 | |
| 1747 | /* ... and maybe likewise for status transfer */ |
| 1748 | #if defined(DEV_CONFIG_CDC) |
| 1749 | if (dev->status_ep) { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1750 | dev->stat_req = usb_ep_alloc_request(dev->status_ep, |
| 1751 | GFP_KERNEL); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1752 | if (!dev->stat_req) { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1753 | usb_ep_free_request(dev->status_ep, dev->req); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1754 | |
| 1755 | goto fail; |
| 1756 | } |
Vitaly Kuzmichev | df559c1 | 2010-08-13 17:01:06 +0400 | [diff] [blame] | 1757 | dev->stat_req->buf = status_req; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1758 | dev->stat_req->context = NULL; |
| 1759 | } |
| 1760 | #endif |
| 1761 | |
| 1762 | /* finish hookup to lower layer ... */ |
| 1763 | dev->gadget = gadget; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1764 | set_gadget_data(gadget, dev); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1765 | gadget->ep0->driver_data = dev; |
| 1766 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1767 | /* |
| 1768 | * two kinds of host-initiated state changes: |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1769 | * - iff DATA transfer is active, carrier is "on" |
| 1770 | * - tx queueing enabled if open *and* carrier is "on" |
| 1771 | */ |
| 1772 | return 0; |
| 1773 | |
| 1774 | fail: |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1775 | error("%s failed", __func__); |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1776 | eth_unbind(gadget); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1777 | return -ENOMEM; |
| 1778 | } |
| 1779 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1780 | static int usb_eth_init(struct eth_device *netdev, bd_t *bd) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1781 | { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1782 | struct eth_dev *dev = &l_ethdev; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1783 | struct usb_gadget *gadget; |
| 1784 | unsigned long ts; |
| 1785 | unsigned long timeout = USB_CONNECT_TIMEOUT; |
| 1786 | |
| 1787 | if (!netdev) { |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1788 | error("received NULL ptr"); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1789 | goto fail; |
| 1790 | } |
| 1791 | |
| 1792 | dev->network_started = 0; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1793 | |
| 1794 | packet_received = 0; |
| 1795 | packet_sent = 0; |
| 1796 | |
| 1797 | gadget = dev->gadget; |
| 1798 | usb_gadget_connect(gadget); |
| 1799 | |
| 1800 | if (getenv("cdc_connect_timeout")) |
| 1801 | timeout = simple_strtoul(getenv("cdc_connect_timeout"), |
| 1802 | NULL, 10) * CONFIG_SYS_HZ; |
| 1803 | ts = get_timer(0); |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1804 | while (!l_ethdev.network_started) { |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1805 | /* Handle control-c and timeouts */ |
| 1806 | if (ctrlc() || (get_timer(ts) > timeout)) { |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1807 | error("The remote end did not respond in time."); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1808 | goto fail; |
| 1809 | } |
| 1810 | usb_gadget_handle_interrupts(); |
| 1811 | } |
| 1812 | |
Vitaly Kuzmichev | 98fae97 | 2010-09-22 13:13:56 +0400 | [diff] [blame^] | 1813 | packet_received = 0; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1814 | rx_submit(dev, dev->rx_req, 0); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1815 | return 0; |
| 1816 | fail: |
| 1817 | return -1; |
| 1818 | } |
| 1819 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1820 | static int usb_eth_send(struct eth_device *netdev, |
| 1821 | volatile void *packet, int length) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1822 | { |
| 1823 | int retval; |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1824 | struct eth_dev *dev = &l_ethdev; |
Vitaly Kuzmichev | ac5d32d | 2010-09-22 13:13:55 +0400 | [diff] [blame] | 1825 | struct usb_request *req = dev->tx_req; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1826 | unsigned long ts; |
| 1827 | unsigned long timeout = USB_CONNECT_TIMEOUT; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1828 | |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1829 | debug("%s:...\n", __func__); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1830 | |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1831 | req->buf = (void *)packet; |
| 1832 | req->context = NULL; |
| 1833 | req->complete = tx_complete; |
| 1834 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1835 | /* |
| 1836 | * use zlp framing on tx for strict CDC-Ether conformance, |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1837 | * though any robust network rx path ignores extra padding. |
| 1838 | * and some hardware doesn't like to write zlps. |
| 1839 | */ |
| 1840 | req->zero = 1; |
| 1841 | if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0) |
| 1842 | length++; |
| 1843 | |
| 1844 | req->length = length; |
| 1845 | #if 0 |
| 1846 | /* throttle highspeed IRQ rate back slightly */ |
| 1847 | if (gadget_is_dualspeed(dev->gadget)) |
| 1848 | req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH) |
| 1849 | ? ((dev->tx_qlen % qmult) != 0) : 0; |
| 1850 | #endif |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1851 | dev->tx_qlen = 1; |
| 1852 | ts = get_timer(0); |
| 1853 | packet_sent = 0; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1854 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1855 | retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1856 | |
| 1857 | if (!retval) |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1858 | debug("%s: packet queued\n", __func__); |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1859 | while (!packet_sent) { |
| 1860 | if (get_timer(ts) > timeout) { |
| 1861 | printf("timeout sending packets to usb ethernet\n"); |
| 1862 | return -1; |
| 1863 | } |
| 1864 | usb_gadget_handle_interrupts(); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1865 | } |
| 1866 | |
| 1867 | return 0; |
| 1868 | } |
| 1869 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1870 | static int usb_eth_recv(struct eth_device *netdev) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1871 | { |
| 1872 | struct eth_dev *dev = &l_ethdev; |
| 1873 | |
| 1874 | usb_gadget_handle_interrupts(); |
| 1875 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1876 | if (packet_received) { |
| 1877 | debug("%s: packet received\n", __func__); |
| 1878 | if (dev->rx_req) { |
| 1879 | NetReceive(NetRxPackets[0], dev->rx_req->length); |
| 1880 | packet_received = 0; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1881 | |
Vitaly Kuzmichev | ac5d32d | 2010-09-22 13:13:55 +0400 | [diff] [blame] | 1882 | rx_submit(dev, dev->rx_req, 0); |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1883 | } else |
| 1884 | error("dev->rx_req invalid"); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1885 | } |
| 1886 | return 0; |
| 1887 | } |
| 1888 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1889 | void usb_eth_halt(struct eth_device *netdev) |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1890 | { |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1891 | struct eth_dev *dev = &l_ethdev; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1892 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1893 | if (!netdev) { |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1894 | error("received NULL ptr"); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1895 | return; |
| 1896 | } |
| 1897 | |
| 1898 | usb_gadget_disconnect(dev->gadget); |
| 1899 | } |
| 1900 | |
| 1901 | static struct usb_gadget_driver eth_driver = { |
| 1902 | .speed = DEVSPEED, |
| 1903 | |
| 1904 | .bind = eth_bind, |
| 1905 | .unbind = eth_unbind, |
| 1906 | |
| 1907 | .setup = eth_setup, |
| 1908 | .disconnect = eth_disconnect, |
| 1909 | |
| 1910 | .suspend = eth_suspend, |
| 1911 | .resume = eth_resume, |
| 1912 | }; |
| 1913 | |
| 1914 | int usb_eth_initialize(bd_t *bi) |
| 1915 | { |
| 1916 | int status = 0; |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1917 | struct eth_device *netdev = &l_netdev; |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1918 | |
Vitaly Kuzmichev | 6142e0a | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1919 | sprintf(netdev->name, "usb_ether"); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1920 | |
| 1921 | netdev->init = usb_eth_init; |
| 1922 | netdev->send = usb_eth_send; |
| 1923 | netdev->recv = usb_eth_recv; |
| 1924 | netdev->halt = usb_eth_halt; |
| 1925 | |
| 1926 | #ifdef CONFIG_MCAST_TFTP |
| 1927 | #error not supported |
| 1928 | #endif |
| 1929 | /* Configure default mac-addresses for the USB ethernet device */ |
| 1930 | #ifdef CONFIG_USBNET_DEV_ADDR |
| 1931 | strncpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr)); |
| 1932 | #endif |
| 1933 | #ifdef CONFIG_USBNET_HOST_ADDR |
| 1934 | strncpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr)); |
| 1935 | #endif |
| 1936 | /* Check if the user overruled the MAC addresses */ |
| 1937 | if (getenv("usbnet_devaddr")) |
| 1938 | strncpy(dev_addr, getenv("usbnet_devaddr"), |
| 1939 | sizeof(dev_addr)); |
| 1940 | |
| 1941 | if (getenv("usbnet_hostaddr")) |
| 1942 | strncpy(host_addr, getenv("usbnet_hostaddr"), |
| 1943 | sizeof(host_addr)); |
| 1944 | |
| 1945 | /* Make sure both strings are terminated */ |
| 1946 | dev_addr[sizeof(dev_addr)-1] = '\0'; |
| 1947 | host_addr[sizeof(host_addr)-1] = '\0'; |
| 1948 | |
| 1949 | if (!is_eth_addr_valid(dev_addr)) { |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1950 | error("Need valid 'usbnet_devaddr' to be set"); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1951 | status = -1; |
| 1952 | } |
| 1953 | if (!is_eth_addr_valid(host_addr)) { |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1954 | error("Need valid 'usbnet_hostaddr' to be set"); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1955 | status = -1; |
| 1956 | } |
| 1957 | if (status) |
| 1958 | goto fail; |
| 1959 | |
| 1960 | status = usb_gadget_register_driver(ð_driver); |
| 1961 | if (status < 0) |
| 1962 | goto fail; |
| 1963 | |
| 1964 | eth_register(netdev); |
| 1965 | return 0; |
| 1966 | |
| 1967 | fail: |
Vitaly Kuzmichev | 7de7318 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1968 | error("%s failed. error = %d", __func__, status); |
Remy Bohmer | 23cd138 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1969 | return status; |
| 1970 | } |
| 1971 | |