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;
123 #define WORK_RX_MEMORY 0
125 u8 host_mac [ETH_ALEN];
128 /* This version autoconfigures as much as possible at run-time.
130 * It also ASSUMES a self-powered device, without remote wakeup,
131 * although remote wakeup support would make sense.
133 static const char *EP_IN_NAME;
134 static const char *EP_OUT_NAME;
135 static const char *EP_STATUS_NAME;
137 /*-------------------------------------------------------------------------*/
139 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
140 * Instead: allocate your own, using normal USB-IF procedures.
143 /* Thanks to NetChip Technologies for donating this product ID.
144 * It's for devices with only CDC Ethernet configurations.
146 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
147 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
149 /* For hardware that can't talk CDC, we use the same vendor ID that
150 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
151 * with pxa250. We're protocol-compatible, if the host-side drivers
152 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
153 * drivers that need to hard-wire endpoint numbers have a hook.
155 * The protocol is a minimal subset of CDC Ether, which works on any bulk
156 * hardware that's not deeply broken ... even on hardware that can't talk
157 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
158 * doesn't handle control-OUT).
160 #define SIMPLE_VENDOR_NUM 0x049f
161 #define SIMPLE_PRODUCT_NUM 0x505a
163 /* For hardware that can talk RNDIS and either of the above protocols,
164 * use this ID ... the windows INF files will know it. Unless it's
165 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
166 * the non-RNDIS configuration.
168 #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
169 #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
172 /* Some systems will want different product identifers published in the
173 * device descriptor, either numbers or strings or both. These string
174 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
177 static ushort __initdata idVendor;
178 module_param(idVendor, ushort, S_IRUGO);
179 MODULE_PARM_DESC(idVendor, "USB Vendor ID");
181 static ushort __initdata idProduct;
182 module_param(idProduct, ushort, S_IRUGO);
183 MODULE_PARM_DESC(idProduct, "USB Product ID");
185 static ushort __initdata bcdDevice;
186 module_param(bcdDevice, ushort, S_IRUGO);
187 MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
189 static char *__initdata iManufacturer;
190 module_param(iManufacturer, charp, S_IRUGO);
191 MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
193 static char *__initdata iProduct;
194 module_param(iProduct, charp, S_IRUGO);
195 MODULE_PARM_DESC(iProduct, "USB Product string");
197 /* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */
198 static char *__initdata dev_addr;
199 module_param(dev_addr, charp, S_IRUGO);
200 MODULE_PARM_DESC(iProduct, "Device Ethernet Address");
202 /* this address is invisible to ifconfig */
203 static char *__initdata host_addr;
204 module_param(host_addr, charp, S_IRUGO);
205 MODULE_PARM_DESC(host_addr, "Host Ethernet Address");
208 /*-------------------------------------------------------------------------*/
210 /* Include CDC support if we could run on CDC-capable hardware. */
212 #ifdef CONFIG_USB_GADGET_NET2280
213 #define DEV_CONFIG_CDC
216 #ifdef CONFIG_USB_GADGET_DUMMY_HCD
217 #define DEV_CONFIG_CDC
220 #ifdef CONFIG_USB_GADGET_GOKU
221 #define DEV_CONFIG_CDC
224 #ifdef CONFIG_USB_GADGET_MQ11XX
225 #define DEV_CONFIG_CDC
228 #ifdef CONFIG_USB_GADGET_OMAP
229 #define DEV_CONFIG_CDC
233 /* For CDC-incapable hardware, choose the simple cdc subset.
234 * Anything that talks bulk (without notable bugs) can do this.
236 #ifdef CONFIG_USB_GADGET_PXA
237 #define DEV_CONFIG_SUBSET
240 #ifdef CONFIG_USB_GADGET_SH
241 #define DEV_CONFIG_SUBSET
244 #ifdef CONFIG_USB_GADGET_SA1100
245 /* use non-CDC for backwards compatibility */
246 #define DEV_CONFIG_SUBSET
250 /*-------------------------------------------------------------------------*/
252 #define DEFAULT_QLEN 2 /* double buffering by default */
254 #ifdef CONFIG_USB_GADGET_DUALSPEED
256 static unsigned qmult = 5;
257 module_param (qmult, uint, S_IRUGO|S_IWUSR);
260 /* for dual-speed hardware, use deeper queues at highspeed */
261 #define qlen(gadget) \
262 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
264 /* also defer IRQs on highspeed TX */
265 #define TX_DELAY qmult
267 #define BITRATE(g) ((g->speed == USB_SPEED_HIGH) ? 4800000 : 120000)
269 #else /* full speed (low speed doesn't do bulk) */
270 #define qlen(gadget) DEFAULT_QLEN
272 #define BITRATE(g) (12000)
276 /*-------------------------------------------------------------------------*/
278 #define xprintk(d,level,fmt,args...) \
279 printk(level "%s: " fmt , (d)->net->name , ## args)
283 #define DEBUG(dev,fmt,args...) \
284 xprintk(dev , KERN_DEBUG , fmt , ## args)
286 #define DEBUG(dev,fmt,args...) \
293 #define VDEBUG(dev,fmt,args...) \
297 #define ERROR(dev,fmt,args...) \
298 xprintk(dev , KERN_ERR , fmt , ## args)
299 #define WARN(dev,fmt,args...) \
300 xprintk(dev , KERN_WARNING , fmt , ## args)
301 #define INFO(dev,fmt,args...) \
302 xprintk(dev , KERN_INFO , fmt , ## args)
304 /*-------------------------------------------------------------------------*/
306 /* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
307 * ep0 implementation: descriptors, config management, setup().
308 * also optional class-specific notification interrupt transfer.
312 * DESCRIPTORS ... most are static, but strings and (full) configuration
313 * descriptors are built on demand. For now we do either full CDC, or
314 * our simple subset, with RNDIS as an optional second configuration.
316 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
317 * the class descriptors match a modem (they're ignored; it's really just
318 * Ethernet functionality), they don't need the NOP altsetting, and the
319 * status transfer endpoint isn't optional.
322 #define STRING_MANUFACTURER 1
323 #define STRING_PRODUCT 2
324 #define STRING_ETHADDR 3
325 #define STRING_DATA 4
326 #define STRING_CONTROL 5
327 #define STRING_RNDIS_CONTROL 6
329 #define STRING_SUBSET 8
330 #define STRING_RNDIS 9
332 #define USB_BUFSIZ 256 /* holds our biggest descriptor */
335 * This device advertises one configuration, eth_config, unless RNDIS
336 * is enabled (rndis_config) on hardware supporting at least two configs.
338 * NOTE: Controllers like superh_udc should probably be able to use
339 * an RNDIS-only configuration.
342 #define DEV_CONFIG_VALUE 1 /* cdc or subset */
343 #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
345 static struct usb_device_descriptor
347 .bLength = sizeof device_desc,
348 .bDescriptorType = USB_DT_DEVICE,
350 .bcdUSB = __constant_cpu_to_le16 (0x0200),
352 .bDeviceClass = USB_CLASS_COMM,
353 .bDeviceSubClass = 0,
354 .bDeviceProtocol = 0,
356 .idVendor = __constant_cpu_to_le16 (CDC_VENDOR_NUM),
357 .idProduct = __constant_cpu_to_le16 (CDC_PRODUCT_NUM),
358 .iManufacturer = STRING_MANUFACTURER,
359 .iProduct = STRING_PRODUCT,
360 .bNumConfigurations = 1,
363 static struct usb_config_descriptor
365 .bLength = sizeof eth_config,
366 .bDescriptorType = USB_DT_CONFIG,
368 /* compute wTotalLength on the fly */
370 .bConfigurationValue = DEV_CONFIG_VALUE,
371 .iConfiguration = STRING_CDC,
372 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
376 #ifdef CONFIG_USB_ETH_RNDIS
377 static const struct usb_config_descriptor
379 .bLength = sizeof rndis_config,
380 .bDescriptorType = USB_DT_CONFIG,
382 /* compute wTotalLength on the fly */
384 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
385 .iConfiguration = STRING_RNDIS,
386 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
392 * Compared to the simple CDC subset, the full CDC Ethernet model adds
393 * three class descriptors, two interface descriptors, optional status
394 * endpoint. Both have a "data" interface and two bulk endpoints.
395 * There are also differences in how control requests are handled.
397 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of
398 * the CDC-ACM (modem) spec.
401 #ifdef DEV_CONFIG_CDC
402 static struct usb_interface_descriptor
404 .bLength = sizeof control_intf,
405 .bDescriptorType = USB_DT_INTERFACE,
407 .bInterfaceNumber = 0,
408 /* status endpoint is optional; this may be patched later */
410 .bInterfaceClass = USB_CLASS_COMM,
411 .bInterfaceSubClass = 6, /* ethernet control model */
412 .bInterfaceProtocol = 0,
413 .iInterface = STRING_CONTROL,
417 #ifdef CONFIG_USB_ETH_RNDIS
418 static const struct usb_interface_descriptor
419 rndis_control_intf = {
420 .bLength = sizeof rndis_control_intf,
421 .bDescriptorType = USB_DT_INTERFACE,
423 .bInterfaceNumber = 0,
425 .bInterfaceClass = USB_CLASS_COMM,
426 .bInterfaceSubClass = 2, /* abstract control model */
427 .bInterfaceProtocol = 0xff, /* vendor specific */
428 .iInterface = STRING_RNDIS_CONTROL,
432 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
434 /* "Header Functional Descriptor" from CDC spec 5.2.3.1 */
438 u8 bDescriptorSubType;
441 } __attribute__ ((packed));
443 static const struct header_desc header_desc = {
444 .bLength = sizeof header_desc,
445 .bDescriptorType = USB_DT_CS_INTERFACE,
446 .bDescriptorSubType = 0,
448 .bcdCDC = __constant_cpu_to_le16 (0x0110),
451 /* "Union Functional Descriptor" from CDC spec 5.2.3.8 */
455 u8 bDescriptorSubType;
457 u8 bMasterInterface0;
459 /* ... and there could be other slave interfaces */
460 } __attribute__ ((packed));
462 static const struct union_desc union_desc = {
463 .bLength = sizeof union_desc,
464 .bDescriptorType = USB_DT_CS_INTERFACE,
465 .bDescriptorSubType = 6,
467 .bMasterInterface0 = 0, /* index of control interface */
468 .bSlaveInterface0 = 1, /* index of DATA interface */
471 #endif /* CDC || RNDIS */
473 #ifdef CONFIG_USB_ETH_RNDIS
475 /* "Call Management Descriptor" from CDC spec 5.2.3.3 */
476 struct call_mgmt_descriptor {
479 u8 bDescriptorSubType;
483 } __attribute__ ((packed));
485 static const struct call_mgmt_descriptor call_mgmt_descriptor = {
486 .bLength = sizeof call_mgmt_descriptor,
487 .bDescriptorType = USB_DT_CS_INTERFACE,
488 .bDescriptorSubType = 0x01,
490 .bmCapabilities = 0x00,
491 .bDataInterface = 0x01,
495 /* "Abstract Control Management Descriptor" from CDC spec 5.2.3.4 */
496 struct acm_descriptor {
499 u8 bDescriptorSubType;
502 } __attribute__ ((packed));
504 static struct acm_descriptor acm_descriptor = {
505 .bLength = sizeof acm_descriptor,
506 .bDescriptorType = USB_DT_CS_INTERFACE,
507 .bDescriptorSubType = 0x02,
509 .bmCapabilities = 0X00,
514 #ifdef DEV_CONFIG_CDC
516 /* "Ethernet Networking Functional Descriptor" from CDC spec 5.2.3.16 */
520 u8 bDescriptorSubType;
523 u32 bmEthernetStatistics;
525 u16 wNumberMCFilters;
526 u8 bNumberPowerFilters;
527 } __attribute__ ((packed));
529 static const struct ether_desc ether_desc = {
530 .bLength = sizeof ether_desc,
531 .bDescriptorType = USB_DT_CS_INTERFACE,
532 .bDescriptorSubType = 0x0f,
534 /* this descriptor actually adds value, surprise! */
535 .iMACAddress = STRING_ETHADDR,
536 .bmEthernetStatistics = __constant_cpu_to_le32 (0), /* no statistics */
537 .wMaxSegmentSize = __constant_cpu_to_le16 (ETH_FRAME_LEN),
538 .wNumberMCFilters = __constant_cpu_to_le16 (0),
539 .bNumberPowerFilters = 0,
544 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
546 /* include the status endpoint if we can, even where it's optional.
547 * use small wMaxPacketSize, since many "interrupt" endpoints have
548 * very small fifos and it's no big deal if CDC_NOTIFY_SPEED_CHANGE
549 * takes two packets. also default to a big transfer interval, to
550 * waste less bandwidth.
552 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
553 * if they ignore the connect/disconnect notifications that real aether
554 * can provide. more advanced cdc configurations might want to support
555 * encapsulated commands (vendor-specific, using control-OUT).
557 * RNDIS requires the status endpoint, since it uses that encapsulation
558 * mechanism for its funky RPC scheme.
561 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
562 #define STATUS_BYTECOUNT 8 /* 8 byte header + data */
564 static struct usb_endpoint_descriptor
566 .bLength = USB_DT_ENDPOINT_SIZE,
567 .bDescriptorType = USB_DT_ENDPOINT,
569 .bEndpointAddress = USB_DIR_IN,
570 .bmAttributes = USB_ENDPOINT_XFER_INT,
571 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
572 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
576 #ifdef DEV_CONFIG_CDC
578 /* the default data interface has no endpoints ... */
580 static const struct usb_interface_descriptor
582 .bLength = sizeof data_nop_intf,
583 .bDescriptorType = USB_DT_INTERFACE,
585 .bInterfaceNumber = 1,
586 .bAlternateSetting = 0,
588 .bInterfaceClass = USB_CLASS_CDC_DATA,
589 .bInterfaceSubClass = 0,
590 .bInterfaceProtocol = 0,
593 /* ... but the "real" data interface has two bulk endpoints */
595 static const struct usb_interface_descriptor
597 .bLength = sizeof data_intf,
598 .bDescriptorType = USB_DT_INTERFACE,
600 .bInterfaceNumber = 1,
601 .bAlternateSetting = 1,
603 .bInterfaceClass = USB_CLASS_CDC_DATA,
604 .bInterfaceSubClass = 0,
605 .bInterfaceProtocol = 0,
606 .iInterface = STRING_DATA,
611 #ifdef CONFIG_USB_ETH_RNDIS
613 /* RNDIS doesn't activate by changing to the "real" altsetting */
615 static const struct usb_interface_descriptor
617 .bLength = sizeof rndis_data_intf,
618 .bDescriptorType = USB_DT_INTERFACE,
620 .bInterfaceNumber = 1,
621 .bAlternateSetting = 0,
623 .bInterfaceClass = USB_CLASS_CDC_DATA,
624 .bInterfaceSubClass = 0,
625 .bInterfaceProtocol = 0,
626 .iInterface = STRING_DATA,
631 #ifdef DEV_CONFIG_SUBSET
634 * "Simple" CDC-subset option is a simple vendor-neutral model that most
635 * full speed controllers can handle: one interface, two bulk endpoints.
638 static const struct usb_interface_descriptor
640 .bLength = sizeof subset_data_intf,
641 .bDescriptorType = USB_DT_INTERFACE,
643 .bInterfaceNumber = 0,
644 .bAlternateSetting = 0,
646 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
647 .bInterfaceSubClass = 0,
648 .bInterfaceProtocol = 0,
649 .iInterface = STRING_DATA,
655 static struct usb_endpoint_descriptor
657 .bLength = USB_DT_ENDPOINT_SIZE,
658 .bDescriptorType = USB_DT_ENDPOINT,
660 .bEndpointAddress = USB_DIR_IN,
661 .bmAttributes = USB_ENDPOINT_XFER_BULK,
664 static struct usb_endpoint_descriptor
666 .bLength = USB_DT_ENDPOINT_SIZE,
667 .bDescriptorType = USB_DT_ENDPOINT,
669 .bEndpointAddress = USB_DIR_OUT,
670 .bmAttributes = USB_ENDPOINT_XFER_BULK,
673 static const struct usb_descriptor_header *fs_eth_function [10] = {
674 #ifdef DEV_CONFIG_CDC
675 /* "cdc" mode descriptors */
676 (struct usb_descriptor_header *) &control_intf,
677 (struct usb_descriptor_header *) &header_desc,
678 (struct usb_descriptor_header *) &union_desc,
679 (struct usb_descriptor_header *) ðer_desc,
680 /* NOTE: status endpoint may need to be removed */
681 (struct usb_descriptor_header *) &fs_status_desc,
682 /* data interface, with altsetting */
683 (struct usb_descriptor_header *) &data_nop_intf,
684 (struct usb_descriptor_header *) &data_intf,
685 (struct usb_descriptor_header *) &fs_source_desc,
686 (struct usb_descriptor_header *) &fs_sink_desc,
688 #endif /* DEV_CONFIG_CDC */
691 static inline void __init fs_subset_descriptors(void)
693 #ifdef DEV_CONFIG_SUBSET
694 fs_eth_function[0] = (struct usb_descriptor_header *) &subset_data_intf;
695 fs_eth_function[1] = (struct usb_descriptor_header *) &fs_source_desc;
696 fs_eth_function[2] = (struct usb_descriptor_header *) &fs_sink_desc;
697 fs_eth_function[3] = 0;
699 fs_eth_function[0] = 0;
703 #ifdef CONFIG_USB_ETH_RNDIS
704 static const struct usb_descriptor_header *fs_rndis_function [] = {
705 /* control interface matches ACM, not Ethernet */
706 (struct usb_descriptor_header *) &rndis_control_intf,
707 (struct usb_descriptor_header *) &header_desc,
708 (struct usb_descriptor_header *) &call_mgmt_descriptor,
709 (struct usb_descriptor_header *) &acm_descriptor,
710 (struct usb_descriptor_header *) &union_desc,
711 (struct usb_descriptor_header *) &fs_status_desc,
712 /* data interface has no altsetting */
713 (struct usb_descriptor_header *) &rndis_data_intf,
714 (struct usb_descriptor_header *) &fs_source_desc,
715 (struct usb_descriptor_header *) &fs_sink_desc,
720 #ifdef CONFIG_USB_GADGET_DUALSPEED
723 * usb 2.0 devices need to expose both high speed and full speed
724 * descriptors, unless they only run at full speed.
727 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
728 static struct usb_endpoint_descriptor
730 .bLength = USB_DT_ENDPOINT_SIZE,
731 .bDescriptorType = USB_DT_ENDPOINT,
733 .bmAttributes = USB_ENDPOINT_XFER_INT,
734 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
735 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
737 #endif /* DEV_CONFIG_CDC */
739 static struct usb_endpoint_descriptor
741 .bLength = USB_DT_ENDPOINT_SIZE,
742 .bDescriptorType = USB_DT_ENDPOINT,
744 .bmAttributes = USB_ENDPOINT_XFER_BULK,
745 .wMaxPacketSize = __constant_cpu_to_le16 (512),
748 static struct usb_endpoint_descriptor
750 .bLength = USB_DT_ENDPOINT_SIZE,
751 .bDescriptorType = USB_DT_ENDPOINT,
753 .bmAttributes = USB_ENDPOINT_XFER_BULK,
754 .wMaxPacketSize = __constant_cpu_to_le16 (512),
757 static struct usb_qualifier_descriptor
759 .bLength = sizeof dev_qualifier,
760 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
762 .bcdUSB = __constant_cpu_to_le16 (0x0200),
763 .bDeviceClass = USB_CLASS_COMM,
765 .bNumConfigurations = 1,
768 static const struct usb_descriptor_header *hs_eth_function [10] = {
769 #ifdef DEV_CONFIG_CDC
770 /* "cdc" mode descriptors */
771 (struct usb_descriptor_header *) &control_intf,
772 (struct usb_descriptor_header *) &header_desc,
773 (struct usb_descriptor_header *) &union_desc,
774 (struct usb_descriptor_header *) ðer_desc,
775 /* NOTE: status endpoint may need to be removed */
776 (struct usb_descriptor_header *) &hs_status_desc,
777 /* data interface, with altsetting */
778 (struct usb_descriptor_header *) &data_nop_intf,
779 (struct usb_descriptor_header *) &data_intf,
780 (struct usb_descriptor_header *) &hs_source_desc,
781 (struct usb_descriptor_header *) &hs_sink_desc,
783 #endif /* DEV_CONFIG_CDC */
786 static inline void __init hs_subset_descriptors(void)
788 #ifdef DEV_CONFIG_SUBSET
789 hs_eth_function[0] = (struct usb_descriptor_header *) &subset_data_intf;
790 hs_eth_function[1] = (struct usb_descriptor_header *) &fs_source_desc;
791 hs_eth_function[2] = (struct usb_descriptor_header *) &fs_sink_desc;
792 hs_eth_function[3] = 0;
794 hs_eth_function[0] = 0;
798 #ifdef CONFIG_USB_ETH_RNDIS
799 static const struct usb_descriptor_header *hs_rndis_function [] = {
800 /* control interface matches ACM, not Ethernet */
801 (struct usb_descriptor_header *) &rndis_control_intf,
802 (struct usb_descriptor_header *) &header_desc,
803 (struct usb_descriptor_header *) &call_mgmt_descriptor,
804 (struct usb_descriptor_header *) &acm_descriptor,
805 (struct usb_descriptor_header *) &union_desc,
806 (struct usb_descriptor_header *) &hs_status_desc,
807 /* data interface has no altsetting */
808 (struct usb_descriptor_header *) &rndis_data_intf,
809 (struct usb_descriptor_header *) &hs_source_desc,
810 (struct usb_descriptor_header *) &hs_sink_desc,
816 /* maxpacket and other transfer characteristics vary by speed. */
817 #define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
821 /* if there's no high speed support, maxpacket doesn't change. */
822 #define ep_desc(g,hs,fs) fs
824 static inline void __init hs_subset_descriptors(void)
828 #endif /* !CONFIG_USB_GADGET_DUALSPEED */
830 /*-------------------------------------------------------------------------*/
832 /* descriptors that are built on-demand */
834 static char manufacturer [40];
835 static char product_desc [40] = DRIVER_DESC;
837 #ifdef DEV_CONFIG_CDC
838 /* address that the host will use ... usually assigned at random */
839 static char ethaddr [2 * ETH_ALEN + 1];
842 /* static strings, in iso 8859/1 */
843 static struct usb_string strings [] = {
844 { STRING_MANUFACTURER, manufacturer, },
845 { STRING_PRODUCT, product_desc, },
846 { STRING_DATA, "Ethernet Data", },
847 #ifdef DEV_CONFIG_CDC
848 { STRING_CDC, "CDC Ethernet", },
849 { STRING_ETHADDR, ethaddr, },
850 { STRING_CONTROL, "CDC Communications Control", },
852 #ifdef DEV_CONFIG_SUBSET
853 { STRING_SUBSET, "CDC Ethernet Subset", },
855 #ifdef CONFIG_USB_ETH_RNDIS
856 { STRING_RNDIS, "RNDIS", },
857 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
859 { } /* end of list */
862 static struct usb_gadget_strings stringtab = {
863 .language = 0x0409, /* en-us */
868 * one config, two interfaces: control, data.
869 * complications: class descriptors, and an altsetting.
872 config_buf (enum usb_device_speed speed, u8 *buf, u8 type, unsigned index)
875 #ifdef CONFIG_USB_GADGET_DUALSPEED
876 int hs = (speed == USB_SPEED_HIGH);
878 if (type == USB_DT_OTHER_SPEED_CONFIG)
880 #define which_config(t) (hs ? & t ## _config : & t ## _config)
881 #define which_fn(t) (hs ? & hs_ ## t ## _function : & fs_ ## t ## _function)
883 #define which_config(t) (& t ## _config)
884 #define which_fn(t) (& fs_ ## t ## _function)
887 if (index >= device_desc.bNumConfigurations)
890 #ifdef CONFIG_USB_ETH_RNDIS
891 /* list the RNDIS config first, to make Microsoft's drivers
892 * happy. DOCSIS 1.0 needs this too.
894 if (device_desc.bNumConfigurations == 2 && index == 0)
895 len = usb_gadget_config_buf (which_config (rndis), buf,
896 USB_BUFSIZ, (const struct usb_descriptor_header **)
900 len = usb_gadget_config_buf (which_config (eth), buf,
901 USB_BUFSIZ, (const struct usb_descriptor_header **)
905 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
909 /*-------------------------------------------------------------------------*/
911 static void eth_start (struct eth_dev *dev, int gfp_flags);
912 static int alloc_requests (struct eth_dev *dev, unsigned n, int gfp_flags);
914 #ifdef DEV_CONFIG_CDC
915 static inline int ether_alt_ep_setup (struct eth_dev *dev, struct usb_ep *ep)
917 const struct usb_endpoint_descriptor *d;
919 /* With CDC, the host isn't allowed to use these two data
920 * endpoints in the default altsetting for the interface.
921 * so we don't activate them yet. Reset from SET_INTERFACE.
923 * Strictly speaking RNDIS should work the same: activation is
924 * a side effect of setting a packet filter. Deactivation is
925 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
928 /* one endpoint writes data back IN to the host */
929 if (strcmp (ep->name, EP_IN_NAME) == 0) {
930 d = ep_desc (dev->gadget, &hs_source_desc, &fs_source_desc);
931 ep->driver_data = dev;
935 /* one endpoint just reads OUT packets */
936 } else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
937 d = ep_desc (dev->gadget, &hs_sink_desc, &fs_sink_desc);
938 ep->driver_data = dev;
942 /* optional status/notification endpoint */
943 } else if (EP_STATUS_NAME &&
944 strcmp (ep->name, EP_STATUS_NAME) == 0) {
947 d = ep_desc (dev->gadget, &hs_status_desc, &fs_status_desc);
948 result = usb_ep_enable (ep, d);
952 ep->driver_data = dev;
960 #if defined(DEV_CONFIG_SUBSET) || defined(CONFIG_USB_ETH_RNDIS)
961 static inline int ether_ep_setup (struct eth_dev *dev, struct usb_ep *ep)
964 const struct usb_endpoint_descriptor *d;
966 /* CDC subset is simpler: if the device is there,
967 * it's live with rx and tx endpoints.
969 * Do this as a shortcut for RNDIS too.
972 /* one endpoint writes data back IN to the host */
973 if (strcmp (ep->name, EP_IN_NAME) == 0) {
974 d = ep_desc (dev->gadget, &hs_source_desc, &fs_source_desc);
975 result = usb_ep_enable (ep, d);
979 ep->driver_data = dev;
983 /* one endpoint just reads OUT packets */
984 } else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
985 d = ep_desc (dev->gadget, &hs_sink_desc, &fs_sink_desc);
986 result = usb_ep_enable (ep, d);
990 ep->driver_data = dev;
1000 set_ether_config (struct eth_dev *dev, int gfp_flags)
1004 struct usb_gadget *gadget = dev->gadget;
1006 gadget_for_each_ep (ep, gadget) {
1007 #ifdef DEV_CONFIG_CDC
1008 if (!dev->rndis && dev->cdc) {
1009 result = ether_alt_ep_setup (dev, ep);
1015 #ifdef CONFIG_USB_ETH_RNDIS
1016 if (dev->rndis && strcmp (ep->name, EP_STATUS_NAME) == 0) {
1017 const struct usb_endpoint_descriptor *d;
1018 d = ep_desc (gadget, &hs_status_desc, &fs_status_desc);
1019 result = usb_ep_enable (ep, d);
1021 ep->driver_data = dev;
1022 dev->status_ep = ep;
1030 #if defined(DEV_CONFIG_SUBSET) || defined(CONFIG_USB_ETH_RNDIS)
1031 result = ether_ep_setup (dev, ep);
1038 ERROR (dev, "can't enable %s, result %d\n", ep->name, result);
1041 if (!result && (!dev->in_ep || !dev->out_ep))
1045 result = alloc_requests (dev, qlen (gadget), gfp_flags);
1047 /* on error, disable any endpoints */
1049 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
1051 (void) usb_ep_disable (dev->status_ep);
1055 #if defined(DEV_CONFIG_SUBSET) || defined(CONFIG_USB_ETH_RNDIS)
1056 if (dev->rndis || !dev->cdc) {
1058 (void) usb_ep_disable (dev->in_ep);
1060 (void) usb_ep_disable (dev->out_ep);
1069 /* activate non-CDC configs right away
1070 * this isn't strictly according to the RNDIS spec
1072 #if defined(DEV_CONFIG_SUBSET) || defined(CONFIG_USB_ETH_RNDIS)
1073 if (dev->rndis || !dev->cdc) {
1074 netif_carrier_on (dev->net);
1075 if (netif_running (dev->net)) {
1076 spin_unlock (&dev->lock);
1077 eth_start (dev, GFP_ATOMIC);
1078 spin_lock (&dev->lock);
1084 DEBUG (dev, "qlen %d\n", qlen (gadget));
1086 /* caller is responsible for cleanup on error */
1090 static void eth_reset_config (struct eth_dev *dev)
1092 struct usb_request *req;
1094 if (dev->config == 0)
1097 DEBUG (dev, "%s\n", __FUNCTION__);
1099 netif_stop_queue (dev->net);
1100 netif_carrier_off (dev->net);
1102 /* disable endpoints, forcing (synchronous) completion of
1103 * pending i/o. then free the requests.
1106 usb_ep_disable (dev->in_ep);
1107 while (likely (!list_empty (&dev->tx_reqs))) {
1108 req = container_of (dev->tx_reqs.next,
1109 struct usb_request, list);
1110 list_del (&req->list);
1111 usb_ep_free_request (dev->in_ep, req);
1116 usb_ep_disable (dev->out_ep);
1117 while (likely (!list_empty (&dev->rx_reqs))) {
1118 req = container_of (dev->rx_reqs.next,
1119 struct usb_request, list);
1120 list_del (&req->list);
1121 usb_ep_free_request (dev->out_ep, req);
1126 if (dev->status_ep) {
1127 usb_ep_disable (dev->status_ep);
1133 /* change our operational config. must agree with the code
1134 * that returns config descriptors, and altsetting code.
1137 eth_set_config (struct eth_dev *dev, unsigned number, int gfp_flags)
1140 struct usb_gadget *gadget = dev->gadget;
1142 if (number == dev->config)
1145 if (gadget_is_sa1100 (gadget)
1147 && atomic_read (&dev->tx_qlen) != 0) {
1148 /* tx fifo is full, but we can't clear it...*/
1149 INFO (dev, "can't change configurations\n");
1152 eth_reset_config (dev);
1154 /* default: pass all packets, no multicast filtering */
1155 dev->cdc_filter = 0x000f;
1158 case DEV_CONFIG_VALUE:
1160 result = set_ether_config (dev, gfp_flags);
1162 #ifdef CONFIG_USB_ETH_RNDIS
1163 case DEV_RNDIS_CONFIG_VALUE:
1165 result = set_ether_config (dev, gfp_flags);
1176 eth_reset_config (dev);
1180 switch (gadget->speed) {
1181 case USB_SPEED_FULL: speed = "full"; break;
1182 #ifdef CONFIG_USB_GADGET_DUALSPEED
1183 case USB_SPEED_HIGH: speed = "high"; break;
1185 default: speed = "?"; break;
1188 dev->config = number;
1189 INFO (dev, "%s speed config #%d: %s, using %s\n",
1190 speed, number, driver_desc,
1195 : "CDC Ethernet Subset"));
1200 /*-------------------------------------------------------------------------*/
1202 /* section 3.8.2 table 11 of the CDC spec lists Ethernet notifications
1203 * section 3.6.2.1 table 5 specifies ACM notifications, accepted by RNDIS
1204 * and RNDIS also defines its own bit-incompatible notifications
1206 #define CDC_NOTIFY_NETWORK_CONNECTION 0x00 /* required; 6.3.1 */
1207 #define CDC_NOTIFY_RESPONSE_AVAILABLE 0x01 /* optional; 6.3.2 */
1208 #define CDC_NOTIFY_SPEED_CHANGE 0x2a /* required; 6.3.8 */
1210 #ifdef DEV_CONFIG_CDC
1212 struct cdc_notification {
1214 u8 bNotificationType;
1219 /* SPEED_CHANGE data looks like this */
1223 static void eth_status_complete (struct usb_ep *ep, struct usb_request *req)
1225 struct cdc_notification *event = req->buf;
1226 int value = req->status;
1227 struct eth_dev *dev = ep->driver_data;
1229 /* issue the second notification if host reads the first */
1230 if (event->bNotificationType == CDC_NOTIFY_NETWORK_CONNECTION
1232 event->bmRequestType = 0xA1;
1233 event->bNotificationType = CDC_NOTIFY_SPEED_CHANGE;
1234 event->wValue = __constant_cpu_to_le16 (0);
1235 event->wIndex = __constant_cpu_to_le16 (1);
1236 event->wLength = __constant_cpu_to_le16 (8);
1238 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1239 event->data [0] = event->data [1] =
1240 (dev->gadget->speed == USB_SPEED_HIGH)
1241 ? (13 * 512 * 8 * 1000 * 8)
1242 : (19 * 64 * 1 * 1000 * 8);
1245 value = usb_ep_queue (ep, req, GFP_ATOMIC);
1246 DEBUG (dev, "send SPEED_CHANGE --> %d\n", value);
1250 DEBUG (dev, "event %02x --> %d\n",
1251 event->bNotificationType, value);
1253 /* free when done */
1254 usb_ep_free_buffer (ep, req->buf, req->dma, 16);
1255 usb_ep_free_request (ep, req);
1258 static void issue_start_status (struct eth_dev *dev)
1260 struct usb_request *req;
1261 struct cdc_notification *event;
1264 DEBUG (dev, "%s, flush old status first\n", __FUNCTION__);
1268 * FIXME ugly idiom, maybe we'd be better with just
1269 * a "cancel the whole queue" primitive since any
1270 * unlink-one primitive has way too many error modes.
1271 * here, we "know" toggle is already clear...
1273 usb_ep_disable (dev->status_ep);
1274 usb_ep_enable (dev->status_ep, dev->status);
1276 /* FIXME make these allocations static like dev->req */
1277 req = usb_ep_alloc_request (dev->status_ep, GFP_ATOMIC);
1279 DEBUG (dev, "status ENOMEM\n");
1282 req->buf = usb_ep_alloc_buffer (dev->status_ep, 16,
1283 &dev->req->dma, GFP_ATOMIC);
1284 if (req->buf == 0) {
1285 DEBUG (dev, "status buf ENOMEM\n");
1287 usb_ep_free_request (dev->status_ep, req);
1291 /* 3.8.1 says to issue first NETWORK_CONNECTION, then
1292 * a SPEED_CHANGE. could be useful in some configs.
1295 event->bmRequestType = 0xA1;
1296 event->bNotificationType = CDC_NOTIFY_NETWORK_CONNECTION;
1297 event->wValue = __constant_cpu_to_le16 (1); /* connected */
1298 event->wIndex = __constant_cpu_to_le16 (1);
1302 req->complete = eth_status_complete;
1303 value = usb_ep_queue (dev->status_ep, req, GFP_ATOMIC);
1305 DEBUG (dev, "status buf queue --> %d\n", value);
1306 usb_ep_free_buffer (dev->status_ep,
1307 req->buf, dev->req->dma, 16);
1314 /*-------------------------------------------------------------------------*/
1316 static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req)
1318 if (req->status || req->actual != req->length)
1319 DEBUG ((struct eth_dev *) ep->driver_data,
1320 "setup complete --> %d, %d/%d\n",
1321 req->status, req->actual, req->length);
1324 /* see section 3.8.2 table 10 of the CDC spec for more ethernet
1325 * requests, mostly for filters (multicast, pm) and statistics
1326 * section 3.6.2.1 table 4 has ACM requests; RNDIS requires the
1327 * encapsulated command mechanism.
1329 #define CDC_SEND_ENCAPSULATED_COMMAND 0x00 /* optional */
1330 #define CDC_GET_ENCAPSULATED_RESPONSE 0x01 /* optional */
1331 #define CDC_SET_ETHERNET_MULTICAST_FILTERS 0x40 /* optional */
1332 #define CDC_SET_ETHERNET_PM_PATTERN_FILTER 0x41 /* optional */
1333 #define CDC_GET_ETHERNET_PM_PATTERN_FILTER 0x42 /* optional */
1334 #define CDC_SET_ETHERNET_PACKET_FILTER 0x43 /* required */
1335 #define CDC_GET_ETHERNET_STATISTIC 0x44 /* optional */
1337 /* table 62; bits in cdc_filter */
1338 #define CDC_PACKET_TYPE_PROMISCUOUS (1 << 0)
1339 #define CDC_PACKET_TYPE_ALL_MULTICAST (1 << 1) /* no filter */
1340 #define CDC_PACKET_TYPE_DIRECTED (1 << 2)
1341 #define CDC_PACKET_TYPE_BROADCAST (1 << 3)
1342 #define CDC_PACKET_TYPE_MULTICAST (1 << 4) /* filtered */
1344 #ifdef CONFIG_USB_ETH_RNDIS
1346 static void rndis_response_complete (struct usb_ep *ep, struct usb_request *req)
1348 struct eth_dev *dev = ep->driver_data;
1350 if (req->status || req->actual != req->length)
1351 DEBUG (dev, "rndis response complete --> %d, %d/%d\n",
1352 req->status, req->actual, req->length);
1354 /* done sending after CDC_GET_ENCAPSULATED_RESPONSE */
1355 rndis_free_response (dev->rndis_config, req->buf);
1358 static void rndis_command_complete (struct usb_ep *ep, struct usb_request *req)
1360 struct eth_dev *dev = ep->driver_data;
1362 /* received RNDIS command from CDC_SEND_ENCAPSULATED_COMMAND */
1363 spin_lock(&dev->lock);
1364 if (rndis_msg_parser (dev->rndis_config, (u8 *) req->buf))
1365 ERROR(dev, "%s: rndis parse error\n", __FUNCTION__ );
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;
1584 /* else stalls ... spec says to avoid that */
1591 "unknown control req%02x.%02x v%04x i%04x l%d\n",
1592 ctrl->bRequestType, ctrl->bRequest,
1593 ctrl->wValue, ctrl->wIndex, ctrl->wLength);
1596 /* respond with data transfer before status phase? */
1598 req->length = value;
1599 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1601 DEBUG (dev, "ep_queue --> %d\n", value);
1603 eth_setup_complete (gadget->ep0, req);
1607 /* host either stalls (value < 0) or reports success */
1612 eth_disconnect (struct usb_gadget *gadget)
1614 struct eth_dev *dev = get_gadget_data (gadget);
1615 unsigned long flags;
1617 spin_lock_irqsave (&dev->lock, flags);
1618 netif_stop_queue (dev->net);
1619 netif_carrier_off (dev->net);
1620 eth_reset_config (dev);
1621 spin_unlock_irqrestore (&dev->lock, flags);
1623 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1625 /* next we may get setup() calls to enumerate new connections;
1626 * or an unbind() during shutdown (including removing module).
1630 /*-------------------------------------------------------------------------*/
1632 /* NETWORK DRIVER HOOKUP (to the layer above this driver) */
1634 static int eth_change_mtu (struct net_device *net, int new_mtu)
1636 struct eth_dev *dev = (struct eth_dev *) net->priv;
1638 // FIXME if rndis, don't change while link's live
1640 if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN)
1642 /* no zero-length packet read wanted after mtu-sized packets */
1643 if (((new_mtu + sizeof (struct ethhdr)) % dev->in_ep->maxpacket) == 0)
1649 static struct net_device_stats *eth_get_stats (struct net_device *net)
1651 return &((struct eth_dev *) net->priv)->stats;
1654 static int eth_ethtool_ioctl (struct net_device *net, void *useraddr)
1656 struct eth_dev *dev = (struct eth_dev *) net->priv;
1659 if (get_user (cmd, (u32 *)useraddr))
1663 case ETHTOOL_GDRVINFO: { /* get driver info */
1664 struct ethtool_drvinfo info;
1666 memset (&info, 0, sizeof info);
1667 info.cmd = ETHTOOL_GDRVINFO;
1668 strlcpy (info.driver, shortname, sizeof info.driver);
1669 strlcpy (info.version, DRIVER_VERSION, sizeof info.version);
1670 strlcpy (info.fw_version, dev->gadget->name,
1671 sizeof info.fw_version);
1672 strlcpy (info.bus_info, dev->gadget->dev.bus_id,
1673 sizeof info.bus_info);
1674 if (copy_to_user (useraddr, &info, sizeof (info)))
1679 case ETHTOOL_GLINK: { /* get link status */
1680 struct ethtool_value edata = { ETHTOOL_GLINK };
1682 edata.data = (dev->gadget->speed != USB_SPEED_UNKNOWN);
1683 if (copy_to_user (useraddr, &edata, sizeof (edata)))
1689 /* Note that the ethtool user space code requires EOPNOTSUPP */
1693 static int eth_ioctl (struct net_device *net, struct ifreq *rq, int cmd)
1697 return eth_ethtool_ioctl (net, (void *)rq->ifr_data);
1703 static void defer_kevent (struct eth_dev *dev, int flag)
1705 if (test_and_set_bit (flag, &dev->todo))
1707 if (!schedule_work (&dev->work))
1708 ERROR (dev, "kevent %d may have been dropped\n", flag);
1710 DEBUG (dev, "kevent %d scheduled\n", flag);
1713 static void rx_complete (struct usb_ep *ep, struct usb_request *req);
1716 rx_submit (struct eth_dev *dev, struct usb_request *req, int gfp_flags)
1718 struct sk_buff *skb;
1719 int retval = -ENOMEM;
1722 /* Padding up to RX_EXTRA handles minor disagreements with host.
1723 * Normally we use the USB "terminate on short read" convention;
1724 * so allow up to (N*maxpacket)-1, since that memory is normally
1725 * already allocated. Major loss of synch means -EOVERFLOW; any
1726 * obviously corrupted packets will automatically be discarded.
1728 * RNDIS uses internal framing, and explicitly allows senders to
1729 * pad to end-of-packet. That's potentially nice for speed,
1730 * but means receivers can't recover synch on their own.
1732 size = (sizeof (struct ethhdr) + dev->net->mtu + RX_EXTRA);
1733 size += dev->out_ep->maxpacket - 1;
1734 #ifdef CONFIG_USB_ETH_RNDIS
1736 size += sizeof (struct rndis_packet_msg_type);
1738 size -= size % dev->out_ep->maxpacket;
1739 #ifdef CONFIG_USB_ETH_RNDIS
1744 if ((skb = alloc_skb (size, gfp_flags)) == 0) {
1745 DEBUG (dev, "no rx skb\n");
1749 req->buf = skb->data;
1751 req->complete = rx_complete;
1754 retval = usb_ep_queue (dev->out_ep, req, gfp_flags);
1755 if (retval == -ENOMEM)
1757 defer_kevent (dev, WORK_RX_MEMORY);
1759 DEBUG (dev, "rx submit --> %d\n", retval);
1760 dev_kfree_skb_any (skb);
1761 spin_lock (&dev->lock);
1762 list_add (&req->list, &dev->rx_reqs);
1763 spin_unlock (&dev->lock);
1768 static void rx_complete (struct usb_ep *ep, struct usb_request *req)
1770 struct sk_buff *skb = req->context;
1771 struct eth_dev *dev = ep->driver_data;
1772 int status = req->status;
1776 /* normal completion */
1778 skb_put (skb, req->actual);
1779 #ifdef CONFIG_USB_ETH_RNDIS
1780 /* we know MaxPacketsPerTransfer == 1 here */
1782 rndis_rm_hdr (req->buf, &(skb->len));
1784 if (ETH_HLEN > skb->len || skb->len > ETH_FRAME_LEN) {
1785 dev->stats.rx_errors++;
1786 dev->stats.rx_length_errors++;
1787 DEBUG (dev, "rx length %d\n", skb->len);
1791 skb->dev = dev->net;
1792 skb->protocol = eth_type_trans (skb, dev->net);
1793 dev->stats.rx_packets++;
1794 dev->stats.rx_bytes += skb->len;
1796 /* no buffer copies needed, unless hardware can't
1799 status = netif_rx (skb);
1803 /* software-driven interface shutdown */
1804 case -ECONNRESET: // unlink
1805 case -ESHUTDOWN: // disconnect etc
1806 VDEBUG (dev, "rx shutdown, code %d\n", status);
1809 /* for hardware automagic (such as pxa) */
1810 case -ECONNABORTED: // endpoint reset
1811 DEBUG (dev, "rx %s reset\n", ep->name);
1812 defer_kevent (dev, WORK_RX_MEMORY);
1814 dev_kfree_skb_any (skb);
1819 dev->stats.rx_over_errors++;
1823 dev->stats.rx_errors++;
1824 DEBUG (dev, "rx status %d\n", status);
1829 dev_kfree_skb_any (skb);
1830 if (!netif_running (dev->net)) {
1832 /* nobody reading rx_reqs, so no dev->lock */
1833 list_add (&req->list, &dev->rx_reqs);
1837 rx_submit (dev, req, GFP_ATOMIC);
1840 static int prealloc (struct list_head *list, struct usb_ep *ep,
1841 unsigned n, int gfp_flags)
1844 struct usb_request *req;
1849 /* queue/recycle up to N requests */
1851 list_for_each_entry (req, list, list) {
1856 req = usb_ep_alloc_request (ep, gfp_flags);
1858 return list_empty (list) ? -ENOMEM : 0;
1859 list_add (&req->list, list);
1866 struct list_head *next;
1868 next = req->list.next;
1869 list_del (&req->list);
1870 usb_ep_free_request (ep, req);
1875 req = container_of (next, struct usb_request, list);
1880 static int alloc_requests (struct eth_dev *dev, unsigned n, int gfp_flags)
1884 status = prealloc (&dev->tx_reqs, dev->in_ep, n, gfp_flags);
1887 status = prealloc (&dev->rx_reqs, dev->out_ep, n, gfp_flags);
1892 DEBUG (dev, "can't alloc requests\n");
1896 static void rx_fill (struct eth_dev *dev, int gfp_flags)
1898 struct usb_request *req;
1899 unsigned long flags;
1901 clear_bit (WORK_RX_MEMORY, &dev->todo);
1903 /* fill unused rxq slots with some skb */
1904 spin_lock_irqsave (&dev->lock, flags);
1905 while (!list_empty (&dev->rx_reqs)) {
1906 req = container_of (dev->rx_reqs.next,
1907 struct usb_request, list);
1908 list_del_init (&req->list);
1909 spin_unlock_irqrestore (&dev->lock, flags);
1911 if (rx_submit (dev, req, gfp_flags) < 0) {
1912 defer_kevent (dev, WORK_RX_MEMORY);
1916 spin_lock_irqsave (&dev->lock, flags);
1918 spin_unlock_irqrestore (&dev->lock, flags);
1921 static void eth_work (void *_dev)
1923 struct eth_dev *dev = _dev;
1925 if (test_bit (WORK_RX_MEMORY, &dev->todo)) {
1926 if (netif_running (dev->net))
1927 rx_fill (dev, GFP_KERNEL);
1929 clear_bit (WORK_RX_MEMORY, &dev->todo);
1933 DEBUG (dev, "work done, flags = 0x%lx\n", dev->todo);
1936 static void tx_complete (struct usb_ep *ep, struct usb_request *req)
1938 struct sk_buff *skb = req->context;
1939 struct eth_dev *dev = ep->driver_data;
1941 switch (req->status) {
1943 dev->stats.tx_errors++;
1944 VDEBUG (dev, "tx err %d\n", req->status);
1946 case -ECONNRESET: // unlink
1947 case -ESHUTDOWN: // disconnect etc
1950 dev->stats.tx_bytes += skb->len;
1952 dev->stats.tx_packets++;
1954 spin_lock (&dev->lock);
1955 list_add (&req->list, &dev->tx_reqs);
1956 spin_unlock (&dev->lock);
1957 dev_kfree_skb_any (skb);
1959 atomic_dec (&dev->tx_qlen);
1960 if (netif_carrier_ok (dev->net))
1961 netif_wake_queue (dev->net);
1964 static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1966 struct eth_dev *dev = (struct eth_dev *) net->priv;
1967 int length = skb->len;
1969 struct usb_request *req = 0;
1970 unsigned long flags;
1972 /* FIXME check dev->cdc_filter to decide whether to send this,
1973 * instead of acting as if CDC_PACKET_TYPE_PROMISCUOUS were
1974 * always set. RNDIS has the same kind of outgoing filter.
1977 spin_lock_irqsave (&dev->lock, flags);
1978 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1979 list_del (&req->list);
1980 if (list_empty (&dev->tx_reqs))
1981 netif_stop_queue (net);
1982 spin_unlock_irqrestore (&dev->lock, flags);
1984 /* no buffer copies needed, unless the network stack did it
1985 * or the hardware can't use skb buffers.
1986 * or there's not enough space for any RNDIS headers we need
1988 #ifdef CONFIG_USB_ETH_RNDIS
1990 struct sk_buff *skb_rndis;
1992 skb_rndis = skb_realloc_headroom (skb,
1993 sizeof (struct rndis_packet_msg_type));
1997 dev_kfree_skb_any (skb);
1999 rndis_add_hdr (skb);
2003 req->buf = skb->data;
2005 req->complete = tx_complete;
2007 /* use zlp framing on tx for strict CDC-Ether conformance,
2008 * though any robust network rx path ignores extra padding.
2009 * and some hardware doesn't like to write zlps.
2012 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2015 req->length = length;
2017 #ifdef CONFIG_USB_GADGET_DUALSPEED
2018 /* throttle highspeed IRQ rate back slightly */
2019 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2020 ? ((atomic_read (&dev->tx_qlen) % TX_DELAY) != 0)
2024 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
2027 DEBUG (dev, "tx queue err %d\n", retval);
2030 net->trans_start = jiffies;
2031 atomic_inc (&dev->tx_qlen);
2035 #ifdef CONFIG_USB_ETH_RNDIS
2038 dev->stats.tx_dropped++;
2039 dev_kfree_skb_any (skb);
2040 spin_lock_irqsave (&dev->lock, flags);
2041 if (list_empty (&dev->tx_reqs))
2042 netif_start_queue (net);
2043 list_add (&req->list, &dev->tx_reqs);
2044 spin_unlock_irqrestore (&dev->lock, flags);
2049 /*-------------------------------------------------------------------------*/
2051 #ifdef CONFIG_USB_ETH_RNDIS
2053 static void rndis_send_media_state (struct eth_dev *dev, int connect)
2059 if (rndis_signal_connect (dev->rndis_config))
2062 if (rndis_signal_disconnect (dev->rndis_config))
2067 static int rndis_control_ack (struct net_device *net)
2069 struct eth_dev *dev = (struct eth_dev *) net->priv;
2071 struct usb_request *resp;
2073 /* in case RNDIS calls this after disconnect */
2074 if (!dev->status_ep) {
2075 DEBUG (dev, "status ENODEV\n");
2079 /* Allocate memory for notification ie. ACK */
2080 resp = usb_ep_alloc_request (dev->status_ep, GFP_ATOMIC);
2082 DEBUG (dev, "status ENOMEM\n");
2086 resp->buf = usb_ep_alloc_buffer (dev->status_ep, 8,
2087 &resp->dma, GFP_ATOMIC);
2089 DEBUG (dev, "status buf ENOMEM\n");
2090 usb_ep_free_request (dev->status_ep, resp);
2094 /* Send RNDIS RESPONSE_AVAILABLE notification;
2095 * CDC_NOTIFY_RESPONSE_AVAILABLE should work too
2098 resp->complete = rndis_response_complete;
2100 *((u32 *) resp->buf) = __constant_cpu_to_le32 (1);
2101 *((u32 *) resp->buf + 1) = __constant_cpu_to_le32 (0);
2103 length = usb_ep_queue (dev->status_ep, resp, GFP_ATOMIC);
2106 rndis_response_complete (dev->status_ep, resp);
2114 static void eth_start (struct eth_dev *dev, int gfp_flags)
2116 DEBUG (dev, "%s\n", __FUNCTION__);
2118 /* fill the rx queue */
2119 rx_fill (dev, gfp_flags);
2121 /* and open the tx floodgates */
2122 atomic_set (&dev->tx_qlen, 0);
2123 netif_wake_queue (dev->net);
2124 #ifdef CONFIG_USB_ETH_RNDIS
2126 rndis_set_param_medium (dev->rndis_config,
2128 BITRATE(dev->gadget));
2129 rndis_send_media_state (dev, 1);
2134 static int eth_open (struct net_device *net)
2136 struct eth_dev *dev = (struct eth_dev *) net->priv;
2138 DEBUG (dev, "%s\n", __FUNCTION__);
2139 if (netif_carrier_ok (dev->net))
2140 eth_start (dev, GFP_KERNEL);
2144 static int eth_stop (struct net_device *net)
2146 struct eth_dev *dev = (struct eth_dev *) net->priv;
2148 VDEBUG (dev, "%s\n", __FUNCTION__);
2149 netif_stop_queue (net);
2151 DEBUG (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
2152 dev->stats.rx_packets, dev->stats.tx_packets,
2153 dev->stats.rx_errors, dev->stats.tx_errors
2156 /* ensure there are no more active requests */
2157 if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
2158 usb_ep_disable (dev->in_ep);
2159 usb_ep_disable (dev->out_ep);
2160 if (netif_carrier_ok (dev->net)) {
2161 DEBUG (dev, "host still using in/out endpoints\n");
2162 // FIXME idiom may leave toggle wrong here
2163 usb_ep_enable (dev->in_ep, dev->in);
2164 usb_ep_enable (dev->out_ep, dev->out);
2166 if (dev->status_ep) {
2167 usb_ep_disable (dev->status_ep);
2168 usb_ep_enable (dev->status_ep, dev->status);
2172 #ifdef CONFIG_USB_ETH_RNDIS
2174 rndis_set_param_medium (dev->rndis_config,
2175 NDIS_MEDIUM_802_3, 0);
2176 rndis_send_media_state (dev, 0);
2183 /*-------------------------------------------------------------------------*/
2186 eth_unbind (struct usb_gadget *gadget)
2188 struct eth_dev *dev = get_gadget_data (gadget);
2190 DEBUG (dev, "unbind\n");
2191 #ifdef CONFIG_USB_ETH_RNDIS
2192 rndis_deregister (dev->rndis_config);
2196 /* we've already been disconnected ... no i/o is active */
2198 usb_ep_free_buffer (gadget->ep0,
2199 dev->req->buf, dev->req->dma,
2201 usb_ep_free_request (gadget->ep0, dev->req);
2205 unregister_netdev (dev->net);
2206 free_netdev(dev->net);
2208 /* assuming we used keventd, it must quiesce too */
2209 flush_scheduled_work ();
2210 set_gadget_data (gadget, 0);
2213 static u8 __init nibble (unsigned char c)
2215 if (likely (isdigit (c)))
2218 if (likely (isxdigit (c)))
2219 return 10 + c - 'A';
2223 static void __init get_ether_addr (const char *str, u8 *dev_addr)
2228 for (i = 0; i < 6; i++) {
2231 if((*str == '.') || (*str == ':'))
2233 num = nibble(*str++) << 4;
2234 num |= (nibble(*str++));
2237 if (is_valid_ether_addr (dev_addr))
2240 random_ether_addr(dev_addr);
2244 eth_bind (struct usb_gadget *gadget)
2246 struct eth_dev *dev;
2247 struct net_device *net;
2248 u8 cdc = 1, zlp = 1, rndis = 1;
2250 int status = -ENOMEM;
2252 /* these flags are only ever cleared; compiler take note */
2253 #ifndef DEV_CONFIG_CDC
2256 #ifndef CONFIG_USB_ETH_RNDIS
2260 /* Because most host side USB stacks handle CDC Ethernet, that
2261 * standard protocol is _strongly_ preferred for interop purposes.
2262 * (By everyone except Microsoft.)
2264 if (gadget_is_net2280 (gadget)) {
2265 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0201);
2266 } else if (gadget_is_dummy (gadget)) {
2267 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0202);
2268 } else if (gadget_is_pxa (gadget)) {
2269 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0203);
2270 /* pxa doesn't support altsettings */
2272 } else if (gadget_is_sh(gadget)) {
2273 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0204);
2274 /* sh doesn't support multiple interfaces or configs */
2277 } else if (gadget_is_sa1100 (gadget)) {
2278 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0205);
2279 /* hardware can't write zlps */
2281 /* sa1100 CAN do CDC, without status endpoint ... we use
2282 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2285 } else if (gadget_is_goku (gadget)) {
2286 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0206);
2287 } else if (gadget_is_mq11xx (gadget)) {
2288 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0207);
2289 } else if (gadget_is_omap (gadget)) {
2290 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0208);
2292 /* can't assume CDC works. don't want to default to
2293 * anything less functional on CDC-capable hardware,
2294 * so we fail in this case.
2296 dev_err (&gadget->dev,
2297 "controller '%s' not recognized\n",
2301 snprintf (manufacturer, sizeof manufacturer,
2302 UTS_SYSNAME " " UTS_RELEASE "/%s",
2305 /* CDC subset ... recognized by Linux since 2.4.10, but Windows
2306 * drivers aren't widely available.
2309 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2310 device_desc.idVendor =
2311 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2312 device_desc.idProduct =
2313 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
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);
2331 /* support optional vendor/distro customization */
2334 dev_err (&gadget->dev, "idVendor needs idProduct!\n");
2337 device_desc.idVendor = cpu_to_le16(idVendor);
2338 device_desc.idProduct = cpu_to_le16(idProduct);
2340 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2343 strlcpy (manufacturer, iManufacturer, sizeof manufacturer);
2345 strlcpy (product_desc, iProduct, sizeof product_desc);
2347 /* all we really need is bulk IN/OUT */
2348 usb_ep_autoconfig_reset (gadget);
2349 ep = usb_ep_autoconfig (gadget, &fs_source_desc);
2352 dev_err (&gadget->dev,
2353 "can't autoconfigure on %s\n",
2357 EP_IN_NAME = ep->name;
2358 ep->driver_data = ep; /* claim */
2360 ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
2363 EP_OUT_NAME = ep->name;
2364 ep->driver_data = ep; /* claim */
2366 /* CDC Ethernet control interface doesn't require a status endpoint.
2367 * Since some hosts expect one, try to allocate one anyway.
2370 ep = usb_ep_autoconfig (gadget, &fs_status_desc);
2372 EP_STATUS_NAME = ep->name;
2373 ep->driver_data = ep; /* claim */
2375 dev_err (&gadget->dev,
2376 "can't run RNDIS on %s\n",
2379 #ifdef DEV_CONFIG_CDC
2381 control_intf.bNumEndpoints = 0;
2382 /* FIXME remove endpoint from descriptor list */
2387 /* one config: cdc, else minimal subset */
2389 eth_config.bNumInterfaces = 1;
2390 eth_config.iConfiguration = STRING_SUBSET;
2391 fs_subset_descriptors();
2392 hs_subset_descriptors();
2395 /* For now RNDIS is always a second config */
2397 device_desc.bNumConfigurations = 2;
2399 #ifdef CONFIG_USB_GADGET_DUALSPEED
2401 dev_qualifier.bNumConfigurations = 2;
2403 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2405 /* assumes ep0 uses the same value for both speeds ... */
2406 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2408 /* and that all endpoints are dual-speed */
2409 hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
2410 hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
2411 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2413 hs_status_desc.bEndpointAddress =
2414 fs_status_desc.bEndpointAddress;
2416 #endif /* DUALSPEED */
2418 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
2419 usb_gadget_set_selfpowered (gadget);
2421 net = alloc_etherdev (sizeof *dev);
2425 spin_lock_init (&dev->lock);
2426 INIT_WORK (&dev->work, eth_work, dev);
2427 INIT_LIST_HEAD (&dev->tx_reqs);
2428 INIT_LIST_HEAD (&dev->rx_reqs);
2430 /* network device setup */
2432 SET_MODULE_OWNER (net);
2433 strcpy (net->name, "usb%d");
2437 /* Module params for these addresses should come from ID proms.
2438 * The host side address is used with CDC and RNDIS, and commonly
2439 * end ups in a persistent config database.
2441 get_ether_addr(dev_addr, net->dev_addr);
2443 get_ether_addr(host_addr, dev->host_mac);
2444 #ifdef DEV_CONFIG_CDC
2445 snprintf (ethaddr, sizeof ethaddr, "%02X%02X%02X%02X%02X%02X",
2446 dev->host_mac [0], dev->host_mac [1],
2447 dev->host_mac [2], dev->host_mac [3],
2448 dev->host_mac [4], dev->host_mac [5]);
2453 status = rndis_init();
2455 dev_err (&gadget->dev, "can't init RNDIS, %d\n",
2461 net->change_mtu = eth_change_mtu;
2462 net->get_stats = eth_get_stats;
2463 net->hard_start_xmit = eth_start_xmit;
2464 net->open = eth_open;
2465 net->stop = eth_stop;
2466 // watchdog_timeo, tx_timeout ...
2467 // set_multicast_list
2468 net->do_ioctl = eth_ioctl;
2470 /* preallocate control response and buffer */
2471 dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
2474 dev->req->complete = eth_setup_complete;
2475 dev->req->buf = usb_ep_alloc_buffer (gadget->ep0, USB_BUFSIZ,
2476 &dev->req->dma, GFP_KERNEL);
2477 if (!dev->req->buf) {
2478 usb_ep_free_request (gadget->ep0, dev->req);
2482 /* finish hookup to lower layer ... */
2483 dev->gadget = gadget;
2484 set_gadget_data (gadget, dev);
2485 gadget->ep0->driver_data = dev;
2487 /* two kinds of host-initiated state changes:
2488 * - iff DATA transfer is active, carrier is "on"
2489 * - tx queueing enabled if open *and* carrier is "on"
2491 netif_stop_queue (dev->net);
2492 netif_carrier_off (dev->net);
2494 // SET_NETDEV_DEV (dev->net, &gadget->dev);
2495 status = register_netdev (dev->net);
2499 INFO (dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
2500 INFO (dev, "using %s, OUT %s IN %s%s%s\n", gadget->name,
2501 EP_OUT_NAME, EP_IN_NAME,
2502 EP_STATUS_NAME ? " STATUS " : "",
2503 EP_STATUS_NAME ? EP_STATUS_NAME : ""
2505 INFO (dev, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2506 net->dev_addr [0], net->dev_addr [1],
2507 net->dev_addr [2], net->dev_addr [3],
2508 net->dev_addr [4], net->dev_addr [5]);
2511 INFO (dev, "HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2512 dev->host_mac [0], dev->host_mac [1],
2513 dev->host_mac [2], dev->host_mac [3],
2514 dev->host_mac [4], dev->host_mac [5]);
2516 #ifdef CONFIG_USB_ETH_RNDIS
2520 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2522 dev->rndis_config = rndis_register (rndis_control_ack);
2523 if (dev->rndis_config < 0) {
2525 unregister_netdev (dev->net);
2530 /* these set up a lot of the OIDs that RNDIS needs */
2531 rndis_set_host_mac (dev->rndis_config, dev->host_mac);
2532 if (rndis_set_param_dev (dev->rndis_config, dev->net,
2535 if (rndis_set_param_vendor (dev->rndis_config, vendorID,
2538 if (rndis_set_param_medium (dev->rndis_config,
2542 INFO (dev, "RNDIS ready\n");
2549 dev_dbg(&gadget->dev, "register_netdev failed, %d\n", status);
2551 eth_unbind (gadget);
2555 /*-------------------------------------------------------------------------*/
2557 static struct usb_gadget_driver eth_driver = {
2558 #ifdef CONFIG_USB_GADGET_DUALSPEED
2559 .speed = USB_SPEED_HIGH,
2561 .speed = USB_SPEED_FULL,
2563 .function = (char *) driver_desc,
2565 .unbind = eth_unbind,
2568 .disconnect = eth_disconnect,
2571 .name = (char *) shortname,
2578 MODULE_DESCRIPTION (DRIVER_DESC);
2579 MODULE_AUTHOR ("David Brownell, Benedikt Spanger");
2580 MODULE_LICENSE ("GPL");
2583 static int __init init (void)
2585 return usb_gadget_register_driver (ð_driver);
2589 static void __exit cleanup (void)
2591 usb_gadget_unregister_driver (ð_driver);
2593 module_exit (cleanup);