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_PXA2XX
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.
342 * FIXME define some higher-powered configurations to make it easier
343 * to recharge batteries ...
346 #define DEV_CONFIG_VALUE 1 /* cdc or subset */
347 #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
349 static struct usb_device_descriptor
351 .bLength = sizeof device_desc,
352 .bDescriptorType = USB_DT_DEVICE,
354 .bcdUSB = __constant_cpu_to_le16 (0x0200),
356 .bDeviceClass = USB_CLASS_COMM,
357 .bDeviceSubClass = 0,
358 .bDeviceProtocol = 0,
360 .idVendor = __constant_cpu_to_le16 (CDC_VENDOR_NUM),
361 .idProduct = __constant_cpu_to_le16 (CDC_PRODUCT_NUM),
362 .iManufacturer = STRING_MANUFACTURER,
363 .iProduct = STRING_PRODUCT,
364 .bNumConfigurations = 1,
367 static struct usb_otg_descriptor
369 .bLength = sizeof otg_descriptor,
370 .bDescriptorType = USB_DT_OTG,
372 .bmAttributes = USB_OTG_SRP,
375 static struct usb_config_descriptor
377 .bLength = sizeof eth_config,
378 .bDescriptorType = USB_DT_CONFIG,
380 /* compute wTotalLength on the fly */
382 .bConfigurationValue = DEV_CONFIG_VALUE,
383 .iConfiguration = STRING_CDC,
384 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
388 #ifdef CONFIG_USB_ETH_RNDIS
389 static struct usb_config_descriptor
391 .bLength = sizeof rndis_config,
392 .bDescriptorType = USB_DT_CONFIG,
394 /* compute wTotalLength on the fly */
396 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
397 .iConfiguration = STRING_RNDIS,
398 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
404 * Compared to the simple CDC subset, the full CDC Ethernet model adds
405 * three class descriptors, two interface descriptors, optional status
406 * endpoint. Both have a "data" interface and two bulk endpoints.
407 * There are also differences in how control requests are handled.
409 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of
410 * the CDC-ACM (modem) spec.
413 #ifdef DEV_CONFIG_CDC
414 static struct usb_interface_descriptor
416 .bLength = sizeof control_intf,
417 .bDescriptorType = USB_DT_INTERFACE,
419 .bInterfaceNumber = 0,
420 /* status endpoint is optional; this may be patched later */
422 .bInterfaceClass = USB_CLASS_COMM,
423 .bInterfaceSubClass = 6, /* ethernet control model */
424 .bInterfaceProtocol = 0,
425 .iInterface = STRING_CONTROL,
429 #ifdef CONFIG_USB_ETH_RNDIS
430 static const struct usb_interface_descriptor
431 rndis_control_intf = {
432 .bLength = sizeof rndis_control_intf,
433 .bDescriptorType = USB_DT_INTERFACE,
435 .bInterfaceNumber = 0,
437 .bInterfaceClass = USB_CLASS_COMM,
438 .bInterfaceSubClass = 2, /* abstract control model */
439 .bInterfaceProtocol = 0xff, /* vendor specific */
440 .iInterface = STRING_RNDIS_CONTROL,
444 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
446 /* "Header Functional Descriptor" from CDC spec 5.2.3.1 */
450 u8 bDescriptorSubType;
453 } __attribute__ ((packed));
455 static const struct header_desc header_desc = {
456 .bLength = sizeof header_desc,
457 .bDescriptorType = USB_DT_CS_INTERFACE,
458 .bDescriptorSubType = 0,
460 .bcdCDC = __constant_cpu_to_le16 (0x0110),
463 /* "Union Functional Descriptor" from CDC spec 5.2.3.8 */
467 u8 bDescriptorSubType;
469 u8 bMasterInterface0;
471 /* ... and there could be other slave interfaces */
472 } __attribute__ ((packed));
474 static const struct union_desc union_desc = {
475 .bLength = sizeof union_desc,
476 .bDescriptorType = USB_DT_CS_INTERFACE,
477 .bDescriptorSubType = 6,
479 .bMasterInterface0 = 0, /* index of control interface */
480 .bSlaveInterface0 = 1, /* index of DATA interface */
483 #endif /* CDC || RNDIS */
485 #ifdef CONFIG_USB_ETH_RNDIS
487 /* "Call Management Descriptor" from CDC spec 5.2.3.3 */
488 struct call_mgmt_descriptor {
491 u8 bDescriptorSubType;
495 } __attribute__ ((packed));
497 static const struct call_mgmt_descriptor call_mgmt_descriptor = {
498 .bLength = sizeof call_mgmt_descriptor,
499 .bDescriptorType = USB_DT_CS_INTERFACE,
500 .bDescriptorSubType = 0x01,
502 .bmCapabilities = 0x00,
503 .bDataInterface = 0x01,
507 /* "Abstract Control Management Descriptor" from CDC spec 5.2.3.4 */
508 struct acm_descriptor {
511 u8 bDescriptorSubType;
514 } __attribute__ ((packed));
516 static struct acm_descriptor acm_descriptor = {
517 .bLength = sizeof acm_descriptor,
518 .bDescriptorType = USB_DT_CS_INTERFACE,
519 .bDescriptorSubType = 0x02,
521 .bmCapabilities = 0X00,
526 #ifdef DEV_CONFIG_CDC
528 /* "Ethernet Networking Functional Descriptor" from CDC spec 5.2.3.16 */
532 u8 bDescriptorSubType;
535 u32 bmEthernetStatistics;
537 u16 wNumberMCFilters;
538 u8 bNumberPowerFilters;
539 } __attribute__ ((packed));
541 static const struct ether_desc ether_desc = {
542 .bLength = sizeof ether_desc,
543 .bDescriptorType = USB_DT_CS_INTERFACE,
544 .bDescriptorSubType = 0x0f,
546 /* this descriptor actually adds value, surprise! */
547 .iMACAddress = STRING_ETHADDR,
548 .bmEthernetStatistics = __constant_cpu_to_le32 (0), /* no statistics */
549 .wMaxSegmentSize = __constant_cpu_to_le16 (ETH_FRAME_LEN),
550 .wNumberMCFilters = __constant_cpu_to_le16 (0),
551 .bNumberPowerFilters = 0,
556 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
558 /* include the status endpoint if we can, even where it's optional.
559 * use small wMaxPacketSize, since many "interrupt" endpoints have
560 * very small fifos and it's no big deal if CDC_NOTIFY_SPEED_CHANGE
561 * takes two packets. also default to a big transfer interval, to
562 * waste less bandwidth.
564 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
565 * if they ignore the connect/disconnect notifications that real aether
566 * can provide. more advanced cdc configurations might want to support
567 * encapsulated commands (vendor-specific, using control-OUT).
569 * RNDIS requires the status endpoint, since it uses that encapsulation
570 * mechanism for its funky RPC scheme.
573 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
574 #define STATUS_BYTECOUNT 8 /* 8 byte header + data */
576 static struct usb_endpoint_descriptor
578 .bLength = USB_DT_ENDPOINT_SIZE,
579 .bDescriptorType = USB_DT_ENDPOINT,
581 .bEndpointAddress = USB_DIR_IN,
582 .bmAttributes = USB_ENDPOINT_XFER_INT,
583 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
584 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
588 #ifdef DEV_CONFIG_CDC
590 /* the default data interface has no endpoints ... */
592 static const struct usb_interface_descriptor
594 .bLength = sizeof data_nop_intf,
595 .bDescriptorType = USB_DT_INTERFACE,
597 .bInterfaceNumber = 1,
598 .bAlternateSetting = 0,
600 .bInterfaceClass = USB_CLASS_CDC_DATA,
601 .bInterfaceSubClass = 0,
602 .bInterfaceProtocol = 0,
605 /* ... but the "real" data interface has two bulk endpoints */
607 static const struct usb_interface_descriptor
609 .bLength = sizeof data_intf,
610 .bDescriptorType = USB_DT_INTERFACE,
612 .bInterfaceNumber = 1,
613 .bAlternateSetting = 1,
615 .bInterfaceClass = USB_CLASS_CDC_DATA,
616 .bInterfaceSubClass = 0,
617 .bInterfaceProtocol = 0,
618 .iInterface = STRING_DATA,
623 #ifdef CONFIG_USB_ETH_RNDIS
625 /* RNDIS doesn't activate by changing to the "real" altsetting */
627 static const struct usb_interface_descriptor
629 .bLength = sizeof rndis_data_intf,
630 .bDescriptorType = USB_DT_INTERFACE,
632 .bInterfaceNumber = 1,
633 .bAlternateSetting = 0,
635 .bInterfaceClass = USB_CLASS_CDC_DATA,
636 .bInterfaceSubClass = 0,
637 .bInterfaceProtocol = 0,
638 .iInterface = STRING_DATA,
643 #ifdef DEV_CONFIG_SUBSET
646 * "Simple" CDC-subset option is a simple vendor-neutral model that most
647 * full speed controllers can handle: one interface, two bulk endpoints.
650 static const struct usb_interface_descriptor
652 .bLength = sizeof subset_data_intf,
653 .bDescriptorType = USB_DT_INTERFACE,
655 .bInterfaceNumber = 0,
656 .bAlternateSetting = 0,
658 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
659 .bInterfaceSubClass = 0,
660 .bInterfaceProtocol = 0,
661 .iInterface = STRING_DATA,
667 static struct usb_endpoint_descriptor
669 .bLength = USB_DT_ENDPOINT_SIZE,
670 .bDescriptorType = USB_DT_ENDPOINT,
672 .bEndpointAddress = USB_DIR_IN,
673 .bmAttributes = USB_ENDPOINT_XFER_BULK,
676 static struct usb_endpoint_descriptor
678 .bLength = USB_DT_ENDPOINT_SIZE,
679 .bDescriptorType = USB_DT_ENDPOINT,
681 .bEndpointAddress = USB_DIR_OUT,
682 .bmAttributes = USB_ENDPOINT_XFER_BULK,
685 static const struct usb_descriptor_header *fs_eth_function [11] = {
686 (struct usb_descriptor_header *) &otg_descriptor,
687 #ifdef DEV_CONFIG_CDC
688 /* "cdc" mode descriptors */
689 (struct usb_descriptor_header *) &control_intf,
690 (struct usb_descriptor_header *) &header_desc,
691 (struct usb_descriptor_header *) &union_desc,
692 (struct usb_descriptor_header *) ðer_desc,
693 /* NOTE: status endpoint may need to be removed */
694 (struct usb_descriptor_header *) &fs_status_desc,
695 /* data interface, with altsetting */
696 (struct usb_descriptor_header *) &data_nop_intf,
697 (struct usb_descriptor_header *) &data_intf,
698 (struct usb_descriptor_header *) &fs_source_desc,
699 (struct usb_descriptor_header *) &fs_sink_desc,
701 #endif /* DEV_CONFIG_CDC */
704 static inline void __init fs_subset_descriptors(void)
706 #ifdef DEV_CONFIG_SUBSET
707 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
708 fs_eth_function[2] = (struct usb_descriptor_header *) &fs_source_desc;
709 fs_eth_function[3] = (struct usb_descriptor_header *) &fs_sink_desc;
710 fs_eth_function[4] = NULL;
712 fs_eth_function[1] = NULL;
716 #ifdef CONFIG_USB_ETH_RNDIS
717 static const struct usb_descriptor_header *fs_rndis_function [] = {
718 (struct usb_descriptor_header *) &otg_descriptor,
719 /* control interface matches ACM, not Ethernet */
720 (struct usb_descriptor_header *) &rndis_control_intf,
721 (struct usb_descriptor_header *) &header_desc,
722 (struct usb_descriptor_header *) &call_mgmt_descriptor,
723 (struct usb_descriptor_header *) &acm_descriptor,
724 (struct usb_descriptor_header *) &union_desc,
725 (struct usb_descriptor_header *) &fs_status_desc,
726 /* data interface has no altsetting */
727 (struct usb_descriptor_header *) &rndis_data_intf,
728 (struct usb_descriptor_header *) &fs_source_desc,
729 (struct usb_descriptor_header *) &fs_sink_desc,
734 #ifdef CONFIG_USB_GADGET_DUALSPEED
737 * usb 2.0 devices need to expose both high speed and full speed
738 * descriptors, unless they only run at full speed.
741 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
742 static struct usb_endpoint_descriptor
744 .bLength = USB_DT_ENDPOINT_SIZE,
745 .bDescriptorType = USB_DT_ENDPOINT,
747 .bmAttributes = USB_ENDPOINT_XFER_INT,
748 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
749 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
751 #endif /* DEV_CONFIG_CDC */
753 static struct usb_endpoint_descriptor
755 .bLength = USB_DT_ENDPOINT_SIZE,
756 .bDescriptorType = USB_DT_ENDPOINT,
758 .bmAttributes = USB_ENDPOINT_XFER_BULK,
759 .wMaxPacketSize = __constant_cpu_to_le16 (512),
762 static struct usb_endpoint_descriptor
764 .bLength = USB_DT_ENDPOINT_SIZE,
765 .bDescriptorType = USB_DT_ENDPOINT,
767 .bmAttributes = USB_ENDPOINT_XFER_BULK,
768 .wMaxPacketSize = __constant_cpu_to_le16 (512),
771 static struct usb_qualifier_descriptor
773 .bLength = sizeof dev_qualifier,
774 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
776 .bcdUSB = __constant_cpu_to_le16 (0x0200),
777 .bDeviceClass = USB_CLASS_COMM,
779 .bNumConfigurations = 1,
782 static const struct usb_descriptor_header *hs_eth_function [11] = {
783 (struct usb_descriptor_header *) &otg_descriptor,
784 #ifdef DEV_CONFIG_CDC
785 /* "cdc" mode descriptors */
786 (struct usb_descriptor_header *) &control_intf,
787 (struct usb_descriptor_header *) &header_desc,
788 (struct usb_descriptor_header *) &union_desc,
789 (struct usb_descriptor_header *) ðer_desc,
790 /* NOTE: status endpoint may need to be removed */
791 (struct usb_descriptor_header *) &hs_status_desc,
792 /* data interface, with altsetting */
793 (struct usb_descriptor_header *) &data_nop_intf,
794 (struct usb_descriptor_header *) &data_intf,
795 (struct usb_descriptor_header *) &hs_source_desc,
796 (struct usb_descriptor_header *) &hs_sink_desc,
798 #endif /* DEV_CONFIG_CDC */
801 static inline void __init hs_subset_descriptors(void)
803 #ifdef DEV_CONFIG_SUBSET
804 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
805 hs_eth_function[2] = (struct usb_descriptor_header *) &fs_source_desc;
806 hs_eth_function[3] = (struct usb_descriptor_header *) &fs_sink_desc;
807 hs_eth_function[4] = NULL;
809 hs_eth_function[1] = NULL;
813 #ifdef CONFIG_USB_ETH_RNDIS
814 static const struct usb_descriptor_header *hs_rndis_function [] = {
815 (struct usb_descriptor_header *) &otg_descriptor,
816 /* control interface matches ACM, not Ethernet */
817 (struct usb_descriptor_header *) &rndis_control_intf,
818 (struct usb_descriptor_header *) &header_desc,
819 (struct usb_descriptor_header *) &call_mgmt_descriptor,
820 (struct usb_descriptor_header *) &acm_descriptor,
821 (struct usb_descriptor_header *) &union_desc,
822 (struct usb_descriptor_header *) &hs_status_desc,
823 /* data interface has no altsetting */
824 (struct usb_descriptor_header *) &rndis_data_intf,
825 (struct usb_descriptor_header *) &hs_source_desc,
826 (struct usb_descriptor_header *) &hs_sink_desc,
832 /* maxpacket and other transfer characteristics vary by speed. */
833 #define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
837 /* if there's no high speed support, maxpacket doesn't change. */
838 #define ep_desc(g,hs,fs) fs
840 static inline void __init hs_subset_descriptors(void)
844 #endif /* !CONFIG_USB_GADGET_DUALSPEED */
846 /*-------------------------------------------------------------------------*/
848 /* descriptors that are built on-demand */
850 static char manufacturer [40];
851 static char product_desc [40] = DRIVER_DESC;
853 #ifdef DEV_CONFIG_CDC
854 /* address that the host will use ... usually assigned at random */
855 static char ethaddr [2 * ETH_ALEN + 1];
858 /* static strings, in iso 8859/1 */
859 static struct usb_string strings [] = {
860 { STRING_MANUFACTURER, manufacturer, },
861 { STRING_PRODUCT, product_desc, },
862 { STRING_DATA, "Ethernet Data", },
863 #ifdef DEV_CONFIG_CDC
864 { STRING_CDC, "CDC Ethernet", },
865 { STRING_ETHADDR, ethaddr, },
866 { STRING_CONTROL, "CDC Communications Control", },
868 #ifdef DEV_CONFIG_SUBSET
869 { STRING_SUBSET, "CDC Ethernet Subset", },
871 #ifdef CONFIG_USB_ETH_RNDIS
872 { STRING_RNDIS, "RNDIS", },
873 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
875 { } /* end of list */
878 static struct usb_gadget_strings stringtab = {
879 .language = 0x0409, /* en-us */
884 * one config, two interfaces: control, data.
885 * complications: class descriptors, and an altsetting.
888 config_buf (enum usb_device_speed speed,
890 unsigned index, int is_otg)
893 const struct usb_config_descriptor *config;
894 const struct usb_descriptor_header **function;
895 #ifdef CONFIG_USB_GADGET_DUALSPEED
896 int hs = (speed == USB_SPEED_HIGH);
898 if (type == USB_DT_OTHER_SPEED_CONFIG)
900 #define which_fn(t) (hs ? & hs_ ## t ## _function : & fs_ ## t ## _function)
902 #define which_fn(t) (& fs_ ## t ## _function)
905 if (index >= device_desc.bNumConfigurations)
908 #ifdef CONFIG_USB_ETH_RNDIS
909 /* list the RNDIS config first, to make Microsoft's drivers
910 * happy. DOCSIS 1.0 needs this too.
912 if (device_desc.bNumConfigurations == 2 && index == 0) {
913 config = &rndis_config;
914 function = (const struct usb_descriptor_header **)
919 config = ð_config;
920 function = (const struct usb_descriptor_header **)
924 /* for now, don't advertise srp-only devices */
928 len = usb_gadget_config_buf (config, buf, USB_BUFSIZ, function);
931 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
935 /*-------------------------------------------------------------------------*/
937 static void eth_start (struct eth_dev *dev, int gfp_flags);
938 static int alloc_requests (struct eth_dev *dev, unsigned n, int gfp_flags);
940 #ifdef DEV_CONFIG_CDC
941 static inline int ether_alt_ep_setup (struct eth_dev *dev, struct usb_ep *ep)
943 const struct usb_endpoint_descriptor *d;
945 /* With CDC, the host isn't allowed to use these two data
946 * endpoints in the default altsetting for the interface.
947 * so we don't activate them yet. Reset from SET_INTERFACE.
949 * Strictly speaking RNDIS should work the same: activation is
950 * a side effect of setting a packet filter. Deactivation is
951 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
954 /* one endpoint writes data back IN to the host */
955 if (strcmp (ep->name, EP_IN_NAME) == 0) {
956 d = ep_desc (dev->gadget, &hs_source_desc, &fs_source_desc);
957 ep->driver_data = dev;
961 /* one endpoint just reads OUT packets */
962 } else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
963 d = ep_desc (dev->gadget, &hs_sink_desc, &fs_sink_desc);
964 ep->driver_data = dev;
968 /* optional status/notification endpoint */
969 } else if (EP_STATUS_NAME &&
970 strcmp (ep->name, EP_STATUS_NAME) == 0) {
973 d = ep_desc (dev->gadget, &hs_status_desc, &fs_status_desc);
974 result = usb_ep_enable (ep, d);
978 ep->driver_data = dev;
986 #if defined(DEV_CONFIG_SUBSET) || defined(CONFIG_USB_ETH_RNDIS)
987 static inline int ether_ep_setup (struct eth_dev *dev, struct usb_ep *ep)
990 const struct usb_endpoint_descriptor *d;
992 /* CDC subset is simpler: if the device is there,
993 * it's live with rx and tx endpoints.
995 * Do this as a shortcut for RNDIS too.
998 /* one endpoint writes data back IN to the host */
999 if (strcmp (ep->name, EP_IN_NAME) == 0) {
1000 d = ep_desc (dev->gadget, &hs_source_desc, &fs_source_desc);
1001 result = usb_ep_enable (ep, d);
1005 ep->driver_data = dev;
1009 /* one endpoint just reads OUT packets */
1010 } else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
1011 d = ep_desc (dev->gadget, &hs_sink_desc, &fs_sink_desc);
1012 result = usb_ep_enable (ep, d);
1016 ep->driver_data = dev;
1026 set_ether_config (struct eth_dev *dev, int gfp_flags)
1030 struct usb_gadget *gadget = dev->gadget;
1032 gadget_for_each_ep (ep, gadget) {
1033 #ifdef DEV_CONFIG_CDC
1034 if (!dev->rndis && dev->cdc) {
1035 result = ether_alt_ep_setup (dev, ep);
1041 #ifdef CONFIG_USB_ETH_RNDIS
1042 if (dev->rndis && strcmp (ep->name, EP_STATUS_NAME) == 0) {
1043 const struct usb_endpoint_descriptor *d;
1044 d = ep_desc (gadget, &hs_status_desc, &fs_status_desc);
1045 result = usb_ep_enable (ep, d);
1047 ep->driver_data = dev;
1048 dev->status_ep = ep;
1056 #if defined(DEV_CONFIG_SUBSET) || defined(CONFIG_USB_ETH_RNDIS)
1057 result = ether_ep_setup (dev, ep);
1064 ERROR (dev, "can't enable %s, result %d\n", ep->name, result);
1067 if (!result && (!dev->in_ep || !dev->out_ep))
1071 result = alloc_requests (dev, qlen (gadget), gfp_flags);
1073 /* on error, disable any endpoints */
1075 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
1077 (void) usb_ep_disable (dev->status_ep);
1079 dev->status_ep = NULL;
1081 #if defined(DEV_CONFIG_SUBSET) || defined(CONFIG_USB_ETH_RNDIS)
1082 if (dev->rndis || !dev->cdc) {
1084 (void) usb_ep_disable (dev->in_ep);
1086 (void) usb_ep_disable (dev->out_ep);
1095 /* activate non-CDC configs right away
1096 * this isn't strictly according to the RNDIS spec
1098 #if defined(DEV_CONFIG_SUBSET) || defined(CONFIG_USB_ETH_RNDIS)
1099 if (dev->rndis || !dev->cdc) {
1100 netif_carrier_on (dev->net);
1101 if (netif_running (dev->net)) {
1102 spin_unlock (&dev->lock);
1103 eth_start (dev, GFP_ATOMIC);
1104 spin_lock (&dev->lock);
1110 DEBUG (dev, "qlen %d\n", qlen (gadget));
1112 /* caller is responsible for cleanup on error */
1116 static void eth_reset_config (struct eth_dev *dev)
1118 struct usb_request *req;
1120 if (dev->config == 0)
1123 DEBUG (dev, "%s\n", __FUNCTION__);
1125 netif_stop_queue (dev->net);
1126 netif_carrier_off (dev->net);
1128 /* disable endpoints, forcing (synchronous) completion of
1129 * pending i/o. then free the requests.
1132 usb_ep_disable (dev->in_ep);
1133 while (likely (!list_empty (&dev->tx_reqs))) {
1134 req = container_of (dev->tx_reqs.next,
1135 struct usb_request, list);
1136 list_del (&req->list);
1137 usb_ep_free_request (dev->in_ep, req);
1142 usb_ep_disable (dev->out_ep);
1143 while (likely (!list_empty (&dev->rx_reqs))) {
1144 req = container_of (dev->rx_reqs.next,
1145 struct usb_request, list);
1146 list_del (&req->list);
1147 usb_ep_free_request (dev->out_ep, req);
1152 if (dev->status_ep) {
1153 usb_ep_disable (dev->status_ep);
1154 dev->status_ep = NULL;
1159 /* change our operational config. must agree with the code
1160 * that returns config descriptors, and altsetting code.
1163 eth_set_config (struct eth_dev *dev, unsigned number, int gfp_flags)
1166 struct usb_gadget *gadget = dev->gadget;
1168 if (number == dev->config)
1171 if (gadget_is_sa1100 (gadget)
1173 && atomic_read (&dev->tx_qlen) != 0) {
1174 /* tx fifo is full, but we can't clear it...*/
1175 INFO (dev, "can't change configurations\n");
1178 eth_reset_config (dev);
1180 /* default: pass all packets, no multicast filtering */
1181 dev->cdc_filter = 0x000f;
1184 case DEV_CONFIG_VALUE:
1186 result = set_ether_config (dev, gfp_flags);
1188 #ifdef CONFIG_USB_ETH_RNDIS
1189 case DEV_RNDIS_CONFIG_VALUE:
1191 result = set_ether_config (dev, gfp_flags);
1202 eth_reset_config (dev);
1206 switch (gadget->speed) {
1207 case USB_SPEED_FULL: speed = "full"; break;
1208 #ifdef CONFIG_USB_GADGET_DUALSPEED
1209 case USB_SPEED_HIGH: speed = "high"; break;
1211 default: speed = "?"; break;
1214 dev->config = number;
1215 INFO (dev, "%s speed config #%d: %s, using %s\n",
1216 speed, number, driver_desc,
1221 : "CDC Ethernet Subset"));
1226 /*-------------------------------------------------------------------------*/
1228 /* section 3.8.2 table 11 of the CDC spec lists Ethernet notifications
1229 * section 3.6.2.1 table 5 specifies ACM notifications, accepted by RNDIS
1230 * and RNDIS also defines its own bit-incompatible notifications
1232 #define CDC_NOTIFY_NETWORK_CONNECTION 0x00 /* required; 6.3.1 */
1233 #define CDC_NOTIFY_RESPONSE_AVAILABLE 0x01 /* optional; 6.3.2 */
1234 #define CDC_NOTIFY_SPEED_CHANGE 0x2a /* required; 6.3.8 */
1236 #ifdef DEV_CONFIG_CDC
1238 struct cdc_notification {
1240 u8 bNotificationType;
1245 /* SPEED_CHANGE data looks like this */
1249 static void eth_status_complete (struct usb_ep *ep, struct usb_request *req)
1251 struct cdc_notification *event = req->buf;
1252 int value = req->status;
1253 struct eth_dev *dev = ep->driver_data;
1255 /* issue the second notification if host reads the first */
1256 if (event->bNotificationType == CDC_NOTIFY_NETWORK_CONNECTION
1258 event->bmRequestType = 0xA1;
1259 event->bNotificationType = CDC_NOTIFY_SPEED_CHANGE;
1260 event->wValue = __constant_cpu_to_le16 (0);
1261 event->wIndex = __constant_cpu_to_le16 (1);
1262 event->wLength = __constant_cpu_to_le16 (8);
1264 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1265 event->data [0] = event->data [1] =
1266 (dev->gadget->speed == USB_SPEED_HIGH)
1267 ? (13 * 512 * 8 * 1000 * 8)
1268 : (19 * 64 * 1 * 1000 * 8);
1271 value = usb_ep_queue (ep, req, GFP_ATOMIC);
1272 DEBUG (dev, "send SPEED_CHANGE --> %d\n", value);
1276 DEBUG (dev, "event %02x --> %d\n",
1277 event->bNotificationType, value);
1279 /* free when done */
1280 usb_ep_free_buffer (ep, req->buf, req->dma, 16);
1281 usb_ep_free_request (ep, req);
1284 static void issue_start_status (struct eth_dev *dev)
1286 struct usb_request *req;
1287 struct cdc_notification *event;
1290 DEBUG (dev, "%s, flush old status first\n", __FUNCTION__);
1294 * FIXME ugly idiom, maybe we'd be better with just
1295 * a "cancel the whole queue" primitive since any
1296 * unlink-one primitive has way too many error modes.
1297 * here, we "know" toggle is already clear...
1299 usb_ep_disable (dev->status_ep);
1300 usb_ep_enable (dev->status_ep, dev->status);
1302 /* FIXME make these allocations static like dev->req */
1303 req = usb_ep_alloc_request (dev->status_ep, GFP_ATOMIC);
1305 DEBUG (dev, "status ENOMEM\n");
1308 req->buf = usb_ep_alloc_buffer (dev->status_ep, 16,
1309 &dev->req->dma, GFP_ATOMIC);
1310 if (req->buf == 0) {
1311 DEBUG (dev, "status buf ENOMEM\n");
1313 usb_ep_free_request (dev->status_ep, req);
1317 /* 3.8.1 says to issue first NETWORK_CONNECTION, then
1318 * a SPEED_CHANGE. could be useful in some configs.
1321 event->bmRequestType = 0xA1;
1322 event->bNotificationType = CDC_NOTIFY_NETWORK_CONNECTION;
1323 event->wValue = __constant_cpu_to_le16 (1); /* connected */
1324 event->wIndex = __constant_cpu_to_le16 (1);
1328 req->complete = eth_status_complete;
1329 value = usb_ep_queue (dev->status_ep, req, GFP_ATOMIC);
1331 DEBUG (dev, "status buf queue --> %d\n", value);
1332 usb_ep_free_buffer (dev->status_ep,
1333 req->buf, dev->req->dma, 16);
1340 /*-------------------------------------------------------------------------*/
1342 static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req)
1344 if (req->status || req->actual != req->length)
1345 DEBUG ((struct eth_dev *) ep->driver_data,
1346 "setup complete --> %d, %d/%d\n",
1347 req->status, req->actual, req->length);
1350 /* see section 3.8.2 table 10 of the CDC spec for more ethernet
1351 * requests, mostly for filters (multicast, pm) and statistics
1352 * section 3.6.2.1 table 4 has ACM requests; RNDIS requires the
1353 * encapsulated command mechanism.
1355 #define CDC_SEND_ENCAPSULATED_COMMAND 0x00 /* optional */
1356 #define CDC_GET_ENCAPSULATED_RESPONSE 0x01 /* optional */
1357 #define CDC_SET_ETHERNET_MULTICAST_FILTERS 0x40 /* optional */
1358 #define CDC_SET_ETHERNET_PM_PATTERN_FILTER 0x41 /* optional */
1359 #define CDC_GET_ETHERNET_PM_PATTERN_FILTER 0x42 /* optional */
1360 #define CDC_SET_ETHERNET_PACKET_FILTER 0x43 /* required */
1361 #define CDC_GET_ETHERNET_STATISTIC 0x44 /* optional */
1363 /* table 62; bits in cdc_filter */
1364 #define CDC_PACKET_TYPE_PROMISCUOUS (1 << 0)
1365 #define CDC_PACKET_TYPE_ALL_MULTICAST (1 << 1) /* no filter */
1366 #define CDC_PACKET_TYPE_DIRECTED (1 << 2)
1367 #define CDC_PACKET_TYPE_BROADCAST (1 << 3)
1368 #define CDC_PACKET_TYPE_MULTICAST (1 << 4) /* filtered */
1370 #ifdef CONFIG_USB_ETH_RNDIS
1372 static void rndis_response_complete (struct usb_ep *ep, struct usb_request *req)
1374 if (req->status || req->actual != req->length)
1375 DEBUG (dev, "rndis response complete --> %d, %d/%d\n",
1376 req->status, req->actual, req->length);
1378 /* done sending after CDC_GET_ENCAPSULATED_RESPONSE */
1381 static void rndis_command_complete (struct usb_ep *ep, struct usb_request *req)
1383 struct eth_dev *dev = ep->driver_data;
1386 /* received RNDIS command from CDC_SEND_ENCAPSULATED_COMMAND */
1387 spin_lock(&dev->lock);
1388 status = rndis_msg_parser (dev->rndis_config, (u8 *) req->buf);
1390 ERROR(dev, "%s: rndis parse error %d\n", __FUNCTION__, status);
1391 spin_unlock(&dev->lock);
1397 * The setup() callback implements all the ep0 functionality that's not
1398 * handled lower down. CDC has a number of less-common features:
1400 * - two interfaces: control, and ethernet data
1401 * - Ethernet data interface has two altsettings: default, and active
1402 * - class-specific descriptors for the control interface
1403 * - class-specific control requests
1406 eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1408 struct eth_dev *dev = get_gadget_data (gadget);
1409 struct usb_request *req = dev->req;
1410 int value = -EOPNOTSUPP;
1412 /* descriptors just go into the pre-allocated ep0 buffer,
1413 * while config change events may enable network traffic.
1415 req->complete = eth_setup_complete;
1416 switch (ctrl->bRequest) {
1418 case USB_REQ_GET_DESCRIPTOR:
1419 if (ctrl->bRequestType != USB_DIR_IN)
1421 switch (ctrl->wValue >> 8) {
1424 value = min (ctrl->wLength, (u16) sizeof device_desc);
1425 memcpy (req->buf, &device_desc, value);
1427 #ifdef CONFIG_USB_GADGET_DUALSPEED
1428 case USB_DT_DEVICE_QUALIFIER:
1429 if (!gadget->is_dualspeed)
1431 value = min (ctrl->wLength, (u16) sizeof dev_qualifier);
1432 memcpy (req->buf, &dev_qualifier, value);
1435 case USB_DT_OTHER_SPEED_CONFIG:
1436 if (!gadget->is_dualspeed)
1439 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1441 value = config_buf (gadget->speed, req->buf,
1443 ctrl->wValue & 0xff,
1446 value = min (ctrl->wLength, (u16) value);
1450 value = usb_gadget_get_string (&stringtab,
1451 ctrl->wValue & 0xff, req->buf);
1453 value = min (ctrl->wLength, (u16) value);
1458 case USB_REQ_SET_CONFIGURATION:
1459 if (ctrl->bRequestType != 0)
1461 if (gadget->a_hnp_support)
1462 DEBUG (dev, "HNP available\n");
1463 else if (gadget->a_alt_hnp_support)
1464 DEBUG (dev, "HNP needs a different root port\n");
1465 spin_lock (&dev->lock);
1466 value = eth_set_config (dev, ctrl->wValue, GFP_ATOMIC);
1467 spin_unlock (&dev->lock);
1469 case USB_REQ_GET_CONFIGURATION:
1470 if (ctrl->bRequestType != USB_DIR_IN)
1472 *(u8 *)req->buf = dev->config;
1473 value = min (ctrl->wLength, (u16) 1);
1476 case USB_REQ_SET_INTERFACE:
1477 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1479 || ctrl->wIndex > 1)
1481 if (!dev->cdc && ctrl->wIndex != 0)
1483 spin_lock (&dev->lock);
1485 /* PXA hardware partially handles SET_INTERFACE;
1486 * we need to kluge around that interference.
1488 if (gadget_is_pxa (gadget)) {
1489 value = eth_set_config (dev, DEV_CONFIG_VALUE,
1494 #ifdef DEV_CONFIG_CDC
1495 switch (ctrl->wIndex) {
1496 case 0: /* control/master intf */
1497 if (ctrl->wValue != 0)
1499 if (dev->status_ep) {
1500 usb_ep_disable (dev->status_ep);
1501 usb_ep_enable (dev->status_ep, dev->status);
1505 case 1: /* data intf */
1506 if (ctrl->wValue > 1)
1508 usb_ep_disable (dev->in_ep);
1509 usb_ep_disable (dev->out_ep);
1511 /* CDC requires the data transfers not be done from
1512 * the default interface setting ... also, setting
1513 * the non-default interface clears filters etc.
1515 if (ctrl->wValue == 1) {
1516 usb_ep_enable (dev->in_ep, dev->in);
1517 usb_ep_enable (dev->out_ep, dev->out);
1518 netif_carrier_on (dev->net);
1520 issue_start_status (dev);
1521 if (netif_running (dev->net)) {
1522 spin_unlock (&dev->lock);
1523 eth_start (dev, GFP_ATOMIC);
1524 spin_lock (&dev->lock);
1527 netif_stop_queue (dev->net);
1528 netif_carrier_off (dev->net);
1534 /* FIXME this is wrong, as is the assumption that
1535 * all non-PXA hardware talks real CDC ...
1537 dev_warn (&gadget->dev, "set_interface ignored!\n");
1538 #endif /* DEV_CONFIG_CDC */
1541 spin_unlock (&dev->lock);
1543 case USB_REQ_GET_INTERFACE:
1544 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1546 || ctrl->wIndex > 1)
1548 if (!(dev->cdc || dev->rndis) && ctrl->wIndex != 0)
1551 /* for CDC, iff carrier is on, data interface is active. */
1552 if (dev->rndis || ctrl->wIndex != 1)
1553 *(u8 *)req->buf = 0;
1555 *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0;
1556 value = min (ctrl->wLength, (u16) 1);
1559 #ifdef DEV_CONFIG_CDC
1560 case CDC_SET_ETHERNET_PACKET_FILTER:
1561 /* see 6.2.30: no data, wIndex = interface,
1562 * wValue = packet filter bitmap
1564 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1567 || ctrl->wLength != 0
1568 || ctrl->wIndex > 1)
1570 DEBUG (dev, "NOP packet filter %04x\n", ctrl->wValue);
1571 /* NOTE: table 62 has 5 filter bits to reduce traffic,
1572 * and we "must" support multicast and promiscuous.
1573 * this NOP implements a bad filter (always promisc)
1575 dev->cdc_filter = ctrl->wValue;
1578 #endif /* DEV_CONFIG_CDC */
1580 #ifdef CONFIG_USB_ETH_RNDIS
1581 /* RNDIS uses the CDC command encapsulation mechanism to implement
1582 * an RPC scheme, with much getting/setting of attributes by OID.
1584 case CDC_SEND_ENCAPSULATED_COMMAND:
1585 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1587 || ctrl->wLength > USB_BUFSIZ
1589 || rndis_control_intf.bInterfaceNumber
1592 /* read the request, then process it */
1593 value = ctrl->wLength;
1594 req->complete = rndis_command_complete;
1595 /* later, rndis_control_ack () sends a notification */
1598 case CDC_GET_ENCAPSULATED_RESPONSE:
1599 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1600 == ctrl->bRequestType
1602 // && ctrl->wLength >= 0x0400
1604 && rndis_control_intf.bInterfaceNumber
1608 /* return the result */
1609 buf = rndis_get_next_response (dev->rndis_config,
1612 memcpy (req->buf, buf, value);
1613 req->complete = rndis_response_complete;
1614 rndis_free_response(dev->rndis_config, buf);
1616 /* else stalls ... spec says to avoid that */
1623 "unknown control req%02x.%02x v%04x i%04x l%d\n",
1624 ctrl->bRequestType, ctrl->bRequest,
1625 ctrl->wValue, ctrl->wIndex, ctrl->wLength);
1628 /* respond with data transfer before status phase? */
1630 req->length = value;
1631 req->zero = value < ctrl->wLength
1632 && (value % gadget->ep0->maxpacket) == 0;
1633 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1635 DEBUG (dev, "ep_queue --> %d\n", value);
1637 eth_setup_complete (gadget->ep0, req);
1641 /* host either stalls (value < 0) or reports success */
1646 eth_disconnect (struct usb_gadget *gadget)
1648 struct eth_dev *dev = get_gadget_data (gadget);
1649 unsigned long flags;
1651 spin_lock_irqsave (&dev->lock, flags);
1652 netif_stop_queue (dev->net);
1653 netif_carrier_off (dev->net);
1654 eth_reset_config (dev);
1655 spin_unlock_irqrestore (&dev->lock, flags);
1657 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1659 /* next we may get setup() calls to enumerate new connections;
1660 * or an unbind() during shutdown (including removing module).
1664 /*-------------------------------------------------------------------------*/
1666 /* NETWORK DRIVER HOOKUP (to the layer above this driver) */
1668 static int eth_change_mtu (struct net_device *net, int new_mtu)
1670 struct eth_dev *dev = (struct eth_dev *) net->priv;
1672 // FIXME if rndis, don't change while link's live
1674 if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN)
1676 /* no zero-length packet read wanted after mtu-sized packets */
1677 if (((new_mtu + sizeof (struct ethhdr)) % dev->in_ep->maxpacket) == 0)
1683 static struct net_device_stats *eth_get_stats (struct net_device *net)
1685 return &((struct eth_dev *) net->priv)->stats;
1688 static int eth_ethtool_ioctl (struct net_device *net, void __user *useraddr)
1690 struct eth_dev *dev = (struct eth_dev *) net->priv;
1693 if (get_user (cmd, (u32 __user *)useraddr))
1697 case ETHTOOL_GDRVINFO: { /* get driver info */
1698 struct ethtool_drvinfo info;
1700 memset (&info, 0, sizeof info);
1701 info.cmd = ETHTOOL_GDRVINFO;
1702 strlcpy (info.driver, shortname, sizeof info.driver);
1703 strlcpy (info.version, DRIVER_VERSION, sizeof info.version);
1704 strlcpy (info.fw_version, dev->gadget->name,
1705 sizeof info.fw_version);
1706 strlcpy (info.bus_info, dev->gadget->dev.bus_id,
1707 sizeof info.bus_info);
1708 if (copy_to_user (useraddr, &info, sizeof (info)))
1713 case ETHTOOL_GLINK: { /* get link status */
1714 struct ethtool_value edata = { ETHTOOL_GLINK };
1716 edata.data = (dev->gadget->speed != USB_SPEED_UNKNOWN);
1717 if (copy_to_user (useraddr, &edata, sizeof (edata)))
1723 /* Note that the ethtool user space code requires EOPNOTSUPP */
1727 static int eth_ioctl (struct net_device *net, struct ifreq *rq, int cmd)
1731 return eth_ethtool_ioctl(net, rq->ifr_data);
1737 static void defer_kevent (struct eth_dev *dev, int flag)
1739 if (test_and_set_bit (flag, &dev->todo))
1741 if (!schedule_work (&dev->work))
1742 ERROR (dev, "kevent %d may have been dropped\n", flag);
1744 DEBUG (dev, "kevent %d scheduled\n", flag);
1747 static void rx_complete (struct usb_ep *ep, struct usb_request *req);
1750 rx_submit (struct eth_dev *dev, struct usb_request *req, int gfp_flags)
1752 struct sk_buff *skb;
1753 int retval = -ENOMEM;
1756 /* Padding up to RX_EXTRA handles minor disagreements with host.
1757 * Normally we use the USB "terminate on short read" convention;
1758 * so allow up to (N*maxpacket), since that memory is normally
1759 * already allocated. Some hardware doesn't deal well with short
1760 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1761 * byte off the end (to force hardware errors on overflow).
1763 * RNDIS uses internal framing, and explicitly allows senders to
1764 * pad to end-of-packet. That's potentially nice for speed,
1765 * but means receivers can't recover synch on their own.
1767 size = (sizeof (struct ethhdr) + dev->net->mtu + RX_EXTRA);
1768 size += dev->out_ep->maxpacket - 1;
1769 #ifdef CONFIG_USB_ETH_RNDIS
1771 size += sizeof (struct rndis_packet_msg_type);
1773 size -= size % dev->out_ep->maxpacket;
1775 if ((skb = alloc_skb (size, gfp_flags)) == 0) {
1776 DEBUG (dev, "no rx skb\n");
1780 req->buf = skb->data;
1782 req->complete = rx_complete;
1785 retval = usb_ep_queue (dev->out_ep, req, gfp_flags);
1786 if (retval == -ENOMEM)
1788 defer_kevent (dev, WORK_RX_MEMORY);
1790 DEBUG (dev, "rx submit --> %d\n", retval);
1791 dev_kfree_skb_any (skb);
1792 spin_lock (&dev->lock);
1793 list_add (&req->list, &dev->rx_reqs);
1794 spin_unlock (&dev->lock);
1799 static void rx_complete (struct usb_ep *ep, struct usb_request *req)
1801 struct sk_buff *skb = req->context;
1802 struct eth_dev *dev = ep->driver_data;
1803 int status = req->status;
1807 /* normal completion */
1809 skb_put (skb, req->actual);
1810 #ifdef CONFIG_USB_ETH_RNDIS
1811 /* we know MaxPacketsPerTransfer == 1 here */
1813 rndis_rm_hdr (req->buf, &(skb->len));
1815 if (ETH_HLEN > skb->len || skb->len > ETH_FRAME_LEN) {
1816 dev->stats.rx_errors++;
1817 dev->stats.rx_length_errors++;
1818 DEBUG (dev, "rx length %d\n", skb->len);
1822 skb->dev = dev->net;
1823 skb->protocol = eth_type_trans (skb, dev->net);
1824 dev->stats.rx_packets++;
1825 dev->stats.rx_bytes += skb->len;
1827 /* no buffer copies needed, unless hardware can't
1830 status = netif_rx (skb);
1834 /* software-driven interface shutdown */
1835 case -ECONNRESET: // unlink
1836 case -ESHUTDOWN: // disconnect etc
1837 VDEBUG (dev, "rx shutdown, code %d\n", status);
1840 /* for hardware automagic (such as pxa) */
1841 case -ECONNABORTED: // endpoint reset
1842 DEBUG (dev, "rx %s reset\n", ep->name);
1843 defer_kevent (dev, WORK_RX_MEMORY);
1845 dev_kfree_skb_any (skb);
1850 dev->stats.rx_over_errors++;
1854 dev->stats.rx_errors++;
1855 DEBUG (dev, "rx status %d\n", status);
1860 dev_kfree_skb_any (skb);
1861 if (!netif_running (dev->net)) {
1863 /* nobody reading rx_reqs, so no dev->lock */
1864 list_add (&req->list, &dev->rx_reqs);
1868 rx_submit (dev, req, GFP_ATOMIC);
1871 static int prealloc (struct list_head *list, struct usb_ep *ep,
1872 unsigned n, int gfp_flags)
1875 struct usb_request *req;
1880 /* queue/recycle up to N requests */
1882 list_for_each_entry (req, list, list) {
1887 req = usb_ep_alloc_request (ep, gfp_flags);
1889 return list_empty (list) ? -ENOMEM : 0;
1890 list_add (&req->list, list);
1897 struct list_head *next;
1899 next = req->list.next;
1900 list_del (&req->list);
1901 usb_ep_free_request (ep, req);
1906 req = container_of (next, struct usb_request, list);
1911 static int alloc_requests (struct eth_dev *dev, unsigned n, int gfp_flags)
1915 status = prealloc (&dev->tx_reqs, dev->in_ep, n, gfp_flags);
1918 status = prealloc (&dev->rx_reqs, dev->out_ep, n, gfp_flags);
1923 DEBUG (dev, "can't alloc requests\n");
1927 static void rx_fill (struct eth_dev *dev, int gfp_flags)
1929 struct usb_request *req;
1930 unsigned long flags;
1932 clear_bit (WORK_RX_MEMORY, &dev->todo);
1934 /* fill unused rxq slots with some skb */
1935 spin_lock_irqsave (&dev->lock, flags);
1936 while (!list_empty (&dev->rx_reqs)) {
1937 req = container_of (dev->rx_reqs.next,
1938 struct usb_request, list);
1939 list_del_init (&req->list);
1940 spin_unlock_irqrestore (&dev->lock, flags);
1942 if (rx_submit (dev, req, gfp_flags) < 0) {
1943 defer_kevent (dev, WORK_RX_MEMORY);
1947 spin_lock_irqsave (&dev->lock, flags);
1949 spin_unlock_irqrestore (&dev->lock, flags);
1952 static void eth_work (void *_dev)
1954 struct eth_dev *dev = _dev;
1956 if (test_bit (WORK_RX_MEMORY, &dev->todo)) {
1957 if (netif_running (dev->net))
1958 rx_fill (dev, GFP_KERNEL);
1960 clear_bit (WORK_RX_MEMORY, &dev->todo);
1964 DEBUG (dev, "work done, flags = 0x%lx\n", dev->todo);
1967 static void tx_complete (struct usb_ep *ep, struct usb_request *req)
1969 struct sk_buff *skb = req->context;
1970 struct eth_dev *dev = ep->driver_data;
1972 switch (req->status) {
1974 dev->stats.tx_errors++;
1975 VDEBUG (dev, "tx err %d\n", req->status);
1977 case -ECONNRESET: // unlink
1978 case -ESHUTDOWN: // disconnect etc
1981 dev->stats.tx_bytes += skb->len;
1983 dev->stats.tx_packets++;
1985 spin_lock (&dev->lock);
1986 list_add (&req->list, &dev->tx_reqs);
1987 spin_unlock (&dev->lock);
1988 dev_kfree_skb_any (skb);
1990 atomic_dec (&dev->tx_qlen);
1991 if (netif_carrier_ok (dev->net))
1992 netif_wake_queue (dev->net);
1995 static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1997 struct eth_dev *dev = (struct eth_dev *) net->priv;
1998 int length = skb->len;
2000 struct usb_request *req = NULL;
2001 unsigned long flags;
2003 /* FIXME check dev->cdc_filter to decide whether to send this,
2004 * instead of acting as if CDC_PACKET_TYPE_PROMISCUOUS were
2005 * always set. RNDIS has the same kind of outgoing filter.
2008 spin_lock_irqsave (&dev->lock, flags);
2009 req = container_of (dev->tx_reqs.next, struct usb_request, list);
2010 list_del (&req->list);
2011 if (list_empty (&dev->tx_reqs))
2012 netif_stop_queue (net);
2013 spin_unlock_irqrestore (&dev->lock, flags);
2015 /* no buffer copies needed, unless the network stack did it
2016 * or the hardware can't use skb buffers.
2017 * or there's not enough space for any RNDIS headers we need
2019 #ifdef CONFIG_USB_ETH_RNDIS
2021 struct sk_buff *skb_rndis;
2023 skb_rndis = skb_realloc_headroom (skb,
2024 sizeof (struct rndis_packet_msg_type));
2028 dev_kfree_skb_any (skb);
2030 rndis_add_hdr (skb);
2034 req->buf = skb->data;
2036 req->complete = tx_complete;
2038 /* use zlp framing on tx for strict CDC-Ether conformance,
2039 * though any robust network rx path ignores extra padding.
2040 * and some hardware doesn't like to write zlps.
2043 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2046 req->length = length;
2048 #ifdef CONFIG_USB_GADGET_DUALSPEED
2049 /* throttle highspeed IRQ rate back slightly */
2050 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2051 ? ((atomic_read (&dev->tx_qlen) % TX_DELAY) != 0)
2055 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
2058 DEBUG (dev, "tx queue err %d\n", retval);
2061 net->trans_start = jiffies;
2062 atomic_inc (&dev->tx_qlen);
2066 #ifdef CONFIG_USB_ETH_RNDIS
2069 dev->stats.tx_dropped++;
2070 dev_kfree_skb_any (skb);
2071 spin_lock_irqsave (&dev->lock, flags);
2072 if (list_empty (&dev->tx_reqs))
2073 netif_start_queue (net);
2074 list_add (&req->list, &dev->tx_reqs);
2075 spin_unlock_irqrestore (&dev->lock, flags);
2080 /*-------------------------------------------------------------------------*/
2082 #ifdef CONFIG_USB_ETH_RNDIS
2084 static void rndis_send_media_state (struct eth_dev *dev, int connect)
2090 if (rndis_signal_connect (dev->rndis_config))
2093 if (rndis_signal_disconnect (dev->rndis_config))
2098 static void rndis_control_ack_complete (struct usb_ep *ep, struct usb_request *req)
2100 if (req->status || req->actual != req->length)
2101 DEBUG (dev, "rndis control ack complete --> %d, %d/%d\n",
2102 req->status, req->actual, req->length);
2104 usb_ep_free_buffer(ep, req->buf, req->dma, 8);
2105 usb_ep_free_request(ep, req);
2108 static int rndis_control_ack (struct net_device *net)
2110 struct eth_dev *dev = (struct eth_dev *) net->priv;
2112 struct usb_request *resp;
2114 /* in case RNDIS calls this after disconnect */
2115 if (!dev->status_ep) {
2116 DEBUG (dev, "status ENODEV\n");
2120 /* Allocate memory for notification ie. ACK */
2121 resp = usb_ep_alloc_request (dev->status_ep, GFP_ATOMIC);
2123 DEBUG (dev, "status ENOMEM\n");
2127 resp->buf = usb_ep_alloc_buffer (dev->status_ep, 8,
2128 &resp->dma, GFP_ATOMIC);
2130 DEBUG (dev, "status buf ENOMEM\n");
2131 usb_ep_free_request (dev->status_ep, resp);
2135 /* Send RNDIS RESPONSE_AVAILABLE notification;
2136 * CDC_NOTIFY_RESPONSE_AVAILABLE should work too
2139 resp->complete = rndis_control_ack_complete;
2141 *((u32 *) resp->buf) = __constant_cpu_to_le32 (1);
2142 *((u32 *) resp->buf + 1) = __constant_cpu_to_le32 (0);
2144 length = usb_ep_queue (dev->status_ep, resp, GFP_ATOMIC);
2147 rndis_control_ack_complete (dev->status_ep, resp);
2155 static void eth_start (struct eth_dev *dev, int gfp_flags)
2157 DEBUG (dev, "%s\n", __FUNCTION__);
2159 /* fill the rx queue */
2160 rx_fill (dev, gfp_flags);
2162 /* and open the tx floodgates */
2163 atomic_set (&dev->tx_qlen, 0);
2164 netif_wake_queue (dev->net);
2165 #ifdef CONFIG_USB_ETH_RNDIS
2167 rndis_set_param_medium (dev->rndis_config,
2169 BITRATE(dev->gadget));
2170 rndis_send_media_state (dev, 1);
2175 static int eth_open (struct net_device *net)
2177 struct eth_dev *dev = (struct eth_dev *) net->priv;
2179 DEBUG (dev, "%s\n", __FUNCTION__);
2180 if (netif_carrier_ok (dev->net))
2181 eth_start (dev, GFP_KERNEL);
2185 static int eth_stop (struct net_device *net)
2187 struct eth_dev *dev = (struct eth_dev *) net->priv;
2189 VDEBUG (dev, "%s\n", __FUNCTION__);
2190 netif_stop_queue (net);
2192 DEBUG (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
2193 dev->stats.rx_packets, dev->stats.tx_packets,
2194 dev->stats.rx_errors, dev->stats.tx_errors
2197 /* ensure there are no more active requests */
2198 if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
2199 usb_ep_disable (dev->in_ep);
2200 usb_ep_disable (dev->out_ep);
2201 if (netif_carrier_ok (dev->net)) {
2202 DEBUG (dev, "host still using in/out endpoints\n");
2203 // FIXME idiom may leave toggle wrong here
2204 usb_ep_enable (dev->in_ep, dev->in);
2205 usb_ep_enable (dev->out_ep, dev->out);
2207 if (dev->status_ep) {
2208 usb_ep_disable (dev->status_ep);
2209 usb_ep_enable (dev->status_ep, dev->status);
2213 #ifdef CONFIG_USB_ETH_RNDIS
2215 rndis_set_param_medium (dev->rndis_config,
2216 NDIS_MEDIUM_802_3, 0);
2217 rndis_send_media_state (dev, 0);
2224 /*-------------------------------------------------------------------------*/
2227 eth_unbind (struct usb_gadget *gadget)
2229 struct eth_dev *dev = get_gadget_data (gadget);
2231 DEBUG (dev, "unbind\n");
2232 #ifdef CONFIG_USB_ETH_RNDIS
2233 rndis_deregister (dev->rndis_config);
2237 /* we've already been disconnected ... no i/o is active */
2239 usb_ep_free_buffer (gadget->ep0,
2240 dev->req->buf, dev->req->dma,
2242 usb_ep_free_request (gadget->ep0, dev->req);
2246 unregister_netdev (dev->net);
2247 free_netdev(dev->net);
2249 /* assuming we used keventd, it must quiesce too */
2250 flush_scheduled_work ();
2251 set_gadget_data (gadget, NULL);
2254 static u8 __init nibble (unsigned char c)
2256 if (likely (isdigit (c)))
2259 if (likely (isxdigit (c)))
2260 return 10 + c - 'A';
2264 static void __init get_ether_addr (const char *str, u8 *dev_addr)
2269 for (i = 0; i < 6; i++) {
2272 if((*str == '.') || (*str == ':'))
2274 num = nibble(*str++) << 4;
2275 num |= (nibble(*str++));
2278 if (is_valid_ether_addr (dev_addr))
2281 random_ether_addr(dev_addr);
2285 eth_bind (struct usb_gadget *gadget)
2287 struct eth_dev *dev;
2288 struct net_device *net;
2289 u8 cdc = 1, zlp = 1, rndis = 1;
2291 int status = -ENOMEM;
2293 /* these flags are only ever cleared; compiler take note */
2294 #ifndef DEV_CONFIG_CDC
2297 #ifndef CONFIG_USB_ETH_RNDIS
2301 /* Because most host side USB stacks handle CDC Ethernet, that
2302 * standard protocol is _strongly_ preferred for interop purposes.
2303 * (By everyone except Microsoft.)
2305 if (gadget_is_net2280 (gadget)) {
2306 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0201);
2307 } else if (gadget_is_dummy (gadget)) {
2308 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0202);
2309 } else if (gadget_is_pxa (gadget)) {
2310 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0203);
2311 /* pxa doesn't support altsettings */
2313 } else if (gadget_is_sh(gadget)) {
2314 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0204);
2315 /* sh doesn't support multiple interfaces or configs */
2318 } else if (gadget_is_sa1100 (gadget)) {
2319 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0205);
2320 /* hardware can't write zlps */
2322 /* sa1100 CAN do CDC, without status endpoint ... we use
2323 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2326 } else if (gadget_is_goku (gadget)) {
2327 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0206);
2328 } else if (gadget_is_mq11xx (gadget)) {
2329 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0207);
2330 } else if (gadget_is_omap (gadget)) {
2331 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0208);
2333 /* can't assume CDC works. don't want to default to
2334 * anything less functional on CDC-capable hardware,
2335 * so we fail in this case.
2337 dev_err (&gadget->dev,
2338 "controller '%s' not recognized\n",
2342 snprintf (manufacturer, sizeof manufacturer,
2343 UTS_SYSNAME " " UTS_RELEASE "/%s",
2346 /* If there's an RNDIS configuration, that's what Windows wants to
2347 * be using ... so use these product IDs here and in the "linux.inf"
2348 * needed to install MSFT drivers. Current Linux kernels will use
2349 * the second configuration if it's CDC Ethernet, and need some help
2350 * to choose the right configuration otherwise.
2353 device_desc.idVendor =
2354 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2355 device_desc.idProduct =
2356 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2357 snprintf (product_desc, sizeof product_desc,
2358 "RNDIS/%s", driver_desc);
2360 /* CDC subset ... recognized by Linux since 2.4.10, but Windows
2361 * drivers aren't widely available.
2364 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2365 device_desc.idVendor =
2366 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2367 device_desc.idProduct =
2368 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2371 /* support optional vendor/distro customization */
2374 dev_err (&gadget->dev, "idVendor needs idProduct!\n");
2377 device_desc.idVendor = cpu_to_le16(idVendor);
2378 device_desc.idProduct = cpu_to_le16(idProduct);
2380 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2383 strlcpy (manufacturer, iManufacturer, sizeof manufacturer);
2385 strlcpy (product_desc, iProduct, sizeof product_desc);
2387 /* all we really need is bulk IN/OUT */
2388 usb_ep_autoconfig_reset (gadget);
2389 ep = usb_ep_autoconfig (gadget, &fs_source_desc);
2392 dev_err (&gadget->dev,
2393 "can't autoconfigure on %s\n",
2397 EP_IN_NAME = ep->name;
2398 ep->driver_data = ep; /* claim */
2400 ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
2403 EP_OUT_NAME = ep->name;
2404 ep->driver_data = ep; /* claim */
2406 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2407 /* CDC Ethernet control interface doesn't require a status endpoint.
2408 * Since some hosts expect one, try to allocate one anyway.
2411 ep = usb_ep_autoconfig (gadget, &fs_status_desc);
2413 EP_STATUS_NAME = ep->name;
2414 ep->driver_data = ep; /* claim */
2416 dev_err (&gadget->dev,
2417 "can't run RNDIS on %s\n",
2421 control_intf.bNumEndpoints = 0;
2422 /* FIXME remove endpoint from descriptor list */
2427 /* one config: cdc, else minimal subset */
2429 eth_config.bNumInterfaces = 1;
2430 eth_config.iConfiguration = STRING_SUBSET;
2431 fs_subset_descriptors();
2432 hs_subset_descriptors();
2435 /* For now RNDIS is always a second config */
2437 device_desc.bNumConfigurations = 2;
2439 #ifdef CONFIG_USB_GADGET_DUALSPEED
2441 dev_qualifier.bNumConfigurations = 2;
2443 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2445 /* assumes ep0 uses the same value for both speeds ... */
2446 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2448 /* and that all endpoints are dual-speed */
2449 hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
2450 hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
2451 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2453 hs_status_desc.bEndpointAddress =
2454 fs_status_desc.bEndpointAddress;
2456 #endif /* DUALSPEED */
2458 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
2459 usb_gadget_set_selfpowered (gadget);
2461 if (gadget->is_otg) {
2462 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2463 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2464 #ifdef CONFIG_USB_ETH_RNDIS
2465 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2469 net = alloc_etherdev (sizeof *dev);
2473 spin_lock_init (&dev->lock);
2474 INIT_WORK (&dev->work, eth_work, dev);
2475 INIT_LIST_HEAD (&dev->tx_reqs);
2476 INIT_LIST_HEAD (&dev->rx_reqs);
2478 /* network device setup */
2480 SET_MODULE_OWNER (net);
2481 strcpy (net->name, "usb%d");
2485 /* Module params for these addresses should come from ID proms.
2486 * The host side address is used with CDC and RNDIS, and commonly
2487 * ends up in a persistent config database.
2489 get_ether_addr(dev_addr, net->dev_addr);
2491 get_ether_addr(host_addr, dev->host_mac);
2492 #ifdef DEV_CONFIG_CDC
2493 snprintf (ethaddr, sizeof ethaddr, "%02X%02X%02X%02X%02X%02X",
2494 dev->host_mac [0], dev->host_mac [1],
2495 dev->host_mac [2], dev->host_mac [3],
2496 dev->host_mac [4], dev->host_mac [5]);
2501 status = rndis_init();
2503 dev_err (&gadget->dev, "can't init RNDIS, %d\n",
2509 net->change_mtu = eth_change_mtu;
2510 net->get_stats = eth_get_stats;
2511 net->hard_start_xmit = eth_start_xmit;
2512 net->open = eth_open;
2513 net->stop = eth_stop;
2514 // watchdog_timeo, tx_timeout ...
2515 // set_multicast_list
2516 net->do_ioctl = eth_ioctl;
2518 /* preallocate control response and buffer */
2519 dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
2522 dev->req->complete = eth_setup_complete;
2523 dev->req->buf = usb_ep_alloc_buffer (gadget->ep0, USB_BUFSIZ,
2524 &dev->req->dma, GFP_KERNEL);
2525 if (!dev->req->buf) {
2526 usb_ep_free_request (gadget->ep0, dev->req);
2530 /* finish hookup to lower layer ... */
2531 dev->gadget = gadget;
2532 set_gadget_data (gadget, dev);
2533 gadget->ep0->driver_data = dev;
2535 /* two kinds of host-initiated state changes:
2536 * - iff DATA transfer is active, carrier is "on"
2537 * - tx queueing enabled if open *and* carrier is "on"
2539 netif_stop_queue (dev->net);
2540 netif_carrier_off (dev->net);
2542 // SET_NETDEV_DEV (dev->net, &gadget->dev);
2543 status = register_netdev (dev->net);
2547 INFO (dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
2548 INFO (dev, "using %s, OUT %s IN %s%s%s\n", gadget->name,
2549 EP_OUT_NAME, EP_IN_NAME,
2550 EP_STATUS_NAME ? " STATUS " : "",
2551 EP_STATUS_NAME ? EP_STATUS_NAME : ""
2553 INFO (dev, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2554 net->dev_addr [0], net->dev_addr [1],
2555 net->dev_addr [2], net->dev_addr [3],
2556 net->dev_addr [4], net->dev_addr [5]);
2559 INFO (dev, "HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2560 dev->host_mac [0], dev->host_mac [1],
2561 dev->host_mac [2], dev->host_mac [3],
2562 dev->host_mac [4], dev->host_mac [5]);
2564 #ifdef CONFIG_USB_ETH_RNDIS
2568 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2570 dev->rndis_config = rndis_register (rndis_control_ack);
2571 if (dev->rndis_config < 0) {
2573 unregister_netdev (dev->net);
2578 /* these set up a lot of the OIDs that RNDIS needs */
2579 rndis_set_host_mac (dev->rndis_config, dev->host_mac);
2580 if (rndis_set_param_dev (dev->rndis_config, dev->net,
2583 if (rndis_set_param_vendor (dev->rndis_config, vendorID,
2586 if (rndis_set_param_medium (dev->rndis_config,
2590 INFO (dev, "RNDIS ready\n");
2597 dev_dbg(&gadget->dev, "register_netdev failed, %d\n", status);
2599 eth_unbind (gadget);
2603 /*-------------------------------------------------------------------------*/
2606 eth_suspend (struct usb_gadget *gadget)
2608 struct eth_dev *dev = get_gadget_data (gadget);
2610 DEBUG (dev, "suspend\n");
2615 eth_resume (struct usb_gadget *gadget)
2617 struct eth_dev *dev = get_gadget_data (gadget);
2619 DEBUG (dev, "resume\n");
2623 /*-------------------------------------------------------------------------*/
2625 static struct usb_gadget_driver eth_driver = {
2626 #ifdef CONFIG_USB_GADGET_DUALSPEED
2627 .speed = USB_SPEED_HIGH,
2629 .speed = USB_SPEED_FULL,
2631 .function = (char *) driver_desc,
2633 .unbind = eth_unbind,
2636 .disconnect = eth_disconnect,
2638 .suspend = eth_suspend,
2639 .resume = eth_resume,
2642 .name = (char *) shortname,
2649 MODULE_DESCRIPTION (DRIVER_DESC);
2650 MODULE_AUTHOR ("David Brownell, Benedikt Spanger");
2651 MODULE_LICENSE ("GPL");
2654 static int __init init (void)
2656 return usb_gadget_register_driver (ð_driver);
2660 static void __exit cleanup (void)
2662 usb_gadget_unregister_driver (ð_driver);
2664 module_exit (cleanup);