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"
28 #include <dm/uclass-internal.h>
29 #include <dm/device-internal.h>
31 #define USB_NET_NAME "usb_ether"
34 extern struct platform_data brd;
37 unsigned packet_received, packet_sent;
40 * Ethernet gadget driver -- with CDC and non-CDC options
41 * Builds on hardware support for a full duplex link.
43 * CDC Ethernet is the standard USB solution for sending Ethernet frames
44 * using USB. Real hardware tends to use the same framing protocol but look
45 * different for control features. This driver strongly prefers to use
46 * this USB-IF standard as its open-systems interoperability solution;
47 * most host side USB stacks (except from Microsoft) support it.
49 * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
50 * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
51 * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
53 * There's some hardware that can't talk CDC ECM. We make that hardware
54 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
55 * link-level setup only requires activating the configuration. Only the
56 * endpoint descriptors, and product/vendor IDs, are relevant; no control
57 * operations are available. Linux supports it, but other host operating
58 * systems may not. (This is a subset of CDC Ethernet.)
60 * It turns out that if you add a few descriptors to that "CDC Subset",
61 * (Windows) host side drivers from MCCI can treat it as one submode of
62 * a proprietary scheme called "SAFE" ... without needing to know about
63 * specific product/vendor IDs. So we do that, making it easier to use
64 * those MS-Windows drivers. Those added descriptors make it resemble a
65 * CDC MDLM device, but they don't change device behavior at all. (See
66 * MCCI Engineering report 950198 "SAFE Networking Functions".)
68 * A third option is also in use. Rather than CDC Ethernet, or something
69 * simpler, Microsoft pushes their own approach: RNDIS. The published
70 * RNDIS specs are ambiguous and appear to be incomplete, and are also
71 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
73 #define ETH_ALEN 6 /* Octets in one ethernet addr */
74 #define ETH_HLEN 14 /* Total octets in header. */
75 #define ETH_ZLEN 60 /* Min. octets in frame sans FCS */
76 #define ETH_DATA_LEN 1500 /* Max. octets in payload */
77 #define ETH_FRAME_LEN PKTSIZE_ALIGN /* Max. octets in frame sans FCS */
79 #define DRIVER_DESC "Ethernet Gadget"
80 /* Based on linux 2.6.27 version */
81 #define DRIVER_VERSION "May Day 2005"
83 static const char shortname[] = "ether";
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 struct eth_device *net;
120 struct net_device_stats stats;
121 unsigned int tx_qlen;
126 unsigned suspended:1;
127 unsigned network_started:1;
131 #define WORK_RX_MEMORY 0
133 u8 host_mac[ETH_ALEN];
137 * This version autoconfigures as much as possible at run-time.
139 * It also ASSUMES a self-powered device, without remote wakeup,
140 * although remote wakeup support would make sense.
143 /*-------------------------------------------------------------------------*/
145 struct eth_dev ethdev;
146 struct eth_device netdev;
147 struct usb_gadget_driver eth_driver;
150 struct ether_priv eth_priv;
151 struct ether_priv *l_priv = ð_priv;
153 /*-------------------------------------------------------------------------*/
155 /* "main" config is either CDC, or its simple subset */
156 static inline int is_cdc(struct eth_dev *dev)
158 #if !defined(CONFIG_USB_ETH_SUBSET)
159 return 1; /* only cdc possible */
160 #elif !defined(CONFIG_USB_ETH_CDC)
161 return 0; /* only subset possible */
163 return dev->cdc; /* depends on what hardware we found */
167 /* "secondary" RNDIS config may sometimes be activated */
168 static inline int rndis_active(struct eth_dev *dev)
170 #ifdef CONFIG_USB_ETH_RNDIS
177 #define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
178 #define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev))
180 #define DEFAULT_QLEN 2 /* double buffering by default */
182 /* peak bulk transfer bits-per-second */
183 #define HS_BPS (13 * 512 * 8 * 1000 * 8)
184 #define FS_BPS (19 * 64 * 1 * 1000 * 8)
186 #ifdef CONFIG_USB_GADGET_DUALSPEED
187 #define DEVSPEED USB_SPEED_HIGH
189 #ifdef CONFIG_USB_ETH_QMULT
190 #define qmult CONFIG_USB_ETH_QMULT
195 /* for dual-speed hardware, use deeper queues at highspeed */
196 #define qlen(gadget) \
197 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
199 static inline int BITRATE(struct usb_gadget *g)
201 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
204 #else /* full speed (low speed doesn't do bulk) */
208 #define DEVSPEED USB_SPEED_FULL
210 #define qlen(gadget) DEFAULT_QLEN
212 static inline int BITRATE(struct usb_gadget *g)
218 /*-------------------------------------------------------------------------*/
221 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
222 * Instead: allocate your own, using normal USB-IF procedures.
226 * Thanks to NetChip Technologies for donating this product ID.
227 * It's for devices with only CDC Ethernet configurations.
229 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
230 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
233 * For hardware that can't talk CDC, we use the same vendor ID that
234 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
235 * with pxa250. We're protocol-compatible, if the host-side drivers
236 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
237 * drivers that need to hard-wire endpoint numbers have a hook.
239 * The protocol is a minimal subset of CDC Ether, which works on any bulk
240 * hardware that's not deeply broken ... even on hardware that can't talk
241 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
242 * doesn't handle control-OUT).
244 #define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */
245 #define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */
248 * For hardware that can talk RNDIS and either of the above protocols,
249 * use this ID ... the windows INF files will know it. Unless it's
250 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
251 * the non-RNDIS configuration.
253 #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
254 #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
257 * Some systems will want different product identifers published in the
258 * device descriptor, either numbers or strings or both. These string
259 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
263 * Emulating them in eth_bind:
264 * static ushort idVendor;
265 * static ushort idProduct;
268 #if defined(CONFIG_USBNET_MANUFACTURER)
269 static char *iManufacturer = CONFIG_USBNET_MANUFACTURER;
271 static char *iManufacturer = "U-Boot";
274 /* These probably need to be configurable. */
275 static ushort bcdDevice;
276 static char *iProduct;
277 static char *iSerialNumber;
279 static char dev_addr[18];
281 static char host_addr[18];
284 /*-------------------------------------------------------------------------*/
287 * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
288 * ep0 implementation: descriptors, config management, setup().
289 * also optional class-specific notification interrupt transfer.
293 * DESCRIPTORS ... most are static, but strings and (full) configuration
294 * descriptors are built on demand. For now we do either full CDC, or
295 * our simple subset, with RNDIS as an optional second configuration.
297 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
298 * the class descriptors match a modem (they're ignored; it's really just
299 * Ethernet functionality), they don't need the NOP altsetting, and the
300 * status transfer endpoint isn't optional.
303 #define STRING_MANUFACTURER 1
304 #define STRING_PRODUCT 2
305 #define STRING_ETHADDR 3
306 #define STRING_DATA 4
307 #define STRING_CONTROL 5
308 #define STRING_RNDIS_CONTROL 6
310 #define STRING_SUBSET 8
311 #define STRING_RNDIS 9
312 #define STRING_SERIALNUMBER 10
314 /* holds our biggest descriptor (or RNDIS response) */
315 #define USB_BUFSIZ 256
318 * This device advertises one configuration, eth_config, unless RNDIS
319 * is enabled (rndis_config) on hardware supporting at least two configs.
321 * NOTE: Controllers like superh_udc should probably be able to use
322 * an RNDIS-only configuration.
324 * FIXME define some higher-powered configurations to make it easier
325 * to recharge batteries ...
328 #define DEV_CONFIG_VALUE 1 /* cdc or subset */
329 #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
331 static struct usb_device_descriptor
333 .bLength = sizeof device_desc,
334 .bDescriptorType = USB_DT_DEVICE,
336 .bcdUSB = __constant_cpu_to_le16(0x0200),
338 .bDeviceClass = USB_CLASS_COMM,
339 .bDeviceSubClass = 0,
340 .bDeviceProtocol = 0,
342 .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
343 .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
344 .iManufacturer = STRING_MANUFACTURER,
345 .iProduct = STRING_PRODUCT,
346 .bNumConfigurations = 1,
349 static struct usb_otg_descriptor
351 .bLength = sizeof otg_descriptor,
352 .bDescriptorType = USB_DT_OTG,
354 .bmAttributes = USB_OTG_SRP,
357 static struct usb_config_descriptor
359 .bLength = sizeof eth_config,
360 .bDescriptorType = USB_DT_CONFIG,
362 /* compute wTotalLength on the fly */
364 .bConfigurationValue = DEV_CONFIG_VALUE,
365 .iConfiguration = STRING_CDC,
366 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
370 #ifdef CONFIG_USB_ETH_RNDIS
371 static struct usb_config_descriptor
373 .bLength = sizeof rndis_config,
374 .bDescriptorType = USB_DT_CONFIG,
376 /* compute wTotalLength on the fly */
378 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
379 .iConfiguration = STRING_RNDIS,
380 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
386 * Compared to the simple CDC subset, the full CDC Ethernet model adds
387 * three class descriptors, two interface descriptors, optional status
388 * endpoint. Both have a "data" interface and two bulk endpoints.
389 * There are also differences in how control requests are handled.
391 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
392 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
393 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
394 * work around those bugs) are unlikely to ever come from MSFT, you may
395 * wish to avoid using RNDIS.
397 * MCCI offers an alternative to RNDIS if you need to connect to Windows
398 * but have hardware that can't support CDC Ethernet. We add descriptors
399 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
400 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
401 * get those drivers from MCCI, or bundled with various products.
404 #ifdef CONFIG_USB_ETH_CDC
405 static struct usb_interface_descriptor
407 .bLength = sizeof control_intf,
408 .bDescriptorType = USB_DT_INTERFACE,
410 .bInterfaceNumber = 0,
411 /* status endpoint is optional; this may be patched later */
413 .bInterfaceClass = USB_CLASS_COMM,
414 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
415 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
416 .iInterface = STRING_CONTROL,
420 #ifdef CONFIG_USB_ETH_RNDIS
421 static const struct usb_interface_descriptor
422 rndis_control_intf = {
423 .bLength = sizeof rndis_control_intf,
424 .bDescriptorType = USB_DT_INTERFACE,
426 .bInterfaceNumber = 0,
428 .bInterfaceClass = USB_CLASS_COMM,
429 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
430 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
431 .iInterface = STRING_RNDIS_CONTROL,
435 static const struct usb_cdc_header_desc header_desc = {
436 .bLength = sizeof header_desc,
437 .bDescriptorType = USB_DT_CS_INTERFACE,
438 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
440 .bcdCDC = __constant_cpu_to_le16(0x0110),
443 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
445 static const struct usb_cdc_union_desc union_desc = {
446 .bLength = sizeof union_desc,
447 .bDescriptorType = USB_DT_CS_INTERFACE,
448 .bDescriptorSubType = USB_CDC_UNION_TYPE,
450 .bMasterInterface0 = 0, /* index of control interface */
451 .bSlaveInterface0 = 1, /* index of DATA interface */
454 #endif /* CDC || RNDIS */
456 #ifdef CONFIG_USB_ETH_RNDIS
458 static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
459 .bLength = sizeof call_mgmt_descriptor,
460 .bDescriptorType = USB_DT_CS_INTERFACE,
461 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
463 .bmCapabilities = 0x00,
464 .bDataInterface = 0x01,
467 static const struct usb_cdc_acm_descriptor acm_descriptor = {
468 .bLength = sizeof acm_descriptor,
469 .bDescriptorType = USB_DT_CS_INTERFACE,
470 .bDescriptorSubType = USB_CDC_ACM_TYPE,
472 .bmCapabilities = 0x00,
477 #ifndef CONFIG_USB_ETH_CDC
480 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
481 * ways: data endpoints live in the control interface, there's no data
482 * interface, and it's not used to talk to a cell phone radio.
485 static const struct usb_cdc_mdlm_desc mdlm_desc = {
486 .bLength = sizeof mdlm_desc,
487 .bDescriptorType = USB_DT_CS_INTERFACE,
488 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
490 .bcdVersion = __constant_cpu_to_le16(0x0100),
492 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
493 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
498 * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
499 * can't really use its struct. All we do here is say that we're using
500 * the submode of "SAFE" which directly matches the CDC Subset.
502 static const u8 mdlm_detail_desc[] = {
505 USB_CDC_MDLM_DETAIL_TYPE,
508 0, /* network control capabilities (none) */
509 0, /* network data capabilities ("raw" encapsulation) */
514 static const struct usb_cdc_ether_desc ether_desc = {
515 .bLength = sizeof(ether_desc),
516 .bDescriptorType = USB_DT_CS_INTERFACE,
517 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
519 /* this descriptor actually adds value, surprise! */
520 .iMACAddress = STRING_ETHADDR,
521 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
522 .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN),
523 .wNumberMCFilters = __constant_cpu_to_le16(0),
524 .bNumberPowerFilters = 0,
527 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
530 * include the status endpoint if we can, even where it's optional.
531 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
532 * packet, to simplify cancellation; and a big transfer interval, to
533 * waste less bandwidth.
535 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
536 * if they ignore the connect/disconnect notifications that real aether
537 * can provide. more advanced cdc configurations might want to support
538 * encapsulated commands (vendor-specific, using control-OUT).
540 * RNDIS requires the status endpoint, since it uses that encapsulation
541 * mechanism for its funky RPC scheme.
544 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
545 #define STATUS_BYTECOUNT 16 /* 8 byte header + data */
547 static struct usb_endpoint_descriptor
549 .bLength = USB_DT_ENDPOINT_SIZE,
550 .bDescriptorType = USB_DT_ENDPOINT,
552 .bEndpointAddress = USB_DIR_IN,
553 .bmAttributes = USB_ENDPOINT_XFER_INT,
554 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
555 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
559 #ifdef CONFIG_USB_ETH_CDC
561 /* the default data interface has no endpoints ... */
563 static const struct usb_interface_descriptor
565 .bLength = sizeof data_nop_intf,
566 .bDescriptorType = USB_DT_INTERFACE,
568 .bInterfaceNumber = 1,
569 .bAlternateSetting = 0,
571 .bInterfaceClass = USB_CLASS_CDC_DATA,
572 .bInterfaceSubClass = 0,
573 .bInterfaceProtocol = 0,
576 /* ... but the "real" data interface has two bulk endpoints */
578 static const struct usb_interface_descriptor
580 .bLength = sizeof data_intf,
581 .bDescriptorType = USB_DT_INTERFACE,
583 .bInterfaceNumber = 1,
584 .bAlternateSetting = 1,
586 .bInterfaceClass = USB_CLASS_CDC_DATA,
587 .bInterfaceSubClass = 0,
588 .bInterfaceProtocol = 0,
589 .iInterface = STRING_DATA,
594 #ifdef CONFIG_USB_ETH_RNDIS
596 /* RNDIS doesn't activate by changing to the "real" altsetting */
598 static const struct usb_interface_descriptor
600 .bLength = sizeof rndis_data_intf,
601 .bDescriptorType = USB_DT_INTERFACE,
603 .bInterfaceNumber = 1,
604 .bAlternateSetting = 0,
606 .bInterfaceClass = USB_CLASS_CDC_DATA,
607 .bInterfaceSubClass = 0,
608 .bInterfaceProtocol = 0,
609 .iInterface = STRING_DATA,
614 #ifdef CONFIG_USB_ETH_SUBSET
617 * "Simple" CDC-subset option is a simple vendor-neutral model that most
618 * full speed controllers can handle: one interface, two bulk endpoints.
620 * To assist host side drivers, we fancy it up a bit, and add descriptors
621 * so some host side drivers will understand it as a "SAFE" variant.
624 static const struct usb_interface_descriptor
626 .bLength = sizeof subset_data_intf,
627 .bDescriptorType = USB_DT_INTERFACE,
629 .bInterfaceNumber = 0,
630 .bAlternateSetting = 0,
632 .bInterfaceClass = USB_CLASS_COMM,
633 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
634 .bInterfaceProtocol = 0,
635 .iInterface = STRING_DATA,
640 static struct usb_endpoint_descriptor
642 .bLength = USB_DT_ENDPOINT_SIZE,
643 .bDescriptorType = USB_DT_ENDPOINT,
645 .bEndpointAddress = USB_DIR_IN,
646 .bmAttributes = USB_ENDPOINT_XFER_BULK,
647 .wMaxPacketSize = __constant_cpu_to_le16(64),
650 static struct usb_endpoint_descriptor
652 .bLength = USB_DT_ENDPOINT_SIZE,
653 .bDescriptorType = USB_DT_ENDPOINT,
655 .bEndpointAddress = USB_DIR_OUT,
656 .bmAttributes = USB_ENDPOINT_XFER_BULK,
657 .wMaxPacketSize = __constant_cpu_to_le16(64),
660 static const struct usb_descriptor_header *fs_eth_function[11] = {
661 (struct usb_descriptor_header *) &otg_descriptor,
662 #ifdef CONFIG_USB_ETH_CDC
663 /* "cdc" mode descriptors */
664 (struct usb_descriptor_header *) &control_intf,
665 (struct usb_descriptor_header *) &header_desc,
666 (struct usb_descriptor_header *) &union_desc,
667 (struct usb_descriptor_header *) ðer_desc,
668 /* NOTE: status endpoint may need to be removed */
669 (struct usb_descriptor_header *) &fs_status_desc,
670 /* data interface, with altsetting */
671 (struct usb_descriptor_header *) &data_nop_intf,
672 (struct usb_descriptor_header *) &data_intf,
673 (struct usb_descriptor_header *) &fs_source_desc,
674 (struct usb_descriptor_header *) &fs_sink_desc,
676 #endif /* CONFIG_USB_ETH_CDC */
679 static inline void fs_subset_descriptors(void)
681 #ifdef CONFIG_USB_ETH_SUBSET
682 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
683 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
684 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
685 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
686 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
687 fs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
688 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
689 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
690 fs_eth_function[8] = NULL;
692 fs_eth_function[1] = NULL;
696 #ifdef CONFIG_USB_ETH_RNDIS
697 static const struct usb_descriptor_header *fs_rndis_function[] = {
698 (struct usb_descriptor_header *) &otg_descriptor,
699 /* control interface matches ACM, not Ethernet */
700 (struct usb_descriptor_header *) &rndis_control_intf,
701 (struct usb_descriptor_header *) &header_desc,
702 (struct usb_descriptor_header *) &call_mgmt_descriptor,
703 (struct usb_descriptor_header *) &acm_descriptor,
704 (struct usb_descriptor_header *) &union_desc,
705 (struct usb_descriptor_header *) &fs_status_desc,
706 /* data interface has no altsetting */
707 (struct usb_descriptor_header *) &rndis_data_intf,
708 (struct usb_descriptor_header *) &fs_source_desc,
709 (struct usb_descriptor_header *) &fs_sink_desc,
715 * usb 2.0 devices need to expose both high speed and full speed
716 * descriptors, unless they only run at full speed.
719 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
720 static struct usb_endpoint_descriptor
722 .bLength = USB_DT_ENDPOINT_SIZE,
723 .bDescriptorType = USB_DT_ENDPOINT,
725 .bmAttributes = USB_ENDPOINT_XFER_INT,
726 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
727 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
729 #endif /* CONFIG_USB_ETH_CDC */
731 static struct usb_endpoint_descriptor
733 .bLength = USB_DT_ENDPOINT_SIZE,
734 .bDescriptorType = USB_DT_ENDPOINT,
736 .bmAttributes = USB_ENDPOINT_XFER_BULK,
737 .wMaxPacketSize = __constant_cpu_to_le16(512),
740 static struct usb_endpoint_descriptor
742 .bLength = USB_DT_ENDPOINT_SIZE,
743 .bDescriptorType = USB_DT_ENDPOINT,
745 .bmAttributes = USB_ENDPOINT_XFER_BULK,
746 .wMaxPacketSize = __constant_cpu_to_le16(512),
749 static struct usb_qualifier_descriptor
751 .bLength = sizeof dev_qualifier,
752 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
754 .bcdUSB = __constant_cpu_to_le16(0x0200),
755 .bDeviceClass = USB_CLASS_COMM,
757 .bNumConfigurations = 1,
760 static const struct usb_descriptor_header *hs_eth_function[11] = {
761 (struct usb_descriptor_header *) &otg_descriptor,
762 #ifdef CONFIG_USB_ETH_CDC
763 /* "cdc" mode descriptors */
764 (struct usb_descriptor_header *) &control_intf,
765 (struct usb_descriptor_header *) &header_desc,
766 (struct usb_descriptor_header *) &union_desc,
767 (struct usb_descriptor_header *) ðer_desc,
768 /* NOTE: status endpoint may need to be removed */
769 (struct usb_descriptor_header *) &hs_status_desc,
770 /* data interface, with altsetting */
771 (struct usb_descriptor_header *) &data_nop_intf,
772 (struct usb_descriptor_header *) &data_intf,
773 (struct usb_descriptor_header *) &hs_source_desc,
774 (struct usb_descriptor_header *) &hs_sink_desc,
776 #endif /* CONFIG_USB_ETH_CDC */
779 static inline void hs_subset_descriptors(void)
781 #ifdef CONFIG_USB_ETH_SUBSET
782 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
783 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
784 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
785 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
786 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
787 hs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
788 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
789 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
790 hs_eth_function[8] = NULL;
792 hs_eth_function[1] = NULL;
796 #ifdef CONFIG_USB_ETH_RNDIS
797 static const struct usb_descriptor_header *hs_rndis_function[] = {
798 (struct usb_descriptor_header *) &otg_descriptor,
799 /* control interface matches ACM, not Ethernet */
800 (struct usb_descriptor_header *) &rndis_control_intf,
801 (struct usb_descriptor_header *) &header_desc,
802 (struct usb_descriptor_header *) &call_mgmt_descriptor,
803 (struct usb_descriptor_header *) &acm_descriptor,
804 (struct usb_descriptor_header *) &union_desc,
805 (struct usb_descriptor_header *) &hs_status_desc,
806 /* data interface has no altsetting */
807 (struct usb_descriptor_header *) &rndis_data_intf,
808 (struct usb_descriptor_header *) &hs_source_desc,
809 (struct usb_descriptor_header *) &hs_sink_desc,
815 /* maxpacket and other transfer characteristics vary by speed. */
816 static inline struct usb_endpoint_descriptor *
817 ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
818 struct usb_endpoint_descriptor *fs)
820 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
825 /*-------------------------------------------------------------------------*/
827 /* descriptors that are built on-demand */
829 static char manufacturer[50];
830 static char product_desc[40] = DRIVER_DESC;
831 static char serial_number[20];
833 /* address that the host will use ... usually assigned at random */
834 static char ethaddr[2 * ETH_ALEN + 1];
836 /* static strings, in UTF-8 */
837 static struct usb_string strings[] = {
838 { STRING_MANUFACTURER, manufacturer, },
839 { STRING_PRODUCT, product_desc, },
840 { STRING_SERIALNUMBER, serial_number, },
841 { STRING_DATA, "Ethernet Data", },
842 { STRING_ETHADDR, ethaddr, },
843 #ifdef CONFIG_USB_ETH_CDC
844 { STRING_CDC, "CDC Ethernet", },
845 { STRING_CONTROL, "CDC Communications Control", },
847 #ifdef CONFIG_USB_ETH_SUBSET
848 { STRING_SUBSET, "CDC Ethernet Subset", },
850 #ifdef CONFIG_USB_ETH_RNDIS
851 { STRING_RNDIS, "RNDIS", },
852 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
854 { } /* end of list */
857 static struct usb_gadget_strings stringtab = {
858 .language = 0x0409, /* en-us */
862 /*============================================================================*/
863 DEFINE_CACHE_ALIGN_BUFFER(u8, control_req, USB_BUFSIZ);
865 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
866 DEFINE_CACHE_ALIGN_BUFFER(u8, status_req, STATUS_BYTECOUNT);
869 /*============================================================================*/
872 * one config, two interfaces: control, data.
873 * complications: class descriptors, and an altsetting.
876 config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
879 const struct usb_config_descriptor *config;
880 const struct usb_descriptor_header **function;
883 if (gadget_is_dualspeed(g)) {
884 hs = (g->speed == USB_SPEED_HIGH);
885 if (type == USB_DT_OTHER_SPEED_CONFIG)
888 #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
890 if (index >= device_desc.bNumConfigurations)
893 #ifdef CONFIG_USB_ETH_RNDIS
895 * list the RNDIS config first, to make Microsoft's drivers
896 * happy. DOCSIS 1.0 needs this too.
898 if (device_desc.bNumConfigurations == 2 && index == 0) {
899 config = &rndis_config;
900 function = which_fn(rndis);
904 config = ð_config;
905 function = which_fn(eth);
908 /* for now, don't advertise srp-only devices */
912 len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
915 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
919 /*-------------------------------------------------------------------------*/
921 static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
922 static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
925 set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
928 struct usb_gadget *gadget = dev->gadget;
930 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
931 /* status endpoint used for RNDIS and (optionally) CDC */
932 if (!subset_active(dev) && dev->status_ep) {
933 dev->status = ep_desc(gadget, &hs_status_desc,
935 dev->status_ep->driver_data = dev;
937 result = usb_ep_enable(dev->status_ep, dev->status);
939 debug("enable %s --> %d\n",
940 dev->status_ep->name, result);
946 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
947 dev->in_ep->driver_data = dev;
949 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
950 dev->out_ep->driver_data = dev;
953 * With CDC, the host isn't allowed to use these two data
954 * endpoints in the default altsetting for the interface.
955 * so we don't activate them yet. Reset from SET_INTERFACE.
957 * Strictly speaking RNDIS should work the same: activation is
958 * a side effect of setting a packet filter. Deactivation is
959 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
961 if (!cdc_active(dev)) {
962 result = usb_ep_enable(dev->in_ep, dev->in);
964 debug("enable %s --> %d\n",
965 dev->in_ep->name, result);
969 result = usb_ep_enable(dev->out_ep, dev->out);
971 debug("enable %s --> %d\n",
972 dev->out_ep->name, result);
979 result = alloc_requests(dev, qlen(gadget), gfp_flags);
981 /* on error, disable any endpoints */
983 if (!subset_active(dev) && dev->status_ep)
984 (void) usb_ep_disable(dev->status_ep);
986 (void) usb_ep_disable(dev->in_ep);
987 (void) usb_ep_disable(dev->out_ep);
990 } else if (!cdc_active(dev)) {
992 * activate non-CDC configs right away
993 * this isn't strictly according to the RNDIS spec
995 eth_start(dev, GFP_ATOMIC);
998 /* caller is responsible for cleanup on error */
1002 static void eth_reset_config(struct eth_dev *dev)
1004 if (dev->config == 0)
1007 debug("%s\n", __func__);
1009 rndis_uninit(dev->rndis_config);
1012 * disable endpoints, forcing (synchronous) completion of
1013 * pending i/o. then free the requests.
1017 usb_ep_disable(dev->in_ep);
1019 usb_ep_free_request(dev->in_ep, dev->tx_req);
1024 usb_ep_disable(dev->out_ep);
1026 usb_ep_free_request(dev->out_ep, dev->rx_req);
1031 usb_ep_disable(dev->status_ep);
1034 dev->cdc_filter = 0;
1039 * change our operational config. must agree with the code
1040 * that returns config descriptors, and altsetting code.
1042 static int eth_set_config(struct eth_dev *dev, unsigned number,
1046 struct usb_gadget *gadget = dev->gadget;
1048 if (gadget_is_sa1100(gadget)
1050 && dev->tx_qlen != 0) {
1051 /* tx fifo is full, but we can't clear it...*/
1052 error("can't change configurations");
1055 eth_reset_config(dev);
1058 case DEV_CONFIG_VALUE:
1059 result = set_ether_config(dev, gfp_flags);
1061 #ifdef CONFIG_USB_ETH_RNDIS
1062 case DEV_RNDIS_CONFIG_VALUE:
1064 result = set_ether_config(dev, gfp_flags);
1076 eth_reset_config(dev);
1077 usb_gadget_vbus_draw(dev->gadget,
1078 gadget_is_otg(dev->gadget) ? 8 : 100);
1083 power = 2 * eth_config.bMaxPower;
1084 usb_gadget_vbus_draw(dev->gadget, power);
1086 switch (gadget->speed) {
1087 case USB_SPEED_FULL:
1088 speed = "full"; break;
1089 #ifdef CONFIG_USB_GADGET_DUALSPEED
1090 case USB_SPEED_HIGH:
1091 speed = "high"; break;
1097 dev->config = number;
1098 printf("%s speed config #%d: %d mA, %s, using %s\n",
1099 speed, number, power, driver_desc,
1104 : "CDC Ethernet Subset"));
1109 /*-------------------------------------------------------------------------*/
1111 #ifdef CONFIG_USB_ETH_CDC
1114 * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
1115 * only to notify the host about link status changes (which we support) or
1116 * report completion of some encapsulated command (as used in RNDIS). Since
1117 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1118 * command mechanism; and only one status request is ever queued.
1120 static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
1122 struct usb_cdc_notification *event = req->buf;
1123 int value = req->status;
1124 struct eth_dev *dev = ep->driver_data;
1126 /* issue the second notification if host reads the first */
1127 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1129 __le32 *data = req->buf + sizeof *event;
1131 event->bmRequestType = 0xA1;
1132 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
1133 event->wValue = __constant_cpu_to_le16(0);
1134 event->wIndex = __constant_cpu_to_le16(1);
1135 event->wLength = __constant_cpu_to_le16(8);
1137 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1138 data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
1140 req->length = STATUS_BYTECOUNT;
1141 value = usb_ep_queue(ep, req, GFP_ATOMIC);
1142 debug("send SPEED_CHANGE --> %d\n", value);
1145 } else if (value != -ECONNRESET) {
1146 debug("event %02x --> %d\n",
1147 event->bNotificationType, value);
1148 if (event->bNotificationType ==
1149 USB_CDC_NOTIFY_SPEED_CHANGE) {
1150 dev->network_started = 1;
1151 printf("USB network up!\n");
1154 req->context = NULL;
1157 static void issue_start_status(struct eth_dev *dev)
1159 struct usb_request *req = dev->stat_req;
1160 struct usb_cdc_notification *event;
1166 * FIXME ugly idiom, maybe we'd be better with just
1167 * a "cancel the whole queue" primitive since any
1168 * unlink-one primitive has way too many error modes.
1169 * here, we "know" toggle is already clear...
1171 * FIXME iff req->context != null just dequeue it
1173 usb_ep_disable(dev->status_ep);
1174 usb_ep_enable(dev->status_ep, dev->status);
1177 * 3.8.1 says to issue first NETWORK_CONNECTION, then
1178 * a SPEED_CHANGE. could be useful in some configs.
1181 event->bmRequestType = 0xA1;
1182 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
1183 event->wValue = __constant_cpu_to_le16(1); /* connected */
1184 event->wIndex = __constant_cpu_to_le16(1);
1187 req->length = sizeof *event;
1188 req->complete = eth_status_complete;
1191 value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
1193 debug("status buf queue --> %d\n", value);
1198 /*-------------------------------------------------------------------------*/
1200 static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
1202 if (req->status || req->actual != req->length)
1203 debug("setup complete --> %d, %d/%d\n",
1204 req->status, req->actual, req->length);
1207 #ifdef CONFIG_USB_ETH_RNDIS
1209 static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
1211 if (req->status || req->actual != req->length)
1212 debug("rndis response complete --> %d, %d/%d\n",
1213 req->status, req->actual, req->length);
1215 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1218 static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
1220 struct eth_dev *dev = ep->driver_data;
1223 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1224 status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
1226 error("%s: rndis parse error %d", __func__, status);
1232 * The setup() callback implements all the ep0 functionality that's not
1233 * handled lower down. CDC has a number of less-common features:
1235 * - two interfaces: control, and ethernet data
1236 * - Ethernet data interface has two altsettings: default, and active
1237 * - class-specific descriptors for the control interface
1238 * - class-specific control requests
1241 eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1243 struct eth_dev *dev = get_gadget_data(gadget);
1244 struct usb_request *req = dev->req;
1245 int value = -EOPNOTSUPP;
1246 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1247 u16 wValue = le16_to_cpu(ctrl->wValue);
1248 u16 wLength = le16_to_cpu(ctrl->wLength);
1251 * descriptors just go into the pre-allocated ep0 buffer,
1252 * while config change events may enable network traffic.
1255 debug("%s\n", __func__);
1257 req->complete = eth_setup_complete;
1258 switch (ctrl->bRequest) {
1260 case USB_REQ_GET_DESCRIPTOR:
1261 if (ctrl->bRequestType != USB_DIR_IN)
1263 switch (wValue >> 8) {
1266 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1267 value = min(wLength, (u16) sizeof device_desc);
1268 memcpy(req->buf, &device_desc, value);
1270 case USB_DT_DEVICE_QUALIFIER:
1271 if (!gadget_is_dualspeed(gadget))
1273 value = min(wLength, (u16) sizeof dev_qualifier);
1274 memcpy(req->buf, &dev_qualifier, value);
1277 case USB_DT_OTHER_SPEED_CONFIG:
1278 if (!gadget_is_dualspeed(gadget))
1282 value = config_buf(gadget, req->buf,
1285 gadget_is_otg(gadget));
1287 value = min(wLength, (u16) value);
1291 value = usb_gadget_get_string(&stringtab,
1292 wValue & 0xff, req->buf);
1295 value = min(wLength, (u16) value);
1301 case USB_REQ_SET_CONFIGURATION:
1302 if (ctrl->bRequestType != 0)
1304 if (gadget->a_hnp_support)
1305 debug("HNP available\n");
1306 else if (gadget->a_alt_hnp_support)
1307 debug("HNP needs a different root port\n");
1308 value = eth_set_config(dev, wValue, GFP_ATOMIC);
1310 case USB_REQ_GET_CONFIGURATION:
1311 if (ctrl->bRequestType != USB_DIR_IN)
1313 *(u8 *)req->buf = dev->config;
1314 value = min(wLength, (u16) 1);
1317 case USB_REQ_SET_INTERFACE:
1318 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1322 if (!cdc_active(dev) && wIndex != 0)
1326 * PXA hardware partially handles SET_INTERFACE;
1327 * we need to kluge around that interference.
1329 if (gadget_is_pxa(gadget)) {
1330 value = eth_set_config(dev, DEV_CONFIG_VALUE,
1333 * PXA25x driver use non-CDC ethernet gadget.
1334 * But only _CDC and _RNDIS code can signalize
1335 * that network is working. So we signalize it
1338 dev->network_started = 1;
1339 debug("USB network up!\n");
1343 #ifdef CONFIG_USB_ETH_CDC
1345 case 0: /* control/master intf */
1349 usb_ep_disable(dev->status_ep);
1350 usb_ep_enable(dev->status_ep, dev->status);
1355 case 1: /* data intf */
1358 usb_ep_disable(dev->in_ep);
1359 usb_ep_disable(dev->out_ep);
1362 * CDC requires the data transfers not be done from
1363 * the default interface setting ... also, setting
1364 * the non-default interface resets filters etc.
1367 if (!cdc_active(dev))
1369 usb_ep_enable(dev->in_ep, dev->in);
1370 usb_ep_enable(dev->out_ep, dev->out);
1371 dev->cdc_filter = DEFAULT_FILTER;
1373 issue_start_status(dev);
1374 eth_start(dev, GFP_ATOMIC);
1381 * FIXME this is wrong, as is the assumption that
1382 * all non-PXA hardware talks real CDC ...
1384 debug("set_interface ignored!\n");
1385 #endif /* CONFIG_USB_ETH_CDC */
1389 case USB_REQ_GET_INTERFACE:
1390 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1394 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
1397 /* for CDC, iff carrier is on, data interface is active. */
1398 if (rndis_active(dev) || wIndex != 1)
1399 *(u8 *)req->buf = 0;
1401 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1402 /* carrier always ok ...*/
1403 *(u8 *)req->buf = 1 ;
1405 value = min(wLength, (u16) 1);
1408 #ifdef CONFIG_USB_ETH_CDC
1409 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
1411 * see 6.2.30: no data, wIndex = interface,
1412 * wValue = packet filter bitmap
1414 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1419 debug("packet filter %02x\n", wValue);
1420 dev->cdc_filter = wValue;
1426 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1427 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1428 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1429 * case USB_CDC_GET_ETHERNET_STATISTIC:
1432 #endif /* CONFIG_USB_ETH_CDC */
1434 #ifdef CONFIG_USB_ETH_RNDIS
1436 * RNDIS uses the CDC command encapsulation mechanism to implement
1437 * an RPC scheme, with much getting/setting of attributes by OID.
1439 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1440 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1441 || !rndis_active(dev)
1442 || wLength > USB_BUFSIZ
1444 || rndis_control_intf.bInterfaceNumber
1447 /* read the request, then process it */
1449 req->complete = rndis_command_complete;
1450 /* later, rndis_control_ack () sends a notification */
1453 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1454 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1455 == ctrl->bRequestType
1456 && rndis_active(dev)
1457 /* && wLength >= 0x0400 */
1459 && rndis_control_intf.bInterfaceNumber
1464 /* return the result */
1465 buf = rndis_get_next_response(dev->rndis_config, &n);
1467 memcpy(req->buf, buf, n);
1468 req->complete = rndis_response_complete;
1469 rndis_free_response(dev->rndis_config, buf);
1472 /* else stalls ... spec says to avoid that */
1478 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
1479 ctrl->bRequestType, ctrl->bRequest,
1480 wValue, wIndex, wLength);
1483 /* respond with data transfer before status phase? */
1485 debug("respond with data transfer before status phase\n");
1486 req->length = value;
1487 req->zero = value < wLength
1488 && (value % gadget->ep0->maxpacket) == 0;
1489 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1491 debug("ep_queue --> %d\n", value);
1493 eth_setup_complete(gadget->ep0, req);
1497 /* host either stalls (value < 0) or reports success */
1501 /*-------------------------------------------------------------------------*/
1503 static void rx_complete(struct usb_ep *ep, struct usb_request *req);
1505 static int rx_submit(struct eth_dev *dev, struct usb_request *req,
1508 int retval = -ENOMEM;
1512 * Padding up to RX_EXTRA handles minor disagreements with host.
1513 * Normally we use the USB "terminate on short read" convention;
1514 * so allow up to (N*maxpacket), since that memory is normally
1515 * already allocated. Some hardware doesn't deal well with short
1516 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1517 * byte off the end (to force hardware errors on overflow).
1519 * RNDIS uses internal framing, and explicitly allows senders to
1520 * pad to end-of-packet. That's potentially nice for speed,
1521 * but means receivers can't recover synch on their own.
1524 debug("%s\n", __func__);
1528 size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1529 size += dev->out_ep->maxpacket - 1;
1530 if (rndis_active(dev))
1531 size += sizeof(struct rndis_packet_msg_type);
1532 size -= size % dev->out_ep->maxpacket;
1535 * Some platforms perform better when IP packets are aligned,
1536 * but on at least one, checksumming fails otherwise. Note:
1537 * RNDIS headers involve variable numbers of LE32 values.
1540 req->buf = (u8 *)net_rx_packets[0];
1542 req->complete = rx_complete;
1544 retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
1547 error("rx submit --> %d", retval);
1552 static void rx_complete(struct usb_ep *ep, struct usb_request *req)
1554 struct eth_dev *dev = ep->driver_data;
1556 debug("%s: status %d\n", __func__, req->status);
1557 switch (req->status) {
1558 /* normal completion */
1560 if (rndis_active(dev)) {
1561 /* we know MaxPacketsPerTransfer == 1 here */
1562 int length = rndis_rm_hdr(req->buf, req->actual);
1565 req->length -= length;
1566 req->actual -= length;
1568 if (req->actual < ETH_HLEN || ETH_FRAME_LEN < req->actual) {
1570 dev->stats.rx_errors++;
1571 dev->stats.rx_length_errors++;
1572 debug("rx length %d\n", req->length);
1576 dev->stats.rx_packets++;
1577 dev->stats.rx_bytes += req->length;
1580 /* software-driven interface shutdown */
1581 case -ECONNRESET: /* unlink */
1582 case -ESHUTDOWN: /* disconnect etc */
1583 /* for hardware automagic (such as pxa) */
1584 case -ECONNABORTED: /* endpoint reset */
1589 dev->stats.rx_over_errors++;
1592 dev->stats.rx_errors++;
1596 packet_received = 1;
1599 static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
1602 dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
1607 dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
1615 usb_ep_free_request(dev->in_ep, dev->tx_req);
1617 error("can't alloc requests");
1621 static void tx_complete(struct usb_ep *ep, struct usb_request *req)
1623 struct eth_dev *dev = ep->driver_data;
1625 debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
1626 switch (req->status) {
1628 dev->stats.tx_errors++;
1629 debug("tx err %d\n", req->status);
1631 case -ECONNRESET: /* unlink */
1632 case -ESHUTDOWN: /* disconnect etc */
1635 dev->stats.tx_bytes += req->length;
1637 dev->stats.tx_packets++;
1642 static inline int eth_is_promisc(struct eth_dev *dev)
1644 /* no filters for the CDC subset; always promisc */
1645 if (subset_active(dev))
1647 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1651 static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1653 struct eth_dev *dev = netdev_priv(net);
1654 int length = skb->len;
1656 struct usb_request *req = NULL;
1657 unsigned long flags;
1659 /* apply outgoing CDC or RNDIS filters */
1660 if (!eth_is_promisc (dev)) {
1661 u8 *dest = skb->data;
1663 if (is_multicast_ethaddr(dest)) {
1666 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1667 * SET_ETHERNET_MULTICAST_FILTERS requests
1669 if (is_broadcast_ethaddr(dest))
1670 type = USB_CDC_PACKET_TYPE_BROADCAST;
1672 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1673 if (!(dev->cdc_filter & type)) {
1674 dev_kfree_skb_any (skb);
1678 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1681 spin_lock_irqsave(&dev->req_lock, flags);
1683 * this freelist can be empty if an interrupt triggered disconnect()
1684 * and reconfigured the gadget (shutting down this queue) after the
1685 * network stack decided to xmit but before we got the spinlock.
1687 if (list_empty(&dev->tx_reqs)) {
1688 spin_unlock_irqrestore(&dev->req_lock, flags);
1692 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1693 list_del (&req->list);
1695 /* temporarily stop TX queue when the freelist empties */
1696 if (list_empty (&dev->tx_reqs))
1697 netif_stop_queue (net);
1698 spin_unlock_irqrestore(&dev->req_lock, flags);
1700 /* no buffer copies needed, unless the network stack did it
1701 * or the hardware can't use skb buffers.
1702 * or there's not enough space for any RNDIS headers we need
1704 if (rndis_active(dev)) {
1705 struct sk_buff *skb_rndis;
1707 skb_rndis = skb_realloc_headroom (skb,
1708 sizeof (struct rndis_packet_msg_type));
1712 dev_kfree_skb_any (skb);
1714 rndis_add_hdr (skb);
1717 req->buf = skb->data;
1719 req->complete = tx_complete;
1721 /* use zlp framing on tx for strict CDC-Ether conformance,
1722 * though any robust network rx path ignores extra padding.
1723 * and some hardware doesn't like to write zlps.
1726 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1729 req->length = length;
1731 /* throttle highspeed IRQ rate back slightly */
1732 if (gadget_is_dualspeed(dev->gadget))
1733 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1734 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1737 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1740 DEBUG (dev, "tx queue err %d\n", retval);
1743 net->trans_start = jiffies;
1744 atomic_inc (&dev->tx_qlen);
1749 dev->stats.tx_dropped++;
1750 dev_kfree_skb_any (skb);
1751 spin_lock_irqsave(&dev->req_lock, flags);
1752 if (list_empty (&dev->tx_reqs))
1753 netif_start_queue (net);
1754 list_add (&req->list, &dev->tx_reqs);
1755 spin_unlock_irqrestore(&dev->req_lock, flags);
1760 /*-------------------------------------------------------------------------*/
1763 static void eth_unbind(struct usb_gadget *gadget)
1765 struct eth_dev *dev = get_gadget_data(gadget);
1767 debug("%s...\n", __func__);
1768 rndis_deregister(dev->rndis_config);
1771 /* we've already been disconnected ... no i/o is active */
1773 usb_ep_free_request(gadget->ep0, dev->req);
1776 if (dev->stat_req) {
1777 usb_ep_free_request(dev->status_ep, dev->stat_req);
1778 dev->stat_req = NULL;
1782 usb_ep_free_request(dev->in_ep, dev->tx_req);
1787 usb_ep_free_request(dev->out_ep, dev->rx_req);
1791 /* unregister_netdev (dev->net);*/
1792 /* free_netdev(dev->net);*/
1795 set_gadget_data(gadget, NULL);
1798 static void eth_disconnect(struct usb_gadget *gadget)
1800 eth_reset_config(get_gadget_data(gadget));
1801 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1804 static void eth_suspend(struct usb_gadget *gadget)
1809 static void eth_resume(struct usb_gadget *gadget)
1814 /*-------------------------------------------------------------------------*/
1816 #ifdef CONFIG_USB_ETH_RNDIS
1819 * The interrupt endpoint is used in RNDIS to notify the host when messages
1820 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1821 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1822 * REMOTE_NDIS_KEEPALIVE_MSG.
1824 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1825 * normally just one notification will be queued.
1828 static void rndis_control_ack_complete(struct usb_ep *ep,
1829 struct usb_request *req)
1831 struct eth_dev *dev = ep->driver_data;
1833 debug("%s...\n", __func__);
1834 if (req->status || req->actual != req->length)
1835 debug("rndis control ack complete --> %d, %d/%d\n",
1836 req->status, req->actual, req->length);
1838 if (!dev->network_started) {
1839 if (rndis_get_state(dev->rndis_config)
1840 == RNDIS_DATA_INITIALIZED) {
1841 dev->network_started = 1;
1842 printf("USB RNDIS network up!\n");
1846 req->context = NULL;
1848 if (req != dev->stat_req)
1849 usb_ep_free_request(ep, req);
1852 static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
1854 static int rndis_control_ack(struct eth_device *net)
1856 struct ether_priv *priv = (struct ether_priv *)net->priv;
1857 struct eth_dev *dev = &priv->ethdev;
1859 struct usb_request *resp = dev->stat_req;
1861 /* in case RNDIS calls this after disconnect */
1863 debug("status ENODEV\n");
1867 /* in case queue length > 1 */
1868 if (resp->context) {
1869 resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
1872 resp->buf = rndis_resp_buf;
1876 * Send RNDIS RESPONSE_AVAILABLE notification;
1877 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1880 resp->complete = rndis_control_ack_complete;
1881 resp->context = dev;
1883 *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
1884 *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
1886 length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
1889 rndis_control_ack_complete(dev->status_ep, resp);
1897 #define rndis_control_ack NULL
1901 static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
1903 if (rndis_active(dev)) {
1904 rndis_set_param_medium(dev->rndis_config,
1906 BITRATE(dev->gadget)/100);
1907 rndis_signal_connect(dev->rndis_config);
1911 static int eth_stop(struct eth_dev *dev)
1913 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1915 unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1918 if (rndis_active(dev)) {
1919 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
1920 rndis_signal_disconnect(dev->rndis_config);
1922 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1923 /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1925 while (get_timer(ts) < timeout)
1926 usb_gadget_handle_interrupts(0);
1929 rndis_uninit(dev->rndis_config);
1936 /*-------------------------------------------------------------------------*/
1938 static int is_eth_addr_valid(char *str)
1940 if (strlen(str) == 17) {
1945 /* see if it looks like an ethernet address */
1949 for (i = 0; i < 6; i++) {
1950 char term = (i == 5 ? '\0' : ':');
1952 ea[i] = simple_strtol(p, &q, 16);
1954 if ((q - p) != 2 || *q++ != term)
1960 /* Now check the contents. */
1961 return is_valid_ethaddr(ea);
1966 static u8 nibble(unsigned char c)
1968 if (likely(isdigit(c)))
1971 if (likely(isxdigit(c)))
1972 return 10 + c - 'A';
1976 static int get_ether_addr(const char *str, u8 *dev_addr)
1981 for (i = 0; i < 6; i++) {
1984 if ((*str == '.') || (*str == ':'))
1986 num = nibble(*str++) << 4;
1987 num |= (nibble(*str++));
1990 if (is_valid_ethaddr(dev_addr))
1996 static int eth_bind(struct usb_gadget *gadget)
1998 struct eth_dev *dev = &l_priv->ethdev;
1999 u8 cdc = 1, zlp = 1, rndis = 1;
2000 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
2001 int status = -ENOMEM;
2005 /* these flags are only ever cleared; compiler take note */
2006 #ifndef CONFIG_USB_ETH_CDC
2009 #ifndef CONFIG_USB_ETH_RNDIS
2013 * Because most host side USB stacks handle CDC Ethernet, that
2014 * standard protocol is _strongly_ preferred for interop purposes.
2015 * (By everyone except Microsoft.)
2017 if (gadget_is_pxa(gadget)) {
2018 /* pxa doesn't support altsettings */
2020 } else if (gadget_is_musbhdrc(gadget)) {
2021 /* reduce tx dma overhead by avoiding special cases */
2023 } else if (gadget_is_sh(gadget)) {
2024 /* sh doesn't support multiple interfaces or configs */
2027 } else if (gadget_is_sa1100(gadget)) {
2028 /* hardware can't write zlps */
2031 * sa1100 CAN do CDC, without status endpoint ... we use
2032 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2037 gcnum = usb_gadget_controller_number(gadget);
2039 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
2042 * can't assume CDC works. don't want to default to
2043 * anything less functional on CDC-capable hardware,
2044 * so we fail in this case.
2046 error("controller '%s' not recognized",
2052 * If there's an RNDIS configuration, that's what Windows wants to
2053 * be using ... so use these product IDs here and in the "linux.inf"
2054 * needed to install MSFT drivers. Current Linux kernels will use
2055 * the second configuration if it's CDC Ethernet, and need some help
2056 * to choose the right configuration otherwise.
2059 #if defined(CONFIG_USB_RNDIS_VENDOR_ID) && defined(CONFIG_USB_RNDIS_PRODUCT_ID)
2060 device_desc.idVendor =
2061 __constant_cpu_to_le16(CONFIG_USB_RNDIS_VENDOR_ID);
2062 device_desc.idProduct =
2063 __constant_cpu_to_le16(CONFIG_USB_RNDIS_PRODUCT_ID);
2065 device_desc.idVendor =
2066 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2067 device_desc.idProduct =
2068 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2070 sprintf(product_desc, "RNDIS/%s", driver_desc);
2073 * CDC subset ... recognized by Linux since 2.4.10, but Windows
2074 * drivers aren't widely available. (That may be improved by
2075 * supporting one submode of the "SAFE" variant of MDLM.)
2078 #if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID)
2079 device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID);
2080 device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID);
2083 device_desc.idVendor =
2084 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2085 device_desc.idProduct =
2086 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2090 /* support optional vendor/distro customization */
2092 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2094 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
2096 strlcpy(product_desc, iProduct, sizeof product_desc);
2097 if (iSerialNumber) {
2098 device_desc.iSerialNumber = STRING_SERIALNUMBER,
2099 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
2102 /* all we really need is bulk IN/OUT */
2103 usb_ep_autoconfig_reset(gadget);
2104 in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
2107 error("can't autoconfigure on %s\n",
2111 in_ep->driver_data = in_ep; /* claim */
2113 out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
2116 out_ep->driver_data = out_ep; /* claim */
2118 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2120 * CDC Ethernet control interface doesn't require a status endpoint.
2121 * Since some hosts expect one, try to allocate one anyway.
2124 status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
2126 status_ep->driver_data = status_ep; /* claim */
2128 error("can't run RNDIS on %s", gadget->name);
2130 #ifdef CONFIG_USB_ETH_CDC
2132 control_intf.bNumEndpoints = 0;
2133 /* FIXME remove endpoint from descriptor list */
2139 /* one config: cdc, else minimal subset */
2141 eth_config.bNumInterfaces = 1;
2142 eth_config.iConfiguration = STRING_SUBSET;
2145 * use functions to set these up, in case we're built to work
2146 * with multiple controllers and must override CDC Ethernet.
2148 fs_subset_descriptors();
2149 hs_subset_descriptors();
2152 usb_gadget_set_selfpowered(gadget);
2154 /* For now RNDIS is always a second config */
2156 device_desc.bNumConfigurations = 2;
2158 if (gadget_is_dualspeed(gadget)) {
2160 dev_qualifier.bNumConfigurations = 2;
2162 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2164 /* assumes ep0 uses the same value for both speeds ... */
2165 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2167 /* and that all endpoints are dual-speed */
2168 hs_source_desc.bEndpointAddress =
2169 fs_source_desc.bEndpointAddress;
2170 hs_sink_desc.bEndpointAddress =
2171 fs_sink_desc.bEndpointAddress;
2172 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2174 hs_status_desc.bEndpointAddress =
2175 fs_status_desc.bEndpointAddress;
2179 if (gadget_is_otg(gadget)) {
2180 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2181 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2182 eth_config.bMaxPower = 4;
2183 #ifdef CONFIG_USB_ETH_RNDIS
2184 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2185 rndis_config.bMaxPower = 4;
2190 /* network device setup */
2191 dev->net = &l_priv->netdev;
2197 dev->out_ep = out_ep;
2198 dev->status_ep = status_ep;
2201 * Module params for these addresses should come from ID proms.
2202 * The host side address is used with CDC and RNDIS, and commonly
2203 * ends up in a persistent config database. It's not clear if
2204 * host side code for the SAFE thing cares -- its original BLAN
2205 * thing didn't, Sharp never assigned those addresses on Zaurii.
2207 get_ether_addr(dev_addr, dev->net->enetaddr);
2209 memset(tmp, 0, sizeof(tmp));
2210 memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
2212 get_ether_addr(host_addr, dev->host_mac);
2214 sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
2215 dev->host_mac[0], dev->host_mac[1],
2216 dev->host_mac[2], dev->host_mac[3],
2217 dev->host_mac[4], dev->host_mac[5]);
2220 status = rndis_init();
2222 error("can't init RNDIS, %d", status);
2228 * use PKTSIZE (or aligned... from u-boot) and set
2229 * wMaxSegmentSize accordingly
2231 dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
2233 /* preallocate control message data and buffer */
2234 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
2237 dev->req->buf = control_req;
2238 dev->req->complete = eth_setup_complete;
2240 /* ... and maybe likewise for status transfer */
2241 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2242 if (dev->status_ep) {
2243 dev->stat_req = usb_ep_alloc_request(dev->status_ep,
2245 if (!dev->stat_req) {
2246 usb_ep_free_request(dev->status_ep, dev->req);
2250 dev->stat_req->buf = status_req;
2251 dev->stat_req->context = NULL;
2255 /* finish hookup to lower layer ... */
2256 dev->gadget = gadget;
2257 set_gadget_data(gadget, dev);
2258 gadget->ep0->driver_data = dev;
2261 * two kinds of host-initiated state changes:
2262 * - iff DATA transfer is active, carrier is "on"
2263 * - tx queueing enabled if open *and* carrier is "on"
2266 printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
2267 out_ep->name, in_ep->name,
2268 status_ep ? " STATUS " : "",
2269 status_ep ? status_ep->name : ""
2271 printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2272 dev->net->enetaddr[0], dev->net->enetaddr[1],
2273 dev->net->enetaddr[2], dev->net->enetaddr[3],
2274 dev->net->enetaddr[4], dev->net->enetaddr[5]);
2277 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2278 dev->host_mac[0], dev->host_mac[1],
2279 dev->host_mac[2], dev->host_mac[3],
2280 dev->host_mac[4], dev->host_mac[5]);
2285 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2287 dev->rndis_config = rndis_register(rndis_control_ack);
2288 if (dev->rndis_config < 0) {
2291 debug("RNDIS setup failed\n");
2296 /* these set up a lot of the OIDs that RNDIS needs */
2297 rndis_set_host_mac(dev->rndis_config, dev->host_mac);
2298 if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
2299 &dev->stats, &dev->cdc_filter))
2301 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2304 if (rndis_set_param_medium(dev->rndis_config,
2305 NDIS_MEDIUM_802_3, 0))
2307 printf("RNDIS ready\n");
2312 error("%s failed, status = %d", __func__, status);
2317 /*-------------------------------------------------------------------------*/
2319 #ifdef CONFIG_DM_USB
2320 int dm_usb_init(struct eth_dev *e_dev)
2322 struct udevice *dev = NULL;
2325 ret = uclass_first_device(UCLASS_USB_DEV_GENERIC, &dev);
2327 error("No USB device found\n");
2331 e_dev->usb_udev = dev;
2337 static int _usb_eth_init(struct ether_priv *priv)
2339 struct eth_dev *dev = &priv->ethdev;
2340 struct usb_gadget *gadget;
2342 unsigned long timeout = USB_CONNECT_TIMEOUT;
2344 #ifdef CONFIG_DM_USB
2345 if (dm_usb_init(dev)) {
2346 error("USB ether not found\n");
2350 board_usb_init(0, USB_INIT_DEVICE);
2353 /* Configure default mac-addresses for the USB ethernet device */
2354 #ifdef CONFIG_USBNET_DEV_ADDR
2355 strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
2357 #ifdef CONFIG_USBNET_HOST_ADDR
2358 strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
2360 /* Check if the user overruled the MAC addresses */
2361 if (getenv("usbnet_devaddr"))
2362 strlcpy(dev_addr, getenv("usbnet_devaddr"),
2365 if (getenv("usbnet_hostaddr"))
2366 strlcpy(host_addr, getenv("usbnet_hostaddr"),
2369 if (!is_eth_addr_valid(dev_addr)) {
2370 error("Need valid 'usbnet_devaddr' to be set");
2373 if (!is_eth_addr_valid(host_addr)) {
2374 error("Need valid 'usbnet_hostaddr' to be set");
2378 priv->eth_driver.speed = DEVSPEED;
2379 priv->eth_driver.bind = eth_bind;
2380 priv->eth_driver.unbind = eth_unbind;
2381 priv->eth_driver.setup = eth_setup;
2382 priv->eth_driver.reset = eth_disconnect;
2383 priv->eth_driver.disconnect = eth_disconnect;
2384 priv->eth_driver.suspend = eth_suspend;
2385 priv->eth_driver.resume = eth_resume;
2386 if (usb_gadget_register_driver(&priv->eth_driver) < 0)
2389 dev->network_started = 0;
2391 packet_received = 0;
2394 gadget = dev->gadget;
2395 usb_gadget_connect(gadget);
2397 if (getenv("cdc_connect_timeout"))
2398 timeout = simple_strtoul(getenv("cdc_connect_timeout"),
2399 NULL, 10) * CONFIG_SYS_HZ;
2401 while (!dev->network_started) {
2402 /* Handle control-c and timeouts */
2403 if (ctrlc() || (get_timer(ts) > timeout)) {
2404 error("The remote end did not respond in time.");
2407 usb_gadget_handle_interrupts(0);
2410 packet_received = 0;
2411 rx_submit(dev, dev->rx_req, 0);
2417 static int _usb_eth_send(struct ether_priv *priv, void *packet, int length)
2420 void *rndis_pkt = NULL;
2421 struct eth_dev *dev = &priv->ethdev;
2422 struct usb_request *req = dev->tx_req;
2424 unsigned long timeout = USB_CONNECT_TIMEOUT;
2426 debug("%s:...\n", __func__);
2428 /* new buffer is needed to include RNDIS header */
2429 if (rndis_active(dev)) {
2430 rndis_pkt = malloc(length +
2431 sizeof(struct rndis_packet_msg_type));
2433 error("No memory to alloc RNDIS packet");
2436 rndis_add_hdr(rndis_pkt, length);
2437 memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
2440 length += sizeof(struct rndis_packet_msg_type);
2443 req->context = NULL;
2444 req->complete = tx_complete;
2447 * use zlp framing on tx for strict CDC-Ether conformance,
2448 * though any robust network rx path ignores extra padding.
2449 * and some hardware doesn't like to write zlps.
2452 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2455 req->length = length;
2457 /* throttle highspeed IRQ rate back slightly */
2458 if (gadget_is_dualspeed(dev->gadget))
2459 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2460 ? ((dev->tx_qlen % qmult) != 0) : 0;
2466 retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
2469 debug("%s: packet queued\n", __func__);
2470 while (!packet_sent) {
2471 if (get_timer(ts) > timeout) {
2472 printf("timeout sending packets to usb ethernet\n");
2475 usb_gadget_handle_interrupts(0);
2482 dev->stats.tx_dropped++;
2486 static int _usb_eth_recv(struct ether_priv *priv)
2488 usb_gadget_handle_interrupts(0);
2493 void _usb_eth_halt(struct ether_priv *priv)
2495 struct eth_dev *dev = &priv->ethdev;
2497 /* If the gadget not registered, simple return */
2502 * Some USB controllers may need additional deinitialization here
2503 * before dropping pull-up (also due to hardware issues).
2504 * For example: unhandled interrupt with status stage started may
2505 * bring the controller to fully broken state (until board reset).
2506 * There are some variants to debug and fix such cases:
2507 * 1) In the case of RNDIS connection eth_stop can perform additional
2508 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2509 * 2) 'pullup' callback in your UDC driver can be improved to perform
2510 * this deinitialization.
2514 usb_gadget_disconnect(dev->gadget);
2516 /* Clear pending interrupt */
2517 if (dev->network_started) {
2518 usb_gadget_handle_interrupts(0);
2519 dev->network_started = 0;
2522 usb_gadget_unregister_driver(&priv->eth_driver);
2523 #ifdef CONFIG_DM_USB
2524 device_remove(dev->usb_udev);
2526 board_usb_cleanup(0, USB_INIT_DEVICE);
2530 static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
2532 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2534 return _usb_eth_init(priv);
2537 static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
2539 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2541 return _usb_eth_send(priv, packet, length);
2544 static int usb_eth_recv(struct eth_device *netdev)
2546 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2547 struct eth_dev *dev = &priv->ethdev;
2550 ret = _usb_eth_recv(priv);
2552 error("error packet receive\n");
2556 if (!packet_received)
2560 net_process_received_packet(net_rx_packets[0],
2561 dev->rx_req->length);
2563 error("dev->rx_req invalid");
2565 packet_received = 0;
2566 rx_submit(dev, dev->rx_req, 0);
2571 void usb_eth_halt(struct eth_device *netdev)
2573 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2575 _usb_eth_halt(priv);
2578 int usb_eth_initialize(bd_t *bi)
2580 struct eth_device *netdev = &l_priv->netdev;
2582 strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
2584 netdev->init = usb_eth_init;
2585 netdev->send = usb_eth_send;
2586 netdev->recv = usb_eth_recv;
2587 netdev->halt = usb_eth_halt;
2588 netdev->priv = l_priv;
2590 #ifdef CONFIG_MCAST_TFTP
2591 #error not supported
2593 eth_register(netdev);