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/module.h>
27 #include <linux/kernel.h>
28 #include <linux/delay.h>
29 #include <linux/ioport.h>
30 #include <linux/slab.h>
31 #include <linux/errno.h>
32 #include <linux/init.h>
33 #include <linux/timer.h>
34 #include <linux/list.h>
35 #include <linux/interrupt.h>
36 #include <linux/utsname.h>
37 #include <linux/device.h>
38 #include <linux/moduleparam.h>
39 #include <linux/ctype.h>
41 #include <asm/byteorder.h>
44 #include <asm/system.h>
45 #include <asm/uaccess.h>
46 #include <asm/unaligned.h>
48 #include <linux/usb/ch9.h>
49 #include <linux/usb/cdc.h>
50 #include <linux/usb_gadget.h>
52 #include <linux/random.h>
53 #include <linux/netdevice.h>
54 #include <linux/etherdevice.h>
55 #include <linux/ethtool.h>
57 #include "gadget_chips.h"
59 /*-------------------------------------------------------------------------*/
62 * Ethernet gadget driver -- with CDC and non-CDC options
63 * Builds on hardware support for a full duplex link.
65 * CDC Ethernet is the standard USB solution for sending Ethernet frames
66 * using USB. Real hardware tends to use the same framing protocol but look
67 * different for control features. This driver strongly prefers to use
68 * this USB-IF standard as its open-systems interoperability solution;
69 * most host side USB stacks (except from Microsoft) support it.
71 * There's some hardware that can't talk CDC. We make that hardware
72 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
73 * link-level setup only requires activating the configuration. Only the
74 * endpoint descriptors, and product/vendor IDs, are relevant; no control
75 * operations are available. Linux supports it, but other host operating
76 * systems may not. (This is a subset of CDC Ethernet.)
78 * It turns out that if you add a few descriptors to that "CDC Subset",
79 * (Windows) host side drivers from MCCI can treat it as one submode of
80 * a proprietary scheme called "SAFE" ... without needing to know about
81 * specific product/vendor IDs. So we do that, making it easier to use
82 * those MS-Windows drivers. Those added descriptors make it resemble a
83 * CDC MDLM device, but they don't change device behavior at all. (See
84 * MCCI Engineering report 950198 "SAFE Networking Functions".)
86 * A third option is also in use. Rather than CDC Ethernet, or something
87 * simpler, Microsoft pushes their own approach: RNDIS. The published
88 * RNDIS specs are ambiguous and appear to be incomplete, and are also
92 #define DRIVER_DESC "Ethernet Gadget"
93 #define DRIVER_VERSION "May Day 2005"
95 static const char shortname
[] = "ether";
96 static const char driver_desc
[] = DRIVER_DESC
;
98 #define RX_EXTRA 20 /* guard against rx overflows */
102 #ifndef CONFIG_USB_ETH_RNDIS
103 #define rndis_uninit(x) do{}while(0)
104 #define rndis_deregister(c) do{}while(0)
105 #define rndis_exit() do{}while(0)
108 /* CDC and RNDIS support the same host-chosen outgoing packet filters. */
109 #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
110 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
111 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
112 |USB_CDC_PACKET_TYPE_DIRECTED)
115 /*-------------------------------------------------------------------------*/
119 struct usb_gadget
*gadget
;
120 struct usb_request
*req
; /* for control responses */
121 struct usb_request
*stat_req
; /* for cdc & rndis status */
124 struct usb_ep
*in_ep
, *out_ep
, *status_ep
;
125 const struct usb_endpoint_descriptor
129 struct list_head tx_reqs
, rx_reqs
;
131 struct net_device
*net
;
132 struct net_device_stats stats
;
135 struct work_struct work
;
139 unsigned suspended
:1;
142 #define WORK_RX_MEMORY 0
144 u8 host_mac
[ETH_ALEN
];
147 /* This version autoconfigures as much as possible at run-time.
149 * It also ASSUMES a self-powered device, without remote wakeup,
150 * although remote wakeup support would make sense.
153 /*-------------------------------------------------------------------------*/
155 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
156 * Instead: allocate your own, using normal USB-IF procedures.
159 /* Thanks to NetChip Technologies for donating this product ID.
160 * It's for devices with only CDC Ethernet configurations.
162 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
163 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
165 /* For hardware that can't talk CDC, we use the same vendor ID that
166 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
167 * with pxa250. We're protocol-compatible, if the host-side drivers
168 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
169 * drivers that need to hard-wire endpoint numbers have a hook.
171 * The protocol is a minimal subset of CDC Ether, which works on any bulk
172 * hardware that's not deeply broken ... even on hardware that can't talk
173 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
174 * doesn't handle control-OUT).
176 #define SIMPLE_VENDOR_NUM 0x049f
177 #define SIMPLE_PRODUCT_NUM 0x505a
179 /* For hardware that can talk RNDIS and either of the above protocols,
180 * use this ID ... the windows INF files will know it. Unless it's
181 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
182 * the non-RNDIS configuration.
184 #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
185 #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
188 /* Some systems will want different product identifers published in the
189 * device descriptor, either numbers or strings or both. These string
190 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
193 static ushort idVendor
;
194 module_param(idVendor
, ushort
, S_IRUGO
);
195 MODULE_PARM_DESC(idVendor
, "USB Vendor ID");
197 static ushort idProduct
;
198 module_param(idProduct
, ushort
, S_IRUGO
);
199 MODULE_PARM_DESC(idProduct
, "USB Product ID");
201 static ushort bcdDevice
;
202 module_param(bcdDevice
, ushort
, S_IRUGO
);
203 MODULE_PARM_DESC(bcdDevice
, "USB Device version (BCD)");
205 static char *iManufacturer
;
206 module_param(iManufacturer
, charp
, S_IRUGO
);
207 MODULE_PARM_DESC(iManufacturer
, "USB Manufacturer string");
209 static char *iProduct
;
210 module_param(iProduct
, charp
, S_IRUGO
);
211 MODULE_PARM_DESC(iProduct
, "USB Product string");
213 static char *iSerialNumber
;
214 module_param(iSerialNumber
, charp
, S_IRUGO
);
215 MODULE_PARM_DESC(iSerialNumber
, "SerialNumber");
217 /* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */
218 static char *dev_addr
;
219 module_param(dev_addr
, charp
, S_IRUGO
);
220 MODULE_PARM_DESC(dev_addr
, "Device Ethernet Address");
222 /* this address is invisible to ifconfig */
223 static char *host_addr
;
224 module_param(host_addr
, charp
, S_IRUGO
);
225 MODULE_PARM_DESC(host_addr
, "Host Ethernet Address");
228 /*-------------------------------------------------------------------------*/
230 /* Include CDC support if we could run on CDC-capable hardware. */
232 #ifdef CONFIG_USB_GADGET_NET2280
233 #define DEV_CONFIG_CDC
236 #ifdef CONFIG_USB_GADGET_DUMMY_HCD
237 #define DEV_CONFIG_CDC
240 #ifdef CONFIG_USB_GADGET_GOKU
241 #define DEV_CONFIG_CDC
244 #ifdef CONFIG_USB_GADGET_LH7A40X
245 #define DEV_CONFIG_CDC
248 #ifdef CONFIG_USB_GADGET_MQ11XX
249 #define DEV_CONFIG_CDC
252 #ifdef CONFIG_USB_GADGET_OMAP
253 #define DEV_CONFIG_CDC
256 #ifdef CONFIG_USB_GADGET_N9604
257 #define DEV_CONFIG_CDC
260 #ifdef CONFIG_USB_GADGET_PXA27X
261 #define DEV_CONFIG_CDC
264 #ifdef CONFIG_USB_GADGET_S3C2410
265 #define DEV_CONFIG_CDC
268 #ifdef CONFIG_USB_GADGET_AT91
269 #define DEV_CONFIG_CDC
272 #ifdef CONFIG_USB_GADGET_MUSBHSFC
273 #define DEV_CONFIG_CDC
276 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
277 #define DEV_CONFIG_CDC
280 #ifdef CONFIG_USB_GADGET_ATMEL_USBA
281 #define DEV_CONFIG_CDC
284 #ifdef CONFIG_USB_GADGET_FSL_USB2
285 #define DEV_CONFIG_CDC
288 /* For CDC-incapable hardware, choose the simple cdc subset.
289 * Anything that talks bulk (without notable bugs) can do this.
291 #ifdef CONFIG_USB_GADGET_PXA2XX
292 #define DEV_CONFIG_SUBSET
295 #ifdef CONFIG_USB_GADGET_SUPERH
296 #define DEV_CONFIG_SUBSET
299 #ifdef CONFIG_USB_GADGET_SA1100
300 /* use non-CDC for backwards compatibility */
301 #define DEV_CONFIG_SUBSET
304 #ifdef CONFIG_USB_GADGET_M66592
305 #define DEV_CONFIG_CDC
308 #ifdef CONFIG_USB_GADGET_AMD5536UDC
309 #define DEV_CONFIG_CDC
313 /*-------------------------------------------------------------------------*/
315 /* "main" config is either CDC, or its simple subset */
316 static inline int is_cdc(struct eth_dev
*dev
)
318 #if !defined(DEV_CONFIG_SUBSET)
319 return 1; /* only cdc possible */
320 #elif !defined (DEV_CONFIG_CDC)
321 return 0; /* only subset possible */
323 return dev
->cdc
; /* depends on what hardware we found */
327 /* "secondary" RNDIS config may sometimes be activated */
328 static inline int rndis_active(struct eth_dev
*dev
)
330 #ifdef CONFIG_USB_ETH_RNDIS
337 #define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
338 #define cdc_active(dev) ( is_cdc(dev) && !rndis_active(dev))
342 #define DEFAULT_QLEN 2 /* double buffering by default */
344 /* peak bulk transfer bits-per-second */
345 #define HS_BPS (13 * 512 * 8 * 1000 * 8)
346 #define FS_BPS (19 * 64 * 1 * 1000 * 8)
348 #ifdef CONFIG_USB_GADGET_DUALSPEED
349 #define DEVSPEED USB_SPEED_HIGH
351 static unsigned qmult
= 5;
352 module_param (qmult
, uint
, S_IRUGO
|S_IWUSR
);
355 /* for dual-speed hardware, use deeper queues at highspeed */
356 #define qlen(gadget) \
357 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
359 /* also defer IRQs on highspeed TX */
360 #define TX_DELAY qmult
362 static inline int BITRATE(struct usb_gadget
*g
)
364 return (g
->speed
== USB_SPEED_HIGH
) ? HS_BPS
: FS_BPS
;
367 #else /* full speed (low speed doesn't do bulk) */
368 #define DEVSPEED USB_SPEED_FULL
370 #define qlen(gadget) DEFAULT_QLEN
372 static inline int BITRATE(struct usb_gadget
*g
)
379 /*-------------------------------------------------------------------------*/
381 #define xprintk(d,level,fmt,args...) \
382 printk(level "%s: " fmt , (d)->net->name , ## args)
386 #define DEBUG(dev,fmt,args...) \
387 xprintk(dev , KERN_DEBUG , fmt , ## args)
389 #define DEBUG(dev,fmt,args...) \
396 #define VDEBUG(dev,fmt,args...) \
400 #define ERROR(dev,fmt,args...) \
401 xprintk(dev , KERN_ERR , fmt , ## args)
402 #define WARN(dev,fmt,args...) \
403 xprintk(dev , KERN_WARNING , fmt , ## args)
404 #define INFO(dev,fmt,args...) \
405 xprintk(dev , KERN_INFO , fmt , ## args)
407 /*-------------------------------------------------------------------------*/
409 /* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
410 * ep0 implementation: descriptors, config management, setup().
411 * also optional class-specific notification interrupt transfer.
415 * DESCRIPTORS ... most are static, but strings and (full) configuration
416 * descriptors are built on demand. For now we do either full CDC, or
417 * our simple subset, with RNDIS as an optional second configuration.
419 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
420 * the class descriptors match a modem (they're ignored; it's really just
421 * Ethernet functionality), they don't need the NOP altsetting, and the
422 * status transfer endpoint isn't optional.
425 #define STRING_MANUFACTURER 1
426 #define STRING_PRODUCT 2
427 #define STRING_ETHADDR 3
428 #define STRING_DATA 4
429 #define STRING_CONTROL 5
430 #define STRING_RNDIS_CONTROL 6
432 #define STRING_SUBSET 8
433 #define STRING_RNDIS 9
434 #define STRING_SERIALNUMBER 10
436 /* holds our biggest descriptor (or RNDIS response) */
437 #define USB_BUFSIZ 256
440 * This device advertises one configuration, eth_config, unless RNDIS
441 * is enabled (rndis_config) on hardware supporting at least two configs.
443 * NOTE: Controllers like superh_udc should probably be able to use
444 * an RNDIS-only configuration.
446 * FIXME define some higher-powered configurations to make it easier
447 * to recharge batteries ...
450 #define DEV_CONFIG_VALUE 1 /* cdc or subset */
451 #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
453 static struct usb_device_descriptor
455 .bLength
= sizeof device_desc
,
456 .bDescriptorType
= USB_DT_DEVICE
,
458 .bcdUSB
= __constant_cpu_to_le16 (0x0200),
460 .bDeviceClass
= USB_CLASS_COMM
,
461 .bDeviceSubClass
= 0,
462 .bDeviceProtocol
= 0,
464 .idVendor
= __constant_cpu_to_le16 (CDC_VENDOR_NUM
),
465 .idProduct
= __constant_cpu_to_le16 (CDC_PRODUCT_NUM
),
466 .iManufacturer
= STRING_MANUFACTURER
,
467 .iProduct
= STRING_PRODUCT
,
468 .bNumConfigurations
= 1,
471 static struct usb_otg_descriptor
473 .bLength
= sizeof otg_descriptor
,
474 .bDescriptorType
= USB_DT_OTG
,
476 .bmAttributes
= USB_OTG_SRP
,
479 static struct usb_config_descriptor
481 .bLength
= sizeof eth_config
,
482 .bDescriptorType
= USB_DT_CONFIG
,
484 /* compute wTotalLength on the fly */
486 .bConfigurationValue
= DEV_CONFIG_VALUE
,
487 .iConfiguration
= STRING_CDC
,
488 .bmAttributes
= USB_CONFIG_ATT_ONE
| USB_CONFIG_ATT_SELFPOWER
,
492 #ifdef CONFIG_USB_ETH_RNDIS
493 static struct usb_config_descriptor
495 .bLength
= sizeof rndis_config
,
496 .bDescriptorType
= USB_DT_CONFIG
,
498 /* compute wTotalLength on the fly */
500 .bConfigurationValue
= DEV_RNDIS_CONFIG_VALUE
,
501 .iConfiguration
= STRING_RNDIS
,
502 .bmAttributes
= USB_CONFIG_ATT_ONE
| USB_CONFIG_ATT_SELFPOWER
,
508 * Compared to the simple CDC subset, the full CDC Ethernet model adds
509 * three class descriptors, two interface descriptors, optional status
510 * endpoint. Both have a "data" interface and two bulk endpoints.
511 * There are also differences in how control requests are handled.
513 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
514 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
515 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
516 * work around those bugs) are unlikely to ever come from MSFT, you may
517 * wish to avoid using RNDIS.
519 * MCCI offers an alternative to RNDIS if you need to connect to Windows
520 * but have hardware that can't support CDC Ethernet. We add descriptors
521 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
522 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
523 * get those drivers from MCCI, or bundled with various products.
526 #ifdef DEV_CONFIG_CDC
527 static struct usb_interface_descriptor
529 .bLength
= sizeof control_intf
,
530 .bDescriptorType
= USB_DT_INTERFACE
,
532 .bInterfaceNumber
= 0,
533 /* status endpoint is optional; this may be patched later */
535 .bInterfaceClass
= USB_CLASS_COMM
,
536 .bInterfaceSubClass
= USB_CDC_SUBCLASS_ETHERNET
,
537 .bInterfaceProtocol
= USB_CDC_PROTO_NONE
,
538 .iInterface
= STRING_CONTROL
,
542 #ifdef CONFIG_USB_ETH_RNDIS
543 static const struct usb_interface_descriptor
544 rndis_control_intf
= {
545 .bLength
= sizeof rndis_control_intf
,
546 .bDescriptorType
= USB_DT_INTERFACE
,
548 .bInterfaceNumber
= 0,
550 .bInterfaceClass
= USB_CLASS_COMM
,
551 .bInterfaceSubClass
= USB_CDC_SUBCLASS_ACM
,
552 .bInterfaceProtocol
= USB_CDC_ACM_PROTO_VENDOR
,
553 .iInterface
= STRING_RNDIS_CONTROL
,
557 static const struct usb_cdc_header_desc header_desc
= {
558 .bLength
= sizeof header_desc
,
559 .bDescriptorType
= USB_DT_CS_INTERFACE
,
560 .bDescriptorSubType
= USB_CDC_HEADER_TYPE
,
562 .bcdCDC
= __constant_cpu_to_le16 (0x0110),
565 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
567 static const struct usb_cdc_union_desc union_desc
= {
568 .bLength
= sizeof union_desc
,
569 .bDescriptorType
= USB_DT_CS_INTERFACE
,
570 .bDescriptorSubType
= USB_CDC_UNION_TYPE
,
572 .bMasterInterface0
= 0, /* index of control interface */
573 .bSlaveInterface0
= 1, /* index of DATA interface */
576 #endif /* CDC || RNDIS */
578 #ifdef CONFIG_USB_ETH_RNDIS
580 static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor
= {
581 .bLength
= sizeof call_mgmt_descriptor
,
582 .bDescriptorType
= USB_DT_CS_INTERFACE
,
583 .bDescriptorSubType
= USB_CDC_CALL_MANAGEMENT_TYPE
,
585 .bmCapabilities
= 0x00,
586 .bDataInterface
= 0x01,
589 static const struct usb_cdc_acm_descriptor acm_descriptor
= {
590 .bLength
= sizeof acm_descriptor
,
591 .bDescriptorType
= USB_DT_CS_INTERFACE
,
592 .bDescriptorSubType
= USB_CDC_ACM_TYPE
,
594 .bmCapabilities
= 0x00,
599 #ifndef DEV_CONFIG_CDC
601 /* "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
602 * ways: data endpoints live in the control interface, there's no data
603 * interface, and it's not used to talk to a cell phone radio.
606 static const struct usb_cdc_mdlm_desc mdlm_desc
= {
607 .bLength
= sizeof mdlm_desc
,
608 .bDescriptorType
= USB_DT_CS_INTERFACE
,
609 .bDescriptorSubType
= USB_CDC_MDLM_TYPE
,
611 .bcdVersion
= __constant_cpu_to_le16(0x0100),
613 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
614 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
618 /* since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
619 * can't really use its struct. All we do here is say that we're using
620 * the submode of "SAFE" which directly matches the CDC Subset.
622 static const u8 mdlm_detail_desc
[] = {
625 USB_CDC_MDLM_DETAIL_TYPE
,
628 0, /* network control capabilities (none) */
629 0, /* network data capabilities ("raw" encapsulation) */
634 static const struct usb_cdc_ether_desc ether_desc
= {
635 .bLength
= sizeof ether_desc
,
636 .bDescriptorType
= USB_DT_CS_INTERFACE
,
637 .bDescriptorSubType
= USB_CDC_ETHERNET_TYPE
,
639 /* this descriptor actually adds value, surprise! */
640 .iMACAddress
= STRING_ETHADDR
,
641 .bmEthernetStatistics
= __constant_cpu_to_le32 (0), /* no statistics */
642 .wMaxSegmentSize
= __constant_cpu_to_le16 (ETH_FRAME_LEN
),
643 .wNumberMCFilters
= __constant_cpu_to_le16 (0),
644 .bNumberPowerFilters
= 0,
648 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
650 /* include the status endpoint if we can, even where it's optional.
651 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
652 * packet, to simplify cancellation; and a big transfer interval, to
653 * waste less bandwidth.
655 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
656 * if they ignore the connect/disconnect notifications that real aether
657 * can provide. more advanced cdc configurations might want to support
658 * encapsulated commands (vendor-specific, using control-OUT).
660 * RNDIS requires the status endpoint, since it uses that encapsulation
661 * mechanism for its funky RPC scheme.
664 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
665 #define STATUS_BYTECOUNT 16 /* 8 byte header + data */
667 static struct usb_endpoint_descriptor
669 .bLength
= USB_DT_ENDPOINT_SIZE
,
670 .bDescriptorType
= USB_DT_ENDPOINT
,
672 .bEndpointAddress
= USB_DIR_IN
,
673 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
674 .wMaxPacketSize
= __constant_cpu_to_le16 (STATUS_BYTECOUNT
),
675 .bInterval
= 1 << LOG2_STATUS_INTERVAL_MSEC
,
679 #ifdef DEV_CONFIG_CDC
681 /* the default data interface has no endpoints ... */
683 static const struct usb_interface_descriptor
685 .bLength
= sizeof data_nop_intf
,
686 .bDescriptorType
= USB_DT_INTERFACE
,
688 .bInterfaceNumber
= 1,
689 .bAlternateSetting
= 0,
691 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
692 .bInterfaceSubClass
= 0,
693 .bInterfaceProtocol
= 0,
696 /* ... but the "real" data interface has two bulk endpoints */
698 static const struct usb_interface_descriptor
700 .bLength
= sizeof data_intf
,
701 .bDescriptorType
= USB_DT_INTERFACE
,
703 .bInterfaceNumber
= 1,
704 .bAlternateSetting
= 1,
706 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
707 .bInterfaceSubClass
= 0,
708 .bInterfaceProtocol
= 0,
709 .iInterface
= STRING_DATA
,
714 #ifdef CONFIG_USB_ETH_RNDIS
716 /* RNDIS doesn't activate by changing to the "real" altsetting */
718 static const struct usb_interface_descriptor
720 .bLength
= sizeof rndis_data_intf
,
721 .bDescriptorType
= USB_DT_INTERFACE
,
723 .bInterfaceNumber
= 1,
724 .bAlternateSetting
= 0,
726 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
727 .bInterfaceSubClass
= 0,
728 .bInterfaceProtocol
= 0,
729 .iInterface
= STRING_DATA
,
734 #ifdef DEV_CONFIG_SUBSET
737 * "Simple" CDC-subset option is a simple vendor-neutral model that most
738 * full speed controllers can handle: one interface, two bulk endpoints.
740 * To assist host side drivers, we fancy it up a bit, and add descriptors
741 * so some host side drivers will understand it as a "SAFE" variant.
744 static const struct usb_interface_descriptor
746 .bLength
= sizeof subset_data_intf
,
747 .bDescriptorType
= USB_DT_INTERFACE
,
749 .bInterfaceNumber
= 0,
750 .bAlternateSetting
= 0,
752 .bInterfaceClass
= USB_CLASS_COMM
,
753 .bInterfaceSubClass
= USB_CDC_SUBCLASS_MDLM
,
754 .bInterfaceProtocol
= 0,
755 .iInterface
= STRING_DATA
,
761 static struct usb_endpoint_descriptor
763 .bLength
= USB_DT_ENDPOINT_SIZE
,
764 .bDescriptorType
= USB_DT_ENDPOINT
,
766 .bEndpointAddress
= USB_DIR_IN
,
767 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
770 static struct usb_endpoint_descriptor
772 .bLength
= USB_DT_ENDPOINT_SIZE
,
773 .bDescriptorType
= USB_DT_ENDPOINT
,
775 .bEndpointAddress
= USB_DIR_OUT
,
776 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
779 static const struct usb_descriptor_header
*fs_eth_function
[11] = {
780 (struct usb_descriptor_header
*) &otg_descriptor
,
781 #ifdef DEV_CONFIG_CDC
782 /* "cdc" mode descriptors */
783 (struct usb_descriptor_header
*) &control_intf
,
784 (struct usb_descriptor_header
*) &header_desc
,
785 (struct usb_descriptor_header
*) &union_desc
,
786 (struct usb_descriptor_header
*) ðer_desc
,
787 /* NOTE: status endpoint may need to be removed */
788 (struct usb_descriptor_header
*) &fs_status_desc
,
789 /* data interface, with altsetting */
790 (struct usb_descriptor_header
*) &data_nop_intf
,
791 (struct usb_descriptor_header
*) &data_intf
,
792 (struct usb_descriptor_header
*) &fs_source_desc
,
793 (struct usb_descriptor_header
*) &fs_sink_desc
,
795 #endif /* DEV_CONFIG_CDC */
798 static inline void __init
fs_subset_descriptors(void)
800 #ifdef DEV_CONFIG_SUBSET
801 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
802 fs_eth_function
[1] = (struct usb_descriptor_header
*) &subset_data_intf
;
803 fs_eth_function
[2] = (struct usb_descriptor_header
*) &header_desc
;
804 fs_eth_function
[3] = (struct usb_descriptor_header
*) &mdlm_desc
;
805 fs_eth_function
[4] = (struct usb_descriptor_header
*) &mdlm_detail_desc
;
806 fs_eth_function
[5] = (struct usb_descriptor_header
*) ðer_desc
;
807 fs_eth_function
[6] = (struct usb_descriptor_header
*) &fs_source_desc
;
808 fs_eth_function
[7] = (struct usb_descriptor_header
*) &fs_sink_desc
;
809 fs_eth_function
[8] = NULL
;
811 fs_eth_function
[1] = NULL
;
815 #ifdef CONFIG_USB_ETH_RNDIS
816 static const struct usb_descriptor_header
*fs_rndis_function
[] = {
817 (struct usb_descriptor_header
*) &otg_descriptor
,
818 /* control interface matches ACM, not Ethernet */
819 (struct usb_descriptor_header
*) &rndis_control_intf
,
820 (struct usb_descriptor_header
*) &header_desc
,
821 (struct usb_descriptor_header
*) &call_mgmt_descriptor
,
822 (struct usb_descriptor_header
*) &acm_descriptor
,
823 (struct usb_descriptor_header
*) &union_desc
,
824 (struct usb_descriptor_header
*) &fs_status_desc
,
825 /* data interface has no altsetting */
826 (struct usb_descriptor_header
*) &rndis_data_intf
,
827 (struct usb_descriptor_header
*) &fs_source_desc
,
828 (struct usb_descriptor_header
*) &fs_sink_desc
,
833 #ifdef CONFIG_USB_GADGET_DUALSPEED
836 * usb 2.0 devices need to expose both high speed and full speed
837 * descriptors, unless they only run at full speed.
840 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
841 static struct usb_endpoint_descriptor
843 .bLength
= USB_DT_ENDPOINT_SIZE
,
844 .bDescriptorType
= USB_DT_ENDPOINT
,
846 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
847 .wMaxPacketSize
= __constant_cpu_to_le16 (STATUS_BYTECOUNT
),
848 .bInterval
= LOG2_STATUS_INTERVAL_MSEC
+ 4,
850 #endif /* DEV_CONFIG_CDC */
852 static struct usb_endpoint_descriptor
854 .bLength
= USB_DT_ENDPOINT_SIZE
,
855 .bDescriptorType
= USB_DT_ENDPOINT
,
857 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
858 .wMaxPacketSize
= __constant_cpu_to_le16 (512),
861 static struct usb_endpoint_descriptor
863 .bLength
= USB_DT_ENDPOINT_SIZE
,
864 .bDescriptorType
= USB_DT_ENDPOINT
,
866 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
867 .wMaxPacketSize
= __constant_cpu_to_le16 (512),
870 static struct usb_qualifier_descriptor
872 .bLength
= sizeof dev_qualifier
,
873 .bDescriptorType
= USB_DT_DEVICE_QUALIFIER
,
875 .bcdUSB
= __constant_cpu_to_le16 (0x0200),
876 .bDeviceClass
= USB_CLASS_COMM
,
878 .bNumConfigurations
= 1,
881 static const struct usb_descriptor_header
*hs_eth_function
[11] = {
882 (struct usb_descriptor_header
*) &otg_descriptor
,
883 #ifdef DEV_CONFIG_CDC
884 /* "cdc" mode descriptors */
885 (struct usb_descriptor_header
*) &control_intf
,
886 (struct usb_descriptor_header
*) &header_desc
,
887 (struct usb_descriptor_header
*) &union_desc
,
888 (struct usb_descriptor_header
*) ðer_desc
,
889 /* NOTE: status endpoint may need to be removed */
890 (struct usb_descriptor_header
*) &hs_status_desc
,
891 /* data interface, with altsetting */
892 (struct usb_descriptor_header
*) &data_nop_intf
,
893 (struct usb_descriptor_header
*) &data_intf
,
894 (struct usb_descriptor_header
*) &hs_source_desc
,
895 (struct usb_descriptor_header
*) &hs_sink_desc
,
897 #endif /* DEV_CONFIG_CDC */
900 static inline void __init
hs_subset_descriptors(void)
902 #ifdef DEV_CONFIG_SUBSET
903 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
904 hs_eth_function
[1] = (struct usb_descriptor_header
*) &subset_data_intf
;
905 hs_eth_function
[2] = (struct usb_descriptor_header
*) &header_desc
;
906 hs_eth_function
[3] = (struct usb_descriptor_header
*) &mdlm_desc
;
907 hs_eth_function
[4] = (struct usb_descriptor_header
*) &mdlm_detail_desc
;
908 hs_eth_function
[5] = (struct usb_descriptor_header
*) ðer_desc
;
909 hs_eth_function
[6] = (struct usb_descriptor_header
*) &hs_source_desc
;
910 hs_eth_function
[7] = (struct usb_descriptor_header
*) &hs_sink_desc
;
911 hs_eth_function
[8] = NULL
;
913 hs_eth_function
[1] = NULL
;
917 #ifdef CONFIG_USB_ETH_RNDIS
918 static const struct usb_descriptor_header
*hs_rndis_function
[] = {
919 (struct usb_descriptor_header
*) &otg_descriptor
,
920 /* control interface matches ACM, not Ethernet */
921 (struct usb_descriptor_header
*) &rndis_control_intf
,
922 (struct usb_descriptor_header
*) &header_desc
,
923 (struct usb_descriptor_header
*) &call_mgmt_descriptor
,
924 (struct usb_descriptor_header
*) &acm_descriptor
,
925 (struct usb_descriptor_header
*) &union_desc
,
926 (struct usb_descriptor_header
*) &hs_status_desc
,
927 /* data interface has no altsetting */
928 (struct usb_descriptor_header
*) &rndis_data_intf
,
929 (struct usb_descriptor_header
*) &hs_source_desc
,
930 (struct usb_descriptor_header
*) &hs_sink_desc
,
936 /* maxpacket and other transfer characteristics vary by speed. */
937 #define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
941 /* if there's no high speed support, maxpacket doesn't change. */
942 #define ep_desc(g,hs,fs) (((void)(g)), (fs))
944 static inline void __init
hs_subset_descriptors(void)
948 #endif /* !CONFIG_USB_GADGET_DUALSPEED */
950 /*-------------------------------------------------------------------------*/
952 /* descriptors that are built on-demand */
954 static char manufacturer
[50];
955 static char product_desc
[40] = DRIVER_DESC
;
956 static char serial_number
[20];
958 /* address that the host will use ... usually assigned at random */
959 static char ethaddr
[2 * ETH_ALEN
+ 1];
961 /* static strings, in UTF-8 */
962 static struct usb_string strings
[] = {
963 { STRING_MANUFACTURER
, manufacturer
, },
964 { STRING_PRODUCT
, product_desc
, },
965 { STRING_SERIALNUMBER
, serial_number
, },
966 { STRING_DATA
, "Ethernet Data", },
967 { STRING_ETHADDR
, ethaddr
, },
968 #ifdef DEV_CONFIG_CDC
969 { STRING_CDC
, "CDC Ethernet", },
970 { STRING_CONTROL
, "CDC Communications Control", },
972 #ifdef DEV_CONFIG_SUBSET
973 { STRING_SUBSET
, "CDC Ethernet Subset", },
975 #ifdef CONFIG_USB_ETH_RNDIS
976 { STRING_RNDIS
, "RNDIS", },
977 { STRING_RNDIS_CONTROL
, "RNDIS Communications Control", },
979 { } /* end of list */
982 static struct usb_gadget_strings stringtab
= {
983 .language
= 0x0409, /* en-us */
988 * one config, two interfaces: control, data.
989 * complications: class descriptors, and an altsetting.
992 config_buf (enum usb_device_speed speed
,
994 unsigned index
, int is_otg
)
997 const struct usb_config_descriptor
*config
;
998 const struct usb_descriptor_header
**function
;
999 #ifdef CONFIG_USB_GADGET_DUALSPEED
1000 int hs
= (speed
== USB_SPEED_HIGH
);
1002 if (type
== USB_DT_OTHER_SPEED_CONFIG
)
1004 #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
1006 #define which_fn(t) (fs_ ## t ## _function)
1009 if (index
>= device_desc
.bNumConfigurations
)
1012 #ifdef CONFIG_USB_ETH_RNDIS
1013 /* list the RNDIS config first, to make Microsoft's drivers
1014 * happy. DOCSIS 1.0 needs this too.
1016 if (device_desc
.bNumConfigurations
== 2 && index
== 0) {
1017 config
= &rndis_config
;
1018 function
= which_fn (rndis
);
1022 config
= ð_config
;
1023 function
= which_fn (eth
);
1026 /* for now, don't advertise srp-only devices */
1030 len
= usb_gadget_config_buf (config
, buf
, USB_BUFSIZ
, function
);
1033 ((struct usb_config_descriptor
*) buf
)->bDescriptorType
= type
;
1037 /*-------------------------------------------------------------------------*/
1039 static void eth_start (struct eth_dev
*dev
, gfp_t gfp_flags
);
1040 static int alloc_requests (struct eth_dev
*dev
, unsigned n
, gfp_t gfp_flags
);
1043 set_ether_config (struct eth_dev
*dev
, gfp_t gfp_flags
)
1046 struct usb_gadget
*gadget
= dev
->gadget
;
1048 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
1049 /* status endpoint used for RNDIS and (optionally) CDC */
1050 if (!subset_active(dev
) && dev
->status_ep
) {
1051 dev
->status
= ep_desc (gadget
, &hs_status_desc
,
1053 dev
->status_ep
->driver_data
= dev
;
1055 result
= usb_ep_enable (dev
->status_ep
, dev
->status
);
1057 DEBUG (dev
, "enable %s --> %d\n",
1058 dev
->status_ep
->name
, result
);
1064 dev
->in
= ep_desc(gadget
, &hs_source_desc
, &fs_source_desc
);
1065 dev
->in_ep
->driver_data
= dev
;
1067 dev
->out
= ep_desc(gadget
, &hs_sink_desc
, &fs_sink_desc
);
1068 dev
->out_ep
->driver_data
= dev
;
1070 /* With CDC, the host isn't allowed to use these two data
1071 * endpoints in the default altsetting for the interface.
1072 * so we don't activate them yet. Reset from SET_INTERFACE.
1074 * Strictly speaking RNDIS should work the same: activation is
1075 * a side effect of setting a packet filter. Deactivation is
1076 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
1078 if (!cdc_active(dev
)) {
1079 result
= usb_ep_enable (dev
->in_ep
, dev
->in
);
1081 DEBUG(dev
, "enable %s --> %d\n",
1082 dev
->in_ep
->name
, result
);
1086 result
= usb_ep_enable (dev
->out_ep
, dev
->out
);
1088 DEBUG (dev
, "enable %s --> %d\n",
1089 dev
->out_ep
->name
, result
);
1096 result
= alloc_requests (dev
, qlen (gadget
), gfp_flags
);
1098 /* on error, disable any endpoints */
1100 if (!subset_active(dev
))
1101 (void) usb_ep_disable (dev
->status_ep
);
1103 (void) usb_ep_disable (dev
->in_ep
);
1104 (void) usb_ep_disable (dev
->out_ep
);
1109 /* activate non-CDC configs right away
1110 * this isn't strictly according to the RNDIS spec
1112 if (!cdc_active (dev
)) {
1113 netif_carrier_on (dev
->net
);
1114 if (netif_running (dev
->net
)) {
1115 spin_unlock (&dev
->lock
);
1116 eth_start (dev
, GFP_ATOMIC
);
1117 spin_lock (&dev
->lock
);
1122 DEBUG (dev
, "qlen %d\n", qlen (gadget
));
1124 /* caller is responsible for cleanup on error */
1128 static void eth_reset_config (struct eth_dev
*dev
)
1130 struct usb_request
*req
;
1132 if (dev
->config
== 0)
1135 DEBUG (dev
, "%s\n", __FUNCTION__
);
1137 netif_stop_queue (dev
->net
);
1138 netif_carrier_off (dev
->net
);
1139 rndis_uninit(dev
->rndis_config
);
1141 /* disable endpoints, forcing (synchronous) completion of
1142 * pending i/o. then free the requests.
1145 usb_ep_disable (dev
->in_ep
);
1146 spin_lock(&dev
->req_lock
);
1147 while (likely (!list_empty (&dev
->tx_reqs
))) {
1148 req
= container_of (dev
->tx_reqs
.next
,
1149 struct usb_request
, list
);
1150 list_del (&req
->list
);
1152 spin_unlock(&dev
->req_lock
);
1153 usb_ep_free_request (dev
->in_ep
, req
);
1154 spin_lock(&dev
->req_lock
);
1156 spin_unlock(&dev
->req_lock
);
1159 usb_ep_disable (dev
->out_ep
);
1160 spin_lock(&dev
->req_lock
);
1161 while (likely (!list_empty (&dev
->rx_reqs
))) {
1162 req
= container_of (dev
->rx_reqs
.next
,
1163 struct usb_request
, list
);
1164 list_del (&req
->list
);
1166 spin_unlock(&dev
->req_lock
);
1167 usb_ep_free_request (dev
->out_ep
, req
);
1168 spin_lock(&dev
->req_lock
);
1170 spin_unlock(&dev
->req_lock
);
1174 usb_ep_disable (dev
->status_ep
);
1177 dev
->cdc_filter
= 0;
1181 /* change our operational config. must agree with the code
1182 * that returns config descriptors, and altsetting code.
1185 eth_set_config (struct eth_dev
*dev
, unsigned number
, gfp_t gfp_flags
)
1188 struct usb_gadget
*gadget
= dev
->gadget
;
1190 if (gadget_is_sa1100 (gadget
)
1192 && atomic_read (&dev
->tx_qlen
) != 0) {
1193 /* tx fifo is full, but we can't clear it...*/
1194 INFO (dev
, "can't change configurations\n");
1197 eth_reset_config (dev
);
1200 case DEV_CONFIG_VALUE
:
1201 result
= set_ether_config (dev
, gfp_flags
);
1203 #ifdef CONFIG_USB_ETH_RNDIS
1204 case DEV_RNDIS_CONFIG_VALUE
:
1206 result
= set_ether_config (dev
, gfp_flags
);
1218 eth_reset_config (dev
);
1219 usb_gadget_vbus_draw(dev
->gadget
,
1220 dev
->gadget
->is_otg
? 8 : 100);
1225 power
= 2 * eth_config
.bMaxPower
;
1226 usb_gadget_vbus_draw(dev
->gadget
, power
);
1228 switch (gadget
->speed
) {
1229 case USB_SPEED_FULL
: speed
= "full"; break;
1230 #ifdef CONFIG_USB_GADGET_DUALSPEED
1231 case USB_SPEED_HIGH
: speed
= "high"; break;
1233 default: speed
= "?"; break;
1236 dev
->config
= number
;
1237 INFO (dev
, "%s speed config #%d: %d mA, %s, using %s\n",
1238 speed
, number
, power
, driver_desc
,
1243 : "CDC Ethernet Subset"));
1248 /*-------------------------------------------------------------------------*/
1250 #ifdef DEV_CONFIG_CDC
1252 /* The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
1253 * only to notify the host about link status changes (which we support) or
1254 * report completion of some encapsulated command (as used in RNDIS). Since
1255 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1256 * command mechanism; and only one status request is ever queued.
1259 static void eth_status_complete (struct usb_ep
*ep
, struct usb_request
*req
)
1261 struct usb_cdc_notification
*event
= req
->buf
;
1262 int value
= req
->status
;
1263 struct eth_dev
*dev
= ep
->driver_data
;
1265 /* issue the second notification if host reads the first */
1266 if (event
->bNotificationType
== USB_CDC_NOTIFY_NETWORK_CONNECTION
1268 __le32
*data
= req
->buf
+ sizeof *event
;
1270 event
->bmRequestType
= 0xA1;
1271 event
->bNotificationType
= USB_CDC_NOTIFY_SPEED_CHANGE
;
1272 event
->wValue
= __constant_cpu_to_le16 (0);
1273 event
->wIndex
= __constant_cpu_to_le16 (1);
1274 event
->wLength
= __constant_cpu_to_le16 (8);
1276 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1277 data
[0] = data
[1] = cpu_to_le32 (BITRATE (dev
->gadget
));
1279 req
->length
= STATUS_BYTECOUNT
;
1280 value
= usb_ep_queue (ep
, req
, GFP_ATOMIC
);
1281 DEBUG (dev
, "send SPEED_CHANGE --> %d\n", value
);
1284 } else if (value
!= -ECONNRESET
)
1285 DEBUG (dev
, "event %02x --> %d\n",
1286 event
->bNotificationType
, value
);
1287 req
->context
= NULL
;
1290 static void issue_start_status (struct eth_dev
*dev
)
1292 struct usb_request
*req
= dev
->stat_req
;
1293 struct usb_cdc_notification
*event
;
1296 DEBUG (dev
, "%s, flush old status first\n", __FUNCTION__
);
1300 * FIXME ugly idiom, maybe we'd be better with just
1301 * a "cancel the whole queue" primitive since any
1302 * unlink-one primitive has way too many error modes.
1303 * here, we "know" toggle is already clear...
1305 * FIXME iff req->context != null just dequeue it
1307 usb_ep_disable (dev
->status_ep
);
1308 usb_ep_enable (dev
->status_ep
, dev
->status
);
1310 /* 3.8.1 says to issue first NETWORK_CONNECTION, then
1311 * a SPEED_CHANGE. could be useful in some configs.
1314 event
->bmRequestType
= 0xA1;
1315 event
->bNotificationType
= USB_CDC_NOTIFY_NETWORK_CONNECTION
;
1316 event
->wValue
= __constant_cpu_to_le16 (1); /* connected */
1317 event
->wIndex
= __constant_cpu_to_le16 (1);
1320 req
->length
= sizeof *event
;
1321 req
->complete
= eth_status_complete
;
1324 value
= usb_ep_queue (dev
->status_ep
, req
, GFP_ATOMIC
);
1326 DEBUG (dev
, "status buf queue --> %d\n", value
);
1331 /*-------------------------------------------------------------------------*/
1333 static void eth_setup_complete (struct usb_ep
*ep
, struct usb_request
*req
)
1335 if (req
->status
|| req
->actual
!= req
->length
)
1336 DEBUG ((struct eth_dev
*) ep
->driver_data
,
1337 "setup complete --> %d, %d/%d\n",
1338 req
->status
, req
->actual
, req
->length
);
1341 #ifdef CONFIG_USB_ETH_RNDIS
1343 static void rndis_response_complete (struct usb_ep
*ep
, struct usb_request
*req
)
1345 if (req
->status
|| req
->actual
!= req
->length
)
1346 DEBUG ((struct eth_dev
*) ep
->driver_data
,
1347 "rndis response complete --> %d, %d/%d\n",
1348 req
->status
, req
->actual
, req
->length
);
1350 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1353 static void rndis_command_complete (struct usb_ep
*ep
, struct usb_request
*req
)
1355 struct eth_dev
*dev
= ep
->driver_data
;
1358 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1359 spin_lock(&dev
->lock
);
1360 status
= rndis_msg_parser (dev
->rndis_config
, (u8
*) req
->buf
);
1362 ERROR(dev
, "%s: rndis parse error %d\n", __FUNCTION__
, status
);
1363 spin_unlock(&dev
->lock
);
1369 * The setup() callback implements all the ep0 functionality that's not
1370 * handled lower down. CDC has a number of less-common features:
1372 * - two interfaces: control, and ethernet data
1373 * - Ethernet data interface has two altsettings: default, and active
1374 * - class-specific descriptors for the control interface
1375 * - class-specific control requests
1378 eth_setup (struct usb_gadget
*gadget
, const struct usb_ctrlrequest
*ctrl
)
1380 struct eth_dev
*dev
= get_gadget_data (gadget
);
1381 struct usb_request
*req
= dev
->req
;
1382 int value
= -EOPNOTSUPP
;
1383 u16 wIndex
= le16_to_cpu(ctrl
->wIndex
);
1384 u16 wValue
= le16_to_cpu(ctrl
->wValue
);
1385 u16 wLength
= le16_to_cpu(ctrl
->wLength
);
1387 /* descriptors just go into the pre-allocated ep0 buffer,
1388 * while config change events may enable network traffic.
1390 req
->complete
= eth_setup_complete
;
1391 switch (ctrl
->bRequest
) {
1393 case USB_REQ_GET_DESCRIPTOR
:
1394 if (ctrl
->bRequestType
!= USB_DIR_IN
)
1396 switch (wValue
>> 8) {
1399 value
= min (wLength
, (u16
) sizeof device_desc
);
1400 memcpy (req
->buf
, &device_desc
, value
);
1402 #ifdef CONFIG_USB_GADGET_DUALSPEED
1403 case USB_DT_DEVICE_QUALIFIER
:
1404 if (!gadget
->is_dualspeed
)
1406 value
= min (wLength
, (u16
) sizeof dev_qualifier
);
1407 memcpy (req
->buf
, &dev_qualifier
, value
);
1410 case USB_DT_OTHER_SPEED_CONFIG
:
1411 if (!gadget
->is_dualspeed
)
1414 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1416 value
= config_buf (gadget
->speed
, req
->buf
,
1421 value
= min (wLength
, (u16
) value
);
1425 value
= usb_gadget_get_string (&stringtab
,
1426 wValue
& 0xff, req
->buf
);
1428 value
= min (wLength
, (u16
) value
);
1433 case USB_REQ_SET_CONFIGURATION
:
1434 if (ctrl
->bRequestType
!= 0)
1436 if (gadget
->a_hnp_support
)
1437 DEBUG (dev
, "HNP available\n");
1438 else if (gadget
->a_alt_hnp_support
)
1439 DEBUG (dev
, "HNP needs a different root port\n");
1440 spin_lock (&dev
->lock
);
1441 value
= eth_set_config (dev
, wValue
, GFP_ATOMIC
);
1442 spin_unlock (&dev
->lock
);
1444 case USB_REQ_GET_CONFIGURATION
:
1445 if (ctrl
->bRequestType
!= USB_DIR_IN
)
1447 *(u8
*)req
->buf
= dev
->config
;
1448 value
= min (wLength
, (u16
) 1);
1451 case USB_REQ_SET_INTERFACE
:
1452 if (ctrl
->bRequestType
!= USB_RECIP_INTERFACE
1456 if (!cdc_active(dev
) && wIndex
!= 0)
1458 spin_lock (&dev
->lock
);
1460 /* PXA hardware partially handles SET_INTERFACE;
1461 * we need to kluge around that interference.
1463 if (gadget_is_pxa (gadget
)) {
1464 value
= eth_set_config (dev
, DEV_CONFIG_VALUE
,
1469 #ifdef DEV_CONFIG_CDC
1471 case 0: /* control/master intf */
1475 usb_ep_disable (dev
->status_ep
);
1476 usb_ep_enable (dev
->status_ep
, dev
->status
);
1480 case 1: /* data intf */
1483 usb_ep_disable (dev
->in_ep
);
1484 usb_ep_disable (dev
->out_ep
);
1486 /* CDC requires the data transfers not be done from
1487 * the default interface setting ... also, setting
1488 * the non-default interface resets filters etc.
1491 if (!cdc_active (dev
))
1493 usb_ep_enable (dev
->in_ep
, dev
->in
);
1494 usb_ep_enable (dev
->out_ep
, dev
->out
);
1495 dev
->cdc_filter
= DEFAULT_FILTER
;
1496 netif_carrier_on (dev
->net
);
1498 issue_start_status (dev
);
1499 if (netif_running (dev
->net
)) {
1500 spin_unlock (&dev
->lock
);
1501 eth_start (dev
, GFP_ATOMIC
);
1502 spin_lock (&dev
->lock
);
1505 netif_stop_queue (dev
->net
);
1506 netif_carrier_off (dev
->net
);
1512 /* FIXME this is wrong, as is the assumption that
1513 * all non-PXA hardware talks real CDC ...
1515 dev_warn (&gadget
->dev
, "set_interface ignored!\n");
1516 #endif /* DEV_CONFIG_CDC */
1519 spin_unlock (&dev
->lock
);
1521 case USB_REQ_GET_INTERFACE
:
1522 if (ctrl
->bRequestType
!= (USB_DIR_IN
|USB_RECIP_INTERFACE
)
1526 if (!(cdc_active(dev
) || rndis_active(dev
)) && wIndex
!= 0)
1529 /* for CDC, iff carrier is on, data interface is active. */
1530 if (rndis_active(dev
) || wIndex
!= 1)
1531 *(u8
*)req
->buf
= 0;
1533 *(u8
*)req
->buf
= netif_carrier_ok (dev
->net
) ? 1 : 0;
1534 value
= min (wLength
, (u16
) 1);
1537 #ifdef DEV_CONFIG_CDC
1538 case USB_CDC_SET_ETHERNET_PACKET_FILTER
:
1539 /* see 6.2.30: no data, wIndex = interface,
1540 * wValue = packet filter bitmap
1542 if (ctrl
->bRequestType
!= (USB_TYPE_CLASS
|USB_RECIP_INTERFACE
)
1547 DEBUG (dev
, "packet filter %02x\n", wValue
);
1548 dev
->cdc_filter
= wValue
;
1553 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1554 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1555 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1556 * case USB_CDC_GET_ETHERNET_STATISTIC:
1559 #endif /* DEV_CONFIG_CDC */
1561 #ifdef CONFIG_USB_ETH_RNDIS
1562 /* RNDIS uses the CDC command encapsulation mechanism to implement
1563 * an RPC scheme, with much getting/setting of attributes by OID.
1565 case USB_CDC_SEND_ENCAPSULATED_COMMAND
:
1566 if (ctrl
->bRequestType
!= (USB_TYPE_CLASS
|USB_RECIP_INTERFACE
)
1567 || !rndis_active(dev
)
1568 || wLength
> USB_BUFSIZ
1570 || rndis_control_intf
.bInterfaceNumber
1573 /* read the request, then process it */
1575 req
->complete
= rndis_command_complete
;
1576 /* later, rndis_control_ack () sends a notification */
1579 case USB_CDC_GET_ENCAPSULATED_RESPONSE
:
1580 if ((USB_DIR_IN
|USB_TYPE_CLASS
|USB_RECIP_INTERFACE
)
1581 == ctrl
->bRequestType
1582 && rndis_active(dev
)
1583 // && wLength >= 0x0400
1585 && rndis_control_intf
.bInterfaceNumber
1589 /* return the result */
1590 buf
= rndis_get_next_response (dev
->rndis_config
,
1593 memcpy (req
->buf
, buf
, value
);
1594 req
->complete
= rndis_response_complete
;
1595 rndis_free_response(dev
->rndis_config
, buf
);
1597 /* else stalls ... spec says to avoid that */
1604 "unknown control req%02x.%02x v%04x i%04x l%d\n",
1605 ctrl
->bRequestType
, ctrl
->bRequest
,
1606 wValue
, wIndex
, wLength
);
1609 /* respond with data transfer before status phase? */
1611 req
->length
= value
;
1612 req
->zero
= value
< wLength
1613 && (value
% gadget
->ep0
->maxpacket
) == 0;
1614 value
= usb_ep_queue (gadget
->ep0
, req
, GFP_ATOMIC
);
1616 DEBUG (dev
, "ep_queue --> %d\n", value
);
1618 eth_setup_complete (gadget
->ep0
, req
);
1622 /* host either stalls (value < 0) or reports success */
1627 eth_disconnect (struct usb_gadget
*gadget
)
1629 struct eth_dev
*dev
= get_gadget_data (gadget
);
1630 unsigned long flags
;
1632 spin_lock_irqsave (&dev
->lock
, flags
);
1633 netif_stop_queue (dev
->net
);
1634 netif_carrier_off (dev
->net
);
1635 eth_reset_config (dev
);
1636 spin_unlock_irqrestore (&dev
->lock
, flags
);
1638 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1640 /* next we may get setup() calls to enumerate new connections;
1641 * or an unbind() during shutdown (including removing module).
1645 /*-------------------------------------------------------------------------*/
1647 /* NETWORK DRIVER HOOKUP (to the layer above this driver) */
1649 static int eth_change_mtu (struct net_device
*net
, int new_mtu
)
1651 struct eth_dev
*dev
= netdev_priv(net
);
1656 if (new_mtu
<= ETH_HLEN
|| new_mtu
> ETH_FRAME_LEN
)
1658 /* no zero-length packet read wanted after mtu-sized packets */
1659 if (((new_mtu
+ sizeof (struct ethhdr
)) % dev
->in_ep
->maxpacket
) == 0)
1665 static struct net_device_stats
*eth_get_stats (struct net_device
*net
)
1667 return &((struct eth_dev
*)netdev_priv(net
))->stats
;
1670 static void eth_get_drvinfo(struct net_device
*net
, struct ethtool_drvinfo
*p
)
1672 struct eth_dev
*dev
= netdev_priv(net
);
1673 strlcpy(p
->driver
, shortname
, sizeof p
->driver
);
1674 strlcpy(p
->version
, DRIVER_VERSION
, sizeof p
->version
);
1675 strlcpy(p
->fw_version
, dev
->gadget
->name
, sizeof p
->fw_version
);
1676 strlcpy (p
->bus_info
, dev
->gadget
->dev
.bus_id
, sizeof p
->bus_info
);
1679 static u32
eth_get_link(struct net_device
*net
)
1681 struct eth_dev
*dev
= netdev_priv(net
);
1682 return dev
->gadget
->speed
!= USB_SPEED_UNKNOWN
;
1685 static struct ethtool_ops ops
= {
1686 .get_drvinfo
= eth_get_drvinfo
,
1687 .get_link
= eth_get_link
1690 static void defer_kevent (struct eth_dev
*dev
, int flag
)
1692 if (test_and_set_bit (flag
, &dev
->todo
))
1694 if (!schedule_work (&dev
->work
))
1695 ERROR (dev
, "kevent %d may have been dropped\n", flag
);
1697 DEBUG (dev
, "kevent %d scheduled\n", flag
);
1700 static void rx_complete (struct usb_ep
*ep
, struct usb_request
*req
);
1703 rx_submit (struct eth_dev
*dev
, struct usb_request
*req
, gfp_t gfp_flags
)
1705 struct sk_buff
*skb
;
1706 int retval
= -ENOMEM
;
1709 /* Padding up to RX_EXTRA handles minor disagreements with host.
1710 * Normally we use the USB "terminate on short read" convention;
1711 * so allow up to (N*maxpacket), since that memory is normally
1712 * already allocated. Some hardware doesn't deal well with short
1713 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1714 * byte off the end (to force hardware errors on overflow).
1716 * RNDIS uses internal framing, and explicitly allows senders to
1717 * pad to end-of-packet. That's potentially nice for speed,
1718 * but means receivers can't recover synch on their own.
1720 size
= (sizeof (struct ethhdr
) + dev
->net
->mtu
+ RX_EXTRA
);
1721 size
+= dev
->out_ep
->maxpacket
- 1;
1722 if (rndis_active(dev
))
1723 size
+= sizeof (struct rndis_packet_msg_type
);
1724 size
-= size
% dev
->out_ep
->maxpacket
;
1726 skb
= alloc_skb(size
+ NET_IP_ALIGN
, gfp_flags
);
1728 DEBUG (dev
, "no rx skb\n");
1732 /* Some platforms perform better when IP packets are aligned,
1733 * but on at least one, checksumming fails otherwise. Note:
1734 * RNDIS headers involve variable numbers of LE32 values.
1736 skb_reserve(skb
, NET_IP_ALIGN
);
1738 req
->buf
= skb
->data
;
1740 req
->complete
= rx_complete
;
1743 retval
= usb_ep_queue (dev
->out_ep
, req
, gfp_flags
);
1744 if (retval
== -ENOMEM
)
1746 defer_kevent (dev
, WORK_RX_MEMORY
);
1748 DEBUG (dev
, "rx submit --> %d\n", retval
);
1750 dev_kfree_skb_any(skb
);
1751 spin_lock(&dev
->req_lock
);
1752 list_add (&req
->list
, &dev
->rx_reqs
);
1753 spin_unlock(&dev
->req_lock
);
1758 static void rx_complete (struct usb_ep
*ep
, struct usb_request
*req
)
1760 struct sk_buff
*skb
= req
->context
;
1761 struct eth_dev
*dev
= ep
->driver_data
;
1762 int status
= req
->status
;
1766 /* normal completion */
1768 skb_put (skb
, req
->actual
);
1769 /* we know MaxPacketsPerTransfer == 1 here */
1770 if (rndis_active(dev
))
1771 status
= rndis_rm_hdr (skb
);
1773 || ETH_HLEN
> skb
->len
1774 || skb
->len
> ETH_FRAME_LEN
) {
1775 dev
->stats
.rx_errors
++;
1776 dev
->stats
.rx_length_errors
++;
1777 DEBUG (dev
, "rx length %d\n", skb
->len
);
1781 skb
->protocol
= eth_type_trans (skb
, dev
->net
);
1782 dev
->stats
.rx_packets
++;
1783 dev
->stats
.rx_bytes
+= skb
->len
;
1785 /* no buffer copies needed, unless hardware can't
1788 status
= netif_rx (skb
);
1792 /* software-driven interface shutdown */
1793 case -ECONNRESET
: // unlink
1794 case -ESHUTDOWN
: // disconnect etc
1795 VDEBUG (dev
, "rx shutdown, code %d\n", status
);
1798 /* for hardware automagic (such as pxa) */
1799 case -ECONNABORTED
: // endpoint reset
1800 DEBUG (dev
, "rx %s reset\n", ep
->name
);
1801 defer_kevent (dev
, WORK_RX_MEMORY
);
1803 dev_kfree_skb_any (skb
);
1808 dev
->stats
.rx_over_errors
++;
1812 dev
->stats
.rx_errors
++;
1813 DEBUG (dev
, "rx status %d\n", status
);
1818 dev_kfree_skb_any (skb
);
1819 if (!netif_running (dev
->net
)) {
1821 spin_lock(&dev
->req_lock
);
1822 list_add (&req
->list
, &dev
->rx_reqs
);
1823 spin_unlock(&dev
->req_lock
);
1827 rx_submit (dev
, req
, GFP_ATOMIC
);
1830 static int prealloc (struct list_head
*list
, struct usb_ep
*ep
,
1831 unsigned n
, gfp_t gfp_flags
)
1834 struct usb_request
*req
;
1839 /* queue/recycle up to N requests */
1841 list_for_each_entry (req
, list
, list
) {
1846 req
= usb_ep_alloc_request (ep
, gfp_flags
);
1848 return list_empty (list
) ? -ENOMEM
: 0;
1849 list_add (&req
->list
, list
);
1856 struct list_head
*next
;
1858 next
= req
->list
.next
;
1859 list_del (&req
->list
);
1860 usb_ep_free_request (ep
, req
);
1865 req
= container_of (next
, struct usb_request
, list
);
1870 static int alloc_requests (struct eth_dev
*dev
, unsigned n
, gfp_t gfp_flags
)
1874 spin_lock(&dev
->req_lock
);
1875 status
= prealloc (&dev
->tx_reqs
, dev
->in_ep
, n
, gfp_flags
);
1878 status
= prealloc (&dev
->rx_reqs
, dev
->out_ep
, n
, gfp_flags
);
1883 DEBUG (dev
, "can't alloc requests\n");
1885 spin_unlock(&dev
->req_lock
);
1889 static void rx_fill (struct eth_dev
*dev
, gfp_t gfp_flags
)
1891 struct usb_request
*req
;
1892 unsigned long flags
;
1894 /* fill unused rxq slots with some skb */
1895 spin_lock_irqsave(&dev
->req_lock
, flags
);
1896 while (!list_empty (&dev
->rx_reqs
)) {
1897 req
= container_of (dev
->rx_reqs
.next
,
1898 struct usb_request
, list
);
1899 list_del_init (&req
->list
);
1900 spin_unlock_irqrestore(&dev
->req_lock
, flags
);
1902 if (rx_submit (dev
, req
, gfp_flags
) < 0) {
1903 defer_kevent (dev
, WORK_RX_MEMORY
);
1907 spin_lock_irqsave(&dev
->req_lock
, flags
);
1909 spin_unlock_irqrestore(&dev
->req_lock
, flags
);
1912 static void eth_work (struct work_struct
*work
)
1914 struct eth_dev
*dev
= container_of(work
, struct eth_dev
, work
);
1916 if (test_and_clear_bit (WORK_RX_MEMORY
, &dev
->todo
)) {
1917 if (netif_running (dev
->net
))
1918 rx_fill (dev
, GFP_KERNEL
);
1922 DEBUG (dev
, "work done, flags = 0x%lx\n", dev
->todo
);
1925 static void tx_complete (struct usb_ep
*ep
, struct usb_request
*req
)
1927 struct sk_buff
*skb
= req
->context
;
1928 struct eth_dev
*dev
= ep
->driver_data
;
1930 switch (req
->status
) {
1932 dev
->stats
.tx_errors
++;
1933 VDEBUG (dev
, "tx err %d\n", req
->status
);
1935 case -ECONNRESET
: // unlink
1936 case -ESHUTDOWN
: // disconnect etc
1939 dev
->stats
.tx_bytes
+= skb
->len
;
1941 dev
->stats
.tx_packets
++;
1943 spin_lock(&dev
->req_lock
);
1944 list_add (&req
->list
, &dev
->tx_reqs
);
1945 spin_unlock(&dev
->req_lock
);
1946 dev_kfree_skb_any (skb
);
1948 atomic_dec (&dev
->tx_qlen
);
1949 if (netif_carrier_ok (dev
->net
))
1950 netif_wake_queue (dev
->net
);
1953 static inline int eth_is_promisc (struct eth_dev
*dev
)
1955 /* no filters for the CDC subset; always promisc */
1956 if (subset_active (dev
))
1958 return dev
->cdc_filter
& USB_CDC_PACKET_TYPE_PROMISCUOUS
;
1961 static int eth_start_xmit (struct sk_buff
*skb
, struct net_device
*net
)
1963 struct eth_dev
*dev
= netdev_priv(net
);
1964 int length
= skb
->len
;
1966 struct usb_request
*req
= NULL
;
1967 unsigned long flags
;
1969 /* apply outgoing CDC or RNDIS filters */
1970 if (!eth_is_promisc (dev
)) {
1971 u8
*dest
= skb
->data
;
1973 if (is_multicast_ether_addr(dest
)) {
1976 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1977 * SET_ETHERNET_MULTICAST_FILTERS requests
1979 if (is_broadcast_ether_addr(dest
))
1980 type
= USB_CDC_PACKET_TYPE_BROADCAST
;
1982 type
= USB_CDC_PACKET_TYPE_ALL_MULTICAST
;
1983 if (!(dev
->cdc_filter
& type
)) {
1984 dev_kfree_skb_any (skb
);
1988 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1991 spin_lock_irqsave(&dev
->req_lock
, flags
);
1992 req
= container_of (dev
->tx_reqs
.next
, struct usb_request
, list
);
1993 list_del (&req
->list
);
1994 if (list_empty (&dev
->tx_reqs
))
1995 netif_stop_queue (net
);
1996 spin_unlock_irqrestore(&dev
->req_lock
, flags
);
1998 /* no buffer copies needed, unless the network stack did it
1999 * or the hardware can't use skb buffers.
2000 * or there's not enough space for any RNDIS headers we need
2002 if (rndis_active(dev
)) {
2003 struct sk_buff
*skb_rndis
;
2005 skb_rndis
= skb_realloc_headroom (skb
,
2006 sizeof (struct rndis_packet_msg_type
));
2010 dev_kfree_skb_any (skb
);
2012 rndis_add_hdr (skb
);
2015 req
->buf
= skb
->data
;
2017 req
->complete
= tx_complete
;
2019 /* use zlp framing on tx for strict CDC-Ether conformance,
2020 * though any robust network rx path ignores extra padding.
2021 * and some hardware doesn't like to write zlps.
2024 if (!dev
->zlp
&& (length
% dev
->in_ep
->maxpacket
) == 0)
2027 req
->length
= length
;
2029 #ifdef CONFIG_USB_GADGET_DUALSPEED
2030 /* throttle highspeed IRQ rate back slightly */
2031 req
->no_interrupt
= (dev
->gadget
->speed
== USB_SPEED_HIGH
)
2032 ? ((atomic_read (&dev
->tx_qlen
) % TX_DELAY
) != 0)
2036 retval
= usb_ep_queue (dev
->in_ep
, req
, GFP_ATOMIC
);
2039 DEBUG (dev
, "tx queue err %d\n", retval
);
2042 net
->trans_start
= jiffies
;
2043 atomic_inc (&dev
->tx_qlen
);
2048 dev
->stats
.tx_dropped
++;
2049 dev_kfree_skb_any (skb
);
2050 spin_lock_irqsave(&dev
->req_lock
, flags
);
2051 if (list_empty (&dev
->tx_reqs
))
2052 netif_start_queue (net
);
2053 list_add (&req
->list
, &dev
->tx_reqs
);
2054 spin_unlock_irqrestore(&dev
->req_lock
, flags
);
2059 /*-------------------------------------------------------------------------*/
2061 #ifdef CONFIG_USB_ETH_RNDIS
2063 /* The interrupt endpoint is used in RNDIS to notify the host when messages
2064 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
2065 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
2066 * REMOTE_NDIS_KEEPALIVE_MSG.
2068 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
2069 * normally just one notification will be queued.
2072 static struct usb_request
*eth_req_alloc (struct usb_ep
*, unsigned, gfp_t
);
2073 static void eth_req_free (struct usb_ep
*ep
, struct usb_request
*req
);
2076 rndis_control_ack_complete (struct usb_ep
*ep
, struct usb_request
*req
)
2078 struct eth_dev
*dev
= ep
->driver_data
;
2080 if (req
->status
|| req
->actual
!= req
->length
)
2082 "rndis control ack complete --> %d, %d/%d\n",
2083 req
->status
, req
->actual
, req
->length
);
2084 req
->context
= NULL
;
2086 if (req
!= dev
->stat_req
)
2087 eth_req_free(ep
, req
);
2090 static int rndis_control_ack (struct net_device
*net
)
2092 struct eth_dev
*dev
= netdev_priv(net
);
2094 struct usb_request
*resp
= dev
->stat_req
;
2096 /* in case RNDIS calls this after disconnect */
2098 DEBUG (dev
, "status ENODEV\n");
2102 /* in case queue length > 1 */
2103 if (resp
->context
) {
2104 resp
= eth_req_alloc (dev
->status_ep
, 8, GFP_ATOMIC
);
2109 /* Send RNDIS RESPONSE_AVAILABLE notification;
2110 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
2113 resp
->complete
= rndis_control_ack_complete
;
2114 resp
->context
= dev
;
2116 *((__le32
*) resp
->buf
) = __constant_cpu_to_le32 (1);
2117 *((__le32
*) resp
->buf
+ 1) = __constant_cpu_to_le32 (0);
2119 length
= usb_ep_queue (dev
->status_ep
, resp
, GFP_ATOMIC
);
2122 rndis_control_ack_complete (dev
->status_ep
, resp
);
2130 #define rndis_control_ack NULL
2134 static void eth_start (struct eth_dev
*dev
, gfp_t gfp_flags
)
2136 DEBUG (dev
, "%s\n", __FUNCTION__
);
2138 /* fill the rx queue */
2139 rx_fill (dev
, gfp_flags
);
2141 /* and open the tx floodgates */
2142 atomic_set (&dev
->tx_qlen
, 0);
2143 netif_wake_queue (dev
->net
);
2144 if (rndis_active(dev
)) {
2145 rndis_set_param_medium (dev
->rndis_config
,
2147 BITRATE(dev
->gadget
)/100);
2148 (void) rndis_signal_connect (dev
->rndis_config
);
2152 static int eth_open (struct net_device
*net
)
2154 struct eth_dev
*dev
= netdev_priv(net
);
2156 DEBUG (dev
, "%s\n", __FUNCTION__
);
2157 if (netif_carrier_ok (dev
->net
))
2158 eth_start (dev
, GFP_KERNEL
);
2162 static int eth_stop (struct net_device
*net
)
2164 struct eth_dev
*dev
= netdev_priv(net
);
2166 VDEBUG (dev
, "%s\n", __FUNCTION__
);
2167 netif_stop_queue (net
);
2169 DEBUG (dev
, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
2170 dev
->stats
.rx_packets
, dev
->stats
.tx_packets
,
2171 dev
->stats
.rx_errors
, dev
->stats
.tx_errors
2174 /* ensure there are no more active requests */
2176 usb_ep_disable (dev
->in_ep
);
2177 usb_ep_disable (dev
->out_ep
);
2178 if (netif_carrier_ok (dev
->net
)) {
2179 DEBUG (dev
, "host still using in/out endpoints\n");
2180 // FIXME idiom may leave toggle wrong here
2181 usb_ep_enable (dev
->in_ep
, dev
->in
);
2182 usb_ep_enable (dev
->out_ep
, dev
->out
);
2184 if (dev
->status_ep
) {
2185 usb_ep_disable (dev
->status_ep
);
2186 usb_ep_enable (dev
->status_ep
, dev
->status
);
2190 if (rndis_active(dev
)) {
2191 rndis_set_param_medium (dev
->rndis_config
,
2192 NDIS_MEDIUM_802_3
, 0);
2193 (void) rndis_signal_disconnect (dev
->rndis_config
);
2199 /*-------------------------------------------------------------------------*/
2201 static struct usb_request
*
2202 eth_req_alloc (struct usb_ep
*ep
, unsigned size
, gfp_t gfp_flags
)
2204 struct usb_request
*req
;
2206 req
= usb_ep_alloc_request (ep
, gfp_flags
);
2210 req
->buf
= kmalloc (size
, gfp_flags
);
2212 usb_ep_free_request (ep
, req
);
2219 eth_req_free (struct usb_ep
*ep
, struct usb_request
*req
)
2222 usb_ep_free_request (ep
, req
);
2226 static void /* __init_or_exit */
2227 eth_unbind (struct usb_gadget
*gadget
)
2229 struct eth_dev
*dev
= get_gadget_data (gadget
);
2231 DEBUG (dev
, "unbind\n");
2232 rndis_deregister (dev
->rndis_config
);
2235 /* we've already been disconnected ... no i/o is active */
2237 eth_req_free (gadget
->ep0
, dev
->req
);
2240 if (dev
->stat_req
) {
2241 eth_req_free (dev
->status_ep
, dev
->stat_req
);
2242 dev
->stat_req
= NULL
;
2245 unregister_netdev (dev
->net
);
2246 free_netdev(dev
->net
);
2248 /* assuming we used keventd, it must quiesce too */
2249 flush_scheduled_work ();
2250 set_gadget_data (gadget
, NULL
);
2253 static u8 __devinit
nibble (unsigned char c
)
2255 if (likely (isdigit (c
)))
2258 if (likely (isxdigit (c
)))
2259 return 10 + c
- 'A';
2263 static int __devinit
get_ether_addr(const char *str
, u8
*dev_addr
)
2268 for (i
= 0; i
< 6; i
++) {
2271 if((*str
== '.') || (*str
== ':'))
2273 num
= nibble(*str
++) << 4;
2274 num
|= (nibble(*str
++));
2277 if (is_valid_ether_addr (dev_addr
))
2280 random_ether_addr(dev_addr
);
2284 static int __devinit
2285 eth_bind (struct usb_gadget
*gadget
)
2287 struct eth_dev
*dev
;
2288 struct net_device
*net
;
2289 u8 cdc
= 1, zlp
= 1, rndis
= 1;
2290 struct usb_ep
*in_ep
, *out_ep
, *status_ep
= NULL
;
2291 int status
= -ENOMEM
;
2294 /* these flags are only ever cleared; compiler take note */
2295 #ifndef DEV_CONFIG_CDC
2298 #ifndef CONFIG_USB_ETH_RNDIS
2302 /* Because most host side USB stacks handle CDC Ethernet, that
2303 * standard protocol is _strongly_ preferred for interop purposes.
2304 * (By everyone except Microsoft.)
2306 if (gadget_is_pxa (gadget
)) {
2307 /* pxa doesn't support altsettings */
2309 } else if (gadget_is_musbhdrc(gadget
)) {
2310 /* reduce tx dma overhead by avoiding special cases */
2312 } else if (gadget_is_sh(gadget
)) {
2313 /* sh doesn't support multiple interfaces or configs */
2316 } else if (gadget_is_sa1100 (gadget
)) {
2317 /* hardware can't write zlps */
2319 /* sa1100 CAN do CDC, without status endpoint ... we use
2320 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2325 gcnum
= usb_gadget_controller_number (gadget
);
2327 device_desc
.bcdDevice
= cpu_to_le16 (0x0200 + gcnum
);
2329 /* can't assume CDC works. don't want to default to
2330 * anything less functional on CDC-capable hardware,
2331 * so we fail in this case.
2333 dev_err (&gadget
->dev
,
2334 "controller '%s' not recognized\n",
2338 snprintf (manufacturer
, sizeof manufacturer
, "%s %s/%s",
2339 init_utsname()->sysname
, init_utsname()->release
,
2342 /* If there's an RNDIS configuration, that's what Windows wants to
2343 * be using ... so use these product IDs here and in the "linux.inf"
2344 * needed to install MSFT drivers. Current Linux kernels will use
2345 * the second configuration if it's CDC Ethernet, and need some help
2346 * to choose the right configuration otherwise.
2349 device_desc
.idVendor
=
2350 __constant_cpu_to_le16(RNDIS_VENDOR_NUM
);
2351 device_desc
.idProduct
=
2352 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM
);
2353 snprintf (product_desc
, sizeof product_desc
,
2354 "RNDIS/%s", driver_desc
);
2356 /* CDC subset ... recognized by Linux since 2.4.10, but Windows
2357 * drivers aren't widely available. (That may be improved by
2358 * supporting one submode of the "SAFE" variant of MDLM.)
2361 device_desc
.idVendor
=
2362 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM
);
2363 device_desc
.idProduct
=
2364 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM
);
2367 /* support optional vendor/distro customization */
2370 dev_err (&gadget
->dev
, "idVendor needs idProduct!\n");
2373 device_desc
.idVendor
= cpu_to_le16(idVendor
);
2374 device_desc
.idProduct
= cpu_to_le16(idProduct
);
2376 device_desc
.bcdDevice
= cpu_to_le16(bcdDevice
);
2379 strlcpy (manufacturer
, iManufacturer
, sizeof manufacturer
);
2381 strlcpy (product_desc
, iProduct
, sizeof product_desc
);
2382 if (iSerialNumber
) {
2383 device_desc
.iSerialNumber
= STRING_SERIALNUMBER
,
2384 strlcpy(serial_number
, iSerialNumber
, sizeof serial_number
);
2387 /* all we really need is bulk IN/OUT */
2388 usb_ep_autoconfig_reset (gadget
);
2389 in_ep
= usb_ep_autoconfig (gadget
, &fs_source_desc
);
2392 dev_err (&gadget
->dev
,
2393 "can't autoconfigure on %s\n",
2397 in_ep
->driver_data
= in_ep
; /* claim */
2399 out_ep
= usb_ep_autoconfig (gadget
, &fs_sink_desc
);
2402 out_ep
->driver_data
= out_ep
; /* claim */
2404 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2405 /* CDC Ethernet control interface doesn't require a status endpoint.
2406 * Since some hosts expect one, try to allocate one anyway.
2409 status_ep
= usb_ep_autoconfig (gadget
, &fs_status_desc
);
2411 status_ep
->driver_data
= status_ep
; /* claim */
2413 dev_err (&gadget
->dev
,
2414 "can't run RNDIS on %s\n",
2417 #ifdef DEV_CONFIG_CDC
2418 /* pxa25x only does CDC subset; often used with RNDIS */
2420 control_intf
.bNumEndpoints
= 0;
2421 /* FIXME remove endpoint from descriptor list */
2427 /* one config: cdc, else minimal subset */
2429 eth_config
.bNumInterfaces
= 1;
2430 eth_config
.iConfiguration
= STRING_SUBSET
;
2432 /* use functions to set these up, in case we're built to work
2433 * with multiple controllers and must override CDC Ethernet.
2435 fs_subset_descriptors();
2436 hs_subset_descriptors();
2439 device_desc
.bMaxPacketSize0
= gadget
->ep0
->maxpacket
;
2440 usb_gadget_set_selfpowered (gadget
);
2442 /* For now RNDIS is always a second config */
2444 device_desc
.bNumConfigurations
= 2;
2446 #ifdef CONFIG_USB_GADGET_DUALSPEED
2448 dev_qualifier
.bNumConfigurations
= 2;
2450 dev_qualifier
.bDeviceClass
= USB_CLASS_VENDOR_SPEC
;
2452 /* assumes ep0 uses the same value for both speeds ... */
2453 dev_qualifier
.bMaxPacketSize0
= device_desc
.bMaxPacketSize0
;
2455 /* and that all endpoints are dual-speed */
2456 hs_source_desc
.bEndpointAddress
= fs_source_desc
.bEndpointAddress
;
2457 hs_sink_desc
.bEndpointAddress
= fs_sink_desc
.bEndpointAddress
;
2458 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2460 hs_status_desc
.bEndpointAddress
=
2461 fs_status_desc
.bEndpointAddress
;
2463 #endif /* DUALSPEED */
2465 if (gadget
->is_otg
) {
2466 otg_descriptor
.bmAttributes
|= USB_OTG_HNP
,
2467 eth_config
.bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
2468 eth_config
.bMaxPower
= 4;
2469 #ifdef CONFIG_USB_ETH_RNDIS
2470 rndis_config
.bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
2471 rndis_config
.bMaxPower
= 4;
2475 net
= alloc_etherdev (sizeof *dev
);
2478 dev
= netdev_priv(net
);
2479 spin_lock_init (&dev
->lock
);
2480 spin_lock_init (&dev
->req_lock
);
2481 INIT_WORK (&dev
->work
, eth_work
);
2482 INIT_LIST_HEAD (&dev
->tx_reqs
);
2483 INIT_LIST_HEAD (&dev
->rx_reqs
);
2485 /* network device setup */
2487 SET_MODULE_OWNER (net
);
2488 strcpy (net
->name
, "usb%d");
2493 dev
->out_ep
= out_ep
;
2494 dev
->status_ep
= status_ep
;
2496 /* Module params for these addresses should come from ID proms.
2497 * The host side address is used with CDC and RNDIS, and commonly
2498 * ends up in a persistent config database. It's not clear if
2499 * host side code for the SAFE thing cares -- its original BLAN
2500 * thing didn't, Sharp never assigned those addresses on Zaurii.
2502 if (get_ether_addr(dev_addr
, net
->dev_addr
))
2503 dev_warn(&gadget
->dev
,
2504 "using random %s ethernet address\n", "self");
2505 if (get_ether_addr(host_addr
, dev
->host_mac
))
2506 dev_warn(&gadget
->dev
,
2507 "using random %s ethernet address\n", "host");
2508 snprintf (ethaddr
, sizeof ethaddr
, "%02X%02X%02X%02X%02X%02X",
2509 dev
->host_mac
[0], dev
->host_mac
[1],
2510 dev
->host_mac
[2], dev
->host_mac
[3],
2511 dev
->host_mac
[4], dev
->host_mac
[5]);
2514 status
= rndis_init();
2516 dev_err (&gadget
->dev
, "can't init RNDIS, %d\n",
2522 net
->change_mtu
= eth_change_mtu
;
2523 net
->get_stats
= eth_get_stats
;
2524 net
->hard_start_xmit
= eth_start_xmit
;
2525 net
->open
= eth_open
;
2526 net
->stop
= eth_stop
;
2527 // watchdog_timeo, tx_timeout ...
2528 // set_multicast_list
2529 SET_ETHTOOL_OPS(net
, &ops
);
2531 /* preallocate control message data and buffer */
2532 dev
->req
= eth_req_alloc (gadget
->ep0
, USB_BUFSIZ
, GFP_KERNEL
);
2535 dev
->req
->complete
= eth_setup_complete
;
2537 /* ... and maybe likewise for status transfer */
2538 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2539 if (dev
->status_ep
) {
2540 dev
->stat_req
= eth_req_alloc (dev
->status_ep
,
2541 STATUS_BYTECOUNT
, GFP_KERNEL
);
2542 if (!dev
->stat_req
) {
2543 eth_req_free (gadget
->ep0
, dev
->req
);
2546 dev
->stat_req
->context
= NULL
;
2550 /* finish hookup to lower layer ... */
2551 dev
->gadget
= gadget
;
2552 set_gadget_data (gadget
, dev
);
2553 gadget
->ep0
->driver_data
= dev
;
2555 /* two kinds of host-initiated state changes:
2556 * - iff DATA transfer is active, carrier is "on"
2557 * - tx queueing enabled if open *and* carrier is "on"
2559 netif_stop_queue (dev
->net
);
2560 netif_carrier_off (dev
->net
);
2562 SET_NETDEV_DEV (dev
->net
, &gadget
->dev
);
2563 status
= register_netdev (dev
->net
);
2567 INFO (dev
, "%s, version: " DRIVER_VERSION
"\n", driver_desc
);
2568 INFO (dev
, "using %s, OUT %s IN %s%s%s\n", gadget
->name
,
2569 out_ep
->name
, in_ep
->name
,
2570 status_ep
? " STATUS " : "",
2571 status_ep
? status_ep
->name
: ""
2573 INFO (dev
, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2574 net
->dev_addr
[0], net
->dev_addr
[1],
2575 net
->dev_addr
[2], net
->dev_addr
[3],
2576 net
->dev_addr
[4], net
->dev_addr
[5]);
2579 INFO (dev
, "HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2580 dev
->host_mac
[0], dev
->host_mac
[1],
2581 dev
->host_mac
[2], dev
->host_mac
[3],
2582 dev
->host_mac
[4], dev
->host_mac
[5]);
2587 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2589 dev
->rndis_config
= rndis_register (rndis_control_ack
);
2590 if (dev
->rndis_config
< 0) {
2592 unregister_netdev (dev
->net
);
2597 /* these set up a lot of the OIDs that RNDIS needs */
2598 rndis_set_host_mac (dev
->rndis_config
, dev
->host_mac
);
2599 if (rndis_set_param_dev (dev
->rndis_config
, dev
->net
,
2600 &dev
->stats
, &dev
->cdc_filter
))
2602 if (rndis_set_param_vendor (dev
->rndis_config
, vendorID
,
2605 if (rndis_set_param_medium (dev
->rndis_config
,
2609 INFO (dev
, "RNDIS ready\n");
2615 dev_dbg(&gadget
->dev
, "register_netdev failed, %d\n", status
);
2617 eth_unbind (gadget
);
2621 /*-------------------------------------------------------------------------*/
2624 eth_suspend (struct usb_gadget
*gadget
)
2626 struct eth_dev
*dev
= get_gadget_data (gadget
);
2628 DEBUG (dev
, "suspend\n");
2633 eth_resume (struct usb_gadget
*gadget
)
2635 struct eth_dev
*dev
= get_gadget_data (gadget
);
2637 DEBUG (dev
, "resume\n");
2641 /*-------------------------------------------------------------------------*/
2643 static struct usb_gadget_driver eth_driver
= {
2646 .function
= (char *) driver_desc
,
2648 .unbind
= eth_unbind
,
2651 .disconnect
= eth_disconnect
,
2653 .suspend
= eth_suspend
,
2654 .resume
= eth_resume
,
2657 .name
= (char *) shortname
,
2658 .owner
= THIS_MODULE
,
2662 MODULE_DESCRIPTION (DRIVER_DESC
);
2663 MODULE_AUTHOR ("David Brownell, Benedikt Spanger");
2664 MODULE_LICENSE ("GPL");
2667 static int __init
init (void)
2669 return usb_gadget_register_driver (ð_driver
);
2673 static void __exit
cleanup (void)
2675 usb_gadget_unregister_driver (ð_driver
);
2677 module_exit (cleanup
);