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>
43 #include <linux/ctype.h>
45 #include <asm/byteorder.h>
48 #include <asm/system.h>
49 #include <asm/uaccess.h>
50 #include <asm/unaligned.h>
52 #include <linux/usb_ch9.h>
53 #include <linux/usb_gadget.h>
55 #include <linux/random.h>
56 #include <linux/netdevice.h>
57 #include <linux/etherdevice.h>
58 #include <linux/ethtool.h>
60 #include "gadget_chips.h"
62 /*-------------------------------------------------------------------------*/
65 * Ethernet gadget driver -- with CDC and non-CDC options
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 "St Patrick's Day 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_MQ11XX
226 #define DEV_CONFIG_CDC
229 #ifdef CONFIG_USB_GADGET_OMAP
230 #define DEV_CONFIG_CDC
234 /* For CDC-incapable hardware, choose the simple cdc subset.
235 * Anything that talks bulk (without notable bugs) can do this.
237 #ifdef CONFIG_USB_GADGET_PXA
238 #define DEV_CONFIG_SUBSET
241 #ifdef CONFIG_USB_GADGET_SH
242 #define DEV_CONFIG_SUBSET
245 #ifdef CONFIG_USB_GADGET_SA1100
246 /* use non-CDC for backwards compatibility */
247 #define DEV_CONFIG_SUBSET
251 /*-------------------------------------------------------------------------*/
253 #define DEFAULT_QLEN 2 /* double buffering by default */
255 #ifdef CONFIG_USB_GADGET_DUALSPEED
257 static unsigned qmult = 5;
258 module_param (qmult, uint, S_IRUGO|S_IWUSR);
261 /* for dual-speed hardware, use deeper queues at highspeed */
262 #define qlen(gadget) \
263 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
265 /* also defer IRQs on highspeed TX */
266 #define TX_DELAY qmult
268 #define BITRATE(g) ((g->speed == USB_SPEED_HIGH) ? 4800000 : 120000)
270 #else /* full speed (low speed doesn't do bulk) */
271 #define qlen(gadget) DEFAULT_QLEN
273 #define BITRATE(g) (12000)
277 /*-------------------------------------------------------------------------*/
279 #define xprintk(d,level,fmt,args...) \
280 printk(level "%s: " fmt , (d)->net->name , ## args)
284 #define DEBUG(dev,fmt,args...) \
285 xprintk(dev , KERN_DEBUG , fmt , ## args)
287 #define DEBUG(dev,fmt,args...) \
294 #define VDEBUG(dev,fmt,args...) \
298 #define ERROR(dev,fmt,args...) \
299 xprintk(dev , KERN_ERR , fmt , ## args)
300 #define WARN(dev,fmt,args...) \
301 xprintk(dev , KERN_WARNING , fmt , ## args)
302 #define INFO(dev,fmt,args...) \
303 xprintk(dev , KERN_INFO , fmt , ## args)
305 /*-------------------------------------------------------------------------*/
307 /* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
308 * ep0 implementation: descriptors, config management, setup().
309 * also optional class-specific notification interrupt transfer.
313 * DESCRIPTORS ... most are static, but strings and (full) configuration
314 * descriptors are built on demand. For now we do either full CDC, or
315 * our simple subset, with RNDIS as an optional second configuration.
317 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
318 * the class descriptors match a modem (they're ignored; it's really just
319 * Ethernet functionality), they don't need the NOP altsetting, and the
320 * status transfer endpoint isn't optional.
323 #define STRING_MANUFACTURER 1
324 #define STRING_PRODUCT 2
325 #define STRING_ETHADDR 3
326 #define STRING_DATA 4
327 #define STRING_CONTROL 5
328 #define STRING_RNDIS_CONTROL 6
330 #define STRING_SUBSET 8
331 #define STRING_RNDIS 9
333 #define USB_BUFSIZ 256 /* holds our biggest descriptor */
336 * This device advertises one configuration, eth_config, unless RNDIS
337 * is enabled (rndis_config) on hardware supporting at least two configs.
339 * NOTE: Controllers like superh_udc should probably be able to use
340 * an RNDIS-only configuration.
343 #define DEV_CONFIG_VALUE 1 /* cdc or subset */
344 #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
346 static struct usb_device_descriptor
348 .bLength = sizeof device_desc,
349 .bDescriptorType = USB_DT_DEVICE,
351 .bcdUSB = __constant_cpu_to_le16 (0x0200),
353 .bDeviceClass = USB_CLASS_COMM,
354 .bDeviceSubClass = 0,
355 .bDeviceProtocol = 0,
357 .idVendor = __constant_cpu_to_le16 (CDC_VENDOR_NUM),
358 .idProduct = __constant_cpu_to_le16 (CDC_PRODUCT_NUM),
359 .iManufacturer = STRING_MANUFACTURER,
360 .iProduct = STRING_PRODUCT,
361 .bNumConfigurations = 1,
364 static struct usb_config_descriptor
366 .bLength = sizeof eth_config,
367 .bDescriptorType = USB_DT_CONFIG,
369 /* compute wTotalLength on the fly */
371 .bConfigurationValue = DEV_CONFIG_VALUE,
372 .iConfiguration = STRING_CDC,
373 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
377 #ifdef CONFIG_USB_ETH_RNDIS
378 static const struct usb_config_descriptor
380 .bLength = sizeof rndis_config,
381 .bDescriptorType = USB_DT_CONFIG,
383 /* compute wTotalLength on the fly */
385 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
386 .iConfiguration = STRING_RNDIS,
387 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
393 * Compared to the simple CDC subset, the full CDC Ethernet model adds
394 * three class descriptors, two interface descriptors, optional status
395 * endpoint. Both have a "data" interface and two bulk endpoints.
396 * There are also differences in how control requests are handled.
398 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of
399 * the CDC-ACM (modem) spec.
402 #ifdef DEV_CONFIG_CDC
403 static struct usb_interface_descriptor
405 .bLength = sizeof control_intf,
406 .bDescriptorType = USB_DT_INTERFACE,
408 .bInterfaceNumber = 0,
409 /* status endpoint is optional; this may be patched later */
411 .bInterfaceClass = USB_CLASS_COMM,
412 .bInterfaceSubClass = 6, /* ethernet control model */
413 .bInterfaceProtocol = 0,
414 .iInterface = STRING_CONTROL,
418 #ifdef CONFIG_USB_ETH_RNDIS
419 static const struct usb_interface_descriptor
420 rndis_control_intf = {
421 .bLength = sizeof rndis_control_intf,
422 .bDescriptorType = USB_DT_INTERFACE,
424 .bInterfaceNumber = 0,
426 .bInterfaceClass = USB_CLASS_COMM,
427 .bInterfaceSubClass = 2, /* abstract control model */
428 .bInterfaceProtocol = 0xff, /* vendor specific */
429 .iInterface = STRING_RNDIS_CONTROL,
433 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
435 /* "Header Functional Descriptor" from CDC spec 5.2.3.1 */
439 u8 bDescriptorSubType;
442 } __attribute__ ((packed));
444 static const struct header_desc header_desc = {
445 .bLength = sizeof header_desc,
446 .bDescriptorType = USB_DT_CS_INTERFACE,
447 .bDescriptorSubType = 0,
449 .bcdCDC = __constant_cpu_to_le16 (0x0110),
452 /* "Union Functional Descriptor" from CDC spec 5.2.3.8 */
456 u8 bDescriptorSubType;
458 u8 bMasterInterface0;
460 /* ... and there could be other slave interfaces */
461 } __attribute__ ((packed));
463 static const struct union_desc union_desc = {
464 .bLength = sizeof union_desc,
465 .bDescriptorType = USB_DT_CS_INTERFACE,
466 .bDescriptorSubType = 6,
468 .bMasterInterface0 = 0, /* index of control interface */
469 .bSlaveInterface0 = 1, /* index of DATA interface */
472 #endif /* CDC || RNDIS */
474 #ifdef CONFIG_USB_ETH_RNDIS
476 /* "Call Management Descriptor" from CDC spec 5.2.3.3 */
477 struct call_mgmt_descriptor {
480 u8 bDescriptorSubType;
484 } __attribute__ ((packed));
486 static const struct call_mgmt_descriptor call_mgmt_descriptor = {
487 .bLength = sizeof call_mgmt_descriptor,
488 .bDescriptorType = USB_DT_CS_INTERFACE,
489 .bDescriptorSubType = 0x01,
491 .bmCapabilities = 0x00,
492 .bDataInterface = 0x01,
496 /* "Abstract Control Management Descriptor" from CDC spec 5.2.3.4 */
497 struct acm_descriptor {
500 u8 bDescriptorSubType;
503 } __attribute__ ((packed));
505 static struct acm_descriptor acm_descriptor = {
506 .bLength = sizeof acm_descriptor,
507 .bDescriptorType = USB_DT_CS_INTERFACE,
508 .bDescriptorSubType = 0x02,
510 .bmCapabilities = 0X00,
515 #ifdef DEV_CONFIG_CDC
517 /* "Ethernet Networking Functional Descriptor" from CDC spec 5.2.3.16 */
521 u8 bDescriptorSubType;
524 u32 bmEthernetStatistics;
526 u16 wNumberMCFilters;
527 u8 bNumberPowerFilters;
528 } __attribute__ ((packed));
530 static const struct ether_desc ether_desc = {
531 .bLength = sizeof ether_desc,
532 .bDescriptorType = USB_DT_CS_INTERFACE,
533 .bDescriptorSubType = 0x0f,
535 /* this descriptor actually adds value, surprise! */
536 .iMACAddress = STRING_ETHADDR,
537 .bmEthernetStatistics = __constant_cpu_to_le32 (0), /* no statistics */
538 .wMaxSegmentSize = __constant_cpu_to_le16 (ETH_FRAME_LEN),
539 .wNumberMCFilters = __constant_cpu_to_le16 (0),
540 .bNumberPowerFilters = 0,
545 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
547 /* include the status endpoint if we can, even where it's optional.
548 * use small wMaxPacketSize, since many "interrupt" endpoints have
549 * very small fifos and it's no big deal if CDC_NOTIFY_SPEED_CHANGE
550 * takes two packets. also default to a big transfer interval, to
551 * waste less bandwidth.
553 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
554 * if they ignore the connect/disconnect notifications that real aether
555 * can provide. more advanced cdc configurations might want to support
556 * encapsulated commands (vendor-specific, using control-OUT).
558 * RNDIS requires the status endpoint, since it uses that encapsulation
559 * mechanism for its funky RPC scheme.
562 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
563 #define STATUS_BYTECOUNT 8 /* 8 byte header + data */
565 static struct usb_endpoint_descriptor
567 .bLength = USB_DT_ENDPOINT_SIZE,
568 .bDescriptorType = USB_DT_ENDPOINT,
570 .bEndpointAddress = USB_DIR_IN,
571 .bmAttributes = USB_ENDPOINT_XFER_INT,
572 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
573 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
577 #ifdef DEV_CONFIG_CDC
579 /* the default data interface has no endpoints ... */
581 static const struct usb_interface_descriptor
583 .bLength = sizeof data_nop_intf,
584 .bDescriptorType = USB_DT_INTERFACE,
586 .bInterfaceNumber = 1,
587 .bAlternateSetting = 0,
589 .bInterfaceClass = USB_CLASS_CDC_DATA,
590 .bInterfaceSubClass = 0,
591 .bInterfaceProtocol = 0,
594 /* ... but the "real" data interface has two bulk endpoints */
596 static const struct usb_interface_descriptor
598 .bLength = sizeof data_intf,
599 .bDescriptorType = USB_DT_INTERFACE,
601 .bInterfaceNumber = 1,
602 .bAlternateSetting = 1,
604 .bInterfaceClass = USB_CLASS_CDC_DATA,
605 .bInterfaceSubClass = 0,
606 .bInterfaceProtocol = 0,
607 .iInterface = STRING_DATA,
612 #ifdef CONFIG_USB_ETH_RNDIS
614 /* RNDIS doesn't activate by changing to the "real" altsetting */
616 static const struct usb_interface_descriptor
618 .bLength = sizeof rndis_data_intf,
619 .bDescriptorType = USB_DT_INTERFACE,
621 .bInterfaceNumber = 1,
622 .bAlternateSetting = 0,
624 .bInterfaceClass = USB_CLASS_CDC_DATA,
625 .bInterfaceSubClass = 0,
626 .bInterfaceProtocol = 0,
627 .iInterface = STRING_DATA,
632 #ifdef DEV_CONFIG_SUBSET
635 * "Simple" CDC-subset option is a simple vendor-neutral model that most
636 * full speed controllers can handle: one interface, two bulk endpoints.
639 static const struct usb_interface_descriptor
641 .bLength = sizeof subset_data_intf,
642 .bDescriptorType = USB_DT_INTERFACE,
644 .bInterfaceNumber = 0,
645 .bAlternateSetting = 0,
647 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
648 .bInterfaceSubClass = 0,
649 .bInterfaceProtocol = 0,
650 .iInterface = STRING_DATA,
656 static struct usb_endpoint_descriptor
658 .bLength = USB_DT_ENDPOINT_SIZE,
659 .bDescriptorType = USB_DT_ENDPOINT,
661 .bEndpointAddress = USB_DIR_IN,
662 .bmAttributes = USB_ENDPOINT_XFER_BULK,
665 static struct usb_endpoint_descriptor
667 .bLength = USB_DT_ENDPOINT_SIZE,
668 .bDescriptorType = USB_DT_ENDPOINT,
670 .bEndpointAddress = USB_DIR_OUT,
671 .bmAttributes = USB_ENDPOINT_XFER_BULK,
674 static const struct usb_descriptor_header *fs_eth_function [10] = {
675 #ifdef DEV_CONFIG_CDC
676 /* "cdc" mode descriptors */
677 (struct usb_descriptor_header *) &control_intf,
678 (struct usb_descriptor_header *) &header_desc,
679 (struct usb_descriptor_header *) &union_desc,
680 (struct usb_descriptor_header *) ðer_desc,
681 /* NOTE: status endpoint may need to be removed */
682 (struct usb_descriptor_header *) &fs_status_desc,
683 /* data interface, with altsetting */
684 (struct usb_descriptor_header *) &data_nop_intf,
685 (struct usb_descriptor_header *) &data_intf,
686 (struct usb_descriptor_header *) &fs_source_desc,
687 (struct usb_descriptor_header *) &fs_sink_desc,
689 #endif /* DEV_CONFIG_CDC */
692 static inline void __init fs_subset_descriptors(void)
694 #ifdef DEV_CONFIG_SUBSET
695 fs_eth_function[0] = (struct usb_descriptor_header *) &subset_data_intf;
696 fs_eth_function[1] = (struct usb_descriptor_header *) &fs_source_desc;
697 fs_eth_function[2] = (struct usb_descriptor_header *) &fs_sink_desc;
698 fs_eth_function[3] = 0;
700 fs_eth_function[0] = 0;
704 #ifdef CONFIG_USB_ETH_RNDIS
705 static const struct usb_descriptor_header *fs_rndis_function [] = {
706 /* control interface matches ACM, not Ethernet */
707 (struct usb_descriptor_header *) &rndis_control_intf,
708 (struct usb_descriptor_header *) &header_desc,
709 (struct usb_descriptor_header *) &call_mgmt_descriptor,
710 (struct usb_descriptor_header *) &acm_descriptor,
711 (struct usb_descriptor_header *) &union_desc,
712 (struct usb_descriptor_header *) &fs_status_desc,
713 /* data interface has no altsetting */
714 (struct usb_descriptor_header *) &rndis_data_intf,
715 (struct usb_descriptor_header *) &fs_source_desc,
716 (struct usb_descriptor_header *) &fs_sink_desc,
721 #ifdef CONFIG_USB_GADGET_DUALSPEED
724 * usb 2.0 devices need to expose both high speed and full speed
725 * descriptors, unless they only run at full speed.
728 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
729 static struct usb_endpoint_descriptor
731 .bLength = USB_DT_ENDPOINT_SIZE,
732 .bDescriptorType = USB_DT_ENDPOINT,
734 .bmAttributes = USB_ENDPOINT_XFER_INT,
735 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
736 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
738 #endif /* DEV_CONFIG_CDC */
740 static struct usb_endpoint_descriptor
742 .bLength = USB_DT_ENDPOINT_SIZE,
743 .bDescriptorType = USB_DT_ENDPOINT,
745 .bmAttributes = USB_ENDPOINT_XFER_BULK,
746 .wMaxPacketSize = __constant_cpu_to_le16 (512),
749 static struct usb_endpoint_descriptor
751 .bLength = USB_DT_ENDPOINT_SIZE,
752 .bDescriptorType = USB_DT_ENDPOINT,
754 .bmAttributes = USB_ENDPOINT_XFER_BULK,
755 .wMaxPacketSize = __constant_cpu_to_le16 (512),
758 static struct usb_qualifier_descriptor
760 .bLength = sizeof dev_qualifier,
761 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
763 .bcdUSB = __constant_cpu_to_le16 (0x0200),
764 .bDeviceClass = USB_CLASS_COMM,
766 .bNumConfigurations = 1,
769 static const struct usb_descriptor_header *hs_eth_function [10] = {
770 #ifdef DEV_CONFIG_CDC
771 /* "cdc" mode descriptors */
772 (struct usb_descriptor_header *) &control_intf,
773 (struct usb_descriptor_header *) &header_desc,
774 (struct usb_descriptor_header *) &union_desc,
775 (struct usb_descriptor_header *) ðer_desc,
776 /* NOTE: status endpoint may need to be removed */
777 (struct usb_descriptor_header *) &hs_status_desc,
778 /* data interface, with altsetting */
779 (struct usb_descriptor_header *) &data_nop_intf,
780 (struct usb_descriptor_header *) &data_intf,
781 (struct usb_descriptor_header *) &hs_source_desc,
782 (struct usb_descriptor_header *) &hs_sink_desc,
784 #endif /* DEV_CONFIG_CDC */
787 static inline void __init hs_subset_descriptors(void)
789 #ifdef DEV_CONFIG_SUBSET
790 hs_eth_function[0] = (struct usb_descriptor_header *) &subset_data_intf;
791 hs_eth_function[1] = (struct usb_descriptor_header *) &fs_source_desc;
792 hs_eth_function[2] = (struct usb_descriptor_header *) &fs_sink_desc;
793 hs_eth_function[3] = 0;
795 hs_eth_function[0] = 0;
799 #ifdef CONFIG_USB_ETH_RNDIS
800 static const struct usb_descriptor_header *hs_rndis_function [] = {
801 /* control interface matches ACM, not Ethernet */
802 (struct usb_descriptor_header *) &rndis_control_intf,
803 (struct usb_descriptor_header *) &header_desc,
804 (struct usb_descriptor_header *) &call_mgmt_descriptor,
805 (struct usb_descriptor_header *) &acm_descriptor,
806 (struct usb_descriptor_header *) &union_desc,
807 (struct usb_descriptor_header *) &hs_status_desc,
808 /* data interface has no altsetting */
809 (struct usb_descriptor_header *) &rndis_data_intf,
810 (struct usb_descriptor_header *) &hs_source_desc,
811 (struct usb_descriptor_header *) &hs_sink_desc,
817 /* maxpacket and other transfer characteristics vary by speed. */
818 #define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
822 /* if there's no high speed support, maxpacket doesn't change. */
823 #define ep_desc(g,hs,fs) fs
825 static inline void __init hs_subset_descriptors(void)
829 #endif /* !CONFIG_USB_GADGET_DUALSPEED */
831 /*-------------------------------------------------------------------------*/
833 /* descriptors that are built on-demand */
835 static char manufacturer [40];
836 static char product_desc [40] = DRIVER_DESC;
838 #ifdef DEV_CONFIG_CDC
839 /* address that the host will use ... usually assigned at random */
840 static char ethaddr [2 * ETH_ALEN + 1];
843 /* static strings, in iso 8859/1 */
844 static struct usb_string strings [] = {
845 { STRING_MANUFACTURER, manufacturer, },
846 { STRING_PRODUCT, product_desc, },
847 { STRING_DATA, "Ethernet Data", },
848 #ifdef DEV_CONFIG_CDC
849 { STRING_CDC, "CDC Ethernet", },
850 { STRING_ETHADDR, ethaddr, },
851 { STRING_CONTROL, "CDC Communications Control", },
853 #ifdef DEV_CONFIG_SUBSET
854 { STRING_SUBSET, "CDC Ethernet Subset", },
856 #ifdef CONFIG_USB_ETH_RNDIS
857 { STRING_RNDIS, "RNDIS", },
858 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
860 { } /* end of list */
863 static struct usb_gadget_strings stringtab = {
864 .language = 0x0409, /* en-us */
869 * one config, two interfaces: control, data.
870 * complications: class descriptors, and an altsetting.
873 config_buf (enum usb_device_speed speed, u8 *buf, u8 type, unsigned index)
876 #ifdef CONFIG_USB_GADGET_DUALSPEED
877 int hs = (speed == USB_SPEED_HIGH);
879 if (type == USB_DT_OTHER_SPEED_CONFIG)
881 #define which_config(t) (hs ? & t ## _config : & t ## _config)
882 #define which_fn(t) (hs ? & hs_ ## t ## _function : & fs_ ## t ## _function)
884 #define which_config(t) (& t ## _config)
885 #define which_fn(t) (& fs_ ## t ## _function)
888 if (index >= device_desc.bNumConfigurations)
891 #ifdef CONFIG_USB_ETH_RNDIS
892 /* list the RNDIS config first, to make Microsoft's drivers
893 * happy. DOCSIS 1.0 needs this too.
895 if (device_desc.bNumConfigurations == 2 && index == 0)
896 len = usb_gadget_config_buf (which_config (rndis), buf,
897 USB_BUFSIZ, (const struct usb_descriptor_header **)
901 len = usb_gadget_config_buf (which_config (eth), buf,
902 USB_BUFSIZ, (const struct usb_descriptor_header **)
906 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
910 /*-------------------------------------------------------------------------*/
912 static void eth_start (struct eth_dev *dev, int gfp_flags);
913 static int alloc_requests (struct eth_dev *dev, unsigned n, int gfp_flags);
915 #ifdef DEV_CONFIG_CDC
916 static inline int ether_alt_ep_setup (struct eth_dev *dev, struct usb_ep *ep)
918 const struct usb_endpoint_descriptor *d;
920 /* With CDC, the host isn't allowed to use these two data
921 * endpoints in the default altsetting for the interface.
922 * so we don't activate them yet. Reset from SET_INTERFACE.
924 * Strictly speaking RNDIS should work the same: activation is
925 * a side effect of setting a packet filter. Deactivation is
926 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
929 /* one endpoint writes data back IN to the host */
930 if (strcmp (ep->name, EP_IN_NAME) == 0) {
931 d = ep_desc (dev->gadget, &hs_source_desc, &fs_source_desc);
932 ep->driver_data = dev;
936 /* one endpoint just reads OUT packets */
937 } else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
938 d = ep_desc (dev->gadget, &hs_sink_desc, &fs_sink_desc);
939 ep->driver_data = dev;
943 /* optional status/notification endpoint */
944 } else if (EP_STATUS_NAME &&
945 strcmp (ep->name, EP_STATUS_NAME) == 0) {
948 d = ep_desc (dev->gadget, &hs_status_desc, &fs_status_desc);
949 result = usb_ep_enable (ep, d);
953 ep->driver_data = dev;
961 #if defined(DEV_CONFIG_SUBSET) || defined(CONFIG_USB_ETH_RNDIS)
962 static inline int ether_ep_setup (struct eth_dev *dev, struct usb_ep *ep)
965 const struct usb_endpoint_descriptor *d;
967 /* CDC subset is simpler: if the device is there,
968 * it's live with rx and tx endpoints.
970 * Do this as a shortcut for RNDIS too.
973 /* one endpoint writes data back IN to the host */
974 if (strcmp (ep->name, EP_IN_NAME) == 0) {
975 d = ep_desc (dev->gadget, &hs_source_desc, &fs_source_desc);
976 result = usb_ep_enable (ep, d);
980 ep->driver_data = dev;
984 /* one endpoint just reads OUT packets */
985 } else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
986 d = ep_desc (dev->gadget, &hs_sink_desc, &fs_sink_desc);
987 result = usb_ep_enable (ep, d);
991 ep->driver_data = dev;
1001 set_ether_config (struct eth_dev *dev, int gfp_flags)
1005 struct usb_gadget *gadget = dev->gadget;
1007 gadget_for_each_ep (ep, gadget) {
1008 #ifdef DEV_CONFIG_CDC
1009 if (!dev->rndis && dev->cdc) {
1010 result = ether_alt_ep_setup (dev, ep);
1016 #ifdef CONFIG_USB_ETH_RNDIS
1017 if (dev->rndis && strcmp (ep->name, EP_STATUS_NAME) == 0) {
1018 const struct usb_endpoint_descriptor *d;
1019 d = ep_desc (gadget, &hs_status_desc, &fs_status_desc);
1020 result = usb_ep_enable (ep, d);
1022 ep->driver_data = dev;
1023 dev->status_ep = ep;
1031 #if defined(DEV_CONFIG_SUBSET) || defined(CONFIG_USB_ETH_RNDIS)
1032 result = ether_ep_setup (dev, ep);
1039 ERROR (dev, "can't enable %s, result %d\n", ep->name, result);
1042 if (!result && (!dev->in_ep || !dev->out_ep))
1046 result = alloc_requests (dev, qlen (gadget), gfp_flags);
1048 /* on error, disable any endpoints */
1050 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
1052 (void) usb_ep_disable (dev->status_ep);
1056 #if defined(DEV_CONFIG_SUBSET) || defined(CONFIG_USB_ETH_RNDIS)
1057 if (dev->rndis || !dev->cdc) {
1059 (void) usb_ep_disable (dev->in_ep);
1061 (void) usb_ep_disable (dev->out_ep);
1070 /* activate non-CDC configs right away
1071 * this isn't strictly according to the RNDIS spec
1073 #if defined(DEV_CONFIG_SUBSET) || defined(CONFIG_USB_ETH_RNDIS)
1074 if (dev->rndis || !dev->cdc) {
1075 netif_carrier_on (dev->net);
1076 if (netif_running (dev->net)) {
1077 spin_unlock (&dev->lock);
1078 eth_start (dev, GFP_ATOMIC);
1079 spin_lock (&dev->lock);
1085 DEBUG (dev, "qlen %d\n", qlen (gadget));
1087 /* caller is responsible for cleanup on error */
1091 static void eth_reset_config (struct eth_dev *dev)
1093 struct usb_request *req;
1095 if (dev->config == 0)
1098 DEBUG (dev, "%s\n", __FUNCTION__);
1100 netif_stop_queue (dev->net);
1101 netif_carrier_off (dev->net);
1103 /* disable endpoints, forcing (synchronous) completion of
1104 * pending i/o. then free the requests.
1107 usb_ep_disable (dev->in_ep);
1108 while (likely (!list_empty (&dev->tx_reqs))) {
1109 req = container_of (dev->tx_reqs.next,
1110 struct usb_request, list);
1111 list_del (&req->list);
1112 usb_ep_free_request (dev->in_ep, req);
1117 usb_ep_disable (dev->out_ep);
1118 while (likely (!list_empty (&dev->rx_reqs))) {
1119 req = container_of (dev->rx_reqs.next,
1120 struct usb_request, list);
1121 list_del (&req->list);
1122 usb_ep_free_request (dev->out_ep, req);
1127 if (dev->status_ep) {
1128 usb_ep_disable (dev->status_ep);
1134 /* change our operational config. must agree with the code
1135 * that returns config descriptors, and altsetting code.
1138 eth_set_config (struct eth_dev *dev, unsigned number, int gfp_flags)
1141 struct usb_gadget *gadget = dev->gadget;
1143 if (number == dev->config)
1146 if (gadget_is_sa1100 (gadget)
1148 && atomic_read (&dev->tx_qlen) != 0) {
1149 /* tx fifo is full, but we can't clear it...*/
1150 INFO (dev, "can't change configurations\n");
1153 eth_reset_config (dev);
1155 /* default: pass all packets, no multicast filtering */
1156 dev->cdc_filter = 0x000f;
1159 case DEV_CONFIG_VALUE:
1161 result = set_ether_config (dev, gfp_flags);
1163 #ifdef CONFIG_USB_ETH_RNDIS
1164 case DEV_RNDIS_CONFIG_VALUE:
1166 result = set_ether_config (dev, gfp_flags);
1177 eth_reset_config (dev);
1181 switch (gadget->speed) {
1182 case USB_SPEED_FULL: speed = "full"; break;
1183 #ifdef CONFIG_USB_GADGET_DUALSPEED
1184 case USB_SPEED_HIGH: speed = "high"; break;
1186 default: speed = "?"; break;
1189 dev->config = number;
1190 INFO (dev, "%s speed config #%d: %s, using %s\n",
1191 speed, number, driver_desc,
1196 : "CDC Ethernet Subset"));
1201 /*-------------------------------------------------------------------------*/
1203 /* section 3.8.2 table 11 of the CDC spec lists Ethernet notifications
1204 * section 3.6.2.1 table 5 specifies ACM notifications, accepted by RNDIS
1205 * and RNDIS also defines its own bit-incompatible notifications
1207 #define CDC_NOTIFY_NETWORK_CONNECTION 0x00 /* required; 6.3.1 */
1208 #define CDC_NOTIFY_RESPONSE_AVAILABLE 0x01 /* optional; 6.3.2 */
1209 #define CDC_NOTIFY_SPEED_CHANGE 0x2a /* required; 6.3.8 */
1211 #ifdef DEV_CONFIG_CDC
1213 struct cdc_notification {
1215 u8 bNotificationType;
1220 /* SPEED_CHANGE data looks like this */
1224 static void eth_status_complete (struct usb_ep *ep, struct usb_request *req)
1226 struct cdc_notification *event = req->buf;
1227 int value = req->status;
1228 struct eth_dev *dev = ep->driver_data;
1230 /* issue the second notification if host reads the first */
1231 if (event->bNotificationType == CDC_NOTIFY_NETWORK_CONNECTION
1233 event->bmRequestType = 0xA1;
1234 event->bNotificationType = CDC_NOTIFY_SPEED_CHANGE;
1235 event->wValue = __constant_cpu_to_le16 (0);
1236 event->wIndex = __constant_cpu_to_le16 (1);
1237 event->wLength = __constant_cpu_to_le16 (8);
1239 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1240 event->data [0] = event->data [1] =
1241 (dev->gadget->speed == USB_SPEED_HIGH)
1242 ? (13 * 512 * 8 * 1000 * 8)
1243 : (19 * 64 * 1 * 1000 * 8);
1246 value = usb_ep_queue (ep, req, GFP_ATOMIC);
1247 DEBUG (dev, "send SPEED_CHANGE --> %d\n", value);
1251 DEBUG (dev, "event %02x --> %d\n",
1252 event->bNotificationType, value);
1254 /* free when done */
1255 usb_ep_free_buffer (ep, req->buf, req->dma, 16);
1256 usb_ep_free_request (ep, req);
1259 static void issue_start_status (struct eth_dev *dev)
1261 struct usb_request *req;
1262 struct cdc_notification *event;
1265 DEBUG (dev, "%s, flush old status first\n", __FUNCTION__);
1269 * FIXME ugly idiom, maybe we'd be better with just
1270 * a "cancel the whole queue" primitive since any
1271 * unlink-one primitive has way too many error modes.
1272 * here, we "know" toggle is already clear...
1274 usb_ep_disable (dev->status_ep);
1275 usb_ep_enable (dev->status_ep, dev->status);
1277 /* FIXME make these allocations static like dev->req */
1278 req = usb_ep_alloc_request (dev->status_ep, GFP_ATOMIC);
1280 DEBUG (dev, "status ENOMEM\n");
1283 req->buf = usb_ep_alloc_buffer (dev->status_ep, 16,
1284 &dev->req->dma, GFP_ATOMIC);
1285 if (req->buf == 0) {
1286 DEBUG (dev, "status buf ENOMEM\n");
1288 usb_ep_free_request (dev->status_ep, req);
1292 /* 3.8.1 says to issue first NETWORK_CONNECTION, then
1293 * a SPEED_CHANGE. could be useful in some configs.
1296 event->bmRequestType = 0xA1;
1297 event->bNotificationType = CDC_NOTIFY_NETWORK_CONNECTION;
1298 event->wValue = __constant_cpu_to_le16 (1); /* connected */
1299 event->wIndex = __constant_cpu_to_le16 (1);
1303 req->complete = eth_status_complete;
1304 value = usb_ep_queue (dev->status_ep, req, GFP_ATOMIC);
1306 DEBUG (dev, "status buf queue --> %d\n", value);
1307 usb_ep_free_buffer (dev->status_ep,
1308 req->buf, dev->req->dma, 16);
1315 /*-------------------------------------------------------------------------*/
1317 static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req)
1319 if (req->status || req->actual != req->length)
1320 DEBUG ((struct eth_dev *) ep->driver_data,
1321 "setup complete --> %d, %d/%d\n",
1322 req->status, req->actual, req->length);
1325 /* see section 3.8.2 table 10 of the CDC spec for more ethernet
1326 * requests, mostly for filters (multicast, pm) and statistics
1327 * section 3.6.2.1 table 4 has ACM requests; RNDIS requires the
1328 * encapsulated command mechanism.
1330 #define CDC_SEND_ENCAPSULATED_COMMAND 0x00 /* optional */
1331 #define CDC_GET_ENCAPSULATED_RESPONSE 0x01 /* optional */
1332 #define CDC_SET_ETHERNET_MULTICAST_FILTERS 0x40 /* optional */
1333 #define CDC_SET_ETHERNET_PM_PATTERN_FILTER 0x41 /* optional */
1334 #define CDC_GET_ETHERNET_PM_PATTERN_FILTER 0x42 /* optional */
1335 #define CDC_SET_ETHERNET_PACKET_FILTER 0x43 /* required */
1336 #define CDC_GET_ETHERNET_STATISTIC 0x44 /* optional */
1338 /* table 62; bits in cdc_filter */
1339 #define CDC_PACKET_TYPE_PROMISCUOUS (1 << 0)
1340 #define CDC_PACKET_TYPE_ALL_MULTICAST (1 << 1) /* no filter */
1341 #define CDC_PACKET_TYPE_DIRECTED (1 << 2)
1342 #define CDC_PACKET_TYPE_BROADCAST (1 << 3)
1343 #define CDC_PACKET_TYPE_MULTICAST (1 << 4) /* filtered */
1345 #ifdef CONFIG_USB_ETH_RNDIS
1347 static void rndis_response_complete (struct usb_ep *ep, struct usb_request *req)
1349 if (req->status || req->actual != req->length)
1350 DEBUG (dev, "rndis response complete --> %d, %d/%d\n",
1351 req->status, req->actual, req->length);
1353 /* done sending after CDC_GET_ENCAPSULATED_RESPONSE */
1356 static void rndis_command_complete (struct usb_ep *ep, struct usb_request *req)
1358 struct eth_dev *dev = ep->driver_data;
1361 /* received RNDIS command from CDC_SEND_ENCAPSULATED_COMMAND */
1362 spin_lock(&dev->lock);
1363 status = rndis_msg_parser (dev->rndis_config, (u8 *) req->buf);
1365 ERROR(dev, "%s: rndis parse error %d\n", __FUNCTION__, status);
1366 spin_unlock(&dev->lock);
1372 * The setup() callback implements all the ep0 functionality that's not
1373 * handled lower down. CDC has a number of less-common features:
1375 * - two interfaces: control, and ethernet data
1376 * - Ethernet data interface has two altsettings: default, and active
1377 * - class-specific descriptors for the control interface
1378 * - class-specific control requests
1381 eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1383 struct eth_dev *dev = get_gadget_data (gadget);
1384 struct usb_request *req = dev->req;
1385 int value = -EOPNOTSUPP;
1387 /* descriptors just go into the pre-allocated ep0 buffer,
1388 * while config change events may enable network traffic.
1390 switch (ctrl->bRequest) {
1392 case USB_REQ_GET_DESCRIPTOR:
1393 if (ctrl->bRequestType != USB_DIR_IN)
1395 switch (ctrl->wValue >> 8) {
1398 value = min (ctrl->wLength, (u16) sizeof device_desc);
1399 memcpy (req->buf, &device_desc, value);
1401 #ifdef CONFIG_USB_GADGET_DUALSPEED
1402 case USB_DT_DEVICE_QUALIFIER:
1403 if (!gadget->is_dualspeed)
1405 value = min (ctrl->wLength, (u16) sizeof dev_qualifier);
1406 memcpy (req->buf, &dev_qualifier, value);
1409 case USB_DT_OTHER_SPEED_CONFIG:
1410 if (!gadget->is_dualspeed)
1413 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1415 value = config_buf (gadget->speed, req->buf,
1417 ctrl->wValue & 0xff);
1419 value = min (ctrl->wLength, (u16) value);
1423 value = usb_gadget_get_string (&stringtab,
1424 ctrl->wValue & 0xff, req->buf);
1426 value = min (ctrl->wLength, (u16) value);
1431 case USB_REQ_SET_CONFIGURATION:
1432 if (ctrl->bRequestType != 0)
1434 spin_lock (&dev->lock);
1435 value = eth_set_config (dev, ctrl->wValue, GFP_ATOMIC);
1436 spin_unlock (&dev->lock);
1438 case USB_REQ_GET_CONFIGURATION:
1439 if (ctrl->bRequestType != USB_DIR_IN)
1441 *(u8 *)req->buf = dev->config;
1442 value = min (ctrl->wLength, (u16) 1);
1445 case USB_REQ_SET_INTERFACE:
1446 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1448 || ctrl->wIndex > 1)
1450 if (!dev->cdc && ctrl->wIndex != 0)
1452 spin_lock (&dev->lock);
1454 /* PXA hardware partially handles SET_INTERFACE;
1455 * we need to kluge around that interference.
1457 if (gadget_is_pxa (gadget)) {
1458 value = eth_set_config (dev, DEV_CONFIG_VALUE,
1463 #ifdef DEV_CONFIG_CDC
1464 switch (ctrl->wIndex) {
1465 case 0: /* control/master intf */
1466 if (ctrl->wValue != 0)
1468 if (dev->status_ep) {
1469 usb_ep_disable (dev->status_ep);
1470 usb_ep_enable (dev->status_ep, dev->status);
1474 case 1: /* data intf */
1475 if (ctrl->wValue > 1)
1477 usb_ep_disable (dev->in_ep);
1478 usb_ep_disable (dev->out_ep);
1480 /* CDC requires the data transfers not be done from
1481 * the default interface setting ... also, setting
1482 * the non-default interface clears filters etc.
1484 if (ctrl->wValue == 1) {
1485 usb_ep_enable (dev->in_ep, dev->in);
1486 usb_ep_enable (dev->out_ep, dev->out);
1487 netif_carrier_on (dev->net);
1489 issue_start_status (dev);
1490 if (netif_running (dev->net)) {
1491 spin_unlock (&dev->lock);
1492 eth_start (dev, GFP_ATOMIC);
1493 spin_lock (&dev->lock);
1496 netif_stop_queue (dev->net);
1497 netif_carrier_off (dev->net);
1503 /* FIXME this is wrong, as is the assumption that
1504 * all non-PXA hardware talks real CDC ...
1506 dev_warn (&gadget->dev, "set_interface ignored!\n");
1507 #endif /* DEV_CONFIG_CDC */
1510 spin_unlock (&dev->lock);
1512 case USB_REQ_GET_INTERFACE:
1513 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1515 || ctrl->wIndex > 1)
1517 if (!(dev->cdc || dev->rndis) && ctrl->wIndex != 0)
1520 /* for CDC, iff carrier is on, data interface is active. */
1521 if (dev->rndis || ctrl->wIndex != 1)
1522 *(u8 *)req->buf = 0;
1524 *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0;
1525 value = min (ctrl->wLength, (u16) 1);
1528 #ifdef DEV_CONFIG_CDC
1529 case CDC_SET_ETHERNET_PACKET_FILTER:
1530 /* see 6.2.30: no data, wIndex = interface,
1531 * wValue = packet filter bitmap
1533 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1536 || ctrl->wLength != 0
1537 || ctrl->wIndex > 1)
1539 DEBUG (dev, "NOP packet filter %04x\n", ctrl->wValue);
1540 /* NOTE: table 62 has 5 filter bits to reduce traffic,
1541 * and we "must" support multicast and promiscuous.
1542 * this NOP implements a bad filter (always promisc)
1544 dev->cdc_filter = ctrl->wValue;
1547 #endif /* DEV_CONFIG_CDC */
1549 #ifdef CONFIG_USB_ETH_RNDIS
1550 /* RNDIS uses the CDC command encapsulation mechanism to implement
1551 * an RPC scheme, with much getting/setting of attributes by OID.
1553 case CDC_SEND_ENCAPSULATED_COMMAND:
1554 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1556 || ctrl->wLength > USB_BUFSIZ
1558 || rndis_control_intf.bInterfaceNumber
1561 /* read the request, then process it */
1562 value = ctrl->wLength;
1563 req->complete = rndis_command_complete;
1564 /* later, rndis_control_ack () sends a notification */
1567 case CDC_GET_ENCAPSULATED_RESPONSE:
1568 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1569 == ctrl->bRequestType
1571 // && ctrl->wLength >= 0x0400
1573 && rndis_control_intf.bInterfaceNumber
1577 /* return the result */
1578 buf = rndis_get_next_response (dev->rndis_config,
1581 memcpy (req->buf, buf, value);
1582 req->complete = rndis_response_complete;
1583 rndis_free_response(dev->rndis_config, buf);
1585 /* else stalls ... spec says to avoid that */
1592 "unknown control req%02x.%02x v%04x i%04x l%d\n",
1593 ctrl->bRequestType, ctrl->bRequest,
1594 ctrl->wValue, ctrl->wIndex, ctrl->wLength);
1597 /* respond with data transfer before status phase? */
1599 req->length = value;
1600 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1602 DEBUG (dev, "ep_queue --> %d\n", value);
1604 eth_setup_complete (gadget->ep0, req);
1608 /* host either stalls (value < 0) or reports success */
1613 eth_disconnect (struct usb_gadget *gadget)
1615 struct eth_dev *dev = get_gadget_data (gadget);
1616 unsigned long flags;
1618 spin_lock_irqsave (&dev->lock, flags);
1619 netif_stop_queue (dev->net);
1620 netif_carrier_off (dev->net);
1621 eth_reset_config (dev);
1622 spin_unlock_irqrestore (&dev->lock, flags);
1624 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1626 /* next we may get setup() calls to enumerate new connections;
1627 * or an unbind() during shutdown (including removing module).
1631 /*-------------------------------------------------------------------------*/
1633 /* NETWORK DRIVER HOOKUP (to the layer above this driver) */
1635 static int eth_change_mtu (struct net_device *net, int new_mtu)
1637 struct eth_dev *dev = (struct eth_dev *) net->priv;
1639 // FIXME if rndis, don't change while link's live
1641 if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN)
1643 /* no zero-length packet read wanted after mtu-sized packets */
1644 if (((new_mtu + sizeof (struct ethhdr)) % dev->in_ep->maxpacket) == 0)
1650 static struct net_device_stats *eth_get_stats (struct net_device *net)
1652 return &((struct eth_dev *) net->priv)->stats;
1655 static int eth_ethtool_ioctl (struct net_device *net, void __user *useraddr)
1657 struct eth_dev *dev = (struct eth_dev *) net->priv;
1660 if (get_user (cmd, (u32 __user *)useraddr))
1664 case ETHTOOL_GDRVINFO: { /* get driver info */
1665 struct ethtool_drvinfo info;
1667 memset (&info, 0, sizeof info);
1668 info.cmd = ETHTOOL_GDRVINFO;
1669 strlcpy (info.driver, shortname, sizeof info.driver);
1670 strlcpy (info.version, DRIVER_VERSION, sizeof info.version);
1671 strlcpy (info.fw_version, dev->gadget->name,
1672 sizeof info.fw_version);
1673 strlcpy (info.bus_info, dev->gadget->dev.bus_id,
1674 sizeof info.bus_info);
1675 if (copy_to_user (useraddr, &info, sizeof (info)))
1680 case ETHTOOL_GLINK: { /* get link status */
1681 struct ethtool_value edata = { ETHTOOL_GLINK };
1683 edata.data = (dev->gadget->speed != USB_SPEED_UNKNOWN);
1684 if (copy_to_user (useraddr, &edata, sizeof (edata)))
1690 /* Note that the ethtool user space code requires EOPNOTSUPP */
1694 static int eth_ioctl (struct net_device *net, struct ifreq *rq, int cmd)
1698 return eth_ethtool_ioctl(net, rq->ifr_data);
1704 static void defer_kevent (struct eth_dev *dev, int flag)
1706 if (test_and_set_bit (flag, &dev->todo))
1708 if (!schedule_work (&dev->work))
1709 ERROR (dev, "kevent %d may have been dropped\n", flag);
1711 DEBUG (dev, "kevent %d scheduled\n", flag);
1714 static void rx_complete (struct usb_ep *ep, struct usb_request *req);
1717 rx_submit (struct eth_dev *dev, struct usb_request *req, int gfp_flags)
1719 struct sk_buff *skb;
1720 int retval = -ENOMEM;
1723 /* Padding up to RX_EXTRA handles minor disagreements with host.
1724 * Normally we use the USB "terminate on short read" convention;
1725 * so allow up to (N*maxpacket)-1, since that memory is normally
1726 * already allocated. Major loss of synch means -EOVERFLOW; any
1727 * obviously corrupted packets will automatically be discarded.
1729 * RNDIS uses internal framing, and explicitly allows senders to
1730 * pad to end-of-packet. That's potentially nice for speed,
1731 * but means receivers can't recover synch on their own.
1733 size = (sizeof (struct ethhdr) + dev->net->mtu + RX_EXTRA);
1734 size += dev->out_ep->maxpacket - 1;
1735 #ifdef CONFIG_USB_ETH_RNDIS
1737 size += sizeof (struct rndis_packet_msg_type);
1739 size -= size % dev->out_ep->maxpacket;
1740 #ifdef CONFIG_USB_ETH_RNDIS
1745 if ((skb = alloc_skb (size, gfp_flags)) == 0) {
1746 DEBUG (dev, "no rx skb\n");
1750 req->buf = skb->data;
1752 req->complete = rx_complete;
1755 retval = usb_ep_queue (dev->out_ep, req, gfp_flags);
1756 if (retval == -ENOMEM)
1758 defer_kevent (dev, WORK_RX_MEMORY);
1760 DEBUG (dev, "rx submit --> %d\n", retval);
1761 dev_kfree_skb_any (skb);
1762 spin_lock (&dev->lock);
1763 list_add (&req->list, &dev->rx_reqs);
1764 spin_unlock (&dev->lock);
1769 static void rx_complete (struct usb_ep *ep, struct usb_request *req)
1771 struct sk_buff *skb = req->context;
1772 struct eth_dev *dev = ep->driver_data;
1773 int status = req->status;
1777 /* normal completion */
1779 skb_put (skb, req->actual);
1780 #ifdef CONFIG_USB_ETH_RNDIS
1781 /* we know MaxPacketsPerTransfer == 1 here */
1783 rndis_rm_hdr (req->buf, &(skb->len));
1785 if (ETH_HLEN > skb->len || skb->len > ETH_FRAME_LEN) {
1786 dev->stats.rx_errors++;
1787 dev->stats.rx_length_errors++;
1788 DEBUG (dev, "rx length %d\n", skb->len);
1792 skb->dev = dev->net;
1793 skb->protocol = eth_type_trans (skb, dev->net);
1794 dev->stats.rx_packets++;
1795 dev->stats.rx_bytes += skb->len;
1797 /* no buffer copies needed, unless hardware can't
1800 status = netif_rx (skb);
1804 /* software-driven interface shutdown */
1805 case -ECONNRESET: // unlink
1806 case -ESHUTDOWN: // disconnect etc
1807 VDEBUG (dev, "rx shutdown, code %d\n", status);
1810 /* for hardware automagic (such as pxa) */
1811 case -ECONNABORTED: // endpoint reset
1812 DEBUG (dev, "rx %s reset\n", ep->name);
1813 defer_kevent (dev, WORK_RX_MEMORY);
1815 dev_kfree_skb_any (skb);
1820 dev->stats.rx_over_errors++;
1824 dev->stats.rx_errors++;
1825 DEBUG (dev, "rx status %d\n", status);
1830 dev_kfree_skb_any (skb);
1831 if (!netif_running (dev->net)) {
1833 /* nobody reading rx_reqs, so no dev->lock */
1834 list_add (&req->list, &dev->rx_reqs);
1838 rx_submit (dev, req, GFP_ATOMIC);
1841 static int prealloc (struct list_head *list, struct usb_ep *ep,
1842 unsigned n, int gfp_flags)
1845 struct usb_request *req;
1850 /* queue/recycle up to N requests */
1852 list_for_each_entry (req, list, list) {
1857 req = usb_ep_alloc_request (ep, gfp_flags);
1859 return list_empty (list) ? -ENOMEM : 0;
1860 list_add (&req->list, list);
1867 struct list_head *next;
1869 next = req->list.next;
1870 list_del (&req->list);
1871 usb_ep_free_request (ep, req);
1876 req = container_of (next, struct usb_request, list);
1881 static int alloc_requests (struct eth_dev *dev, unsigned n, int gfp_flags)
1885 status = prealloc (&dev->tx_reqs, dev->in_ep, n, gfp_flags);
1888 status = prealloc (&dev->rx_reqs, dev->out_ep, n, gfp_flags);
1893 DEBUG (dev, "can't alloc requests\n");
1897 static void rx_fill (struct eth_dev *dev, int gfp_flags)
1899 struct usb_request *req;
1900 unsigned long flags;
1902 clear_bit (WORK_RX_MEMORY, &dev->todo);
1904 /* fill unused rxq slots with some skb */
1905 spin_lock_irqsave (&dev->lock, flags);
1906 while (!list_empty (&dev->rx_reqs)) {
1907 req = container_of (dev->rx_reqs.next,
1908 struct usb_request, list);
1909 list_del_init (&req->list);
1910 spin_unlock_irqrestore (&dev->lock, flags);
1912 if (rx_submit (dev, req, gfp_flags) < 0) {
1913 defer_kevent (dev, WORK_RX_MEMORY);
1917 spin_lock_irqsave (&dev->lock, flags);
1919 spin_unlock_irqrestore (&dev->lock, flags);
1922 static void eth_work (void *_dev)
1924 struct eth_dev *dev = _dev;
1926 if (test_bit (WORK_RX_MEMORY, &dev->todo)) {
1927 if (netif_running (dev->net))
1928 rx_fill (dev, GFP_KERNEL);
1930 clear_bit (WORK_RX_MEMORY, &dev->todo);
1934 DEBUG (dev, "work done, flags = 0x%lx\n", dev->todo);
1937 static void tx_complete (struct usb_ep *ep, struct usb_request *req)
1939 struct sk_buff *skb = req->context;
1940 struct eth_dev *dev = ep->driver_data;
1942 switch (req->status) {
1944 dev->stats.tx_errors++;
1945 VDEBUG (dev, "tx err %d\n", req->status);
1947 case -ECONNRESET: // unlink
1948 case -ESHUTDOWN: // disconnect etc
1951 dev->stats.tx_bytes += skb->len;
1953 dev->stats.tx_packets++;
1955 spin_lock (&dev->lock);
1956 list_add (&req->list, &dev->tx_reqs);
1957 spin_unlock (&dev->lock);
1958 dev_kfree_skb_any (skb);
1960 atomic_dec (&dev->tx_qlen);
1961 if (netif_carrier_ok (dev->net))
1962 netif_wake_queue (dev->net);
1965 static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1967 struct eth_dev *dev = (struct eth_dev *) net->priv;
1968 int length = skb->len;
1970 struct usb_request *req = 0;
1971 unsigned long flags;
1973 /* FIXME check dev->cdc_filter to decide whether to send this,
1974 * instead of acting as if CDC_PACKET_TYPE_PROMISCUOUS were
1975 * always set. RNDIS has the same kind of outgoing filter.
1978 spin_lock_irqsave (&dev->lock, flags);
1979 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1980 list_del (&req->list);
1981 if (list_empty (&dev->tx_reqs))
1982 netif_stop_queue (net);
1983 spin_unlock_irqrestore (&dev->lock, flags);
1985 /* no buffer copies needed, unless the network stack did it
1986 * or the hardware can't use skb buffers.
1987 * or there's not enough space for any RNDIS headers we need
1989 #ifdef CONFIG_USB_ETH_RNDIS
1991 struct sk_buff *skb_rndis;
1993 skb_rndis = skb_realloc_headroom (skb,
1994 sizeof (struct rndis_packet_msg_type));
1998 dev_kfree_skb_any (skb);
2000 rndis_add_hdr (skb);
2004 req->buf = skb->data;
2006 req->complete = tx_complete;
2008 /* use zlp framing on tx for strict CDC-Ether conformance,
2009 * though any robust network rx path ignores extra padding.
2010 * and some hardware doesn't like to write zlps.
2013 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2016 req->length = length;
2018 #ifdef CONFIG_USB_GADGET_DUALSPEED
2019 /* throttle highspeed IRQ rate back slightly */
2020 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2021 ? ((atomic_read (&dev->tx_qlen) % TX_DELAY) != 0)
2025 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
2028 DEBUG (dev, "tx queue err %d\n", retval);
2031 net->trans_start = jiffies;
2032 atomic_inc (&dev->tx_qlen);
2036 #ifdef CONFIG_USB_ETH_RNDIS
2039 dev->stats.tx_dropped++;
2040 dev_kfree_skb_any (skb);
2041 spin_lock_irqsave (&dev->lock, flags);
2042 if (list_empty (&dev->tx_reqs))
2043 netif_start_queue (net);
2044 list_add (&req->list, &dev->tx_reqs);
2045 spin_unlock_irqrestore (&dev->lock, flags);
2050 /*-------------------------------------------------------------------------*/
2052 #ifdef CONFIG_USB_ETH_RNDIS
2054 static void rndis_send_media_state (struct eth_dev *dev, int connect)
2060 if (rndis_signal_connect (dev->rndis_config))
2063 if (rndis_signal_disconnect (dev->rndis_config))
2068 static void rndis_control_ack_complete (struct usb_ep *ep, struct usb_request *req)
2070 if (req->status || req->actual != req->length)
2071 DEBUG (dev, "rndis control ack complete --> %d, %d/%d\n",
2072 req->status, req->actual, req->length);
2074 usb_ep_free_buffer(ep, req->buf, req->dma, 8);
2075 usb_ep_free_request(ep, req);
2078 static int rndis_control_ack (struct net_device *net)
2080 struct eth_dev *dev = (struct eth_dev *) net->priv;
2082 struct usb_request *resp;
2084 /* in case RNDIS calls this after disconnect */
2085 if (!dev->status_ep) {
2086 DEBUG (dev, "status ENODEV\n");
2090 /* Allocate memory for notification ie. ACK */
2091 resp = usb_ep_alloc_request (dev->status_ep, GFP_ATOMIC);
2093 DEBUG (dev, "status ENOMEM\n");
2097 resp->buf = usb_ep_alloc_buffer (dev->status_ep, 8,
2098 &resp->dma, GFP_ATOMIC);
2100 DEBUG (dev, "status buf ENOMEM\n");
2101 usb_ep_free_request (dev->status_ep, resp);
2105 /* Send RNDIS RESPONSE_AVAILABLE notification;
2106 * CDC_NOTIFY_RESPONSE_AVAILABLE should work too
2109 resp->complete = rndis_control_ack_complete;
2111 *((u32 *) resp->buf) = __constant_cpu_to_le32 (1);
2112 *((u32 *) resp->buf + 1) = __constant_cpu_to_le32 (0);
2114 length = usb_ep_queue (dev->status_ep, resp, GFP_ATOMIC);
2117 rndis_control_ack_complete (dev->status_ep, resp);
2125 static void eth_start (struct eth_dev *dev, int gfp_flags)
2127 DEBUG (dev, "%s\n", __FUNCTION__);
2129 /* fill the rx queue */
2130 rx_fill (dev, gfp_flags);
2132 /* and open the tx floodgates */
2133 atomic_set (&dev->tx_qlen, 0);
2134 netif_wake_queue (dev->net);
2135 #ifdef CONFIG_USB_ETH_RNDIS
2137 rndis_set_param_medium (dev->rndis_config,
2139 BITRATE(dev->gadget));
2140 rndis_send_media_state (dev, 1);
2145 static int eth_open (struct net_device *net)
2147 struct eth_dev *dev = (struct eth_dev *) net->priv;
2149 DEBUG (dev, "%s\n", __FUNCTION__);
2150 if (netif_carrier_ok (dev->net))
2151 eth_start (dev, GFP_KERNEL);
2155 static int eth_stop (struct net_device *net)
2157 struct eth_dev *dev = (struct eth_dev *) net->priv;
2159 VDEBUG (dev, "%s\n", __FUNCTION__);
2160 netif_stop_queue (net);
2162 DEBUG (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
2163 dev->stats.rx_packets, dev->stats.tx_packets,
2164 dev->stats.rx_errors, dev->stats.tx_errors
2167 /* ensure there are no more active requests */
2168 if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
2169 usb_ep_disable (dev->in_ep);
2170 usb_ep_disable (dev->out_ep);
2171 if (netif_carrier_ok (dev->net)) {
2172 DEBUG (dev, "host still using in/out endpoints\n");
2173 // FIXME idiom may leave toggle wrong here
2174 usb_ep_enable (dev->in_ep, dev->in);
2175 usb_ep_enable (dev->out_ep, dev->out);
2177 if (dev->status_ep) {
2178 usb_ep_disable (dev->status_ep);
2179 usb_ep_enable (dev->status_ep, dev->status);
2183 #ifdef CONFIG_USB_ETH_RNDIS
2185 rndis_set_param_medium (dev->rndis_config,
2186 NDIS_MEDIUM_802_3, 0);
2187 rndis_send_media_state (dev, 0);
2194 /*-------------------------------------------------------------------------*/
2197 eth_unbind (struct usb_gadget *gadget)
2199 struct eth_dev *dev = get_gadget_data (gadget);
2201 DEBUG (dev, "unbind\n");
2202 #ifdef CONFIG_USB_ETH_RNDIS
2203 rndis_deregister (dev->rndis_config);
2207 /* we've already been disconnected ... no i/o is active */
2209 usb_ep_free_buffer (gadget->ep0,
2210 dev->req->buf, dev->req->dma,
2212 usb_ep_free_request (gadget->ep0, dev->req);
2216 unregister_netdev (dev->net);
2217 free_netdev(dev->net);
2219 /* assuming we used keventd, it must quiesce too */
2220 flush_scheduled_work ();
2221 set_gadget_data (gadget, 0);
2224 static u8 __init nibble (unsigned char c)
2226 if (likely (isdigit (c)))
2229 if (likely (isxdigit (c)))
2230 return 10 + c - 'A';
2234 static void __init get_ether_addr (const char *str, u8 *dev_addr)
2239 for (i = 0; i < 6; i++) {
2242 if((*str == '.') || (*str == ':'))
2244 num = nibble(*str++) << 4;
2245 num |= (nibble(*str++));
2248 if (is_valid_ether_addr (dev_addr))
2251 random_ether_addr(dev_addr);
2255 eth_bind (struct usb_gadget *gadget)
2257 struct eth_dev *dev;
2258 struct net_device *net;
2259 u8 cdc = 1, zlp = 1, rndis = 1;
2261 int status = -ENOMEM;
2263 /* these flags are only ever cleared; compiler take note */
2264 #ifndef DEV_CONFIG_CDC
2267 #ifndef CONFIG_USB_ETH_RNDIS
2271 /* Because most host side USB stacks handle CDC Ethernet, that
2272 * standard protocol is _strongly_ preferred for interop purposes.
2273 * (By everyone except Microsoft.)
2275 if (gadget_is_net2280 (gadget)) {
2276 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0201);
2277 } else if (gadget_is_dummy (gadget)) {
2278 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0202);
2279 } else if (gadget_is_pxa (gadget)) {
2280 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0203);
2281 /* pxa doesn't support altsettings */
2283 } else if (gadget_is_sh(gadget)) {
2284 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0204);
2285 /* sh doesn't support multiple interfaces or configs */
2288 } else if (gadget_is_sa1100 (gadget)) {
2289 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0205);
2290 /* hardware can't write zlps */
2292 /* sa1100 CAN do CDC, without status endpoint ... we use
2293 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2296 } else if (gadget_is_goku (gadget)) {
2297 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0206);
2298 } else if (gadget_is_mq11xx (gadget)) {
2299 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0207);
2300 } else if (gadget_is_omap (gadget)) {
2301 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0208);
2303 /* can't assume CDC works. don't want to default to
2304 * anything less functional on CDC-capable hardware,
2305 * so we fail in this case.
2307 dev_err (&gadget->dev,
2308 "controller '%s' not recognized\n",
2312 snprintf (manufacturer, sizeof manufacturer,
2313 UTS_SYSNAME " " UTS_RELEASE "/%s",
2316 /* If there's an RNDIS configuration, that's what Windows wants to
2317 * be using ... so use these product IDs here and in the "linux.inf"
2318 * needed to install MSFT drivers. Current Linux kernels will use
2319 * the second configuration if it's CDC Ethernet, and need some help
2320 * to choose the right configuration otherwise.
2323 device_desc.idVendor =
2324 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2325 device_desc.idProduct =
2326 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2327 snprintf (product_desc, sizeof product_desc,
2328 "RNDIS/%s", driver_desc);
2330 /* CDC subset ... recognized by Linux since 2.4.10, but Windows
2331 * drivers aren't widely available.
2334 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2335 device_desc.idVendor =
2336 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2337 device_desc.idProduct =
2338 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2341 /* support optional vendor/distro customization */
2344 dev_err (&gadget->dev, "idVendor needs idProduct!\n");
2347 device_desc.idVendor = cpu_to_le16(idVendor);
2348 device_desc.idProduct = cpu_to_le16(idProduct);
2350 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2353 strlcpy (manufacturer, iManufacturer, sizeof manufacturer);
2355 strlcpy (product_desc, iProduct, sizeof product_desc);
2357 /* all we really need is bulk IN/OUT */
2358 usb_ep_autoconfig_reset (gadget);
2359 ep = usb_ep_autoconfig (gadget, &fs_source_desc);
2362 dev_err (&gadget->dev,
2363 "can't autoconfigure on %s\n",
2367 EP_IN_NAME = ep->name;
2368 ep->driver_data = ep; /* claim */
2370 ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
2373 EP_OUT_NAME = ep->name;
2374 ep->driver_data = ep; /* claim */
2376 /* CDC Ethernet control interface doesn't require a status endpoint.
2377 * Since some hosts expect one, try to allocate one anyway.
2380 ep = usb_ep_autoconfig (gadget, &fs_status_desc);
2382 EP_STATUS_NAME = ep->name;
2383 ep->driver_data = ep; /* claim */
2385 dev_err (&gadget->dev,
2386 "can't run RNDIS on %s\n",
2389 #ifdef DEV_CONFIG_CDC
2391 control_intf.bNumEndpoints = 0;
2392 /* FIXME remove endpoint from descriptor list */
2397 /* one config: cdc, else minimal subset */
2399 eth_config.bNumInterfaces = 1;
2400 eth_config.iConfiguration = STRING_SUBSET;
2401 fs_subset_descriptors();
2402 hs_subset_descriptors();
2405 /* For now RNDIS is always a second config */
2407 device_desc.bNumConfigurations = 2;
2409 #ifdef CONFIG_USB_GADGET_DUALSPEED
2411 dev_qualifier.bNumConfigurations = 2;
2413 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2415 /* assumes ep0 uses the same value for both speeds ... */
2416 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2418 /* and that all endpoints are dual-speed */
2419 hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
2420 hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
2421 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2423 hs_status_desc.bEndpointAddress =
2424 fs_status_desc.bEndpointAddress;
2426 #endif /* DUALSPEED */
2428 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
2429 usb_gadget_set_selfpowered (gadget);
2431 net = alloc_etherdev (sizeof *dev);
2435 spin_lock_init (&dev->lock);
2436 INIT_WORK (&dev->work, eth_work, dev);
2437 INIT_LIST_HEAD (&dev->tx_reqs);
2438 INIT_LIST_HEAD (&dev->rx_reqs);
2440 /* network device setup */
2442 SET_MODULE_OWNER (net);
2443 strcpy (net->name, "usb%d");
2447 /* Module params for these addresses should come from ID proms.
2448 * The host side address is used with CDC and RNDIS, and commonly
2449 * end ups in a persistent config database.
2451 get_ether_addr(dev_addr, net->dev_addr);
2453 get_ether_addr(host_addr, dev->host_mac);
2454 #ifdef DEV_CONFIG_CDC
2455 snprintf (ethaddr, sizeof ethaddr, "%02X%02X%02X%02X%02X%02X",
2456 dev->host_mac [0], dev->host_mac [1],
2457 dev->host_mac [2], dev->host_mac [3],
2458 dev->host_mac [4], dev->host_mac [5]);
2463 status = rndis_init();
2465 dev_err (&gadget->dev, "can't init RNDIS, %d\n",
2471 net->change_mtu = eth_change_mtu;
2472 net->get_stats = eth_get_stats;
2473 net->hard_start_xmit = eth_start_xmit;
2474 net->open = eth_open;
2475 net->stop = eth_stop;
2476 // watchdog_timeo, tx_timeout ...
2477 // set_multicast_list
2478 net->do_ioctl = eth_ioctl;
2480 /* preallocate control response and buffer */
2481 dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
2484 dev->req->complete = eth_setup_complete;
2485 dev->req->buf = usb_ep_alloc_buffer (gadget->ep0, USB_BUFSIZ,
2486 &dev->req->dma, GFP_KERNEL);
2487 if (!dev->req->buf) {
2488 usb_ep_free_request (gadget->ep0, dev->req);
2492 /* finish hookup to lower layer ... */
2493 dev->gadget = gadget;
2494 set_gadget_data (gadget, dev);
2495 gadget->ep0->driver_data = dev;
2497 /* two kinds of host-initiated state changes:
2498 * - iff DATA transfer is active, carrier is "on"
2499 * - tx queueing enabled if open *and* carrier is "on"
2501 netif_stop_queue (dev->net);
2502 netif_carrier_off (dev->net);
2504 // SET_NETDEV_DEV (dev->net, &gadget->dev);
2505 status = register_netdev (dev->net);
2509 INFO (dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
2510 INFO (dev, "using %s, OUT %s IN %s%s%s\n", gadget->name,
2511 EP_OUT_NAME, EP_IN_NAME,
2512 EP_STATUS_NAME ? " STATUS " : "",
2513 EP_STATUS_NAME ? EP_STATUS_NAME : ""
2515 INFO (dev, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2516 net->dev_addr [0], net->dev_addr [1],
2517 net->dev_addr [2], net->dev_addr [3],
2518 net->dev_addr [4], net->dev_addr [5]);
2521 INFO (dev, "HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2522 dev->host_mac [0], dev->host_mac [1],
2523 dev->host_mac [2], dev->host_mac [3],
2524 dev->host_mac [4], dev->host_mac [5]);
2526 #ifdef CONFIG_USB_ETH_RNDIS
2530 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2532 dev->rndis_config = rndis_register (rndis_control_ack);
2533 if (dev->rndis_config < 0) {
2535 unregister_netdev (dev->net);
2540 /* these set up a lot of the OIDs that RNDIS needs */
2541 rndis_set_host_mac (dev->rndis_config, dev->host_mac);
2542 if (rndis_set_param_dev (dev->rndis_config, dev->net,
2545 if (rndis_set_param_vendor (dev->rndis_config, vendorID,
2548 if (rndis_set_param_medium (dev->rndis_config,
2552 INFO (dev, "RNDIS ready\n");
2559 dev_dbg(&gadget->dev, "register_netdev failed, %d\n", status);
2561 eth_unbind (gadget);
2565 /*-------------------------------------------------------------------------*/
2568 eth_suspend (struct usb_gadget *gadget)
2570 struct eth_dev *dev = get_gadget_data (gadget);
2572 DEBUG (dev, "suspend\n");
2577 eth_resume (struct usb_gadget *gadget)
2579 struct eth_dev *dev = get_gadget_data (gadget);
2581 DEBUG (dev, "resume\n");
2585 /*-------------------------------------------------------------------------*/
2587 static struct usb_gadget_driver eth_driver = {
2588 #ifdef CONFIG_USB_GADGET_DUALSPEED
2589 .speed = USB_SPEED_HIGH,
2591 .speed = USB_SPEED_FULL,
2593 .function = (char *) driver_desc,
2595 .unbind = eth_unbind,
2598 .disconnect = eth_disconnect,
2600 .suspend = eth_suspend,
2601 .resume = eth_resume,
2604 .name = (char *) shortname,
2611 MODULE_DESCRIPTION (DRIVER_DESC);
2612 MODULE_AUTHOR ("David Brownell, Benedikt Spanger");
2613 MODULE_LICENSE ("GPL");
2616 static int __init init (void)
2618 return usb_gadget_register_driver (ð_driver);
2622 static void __exit cleanup (void)
2624 usb_gadget_unregister_driver (ð_driver);
2626 module_exit (cleanup);