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