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/device.h>
42 #include <linux/moduleparam.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
66 * CDC Ethernet is the standard USB solution for sending Ethernet frames
67 * using USB. Real hardware tends to use the same framing protocol but look
68 * different for control features. This driver strongly prefers to use
69 * this USB-IF standard as its open-systems interoperability solution;
70 * most host side USB stacks (except from Microsoft) support it.
72 * There's some hardware that can't talk CDC. We make that hardware
73 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
74 * link-level setup only requires activating the configuration.
75 * Linux supports it, but other host operating systems may not.
76 * (This is a subset of CDC Ethernet.)
78 * A third option is also in use. Rather than CDC Ethernet, or something
79 * simpler, Microsoft pushes their own approach: RNDIS. The published
80 * RNDIS specs are ambiguous and appear to be incomplete, and are also
84 #define DRIVER_DESC "Ethernet Gadget"
85 #define DRIVER_VERSION "St Patrick's Day 2004"
87 static const char shortname [] = "ether";
88 static const char driver_desc [] = DRIVER_DESC;
90 #define RX_EXTRA 20 /* guard against rx overflows */
92 #ifdef CONFIG_USB_ETH_RNDIS
95 #define rndis_init() 0
96 #define rndis_exit() do{}while(0)
99 /*-------------------------------------------------------------------------*/
103 struct usb_gadget *gadget;
104 struct usb_request *req; /* for control responses */
107 struct usb_ep *in_ep, *out_ep, *status_ep;
108 const struct usb_endpoint_descriptor
110 struct list_head tx_reqs, rx_reqs;
112 struct net_device *net;
113 struct net_device_stats stats;
116 struct work_struct work;
121 #define WORK_RX_MEMORY 0
123 u8 host_mac [ETH_ALEN];
126 /* This version autoconfigures as much as possible at run-time.
128 * It also ASSUMES a self-powered device, without remote wakeup,
129 * although remote wakeup support would make sense.
131 static const char *EP_IN_NAME;
132 static const char *EP_OUT_NAME;
133 static const char *EP_STATUS_NAME;
135 /*-------------------------------------------------------------------------*/
137 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
138 * Instead: allocate your own, using normal USB-IF procedures.
141 /* Thanks to NetChip Technologies for donating this product ID.
142 * It's for devices with only CDC Ethernet configurations.
144 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
145 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
147 /* For hardware that can't talk CDC, we use the same vendor ID that
148 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
149 * with pxa250. We're protocol-compatible, if the host-side drivers
150 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
151 * drivers that need to hard-wire endpoint numbers have a hook.
153 * The protocol is a minimal subset of CDC Ether, which works on any bulk
154 * hardware that's not deeply broken ... even on hardware that can't talk
155 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
156 * doesn't handle control-OUT).
158 #define SIMPLE_VENDOR_NUM 0x049f
159 #define SIMPLE_PRODUCT_NUM 0x505a
161 /* For hardware that can talk RNDIS and either of the above protocols,
162 * use this ID ... the windows INF files will know it. Unless it's
163 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
164 * the non-RNDIS configuration.
166 #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
167 #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
170 /* Some systems will want different product identifers published in the
171 * device descriptor, either numbers or strings or both. These string
172 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
175 static ushort __initdata idVendor;
176 module_param(idVendor, ushort, S_IRUGO);
177 MODULE_PARM_DESC(idVendor, "USB Vendor ID");
179 static ushort __initdata idProduct;
180 module_param(idProduct, ushort, S_IRUGO);
181 MODULE_PARM_DESC(idProduct, "USB Product ID");
183 static ushort __initdata bcdDevice;
184 module_param(bcdDevice, ushort, S_IRUGO);
185 MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
187 static char *__initdata iManufacturer;
188 module_param(iManufacturer, charp, S_IRUGO);
189 MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
191 static char *__initdata iProduct;
192 module_param(iProduct, charp, S_IRUGO);
193 MODULE_PARM_DESC(iProduct, "USB Product string");
196 /*-------------------------------------------------------------------------*/
198 /* Include CDC support if we could run on CDC-capable hardware. */
200 #ifdef CONFIG_USB_GADGET_NET2280
201 #define DEV_CONFIG_CDC
204 #ifdef CONFIG_USB_GADGET_DUMMY_HCD
205 #define DEV_CONFIG_CDC
208 #ifdef CONFIG_USB_GADGET_GOKU
209 #define DEV_CONFIG_CDC
212 #ifdef CONFIG_USB_GADGET_MQ11XX
213 #define DEV_CONFIG_CDC
216 #ifdef CONFIG_USB_GADGET_OMAP
217 #define DEV_CONFIG_CDC
221 /* For CDC-incapable hardware, choose the simple cdc subset.
222 * Anything that talks bulk (without notable bugs) can do this.
224 #ifdef CONFIG_USB_GADGET_PXA
225 #define DEV_CONFIG_SUBSET
228 #ifdef CONFIG_USB_GADGET_SH
229 #define DEV_CONFIG_SUBSET
232 #ifdef CONFIG_USB_GADGET_SA1100
233 /* use non-CDC for backwards compatibility */
234 #define DEV_CONFIG_SUBSET
238 /*-------------------------------------------------------------------------*/
240 #define DEFAULT_QLEN 2 /* double buffering by default */
242 #ifdef CONFIG_USB_GADGET_DUALSPEED
244 static unsigned qmult = 5;
245 module_param (qmult, uint, S_IRUGO|S_IWUSR);
248 /* for dual-speed hardware, use deeper queues at highspeed */
249 #define qlen(gadget) \
250 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
252 /* also defer IRQs on highspeed TX */
253 #define TX_DELAY qmult
255 #define BITRATE(g) ((g->speed == USB_SPEED_HIGH) ? 4800000 : 120000)
257 #else /* full speed (low speed doesn't do bulk) */
258 #define qlen(gadget) DEFAULT_QLEN
260 #define BITRATE(g) (12000)
264 /*-------------------------------------------------------------------------*/
266 #define xprintk(d,level,fmt,args...) \
267 printk(level "%s: " fmt , (d)->net->name , ## args)
271 #define DEBUG(dev,fmt,args...) \
272 xprintk(dev , KERN_DEBUG , fmt , ## args)
274 #define DEBUG(dev,fmt,args...) \
281 #define VDEBUG(dev,fmt,args...) \
285 #define ERROR(dev,fmt,args...) \
286 xprintk(dev , KERN_ERR , fmt , ## args)
287 #define WARN(dev,fmt,args...) \
288 xprintk(dev , KERN_WARNING , fmt , ## args)
289 #define INFO(dev,fmt,args...) \
290 xprintk(dev , KERN_INFO , fmt , ## args)
292 /*-------------------------------------------------------------------------*/
294 /* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
295 * ep0 implementation: descriptors, config management, setup().
296 * also optional class-specific notification interrupt transfer.
300 * DESCRIPTORS ... most are static, but strings and (full) configuration
301 * descriptors are built on demand. For now we do either full CDC, or
302 * our simple subset, with RNDIS as an optional second configuration.
304 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
305 * the class descriptors match a modem (they're ignored; it's really just
306 * Ethernet functionality), they don't need the NOP altsetting, and the
307 * status transfer endpoint isn't optional.
310 #define STRING_MANUFACTURER 1
311 #define STRING_PRODUCT 2
312 #define STRING_ETHADDR 3
313 #define STRING_DATA 4
314 #define STRING_CONTROL 5
315 #define STRING_RNDIS_CONTROL 6
317 #define STRING_SUBSET 8
318 #define STRING_RNDIS 9
320 #define USB_BUFSIZ 256 /* holds our biggest descriptor */
323 * This device advertises one configuration, eth_config, unless RNDIS
324 * is enabled (rndis_config) on hardware supporting at least two configs.
326 * NOTE: Controllers like superh_udc should probably be able to use
327 * an RNDIS-only configuration.
330 #define DEV_CONFIG_VALUE 1 /* cdc or subset */
331 #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
333 static struct usb_device_descriptor
335 .bLength = sizeof device_desc,
336 .bDescriptorType = USB_DT_DEVICE,
338 .bcdUSB = __constant_cpu_to_le16 (0x0200),
340 .bDeviceClass = USB_CLASS_COMM,
341 .bDeviceSubClass = 0,
342 .bDeviceProtocol = 0,
344 .idVendor = __constant_cpu_to_le16 (CDC_VENDOR_NUM),
345 .idProduct = __constant_cpu_to_le16 (CDC_PRODUCT_NUM),
346 .iManufacturer = STRING_MANUFACTURER,
347 .iProduct = STRING_PRODUCT,
348 .bNumConfigurations = 1,
351 static struct usb_config_descriptor
353 .bLength = sizeof eth_config,
354 .bDescriptorType = USB_DT_CONFIG,
356 /* compute wTotalLength on the fly */
358 .bConfigurationValue = DEV_CONFIG_VALUE,
359 .iConfiguration = STRING_CDC,
360 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
364 #ifdef CONFIG_USB_ETH_RNDIS
365 static const struct usb_config_descriptor
367 .bLength = sizeof rndis_config,
368 .bDescriptorType = USB_DT_CONFIG,
370 /* compute wTotalLength on the fly */
372 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
373 .iConfiguration = STRING_RNDIS,
374 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
380 * Compared to the simple CDC subset, the full CDC Ethernet model adds
381 * three class descriptors, two interface descriptors, optional status
382 * endpoint. Both have a "data" interface and two bulk endpoints.
383 * There are also differences in how control requests are handled.
385 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of
386 * the CDC-ACM (modem) spec.
389 #ifdef DEV_CONFIG_CDC
390 static struct usb_interface_descriptor
392 .bLength = sizeof control_intf,
393 .bDescriptorType = USB_DT_INTERFACE,
395 .bInterfaceNumber = 0,
396 /* status endpoint is optional; this may be patched later */
398 .bInterfaceClass = USB_CLASS_COMM,
399 .bInterfaceSubClass = 6, /* ethernet control model */
400 .bInterfaceProtocol = 0,
401 .iInterface = STRING_CONTROL,
405 #ifdef CONFIG_USB_ETH_RNDIS
406 static const struct usb_interface_descriptor
407 rndis_control_intf = {
408 .bLength = sizeof rndis_control_intf,
409 .bDescriptorType = USB_DT_INTERFACE,
411 .bInterfaceNumber = 0,
413 .bInterfaceClass = USB_CLASS_COMM,
414 .bInterfaceSubClass = 2, /* abstract control model */
415 .bInterfaceProtocol = 0xff, /* vendor specific */
416 .iInterface = STRING_RNDIS_CONTROL,
420 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
422 /* "Header Functional Descriptor" from CDC spec 5.2.3.1 */
426 u8 bDescriptorSubType;
429 } __attribute__ ((packed));
431 static const struct header_desc header_desc = {
432 .bLength = sizeof header_desc,
433 .bDescriptorType = USB_DT_CS_INTERFACE,
434 .bDescriptorSubType = 0,
436 .bcdCDC = __constant_cpu_to_le16 (0x0110),
439 /* "Union Functional Descriptor" from CDC spec 5.2.3.8 */
443 u8 bDescriptorSubType;
445 u8 bMasterInterface0;
447 /* ... and there could be other slave interfaces */
448 } __attribute__ ((packed));
450 static const struct union_desc union_desc = {
451 .bLength = sizeof union_desc,
452 .bDescriptorType = USB_DT_CS_INTERFACE,
453 .bDescriptorSubType = 6,
455 .bMasterInterface0 = 0, /* index of control interface */
456 .bSlaveInterface0 = 1, /* index of DATA interface */
459 #endif /* CDC || RNDIS */
461 #ifdef CONFIG_USB_ETH_RNDIS
463 /* "Call Management Descriptor" from CDC spec 5.2.3.3 */
464 struct call_mgmt_descriptor {
467 u8 bDescriptorSubType;
471 } __attribute__ ((packed));
473 static const struct call_mgmt_descriptor call_mgmt_descriptor = {
474 .bLength = sizeof call_mgmt_descriptor,
475 .bDescriptorType = USB_DT_CS_INTERFACE,
476 .bDescriptorSubType = 0x01,
478 .bmCapabilities = 0x00,
479 .bDataInterface = 0x01,
483 /* "Abstract Control Management Descriptor" from CDC spec 5.2.3.4 */
484 struct acm_descriptor {
487 u8 bDescriptorSubType;
490 } __attribute__ ((packed));
492 static struct acm_descriptor acm_descriptor = {
493 .bLength = sizeof acm_descriptor,
494 .bDescriptorType = USB_DT_CS_INTERFACE,
495 .bDescriptorSubType = 0x02,
497 .bmCapabilities = 0X00,
502 #ifdef DEV_CONFIG_CDC
504 /* "Ethernet Networking Functional Descriptor" from CDC spec 5.2.3.16 */
508 u8 bDescriptorSubType;
511 u32 bmEthernetStatistics;
513 u16 wNumberMCFilters;
514 u8 bNumberPowerFilters;
515 } __attribute__ ((packed));
517 static const struct ether_desc ether_desc = {
518 .bLength = sizeof ether_desc,
519 .bDescriptorType = USB_DT_CS_INTERFACE,
520 .bDescriptorSubType = 0x0f,
522 /* this descriptor actually adds value, surprise! */
523 .iMACAddress = STRING_ETHADDR,
524 .bmEthernetStatistics = __constant_cpu_to_le32 (0), /* no statistics */
525 .wMaxSegmentSize = __constant_cpu_to_le16 (ETH_FRAME_LEN),
526 .wNumberMCFilters = __constant_cpu_to_le16 (0),
527 .bNumberPowerFilters = 0,
532 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
534 /* include the status endpoint if we can, even where it's optional.
535 * use small wMaxPacketSize, since many "interrupt" endpoints have
536 * very small fifos and it's no big deal if CDC_NOTIFY_SPEED_CHANGE
537 * takes two packets. also default to a big transfer interval, to
538 * waste less bandwidth.
540 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
541 * if they ignore the connect/disconnect notifications that real aether
542 * can provide. more advanced cdc configurations might want to support
543 * encapsulated commands (vendor-specific, using control-OUT).
545 * RNDIS requires the status endpoint, since it uses that encapsulation
546 * mechanism for its funky RPC scheme.
549 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
550 #define STATUS_BYTECOUNT 8 /* 8 byte header + data */
552 static struct usb_endpoint_descriptor
554 .bLength = USB_DT_ENDPOINT_SIZE,
555 .bDescriptorType = USB_DT_ENDPOINT,
557 .bEndpointAddress = USB_DIR_IN,
558 .bmAttributes = USB_ENDPOINT_XFER_INT,
559 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
560 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
564 #ifdef DEV_CONFIG_CDC
566 /* the default data interface has no endpoints ... */
568 static const struct usb_interface_descriptor
570 .bLength = sizeof data_nop_intf,
571 .bDescriptorType = USB_DT_INTERFACE,
573 .bInterfaceNumber = 1,
574 .bAlternateSetting = 0,
576 .bInterfaceClass = USB_CLASS_CDC_DATA,
577 .bInterfaceSubClass = 0,
578 .bInterfaceProtocol = 0,
581 /* ... but the "real" data interface has two bulk endpoints */
583 static const struct usb_interface_descriptor
585 .bLength = sizeof data_intf,
586 .bDescriptorType = USB_DT_INTERFACE,
588 .bInterfaceNumber = 1,
589 .bAlternateSetting = 1,
591 .bInterfaceClass = USB_CLASS_CDC_DATA,
592 .bInterfaceSubClass = 0,
593 .bInterfaceProtocol = 0,
594 .iInterface = STRING_DATA,
599 #ifdef CONFIG_USB_ETH_RNDIS
601 /* RNDIS doesn't activate by changing to the "real" altsetting */
603 static const struct usb_interface_descriptor
605 .bLength = sizeof rndis_data_intf,
606 .bDescriptorType = USB_DT_INTERFACE,
608 .bInterfaceNumber = 1,
609 .bAlternateSetting = 0,
611 .bInterfaceClass = USB_CLASS_CDC_DATA,
612 .bInterfaceSubClass = 0,
613 .bInterfaceProtocol = 0,
614 .iInterface = STRING_DATA,
619 #ifdef DEV_CONFIG_SUBSET
622 * "Simple" CDC-subset option is a simple vendor-neutral model that most
623 * full speed controllers can handle: one interface, two bulk endpoints.
626 static const struct usb_interface_descriptor
628 .bLength = sizeof subset_data_intf,
629 .bDescriptorType = USB_DT_INTERFACE,
631 .bInterfaceNumber = 0,
632 .bAlternateSetting = 0,
634 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
635 .bInterfaceSubClass = 0,
636 .bInterfaceProtocol = 0,
637 .iInterface = STRING_DATA,
643 static struct usb_endpoint_descriptor
645 .bLength = USB_DT_ENDPOINT_SIZE,
646 .bDescriptorType = USB_DT_ENDPOINT,
648 .bEndpointAddress = USB_DIR_IN,
649 .bmAttributes = USB_ENDPOINT_XFER_BULK,
652 static struct usb_endpoint_descriptor
654 .bLength = USB_DT_ENDPOINT_SIZE,
655 .bDescriptorType = USB_DT_ENDPOINT,
657 .bEndpointAddress = USB_DIR_OUT,
658 .bmAttributes = USB_ENDPOINT_XFER_BULK,
661 static const struct usb_descriptor_header *fs_eth_function [10] = {
662 #ifdef DEV_CONFIG_CDC
663 /* "cdc" mode descriptors */
664 (struct usb_descriptor_header *) &control_intf,
665 (struct usb_descriptor_header *) &header_desc,
666 (struct usb_descriptor_header *) &union_desc,
667 (struct usb_descriptor_header *) ðer_desc,
668 /* NOTE: status endpoint may need to be removed */
669 (struct usb_descriptor_header *) &fs_status_desc,
670 /* data interface, with altsetting */
671 (struct usb_descriptor_header *) &data_nop_intf,
672 (struct usb_descriptor_header *) &data_intf,
673 (struct usb_descriptor_header *) &fs_source_desc,
674 (struct usb_descriptor_header *) &fs_sink_desc,
676 #endif /* DEV_CONFIG_CDC */
679 static inline void __init fs_subset_descriptors(void)
681 #ifdef DEV_CONFIG_SUBSET
682 fs_eth_function[0] = (struct usb_descriptor_header *) &subset_data_intf;
683 fs_eth_function[1] = (struct usb_descriptor_header *) &fs_source_desc;
684 fs_eth_function[2] = (struct usb_descriptor_header *) &fs_sink_desc;
685 fs_eth_function[3] = 0;
687 fs_eth_function[0] = 0;
691 #ifdef CONFIG_USB_ETH_RNDIS
692 static const struct usb_descriptor_header *fs_rndis_function [] = {
693 /* control interface matches ACM, not Ethernet */
694 (struct usb_descriptor_header *) &rndis_control_intf,
695 (struct usb_descriptor_header *) &header_desc,
696 (struct usb_descriptor_header *) &call_mgmt_descriptor,
697 (struct usb_descriptor_header *) &acm_descriptor,
698 (struct usb_descriptor_header *) &union_desc,
699 (struct usb_descriptor_header *) &fs_status_desc,
700 /* data interface has no altsetting */
701 (struct usb_descriptor_header *) &rndis_data_intf,
702 (struct usb_descriptor_header *) &fs_source_desc,
703 (struct usb_descriptor_header *) &fs_sink_desc,
708 #ifdef CONFIG_USB_GADGET_DUALSPEED
711 * usb 2.0 devices need to expose both high speed and full speed
712 * descriptors, unless they only run at full speed.
715 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
716 static struct usb_endpoint_descriptor
718 .bLength = USB_DT_ENDPOINT_SIZE,
719 .bDescriptorType = USB_DT_ENDPOINT,
721 .bmAttributes = USB_ENDPOINT_XFER_INT,
722 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
723 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
725 #endif /* DEV_CONFIG_CDC */
727 static struct usb_endpoint_descriptor
729 .bLength = USB_DT_ENDPOINT_SIZE,
730 .bDescriptorType = USB_DT_ENDPOINT,
732 .bmAttributes = USB_ENDPOINT_XFER_BULK,
733 .wMaxPacketSize = __constant_cpu_to_le16 (512),
736 static struct usb_endpoint_descriptor
738 .bLength = USB_DT_ENDPOINT_SIZE,
739 .bDescriptorType = USB_DT_ENDPOINT,
741 .bmAttributes = USB_ENDPOINT_XFER_BULK,
742 .wMaxPacketSize = __constant_cpu_to_le16 (512),
745 static struct usb_qualifier_descriptor
747 .bLength = sizeof dev_qualifier,
748 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
750 .bcdUSB = __constant_cpu_to_le16 (0x0200),
751 .bDeviceClass = USB_CLASS_COMM,
753 .bNumConfigurations = 1,
756 static const struct usb_descriptor_header *hs_eth_function [10] = {
757 #ifdef DEV_CONFIG_CDC
758 /* "cdc" mode descriptors */
759 (struct usb_descriptor_header *) &control_intf,
760 (struct usb_descriptor_header *) &header_desc,
761 (struct usb_descriptor_header *) &union_desc,
762 (struct usb_descriptor_header *) ðer_desc,
763 /* NOTE: status endpoint may need to be removed */
764 (struct usb_descriptor_header *) &hs_status_desc,
765 /* data interface, with altsetting */
766 (struct usb_descriptor_header *) &data_nop_intf,
767 (struct usb_descriptor_header *) &data_intf,
768 (struct usb_descriptor_header *) &hs_source_desc,
769 (struct usb_descriptor_header *) &hs_sink_desc,
771 #endif /* DEV_CONFIG_CDC */
774 static inline void __init hs_subset_descriptors(void)
776 #ifdef DEV_CONFIG_SUBSET
777 hs_eth_function[0] = (struct usb_descriptor_header *) &subset_data_intf;
778 hs_eth_function[1] = (struct usb_descriptor_header *) &fs_source_desc;
779 hs_eth_function[2] = (struct usb_descriptor_header *) &fs_sink_desc;
780 hs_eth_function[3] = 0;
782 hs_eth_function[0] = 0;
786 #ifdef CONFIG_USB_ETH_RNDIS
787 static const struct usb_descriptor_header *hs_rndis_function [] = {
788 /* control interface matches ACM, not Ethernet */
789 (struct usb_descriptor_header *) &rndis_control_intf,
790 (struct usb_descriptor_header *) &header_desc,
791 (struct usb_descriptor_header *) &call_mgmt_descriptor,
792 (struct usb_descriptor_header *) &acm_descriptor,
793 (struct usb_descriptor_header *) &union_desc,
794 (struct usb_descriptor_header *) &hs_status_desc,
795 /* data interface has no altsetting */
796 (struct usb_descriptor_header *) &rndis_data_intf,
797 (struct usb_descriptor_header *) &hs_source_desc,
798 (struct usb_descriptor_header *) &hs_sink_desc,
804 /* maxpacket and other transfer characteristics vary by speed. */
805 #define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
809 /* if there's no high speed support, maxpacket doesn't change. */
810 #define ep_desc(g,hs,fs) fs
812 static inline void __init hs_subset_descriptors(void)
816 #endif /* !CONFIG_USB_GADGET_DUALSPEED */
818 /*-------------------------------------------------------------------------*/
820 /* descriptors that are built on-demand */
822 static char manufacturer [40];
823 static char product_desc [40] = DRIVER_DESC;
825 #ifdef DEV_CONFIG_CDC
826 /* address that the host will use ... usually assigned at random */
827 static char ethaddr [2 * ETH_ALEN + 1];
830 /* static strings, in iso 8859/1 */
831 static struct usb_string strings [] = {
832 { STRING_MANUFACTURER, manufacturer, },
833 { STRING_PRODUCT, product_desc, },
834 { STRING_DATA, "Ethernet Data", },
835 #ifdef DEV_CONFIG_CDC
836 { STRING_CDC, "CDC Ethernet", },
837 { STRING_ETHADDR, ethaddr, },
838 { STRING_CONTROL, "CDC Communications Control", },
840 #ifdef DEV_CONFIG_SUBSET
841 { STRING_SUBSET, "CDC Ethernet Subset", },
843 #ifdef CONFIG_USB_ETH_RNDIS
844 { STRING_RNDIS, "RNDIS", },
845 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
847 { } /* end of list */
850 static struct usb_gadget_strings stringtab = {
851 .language = 0x0409, /* en-us */
856 * one config, two interfaces: control, data.
857 * complications: class descriptors, and an altsetting.
860 config_buf (enum usb_device_speed speed, u8 *buf, u8 type, unsigned index)
863 #ifdef CONFIG_USB_GADGET_DUALSPEED
864 int hs = (speed == USB_SPEED_HIGH);
866 if (type == USB_DT_OTHER_SPEED_CONFIG)
868 #define which_config(t) (hs ? & t ## _config : & t ## _config)
869 #define which_fn(t) (hs ? & hs_ ## t ## _function : & fs_ ## t ## _function)
871 #define which_config(t) (& t ## _config)
872 #define which_fn(t) (& fs_ ## t ## _function)
875 if (index >= device_desc.bNumConfigurations)
878 #ifdef CONFIG_USB_ETH_RNDIS
879 /* list the RNDIS config first, to make Microsoft's drivers
880 * happy. DOCSIS 1.0 needs this too.
882 if (device_desc.bNumConfigurations == 2 && index == 0)
883 len = usb_gadget_config_buf (which_config (rndis), buf,
884 USB_BUFSIZ, (const struct usb_descriptor_header **)
888 len = usb_gadget_config_buf (which_config (eth), buf,
889 USB_BUFSIZ, (const struct usb_descriptor_header **)
893 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
897 /*-------------------------------------------------------------------------*/
899 static void eth_start (struct eth_dev *dev, int gfp_flags);
900 static int alloc_requests (struct eth_dev *dev, unsigned n, int gfp_flags);
902 #ifdef DEV_CONFIG_CDC
903 static inline int ether_alt_ep_setup (struct eth_dev *dev, struct usb_ep *ep)
905 const struct usb_endpoint_descriptor *d;
907 /* With CDC, the host isn't allowed to use these two data
908 * endpoints in the default altsetting for the interface.
909 * so we don't activate them yet. Reset from SET_INTERFACE.
911 * Strictly speaking RNDIS should work the same: activation is
912 * a side effect of setting a packet filter. Deactivation is
913 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
916 /* one endpoint writes data back IN to the host */
917 if (strcmp (ep->name, EP_IN_NAME) == 0) {
918 d = ep_desc (dev->gadget, &hs_source_desc, &fs_source_desc);
919 ep->driver_data = dev;
923 /* one endpoint just reads OUT packets */
924 } else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
925 d = ep_desc (dev->gadget, &hs_sink_desc, &fs_sink_desc);
926 ep->driver_data = dev;
930 /* optional status/notification endpoint */
931 } else if (EP_STATUS_NAME &&
932 strcmp (ep->name, EP_STATUS_NAME) == 0) {
935 d = ep_desc (dev->gadget, &hs_status_desc, &fs_status_desc);
936 result = usb_ep_enable (ep, d);
940 ep->driver_data = dev;
948 #if defined(DEV_CONFIG_SUBSET) || defined(CONFIG_USB_ETH_RNDIS)
949 static inline int ether_ep_setup (struct eth_dev *dev, struct usb_ep *ep)
952 const struct usb_endpoint_descriptor *d;
954 /* CDC subset is simpler: if the device is there,
955 * it's live with rx and tx endpoints.
957 * Do this as a shortcut for RNDIS too.
960 /* one endpoint writes data back IN to the host */
961 if (strcmp (ep->name, EP_IN_NAME) == 0) {
962 d = ep_desc (dev->gadget, &hs_source_desc, &fs_source_desc);
963 result = usb_ep_enable (ep, d);
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 result = usb_ep_enable (ep, d);
978 ep->driver_data = dev;
988 set_ether_config (struct eth_dev *dev, int gfp_flags)
992 struct usb_gadget *gadget = dev->gadget;
994 gadget_for_each_ep (ep, gadget) {
995 #ifdef DEV_CONFIG_CDC
996 if (!dev->rndis && dev->cdc) {
997 result = ether_alt_ep_setup (dev, ep);
1003 #ifdef CONFIG_USB_ETH_RNDIS
1004 if (dev->rndis && strcmp (ep->name, EP_STATUS_NAME) == 0) {
1005 const struct usb_endpoint_descriptor *d;
1006 d = ep_desc (gadget, &hs_status_desc, &fs_status_desc);
1007 result = usb_ep_enable (ep, d);
1009 ep->driver_data = dev;
1010 dev->status_ep = ep;
1018 #if defined(DEV_CONFIG_SUBSET) || defined(CONFIG_USB_ETH_RNDIS)
1019 result = ether_ep_setup (dev, ep);
1026 ERROR (dev, "can't enable %s, result %d\n", ep->name, result);
1029 if (!result && (!dev->in_ep || !dev->out_ep))
1033 result = alloc_requests (dev, qlen (gadget), gfp_flags);
1035 /* on error, disable any endpoints */
1037 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
1039 (void) usb_ep_disable (dev->status_ep);
1043 #if defined(DEV_CONFIG_SUBSET) || defined(CONFIG_USB_ETH_RNDIS)
1044 if (dev->rndis || !dev->cdc) {
1046 (void) usb_ep_disable (dev->in_ep);
1048 (void) usb_ep_disable (dev->out_ep);
1057 /* activate non-CDC configs right away
1058 * this isn't strictly according to the RNDIS spec
1060 #if defined(DEV_CONFIG_SUBSET) || defined(CONFIG_USB_ETH_RNDIS)
1061 if (dev->rndis || !dev->cdc) {
1062 netif_carrier_on (dev->net);
1063 if (netif_running (dev->net)) {
1064 spin_unlock (&dev->lock);
1065 eth_start (dev, GFP_ATOMIC);
1066 spin_lock (&dev->lock);
1072 DEBUG (dev, "qlen %d\n", qlen (gadget));
1074 /* caller is responsible for cleanup on error */
1078 static void eth_reset_config (struct eth_dev *dev)
1080 struct usb_request *req;
1082 if (dev->config == 0)
1085 DEBUG (dev, "%s\n", __FUNCTION__);
1087 netif_stop_queue (dev->net);
1088 netif_carrier_off (dev->net);
1090 /* disable endpoints, forcing (synchronous) completion of
1091 * pending i/o. then free the requests.
1094 usb_ep_disable (dev->in_ep);
1095 while (likely (!list_empty (&dev->tx_reqs))) {
1096 req = container_of (dev->tx_reqs.next,
1097 struct usb_request, list);
1098 list_del (&req->list);
1099 usb_ep_free_request (dev->in_ep, req);
1104 usb_ep_disable (dev->out_ep);
1105 while (likely (!list_empty (&dev->rx_reqs))) {
1106 req = container_of (dev->rx_reqs.next,
1107 struct usb_request, list);
1108 list_del (&req->list);
1109 usb_ep_free_request (dev->out_ep, req);
1114 if (dev->status_ep) {
1115 usb_ep_disable (dev->status_ep);
1121 /* change our operational config. must agree with the code
1122 * that returns config descriptors, and altsetting code.
1125 eth_set_config (struct eth_dev *dev, unsigned number, int gfp_flags)
1128 struct usb_gadget *gadget = dev->gadget;
1130 if (number == dev->config)
1133 if (gadget_is_sa1100 (gadget)
1135 && atomic_read (&dev->tx_qlen) != 0) {
1136 /* tx fifo is full, but we can't clear it...*/
1137 INFO (dev, "can't change configurations\n");
1140 eth_reset_config (dev);
1143 case DEV_CONFIG_VALUE:
1145 result = set_ether_config (dev, gfp_flags);
1147 #ifdef CONFIG_USB_ETH_RNDIS
1148 case DEV_RNDIS_CONFIG_VALUE:
1150 result = set_ether_config (dev, gfp_flags);
1161 eth_reset_config (dev);
1165 switch (gadget->speed) {
1166 case USB_SPEED_FULL: speed = "full"; break;
1167 #ifdef CONFIG_USB_GADGET_DUALSPEED
1168 case USB_SPEED_HIGH: speed = "high"; break;
1170 default: speed = "?"; break;
1173 dev->config = number;
1174 INFO (dev, "%s speed config #%d: %s, using %s\n",
1175 speed, number, driver_desc,
1180 : "CDC Ethernet Subset"));
1185 /*-------------------------------------------------------------------------*/
1187 /* section 3.8.2 table 11 of the CDC spec lists Ethernet notifications
1188 * section 3.6.2.1 table 5 specifies ACM notifications, accepted by RNDIS
1189 * and RNDIS also defines its own bit-incompatible notifications
1191 #define CDC_NOTIFY_NETWORK_CONNECTION 0x00 /* required; 6.3.1 */
1192 #define CDC_NOTIFY_RESPONSE_AVAILABLE 0x01 /* optional; 6.3.2 */
1193 #define CDC_NOTIFY_SPEED_CHANGE 0x2a /* required; 6.3.8 */
1195 #ifdef DEV_CONFIG_CDC
1197 struct cdc_notification {
1199 u8 bNotificationType;
1204 /* SPEED_CHANGE data looks like this */
1208 static void eth_status_complete (struct usb_ep *ep, struct usb_request *req)
1210 struct cdc_notification *event = req->buf;
1211 int value = req->status;
1212 struct eth_dev *dev = ep->driver_data;
1214 /* issue the second notification if host reads the first */
1215 if (event->bNotificationType == CDC_NOTIFY_NETWORK_CONNECTION
1217 event->bmRequestType = 0xA1;
1218 event->bNotificationType = CDC_NOTIFY_SPEED_CHANGE;
1219 event->wValue = __constant_cpu_to_le16 (0);
1220 event->wIndex = __constant_cpu_to_le16 (1);
1221 event->wLength = __constant_cpu_to_le16 (8);
1223 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1224 event->data [0] = event->data [1] =
1225 (dev->gadget->speed == USB_SPEED_HIGH)
1226 ? (13 * 512 * 8 * 1000 * 8)
1227 : (19 * 64 * 1 * 1000 * 8);
1230 value = usb_ep_queue (ep, req, GFP_ATOMIC);
1231 DEBUG (dev, "send SPEED_CHANGE --> %d\n", value);
1235 DEBUG (dev, "event %02x --> %d\n",
1236 event->bNotificationType, value);
1238 /* free when done */
1239 usb_ep_free_buffer (ep, req->buf, req->dma, 16);
1240 usb_ep_free_request (ep, req);
1243 static void issue_start_status (struct eth_dev *dev)
1245 struct usb_request *req;
1246 struct cdc_notification *event;
1249 DEBUG (dev, "%s, flush old status first\n", __FUNCTION__);
1253 * FIXME ugly idiom, maybe we'd be better with just
1254 * a "cancel the whole queue" primitive since any
1255 * unlink-one primitive has way too many error modes.
1256 * here, we "know" toggle is already clear...
1258 usb_ep_disable (dev->status_ep);
1259 usb_ep_enable (dev->status_ep, dev->status);
1261 /* FIXME make these allocations static like dev->req */
1262 req = usb_ep_alloc_request (dev->status_ep, GFP_ATOMIC);
1264 DEBUG (dev, "status ENOMEM\n");
1267 req->buf = usb_ep_alloc_buffer (dev->status_ep, 16,
1268 &dev->req->dma, GFP_ATOMIC);
1269 if (req->buf == 0) {
1270 DEBUG (dev, "status buf ENOMEM\n");
1272 usb_ep_free_request (dev->status_ep, req);
1276 /* 3.8.1 says to issue first NETWORK_CONNECTION, then
1277 * a SPEED_CHANGE. could be useful in some configs.
1280 event->bmRequestType = 0xA1;
1281 event->bNotificationType = CDC_NOTIFY_NETWORK_CONNECTION;
1282 event->wValue = __constant_cpu_to_le16 (1); /* connected */
1283 event->wIndex = __constant_cpu_to_le16 (1);
1287 req->complete = eth_status_complete;
1288 value = usb_ep_queue (dev->status_ep, req, GFP_ATOMIC);
1290 DEBUG (dev, "status buf queue --> %d\n", value);
1291 usb_ep_free_buffer (dev->status_ep,
1292 req->buf, dev->req->dma, 16);
1299 /*-------------------------------------------------------------------------*/
1301 static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req)
1303 if (req->status || req->actual != req->length)
1304 DEBUG ((struct eth_dev *) ep->driver_data,
1305 "setup complete --> %d, %d/%d\n",
1306 req->status, req->actual, req->length);
1309 /* see section 3.8.2 table 10 of the CDC spec for more ethernet
1310 * requests, mostly for filters (multicast, pm) and statistics
1311 * section 3.6.2.1 table 4 has ACM requests; RNDIS requires the
1312 * encapsulated command mechanism.
1314 #define CDC_SEND_ENCAPSULATED_COMMAND 0x00 /* optional */
1315 #define CDC_GET_ENCAPSULATED_RESPONSE 0x01 /* optional */
1316 #define CDC_SET_ETHERNET_PACKET_FILTER 0x43 /* required */
1318 #ifdef CONFIG_USB_ETH_RNDIS
1320 static void rndis_response_complete (struct usb_ep *ep, struct usb_request *req)
1322 struct eth_dev *dev = ep->driver_data;
1324 if (req->status || req->actual != req->length)
1325 DEBUG (dev, "rndis response complete --> %d, %d/%d\n",
1326 req->status, req->actual, req->length);
1328 /* done sending after CDC_GET_ENCAPSULATED_RESPONSE */
1329 rndis_free_response (dev->rndis_config, req->buf);
1332 static void rndis_command_complete (struct usb_ep *ep, struct usb_request *req)
1334 struct eth_dev *dev = ep->driver_data;
1336 /* received RNDIS command from CDC_SEND_ENCAPSULATED_COMMAND */
1337 spin_lock(&dev->lock);
1338 if (rndis_msg_parser (dev->rndis_config, (u8 *) req->buf))
1339 ERROR(dev, "%s: rndis parse error\n", __FUNCTION__ );
1340 spin_unlock(&dev->lock);
1346 * The setup() callback implements all the ep0 functionality that's not
1347 * handled lower down. CDC has a number of less-common features:
1349 * - two interfaces: control, and ethernet data
1350 * - Ethernet data interface has two altsettings: default, and active
1351 * - class-specific descriptors for the control interface
1352 * - class-specific control requests
1355 eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1357 struct eth_dev *dev = get_gadget_data (gadget);
1358 struct usb_request *req = dev->req;
1359 int value = -EOPNOTSUPP;
1361 /* descriptors just go into the pre-allocated ep0 buffer,
1362 * while config change events may enable network traffic.
1364 switch (ctrl->bRequest) {
1366 case USB_REQ_GET_DESCRIPTOR:
1367 if (ctrl->bRequestType != USB_DIR_IN)
1369 switch (ctrl->wValue >> 8) {
1372 value = min (ctrl->wLength, (u16) sizeof device_desc);
1373 memcpy (req->buf, &device_desc, value);
1375 #ifdef CONFIG_USB_GADGET_DUALSPEED
1376 case USB_DT_DEVICE_QUALIFIER:
1377 if (!gadget->is_dualspeed)
1379 value = min (ctrl->wLength, (u16) sizeof dev_qualifier);
1380 memcpy (req->buf, &dev_qualifier, value);
1383 case USB_DT_OTHER_SPEED_CONFIG:
1384 if (!gadget->is_dualspeed)
1387 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1389 value = config_buf (gadget->speed, req->buf,
1391 ctrl->wValue & 0xff);
1393 value = min (ctrl->wLength, (u16) value);
1397 value = usb_gadget_get_string (&stringtab,
1398 ctrl->wValue & 0xff, req->buf);
1400 value = min (ctrl->wLength, (u16) value);
1405 case USB_REQ_SET_CONFIGURATION:
1406 if (ctrl->bRequestType != 0)
1408 spin_lock (&dev->lock);
1409 value = eth_set_config (dev, ctrl->wValue, GFP_ATOMIC);
1410 spin_unlock (&dev->lock);
1412 case USB_REQ_GET_CONFIGURATION:
1413 if (ctrl->bRequestType != USB_DIR_IN)
1415 *(u8 *)req->buf = dev->config;
1416 value = min (ctrl->wLength, (u16) 1);
1419 case USB_REQ_SET_INTERFACE:
1420 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1422 || ctrl->wIndex > 1)
1424 if (!dev->cdc && ctrl->wIndex != 0)
1426 spin_lock (&dev->lock);
1428 /* PXA hardware partially handles SET_INTERFACE;
1429 * we need to kluge around that interference.
1431 if (gadget_is_pxa (gadget)) {
1432 value = eth_set_config (dev, DEV_CONFIG_VALUE,
1437 #ifdef DEV_CONFIG_CDC
1438 switch (ctrl->wIndex) {
1439 case 0: /* control/master intf */
1440 if (ctrl->wValue != 0)
1442 if (dev->status_ep) {
1443 usb_ep_disable (dev->status_ep);
1444 usb_ep_enable (dev->status_ep, dev->status);
1448 case 1: /* data intf */
1449 if (ctrl->wValue > 1)
1451 usb_ep_disable (dev->in_ep);
1452 usb_ep_disable (dev->out_ep);
1454 /* CDC requires the data transfers not be done from
1455 * the default interface setting ... also, setting
1456 * the non-default interface clears filters etc.
1458 if (ctrl->wValue == 1) {
1459 usb_ep_enable (dev->in_ep, dev->in);
1460 usb_ep_enable (dev->out_ep, dev->out);
1461 netif_carrier_on (dev->net);
1463 issue_start_status (dev);
1464 if (netif_running (dev->net)) {
1465 spin_unlock (&dev->lock);
1466 eth_start (dev, GFP_ATOMIC);
1467 spin_lock (&dev->lock);
1470 netif_stop_queue (dev->net);
1471 netif_carrier_off (dev->net);
1477 /* FIXME this is wrong, as is the assumption that
1478 * all non-PXA hardware talks real CDC ...
1480 dev_warn (&gadget->dev, "set_interface ignored!\n");
1481 #endif /* DEV_CONFIG_CDC */
1484 spin_unlock (&dev->lock);
1486 case USB_REQ_GET_INTERFACE:
1487 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1489 || ctrl->wIndex > 1)
1491 if (!(dev->cdc || dev->rndis) && ctrl->wIndex != 0)
1494 /* for CDC, iff carrier is on, data interface is active. */
1495 if (dev->rndis || ctrl->wIndex != 1)
1496 *(u8 *)req->buf = 0;
1498 *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0;
1499 value = min (ctrl->wLength, (u16) 1);
1502 #ifdef DEV_CONFIG_CDC
1503 case CDC_SET_ETHERNET_PACKET_FILTER:
1504 /* see 6.2.30: no data, wIndex = interface,
1505 * wValue = packet filter bitmap
1507 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1510 || ctrl->wLength != 0
1511 || ctrl->wIndex > 1)
1513 DEBUG (dev, "NOP packet filter %04x\n", ctrl->wValue);
1514 /* NOTE: table 62 has 5 filter bits to reduce traffic,
1515 * and we "must" support multicast and promiscuous.
1516 * this NOP implements a bad filter...
1520 #endif /* DEV_CONFIG_CDC */
1522 #ifdef CONFIG_USB_ETH_RNDIS
1523 /* RNDIS uses the CDC command encapsulation mechanism to implement
1524 * an RPC scheme, with much getting/setting of attributes by OID.
1526 case CDC_SEND_ENCAPSULATED_COMMAND:
1527 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1529 || ctrl->wLength > USB_BUFSIZ
1531 || rndis_control_intf.bInterfaceNumber
1534 /* read the request, then process it */
1535 value = ctrl->wLength;
1536 req->complete = rndis_command_complete;
1537 /* later, rndis_control_ack () sends a notification */
1540 case CDC_GET_ENCAPSULATED_RESPONSE:
1541 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1542 == ctrl->bRequestType
1544 // && ctrl->wLength >= 0x0400
1546 && rndis_control_intf.bInterfaceNumber
1550 /* return the result */
1551 buf = rndis_get_next_response (dev->rndis_config,
1554 memcpy (req->buf, buf, value);
1555 req->complete = rndis_response_complete;
1557 /* else stalls ... spec says to avoid that */
1564 "unknown control req%02x.%02x v%04x i%04x l%d\n",
1565 ctrl->bRequestType, ctrl->bRequest,
1566 ctrl->wValue, ctrl->wIndex, ctrl->wLength);
1569 /* respond with data transfer before status phase? */
1571 req->length = value;
1572 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1574 DEBUG (dev, "ep_queue --> %d\n", value);
1576 eth_setup_complete (gadget->ep0, req);
1580 /* host either stalls (value < 0) or reports success */
1585 eth_disconnect (struct usb_gadget *gadget)
1587 struct eth_dev *dev = get_gadget_data (gadget);
1588 unsigned long flags;
1590 spin_lock_irqsave (&dev->lock, flags);
1591 netif_stop_queue (dev->net);
1592 netif_carrier_off (dev->net);
1593 eth_reset_config (dev);
1594 spin_unlock_irqrestore (&dev->lock, flags);
1596 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1598 /* next we may get setup() calls to enumerate new connections;
1599 * or an unbind() during shutdown (including removing module).
1603 /*-------------------------------------------------------------------------*/
1605 /* NETWORK DRIVER HOOKUP (to the layer above this driver) */
1607 static int eth_change_mtu (struct net_device *net, int new_mtu)
1609 struct eth_dev *dev = (struct eth_dev *) net->priv;
1611 // FIXME if rndis, don't change while link's live
1613 if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN)
1615 /* no zero-length packet read wanted after mtu-sized packets */
1616 if (((new_mtu + sizeof (struct ethhdr)) % dev->in_ep->maxpacket) == 0)
1622 static struct net_device_stats *eth_get_stats (struct net_device *net)
1624 return &((struct eth_dev *) net->priv)->stats;
1627 static int eth_ethtool_ioctl (struct net_device *net, void *useraddr)
1629 struct eth_dev *dev = (struct eth_dev *) net->priv;
1632 if (get_user (cmd, (u32 *)useraddr))
1636 case ETHTOOL_GDRVINFO: { /* get driver info */
1637 struct ethtool_drvinfo info;
1639 memset (&info, 0, sizeof info);
1640 info.cmd = ETHTOOL_GDRVINFO;
1641 strlcpy (info.driver, shortname, sizeof info.driver);
1642 strlcpy (info.version, DRIVER_VERSION, sizeof info.version);
1643 strlcpy (info.fw_version, dev->gadget->name,
1644 sizeof info.fw_version);
1645 strlcpy (info.bus_info, dev->gadget->dev.bus_id,
1646 sizeof info.bus_info);
1647 if (copy_to_user (useraddr, &info, sizeof (info)))
1652 case ETHTOOL_GLINK: { /* get link status */
1653 struct ethtool_value edata = { ETHTOOL_GLINK };
1655 edata.data = (dev->gadget->speed != USB_SPEED_UNKNOWN);
1656 if (copy_to_user (useraddr, &edata, sizeof (edata)))
1662 /* Note that the ethtool user space code requires EOPNOTSUPP */
1666 static int eth_ioctl (struct net_device *net, struct ifreq *rq, int cmd)
1670 return eth_ethtool_ioctl (net, (void *)rq->ifr_data);
1676 static void defer_kevent (struct eth_dev *dev, int flag)
1678 if (test_and_set_bit (flag, &dev->todo))
1680 if (!schedule_work (&dev->work))
1681 ERROR (dev, "kevent %d may have been dropped\n", flag);
1683 DEBUG (dev, "kevent %d scheduled\n", flag);
1686 static void rx_complete (struct usb_ep *ep, struct usb_request *req);
1689 rx_submit (struct eth_dev *dev, struct usb_request *req, int gfp_flags)
1691 struct sk_buff *skb;
1692 int retval = -ENOMEM;
1695 /* Padding up to RX_EXTRA handles minor disagreements with host.
1696 * Normally we use the USB "terminate on short read" convention;
1697 * so allow up to (N*maxpacket)-1, since that memory is normally
1698 * already allocated. Major loss of synch means -EOVERFLOW; any
1699 * obviously corrupted packets will automatically be discarded.
1701 * RNDIS uses internal framing, and explicitly allows senders to
1702 * pad to end-of-packet. That's potentially nice for speed,
1703 * but means receivers can't recover synch on their own.
1705 size = (sizeof (struct ethhdr) + dev->net->mtu + RX_EXTRA);
1706 size += dev->out_ep->maxpacket - 1;
1707 #ifdef CONFIG_USB_ETH_RNDIS
1709 size += sizeof (struct rndis_packet_msg_type);
1711 size -= size % dev->out_ep->maxpacket;
1712 #ifdef CONFIG_USB_ETH_RNDIS
1717 if ((skb = alloc_skb (size, gfp_flags)) == 0) {
1718 DEBUG (dev, "no rx skb\n");
1722 req->buf = skb->data;
1724 req->complete = rx_complete;
1727 retval = usb_ep_queue (dev->out_ep, req, gfp_flags);
1728 if (retval == -ENOMEM)
1730 defer_kevent (dev, WORK_RX_MEMORY);
1732 DEBUG (dev, "rx submit --> %d\n", retval);
1733 dev_kfree_skb_any (skb);
1734 spin_lock (&dev->lock);
1735 list_add (&req->list, &dev->rx_reqs);
1736 spin_unlock (&dev->lock);
1741 static void rx_complete (struct usb_ep *ep, struct usb_request *req)
1743 struct sk_buff *skb = req->context;
1744 struct eth_dev *dev = ep->driver_data;
1745 int status = req->status;
1749 /* normal completion */
1751 skb_put (skb, req->actual);
1752 #ifdef CONFIG_USB_ETH_RNDIS
1753 /* we know MaxPacketsPerTransfer == 1 here */
1755 rndis_rm_hdr (req->buf, &(skb->len));
1757 if (ETH_HLEN > skb->len || skb->len > ETH_FRAME_LEN) {
1758 dev->stats.rx_errors++;
1759 dev->stats.rx_length_errors++;
1760 DEBUG (dev, "rx length %d\n", skb->len);
1764 skb->dev = dev->net;
1765 skb->protocol = eth_type_trans (skb, dev->net);
1766 dev->stats.rx_packets++;
1767 dev->stats.rx_bytes += skb->len;
1769 /* no buffer copies needed, unless hardware can't
1772 status = netif_rx (skb);
1776 /* software-driven interface shutdown */
1777 case -ECONNRESET: // unlink
1778 case -ESHUTDOWN: // disconnect etc
1779 VDEBUG (dev, "rx shutdown, code %d\n", status);
1782 /* for hardware automagic (such as pxa) */
1783 case -ECONNABORTED: // endpoint reset
1784 DEBUG (dev, "rx %s reset\n", ep->name);
1785 defer_kevent (dev, WORK_RX_MEMORY);
1787 dev_kfree_skb_any (skb);
1792 dev->stats.rx_over_errors++;
1796 dev->stats.rx_errors++;
1797 DEBUG (dev, "rx status %d\n", status);
1802 dev_kfree_skb_any (skb);
1803 if (!netif_running (dev->net)) {
1805 /* nobody reading rx_reqs, so no dev->lock */
1806 list_add (&req->list, &dev->rx_reqs);
1810 rx_submit (dev, req, GFP_ATOMIC);
1813 static int prealloc (struct list_head *list, struct usb_ep *ep,
1814 unsigned n, int gfp_flags)
1817 struct usb_request *req;
1822 /* queue/recycle up to N requests */
1824 list_for_each_entry (req, list, list) {
1829 req = usb_ep_alloc_request (ep, gfp_flags);
1831 return list_empty (list) ? -ENOMEM : 0;
1832 list_add (&req->list, list);
1839 struct list_head *next;
1841 next = req->list.next;
1842 list_del (&req->list);
1843 usb_ep_free_request (ep, req);
1848 req = container_of (next, struct usb_request, list);
1853 static int alloc_requests (struct eth_dev *dev, unsigned n, int gfp_flags)
1857 status = prealloc (&dev->tx_reqs, dev->in_ep, n, gfp_flags);
1860 status = prealloc (&dev->rx_reqs, dev->out_ep, n, gfp_flags);
1865 DEBUG (dev, "can't alloc requests\n");
1869 static void rx_fill (struct eth_dev *dev, int gfp_flags)
1871 struct usb_request *req;
1872 unsigned long flags;
1874 clear_bit (WORK_RX_MEMORY, &dev->todo);
1876 /* fill unused rxq slots with some skb */
1877 spin_lock_irqsave (&dev->lock, flags);
1878 while (!list_empty (&dev->rx_reqs)) {
1879 req = container_of (dev->rx_reqs.next,
1880 struct usb_request, list);
1881 list_del_init (&req->list);
1882 spin_unlock_irqrestore (&dev->lock, flags);
1884 if (rx_submit (dev, req, gfp_flags) < 0) {
1885 defer_kevent (dev, WORK_RX_MEMORY);
1889 spin_lock_irqsave (&dev->lock, flags);
1891 spin_unlock_irqrestore (&dev->lock, flags);
1894 static void eth_work (void *_dev)
1896 struct eth_dev *dev = _dev;
1898 if (test_bit (WORK_RX_MEMORY, &dev->todo)) {
1899 if (netif_running (dev->net))
1900 rx_fill (dev, GFP_KERNEL);
1902 clear_bit (WORK_RX_MEMORY, &dev->todo);
1906 DEBUG (dev, "work done, flags = 0x%lx\n", dev->todo);
1909 static void tx_complete (struct usb_ep *ep, struct usb_request *req)
1911 struct sk_buff *skb = req->context;
1912 struct eth_dev *dev = ep->driver_data;
1914 switch (req->status) {
1916 dev->stats.tx_errors++;
1917 VDEBUG (dev, "tx err %d\n", req->status);
1919 case -ECONNRESET: // unlink
1920 case -ESHUTDOWN: // disconnect etc
1923 dev->stats.tx_bytes += skb->len;
1925 dev->stats.tx_packets++;
1927 spin_lock (&dev->lock);
1928 list_add (&req->list, &dev->tx_reqs);
1929 spin_unlock (&dev->lock);
1930 dev_kfree_skb_any (skb);
1932 atomic_dec (&dev->tx_qlen);
1933 if (netif_carrier_ok (dev->net))
1934 netif_wake_queue (dev->net);
1937 static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1939 struct eth_dev *dev = (struct eth_dev *) net->priv;
1940 int length = skb->len;
1942 struct usb_request *req = 0;
1943 unsigned long flags;
1945 spin_lock_irqsave (&dev->lock, flags);
1946 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1947 list_del (&req->list);
1948 if (list_empty (&dev->tx_reqs))
1949 netif_stop_queue (net);
1950 spin_unlock_irqrestore (&dev->lock, flags);
1952 /* no buffer copies needed, unless the network stack did it
1953 * or the hardware can't use skb buffers.
1954 * or there's not enough space for any RNDIS headers we need
1956 #ifdef CONFIG_USB_ETH_RNDIS
1958 struct sk_buff *skb_rndis;
1960 skb_rndis = skb_realloc_headroom (skb,
1961 sizeof (struct rndis_packet_msg_type));
1965 dev_kfree_skb_any (skb);
1967 rndis_add_hdr (skb);
1971 req->buf = skb->data;
1973 req->complete = tx_complete;
1975 /* use zlp framing on tx for strict CDC-Ether conformance,
1976 * though any robust network rx path ignores extra padding.
1977 * and some hardware doesn't like to write zlps.
1980 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1983 req->length = length;
1985 #ifdef CONFIG_USB_GADGET_DUALSPEED
1986 /* throttle highspeed IRQ rate back slightly */
1987 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1988 ? ((atomic_read (&dev->tx_qlen) % TX_DELAY) != 0)
1992 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1995 DEBUG (dev, "tx queue err %d\n", retval);
1998 net->trans_start = jiffies;
1999 atomic_inc (&dev->tx_qlen);
2003 #ifdef CONFIG_USB_ETH_RNDIS
2006 dev->stats.tx_dropped++;
2007 dev_kfree_skb_any (skb);
2008 spin_lock_irqsave (&dev->lock, flags);
2009 if (list_empty (&dev->tx_reqs))
2010 netif_start_queue (net);
2011 list_add (&req->list, &dev->tx_reqs);
2012 spin_unlock_irqrestore (&dev->lock, flags);
2017 /*-------------------------------------------------------------------------*/
2019 #ifdef CONFIG_USB_ETH_RNDIS
2021 static void rndis_send_media_state (struct eth_dev *dev, int connect)
2027 if (rndis_signal_connect (dev->rndis_config))
2030 if (rndis_signal_disconnect (dev->rndis_config))
2035 static int rndis_control_ack (struct net_device *net)
2037 struct eth_dev *dev = (struct eth_dev *) net->priv;
2039 struct usb_request *resp;
2041 /* in case RNDIS calls this after disconnect */
2042 if (!dev->status_ep) {
2043 DEBUG (dev, "status ENODEV\n");
2047 /* Allocate memory for notification ie. ACK */
2048 resp = usb_ep_alloc_request (dev->status_ep, GFP_ATOMIC);
2050 DEBUG (dev, "status ENOMEM\n");
2054 resp->buf = usb_ep_alloc_buffer (dev->status_ep, 8,
2055 &resp->dma, GFP_ATOMIC);
2057 DEBUG (dev, "status buf ENOMEM\n");
2058 usb_ep_free_request (dev->status_ep, resp);
2062 /* Send RNDIS RESPONSE_AVAILABLE notification;
2063 * CDC_NOTIFY_RESPONSE_AVAILABLE should work too
2066 resp->complete = rndis_response_complete;
2068 *((u32 *) resp->buf) = __constant_cpu_to_le32 (1);
2069 *((u32 *) resp->buf + 1) = __constant_cpu_to_le32 (0);
2071 length = usb_ep_queue (dev->status_ep, resp, GFP_ATOMIC);
2074 rndis_response_complete (dev->status_ep, resp);
2082 static void eth_start (struct eth_dev *dev, int gfp_flags)
2084 DEBUG (dev, "%s\n", __FUNCTION__);
2086 /* fill the rx queue */
2087 rx_fill (dev, gfp_flags);
2089 /* and open the tx floodgates */
2090 atomic_set (&dev->tx_qlen, 0);
2091 netif_wake_queue (dev->net);
2092 #ifdef CONFIG_USB_ETH_RNDIS
2094 rndis_set_param_medium (dev->rndis_config,
2096 BITRATE(dev->gadget));
2097 rndis_send_media_state (dev, 1);
2102 static int eth_open (struct net_device *net)
2104 struct eth_dev *dev = (struct eth_dev *) net->priv;
2106 DEBUG (dev, "%s\n", __FUNCTION__);
2107 if (netif_carrier_ok (dev->net))
2108 eth_start (dev, GFP_KERNEL);
2112 static int eth_stop (struct net_device *net)
2114 struct eth_dev *dev = (struct eth_dev *) net->priv;
2116 VDEBUG (dev, "%s\n", __FUNCTION__);
2117 netif_stop_queue (net);
2119 DEBUG (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
2120 dev->stats.rx_packets, dev->stats.tx_packets,
2121 dev->stats.rx_errors, dev->stats.tx_errors
2124 /* ensure there are no more active requests */
2125 if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
2126 usb_ep_disable (dev->in_ep);
2127 usb_ep_disable (dev->out_ep);
2128 if (netif_carrier_ok (dev->net)) {
2129 DEBUG (dev, "host still using in/out endpoints\n");
2130 // FIXME idiom may leave toggle wrong here
2131 usb_ep_enable (dev->in_ep, dev->in);
2132 usb_ep_enable (dev->out_ep, dev->out);
2134 if (dev->status_ep) {
2135 usb_ep_disable (dev->status_ep);
2136 usb_ep_enable (dev->status_ep, dev->status);
2140 #ifdef CONFIG_USB_ETH_RNDIS
2142 rndis_set_param_medium (dev->rndis_config,
2143 NDIS_MEDIUM_802_3, 0);
2144 rndis_send_media_state (dev, 0);
2151 /*-------------------------------------------------------------------------*/
2154 eth_unbind (struct usb_gadget *gadget)
2156 struct eth_dev *dev = get_gadget_data (gadget);
2158 DEBUG (dev, "unbind\n");
2159 #ifdef CONFIG_USB_ETH_RNDIS
2160 rndis_deregister (dev->rndis_config);
2164 /* we've already been disconnected ... no i/o is active */
2166 usb_ep_free_buffer (gadget->ep0,
2167 dev->req->buf, dev->req->dma,
2169 usb_ep_free_request (gadget->ep0, dev->req);
2173 unregister_netdev (dev->net);
2174 free_netdev(dev->net);
2176 /* assuming we used keventd, it must quiesce too */
2177 flush_scheduled_work ();
2178 set_gadget_data (gadget, 0);
2182 eth_bind (struct usb_gadget *gadget)
2184 struct eth_dev *dev;
2185 struct net_device *net;
2186 u8 cdc = 1, zlp = 1, rndis = 1;
2188 int status = -ENOMEM;
2190 /* these flags are only ever cleared; compiler take note */
2191 #ifndef DEV_CONFIG_CDC
2194 #ifndef CONFIG_USB_ETH_RNDIS
2198 /* Because most host side USB stacks handle CDC Ethernet, that
2199 * standard protocol is _strongly_ preferred for interop purposes.
2200 * (By everyone except Microsoft.)
2202 if (gadget_is_net2280 (gadget)) {
2203 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0201);
2204 } else if (gadget_is_dummy (gadget)) {
2205 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0202);
2206 } else if (gadget_is_pxa (gadget)) {
2207 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0203);
2208 /* pxa doesn't support altsettings */
2210 } else if (gadget_is_sh(gadget)) {
2211 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0204);
2212 /* sh doesn't support multiple interfaces or configs */
2215 } else if (gadget_is_sa1100 (gadget)) {
2216 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0205);
2217 /* hardware can't write zlps */
2219 /* sa1100 CAN do CDC, without status endpoint ... we use
2220 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2223 } else if (gadget_is_goku (gadget)) {
2224 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0206);
2225 } else if (gadget_is_mq11xx (gadget)) {
2226 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0207);
2227 } else if (gadget_is_omap (gadget)) {
2228 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0208);
2230 /* can't assume CDC works. don't want to default to
2231 * anything less functional on CDC-capable hardware,
2232 * so we fail in this case.
2234 dev_err (&gadget->dev,
2235 "controller '%s' not recognized\n",
2239 snprintf (manufacturer, sizeof manufacturer,
2240 UTS_SYSNAME " " UTS_RELEASE "/%s",
2243 /* CDC subset ... recognized by Linux since 2.4.10, but Windows
2244 * drivers aren't widely available.
2247 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2248 device_desc.idVendor =
2249 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2250 device_desc.idProduct =
2251 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2254 /* If there's an RNDIS configuration, that's what Windows wants to
2255 * be using ... so use these product IDs here and in the "linux.inf"
2256 * needed to install MSFT drivers. Current Linux kernels will use
2257 * the second configuration if it's CDC Ethernet, and need some help
2258 * to choose the right configuration otherwise.
2261 device_desc.idVendor =
2262 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2263 device_desc.idProduct =
2264 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2265 snprintf (product_desc, sizeof product_desc,
2266 "RNDIS/%s", driver_desc);
2269 /* support optional vendor/distro customization */
2272 dev_err (&gadget->dev, "idVendor needs idProduct!\n");
2275 device_desc.idVendor = cpu_to_le16(idVendor);
2276 device_desc.idProduct = cpu_to_le16(idProduct);
2278 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2281 strlcpy (manufacturer, iManufacturer, sizeof manufacturer);
2283 strlcpy (product_desc, iProduct, sizeof product_desc);
2285 /* all we really need is bulk IN/OUT */
2286 usb_ep_autoconfig_reset (gadget);
2287 ep = usb_ep_autoconfig (gadget, &fs_source_desc);
2290 dev_err (&gadget->dev,
2291 "can't autoconfigure on %s\n",
2295 EP_IN_NAME = ep->name;
2296 ep->driver_data = ep; /* claim */
2298 ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
2301 EP_OUT_NAME = ep->name;
2302 ep->driver_data = ep; /* claim */
2304 /* CDC Ethernet control interface doesn't require a status endpoint.
2305 * Since some hosts expect one, try to allocate one anyway.
2308 ep = usb_ep_autoconfig (gadget, &fs_status_desc);
2310 EP_STATUS_NAME = ep->name;
2311 ep->driver_data = ep; /* claim */
2313 dev_err (&gadget->dev,
2314 "can't run RNDIS on %s\n",
2317 #ifdef DEV_CONFIG_CDC
2319 control_intf.bNumEndpoints = 0;
2320 /* FIXME remove endpoint from descriptor list */
2325 /* one config: cdc, else minimal subset */
2327 eth_config.bNumInterfaces = 1;
2328 eth_config.iConfiguration = STRING_SUBSET;
2329 fs_subset_descriptors();
2330 hs_subset_descriptors();
2333 /* For now RNDIS is always a second config */
2335 device_desc.bNumConfigurations = 2;
2337 #ifdef CONFIG_USB_GADGET_DUALSPEED
2339 dev_qualifier.bNumConfigurations = 2;
2341 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2343 /* assumes ep0 uses the same value for both speeds ... */
2344 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2346 /* and that all endpoints are dual-speed */
2347 hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
2348 hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
2349 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2351 hs_status_desc.bEndpointAddress =
2352 fs_status_desc.bEndpointAddress;
2354 #endif /* DUALSPEED */
2356 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
2357 usb_gadget_set_selfpowered (gadget);
2359 net = alloc_etherdev (sizeof *dev);
2363 spin_lock_init (&dev->lock);
2364 INIT_WORK (&dev->work, eth_work, dev);
2365 INIT_LIST_HEAD (&dev->tx_reqs);
2366 INIT_LIST_HEAD (&dev->rx_reqs);
2368 /* network device setup */
2370 SET_MODULE_OWNER (net);
2371 strcpy (net->name, "usb%d");
2375 /* FIXME make these addresses configurable with module params.
2376 * also the manufacturer and product strings.
2379 /* one random address for the gadget device ... both of these could
2380 * reasonably come from an id prom or a module parameter.
2382 random_ether_addr(net->dev_addr);
2384 /* ... another address for the host, on the other end of the
2385 * link, gets exported through CDC (see CDC spec table 41)
2389 random_ether_addr(dev->host_mac);
2390 #ifdef DEV_CONFIG_CDC
2391 snprintf (ethaddr, sizeof ethaddr, "%02X%02X%02X%02X%02X%02X",
2392 dev->host_mac [0], dev->host_mac [1],
2393 dev->host_mac [2], dev->host_mac [3],
2394 dev->host_mac [4], dev->host_mac [5]);
2399 status = rndis_init();
2401 dev_err (&gadget->dev, "can't init RNDIS, %d\n",
2407 net->change_mtu = eth_change_mtu;
2408 net->get_stats = eth_get_stats;
2409 net->hard_start_xmit = eth_start_xmit;
2410 net->open = eth_open;
2411 net->stop = eth_stop;
2412 // watchdog_timeo, tx_timeout ...
2413 // set_multicast_list
2414 net->do_ioctl = eth_ioctl;
2416 /* preallocate control response and buffer */
2417 dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
2420 dev->req->complete = eth_setup_complete;
2421 dev->req->buf = usb_ep_alloc_buffer (gadget->ep0, USB_BUFSIZ,
2422 &dev->req->dma, GFP_KERNEL);
2423 if (!dev->req->buf) {
2424 usb_ep_free_request (gadget->ep0, dev->req);
2428 /* finish hookup to lower layer ... */
2429 dev->gadget = gadget;
2430 set_gadget_data (gadget, dev);
2431 gadget->ep0->driver_data = dev;
2433 /* two kinds of host-initiated state changes:
2434 * - iff DATA transfer is active, carrier is "on"
2435 * - tx queueing enabled if open *and* carrier is "on"
2437 netif_stop_queue (dev->net);
2438 netif_carrier_off (dev->net);
2440 // SET_NETDEV_DEV (dev->net, &gadget->dev);
2441 status = register_netdev (dev->net);
2445 INFO (dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
2446 INFO (dev, "using %s, OUT %s IN %s%s%s\n", gadget->name,
2447 EP_OUT_NAME, EP_IN_NAME,
2448 EP_STATUS_NAME ? " STATUS " : "",
2449 EP_STATUS_NAME ? EP_STATUS_NAME : ""
2451 INFO (dev, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2452 net->dev_addr [0], net->dev_addr [1],
2453 net->dev_addr [2], net->dev_addr [3],
2454 net->dev_addr [4], net->dev_addr [5]);
2457 INFO (dev, "HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2458 dev->host_mac [0], dev->host_mac [1],
2459 dev->host_mac [2], dev->host_mac [3],
2460 dev->host_mac [4], dev->host_mac [5]);
2462 #ifdef CONFIG_USB_ETH_RNDIS
2466 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2468 dev->rndis_config = rndis_register (rndis_control_ack);
2469 if (dev->rndis_config < 0) {
2471 unregister_netdev (dev->net);
2476 /* these set up a lot of the OIDs that RNDIS needs */
2477 rndis_set_host_mac (dev->rndis_config, dev->host_mac);
2478 if (rndis_set_param_dev (dev->rndis_config, dev->net,
2481 if (rndis_set_param_vendor (dev->rndis_config, vendorID,
2484 if (rndis_set_param_medium (dev->rndis_config,
2488 INFO (dev, "RNDIS ready\n");
2495 dev_dbg(&gadget->dev, "register_netdev failed, %d\n", status);
2497 eth_unbind (gadget);
2501 /*-------------------------------------------------------------------------*/
2503 static struct usb_gadget_driver eth_driver = {
2504 #ifdef CONFIG_USB_GADGET_DUALSPEED
2505 .speed = USB_SPEED_HIGH,
2507 .speed = USB_SPEED_FULL,
2509 .function = (char *) driver_desc,
2511 .unbind = eth_unbind,
2514 .disconnect = eth_disconnect,
2517 .name = (char *) shortname,
2524 MODULE_DESCRIPTION (DRIVER_DESC);
2525 MODULE_AUTHOR ("David Brownell, Benedikt Spanger");
2526 MODULE_LICENSE ("GPL");
2529 static int __init init (void)
2531 return usb_gadget_register_driver (ð_driver);
2535 static void __exit cleanup (void)
2537 usb_gadget_unregister_driver (ð_driver);
2539 module_exit (cleanup);