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