2 * ether.c -- Ethernet gadget driver, with CDC and non-CDC options
4 * Copyright (C) 2003-2005 David Brownell
5 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/delay.h>
29 #include <linux/ioport.h>
30 #include <linux/sched.h>
31 #include <linux/slab.h>
32 #include <linux/smp_lock.h>
33 #include <linux/errno.h>
34 #include <linux/init.h>
35 #include <linux/timer.h>
36 #include <linux/list.h>
37 #include <linux/interrupt.h>
38 #include <linux/utsname.h>
39 #include <linux/device.h>
40 #include <linux/moduleparam.h>
41 #include <linux/ctype.h>
43 #include <asm/byteorder.h>
46 #include <asm/system.h>
47 #include <asm/uaccess.h>
48 #include <asm/unaligned.h>
50 #include <linux/usb/ch9.h>
51 #include <linux/usb/cdc.h>
52 #include <linux/usb_gadget.h>
54 #include <linux/random.h>
55 #include <linux/netdevice.h>
56 #include <linux/etherdevice.h>
57 #include <linux/ethtool.h>
59 #include "gadget_chips.h"
61 /*-------------------------------------------------------------------------*/
64 * Ethernet gadget driver -- with CDC and non-CDC options
65 * Builds on hardware support for a full duplex link.
67 * CDC Ethernet is the standard USB solution for sending Ethernet frames
68 * using USB. Real hardware tends to use the same framing protocol but look
69 * different for control features. This driver strongly prefers to use
70 * this USB-IF standard as its open-systems interoperability solution;
71 * most host side USB stacks (except from Microsoft) support it.
73 * There's some hardware that can't talk CDC. We make that hardware
74 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
75 * link-level setup only requires activating the configuration. Only the
76 * endpoint descriptors, and product/vendor IDs, are relevant; no control
77 * operations are available. Linux supports it, but other host operating
78 * systems may not. (This is a subset of CDC Ethernet.)
80 * It turns out that if you add a few descriptors to that "CDC Subset",
81 * (Windows) host side drivers from MCCI can treat it as one submode of
82 * a proprietary scheme called "SAFE" ... without needing to know about
83 * specific product/vendor IDs. So we do that, making it easier to use
84 * those MS-Windows drivers. Those added descriptors make it resemble a
85 * CDC MDLM device, but they don't change device behavior at all. (See
86 * MCCI Engineering report 950198 "SAFE Networking Functions".)
88 * A third option is also in use. Rather than CDC Ethernet, or something
89 * simpler, Microsoft pushes their own approach: RNDIS. The published
90 * RNDIS specs are ambiguous and appear to be incomplete, and are also
94 #define DRIVER_DESC "Ethernet Gadget"
95 #define DRIVER_VERSION "May Day 2005"
97 static const char shortname [] = "ether";
98 static const char driver_desc [] = DRIVER_DESC;
100 #define RX_EXTRA 20 /* guard against rx overflows */
104 #ifndef CONFIG_USB_ETH_RNDIS
105 #define rndis_uninit(x) do{}while(0)
106 #define rndis_deregister(c) do{}while(0)
107 #define rndis_exit() do{}while(0)
110 /* CDC and RNDIS support the same host-chosen outgoing packet filters. */
111 #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
112 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
113 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
114 |USB_CDC_PACKET_TYPE_DIRECTED)
117 /*-------------------------------------------------------------------------*/
121 struct usb_gadget *gadget;
122 struct usb_request *req; /* for control responses */
123 struct usb_request *stat_req; /* for cdc & rndis status */
126 struct usb_ep *in_ep, *out_ep, *status_ep;
127 const struct usb_endpoint_descriptor
131 struct list_head tx_reqs, rx_reqs;
133 struct net_device *net;
134 struct net_device_stats stats;
137 struct work_struct work;
141 unsigned suspended:1;
144 #define WORK_RX_MEMORY 0
146 u8 host_mac [ETH_ALEN];
149 /* This version autoconfigures as much as possible at run-time.
151 * It also ASSUMES a self-powered device, without remote wakeup,
152 * although remote wakeup support would make sense.
155 /*-------------------------------------------------------------------------*/
157 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
158 * Instead: allocate your own, using normal USB-IF procedures.
161 /* Thanks to NetChip Technologies for donating this product ID.
162 * It's for devices with only CDC Ethernet configurations.
164 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
165 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
167 /* For hardware that can't talk CDC, we use the same vendor ID that
168 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
169 * with pxa250. We're protocol-compatible, if the host-side drivers
170 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
171 * drivers that need to hard-wire endpoint numbers have a hook.
173 * The protocol is a minimal subset of CDC Ether, which works on any bulk
174 * hardware that's not deeply broken ... even on hardware that can't talk
175 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
176 * doesn't handle control-OUT).
178 #define SIMPLE_VENDOR_NUM 0x049f
179 #define SIMPLE_PRODUCT_NUM 0x505a
181 /* For hardware that can talk RNDIS and either of the above protocols,
182 * use this ID ... the windows INF files will know it. Unless it's
183 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
184 * the non-RNDIS configuration.
186 #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
187 #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
190 /* Some systems will want different product identifers published in the
191 * device descriptor, either numbers or strings or both. These string
192 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
195 static ushort idVendor;
196 module_param(idVendor, ushort, S_IRUGO);
197 MODULE_PARM_DESC(idVendor, "USB Vendor ID");
199 static ushort idProduct;
200 module_param(idProduct, ushort, S_IRUGO);
201 MODULE_PARM_DESC(idProduct, "USB Product ID");
203 static ushort bcdDevice;
204 module_param(bcdDevice, ushort, S_IRUGO);
205 MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
207 static char *iManufacturer;
208 module_param(iManufacturer, charp, S_IRUGO);
209 MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
211 static char *iProduct;
212 module_param(iProduct, charp, S_IRUGO);
213 MODULE_PARM_DESC(iProduct, "USB Product string");
215 static char *iSerialNumber;
216 module_param(iSerialNumber, charp, S_IRUGO);
217 MODULE_PARM_DESC(iSerialNumber, "SerialNumber");
219 /* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */
220 static char *dev_addr;
221 module_param(dev_addr, charp, S_IRUGO);
222 MODULE_PARM_DESC(dev_addr, "Device Ethernet Address");
224 /* this address is invisible to ifconfig */
225 static char *host_addr;
226 module_param(host_addr, charp, S_IRUGO);
227 MODULE_PARM_DESC(host_addr, "Host Ethernet Address");
230 /*-------------------------------------------------------------------------*/
232 /* Include CDC support if we could run on CDC-capable hardware. */
234 #ifdef CONFIG_USB_GADGET_NET2280
235 #define DEV_CONFIG_CDC
238 #ifdef CONFIG_USB_GADGET_DUMMY_HCD
239 #define DEV_CONFIG_CDC
242 #ifdef CONFIG_USB_GADGET_GOKU
243 #define DEV_CONFIG_CDC
246 #ifdef CONFIG_USB_GADGET_LH7A40X
247 #define DEV_CONFIG_CDC
250 #ifdef CONFIG_USB_GADGET_MQ11XX
251 #define DEV_CONFIG_CDC
254 #ifdef CONFIG_USB_GADGET_OMAP
255 #define DEV_CONFIG_CDC
258 #ifdef CONFIG_USB_GADGET_N9604
259 #define DEV_CONFIG_CDC
262 #ifdef CONFIG_USB_GADGET_PXA27X
263 #define DEV_CONFIG_CDC
266 #ifdef CONFIG_USB_GADGET_S3C2410
267 #define DEV_CONFIG_CDC
270 #ifdef CONFIG_USB_GADGET_AT91
271 #define DEV_CONFIG_CDC
274 #ifdef CONFIG_USB_GADGET_MUSBHSFC
275 #define DEV_CONFIG_CDC
278 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
279 #define DEV_CONFIG_CDC
282 #ifdef CONFIG_USB_GADGET_HUSB2DEV
283 #define DEV_CONFIG_CDC
287 /* For CDC-incapable hardware, choose the simple cdc subset.
288 * Anything that talks bulk (without notable bugs) can do this.
290 #ifdef CONFIG_USB_GADGET_PXA2XX
291 #define DEV_CONFIG_SUBSET
294 #ifdef CONFIG_USB_GADGET_SH
295 #define DEV_CONFIG_SUBSET
298 #ifdef CONFIG_USB_GADGET_SA1100
299 /* use non-CDC for backwards compatibility */
300 #define DEV_CONFIG_SUBSET
304 /*-------------------------------------------------------------------------*/
306 /* "main" config is either CDC, or its simple subset */
307 static inline int is_cdc(struct eth_dev *dev)
309 #if !defined(DEV_CONFIG_SUBSET)
310 return 1; /* only cdc possible */
311 #elif !defined (DEV_CONFIG_CDC)
312 return 0; /* only subset possible */
314 return dev->cdc; /* depends on what hardware we found */
318 /* "secondary" RNDIS config may sometimes be activated */
319 static inline int rndis_active(struct eth_dev *dev)
321 #ifdef CONFIG_USB_ETH_RNDIS
328 #define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
329 #define cdc_active(dev) ( is_cdc(dev) && !rndis_active(dev))
333 #define DEFAULT_QLEN 2 /* double buffering by default */
335 /* peak bulk transfer bits-per-second */
336 #define HS_BPS (13 * 512 * 8 * 1000 * 8)
337 #define FS_BPS (19 * 64 * 1 * 1000 * 8)
339 #ifdef CONFIG_USB_GADGET_DUALSPEED
340 #define DEVSPEED USB_SPEED_HIGH
342 static unsigned qmult = 5;
343 module_param (qmult, uint, S_IRUGO|S_IWUSR);
346 /* for dual-speed hardware, use deeper queues at highspeed */
347 #define qlen(gadget) \
348 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
350 /* also defer IRQs on highspeed TX */
351 #define TX_DELAY qmult
353 static inline int BITRATE(struct usb_gadget *g)
355 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
358 #else /* full speed (low speed doesn't do bulk) */
359 #define DEVSPEED USB_SPEED_FULL
361 #define qlen(gadget) DEFAULT_QLEN
363 static inline int BITRATE(struct usb_gadget *g)
370 /*-------------------------------------------------------------------------*/
372 #define xprintk(d,level,fmt,args...) \
373 printk(level "%s: " fmt , (d)->net->name , ## args)
377 #define DEBUG(dev,fmt,args...) \
378 xprintk(dev , KERN_DEBUG , fmt , ## args)
380 #define DEBUG(dev,fmt,args...) \
387 #define VDEBUG(dev,fmt,args...) \
391 #define ERROR(dev,fmt,args...) \
392 xprintk(dev , KERN_ERR , fmt , ## args)
393 #define WARN(dev,fmt,args...) \
394 xprintk(dev , KERN_WARNING , fmt , ## args)
395 #define INFO(dev,fmt,args...) \
396 xprintk(dev , KERN_INFO , fmt , ## args)
398 /*-------------------------------------------------------------------------*/
400 /* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
401 * ep0 implementation: descriptors, config management, setup().
402 * also optional class-specific notification interrupt transfer.
406 * DESCRIPTORS ... most are static, but strings and (full) configuration
407 * descriptors are built on demand. For now we do either full CDC, or
408 * our simple subset, with RNDIS as an optional second configuration.
410 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
411 * the class descriptors match a modem (they're ignored; it's really just
412 * Ethernet functionality), they don't need the NOP altsetting, and the
413 * status transfer endpoint isn't optional.
416 #define STRING_MANUFACTURER 1
417 #define STRING_PRODUCT 2
418 #define STRING_ETHADDR 3
419 #define STRING_DATA 4
420 #define STRING_CONTROL 5
421 #define STRING_RNDIS_CONTROL 6
423 #define STRING_SUBSET 8
424 #define STRING_RNDIS 9
425 #define STRING_SERIALNUMBER 10
427 /* holds our biggest descriptor (or RNDIS response) */
428 #define USB_BUFSIZ 256
431 * This device advertises one configuration, eth_config, unless RNDIS
432 * is enabled (rndis_config) on hardware supporting at least two configs.
434 * NOTE: Controllers like superh_udc should probably be able to use
435 * an RNDIS-only configuration.
437 * FIXME define some higher-powered configurations to make it easier
438 * to recharge batteries ...
441 #define DEV_CONFIG_VALUE 1 /* cdc or subset */
442 #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
444 static struct usb_device_descriptor
446 .bLength = sizeof device_desc,
447 .bDescriptorType = USB_DT_DEVICE,
449 .bcdUSB = __constant_cpu_to_le16 (0x0200),
451 .bDeviceClass = USB_CLASS_COMM,
452 .bDeviceSubClass = 0,
453 .bDeviceProtocol = 0,
455 .idVendor = __constant_cpu_to_le16 (CDC_VENDOR_NUM),
456 .idProduct = __constant_cpu_to_le16 (CDC_PRODUCT_NUM),
457 .iManufacturer = STRING_MANUFACTURER,
458 .iProduct = STRING_PRODUCT,
459 .bNumConfigurations = 1,
462 static struct usb_otg_descriptor
464 .bLength = sizeof otg_descriptor,
465 .bDescriptorType = USB_DT_OTG,
467 .bmAttributes = USB_OTG_SRP,
470 static struct usb_config_descriptor
472 .bLength = sizeof eth_config,
473 .bDescriptorType = USB_DT_CONFIG,
475 /* compute wTotalLength on the fly */
477 .bConfigurationValue = DEV_CONFIG_VALUE,
478 .iConfiguration = STRING_CDC,
479 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
483 #ifdef CONFIG_USB_ETH_RNDIS
484 static struct usb_config_descriptor
486 .bLength = sizeof rndis_config,
487 .bDescriptorType = USB_DT_CONFIG,
489 /* compute wTotalLength on the fly */
491 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
492 .iConfiguration = STRING_RNDIS,
493 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
499 * Compared to the simple CDC subset, the full CDC Ethernet model adds
500 * three class descriptors, two interface descriptors, optional status
501 * endpoint. Both have a "data" interface and two bulk endpoints.
502 * There are also differences in how control requests are handled.
504 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
505 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
506 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
507 * work around those bugs) are unlikely to ever come from MSFT, you may
508 * wish to avoid using RNDIS.
510 * MCCI offers an alternative to RNDIS if you need to connect to Windows
511 * but have hardware that can't support CDC Ethernet. We add descriptors
512 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
513 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
514 * get those drivers from MCCI, or bundled with various products.
517 #ifdef DEV_CONFIG_CDC
518 static struct usb_interface_descriptor
520 .bLength = sizeof control_intf,
521 .bDescriptorType = USB_DT_INTERFACE,
523 .bInterfaceNumber = 0,
524 /* status endpoint is optional; this may be patched later */
526 .bInterfaceClass = USB_CLASS_COMM,
527 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
528 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
529 .iInterface = STRING_CONTROL,
533 #ifdef CONFIG_USB_ETH_RNDIS
534 static const struct usb_interface_descriptor
535 rndis_control_intf = {
536 .bLength = sizeof rndis_control_intf,
537 .bDescriptorType = USB_DT_INTERFACE,
539 .bInterfaceNumber = 0,
541 .bInterfaceClass = USB_CLASS_COMM,
542 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
543 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
544 .iInterface = STRING_RNDIS_CONTROL,
548 static const struct usb_cdc_header_desc header_desc = {
549 .bLength = sizeof header_desc,
550 .bDescriptorType = USB_DT_CS_INTERFACE,
551 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
553 .bcdCDC = __constant_cpu_to_le16 (0x0110),
556 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
558 static const struct usb_cdc_union_desc union_desc = {
559 .bLength = sizeof union_desc,
560 .bDescriptorType = USB_DT_CS_INTERFACE,
561 .bDescriptorSubType = USB_CDC_UNION_TYPE,
563 .bMasterInterface0 = 0, /* index of control interface */
564 .bSlaveInterface0 = 1, /* index of DATA interface */
567 #endif /* CDC || RNDIS */
569 #ifdef CONFIG_USB_ETH_RNDIS
571 static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
572 .bLength = sizeof call_mgmt_descriptor,
573 .bDescriptorType = USB_DT_CS_INTERFACE,
574 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
576 .bmCapabilities = 0x00,
577 .bDataInterface = 0x01,
580 static const struct usb_cdc_acm_descriptor acm_descriptor = {
581 .bLength = sizeof acm_descriptor,
582 .bDescriptorType = USB_DT_CS_INTERFACE,
583 .bDescriptorSubType = USB_CDC_ACM_TYPE,
585 .bmCapabilities = 0x00,
590 #ifndef DEV_CONFIG_CDC
592 /* "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
593 * ways: data endpoints live in the control interface, there's no data
594 * interface, and it's not used to talk to a cell phone radio.
597 static const struct usb_cdc_mdlm_desc mdlm_desc = {
598 .bLength = sizeof mdlm_desc,
599 .bDescriptorType = USB_DT_CS_INTERFACE,
600 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
602 .bcdVersion = __constant_cpu_to_le16(0x0100),
604 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
605 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
609 /* since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
610 * can't really use its struct. All we do here is say that we're using
611 * the submode of "SAFE" which directly matches the CDC Subset.
613 static const u8 mdlm_detail_desc[] = {
616 USB_CDC_MDLM_DETAIL_TYPE,
619 0, /* network control capabilities (none) */
620 0, /* network data capabilities ("raw" encapsulation) */
625 static const struct usb_cdc_ether_desc ether_desc = {
626 .bLength = sizeof ether_desc,
627 .bDescriptorType = USB_DT_CS_INTERFACE,
628 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
630 /* this descriptor actually adds value, surprise! */
631 .iMACAddress = STRING_ETHADDR,
632 .bmEthernetStatistics = __constant_cpu_to_le32 (0), /* no statistics */
633 .wMaxSegmentSize = __constant_cpu_to_le16 (ETH_FRAME_LEN),
634 .wNumberMCFilters = __constant_cpu_to_le16 (0),
635 .bNumberPowerFilters = 0,
639 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
641 /* include the status endpoint if we can, even where it's optional.
642 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
643 * packet, to simplify cancellation; and a big transfer interval, to
644 * waste less bandwidth.
646 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
647 * if they ignore the connect/disconnect notifications that real aether
648 * can provide. more advanced cdc configurations might want to support
649 * encapsulated commands (vendor-specific, using control-OUT).
651 * RNDIS requires the status endpoint, since it uses that encapsulation
652 * mechanism for its funky RPC scheme.
655 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
656 #define STATUS_BYTECOUNT 16 /* 8 byte header + data */
658 static struct usb_endpoint_descriptor
660 .bLength = USB_DT_ENDPOINT_SIZE,
661 .bDescriptorType = USB_DT_ENDPOINT,
663 .bEndpointAddress = USB_DIR_IN,
664 .bmAttributes = USB_ENDPOINT_XFER_INT,
665 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
666 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
670 #ifdef DEV_CONFIG_CDC
672 /* the default data interface has no endpoints ... */
674 static const struct usb_interface_descriptor
676 .bLength = sizeof data_nop_intf,
677 .bDescriptorType = USB_DT_INTERFACE,
679 .bInterfaceNumber = 1,
680 .bAlternateSetting = 0,
682 .bInterfaceClass = USB_CLASS_CDC_DATA,
683 .bInterfaceSubClass = 0,
684 .bInterfaceProtocol = 0,
687 /* ... but the "real" data interface has two bulk endpoints */
689 static const struct usb_interface_descriptor
691 .bLength = sizeof data_intf,
692 .bDescriptorType = USB_DT_INTERFACE,
694 .bInterfaceNumber = 1,
695 .bAlternateSetting = 1,
697 .bInterfaceClass = USB_CLASS_CDC_DATA,
698 .bInterfaceSubClass = 0,
699 .bInterfaceProtocol = 0,
700 .iInterface = STRING_DATA,
705 #ifdef CONFIG_USB_ETH_RNDIS
707 /* RNDIS doesn't activate by changing to the "real" altsetting */
709 static const struct usb_interface_descriptor
711 .bLength = sizeof rndis_data_intf,
712 .bDescriptorType = USB_DT_INTERFACE,
714 .bInterfaceNumber = 1,
715 .bAlternateSetting = 0,
717 .bInterfaceClass = USB_CLASS_CDC_DATA,
718 .bInterfaceSubClass = 0,
719 .bInterfaceProtocol = 0,
720 .iInterface = STRING_DATA,
725 #ifdef DEV_CONFIG_SUBSET
728 * "Simple" CDC-subset option is a simple vendor-neutral model that most
729 * full speed controllers can handle: one interface, two bulk endpoints.
731 * To assist host side drivers, we fancy it up a bit, and add descriptors
732 * so some host side drivers will understand it as a "SAFE" variant.
735 static const struct usb_interface_descriptor
737 .bLength = sizeof subset_data_intf,
738 .bDescriptorType = USB_DT_INTERFACE,
740 .bInterfaceNumber = 0,
741 .bAlternateSetting = 0,
743 .bInterfaceClass = USB_CLASS_COMM,
744 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
745 .bInterfaceProtocol = 0,
746 .iInterface = STRING_DATA,
752 static struct usb_endpoint_descriptor
754 .bLength = USB_DT_ENDPOINT_SIZE,
755 .bDescriptorType = USB_DT_ENDPOINT,
757 .bEndpointAddress = USB_DIR_IN,
758 .bmAttributes = USB_ENDPOINT_XFER_BULK,
761 static struct usb_endpoint_descriptor
763 .bLength = USB_DT_ENDPOINT_SIZE,
764 .bDescriptorType = USB_DT_ENDPOINT,
766 .bEndpointAddress = USB_DIR_OUT,
767 .bmAttributes = USB_ENDPOINT_XFER_BULK,
770 static const struct usb_descriptor_header *fs_eth_function [11] = {
771 (struct usb_descriptor_header *) &otg_descriptor,
772 #ifdef DEV_CONFIG_CDC
773 /* "cdc" mode descriptors */
774 (struct usb_descriptor_header *) &control_intf,
775 (struct usb_descriptor_header *) &header_desc,
776 (struct usb_descriptor_header *) &union_desc,
777 (struct usb_descriptor_header *) ðer_desc,
778 /* NOTE: status endpoint may need to be removed */
779 (struct usb_descriptor_header *) &fs_status_desc,
780 /* data interface, with altsetting */
781 (struct usb_descriptor_header *) &data_nop_intf,
782 (struct usb_descriptor_header *) &data_intf,
783 (struct usb_descriptor_header *) &fs_source_desc,
784 (struct usb_descriptor_header *) &fs_sink_desc,
786 #endif /* DEV_CONFIG_CDC */
789 static inline void __init fs_subset_descriptors(void)
791 #ifdef DEV_CONFIG_SUBSET
792 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
793 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
794 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
795 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
796 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
797 fs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
798 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
799 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
800 fs_eth_function[8] = NULL;
802 fs_eth_function[1] = NULL;
806 #ifdef CONFIG_USB_ETH_RNDIS
807 static const struct usb_descriptor_header *fs_rndis_function [] = {
808 (struct usb_descriptor_header *) &otg_descriptor,
809 /* control interface matches ACM, not Ethernet */
810 (struct usb_descriptor_header *) &rndis_control_intf,
811 (struct usb_descriptor_header *) &header_desc,
812 (struct usb_descriptor_header *) &call_mgmt_descriptor,
813 (struct usb_descriptor_header *) &acm_descriptor,
814 (struct usb_descriptor_header *) &union_desc,
815 (struct usb_descriptor_header *) &fs_status_desc,
816 /* data interface has no altsetting */
817 (struct usb_descriptor_header *) &rndis_data_intf,
818 (struct usb_descriptor_header *) &fs_source_desc,
819 (struct usb_descriptor_header *) &fs_sink_desc,
824 #ifdef CONFIG_USB_GADGET_DUALSPEED
827 * usb 2.0 devices need to expose both high speed and full speed
828 * descriptors, unless they only run at full speed.
831 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
832 static struct usb_endpoint_descriptor
834 .bLength = USB_DT_ENDPOINT_SIZE,
835 .bDescriptorType = USB_DT_ENDPOINT,
837 .bmAttributes = USB_ENDPOINT_XFER_INT,
838 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
839 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
841 #endif /* DEV_CONFIG_CDC */
843 static struct usb_endpoint_descriptor
845 .bLength = USB_DT_ENDPOINT_SIZE,
846 .bDescriptorType = USB_DT_ENDPOINT,
848 .bmAttributes = USB_ENDPOINT_XFER_BULK,
849 .wMaxPacketSize = __constant_cpu_to_le16 (512),
852 static struct usb_endpoint_descriptor
854 .bLength = USB_DT_ENDPOINT_SIZE,
855 .bDescriptorType = USB_DT_ENDPOINT,
857 .bmAttributes = USB_ENDPOINT_XFER_BULK,
858 .wMaxPacketSize = __constant_cpu_to_le16 (512),
861 static struct usb_qualifier_descriptor
863 .bLength = sizeof dev_qualifier,
864 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
866 .bcdUSB = __constant_cpu_to_le16 (0x0200),
867 .bDeviceClass = USB_CLASS_COMM,
869 .bNumConfigurations = 1,
872 static const struct usb_descriptor_header *hs_eth_function [11] = {
873 (struct usb_descriptor_header *) &otg_descriptor,
874 #ifdef DEV_CONFIG_CDC
875 /* "cdc" mode descriptors */
876 (struct usb_descriptor_header *) &control_intf,
877 (struct usb_descriptor_header *) &header_desc,
878 (struct usb_descriptor_header *) &union_desc,
879 (struct usb_descriptor_header *) ðer_desc,
880 /* NOTE: status endpoint may need to be removed */
881 (struct usb_descriptor_header *) &hs_status_desc,
882 /* data interface, with altsetting */
883 (struct usb_descriptor_header *) &data_nop_intf,
884 (struct usb_descriptor_header *) &data_intf,
885 (struct usb_descriptor_header *) &hs_source_desc,
886 (struct usb_descriptor_header *) &hs_sink_desc,
888 #endif /* DEV_CONFIG_CDC */
891 static inline void __init hs_subset_descriptors(void)
893 #ifdef DEV_CONFIG_SUBSET
894 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
895 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
896 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
897 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
898 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
899 hs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
900 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
901 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
902 hs_eth_function[8] = NULL;
904 hs_eth_function[1] = NULL;
908 #ifdef CONFIG_USB_ETH_RNDIS
909 static const struct usb_descriptor_header *hs_rndis_function [] = {
910 (struct usb_descriptor_header *) &otg_descriptor,
911 /* control interface matches ACM, not Ethernet */
912 (struct usb_descriptor_header *) &rndis_control_intf,
913 (struct usb_descriptor_header *) &header_desc,
914 (struct usb_descriptor_header *) &call_mgmt_descriptor,
915 (struct usb_descriptor_header *) &acm_descriptor,
916 (struct usb_descriptor_header *) &union_desc,
917 (struct usb_descriptor_header *) &hs_status_desc,
918 /* data interface has no altsetting */
919 (struct usb_descriptor_header *) &rndis_data_intf,
920 (struct usb_descriptor_header *) &hs_source_desc,
921 (struct usb_descriptor_header *) &hs_sink_desc,
927 /* maxpacket and other transfer characteristics vary by speed. */
928 #define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
932 /* if there's no high speed support, maxpacket doesn't change. */
933 #define ep_desc(g,hs,fs) (((void)(g)), (fs))
935 static inline void __init hs_subset_descriptors(void)
939 #endif /* !CONFIG_USB_GADGET_DUALSPEED */
941 /*-------------------------------------------------------------------------*/
943 /* descriptors that are built on-demand */
945 static char manufacturer [50];
946 static char product_desc [40] = DRIVER_DESC;
947 static char serial_number [20];
949 /* address that the host will use ... usually assigned at random */
950 static char ethaddr [2 * ETH_ALEN + 1];
952 /* static strings, in UTF-8 */
953 static struct usb_string strings [] = {
954 { STRING_MANUFACTURER, manufacturer, },
955 { STRING_PRODUCT, product_desc, },
956 { STRING_SERIALNUMBER, serial_number, },
957 { STRING_DATA, "Ethernet Data", },
958 { STRING_ETHADDR, ethaddr, },
959 #ifdef DEV_CONFIG_CDC
960 { STRING_CDC, "CDC Ethernet", },
961 { STRING_CONTROL, "CDC Communications Control", },
963 #ifdef DEV_CONFIG_SUBSET
964 { STRING_SUBSET, "CDC Ethernet Subset", },
966 #ifdef CONFIG_USB_ETH_RNDIS
967 { STRING_RNDIS, "RNDIS", },
968 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
970 { } /* end of list */
973 static struct usb_gadget_strings stringtab = {
974 .language = 0x0409, /* en-us */
979 * one config, two interfaces: control, data.
980 * complications: class descriptors, and an altsetting.
983 config_buf (enum usb_device_speed speed,
985 unsigned index, int is_otg)
988 const struct usb_config_descriptor *config;
989 const struct usb_descriptor_header **function;
990 #ifdef CONFIG_USB_GADGET_DUALSPEED
991 int hs = (speed == USB_SPEED_HIGH);
993 if (type == USB_DT_OTHER_SPEED_CONFIG)
995 #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
997 #define which_fn(t) (fs_ ## t ## _function)
1000 if (index >= device_desc.bNumConfigurations)
1003 #ifdef CONFIG_USB_ETH_RNDIS
1004 /* list the RNDIS config first, to make Microsoft's drivers
1005 * happy. DOCSIS 1.0 needs this too.
1007 if (device_desc.bNumConfigurations == 2 && index == 0) {
1008 config = &rndis_config;
1009 function = which_fn (rndis);
1013 config = ð_config;
1014 function = which_fn (eth);
1017 /* for now, don't advertise srp-only devices */
1021 len = usb_gadget_config_buf (config, buf, USB_BUFSIZ, function);
1024 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
1028 /*-------------------------------------------------------------------------*/
1030 static void eth_start (struct eth_dev *dev, gfp_t gfp_flags);
1031 static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
1034 set_ether_config (struct eth_dev *dev, gfp_t gfp_flags)
1037 struct usb_gadget *gadget = dev->gadget;
1039 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
1040 /* status endpoint used for RNDIS and (optionally) CDC */
1041 if (!subset_active(dev) && dev->status_ep) {
1042 dev->status = ep_desc (gadget, &hs_status_desc,
1044 dev->status_ep->driver_data = dev;
1046 result = usb_ep_enable (dev->status_ep, dev->status);
1048 DEBUG (dev, "enable %s --> %d\n",
1049 dev->status_ep->name, result);
1055 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
1056 dev->in_ep->driver_data = dev;
1058 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
1059 dev->out_ep->driver_data = dev;
1061 /* With CDC, the host isn't allowed to use these two data
1062 * endpoints in the default altsetting for the interface.
1063 * so we don't activate them yet. Reset from SET_INTERFACE.
1065 * Strictly speaking RNDIS should work the same: activation is
1066 * a side effect of setting a packet filter. Deactivation is
1067 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
1069 if (!cdc_active(dev)) {
1070 result = usb_ep_enable (dev->in_ep, dev->in);
1072 DEBUG(dev, "enable %s --> %d\n",
1073 dev->in_ep->name, result);
1077 result = usb_ep_enable (dev->out_ep, dev->out);
1079 DEBUG (dev, "enable %s --> %d\n",
1080 dev->out_ep->name, result);
1087 result = alloc_requests (dev, qlen (gadget), gfp_flags);
1089 /* on error, disable any endpoints */
1091 if (!subset_active(dev))
1092 (void) usb_ep_disable (dev->status_ep);
1094 (void) usb_ep_disable (dev->in_ep);
1095 (void) usb_ep_disable (dev->out_ep);
1100 /* activate non-CDC configs right away
1101 * this isn't strictly according to the RNDIS spec
1103 if (!cdc_active (dev)) {
1104 netif_carrier_on (dev->net);
1105 if (netif_running (dev->net)) {
1106 spin_unlock (&dev->lock);
1107 eth_start (dev, GFP_ATOMIC);
1108 spin_lock (&dev->lock);
1113 DEBUG (dev, "qlen %d\n", qlen (gadget));
1115 /* caller is responsible for cleanup on error */
1119 static void eth_reset_config (struct eth_dev *dev)
1121 struct usb_request *req;
1123 if (dev->config == 0)
1126 DEBUG (dev, "%s\n", __FUNCTION__);
1128 netif_stop_queue (dev->net);
1129 netif_carrier_off (dev->net);
1130 rndis_uninit(dev->rndis_config);
1132 /* disable endpoints, forcing (synchronous) completion of
1133 * pending i/o. then free the requests.
1136 usb_ep_disable (dev->in_ep);
1137 spin_lock(&dev->req_lock);
1138 while (likely (!list_empty (&dev->tx_reqs))) {
1139 req = container_of (dev->tx_reqs.next,
1140 struct usb_request, list);
1141 list_del (&req->list);
1143 spin_unlock(&dev->req_lock);
1144 usb_ep_free_request (dev->in_ep, req);
1145 spin_lock(&dev->req_lock);
1147 spin_unlock(&dev->req_lock);
1150 usb_ep_disable (dev->out_ep);
1151 spin_lock(&dev->req_lock);
1152 while (likely (!list_empty (&dev->rx_reqs))) {
1153 req = container_of (dev->rx_reqs.next,
1154 struct usb_request, list);
1155 list_del (&req->list);
1157 spin_unlock(&dev->req_lock);
1158 usb_ep_free_request (dev->out_ep, req);
1159 spin_lock(&dev->req_lock);
1161 spin_unlock(&dev->req_lock);
1165 usb_ep_disable (dev->status_ep);
1168 dev->cdc_filter = 0;
1172 /* change our operational config. must agree with the code
1173 * that returns config descriptors, and altsetting code.
1176 eth_set_config (struct eth_dev *dev, unsigned number, gfp_t gfp_flags)
1179 struct usb_gadget *gadget = dev->gadget;
1181 if (gadget_is_sa1100 (gadget)
1183 && atomic_read (&dev->tx_qlen) != 0) {
1184 /* tx fifo is full, but we can't clear it...*/
1185 INFO (dev, "can't change configurations\n");
1188 eth_reset_config (dev);
1191 case DEV_CONFIG_VALUE:
1192 result = set_ether_config (dev, gfp_flags);
1194 #ifdef CONFIG_USB_ETH_RNDIS
1195 case DEV_RNDIS_CONFIG_VALUE:
1197 result = set_ether_config (dev, gfp_flags);
1209 eth_reset_config (dev);
1210 usb_gadget_vbus_draw(dev->gadget,
1211 dev->gadget->is_otg ? 8 : 100);
1216 power = 2 * eth_config.bMaxPower;
1217 usb_gadget_vbus_draw(dev->gadget, power);
1219 switch (gadget->speed) {
1220 case USB_SPEED_FULL: speed = "full"; break;
1221 #ifdef CONFIG_USB_GADGET_DUALSPEED
1222 case USB_SPEED_HIGH: speed = "high"; break;
1224 default: speed = "?"; break;
1227 dev->config = number;
1228 INFO (dev, "%s speed config #%d: %d mA, %s, using %s\n",
1229 speed, number, power, driver_desc,
1234 : "CDC Ethernet Subset"));
1239 /*-------------------------------------------------------------------------*/
1241 #ifdef DEV_CONFIG_CDC
1243 /* The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
1244 * only to notify the host about link status changes (which we support) or
1245 * report completion of some encapsulated command (as used in RNDIS). Since
1246 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1247 * command mechanism; and only one status request is ever queued.
1250 static void eth_status_complete (struct usb_ep *ep, struct usb_request *req)
1252 struct usb_cdc_notification *event = req->buf;
1253 int value = req->status;
1254 struct eth_dev *dev = ep->driver_data;
1256 /* issue the second notification if host reads the first */
1257 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1259 __le32 *data = req->buf + sizeof *event;
1261 event->bmRequestType = 0xA1;
1262 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
1263 event->wValue = __constant_cpu_to_le16 (0);
1264 event->wIndex = __constant_cpu_to_le16 (1);
1265 event->wLength = __constant_cpu_to_le16 (8);
1267 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1268 data [0] = data [1] = cpu_to_le32 (BITRATE (dev->gadget));
1270 req->length = STATUS_BYTECOUNT;
1271 value = usb_ep_queue (ep, req, GFP_ATOMIC);
1272 DEBUG (dev, "send SPEED_CHANGE --> %d\n", value);
1275 } else if (value != -ECONNRESET)
1276 DEBUG (dev, "event %02x --> %d\n",
1277 event->bNotificationType, value);
1278 req->context = NULL;
1281 static void issue_start_status (struct eth_dev *dev)
1283 struct usb_request *req = dev->stat_req;
1284 struct usb_cdc_notification *event;
1287 DEBUG (dev, "%s, flush old status first\n", __FUNCTION__);
1291 * FIXME ugly idiom, maybe we'd be better with just
1292 * a "cancel the whole queue" primitive since any
1293 * unlink-one primitive has way too many error modes.
1294 * here, we "know" toggle is already clear...
1296 * FIXME iff req->context != null just dequeue it
1298 usb_ep_disable (dev->status_ep);
1299 usb_ep_enable (dev->status_ep, dev->status);
1301 /* 3.8.1 says to issue first NETWORK_CONNECTION, then
1302 * a SPEED_CHANGE. could be useful in some configs.
1305 event->bmRequestType = 0xA1;
1306 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
1307 event->wValue = __constant_cpu_to_le16 (1); /* connected */
1308 event->wIndex = __constant_cpu_to_le16 (1);
1311 req->length = sizeof *event;
1312 req->complete = eth_status_complete;
1315 value = usb_ep_queue (dev->status_ep, req, GFP_ATOMIC);
1317 DEBUG (dev, "status buf queue --> %d\n", value);
1322 /*-------------------------------------------------------------------------*/
1324 static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req)
1326 if (req->status || req->actual != req->length)
1327 DEBUG ((struct eth_dev *) ep->driver_data,
1328 "setup complete --> %d, %d/%d\n",
1329 req->status, req->actual, req->length);
1332 #ifdef CONFIG_USB_ETH_RNDIS
1334 static void rndis_response_complete (struct usb_ep *ep, struct usb_request *req)
1336 if (req->status || req->actual != req->length)
1337 DEBUG ((struct eth_dev *) ep->driver_data,
1338 "rndis response complete --> %d, %d/%d\n",
1339 req->status, req->actual, req->length);
1341 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1344 static void rndis_command_complete (struct usb_ep *ep, struct usb_request *req)
1346 struct eth_dev *dev = ep->driver_data;
1349 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1350 spin_lock(&dev->lock);
1351 status = rndis_msg_parser (dev->rndis_config, (u8 *) req->buf);
1353 ERROR(dev, "%s: rndis parse error %d\n", __FUNCTION__, status);
1354 spin_unlock(&dev->lock);
1360 * The setup() callback implements all the ep0 functionality that's not
1361 * handled lower down. CDC has a number of less-common features:
1363 * - two interfaces: control, and ethernet data
1364 * - Ethernet data interface has two altsettings: default, and active
1365 * - class-specific descriptors for the control interface
1366 * - class-specific control requests
1369 eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1371 struct eth_dev *dev = get_gadget_data (gadget);
1372 struct usb_request *req = dev->req;
1373 int value = -EOPNOTSUPP;
1374 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1375 u16 wValue = le16_to_cpu(ctrl->wValue);
1376 u16 wLength = le16_to_cpu(ctrl->wLength);
1378 /* descriptors just go into the pre-allocated ep0 buffer,
1379 * while config change events may enable network traffic.
1381 req->complete = eth_setup_complete;
1382 switch (ctrl->bRequest) {
1384 case USB_REQ_GET_DESCRIPTOR:
1385 if (ctrl->bRequestType != USB_DIR_IN)
1387 switch (wValue >> 8) {
1390 value = min (wLength, (u16) sizeof device_desc);
1391 memcpy (req->buf, &device_desc, value);
1393 #ifdef CONFIG_USB_GADGET_DUALSPEED
1394 case USB_DT_DEVICE_QUALIFIER:
1395 if (!gadget->is_dualspeed)
1397 value = min (wLength, (u16) sizeof dev_qualifier);
1398 memcpy (req->buf, &dev_qualifier, value);
1401 case USB_DT_OTHER_SPEED_CONFIG:
1402 if (!gadget->is_dualspeed)
1405 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1407 value = config_buf (gadget->speed, req->buf,
1412 value = min (wLength, (u16) value);
1416 value = usb_gadget_get_string (&stringtab,
1417 wValue & 0xff, req->buf);
1419 value = min (wLength, (u16) value);
1424 case USB_REQ_SET_CONFIGURATION:
1425 if (ctrl->bRequestType != 0)
1427 if (gadget->a_hnp_support)
1428 DEBUG (dev, "HNP available\n");
1429 else if (gadget->a_alt_hnp_support)
1430 DEBUG (dev, "HNP needs a different root port\n");
1431 spin_lock (&dev->lock);
1432 value = eth_set_config (dev, wValue, GFP_ATOMIC);
1433 spin_unlock (&dev->lock);
1435 case USB_REQ_GET_CONFIGURATION:
1436 if (ctrl->bRequestType != USB_DIR_IN)
1438 *(u8 *)req->buf = dev->config;
1439 value = min (wLength, (u16) 1);
1442 case USB_REQ_SET_INTERFACE:
1443 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1447 if (!cdc_active(dev) && wIndex != 0)
1449 spin_lock (&dev->lock);
1451 /* PXA hardware partially handles SET_INTERFACE;
1452 * we need to kluge around that interference.
1454 if (gadget_is_pxa (gadget)) {
1455 value = eth_set_config (dev, DEV_CONFIG_VALUE,
1460 #ifdef DEV_CONFIG_CDC
1462 case 0: /* control/master intf */
1466 usb_ep_disable (dev->status_ep);
1467 usb_ep_enable (dev->status_ep, dev->status);
1471 case 1: /* data intf */
1474 usb_ep_disable (dev->in_ep);
1475 usb_ep_disable (dev->out_ep);
1477 /* CDC requires the data transfers not be done from
1478 * the default interface setting ... also, setting
1479 * the non-default interface resets filters etc.
1482 if (!cdc_active (dev))
1484 usb_ep_enable (dev->in_ep, dev->in);
1485 usb_ep_enable (dev->out_ep, dev->out);
1486 dev->cdc_filter = DEFAULT_FILTER;
1487 netif_carrier_on (dev->net);
1489 issue_start_status (dev);
1490 if (netif_running (dev->net)) {
1491 spin_unlock (&dev->lock);
1492 eth_start (dev, GFP_ATOMIC);
1493 spin_lock (&dev->lock);
1496 netif_stop_queue (dev->net);
1497 netif_carrier_off (dev->net);
1503 /* FIXME this is wrong, as is the assumption that
1504 * all non-PXA hardware talks real CDC ...
1506 dev_warn (&gadget->dev, "set_interface ignored!\n");
1507 #endif /* DEV_CONFIG_CDC */
1510 spin_unlock (&dev->lock);
1512 case USB_REQ_GET_INTERFACE:
1513 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1517 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
1520 /* for CDC, iff carrier is on, data interface is active. */
1521 if (rndis_active(dev) || wIndex != 1)
1522 *(u8 *)req->buf = 0;
1524 *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0;
1525 value = min (wLength, (u16) 1);
1528 #ifdef DEV_CONFIG_CDC
1529 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
1530 /* see 6.2.30: no data, wIndex = interface,
1531 * wValue = packet filter bitmap
1533 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1538 DEBUG (dev, "packet filter %02x\n", wValue);
1539 dev->cdc_filter = wValue;
1544 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1545 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1546 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1547 * case USB_CDC_GET_ETHERNET_STATISTIC:
1550 #endif /* DEV_CONFIG_CDC */
1552 #ifdef CONFIG_USB_ETH_RNDIS
1553 /* RNDIS uses the CDC command encapsulation mechanism to implement
1554 * an RPC scheme, with much getting/setting of attributes by OID.
1556 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1557 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1558 || !rndis_active(dev)
1559 || wLength > USB_BUFSIZ
1561 || rndis_control_intf.bInterfaceNumber
1564 /* read the request, then process it */
1566 req->complete = rndis_command_complete;
1567 /* later, rndis_control_ack () sends a notification */
1570 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1571 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1572 == ctrl->bRequestType
1573 && rndis_active(dev)
1574 // && wLength >= 0x0400
1576 && rndis_control_intf.bInterfaceNumber
1580 /* return the result */
1581 buf = rndis_get_next_response (dev->rndis_config,
1584 memcpy (req->buf, buf, value);
1585 req->complete = rndis_response_complete;
1586 rndis_free_response(dev->rndis_config, buf);
1588 /* else stalls ... spec says to avoid that */
1595 "unknown control req%02x.%02x v%04x i%04x l%d\n",
1596 ctrl->bRequestType, ctrl->bRequest,
1597 wValue, wIndex, wLength);
1600 /* respond with data transfer before status phase? */
1602 req->length = value;
1603 req->zero = value < wLength
1604 && (value % gadget->ep0->maxpacket) == 0;
1605 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1607 DEBUG (dev, "ep_queue --> %d\n", value);
1609 eth_setup_complete (gadget->ep0, req);
1613 /* host either stalls (value < 0) or reports success */
1618 eth_disconnect (struct usb_gadget *gadget)
1620 struct eth_dev *dev = get_gadget_data (gadget);
1621 unsigned long flags;
1623 spin_lock_irqsave (&dev->lock, flags);
1624 netif_stop_queue (dev->net);
1625 netif_carrier_off (dev->net);
1626 eth_reset_config (dev);
1627 spin_unlock_irqrestore (&dev->lock, flags);
1629 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1631 /* next we may get setup() calls to enumerate new connections;
1632 * or an unbind() during shutdown (including removing module).
1636 /*-------------------------------------------------------------------------*/
1638 /* NETWORK DRIVER HOOKUP (to the layer above this driver) */
1640 static int eth_change_mtu (struct net_device *net, int new_mtu)
1642 struct eth_dev *dev = netdev_priv(net);
1647 if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN)
1649 /* no zero-length packet read wanted after mtu-sized packets */
1650 if (((new_mtu + sizeof (struct ethhdr)) % dev->in_ep->maxpacket) == 0)
1656 static struct net_device_stats *eth_get_stats (struct net_device *net)
1658 return &((struct eth_dev *)netdev_priv(net))->stats;
1661 static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
1663 struct eth_dev *dev = netdev_priv(net);
1664 strlcpy(p->driver, shortname, sizeof p->driver);
1665 strlcpy(p->version, DRIVER_VERSION, sizeof p->version);
1666 strlcpy(p->fw_version, dev->gadget->name, sizeof p->fw_version);
1667 strlcpy (p->bus_info, dev->gadget->dev.bus_id, sizeof p->bus_info);
1670 static u32 eth_get_link(struct net_device *net)
1672 struct eth_dev *dev = netdev_priv(net);
1673 return dev->gadget->speed != USB_SPEED_UNKNOWN;
1676 static struct ethtool_ops ops = {
1677 .get_drvinfo = eth_get_drvinfo,
1678 .get_link = eth_get_link
1681 static void defer_kevent (struct eth_dev *dev, int flag)
1683 if (test_and_set_bit (flag, &dev->todo))
1685 if (!schedule_work (&dev->work))
1686 ERROR (dev, "kevent %d may have been dropped\n", flag);
1688 DEBUG (dev, "kevent %d scheduled\n", flag);
1691 static void rx_complete (struct usb_ep *ep, struct usb_request *req);
1694 rx_submit (struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
1696 struct sk_buff *skb;
1697 int retval = -ENOMEM;
1700 /* Padding up to RX_EXTRA handles minor disagreements with host.
1701 * Normally we use the USB "terminate on short read" convention;
1702 * so allow up to (N*maxpacket), since that memory is normally
1703 * already allocated. Some hardware doesn't deal well with short
1704 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1705 * byte off the end (to force hardware errors on overflow).
1707 * RNDIS uses internal framing, and explicitly allows senders to
1708 * pad to end-of-packet. That's potentially nice for speed,
1709 * but means receivers can't recover synch on their own.
1711 size = (sizeof (struct ethhdr) + dev->net->mtu + RX_EXTRA);
1712 size += dev->out_ep->maxpacket - 1;
1713 if (rndis_active(dev))
1714 size += sizeof (struct rndis_packet_msg_type);
1715 size -= size % dev->out_ep->maxpacket;
1717 if ((skb = alloc_skb (size + NET_IP_ALIGN, gfp_flags)) == 0) {
1718 DEBUG (dev, "no rx skb\n");
1722 /* Some platforms perform better when IP packets are aligned,
1723 * but on at least one, checksumming fails otherwise. Note:
1724 * RNDIS headers involve variable numbers of LE32 values.
1726 skb_reserve(skb, NET_IP_ALIGN);
1728 req->buf = skb->data;
1730 req->complete = rx_complete;
1733 retval = usb_ep_queue (dev->out_ep, req, gfp_flags);
1734 if (retval == -ENOMEM)
1736 defer_kevent (dev, WORK_RX_MEMORY);
1738 DEBUG (dev, "rx submit --> %d\n", retval);
1739 dev_kfree_skb_any (skb);
1740 spin_lock(&dev->req_lock);
1741 list_add (&req->list, &dev->rx_reqs);
1742 spin_unlock(&dev->req_lock);
1747 static void rx_complete (struct usb_ep *ep, struct usb_request *req)
1749 struct sk_buff *skb = req->context;
1750 struct eth_dev *dev = ep->driver_data;
1751 int status = req->status;
1755 /* normal completion */
1757 skb_put (skb, req->actual);
1758 /* we know MaxPacketsPerTransfer == 1 here */
1759 if (rndis_active(dev))
1760 status = rndis_rm_hdr (skb);
1762 || ETH_HLEN > skb->len
1763 || skb->len > ETH_FRAME_LEN) {
1764 dev->stats.rx_errors++;
1765 dev->stats.rx_length_errors++;
1766 DEBUG (dev, "rx length %d\n", skb->len);
1770 skb->dev = dev->net;
1771 skb->protocol = eth_type_trans (skb, dev->net);
1772 dev->stats.rx_packets++;
1773 dev->stats.rx_bytes += skb->len;
1775 /* no buffer copies needed, unless hardware can't
1778 status = netif_rx (skb);
1782 /* software-driven interface shutdown */
1783 case -ECONNRESET: // unlink
1784 case -ESHUTDOWN: // disconnect etc
1785 VDEBUG (dev, "rx shutdown, code %d\n", status);
1788 /* for hardware automagic (such as pxa) */
1789 case -ECONNABORTED: // endpoint reset
1790 DEBUG (dev, "rx %s reset\n", ep->name);
1791 defer_kevent (dev, WORK_RX_MEMORY);
1793 dev_kfree_skb_any (skb);
1798 dev->stats.rx_over_errors++;
1802 dev->stats.rx_errors++;
1803 DEBUG (dev, "rx status %d\n", status);
1808 dev_kfree_skb_any (skb);
1809 if (!netif_running (dev->net)) {
1811 spin_lock(&dev->req_lock);
1812 list_add (&req->list, &dev->rx_reqs);
1813 spin_unlock(&dev->req_lock);
1817 rx_submit (dev, req, GFP_ATOMIC);
1820 static int prealloc (struct list_head *list, struct usb_ep *ep,
1821 unsigned n, gfp_t gfp_flags)
1824 struct usb_request *req;
1829 /* queue/recycle up to N requests */
1831 list_for_each_entry (req, list, list) {
1836 req = usb_ep_alloc_request (ep, gfp_flags);
1838 return list_empty (list) ? -ENOMEM : 0;
1839 list_add (&req->list, list);
1846 struct list_head *next;
1848 next = req->list.next;
1849 list_del (&req->list);
1850 usb_ep_free_request (ep, req);
1855 req = container_of (next, struct usb_request, list);
1860 static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
1864 spin_lock(&dev->req_lock);
1865 status = prealloc (&dev->tx_reqs, dev->in_ep, n, gfp_flags);
1868 status = prealloc (&dev->rx_reqs, dev->out_ep, n, gfp_flags);
1873 DEBUG (dev, "can't alloc requests\n");
1875 spin_unlock(&dev->req_lock);
1879 static void rx_fill (struct eth_dev *dev, gfp_t gfp_flags)
1881 struct usb_request *req;
1882 unsigned long flags;
1884 /* fill unused rxq slots with some skb */
1885 spin_lock_irqsave(&dev->req_lock, flags);
1886 while (!list_empty (&dev->rx_reqs)) {
1887 req = container_of (dev->rx_reqs.next,
1888 struct usb_request, list);
1889 list_del_init (&req->list);
1890 spin_unlock_irqrestore(&dev->req_lock, flags);
1892 if (rx_submit (dev, req, gfp_flags) < 0) {
1893 defer_kevent (dev, WORK_RX_MEMORY);
1897 spin_lock_irqsave(&dev->req_lock, flags);
1899 spin_unlock_irqrestore(&dev->req_lock, flags);
1902 static void eth_work (struct work_struct *work)
1904 struct eth_dev *dev = container_of(work, struct eth_dev, work);
1906 if (test_and_clear_bit (WORK_RX_MEMORY, &dev->todo)) {
1907 if (netif_running (dev->net))
1908 rx_fill (dev, GFP_KERNEL);
1912 DEBUG (dev, "work done, flags = 0x%lx\n", dev->todo);
1915 static void tx_complete (struct usb_ep *ep, struct usb_request *req)
1917 struct sk_buff *skb = req->context;
1918 struct eth_dev *dev = ep->driver_data;
1920 switch (req->status) {
1922 dev->stats.tx_errors++;
1923 VDEBUG (dev, "tx err %d\n", req->status);
1925 case -ECONNRESET: // unlink
1926 case -ESHUTDOWN: // disconnect etc
1929 dev->stats.tx_bytes += skb->len;
1931 dev->stats.tx_packets++;
1933 spin_lock(&dev->req_lock);
1934 list_add (&req->list, &dev->tx_reqs);
1935 spin_unlock(&dev->req_lock);
1936 dev_kfree_skb_any (skb);
1938 atomic_dec (&dev->tx_qlen);
1939 if (netif_carrier_ok (dev->net))
1940 netif_wake_queue (dev->net);
1943 static inline int eth_is_promisc (struct eth_dev *dev)
1945 /* no filters for the CDC subset; always promisc */
1946 if (subset_active (dev))
1948 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1951 static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1953 struct eth_dev *dev = netdev_priv(net);
1954 int length = skb->len;
1956 struct usb_request *req = NULL;
1957 unsigned long flags;
1959 /* apply outgoing CDC or RNDIS filters */
1960 if (!eth_is_promisc (dev)) {
1961 u8 *dest = skb->data;
1963 if (is_multicast_ether_addr(dest)) {
1966 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1967 * SET_ETHERNET_MULTICAST_FILTERS requests
1969 if (is_broadcast_ether_addr(dest))
1970 type = USB_CDC_PACKET_TYPE_BROADCAST;
1972 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1973 if (!(dev->cdc_filter & type)) {
1974 dev_kfree_skb_any (skb);
1978 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1981 spin_lock_irqsave(&dev->req_lock, flags);
1982 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1983 list_del (&req->list);
1984 if (list_empty (&dev->tx_reqs))
1985 netif_stop_queue (net);
1986 spin_unlock_irqrestore(&dev->req_lock, flags);
1988 /* no buffer copies needed, unless the network stack did it
1989 * or the hardware can't use skb buffers.
1990 * or there's not enough space for any RNDIS headers we need
1992 if (rndis_active(dev)) {
1993 struct sk_buff *skb_rndis;
1995 skb_rndis = skb_realloc_headroom (skb,
1996 sizeof (struct rndis_packet_msg_type));
2000 dev_kfree_skb_any (skb);
2002 rndis_add_hdr (skb);
2005 req->buf = skb->data;
2007 req->complete = tx_complete;
2009 /* use zlp framing on tx for strict CDC-Ether conformance,
2010 * though any robust network rx path ignores extra padding.
2011 * and some hardware doesn't like to write zlps.
2014 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2017 req->length = length;
2019 #ifdef CONFIG_USB_GADGET_DUALSPEED
2020 /* throttle highspeed IRQ rate back slightly */
2021 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2022 ? ((atomic_read (&dev->tx_qlen) % TX_DELAY) != 0)
2026 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
2029 DEBUG (dev, "tx queue err %d\n", retval);
2032 net->trans_start = jiffies;
2033 atomic_inc (&dev->tx_qlen);
2038 dev->stats.tx_dropped++;
2039 dev_kfree_skb_any (skb);
2040 spin_lock_irqsave(&dev->req_lock, flags);
2041 if (list_empty (&dev->tx_reqs))
2042 netif_start_queue (net);
2043 list_add (&req->list, &dev->tx_reqs);
2044 spin_unlock_irqrestore(&dev->req_lock, flags);
2049 /*-------------------------------------------------------------------------*/
2051 #ifdef CONFIG_USB_ETH_RNDIS
2053 /* The interrupt endpoint is used in RNDIS to notify the host when messages
2054 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
2055 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
2056 * REMOTE_NDIS_KEEPALIVE_MSG.
2058 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
2059 * normally just one notification will be queued.
2062 static struct usb_request *eth_req_alloc (struct usb_ep *, unsigned, gfp_t);
2063 static void eth_req_free (struct usb_ep *ep, struct usb_request *req);
2066 rndis_control_ack_complete (struct usb_ep *ep, struct usb_request *req)
2068 struct eth_dev *dev = ep->driver_data;
2070 if (req->status || req->actual != req->length)
2072 "rndis control ack complete --> %d, %d/%d\n",
2073 req->status, req->actual, req->length);
2074 req->context = NULL;
2076 if (req != dev->stat_req)
2077 eth_req_free(ep, req);
2080 static int rndis_control_ack (struct net_device *net)
2082 struct eth_dev *dev = netdev_priv(net);
2084 struct usb_request *resp = dev->stat_req;
2086 /* in case RNDIS calls this after disconnect */
2088 DEBUG (dev, "status ENODEV\n");
2092 /* in case queue length > 1 */
2093 if (resp->context) {
2094 resp = eth_req_alloc (dev->status_ep, 8, GFP_ATOMIC);
2099 /* Send RNDIS RESPONSE_AVAILABLE notification;
2100 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
2103 resp->complete = rndis_control_ack_complete;
2104 resp->context = dev;
2106 *((__le32 *) resp->buf) = __constant_cpu_to_le32 (1);
2107 *((__le32 *) resp->buf + 1) = __constant_cpu_to_le32 (0);
2109 length = usb_ep_queue (dev->status_ep, resp, GFP_ATOMIC);
2112 rndis_control_ack_complete (dev->status_ep, resp);
2120 #define rndis_control_ack NULL
2124 static void eth_start (struct eth_dev *dev, gfp_t gfp_flags)
2126 DEBUG (dev, "%s\n", __FUNCTION__);
2128 /* fill the rx queue */
2129 rx_fill (dev, gfp_flags);
2131 /* and open the tx floodgates */
2132 atomic_set (&dev->tx_qlen, 0);
2133 netif_wake_queue (dev->net);
2134 if (rndis_active(dev)) {
2135 rndis_set_param_medium (dev->rndis_config,
2137 BITRATE(dev->gadget)/100);
2138 (void) rndis_signal_connect (dev->rndis_config);
2142 static int eth_open (struct net_device *net)
2144 struct eth_dev *dev = netdev_priv(net);
2146 DEBUG (dev, "%s\n", __FUNCTION__);
2147 if (netif_carrier_ok (dev->net))
2148 eth_start (dev, GFP_KERNEL);
2152 static int eth_stop (struct net_device *net)
2154 struct eth_dev *dev = netdev_priv(net);
2156 VDEBUG (dev, "%s\n", __FUNCTION__);
2157 netif_stop_queue (net);
2159 DEBUG (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
2160 dev->stats.rx_packets, dev->stats.tx_packets,
2161 dev->stats.rx_errors, dev->stats.tx_errors
2164 /* ensure there are no more active requests */
2166 usb_ep_disable (dev->in_ep);
2167 usb_ep_disable (dev->out_ep);
2168 if (netif_carrier_ok (dev->net)) {
2169 DEBUG (dev, "host still using in/out endpoints\n");
2170 // FIXME idiom may leave toggle wrong here
2171 usb_ep_enable (dev->in_ep, dev->in);
2172 usb_ep_enable (dev->out_ep, dev->out);
2174 if (dev->status_ep) {
2175 usb_ep_disable (dev->status_ep);
2176 usb_ep_enable (dev->status_ep, dev->status);
2180 if (rndis_active(dev)) {
2181 rndis_set_param_medium (dev->rndis_config,
2182 NDIS_MEDIUM_802_3, 0);
2183 (void) rndis_signal_disconnect (dev->rndis_config);
2189 /*-------------------------------------------------------------------------*/
2191 static struct usb_request *
2192 eth_req_alloc (struct usb_ep *ep, unsigned size, gfp_t gfp_flags)
2194 struct usb_request *req;
2196 req = usb_ep_alloc_request (ep, gfp_flags);
2200 req->buf = kmalloc (size, gfp_flags);
2202 usb_ep_free_request (ep, req);
2209 eth_req_free (struct usb_ep *ep, struct usb_request *req)
2212 usb_ep_free_request (ep, req);
2216 static void /* __init_or_exit */
2217 eth_unbind (struct usb_gadget *gadget)
2219 struct eth_dev *dev = get_gadget_data (gadget);
2221 DEBUG (dev, "unbind\n");
2222 rndis_deregister (dev->rndis_config);
2225 /* we've already been disconnected ... no i/o is active */
2227 eth_req_free (gadget->ep0, dev->req);
2230 if (dev->stat_req) {
2231 eth_req_free (dev->status_ep, dev->stat_req);
2232 dev->stat_req = NULL;
2235 unregister_netdev (dev->net);
2236 free_netdev(dev->net);
2238 /* assuming we used keventd, it must quiesce too */
2239 flush_scheduled_work ();
2240 set_gadget_data (gadget, NULL);
2243 static u8 __devinit nibble (unsigned char c)
2245 if (likely (isdigit (c)))
2248 if (likely (isxdigit (c)))
2249 return 10 + c - 'A';
2253 static int __devinit get_ether_addr(const char *str, u8 *dev_addr)
2258 for (i = 0; i < 6; i++) {
2261 if((*str == '.') || (*str == ':'))
2263 num = nibble(*str++) << 4;
2264 num |= (nibble(*str++));
2267 if (is_valid_ether_addr (dev_addr))
2270 random_ether_addr(dev_addr);
2274 static int __devinit
2275 eth_bind (struct usb_gadget *gadget)
2277 struct eth_dev *dev;
2278 struct net_device *net;
2279 u8 cdc = 1, zlp = 1, rndis = 1;
2280 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
2281 int status = -ENOMEM;
2284 /* these flags are only ever cleared; compiler take note */
2285 #ifndef DEV_CONFIG_CDC
2288 #ifndef CONFIG_USB_ETH_RNDIS
2292 /* Because most host side USB stacks handle CDC Ethernet, that
2293 * standard protocol is _strongly_ preferred for interop purposes.
2294 * (By everyone except Microsoft.)
2296 if (gadget_is_pxa (gadget)) {
2297 /* pxa doesn't support altsettings */
2299 } else if (gadget_is_musbhdrc(gadget)) {
2300 /* reduce tx dma overhead by avoiding special cases */
2302 } else if (gadget_is_sh(gadget)) {
2303 /* sh doesn't support multiple interfaces or configs */
2306 } else if (gadget_is_sa1100 (gadget)) {
2307 /* hardware can't write zlps */
2309 /* sa1100 CAN do CDC, without status endpoint ... we use
2310 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2315 gcnum = usb_gadget_controller_number (gadget);
2317 device_desc.bcdDevice = cpu_to_le16 (0x0200 + gcnum);
2319 /* can't assume CDC works. don't want to default to
2320 * anything less functional on CDC-capable hardware,
2321 * so we fail in this case.
2323 dev_err (&gadget->dev,
2324 "controller '%s' not recognized\n",
2328 snprintf (manufacturer, sizeof manufacturer, "%s %s/%s",
2329 init_utsname()->sysname, init_utsname()->release,
2332 /* If there's an RNDIS configuration, that's what Windows wants to
2333 * be using ... so use these product IDs here and in the "linux.inf"
2334 * needed to install MSFT drivers. Current Linux kernels will use
2335 * the second configuration if it's CDC Ethernet, and need some help
2336 * to choose the right configuration otherwise.
2339 device_desc.idVendor =
2340 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2341 device_desc.idProduct =
2342 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2343 snprintf (product_desc, sizeof product_desc,
2344 "RNDIS/%s", driver_desc);
2346 /* CDC subset ... recognized by Linux since 2.4.10, but Windows
2347 * drivers aren't widely available. (That may be improved by
2348 * supporting one submode of the "SAFE" variant of MDLM.)
2351 device_desc.idVendor =
2352 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2353 device_desc.idProduct =
2354 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2357 /* support optional vendor/distro customization */
2360 dev_err (&gadget->dev, "idVendor needs idProduct!\n");
2363 device_desc.idVendor = cpu_to_le16(idVendor);
2364 device_desc.idProduct = cpu_to_le16(idProduct);
2366 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2369 strlcpy (manufacturer, iManufacturer, sizeof manufacturer);
2371 strlcpy (product_desc, iProduct, sizeof product_desc);
2372 if (iSerialNumber) {
2373 device_desc.iSerialNumber = STRING_SERIALNUMBER,
2374 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
2377 /* all we really need is bulk IN/OUT */
2378 usb_ep_autoconfig_reset (gadget);
2379 in_ep = usb_ep_autoconfig (gadget, &fs_source_desc);
2382 dev_err (&gadget->dev,
2383 "can't autoconfigure on %s\n",
2387 in_ep->driver_data = in_ep; /* claim */
2389 out_ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
2392 out_ep->driver_data = out_ep; /* claim */
2394 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2395 /* CDC Ethernet control interface doesn't require a status endpoint.
2396 * Since some hosts expect one, try to allocate one anyway.
2399 status_ep = usb_ep_autoconfig (gadget, &fs_status_desc);
2401 status_ep->driver_data = status_ep; /* claim */
2403 dev_err (&gadget->dev,
2404 "can't run RNDIS on %s\n",
2407 #ifdef DEV_CONFIG_CDC
2408 /* pxa25x only does CDC subset; often used with RNDIS */
2410 control_intf.bNumEndpoints = 0;
2411 /* FIXME remove endpoint from descriptor list */
2417 /* one config: cdc, else minimal subset */
2419 eth_config.bNumInterfaces = 1;
2420 eth_config.iConfiguration = STRING_SUBSET;
2422 /* use functions to set these up, in case we're built to work
2423 * with multiple controllers and must override CDC Ethernet.
2425 fs_subset_descriptors();
2426 hs_subset_descriptors();
2429 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
2430 usb_gadget_set_selfpowered (gadget);
2432 /* For now RNDIS is always a second config */
2434 device_desc.bNumConfigurations = 2;
2436 #ifdef CONFIG_USB_GADGET_DUALSPEED
2438 dev_qualifier.bNumConfigurations = 2;
2440 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2442 /* assumes ep0 uses the same value for both speeds ... */
2443 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2445 /* and that all endpoints are dual-speed */
2446 hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
2447 hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
2448 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2450 hs_status_desc.bEndpointAddress =
2451 fs_status_desc.bEndpointAddress;
2453 #endif /* DUALSPEED */
2455 if (gadget->is_otg) {
2456 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2457 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2458 eth_config.bMaxPower = 4;
2459 #ifdef CONFIG_USB_ETH_RNDIS
2460 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2461 rndis_config.bMaxPower = 4;
2465 net = alloc_etherdev (sizeof *dev);
2468 dev = netdev_priv(net);
2469 spin_lock_init (&dev->lock);
2470 spin_lock_init (&dev->req_lock);
2471 INIT_WORK (&dev->work, eth_work);
2472 INIT_LIST_HEAD (&dev->tx_reqs);
2473 INIT_LIST_HEAD (&dev->rx_reqs);
2475 /* network device setup */
2477 SET_MODULE_OWNER (net);
2478 strcpy (net->name, "usb%d");
2483 dev->out_ep = out_ep;
2484 dev->status_ep = status_ep;
2486 /* Module params for these addresses should come from ID proms.
2487 * The host side address is used with CDC and RNDIS, and commonly
2488 * ends up in a persistent config database. It's not clear if
2489 * host side code for the SAFE thing cares -- its original BLAN
2490 * thing didn't, Sharp never assigned those addresses on Zaurii.
2492 if (get_ether_addr(dev_addr, net->dev_addr))
2493 dev_warn(&gadget->dev,
2494 "using random %s ethernet address\n", "self");
2495 if (get_ether_addr(host_addr, dev->host_mac))
2496 dev_warn(&gadget->dev,
2497 "using random %s ethernet address\n", "host");
2498 snprintf (ethaddr, sizeof ethaddr, "%02X%02X%02X%02X%02X%02X",
2499 dev->host_mac [0], dev->host_mac [1],
2500 dev->host_mac [2], dev->host_mac [3],
2501 dev->host_mac [4], dev->host_mac [5]);
2504 status = rndis_init();
2506 dev_err (&gadget->dev, "can't init RNDIS, %d\n",
2512 net->change_mtu = eth_change_mtu;
2513 net->get_stats = eth_get_stats;
2514 net->hard_start_xmit = eth_start_xmit;
2515 net->open = eth_open;
2516 net->stop = eth_stop;
2517 // watchdog_timeo, tx_timeout ...
2518 // set_multicast_list
2519 SET_ETHTOOL_OPS(net, &ops);
2521 /* preallocate control message data and buffer */
2522 dev->req = eth_req_alloc (gadget->ep0, USB_BUFSIZ, GFP_KERNEL);
2525 dev->req->complete = eth_setup_complete;
2527 /* ... and maybe likewise for status transfer */
2528 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2529 if (dev->status_ep) {
2530 dev->stat_req = eth_req_alloc (dev->status_ep,
2531 STATUS_BYTECOUNT, GFP_KERNEL);
2532 if (!dev->stat_req) {
2533 eth_req_free (gadget->ep0, dev->req);
2536 dev->stat_req->context = NULL;
2540 /* finish hookup to lower layer ... */
2541 dev->gadget = gadget;
2542 set_gadget_data (gadget, dev);
2543 gadget->ep0->driver_data = dev;
2545 /* two kinds of host-initiated state changes:
2546 * - iff DATA transfer is active, carrier is "on"
2547 * - tx queueing enabled if open *and* carrier is "on"
2549 netif_stop_queue (dev->net);
2550 netif_carrier_off (dev->net);
2552 SET_NETDEV_DEV (dev->net, &gadget->dev);
2553 status = register_netdev (dev->net);
2557 INFO (dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
2558 INFO (dev, "using %s, OUT %s IN %s%s%s\n", gadget->name,
2559 out_ep->name, in_ep->name,
2560 status_ep ? " STATUS " : "",
2561 status_ep ? status_ep->name : ""
2563 INFO (dev, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2564 net->dev_addr [0], net->dev_addr [1],
2565 net->dev_addr [2], net->dev_addr [3],
2566 net->dev_addr [4], net->dev_addr [5]);
2569 INFO (dev, "HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2570 dev->host_mac [0], dev->host_mac [1],
2571 dev->host_mac [2], dev->host_mac [3],
2572 dev->host_mac [4], dev->host_mac [5]);
2577 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2579 dev->rndis_config = rndis_register (rndis_control_ack);
2580 if (dev->rndis_config < 0) {
2582 unregister_netdev (dev->net);
2587 /* these set up a lot of the OIDs that RNDIS needs */
2588 rndis_set_host_mac (dev->rndis_config, dev->host_mac);
2589 if (rndis_set_param_dev (dev->rndis_config, dev->net,
2590 &dev->stats, &dev->cdc_filter))
2592 if (rndis_set_param_vendor (dev->rndis_config, vendorID,
2595 if (rndis_set_param_medium (dev->rndis_config,
2599 INFO (dev, "RNDIS ready\n");
2605 dev_dbg(&gadget->dev, "register_netdev failed, %d\n", status);
2607 eth_unbind (gadget);
2611 /*-------------------------------------------------------------------------*/
2614 eth_suspend (struct usb_gadget *gadget)
2616 struct eth_dev *dev = get_gadget_data (gadget);
2618 DEBUG (dev, "suspend\n");
2623 eth_resume (struct usb_gadget *gadget)
2625 struct eth_dev *dev = get_gadget_data (gadget);
2627 DEBUG (dev, "resume\n");
2631 /*-------------------------------------------------------------------------*/
2633 static struct usb_gadget_driver eth_driver = {
2636 .function = (char *) driver_desc,
2638 .unbind = eth_unbind,
2641 .disconnect = eth_disconnect,
2643 .suspend = eth_suspend,
2644 .resume = eth_resume,
2647 .name = (char *) shortname,
2648 .owner = THIS_MODULE,
2652 MODULE_DESCRIPTION (DRIVER_DESC);
2653 MODULE_AUTHOR ("David Brownell, Benedikt Spanger");
2654 MODULE_LICENSE ("GPL");
2657 static int __init init (void)
2659 return usb_gadget_register_driver (ð_driver);
2663 static void __exit cleanup (void)
2665 usb_gadget_unregister_driver (ð_driver);
2667 module_exit (cleanup);