2 * ether.c -- Ethernet gadget driver, with CDC and non-CDC options
4 * Copyright (C) 2003-2004 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/config.h>
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/delay.h>
30 #include <linux/ioport.h>
31 #include <linux/sched.h>
32 #include <linux/slab.h>
33 #include <linux/smp_lock.h>
34 #include <linux/errno.h>
35 #include <linux/init.h>
36 #include <linux/timer.h>
37 #include <linux/list.h>
38 #include <linux/interrupt.h>
39 #include <linux/uts.h>
40 #include <linux/version.h>
41 #include <linux/moduleparam.h>
42 #include <linux/ctype.h>
44 #include <asm/byteorder.h>
47 #include <asm/system.h>
48 #include <asm/uaccess.h>
49 #include <asm/unaligned.h>
51 #include <linux/usb_ch9.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.
76 * Linux supports it, but other host operating systems may not.
77 * (This is a subset of CDC Ethernet.)
79 * A third option is also in use. Rather than CDC Ethernet, or something
80 * simpler, Microsoft pushes their own approach: RNDIS. The published
81 * RNDIS specs are ambiguous and appear to be incomplete, and are also
85 #define DRIVER_DESC "Ethernet Gadget"
86 #define DRIVER_VERSION "Equinox 2004"
88 static const char shortname [] = "ether";
89 static const char driver_desc [] = DRIVER_DESC;
91 #define RX_EXTRA 20 /* guard against rx overflows */
93 #ifdef CONFIG_USB_ETH_RNDIS
96 #define rndis_init() 0
97 #define rndis_exit() do{}while(0)
102 #define container_of list_entry
105 #include <linux/tqueue.h>
106 #define work_struct tq_struct
107 #define INIT_WORK INIT_TQUEUE
108 #define schedule_work schedule_task
109 #define flush_scheduled_work flush_scheduled_tasks
111 static void random_ether_addr (u8 *addr)
113 get_random_bytes (addr, ETH_ALEN);
114 addr [0] &= 0xfe; // clear multicast bit
115 addr [0] |= 0x02; // set local assignment bit (IEEE802)
118 /*-------------------------------------------------------------------------*/
122 struct usb_gadget *gadget;
123 struct usb_request *req; /* for control responses */
126 struct usb_ep *in_ep, *out_ep, *status_ep;
127 const struct usb_endpoint_descriptor
129 struct list_head tx_reqs, rx_reqs;
131 struct net_device *net;
132 struct net_device_stats stats;
135 struct work_struct work;
139 unsigned suspended:1;
142 #define WORK_RX_MEMORY 0
144 u8 host_mac [ETH_ALEN];
147 /* This version autoconfigures as much as possible at run-time.
149 * It also ASSUMES a self-powered device, without remote wakeup,
150 * although remote wakeup support would make sense.
152 static const char *EP_IN_NAME;
153 static const char *EP_OUT_NAME;
154 static const char *EP_STATUS_NAME;
156 /*-------------------------------------------------------------------------*/
158 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
159 * Instead: allocate your own, using normal USB-IF procedures.
162 /* Thanks to NetChip Technologies for donating this product ID.
163 * It's for devices with only CDC Ethernet configurations.
165 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
166 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
168 /* For hardware that can't talk CDC, we use the same vendor ID that
169 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
170 * with pxa250. We're protocol-compatible, if the host-side drivers
171 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
172 * drivers that need to hard-wire endpoint numbers have a hook.
174 * The protocol is a minimal subset of CDC Ether, which works on any bulk
175 * hardware that's not deeply broken ... even on hardware that can't talk
176 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
177 * doesn't handle control-OUT).
179 #define SIMPLE_VENDOR_NUM 0x049f
180 #define SIMPLE_PRODUCT_NUM 0x505a
182 /* For hardware that can talk RNDIS and either of the above protocols,
183 * use this ID ... the windows INF files will know it. Unless it's
184 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
185 * the non-RNDIS configuration.
187 #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
188 #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
191 /* Some systems will want different product identifers published in the
192 * device descriptor, either numbers or strings or both. These string
193 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
196 static ushort __initdata idVendor;
197 module_param(idVendor, ushort, 0);
198 MODULE_PARM_DESC(idVendor, "USB Vendor ID");
200 static ushort __initdata idProduct;
201 module_param(idProduct, ushort, 0);
202 MODULE_PARM_DESC(idProduct, "USB Product ID");
204 static ushort __initdata bcdDevice;
205 module_param(bcdDevice, ushort, 0);
206 MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
208 static char *__initdata iManufacturer;
209 MODULE_PARM(iManufacturer, "s");
210 MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
212 static char *__initdata iProduct;
213 MODULE_PARM(iProduct, "s");
214 MODULE_PARM_DESC(iProduct, "USB Product string");
216 /* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */
217 static char *__initdata dev_addr;
218 MODULE_PARM(dev_addr, "s");
219 MODULE_PARM_DESC(dev_addr, "Device Ethernet Address");
221 /* this address is invisible to ifconfig */
222 static char *__initdata host_addr;
223 MODULE_PARM(host_addr, "s");
224 MODULE_PARM_DESC(host_addr, "Host Ethernet Address");
227 /*-------------------------------------------------------------------------*/
229 /* Include CDC support if we could run on CDC-capable hardware. */
231 #ifdef CONFIG_USB_GADGET_NET2280
232 #define DEV_CONFIG_CDC
235 #ifdef CONFIG_USB_GADGET_DUMMY_HCD
236 #define DEV_CONFIG_CDC
239 #ifdef CONFIG_USB_GADGET_GOKU
240 #define DEV_CONFIG_CDC
243 #ifdef CONFIG_USB_GADGET_LH7A40X
244 #define DEV_CONFIG_CDC
247 #ifdef CONFIG_USB_GADGET_MQ11XX
248 #define DEV_CONFIG_CDC
251 #ifdef CONFIG_USB_GADGET_OMAP
252 #define DEV_CONFIG_CDC
255 #ifdef CONFIG_USB_GADGET_N9604
256 #define DEV_CONFIG_CDC
259 #ifdef CONFIG_USB_GADGET_PXA27X
260 #define DEV_CONFIG_CDC
264 /* For CDC-incapable hardware, choose the simple cdc subset.
265 * Anything that talks bulk (without notable bugs) can do this.
267 #ifdef CONFIG_USB_GADGET_PXA2XX
268 #define DEV_CONFIG_SUBSET
271 #ifdef CONFIG_USB_GADGET_SH
272 #define DEV_CONFIG_SUBSET
275 #ifdef CONFIG_USB_GADGET_SA1100
276 /* use non-CDC for backwards compatibility */
277 #define DEV_CONFIG_SUBSET
281 /*-------------------------------------------------------------------------*/
283 #define DEFAULT_QLEN 2 /* double buffering by default */
285 #ifdef CONFIG_USB_GADGET_DUALSPEED
287 static unsigned qmult = 5;
288 module_param (qmult, uint, 0);
291 /* for dual-speed hardware, use deeper queues at highspeed */
292 #define qlen(gadget) \
293 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
295 /* also defer IRQs on highspeed TX */
296 #define TX_DELAY qmult
298 #define BITRATE(g) ((g->speed == USB_SPEED_HIGH) ? 4800000 : 120000)
300 #else /* full speed (low speed doesn't do bulk) */
301 #define qlen(gadget) DEFAULT_QLEN
303 #define BITRATE(g) (12000)
307 /*-------------------------------------------------------------------------*/
309 #define xprintk(d,level,fmt,args...) \
310 printk(level "%s: " fmt , (d)->net->name , ## args)
314 #define DEBUG(dev,fmt,args...) \
315 xprintk(dev , KERN_DEBUG , fmt , ## args)
317 #define DEBUG(dev,fmt,args...) \
324 #define VDEBUG(dev,fmt,args...) \
328 #define ERROR(dev,fmt,args...) \
329 xprintk(dev , KERN_ERR , fmt , ## args)
330 #define WARN(dev,fmt,args...) \
331 xprintk(dev , KERN_WARNING , fmt , ## args)
332 #define INFO(dev,fmt,args...) \
333 xprintk(dev , KERN_INFO , fmt , ## args)
335 /*-------------------------------------------------------------------------*/
337 /* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
338 * ep0 implementation: descriptors, config management, setup().
339 * also optional class-specific notification interrupt transfer.
343 * DESCRIPTORS ... most are static, but strings and (full) configuration
344 * descriptors are built on demand. For now we do either full CDC, or
345 * our simple subset, with RNDIS as an optional second configuration.
347 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
348 * the class descriptors match a modem (they're ignored; it's really just
349 * Ethernet functionality), they don't need the NOP altsetting, and the
350 * status transfer endpoint isn't optional.
353 #define STRING_MANUFACTURER 1
354 #define STRING_PRODUCT 2
355 #define STRING_ETHADDR 3
356 #define STRING_DATA 4
357 #define STRING_CONTROL 5
358 #define STRING_RNDIS_CONTROL 6
360 #define STRING_SUBSET 8
361 #define STRING_RNDIS 9
363 #define USB_BUFSIZ 256 /* holds our biggest descriptor */
366 * This device advertises one configuration, eth_config, unless RNDIS
367 * is enabled (rndis_config) on hardware supporting at least two configs.
369 * NOTE: Controllers like superh_udc should probably be able to use
370 * an RNDIS-only configuration.
372 * FIXME define some higher-powered configurations to make it easier
373 * to recharge batteries ...
376 #define DEV_CONFIG_VALUE 1 /* cdc or subset */
377 #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
379 static struct usb_device_descriptor
381 .bLength = sizeof device_desc,
382 .bDescriptorType = USB_DT_DEVICE,
384 .bcdUSB = __constant_cpu_to_le16 (0x0200),
386 .bDeviceClass = USB_CLASS_COMM,
387 .bDeviceSubClass = 0,
388 .bDeviceProtocol = 0,
390 .idVendor = __constant_cpu_to_le16 (CDC_VENDOR_NUM),
391 .idProduct = __constant_cpu_to_le16 (CDC_PRODUCT_NUM),
392 .iManufacturer = STRING_MANUFACTURER,
393 .iProduct = STRING_PRODUCT,
394 .bNumConfigurations = 1,
397 static struct usb_otg_descriptor
399 .bLength = sizeof otg_descriptor,
400 .bDescriptorType = USB_DT_OTG,
402 .bmAttributes = USB_OTG_SRP,
405 static struct usb_config_descriptor
407 .bLength = sizeof eth_config,
408 .bDescriptorType = USB_DT_CONFIG,
410 /* compute wTotalLength on the fly */
412 .bConfigurationValue = DEV_CONFIG_VALUE,
413 .iConfiguration = STRING_CDC,
414 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
418 #ifdef CONFIG_USB_ETH_RNDIS
419 static struct usb_config_descriptor
421 .bLength = sizeof rndis_config,
422 .bDescriptorType = USB_DT_CONFIG,
424 /* compute wTotalLength on the fly */
426 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
427 .iConfiguration = STRING_RNDIS,
428 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
434 * Compared to the simple CDC subset, the full CDC Ethernet model adds
435 * three class descriptors, two interface descriptors, optional status
436 * endpoint. Both have a "data" interface and two bulk endpoints.
437 * There are also differences in how control requests are handled.
439 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of
440 * the CDC-ACM (modem) spec.
443 #ifdef DEV_CONFIG_CDC
444 static struct usb_interface_descriptor
446 .bLength = sizeof control_intf,
447 .bDescriptorType = USB_DT_INTERFACE,
449 .bInterfaceNumber = 0,
450 /* status endpoint is optional; this may be patched later */
452 .bInterfaceClass = USB_CLASS_COMM,
453 .bInterfaceSubClass = 6, /* ethernet control model */
454 .bInterfaceProtocol = 0,
455 .iInterface = STRING_CONTROL,
459 #ifdef CONFIG_USB_ETH_RNDIS
460 static const struct usb_interface_descriptor
461 rndis_control_intf = {
462 .bLength = sizeof rndis_control_intf,
463 .bDescriptorType = USB_DT_INTERFACE,
465 .bInterfaceNumber = 0,
467 .bInterfaceClass = USB_CLASS_COMM,
468 .bInterfaceSubClass = 2, /* abstract control model */
469 .bInterfaceProtocol = 0xff, /* vendor specific */
470 .iInterface = STRING_RNDIS_CONTROL,
474 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
476 /* "Header Functional Descriptor" from CDC spec 5.2.3.1 */
480 u8 bDescriptorSubType;
483 } __attribute__ ((packed));
485 static const struct header_desc header_desc = {
486 .bLength = sizeof header_desc,
487 .bDescriptorType = USB_DT_CS_INTERFACE,
488 .bDescriptorSubType = 0,
490 .bcdCDC = __constant_cpu_to_le16 (0x0110),
493 /* "Union Functional Descriptor" from CDC spec 5.2.3.8 */
497 u8 bDescriptorSubType;
499 u8 bMasterInterface0;
501 /* ... and there could be other slave interfaces */
502 } __attribute__ ((packed));
504 static const struct union_desc union_desc = {
505 .bLength = sizeof union_desc,
506 .bDescriptorType = USB_DT_CS_INTERFACE,
507 .bDescriptorSubType = 6,
509 .bMasterInterface0 = 0, /* index of control interface */
510 .bSlaveInterface0 = 1, /* index of DATA interface */
513 #endif /* CDC || RNDIS */
515 #ifdef CONFIG_USB_ETH_RNDIS
517 /* "Call Management Descriptor" from CDC spec 5.2.3.3 */
518 struct call_mgmt_descriptor {
521 u8 bDescriptorSubType;
525 } __attribute__ ((packed));
527 static const struct call_mgmt_descriptor call_mgmt_descriptor = {
528 .bLength = sizeof call_mgmt_descriptor,
529 .bDescriptorType = USB_DT_CS_INTERFACE,
530 .bDescriptorSubType = 0x01,
532 .bmCapabilities = 0x00,
533 .bDataInterface = 0x01,
537 /* "Abstract Control Management Descriptor" from CDC spec 5.2.3.4 */
538 struct acm_descriptor {
541 u8 bDescriptorSubType;
544 } __attribute__ ((packed));
546 static struct acm_descriptor acm_descriptor = {
547 .bLength = sizeof acm_descriptor,
548 .bDescriptorType = USB_DT_CS_INTERFACE,
549 .bDescriptorSubType = 0x02,
551 .bmCapabilities = 0X00,
556 #ifdef DEV_CONFIG_CDC
558 /* "Ethernet Networking Functional Descriptor" from CDC spec 5.2.3.16 */
562 u8 bDescriptorSubType;
565 u32 bmEthernetStatistics;
567 u16 wNumberMCFilters;
568 u8 bNumberPowerFilters;
569 } __attribute__ ((packed));
571 static const struct ether_desc ether_desc = {
572 .bLength = sizeof ether_desc,
573 .bDescriptorType = USB_DT_CS_INTERFACE,
574 .bDescriptorSubType = 0x0f,
576 /* this descriptor actually adds value, surprise! */
577 .iMACAddress = STRING_ETHADDR,
578 .bmEthernetStatistics = __constant_cpu_to_le32 (0), /* no statistics */
579 .wMaxSegmentSize = __constant_cpu_to_le16 (ETH_FRAME_LEN),
580 .wNumberMCFilters = __constant_cpu_to_le16 (0),
581 .bNumberPowerFilters = 0,
586 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
588 /* include the status endpoint if we can, even where it's optional.
589 * use small wMaxPacketSize, since many "interrupt" endpoints have
590 * very small fifos and it's no big deal if CDC_NOTIFY_SPEED_CHANGE
591 * takes two packets. also default to a big transfer interval, to
592 * waste less bandwidth.
594 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
595 * if they ignore the connect/disconnect notifications that real aether
596 * can provide. more advanced cdc configurations might want to support
597 * encapsulated commands (vendor-specific, using control-OUT).
599 * RNDIS requires the status endpoint, since it uses that encapsulation
600 * mechanism for its funky RPC scheme.
603 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
604 #define STATUS_BYTECOUNT 8 /* 8 byte header + data */
606 static struct usb_endpoint_descriptor
608 .bLength = USB_DT_ENDPOINT_SIZE,
609 .bDescriptorType = USB_DT_ENDPOINT,
611 .bEndpointAddress = USB_DIR_IN,
612 .bmAttributes = USB_ENDPOINT_XFER_INT,
613 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
614 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
618 #ifdef DEV_CONFIG_CDC
620 /* the default data interface has no endpoints ... */
622 static const struct usb_interface_descriptor
624 .bLength = sizeof data_nop_intf,
625 .bDescriptorType = USB_DT_INTERFACE,
627 .bInterfaceNumber = 1,
628 .bAlternateSetting = 0,
630 .bInterfaceClass = USB_CLASS_CDC_DATA,
631 .bInterfaceSubClass = 0,
632 .bInterfaceProtocol = 0,
635 /* ... but the "real" data interface has two bulk endpoints */
637 static const struct usb_interface_descriptor
639 .bLength = sizeof data_intf,
640 .bDescriptorType = USB_DT_INTERFACE,
642 .bInterfaceNumber = 1,
643 .bAlternateSetting = 1,
645 .bInterfaceClass = USB_CLASS_CDC_DATA,
646 .bInterfaceSubClass = 0,
647 .bInterfaceProtocol = 0,
648 .iInterface = STRING_DATA,
653 #ifdef CONFIG_USB_ETH_RNDIS
655 /* RNDIS doesn't activate by changing to the "real" altsetting */
657 static const struct usb_interface_descriptor
659 .bLength = sizeof rndis_data_intf,
660 .bDescriptorType = USB_DT_INTERFACE,
662 .bInterfaceNumber = 1,
663 .bAlternateSetting = 0,
665 .bInterfaceClass = USB_CLASS_CDC_DATA,
666 .bInterfaceSubClass = 0,
667 .bInterfaceProtocol = 0,
668 .iInterface = STRING_DATA,
673 #ifdef DEV_CONFIG_SUBSET
676 * "Simple" CDC-subset option is a simple vendor-neutral model that most
677 * full speed controllers can handle: one interface, two bulk endpoints.
680 static const struct usb_interface_descriptor
682 .bLength = sizeof subset_data_intf,
683 .bDescriptorType = USB_DT_INTERFACE,
685 .bInterfaceNumber = 0,
686 .bAlternateSetting = 0,
688 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
689 .bInterfaceSubClass = 0,
690 .bInterfaceProtocol = 0,
691 .iInterface = STRING_DATA,
697 static struct usb_endpoint_descriptor
699 .bLength = USB_DT_ENDPOINT_SIZE,
700 .bDescriptorType = USB_DT_ENDPOINT,
702 .bEndpointAddress = USB_DIR_IN,
703 .bmAttributes = USB_ENDPOINT_XFER_BULK,
706 static struct usb_endpoint_descriptor
708 .bLength = USB_DT_ENDPOINT_SIZE,
709 .bDescriptorType = USB_DT_ENDPOINT,
711 .bEndpointAddress = USB_DIR_OUT,
712 .bmAttributes = USB_ENDPOINT_XFER_BULK,
715 static const struct usb_descriptor_header *fs_eth_function [11] = {
716 (struct usb_descriptor_header *) &otg_descriptor,
717 #ifdef DEV_CONFIG_CDC
718 /* "cdc" mode descriptors */
719 (struct usb_descriptor_header *) &control_intf,
720 (struct usb_descriptor_header *) &header_desc,
721 (struct usb_descriptor_header *) &union_desc,
722 (struct usb_descriptor_header *) ðer_desc,
723 /* NOTE: status endpoint may need to be removed */
724 (struct usb_descriptor_header *) &fs_status_desc,
725 /* data interface, with altsetting */
726 (struct usb_descriptor_header *) &data_nop_intf,
727 (struct usb_descriptor_header *) &data_intf,
728 (struct usb_descriptor_header *) &fs_source_desc,
729 (struct usb_descriptor_header *) &fs_sink_desc,
731 #endif /* DEV_CONFIG_CDC */
734 static inline void __init fs_subset_descriptors(void)
736 #ifdef DEV_CONFIG_SUBSET
737 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
738 fs_eth_function[2] = (struct usb_descriptor_header *) &fs_source_desc;
739 fs_eth_function[3] = (struct usb_descriptor_header *) &fs_sink_desc;
740 fs_eth_function[4] = NULL;
742 fs_eth_function[1] = NULL;
746 #ifdef CONFIG_USB_ETH_RNDIS
747 static const struct usb_descriptor_header *fs_rndis_function [] = {
748 (struct usb_descriptor_header *) &otg_descriptor,
749 /* control interface matches ACM, not Ethernet */
750 (struct usb_descriptor_header *) &rndis_control_intf,
751 (struct usb_descriptor_header *) &header_desc,
752 (struct usb_descriptor_header *) &call_mgmt_descriptor,
753 (struct usb_descriptor_header *) &acm_descriptor,
754 (struct usb_descriptor_header *) &union_desc,
755 (struct usb_descriptor_header *) &fs_status_desc,
756 /* data interface has no altsetting */
757 (struct usb_descriptor_header *) &rndis_data_intf,
758 (struct usb_descriptor_header *) &fs_source_desc,
759 (struct usb_descriptor_header *) &fs_sink_desc,
764 #ifdef CONFIG_USB_GADGET_DUALSPEED
767 * usb 2.0 devices need to expose both high speed and full speed
768 * descriptors, unless they only run at full speed.
771 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
772 static struct usb_endpoint_descriptor
774 .bLength = USB_DT_ENDPOINT_SIZE,
775 .bDescriptorType = USB_DT_ENDPOINT,
777 .bmAttributes = USB_ENDPOINT_XFER_INT,
778 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
779 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
781 #endif /* DEV_CONFIG_CDC */
783 static struct usb_endpoint_descriptor
785 .bLength = USB_DT_ENDPOINT_SIZE,
786 .bDescriptorType = USB_DT_ENDPOINT,
788 .bmAttributes = USB_ENDPOINT_XFER_BULK,
789 .wMaxPacketSize = __constant_cpu_to_le16 (512),
792 static struct usb_endpoint_descriptor
794 .bLength = USB_DT_ENDPOINT_SIZE,
795 .bDescriptorType = USB_DT_ENDPOINT,
797 .bmAttributes = USB_ENDPOINT_XFER_BULK,
798 .wMaxPacketSize = __constant_cpu_to_le16 (512),
801 static struct usb_qualifier_descriptor
803 .bLength = sizeof dev_qualifier,
804 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
806 .bcdUSB = __constant_cpu_to_le16 (0x0200),
807 .bDeviceClass = USB_CLASS_COMM,
809 .bNumConfigurations = 1,
812 static const struct usb_descriptor_header *hs_eth_function [11] = {
813 (struct usb_descriptor_header *) &otg_descriptor,
814 #ifdef DEV_CONFIG_CDC
815 /* "cdc" mode descriptors */
816 (struct usb_descriptor_header *) &control_intf,
817 (struct usb_descriptor_header *) &header_desc,
818 (struct usb_descriptor_header *) &union_desc,
819 (struct usb_descriptor_header *) ðer_desc,
820 /* NOTE: status endpoint may need to be removed */
821 (struct usb_descriptor_header *) &hs_status_desc,
822 /* data interface, with altsetting */
823 (struct usb_descriptor_header *) &data_nop_intf,
824 (struct usb_descriptor_header *) &data_intf,
825 (struct usb_descriptor_header *) &hs_source_desc,
826 (struct usb_descriptor_header *) &hs_sink_desc,
828 #endif /* DEV_CONFIG_CDC */
831 static inline void __init hs_subset_descriptors(void)
833 #ifdef DEV_CONFIG_SUBSET
834 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
835 hs_eth_function[2] = (struct usb_descriptor_header *) &fs_source_desc;
836 hs_eth_function[3] = (struct usb_descriptor_header *) &fs_sink_desc;
837 hs_eth_function[4] = NULL;
839 hs_eth_function[1] = NULL;
843 #ifdef CONFIG_USB_ETH_RNDIS
844 static const struct usb_descriptor_header *hs_rndis_function [] = {
845 (struct usb_descriptor_header *) &otg_descriptor,
846 /* control interface matches ACM, not Ethernet */
847 (struct usb_descriptor_header *) &rndis_control_intf,
848 (struct usb_descriptor_header *) &header_desc,
849 (struct usb_descriptor_header *) &call_mgmt_descriptor,
850 (struct usb_descriptor_header *) &acm_descriptor,
851 (struct usb_descriptor_header *) &union_desc,
852 (struct usb_descriptor_header *) &hs_status_desc,
853 /* data interface has no altsetting */
854 (struct usb_descriptor_header *) &rndis_data_intf,
855 (struct usb_descriptor_header *) &hs_source_desc,
856 (struct usb_descriptor_header *) &hs_sink_desc,
862 /* maxpacket and other transfer characteristics vary by speed. */
863 #define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
867 /* if there's no high speed support, maxpacket doesn't change. */
868 #define ep_desc(g,hs,fs) fs
870 static inline void __init hs_subset_descriptors(void)
874 #endif /* !CONFIG_USB_GADGET_DUALSPEED */
876 /*-------------------------------------------------------------------------*/
878 /* descriptors that are built on-demand */
880 static char manufacturer [50];
881 static char product_desc [40] = DRIVER_DESC;
883 #ifdef DEV_CONFIG_CDC
884 /* address that the host will use ... usually assigned at random */
885 static char ethaddr [2 * ETH_ALEN + 1];
888 /* static strings, in UTF-8 */
889 static struct usb_string strings [] = {
890 { STRING_MANUFACTURER, manufacturer, },
891 { STRING_PRODUCT, product_desc, },
892 { STRING_DATA, "Ethernet Data", },
893 #ifdef DEV_CONFIG_CDC
894 { STRING_CDC, "CDC Ethernet", },
895 { STRING_ETHADDR, ethaddr, },
896 { STRING_CONTROL, "CDC Communications Control", },
898 #ifdef DEV_CONFIG_SUBSET
899 { STRING_SUBSET, "CDC Ethernet Subset", },
901 #ifdef CONFIG_USB_ETH_RNDIS
902 { STRING_RNDIS, "RNDIS", },
903 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
905 { } /* end of list */
908 static struct usb_gadget_strings stringtab = {
909 .language = 0x0409, /* en-us */
914 * one config, two interfaces: control, data.
915 * complications: class descriptors, and an altsetting.
918 config_buf (enum usb_device_speed speed,
920 unsigned index, int is_otg)
923 const struct usb_config_descriptor *config;
924 const struct usb_descriptor_header **function;
925 #ifdef CONFIG_USB_GADGET_DUALSPEED
926 int hs = (speed == USB_SPEED_HIGH);
928 if (type == USB_DT_OTHER_SPEED_CONFIG)
930 #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
932 #define which_fn(t) (fs_ ## t ## _function)
935 if (index >= device_desc.bNumConfigurations)
938 #ifdef CONFIG_USB_ETH_RNDIS
939 /* list the RNDIS config first, to make Microsoft's drivers
940 * happy. DOCSIS 1.0 needs this too.
942 if (device_desc.bNumConfigurations == 2 && index == 0) {
943 config = &rndis_config;
944 function = which_fn (rndis);
948 config = ð_config;
949 function = which_fn (eth);
952 /* for now, don't advertise srp-only devices */
956 len = usb_gadget_config_buf (config, buf, USB_BUFSIZ, function);
959 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
963 /*-------------------------------------------------------------------------*/
965 static void eth_start (struct eth_dev *dev, int gfp_flags);
966 static int alloc_requests (struct eth_dev *dev, unsigned n, int gfp_flags);
968 #ifdef DEV_CONFIG_CDC
969 static inline int ether_alt_ep_setup (struct eth_dev *dev, struct usb_ep *ep)
971 const struct usb_endpoint_descriptor *d;
973 /* With CDC, the host isn't allowed to use these two data
974 * endpoints in the default altsetting for the interface.
975 * so we don't activate them yet. Reset from SET_INTERFACE.
977 * Strictly speaking RNDIS should work the same: activation is
978 * a side effect of setting a packet filter. Deactivation is
979 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
982 /* one endpoint writes data back IN to the host */
983 if (strcmp (ep->name, EP_IN_NAME) == 0) {
984 d = ep_desc (dev->gadget, &hs_source_desc, &fs_source_desc);
985 ep->driver_data = dev;
989 /* one endpoint just reads OUT packets */
990 } else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
991 d = ep_desc (dev->gadget, &hs_sink_desc, &fs_sink_desc);
992 ep->driver_data = dev;
996 /* optional status/notification endpoint */
997 } else if (EP_STATUS_NAME &&
998 strcmp (ep->name, EP_STATUS_NAME) == 0) {
1001 d = ep_desc (dev->gadget, &hs_status_desc, &fs_status_desc);
1002 result = usb_ep_enable (ep, d);
1006 ep->driver_data = dev;
1007 dev->status_ep = ep;
1014 #if defined(DEV_CONFIG_SUBSET) || defined(CONFIG_USB_ETH_RNDIS)
1015 static inline int ether_ep_setup (struct eth_dev *dev, struct usb_ep *ep)
1018 const struct usb_endpoint_descriptor *d;
1020 /* CDC subset is simpler: if the device is there,
1021 * it's live with rx and tx endpoints.
1023 * Do this as a shortcut for RNDIS too.
1026 /* one endpoint writes data back IN to the host */
1027 if (strcmp (ep->name, EP_IN_NAME) == 0) {
1028 d = ep_desc (dev->gadget, &hs_source_desc, &fs_source_desc);
1029 result = usb_ep_enable (ep, d);
1033 ep->driver_data = dev;
1037 /* one endpoint just reads OUT packets */
1038 } else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
1039 d = ep_desc (dev->gadget, &hs_sink_desc, &fs_sink_desc);
1040 result = usb_ep_enable (ep, d);
1044 ep->driver_data = dev;
1054 set_ether_config (struct eth_dev *dev, int gfp_flags)
1058 struct usb_gadget *gadget = dev->gadget;
1060 gadget_for_each_ep (ep, gadget) {
1061 #ifdef DEV_CONFIG_CDC
1062 if (!dev->rndis && dev->cdc) {
1063 result = ether_alt_ep_setup (dev, ep);
1069 #ifdef CONFIG_USB_ETH_RNDIS
1070 if (dev->rndis && strcmp (ep->name, EP_STATUS_NAME) == 0) {
1071 const struct usb_endpoint_descriptor *d;
1072 d = ep_desc (gadget, &hs_status_desc, &fs_status_desc);
1073 result = usb_ep_enable (ep, d);
1075 ep->driver_data = dev;
1076 dev->status_ep = ep;
1084 #if defined(DEV_CONFIG_SUBSET) || defined(CONFIG_USB_ETH_RNDIS)
1085 result = ether_ep_setup (dev, ep);
1092 ERROR (dev, "can't enable %s, result %d\n", ep->name, result);
1095 if (!result && (!dev->in_ep || !dev->out_ep))
1099 result = alloc_requests (dev, qlen (gadget), gfp_flags);
1101 /* on error, disable any endpoints */
1103 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
1105 (void) usb_ep_disable (dev->status_ep);
1107 dev->status_ep = NULL;
1109 #if defined(DEV_CONFIG_SUBSET) || defined(CONFIG_USB_ETH_RNDIS)
1110 if (dev->rndis || !dev->cdc) {
1112 (void) usb_ep_disable (dev->in_ep);
1114 (void) usb_ep_disable (dev->out_ep);
1123 /* activate non-CDC configs right away
1124 * this isn't strictly according to the RNDIS spec
1126 #if defined(DEV_CONFIG_SUBSET) || defined(CONFIG_USB_ETH_RNDIS)
1127 if (dev->rndis || !dev->cdc) {
1128 netif_carrier_on (dev->net);
1129 if (netif_running (dev->net)) {
1130 spin_unlock (&dev->lock);
1131 eth_start (dev, GFP_ATOMIC);
1132 spin_lock (&dev->lock);
1138 DEBUG (dev, "qlen %d\n", qlen (gadget));
1140 /* caller is responsible for cleanup on error */
1144 static void eth_reset_config (struct eth_dev *dev)
1146 struct usb_request *req;
1148 if (dev->config == 0)
1151 DEBUG (dev, "%s\n", __FUNCTION__);
1153 netif_stop_queue (dev->net);
1154 netif_carrier_off (dev->net);
1156 /* disable endpoints, forcing (synchronous) completion of
1157 * pending i/o. then free the requests.
1160 usb_ep_disable (dev->in_ep);
1161 while (likely (!list_empty (&dev->tx_reqs))) {
1162 req = container_of (dev->tx_reqs.next,
1163 struct usb_request, list);
1164 list_del (&req->list);
1165 usb_ep_free_request (dev->in_ep, req);
1170 usb_ep_disable (dev->out_ep);
1171 while (likely (!list_empty (&dev->rx_reqs))) {
1172 req = container_of (dev->rx_reqs.next,
1173 struct usb_request, list);
1174 list_del (&req->list);
1175 usb_ep_free_request (dev->out_ep, req);
1180 if (dev->status_ep) {
1181 usb_ep_disable (dev->status_ep);
1182 dev->status_ep = NULL;
1187 /* change our operational config. must agree with the code
1188 * that returns config descriptors, and altsetting code.
1191 eth_set_config (struct eth_dev *dev, unsigned number, int gfp_flags)
1194 struct usb_gadget *gadget = dev->gadget;
1196 if (number == dev->config)
1199 if (gadget_is_sa1100 (gadget)
1201 && atomic_read (&dev->tx_qlen) != 0) {
1202 /* tx fifo is full, but we can't clear it...*/
1203 INFO (dev, "can't change configurations\n");
1206 eth_reset_config (dev);
1208 /* default: pass all packets, no multicast filtering */
1209 dev->cdc_filter = 0x000f;
1212 case DEV_CONFIG_VALUE:
1214 result = set_ether_config (dev, gfp_flags);
1216 #ifdef CONFIG_USB_ETH_RNDIS
1217 case DEV_RNDIS_CONFIG_VALUE:
1219 result = set_ether_config (dev, gfp_flags);
1231 eth_reset_config (dev);
1232 usb_gadget_vbus_draw(dev->gadget,
1233 dev->gadget->is_otg ? 8 : 100);
1238 power = 2 * eth_config.bMaxPower;
1239 usb_gadget_vbus_draw(dev->gadget, power);
1241 switch (gadget->speed) {
1242 case USB_SPEED_FULL: speed = "full"; break;
1243 #ifdef CONFIG_USB_GADGET_DUALSPEED
1244 case USB_SPEED_HIGH: speed = "high"; break;
1246 default: speed = "?"; break;
1249 dev->config = number;
1250 INFO (dev, "%s speed config #%d: %d mA, %s, using %s\n",
1251 speed, number, power, driver_desc,
1256 : "CDC Ethernet Subset"));
1261 /*-------------------------------------------------------------------------*/
1263 /* section 3.8.2 table 11 of the CDC spec lists Ethernet notifications
1264 * section 3.6.2.1 table 5 specifies ACM notifications, accepted by RNDIS
1265 * and RNDIS also defines its own bit-incompatible notifications
1267 #define CDC_NOTIFY_NETWORK_CONNECTION 0x00 /* required; 6.3.1 */
1268 #define CDC_NOTIFY_RESPONSE_AVAILABLE 0x01 /* optional; 6.3.2 */
1269 #define CDC_NOTIFY_SPEED_CHANGE 0x2a /* required; 6.3.8 */
1271 #ifdef DEV_CONFIG_CDC
1273 struct cdc_notification {
1275 u8 bNotificationType;
1280 /* SPEED_CHANGE data looks like this */
1284 static void eth_status_complete (struct usb_ep *ep, struct usb_request *req)
1286 struct cdc_notification *event = req->buf;
1287 int value = req->status;
1288 struct eth_dev *dev = ep->driver_data;
1290 /* issue the second notification if host reads the first */
1291 if (event->bNotificationType == CDC_NOTIFY_NETWORK_CONNECTION
1293 event->bmRequestType = 0xA1;
1294 event->bNotificationType = CDC_NOTIFY_SPEED_CHANGE;
1295 event->wValue = __constant_cpu_to_le16 (0);
1296 event->wIndex = __constant_cpu_to_le16 (1);
1297 event->wLength = __constant_cpu_to_le16 (8);
1299 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1300 event->data [0] = event->data [1] =
1301 (dev->gadget->speed == USB_SPEED_HIGH)
1302 ? (13 * 512 * 8 * 1000 * 8)
1303 : (19 * 64 * 1 * 1000 * 8);
1306 value = usb_ep_queue (ep, req, GFP_ATOMIC);
1307 DEBUG (dev, "send SPEED_CHANGE --> %d\n", value);
1311 DEBUG (dev, "event %02x --> %d\n",
1312 event->bNotificationType, value);
1314 /* free when done */
1315 usb_ep_free_buffer (ep, req->buf, req->dma, 16);
1316 usb_ep_free_request (ep, req);
1319 static void issue_start_status (struct eth_dev *dev)
1321 struct usb_request *req;
1322 struct cdc_notification *event;
1325 DEBUG (dev, "%s, flush old status first\n", __FUNCTION__);
1329 * FIXME ugly idiom, maybe we'd be better with just
1330 * a "cancel the whole queue" primitive since any
1331 * unlink-one primitive has way too many error modes.
1332 * here, we "know" toggle is already clear...
1334 usb_ep_disable (dev->status_ep);
1335 usb_ep_enable (dev->status_ep, dev->status);
1337 /* FIXME make these allocations static like dev->req */
1338 req = usb_ep_alloc_request (dev->status_ep, GFP_ATOMIC);
1340 DEBUG (dev, "status ENOMEM\n");
1343 req->buf = usb_ep_alloc_buffer (dev->status_ep, 16,
1344 &dev->req->dma, GFP_ATOMIC);
1345 if (req->buf == 0) {
1346 DEBUG (dev, "status buf ENOMEM\n");
1348 usb_ep_free_request (dev->status_ep, req);
1352 /* 3.8.1 says to issue first NETWORK_CONNECTION, then
1353 * a SPEED_CHANGE. could be useful in some configs.
1356 event->bmRequestType = 0xA1;
1357 event->bNotificationType = CDC_NOTIFY_NETWORK_CONNECTION;
1358 event->wValue = __constant_cpu_to_le16 (1); /* connected */
1359 event->wIndex = __constant_cpu_to_le16 (1);
1363 req->complete = eth_status_complete;
1364 value = usb_ep_queue (dev->status_ep, req, GFP_ATOMIC);
1366 DEBUG (dev, "status buf queue --> %d\n", value);
1367 usb_ep_free_buffer (dev->status_ep,
1368 req->buf, dev->req->dma, 16);
1375 /*-------------------------------------------------------------------------*/
1377 static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req)
1379 if (req->status || req->actual != req->length)
1380 DEBUG ((struct eth_dev *) ep->driver_data,
1381 "setup complete --> %d, %d/%d\n",
1382 req->status, req->actual, req->length);
1385 /* see section 3.8.2 table 10 of the CDC spec for more ethernet
1386 * requests, mostly for filters (multicast, pm) and statistics
1387 * section 3.6.2.1 table 4 has ACM requests; RNDIS requires the
1388 * encapsulated command mechanism.
1390 #define CDC_SEND_ENCAPSULATED_COMMAND 0x00 /* optional */
1391 #define CDC_GET_ENCAPSULATED_RESPONSE 0x01 /* optional */
1392 #define CDC_SET_ETHERNET_MULTICAST_FILTERS 0x40 /* optional */
1393 #define CDC_SET_ETHERNET_PM_PATTERN_FILTER 0x41 /* optional */
1394 #define CDC_GET_ETHERNET_PM_PATTERN_FILTER 0x42 /* optional */
1395 #define CDC_SET_ETHERNET_PACKET_FILTER 0x43 /* required */
1396 #define CDC_GET_ETHERNET_STATISTIC 0x44 /* optional */
1398 /* table 62; bits in cdc_filter */
1399 #define CDC_PACKET_TYPE_PROMISCUOUS (1 << 0)
1400 #define CDC_PACKET_TYPE_ALL_MULTICAST (1 << 1) /* no filter */
1401 #define CDC_PACKET_TYPE_DIRECTED (1 << 2)
1402 #define CDC_PACKET_TYPE_BROADCAST (1 << 3)
1403 #define CDC_PACKET_TYPE_MULTICAST (1 << 4) /* filtered */
1405 #ifdef CONFIG_USB_ETH_RNDIS
1407 static void rndis_response_complete (struct usb_ep *ep, struct usb_request *req)
1409 if (req->status || req->actual != req->length)
1410 DEBUG ((struct eth_dev *) ep->driver_data,
1411 "rndis response complete --> %d, %d/%d\n",
1412 req->status, req->actual, req->length);
1414 /* done sending after CDC_GET_ENCAPSULATED_RESPONSE */
1417 static void rndis_command_complete (struct usb_ep *ep, struct usb_request *req)
1419 struct eth_dev *dev = ep->driver_data;
1422 /* received RNDIS command from CDC_SEND_ENCAPSULATED_COMMAND */
1423 spin_lock(&dev->lock);
1424 status = rndis_msg_parser (dev->rndis_config, (u8 *) req->buf);
1426 ERROR(dev, "%s: rndis parse error %d\n", __FUNCTION__, status);
1427 spin_unlock(&dev->lock);
1433 * The setup() callback implements all the ep0 functionality that's not
1434 * handled lower down. CDC has a number of less-common features:
1436 * - two interfaces: control, and ethernet data
1437 * - Ethernet data interface has two altsettings: default, and active
1438 * - class-specific descriptors for the control interface
1439 * - class-specific control requests
1442 eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1444 struct eth_dev *dev = get_gadget_data (gadget);
1445 struct usb_request *req = dev->req;
1446 int value = -EOPNOTSUPP;
1448 /* descriptors just go into the pre-allocated ep0 buffer,
1449 * while config change events may enable network traffic.
1451 req->complete = eth_setup_complete;
1452 switch (ctrl->bRequest) {
1454 case USB_REQ_GET_DESCRIPTOR:
1455 if (ctrl->bRequestType != USB_DIR_IN)
1457 switch (ctrl->wValue >> 8) {
1460 value = min (ctrl->wLength, (u16) sizeof device_desc);
1461 memcpy (req->buf, &device_desc, value);
1463 #ifdef CONFIG_USB_GADGET_DUALSPEED
1464 case USB_DT_DEVICE_QUALIFIER:
1465 if (!gadget->is_dualspeed)
1467 value = min (ctrl->wLength, (u16) sizeof dev_qualifier);
1468 memcpy (req->buf, &dev_qualifier, value);
1471 case USB_DT_OTHER_SPEED_CONFIG:
1472 if (!gadget->is_dualspeed)
1475 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1477 value = config_buf (gadget->speed, req->buf,
1479 ctrl->wValue & 0xff,
1482 value = min (ctrl->wLength, (u16) value);
1486 value = usb_gadget_get_string (&stringtab,
1487 ctrl->wValue & 0xff, req->buf);
1489 value = min (ctrl->wLength, (u16) value);
1494 case USB_REQ_SET_CONFIGURATION:
1495 if (ctrl->bRequestType != 0)
1497 if (gadget->a_hnp_support)
1498 DEBUG (dev, "HNP available\n");
1499 else if (gadget->a_alt_hnp_support)
1500 DEBUG (dev, "HNP needs a different root port\n");
1501 spin_lock (&dev->lock);
1502 value = eth_set_config (dev, ctrl->wValue, GFP_ATOMIC);
1503 spin_unlock (&dev->lock);
1505 case USB_REQ_GET_CONFIGURATION:
1506 if (ctrl->bRequestType != USB_DIR_IN)
1508 *(u8 *)req->buf = dev->config;
1509 value = min (ctrl->wLength, (u16) 1);
1512 case USB_REQ_SET_INTERFACE:
1513 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1515 || ctrl->wIndex > 1)
1517 if (!dev->cdc && ctrl->wIndex != 0)
1519 spin_lock (&dev->lock);
1521 /* PXA hardware partially handles SET_INTERFACE;
1522 * we need to kluge around that interference.
1524 if (gadget_is_pxa (gadget)) {
1525 value = eth_set_config (dev, DEV_CONFIG_VALUE,
1530 #ifdef DEV_CONFIG_CDC
1531 switch (ctrl->wIndex) {
1532 case 0: /* control/master intf */
1533 if (ctrl->wValue != 0)
1535 if (dev->status_ep) {
1536 usb_ep_disable (dev->status_ep);
1537 usb_ep_enable (dev->status_ep, dev->status);
1541 case 1: /* data intf */
1542 if (ctrl->wValue > 1)
1544 usb_ep_disable (dev->in_ep);
1545 usb_ep_disable (dev->out_ep);
1547 /* CDC requires the data transfers not be done from
1548 * the default interface setting ... also, setting
1549 * the non-default interface clears filters etc.
1551 if (ctrl->wValue == 1) {
1552 usb_ep_enable (dev->in_ep, dev->in);
1553 usb_ep_enable (dev->out_ep, dev->out);
1554 netif_carrier_on (dev->net);
1556 issue_start_status (dev);
1557 if (netif_running (dev->net)) {
1558 spin_unlock (&dev->lock);
1559 eth_start (dev, GFP_ATOMIC);
1560 spin_lock (&dev->lock);
1563 netif_stop_queue (dev->net);
1564 netif_carrier_off (dev->net);
1570 /* FIXME this is wrong, as is the assumption that
1571 * all non-PXA hardware talks real CDC ...
1573 WARN(dev, "set_interface ignored!\n");
1574 #endif /* DEV_CONFIG_CDC */
1577 spin_unlock (&dev->lock);
1579 case USB_REQ_GET_INTERFACE:
1580 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1582 || ctrl->wIndex > 1)
1584 if (!(dev->cdc || dev->rndis) && ctrl->wIndex != 0)
1587 /* for CDC, iff carrier is on, data interface is active. */
1588 if (dev->rndis || ctrl->wIndex != 1)
1589 *(u8 *)req->buf = 0;
1591 *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0;
1592 value = min (ctrl->wLength, (u16) 1);
1595 #ifdef DEV_CONFIG_CDC
1596 case CDC_SET_ETHERNET_PACKET_FILTER:
1597 /* see 6.2.30: no data, wIndex = interface,
1598 * wValue = packet filter bitmap
1600 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1603 || ctrl->wLength != 0
1604 || ctrl->wIndex > 1)
1606 DEBUG (dev, "NOP packet filter %04x\n", ctrl->wValue);
1607 /* NOTE: table 62 has 5 filter bits to reduce traffic,
1608 * and we "must" support multicast and promiscuous.
1609 * this NOP implements a bad filter (always promisc)
1611 dev->cdc_filter = ctrl->wValue;
1614 #endif /* DEV_CONFIG_CDC */
1616 #ifdef CONFIG_USB_ETH_RNDIS
1617 /* RNDIS uses the CDC command encapsulation mechanism to implement
1618 * an RPC scheme, with much getting/setting of attributes by OID.
1620 case CDC_SEND_ENCAPSULATED_COMMAND:
1621 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1623 || ctrl->wLength > USB_BUFSIZ
1625 || rndis_control_intf.bInterfaceNumber
1628 /* read the request, then process it */
1629 value = ctrl->wLength;
1630 req->complete = rndis_command_complete;
1631 /* later, rndis_control_ack () sends a notification */
1634 case CDC_GET_ENCAPSULATED_RESPONSE:
1635 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1636 == ctrl->bRequestType
1638 // && ctrl->wLength >= 0x0400
1640 && rndis_control_intf.bInterfaceNumber
1644 /* return the result */
1645 buf = rndis_get_next_response (dev->rndis_config,
1648 memcpy (req->buf, buf, value);
1649 req->complete = rndis_response_complete;
1650 rndis_free_response(dev->rndis_config, buf);
1652 /* else stalls ... spec says to avoid that */
1659 "unknown control req%02x.%02x v%04x i%04x l%d\n",
1660 ctrl->bRequestType, ctrl->bRequest,
1661 ctrl->wValue, ctrl->wIndex, ctrl->wLength);
1664 /* respond with data transfer before status phase? */
1666 req->length = value;
1667 req->zero = value < ctrl->wLength
1668 && (value % gadget->ep0->maxpacket) == 0;
1669 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1671 DEBUG (dev, "ep_queue --> %d\n", value);
1673 eth_setup_complete (gadget->ep0, req);
1677 /* host either stalls (value < 0) or reports success */
1682 eth_disconnect (struct usb_gadget *gadget)
1684 struct eth_dev *dev = get_gadget_data (gadget);
1685 unsigned long flags;
1687 spin_lock_irqsave (&dev->lock, flags);
1688 netif_stop_queue (dev->net);
1689 netif_carrier_off (dev->net);
1690 eth_reset_config (dev);
1691 spin_unlock_irqrestore (&dev->lock, flags);
1693 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1695 /* next we may get setup() calls to enumerate new connections;
1696 * or an unbind() during shutdown (including removing module).
1700 /*-------------------------------------------------------------------------*/
1702 /* NETWORK DRIVER HOOKUP (to the layer above this driver) */
1704 static int eth_change_mtu (struct net_device *net, int new_mtu)
1706 struct eth_dev *dev = netdev_priv(net);
1708 // FIXME if rndis, don't change while link's live
1710 if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN)
1712 /* no zero-length packet read wanted after mtu-sized packets */
1713 if (((new_mtu + sizeof (struct ethhdr)) % dev->in_ep->maxpacket) == 0)
1719 static struct net_device_stats *eth_get_stats (struct net_device *net)
1721 return &((struct eth_dev *)netdev_priv(net))->stats;
1724 static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
1726 struct eth_dev *dev = netdev_priv(net);
1727 memset(p, 0, sizeof *p);
1728 strncpy(p->driver, shortname, sizeof p->driver);
1729 strncpy(p->version, DRIVER_VERSION, sizeof p->version);
1730 strncpy(p->fw_version, dev->gadget->name, sizeof p->fw_version);
1731 strncpy (p->bus_info, dev->gadget->dev.bus_id, sizeof p->bus_info);
1734 static u32 eth_get_link(struct net_device *net)
1736 struct eth_dev *dev = netdev_priv(net);
1737 return dev->gadget->speed != USB_SPEED_UNKNOWN;
1740 static struct ethtool_ops ops = {
1741 .get_drvinfo = eth_get_drvinfo,
1742 .get_link = eth_get_link
1745 static void defer_kevent (struct eth_dev *dev, int flag)
1747 if (test_and_set_bit (flag, &dev->todo))
1749 if (!schedule_work (&dev->work))
1750 ERROR (dev, "kevent %d may have been dropped\n", flag);
1752 DEBUG (dev, "kevent %d scheduled\n", flag);
1755 static void rx_complete (struct usb_ep *ep, struct usb_request *req);
1758 rx_submit (struct eth_dev *dev, struct usb_request *req, int gfp_flags)
1760 struct sk_buff *skb;
1761 int retval = -ENOMEM;
1764 /* Padding up to RX_EXTRA handles minor disagreements with host.
1765 * Normally we use the USB "terminate on short read" convention;
1766 * so allow up to (N*maxpacket), since that memory is normally
1767 * already allocated. Some hardware doesn't deal well with short
1768 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1769 * byte off the end (to force hardware errors on overflow).
1771 * RNDIS uses internal framing, and explicitly allows senders to
1772 * pad to end-of-packet. That's potentially nice for speed,
1773 * but means receivers can't recover synch on their own.
1775 size = (sizeof (struct ethhdr) + dev->net->mtu + RX_EXTRA);
1776 size += dev->out_ep->maxpacket - 1;
1777 #ifdef CONFIG_USB_ETH_RNDIS
1779 size += sizeof (struct rndis_packet_msg_type);
1781 size -= size % dev->out_ep->maxpacket;
1783 if ((skb = alloc_skb (size, gfp_flags)) == 0) {
1784 DEBUG (dev, "no rx skb\n");
1788 req->buf = skb->data;
1790 req->complete = rx_complete;
1793 retval = usb_ep_queue (dev->out_ep, req, gfp_flags);
1794 if (retval == -ENOMEM)
1796 defer_kevent (dev, WORK_RX_MEMORY);
1798 DEBUG (dev, "rx submit --> %d\n", retval);
1799 dev_kfree_skb_any (skb);
1800 spin_lock (&dev->lock);
1801 list_add (&req->list, &dev->rx_reqs);
1802 spin_unlock (&dev->lock);
1807 static void rx_complete (struct usb_ep *ep, struct usb_request *req)
1809 struct sk_buff *skb = req->context;
1810 struct eth_dev *dev = ep->driver_data;
1811 int status = req->status;
1815 /* normal completion */
1817 skb_put (skb, req->actual);
1818 #ifdef CONFIG_USB_ETH_RNDIS
1819 /* we know MaxPacketsPerTransfer == 1 here */
1821 rndis_rm_hdr (req->buf, &(skb->len));
1823 if (ETH_HLEN > skb->len || skb->len > ETH_FRAME_LEN) {
1824 dev->stats.rx_errors++;
1825 dev->stats.rx_length_errors++;
1826 DEBUG (dev, "rx length %d\n", skb->len);
1830 skb->dev = dev->net;
1831 skb->protocol = eth_type_trans (skb, dev->net);
1832 dev->stats.rx_packets++;
1833 dev->stats.rx_bytes += skb->len;
1835 /* no buffer copies needed, unless hardware can't
1838 status = netif_rx (skb);
1842 /* software-driven interface shutdown */
1843 case -ECONNRESET: // unlink
1844 case -ESHUTDOWN: // disconnect etc
1845 VDEBUG (dev, "rx shutdown, code %d\n", status);
1848 /* for hardware automagic (such as pxa) */
1849 case -ECONNABORTED: // endpoint reset
1850 DEBUG (dev, "rx %s reset\n", ep->name);
1851 defer_kevent (dev, WORK_RX_MEMORY);
1853 dev_kfree_skb_any (skb);
1858 dev->stats.rx_over_errors++;
1862 dev->stats.rx_errors++;
1863 DEBUG (dev, "rx status %d\n", status);
1868 dev_kfree_skb_any (skb);
1869 if (!netif_running (dev->net)) {
1871 /* nobody reading rx_reqs, so no dev->lock */
1872 list_add (&req->list, &dev->rx_reqs);
1876 rx_submit (dev, req, GFP_ATOMIC);
1879 static int prealloc (struct list_head *list, struct usb_ep *ep,
1880 unsigned n, int gfp_flags)
1883 struct usb_request *req;
1888 /* queue/recycle up to N requests */
1890 list_for_each_entry (req, list, list) {
1895 req = usb_ep_alloc_request (ep, gfp_flags);
1897 return list_empty (list) ? -ENOMEM : 0;
1898 list_add (&req->list, list);
1905 struct list_head *next;
1907 next = req->list.next;
1908 list_del (&req->list);
1909 usb_ep_free_request (ep, req);
1914 req = container_of (next, struct usb_request, list);
1919 static int alloc_requests (struct eth_dev *dev, unsigned n, int gfp_flags)
1923 status = prealloc (&dev->tx_reqs, dev->in_ep, n, gfp_flags);
1926 status = prealloc (&dev->rx_reqs, dev->out_ep, n, gfp_flags);
1931 DEBUG (dev, "can't alloc requests\n");
1935 static void rx_fill (struct eth_dev *dev, int gfp_flags)
1937 struct usb_request *req;
1938 unsigned long flags;
1940 clear_bit (WORK_RX_MEMORY, &dev->todo);
1942 /* fill unused rxq slots with some skb */
1943 spin_lock_irqsave (&dev->lock, flags);
1944 while (!list_empty (&dev->rx_reqs)) {
1945 req = container_of (dev->rx_reqs.next,
1946 struct usb_request, list);
1947 list_del_init (&req->list);
1948 spin_unlock_irqrestore (&dev->lock, flags);
1950 if (rx_submit (dev, req, gfp_flags) < 0) {
1951 defer_kevent (dev, WORK_RX_MEMORY);
1955 spin_lock_irqsave (&dev->lock, flags);
1957 spin_unlock_irqrestore (&dev->lock, flags);
1960 static void eth_work (void *_dev)
1962 struct eth_dev *dev = _dev;
1964 if (test_bit (WORK_RX_MEMORY, &dev->todo)) {
1965 if (netif_running (dev->net))
1966 rx_fill (dev, GFP_KERNEL);
1968 clear_bit (WORK_RX_MEMORY, &dev->todo);
1972 DEBUG (dev, "work done, flags = 0x%lx\n", dev->todo);
1975 static void tx_complete (struct usb_ep *ep, struct usb_request *req)
1977 struct sk_buff *skb = req->context;
1978 struct eth_dev *dev = ep->driver_data;
1980 switch (req->status) {
1982 dev->stats.tx_errors++;
1983 VDEBUG (dev, "tx err %d\n", req->status);
1985 case -ECONNRESET: // unlink
1986 case -ESHUTDOWN: // disconnect etc
1989 dev->stats.tx_bytes += skb->len;
1991 dev->stats.tx_packets++;
1993 spin_lock (&dev->lock);
1994 list_add (&req->list, &dev->tx_reqs);
1995 spin_unlock (&dev->lock);
1996 dev_kfree_skb_any (skb);
1998 atomic_dec (&dev->tx_qlen);
1999 if (netif_carrier_ok (dev->net))
2000 netif_wake_queue (dev->net);
2003 static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
2005 struct eth_dev *dev = netdev_priv(net);
2006 int length = skb->len;
2008 struct usb_request *req = NULL;
2009 unsigned long flags;
2011 /* FIXME check dev->cdc_filter to decide whether to send this,
2012 * instead of acting as if CDC_PACKET_TYPE_PROMISCUOUS were
2013 * always set. RNDIS has the same kind of outgoing filter.
2016 spin_lock_irqsave (&dev->lock, flags);
2017 req = container_of (dev->tx_reqs.next, struct usb_request, list);
2018 list_del (&req->list);
2019 if (list_empty (&dev->tx_reqs))
2020 netif_stop_queue (net);
2021 spin_unlock_irqrestore (&dev->lock, flags);
2023 /* no buffer copies needed, unless the network stack did it
2024 * or the hardware can't use skb buffers.
2025 * or there's not enough space for any RNDIS headers we need
2027 #ifdef CONFIG_USB_ETH_RNDIS
2029 struct sk_buff *skb_rndis;
2031 skb_rndis = skb_realloc_headroom (skb,
2032 sizeof (struct rndis_packet_msg_type));
2036 dev_kfree_skb_any (skb);
2038 rndis_add_hdr (skb);
2042 req->buf = skb->data;
2044 req->complete = tx_complete;
2046 /* use zlp framing on tx for strict CDC-Ether conformance,
2047 * though any robust network rx path ignores extra padding.
2048 * and some hardware doesn't like to write zlps.
2051 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2054 req->length = length;
2056 #ifdef CONFIG_USB_GADGET_DUALSPEED
2057 /* throttle highspeed IRQ rate back slightly */
2058 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2059 ? ((atomic_read (&dev->tx_qlen) % TX_DELAY) != 0)
2063 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
2066 DEBUG (dev, "tx queue err %d\n", retval);
2069 net->trans_start = jiffies;
2070 atomic_inc (&dev->tx_qlen);
2074 #ifdef CONFIG_USB_ETH_RNDIS
2077 dev->stats.tx_dropped++;
2078 dev_kfree_skb_any (skb);
2079 spin_lock_irqsave (&dev->lock, flags);
2080 if (list_empty (&dev->tx_reqs))
2081 netif_start_queue (net);
2082 list_add (&req->list, &dev->tx_reqs);
2083 spin_unlock_irqrestore (&dev->lock, flags);
2088 /*-------------------------------------------------------------------------*/
2090 #ifdef CONFIG_USB_ETH_RNDIS
2092 static void rndis_send_media_state (struct eth_dev *dev, int connect)
2098 if (rndis_signal_connect (dev->rndis_config))
2101 if (rndis_signal_disconnect (dev->rndis_config))
2107 rndis_control_ack_complete (struct usb_ep *ep, struct usb_request *req)
2109 if (req->status || req->actual != req->length)
2110 DEBUG ((struct eth_dev *) ep->driver_data,
2111 "rndis control ack complete --> %d, %d/%d\n",
2112 req->status, req->actual, req->length);
2114 usb_ep_free_buffer(ep, req->buf, req->dma, 8);
2115 usb_ep_free_request(ep, req);
2118 static int rndis_control_ack (struct net_device *net)
2120 struct eth_dev *dev = netdev_priv(net);
2122 struct usb_request *resp;
2124 /* in case RNDIS calls this after disconnect */
2125 if (!dev->status_ep) {
2126 DEBUG (dev, "status ENODEV\n");
2130 /* Allocate memory for notification ie. ACK */
2131 resp = usb_ep_alloc_request (dev->status_ep, GFP_ATOMIC);
2133 DEBUG (dev, "status ENOMEM\n");
2137 resp->buf = usb_ep_alloc_buffer (dev->status_ep, 8,
2138 &resp->dma, GFP_ATOMIC);
2140 DEBUG (dev, "status buf ENOMEM\n");
2141 usb_ep_free_request (dev->status_ep, resp);
2145 /* Send RNDIS RESPONSE_AVAILABLE notification;
2146 * CDC_NOTIFY_RESPONSE_AVAILABLE should work too
2149 resp->complete = rndis_control_ack_complete;
2151 *((u32 *) resp->buf) = __constant_cpu_to_le32 (1);
2152 *((u32 *) resp->buf + 1) = __constant_cpu_to_le32 (0);
2154 length = usb_ep_queue (dev->status_ep, resp, GFP_ATOMIC);
2157 rndis_control_ack_complete (dev->status_ep, resp);
2165 static void eth_start (struct eth_dev *dev, int gfp_flags)
2167 DEBUG (dev, "%s\n", __FUNCTION__);
2169 /* fill the rx queue */
2170 rx_fill (dev, gfp_flags);
2172 /* and open the tx floodgates */
2173 atomic_set (&dev->tx_qlen, 0);
2174 netif_wake_queue (dev->net);
2175 #ifdef CONFIG_USB_ETH_RNDIS
2177 rndis_set_param_medium (dev->rndis_config,
2179 BITRATE(dev->gadget));
2180 rndis_send_media_state (dev, 1);
2185 static int eth_open (struct net_device *net)
2187 struct eth_dev *dev = netdev_priv(net);
2189 DEBUG (dev, "%s\n", __FUNCTION__);
2190 if (netif_carrier_ok (dev->net))
2191 eth_start (dev, GFP_KERNEL);
2195 static int eth_stop (struct net_device *net)
2197 struct eth_dev *dev = netdev_priv(net);
2199 VDEBUG (dev, "%s\n", __FUNCTION__);
2200 netif_stop_queue (net);
2202 DEBUG (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
2203 dev->stats.rx_packets, dev->stats.tx_packets,
2204 dev->stats.rx_errors, dev->stats.tx_errors
2207 /* ensure there are no more active requests */
2209 usb_ep_disable (dev->in_ep);
2210 usb_ep_disable (dev->out_ep);
2211 if (netif_carrier_ok (dev->net)) {
2212 DEBUG (dev, "host still using in/out endpoints\n");
2213 // FIXME idiom may leave toggle wrong here
2214 usb_ep_enable (dev->in_ep, dev->in);
2215 usb_ep_enable (dev->out_ep, dev->out);
2217 if (dev->status_ep) {
2218 usb_ep_disable (dev->status_ep);
2219 usb_ep_enable (dev->status_ep, dev->status);
2223 #ifdef CONFIG_USB_ETH_RNDIS
2225 rndis_set_param_medium (dev->rndis_config,
2226 NDIS_MEDIUM_802_3, 0);
2227 rndis_send_media_state (dev, 0);
2234 /*-------------------------------------------------------------------------*/
2237 eth_unbind (struct usb_gadget *gadget)
2239 struct eth_dev *dev = get_gadget_data (gadget);
2241 DEBUG (dev, "unbind\n");
2242 #ifdef CONFIG_USB_ETH_RNDIS
2243 rndis_deregister (dev->rndis_config);
2247 /* we've already been disconnected ... no i/o is active */
2249 usb_ep_free_buffer (gadget->ep0,
2250 dev->req->buf, dev->req->dma,
2252 usb_ep_free_request (gadget->ep0, dev->req);
2256 unregister_netdev (dev->net);
2257 free_netdev(dev->net);
2259 /* assuming we used keventd, it must quiesce too */
2260 flush_scheduled_work ();
2261 set_gadget_data (gadget, NULL);
2264 static u8 __init nibble (unsigned char c)
2266 if (likely (isdigit (c)))
2269 if (likely (isxdigit (c)))
2270 return 10 + c - 'A';
2274 static void __init get_ether_addr (const char *str, u8 *dev_addr)
2279 for (i = 0; i < 6; i++) {
2282 if((*str == '.') || (*str == ':'))
2284 num = nibble(*str++) << 4;
2285 num |= (nibble(*str++));
2288 if (is_valid_ether_addr (dev_addr))
2291 random_ether_addr(dev_addr);
2295 eth_bind (struct usb_gadget *gadget)
2297 struct eth_dev *dev;
2298 struct net_device *net;
2299 u8 cdc = 1, zlp = 1, rndis = 1;
2301 int status = -ENOMEM;
2303 /* these flags are only ever cleared; compiler take note */
2304 #ifndef DEV_CONFIG_CDC
2307 #ifndef CONFIG_USB_ETH_RNDIS
2311 /* Because most host side USB stacks handle CDC Ethernet, that
2312 * standard protocol is _strongly_ preferred for interop purposes.
2313 * (By everyone except Microsoft.)
2315 if (gadget_is_net2280 (gadget)) {
2316 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0201);
2317 } else if (gadget_is_dummy (gadget)) {
2318 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0202);
2319 } else if (gadget_is_pxa (gadget)) {
2320 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0203);
2321 /* pxa doesn't support altsettings */
2323 } else if (gadget_is_sh(gadget)) {
2324 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0204);
2325 /* sh doesn't support multiple interfaces or configs */
2328 } else if (gadget_is_sa1100 (gadget)) {
2329 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0205);
2330 /* hardware can't write zlps */
2332 /* sa1100 CAN do CDC, without status endpoint ... we use
2333 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2336 } else if (gadget_is_goku (gadget)) {
2337 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0206);
2338 } else if (gadget_is_mq11xx (gadget)) {
2339 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0207);
2340 } else if (gadget_is_omap (gadget)) {
2341 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0208);
2342 } else if (gadget_is_lh7a40x(gadget)) {
2343 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0209);
2344 } else if (gadget_is_n9604(gadget)) {
2345 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0210);
2346 } else if (gadget_is_pxa27x(gadget)) {
2347 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0211);
2349 /* can't assume CDC works. don't want to default to
2350 * anything less functional on CDC-capable hardware,
2351 * so we fail in this case.
2353 printk (KERN_ERR "%s: "
2354 "controller '%s' not recognized\n",
2355 shortname, gadget->name);
2358 snprintf (manufacturer, sizeof manufacturer,
2359 UTS_SYSNAME " " UTS_RELEASE "/%s",
2362 /* If there's an RNDIS configuration, that's what Windows wants to
2363 * be using ... so use these product IDs here and in the "linux.inf"
2364 * needed to install MSFT drivers. Current Linux kernels will use
2365 * the second configuration if it's CDC Ethernet, and need some help
2366 * to choose the right configuration otherwise.
2369 device_desc.idVendor =
2370 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2371 device_desc.idProduct =
2372 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2373 snprintf (product_desc, sizeof product_desc,
2374 "RNDIS/%s", driver_desc);
2376 /* CDC subset ... recognized by Linux since 2.4.10, but Windows
2377 * drivers aren't widely available.
2380 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2381 device_desc.idVendor =
2382 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2383 device_desc.idProduct =
2384 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2388 /* support optional vendor/distro customization */
2391 printk (KERN_ERR "%s: idVendor needs idProduct!\n",
2395 device_desc.idVendor = cpu_to_le16(idVendor);
2396 device_desc.idProduct = cpu_to_le16(idProduct);
2398 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2401 strncpy (manufacturer, iManufacturer, sizeof manufacturer);
2403 strncpy (product_desc, iProduct, sizeof product_desc);
2405 /* all we really need is bulk IN/OUT */
2406 usb_ep_autoconfig_reset (gadget);
2407 ep = usb_ep_autoconfig (gadget, &fs_source_desc);
2410 printk (KERN_ERR "%s: can't autoconfigure on %s\n",
2411 shortname, gadget->name);
2414 EP_IN_NAME = ep->name;
2415 ep->driver_data = ep; /* claim */
2417 ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
2420 EP_OUT_NAME = ep->name;
2421 ep->driver_data = ep; /* claim */
2423 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2424 /* CDC Ethernet control interface doesn't require a status endpoint.
2425 * Since some hosts expect one, try to allocate one anyway.
2428 ep = usb_ep_autoconfig (gadget, &fs_status_desc);
2430 EP_STATUS_NAME = ep->name;
2431 ep->driver_data = ep; /* claim */
2433 printk (KERN_ERR "%s: "
2434 "can't run RNDIS on %s\n",
2435 shortname, gadget->name);
2438 control_intf.bNumEndpoints = 0;
2439 /* FIXME remove endpoint from descriptor list */
2444 /* one config: cdc, else minimal subset */
2446 eth_config.bNumInterfaces = 1;
2447 eth_config.iConfiguration = STRING_SUBSET;
2448 fs_subset_descriptors();
2449 hs_subset_descriptors();
2452 /* For now RNDIS is always a second config */
2454 device_desc.bNumConfigurations = 2;
2456 #ifdef CONFIG_USB_GADGET_DUALSPEED
2458 dev_qualifier.bNumConfigurations = 2;
2460 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2462 /* assumes ep0 uses the same value for both speeds ... */
2463 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2465 /* and that all endpoints are dual-speed */
2466 hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
2467 hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
2468 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2470 hs_status_desc.bEndpointAddress =
2471 fs_status_desc.bEndpointAddress;
2473 #endif /* DUALSPEED */
2475 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
2476 usb_gadget_set_selfpowered (gadget);
2478 if (gadget->is_otg) {
2479 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2480 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2481 eth_config.bMaxPower = 4;
2482 #ifdef CONFIG_USB_ETH_RNDIS
2483 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2484 rndis_config.bMaxPower = 4;
2488 net = alloc_etherdev (sizeof *dev);
2491 dev = netdev_priv(net);
2492 spin_lock_init (&dev->lock);
2493 INIT_WORK (&dev->work, eth_work, dev);
2494 INIT_LIST_HEAD (&dev->tx_reqs);
2495 INIT_LIST_HEAD (&dev->rx_reqs);
2497 /* network device setup */
2499 SET_MODULE_OWNER (net);
2500 strcpy (net->name, "usb%d");
2504 /* Module params for these addresses should come from ID proms.
2505 * The host side address is used with CDC and RNDIS, and commonly
2506 * ends up in a persistent config database.
2508 get_ether_addr(dev_addr, net->dev_addr);
2510 get_ether_addr(host_addr, dev->host_mac);
2511 #ifdef DEV_CONFIG_CDC
2512 snprintf (ethaddr, sizeof ethaddr, "%02X%02X%02X%02X%02X%02X",
2513 dev->host_mac [0], dev->host_mac [1],
2514 dev->host_mac [2], dev->host_mac [3],
2515 dev->host_mac [4], dev->host_mac [5]);
2520 status = rndis_init();
2522 printk (KERN_ERR "%s: can't init RNDIS, %d\n",
2528 net->change_mtu = eth_change_mtu;
2529 net->get_stats = eth_get_stats;
2530 net->hard_start_xmit = eth_start_xmit;
2531 net->open = eth_open;
2532 net->stop = eth_stop;
2533 // watchdog_timeo, tx_timeout ...
2534 // set_multicast_list
2535 SET_ETHTOOL_OPS(net, &ops);
2537 /* preallocate control response and buffer */
2538 dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
2541 dev->req->complete = eth_setup_complete;
2542 dev->req->buf = usb_ep_alloc_buffer (gadget->ep0, USB_BUFSIZ,
2543 &dev->req->dma, GFP_KERNEL);
2544 if (!dev->req->buf) {
2545 usb_ep_free_request (gadget->ep0, dev->req);
2549 /* finish hookup to lower layer ... */
2550 dev->gadget = gadget;
2551 set_gadget_data (gadget, dev);
2552 gadget->ep0->driver_data = dev;
2554 /* two kinds of host-initiated state changes:
2555 * - iff DATA transfer is active, carrier is "on"
2556 * - tx queueing enabled if open *and* carrier is "on"
2558 netif_stop_queue (dev->net);
2559 netif_carrier_off (dev->net);
2561 // SET_NETDEV_DEV (dev->net, &gadget->dev);
2562 status = register_netdev (dev->net);
2566 INFO (dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
2567 INFO (dev, "using %s, OUT %s IN %s%s%s\n", gadget->name,
2568 EP_OUT_NAME, EP_IN_NAME,
2569 EP_STATUS_NAME ? " STATUS " : "",
2570 EP_STATUS_NAME ? EP_STATUS_NAME : ""
2572 INFO (dev, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2573 net->dev_addr [0], net->dev_addr [1],
2574 net->dev_addr [2], net->dev_addr [3],
2575 net->dev_addr [4], net->dev_addr [5]);
2578 INFO (dev, "HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2579 dev->host_mac [0], dev->host_mac [1],
2580 dev->host_mac [2], dev->host_mac [3],
2581 dev->host_mac [4], dev->host_mac [5]);
2583 #ifdef CONFIG_USB_ETH_RNDIS
2587 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2589 dev->rndis_config = rndis_register (rndis_control_ack);
2590 if (dev->rndis_config < 0) {
2592 unregister_netdev (dev->net);
2597 /* these set up a lot of the OIDs that RNDIS needs */
2598 rndis_set_host_mac (dev->rndis_config, dev->host_mac);
2599 if (rndis_set_param_dev (dev->rndis_config, dev->net,
2602 if (rndis_set_param_vendor (dev->rndis_config, vendorID,
2605 if (rndis_set_param_medium (dev->rndis_config,
2609 INFO (dev, "RNDIS ready\n");
2616 DEBUG (dev, "register_netdev failed, %d\n", status);
2618 eth_unbind (gadget);
2622 /*-------------------------------------------------------------------------*/
2625 eth_suspend (struct usb_gadget *gadget)
2627 struct eth_dev *dev = get_gadget_data (gadget);
2629 DEBUG (dev, "suspend\n");
2634 eth_resume (struct usb_gadget *gadget)
2636 struct eth_dev *dev = get_gadget_data (gadget);
2638 DEBUG (dev, "resume\n");
2642 /*-------------------------------------------------------------------------*/
2644 static struct usb_gadget_driver eth_driver = {
2645 #ifdef CONFIG_USB_GADGET_DUALSPEED
2646 .speed = USB_SPEED_HIGH,
2648 .speed = USB_SPEED_FULL,
2650 .function = (char *) driver_desc,
2652 .unbind = eth_unbind,
2655 .disconnect = eth_disconnect,
2657 .suspend = eth_suspend,
2658 .resume = eth_resume,
2661 .name = (char *) shortname,
2668 MODULE_DESCRIPTION (DRIVER_DESC);
2669 MODULE_AUTHOR ("David Brownell, Benedikt Spanger");
2670 MODULE_LICENSE ("GPL");
2673 static int __init init (void)
2675 return usb_gadget_register_driver (ð_driver);
2679 static void __exit cleanup (void)
2681 usb_gadget_unregister_driver (ð_driver);
2683 module_exit (cleanup);