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