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 <linux/usb/cdc.h>
28 #include <linux/usb/gadget.h>
31 #include <linux/ctype.h>
33 #include "gadget_chips.h"
36 #define USB_NET_NAME "usb_ether"
39 extern struct platform_data brd;
41 #define spin_unlock(x)
44 unsigned packet_received, packet_sent;
46 #define GFP_ATOMIC ((gfp_t) 0)
47 #define GFP_KERNEL ((gfp_t) 0)
50 * Ethernet gadget driver -- with CDC and non-CDC options
51 * Builds on hardware support for a full duplex link.
53 * CDC Ethernet is the standard USB solution for sending Ethernet frames
54 * using USB. Real hardware tends to use the same framing protocol but look
55 * different for control features. This driver strongly prefers to use
56 * this USB-IF standard as its open-systems interoperability solution;
57 * most host side USB stacks (except from Microsoft) support it.
59 * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
60 * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
61 * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
63 * There's some hardware that can't talk CDC ECM. We make that hardware
64 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
65 * link-level setup only requires activating the configuration. Only the
66 * endpoint descriptors, and product/vendor IDs, are relevant; no control
67 * operations are available. Linux supports it, but other host operating
68 * systems may not. (This is a subset of CDC Ethernet.)
70 * It turns out that if you add a few descriptors to that "CDC Subset",
71 * (Windows) host side drivers from MCCI can treat it as one submode of
72 * a proprietary scheme called "SAFE" ... without needing to know about
73 * specific product/vendor IDs. So we do that, making it easier to use
74 * those MS-Windows drivers. Those added descriptors make it resemble a
75 * CDC MDLM device, but they don't change device behavior at all. (See
76 * MCCI Engineering report 950198 "SAFE Networking Functions".)
78 * A third option is also in use. Rather than CDC Ethernet, or something
79 * simpler, Microsoft pushes their own approach: RNDIS. The published
80 * RNDIS specs are ambiguous and appear to be incomplete, and are also
81 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
83 #define ETH_ALEN 6 /* Octets in one ethernet addr */
84 #define ETH_HLEN 14 /* Total octets in header. */
85 #define ETH_ZLEN 60 /* Min. octets in frame sans FCS */
86 #define ETH_DATA_LEN 1500 /* Max. octets in payload */
87 #define ETH_FRAME_LEN PKTSIZE_ALIGN /* Max. octets in frame sans FCS */
88 #define ETH_FCS_LEN 4 /* Octets in the FCS */
90 #define DRIVER_DESC "Ethernet Gadget"
91 /* Based on linux 2.6.27 version */
92 #define DRIVER_VERSION "May Day 2005"
94 static const char shortname[] = "ether";
95 static const char driver_desc[] = DRIVER_DESC;
97 #define RX_EXTRA 20 /* guard against rx overflows */
99 #ifndef CONFIG_USB_ETH_RNDIS
100 #define rndis_uninit(x) do {} while (0)
101 #define rndis_deregister(c) do {} while (0)
102 #define rndis_exit() do {} while (0)
105 /* CDC and RNDIS support the same host-chosen outgoing packet filters. */
106 #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
107 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
108 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
109 |USB_CDC_PACKET_TYPE_DIRECTED)
111 #define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
113 /*-------------------------------------------------------------------------*/
116 struct usb_gadget *gadget;
117 struct usb_request *req; /* for control responses */
118 struct usb_request *stat_req; /* for cdc & rndis status */
121 struct usb_ep *in_ep, *out_ep, *status_ep;
122 const struct usb_endpoint_descriptor
125 struct usb_request *tx_req, *rx_req;
127 struct eth_device *net;
128 struct net_device_stats stats;
129 unsigned int tx_qlen;
134 unsigned suspended:1;
135 unsigned network_started:1;
139 #define WORK_RX_MEMORY 0
141 u8 host_mac[ETH_ALEN];
145 * This version autoconfigures as much as possible at run-time.
147 * It also ASSUMES a self-powered device, without remote wakeup,
148 * although remote wakeup support would make sense.
151 /*-------------------------------------------------------------------------*/
152 static struct eth_dev l_ethdev;
153 static struct eth_device l_netdev;
154 static struct usb_gadget_driver eth_driver;
156 /*-------------------------------------------------------------------------*/
158 /* "main" config is either CDC, or its simple subset */
159 static inline int is_cdc(struct eth_dev *dev)
161 #if !defined(CONFIG_USB_ETH_SUBSET)
162 return 1; /* only cdc possible */
163 #elif !defined(CONFIG_USB_ETH_CDC)
164 return 0; /* only subset possible */
166 return dev->cdc; /* depends on what hardware we found */
170 /* "secondary" RNDIS config may sometimes be activated */
171 static inline int rndis_active(struct eth_dev *dev)
173 #ifdef CONFIG_USB_ETH_RNDIS
180 #define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
181 #define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev))
183 #define DEFAULT_QLEN 2 /* double buffering by default */
185 /* peak bulk transfer bits-per-second */
186 #define HS_BPS (13 * 512 * 8 * 1000 * 8)
187 #define FS_BPS (19 * 64 * 1 * 1000 * 8)
189 #ifdef CONFIG_USB_GADGET_DUALSPEED
190 #define DEVSPEED USB_SPEED_HIGH
192 #ifdef CONFIG_USB_ETH_QMULT
193 #define qmult CONFIG_USB_ETH_QMULT
198 /* for dual-speed hardware, use deeper queues at highspeed */
199 #define qlen(gadget) \
200 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
202 static inline int BITRATE(struct usb_gadget *g)
204 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
207 #else /* full speed (low speed doesn't do bulk) */
211 #define DEVSPEED USB_SPEED_FULL
213 #define qlen(gadget) DEFAULT_QLEN
215 static inline int BITRATE(struct usb_gadget *g)
221 /*-------------------------------------------------------------------------*/
224 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
225 * Instead: allocate your own, using normal USB-IF procedures.
229 * Thanks to NetChip Technologies for donating this product ID.
230 * It's for devices with only CDC Ethernet configurations.
232 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
233 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
236 * For hardware that can't talk CDC, we use the same vendor ID that
237 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
238 * with pxa250. We're protocol-compatible, if the host-side drivers
239 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
240 * drivers that need to hard-wire endpoint numbers have a hook.
242 * The protocol is a minimal subset of CDC Ether, which works on any bulk
243 * hardware that's not deeply broken ... even on hardware that can't talk
244 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
245 * doesn't handle control-OUT).
247 #define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */
248 #define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */
251 * For hardware that can talk RNDIS and either of the above protocols,
252 * use this ID ... the windows INF files will know it. Unless it's
253 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
254 * the non-RNDIS configuration.
256 #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
257 #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
260 * Some systems will want different product identifers published in the
261 * device descriptor, either numbers or strings or both. These string
262 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
266 * Emulating them in eth_bind:
267 * static ushort idVendor;
268 * static ushort idProduct;
271 #if defined(CONFIG_USBNET_MANUFACTURER)
272 static char *iManufacturer = CONFIG_USBNET_MANUFACTURER;
274 static char *iManufacturer = "U-boot";
277 /* These probably need to be configurable. */
278 static ushort bcdDevice;
279 static char *iProduct;
280 static char *iSerialNumber;
282 static char dev_addr[18];
284 static char host_addr[18];
287 /*-------------------------------------------------------------------------*/
290 * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
291 * ep0 implementation: descriptors, config management, setup().
292 * also optional class-specific notification interrupt transfer.
296 * DESCRIPTORS ... most are static, but strings and (full) configuration
297 * descriptors are built on demand. For now we do either full CDC, or
298 * our simple subset, with RNDIS as an optional second configuration.
300 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
301 * the class descriptors match a modem (they're ignored; it's really just
302 * Ethernet functionality), they don't need the NOP altsetting, and the
303 * status transfer endpoint isn't optional.
306 #define STRING_MANUFACTURER 1
307 #define STRING_PRODUCT 2
308 #define STRING_ETHADDR 3
309 #define STRING_DATA 4
310 #define STRING_CONTROL 5
311 #define STRING_RNDIS_CONTROL 6
313 #define STRING_SUBSET 8
314 #define STRING_RNDIS 9
315 #define STRING_SERIALNUMBER 10
317 /* holds our biggest descriptor (or RNDIS response) */
318 #define USB_BUFSIZ 256
321 * This device advertises one configuration, eth_config, unless RNDIS
322 * is enabled (rndis_config) on hardware supporting at least two configs.
324 * NOTE: Controllers like superh_udc should probably be able to use
325 * an RNDIS-only configuration.
327 * FIXME define some higher-powered configurations to make it easier
328 * to recharge batteries ...
331 #define DEV_CONFIG_VALUE 1 /* cdc or subset */
332 #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
334 static struct usb_device_descriptor
336 .bLength = sizeof device_desc,
337 .bDescriptorType = USB_DT_DEVICE,
339 .bcdUSB = __constant_cpu_to_le16(0x0200),
341 .bDeviceClass = USB_CLASS_COMM,
342 .bDeviceSubClass = 0,
343 .bDeviceProtocol = 0,
345 .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
346 .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
347 .iManufacturer = STRING_MANUFACTURER,
348 .iProduct = STRING_PRODUCT,
349 .bNumConfigurations = 1,
352 static struct usb_otg_descriptor
354 .bLength = sizeof otg_descriptor,
355 .bDescriptorType = USB_DT_OTG,
357 .bmAttributes = USB_OTG_SRP,
360 static struct usb_config_descriptor
362 .bLength = sizeof eth_config,
363 .bDescriptorType = USB_DT_CONFIG,
365 /* compute wTotalLength on the fly */
367 .bConfigurationValue = DEV_CONFIG_VALUE,
368 .iConfiguration = STRING_CDC,
369 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
373 #ifdef CONFIG_USB_ETH_RNDIS
374 static struct usb_config_descriptor
376 .bLength = sizeof rndis_config,
377 .bDescriptorType = USB_DT_CONFIG,
379 /* compute wTotalLength on the fly */
381 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
382 .iConfiguration = STRING_RNDIS,
383 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
389 * Compared to the simple CDC subset, the full CDC Ethernet model adds
390 * three class descriptors, two interface descriptors, optional status
391 * endpoint. Both have a "data" interface and two bulk endpoints.
392 * There are also differences in how control requests are handled.
394 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
395 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
396 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
397 * work around those bugs) are unlikely to ever come from MSFT, you may
398 * wish to avoid using RNDIS.
400 * MCCI offers an alternative to RNDIS if you need to connect to Windows
401 * but have hardware that can't support CDC Ethernet. We add descriptors
402 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
403 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
404 * get those drivers from MCCI, or bundled with various products.
407 #ifdef CONFIG_USB_ETH_CDC
408 static struct usb_interface_descriptor
410 .bLength = sizeof control_intf,
411 .bDescriptorType = USB_DT_INTERFACE,
413 .bInterfaceNumber = 0,
414 /* status endpoint is optional; this may be patched later */
416 .bInterfaceClass = USB_CLASS_COMM,
417 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
418 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
419 .iInterface = STRING_CONTROL,
423 #ifdef CONFIG_USB_ETH_RNDIS
424 static const struct usb_interface_descriptor
425 rndis_control_intf = {
426 .bLength = sizeof rndis_control_intf,
427 .bDescriptorType = USB_DT_INTERFACE,
429 .bInterfaceNumber = 0,
431 .bInterfaceClass = USB_CLASS_COMM,
432 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
433 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
434 .iInterface = STRING_RNDIS_CONTROL,
438 static const struct usb_cdc_header_desc header_desc = {
439 .bLength = sizeof header_desc,
440 .bDescriptorType = USB_DT_CS_INTERFACE,
441 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
443 .bcdCDC = __constant_cpu_to_le16(0x0110),
446 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
448 static const struct usb_cdc_union_desc union_desc = {
449 .bLength = sizeof union_desc,
450 .bDescriptorType = USB_DT_CS_INTERFACE,
451 .bDescriptorSubType = USB_CDC_UNION_TYPE,
453 .bMasterInterface0 = 0, /* index of control interface */
454 .bSlaveInterface0 = 1, /* index of DATA interface */
457 #endif /* CDC || RNDIS */
459 #ifdef CONFIG_USB_ETH_RNDIS
461 static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
462 .bLength = sizeof call_mgmt_descriptor,
463 .bDescriptorType = USB_DT_CS_INTERFACE,
464 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
466 .bmCapabilities = 0x00,
467 .bDataInterface = 0x01,
470 static const struct usb_cdc_acm_descriptor acm_descriptor = {
471 .bLength = sizeof acm_descriptor,
472 .bDescriptorType = USB_DT_CS_INTERFACE,
473 .bDescriptorSubType = USB_CDC_ACM_TYPE,
475 .bmCapabilities = 0x00,
480 #ifndef CONFIG_USB_ETH_CDC
483 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
484 * ways: data endpoints live in the control interface, there's no data
485 * interface, and it's not used to talk to a cell phone radio.
488 static const struct usb_cdc_mdlm_desc mdlm_desc = {
489 .bLength = sizeof mdlm_desc,
490 .bDescriptorType = USB_DT_CS_INTERFACE,
491 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
493 .bcdVersion = __constant_cpu_to_le16(0x0100),
495 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
496 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
501 * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
502 * can't really use its struct. All we do here is say that we're using
503 * the submode of "SAFE" which directly matches the CDC Subset.
505 static const u8 mdlm_detail_desc[] = {
508 USB_CDC_MDLM_DETAIL_TYPE,
511 0, /* network control capabilities (none) */
512 0, /* network data capabilities ("raw" encapsulation) */
517 static const struct usb_cdc_ether_desc ether_desc = {
518 .bLength = sizeof(ether_desc),
519 .bDescriptorType = USB_DT_CS_INTERFACE,
520 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
522 /* this descriptor actually adds value, surprise! */
523 .iMACAddress = STRING_ETHADDR,
524 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
525 .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN),
526 .wNumberMCFilters = __constant_cpu_to_le16(0),
527 .bNumberPowerFilters = 0,
530 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
533 * include the status endpoint if we can, even where it's optional.
534 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
535 * packet, to simplify cancellation; and a big transfer interval, to
536 * waste less bandwidth.
538 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
539 * if they ignore the connect/disconnect notifications that real aether
540 * can provide. more advanced cdc configurations might want to support
541 * encapsulated commands (vendor-specific, using control-OUT).
543 * RNDIS requires the status endpoint, since it uses that encapsulation
544 * mechanism for its funky RPC scheme.
547 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
548 #define STATUS_BYTECOUNT 16 /* 8 byte header + data */
550 static struct usb_endpoint_descriptor
552 .bLength = USB_DT_ENDPOINT_SIZE,
553 .bDescriptorType = USB_DT_ENDPOINT,
555 .bEndpointAddress = USB_DIR_IN,
556 .bmAttributes = USB_ENDPOINT_XFER_INT,
557 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
558 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
562 #ifdef CONFIG_USB_ETH_CDC
564 /* the default data interface has no endpoints ... */
566 static const struct usb_interface_descriptor
568 .bLength = sizeof data_nop_intf,
569 .bDescriptorType = USB_DT_INTERFACE,
571 .bInterfaceNumber = 1,
572 .bAlternateSetting = 0,
574 .bInterfaceClass = USB_CLASS_CDC_DATA,
575 .bInterfaceSubClass = 0,
576 .bInterfaceProtocol = 0,
579 /* ... but the "real" data interface has two bulk endpoints */
581 static const struct usb_interface_descriptor
583 .bLength = sizeof data_intf,
584 .bDescriptorType = USB_DT_INTERFACE,
586 .bInterfaceNumber = 1,
587 .bAlternateSetting = 1,
589 .bInterfaceClass = USB_CLASS_CDC_DATA,
590 .bInterfaceSubClass = 0,
591 .bInterfaceProtocol = 0,
592 .iInterface = STRING_DATA,
597 #ifdef CONFIG_USB_ETH_RNDIS
599 /* RNDIS doesn't activate by changing to the "real" altsetting */
601 static const struct usb_interface_descriptor
603 .bLength = sizeof rndis_data_intf,
604 .bDescriptorType = USB_DT_INTERFACE,
606 .bInterfaceNumber = 1,
607 .bAlternateSetting = 0,
609 .bInterfaceClass = USB_CLASS_CDC_DATA,
610 .bInterfaceSubClass = 0,
611 .bInterfaceProtocol = 0,
612 .iInterface = STRING_DATA,
617 #ifdef CONFIG_USB_ETH_SUBSET
620 * "Simple" CDC-subset option is a simple vendor-neutral model that most
621 * full speed controllers can handle: one interface, two bulk endpoints.
623 * To assist host side drivers, we fancy it up a bit, and add descriptors
624 * so some host side drivers will understand it as a "SAFE" variant.
627 static const struct usb_interface_descriptor
629 .bLength = sizeof subset_data_intf,
630 .bDescriptorType = USB_DT_INTERFACE,
632 .bInterfaceNumber = 0,
633 .bAlternateSetting = 0,
635 .bInterfaceClass = USB_CLASS_COMM,
636 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
637 .bInterfaceProtocol = 0,
638 .iInterface = STRING_DATA,
643 static struct usb_endpoint_descriptor
645 .bLength = USB_DT_ENDPOINT_SIZE,
646 .bDescriptorType = USB_DT_ENDPOINT,
648 .bEndpointAddress = USB_DIR_IN,
649 .bmAttributes = USB_ENDPOINT_XFER_BULK,
652 static struct usb_endpoint_descriptor
654 .bLength = USB_DT_ENDPOINT_SIZE,
655 .bDescriptorType = USB_DT_ENDPOINT,
657 .bEndpointAddress = USB_DIR_OUT,
658 .bmAttributes = USB_ENDPOINT_XFER_BULK,
661 static const struct usb_descriptor_header *fs_eth_function[11] = {
662 (struct usb_descriptor_header *) &otg_descriptor,
663 #ifdef CONFIG_USB_ETH_CDC
664 /* "cdc" mode descriptors */
665 (struct usb_descriptor_header *) &control_intf,
666 (struct usb_descriptor_header *) &header_desc,
667 (struct usb_descriptor_header *) &union_desc,
668 (struct usb_descriptor_header *) ðer_desc,
669 /* NOTE: status endpoint may need to be removed */
670 (struct usb_descriptor_header *) &fs_status_desc,
671 /* data interface, with altsetting */
672 (struct usb_descriptor_header *) &data_nop_intf,
673 (struct usb_descriptor_header *) &data_intf,
674 (struct usb_descriptor_header *) &fs_source_desc,
675 (struct usb_descriptor_header *) &fs_sink_desc,
677 #endif /* CONFIG_USB_ETH_CDC */
680 static inline void fs_subset_descriptors(void)
682 #ifdef CONFIG_USB_ETH_SUBSET
683 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
684 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
685 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
686 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
687 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
688 fs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
689 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
690 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
691 fs_eth_function[8] = NULL;
693 fs_eth_function[1] = NULL;
697 #ifdef CONFIG_USB_ETH_RNDIS
698 static const struct usb_descriptor_header *fs_rndis_function[] = {
699 (struct usb_descriptor_header *) &otg_descriptor,
700 /* control interface matches ACM, not Ethernet */
701 (struct usb_descriptor_header *) &rndis_control_intf,
702 (struct usb_descriptor_header *) &header_desc,
703 (struct usb_descriptor_header *) &call_mgmt_descriptor,
704 (struct usb_descriptor_header *) &acm_descriptor,
705 (struct usb_descriptor_header *) &union_desc,
706 (struct usb_descriptor_header *) &fs_status_desc,
707 /* data interface has no altsetting */
708 (struct usb_descriptor_header *) &rndis_data_intf,
709 (struct usb_descriptor_header *) &fs_source_desc,
710 (struct usb_descriptor_header *) &fs_sink_desc,
716 * usb 2.0 devices need to expose both high speed and full speed
717 * descriptors, unless they only run at full speed.
720 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
721 static struct usb_endpoint_descriptor
723 .bLength = USB_DT_ENDPOINT_SIZE,
724 .bDescriptorType = USB_DT_ENDPOINT,
726 .bmAttributes = USB_ENDPOINT_XFER_INT,
727 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
728 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
730 #endif /* CONFIG_USB_ETH_CDC */
732 static struct usb_endpoint_descriptor
734 .bLength = USB_DT_ENDPOINT_SIZE,
735 .bDescriptorType = USB_DT_ENDPOINT,
737 .bmAttributes = USB_ENDPOINT_XFER_BULK,
738 .wMaxPacketSize = __constant_cpu_to_le16(512),
741 static struct usb_endpoint_descriptor
743 .bLength = USB_DT_ENDPOINT_SIZE,
744 .bDescriptorType = USB_DT_ENDPOINT,
746 .bmAttributes = USB_ENDPOINT_XFER_BULK,
747 .wMaxPacketSize = __constant_cpu_to_le16(512),
750 static struct usb_qualifier_descriptor
752 .bLength = sizeof dev_qualifier,
753 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
755 .bcdUSB = __constant_cpu_to_le16(0x0200),
756 .bDeviceClass = USB_CLASS_COMM,
758 .bNumConfigurations = 1,
761 static const struct usb_descriptor_header *hs_eth_function[11] = {
762 (struct usb_descriptor_header *) &otg_descriptor,
763 #ifdef CONFIG_USB_ETH_CDC
764 /* "cdc" mode descriptors */
765 (struct usb_descriptor_header *) &control_intf,
766 (struct usb_descriptor_header *) &header_desc,
767 (struct usb_descriptor_header *) &union_desc,
768 (struct usb_descriptor_header *) ðer_desc,
769 /* NOTE: status endpoint may need to be removed */
770 (struct usb_descriptor_header *) &hs_status_desc,
771 /* data interface, with altsetting */
772 (struct usb_descriptor_header *) &data_nop_intf,
773 (struct usb_descriptor_header *) &data_intf,
774 (struct usb_descriptor_header *) &hs_source_desc,
775 (struct usb_descriptor_header *) &hs_sink_desc,
777 #endif /* CONFIG_USB_ETH_CDC */
780 static inline void hs_subset_descriptors(void)
782 #ifdef CONFIG_USB_ETH_SUBSET
783 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
784 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
785 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
786 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
787 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
788 hs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
789 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
790 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
791 hs_eth_function[8] = NULL;
793 hs_eth_function[1] = NULL;
797 #ifdef CONFIG_USB_ETH_RNDIS
798 static const struct usb_descriptor_header *hs_rndis_function[] = {
799 (struct usb_descriptor_header *) &otg_descriptor,
800 /* control interface matches ACM, not Ethernet */
801 (struct usb_descriptor_header *) &rndis_control_intf,
802 (struct usb_descriptor_header *) &header_desc,
803 (struct usb_descriptor_header *) &call_mgmt_descriptor,
804 (struct usb_descriptor_header *) &acm_descriptor,
805 (struct usb_descriptor_header *) &union_desc,
806 (struct usb_descriptor_header *) &hs_status_desc,
807 /* data interface has no altsetting */
808 (struct usb_descriptor_header *) &rndis_data_intf,
809 (struct usb_descriptor_header *) &hs_source_desc,
810 (struct usb_descriptor_header *) &hs_sink_desc,
816 /* maxpacket and other transfer characteristics vary by speed. */
817 static inline struct usb_endpoint_descriptor *
818 ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
819 struct usb_endpoint_descriptor *fs)
821 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
826 /*-------------------------------------------------------------------------*/
828 /* descriptors that are built on-demand */
830 static char manufacturer[50];
831 static char product_desc[40] = DRIVER_DESC;
832 static char serial_number[20];
834 /* address that the host will use ... usually assigned at random */
835 static char ethaddr[2 * ETH_ALEN + 1];
837 /* static strings, in UTF-8 */
838 static struct usb_string strings[] = {
839 { STRING_MANUFACTURER, manufacturer, },
840 { STRING_PRODUCT, product_desc, },
841 { STRING_SERIALNUMBER, serial_number, },
842 { STRING_DATA, "Ethernet Data", },
843 { STRING_ETHADDR, ethaddr, },
844 #ifdef CONFIG_USB_ETH_CDC
845 { STRING_CDC, "CDC Ethernet", },
846 { STRING_CONTROL, "CDC Communications Control", },
848 #ifdef CONFIG_USB_ETH_SUBSET
849 { STRING_SUBSET, "CDC Ethernet Subset", },
851 #ifdef CONFIG_USB_ETH_RNDIS
852 { STRING_RNDIS, "RNDIS", },
853 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
855 { } /* end of list */
858 static struct usb_gadget_strings stringtab = {
859 .language = 0x0409, /* en-us */
863 /*============================================================================*/
864 static u8 control_req[USB_BUFSIZ];
865 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
866 static u8 status_req[STATUS_BYTECOUNT] __attribute__ ((aligned(4)));
871 * strlcpy - Copy a %NUL terminated string into a sized buffer
872 * @dest: Where to copy the string to
873 * @src: Where to copy the string from
874 * @size: size of destination buffer
876 * Compatible with *BSD: the result is always a valid
877 * NUL-terminated string that fits in the buffer (unless,
878 * of course, the buffer size is zero). It does not pad
879 * out the result like strncpy() does.
881 size_t strlcpy(char *dest, const char *src, size_t size)
883 size_t ret = strlen(src);
886 size_t len = (ret >= size) ? size - 1 : ret;
887 memcpy(dest, src, len);
893 /*============================================================================*/
896 * one config, two interfaces: control, data.
897 * complications: class descriptors, and an altsetting.
900 config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
903 const struct usb_config_descriptor *config;
904 const struct usb_descriptor_header **function;
907 if (gadget_is_dualspeed(g)) {
908 hs = (g->speed == USB_SPEED_HIGH);
909 if (type == USB_DT_OTHER_SPEED_CONFIG)
912 #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
914 if (index >= device_desc.bNumConfigurations)
917 #ifdef CONFIG_USB_ETH_RNDIS
919 * list the RNDIS config first, to make Microsoft's drivers
920 * happy. DOCSIS 1.0 needs this too.
922 if (device_desc.bNumConfigurations == 2 && index == 0) {
923 config = &rndis_config;
924 function = which_fn(rndis);
928 config = ð_config;
929 function = which_fn(eth);
932 /* for now, don't advertise srp-only devices */
936 len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
939 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
943 /*-------------------------------------------------------------------------*/
945 static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
946 static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
949 set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
952 struct usb_gadget *gadget = dev->gadget;
954 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
955 /* status endpoint used for RNDIS and (optionally) CDC */
956 if (!subset_active(dev) && dev->status_ep) {
957 dev->status = ep_desc(gadget, &hs_status_desc,
959 dev->status_ep->driver_data = dev;
961 result = usb_ep_enable(dev->status_ep, dev->status);
963 debug("enable %s --> %d\n",
964 dev->status_ep->name, result);
970 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
971 dev->in_ep->driver_data = dev;
973 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
974 dev->out_ep->driver_data = dev;
977 * With CDC, the host isn't allowed to use these two data
978 * endpoints in the default altsetting for the interface.
979 * so we don't activate them yet. Reset from SET_INTERFACE.
981 * Strictly speaking RNDIS should work the same: activation is
982 * a side effect of setting a packet filter. Deactivation is
983 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
985 if (!cdc_active(dev)) {
986 result = usb_ep_enable(dev->in_ep, dev->in);
988 debug("enable %s --> %d\n",
989 dev->in_ep->name, result);
993 result = usb_ep_enable(dev->out_ep, dev->out);
995 debug("enable %s --> %d\n",
996 dev->out_ep->name, result);
1003 result = alloc_requests(dev, qlen(gadget), gfp_flags);
1005 /* on error, disable any endpoints */
1007 if (!subset_active(dev) && dev->status_ep)
1008 (void) usb_ep_disable(dev->status_ep);
1010 (void) usb_ep_disable(dev->in_ep);
1011 (void) usb_ep_disable(dev->out_ep);
1014 } else if (!cdc_active(dev)) {
1016 * activate non-CDC configs right away
1017 * this isn't strictly according to the RNDIS spec
1019 eth_start(dev, GFP_ATOMIC);
1022 /* caller is responsible for cleanup on error */
1026 static void eth_reset_config(struct eth_dev *dev)
1028 if (dev->config == 0)
1031 debug("%s\n", __func__);
1033 rndis_uninit(dev->rndis_config);
1036 * disable endpoints, forcing (synchronous) completion of
1037 * pending i/o. then free the requests.
1041 usb_ep_disable(dev->in_ep);
1043 usb_ep_free_request(dev->in_ep, dev->tx_req);
1048 usb_ep_disable(dev->out_ep);
1050 usb_ep_free_request(dev->out_ep, dev->rx_req);
1055 usb_ep_disable(dev->status_ep);
1058 dev->cdc_filter = 0;
1063 * change our operational config. must agree with the code
1064 * that returns config descriptors, and altsetting code.
1066 static int eth_set_config(struct eth_dev *dev, unsigned number,
1070 struct usb_gadget *gadget = dev->gadget;
1072 if (gadget_is_sa1100(gadget)
1074 && dev->tx_qlen != 0) {
1075 /* tx fifo is full, but we can't clear it...*/
1076 error("can't change configurations");
1079 eth_reset_config(dev);
1082 case DEV_CONFIG_VALUE:
1083 result = set_ether_config(dev, gfp_flags);
1085 #ifdef CONFIG_USB_ETH_RNDIS
1086 case DEV_RNDIS_CONFIG_VALUE:
1088 result = set_ether_config(dev, gfp_flags);
1100 eth_reset_config(dev);
1101 usb_gadget_vbus_draw(dev->gadget,
1102 gadget_is_otg(dev->gadget) ? 8 : 100);
1107 power = 2 * eth_config.bMaxPower;
1108 usb_gadget_vbus_draw(dev->gadget, power);
1110 switch (gadget->speed) {
1111 case USB_SPEED_FULL:
1112 speed = "full"; break;
1113 #ifdef CONFIG_USB_GADGET_DUALSPEED
1114 case USB_SPEED_HIGH:
1115 speed = "high"; break;
1121 dev->config = number;
1122 printf("%s speed config #%d: %d mA, %s, using %s\n",
1123 speed, number, power, driver_desc,
1128 : "CDC Ethernet Subset"));
1133 /*-------------------------------------------------------------------------*/
1135 #ifdef CONFIG_USB_ETH_CDC
1138 * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
1139 * only to notify the host about link status changes (which we support) or
1140 * report completion of some encapsulated command (as used in RNDIS). Since
1141 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1142 * command mechanism; and only one status request is ever queued.
1144 static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
1146 struct usb_cdc_notification *event = req->buf;
1147 int value = req->status;
1148 struct eth_dev *dev = ep->driver_data;
1150 /* issue the second notification if host reads the first */
1151 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1153 __le32 *data = req->buf + sizeof *event;
1155 event->bmRequestType = 0xA1;
1156 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
1157 event->wValue = __constant_cpu_to_le16(0);
1158 event->wIndex = __constant_cpu_to_le16(1);
1159 event->wLength = __constant_cpu_to_le16(8);
1161 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1162 data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
1164 req->length = STATUS_BYTECOUNT;
1165 value = usb_ep_queue(ep, req, GFP_ATOMIC);
1166 debug("send SPEED_CHANGE --> %d\n", value);
1169 } else if (value != -ECONNRESET) {
1170 debug("event %02x --> %d\n",
1171 event->bNotificationType, value);
1172 if (event->bNotificationType ==
1173 USB_CDC_NOTIFY_SPEED_CHANGE) {
1174 l_ethdev.network_started = 1;
1175 printf("USB network up!\n");
1178 req->context = NULL;
1181 static void issue_start_status(struct eth_dev *dev)
1183 struct usb_request *req = dev->stat_req;
1184 struct usb_cdc_notification *event;
1190 * FIXME ugly idiom, maybe we'd be better with just
1191 * a "cancel the whole queue" primitive since any
1192 * unlink-one primitive has way too many error modes.
1193 * here, we "know" toggle is already clear...
1195 * FIXME iff req->context != null just dequeue it
1197 usb_ep_disable(dev->status_ep);
1198 usb_ep_enable(dev->status_ep, dev->status);
1201 * 3.8.1 says to issue first NETWORK_CONNECTION, then
1202 * a SPEED_CHANGE. could be useful in some configs.
1205 event->bmRequestType = 0xA1;
1206 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
1207 event->wValue = __constant_cpu_to_le16(1); /* connected */
1208 event->wIndex = __constant_cpu_to_le16(1);
1211 req->length = sizeof *event;
1212 req->complete = eth_status_complete;
1215 value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
1217 debug("status buf queue --> %d\n", value);
1222 /*-------------------------------------------------------------------------*/
1224 static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
1226 if (req->status || req->actual != req->length)
1227 debug("setup complete --> %d, %d/%d\n",
1228 req->status, req->actual, req->length);
1231 #ifdef CONFIG_USB_ETH_RNDIS
1233 static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
1235 if (req->status || req->actual != req->length)
1236 debug("rndis response complete --> %d, %d/%d\n",
1237 req->status, req->actual, req->length);
1239 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1242 static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
1244 struct eth_dev *dev = ep->driver_data;
1247 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1248 status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
1250 error("%s: rndis parse error %d", __func__, status);
1256 * The setup() callback implements all the ep0 functionality that's not
1257 * handled lower down. CDC has a number of less-common features:
1259 * - two interfaces: control, and ethernet data
1260 * - Ethernet data interface has two altsettings: default, and active
1261 * - class-specific descriptors for the control interface
1262 * - class-specific control requests
1265 eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1267 struct eth_dev *dev = get_gadget_data(gadget);
1268 struct usb_request *req = dev->req;
1269 int value = -EOPNOTSUPP;
1270 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1271 u16 wValue = le16_to_cpu(ctrl->wValue);
1272 u16 wLength = le16_to_cpu(ctrl->wLength);
1275 * descriptors just go into the pre-allocated ep0 buffer,
1276 * while config change events may enable network traffic.
1279 debug("%s\n", __func__);
1281 req->complete = eth_setup_complete;
1282 switch (ctrl->bRequest) {
1284 case USB_REQ_GET_DESCRIPTOR:
1285 if (ctrl->bRequestType != USB_DIR_IN)
1287 switch (wValue >> 8) {
1290 value = min(wLength, (u16) sizeof device_desc);
1291 memcpy(req->buf, &device_desc, value);
1293 case USB_DT_DEVICE_QUALIFIER:
1294 if (!gadget_is_dualspeed(gadget))
1296 value = min(wLength, (u16) sizeof dev_qualifier);
1297 memcpy(req->buf, &dev_qualifier, value);
1300 case USB_DT_OTHER_SPEED_CONFIG:
1301 if (!gadget_is_dualspeed(gadget))
1305 value = config_buf(gadget, req->buf,
1308 gadget_is_otg(gadget));
1310 value = min(wLength, (u16) value);
1314 value = usb_gadget_get_string(&stringtab,
1315 wValue & 0xff, req->buf);
1318 value = min(wLength, (u16) value);
1324 case USB_REQ_SET_CONFIGURATION:
1325 if (ctrl->bRequestType != 0)
1327 if (gadget->a_hnp_support)
1328 debug("HNP available\n");
1329 else if (gadget->a_alt_hnp_support)
1330 debug("HNP needs a different root port\n");
1331 value = eth_set_config(dev, wValue, GFP_ATOMIC);
1333 case USB_REQ_GET_CONFIGURATION:
1334 if (ctrl->bRequestType != USB_DIR_IN)
1336 *(u8 *)req->buf = dev->config;
1337 value = min(wLength, (u16) 1);
1340 case USB_REQ_SET_INTERFACE:
1341 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1345 if (!cdc_active(dev) && wIndex != 0)
1349 * PXA hardware partially handles SET_INTERFACE;
1350 * we need to kluge around that interference.
1352 if (gadget_is_pxa(gadget)) {
1353 value = eth_set_config(dev, DEV_CONFIG_VALUE,
1356 * PXA25x driver use non-CDC ethernet gadget.
1357 * But only _CDC and _RNDIS code can signalize
1358 * that network is working. So we signalize it
1361 l_ethdev.network_started = 1;
1362 debug("USB network up!\n");
1366 #ifdef CONFIG_USB_ETH_CDC
1368 case 0: /* control/master intf */
1372 usb_ep_disable(dev->status_ep);
1373 usb_ep_enable(dev->status_ep, dev->status);
1378 case 1: /* data intf */
1381 usb_ep_disable(dev->in_ep);
1382 usb_ep_disable(dev->out_ep);
1385 * CDC requires the data transfers not be done from
1386 * the default interface setting ... also, setting
1387 * the non-default interface resets filters etc.
1390 if (!cdc_active(dev))
1392 usb_ep_enable(dev->in_ep, dev->in);
1393 usb_ep_enable(dev->out_ep, dev->out);
1394 dev->cdc_filter = DEFAULT_FILTER;
1396 issue_start_status(dev);
1397 eth_start(dev, GFP_ATOMIC);
1404 * FIXME this is wrong, as is the assumption that
1405 * all non-PXA hardware talks real CDC ...
1407 debug("set_interface ignored!\n");
1408 #endif /* CONFIG_USB_ETH_CDC */
1412 case USB_REQ_GET_INTERFACE:
1413 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1417 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
1420 /* for CDC, iff carrier is on, data interface is active. */
1421 if (rndis_active(dev) || wIndex != 1)
1422 *(u8 *)req->buf = 0;
1424 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1425 /* carrier always ok ...*/
1426 *(u8 *)req->buf = 1 ;
1428 value = min(wLength, (u16) 1);
1431 #ifdef CONFIG_USB_ETH_CDC
1432 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
1434 * see 6.2.30: no data, wIndex = interface,
1435 * wValue = packet filter bitmap
1437 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1442 debug("packet filter %02x\n", wValue);
1443 dev->cdc_filter = wValue;
1449 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1450 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1451 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1452 * case USB_CDC_GET_ETHERNET_STATISTIC:
1455 #endif /* CONFIG_USB_ETH_CDC */
1457 #ifdef CONFIG_USB_ETH_RNDIS
1459 * RNDIS uses the CDC command encapsulation mechanism to implement
1460 * an RPC scheme, with much getting/setting of attributes by OID.
1462 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1463 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1464 || !rndis_active(dev)
1465 || wLength > USB_BUFSIZ
1467 || rndis_control_intf.bInterfaceNumber
1470 /* read the request, then process it */
1472 req->complete = rndis_command_complete;
1473 /* later, rndis_control_ack () sends a notification */
1476 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1477 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1478 == ctrl->bRequestType
1479 && rndis_active(dev)
1480 /* && wLength >= 0x0400 */
1482 && rndis_control_intf.bInterfaceNumber
1487 /* return the result */
1488 buf = rndis_get_next_response(dev->rndis_config, &n);
1490 memcpy(req->buf, buf, n);
1491 req->complete = rndis_response_complete;
1492 rndis_free_response(dev->rndis_config, buf);
1495 /* else stalls ... spec says to avoid that */
1501 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
1502 ctrl->bRequestType, ctrl->bRequest,
1503 wValue, wIndex, wLength);
1506 /* respond with data transfer before status phase? */
1508 debug("respond with data transfer before status phase\n");
1509 req->length = value;
1510 req->zero = value < wLength
1511 && (value % gadget->ep0->maxpacket) == 0;
1512 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1514 debug("ep_queue --> %d\n", value);
1516 eth_setup_complete(gadget->ep0, req);
1520 /* host either stalls (value < 0) or reports success */
1524 /*-------------------------------------------------------------------------*/
1526 static void rx_complete(struct usb_ep *ep, struct usb_request *req);
1528 static int rx_submit(struct eth_dev *dev, struct usb_request *req,
1531 int retval = -ENOMEM;
1535 * Padding up to RX_EXTRA handles minor disagreements with host.
1536 * Normally we use the USB "terminate on short read" convention;
1537 * so allow up to (N*maxpacket), since that memory is normally
1538 * already allocated. Some hardware doesn't deal well with short
1539 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1540 * byte off the end (to force hardware errors on overflow).
1542 * RNDIS uses internal framing, and explicitly allows senders to
1543 * pad to end-of-packet. That's potentially nice for speed,
1544 * but means receivers can't recover synch on their own.
1547 debug("%s\n", __func__);
1549 size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1550 size += dev->out_ep->maxpacket - 1;
1551 if (rndis_active(dev))
1552 size += sizeof(struct rndis_packet_msg_type);
1553 size -= size % dev->out_ep->maxpacket;
1556 * Some platforms perform better when IP packets are aligned,
1557 * but on at least one, checksumming fails otherwise. Note:
1558 * RNDIS headers involve variable numbers of LE32 values.
1561 req->buf = (u8 *) NetRxPackets[0];
1563 req->complete = rx_complete;
1565 retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
1568 error("rx submit --> %d", retval);
1573 static void rx_complete(struct usb_ep *ep, struct usb_request *req)
1575 struct eth_dev *dev = ep->driver_data;
1577 debug("%s: status %d\n", __func__, req->status);
1578 switch (req->status) {
1579 /* normal completion */
1581 if (rndis_active(dev)) {
1582 /* we know MaxPacketsPerTransfer == 1 here */
1583 int length = rndis_rm_hdr(req->buf, req->actual);
1586 req->length -= length;
1587 req->actual -= length;
1589 if (req->actual < ETH_HLEN || ETH_FRAME_LEN < req->actual) {
1591 dev->stats.rx_errors++;
1592 dev->stats.rx_length_errors++;
1593 debug("rx length %d\n", req->length);
1597 dev->stats.rx_packets++;
1598 dev->stats.rx_bytes += req->length;
1601 /* software-driven interface shutdown */
1602 case -ECONNRESET: /* unlink */
1603 case -ESHUTDOWN: /* disconnect etc */
1604 /* for hardware automagic (such as pxa) */
1605 case -ECONNABORTED: /* endpoint reset */
1610 dev->stats.rx_over_errors++;
1613 dev->stats.rx_errors++;
1617 packet_received = 1;
1620 static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
1623 dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
1628 dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
1636 usb_ep_free_request(dev->in_ep, dev->tx_req);
1638 error("can't alloc requests");
1642 static void tx_complete(struct usb_ep *ep, struct usb_request *req)
1644 struct eth_dev *dev = ep->driver_data;
1646 debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
1647 switch (req->status) {
1649 dev->stats.tx_errors++;
1650 debug("tx err %d\n", req->status);
1652 case -ECONNRESET: /* unlink */
1653 case -ESHUTDOWN: /* disconnect etc */
1656 dev->stats.tx_bytes += req->length;
1658 dev->stats.tx_packets++;
1663 static inline int eth_is_promisc(struct eth_dev *dev)
1665 /* no filters for the CDC subset; always promisc */
1666 if (subset_active(dev))
1668 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1672 static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1674 struct eth_dev *dev = netdev_priv(net);
1675 int length = skb->len;
1677 struct usb_request *req = NULL;
1678 unsigned long flags;
1680 /* apply outgoing CDC or RNDIS filters */
1681 if (!eth_is_promisc (dev)) {
1682 u8 *dest = skb->data;
1684 if (is_multicast_ether_addr(dest)) {
1687 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1688 * SET_ETHERNET_MULTICAST_FILTERS requests
1690 if (is_broadcast_ether_addr(dest))
1691 type = USB_CDC_PACKET_TYPE_BROADCAST;
1693 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1694 if (!(dev->cdc_filter & type)) {
1695 dev_kfree_skb_any (skb);
1699 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1702 spin_lock_irqsave(&dev->req_lock, flags);
1704 * this freelist can be empty if an interrupt triggered disconnect()
1705 * and reconfigured the gadget (shutting down this queue) after the
1706 * network stack decided to xmit but before we got the spinlock.
1708 if (list_empty(&dev->tx_reqs)) {
1709 spin_unlock_irqrestore(&dev->req_lock, flags);
1713 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1714 list_del (&req->list);
1716 /* temporarily stop TX queue when the freelist empties */
1717 if (list_empty (&dev->tx_reqs))
1718 netif_stop_queue (net);
1719 spin_unlock_irqrestore(&dev->req_lock, flags);
1721 /* no buffer copies needed, unless the network stack did it
1722 * or the hardware can't use skb buffers.
1723 * or there's not enough space for any RNDIS headers we need
1725 if (rndis_active(dev)) {
1726 struct sk_buff *skb_rndis;
1728 skb_rndis = skb_realloc_headroom (skb,
1729 sizeof (struct rndis_packet_msg_type));
1733 dev_kfree_skb_any (skb);
1735 rndis_add_hdr (skb);
1738 req->buf = skb->data;
1740 req->complete = tx_complete;
1742 /* use zlp framing on tx for strict CDC-Ether conformance,
1743 * though any robust network rx path ignores extra padding.
1744 * and some hardware doesn't like to write zlps.
1747 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1750 req->length = length;
1752 /* throttle highspeed IRQ rate back slightly */
1753 if (gadget_is_dualspeed(dev->gadget))
1754 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1755 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1758 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1761 DEBUG (dev, "tx queue err %d\n", retval);
1764 net->trans_start = jiffies;
1765 atomic_inc (&dev->tx_qlen);
1770 dev->stats.tx_dropped++;
1771 dev_kfree_skb_any (skb);
1772 spin_lock_irqsave(&dev->req_lock, flags);
1773 if (list_empty (&dev->tx_reqs))
1774 netif_start_queue (net);
1775 list_add (&req->list, &dev->tx_reqs);
1776 spin_unlock_irqrestore(&dev->req_lock, flags);
1781 /*-------------------------------------------------------------------------*/
1784 static void eth_unbind(struct usb_gadget *gadget)
1786 struct eth_dev *dev = get_gadget_data(gadget);
1788 debug("%s...\n", __func__);
1789 rndis_deregister(dev->rndis_config);
1792 /* we've already been disconnected ... no i/o is active */
1794 usb_ep_free_request(gadget->ep0, dev->req);
1797 if (dev->stat_req) {
1798 usb_ep_free_request(dev->status_ep, dev->stat_req);
1799 dev->stat_req = NULL;
1803 usb_ep_free_request(dev->in_ep, dev->tx_req);
1808 usb_ep_free_request(dev->out_ep, dev->rx_req);
1812 /* unregister_netdev (dev->net);*/
1813 /* free_netdev(dev->net);*/
1816 set_gadget_data(gadget, NULL);
1819 static void eth_disconnect(struct usb_gadget *gadget)
1821 eth_reset_config(get_gadget_data(gadget));
1822 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1825 static void eth_suspend(struct usb_gadget *gadget)
1830 static void eth_resume(struct usb_gadget *gadget)
1835 /*-------------------------------------------------------------------------*/
1837 #ifdef CONFIG_USB_ETH_RNDIS
1840 * The interrupt endpoint is used in RNDIS to notify the host when messages
1841 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1842 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1843 * REMOTE_NDIS_KEEPALIVE_MSG.
1845 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1846 * normally just one notification will be queued.
1849 static void rndis_control_ack_complete(struct usb_ep *ep,
1850 struct usb_request *req)
1852 struct eth_dev *dev = ep->driver_data;
1854 debug("%s...\n", __func__);
1855 if (req->status || req->actual != req->length)
1856 debug("rndis control ack complete --> %d, %d/%d\n",
1857 req->status, req->actual, req->length);
1859 if (!l_ethdev.network_started) {
1860 if (rndis_get_state(dev->rndis_config)
1861 == RNDIS_DATA_INITIALIZED) {
1862 l_ethdev.network_started = 1;
1863 printf("USB RNDIS network up!\n");
1867 req->context = NULL;
1869 if (req != dev->stat_req)
1870 usb_ep_free_request(ep, req);
1873 static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
1875 static int rndis_control_ack(struct eth_device *net)
1877 struct eth_dev *dev = &l_ethdev;
1879 struct usb_request *resp = dev->stat_req;
1881 /* in case RNDIS calls this after disconnect */
1883 debug("status ENODEV\n");
1887 /* in case queue length > 1 */
1888 if (resp->context) {
1889 resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
1892 resp->buf = rndis_resp_buf;
1896 * Send RNDIS RESPONSE_AVAILABLE notification;
1897 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1900 resp->complete = rndis_control_ack_complete;
1901 resp->context = dev;
1903 *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
1904 *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
1906 length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
1909 rndis_control_ack_complete(dev->status_ep, resp);
1917 #define rndis_control_ack NULL
1921 static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
1923 if (rndis_active(dev)) {
1924 rndis_set_param_medium(dev->rndis_config,
1926 BITRATE(dev->gadget)/100);
1927 rndis_signal_connect(dev->rndis_config);
1931 static int eth_stop(struct eth_dev *dev)
1933 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1935 unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1938 if (rndis_active(dev)) {
1939 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
1940 rndis_signal_disconnect(dev->rndis_config);
1942 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1943 /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1945 while (get_timer(ts) < timeout)
1946 usb_gadget_handle_interrupts();
1949 rndis_uninit(dev->rndis_config);
1956 /*-------------------------------------------------------------------------*/
1958 static int is_eth_addr_valid(char *str)
1960 if (strlen(str) == 17) {
1965 /* see if it looks like an ethernet address */
1969 for (i = 0; i < 6; i++) {
1970 char term = (i == 5 ? '\0' : ':');
1972 ea[i] = simple_strtol(p, &q, 16);
1974 if ((q - p) != 2 || *q++ != term)
1980 /* Now check the contents. */
1981 return is_valid_ether_addr(ea);
1986 static u8 nibble(unsigned char c)
1988 if (likely(isdigit(c)))
1991 if (likely(isxdigit(c)))
1992 return 10 + c - 'A';
1996 static int get_ether_addr(const char *str, u8 *dev_addr)
2001 for (i = 0; i < 6; i++) {
2004 if ((*str == '.') || (*str == ':'))
2006 num = nibble(*str++) << 4;
2007 num |= (nibble(*str++));
2010 if (is_valid_ether_addr(dev_addr))
2016 static int eth_bind(struct usb_gadget *gadget)
2018 struct eth_dev *dev = &l_ethdev;
2019 u8 cdc = 1, zlp = 1, rndis = 1;
2020 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
2021 int status = -ENOMEM;
2025 /* these flags are only ever cleared; compiler take note */
2026 #ifndef CONFIG_USB_ETH_CDC
2029 #ifndef CONFIG_USB_ETH_RNDIS
2033 * Because most host side USB stacks handle CDC Ethernet, that
2034 * standard protocol is _strongly_ preferred for interop purposes.
2035 * (By everyone except Microsoft.)
2037 if (gadget_is_pxa(gadget)) {
2038 /* pxa doesn't support altsettings */
2040 } else if (gadget_is_musbhdrc(gadget)) {
2041 /* reduce tx dma overhead by avoiding special cases */
2043 } else if (gadget_is_sh(gadget)) {
2044 /* sh doesn't support multiple interfaces or configs */
2047 } else if (gadget_is_sa1100(gadget)) {
2048 /* hardware can't write zlps */
2051 * sa1100 CAN do CDC, without status endpoint ... we use
2052 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2057 gcnum = usb_gadget_controller_number(gadget);
2059 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
2062 * can't assume CDC works. don't want to default to
2063 * anything less functional on CDC-capable hardware,
2064 * so we fail in this case.
2066 error("controller '%s' not recognized",
2072 * If there's an RNDIS configuration, that's what Windows wants to
2073 * be using ... so use these product IDs here and in the "linux.inf"
2074 * needed to install MSFT drivers. Current Linux kernels will use
2075 * the second configuration if it's CDC Ethernet, and need some help
2076 * to choose the right configuration otherwise.
2079 #if defined(CONFIG_USB_RNDIS_VENDOR_ID) && defined(CONFIG_USB_RNDIS_PRODUCT_ID)
2080 device_desc.idVendor =
2081 __constant_cpu_to_le16(CONFIG_USB_RNDIS_VENDOR_ID);
2082 device_desc.idProduct =
2083 __constant_cpu_to_le16(CONFIG_USB_RNDIS_PRODUCT_ID);
2085 device_desc.idVendor =
2086 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2087 device_desc.idProduct =
2088 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2090 sprintf(product_desc, "RNDIS/%s", driver_desc);
2093 * CDC subset ... recognized by Linux since 2.4.10, but Windows
2094 * drivers aren't widely available. (That may be improved by
2095 * supporting one submode of the "SAFE" variant of MDLM.)
2098 #if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID)
2099 device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID);
2100 device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID);
2103 device_desc.idVendor =
2104 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2105 device_desc.idProduct =
2106 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2110 /* support optional vendor/distro customization */
2112 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2114 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
2116 strlcpy(product_desc, iProduct, sizeof product_desc);
2117 if (iSerialNumber) {
2118 device_desc.iSerialNumber = STRING_SERIALNUMBER,
2119 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
2122 /* all we really need is bulk IN/OUT */
2123 usb_ep_autoconfig_reset(gadget);
2124 in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
2127 error("can't autoconfigure on %s\n",
2131 in_ep->driver_data = in_ep; /* claim */
2133 out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
2136 out_ep->driver_data = out_ep; /* claim */
2138 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2140 * CDC Ethernet control interface doesn't require a status endpoint.
2141 * Since some hosts expect one, try to allocate one anyway.
2144 status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
2146 status_ep->driver_data = status_ep; /* claim */
2148 error("can't run RNDIS on %s", gadget->name);
2150 #ifdef CONFIG_USB_ETH_CDC
2152 control_intf.bNumEndpoints = 0;
2153 /* FIXME remove endpoint from descriptor list */
2159 /* one config: cdc, else minimal subset */
2161 eth_config.bNumInterfaces = 1;
2162 eth_config.iConfiguration = STRING_SUBSET;
2165 * use functions to set these up, in case we're built to work
2166 * with multiple controllers and must override CDC Ethernet.
2168 fs_subset_descriptors();
2169 hs_subset_descriptors();
2172 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
2173 usb_gadget_set_selfpowered(gadget);
2175 /* For now RNDIS is always a second config */
2177 device_desc.bNumConfigurations = 2;
2179 if (gadget_is_dualspeed(gadget)) {
2181 dev_qualifier.bNumConfigurations = 2;
2183 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2185 /* assumes ep0 uses the same value for both speeds ... */
2186 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2188 /* and that all endpoints are dual-speed */
2189 hs_source_desc.bEndpointAddress =
2190 fs_source_desc.bEndpointAddress;
2191 hs_sink_desc.bEndpointAddress =
2192 fs_sink_desc.bEndpointAddress;
2193 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2195 hs_status_desc.bEndpointAddress =
2196 fs_status_desc.bEndpointAddress;
2200 if (gadget_is_otg(gadget)) {
2201 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2202 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2203 eth_config.bMaxPower = 4;
2204 #ifdef CONFIG_USB_ETH_RNDIS
2205 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2206 rndis_config.bMaxPower = 4;
2211 /* network device setup */
2212 dev->net = &l_netdev;
2218 dev->out_ep = out_ep;
2219 dev->status_ep = status_ep;
2222 * Module params for these addresses should come from ID proms.
2223 * The host side address is used with CDC and RNDIS, and commonly
2224 * ends up in a persistent config database. It's not clear if
2225 * host side code for the SAFE thing cares -- its original BLAN
2226 * thing didn't, Sharp never assigned those addresses on Zaurii.
2228 get_ether_addr(dev_addr, dev->net->enetaddr);
2230 memset(tmp, 0, sizeof(tmp));
2231 memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
2233 get_ether_addr(host_addr, dev->host_mac);
2235 sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
2236 dev->host_mac[0], dev->host_mac[1],
2237 dev->host_mac[2], dev->host_mac[3],
2238 dev->host_mac[4], dev->host_mac[5]);
2241 status = rndis_init();
2243 error("can't init RNDIS, %d", status);
2249 * use PKTSIZE (or aligned... from u-boot) and set
2250 * wMaxSegmentSize accordingly
2252 dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
2254 /* preallocate control message data and buffer */
2255 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
2258 dev->req->buf = control_req;
2259 dev->req->complete = eth_setup_complete;
2261 /* ... and maybe likewise for status transfer */
2262 #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2263 if (dev->status_ep) {
2264 dev->stat_req = usb_ep_alloc_request(dev->status_ep,
2266 if (!dev->stat_req) {
2267 usb_ep_free_request(dev->status_ep, dev->req);
2271 dev->stat_req->buf = status_req;
2272 dev->stat_req->context = NULL;
2276 /* finish hookup to lower layer ... */
2277 dev->gadget = gadget;
2278 set_gadget_data(gadget, dev);
2279 gadget->ep0->driver_data = dev;
2282 * two kinds of host-initiated state changes:
2283 * - iff DATA transfer is active, carrier is "on"
2284 * - tx queueing enabled if open *and* carrier is "on"
2287 printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
2288 out_ep->name, in_ep->name,
2289 status_ep ? " STATUS " : "",
2290 status_ep ? status_ep->name : ""
2292 printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2293 dev->net->enetaddr[0], dev->net->enetaddr[1],
2294 dev->net->enetaddr[2], dev->net->enetaddr[3],
2295 dev->net->enetaddr[4], dev->net->enetaddr[5]);
2298 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2299 dev->host_mac[0], dev->host_mac[1],
2300 dev->host_mac[2], dev->host_mac[3],
2301 dev->host_mac[4], dev->host_mac[5]);
2306 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2308 dev->rndis_config = rndis_register(rndis_control_ack);
2309 if (dev->rndis_config < 0) {
2312 debug("RNDIS setup failed\n");
2317 /* these set up a lot of the OIDs that RNDIS needs */
2318 rndis_set_host_mac(dev->rndis_config, dev->host_mac);
2319 if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
2320 &dev->stats, &dev->cdc_filter))
2322 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2325 if (rndis_set_param_medium(dev->rndis_config,
2326 NDIS_MEDIUM_802_3, 0))
2328 printf("RNDIS ready\n");
2333 error("%s failed, status = %d", __func__, status);
2338 /*-------------------------------------------------------------------------*/
2340 static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
2342 struct eth_dev *dev = &l_ethdev;
2343 struct usb_gadget *gadget;
2345 unsigned long timeout = USB_CONNECT_TIMEOUT;
2348 error("received NULL ptr");
2352 /* Configure default mac-addresses for the USB ethernet device */
2353 #ifdef CONFIG_USBNET_DEV_ADDR
2354 strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
2356 #ifdef CONFIG_USBNET_HOST_ADDR
2357 strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
2359 /* Check if the user overruled the MAC addresses */
2360 if (getenv("usbnet_devaddr"))
2361 strlcpy(dev_addr, getenv("usbnet_devaddr"),
2364 if (getenv("usbnet_hostaddr"))
2365 strlcpy(host_addr, getenv("usbnet_hostaddr"),
2368 if (!is_eth_addr_valid(dev_addr)) {
2369 error("Need valid 'usbnet_devaddr' to be set");
2372 if (!is_eth_addr_valid(host_addr)) {
2373 error("Need valid 'usbnet_hostaddr' to be set");
2377 if (usb_gadget_register_driver(ð_driver) < 0)
2380 dev->network_started = 0;
2382 packet_received = 0;
2385 gadget = dev->gadget;
2386 usb_gadget_connect(gadget);
2388 if (getenv("cdc_connect_timeout"))
2389 timeout = simple_strtoul(getenv("cdc_connect_timeout"),
2390 NULL, 10) * CONFIG_SYS_HZ;
2392 while (!l_ethdev.network_started) {
2393 /* Handle control-c and timeouts */
2394 if (ctrlc() || (get_timer(ts) > timeout)) {
2395 error("The remote end did not respond in time.");
2398 usb_gadget_handle_interrupts();
2401 packet_received = 0;
2402 rx_submit(dev, dev->rx_req, 0);
2408 static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
2411 void *rndis_pkt = NULL;
2412 struct eth_dev *dev = &l_ethdev;
2413 struct usb_request *req = dev->tx_req;
2415 unsigned long timeout = USB_CONNECT_TIMEOUT;
2417 debug("%s:...\n", __func__);
2419 /* new buffer is needed to include RNDIS header */
2420 if (rndis_active(dev)) {
2421 rndis_pkt = malloc(length +
2422 sizeof(struct rndis_packet_msg_type));
2424 error("No memory to alloc RNDIS packet");
2427 rndis_add_hdr(rndis_pkt, length);
2428 memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
2431 length += sizeof(struct rndis_packet_msg_type);
2434 req->context = NULL;
2435 req->complete = tx_complete;
2438 * use zlp framing on tx for strict CDC-Ether conformance,
2439 * though any robust network rx path ignores extra padding.
2440 * and some hardware doesn't like to write zlps.
2443 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2446 req->length = length;
2448 /* throttle highspeed IRQ rate back slightly */
2449 if (gadget_is_dualspeed(dev->gadget))
2450 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2451 ? ((dev->tx_qlen % qmult) != 0) : 0;
2457 retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
2460 debug("%s: packet queued\n", __func__);
2461 while (!packet_sent) {
2462 if (get_timer(ts) > timeout) {
2463 printf("timeout sending packets to usb ethernet\n");
2466 usb_gadget_handle_interrupts();
2473 dev->stats.tx_dropped++;
2477 static int usb_eth_recv(struct eth_device *netdev)
2479 struct eth_dev *dev = &l_ethdev;
2481 usb_gadget_handle_interrupts();
2483 if (packet_received) {
2484 debug("%s: packet received\n", __func__);
2486 NetReceive(NetRxPackets[0], dev->rx_req->length);
2487 packet_received = 0;
2489 rx_submit(dev, dev->rx_req, 0);
2491 error("dev->rx_req invalid");
2496 void usb_eth_halt(struct eth_device *netdev)
2498 struct eth_dev *dev = &l_ethdev;
2501 error("received NULL ptr");
2505 /* If the gadget not registered, simple return */
2510 * Some USB controllers may need additional deinitialization here
2511 * before dropping pull-up (also due to hardware issues).
2512 * For example: unhandled interrupt with status stage started may
2513 * bring the controller to fully broken state (until board reset).
2514 * There are some variants to debug and fix such cases:
2515 * 1) In the case of RNDIS connection eth_stop can perform additional
2516 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2517 * 2) 'pullup' callback in your UDC driver can be improved to perform
2518 * this deinitialization.
2522 usb_gadget_disconnect(dev->gadget);
2524 /* Clear pending interrupt */
2525 if (dev->network_started) {
2526 usb_gadget_handle_interrupts();
2527 dev->network_started = 0;
2530 usb_gadget_unregister_driver(ð_driver);
2533 static struct usb_gadget_driver eth_driver = {
2537 .unbind = eth_unbind,
2540 .disconnect = eth_disconnect,
2542 .suspend = eth_suspend,
2543 .resume = eth_resume,
2546 int usb_eth_initialize(bd_t *bi)
2548 struct eth_device *netdev = &l_netdev;
2550 strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
2552 netdev->init = usb_eth_init;
2553 netdev->send = usb_eth_send;
2554 netdev->recv = usb_eth_recv;
2555 netdev->halt = usb_eth_halt;
2557 #ifdef CONFIG_MCAST_TFTP
2558 #error not supported
2560 eth_register(netdev);