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 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <asm/errno.h>
25 #include <linux/netdevice.h>
26 #include <linux/usb/ch9.h>
27 #include <usbdescriptors.h>
28 #include <linux/usb/cdc.h>
29 #include <linux/usb/gadget.h>
32 #include <linux/ctype.h>
34 #include "gadget_chips.h"
37 #define USB_NET_NAME "usb_ether"
40 extern struct platform_data brd;
42 #define spin_unlock(x)
45 unsigned packet_received, packet_sent;
47 #ifdef CONFIG_USB_GADGET_PXA2XX
48 # undef DEV_CONFIG_CDC
49 # define DEV_CONFIG_SUBSET 1
51 # define DEV_CONFIG_CDC 1
53 #define GFP_ATOMIC ((gfp_t) 0)
54 #define GFP_KERNEL ((gfp_t) 0)
57 * Ethernet gadget driver -- with CDC and non-CDC options
58 * Builds on hardware support for a full duplex link.
60 * CDC Ethernet is the standard USB solution for sending Ethernet frames
61 * using USB. Real hardware tends to use the same framing protocol but look
62 * different for control features. This driver strongly prefers to use
63 * this USB-IF standard as its open-systems interoperability solution;
64 * most host side USB stacks (except from Microsoft) support it.
66 * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
67 * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
68 * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
70 * There's some hardware that can't talk CDC ECM. We make that hardware
71 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
72 * link-level setup only requires activating the configuration. Only the
73 * endpoint descriptors, and product/vendor IDs, are relevant; no control
74 * operations are available. Linux supports it, but other host operating
75 * systems may not. (This is a subset of CDC Ethernet.)
77 * It turns out that if you add a few descriptors to that "CDC Subset",
78 * (Windows) host side drivers from MCCI can treat it as one submode of
79 * a proprietary scheme called "SAFE" ... without needing to know about
80 * specific product/vendor IDs. So we do that, making it easier to use
81 * those MS-Windows drivers. Those added descriptors make it resemble a
82 * CDC MDLM device, but they don't change device behavior at all. (See
83 * MCCI Engineering report 950198 "SAFE Networking Functions".)
85 * A third option is also in use. Rather than CDC Ethernet, or something
86 * simpler, Microsoft pushes their own approach: RNDIS. The published
87 * RNDIS specs are ambiguous and appear to be incomplete, and are also
88 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
90 #define ETH_ALEN 6 /* Octets in one ethernet addr */
91 #define ETH_HLEN 14 /* Total octets in header. */
92 #define ETH_ZLEN 60 /* Min. octets in frame sans FCS */
93 #define ETH_DATA_LEN 1500 /* Max. octets in payload */
94 #define ETH_FRAME_LEN PKTSIZE_ALIGN /* Max. octets in frame sans FCS */
95 #define ETH_FCS_LEN 4 /* Octets in the FCS */
97 #define DRIVER_DESC "Ethernet Gadget"
98 /* Based on linux 2.6.27 version */
99 #define DRIVER_VERSION "May Day 2005"
101 static const char shortname[] = "ether";
102 static const char driver_desc[] = DRIVER_DESC;
104 #define RX_EXTRA 20 /* guard against rx overflows */
106 #ifndef CONFIG_USB_ETH_RNDIS
107 #define rndis_uninit(x) do {} while (0)
108 #define rndis_deregister(c) do {} while (0)
109 #define rndis_exit() do {} while (0)
112 /* CDC and RNDIS support the same host-chosen outgoing packet filters. */
113 #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
114 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
115 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
116 |USB_CDC_PACKET_TYPE_DIRECTED)
118 #define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
120 /*-------------------------------------------------------------------------*/
123 struct usb_gadget *gadget;
124 struct usb_request *req; /* for control responses */
125 struct usb_request *stat_req; /* for cdc & rndis status */
128 struct usb_ep *in_ep, *out_ep, *status_ep;
129 const struct usb_endpoint_descriptor
132 struct usb_request *tx_req, *rx_req;
134 struct eth_device *net;
135 struct net_device_stats stats;
136 unsigned int tx_qlen;
141 unsigned suspended:1;
142 unsigned network_started:1;
146 #define WORK_RX_MEMORY 0
148 u8 host_mac[ETH_ALEN];
152 * This version autoconfigures as much as possible at run-time.
154 * It also ASSUMES a self-powered device, without remote wakeup,
155 * although remote wakeup support would make sense.
158 /*-------------------------------------------------------------------------*/
159 static struct eth_dev l_ethdev;
160 static struct eth_device l_netdev;
161 static struct usb_gadget_driver eth_driver;
163 /*-------------------------------------------------------------------------*/
165 /* "main" config is either CDC, or its simple subset */
166 static inline int is_cdc(struct eth_dev *dev)
168 #if !defined(DEV_CONFIG_SUBSET)
169 return 1; /* only cdc possible */
170 #elif !defined(DEV_CONFIG_CDC)
171 return 0; /* only subset possible */
173 return dev->cdc; /* depends on what hardware we found */
177 /* "secondary" RNDIS config may sometimes be activated */
178 static inline int rndis_active(struct eth_dev *dev)
180 #ifdef CONFIG_USB_ETH_RNDIS
187 #define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
188 #define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev))
190 #define DEFAULT_QLEN 2 /* double buffering by default */
192 /* peak bulk transfer bits-per-second */
193 #define HS_BPS (13 * 512 * 8 * 1000 * 8)
194 #define FS_BPS (19 * 64 * 1 * 1000 * 8)
196 #ifdef CONFIG_USB_GADGET_DUALSPEED
197 #define DEVSPEED USB_SPEED_HIGH
199 #ifdef CONFIG_USB_ETH_QMULT
200 #define qmult CONFIG_USB_ETH_QMULT
205 /* for dual-speed hardware, use deeper queues at highspeed */
206 #define qlen(gadget) \
207 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
209 static inline int BITRATE(struct usb_gadget *g)
211 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
214 #else /* full speed (low speed doesn't do bulk) */
218 #define DEVSPEED USB_SPEED_FULL
220 #define qlen(gadget) DEFAULT_QLEN
222 static inline int BITRATE(struct usb_gadget *g)
228 /*-------------------------------------------------------------------------*/
231 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
232 * Instead: allocate your own, using normal USB-IF procedures.
236 * Thanks to NetChip Technologies for donating this product ID.
237 * It's for devices with only CDC Ethernet configurations.
239 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
240 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
243 * For hardware that can't talk CDC, we use the same vendor ID that
244 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
245 * with pxa250. We're protocol-compatible, if the host-side drivers
246 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
247 * drivers that need to hard-wire endpoint numbers have a hook.
249 * The protocol is a minimal subset of CDC Ether, which works on any bulk
250 * hardware that's not deeply broken ... even on hardware that can't talk
251 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
252 * doesn't handle control-OUT).
254 #define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */
255 #define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */
258 * For hardware that can talk RNDIS and either of the above protocols,
259 * use this ID ... the windows INF files will know it. Unless it's
260 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
261 * the non-RNDIS configuration.
263 #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
264 #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
267 * Some systems will want different product identifers published in the
268 * device descriptor, either numbers or strings or both. These string
269 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
273 * Emulating them in eth_bind:
274 * static ushort idVendor;
275 * static ushort idProduct;
278 #if defined(CONFIG_USBNET_MANUFACTURER)
279 static char *iManufacturer = CONFIG_USBNET_MANUFACTURER;
281 static char *iManufacturer = "U-boot";
284 /* These probably need to be configurable. */
285 static ushort bcdDevice;
286 static char *iProduct;
287 static char *iSerialNumber;
289 static char dev_addr[18];
291 static char host_addr[18];
294 /*-------------------------------------------------------------------------*/
297 * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
298 * ep0 implementation: descriptors, config management, setup().
299 * also optional class-specific notification interrupt transfer.
303 * DESCRIPTORS ... most are static, but strings and (full) configuration
304 * descriptors are built on demand. For now we do either full CDC, or
305 * our simple subset, with RNDIS as an optional second configuration.
307 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
308 * the class descriptors match a modem (they're ignored; it's really just
309 * Ethernet functionality), they don't need the NOP altsetting, and the
310 * status transfer endpoint isn't optional.
313 #define STRING_MANUFACTURER 1
314 #define STRING_PRODUCT 2
315 #define STRING_ETHADDR 3
316 #define STRING_DATA 4
317 #define STRING_CONTROL 5
318 #define STRING_RNDIS_CONTROL 6
320 #define STRING_SUBSET 8
321 #define STRING_RNDIS 9
322 #define STRING_SERIALNUMBER 10
324 /* holds our biggest descriptor (or RNDIS response) */
325 #define USB_BUFSIZ 256
328 * This device advertises one configuration, eth_config, unless RNDIS
329 * is enabled (rndis_config) on hardware supporting at least two configs.
331 * NOTE: Controllers like superh_udc should probably be able to use
332 * an RNDIS-only configuration.
334 * FIXME define some higher-powered configurations to make it easier
335 * to recharge batteries ...
338 #define DEV_CONFIG_VALUE 1 /* cdc or subset */
339 #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
341 static struct usb_device_descriptor
343 .bLength = sizeof device_desc,
344 .bDescriptorType = USB_DT_DEVICE,
346 .bcdUSB = __constant_cpu_to_le16(0x0200),
348 .bDeviceClass = USB_CLASS_COMM,
349 .bDeviceSubClass = 0,
350 .bDeviceProtocol = 0,
352 .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
353 .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
354 .iManufacturer = STRING_MANUFACTURER,
355 .iProduct = STRING_PRODUCT,
356 .bNumConfigurations = 1,
359 static struct usb_otg_descriptor
361 .bLength = sizeof otg_descriptor,
362 .bDescriptorType = USB_DT_OTG,
364 .bmAttributes = USB_OTG_SRP,
367 static struct usb_config_descriptor
369 .bLength = sizeof eth_config,
370 .bDescriptorType = USB_DT_CONFIG,
372 /* compute wTotalLength on the fly */
374 .bConfigurationValue = DEV_CONFIG_VALUE,
375 .iConfiguration = STRING_CDC,
376 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
380 #ifdef CONFIG_USB_ETH_RNDIS
381 static struct usb_config_descriptor
383 .bLength = sizeof rndis_config,
384 .bDescriptorType = USB_DT_CONFIG,
386 /* compute wTotalLength on the fly */
388 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
389 .iConfiguration = STRING_RNDIS,
390 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
396 * Compared to the simple CDC subset, the full CDC Ethernet model adds
397 * three class descriptors, two interface descriptors, optional status
398 * endpoint. Both have a "data" interface and two bulk endpoints.
399 * There are also differences in how control requests are handled.
401 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
402 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
403 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
404 * work around those bugs) are unlikely to ever come from MSFT, you may
405 * wish to avoid using RNDIS.
407 * MCCI offers an alternative to RNDIS if you need to connect to Windows
408 * but have hardware that can't support CDC Ethernet. We add descriptors
409 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
410 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
411 * get those drivers from MCCI, or bundled with various products.
414 #ifdef DEV_CONFIG_CDC
415 static struct usb_interface_descriptor
417 .bLength = sizeof control_intf,
418 .bDescriptorType = USB_DT_INTERFACE,
420 .bInterfaceNumber = 0,
421 /* status endpoint is optional; this may be patched later */
423 .bInterfaceClass = USB_CLASS_COMM,
424 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
425 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
426 .iInterface = STRING_CONTROL,
430 #ifdef CONFIG_USB_ETH_RNDIS
431 static const struct usb_interface_descriptor
432 rndis_control_intf = {
433 .bLength = sizeof rndis_control_intf,
434 .bDescriptorType = USB_DT_INTERFACE,
436 .bInterfaceNumber = 0,
438 .bInterfaceClass = USB_CLASS_COMM,
439 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
440 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
441 .iInterface = STRING_RNDIS_CONTROL,
445 static const struct usb_cdc_header_desc header_desc = {
446 .bLength = sizeof header_desc,
447 .bDescriptorType = USB_DT_CS_INTERFACE,
448 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
450 .bcdCDC = __constant_cpu_to_le16(0x0110),
453 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
455 static const struct usb_cdc_union_desc union_desc = {
456 .bLength = sizeof union_desc,
457 .bDescriptorType = USB_DT_CS_INTERFACE,
458 .bDescriptorSubType = USB_CDC_UNION_TYPE,
460 .bMasterInterface0 = 0, /* index of control interface */
461 .bSlaveInterface0 = 1, /* index of DATA interface */
464 #endif /* CDC || RNDIS */
466 #ifdef CONFIG_USB_ETH_RNDIS
468 static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
469 .bLength = sizeof call_mgmt_descriptor,
470 .bDescriptorType = USB_DT_CS_INTERFACE,
471 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
473 .bmCapabilities = 0x00,
474 .bDataInterface = 0x01,
477 static const struct usb_cdc_acm_descriptor acm_descriptor = {
478 .bLength = sizeof acm_descriptor,
479 .bDescriptorType = USB_DT_CS_INTERFACE,
480 .bDescriptorSubType = USB_CDC_ACM_TYPE,
482 .bmCapabilities = 0x00,
487 #ifndef DEV_CONFIG_CDC
490 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
491 * ways: data endpoints live in the control interface, there's no data
492 * interface, and it's not used to talk to a cell phone radio.
495 static const struct usb_cdc_mdlm_desc mdlm_desc = {
496 .bLength = sizeof mdlm_desc,
497 .bDescriptorType = USB_DT_CS_INTERFACE,
498 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
500 .bcdVersion = __constant_cpu_to_le16(0x0100),
502 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
503 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
508 * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
509 * can't really use its struct. All we do here is say that we're using
510 * the submode of "SAFE" which directly matches the CDC Subset.
512 static const u8 mdlm_detail_desc[] = {
515 USB_CDC_MDLM_DETAIL_TYPE,
518 0, /* network control capabilities (none) */
519 0, /* network data capabilities ("raw" encapsulation) */
524 static const struct usb_cdc_ether_desc ether_desc = {
525 .bLength = sizeof(ether_desc),
526 .bDescriptorType = USB_DT_CS_INTERFACE,
527 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
529 /* this descriptor actually adds value, surprise! */
530 .iMACAddress = STRING_ETHADDR,
531 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
532 .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN),
533 .wNumberMCFilters = __constant_cpu_to_le16(0),
534 .bNumberPowerFilters = 0,
537 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
540 * include the status endpoint if we can, even where it's optional.
541 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
542 * packet, to simplify cancellation; and a big transfer interval, to
543 * waste less bandwidth.
545 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
546 * if they ignore the connect/disconnect notifications that real aether
547 * can provide. more advanced cdc configurations might want to support
548 * encapsulated commands (vendor-specific, using control-OUT).
550 * RNDIS requires the status endpoint, since it uses that encapsulation
551 * mechanism for its funky RPC scheme.
554 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
555 #define STATUS_BYTECOUNT 16 /* 8 byte header + data */
557 static struct usb_endpoint_descriptor
559 .bLength = USB_DT_ENDPOINT_SIZE,
560 .bDescriptorType = USB_DT_ENDPOINT,
562 .bEndpointAddress = USB_DIR_IN,
563 .bmAttributes = USB_ENDPOINT_XFER_INT,
564 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
565 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
569 #ifdef DEV_CONFIG_CDC
571 /* the default data interface has no endpoints ... */
573 static const struct usb_interface_descriptor
575 .bLength = sizeof data_nop_intf,
576 .bDescriptorType = USB_DT_INTERFACE,
578 .bInterfaceNumber = 1,
579 .bAlternateSetting = 0,
581 .bInterfaceClass = USB_CLASS_CDC_DATA,
582 .bInterfaceSubClass = 0,
583 .bInterfaceProtocol = 0,
586 /* ... but the "real" data interface has two bulk endpoints */
588 static const struct usb_interface_descriptor
590 .bLength = sizeof data_intf,
591 .bDescriptorType = USB_DT_INTERFACE,
593 .bInterfaceNumber = 1,
594 .bAlternateSetting = 1,
596 .bInterfaceClass = USB_CLASS_CDC_DATA,
597 .bInterfaceSubClass = 0,
598 .bInterfaceProtocol = 0,
599 .iInterface = STRING_DATA,
604 #ifdef CONFIG_USB_ETH_RNDIS
606 /* RNDIS doesn't activate by changing to the "real" altsetting */
608 static const struct usb_interface_descriptor
610 .bLength = sizeof rndis_data_intf,
611 .bDescriptorType = USB_DT_INTERFACE,
613 .bInterfaceNumber = 1,
614 .bAlternateSetting = 0,
616 .bInterfaceClass = USB_CLASS_CDC_DATA,
617 .bInterfaceSubClass = 0,
618 .bInterfaceProtocol = 0,
619 .iInterface = STRING_DATA,
624 #ifdef DEV_CONFIG_SUBSET
627 * "Simple" CDC-subset option is a simple vendor-neutral model that most
628 * full speed controllers can handle: one interface, two bulk endpoints.
630 * To assist host side drivers, we fancy it up a bit, and add descriptors
631 * so some host side drivers will understand it as a "SAFE" variant.
634 static const struct usb_interface_descriptor
636 .bLength = sizeof subset_data_intf,
637 .bDescriptorType = USB_DT_INTERFACE,
639 .bInterfaceNumber = 0,
640 .bAlternateSetting = 0,
642 .bInterfaceClass = USB_CLASS_COMM,
643 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
644 .bInterfaceProtocol = 0,
645 .iInterface = STRING_DATA,
650 static struct usb_endpoint_descriptor
652 .bLength = USB_DT_ENDPOINT_SIZE,
653 .bDescriptorType = USB_DT_ENDPOINT,
655 .bEndpointAddress = USB_DIR_IN,
656 .bmAttributes = USB_ENDPOINT_XFER_BULK,
659 static struct usb_endpoint_descriptor
661 .bLength = USB_DT_ENDPOINT_SIZE,
662 .bDescriptorType = USB_DT_ENDPOINT,
664 .bEndpointAddress = USB_DIR_OUT,
665 .bmAttributes = USB_ENDPOINT_XFER_BULK,
668 static const struct usb_descriptor_header *fs_eth_function[11] = {
669 (struct usb_descriptor_header *) &otg_descriptor,
670 #ifdef DEV_CONFIG_CDC
671 /* "cdc" mode descriptors */
672 (struct usb_descriptor_header *) &control_intf,
673 (struct usb_descriptor_header *) &header_desc,
674 (struct usb_descriptor_header *) &union_desc,
675 (struct usb_descriptor_header *) ðer_desc,
676 /* NOTE: status endpoint may need to be removed */
677 (struct usb_descriptor_header *) &fs_status_desc,
678 /* data interface, with altsetting */
679 (struct usb_descriptor_header *) &data_nop_intf,
680 (struct usb_descriptor_header *) &data_intf,
681 (struct usb_descriptor_header *) &fs_source_desc,
682 (struct usb_descriptor_header *) &fs_sink_desc,
684 #endif /* DEV_CONFIG_CDC */
687 static inline void fs_subset_descriptors(void)
689 #ifdef DEV_CONFIG_SUBSET
690 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
691 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
692 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
693 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
694 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
695 fs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
696 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
697 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
698 fs_eth_function[8] = NULL;
700 fs_eth_function[1] = NULL;
704 #ifdef CONFIG_USB_ETH_RNDIS
705 static const struct usb_descriptor_header *fs_rndis_function[] = {
706 (struct usb_descriptor_header *) &otg_descriptor,
707 /* control interface matches ACM, not Ethernet */
708 (struct usb_descriptor_header *) &rndis_control_intf,
709 (struct usb_descriptor_header *) &header_desc,
710 (struct usb_descriptor_header *) &call_mgmt_descriptor,
711 (struct usb_descriptor_header *) &acm_descriptor,
712 (struct usb_descriptor_header *) &union_desc,
713 (struct usb_descriptor_header *) &fs_status_desc,
714 /* data interface has no altsetting */
715 (struct usb_descriptor_header *) &rndis_data_intf,
716 (struct usb_descriptor_header *) &fs_source_desc,
717 (struct usb_descriptor_header *) &fs_sink_desc,
723 * usb 2.0 devices need to expose both high speed and full speed
724 * descriptors, unless they only run at full speed.
727 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
728 static struct usb_endpoint_descriptor
730 .bLength = USB_DT_ENDPOINT_SIZE,
731 .bDescriptorType = USB_DT_ENDPOINT,
733 .bmAttributes = USB_ENDPOINT_XFER_INT,
734 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
735 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
737 #endif /* DEV_CONFIG_CDC */
739 static struct usb_endpoint_descriptor
741 .bLength = USB_DT_ENDPOINT_SIZE,
742 .bDescriptorType = USB_DT_ENDPOINT,
744 .bmAttributes = USB_ENDPOINT_XFER_BULK,
745 .wMaxPacketSize = __constant_cpu_to_le16(512),
748 static struct usb_endpoint_descriptor
750 .bLength = USB_DT_ENDPOINT_SIZE,
751 .bDescriptorType = USB_DT_ENDPOINT,
753 .bmAttributes = USB_ENDPOINT_XFER_BULK,
754 .wMaxPacketSize = __constant_cpu_to_le16(512),
757 static struct usb_qualifier_descriptor
759 .bLength = sizeof dev_qualifier,
760 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
762 .bcdUSB = __constant_cpu_to_le16(0x0200),
763 .bDeviceClass = USB_CLASS_COMM,
765 .bNumConfigurations = 1,
768 static const struct usb_descriptor_header *hs_eth_function[11] = {
769 (struct usb_descriptor_header *) &otg_descriptor,
770 #ifdef DEV_CONFIG_CDC
771 /* "cdc" mode descriptors */
772 (struct usb_descriptor_header *) &control_intf,
773 (struct usb_descriptor_header *) &header_desc,
774 (struct usb_descriptor_header *) &union_desc,
775 (struct usb_descriptor_header *) ðer_desc,
776 /* NOTE: status endpoint may need to be removed */
777 (struct usb_descriptor_header *) &hs_status_desc,
778 /* data interface, with altsetting */
779 (struct usb_descriptor_header *) &data_nop_intf,
780 (struct usb_descriptor_header *) &data_intf,
781 (struct usb_descriptor_header *) &hs_source_desc,
782 (struct usb_descriptor_header *) &hs_sink_desc,
784 #endif /* DEV_CONFIG_CDC */
787 static inline void hs_subset_descriptors(void)
789 #ifdef DEV_CONFIG_SUBSET
790 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
791 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
792 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
793 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
794 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
795 hs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
796 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
797 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
798 hs_eth_function[8] = NULL;
800 hs_eth_function[1] = NULL;
804 #ifdef CONFIG_USB_ETH_RNDIS
805 static const struct usb_descriptor_header *hs_rndis_function[] = {
806 (struct usb_descriptor_header *) &otg_descriptor,
807 /* control interface matches ACM, not Ethernet */
808 (struct usb_descriptor_header *) &rndis_control_intf,
809 (struct usb_descriptor_header *) &header_desc,
810 (struct usb_descriptor_header *) &call_mgmt_descriptor,
811 (struct usb_descriptor_header *) &acm_descriptor,
812 (struct usb_descriptor_header *) &union_desc,
813 (struct usb_descriptor_header *) &hs_status_desc,
814 /* data interface has no altsetting */
815 (struct usb_descriptor_header *) &rndis_data_intf,
816 (struct usb_descriptor_header *) &hs_source_desc,
817 (struct usb_descriptor_header *) &hs_sink_desc,
823 /* maxpacket and other transfer characteristics vary by speed. */
824 static inline struct usb_endpoint_descriptor *
825 ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
826 struct usb_endpoint_descriptor *fs)
828 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
833 /*-------------------------------------------------------------------------*/
835 /* descriptors that are built on-demand */
837 static char manufacturer[50];
838 static char product_desc[40] = DRIVER_DESC;
839 static char serial_number[20];
841 /* address that the host will use ... usually assigned at random */
842 static char ethaddr[2 * ETH_ALEN + 1];
844 /* static strings, in UTF-8 */
845 static struct usb_string strings[] = {
846 { STRING_MANUFACTURER, manufacturer, },
847 { STRING_PRODUCT, product_desc, },
848 { STRING_SERIALNUMBER, serial_number, },
849 { STRING_DATA, "Ethernet Data", },
850 { STRING_ETHADDR, ethaddr, },
851 #ifdef DEV_CONFIG_CDC
852 { STRING_CDC, "CDC Ethernet", },
853 { STRING_CONTROL, "CDC Communications Control", },
855 #ifdef DEV_CONFIG_SUBSET
856 { STRING_SUBSET, "CDC Ethernet Subset", },
858 #ifdef CONFIG_USB_ETH_RNDIS
859 { STRING_RNDIS, "RNDIS", },
860 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
862 { } /* end of list */
865 static struct usb_gadget_strings stringtab = {
866 .language = 0x0409, /* en-us */
870 /*============================================================================*/
871 static u8 control_req[USB_BUFSIZ];
872 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
873 static u8 status_req[STATUS_BYTECOUNT] __attribute__ ((aligned(4)));
878 * strlcpy - Copy a %NUL terminated string into a sized buffer
879 * @dest: Where to copy the string to
880 * @src: Where to copy the string from
881 * @size: size of destination buffer
883 * Compatible with *BSD: the result is always a valid
884 * NUL-terminated string that fits in the buffer (unless,
885 * of course, the buffer size is zero). It does not pad
886 * out the result like strncpy() does.
888 size_t strlcpy(char *dest, const char *src, size_t size)
890 size_t ret = strlen(src);
893 size_t len = (ret >= size) ? size - 1 : ret;
894 memcpy(dest, src, len);
900 /*============================================================================*/
903 * one config, two interfaces: control, data.
904 * complications: class descriptors, and an altsetting.
907 config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
910 const struct usb_config_descriptor *config;
911 const struct usb_descriptor_header **function;
914 if (gadget_is_dualspeed(g)) {
915 hs = (g->speed == USB_SPEED_HIGH);
916 if (type == USB_DT_OTHER_SPEED_CONFIG)
919 #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
921 if (index >= device_desc.bNumConfigurations)
924 #ifdef CONFIG_USB_ETH_RNDIS
926 * list the RNDIS config first, to make Microsoft's drivers
927 * happy. DOCSIS 1.0 needs this too.
929 if (device_desc.bNumConfigurations == 2 && index == 0) {
930 config = &rndis_config;
931 function = which_fn(rndis);
935 config = ð_config;
936 function = which_fn(eth);
939 /* for now, don't advertise srp-only devices */
943 len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
946 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
950 /*-------------------------------------------------------------------------*/
952 static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
953 static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
956 set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
959 struct usb_gadget *gadget = dev->gadget;
961 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
962 /* status endpoint used for RNDIS and (optionally) CDC */
963 if (!subset_active(dev) && dev->status_ep) {
964 dev->status = ep_desc(gadget, &hs_status_desc,
966 dev->status_ep->driver_data = dev;
968 result = usb_ep_enable(dev->status_ep, dev->status);
970 debug("enable %s --> %d\n",
971 dev->status_ep->name, result);
977 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
978 dev->in_ep->driver_data = dev;
980 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
981 dev->out_ep->driver_data = dev;
984 * With CDC, the host isn't allowed to use these two data
985 * endpoints in the default altsetting for the interface.
986 * so we don't activate them yet. Reset from SET_INTERFACE.
988 * Strictly speaking RNDIS should work the same: activation is
989 * a side effect of setting a packet filter. Deactivation is
990 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
992 if (!cdc_active(dev)) {
993 result = usb_ep_enable(dev->in_ep, dev->in);
995 debug("enable %s --> %d\n",
996 dev->in_ep->name, result);
1000 result = usb_ep_enable(dev->out_ep, dev->out);
1002 debug("enable %s --> %d\n",
1003 dev->out_ep->name, result);
1010 result = alloc_requests(dev, qlen(gadget), gfp_flags);
1012 /* on error, disable any endpoints */
1014 if (!subset_active(dev) && dev->status_ep)
1015 (void) usb_ep_disable(dev->status_ep);
1017 (void) usb_ep_disable(dev->in_ep);
1018 (void) usb_ep_disable(dev->out_ep);
1021 } else if (!cdc_active(dev)) {
1023 * activate non-CDC configs right away
1024 * this isn't strictly according to the RNDIS spec
1026 eth_start(dev, GFP_ATOMIC);
1029 /* caller is responsible for cleanup on error */
1033 static void eth_reset_config(struct eth_dev *dev)
1035 if (dev->config == 0)
1038 debug("%s\n", __func__);
1040 rndis_uninit(dev->rndis_config);
1043 * disable endpoints, forcing (synchronous) completion of
1044 * pending i/o. then free the requests.
1048 usb_ep_disable(dev->in_ep);
1050 usb_ep_free_request(dev->in_ep, dev->tx_req);
1055 usb_ep_disable(dev->out_ep);
1057 usb_ep_free_request(dev->out_ep, dev->rx_req);
1062 usb_ep_disable(dev->status_ep);
1065 dev->cdc_filter = 0;
1070 * change our operational config. must agree with the code
1071 * that returns config descriptors, and altsetting code.
1073 static int eth_set_config(struct eth_dev *dev, unsigned number,
1077 struct usb_gadget *gadget = dev->gadget;
1079 if (gadget_is_sa1100(gadget)
1081 && dev->tx_qlen != 0) {
1082 /* tx fifo is full, but we can't clear it...*/
1083 error("can't change configurations");
1086 eth_reset_config(dev);
1089 case DEV_CONFIG_VALUE:
1090 result = set_ether_config(dev, gfp_flags);
1092 #ifdef CONFIG_USB_ETH_RNDIS
1093 case DEV_RNDIS_CONFIG_VALUE:
1095 result = set_ether_config(dev, gfp_flags);
1107 eth_reset_config(dev);
1108 usb_gadget_vbus_draw(dev->gadget,
1109 gadget_is_otg(dev->gadget) ? 8 : 100);
1114 power = 2 * eth_config.bMaxPower;
1115 usb_gadget_vbus_draw(dev->gadget, power);
1117 switch (gadget->speed) {
1118 case USB_SPEED_FULL:
1119 speed = "full"; break;
1120 #ifdef CONFIG_USB_GADGET_DUALSPEED
1121 case USB_SPEED_HIGH:
1122 speed = "high"; break;
1128 dev->config = number;
1129 printf("%s speed config #%d: %d mA, %s, using %s\n",
1130 speed, number, power, driver_desc,
1135 : "CDC Ethernet Subset"));
1140 /*-------------------------------------------------------------------------*/
1142 #ifdef DEV_CONFIG_CDC
1145 * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
1146 * only to notify the host about link status changes (which we support) or
1147 * report completion of some encapsulated command (as used in RNDIS). Since
1148 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1149 * command mechanism; and only one status request is ever queued.
1151 static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
1153 struct usb_cdc_notification *event = req->buf;
1154 int value = req->status;
1155 struct eth_dev *dev = ep->driver_data;
1157 /* issue the second notification if host reads the first */
1158 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1160 __le32 *data = req->buf + sizeof *event;
1162 event->bmRequestType = 0xA1;
1163 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
1164 event->wValue = __constant_cpu_to_le16(0);
1165 event->wIndex = __constant_cpu_to_le16(1);
1166 event->wLength = __constant_cpu_to_le16(8);
1168 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1169 data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
1171 req->length = STATUS_BYTECOUNT;
1172 value = usb_ep_queue(ep, req, GFP_ATOMIC);
1173 debug("send SPEED_CHANGE --> %d\n", value);
1176 } else if (value != -ECONNRESET) {
1177 debug("event %02x --> %d\n",
1178 event->bNotificationType, value);
1179 if (event->bNotificationType ==
1180 USB_CDC_NOTIFY_SPEED_CHANGE) {
1181 l_ethdev.network_started = 1;
1182 printf("USB network up!\n");
1185 req->context = NULL;
1188 static void issue_start_status(struct eth_dev *dev)
1190 struct usb_request *req = dev->stat_req;
1191 struct usb_cdc_notification *event;
1197 * FIXME ugly idiom, maybe we'd be better with just
1198 * a "cancel the whole queue" primitive since any
1199 * unlink-one primitive has way too many error modes.
1200 * here, we "know" toggle is already clear...
1202 * FIXME iff req->context != null just dequeue it
1204 usb_ep_disable(dev->status_ep);
1205 usb_ep_enable(dev->status_ep, dev->status);
1208 * 3.8.1 says to issue first NETWORK_CONNECTION, then
1209 * a SPEED_CHANGE. could be useful in some configs.
1212 event->bmRequestType = 0xA1;
1213 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
1214 event->wValue = __constant_cpu_to_le16(1); /* connected */
1215 event->wIndex = __constant_cpu_to_le16(1);
1218 req->length = sizeof *event;
1219 req->complete = eth_status_complete;
1222 value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
1224 debug("status buf queue --> %d\n", value);
1229 /*-------------------------------------------------------------------------*/
1231 static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
1233 if (req->status || req->actual != req->length)
1234 debug("setup complete --> %d, %d/%d\n",
1235 req->status, req->actual, req->length);
1238 #ifdef CONFIG_USB_ETH_RNDIS
1240 static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
1242 if (req->status || req->actual != req->length)
1243 debug("rndis response complete --> %d, %d/%d\n",
1244 req->status, req->actual, req->length);
1246 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1249 static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
1251 struct eth_dev *dev = ep->driver_data;
1254 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1255 status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
1257 error("%s: rndis parse error %d", __func__, status);
1263 * The setup() callback implements all the ep0 functionality that's not
1264 * handled lower down. CDC has a number of less-common features:
1266 * - two interfaces: control, and ethernet data
1267 * - Ethernet data interface has two altsettings: default, and active
1268 * - class-specific descriptors for the control interface
1269 * - class-specific control requests
1272 eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1274 struct eth_dev *dev = get_gadget_data(gadget);
1275 struct usb_request *req = dev->req;
1276 int value = -EOPNOTSUPP;
1277 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1278 u16 wValue = le16_to_cpu(ctrl->wValue);
1279 u16 wLength = le16_to_cpu(ctrl->wLength);
1282 * descriptors just go into the pre-allocated ep0 buffer,
1283 * while config change events may enable network traffic.
1286 debug("%s\n", __func__);
1288 req->complete = eth_setup_complete;
1289 switch (ctrl->bRequest) {
1291 case USB_REQ_GET_DESCRIPTOR:
1292 if (ctrl->bRequestType != USB_DIR_IN)
1294 switch (wValue >> 8) {
1297 value = min(wLength, (u16) sizeof device_desc);
1298 memcpy(req->buf, &device_desc, value);
1300 case USB_DT_DEVICE_QUALIFIER:
1301 if (!gadget_is_dualspeed(gadget))
1303 value = min(wLength, (u16) sizeof dev_qualifier);
1304 memcpy(req->buf, &dev_qualifier, value);
1307 case USB_DT_OTHER_SPEED_CONFIG:
1308 if (!gadget_is_dualspeed(gadget))
1312 value = config_buf(gadget, req->buf,
1315 gadget_is_otg(gadget));
1317 value = min(wLength, (u16) value);
1321 value = usb_gadget_get_string(&stringtab,
1322 wValue & 0xff, req->buf);
1325 value = min(wLength, (u16) value);
1331 case USB_REQ_SET_CONFIGURATION:
1332 if (ctrl->bRequestType != 0)
1334 if (gadget->a_hnp_support)
1335 debug("HNP available\n");
1336 else if (gadget->a_alt_hnp_support)
1337 debug("HNP needs a different root port\n");
1338 value = eth_set_config(dev, wValue, GFP_ATOMIC);
1340 case USB_REQ_GET_CONFIGURATION:
1341 if (ctrl->bRequestType != USB_DIR_IN)
1343 *(u8 *)req->buf = dev->config;
1344 value = min(wLength, (u16) 1);
1347 case USB_REQ_SET_INTERFACE:
1348 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1352 if (!cdc_active(dev) && wIndex != 0)
1356 * PXA hardware partially handles SET_INTERFACE;
1357 * we need to kluge around that interference.
1359 if (gadget_is_pxa(gadget)) {
1360 value = eth_set_config(dev, DEV_CONFIG_VALUE,
1363 * PXA25x driver use non-CDC ethernet gadget.
1364 * But only _CDC and _RNDIS code can signalize
1365 * that network is working. So we signalize it
1368 l_ethdev.network_started = 1;
1369 debug("USB network up!\n");
1373 #ifdef DEV_CONFIG_CDC
1375 case 0: /* control/master intf */
1379 usb_ep_disable(dev->status_ep);
1380 usb_ep_enable(dev->status_ep, dev->status);
1385 case 1: /* data intf */
1388 usb_ep_disable(dev->in_ep);
1389 usb_ep_disable(dev->out_ep);
1392 * CDC requires the data transfers not be done from
1393 * the default interface setting ... also, setting
1394 * the non-default interface resets filters etc.
1397 if (!cdc_active(dev))
1399 usb_ep_enable(dev->in_ep, dev->in);
1400 usb_ep_enable(dev->out_ep, dev->out);
1401 dev->cdc_filter = DEFAULT_FILTER;
1403 issue_start_status(dev);
1404 eth_start(dev, GFP_ATOMIC);
1411 * FIXME this is wrong, as is the assumption that
1412 * all non-PXA hardware talks real CDC ...
1414 debug("set_interface ignored!\n");
1415 #endif /* DEV_CONFIG_CDC */
1419 case USB_REQ_GET_INTERFACE:
1420 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1424 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
1427 /* for CDC, iff carrier is on, data interface is active. */
1428 if (rndis_active(dev) || wIndex != 1)
1429 *(u8 *)req->buf = 0;
1431 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1432 /* carrier always ok ...*/
1433 *(u8 *)req->buf = 1 ;
1435 value = min(wLength, (u16) 1);
1438 #ifdef DEV_CONFIG_CDC
1439 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
1441 * see 6.2.30: no data, wIndex = interface,
1442 * wValue = packet filter bitmap
1444 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1449 debug("packet filter %02x\n", wValue);
1450 dev->cdc_filter = wValue;
1456 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1457 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1458 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1459 * case USB_CDC_GET_ETHERNET_STATISTIC:
1462 #endif /* DEV_CONFIG_CDC */
1464 #ifdef CONFIG_USB_ETH_RNDIS
1466 * RNDIS uses the CDC command encapsulation mechanism to implement
1467 * an RPC scheme, with much getting/setting of attributes by OID.
1469 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1470 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1471 || !rndis_active(dev)
1472 || wLength > USB_BUFSIZ
1474 || rndis_control_intf.bInterfaceNumber
1477 /* read the request, then process it */
1479 req->complete = rndis_command_complete;
1480 /* later, rndis_control_ack () sends a notification */
1483 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1484 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1485 == ctrl->bRequestType
1486 && rndis_active(dev)
1487 /* && wLength >= 0x0400 */
1489 && rndis_control_intf.bInterfaceNumber
1494 /* return the result */
1495 buf = rndis_get_next_response(dev->rndis_config, &n);
1497 memcpy(req->buf, buf, n);
1498 req->complete = rndis_response_complete;
1499 rndis_free_response(dev->rndis_config, buf);
1502 /* else stalls ... spec says to avoid that */
1508 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
1509 ctrl->bRequestType, ctrl->bRequest,
1510 wValue, wIndex, wLength);
1513 /* respond with data transfer before status phase? */
1515 debug("respond with data transfer before status phase\n");
1516 req->length = value;
1517 req->zero = value < wLength
1518 && (value % gadget->ep0->maxpacket) == 0;
1519 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1521 debug("ep_queue --> %d\n", value);
1523 eth_setup_complete(gadget->ep0, req);
1527 /* host either stalls (value < 0) or reports success */
1531 /*-------------------------------------------------------------------------*/
1533 static void rx_complete(struct usb_ep *ep, struct usb_request *req);
1535 static int rx_submit(struct eth_dev *dev, struct usb_request *req,
1538 int retval = -ENOMEM;
1542 * Padding up to RX_EXTRA handles minor disagreements with host.
1543 * Normally we use the USB "terminate on short read" convention;
1544 * so allow up to (N*maxpacket), since that memory is normally
1545 * already allocated. Some hardware doesn't deal well with short
1546 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1547 * byte off the end (to force hardware errors on overflow).
1549 * RNDIS uses internal framing, and explicitly allows senders to
1550 * pad to end-of-packet. That's potentially nice for speed,
1551 * but means receivers can't recover synch on their own.
1554 debug("%s\n", __func__);
1556 size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1557 size += dev->out_ep->maxpacket - 1;
1558 if (rndis_active(dev))
1559 size += sizeof(struct rndis_packet_msg_type);
1560 size -= size % dev->out_ep->maxpacket;
1563 * Some platforms perform better when IP packets are aligned,
1564 * but on at least one, checksumming fails otherwise. Note:
1565 * RNDIS headers involve variable numbers of LE32 values.
1568 req->buf = (u8 *) NetRxPackets[0];
1570 req->complete = rx_complete;
1572 retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
1575 error("rx submit --> %d", retval);
1580 static void rx_complete(struct usb_ep *ep, struct usb_request *req)
1582 struct eth_dev *dev = ep->driver_data;
1584 debug("%s: status %d\n", __func__, req->status);
1585 switch (req->status) {
1586 /* normal completion */
1588 if (rndis_active(dev)) {
1589 /* we know MaxPacketsPerTransfer == 1 here */
1590 int length = rndis_rm_hdr(req->buf, req->actual);
1593 req->length -= length;
1594 req->actual -= length;
1596 if (req->actual < ETH_HLEN || ETH_FRAME_LEN < req->actual) {
1598 dev->stats.rx_errors++;
1599 dev->stats.rx_length_errors++;
1600 debug("rx length %d\n", req->length);
1604 dev->stats.rx_packets++;
1605 dev->stats.rx_bytes += req->length;
1608 /* software-driven interface shutdown */
1609 case -ECONNRESET: /* unlink */
1610 case -ESHUTDOWN: /* disconnect etc */
1611 /* for hardware automagic (such as pxa) */
1612 case -ECONNABORTED: /* endpoint reset */
1617 dev->stats.rx_over_errors++;
1620 dev->stats.rx_errors++;
1624 packet_received = 1;
1627 static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
1630 dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
1635 dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
1643 usb_ep_free_request(dev->in_ep, dev->tx_req);
1645 error("can't alloc requests");
1649 static void tx_complete(struct usb_ep *ep, struct usb_request *req)
1651 struct eth_dev *dev = ep->driver_data;
1653 debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
1654 switch (req->status) {
1656 dev->stats.tx_errors++;
1657 debug("tx err %d\n", req->status);
1659 case -ECONNRESET: /* unlink */
1660 case -ESHUTDOWN: /* disconnect etc */
1663 dev->stats.tx_bytes += req->length;
1665 dev->stats.tx_packets++;
1670 static inline int eth_is_promisc(struct eth_dev *dev)
1672 /* no filters for the CDC subset; always promisc */
1673 if (subset_active(dev))
1675 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1679 static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1681 struct eth_dev *dev = netdev_priv(net);
1682 int length = skb->len;
1684 struct usb_request *req = NULL;
1685 unsigned long flags;
1687 /* apply outgoing CDC or RNDIS filters */
1688 if (!eth_is_promisc (dev)) {
1689 u8 *dest = skb->data;
1691 if (is_multicast_ether_addr(dest)) {
1694 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1695 * SET_ETHERNET_MULTICAST_FILTERS requests
1697 if (is_broadcast_ether_addr(dest))
1698 type = USB_CDC_PACKET_TYPE_BROADCAST;
1700 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1701 if (!(dev->cdc_filter & type)) {
1702 dev_kfree_skb_any (skb);
1706 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1709 spin_lock_irqsave(&dev->req_lock, flags);
1711 * this freelist can be empty if an interrupt triggered disconnect()
1712 * and reconfigured the gadget (shutting down this queue) after the
1713 * network stack decided to xmit but before we got the spinlock.
1715 if (list_empty(&dev->tx_reqs)) {
1716 spin_unlock_irqrestore(&dev->req_lock, flags);
1720 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1721 list_del (&req->list);
1723 /* temporarily stop TX queue when the freelist empties */
1724 if (list_empty (&dev->tx_reqs))
1725 netif_stop_queue (net);
1726 spin_unlock_irqrestore(&dev->req_lock, flags);
1728 /* no buffer copies needed, unless the network stack did it
1729 * or the hardware can't use skb buffers.
1730 * or there's not enough space for any RNDIS headers we need
1732 if (rndis_active(dev)) {
1733 struct sk_buff *skb_rndis;
1735 skb_rndis = skb_realloc_headroom (skb,
1736 sizeof (struct rndis_packet_msg_type));
1740 dev_kfree_skb_any (skb);
1742 rndis_add_hdr (skb);
1745 req->buf = skb->data;
1747 req->complete = tx_complete;
1749 /* use zlp framing on tx for strict CDC-Ether conformance,
1750 * though any robust network rx path ignores extra padding.
1751 * and some hardware doesn't like to write zlps.
1754 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1757 req->length = length;
1759 /* throttle highspeed IRQ rate back slightly */
1760 if (gadget_is_dualspeed(dev->gadget))
1761 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1762 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1765 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1768 DEBUG (dev, "tx queue err %d\n", retval);
1771 net->trans_start = jiffies;
1772 atomic_inc (&dev->tx_qlen);
1777 dev->stats.tx_dropped++;
1778 dev_kfree_skb_any (skb);
1779 spin_lock_irqsave(&dev->req_lock, flags);
1780 if (list_empty (&dev->tx_reqs))
1781 netif_start_queue (net);
1782 list_add (&req->list, &dev->tx_reqs);
1783 spin_unlock_irqrestore(&dev->req_lock, flags);
1788 /*-------------------------------------------------------------------------*/
1791 static void eth_unbind(struct usb_gadget *gadget)
1793 struct eth_dev *dev = get_gadget_data(gadget);
1795 debug("%s...\n", __func__);
1796 rndis_deregister(dev->rndis_config);
1799 /* we've already been disconnected ... no i/o is active */
1801 usb_ep_free_request(gadget->ep0, dev->req);
1804 if (dev->stat_req) {
1805 usb_ep_free_request(dev->status_ep, dev->stat_req);
1806 dev->stat_req = NULL;
1810 usb_ep_free_request(dev->in_ep, dev->tx_req);
1815 usb_ep_free_request(dev->out_ep, dev->rx_req);
1819 /* unregister_netdev (dev->net);*/
1820 /* free_netdev(dev->net);*/
1823 set_gadget_data(gadget, NULL);
1826 static void eth_disconnect(struct usb_gadget *gadget)
1828 eth_reset_config(get_gadget_data(gadget));
1829 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1832 static void eth_suspend(struct usb_gadget *gadget)
1837 static void eth_resume(struct usb_gadget *gadget)
1842 /*-------------------------------------------------------------------------*/
1844 #ifdef CONFIG_USB_ETH_RNDIS
1847 * The interrupt endpoint is used in RNDIS to notify the host when messages
1848 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1849 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1850 * REMOTE_NDIS_KEEPALIVE_MSG.
1852 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1853 * normally just one notification will be queued.
1856 static void rndis_control_ack_complete(struct usb_ep *ep,
1857 struct usb_request *req)
1859 struct eth_dev *dev = ep->driver_data;
1861 debug("%s...\n", __func__);
1862 if (req->status || req->actual != req->length)
1863 debug("rndis control ack complete --> %d, %d/%d\n",
1864 req->status, req->actual, req->length);
1866 if (!l_ethdev.network_started) {
1867 if (rndis_get_state(dev->rndis_config)
1868 == RNDIS_DATA_INITIALIZED) {
1869 l_ethdev.network_started = 1;
1870 printf("USB RNDIS network up!\n");
1874 req->context = NULL;
1876 if (req != dev->stat_req)
1877 usb_ep_free_request(ep, req);
1880 static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
1882 static int rndis_control_ack(struct eth_device *net)
1884 struct eth_dev *dev = &l_ethdev;
1886 struct usb_request *resp = dev->stat_req;
1888 /* in case RNDIS calls this after disconnect */
1890 debug("status ENODEV\n");
1894 /* in case queue length > 1 */
1895 if (resp->context) {
1896 resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
1899 resp->buf = rndis_resp_buf;
1903 * Send RNDIS RESPONSE_AVAILABLE notification;
1904 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1907 resp->complete = rndis_control_ack_complete;
1908 resp->context = dev;
1910 *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
1911 *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
1913 length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
1916 rndis_control_ack_complete(dev->status_ep, resp);
1924 #define rndis_control_ack NULL
1928 static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
1930 if (rndis_active(dev)) {
1931 rndis_set_param_medium(dev->rndis_config,
1933 BITRATE(dev->gadget)/100);
1934 rndis_signal_connect(dev->rndis_config);
1938 static int eth_stop(struct eth_dev *dev)
1940 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1942 unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1945 if (rndis_active(dev)) {
1946 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
1947 rndis_signal_disconnect(dev->rndis_config);
1949 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1950 /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1952 while (get_timer(ts) < timeout)
1953 usb_gadget_handle_interrupts();
1956 rndis_uninit(dev->rndis_config);
1963 /*-------------------------------------------------------------------------*/
1965 static int is_eth_addr_valid(char *str)
1967 if (strlen(str) == 17) {
1972 /* see if it looks like an ethernet address */
1976 for (i = 0; i < 6; i++) {
1977 char term = (i == 5 ? '\0' : ':');
1979 ea[i] = simple_strtol(p, &q, 16);
1981 if ((q - p) != 2 || *q++ != term)
1987 if (i == 6) /* it looks ok */
1993 static u8 nibble(unsigned char c)
1995 if (likely(isdigit(c)))
1998 if (likely(isxdigit(c)))
1999 return 10 + c - 'A';
2003 static int get_ether_addr(const char *str, u8 *dev_addr)
2008 for (i = 0; i < 6; i++) {
2011 if ((*str == '.') || (*str == ':'))
2013 num = nibble(*str++) << 4;
2014 num |= (nibble(*str++));
2017 if (is_valid_ether_addr(dev_addr))
2023 static int eth_bind(struct usb_gadget *gadget)
2025 struct eth_dev *dev = &l_ethdev;
2026 u8 cdc = 1, zlp = 1, rndis = 1;
2027 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
2028 int status = -ENOMEM;
2032 /* these flags are only ever cleared; compiler take note */
2033 #ifndef DEV_CONFIG_CDC
2036 #ifndef CONFIG_USB_ETH_RNDIS
2040 * Because most host side USB stacks handle CDC Ethernet, that
2041 * standard protocol is _strongly_ preferred for interop purposes.
2042 * (By everyone except Microsoft.)
2044 if (gadget_is_pxa(gadget)) {
2045 /* pxa doesn't support altsettings */
2047 } else if (gadget_is_musbhdrc(gadget)) {
2048 /* reduce tx dma overhead by avoiding special cases */
2050 } else if (gadget_is_sh(gadget)) {
2051 /* sh doesn't support multiple interfaces or configs */
2054 } else if (gadget_is_sa1100(gadget)) {
2055 /* hardware can't write zlps */
2058 * sa1100 CAN do CDC, without status endpoint ... we use
2059 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2064 gcnum = usb_gadget_controller_number(gadget);
2066 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
2069 * can't assume CDC works. don't want to default to
2070 * anything less functional on CDC-capable hardware,
2071 * so we fail in this case.
2073 error("controller '%s' not recognized",
2079 * If there's an RNDIS configuration, that's what Windows wants to
2080 * be using ... so use these product IDs here and in the "linux.inf"
2081 * needed to install MSFT drivers. Current Linux kernels will use
2082 * the second configuration if it's CDC Ethernet, and need some help
2083 * to choose the right configuration otherwise.
2086 #if defined(CONFIG_USB_RNDIS_VENDOR_ID) && defined(CONFIG_USB_RNDIS_PRODUCT_ID)
2087 device_desc.idVendor =
2088 __constant_cpu_to_le16(CONFIG_USB_RNDIS_VENDOR_ID);
2089 device_desc.idProduct =
2090 __constant_cpu_to_le16(CONFIG_USB_RNDIS_PRODUCT_ID);
2092 device_desc.idVendor =
2093 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2094 device_desc.idProduct =
2095 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2097 sprintf(product_desc, "RNDIS/%s", driver_desc);
2100 * CDC subset ... recognized by Linux since 2.4.10, but Windows
2101 * drivers aren't widely available. (That may be improved by
2102 * supporting one submode of the "SAFE" variant of MDLM.)
2105 #if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID)
2106 device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID);
2107 device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID);
2110 device_desc.idVendor =
2111 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2112 device_desc.idProduct =
2113 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2117 /* support optional vendor/distro customization */
2119 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2121 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
2123 strlcpy(product_desc, iProduct, sizeof product_desc);
2124 if (iSerialNumber) {
2125 device_desc.iSerialNumber = STRING_SERIALNUMBER,
2126 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
2129 /* all we really need is bulk IN/OUT */
2130 usb_ep_autoconfig_reset(gadget);
2131 in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
2134 error("can't autoconfigure on %s\n",
2138 in_ep->driver_data = in_ep; /* claim */
2140 out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
2143 out_ep->driver_data = out_ep; /* claim */
2145 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2147 * CDC Ethernet control interface doesn't require a status endpoint.
2148 * Since some hosts expect one, try to allocate one anyway.
2151 status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
2153 status_ep->driver_data = status_ep; /* claim */
2155 error("can't run RNDIS on %s", gadget->name);
2157 #ifdef DEV_CONFIG_CDC
2159 control_intf.bNumEndpoints = 0;
2160 /* FIXME remove endpoint from descriptor list */
2166 /* one config: cdc, else minimal subset */
2168 eth_config.bNumInterfaces = 1;
2169 eth_config.iConfiguration = STRING_SUBSET;
2172 * use functions to set these up, in case we're built to work
2173 * with multiple controllers and must override CDC Ethernet.
2175 fs_subset_descriptors();
2176 hs_subset_descriptors();
2179 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
2180 usb_gadget_set_selfpowered(gadget);
2182 /* For now RNDIS is always a second config */
2184 device_desc.bNumConfigurations = 2;
2186 if (gadget_is_dualspeed(gadget)) {
2188 dev_qualifier.bNumConfigurations = 2;
2190 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2192 /* assumes ep0 uses the same value for both speeds ... */
2193 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2195 /* and that all endpoints are dual-speed */
2196 hs_source_desc.bEndpointAddress =
2197 fs_source_desc.bEndpointAddress;
2198 hs_sink_desc.bEndpointAddress =
2199 fs_sink_desc.bEndpointAddress;
2200 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2202 hs_status_desc.bEndpointAddress =
2203 fs_status_desc.bEndpointAddress;
2207 if (gadget_is_otg(gadget)) {
2208 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2209 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2210 eth_config.bMaxPower = 4;
2211 #ifdef CONFIG_USB_ETH_RNDIS
2212 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2213 rndis_config.bMaxPower = 4;
2218 /* network device setup */
2219 dev->net = &l_netdev;
2225 dev->out_ep = out_ep;
2226 dev->status_ep = status_ep;
2229 * Module params for these addresses should come from ID proms.
2230 * The host side address is used with CDC and RNDIS, and commonly
2231 * ends up in a persistent config database. It's not clear if
2232 * host side code for the SAFE thing cares -- its original BLAN
2233 * thing didn't, Sharp never assigned those addresses on Zaurii.
2235 get_ether_addr(dev_addr, dev->net->enetaddr);
2237 memset(tmp, 0, sizeof(tmp));
2238 memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
2240 get_ether_addr(host_addr, dev->host_mac);
2242 sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
2243 dev->host_mac[0], dev->host_mac[1],
2244 dev->host_mac[2], dev->host_mac[3],
2245 dev->host_mac[4], dev->host_mac[5]);
2248 status = rndis_init();
2250 error("can't init RNDIS, %d", status);
2256 * use PKTSIZE (or aligned... from u-boot) and set
2257 * wMaxSegmentSize accordingly
2259 dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
2261 /* preallocate control message data and buffer */
2262 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
2265 dev->req->buf = control_req;
2266 dev->req->complete = eth_setup_complete;
2268 /* ... and maybe likewise for status transfer */
2269 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2270 if (dev->status_ep) {
2271 dev->stat_req = usb_ep_alloc_request(dev->status_ep,
2273 if (!dev->stat_req) {
2274 usb_ep_free_request(dev->status_ep, dev->req);
2278 dev->stat_req->buf = status_req;
2279 dev->stat_req->context = NULL;
2283 /* finish hookup to lower layer ... */
2284 dev->gadget = gadget;
2285 set_gadget_data(gadget, dev);
2286 gadget->ep0->driver_data = dev;
2289 * two kinds of host-initiated state changes:
2290 * - iff DATA transfer is active, carrier is "on"
2291 * - tx queueing enabled if open *and* carrier is "on"
2294 printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
2295 out_ep->name, in_ep->name,
2296 status_ep ? " STATUS " : "",
2297 status_ep ? status_ep->name : ""
2299 printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2300 dev->net->enetaddr[0], dev->net->enetaddr[1],
2301 dev->net->enetaddr[2], dev->net->enetaddr[3],
2302 dev->net->enetaddr[4], dev->net->enetaddr[5]);
2305 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2306 dev->host_mac[0], dev->host_mac[1],
2307 dev->host_mac[2], dev->host_mac[3],
2308 dev->host_mac[4], dev->host_mac[5]);
2313 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2315 dev->rndis_config = rndis_register(rndis_control_ack);
2316 if (dev->rndis_config < 0) {
2319 debug("RNDIS setup failed\n");
2324 /* these set up a lot of the OIDs that RNDIS needs */
2325 rndis_set_host_mac(dev->rndis_config, dev->host_mac);
2326 if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
2327 &dev->stats, &dev->cdc_filter))
2329 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2332 if (rndis_set_param_medium(dev->rndis_config,
2333 NDIS_MEDIUM_802_3, 0))
2335 printf("RNDIS ready\n");
2340 error("%s failed, status = %d", __func__, status);
2345 /*-------------------------------------------------------------------------*/
2347 static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
2349 struct eth_dev *dev = &l_ethdev;
2350 struct usb_gadget *gadget;
2352 unsigned long timeout = USB_CONNECT_TIMEOUT;
2355 error("received NULL ptr");
2359 /* Configure default mac-addresses for the USB ethernet device */
2360 #ifdef CONFIG_USBNET_DEV_ADDR
2361 strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
2363 #ifdef CONFIG_USBNET_HOST_ADDR
2364 strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
2366 /* Check if the user overruled the MAC addresses */
2367 if (getenv("usbnet_devaddr"))
2368 strlcpy(dev_addr, getenv("usbnet_devaddr"),
2371 if (getenv("usbnet_hostaddr"))
2372 strlcpy(host_addr, getenv("usbnet_hostaddr"),
2375 if (!is_eth_addr_valid(dev_addr)) {
2376 error("Need valid 'usbnet_devaddr' to be set");
2379 if (!is_eth_addr_valid(host_addr)) {
2380 error("Need valid 'usbnet_hostaddr' to be set");
2384 if (usb_gadget_register_driver(ð_driver) < 0)
2387 dev->network_started = 0;
2389 packet_received = 0;
2392 gadget = dev->gadget;
2393 usb_gadget_connect(gadget);
2395 if (getenv("cdc_connect_timeout"))
2396 timeout = simple_strtoul(getenv("cdc_connect_timeout"),
2397 NULL, 10) * CONFIG_SYS_HZ;
2399 while (!l_ethdev.network_started) {
2400 /* Handle control-c and timeouts */
2401 if (ctrlc() || (get_timer(ts) > timeout)) {
2402 error("The remote end did not respond in time.");
2405 usb_gadget_handle_interrupts();
2408 packet_received = 0;
2409 rx_submit(dev, dev->rx_req, 0);
2415 static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
2418 void *rndis_pkt = NULL;
2419 struct eth_dev *dev = &l_ethdev;
2420 struct usb_request *req = dev->tx_req;
2422 unsigned long timeout = USB_CONNECT_TIMEOUT;
2424 debug("%s:...\n", __func__);
2426 /* new buffer is needed to include RNDIS header */
2427 if (rndis_active(dev)) {
2428 rndis_pkt = malloc(length +
2429 sizeof(struct rndis_packet_msg_type));
2431 error("No memory to alloc RNDIS packet");
2434 rndis_add_hdr(rndis_pkt, length);
2435 memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
2438 length += sizeof(struct rndis_packet_msg_type);
2441 req->context = NULL;
2442 req->complete = tx_complete;
2445 * use zlp framing on tx for strict CDC-Ether conformance,
2446 * though any robust network rx path ignores extra padding.
2447 * and some hardware doesn't like to write zlps.
2450 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2453 req->length = length;
2455 /* throttle highspeed IRQ rate back slightly */
2456 if (gadget_is_dualspeed(dev->gadget))
2457 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2458 ? ((dev->tx_qlen % qmult) != 0) : 0;
2464 retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
2467 debug("%s: packet queued\n", __func__);
2468 while (!packet_sent) {
2469 if (get_timer(ts) > timeout) {
2470 printf("timeout sending packets to usb ethernet\n");
2473 usb_gadget_handle_interrupts();
2480 dev->stats.tx_dropped++;
2484 static int usb_eth_recv(struct eth_device *netdev)
2486 struct eth_dev *dev = &l_ethdev;
2488 usb_gadget_handle_interrupts();
2490 if (packet_received) {
2491 debug("%s: packet received\n", __func__);
2493 NetReceive(NetRxPackets[0], dev->rx_req->length);
2494 packet_received = 0;
2496 rx_submit(dev, dev->rx_req, 0);
2498 error("dev->rx_req invalid");
2503 void usb_eth_halt(struct eth_device *netdev)
2505 struct eth_dev *dev = &l_ethdev;
2508 error("received NULL ptr");
2512 /* If the gadget not registered, simple return */
2517 * Some USB controllers may need additional deinitialization here
2518 * before dropping pull-up (also due to hardware issues).
2519 * For example: unhandled interrupt with status stage started may
2520 * bring the controller to fully broken state (until board reset).
2521 * There are some variants to debug and fix such cases:
2522 * 1) In the case of RNDIS connection eth_stop can perform additional
2523 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2524 * 2) 'pullup' callback in your UDC driver can be improved to perform
2525 * this deinitialization.
2529 usb_gadget_disconnect(dev->gadget);
2531 /* Clear pending interrupt */
2532 if (dev->network_started) {
2533 usb_gadget_handle_interrupts();
2534 dev->network_started = 0;
2537 usb_gadget_unregister_driver(ð_driver);
2540 static struct usb_gadget_driver eth_driver = {
2544 .unbind = eth_unbind,
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);