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"
27 #define USB_NET_NAME "usb_ether"
30 extern struct platform_data brd;
33 unsigned packet_received, packet_sent;
36 * Ethernet gadget driver -- with CDC and non-CDC options
37 * Builds on hardware support for a full duplex link.
39 * CDC Ethernet is the standard USB solution for sending Ethernet frames
40 * using USB. Real hardware tends to use the same framing protocol but look
41 * different for control features. This driver strongly prefers to use
42 * this USB-IF standard as its open-systems interoperability solution;
43 * most host side USB stacks (except from Microsoft) support it.
45 * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
46 * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
47 * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
49 * There's some hardware that can't talk CDC ECM. We make that hardware
50 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
51 * link-level setup only requires activating the configuration. Only the
52 * endpoint descriptors, and product/vendor IDs, are relevant; no control
53 * operations are available. Linux supports it, but other host operating
54 * systems may not. (This is a subset of CDC Ethernet.)
56 * It turns out that if you add a few descriptors to that "CDC Subset",
57 * (Windows) host side drivers from MCCI can treat it as one submode of
58 * a proprietary scheme called "SAFE" ... without needing to know about
59 * specific product/vendor IDs. So we do that, making it easier to use
60 * those MS-Windows drivers. Those added descriptors make it resemble a
61 * CDC MDLM device, but they don't change device behavior at all. (See
62 * MCCI Engineering report 950198 "SAFE Networking Functions".)
64 * A third option is also in use. Rather than CDC Ethernet, or something
65 * simpler, Microsoft pushes their own approach: RNDIS. The published
66 * RNDIS specs are ambiguous and appear to be incomplete, and are also
67 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
69 #define ETH_ALEN 6 /* Octets in one ethernet addr */
70 #define ETH_HLEN 14 /* Total octets in header. */
71 #define ETH_ZLEN 60 /* Min. octets in frame sans FCS */
72 #define ETH_DATA_LEN 1500 /* Max. octets in payload */
73 #define ETH_FRAME_LEN PKTSIZE_ALIGN /* Max. octets in frame sans FCS */
75 #define DRIVER_DESC "Ethernet Gadget"
76 /* Based on linux 2.6.27 version */
77 #define DRIVER_VERSION "May Day 2005"
79 static const char shortname[] = "ether";
80 static const char driver_desc[] = DRIVER_DESC;
82 #define RX_EXTRA 20 /* guard against rx overflows */
84 #ifndef CONFIG_USB_ETH_RNDIS
85 #define rndis_uninit(x) do {} while (0)
86 #define rndis_deregister(c) do {} while (0)
87 #define rndis_exit() do {} while (0)
90 /* CDC and RNDIS support the same host-chosen outgoing packet filters. */
91 #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
92 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
93 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
94 |USB_CDC_PACKET_TYPE_DIRECTED)
96 #define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
98 /*-------------------------------------------------------------------------*/
101 struct usb_gadget *gadget;
102 struct usb_request *req; /* for control responses */
103 struct usb_request *stat_req; /* for cdc & rndis status */
106 struct usb_ep *in_ep, *out_ep, *status_ep;
107 const struct usb_endpoint_descriptor
110 struct usb_request *tx_req, *rx_req;
112 struct eth_device *net;
113 struct net_device_stats stats;
114 unsigned int tx_qlen;
119 unsigned suspended:1;
120 unsigned network_started:1;
124 #define WORK_RX_MEMORY 0
126 u8 host_mac[ETH_ALEN];
130 * This version autoconfigures as much as possible at run-time.
132 * It also ASSUMES a self-powered device, without remote wakeup,
133 * although remote wakeup support would make sense.
136 /*-------------------------------------------------------------------------*/
137 static struct eth_dev l_ethdev;
138 static struct eth_device l_netdev;
139 static struct usb_gadget_driver eth_driver;
141 /*-------------------------------------------------------------------------*/
143 /* "main" config is either CDC, or its simple subset */
144 static inline int is_cdc(struct eth_dev *dev)
146 #if !defined(CONFIG_USB_ETH_SUBSET)
147 return 1; /* only cdc possible */
148 #elif !defined(CONFIG_USB_ETH_CDC)
149 return 0; /* only subset possible */
151 return dev->cdc; /* depends on what hardware we found */
155 /* "secondary" RNDIS config may sometimes be activated */
156 static inline int rndis_active(struct eth_dev *dev)
158 #ifdef CONFIG_USB_ETH_RNDIS
165 #define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
166 #define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev))
168 #define DEFAULT_QLEN 2 /* double buffering by default */
170 /* peak bulk transfer bits-per-second */
171 #define HS_BPS (13 * 512 * 8 * 1000 * 8)
172 #define FS_BPS (19 * 64 * 1 * 1000 * 8)
174 #ifdef CONFIG_USB_GADGET_DUALSPEED
175 #define DEVSPEED USB_SPEED_HIGH
177 #ifdef CONFIG_USB_ETH_QMULT
178 #define qmult CONFIG_USB_ETH_QMULT
183 /* for dual-speed hardware, use deeper queues at highspeed */
184 #define qlen(gadget) \
185 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
187 static inline int BITRATE(struct usb_gadget *g)
189 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
192 #else /* full speed (low speed doesn't do bulk) */
196 #define DEVSPEED USB_SPEED_FULL
198 #define qlen(gadget) DEFAULT_QLEN
200 static inline int BITRATE(struct usb_gadget *g)
206 /*-------------------------------------------------------------------------*/
209 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
210 * Instead: allocate your own, using normal USB-IF procedures.
214 * Thanks to NetChip Technologies for donating this product ID.
215 * It's for devices with only CDC Ethernet configurations.
217 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
218 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
221 * For hardware that can't talk CDC, we use the same vendor ID that
222 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
223 * with pxa250. We're protocol-compatible, if the host-side drivers
224 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
225 * drivers that need to hard-wire endpoint numbers have a hook.
227 * The protocol is a minimal subset of CDC Ether, which works on any bulk
228 * hardware that's not deeply broken ... even on hardware that can't talk
229 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
230 * doesn't handle control-OUT).
232 #define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */
233 #define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */
236 * For hardware that can talk RNDIS and either of the above protocols,
237 * use this ID ... the windows INF files will know it. Unless it's
238 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
239 * the non-RNDIS configuration.
241 #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
242 #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
245 * Some systems will want different product identifers published in the
246 * device descriptor, either numbers or strings or both. These string
247 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
251 * Emulating them in eth_bind:
252 * static ushort idVendor;
253 * static ushort idProduct;
256 #if defined(CONFIG_USBNET_MANUFACTURER)
257 static char *iManufacturer = CONFIG_USBNET_MANUFACTURER;
259 static char *iManufacturer = "U-Boot";
262 /* These probably need to be configurable. */
263 static ushort bcdDevice;
264 static char *iProduct;
265 static char *iSerialNumber;
267 static char dev_addr[18];
269 static char host_addr[18];
272 /*-------------------------------------------------------------------------*/
275 * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
276 * ep0 implementation: descriptors, config management, setup().
277 * also optional class-specific notification interrupt transfer.
281 * DESCRIPTORS ... most are static, but strings and (full) configuration
282 * descriptors are built on demand. For now we do either full CDC, or
283 * our simple subset, with RNDIS as an optional second configuration.
285 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
286 * the class descriptors match a modem (they're ignored; it's really just
287 * Ethernet functionality), they don't need the NOP altsetting, and the
288 * status transfer endpoint isn't optional.
291 #define STRING_MANUFACTURER 1
292 #define STRING_PRODUCT 2
293 #define STRING_ETHADDR 3
294 #define STRING_DATA 4
295 #define STRING_CONTROL 5
296 #define STRING_RNDIS_CONTROL 6
298 #define STRING_SUBSET 8
299 #define STRING_RNDIS 9
300 #define STRING_SERIALNUMBER 10
302 /* holds our biggest descriptor (or RNDIS response) */
303 #define USB_BUFSIZ 256
306 * This device advertises one configuration, eth_config, unless RNDIS
307 * is enabled (rndis_config) on hardware supporting at least two configs.
309 * NOTE: Controllers like superh_udc should probably be able to use
310 * an RNDIS-only configuration.
312 * FIXME define some higher-powered configurations to make it easier
313 * to recharge batteries ...
316 #define DEV_CONFIG_VALUE 1 /* cdc or subset */
317 #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
319 static struct usb_device_descriptor
321 .bLength = sizeof device_desc,
322 .bDescriptorType = USB_DT_DEVICE,
324 .bcdUSB = __constant_cpu_to_le16(0x0200),
326 .bDeviceClass = USB_CLASS_COMM,
327 .bDeviceSubClass = 0,
328 .bDeviceProtocol = 0,
330 .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
331 .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
332 .iManufacturer = STRING_MANUFACTURER,
333 .iProduct = STRING_PRODUCT,
334 .bNumConfigurations = 1,
337 static struct usb_otg_descriptor
339 .bLength = sizeof otg_descriptor,
340 .bDescriptorType = USB_DT_OTG,
342 .bmAttributes = USB_OTG_SRP,
345 static struct usb_config_descriptor
347 .bLength = sizeof eth_config,
348 .bDescriptorType = USB_DT_CONFIG,
350 /* compute wTotalLength on the fly */
352 .bConfigurationValue = DEV_CONFIG_VALUE,
353 .iConfiguration = STRING_CDC,
354 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
358 #ifdef CONFIG_USB_ETH_RNDIS
359 static struct usb_config_descriptor
361 .bLength = sizeof rndis_config,
362 .bDescriptorType = USB_DT_CONFIG,
364 /* compute wTotalLength on the fly */
366 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
367 .iConfiguration = STRING_RNDIS,
368 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
374 * Compared to the simple CDC subset, the full CDC Ethernet model adds
375 * three class descriptors, two interface descriptors, optional status
376 * endpoint. Both have a "data" interface and two bulk endpoints.
377 * There are also differences in how control requests are handled.
379 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
380 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
381 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
382 * work around those bugs) are unlikely to ever come from MSFT, you may
383 * wish to avoid using RNDIS.
385 * MCCI offers an alternative to RNDIS if you need to connect to Windows
386 * but have hardware that can't support CDC Ethernet. We add descriptors
387 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
388 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
389 * get those drivers from MCCI, or bundled with various products.
392 #ifdef CONFIG_USB_ETH_CDC
393 static struct usb_interface_descriptor
395 .bLength = sizeof control_intf,
396 .bDescriptorType = USB_DT_INTERFACE,
398 .bInterfaceNumber = 0,
399 /* status endpoint is optional; this may be patched later */
401 .bInterfaceClass = USB_CLASS_COMM,
402 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
403 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
404 .iInterface = STRING_CONTROL,
408 #ifdef CONFIG_USB_ETH_RNDIS
409 static const struct usb_interface_descriptor
410 rndis_control_intf = {
411 .bLength = sizeof rndis_control_intf,
412 .bDescriptorType = USB_DT_INTERFACE,
414 .bInterfaceNumber = 0,
416 .bInterfaceClass = USB_CLASS_COMM,
417 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
418 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
419 .iInterface = STRING_RNDIS_CONTROL,
423 static const struct usb_cdc_header_desc header_desc = {
424 .bLength = sizeof header_desc,
425 .bDescriptorType = USB_DT_CS_INTERFACE,
426 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
428 .bcdCDC = __constant_cpu_to_le16(0x0110),
431 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
433 static const struct usb_cdc_union_desc union_desc = {
434 .bLength = sizeof union_desc,
435 .bDescriptorType = USB_DT_CS_INTERFACE,
436 .bDescriptorSubType = USB_CDC_UNION_TYPE,
438 .bMasterInterface0 = 0, /* index of control interface */
439 .bSlaveInterface0 = 1, /* index of DATA interface */
442 #endif /* CDC || RNDIS */
444 #ifdef CONFIG_USB_ETH_RNDIS
446 static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
447 .bLength = sizeof call_mgmt_descriptor,
448 .bDescriptorType = USB_DT_CS_INTERFACE,
449 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
451 .bmCapabilities = 0x00,
452 .bDataInterface = 0x01,
455 static const struct usb_cdc_acm_descriptor acm_descriptor = {
456 .bLength = sizeof acm_descriptor,
457 .bDescriptorType = USB_DT_CS_INTERFACE,
458 .bDescriptorSubType = USB_CDC_ACM_TYPE,
460 .bmCapabilities = 0x00,
465 #ifndef CONFIG_USB_ETH_CDC
468 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
469 * ways: data endpoints live in the control interface, there's no data
470 * interface, and it's not used to talk to a cell phone radio.
473 static const struct usb_cdc_mdlm_desc mdlm_desc = {
474 .bLength = sizeof mdlm_desc,
475 .bDescriptorType = USB_DT_CS_INTERFACE,
476 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
478 .bcdVersion = __constant_cpu_to_le16(0x0100),
480 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
481 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
486 * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
487 * can't really use its struct. All we do here is say that we're using
488 * the submode of "SAFE" which directly matches the CDC Subset.
490 static const u8 mdlm_detail_desc[] = {
493 USB_CDC_MDLM_DETAIL_TYPE,
496 0, /* network control capabilities (none) */
497 0, /* network data capabilities ("raw" encapsulation) */
502 static const struct usb_cdc_ether_desc ether_desc = {
503 .bLength = sizeof(ether_desc),
504 .bDescriptorType = USB_DT_CS_INTERFACE,
505 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
507 /* this descriptor actually adds value, surprise! */
508 .iMACAddress = STRING_ETHADDR,
509 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
510 .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN),
511 .wNumberMCFilters = __constant_cpu_to_le16(0),
512 .bNumberPowerFilters = 0,
515 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
518 * include the status endpoint if we can, even where it's optional.
519 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
520 * packet, to simplify cancellation; and a big transfer interval, to
521 * waste less bandwidth.
523 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
524 * if they ignore the connect/disconnect notifications that real aether
525 * can provide. more advanced cdc configurations might want to support
526 * encapsulated commands (vendor-specific, using control-OUT).
528 * RNDIS requires the status endpoint, since it uses that encapsulation
529 * mechanism for its funky RPC scheme.
532 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
533 #define STATUS_BYTECOUNT 16 /* 8 byte header + data */
535 static struct usb_endpoint_descriptor
537 .bLength = USB_DT_ENDPOINT_SIZE,
538 .bDescriptorType = USB_DT_ENDPOINT,
540 .bEndpointAddress = USB_DIR_IN,
541 .bmAttributes = USB_ENDPOINT_XFER_INT,
542 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
543 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
547 #ifdef CONFIG_USB_ETH_CDC
549 /* the default data interface has no endpoints ... */
551 static const struct usb_interface_descriptor
553 .bLength = sizeof data_nop_intf,
554 .bDescriptorType = USB_DT_INTERFACE,
556 .bInterfaceNumber = 1,
557 .bAlternateSetting = 0,
559 .bInterfaceClass = USB_CLASS_CDC_DATA,
560 .bInterfaceSubClass = 0,
561 .bInterfaceProtocol = 0,
564 /* ... but the "real" data interface has two bulk endpoints */
566 static const struct usb_interface_descriptor
568 .bLength = sizeof data_intf,
569 .bDescriptorType = USB_DT_INTERFACE,
571 .bInterfaceNumber = 1,
572 .bAlternateSetting = 1,
574 .bInterfaceClass = USB_CLASS_CDC_DATA,
575 .bInterfaceSubClass = 0,
576 .bInterfaceProtocol = 0,
577 .iInterface = STRING_DATA,
582 #ifdef CONFIG_USB_ETH_RNDIS
584 /* RNDIS doesn't activate by changing to the "real" altsetting */
586 static const struct usb_interface_descriptor
588 .bLength = sizeof rndis_data_intf,
589 .bDescriptorType = USB_DT_INTERFACE,
591 .bInterfaceNumber = 1,
592 .bAlternateSetting = 0,
594 .bInterfaceClass = USB_CLASS_CDC_DATA,
595 .bInterfaceSubClass = 0,
596 .bInterfaceProtocol = 0,
597 .iInterface = STRING_DATA,
602 #ifdef CONFIG_USB_ETH_SUBSET
605 * "Simple" CDC-subset option is a simple vendor-neutral model that most
606 * full speed controllers can handle: one interface, two bulk endpoints.
608 * To assist host side drivers, we fancy it up a bit, and add descriptors
609 * so some host side drivers will understand it as a "SAFE" variant.
612 static const struct usb_interface_descriptor
614 .bLength = sizeof subset_data_intf,
615 .bDescriptorType = USB_DT_INTERFACE,
617 .bInterfaceNumber = 0,
618 .bAlternateSetting = 0,
620 .bInterfaceClass = USB_CLASS_COMM,
621 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
622 .bInterfaceProtocol = 0,
623 .iInterface = STRING_DATA,
628 static struct usb_endpoint_descriptor
630 .bLength = USB_DT_ENDPOINT_SIZE,
631 .bDescriptorType = USB_DT_ENDPOINT,
633 .bEndpointAddress = USB_DIR_IN,
634 .bmAttributes = USB_ENDPOINT_XFER_BULK,
635 .wMaxPacketSize = __constant_cpu_to_le16(64),
638 static struct usb_endpoint_descriptor
640 .bLength = USB_DT_ENDPOINT_SIZE,
641 .bDescriptorType = USB_DT_ENDPOINT,
643 .bEndpointAddress = USB_DIR_OUT,
644 .bmAttributes = USB_ENDPOINT_XFER_BULK,
645 .wMaxPacketSize = __constant_cpu_to_le16(64),
648 static const struct usb_descriptor_header *fs_eth_function[11] = {
649 (struct usb_descriptor_header *) &otg_descriptor,
650 #ifdef CONFIG_USB_ETH_CDC
651 /* "cdc" mode descriptors */
652 (struct usb_descriptor_header *) &control_intf,
653 (struct usb_descriptor_header *) &header_desc,
654 (struct usb_descriptor_header *) &union_desc,
655 (struct usb_descriptor_header *) ðer_desc,
656 /* NOTE: status endpoint may need to be removed */
657 (struct usb_descriptor_header *) &fs_status_desc,
658 /* data interface, with altsetting */
659 (struct usb_descriptor_header *) &data_nop_intf,
660 (struct usb_descriptor_header *) &data_intf,
661 (struct usb_descriptor_header *) &fs_source_desc,
662 (struct usb_descriptor_header *) &fs_sink_desc,
664 #endif /* CONFIG_USB_ETH_CDC */
667 static inline void fs_subset_descriptors(void)
669 #ifdef CONFIG_USB_ETH_SUBSET
670 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
671 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
672 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
673 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
674 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
675 fs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
676 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
677 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
678 fs_eth_function[8] = NULL;
680 fs_eth_function[1] = NULL;
684 #ifdef CONFIG_USB_ETH_RNDIS
685 static const struct usb_descriptor_header *fs_rndis_function[] = {
686 (struct usb_descriptor_header *) &otg_descriptor,
687 /* control interface matches ACM, not Ethernet */
688 (struct usb_descriptor_header *) &rndis_control_intf,
689 (struct usb_descriptor_header *) &header_desc,
690 (struct usb_descriptor_header *) &call_mgmt_descriptor,
691 (struct usb_descriptor_header *) &acm_descriptor,
692 (struct usb_descriptor_header *) &union_desc,
693 (struct usb_descriptor_header *) &fs_status_desc,
694 /* data interface has no altsetting */
695 (struct usb_descriptor_header *) &rndis_data_intf,
696 (struct usb_descriptor_header *) &fs_source_desc,
697 (struct usb_descriptor_header *) &fs_sink_desc,
703 * usb 2.0 devices need to expose both high speed and full speed
704 * descriptors, unless they only run at full speed.
707 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
708 static struct usb_endpoint_descriptor
710 .bLength = USB_DT_ENDPOINT_SIZE,
711 .bDescriptorType = USB_DT_ENDPOINT,
713 .bmAttributes = USB_ENDPOINT_XFER_INT,
714 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
715 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
717 #endif /* CONFIG_USB_ETH_CDC */
719 static struct usb_endpoint_descriptor
721 .bLength = USB_DT_ENDPOINT_SIZE,
722 .bDescriptorType = USB_DT_ENDPOINT,
724 .bmAttributes = USB_ENDPOINT_XFER_BULK,
725 .wMaxPacketSize = __constant_cpu_to_le16(512),
728 static struct usb_endpoint_descriptor
730 .bLength = USB_DT_ENDPOINT_SIZE,
731 .bDescriptorType = USB_DT_ENDPOINT,
733 .bmAttributes = USB_ENDPOINT_XFER_BULK,
734 .wMaxPacketSize = __constant_cpu_to_le16(512),
737 static struct usb_qualifier_descriptor
739 .bLength = sizeof dev_qualifier,
740 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
742 .bcdUSB = __constant_cpu_to_le16(0x0200),
743 .bDeviceClass = USB_CLASS_COMM,
745 .bNumConfigurations = 1,
748 static const struct usb_descriptor_header *hs_eth_function[11] = {
749 (struct usb_descriptor_header *) &otg_descriptor,
750 #ifdef CONFIG_USB_ETH_CDC
751 /* "cdc" mode descriptors */
752 (struct usb_descriptor_header *) &control_intf,
753 (struct usb_descriptor_header *) &header_desc,
754 (struct usb_descriptor_header *) &union_desc,
755 (struct usb_descriptor_header *) ðer_desc,
756 /* NOTE: status endpoint may need to be removed */
757 (struct usb_descriptor_header *) &hs_status_desc,
758 /* data interface, with altsetting */
759 (struct usb_descriptor_header *) &data_nop_intf,
760 (struct usb_descriptor_header *) &data_intf,
761 (struct usb_descriptor_header *) &hs_source_desc,
762 (struct usb_descriptor_header *) &hs_sink_desc,
764 #endif /* CONFIG_USB_ETH_CDC */
767 static inline void hs_subset_descriptors(void)
769 #ifdef CONFIG_USB_ETH_SUBSET
770 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
771 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
772 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
773 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
774 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
775 hs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
776 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
777 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
778 hs_eth_function[8] = NULL;
780 hs_eth_function[1] = NULL;
784 #ifdef CONFIG_USB_ETH_RNDIS
785 static const struct usb_descriptor_header *hs_rndis_function[] = {
786 (struct usb_descriptor_header *) &otg_descriptor,
787 /* control interface matches ACM, not Ethernet */
788 (struct usb_descriptor_header *) &rndis_control_intf,
789 (struct usb_descriptor_header *) &header_desc,
790 (struct usb_descriptor_header *) &call_mgmt_descriptor,
791 (struct usb_descriptor_header *) &acm_descriptor,
792 (struct usb_descriptor_header *) &union_desc,
793 (struct usb_descriptor_header *) &hs_status_desc,
794 /* data interface has no altsetting */
795 (struct usb_descriptor_header *) &rndis_data_intf,
796 (struct usb_descriptor_header *) &hs_source_desc,
797 (struct usb_descriptor_header *) &hs_sink_desc,
803 /* maxpacket and other transfer characteristics vary by speed. */
804 static inline struct usb_endpoint_descriptor *
805 ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
806 struct usb_endpoint_descriptor *fs)
808 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
813 /*-------------------------------------------------------------------------*/
815 /* descriptors that are built on-demand */
817 static char manufacturer[50];
818 static char product_desc[40] = DRIVER_DESC;
819 static char serial_number[20];
821 /* address that the host will use ... usually assigned at random */
822 static char ethaddr[2 * ETH_ALEN + 1];
824 /* static strings, in UTF-8 */
825 static struct usb_string strings[] = {
826 { STRING_MANUFACTURER, manufacturer, },
827 { STRING_PRODUCT, product_desc, },
828 { STRING_SERIALNUMBER, serial_number, },
829 { STRING_DATA, "Ethernet Data", },
830 { STRING_ETHADDR, ethaddr, },
831 #ifdef CONFIG_USB_ETH_CDC
832 { STRING_CDC, "CDC Ethernet", },
833 { STRING_CONTROL, "CDC Communications Control", },
835 #ifdef CONFIG_USB_ETH_SUBSET
836 { STRING_SUBSET, "CDC Ethernet Subset", },
838 #ifdef CONFIG_USB_ETH_RNDIS
839 { STRING_RNDIS, "RNDIS", },
840 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
842 { } /* end of list */
845 static struct usb_gadget_strings stringtab = {
846 .language = 0x0409, /* en-us */
850 /*============================================================================*/
851 DEFINE_CACHE_ALIGN_BUFFER(u8, control_req, USB_BUFSIZ);
853 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
854 DEFINE_CACHE_ALIGN_BUFFER(u8, status_req, STATUS_BYTECOUNT);
857 /*============================================================================*/
860 * one config, two interfaces: control, data.
861 * complications: class descriptors, and an altsetting.
864 config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
867 const struct usb_config_descriptor *config;
868 const struct usb_descriptor_header **function;
871 if (gadget_is_dualspeed(g)) {
872 hs = (g->speed == USB_SPEED_HIGH);
873 if (type == USB_DT_OTHER_SPEED_CONFIG)
876 #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
878 if (index >= device_desc.bNumConfigurations)
881 #ifdef CONFIG_USB_ETH_RNDIS
883 * list the RNDIS config first, to make Microsoft's drivers
884 * happy. DOCSIS 1.0 needs this too.
886 if (device_desc.bNumConfigurations == 2 && index == 0) {
887 config = &rndis_config;
888 function = which_fn(rndis);
892 config = ð_config;
893 function = which_fn(eth);
896 /* for now, don't advertise srp-only devices */
900 len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
903 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
907 /*-------------------------------------------------------------------------*/
909 static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
910 static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
913 set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
916 struct usb_gadget *gadget = dev->gadget;
918 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
919 /* status endpoint used for RNDIS and (optionally) CDC */
920 if (!subset_active(dev) && dev->status_ep) {
921 dev->status = ep_desc(gadget, &hs_status_desc,
923 dev->status_ep->driver_data = dev;
925 result = usb_ep_enable(dev->status_ep, dev->status);
927 debug("enable %s --> %d\n",
928 dev->status_ep->name, result);
934 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
935 dev->in_ep->driver_data = dev;
937 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
938 dev->out_ep->driver_data = dev;
941 * With CDC, the host isn't allowed to use these two data
942 * endpoints in the default altsetting for the interface.
943 * so we don't activate them yet. Reset from SET_INTERFACE.
945 * Strictly speaking RNDIS should work the same: activation is
946 * a side effect of setting a packet filter. Deactivation is
947 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
949 if (!cdc_active(dev)) {
950 result = usb_ep_enable(dev->in_ep, dev->in);
952 debug("enable %s --> %d\n",
953 dev->in_ep->name, result);
957 result = usb_ep_enable(dev->out_ep, dev->out);
959 debug("enable %s --> %d\n",
960 dev->out_ep->name, result);
967 result = alloc_requests(dev, qlen(gadget), gfp_flags);
969 /* on error, disable any endpoints */
971 if (!subset_active(dev) && dev->status_ep)
972 (void) usb_ep_disable(dev->status_ep);
974 (void) usb_ep_disable(dev->in_ep);
975 (void) usb_ep_disable(dev->out_ep);
978 } else if (!cdc_active(dev)) {
980 * activate non-CDC configs right away
981 * this isn't strictly according to the RNDIS spec
983 eth_start(dev, GFP_ATOMIC);
986 /* caller is responsible for cleanup on error */
990 static void eth_reset_config(struct eth_dev *dev)
992 if (dev->config == 0)
995 debug("%s\n", __func__);
997 rndis_uninit(dev->rndis_config);
1000 * disable endpoints, forcing (synchronous) completion of
1001 * pending i/o. then free the requests.
1005 usb_ep_disable(dev->in_ep);
1007 usb_ep_free_request(dev->in_ep, dev->tx_req);
1012 usb_ep_disable(dev->out_ep);
1014 usb_ep_free_request(dev->out_ep, dev->rx_req);
1019 usb_ep_disable(dev->status_ep);
1022 dev->cdc_filter = 0;
1027 * change our operational config. must agree with the code
1028 * that returns config descriptors, and altsetting code.
1030 static int eth_set_config(struct eth_dev *dev, unsigned number,
1034 struct usb_gadget *gadget = dev->gadget;
1036 if (gadget_is_sa1100(gadget)
1038 && dev->tx_qlen != 0) {
1039 /* tx fifo is full, but we can't clear it...*/
1040 error("can't change configurations");
1043 eth_reset_config(dev);
1046 case DEV_CONFIG_VALUE:
1047 result = set_ether_config(dev, gfp_flags);
1049 #ifdef CONFIG_USB_ETH_RNDIS
1050 case DEV_RNDIS_CONFIG_VALUE:
1052 result = set_ether_config(dev, gfp_flags);
1064 eth_reset_config(dev);
1065 usb_gadget_vbus_draw(dev->gadget,
1066 gadget_is_otg(dev->gadget) ? 8 : 100);
1071 power = 2 * eth_config.bMaxPower;
1072 usb_gadget_vbus_draw(dev->gadget, power);
1074 switch (gadget->speed) {
1075 case USB_SPEED_FULL:
1076 speed = "full"; break;
1077 #ifdef CONFIG_USB_GADGET_DUALSPEED
1078 case USB_SPEED_HIGH:
1079 speed = "high"; break;
1085 dev->config = number;
1086 printf("%s speed config #%d: %d mA, %s, using %s\n",
1087 speed, number, power, driver_desc,
1092 : "CDC Ethernet Subset"));
1097 /*-------------------------------------------------------------------------*/
1099 #ifdef CONFIG_USB_ETH_CDC
1102 * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
1103 * only to notify the host about link status changes (which we support) or
1104 * report completion of some encapsulated command (as used in RNDIS). Since
1105 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1106 * command mechanism; and only one status request is ever queued.
1108 static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
1110 struct usb_cdc_notification *event = req->buf;
1111 int value = req->status;
1112 struct eth_dev *dev = ep->driver_data;
1114 /* issue the second notification if host reads the first */
1115 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1117 __le32 *data = req->buf + sizeof *event;
1119 event->bmRequestType = 0xA1;
1120 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
1121 event->wValue = __constant_cpu_to_le16(0);
1122 event->wIndex = __constant_cpu_to_le16(1);
1123 event->wLength = __constant_cpu_to_le16(8);
1125 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1126 data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
1128 req->length = STATUS_BYTECOUNT;
1129 value = usb_ep_queue(ep, req, GFP_ATOMIC);
1130 debug("send SPEED_CHANGE --> %d\n", value);
1133 } else if (value != -ECONNRESET) {
1134 debug("event %02x --> %d\n",
1135 event->bNotificationType, value);
1136 if (event->bNotificationType ==
1137 USB_CDC_NOTIFY_SPEED_CHANGE) {
1138 l_ethdev.network_started = 1;
1139 printf("USB network up!\n");
1142 req->context = NULL;
1145 static void issue_start_status(struct eth_dev *dev)
1147 struct usb_request *req = dev->stat_req;
1148 struct usb_cdc_notification *event;
1154 * FIXME ugly idiom, maybe we'd be better with just
1155 * a "cancel the whole queue" primitive since any
1156 * unlink-one primitive has way too many error modes.
1157 * here, we "know" toggle is already clear...
1159 * FIXME iff req->context != null just dequeue it
1161 usb_ep_disable(dev->status_ep);
1162 usb_ep_enable(dev->status_ep, dev->status);
1165 * 3.8.1 says to issue first NETWORK_CONNECTION, then
1166 * a SPEED_CHANGE. could be useful in some configs.
1169 event->bmRequestType = 0xA1;
1170 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
1171 event->wValue = __constant_cpu_to_le16(1); /* connected */
1172 event->wIndex = __constant_cpu_to_le16(1);
1175 req->length = sizeof *event;
1176 req->complete = eth_status_complete;
1179 value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
1181 debug("status buf queue --> %d\n", value);
1186 /*-------------------------------------------------------------------------*/
1188 static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
1190 if (req->status || req->actual != req->length)
1191 debug("setup complete --> %d, %d/%d\n",
1192 req->status, req->actual, req->length);
1195 #ifdef CONFIG_USB_ETH_RNDIS
1197 static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
1199 if (req->status || req->actual != req->length)
1200 debug("rndis response complete --> %d, %d/%d\n",
1201 req->status, req->actual, req->length);
1203 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1206 static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
1208 struct eth_dev *dev = ep->driver_data;
1211 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1212 status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
1214 error("%s: rndis parse error %d", __func__, status);
1220 * The setup() callback implements all the ep0 functionality that's not
1221 * handled lower down. CDC has a number of less-common features:
1223 * - two interfaces: control, and ethernet data
1224 * - Ethernet data interface has two altsettings: default, and active
1225 * - class-specific descriptors for the control interface
1226 * - class-specific control requests
1229 eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1231 struct eth_dev *dev = get_gadget_data(gadget);
1232 struct usb_request *req = dev->req;
1233 int value = -EOPNOTSUPP;
1234 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1235 u16 wValue = le16_to_cpu(ctrl->wValue);
1236 u16 wLength = le16_to_cpu(ctrl->wLength);
1239 * descriptors just go into the pre-allocated ep0 buffer,
1240 * while config change events may enable network traffic.
1243 debug("%s\n", __func__);
1245 req->complete = eth_setup_complete;
1246 switch (ctrl->bRequest) {
1248 case USB_REQ_GET_DESCRIPTOR:
1249 if (ctrl->bRequestType != USB_DIR_IN)
1251 switch (wValue >> 8) {
1254 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1255 value = min(wLength, (u16) sizeof device_desc);
1256 memcpy(req->buf, &device_desc, value);
1258 case USB_DT_DEVICE_QUALIFIER:
1259 if (!gadget_is_dualspeed(gadget))
1261 value = min(wLength, (u16) sizeof dev_qualifier);
1262 memcpy(req->buf, &dev_qualifier, value);
1265 case USB_DT_OTHER_SPEED_CONFIG:
1266 if (!gadget_is_dualspeed(gadget))
1270 value = config_buf(gadget, req->buf,
1273 gadget_is_otg(gadget));
1275 value = min(wLength, (u16) value);
1279 value = usb_gadget_get_string(&stringtab,
1280 wValue & 0xff, req->buf);
1283 value = min(wLength, (u16) value);
1289 case USB_REQ_SET_CONFIGURATION:
1290 if (ctrl->bRequestType != 0)
1292 if (gadget->a_hnp_support)
1293 debug("HNP available\n");
1294 else if (gadget->a_alt_hnp_support)
1295 debug("HNP needs a different root port\n");
1296 value = eth_set_config(dev, wValue, GFP_ATOMIC);
1298 case USB_REQ_GET_CONFIGURATION:
1299 if (ctrl->bRequestType != USB_DIR_IN)
1301 *(u8 *)req->buf = dev->config;
1302 value = min(wLength, (u16) 1);
1305 case USB_REQ_SET_INTERFACE:
1306 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1310 if (!cdc_active(dev) && wIndex != 0)
1314 * PXA hardware partially handles SET_INTERFACE;
1315 * we need to kluge around that interference.
1317 if (gadget_is_pxa(gadget)) {
1318 value = eth_set_config(dev, DEV_CONFIG_VALUE,
1321 * PXA25x driver use non-CDC ethernet gadget.
1322 * But only _CDC and _RNDIS code can signalize
1323 * that network is working. So we signalize it
1326 l_ethdev.network_started = 1;
1327 debug("USB network up!\n");
1331 #ifdef CONFIG_USB_ETH_CDC
1333 case 0: /* control/master intf */
1337 usb_ep_disable(dev->status_ep);
1338 usb_ep_enable(dev->status_ep, dev->status);
1343 case 1: /* data intf */
1346 usb_ep_disable(dev->in_ep);
1347 usb_ep_disable(dev->out_ep);
1350 * CDC requires the data transfers not be done from
1351 * the default interface setting ... also, setting
1352 * the non-default interface resets filters etc.
1355 if (!cdc_active(dev))
1357 usb_ep_enable(dev->in_ep, dev->in);
1358 usb_ep_enable(dev->out_ep, dev->out);
1359 dev->cdc_filter = DEFAULT_FILTER;
1361 issue_start_status(dev);
1362 eth_start(dev, GFP_ATOMIC);
1369 * FIXME this is wrong, as is the assumption that
1370 * all non-PXA hardware talks real CDC ...
1372 debug("set_interface ignored!\n");
1373 #endif /* CONFIG_USB_ETH_CDC */
1377 case USB_REQ_GET_INTERFACE:
1378 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1382 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
1385 /* for CDC, iff carrier is on, data interface is active. */
1386 if (rndis_active(dev) || wIndex != 1)
1387 *(u8 *)req->buf = 0;
1389 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1390 /* carrier always ok ...*/
1391 *(u8 *)req->buf = 1 ;
1393 value = min(wLength, (u16) 1);
1396 #ifdef CONFIG_USB_ETH_CDC
1397 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
1399 * see 6.2.30: no data, wIndex = interface,
1400 * wValue = packet filter bitmap
1402 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1407 debug("packet filter %02x\n", wValue);
1408 dev->cdc_filter = wValue;
1414 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1415 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1416 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1417 * case USB_CDC_GET_ETHERNET_STATISTIC:
1420 #endif /* CONFIG_USB_ETH_CDC */
1422 #ifdef CONFIG_USB_ETH_RNDIS
1424 * RNDIS uses the CDC command encapsulation mechanism to implement
1425 * an RPC scheme, with much getting/setting of attributes by OID.
1427 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1428 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1429 || !rndis_active(dev)
1430 || wLength > USB_BUFSIZ
1432 || rndis_control_intf.bInterfaceNumber
1435 /* read the request, then process it */
1437 req->complete = rndis_command_complete;
1438 /* later, rndis_control_ack () sends a notification */
1441 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1442 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1443 == ctrl->bRequestType
1444 && rndis_active(dev)
1445 /* && wLength >= 0x0400 */
1447 && rndis_control_intf.bInterfaceNumber
1452 /* return the result */
1453 buf = rndis_get_next_response(dev->rndis_config, &n);
1455 memcpy(req->buf, buf, n);
1456 req->complete = rndis_response_complete;
1457 rndis_free_response(dev->rndis_config, buf);
1460 /* else stalls ... spec says to avoid that */
1466 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
1467 ctrl->bRequestType, ctrl->bRequest,
1468 wValue, wIndex, wLength);
1471 /* respond with data transfer before status phase? */
1473 debug("respond with data transfer before status phase\n");
1474 req->length = value;
1475 req->zero = value < wLength
1476 && (value % gadget->ep0->maxpacket) == 0;
1477 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1479 debug("ep_queue --> %d\n", value);
1481 eth_setup_complete(gadget->ep0, req);
1485 /* host either stalls (value < 0) or reports success */
1489 /*-------------------------------------------------------------------------*/
1491 static void rx_complete(struct usb_ep *ep, struct usb_request *req);
1493 static int rx_submit(struct eth_dev *dev, struct usb_request *req,
1496 int retval = -ENOMEM;
1500 * Padding up to RX_EXTRA handles minor disagreements with host.
1501 * Normally we use the USB "terminate on short read" convention;
1502 * so allow up to (N*maxpacket), since that memory is normally
1503 * already allocated. Some hardware doesn't deal well with short
1504 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1505 * byte off the end (to force hardware errors on overflow).
1507 * RNDIS uses internal framing, and explicitly allows senders to
1508 * pad to end-of-packet. That's potentially nice for speed,
1509 * but means receivers can't recover synch on their own.
1512 debug("%s\n", __func__);
1516 size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1517 size += dev->out_ep->maxpacket - 1;
1518 if (rndis_active(dev))
1519 size += sizeof(struct rndis_packet_msg_type);
1520 size -= size % dev->out_ep->maxpacket;
1523 * Some platforms perform better when IP packets are aligned,
1524 * but on at least one, checksumming fails otherwise. Note:
1525 * RNDIS headers involve variable numbers of LE32 values.
1528 req->buf = (u8 *)net_rx_packets[0];
1530 req->complete = rx_complete;
1532 retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
1535 error("rx submit --> %d", retval);
1540 static void rx_complete(struct usb_ep *ep, struct usb_request *req)
1542 struct eth_dev *dev = ep->driver_data;
1544 debug("%s: status %d\n", __func__, req->status);
1545 switch (req->status) {
1546 /* normal completion */
1548 if (rndis_active(dev)) {
1549 /* we know MaxPacketsPerTransfer == 1 here */
1550 int length = rndis_rm_hdr(req->buf, req->actual);
1553 req->length -= length;
1554 req->actual -= length;
1556 if (req->actual < ETH_HLEN || ETH_FRAME_LEN < req->actual) {
1558 dev->stats.rx_errors++;
1559 dev->stats.rx_length_errors++;
1560 debug("rx length %d\n", req->length);
1564 dev->stats.rx_packets++;
1565 dev->stats.rx_bytes += req->length;
1568 /* software-driven interface shutdown */
1569 case -ECONNRESET: /* unlink */
1570 case -ESHUTDOWN: /* disconnect etc */
1571 /* for hardware automagic (such as pxa) */
1572 case -ECONNABORTED: /* endpoint reset */
1577 dev->stats.rx_over_errors++;
1580 dev->stats.rx_errors++;
1584 packet_received = 1;
1587 static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
1590 dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
1595 dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
1603 usb_ep_free_request(dev->in_ep, dev->tx_req);
1605 error("can't alloc requests");
1609 static void tx_complete(struct usb_ep *ep, struct usb_request *req)
1611 struct eth_dev *dev = ep->driver_data;
1613 debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
1614 switch (req->status) {
1616 dev->stats.tx_errors++;
1617 debug("tx err %d\n", req->status);
1619 case -ECONNRESET: /* unlink */
1620 case -ESHUTDOWN: /* disconnect etc */
1623 dev->stats.tx_bytes += req->length;
1625 dev->stats.tx_packets++;
1630 static inline int eth_is_promisc(struct eth_dev *dev)
1632 /* no filters for the CDC subset; always promisc */
1633 if (subset_active(dev))
1635 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1639 static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1641 struct eth_dev *dev = netdev_priv(net);
1642 int length = skb->len;
1644 struct usb_request *req = NULL;
1645 unsigned long flags;
1647 /* apply outgoing CDC or RNDIS filters */
1648 if (!eth_is_promisc (dev)) {
1649 u8 *dest = skb->data;
1651 if (is_multicast_ethaddr(dest)) {
1654 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1655 * SET_ETHERNET_MULTICAST_FILTERS requests
1657 if (is_broadcast_ethaddr(dest))
1658 type = USB_CDC_PACKET_TYPE_BROADCAST;
1660 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1661 if (!(dev->cdc_filter & type)) {
1662 dev_kfree_skb_any (skb);
1666 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1669 spin_lock_irqsave(&dev->req_lock, flags);
1671 * this freelist can be empty if an interrupt triggered disconnect()
1672 * and reconfigured the gadget (shutting down this queue) after the
1673 * network stack decided to xmit but before we got the spinlock.
1675 if (list_empty(&dev->tx_reqs)) {
1676 spin_unlock_irqrestore(&dev->req_lock, flags);
1680 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1681 list_del (&req->list);
1683 /* temporarily stop TX queue when the freelist empties */
1684 if (list_empty (&dev->tx_reqs))
1685 netif_stop_queue (net);
1686 spin_unlock_irqrestore(&dev->req_lock, flags);
1688 /* no buffer copies needed, unless the network stack did it
1689 * or the hardware can't use skb buffers.
1690 * or there's not enough space for any RNDIS headers we need
1692 if (rndis_active(dev)) {
1693 struct sk_buff *skb_rndis;
1695 skb_rndis = skb_realloc_headroom (skb,
1696 sizeof (struct rndis_packet_msg_type));
1700 dev_kfree_skb_any (skb);
1702 rndis_add_hdr (skb);
1705 req->buf = skb->data;
1707 req->complete = tx_complete;
1709 /* use zlp framing on tx for strict CDC-Ether conformance,
1710 * though any robust network rx path ignores extra padding.
1711 * and some hardware doesn't like to write zlps.
1714 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1717 req->length = length;
1719 /* throttle highspeed IRQ rate back slightly */
1720 if (gadget_is_dualspeed(dev->gadget))
1721 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1722 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1725 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1728 DEBUG (dev, "tx queue err %d\n", retval);
1731 net->trans_start = jiffies;
1732 atomic_inc (&dev->tx_qlen);
1737 dev->stats.tx_dropped++;
1738 dev_kfree_skb_any (skb);
1739 spin_lock_irqsave(&dev->req_lock, flags);
1740 if (list_empty (&dev->tx_reqs))
1741 netif_start_queue (net);
1742 list_add (&req->list, &dev->tx_reqs);
1743 spin_unlock_irqrestore(&dev->req_lock, flags);
1748 /*-------------------------------------------------------------------------*/
1751 static void eth_unbind(struct usb_gadget *gadget)
1753 struct eth_dev *dev = get_gadget_data(gadget);
1755 debug("%s...\n", __func__);
1756 rndis_deregister(dev->rndis_config);
1759 /* we've already been disconnected ... no i/o is active */
1761 usb_ep_free_request(gadget->ep0, dev->req);
1764 if (dev->stat_req) {
1765 usb_ep_free_request(dev->status_ep, dev->stat_req);
1766 dev->stat_req = NULL;
1770 usb_ep_free_request(dev->in_ep, dev->tx_req);
1775 usb_ep_free_request(dev->out_ep, dev->rx_req);
1779 /* unregister_netdev (dev->net);*/
1780 /* free_netdev(dev->net);*/
1783 set_gadget_data(gadget, NULL);
1786 static void eth_disconnect(struct usb_gadget *gadget)
1788 eth_reset_config(get_gadget_data(gadget));
1789 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1792 static void eth_suspend(struct usb_gadget *gadget)
1797 static void eth_resume(struct usb_gadget *gadget)
1802 /*-------------------------------------------------------------------------*/
1804 #ifdef CONFIG_USB_ETH_RNDIS
1807 * The interrupt endpoint is used in RNDIS to notify the host when messages
1808 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1809 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1810 * REMOTE_NDIS_KEEPALIVE_MSG.
1812 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1813 * normally just one notification will be queued.
1816 static void rndis_control_ack_complete(struct usb_ep *ep,
1817 struct usb_request *req)
1819 struct eth_dev *dev = ep->driver_data;
1821 debug("%s...\n", __func__);
1822 if (req->status || req->actual != req->length)
1823 debug("rndis control ack complete --> %d, %d/%d\n",
1824 req->status, req->actual, req->length);
1826 if (!l_ethdev.network_started) {
1827 if (rndis_get_state(dev->rndis_config)
1828 == RNDIS_DATA_INITIALIZED) {
1829 l_ethdev.network_started = 1;
1830 printf("USB RNDIS network up!\n");
1834 req->context = NULL;
1836 if (req != dev->stat_req)
1837 usb_ep_free_request(ep, req);
1840 static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
1842 static int rndis_control_ack(struct eth_device *net)
1844 struct eth_dev *dev = &l_ethdev;
1846 struct usb_request *resp = dev->stat_req;
1848 /* in case RNDIS calls this after disconnect */
1850 debug("status ENODEV\n");
1854 /* in case queue length > 1 */
1855 if (resp->context) {
1856 resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
1859 resp->buf = rndis_resp_buf;
1863 * Send RNDIS RESPONSE_AVAILABLE notification;
1864 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1867 resp->complete = rndis_control_ack_complete;
1868 resp->context = dev;
1870 *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
1871 *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
1873 length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
1876 rndis_control_ack_complete(dev->status_ep, resp);
1884 #define rndis_control_ack NULL
1888 static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
1890 if (rndis_active(dev)) {
1891 rndis_set_param_medium(dev->rndis_config,
1893 BITRATE(dev->gadget)/100);
1894 rndis_signal_connect(dev->rndis_config);
1898 static int eth_stop(struct eth_dev *dev)
1900 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1902 unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1905 if (rndis_active(dev)) {
1906 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
1907 rndis_signal_disconnect(dev->rndis_config);
1909 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1910 /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1912 while (get_timer(ts) < timeout)
1913 usb_gadget_handle_interrupts(0);
1916 rndis_uninit(dev->rndis_config);
1923 /*-------------------------------------------------------------------------*/
1925 static int is_eth_addr_valid(char *str)
1927 if (strlen(str) == 17) {
1932 /* see if it looks like an ethernet address */
1936 for (i = 0; i < 6; i++) {
1937 char term = (i == 5 ? '\0' : ':');
1939 ea[i] = simple_strtol(p, &q, 16);
1941 if ((q - p) != 2 || *q++ != term)
1947 /* Now check the contents. */
1948 return is_valid_ethaddr(ea);
1953 static u8 nibble(unsigned char c)
1955 if (likely(isdigit(c)))
1958 if (likely(isxdigit(c)))
1959 return 10 + c - 'A';
1963 static int get_ether_addr(const char *str, u8 *dev_addr)
1968 for (i = 0; i < 6; i++) {
1971 if ((*str == '.') || (*str == ':'))
1973 num = nibble(*str++) << 4;
1974 num |= (nibble(*str++));
1977 if (is_valid_ethaddr(dev_addr))
1983 static int eth_bind(struct usb_gadget *gadget)
1985 struct eth_dev *dev = &l_ethdev;
1986 u8 cdc = 1, zlp = 1, rndis = 1;
1987 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
1988 int status = -ENOMEM;
1992 /* these flags are only ever cleared; compiler take note */
1993 #ifndef CONFIG_USB_ETH_CDC
1996 #ifndef CONFIG_USB_ETH_RNDIS
2000 * Because most host side USB stacks handle CDC Ethernet, that
2001 * standard protocol is _strongly_ preferred for interop purposes.
2002 * (By everyone except Microsoft.)
2004 if (gadget_is_pxa(gadget)) {
2005 /* pxa doesn't support altsettings */
2007 } else if (gadget_is_musbhdrc(gadget)) {
2008 /* reduce tx dma overhead by avoiding special cases */
2010 } else if (gadget_is_sh(gadget)) {
2011 /* sh doesn't support multiple interfaces or configs */
2014 } else if (gadget_is_sa1100(gadget)) {
2015 /* hardware can't write zlps */
2018 * sa1100 CAN do CDC, without status endpoint ... we use
2019 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2024 gcnum = usb_gadget_controller_number(gadget);
2026 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
2029 * can't assume CDC works. don't want to default to
2030 * anything less functional on CDC-capable hardware,
2031 * so we fail in this case.
2033 error("controller '%s' not recognized",
2039 * If there's an RNDIS configuration, that's what Windows wants to
2040 * be using ... so use these product IDs here and in the "linux.inf"
2041 * needed to install MSFT drivers. Current Linux kernels will use
2042 * the second configuration if it's CDC Ethernet, and need some help
2043 * to choose the right configuration otherwise.
2046 #if defined(CONFIG_USB_RNDIS_VENDOR_ID) && defined(CONFIG_USB_RNDIS_PRODUCT_ID)
2047 device_desc.idVendor =
2048 __constant_cpu_to_le16(CONFIG_USB_RNDIS_VENDOR_ID);
2049 device_desc.idProduct =
2050 __constant_cpu_to_le16(CONFIG_USB_RNDIS_PRODUCT_ID);
2052 device_desc.idVendor =
2053 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2054 device_desc.idProduct =
2055 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2057 sprintf(product_desc, "RNDIS/%s", driver_desc);
2060 * CDC subset ... recognized by Linux since 2.4.10, but Windows
2061 * drivers aren't widely available. (That may be improved by
2062 * supporting one submode of the "SAFE" variant of MDLM.)
2065 #if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID)
2066 device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID);
2067 device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID);
2070 device_desc.idVendor =
2071 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2072 device_desc.idProduct =
2073 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2077 /* support optional vendor/distro customization */
2079 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2081 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
2083 strlcpy(product_desc, iProduct, sizeof product_desc);
2084 if (iSerialNumber) {
2085 device_desc.iSerialNumber = STRING_SERIALNUMBER,
2086 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
2089 /* all we really need is bulk IN/OUT */
2090 usb_ep_autoconfig_reset(gadget);
2091 in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
2094 error("can't autoconfigure on %s\n",
2098 in_ep->driver_data = in_ep; /* claim */
2100 out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
2103 out_ep->driver_data = out_ep; /* claim */
2105 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2107 * CDC Ethernet control interface doesn't require a status endpoint.
2108 * Since some hosts expect one, try to allocate one anyway.
2111 status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
2113 status_ep->driver_data = status_ep; /* claim */
2115 error("can't run RNDIS on %s", gadget->name);
2117 #ifdef CONFIG_USB_ETH_CDC
2119 control_intf.bNumEndpoints = 0;
2120 /* FIXME remove endpoint from descriptor list */
2126 /* one config: cdc, else minimal subset */
2128 eth_config.bNumInterfaces = 1;
2129 eth_config.iConfiguration = STRING_SUBSET;
2132 * use functions to set these up, in case we're built to work
2133 * with multiple controllers and must override CDC Ethernet.
2135 fs_subset_descriptors();
2136 hs_subset_descriptors();
2139 usb_gadget_set_selfpowered(gadget);
2141 /* For now RNDIS is always a second config */
2143 device_desc.bNumConfigurations = 2;
2145 if (gadget_is_dualspeed(gadget)) {
2147 dev_qualifier.bNumConfigurations = 2;
2149 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2151 /* assumes ep0 uses the same value for both speeds ... */
2152 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2154 /* and that all endpoints are dual-speed */
2155 hs_source_desc.bEndpointAddress =
2156 fs_source_desc.bEndpointAddress;
2157 hs_sink_desc.bEndpointAddress =
2158 fs_sink_desc.bEndpointAddress;
2159 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2161 hs_status_desc.bEndpointAddress =
2162 fs_status_desc.bEndpointAddress;
2166 if (gadget_is_otg(gadget)) {
2167 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2168 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2169 eth_config.bMaxPower = 4;
2170 #ifdef CONFIG_USB_ETH_RNDIS
2171 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2172 rndis_config.bMaxPower = 4;
2177 /* network device setup */
2178 dev->net = &l_netdev;
2184 dev->out_ep = out_ep;
2185 dev->status_ep = status_ep;
2188 * Module params for these addresses should come from ID proms.
2189 * The host side address is used with CDC and RNDIS, and commonly
2190 * ends up in a persistent config database. It's not clear if
2191 * host side code for the SAFE thing cares -- its original BLAN
2192 * thing didn't, Sharp never assigned those addresses on Zaurii.
2194 get_ether_addr(dev_addr, dev->net->enetaddr);
2196 memset(tmp, 0, sizeof(tmp));
2197 memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
2199 get_ether_addr(host_addr, dev->host_mac);
2201 sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
2202 dev->host_mac[0], dev->host_mac[1],
2203 dev->host_mac[2], dev->host_mac[3],
2204 dev->host_mac[4], dev->host_mac[5]);
2207 status = rndis_init();
2209 error("can't init RNDIS, %d", status);
2215 * use PKTSIZE (or aligned... from u-boot) and set
2216 * wMaxSegmentSize accordingly
2218 dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
2220 /* preallocate control message data and buffer */
2221 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
2224 dev->req->buf = control_req;
2225 dev->req->complete = eth_setup_complete;
2227 /* ... and maybe likewise for status transfer */
2228 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2229 if (dev->status_ep) {
2230 dev->stat_req = usb_ep_alloc_request(dev->status_ep,
2232 if (!dev->stat_req) {
2233 usb_ep_free_request(dev->status_ep, dev->req);
2237 dev->stat_req->buf = status_req;
2238 dev->stat_req->context = NULL;
2242 /* finish hookup to lower layer ... */
2243 dev->gadget = gadget;
2244 set_gadget_data(gadget, dev);
2245 gadget->ep0->driver_data = dev;
2248 * two kinds of host-initiated state changes:
2249 * - iff DATA transfer is active, carrier is "on"
2250 * - tx queueing enabled if open *and* carrier is "on"
2253 printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
2254 out_ep->name, in_ep->name,
2255 status_ep ? " STATUS " : "",
2256 status_ep ? status_ep->name : ""
2258 printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2259 dev->net->enetaddr[0], dev->net->enetaddr[1],
2260 dev->net->enetaddr[2], dev->net->enetaddr[3],
2261 dev->net->enetaddr[4], dev->net->enetaddr[5]);
2264 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2265 dev->host_mac[0], dev->host_mac[1],
2266 dev->host_mac[2], dev->host_mac[3],
2267 dev->host_mac[4], dev->host_mac[5]);
2272 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2274 dev->rndis_config = rndis_register(rndis_control_ack);
2275 if (dev->rndis_config < 0) {
2278 debug("RNDIS setup failed\n");
2283 /* these set up a lot of the OIDs that RNDIS needs */
2284 rndis_set_host_mac(dev->rndis_config, dev->host_mac);
2285 if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
2286 &dev->stats, &dev->cdc_filter))
2288 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2291 if (rndis_set_param_medium(dev->rndis_config,
2292 NDIS_MEDIUM_802_3, 0))
2294 printf("RNDIS ready\n");
2299 error("%s failed, status = %d", __func__, status);
2304 /*-------------------------------------------------------------------------*/
2306 static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
2308 struct eth_dev *dev = &l_ethdev;
2309 struct usb_gadget *gadget;
2311 unsigned long timeout = USB_CONNECT_TIMEOUT;
2314 error("received NULL ptr");
2318 board_usb_init(0, USB_INIT_DEVICE);
2320 /* Configure default mac-addresses for the USB ethernet device */
2321 #ifdef CONFIG_USBNET_DEV_ADDR
2322 strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
2324 #ifdef CONFIG_USBNET_HOST_ADDR
2325 strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
2327 /* Check if the user overruled the MAC addresses */
2328 if (getenv("usbnet_devaddr"))
2329 strlcpy(dev_addr, getenv("usbnet_devaddr"),
2332 if (getenv("usbnet_hostaddr"))
2333 strlcpy(host_addr, getenv("usbnet_hostaddr"),
2336 if (!is_eth_addr_valid(dev_addr)) {
2337 error("Need valid 'usbnet_devaddr' to be set");
2340 if (!is_eth_addr_valid(host_addr)) {
2341 error("Need valid 'usbnet_hostaddr' to be set");
2345 if (usb_gadget_register_driver(ð_driver) < 0)
2348 dev->network_started = 0;
2350 packet_received = 0;
2353 gadget = dev->gadget;
2354 usb_gadget_connect(gadget);
2356 if (getenv("cdc_connect_timeout"))
2357 timeout = simple_strtoul(getenv("cdc_connect_timeout"),
2358 NULL, 10) * CONFIG_SYS_HZ;
2360 while (!l_ethdev.network_started) {
2361 /* Handle control-c and timeouts */
2362 if (ctrlc() || (get_timer(ts) > timeout)) {
2363 error("The remote end did not respond in time.");
2366 usb_gadget_handle_interrupts(0);
2369 packet_received = 0;
2370 rx_submit(dev, dev->rx_req, 0);
2376 static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
2379 void *rndis_pkt = NULL;
2380 struct eth_dev *dev = &l_ethdev;
2381 struct usb_request *req = dev->tx_req;
2383 unsigned long timeout = USB_CONNECT_TIMEOUT;
2385 debug("%s:...\n", __func__);
2387 /* new buffer is needed to include RNDIS header */
2388 if (rndis_active(dev)) {
2389 rndis_pkt = malloc(length +
2390 sizeof(struct rndis_packet_msg_type));
2392 error("No memory to alloc RNDIS packet");
2395 rndis_add_hdr(rndis_pkt, length);
2396 memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
2399 length += sizeof(struct rndis_packet_msg_type);
2402 req->context = NULL;
2403 req->complete = tx_complete;
2406 * use zlp framing on tx for strict CDC-Ether conformance,
2407 * though any robust network rx path ignores extra padding.
2408 * and some hardware doesn't like to write zlps.
2411 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2414 req->length = length;
2416 /* throttle highspeed IRQ rate back slightly */
2417 if (gadget_is_dualspeed(dev->gadget))
2418 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2419 ? ((dev->tx_qlen % qmult) != 0) : 0;
2425 retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
2428 debug("%s: packet queued\n", __func__);
2429 while (!packet_sent) {
2430 if (get_timer(ts) > timeout) {
2431 printf("timeout sending packets to usb ethernet\n");
2434 usb_gadget_handle_interrupts(0);
2441 dev->stats.tx_dropped++;
2445 static int usb_eth_recv(struct eth_device *netdev)
2447 struct eth_dev *dev = &l_ethdev;
2449 usb_gadget_handle_interrupts(0);
2451 if (packet_received) {
2452 debug("%s: packet received\n", __func__);
2454 net_process_received_packet(net_rx_packets[0],
2455 dev->rx_req->length);
2456 packet_received = 0;
2458 rx_submit(dev, dev->rx_req, 0);
2460 error("dev->rx_req invalid");
2465 void usb_eth_halt(struct eth_device *netdev)
2467 struct eth_dev *dev = &l_ethdev;
2470 error("received NULL ptr");
2474 /* If the gadget not registered, simple return */
2479 * Some USB controllers may need additional deinitialization here
2480 * before dropping pull-up (also due to hardware issues).
2481 * For example: unhandled interrupt with status stage started may
2482 * bring the controller to fully broken state (until board reset).
2483 * There are some variants to debug and fix such cases:
2484 * 1) In the case of RNDIS connection eth_stop can perform additional
2485 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2486 * 2) 'pullup' callback in your UDC driver can be improved to perform
2487 * this deinitialization.
2491 usb_gadget_disconnect(dev->gadget);
2493 /* Clear pending interrupt */
2494 if (dev->network_started) {
2495 usb_gadget_handle_interrupts(0);
2496 dev->network_started = 0;
2499 usb_gadget_unregister_driver(ð_driver);
2500 board_usb_cleanup(0, USB_INIT_DEVICE);
2503 static struct usb_gadget_driver eth_driver = {
2507 .unbind = eth_unbind,
2510 .reset = eth_disconnect,
2511 .disconnect = eth_disconnect,
2513 .suspend = eth_suspend,
2514 .resume = eth_resume,
2517 int usb_eth_initialize(bd_t *bi)
2519 struct eth_device *netdev = &l_netdev;
2521 strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
2523 netdev->init = usb_eth_init;
2524 netdev->send = usb_eth_send;
2525 netdev->recv = usb_eth_recv;
2526 netdev->halt = usb_eth_halt;
2528 #ifdef CONFIG_MCAST_TFTP
2529 #error not supported
2531 eth_register(netdev);