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 /*-------------------------------------------------------------------------*/
144 static struct eth_dev l_ethdev;
145 static struct eth_device l_netdev;
146 static struct usb_gadget_driver eth_driver;
148 /*-------------------------------------------------------------------------*/
150 /* "main" config is either CDC, or its simple subset */
151 static inline int is_cdc(struct eth_dev *dev)
153 #if !defined(CONFIG_USB_ETH_SUBSET)
154 return 1; /* only cdc possible */
155 #elif !defined(CONFIG_USB_ETH_CDC)
156 return 0; /* only subset possible */
158 return dev->cdc; /* depends on what hardware we found */
162 /* "secondary" RNDIS config may sometimes be activated */
163 static inline int rndis_active(struct eth_dev *dev)
165 #ifdef CONFIG_USB_ETH_RNDIS
172 #define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
173 #define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev))
175 #define DEFAULT_QLEN 2 /* double buffering by default */
177 /* peak bulk transfer bits-per-second */
178 #define HS_BPS (13 * 512 * 8 * 1000 * 8)
179 #define FS_BPS (19 * 64 * 1 * 1000 * 8)
181 #ifdef CONFIG_USB_GADGET_DUALSPEED
182 #define DEVSPEED USB_SPEED_HIGH
184 #ifdef CONFIG_USB_ETH_QMULT
185 #define qmult CONFIG_USB_ETH_QMULT
190 /* for dual-speed hardware, use deeper queues at highspeed */
191 #define qlen(gadget) \
192 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
194 static inline int BITRATE(struct usb_gadget *g)
196 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
199 #else /* full speed (low speed doesn't do bulk) */
203 #define DEVSPEED USB_SPEED_FULL
205 #define qlen(gadget) DEFAULT_QLEN
207 static inline int BITRATE(struct usb_gadget *g)
213 /*-------------------------------------------------------------------------*/
216 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
217 * Instead: allocate your own, using normal USB-IF procedures.
221 * Thanks to NetChip Technologies for donating this product ID.
222 * It's for devices with only CDC Ethernet configurations.
224 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
225 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
228 * For hardware that can't talk CDC, we use the same vendor ID that
229 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
230 * with pxa250. We're protocol-compatible, if the host-side drivers
231 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
232 * drivers that need to hard-wire endpoint numbers have a hook.
234 * The protocol is a minimal subset of CDC Ether, which works on any bulk
235 * hardware that's not deeply broken ... even on hardware that can't talk
236 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
237 * doesn't handle control-OUT).
239 #define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */
240 #define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */
243 * For hardware that can talk RNDIS and either of the above protocols,
244 * use this ID ... the windows INF files will know it. Unless it's
245 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
246 * the non-RNDIS configuration.
248 #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
249 #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
252 * Some systems will want different product identifers published in the
253 * device descriptor, either numbers or strings or both. These string
254 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
258 * Emulating them in eth_bind:
259 * static ushort idVendor;
260 * static ushort idProduct;
263 #if defined(CONFIG_USBNET_MANUFACTURER)
264 static char *iManufacturer = CONFIG_USBNET_MANUFACTURER;
266 static char *iManufacturer = "U-Boot";
269 /* These probably need to be configurable. */
270 static ushort bcdDevice;
271 static char *iProduct;
272 static char *iSerialNumber;
274 static char dev_addr[18];
276 static char host_addr[18];
279 /*-------------------------------------------------------------------------*/
282 * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
283 * ep0 implementation: descriptors, config management, setup().
284 * also optional class-specific notification interrupt transfer.
288 * DESCRIPTORS ... most are static, but strings and (full) configuration
289 * descriptors are built on demand. For now we do either full CDC, or
290 * our simple subset, with RNDIS as an optional second configuration.
292 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
293 * the class descriptors match a modem (they're ignored; it's really just
294 * Ethernet functionality), they don't need the NOP altsetting, and the
295 * status transfer endpoint isn't optional.
298 #define STRING_MANUFACTURER 1
299 #define STRING_PRODUCT 2
300 #define STRING_ETHADDR 3
301 #define STRING_DATA 4
302 #define STRING_CONTROL 5
303 #define STRING_RNDIS_CONTROL 6
305 #define STRING_SUBSET 8
306 #define STRING_RNDIS 9
307 #define STRING_SERIALNUMBER 10
309 /* holds our biggest descriptor (or RNDIS response) */
310 #define USB_BUFSIZ 256
313 * This device advertises one configuration, eth_config, unless RNDIS
314 * is enabled (rndis_config) on hardware supporting at least two configs.
316 * NOTE: Controllers like superh_udc should probably be able to use
317 * an RNDIS-only configuration.
319 * FIXME define some higher-powered configurations to make it easier
320 * to recharge batteries ...
323 #define DEV_CONFIG_VALUE 1 /* cdc or subset */
324 #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
326 static struct usb_device_descriptor
328 .bLength = sizeof device_desc,
329 .bDescriptorType = USB_DT_DEVICE,
331 .bcdUSB = __constant_cpu_to_le16(0x0200),
333 .bDeviceClass = USB_CLASS_COMM,
334 .bDeviceSubClass = 0,
335 .bDeviceProtocol = 0,
337 .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
338 .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
339 .iManufacturer = STRING_MANUFACTURER,
340 .iProduct = STRING_PRODUCT,
341 .bNumConfigurations = 1,
344 static struct usb_otg_descriptor
346 .bLength = sizeof otg_descriptor,
347 .bDescriptorType = USB_DT_OTG,
349 .bmAttributes = USB_OTG_SRP,
352 static struct usb_config_descriptor
354 .bLength = sizeof eth_config,
355 .bDescriptorType = USB_DT_CONFIG,
357 /* compute wTotalLength on the fly */
359 .bConfigurationValue = DEV_CONFIG_VALUE,
360 .iConfiguration = STRING_CDC,
361 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
365 #ifdef CONFIG_USB_ETH_RNDIS
366 static struct usb_config_descriptor
368 .bLength = sizeof rndis_config,
369 .bDescriptorType = USB_DT_CONFIG,
371 /* compute wTotalLength on the fly */
373 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
374 .iConfiguration = STRING_RNDIS,
375 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
381 * Compared to the simple CDC subset, the full CDC Ethernet model adds
382 * three class descriptors, two interface descriptors, optional status
383 * endpoint. Both have a "data" interface and two bulk endpoints.
384 * There are also differences in how control requests are handled.
386 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
387 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
388 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
389 * work around those bugs) are unlikely to ever come from MSFT, you may
390 * wish to avoid using RNDIS.
392 * MCCI offers an alternative to RNDIS if you need to connect to Windows
393 * but have hardware that can't support CDC Ethernet. We add descriptors
394 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
395 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
396 * get those drivers from MCCI, or bundled with various products.
399 #ifdef CONFIG_USB_ETH_CDC
400 static struct usb_interface_descriptor
402 .bLength = sizeof control_intf,
403 .bDescriptorType = USB_DT_INTERFACE,
405 .bInterfaceNumber = 0,
406 /* status endpoint is optional; this may be patched later */
408 .bInterfaceClass = USB_CLASS_COMM,
409 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
410 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
411 .iInterface = STRING_CONTROL,
415 #ifdef CONFIG_USB_ETH_RNDIS
416 static const struct usb_interface_descriptor
417 rndis_control_intf = {
418 .bLength = sizeof rndis_control_intf,
419 .bDescriptorType = USB_DT_INTERFACE,
421 .bInterfaceNumber = 0,
423 .bInterfaceClass = USB_CLASS_COMM,
424 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
425 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
426 .iInterface = STRING_RNDIS_CONTROL,
430 static const struct usb_cdc_header_desc header_desc = {
431 .bLength = sizeof header_desc,
432 .bDescriptorType = USB_DT_CS_INTERFACE,
433 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
435 .bcdCDC = __constant_cpu_to_le16(0x0110),
438 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
440 static const struct usb_cdc_union_desc union_desc = {
441 .bLength = sizeof union_desc,
442 .bDescriptorType = USB_DT_CS_INTERFACE,
443 .bDescriptorSubType = USB_CDC_UNION_TYPE,
445 .bMasterInterface0 = 0, /* index of control interface */
446 .bSlaveInterface0 = 1, /* index of DATA interface */
449 #endif /* CDC || RNDIS */
451 #ifdef CONFIG_USB_ETH_RNDIS
453 static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
454 .bLength = sizeof call_mgmt_descriptor,
455 .bDescriptorType = USB_DT_CS_INTERFACE,
456 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
458 .bmCapabilities = 0x00,
459 .bDataInterface = 0x01,
462 static const struct usb_cdc_acm_descriptor acm_descriptor = {
463 .bLength = sizeof acm_descriptor,
464 .bDescriptorType = USB_DT_CS_INTERFACE,
465 .bDescriptorSubType = USB_CDC_ACM_TYPE,
467 .bmCapabilities = 0x00,
472 #ifndef CONFIG_USB_ETH_CDC
475 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
476 * ways: data endpoints live in the control interface, there's no data
477 * interface, and it's not used to talk to a cell phone radio.
480 static const struct usb_cdc_mdlm_desc mdlm_desc = {
481 .bLength = sizeof mdlm_desc,
482 .bDescriptorType = USB_DT_CS_INTERFACE,
483 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
485 .bcdVersion = __constant_cpu_to_le16(0x0100),
487 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
488 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
493 * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
494 * can't really use its struct. All we do here is say that we're using
495 * the submode of "SAFE" which directly matches the CDC Subset.
497 static const u8 mdlm_detail_desc[] = {
500 USB_CDC_MDLM_DETAIL_TYPE,
503 0, /* network control capabilities (none) */
504 0, /* network data capabilities ("raw" encapsulation) */
509 static const struct usb_cdc_ether_desc ether_desc = {
510 .bLength = sizeof(ether_desc),
511 .bDescriptorType = USB_DT_CS_INTERFACE,
512 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
514 /* this descriptor actually adds value, surprise! */
515 .iMACAddress = STRING_ETHADDR,
516 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
517 .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN),
518 .wNumberMCFilters = __constant_cpu_to_le16(0),
519 .bNumberPowerFilters = 0,
522 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
525 * include the status endpoint if we can, even where it's optional.
526 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
527 * packet, to simplify cancellation; and a big transfer interval, to
528 * waste less bandwidth.
530 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
531 * if they ignore the connect/disconnect notifications that real aether
532 * can provide. more advanced cdc configurations might want to support
533 * encapsulated commands (vendor-specific, using control-OUT).
535 * RNDIS requires the status endpoint, since it uses that encapsulation
536 * mechanism for its funky RPC scheme.
539 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
540 #define STATUS_BYTECOUNT 16 /* 8 byte header + data */
542 static struct usb_endpoint_descriptor
544 .bLength = USB_DT_ENDPOINT_SIZE,
545 .bDescriptorType = USB_DT_ENDPOINT,
547 .bEndpointAddress = USB_DIR_IN,
548 .bmAttributes = USB_ENDPOINT_XFER_INT,
549 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
550 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
554 #ifdef CONFIG_USB_ETH_CDC
556 /* the default data interface has no endpoints ... */
558 static const struct usb_interface_descriptor
560 .bLength = sizeof data_nop_intf,
561 .bDescriptorType = USB_DT_INTERFACE,
563 .bInterfaceNumber = 1,
564 .bAlternateSetting = 0,
566 .bInterfaceClass = USB_CLASS_CDC_DATA,
567 .bInterfaceSubClass = 0,
568 .bInterfaceProtocol = 0,
571 /* ... but the "real" data interface has two bulk endpoints */
573 static const struct usb_interface_descriptor
575 .bLength = sizeof data_intf,
576 .bDescriptorType = USB_DT_INTERFACE,
578 .bInterfaceNumber = 1,
579 .bAlternateSetting = 1,
581 .bInterfaceClass = USB_CLASS_CDC_DATA,
582 .bInterfaceSubClass = 0,
583 .bInterfaceProtocol = 0,
584 .iInterface = STRING_DATA,
589 #ifdef CONFIG_USB_ETH_RNDIS
591 /* RNDIS doesn't activate by changing to the "real" altsetting */
593 static const struct usb_interface_descriptor
595 .bLength = sizeof rndis_data_intf,
596 .bDescriptorType = USB_DT_INTERFACE,
598 .bInterfaceNumber = 1,
599 .bAlternateSetting = 0,
601 .bInterfaceClass = USB_CLASS_CDC_DATA,
602 .bInterfaceSubClass = 0,
603 .bInterfaceProtocol = 0,
604 .iInterface = STRING_DATA,
609 #ifdef CONFIG_USB_ETH_SUBSET
612 * "Simple" CDC-subset option is a simple vendor-neutral model that most
613 * full speed controllers can handle: one interface, two bulk endpoints.
615 * To assist host side drivers, we fancy it up a bit, and add descriptors
616 * so some host side drivers will understand it as a "SAFE" variant.
619 static const struct usb_interface_descriptor
621 .bLength = sizeof subset_data_intf,
622 .bDescriptorType = USB_DT_INTERFACE,
624 .bInterfaceNumber = 0,
625 .bAlternateSetting = 0,
627 .bInterfaceClass = USB_CLASS_COMM,
628 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
629 .bInterfaceProtocol = 0,
630 .iInterface = STRING_DATA,
635 static struct usb_endpoint_descriptor
637 .bLength = USB_DT_ENDPOINT_SIZE,
638 .bDescriptorType = USB_DT_ENDPOINT,
640 .bEndpointAddress = USB_DIR_IN,
641 .bmAttributes = USB_ENDPOINT_XFER_BULK,
642 .wMaxPacketSize = __constant_cpu_to_le16(64),
645 static struct usb_endpoint_descriptor
647 .bLength = USB_DT_ENDPOINT_SIZE,
648 .bDescriptorType = USB_DT_ENDPOINT,
650 .bEndpointAddress = USB_DIR_OUT,
651 .bmAttributes = USB_ENDPOINT_XFER_BULK,
652 .wMaxPacketSize = __constant_cpu_to_le16(64),
655 static const struct usb_descriptor_header *fs_eth_function[11] = {
656 (struct usb_descriptor_header *) &otg_descriptor,
657 #ifdef CONFIG_USB_ETH_CDC
658 /* "cdc" mode descriptors */
659 (struct usb_descriptor_header *) &control_intf,
660 (struct usb_descriptor_header *) &header_desc,
661 (struct usb_descriptor_header *) &union_desc,
662 (struct usb_descriptor_header *) ðer_desc,
663 /* NOTE: status endpoint may need to be removed */
664 (struct usb_descriptor_header *) &fs_status_desc,
665 /* data interface, with altsetting */
666 (struct usb_descriptor_header *) &data_nop_intf,
667 (struct usb_descriptor_header *) &data_intf,
668 (struct usb_descriptor_header *) &fs_source_desc,
669 (struct usb_descriptor_header *) &fs_sink_desc,
671 #endif /* CONFIG_USB_ETH_CDC */
674 static inline void fs_subset_descriptors(void)
676 #ifdef CONFIG_USB_ETH_SUBSET
677 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
678 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
679 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
680 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
681 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
682 fs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
683 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
684 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
685 fs_eth_function[8] = NULL;
687 fs_eth_function[1] = NULL;
691 #ifdef CONFIG_USB_ETH_RNDIS
692 static const struct usb_descriptor_header *fs_rndis_function[] = {
693 (struct usb_descriptor_header *) &otg_descriptor,
694 /* control interface matches ACM, not Ethernet */
695 (struct usb_descriptor_header *) &rndis_control_intf,
696 (struct usb_descriptor_header *) &header_desc,
697 (struct usb_descriptor_header *) &call_mgmt_descriptor,
698 (struct usb_descriptor_header *) &acm_descriptor,
699 (struct usb_descriptor_header *) &union_desc,
700 (struct usb_descriptor_header *) &fs_status_desc,
701 /* data interface has no altsetting */
702 (struct usb_descriptor_header *) &rndis_data_intf,
703 (struct usb_descriptor_header *) &fs_source_desc,
704 (struct usb_descriptor_header *) &fs_sink_desc,
710 * usb 2.0 devices need to expose both high speed and full speed
711 * descriptors, unless they only run at full speed.
714 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
715 static struct usb_endpoint_descriptor
717 .bLength = USB_DT_ENDPOINT_SIZE,
718 .bDescriptorType = USB_DT_ENDPOINT,
720 .bmAttributes = USB_ENDPOINT_XFER_INT,
721 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
722 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
724 #endif /* CONFIG_USB_ETH_CDC */
726 static struct usb_endpoint_descriptor
728 .bLength = USB_DT_ENDPOINT_SIZE,
729 .bDescriptorType = USB_DT_ENDPOINT,
731 .bmAttributes = USB_ENDPOINT_XFER_BULK,
732 .wMaxPacketSize = __constant_cpu_to_le16(512),
735 static struct usb_endpoint_descriptor
737 .bLength = USB_DT_ENDPOINT_SIZE,
738 .bDescriptorType = USB_DT_ENDPOINT,
740 .bmAttributes = USB_ENDPOINT_XFER_BULK,
741 .wMaxPacketSize = __constant_cpu_to_le16(512),
744 static struct usb_qualifier_descriptor
746 .bLength = sizeof dev_qualifier,
747 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
749 .bcdUSB = __constant_cpu_to_le16(0x0200),
750 .bDeviceClass = USB_CLASS_COMM,
752 .bNumConfigurations = 1,
755 static const struct usb_descriptor_header *hs_eth_function[11] = {
756 (struct usb_descriptor_header *) &otg_descriptor,
757 #ifdef CONFIG_USB_ETH_CDC
758 /* "cdc" mode descriptors */
759 (struct usb_descriptor_header *) &control_intf,
760 (struct usb_descriptor_header *) &header_desc,
761 (struct usb_descriptor_header *) &union_desc,
762 (struct usb_descriptor_header *) ðer_desc,
763 /* NOTE: status endpoint may need to be removed */
764 (struct usb_descriptor_header *) &hs_status_desc,
765 /* data interface, with altsetting */
766 (struct usb_descriptor_header *) &data_nop_intf,
767 (struct usb_descriptor_header *) &data_intf,
768 (struct usb_descriptor_header *) &hs_source_desc,
769 (struct usb_descriptor_header *) &hs_sink_desc,
771 #endif /* CONFIG_USB_ETH_CDC */
774 static inline void hs_subset_descriptors(void)
776 #ifdef CONFIG_USB_ETH_SUBSET
777 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
778 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
779 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
780 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
781 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
782 hs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
783 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
784 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
785 hs_eth_function[8] = NULL;
787 hs_eth_function[1] = NULL;
791 #ifdef CONFIG_USB_ETH_RNDIS
792 static const struct usb_descriptor_header *hs_rndis_function[] = {
793 (struct usb_descriptor_header *) &otg_descriptor,
794 /* control interface matches ACM, not Ethernet */
795 (struct usb_descriptor_header *) &rndis_control_intf,
796 (struct usb_descriptor_header *) &header_desc,
797 (struct usb_descriptor_header *) &call_mgmt_descriptor,
798 (struct usb_descriptor_header *) &acm_descriptor,
799 (struct usb_descriptor_header *) &union_desc,
800 (struct usb_descriptor_header *) &hs_status_desc,
801 /* data interface has no altsetting */
802 (struct usb_descriptor_header *) &rndis_data_intf,
803 (struct usb_descriptor_header *) &hs_source_desc,
804 (struct usb_descriptor_header *) &hs_sink_desc,
810 /* maxpacket and other transfer characteristics vary by speed. */
811 static inline struct usb_endpoint_descriptor *
812 ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
813 struct usb_endpoint_descriptor *fs)
815 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
820 /*-------------------------------------------------------------------------*/
822 /* descriptors that are built on-demand */
824 static char manufacturer[50];
825 static char product_desc[40] = DRIVER_DESC;
826 static char serial_number[20];
828 /* address that the host will use ... usually assigned at random */
829 static char ethaddr[2 * ETH_ALEN + 1];
831 /* static strings, in UTF-8 */
832 static struct usb_string strings[] = {
833 { STRING_MANUFACTURER, manufacturer, },
834 { STRING_PRODUCT, product_desc, },
835 { STRING_SERIALNUMBER, serial_number, },
836 { STRING_DATA, "Ethernet Data", },
837 { STRING_ETHADDR, ethaddr, },
838 #ifdef CONFIG_USB_ETH_CDC
839 { STRING_CDC, "CDC Ethernet", },
840 { STRING_CONTROL, "CDC Communications Control", },
842 #ifdef CONFIG_USB_ETH_SUBSET
843 { STRING_SUBSET, "CDC Ethernet Subset", },
845 #ifdef CONFIG_USB_ETH_RNDIS
846 { STRING_RNDIS, "RNDIS", },
847 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
849 { } /* end of list */
852 static struct usb_gadget_strings stringtab = {
853 .language = 0x0409, /* en-us */
857 /*============================================================================*/
858 DEFINE_CACHE_ALIGN_BUFFER(u8, control_req, USB_BUFSIZ);
860 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
861 DEFINE_CACHE_ALIGN_BUFFER(u8, status_req, STATUS_BYTECOUNT);
864 /*============================================================================*/
867 * one config, two interfaces: control, data.
868 * complications: class descriptors, and an altsetting.
871 config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
874 const struct usb_config_descriptor *config;
875 const struct usb_descriptor_header **function;
878 if (gadget_is_dualspeed(g)) {
879 hs = (g->speed == USB_SPEED_HIGH);
880 if (type == USB_DT_OTHER_SPEED_CONFIG)
883 #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
885 if (index >= device_desc.bNumConfigurations)
888 #ifdef CONFIG_USB_ETH_RNDIS
890 * list the RNDIS config first, to make Microsoft's drivers
891 * happy. DOCSIS 1.0 needs this too.
893 if (device_desc.bNumConfigurations == 2 && index == 0) {
894 config = &rndis_config;
895 function = which_fn(rndis);
899 config = ð_config;
900 function = which_fn(eth);
903 /* for now, don't advertise srp-only devices */
907 len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
910 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
914 /*-------------------------------------------------------------------------*/
916 static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
917 static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
920 set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
923 struct usb_gadget *gadget = dev->gadget;
925 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
926 /* status endpoint used for RNDIS and (optionally) CDC */
927 if (!subset_active(dev) && dev->status_ep) {
928 dev->status = ep_desc(gadget, &hs_status_desc,
930 dev->status_ep->driver_data = dev;
932 result = usb_ep_enable(dev->status_ep, dev->status);
934 debug("enable %s --> %d\n",
935 dev->status_ep->name, result);
941 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
942 dev->in_ep->driver_data = dev;
944 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
945 dev->out_ep->driver_data = dev;
948 * With CDC, the host isn't allowed to use these two data
949 * endpoints in the default altsetting for the interface.
950 * so we don't activate them yet. Reset from SET_INTERFACE.
952 * Strictly speaking RNDIS should work the same: activation is
953 * a side effect of setting a packet filter. Deactivation is
954 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
956 if (!cdc_active(dev)) {
957 result = usb_ep_enable(dev->in_ep, dev->in);
959 debug("enable %s --> %d\n",
960 dev->in_ep->name, result);
964 result = usb_ep_enable(dev->out_ep, dev->out);
966 debug("enable %s --> %d\n",
967 dev->out_ep->name, result);
974 result = alloc_requests(dev, qlen(gadget), gfp_flags);
976 /* on error, disable any endpoints */
978 if (!subset_active(dev) && dev->status_ep)
979 (void) usb_ep_disable(dev->status_ep);
981 (void) usb_ep_disable(dev->in_ep);
982 (void) usb_ep_disable(dev->out_ep);
985 } else if (!cdc_active(dev)) {
987 * activate non-CDC configs right away
988 * this isn't strictly according to the RNDIS spec
990 eth_start(dev, GFP_ATOMIC);
993 /* caller is responsible for cleanup on error */
997 static void eth_reset_config(struct eth_dev *dev)
999 if (dev->config == 0)
1002 debug("%s\n", __func__);
1004 rndis_uninit(dev->rndis_config);
1007 * disable endpoints, forcing (synchronous) completion of
1008 * pending i/o. then free the requests.
1012 usb_ep_disable(dev->in_ep);
1014 usb_ep_free_request(dev->in_ep, dev->tx_req);
1019 usb_ep_disable(dev->out_ep);
1021 usb_ep_free_request(dev->out_ep, dev->rx_req);
1026 usb_ep_disable(dev->status_ep);
1029 dev->cdc_filter = 0;
1034 * change our operational config. must agree with the code
1035 * that returns config descriptors, and altsetting code.
1037 static int eth_set_config(struct eth_dev *dev, unsigned number,
1041 struct usb_gadget *gadget = dev->gadget;
1043 if (gadget_is_sa1100(gadget)
1045 && dev->tx_qlen != 0) {
1046 /* tx fifo is full, but we can't clear it...*/
1047 error("can't change configurations");
1050 eth_reset_config(dev);
1053 case DEV_CONFIG_VALUE:
1054 result = set_ether_config(dev, gfp_flags);
1056 #ifdef CONFIG_USB_ETH_RNDIS
1057 case DEV_RNDIS_CONFIG_VALUE:
1059 result = set_ether_config(dev, gfp_flags);
1071 eth_reset_config(dev);
1072 usb_gadget_vbus_draw(dev->gadget,
1073 gadget_is_otg(dev->gadget) ? 8 : 100);
1078 power = 2 * eth_config.bMaxPower;
1079 usb_gadget_vbus_draw(dev->gadget, power);
1081 switch (gadget->speed) {
1082 case USB_SPEED_FULL:
1083 speed = "full"; break;
1084 #ifdef CONFIG_USB_GADGET_DUALSPEED
1085 case USB_SPEED_HIGH:
1086 speed = "high"; break;
1092 dev->config = number;
1093 printf("%s speed config #%d: %d mA, %s, using %s\n",
1094 speed, number, power, driver_desc,
1099 : "CDC Ethernet Subset"));
1104 /*-------------------------------------------------------------------------*/
1106 #ifdef CONFIG_USB_ETH_CDC
1109 * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
1110 * only to notify the host about link status changes (which we support) or
1111 * report completion of some encapsulated command (as used in RNDIS). Since
1112 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1113 * command mechanism; and only one status request is ever queued.
1115 static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
1117 struct usb_cdc_notification *event = req->buf;
1118 int value = req->status;
1119 struct eth_dev *dev = ep->driver_data;
1121 /* issue the second notification if host reads the first */
1122 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1124 __le32 *data = req->buf + sizeof *event;
1126 event->bmRequestType = 0xA1;
1127 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
1128 event->wValue = __constant_cpu_to_le16(0);
1129 event->wIndex = __constant_cpu_to_le16(1);
1130 event->wLength = __constant_cpu_to_le16(8);
1132 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1133 data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
1135 req->length = STATUS_BYTECOUNT;
1136 value = usb_ep_queue(ep, req, GFP_ATOMIC);
1137 debug("send SPEED_CHANGE --> %d\n", value);
1140 } else if (value != -ECONNRESET) {
1141 debug("event %02x --> %d\n",
1142 event->bNotificationType, value);
1143 if (event->bNotificationType ==
1144 USB_CDC_NOTIFY_SPEED_CHANGE) {
1145 dev->network_started = 1;
1146 printf("USB network up!\n");
1149 req->context = NULL;
1152 static void issue_start_status(struct eth_dev *dev)
1154 struct usb_request *req = dev->stat_req;
1155 struct usb_cdc_notification *event;
1161 * FIXME ugly idiom, maybe we'd be better with just
1162 * a "cancel the whole queue" primitive since any
1163 * unlink-one primitive has way too many error modes.
1164 * here, we "know" toggle is already clear...
1166 * FIXME iff req->context != null just dequeue it
1168 usb_ep_disable(dev->status_ep);
1169 usb_ep_enable(dev->status_ep, dev->status);
1172 * 3.8.1 says to issue first NETWORK_CONNECTION, then
1173 * a SPEED_CHANGE. could be useful in some configs.
1176 event->bmRequestType = 0xA1;
1177 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
1178 event->wValue = __constant_cpu_to_le16(1); /* connected */
1179 event->wIndex = __constant_cpu_to_le16(1);
1182 req->length = sizeof *event;
1183 req->complete = eth_status_complete;
1186 value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
1188 debug("status buf queue --> %d\n", value);
1193 /*-------------------------------------------------------------------------*/
1195 static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
1197 if (req->status || req->actual != req->length)
1198 debug("setup complete --> %d, %d/%d\n",
1199 req->status, req->actual, req->length);
1202 #ifdef CONFIG_USB_ETH_RNDIS
1204 static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
1206 if (req->status || req->actual != req->length)
1207 debug("rndis response complete --> %d, %d/%d\n",
1208 req->status, req->actual, req->length);
1210 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1213 static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
1215 struct eth_dev *dev = ep->driver_data;
1218 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1219 status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
1221 error("%s: rndis parse error %d", __func__, status);
1227 * The setup() callback implements all the ep0 functionality that's not
1228 * handled lower down. CDC has a number of less-common features:
1230 * - two interfaces: control, and ethernet data
1231 * - Ethernet data interface has two altsettings: default, and active
1232 * - class-specific descriptors for the control interface
1233 * - class-specific control requests
1236 eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1238 struct eth_dev *dev = get_gadget_data(gadget);
1239 struct usb_request *req = dev->req;
1240 int value = -EOPNOTSUPP;
1241 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1242 u16 wValue = le16_to_cpu(ctrl->wValue);
1243 u16 wLength = le16_to_cpu(ctrl->wLength);
1246 * descriptors just go into the pre-allocated ep0 buffer,
1247 * while config change events may enable network traffic.
1250 debug("%s\n", __func__);
1252 req->complete = eth_setup_complete;
1253 switch (ctrl->bRequest) {
1255 case USB_REQ_GET_DESCRIPTOR:
1256 if (ctrl->bRequestType != USB_DIR_IN)
1258 switch (wValue >> 8) {
1261 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1262 value = min(wLength, (u16) sizeof device_desc);
1263 memcpy(req->buf, &device_desc, value);
1265 case USB_DT_DEVICE_QUALIFIER:
1266 if (!gadget_is_dualspeed(gadget))
1268 value = min(wLength, (u16) sizeof dev_qualifier);
1269 memcpy(req->buf, &dev_qualifier, value);
1272 case USB_DT_OTHER_SPEED_CONFIG:
1273 if (!gadget_is_dualspeed(gadget))
1277 value = config_buf(gadget, req->buf,
1280 gadget_is_otg(gadget));
1282 value = min(wLength, (u16) value);
1286 value = usb_gadget_get_string(&stringtab,
1287 wValue & 0xff, req->buf);
1290 value = min(wLength, (u16) value);
1296 case USB_REQ_SET_CONFIGURATION:
1297 if (ctrl->bRequestType != 0)
1299 if (gadget->a_hnp_support)
1300 debug("HNP available\n");
1301 else if (gadget->a_alt_hnp_support)
1302 debug("HNP needs a different root port\n");
1303 value = eth_set_config(dev, wValue, GFP_ATOMIC);
1305 case USB_REQ_GET_CONFIGURATION:
1306 if (ctrl->bRequestType != USB_DIR_IN)
1308 *(u8 *)req->buf = dev->config;
1309 value = min(wLength, (u16) 1);
1312 case USB_REQ_SET_INTERFACE:
1313 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1317 if (!cdc_active(dev) && wIndex != 0)
1321 * PXA hardware partially handles SET_INTERFACE;
1322 * we need to kluge around that interference.
1324 if (gadget_is_pxa(gadget)) {
1325 value = eth_set_config(dev, DEV_CONFIG_VALUE,
1328 * PXA25x driver use non-CDC ethernet gadget.
1329 * But only _CDC and _RNDIS code can signalize
1330 * that network is working. So we signalize it
1333 dev->network_started = 1;
1334 debug("USB network up!\n");
1338 #ifdef CONFIG_USB_ETH_CDC
1340 case 0: /* control/master intf */
1344 usb_ep_disable(dev->status_ep);
1345 usb_ep_enable(dev->status_ep, dev->status);
1350 case 1: /* data intf */
1353 usb_ep_disable(dev->in_ep);
1354 usb_ep_disable(dev->out_ep);
1357 * CDC requires the data transfers not be done from
1358 * the default interface setting ... also, setting
1359 * the non-default interface resets filters etc.
1362 if (!cdc_active(dev))
1364 usb_ep_enable(dev->in_ep, dev->in);
1365 usb_ep_enable(dev->out_ep, dev->out);
1366 dev->cdc_filter = DEFAULT_FILTER;
1368 issue_start_status(dev);
1369 eth_start(dev, GFP_ATOMIC);
1376 * FIXME this is wrong, as is the assumption that
1377 * all non-PXA hardware talks real CDC ...
1379 debug("set_interface ignored!\n");
1380 #endif /* CONFIG_USB_ETH_CDC */
1384 case USB_REQ_GET_INTERFACE:
1385 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1389 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
1392 /* for CDC, iff carrier is on, data interface is active. */
1393 if (rndis_active(dev) || wIndex != 1)
1394 *(u8 *)req->buf = 0;
1396 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1397 /* carrier always ok ...*/
1398 *(u8 *)req->buf = 1 ;
1400 value = min(wLength, (u16) 1);
1403 #ifdef CONFIG_USB_ETH_CDC
1404 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
1406 * see 6.2.30: no data, wIndex = interface,
1407 * wValue = packet filter bitmap
1409 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1414 debug("packet filter %02x\n", wValue);
1415 dev->cdc_filter = wValue;
1421 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1422 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1423 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1424 * case USB_CDC_GET_ETHERNET_STATISTIC:
1427 #endif /* CONFIG_USB_ETH_CDC */
1429 #ifdef CONFIG_USB_ETH_RNDIS
1431 * RNDIS uses the CDC command encapsulation mechanism to implement
1432 * an RPC scheme, with much getting/setting of attributes by OID.
1434 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1435 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1436 || !rndis_active(dev)
1437 || wLength > USB_BUFSIZ
1439 || rndis_control_intf.bInterfaceNumber
1442 /* read the request, then process it */
1444 req->complete = rndis_command_complete;
1445 /* later, rndis_control_ack () sends a notification */
1448 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1449 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1450 == ctrl->bRequestType
1451 && rndis_active(dev)
1452 /* && wLength >= 0x0400 */
1454 && rndis_control_intf.bInterfaceNumber
1459 /* return the result */
1460 buf = rndis_get_next_response(dev->rndis_config, &n);
1462 memcpy(req->buf, buf, n);
1463 req->complete = rndis_response_complete;
1464 rndis_free_response(dev->rndis_config, buf);
1467 /* else stalls ... spec says to avoid that */
1473 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
1474 ctrl->bRequestType, ctrl->bRequest,
1475 wValue, wIndex, wLength);
1478 /* respond with data transfer before status phase? */
1480 debug("respond with data transfer before status phase\n");
1481 req->length = value;
1482 req->zero = value < wLength
1483 && (value % gadget->ep0->maxpacket) == 0;
1484 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1486 debug("ep_queue --> %d\n", value);
1488 eth_setup_complete(gadget->ep0, req);
1492 /* host either stalls (value < 0) or reports success */
1496 /*-------------------------------------------------------------------------*/
1498 static void rx_complete(struct usb_ep *ep, struct usb_request *req);
1500 static int rx_submit(struct eth_dev *dev, struct usb_request *req,
1503 int retval = -ENOMEM;
1507 * Padding up to RX_EXTRA handles minor disagreements with host.
1508 * Normally we use the USB "terminate on short read" convention;
1509 * so allow up to (N*maxpacket), since that memory is normally
1510 * already allocated. Some hardware doesn't deal well with short
1511 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1512 * byte off the end (to force hardware errors on overflow).
1514 * RNDIS uses internal framing, and explicitly allows senders to
1515 * pad to end-of-packet. That's potentially nice for speed,
1516 * but means receivers can't recover synch on their own.
1519 debug("%s\n", __func__);
1523 size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1524 size += dev->out_ep->maxpacket - 1;
1525 if (rndis_active(dev))
1526 size += sizeof(struct rndis_packet_msg_type);
1527 size -= size % dev->out_ep->maxpacket;
1530 * Some platforms perform better when IP packets are aligned,
1531 * but on at least one, checksumming fails otherwise. Note:
1532 * RNDIS headers involve variable numbers of LE32 values.
1535 req->buf = (u8 *)net_rx_packets[0];
1537 req->complete = rx_complete;
1539 retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
1542 error("rx submit --> %d", retval);
1547 static void rx_complete(struct usb_ep *ep, struct usb_request *req)
1549 struct eth_dev *dev = ep->driver_data;
1551 debug("%s: status %d\n", __func__, req->status);
1552 switch (req->status) {
1553 /* normal completion */
1555 if (rndis_active(dev)) {
1556 /* we know MaxPacketsPerTransfer == 1 here */
1557 int length = rndis_rm_hdr(req->buf, req->actual);
1560 req->length -= length;
1561 req->actual -= length;
1563 if (req->actual < ETH_HLEN || ETH_FRAME_LEN < req->actual) {
1565 dev->stats.rx_errors++;
1566 dev->stats.rx_length_errors++;
1567 debug("rx length %d\n", req->length);
1571 dev->stats.rx_packets++;
1572 dev->stats.rx_bytes += req->length;
1575 /* software-driven interface shutdown */
1576 case -ECONNRESET: /* unlink */
1577 case -ESHUTDOWN: /* disconnect etc */
1578 /* for hardware automagic (such as pxa) */
1579 case -ECONNABORTED: /* endpoint reset */
1584 dev->stats.rx_over_errors++;
1587 dev->stats.rx_errors++;
1591 packet_received = 1;
1594 static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
1597 dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
1602 dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
1610 usb_ep_free_request(dev->in_ep, dev->tx_req);
1612 error("can't alloc requests");
1616 static void tx_complete(struct usb_ep *ep, struct usb_request *req)
1618 struct eth_dev *dev = ep->driver_data;
1620 debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
1621 switch (req->status) {
1623 dev->stats.tx_errors++;
1624 debug("tx err %d\n", req->status);
1626 case -ECONNRESET: /* unlink */
1627 case -ESHUTDOWN: /* disconnect etc */
1630 dev->stats.tx_bytes += req->length;
1632 dev->stats.tx_packets++;
1637 static inline int eth_is_promisc(struct eth_dev *dev)
1639 /* no filters for the CDC subset; always promisc */
1640 if (subset_active(dev))
1642 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1646 static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1648 struct eth_dev *dev = netdev_priv(net);
1649 int length = skb->len;
1651 struct usb_request *req = NULL;
1652 unsigned long flags;
1654 /* apply outgoing CDC or RNDIS filters */
1655 if (!eth_is_promisc (dev)) {
1656 u8 *dest = skb->data;
1658 if (is_multicast_ethaddr(dest)) {
1661 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1662 * SET_ETHERNET_MULTICAST_FILTERS requests
1664 if (is_broadcast_ethaddr(dest))
1665 type = USB_CDC_PACKET_TYPE_BROADCAST;
1667 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1668 if (!(dev->cdc_filter & type)) {
1669 dev_kfree_skb_any (skb);
1673 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1676 spin_lock_irqsave(&dev->req_lock, flags);
1678 * this freelist can be empty if an interrupt triggered disconnect()
1679 * and reconfigured the gadget (shutting down this queue) after the
1680 * network stack decided to xmit but before we got the spinlock.
1682 if (list_empty(&dev->tx_reqs)) {
1683 spin_unlock_irqrestore(&dev->req_lock, flags);
1687 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1688 list_del (&req->list);
1690 /* temporarily stop TX queue when the freelist empties */
1691 if (list_empty (&dev->tx_reqs))
1692 netif_stop_queue (net);
1693 spin_unlock_irqrestore(&dev->req_lock, flags);
1695 /* no buffer copies needed, unless the network stack did it
1696 * or the hardware can't use skb buffers.
1697 * or there's not enough space for any RNDIS headers we need
1699 if (rndis_active(dev)) {
1700 struct sk_buff *skb_rndis;
1702 skb_rndis = skb_realloc_headroom (skb,
1703 sizeof (struct rndis_packet_msg_type));
1707 dev_kfree_skb_any (skb);
1709 rndis_add_hdr (skb);
1712 req->buf = skb->data;
1714 req->complete = tx_complete;
1716 /* use zlp framing on tx for strict CDC-Ether conformance,
1717 * though any robust network rx path ignores extra padding.
1718 * and some hardware doesn't like to write zlps.
1721 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1724 req->length = length;
1726 /* throttle highspeed IRQ rate back slightly */
1727 if (gadget_is_dualspeed(dev->gadget))
1728 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1729 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1732 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1735 DEBUG (dev, "tx queue err %d\n", retval);
1738 net->trans_start = jiffies;
1739 atomic_inc (&dev->tx_qlen);
1744 dev->stats.tx_dropped++;
1745 dev_kfree_skb_any (skb);
1746 spin_lock_irqsave(&dev->req_lock, flags);
1747 if (list_empty (&dev->tx_reqs))
1748 netif_start_queue (net);
1749 list_add (&req->list, &dev->tx_reqs);
1750 spin_unlock_irqrestore(&dev->req_lock, flags);
1755 /*-------------------------------------------------------------------------*/
1758 static void eth_unbind(struct usb_gadget *gadget)
1760 struct eth_dev *dev = get_gadget_data(gadget);
1762 debug("%s...\n", __func__);
1763 rndis_deregister(dev->rndis_config);
1766 /* we've already been disconnected ... no i/o is active */
1768 usb_ep_free_request(gadget->ep0, dev->req);
1771 if (dev->stat_req) {
1772 usb_ep_free_request(dev->status_ep, dev->stat_req);
1773 dev->stat_req = NULL;
1777 usb_ep_free_request(dev->in_ep, dev->tx_req);
1782 usb_ep_free_request(dev->out_ep, dev->rx_req);
1786 /* unregister_netdev (dev->net);*/
1787 /* free_netdev(dev->net);*/
1790 set_gadget_data(gadget, NULL);
1793 static void eth_disconnect(struct usb_gadget *gadget)
1795 eth_reset_config(get_gadget_data(gadget));
1796 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1799 static void eth_suspend(struct usb_gadget *gadget)
1804 static void eth_resume(struct usb_gadget *gadget)
1809 /*-------------------------------------------------------------------------*/
1811 #ifdef CONFIG_USB_ETH_RNDIS
1814 * The interrupt endpoint is used in RNDIS to notify the host when messages
1815 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1816 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1817 * REMOTE_NDIS_KEEPALIVE_MSG.
1819 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1820 * normally just one notification will be queued.
1823 static void rndis_control_ack_complete(struct usb_ep *ep,
1824 struct usb_request *req)
1826 struct eth_dev *dev = ep->driver_data;
1828 debug("%s...\n", __func__);
1829 if (req->status || req->actual != req->length)
1830 debug("rndis control ack complete --> %d, %d/%d\n",
1831 req->status, req->actual, req->length);
1833 if (!dev->network_started) {
1834 if (rndis_get_state(dev->rndis_config)
1835 == RNDIS_DATA_INITIALIZED) {
1836 dev->network_started = 1;
1837 printf("USB RNDIS network up!\n");
1841 req->context = NULL;
1843 if (req != dev->stat_req)
1844 usb_ep_free_request(ep, req);
1847 static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
1849 static int rndis_control_ack(struct eth_device *net)
1851 struct eth_dev *dev = &l_ethdev;
1853 struct usb_request *resp = dev->stat_req;
1855 /* in case RNDIS calls this after disconnect */
1857 debug("status ENODEV\n");
1861 /* in case queue length > 1 */
1862 if (resp->context) {
1863 resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
1866 resp->buf = rndis_resp_buf;
1870 * Send RNDIS RESPONSE_AVAILABLE notification;
1871 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1874 resp->complete = rndis_control_ack_complete;
1875 resp->context = dev;
1877 *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
1878 *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
1880 length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
1883 rndis_control_ack_complete(dev->status_ep, resp);
1891 #define rndis_control_ack NULL
1895 static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
1897 if (rndis_active(dev)) {
1898 rndis_set_param_medium(dev->rndis_config,
1900 BITRATE(dev->gadget)/100);
1901 rndis_signal_connect(dev->rndis_config);
1905 static int eth_stop(struct eth_dev *dev)
1907 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1909 unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1912 if (rndis_active(dev)) {
1913 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
1914 rndis_signal_disconnect(dev->rndis_config);
1916 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1917 /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1919 while (get_timer(ts) < timeout)
1920 usb_gadget_handle_interrupts(0);
1923 rndis_uninit(dev->rndis_config);
1930 /*-------------------------------------------------------------------------*/
1932 static int is_eth_addr_valid(char *str)
1934 if (strlen(str) == 17) {
1939 /* see if it looks like an ethernet address */
1943 for (i = 0; i < 6; i++) {
1944 char term = (i == 5 ? '\0' : ':');
1946 ea[i] = simple_strtol(p, &q, 16);
1948 if ((q - p) != 2 || *q++ != term)
1954 /* Now check the contents. */
1955 return is_valid_ethaddr(ea);
1960 static u8 nibble(unsigned char c)
1962 if (likely(isdigit(c)))
1965 if (likely(isxdigit(c)))
1966 return 10 + c - 'A';
1970 static int get_ether_addr(const char *str, u8 *dev_addr)
1975 for (i = 0; i < 6; i++) {
1978 if ((*str == '.') || (*str == ':'))
1980 num = nibble(*str++) << 4;
1981 num |= (nibble(*str++));
1984 if (is_valid_ethaddr(dev_addr))
1990 static int eth_bind(struct usb_gadget *gadget)
1992 struct eth_dev *dev = &l_ethdev;
1993 u8 cdc = 1, zlp = 1, rndis = 1;
1994 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
1995 int status = -ENOMEM;
1999 /* these flags are only ever cleared; compiler take note */
2000 #ifndef CONFIG_USB_ETH_CDC
2003 #ifndef CONFIG_USB_ETH_RNDIS
2007 * Because most host side USB stacks handle CDC Ethernet, that
2008 * standard protocol is _strongly_ preferred for interop purposes.
2009 * (By everyone except Microsoft.)
2011 if (gadget_is_pxa(gadget)) {
2012 /* pxa doesn't support altsettings */
2014 } else if (gadget_is_musbhdrc(gadget)) {
2015 /* reduce tx dma overhead by avoiding special cases */
2017 } else if (gadget_is_sh(gadget)) {
2018 /* sh doesn't support multiple interfaces or configs */
2021 } else if (gadget_is_sa1100(gadget)) {
2022 /* hardware can't write zlps */
2025 * sa1100 CAN do CDC, without status endpoint ... we use
2026 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2031 gcnum = usb_gadget_controller_number(gadget);
2033 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
2036 * can't assume CDC works. don't want to default to
2037 * anything less functional on CDC-capable hardware,
2038 * so we fail in this case.
2040 error("controller '%s' not recognized",
2046 * If there's an RNDIS configuration, that's what Windows wants to
2047 * be using ... so use these product IDs here and in the "linux.inf"
2048 * needed to install MSFT drivers. Current Linux kernels will use
2049 * the second configuration if it's CDC Ethernet, and need some help
2050 * to choose the right configuration otherwise.
2053 #if defined(CONFIG_USB_RNDIS_VENDOR_ID) && defined(CONFIG_USB_RNDIS_PRODUCT_ID)
2054 device_desc.idVendor =
2055 __constant_cpu_to_le16(CONFIG_USB_RNDIS_VENDOR_ID);
2056 device_desc.idProduct =
2057 __constant_cpu_to_le16(CONFIG_USB_RNDIS_PRODUCT_ID);
2059 device_desc.idVendor =
2060 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2061 device_desc.idProduct =
2062 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2064 sprintf(product_desc, "RNDIS/%s", driver_desc);
2067 * CDC subset ... recognized by Linux since 2.4.10, but Windows
2068 * drivers aren't widely available. (That may be improved by
2069 * supporting one submode of the "SAFE" variant of MDLM.)
2072 #if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID)
2073 device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID);
2074 device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID);
2077 device_desc.idVendor =
2078 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2079 device_desc.idProduct =
2080 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2084 /* support optional vendor/distro customization */
2086 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2088 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
2090 strlcpy(product_desc, iProduct, sizeof product_desc);
2091 if (iSerialNumber) {
2092 device_desc.iSerialNumber = STRING_SERIALNUMBER,
2093 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
2096 /* all we really need is bulk IN/OUT */
2097 usb_ep_autoconfig_reset(gadget);
2098 in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
2101 error("can't autoconfigure on %s\n",
2105 in_ep->driver_data = in_ep; /* claim */
2107 out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
2110 out_ep->driver_data = out_ep; /* claim */
2112 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2114 * CDC Ethernet control interface doesn't require a status endpoint.
2115 * Since some hosts expect one, try to allocate one anyway.
2118 status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
2120 status_ep->driver_data = status_ep; /* claim */
2122 error("can't run RNDIS on %s", gadget->name);
2124 #ifdef CONFIG_USB_ETH_CDC
2126 control_intf.bNumEndpoints = 0;
2127 /* FIXME remove endpoint from descriptor list */
2133 /* one config: cdc, else minimal subset */
2135 eth_config.bNumInterfaces = 1;
2136 eth_config.iConfiguration = STRING_SUBSET;
2139 * use functions to set these up, in case we're built to work
2140 * with multiple controllers and must override CDC Ethernet.
2142 fs_subset_descriptors();
2143 hs_subset_descriptors();
2146 usb_gadget_set_selfpowered(gadget);
2148 /* For now RNDIS is always a second config */
2150 device_desc.bNumConfigurations = 2;
2152 if (gadget_is_dualspeed(gadget)) {
2154 dev_qualifier.bNumConfigurations = 2;
2156 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2158 /* assumes ep0 uses the same value for both speeds ... */
2159 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2161 /* and that all endpoints are dual-speed */
2162 hs_source_desc.bEndpointAddress =
2163 fs_source_desc.bEndpointAddress;
2164 hs_sink_desc.bEndpointAddress =
2165 fs_sink_desc.bEndpointAddress;
2166 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2168 hs_status_desc.bEndpointAddress =
2169 fs_status_desc.bEndpointAddress;
2173 if (gadget_is_otg(gadget)) {
2174 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2175 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2176 eth_config.bMaxPower = 4;
2177 #ifdef CONFIG_USB_ETH_RNDIS
2178 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2179 rndis_config.bMaxPower = 4;
2184 /* network device setup */
2185 dev->net = &l_netdev;
2191 dev->out_ep = out_ep;
2192 dev->status_ep = status_ep;
2195 * Module params for these addresses should come from ID proms.
2196 * The host side address is used with CDC and RNDIS, and commonly
2197 * ends up in a persistent config database. It's not clear if
2198 * host side code for the SAFE thing cares -- its original BLAN
2199 * thing didn't, Sharp never assigned those addresses on Zaurii.
2201 get_ether_addr(dev_addr, dev->net->enetaddr);
2203 memset(tmp, 0, sizeof(tmp));
2204 memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
2206 get_ether_addr(host_addr, dev->host_mac);
2208 sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
2209 dev->host_mac[0], dev->host_mac[1],
2210 dev->host_mac[2], dev->host_mac[3],
2211 dev->host_mac[4], dev->host_mac[5]);
2214 status = rndis_init();
2216 error("can't init RNDIS, %d", status);
2222 * use PKTSIZE (or aligned... from u-boot) and set
2223 * wMaxSegmentSize accordingly
2225 dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
2227 /* preallocate control message data and buffer */
2228 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
2231 dev->req->buf = control_req;
2232 dev->req->complete = eth_setup_complete;
2234 /* ... and maybe likewise for status transfer */
2235 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2236 if (dev->status_ep) {
2237 dev->stat_req = usb_ep_alloc_request(dev->status_ep,
2239 if (!dev->stat_req) {
2240 usb_ep_free_request(dev->status_ep, dev->req);
2244 dev->stat_req->buf = status_req;
2245 dev->stat_req->context = NULL;
2249 /* finish hookup to lower layer ... */
2250 dev->gadget = gadget;
2251 set_gadget_data(gadget, dev);
2252 gadget->ep0->driver_data = dev;
2255 * two kinds of host-initiated state changes:
2256 * - iff DATA transfer is active, carrier is "on"
2257 * - tx queueing enabled if open *and* carrier is "on"
2260 printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
2261 out_ep->name, in_ep->name,
2262 status_ep ? " STATUS " : "",
2263 status_ep ? status_ep->name : ""
2265 printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2266 dev->net->enetaddr[0], dev->net->enetaddr[1],
2267 dev->net->enetaddr[2], dev->net->enetaddr[3],
2268 dev->net->enetaddr[4], dev->net->enetaddr[5]);
2271 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2272 dev->host_mac[0], dev->host_mac[1],
2273 dev->host_mac[2], dev->host_mac[3],
2274 dev->host_mac[4], dev->host_mac[5]);
2279 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2281 dev->rndis_config = rndis_register(rndis_control_ack);
2282 if (dev->rndis_config < 0) {
2285 debug("RNDIS setup failed\n");
2290 /* these set up a lot of the OIDs that RNDIS needs */
2291 rndis_set_host_mac(dev->rndis_config, dev->host_mac);
2292 if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
2293 &dev->stats, &dev->cdc_filter))
2295 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2298 if (rndis_set_param_medium(dev->rndis_config,
2299 NDIS_MEDIUM_802_3, 0))
2301 printf("RNDIS ready\n");
2306 error("%s failed, status = %d", __func__, status);
2311 /*-------------------------------------------------------------------------*/
2313 #ifdef CONFIG_DM_USB
2314 int dm_usb_init(struct eth_dev *e_dev)
2316 struct udevice *dev = NULL;
2319 ret = uclass_first_device(UCLASS_USB_DEV_GENERIC, &dev);
2321 error("No USB device found\n");
2325 e_dev->usb_udev = dev;
2331 static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
2333 struct eth_dev *dev = &l_ethdev;
2334 struct usb_gadget *gadget;
2336 unsigned long timeout = USB_CONNECT_TIMEOUT;
2339 error("received NULL ptr");
2343 #ifdef CONFIG_DM_USB
2344 if (dm_usb_init(dev)) {
2345 error("USB ether not found\n");
2349 board_usb_init(0, USB_INIT_DEVICE);
2352 /* Configure default mac-addresses for the USB ethernet device */
2353 #ifdef CONFIG_USBNET_DEV_ADDR
2354 strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
2356 #ifdef CONFIG_USBNET_HOST_ADDR
2357 strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
2359 /* Check if the user overruled the MAC addresses */
2360 if (getenv("usbnet_devaddr"))
2361 strlcpy(dev_addr, getenv("usbnet_devaddr"),
2364 if (getenv("usbnet_hostaddr"))
2365 strlcpy(host_addr, getenv("usbnet_hostaddr"),
2368 if (!is_eth_addr_valid(dev_addr)) {
2369 error("Need valid 'usbnet_devaddr' to be set");
2372 if (!is_eth_addr_valid(host_addr)) {
2373 error("Need valid 'usbnet_hostaddr' to be set");
2377 if (usb_gadget_register_driver(ð_driver) < 0)
2380 dev->network_started = 0;
2382 packet_received = 0;
2385 gadget = dev->gadget;
2386 usb_gadget_connect(gadget);
2388 if (getenv("cdc_connect_timeout"))
2389 timeout = simple_strtoul(getenv("cdc_connect_timeout"),
2390 NULL, 10) * CONFIG_SYS_HZ;
2392 while (!dev->network_started) {
2393 /* Handle control-c and timeouts */
2394 if (ctrlc() || (get_timer(ts) > timeout)) {
2395 error("The remote end did not respond in time.");
2398 usb_gadget_handle_interrupts(0);
2401 packet_received = 0;
2402 rx_submit(dev, dev->rx_req, 0);
2408 static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
2411 void *rndis_pkt = NULL;
2412 struct eth_dev *dev = &l_ethdev;
2413 struct usb_request *req = dev->tx_req;
2415 unsigned long timeout = USB_CONNECT_TIMEOUT;
2417 debug("%s:...\n", __func__);
2419 /* new buffer is needed to include RNDIS header */
2420 if (rndis_active(dev)) {
2421 rndis_pkt = malloc(length +
2422 sizeof(struct rndis_packet_msg_type));
2424 error("No memory to alloc RNDIS packet");
2427 rndis_add_hdr(rndis_pkt, length);
2428 memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
2431 length += sizeof(struct rndis_packet_msg_type);
2434 req->context = NULL;
2435 req->complete = tx_complete;
2438 * use zlp framing on tx for strict CDC-Ether conformance,
2439 * though any robust network rx path ignores extra padding.
2440 * and some hardware doesn't like to write zlps.
2443 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2446 req->length = length;
2448 /* throttle highspeed IRQ rate back slightly */
2449 if (gadget_is_dualspeed(dev->gadget))
2450 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2451 ? ((dev->tx_qlen % qmult) != 0) : 0;
2457 retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
2460 debug("%s: packet queued\n", __func__);
2461 while (!packet_sent) {
2462 if (get_timer(ts) > timeout) {
2463 printf("timeout sending packets to usb ethernet\n");
2466 usb_gadget_handle_interrupts(0);
2473 dev->stats.tx_dropped++;
2477 static int usb_eth_recv(struct eth_device *netdev)
2479 struct eth_dev *dev = &l_ethdev;
2481 usb_gadget_handle_interrupts(0);
2483 if (packet_received) {
2484 debug("%s: packet received\n", __func__);
2486 net_process_received_packet(net_rx_packets[0],
2487 dev->rx_req->length);
2488 packet_received = 0;
2490 rx_submit(dev, dev->rx_req, 0);
2492 error("dev->rx_req invalid");
2497 void usb_eth_halt(struct eth_device *netdev)
2499 struct eth_dev *dev = &l_ethdev;
2502 error("received NULL ptr");
2506 /* If the gadget not registered, simple return */
2511 * Some USB controllers may need additional deinitialization here
2512 * before dropping pull-up (also due to hardware issues).
2513 * For example: unhandled interrupt with status stage started may
2514 * bring the controller to fully broken state (until board reset).
2515 * There are some variants to debug and fix such cases:
2516 * 1) In the case of RNDIS connection eth_stop can perform additional
2517 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2518 * 2) 'pullup' callback in your UDC driver can be improved to perform
2519 * this deinitialization.
2523 usb_gadget_disconnect(dev->gadget);
2525 /* Clear pending interrupt */
2526 if (dev->network_started) {
2527 usb_gadget_handle_interrupts(0);
2528 dev->network_started = 0;
2531 usb_gadget_unregister_driver(ð_driver);
2532 #ifdef CONFIG_DM_USB
2533 device_remove(dev->usb_udev);
2535 board_usb_cleanup(0, USB_INIT_DEVICE);
2539 static struct usb_gadget_driver eth_driver = {
2543 .unbind = eth_unbind,
2546 .reset = eth_disconnect,
2547 .disconnect = eth_disconnect,
2549 .suspend = eth_suspend,
2550 .resume = eth_resume,
2553 int usb_eth_initialize(bd_t *bi)
2555 struct eth_device *netdev = &l_netdev;
2557 strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
2559 netdev->init = usb_eth_init;
2560 netdev->send = usb_eth_send;
2561 netdev->recv = usb_eth_recv;
2562 netdev->halt = usb_eth_halt;
2564 #ifdef CONFIG_MCAST_TFTP
2565 #error not supported
2567 eth_register(netdev);