1 // SPDX-License-Identifier: GPL-2.0+
3 * ether.c -- Ethernet gadget driver, with CDC and non-CDC options
5 * Copyright (C) 2003-2005,2008 David Brownell
6 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
7 * Copyright (C) 2008 Nokia Corporation
15 #include <linux/errno.h>
16 #include <linux/netdevice.h>
17 #include <linux/usb/ch9.h>
18 #include <linux/usb/cdc.h>
19 #include <linux/usb/gadget.h>
24 #include <linux/ctype.h>
26 #include "gadget_chips.h"
31 #include <dm/uclass-internal.h>
32 #include <dm/device-internal.h>
34 #define USB_NET_NAME "usb_ether"
36 extern struct platform_data brd;
39 unsigned packet_received, packet_sent;
42 * Ethernet gadget driver -- with CDC and non-CDC options
43 * Builds on hardware support for a full duplex link.
45 * CDC Ethernet is the standard USB solution for sending Ethernet frames
46 * using USB. Real hardware tends to use the same framing protocol but look
47 * different for control features. This driver strongly prefers to use
48 * this USB-IF standard as its open-systems interoperability solution;
49 * most host side USB stacks (except from Microsoft) support it.
51 * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
52 * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
53 * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
55 * There's some hardware that can't talk CDC ECM. We make that hardware
56 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
57 * link-level setup only requires activating the configuration. Only the
58 * endpoint descriptors, and product/vendor IDs, are relevant; no control
59 * operations are available. Linux supports it, but other host operating
60 * systems may not. (This is a subset of CDC Ethernet.)
62 * It turns out that if you add a few descriptors to that "CDC Subset",
63 * (Windows) host side drivers from MCCI can treat it as one submode of
64 * a proprietary scheme called "SAFE" ... without needing to know about
65 * specific product/vendor IDs. So we do that, making it easier to use
66 * those MS-Windows drivers. Those added descriptors make it resemble a
67 * CDC MDLM device, but they don't change device behavior at all. (See
68 * MCCI Engineering report 950198 "SAFE Networking Functions".)
70 * A third option is also in use. Rather than CDC Ethernet, or something
71 * simpler, Microsoft pushes their own approach: RNDIS. The published
72 * RNDIS specs are ambiguous and appear to be incomplete, and are also
73 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
76 #define DRIVER_DESC "Ethernet Gadget"
77 /* Based on linux 2.6.27 version */
78 #define DRIVER_VERSION "May Day 2005"
80 static const char driver_desc[] = DRIVER_DESC;
82 #define RX_EXTRA 20 /* guard against rx overflows */
84 #ifndef CONFIG_USB_ETH_RNDIS
85 #define rndis_uninit(x) do {} while (0)
86 #define rndis_deregister(c) do {} while (0)
87 #define rndis_exit() do {} while (0)
90 /* CDC and RNDIS support the same host-chosen outgoing packet filters. */
91 #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
92 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
93 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
94 |USB_CDC_PACKET_TYPE_DIRECTED)
96 #define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
98 /*-------------------------------------------------------------------------*/
101 struct usb_gadget *gadget;
102 struct usb_request *req; /* for control responses */
103 struct usb_request *stat_req; /* for cdc & rndis status */
106 struct usb_ep *in_ep, *out_ep, *status_ep;
107 const struct usb_endpoint_descriptor
110 struct usb_request *tx_req, *rx_req;
112 #ifndef CONFIG_DM_ETH
113 struct eth_device *net;
117 struct net_device_stats stats;
118 unsigned int tx_qlen;
123 unsigned suspended:1;
124 unsigned network_started:1;
128 #define WORK_RX_MEMORY 0
130 u8 host_mac[ETH_ALEN];
134 * This version autoconfigures as much as possible at run-time.
136 * It also ASSUMES a self-powered device, without remote wakeup,
137 * although remote wakeup support would make sense.
140 /*-------------------------------------------------------------------------*/
142 struct eth_dev ethdev;
143 #ifndef CONFIG_DM_ETH
144 struct eth_device netdev;
146 struct udevice *netdev;
148 struct usb_gadget_driver eth_driver;
151 struct ether_priv eth_priv;
152 struct ether_priv *l_priv = ð_priv;
154 /*-------------------------------------------------------------------------*/
156 /* "main" config is either CDC, or its simple subset */
157 static inline int is_cdc(struct eth_dev *dev)
159 #if !defined(CONFIG_USB_ETH_SUBSET)
160 return 1; /* only cdc possible */
161 #elif !defined(CONFIG_USB_ETH_CDC)
162 return 0; /* only subset possible */
164 return dev->cdc; /* depends on what hardware we found */
168 /* "secondary" RNDIS config may sometimes be activated */
169 static inline int rndis_active(struct eth_dev *dev)
171 #ifdef CONFIG_USB_ETH_RNDIS
178 #define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
179 #define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev))
181 #define DEFAULT_QLEN 2 /* double buffering by default */
183 /* peak bulk transfer bits-per-second */
184 #define HS_BPS (13 * 512 * 8 * 1000 * 8)
185 #define FS_BPS (19 * 64 * 1 * 1000 * 8)
187 #ifdef CONFIG_USB_GADGET_DUALSPEED
188 #define DEVSPEED USB_SPEED_HIGH
190 #ifdef CONFIG_USB_ETH_QMULT
191 #define qmult CONFIG_USB_ETH_QMULT
196 /* for dual-speed hardware, use deeper queues at highspeed */
197 #define qlen(gadget) \
198 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
200 static inline int BITRATE(struct usb_gadget *g)
202 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
205 #else /* full speed (low speed doesn't do bulk) */
209 #define DEVSPEED USB_SPEED_FULL
211 #define qlen(gadget) DEFAULT_QLEN
213 static inline int BITRATE(struct usb_gadget *g)
219 /*-------------------------------------------------------------------------*/
222 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
223 * Instead: allocate your own, using normal USB-IF procedures.
227 * Thanks to NetChip Technologies for donating this product ID.
228 * It's for devices with only CDC Ethernet configurations.
230 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
231 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
234 * For hardware that can't talk CDC, we use the same vendor ID that
235 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
236 * with pxa250. We're protocol-compatible, if the host-side drivers
237 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
238 * drivers that need to hard-wire endpoint numbers have a hook.
240 * The protocol is a minimal subset of CDC Ether, which works on any bulk
241 * hardware that's not deeply broken ... even on hardware that can't talk
242 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
243 * doesn't handle control-OUT).
245 #define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */
246 #define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */
249 * For hardware that can talk RNDIS and either of the above protocols,
250 * use this ID ... the windows INF files will know it. Unless it's
251 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
252 * the non-RNDIS configuration.
254 #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
255 #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
258 * Some systems will want different product identifers published in the
259 * device descriptor, either numbers or strings or both. These string
260 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
264 * Emulating them in eth_bind:
265 * static ushort idVendor;
266 * static ushort idProduct;
269 #if defined(CONFIG_USB_GADGET_MANUFACTURER)
270 static char *iManufacturer = CONFIG_USB_GADGET_MANUFACTURER;
272 static char *iManufacturer = "U-Boot";
275 /* These probably need to be configurable. */
276 static ushort bcdDevice;
277 static char *iProduct;
278 static char *iSerialNumber;
280 static char dev_addr[18];
282 static char host_addr[18];
285 /*-------------------------------------------------------------------------*/
288 * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
289 * ep0 implementation: descriptors, config management, setup().
290 * also optional class-specific notification interrupt transfer.
294 * DESCRIPTORS ... most are static, but strings and (full) configuration
295 * descriptors are built on demand. For now we do either full CDC, or
296 * our simple subset, with RNDIS as an optional second configuration.
298 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
299 * the class descriptors match a modem (they're ignored; it's really just
300 * Ethernet functionality), they don't need the NOP altsetting, and the
301 * status transfer endpoint isn't optional.
304 #define STRING_MANUFACTURER 1
305 #define STRING_PRODUCT 2
306 #define STRING_ETHADDR 3
307 #define STRING_DATA 4
308 #define STRING_CONTROL 5
309 #define STRING_RNDIS_CONTROL 6
311 #define STRING_SUBSET 8
312 #define STRING_RNDIS 9
313 #define STRING_SERIALNUMBER 10
315 /* holds our biggest descriptor (or RNDIS response) */
316 #define USB_BUFSIZ 256
319 * This device advertises one configuration, eth_config, unless RNDIS
320 * is enabled (rndis_config) on hardware supporting at least two configs.
322 * NOTE: Controllers like superh_udc should probably be able to use
323 * an RNDIS-only configuration.
325 * FIXME define some higher-powered configurations to make it easier
326 * to recharge batteries ...
329 #define DEV_CONFIG_VALUE 1 /* cdc or subset */
330 #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
332 static struct usb_device_descriptor
334 .bLength = sizeof device_desc,
335 .bDescriptorType = USB_DT_DEVICE,
337 .bcdUSB = __constant_cpu_to_le16(0x0200),
339 .bDeviceClass = USB_CLASS_COMM,
340 .bDeviceSubClass = 0,
341 .bDeviceProtocol = 0,
343 .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
344 .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
345 .iManufacturer = STRING_MANUFACTURER,
346 .iProduct = STRING_PRODUCT,
347 .bNumConfigurations = 1,
350 static struct usb_otg_descriptor
352 .bLength = sizeof otg_descriptor,
353 .bDescriptorType = USB_DT_OTG,
355 .bmAttributes = USB_OTG_SRP,
358 static struct usb_config_descriptor
360 .bLength = sizeof eth_config,
361 .bDescriptorType = USB_DT_CONFIG,
363 /* compute wTotalLength on the fly */
365 .bConfigurationValue = DEV_CONFIG_VALUE,
366 .iConfiguration = STRING_CDC,
367 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
371 #ifdef CONFIG_USB_ETH_RNDIS
372 static struct usb_config_descriptor
374 .bLength = sizeof rndis_config,
375 .bDescriptorType = USB_DT_CONFIG,
377 /* compute wTotalLength on the fly */
379 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
380 .iConfiguration = STRING_RNDIS,
381 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
387 * Compared to the simple CDC subset, the full CDC Ethernet model adds
388 * three class descriptors, two interface descriptors, optional status
389 * endpoint. Both have a "data" interface and two bulk endpoints.
390 * There are also differences in how control requests are handled.
392 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
393 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
394 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
395 * work around those bugs) are unlikely to ever come from MSFT, you may
396 * wish to avoid using RNDIS.
398 * MCCI offers an alternative to RNDIS if you need to connect to Windows
399 * but have hardware that can't support CDC Ethernet. We add descriptors
400 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
401 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
402 * get those drivers from MCCI, or bundled with various products.
405 #ifdef CONFIG_USB_ETH_CDC
406 static struct usb_interface_descriptor
408 .bLength = sizeof control_intf,
409 .bDescriptorType = USB_DT_INTERFACE,
411 .bInterfaceNumber = 0,
412 /* status endpoint is optional; this may be patched later */
414 .bInterfaceClass = USB_CLASS_COMM,
415 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
416 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
417 .iInterface = STRING_CONTROL,
421 #ifdef CONFIG_USB_ETH_RNDIS
422 static const struct usb_interface_descriptor
423 rndis_control_intf = {
424 .bLength = sizeof rndis_control_intf,
425 .bDescriptorType = USB_DT_INTERFACE,
427 .bInterfaceNumber = 0,
429 .bInterfaceClass = USB_CLASS_COMM,
430 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
431 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
432 .iInterface = STRING_RNDIS_CONTROL,
436 static const struct usb_cdc_header_desc header_desc = {
437 .bLength = sizeof header_desc,
438 .bDescriptorType = USB_DT_CS_INTERFACE,
439 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
441 .bcdCDC = __constant_cpu_to_le16(0x0110),
444 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
446 static const struct usb_cdc_union_desc union_desc = {
447 .bLength = sizeof union_desc,
448 .bDescriptorType = USB_DT_CS_INTERFACE,
449 .bDescriptorSubType = USB_CDC_UNION_TYPE,
451 .bMasterInterface0 = 0, /* index of control interface */
452 .bSlaveInterface0 = 1, /* index of DATA interface */
455 #endif /* CDC || RNDIS */
457 #ifdef CONFIG_USB_ETH_RNDIS
459 static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
460 .bLength = sizeof call_mgmt_descriptor,
461 .bDescriptorType = USB_DT_CS_INTERFACE,
462 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
464 .bmCapabilities = 0x00,
465 .bDataInterface = 0x01,
468 static const struct usb_cdc_acm_descriptor acm_descriptor = {
469 .bLength = sizeof acm_descriptor,
470 .bDescriptorType = USB_DT_CS_INTERFACE,
471 .bDescriptorSubType = USB_CDC_ACM_TYPE,
473 .bmCapabilities = 0x00,
478 #ifndef CONFIG_USB_ETH_CDC
481 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
482 * ways: data endpoints live in the control interface, there's no data
483 * interface, and it's not used to talk to a cell phone radio.
486 static const struct usb_cdc_mdlm_desc mdlm_desc = {
487 .bLength = sizeof mdlm_desc,
488 .bDescriptorType = USB_DT_CS_INTERFACE,
489 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
491 .bcdVersion = __constant_cpu_to_le16(0x0100),
493 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
494 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
499 * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
500 * can't really use its struct. All we do here is say that we're using
501 * the submode of "SAFE" which directly matches the CDC Subset.
503 #ifdef CONFIG_USB_ETH_SUBSET
504 static const u8 mdlm_detail_desc[] = {
507 USB_CDC_MDLM_DETAIL_TYPE,
510 0, /* network control capabilities (none) */
511 0, /* network data capabilities ("raw" encapsulation) */
517 static const struct usb_cdc_ether_desc ether_desc = {
518 .bLength = sizeof(ether_desc),
519 .bDescriptorType = USB_DT_CS_INTERFACE,
520 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
522 /* this descriptor actually adds value, surprise! */
523 .iMACAddress = STRING_ETHADDR,
524 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
525 .wMaxSegmentSize = __constant_cpu_to_le16(PKTSIZE_ALIGN),
526 .wNumberMCFilters = __constant_cpu_to_le16(0),
527 .bNumberPowerFilters = 0,
530 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
533 * include the status endpoint if we can, even where it's optional.
534 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
535 * packet, to simplify cancellation; and a big transfer interval, to
536 * waste less bandwidth.
538 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
539 * if they ignore the connect/disconnect notifications that real aether
540 * can provide. more advanced cdc configurations might want to support
541 * encapsulated commands (vendor-specific, using control-OUT).
543 * RNDIS requires the status endpoint, since it uses that encapsulation
544 * mechanism for its funky RPC scheme.
547 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
548 #define STATUS_BYTECOUNT 16 /* 8 byte header + data */
550 static struct usb_endpoint_descriptor
552 .bLength = USB_DT_ENDPOINT_SIZE,
553 .bDescriptorType = USB_DT_ENDPOINT,
555 .bEndpointAddress = USB_DIR_IN,
556 .bmAttributes = USB_ENDPOINT_XFER_INT,
557 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
558 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
562 #ifdef CONFIG_USB_ETH_CDC
564 /* the default data interface has no endpoints ... */
566 static const struct usb_interface_descriptor
568 .bLength = sizeof data_nop_intf,
569 .bDescriptorType = USB_DT_INTERFACE,
571 .bInterfaceNumber = 1,
572 .bAlternateSetting = 0,
574 .bInterfaceClass = USB_CLASS_CDC_DATA,
575 .bInterfaceSubClass = 0,
576 .bInterfaceProtocol = 0,
579 /* ... but the "real" data interface has two bulk endpoints */
581 static const struct usb_interface_descriptor
583 .bLength = sizeof data_intf,
584 .bDescriptorType = USB_DT_INTERFACE,
586 .bInterfaceNumber = 1,
587 .bAlternateSetting = 1,
589 .bInterfaceClass = USB_CLASS_CDC_DATA,
590 .bInterfaceSubClass = 0,
591 .bInterfaceProtocol = 0,
592 .iInterface = STRING_DATA,
597 #ifdef CONFIG_USB_ETH_RNDIS
599 /* RNDIS doesn't activate by changing to the "real" altsetting */
601 static const struct usb_interface_descriptor
603 .bLength = sizeof rndis_data_intf,
604 .bDescriptorType = USB_DT_INTERFACE,
606 .bInterfaceNumber = 1,
607 .bAlternateSetting = 0,
609 .bInterfaceClass = USB_CLASS_CDC_DATA,
610 .bInterfaceSubClass = 0,
611 .bInterfaceProtocol = 0,
612 .iInterface = STRING_DATA,
617 #ifdef CONFIG_USB_ETH_SUBSET
620 * "Simple" CDC-subset option is a simple vendor-neutral model that most
621 * full speed controllers can handle: one interface, two bulk endpoints.
623 * To assist host side drivers, we fancy it up a bit, and add descriptors
624 * so some host side drivers will understand it as a "SAFE" variant.
627 static const struct usb_interface_descriptor
629 .bLength = sizeof subset_data_intf,
630 .bDescriptorType = USB_DT_INTERFACE,
632 .bInterfaceNumber = 0,
633 .bAlternateSetting = 0,
635 .bInterfaceClass = USB_CLASS_COMM,
636 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
637 .bInterfaceProtocol = 0,
638 .iInterface = STRING_DATA,
643 static struct usb_endpoint_descriptor
645 .bLength = USB_DT_ENDPOINT_SIZE,
646 .bDescriptorType = USB_DT_ENDPOINT,
648 .bEndpointAddress = USB_DIR_IN,
649 .bmAttributes = USB_ENDPOINT_XFER_BULK,
650 .wMaxPacketSize = __constant_cpu_to_le16(64),
653 static struct usb_endpoint_descriptor
655 .bLength = USB_DT_ENDPOINT_SIZE,
656 .bDescriptorType = USB_DT_ENDPOINT,
658 .bEndpointAddress = USB_DIR_OUT,
659 .bmAttributes = USB_ENDPOINT_XFER_BULK,
660 .wMaxPacketSize = __constant_cpu_to_le16(64),
663 static const struct usb_descriptor_header *fs_eth_function[11] = {
664 (struct usb_descriptor_header *) &otg_descriptor,
665 #ifdef CONFIG_USB_ETH_CDC
666 /* "cdc" mode descriptors */
667 (struct usb_descriptor_header *) &control_intf,
668 (struct usb_descriptor_header *) &header_desc,
669 (struct usb_descriptor_header *) &union_desc,
670 (struct usb_descriptor_header *) ðer_desc,
671 /* NOTE: status endpoint may need to be removed */
672 (struct usb_descriptor_header *) &fs_status_desc,
673 /* data interface, with altsetting */
674 (struct usb_descriptor_header *) &data_nop_intf,
675 (struct usb_descriptor_header *) &data_intf,
676 (struct usb_descriptor_header *) &fs_source_desc,
677 (struct usb_descriptor_header *) &fs_sink_desc,
679 #endif /* CONFIG_USB_ETH_CDC */
682 static inline void fs_subset_descriptors(void)
684 #ifdef CONFIG_USB_ETH_SUBSET
685 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
686 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
687 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
688 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
689 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
690 fs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
691 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
692 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
693 fs_eth_function[8] = NULL;
695 fs_eth_function[1] = NULL;
699 #ifdef CONFIG_USB_ETH_RNDIS
700 static const struct usb_descriptor_header *fs_rndis_function[] = {
701 (struct usb_descriptor_header *) &otg_descriptor,
702 /* control interface matches ACM, not Ethernet */
703 (struct usb_descriptor_header *) &rndis_control_intf,
704 (struct usb_descriptor_header *) &header_desc,
705 (struct usb_descriptor_header *) &call_mgmt_descriptor,
706 (struct usb_descriptor_header *) &acm_descriptor,
707 (struct usb_descriptor_header *) &union_desc,
708 (struct usb_descriptor_header *) &fs_status_desc,
709 /* data interface has no altsetting */
710 (struct usb_descriptor_header *) &rndis_data_intf,
711 (struct usb_descriptor_header *) &fs_source_desc,
712 (struct usb_descriptor_header *) &fs_sink_desc,
718 * usb 2.0 devices need to expose both high speed and full speed
719 * descriptors, unless they only run at full speed.
722 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
723 static struct usb_endpoint_descriptor
725 .bLength = USB_DT_ENDPOINT_SIZE,
726 .bDescriptorType = USB_DT_ENDPOINT,
728 .bmAttributes = USB_ENDPOINT_XFER_INT,
729 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
730 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
732 #endif /* CONFIG_USB_ETH_CDC */
734 static struct usb_endpoint_descriptor
736 .bLength = USB_DT_ENDPOINT_SIZE,
737 .bDescriptorType = USB_DT_ENDPOINT,
739 .bmAttributes = USB_ENDPOINT_XFER_BULK,
740 .wMaxPacketSize = __constant_cpu_to_le16(512),
743 static struct usb_endpoint_descriptor
745 .bLength = USB_DT_ENDPOINT_SIZE,
746 .bDescriptorType = USB_DT_ENDPOINT,
748 .bmAttributes = USB_ENDPOINT_XFER_BULK,
749 .wMaxPacketSize = __constant_cpu_to_le16(512),
752 static struct usb_qualifier_descriptor
754 .bLength = sizeof dev_qualifier,
755 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
757 .bcdUSB = __constant_cpu_to_le16(0x0200),
758 .bDeviceClass = USB_CLASS_COMM,
760 .bNumConfigurations = 1,
763 static const struct usb_descriptor_header *hs_eth_function[11] = {
764 (struct usb_descriptor_header *) &otg_descriptor,
765 #ifdef CONFIG_USB_ETH_CDC
766 /* "cdc" mode descriptors */
767 (struct usb_descriptor_header *) &control_intf,
768 (struct usb_descriptor_header *) &header_desc,
769 (struct usb_descriptor_header *) &union_desc,
770 (struct usb_descriptor_header *) ðer_desc,
771 /* NOTE: status endpoint may need to be removed */
772 (struct usb_descriptor_header *) &hs_status_desc,
773 /* data interface, with altsetting */
774 (struct usb_descriptor_header *) &data_nop_intf,
775 (struct usb_descriptor_header *) &data_intf,
776 (struct usb_descriptor_header *) &hs_source_desc,
777 (struct usb_descriptor_header *) &hs_sink_desc,
779 #endif /* CONFIG_USB_ETH_CDC */
782 static inline void hs_subset_descriptors(void)
784 #ifdef CONFIG_USB_ETH_SUBSET
785 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
786 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
787 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
788 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
789 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
790 hs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
791 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
792 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
793 hs_eth_function[8] = NULL;
795 hs_eth_function[1] = NULL;
799 #ifdef CONFIG_USB_ETH_RNDIS
800 static const struct usb_descriptor_header *hs_rndis_function[] = {
801 (struct usb_descriptor_header *) &otg_descriptor,
802 /* control interface matches ACM, not Ethernet */
803 (struct usb_descriptor_header *) &rndis_control_intf,
804 (struct usb_descriptor_header *) &header_desc,
805 (struct usb_descriptor_header *) &call_mgmt_descriptor,
806 (struct usb_descriptor_header *) &acm_descriptor,
807 (struct usb_descriptor_header *) &union_desc,
808 (struct usb_descriptor_header *) &hs_status_desc,
809 /* data interface has no altsetting */
810 (struct usb_descriptor_header *) &rndis_data_intf,
811 (struct usb_descriptor_header *) &hs_source_desc,
812 (struct usb_descriptor_header *) &hs_sink_desc,
818 /* maxpacket and other transfer characteristics vary by speed. */
819 static inline struct usb_endpoint_descriptor *
820 ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
821 struct usb_endpoint_descriptor *fs)
823 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
828 /*-------------------------------------------------------------------------*/
830 /* descriptors that are built on-demand */
832 static char manufacturer[50];
833 static char product_desc[40] = DRIVER_DESC;
834 static char serial_number[20];
836 /* address that the host will use ... usually assigned at random */
837 static char ethaddr[2 * ETH_ALEN + 1];
839 /* static strings, in UTF-8 */
840 static struct usb_string strings[] = {
841 { STRING_MANUFACTURER, manufacturer, },
842 { STRING_PRODUCT, product_desc, },
843 { STRING_SERIALNUMBER, serial_number, },
844 { STRING_DATA, "Ethernet Data", },
845 { STRING_ETHADDR, ethaddr, },
846 #ifdef CONFIG_USB_ETH_CDC
847 { STRING_CDC, "CDC Ethernet", },
848 { STRING_CONTROL, "CDC Communications Control", },
850 #ifdef CONFIG_USB_ETH_SUBSET
851 { STRING_SUBSET, "CDC Ethernet Subset", },
853 #ifdef CONFIG_USB_ETH_RNDIS
854 { STRING_RNDIS, "RNDIS", },
855 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
857 { } /* end of list */
860 static struct usb_gadget_strings stringtab = {
861 .language = 0x0409, /* en-us */
865 /*============================================================================*/
866 DEFINE_CACHE_ALIGN_BUFFER(u8, control_req, USB_BUFSIZ);
868 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
869 DEFINE_CACHE_ALIGN_BUFFER(u8, status_req, STATUS_BYTECOUNT);
872 /*============================================================================*/
875 * one config, two interfaces: control, data.
876 * complications: class descriptors, and an altsetting.
879 config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
882 const struct usb_config_descriptor *config;
883 const struct usb_descriptor_header **function;
886 if (gadget_is_dualspeed(g)) {
887 hs = (g->speed == USB_SPEED_HIGH);
888 if (type == USB_DT_OTHER_SPEED_CONFIG)
891 #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
893 if (index >= device_desc.bNumConfigurations)
896 #ifdef CONFIG_USB_ETH_RNDIS
898 * list the RNDIS config first, to make Microsoft's drivers
899 * happy. DOCSIS 1.0 needs this too.
901 if (device_desc.bNumConfigurations == 2 && index == 0) {
902 config = &rndis_config;
903 function = which_fn(rndis);
907 config = ð_config;
908 function = which_fn(eth);
911 /* for now, don't advertise srp-only devices */
915 len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
918 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
922 /*-------------------------------------------------------------------------*/
924 static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
925 static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
928 set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
931 struct usb_gadget *gadget = dev->gadget;
933 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
934 /* status endpoint used for RNDIS and (optionally) CDC */
935 if (!subset_active(dev) && dev->status_ep) {
936 dev->status = ep_desc(gadget, &hs_status_desc,
938 dev->status_ep->driver_data = dev;
940 result = usb_ep_enable(dev->status_ep, dev->status);
942 debug("enable %s --> %d\n",
943 dev->status_ep->name, result);
949 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
950 dev->in_ep->driver_data = dev;
952 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
953 dev->out_ep->driver_data = dev;
956 * With CDC, the host isn't allowed to use these two data
957 * endpoints in the default altsetting for the interface.
958 * so we don't activate them yet. Reset from SET_INTERFACE.
960 * Strictly speaking RNDIS should work the same: activation is
961 * a side effect of setting a packet filter. Deactivation is
962 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
964 if (!cdc_active(dev)) {
965 result = usb_ep_enable(dev->in_ep, dev->in);
967 debug("enable %s --> %d\n",
968 dev->in_ep->name, result);
972 result = usb_ep_enable(dev->out_ep, dev->out);
974 debug("enable %s --> %d\n",
975 dev->out_ep->name, result);
982 result = alloc_requests(dev, qlen(gadget), gfp_flags);
984 /* on error, disable any endpoints */
986 if (!subset_active(dev) && dev->status_ep)
987 (void) usb_ep_disable(dev->status_ep);
989 (void) usb_ep_disable(dev->in_ep);
990 (void) usb_ep_disable(dev->out_ep);
993 } else if (!cdc_active(dev)) {
995 * activate non-CDC configs right away
996 * this isn't strictly according to the RNDIS spec
998 eth_start(dev, GFP_ATOMIC);
1001 /* caller is responsible for cleanup on error */
1005 static void eth_reset_config(struct eth_dev *dev)
1007 if (dev->config == 0)
1010 debug("%s\n", __func__);
1012 rndis_uninit(dev->rndis_config);
1015 * disable endpoints, forcing (synchronous) completion of
1016 * pending i/o. then free the requests.
1020 usb_ep_disable(dev->in_ep);
1022 usb_ep_free_request(dev->in_ep, dev->tx_req);
1027 usb_ep_disable(dev->out_ep);
1029 usb_ep_free_request(dev->out_ep, dev->rx_req);
1034 usb_ep_disable(dev->status_ep);
1037 dev->cdc_filter = 0;
1042 * change our operational config. must agree with the code
1043 * that returns config descriptors, and altsetting code.
1045 static int eth_set_config(struct eth_dev *dev, unsigned number,
1049 struct usb_gadget *gadget = dev->gadget;
1051 eth_reset_config(dev);
1054 case DEV_CONFIG_VALUE:
1055 result = set_ether_config(dev, gfp_flags);
1057 #ifdef CONFIG_USB_ETH_RNDIS
1058 case DEV_RNDIS_CONFIG_VALUE:
1060 result = set_ether_config(dev, gfp_flags);
1072 eth_reset_config(dev);
1073 usb_gadget_vbus_draw(dev->gadget,
1074 gadget_is_otg(dev->gadget) ? 8 : 100);
1079 power = 2 * eth_config.bMaxPower;
1080 usb_gadget_vbus_draw(dev->gadget, power);
1082 switch (gadget->speed) {
1083 case USB_SPEED_FULL:
1084 speed = "full"; break;
1085 #ifdef CONFIG_USB_GADGET_DUALSPEED
1086 case USB_SPEED_HIGH:
1087 speed = "high"; break;
1093 dev->config = number;
1094 printf("%s speed config #%d: %d mA, %s, using %s\n",
1095 speed, number, power, driver_desc,
1100 : "CDC Ethernet Subset"));
1105 /*-------------------------------------------------------------------------*/
1107 #ifdef CONFIG_USB_ETH_CDC
1110 * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
1111 * only to notify the host about link status changes (which we support) or
1112 * report completion of some encapsulated command (as used in RNDIS). Since
1113 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1114 * command mechanism; and only one status request is ever queued.
1116 static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
1118 struct usb_cdc_notification *event = req->buf;
1119 int value = req->status;
1120 struct eth_dev *dev = ep->driver_data;
1122 /* issue the second notification if host reads the first */
1123 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1125 __le32 *data = req->buf + sizeof *event;
1127 event->bmRequestType = 0xA1;
1128 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
1129 event->wValue = __constant_cpu_to_le16(0);
1130 event->wIndex = __constant_cpu_to_le16(1);
1131 event->wLength = __constant_cpu_to_le16(8);
1133 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1134 data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
1136 req->length = STATUS_BYTECOUNT;
1137 value = usb_ep_queue(ep, req, GFP_ATOMIC);
1138 debug("send SPEED_CHANGE --> %d\n", value);
1141 } else if (value != -ECONNRESET) {
1142 debug("event %02x --> %d\n",
1143 event->bNotificationType, value);
1144 if (event->bNotificationType ==
1145 USB_CDC_NOTIFY_SPEED_CHANGE) {
1146 dev->network_started = 1;
1147 printf("USB network up!\n");
1150 req->context = NULL;
1153 static void issue_start_status(struct eth_dev *dev)
1155 struct usb_request *req = dev->stat_req;
1156 struct usb_cdc_notification *event;
1162 * FIXME ugly idiom, maybe we'd be better with just
1163 * a "cancel the whole queue" primitive since any
1164 * unlink-one primitive has way too many error modes.
1165 * here, we "know" toggle is already clear...
1167 * FIXME iff req->context != null just dequeue it
1169 usb_ep_disable(dev->status_ep);
1170 usb_ep_enable(dev->status_ep, dev->status);
1173 * 3.8.1 says to issue first NETWORK_CONNECTION, then
1174 * a SPEED_CHANGE. could be useful in some configs.
1177 event->bmRequestType = 0xA1;
1178 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
1179 event->wValue = __constant_cpu_to_le16(1); /* connected */
1180 event->wIndex = __constant_cpu_to_le16(1);
1183 req->length = sizeof *event;
1184 req->complete = eth_status_complete;
1187 value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
1189 debug("status buf queue --> %d\n", value);
1194 /*-------------------------------------------------------------------------*/
1196 static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
1198 if (req->status || req->actual != req->length)
1199 debug("setup complete --> %d, %d/%d\n",
1200 req->status, req->actual, req->length);
1203 #ifdef CONFIG_USB_ETH_RNDIS
1205 static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
1207 if (req->status || req->actual != req->length)
1208 debug("rndis response complete --> %d, %d/%d\n",
1209 req->status, req->actual, req->length);
1211 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1214 static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
1216 struct eth_dev *dev = ep->driver_data;
1219 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1220 status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
1222 pr_err("%s: rndis parse error %d", __func__, status);
1228 * The setup() callback implements all the ep0 functionality that's not
1229 * handled lower down. CDC has a number of less-common features:
1231 * - two interfaces: control, and ethernet data
1232 * - Ethernet data interface has two altsettings: default, and active
1233 * - class-specific descriptors for the control interface
1234 * - class-specific control requests
1237 eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1239 struct eth_dev *dev = get_gadget_data(gadget);
1240 struct usb_request *req = dev->req;
1241 int value = -EOPNOTSUPP;
1242 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1243 u16 wValue = le16_to_cpu(ctrl->wValue);
1244 u16 wLength = le16_to_cpu(ctrl->wLength);
1247 * descriptors just go into the pre-allocated ep0 buffer,
1248 * while config change events may enable network traffic.
1251 debug("%s\n", __func__);
1253 req->complete = eth_setup_complete;
1254 switch (ctrl->bRequest) {
1256 case USB_REQ_GET_DESCRIPTOR:
1257 if (ctrl->bRequestType != USB_DIR_IN)
1259 switch (wValue >> 8) {
1262 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1263 value = min(wLength, (u16) sizeof device_desc);
1264 memcpy(req->buf, &device_desc, value);
1266 case USB_DT_DEVICE_QUALIFIER:
1267 if (!gadget_is_dualspeed(gadget))
1269 value = min(wLength, (u16) sizeof dev_qualifier);
1270 memcpy(req->buf, &dev_qualifier, value);
1273 case USB_DT_OTHER_SPEED_CONFIG:
1274 if (!gadget_is_dualspeed(gadget))
1278 value = config_buf(gadget, req->buf,
1281 gadget_is_otg(gadget));
1283 value = min(wLength, (u16) value);
1287 value = usb_gadget_get_string(&stringtab,
1288 wValue & 0xff, req->buf);
1291 value = min(wLength, (u16) value);
1297 case USB_REQ_SET_CONFIGURATION:
1298 if (ctrl->bRequestType != 0)
1300 if (gadget->a_hnp_support)
1301 debug("HNP available\n");
1302 else if (gadget->a_alt_hnp_support)
1303 debug("HNP needs a different root port\n");
1304 value = eth_set_config(dev, wValue, GFP_ATOMIC);
1306 case USB_REQ_GET_CONFIGURATION:
1307 if (ctrl->bRequestType != USB_DIR_IN)
1309 *(u8 *)req->buf = dev->config;
1310 value = min(wLength, (u16) 1);
1313 case USB_REQ_SET_INTERFACE:
1314 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1318 if (!cdc_active(dev) && wIndex != 0)
1321 #ifdef CONFIG_USB_ETH_CDC
1323 case 0: /* control/master intf */
1327 usb_ep_disable(dev->status_ep);
1328 usb_ep_enable(dev->status_ep, dev->status);
1333 case 1: /* data intf */
1336 usb_ep_disable(dev->in_ep);
1337 usb_ep_disable(dev->out_ep);
1340 * CDC requires the data transfers not be done from
1341 * the default interface setting ... also, setting
1342 * the non-default interface resets filters etc.
1345 if (!cdc_active(dev))
1347 usb_ep_enable(dev->in_ep, dev->in);
1348 usb_ep_enable(dev->out_ep, dev->out);
1349 dev->cdc_filter = DEFAULT_FILTER;
1351 issue_start_status(dev);
1352 eth_start(dev, GFP_ATOMIC);
1359 * FIXME this is wrong, as is the assumption that
1360 * all non-PXA hardware talks real CDC ...
1362 debug("set_interface ignored!\n");
1363 #endif /* CONFIG_USB_ETH_CDC */
1365 case USB_REQ_GET_INTERFACE:
1366 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1370 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
1373 /* for CDC, iff carrier is on, data interface is active. */
1374 if (rndis_active(dev) || wIndex != 1)
1375 *(u8 *)req->buf = 0;
1377 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1378 /* carrier always ok ...*/
1379 *(u8 *)req->buf = 1 ;
1381 value = min(wLength, (u16) 1);
1384 #ifdef CONFIG_USB_ETH_CDC
1385 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
1387 * see 6.2.30: no data, wIndex = interface,
1388 * wValue = packet filter bitmap
1390 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1395 debug("packet filter %02x\n", wValue);
1396 dev->cdc_filter = wValue;
1402 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1403 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1404 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1405 * case USB_CDC_GET_ETHERNET_STATISTIC:
1408 #endif /* CONFIG_USB_ETH_CDC */
1410 #ifdef CONFIG_USB_ETH_RNDIS
1412 * RNDIS uses the CDC command encapsulation mechanism to implement
1413 * an RPC scheme, with much getting/setting of attributes by OID.
1415 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1416 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1417 || !rndis_active(dev)
1418 || wLength > USB_BUFSIZ
1420 || rndis_control_intf.bInterfaceNumber
1423 /* read the request, then process it */
1425 req->complete = rndis_command_complete;
1426 /* later, rndis_control_ack () sends a notification */
1429 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1430 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1431 == ctrl->bRequestType
1432 && rndis_active(dev)
1433 /* && wLength >= 0x0400 */
1435 && rndis_control_intf.bInterfaceNumber
1440 /* return the result */
1441 buf = rndis_get_next_response(dev->rndis_config, &n);
1443 memcpy(req->buf, buf, n);
1444 req->complete = rndis_response_complete;
1445 rndis_free_response(dev->rndis_config, buf);
1448 /* else stalls ... spec says to avoid that */
1454 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
1455 ctrl->bRequestType, ctrl->bRequest,
1456 wValue, wIndex, wLength);
1459 /* respond with data transfer before status phase? */
1461 debug("respond with data transfer before status phase\n");
1462 req->length = value;
1463 req->zero = value < wLength
1464 && (value % gadget->ep0->maxpacket) == 0;
1465 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1467 debug("ep_queue --> %d\n", value);
1469 eth_setup_complete(gadget->ep0, req);
1473 /* host either stalls (value < 0) or reports success */
1477 /*-------------------------------------------------------------------------*/
1479 static void rx_complete(struct usb_ep *ep, struct usb_request *req);
1481 static int rx_submit(struct eth_dev *dev, struct usb_request *req,
1484 int retval = -ENOMEM;
1488 * Padding up to RX_EXTRA handles minor disagreements with host.
1489 * Normally we use the USB "terminate on short read" convention;
1490 * so allow up to (N*maxpacket), since that memory is normally
1491 * already allocated. Some hardware doesn't deal well with short
1492 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1493 * byte off the end (to force hardware errors on overflow).
1495 * RNDIS uses internal framing, and explicitly allows senders to
1496 * pad to end-of-packet. That's potentially nice for speed,
1497 * but means receivers can't recover synch on their own.
1500 debug("%s\n", __func__);
1504 size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1505 size += dev->out_ep->maxpacket - 1;
1506 if (rndis_active(dev))
1507 size += sizeof(struct rndis_packet_msg_type);
1508 size -= size % dev->out_ep->maxpacket;
1511 * Some platforms perform better when IP packets are aligned,
1512 * but on at least one, checksumming fails otherwise. Note:
1513 * RNDIS headers involve variable numbers of LE32 values.
1516 req->buf = (u8 *)net_rx_packets[0];
1518 req->complete = rx_complete;
1520 retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
1523 pr_err("rx submit --> %d", retval);
1528 static void rx_complete(struct usb_ep *ep, struct usb_request *req)
1530 struct eth_dev *dev = ep->driver_data;
1532 debug("%s: status %d\n", __func__, req->status);
1533 switch (req->status) {
1534 /* normal completion */
1536 if (rndis_active(dev)) {
1537 /* we know MaxPacketsPerTransfer == 1 here */
1538 int length = rndis_rm_hdr(req->buf, req->actual);
1541 req->length -= length;
1542 req->actual -= length;
1544 if (req->actual < ETH_HLEN || PKTSIZE_ALIGN < req->actual) {
1546 dev->stats.rx_errors++;
1547 dev->stats.rx_length_errors++;
1548 debug("rx length %d\n", req->length);
1552 dev->stats.rx_packets++;
1553 dev->stats.rx_bytes += req->length;
1556 /* software-driven interface shutdown */
1557 case -ECONNRESET: /* unlink */
1558 case -ESHUTDOWN: /* disconnect etc */
1559 /* for hardware automagic (such as pxa) */
1560 case -ECONNABORTED: /* endpoint reset */
1565 dev->stats.rx_over_errors++;
1568 dev->stats.rx_errors++;
1572 packet_received = 1;
1575 static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
1578 dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
1583 dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
1591 usb_ep_free_request(dev->in_ep, dev->tx_req);
1593 pr_err("can't alloc requests");
1597 static void tx_complete(struct usb_ep *ep, struct usb_request *req)
1599 struct eth_dev *dev = ep->driver_data;
1601 debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
1602 switch (req->status) {
1604 dev->stats.tx_errors++;
1605 debug("tx err %d\n", req->status);
1607 case -ECONNRESET: /* unlink */
1608 case -ESHUTDOWN: /* disconnect etc */
1611 dev->stats.tx_bytes += req->length;
1613 dev->stats.tx_packets++;
1618 static inline int eth_is_promisc(struct eth_dev *dev)
1620 /* no filters for the CDC subset; always promisc */
1621 if (subset_active(dev))
1623 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1627 static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1629 struct eth_dev *dev = netdev_priv(net);
1630 int length = skb->len;
1632 struct usb_request *req = NULL;
1633 unsigned long flags;
1635 /* apply outgoing CDC or RNDIS filters */
1636 if (!eth_is_promisc (dev)) {
1637 u8 *dest = skb->data;
1639 if (is_multicast_ethaddr(dest)) {
1642 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1643 * SET_ETHERNET_MULTICAST_FILTERS requests
1645 if (is_broadcast_ethaddr(dest))
1646 type = USB_CDC_PACKET_TYPE_BROADCAST;
1648 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1649 if (!(dev->cdc_filter & type)) {
1650 dev_kfree_skb_any (skb);
1654 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1657 spin_lock_irqsave(&dev->req_lock, flags);
1659 * this freelist can be empty if an interrupt triggered disconnect()
1660 * and reconfigured the gadget (shutting down this queue) after the
1661 * network stack decided to xmit but before we got the spinlock.
1663 if (list_empty(&dev->tx_reqs)) {
1664 spin_unlock_irqrestore(&dev->req_lock, flags);
1668 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1669 list_del (&req->list);
1671 /* temporarily stop TX queue when the freelist empties */
1672 if (list_empty (&dev->tx_reqs))
1673 netif_stop_queue (net);
1674 spin_unlock_irqrestore(&dev->req_lock, flags);
1676 /* no buffer copies needed, unless the network stack did it
1677 * or the hardware can't use skb buffers.
1678 * or there's not enough space for any RNDIS headers we need
1680 if (rndis_active(dev)) {
1681 struct sk_buff *skb_rndis;
1683 skb_rndis = skb_realloc_headroom (skb,
1684 sizeof (struct rndis_packet_msg_type));
1688 dev_kfree_skb_any (skb);
1690 rndis_add_hdr (skb);
1693 req->buf = skb->data;
1695 req->complete = tx_complete;
1697 /* use zlp framing on tx for strict CDC-Ether conformance,
1698 * though any robust network rx path ignores extra padding.
1699 * and some hardware doesn't like to write zlps.
1702 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1705 req->length = length;
1707 /* throttle highspeed IRQ rate back slightly */
1708 if (gadget_is_dualspeed(dev->gadget))
1709 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1710 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1713 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1716 DEBUG (dev, "tx queue err %d\n", retval);
1719 net->trans_start = jiffies;
1720 atomic_inc (&dev->tx_qlen);
1725 dev->stats.tx_dropped++;
1726 dev_kfree_skb_any (skb);
1727 spin_lock_irqsave(&dev->req_lock, flags);
1728 if (list_empty (&dev->tx_reqs))
1729 netif_start_queue (net);
1730 list_add (&req->list, &dev->tx_reqs);
1731 spin_unlock_irqrestore(&dev->req_lock, flags);
1736 /*-------------------------------------------------------------------------*/
1739 static void eth_unbind(struct usb_gadget *gadget)
1741 struct eth_dev *dev = get_gadget_data(gadget);
1743 debug("%s...\n", __func__);
1744 rndis_deregister(dev->rndis_config);
1747 /* we've already been disconnected ... no i/o is active */
1749 usb_ep_free_request(gadget->ep0, dev->req);
1752 if (dev->stat_req) {
1753 usb_ep_free_request(dev->status_ep, dev->stat_req);
1754 dev->stat_req = NULL;
1758 usb_ep_free_request(dev->in_ep, dev->tx_req);
1763 usb_ep_free_request(dev->out_ep, dev->rx_req);
1767 /* unregister_netdev (dev->net);*/
1768 /* free_netdev(dev->net);*/
1771 set_gadget_data(gadget, NULL);
1774 static void eth_disconnect(struct usb_gadget *gadget)
1776 eth_reset_config(get_gadget_data(gadget));
1777 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1780 static void eth_suspend(struct usb_gadget *gadget)
1785 static void eth_resume(struct usb_gadget *gadget)
1790 /*-------------------------------------------------------------------------*/
1792 #ifdef CONFIG_USB_ETH_RNDIS
1795 * The interrupt endpoint is used in RNDIS to notify the host when messages
1796 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1797 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1798 * REMOTE_NDIS_KEEPALIVE_MSG.
1800 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1801 * normally just one notification will be queued.
1804 static void rndis_control_ack_complete(struct usb_ep *ep,
1805 struct usb_request *req)
1807 struct eth_dev *dev = ep->driver_data;
1809 debug("%s...\n", __func__);
1810 if (req->status || req->actual != req->length)
1811 debug("rndis control ack complete --> %d, %d/%d\n",
1812 req->status, req->actual, req->length);
1814 if (!dev->network_started) {
1815 if (rndis_get_state(dev->rndis_config)
1816 == RNDIS_DATA_INITIALIZED) {
1817 dev->network_started = 1;
1818 printf("USB RNDIS network up!\n");
1822 req->context = NULL;
1824 if (req != dev->stat_req)
1825 usb_ep_free_request(ep, req);
1828 static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
1830 #ifndef CONFIG_DM_ETH
1831 static int rndis_control_ack(struct eth_device *net)
1833 static int rndis_control_ack(struct udevice *net)
1836 struct ether_priv *priv;
1837 struct eth_dev *dev;
1839 struct usb_request *resp;
1841 #ifndef CONFIG_DM_ETH
1842 priv = (struct ether_priv *)net->priv;
1844 priv = dev_get_priv(net);
1846 dev = &priv->ethdev;
1847 resp = dev->stat_req;
1849 /* in case RNDIS calls this after disconnect */
1851 debug("status ENODEV\n");
1855 /* in case queue length > 1 */
1856 if (resp->context) {
1857 resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
1860 resp->buf = rndis_resp_buf;
1864 * Send RNDIS RESPONSE_AVAILABLE notification;
1865 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1868 resp->complete = rndis_control_ack_complete;
1869 resp->context = dev;
1871 *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
1872 *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
1874 length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
1877 rndis_control_ack_complete(dev->status_ep, resp);
1885 #define rndis_control_ack NULL
1889 static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
1891 if (rndis_active(dev)) {
1892 rndis_set_param_medium(dev->rndis_config,
1894 BITRATE(dev->gadget)/100);
1895 rndis_signal_connect(dev->rndis_config);
1899 static int eth_stop(struct eth_dev *dev)
1901 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1903 unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1906 if (rndis_active(dev)) {
1907 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
1908 rndis_signal_disconnect(dev->rndis_config);
1910 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1911 /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1913 while (get_timer(ts) < timeout)
1914 usb_gadget_handle_interrupts(0);
1917 rndis_uninit(dev->rndis_config);
1924 /*-------------------------------------------------------------------------*/
1926 static int is_eth_addr_valid(char *str)
1928 if (strlen(str) == 17) {
1933 /* see if it looks like an ethernet address */
1937 for (i = 0; i < 6; i++) {
1938 char term = (i == 5 ? '\0' : ':');
1940 ea[i] = simple_strtol(p, &q, 16);
1942 if ((q - p) != 2 || *q++ != term)
1948 /* Now check the contents. */
1949 return is_valid_ethaddr(ea);
1954 static u8 nibble(unsigned char c)
1956 if (likely(isdigit(c)))
1959 if (likely(isxdigit(c)))
1960 return 10 + c - 'A';
1964 static int get_ether_addr(const char *str, u8 *dev_addr)
1969 for (i = 0; i < 6; i++) {
1972 if ((*str == '.') || (*str == ':'))
1974 num = nibble(*str++) << 4;
1975 num |= (nibble(*str++));
1978 if (is_valid_ethaddr(dev_addr))
1984 static int eth_bind(struct usb_gadget *gadget)
1986 struct eth_dev *dev = &l_priv->ethdev;
1987 u8 cdc = 1, zlp = 1, rndis = 1;
1988 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
1989 int status = -ENOMEM;
1992 #ifdef CONFIG_DM_ETH
1993 struct eth_pdata *pdata = dev_get_plat(l_priv->netdev);
1996 /* these flags are only ever cleared; compiler take note */
1997 #ifndef CONFIG_USB_ETH_CDC
2000 #ifndef CONFIG_USB_ETH_RNDIS
2004 * Because most host side USB stacks handle CDC Ethernet, that
2005 * standard protocol is _strongly_ preferred for interop purposes.
2006 * (By everyone except Microsoft.)
2008 if (gadget_is_musbhdrc(gadget)) {
2009 /* reduce tx dma overhead by avoiding special cases */
2011 } else if (gadget_is_sh(gadget)) {
2012 /* sh doesn't support multiple interfaces or configs */
2017 gcnum = usb_gadget_controller_number(gadget);
2019 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
2022 * can't assume CDC works. don't want to default to
2023 * anything less functional on CDC-capable hardware,
2024 * so we fail in this case.
2026 pr_err("controller '%s' not recognized",
2032 * If there's an RNDIS configuration, that's what Windows wants to
2033 * be using ... so use these product IDs here and in the "linux.inf"
2034 * needed to install MSFT drivers. Current Linux kernels will use
2035 * the second configuration if it's CDC Ethernet, and need some help
2036 * to choose the right configuration otherwise.
2039 #if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
2040 device_desc.idVendor =
2041 __constant_cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
2042 device_desc.idProduct =
2043 __constant_cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
2045 device_desc.idVendor =
2046 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2047 device_desc.idProduct =
2048 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2050 sprintf(product_desc, "RNDIS/%s", driver_desc);
2053 * CDC subset ... recognized by Linux since 2.4.10, but Windows
2054 * drivers aren't widely available. (That may be improved by
2055 * supporting one submode of the "SAFE" variant of MDLM.)
2058 #if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
2059 device_desc.idVendor = cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
2060 device_desc.idProduct = cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
2063 device_desc.idVendor =
2064 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2065 device_desc.idProduct =
2066 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2070 /* support optional vendor/distro customization */
2072 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2074 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
2076 strlcpy(product_desc, iProduct, sizeof product_desc);
2077 if (iSerialNumber) {
2078 device_desc.iSerialNumber = STRING_SERIALNUMBER,
2079 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
2082 /* all we really need is bulk IN/OUT */
2083 usb_ep_autoconfig_reset(gadget);
2084 in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
2087 pr_err("can't autoconfigure on %s\n",
2091 in_ep->driver_data = in_ep; /* claim */
2093 out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
2096 out_ep->driver_data = out_ep; /* claim */
2098 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2100 * CDC Ethernet control interface doesn't require a status endpoint.
2101 * Since some hosts expect one, try to allocate one anyway.
2104 status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
2106 status_ep->driver_data = status_ep; /* claim */
2108 pr_err("can't run RNDIS on %s", gadget->name);
2110 #ifdef CONFIG_USB_ETH_CDC
2112 control_intf.bNumEndpoints = 0;
2113 /* FIXME remove endpoint from descriptor list */
2119 /* one config: cdc, else minimal subset */
2121 eth_config.bNumInterfaces = 1;
2122 eth_config.iConfiguration = STRING_SUBSET;
2125 * use functions to set these up, in case we're built to work
2126 * with multiple controllers and must override CDC Ethernet.
2128 fs_subset_descriptors();
2129 hs_subset_descriptors();
2132 usb_gadget_set_selfpowered(gadget);
2134 /* For now RNDIS is always a second config */
2136 device_desc.bNumConfigurations = 2;
2138 if (gadget_is_dualspeed(gadget)) {
2140 dev_qualifier.bNumConfigurations = 2;
2142 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2144 /* assumes ep0 uses the same value for both speeds ... */
2145 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2147 /* and that all endpoints are dual-speed */
2148 hs_source_desc.bEndpointAddress =
2149 fs_source_desc.bEndpointAddress;
2150 hs_sink_desc.bEndpointAddress =
2151 fs_sink_desc.bEndpointAddress;
2152 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2154 hs_status_desc.bEndpointAddress =
2155 fs_status_desc.bEndpointAddress;
2159 if (gadget_is_otg(gadget)) {
2160 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2161 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2162 eth_config.bMaxPower = 4;
2163 #ifdef CONFIG_USB_ETH_RNDIS
2164 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2165 rndis_config.bMaxPower = 4;
2170 /* network device setup */
2171 #ifndef CONFIG_DM_ETH
2172 dev->net = &l_priv->netdev;
2174 dev->net = l_priv->netdev;
2181 dev->out_ep = out_ep;
2182 dev->status_ep = status_ep;
2184 memset(tmp, 0, sizeof(tmp));
2186 * Module params for these addresses should come from ID proms.
2187 * The host side address is used with CDC and RNDIS, and commonly
2188 * ends up in a persistent config database. It's not clear if
2189 * host side code for the SAFE thing cares -- its original BLAN
2190 * thing didn't, Sharp never assigned those addresses on Zaurii.
2192 #ifndef CONFIG_DM_ETH
2193 get_ether_addr(dev_addr, dev->net->enetaddr);
2194 memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
2196 get_ether_addr(dev_addr, pdata->enetaddr);
2197 memcpy(tmp, pdata->enetaddr, sizeof(pdata->enetaddr));
2200 get_ether_addr(host_addr, dev->host_mac);
2202 sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
2203 dev->host_mac[0], dev->host_mac[1],
2204 dev->host_mac[2], dev->host_mac[3],
2205 dev->host_mac[4], dev->host_mac[5]);
2208 status = rndis_init();
2210 pr_err("can't init RNDIS, %d", status);
2216 * use PKTSIZE (or aligned... from u-boot) and set
2217 * wMaxSegmentSize accordingly
2219 dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
2221 /* preallocate control message data and buffer */
2222 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
2225 dev->req->buf = control_req;
2226 dev->req->complete = eth_setup_complete;
2228 /* ... and maybe likewise for status transfer */
2229 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2230 if (dev->status_ep) {
2231 dev->stat_req = usb_ep_alloc_request(dev->status_ep,
2233 if (!dev->stat_req) {
2234 usb_ep_free_request(dev->status_ep, dev->req);
2238 dev->stat_req->buf = status_req;
2239 dev->stat_req->context = NULL;
2243 /* finish hookup to lower layer ... */
2244 dev->gadget = gadget;
2245 set_gadget_data(gadget, dev);
2246 gadget->ep0->driver_data = dev;
2249 * two kinds of host-initiated state changes:
2250 * - iff DATA transfer is active, carrier is "on"
2251 * - tx queueing enabled if open *and* carrier is "on"
2254 printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
2255 out_ep->name, in_ep->name,
2256 status_ep ? " STATUS " : "",
2257 status_ep ? status_ep->name : ""
2259 #ifndef CONFIG_DM_ETH
2260 printf("MAC %pM\n", dev->net->enetaddr);
2262 printf("MAC %pM\n", pdata->enetaddr);
2266 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2267 dev->host_mac[0], dev->host_mac[1],
2268 dev->host_mac[2], dev->host_mac[3],
2269 dev->host_mac[4], dev->host_mac[5]);
2274 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2276 dev->rndis_config = rndis_register(rndis_control_ack);
2277 if (dev->rndis_config < 0) {
2280 debug("RNDIS setup failed\n");
2285 /* these set up a lot of the OIDs that RNDIS needs */
2286 rndis_set_host_mac(dev->rndis_config, dev->host_mac);
2287 if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
2288 &dev->stats, &dev->cdc_filter))
2290 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2293 if (rndis_set_param_medium(dev->rndis_config,
2294 NDIS_MEDIUM_802_3, 0))
2296 printf("RNDIS ready\n");
2301 pr_err("%s failed, status = %d", __func__, status);
2306 /*-------------------------------------------------------------------------*/
2307 static void _usb_eth_halt(struct ether_priv *priv);
2309 static int _usb_eth_init(struct ether_priv *priv)
2311 struct eth_dev *dev = &priv->ethdev;
2312 struct usb_gadget *gadget;
2315 unsigned long timeout = USB_CONNECT_TIMEOUT;
2317 ret = usb_gadget_initialize(0);
2321 /* Configure default mac-addresses for the USB ethernet device */
2322 #ifdef CONFIG_USBNET_DEV_ADDR
2323 strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
2325 #ifdef CONFIG_USBNET_HOST_ADDR
2326 strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
2328 /* Check if the user overruled the MAC addresses */
2329 if (env_get("usbnet_devaddr"))
2330 strlcpy(dev_addr, env_get("usbnet_devaddr"),
2333 if (env_get("usbnet_hostaddr"))
2334 strlcpy(host_addr, env_get("usbnet_hostaddr"),
2337 if (!is_eth_addr_valid(dev_addr)) {
2338 pr_err("Need valid 'usbnet_devaddr' to be set");
2341 if (!is_eth_addr_valid(host_addr)) {
2342 pr_err("Need valid 'usbnet_hostaddr' to be set");
2346 priv->eth_driver.speed = DEVSPEED;
2347 priv->eth_driver.bind = eth_bind;
2348 priv->eth_driver.unbind = eth_unbind;
2349 priv->eth_driver.setup = eth_setup;
2350 priv->eth_driver.reset = eth_disconnect;
2351 priv->eth_driver.disconnect = eth_disconnect;
2352 priv->eth_driver.suspend = eth_suspend;
2353 priv->eth_driver.resume = eth_resume;
2354 if (usb_gadget_register_driver(&priv->eth_driver) < 0)
2357 dev->network_started = 0;
2359 packet_received = 0;
2362 gadget = dev->gadget;
2363 usb_gadget_connect(gadget);
2365 if (env_get("cdc_connect_timeout"))
2366 timeout = dectoul(env_get("cdc_connect_timeout"), NULL) * CONFIG_SYS_HZ;
2368 while (!dev->network_started) {
2369 /* Handle control-c and timeouts */
2370 if (ctrlc() || (get_timer(ts) > timeout)) {
2371 pr_err("The remote end did not respond in time.");
2374 usb_gadget_handle_interrupts(0);
2377 packet_received = 0;
2378 rx_submit(dev, dev->rx_req, 0);
2381 _usb_eth_halt(priv);
2385 static int _usb_eth_send(struct ether_priv *priv, void *packet, int length)
2388 void *rndis_pkt = NULL;
2389 struct eth_dev *dev = &priv->ethdev;
2390 struct usb_request *req = dev->tx_req;
2392 unsigned long timeout = USB_CONNECT_TIMEOUT;
2394 debug("%s:...\n", __func__);
2396 /* new buffer is needed to include RNDIS header */
2397 if (rndis_active(dev)) {
2398 rndis_pkt = malloc(length +
2399 sizeof(struct rndis_packet_msg_type));
2401 pr_err("No memory to alloc RNDIS packet");
2404 rndis_add_hdr(rndis_pkt, length);
2405 memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
2408 length += sizeof(struct rndis_packet_msg_type);
2411 req->context = NULL;
2412 req->complete = tx_complete;
2415 * use zlp framing on tx for strict CDC-Ether conformance,
2416 * though any robust network rx path ignores extra padding.
2417 * and some hardware doesn't like to write zlps.
2420 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2423 req->length = length;
2425 /* throttle highspeed IRQ rate back slightly */
2426 if (gadget_is_dualspeed(dev->gadget))
2427 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2428 ? ((dev->tx_qlen % qmult) != 0) : 0;
2434 retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
2437 debug("%s: packet queued\n", __func__);
2438 while (!packet_sent) {
2439 if (get_timer(ts) > timeout) {
2440 printf("timeout sending packets to usb ethernet\n");
2443 usb_gadget_handle_interrupts(0);
2449 dev->stats.tx_dropped++;
2453 static int _usb_eth_recv(struct ether_priv *priv)
2455 usb_gadget_handle_interrupts(0);
2460 static void _usb_eth_halt(struct ether_priv *priv)
2462 struct eth_dev *dev = &priv->ethdev;
2464 /* If the gadget not registered, simple return */
2469 * Some USB controllers may need additional deinitialization here
2470 * before dropping pull-up (also due to hardware issues).
2471 * For example: unhandled interrupt with status stage started may
2472 * bring the controller to fully broken state (until board reset).
2473 * There are some variants to debug and fix such cases:
2474 * 1) In the case of RNDIS connection eth_stop can perform additional
2475 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2476 * 2) 'pullup' callback in your UDC driver can be improved to perform
2477 * this deinitialization.
2481 usb_gadget_disconnect(dev->gadget);
2483 /* Clear pending interrupt */
2484 if (dev->network_started) {
2485 usb_gadget_handle_interrupts(0);
2486 dev->network_started = 0;
2489 usb_gadget_unregister_driver(&priv->eth_driver);
2490 usb_gadget_release(0);
2493 #ifndef CONFIG_DM_ETH
2494 static int usb_eth_init(struct eth_device *netdev, struct bd_info *bd)
2496 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2498 return _usb_eth_init(priv);
2501 static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
2503 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2505 return _usb_eth_send(priv, packet, length);
2508 static int usb_eth_recv(struct eth_device *netdev)
2510 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2511 struct eth_dev *dev = &priv->ethdev;
2514 ret = _usb_eth_recv(priv);
2516 pr_err("error packet receive\n");
2520 if (!packet_received)
2524 net_process_received_packet(net_rx_packets[0],
2525 dev->rx_req->length);
2527 pr_err("dev->rx_req invalid");
2529 packet_received = 0;
2530 rx_submit(dev, dev->rx_req, 0);
2535 void usb_eth_halt(struct eth_device *netdev)
2537 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2539 _usb_eth_halt(priv);
2542 int usb_eth_initialize(struct bd_info *bi)
2544 struct eth_device *netdev = &l_priv->netdev;
2546 strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
2548 netdev->init = usb_eth_init;
2549 netdev->send = usb_eth_send;
2550 netdev->recv = usb_eth_recv;
2551 netdev->halt = usb_eth_halt;
2552 netdev->priv = l_priv;
2554 eth_register(netdev);
2558 static int usb_eth_start(struct udevice *dev)
2560 struct ether_priv *priv = dev_get_priv(dev);
2562 return _usb_eth_init(priv);
2565 static int usb_eth_send(struct udevice *dev, void *packet, int length)
2567 struct ether_priv *priv = dev_get_priv(dev);
2569 return _usb_eth_send(priv, packet, length);
2572 static int usb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
2574 struct ether_priv *priv = dev_get_priv(dev);
2575 struct eth_dev *ethdev = &priv->ethdev;
2578 ret = _usb_eth_recv(priv);
2580 pr_err("error packet receive\n");
2584 if (packet_received) {
2585 if (ethdev->rx_req) {
2586 *packetp = (uchar *)net_rx_packets[0];
2587 return ethdev->rx_req->length;
2589 pr_err("dev->rx_req invalid");
2597 static int usb_eth_free_pkt(struct udevice *dev, uchar *packet,
2600 struct ether_priv *priv = dev_get_priv(dev);
2601 struct eth_dev *ethdev = &priv->ethdev;
2603 packet_received = 0;
2605 return rx_submit(ethdev, ethdev->rx_req, 0);
2608 static void usb_eth_stop(struct udevice *dev)
2610 struct ether_priv *priv = dev_get_priv(dev);
2612 _usb_eth_halt(priv);
2615 static int usb_eth_probe(struct udevice *dev)
2617 struct ether_priv *priv = dev_get_priv(dev);
2618 struct eth_pdata *pdata = dev_get_plat(dev);
2623 get_ether_addr(CONFIG_USBNET_DEV_ADDR, pdata->enetaddr);
2624 eth_env_set_enetaddr("usbnet_devaddr", pdata->enetaddr);
2629 static const struct eth_ops usb_eth_ops = {
2630 .start = usb_eth_start,
2631 .send = usb_eth_send,
2632 .recv = usb_eth_recv,
2633 .free_pkt = usb_eth_free_pkt,
2634 .stop = usb_eth_stop,
2637 int usb_ether_init(void)
2639 struct udevice *dev;
2640 struct udevice *usb_dev;
2643 ret = uclass_first_device(UCLASS_USB_GADGET_GENERIC, &usb_dev);
2644 if (!usb_dev || ret) {
2645 pr_err("No USB device found\n");
2649 ret = device_bind_driver(usb_dev, "usb_ether", "usb_ether", &dev);
2651 pr_err("usb - not able to bind usb_ether device\n");
2658 U_BOOT_DRIVER(eth_usb) = {
2659 .name = "usb_ether",
2661 .probe = usb_eth_probe,
2662 .ops = &usb_eth_ops,
2663 .priv_auto = sizeof(struct ether_priv),
2664 .plat_auto = sizeof(struct eth_pdata),
2665 .flags = DM_FLAG_ALLOC_PRIV_DMA,
2667 #endif /* CONFIG_DM_ETH */