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 if ((skb
= alloc_skb (size
+ NET_IP_ALIGN
, gfp_flags
)) == 0) {
1727 DEBUG (dev
, "no rx skb\n");
1731 /* Some platforms perform better when IP packets are aligned,
1732 * but on at least one, checksumming fails otherwise. Note:
1733 * RNDIS headers involve variable numbers of LE32 values.
1735 skb_reserve(skb
, NET_IP_ALIGN
);
1737 req
->buf
= skb
->data
;
1739 req
->complete
= rx_complete
;
1742 retval
= usb_ep_queue (dev
->out_ep
, req
, gfp_flags
);
1743 if (retval
== -ENOMEM
)
1745 defer_kevent (dev
, WORK_RX_MEMORY
);
1747 DEBUG (dev
, "rx submit --> %d\n", retval
);
1749 dev_kfree_skb_any(skb
);
1750 spin_lock(&dev
->req_lock
);
1751 list_add (&req
->list
, &dev
->rx_reqs
);
1752 spin_unlock(&dev
->req_lock
);
1757 static void rx_complete (struct usb_ep
*ep
, struct usb_request
*req
)
1759 struct sk_buff
*skb
= req
->context
;
1760 struct eth_dev
*dev
= ep
->driver_data
;
1761 int status
= req
->status
;
1765 /* normal completion */
1767 skb_put (skb
, req
->actual
);
1768 /* we know MaxPacketsPerTransfer == 1 here */
1769 if (rndis_active(dev
))
1770 status
= rndis_rm_hdr (skb
);
1772 || ETH_HLEN
> skb
->len
1773 || skb
->len
> ETH_FRAME_LEN
) {
1774 dev
->stats
.rx_errors
++;
1775 dev
->stats
.rx_length_errors
++;
1776 DEBUG (dev
, "rx length %d\n", skb
->len
);
1780 skb
->protocol
= eth_type_trans (skb
, dev
->net
);
1781 dev
->stats
.rx_packets
++;
1782 dev
->stats
.rx_bytes
+= skb
->len
;
1784 /* no buffer copies needed, unless hardware can't
1787 status
= netif_rx (skb
);
1791 /* software-driven interface shutdown */
1792 case -ECONNRESET
: // unlink
1793 case -ESHUTDOWN
: // disconnect etc
1794 VDEBUG (dev
, "rx shutdown, code %d\n", status
);
1797 /* for hardware automagic (such as pxa) */
1798 case -ECONNABORTED
: // endpoint reset
1799 DEBUG (dev
, "rx %s reset\n", ep
->name
);
1800 defer_kevent (dev
, WORK_RX_MEMORY
);
1802 dev_kfree_skb_any (skb
);
1807 dev
->stats
.rx_over_errors
++;
1811 dev
->stats
.rx_errors
++;
1812 DEBUG (dev
, "rx status %d\n", status
);
1817 dev_kfree_skb_any (skb
);
1818 if (!netif_running (dev
->net
)) {
1820 spin_lock(&dev
->req_lock
);
1821 list_add (&req
->list
, &dev
->rx_reqs
);
1822 spin_unlock(&dev
->req_lock
);
1826 rx_submit (dev
, req
, GFP_ATOMIC
);
1829 static int prealloc (struct list_head
*list
, struct usb_ep
*ep
,
1830 unsigned n
, gfp_t gfp_flags
)
1833 struct usb_request
*req
;
1838 /* queue/recycle up to N requests */
1840 list_for_each_entry (req
, list
, list
) {
1845 req
= usb_ep_alloc_request (ep
, gfp_flags
);
1847 return list_empty (list
) ? -ENOMEM
: 0;
1848 list_add (&req
->list
, list
);
1855 struct list_head
*next
;
1857 next
= req
->list
.next
;
1858 list_del (&req
->list
);
1859 usb_ep_free_request (ep
, req
);
1864 req
= container_of (next
, struct usb_request
, list
);
1869 static int alloc_requests (struct eth_dev
*dev
, unsigned n
, gfp_t gfp_flags
)
1873 spin_lock(&dev
->req_lock
);
1874 status
= prealloc (&dev
->tx_reqs
, dev
->in_ep
, n
, gfp_flags
);
1877 status
= prealloc (&dev
->rx_reqs
, dev
->out_ep
, n
, gfp_flags
);
1882 DEBUG (dev
, "can't alloc requests\n");
1884 spin_unlock(&dev
->req_lock
);
1888 static void rx_fill (struct eth_dev
*dev
, gfp_t gfp_flags
)
1890 struct usb_request
*req
;
1891 unsigned long flags
;
1893 /* fill unused rxq slots with some skb */
1894 spin_lock_irqsave(&dev
->req_lock
, flags
);
1895 while (!list_empty (&dev
->rx_reqs
)) {
1896 req
= container_of (dev
->rx_reqs
.next
,
1897 struct usb_request
, list
);
1898 list_del_init (&req
->list
);
1899 spin_unlock_irqrestore(&dev
->req_lock
, flags
);
1901 if (rx_submit (dev
, req
, gfp_flags
) < 0) {
1902 defer_kevent (dev
, WORK_RX_MEMORY
);
1906 spin_lock_irqsave(&dev
->req_lock
, flags
);
1908 spin_unlock_irqrestore(&dev
->req_lock
, flags
);
1911 static void eth_work (struct work_struct
*work
)
1913 struct eth_dev
*dev
= container_of(work
, struct eth_dev
, work
);
1915 if (test_and_clear_bit (WORK_RX_MEMORY
, &dev
->todo
)) {
1916 if (netif_running (dev
->net
))
1917 rx_fill (dev
, GFP_KERNEL
);
1921 DEBUG (dev
, "work done, flags = 0x%lx\n", dev
->todo
);
1924 static void tx_complete (struct usb_ep
*ep
, struct usb_request
*req
)
1926 struct sk_buff
*skb
= req
->context
;
1927 struct eth_dev
*dev
= ep
->driver_data
;
1929 switch (req
->status
) {
1931 dev
->stats
.tx_errors
++;
1932 VDEBUG (dev
, "tx err %d\n", req
->status
);
1934 case -ECONNRESET
: // unlink
1935 case -ESHUTDOWN
: // disconnect etc
1938 dev
->stats
.tx_bytes
+= skb
->len
;
1940 dev
->stats
.tx_packets
++;
1942 spin_lock(&dev
->req_lock
);
1943 list_add (&req
->list
, &dev
->tx_reqs
);
1944 spin_unlock(&dev
->req_lock
);
1945 dev_kfree_skb_any (skb
);
1947 atomic_dec (&dev
->tx_qlen
);
1948 if (netif_carrier_ok (dev
->net
))
1949 netif_wake_queue (dev
->net
);
1952 static inline int eth_is_promisc (struct eth_dev
*dev
)
1954 /* no filters for the CDC subset; always promisc */
1955 if (subset_active (dev
))
1957 return dev
->cdc_filter
& USB_CDC_PACKET_TYPE_PROMISCUOUS
;
1960 static int eth_start_xmit (struct sk_buff
*skb
, struct net_device
*net
)
1962 struct eth_dev
*dev
= netdev_priv(net
);
1963 int length
= skb
->len
;
1965 struct usb_request
*req
= NULL
;
1966 unsigned long flags
;
1968 /* apply outgoing CDC or RNDIS filters */
1969 if (!eth_is_promisc (dev
)) {
1970 u8
*dest
= skb
->data
;
1972 if (is_multicast_ether_addr(dest
)) {
1975 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1976 * SET_ETHERNET_MULTICAST_FILTERS requests
1978 if (is_broadcast_ether_addr(dest
))
1979 type
= USB_CDC_PACKET_TYPE_BROADCAST
;
1981 type
= USB_CDC_PACKET_TYPE_ALL_MULTICAST
;
1982 if (!(dev
->cdc_filter
& type
)) {
1983 dev_kfree_skb_any (skb
);
1987 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1990 spin_lock_irqsave(&dev
->req_lock
, flags
);
1991 req
= container_of (dev
->tx_reqs
.next
, struct usb_request
, list
);
1992 list_del (&req
->list
);
1993 if (list_empty (&dev
->tx_reqs
))
1994 netif_stop_queue (net
);
1995 spin_unlock_irqrestore(&dev
->req_lock
, flags
);
1997 /* no buffer copies needed, unless the network stack did it
1998 * or the hardware can't use skb buffers.
1999 * or there's not enough space for any RNDIS headers we need
2001 if (rndis_active(dev
)) {
2002 struct sk_buff
*skb_rndis
;
2004 skb_rndis
= skb_realloc_headroom (skb
,
2005 sizeof (struct rndis_packet_msg_type
));
2009 dev_kfree_skb_any (skb
);
2011 rndis_add_hdr (skb
);
2014 req
->buf
= skb
->data
;
2016 req
->complete
= tx_complete
;
2018 /* use zlp framing on tx for strict CDC-Ether conformance,
2019 * though any robust network rx path ignores extra padding.
2020 * and some hardware doesn't like to write zlps.
2023 if (!dev
->zlp
&& (length
% dev
->in_ep
->maxpacket
) == 0)
2026 req
->length
= length
;
2028 #ifdef CONFIG_USB_GADGET_DUALSPEED
2029 /* throttle highspeed IRQ rate back slightly */
2030 req
->no_interrupt
= (dev
->gadget
->speed
== USB_SPEED_HIGH
)
2031 ? ((atomic_read (&dev
->tx_qlen
) % TX_DELAY
) != 0)
2035 retval
= usb_ep_queue (dev
->in_ep
, req
, GFP_ATOMIC
);
2038 DEBUG (dev
, "tx queue err %d\n", retval
);
2041 net
->trans_start
= jiffies
;
2042 atomic_inc (&dev
->tx_qlen
);
2047 dev
->stats
.tx_dropped
++;
2048 dev_kfree_skb_any (skb
);
2049 spin_lock_irqsave(&dev
->req_lock
, flags
);
2050 if (list_empty (&dev
->tx_reqs
))
2051 netif_start_queue (net
);
2052 list_add (&req
->list
, &dev
->tx_reqs
);
2053 spin_unlock_irqrestore(&dev
->req_lock
, flags
);
2058 /*-------------------------------------------------------------------------*/
2060 #ifdef CONFIG_USB_ETH_RNDIS
2062 /* The interrupt endpoint is used in RNDIS to notify the host when messages
2063 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
2064 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
2065 * REMOTE_NDIS_KEEPALIVE_MSG.
2067 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
2068 * normally just one notification will be queued.
2071 static struct usb_request
*eth_req_alloc (struct usb_ep
*, unsigned, gfp_t
);
2072 static void eth_req_free (struct usb_ep
*ep
, struct usb_request
*req
);
2075 rndis_control_ack_complete (struct usb_ep
*ep
, struct usb_request
*req
)
2077 struct eth_dev
*dev
= ep
->driver_data
;
2079 if (req
->status
|| req
->actual
!= req
->length
)
2081 "rndis control ack complete --> %d, %d/%d\n",
2082 req
->status
, req
->actual
, req
->length
);
2083 req
->context
= NULL
;
2085 if (req
!= dev
->stat_req
)
2086 eth_req_free(ep
, req
);
2089 static int rndis_control_ack (struct net_device
*net
)
2091 struct eth_dev
*dev
= netdev_priv(net
);
2093 struct usb_request
*resp
= dev
->stat_req
;
2095 /* in case RNDIS calls this after disconnect */
2097 DEBUG (dev
, "status ENODEV\n");
2101 /* in case queue length > 1 */
2102 if (resp
->context
) {
2103 resp
= eth_req_alloc (dev
->status_ep
, 8, GFP_ATOMIC
);
2108 /* Send RNDIS RESPONSE_AVAILABLE notification;
2109 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
2112 resp
->complete
= rndis_control_ack_complete
;
2113 resp
->context
= dev
;
2115 *((__le32
*) resp
->buf
) = __constant_cpu_to_le32 (1);
2116 *((__le32
*) resp
->buf
+ 1) = __constant_cpu_to_le32 (0);
2118 length
= usb_ep_queue (dev
->status_ep
, resp
, GFP_ATOMIC
);
2121 rndis_control_ack_complete (dev
->status_ep
, resp
);
2129 #define rndis_control_ack NULL
2133 static void eth_start (struct eth_dev
*dev
, gfp_t gfp_flags
)
2135 DEBUG (dev
, "%s\n", __FUNCTION__
);
2137 /* fill the rx queue */
2138 rx_fill (dev
, gfp_flags
);
2140 /* and open the tx floodgates */
2141 atomic_set (&dev
->tx_qlen
, 0);
2142 netif_wake_queue (dev
->net
);
2143 if (rndis_active(dev
)) {
2144 rndis_set_param_medium (dev
->rndis_config
,
2146 BITRATE(dev
->gadget
)/100);
2147 (void) rndis_signal_connect (dev
->rndis_config
);
2151 static int eth_open (struct net_device
*net
)
2153 struct eth_dev
*dev
= netdev_priv(net
);
2155 DEBUG (dev
, "%s\n", __FUNCTION__
);
2156 if (netif_carrier_ok (dev
->net
))
2157 eth_start (dev
, GFP_KERNEL
);
2161 static int eth_stop (struct net_device
*net
)
2163 struct eth_dev
*dev
= netdev_priv(net
);
2165 VDEBUG (dev
, "%s\n", __FUNCTION__
);
2166 netif_stop_queue (net
);
2168 DEBUG (dev
, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
2169 dev
->stats
.rx_packets
, dev
->stats
.tx_packets
,
2170 dev
->stats
.rx_errors
, dev
->stats
.tx_errors
2173 /* ensure there are no more active requests */
2175 usb_ep_disable (dev
->in_ep
);
2176 usb_ep_disable (dev
->out_ep
);
2177 if (netif_carrier_ok (dev
->net
)) {
2178 DEBUG (dev
, "host still using in/out endpoints\n");
2179 // FIXME idiom may leave toggle wrong here
2180 usb_ep_enable (dev
->in_ep
, dev
->in
);
2181 usb_ep_enable (dev
->out_ep
, dev
->out
);
2183 if (dev
->status_ep
) {
2184 usb_ep_disable (dev
->status_ep
);
2185 usb_ep_enable (dev
->status_ep
, dev
->status
);
2189 if (rndis_active(dev
)) {
2190 rndis_set_param_medium (dev
->rndis_config
,
2191 NDIS_MEDIUM_802_3
, 0);
2192 (void) rndis_signal_disconnect (dev
->rndis_config
);
2198 /*-------------------------------------------------------------------------*/
2200 static struct usb_request
*
2201 eth_req_alloc (struct usb_ep
*ep
, unsigned size
, gfp_t gfp_flags
)
2203 struct usb_request
*req
;
2205 req
= usb_ep_alloc_request (ep
, gfp_flags
);
2209 req
->buf
= kmalloc (size
, gfp_flags
);
2211 usb_ep_free_request (ep
, req
);
2218 eth_req_free (struct usb_ep
*ep
, struct usb_request
*req
)
2221 usb_ep_free_request (ep
, req
);
2225 static void /* __init_or_exit */
2226 eth_unbind (struct usb_gadget
*gadget
)
2228 struct eth_dev
*dev
= get_gadget_data (gadget
);
2230 DEBUG (dev
, "unbind\n");
2231 rndis_deregister (dev
->rndis_config
);
2234 /* we've already been disconnected ... no i/o is active */
2236 eth_req_free (gadget
->ep0
, dev
->req
);
2239 if (dev
->stat_req
) {
2240 eth_req_free (dev
->status_ep
, dev
->stat_req
);
2241 dev
->stat_req
= NULL
;
2244 unregister_netdev (dev
->net
);
2245 free_netdev(dev
->net
);
2247 /* assuming we used keventd, it must quiesce too */
2248 flush_scheduled_work ();
2249 set_gadget_data (gadget
, NULL
);
2252 static u8 __devinit
nibble (unsigned char c
)
2254 if (likely (isdigit (c
)))
2257 if (likely (isxdigit (c
)))
2258 return 10 + c
- 'A';
2262 static int __devinit
get_ether_addr(const char *str
, u8
*dev_addr
)
2267 for (i
= 0; i
< 6; i
++) {
2270 if((*str
== '.') || (*str
== ':'))
2272 num
= nibble(*str
++) << 4;
2273 num
|= (nibble(*str
++));
2276 if (is_valid_ether_addr (dev_addr
))
2279 random_ether_addr(dev_addr
);
2283 static int __devinit
2284 eth_bind (struct usb_gadget
*gadget
)
2286 struct eth_dev
*dev
;
2287 struct net_device
*net
;
2288 u8 cdc
= 1, zlp
= 1, rndis
= 1;
2289 struct usb_ep
*in_ep
, *out_ep
, *status_ep
= NULL
;
2290 int status
= -ENOMEM
;
2293 /* these flags are only ever cleared; compiler take note */
2294 #ifndef DEV_CONFIG_CDC
2297 #ifndef CONFIG_USB_ETH_RNDIS
2301 /* Because most host side USB stacks handle CDC Ethernet, that
2302 * standard protocol is _strongly_ preferred for interop purposes.
2303 * (By everyone except Microsoft.)
2305 if (gadget_is_pxa (gadget
)) {
2306 /* pxa doesn't support altsettings */
2308 } else if (gadget_is_musbhdrc(gadget
)) {
2309 /* reduce tx dma overhead by avoiding special cases */
2311 } else if (gadget_is_sh(gadget
)) {
2312 /* sh doesn't support multiple interfaces or configs */
2315 } else if (gadget_is_sa1100 (gadget
)) {
2316 /* hardware can't write zlps */
2318 /* sa1100 CAN do CDC, without status endpoint ... we use
2319 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2324 gcnum
= usb_gadget_controller_number (gadget
);
2326 device_desc
.bcdDevice
= cpu_to_le16 (0x0200 + gcnum
);
2328 /* can't assume CDC works. don't want to default to
2329 * anything less functional on CDC-capable hardware,
2330 * so we fail in this case.
2332 dev_err (&gadget
->dev
,
2333 "controller '%s' not recognized\n",
2337 snprintf (manufacturer
, sizeof manufacturer
, "%s %s/%s",
2338 init_utsname()->sysname
, init_utsname()->release
,
2341 /* If there's an RNDIS configuration, that's what Windows wants to
2342 * be using ... so use these product IDs here and in the "linux.inf"
2343 * needed to install MSFT drivers. Current Linux kernels will use
2344 * the second configuration if it's CDC Ethernet, and need some help
2345 * to choose the right configuration otherwise.
2348 device_desc
.idVendor
=
2349 __constant_cpu_to_le16(RNDIS_VENDOR_NUM
);
2350 device_desc
.idProduct
=
2351 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM
);
2352 snprintf (product_desc
, sizeof product_desc
,
2353 "RNDIS/%s", driver_desc
);
2355 /* CDC subset ... recognized by Linux since 2.4.10, but Windows
2356 * drivers aren't widely available. (That may be improved by
2357 * supporting one submode of the "SAFE" variant of MDLM.)
2360 device_desc
.idVendor
=
2361 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM
);
2362 device_desc
.idProduct
=
2363 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM
);
2366 /* support optional vendor/distro customization */
2369 dev_err (&gadget
->dev
, "idVendor needs idProduct!\n");
2372 device_desc
.idVendor
= cpu_to_le16(idVendor
);
2373 device_desc
.idProduct
= cpu_to_le16(idProduct
);
2375 device_desc
.bcdDevice
= cpu_to_le16(bcdDevice
);
2378 strlcpy (manufacturer
, iManufacturer
, sizeof manufacturer
);
2380 strlcpy (product_desc
, iProduct
, sizeof product_desc
);
2381 if (iSerialNumber
) {
2382 device_desc
.iSerialNumber
= STRING_SERIALNUMBER
,
2383 strlcpy(serial_number
, iSerialNumber
, sizeof serial_number
);
2386 /* all we really need is bulk IN/OUT */
2387 usb_ep_autoconfig_reset (gadget
);
2388 in_ep
= usb_ep_autoconfig (gadget
, &fs_source_desc
);
2391 dev_err (&gadget
->dev
,
2392 "can't autoconfigure on %s\n",
2396 in_ep
->driver_data
= in_ep
; /* claim */
2398 out_ep
= usb_ep_autoconfig (gadget
, &fs_sink_desc
);
2401 out_ep
->driver_data
= out_ep
; /* claim */
2403 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2404 /* CDC Ethernet control interface doesn't require a status endpoint.
2405 * Since some hosts expect one, try to allocate one anyway.
2408 status_ep
= usb_ep_autoconfig (gadget
, &fs_status_desc
);
2410 status_ep
->driver_data
= status_ep
; /* claim */
2412 dev_err (&gadget
->dev
,
2413 "can't run RNDIS on %s\n",
2416 #ifdef DEV_CONFIG_CDC
2417 /* pxa25x only does CDC subset; often used with RNDIS */
2419 control_intf
.bNumEndpoints
= 0;
2420 /* FIXME remove endpoint from descriptor list */
2426 /* one config: cdc, else minimal subset */
2428 eth_config
.bNumInterfaces
= 1;
2429 eth_config
.iConfiguration
= STRING_SUBSET
;
2431 /* use functions to set these up, in case we're built to work
2432 * with multiple controllers and must override CDC Ethernet.
2434 fs_subset_descriptors();
2435 hs_subset_descriptors();
2438 device_desc
.bMaxPacketSize0
= gadget
->ep0
->maxpacket
;
2439 usb_gadget_set_selfpowered (gadget
);
2441 /* For now RNDIS is always a second config */
2443 device_desc
.bNumConfigurations
= 2;
2445 #ifdef CONFIG_USB_GADGET_DUALSPEED
2447 dev_qualifier
.bNumConfigurations
= 2;
2449 dev_qualifier
.bDeviceClass
= USB_CLASS_VENDOR_SPEC
;
2451 /* assumes ep0 uses the same value for both speeds ... */
2452 dev_qualifier
.bMaxPacketSize0
= device_desc
.bMaxPacketSize0
;
2454 /* and that all endpoints are dual-speed */
2455 hs_source_desc
.bEndpointAddress
= fs_source_desc
.bEndpointAddress
;
2456 hs_sink_desc
.bEndpointAddress
= fs_sink_desc
.bEndpointAddress
;
2457 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2459 hs_status_desc
.bEndpointAddress
=
2460 fs_status_desc
.bEndpointAddress
;
2462 #endif /* DUALSPEED */
2464 if (gadget
->is_otg
) {
2465 otg_descriptor
.bmAttributes
|= USB_OTG_HNP
,
2466 eth_config
.bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
2467 eth_config
.bMaxPower
= 4;
2468 #ifdef CONFIG_USB_ETH_RNDIS
2469 rndis_config
.bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
2470 rndis_config
.bMaxPower
= 4;
2474 net
= alloc_etherdev (sizeof *dev
);
2477 dev
= netdev_priv(net
);
2478 spin_lock_init (&dev
->lock
);
2479 spin_lock_init (&dev
->req_lock
);
2480 INIT_WORK (&dev
->work
, eth_work
);
2481 INIT_LIST_HEAD (&dev
->tx_reqs
);
2482 INIT_LIST_HEAD (&dev
->rx_reqs
);
2484 /* network device setup */
2486 SET_MODULE_OWNER (net
);
2487 strcpy (net
->name
, "usb%d");
2492 dev
->out_ep
= out_ep
;
2493 dev
->status_ep
= status_ep
;
2495 /* Module params for these addresses should come from ID proms.
2496 * The host side address is used with CDC and RNDIS, and commonly
2497 * ends up in a persistent config database. It's not clear if
2498 * host side code for the SAFE thing cares -- its original BLAN
2499 * thing didn't, Sharp never assigned those addresses on Zaurii.
2501 if (get_ether_addr(dev_addr
, net
->dev_addr
))
2502 dev_warn(&gadget
->dev
,
2503 "using random %s ethernet address\n", "self");
2504 if (get_ether_addr(host_addr
, dev
->host_mac
))
2505 dev_warn(&gadget
->dev
,
2506 "using random %s ethernet address\n", "host");
2507 snprintf (ethaddr
, sizeof ethaddr
, "%02X%02X%02X%02X%02X%02X",
2508 dev
->host_mac
[0], dev
->host_mac
[1],
2509 dev
->host_mac
[2], dev
->host_mac
[3],
2510 dev
->host_mac
[4], dev
->host_mac
[5]);
2513 status
= rndis_init();
2515 dev_err (&gadget
->dev
, "can't init RNDIS, %d\n",
2521 net
->change_mtu
= eth_change_mtu
;
2522 net
->get_stats
= eth_get_stats
;
2523 net
->hard_start_xmit
= eth_start_xmit
;
2524 net
->open
= eth_open
;
2525 net
->stop
= eth_stop
;
2526 // watchdog_timeo, tx_timeout ...
2527 // set_multicast_list
2528 SET_ETHTOOL_OPS(net
, &ops
);
2530 /* preallocate control message data and buffer */
2531 dev
->req
= eth_req_alloc (gadget
->ep0
, USB_BUFSIZ
, GFP_KERNEL
);
2534 dev
->req
->complete
= eth_setup_complete
;
2536 /* ... and maybe likewise for status transfer */
2537 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2538 if (dev
->status_ep
) {
2539 dev
->stat_req
= eth_req_alloc (dev
->status_ep
,
2540 STATUS_BYTECOUNT
, GFP_KERNEL
);
2541 if (!dev
->stat_req
) {
2542 eth_req_free (gadget
->ep0
, dev
->req
);
2545 dev
->stat_req
->context
= NULL
;
2549 /* finish hookup to lower layer ... */
2550 dev
->gadget
= gadget
;
2551 set_gadget_data (gadget
, dev
);
2552 gadget
->ep0
->driver_data
= dev
;
2554 /* two kinds of host-initiated state changes:
2555 * - iff DATA transfer is active, carrier is "on"
2556 * - tx queueing enabled if open *and* carrier is "on"
2558 netif_stop_queue (dev
->net
);
2559 netif_carrier_off (dev
->net
);
2561 SET_NETDEV_DEV (dev
->net
, &gadget
->dev
);
2562 status
= register_netdev (dev
->net
);
2566 INFO (dev
, "%s, version: " DRIVER_VERSION
"\n", driver_desc
);
2567 INFO (dev
, "using %s, OUT %s IN %s%s%s\n", gadget
->name
,
2568 out_ep
->name
, in_ep
->name
,
2569 status_ep
? " STATUS " : "",
2570 status_ep
? status_ep
->name
: ""
2572 INFO (dev
, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2573 net
->dev_addr
[0], net
->dev_addr
[1],
2574 net
->dev_addr
[2], net
->dev_addr
[3],
2575 net
->dev_addr
[4], net
->dev_addr
[5]);
2578 INFO (dev
, "HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2579 dev
->host_mac
[0], dev
->host_mac
[1],
2580 dev
->host_mac
[2], dev
->host_mac
[3],
2581 dev
->host_mac
[4], dev
->host_mac
[5]);
2586 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2588 dev
->rndis_config
= rndis_register (rndis_control_ack
);
2589 if (dev
->rndis_config
< 0) {
2591 unregister_netdev (dev
->net
);
2596 /* these set up a lot of the OIDs that RNDIS needs */
2597 rndis_set_host_mac (dev
->rndis_config
, dev
->host_mac
);
2598 if (rndis_set_param_dev (dev
->rndis_config
, dev
->net
,
2599 &dev
->stats
, &dev
->cdc_filter
))
2601 if (rndis_set_param_vendor (dev
->rndis_config
, vendorID
,
2604 if (rndis_set_param_medium (dev
->rndis_config
,
2608 INFO (dev
, "RNDIS ready\n");
2614 dev_dbg(&gadget
->dev
, "register_netdev failed, %d\n", status
);
2616 eth_unbind (gadget
);
2620 /*-------------------------------------------------------------------------*/
2623 eth_suspend (struct usb_gadget
*gadget
)
2625 struct eth_dev
*dev
= get_gadget_data (gadget
);
2627 DEBUG (dev
, "suspend\n");
2632 eth_resume (struct usb_gadget
*gadget
)
2634 struct eth_dev
*dev
= get_gadget_data (gadget
);
2636 DEBUG (dev
, "resume\n");
2640 /*-------------------------------------------------------------------------*/
2642 static struct usb_gadget_driver eth_driver
= {
2645 .function
= (char *) driver_desc
,
2647 .unbind
= eth_unbind
,
2650 .disconnect
= eth_disconnect
,
2652 .suspend
= eth_suspend
,
2653 .resume
= eth_resume
,
2656 .name
= (char *) shortname
,
2657 .owner
= THIS_MODULE
,
2661 MODULE_DESCRIPTION (DRIVER_DESC
);
2662 MODULE_AUTHOR ("David Brownell, Benedikt Spanger");
2663 MODULE_LICENSE ("GPL");
2666 static int __init
init (void)
2668 return usb_gadget_register_driver (ð_driver
);
2672 static void __exit
cleanup (void)
2674 usb_gadget_unregister_driver (ð_driver
);
2676 module_exit (cleanup
);