2 * ether.c -- Ethernet gadget driver, with CDC and non-CDC options
4 * Copyright (C) 2003-2005,2008 David Brownell
5 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
6 * Copyright (C) 2008 Nokia Corporation
8 * SPDX-License-Identifier: GPL-2.0+
13 #include <linux/errno.h>
14 #include <linux/netdevice.h>
15 #include <linux/usb/ch9.h>
16 #include <linux/usb/cdc.h>
17 #include <linux/usb/gadget.h>
22 #include <linux/ctype.h>
24 #include "gadget_chips.h"
29 #include <dm/uclass-internal.h>
30 #include <dm/device-internal.h>
32 #define USB_NET_NAME "usb_ether"
35 extern struct platform_data brd;
38 unsigned packet_received, packet_sent;
41 * Ethernet gadget driver -- with CDC and non-CDC options
42 * Builds on hardware support for a full duplex link.
44 * CDC Ethernet is the standard USB solution for sending Ethernet frames
45 * using USB. Real hardware tends to use the same framing protocol but look
46 * different for control features. This driver strongly prefers to use
47 * this USB-IF standard as its open-systems interoperability solution;
48 * most host side USB stacks (except from Microsoft) support it.
50 * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
51 * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
52 * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
54 * There's some hardware that can't talk CDC ECM. We make that hardware
55 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
56 * link-level setup only requires activating the configuration. Only the
57 * endpoint descriptors, and product/vendor IDs, are relevant; no control
58 * operations are available. Linux supports it, but other host operating
59 * systems may not. (This is a subset of CDC Ethernet.)
61 * It turns out that if you add a few descriptors to that "CDC Subset",
62 * (Windows) host side drivers from MCCI can treat it as one submode of
63 * a proprietary scheme called "SAFE" ... without needing to know about
64 * specific product/vendor IDs. So we do that, making it easier to use
65 * those MS-Windows drivers. Those added descriptors make it resemble a
66 * CDC MDLM device, but they don't change device behavior at all. (See
67 * MCCI Engineering report 950198 "SAFE Networking Functions".)
69 * A third option is also in use. Rather than CDC Ethernet, or something
70 * simpler, Microsoft pushes their own approach: RNDIS. The published
71 * RNDIS specs are ambiguous and appear to be incomplete, and are also
72 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
74 #define ETH_ALEN 6 /* Octets in one ethernet addr */
75 #define ETH_HLEN 14 /* Total octets in header. */
76 #define ETH_ZLEN 60 /* Min. octets in frame sans FCS */
77 #define ETH_DATA_LEN 1500 /* Max. octets in payload */
78 #define ETH_FRAME_LEN PKTSIZE_ALIGN /* Max. octets in frame sans FCS */
80 #define DRIVER_DESC "Ethernet Gadget"
81 /* Based on linux 2.6.27 version */
82 #define DRIVER_VERSION "May Day 2005"
84 static const char driver_desc[] = DRIVER_DESC;
86 #define RX_EXTRA 20 /* guard against rx overflows */
88 #ifndef CONFIG_USB_ETH_RNDIS
89 #define rndis_uninit(x) do {} while (0)
90 #define rndis_deregister(c) do {} while (0)
91 #define rndis_exit() do {} while (0)
94 /* CDC and RNDIS support the same host-chosen outgoing packet filters. */
95 #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
96 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
97 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
98 |USB_CDC_PACKET_TYPE_DIRECTED)
100 #define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
102 /*-------------------------------------------------------------------------*/
105 struct usb_gadget *gadget;
106 struct usb_request *req; /* for control responses */
107 struct usb_request *stat_req; /* for cdc & rndis status */
109 struct udevice *usb_udev;
113 struct usb_ep *in_ep, *out_ep, *status_ep;
114 const struct usb_endpoint_descriptor
117 struct usb_request *tx_req, *rx_req;
119 #ifndef CONFIG_DM_ETH
120 struct eth_device *net;
124 struct net_device_stats stats;
125 unsigned int tx_qlen;
130 unsigned suspended:1;
131 unsigned network_started:1;
135 #define WORK_RX_MEMORY 0
137 u8 host_mac[ETH_ALEN];
141 * This version autoconfigures as much as possible at run-time.
143 * It also ASSUMES a self-powered device, without remote wakeup,
144 * although remote wakeup support would make sense.
147 /*-------------------------------------------------------------------------*/
149 struct eth_dev ethdev;
150 #ifndef CONFIG_DM_ETH
151 struct eth_device netdev;
153 struct udevice *netdev;
155 struct usb_gadget_driver eth_driver;
158 struct ether_priv eth_priv;
159 struct ether_priv *l_priv = ð_priv;
161 /*-------------------------------------------------------------------------*/
163 /* "main" config is either CDC, or its simple subset */
164 static inline int is_cdc(struct eth_dev *dev)
166 #if !defined(CONFIG_USB_ETH_SUBSET)
167 return 1; /* only cdc possible */
168 #elif !defined(CONFIG_USB_ETH_CDC)
169 return 0; /* only subset possible */
171 return dev->cdc; /* depends on what hardware we found */
175 /* "secondary" RNDIS config may sometimes be activated */
176 static inline int rndis_active(struct eth_dev *dev)
178 #ifdef CONFIG_USB_ETH_RNDIS
185 #define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
186 #define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev))
188 #define DEFAULT_QLEN 2 /* double buffering by default */
190 /* peak bulk transfer bits-per-second */
191 #define HS_BPS (13 * 512 * 8 * 1000 * 8)
192 #define FS_BPS (19 * 64 * 1 * 1000 * 8)
194 #ifdef CONFIG_USB_GADGET_DUALSPEED
195 #define DEVSPEED USB_SPEED_HIGH
197 #ifdef CONFIG_USB_ETH_QMULT
198 #define qmult CONFIG_USB_ETH_QMULT
203 /* for dual-speed hardware, use deeper queues at highspeed */
204 #define qlen(gadget) \
205 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
207 static inline int BITRATE(struct usb_gadget *g)
209 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
212 #else /* full speed (low speed doesn't do bulk) */
216 #define DEVSPEED USB_SPEED_FULL
218 #define qlen(gadget) DEFAULT_QLEN
220 static inline int BITRATE(struct usb_gadget *g)
226 /*-------------------------------------------------------------------------*/
229 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
230 * Instead: allocate your own, using normal USB-IF procedures.
234 * Thanks to NetChip Technologies for donating this product ID.
235 * It's for devices with only CDC Ethernet configurations.
237 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
238 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
241 * For hardware that can't talk CDC, we use the same vendor ID that
242 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
243 * with pxa250. We're protocol-compatible, if the host-side drivers
244 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
245 * drivers that need to hard-wire endpoint numbers have a hook.
247 * The protocol is a minimal subset of CDC Ether, which works on any bulk
248 * hardware that's not deeply broken ... even on hardware that can't talk
249 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
250 * doesn't handle control-OUT).
252 #define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */
253 #define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */
256 * For hardware that can talk RNDIS and either of the above protocols,
257 * use this ID ... the windows INF files will know it. Unless it's
258 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
259 * the non-RNDIS configuration.
261 #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
262 #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
265 * Some systems will want different product identifers published in the
266 * device descriptor, either numbers or strings or both. These string
267 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
271 * Emulating them in eth_bind:
272 * static ushort idVendor;
273 * static ushort idProduct;
276 #if defined(CONFIG_USB_GADGET_MANUFACTURER)
277 static char *iManufacturer = CONFIG_USB_GADGET_MANUFACTURER;
279 static char *iManufacturer = "U-Boot";
282 /* These probably need to be configurable. */
283 static ushort bcdDevice;
284 static char *iProduct;
285 static char *iSerialNumber;
287 static char dev_addr[18];
289 static char host_addr[18];
292 /*-------------------------------------------------------------------------*/
295 * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
296 * ep0 implementation: descriptors, config management, setup().
297 * also optional class-specific notification interrupt transfer.
301 * DESCRIPTORS ... most are static, but strings and (full) configuration
302 * descriptors are built on demand. For now we do either full CDC, or
303 * our simple subset, with RNDIS as an optional second configuration.
305 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
306 * the class descriptors match a modem (they're ignored; it's really just
307 * Ethernet functionality), they don't need the NOP altsetting, and the
308 * status transfer endpoint isn't optional.
311 #define STRING_MANUFACTURER 1
312 #define STRING_PRODUCT 2
313 #define STRING_ETHADDR 3
314 #define STRING_DATA 4
315 #define STRING_CONTROL 5
316 #define STRING_RNDIS_CONTROL 6
318 #define STRING_SUBSET 8
319 #define STRING_RNDIS 9
320 #define STRING_SERIALNUMBER 10
322 /* holds our biggest descriptor (or RNDIS response) */
323 #define USB_BUFSIZ 256
326 * This device advertises one configuration, eth_config, unless RNDIS
327 * is enabled (rndis_config) on hardware supporting at least two configs.
329 * NOTE: Controllers like superh_udc should probably be able to use
330 * an RNDIS-only configuration.
332 * FIXME define some higher-powered configurations to make it easier
333 * to recharge batteries ...
336 #define DEV_CONFIG_VALUE 1 /* cdc or subset */
337 #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
339 static struct usb_device_descriptor
341 .bLength = sizeof device_desc,
342 .bDescriptorType = USB_DT_DEVICE,
344 .bcdUSB = __constant_cpu_to_le16(0x0200),
346 .bDeviceClass = USB_CLASS_COMM,
347 .bDeviceSubClass = 0,
348 .bDeviceProtocol = 0,
350 .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
351 .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
352 .iManufacturer = STRING_MANUFACTURER,
353 .iProduct = STRING_PRODUCT,
354 .bNumConfigurations = 1,
357 static struct usb_otg_descriptor
359 .bLength = sizeof otg_descriptor,
360 .bDescriptorType = USB_DT_OTG,
362 .bmAttributes = USB_OTG_SRP,
365 static struct usb_config_descriptor
367 .bLength = sizeof eth_config,
368 .bDescriptorType = USB_DT_CONFIG,
370 /* compute wTotalLength on the fly */
372 .bConfigurationValue = DEV_CONFIG_VALUE,
373 .iConfiguration = STRING_CDC,
374 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
378 #ifdef CONFIG_USB_ETH_RNDIS
379 static struct usb_config_descriptor
381 .bLength = sizeof rndis_config,
382 .bDescriptorType = USB_DT_CONFIG,
384 /* compute wTotalLength on the fly */
386 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
387 .iConfiguration = STRING_RNDIS,
388 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
394 * Compared to the simple CDC subset, the full CDC Ethernet model adds
395 * three class descriptors, two interface descriptors, optional status
396 * endpoint. Both have a "data" interface and two bulk endpoints.
397 * There are also differences in how control requests are handled.
399 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
400 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
401 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
402 * work around those bugs) are unlikely to ever come from MSFT, you may
403 * wish to avoid using RNDIS.
405 * MCCI offers an alternative to RNDIS if you need to connect to Windows
406 * but have hardware that can't support CDC Ethernet. We add descriptors
407 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
408 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
409 * get those drivers from MCCI, or bundled with various products.
412 #ifdef CONFIG_USB_ETH_CDC
413 static struct usb_interface_descriptor
415 .bLength = sizeof control_intf,
416 .bDescriptorType = USB_DT_INTERFACE,
418 .bInterfaceNumber = 0,
419 /* status endpoint is optional; this may be patched later */
421 .bInterfaceClass = USB_CLASS_COMM,
422 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
423 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
424 .iInterface = STRING_CONTROL,
428 #ifdef CONFIG_USB_ETH_RNDIS
429 static const struct usb_interface_descriptor
430 rndis_control_intf = {
431 .bLength = sizeof rndis_control_intf,
432 .bDescriptorType = USB_DT_INTERFACE,
434 .bInterfaceNumber = 0,
436 .bInterfaceClass = USB_CLASS_COMM,
437 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
438 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
439 .iInterface = STRING_RNDIS_CONTROL,
443 static const struct usb_cdc_header_desc header_desc = {
444 .bLength = sizeof header_desc,
445 .bDescriptorType = USB_DT_CS_INTERFACE,
446 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
448 .bcdCDC = __constant_cpu_to_le16(0x0110),
451 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
453 static const struct usb_cdc_union_desc union_desc = {
454 .bLength = sizeof union_desc,
455 .bDescriptorType = USB_DT_CS_INTERFACE,
456 .bDescriptorSubType = USB_CDC_UNION_TYPE,
458 .bMasterInterface0 = 0, /* index of control interface */
459 .bSlaveInterface0 = 1, /* index of DATA interface */
462 #endif /* CDC || RNDIS */
464 #ifdef CONFIG_USB_ETH_RNDIS
466 static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
467 .bLength = sizeof call_mgmt_descriptor,
468 .bDescriptorType = USB_DT_CS_INTERFACE,
469 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
471 .bmCapabilities = 0x00,
472 .bDataInterface = 0x01,
475 static const struct usb_cdc_acm_descriptor acm_descriptor = {
476 .bLength = sizeof acm_descriptor,
477 .bDescriptorType = USB_DT_CS_INTERFACE,
478 .bDescriptorSubType = USB_CDC_ACM_TYPE,
480 .bmCapabilities = 0x00,
485 #ifndef CONFIG_USB_ETH_CDC
488 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
489 * ways: data endpoints live in the control interface, there's no data
490 * interface, and it's not used to talk to a cell phone radio.
493 static const struct usb_cdc_mdlm_desc mdlm_desc = {
494 .bLength = sizeof mdlm_desc,
495 .bDescriptorType = USB_DT_CS_INTERFACE,
496 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
498 .bcdVersion = __constant_cpu_to_le16(0x0100),
500 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
501 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
506 * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
507 * can't really use its struct. All we do here is say that we're using
508 * the submode of "SAFE" which directly matches the CDC Subset.
510 #ifdef CONFIG_USB_ETH_SUBSET
511 static const u8 mdlm_detail_desc[] = {
514 USB_CDC_MDLM_DETAIL_TYPE,
517 0, /* network control capabilities (none) */
518 0, /* network data capabilities ("raw" encapsulation) */
524 static const struct usb_cdc_ether_desc ether_desc = {
525 .bLength = sizeof(ether_desc),
526 .bDescriptorType = USB_DT_CS_INTERFACE,
527 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
529 /* this descriptor actually adds value, surprise! */
530 .iMACAddress = STRING_ETHADDR,
531 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
532 .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN),
533 .wNumberMCFilters = __constant_cpu_to_le16(0),
534 .bNumberPowerFilters = 0,
537 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
540 * include the status endpoint if we can, even where it's optional.
541 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
542 * packet, to simplify cancellation; and a big transfer interval, to
543 * waste less bandwidth.
545 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
546 * if they ignore the connect/disconnect notifications that real aether
547 * can provide. more advanced cdc configurations might want to support
548 * encapsulated commands (vendor-specific, using control-OUT).
550 * RNDIS requires the status endpoint, since it uses that encapsulation
551 * mechanism for its funky RPC scheme.
554 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
555 #define STATUS_BYTECOUNT 16 /* 8 byte header + data */
557 static struct usb_endpoint_descriptor
559 .bLength = USB_DT_ENDPOINT_SIZE,
560 .bDescriptorType = USB_DT_ENDPOINT,
562 .bEndpointAddress = USB_DIR_IN,
563 .bmAttributes = USB_ENDPOINT_XFER_INT,
564 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
565 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
569 #ifdef CONFIG_USB_ETH_CDC
571 /* the default data interface has no endpoints ... */
573 static const struct usb_interface_descriptor
575 .bLength = sizeof data_nop_intf,
576 .bDescriptorType = USB_DT_INTERFACE,
578 .bInterfaceNumber = 1,
579 .bAlternateSetting = 0,
581 .bInterfaceClass = USB_CLASS_CDC_DATA,
582 .bInterfaceSubClass = 0,
583 .bInterfaceProtocol = 0,
586 /* ... but the "real" data interface has two bulk endpoints */
588 static const struct usb_interface_descriptor
590 .bLength = sizeof data_intf,
591 .bDescriptorType = USB_DT_INTERFACE,
593 .bInterfaceNumber = 1,
594 .bAlternateSetting = 1,
596 .bInterfaceClass = USB_CLASS_CDC_DATA,
597 .bInterfaceSubClass = 0,
598 .bInterfaceProtocol = 0,
599 .iInterface = STRING_DATA,
604 #ifdef CONFIG_USB_ETH_RNDIS
606 /* RNDIS doesn't activate by changing to the "real" altsetting */
608 static const struct usb_interface_descriptor
610 .bLength = sizeof rndis_data_intf,
611 .bDescriptorType = USB_DT_INTERFACE,
613 .bInterfaceNumber = 1,
614 .bAlternateSetting = 0,
616 .bInterfaceClass = USB_CLASS_CDC_DATA,
617 .bInterfaceSubClass = 0,
618 .bInterfaceProtocol = 0,
619 .iInterface = STRING_DATA,
624 #ifdef CONFIG_USB_ETH_SUBSET
627 * "Simple" CDC-subset option is a simple vendor-neutral model that most
628 * full speed controllers can handle: one interface, two bulk endpoints.
630 * To assist host side drivers, we fancy it up a bit, and add descriptors
631 * so some host side drivers will understand it as a "SAFE" variant.
634 static const struct usb_interface_descriptor
636 .bLength = sizeof subset_data_intf,
637 .bDescriptorType = USB_DT_INTERFACE,
639 .bInterfaceNumber = 0,
640 .bAlternateSetting = 0,
642 .bInterfaceClass = USB_CLASS_COMM,
643 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
644 .bInterfaceProtocol = 0,
645 .iInterface = STRING_DATA,
650 static struct usb_endpoint_descriptor
652 .bLength = USB_DT_ENDPOINT_SIZE,
653 .bDescriptorType = USB_DT_ENDPOINT,
655 .bEndpointAddress = USB_DIR_IN,
656 .bmAttributes = USB_ENDPOINT_XFER_BULK,
657 .wMaxPacketSize = __constant_cpu_to_le16(64),
660 static struct usb_endpoint_descriptor
662 .bLength = USB_DT_ENDPOINT_SIZE,
663 .bDescriptorType = USB_DT_ENDPOINT,
665 .bEndpointAddress = USB_DIR_OUT,
666 .bmAttributes = USB_ENDPOINT_XFER_BULK,
667 .wMaxPacketSize = __constant_cpu_to_le16(64),
670 static const struct usb_descriptor_header *fs_eth_function[11] = {
671 (struct usb_descriptor_header *) &otg_descriptor,
672 #ifdef CONFIG_USB_ETH_CDC
673 /* "cdc" mode descriptors */
674 (struct usb_descriptor_header *) &control_intf,
675 (struct usb_descriptor_header *) &header_desc,
676 (struct usb_descriptor_header *) &union_desc,
677 (struct usb_descriptor_header *) ðer_desc,
678 /* NOTE: status endpoint may need to be removed */
679 (struct usb_descriptor_header *) &fs_status_desc,
680 /* data interface, with altsetting */
681 (struct usb_descriptor_header *) &data_nop_intf,
682 (struct usb_descriptor_header *) &data_intf,
683 (struct usb_descriptor_header *) &fs_source_desc,
684 (struct usb_descriptor_header *) &fs_sink_desc,
686 #endif /* CONFIG_USB_ETH_CDC */
689 static inline void fs_subset_descriptors(void)
691 #ifdef CONFIG_USB_ETH_SUBSET
692 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
693 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
694 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
695 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
696 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
697 fs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
698 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
699 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
700 fs_eth_function[8] = NULL;
702 fs_eth_function[1] = NULL;
706 #ifdef CONFIG_USB_ETH_RNDIS
707 static const struct usb_descriptor_header *fs_rndis_function[] = {
708 (struct usb_descriptor_header *) &otg_descriptor,
709 /* control interface matches ACM, not Ethernet */
710 (struct usb_descriptor_header *) &rndis_control_intf,
711 (struct usb_descriptor_header *) &header_desc,
712 (struct usb_descriptor_header *) &call_mgmt_descriptor,
713 (struct usb_descriptor_header *) &acm_descriptor,
714 (struct usb_descriptor_header *) &union_desc,
715 (struct usb_descriptor_header *) &fs_status_desc,
716 /* data interface has no altsetting */
717 (struct usb_descriptor_header *) &rndis_data_intf,
718 (struct usb_descriptor_header *) &fs_source_desc,
719 (struct usb_descriptor_header *) &fs_sink_desc,
725 * usb 2.0 devices need to expose both high speed and full speed
726 * descriptors, unless they only run at full speed.
729 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
730 static struct usb_endpoint_descriptor
732 .bLength = USB_DT_ENDPOINT_SIZE,
733 .bDescriptorType = USB_DT_ENDPOINT,
735 .bmAttributes = USB_ENDPOINT_XFER_INT,
736 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
737 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
739 #endif /* CONFIG_USB_ETH_CDC */
741 static struct usb_endpoint_descriptor
743 .bLength = USB_DT_ENDPOINT_SIZE,
744 .bDescriptorType = USB_DT_ENDPOINT,
746 .bmAttributes = USB_ENDPOINT_XFER_BULK,
747 .wMaxPacketSize = __constant_cpu_to_le16(512),
750 static struct usb_endpoint_descriptor
752 .bLength = USB_DT_ENDPOINT_SIZE,
753 .bDescriptorType = USB_DT_ENDPOINT,
755 .bmAttributes = USB_ENDPOINT_XFER_BULK,
756 .wMaxPacketSize = __constant_cpu_to_le16(512),
759 static struct usb_qualifier_descriptor
761 .bLength = sizeof dev_qualifier,
762 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
764 .bcdUSB = __constant_cpu_to_le16(0x0200),
765 .bDeviceClass = USB_CLASS_COMM,
767 .bNumConfigurations = 1,
770 static const struct usb_descriptor_header *hs_eth_function[11] = {
771 (struct usb_descriptor_header *) &otg_descriptor,
772 #ifdef CONFIG_USB_ETH_CDC
773 /* "cdc" mode descriptors */
774 (struct usb_descriptor_header *) &control_intf,
775 (struct usb_descriptor_header *) &header_desc,
776 (struct usb_descriptor_header *) &union_desc,
777 (struct usb_descriptor_header *) ðer_desc,
778 /* NOTE: status endpoint may need to be removed */
779 (struct usb_descriptor_header *) &hs_status_desc,
780 /* data interface, with altsetting */
781 (struct usb_descriptor_header *) &data_nop_intf,
782 (struct usb_descriptor_header *) &data_intf,
783 (struct usb_descriptor_header *) &hs_source_desc,
784 (struct usb_descriptor_header *) &hs_sink_desc,
786 #endif /* CONFIG_USB_ETH_CDC */
789 static inline void hs_subset_descriptors(void)
791 #ifdef CONFIG_USB_ETH_SUBSET
792 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
793 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
794 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
795 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
796 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
797 hs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
798 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
799 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
800 hs_eth_function[8] = NULL;
802 hs_eth_function[1] = NULL;
806 #ifdef CONFIG_USB_ETH_RNDIS
807 static const struct usb_descriptor_header *hs_rndis_function[] = {
808 (struct usb_descriptor_header *) &otg_descriptor,
809 /* control interface matches ACM, not Ethernet */
810 (struct usb_descriptor_header *) &rndis_control_intf,
811 (struct usb_descriptor_header *) &header_desc,
812 (struct usb_descriptor_header *) &call_mgmt_descriptor,
813 (struct usb_descriptor_header *) &acm_descriptor,
814 (struct usb_descriptor_header *) &union_desc,
815 (struct usb_descriptor_header *) &hs_status_desc,
816 /* data interface has no altsetting */
817 (struct usb_descriptor_header *) &rndis_data_intf,
818 (struct usb_descriptor_header *) &hs_source_desc,
819 (struct usb_descriptor_header *) &hs_sink_desc,
825 /* maxpacket and other transfer characteristics vary by speed. */
826 static inline struct usb_endpoint_descriptor *
827 ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
828 struct usb_endpoint_descriptor *fs)
830 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
835 /*-------------------------------------------------------------------------*/
837 /* descriptors that are built on-demand */
839 static char manufacturer[50];
840 static char product_desc[40] = DRIVER_DESC;
841 static char serial_number[20];
843 /* address that the host will use ... usually assigned at random */
844 static char ethaddr[2 * ETH_ALEN + 1];
846 /* static strings, in UTF-8 */
847 static struct usb_string strings[] = {
848 { STRING_MANUFACTURER, manufacturer, },
849 { STRING_PRODUCT, product_desc, },
850 { STRING_SERIALNUMBER, serial_number, },
851 { STRING_DATA, "Ethernet Data", },
852 { STRING_ETHADDR, ethaddr, },
853 #ifdef CONFIG_USB_ETH_CDC
854 { STRING_CDC, "CDC Ethernet", },
855 { STRING_CONTROL, "CDC Communications Control", },
857 #ifdef CONFIG_USB_ETH_SUBSET
858 { STRING_SUBSET, "CDC Ethernet Subset", },
860 #ifdef CONFIG_USB_ETH_RNDIS
861 { STRING_RNDIS, "RNDIS", },
862 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
864 { } /* end of list */
867 static struct usb_gadget_strings stringtab = {
868 .language = 0x0409, /* en-us */
872 /*============================================================================*/
873 DEFINE_CACHE_ALIGN_BUFFER(u8, control_req, USB_BUFSIZ);
875 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
876 DEFINE_CACHE_ALIGN_BUFFER(u8, status_req, STATUS_BYTECOUNT);
879 /*============================================================================*/
882 * one config, two interfaces: control, data.
883 * complications: class descriptors, and an altsetting.
886 config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
889 const struct usb_config_descriptor *config;
890 const struct usb_descriptor_header **function;
893 if (gadget_is_dualspeed(g)) {
894 hs = (g->speed == USB_SPEED_HIGH);
895 if (type == USB_DT_OTHER_SPEED_CONFIG)
898 #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
900 if (index >= device_desc.bNumConfigurations)
903 #ifdef CONFIG_USB_ETH_RNDIS
905 * list the RNDIS config first, to make Microsoft's drivers
906 * happy. DOCSIS 1.0 needs this too.
908 if (device_desc.bNumConfigurations == 2 && index == 0) {
909 config = &rndis_config;
910 function = which_fn(rndis);
914 config = ð_config;
915 function = which_fn(eth);
918 /* for now, don't advertise srp-only devices */
922 len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
925 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
929 /*-------------------------------------------------------------------------*/
931 static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
932 static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
935 set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
938 struct usb_gadget *gadget = dev->gadget;
940 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
941 /* status endpoint used for RNDIS and (optionally) CDC */
942 if (!subset_active(dev) && dev->status_ep) {
943 dev->status = ep_desc(gadget, &hs_status_desc,
945 dev->status_ep->driver_data = dev;
947 result = usb_ep_enable(dev->status_ep, dev->status);
949 debug("enable %s --> %d\n",
950 dev->status_ep->name, result);
956 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
957 dev->in_ep->driver_data = dev;
959 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
960 dev->out_ep->driver_data = dev;
963 * With CDC, the host isn't allowed to use these two data
964 * endpoints in the default altsetting for the interface.
965 * so we don't activate them yet. Reset from SET_INTERFACE.
967 * Strictly speaking RNDIS should work the same: activation is
968 * a side effect of setting a packet filter. Deactivation is
969 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
971 if (!cdc_active(dev)) {
972 result = usb_ep_enable(dev->in_ep, dev->in);
974 debug("enable %s --> %d\n",
975 dev->in_ep->name, result);
979 result = usb_ep_enable(dev->out_ep, dev->out);
981 debug("enable %s --> %d\n",
982 dev->out_ep->name, result);
989 result = alloc_requests(dev, qlen(gadget), gfp_flags);
991 /* on error, disable any endpoints */
993 if (!subset_active(dev) && dev->status_ep)
994 (void) usb_ep_disable(dev->status_ep);
996 (void) usb_ep_disable(dev->in_ep);
997 (void) usb_ep_disable(dev->out_ep);
1000 } else if (!cdc_active(dev)) {
1002 * activate non-CDC configs right away
1003 * this isn't strictly according to the RNDIS spec
1005 eth_start(dev, GFP_ATOMIC);
1008 /* caller is responsible for cleanup on error */
1012 static void eth_reset_config(struct eth_dev *dev)
1014 if (dev->config == 0)
1017 debug("%s\n", __func__);
1019 rndis_uninit(dev->rndis_config);
1022 * disable endpoints, forcing (synchronous) completion of
1023 * pending i/o. then free the requests.
1027 usb_ep_disable(dev->in_ep);
1029 usb_ep_free_request(dev->in_ep, dev->tx_req);
1034 usb_ep_disable(dev->out_ep);
1036 usb_ep_free_request(dev->out_ep, dev->rx_req);
1041 usb_ep_disable(dev->status_ep);
1044 dev->cdc_filter = 0;
1049 * change our operational config. must agree with the code
1050 * that returns config descriptors, and altsetting code.
1052 static int eth_set_config(struct eth_dev *dev, unsigned number,
1056 struct usb_gadget *gadget = dev->gadget;
1058 if (gadget_is_sa1100(gadget)
1060 && dev->tx_qlen != 0) {
1061 /* tx fifo is full, but we can't clear it...*/
1062 pr_err("can't change configurations");
1065 eth_reset_config(dev);
1068 case DEV_CONFIG_VALUE:
1069 result = set_ether_config(dev, gfp_flags);
1071 #ifdef CONFIG_USB_ETH_RNDIS
1072 case DEV_RNDIS_CONFIG_VALUE:
1074 result = set_ether_config(dev, gfp_flags);
1086 eth_reset_config(dev);
1087 usb_gadget_vbus_draw(dev->gadget,
1088 gadget_is_otg(dev->gadget) ? 8 : 100);
1093 power = 2 * eth_config.bMaxPower;
1094 usb_gadget_vbus_draw(dev->gadget, power);
1096 switch (gadget->speed) {
1097 case USB_SPEED_FULL:
1098 speed = "full"; break;
1099 #ifdef CONFIG_USB_GADGET_DUALSPEED
1100 case USB_SPEED_HIGH:
1101 speed = "high"; break;
1107 dev->config = number;
1108 printf("%s speed config #%d: %d mA, %s, using %s\n",
1109 speed, number, power, driver_desc,
1114 : "CDC Ethernet Subset"));
1119 /*-------------------------------------------------------------------------*/
1121 #ifdef CONFIG_USB_ETH_CDC
1124 * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
1125 * only to notify the host about link status changes (which we support) or
1126 * report completion of some encapsulated command (as used in RNDIS). Since
1127 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1128 * command mechanism; and only one status request is ever queued.
1130 static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
1132 struct usb_cdc_notification *event = req->buf;
1133 int value = req->status;
1134 struct eth_dev *dev = ep->driver_data;
1136 /* issue the second notification if host reads the first */
1137 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1139 __le32 *data = req->buf + sizeof *event;
1141 event->bmRequestType = 0xA1;
1142 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
1143 event->wValue = __constant_cpu_to_le16(0);
1144 event->wIndex = __constant_cpu_to_le16(1);
1145 event->wLength = __constant_cpu_to_le16(8);
1147 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1148 data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
1150 req->length = STATUS_BYTECOUNT;
1151 value = usb_ep_queue(ep, req, GFP_ATOMIC);
1152 debug("send SPEED_CHANGE --> %d\n", value);
1155 } else if (value != -ECONNRESET) {
1156 debug("event %02x --> %d\n",
1157 event->bNotificationType, value);
1158 if (event->bNotificationType ==
1159 USB_CDC_NOTIFY_SPEED_CHANGE) {
1160 dev->network_started = 1;
1161 printf("USB network up!\n");
1164 req->context = NULL;
1167 static void issue_start_status(struct eth_dev *dev)
1169 struct usb_request *req = dev->stat_req;
1170 struct usb_cdc_notification *event;
1176 * FIXME ugly idiom, maybe we'd be better with just
1177 * a "cancel the whole queue" primitive since any
1178 * unlink-one primitive has way too many error modes.
1179 * here, we "know" toggle is already clear...
1181 * FIXME iff req->context != null just dequeue it
1183 usb_ep_disable(dev->status_ep);
1184 usb_ep_enable(dev->status_ep, dev->status);
1187 * 3.8.1 says to issue first NETWORK_CONNECTION, then
1188 * a SPEED_CHANGE. could be useful in some configs.
1191 event->bmRequestType = 0xA1;
1192 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
1193 event->wValue = __constant_cpu_to_le16(1); /* connected */
1194 event->wIndex = __constant_cpu_to_le16(1);
1197 req->length = sizeof *event;
1198 req->complete = eth_status_complete;
1201 value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
1203 debug("status buf queue --> %d\n", value);
1208 /*-------------------------------------------------------------------------*/
1210 static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
1212 if (req->status || req->actual != req->length)
1213 debug("setup complete --> %d, %d/%d\n",
1214 req->status, req->actual, req->length);
1217 #ifdef CONFIG_USB_ETH_RNDIS
1219 static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
1221 if (req->status || req->actual != req->length)
1222 debug("rndis response complete --> %d, %d/%d\n",
1223 req->status, req->actual, req->length);
1225 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1228 static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
1230 struct eth_dev *dev = ep->driver_data;
1233 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1234 status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
1236 pr_err("%s: rndis parse error %d", __func__, status);
1242 * The setup() callback implements all the ep0 functionality that's not
1243 * handled lower down. CDC has a number of less-common features:
1245 * - two interfaces: control, and ethernet data
1246 * - Ethernet data interface has two altsettings: default, and active
1247 * - class-specific descriptors for the control interface
1248 * - class-specific control requests
1251 eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1253 struct eth_dev *dev = get_gadget_data(gadget);
1254 struct usb_request *req = dev->req;
1255 int value = -EOPNOTSUPP;
1256 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1257 u16 wValue = le16_to_cpu(ctrl->wValue);
1258 u16 wLength = le16_to_cpu(ctrl->wLength);
1261 * descriptors just go into the pre-allocated ep0 buffer,
1262 * while config change events may enable network traffic.
1265 debug("%s\n", __func__);
1267 req->complete = eth_setup_complete;
1268 switch (ctrl->bRequest) {
1270 case USB_REQ_GET_DESCRIPTOR:
1271 if (ctrl->bRequestType != USB_DIR_IN)
1273 switch (wValue >> 8) {
1276 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1277 value = min(wLength, (u16) sizeof device_desc);
1278 memcpy(req->buf, &device_desc, value);
1280 case USB_DT_DEVICE_QUALIFIER:
1281 if (!gadget_is_dualspeed(gadget))
1283 value = min(wLength, (u16) sizeof dev_qualifier);
1284 memcpy(req->buf, &dev_qualifier, value);
1287 case USB_DT_OTHER_SPEED_CONFIG:
1288 if (!gadget_is_dualspeed(gadget))
1292 value = config_buf(gadget, req->buf,
1295 gadget_is_otg(gadget));
1297 value = min(wLength, (u16) value);
1301 value = usb_gadget_get_string(&stringtab,
1302 wValue & 0xff, req->buf);
1305 value = min(wLength, (u16) value);
1311 case USB_REQ_SET_CONFIGURATION:
1312 if (ctrl->bRequestType != 0)
1314 if (gadget->a_hnp_support)
1315 debug("HNP available\n");
1316 else if (gadget->a_alt_hnp_support)
1317 debug("HNP needs a different root port\n");
1318 value = eth_set_config(dev, wValue, GFP_ATOMIC);
1320 case USB_REQ_GET_CONFIGURATION:
1321 if (ctrl->bRequestType != USB_DIR_IN)
1323 *(u8 *)req->buf = dev->config;
1324 value = min(wLength, (u16) 1);
1327 case USB_REQ_SET_INTERFACE:
1328 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1332 if (!cdc_active(dev) && wIndex != 0)
1336 * PXA hardware partially handles SET_INTERFACE;
1337 * we need to kluge around that interference.
1339 if (gadget_is_pxa(gadget)) {
1340 value = eth_set_config(dev, DEV_CONFIG_VALUE,
1343 * PXA25x driver use non-CDC ethernet gadget.
1344 * But only _CDC and _RNDIS code can signalize
1345 * that network is working. So we signalize it
1348 dev->network_started = 1;
1349 debug("USB network up!\n");
1353 #ifdef CONFIG_USB_ETH_CDC
1355 case 0: /* control/master intf */
1359 usb_ep_disable(dev->status_ep);
1360 usb_ep_enable(dev->status_ep, dev->status);
1365 case 1: /* data intf */
1368 usb_ep_disable(dev->in_ep);
1369 usb_ep_disable(dev->out_ep);
1372 * CDC requires the data transfers not be done from
1373 * the default interface setting ... also, setting
1374 * the non-default interface resets filters etc.
1377 if (!cdc_active(dev))
1379 usb_ep_enable(dev->in_ep, dev->in);
1380 usb_ep_enable(dev->out_ep, dev->out);
1381 dev->cdc_filter = DEFAULT_FILTER;
1383 issue_start_status(dev);
1384 eth_start(dev, GFP_ATOMIC);
1391 * FIXME this is wrong, as is the assumption that
1392 * all non-PXA hardware talks real CDC ...
1394 debug("set_interface ignored!\n");
1395 #endif /* CONFIG_USB_ETH_CDC */
1399 case USB_REQ_GET_INTERFACE:
1400 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1404 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
1407 /* for CDC, iff carrier is on, data interface is active. */
1408 if (rndis_active(dev) || wIndex != 1)
1409 *(u8 *)req->buf = 0;
1411 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1412 /* carrier always ok ...*/
1413 *(u8 *)req->buf = 1 ;
1415 value = min(wLength, (u16) 1);
1418 #ifdef CONFIG_USB_ETH_CDC
1419 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
1421 * see 6.2.30: no data, wIndex = interface,
1422 * wValue = packet filter bitmap
1424 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1429 debug("packet filter %02x\n", wValue);
1430 dev->cdc_filter = wValue;
1436 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1437 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1438 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1439 * case USB_CDC_GET_ETHERNET_STATISTIC:
1442 #endif /* CONFIG_USB_ETH_CDC */
1444 #ifdef CONFIG_USB_ETH_RNDIS
1446 * RNDIS uses the CDC command encapsulation mechanism to implement
1447 * an RPC scheme, with much getting/setting of attributes by OID.
1449 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1450 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1451 || !rndis_active(dev)
1452 || wLength > USB_BUFSIZ
1454 || rndis_control_intf.bInterfaceNumber
1457 /* read the request, then process it */
1459 req->complete = rndis_command_complete;
1460 /* later, rndis_control_ack () sends a notification */
1463 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1464 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1465 == ctrl->bRequestType
1466 && rndis_active(dev)
1467 /* && wLength >= 0x0400 */
1469 && rndis_control_intf.bInterfaceNumber
1474 /* return the result */
1475 buf = rndis_get_next_response(dev->rndis_config, &n);
1477 memcpy(req->buf, buf, n);
1478 req->complete = rndis_response_complete;
1479 rndis_free_response(dev->rndis_config, buf);
1482 /* else stalls ... spec says to avoid that */
1488 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
1489 ctrl->bRequestType, ctrl->bRequest,
1490 wValue, wIndex, wLength);
1493 /* respond with data transfer before status phase? */
1495 debug("respond with data transfer before status phase\n");
1496 req->length = value;
1497 req->zero = value < wLength
1498 && (value % gadget->ep0->maxpacket) == 0;
1499 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1501 debug("ep_queue --> %d\n", value);
1503 eth_setup_complete(gadget->ep0, req);
1507 /* host either stalls (value < 0) or reports success */
1511 /*-------------------------------------------------------------------------*/
1513 static void rx_complete(struct usb_ep *ep, struct usb_request *req);
1515 static int rx_submit(struct eth_dev *dev, struct usb_request *req,
1518 int retval = -ENOMEM;
1522 * Padding up to RX_EXTRA handles minor disagreements with host.
1523 * Normally we use the USB "terminate on short read" convention;
1524 * so allow up to (N*maxpacket), since that memory is normally
1525 * already allocated. Some hardware doesn't deal well with short
1526 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1527 * byte off the end (to force hardware errors on overflow).
1529 * RNDIS uses internal framing, and explicitly allows senders to
1530 * pad to end-of-packet. That's potentially nice for speed,
1531 * but means receivers can't recover synch on their own.
1534 debug("%s\n", __func__);
1538 size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1539 size += dev->out_ep->maxpacket - 1;
1540 if (rndis_active(dev))
1541 size += sizeof(struct rndis_packet_msg_type);
1542 size -= size % dev->out_ep->maxpacket;
1545 * Some platforms perform better when IP packets are aligned,
1546 * but on at least one, checksumming fails otherwise. Note:
1547 * RNDIS headers involve variable numbers of LE32 values.
1550 req->buf = (u8 *)net_rx_packets[0];
1552 req->complete = rx_complete;
1554 retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
1557 pr_err("rx submit --> %d", retval);
1562 static void rx_complete(struct usb_ep *ep, struct usb_request *req)
1564 struct eth_dev *dev = ep->driver_data;
1566 debug("%s: status %d\n", __func__, req->status);
1567 switch (req->status) {
1568 /* normal completion */
1570 if (rndis_active(dev)) {
1571 /* we know MaxPacketsPerTransfer == 1 here */
1572 int length = rndis_rm_hdr(req->buf, req->actual);
1575 req->length -= length;
1576 req->actual -= length;
1578 if (req->actual < ETH_HLEN || ETH_FRAME_LEN < req->actual) {
1580 dev->stats.rx_errors++;
1581 dev->stats.rx_length_errors++;
1582 debug("rx length %d\n", req->length);
1586 dev->stats.rx_packets++;
1587 dev->stats.rx_bytes += req->length;
1590 /* software-driven interface shutdown */
1591 case -ECONNRESET: /* unlink */
1592 case -ESHUTDOWN: /* disconnect etc */
1593 /* for hardware automagic (such as pxa) */
1594 case -ECONNABORTED: /* endpoint reset */
1599 dev->stats.rx_over_errors++;
1602 dev->stats.rx_errors++;
1606 packet_received = 1;
1609 static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
1612 dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
1617 dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
1625 usb_ep_free_request(dev->in_ep, dev->tx_req);
1627 pr_err("can't alloc requests");
1631 static void tx_complete(struct usb_ep *ep, struct usb_request *req)
1633 struct eth_dev *dev = ep->driver_data;
1635 debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
1636 switch (req->status) {
1638 dev->stats.tx_errors++;
1639 debug("tx err %d\n", req->status);
1641 case -ECONNRESET: /* unlink */
1642 case -ESHUTDOWN: /* disconnect etc */
1645 dev->stats.tx_bytes += req->length;
1647 dev->stats.tx_packets++;
1652 static inline int eth_is_promisc(struct eth_dev *dev)
1654 /* no filters for the CDC subset; always promisc */
1655 if (subset_active(dev))
1657 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1661 static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1663 struct eth_dev *dev = netdev_priv(net);
1664 int length = skb->len;
1666 struct usb_request *req = NULL;
1667 unsigned long flags;
1669 /* apply outgoing CDC or RNDIS filters */
1670 if (!eth_is_promisc (dev)) {
1671 u8 *dest = skb->data;
1673 if (is_multicast_ethaddr(dest)) {
1676 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1677 * SET_ETHERNET_MULTICAST_FILTERS requests
1679 if (is_broadcast_ethaddr(dest))
1680 type = USB_CDC_PACKET_TYPE_BROADCAST;
1682 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1683 if (!(dev->cdc_filter & type)) {
1684 dev_kfree_skb_any (skb);
1688 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1691 spin_lock_irqsave(&dev->req_lock, flags);
1693 * this freelist can be empty if an interrupt triggered disconnect()
1694 * and reconfigured the gadget (shutting down this queue) after the
1695 * network stack decided to xmit but before we got the spinlock.
1697 if (list_empty(&dev->tx_reqs)) {
1698 spin_unlock_irqrestore(&dev->req_lock, flags);
1702 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1703 list_del (&req->list);
1705 /* temporarily stop TX queue when the freelist empties */
1706 if (list_empty (&dev->tx_reqs))
1707 netif_stop_queue (net);
1708 spin_unlock_irqrestore(&dev->req_lock, flags);
1710 /* no buffer copies needed, unless the network stack did it
1711 * or the hardware can't use skb buffers.
1712 * or there's not enough space for any RNDIS headers we need
1714 if (rndis_active(dev)) {
1715 struct sk_buff *skb_rndis;
1717 skb_rndis = skb_realloc_headroom (skb,
1718 sizeof (struct rndis_packet_msg_type));
1722 dev_kfree_skb_any (skb);
1724 rndis_add_hdr (skb);
1727 req->buf = skb->data;
1729 req->complete = tx_complete;
1731 /* use zlp framing on tx for strict CDC-Ether conformance,
1732 * though any robust network rx path ignores extra padding.
1733 * and some hardware doesn't like to write zlps.
1736 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1739 req->length = length;
1741 /* throttle highspeed IRQ rate back slightly */
1742 if (gadget_is_dualspeed(dev->gadget))
1743 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1744 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1747 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1750 DEBUG (dev, "tx queue err %d\n", retval);
1753 net->trans_start = jiffies;
1754 atomic_inc (&dev->tx_qlen);
1759 dev->stats.tx_dropped++;
1760 dev_kfree_skb_any (skb);
1761 spin_lock_irqsave(&dev->req_lock, flags);
1762 if (list_empty (&dev->tx_reqs))
1763 netif_start_queue (net);
1764 list_add (&req->list, &dev->tx_reqs);
1765 spin_unlock_irqrestore(&dev->req_lock, flags);
1770 /*-------------------------------------------------------------------------*/
1773 static void eth_unbind(struct usb_gadget *gadget)
1775 struct eth_dev *dev = get_gadget_data(gadget);
1777 debug("%s...\n", __func__);
1778 rndis_deregister(dev->rndis_config);
1781 /* we've already been disconnected ... no i/o is active */
1783 usb_ep_free_request(gadget->ep0, dev->req);
1786 if (dev->stat_req) {
1787 usb_ep_free_request(dev->status_ep, dev->stat_req);
1788 dev->stat_req = NULL;
1792 usb_ep_free_request(dev->in_ep, dev->tx_req);
1797 usb_ep_free_request(dev->out_ep, dev->rx_req);
1801 /* unregister_netdev (dev->net);*/
1802 /* free_netdev(dev->net);*/
1805 set_gadget_data(gadget, NULL);
1808 static void eth_disconnect(struct usb_gadget *gadget)
1810 eth_reset_config(get_gadget_data(gadget));
1811 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1814 static void eth_suspend(struct usb_gadget *gadget)
1819 static void eth_resume(struct usb_gadget *gadget)
1824 /*-------------------------------------------------------------------------*/
1826 #ifdef CONFIG_USB_ETH_RNDIS
1829 * The interrupt endpoint is used in RNDIS to notify the host when messages
1830 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1831 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1832 * REMOTE_NDIS_KEEPALIVE_MSG.
1834 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1835 * normally just one notification will be queued.
1838 static void rndis_control_ack_complete(struct usb_ep *ep,
1839 struct usb_request *req)
1841 struct eth_dev *dev = ep->driver_data;
1843 debug("%s...\n", __func__);
1844 if (req->status || req->actual != req->length)
1845 debug("rndis control ack complete --> %d, %d/%d\n",
1846 req->status, req->actual, req->length);
1848 if (!dev->network_started) {
1849 if (rndis_get_state(dev->rndis_config)
1850 == RNDIS_DATA_INITIALIZED) {
1851 dev->network_started = 1;
1852 printf("USB RNDIS network up!\n");
1856 req->context = NULL;
1858 if (req != dev->stat_req)
1859 usb_ep_free_request(ep, req);
1862 static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
1864 #ifndef CONFIG_DM_ETH
1865 static int rndis_control_ack(struct eth_device *net)
1867 static int rndis_control_ack(struct udevice *net)
1870 struct ether_priv *priv = (struct ether_priv *)net->priv;
1871 struct eth_dev *dev = &priv->ethdev;
1873 struct usb_request *resp = dev->stat_req;
1875 /* in case RNDIS calls this after disconnect */
1877 debug("status ENODEV\n");
1881 /* in case queue length > 1 */
1882 if (resp->context) {
1883 resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
1886 resp->buf = rndis_resp_buf;
1890 * Send RNDIS RESPONSE_AVAILABLE notification;
1891 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1894 resp->complete = rndis_control_ack_complete;
1895 resp->context = dev;
1897 *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
1898 *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
1900 length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
1903 rndis_control_ack_complete(dev->status_ep, resp);
1911 #define rndis_control_ack NULL
1915 static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
1917 if (rndis_active(dev)) {
1918 rndis_set_param_medium(dev->rndis_config,
1920 BITRATE(dev->gadget)/100);
1921 rndis_signal_connect(dev->rndis_config);
1925 static int eth_stop(struct eth_dev *dev)
1927 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1929 unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1932 if (rndis_active(dev)) {
1933 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
1934 rndis_signal_disconnect(dev->rndis_config);
1936 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1937 /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1939 while (get_timer(ts) < timeout)
1940 usb_gadget_handle_interrupts(0);
1943 rndis_uninit(dev->rndis_config);
1950 /*-------------------------------------------------------------------------*/
1952 static int is_eth_addr_valid(char *str)
1954 if (strlen(str) == 17) {
1959 /* see if it looks like an ethernet address */
1963 for (i = 0; i < 6; i++) {
1964 char term = (i == 5 ? '\0' : ':');
1966 ea[i] = simple_strtol(p, &q, 16);
1968 if ((q - p) != 2 || *q++ != term)
1974 /* Now check the contents. */
1975 return is_valid_ethaddr(ea);
1980 static u8 nibble(unsigned char c)
1982 if (likely(isdigit(c)))
1985 if (likely(isxdigit(c)))
1986 return 10 + c - 'A';
1990 static int get_ether_addr(const char *str, u8 *dev_addr)
1995 for (i = 0; i < 6; i++) {
1998 if ((*str == '.') || (*str == ':'))
2000 num = nibble(*str++) << 4;
2001 num |= (nibble(*str++));
2004 if (is_valid_ethaddr(dev_addr))
2010 static int eth_bind(struct usb_gadget *gadget)
2012 struct eth_dev *dev = &l_priv->ethdev;
2013 u8 cdc = 1, zlp = 1, rndis = 1;
2014 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
2015 int status = -ENOMEM;
2018 #ifdef CONFIG_DM_ETH
2019 struct eth_pdata *pdata = dev_get_platdata(l_priv->netdev);
2022 /* these flags are only ever cleared; compiler take note */
2023 #ifndef CONFIG_USB_ETH_CDC
2026 #ifndef CONFIG_USB_ETH_RNDIS
2030 * Because most host side USB stacks handle CDC Ethernet, that
2031 * standard protocol is _strongly_ preferred for interop purposes.
2032 * (By everyone except Microsoft.)
2034 if (gadget_is_pxa(gadget)) {
2035 /* pxa doesn't support altsettings */
2037 } else if (gadget_is_musbhdrc(gadget)) {
2038 /* reduce tx dma overhead by avoiding special cases */
2040 } else if (gadget_is_sh(gadget)) {
2041 /* sh doesn't support multiple interfaces or configs */
2044 } else if (gadget_is_sa1100(gadget)) {
2045 /* hardware can't write zlps */
2048 * sa1100 CAN do CDC, without status endpoint ... we use
2049 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2054 gcnum = usb_gadget_controller_number(gadget);
2056 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
2059 * can't assume CDC works. don't want to default to
2060 * anything less functional on CDC-capable hardware,
2061 * so we fail in this case.
2063 pr_err("controller '%s' not recognized",
2069 * If there's an RNDIS configuration, that's what Windows wants to
2070 * be using ... so use these product IDs here and in the "linux.inf"
2071 * needed to install MSFT drivers. Current Linux kernels will use
2072 * the second configuration if it's CDC Ethernet, and need some help
2073 * to choose the right configuration otherwise.
2076 #if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
2077 device_desc.idVendor =
2078 __constant_cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
2079 device_desc.idProduct =
2080 __constant_cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
2082 device_desc.idVendor =
2083 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2084 device_desc.idProduct =
2085 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2087 sprintf(product_desc, "RNDIS/%s", driver_desc);
2090 * CDC subset ... recognized by Linux since 2.4.10, but Windows
2091 * drivers aren't widely available. (That may be improved by
2092 * supporting one submode of the "SAFE" variant of MDLM.)
2095 #if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
2096 device_desc.idVendor = cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
2097 device_desc.idProduct = cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
2100 device_desc.idVendor =
2101 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2102 device_desc.idProduct =
2103 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2107 /* support optional vendor/distro customization */
2109 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2111 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
2113 strlcpy(product_desc, iProduct, sizeof product_desc);
2114 if (iSerialNumber) {
2115 device_desc.iSerialNumber = STRING_SERIALNUMBER,
2116 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
2119 /* all we really need is bulk IN/OUT */
2120 usb_ep_autoconfig_reset(gadget);
2121 in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
2124 pr_err("can't autoconfigure on %s\n",
2128 in_ep->driver_data = in_ep; /* claim */
2130 out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
2133 out_ep->driver_data = out_ep; /* claim */
2135 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2137 * CDC Ethernet control interface doesn't require a status endpoint.
2138 * Since some hosts expect one, try to allocate one anyway.
2141 status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
2143 status_ep->driver_data = status_ep; /* claim */
2145 pr_err("can't run RNDIS on %s", gadget->name);
2147 #ifdef CONFIG_USB_ETH_CDC
2149 control_intf.bNumEndpoints = 0;
2150 /* FIXME remove endpoint from descriptor list */
2156 /* one config: cdc, else minimal subset */
2158 eth_config.bNumInterfaces = 1;
2159 eth_config.iConfiguration = STRING_SUBSET;
2162 * use functions to set these up, in case we're built to work
2163 * with multiple controllers and must override CDC Ethernet.
2165 fs_subset_descriptors();
2166 hs_subset_descriptors();
2169 usb_gadget_set_selfpowered(gadget);
2171 /* For now RNDIS is always a second config */
2173 device_desc.bNumConfigurations = 2;
2175 if (gadget_is_dualspeed(gadget)) {
2177 dev_qualifier.bNumConfigurations = 2;
2179 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2181 /* assumes ep0 uses the same value for both speeds ... */
2182 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2184 /* and that all endpoints are dual-speed */
2185 hs_source_desc.bEndpointAddress =
2186 fs_source_desc.bEndpointAddress;
2187 hs_sink_desc.bEndpointAddress =
2188 fs_sink_desc.bEndpointAddress;
2189 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2191 hs_status_desc.bEndpointAddress =
2192 fs_status_desc.bEndpointAddress;
2196 if (gadget_is_otg(gadget)) {
2197 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2198 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2199 eth_config.bMaxPower = 4;
2200 #ifdef CONFIG_USB_ETH_RNDIS
2201 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2202 rndis_config.bMaxPower = 4;
2207 /* network device setup */
2208 #ifndef CONFIG_DM_ETH
2209 dev->net = &l_priv->netdev;
2211 dev->net = l_priv->netdev;
2218 dev->out_ep = out_ep;
2219 dev->status_ep = status_ep;
2221 memset(tmp, 0, sizeof(tmp));
2223 * Module params for these addresses should come from ID proms.
2224 * The host side address is used with CDC and RNDIS, and commonly
2225 * ends up in a persistent config database. It's not clear if
2226 * host side code for the SAFE thing cares -- its original BLAN
2227 * thing didn't, Sharp never assigned those addresses on Zaurii.
2229 #ifndef CONFIG_DM_ETH
2230 get_ether_addr(dev_addr, dev->net->enetaddr);
2231 memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
2233 get_ether_addr(dev_addr, pdata->enetaddr);
2234 memcpy(tmp, pdata->enetaddr, sizeof(pdata->enetaddr));
2237 get_ether_addr(host_addr, dev->host_mac);
2239 sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
2240 dev->host_mac[0], dev->host_mac[1],
2241 dev->host_mac[2], dev->host_mac[3],
2242 dev->host_mac[4], dev->host_mac[5]);
2245 status = rndis_init();
2247 pr_err("can't init RNDIS, %d", status);
2253 * use PKTSIZE (or aligned... from u-boot) and set
2254 * wMaxSegmentSize accordingly
2256 dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
2258 /* preallocate control message data and buffer */
2259 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
2262 dev->req->buf = control_req;
2263 dev->req->complete = eth_setup_complete;
2265 /* ... and maybe likewise for status transfer */
2266 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2267 if (dev->status_ep) {
2268 dev->stat_req = usb_ep_alloc_request(dev->status_ep,
2270 if (!dev->stat_req) {
2271 usb_ep_free_request(dev->status_ep, dev->req);
2275 dev->stat_req->buf = status_req;
2276 dev->stat_req->context = NULL;
2280 /* finish hookup to lower layer ... */
2281 dev->gadget = gadget;
2282 set_gadget_data(gadget, dev);
2283 gadget->ep0->driver_data = dev;
2286 * two kinds of host-initiated state changes:
2287 * - iff DATA transfer is active, carrier is "on"
2288 * - tx queueing enabled if open *and* carrier is "on"
2291 printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
2292 out_ep->name, in_ep->name,
2293 status_ep ? " STATUS " : "",
2294 status_ep ? status_ep->name : ""
2296 #ifndef CONFIG_DM_ETH
2297 printf("MAC %pM\n", dev->net->enetaddr);
2299 printf("MAC %pM\n", pdata->enetaddr);
2303 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2304 dev->host_mac[0], dev->host_mac[1],
2305 dev->host_mac[2], dev->host_mac[3],
2306 dev->host_mac[4], dev->host_mac[5]);
2311 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2313 dev->rndis_config = rndis_register(rndis_control_ack);
2314 if (dev->rndis_config < 0) {
2317 debug("RNDIS setup failed\n");
2322 /* these set up a lot of the OIDs that RNDIS needs */
2323 rndis_set_host_mac(dev->rndis_config, dev->host_mac);
2324 if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
2325 &dev->stats, &dev->cdc_filter))
2327 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2330 if (rndis_set_param_medium(dev->rndis_config,
2331 NDIS_MEDIUM_802_3, 0))
2333 printf("RNDIS ready\n");
2338 pr_err("%s failed, status = %d", __func__, status);
2343 /*-------------------------------------------------------------------------*/
2345 #ifdef CONFIG_DM_USB
2346 int dm_usb_init(struct eth_dev *e_dev)
2348 struct udevice *dev = NULL;
2351 ret = uclass_first_device(UCLASS_USB_DEV_GENERIC, &dev);
2353 pr_err("No USB device found\n");
2357 e_dev->usb_udev = dev;
2363 static int _usb_eth_init(struct ether_priv *priv)
2365 struct eth_dev *dev = &priv->ethdev;
2366 struct usb_gadget *gadget;
2368 unsigned long timeout = USB_CONNECT_TIMEOUT;
2370 #ifdef CONFIG_DM_USB
2371 if (dm_usb_init(dev)) {
2372 pr_err("USB ether not found\n");
2376 board_usb_init(0, USB_INIT_DEVICE);
2379 /* Configure default mac-addresses for the USB ethernet device */
2380 #ifdef CONFIG_USBNET_DEV_ADDR
2381 strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
2383 #ifdef CONFIG_USBNET_HOST_ADDR
2384 strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
2386 /* Check if the user overruled the MAC addresses */
2387 if (env_get("usbnet_devaddr"))
2388 strlcpy(dev_addr, env_get("usbnet_devaddr"),
2391 if (env_get("usbnet_hostaddr"))
2392 strlcpy(host_addr, env_get("usbnet_hostaddr"),
2395 if (!is_eth_addr_valid(dev_addr)) {
2396 pr_err("Need valid 'usbnet_devaddr' to be set");
2399 if (!is_eth_addr_valid(host_addr)) {
2400 pr_err("Need valid 'usbnet_hostaddr' to be set");
2404 priv->eth_driver.speed = DEVSPEED;
2405 priv->eth_driver.bind = eth_bind;
2406 priv->eth_driver.unbind = eth_unbind;
2407 priv->eth_driver.setup = eth_setup;
2408 priv->eth_driver.reset = eth_disconnect;
2409 priv->eth_driver.disconnect = eth_disconnect;
2410 priv->eth_driver.suspend = eth_suspend;
2411 priv->eth_driver.resume = eth_resume;
2412 if (usb_gadget_register_driver(&priv->eth_driver) < 0)
2415 dev->network_started = 0;
2417 packet_received = 0;
2420 gadget = dev->gadget;
2421 usb_gadget_connect(gadget);
2423 if (env_get("cdc_connect_timeout"))
2424 timeout = simple_strtoul(env_get("cdc_connect_timeout"),
2425 NULL, 10) * CONFIG_SYS_HZ;
2427 while (!dev->network_started) {
2428 /* Handle control-c and timeouts */
2429 if (ctrlc() || (get_timer(ts) > timeout)) {
2430 pr_err("The remote end did not respond in time.");
2433 usb_gadget_handle_interrupts(0);
2436 packet_received = 0;
2437 rx_submit(dev, dev->rx_req, 0);
2443 static int _usb_eth_send(struct ether_priv *priv, void *packet, int length)
2446 void *rndis_pkt = NULL;
2447 struct eth_dev *dev = &priv->ethdev;
2448 struct usb_request *req = dev->tx_req;
2450 unsigned long timeout = USB_CONNECT_TIMEOUT;
2452 debug("%s:...\n", __func__);
2454 /* new buffer is needed to include RNDIS header */
2455 if (rndis_active(dev)) {
2456 rndis_pkt = malloc(length +
2457 sizeof(struct rndis_packet_msg_type));
2459 pr_err("No memory to alloc RNDIS packet");
2462 rndis_add_hdr(rndis_pkt, length);
2463 memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
2466 length += sizeof(struct rndis_packet_msg_type);
2469 req->context = NULL;
2470 req->complete = tx_complete;
2473 * use zlp framing on tx for strict CDC-Ether conformance,
2474 * though any robust network rx path ignores extra padding.
2475 * and some hardware doesn't like to write zlps.
2478 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2481 req->length = length;
2483 /* throttle highspeed IRQ rate back slightly */
2484 if (gadget_is_dualspeed(dev->gadget))
2485 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2486 ? ((dev->tx_qlen % qmult) != 0) : 0;
2492 retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
2495 debug("%s: packet queued\n", __func__);
2496 while (!packet_sent) {
2497 if (get_timer(ts) > timeout) {
2498 printf("timeout sending packets to usb ethernet\n");
2501 usb_gadget_handle_interrupts(0);
2508 dev->stats.tx_dropped++;
2512 static int _usb_eth_recv(struct ether_priv *priv)
2514 usb_gadget_handle_interrupts(0);
2519 void _usb_eth_halt(struct ether_priv *priv)
2521 struct eth_dev *dev = &priv->ethdev;
2523 /* If the gadget not registered, simple return */
2528 * Some USB controllers may need additional deinitialization here
2529 * before dropping pull-up (also due to hardware issues).
2530 * For example: unhandled interrupt with status stage started may
2531 * bring the controller to fully broken state (until board reset).
2532 * There are some variants to debug and fix such cases:
2533 * 1) In the case of RNDIS connection eth_stop can perform additional
2534 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2535 * 2) 'pullup' callback in your UDC driver can be improved to perform
2536 * this deinitialization.
2540 usb_gadget_disconnect(dev->gadget);
2542 /* Clear pending interrupt */
2543 if (dev->network_started) {
2544 usb_gadget_handle_interrupts(0);
2545 dev->network_started = 0;
2548 usb_gadget_unregister_driver(&priv->eth_driver);
2549 #ifndef CONFIG_DM_USB
2550 board_usb_cleanup(0, USB_INIT_DEVICE);
2554 #ifndef CONFIG_DM_ETH
2555 static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
2557 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2559 return _usb_eth_init(priv);
2562 static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
2564 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2566 return _usb_eth_send(priv, packet, length);
2569 static int usb_eth_recv(struct eth_device *netdev)
2571 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2572 struct eth_dev *dev = &priv->ethdev;
2575 ret = _usb_eth_recv(priv);
2577 pr_err("error packet receive\n");
2581 if (!packet_received)
2585 net_process_received_packet(net_rx_packets[0],
2586 dev->rx_req->length);
2588 pr_err("dev->rx_req invalid");
2590 packet_received = 0;
2591 rx_submit(dev, dev->rx_req, 0);
2596 void usb_eth_halt(struct eth_device *netdev)
2598 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2600 _usb_eth_halt(priv);
2603 int usb_eth_initialize(bd_t *bi)
2605 struct eth_device *netdev = &l_priv->netdev;
2607 strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
2609 netdev->init = usb_eth_init;
2610 netdev->send = usb_eth_send;
2611 netdev->recv = usb_eth_recv;
2612 netdev->halt = usb_eth_halt;
2613 netdev->priv = l_priv;
2615 #ifdef CONFIG_MCAST_TFTP
2616 #error not supported
2618 eth_register(netdev);
2622 static int usb_eth_start(struct udevice *dev)
2624 struct ether_priv *priv = dev_get_priv(dev);
2626 return _usb_eth_init(priv);
2629 static int usb_eth_send(struct udevice *dev, void *packet, int length)
2631 struct ether_priv *priv = dev_get_priv(dev);
2633 return _usb_eth_send(priv, packet, length);
2636 static int usb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
2638 struct ether_priv *priv = dev_get_priv(dev);
2639 struct eth_dev *ethdev = &priv->ethdev;
2642 ret = _usb_eth_recv(priv);
2644 pr_err("error packet receive\n");
2648 if (packet_received) {
2649 if (ethdev->rx_req) {
2650 *packetp = (uchar *)net_rx_packets[0];
2651 return ethdev->rx_req->length;
2653 pr_err("dev->rx_req invalid");
2661 static int usb_eth_free_pkt(struct udevice *dev, uchar *packet,
2664 struct ether_priv *priv = dev_get_priv(dev);
2665 struct eth_dev *ethdev = &priv->ethdev;
2667 packet_received = 0;
2669 return rx_submit(ethdev, ethdev->rx_req, 0);
2672 static void usb_eth_stop(struct udevice *dev)
2674 struct ether_priv *priv = dev_get_priv(dev);
2676 _usb_eth_halt(priv);
2679 static int usb_eth_probe(struct udevice *dev)
2681 struct ether_priv *priv = dev_get_priv(dev);
2682 struct eth_pdata *pdata = dev_get_platdata(dev);
2687 get_ether_addr(CONFIG_USBNET_DEVADDR, pdata->enetaddr);
2688 eth_env_set_enetaddr("usbnet_devaddr", pdata->enetaddr);
2693 static const struct eth_ops usb_eth_ops = {
2694 .start = usb_eth_start,
2695 .send = usb_eth_send,
2696 .recv = usb_eth_recv,
2697 .free_pkt = usb_eth_free_pkt,
2698 .stop = usb_eth_stop,
2701 int usb_ether_init(void)
2703 struct udevice *dev;
2704 struct udevice *usb_dev;
2707 ret = uclass_first_device(UCLASS_USB_DEV_GENERIC, &usb_dev);
2708 if (!usb_dev || ret) {
2709 pr_err("No USB device found\n");
2713 ret = device_bind_driver(usb_dev, "usb_ether", "usb_ether", &dev);
2715 pr_err("usb - not able to bind usb_ether device\n");
2722 U_BOOT_DRIVER(eth_usb) = {
2723 .name = "usb_ether",
2725 .probe = usb_eth_probe,
2726 .ops = &usb_eth_ops,
2727 .priv_auto_alloc_size = sizeof(struct ether_priv),
2728 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
2729 .flags = DM_FLAG_ALLOC_PRIV_DMA,
2731 #endif /* CONFIG_DM_ETH */