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_HUSB2DEV
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_SH
296 #define DEV_CONFIG_SUBSET
299 #ifdef CONFIG_USB_GADGET_SA1100
300 /* use non-CDC for backwards compatibility */
301 #define DEV_CONFIG_SUBSET
305 /*-------------------------------------------------------------------------*/
307 /* "main" config is either CDC, or its simple subset */
308 static inline int is_cdc(struct eth_dev *dev)
310 #if !defined(DEV_CONFIG_SUBSET)
311 return 1; /* only cdc possible */
312 #elif !defined (DEV_CONFIG_CDC)
313 return 0; /* only subset possible */
315 return dev->cdc; /* depends on what hardware we found */
319 /* "secondary" RNDIS config may sometimes be activated */
320 static inline int rndis_active(struct eth_dev *dev)
322 #ifdef CONFIG_USB_ETH_RNDIS
329 #define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
330 #define cdc_active(dev) ( is_cdc(dev) && !rndis_active(dev))
334 #define DEFAULT_QLEN 2 /* double buffering by default */
336 /* peak bulk transfer bits-per-second */
337 #define HS_BPS (13 * 512 * 8 * 1000 * 8)
338 #define FS_BPS (19 * 64 * 1 * 1000 * 8)
340 #ifdef CONFIG_USB_GADGET_DUALSPEED
341 #define DEVSPEED USB_SPEED_HIGH
343 static unsigned qmult = 5;
344 module_param (qmult, uint, S_IRUGO|S_IWUSR);
347 /* for dual-speed hardware, use deeper queues at highspeed */
348 #define qlen(gadget) \
349 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
351 /* also defer IRQs on highspeed TX */
352 #define TX_DELAY qmult
354 static inline int BITRATE(struct usb_gadget *g)
356 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
359 #else /* full speed (low speed doesn't do bulk) */
360 #define DEVSPEED USB_SPEED_FULL
362 #define qlen(gadget) DEFAULT_QLEN
364 static inline int BITRATE(struct usb_gadget *g)
371 /*-------------------------------------------------------------------------*/
373 #define xprintk(d,level,fmt,args...) \
374 printk(level "%s: " fmt , (d)->net->name , ## args)
378 #define DEBUG(dev,fmt,args...) \
379 xprintk(dev , KERN_DEBUG , fmt , ## args)
381 #define DEBUG(dev,fmt,args...) \
388 #define VDEBUG(dev,fmt,args...) \
392 #define ERROR(dev,fmt,args...) \
393 xprintk(dev , KERN_ERR , fmt , ## args)
394 #define WARN(dev,fmt,args...) \
395 xprintk(dev , KERN_WARNING , fmt , ## args)
396 #define INFO(dev,fmt,args...) \
397 xprintk(dev , KERN_INFO , fmt , ## args)
399 /*-------------------------------------------------------------------------*/
401 /* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
402 * ep0 implementation: descriptors, config management, setup().
403 * also optional class-specific notification interrupt transfer.
407 * DESCRIPTORS ... most are static, but strings and (full) configuration
408 * descriptors are built on demand. For now we do either full CDC, or
409 * our simple subset, with RNDIS as an optional second configuration.
411 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
412 * the class descriptors match a modem (they're ignored; it's really just
413 * Ethernet functionality), they don't need the NOP altsetting, and the
414 * status transfer endpoint isn't optional.
417 #define STRING_MANUFACTURER 1
418 #define STRING_PRODUCT 2
419 #define STRING_ETHADDR 3
420 #define STRING_DATA 4
421 #define STRING_CONTROL 5
422 #define STRING_RNDIS_CONTROL 6
424 #define STRING_SUBSET 8
425 #define STRING_RNDIS 9
426 #define STRING_SERIALNUMBER 10
428 /* holds our biggest descriptor (or RNDIS response) */
429 #define USB_BUFSIZ 256
432 * This device advertises one configuration, eth_config, unless RNDIS
433 * is enabled (rndis_config) on hardware supporting at least two configs.
435 * NOTE: Controllers like superh_udc should probably be able to use
436 * an RNDIS-only configuration.
438 * FIXME define some higher-powered configurations to make it easier
439 * to recharge batteries ...
442 #define DEV_CONFIG_VALUE 1 /* cdc or subset */
443 #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
445 static struct usb_device_descriptor
447 .bLength = sizeof device_desc,
448 .bDescriptorType = USB_DT_DEVICE,
450 .bcdUSB = __constant_cpu_to_le16 (0x0200),
452 .bDeviceClass = USB_CLASS_COMM,
453 .bDeviceSubClass = 0,
454 .bDeviceProtocol = 0,
456 .idVendor = __constant_cpu_to_le16 (CDC_VENDOR_NUM),
457 .idProduct = __constant_cpu_to_le16 (CDC_PRODUCT_NUM),
458 .iManufacturer = STRING_MANUFACTURER,
459 .iProduct = STRING_PRODUCT,
460 .bNumConfigurations = 1,
463 static struct usb_otg_descriptor
465 .bLength = sizeof otg_descriptor,
466 .bDescriptorType = USB_DT_OTG,
468 .bmAttributes = USB_OTG_SRP,
471 static struct usb_config_descriptor
473 .bLength = sizeof eth_config,
474 .bDescriptorType = USB_DT_CONFIG,
476 /* compute wTotalLength on the fly */
478 .bConfigurationValue = DEV_CONFIG_VALUE,
479 .iConfiguration = STRING_CDC,
480 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
484 #ifdef CONFIG_USB_ETH_RNDIS
485 static struct usb_config_descriptor
487 .bLength = sizeof rndis_config,
488 .bDescriptorType = USB_DT_CONFIG,
490 /* compute wTotalLength on the fly */
492 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
493 .iConfiguration = STRING_RNDIS,
494 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
500 * Compared to the simple CDC subset, the full CDC Ethernet model adds
501 * three class descriptors, two interface descriptors, optional status
502 * endpoint. Both have a "data" interface and two bulk endpoints.
503 * There are also differences in how control requests are handled.
505 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
506 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
507 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
508 * work around those bugs) are unlikely to ever come from MSFT, you may
509 * wish to avoid using RNDIS.
511 * MCCI offers an alternative to RNDIS if you need to connect to Windows
512 * but have hardware that can't support CDC Ethernet. We add descriptors
513 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
514 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
515 * get those drivers from MCCI, or bundled with various products.
518 #ifdef DEV_CONFIG_CDC
519 static struct usb_interface_descriptor
521 .bLength = sizeof control_intf,
522 .bDescriptorType = USB_DT_INTERFACE,
524 .bInterfaceNumber = 0,
525 /* status endpoint is optional; this may be patched later */
527 .bInterfaceClass = USB_CLASS_COMM,
528 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
529 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
530 .iInterface = STRING_CONTROL,
534 #ifdef CONFIG_USB_ETH_RNDIS
535 static const struct usb_interface_descriptor
536 rndis_control_intf = {
537 .bLength = sizeof rndis_control_intf,
538 .bDescriptorType = USB_DT_INTERFACE,
540 .bInterfaceNumber = 0,
542 .bInterfaceClass = USB_CLASS_COMM,
543 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
544 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
545 .iInterface = STRING_RNDIS_CONTROL,
549 static const struct usb_cdc_header_desc header_desc = {
550 .bLength = sizeof header_desc,
551 .bDescriptorType = USB_DT_CS_INTERFACE,
552 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
554 .bcdCDC = __constant_cpu_to_le16 (0x0110),
557 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
559 static const struct usb_cdc_union_desc union_desc = {
560 .bLength = sizeof union_desc,
561 .bDescriptorType = USB_DT_CS_INTERFACE,
562 .bDescriptorSubType = USB_CDC_UNION_TYPE,
564 .bMasterInterface0 = 0, /* index of control interface */
565 .bSlaveInterface0 = 1, /* index of DATA interface */
568 #endif /* CDC || RNDIS */
570 #ifdef CONFIG_USB_ETH_RNDIS
572 static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
573 .bLength = sizeof call_mgmt_descriptor,
574 .bDescriptorType = USB_DT_CS_INTERFACE,
575 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
577 .bmCapabilities = 0x00,
578 .bDataInterface = 0x01,
581 static const struct usb_cdc_acm_descriptor acm_descriptor = {
582 .bLength = sizeof acm_descriptor,
583 .bDescriptorType = USB_DT_CS_INTERFACE,
584 .bDescriptorSubType = USB_CDC_ACM_TYPE,
586 .bmCapabilities = 0x00,
591 #ifndef DEV_CONFIG_CDC
593 /* "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
594 * ways: data endpoints live in the control interface, there's no data
595 * interface, and it's not used to talk to a cell phone radio.
598 static const struct usb_cdc_mdlm_desc mdlm_desc = {
599 .bLength = sizeof mdlm_desc,
600 .bDescriptorType = USB_DT_CS_INTERFACE,
601 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
603 .bcdVersion = __constant_cpu_to_le16(0x0100),
605 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
606 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
610 /* since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
611 * can't really use its struct. All we do here is say that we're using
612 * the submode of "SAFE" which directly matches the CDC Subset.
614 static const u8 mdlm_detail_desc[] = {
617 USB_CDC_MDLM_DETAIL_TYPE,
620 0, /* network control capabilities (none) */
621 0, /* network data capabilities ("raw" encapsulation) */
626 static const struct usb_cdc_ether_desc ether_desc = {
627 .bLength = sizeof ether_desc,
628 .bDescriptorType = USB_DT_CS_INTERFACE,
629 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
631 /* this descriptor actually adds value, surprise! */
632 .iMACAddress = STRING_ETHADDR,
633 .bmEthernetStatistics = __constant_cpu_to_le32 (0), /* no statistics */
634 .wMaxSegmentSize = __constant_cpu_to_le16 (ETH_FRAME_LEN),
635 .wNumberMCFilters = __constant_cpu_to_le16 (0),
636 .bNumberPowerFilters = 0,
640 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
642 /* include the status endpoint if we can, even where it's optional.
643 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
644 * packet, to simplify cancellation; and a big transfer interval, to
645 * waste less bandwidth.
647 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
648 * if they ignore the connect/disconnect notifications that real aether
649 * can provide. more advanced cdc configurations might want to support
650 * encapsulated commands (vendor-specific, using control-OUT).
652 * RNDIS requires the status endpoint, since it uses that encapsulation
653 * mechanism for its funky RPC scheme.
656 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
657 #define STATUS_BYTECOUNT 16 /* 8 byte header + data */
659 static struct usb_endpoint_descriptor
661 .bLength = USB_DT_ENDPOINT_SIZE,
662 .bDescriptorType = USB_DT_ENDPOINT,
664 .bEndpointAddress = USB_DIR_IN,
665 .bmAttributes = USB_ENDPOINT_XFER_INT,
666 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
667 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
671 #ifdef DEV_CONFIG_CDC
673 /* the default data interface has no endpoints ... */
675 static const struct usb_interface_descriptor
677 .bLength = sizeof data_nop_intf,
678 .bDescriptorType = USB_DT_INTERFACE,
680 .bInterfaceNumber = 1,
681 .bAlternateSetting = 0,
683 .bInterfaceClass = USB_CLASS_CDC_DATA,
684 .bInterfaceSubClass = 0,
685 .bInterfaceProtocol = 0,
688 /* ... but the "real" data interface has two bulk endpoints */
690 static const struct usb_interface_descriptor
692 .bLength = sizeof data_intf,
693 .bDescriptorType = USB_DT_INTERFACE,
695 .bInterfaceNumber = 1,
696 .bAlternateSetting = 1,
698 .bInterfaceClass = USB_CLASS_CDC_DATA,
699 .bInterfaceSubClass = 0,
700 .bInterfaceProtocol = 0,
701 .iInterface = STRING_DATA,
706 #ifdef CONFIG_USB_ETH_RNDIS
708 /* RNDIS doesn't activate by changing to the "real" altsetting */
710 static const struct usb_interface_descriptor
712 .bLength = sizeof rndis_data_intf,
713 .bDescriptorType = USB_DT_INTERFACE,
715 .bInterfaceNumber = 1,
716 .bAlternateSetting = 0,
718 .bInterfaceClass = USB_CLASS_CDC_DATA,
719 .bInterfaceSubClass = 0,
720 .bInterfaceProtocol = 0,
721 .iInterface = STRING_DATA,
726 #ifdef DEV_CONFIG_SUBSET
729 * "Simple" CDC-subset option is a simple vendor-neutral model that most
730 * full speed controllers can handle: one interface, two bulk endpoints.
732 * To assist host side drivers, we fancy it up a bit, and add descriptors
733 * so some host side drivers will understand it as a "SAFE" variant.
736 static const struct usb_interface_descriptor
738 .bLength = sizeof subset_data_intf,
739 .bDescriptorType = USB_DT_INTERFACE,
741 .bInterfaceNumber = 0,
742 .bAlternateSetting = 0,
744 .bInterfaceClass = USB_CLASS_COMM,
745 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
746 .bInterfaceProtocol = 0,
747 .iInterface = STRING_DATA,
753 static struct usb_endpoint_descriptor
755 .bLength = USB_DT_ENDPOINT_SIZE,
756 .bDescriptorType = USB_DT_ENDPOINT,
758 .bEndpointAddress = USB_DIR_IN,
759 .bmAttributes = USB_ENDPOINT_XFER_BULK,
762 static struct usb_endpoint_descriptor
764 .bLength = USB_DT_ENDPOINT_SIZE,
765 .bDescriptorType = USB_DT_ENDPOINT,
767 .bEndpointAddress = USB_DIR_OUT,
768 .bmAttributes = USB_ENDPOINT_XFER_BULK,
771 static const struct usb_descriptor_header *fs_eth_function [11] = {
772 (struct usb_descriptor_header *) &otg_descriptor,
773 #ifdef DEV_CONFIG_CDC
774 /* "cdc" mode descriptors */
775 (struct usb_descriptor_header *) &control_intf,
776 (struct usb_descriptor_header *) &header_desc,
777 (struct usb_descriptor_header *) &union_desc,
778 (struct usb_descriptor_header *) ðer_desc,
779 /* NOTE: status endpoint may need to be removed */
780 (struct usb_descriptor_header *) &fs_status_desc,
781 /* data interface, with altsetting */
782 (struct usb_descriptor_header *) &data_nop_intf,
783 (struct usb_descriptor_header *) &data_intf,
784 (struct usb_descriptor_header *) &fs_source_desc,
785 (struct usb_descriptor_header *) &fs_sink_desc,
787 #endif /* DEV_CONFIG_CDC */
790 static inline void __init fs_subset_descriptors(void)
792 #ifdef DEV_CONFIG_SUBSET
793 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
794 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
795 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
796 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
797 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
798 fs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
799 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
800 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
801 fs_eth_function[8] = NULL;
803 fs_eth_function[1] = NULL;
807 #ifdef CONFIG_USB_ETH_RNDIS
808 static const struct usb_descriptor_header *fs_rndis_function [] = {
809 (struct usb_descriptor_header *) &otg_descriptor,
810 /* control interface matches ACM, not Ethernet */
811 (struct usb_descriptor_header *) &rndis_control_intf,
812 (struct usb_descriptor_header *) &header_desc,
813 (struct usb_descriptor_header *) &call_mgmt_descriptor,
814 (struct usb_descriptor_header *) &acm_descriptor,
815 (struct usb_descriptor_header *) &union_desc,
816 (struct usb_descriptor_header *) &fs_status_desc,
817 /* data interface has no altsetting */
818 (struct usb_descriptor_header *) &rndis_data_intf,
819 (struct usb_descriptor_header *) &fs_source_desc,
820 (struct usb_descriptor_header *) &fs_sink_desc,
825 #ifdef CONFIG_USB_GADGET_DUALSPEED
828 * usb 2.0 devices need to expose both high speed and full speed
829 * descriptors, unless they only run at full speed.
832 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
833 static struct usb_endpoint_descriptor
835 .bLength = USB_DT_ENDPOINT_SIZE,
836 .bDescriptorType = USB_DT_ENDPOINT,
838 .bmAttributes = USB_ENDPOINT_XFER_INT,
839 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
840 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
842 #endif /* DEV_CONFIG_CDC */
844 static struct usb_endpoint_descriptor
846 .bLength = USB_DT_ENDPOINT_SIZE,
847 .bDescriptorType = USB_DT_ENDPOINT,
849 .bmAttributes = USB_ENDPOINT_XFER_BULK,
850 .wMaxPacketSize = __constant_cpu_to_le16 (512),
853 static struct usb_endpoint_descriptor
855 .bLength = USB_DT_ENDPOINT_SIZE,
856 .bDescriptorType = USB_DT_ENDPOINT,
858 .bmAttributes = USB_ENDPOINT_XFER_BULK,
859 .wMaxPacketSize = __constant_cpu_to_le16 (512),
862 static struct usb_qualifier_descriptor
864 .bLength = sizeof dev_qualifier,
865 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
867 .bcdUSB = __constant_cpu_to_le16 (0x0200),
868 .bDeviceClass = USB_CLASS_COMM,
870 .bNumConfigurations = 1,
873 static const struct usb_descriptor_header *hs_eth_function [11] = {
874 (struct usb_descriptor_header *) &otg_descriptor,
875 #ifdef DEV_CONFIG_CDC
876 /* "cdc" mode descriptors */
877 (struct usb_descriptor_header *) &control_intf,
878 (struct usb_descriptor_header *) &header_desc,
879 (struct usb_descriptor_header *) &union_desc,
880 (struct usb_descriptor_header *) ðer_desc,
881 /* NOTE: status endpoint may need to be removed */
882 (struct usb_descriptor_header *) &hs_status_desc,
883 /* data interface, with altsetting */
884 (struct usb_descriptor_header *) &data_nop_intf,
885 (struct usb_descriptor_header *) &data_intf,
886 (struct usb_descriptor_header *) &hs_source_desc,
887 (struct usb_descriptor_header *) &hs_sink_desc,
889 #endif /* DEV_CONFIG_CDC */
892 static inline void __init hs_subset_descriptors(void)
894 #ifdef DEV_CONFIG_SUBSET
895 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
896 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
897 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
898 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
899 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
900 hs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
901 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
902 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
903 hs_eth_function[8] = NULL;
905 hs_eth_function[1] = NULL;
909 #ifdef CONFIG_USB_ETH_RNDIS
910 static const struct usb_descriptor_header *hs_rndis_function [] = {
911 (struct usb_descriptor_header *) &otg_descriptor,
912 /* control interface matches ACM, not Ethernet */
913 (struct usb_descriptor_header *) &rndis_control_intf,
914 (struct usb_descriptor_header *) &header_desc,
915 (struct usb_descriptor_header *) &call_mgmt_descriptor,
916 (struct usb_descriptor_header *) &acm_descriptor,
917 (struct usb_descriptor_header *) &union_desc,
918 (struct usb_descriptor_header *) &hs_status_desc,
919 /* data interface has no altsetting */
920 (struct usb_descriptor_header *) &rndis_data_intf,
921 (struct usb_descriptor_header *) &hs_source_desc,
922 (struct usb_descriptor_header *) &hs_sink_desc,
928 /* maxpacket and other transfer characteristics vary by speed. */
929 #define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
933 /* if there's no high speed support, maxpacket doesn't change. */
934 #define ep_desc(g,hs,fs) (((void)(g)), (fs))
936 static inline void __init hs_subset_descriptors(void)
940 #endif /* !CONFIG_USB_GADGET_DUALSPEED */
942 /*-------------------------------------------------------------------------*/
944 /* descriptors that are built on-demand */
946 static char manufacturer [50];
947 static char product_desc [40] = DRIVER_DESC;
948 static char serial_number [20];
950 /* address that the host will use ... usually assigned at random */
951 static char ethaddr [2 * ETH_ALEN + 1];
953 /* static strings, in UTF-8 */
954 static struct usb_string strings [] = {
955 { STRING_MANUFACTURER, manufacturer, },
956 { STRING_PRODUCT, product_desc, },
957 { STRING_SERIALNUMBER, serial_number, },
958 { STRING_DATA, "Ethernet Data", },
959 { STRING_ETHADDR, ethaddr, },
960 #ifdef DEV_CONFIG_CDC
961 { STRING_CDC, "CDC Ethernet", },
962 { STRING_CONTROL, "CDC Communications Control", },
964 #ifdef DEV_CONFIG_SUBSET
965 { STRING_SUBSET, "CDC Ethernet Subset", },
967 #ifdef CONFIG_USB_ETH_RNDIS
968 { STRING_RNDIS, "RNDIS", },
969 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
971 { } /* end of list */
974 static struct usb_gadget_strings stringtab = {
975 .language = 0x0409, /* en-us */
980 * one config, two interfaces: control, data.
981 * complications: class descriptors, and an altsetting.
984 config_buf (enum usb_device_speed speed,
986 unsigned index, int is_otg)
989 const struct usb_config_descriptor *config;
990 const struct usb_descriptor_header **function;
991 #ifdef CONFIG_USB_GADGET_DUALSPEED
992 int hs = (speed == USB_SPEED_HIGH);
994 if (type == USB_DT_OTHER_SPEED_CONFIG)
996 #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
998 #define which_fn(t) (fs_ ## t ## _function)
1001 if (index >= device_desc.bNumConfigurations)
1004 #ifdef CONFIG_USB_ETH_RNDIS
1005 /* list the RNDIS config first, to make Microsoft's drivers
1006 * happy. DOCSIS 1.0 needs this too.
1008 if (device_desc.bNumConfigurations == 2 && index == 0) {
1009 config = &rndis_config;
1010 function = which_fn (rndis);
1014 config = ð_config;
1015 function = which_fn (eth);
1018 /* for now, don't advertise srp-only devices */
1022 len = usb_gadget_config_buf (config, buf, USB_BUFSIZ, function);
1025 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
1029 /*-------------------------------------------------------------------------*/
1031 static void eth_start (struct eth_dev *dev, gfp_t gfp_flags);
1032 static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
1035 set_ether_config (struct eth_dev *dev, gfp_t gfp_flags)
1038 struct usb_gadget *gadget = dev->gadget;
1040 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
1041 /* status endpoint used for RNDIS and (optionally) CDC */
1042 if (!subset_active(dev) && dev->status_ep) {
1043 dev->status = ep_desc (gadget, &hs_status_desc,
1045 dev->status_ep->driver_data = dev;
1047 result = usb_ep_enable (dev->status_ep, dev->status);
1049 DEBUG (dev, "enable %s --> %d\n",
1050 dev->status_ep->name, result);
1056 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
1057 dev->in_ep->driver_data = dev;
1059 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
1060 dev->out_ep->driver_data = dev;
1062 /* With CDC, the host isn't allowed to use these two data
1063 * endpoints in the default altsetting for the interface.
1064 * so we don't activate them yet. Reset from SET_INTERFACE.
1066 * Strictly speaking RNDIS should work the same: activation is
1067 * a side effect of setting a packet filter. Deactivation is
1068 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
1070 if (!cdc_active(dev)) {
1071 result = usb_ep_enable (dev->in_ep, dev->in);
1073 DEBUG(dev, "enable %s --> %d\n",
1074 dev->in_ep->name, result);
1078 result = usb_ep_enable (dev->out_ep, dev->out);
1080 DEBUG (dev, "enable %s --> %d\n",
1081 dev->out_ep->name, result);
1088 result = alloc_requests (dev, qlen (gadget), gfp_flags);
1090 /* on error, disable any endpoints */
1092 if (!subset_active(dev))
1093 (void) usb_ep_disable (dev->status_ep);
1095 (void) usb_ep_disable (dev->in_ep);
1096 (void) usb_ep_disable (dev->out_ep);
1101 /* activate non-CDC configs right away
1102 * this isn't strictly according to the RNDIS spec
1104 if (!cdc_active (dev)) {
1105 netif_carrier_on (dev->net);
1106 if (netif_running (dev->net)) {
1107 spin_unlock (&dev->lock);
1108 eth_start (dev, GFP_ATOMIC);
1109 spin_lock (&dev->lock);
1114 DEBUG (dev, "qlen %d\n", qlen (gadget));
1116 /* caller is responsible for cleanup on error */
1120 static void eth_reset_config (struct eth_dev *dev)
1122 struct usb_request *req;
1124 if (dev->config == 0)
1127 DEBUG (dev, "%s\n", __FUNCTION__);
1129 netif_stop_queue (dev->net);
1130 netif_carrier_off (dev->net);
1131 rndis_uninit(dev->rndis_config);
1133 /* disable endpoints, forcing (synchronous) completion of
1134 * pending i/o. then free the requests.
1137 usb_ep_disable (dev->in_ep);
1138 spin_lock(&dev->req_lock);
1139 while (likely (!list_empty (&dev->tx_reqs))) {
1140 req = container_of (dev->tx_reqs.next,
1141 struct usb_request, list);
1142 list_del (&req->list);
1144 spin_unlock(&dev->req_lock);
1145 usb_ep_free_request (dev->in_ep, req);
1146 spin_lock(&dev->req_lock);
1148 spin_unlock(&dev->req_lock);
1151 usb_ep_disable (dev->out_ep);
1152 spin_lock(&dev->req_lock);
1153 while (likely (!list_empty (&dev->rx_reqs))) {
1154 req = container_of (dev->rx_reqs.next,
1155 struct usb_request, list);
1156 list_del (&req->list);
1158 spin_unlock(&dev->req_lock);
1159 usb_ep_free_request (dev->out_ep, req);
1160 spin_lock(&dev->req_lock);
1162 spin_unlock(&dev->req_lock);
1166 usb_ep_disable (dev->status_ep);
1169 dev->cdc_filter = 0;
1173 /* change our operational config. must agree with the code
1174 * that returns config descriptors, and altsetting code.
1177 eth_set_config (struct eth_dev *dev, unsigned number, gfp_t gfp_flags)
1180 struct usb_gadget *gadget = dev->gadget;
1182 if (gadget_is_sa1100 (gadget)
1184 && atomic_read (&dev->tx_qlen) != 0) {
1185 /* tx fifo is full, but we can't clear it...*/
1186 INFO (dev, "can't change configurations\n");
1189 eth_reset_config (dev);
1192 case DEV_CONFIG_VALUE:
1193 result = set_ether_config (dev, gfp_flags);
1195 #ifdef CONFIG_USB_ETH_RNDIS
1196 case DEV_RNDIS_CONFIG_VALUE:
1198 result = set_ether_config (dev, gfp_flags);
1210 eth_reset_config (dev);
1211 usb_gadget_vbus_draw(dev->gadget,
1212 dev->gadget->is_otg ? 8 : 100);
1217 power = 2 * eth_config.bMaxPower;
1218 usb_gadget_vbus_draw(dev->gadget, power);
1220 switch (gadget->speed) {
1221 case USB_SPEED_FULL: speed = "full"; break;
1222 #ifdef CONFIG_USB_GADGET_DUALSPEED
1223 case USB_SPEED_HIGH: speed = "high"; break;
1225 default: speed = "?"; break;
1228 dev->config = number;
1229 INFO (dev, "%s speed config #%d: %d mA, %s, using %s\n",
1230 speed, number, power, driver_desc,
1235 : "CDC Ethernet Subset"));
1240 /*-------------------------------------------------------------------------*/
1242 #ifdef DEV_CONFIG_CDC
1244 /* The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
1245 * only to notify the host about link status changes (which we support) or
1246 * report completion of some encapsulated command (as used in RNDIS). Since
1247 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1248 * command mechanism; and only one status request is ever queued.
1251 static void eth_status_complete (struct usb_ep *ep, struct usb_request *req)
1253 struct usb_cdc_notification *event = req->buf;
1254 int value = req->status;
1255 struct eth_dev *dev = ep->driver_data;
1257 /* issue the second notification if host reads the first */
1258 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1260 __le32 *data = req->buf + sizeof *event;
1262 event->bmRequestType = 0xA1;
1263 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
1264 event->wValue = __constant_cpu_to_le16 (0);
1265 event->wIndex = __constant_cpu_to_le16 (1);
1266 event->wLength = __constant_cpu_to_le16 (8);
1268 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1269 data [0] = data [1] = cpu_to_le32 (BITRATE (dev->gadget));
1271 req->length = STATUS_BYTECOUNT;
1272 value = usb_ep_queue (ep, req, GFP_ATOMIC);
1273 DEBUG (dev, "send SPEED_CHANGE --> %d\n", value);
1276 } else if (value != -ECONNRESET)
1277 DEBUG (dev, "event %02x --> %d\n",
1278 event->bNotificationType, value);
1279 req->context = NULL;
1282 static void issue_start_status (struct eth_dev *dev)
1284 struct usb_request *req = dev->stat_req;
1285 struct usb_cdc_notification *event;
1288 DEBUG (dev, "%s, flush old status first\n", __FUNCTION__);
1292 * FIXME ugly idiom, maybe we'd be better with just
1293 * a "cancel the whole queue" primitive since any
1294 * unlink-one primitive has way too many error modes.
1295 * here, we "know" toggle is already clear...
1297 * FIXME iff req->context != null just dequeue it
1299 usb_ep_disable (dev->status_ep);
1300 usb_ep_enable (dev->status_ep, dev->status);
1302 /* 3.8.1 says to issue first NETWORK_CONNECTION, then
1303 * a SPEED_CHANGE. could be useful in some configs.
1306 event->bmRequestType = 0xA1;
1307 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
1308 event->wValue = __constant_cpu_to_le16 (1); /* connected */
1309 event->wIndex = __constant_cpu_to_le16 (1);
1312 req->length = sizeof *event;
1313 req->complete = eth_status_complete;
1316 value = usb_ep_queue (dev->status_ep, req, GFP_ATOMIC);
1318 DEBUG (dev, "status buf queue --> %d\n", value);
1323 /*-------------------------------------------------------------------------*/
1325 static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req)
1327 if (req->status || req->actual != req->length)
1328 DEBUG ((struct eth_dev *) ep->driver_data,
1329 "setup complete --> %d, %d/%d\n",
1330 req->status, req->actual, req->length);
1333 #ifdef CONFIG_USB_ETH_RNDIS
1335 static void rndis_response_complete (struct usb_ep *ep, struct usb_request *req)
1337 if (req->status || req->actual != req->length)
1338 DEBUG ((struct eth_dev *) ep->driver_data,
1339 "rndis response complete --> %d, %d/%d\n",
1340 req->status, req->actual, req->length);
1342 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1345 static void rndis_command_complete (struct usb_ep *ep, struct usb_request *req)
1347 struct eth_dev *dev = ep->driver_data;
1350 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1351 spin_lock(&dev->lock);
1352 status = rndis_msg_parser (dev->rndis_config, (u8 *) req->buf);
1354 ERROR(dev, "%s: rndis parse error %d\n", __FUNCTION__, status);
1355 spin_unlock(&dev->lock);
1361 * The setup() callback implements all the ep0 functionality that's not
1362 * handled lower down. CDC has a number of less-common features:
1364 * - two interfaces: control, and ethernet data
1365 * - Ethernet data interface has two altsettings: default, and active
1366 * - class-specific descriptors for the control interface
1367 * - class-specific control requests
1370 eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1372 struct eth_dev *dev = get_gadget_data (gadget);
1373 struct usb_request *req = dev->req;
1374 int value = -EOPNOTSUPP;
1375 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1376 u16 wValue = le16_to_cpu(ctrl->wValue);
1377 u16 wLength = le16_to_cpu(ctrl->wLength);
1379 /* descriptors just go into the pre-allocated ep0 buffer,
1380 * while config change events may enable network traffic.
1382 req->complete = eth_setup_complete;
1383 switch (ctrl->bRequest) {
1385 case USB_REQ_GET_DESCRIPTOR:
1386 if (ctrl->bRequestType != USB_DIR_IN)
1388 switch (wValue >> 8) {
1391 value = min (wLength, (u16) sizeof device_desc);
1392 memcpy (req->buf, &device_desc, value);
1394 #ifdef CONFIG_USB_GADGET_DUALSPEED
1395 case USB_DT_DEVICE_QUALIFIER:
1396 if (!gadget->is_dualspeed)
1398 value = min (wLength, (u16) sizeof dev_qualifier);
1399 memcpy (req->buf, &dev_qualifier, value);
1402 case USB_DT_OTHER_SPEED_CONFIG:
1403 if (!gadget->is_dualspeed)
1406 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1408 value = config_buf (gadget->speed, req->buf,
1413 value = min (wLength, (u16) value);
1417 value = usb_gadget_get_string (&stringtab,
1418 wValue & 0xff, req->buf);
1420 value = min (wLength, (u16) value);
1425 case USB_REQ_SET_CONFIGURATION:
1426 if (ctrl->bRequestType != 0)
1428 if (gadget->a_hnp_support)
1429 DEBUG (dev, "HNP available\n");
1430 else if (gadget->a_alt_hnp_support)
1431 DEBUG (dev, "HNP needs a different root port\n");
1432 spin_lock (&dev->lock);
1433 value = eth_set_config (dev, wValue, GFP_ATOMIC);
1434 spin_unlock (&dev->lock);
1436 case USB_REQ_GET_CONFIGURATION:
1437 if (ctrl->bRequestType != USB_DIR_IN)
1439 *(u8 *)req->buf = dev->config;
1440 value = min (wLength, (u16) 1);
1443 case USB_REQ_SET_INTERFACE:
1444 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1448 if (!cdc_active(dev) && wIndex != 0)
1450 spin_lock (&dev->lock);
1452 /* PXA hardware partially handles SET_INTERFACE;
1453 * we need to kluge around that interference.
1455 if (gadget_is_pxa (gadget)) {
1456 value = eth_set_config (dev, DEV_CONFIG_VALUE,
1461 #ifdef DEV_CONFIG_CDC
1463 case 0: /* control/master intf */
1467 usb_ep_disable (dev->status_ep);
1468 usb_ep_enable (dev->status_ep, dev->status);
1472 case 1: /* data intf */
1475 usb_ep_disable (dev->in_ep);
1476 usb_ep_disable (dev->out_ep);
1478 /* CDC requires the data transfers not be done from
1479 * the default interface setting ... also, setting
1480 * the non-default interface resets filters etc.
1483 if (!cdc_active (dev))
1485 usb_ep_enable (dev->in_ep, dev->in);
1486 usb_ep_enable (dev->out_ep, dev->out);
1487 dev->cdc_filter = DEFAULT_FILTER;
1488 netif_carrier_on (dev->net);
1490 issue_start_status (dev);
1491 if (netif_running (dev->net)) {
1492 spin_unlock (&dev->lock);
1493 eth_start (dev, GFP_ATOMIC);
1494 spin_lock (&dev->lock);
1497 netif_stop_queue (dev->net);
1498 netif_carrier_off (dev->net);
1504 /* FIXME this is wrong, as is the assumption that
1505 * all non-PXA hardware talks real CDC ...
1507 dev_warn (&gadget->dev, "set_interface ignored!\n");
1508 #endif /* DEV_CONFIG_CDC */
1511 spin_unlock (&dev->lock);
1513 case USB_REQ_GET_INTERFACE:
1514 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1518 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
1521 /* for CDC, iff carrier is on, data interface is active. */
1522 if (rndis_active(dev) || wIndex != 1)
1523 *(u8 *)req->buf = 0;
1525 *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0;
1526 value = min (wLength, (u16) 1);
1529 #ifdef DEV_CONFIG_CDC
1530 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
1531 /* see 6.2.30: no data, wIndex = interface,
1532 * wValue = packet filter bitmap
1534 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1539 DEBUG (dev, "packet filter %02x\n", wValue);
1540 dev->cdc_filter = wValue;
1545 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1546 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1547 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1548 * case USB_CDC_GET_ETHERNET_STATISTIC:
1551 #endif /* DEV_CONFIG_CDC */
1553 #ifdef CONFIG_USB_ETH_RNDIS
1554 /* RNDIS uses the CDC command encapsulation mechanism to implement
1555 * an RPC scheme, with much getting/setting of attributes by OID.
1557 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1558 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1559 || !rndis_active(dev)
1560 || wLength > USB_BUFSIZ
1562 || rndis_control_intf.bInterfaceNumber
1565 /* read the request, then process it */
1567 req->complete = rndis_command_complete;
1568 /* later, rndis_control_ack () sends a notification */
1571 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1572 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1573 == ctrl->bRequestType
1574 && rndis_active(dev)
1575 // && wLength >= 0x0400
1577 && rndis_control_intf.bInterfaceNumber
1581 /* return the result */
1582 buf = rndis_get_next_response (dev->rndis_config,
1585 memcpy (req->buf, buf, value);
1586 req->complete = rndis_response_complete;
1587 rndis_free_response(dev->rndis_config, buf);
1589 /* else stalls ... spec says to avoid that */
1596 "unknown control req%02x.%02x v%04x i%04x l%d\n",
1597 ctrl->bRequestType, ctrl->bRequest,
1598 wValue, wIndex, wLength);
1601 /* respond with data transfer before status phase? */
1603 req->length = value;
1604 req->zero = value < wLength
1605 && (value % gadget->ep0->maxpacket) == 0;
1606 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1608 DEBUG (dev, "ep_queue --> %d\n", value);
1610 eth_setup_complete (gadget->ep0, req);
1614 /* host either stalls (value < 0) or reports success */
1619 eth_disconnect (struct usb_gadget *gadget)
1621 struct eth_dev *dev = get_gadget_data (gadget);
1622 unsigned long flags;
1624 spin_lock_irqsave (&dev->lock, flags);
1625 netif_stop_queue (dev->net);
1626 netif_carrier_off (dev->net);
1627 eth_reset_config (dev);
1628 spin_unlock_irqrestore (&dev->lock, flags);
1630 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1632 /* next we may get setup() calls to enumerate new connections;
1633 * or an unbind() during shutdown (including removing module).
1637 /*-------------------------------------------------------------------------*/
1639 /* NETWORK DRIVER HOOKUP (to the layer above this driver) */
1641 static int eth_change_mtu (struct net_device *net, int new_mtu)
1643 struct eth_dev *dev = netdev_priv(net);
1648 if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN)
1650 /* no zero-length packet read wanted after mtu-sized packets */
1651 if (((new_mtu + sizeof (struct ethhdr)) % dev->in_ep->maxpacket) == 0)
1657 static struct net_device_stats *eth_get_stats (struct net_device *net)
1659 return &((struct eth_dev *)netdev_priv(net))->stats;
1662 static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
1664 struct eth_dev *dev = netdev_priv(net);
1665 strlcpy(p->driver, shortname, sizeof p->driver);
1666 strlcpy(p->version, DRIVER_VERSION, sizeof p->version);
1667 strlcpy(p->fw_version, dev->gadget->name, sizeof p->fw_version);
1668 strlcpy (p->bus_info, dev->gadget->dev.bus_id, sizeof p->bus_info);
1671 static u32 eth_get_link(struct net_device *net)
1673 struct eth_dev *dev = netdev_priv(net);
1674 return dev->gadget->speed != USB_SPEED_UNKNOWN;
1677 static struct ethtool_ops ops = {
1678 .get_drvinfo = eth_get_drvinfo,
1679 .get_link = eth_get_link
1682 static void defer_kevent (struct eth_dev *dev, int flag)
1684 if (test_and_set_bit (flag, &dev->todo))
1686 if (!schedule_work (&dev->work))
1687 ERROR (dev, "kevent %d may have been dropped\n", flag);
1689 DEBUG (dev, "kevent %d scheduled\n", flag);
1692 static void rx_complete (struct usb_ep *ep, struct usb_request *req);
1695 rx_submit (struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
1697 struct sk_buff *skb;
1698 int retval = -ENOMEM;
1701 /* Padding up to RX_EXTRA handles minor disagreements with host.
1702 * Normally we use the USB "terminate on short read" convention;
1703 * so allow up to (N*maxpacket), since that memory is normally
1704 * already allocated. Some hardware doesn't deal well with short
1705 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1706 * byte off the end (to force hardware errors on overflow).
1708 * RNDIS uses internal framing, and explicitly allows senders to
1709 * pad to end-of-packet. That's potentially nice for speed,
1710 * but means receivers can't recover synch on their own.
1712 size = (sizeof (struct ethhdr) + dev->net->mtu + RX_EXTRA);
1713 size += dev->out_ep->maxpacket - 1;
1714 if (rndis_active(dev))
1715 size += sizeof (struct rndis_packet_msg_type);
1716 size -= size % dev->out_ep->maxpacket;
1718 if ((skb = alloc_skb (size + NET_IP_ALIGN, gfp_flags)) == 0) {
1719 DEBUG (dev, "no rx skb\n");
1723 /* Some platforms perform better when IP packets are aligned,
1724 * but on at least one, checksumming fails otherwise. Note:
1725 * RNDIS headers involve variable numbers of LE32 values.
1727 skb_reserve(skb, NET_IP_ALIGN);
1729 req->buf = skb->data;
1731 req->complete = rx_complete;
1734 retval = usb_ep_queue (dev->out_ep, req, gfp_flags);
1735 if (retval == -ENOMEM)
1737 defer_kevent (dev, WORK_RX_MEMORY);
1739 DEBUG (dev, "rx submit --> %d\n", retval);
1741 dev_kfree_skb_any(skb);
1742 spin_lock(&dev->req_lock);
1743 list_add (&req->list, &dev->rx_reqs);
1744 spin_unlock(&dev->req_lock);
1749 static void rx_complete (struct usb_ep *ep, struct usb_request *req)
1751 struct sk_buff *skb = req->context;
1752 struct eth_dev *dev = ep->driver_data;
1753 int status = req->status;
1757 /* normal completion */
1759 skb_put (skb, req->actual);
1760 /* we know MaxPacketsPerTransfer == 1 here */
1761 if (rndis_active(dev))
1762 status = rndis_rm_hdr (skb);
1764 || ETH_HLEN > skb->len
1765 || skb->len > ETH_FRAME_LEN) {
1766 dev->stats.rx_errors++;
1767 dev->stats.rx_length_errors++;
1768 DEBUG (dev, "rx length %d\n", skb->len);
1772 skb->protocol = eth_type_trans (skb, dev->net);
1773 dev->stats.rx_packets++;
1774 dev->stats.rx_bytes += skb->len;
1776 /* no buffer copies needed, unless hardware can't
1779 status = netif_rx (skb);
1783 /* software-driven interface shutdown */
1784 case -ECONNRESET: // unlink
1785 case -ESHUTDOWN: // disconnect etc
1786 VDEBUG (dev, "rx shutdown, code %d\n", status);
1789 /* for hardware automagic (such as pxa) */
1790 case -ECONNABORTED: // endpoint reset
1791 DEBUG (dev, "rx %s reset\n", ep->name);
1792 defer_kevent (dev, WORK_RX_MEMORY);
1794 dev_kfree_skb_any (skb);
1799 dev->stats.rx_over_errors++;
1803 dev->stats.rx_errors++;
1804 DEBUG (dev, "rx status %d\n", status);
1809 dev_kfree_skb_any (skb);
1810 if (!netif_running (dev->net)) {
1812 spin_lock(&dev->req_lock);
1813 list_add (&req->list, &dev->rx_reqs);
1814 spin_unlock(&dev->req_lock);
1818 rx_submit (dev, req, GFP_ATOMIC);
1821 static int prealloc (struct list_head *list, struct usb_ep *ep,
1822 unsigned n, gfp_t gfp_flags)
1825 struct usb_request *req;
1830 /* queue/recycle up to N requests */
1832 list_for_each_entry (req, list, list) {
1837 req = usb_ep_alloc_request (ep, gfp_flags);
1839 return list_empty (list) ? -ENOMEM : 0;
1840 list_add (&req->list, list);
1847 struct list_head *next;
1849 next = req->list.next;
1850 list_del (&req->list);
1851 usb_ep_free_request (ep, req);
1856 req = container_of (next, struct usb_request, list);
1861 static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
1865 spin_lock(&dev->req_lock);
1866 status = prealloc (&dev->tx_reqs, dev->in_ep, n, gfp_flags);
1869 status = prealloc (&dev->rx_reqs, dev->out_ep, n, gfp_flags);
1874 DEBUG (dev, "can't alloc requests\n");
1876 spin_unlock(&dev->req_lock);
1880 static void rx_fill (struct eth_dev *dev, gfp_t gfp_flags)
1882 struct usb_request *req;
1883 unsigned long flags;
1885 /* fill unused rxq slots with some skb */
1886 spin_lock_irqsave(&dev->req_lock, flags);
1887 while (!list_empty (&dev->rx_reqs)) {
1888 req = container_of (dev->rx_reqs.next,
1889 struct usb_request, list);
1890 list_del_init (&req->list);
1891 spin_unlock_irqrestore(&dev->req_lock, flags);
1893 if (rx_submit (dev, req, gfp_flags) < 0) {
1894 defer_kevent (dev, WORK_RX_MEMORY);
1898 spin_lock_irqsave(&dev->req_lock, flags);
1900 spin_unlock_irqrestore(&dev->req_lock, flags);
1903 static void eth_work (struct work_struct *work)
1905 struct eth_dev *dev = container_of(work, struct eth_dev, work);
1907 if (test_and_clear_bit (WORK_RX_MEMORY, &dev->todo)) {
1908 if (netif_running (dev->net))
1909 rx_fill (dev, GFP_KERNEL);
1913 DEBUG (dev, "work done, flags = 0x%lx\n", dev->todo);
1916 static void tx_complete (struct usb_ep *ep, struct usb_request *req)
1918 struct sk_buff *skb = req->context;
1919 struct eth_dev *dev = ep->driver_data;
1921 switch (req->status) {
1923 dev->stats.tx_errors++;
1924 VDEBUG (dev, "tx err %d\n", req->status);
1926 case -ECONNRESET: // unlink
1927 case -ESHUTDOWN: // disconnect etc
1930 dev->stats.tx_bytes += skb->len;
1932 dev->stats.tx_packets++;
1934 spin_lock(&dev->req_lock);
1935 list_add (&req->list, &dev->tx_reqs);
1936 spin_unlock(&dev->req_lock);
1937 dev_kfree_skb_any (skb);
1939 atomic_dec (&dev->tx_qlen);
1940 if (netif_carrier_ok (dev->net))
1941 netif_wake_queue (dev->net);
1944 static inline int eth_is_promisc (struct eth_dev *dev)
1946 /* no filters for the CDC subset; always promisc */
1947 if (subset_active (dev))
1949 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1952 static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1954 struct eth_dev *dev = netdev_priv(net);
1955 int length = skb->len;
1957 struct usb_request *req = NULL;
1958 unsigned long flags;
1960 /* apply outgoing CDC or RNDIS filters */
1961 if (!eth_is_promisc (dev)) {
1962 u8 *dest = skb->data;
1964 if (is_multicast_ether_addr(dest)) {
1967 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1968 * SET_ETHERNET_MULTICAST_FILTERS requests
1970 if (is_broadcast_ether_addr(dest))
1971 type = USB_CDC_PACKET_TYPE_BROADCAST;
1973 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1974 if (!(dev->cdc_filter & type)) {
1975 dev_kfree_skb_any (skb);
1979 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1982 spin_lock_irqsave(&dev->req_lock, flags);
1983 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1984 list_del (&req->list);
1985 if (list_empty (&dev->tx_reqs))
1986 netif_stop_queue (net);
1987 spin_unlock_irqrestore(&dev->req_lock, flags);
1989 /* no buffer copies needed, unless the network stack did it
1990 * or the hardware can't use skb buffers.
1991 * or there's not enough space for any RNDIS headers we need
1993 if (rndis_active(dev)) {
1994 struct sk_buff *skb_rndis;
1996 skb_rndis = skb_realloc_headroom (skb,
1997 sizeof (struct rndis_packet_msg_type));
2001 dev_kfree_skb_any (skb);
2003 rndis_add_hdr (skb);
2006 req->buf = skb->data;
2008 req->complete = tx_complete;
2010 /* use zlp framing on tx for strict CDC-Ether conformance,
2011 * though any robust network rx path ignores extra padding.
2012 * and some hardware doesn't like to write zlps.
2015 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2018 req->length = length;
2020 #ifdef CONFIG_USB_GADGET_DUALSPEED
2021 /* throttle highspeed IRQ rate back slightly */
2022 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2023 ? ((atomic_read (&dev->tx_qlen) % TX_DELAY) != 0)
2027 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
2030 DEBUG (dev, "tx queue err %d\n", retval);
2033 net->trans_start = jiffies;
2034 atomic_inc (&dev->tx_qlen);
2039 dev->stats.tx_dropped++;
2040 dev_kfree_skb_any (skb);
2041 spin_lock_irqsave(&dev->req_lock, flags);
2042 if (list_empty (&dev->tx_reqs))
2043 netif_start_queue (net);
2044 list_add (&req->list, &dev->tx_reqs);
2045 spin_unlock_irqrestore(&dev->req_lock, flags);
2050 /*-------------------------------------------------------------------------*/
2052 #ifdef CONFIG_USB_ETH_RNDIS
2054 /* The interrupt endpoint is used in RNDIS to notify the host when messages
2055 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
2056 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
2057 * REMOTE_NDIS_KEEPALIVE_MSG.
2059 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
2060 * normally just one notification will be queued.
2063 static struct usb_request *eth_req_alloc (struct usb_ep *, unsigned, gfp_t);
2064 static void eth_req_free (struct usb_ep *ep, struct usb_request *req);
2067 rndis_control_ack_complete (struct usb_ep *ep, struct usb_request *req)
2069 struct eth_dev *dev = ep->driver_data;
2071 if (req->status || req->actual != req->length)
2073 "rndis control ack complete --> %d, %d/%d\n",
2074 req->status, req->actual, req->length);
2075 req->context = NULL;
2077 if (req != dev->stat_req)
2078 eth_req_free(ep, req);
2081 static int rndis_control_ack (struct net_device *net)
2083 struct eth_dev *dev = netdev_priv(net);
2085 struct usb_request *resp = dev->stat_req;
2087 /* in case RNDIS calls this after disconnect */
2089 DEBUG (dev, "status ENODEV\n");
2093 /* in case queue length > 1 */
2094 if (resp->context) {
2095 resp = eth_req_alloc (dev->status_ep, 8, GFP_ATOMIC);
2100 /* Send RNDIS RESPONSE_AVAILABLE notification;
2101 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
2104 resp->complete = rndis_control_ack_complete;
2105 resp->context = dev;
2107 *((__le32 *) resp->buf) = __constant_cpu_to_le32 (1);
2108 *((__le32 *) resp->buf + 1) = __constant_cpu_to_le32 (0);
2110 length = usb_ep_queue (dev->status_ep, resp, GFP_ATOMIC);
2113 rndis_control_ack_complete (dev->status_ep, resp);
2121 #define rndis_control_ack NULL
2125 static void eth_start (struct eth_dev *dev, gfp_t gfp_flags)
2127 DEBUG (dev, "%s\n", __FUNCTION__);
2129 /* fill the rx queue */
2130 rx_fill (dev, gfp_flags);
2132 /* and open the tx floodgates */
2133 atomic_set (&dev->tx_qlen, 0);
2134 netif_wake_queue (dev->net);
2135 if (rndis_active(dev)) {
2136 rndis_set_param_medium (dev->rndis_config,
2138 BITRATE(dev->gadget)/100);
2139 (void) rndis_signal_connect (dev->rndis_config);
2143 static int eth_open (struct net_device *net)
2145 struct eth_dev *dev = netdev_priv(net);
2147 DEBUG (dev, "%s\n", __FUNCTION__);
2148 if (netif_carrier_ok (dev->net))
2149 eth_start (dev, GFP_KERNEL);
2153 static int eth_stop (struct net_device *net)
2155 struct eth_dev *dev = netdev_priv(net);
2157 VDEBUG (dev, "%s\n", __FUNCTION__);
2158 netif_stop_queue (net);
2160 DEBUG (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
2161 dev->stats.rx_packets, dev->stats.tx_packets,
2162 dev->stats.rx_errors, dev->stats.tx_errors
2165 /* ensure there are no more active requests */
2167 usb_ep_disable (dev->in_ep);
2168 usb_ep_disable (dev->out_ep);
2169 if (netif_carrier_ok (dev->net)) {
2170 DEBUG (dev, "host still using in/out endpoints\n");
2171 // FIXME idiom may leave toggle wrong here
2172 usb_ep_enable (dev->in_ep, dev->in);
2173 usb_ep_enable (dev->out_ep, dev->out);
2175 if (dev->status_ep) {
2176 usb_ep_disable (dev->status_ep);
2177 usb_ep_enable (dev->status_ep, dev->status);
2181 if (rndis_active(dev)) {
2182 rndis_set_param_medium (dev->rndis_config,
2183 NDIS_MEDIUM_802_3, 0);
2184 (void) rndis_signal_disconnect (dev->rndis_config);
2190 /*-------------------------------------------------------------------------*/
2192 static struct usb_request *
2193 eth_req_alloc (struct usb_ep *ep, unsigned size, gfp_t gfp_flags)
2195 struct usb_request *req;
2197 req = usb_ep_alloc_request (ep, gfp_flags);
2201 req->buf = kmalloc (size, gfp_flags);
2203 usb_ep_free_request (ep, req);
2210 eth_req_free (struct usb_ep *ep, struct usb_request *req)
2213 usb_ep_free_request (ep, req);
2217 static void /* __init_or_exit */
2218 eth_unbind (struct usb_gadget *gadget)
2220 struct eth_dev *dev = get_gadget_data (gadget);
2222 DEBUG (dev, "unbind\n");
2223 rndis_deregister (dev->rndis_config);
2226 /* we've already been disconnected ... no i/o is active */
2228 eth_req_free (gadget->ep0, dev->req);
2231 if (dev->stat_req) {
2232 eth_req_free (dev->status_ep, dev->stat_req);
2233 dev->stat_req = NULL;
2236 unregister_netdev (dev->net);
2237 free_netdev(dev->net);
2239 /* assuming we used keventd, it must quiesce too */
2240 flush_scheduled_work ();
2241 set_gadget_data (gadget, NULL);
2244 static u8 __devinit nibble (unsigned char c)
2246 if (likely (isdigit (c)))
2249 if (likely (isxdigit (c)))
2250 return 10 + c - 'A';
2254 static int __devinit get_ether_addr(const char *str, u8 *dev_addr)
2259 for (i = 0; i < 6; i++) {
2262 if((*str == '.') || (*str == ':'))
2264 num = nibble(*str++) << 4;
2265 num |= (nibble(*str++));
2268 if (is_valid_ether_addr (dev_addr))
2271 random_ether_addr(dev_addr);
2275 static int __devinit
2276 eth_bind (struct usb_gadget *gadget)
2278 struct eth_dev *dev;
2279 struct net_device *net;
2280 u8 cdc = 1, zlp = 1, rndis = 1;
2281 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
2282 int status = -ENOMEM;
2285 /* these flags are only ever cleared; compiler take note */
2286 #ifndef DEV_CONFIG_CDC
2289 #ifndef CONFIG_USB_ETH_RNDIS
2293 /* Because most host side USB stacks handle CDC Ethernet, that
2294 * standard protocol is _strongly_ preferred for interop purposes.
2295 * (By everyone except Microsoft.)
2297 if (gadget_is_pxa (gadget)) {
2298 /* pxa doesn't support altsettings */
2300 } else if (gadget_is_musbhdrc(gadget)) {
2301 /* reduce tx dma overhead by avoiding special cases */
2303 } else if (gadget_is_sh(gadget)) {
2304 /* sh doesn't support multiple interfaces or configs */
2307 } else if (gadget_is_sa1100 (gadget)) {
2308 /* hardware can't write zlps */
2310 /* sa1100 CAN do CDC, without status endpoint ... we use
2311 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2316 gcnum = usb_gadget_controller_number (gadget);
2318 device_desc.bcdDevice = cpu_to_le16 (0x0200 + gcnum);
2320 /* can't assume CDC works. don't want to default to
2321 * anything less functional on CDC-capable hardware,
2322 * so we fail in this case.
2324 dev_err (&gadget->dev,
2325 "controller '%s' not recognized\n",
2329 snprintf (manufacturer, sizeof manufacturer, "%s %s/%s",
2330 init_utsname()->sysname, init_utsname()->release,
2333 /* If there's an RNDIS configuration, that's what Windows wants to
2334 * be using ... so use these product IDs here and in the "linux.inf"
2335 * needed to install MSFT drivers. Current Linux kernels will use
2336 * the second configuration if it's CDC Ethernet, and need some help
2337 * to choose the right configuration otherwise.
2340 device_desc.idVendor =
2341 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2342 device_desc.idProduct =
2343 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2344 snprintf (product_desc, sizeof product_desc,
2345 "RNDIS/%s", driver_desc);
2347 /* CDC subset ... recognized by Linux since 2.4.10, but Windows
2348 * drivers aren't widely available. (That may be improved by
2349 * supporting one submode of the "SAFE" variant of MDLM.)
2352 device_desc.idVendor =
2353 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2354 device_desc.idProduct =
2355 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2358 /* support optional vendor/distro customization */
2361 dev_err (&gadget->dev, "idVendor needs idProduct!\n");
2364 device_desc.idVendor = cpu_to_le16(idVendor);
2365 device_desc.idProduct = cpu_to_le16(idProduct);
2367 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2370 strlcpy (manufacturer, iManufacturer, sizeof manufacturer);
2372 strlcpy (product_desc, iProduct, sizeof product_desc);
2373 if (iSerialNumber) {
2374 device_desc.iSerialNumber = STRING_SERIALNUMBER,
2375 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
2378 /* all we really need is bulk IN/OUT */
2379 usb_ep_autoconfig_reset (gadget);
2380 in_ep = usb_ep_autoconfig (gadget, &fs_source_desc);
2383 dev_err (&gadget->dev,
2384 "can't autoconfigure on %s\n",
2388 in_ep->driver_data = in_ep; /* claim */
2390 out_ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
2393 out_ep->driver_data = out_ep; /* claim */
2395 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2396 /* CDC Ethernet control interface doesn't require a status endpoint.
2397 * Since some hosts expect one, try to allocate one anyway.
2400 status_ep = usb_ep_autoconfig (gadget, &fs_status_desc);
2402 status_ep->driver_data = status_ep; /* claim */
2404 dev_err (&gadget->dev,
2405 "can't run RNDIS on %s\n",
2408 #ifdef DEV_CONFIG_CDC
2409 /* pxa25x only does CDC subset; often used with RNDIS */
2411 control_intf.bNumEndpoints = 0;
2412 /* FIXME remove endpoint from descriptor list */
2418 /* one config: cdc, else minimal subset */
2420 eth_config.bNumInterfaces = 1;
2421 eth_config.iConfiguration = STRING_SUBSET;
2423 /* use functions to set these up, in case we're built to work
2424 * with multiple controllers and must override CDC Ethernet.
2426 fs_subset_descriptors();
2427 hs_subset_descriptors();
2430 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
2431 usb_gadget_set_selfpowered (gadget);
2433 /* For now RNDIS is always a second config */
2435 device_desc.bNumConfigurations = 2;
2437 #ifdef CONFIG_USB_GADGET_DUALSPEED
2439 dev_qualifier.bNumConfigurations = 2;
2441 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2443 /* assumes ep0 uses the same value for both speeds ... */
2444 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2446 /* and that all endpoints are dual-speed */
2447 hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
2448 hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
2449 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2451 hs_status_desc.bEndpointAddress =
2452 fs_status_desc.bEndpointAddress;
2454 #endif /* DUALSPEED */
2456 if (gadget->is_otg) {
2457 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2458 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2459 eth_config.bMaxPower = 4;
2460 #ifdef CONFIG_USB_ETH_RNDIS
2461 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2462 rndis_config.bMaxPower = 4;
2466 net = alloc_etherdev (sizeof *dev);
2469 dev = netdev_priv(net);
2470 spin_lock_init (&dev->lock);
2471 spin_lock_init (&dev->req_lock);
2472 INIT_WORK (&dev->work, eth_work);
2473 INIT_LIST_HEAD (&dev->tx_reqs);
2474 INIT_LIST_HEAD (&dev->rx_reqs);
2476 /* network device setup */
2478 SET_MODULE_OWNER (net);
2479 strcpy (net->name, "usb%d");
2484 dev->out_ep = out_ep;
2485 dev->status_ep = status_ep;
2487 /* Module params for these addresses should come from ID proms.
2488 * The host side address is used with CDC and RNDIS, and commonly
2489 * ends up in a persistent config database. It's not clear if
2490 * host side code for the SAFE thing cares -- its original BLAN
2491 * thing didn't, Sharp never assigned those addresses on Zaurii.
2493 if (get_ether_addr(dev_addr, net->dev_addr))
2494 dev_warn(&gadget->dev,
2495 "using random %s ethernet address\n", "self");
2496 if (get_ether_addr(host_addr, dev->host_mac))
2497 dev_warn(&gadget->dev,
2498 "using random %s ethernet address\n", "host");
2499 snprintf (ethaddr, sizeof ethaddr, "%02X%02X%02X%02X%02X%02X",
2500 dev->host_mac [0], dev->host_mac [1],
2501 dev->host_mac [2], dev->host_mac [3],
2502 dev->host_mac [4], dev->host_mac [5]);
2505 status = rndis_init();
2507 dev_err (&gadget->dev, "can't init RNDIS, %d\n",
2513 net->change_mtu = eth_change_mtu;
2514 net->get_stats = eth_get_stats;
2515 net->hard_start_xmit = eth_start_xmit;
2516 net->open = eth_open;
2517 net->stop = eth_stop;
2518 // watchdog_timeo, tx_timeout ...
2519 // set_multicast_list
2520 SET_ETHTOOL_OPS(net, &ops);
2522 /* preallocate control message data and buffer */
2523 dev->req = eth_req_alloc (gadget->ep0, USB_BUFSIZ, GFP_KERNEL);
2526 dev->req->complete = eth_setup_complete;
2528 /* ... and maybe likewise for status transfer */
2529 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2530 if (dev->status_ep) {
2531 dev->stat_req = eth_req_alloc (dev->status_ep,
2532 STATUS_BYTECOUNT, GFP_KERNEL);
2533 if (!dev->stat_req) {
2534 eth_req_free (gadget->ep0, dev->req);
2537 dev->stat_req->context = NULL;
2541 /* finish hookup to lower layer ... */
2542 dev->gadget = gadget;
2543 set_gadget_data (gadget, dev);
2544 gadget->ep0->driver_data = dev;
2546 /* two kinds of host-initiated state changes:
2547 * - iff DATA transfer is active, carrier is "on"
2548 * - tx queueing enabled if open *and* carrier is "on"
2550 netif_stop_queue (dev->net);
2551 netif_carrier_off (dev->net);
2553 SET_NETDEV_DEV (dev->net, &gadget->dev);
2554 status = register_netdev (dev->net);
2558 INFO (dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
2559 INFO (dev, "using %s, OUT %s IN %s%s%s\n", gadget->name,
2560 out_ep->name, in_ep->name,
2561 status_ep ? " STATUS " : "",
2562 status_ep ? status_ep->name : ""
2564 INFO (dev, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2565 net->dev_addr [0], net->dev_addr [1],
2566 net->dev_addr [2], net->dev_addr [3],
2567 net->dev_addr [4], net->dev_addr [5]);
2570 INFO (dev, "HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2571 dev->host_mac [0], dev->host_mac [1],
2572 dev->host_mac [2], dev->host_mac [3],
2573 dev->host_mac [4], dev->host_mac [5]);
2578 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2580 dev->rndis_config = rndis_register (rndis_control_ack);
2581 if (dev->rndis_config < 0) {
2583 unregister_netdev (dev->net);
2588 /* these set up a lot of the OIDs that RNDIS needs */
2589 rndis_set_host_mac (dev->rndis_config, dev->host_mac);
2590 if (rndis_set_param_dev (dev->rndis_config, dev->net,
2591 &dev->stats, &dev->cdc_filter))
2593 if (rndis_set_param_vendor (dev->rndis_config, vendorID,
2596 if (rndis_set_param_medium (dev->rndis_config,
2600 INFO (dev, "RNDIS ready\n");
2606 dev_dbg(&gadget->dev, "register_netdev failed, %d\n", status);
2608 eth_unbind (gadget);
2612 /*-------------------------------------------------------------------------*/
2615 eth_suspend (struct usb_gadget *gadget)
2617 struct eth_dev *dev = get_gadget_data (gadget);
2619 DEBUG (dev, "suspend\n");
2624 eth_resume (struct usb_gadget *gadget)
2626 struct eth_dev *dev = get_gadget_data (gadget);
2628 DEBUG (dev, "resume\n");
2632 /*-------------------------------------------------------------------------*/
2634 static struct usb_gadget_driver eth_driver = {
2637 .function = (char *) driver_desc,
2639 .unbind = eth_unbind,
2642 .disconnect = eth_disconnect,
2644 .suspend = eth_suspend,
2645 .resume = eth_resume,
2648 .name = (char *) shortname,
2649 .owner = THIS_MODULE,
2653 MODULE_DESCRIPTION (DRIVER_DESC);
2654 MODULE_AUTHOR ("David Brownell, Benedikt Spanger");
2655 MODULE_LICENSE ("GPL");
2658 static int __init init (void)
2660 return usb_gadget_register_driver (ð_driver);
2664 static void __exit cleanup (void)
2666 usb_gadget_unregister_driver (ð_driver);
2668 module_exit (cleanup);