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;
113 struct net_device_stats stats;
114 unsigned int tx_qlen;
119 unsigned suspended:1;
120 unsigned network_started:1;
124 #define WORK_RX_MEMORY 0
126 u8 host_mac[ETH_ALEN];
130 * This version autoconfigures as much as possible at run-time.
132 * It also ASSUMES a self-powered device, without remote wakeup,
133 * although remote wakeup support would make sense.
136 /*-------------------------------------------------------------------------*/
138 struct eth_dev ethdev;
139 struct udevice *netdev;
140 struct usb_gadget_driver eth_driver;
143 struct ether_priv eth_priv;
144 struct ether_priv *l_priv = ð_priv;
146 /*-------------------------------------------------------------------------*/
148 /* "main" config is either CDC, or its simple subset */
149 static inline int is_cdc(struct eth_dev *dev)
151 #if !defined(CONFIG_USB_ETH_SUBSET)
152 return 1; /* only cdc possible */
153 #elif !defined(CONFIG_USB_ETH_CDC)
154 return 0; /* only subset possible */
156 return dev->cdc; /* depends on what hardware we found */
160 /* "secondary" RNDIS config may sometimes be activated */
161 static inline int rndis_active(struct eth_dev *dev)
163 #ifdef CONFIG_USB_ETH_RNDIS
170 #define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
171 #define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev))
173 #define DEFAULT_QLEN 2 /* double buffering by default */
175 /* peak bulk transfer bits-per-second */
176 #define HS_BPS (13 * 512 * 8 * 1000 * 8)
177 #define FS_BPS (19 * 64 * 1 * 1000 * 8)
179 #ifdef CONFIG_USB_GADGET_DUALSPEED
180 #define DEVSPEED USB_SPEED_HIGH
182 #ifdef CONFIG_USB_ETH_QMULT
183 #define qmult CONFIG_USB_ETH_QMULT
188 /* for dual-speed hardware, use deeper queues at highspeed */
189 #define qlen(gadget) \
190 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
192 static inline int BITRATE(struct usb_gadget *g)
194 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
197 #else /* full speed (low speed doesn't do bulk) */
201 #define DEVSPEED USB_SPEED_FULL
203 #define qlen(gadget) DEFAULT_QLEN
205 static inline int BITRATE(struct usb_gadget *g)
211 /*-------------------------------------------------------------------------*/
214 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
215 * Instead: allocate your own, using normal USB-IF procedures.
219 * Thanks to NetChip Technologies for donating this product ID.
220 * It's for devices with only CDC Ethernet configurations.
222 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
223 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
226 * For hardware that can't talk CDC, we use the same vendor ID that
227 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
228 * with pxa250. We're protocol-compatible, if the host-side drivers
229 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
230 * drivers that need to hard-wire endpoint numbers have a hook.
232 * The protocol is a minimal subset of CDC Ether, which works on any bulk
233 * hardware that's not deeply broken ... even on hardware that can't talk
234 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
235 * doesn't handle control-OUT).
237 #define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */
238 #define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */
241 * For hardware that can talk RNDIS and either of the above protocols,
242 * use this ID ... the windows INF files will know it. Unless it's
243 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
244 * the non-RNDIS configuration.
246 #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
247 #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
250 * Some systems will want different product identifers published in the
251 * device descriptor, either numbers or strings or both. These string
252 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
256 * Emulating them in eth_bind:
257 * static ushort idVendor;
258 * static ushort idProduct;
261 #if defined(CONFIG_USB_GADGET_MANUFACTURER)
262 static char *iManufacturer = CONFIG_USB_GADGET_MANUFACTURER;
264 static char *iManufacturer = "U-Boot";
267 /* These probably need to be configurable. */
268 static ushort bcdDevice;
269 static char *iProduct;
270 static char *iSerialNumber;
272 static char dev_addr[18];
274 static char host_addr[18];
277 /*-------------------------------------------------------------------------*/
280 * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
281 * ep0 implementation: descriptors, config management, setup().
282 * also optional class-specific notification interrupt transfer.
286 * DESCRIPTORS ... most are static, but strings and (full) configuration
287 * descriptors are built on demand. For now we do either full CDC, or
288 * our simple subset, with RNDIS as an optional second configuration.
290 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
291 * the class descriptors match a modem (they're ignored; it's really just
292 * Ethernet functionality), they don't need the NOP altsetting, and the
293 * status transfer endpoint isn't optional.
296 #define STRING_MANUFACTURER 1
297 #define STRING_PRODUCT 2
298 #define STRING_ETHADDR 3
299 #define STRING_DATA 4
300 #define STRING_CONTROL 5
301 #define STRING_RNDIS_CONTROL 6
303 #define STRING_SUBSET 8
304 #define STRING_RNDIS 9
305 #define STRING_SERIALNUMBER 10
307 /* holds our biggest descriptor (or RNDIS response) */
308 #define USB_BUFSIZ 256
311 * This device advertises one configuration, eth_config, unless RNDIS
312 * is enabled (rndis_config) on hardware supporting at least two configs.
314 * NOTE: Controllers like superh_udc should probably be able to use
315 * an RNDIS-only configuration.
317 * FIXME define some higher-powered configurations to make it easier
318 * to recharge batteries ...
321 #define DEV_CONFIG_VALUE 1 /* cdc or subset */
322 #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
324 static struct usb_device_descriptor
326 .bLength = sizeof device_desc,
327 .bDescriptorType = USB_DT_DEVICE,
329 .bcdUSB = __constant_cpu_to_le16(0x0200),
331 .bDeviceClass = USB_CLASS_COMM,
332 .bDeviceSubClass = 0,
333 .bDeviceProtocol = 0,
335 .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
336 .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
337 .iManufacturer = STRING_MANUFACTURER,
338 .iProduct = STRING_PRODUCT,
339 .bNumConfigurations = 1,
342 static struct usb_otg_descriptor
344 .bLength = sizeof otg_descriptor,
345 .bDescriptorType = USB_DT_OTG,
347 .bmAttributes = USB_OTG_SRP,
350 static struct usb_config_descriptor
352 .bLength = sizeof eth_config,
353 .bDescriptorType = USB_DT_CONFIG,
355 /* compute wTotalLength on the fly */
357 .bConfigurationValue = DEV_CONFIG_VALUE,
358 .iConfiguration = STRING_CDC,
359 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
363 #ifdef CONFIG_USB_ETH_RNDIS
364 static struct usb_config_descriptor
366 .bLength = sizeof rndis_config,
367 .bDescriptorType = USB_DT_CONFIG,
369 /* compute wTotalLength on the fly */
371 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
372 .iConfiguration = STRING_RNDIS,
373 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
379 * Compared to the simple CDC subset, the full CDC Ethernet model adds
380 * three class descriptors, two interface descriptors, optional status
381 * endpoint. Both have a "data" interface and two bulk endpoints.
382 * There are also differences in how control requests are handled.
384 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
385 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
386 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
387 * work around those bugs) are unlikely to ever come from MSFT, you may
388 * wish to avoid using RNDIS.
390 * MCCI offers an alternative to RNDIS if you need to connect to Windows
391 * but have hardware that can't support CDC Ethernet. We add descriptors
392 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
393 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
394 * get those drivers from MCCI, or bundled with various products.
397 #ifdef CONFIG_USB_ETH_CDC
398 static struct usb_interface_descriptor
400 .bLength = sizeof control_intf,
401 .bDescriptorType = USB_DT_INTERFACE,
403 .bInterfaceNumber = 0,
404 /* status endpoint is optional; this may be patched later */
406 .bInterfaceClass = USB_CLASS_COMM,
407 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
408 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
409 .iInterface = STRING_CONTROL,
413 #ifdef CONFIG_USB_ETH_RNDIS
414 static const struct usb_interface_descriptor
415 rndis_control_intf = {
416 .bLength = sizeof rndis_control_intf,
417 .bDescriptorType = USB_DT_INTERFACE,
419 .bInterfaceNumber = 0,
421 .bInterfaceClass = USB_CLASS_COMM,
422 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
423 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
424 .iInterface = STRING_RNDIS_CONTROL,
428 static const struct usb_cdc_header_desc header_desc = {
429 .bLength = sizeof header_desc,
430 .bDescriptorType = USB_DT_CS_INTERFACE,
431 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
433 .bcdCDC = __constant_cpu_to_le16(0x0110),
436 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
438 static const struct usb_cdc_union_desc union_desc = {
439 .bLength = sizeof union_desc,
440 .bDescriptorType = USB_DT_CS_INTERFACE,
441 .bDescriptorSubType = USB_CDC_UNION_TYPE,
443 .bMasterInterface0 = 0, /* index of control interface */
444 .bSlaveInterface0 = 1, /* index of DATA interface */
447 #endif /* CDC || RNDIS */
449 #ifdef CONFIG_USB_ETH_RNDIS
451 static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
452 .bLength = sizeof call_mgmt_descriptor,
453 .bDescriptorType = USB_DT_CS_INTERFACE,
454 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
456 .bmCapabilities = 0x00,
457 .bDataInterface = 0x01,
460 static const struct usb_cdc_acm_descriptor acm_descriptor = {
461 .bLength = sizeof acm_descriptor,
462 .bDescriptorType = USB_DT_CS_INTERFACE,
463 .bDescriptorSubType = USB_CDC_ACM_TYPE,
465 .bmCapabilities = 0x00,
470 #ifndef CONFIG_USB_ETH_CDC
473 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
474 * ways: data endpoints live in the control interface, there's no data
475 * interface, and it's not used to talk to a cell phone radio.
478 static const struct usb_cdc_mdlm_desc mdlm_desc = {
479 .bLength = sizeof mdlm_desc,
480 .bDescriptorType = USB_DT_CS_INTERFACE,
481 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
483 .bcdVersion = __constant_cpu_to_le16(0x0100),
485 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
486 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
491 * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
492 * can't really use its struct. All we do here is say that we're using
493 * the submode of "SAFE" which directly matches the CDC Subset.
495 #ifdef CONFIG_USB_ETH_SUBSET
496 static const u8 mdlm_detail_desc[] = {
499 USB_CDC_MDLM_DETAIL_TYPE,
502 0, /* network control capabilities (none) */
503 0, /* network data capabilities ("raw" encapsulation) */
509 static const struct usb_cdc_ether_desc ether_desc = {
510 .bLength = sizeof(ether_desc),
511 .bDescriptorType = USB_DT_CS_INTERFACE,
512 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
514 /* this descriptor actually adds value, surprise! */
515 .iMACAddress = STRING_ETHADDR,
516 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
517 .wMaxSegmentSize = __constant_cpu_to_le16(PKTSIZE_ALIGN),
518 .wNumberMCFilters = __constant_cpu_to_le16(0),
519 .bNumberPowerFilters = 0,
522 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
525 * include the status endpoint if we can, even where it's optional.
526 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
527 * packet, to simplify cancellation; and a big transfer interval, to
528 * waste less bandwidth.
530 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
531 * if they ignore the connect/disconnect notifications that real aether
532 * can provide. more advanced cdc configurations might want to support
533 * encapsulated commands (vendor-specific, using control-OUT).
535 * RNDIS requires the status endpoint, since it uses that encapsulation
536 * mechanism for its funky RPC scheme.
539 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
540 #define STATUS_BYTECOUNT 16 /* 8 byte header + data */
542 static struct usb_endpoint_descriptor
544 .bLength = USB_DT_ENDPOINT_SIZE,
545 .bDescriptorType = USB_DT_ENDPOINT,
547 .bEndpointAddress = USB_DIR_IN,
548 .bmAttributes = USB_ENDPOINT_XFER_INT,
549 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
550 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
554 #ifdef CONFIG_USB_ETH_CDC
556 /* the default data interface has no endpoints ... */
558 static const struct usb_interface_descriptor
560 .bLength = sizeof data_nop_intf,
561 .bDescriptorType = USB_DT_INTERFACE,
563 .bInterfaceNumber = 1,
564 .bAlternateSetting = 0,
566 .bInterfaceClass = USB_CLASS_CDC_DATA,
567 .bInterfaceSubClass = 0,
568 .bInterfaceProtocol = 0,
571 /* ... but the "real" data interface has two bulk endpoints */
573 static const struct usb_interface_descriptor
575 .bLength = sizeof data_intf,
576 .bDescriptorType = USB_DT_INTERFACE,
578 .bInterfaceNumber = 1,
579 .bAlternateSetting = 1,
581 .bInterfaceClass = USB_CLASS_CDC_DATA,
582 .bInterfaceSubClass = 0,
583 .bInterfaceProtocol = 0,
584 .iInterface = STRING_DATA,
589 #ifdef CONFIG_USB_ETH_RNDIS
591 /* RNDIS doesn't activate by changing to the "real" altsetting */
593 static const struct usb_interface_descriptor
595 .bLength = sizeof rndis_data_intf,
596 .bDescriptorType = USB_DT_INTERFACE,
598 .bInterfaceNumber = 1,
599 .bAlternateSetting = 0,
601 .bInterfaceClass = USB_CLASS_CDC_DATA,
602 .bInterfaceSubClass = 0,
603 .bInterfaceProtocol = 0,
604 .iInterface = STRING_DATA,
609 #ifdef CONFIG_USB_ETH_SUBSET
612 * "Simple" CDC-subset option is a simple vendor-neutral model that most
613 * full speed controllers can handle: one interface, two bulk endpoints.
615 * To assist host side drivers, we fancy it up a bit, and add descriptors
616 * so some host side drivers will understand it as a "SAFE" variant.
619 static const struct usb_interface_descriptor
621 .bLength = sizeof subset_data_intf,
622 .bDescriptorType = USB_DT_INTERFACE,
624 .bInterfaceNumber = 0,
625 .bAlternateSetting = 0,
627 .bInterfaceClass = USB_CLASS_COMM,
628 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
629 .bInterfaceProtocol = 0,
630 .iInterface = STRING_DATA,
635 static struct usb_endpoint_descriptor
637 .bLength = USB_DT_ENDPOINT_SIZE,
638 .bDescriptorType = USB_DT_ENDPOINT,
640 .bEndpointAddress = USB_DIR_IN,
641 .bmAttributes = USB_ENDPOINT_XFER_BULK,
642 .wMaxPacketSize = __constant_cpu_to_le16(64),
645 static struct usb_endpoint_descriptor
647 .bLength = USB_DT_ENDPOINT_SIZE,
648 .bDescriptorType = USB_DT_ENDPOINT,
650 .bEndpointAddress = USB_DIR_OUT,
651 .bmAttributes = USB_ENDPOINT_XFER_BULK,
652 .wMaxPacketSize = __constant_cpu_to_le16(64),
655 static const struct usb_descriptor_header *fs_eth_function[11] = {
656 (struct usb_descriptor_header *) &otg_descriptor,
657 #ifdef CONFIG_USB_ETH_CDC
658 /* "cdc" mode descriptors */
659 (struct usb_descriptor_header *) &control_intf,
660 (struct usb_descriptor_header *) &header_desc,
661 (struct usb_descriptor_header *) &union_desc,
662 (struct usb_descriptor_header *) ðer_desc,
663 /* NOTE: status endpoint may need to be removed */
664 (struct usb_descriptor_header *) &fs_status_desc,
665 /* data interface, with altsetting */
666 (struct usb_descriptor_header *) &data_nop_intf,
667 (struct usb_descriptor_header *) &data_intf,
668 (struct usb_descriptor_header *) &fs_source_desc,
669 (struct usb_descriptor_header *) &fs_sink_desc,
671 #endif /* CONFIG_USB_ETH_CDC */
674 static inline void fs_subset_descriptors(void)
676 #ifdef CONFIG_USB_ETH_SUBSET
677 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
678 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
679 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
680 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
681 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
682 fs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
683 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
684 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
685 fs_eth_function[8] = NULL;
687 fs_eth_function[1] = NULL;
691 #ifdef CONFIG_USB_ETH_RNDIS
692 static const struct usb_descriptor_header *fs_rndis_function[] = {
693 (struct usb_descriptor_header *) &otg_descriptor,
694 /* control interface matches ACM, not Ethernet */
695 (struct usb_descriptor_header *) &rndis_control_intf,
696 (struct usb_descriptor_header *) &header_desc,
697 (struct usb_descriptor_header *) &call_mgmt_descriptor,
698 (struct usb_descriptor_header *) &acm_descriptor,
699 (struct usb_descriptor_header *) &union_desc,
700 (struct usb_descriptor_header *) &fs_status_desc,
701 /* data interface has no altsetting */
702 (struct usb_descriptor_header *) &rndis_data_intf,
703 (struct usb_descriptor_header *) &fs_source_desc,
704 (struct usb_descriptor_header *) &fs_sink_desc,
710 * usb 2.0 devices need to expose both high speed and full speed
711 * descriptors, unless they only run at full speed.
714 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
715 static struct usb_endpoint_descriptor
717 .bLength = USB_DT_ENDPOINT_SIZE,
718 .bDescriptorType = USB_DT_ENDPOINT,
720 .bmAttributes = USB_ENDPOINT_XFER_INT,
721 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
722 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
724 #endif /* CONFIG_USB_ETH_CDC */
726 static struct usb_endpoint_descriptor
728 .bLength = USB_DT_ENDPOINT_SIZE,
729 .bDescriptorType = USB_DT_ENDPOINT,
731 .bmAttributes = USB_ENDPOINT_XFER_BULK,
732 .wMaxPacketSize = __constant_cpu_to_le16(512),
735 static struct usb_endpoint_descriptor
737 .bLength = USB_DT_ENDPOINT_SIZE,
738 .bDescriptorType = USB_DT_ENDPOINT,
740 .bmAttributes = USB_ENDPOINT_XFER_BULK,
741 .wMaxPacketSize = __constant_cpu_to_le16(512),
744 static struct usb_qualifier_descriptor
746 .bLength = sizeof dev_qualifier,
747 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
749 .bcdUSB = __constant_cpu_to_le16(0x0200),
750 .bDeviceClass = USB_CLASS_COMM,
752 .bNumConfigurations = 1,
755 static const struct usb_descriptor_header *hs_eth_function[11] = {
756 (struct usb_descriptor_header *) &otg_descriptor,
757 #ifdef CONFIG_USB_ETH_CDC
758 /* "cdc" mode descriptors */
759 (struct usb_descriptor_header *) &control_intf,
760 (struct usb_descriptor_header *) &header_desc,
761 (struct usb_descriptor_header *) &union_desc,
762 (struct usb_descriptor_header *) ðer_desc,
763 /* NOTE: status endpoint may need to be removed */
764 (struct usb_descriptor_header *) &hs_status_desc,
765 /* data interface, with altsetting */
766 (struct usb_descriptor_header *) &data_nop_intf,
767 (struct usb_descriptor_header *) &data_intf,
768 (struct usb_descriptor_header *) &hs_source_desc,
769 (struct usb_descriptor_header *) &hs_sink_desc,
771 #endif /* CONFIG_USB_ETH_CDC */
774 static inline void hs_subset_descriptors(void)
776 #ifdef CONFIG_USB_ETH_SUBSET
777 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
778 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
779 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
780 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
781 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
782 hs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
783 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
784 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
785 hs_eth_function[8] = NULL;
787 hs_eth_function[1] = NULL;
791 #ifdef CONFIG_USB_ETH_RNDIS
792 static const struct usb_descriptor_header *hs_rndis_function[] = {
793 (struct usb_descriptor_header *) &otg_descriptor,
794 /* control interface matches ACM, not Ethernet */
795 (struct usb_descriptor_header *) &rndis_control_intf,
796 (struct usb_descriptor_header *) &header_desc,
797 (struct usb_descriptor_header *) &call_mgmt_descriptor,
798 (struct usb_descriptor_header *) &acm_descriptor,
799 (struct usb_descriptor_header *) &union_desc,
800 (struct usb_descriptor_header *) &hs_status_desc,
801 /* data interface has no altsetting */
802 (struct usb_descriptor_header *) &rndis_data_intf,
803 (struct usb_descriptor_header *) &hs_source_desc,
804 (struct usb_descriptor_header *) &hs_sink_desc,
810 /* maxpacket and other transfer characteristics vary by speed. */
811 static inline struct usb_endpoint_descriptor *
812 ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
813 struct usb_endpoint_descriptor *fs)
815 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
820 /*-------------------------------------------------------------------------*/
822 /* descriptors that are built on-demand */
824 static char manufacturer[50];
825 static char product_desc[40] = DRIVER_DESC;
826 static char serial_number[20];
828 /* address that the host will use ... usually assigned at random */
829 static char ethaddr[2 * ETH_ALEN + 1];
831 /* static strings, in UTF-8 */
832 static struct usb_string strings[] = {
833 { STRING_MANUFACTURER, manufacturer, },
834 { STRING_PRODUCT, product_desc, },
835 { STRING_SERIALNUMBER, serial_number, },
836 { STRING_DATA, "Ethernet Data", },
837 { STRING_ETHADDR, ethaddr, },
838 #ifdef CONFIG_USB_ETH_CDC
839 { STRING_CDC, "CDC Ethernet", },
840 { STRING_CONTROL, "CDC Communications Control", },
842 #ifdef CONFIG_USB_ETH_SUBSET
843 { STRING_SUBSET, "CDC Ethernet Subset", },
845 #ifdef CONFIG_USB_ETH_RNDIS
846 { STRING_RNDIS, "RNDIS", },
847 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
849 { } /* end of list */
852 static struct usb_gadget_strings stringtab = {
853 .language = 0x0409, /* en-us */
857 /*============================================================================*/
858 DEFINE_CACHE_ALIGN_BUFFER(u8, control_req, USB_BUFSIZ);
860 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
861 DEFINE_CACHE_ALIGN_BUFFER(u8, status_req, STATUS_BYTECOUNT);
864 /*============================================================================*/
867 * one config, two interfaces: control, data.
868 * complications: class descriptors, and an altsetting.
871 config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
874 const struct usb_config_descriptor *config;
875 const struct usb_descriptor_header **function;
878 if (gadget_is_dualspeed(g)) {
879 hs = (g->speed == USB_SPEED_HIGH);
880 if (type == USB_DT_OTHER_SPEED_CONFIG)
883 #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
885 if (index >= device_desc.bNumConfigurations)
888 #ifdef CONFIG_USB_ETH_RNDIS
890 * list the RNDIS config first, to make Microsoft's drivers
891 * happy. DOCSIS 1.0 needs this too.
893 if (device_desc.bNumConfigurations == 2 && index == 0) {
894 config = &rndis_config;
895 function = which_fn(rndis);
899 config = ð_config;
900 function = which_fn(eth);
903 /* for now, don't advertise srp-only devices */
907 len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
910 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
914 /*-------------------------------------------------------------------------*/
916 static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
917 static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
920 set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
923 struct usb_gadget *gadget = dev->gadget;
925 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
926 /* status endpoint used for RNDIS and (optionally) CDC */
927 if (!subset_active(dev) && dev->status_ep) {
928 dev->status = ep_desc(gadget, &hs_status_desc,
930 dev->status_ep->driver_data = dev;
932 result = usb_ep_enable(dev->status_ep, dev->status);
934 debug("enable %s --> %d\n",
935 dev->status_ep->name, result);
941 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
942 dev->in_ep->driver_data = dev;
944 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
945 dev->out_ep->driver_data = dev;
948 * With CDC, the host isn't allowed to use these two data
949 * endpoints in the default altsetting for the interface.
950 * so we don't activate them yet. Reset from SET_INTERFACE.
952 * Strictly speaking RNDIS should work the same: activation is
953 * a side effect of setting a packet filter. Deactivation is
954 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
956 if (!cdc_active(dev)) {
957 result = usb_ep_enable(dev->in_ep, dev->in);
959 debug("enable %s --> %d\n",
960 dev->in_ep->name, result);
964 result = usb_ep_enable(dev->out_ep, dev->out);
966 debug("enable %s --> %d\n",
967 dev->out_ep->name, result);
974 result = alloc_requests(dev, qlen(gadget), gfp_flags);
976 /* on error, disable any endpoints */
978 if (!subset_active(dev) && dev->status_ep)
979 (void) usb_ep_disable(dev->status_ep);
981 (void) usb_ep_disable(dev->in_ep);
982 (void) usb_ep_disable(dev->out_ep);
985 } else if (!cdc_active(dev)) {
987 * activate non-CDC configs right away
988 * this isn't strictly according to the RNDIS spec
990 eth_start(dev, GFP_ATOMIC);
993 /* caller is responsible for cleanup on error */
997 static void eth_reset_config(struct eth_dev *dev)
999 if (dev->config == 0)
1002 debug("%s\n", __func__);
1004 rndis_uninit(dev->rndis_config);
1007 * disable endpoints, forcing (synchronous) completion of
1008 * pending i/o. then free the requests.
1012 usb_ep_disable(dev->in_ep);
1014 usb_ep_free_request(dev->in_ep, dev->tx_req);
1019 usb_ep_disable(dev->out_ep);
1021 usb_ep_free_request(dev->out_ep, dev->rx_req);
1026 usb_ep_disable(dev->status_ep);
1029 dev->cdc_filter = 0;
1034 * change our operational config. must agree with the code
1035 * that returns config descriptors, and altsetting code.
1037 static int eth_set_config(struct eth_dev *dev, unsigned number,
1041 struct usb_gadget *gadget = dev->gadget;
1043 eth_reset_config(dev);
1046 case DEV_CONFIG_VALUE:
1047 result = set_ether_config(dev, gfp_flags);
1049 #ifdef CONFIG_USB_ETH_RNDIS
1050 case DEV_RNDIS_CONFIG_VALUE:
1052 result = set_ether_config(dev, gfp_flags);
1064 eth_reset_config(dev);
1065 usb_gadget_vbus_draw(dev->gadget,
1066 gadget_is_otg(dev->gadget) ? 8 : 100);
1071 power = 2 * eth_config.bMaxPower;
1072 usb_gadget_vbus_draw(dev->gadget, power);
1074 switch (gadget->speed) {
1075 case USB_SPEED_FULL:
1076 speed = "full"; break;
1077 #ifdef CONFIG_USB_GADGET_DUALSPEED
1078 case USB_SPEED_HIGH:
1079 speed = "high"; break;
1085 dev->config = number;
1086 printf("%s speed config #%d: %d mA, %s, using %s\n",
1087 speed, number, power, driver_desc,
1092 : "CDC Ethernet Subset"));
1097 /*-------------------------------------------------------------------------*/
1099 #ifdef CONFIG_USB_ETH_CDC
1102 * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
1103 * only to notify the host about link status changes (which we support) or
1104 * report completion of some encapsulated command (as used in RNDIS). Since
1105 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1106 * command mechanism; and only one status request is ever queued.
1108 static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
1110 struct usb_cdc_notification *event = req->buf;
1111 int value = req->status;
1112 struct eth_dev *dev = ep->driver_data;
1114 /* issue the second notification if host reads the first */
1115 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1117 __le32 *data = req->buf + sizeof *event;
1119 event->bmRequestType = 0xA1;
1120 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
1121 event->wValue = __constant_cpu_to_le16(0);
1122 event->wIndex = __constant_cpu_to_le16(1);
1123 event->wLength = __constant_cpu_to_le16(8);
1125 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1126 data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
1128 req->length = STATUS_BYTECOUNT;
1129 value = usb_ep_queue(ep, req, GFP_ATOMIC);
1130 debug("send SPEED_CHANGE --> %d\n", value);
1133 } else if (value != -ECONNRESET) {
1134 debug("event %02x --> %d\n",
1135 event->bNotificationType, value);
1136 if (event->bNotificationType ==
1137 USB_CDC_NOTIFY_SPEED_CHANGE) {
1138 dev->network_started = 1;
1139 printf("USB network up!\n");
1142 req->context = NULL;
1145 static void issue_start_status(struct eth_dev *dev)
1147 struct usb_request *req = dev->stat_req;
1148 struct usb_cdc_notification *event;
1154 * FIXME ugly idiom, maybe we'd be better with just
1155 * a "cancel the whole queue" primitive since any
1156 * unlink-one primitive has way too many error modes.
1157 * here, we "know" toggle is already clear...
1159 * FIXME iff req->context != null just dequeue it
1161 usb_ep_disable(dev->status_ep);
1162 usb_ep_enable(dev->status_ep, dev->status);
1165 * 3.8.1 says to issue first NETWORK_CONNECTION, then
1166 * a SPEED_CHANGE. could be useful in some configs.
1169 event->bmRequestType = 0xA1;
1170 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
1171 event->wValue = __constant_cpu_to_le16(1); /* connected */
1172 event->wIndex = __constant_cpu_to_le16(1);
1175 req->length = sizeof *event;
1176 req->complete = eth_status_complete;
1179 value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
1181 debug("status buf queue --> %d\n", value);
1186 /*-------------------------------------------------------------------------*/
1188 static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
1190 if (req->status || req->actual != req->length)
1191 debug("setup complete --> %d, %d/%d\n",
1192 req->status, req->actual, req->length);
1195 #ifdef CONFIG_USB_ETH_RNDIS
1197 static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
1199 if (req->status || req->actual != req->length)
1200 debug("rndis response complete --> %d, %d/%d\n",
1201 req->status, req->actual, req->length);
1203 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1206 static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
1208 struct eth_dev *dev = ep->driver_data;
1211 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1212 status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
1214 pr_err("%s: rndis parse error %d", __func__, status);
1220 * The setup() callback implements all the ep0 functionality that's not
1221 * handled lower down. CDC has a number of less-common features:
1223 * - two interfaces: control, and ethernet data
1224 * - Ethernet data interface has two altsettings: default, and active
1225 * - class-specific descriptors for the control interface
1226 * - class-specific control requests
1229 eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1231 struct eth_dev *dev = get_gadget_data(gadget);
1232 struct usb_request *req = dev->req;
1233 int value = -EOPNOTSUPP;
1234 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1235 u16 wValue = le16_to_cpu(ctrl->wValue);
1236 u16 wLength = le16_to_cpu(ctrl->wLength);
1239 * descriptors just go into the pre-allocated ep0 buffer,
1240 * while config change events may enable network traffic.
1243 debug("%s\n", __func__);
1245 req->complete = eth_setup_complete;
1246 switch (ctrl->bRequest) {
1248 case USB_REQ_GET_DESCRIPTOR:
1249 if (ctrl->bRequestType != USB_DIR_IN)
1251 switch (wValue >> 8) {
1254 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1255 value = min(wLength, (u16) sizeof device_desc);
1256 memcpy(req->buf, &device_desc, value);
1258 case USB_DT_DEVICE_QUALIFIER:
1259 if (!gadget_is_dualspeed(gadget))
1261 value = min(wLength, (u16) sizeof dev_qualifier);
1262 memcpy(req->buf, &dev_qualifier, value);
1265 case USB_DT_OTHER_SPEED_CONFIG:
1266 if (!gadget_is_dualspeed(gadget))
1270 value = config_buf(gadget, req->buf,
1273 gadget_is_otg(gadget));
1275 value = min(wLength, (u16) value);
1279 value = usb_gadget_get_string(&stringtab,
1280 wValue & 0xff, req->buf);
1283 value = min(wLength, (u16) value);
1289 case USB_REQ_SET_CONFIGURATION:
1290 if (ctrl->bRequestType != 0)
1292 if (gadget->a_hnp_support)
1293 debug("HNP available\n");
1294 else if (gadget->a_alt_hnp_support)
1295 debug("HNP needs a different root port\n");
1296 value = eth_set_config(dev, wValue, GFP_ATOMIC);
1298 case USB_REQ_GET_CONFIGURATION:
1299 if (ctrl->bRequestType != USB_DIR_IN)
1301 *(u8 *)req->buf = dev->config;
1302 value = min(wLength, (u16) 1);
1305 case USB_REQ_SET_INTERFACE:
1306 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1310 if (!cdc_active(dev) && wIndex != 0)
1313 #ifdef CONFIG_USB_ETH_CDC
1315 case 0: /* control/master intf */
1319 usb_ep_disable(dev->status_ep);
1320 usb_ep_enable(dev->status_ep, dev->status);
1325 case 1: /* data intf */
1328 usb_ep_disable(dev->in_ep);
1329 usb_ep_disable(dev->out_ep);
1332 * CDC requires the data transfers not be done from
1333 * the default interface setting ... also, setting
1334 * the non-default interface resets filters etc.
1337 if (!cdc_active(dev))
1339 usb_ep_enable(dev->in_ep, dev->in);
1340 usb_ep_enable(dev->out_ep, dev->out);
1341 dev->cdc_filter = DEFAULT_FILTER;
1343 issue_start_status(dev);
1344 eth_start(dev, GFP_ATOMIC);
1351 * FIXME this is wrong, as is the assumption that
1352 * all non-PXA hardware talks real CDC ...
1354 debug("set_interface ignored!\n");
1355 #endif /* CONFIG_USB_ETH_CDC */
1357 case USB_REQ_GET_INTERFACE:
1358 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1362 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
1365 /* for CDC, iff carrier is on, data interface is active. */
1366 if (rndis_active(dev) || wIndex != 1)
1367 *(u8 *)req->buf = 0;
1369 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1370 /* carrier always ok ...*/
1371 *(u8 *)req->buf = 1 ;
1373 value = min(wLength, (u16) 1);
1376 #ifdef CONFIG_USB_ETH_CDC
1377 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
1379 * see 6.2.30: no data, wIndex = interface,
1380 * wValue = packet filter bitmap
1382 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1387 debug("packet filter %02x\n", wValue);
1388 dev->cdc_filter = wValue;
1394 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1395 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1396 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1397 * case USB_CDC_GET_ETHERNET_STATISTIC:
1400 #endif /* CONFIG_USB_ETH_CDC */
1402 #ifdef CONFIG_USB_ETH_RNDIS
1404 * RNDIS uses the CDC command encapsulation mechanism to implement
1405 * an RPC scheme, with much getting/setting of attributes by OID.
1407 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1408 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1409 || !rndis_active(dev)
1410 || wLength > USB_BUFSIZ
1412 || rndis_control_intf.bInterfaceNumber
1415 /* read the request, then process it */
1417 req->complete = rndis_command_complete;
1418 /* later, rndis_control_ack () sends a notification */
1421 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1422 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1423 == ctrl->bRequestType
1424 && rndis_active(dev)
1425 /* && wLength >= 0x0400 */
1427 && rndis_control_intf.bInterfaceNumber
1432 /* return the result */
1433 buf = rndis_get_next_response(dev->rndis_config, &n);
1435 memcpy(req->buf, buf, n);
1436 req->complete = rndis_response_complete;
1437 rndis_free_response(dev->rndis_config, buf);
1440 /* else stalls ... spec says to avoid that */
1446 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
1447 ctrl->bRequestType, ctrl->bRequest,
1448 wValue, wIndex, wLength);
1451 /* respond with data transfer before status phase? */
1453 debug("respond with data transfer before status phase\n");
1454 req->length = value;
1455 req->zero = value < wLength
1456 && (value % gadget->ep0->maxpacket) == 0;
1457 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1459 debug("ep_queue --> %d\n", value);
1461 eth_setup_complete(gadget->ep0, req);
1465 /* host either stalls (value < 0) or reports success */
1469 /*-------------------------------------------------------------------------*/
1471 static void rx_complete(struct usb_ep *ep, struct usb_request *req);
1473 static int rx_submit(struct eth_dev *dev, struct usb_request *req,
1476 int retval = -ENOMEM;
1480 * Padding up to RX_EXTRA handles minor disagreements with host.
1481 * Normally we use the USB "terminate on short read" convention;
1482 * so allow up to (N*maxpacket), since that memory is normally
1483 * already allocated. Some hardware doesn't deal well with short
1484 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1485 * byte off the end (to force hardware errors on overflow).
1487 * RNDIS uses internal framing, and explicitly allows senders to
1488 * pad to end-of-packet. That's potentially nice for speed,
1489 * but means receivers can't recover synch on their own.
1492 debug("%s\n", __func__);
1496 size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1497 size += dev->out_ep->maxpacket - 1;
1498 if (rndis_active(dev))
1499 size += sizeof(struct rndis_packet_msg_type);
1500 size -= size % dev->out_ep->maxpacket;
1503 * Some platforms perform better when IP packets are aligned,
1504 * but on at least one, checksumming fails otherwise. Note:
1505 * RNDIS headers involve variable numbers of LE32 values.
1508 req->buf = (u8 *)net_rx_packets[0];
1510 req->complete = rx_complete;
1512 retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
1515 pr_err("rx submit --> %d", retval);
1520 static void rx_complete(struct usb_ep *ep, struct usb_request *req)
1522 struct eth_dev *dev = ep->driver_data;
1524 debug("%s: status %d\n", __func__, req->status);
1525 switch (req->status) {
1526 /* normal completion */
1528 if (rndis_active(dev)) {
1529 /* we know MaxPacketsPerTransfer == 1 here */
1530 int length = rndis_rm_hdr(req->buf, req->actual);
1533 req->length -= length;
1534 req->actual -= length;
1536 if (req->actual < ETH_HLEN || PKTSIZE_ALIGN < req->actual) {
1538 dev->stats.rx_errors++;
1539 dev->stats.rx_length_errors++;
1540 debug("rx length %d\n", req->length);
1544 dev->stats.rx_packets++;
1545 dev->stats.rx_bytes += req->length;
1548 /* software-driven interface shutdown */
1549 case -ECONNRESET: /* unlink */
1550 case -ESHUTDOWN: /* disconnect etc */
1551 /* for hardware automagic (such as pxa) */
1552 case -ECONNABORTED: /* endpoint reset */
1557 dev->stats.rx_over_errors++;
1560 dev->stats.rx_errors++;
1564 packet_received = 1;
1567 static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
1570 dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
1575 dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
1583 usb_ep_free_request(dev->in_ep, dev->tx_req);
1585 pr_err("can't alloc requests");
1589 static void tx_complete(struct usb_ep *ep, struct usb_request *req)
1591 struct eth_dev *dev = ep->driver_data;
1593 debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
1594 switch (req->status) {
1596 dev->stats.tx_errors++;
1597 debug("tx err %d\n", req->status);
1599 case -ECONNRESET: /* unlink */
1600 case -ESHUTDOWN: /* disconnect etc */
1603 dev->stats.tx_bytes += req->length;
1605 dev->stats.tx_packets++;
1610 static inline int eth_is_promisc(struct eth_dev *dev)
1612 /* no filters for the CDC subset; always promisc */
1613 if (subset_active(dev))
1615 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1619 static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1621 struct eth_dev *dev = netdev_priv(net);
1622 int length = skb->len;
1624 struct usb_request *req = NULL;
1625 unsigned long flags;
1627 /* apply outgoing CDC or RNDIS filters */
1628 if (!eth_is_promisc (dev)) {
1629 u8 *dest = skb->data;
1631 if (is_multicast_ethaddr(dest)) {
1634 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1635 * SET_ETHERNET_MULTICAST_FILTERS requests
1637 if (is_broadcast_ethaddr(dest))
1638 type = USB_CDC_PACKET_TYPE_BROADCAST;
1640 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1641 if (!(dev->cdc_filter & type)) {
1642 dev_kfree_skb_any (skb);
1646 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1649 spin_lock_irqsave(&dev->req_lock, flags);
1651 * this freelist can be empty if an interrupt triggered disconnect()
1652 * and reconfigured the gadget (shutting down this queue) after the
1653 * network stack decided to xmit but before we got the spinlock.
1655 if (list_empty(&dev->tx_reqs)) {
1656 spin_unlock_irqrestore(&dev->req_lock, flags);
1660 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1661 list_del (&req->list);
1663 /* temporarily stop TX queue when the freelist empties */
1664 if (list_empty (&dev->tx_reqs))
1665 netif_stop_queue (net);
1666 spin_unlock_irqrestore(&dev->req_lock, flags);
1668 /* no buffer copies needed, unless the network stack did it
1669 * or the hardware can't use skb buffers.
1670 * or there's not enough space for any RNDIS headers we need
1672 if (rndis_active(dev)) {
1673 struct sk_buff *skb_rndis;
1675 skb_rndis = skb_realloc_headroom (skb,
1676 sizeof (struct rndis_packet_msg_type));
1680 dev_kfree_skb_any (skb);
1682 rndis_add_hdr (skb);
1685 req->buf = skb->data;
1687 req->complete = tx_complete;
1689 /* use zlp framing on tx for strict CDC-Ether conformance,
1690 * though any robust network rx path ignores extra padding.
1691 * and some hardware doesn't like to write zlps.
1694 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1697 req->length = length;
1699 /* throttle highspeed IRQ rate back slightly */
1700 if (gadget_is_dualspeed(dev->gadget))
1701 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1702 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1705 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1708 DEBUG (dev, "tx queue err %d\n", retval);
1711 net->trans_start = jiffies;
1712 atomic_inc (&dev->tx_qlen);
1717 dev->stats.tx_dropped++;
1718 dev_kfree_skb_any (skb);
1719 spin_lock_irqsave(&dev->req_lock, flags);
1720 if (list_empty (&dev->tx_reqs))
1721 netif_start_queue (net);
1722 list_add (&req->list, &dev->tx_reqs);
1723 spin_unlock_irqrestore(&dev->req_lock, flags);
1728 /*-------------------------------------------------------------------------*/
1731 static void eth_unbind(struct usb_gadget *gadget)
1733 struct eth_dev *dev = get_gadget_data(gadget);
1735 debug("%s...\n", __func__);
1736 rndis_deregister(dev->rndis_config);
1739 /* we've already been disconnected ... no i/o is active */
1741 usb_ep_free_request(gadget->ep0, dev->req);
1744 if (dev->stat_req) {
1745 usb_ep_free_request(dev->status_ep, dev->stat_req);
1746 dev->stat_req = NULL;
1750 usb_ep_free_request(dev->in_ep, dev->tx_req);
1755 usb_ep_free_request(dev->out_ep, dev->rx_req);
1759 /* unregister_netdev (dev->net);*/
1760 /* free_netdev(dev->net);*/
1763 set_gadget_data(gadget, NULL);
1766 static void eth_disconnect(struct usb_gadget *gadget)
1768 eth_reset_config(get_gadget_data(gadget));
1769 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1772 static void eth_suspend(struct usb_gadget *gadget)
1777 static void eth_resume(struct usb_gadget *gadget)
1782 /*-------------------------------------------------------------------------*/
1784 #ifdef CONFIG_USB_ETH_RNDIS
1787 * The interrupt endpoint is used in RNDIS to notify the host when messages
1788 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1789 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1790 * REMOTE_NDIS_KEEPALIVE_MSG.
1792 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1793 * normally just one notification will be queued.
1796 static void rndis_control_ack_complete(struct usb_ep *ep,
1797 struct usb_request *req)
1799 struct eth_dev *dev = ep->driver_data;
1801 debug("%s...\n", __func__);
1802 if (req->status || req->actual != req->length)
1803 debug("rndis control ack complete --> %d, %d/%d\n",
1804 req->status, req->actual, req->length);
1806 if (!dev->network_started) {
1807 if (rndis_get_state(dev->rndis_config)
1808 == RNDIS_DATA_INITIALIZED) {
1809 dev->network_started = 1;
1810 printf("USB RNDIS network up!\n");
1814 req->context = NULL;
1816 if (req != dev->stat_req)
1817 usb_ep_free_request(ep, req);
1820 static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
1822 static int rndis_control_ack(struct udevice *net)
1824 struct ether_priv *priv;
1825 struct eth_dev *dev;
1827 struct usb_request *resp;
1829 priv = dev_get_priv(net);
1830 dev = &priv->ethdev;
1831 resp = dev->stat_req;
1833 /* in case RNDIS calls this after disconnect */
1835 debug("status ENODEV\n");
1839 /* in case queue length > 1 */
1840 if (resp->context) {
1841 resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
1844 resp->buf = rndis_resp_buf;
1848 * Send RNDIS RESPONSE_AVAILABLE notification;
1849 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1852 resp->complete = rndis_control_ack_complete;
1853 resp->context = dev;
1855 *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
1856 *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
1858 length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
1861 rndis_control_ack_complete(dev->status_ep, resp);
1869 #define rndis_control_ack NULL
1873 static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
1875 if (rndis_active(dev)) {
1876 rndis_set_param_medium(dev->rndis_config,
1878 BITRATE(dev->gadget)/100);
1879 rndis_signal_connect(dev->rndis_config);
1883 static int eth_stop(struct eth_dev *dev)
1885 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1887 unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1890 if (rndis_active(dev)) {
1891 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
1892 rndis_signal_disconnect(dev->rndis_config);
1894 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1895 /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1897 while (get_timer(ts) < timeout)
1898 usb_gadget_handle_interrupts(0);
1901 rndis_uninit(dev->rndis_config);
1908 /*-------------------------------------------------------------------------*/
1910 static int is_eth_addr_valid(char *str)
1912 if (strlen(str) == 17) {
1917 /* see if it looks like an ethernet address */
1921 for (i = 0; i < 6; i++) {
1922 char term = (i == 5 ? '\0' : ':');
1924 ea[i] = simple_strtol(p, &q, 16);
1926 if ((q - p) != 2 || *q++ != term)
1932 /* Now check the contents. */
1933 return is_valid_ethaddr(ea);
1938 static u8 nibble(unsigned char c)
1940 if (likely(isdigit(c)))
1943 if (likely(isxdigit(c)))
1944 return 10 + c - 'A';
1948 static int get_ether_addr(const char *str, u8 *dev_addr)
1953 for (i = 0; i < 6; i++) {
1956 if ((*str == '.') || (*str == ':'))
1958 num = nibble(*str++) << 4;
1959 num |= (nibble(*str++));
1962 if (is_valid_ethaddr(dev_addr))
1968 static int eth_bind(struct usb_gadget *gadget)
1970 struct eth_dev *dev = &l_priv->ethdev;
1971 u8 cdc = 1, zlp = 1, rndis = 1;
1972 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
1973 int status = -ENOMEM;
1976 struct eth_pdata *pdata = dev_get_plat(l_priv->netdev);
1978 /* these flags are only ever cleared; compiler take note */
1979 #ifndef CONFIG_USB_ETH_CDC
1982 #ifndef CONFIG_USB_ETH_RNDIS
1986 * Because most host side USB stacks handle CDC Ethernet, that
1987 * standard protocol is _strongly_ preferred for interop purposes.
1988 * (By everyone except Microsoft.)
1990 if (gadget_is_musbhdrc(gadget)) {
1991 /* reduce tx dma overhead by avoiding special cases */
1993 } else if (gadget_is_sh(gadget)) {
1994 /* sh doesn't support multiple interfaces or configs */
1999 gcnum = usb_gadget_controller_number(gadget);
2001 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
2004 * can't assume CDC works. don't want to default to
2005 * anything less functional on CDC-capable hardware,
2006 * so we fail in this case.
2008 pr_err("controller '%s' not recognized",
2014 * If there's an RNDIS configuration, that's what Windows wants to
2015 * be using ... so use these product IDs here and in the "linux.inf"
2016 * needed to install MSFT drivers. Current Linux kernels will use
2017 * the second configuration if it's CDC Ethernet, and need some help
2018 * to choose the right configuration otherwise.
2021 #if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
2022 device_desc.idVendor =
2023 __constant_cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
2024 device_desc.idProduct =
2025 __constant_cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
2027 device_desc.idVendor =
2028 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2029 device_desc.idProduct =
2030 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2032 sprintf(product_desc, "RNDIS/%s", driver_desc);
2035 * CDC subset ... recognized by Linux since 2.4.10, but Windows
2036 * drivers aren't widely available. (That may be improved by
2037 * supporting one submode of the "SAFE" variant of MDLM.)
2040 #if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
2041 device_desc.idVendor = cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
2042 device_desc.idProduct = cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
2045 device_desc.idVendor =
2046 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2047 device_desc.idProduct =
2048 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2052 /* support optional vendor/distro customization */
2054 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2056 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
2058 strlcpy(product_desc, iProduct, sizeof product_desc);
2059 if (iSerialNumber) {
2060 device_desc.iSerialNumber = STRING_SERIALNUMBER,
2061 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
2064 /* all we really need is bulk IN/OUT */
2065 usb_ep_autoconfig_reset(gadget);
2066 in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
2069 pr_err("can't autoconfigure on %s\n",
2073 in_ep->driver_data = in_ep; /* claim */
2075 out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
2078 out_ep->driver_data = out_ep; /* claim */
2080 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2082 * CDC Ethernet control interface doesn't require a status endpoint.
2083 * Since some hosts expect one, try to allocate one anyway.
2086 status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
2088 status_ep->driver_data = status_ep; /* claim */
2090 pr_err("can't run RNDIS on %s", gadget->name);
2092 #ifdef CONFIG_USB_ETH_CDC
2094 control_intf.bNumEndpoints = 0;
2095 /* FIXME remove endpoint from descriptor list */
2101 /* one config: cdc, else minimal subset */
2103 eth_config.bNumInterfaces = 1;
2104 eth_config.iConfiguration = STRING_SUBSET;
2107 * use functions to set these up, in case we're built to work
2108 * with multiple controllers and must override CDC Ethernet.
2110 fs_subset_descriptors();
2111 hs_subset_descriptors();
2114 usb_gadget_set_selfpowered(gadget);
2116 /* For now RNDIS is always a second config */
2118 device_desc.bNumConfigurations = 2;
2120 if (gadget_is_dualspeed(gadget)) {
2122 dev_qualifier.bNumConfigurations = 2;
2124 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2126 /* assumes ep0 uses the same value for both speeds ... */
2127 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2129 /* and that all endpoints are dual-speed */
2130 hs_source_desc.bEndpointAddress =
2131 fs_source_desc.bEndpointAddress;
2132 hs_sink_desc.bEndpointAddress =
2133 fs_sink_desc.bEndpointAddress;
2134 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2136 hs_status_desc.bEndpointAddress =
2137 fs_status_desc.bEndpointAddress;
2141 if (gadget_is_otg(gadget)) {
2142 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2143 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2144 eth_config.bMaxPower = 4;
2145 #ifdef CONFIG_USB_ETH_RNDIS
2146 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2147 rndis_config.bMaxPower = 4;
2152 /* network device setup */
2153 dev->net = l_priv->netdev;
2159 dev->out_ep = out_ep;
2160 dev->status_ep = status_ep;
2162 memset(tmp, 0, sizeof(tmp));
2164 * Module params for these addresses should come from ID proms.
2165 * The host side address is used with CDC and RNDIS, and commonly
2166 * ends up in a persistent config database. It's not clear if
2167 * host side code for the SAFE thing cares -- its original BLAN
2168 * thing didn't, Sharp never assigned those addresses on Zaurii.
2170 get_ether_addr(dev_addr, pdata->enetaddr);
2171 memcpy(tmp, pdata->enetaddr, sizeof(pdata->enetaddr));
2173 get_ether_addr(host_addr, dev->host_mac);
2175 sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
2176 dev->host_mac[0], dev->host_mac[1],
2177 dev->host_mac[2], dev->host_mac[3],
2178 dev->host_mac[4], dev->host_mac[5]);
2181 status = rndis_init();
2183 pr_err("can't init RNDIS, %d", status);
2189 * use PKTSIZE (or aligned... from u-boot) and set
2190 * wMaxSegmentSize accordingly
2192 dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
2194 /* preallocate control message data and buffer */
2195 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
2198 dev->req->buf = control_req;
2199 dev->req->complete = eth_setup_complete;
2201 /* ... and maybe likewise for status transfer */
2202 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2203 if (dev->status_ep) {
2204 dev->stat_req = usb_ep_alloc_request(dev->status_ep,
2206 if (!dev->stat_req) {
2207 usb_ep_free_request(dev->status_ep, dev->req);
2211 dev->stat_req->buf = status_req;
2212 dev->stat_req->context = NULL;
2216 /* finish hookup to lower layer ... */
2217 dev->gadget = gadget;
2218 set_gadget_data(gadget, dev);
2219 gadget->ep0->driver_data = dev;
2222 * two kinds of host-initiated state changes:
2223 * - iff DATA transfer is active, carrier is "on"
2224 * - tx queueing enabled if open *and* carrier is "on"
2227 printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
2228 out_ep->name, in_ep->name,
2229 status_ep ? " STATUS " : "",
2230 status_ep ? status_ep->name : ""
2232 printf("MAC %pM\n", pdata->enetaddr);
2235 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2236 dev->host_mac[0], dev->host_mac[1],
2237 dev->host_mac[2], dev->host_mac[3],
2238 dev->host_mac[4], dev->host_mac[5]);
2243 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2245 dev->rndis_config = rndis_register(rndis_control_ack);
2246 if (dev->rndis_config < 0) {
2249 debug("RNDIS setup failed\n");
2254 /* these set up a lot of the OIDs that RNDIS needs */
2255 rndis_set_host_mac(dev->rndis_config, dev->host_mac);
2256 if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
2257 &dev->stats, &dev->cdc_filter))
2259 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2262 if (rndis_set_param_medium(dev->rndis_config,
2263 NDIS_MEDIUM_802_3, 0))
2265 printf("RNDIS ready\n");
2270 pr_err("%s failed, status = %d", __func__, status);
2275 /*-------------------------------------------------------------------------*/
2276 static void usb_eth_stop(struct udevice *dev);
2278 static int usb_eth_start(struct udevice *udev)
2280 struct ether_priv *priv = dev_get_priv(udev);
2281 struct eth_dev *dev = &priv->ethdev;
2282 struct usb_gadget *gadget;
2284 unsigned long timeout = USB_CONNECT_TIMEOUT;
2286 dev->network_started = 0;
2288 packet_received = 0;
2291 gadget = dev->gadget;
2292 usb_gadget_connect(gadget);
2294 if (env_get("cdc_connect_timeout"))
2295 timeout = dectoul(env_get("cdc_connect_timeout"), NULL) * CONFIG_SYS_HZ;
2297 while (!dev->network_started) {
2298 /* Handle control-c and timeouts */
2299 if (ctrlc() || (get_timer(ts) > timeout)) {
2300 pr_err("The remote end did not respond in time.");
2303 usb_gadget_handle_interrupts(0);
2306 packet_received = 0;
2307 rx_submit(dev, dev->rx_req, 0);
2314 static int usb_eth_send(struct udevice *udev, void *packet, int length)
2316 struct ether_priv *priv = dev_get_priv(udev);
2318 void *rndis_pkt = NULL;
2319 struct eth_dev *dev = &priv->ethdev;
2320 struct usb_request *req = dev->tx_req;
2322 unsigned long timeout = USB_CONNECT_TIMEOUT;
2324 debug("%s:...\n", __func__);
2326 /* new buffer is needed to include RNDIS header */
2327 if (rndis_active(dev)) {
2328 rndis_pkt = malloc(length +
2329 sizeof(struct rndis_packet_msg_type));
2331 pr_err("No memory to alloc RNDIS packet");
2334 rndis_add_hdr(rndis_pkt, length);
2335 memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
2338 length += sizeof(struct rndis_packet_msg_type);
2341 req->context = NULL;
2342 req->complete = tx_complete;
2345 * use zlp framing on tx for strict CDC-Ether conformance,
2346 * though any robust network rx path ignores extra padding.
2347 * and some hardware doesn't like to write zlps.
2350 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2353 req->length = length;
2355 /* throttle highspeed IRQ rate back slightly */
2356 if (gadget_is_dualspeed(dev->gadget))
2357 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2358 ? ((dev->tx_qlen % qmult) != 0) : 0;
2364 retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
2367 debug("%s: packet queued\n", __func__);
2368 while (!packet_sent) {
2369 if (get_timer(ts) > timeout) {
2370 printf("timeout sending packets to usb ethernet\n");
2373 usb_gadget_handle_interrupts(0);
2379 dev->stats.tx_dropped++;
2383 static void usb_eth_stop(struct udevice *udev)
2385 struct ether_priv *priv = dev_get_priv(udev);
2386 struct eth_dev *dev = &priv->ethdev;
2388 /* If the gadget not registered, simple return */
2393 * Some USB controllers may need additional deinitialization here
2394 * before dropping pull-up (also due to hardware issues).
2395 * For example: unhandled interrupt with status stage started may
2396 * bring the controller to fully broken state (until board reset).
2397 * There are some variants to debug and fix such cases:
2398 * 1) In the case of RNDIS connection eth_stop can perform additional
2399 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2400 * 2) 'pullup' callback in your UDC driver can be improved to perform
2401 * this deinitialization.
2405 usb_gadget_disconnect(dev->gadget);
2407 /* Clear pending interrupt */
2408 if (dev->network_started) {
2409 usb_gadget_handle_interrupts(0);
2410 dev->network_started = 0;
2414 static int usb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
2416 struct ether_priv *priv = dev_get_priv(dev);
2417 struct eth_dev *ethdev = &priv->ethdev;
2419 usb_gadget_handle_interrupts(0);
2421 if (packet_received) {
2422 if (ethdev->rx_req) {
2423 *packetp = (uchar *)net_rx_packets[0];
2424 return ethdev->rx_req->length;
2426 pr_err("dev->rx_req invalid");
2434 static int usb_eth_free_pkt(struct udevice *dev, uchar *packet,
2437 struct ether_priv *priv = dev_get_priv(dev);
2438 struct eth_dev *ethdev = &priv->ethdev;
2440 packet_received = 0;
2442 return rx_submit(ethdev, ethdev->rx_req, 0);
2445 static const struct eth_ops usb_eth_ops = {
2446 .start = usb_eth_start,
2447 .send = usb_eth_send,
2448 .recv = usb_eth_recv,
2449 .free_pkt = usb_eth_free_pkt,
2450 .stop = usb_eth_stop,
2453 int usb_ether_init(void)
2455 struct udevice *usb_dev;
2458 uclass_first_device(UCLASS_USB_GADGET_GENERIC, &usb_dev);
2460 pr_err("No USB device found\n");
2464 ret = device_bind_driver(usb_dev, "usb_ether", "usb_ether", NULL);
2466 pr_err("usb - not able to bind usb_ether device\n");
2470 return usb_gadget_initialize(0);
2473 static int usb_eth_probe(struct udevice *dev)
2475 struct ether_priv *priv = dev_get_priv(dev);
2476 struct eth_pdata *pdata = dev_get_plat(dev);
2481 get_ether_addr(CONFIG_USBNET_DEV_ADDR, pdata->enetaddr);
2482 eth_env_set_enetaddr("usbnet_devaddr", pdata->enetaddr);
2484 /* Configure default mac-addresses for the USB ethernet device */
2485 #ifdef CONFIG_USBNET_DEV_ADDR
2486 strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
2488 #ifdef CONFIG_USBNET_HOST_ADDR
2489 strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
2491 /* Check if the user overruled the MAC addresses */
2492 if (env_get("usbnet_devaddr"))
2493 strlcpy(dev_addr, env_get("usbnet_devaddr"),
2496 if (env_get("usbnet_hostaddr"))
2497 strlcpy(host_addr, env_get("usbnet_hostaddr"),
2500 if (!is_eth_addr_valid(dev_addr)) {
2501 pr_err("Need valid 'usbnet_devaddr' to be set");
2504 if (!is_eth_addr_valid(host_addr)) {
2505 pr_err("Need valid 'usbnet_hostaddr' to be set");
2509 priv->eth_driver.speed = DEVSPEED;
2510 priv->eth_driver.bind = eth_bind;
2511 priv->eth_driver.unbind = eth_unbind;
2512 priv->eth_driver.setup = eth_setup;
2513 priv->eth_driver.reset = eth_disconnect;
2514 priv->eth_driver.disconnect = eth_disconnect;
2515 priv->eth_driver.suspend = eth_suspend;
2516 priv->eth_driver.resume = eth_resume;
2517 return usb_gadget_register_driver(&priv->eth_driver);
2520 static int usb_eth_remove(struct udevice *dev)
2522 struct ether_priv *priv = dev_get_priv(dev);
2524 usb_gadget_unregister_driver(&priv->eth_driver);
2529 static int usb_eth_unbind(struct udevice *dev)
2531 usb_gadget_release(0);
2536 U_BOOT_DRIVER(eth_usb) = {
2537 .name = "usb_ether",
2539 .probe = usb_eth_probe,
2540 .remove = usb_eth_remove,
2541 .unbind = usb_eth_unbind,
2542 .ops = &usb_eth_ops,
2543 .priv_auto = sizeof(struct ether_priv),
2544 .plat_auto = sizeof(struct eth_pdata),
2545 .flags = DM_FLAG_ALLOC_PRIV_DMA,