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/smp_lock.h>
32 #include <linux/errno.h>
33 #include <linux/init.h>
34 #include <linux/timer.h>
35 #include <linux/list.h>
36 #include <linux/interrupt.h>
37 #include <linux/utsname.h>
38 #include <linux/device.h>
39 #include <linux/moduleparam.h>
40 #include <linux/ctype.h>
42 #include <asm/byteorder.h>
45 #include <asm/system.h>
46 #include <asm/uaccess.h>
47 #include <asm/unaligned.h>
49 #include <linux/usb/ch9.h>
50 #include <linux/usb/cdc.h>
51 #include <linux/usb_gadget.h>
53 #include <linux/random.h>
54 #include <linux/netdevice.h>
55 #include <linux/etherdevice.h>
56 #include <linux/ethtool.h>
58 #include "gadget_chips.h"
60 /*-------------------------------------------------------------------------*/
63 * Ethernet gadget driver -- with CDC and non-CDC options
64 * Builds on hardware support for a full duplex link.
66 * CDC Ethernet is the standard USB solution for sending Ethernet frames
67 * using USB. Real hardware tends to use the same framing protocol but look
68 * different for control features. This driver strongly prefers to use
69 * this USB-IF standard as its open-systems interoperability solution;
70 * most host side USB stacks (except from Microsoft) support it.
72 * There's some hardware that can't talk CDC. We make that hardware
73 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
74 * link-level setup only requires activating the configuration. Only the
75 * endpoint descriptors, and product/vendor IDs, are relevant; no control
76 * operations are available. Linux supports it, but other host operating
77 * systems may not. (This is a subset of CDC Ethernet.)
79 * It turns out that if you add a few descriptors to that "CDC Subset",
80 * (Windows) host side drivers from MCCI can treat it as one submode of
81 * a proprietary scheme called "SAFE" ... without needing to know about
82 * specific product/vendor IDs. So we do that, making it easier to use
83 * those MS-Windows drivers. Those added descriptors make it resemble a
84 * CDC MDLM device, but they don't change device behavior at all. (See
85 * MCCI Engineering report 950198 "SAFE Networking Functions".)
87 * A third option is also in use. Rather than CDC Ethernet, or something
88 * simpler, Microsoft pushes their own approach: RNDIS. The published
89 * RNDIS specs are ambiguous and appear to be incomplete, and are also
93 #define DRIVER_DESC "Ethernet Gadget"
94 #define DRIVER_VERSION "May Day 2005"
96 static const char shortname
[] = "ether";
97 static const char driver_desc
[] = DRIVER_DESC
;
99 #define RX_EXTRA 20 /* guard against rx overflows */
103 #ifndef CONFIG_USB_ETH_RNDIS
104 #define rndis_uninit(x) do{}while(0)
105 #define rndis_deregister(c) do{}while(0)
106 #define rndis_exit() do{}while(0)
109 /* CDC and RNDIS support the same host-chosen outgoing packet filters. */
110 #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
111 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
112 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
113 |USB_CDC_PACKET_TYPE_DIRECTED)
116 /*-------------------------------------------------------------------------*/
120 struct usb_gadget
*gadget
;
121 struct usb_request
*req
; /* for control responses */
122 struct usb_request
*stat_req
; /* for cdc & rndis status */
125 struct usb_ep
*in_ep
, *out_ep
, *status_ep
;
126 const struct usb_endpoint_descriptor
130 struct list_head tx_reqs
, rx_reqs
;
132 struct net_device
*net
;
133 struct net_device_stats stats
;
136 struct work_struct work
;
140 unsigned suspended
:1;
143 #define WORK_RX_MEMORY 0
145 u8 host_mac
[ETH_ALEN
];
148 /* This version autoconfigures as much as possible at run-time.
150 * It also ASSUMES a self-powered device, without remote wakeup,
151 * although remote wakeup support would make sense.
154 /*-------------------------------------------------------------------------*/
156 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
157 * Instead: allocate your own, using normal USB-IF procedures.
160 /* Thanks to NetChip Technologies for donating this product ID.
161 * It's for devices with only CDC Ethernet configurations.
163 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
164 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
166 /* For hardware that can't talk CDC, we use the same vendor ID that
167 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
168 * with pxa250. We're protocol-compatible, if the host-side drivers
169 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
170 * drivers that need to hard-wire endpoint numbers have a hook.
172 * The protocol is a minimal subset of CDC Ether, which works on any bulk
173 * hardware that's not deeply broken ... even on hardware that can't talk
174 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
175 * doesn't handle control-OUT).
177 #define SIMPLE_VENDOR_NUM 0x049f
178 #define SIMPLE_PRODUCT_NUM 0x505a
180 /* For hardware that can talk RNDIS and either of the above protocols,
181 * use this ID ... the windows INF files will know it. Unless it's
182 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
183 * the non-RNDIS configuration.
185 #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
186 #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
189 /* Some systems will want different product identifers published in the
190 * device descriptor, either numbers or strings or both. These string
191 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
194 static ushort idVendor
;
195 module_param(idVendor
, ushort
, S_IRUGO
);
196 MODULE_PARM_DESC(idVendor
, "USB Vendor ID");
198 static ushort idProduct
;
199 module_param(idProduct
, ushort
, S_IRUGO
);
200 MODULE_PARM_DESC(idProduct
, "USB Product ID");
202 static ushort bcdDevice
;
203 module_param(bcdDevice
, ushort
, S_IRUGO
);
204 MODULE_PARM_DESC(bcdDevice
, "USB Device version (BCD)");
206 static char *iManufacturer
;
207 module_param(iManufacturer
, charp
, S_IRUGO
);
208 MODULE_PARM_DESC(iManufacturer
, "USB Manufacturer string");
210 static char *iProduct
;
211 module_param(iProduct
, charp
, S_IRUGO
);
212 MODULE_PARM_DESC(iProduct
, "USB Product string");
214 static char *iSerialNumber
;
215 module_param(iSerialNumber
, charp
, S_IRUGO
);
216 MODULE_PARM_DESC(iSerialNumber
, "SerialNumber");
218 /* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */
219 static char *dev_addr
;
220 module_param(dev_addr
, charp
, S_IRUGO
);
221 MODULE_PARM_DESC(dev_addr
, "Device Ethernet Address");
223 /* this address is invisible to ifconfig */
224 static char *host_addr
;
225 module_param(host_addr
, charp
, S_IRUGO
);
226 MODULE_PARM_DESC(host_addr
, "Host Ethernet Address");
229 /*-------------------------------------------------------------------------*/
231 /* Include CDC support if we could run on CDC-capable hardware. */
233 #ifdef CONFIG_USB_GADGET_NET2280
234 #define DEV_CONFIG_CDC
237 #ifdef CONFIG_USB_GADGET_DUMMY_HCD
238 #define DEV_CONFIG_CDC
241 #ifdef CONFIG_USB_GADGET_GOKU
242 #define DEV_CONFIG_CDC
245 #ifdef CONFIG_USB_GADGET_LH7A40X
246 #define DEV_CONFIG_CDC
249 #ifdef CONFIG_USB_GADGET_MQ11XX
250 #define DEV_CONFIG_CDC
253 #ifdef CONFIG_USB_GADGET_OMAP
254 #define DEV_CONFIG_CDC
257 #ifdef CONFIG_USB_GADGET_N9604
258 #define DEV_CONFIG_CDC
261 #ifdef CONFIG_USB_GADGET_PXA27X
262 #define DEV_CONFIG_CDC
265 #ifdef CONFIG_USB_GADGET_S3C2410
266 #define DEV_CONFIG_CDC
269 #ifdef CONFIG_USB_GADGET_AT91
270 #define DEV_CONFIG_CDC
273 #ifdef CONFIG_USB_GADGET_MUSBHSFC
274 #define DEV_CONFIG_CDC
277 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
278 #define DEV_CONFIG_CDC
281 #ifdef CONFIG_USB_GADGET_HUSB2DEV
282 #define DEV_CONFIG_CDC
286 /* For CDC-incapable hardware, choose the simple cdc subset.
287 * Anything that talks bulk (without notable bugs) can do this.
289 #ifdef CONFIG_USB_GADGET_PXA2XX
290 #define DEV_CONFIG_SUBSET
293 #ifdef CONFIG_USB_GADGET_SH
294 #define DEV_CONFIG_SUBSET
297 #ifdef CONFIG_USB_GADGET_SA1100
298 /* use non-CDC for backwards compatibility */
299 #define DEV_CONFIG_SUBSET
303 /*-------------------------------------------------------------------------*/
305 /* "main" config is either CDC, or its simple subset */
306 static inline int is_cdc(struct eth_dev
*dev
)
308 #if !defined(DEV_CONFIG_SUBSET)
309 return 1; /* only cdc possible */
310 #elif !defined (DEV_CONFIG_CDC)
311 return 0; /* only subset possible */
313 return dev
->cdc
; /* depends on what hardware we found */
317 /* "secondary" RNDIS config may sometimes be activated */
318 static inline int rndis_active(struct eth_dev
*dev
)
320 #ifdef CONFIG_USB_ETH_RNDIS
327 #define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
328 #define cdc_active(dev) ( is_cdc(dev) && !rndis_active(dev))
332 #define DEFAULT_QLEN 2 /* double buffering by default */
334 /* peak bulk transfer bits-per-second */
335 #define HS_BPS (13 * 512 * 8 * 1000 * 8)
336 #define FS_BPS (19 * 64 * 1 * 1000 * 8)
338 #ifdef CONFIG_USB_GADGET_DUALSPEED
339 #define DEVSPEED USB_SPEED_HIGH
341 static unsigned qmult
= 5;
342 module_param (qmult
, uint
, S_IRUGO
|S_IWUSR
);
345 /* for dual-speed hardware, use deeper queues at highspeed */
346 #define qlen(gadget) \
347 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
349 /* also defer IRQs on highspeed TX */
350 #define TX_DELAY qmult
352 static inline int BITRATE(struct usb_gadget
*g
)
354 return (g
->speed
== USB_SPEED_HIGH
) ? HS_BPS
: FS_BPS
;
357 #else /* full speed (low speed doesn't do bulk) */
358 #define DEVSPEED USB_SPEED_FULL
360 #define qlen(gadget) DEFAULT_QLEN
362 static inline int BITRATE(struct usb_gadget
*g
)
369 /*-------------------------------------------------------------------------*/
371 #define xprintk(d,level,fmt,args...) \
372 printk(level "%s: " fmt , (d)->net->name , ## args)
376 #define DEBUG(dev,fmt,args...) \
377 xprintk(dev , KERN_DEBUG , fmt , ## args)
379 #define DEBUG(dev,fmt,args...) \
386 #define VDEBUG(dev,fmt,args...) \
390 #define ERROR(dev,fmt,args...) \
391 xprintk(dev , KERN_ERR , fmt , ## args)
392 #define WARN(dev,fmt,args...) \
393 xprintk(dev , KERN_WARNING , fmt , ## args)
394 #define INFO(dev,fmt,args...) \
395 xprintk(dev , KERN_INFO , fmt , ## args)
397 /*-------------------------------------------------------------------------*/
399 /* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
400 * ep0 implementation: descriptors, config management, setup().
401 * also optional class-specific notification interrupt transfer.
405 * DESCRIPTORS ... most are static, but strings and (full) configuration
406 * descriptors are built on demand. For now we do either full CDC, or
407 * our simple subset, with RNDIS as an optional second configuration.
409 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
410 * the class descriptors match a modem (they're ignored; it's really just
411 * Ethernet functionality), they don't need the NOP altsetting, and the
412 * status transfer endpoint isn't optional.
415 #define STRING_MANUFACTURER 1
416 #define STRING_PRODUCT 2
417 #define STRING_ETHADDR 3
418 #define STRING_DATA 4
419 #define STRING_CONTROL 5
420 #define STRING_RNDIS_CONTROL 6
422 #define STRING_SUBSET 8
423 #define STRING_RNDIS 9
424 #define STRING_SERIALNUMBER 10
426 /* holds our biggest descriptor (or RNDIS response) */
427 #define USB_BUFSIZ 256
430 * This device advertises one configuration, eth_config, unless RNDIS
431 * is enabled (rndis_config) on hardware supporting at least two configs.
433 * NOTE: Controllers like superh_udc should probably be able to use
434 * an RNDIS-only configuration.
436 * FIXME define some higher-powered configurations to make it easier
437 * to recharge batteries ...
440 #define DEV_CONFIG_VALUE 1 /* cdc or subset */
441 #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
443 static struct usb_device_descriptor
445 .bLength
= sizeof device_desc
,
446 .bDescriptorType
= USB_DT_DEVICE
,
448 .bcdUSB
= __constant_cpu_to_le16 (0x0200),
450 .bDeviceClass
= USB_CLASS_COMM
,
451 .bDeviceSubClass
= 0,
452 .bDeviceProtocol
= 0,
454 .idVendor
= __constant_cpu_to_le16 (CDC_VENDOR_NUM
),
455 .idProduct
= __constant_cpu_to_le16 (CDC_PRODUCT_NUM
),
456 .iManufacturer
= STRING_MANUFACTURER
,
457 .iProduct
= STRING_PRODUCT
,
458 .bNumConfigurations
= 1,
461 static struct usb_otg_descriptor
463 .bLength
= sizeof otg_descriptor
,
464 .bDescriptorType
= USB_DT_OTG
,
466 .bmAttributes
= USB_OTG_SRP
,
469 static struct usb_config_descriptor
471 .bLength
= sizeof eth_config
,
472 .bDescriptorType
= USB_DT_CONFIG
,
474 /* compute wTotalLength on the fly */
476 .bConfigurationValue
= DEV_CONFIG_VALUE
,
477 .iConfiguration
= STRING_CDC
,
478 .bmAttributes
= USB_CONFIG_ATT_ONE
| USB_CONFIG_ATT_SELFPOWER
,
482 #ifdef CONFIG_USB_ETH_RNDIS
483 static struct usb_config_descriptor
485 .bLength
= sizeof rndis_config
,
486 .bDescriptorType
= USB_DT_CONFIG
,
488 /* compute wTotalLength on the fly */
490 .bConfigurationValue
= DEV_RNDIS_CONFIG_VALUE
,
491 .iConfiguration
= STRING_RNDIS
,
492 .bmAttributes
= USB_CONFIG_ATT_ONE
| USB_CONFIG_ATT_SELFPOWER
,
498 * Compared to the simple CDC subset, the full CDC Ethernet model adds
499 * three class descriptors, two interface descriptors, optional status
500 * endpoint. Both have a "data" interface and two bulk endpoints.
501 * There are also differences in how control requests are handled.
503 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
504 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
505 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
506 * work around those bugs) are unlikely to ever come from MSFT, you may
507 * wish to avoid using RNDIS.
509 * MCCI offers an alternative to RNDIS if you need to connect to Windows
510 * but have hardware that can't support CDC Ethernet. We add descriptors
511 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
512 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
513 * get those drivers from MCCI, or bundled with various products.
516 #ifdef DEV_CONFIG_CDC
517 static struct usb_interface_descriptor
519 .bLength
= sizeof control_intf
,
520 .bDescriptorType
= USB_DT_INTERFACE
,
522 .bInterfaceNumber
= 0,
523 /* status endpoint is optional; this may be patched later */
525 .bInterfaceClass
= USB_CLASS_COMM
,
526 .bInterfaceSubClass
= USB_CDC_SUBCLASS_ETHERNET
,
527 .bInterfaceProtocol
= USB_CDC_PROTO_NONE
,
528 .iInterface
= STRING_CONTROL
,
532 #ifdef CONFIG_USB_ETH_RNDIS
533 static const struct usb_interface_descriptor
534 rndis_control_intf
= {
535 .bLength
= sizeof rndis_control_intf
,
536 .bDescriptorType
= USB_DT_INTERFACE
,
538 .bInterfaceNumber
= 0,
540 .bInterfaceClass
= USB_CLASS_COMM
,
541 .bInterfaceSubClass
= USB_CDC_SUBCLASS_ACM
,
542 .bInterfaceProtocol
= USB_CDC_ACM_PROTO_VENDOR
,
543 .iInterface
= STRING_RNDIS_CONTROL
,
547 static const struct usb_cdc_header_desc header_desc
= {
548 .bLength
= sizeof header_desc
,
549 .bDescriptorType
= USB_DT_CS_INTERFACE
,
550 .bDescriptorSubType
= USB_CDC_HEADER_TYPE
,
552 .bcdCDC
= __constant_cpu_to_le16 (0x0110),
555 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
557 static const struct usb_cdc_union_desc union_desc
= {
558 .bLength
= sizeof union_desc
,
559 .bDescriptorType
= USB_DT_CS_INTERFACE
,
560 .bDescriptorSubType
= USB_CDC_UNION_TYPE
,
562 .bMasterInterface0
= 0, /* index of control interface */
563 .bSlaveInterface0
= 1, /* index of DATA interface */
566 #endif /* CDC || RNDIS */
568 #ifdef CONFIG_USB_ETH_RNDIS
570 static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor
= {
571 .bLength
= sizeof call_mgmt_descriptor
,
572 .bDescriptorType
= USB_DT_CS_INTERFACE
,
573 .bDescriptorSubType
= USB_CDC_CALL_MANAGEMENT_TYPE
,
575 .bmCapabilities
= 0x00,
576 .bDataInterface
= 0x01,
579 static const struct usb_cdc_acm_descriptor acm_descriptor
= {
580 .bLength
= sizeof acm_descriptor
,
581 .bDescriptorType
= USB_DT_CS_INTERFACE
,
582 .bDescriptorSubType
= USB_CDC_ACM_TYPE
,
584 .bmCapabilities
= 0x00,
589 #ifndef DEV_CONFIG_CDC
591 /* "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
592 * ways: data endpoints live in the control interface, there's no data
593 * interface, and it's not used to talk to a cell phone radio.
596 static const struct usb_cdc_mdlm_desc mdlm_desc
= {
597 .bLength
= sizeof mdlm_desc
,
598 .bDescriptorType
= USB_DT_CS_INTERFACE
,
599 .bDescriptorSubType
= USB_CDC_MDLM_TYPE
,
601 .bcdVersion
= __constant_cpu_to_le16(0x0100),
603 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
604 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
608 /* since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
609 * can't really use its struct. All we do here is say that we're using
610 * the submode of "SAFE" which directly matches the CDC Subset.
612 static const u8 mdlm_detail_desc
[] = {
615 USB_CDC_MDLM_DETAIL_TYPE
,
618 0, /* network control capabilities (none) */
619 0, /* network data capabilities ("raw" encapsulation) */
624 static const struct usb_cdc_ether_desc ether_desc
= {
625 .bLength
= sizeof ether_desc
,
626 .bDescriptorType
= USB_DT_CS_INTERFACE
,
627 .bDescriptorSubType
= USB_CDC_ETHERNET_TYPE
,
629 /* this descriptor actually adds value, surprise! */
630 .iMACAddress
= STRING_ETHADDR
,
631 .bmEthernetStatistics
= __constant_cpu_to_le32 (0), /* no statistics */
632 .wMaxSegmentSize
= __constant_cpu_to_le16 (ETH_FRAME_LEN
),
633 .wNumberMCFilters
= __constant_cpu_to_le16 (0),
634 .bNumberPowerFilters
= 0,
638 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
640 /* include the status endpoint if we can, even where it's optional.
641 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
642 * packet, to simplify cancellation; and a big transfer interval, to
643 * waste less bandwidth.
645 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
646 * if they ignore the connect/disconnect notifications that real aether
647 * can provide. more advanced cdc configurations might want to support
648 * encapsulated commands (vendor-specific, using control-OUT).
650 * RNDIS requires the status endpoint, since it uses that encapsulation
651 * mechanism for its funky RPC scheme.
654 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
655 #define STATUS_BYTECOUNT 16 /* 8 byte header + data */
657 static struct usb_endpoint_descriptor
659 .bLength
= USB_DT_ENDPOINT_SIZE
,
660 .bDescriptorType
= USB_DT_ENDPOINT
,
662 .bEndpointAddress
= USB_DIR_IN
,
663 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
664 .wMaxPacketSize
= __constant_cpu_to_le16 (STATUS_BYTECOUNT
),
665 .bInterval
= 1 << LOG2_STATUS_INTERVAL_MSEC
,
669 #ifdef DEV_CONFIG_CDC
671 /* the default data interface has no endpoints ... */
673 static const struct usb_interface_descriptor
675 .bLength
= sizeof data_nop_intf
,
676 .bDescriptorType
= USB_DT_INTERFACE
,
678 .bInterfaceNumber
= 1,
679 .bAlternateSetting
= 0,
681 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
682 .bInterfaceSubClass
= 0,
683 .bInterfaceProtocol
= 0,
686 /* ... but the "real" data interface has two bulk endpoints */
688 static const struct usb_interface_descriptor
690 .bLength
= sizeof data_intf
,
691 .bDescriptorType
= USB_DT_INTERFACE
,
693 .bInterfaceNumber
= 1,
694 .bAlternateSetting
= 1,
696 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
697 .bInterfaceSubClass
= 0,
698 .bInterfaceProtocol
= 0,
699 .iInterface
= STRING_DATA
,
704 #ifdef CONFIG_USB_ETH_RNDIS
706 /* RNDIS doesn't activate by changing to the "real" altsetting */
708 static const struct usb_interface_descriptor
710 .bLength
= sizeof rndis_data_intf
,
711 .bDescriptorType
= USB_DT_INTERFACE
,
713 .bInterfaceNumber
= 1,
714 .bAlternateSetting
= 0,
716 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
717 .bInterfaceSubClass
= 0,
718 .bInterfaceProtocol
= 0,
719 .iInterface
= STRING_DATA
,
724 #ifdef DEV_CONFIG_SUBSET
727 * "Simple" CDC-subset option is a simple vendor-neutral model that most
728 * full speed controllers can handle: one interface, two bulk endpoints.
730 * To assist host side drivers, we fancy it up a bit, and add descriptors
731 * so some host side drivers will understand it as a "SAFE" variant.
734 static const struct usb_interface_descriptor
736 .bLength
= sizeof subset_data_intf
,
737 .bDescriptorType
= USB_DT_INTERFACE
,
739 .bInterfaceNumber
= 0,
740 .bAlternateSetting
= 0,
742 .bInterfaceClass
= USB_CLASS_COMM
,
743 .bInterfaceSubClass
= USB_CDC_SUBCLASS_MDLM
,
744 .bInterfaceProtocol
= 0,
745 .iInterface
= STRING_DATA
,
751 static struct usb_endpoint_descriptor
753 .bLength
= USB_DT_ENDPOINT_SIZE
,
754 .bDescriptorType
= USB_DT_ENDPOINT
,
756 .bEndpointAddress
= USB_DIR_IN
,
757 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
760 static struct usb_endpoint_descriptor
762 .bLength
= USB_DT_ENDPOINT_SIZE
,
763 .bDescriptorType
= USB_DT_ENDPOINT
,
765 .bEndpointAddress
= USB_DIR_OUT
,
766 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
769 static const struct usb_descriptor_header
*fs_eth_function
[11] = {
770 (struct usb_descriptor_header
*) &otg_descriptor
,
771 #ifdef DEV_CONFIG_CDC
772 /* "cdc" mode descriptors */
773 (struct usb_descriptor_header
*) &control_intf
,
774 (struct usb_descriptor_header
*) &header_desc
,
775 (struct usb_descriptor_header
*) &union_desc
,
776 (struct usb_descriptor_header
*) ðer_desc
,
777 /* NOTE: status endpoint may need to be removed */
778 (struct usb_descriptor_header
*) &fs_status_desc
,
779 /* data interface, with altsetting */
780 (struct usb_descriptor_header
*) &data_nop_intf
,
781 (struct usb_descriptor_header
*) &data_intf
,
782 (struct usb_descriptor_header
*) &fs_source_desc
,
783 (struct usb_descriptor_header
*) &fs_sink_desc
,
785 #endif /* DEV_CONFIG_CDC */
788 static inline void __init
fs_subset_descriptors(void)
790 #ifdef DEV_CONFIG_SUBSET
791 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
792 fs_eth_function
[1] = (struct usb_descriptor_header
*) &subset_data_intf
;
793 fs_eth_function
[2] = (struct usb_descriptor_header
*) &header_desc
;
794 fs_eth_function
[3] = (struct usb_descriptor_header
*) &mdlm_desc
;
795 fs_eth_function
[4] = (struct usb_descriptor_header
*) &mdlm_detail_desc
;
796 fs_eth_function
[5] = (struct usb_descriptor_header
*) ðer_desc
;
797 fs_eth_function
[6] = (struct usb_descriptor_header
*) &fs_source_desc
;
798 fs_eth_function
[7] = (struct usb_descriptor_header
*) &fs_sink_desc
;
799 fs_eth_function
[8] = NULL
;
801 fs_eth_function
[1] = NULL
;
805 #ifdef CONFIG_USB_ETH_RNDIS
806 static const struct usb_descriptor_header
*fs_rndis_function
[] = {
807 (struct usb_descriptor_header
*) &otg_descriptor
,
808 /* control interface matches ACM, not Ethernet */
809 (struct usb_descriptor_header
*) &rndis_control_intf
,
810 (struct usb_descriptor_header
*) &header_desc
,
811 (struct usb_descriptor_header
*) &call_mgmt_descriptor
,
812 (struct usb_descriptor_header
*) &acm_descriptor
,
813 (struct usb_descriptor_header
*) &union_desc
,
814 (struct usb_descriptor_header
*) &fs_status_desc
,
815 /* data interface has no altsetting */
816 (struct usb_descriptor_header
*) &rndis_data_intf
,
817 (struct usb_descriptor_header
*) &fs_source_desc
,
818 (struct usb_descriptor_header
*) &fs_sink_desc
,
823 #ifdef CONFIG_USB_GADGET_DUALSPEED
826 * usb 2.0 devices need to expose both high speed and full speed
827 * descriptors, unless they only run at full speed.
830 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
831 static struct usb_endpoint_descriptor
833 .bLength
= USB_DT_ENDPOINT_SIZE
,
834 .bDescriptorType
= USB_DT_ENDPOINT
,
836 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
837 .wMaxPacketSize
= __constant_cpu_to_le16 (STATUS_BYTECOUNT
),
838 .bInterval
= LOG2_STATUS_INTERVAL_MSEC
+ 4,
840 #endif /* DEV_CONFIG_CDC */
842 static struct usb_endpoint_descriptor
844 .bLength
= USB_DT_ENDPOINT_SIZE
,
845 .bDescriptorType
= USB_DT_ENDPOINT
,
847 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
848 .wMaxPacketSize
= __constant_cpu_to_le16 (512),
851 static struct usb_endpoint_descriptor
853 .bLength
= USB_DT_ENDPOINT_SIZE
,
854 .bDescriptorType
= USB_DT_ENDPOINT
,
856 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
857 .wMaxPacketSize
= __constant_cpu_to_le16 (512),
860 static struct usb_qualifier_descriptor
862 .bLength
= sizeof dev_qualifier
,
863 .bDescriptorType
= USB_DT_DEVICE_QUALIFIER
,
865 .bcdUSB
= __constant_cpu_to_le16 (0x0200),
866 .bDeviceClass
= USB_CLASS_COMM
,
868 .bNumConfigurations
= 1,
871 static const struct usb_descriptor_header
*hs_eth_function
[11] = {
872 (struct usb_descriptor_header
*) &otg_descriptor
,
873 #ifdef DEV_CONFIG_CDC
874 /* "cdc" mode descriptors */
875 (struct usb_descriptor_header
*) &control_intf
,
876 (struct usb_descriptor_header
*) &header_desc
,
877 (struct usb_descriptor_header
*) &union_desc
,
878 (struct usb_descriptor_header
*) ðer_desc
,
879 /* NOTE: status endpoint may need to be removed */
880 (struct usb_descriptor_header
*) &hs_status_desc
,
881 /* data interface, with altsetting */
882 (struct usb_descriptor_header
*) &data_nop_intf
,
883 (struct usb_descriptor_header
*) &data_intf
,
884 (struct usb_descriptor_header
*) &hs_source_desc
,
885 (struct usb_descriptor_header
*) &hs_sink_desc
,
887 #endif /* DEV_CONFIG_CDC */
890 static inline void __init
hs_subset_descriptors(void)
892 #ifdef DEV_CONFIG_SUBSET
893 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
894 hs_eth_function
[1] = (struct usb_descriptor_header
*) &subset_data_intf
;
895 hs_eth_function
[2] = (struct usb_descriptor_header
*) &header_desc
;
896 hs_eth_function
[3] = (struct usb_descriptor_header
*) &mdlm_desc
;
897 hs_eth_function
[4] = (struct usb_descriptor_header
*) &mdlm_detail_desc
;
898 hs_eth_function
[5] = (struct usb_descriptor_header
*) ðer_desc
;
899 hs_eth_function
[6] = (struct usb_descriptor_header
*) &hs_source_desc
;
900 hs_eth_function
[7] = (struct usb_descriptor_header
*) &hs_sink_desc
;
901 hs_eth_function
[8] = NULL
;
903 hs_eth_function
[1] = NULL
;
907 #ifdef CONFIG_USB_ETH_RNDIS
908 static const struct usb_descriptor_header
*hs_rndis_function
[] = {
909 (struct usb_descriptor_header
*) &otg_descriptor
,
910 /* control interface matches ACM, not Ethernet */
911 (struct usb_descriptor_header
*) &rndis_control_intf
,
912 (struct usb_descriptor_header
*) &header_desc
,
913 (struct usb_descriptor_header
*) &call_mgmt_descriptor
,
914 (struct usb_descriptor_header
*) &acm_descriptor
,
915 (struct usb_descriptor_header
*) &union_desc
,
916 (struct usb_descriptor_header
*) &hs_status_desc
,
917 /* data interface has no altsetting */
918 (struct usb_descriptor_header
*) &rndis_data_intf
,
919 (struct usb_descriptor_header
*) &hs_source_desc
,
920 (struct usb_descriptor_header
*) &hs_sink_desc
,
926 /* maxpacket and other transfer characteristics vary by speed. */
927 #define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
931 /* if there's no high speed support, maxpacket doesn't change. */
932 #define ep_desc(g,hs,fs) (((void)(g)), (fs))
934 static inline void __init
hs_subset_descriptors(void)
938 #endif /* !CONFIG_USB_GADGET_DUALSPEED */
940 /*-------------------------------------------------------------------------*/
942 /* descriptors that are built on-demand */
944 static char manufacturer
[50];
945 static char product_desc
[40] = DRIVER_DESC
;
946 static char serial_number
[20];
948 /* address that the host will use ... usually assigned at random */
949 static char ethaddr
[2 * ETH_ALEN
+ 1];
951 /* static strings, in UTF-8 */
952 static struct usb_string strings
[] = {
953 { STRING_MANUFACTURER
, manufacturer
, },
954 { STRING_PRODUCT
, product_desc
, },
955 { STRING_SERIALNUMBER
, serial_number
, },
956 { STRING_DATA
, "Ethernet Data", },
957 { STRING_ETHADDR
, ethaddr
, },
958 #ifdef DEV_CONFIG_CDC
959 { STRING_CDC
, "CDC Ethernet", },
960 { STRING_CONTROL
, "CDC Communications Control", },
962 #ifdef DEV_CONFIG_SUBSET
963 { STRING_SUBSET
, "CDC Ethernet Subset", },
965 #ifdef CONFIG_USB_ETH_RNDIS
966 { STRING_RNDIS
, "RNDIS", },
967 { STRING_RNDIS_CONTROL
, "RNDIS Communications Control", },
969 { } /* end of list */
972 static struct usb_gadget_strings stringtab
= {
973 .language
= 0x0409, /* en-us */
978 * one config, two interfaces: control, data.
979 * complications: class descriptors, and an altsetting.
982 config_buf (enum usb_device_speed speed
,
984 unsigned index
, int is_otg
)
987 const struct usb_config_descriptor
*config
;
988 const struct usb_descriptor_header
**function
;
989 #ifdef CONFIG_USB_GADGET_DUALSPEED
990 int hs
= (speed
== USB_SPEED_HIGH
);
992 if (type
== USB_DT_OTHER_SPEED_CONFIG
)
994 #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
996 #define which_fn(t) (fs_ ## t ## _function)
999 if (index
>= device_desc
.bNumConfigurations
)
1002 #ifdef CONFIG_USB_ETH_RNDIS
1003 /* list the RNDIS config first, to make Microsoft's drivers
1004 * happy. DOCSIS 1.0 needs this too.
1006 if (device_desc
.bNumConfigurations
== 2 && index
== 0) {
1007 config
= &rndis_config
;
1008 function
= which_fn (rndis
);
1012 config
= ð_config
;
1013 function
= which_fn (eth
);
1016 /* for now, don't advertise srp-only devices */
1020 len
= usb_gadget_config_buf (config
, buf
, USB_BUFSIZ
, function
);
1023 ((struct usb_config_descriptor
*) buf
)->bDescriptorType
= type
;
1027 /*-------------------------------------------------------------------------*/
1029 static void eth_start (struct eth_dev
*dev
, gfp_t gfp_flags
);
1030 static int alloc_requests (struct eth_dev
*dev
, unsigned n
, gfp_t gfp_flags
);
1033 set_ether_config (struct eth_dev
*dev
, gfp_t gfp_flags
)
1036 struct usb_gadget
*gadget
= dev
->gadget
;
1038 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
1039 /* status endpoint used for RNDIS and (optionally) CDC */
1040 if (!subset_active(dev
) && dev
->status_ep
) {
1041 dev
->status
= ep_desc (gadget
, &hs_status_desc
,
1043 dev
->status_ep
->driver_data
= dev
;
1045 result
= usb_ep_enable (dev
->status_ep
, dev
->status
);
1047 DEBUG (dev
, "enable %s --> %d\n",
1048 dev
->status_ep
->name
, result
);
1054 dev
->in
= ep_desc(gadget
, &hs_source_desc
, &fs_source_desc
);
1055 dev
->in_ep
->driver_data
= dev
;
1057 dev
->out
= ep_desc(gadget
, &hs_sink_desc
, &fs_sink_desc
);
1058 dev
->out_ep
->driver_data
= dev
;
1060 /* With CDC, the host isn't allowed to use these two data
1061 * endpoints in the default altsetting for the interface.
1062 * so we don't activate them yet. Reset from SET_INTERFACE.
1064 * Strictly speaking RNDIS should work the same: activation is
1065 * a side effect of setting a packet filter. Deactivation is
1066 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
1068 if (!cdc_active(dev
)) {
1069 result
= usb_ep_enable (dev
->in_ep
, dev
->in
);
1071 DEBUG(dev
, "enable %s --> %d\n",
1072 dev
->in_ep
->name
, result
);
1076 result
= usb_ep_enable (dev
->out_ep
, dev
->out
);
1078 DEBUG (dev
, "enable %s --> %d\n",
1079 dev
->out_ep
->name
, result
);
1086 result
= alloc_requests (dev
, qlen (gadget
), gfp_flags
);
1088 /* on error, disable any endpoints */
1090 if (!subset_active(dev
))
1091 (void) usb_ep_disable (dev
->status_ep
);
1093 (void) usb_ep_disable (dev
->in_ep
);
1094 (void) usb_ep_disable (dev
->out_ep
);
1099 /* activate non-CDC configs right away
1100 * this isn't strictly according to the RNDIS spec
1102 if (!cdc_active (dev
)) {
1103 netif_carrier_on (dev
->net
);
1104 if (netif_running (dev
->net
)) {
1105 spin_unlock (&dev
->lock
);
1106 eth_start (dev
, GFP_ATOMIC
);
1107 spin_lock (&dev
->lock
);
1112 DEBUG (dev
, "qlen %d\n", qlen (gadget
));
1114 /* caller is responsible for cleanup on error */
1118 static void eth_reset_config (struct eth_dev
*dev
)
1120 struct usb_request
*req
;
1122 if (dev
->config
== 0)
1125 DEBUG (dev
, "%s\n", __FUNCTION__
);
1127 netif_stop_queue (dev
->net
);
1128 netif_carrier_off (dev
->net
);
1129 rndis_uninit(dev
->rndis_config
);
1131 /* disable endpoints, forcing (synchronous) completion of
1132 * pending i/o. then free the requests.
1135 usb_ep_disable (dev
->in_ep
);
1136 spin_lock(&dev
->req_lock
);
1137 while (likely (!list_empty (&dev
->tx_reqs
))) {
1138 req
= container_of (dev
->tx_reqs
.next
,
1139 struct usb_request
, list
);
1140 list_del (&req
->list
);
1142 spin_unlock(&dev
->req_lock
);
1143 usb_ep_free_request (dev
->in_ep
, req
);
1144 spin_lock(&dev
->req_lock
);
1146 spin_unlock(&dev
->req_lock
);
1149 usb_ep_disable (dev
->out_ep
);
1150 spin_lock(&dev
->req_lock
);
1151 while (likely (!list_empty (&dev
->rx_reqs
))) {
1152 req
= container_of (dev
->rx_reqs
.next
,
1153 struct usb_request
, list
);
1154 list_del (&req
->list
);
1156 spin_unlock(&dev
->req_lock
);
1157 usb_ep_free_request (dev
->out_ep
, req
);
1158 spin_lock(&dev
->req_lock
);
1160 spin_unlock(&dev
->req_lock
);
1164 usb_ep_disable (dev
->status_ep
);
1167 dev
->cdc_filter
= 0;
1171 /* change our operational config. must agree with the code
1172 * that returns config descriptors, and altsetting code.
1175 eth_set_config (struct eth_dev
*dev
, unsigned number
, gfp_t gfp_flags
)
1178 struct usb_gadget
*gadget
= dev
->gadget
;
1180 if (gadget_is_sa1100 (gadget
)
1182 && atomic_read (&dev
->tx_qlen
) != 0) {
1183 /* tx fifo is full, but we can't clear it...*/
1184 INFO (dev
, "can't change configurations\n");
1187 eth_reset_config (dev
);
1190 case DEV_CONFIG_VALUE
:
1191 result
= set_ether_config (dev
, gfp_flags
);
1193 #ifdef CONFIG_USB_ETH_RNDIS
1194 case DEV_RNDIS_CONFIG_VALUE
:
1196 result
= set_ether_config (dev
, gfp_flags
);
1208 eth_reset_config (dev
);
1209 usb_gadget_vbus_draw(dev
->gadget
,
1210 dev
->gadget
->is_otg
? 8 : 100);
1215 power
= 2 * eth_config
.bMaxPower
;
1216 usb_gadget_vbus_draw(dev
->gadget
, power
);
1218 switch (gadget
->speed
) {
1219 case USB_SPEED_FULL
: speed
= "full"; break;
1220 #ifdef CONFIG_USB_GADGET_DUALSPEED
1221 case USB_SPEED_HIGH
: speed
= "high"; break;
1223 default: speed
= "?"; break;
1226 dev
->config
= number
;
1227 INFO (dev
, "%s speed config #%d: %d mA, %s, using %s\n",
1228 speed
, number
, power
, driver_desc
,
1233 : "CDC Ethernet Subset"));
1238 /*-------------------------------------------------------------------------*/
1240 #ifdef DEV_CONFIG_CDC
1242 /* The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
1243 * only to notify the host about link status changes (which we support) or
1244 * report completion of some encapsulated command (as used in RNDIS). Since
1245 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1246 * command mechanism; and only one status request is ever queued.
1249 static void eth_status_complete (struct usb_ep
*ep
, struct usb_request
*req
)
1251 struct usb_cdc_notification
*event
= req
->buf
;
1252 int value
= req
->status
;
1253 struct eth_dev
*dev
= ep
->driver_data
;
1255 /* issue the second notification if host reads the first */
1256 if (event
->bNotificationType
== USB_CDC_NOTIFY_NETWORK_CONNECTION
1258 __le32
*data
= req
->buf
+ sizeof *event
;
1260 event
->bmRequestType
= 0xA1;
1261 event
->bNotificationType
= USB_CDC_NOTIFY_SPEED_CHANGE
;
1262 event
->wValue
= __constant_cpu_to_le16 (0);
1263 event
->wIndex
= __constant_cpu_to_le16 (1);
1264 event
->wLength
= __constant_cpu_to_le16 (8);
1266 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1267 data
[0] = data
[1] = cpu_to_le32 (BITRATE (dev
->gadget
));
1269 req
->length
= STATUS_BYTECOUNT
;
1270 value
= usb_ep_queue (ep
, req
, GFP_ATOMIC
);
1271 DEBUG (dev
, "send SPEED_CHANGE --> %d\n", value
);
1274 } else if (value
!= -ECONNRESET
)
1275 DEBUG (dev
, "event %02x --> %d\n",
1276 event
->bNotificationType
, value
);
1277 req
->context
= NULL
;
1280 static void issue_start_status (struct eth_dev
*dev
)
1282 struct usb_request
*req
= dev
->stat_req
;
1283 struct usb_cdc_notification
*event
;
1286 DEBUG (dev
, "%s, flush old status first\n", __FUNCTION__
);
1290 * FIXME ugly idiom, maybe we'd be better with just
1291 * a "cancel the whole queue" primitive since any
1292 * unlink-one primitive has way too many error modes.
1293 * here, we "know" toggle is already clear...
1295 * FIXME iff req->context != null just dequeue it
1297 usb_ep_disable (dev
->status_ep
);
1298 usb_ep_enable (dev
->status_ep
, dev
->status
);
1300 /* 3.8.1 says to issue first NETWORK_CONNECTION, then
1301 * a SPEED_CHANGE. could be useful in some configs.
1304 event
->bmRequestType
= 0xA1;
1305 event
->bNotificationType
= USB_CDC_NOTIFY_NETWORK_CONNECTION
;
1306 event
->wValue
= __constant_cpu_to_le16 (1); /* connected */
1307 event
->wIndex
= __constant_cpu_to_le16 (1);
1310 req
->length
= sizeof *event
;
1311 req
->complete
= eth_status_complete
;
1314 value
= usb_ep_queue (dev
->status_ep
, req
, GFP_ATOMIC
);
1316 DEBUG (dev
, "status buf queue --> %d\n", value
);
1321 /*-------------------------------------------------------------------------*/
1323 static void eth_setup_complete (struct usb_ep
*ep
, struct usb_request
*req
)
1325 if (req
->status
|| req
->actual
!= req
->length
)
1326 DEBUG ((struct eth_dev
*) ep
->driver_data
,
1327 "setup complete --> %d, %d/%d\n",
1328 req
->status
, req
->actual
, req
->length
);
1331 #ifdef CONFIG_USB_ETH_RNDIS
1333 static void rndis_response_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 "rndis response complete --> %d, %d/%d\n",
1338 req
->status
, req
->actual
, req
->length
);
1340 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1343 static void rndis_command_complete (struct usb_ep
*ep
, struct usb_request
*req
)
1345 struct eth_dev
*dev
= ep
->driver_data
;
1348 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1349 spin_lock(&dev
->lock
);
1350 status
= rndis_msg_parser (dev
->rndis_config
, (u8
*) req
->buf
);
1352 ERROR(dev
, "%s: rndis parse error %d\n", __FUNCTION__
, status
);
1353 spin_unlock(&dev
->lock
);
1359 * The setup() callback implements all the ep0 functionality that's not
1360 * handled lower down. CDC has a number of less-common features:
1362 * - two interfaces: control, and ethernet data
1363 * - Ethernet data interface has two altsettings: default, and active
1364 * - class-specific descriptors for the control interface
1365 * - class-specific control requests
1368 eth_setup (struct usb_gadget
*gadget
, const struct usb_ctrlrequest
*ctrl
)
1370 struct eth_dev
*dev
= get_gadget_data (gadget
);
1371 struct usb_request
*req
= dev
->req
;
1372 int value
= -EOPNOTSUPP
;
1373 u16 wIndex
= le16_to_cpu(ctrl
->wIndex
);
1374 u16 wValue
= le16_to_cpu(ctrl
->wValue
);
1375 u16 wLength
= le16_to_cpu(ctrl
->wLength
);
1377 /* descriptors just go into the pre-allocated ep0 buffer,
1378 * while config change events may enable network traffic.
1380 req
->complete
= eth_setup_complete
;
1381 switch (ctrl
->bRequest
) {
1383 case USB_REQ_GET_DESCRIPTOR
:
1384 if (ctrl
->bRequestType
!= USB_DIR_IN
)
1386 switch (wValue
>> 8) {
1389 value
= min (wLength
, (u16
) sizeof device_desc
);
1390 memcpy (req
->buf
, &device_desc
, value
);
1392 #ifdef CONFIG_USB_GADGET_DUALSPEED
1393 case USB_DT_DEVICE_QUALIFIER
:
1394 if (!gadget
->is_dualspeed
)
1396 value
= min (wLength
, (u16
) sizeof dev_qualifier
);
1397 memcpy (req
->buf
, &dev_qualifier
, value
);
1400 case USB_DT_OTHER_SPEED_CONFIG
:
1401 if (!gadget
->is_dualspeed
)
1404 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1406 value
= config_buf (gadget
->speed
, req
->buf
,
1411 value
= min (wLength
, (u16
) value
);
1415 value
= usb_gadget_get_string (&stringtab
,
1416 wValue
& 0xff, req
->buf
);
1418 value
= min (wLength
, (u16
) value
);
1423 case USB_REQ_SET_CONFIGURATION
:
1424 if (ctrl
->bRequestType
!= 0)
1426 if (gadget
->a_hnp_support
)
1427 DEBUG (dev
, "HNP available\n");
1428 else if (gadget
->a_alt_hnp_support
)
1429 DEBUG (dev
, "HNP needs a different root port\n");
1430 spin_lock (&dev
->lock
);
1431 value
= eth_set_config (dev
, wValue
, GFP_ATOMIC
);
1432 spin_unlock (&dev
->lock
);
1434 case USB_REQ_GET_CONFIGURATION
:
1435 if (ctrl
->bRequestType
!= USB_DIR_IN
)
1437 *(u8
*)req
->buf
= dev
->config
;
1438 value
= min (wLength
, (u16
) 1);
1441 case USB_REQ_SET_INTERFACE
:
1442 if (ctrl
->bRequestType
!= USB_RECIP_INTERFACE
1446 if (!cdc_active(dev
) && wIndex
!= 0)
1448 spin_lock (&dev
->lock
);
1450 /* PXA hardware partially handles SET_INTERFACE;
1451 * we need to kluge around that interference.
1453 if (gadget_is_pxa (gadget
)) {
1454 value
= eth_set_config (dev
, DEV_CONFIG_VALUE
,
1459 #ifdef DEV_CONFIG_CDC
1461 case 0: /* control/master intf */
1465 usb_ep_disable (dev
->status_ep
);
1466 usb_ep_enable (dev
->status_ep
, dev
->status
);
1470 case 1: /* data intf */
1473 usb_ep_disable (dev
->in_ep
);
1474 usb_ep_disable (dev
->out_ep
);
1476 /* CDC requires the data transfers not be done from
1477 * the default interface setting ... also, setting
1478 * the non-default interface resets filters etc.
1481 if (!cdc_active (dev
))
1483 usb_ep_enable (dev
->in_ep
, dev
->in
);
1484 usb_ep_enable (dev
->out_ep
, dev
->out
);
1485 dev
->cdc_filter
= DEFAULT_FILTER
;
1486 netif_carrier_on (dev
->net
);
1488 issue_start_status (dev
);
1489 if (netif_running (dev
->net
)) {
1490 spin_unlock (&dev
->lock
);
1491 eth_start (dev
, GFP_ATOMIC
);
1492 spin_lock (&dev
->lock
);
1495 netif_stop_queue (dev
->net
);
1496 netif_carrier_off (dev
->net
);
1502 /* FIXME this is wrong, as is the assumption that
1503 * all non-PXA hardware talks real CDC ...
1505 dev_warn (&gadget
->dev
, "set_interface ignored!\n");
1506 #endif /* DEV_CONFIG_CDC */
1509 spin_unlock (&dev
->lock
);
1511 case USB_REQ_GET_INTERFACE
:
1512 if (ctrl
->bRequestType
!= (USB_DIR_IN
|USB_RECIP_INTERFACE
)
1516 if (!(cdc_active(dev
) || rndis_active(dev
)) && wIndex
!= 0)
1519 /* for CDC, iff carrier is on, data interface is active. */
1520 if (rndis_active(dev
) || wIndex
!= 1)
1521 *(u8
*)req
->buf
= 0;
1523 *(u8
*)req
->buf
= netif_carrier_ok (dev
->net
) ? 1 : 0;
1524 value
= min (wLength
, (u16
) 1);
1527 #ifdef DEV_CONFIG_CDC
1528 case USB_CDC_SET_ETHERNET_PACKET_FILTER
:
1529 /* see 6.2.30: no data, wIndex = interface,
1530 * wValue = packet filter bitmap
1532 if (ctrl
->bRequestType
!= (USB_TYPE_CLASS
|USB_RECIP_INTERFACE
)
1537 DEBUG (dev
, "packet filter %02x\n", wValue
);
1538 dev
->cdc_filter
= wValue
;
1543 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1544 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1545 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1546 * case USB_CDC_GET_ETHERNET_STATISTIC:
1549 #endif /* DEV_CONFIG_CDC */
1551 #ifdef CONFIG_USB_ETH_RNDIS
1552 /* RNDIS uses the CDC command encapsulation mechanism to implement
1553 * an RPC scheme, with much getting/setting of attributes by OID.
1555 case USB_CDC_SEND_ENCAPSULATED_COMMAND
:
1556 if (ctrl
->bRequestType
!= (USB_TYPE_CLASS
|USB_RECIP_INTERFACE
)
1557 || !rndis_active(dev
)
1558 || wLength
> USB_BUFSIZ
1560 || rndis_control_intf
.bInterfaceNumber
1563 /* read the request, then process it */
1565 req
->complete
= rndis_command_complete
;
1566 /* later, rndis_control_ack () sends a notification */
1569 case USB_CDC_GET_ENCAPSULATED_RESPONSE
:
1570 if ((USB_DIR_IN
|USB_TYPE_CLASS
|USB_RECIP_INTERFACE
)
1571 == ctrl
->bRequestType
1572 && rndis_active(dev
)
1573 // && wLength >= 0x0400
1575 && rndis_control_intf
.bInterfaceNumber
1579 /* return the result */
1580 buf
= rndis_get_next_response (dev
->rndis_config
,
1583 memcpy (req
->buf
, buf
, value
);
1584 req
->complete
= rndis_response_complete
;
1585 rndis_free_response(dev
->rndis_config
, buf
);
1587 /* else stalls ... spec says to avoid that */
1594 "unknown control req%02x.%02x v%04x i%04x l%d\n",
1595 ctrl
->bRequestType
, ctrl
->bRequest
,
1596 wValue
, wIndex
, wLength
);
1599 /* respond with data transfer before status phase? */
1601 req
->length
= value
;
1602 req
->zero
= value
< wLength
1603 && (value
% gadget
->ep0
->maxpacket
) == 0;
1604 value
= usb_ep_queue (gadget
->ep0
, req
, GFP_ATOMIC
);
1606 DEBUG (dev
, "ep_queue --> %d\n", value
);
1608 eth_setup_complete (gadget
->ep0
, req
);
1612 /* host either stalls (value < 0) or reports success */
1617 eth_disconnect (struct usb_gadget
*gadget
)
1619 struct eth_dev
*dev
= get_gadget_data (gadget
);
1620 unsigned long flags
;
1622 spin_lock_irqsave (&dev
->lock
, flags
);
1623 netif_stop_queue (dev
->net
);
1624 netif_carrier_off (dev
->net
);
1625 eth_reset_config (dev
);
1626 spin_unlock_irqrestore (&dev
->lock
, flags
);
1628 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1630 /* next we may get setup() calls to enumerate new connections;
1631 * or an unbind() during shutdown (including removing module).
1635 /*-------------------------------------------------------------------------*/
1637 /* NETWORK DRIVER HOOKUP (to the layer above this driver) */
1639 static int eth_change_mtu (struct net_device
*net
, int new_mtu
)
1641 struct eth_dev
*dev
= netdev_priv(net
);
1646 if (new_mtu
<= ETH_HLEN
|| new_mtu
> ETH_FRAME_LEN
)
1648 /* no zero-length packet read wanted after mtu-sized packets */
1649 if (((new_mtu
+ sizeof (struct ethhdr
)) % dev
->in_ep
->maxpacket
) == 0)
1655 static struct net_device_stats
*eth_get_stats (struct net_device
*net
)
1657 return &((struct eth_dev
*)netdev_priv(net
))->stats
;
1660 static void eth_get_drvinfo(struct net_device
*net
, struct ethtool_drvinfo
*p
)
1662 struct eth_dev
*dev
= netdev_priv(net
);
1663 strlcpy(p
->driver
, shortname
, sizeof p
->driver
);
1664 strlcpy(p
->version
, DRIVER_VERSION
, sizeof p
->version
);
1665 strlcpy(p
->fw_version
, dev
->gadget
->name
, sizeof p
->fw_version
);
1666 strlcpy (p
->bus_info
, dev
->gadget
->dev
.bus_id
, sizeof p
->bus_info
);
1669 static u32
eth_get_link(struct net_device
*net
)
1671 struct eth_dev
*dev
= netdev_priv(net
);
1672 return dev
->gadget
->speed
!= USB_SPEED_UNKNOWN
;
1675 static struct ethtool_ops ops
= {
1676 .get_drvinfo
= eth_get_drvinfo
,
1677 .get_link
= eth_get_link
1680 static void defer_kevent (struct eth_dev
*dev
, int flag
)
1682 if (test_and_set_bit (flag
, &dev
->todo
))
1684 if (!schedule_work (&dev
->work
))
1685 ERROR (dev
, "kevent %d may have been dropped\n", flag
);
1687 DEBUG (dev
, "kevent %d scheduled\n", flag
);
1690 static void rx_complete (struct usb_ep
*ep
, struct usb_request
*req
);
1693 rx_submit (struct eth_dev
*dev
, struct usb_request
*req
, gfp_t gfp_flags
)
1695 struct sk_buff
*skb
;
1696 int retval
= -ENOMEM
;
1699 /* Padding up to RX_EXTRA handles minor disagreements with host.
1700 * Normally we use the USB "terminate on short read" convention;
1701 * so allow up to (N*maxpacket), since that memory is normally
1702 * already allocated. Some hardware doesn't deal well with short
1703 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1704 * byte off the end (to force hardware errors on overflow).
1706 * RNDIS uses internal framing, and explicitly allows senders to
1707 * pad to end-of-packet. That's potentially nice for speed,
1708 * but means receivers can't recover synch on their own.
1710 size
= (sizeof (struct ethhdr
) + dev
->net
->mtu
+ RX_EXTRA
);
1711 size
+= dev
->out_ep
->maxpacket
- 1;
1712 if (rndis_active(dev
))
1713 size
+= sizeof (struct rndis_packet_msg_type
);
1714 size
-= size
% dev
->out_ep
->maxpacket
;
1716 if ((skb
= alloc_skb (size
+ NET_IP_ALIGN
, gfp_flags
)) == 0) {
1717 DEBUG (dev
, "no rx skb\n");
1721 /* Some platforms perform better when IP packets are aligned,
1722 * but on at least one, checksumming fails otherwise. Note:
1723 * RNDIS headers involve variable numbers of LE32 values.
1725 skb_reserve(skb
, NET_IP_ALIGN
);
1727 req
->buf
= skb
->data
;
1729 req
->complete
= rx_complete
;
1732 retval
= usb_ep_queue (dev
->out_ep
, req
, gfp_flags
);
1733 if (retval
== -ENOMEM
)
1735 defer_kevent (dev
, WORK_RX_MEMORY
);
1737 DEBUG (dev
, "rx submit --> %d\n", retval
);
1738 dev_kfree_skb_any (skb
);
1739 spin_lock(&dev
->req_lock
);
1740 list_add (&req
->list
, &dev
->rx_reqs
);
1741 spin_unlock(&dev
->req_lock
);
1746 static void rx_complete (struct usb_ep
*ep
, struct usb_request
*req
)
1748 struct sk_buff
*skb
= req
->context
;
1749 struct eth_dev
*dev
= ep
->driver_data
;
1750 int status
= req
->status
;
1754 /* normal completion */
1756 skb_put (skb
, req
->actual
);
1757 /* we know MaxPacketsPerTransfer == 1 here */
1758 if (rndis_active(dev
))
1759 status
= rndis_rm_hdr (skb
);
1761 || ETH_HLEN
> skb
->len
1762 || skb
->len
> ETH_FRAME_LEN
) {
1763 dev
->stats
.rx_errors
++;
1764 dev
->stats
.rx_length_errors
++;
1765 DEBUG (dev
, "rx length %d\n", skb
->len
);
1769 skb
->protocol
= eth_type_trans (skb
, dev
->net
);
1770 dev
->stats
.rx_packets
++;
1771 dev
->stats
.rx_bytes
+= skb
->len
;
1773 /* no buffer copies needed, unless hardware can't
1776 status
= netif_rx (skb
);
1780 /* software-driven interface shutdown */
1781 case -ECONNRESET
: // unlink
1782 case -ESHUTDOWN
: // disconnect etc
1783 VDEBUG (dev
, "rx shutdown, code %d\n", status
);
1786 /* for hardware automagic (such as pxa) */
1787 case -ECONNABORTED
: // endpoint reset
1788 DEBUG (dev
, "rx %s reset\n", ep
->name
);
1789 defer_kevent (dev
, WORK_RX_MEMORY
);
1791 dev_kfree_skb_any (skb
);
1796 dev
->stats
.rx_over_errors
++;
1800 dev
->stats
.rx_errors
++;
1801 DEBUG (dev
, "rx status %d\n", status
);
1806 dev_kfree_skb_any (skb
);
1807 if (!netif_running (dev
->net
)) {
1809 spin_lock(&dev
->req_lock
);
1810 list_add (&req
->list
, &dev
->rx_reqs
);
1811 spin_unlock(&dev
->req_lock
);
1815 rx_submit (dev
, req
, GFP_ATOMIC
);
1818 static int prealloc (struct list_head
*list
, struct usb_ep
*ep
,
1819 unsigned n
, gfp_t gfp_flags
)
1822 struct usb_request
*req
;
1827 /* queue/recycle up to N requests */
1829 list_for_each_entry (req
, list
, list
) {
1834 req
= usb_ep_alloc_request (ep
, gfp_flags
);
1836 return list_empty (list
) ? -ENOMEM
: 0;
1837 list_add (&req
->list
, list
);
1844 struct list_head
*next
;
1846 next
= req
->list
.next
;
1847 list_del (&req
->list
);
1848 usb_ep_free_request (ep
, req
);
1853 req
= container_of (next
, struct usb_request
, list
);
1858 static int alloc_requests (struct eth_dev
*dev
, unsigned n
, gfp_t gfp_flags
)
1862 spin_lock(&dev
->req_lock
);
1863 status
= prealloc (&dev
->tx_reqs
, dev
->in_ep
, n
, gfp_flags
);
1866 status
= prealloc (&dev
->rx_reqs
, dev
->out_ep
, n
, gfp_flags
);
1871 DEBUG (dev
, "can't alloc requests\n");
1873 spin_unlock(&dev
->req_lock
);
1877 static void rx_fill (struct eth_dev
*dev
, gfp_t gfp_flags
)
1879 struct usb_request
*req
;
1880 unsigned long flags
;
1882 /* fill unused rxq slots with some skb */
1883 spin_lock_irqsave(&dev
->req_lock
, flags
);
1884 while (!list_empty (&dev
->rx_reqs
)) {
1885 req
= container_of (dev
->rx_reqs
.next
,
1886 struct usb_request
, list
);
1887 list_del_init (&req
->list
);
1888 spin_unlock_irqrestore(&dev
->req_lock
, flags
);
1890 if (rx_submit (dev
, req
, gfp_flags
) < 0) {
1891 defer_kevent (dev
, WORK_RX_MEMORY
);
1895 spin_lock_irqsave(&dev
->req_lock
, flags
);
1897 spin_unlock_irqrestore(&dev
->req_lock
, flags
);
1900 static void eth_work (struct work_struct
*work
)
1902 struct eth_dev
*dev
= container_of(work
, struct eth_dev
, work
);
1904 if (test_and_clear_bit (WORK_RX_MEMORY
, &dev
->todo
)) {
1905 if (netif_running (dev
->net
))
1906 rx_fill (dev
, GFP_KERNEL
);
1910 DEBUG (dev
, "work done, flags = 0x%lx\n", dev
->todo
);
1913 static void tx_complete (struct usb_ep
*ep
, struct usb_request
*req
)
1915 struct sk_buff
*skb
= req
->context
;
1916 struct eth_dev
*dev
= ep
->driver_data
;
1918 switch (req
->status
) {
1920 dev
->stats
.tx_errors
++;
1921 VDEBUG (dev
, "tx err %d\n", req
->status
);
1923 case -ECONNRESET
: // unlink
1924 case -ESHUTDOWN
: // disconnect etc
1927 dev
->stats
.tx_bytes
+= skb
->len
;
1929 dev
->stats
.tx_packets
++;
1931 spin_lock(&dev
->req_lock
);
1932 list_add (&req
->list
, &dev
->tx_reqs
);
1933 spin_unlock(&dev
->req_lock
);
1934 dev_kfree_skb_any (skb
);
1936 atomic_dec (&dev
->tx_qlen
);
1937 if (netif_carrier_ok (dev
->net
))
1938 netif_wake_queue (dev
->net
);
1941 static inline int eth_is_promisc (struct eth_dev
*dev
)
1943 /* no filters for the CDC subset; always promisc */
1944 if (subset_active (dev
))
1946 return dev
->cdc_filter
& USB_CDC_PACKET_TYPE_PROMISCUOUS
;
1949 static int eth_start_xmit (struct sk_buff
*skb
, struct net_device
*net
)
1951 struct eth_dev
*dev
= netdev_priv(net
);
1952 int length
= skb
->len
;
1954 struct usb_request
*req
= NULL
;
1955 unsigned long flags
;
1957 /* apply outgoing CDC or RNDIS filters */
1958 if (!eth_is_promisc (dev
)) {
1959 u8
*dest
= skb
->data
;
1961 if (is_multicast_ether_addr(dest
)) {
1964 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1965 * SET_ETHERNET_MULTICAST_FILTERS requests
1967 if (is_broadcast_ether_addr(dest
))
1968 type
= USB_CDC_PACKET_TYPE_BROADCAST
;
1970 type
= USB_CDC_PACKET_TYPE_ALL_MULTICAST
;
1971 if (!(dev
->cdc_filter
& type
)) {
1972 dev_kfree_skb_any (skb
);
1976 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1979 spin_lock_irqsave(&dev
->req_lock
, flags
);
1980 req
= container_of (dev
->tx_reqs
.next
, struct usb_request
, list
);
1981 list_del (&req
->list
);
1982 if (list_empty (&dev
->tx_reqs
))
1983 netif_stop_queue (net
);
1984 spin_unlock_irqrestore(&dev
->req_lock
, flags
);
1986 /* no buffer copies needed, unless the network stack did it
1987 * or the hardware can't use skb buffers.
1988 * or there's not enough space for any RNDIS headers we need
1990 if (rndis_active(dev
)) {
1991 struct sk_buff
*skb_rndis
;
1993 skb_rndis
= skb_realloc_headroom (skb
,
1994 sizeof (struct rndis_packet_msg_type
));
1998 dev_kfree_skb_any (skb
);
2000 rndis_add_hdr (skb
);
2003 req
->buf
= skb
->data
;
2005 req
->complete
= tx_complete
;
2007 /* use zlp framing on tx for strict CDC-Ether conformance,
2008 * though any robust network rx path ignores extra padding.
2009 * and some hardware doesn't like to write zlps.
2012 if (!dev
->zlp
&& (length
% dev
->in_ep
->maxpacket
) == 0)
2015 req
->length
= length
;
2017 #ifdef CONFIG_USB_GADGET_DUALSPEED
2018 /* throttle highspeed IRQ rate back slightly */
2019 req
->no_interrupt
= (dev
->gadget
->speed
== USB_SPEED_HIGH
)
2020 ? ((atomic_read (&dev
->tx_qlen
) % TX_DELAY
) != 0)
2024 retval
= usb_ep_queue (dev
->in_ep
, req
, GFP_ATOMIC
);
2027 DEBUG (dev
, "tx queue err %d\n", retval
);
2030 net
->trans_start
= jiffies
;
2031 atomic_inc (&dev
->tx_qlen
);
2036 dev
->stats
.tx_dropped
++;
2037 dev_kfree_skb_any (skb
);
2038 spin_lock_irqsave(&dev
->req_lock
, flags
);
2039 if (list_empty (&dev
->tx_reqs
))
2040 netif_start_queue (net
);
2041 list_add (&req
->list
, &dev
->tx_reqs
);
2042 spin_unlock_irqrestore(&dev
->req_lock
, flags
);
2047 /*-------------------------------------------------------------------------*/
2049 #ifdef CONFIG_USB_ETH_RNDIS
2051 /* The interrupt endpoint is used in RNDIS to notify the host when messages
2052 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
2053 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
2054 * REMOTE_NDIS_KEEPALIVE_MSG.
2056 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
2057 * normally just one notification will be queued.
2060 static struct usb_request
*eth_req_alloc (struct usb_ep
*, unsigned, gfp_t
);
2061 static void eth_req_free (struct usb_ep
*ep
, struct usb_request
*req
);
2064 rndis_control_ack_complete (struct usb_ep
*ep
, struct usb_request
*req
)
2066 struct eth_dev
*dev
= ep
->driver_data
;
2068 if (req
->status
|| req
->actual
!= req
->length
)
2070 "rndis control ack complete --> %d, %d/%d\n",
2071 req
->status
, req
->actual
, req
->length
);
2072 req
->context
= NULL
;
2074 if (req
!= dev
->stat_req
)
2075 eth_req_free(ep
, req
);
2078 static int rndis_control_ack (struct net_device
*net
)
2080 struct eth_dev
*dev
= netdev_priv(net
);
2082 struct usb_request
*resp
= dev
->stat_req
;
2084 /* in case RNDIS calls this after disconnect */
2086 DEBUG (dev
, "status ENODEV\n");
2090 /* in case queue length > 1 */
2091 if (resp
->context
) {
2092 resp
= eth_req_alloc (dev
->status_ep
, 8, GFP_ATOMIC
);
2097 /* Send RNDIS RESPONSE_AVAILABLE notification;
2098 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
2101 resp
->complete
= rndis_control_ack_complete
;
2102 resp
->context
= dev
;
2104 *((__le32
*) resp
->buf
) = __constant_cpu_to_le32 (1);
2105 *((__le32
*) resp
->buf
+ 1) = __constant_cpu_to_le32 (0);
2107 length
= usb_ep_queue (dev
->status_ep
, resp
, GFP_ATOMIC
);
2110 rndis_control_ack_complete (dev
->status_ep
, resp
);
2118 #define rndis_control_ack NULL
2122 static void eth_start (struct eth_dev
*dev
, gfp_t gfp_flags
)
2124 DEBUG (dev
, "%s\n", __FUNCTION__
);
2126 /* fill the rx queue */
2127 rx_fill (dev
, gfp_flags
);
2129 /* and open the tx floodgates */
2130 atomic_set (&dev
->tx_qlen
, 0);
2131 netif_wake_queue (dev
->net
);
2132 if (rndis_active(dev
)) {
2133 rndis_set_param_medium (dev
->rndis_config
,
2135 BITRATE(dev
->gadget
)/100);
2136 (void) rndis_signal_connect (dev
->rndis_config
);
2140 static int eth_open (struct net_device
*net
)
2142 struct eth_dev
*dev
= netdev_priv(net
);
2144 DEBUG (dev
, "%s\n", __FUNCTION__
);
2145 if (netif_carrier_ok (dev
->net
))
2146 eth_start (dev
, GFP_KERNEL
);
2150 static int eth_stop (struct net_device
*net
)
2152 struct eth_dev
*dev
= netdev_priv(net
);
2154 VDEBUG (dev
, "%s\n", __FUNCTION__
);
2155 netif_stop_queue (net
);
2157 DEBUG (dev
, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
2158 dev
->stats
.rx_packets
, dev
->stats
.tx_packets
,
2159 dev
->stats
.rx_errors
, dev
->stats
.tx_errors
2162 /* ensure there are no more active requests */
2164 usb_ep_disable (dev
->in_ep
);
2165 usb_ep_disable (dev
->out_ep
);
2166 if (netif_carrier_ok (dev
->net
)) {
2167 DEBUG (dev
, "host still using in/out endpoints\n");
2168 // FIXME idiom may leave toggle wrong here
2169 usb_ep_enable (dev
->in_ep
, dev
->in
);
2170 usb_ep_enable (dev
->out_ep
, dev
->out
);
2172 if (dev
->status_ep
) {
2173 usb_ep_disable (dev
->status_ep
);
2174 usb_ep_enable (dev
->status_ep
, dev
->status
);
2178 if (rndis_active(dev
)) {
2179 rndis_set_param_medium (dev
->rndis_config
,
2180 NDIS_MEDIUM_802_3
, 0);
2181 (void) rndis_signal_disconnect (dev
->rndis_config
);
2187 /*-------------------------------------------------------------------------*/
2189 static struct usb_request
*
2190 eth_req_alloc (struct usb_ep
*ep
, unsigned size
, gfp_t gfp_flags
)
2192 struct usb_request
*req
;
2194 req
= usb_ep_alloc_request (ep
, gfp_flags
);
2198 req
->buf
= kmalloc (size
, gfp_flags
);
2200 usb_ep_free_request (ep
, req
);
2207 eth_req_free (struct usb_ep
*ep
, struct usb_request
*req
)
2210 usb_ep_free_request (ep
, req
);
2214 static void /* __init_or_exit */
2215 eth_unbind (struct usb_gadget
*gadget
)
2217 struct eth_dev
*dev
= get_gadget_data (gadget
);
2219 DEBUG (dev
, "unbind\n");
2220 rndis_deregister (dev
->rndis_config
);
2223 /* we've already been disconnected ... no i/o is active */
2225 eth_req_free (gadget
->ep0
, dev
->req
);
2228 if (dev
->stat_req
) {
2229 eth_req_free (dev
->status_ep
, dev
->stat_req
);
2230 dev
->stat_req
= NULL
;
2233 unregister_netdev (dev
->net
);
2234 free_netdev(dev
->net
);
2236 /* assuming we used keventd, it must quiesce too */
2237 flush_scheduled_work ();
2238 set_gadget_data (gadget
, NULL
);
2241 static u8 __devinit
nibble (unsigned char c
)
2243 if (likely (isdigit (c
)))
2246 if (likely (isxdigit (c
)))
2247 return 10 + c
- 'A';
2251 static int __devinit
get_ether_addr(const char *str
, u8
*dev_addr
)
2256 for (i
= 0; i
< 6; i
++) {
2259 if((*str
== '.') || (*str
== ':'))
2261 num
= nibble(*str
++) << 4;
2262 num
|= (nibble(*str
++));
2265 if (is_valid_ether_addr (dev_addr
))
2268 random_ether_addr(dev_addr
);
2272 static int __devinit
2273 eth_bind (struct usb_gadget
*gadget
)
2275 struct eth_dev
*dev
;
2276 struct net_device
*net
;
2277 u8 cdc
= 1, zlp
= 1, rndis
= 1;
2278 struct usb_ep
*in_ep
, *out_ep
, *status_ep
= NULL
;
2279 int status
= -ENOMEM
;
2282 /* these flags are only ever cleared; compiler take note */
2283 #ifndef DEV_CONFIG_CDC
2286 #ifndef CONFIG_USB_ETH_RNDIS
2290 /* Because most host side USB stacks handle CDC Ethernet, that
2291 * standard protocol is _strongly_ preferred for interop purposes.
2292 * (By everyone except Microsoft.)
2294 if (gadget_is_pxa (gadget
)) {
2295 /* pxa doesn't support altsettings */
2297 } else if (gadget_is_musbhdrc(gadget
)) {
2298 /* reduce tx dma overhead by avoiding special cases */
2300 } else if (gadget_is_sh(gadget
)) {
2301 /* sh doesn't support multiple interfaces or configs */
2304 } else if (gadget_is_sa1100 (gadget
)) {
2305 /* hardware can't write zlps */
2307 /* sa1100 CAN do CDC, without status endpoint ... we use
2308 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2313 gcnum
= usb_gadget_controller_number (gadget
);
2315 device_desc
.bcdDevice
= cpu_to_le16 (0x0200 + gcnum
);
2317 /* can't assume CDC works. don't want to default to
2318 * anything less functional on CDC-capable hardware,
2319 * so we fail in this case.
2321 dev_err (&gadget
->dev
,
2322 "controller '%s' not recognized\n",
2326 snprintf (manufacturer
, sizeof manufacturer
, "%s %s/%s",
2327 init_utsname()->sysname
, init_utsname()->release
,
2330 /* If there's an RNDIS configuration, that's what Windows wants to
2331 * be using ... so use these product IDs here and in the "linux.inf"
2332 * needed to install MSFT drivers. Current Linux kernels will use
2333 * the second configuration if it's CDC Ethernet, and need some help
2334 * to choose the right configuration otherwise.
2337 device_desc
.idVendor
=
2338 __constant_cpu_to_le16(RNDIS_VENDOR_NUM
);
2339 device_desc
.idProduct
=
2340 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM
);
2341 snprintf (product_desc
, sizeof product_desc
,
2342 "RNDIS/%s", driver_desc
);
2344 /* CDC subset ... recognized by Linux since 2.4.10, but Windows
2345 * drivers aren't widely available. (That may be improved by
2346 * supporting one submode of the "SAFE" variant of MDLM.)
2349 device_desc
.idVendor
=
2350 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM
);
2351 device_desc
.idProduct
=
2352 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM
);
2355 /* support optional vendor/distro customization */
2358 dev_err (&gadget
->dev
, "idVendor needs idProduct!\n");
2361 device_desc
.idVendor
= cpu_to_le16(idVendor
);
2362 device_desc
.idProduct
= cpu_to_le16(idProduct
);
2364 device_desc
.bcdDevice
= cpu_to_le16(bcdDevice
);
2367 strlcpy (manufacturer
, iManufacturer
, sizeof manufacturer
);
2369 strlcpy (product_desc
, iProduct
, sizeof product_desc
);
2370 if (iSerialNumber
) {
2371 device_desc
.iSerialNumber
= STRING_SERIALNUMBER
,
2372 strlcpy(serial_number
, iSerialNumber
, sizeof serial_number
);
2375 /* all we really need is bulk IN/OUT */
2376 usb_ep_autoconfig_reset (gadget
);
2377 in_ep
= usb_ep_autoconfig (gadget
, &fs_source_desc
);
2380 dev_err (&gadget
->dev
,
2381 "can't autoconfigure on %s\n",
2385 in_ep
->driver_data
= in_ep
; /* claim */
2387 out_ep
= usb_ep_autoconfig (gadget
, &fs_sink_desc
);
2390 out_ep
->driver_data
= out_ep
; /* claim */
2392 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2393 /* CDC Ethernet control interface doesn't require a status endpoint.
2394 * Since some hosts expect one, try to allocate one anyway.
2397 status_ep
= usb_ep_autoconfig (gadget
, &fs_status_desc
);
2399 status_ep
->driver_data
= status_ep
; /* claim */
2401 dev_err (&gadget
->dev
,
2402 "can't run RNDIS on %s\n",
2405 #ifdef DEV_CONFIG_CDC
2406 /* pxa25x only does CDC subset; often used with RNDIS */
2408 control_intf
.bNumEndpoints
= 0;
2409 /* FIXME remove endpoint from descriptor list */
2415 /* one config: cdc, else minimal subset */
2417 eth_config
.bNumInterfaces
= 1;
2418 eth_config
.iConfiguration
= STRING_SUBSET
;
2420 /* use functions to set these up, in case we're built to work
2421 * with multiple controllers and must override CDC Ethernet.
2423 fs_subset_descriptors();
2424 hs_subset_descriptors();
2427 device_desc
.bMaxPacketSize0
= gadget
->ep0
->maxpacket
;
2428 usb_gadget_set_selfpowered (gadget
);
2430 /* For now RNDIS is always a second config */
2432 device_desc
.bNumConfigurations
= 2;
2434 #ifdef CONFIG_USB_GADGET_DUALSPEED
2436 dev_qualifier
.bNumConfigurations
= 2;
2438 dev_qualifier
.bDeviceClass
= USB_CLASS_VENDOR_SPEC
;
2440 /* assumes ep0 uses the same value for both speeds ... */
2441 dev_qualifier
.bMaxPacketSize0
= device_desc
.bMaxPacketSize0
;
2443 /* and that all endpoints are dual-speed */
2444 hs_source_desc
.bEndpointAddress
= fs_source_desc
.bEndpointAddress
;
2445 hs_sink_desc
.bEndpointAddress
= fs_sink_desc
.bEndpointAddress
;
2446 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2448 hs_status_desc
.bEndpointAddress
=
2449 fs_status_desc
.bEndpointAddress
;
2451 #endif /* DUALSPEED */
2453 if (gadget
->is_otg
) {
2454 otg_descriptor
.bmAttributes
|= USB_OTG_HNP
,
2455 eth_config
.bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
2456 eth_config
.bMaxPower
= 4;
2457 #ifdef CONFIG_USB_ETH_RNDIS
2458 rndis_config
.bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
2459 rndis_config
.bMaxPower
= 4;
2463 net
= alloc_etherdev (sizeof *dev
);
2466 dev
= netdev_priv(net
);
2467 spin_lock_init (&dev
->lock
);
2468 spin_lock_init (&dev
->req_lock
);
2469 INIT_WORK (&dev
->work
, eth_work
);
2470 INIT_LIST_HEAD (&dev
->tx_reqs
);
2471 INIT_LIST_HEAD (&dev
->rx_reqs
);
2473 /* network device setup */
2475 SET_MODULE_OWNER (net
);
2476 strcpy (net
->name
, "usb%d");
2481 dev
->out_ep
= out_ep
;
2482 dev
->status_ep
= status_ep
;
2484 /* Module params for these addresses should come from ID proms.
2485 * The host side address is used with CDC and RNDIS, and commonly
2486 * ends up in a persistent config database. It's not clear if
2487 * host side code for the SAFE thing cares -- its original BLAN
2488 * thing didn't, Sharp never assigned those addresses on Zaurii.
2490 if (get_ether_addr(dev_addr
, net
->dev_addr
))
2491 dev_warn(&gadget
->dev
,
2492 "using random %s ethernet address\n", "self");
2493 if (get_ether_addr(host_addr
, dev
->host_mac
))
2494 dev_warn(&gadget
->dev
,
2495 "using random %s ethernet address\n", "host");
2496 snprintf (ethaddr
, sizeof ethaddr
, "%02X%02X%02X%02X%02X%02X",
2497 dev
->host_mac
[0], dev
->host_mac
[1],
2498 dev
->host_mac
[2], dev
->host_mac
[3],
2499 dev
->host_mac
[4], dev
->host_mac
[5]);
2502 status
= rndis_init();
2504 dev_err (&gadget
->dev
, "can't init RNDIS, %d\n",
2510 net
->change_mtu
= eth_change_mtu
;
2511 net
->get_stats
= eth_get_stats
;
2512 net
->hard_start_xmit
= eth_start_xmit
;
2513 net
->open
= eth_open
;
2514 net
->stop
= eth_stop
;
2515 // watchdog_timeo, tx_timeout ...
2516 // set_multicast_list
2517 SET_ETHTOOL_OPS(net
, &ops
);
2519 /* preallocate control message data and buffer */
2520 dev
->req
= eth_req_alloc (gadget
->ep0
, USB_BUFSIZ
, GFP_KERNEL
);
2523 dev
->req
->complete
= eth_setup_complete
;
2525 /* ... and maybe likewise for status transfer */
2526 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2527 if (dev
->status_ep
) {
2528 dev
->stat_req
= eth_req_alloc (dev
->status_ep
,
2529 STATUS_BYTECOUNT
, GFP_KERNEL
);
2530 if (!dev
->stat_req
) {
2531 eth_req_free (gadget
->ep0
, dev
->req
);
2534 dev
->stat_req
->context
= NULL
;
2538 /* finish hookup to lower layer ... */
2539 dev
->gadget
= gadget
;
2540 set_gadget_data (gadget
, dev
);
2541 gadget
->ep0
->driver_data
= dev
;
2543 /* two kinds of host-initiated state changes:
2544 * - iff DATA transfer is active, carrier is "on"
2545 * - tx queueing enabled if open *and* carrier is "on"
2547 netif_stop_queue (dev
->net
);
2548 netif_carrier_off (dev
->net
);
2550 SET_NETDEV_DEV (dev
->net
, &gadget
->dev
);
2551 status
= register_netdev (dev
->net
);
2555 INFO (dev
, "%s, version: " DRIVER_VERSION
"\n", driver_desc
);
2556 INFO (dev
, "using %s, OUT %s IN %s%s%s\n", gadget
->name
,
2557 out_ep
->name
, in_ep
->name
,
2558 status_ep
? " STATUS " : "",
2559 status_ep
? status_ep
->name
: ""
2561 INFO (dev
, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2562 net
->dev_addr
[0], net
->dev_addr
[1],
2563 net
->dev_addr
[2], net
->dev_addr
[3],
2564 net
->dev_addr
[4], net
->dev_addr
[5]);
2567 INFO (dev
, "HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2568 dev
->host_mac
[0], dev
->host_mac
[1],
2569 dev
->host_mac
[2], dev
->host_mac
[3],
2570 dev
->host_mac
[4], dev
->host_mac
[5]);
2575 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2577 dev
->rndis_config
= rndis_register (rndis_control_ack
);
2578 if (dev
->rndis_config
< 0) {
2580 unregister_netdev (dev
->net
);
2585 /* these set up a lot of the OIDs that RNDIS needs */
2586 rndis_set_host_mac (dev
->rndis_config
, dev
->host_mac
);
2587 if (rndis_set_param_dev (dev
->rndis_config
, dev
->net
,
2588 &dev
->stats
, &dev
->cdc_filter
))
2590 if (rndis_set_param_vendor (dev
->rndis_config
, vendorID
,
2593 if (rndis_set_param_medium (dev
->rndis_config
,
2597 INFO (dev
, "RNDIS ready\n");
2603 dev_dbg(&gadget
->dev
, "register_netdev failed, %d\n", status
);
2605 eth_unbind (gadget
);
2609 /*-------------------------------------------------------------------------*/
2612 eth_suspend (struct usb_gadget
*gadget
)
2614 struct eth_dev
*dev
= get_gadget_data (gadget
);
2616 DEBUG (dev
, "suspend\n");
2621 eth_resume (struct usb_gadget
*gadget
)
2623 struct eth_dev
*dev
= get_gadget_data (gadget
);
2625 DEBUG (dev
, "resume\n");
2629 /*-------------------------------------------------------------------------*/
2631 static struct usb_gadget_driver eth_driver
= {
2634 .function
= (char *) driver_desc
,
2636 .unbind
= eth_unbind
,
2639 .disconnect
= eth_disconnect
,
2641 .suspend
= eth_suspend
,
2642 .resume
= eth_resume
,
2645 .name
= (char *) shortname
,
2646 .owner
= THIS_MODULE
,
2650 MODULE_DESCRIPTION (DRIVER_DESC
);
2651 MODULE_AUTHOR ("David Brownell, Benedikt Spanger");
2652 MODULE_LICENSE ("GPL");
2655 static int __init
init (void)
2657 return usb_gadget_register_driver (ð_driver
);
2661 static void __exit
cleanup (void)
2663 usb_gadget_unregister_driver (ð_driver
);
2665 module_exit (cleanup
);