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
309 /*-------------------------------------------------------------------------*/
311 /* "main" config is either CDC, or its simple subset */
312 static inline int is_cdc(struct eth_dev
*dev
)
314 #if !defined(DEV_CONFIG_SUBSET)
315 return 1; /* only cdc possible */
316 #elif !defined (DEV_CONFIG_CDC)
317 return 0; /* only subset possible */
319 return dev
->cdc
; /* depends on what hardware we found */
323 /* "secondary" RNDIS config may sometimes be activated */
324 static inline int rndis_active(struct eth_dev
*dev
)
326 #ifdef CONFIG_USB_ETH_RNDIS
333 #define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
334 #define cdc_active(dev) ( is_cdc(dev) && !rndis_active(dev))
338 #define DEFAULT_QLEN 2 /* double buffering by default */
340 /* peak bulk transfer bits-per-second */
341 #define HS_BPS (13 * 512 * 8 * 1000 * 8)
342 #define FS_BPS (19 * 64 * 1 * 1000 * 8)
344 #ifdef CONFIG_USB_GADGET_DUALSPEED
345 #define DEVSPEED USB_SPEED_HIGH
347 static unsigned qmult
= 5;
348 module_param (qmult
, uint
, S_IRUGO
|S_IWUSR
);
351 /* for dual-speed hardware, use deeper queues at highspeed */
352 #define qlen(gadget) \
353 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
355 /* also defer IRQs on highspeed TX */
356 #define TX_DELAY qmult
358 static inline int BITRATE(struct usb_gadget
*g
)
360 return (g
->speed
== USB_SPEED_HIGH
) ? HS_BPS
: FS_BPS
;
363 #else /* full speed (low speed doesn't do bulk) */
364 #define DEVSPEED USB_SPEED_FULL
366 #define qlen(gadget) DEFAULT_QLEN
368 static inline int BITRATE(struct usb_gadget
*g
)
375 /*-------------------------------------------------------------------------*/
377 #define xprintk(d,level,fmt,args...) \
378 printk(level "%s: " fmt , (d)->net->name , ## args)
382 #define DEBUG(dev,fmt,args...) \
383 xprintk(dev , KERN_DEBUG , fmt , ## args)
385 #define DEBUG(dev,fmt,args...) \
392 #define VDEBUG(dev,fmt,args...) \
396 #define ERROR(dev,fmt,args...) \
397 xprintk(dev , KERN_ERR , fmt , ## args)
398 #define WARN(dev,fmt,args...) \
399 xprintk(dev , KERN_WARNING , fmt , ## args)
400 #define INFO(dev,fmt,args...) \
401 xprintk(dev , KERN_INFO , fmt , ## args)
403 /*-------------------------------------------------------------------------*/
405 /* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
406 * ep0 implementation: descriptors, config management, setup().
407 * also optional class-specific notification interrupt transfer.
411 * DESCRIPTORS ... most are static, but strings and (full) configuration
412 * descriptors are built on demand. For now we do either full CDC, or
413 * our simple subset, with RNDIS as an optional second configuration.
415 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
416 * the class descriptors match a modem (they're ignored; it's really just
417 * Ethernet functionality), they don't need the NOP altsetting, and the
418 * status transfer endpoint isn't optional.
421 #define STRING_MANUFACTURER 1
422 #define STRING_PRODUCT 2
423 #define STRING_ETHADDR 3
424 #define STRING_DATA 4
425 #define STRING_CONTROL 5
426 #define STRING_RNDIS_CONTROL 6
428 #define STRING_SUBSET 8
429 #define STRING_RNDIS 9
430 #define STRING_SERIALNUMBER 10
432 /* holds our biggest descriptor (or RNDIS response) */
433 #define USB_BUFSIZ 256
436 * This device advertises one configuration, eth_config, unless RNDIS
437 * is enabled (rndis_config) on hardware supporting at least two configs.
439 * NOTE: Controllers like superh_udc should probably be able to use
440 * an RNDIS-only configuration.
442 * FIXME define some higher-powered configurations to make it easier
443 * to recharge batteries ...
446 #define DEV_CONFIG_VALUE 1 /* cdc or subset */
447 #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
449 static struct usb_device_descriptor
451 .bLength
= sizeof device_desc
,
452 .bDescriptorType
= USB_DT_DEVICE
,
454 .bcdUSB
= __constant_cpu_to_le16 (0x0200),
456 .bDeviceClass
= USB_CLASS_COMM
,
457 .bDeviceSubClass
= 0,
458 .bDeviceProtocol
= 0,
460 .idVendor
= __constant_cpu_to_le16 (CDC_VENDOR_NUM
),
461 .idProduct
= __constant_cpu_to_le16 (CDC_PRODUCT_NUM
),
462 .iManufacturer
= STRING_MANUFACTURER
,
463 .iProduct
= STRING_PRODUCT
,
464 .bNumConfigurations
= 1,
467 static struct usb_otg_descriptor
469 .bLength
= sizeof otg_descriptor
,
470 .bDescriptorType
= USB_DT_OTG
,
472 .bmAttributes
= USB_OTG_SRP
,
475 static struct usb_config_descriptor
477 .bLength
= sizeof eth_config
,
478 .bDescriptorType
= USB_DT_CONFIG
,
480 /* compute wTotalLength on the fly */
482 .bConfigurationValue
= DEV_CONFIG_VALUE
,
483 .iConfiguration
= STRING_CDC
,
484 .bmAttributes
= USB_CONFIG_ATT_ONE
| USB_CONFIG_ATT_SELFPOWER
,
488 #ifdef CONFIG_USB_ETH_RNDIS
489 static struct usb_config_descriptor
491 .bLength
= sizeof rndis_config
,
492 .bDescriptorType
= USB_DT_CONFIG
,
494 /* compute wTotalLength on the fly */
496 .bConfigurationValue
= DEV_RNDIS_CONFIG_VALUE
,
497 .iConfiguration
= STRING_RNDIS
,
498 .bmAttributes
= USB_CONFIG_ATT_ONE
| USB_CONFIG_ATT_SELFPOWER
,
504 * Compared to the simple CDC subset, the full CDC Ethernet model adds
505 * three class descriptors, two interface descriptors, optional status
506 * endpoint. Both have a "data" interface and two bulk endpoints.
507 * There are also differences in how control requests are handled.
509 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
510 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
511 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
512 * work around those bugs) are unlikely to ever come from MSFT, you may
513 * wish to avoid using RNDIS.
515 * MCCI offers an alternative to RNDIS if you need to connect to Windows
516 * but have hardware that can't support CDC Ethernet. We add descriptors
517 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
518 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
519 * get those drivers from MCCI, or bundled with various products.
522 #ifdef DEV_CONFIG_CDC
523 static struct usb_interface_descriptor
525 .bLength
= sizeof control_intf
,
526 .bDescriptorType
= USB_DT_INTERFACE
,
528 .bInterfaceNumber
= 0,
529 /* status endpoint is optional; this may be patched later */
531 .bInterfaceClass
= USB_CLASS_COMM
,
532 .bInterfaceSubClass
= USB_CDC_SUBCLASS_ETHERNET
,
533 .bInterfaceProtocol
= USB_CDC_PROTO_NONE
,
534 .iInterface
= STRING_CONTROL
,
538 #ifdef CONFIG_USB_ETH_RNDIS
539 static const struct usb_interface_descriptor
540 rndis_control_intf
= {
541 .bLength
= sizeof rndis_control_intf
,
542 .bDescriptorType
= USB_DT_INTERFACE
,
544 .bInterfaceNumber
= 0,
546 .bInterfaceClass
= USB_CLASS_COMM
,
547 .bInterfaceSubClass
= USB_CDC_SUBCLASS_ACM
,
548 .bInterfaceProtocol
= USB_CDC_ACM_PROTO_VENDOR
,
549 .iInterface
= STRING_RNDIS_CONTROL
,
553 static const struct usb_cdc_header_desc header_desc
= {
554 .bLength
= sizeof header_desc
,
555 .bDescriptorType
= USB_DT_CS_INTERFACE
,
556 .bDescriptorSubType
= USB_CDC_HEADER_TYPE
,
558 .bcdCDC
= __constant_cpu_to_le16 (0x0110),
561 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
563 static const struct usb_cdc_union_desc union_desc
= {
564 .bLength
= sizeof union_desc
,
565 .bDescriptorType
= USB_DT_CS_INTERFACE
,
566 .bDescriptorSubType
= USB_CDC_UNION_TYPE
,
568 .bMasterInterface0
= 0, /* index of control interface */
569 .bSlaveInterface0
= 1, /* index of DATA interface */
572 #endif /* CDC || RNDIS */
574 #ifdef CONFIG_USB_ETH_RNDIS
576 static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor
= {
577 .bLength
= sizeof call_mgmt_descriptor
,
578 .bDescriptorType
= USB_DT_CS_INTERFACE
,
579 .bDescriptorSubType
= USB_CDC_CALL_MANAGEMENT_TYPE
,
581 .bmCapabilities
= 0x00,
582 .bDataInterface
= 0x01,
585 static const struct usb_cdc_acm_descriptor acm_descriptor
= {
586 .bLength
= sizeof acm_descriptor
,
587 .bDescriptorType
= USB_DT_CS_INTERFACE
,
588 .bDescriptorSubType
= USB_CDC_ACM_TYPE
,
590 .bmCapabilities
= 0x00,
595 #ifndef DEV_CONFIG_CDC
597 /* "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
598 * ways: data endpoints live in the control interface, there's no data
599 * interface, and it's not used to talk to a cell phone radio.
602 static const struct usb_cdc_mdlm_desc mdlm_desc
= {
603 .bLength
= sizeof mdlm_desc
,
604 .bDescriptorType
= USB_DT_CS_INTERFACE
,
605 .bDescriptorSubType
= USB_CDC_MDLM_TYPE
,
607 .bcdVersion
= __constant_cpu_to_le16(0x0100),
609 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
610 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
614 /* since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
615 * can't really use its struct. All we do here is say that we're using
616 * the submode of "SAFE" which directly matches the CDC Subset.
618 static const u8 mdlm_detail_desc
[] = {
621 USB_CDC_MDLM_DETAIL_TYPE
,
624 0, /* network control capabilities (none) */
625 0, /* network data capabilities ("raw" encapsulation) */
630 static const struct usb_cdc_ether_desc ether_desc
= {
631 .bLength
= sizeof ether_desc
,
632 .bDescriptorType
= USB_DT_CS_INTERFACE
,
633 .bDescriptorSubType
= USB_CDC_ETHERNET_TYPE
,
635 /* this descriptor actually adds value, surprise! */
636 .iMACAddress
= STRING_ETHADDR
,
637 .bmEthernetStatistics
= __constant_cpu_to_le32 (0), /* no statistics */
638 .wMaxSegmentSize
= __constant_cpu_to_le16 (ETH_FRAME_LEN
),
639 .wNumberMCFilters
= __constant_cpu_to_le16 (0),
640 .bNumberPowerFilters
= 0,
644 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
646 /* include the status endpoint if we can, even where it's optional.
647 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
648 * packet, to simplify cancellation; and a big transfer interval, to
649 * waste less bandwidth.
651 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
652 * if they ignore the connect/disconnect notifications that real aether
653 * can provide. more advanced cdc configurations might want to support
654 * encapsulated commands (vendor-specific, using control-OUT).
656 * RNDIS requires the status endpoint, since it uses that encapsulation
657 * mechanism for its funky RPC scheme.
660 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
661 #define STATUS_BYTECOUNT 16 /* 8 byte header + data */
663 static struct usb_endpoint_descriptor
665 .bLength
= USB_DT_ENDPOINT_SIZE
,
666 .bDescriptorType
= USB_DT_ENDPOINT
,
668 .bEndpointAddress
= USB_DIR_IN
,
669 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
670 .wMaxPacketSize
= __constant_cpu_to_le16 (STATUS_BYTECOUNT
),
671 .bInterval
= 1 << LOG2_STATUS_INTERVAL_MSEC
,
675 #ifdef DEV_CONFIG_CDC
677 /* the default data interface has no endpoints ... */
679 static const struct usb_interface_descriptor
681 .bLength
= sizeof data_nop_intf
,
682 .bDescriptorType
= USB_DT_INTERFACE
,
684 .bInterfaceNumber
= 1,
685 .bAlternateSetting
= 0,
687 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
688 .bInterfaceSubClass
= 0,
689 .bInterfaceProtocol
= 0,
692 /* ... but the "real" data interface has two bulk endpoints */
694 static const struct usb_interface_descriptor
696 .bLength
= sizeof data_intf
,
697 .bDescriptorType
= USB_DT_INTERFACE
,
699 .bInterfaceNumber
= 1,
700 .bAlternateSetting
= 1,
702 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
703 .bInterfaceSubClass
= 0,
704 .bInterfaceProtocol
= 0,
705 .iInterface
= STRING_DATA
,
710 #ifdef CONFIG_USB_ETH_RNDIS
712 /* RNDIS doesn't activate by changing to the "real" altsetting */
714 static const struct usb_interface_descriptor
716 .bLength
= sizeof rndis_data_intf
,
717 .bDescriptorType
= USB_DT_INTERFACE
,
719 .bInterfaceNumber
= 1,
720 .bAlternateSetting
= 0,
722 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
723 .bInterfaceSubClass
= 0,
724 .bInterfaceProtocol
= 0,
725 .iInterface
= STRING_DATA
,
730 #ifdef DEV_CONFIG_SUBSET
733 * "Simple" CDC-subset option is a simple vendor-neutral model that most
734 * full speed controllers can handle: one interface, two bulk endpoints.
736 * To assist host side drivers, we fancy it up a bit, and add descriptors
737 * so some host side drivers will understand it as a "SAFE" variant.
740 static const struct usb_interface_descriptor
742 .bLength
= sizeof subset_data_intf
,
743 .bDescriptorType
= USB_DT_INTERFACE
,
745 .bInterfaceNumber
= 0,
746 .bAlternateSetting
= 0,
748 .bInterfaceClass
= USB_CLASS_COMM
,
749 .bInterfaceSubClass
= USB_CDC_SUBCLASS_MDLM
,
750 .bInterfaceProtocol
= 0,
751 .iInterface
= STRING_DATA
,
757 static struct usb_endpoint_descriptor
759 .bLength
= USB_DT_ENDPOINT_SIZE
,
760 .bDescriptorType
= USB_DT_ENDPOINT
,
762 .bEndpointAddress
= USB_DIR_IN
,
763 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
766 static struct usb_endpoint_descriptor
768 .bLength
= USB_DT_ENDPOINT_SIZE
,
769 .bDescriptorType
= USB_DT_ENDPOINT
,
771 .bEndpointAddress
= USB_DIR_OUT
,
772 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
775 static const struct usb_descriptor_header
*fs_eth_function
[11] = {
776 (struct usb_descriptor_header
*) &otg_descriptor
,
777 #ifdef DEV_CONFIG_CDC
778 /* "cdc" mode descriptors */
779 (struct usb_descriptor_header
*) &control_intf
,
780 (struct usb_descriptor_header
*) &header_desc
,
781 (struct usb_descriptor_header
*) &union_desc
,
782 (struct usb_descriptor_header
*) ðer_desc
,
783 /* NOTE: status endpoint may need to be removed */
784 (struct usb_descriptor_header
*) &fs_status_desc
,
785 /* data interface, with altsetting */
786 (struct usb_descriptor_header
*) &data_nop_intf
,
787 (struct usb_descriptor_header
*) &data_intf
,
788 (struct usb_descriptor_header
*) &fs_source_desc
,
789 (struct usb_descriptor_header
*) &fs_sink_desc
,
791 #endif /* DEV_CONFIG_CDC */
794 static inline void __init
fs_subset_descriptors(void)
796 #ifdef DEV_CONFIG_SUBSET
797 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
798 fs_eth_function
[1] = (struct usb_descriptor_header
*) &subset_data_intf
;
799 fs_eth_function
[2] = (struct usb_descriptor_header
*) &header_desc
;
800 fs_eth_function
[3] = (struct usb_descriptor_header
*) &mdlm_desc
;
801 fs_eth_function
[4] = (struct usb_descriptor_header
*) &mdlm_detail_desc
;
802 fs_eth_function
[5] = (struct usb_descriptor_header
*) ðer_desc
;
803 fs_eth_function
[6] = (struct usb_descriptor_header
*) &fs_source_desc
;
804 fs_eth_function
[7] = (struct usb_descriptor_header
*) &fs_sink_desc
;
805 fs_eth_function
[8] = NULL
;
807 fs_eth_function
[1] = NULL
;
811 #ifdef CONFIG_USB_ETH_RNDIS
812 static const struct usb_descriptor_header
*fs_rndis_function
[] = {
813 (struct usb_descriptor_header
*) &otg_descriptor
,
814 /* control interface matches ACM, not Ethernet */
815 (struct usb_descriptor_header
*) &rndis_control_intf
,
816 (struct usb_descriptor_header
*) &header_desc
,
817 (struct usb_descriptor_header
*) &call_mgmt_descriptor
,
818 (struct usb_descriptor_header
*) &acm_descriptor
,
819 (struct usb_descriptor_header
*) &union_desc
,
820 (struct usb_descriptor_header
*) &fs_status_desc
,
821 /* data interface has no altsetting */
822 (struct usb_descriptor_header
*) &rndis_data_intf
,
823 (struct usb_descriptor_header
*) &fs_source_desc
,
824 (struct usb_descriptor_header
*) &fs_sink_desc
,
829 #ifdef CONFIG_USB_GADGET_DUALSPEED
832 * usb 2.0 devices need to expose both high speed and full speed
833 * descriptors, unless they only run at full speed.
836 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
837 static struct usb_endpoint_descriptor
839 .bLength
= USB_DT_ENDPOINT_SIZE
,
840 .bDescriptorType
= USB_DT_ENDPOINT
,
842 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
843 .wMaxPacketSize
= __constant_cpu_to_le16 (STATUS_BYTECOUNT
),
844 .bInterval
= LOG2_STATUS_INTERVAL_MSEC
+ 4,
846 #endif /* DEV_CONFIG_CDC */
848 static struct usb_endpoint_descriptor
850 .bLength
= USB_DT_ENDPOINT_SIZE
,
851 .bDescriptorType
= USB_DT_ENDPOINT
,
853 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
854 .wMaxPacketSize
= __constant_cpu_to_le16 (512),
857 static struct usb_endpoint_descriptor
859 .bLength
= USB_DT_ENDPOINT_SIZE
,
860 .bDescriptorType
= USB_DT_ENDPOINT
,
862 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
863 .wMaxPacketSize
= __constant_cpu_to_le16 (512),
866 static struct usb_qualifier_descriptor
868 .bLength
= sizeof dev_qualifier
,
869 .bDescriptorType
= USB_DT_DEVICE_QUALIFIER
,
871 .bcdUSB
= __constant_cpu_to_le16 (0x0200),
872 .bDeviceClass
= USB_CLASS_COMM
,
874 .bNumConfigurations
= 1,
877 static const struct usb_descriptor_header
*hs_eth_function
[11] = {
878 (struct usb_descriptor_header
*) &otg_descriptor
,
879 #ifdef DEV_CONFIG_CDC
880 /* "cdc" mode descriptors */
881 (struct usb_descriptor_header
*) &control_intf
,
882 (struct usb_descriptor_header
*) &header_desc
,
883 (struct usb_descriptor_header
*) &union_desc
,
884 (struct usb_descriptor_header
*) ðer_desc
,
885 /* NOTE: status endpoint may need to be removed */
886 (struct usb_descriptor_header
*) &hs_status_desc
,
887 /* data interface, with altsetting */
888 (struct usb_descriptor_header
*) &data_nop_intf
,
889 (struct usb_descriptor_header
*) &data_intf
,
890 (struct usb_descriptor_header
*) &hs_source_desc
,
891 (struct usb_descriptor_header
*) &hs_sink_desc
,
893 #endif /* DEV_CONFIG_CDC */
896 static inline void __init
hs_subset_descriptors(void)
898 #ifdef DEV_CONFIG_SUBSET
899 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
900 hs_eth_function
[1] = (struct usb_descriptor_header
*) &subset_data_intf
;
901 hs_eth_function
[2] = (struct usb_descriptor_header
*) &header_desc
;
902 hs_eth_function
[3] = (struct usb_descriptor_header
*) &mdlm_desc
;
903 hs_eth_function
[4] = (struct usb_descriptor_header
*) &mdlm_detail_desc
;
904 hs_eth_function
[5] = (struct usb_descriptor_header
*) ðer_desc
;
905 hs_eth_function
[6] = (struct usb_descriptor_header
*) &hs_source_desc
;
906 hs_eth_function
[7] = (struct usb_descriptor_header
*) &hs_sink_desc
;
907 hs_eth_function
[8] = NULL
;
909 hs_eth_function
[1] = NULL
;
913 #ifdef CONFIG_USB_ETH_RNDIS
914 static const struct usb_descriptor_header
*hs_rndis_function
[] = {
915 (struct usb_descriptor_header
*) &otg_descriptor
,
916 /* control interface matches ACM, not Ethernet */
917 (struct usb_descriptor_header
*) &rndis_control_intf
,
918 (struct usb_descriptor_header
*) &header_desc
,
919 (struct usb_descriptor_header
*) &call_mgmt_descriptor
,
920 (struct usb_descriptor_header
*) &acm_descriptor
,
921 (struct usb_descriptor_header
*) &union_desc
,
922 (struct usb_descriptor_header
*) &hs_status_desc
,
923 /* data interface has no altsetting */
924 (struct usb_descriptor_header
*) &rndis_data_intf
,
925 (struct usb_descriptor_header
*) &hs_source_desc
,
926 (struct usb_descriptor_header
*) &hs_sink_desc
,
932 /* maxpacket and other transfer characteristics vary by speed. */
933 #define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
937 /* if there's no high speed support, maxpacket doesn't change. */
938 #define ep_desc(g,hs,fs) (((void)(g)), (fs))
940 static inline void __init
hs_subset_descriptors(void)
944 #endif /* !CONFIG_USB_GADGET_DUALSPEED */
946 /*-------------------------------------------------------------------------*/
948 /* descriptors that are built on-demand */
950 static char manufacturer
[50];
951 static char product_desc
[40] = DRIVER_DESC
;
952 static char serial_number
[20];
954 /* address that the host will use ... usually assigned at random */
955 static char ethaddr
[2 * ETH_ALEN
+ 1];
957 /* static strings, in UTF-8 */
958 static struct usb_string strings
[] = {
959 { STRING_MANUFACTURER
, manufacturer
, },
960 { STRING_PRODUCT
, product_desc
, },
961 { STRING_SERIALNUMBER
, serial_number
, },
962 { STRING_DATA
, "Ethernet Data", },
963 { STRING_ETHADDR
, ethaddr
, },
964 #ifdef DEV_CONFIG_CDC
965 { STRING_CDC
, "CDC Ethernet", },
966 { STRING_CONTROL
, "CDC Communications Control", },
968 #ifdef DEV_CONFIG_SUBSET
969 { STRING_SUBSET
, "CDC Ethernet Subset", },
971 #ifdef CONFIG_USB_ETH_RNDIS
972 { STRING_RNDIS
, "RNDIS", },
973 { STRING_RNDIS_CONTROL
, "RNDIS Communications Control", },
975 { } /* end of list */
978 static struct usb_gadget_strings stringtab
= {
979 .language
= 0x0409, /* en-us */
984 * one config, two interfaces: control, data.
985 * complications: class descriptors, and an altsetting.
988 config_buf (enum usb_device_speed speed
,
990 unsigned index
, int is_otg
)
993 const struct usb_config_descriptor
*config
;
994 const struct usb_descriptor_header
**function
;
995 #ifdef CONFIG_USB_GADGET_DUALSPEED
996 int hs
= (speed
== USB_SPEED_HIGH
);
998 if (type
== USB_DT_OTHER_SPEED_CONFIG
)
1000 #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
1002 #define which_fn(t) (fs_ ## t ## _function)
1005 if (index
>= device_desc
.bNumConfigurations
)
1008 #ifdef CONFIG_USB_ETH_RNDIS
1009 /* list the RNDIS config first, to make Microsoft's drivers
1010 * happy. DOCSIS 1.0 needs this too.
1012 if (device_desc
.bNumConfigurations
== 2 && index
== 0) {
1013 config
= &rndis_config
;
1014 function
= which_fn (rndis
);
1018 config
= ð_config
;
1019 function
= which_fn (eth
);
1022 /* for now, don't advertise srp-only devices */
1026 len
= usb_gadget_config_buf (config
, buf
, USB_BUFSIZ
, function
);
1029 ((struct usb_config_descriptor
*) buf
)->bDescriptorType
= type
;
1033 /*-------------------------------------------------------------------------*/
1035 static void eth_start (struct eth_dev
*dev
, gfp_t gfp_flags
);
1036 static int alloc_requests (struct eth_dev
*dev
, unsigned n
, gfp_t gfp_flags
);
1039 set_ether_config (struct eth_dev
*dev
, gfp_t gfp_flags
)
1042 struct usb_gadget
*gadget
= dev
->gadget
;
1044 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
1045 /* status endpoint used for RNDIS and (optionally) CDC */
1046 if (!subset_active(dev
) && dev
->status_ep
) {
1047 dev
->status
= ep_desc (gadget
, &hs_status_desc
,
1049 dev
->status_ep
->driver_data
= dev
;
1051 result
= usb_ep_enable (dev
->status_ep
, dev
->status
);
1053 DEBUG (dev
, "enable %s --> %d\n",
1054 dev
->status_ep
->name
, result
);
1060 dev
->in
= ep_desc(gadget
, &hs_source_desc
, &fs_source_desc
);
1061 dev
->in_ep
->driver_data
= dev
;
1063 dev
->out
= ep_desc(gadget
, &hs_sink_desc
, &fs_sink_desc
);
1064 dev
->out_ep
->driver_data
= dev
;
1066 /* With CDC, the host isn't allowed to use these two data
1067 * endpoints in the default altsetting for the interface.
1068 * so we don't activate them yet. Reset from SET_INTERFACE.
1070 * Strictly speaking RNDIS should work the same: activation is
1071 * a side effect of setting a packet filter. Deactivation is
1072 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
1074 if (!cdc_active(dev
)) {
1075 result
= usb_ep_enable (dev
->in_ep
, dev
->in
);
1077 DEBUG(dev
, "enable %s --> %d\n",
1078 dev
->in_ep
->name
, result
);
1082 result
= usb_ep_enable (dev
->out_ep
, dev
->out
);
1084 DEBUG (dev
, "enable %s --> %d\n",
1085 dev
->out_ep
->name
, result
);
1092 result
= alloc_requests (dev
, qlen (gadget
), gfp_flags
);
1094 /* on error, disable any endpoints */
1096 if (!subset_active(dev
))
1097 (void) usb_ep_disable (dev
->status_ep
);
1099 (void) usb_ep_disable (dev
->in_ep
);
1100 (void) usb_ep_disable (dev
->out_ep
);
1105 /* activate non-CDC configs right away
1106 * this isn't strictly according to the RNDIS spec
1108 if (!cdc_active (dev
)) {
1109 netif_carrier_on (dev
->net
);
1110 if (netif_running (dev
->net
)) {
1111 spin_unlock (&dev
->lock
);
1112 eth_start (dev
, GFP_ATOMIC
);
1113 spin_lock (&dev
->lock
);
1118 DEBUG (dev
, "qlen %d\n", qlen (gadget
));
1120 /* caller is responsible for cleanup on error */
1124 static void eth_reset_config (struct eth_dev
*dev
)
1126 struct usb_request
*req
;
1128 if (dev
->config
== 0)
1131 DEBUG (dev
, "%s\n", __FUNCTION__
);
1133 netif_stop_queue (dev
->net
);
1134 netif_carrier_off (dev
->net
);
1135 rndis_uninit(dev
->rndis_config
);
1137 /* disable endpoints, forcing (synchronous) completion of
1138 * pending i/o. then free the requests.
1141 usb_ep_disable (dev
->in_ep
);
1142 spin_lock(&dev
->req_lock
);
1143 while (likely (!list_empty (&dev
->tx_reqs
))) {
1144 req
= container_of (dev
->tx_reqs
.next
,
1145 struct usb_request
, list
);
1146 list_del (&req
->list
);
1148 spin_unlock(&dev
->req_lock
);
1149 usb_ep_free_request (dev
->in_ep
, req
);
1150 spin_lock(&dev
->req_lock
);
1152 spin_unlock(&dev
->req_lock
);
1155 usb_ep_disable (dev
->out_ep
);
1156 spin_lock(&dev
->req_lock
);
1157 while (likely (!list_empty (&dev
->rx_reqs
))) {
1158 req
= container_of (dev
->rx_reqs
.next
,
1159 struct usb_request
, list
);
1160 list_del (&req
->list
);
1162 spin_unlock(&dev
->req_lock
);
1163 usb_ep_free_request (dev
->out_ep
, req
);
1164 spin_lock(&dev
->req_lock
);
1166 spin_unlock(&dev
->req_lock
);
1170 usb_ep_disable (dev
->status_ep
);
1173 dev
->cdc_filter
= 0;
1177 /* change our operational config. must agree with the code
1178 * that returns config descriptors, and altsetting code.
1181 eth_set_config (struct eth_dev
*dev
, unsigned number
, gfp_t gfp_flags
)
1184 struct usb_gadget
*gadget
= dev
->gadget
;
1186 if (gadget_is_sa1100 (gadget
)
1188 && atomic_read (&dev
->tx_qlen
) != 0) {
1189 /* tx fifo is full, but we can't clear it...*/
1190 INFO (dev
, "can't change configurations\n");
1193 eth_reset_config (dev
);
1196 case DEV_CONFIG_VALUE
:
1197 result
= set_ether_config (dev
, gfp_flags
);
1199 #ifdef CONFIG_USB_ETH_RNDIS
1200 case DEV_RNDIS_CONFIG_VALUE
:
1202 result
= set_ether_config (dev
, gfp_flags
);
1214 eth_reset_config (dev
);
1215 usb_gadget_vbus_draw(dev
->gadget
,
1216 dev
->gadget
->is_otg
? 8 : 100);
1221 power
= 2 * eth_config
.bMaxPower
;
1222 usb_gadget_vbus_draw(dev
->gadget
, power
);
1224 switch (gadget
->speed
) {
1225 case USB_SPEED_FULL
: speed
= "full"; break;
1226 #ifdef CONFIG_USB_GADGET_DUALSPEED
1227 case USB_SPEED_HIGH
: speed
= "high"; break;
1229 default: speed
= "?"; break;
1232 dev
->config
= number
;
1233 INFO (dev
, "%s speed config #%d: %d mA, %s, using %s\n",
1234 speed
, number
, power
, driver_desc
,
1239 : "CDC Ethernet Subset"));
1244 /*-------------------------------------------------------------------------*/
1246 #ifdef DEV_CONFIG_CDC
1248 /* The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
1249 * only to notify the host about link status changes (which we support) or
1250 * report completion of some encapsulated command (as used in RNDIS). Since
1251 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1252 * command mechanism; and only one status request is ever queued.
1255 static void eth_status_complete (struct usb_ep
*ep
, struct usb_request
*req
)
1257 struct usb_cdc_notification
*event
= req
->buf
;
1258 int value
= req
->status
;
1259 struct eth_dev
*dev
= ep
->driver_data
;
1261 /* issue the second notification if host reads the first */
1262 if (event
->bNotificationType
== USB_CDC_NOTIFY_NETWORK_CONNECTION
1264 __le32
*data
= req
->buf
+ sizeof *event
;
1266 event
->bmRequestType
= 0xA1;
1267 event
->bNotificationType
= USB_CDC_NOTIFY_SPEED_CHANGE
;
1268 event
->wValue
= __constant_cpu_to_le16 (0);
1269 event
->wIndex
= __constant_cpu_to_le16 (1);
1270 event
->wLength
= __constant_cpu_to_le16 (8);
1272 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1273 data
[0] = data
[1] = cpu_to_le32 (BITRATE (dev
->gadget
));
1275 req
->length
= STATUS_BYTECOUNT
;
1276 value
= usb_ep_queue (ep
, req
, GFP_ATOMIC
);
1277 DEBUG (dev
, "send SPEED_CHANGE --> %d\n", value
);
1280 } else if (value
!= -ECONNRESET
)
1281 DEBUG (dev
, "event %02x --> %d\n",
1282 event
->bNotificationType
, value
);
1283 req
->context
= NULL
;
1286 static void issue_start_status (struct eth_dev
*dev
)
1288 struct usb_request
*req
= dev
->stat_req
;
1289 struct usb_cdc_notification
*event
;
1292 DEBUG (dev
, "%s, flush old status first\n", __FUNCTION__
);
1296 * FIXME ugly idiom, maybe we'd be better with just
1297 * a "cancel the whole queue" primitive since any
1298 * unlink-one primitive has way too many error modes.
1299 * here, we "know" toggle is already clear...
1301 * FIXME iff req->context != null just dequeue it
1303 usb_ep_disable (dev
->status_ep
);
1304 usb_ep_enable (dev
->status_ep
, dev
->status
);
1306 /* 3.8.1 says to issue first NETWORK_CONNECTION, then
1307 * a SPEED_CHANGE. could be useful in some configs.
1310 event
->bmRequestType
= 0xA1;
1311 event
->bNotificationType
= USB_CDC_NOTIFY_NETWORK_CONNECTION
;
1312 event
->wValue
= __constant_cpu_to_le16 (1); /* connected */
1313 event
->wIndex
= __constant_cpu_to_le16 (1);
1316 req
->length
= sizeof *event
;
1317 req
->complete
= eth_status_complete
;
1320 value
= usb_ep_queue (dev
->status_ep
, req
, GFP_ATOMIC
);
1322 DEBUG (dev
, "status buf queue --> %d\n", value
);
1327 /*-------------------------------------------------------------------------*/
1329 static void eth_setup_complete (struct usb_ep
*ep
, struct usb_request
*req
)
1331 if (req
->status
|| req
->actual
!= req
->length
)
1332 DEBUG ((struct eth_dev
*) ep
->driver_data
,
1333 "setup complete --> %d, %d/%d\n",
1334 req
->status
, req
->actual
, req
->length
);
1337 #ifdef CONFIG_USB_ETH_RNDIS
1339 static void rndis_response_complete (struct usb_ep
*ep
, struct usb_request
*req
)
1341 if (req
->status
|| req
->actual
!= req
->length
)
1342 DEBUG ((struct eth_dev
*) ep
->driver_data
,
1343 "rndis response complete --> %d, %d/%d\n",
1344 req
->status
, req
->actual
, req
->length
);
1346 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1349 static void rndis_command_complete (struct usb_ep
*ep
, struct usb_request
*req
)
1351 struct eth_dev
*dev
= ep
->driver_data
;
1354 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1355 spin_lock(&dev
->lock
);
1356 status
= rndis_msg_parser (dev
->rndis_config
, (u8
*) req
->buf
);
1358 ERROR(dev
, "%s: rndis parse error %d\n", __FUNCTION__
, status
);
1359 spin_unlock(&dev
->lock
);
1365 * The setup() callback implements all the ep0 functionality that's not
1366 * handled lower down. CDC has a number of less-common features:
1368 * - two interfaces: control, and ethernet data
1369 * - Ethernet data interface has two altsettings: default, and active
1370 * - class-specific descriptors for the control interface
1371 * - class-specific control requests
1374 eth_setup (struct usb_gadget
*gadget
, const struct usb_ctrlrequest
*ctrl
)
1376 struct eth_dev
*dev
= get_gadget_data (gadget
);
1377 struct usb_request
*req
= dev
->req
;
1378 int value
= -EOPNOTSUPP
;
1379 u16 wIndex
= le16_to_cpu(ctrl
->wIndex
);
1380 u16 wValue
= le16_to_cpu(ctrl
->wValue
);
1381 u16 wLength
= le16_to_cpu(ctrl
->wLength
);
1383 /* descriptors just go into the pre-allocated ep0 buffer,
1384 * while config change events may enable network traffic.
1386 req
->complete
= eth_setup_complete
;
1387 switch (ctrl
->bRequest
) {
1389 case USB_REQ_GET_DESCRIPTOR
:
1390 if (ctrl
->bRequestType
!= USB_DIR_IN
)
1392 switch (wValue
>> 8) {
1395 value
= min (wLength
, (u16
) sizeof device_desc
);
1396 memcpy (req
->buf
, &device_desc
, value
);
1398 #ifdef CONFIG_USB_GADGET_DUALSPEED
1399 case USB_DT_DEVICE_QUALIFIER
:
1400 if (!gadget
->is_dualspeed
)
1402 value
= min (wLength
, (u16
) sizeof dev_qualifier
);
1403 memcpy (req
->buf
, &dev_qualifier
, value
);
1406 case USB_DT_OTHER_SPEED_CONFIG
:
1407 if (!gadget
->is_dualspeed
)
1410 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1412 value
= config_buf (gadget
->speed
, req
->buf
,
1417 value
= min (wLength
, (u16
) value
);
1421 value
= usb_gadget_get_string (&stringtab
,
1422 wValue
& 0xff, req
->buf
);
1424 value
= min (wLength
, (u16
) value
);
1429 case USB_REQ_SET_CONFIGURATION
:
1430 if (ctrl
->bRequestType
!= 0)
1432 if (gadget
->a_hnp_support
)
1433 DEBUG (dev
, "HNP available\n");
1434 else if (gadget
->a_alt_hnp_support
)
1435 DEBUG (dev
, "HNP needs a different root port\n");
1436 spin_lock (&dev
->lock
);
1437 value
= eth_set_config (dev
, wValue
, GFP_ATOMIC
);
1438 spin_unlock (&dev
->lock
);
1440 case USB_REQ_GET_CONFIGURATION
:
1441 if (ctrl
->bRequestType
!= USB_DIR_IN
)
1443 *(u8
*)req
->buf
= dev
->config
;
1444 value
= min (wLength
, (u16
) 1);
1447 case USB_REQ_SET_INTERFACE
:
1448 if (ctrl
->bRequestType
!= USB_RECIP_INTERFACE
1452 if (!cdc_active(dev
) && wIndex
!= 0)
1454 spin_lock (&dev
->lock
);
1456 /* PXA hardware partially handles SET_INTERFACE;
1457 * we need to kluge around that interference.
1459 if (gadget_is_pxa (gadget
)) {
1460 value
= eth_set_config (dev
, DEV_CONFIG_VALUE
,
1465 #ifdef DEV_CONFIG_CDC
1467 case 0: /* control/master intf */
1471 usb_ep_disable (dev
->status_ep
);
1472 usb_ep_enable (dev
->status_ep
, dev
->status
);
1476 case 1: /* data intf */
1479 usb_ep_disable (dev
->in_ep
);
1480 usb_ep_disable (dev
->out_ep
);
1482 /* CDC requires the data transfers not be done from
1483 * the default interface setting ... also, setting
1484 * the non-default interface resets filters etc.
1487 if (!cdc_active (dev
))
1489 usb_ep_enable (dev
->in_ep
, dev
->in
);
1490 usb_ep_enable (dev
->out_ep
, dev
->out
);
1491 dev
->cdc_filter
= DEFAULT_FILTER
;
1492 netif_carrier_on (dev
->net
);
1494 issue_start_status (dev
);
1495 if (netif_running (dev
->net
)) {
1496 spin_unlock (&dev
->lock
);
1497 eth_start (dev
, GFP_ATOMIC
);
1498 spin_lock (&dev
->lock
);
1501 netif_stop_queue (dev
->net
);
1502 netif_carrier_off (dev
->net
);
1508 /* FIXME this is wrong, as is the assumption that
1509 * all non-PXA hardware talks real CDC ...
1511 dev_warn (&gadget
->dev
, "set_interface ignored!\n");
1512 #endif /* DEV_CONFIG_CDC */
1515 spin_unlock (&dev
->lock
);
1517 case USB_REQ_GET_INTERFACE
:
1518 if (ctrl
->bRequestType
!= (USB_DIR_IN
|USB_RECIP_INTERFACE
)
1522 if (!(cdc_active(dev
) || rndis_active(dev
)) && wIndex
!= 0)
1525 /* for CDC, iff carrier is on, data interface is active. */
1526 if (rndis_active(dev
) || wIndex
!= 1)
1527 *(u8
*)req
->buf
= 0;
1529 *(u8
*)req
->buf
= netif_carrier_ok (dev
->net
) ? 1 : 0;
1530 value
= min (wLength
, (u16
) 1);
1533 #ifdef DEV_CONFIG_CDC
1534 case USB_CDC_SET_ETHERNET_PACKET_FILTER
:
1535 /* see 6.2.30: no data, wIndex = interface,
1536 * wValue = packet filter bitmap
1538 if (ctrl
->bRequestType
!= (USB_TYPE_CLASS
|USB_RECIP_INTERFACE
)
1543 DEBUG (dev
, "packet filter %02x\n", wValue
);
1544 dev
->cdc_filter
= wValue
;
1549 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1550 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1551 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1552 * case USB_CDC_GET_ETHERNET_STATISTIC:
1555 #endif /* DEV_CONFIG_CDC */
1557 #ifdef CONFIG_USB_ETH_RNDIS
1558 /* RNDIS uses the CDC command encapsulation mechanism to implement
1559 * an RPC scheme, with much getting/setting of attributes by OID.
1561 case USB_CDC_SEND_ENCAPSULATED_COMMAND
:
1562 if (ctrl
->bRequestType
!= (USB_TYPE_CLASS
|USB_RECIP_INTERFACE
)
1563 || !rndis_active(dev
)
1564 || wLength
> USB_BUFSIZ
1566 || rndis_control_intf
.bInterfaceNumber
1569 /* read the request, then process it */
1571 req
->complete
= rndis_command_complete
;
1572 /* later, rndis_control_ack () sends a notification */
1575 case USB_CDC_GET_ENCAPSULATED_RESPONSE
:
1576 if ((USB_DIR_IN
|USB_TYPE_CLASS
|USB_RECIP_INTERFACE
)
1577 == ctrl
->bRequestType
1578 && rndis_active(dev
)
1579 // && wLength >= 0x0400
1581 && rndis_control_intf
.bInterfaceNumber
1585 /* return the result */
1586 buf
= rndis_get_next_response (dev
->rndis_config
,
1589 memcpy (req
->buf
, buf
, value
);
1590 req
->complete
= rndis_response_complete
;
1591 rndis_free_response(dev
->rndis_config
, buf
);
1593 /* else stalls ... spec says to avoid that */
1600 "unknown control req%02x.%02x v%04x i%04x l%d\n",
1601 ctrl
->bRequestType
, ctrl
->bRequest
,
1602 wValue
, wIndex
, wLength
);
1605 /* respond with data transfer before status phase? */
1607 req
->length
= value
;
1608 req
->zero
= value
< wLength
1609 && (value
% gadget
->ep0
->maxpacket
) == 0;
1610 value
= usb_ep_queue (gadget
->ep0
, req
, GFP_ATOMIC
);
1612 DEBUG (dev
, "ep_queue --> %d\n", value
);
1614 eth_setup_complete (gadget
->ep0
, req
);
1618 /* host either stalls (value < 0) or reports success */
1623 eth_disconnect (struct usb_gadget
*gadget
)
1625 struct eth_dev
*dev
= get_gadget_data (gadget
);
1626 unsigned long flags
;
1628 spin_lock_irqsave (&dev
->lock
, flags
);
1629 netif_stop_queue (dev
->net
);
1630 netif_carrier_off (dev
->net
);
1631 eth_reset_config (dev
);
1632 spin_unlock_irqrestore (&dev
->lock
, flags
);
1634 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1636 /* next we may get setup() calls to enumerate new connections;
1637 * or an unbind() during shutdown (including removing module).
1641 /*-------------------------------------------------------------------------*/
1643 /* NETWORK DRIVER HOOKUP (to the layer above this driver) */
1645 static int eth_change_mtu (struct net_device
*net
, int new_mtu
)
1647 struct eth_dev
*dev
= netdev_priv(net
);
1652 if (new_mtu
<= ETH_HLEN
|| new_mtu
> ETH_FRAME_LEN
)
1654 /* no zero-length packet read wanted after mtu-sized packets */
1655 if (((new_mtu
+ sizeof (struct ethhdr
)) % dev
->in_ep
->maxpacket
) == 0)
1661 static struct net_device_stats
*eth_get_stats (struct net_device
*net
)
1663 return &((struct eth_dev
*)netdev_priv(net
))->stats
;
1666 static void eth_get_drvinfo(struct net_device
*net
, struct ethtool_drvinfo
*p
)
1668 struct eth_dev
*dev
= netdev_priv(net
);
1669 strlcpy(p
->driver
, shortname
, sizeof p
->driver
);
1670 strlcpy(p
->version
, DRIVER_VERSION
, sizeof p
->version
);
1671 strlcpy(p
->fw_version
, dev
->gadget
->name
, sizeof p
->fw_version
);
1672 strlcpy (p
->bus_info
, dev
->gadget
->dev
.bus_id
, sizeof p
->bus_info
);
1675 static u32
eth_get_link(struct net_device
*net
)
1677 struct eth_dev
*dev
= netdev_priv(net
);
1678 return dev
->gadget
->speed
!= USB_SPEED_UNKNOWN
;
1681 static struct ethtool_ops ops
= {
1682 .get_drvinfo
= eth_get_drvinfo
,
1683 .get_link
= eth_get_link
1686 static void defer_kevent (struct eth_dev
*dev
, int flag
)
1688 if (test_and_set_bit (flag
, &dev
->todo
))
1690 if (!schedule_work (&dev
->work
))
1691 ERROR (dev
, "kevent %d may have been dropped\n", flag
);
1693 DEBUG (dev
, "kevent %d scheduled\n", flag
);
1696 static void rx_complete (struct usb_ep
*ep
, struct usb_request
*req
);
1699 rx_submit (struct eth_dev
*dev
, struct usb_request
*req
, gfp_t gfp_flags
)
1701 struct sk_buff
*skb
;
1702 int retval
= -ENOMEM
;
1705 /* Padding up to RX_EXTRA handles minor disagreements with host.
1706 * Normally we use the USB "terminate on short read" convention;
1707 * so allow up to (N*maxpacket), since that memory is normally
1708 * already allocated. Some hardware doesn't deal well with short
1709 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1710 * byte off the end (to force hardware errors on overflow).
1712 * RNDIS uses internal framing, and explicitly allows senders to
1713 * pad to end-of-packet. That's potentially nice for speed,
1714 * but means receivers can't recover synch on their own.
1716 size
= (sizeof (struct ethhdr
) + dev
->net
->mtu
+ RX_EXTRA
);
1717 size
+= dev
->out_ep
->maxpacket
- 1;
1718 if (rndis_active(dev
))
1719 size
+= sizeof (struct rndis_packet_msg_type
);
1720 size
-= size
% dev
->out_ep
->maxpacket
;
1722 if ((skb
= alloc_skb (size
+ NET_IP_ALIGN
, gfp_flags
)) == 0) {
1723 DEBUG (dev
, "no rx skb\n");
1727 /* Some platforms perform better when IP packets are aligned,
1728 * but on at least one, checksumming fails otherwise. Note:
1729 * RNDIS headers involve variable numbers of LE32 values.
1731 skb_reserve(skb
, NET_IP_ALIGN
);
1733 req
->buf
= skb
->data
;
1735 req
->complete
= rx_complete
;
1738 retval
= usb_ep_queue (dev
->out_ep
, req
, gfp_flags
);
1739 if (retval
== -ENOMEM
)
1741 defer_kevent (dev
, WORK_RX_MEMORY
);
1743 DEBUG (dev
, "rx submit --> %d\n", retval
);
1745 dev_kfree_skb_any(skb
);
1746 spin_lock(&dev
->req_lock
);
1747 list_add (&req
->list
, &dev
->rx_reqs
);
1748 spin_unlock(&dev
->req_lock
);
1753 static void rx_complete (struct usb_ep
*ep
, struct usb_request
*req
)
1755 struct sk_buff
*skb
= req
->context
;
1756 struct eth_dev
*dev
= ep
->driver_data
;
1757 int status
= req
->status
;
1761 /* normal completion */
1763 skb_put (skb
, req
->actual
);
1764 /* we know MaxPacketsPerTransfer == 1 here */
1765 if (rndis_active(dev
))
1766 status
= rndis_rm_hdr (skb
);
1768 || ETH_HLEN
> skb
->len
1769 || skb
->len
> ETH_FRAME_LEN
) {
1770 dev
->stats
.rx_errors
++;
1771 dev
->stats
.rx_length_errors
++;
1772 DEBUG (dev
, "rx length %d\n", skb
->len
);
1776 skb
->protocol
= eth_type_trans (skb
, dev
->net
);
1777 dev
->stats
.rx_packets
++;
1778 dev
->stats
.rx_bytes
+= skb
->len
;
1780 /* no buffer copies needed, unless hardware can't
1783 status
= netif_rx (skb
);
1787 /* software-driven interface shutdown */
1788 case -ECONNRESET
: // unlink
1789 case -ESHUTDOWN
: // disconnect etc
1790 VDEBUG (dev
, "rx shutdown, code %d\n", status
);
1793 /* for hardware automagic (such as pxa) */
1794 case -ECONNABORTED
: // endpoint reset
1795 DEBUG (dev
, "rx %s reset\n", ep
->name
);
1796 defer_kevent (dev
, WORK_RX_MEMORY
);
1798 dev_kfree_skb_any (skb
);
1803 dev
->stats
.rx_over_errors
++;
1807 dev
->stats
.rx_errors
++;
1808 DEBUG (dev
, "rx status %d\n", status
);
1813 dev_kfree_skb_any (skb
);
1814 if (!netif_running (dev
->net
)) {
1816 spin_lock(&dev
->req_lock
);
1817 list_add (&req
->list
, &dev
->rx_reqs
);
1818 spin_unlock(&dev
->req_lock
);
1822 rx_submit (dev
, req
, GFP_ATOMIC
);
1825 static int prealloc (struct list_head
*list
, struct usb_ep
*ep
,
1826 unsigned n
, gfp_t gfp_flags
)
1829 struct usb_request
*req
;
1834 /* queue/recycle up to N requests */
1836 list_for_each_entry (req
, list
, list
) {
1841 req
= usb_ep_alloc_request (ep
, gfp_flags
);
1843 return list_empty (list
) ? -ENOMEM
: 0;
1844 list_add (&req
->list
, list
);
1851 struct list_head
*next
;
1853 next
= req
->list
.next
;
1854 list_del (&req
->list
);
1855 usb_ep_free_request (ep
, req
);
1860 req
= container_of (next
, struct usb_request
, list
);
1865 static int alloc_requests (struct eth_dev
*dev
, unsigned n
, gfp_t gfp_flags
)
1869 spin_lock(&dev
->req_lock
);
1870 status
= prealloc (&dev
->tx_reqs
, dev
->in_ep
, n
, gfp_flags
);
1873 status
= prealloc (&dev
->rx_reqs
, dev
->out_ep
, n
, gfp_flags
);
1878 DEBUG (dev
, "can't alloc requests\n");
1880 spin_unlock(&dev
->req_lock
);
1884 static void rx_fill (struct eth_dev
*dev
, gfp_t gfp_flags
)
1886 struct usb_request
*req
;
1887 unsigned long flags
;
1889 /* fill unused rxq slots with some skb */
1890 spin_lock_irqsave(&dev
->req_lock
, flags
);
1891 while (!list_empty (&dev
->rx_reqs
)) {
1892 req
= container_of (dev
->rx_reqs
.next
,
1893 struct usb_request
, list
);
1894 list_del_init (&req
->list
);
1895 spin_unlock_irqrestore(&dev
->req_lock
, flags
);
1897 if (rx_submit (dev
, req
, gfp_flags
) < 0) {
1898 defer_kevent (dev
, WORK_RX_MEMORY
);
1902 spin_lock_irqsave(&dev
->req_lock
, flags
);
1904 spin_unlock_irqrestore(&dev
->req_lock
, flags
);
1907 static void eth_work (struct work_struct
*work
)
1909 struct eth_dev
*dev
= container_of(work
, struct eth_dev
, work
);
1911 if (test_and_clear_bit (WORK_RX_MEMORY
, &dev
->todo
)) {
1912 if (netif_running (dev
->net
))
1913 rx_fill (dev
, GFP_KERNEL
);
1917 DEBUG (dev
, "work done, flags = 0x%lx\n", dev
->todo
);
1920 static void tx_complete (struct usb_ep
*ep
, struct usb_request
*req
)
1922 struct sk_buff
*skb
= req
->context
;
1923 struct eth_dev
*dev
= ep
->driver_data
;
1925 switch (req
->status
) {
1927 dev
->stats
.tx_errors
++;
1928 VDEBUG (dev
, "tx err %d\n", req
->status
);
1930 case -ECONNRESET
: // unlink
1931 case -ESHUTDOWN
: // disconnect etc
1934 dev
->stats
.tx_bytes
+= skb
->len
;
1936 dev
->stats
.tx_packets
++;
1938 spin_lock(&dev
->req_lock
);
1939 list_add (&req
->list
, &dev
->tx_reqs
);
1940 spin_unlock(&dev
->req_lock
);
1941 dev_kfree_skb_any (skb
);
1943 atomic_dec (&dev
->tx_qlen
);
1944 if (netif_carrier_ok (dev
->net
))
1945 netif_wake_queue (dev
->net
);
1948 static inline int eth_is_promisc (struct eth_dev
*dev
)
1950 /* no filters for the CDC subset; always promisc */
1951 if (subset_active (dev
))
1953 return dev
->cdc_filter
& USB_CDC_PACKET_TYPE_PROMISCUOUS
;
1956 static int eth_start_xmit (struct sk_buff
*skb
, struct net_device
*net
)
1958 struct eth_dev
*dev
= netdev_priv(net
);
1959 int length
= skb
->len
;
1961 struct usb_request
*req
= NULL
;
1962 unsigned long flags
;
1964 /* apply outgoing CDC or RNDIS filters */
1965 if (!eth_is_promisc (dev
)) {
1966 u8
*dest
= skb
->data
;
1968 if (is_multicast_ether_addr(dest
)) {
1971 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1972 * SET_ETHERNET_MULTICAST_FILTERS requests
1974 if (is_broadcast_ether_addr(dest
))
1975 type
= USB_CDC_PACKET_TYPE_BROADCAST
;
1977 type
= USB_CDC_PACKET_TYPE_ALL_MULTICAST
;
1978 if (!(dev
->cdc_filter
& type
)) {
1979 dev_kfree_skb_any (skb
);
1983 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1986 spin_lock_irqsave(&dev
->req_lock
, flags
);
1987 req
= container_of (dev
->tx_reqs
.next
, struct usb_request
, list
);
1988 list_del (&req
->list
);
1989 if (list_empty (&dev
->tx_reqs
))
1990 netif_stop_queue (net
);
1991 spin_unlock_irqrestore(&dev
->req_lock
, flags
);
1993 /* no buffer copies needed, unless the network stack did it
1994 * or the hardware can't use skb buffers.
1995 * or there's not enough space for any RNDIS headers we need
1997 if (rndis_active(dev
)) {
1998 struct sk_buff
*skb_rndis
;
2000 skb_rndis
= skb_realloc_headroom (skb
,
2001 sizeof (struct rndis_packet_msg_type
));
2005 dev_kfree_skb_any (skb
);
2007 rndis_add_hdr (skb
);
2010 req
->buf
= skb
->data
;
2012 req
->complete
= tx_complete
;
2014 /* use zlp framing on tx for strict CDC-Ether conformance,
2015 * though any robust network rx path ignores extra padding.
2016 * and some hardware doesn't like to write zlps.
2019 if (!dev
->zlp
&& (length
% dev
->in_ep
->maxpacket
) == 0)
2022 req
->length
= length
;
2024 #ifdef CONFIG_USB_GADGET_DUALSPEED
2025 /* throttle highspeed IRQ rate back slightly */
2026 req
->no_interrupt
= (dev
->gadget
->speed
== USB_SPEED_HIGH
)
2027 ? ((atomic_read (&dev
->tx_qlen
) % TX_DELAY
) != 0)
2031 retval
= usb_ep_queue (dev
->in_ep
, req
, GFP_ATOMIC
);
2034 DEBUG (dev
, "tx queue err %d\n", retval
);
2037 net
->trans_start
= jiffies
;
2038 atomic_inc (&dev
->tx_qlen
);
2043 dev
->stats
.tx_dropped
++;
2044 dev_kfree_skb_any (skb
);
2045 spin_lock_irqsave(&dev
->req_lock
, flags
);
2046 if (list_empty (&dev
->tx_reqs
))
2047 netif_start_queue (net
);
2048 list_add (&req
->list
, &dev
->tx_reqs
);
2049 spin_unlock_irqrestore(&dev
->req_lock
, flags
);
2054 /*-------------------------------------------------------------------------*/
2056 #ifdef CONFIG_USB_ETH_RNDIS
2058 /* The interrupt endpoint is used in RNDIS to notify the host when messages
2059 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
2060 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
2061 * REMOTE_NDIS_KEEPALIVE_MSG.
2063 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
2064 * normally just one notification will be queued.
2067 static struct usb_request
*eth_req_alloc (struct usb_ep
*, unsigned, gfp_t
);
2068 static void eth_req_free (struct usb_ep
*ep
, struct usb_request
*req
);
2071 rndis_control_ack_complete (struct usb_ep
*ep
, struct usb_request
*req
)
2073 struct eth_dev
*dev
= ep
->driver_data
;
2075 if (req
->status
|| req
->actual
!= req
->length
)
2077 "rndis control ack complete --> %d, %d/%d\n",
2078 req
->status
, req
->actual
, req
->length
);
2079 req
->context
= NULL
;
2081 if (req
!= dev
->stat_req
)
2082 eth_req_free(ep
, req
);
2085 static int rndis_control_ack (struct net_device
*net
)
2087 struct eth_dev
*dev
= netdev_priv(net
);
2089 struct usb_request
*resp
= dev
->stat_req
;
2091 /* in case RNDIS calls this after disconnect */
2093 DEBUG (dev
, "status ENODEV\n");
2097 /* in case queue length > 1 */
2098 if (resp
->context
) {
2099 resp
= eth_req_alloc (dev
->status_ep
, 8, GFP_ATOMIC
);
2104 /* Send RNDIS RESPONSE_AVAILABLE notification;
2105 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
2108 resp
->complete
= rndis_control_ack_complete
;
2109 resp
->context
= dev
;
2111 *((__le32
*) resp
->buf
) = __constant_cpu_to_le32 (1);
2112 *((__le32
*) resp
->buf
+ 1) = __constant_cpu_to_le32 (0);
2114 length
= usb_ep_queue (dev
->status_ep
, resp
, GFP_ATOMIC
);
2117 rndis_control_ack_complete (dev
->status_ep
, resp
);
2125 #define rndis_control_ack NULL
2129 static void eth_start (struct eth_dev
*dev
, gfp_t gfp_flags
)
2131 DEBUG (dev
, "%s\n", __FUNCTION__
);
2133 /* fill the rx queue */
2134 rx_fill (dev
, gfp_flags
);
2136 /* and open the tx floodgates */
2137 atomic_set (&dev
->tx_qlen
, 0);
2138 netif_wake_queue (dev
->net
);
2139 if (rndis_active(dev
)) {
2140 rndis_set_param_medium (dev
->rndis_config
,
2142 BITRATE(dev
->gadget
)/100);
2143 (void) rndis_signal_connect (dev
->rndis_config
);
2147 static int eth_open (struct net_device
*net
)
2149 struct eth_dev
*dev
= netdev_priv(net
);
2151 DEBUG (dev
, "%s\n", __FUNCTION__
);
2152 if (netif_carrier_ok (dev
->net
))
2153 eth_start (dev
, GFP_KERNEL
);
2157 static int eth_stop (struct net_device
*net
)
2159 struct eth_dev
*dev
= netdev_priv(net
);
2161 VDEBUG (dev
, "%s\n", __FUNCTION__
);
2162 netif_stop_queue (net
);
2164 DEBUG (dev
, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
2165 dev
->stats
.rx_packets
, dev
->stats
.tx_packets
,
2166 dev
->stats
.rx_errors
, dev
->stats
.tx_errors
2169 /* ensure there are no more active requests */
2171 usb_ep_disable (dev
->in_ep
);
2172 usb_ep_disable (dev
->out_ep
);
2173 if (netif_carrier_ok (dev
->net
)) {
2174 DEBUG (dev
, "host still using in/out endpoints\n");
2175 // FIXME idiom may leave toggle wrong here
2176 usb_ep_enable (dev
->in_ep
, dev
->in
);
2177 usb_ep_enable (dev
->out_ep
, dev
->out
);
2179 if (dev
->status_ep
) {
2180 usb_ep_disable (dev
->status_ep
);
2181 usb_ep_enable (dev
->status_ep
, dev
->status
);
2185 if (rndis_active(dev
)) {
2186 rndis_set_param_medium (dev
->rndis_config
,
2187 NDIS_MEDIUM_802_3
, 0);
2188 (void) rndis_signal_disconnect (dev
->rndis_config
);
2194 /*-------------------------------------------------------------------------*/
2196 static struct usb_request
*
2197 eth_req_alloc (struct usb_ep
*ep
, unsigned size
, gfp_t gfp_flags
)
2199 struct usb_request
*req
;
2201 req
= usb_ep_alloc_request (ep
, gfp_flags
);
2205 req
->buf
= kmalloc (size
, gfp_flags
);
2207 usb_ep_free_request (ep
, req
);
2214 eth_req_free (struct usb_ep
*ep
, struct usb_request
*req
)
2217 usb_ep_free_request (ep
, req
);
2221 static void /* __init_or_exit */
2222 eth_unbind (struct usb_gadget
*gadget
)
2224 struct eth_dev
*dev
= get_gadget_data (gadget
);
2226 DEBUG (dev
, "unbind\n");
2227 rndis_deregister (dev
->rndis_config
);
2230 /* we've already been disconnected ... no i/o is active */
2232 eth_req_free (gadget
->ep0
, dev
->req
);
2235 if (dev
->stat_req
) {
2236 eth_req_free (dev
->status_ep
, dev
->stat_req
);
2237 dev
->stat_req
= NULL
;
2240 unregister_netdev (dev
->net
);
2241 free_netdev(dev
->net
);
2243 /* assuming we used keventd, it must quiesce too */
2244 flush_scheduled_work ();
2245 set_gadget_data (gadget
, NULL
);
2248 static u8 __devinit
nibble (unsigned char c
)
2250 if (likely (isdigit (c
)))
2253 if (likely (isxdigit (c
)))
2254 return 10 + c
- 'A';
2258 static int __devinit
get_ether_addr(const char *str
, u8
*dev_addr
)
2263 for (i
= 0; i
< 6; i
++) {
2266 if((*str
== '.') || (*str
== ':'))
2268 num
= nibble(*str
++) << 4;
2269 num
|= (nibble(*str
++));
2272 if (is_valid_ether_addr (dev_addr
))
2275 random_ether_addr(dev_addr
);
2279 static int __devinit
2280 eth_bind (struct usb_gadget
*gadget
)
2282 struct eth_dev
*dev
;
2283 struct net_device
*net
;
2284 u8 cdc
= 1, zlp
= 1, rndis
= 1;
2285 struct usb_ep
*in_ep
, *out_ep
, *status_ep
= NULL
;
2286 int status
= -ENOMEM
;
2289 /* these flags are only ever cleared; compiler take note */
2290 #ifndef DEV_CONFIG_CDC
2293 #ifndef CONFIG_USB_ETH_RNDIS
2297 /* Because most host side USB stacks handle CDC Ethernet, that
2298 * standard protocol is _strongly_ preferred for interop purposes.
2299 * (By everyone except Microsoft.)
2301 if (gadget_is_pxa (gadget
)) {
2302 /* pxa doesn't support altsettings */
2304 } else if (gadget_is_musbhdrc(gadget
)) {
2305 /* reduce tx dma overhead by avoiding special cases */
2307 } else if (gadget_is_sh(gadget
)) {
2308 /* sh doesn't support multiple interfaces or configs */
2311 } else if (gadget_is_sa1100 (gadget
)) {
2312 /* hardware can't write zlps */
2314 /* sa1100 CAN do CDC, without status endpoint ... we use
2315 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2320 gcnum
= usb_gadget_controller_number (gadget
);
2322 device_desc
.bcdDevice
= cpu_to_le16 (0x0200 + gcnum
);
2324 /* can't assume CDC works. don't want to default to
2325 * anything less functional on CDC-capable hardware,
2326 * so we fail in this case.
2328 dev_err (&gadget
->dev
,
2329 "controller '%s' not recognized\n",
2333 snprintf (manufacturer
, sizeof manufacturer
, "%s %s/%s",
2334 init_utsname()->sysname
, init_utsname()->release
,
2337 /* If there's an RNDIS configuration, that's what Windows wants to
2338 * be using ... so use these product IDs here and in the "linux.inf"
2339 * needed to install MSFT drivers. Current Linux kernels will use
2340 * the second configuration if it's CDC Ethernet, and need some help
2341 * to choose the right configuration otherwise.
2344 device_desc
.idVendor
=
2345 __constant_cpu_to_le16(RNDIS_VENDOR_NUM
);
2346 device_desc
.idProduct
=
2347 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM
);
2348 snprintf (product_desc
, sizeof product_desc
,
2349 "RNDIS/%s", driver_desc
);
2351 /* CDC subset ... recognized by Linux since 2.4.10, but Windows
2352 * drivers aren't widely available. (That may be improved by
2353 * supporting one submode of the "SAFE" variant of MDLM.)
2356 device_desc
.idVendor
=
2357 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM
);
2358 device_desc
.idProduct
=
2359 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM
);
2362 /* support optional vendor/distro customization */
2365 dev_err (&gadget
->dev
, "idVendor needs idProduct!\n");
2368 device_desc
.idVendor
= cpu_to_le16(idVendor
);
2369 device_desc
.idProduct
= cpu_to_le16(idProduct
);
2371 device_desc
.bcdDevice
= cpu_to_le16(bcdDevice
);
2374 strlcpy (manufacturer
, iManufacturer
, sizeof manufacturer
);
2376 strlcpy (product_desc
, iProduct
, sizeof product_desc
);
2377 if (iSerialNumber
) {
2378 device_desc
.iSerialNumber
= STRING_SERIALNUMBER
,
2379 strlcpy(serial_number
, iSerialNumber
, sizeof serial_number
);
2382 /* all we really need is bulk IN/OUT */
2383 usb_ep_autoconfig_reset (gadget
);
2384 in_ep
= usb_ep_autoconfig (gadget
, &fs_source_desc
);
2387 dev_err (&gadget
->dev
,
2388 "can't autoconfigure on %s\n",
2392 in_ep
->driver_data
= in_ep
; /* claim */
2394 out_ep
= usb_ep_autoconfig (gadget
, &fs_sink_desc
);
2397 out_ep
->driver_data
= out_ep
; /* claim */
2399 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2400 /* CDC Ethernet control interface doesn't require a status endpoint.
2401 * Since some hosts expect one, try to allocate one anyway.
2404 status_ep
= usb_ep_autoconfig (gadget
, &fs_status_desc
);
2406 status_ep
->driver_data
= status_ep
; /* claim */
2408 dev_err (&gadget
->dev
,
2409 "can't run RNDIS on %s\n",
2412 #ifdef DEV_CONFIG_CDC
2413 /* pxa25x only does CDC subset; often used with RNDIS */
2415 control_intf
.bNumEndpoints
= 0;
2416 /* FIXME remove endpoint from descriptor list */
2422 /* one config: cdc, else minimal subset */
2424 eth_config
.bNumInterfaces
= 1;
2425 eth_config
.iConfiguration
= STRING_SUBSET
;
2427 /* use functions to set these up, in case we're built to work
2428 * with multiple controllers and must override CDC Ethernet.
2430 fs_subset_descriptors();
2431 hs_subset_descriptors();
2434 device_desc
.bMaxPacketSize0
= gadget
->ep0
->maxpacket
;
2435 usb_gadget_set_selfpowered (gadget
);
2437 /* For now RNDIS is always a second config */
2439 device_desc
.bNumConfigurations
= 2;
2441 #ifdef CONFIG_USB_GADGET_DUALSPEED
2443 dev_qualifier
.bNumConfigurations
= 2;
2445 dev_qualifier
.bDeviceClass
= USB_CLASS_VENDOR_SPEC
;
2447 /* assumes ep0 uses the same value for both speeds ... */
2448 dev_qualifier
.bMaxPacketSize0
= device_desc
.bMaxPacketSize0
;
2450 /* and that all endpoints are dual-speed */
2451 hs_source_desc
.bEndpointAddress
= fs_source_desc
.bEndpointAddress
;
2452 hs_sink_desc
.bEndpointAddress
= fs_sink_desc
.bEndpointAddress
;
2453 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2455 hs_status_desc
.bEndpointAddress
=
2456 fs_status_desc
.bEndpointAddress
;
2458 #endif /* DUALSPEED */
2460 if (gadget
->is_otg
) {
2461 otg_descriptor
.bmAttributes
|= USB_OTG_HNP
,
2462 eth_config
.bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
2463 eth_config
.bMaxPower
= 4;
2464 #ifdef CONFIG_USB_ETH_RNDIS
2465 rndis_config
.bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
2466 rndis_config
.bMaxPower
= 4;
2470 net
= alloc_etherdev (sizeof *dev
);
2473 dev
= netdev_priv(net
);
2474 spin_lock_init (&dev
->lock
);
2475 spin_lock_init (&dev
->req_lock
);
2476 INIT_WORK (&dev
->work
, eth_work
);
2477 INIT_LIST_HEAD (&dev
->tx_reqs
);
2478 INIT_LIST_HEAD (&dev
->rx_reqs
);
2480 /* network device setup */
2482 SET_MODULE_OWNER (net
);
2483 strcpy (net
->name
, "usb%d");
2488 dev
->out_ep
= out_ep
;
2489 dev
->status_ep
= status_ep
;
2491 /* Module params for these addresses should come from ID proms.
2492 * The host side address is used with CDC and RNDIS, and commonly
2493 * ends up in a persistent config database. It's not clear if
2494 * host side code for the SAFE thing cares -- its original BLAN
2495 * thing didn't, Sharp never assigned those addresses on Zaurii.
2497 if (get_ether_addr(dev_addr
, net
->dev_addr
))
2498 dev_warn(&gadget
->dev
,
2499 "using random %s ethernet address\n", "self");
2500 if (get_ether_addr(host_addr
, dev
->host_mac
))
2501 dev_warn(&gadget
->dev
,
2502 "using random %s ethernet address\n", "host");
2503 snprintf (ethaddr
, sizeof ethaddr
, "%02X%02X%02X%02X%02X%02X",
2504 dev
->host_mac
[0], dev
->host_mac
[1],
2505 dev
->host_mac
[2], dev
->host_mac
[3],
2506 dev
->host_mac
[4], dev
->host_mac
[5]);
2509 status
= rndis_init();
2511 dev_err (&gadget
->dev
, "can't init RNDIS, %d\n",
2517 net
->change_mtu
= eth_change_mtu
;
2518 net
->get_stats
= eth_get_stats
;
2519 net
->hard_start_xmit
= eth_start_xmit
;
2520 net
->open
= eth_open
;
2521 net
->stop
= eth_stop
;
2522 // watchdog_timeo, tx_timeout ...
2523 // set_multicast_list
2524 SET_ETHTOOL_OPS(net
, &ops
);
2526 /* preallocate control message data and buffer */
2527 dev
->req
= eth_req_alloc (gadget
->ep0
, USB_BUFSIZ
, GFP_KERNEL
);
2530 dev
->req
->complete
= eth_setup_complete
;
2532 /* ... and maybe likewise for status transfer */
2533 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2534 if (dev
->status_ep
) {
2535 dev
->stat_req
= eth_req_alloc (dev
->status_ep
,
2536 STATUS_BYTECOUNT
, GFP_KERNEL
);
2537 if (!dev
->stat_req
) {
2538 eth_req_free (gadget
->ep0
, dev
->req
);
2541 dev
->stat_req
->context
= NULL
;
2545 /* finish hookup to lower layer ... */
2546 dev
->gadget
= gadget
;
2547 set_gadget_data (gadget
, dev
);
2548 gadget
->ep0
->driver_data
= dev
;
2550 /* two kinds of host-initiated state changes:
2551 * - iff DATA transfer is active, carrier is "on"
2552 * - tx queueing enabled if open *and* carrier is "on"
2554 netif_stop_queue (dev
->net
);
2555 netif_carrier_off (dev
->net
);
2557 SET_NETDEV_DEV (dev
->net
, &gadget
->dev
);
2558 status
= register_netdev (dev
->net
);
2562 INFO (dev
, "%s, version: " DRIVER_VERSION
"\n", driver_desc
);
2563 INFO (dev
, "using %s, OUT %s IN %s%s%s\n", gadget
->name
,
2564 out_ep
->name
, in_ep
->name
,
2565 status_ep
? " STATUS " : "",
2566 status_ep
? status_ep
->name
: ""
2568 INFO (dev
, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2569 net
->dev_addr
[0], net
->dev_addr
[1],
2570 net
->dev_addr
[2], net
->dev_addr
[3],
2571 net
->dev_addr
[4], net
->dev_addr
[5]);
2574 INFO (dev
, "HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2575 dev
->host_mac
[0], dev
->host_mac
[1],
2576 dev
->host_mac
[2], dev
->host_mac
[3],
2577 dev
->host_mac
[4], dev
->host_mac
[5]);
2582 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2584 dev
->rndis_config
= rndis_register (rndis_control_ack
);
2585 if (dev
->rndis_config
< 0) {
2587 unregister_netdev (dev
->net
);
2592 /* these set up a lot of the OIDs that RNDIS needs */
2593 rndis_set_host_mac (dev
->rndis_config
, dev
->host_mac
);
2594 if (rndis_set_param_dev (dev
->rndis_config
, dev
->net
,
2595 &dev
->stats
, &dev
->cdc_filter
))
2597 if (rndis_set_param_vendor (dev
->rndis_config
, vendorID
,
2600 if (rndis_set_param_medium (dev
->rndis_config
,
2604 INFO (dev
, "RNDIS ready\n");
2610 dev_dbg(&gadget
->dev
, "register_netdev failed, %d\n", status
);
2612 eth_unbind (gadget
);
2616 /*-------------------------------------------------------------------------*/
2619 eth_suspend (struct usb_gadget
*gadget
)
2621 struct eth_dev
*dev
= get_gadget_data (gadget
);
2623 DEBUG (dev
, "suspend\n");
2628 eth_resume (struct usb_gadget
*gadget
)
2630 struct eth_dev
*dev
= get_gadget_data (gadget
);
2632 DEBUG (dev
, "resume\n");
2636 /*-------------------------------------------------------------------------*/
2638 static struct usb_gadget_driver eth_driver
= {
2641 .function
= (char *) driver_desc
,
2643 .unbind
= eth_unbind
,
2646 .disconnect
= eth_disconnect
,
2648 .suspend
= eth_suspend
,
2649 .resume
= eth_resume
,
2652 .name
= (char *) shortname
,
2653 .owner
= THIS_MODULE
,
2657 MODULE_DESCRIPTION (DRIVER_DESC
);
2658 MODULE_AUTHOR ("David Brownell, Benedikt Spanger");
2659 MODULE_LICENSE ("GPL");
2662 static int __init
init (void)
2664 return usb_gadget_register_driver (ð_driver
);
2668 static void __exit
cleanup (void)
2670 usb_gadget_unregister_driver (ð_driver
);
2672 module_exit (cleanup
);