2 * ether.c -- Ethernet gadget driver, with CDC and non-CDC options
4 * Copyright (C) 2003-2005 David Brownell
5 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <linux/config.h>
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/delay.h>
30 #include <linux/ioport.h>
31 #include <linux/sched.h>
32 #include <linux/slab.h>
33 #include <linux/smp_lock.h>
34 #include <linux/errno.h>
35 #include <linux/init.h>
36 #include <linux/timer.h>
37 #include <linux/list.h>
38 #include <linux/interrupt.h>
39 #include <linux/utsname.h>
40 #include <linux/device.h>
41 #include <linux/moduleparam.h>
42 #include <linux/ctype.h>
44 #include <asm/byteorder.h>
47 #include <asm/system.h>
48 #include <asm/uaccess.h>
49 #include <asm/unaligned.h>
51 #include <linux/usb_ch9.h>
52 #include <linux/usb_cdc.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
66 * Builds on hardware support for a full duplex link.
68 * CDC Ethernet is the standard USB solution for sending Ethernet frames
69 * using USB. Real hardware tends to use the same framing protocol but look
70 * different for control features. This driver strongly prefers to use
71 * this USB-IF standard as its open-systems interoperability solution;
72 * most host side USB stacks (except from Microsoft) support it.
74 * There's some hardware that can't talk CDC. We make that hardware
75 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
76 * link-level setup only requires activating the configuration.
77 * Linux supports it, but other host operating systems may not.
78 * (This is a subset of CDC Ethernet.)
80 * A third option is also in use. Rather than CDC Ethernet, or something
81 * simpler, Microsoft pushes their own approach: RNDIS. The published
82 * RNDIS specs are ambiguous and appear to be incomplete, and are also
86 #define DRIVER_DESC "Ethernet Gadget"
87 #define DRIVER_VERSION "Equinox 2004"
89 static const char shortname
[] = "ether";
90 static const char driver_desc
[] = DRIVER_DESC
;
92 #define RX_EXTRA 20 /* guard against rx overflows */
94 #ifdef CONFIG_USB_ETH_RNDIS
97 #define rndis_init() 0
98 #define rndis_exit() do{}while(0)
101 /* CDC and RNDIS support the same host-chosen outgoing packet filters. */
102 #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
103 |USB_CDC_PACKET_TYPE_DIRECTED)
106 /*-------------------------------------------------------------------------*/
110 struct usb_gadget
*gadget
;
111 struct usb_request
*req
; /* for control responses */
112 struct usb_request
*stat_req
; /* for cdc & rndis status */
115 struct usb_ep
*in_ep
, *out_ep
, *status_ep
;
116 const struct usb_endpoint_descriptor
118 struct list_head tx_reqs
, rx_reqs
;
120 struct net_device
*net
;
121 struct net_device_stats stats
;
124 struct work_struct work
;
128 unsigned suspended
:1;
131 #define WORK_RX_MEMORY 0
133 u8 host_mac
[ETH_ALEN
];
136 /* This version autoconfigures as much as possible at run-time.
138 * It also ASSUMES a self-powered device, without remote wakeup,
139 * although remote wakeup support would make sense.
141 static const char *EP_IN_NAME
;
142 static const char *EP_OUT_NAME
;
143 static const char *EP_STATUS_NAME
;
145 /*-------------------------------------------------------------------------*/
147 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
148 * Instead: allocate your own, using normal USB-IF procedures.
151 /* Thanks to NetChip Technologies for donating this product ID.
152 * It's for devices with only CDC Ethernet configurations.
154 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
155 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
157 /* For hardware that can't talk CDC, we use the same vendor ID that
158 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
159 * with pxa250. We're protocol-compatible, if the host-side drivers
160 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
161 * drivers that need to hard-wire endpoint numbers have a hook.
163 * The protocol is a minimal subset of CDC Ether, which works on any bulk
164 * hardware that's not deeply broken ... even on hardware that can't talk
165 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
166 * doesn't handle control-OUT).
168 #define SIMPLE_VENDOR_NUM 0x049f
169 #define SIMPLE_PRODUCT_NUM 0x505a
171 /* For hardware that can talk RNDIS and either of the above protocols,
172 * use this ID ... the windows INF files will know it. Unless it's
173 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
174 * the non-RNDIS configuration.
176 #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
177 #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
180 /* Some systems will want different product identifers published in the
181 * device descriptor, either numbers or strings or both. These string
182 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
185 static ushort __initdata idVendor
;
186 module_param(idVendor
, ushort
, S_IRUGO
);
187 MODULE_PARM_DESC(idVendor
, "USB Vendor ID");
189 static ushort __initdata idProduct
;
190 module_param(idProduct
, ushort
, S_IRUGO
);
191 MODULE_PARM_DESC(idProduct
, "USB Product ID");
193 static ushort __initdata bcdDevice
;
194 module_param(bcdDevice
, ushort
, S_IRUGO
);
195 MODULE_PARM_DESC(bcdDevice
, "USB Device version (BCD)");
197 static char *__initdata iManufacturer
;
198 module_param(iManufacturer
, charp
, S_IRUGO
);
199 MODULE_PARM_DESC(iManufacturer
, "USB Manufacturer string");
201 static char *__initdata iProduct
;
202 module_param(iProduct
, charp
, S_IRUGO
);
203 MODULE_PARM_DESC(iProduct
, "USB Product string");
205 /* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */
206 static char *__initdata dev_addr
;
207 module_param(dev_addr
, charp
, S_IRUGO
);
208 MODULE_PARM_DESC(dev_addr
, "Device Ethernet Address");
210 /* this address is invisible to ifconfig */
211 static char *__initdata host_addr
;
212 module_param(host_addr
, charp
, S_IRUGO
);
213 MODULE_PARM_DESC(host_addr
, "Host Ethernet Address");
216 /*-------------------------------------------------------------------------*/
218 /* Include CDC support if we could run on CDC-capable hardware. */
220 #ifdef CONFIG_USB_GADGET_NET2280
221 #define DEV_CONFIG_CDC
224 #ifdef CONFIG_USB_GADGET_DUMMY_HCD
225 #define DEV_CONFIG_CDC
228 #ifdef CONFIG_USB_GADGET_GOKU
229 #define DEV_CONFIG_CDC
232 #ifdef CONFIG_USB_GADGET_LH7A40X
233 #define DEV_CONFIG_CDC
236 #ifdef CONFIG_USB_GADGET_MQ11XX
237 #define DEV_CONFIG_CDC
240 #ifdef CONFIG_USB_GADGET_OMAP
241 #define DEV_CONFIG_CDC
244 #ifdef CONFIG_USB_GADGET_N9604
245 #define DEV_CONFIG_CDC
248 #ifdef CONFIG_USB_GADGET_PXA27X
249 #define DEV_CONFIG_CDC
252 #ifdef CONFIG_USB_GADGET_AT91
253 #define DEV_CONFIG_CDC
257 /* For CDC-incapable hardware, choose the simple cdc subset.
258 * Anything that talks bulk (without notable bugs) can do this.
260 #ifdef CONFIG_USB_GADGET_PXA2XX
261 #define DEV_CONFIG_SUBSET
264 #ifdef CONFIG_USB_GADGET_SH
265 #define DEV_CONFIG_SUBSET
268 #ifdef CONFIG_USB_GADGET_SA1100
269 /* use non-CDC for backwards compatibility */
270 #define DEV_CONFIG_SUBSET
273 #ifdef CONFIG_USB_GADGET_S3C2410
274 #define DEV_CONFIG_CDC
277 /*-------------------------------------------------------------------------*/
279 /* "main" config is either CDC, or its simple subset */
280 static inline int is_cdc(struct eth_dev
*dev
)
282 #if !defined(DEV_CONFIG_SUBSET)
283 return 1; /* only cdc possible */
284 #elif !defined (DEV_CONFIG_CDC)
285 return 0; /* only subset possible */
287 return dev
->cdc
; /* depends on what hardware we found */
291 /* "secondary" RNDIS config may sometimes be activated */
292 static inline int rndis_active(struct eth_dev
*dev
)
294 #ifdef CONFIG_USB_ETH_RNDIS
301 #define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
302 #define cdc_active(dev) ( is_cdc(dev) && !rndis_active(dev))
306 #define DEFAULT_QLEN 2 /* double buffering by default */
308 /* peak bulk transfer bits-per-second */
309 #define HS_BPS (13 * 512 * 8 * 1000 * 8)
310 #define FS_BPS (19 * 64 * 1 * 1000 * 8)
312 #ifdef CONFIG_USB_GADGET_DUALSPEED
314 static unsigned qmult
= 5;
315 module_param (qmult
, uint
, S_IRUGO
|S_IWUSR
);
318 /* for dual-speed hardware, use deeper queues at highspeed */
319 #define qlen(gadget) \
320 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
322 /* also defer IRQs on highspeed TX */
323 #define TX_DELAY qmult
325 #define BITRATE(g) (((g)->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS)
327 #else /* full speed (low speed doesn't do bulk) */
328 #define qlen(gadget) DEFAULT_QLEN
330 #define BITRATE(g) FS_BPS
334 /*-------------------------------------------------------------------------*/
336 #define xprintk(d,level,fmt,args...) \
337 printk(level "%s: " fmt , (d)->net->name , ## args)
341 #define DEBUG(dev,fmt,args...) \
342 xprintk(dev , KERN_DEBUG , fmt , ## args)
344 #define DEBUG(dev,fmt,args...) \
351 #define VDEBUG(dev,fmt,args...) \
355 #define ERROR(dev,fmt,args...) \
356 xprintk(dev , KERN_ERR , fmt , ## args)
357 #define WARN(dev,fmt,args...) \
358 xprintk(dev , KERN_WARNING , fmt , ## args)
359 #define INFO(dev,fmt,args...) \
360 xprintk(dev , KERN_INFO , fmt , ## args)
362 /*-------------------------------------------------------------------------*/
364 /* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
365 * ep0 implementation: descriptors, config management, setup().
366 * also optional class-specific notification interrupt transfer.
370 * DESCRIPTORS ... most are static, but strings and (full) configuration
371 * descriptors are built on demand. For now we do either full CDC, or
372 * our simple subset, with RNDIS as an optional second configuration.
374 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
375 * the class descriptors match a modem (they're ignored; it's really just
376 * Ethernet functionality), they don't need the NOP altsetting, and the
377 * status transfer endpoint isn't optional.
380 #define STRING_MANUFACTURER 1
381 #define STRING_PRODUCT 2
382 #define STRING_ETHADDR 3
383 #define STRING_DATA 4
384 #define STRING_CONTROL 5
385 #define STRING_RNDIS_CONTROL 6
387 #define STRING_SUBSET 8
388 #define STRING_RNDIS 9
390 #define USB_BUFSIZ 256 /* holds our biggest descriptor */
393 * This device advertises one configuration, eth_config, unless RNDIS
394 * is enabled (rndis_config) on hardware supporting at least two configs.
396 * NOTE: Controllers like superh_udc should probably be able to use
397 * an RNDIS-only configuration.
399 * FIXME define some higher-powered configurations to make it easier
400 * to recharge batteries ...
403 #define DEV_CONFIG_VALUE 1 /* cdc or subset */
404 #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
406 static struct usb_device_descriptor
408 .bLength
= sizeof device_desc
,
409 .bDescriptorType
= USB_DT_DEVICE
,
411 .bcdUSB
= __constant_cpu_to_le16 (0x0200),
413 .bDeviceClass
= USB_CLASS_COMM
,
414 .bDeviceSubClass
= 0,
415 .bDeviceProtocol
= 0,
417 .idVendor
= __constant_cpu_to_le16 (CDC_VENDOR_NUM
),
418 .idProduct
= __constant_cpu_to_le16 (CDC_PRODUCT_NUM
),
419 .iManufacturer
= STRING_MANUFACTURER
,
420 .iProduct
= STRING_PRODUCT
,
421 .bNumConfigurations
= 1,
424 static struct usb_otg_descriptor
426 .bLength
= sizeof otg_descriptor
,
427 .bDescriptorType
= USB_DT_OTG
,
429 .bmAttributes
= USB_OTG_SRP
,
432 static struct usb_config_descriptor
434 .bLength
= sizeof eth_config
,
435 .bDescriptorType
= USB_DT_CONFIG
,
437 /* compute wTotalLength on the fly */
439 .bConfigurationValue
= DEV_CONFIG_VALUE
,
440 .iConfiguration
= STRING_CDC
,
441 .bmAttributes
= USB_CONFIG_ATT_ONE
| USB_CONFIG_ATT_SELFPOWER
,
445 #ifdef CONFIG_USB_ETH_RNDIS
446 static struct usb_config_descriptor
448 .bLength
= sizeof rndis_config
,
449 .bDescriptorType
= USB_DT_CONFIG
,
451 /* compute wTotalLength on the fly */
453 .bConfigurationValue
= DEV_RNDIS_CONFIG_VALUE
,
454 .iConfiguration
= STRING_RNDIS
,
455 .bmAttributes
= USB_CONFIG_ATT_ONE
| USB_CONFIG_ATT_SELFPOWER
,
461 * Compared to the simple CDC subset, the full CDC Ethernet model adds
462 * three class descriptors, two interface descriptors, optional status
463 * endpoint. Both have a "data" interface and two bulk endpoints.
464 * There are also differences in how control requests are handled.
466 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of
467 * the CDC-ACM (modem) spec.
470 #ifdef DEV_CONFIG_CDC
471 static struct usb_interface_descriptor
473 .bLength
= sizeof control_intf
,
474 .bDescriptorType
= USB_DT_INTERFACE
,
476 .bInterfaceNumber
= 0,
477 /* status endpoint is optional; this may be patched later */
479 .bInterfaceClass
= USB_CLASS_COMM
,
480 .bInterfaceSubClass
= USB_CDC_SUBCLASS_ETHERNET
,
481 .bInterfaceProtocol
= USB_CDC_PROTO_NONE
,
482 .iInterface
= STRING_CONTROL
,
486 #ifdef CONFIG_USB_ETH_RNDIS
487 static const struct usb_interface_descriptor
488 rndis_control_intf
= {
489 .bLength
= sizeof rndis_control_intf
,
490 .bDescriptorType
= USB_DT_INTERFACE
,
492 .bInterfaceNumber
= 0,
494 .bInterfaceClass
= USB_CLASS_COMM
,
495 .bInterfaceSubClass
= USB_CDC_SUBCLASS_ACM
,
496 .bInterfaceProtocol
= USB_CDC_ACM_PROTO_VENDOR
,
497 .iInterface
= STRING_RNDIS_CONTROL
,
501 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
503 static const struct usb_cdc_header_desc header_desc
= {
504 .bLength
= sizeof header_desc
,
505 .bDescriptorType
= USB_DT_CS_INTERFACE
,
506 .bDescriptorSubType
= USB_CDC_HEADER_TYPE
,
508 .bcdCDC
= __constant_cpu_to_le16 (0x0110),
511 static const struct usb_cdc_union_desc union_desc
= {
512 .bLength
= sizeof union_desc
,
513 .bDescriptorType
= USB_DT_CS_INTERFACE
,
514 .bDescriptorSubType
= USB_CDC_UNION_TYPE
,
516 .bMasterInterface0
= 0, /* index of control interface */
517 .bSlaveInterface0
= 1, /* index of DATA interface */
520 #endif /* CDC || RNDIS */
522 #ifdef CONFIG_USB_ETH_RNDIS
524 static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor
= {
525 .bLength
= sizeof call_mgmt_descriptor
,
526 .bDescriptorType
= USB_DT_CS_INTERFACE
,
527 .bDescriptorSubType
= USB_CDC_CALL_MANAGEMENT_TYPE
,
529 .bmCapabilities
= 0x00,
530 .bDataInterface
= 0x01,
533 static struct usb_cdc_acm_descriptor acm_descriptor
= {
534 .bLength
= sizeof acm_descriptor
,
535 .bDescriptorType
= USB_DT_CS_INTERFACE
,
536 .bDescriptorSubType
= USB_CDC_ACM_TYPE
,
538 .bmCapabilities
= 0x00,
543 #ifdef DEV_CONFIG_CDC
545 static const struct usb_cdc_ether_desc ether_desc
= {
546 .bLength
= sizeof ether_desc
,
547 .bDescriptorType
= USB_DT_CS_INTERFACE
,
548 .bDescriptorSubType
= USB_CDC_ETHERNET_TYPE
,
550 /* this descriptor actually adds value, surprise! */
551 .iMACAddress
= STRING_ETHADDR
,
552 .bmEthernetStatistics
= __constant_cpu_to_le32 (0), /* no statistics */
553 .wMaxSegmentSize
= __constant_cpu_to_le16 (ETH_FRAME_LEN
),
554 .wNumberMCFilters
= __constant_cpu_to_le16 (0),
555 .bNumberPowerFilters
= 0,
560 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
562 /* include the status endpoint if we can, even where it's optional.
563 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
564 * packet, to simplify cancelation; and a big transfer interval, to
565 * waste less bandwidth.
567 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
568 * if they ignore the connect/disconnect notifications that real aether
569 * can provide. more advanced cdc configurations might want to support
570 * encapsulated commands (vendor-specific, using control-OUT).
572 * RNDIS requires the status endpoint, since it uses that encapsulation
573 * mechanism for its funky RPC scheme.
576 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
577 #define STATUS_BYTECOUNT 16 /* 8 byte header + data */
579 static struct usb_endpoint_descriptor
581 .bLength
= USB_DT_ENDPOINT_SIZE
,
582 .bDescriptorType
= USB_DT_ENDPOINT
,
584 .bEndpointAddress
= USB_DIR_IN
,
585 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
586 .wMaxPacketSize
= __constant_cpu_to_le16 (STATUS_BYTECOUNT
),
587 .bInterval
= 1 << LOG2_STATUS_INTERVAL_MSEC
,
591 #ifdef DEV_CONFIG_CDC
593 /* the default data interface has no endpoints ... */
595 static const struct usb_interface_descriptor
597 .bLength
= sizeof data_nop_intf
,
598 .bDescriptorType
= USB_DT_INTERFACE
,
600 .bInterfaceNumber
= 1,
601 .bAlternateSetting
= 0,
603 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
604 .bInterfaceSubClass
= 0,
605 .bInterfaceProtocol
= 0,
608 /* ... but the "real" data interface has two bulk endpoints */
610 static const struct usb_interface_descriptor
612 .bLength
= sizeof data_intf
,
613 .bDescriptorType
= USB_DT_INTERFACE
,
615 .bInterfaceNumber
= 1,
616 .bAlternateSetting
= 1,
618 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
619 .bInterfaceSubClass
= 0,
620 .bInterfaceProtocol
= 0,
621 .iInterface
= STRING_DATA
,
626 #ifdef CONFIG_USB_ETH_RNDIS
628 /* RNDIS doesn't activate by changing to the "real" altsetting */
630 static const struct usb_interface_descriptor
632 .bLength
= sizeof rndis_data_intf
,
633 .bDescriptorType
= USB_DT_INTERFACE
,
635 .bInterfaceNumber
= 1,
636 .bAlternateSetting
= 0,
638 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
639 .bInterfaceSubClass
= 0,
640 .bInterfaceProtocol
= 0,
641 .iInterface
= STRING_DATA
,
646 #ifdef DEV_CONFIG_SUBSET
649 * "Simple" CDC-subset option is a simple vendor-neutral model that most
650 * full speed controllers can handle: one interface, two bulk endpoints.
653 static const struct usb_interface_descriptor
655 .bLength
= sizeof subset_data_intf
,
656 .bDescriptorType
= USB_DT_INTERFACE
,
658 .bInterfaceNumber
= 0,
659 .bAlternateSetting
= 0,
661 .bInterfaceClass
= USB_CLASS_VENDOR_SPEC
,
662 .bInterfaceSubClass
= 0,
663 .bInterfaceProtocol
= 0,
664 .iInterface
= STRING_DATA
,
670 static struct usb_endpoint_descriptor
672 .bLength
= USB_DT_ENDPOINT_SIZE
,
673 .bDescriptorType
= USB_DT_ENDPOINT
,
675 .bEndpointAddress
= USB_DIR_IN
,
676 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
679 static struct usb_endpoint_descriptor
681 .bLength
= USB_DT_ENDPOINT_SIZE
,
682 .bDescriptorType
= USB_DT_ENDPOINT
,
684 .bEndpointAddress
= USB_DIR_OUT
,
685 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
688 static const struct usb_descriptor_header
*fs_eth_function
[11] = {
689 (struct usb_descriptor_header
*) &otg_descriptor
,
690 #ifdef DEV_CONFIG_CDC
691 /* "cdc" mode descriptors */
692 (struct usb_descriptor_header
*) &control_intf
,
693 (struct usb_descriptor_header
*) &header_desc
,
694 (struct usb_descriptor_header
*) &union_desc
,
695 (struct usb_descriptor_header
*) ðer_desc
,
696 /* NOTE: status endpoint may need to be removed */
697 (struct usb_descriptor_header
*) &fs_status_desc
,
698 /* data interface, with altsetting */
699 (struct usb_descriptor_header
*) &data_nop_intf
,
700 (struct usb_descriptor_header
*) &data_intf
,
701 (struct usb_descriptor_header
*) &fs_source_desc
,
702 (struct usb_descriptor_header
*) &fs_sink_desc
,
704 #endif /* DEV_CONFIG_CDC */
707 static inline void __init
fs_subset_descriptors(void)
709 #ifdef DEV_CONFIG_SUBSET
710 fs_eth_function
[1] = (struct usb_descriptor_header
*) &subset_data_intf
;
711 fs_eth_function
[2] = (struct usb_descriptor_header
*) &fs_source_desc
;
712 fs_eth_function
[3] = (struct usb_descriptor_header
*) &fs_sink_desc
;
713 fs_eth_function
[4] = NULL
;
715 fs_eth_function
[1] = NULL
;
719 #ifdef CONFIG_USB_ETH_RNDIS
720 static const struct usb_descriptor_header
*fs_rndis_function
[] = {
721 (struct usb_descriptor_header
*) &otg_descriptor
,
722 /* control interface matches ACM, not Ethernet */
723 (struct usb_descriptor_header
*) &rndis_control_intf
,
724 (struct usb_descriptor_header
*) &header_desc
,
725 (struct usb_descriptor_header
*) &call_mgmt_descriptor
,
726 (struct usb_descriptor_header
*) &acm_descriptor
,
727 (struct usb_descriptor_header
*) &union_desc
,
728 (struct usb_descriptor_header
*) &fs_status_desc
,
729 /* data interface has no altsetting */
730 (struct usb_descriptor_header
*) &rndis_data_intf
,
731 (struct usb_descriptor_header
*) &fs_source_desc
,
732 (struct usb_descriptor_header
*) &fs_sink_desc
,
737 #ifdef CONFIG_USB_GADGET_DUALSPEED
740 * usb 2.0 devices need to expose both high speed and full speed
741 * descriptors, unless they only run at full speed.
744 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
745 static struct usb_endpoint_descriptor
747 .bLength
= USB_DT_ENDPOINT_SIZE
,
748 .bDescriptorType
= USB_DT_ENDPOINT
,
750 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
751 .wMaxPacketSize
= __constant_cpu_to_le16 (STATUS_BYTECOUNT
),
752 .bInterval
= LOG2_STATUS_INTERVAL_MSEC
+ 4,
754 #endif /* DEV_CONFIG_CDC */
756 static struct usb_endpoint_descriptor
758 .bLength
= USB_DT_ENDPOINT_SIZE
,
759 .bDescriptorType
= USB_DT_ENDPOINT
,
761 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
762 .wMaxPacketSize
= __constant_cpu_to_le16 (512),
765 static struct usb_endpoint_descriptor
767 .bLength
= USB_DT_ENDPOINT_SIZE
,
768 .bDescriptorType
= USB_DT_ENDPOINT
,
770 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
771 .wMaxPacketSize
= __constant_cpu_to_le16 (512),
774 static struct usb_qualifier_descriptor
776 .bLength
= sizeof dev_qualifier
,
777 .bDescriptorType
= USB_DT_DEVICE_QUALIFIER
,
779 .bcdUSB
= __constant_cpu_to_le16 (0x0200),
780 .bDeviceClass
= USB_CLASS_COMM
,
782 .bNumConfigurations
= 1,
785 static const struct usb_descriptor_header
*hs_eth_function
[11] = {
786 (struct usb_descriptor_header
*) &otg_descriptor
,
787 #ifdef DEV_CONFIG_CDC
788 /* "cdc" mode descriptors */
789 (struct usb_descriptor_header
*) &control_intf
,
790 (struct usb_descriptor_header
*) &header_desc
,
791 (struct usb_descriptor_header
*) &union_desc
,
792 (struct usb_descriptor_header
*) ðer_desc
,
793 /* NOTE: status endpoint may need to be removed */
794 (struct usb_descriptor_header
*) &hs_status_desc
,
795 /* data interface, with altsetting */
796 (struct usb_descriptor_header
*) &data_nop_intf
,
797 (struct usb_descriptor_header
*) &data_intf
,
798 (struct usb_descriptor_header
*) &hs_source_desc
,
799 (struct usb_descriptor_header
*) &hs_sink_desc
,
801 #endif /* DEV_CONFIG_CDC */
804 static inline void __init
hs_subset_descriptors(void)
806 #ifdef DEV_CONFIG_SUBSET
807 hs_eth_function
[1] = (struct usb_descriptor_header
*) &subset_data_intf
;
808 hs_eth_function
[2] = (struct usb_descriptor_header
*) &fs_source_desc
;
809 hs_eth_function
[3] = (struct usb_descriptor_header
*) &fs_sink_desc
;
810 hs_eth_function
[4] = NULL
;
812 hs_eth_function
[1] = NULL
;
816 #ifdef CONFIG_USB_ETH_RNDIS
817 static const struct usb_descriptor_header
*hs_rndis_function
[] = {
818 (struct usb_descriptor_header
*) &otg_descriptor
,
819 /* control interface matches ACM, not Ethernet */
820 (struct usb_descriptor_header
*) &rndis_control_intf
,
821 (struct usb_descriptor_header
*) &header_desc
,
822 (struct usb_descriptor_header
*) &call_mgmt_descriptor
,
823 (struct usb_descriptor_header
*) &acm_descriptor
,
824 (struct usb_descriptor_header
*) &union_desc
,
825 (struct usb_descriptor_header
*) &hs_status_desc
,
826 /* data interface has no altsetting */
827 (struct usb_descriptor_header
*) &rndis_data_intf
,
828 (struct usb_descriptor_header
*) &hs_source_desc
,
829 (struct usb_descriptor_header
*) &hs_sink_desc
,
835 /* maxpacket and other transfer characteristics vary by speed. */
836 #define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
840 /* if there's no high speed support, maxpacket doesn't change. */
841 #define ep_desc(g,hs,fs) fs
843 static inline void __init
hs_subset_descriptors(void)
847 #endif /* !CONFIG_USB_GADGET_DUALSPEED */
849 /*-------------------------------------------------------------------------*/
851 /* descriptors that are built on-demand */
853 static char manufacturer
[50];
854 static char product_desc
[40] = DRIVER_DESC
;
856 #ifdef DEV_CONFIG_CDC
857 /* address that the host will use ... usually assigned at random */
858 static char ethaddr
[2 * ETH_ALEN
+ 1];
861 /* static strings, in UTF-8 */
862 static struct usb_string strings
[] = {
863 { STRING_MANUFACTURER
, manufacturer
, },
864 { STRING_PRODUCT
, product_desc
, },
865 { STRING_DATA
, "Ethernet Data", },
866 #ifdef DEV_CONFIG_CDC
867 { STRING_CDC
, "CDC Ethernet", },
868 { STRING_ETHADDR
, ethaddr
, },
869 { STRING_CONTROL
, "CDC Communications Control", },
871 #ifdef DEV_CONFIG_SUBSET
872 { STRING_SUBSET
, "CDC Ethernet Subset", },
874 #ifdef CONFIG_USB_ETH_RNDIS
875 { STRING_RNDIS
, "RNDIS", },
876 { STRING_RNDIS_CONTROL
, "RNDIS Communications Control", },
878 { } /* end of list */
881 static struct usb_gadget_strings stringtab
= {
882 .language
= 0x0409, /* en-us */
887 * one config, two interfaces: control, data.
888 * complications: class descriptors, and an altsetting.
891 config_buf (enum usb_device_speed speed
,
893 unsigned index
, int is_otg
)
896 const struct usb_config_descriptor
*config
;
897 const struct usb_descriptor_header
**function
;
898 #ifdef CONFIG_USB_GADGET_DUALSPEED
899 int hs
= (speed
== USB_SPEED_HIGH
);
901 if (type
== USB_DT_OTHER_SPEED_CONFIG
)
903 #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
905 #define which_fn(t) (fs_ ## t ## _function)
908 if (index
>= device_desc
.bNumConfigurations
)
911 #ifdef CONFIG_USB_ETH_RNDIS
912 /* list the RNDIS config first, to make Microsoft's drivers
913 * happy. DOCSIS 1.0 needs this too.
915 if (device_desc
.bNumConfigurations
== 2 && index
== 0) {
916 config
= &rndis_config
;
917 function
= which_fn (rndis
);
921 config
= ð_config
;
922 function
= which_fn (eth
);
925 /* for now, don't advertise srp-only devices */
929 len
= usb_gadget_config_buf (config
, buf
, USB_BUFSIZ
, function
);
932 ((struct usb_config_descriptor
*) buf
)->bDescriptorType
= type
;
936 /*-------------------------------------------------------------------------*/
938 static void eth_start (struct eth_dev
*dev
, int gfp_flags
);
939 static int alloc_requests (struct eth_dev
*dev
, unsigned n
, int gfp_flags
);
941 #ifdef DEV_CONFIG_CDC
942 static inline int ether_alt_ep_setup (struct eth_dev
*dev
, struct usb_ep
*ep
)
944 const struct usb_endpoint_descriptor
*d
;
946 /* With CDC, the host isn't allowed to use these two data
947 * endpoints in the default altsetting for the interface.
948 * so we don't activate them yet. Reset from SET_INTERFACE.
950 * Strictly speaking RNDIS should work the same: activation is
951 * a side effect of setting a packet filter. Deactivation is
952 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
955 /* one endpoint writes data back IN to the host */
956 if (strcmp (ep
->name
, EP_IN_NAME
) == 0) {
957 d
= ep_desc (dev
->gadget
, &hs_source_desc
, &fs_source_desc
);
958 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
;
967 /* optional status/notification endpoint */
968 } else if (EP_STATUS_NAME
&&
969 strcmp (ep
->name
, EP_STATUS_NAME
) == 0) {
972 d
= ep_desc (dev
->gadget
, &hs_status_desc
, &fs_status_desc
);
973 result
= usb_ep_enable (ep
, d
);
977 ep
->driver_data
= dev
;
984 #if defined(DEV_CONFIG_SUBSET) || defined(CONFIG_USB_ETH_RNDIS)
985 static inline int ether_ep_setup (struct eth_dev
*dev
, struct usb_ep
*ep
)
988 const struct usb_endpoint_descriptor
*d
;
990 /* CDC subset is simpler: if the device is there,
991 * it's live with rx and tx endpoints.
993 * Do this as a shortcut for RNDIS too.
996 /* one endpoint writes data back IN to the host */
997 if (strcmp (ep
->name
, EP_IN_NAME
) == 0) {
998 d
= ep_desc (dev
->gadget
, &hs_source_desc
, &fs_source_desc
);
999 result
= usb_ep_enable (ep
, d
);
1003 ep
->driver_data
= dev
;
1006 /* one endpoint just reads OUT packets */
1007 } else if (strcmp (ep
->name
, EP_OUT_NAME
) == 0) {
1008 d
= ep_desc (dev
->gadget
, &hs_sink_desc
, &fs_sink_desc
);
1009 result
= usb_ep_enable (ep
, d
);
1013 ep
->driver_data
= dev
;
1022 set_ether_config (struct eth_dev
*dev
, int gfp_flags
)
1026 struct usb_gadget
*gadget
= dev
->gadget
;
1028 gadget_for_each_ep (ep
, gadget
) {
1029 #ifdef DEV_CONFIG_CDC
1030 if (!dev
->rndis
&& dev
->cdc
) {
1031 result
= ether_alt_ep_setup (dev
, ep
);
1037 #ifdef CONFIG_USB_ETH_RNDIS
1038 if (dev
->rndis
&& strcmp (ep
->name
, EP_STATUS_NAME
) == 0) {
1039 const struct usb_endpoint_descriptor
*d
;
1040 d
= ep_desc (gadget
, &hs_status_desc
, &fs_status_desc
);
1041 result
= usb_ep_enable (ep
, d
);
1043 ep
->driver_data
= dev
;
1051 #if defined(DEV_CONFIG_SUBSET) || defined(CONFIG_USB_ETH_RNDIS)
1052 result
= ether_ep_setup (dev
, ep
);
1059 ERROR (dev
, "can't enable %s, result %d\n", ep
->name
, result
);
1062 if (!result
&& (!dev
->in_ep
|| !dev
->out_ep
))
1066 result
= alloc_requests (dev
, qlen (gadget
), gfp_flags
);
1068 /* on error, disable any endpoints */
1070 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
1072 (void) usb_ep_disable (dev
->status_ep
);
1075 #if defined(DEV_CONFIG_SUBSET) || defined(CONFIG_USB_ETH_RNDIS)
1076 if (dev
->rndis
|| !dev
->cdc
) {
1078 (void) usb_ep_disable (dev
->in_ep
);
1080 (void) usb_ep_disable (dev
->out_ep
);
1087 /* activate non-CDC configs right away
1088 * this isn't strictly according to the RNDIS spec
1090 #if defined(DEV_CONFIG_SUBSET) || defined(CONFIG_USB_ETH_RNDIS)
1091 if (dev
->rndis
|| !dev
->cdc
) {
1092 netif_carrier_on (dev
->net
);
1093 if (netif_running (dev
->net
)) {
1094 spin_unlock (&dev
->lock
);
1095 eth_start (dev
, GFP_ATOMIC
);
1096 spin_lock (&dev
->lock
);
1102 DEBUG (dev
, "qlen %d\n", qlen (gadget
));
1104 /* caller is responsible for cleanup on error */
1108 static void eth_reset_config (struct eth_dev
*dev
)
1110 struct usb_request
*req
;
1112 if (dev
->config
== 0)
1115 DEBUG (dev
, "%s\n", __FUNCTION__
);
1117 netif_stop_queue (dev
->net
);
1118 netif_carrier_off (dev
->net
);
1120 /* disable endpoints, forcing (synchronous) completion of
1121 * pending i/o. then free the requests.
1124 usb_ep_disable (dev
->in_ep
);
1125 while (likely (!list_empty (&dev
->tx_reqs
))) {
1126 req
= container_of (dev
->tx_reqs
.next
,
1127 struct usb_request
, list
);
1128 list_del (&req
->list
);
1129 usb_ep_free_request (dev
->in_ep
, req
);
1133 usb_ep_disable (dev
->out_ep
);
1134 while (likely (!list_empty (&dev
->rx_reqs
))) {
1135 req
= container_of (dev
->rx_reqs
.next
,
1136 struct usb_request
, list
);
1137 list_del (&req
->list
);
1138 usb_ep_free_request (dev
->out_ep
, req
);
1143 usb_ep_disable (dev
->status_ep
);
1148 /* change our operational config. must agree with the code
1149 * that returns config descriptors, and altsetting code.
1152 eth_set_config (struct eth_dev
*dev
, unsigned number
, int gfp_flags
)
1155 struct usb_gadget
*gadget
= dev
->gadget
;
1157 if (number
== dev
->config
)
1160 if (gadget_is_sa1100 (gadget
)
1162 && atomic_read (&dev
->tx_qlen
) != 0) {
1163 /* tx fifo is full, but we can't clear it...*/
1164 INFO (dev
, "can't change configurations\n");
1167 eth_reset_config (dev
);
1169 /* default: pass all packets, no multicast filtering */
1170 dev
->cdc_filter
= 0x000f;
1173 case DEV_CONFIG_VALUE
:
1175 result
= set_ether_config (dev
, gfp_flags
);
1177 #ifdef CONFIG_USB_ETH_RNDIS
1178 case DEV_RNDIS_CONFIG_VALUE
:
1180 result
= set_ether_config (dev
, gfp_flags
);
1192 eth_reset_config (dev
);
1193 usb_gadget_vbus_draw(dev
->gadget
,
1194 dev
->gadget
->is_otg
? 8 : 100);
1199 power
= 2 * eth_config
.bMaxPower
;
1200 usb_gadget_vbus_draw(dev
->gadget
, power
);
1202 switch (gadget
->speed
) {
1203 case USB_SPEED_FULL
: speed
= "full"; break;
1204 #ifdef CONFIG_USB_GADGET_DUALSPEED
1205 case USB_SPEED_HIGH
: speed
= "high"; break;
1207 default: speed
= "?"; break;
1210 dev
->config
= number
;
1211 INFO (dev
, "%s speed config #%d: %d mA, %s, using %s\n",
1212 speed
, number
, power
, driver_desc
,
1217 : "CDC Ethernet Subset"));
1222 /*-------------------------------------------------------------------------*/
1224 #ifdef DEV_CONFIG_CDC
1226 static void eth_status_complete (struct usb_ep
*ep
, struct usb_request
*req
)
1228 struct usb_cdc_notification
*event
= req
->buf
;
1229 int value
= req
->status
;
1230 struct eth_dev
*dev
= ep
->driver_data
;
1232 /* issue the second notification if host reads the first */
1233 if (event
->bNotificationType
== USB_CDC_NOTIFY_NETWORK_CONNECTION
1235 __le32
*data
= req
->buf
+ sizeof *event
;
1237 event
->bmRequestType
= 0xA1;
1238 event
->bNotificationType
= USB_CDC_NOTIFY_SPEED_CHANGE
;
1239 event
->wValue
= __constant_cpu_to_le16 (0);
1240 event
->wIndex
= __constant_cpu_to_le16 (1);
1241 event
->wLength
= __constant_cpu_to_le16 (8);
1243 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1244 data
[0] = data
[1] = cpu_to_le32 (BITRATE (dev
->gadget
));
1246 req
->length
= STATUS_BYTECOUNT
;
1247 value
= usb_ep_queue (ep
, req
, GFP_ATOMIC
);
1248 DEBUG (dev
, "send SPEED_CHANGE --> %d\n", value
);
1251 } else if (value
!= -ECONNRESET
)
1252 DEBUG (dev
, "event %02x --> %d\n",
1253 event
->bNotificationType
, value
);
1254 event
->bmRequestType
= 0xff;
1257 static void issue_start_status (struct eth_dev
*dev
)
1259 struct usb_request
*req
= dev
->stat_req
;
1260 struct usb_cdc_notification
*event
;
1263 DEBUG (dev
, "%s, flush old status first\n", __FUNCTION__
);
1267 * FIXME ugly idiom, maybe we'd be better with just
1268 * a "cancel the whole queue" primitive since any
1269 * unlink-one primitive has way too many error modes.
1270 * here, we "know" toggle is already clear...
1272 usb_ep_disable (dev
->status_ep
);
1273 usb_ep_enable (dev
->status_ep
, dev
->status
);
1275 /* 3.8.1 says to issue first NETWORK_CONNECTION, then
1276 * a SPEED_CHANGE. could be useful in some configs.
1279 event
->bmRequestType
= 0xA1;
1280 event
->bNotificationType
= USB_CDC_NOTIFY_NETWORK_CONNECTION
;
1281 event
->wValue
= __constant_cpu_to_le16 (1); /* connected */
1282 event
->wIndex
= __constant_cpu_to_le16 (1);
1285 req
->length
= sizeof *event
;
1286 req
->complete
= eth_status_complete
;
1287 value
= usb_ep_queue (dev
->status_ep
, req
, GFP_ATOMIC
);
1289 DEBUG (dev
, "status buf queue --> %d\n", value
);
1294 /*-------------------------------------------------------------------------*/
1296 static void eth_setup_complete (struct usb_ep
*ep
, struct usb_request
*req
)
1298 if (req
->status
|| req
->actual
!= req
->length
)
1299 DEBUG ((struct eth_dev
*) ep
->driver_data
,
1300 "setup complete --> %d, %d/%d\n",
1301 req
->status
, req
->actual
, req
->length
);
1304 #ifdef CONFIG_USB_ETH_RNDIS
1306 static void rndis_response_complete (struct usb_ep
*ep
, struct usb_request
*req
)
1308 if (req
->status
|| req
->actual
!= req
->length
)
1309 DEBUG ((struct eth_dev
*) ep
->driver_data
,
1310 "rndis response complete --> %d, %d/%d\n",
1311 req
->status
, req
->actual
, req
->length
);
1313 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1316 static void rndis_command_complete (struct usb_ep
*ep
, struct usb_request
*req
)
1318 struct eth_dev
*dev
= ep
->driver_data
;
1321 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1322 spin_lock(&dev
->lock
);
1323 status
= rndis_msg_parser (dev
->rndis_config
, (u8
*) req
->buf
);
1325 ERROR(dev
, "%s: rndis parse error %d\n", __FUNCTION__
, status
);
1326 spin_unlock(&dev
->lock
);
1332 * The setup() callback implements all the ep0 functionality that's not
1333 * handled lower down. CDC has a number of less-common features:
1335 * - two interfaces: control, and ethernet data
1336 * - Ethernet data interface has two altsettings: default, and active
1337 * - class-specific descriptors for the control interface
1338 * - class-specific control requests
1341 eth_setup (struct usb_gadget
*gadget
, const struct usb_ctrlrequest
*ctrl
)
1343 struct eth_dev
*dev
= get_gadget_data (gadget
);
1344 struct usb_request
*req
= dev
->req
;
1345 int value
= -EOPNOTSUPP
;
1346 u16 wIndex
= ctrl
->wIndex
;
1347 u16 wValue
= ctrl
->wValue
;
1348 u16 wLength
= ctrl
->wLength
;
1350 /* descriptors just go into the pre-allocated ep0 buffer,
1351 * while config change events may enable network traffic.
1353 req
->complete
= eth_setup_complete
;
1354 switch (ctrl
->bRequest
) {
1356 case USB_REQ_GET_DESCRIPTOR
:
1357 if (ctrl
->bRequestType
!= USB_DIR_IN
)
1359 switch (wValue
>> 8) {
1362 value
= min (wLength
, (u16
) sizeof device_desc
);
1363 memcpy (req
->buf
, &device_desc
, value
);
1365 #ifdef CONFIG_USB_GADGET_DUALSPEED
1366 case USB_DT_DEVICE_QUALIFIER
:
1367 if (!gadget
->is_dualspeed
)
1369 value
= min (wLength
, (u16
) sizeof dev_qualifier
);
1370 memcpy (req
->buf
, &dev_qualifier
, value
);
1373 case USB_DT_OTHER_SPEED_CONFIG
:
1374 if (!gadget
->is_dualspeed
)
1377 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1379 value
= config_buf (gadget
->speed
, req
->buf
,
1384 value
= min (wLength
, (u16
) value
);
1388 value
= usb_gadget_get_string (&stringtab
,
1389 wValue
& 0xff, req
->buf
);
1391 value
= min (wLength
, (u16
) value
);
1396 case USB_REQ_SET_CONFIGURATION
:
1397 if (ctrl
->bRequestType
!= 0)
1399 if (gadget
->a_hnp_support
)
1400 DEBUG (dev
, "HNP available\n");
1401 else if (gadget
->a_alt_hnp_support
)
1402 DEBUG (dev
, "HNP needs a different root port\n");
1403 spin_lock (&dev
->lock
);
1404 value
= eth_set_config (dev
, wValue
, GFP_ATOMIC
);
1405 spin_unlock (&dev
->lock
);
1407 case USB_REQ_GET_CONFIGURATION
:
1408 if (ctrl
->bRequestType
!= USB_DIR_IN
)
1410 *(u8
*)req
->buf
= dev
->config
;
1411 value
= min (wLength
, (u16
) 1);
1414 case USB_REQ_SET_INTERFACE
:
1415 if (ctrl
->bRequestType
!= USB_RECIP_INTERFACE
1419 if (!dev
->cdc
&& wIndex
!= 0)
1421 spin_lock (&dev
->lock
);
1423 /* PXA hardware partially handles SET_INTERFACE;
1424 * we need to kluge around that interference.
1426 if (gadget_is_pxa (gadget
)) {
1427 value
= eth_set_config (dev
, DEV_CONFIG_VALUE
,
1432 #ifdef DEV_CONFIG_CDC
1434 case 0: /* control/master intf */
1438 usb_ep_disable (dev
->status_ep
);
1439 usb_ep_enable (dev
->status_ep
, dev
->status
);
1443 case 1: /* data intf */
1446 usb_ep_disable (dev
->in_ep
);
1447 usb_ep_disable (dev
->out_ep
);
1449 /* CDC requires the data transfers not be done from
1450 * the default interface setting ... also, setting
1451 * the non-default interface clears filters etc.
1454 usb_ep_enable (dev
->in_ep
, dev
->in
);
1455 usb_ep_enable (dev
->out_ep
, dev
->out
);
1456 dev
->cdc_filter
= DEFAULT_FILTER
;
1457 netif_carrier_on (dev
->net
);
1459 issue_start_status (dev
);
1460 if (netif_running (dev
->net
)) {
1461 spin_unlock (&dev
->lock
);
1462 eth_start (dev
, GFP_ATOMIC
);
1463 spin_lock (&dev
->lock
);
1466 netif_stop_queue (dev
->net
);
1467 netif_carrier_off (dev
->net
);
1473 /* FIXME this is wrong, as is the assumption that
1474 * all non-PXA hardware talks real CDC ...
1476 dev_warn (&gadget
->dev
, "set_interface ignored!\n");
1477 #endif /* DEV_CONFIG_CDC */
1480 spin_unlock (&dev
->lock
);
1482 case USB_REQ_GET_INTERFACE
:
1483 if (ctrl
->bRequestType
!= (USB_DIR_IN
|USB_RECIP_INTERFACE
)
1487 if (!(dev
->cdc
|| dev
->rndis
) && wIndex
!= 0)
1490 /* for CDC, iff carrier is on, data interface is active. */
1491 if (dev
->rndis
|| wIndex
!= 1)
1492 *(u8
*)req
->buf
= 0;
1494 *(u8
*)req
->buf
= netif_carrier_ok (dev
->net
) ? 1 : 0;
1495 value
= min (wLength
, (u16
) 1);
1498 #ifdef DEV_CONFIG_CDC
1499 case USB_CDC_SET_ETHERNET_PACKET_FILTER
:
1500 /* see 6.2.30: no data, wIndex = interface,
1501 * wValue = packet filter bitmap
1503 if (ctrl
->bRequestType
!= (USB_TYPE_CLASS
|USB_RECIP_INTERFACE
)
1509 DEBUG (dev
, "packet filter %02x\n", wValue
);
1510 dev
->cdc_filter
= wValue
;
1515 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1516 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1517 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1518 * case USB_CDC_GET_ETHERNET_STATISTIC:
1521 #endif /* DEV_CONFIG_CDC */
1523 #ifdef CONFIG_USB_ETH_RNDIS
1524 /* RNDIS uses the CDC command encapsulation mechanism to implement
1525 * an RPC scheme, with much getting/setting of attributes by OID.
1527 case USB_CDC_SEND_ENCAPSULATED_COMMAND
:
1528 if (ctrl
->bRequestType
!= (USB_TYPE_CLASS
|USB_RECIP_INTERFACE
)
1530 || wLength
> USB_BUFSIZ
1532 || rndis_control_intf
.bInterfaceNumber
1535 /* read the request, then process it */
1537 req
->complete
= rndis_command_complete
;
1538 /* later, rndis_control_ack () sends a notification */
1541 case USB_CDC_GET_ENCAPSULATED_RESPONSE
:
1542 if ((USB_DIR_IN
|USB_TYPE_CLASS
|USB_RECIP_INTERFACE
)
1543 == ctrl
->bRequestType
1545 // && wLength >= 0x0400
1547 && rndis_control_intf
.bInterfaceNumber
1551 /* return the result */
1552 buf
= rndis_get_next_response (dev
->rndis_config
,
1555 memcpy (req
->buf
, buf
, value
);
1556 req
->complete
= rndis_response_complete
;
1557 rndis_free_response(dev
->rndis_config
, buf
);
1559 /* else stalls ... spec says to avoid that */
1566 "unknown control req%02x.%02x v%04x i%04x l%d\n",
1567 ctrl
->bRequestType
, ctrl
->bRequest
,
1568 wValue
, wIndex
, wLength
);
1571 /* respond with data transfer before status phase? */
1573 req
->length
= value
;
1574 req
->zero
= value
< wLength
1575 && (value
% gadget
->ep0
->maxpacket
) == 0;
1576 value
= usb_ep_queue (gadget
->ep0
, req
, GFP_ATOMIC
);
1578 DEBUG (dev
, "ep_queue --> %d\n", value
);
1580 eth_setup_complete (gadget
->ep0
, req
);
1584 /* host either stalls (value < 0) or reports success */
1589 eth_disconnect (struct usb_gadget
*gadget
)
1591 struct eth_dev
*dev
= get_gadget_data (gadget
);
1592 unsigned long flags
;
1594 spin_lock_irqsave (&dev
->lock
, flags
);
1595 netif_stop_queue (dev
->net
);
1596 netif_carrier_off (dev
->net
);
1597 eth_reset_config (dev
);
1598 spin_unlock_irqrestore (&dev
->lock
, flags
);
1600 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1602 /* next we may get setup() calls to enumerate new connections;
1603 * or an unbind() during shutdown (including removing module).
1607 /*-------------------------------------------------------------------------*/
1609 /* NETWORK DRIVER HOOKUP (to the layer above this driver) */
1611 static int eth_change_mtu (struct net_device
*net
, int new_mtu
)
1613 struct eth_dev
*dev
= netdev_priv(net
);
1615 // FIXME if rndis, don't change while link's live
1617 if (new_mtu
<= ETH_HLEN
|| new_mtu
> ETH_FRAME_LEN
)
1619 /* no zero-length packet read wanted after mtu-sized packets */
1620 if (((new_mtu
+ sizeof (struct ethhdr
)) % dev
->in_ep
->maxpacket
) == 0)
1626 static struct net_device_stats
*eth_get_stats (struct net_device
*net
)
1628 return &((struct eth_dev
*)netdev_priv(net
))->stats
;
1631 static void eth_get_drvinfo(struct net_device
*net
, struct ethtool_drvinfo
*p
)
1633 struct eth_dev
*dev
= netdev_priv(net
);
1634 strlcpy(p
->driver
, shortname
, sizeof p
->driver
);
1635 strlcpy(p
->version
, DRIVER_VERSION
, sizeof p
->version
);
1636 strlcpy(p
->fw_version
, dev
->gadget
->name
, sizeof p
->fw_version
);
1637 strlcpy (p
->bus_info
, dev
->gadget
->dev
.bus_id
, sizeof p
->bus_info
);
1640 static u32
eth_get_link(struct net_device
*net
)
1642 struct eth_dev
*dev
= netdev_priv(net
);
1643 return dev
->gadget
->speed
!= USB_SPEED_UNKNOWN
;
1646 static struct ethtool_ops ops
= {
1647 .get_drvinfo
= eth_get_drvinfo
,
1648 .get_link
= eth_get_link
1651 static void defer_kevent (struct eth_dev
*dev
, int flag
)
1653 if (test_and_set_bit (flag
, &dev
->todo
))
1655 if (!schedule_work (&dev
->work
))
1656 ERROR (dev
, "kevent %d may have been dropped\n", flag
);
1658 DEBUG (dev
, "kevent %d scheduled\n", flag
);
1661 static void rx_complete (struct usb_ep
*ep
, struct usb_request
*req
);
1664 rx_submit (struct eth_dev
*dev
, struct usb_request
*req
, int gfp_flags
)
1666 struct sk_buff
*skb
;
1667 int retval
= -ENOMEM
;
1670 /* Padding up to RX_EXTRA handles minor disagreements with host.
1671 * Normally we use the USB "terminate on short read" convention;
1672 * so allow up to (N*maxpacket), since that memory is normally
1673 * already allocated. Some hardware doesn't deal well with short
1674 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1675 * byte off the end (to force hardware errors on overflow).
1677 * RNDIS uses internal framing, and explicitly allows senders to
1678 * pad to end-of-packet. That's potentially nice for speed,
1679 * but means receivers can't recover synch on their own.
1681 size
= (sizeof (struct ethhdr
) + dev
->net
->mtu
+ RX_EXTRA
);
1682 size
+= dev
->out_ep
->maxpacket
- 1;
1683 #ifdef CONFIG_USB_ETH_RNDIS
1685 size
+= sizeof (struct rndis_packet_msg_type
);
1687 size
-= size
% dev
->out_ep
->maxpacket
;
1689 if ((skb
= alloc_skb (size
+ NET_IP_ALIGN
, gfp_flags
)) == 0) {
1690 DEBUG (dev
, "no rx skb\n");
1694 /* Some platforms perform better when IP packets are aligned,
1695 * but on at least one, checksumming fails otherwise. Note:
1696 * this doesn't account for variable-sized RNDIS headers.
1698 skb_reserve(skb
, NET_IP_ALIGN
);
1700 req
->buf
= skb
->data
;
1702 req
->complete
= rx_complete
;
1705 retval
= usb_ep_queue (dev
->out_ep
, req
, gfp_flags
);
1706 if (retval
== -ENOMEM
)
1708 defer_kevent (dev
, WORK_RX_MEMORY
);
1710 DEBUG (dev
, "rx submit --> %d\n", retval
);
1711 dev_kfree_skb_any (skb
);
1712 spin_lock (&dev
->lock
);
1713 list_add (&req
->list
, &dev
->rx_reqs
);
1714 spin_unlock (&dev
->lock
);
1719 static void rx_complete (struct usb_ep
*ep
, struct usb_request
*req
)
1721 struct sk_buff
*skb
= req
->context
;
1722 struct eth_dev
*dev
= ep
->driver_data
;
1723 int status
= req
->status
;
1727 /* normal completion */
1729 skb_put (skb
, req
->actual
);
1730 #ifdef CONFIG_USB_ETH_RNDIS
1731 /* we know MaxPacketsPerTransfer == 1 here */
1733 rndis_rm_hdr (req
->buf
, &(skb
->len
));
1735 if (ETH_HLEN
> skb
->len
|| skb
->len
> ETH_FRAME_LEN
) {
1736 dev
->stats
.rx_errors
++;
1737 dev
->stats
.rx_length_errors
++;
1738 DEBUG (dev
, "rx length %d\n", skb
->len
);
1742 skb
->dev
= dev
->net
;
1743 skb
->protocol
= eth_type_trans (skb
, dev
->net
);
1744 dev
->stats
.rx_packets
++;
1745 dev
->stats
.rx_bytes
+= skb
->len
;
1747 /* no buffer copies needed, unless hardware can't
1750 status
= netif_rx (skb
);
1754 /* software-driven interface shutdown */
1755 case -ECONNRESET
: // unlink
1756 case -ESHUTDOWN
: // disconnect etc
1757 VDEBUG (dev
, "rx shutdown, code %d\n", status
);
1760 /* for hardware automagic (such as pxa) */
1761 case -ECONNABORTED
: // endpoint reset
1762 DEBUG (dev
, "rx %s reset\n", ep
->name
);
1763 defer_kevent (dev
, WORK_RX_MEMORY
);
1765 dev_kfree_skb_any (skb
);
1770 dev
->stats
.rx_over_errors
++;
1774 dev
->stats
.rx_errors
++;
1775 DEBUG (dev
, "rx status %d\n", status
);
1780 dev_kfree_skb_any (skb
);
1781 if (!netif_running (dev
->net
)) {
1783 /* nobody reading rx_reqs, so no dev->lock */
1784 list_add (&req
->list
, &dev
->rx_reqs
);
1788 rx_submit (dev
, req
, GFP_ATOMIC
);
1791 static int prealloc (struct list_head
*list
, struct usb_ep
*ep
,
1792 unsigned n
, int gfp_flags
)
1795 struct usb_request
*req
;
1800 /* queue/recycle up to N requests */
1802 list_for_each_entry (req
, list
, list
) {
1807 req
= usb_ep_alloc_request (ep
, gfp_flags
);
1809 return list_empty (list
) ? -ENOMEM
: 0;
1810 list_add (&req
->list
, list
);
1817 struct list_head
*next
;
1819 next
= req
->list
.next
;
1820 list_del (&req
->list
);
1821 usb_ep_free_request (ep
, req
);
1826 req
= container_of (next
, struct usb_request
, list
);
1831 static int alloc_requests (struct eth_dev
*dev
, unsigned n
, int gfp_flags
)
1835 status
= prealloc (&dev
->tx_reqs
, dev
->in_ep
, n
, gfp_flags
);
1838 status
= prealloc (&dev
->rx_reqs
, dev
->out_ep
, n
, gfp_flags
);
1843 DEBUG (dev
, "can't alloc requests\n");
1847 static void rx_fill (struct eth_dev
*dev
, int gfp_flags
)
1849 struct usb_request
*req
;
1850 unsigned long flags
;
1852 clear_bit (WORK_RX_MEMORY
, &dev
->todo
);
1854 /* fill unused rxq slots with some skb */
1855 spin_lock_irqsave (&dev
->lock
, flags
);
1856 while (!list_empty (&dev
->rx_reqs
)) {
1857 req
= container_of (dev
->rx_reqs
.next
,
1858 struct usb_request
, list
);
1859 list_del_init (&req
->list
);
1860 spin_unlock_irqrestore (&dev
->lock
, flags
);
1862 if (rx_submit (dev
, req
, gfp_flags
) < 0) {
1863 defer_kevent (dev
, WORK_RX_MEMORY
);
1867 spin_lock_irqsave (&dev
->lock
, flags
);
1869 spin_unlock_irqrestore (&dev
->lock
, flags
);
1872 static void eth_work (void *_dev
)
1874 struct eth_dev
*dev
= _dev
;
1876 if (test_bit (WORK_RX_MEMORY
, &dev
->todo
)) {
1877 if (netif_running (dev
->net
))
1878 rx_fill (dev
, GFP_KERNEL
);
1880 clear_bit (WORK_RX_MEMORY
, &dev
->todo
);
1884 DEBUG (dev
, "work done, flags = 0x%lx\n", dev
->todo
);
1887 static void tx_complete (struct usb_ep
*ep
, struct usb_request
*req
)
1889 struct sk_buff
*skb
= req
->context
;
1890 struct eth_dev
*dev
= ep
->driver_data
;
1892 switch (req
->status
) {
1894 dev
->stats
.tx_errors
++;
1895 VDEBUG (dev
, "tx err %d\n", req
->status
);
1897 case -ECONNRESET
: // unlink
1898 case -ESHUTDOWN
: // disconnect etc
1901 dev
->stats
.tx_bytes
+= skb
->len
;
1903 dev
->stats
.tx_packets
++;
1905 spin_lock (&dev
->lock
);
1906 list_add (&req
->list
, &dev
->tx_reqs
);
1907 spin_unlock (&dev
->lock
);
1908 dev_kfree_skb_any (skb
);
1910 atomic_dec (&dev
->tx_qlen
);
1911 if (netif_carrier_ok (dev
->net
))
1912 netif_wake_queue (dev
->net
);
1915 static inline int eth_is_promisc (struct eth_dev
*dev
)
1917 /* no filters for the CDC subset; always promisc */
1918 if (subset_active (dev
))
1920 return dev
->cdc_filter
& USB_CDC_PACKET_TYPE_PROMISCUOUS
;
1923 static int eth_start_xmit (struct sk_buff
*skb
, struct net_device
*net
)
1925 struct eth_dev
*dev
= netdev_priv(net
);
1926 int length
= skb
->len
;
1928 struct usb_request
*req
= NULL
;
1929 unsigned long flags
;
1931 /* apply outgoing CDC or RNDIS filters */
1932 if (!eth_is_promisc (dev
)) {
1933 u8
*dest
= skb
->data
;
1935 if (dest
[0] & 0x01) {
1938 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1939 * SET_ETHERNET_MULTICAST_FILTERS requests
1941 if (memcmp (dest
, net
->broadcast
, ETH_ALEN
) == 0)
1942 type
= USB_CDC_PACKET_TYPE_BROADCAST
;
1944 type
= USB_CDC_PACKET_TYPE_ALL_MULTICAST
;
1945 if (!(dev
->cdc_filter
& type
)) {
1946 dev_kfree_skb_any (skb
);
1950 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1953 spin_lock_irqsave (&dev
->lock
, flags
);
1954 req
= container_of (dev
->tx_reqs
.next
, struct usb_request
, list
);
1955 list_del (&req
->list
);
1956 if (list_empty (&dev
->tx_reqs
))
1957 netif_stop_queue (net
);
1958 spin_unlock_irqrestore (&dev
->lock
, flags
);
1960 /* no buffer copies needed, unless the network stack did it
1961 * or the hardware can't use skb buffers.
1962 * or there's not enough space for any RNDIS headers we need
1964 #ifdef CONFIG_USB_ETH_RNDIS
1966 struct sk_buff
*skb_rndis
;
1968 skb_rndis
= skb_realloc_headroom (skb
,
1969 sizeof (struct rndis_packet_msg_type
));
1973 dev_kfree_skb_any (skb
);
1975 rndis_add_hdr (skb
);
1979 req
->buf
= skb
->data
;
1981 req
->complete
= tx_complete
;
1983 /* use zlp framing on tx for strict CDC-Ether conformance,
1984 * though any robust network rx path ignores extra padding.
1985 * and some hardware doesn't like to write zlps.
1988 if (!dev
->zlp
&& (length
% dev
->in_ep
->maxpacket
) == 0)
1991 req
->length
= length
;
1993 #ifdef CONFIG_USB_GADGET_DUALSPEED
1994 /* throttle highspeed IRQ rate back slightly */
1995 req
->no_interrupt
= (dev
->gadget
->speed
== USB_SPEED_HIGH
)
1996 ? ((atomic_read (&dev
->tx_qlen
) % TX_DELAY
) != 0)
2000 retval
= usb_ep_queue (dev
->in_ep
, req
, GFP_ATOMIC
);
2003 DEBUG (dev
, "tx queue err %d\n", retval
);
2006 net
->trans_start
= jiffies
;
2007 atomic_inc (&dev
->tx_qlen
);
2011 #ifdef CONFIG_USB_ETH_RNDIS
2014 dev
->stats
.tx_dropped
++;
2015 dev_kfree_skb_any (skb
);
2016 spin_lock_irqsave (&dev
->lock
, flags
);
2017 if (list_empty (&dev
->tx_reqs
))
2018 netif_start_queue (net
);
2019 list_add (&req
->list
, &dev
->tx_reqs
);
2020 spin_unlock_irqrestore (&dev
->lock
, flags
);
2025 /*-------------------------------------------------------------------------*/
2027 #ifdef CONFIG_USB_ETH_RNDIS
2029 static void rndis_send_media_state (struct eth_dev
*dev
, int connect
)
2035 if (rndis_signal_connect (dev
->rndis_config
))
2038 if (rndis_signal_disconnect (dev
->rndis_config
))
2044 rndis_control_ack_complete (struct usb_ep
*ep
, struct usb_request
*req
)
2046 if (req
->status
|| req
->actual
!= req
->length
)
2047 DEBUG ((struct eth_dev
*) ep
->driver_data
,
2048 "rndis control ack complete --> %d, %d/%d\n",
2049 req
->status
, req
->actual
, req
->length
);
2051 usb_ep_free_buffer(ep
, req
->buf
, req
->dma
, 8);
2052 usb_ep_free_request(ep
, req
);
2055 static int rndis_control_ack (struct net_device
*net
)
2057 struct eth_dev
*dev
= netdev_priv(net
);
2059 struct usb_request
*resp
;
2061 /* in case RNDIS calls this after disconnect */
2062 if (!dev
->status_ep
) {
2063 DEBUG (dev
, "status ENODEV\n");
2067 /* Allocate memory for notification ie. ACK */
2068 resp
= usb_ep_alloc_request (dev
->status_ep
, GFP_ATOMIC
);
2070 DEBUG (dev
, "status ENOMEM\n");
2074 resp
->buf
= usb_ep_alloc_buffer (dev
->status_ep
, 8,
2075 &resp
->dma
, GFP_ATOMIC
);
2077 DEBUG (dev
, "status buf ENOMEM\n");
2078 usb_ep_free_request (dev
->status_ep
, resp
);
2082 /* Send RNDIS RESPONSE_AVAILABLE notification;
2083 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
2086 resp
->complete
= rndis_control_ack_complete
;
2088 *((__le32
*) resp
->buf
) = __constant_cpu_to_le32 (1);
2089 *((__le32
*) resp
->buf
+ 1) = __constant_cpu_to_le32 (0);
2091 length
= usb_ep_queue (dev
->status_ep
, resp
, GFP_ATOMIC
);
2094 rndis_control_ack_complete (dev
->status_ep
, resp
);
2102 static void eth_start (struct eth_dev
*dev
, int gfp_flags
)
2104 DEBUG (dev
, "%s\n", __FUNCTION__
);
2106 /* fill the rx queue */
2107 rx_fill (dev
, gfp_flags
);
2109 /* and open the tx floodgates */
2110 atomic_set (&dev
->tx_qlen
, 0);
2111 netif_wake_queue (dev
->net
);
2112 #ifdef CONFIG_USB_ETH_RNDIS
2114 rndis_set_param_medium (dev
->rndis_config
,
2116 BITRATE(dev
->gadget
));
2117 rndis_send_media_state (dev
, 1);
2122 static int eth_open (struct net_device
*net
)
2124 struct eth_dev
*dev
= netdev_priv(net
);
2126 DEBUG (dev
, "%s\n", __FUNCTION__
);
2127 if (netif_carrier_ok (dev
->net
))
2128 eth_start (dev
, GFP_KERNEL
);
2132 static int eth_stop (struct net_device
*net
)
2134 struct eth_dev
*dev
= netdev_priv(net
);
2136 VDEBUG (dev
, "%s\n", __FUNCTION__
);
2137 netif_stop_queue (net
);
2139 DEBUG (dev
, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
2140 dev
->stats
.rx_packets
, dev
->stats
.tx_packets
,
2141 dev
->stats
.rx_errors
, dev
->stats
.tx_errors
2144 /* ensure there are no more active requests */
2146 usb_ep_disable (dev
->in_ep
);
2147 usb_ep_disable (dev
->out_ep
);
2148 if (netif_carrier_ok (dev
->net
)) {
2149 DEBUG (dev
, "host still using in/out endpoints\n");
2150 // FIXME idiom may leave toggle wrong here
2151 usb_ep_enable (dev
->in_ep
, dev
->in
);
2152 usb_ep_enable (dev
->out_ep
, dev
->out
);
2154 if (dev
->status_ep
) {
2155 usb_ep_disable (dev
->status_ep
);
2156 usb_ep_enable (dev
->status_ep
, dev
->status
);
2160 #ifdef CONFIG_USB_ETH_RNDIS
2162 rndis_set_param_medium (dev
->rndis_config
,
2163 NDIS_MEDIUM_802_3
, 0);
2164 rndis_send_media_state (dev
, 0);
2171 /*-------------------------------------------------------------------------*/
2173 static struct usb_request
*eth_req_alloc (struct usb_ep
*ep
, unsigned size
)
2175 struct usb_request
*req
;
2177 req
= usb_ep_alloc_request (ep
, GFP_KERNEL
);
2181 req
->buf
= kmalloc (size
, GFP_KERNEL
);
2183 usb_ep_free_request (ep
, req
);
2190 eth_req_free (struct usb_ep
*ep
, struct usb_request
*req
)
2193 usb_ep_free_request (ep
, req
);
2198 eth_unbind (struct usb_gadget
*gadget
)
2200 struct eth_dev
*dev
= get_gadget_data (gadget
);
2202 DEBUG (dev
, "unbind\n");
2203 #ifdef CONFIG_USB_ETH_RNDIS
2204 rndis_deregister (dev
->rndis_config
);
2208 /* we've already been disconnected ... no i/o is active */
2210 eth_req_free (gadget
->ep0
, dev
->req
);
2213 if (dev
->stat_req
) {
2214 eth_req_free (dev
->status_ep
, dev
->stat_req
);
2215 dev
->stat_req
= NULL
;
2218 unregister_netdev (dev
->net
);
2219 free_netdev(dev
->net
);
2221 /* assuming we used keventd, it must quiesce too */
2222 flush_scheduled_work ();
2223 set_gadget_data (gadget
, NULL
);
2226 static u8 __init
nibble (unsigned char c
)
2228 if (likely (isdigit (c
)))
2231 if (likely (isxdigit (c
)))
2232 return 10 + c
- 'A';
2236 static void __init
get_ether_addr (const char *str
, u8
*dev_addr
)
2241 for (i
= 0; i
< 6; i
++) {
2244 if((*str
== '.') || (*str
== ':'))
2246 num
= nibble(*str
++) << 4;
2247 num
|= (nibble(*str
++));
2250 if (is_valid_ether_addr (dev_addr
))
2253 random_ether_addr(dev_addr
);
2257 eth_bind (struct usb_gadget
*gadget
)
2259 struct eth_dev
*dev
;
2260 struct net_device
*net
;
2261 u8 cdc
= 1, zlp
= 1, rndis
= 1;
2262 struct usb_ep
*in_ep
, *out_ep
, *status_ep
= NULL
;
2263 int status
= -ENOMEM
;
2265 /* these flags are only ever cleared; compiler take note */
2266 #ifndef DEV_CONFIG_CDC
2269 #ifndef CONFIG_USB_ETH_RNDIS
2273 /* Because most host side USB stacks handle CDC Ethernet, that
2274 * standard protocol is _strongly_ preferred for interop purposes.
2275 * (By everyone except Microsoft.)
2277 if (gadget_is_net2280 (gadget
)) {
2278 device_desc
.bcdDevice
= __constant_cpu_to_le16 (0x0201);
2279 } else if (gadget_is_dummy (gadget
)) {
2280 device_desc
.bcdDevice
= __constant_cpu_to_le16 (0x0202);
2281 } else if (gadget_is_pxa (gadget
)) {
2282 device_desc
.bcdDevice
= __constant_cpu_to_le16 (0x0203);
2283 /* pxa doesn't support altsettings */
2285 } else if (gadget_is_sh(gadget
)) {
2286 device_desc
.bcdDevice
= __constant_cpu_to_le16 (0x0204);
2287 /* sh doesn't support multiple interfaces or configs */
2290 } else if (gadget_is_sa1100 (gadget
)) {
2291 device_desc
.bcdDevice
= __constant_cpu_to_le16 (0x0205);
2292 /* hardware can't write zlps */
2294 /* sa1100 CAN do CDC, without status endpoint ... we use
2295 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2298 } else if (gadget_is_goku (gadget
)) {
2299 device_desc
.bcdDevice
= __constant_cpu_to_le16 (0x0206);
2300 } else if (gadget_is_mq11xx (gadget
)) {
2301 device_desc
.bcdDevice
= __constant_cpu_to_le16 (0x0207);
2302 } else if (gadget_is_omap (gadget
)) {
2303 device_desc
.bcdDevice
= __constant_cpu_to_le16 (0x0208);
2304 } else if (gadget_is_lh7a40x(gadget
)) {
2305 device_desc
.bcdDevice
= __constant_cpu_to_le16 (0x0209);
2306 } else if (gadget_is_n9604(gadget
)) {
2307 device_desc
.bcdDevice
= __constant_cpu_to_le16 (0x0210);
2308 } else if (gadget_is_pxa27x(gadget
)) {
2309 device_desc
.bcdDevice
= __constant_cpu_to_le16 (0x0211);
2310 } else if (gadget_is_s3c2410(gadget
)) {
2311 device_desc
.bcdDevice
= __constant_cpu_to_le16 (0x0212);
2312 } else if (gadget_is_at91(gadget
)) {
2313 device_desc
.bcdDevice
= __constant_cpu_to_le16 (0x0213);
2315 /* can't assume CDC works. don't want to default to
2316 * anything less functional on CDC-capable hardware,
2317 * so we fail in this case.
2319 dev_err (&gadget
->dev
,
2320 "controller '%s' not recognized\n",
2324 snprintf (manufacturer
, sizeof manufacturer
, "%s %s/%s",
2325 system_utsname
.sysname
, system_utsname
.release
,
2328 /* If there's an RNDIS configuration, that's what Windows wants to
2329 * be using ... so use these product IDs here and in the "linux.inf"
2330 * needed to install MSFT drivers. Current Linux kernels will use
2331 * the second configuration if it's CDC Ethernet, and need some help
2332 * to choose the right configuration otherwise.
2335 device_desc
.idVendor
=
2336 __constant_cpu_to_le16(RNDIS_VENDOR_NUM
);
2337 device_desc
.idProduct
=
2338 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM
);
2339 snprintf (product_desc
, sizeof product_desc
,
2340 "RNDIS/%s", driver_desc
);
2342 /* CDC subset ... recognized by Linux since 2.4.10, but Windows
2343 * drivers aren't widely available.
2346 device_desc
.bDeviceClass
= USB_CLASS_VENDOR_SPEC
;
2347 device_desc
.idVendor
=
2348 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM
);
2349 device_desc
.idProduct
=
2350 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM
);
2353 /* support optional vendor/distro customization */
2356 dev_err (&gadget
->dev
, "idVendor needs idProduct!\n");
2359 device_desc
.idVendor
= cpu_to_le16(idVendor
);
2360 device_desc
.idProduct
= cpu_to_le16(idProduct
);
2362 device_desc
.bcdDevice
= cpu_to_le16(bcdDevice
);
2365 strlcpy (manufacturer
, iManufacturer
, sizeof manufacturer
);
2367 strlcpy (product_desc
, iProduct
, sizeof product_desc
);
2369 /* all we really need is bulk IN/OUT */
2370 usb_ep_autoconfig_reset (gadget
);
2371 in_ep
= usb_ep_autoconfig (gadget
, &fs_source_desc
);
2374 dev_err (&gadget
->dev
,
2375 "can't autoconfigure on %s\n",
2379 EP_IN_NAME
= in_ep
->name
;
2380 in_ep
->driver_data
= in_ep
; /* claim */
2382 out_ep
= usb_ep_autoconfig (gadget
, &fs_sink_desc
);
2385 EP_OUT_NAME
= out_ep
->name
;
2386 out_ep
->driver_data
= out_ep
; /* claim */
2388 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2389 /* CDC Ethernet control interface doesn't require a status endpoint.
2390 * Since some hosts expect one, try to allocate one anyway.
2393 status_ep
= usb_ep_autoconfig (gadget
, &fs_status_desc
);
2395 EP_STATUS_NAME
= status_ep
->name
;
2396 status_ep
->driver_data
= status_ep
; /* claim */
2398 dev_err (&gadget
->dev
,
2399 "can't run RNDIS on %s\n",
2402 #ifdef DEV_CONFIG_CDC
2403 /* pxa25x only does CDC subset; often used with RNDIS */
2405 control_intf
.bNumEndpoints
= 0;
2406 /* FIXME remove endpoint from descriptor list */
2412 /* one config: cdc, else minimal subset */
2414 eth_config
.bNumInterfaces
= 1;
2415 eth_config
.iConfiguration
= STRING_SUBSET
;
2416 fs_subset_descriptors();
2417 hs_subset_descriptors();
2420 /* For now RNDIS is always a second config */
2422 device_desc
.bNumConfigurations
= 2;
2424 #ifdef CONFIG_USB_GADGET_DUALSPEED
2426 dev_qualifier
.bNumConfigurations
= 2;
2428 dev_qualifier
.bDeviceClass
= USB_CLASS_VENDOR_SPEC
;
2430 /* assumes ep0 uses the same value for both speeds ... */
2431 dev_qualifier
.bMaxPacketSize0
= device_desc
.bMaxPacketSize0
;
2433 /* and that all endpoints are dual-speed */
2434 hs_source_desc
.bEndpointAddress
= fs_source_desc
.bEndpointAddress
;
2435 hs_sink_desc
.bEndpointAddress
= fs_sink_desc
.bEndpointAddress
;
2436 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2438 hs_status_desc
.bEndpointAddress
=
2439 fs_status_desc
.bEndpointAddress
;
2441 #endif /* DUALSPEED */
2443 device_desc
.bMaxPacketSize0
= gadget
->ep0
->maxpacket
;
2444 usb_gadget_set_selfpowered (gadget
);
2446 if (gadget
->is_otg
) {
2447 otg_descriptor
.bmAttributes
|= USB_OTG_HNP
,
2448 eth_config
.bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
2449 eth_config
.bMaxPower
= 4;
2450 #ifdef CONFIG_USB_ETH_RNDIS
2451 rndis_config
.bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
2452 rndis_config
.bMaxPower
= 4;
2456 net
= alloc_etherdev (sizeof *dev
);
2459 dev
= netdev_priv(net
);
2460 spin_lock_init (&dev
->lock
);
2461 INIT_WORK (&dev
->work
, eth_work
, dev
);
2462 INIT_LIST_HEAD (&dev
->tx_reqs
);
2463 INIT_LIST_HEAD (&dev
->rx_reqs
);
2465 /* network device setup */
2467 SET_MODULE_OWNER (net
);
2468 strcpy (net
->name
, "usb%d");
2473 dev
->out_ep
= out_ep
;
2474 dev
->status_ep
= status_ep
;
2476 /* Module params for these addresses should come from ID proms.
2477 * The host side address is used with CDC and RNDIS, and commonly
2478 * ends up in a persistent config database.
2480 get_ether_addr(dev_addr
, net
->dev_addr
);
2482 get_ether_addr(host_addr
, dev
->host_mac
);
2483 #ifdef DEV_CONFIG_CDC
2484 snprintf (ethaddr
, sizeof ethaddr
, "%02X%02X%02X%02X%02X%02X",
2485 dev
->host_mac
[0], dev
->host_mac
[1],
2486 dev
->host_mac
[2], dev
->host_mac
[3],
2487 dev
->host_mac
[4], dev
->host_mac
[5]);
2492 status
= rndis_init();
2494 dev_err (&gadget
->dev
, "can't init RNDIS, %d\n",
2500 net
->change_mtu
= eth_change_mtu
;
2501 net
->get_stats
= eth_get_stats
;
2502 net
->hard_start_xmit
= eth_start_xmit
;
2503 net
->open
= eth_open
;
2504 net
->stop
= eth_stop
;
2505 // watchdog_timeo, tx_timeout ...
2506 // set_multicast_list
2507 SET_ETHTOOL_OPS(net
, &ops
);
2509 /* preallocate control message data and buffer */
2510 dev
->req
= eth_req_alloc (gadget
->ep0
, USB_BUFSIZ
);
2513 dev
->req
->complete
= eth_setup_complete
;
2515 /* ... and maybe likewise for status transfer */
2516 if (dev
->status_ep
) {
2517 dev
->stat_req
= eth_req_alloc (dev
->status_ep
,
2519 if (!dev
->stat_req
) {
2520 eth_req_free (gadget
->ep0
, dev
->req
);
2525 /* finish hookup to lower layer ... */
2526 dev
->gadget
= gadget
;
2527 set_gadget_data (gadget
, dev
);
2528 gadget
->ep0
->driver_data
= dev
;
2530 /* two kinds of host-initiated state changes:
2531 * - iff DATA transfer is active, carrier is "on"
2532 * - tx queueing enabled if open *and* carrier is "on"
2534 netif_stop_queue (dev
->net
);
2535 netif_carrier_off (dev
->net
);
2537 // SET_NETDEV_DEV (dev->net, &gadget->dev);
2538 status
= register_netdev (dev
->net
);
2542 INFO (dev
, "%s, version: " DRIVER_VERSION
"\n", driver_desc
);
2543 INFO (dev
, "using %s, OUT %s IN %s%s%s\n", gadget
->name
,
2544 EP_OUT_NAME
, EP_IN_NAME
,
2545 EP_STATUS_NAME
? " STATUS " : "",
2546 EP_STATUS_NAME
? EP_STATUS_NAME
: ""
2548 INFO (dev
, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2549 net
->dev_addr
[0], net
->dev_addr
[1],
2550 net
->dev_addr
[2], net
->dev_addr
[3],
2551 net
->dev_addr
[4], net
->dev_addr
[5]);
2554 INFO (dev
, "HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2555 dev
->host_mac
[0], dev
->host_mac
[1],
2556 dev
->host_mac
[2], dev
->host_mac
[3],
2557 dev
->host_mac
[4], dev
->host_mac
[5]);
2559 #ifdef CONFIG_USB_ETH_RNDIS
2563 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2565 dev
->rndis_config
= rndis_register (rndis_control_ack
);
2566 if (dev
->rndis_config
< 0) {
2568 unregister_netdev (dev
->net
);
2573 /* these set up a lot of the OIDs that RNDIS needs */
2574 rndis_set_host_mac (dev
->rndis_config
, dev
->host_mac
);
2575 if (rndis_set_param_dev (dev
->rndis_config
, dev
->net
,
2578 if (rndis_set_param_vendor (dev
->rndis_config
, vendorID
,
2581 if (rndis_set_param_medium (dev
->rndis_config
,
2585 INFO (dev
, "RNDIS ready\n");
2592 dev_dbg(&gadget
->dev
, "register_netdev failed, %d\n", status
);
2594 eth_unbind (gadget
);
2598 /*-------------------------------------------------------------------------*/
2601 eth_suspend (struct usb_gadget
*gadget
)
2603 struct eth_dev
*dev
= get_gadget_data (gadget
);
2605 DEBUG (dev
, "suspend\n");
2610 eth_resume (struct usb_gadget
*gadget
)
2612 struct eth_dev
*dev
= get_gadget_data (gadget
);
2614 DEBUG (dev
, "resume\n");
2618 /*-------------------------------------------------------------------------*/
2620 static struct usb_gadget_driver eth_driver
= {
2621 #ifdef CONFIG_USB_GADGET_DUALSPEED
2622 .speed
= USB_SPEED_HIGH
,
2624 .speed
= USB_SPEED_FULL
,
2626 .function
= (char *) driver_desc
,
2628 .unbind
= eth_unbind
,
2631 .disconnect
= eth_disconnect
,
2633 .suspend
= eth_suspend
,
2634 .resume
= eth_resume
,
2637 .name
= (char *) shortname
,
2644 MODULE_DESCRIPTION (DRIVER_DESC
);
2645 MODULE_AUTHOR ("David Brownell, Benedikt Spanger");
2646 MODULE_LICENSE ("GPL");
2649 static int __init
init (void)
2651 return usb_gadget_register_driver (ð_driver
);
2655 static void __exit
cleanup (void)
2657 usb_gadget_unregister_driver (ð_driver
);
2659 module_exit (cleanup
);