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>
30 #include <linux/ctype.h>
32 #include "gadget_chips.h"
34 #define USB_NET_NAME "usb_ether"
37 extern struct platform_data brd;
39 #define spin_unlock(x)
42 unsigned packet_received, packet_sent;
44 #define DEV_CONFIG_CDC 1
45 #define GFP_ATOMIC ((gfp_t) 0)
46 #define GFP_KERNEL ((gfp_t) 0)
49 * Ethernet gadget driver -- with CDC and non-CDC options
50 * Builds on hardware support for a full duplex link.
52 * CDC Ethernet is the standard USB solution for sending Ethernet frames
53 * using USB. Real hardware tends to use the same framing protocol but look
54 * different for control features. This driver strongly prefers to use
55 * this USB-IF standard as its open-systems interoperability solution;
56 * most host side USB stacks (except from Microsoft) support it.
58 * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
59 * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
60 * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
62 * There's some hardware that can't talk CDC ECM. We make that hardware
63 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
64 * link-level setup only requires activating the configuration. Only the
65 * endpoint descriptors, and product/vendor IDs, are relevant; no control
66 * operations are available. Linux supports it, but other host operating
67 * systems may not. (This is a subset of CDC Ethernet.)
69 * It turns out that if you add a few descriptors to that "CDC Subset",
70 * (Windows) host side drivers from MCCI can treat it as one submode of
71 * a proprietary scheme called "SAFE" ... without needing to know about
72 * specific product/vendor IDs. So we do that, making it easier to use
73 * those MS-Windows drivers. Those added descriptors make it resemble a
74 * CDC MDLM device, but they don't change device behavior at all. (See
75 * MCCI Engineering report 950198 "SAFE Networking Functions".)
77 * A third option is also in use. Rather than CDC Ethernet, or something
78 * simpler, Microsoft pushes their own approach: RNDIS. The published
79 * RNDIS specs are ambiguous and appear to be incomplete, and are also
80 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
82 #define ETH_ALEN 6 /* Octets in one ethernet addr */
83 #define ETH_HLEN 14 /* Total octets in header. */
84 #define ETH_ZLEN 60 /* Min. octets in frame sans FCS */
85 #define ETH_DATA_LEN 1500 /* Max. octets in payload */
86 #define ETH_FRAME_LEN PKTSIZE_ALIGN /* Max. octets in frame sans FCS */
87 #define ETH_FCS_LEN 4 /* Octets in the FCS */
89 #define DRIVER_DESC "Ethernet Gadget"
90 /* Based on linux 2.6.27 version */
91 #define DRIVER_VERSION "May Day 2005"
93 static const char shortname[] = "ether";
94 static const char driver_desc[] = DRIVER_DESC;
96 #define RX_EXTRA 20 /* guard against rx overflows */
98 /* CDC support the same host-chosen outgoing packet filters. */
99 #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
100 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
101 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
102 |USB_CDC_PACKET_TYPE_DIRECTED)
104 #define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
106 /*-------------------------------------------------------------------------*/
107 static struct eth_dev l_ethdev;
108 static struct eth_device l_netdev;
109 static struct usb_gadget_driver eth_driver;
111 /*-------------------------------------------------------------------------*/
113 /* "main" config is either CDC, or its simple subset */
114 static inline int is_cdc(struct eth_dev *dev)
116 #if !defined(DEV_CONFIG_SUBSET)
117 return 1; /* only cdc possible */
118 #elif !defined(DEV_CONFIG_CDC)
119 return 0; /* only subset possible */
121 return dev->cdc; /* depends on what hardware we found */
125 #define subset_active(dev) (!is_cdc(dev))
126 #define cdc_active(dev) (is_cdc(dev))
128 #define DEFAULT_QLEN 2 /* double buffering by default */
130 /* peak bulk transfer bits-per-second */
131 #define HS_BPS (13 * 512 * 8 * 1000 * 8)
132 #define FS_BPS (19 * 64 * 1 * 1000 * 8)
134 #ifdef CONFIG_USB_GADGET_DUALSPEED
135 #define DEVSPEED USB_SPEED_HIGH
137 #ifdef CONFIG_USB_ETH_QMULT
138 #define qmult CONFIG_USB_ETH_QMULT
143 /* for dual-speed hardware, use deeper queues at highspeed */
144 #define qlen(gadget) \
145 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
147 static inline int BITRATE(struct usb_gadget *g)
149 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
152 #else /* full speed (low speed doesn't do bulk) */
156 #define DEVSPEED USB_SPEED_FULL
158 #define qlen(gadget) DEFAULT_QLEN
160 static inline int BITRATE(struct usb_gadget *g)
167 struct usb_gadget *gadget;
168 struct usb_request *req; /* for control responses */
169 struct usb_request *stat_req; /* for cdc status */
172 struct usb_ep *in_ep, *out_ep, *status_ep;
173 const struct usb_endpoint_descriptor
176 struct usb_request *tx_req, *rx_req;
178 struct eth_device *net;
179 struct net_device_stats stats;
180 unsigned int tx_qlen;
184 unsigned suspended:1;
185 unsigned network_started:1;
189 #define WORK_RX_MEMORY 0
190 u8 host_mac[ETH_ALEN];
194 * This version autoconfigures as much as possible at run-time.
196 * It also ASSUMES a self-powered device, without remote wakeup,
197 * although remote wakeup support would make sense.
200 /*-------------------------------------------------------------------------*/
203 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
204 * Instead: allocate your own, using normal USB-IF procedures.
208 * Thanks to NetChip Technologies for donating this product ID.
209 * It's for devices with only CDC Ethernet configurations.
211 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
212 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
215 * For hardware that can't talk CDC, we use the same vendor ID that
216 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
217 * with pxa250. We're protocol-compatible, if the host-side drivers
218 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
219 * drivers that need to hard-wire endpoint numbers have a hook.
221 * The protocol is a minimal subset of CDC Ether, which works on any bulk
222 * hardware that's not deeply broken ... even on hardware that can't talk
223 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
224 * doesn't handle control-OUT).
226 #define SIMPLE_VENDOR_NUM 0x049f
227 #define SIMPLE_PRODUCT_NUM 0x505a
230 * Some systems will want different product identifers published in the
231 * device descriptor, either numbers or strings or both. These string
232 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
235 static ushort bcdDevice;
236 #if defined(CONFIG_USBNET_MANUFACTURER)
237 static char *iManufacturer = CONFIG_USBNET_MANUFACTURER;
239 static char *iManufacturer = "U-boot";
241 static char *iProduct;
242 static char *iSerialNumber;
243 static char dev_addr[18];
244 static char host_addr[18];
246 /*-------------------------------------------------------------------------*/
249 * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
250 * ep0 implementation: descriptors, config management, setup().
251 * also optional class-specific notification interrupt transfer.
255 * DESCRIPTORS ... most are static, but strings and (full) configuration
256 * descriptors are built on demand. For now we do either full CDC, or
260 #define STRING_MANUFACTURER 1
261 #define STRING_PRODUCT 2
262 #define STRING_ETHADDR 3
263 #define STRING_DATA 4
264 #define STRING_CONTROL 5
266 #define STRING_SUBSET 8
267 #define STRING_SERIALNUMBER 10
269 /* holds our biggest descriptor */
270 #define USB_BUFSIZ 256
273 * This device advertises one configuration, eth_config,
274 * on hardware supporting at least two configs.
276 * FIXME define some higher-powered configurations to make it easier
277 * to recharge batteries ...
280 #define DEV_CONFIG_VALUE 1 /* cdc or subset */
282 static struct usb_device_descriptor
284 .bLength = sizeof device_desc,
285 .bDescriptorType = USB_DT_DEVICE,
287 .bcdUSB = __constant_cpu_to_le16(0x0200),
289 .bDeviceClass = USB_CLASS_COMM,
290 .bDeviceSubClass = 0,
291 .bDeviceProtocol = 0,
293 .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
294 .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
295 .iManufacturer = STRING_MANUFACTURER,
296 .iProduct = STRING_PRODUCT,
297 .bNumConfigurations = 1,
300 static struct usb_otg_descriptor
302 .bLength = sizeof otg_descriptor,
303 .bDescriptorType = USB_DT_OTG,
305 .bmAttributes = USB_OTG_SRP,
308 static struct usb_config_descriptor
310 .bLength = sizeof eth_config,
311 .bDescriptorType = USB_DT_CONFIG,
313 /* compute wTotalLength on the fly */
315 .bConfigurationValue = DEV_CONFIG_VALUE,
316 .iConfiguration = STRING_CDC,
317 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
322 * Compared to the simple CDC subset, the full CDC Ethernet model adds
323 * three class descriptors, two interface descriptors, optional status
324 * endpoint. Both have a "data" interface and two bulk endpoints.
325 * There are also differences in how control requests are handled.
328 #ifdef DEV_CONFIG_CDC
329 static struct usb_interface_descriptor
331 .bLength = sizeof control_intf,
332 .bDescriptorType = USB_DT_INTERFACE,
334 .bInterfaceNumber = 0,
335 /* status endpoint is optional; this may be patched later */
337 .bInterfaceClass = USB_CLASS_COMM,
338 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
339 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
340 .iInterface = STRING_CONTROL,
344 static const struct usb_cdc_header_desc header_desc = {
345 .bLength = sizeof header_desc,
346 .bDescriptorType = USB_DT_CS_INTERFACE,
347 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
349 .bcdCDC = __constant_cpu_to_le16(0x0110),
352 #if defined(DEV_CONFIG_CDC)
354 static const struct usb_cdc_union_desc union_desc = {
355 .bLength = sizeof union_desc,
356 .bDescriptorType = USB_DT_CS_INTERFACE,
357 .bDescriptorSubType = USB_CDC_UNION_TYPE,
359 .bMasterInterface0 = 0, /* index of control interface */
360 .bSlaveInterface0 = 1, /* index of DATA interface */
365 #ifndef DEV_CONFIG_CDC
368 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
369 * ways: data endpoints live in the control interface, there's no data
370 * interface, and it's not used to talk to a cell phone radio.
373 static const struct usb_cdc_mdlm_desc mdlm_desc = {
374 .bLength = sizeof mdlm_desc,
375 .bDescriptorType = USB_DT_CS_INTERFACE,
376 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
378 .bcdVersion = __constant_cpu_to_le16(0x0100),
380 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
381 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
386 * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
387 * can't really use its struct. All we do here is say that we're using
388 * the submode of "SAFE" which directly matches the CDC Subset.
390 static const u8 mdlm_detail_desc[] = {
393 USB_CDC_MDLM_DETAIL_TYPE,
396 0, /* network control capabilities (none) */
397 0, /* network data capabilities ("raw" encapsulation) */
402 static const struct usb_cdc_ether_desc ether_desc = {
403 .bLength = sizeof(ether_desc),
404 .bDescriptorType = USB_DT_CS_INTERFACE,
405 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
407 /* this descriptor actually adds value, surprise! */
408 .iMACAddress = STRING_ETHADDR,
409 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
410 .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN),
411 .wNumberMCFilters = __constant_cpu_to_le16(0),
412 .bNumberPowerFilters = 0,
415 #if defined(DEV_CONFIG_CDC)
418 * include the status endpoint if we can, even where it's optional.
419 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
420 * packet, to simplify cancellation; and a big transfer interval, to
421 * waste less bandwidth.
423 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
424 * if they ignore the connect/disconnect notifications that real aether
425 * can provide. more advanced cdc configurations might want to support
426 * encapsulated commands (vendor-specific, using control-OUT).
429 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
430 #define STATUS_BYTECOUNT 16 /* 8 byte header + data */
432 static struct usb_endpoint_descriptor
434 .bLength = USB_DT_ENDPOINT_SIZE,
435 .bDescriptorType = USB_DT_ENDPOINT,
437 .bEndpointAddress = USB_DIR_IN,
438 .bmAttributes = USB_ENDPOINT_XFER_INT,
439 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
440 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
444 #ifdef DEV_CONFIG_CDC
446 /* the default data interface has no endpoints ... */
448 static const struct usb_interface_descriptor
450 .bLength = sizeof data_nop_intf,
451 .bDescriptorType = USB_DT_INTERFACE,
453 .bInterfaceNumber = 1,
454 .bAlternateSetting = 0,
456 .bInterfaceClass = USB_CLASS_CDC_DATA,
457 .bInterfaceSubClass = 0,
458 .bInterfaceProtocol = 0,
461 /* ... but the "real" data interface has two bulk endpoints */
463 static const struct usb_interface_descriptor
465 .bLength = sizeof data_intf,
466 .bDescriptorType = USB_DT_INTERFACE,
468 .bInterfaceNumber = 1,
469 .bAlternateSetting = 1,
471 .bInterfaceClass = USB_CLASS_CDC_DATA,
472 .bInterfaceSubClass = 0,
473 .bInterfaceProtocol = 0,
474 .iInterface = STRING_DATA,
479 #ifdef DEV_CONFIG_SUBSET
482 * "Simple" CDC-subset option is a simple vendor-neutral model that most
483 * full speed controllers can handle: one interface, two bulk endpoints.
485 * To assist host side drivers, we fancy it up a bit, and add descriptors
486 * so some host side drivers will understand it as a "SAFE" variant.
489 static const struct usb_interface_descriptor
491 .bLength = sizeof subset_data_intf,
492 .bDescriptorType = USB_DT_INTERFACE,
494 .bInterfaceNumber = 0,
495 .bAlternateSetting = 0,
497 .bInterfaceClass = USB_CLASS_COMM,
498 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
499 .bInterfaceProtocol = 0,
500 .iInterface = STRING_DATA,
505 static struct usb_endpoint_descriptor
507 .bLength = USB_DT_ENDPOINT_SIZE,
508 .bDescriptorType = USB_DT_ENDPOINT,
510 .bEndpointAddress = USB_DIR_IN,
511 .bmAttributes = USB_ENDPOINT_XFER_BULK,
514 static struct usb_endpoint_descriptor
516 .bLength = USB_DT_ENDPOINT_SIZE,
517 .bDescriptorType = USB_DT_ENDPOINT,
519 .bEndpointAddress = USB_DIR_OUT,
520 .bmAttributes = USB_ENDPOINT_XFER_BULK,
523 static const struct usb_descriptor_header *fs_eth_function[11] = {
524 (struct usb_descriptor_header *) &otg_descriptor,
525 #ifdef DEV_CONFIG_CDC
526 /* "cdc" mode descriptors */
527 (struct usb_descriptor_header *) &control_intf,
528 (struct usb_descriptor_header *) &header_desc,
529 (struct usb_descriptor_header *) &union_desc,
530 (struct usb_descriptor_header *) ðer_desc,
531 /* NOTE: status endpoint may need to be removed */
532 (struct usb_descriptor_header *) &fs_status_desc,
533 /* data interface, with altsetting */
534 (struct usb_descriptor_header *) &data_nop_intf,
535 (struct usb_descriptor_header *) &data_intf,
536 (struct usb_descriptor_header *) &fs_source_desc,
537 (struct usb_descriptor_header *) &fs_sink_desc,
539 #endif /* DEV_CONFIG_CDC */
542 static inline void fs_subset_descriptors(void)
544 #ifdef DEV_CONFIG_SUBSET
545 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
546 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
547 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
548 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
549 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
550 fs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
551 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
552 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
553 fs_eth_function[8] = NULL;
555 fs_eth_function[1] = NULL;
560 * usb 2.0 devices need to expose both high speed and full speed
561 * descriptors, unless they only run at full speed.
564 #if defined(DEV_CONFIG_CDC)
565 static struct usb_endpoint_descriptor
567 .bLength = USB_DT_ENDPOINT_SIZE,
568 .bDescriptorType = USB_DT_ENDPOINT,
570 .bmAttributes = USB_ENDPOINT_XFER_INT,
571 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
572 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
574 #endif /* DEV_CONFIG_CDC */
576 static struct usb_endpoint_descriptor
578 .bLength = USB_DT_ENDPOINT_SIZE,
579 .bDescriptorType = USB_DT_ENDPOINT,
581 .bmAttributes = USB_ENDPOINT_XFER_BULK,
582 .wMaxPacketSize = __constant_cpu_to_le16(512),
585 static struct usb_endpoint_descriptor
587 .bLength = USB_DT_ENDPOINT_SIZE,
588 .bDescriptorType = USB_DT_ENDPOINT,
590 .bmAttributes = USB_ENDPOINT_XFER_BULK,
591 .wMaxPacketSize = __constant_cpu_to_le16(512),
594 static struct usb_qualifier_descriptor
596 .bLength = sizeof dev_qualifier,
597 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
599 .bcdUSB = __constant_cpu_to_le16(0x0200),
600 .bDeviceClass = USB_CLASS_COMM,
602 .bNumConfigurations = 1,
605 static const struct usb_descriptor_header *hs_eth_function[11] = {
606 (struct usb_descriptor_header *) &otg_descriptor,
607 #ifdef DEV_CONFIG_CDC
608 /* "cdc" mode descriptors */
609 (struct usb_descriptor_header *) &control_intf,
610 (struct usb_descriptor_header *) &header_desc,
611 (struct usb_descriptor_header *) &union_desc,
612 (struct usb_descriptor_header *) ðer_desc,
613 /* NOTE: status endpoint may need to be removed */
614 (struct usb_descriptor_header *) &hs_status_desc,
615 /* data interface, with altsetting */
616 (struct usb_descriptor_header *) &data_nop_intf,
617 (struct usb_descriptor_header *) &data_intf,
618 (struct usb_descriptor_header *) &hs_source_desc,
619 (struct usb_descriptor_header *) &hs_sink_desc,
621 #endif /* DEV_CONFIG_CDC */
624 static inline void hs_subset_descriptors(void)
626 #ifdef DEV_CONFIG_SUBSET
627 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
628 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
629 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
630 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
631 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
632 hs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
633 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
634 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
635 hs_eth_function[8] = NULL;
637 hs_eth_function[1] = NULL;
641 /* maxpacket and other transfer characteristics vary by speed. */
642 static inline struct usb_endpoint_descriptor *
643 ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
644 struct usb_endpoint_descriptor *fs)
646 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
651 /*-------------------------------------------------------------------------*/
653 /* descriptors that are built on-demand */
655 static char manufacturer[50];
656 static char product_desc[40] = DRIVER_DESC;
657 static char serial_number[20];
659 /* address that the host will use ... usually assigned at random */
660 static char ethaddr[2 * ETH_ALEN + 1];
662 /* static strings, in UTF-8 */
663 static struct usb_string strings[] = {
664 { STRING_MANUFACTURER, manufacturer, },
665 { STRING_PRODUCT, product_desc, },
666 { STRING_SERIALNUMBER, serial_number, },
667 { STRING_DATA, "Ethernet Data", },
668 { STRING_ETHADDR, ethaddr, },
669 #ifdef DEV_CONFIG_CDC
670 { STRING_CDC, "CDC Ethernet", },
671 { STRING_CONTROL, "CDC Communications Control", },
673 #ifdef DEV_CONFIG_SUBSET
674 { STRING_SUBSET, "CDC Ethernet Subset", },
676 { } /* end of list */
679 static struct usb_gadget_strings stringtab = {
680 .language = 0x0409, /* en-us */
684 /*============================================================================*/
685 static u8 control_req[USB_BUFSIZ];
686 static u8 status_req[STATUS_BYTECOUNT] __attribute__ ((aligned(4)));
690 * strlcpy - Copy a %NUL terminated string into a sized buffer
691 * @dest: Where to copy the string to
692 * @src: Where to copy the string from
693 * @size: size of destination buffer
695 * Compatible with *BSD: the result is always a valid
696 * NUL-terminated string that fits in the buffer (unless,
697 * of course, the buffer size is zero). It does not pad
698 * out the result like strncpy() does.
700 size_t strlcpy(char *dest, const char *src, size_t size)
702 size_t ret = strlen(src);
705 size_t len = (ret >= size) ? size - 1 : ret;
706 memcpy(dest, src, len);
712 /*============================================================================*/
715 * one config, two interfaces: control, data.
716 * complications: class descriptors, and an altsetting.
719 config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
722 const struct usb_config_descriptor *config;
723 const struct usb_descriptor_header **function;
726 if (gadget_is_dualspeed(g)) {
727 hs = (g->speed == USB_SPEED_HIGH);
728 if (type == USB_DT_OTHER_SPEED_CONFIG)
731 #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
733 if (index >= device_desc.bNumConfigurations)
736 config = ð_config;
737 function = which_fn(eth);
739 /* for now, don't advertise srp-only devices */
743 len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
746 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
750 /*-------------------------------------------------------------------------*/
752 static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
755 set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
758 struct usb_gadget *gadget = dev->gadget;
760 #if defined(DEV_CONFIG_CDC)
761 /* status endpoint used for (optionally) CDC */
762 if (!subset_active(dev) && dev->status_ep) {
763 dev->status = ep_desc(gadget, &hs_status_desc,
765 dev->status_ep->driver_data = dev;
767 result = usb_ep_enable(dev->status_ep, dev->status);
769 debug("enable %s --> %d\n",
770 dev->status_ep->name, result);
776 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
777 dev->in_ep->driver_data = dev;
779 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
780 dev->out_ep->driver_data = dev;
783 * With CDC, the host isn't allowed to use these two data
784 * endpoints in the default altsetting for the interface.
785 * so we don't activate them yet. Reset from SET_INTERFACE.
787 if (!cdc_active(dev)) {
788 result = usb_ep_enable(dev->in_ep, dev->in);
790 debug("enable %s --> %d\n",
791 dev->in_ep->name, result);
795 result = usb_ep_enable(dev->out_ep, dev->out);
797 debug("enable %s --> %d\n",
798 dev->out_ep->name, result);
805 result = alloc_requests(dev, qlen(gadget), gfp_flags);
807 /* on error, disable any endpoints */
809 if (!subset_active(dev) && dev->status_ep)
810 (void) usb_ep_disable(dev->status_ep);
812 (void) usb_ep_disable(dev->in_ep);
813 (void) usb_ep_disable(dev->out_ep);
818 /* caller is responsible for cleanup on error */
822 static void eth_reset_config(struct eth_dev *dev)
824 if (dev->config == 0)
827 debug("%s\n", __func__);
830 * disable endpoints, forcing (synchronous) completion of
831 * pending i/o. then free the requests.
835 usb_ep_disable(dev->in_ep);
837 usb_ep_free_request(dev->in_ep, dev->tx_req);
842 usb_ep_disable(dev->out_ep);
844 usb_ep_free_request(dev->out_ep, dev->rx_req);
849 usb_ep_disable(dev->status_ep);
856 * change our operational config. must agree with the code
857 * that returns config descriptors, and altsetting code.
859 static int eth_set_config(struct eth_dev *dev, unsigned number,
863 struct usb_gadget *gadget = dev->gadget;
865 if (gadget_is_sa1100(gadget)
867 && dev->tx_qlen != 0) {
868 /* tx fifo is full, but we can't clear it...*/
869 error("can't change configurations");
872 eth_reset_config(dev);
875 case DEV_CONFIG_VALUE:
876 result = set_ether_config(dev, gfp_flags);
887 eth_reset_config(dev);
888 usb_gadget_vbus_draw(dev->gadget,
889 gadget_is_otg(dev->gadget) ? 8 : 100);
894 power = 2 * eth_config.bMaxPower;
895 usb_gadget_vbus_draw(dev->gadget, power);
897 switch (gadget->speed) {
899 speed = "full"; break;
900 #ifdef CONFIG_USB_GADGET_DUALSPEED
902 speed = "high"; break;
908 dev->config = number;
909 printf("%s speed config #%d: %d mA, %s, using %s\n",
910 speed, number, power, driver_desc,
911 (cdc_active(dev) ? "CDC Ethernet"
912 : "CDC Ethernet Subset"));
917 /*-------------------------------------------------------------------------*/
919 #ifdef DEV_CONFIG_CDC
922 * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
923 * only to notify the host about link status changes (which we support) or
924 * report completion of some encapsulated command. Since
925 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
926 * command mechanism; and only one status request is ever queued.
928 static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
930 struct usb_cdc_notification *event = req->buf;
931 int value = req->status;
932 struct eth_dev *dev = ep->driver_data;
934 /* issue the second notification if host reads the first */
935 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
937 __le32 *data = req->buf + sizeof *event;
939 event->bmRequestType = 0xA1;
940 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
941 event->wValue = __constant_cpu_to_le16(0);
942 event->wIndex = __constant_cpu_to_le16(1);
943 event->wLength = __constant_cpu_to_le16(8);
945 /* SPEED_CHANGE data is up/down speeds in bits/sec */
946 data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
948 req->length = STATUS_BYTECOUNT;
949 value = usb_ep_queue(ep, req, GFP_ATOMIC);
950 debug("send SPEED_CHANGE --> %d\n", value);
953 } else if (value != -ECONNRESET) {
954 debug("event %02x --> %d\n",
955 event->bNotificationType, value);
956 if (event->bNotificationType ==
957 USB_CDC_NOTIFY_SPEED_CHANGE) {
958 l_ethdev.network_started = 1;
959 printf("USB network up!\n");
965 static void issue_start_status(struct eth_dev *dev)
967 struct usb_request *req = dev->stat_req;
968 struct usb_cdc_notification *event;
974 * FIXME ugly idiom, maybe we'd be better with just
975 * a "cancel the whole queue" primitive since any
976 * unlink-one primitive has way too many error modes.
977 * here, we "know" toggle is already clear...
979 * FIXME iff req->context != null just dequeue it
981 usb_ep_disable(dev->status_ep);
982 usb_ep_enable(dev->status_ep, dev->status);
985 * 3.8.1 says to issue first NETWORK_CONNECTION, then
986 * a SPEED_CHANGE. could be useful in some configs.
989 event->bmRequestType = 0xA1;
990 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
991 event->wValue = __constant_cpu_to_le16(1); /* connected */
992 event->wIndex = __constant_cpu_to_le16(1);
995 req->length = sizeof *event;
996 req->complete = eth_status_complete;
999 value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
1001 debug("status buf queue --> %d\n", value);
1006 /*-------------------------------------------------------------------------*/
1008 static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
1010 if (req->status || req->actual != req->length)
1011 debug("setup complete --> %d, %d/%d\n",
1012 req->status, req->actual, req->length);
1016 * The setup() callback implements all the ep0 functionality that's not
1017 * handled lower down. CDC has a number of less-common features:
1019 * - two interfaces: control, and ethernet data
1020 * - Ethernet data interface has two altsettings: default, and active
1021 * - class-specific descriptors for the control interface
1022 * - class-specific control requests
1025 eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1027 struct eth_dev *dev = get_gadget_data(gadget);
1028 struct usb_request *req = dev->req;
1029 int value = -EOPNOTSUPP;
1030 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1031 u16 wValue = le16_to_cpu(ctrl->wValue);
1032 u16 wLength = le16_to_cpu(ctrl->wLength);
1035 * descriptors just go into the pre-allocated ep0 buffer,
1036 * while config change events may enable network traffic.
1039 debug("%s\n", __func__);
1041 req->complete = eth_setup_complete;
1042 switch (ctrl->bRequest) {
1044 case USB_REQ_GET_DESCRIPTOR:
1045 if (ctrl->bRequestType != USB_DIR_IN)
1047 switch (wValue >> 8) {
1050 value = min(wLength, (u16) sizeof device_desc);
1051 memcpy(req->buf, &device_desc, value);
1053 case USB_DT_DEVICE_QUALIFIER:
1054 if (!gadget_is_dualspeed(gadget))
1056 value = min(wLength, (u16) sizeof dev_qualifier);
1057 memcpy(req->buf, &dev_qualifier, value);
1060 case USB_DT_OTHER_SPEED_CONFIG:
1061 if (!gadget_is_dualspeed(gadget))
1065 value = config_buf(gadget, req->buf,
1068 gadget_is_otg(gadget));
1070 value = min(wLength, (u16) value);
1074 value = usb_gadget_get_string(&stringtab,
1075 wValue & 0xff, req->buf);
1078 value = min(wLength, (u16) value);
1084 case USB_REQ_SET_CONFIGURATION:
1085 if (ctrl->bRequestType != 0)
1087 if (gadget->a_hnp_support)
1088 debug("HNP available\n");
1089 else if (gadget->a_alt_hnp_support)
1090 debug("HNP needs a different root port\n");
1091 value = eth_set_config(dev, wValue, GFP_ATOMIC);
1093 case USB_REQ_GET_CONFIGURATION:
1094 if (ctrl->bRequestType != USB_DIR_IN)
1096 *(u8 *)req->buf = dev->config;
1097 value = min(wLength, (u16) 1);
1100 case USB_REQ_SET_INTERFACE:
1101 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1105 if (!cdc_active(dev) && wIndex != 0)
1109 * PXA hardware partially handles SET_INTERFACE;
1110 * we need to kluge around that interference.
1112 if (gadget_is_pxa(gadget)) {
1113 value = eth_set_config(dev, DEV_CONFIG_VALUE,
1118 #ifdef DEV_CONFIG_CDC
1120 case 0: /* control/master intf */
1124 usb_ep_disable(dev->status_ep);
1125 usb_ep_enable(dev->status_ep, dev->status);
1129 case 1: /* data intf */
1132 usb_ep_disable(dev->in_ep);
1133 usb_ep_disable(dev->out_ep);
1136 * CDC requires the data transfers not be done from
1137 * the default interface setting ... also, setting
1138 * the non-default interface resets filters etc.
1141 if (!cdc_active(dev))
1143 usb_ep_enable(dev->in_ep, dev->in);
1144 usb_ep_enable(dev->out_ep, dev->out);
1145 dev->cdc_filter = DEFAULT_FILTER;
1147 issue_start_status(dev);
1155 * FIXME this is wrong, as is the assumption that
1156 * all non-PXA hardware talks real CDC ...
1158 debug("set_interface ignored!\n");
1159 #endif /* DEV_CONFIG_CDC */
1163 case USB_REQ_GET_INTERFACE:
1164 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1168 if (!(cdc_active(dev)) && wIndex != 0)
1171 /* for CDC, iff carrier is on, data interface is active. */
1173 *(u8 *)req->buf = 0;
1175 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1176 /* carrier always ok ...*/
1177 *(u8 *)req->buf = 1 ;
1179 value = min(wLength, (u16) 1);
1182 #ifdef DEV_CONFIG_CDC
1183 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
1185 * see 6.2.30: no data, wIndex = interface,
1186 * wValue = packet filter bitmap
1188 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1193 debug("packet filter %02x\n", wValue);
1194 dev->cdc_filter = wValue;
1200 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1201 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1202 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1203 * case USB_CDC_GET_ETHERNET_STATISTIC:
1206 #endif /* DEV_CONFIG_CDC */
1209 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
1210 ctrl->bRequestType, ctrl->bRequest,
1211 wValue, wIndex, wLength);
1214 /* respond with data transfer before status phase? */
1216 debug("respond with data transfer before status phase\n");
1217 req->length = value;
1218 req->zero = value < wLength
1219 && (value % gadget->ep0->maxpacket) == 0;
1220 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1222 debug("ep_queue --> %d\n", value);
1224 eth_setup_complete(gadget->ep0, req);
1228 /* host either stalls (value < 0) or reports success */
1232 /*-------------------------------------------------------------------------*/
1234 static void rx_complete(struct usb_ep *ep, struct usb_request *req);
1236 static int rx_submit(struct eth_dev *dev, struct usb_request *req,
1239 int retval = -ENOMEM;
1243 * Padding up to RX_EXTRA handles minor disagreements with host.
1244 * Normally we use the USB "terminate on short read" convention;
1245 * so allow up to (N*maxpacket), since that memory is normally
1246 * already allocated. Some hardware doesn't deal well with short
1247 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1248 * byte off the end (to force hardware errors on overflow).
1251 debug("%s\n", __func__);
1253 size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1254 size += dev->out_ep->maxpacket - 1;
1255 size -= size % dev->out_ep->maxpacket;
1258 * Some platforms perform better when IP packets are aligned,
1259 * but on at least one, checksumming fails otherwise.
1262 req->buf = (u8 *) NetRxPackets[0];
1264 req->complete = rx_complete;
1266 retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
1269 error("rx submit --> %d", retval);
1274 static void rx_complete(struct usb_ep *ep, struct usb_request *req)
1276 struct eth_dev *dev = ep->driver_data;
1278 debug("%s: status %d\n", __func__, req->status);
1279 switch (req->status) {
1280 /* normal completion */
1282 dev->stats.rx_packets++;
1283 dev->stats.rx_bytes += req->length;
1286 /* software-driven interface shutdown */
1287 case -ECONNRESET: /* unlink */
1288 case -ESHUTDOWN: /* disconnect etc */
1289 /* for hardware automagic (such as pxa) */
1290 case -ECONNABORTED: /* endpoint reset */
1295 dev->stats.rx_over_errors++;
1298 dev->stats.rx_errors++;
1302 packet_received = 1;
1305 static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
1308 dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
1313 dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
1321 usb_ep_free_request(dev->in_ep, dev->tx_req);
1323 error("can't alloc requests");
1327 static void tx_complete(struct usb_ep *ep, struct usb_request *req)
1329 struct eth_dev *dev = ep->driver_data;
1331 debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
1332 switch (req->status) {
1334 dev->stats.tx_errors++;
1335 debug("tx err %d\n", req->status);
1337 case -ECONNRESET: /* unlink */
1338 case -ESHUTDOWN: /* disconnect etc */
1341 dev->stats.tx_bytes += req->length;
1343 dev->stats.tx_packets++;
1348 static inline int eth_is_promisc(struct eth_dev *dev)
1350 /* no filters for the CDC subset; always promisc */
1351 if (subset_active(dev))
1353 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1357 static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1359 struct eth_dev *dev = netdev_priv(net);
1360 int length = skb->len;
1362 struct usb_request *req = NULL;
1363 unsigned long flags;
1365 /* apply outgoing CDC or RNDIS filters */
1366 if (!eth_is_promisc (dev)) {
1367 u8 *dest = skb->data;
1369 if (is_multicast_ether_addr(dest)) {
1372 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1373 * SET_ETHERNET_MULTICAST_FILTERS requests
1375 if (is_broadcast_ether_addr(dest))
1376 type = USB_CDC_PACKET_TYPE_BROADCAST;
1378 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1379 if (!(dev->cdc_filter & type)) {
1380 dev_kfree_skb_any (skb);
1384 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1387 spin_lock_irqsave(&dev->req_lock, flags);
1389 * this freelist can be empty if an interrupt triggered disconnect()
1390 * and reconfigured the gadget (shutting down this queue) after the
1391 * network stack decided to xmit but before we got the spinlock.
1393 if (list_empty(&dev->tx_reqs)) {
1394 spin_unlock_irqrestore(&dev->req_lock, flags);
1398 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1399 list_del (&req->list);
1401 /* temporarily stop TX queue when the freelist empties */
1402 if (list_empty (&dev->tx_reqs))
1403 netif_stop_queue (net);
1404 spin_unlock_irqrestore(&dev->req_lock, flags);
1406 /* no buffer copies needed, unless the network stack did it
1407 * or the hardware can't use skb buffers.
1408 * or there's not enough space for any RNDIS headers we need
1410 if (rndis_active(dev)) {
1411 struct sk_buff *skb_rndis;
1413 skb_rndis = skb_realloc_headroom (skb,
1414 sizeof (struct rndis_packet_msg_type));
1418 dev_kfree_skb_any (skb);
1420 rndis_add_hdr (skb);
1423 req->buf = skb->data;
1425 req->complete = tx_complete;
1427 /* use zlp framing on tx for strict CDC-Ether conformance,
1428 * though any robust network rx path ignores extra padding.
1429 * and some hardware doesn't like to write zlps.
1432 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1435 req->length = length;
1437 /* throttle highspeed IRQ rate back slightly */
1438 if (gadget_is_dualspeed(dev->gadget))
1439 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1440 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1443 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1446 DEBUG (dev, "tx queue err %d\n", retval);
1449 net->trans_start = jiffies;
1450 atomic_inc (&dev->tx_qlen);
1455 dev->stats.tx_dropped++;
1456 dev_kfree_skb_any (skb);
1457 spin_lock_irqsave(&dev->req_lock, flags);
1458 if (list_empty (&dev->tx_reqs))
1459 netif_start_queue (net);
1460 list_add (&req->list, &dev->tx_reqs);
1461 spin_unlock_irqrestore(&dev->req_lock, flags);
1466 /*-------------------------------------------------------------------------*/
1469 static void eth_unbind(struct usb_gadget *gadget)
1471 struct eth_dev *dev = get_gadget_data(gadget);
1473 debug("%s...\n", __func__);
1475 /* we've already been disconnected ... no i/o is active */
1477 usb_ep_free_request(gadget->ep0, dev->req);
1480 if (dev->stat_req) {
1481 usb_ep_free_request(dev->status_ep, dev->stat_req);
1482 dev->stat_req = NULL;
1486 usb_ep_free_request(dev->in_ep, dev->tx_req);
1491 usb_ep_free_request(dev->out_ep, dev->rx_req);
1495 /* unregister_netdev (dev->net);*/
1496 /* free_netdev(dev->net);*/
1499 set_gadget_data(gadget, NULL);
1502 static void eth_disconnect(struct usb_gadget *gadget)
1504 eth_reset_config(get_gadget_data(gadget));
1507 static void eth_suspend(struct usb_gadget *gadget)
1512 static void eth_resume(struct usb_gadget *gadget)
1517 /*-------------------------------------------------------------------------*/
1519 static int is_eth_addr_valid(char *str)
1521 if (strlen(str) == 17) {
1526 /* see if it looks like an ethernet address */
1530 for (i = 0; i < 6; i++) {
1531 char term = (i == 5 ? '\0' : ':');
1533 ea[i] = simple_strtol(p, &q, 16);
1535 if ((q - p) != 2 || *q++ != term)
1541 if (i == 6) /* it looks ok */
1547 static u8 nibble(unsigned char c)
1549 if (likely(isdigit(c)))
1552 if (likely(isxdigit(c)))
1553 return 10 + c - 'A';
1557 static int get_ether_addr(const char *str, u8 *dev_addr)
1562 for (i = 0; i < 6; i++) {
1565 if ((*str == '.') || (*str == ':'))
1567 num = nibble(*str++) << 4;
1568 num |= (nibble(*str++));
1571 if (is_valid_ether_addr(dev_addr))
1577 static int eth_bind(struct usb_gadget *gadget)
1579 struct eth_dev *dev = &l_ethdev;
1580 u8 cdc = 1, zlp = 1;
1581 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
1585 /* these flags are only ever cleared; compiler take note */
1586 #ifndef DEV_CONFIG_CDC
1590 * Because most host side USB stacks handle CDC Ethernet, that
1591 * standard protocol is _strongly_ preferred for interop purposes.
1592 * (By everyone except Microsoft.)
1594 if (gadget_is_pxa(gadget)) {
1595 /* pxa doesn't support altsettings */
1597 } else if (gadget_is_musbhdrc(gadget)) {
1598 /* reduce tx dma overhead by avoiding special cases */
1600 } else if (gadget_is_sh(gadget)) {
1601 /* sh doesn't support multiple interfaces or configs */
1603 } else if (gadget_is_sa1100(gadget)) {
1604 /* hardware can't write zlps */
1607 * sa1100 CAN do CDC, without status endpoint ... we use
1608 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
1613 gcnum = usb_gadget_controller_number(gadget);
1615 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
1618 * can't assume CDC works. don't want to default to
1619 * anything less functional on CDC-capable hardware,
1620 * so we fail in this case.
1622 error("controller '%s' not recognized",
1628 * CDC subset ... recognized by Linux since 2.4.10, but Windows
1629 * drivers aren't widely available. (That may be improved by
1630 * supporting one submode of the "SAFE" variant of MDLM.)
1633 device_desc.idVendor =
1634 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
1635 device_desc.idProduct =
1636 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
1639 /* support optional vendor/distro customization */
1640 #if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID)
1641 device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID);
1642 device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID);
1645 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
1647 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
1649 strlcpy(product_desc, iProduct, sizeof product_desc);
1650 if (iSerialNumber) {
1651 device_desc.iSerialNumber = STRING_SERIALNUMBER,
1652 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
1655 /* all we really need is bulk IN/OUT */
1656 usb_ep_autoconfig_reset(gadget);
1657 in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
1660 error("can't autoconfigure on %s\n",
1664 in_ep->driver_data = in_ep; /* claim */
1666 out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
1669 out_ep->driver_data = out_ep; /* claim */
1671 #if defined(DEV_CONFIG_CDC)
1673 * CDC Ethernet control interface doesn't require a status endpoint.
1674 * Since some hosts expect one, try to allocate one anyway.
1677 status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
1679 status_ep->driver_data = status_ep; /* claim */
1681 control_intf.bNumEndpoints = 0;
1682 /* FIXME remove endpoint from descriptor list */
1687 /* one config: cdc, else minimal subset */
1689 eth_config.bNumInterfaces = 1;
1690 eth_config.iConfiguration = STRING_SUBSET;
1693 * use functions to set these up, in case we're built to work
1694 * with multiple controllers and must override CDC Ethernet.
1696 fs_subset_descriptors();
1697 hs_subset_descriptors();
1700 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1701 usb_gadget_set_selfpowered(gadget);
1703 if (gadget_is_dualspeed(gadget)) {
1705 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
1707 /* assumes ep0 uses the same value for both speeds ... */
1708 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
1710 /* and that all endpoints are dual-speed */
1711 hs_source_desc.bEndpointAddress =
1712 fs_source_desc.bEndpointAddress;
1713 hs_sink_desc.bEndpointAddress =
1714 fs_sink_desc.bEndpointAddress;
1715 #if defined(DEV_CONFIG_CDC)
1717 hs_status_desc.bEndpointAddress =
1718 fs_status_desc.bEndpointAddress;
1722 if (gadget_is_otg(gadget)) {
1723 otg_descriptor.bmAttributes |= USB_OTG_HNP,
1724 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1725 eth_config.bMaxPower = 4;
1728 dev->net = &l_netdev;
1734 dev->out_ep = out_ep;
1735 dev->status_ep = status_ep;
1738 * Module params for these addresses should come from ID proms.
1739 * The host side address is used with CDC, and commonly
1740 * ends up in a persistent config database. It's not clear if
1741 * host side code for the SAFE thing cares -- its original BLAN
1742 * thing didn't, Sharp never assigned those addresses on Zaurii.
1744 get_ether_addr(dev_addr, dev->net->enetaddr);
1746 memset(tmp, 0, sizeof(tmp));
1747 memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
1749 get_ether_addr(host_addr, dev->host_mac);
1751 sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
1752 dev->host_mac[0], dev->host_mac[1],
1753 dev->host_mac[2], dev->host_mac[3],
1754 dev->host_mac[4], dev->host_mac[5]);
1756 printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
1757 out_ep->name, in_ep->name,
1758 status_ep ? " STATUS " : "",
1759 status_ep ? status_ep->name : ""
1761 printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
1762 dev->net->enetaddr[0], dev->net->enetaddr[1],
1763 dev->net->enetaddr[2], dev->net->enetaddr[3],
1764 dev->net->enetaddr[4], dev->net->enetaddr[5]);
1767 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
1768 dev->host_mac[0], dev->host_mac[1],
1769 dev->host_mac[2], dev->host_mac[3],
1770 dev->host_mac[4], dev->host_mac[5]);
1774 * use PKTSIZE (or aligned... from u-boot) and set
1775 * wMaxSegmentSize accordingly
1777 dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
1779 /* preallocate control message data and buffer */
1780 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1783 dev->req->buf = control_req;
1784 dev->req->complete = eth_setup_complete;
1786 /* ... and maybe likewise for status transfer */
1787 #if defined(DEV_CONFIG_CDC)
1788 if (dev->status_ep) {
1789 dev->stat_req = usb_ep_alloc_request(dev->status_ep,
1791 if (!dev->stat_req) {
1792 usb_ep_free_request(dev->status_ep, dev->req);
1796 dev->stat_req->buf = status_req;
1797 dev->stat_req->context = NULL;
1801 /* finish hookup to lower layer ... */
1802 dev->gadget = gadget;
1803 set_gadget_data(gadget, dev);
1804 gadget->ep0->driver_data = dev;
1807 * two kinds of host-initiated state changes:
1808 * - iff DATA transfer is active, carrier is "on"
1809 * - tx queueing enabled if open *and* carrier is "on"
1814 error("%s failed", __func__);
1819 static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
1821 struct eth_dev *dev = &l_ethdev;
1822 struct usb_gadget *gadget;
1824 unsigned long timeout = USB_CONNECT_TIMEOUT;
1827 error("received NULL ptr");
1831 /* Configure default mac-addresses for the USB ethernet device */
1832 #ifdef CONFIG_USBNET_DEV_ADDR
1833 strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
1835 #ifdef CONFIG_USBNET_HOST_ADDR
1836 strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
1838 /* Check if the user overruled the MAC addresses */
1839 if (getenv("usbnet_devaddr"))
1840 strlcpy(dev_addr, getenv("usbnet_devaddr"),
1843 if (getenv("usbnet_hostaddr"))
1844 strlcpy(host_addr, getenv("usbnet_hostaddr"),
1847 if (!is_eth_addr_valid(dev_addr)) {
1848 error("Need valid 'usbnet_devaddr' to be set");
1851 if (!is_eth_addr_valid(host_addr)) {
1852 error("Need valid 'usbnet_hostaddr' to be set");
1856 if (usb_gadget_register_driver(ð_driver) < 0)
1859 dev->network_started = 0;
1861 packet_received = 0;
1864 gadget = dev->gadget;
1865 usb_gadget_connect(gadget);
1867 if (getenv("cdc_connect_timeout"))
1868 timeout = simple_strtoul(getenv("cdc_connect_timeout"),
1869 NULL, 10) * CONFIG_SYS_HZ;
1871 while (!l_ethdev.network_started) {
1872 /* Handle control-c and timeouts */
1873 if (ctrlc() || (get_timer(ts) > timeout)) {
1874 error("The remote end did not respond in time.");
1877 usb_gadget_handle_interrupts();
1880 packet_received = 0;
1881 rx_submit(dev, dev->rx_req, 0);
1887 static int usb_eth_send(struct eth_device *netdev,
1888 volatile void *packet, int length)
1891 struct eth_dev *dev = &l_ethdev;
1892 struct usb_request *req = dev->tx_req;
1894 unsigned long timeout = USB_CONNECT_TIMEOUT;
1896 debug("%s:...\n", __func__);
1898 req->buf = (void *)packet;
1899 req->context = NULL;
1900 req->complete = tx_complete;
1903 * use zlp framing on tx for strict CDC-Ether conformance,
1904 * though any robust network rx path ignores extra padding.
1905 * and some hardware doesn't like to write zlps.
1908 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1911 req->length = length;
1913 /* throttle highspeed IRQ rate back slightly */
1914 if (gadget_is_dualspeed(dev->gadget))
1915 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1916 ? ((dev->tx_qlen % qmult) != 0) : 0;
1922 retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
1925 debug("%s: packet queued\n", __func__);
1926 while (!packet_sent) {
1927 if (get_timer(ts) > timeout) {
1928 printf("timeout sending packets to usb ethernet\n");
1931 usb_gadget_handle_interrupts();
1937 static int usb_eth_recv(struct eth_device *netdev)
1939 struct eth_dev *dev = &l_ethdev;
1941 usb_gadget_handle_interrupts();
1943 if (packet_received) {
1944 debug("%s: packet received\n", __func__);
1946 NetReceive(NetRxPackets[0], dev->rx_req->length);
1947 packet_received = 0;
1949 rx_submit(dev, dev->rx_req, 0);
1951 error("dev->rx_req invalid");
1956 void usb_eth_halt(struct eth_device *netdev)
1958 struct eth_dev *dev = &l_ethdev;
1961 error("received NULL ptr");
1965 /* If the gadget not registered, simple return */
1969 usb_gadget_disconnect(dev->gadget);
1971 /* Clear pending interrupt */
1972 if (dev->network_started) {
1973 usb_gadget_handle_interrupts();
1974 dev->network_started = 0;
1977 usb_gadget_unregister_driver(ð_driver);
1980 static struct usb_gadget_driver eth_driver = {
1984 .unbind = eth_unbind,
1987 .disconnect = eth_disconnect,
1989 .suspend = eth_suspend,
1990 .resume = eth_resume,
1993 int usb_eth_initialize(bd_t *bi)
1995 struct eth_device *netdev = &l_netdev;
1997 strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
1999 netdev->init = usb_eth_init;
2000 netdev->send = usb_eth_send;
2001 netdev->recv = usb_eth_recv;
2002 netdev->halt = usb_eth_halt;
2004 #ifdef CONFIG_MCAST_TFTP
2005 #error not supported
2007 eth_register(netdev);