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