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
285 #ifdef CONFIG_USB_GADGET_FSL_USB2
286 #define DEV_CONFIG_CDC
289 /* For CDC-incapable hardware, choose the simple cdc subset.
290 * Anything that talks bulk (without notable bugs) can do this.
292 #ifdef CONFIG_USB_GADGET_PXA2XX
293 #define DEV_CONFIG_SUBSET
296 #ifdef CONFIG_USB_GADGET_SH
297 #define DEV_CONFIG_SUBSET
300 #ifdef CONFIG_USB_GADGET_SA1100
301 /* use non-CDC for backwards compatibility */
302 #define DEV_CONFIG_SUBSET
306 /*-------------------------------------------------------------------------*/
308 /* "main" config is either CDC, or its simple subset */
309 static inline int is_cdc(struct eth_dev *dev)
311 #if !defined(DEV_CONFIG_SUBSET)
312 return 1; /* only cdc possible */
313 #elif !defined (DEV_CONFIG_CDC)
314 return 0; /* only subset possible */
316 return dev->cdc; /* depends on what hardware we found */
320 /* "secondary" RNDIS config may sometimes be activated */
321 static inline int rndis_active(struct eth_dev *dev)
323 #ifdef CONFIG_USB_ETH_RNDIS
330 #define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
331 #define cdc_active(dev) ( is_cdc(dev) && !rndis_active(dev))
335 #define DEFAULT_QLEN 2 /* double buffering by default */
337 /* peak bulk transfer bits-per-second */
338 #define HS_BPS (13 * 512 * 8 * 1000 * 8)
339 #define FS_BPS (19 * 64 * 1 * 1000 * 8)
341 #ifdef CONFIG_USB_GADGET_DUALSPEED
342 #define DEVSPEED USB_SPEED_HIGH
344 static unsigned qmult = 5;
345 module_param (qmult, uint, S_IRUGO|S_IWUSR);
348 /* for dual-speed hardware, use deeper queues at highspeed */
349 #define qlen(gadget) \
350 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
352 /* also defer IRQs on highspeed TX */
353 #define TX_DELAY qmult
355 static inline int BITRATE(struct usb_gadget *g)
357 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
360 #else /* full speed (low speed doesn't do bulk) */
361 #define DEVSPEED USB_SPEED_FULL
363 #define qlen(gadget) DEFAULT_QLEN
365 static inline int BITRATE(struct usb_gadget *g)
372 /*-------------------------------------------------------------------------*/
374 #define xprintk(d,level,fmt,args...) \
375 printk(level "%s: " fmt , (d)->net->name , ## args)
379 #define DEBUG(dev,fmt,args...) \
380 xprintk(dev , KERN_DEBUG , fmt , ## args)
382 #define DEBUG(dev,fmt,args...) \
389 #define VDEBUG(dev,fmt,args...) \
393 #define ERROR(dev,fmt,args...) \
394 xprintk(dev , KERN_ERR , fmt , ## args)
395 #define WARN(dev,fmt,args...) \
396 xprintk(dev , KERN_WARNING , fmt , ## args)
397 #define INFO(dev,fmt,args...) \
398 xprintk(dev , KERN_INFO , fmt , ## args)
400 /*-------------------------------------------------------------------------*/
402 /* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
403 * ep0 implementation: descriptors, config management, setup().
404 * also optional class-specific notification interrupt transfer.
408 * DESCRIPTORS ... most are static, but strings and (full) configuration
409 * descriptors are built on demand. For now we do either full CDC, or
410 * our simple subset, with RNDIS as an optional second configuration.
412 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
413 * the class descriptors match a modem (they're ignored; it's really just
414 * Ethernet functionality), they don't need the NOP altsetting, and the
415 * status transfer endpoint isn't optional.
418 #define STRING_MANUFACTURER 1
419 #define STRING_PRODUCT 2
420 #define STRING_ETHADDR 3
421 #define STRING_DATA 4
422 #define STRING_CONTROL 5
423 #define STRING_RNDIS_CONTROL 6
425 #define STRING_SUBSET 8
426 #define STRING_RNDIS 9
427 #define STRING_SERIALNUMBER 10
429 /* holds our biggest descriptor (or RNDIS response) */
430 #define USB_BUFSIZ 256
433 * This device advertises one configuration, eth_config, unless RNDIS
434 * is enabled (rndis_config) on hardware supporting at least two configs.
436 * NOTE: Controllers like superh_udc should probably be able to use
437 * an RNDIS-only configuration.
439 * FIXME define some higher-powered configurations to make it easier
440 * to recharge batteries ...
443 #define DEV_CONFIG_VALUE 1 /* cdc or subset */
444 #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
446 static struct usb_device_descriptor
448 .bLength = sizeof device_desc,
449 .bDescriptorType = USB_DT_DEVICE,
451 .bcdUSB = __constant_cpu_to_le16 (0x0200),
453 .bDeviceClass = USB_CLASS_COMM,
454 .bDeviceSubClass = 0,
455 .bDeviceProtocol = 0,
457 .idVendor = __constant_cpu_to_le16 (CDC_VENDOR_NUM),
458 .idProduct = __constant_cpu_to_le16 (CDC_PRODUCT_NUM),
459 .iManufacturer = STRING_MANUFACTURER,
460 .iProduct = STRING_PRODUCT,
461 .bNumConfigurations = 1,
464 static struct usb_otg_descriptor
466 .bLength = sizeof otg_descriptor,
467 .bDescriptorType = USB_DT_OTG,
469 .bmAttributes = USB_OTG_SRP,
472 static struct usb_config_descriptor
474 .bLength = sizeof eth_config,
475 .bDescriptorType = USB_DT_CONFIG,
477 /* compute wTotalLength on the fly */
479 .bConfigurationValue = DEV_CONFIG_VALUE,
480 .iConfiguration = STRING_CDC,
481 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
485 #ifdef CONFIG_USB_ETH_RNDIS
486 static struct usb_config_descriptor
488 .bLength = sizeof rndis_config,
489 .bDescriptorType = USB_DT_CONFIG,
491 /* compute wTotalLength on the fly */
493 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
494 .iConfiguration = STRING_RNDIS,
495 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
501 * Compared to the simple CDC subset, the full CDC Ethernet model adds
502 * three class descriptors, two interface descriptors, optional status
503 * endpoint. Both have a "data" interface and two bulk endpoints.
504 * There are also differences in how control requests are handled.
506 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
507 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
508 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
509 * work around those bugs) are unlikely to ever come from MSFT, you may
510 * wish to avoid using RNDIS.
512 * MCCI offers an alternative to RNDIS if you need to connect to Windows
513 * but have hardware that can't support CDC Ethernet. We add descriptors
514 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
515 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
516 * get those drivers from MCCI, or bundled with various products.
519 #ifdef DEV_CONFIG_CDC
520 static struct usb_interface_descriptor
522 .bLength = sizeof control_intf,
523 .bDescriptorType = USB_DT_INTERFACE,
525 .bInterfaceNumber = 0,
526 /* status endpoint is optional; this may be patched later */
528 .bInterfaceClass = USB_CLASS_COMM,
529 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
530 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
531 .iInterface = STRING_CONTROL,
535 #ifdef CONFIG_USB_ETH_RNDIS
536 static const struct usb_interface_descriptor
537 rndis_control_intf = {
538 .bLength = sizeof rndis_control_intf,
539 .bDescriptorType = USB_DT_INTERFACE,
541 .bInterfaceNumber = 0,
543 .bInterfaceClass = USB_CLASS_COMM,
544 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
545 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
546 .iInterface = STRING_RNDIS_CONTROL,
550 static const struct usb_cdc_header_desc header_desc = {
551 .bLength = sizeof header_desc,
552 .bDescriptorType = USB_DT_CS_INTERFACE,
553 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
555 .bcdCDC = __constant_cpu_to_le16 (0x0110),
558 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
560 static const struct usb_cdc_union_desc union_desc = {
561 .bLength = sizeof union_desc,
562 .bDescriptorType = USB_DT_CS_INTERFACE,
563 .bDescriptorSubType = USB_CDC_UNION_TYPE,
565 .bMasterInterface0 = 0, /* index of control interface */
566 .bSlaveInterface0 = 1, /* index of DATA interface */
569 #endif /* CDC || RNDIS */
571 #ifdef CONFIG_USB_ETH_RNDIS
573 static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
574 .bLength = sizeof call_mgmt_descriptor,
575 .bDescriptorType = USB_DT_CS_INTERFACE,
576 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
578 .bmCapabilities = 0x00,
579 .bDataInterface = 0x01,
582 static const struct usb_cdc_acm_descriptor acm_descriptor = {
583 .bLength = sizeof acm_descriptor,
584 .bDescriptorType = USB_DT_CS_INTERFACE,
585 .bDescriptorSubType = USB_CDC_ACM_TYPE,
587 .bmCapabilities = 0x00,
592 #ifndef DEV_CONFIG_CDC
594 /* "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
595 * ways: data endpoints live in the control interface, there's no data
596 * interface, and it's not used to talk to a cell phone radio.
599 static const struct usb_cdc_mdlm_desc mdlm_desc = {
600 .bLength = sizeof mdlm_desc,
601 .bDescriptorType = USB_DT_CS_INTERFACE,
602 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
604 .bcdVersion = __constant_cpu_to_le16(0x0100),
606 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
607 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
611 /* since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
612 * can't really use its struct. All we do here is say that we're using
613 * the submode of "SAFE" which directly matches the CDC Subset.
615 static const u8 mdlm_detail_desc[] = {
618 USB_CDC_MDLM_DETAIL_TYPE,
621 0, /* network control capabilities (none) */
622 0, /* network data capabilities ("raw" encapsulation) */
627 static const struct usb_cdc_ether_desc ether_desc = {
628 .bLength = sizeof ether_desc,
629 .bDescriptorType = USB_DT_CS_INTERFACE,
630 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
632 /* this descriptor actually adds value, surprise! */
633 .iMACAddress = STRING_ETHADDR,
634 .bmEthernetStatistics = __constant_cpu_to_le32 (0), /* no statistics */
635 .wMaxSegmentSize = __constant_cpu_to_le16 (ETH_FRAME_LEN),
636 .wNumberMCFilters = __constant_cpu_to_le16 (0),
637 .bNumberPowerFilters = 0,
641 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
643 /* include the status endpoint if we can, even where it's optional.
644 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
645 * packet, to simplify cancellation; and a big transfer interval, to
646 * waste less bandwidth.
648 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
649 * if they ignore the connect/disconnect notifications that real aether
650 * can provide. more advanced cdc configurations might want to support
651 * encapsulated commands (vendor-specific, using control-OUT).
653 * RNDIS requires the status endpoint, since it uses that encapsulation
654 * mechanism for its funky RPC scheme.
657 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
658 #define STATUS_BYTECOUNT 16 /* 8 byte header + data */
660 static struct usb_endpoint_descriptor
662 .bLength = USB_DT_ENDPOINT_SIZE,
663 .bDescriptorType = USB_DT_ENDPOINT,
665 .bEndpointAddress = USB_DIR_IN,
666 .bmAttributes = USB_ENDPOINT_XFER_INT,
667 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
668 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
672 #ifdef DEV_CONFIG_CDC
674 /* the default data interface has no endpoints ... */
676 static const struct usb_interface_descriptor
678 .bLength = sizeof data_nop_intf,
679 .bDescriptorType = USB_DT_INTERFACE,
681 .bInterfaceNumber = 1,
682 .bAlternateSetting = 0,
684 .bInterfaceClass = USB_CLASS_CDC_DATA,
685 .bInterfaceSubClass = 0,
686 .bInterfaceProtocol = 0,
689 /* ... but the "real" data interface has two bulk endpoints */
691 static const struct usb_interface_descriptor
693 .bLength = sizeof data_intf,
694 .bDescriptorType = USB_DT_INTERFACE,
696 .bInterfaceNumber = 1,
697 .bAlternateSetting = 1,
699 .bInterfaceClass = USB_CLASS_CDC_DATA,
700 .bInterfaceSubClass = 0,
701 .bInterfaceProtocol = 0,
702 .iInterface = STRING_DATA,
707 #ifdef CONFIG_USB_ETH_RNDIS
709 /* RNDIS doesn't activate by changing to the "real" altsetting */
711 static const struct usb_interface_descriptor
713 .bLength = sizeof rndis_data_intf,
714 .bDescriptorType = USB_DT_INTERFACE,
716 .bInterfaceNumber = 1,
717 .bAlternateSetting = 0,
719 .bInterfaceClass = USB_CLASS_CDC_DATA,
720 .bInterfaceSubClass = 0,
721 .bInterfaceProtocol = 0,
722 .iInterface = STRING_DATA,
727 #ifdef DEV_CONFIG_SUBSET
730 * "Simple" CDC-subset option is a simple vendor-neutral model that most
731 * full speed controllers can handle: one interface, two bulk endpoints.
733 * To assist host side drivers, we fancy it up a bit, and add descriptors
734 * so some host side drivers will understand it as a "SAFE" variant.
737 static const struct usb_interface_descriptor
739 .bLength = sizeof subset_data_intf,
740 .bDescriptorType = USB_DT_INTERFACE,
742 .bInterfaceNumber = 0,
743 .bAlternateSetting = 0,
745 .bInterfaceClass = USB_CLASS_COMM,
746 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
747 .bInterfaceProtocol = 0,
748 .iInterface = STRING_DATA,
754 static struct usb_endpoint_descriptor
756 .bLength = USB_DT_ENDPOINT_SIZE,
757 .bDescriptorType = USB_DT_ENDPOINT,
759 .bEndpointAddress = USB_DIR_IN,
760 .bmAttributes = USB_ENDPOINT_XFER_BULK,
763 static struct usb_endpoint_descriptor
765 .bLength = USB_DT_ENDPOINT_SIZE,
766 .bDescriptorType = USB_DT_ENDPOINT,
768 .bEndpointAddress = USB_DIR_OUT,
769 .bmAttributes = USB_ENDPOINT_XFER_BULK,
772 static const struct usb_descriptor_header *fs_eth_function [11] = {
773 (struct usb_descriptor_header *) &otg_descriptor,
774 #ifdef DEV_CONFIG_CDC
775 /* "cdc" mode descriptors */
776 (struct usb_descriptor_header *) &control_intf,
777 (struct usb_descriptor_header *) &header_desc,
778 (struct usb_descriptor_header *) &union_desc,
779 (struct usb_descriptor_header *) ðer_desc,
780 /* NOTE: status endpoint may need to be removed */
781 (struct usb_descriptor_header *) &fs_status_desc,
782 /* data interface, with altsetting */
783 (struct usb_descriptor_header *) &data_nop_intf,
784 (struct usb_descriptor_header *) &data_intf,
785 (struct usb_descriptor_header *) &fs_source_desc,
786 (struct usb_descriptor_header *) &fs_sink_desc,
788 #endif /* DEV_CONFIG_CDC */
791 static inline void __init fs_subset_descriptors(void)
793 #ifdef DEV_CONFIG_SUBSET
794 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
795 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
796 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
797 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
798 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
799 fs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
800 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
801 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
802 fs_eth_function[8] = NULL;
804 fs_eth_function[1] = NULL;
808 #ifdef CONFIG_USB_ETH_RNDIS
809 static const struct usb_descriptor_header *fs_rndis_function [] = {
810 (struct usb_descriptor_header *) &otg_descriptor,
811 /* control interface matches ACM, not Ethernet */
812 (struct usb_descriptor_header *) &rndis_control_intf,
813 (struct usb_descriptor_header *) &header_desc,
814 (struct usb_descriptor_header *) &call_mgmt_descriptor,
815 (struct usb_descriptor_header *) &acm_descriptor,
816 (struct usb_descriptor_header *) &union_desc,
817 (struct usb_descriptor_header *) &fs_status_desc,
818 /* data interface has no altsetting */
819 (struct usb_descriptor_header *) &rndis_data_intf,
820 (struct usb_descriptor_header *) &fs_source_desc,
821 (struct usb_descriptor_header *) &fs_sink_desc,
826 #ifdef CONFIG_USB_GADGET_DUALSPEED
829 * usb 2.0 devices need to expose both high speed and full speed
830 * descriptors, unless they only run at full speed.
833 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
834 static struct usb_endpoint_descriptor
836 .bLength = USB_DT_ENDPOINT_SIZE,
837 .bDescriptorType = USB_DT_ENDPOINT,
839 .bmAttributes = USB_ENDPOINT_XFER_INT,
840 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
841 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
843 #endif /* DEV_CONFIG_CDC */
845 static struct usb_endpoint_descriptor
847 .bLength = USB_DT_ENDPOINT_SIZE,
848 .bDescriptorType = USB_DT_ENDPOINT,
850 .bmAttributes = USB_ENDPOINT_XFER_BULK,
851 .wMaxPacketSize = __constant_cpu_to_le16 (512),
854 static struct usb_endpoint_descriptor
856 .bLength = USB_DT_ENDPOINT_SIZE,
857 .bDescriptorType = USB_DT_ENDPOINT,
859 .bmAttributes = USB_ENDPOINT_XFER_BULK,
860 .wMaxPacketSize = __constant_cpu_to_le16 (512),
863 static struct usb_qualifier_descriptor
865 .bLength = sizeof dev_qualifier,
866 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
868 .bcdUSB = __constant_cpu_to_le16 (0x0200),
869 .bDeviceClass = USB_CLASS_COMM,
871 .bNumConfigurations = 1,
874 static const struct usb_descriptor_header *hs_eth_function [11] = {
875 (struct usb_descriptor_header *) &otg_descriptor,
876 #ifdef DEV_CONFIG_CDC
877 /* "cdc" mode descriptors */
878 (struct usb_descriptor_header *) &control_intf,
879 (struct usb_descriptor_header *) &header_desc,
880 (struct usb_descriptor_header *) &union_desc,
881 (struct usb_descriptor_header *) ðer_desc,
882 /* NOTE: status endpoint may need to be removed */
883 (struct usb_descriptor_header *) &hs_status_desc,
884 /* data interface, with altsetting */
885 (struct usb_descriptor_header *) &data_nop_intf,
886 (struct usb_descriptor_header *) &data_intf,
887 (struct usb_descriptor_header *) &hs_source_desc,
888 (struct usb_descriptor_header *) &hs_sink_desc,
890 #endif /* DEV_CONFIG_CDC */
893 static inline void __init hs_subset_descriptors(void)
895 #ifdef DEV_CONFIG_SUBSET
896 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
897 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
898 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
899 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
900 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
901 hs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
902 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
903 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
904 hs_eth_function[8] = NULL;
906 hs_eth_function[1] = NULL;
910 #ifdef CONFIG_USB_ETH_RNDIS
911 static const struct usb_descriptor_header *hs_rndis_function [] = {
912 (struct usb_descriptor_header *) &otg_descriptor,
913 /* control interface matches ACM, not Ethernet */
914 (struct usb_descriptor_header *) &rndis_control_intf,
915 (struct usb_descriptor_header *) &header_desc,
916 (struct usb_descriptor_header *) &call_mgmt_descriptor,
917 (struct usb_descriptor_header *) &acm_descriptor,
918 (struct usb_descriptor_header *) &union_desc,
919 (struct usb_descriptor_header *) &hs_status_desc,
920 /* data interface has no altsetting */
921 (struct usb_descriptor_header *) &rndis_data_intf,
922 (struct usb_descriptor_header *) &hs_source_desc,
923 (struct usb_descriptor_header *) &hs_sink_desc,
929 /* maxpacket and other transfer characteristics vary by speed. */
930 #define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
934 /* if there's no high speed support, maxpacket doesn't change. */
935 #define ep_desc(g,hs,fs) (((void)(g)), (fs))
937 static inline void __init hs_subset_descriptors(void)
941 #endif /* !CONFIG_USB_GADGET_DUALSPEED */
943 /*-------------------------------------------------------------------------*/
945 /* descriptors that are built on-demand */
947 static char manufacturer [50];
948 static char product_desc [40] = DRIVER_DESC;
949 static char serial_number [20];
951 /* address that the host will use ... usually assigned at random */
952 static char ethaddr [2 * ETH_ALEN + 1];
954 /* static strings, in UTF-8 */
955 static struct usb_string strings [] = {
956 { STRING_MANUFACTURER, manufacturer, },
957 { STRING_PRODUCT, product_desc, },
958 { STRING_SERIALNUMBER, serial_number, },
959 { STRING_DATA, "Ethernet Data", },
960 { STRING_ETHADDR, ethaddr, },
961 #ifdef DEV_CONFIG_CDC
962 { STRING_CDC, "CDC Ethernet", },
963 { STRING_CONTROL, "CDC Communications Control", },
965 #ifdef DEV_CONFIG_SUBSET
966 { STRING_SUBSET, "CDC Ethernet Subset", },
968 #ifdef CONFIG_USB_ETH_RNDIS
969 { STRING_RNDIS, "RNDIS", },
970 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
972 { } /* end of list */
975 static struct usb_gadget_strings stringtab = {
976 .language = 0x0409, /* en-us */
981 * one config, two interfaces: control, data.
982 * complications: class descriptors, and an altsetting.
985 config_buf (enum usb_device_speed speed,
987 unsigned index, int is_otg)
990 const struct usb_config_descriptor *config;
991 const struct usb_descriptor_header **function;
992 #ifdef CONFIG_USB_GADGET_DUALSPEED
993 int hs = (speed == USB_SPEED_HIGH);
995 if (type == USB_DT_OTHER_SPEED_CONFIG)
997 #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
999 #define which_fn(t) (fs_ ## t ## _function)
1002 if (index >= device_desc.bNumConfigurations)
1005 #ifdef CONFIG_USB_ETH_RNDIS
1006 /* list the RNDIS config first, to make Microsoft's drivers
1007 * happy. DOCSIS 1.0 needs this too.
1009 if (device_desc.bNumConfigurations == 2 && index == 0) {
1010 config = &rndis_config;
1011 function = which_fn (rndis);
1015 config = ð_config;
1016 function = which_fn (eth);
1019 /* for now, don't advertise srp-only devices */
1023 len = usb_gadget_config_buf (config, buf, USB_BUFSIZ, function);
1026 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
1030 /*-------------------------------------------------------------------------*/
1032 static void eth_start (struct eth_dev *dev, gfp_t gfp_flags);
1033 static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
1036 set_ether_config (struct eth_dev *dev, gfp_t gfp_flags)
1039 struct usb_gadget *gadget = dev->gadget;
1041 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
1042 /* status endpoint used for RNDIS and (optionally) CDC */
1043 if (!subset_active(dev) && dev->status_ep) {
1044 dev->status = ep_desc (gadget, &hs_status_desc,
1046 dev->status_ep->driver_data = dev;
1048 result = usb_ep_enable (dev->status_ep, dev->status);
1050 DEBUG (dev, "enable %s --> %d\n",
1051 dev->status_ep->name, result);
1057 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
1058 dev->in_ep->driver_data = dev;
1060 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
1061 dev->out_ep->driver_data = dev;
1063 /* With CDC, the host isn't allowed to use these two data
1064 * endpoints in the default altsetting for the interface.
1065 * so we don't activate them yet. Reset from SET_INTERFACE.
1067 * Strictly speaking RNDIS should work the same: activation is
1068 * a side effect of setting a packet filter. Deactivation is
1069 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
1071 if (!cdc_active(dev)) {
1072 result = usb_ep_enable (dev->in_ep, dev->in);
1074 DEBUG(dev, "enable %s --> %d\n",
1075 dev->in_ep->name, result);
1079 result = usb_ep_enable (dev->out_ep, dev->out);
1081 DEBUG (dev, "enable %s --> %d\n",
1082 dev->out_ep->name, result);
1089 result = alloc_requests (dev, qlen (gadget), gfp_flags);
1091 /* on error, disable any endpoints */
1093 if (!subset_active(dev))
1094 (void) usb_ep_disable (dev->status_ep);
1096 (void) usb_ep_disable (dev->in_ep);
1097 (void) usb_ep_disable (dev->out_ep);
1102 /* activate non-CDC configs right away
1103 * this isn't strictly according to the RNDIS spec
1105 if (!cdc_active (dev)) {
1106 netif_carrier_on (dev->net);
1107 if (netif_running (dev->net)) {
1108 spin_unlock (&dev->lock);
1109 eth_start (dev, GFP_ATOMIC);
1110 spin_lock (&dev->lock);
1115 DEBUG (dev, "qlen %d\n", qlen (gadget));
1117 /* caller is responsible for cleanup on error */
1121 static void eth_reset_config (struct eth_dev *dev)
1123 struct usb_request *req;
1125 if (dev->config == 0)
1128 DEBUG (dev, "%s\n", __FUNCTION__);
1130 netif_stop_queue (dev->net);
1131 netif_carrier_off (dev->net);
1132 rndis_uninit(dev->rndis_config);
1134 /* disable endpoints, forcing (synchronous) completion of
1135 * pending i/o. then free the requests.
1138 usb_ep_disable (dev->in_ep);
1139 spin_lock(&dev->req_lock);
1140 while (likely (!list_empty (&dev->tx_reqs))) {
1141 req = container_of (dev->tx_reqs.next,
1142 struct usb_request, list);
1143 list_del (&req->list);
1145 spin_unlock(&dev->req_lock);
1146 usb_ep_free_request (dev->in_ep, req);
1147 spin_lock(&dev->req_lock);
1149 spin_unlock(&dev->req_lock);
1152 usb_ep_disable (dev->out_ep);
1153 spin_lock(&dev->req_lock);
1154 while (likely (!list_empty (&dev->rx_reqs))) {
1155 req = container_of (dev->rx_reqs.next,
1156 struct usb_request, list);
1157 list_del (&req->list);
1159 spin_unlock(&dev->req_lock);
1160 usb_ep_free_request (dev->out_ep, req);
1161 spin_lock(&dev->req_lock);
1163 spin_unlock(&dev->req_lock);
1167 usb_ep_disable (dev->status_ep);
1170 dev->cdc_filter = 0;
1174 /* change our operational config. must agree with the code
1175 * that returns config descriptors, and altsetting code.
1178 eth_set_config (struct eth_dev *dev, unsigned number, gfp_t gfp_flags)
1181 struct usb_gadget *gadget = dev->gadget;
1183 if (gadget_is_sa1100 (gadget)
1185 && atomic_read (&dev->tx_qlen) != 0) {
1186 /* tx fifo is full, but we can't clear it...*/
1187 INFO (dev, "can't change configurations\n");
1190 eth_reset_config (dev);
1193 case DEV_CONFIG_VALUE:
1194 result = set_ether_config (dev, gfp_flags);
1196 #ifdef CONFIG_USB_ETH_RNDIS
1197 case DEV_RNDIS_CONFIG_VALUE:
1199 result = set_ether_config (dev, gfp_flags);
1211 eth_reset_config (dev);
1212 usb_gadget_vbus_draw(dev->gadget,
1213 dev->gadget->is_otg ? 8 : 100);
1218 power = 2 * eth_config.bMaxPower;
1219 usb_gadget_vbus_draw(dev->gadget, power);
1221 switch (gadget->speed) {
1222 case USB_SPEED_FULL: speed = "full"; break;
1223 #ifdef CONFIG_USB_GADGET_DUALSPEED
1224 case USB_SPEED_HIGH: speed = "high"; break;
1226 default: speed = "?"; break;
1229 dev->config = number;
1230 INFO (dev, "%s speed config #%d: %d mA, %s, using %s\n",
1231 speed, number, power, driver_desc,
1236 : "CDC Ethernet Subset"));
1241 /*-------------------------------------------------------------------------*/
1243 #ifdef DEV_CONFIG_CDC
1245 /* The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
1246 * only to notify the host about link status changes (which we support) or
1247 * report completion of some encapsulated command (as used in RNDIS). Since
1248 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1249 * command mechanism; and only one status request is ever queued.
1252 static void eth_status_complete (struct usb_ep *ep, struct usb_request *req)
1254 struct usb_cdc_notification *event = req->buf;
1255 int value = req->status;
1256 struct eth_dev *dev = ep->driver_data;
1258 /* issue the second notification if host reads the first */
1259 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1261 __le32 *data = req->buf + sizeof *event;
1263 event->bmRequestType = 0xA1;
1264 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
1265 event->wValue = __constant_cpu_to_le16 (0);
1266 event->wIndex = __constant_cpu_to_le16 (1);
1267 event->wLength = __constant_cpu_to_le16 (8);
1269 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1270 data [0] = data [1] = cpu_to_le32 (BITRATE (dev->gadget));
1272 req->length = STATUS_BYTECOUNT;
1273 value = usb_ep_queue (ep, req, GFP_ATOMIC);
1274 DEBUG (dev, "send SPEED_CHANGE --> %d\n", value);
1277 } else if (value != -ECONNRESET)
1278 DEBUG (dev, "event %02x --> %d\n",
1279 event->bNotificationType, value);
1280 req->context = NULL;
1283 static void issue_start_status (struct eth_dev *dev)
1285 struct usb_request *req = dev->stat_req;
1286 struct usb_cdc_notification *event;
1289 DEBUG (dev, "%s, flush old status first\n", __FUNCTION__);
1293 * FIXME ugly idiom, maybe we'd be better with just
1294 * a "cancel the whole queue" primitive since any
1295 * unlink-one primitive has way too many error modes.
1296 * here, we "know" toggle is already clear...
1298 * FIXME iff req->context != null just dequeue it
1300 usb_ep_disable (dev->status_ep);
1301 usb_ep_enable (dev->status_ep, dev->status);
1303 /* 3.8.1 says to issue first NETWORK_CONNECTION, then
1304 * a SPEED_CHANGE. could be useful in some configs.
1307 event->bmRequestType = 0xA1;
1308 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
1309 event->wValue = __constant_cpu_to_le16 (1); /* connected */
1310 event->wIndex = __constant_cpu_to_le16 (1);
1313 req->length = sizeof *event;
1314 req->complete = eth_status_complete;
1317 value = usb_ep_queue (dev->status_ep, req, GFP_ATOMIC);
1319 DEBUG (dev, "status buf queue --> %d\n", value);
1324 /*-------------------------------------------------------------------------*/
1326 static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req)
1328 if (req->status || req->actual != req->length)
1329 DEBUG ((struct eth_dev *) ep->driver_data,
1330 "setup complete --> %d, %d/%d\n",
1331 req->status, req->actual, req->length);
1334 #ifdef CONFIG_USB_ETH_RNDIS
1336 static void rndis_response_complete (struct usb_ep *ep, struct usb_request *req)
1338 if (req->status || req->actual != req->length)
1339 DEBUG ((struct eth_dev *) ep->driver_data,
1340 "rndis response complete --> %d, %d/%d\n",
1341 req->status, req->actual, req->length);
1343 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1346 static void rndis_command_complete (struct usb_ep *ep, struct usb_request *req)
1348 struct eth_dev *dev = ep->driver_data;
1351 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1352 spin_lock(&dev->lock);
1353 status = rndis_msg_parser (dev->rndis_config, (u8 *) req->buf);
1355 ERROR(dev, "%s: rndis parse error %d\n", __FUNCTION__, status);
1356 spin_unlock(&dev->lock);
1362 * The setup() callback implements all the ep0 functionality that's not
1363 * handled lower down. CDC has a number of less-common features:
1365 * - two interfaces: control, and ethernet data
1366 * - Ethernet data interface has two altsettings: default, and active
1367 * - class-specific descriptors for the control interface
1368 * - class-specific control requests
1371 eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1373 struct eth_dev *dev = get_gadget_data (gadget);
1374 struct usb_request *req = dev->req;
1375 int value = -EOPNOTSUPP;
1376 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1377 u16 wValue = le16_to_cpu(ctrl->wValue);
1378 u16 wLength = le16_to_cpu(ctrl->wLength);
1380 /* descriptors just go into the pre-allocated ep0 buffer,
1381 * while config change events may enable network traffic.
1383 req->complete = eth_setup_complete;
1384 switch (ctrl->bRequest) {
1386 case USB_REQ_GET_DESCRIPTOR:
1387 if (ctrl->bRequestType != USB_DIR_IN)
1389 switch (wValue >> 8) {
1392 value = min (wLength, (u16) sizeof device_desc);
1393 memcpy (req->buf, &device_desc, value);
1395 #ifdef CONFIG_USB_GADGET_DUALSPEED
1396 case USB_DT_DEVICE_QUALIFIER:
1397 if (!gadget->is_dualspeed)
1399 value = min (wLength, (u16) sizeof dev_qualifier);
1400 memcpy (req->buf, &dev_qualifier, value);
1403 case USB_DT_OTHER_SPEED_CONFIG:
1404 if (!gadget->is_dualspeed)
1407 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1409 value = config_buf (gadget->speed, req->buf,
1414 value = min (wLength, (u16) value);
1418 value = usb_gadget_get_string (&stringtab,
1419 wValue & 0xff, req->buf);
1421 value = min (wLength, (u16) value);
1426 case USB_REQ_SET_CONFIGURATION:
1427 if (ctrl->bRequestType != 0)
1429 if (gadget->a_hnp_support)
1430 DEBUG (dev, "HNP available\n");
1431 else if (gadget->a_alt_hnp_support)
1432 DEBUG (dev, "HNP needs a different root port\n");
1433 spin_lock (&dev->lock);
1434 value = eth_set_config (dev, wValue, GFP_ATOMIC);
1435 spin_unlock (&dev->lock);
1437 case USB_REQ_GET_CONFIGURATION:
1438 if (ctrl->bRequestType != USB_DIR_IN)
1440 *(u8 *)req->buf = dev->config;
1441 value = min (wLength, (u16) 1);
1444 case USB_REQ_SET_INTERFACE:
1445 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1449 if (!cdc_active(dev) && wIndex != 0)
1451 spin_lock (&dev->lock);
1453 /* PXA hardware partially handles SET_INTERFACE;
1454 * we need to kluge around that interference.
1456 if (gadget_is_pxa (gadget)) {
1457 value = eth_set_config (dev, DEV_CONFIG_VALUE,
1462 #ifdef DEV_CONFIG_CDC
1464 case 0: /* control/master intf */
1468 usb_ep_disable (dev->status_ep);
1469 usb_ep_enable (dev->status_ep, dev->status);
1473 case 1: /* data intf */
1476 usb_ep_disable (dev->in_ep);
1477 usb_ep_disable (dev->out_ep);
1479 /* CDC requires the data transfers not be done from
1480 * the default interface setting ... also, setting
1481 * the non-default interface resets filters etc.
1484 if (!cdc_active (dev))
1486 usb_ep_enable (dev->in_ep, dev->in);
1487 usb_ep_enable (dev->out_ep, dev->out);
1488 dev->cdc_filter = DEFAULT_FILTER;
1489 netif_carrier_on (dev->net);
1491 issue_start_status (dev);
1492 if (netif_running (dev->net)) {
1493 spin_unlock (&dev->lock);
1494 eth_start (dev, GFP_ATOMIC);
1495 spin_lock (&dev->lock);
1498 netif_stop_queue (dev->net);
1499 netif_carrier_off (dev->net);
1505 /* FIXME this is wrong, as is the assumption that
1506 * all non-PXA hardware talks real CDC ...
1508 dev_warn (&gadget->dev, "set_interface ignored!\n");
1509 #endif /* DEV_CONFIG_CDC */
1512 spin_unlock (&dev->lock);
1514 case USB_REQ_GET_INTERFACE:
1515 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1519 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
1522 /* for CDC, iff carrier is on, data interface is active. */
1523 if (rndis_active(dev) || wIndex != 1)
1524 *(u8 *)req->buf = 0;
1526 *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0;
1527 value = min (wLength, (u16) 1);
1530 #ifdef DEV_CONFIG_CDC
1531 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
1532 /* see 6.2.30: no data, wIndex = interface,
1533 * wValue = packet filter bitmap
1535 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1540 DEBUG (dev, "packet filter %02x\n", wValue);
1541 dev->cdc_filter = wValue;
1546 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1547 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1548 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1549 * case USB_CDC_GET_ETHERNET_STATISTIC:
1552 #endif /* DEV_CONFIG_CDC */
1554 #ifdef CONFIG_USB_ETH_RNDIS
1555 /* RNDIS uses the CDC command encapsulation mechanism to implement
1556 * an RPC scheme, with much getting/setting of attributes by OID.
1558 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1559 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1560 || !rndis_active(dev)
1561 || wLength > USB_BUFSIZ
1563 || rndis_control_intf.bInterfaceNumber
1566 /* read the request, then process it */
1568 req->complete = rndis_command_complete;
1569 /* later, rndis_control_ack () sends a notification */
1572 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1573 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1574 == ctrl->bRequestType
1575 && rndis_active(dev)
1576 // && wLength >= 0x0400
1578 && rndis_control_intf.bInterfaceNumber
1582 /* return the result */
1583 buf = rndis_get_next_response (dev->rndis_config,
1586 memcpy (req->buf, buf, value);
1587 req->complete = rndis_response_complete;
1588 rndis_free_response(dev->rndis_config, buf);
1590 /* else stalls ... spec says to avoid that */
1597 "unknown control req%02x.%02x v%04x i%04x l%d\n",
1598 ctrl->bRequestType, ctrl->bRequest,
1599 wValue, wIndex, wLength);
1602 /* respond with data transfer before status phase? */
1604 req->length = value;
1605 req->zero = value < wLength
1606 && (value % gadget->ep0->maxpacket) == 0;
1607 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1609 DEBUG (dev, "ep_queue --> %d\n", value);
1611 eth_setup_complete (gadget->ep0, req);
1615 /* host either stalls (value < 0) or reports success */
1620 eth_disconnect (struct usb_gadget *gadget)
1622 struct eth_dev *dev = get_gadget_data (gadget);
1623 unsigned long flags;
1625 spin_lock_irqsave (&dev->lock, flags);
1626 netif_stop_queue (dev->net);
1627 netif_carrier_off (dev->net);
1628 eth_reset_config (dev);
1629 spin_unlock_irqrestore (&dev->lock, flags);
1631 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1633 /* next we may get setup() calls to enumerate new connections;
1634 * or an unbind() during shutdown (including removing module).
1638 /*-------------------------------------------------------------------------*/
1640 /* NETWORK DRIVER HOOKUP (to the layer above this driver) */
1642 static int eth_change_mtu (struct net_device *net, int new_mtu)
1644 struct eth_dev *dev = netdev_priv(net);
1649 if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN)
1651 /* no zero-length packet read wanted after mtu-sized packets */
1652 if (((new_mtu + sizeof (struct ethhdr)) % dev->in_ep->maxpacket) == 0)
1658 static struct net_device_stats *eth_get_stats (struct net_device *net)
1660 return &((struct eth_dev *)netdev_priv(net))->stats;
1663 static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
1665 struct eth_dev *dev = netdev_priv(net);
1666 strlcpy(p->driver, shortname, sizeof p->driver);
1667 strlcpy(p->version, DRIVER_VERSION, sizeof p->version);
1668 strlcpy(p->fw_version, dev->gadget->name, sizeof p->fw_version);
1669 strlcpy (p->bus_info, dev->gadget->dev.bus_id, sizeof p->bus_info);
1672 static u32 eth_get_link(struct net_device *net)
1674 struct eth_dev *dev = netdev_priv(net);
1675 return dev->gadget->speed != USB_SPEED_UNKNOWN;
1678 static struct ethtool_ops ops = {
1679 .get_drvinfo = eth_get_drvinfo,
1680 .get_link = eth_get_link
1683 static void defer_kevent (struct eth_dev *dev, int flag)
1685 if (test_and_set_bit (flag, &dev->todo))
1687 if (!schedule_work (&dev->work))
1688 ERROR (dev, "kevent %d may have been dropped\n", flag);
1690 DEBUG (dev, "kevent %d scheduled\n", flag);
1693 static void rx_complete (struct usb_ep *ep, struct usb_request *req);
1696 rx_submit (struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
1698 struct sk_buff *skb;
1699 int retval = -ENOMEM;
1702 /* Padding up to RX_EXTRA handles minor disagreements with host.
1703 * Normally we use the USB "terminate on short read" convention;
1704 * so allow up to (N*maxpacket), since that memory is normally
1705 * already allocated. Some hardware doesn't deal well with short
1706 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1707 * byte off the end (to force hardware errors on overflow).
1709 * RNDIS uses internal framing, and explicitly allows senders to
1710 * pad to end-of-packet. That's potentially nice for speed,
1711 * but means receivers can't recover synch on their own.
1713 size = (sizeof (struct ethhdr) + dev->net->mtu + RX_EXTRA);
1714 size += dev->out_ep->maxpacket - 1;
1715 if (rndis_active(dev))
1716 size += sizeof (struct rndis_packet_msg_type);
1717 size -= size % dev->out_ep->maxpacket;
1719 if ((skb = alloc_skb (size + NET_IP_ALIGN, gfp_flags)) == 0) {
1720 DEBUG (dev, "no rx skb\n");
1724 /* Some platforms perform better when IP packets are aligned,
1725 * but on at least one, checksumming fails otherwise. Note:
1726 * RNDIS headers involve variable numbers of LE32 values.
1728 skb_reserve(skb, NET_IP_ALIGN);
1730 req->buf = skb->data;
1732 req->complete = rx_complete;
1735 retval = usb_ep_queue (dev->out_ep, req, gfp_flags);
1736 if (retval == -ENOMEM)
1738 defer_kevent (dev, WORK_RX_MEMORY);
1740 DEBUG (dev, "rx submit --> %d\n", retval);
1742 dev_kfree_skb_any(skb);
1743 spin_lock(&dev->req_lock);
1744 list_add (&req->list, &dev->rx_reqs);
1745 spin_unlock(&dev->req_lock);
1750 static void rx_complete (struct usb_ep *ep, struct usb_request *req)
1752 struct sk_buff *skb = req->context;
1753 struct eth_dev *dev = ep->driver_data;
1754 int status = req->status;
1758 /* normal completion */
1760 skb_put (skb, req->actual);
1761 /* we know MaxPacketsPerTransfer == 1 here */
1762 if (rndis_active(dev))
1763 status = rndis_rm_hdr (skb);
1765 || ETH_HLEN > skb->len
1766 || skb->len > ETH_FRAME_LEN) {
1767 dev->stats.rx_errors++;
1768 dev->stats.rx_length_errors++;
1769 DEBUG (dev, "rx length %d\n", skb->len);
1773 skb->protocol = eth_type_trans (skb, dev->net);
1774 dev->stats.rx_packets++;
1775 dev->stats.rx_bytes += skb->len;
1777 /* no buffer copies needed, unless hardware can't
1780 status = netif_rx (skb);
1784 /* software-driven interface shutdown */
1785 case -ECONNRESET: // unlink
1786 case -ESHUTDOWN: // disconnect etc
1787 VDEBUG (dev, "rx shutdown, code %d\n", status);
1790 /* for hardware automagic (such as pxa) */
1791 case -ECONNABORTED: // endpoint reset
1792 DEBUG (dev, "rx %s reset\n", ep->name);
1793 defer_kevent (dev, WORK_RX_MEMORY);
1795 dev_kfree_skb_any (skb);
1800 dev->stats.rx_over_errors++;
1804 dev->stats.rx_errors++;
1805 DEBUG (dev, "rx status %d\n", status);
1810 dev_kfree_skb_any (skb);
1811 if (!netif_running (dev->net)) {
1813 spin_lock(&dev->req_lock);
1814 list_add (&req->list, &dev->rx_reqs);
1815 spin_unlock(&dev->req_lock);
1819 rx_submit (dev, req, GFP_ATOMIC);
1822 static int prealloc (struct list_head *list, struct usb_ep *ep,
1823 unsigned n, gfp_t gfp_flags)
1826 struct usb_request *req;
1831 /* queue/recycle up to N requests */
1833 list_for_each_entry (req, list, list) {
1838 req = usb_ep_alloc_request (ep, gfp_flags);
1840 return list_empty (list) ? -ENOMEM : 0;
1841 list_add (&req->list, list);
1848 struct list_head *next;
1850 next = req->list.next;
1851 list_del (&req->list);
1852 usb_ep_free_request (ep, req);
1857 req = container_of (next, struct usb_request, list);
1862 static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
1866 spin_lock(&dev->req_lock);
1867 status = prealloc (&dev->tx_reqs, dev->in_ep, n, gfp_flags);
1870 status = prealloc (&dev->rx_reqs, dev->out_ep, n, gfp_flags);
1875 DEBUG (dev, "can't alloc requests\n");
1877 spin_unlock(&dev->req_lock);
1881 static void rx_fill (struct eth_dev *dev, gfp_t gfp_flags)
1883 struct usb_request *req;
1884 unsigned long flags;
1886 /* fill unused rxq slots with some skb */
1887 spin_lock_irqsave(&dev->req_lock, flags);
1888 while (!list_empty (&dev->rx_reqs)) {
1889 req = container_of (dev->rx_reqs.next,
1890 struct usb_request, list);
1891 list_del_init (&req->list);
1892 spin_unlock_irqrestore(&dev->req_lock, flags);
1894 if (rx_submit (dev, req, gfp_flags) < 0) {
1895 defer_kevent (dev, WORK_RX_MEMORY);
1899 spin_lock_irqsave(&dev->req_lock, flags);
1901 spin_unlock_irqrestore(&dev->req_lock, flags);
1904 static void eth_work (struct work_struct *work)
1906 struct eth_dev *dev = container_of(work, struct eth_dev, work);
1908 if (test_and_clear_bit (WORK_RX_MEMORY, &dev->todo)) {
1909 if (netif_running (dev->net))
1910 rx_fill (dev, GFP_KERNEL);
1914 DEBUG (dev, "work done, flags = 0x%lx\n", dev->todo);
1917 static void tx_complete (struct usb_ep *ep, struct usb_request *req)
1919 struct sk_buff *skb = req->context;
1920 struct eth_dev *dev = ep->driver_data;
1922 switch (req->status) {
1924 dev->stats.tx_errors++;
1925 VDEBUG (dev, "tx err %d\n", req->status);
1927 case -ECONNRESET: // unlink
1928 case -ESHUTDOWN: // disconnect etc
1931 dev->stats.tx_bytes += skb->len;
1933 dev->stats.tx_packets++;
1935 spin_lock(&dev->req_lock);
1936 list_add (&req->list, &dev->tx_reqs);
1937 spin_unlock(&dev->req_lock);
1938 dev_kfree_skb_any (skb);
1940 atomic_dec (&dev->tx_qlen);
1941 if (netif_carrier_ok (dev->net))
1942 netif_wake_queue (dev->net);
1945 static inline int eth_is_promisc (struct eth_dev *dev)
1947 /* no filters for the CDC subset; always promisc */
1948 if (subset_active (dev))
1950 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1953 static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1955 struct eth_dev *dev = netdev_priv(net);
1956 int length = skb->len;
1958 struct usb_request *req = NULL;
1959 unsigned long flags;
1961 /* apply outgoing CDC or RNDIS filters */
1962 if (!eth_is_promisc (dev)) {
1963 u8 *dest = skb->data;
1965 if (is_multicast_ether_addr(dest)) {
1968 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1969 * SET_ETHERNET_MULTICAST_FILTERS requests
1971 if (is_broadcast_ether_addr(dest))
1972 type = USB_CDC_PACKET_TYPE_BROADCAST;
1974 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1975 if (!(dev->cdc_filter & type)) {
1976 dev_kfree_skb_any (skb);
1980 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1983 spin_lock_irqsave(&dev->req_lock, flags);
1984 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1985 list_del (&req->list);
1986 if (list_empty (&dev->tx_reqs))
1987 netif_stop_queue (net);
1988 spin_unlock_irqrestore(&dev->req_lock, flags);
1990 /* no buffer copies needed, unless the network stack did it
1991 * or the hardware can't use skb buffers.
1992 * or there's not enough space for any RNDIS headers we need
1994 if (rndis_active(dev)) {
1995 struct sk_buff *skb_rndis;
1997 skb_rndis = skb_realloc_headroom (skb,
1998 sizeof (struct rndis_packet_msg_type));
2002 dev_kfree_skb_any (skb);
2004 rndis_add_hdr (skb);
2007 req->buf = skb->data;
2009 req->complete = tx_complete;
2011 /* use zlp framing on tx for strict CDC-Ether conformance,
2012 * though any robust network rx path ignores extra padding.
2013 * and some hardware doesn't like to write zlps.
2016 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2019 req->length = length;
2021 #ifdef CONFIG_USB_GADGET_DUALSPEED
2022 /* throttle highspeed IRQ rate back slightly */
2023 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2024 ? ((atomic_read (&dev->tx_qlen) % TX_DELAY) != 0)
2028 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
2031 DEBUG (dev, "tx queue err %d\n", retval);
2034 net->trans_start = jiffies;
2035 atomic_inc (&dev->tx_qlen);
2040 dev->stats.tx_dropped++;
2041 dev_kfree_skb_any (skb);
2042 spin_lock_irqsave(&dev->req_lock, flags);
2043 if (list_empty (&dev->tx_reqs))
2044 netif_start_queue (net);
2045 list_add (&req->list, &dev->tx_reqs);
2046 spin_unlock_irqrestore(&dev->req_lock, flags);
2051 /*-------------------------------------------------------------------------*/
2053 #ifdef CONFIG_USB_ETH_RNDIS
2055 /* The interrupt endpoint is used in RNDIS to notify the host when messages
2056 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
2057 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
2058 * REMOTE_NDIS_KEEPALIVE_MSG.
2060 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
2061 * normally just one notification will be queued.
2064 static struct usb_request *eth_req_alloc (struct usb_ep *, unsigned, gfp_t);
2065 static void eth_req_free (struct usb_ep *ep, struct usb_request *req);
2068 rndis_control_ack_complete (struct usb_ep *ep, struct usb_request *req)
2070 struct eth_dev *dev = ep->driver_data;
2072 if (req->status || req->actual != req->length)
2074 "rndis control ack complete --> %d, %d/%d\n",
2075 req->status, req->actual, req->length);
2076 req->context = NULL;
2078 if (req != dev->stat_req)
2079 eth_req_free(ep, req);
2082 static int rndis_control_ack (struct net_device *net)
2084 struct eth_dev *dev = netdev_priv(net);
2086 struct usb_request *resp = dev->stat_req;
2088 /* in case RNDIS calls this after disconnect */
2090 DEBUG (dev, "status ENODEV\n");
2094 /* in case queue length > 1 */
2095 if (resp->context) {
2096 resp = eth_req_alloc (dev->status_ep, 8, GFP_ATOMIC);
2101 /* Send RNDIS RESPONSE_AVAILABLE notification;
2102 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
2105 resp->complete = rndis_control_ack_complete;
2106 resp->context = dev;
2108 *((__le32 *) resp->buf) = __constant_cpu_to_le32 (1);
2109 *((__le32 *) resp->buf + 1) = __constant_cpu_to_le32 (0);
2111 length = usb_ep_queue (dev->status_ep, resp, GFP_ATOMIC);
2114 rndis_control_ack_complete (dev->status_ep, resp);
2122 #define rndis_control_ack NULL
2126 static void eth_start (struct eth_dev *dev, gfp_t gfp_flags)
2128 DEBUG (dev, "%s\n", __FUNCTION__);
2130 /* fill the rx queue */
2131 rx_fill (dev, gfp_flags);
2133 /* and open the tx floodgates */
2134 atomic_set (&dev->tx_qlen, 0);
2135 netif_wake_queue (dev->net);
2136 if (rndis_active(dev)) {
2137 rndis_set_param_medium (dev->rndis_config,
2139 BITRATE(dev->gadget)/100);
2140 (void) rndis_signal_connect (dev->rndis_config);
2144 static int eth_open (struct net_device *net)
2146 struct eth_dev *dev = netdev_priv(net);
2148 DEBUG (dev, "%s\n", __FUNCTION__);
2149 if (netif_carrier_ok (dev->net))
2150 eth_start (dev, GFP_KERNEL);
2154 static int eth_stop (struct net_device *net)
2156 struct eth_dev *dev = netdev_priv(net);
2158 VDEBUG (dev, "%s\n", __FUNCTION__);
2159 netif_stop_queue (net);
2161 DEBUG (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
2162 dev->stats.rx_packets, dev->stats.tx_packets,
2163 dev->stats.rx_errors, dev->stats.tx_errors
2166 /* ensure there are no more active requests */
2168 usb_ep_disable (dev->in_ep);
2169 usb_ep_disable (dev->out_ep);
2170 if (netif_carrier_ok (dev->net)) {
2171 DEBUG (dev, "host still using in/out endpoints\n");
2172 // FIXME idiom may leave toggle wrong here
2173 usb_ep_enable (dev->in_ep, dev->in);
2174 usb_ep_enable (dev->out_ep, dev->out);
2176 if (dev->status_ep) {
2177 usb_ep_disable (dev->status_ep);
2178 usb_ep_enable (dev->status_ep, dev->status);
2182 if (rndis_active(dev)) {
2183 rndis_set_param_medium (dev->rndis_config,
2184 NDIS_MEDIUM_802_3, 0);
2185 (void) rndis_signal_disconnect (dev->rndis_config);
2191 /*-------------------------------------------------------------------------*/
2193 static struct usb_request *
2194 eth_req_alloc (struct usb_ep *ep, unsigned size, gfp_t gfp_flags)
2196 struct usb_request *req;
2198 req = usb_ep_alloc_request (ep, gfp_flags);
2202 req->buf = kmalloc (size, gfp_flags);
2204 usb_ep_free_request (ep, req);
2211 eth_req_free (struct usb_ep *ep, struct usb_request *req)
2214 usb_ep_free_request (ep, req);
2218 static void /* __init_or_exit */
2219 eth_unbind (struct usb_gadget *gadget)
2221 struct eth_dev *dev = get_gadget_data (gadget);
2223 DEBUG (dev, "unbind\n");
2224 rndis_deregister (dev->rndis_config);
2227 /* we've already been disconnected ... no i/o is active */
2229 eth_req_free (gadget->ep0, dev->req);
2232 if (dev->stat_req) {
2233 eth_req_free (dev->status_ep, dev->stat_req);
2234 dev->stat_req = NULL;
2237 unregister_netdev (dev->net);
2238 free_netdev(dev->net);
2240 /* assuming we used keventd, it must quiesce too */
2241 flush_scheduled_work ();
2242 set_gadget_data (gadget, NULL);
2245 static u8 __devinit nibble (unsigned char c)
2247 if (likely (isdigit (c)))
2250 if (likely (isxdigit (c)))
2251 return 10 + c - 'A';
2255 static int __devinit get_ether_addr(const char *str, u8 *dev_addr)
2260 for (i = 0; i < 6; i++) {
2263 if((*str == '.') || (*str == ':'))
2265 num = nibble(*str++) << 4;
2266 num |= (nibble(*str++));
2269 if (is_valid_ether_addr (dev_addr))
2272 random_ether_addr(dev_addr);
2276 static int __devinit
2277 eth_bind (struct usb_gadget *gadget)
2279 struct eth_dev *dev;
2280 struct net_device *net;
2281 u8 cdc = 1, zlp = 1, rndis = 1;
2282 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
2283 int status = -ENOMEM;
2286 /* these flags are only ever cleared; compiler take note */
2287 #ifndef DEV_CONFIG_CDC
2290 #ifndef CONFIG_USB_ETH_RNDIS
2294 /* Because most host side USB stacks handle CDC Ethernet, that
2295 * standard protocol is _strongly_ preferred for interop purposes.
2296 * (By everyone except Microsoft.)
2298 if (gadget_is_pxa (gadget)) {
2299 /* pxa doesn't support altsettings */
2301 } else if (gadget_is_musbhdrc(gadget)) {
2302 /* reduce tx dma overhead by avoiding special cases */
2304 } else if (gadget_is_sh(gadget)) {
2305 /* sh doesn't support multiple interfaces or configs */
2308 } else if (gadget_is_sa1100 (gadget)) {
2309 /* hardware can't write zlps */
2311 /* sa1100 CAN do CDC, without status endpoint ... we use
2312 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2317 gcnum = usb_gadget_controller_number (gadget);
2319 device_desc.bcdDevice = cpu_to_le16 (0x0200 + gcnum);
2321 /* can't assume CDC works. don't want to default to
2322 * anything less functional on CDC-capable hardware,
2323 * so we fail in this case.
2325 dev_err (&gadget->dev,
2326 "controller '%s' not recognized\n",
2330 snprintf (manufacturer, sizeof manufacturer, "%s %s/%s",
2331 init_utsname()->sysname, init_utsname()->release,
2334 /* If there's an RNDIS configuration, that's what Windows wants to
2335 * be using ... so use these product IDs here and in the "linux.inf"
2336 * needed to install MSFT drivers. Current Linux kernels will use
2337 * the second configuration if it's CDC Ethernet, and need some help
2338 * to choose the right configuration otherwise.
2341 device_desc.idVendor =
2342 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2343 device_desc.idProduct =
2344 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2345 snprintf (product_desc, sizeof product_desc,
2346 "RNDIS/%s", driver_desc);
2348 /* CDC subset ... recognized by Linux since 2.4.10, but Windows
2349 * drivers aren't widely available. (That may be improved by
2350 * supporting one submode of the "SAFE" variant of MDLM.)
2353 device_desc.idVendor =
2354 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2355 device_desc.idProduct =
2356 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2359 /* support optional vendor/distro customization */
2362 dev_err (&gadget->dev, "idVendor needs idProduct!\n");
2365 device_desc.idVendor = cpu_to_le16(idVendor);
2366 device_desc.idProduct = cpu_to_le16(idProduct);
2368 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2371 strlcpy (manufacturer, iManufacturer, sizeof manufacturer);
2373 strlcpy (product_desc, iProduct, sizeof product_desc);
2374 if (iSerialNumber) {
2375 device_desc.iSerialNumber = STRING_SERIALNUMBER,
2376 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
2379 /* all we really need is bulk IN/OUT */
2380 usb_ep_autoconfig_reset (gadget);
2381 in_ep = usb_ep_autoconfig (gadget, &fs_source_desc);
2384 dev_err (&gadget->dev,
2385 "can't autoconfigure on %s\n",
2389 in_ep->driver_data = in_ep; /* claim */
2391 out_ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
2394 out_ep->driver_data = out_ep; /* claim */
2396 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2397 /* CDC Ethernet control interface doesn't require a status endpoint.
2398 * Since some hosts expect one, try to allocate one anyway.
2401 status_ep = usb_ep_autoconfig (gadget, &fs_status_desc);
2403 status_ep->driver_data = status_ep; /* claim */
2405 dev_err (&gadget->dev,
2406 "can't run RNDIS on %s\n",
2409 #ifdef DEV_CONFIG_CDC
2410 /* pxa25x only does CDC subset; often used with RNDIS */
2412 control_intf.bNumEndpoints = 0;
2413 /* FIXME remove endpoint from descriptor list */
2419 /* one config: cdc, else minimal subset */
2421 eth_config.bNumInterfaces = 1;
2422 eth_config.iConfiguration = STRING_SUBSET;
2424 /* use functions to set these up, in case we're built to work
2425 * with multiple controllers and must override CDC Ethernet.
2427 fs_subset_descriptors();
2428 hs_subset_descriptors();
2431 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
2432 usb_gadget_set_selfpowered (gadget);
2434 /* For now RNDIS is always a second config */
2436 device_desc.bNumConfigurations = 2;
2438 #ifdef CONFIG_USB_GADGET_DUALSPEED
2440 dev_qualifier.bNumConfigurations = 2;
2442 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2444 /* assumes ep0 uses the same value for both speeds ... */
2445 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2447 /* and that all endpoints are dual-speed */
2448 hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
2449 hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
2450 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2452 hs_status_desc.bEndpointAddress =
2453 fs_status_desc.bEndpointAddress;
2455 #endif /* DUALSPEED */
2457 if (gadget->is_otg) {
2458 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2459 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2460 eth_config.bMaxPower = 4;
2461 #ifdef CONFIG_USB_ETH_RNDIS
2462 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2463 rndis_config.bMaxPower = 4;
2467 net = alloc_etherdev (sizeof *dev);
2470 dev = netdev_priv(net);
2471 spin_lock_init (&dev->lock);
2472 spin_lock_init (&dev->req_lock);
2473 INIT_WORK (&dev->work, eth_work);
2474 INIT_LIST_HEAD (&dev->tx_reqs);
2475 INIT_LIST_HEAD (&dev->rx_reqs);
2477 /* network device setup */
2479 SET_MODULE_OWNER (net);
2480 strcpy (net->name, "usb%d");
2485 dev->out_ep = out_ep;
2486 dev->status_ep = status_ep;
2488 /* Module params for these addresses should come from ID proms.
2489 * The host side address is used with CDC and RNDIS, and commonly
2490 * ends up in a persistent config database. It's not clear if
2491 * host side code for the SAFE thing cares -- its original BLAN
2492 * thing didn't, Sharp never assigned those addresses on Zaurii.
2494 if (get_ether_addr(dev_addr, net->dev_addr))
2495 dev_warn(&gadget->dev,
2496 "using random %s ethernet address\n", "self");
2497 if (get_ether_addr(host_addr, dev->host_mac))
2498 dev_warn(&gadget->dev,
2499 "using random %s ethernet address\n", "host");
2500 snprintf (ethaddr, sizeof ethaddr, "%02X%02X%02X%02X%02X%02X",
2501 dev->host_mac [0], dev->host_mac [1],
2502 dev->host_mac [2], dev->host_mac [3],
2503 dev->host_mac [4], dev->host_mac [5]);
2506 status = rndis_init();
2508 dev_err (&gadget->dev, "can't init RNDIS, %d\n",
2514 net->change_mtu = eth_change_mtu;
2515 net->get_stats = eth_get_stats;
2516 net->hard_start_xmit = eth_start_xmit;
2517 net->open = eth_open;
2518 net->stop = eth_stop;
2519 // watchdog_timeo, tx_timeout ...
2520 // set_multicast_list
2521 SET_ETHTOOL_OPS(net, &ops);
2523 /* preallocate control message data and buffer */
2524 dev->req = eth_req_alloc (gadget->ep0, USB_BUFSIZ, GFP_KERNEL);
2527 dev->req->complete = eth_setup_complete;
2529 /* ... and maybe likewise for status transfer */
2530 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2531 if (dev->status_ep) {
2532 dev->stat_req = eth_req_alloc (dev->status_ep,
2533 STATUS_BYTECOUNT, GFP_KERNEL);
2534 if (!dev->stat_req) {
2535 eth_req_free (gadget->ep0, dev->req);
2538 dev->stat_req->context = NULL;
2542 /* finish hookup to lower layer ... */
2543 dev->gadget = gadget;
2544 set_gadget_data (gadget, dev);
2545 gadget->ep0->driver_data = dev;
2547 /* two kinds of host-initiated state changes:
2548 * - iff DATA transfer is active, carrier is "on"
2549 * - tx queueing enabled if open *and* carrier is "on"
2551 netif_stop_queue (dev->net);
2552 netif_carrier_off (dev->net);
2554 SET_NETDEV_DEV (dev->net, &gadget->dev);
2555 status = register_netdev (dev->net);
2559 INFO (dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
2560 INFO (dev, "using %s, OUT %s IN %s%s%s\n", gadget->name,
2561 out_ep->name, in_ep->name,
2562 status_ep ? " STATUS " : "",
2563 status_ep ? status_ep->name : ""
2565 INFO (dev, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2566 net->dev_addr [0], net->dev_addr [1],
2567 net->dev_addr [2], net->dev_addr [3],
2568 net->dev_addr [4], net->dev_addr [5]);
2571 INFO (dev, "HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2572 dev->host_mac [0], dev->host_mac [1],
2573 dev->host_mac [2], dev->host_mac [3],
2574 dev->host_mac [4], dev->host_mac [5]);
2579 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2581 dev->rndis_config = rndis_register (rndis_control_ack);
2582 if (dev->rndis_config < 0) {
2584 unregister_netdev (dev->net);
2589 /* these set up a lot of the OIDs that RNDIS needs */
2590 rndis_set_host_mac (dev->rndis_config, dev->host_mac);
2591 if (rndis_set_param_dev (dev->rndis_config, dev->net,
2592 &dev->stats, &dev->cdc_filter))
2594 if (rndis_set_param_vendor (dev->rndis_config, vendorID,
2597 if (rndis_set_param_medium (dev->rndis_config,
2601 INFO (dev, "RNDIS ready\n");
2607 dev_dbg(&gadget->dev, "register_netdev failed, %d\n", status);
2609 eth_unbind (gadget);
2613 /*-------------------------------------------------------------------------*/
2616 eth_suspend (struct usb_gadget *gadget)
2618 struct eth_dev *dev = get_gadget_data (gadget);
2620 DEBUG (dev, "suspend\n");
2625 eth_resume (struct usb_gadget *gadget)
2627 struct eth_dev *dev = get_gadget_data (gadget);
2629 DEBUG (dev, "resume\n");
2633 /*-------------------------------------------------------------------------*/
2635 static struct usb_gadget_driver eth_driver = {
2638 .function = (char *) driver_desc,
2640 .unbind = eth_unbind,
2643 .disconnect = eth_disconnect,
2645 .suspend = eth_suspend,
2646 .resume = eth_resume,
2649 .name = (char *) shortname,
2650 .owner = THIS_MODULE,
2654 MODULE_DESCRIPTION (DRIVER_DESC);
2655 MODULE_AUTHOR ("David Brownell, Benedikt Spanger");
2656 MODULE_LICENSE ("GPL");
2659 static int __init init (void)
2661 return usb_gadget_register_driver (ð_driver);
2665 static void __exit cleanup (void)
2667 usb_gadget_unregister_driver (ð_driver);
2669 module_exit (cleanup);