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/utsname.h>
40 #include <linux/device.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)
100 /*-------------------------------------------------------------------------*/
104 struct usb_gadget *gadget;
105 struct usb_request *req; /* for control responses */
108 struct usb_ep *in_ep, *out_ep, *status_ep;
109 const struct usb_endpoint_descriptor
111 struct list_head tx_reqs, rx_reqs;
113 struct net_device *net;
114 struct net_device_stats stats;
117 struct work_struct work;
121 unsigned suspended:1;
124 #define WORK_RX_MEMORY 0
126 u8 host_mac [ETH_ALEN];
129 /* This version autoconfigures as much as possible at run-time.
131 * It also ASSUMES a self-powered device, without remote wakeup,
132 * although remote wakeup support would make sense.
134 static const char *EP_IN_NAME;
135 static const char *EP_OUT_NAME;
136 static const char *EP_STATUS_NAME;
138 /*-------------------------------------------------------------------------*/
140 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
141 * Instead: allocate your own, using normal USB-IF procedures.
144 /* Thanks to NetChip Technologies for donating this product ID.
145 * It's for devices with only CDC Ethernet configurations.
147 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
148 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
150 /* For hardware that can't talk CDC, we use the same vendor ID that
151 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
152 * with pxa250. We're protocol-compatible, if the host-side drivers
153 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
154 * drivers that need to hard-wire endpoint numbers have a hook.
156 * The protocol is a minimal subset of CDC Ether, which works on any bulk
157 * hardware that's not deeply broken ... even on hardware that can't talk
158 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
159 * doesn't handle control-OUT).
161 #define SIMPLE_VENDOR_NUM 0x049f
162 #define SIMPLE_PRODUCT_NUM 0x505a
164 /* For hardware that can talk RNDIS and either of the above protocols,
165 * use this ID ... the windows INF files will know it. Unless it's
166 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
167 * the non-RNDIS configuration.
169 #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
170 #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
173 /* Some systems will want different product identifers published in the
174 * device descriptor, either numbers or strings or both. These string
175 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
178 static ushort __initdata idVendor;
179 module_param(idVendor, ushort, S_IRUGO);
180 MODULE_PARM_DESC(idVendor, "USB Vendor ID");
182 static ushort __initdata idProduct;
183 module_param(idProduct, ushort, S_IRUGO);
184 MODULE_PARM_DESC(idProduct, "USB Product ID");
186 static ushort __initdata bcdDevice;
187 module_param(bcdDevice, ushort, S_IRUGO);
188 MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
190 static char *__initdata iManufacturer;
191 module_param(iManufacturer, charp, S_IRUGO);
192 MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
194 static char *__initdata iProduct;
195 module_param(iProduct, charp, S_IRUGO);
196 MODULE_PARM_DESC(iProduct, "USB Product string");
198 /* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */
199 static char *__initdata dev_addr;
200 module_param(dev_addr, charp, S_IRUGO);
201 MODULE_PARM_DESC(iProduct, "Device Ethernet Address");
203 /* this address is invisible to ifconfig */
204 static char *__initdata host_addr;
205 module_param(host_addr, charp, S_IRUGO);
206 MODULE_PARM_DESC(host_addr, "Host Ethernet Address");
209 /*-------------------------------------------------------------------------*/
211 /* Include CDC support if we could run on CDC-capable hardware. */
213 #ifdef CONFIG_USB_GADGET_NET2280
214 #define DEV_CONFIG_CDC
217 #ifdef CONFIG_USB_GADGET_DUMMY_HCD
218 #define DEV_CONFIG_CDC
221 #ifdef CONFIG_USB_GADGET_GOKU
222 #define DEV_CONFIG_CDC
225 #ifdef CONFIG_USB_GADGET_LH7A40X
226 #define DEV_CONFIG_CDC
229 #ifdef CONFIG_USB_GADGET_MQ11XX
230 #define DEV_CONFIG_CDC
233 #ifdef CONFIG_USB_GADGET_OMAP
234 #define DEV_CONFIG_CDC
237 #ifdef CONFIG_USB_GADGET_N9604
238 #define DEV_CONFIG_CDC
241 #ifdef CONFIG_USB_GADGET_PXA27X
242 #define DEV_CONFIG_CDC
246 /* For CDC-incapable hardware, choose the simple cdc subset.
247 * Anything that talks bulk (without notable bugs) can do this.
249 #ifdef CONFIG_USB_GADGET_PXA2XX
250 #define DEV_CONFIG_SUBSET
253 #ifdef CONFIG_USB_GADGET_SH
254 #define DEV_CONFIG_SUBSET
257 #ifdef CONFIG_USB_GADGET_SA1100
258 /* use non-CDC for backwards compatibility */
259 #define DEV_CONFIG_SUBSET
263 /*-------------------------------------------------------------------------*/
265 #define DEFAULT_QLEN 2 /* double buffering by default */
267 #ifdef CONFIG_USB_GADGET_DUALSPEED
269 static unsigned qmult = 5;
270 module_param (qmult, uint, S_IRUGO|S_IWUSR);
273 /* for dual-speed hardware, use deeper queues at highspeed */
274 #define qlen(gadget) \
275 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
277 /* also defer IRQs on highspeed TX */
278 #define TX_DELAY qmult
280 #define BITRATE(g) ((g->speed == USB_SPEED_HIGH) ? 4800000 : 120000)
282 #else /* full speed (low speed doesn't do bulk) */
283 #define qlen(gadget) DEFAULT_QLEN
285 #define BITRATE(g) (12000)
289 /*-------------------------------------------------------------------------*/
291 #define xprintk(d,level,fmt,args...) \
292 printk(level "%s: " fmt , (d)->net->name , ## args)
296 #define DEBUG(dev,fmt,args...) \
297 xprintk(dev , KERN_DEBUG , fmt , ## args)
299 #define DEBUG(dev,fmt,args...) \
306 #define VDEBUG(dev,fmt,args...) \
310 #define ERROR(dev,fmt,args...) \
311 xprintk(dev , KERN_ERR , fmt , ## args)
312 #define WARN(dev,fmt,args...) \
313 xprintk(dev , KERN_WARNING , fmt , ## args)
314 #define INFO(dev,fmt,args...) \
315 xprintk(dev , KERN_INFO , fmt , ## args)
317 /*-------------------------------------------------------------------------*/
319 /* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
320 * ep0 implementation: descriptors, config management, setup().
321 * also optional class-specific notification interrupt transfer.
325 * DESCRIPTORS ... most are static, but strings and (full) configuration
326 * descriptors are built on demand. For now we do either full CDC, or
327 * our simple subset, with RNDIS as an optional second configuration.
329 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
330 * the class descriptors match a modem (they're ignored; it's really just
331 * Ethernet functionality), they don't need the NOP altsetting, and the
332 * status transfer endpoint isn't optional.
335 #define STRING_MANUFACTURER 1
336 #define STRING_PRODUCT 2
337 #define STRING_ETHADDR 3
338 #define STRING_DATA 4
339 #define STRING_CONTROL 5
340 #define STRING_RNDIS_CONTROL 6
342 #define STRING_SUBSET 8
343 #define STRING_RNDIS 9
345 #define USB_BUFSIZ 256 /* holds our biggest descriptor */
348 * This device advertises one configuration, eth_config, unless RNDIS
349 * is enabled (rndis_config) on hardware supporting at least two configs.
351 * NOTE: Controllers like superh_udc should probably be able to use
352 * an RNDIS-only configuration.
354 * FIXME define some higher-powered configurations to make it easier
355 * to recharge batteries ...
358 #define DEV_CONFIG_VALUE 1 /* cdc or subset */
359 #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
361 static struct usb_device_descriptor
363 .bLength = sizeof device_desc,
364 .bDescriptorType = USB_DT_DEVICE,
366 .bcdUSB = __constant_cpu_to_le16 (0x0200),
368 .bDeviceClass = USB_CLASS_COMM,
369 .bDeviceSubClass = 0,
370 .bDeviceProtocol = 0,
372 .idVendor = __constant_cpu_to_le16 (CDC_VENDOR_NUM),
373 .idProduct = __constant_cpu_to_le16 (CDC_PRODUCT_NUM),
374 .iManufacturer = STRING_MANUFACTURER,
375 .iProduct = STRING_PRODUCT,
376 .bNumConfigurations = 1,
379 static struct usb_otg_descriptor
381 .bLength = sizeof otg_descriptor,
382 .bDescriptorType = USB_DT_OTG,
384 .bmAttributes = USB_OTG_SRP,
387 static struct usb_config_descriptor
389 .bLength = sizeof eth_config,
390 .bDescriptorType = USB_DT_CONFIG,
392 /* compute wTotalLength on the fly */
394 .bConfigurationValue = DEV_CONFIG_VALUE,
395 .iConfiguration = STRING_CDC,
396 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
400 #ifdef CONFIG_USB_ETH_RNDIS
401 static struct usb_config_descriptor
403 .bLength = sizeof rndis_config,
404 .bDescriptorType = USB_DT_CONFIG,
406 /* compute wTotalLength on the fly */
408 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
409 .iConfiguration = STRING_RNDIS,
410 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
416 * Compared to the simple CDC subset, the full CDC Ethernet model adds
417 * three class descriptors, two interface descriptors, optional status
418 * endpoint. Both have a "data" interface and two bulk endpoints.
419 * There are also differences in how control requests are handled.
421 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of
422 * the CDC-ACM (modem) spec.
425 #ifdef DEV_CONFIG_CDC
426 static struct usb_interface_descriptor
428 .bLength = sizeof control_intf,
429 .bDescriptorType = USB_DT_INTERFACE,
431 .bInterfaceNumber = 0,
432 /* status endpoint is optional; this may be patched later */
434 .bInterfaceClass = USB_CLASS_COMM,
435 .bInterfaceSubClass = 6, /* ethernet control model */
436 .bInterfaceProtocol = 0,
437 .iInterface = STRING_CONTROL,
441 #ifdef CONFIG_USB_ETH_RNDIS
442 static const struct usb_interface_descriptor
443 rndis_control_intf = {
444 .bLength = sizeof rndis_control_intf,
445 .bDescriptorType = USB_DT_INTERFACE,
447 .bInterfaceNumber = 0,
449 .bInterfaceClass = USB_CLASS_COMM,
450 .bInterfaceSubClass = 2, /* abstract control model */
451 .bInterfaceProtocol = 0xff, /* vendor specific */
452 .iInterface = STRING_RNDIS_CONTROL,
456 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
458 /* "Header Functional Descriptor" from CDC spec 5.2.3.1 */
462 u8 bDescriptorSubType;
465 } __attribute__ ((packed));
467 static const struct header_desc header_desc = {
468 .bLength = sizeof header_desc,
469 .bDescriptorType = USB_DT_CS_INTERFACE,
470 .bDescriptorSubType = 0,
472 .bcdCDC = __constant_cpu_to_le16 (0x0110),
475 /* "Union Functional Descriptor" from CDC spec 5.2.3.8 */
479 u8 bDescriptorSubType;
481 u8 bMasterInterface0;
483 /* ... and there could be other slave interfaces */
484 } __attribute__ ((packed));
486 static const struct union_desc union_desc = {
487 .bLength = sizeof union_desc,
488 .bDescriptorType = USB_DT_CS_INTERFACE,
489 .bDescriptorSubType = 6,
491 .bMasterInterface0 = 0, /* index of control interface */
492 .bSlaveInterface0 = 1, /* index of DATA interface */
495 #endif /* CDC || RNDIS */
497 #ifdef CONFIG_USB_ETH_RNDIS
499 /* "Call Management Descriptor" from CDC spec 5.2.3.3 */
500 struct call_mgmt_descriptor {
503 u8 bDescriptorSubType;
507 } __attribute__ ((packed));
509 static const struct call_mgmt_descriptor call_mgmt_descriptor = {
510 .bLength = sizeof call_mgmt_descriptor,
511 .bDescriptorType = USB_DT_CS_INTERFACE,
512 .bDescriptorSubType = 0x01,
514 .bmCapabilities = 0x00,
515 .bDataInterface = 0x01,
519 /* "Abstract Control Management Descriptor" from CDC spec 5.2.3.4 */
520 struct acm_descriptor {
523 u8 bDescriptorSubType;
526 } __attribute__ ((packed));
528 static struct acm_descriptor acm_descriptor = {
529 .bLength = sizeof acm_descriptor,
530 .bDescriptorType = USB_DT_CS_INTERFACE,
531 .bDescriptorSubType = 0x02,
533 .bmCapabilities = 0X00,
538 #ifdef DEV_CONFIG_CDC
540 /* "Ethernet Networking Functional Descriptor" from CDC spec 5.2.3.16 */
544 u8 bDescriptorSubType;
547 u32 bmEthernetStatistics;
549 u16 wNumberMCFilters;
550 u8 bNumberPowerFilters;
551 } __attribute__ ((packed));
553 static const struct ether_desc ether_desc = {
554 .bLength = sizeof ether_desc,
555 .bDescriptorType = USB_DT_CS_INTERFACE,
556 .bDescriptorSubType = 0x0f,
558 /* this descriptor actually adds value, surprise! */
559 .iMACAddress = STRING_ETHADDR,
560 .bmEthernetStatistics = __constant_cpu_to_le32 (0), /* no statistics */
561 .wMaxSegmentSize = __constant_cpu_to_le16 (ETH_FRAME_LEN),
562 .wNumberMCFilters = __constant_cpu_to_le16 (0),
563 .bNumberPowerFilters = 0,
568 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
570 /* include the status endpoint if we can, even where it's optional.
571 * use small wMaxPacketSize, since many "interrupt" endpoints have
572 * very small fifos and it's no big deal if CDC_NOTIFY_SPEED_CHANGE
573 * takes two packets. also default to a big transfer interval, to
574 * waste less bandwidth.
576 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
577 * if they ignore the connect/disconnect notifications that real aether
578 * can provide. more advanced cdc configurations might want to support
579 * encapsulated commands (vendor-specific, using control-OUT).
581 * RNDIS requires the status endpoint, since it uses that encapsulation
582 * mechanism for its funky RPC scheme.
585 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
586 #define STATUS_BYTECOUNT 8 /* 8 byte header + data */
588 static struct usb_endpoint_descriptor
590 .bLength = USB_DT_ENDPOINT_SIZE,
591 .bDescriptorType = USB_DT_ENDPOINT,
593 .bEndpointAddress = USB_DIR_IN,
594 .bmAttributes = USB_ENDPOINT_XFER_INT,
595 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
596 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
600 #ifdef DEV_CONFIG_CDC
602 /* the default data interface has no endpoints ... */
604 static const struct usb_interface_descriptor
606 .bLength = sizeof data_nop_intf,
607 .bDescriptorType = USB_DT_INTERFACE,
609 .bInterfaceNumber = 1,
610 .bAlternateSetting = 0,
612 .bInterfaceClass = USB_CLASS_CDC_DATA,
613 .bInterfaceSubClass = 0,
614 .bInterfaceProtocol = 0,
617 /* ... but the "real" data interface has two bulk endpoints */
619 static const struct usb_interface_descriptor
621 .bLength = sizeof data_intf,
622 .bDescriptorType = USB_DT_INTERFACE,
624 .bInterfaceNumber = 1,
625 .bAlternateSetting = 1,
627 .bInterfaceClass = USB_CLASS_CDC_DATA,
628 .bInterfaceSubClass = 0,
629 .bInterfaceProtocol = 0,
630 .iInterface = STRING_DATA,
635 #ifdef CONFIG_USB_ETH_RNDIS
637 /* RNDIS doesn't activate by changing to the "real" altsetting */
639 static const struct usb_interface_descriptor
641 .bLength = sizeof rndis_data_intf,
642 .bDescriptorType = USB_DT_INTERFACE,
644 .bInterfaceNumber = 1,
645 .bAlternateSetting = 0,
647 .bInterfaceClass = USB_CLASS_CDC_DATA,
648 .bInterfaceSubClass = 0,
649 .bInterfaceProtocol = 0,
650 .iInterface = STRING_DATA,
655 #ifdef DEV_CONFIG_SUBSET
658 * "Simple" CDC-subset option is a simple vendor-neutral model that most
659 * full speed controllers can handle: one interface, two bulk endpoints.
662 static const struct usb_interface_descriptor
664 .bLength = sizeof subset_data_intf,
665 .bDescriptorType = USB_DT_INTERFACE,
667 .bInterfaceNumber = 0,
668 .bAlternateSetting = 0,
670 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
671 .bInterfaceSubClass = 0,
672 .bInterfaceProtocol = 0,
673 .iInterface = STRING_DATA,
679 static struct usb_endpoint_descriptor
681 .bLength = USB_DT_ENDPOINT_SIZE,
682 .bDescriptorType = USB_DT_ENDPOINT,
684 .bEndpointAddress = USB_DIR_IN,
685 .bmAttributes = USB_ENDPOINT_XFER_BULK,
688 static struct usb_endpoint_descriptor
690 .bLength = USB_DT_ENDPOINT_SIZE,
691 .bDescriptorType = USB_DT_ENDPOINT,
693 .bEndpointAddress = USB_DIR_OUT,
694 .bmAttributes = USB_ENDPOINT_XFER_BULK,
697 static const struct usb_descriptor_header *fs_eth_function [11] = {
698 (struct usb_descriptor_header *) &otg_descriptor,
699 #ifdef DEV_CONFIG_CDC
700 /* "cdc" mode descriptors */
701 (struct usb_descriptor_header *) &control_intf,
702 (struct usb_descriptor_header *) &header_desc,
703 (struct usb_descriptor_header *) &union_desc,
704 (struct usb_descriptor_header *) ðer_desc,
705 /* NOTE: status endpoint may need to be removed */
706 (struct usb_descriptor_header *) &fs_status_desc,
707 /* data interface, with altsetting */
708 (struct usb_descriptor_header *) &data_nop_intf,
709 (struct usb_descriptor_header *) &data_intf,
710 (struct usb_descriptor_header *) &fs_source_desc,
711 (struct usb_descriptor_header *) &fs_sink_desc,
713 #endif /* DEV_CONFIG_CDC */
716 static inline void __init fs_subset_descriptors(void)
718 #ifdef DEV_CONFIG_SUBSET
719 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
720 fs_eth_function[2] = (struct usb_descriptor_header *) &fs_source_desc;
721 fs_eth_function[3] = (struct usb_descriptor_header *) &fs_sink_desc;
722 fs_eth_function[4] = NULL;
724 fs_eth_function[1] = NULL;
728 #ifdef CONFIG_USB_ETH_RNDIS
729 static const struct usb_descriptor_header *fs_rndis_function [] = {
730 (struct usb_descriptor_header *) &otg_descriptor,
731 /* control interface matches ACM, not Ethernet */
732 (struct usb_descriptor_header *) &rndis_control_intf,
733 (struct usb_descriptor_header *) &header_desc,
734 (struct usb_descriptor_header *) &call_mgmt_descriptor,
735 (struct usb_descriptor_header *) &acm_descriptor,
736 (struct usb_descriptor_header *) &union_desc,
737 (struct usb_descriptor_header *) &fs_status_desc,
738 /* data interface has no altsetting */
739 (struct usb_descriptor_header *) &rndis_data_intf,
740 (struct usb_descriptor_header *) &fs_source_desc,
741 (struct usb_descriptor_header *) &fs_sink_desc,
746 #ifdef CONFIG_USB_GADGET_DUALSPEED
749 * usb 2.0 devices need to expose both high speed and full speed
750 * descriptors, unless they only run at full speed.
753 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
754 static struct usb_endpoint_descriptor
756 .bLength = USB_DT_ENDPOINT_SIZE,
757 .bDescriptorType = USB_DT_ENDPOINT,
759 .bmAttributes = USB_ENDPOINT_XFER_INT,
760 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
761 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
763 #endif /* DEV_CONFIG_CDC */
765 static struct usb_endpoint_descriptor
767 .bLength = USB_DT_ENDPOINT_SIZE,
768 .bDescriptorType = USB_DT_ENDPOINT,
770 .bmAttributes = USB_ENDPOINT_XFER_BULK,
771 .wMaxPacketSize = __constant_cpu_to_le16 (512),
774 static struct usb_endpoint_descriptor
776 .bLength = USB_DT_ENDPOINT_SIZE,
777 .bDescriptorType = USB_DT_ENDPOINT,
779 .bmAttributes = USB_ENDPOINT_XFER_BULK,
780 .wMaxPacketSize = __constant_cpu_to_le16 (512),
783 static struct usb_qualifier_descriptor
785 .bLength = sizeof dev_qualifier,
786 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
788 .bcdUSB = __constant_cpu_to_le16 (0x0200),
789 .bDeviceClass = USB_CLASS_COMM,
791 .bNumConfigurations = 1,
794 static const struct usb_descriptor_header *hs_eth_function [11] = {
795 (struct usb_descriptor_header *) &otg_descriptor,
796 #ifdef DEV_CONFIG_CDC
797 /* "cdc" mode descriptors */
798 (struct usb_descriptor_header *) &control_intf,
799 (struct usb_descriptor_header *) &header_desc,
800 (struct usb_descriptor_header *) &union_desc,
801 (struct usb_descriptor_header *) ðer_desc,
802 /* NOTE: status endpoint may need to be removed */
803 (struct usb_descriptor_header *) &hs_status_desc,
804 /* data interface, with altsetting */
805 (struct usb_descriptor_header *) &data_nop_intf,
806 (struct usb_descriptor_header *) &data_intf,
807 (struct usb_descriptor_header *) &hs_source_desc,
808 (struct usb_descriptor_header *) &hs_sink_desc,
810 #endif /* DEV_CONFIG_CDC */
813 static inline void __init hs_subset_descriptors(void)
815 #ifdef DEV_CONFIG_SUBSET
816 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
817 hs_eth_function[2] = (struct usb_descriptor_header *) &fs_source_desc;
818 hs_eth_function[3] = (struct usb_descriptor_header *) &fs_sink_desc;
819 hs_eth_function[4] = NULL;
821 hs_eth_function[1] = NULL;
825 #ifdef CONFIG_USB_ETH_RNDIS
826 static const struct usb_descriptor_header *hs_rndis_function [] = {
827 (struct usb_descriptor_header *) &otg_descriptor,
828 /* control interface matches ACM, not Ethernet */
829 (struct usb_descriptor_header *) &rndis_control_intf,
830 (struct usb_descriptor_header *) &header_desc,
831 (struct usb_descriptor_header *) &call_mgmt_descriptor,
832 (struct usb_descriptor_header *) &acm_descriptor,
833 (struct usb_descriptor_header *) &union_desc,
834 (struct usb_descriptor_header *) &hs_status_desc,
835 /* data interface has no altsetting */
836 (struct usb_descriptor_header *) &rndis_data_intf,
837 (struct usb_descriptor_header *) &hs_source_desc,
838 (struct usb_descriptor_header *) &hs_sink_desc,
844 /* maxpacket and other transfer characteristics vary by speed. */
845 #define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
849 /* if there's no high speed support, maxpacket doesn't change. */
850 #define ep_desc(g,hs,fs) fs
852 static inline void __init hs_subset_descriptors(void)
856 #endif /* !CONFIG_USB_GADGET_DUALSPEED */
858 /*-------------------------------------------------------------------------*/
860 /* descriptors that are built on-demand */
862 static char manufacturer [50];
863 static char product_desc [40] = DRIVER_DESC;
865 #ifdef DEV_CONFIG_CDC
866 /* address that the host will use ... usually assigned at random */
867 static char ethaddr [2 * ETH_ALEN + 1];
870 /* static strings, in UTF-8 */
871 static struct usb_string strings [] = {
872 { STRING_MANUFACTURER, manufacturer, },
873 { STRING_PRODUCT, product_desc, },
874 { STRING_DATA, "Ethernet Data", },
875 #ifdef DEV_CONFIG_CDC
876 { STRING_CDC, "CDC Ethernet", },
877 { STRING_ETHADDR, ethaddr, },
878 { STRING_CONTROL, "CDC Communications Control", },
880 #ifdef DEV_CONFIG_SUBSET
881 { STRING_SUBSET, "CDC Ethernet Subset", },
883 #ifdef CONFIG_USB_ETH_RNDIS
884 { STRING_RNDIS, "RNDIS", },
885 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
887 { } /* end of list */
890 static struct usb_gadget_strings stringtab = {
891 .language = 0x0409, /* en-us */
896 * one config, two interfaces: control, data.
897 * complications: class descriptors, and an altsetting.
900 config_buf (enum usb_device_speed speed,
902 unsigned index, int is_otg)
905 const struct usb_config_descriptor *config;
906 const struct usb_descriptor_header **function;
907 #ifdef CONFIG_USB_GADGET_DUALSPEED
908 int hs = (speed == USB_SPEED_HIGH);
910 if (type == USB_DT_OTHER_SPEED_CONFIG)
912 #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
914 #define which_fn(t) (fs_ ## t ## _function)
917 if (index >= device_desc.bNumConfigurations)
920 #ifdef CONFIG_USB_ETH_RNDIS
921 /* list the RNDIS config first, to make Microsoft's drivers
922 * happy. DOCSIS 1.0 needs this too.
924 if (device_desc.bNumConfigurations == 2 && index == 0) {
925 config = &rndis_config;
926 function = which_fn (rndis);
930 config = ð_config;
931 function = which_fn (eth);
934 /* for now, don't advertise srp-only devices */
938 len = usb_gadget_config_buf (config, buf, USB_BUFSIZ, function);
941 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
945 /*-------------------------------------------------------------------------*/
947 static void eth_start (struct eth_dev *dev, int gfp_flags);
948 static int alloc_requests (struct eth_dev *dev, unsigned n, int gfp_flags);
950 #ifdef DEV_CONFIG_CDC
951 static inline int ether_alt_ep_setup (struct eth_dev *dev, struct usb_ep *ep)
953 const struct usb_endpoint_descriptor *d;
955 /* With CDC, the host isn't allowed to use these two data
956 * endpoints in the default altsetting for the interface.
957 * so we don't activate them yet. Reset from SET_INTERFACE.
959 * Strictly speaking RNDIS should work the same: activation is
960 * a side effect of setting a packet filter. Deactivation is
961 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
964 /* one endpoint writes data back IN to the host */
965 if (strcmp (ep->name, EP_IN_NAME) == 0) {
966 d = ep_desc (dev->gadget, &hs_source_desc, &fs_source_desc);
967 ep->driver_data = dev;
971 /* one endpoint just reads OUT packets */
972 } else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
973 d = ep_desc (dev->gadget, &hs_sink_desc, &fs_sink_desc);
974 ep->driver_data = dev;
978 /* optional status/notification endpoint */
979 } else if (EP_STATUS_NAME &&
980 strcmp (ep->name, EP_STATUS_NAME) == 0) {
983 d = ep_desc (dev->gadget, &hs_status_desc, &fs_status_desc);
984 result = usb_ep_enable (ep, d);
988 ep->driver_data = dev;
996 #if defined(DEV_CONFIG_SUBSET) || defined(CONFIG_USB_ETH_RNDIS)
997 static inline int ether_ep_setup (struct eth_dev *dev, struct usb_ep *ep)
1000 const struct usb_endpoint_descriptor *d;
1002 /* CDC subset is simpler: if the device is there,
1003 * it's live with rx and tx endpoints.
1005 * Do this as a shortcut for RNDIS too.
1008 /* one endpoint writes data back IN to the host */
1009 if (strcmp (ep->name, EP_IN_NAME) == 0) {
1010 d = ep_desc (dev->gadget, &hs_source_desc, &fs_source_desc);
1011 result = usb_ep_enable (ep, d);
1015 ep->driver_data = dev;
1019 /* one endpoint just reads OUT packets */
1020 } else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
1021 d = ep_desc (dev->gadget, &hs_sink_desc, &fs_sink_desc);
1022 result = usb_ep_enable (ep, d);
1026 ep->driver_data = dev;
1036 set_ether_config (struct eth_dev *dev, int gfp_flags)
1040 struct usb_gadget *gadget = dev->gadget;
1042 gadget_for_each_ep (ep, gadget) {
1043 #ifdef DEV_CONFIG_CDC
1044 if (!dev->rndis && dev->cdc) {
1045 result = ether_alt_ep_setup (dev, ep);
1051 #ifdef CONFIG_USB_ETH_RNDIS
1052 if (dev->rndis && strcmp (ep->name, EP_STATUS_NAME) == 0) {
1053 const struct usb_endpoint_descriptor *d;
1054 d = ep_desc (gadget, &hs_status_desc, &fs_status_desc);
1055 result = usb_ep_enable (ep, d);
1057 ep->driver_data = dev;
1058 dev->status_ep = ep;
1066 #if defined(DEV_CONFIG_SUBSET) || defined(CONFIG_USB_ETH_RNDIS)
1067 result = ether_ep_setup (dev, ep);
1074 ERROR (dev, "can't enable %s, result %d\n", ep->name, result);
1077 if (!result && (!dev->in_ep || !dev->out_ep))
1081 result = alloc_requests (dev, qlen (gadget), gfp_flags);
1083 /* on error, disable any endpoints */
1085 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
1087 (void) usb_ep_disable (dev->status_ep);
1089 dev->status_ep = NULL;
1091 #if defined(DEV_CONFIG_SUBSET) || defined(CONFIG_USB_ETH_RNDIS)
1092 if (dev->rndis || !dev->cdc) {
1094 (void) usb_ep_disable (dev->in_ep);
1096 (void) usb_ep_disable (dev->out_ep);
1105 /* activate non-CDC configs right away
1106 * this isn't strictly according to the RNDIS spec
1108 #if defined(DEV_CONFIG_SUBSET) || defined(CONFIG_USB_ETH_RNDIS)
1109 if (dev->rndis || !dev->cdc) {
1110 netif_carrier_on (dev->net);
1111 if (netif_running (dev->net)) {
1112 spin_unlock (&dev->lock);
1113 eth_start (dev, GFP_ATOMIC);
1114 spin_lock (&dev->lock);
1120 DEBUG (dev, "qlen %d\n", qlen (gadget));
1122 /* caller is responsible for cleanup on error */
1126 static void eth_reset_config (struct eth_dev *dev)
1128 struct usb_request *req;
1130 if (dev->config == 0)
1133 DEBUG (dev, "%s\n", __FUNCTION__);
1135 netif_stop_queue (dev->net);
1136 netif_carrier_off (dev->net);
1138 /* disable endpoints, forcing (synchronous) completion of
1139 * pending i/o. then free the requests.
1142 usb_ep_disable (dev->in_ep);
1143 while (likely (!list_empty (&dev->tx_reqs))) {
1144 req = container_of (dev->tx_reqs.next,
1145 struct usb_request, list);
1146 list_del (&req->list);
1147 usb_ep_free_request (dev->in_ep, req);
1152 usb_ep_disable (dev->out_ep);
1153 while (likely (!list_empty (&dev->rx_reqs))) {
1154 req = container_of (dev->rx_reqs.next,
1155 struct usb_request, list);
1156 list_del (&req->list);
1157 usb_ep_free_request (dev->out_ep, req);
1162 if (dev->status_ep) {
1163 usb_ep_disable (dev->status_ep);
1164 dev->status_ep = NULL;
1169 /* change our operational config. must agree with the code
1170 * that returns config descriptors, and altsetting code.
1173 eth_set_config (struct eth_dev *dev, unsigned number, int gfp_flags)
1176 struct usb_gadget *gadget = dev->gadget;
1178 if (number == dev->config)
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);
1190 /* default: pass all packets, no multicast filtering */
1191 dev->cdc_filter = 0x000f;
1194 case DEV_CONFIG_VALUE:
1196 result = set_ether_config (dev, gfp_flags);
1198 #ifdef CONFIG_USB_ETH_RNDIS
1199 case DEV_RNDIS_CONFIG_VALUE:
1201 result = set_ether_config (dev, gfp_flags);
1213 eth_reset_config (dev);
1214 usb_gadget_vbus_draw(dev->gadget,
1215 dev->gadget->is_otg ? 8 : 100);
1220 power = 2 * eth_config.bMaxPower;
1221 usb_gadget_vbus_draw(dev->gadget, power);
1223 switch (gadget->speed) {
1224 case USB_SPEED_FULL: speed = "full"; break;
1225 #ifdef CONFIG_USB_GADGET_DUALSPEED
1226 case USB_SPEED_HIGH: speed = "high"; break;
1228 default: speed = "?"; break;
1231 dev->config = number;
1232 INFO (dev, "%s speed config #%d: %d mA, %s, using %s\n",
1233 speed, number, power, driver_desc,
1238 : "CDC Ethernet Subset"));
1243 /*-------------------------------------------------------------------------*/
1245 /* section 3.8.2 table 11 of the CDC spec lists Ethernet notifications
1246 * section 3.6.2.1 table 5 specifies ACM notifications, accepted by RNDIS
1247 * and RNDIS also defines its own bit-incompatible notifications
1249 #define CDC_NOTIFY_NETWORK_CONNECTION 0x00 /* required; 6.3.1 */
1250 #define CDC_NOTIFY_RESPONSE_AVAILABLE 0x01 /* optional; 6.3.2 */
1251 #define CDC_NOTIFY_SPEED_CHANGE 0x2a /* required; 6.3.8 */
1253 #ifdef DEV_CONFIG_CDC
1255 struct cdc_notification {
1257 u8 bNotificationType;
1262 /* SPEED_CHANGE data looks like this */
1266 static void eth_status_complete (struct usb_ep *ep, struct usb_request *req)
1268 struct cdc_notification *event = req->buf;
1269 int value = req->status;
1270 struct eth_dev *dev = ep->driver_data;
1272 /* issue the second notification if host reads the first */
1273 if (event->bNotificationType == CDC_NOTIFY_NETWORK_CONNECTION
1275 event->bmRequestType = 0xA1;
1276 event->bNotificationType = CDC_NOTIFY_SPEED_CHANGE;
1277 event->wValue = __constant_cpu_to_le16 (0);
1278 event->wIndex = __constant_cpu_to_le16 (1);
1279 event->wLength = __constant_cpu_to_le16 (8);
1281 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1282 event->data [0] = event->data [1] =
1283 (dev->gadget->speed == USB_SPEED_HIGH)
1284 ? (13 * 512 * 8 * 1000 * 8)
1285 : (19 * 64 * 1 * 1000 * 8);
1288 value = usb_ep_queue (ep, req, GFP_ATOMIC);
1289 DEBUG (dev, "send SPEED_CHANGE --> %d\n", value);
1293 DEBUG (dev, "event %02x --> %d\n",
1294 event->bNotificationType, value);
1296 /* free when done */
1297 usb_ep_free_buffer (ep, req->buf, req->dma, 16);
1298 usb_ep_free_request (ep, req);
1301 static void issue_start_status (struct eth_dev *dev)
1303 struct usb_request *req;
1304 struct cdc_notification *event;
1307 DEBUG (dev, "%s, flush old status first\n", __FUNCTION__);
1311 * FIXME ugly idiom, maybe we'd be better with just
1312 * a "cancel the whole queue" primitive since any
1313 * unlink-one primitive has way too many error modes.
1314 * here, we "know" toggle is already clear...
1316 usb_ep_disable (dev->status_ep);
1317 usb_ep_enable (dev->status_ep, dev->status);
1319 /* FIXME make these allocations static like dev->req */
1320 req = usb_ep_alloc_request (dev->status_ep, GFP_ATOMIC);
1322 DEBUG (dev, "status ENOMEM\n");
1325 req->buf = usb_ep_alloc_buffer (dev->status_ep, 16,
1326 &dev->req->dma, GFP_ATOMIC);
1327 if (req->buf == 0) {
1328 DEBUG (dev, "status buf ENOMEM\n");
1330 usb_ep_free_request (dev->status_ep, req);
1334 /* 3.8.1 says to issue first NETWORK_CONNECTION, then
1335 * a SPEED_CHANGE. could be useful in some configs.
1338 event->bmRequestType = 0xA1;
1339 event->bNotificationType = CDC_NOTIFY_NETWORK_CONNECTION;
1340 event->wValue = __constant_cpu_to_le16 (1); /* connected */
1341 event->wIndex = __constant_cpu_to_le16 (1);
1345 req->complete = eth_status_complete;
1346 value = usb_ep_queue (dev->status_ep, req, GFP_ATOMIC);
1348 DEBUG (dev, "status buf queue --> %d\n", value);
1349 usb_ep_free_buffer (dev->status_ep,
1350 req->buf, dev->req->dma, 16);
1357 /*-------------------------------------------------------------------------*/
1359 static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req)
1361 if (req->status || req->actual != req->length)
1362 DEBUG ((struct eth_dev *) ep->driver_data,
1363 "setup complete --> %d, %d/%d\n",
1364 req->status, req->actual, req->length);
1367 /* see section 3.8.2 table 10 of the CDC spec for more ethernet
1368 * requests, mostly for filters (multicast, pm) and statistics
1369 * section 3.6.2.1 table 4 has ACM requests; RNDIS requires the
1370 * encapsulated command mechanism.
1372 #define CDC_SEND_ENCAPSULATED_COMMAND 0x00 /* optional */
1373 #define CDC_GET_ENCAPSULATED_RESPONSE 0x01 /* optional */
1374 #define CDC_SET_ETHERNET_MULTICAST_FILTERS 0x40 /* optional */
1375 #define CDC_SET_ETHERNET_PM_PATTERN_FILTER 0x41 /* optional */
1376 #define CDC_GET_ETHERNET_PM_PATTERN_FILTER 0x42 /* optional */
1377 #define CDC_SET_ETHERNET_PACKET_FILTER 0x43 /* required */
1378 #define CDC_GET_ETHERNET_STATISTIC 0x44 /* optional */
1380 /* table 62; bits in cdc_filter */
1381 #define CDC_PACKET_TYPE_PROMISCUOUS (1 << 0)
1382 #define CDC_PACKET_TYPE_ALL_MULTICAST (1 << 1) /* no filter */
1383 #define CDC_PACKET_TYPE_DIRECTED (1 << 2)
1384 #define CDC_PACKET_TYPE_BROADCAST (1 << 3)
1385 #define CDC_PACKET_TYPE_MULTICAST (1 << 4) /* filtered */
1387 #ifdef CONFIG_USB_ETH_RNDIS
1389 static void rndis_response_complete (struct usb_ep *ep, struct usb_request *req)
1391 if (req->status || req->actual != req->length)
1392 DEBUG ((struct eth_dev *) ep->driver_data,
1393 "rndis response complete --> %d, %d/%d\n",
1394 req->status, req->actual, req->length);
1396 /* done sending after CDC_GET_ENCAPSULATED_RESPONSE */
1399 static void rndis_command_complete (struct usb_ep *ep, struct usb_request *req)
1401 struct eth_dev *dev = ep->driver_data;
1404 /* received RNDIS command from CDC_SEND_ENCAPSULATED_COMMAND */
1405 spin_lock(&dev->lock);
1406 status = rndis_msg_parser (dev->rndis_config, (u8 *) req->buf);
1408 ERROR(dev, "%s: rndis parse error %d\n", __FUNCTION__, status);
1409 spin_unlock(&dev->lock);
1415 * The setup() callback implements all the ep0 functionality that's not
1416 * handled lower down. CDC has a number of less-common features:
1418 * - two interfaces: control, and ethernet data
1419 * - Ethernet data interface has two altsettings: default, and active
1420 * - class-specific descriptors for the control interface
1421 * - class-specific control requests
1424 eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1426 struct eth_dev *dev = get_gadget_data (gadget);
1427 struct usb_request *req = dev->req;
1428 int value = -EOPNOTSUPP;
1430 /* descriptors just go into the pre-allocated ep0 buffer,
1431 * while config change events may enable network traffic.
1433 req->complete = eth_setup_complete;
1434 switch (ctrl->bRequest) {
1436 case USB_REQ_GET_DESCRIPTOR:
1437 if (ctrl->bRequestType != USB_DIR_IN)
1439 switch (ctrl->wValue >> 8) {
1442 value = min (ctrl->wLength, (u16) sizeof device_desc);
1443 memcpy (req->buf, &device_desc, value);
1445 #ifdef CONFIG_USB_GADGET_DUALSPEED
1446 case USB_DT_DEVICE_QUALIFIER:
1447 if (!gadget->is_dualspeed)
1449 value = min (ctrl->wLength, (u16) sizeof dev_qualifier);
1450 memcpy (req->buf, &dev_qualifier, value);
1453 case USB_DT_OTHER_SPEED_CONFIG:
1454 if (!gadget->is_dualspeed)
1457 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1459 value = config_buf (gadget->speed, req->buf,
1461 ctrl->wValue & 0xff,
1464 value = min (ctrl->wLength, (u16) value);
1468 value = usb_gadget_get_string (&stringtab,
1469 ctrl->wValue & 0xff, req->buf);
1471 value = min (ctrl->wLength, (u16) value);
1476 case USB_REQ_SET_CONFIGURATION:
1477 if (ctrl->bRequestType != 0)
1479 if (gadget->a_hnp_support)
1480 DEBUG (dev, "HNP available\n");
1481 else if (gadget->a_alt_hnp_support)
1482 DEBUG (dev, "HNP needs a different root port\n");
1483 spin_lock (&dev->lock);
1484 value = eth_set_config (dev, ctrl->wValue, GFP_ATOMIC);
1485 spin_unlock (&dev->lock);
1487 case USB_REQ_GET_CONFIGURATION:
1488 if (ctrl->bRequestType != USB_DIR_IN)
1490 *(u8 *)req->buf = dev->config;
1491 value = min (ctrl->wLength, (u16) 1);
1494 case USB_REQ_SET_INTERFACE:
1495 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1497 || ctrl->wIndex > 1)
1499 if (!dev->cdc && ctrl->wIndex != 0)
1501 spin_lock (&dev->lock);
1503 /* PXA hardware partially handles SET_INTERFACE;
1504 * we need to kluge around that interference.
1506 if (gadget_is_pxa (gadget)) {
1507 value = eth_set_config (dev, DEV_CONFIG_VALUE,
1512 #ifdef DEV_CONFIG_CDC
1513 switch (ctrl->wIndex) {
1514 case 0: /* control/master intf */
1515 if (ctrl->wValue != 0)
1517 if (dev->status_ep) {
1518 usb_ep_disable (dev->status_ep);
1519 usb_ep_enable (dev->status_ep, dev->status);
1523 case 1: /* data intf */
1524 if (ctrl->wValue > 1)
1526 usb_ep_disable (dev->in_ep);
1527 usb_ep_disable (dev->out_ep);
1529 /* CDC requires the data transfers not be done from
1530 * the default interface setting ... also, setting
1531 * the non-default interface clears filters etc.
1533 if (ctrl->wValue == 1) {
1534 usb_ep_enable (dev->in_ep, dev->in);
1535 usb_ep_enable (dev->out_ep, dev->out);
1536 netif_carrier_on (dev->net);
1538 issue_start_status (dev);
1539 if (netif_running (dev->net)) {
1540 spin_unlock (&dev->lock);
1541 eth_start (dev, GFP_ATOMIC);
1542 spin_lock (&dev->lock);
1545 netif_stop_queue (dev->net);
1546 netif_carrier_off (dev->net);
1552 /* FIXME this is wrong, as is the assumption that
1553 * all non-PXA hardware talks real CDC ...
1555 dev_warn (&gadget->dev, "set_interface ignored!\n");
1556 #endif /* DEV_CONFIG_CDC */
1559 spin_unlock (&dev->lock);
1561 case USB_REQ_GET_INTERFACE:
1562 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1564 || ctrl->wIndex > 1)
1566 if (!(dev->cdc || dev->rndis) && ctrl->wIndex != 0)
1569 /* for CDC, iff carrier is on, data interface is active. */
1570 if (dev->rndis || ctrl->wIndex != 1)
1571 *(u8 *)req->buf = 0;
1573 *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0;
1574 value = min (ctrl->wLength, (u16) 1);
1577 #ifdef DEV_CONFIG_CDC
1578 case CDC_SET_ETHERNET_PACKET_FILTER:
1579 /* see 6.2.30: no data, wIndex = interface,
1580 * wValue = packet filter bitmap
1582 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1585 || ctrl->wLength != 0
1586 || ctrl->wIndex > 1)
1588 DEBUG (dev, "NOP packet filter %04x\n", ctrl->wValue);
1589 /* NOTE: table 62 has 5 filter bits to reduce traffic,
1590 * and we "must" support multicast and promiscuous.
1591 * this NOP implements a bad filter (always promisc)
1593 dev->cdc_filter = ctrl->wValue;
1596 #endif /* DEV_CONFIG_CDC */
1598 #ifdef CONFIG_USB_ETH_RNDIS
1599 /* RNDIS uses the CDC command encapsulation mechanism to implement
1600 * an RPC scheme, with much getting/setting of attributes by OID.
1602 case CDC_SEND_ENCAPSULATED_COMMAND:
1603 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1605 || ctrl->wLength > USB_BUFSIZ
1607 || rndis_control_intf.bInterfaceNumber
1610 /* read the request, then process it */
1611 value = ctrl->wLength;
1612 req->complete = rndis_command_complete;
1613 /* later, rndis_control_ack () sends a notification */
1616 case CDC_GET_ENCAPSULATED_RESPONSE:
1617 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1618 == ctrl->bRequestType
1620 // && ctrl->wLength >= 0x0400
1622 && rndis_control_intf.bInterfaceNumber
1626 /* return the result */
1627 buf = rndis_get_next_response (dev->rndis_config,
1630 memcpy (req->buf, buf, value);
1631 req->complete = rndis_response_complete;
1632 rndis_free_response(dev->rndis_config, buf);
1634 /* else stalls ... spec says to avoid that */
1641 "unknown control req%02x.%02x v%04x i%04x l%d\n",
1642 ctrl->bRequestType, ctrl->bRequest,
1643 ctrl->wValue, ctrl->wIndex, ctrl->wLength);
1646 /* respond with data transfer before status phase? */
1648 req->length = value;
1649 req->zero = value < ctrl->wLength
1650 && (value % gadget->ep0->maxpacket) == 0;
1651 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1653 DEBUG (dev, "ep_queue --> %d\n", value);
1655 eth_setup_complete (gadget->ep0, req);
1659 /* host either stalls (value < 0) or reports success */
1664 eth_disconnect (struct usb_gadget *gadget)
1666 struct eth_dev *dev = get_gadget_data (gadget);
1667 unsigned long flags;
1669 spin_lock_irqsave (&dev->lock, flags);
1670 netif_stop_queue (dev->net);
1671 netif_carrier_off (dev->net);
1672 eth_reset_config (dev);
1673 spin_unlock_irqrestore (&dev->lock, flags);
1675 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1677 /* next we may get setup() calls to enumerate new connections;
1678 * or an unbind() during shutdown (including removing module).
1682 /*-------------------------------------------------------------------------*/
1684 /* NETWORK DRIVER HOOKUP (to the layer above this driver) */
1686 static int eth_change_mtu (struct net_device *net, int new_mtu)
1688 struct eth_dev *dev = netdev_priv(net);
1690 // FIXME if rndis, don't change while link's live
1692 if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN)
1694 /* no zero-length packet read wanted after mtu-sized packets */
1695 if (((new_mtu + sizeof (struct ethhdr)) % dev->in_ep->maxpacket) == 0)
1701 static struct net_device_stats *eth_get_stats (struct net_device *net)
1703 return &((struct eth_dev *)netdev_priv(net))->stats;
1706 static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
1708 struct eth_dev *dev = netdev_priv(net);
1709 strlcpy(p->driver, shortname, sizeof p->driver);
1710 strlcpy(p->version, DRIVER_VERSION, sizeof p->version);
1711 strlcpy(p->fw_version, dev->gadget->name, sizeof p->fw_version);
1712 strlcpy (p->bus_info, dev->gadget->dev.bus_id, sizeof p->bus_info);
1715 static u32 eth_get_link(struct net_device *net)
1717 struct eth_dev *dev = netdev_priv(net);
1718 return dev->gadget->speed != USB_SPEED_UNKNOWN;
1721 static struct ethtool_ops ops = {
1722 .get_drvinfo = eth_get_drvinfo,
1723 .get_link = eth_get_link
1726 static void defer_kevent (struct eth_dev *dev, int flag)
1728 if (test_and_set_bit (flag, &dev->todo))
1730 if (!schedule_work (&dev->work))
1731 ERROR (dev, "kevent %d may have been dropped\n", flag);
1733 DEBUG (dev, "kevent %d scheduled\n", flag);
1736 static void rx_complete (struct usb_ep *ep, struct usb_request *req);
1739 rx_submit (struct eth_dev *dev, struct usb_request *req, int gfp_flags)
1741 struct sk_buff *skb;
1742 int retval = -ENOMEM;
1745 /* Padding up to RX_EXTRA handles minor disagreements with host.
1746 * Normally we use the USB "terminate on short read" convention;
1747 * so allow up to (N*maxpacket), since that memory is normally
1748 * already allocated. Some hardware doesn't deal well with short
1749 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1750 * byte off the end (to force hardware errors on overflow).
1752 * RNDIS uses internal framing, and explicitly allows senders to
1753 * pad to end-of-packet. That's potentially nice for speed,
1754 * but means receivers can't recover synch on their own.
1756 size = (sizeof (struct ethhdr) + dev->net->mtu + RX_EXTRA);
1757 size += dev->out_ep->maxpacket - 1;
1758 #ifdef CONFIG_USB_ETH_RNDIS
1760 size += sizeof (struct rndis_packet_msg_type);
1762 size -= size % dev->out_ep->maxpacket;
1764 if ((skb = alloc_skb (size, gfp_flags)) == 0) {
1765 DEBUG (dev, "no rx skb\n");
1769 req->buf = skb->data;
1771 req->complete = rx_complete;
1774 retval = usb_ep_queue (dev->out_ep, req, gfp_flags);
1775 if (retval == -ENOMEM)
1777 defer_kevent (dev, WORK_RX_MEMORY);
1779 DEBUG (dev, "rx submit --> %d\n", retval);
1780 dev_kfree_skb_any (skb);
1781 spin_lock (&dev->lock);
1782 list_add (&req->list, &dev->rx_reqs);
1783 spin_unlock (&dev->lock);
1788 static void rx_complete (struct usb_ep *ep, struct usb_request *req)
1790 struct sk_buff *skb = req->context;
1791 struct eth_dev *dev = ep->driver_data;
1792 int status = req->status;
1796 /* normal completion */
1798 skb_put (skb, req->actual);
1799 #ifdef CONFIG_USB_ETH_RNDIS
1800 /* we know MaxPacketsPerTransfer == 1 here */
1802 rndis_rm_hdr (req->buf, &(skb->len));
1804 if (ETH_HLEN > skb->len || skb->len > ETH_FRAME_LEN) {
1805 dev->stats.rx_errors++;
1806 dev->stats.rx_length_errors++;
1807 DEBUG (dev, "rx length %d\n", skb->len);
1811 skb->dev = dev->net;
1812 skb->protocol = eth_type_trans (skb, dev->net);
1813 dev->stats.rx_packets++;
1814 dev->stats.rx_bytes += skb->len;
1816 /* no buffer copies needed, unless hardware can't
1819 status = netif_rx (skb);
1823 /* software-driven interface shutdown */
1824 case -ECONNRESET: // unlink
1825 case -ESHUTDOWN: // disconnect etc
1826 VDEBUG (dev, "rx shutdown, code %d\n", status);
1829 /* for hardware automagic (such as pxa) */
1830 case -ECONNABORTED: // endpoint reset
1831 DEBUG (dev, "rx %s reset\n", ep->name);
1832 defer_kevent (dev, WORK_RX_MEMORY);
1834 dev_kfree_skb_any (skb);
1839 dev->stats.rx_over_errors++;
1843 dev->stats.rx_errors++;
1844 DEBUG (dev, "rx status %d\n", status);
1849 dev_kfree_skb_any (skb);
1850 if (!netif_running (dev->net)) {
1852 /* nobody reading rx_reqs, so no dev->lock */
1853 list_add (&req->list, &dev->rx_reqs);
1857 rx_submit (dev, req, GFP_ATOMIC);
1860 static int prealloc (struct list_head *list, struct usb_ep *ep,
1861 unsigned n, int gfp_flags)
1864 struct usb_request *req;
1869 /* queue/recycle up to N requests */
1871 list_for_each_entry (req, list, list) {
1876 req = usb_ep_alloc_request (ep, gfp_flags);
1878 return list_empty (list) ? -ENOMEM : 0;
1879 list_add (&req->list, list);
1886 struct list_head *next;
1888 next = req->list.next;
1889 list_del (&req->list);
1890 usb_ep_free_request (ep, req);
1895 req = container_of (next, struct usb_request, list);
1900 static int alloc_requests (struct eth_dev *dev, unsigned n, int gfp_flags)
1904 status = prealloc (&dev->tx_reqs, dev->in_ep, n, gfp_flags);
1907 status = prealloc (&dev->rx_reqs, dev->out_ep, n, gfp_flags);
1912 DEBUG (dev, "can't alloc requests\n");
1916 static void rx_fill (struct eth_dev *dev, int gfp_flags)
1918 struct usb_request *req;
1919 unsigned long flags;
1921 clear_bit (WORK_RX_MEMORY, &dev->todo);
1923 /* fill unused rxq slots with some skb */
1924 spin_lock_irqsave (&dev->lock, flags);
1925 while (!list_empty (&dev->rx_reqs)) {
1926 req = container_of (dev->rx_reqs.next,
1927 struct usb_request, list);
1928 list_del_init (&req->list);
1929 spin_unlock_irqrestore (&dev->lock, flags);
1931 if (rx_submit (dev, req, gfp_flags) < 0) {
1932 defer_kevent (dev, WORK_RX_MEMORY);
1936 spin_lock_irqsave (&dev->lock, flags);
1938 spin_unlock_irqrestore (&dev->lock, flags);
1941 static void eth_work (void *_dev)
1943 struct eth_dev *dev = _dev;
1945 if (test_bit (WORK_RX_MEMORY, &dev->todo)) {
1946 if (netif_running (dev->net))
1947 rx_fill (dev, GFP_KERNEL);
1949 clear_bit (WORK_RX_MEMORY, &dev->todo);
1953 DEBUG (dev, "work done, flags = 0x%lx\n", dev->todo);
1956 static void tx_complete (struct usb_ep *ep, struct usb_request *req)
1958 struct sk_buff *skb = req->context;
1959 struct eth_dev *dev = ep->driver_data;
1961 switch (req->status) {
1963 dev->stats.tx_errors++;
1964 VDEBUG (dev, "tx err %d\n", req->status);
1966 case -ECONNRESET: // unlink
1967 case -ESHUTDOWN: // disconnect etc
1970 dev->stats.tx_bytes += skb->len;
1972 dev->stats.tx_packets++;
1974 spin_lock (&dev->lock);
1975 list_add (&req->list, &dev->tx_reqs);
1976 spin_unlock (&dev->lock);
1977 dev_kfree_skb_any (skb);
1979 atomic_dec (&dev->tx_qlen);
1980 if (netif_carrier_ok (dev->net))
1981 netif_wake_queue (dev->net);
1984 static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1986 struct eth_dev *dev = netdev_priv(net);
1987 int length = skb->len;
1989 struct usb_request *req = NULL;
1990 unsigned long flags;
1992 /* FIXME check dev->cdc_filter to decide whether to send this,
1993 * instead of acting as if CDC_PACKET_TYPE_PROMISCUOUS were
1994 * always set. RNDIS has the same kind of outgoing filter.
1997 spin_lock_irqsave (&dev->lock, flags);
1998 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1999 list_del (&req->list);
2000 if (list_empty (&dev->tx_reqs))
2001 netif_stop_queue (net);
2002 spin_unlock_irqrestore (&dev->lock, flags);
2004 /* no buffer copies needed, unless the network stack did it
2005 * or the hardware can't use skb buffers.
2006 * or there's not enough space for any RNDIS headers we need
2008 #ifdef CONFIG_USB_ETH_RNDIS
2010 struct sk_buff *skb_rndis;
2012 skb_rndis = skb_realloc_headroom (skb,
2013 sizeof (struct rndis_packet_msg_type));
2017 dev_kfree_skb_any (skb);
2019 rndis_add_hdr (skb);
2023 req->buf = skb->data;
2025 req->complete = tx_complete;
2027 /* use zlp framing on tx for strict CDC-Ether conformance,
2028 * though any robust network rx path ignores extra padding.
2029 * and some hardware doesn't like to write zlps.
2032 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2035 req->length = length;
2037 #ifdef CONFIG_USB_GADGET_DUALSPEED
2038 /* throttle highspeed IRQ rate back slightly */
2039 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2040 ? ((atomic_read (&dev->tx_qlen) % TX_DELAY) != 0)
2044 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
2047 DEBUG (dev, "tx queue err %d\n", retval);
2050 net->trans_start = jiffies;
2051 atomic_inc (&dev->tx_qlen);
2055 #ifdef CONFIG_USB_ETH_RNDIS
2058 dev->stats.tx_dropped++;
2059 dev_kfree_skb_any (skb);
2060 spin_lock_irqsave (&dev->lock, flags);
2061 if (list_empty (&dev->tx_reqs))
2062 netif_start_queue (net);
2063 list_add (&req->list, &dev->tx_reqs);
2064 spin_unlock_irqrestore (&dev->lock, flags);
2069 /*-------------------------------------------------------------------------*/
2071 #ifdef CONFIG_USB_ETH_RNDIS
2073 static void rndis_send_media_state (struct eth_dev *dev, int connect)
2079 if (rndis_signal_connect (dev->rndis_config))
2082 if (rndis_signal_disconnect (dev->rndis_config))
2088 rndis_control_ack_complete (struct usb_ep *ep, struct usb_request *req)
2090 if (req->status || req->actual != req->length)
2091 DEBUG ((struct eth_dev *) ep->driver_data,
2092 "rndis control ack complete --> %d, %d/%d\n",
2093 req->status, req->actual, req->length);
2095 usb_ep_free_buffer(ep, req->buf, req->dma, 8);
2096 usb_ep_free_request(ep, req);
2099 static int rndis_control_ack (struct net_device *net)
2101 struct eth_dev *dev = netdev_priv(net);
2103 struct usb_request *resp;
2105 /* in case RNDIS calls this after disconnect */
2106 if (!dev->status_ep) {
2107 DEBUG (dev, "status ENODEV\n");
2111 /* Allocate memory for notification ie. ACK */
2112 resp = usb_ep_alloc_request (dev->status_ep, GFP_ATOMIC);
2114 DEBUG (dev, "status ENOMEM\n");
2118 resp->buf = usb_ep_alloc_buffer (dev->status_ep, 8,
2119 &resp->dma, GFP_ATOMIC);
2121 DEBUG (dev, "status buf ENOMEM\n");
2122 usb_ep_free_request (dev->status_ep, resp);
2126 /* Send RNDIS RESPONSE_AVAILABLE notification;
2127 * CDC_NOTIFY_RESPONSE_AVAILABLE should work too
2130 resp->complete = rndis_control_ack_complete;
2132 *((u32 *) resp->buf) = __constant_cpu_to_le32 (1);
2133 *((u32 *) resp->buf + 1) = __constant_cpu_to_le32 (0);
2135 length = usb_ep_queue (dev->status_ep, resp, GFP_ATOMIC);
2138 rndis_control_ack_complete (dev->status_ep, resp);
2146 static void eth_start (struct eth_dev *dev, int gfp_flags)
2148 DEBUG (dev, "%s\n", __FUNCTION__);
2150 /* fill the rx queue */
2151 rx_fill (dev, gfp_flags);
2153 /* and open the tx floodgates */
2154 atomic_set (&dev->tx_qlen, 0);
2155 netif_wake_queue (dev->net);
2156 #ifdef CONFIG_USB_ETH_RNDIS
2158 rndis_set_param_medium (dev->rndis_config,
2160 BITRATE(dev->gadget));
2161 rndis_send_media_state (dev, 1);
2166 static int eth_open (struct net_device *net)
2168 struct eth_dev *dev = netdev_priv(net);
2170 DEBUG (dev, "%s\n", __FUNCTION__);
2171 if (netif_carrier_ok (dev->net))
2172 eth_start (dev, GFP_KERNEL);
2176 static int eth_stop (struct net_device *net)
2178 struct eth_dev *dev = netdev_priv(net);
2180 VDEBUG (dev, "%s\n", __FUNCTION__);
2181 netif_stop_queue (net);
2183 DEBUG (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
2184 dev->stats.rx_packets, dev->stats.tx_packets,
2185 dev->stats.rx_errors, dev->stats.tx_errors
2188 /* ensure there are no more active requests */
2190 usb_ep_disable (dev->in_ep);
2191 usb_ep_disable (dev->out_ep);
2192 if (netif_carrier_ok (dev->net)) {
2193 DEBUG (dev, "host still using in/out endpoints\n");
2194 // FIXME idiom may leave toggle wrong here
2195 usb_ep_enable (dev->in_ep, dev->in);
2196 usb_ep_enable (dev->out_ep, dev->out);
2198 if (dev->status_ep) {
2199 usb_ep_disable (dev->status_ep);
2200 usb_ep_enable (dev->status_ep, dev->status);
2204 #ifdef CONFIG_USB_ETH_RNDIS
2206 rndis_set_param_medium (dev->rndis_config,
2207 NDIS_MEDIUM_802_3, 0);
2208 rndis_send_media_state (dev, 0);
2215 /*-------------------------------------------------------------------------*/
2218 eth_unbind (struct usb_gadget *gadget)
2220 struct eth_dev *dev = get_gadget_data (gadget);
2222 DEBUG (dev, "unbind\n");
2223 #ifdef CONFIG_USB_ETH_RNDIS
2224 rndis_deregister (dev->rndis_config);
2228 /* we've already been disconnected ... no i/o is active */
2230 usb_ep_free_buffer (gadget->ep0,
2231 dev->req->buf, dev->req->dma,
2233 usb_ep_free_request (gadget->ep0, dev->req);
2237 unregister_netdev (dev->net);
2238 free_netdev(dev->net);
2240 /* assuming we used keventd, it must quiesce too */
2241 flush_scheduled_work ();
2242 set_gadget_data (gadget, NULL);
2245 static u8 __init nibble (unsigned char c)
2247 if (likely (isdigit (c)))
2250 if (likely (isxdigit (c)))
2251 return 10 + c - 'A';
2255 static void __init get_ether_addr (const char *str, u8 *dev_addr)
2260 for (i = 0; i < 6; i++) {
2263 if((*str == '.') || (*str == ':'))
2265 num = nibble(*str++) << 4;
2266 num |= (nibble(*str++));
2269 if (is_valid_ether_addr (dev_addr))
2272 random_ether_addr(dev_addr);
2276 eth_bind (struct usb_gadget *gadget)
2278 struct eth_dev *dev;
2279 struct net_device *net;
2280 u8 cdc = 1, zlp = 1, rndis = 1;
2282 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_net2280 (gadget)) {
2297 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0201);
2298 } else if (gadget_is_dummy (gadget)) {
2299 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0202);
2300 } else if (gadget_is_pxa (gadget)) {
2301 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0203);
2302 /* pxa doesn't support altsettings */
2304 } else if (gadget_is_sh(gadget)) {
2305 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0204);
2306 /* sh doesn't support multiple interfaces or configs */
2309 } else if (gadget_is_sa1100 (gadget)) {
2310 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0205);
2311 /* hardware can't write zlps */
2313 /* sa1100 CAN do CDC, without status endpoint ... we use
2314 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2317 } else if (gadget_is_goku (gadget)) {
2318 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0206);
2319 } else if (gadget_is_mq11xx (gadget)) {
2320 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0207);
2321 } else if (gadget_is_omap (gadget)) {
2322 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0208);
2323 } else if (gadget_is_lh7a40x(gadget)) {
2324 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0209);
2325 } else if (gadget_is_n9604(gadget)) {
2326 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0210);
2327 } else if (gadget_is_pxa27x(gadget)) {
2328 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0211);
2330 /* can't assume CDC works. don't want to default to
2331 * anything less functional on CDC-capable hardware,
2332 * so we fail in this case.
2334 dev_err (&gadget->dev,
2335 "controller '%s' not recognized\n",
2339 snprintf (manufacturer, sizeof manufacturer, "%s %s/%s",
2340 system_utsname.sysname, system_utsname.release,
2343 /* If there's an RNDIS configuration, that's what Windows wants to
2344 * be using ... so use these product IDs here and in the "linux.inf"
2345 * needed to install MSFT drivers. Current Linux kernels will use
2346 * the second configuration if it's CDC Ethernet, and need some help
2347 * to choose the right configuration otherwise.
2350 device_desc.idVendor =
2351 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2352 device_desc.idProduct =
2353 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2354 snprintf (product_desc, sizeof product_desc,
2355 "RNDIS/%s", driver_desc);
2357 /* CDC subset ... recognized by Linux since 2.4.10, but Windows
2358 * drivers aren't widely available.
2361 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2362 device_desc.idVendor =
2363 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2364 device_desc.idProduct =
2365 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2368 /* support optional vendor/distro customization */
2371 dev_err (&gadget->dev, "idVendor needs idProduct!\n");
2374 device_desc.idVendor = cpu_to_le16(idVendor);
2375 device_desc.idProduct = cpu_to_le16(idProduct);
2377 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2380 strlcpy (manufacturer, iManufacturer, sizeof manufacturer);
2382 strlcpy (product_desc, iProduct, sizeof product_desc);
2384 /* all we really need is bulk IN/OUT */
2385 usb_ep_autoconfig_reset (gadget);
2386 ep = usb_ep_autoconfig (gadget, &fs_source_desc);
2389 dev_err (&gadget->dev,
2390 "can't autoconfigure on %s\n",
2394 EP_IN_NAME = ep->name;
2395 ep->driver_data = ep; /* claim */
2397 ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
2400 EP_OUT_NAME = ep->name;
2401 ep->driver_data = ep; /* claim */
2403 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2404 /* CDC Ethernet control interface doesn't require a status endpoint.
2405 * Since some hosts expect one, try to allocate one anyway.
2408 ep = usb_ep_autoconfig (gadget, &fs_status_desc);
2410 EP_STATUS_NAME = ep->name;
2411 ep->driver_data = ep; /* claim */
2413 dev_err (&gadget->dev,
2414 "can't run RNDIS on %s\n",
2418 control_intf.bNumEndpoints = 0;
2419 /* FIXME remove endpoint from descriptor list */
2424 /* one config: cdc, else minimal subset */
2426 eth_config.bNumInterfaces = 1;
2427 eth_config.iConfiguration = STRING_SUBSET;
2428 fs_subset_descriptors();
2429 hs_subset_descriptors();
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 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
2456 usb_gadget_set_selfpowered (gadget);
2458 if (gadget->is_otg) {
2459 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2460 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2461 eth_config.bMaxPower = 4;
2462 #ifdef CONFIG_USB_ETH_RNDIS
2463 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2464 rndis_config.bMaxPower = 4;
2468 net = alloc_etherdev (sizeof *dev);
2471 dev = netdev_priv(net);
2472 spin_lock_init (&dev->lock);
2473 INIT_WORK (&dev->work, eth_work, dev);
2474 INIT_LIST_HEAD (&dev->tx_reqs);
2475 INIT_LIST_HEAD (&dev->rx_reqs);
2477 /* network device setup */
2479 SET_MODULE_OWNER (net);
2480 strcpy (net->name, "usb%d");
2484 /* Module params for these addresses should come from ID proms.
2485 * The host side address is used with CDC and RNDIS, and commonly
2486 * ends up in a persistent config database.
2488 get_ether_addr(dev_addr, net->dev_addr);
2490 get_ether_addr(host_addr, dev->host_mac);
2491 #ifdef DEV_CONFIG_CDC
2492 snprintf (ethaddr, sizeof ethaddr, "%02X%02X%02X%02X%02X%02X",
2493 dev->host_mac [0], dev->host_mac [1],
2494 dev->host_mac [2], dev->host_mac [3],
2495 dev->host_mac [4], dev->host_mac [5]);
2500 status = rndis_init();
2502 dev_err (&gadget->dev, "can't init RNDIS, %d\n",
2508 net->change_mtu = eth_change_mtu;
2509 net->get_stats = eth_get_stats;
2510 net->hard_start_xmit = eth_start_xmit;
2511 net->open = eth_open;
2512 net->stop = eth_stop;
2513 // watchdog_timeo, tx_timeout ...
2514 // set_multicast_list
2515 SET_ETHTOOL_OPS(net, &ops);
2517 /* preallocate control response and buffer */
2518 dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
2521 dev->req->complete = eth_setup_complete;
2522 dev->req->buf = usb_ep_alloc_buffer (gadget->ep0, USB_BUFSIZ,
2523 &dev->req->dma, GFP_KERNEL);
2524 if (!dev->req->buf) {
2525 usb_ep_free_request (gadget->ep0, dev->req);
2529 /* finish hookup to lower layer ... */
2530 dev->gadget = gadget;
2531 set_gadget_data (gadget, dev);
2532 gadget->ep0->driver_data = dev;
2534 /* two kinds of host-initiated state changes:
2535 * - iff DATA transfer is active, carrier is "on"
2536 * - tx queueing enabled if open *and* carrier is "on"
2538 netif_stop_queue (dev->net);
2539 netif_carrier_off (dev->net);
2541 // SET_NETDEV_DEV (dev->net, &gadget->dev);
2542 status = register_netdev (dev->net);
2546 INFO (dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
2547 INFO (dev, "using %s, OUT %s IN %s%s%s\n", gadget->name,
2548 EP_OUT_NAME, EP_IN_NAME,
2549 EP_STATUS_NAME ? " STATUS " : "",
2550 EP_STATUS_NAME ? EP_STATUS_NAME : ""
2552 INFO (dev, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2553 net->dev_addr [0], net->dev_addr [1],
2554 net->dev_addr [2], net->dev_addr [3],
2555 net->dev_addr [4], net->dev_addr [5]);
2558 INFO (dev, "HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2559 dev->host_mac [0], dev->host_mac [1],
2560 dev->host_mac [2], dev->host_mac [3],
2561 dev->host_mac [4], dev->host_mac [5]);
2563 #ifdef CONFIG_USB_ETH_RNDIS
2567 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2569 dev->rndis_config = rndis_register (rndis_control_ack);
2570 if (dev->rndis_config < 0) {
2572 unregister_netdev (dev->net);
2577 /* these set up a lot of the OIDs that RNDIS needs */
2578 rndis_set_host_mac (dev->rndis_config, dev->host_mac);
2579 if (rndis_set_param_dev (dev->rndis_config, dev->net,
2582 if (rndis_set_param_vendor (dev->rndis_config, vendorID,
2585 if (rndis_set_param_medium (dev->rndis_config,
2589 INFO (dev, "RNDIS ready\n");
2596 dev_dbg(&gadget->dev, "register_netdev failed, %d\n", status);
2598 eth_unbind (gadget);
2602 /*-------------------------------------------------------------------------*/
2605 eth_suspend (struct usb_gadget *gadget)
2607 struct eth_dev *dev = get_gadget_data (gadget);
2609 DEBUG (dev, "suspend\n");
2614 eth_resume (struct usb_gadget *gadget)
2616 struct eth_dev *dev = get_gadget_data (gadget);
2618 DEBUG (dev, "resume\n");
2622 /*-------------------------------------------------------------------------*/
2624 static struct usb_gadget_driver eth_driver = {
2625 #ifdef CONFIG_USB_GADGET_DUALSPEED
2626 .speed = USB_SPEED_HIGH,
2628 .speed = USB_SPEED_FULL,
2630 .function = (char *) driver_desc,
2632 .unbind = eth_unbind,
2635 .disconnect = eth_disconnect,
2637 .suspend = eth_suspend,
2638 .resume = eth_resume,
2641 .name = (char *) shortname,
2648 MODULE_DESCRIPTION (DRIVER_DESC);
2649 MODULE_AUTHOR ("David Brownell, Benedikt Spanger");
2650 MODULE_LICENSE ("GPL");
2653 static int __init init (void)
2655 return usb_gadget_register_driver (ð_driver);
2659 static void __exit cleanup (void)
2661 usb_gadget_unregister_driver (ð_driver);
2663 module_exit (cleanup);