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/config.h>
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/delay.h>
30 #include <linux/ioport.h>
31 #include <linux/sched.h>
32 #include <linux/slab.h>
33 #include <linux/smp_lock.h>
34 #include <linux/errno.h>
35 #include <linux/init.h>
36 #include <linux/timer.h>
37 #include <linux/list.h>
38 #include <linux/interrupt.h>
39 #include <linux/utsname.h>
40 #include <linux/device.h>
41 #include <linux/moduleparam.h>
42 #include <linux/ctype.h>
44 #include <asm/byteorder.h>
47 #include <asm/system.h>
48 #include <asm/uaccess.h>
49 #include <asm/unaligned.h>
51 #include <linux/usb_ch9.h>
52 #include <linux/usb_cdc.h>
53 #include <linux/usb_gadget.h>
55 #include <linux/random.h>
56 #include <linux/netdevice.h>
57 #include <linux/etherdevice.h>
58 #include <linux/ethtool.h>
60 #include "gadget_chips.h"
62 /*-------------------------------------------------------------------------*/
65 * Ethernet gadget driver -- with CDC and non-CDC options
66 * Builds on hardware support for a full duplex link.
68 * CDC Ethernet is the standard USB solution for sending Ethernet frames
69 * using USB. Real hardware tends to use the same framing protocol but look
70 * different for control features. This driver strongly prefers to use
71 * this USB-IF standard as its open-systems interoperability solution;
72 * most host side USB stacks (except from Microsoft) support it.
74 * There's some hardware that can't talk CDC. We make that hardware
75 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
76 * link-level setup only requires activating the configuration.
77 * Linux supports it, but other host operating systems may not.
78 * (This is a subset of CDC Ethernet.)
80 * A third option is also in use. Rather than CDC Ethernet, or something
81 * simpler, Microsoft pushes their own approach: RNDIS. The published
82 * RNDIS specs are ambiguous and appear to be incomplete, and are also
86 #define DRIVER_DESC "Ethernet Gadget"
87 #define DRIVER_VERSION "May Day 2005"
89 static const char shortname [] = "ether";
90 static const char driver_desc [] = DRIVER_DESC;
92 #define RX_EXTRA 20 /* guard against rx overflows */
96 #ifndef CONFIG_USB_ETH_RNDIS
97 #define rndis_uninit(x) do{}while(0)
98 #define rndis_deregister(c) do{}while(0)
99 #define rndis_exit() do{}while(0)
102 /* CDC and RNDIS support the same host-chosen outgoing packet filters. */
103 #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
104 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
105 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
106 |USB_CDC_PACKET_TYPE_DIRECTED)
109 /*-------------------------------------------------------------------------*/
113 struct usb_gadget *gadget;
114 struct usb_request *req; /* for control responses */
115 struct usb_request *stat_req; /* for cdc & rndis status */
118 struct usb_ep *in_ep, *out_ep, *status_ep;
119 const struct usb_endpoint_descriptor
121 struct list_head tx_reqs, rx_reqs;
123 struct net_device *net;
124 struct net_device_stats stats;
127 struct work_struct work;
131 unsigned suspended:1;
134 #define WORK_RX_MEMORY 0
136 u8 host_mac [ETH_ALEN];
139 /* This version autoconfigures as much as possible at run-time.
141 * It also ASSUMES a self-powered device, without remote wakeup,
142 * although remote wakeup support would make sense.
145 /*-------------------------------------------------------------------------*/
147 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
148 * Instead: allocate your own, using normal USB-IF procedures.
151 /* Thanks to NetChip Technologies for donating this product ID.
152 * It's for devices with only CDC Ethernet configurations.
154 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
155 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
157 /* For hardware that can't talk CDC, we use the same vendor ID that
158 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
159 * with pxa250. We're protocol-compatible, if the host-side drivers
160 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
161 * drivers that need to hard-wire endpoint numbers have a hook.
163 * The protocol is a minimal subset of CDC Ether, which works on any bulk
164 * hardware that's not deeply broken ... even on hardware that can't talk
165 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
166 * doesn't handle control-OUT).
168 #define SIMPLE_VENDOR_NUM 0x049f
169 #define SIMPLE_PRODUCT_NUM 0x505a
171 /* For hardware that can talk RNDIS and either of the above protocols,
172 * use this ID ... the windows INF files will know it. Unless it's
173 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
174 * the non-RNDIS configuration.
176 #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
177 #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
180 /* Some systems will want different product identifers published in the
181 * device descriptor, either numbers or strings or both. These string
182 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
185 static ushort idVendor;
186 module_param(idVendor, ushort, S_IRUGO);
187 MODULE_PARM_DESC(idVendor, "USB Vendor ID");
189 static ushort idProduct;
190 module_param(idProduct, ushort, S_IRUGO);
191 MODULE_PARM_DESC(idProduct, "USB Product ID");
193 static ushort bcdDevice;
194 module_param(bcdDevice, ushort, S_IRUGO);
195 MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
197 static char *iManufacturer;
198 module_param(iManufacturer, charp, S_IRUGO);
199 MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
201 static char *iProduct;
202 module_param(iProduct, charp, S_IRUGO);
203 MODULE_PARM_DESC(iProduct, "USB Product string");
205 static char *iSerialNumber;
206 module_param(iSerialNumber, charp, S_IRUGO);
207 MODULE_PARM_DESC(iSerialNumber, "SerialNumber");
209 /* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */
210 static char *dev_addr;
211 module_param(dev_addr, charp, S_IRUGO);
212 MODULE_PARM_DESC(dev_addr, "Device Ethernet Address");
214 /* this address is invisible to ifconfig */
215 static char *host_addr;
216 module_param(host_addr, charp, S_IRUGO);
217 MODULE_PARM_DESC(host_addr, "Host Ethernet Address");
220 /*-------------------------------------------------------------------------*/
222 /* Include CDC support if we could run on CDC-capable hardware. */
224 #ifdef CONFIG_USB_GADGET_NET2280
225 #define DEV_CONFIG_CDC
228 #ifdef CONFIG_USB_GADGET_DUMMY_HCD
229 #define DEV_CONFIG_CDC
232 #ifdef CONFIG_USB_GADGET_GOKU
233 #define DEV_CONFIG_CDC
236 #ifdef CONFIG_USB_GADGET_LH7A40X
237 #define DEV_CONFIG_CDC
240 #ifdef CONFIG_USB_GADGET_MQ11XX
241 #define DEV_CONFIG_CDC
244 #ifdef CONFIG_USB_GADGET_OMAP
245 #define DEV_CONFIG_CDC
248 #ifdef CONFIG_USB_GADGET_N9604
249 #define DEV_CONFIG_CDC
252 #ifdef CONFIG_USB_GADGET_PXA27X
253 #define DEV_CONFIG_CDC
256 #ifdef CONFIG_USB_GADGET_AT91
257 #define DEV_CONFIG_CDC
260 #ifdef CONFIG_USB_GADGET_MUSBHSFC
261 #define DEV_CONFIG_CDC
264 #ifdef CONFIG_USB_GADGET_MUSBHDRC
265 #define DEV_CONFIG_CDC
269 /* For CDC-incapable hardware, choose the simple cdc subset.
270 * Anything that talks bulk (without notable bugs) can do this.
272 #ifdef CONFIG_USB_GADGET_PXA2XX
273 #define DEV_CONFIG_SUBSET
276 #ifdef CONFIG_USB_GADGET_SH
277 #define DEV_CONFIG_SUBSET
280 #ifdef CONFIG_USB_GADGET_SA1100
281 /* use non-CDC for backwards compatibility */
282 #define DEV_CONFIG_SUBSET
285 #ifdef CONFIG_USB_GADGET_S3C2410
286 #define DEV_CONFIG_CDC
289 /*-------------------------------------------------------------------------*/
291 /* "main" config is either CDC, or its simple subset */
292 static inline int is_cdc(struct eth_dev *dev)
294 #if !defined(DEV_CONFIG_SUBSET)
295 return 1; /* only cdc possible */
296 #elif !defined (DEV_CONFIG_CDC)
297 return 0; /* only subset possible */
299 return dev->cdc; /* depends on what hardware we found */
303 /* "secondary" RNDIS config may sometimes be activated */
304 static inline int rndis_active(struct eth_dev *dev)
306 #ifdef CONFIG_USB_ETH_RNDIS
313 #define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
314 #define cdc_active(dev) ( is_cdc(dev) && !rndis_active(dev))
318 #define DEFAULT_QLEN 2 /* double buffering by default */
320 /* peak bulk transfer bits-per-second */
321 #define HS_BPS (13 * 512 * 8 * 1000 * 8)
322 #define FS_BPS (19 * 64 * 1 * 1000 * 8)
324 #ifdef CONFIG_USB_GADGET_DUALSPEED
325 #define DEVSPEED USB_SPEED_HIGH
327 static unsigned qmult = 5;
328 module_param (qmult, uint, S_IRUGO|S_IWUSR);
331 /* for dual-speed hardware, use deeper queues at highspeed */
332 #define qlen(gadget) \
333 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
335 /* also defer IRQs on highspeed TX */
336 #define TX_DELAY qmult
338 static inline int BITRATE(struct usb_gadget *g)
340 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
343 #else /* full speed (low speed doesn't do bulk) */
344 #define DEVSPEED USB_SPEED_FULL
346 #define qlen(gadget) DEFAULT_QLEN
348 static inline int BITRATE(struct usb_gadget *g)
355 /*-------------------------------------------------------------------------*/
357 #define xprintk(d,level,fmt,args...) \
358 printk(level "%s: " fmt , (d)->net->name , ## args)
362 #define DEBUG(dev,fmt,args...) \
363 xprintk(dev , KERN_DEBUG , fmt , ## args)
365 #define DEBUG(dev,fmt,args...) \
372 #define VDEBUG(dev,fmt,args...) \
376 #define ERROR(dev,fmt,args...) \
377 xprintk(dev , KERN_ERR , fmt , ## args)
378 #define WARN(dev,fmt,args...) \
379 xprintk(dev , KERN_WARNING , fmt , ## args)
380 #define INFO(dev,fmt,args...) \
381 xprintk(dev , KERN_INFO , fmt , ## args)
383 /*-------------------------------------------------------------------------*/
385 /* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
386 * ep0 implementation: descriptors, config management, setup().
387 * also optional class-specific notification interrupt transfer.
391 * DESCRIPTORS ... most are static, but strings and (full) configuration
392 * descriptors are built on demand. For now we do either full CDC, or
393 * our simple subset, with RNDIS as an optional second configuration.
395 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
396 * the class descriptors match a modem (they're ignored; it's really just
397 * Ethernet functionality), they don't need the NOP altsetting, and the
398 * status transfer endpoint isn't optional.
401 #define STRING_MANUFACTURER 1
402 #define STRING_PRODUCT 2
403 #define STRING_ETHADDR 3
404 #define STRING_DATA 4
405 #define STRING_CONTROL 5
406 #define STRING_RNDIS_CONTROL 6
408 #define STRING_SUBSET 8
409 #define STRING_RNDIS 9
410 #define STRING_SERIALNUMBER 10
412 /* holds our biggest descriptor (or RNDIS response) */
413 #define USB_BUFSIZ 256
416 * This device advertises one configuration, eth_config, unless RNDIS
417 * is enabled (rndis_config) on hardware supporting at least two configs.
419 * NOTE: Controllers like superh_udc should probably be able to use
420 * an RNDIS-only configuration.
422 * FIXME define some higher-powered configurations to make it easier
423 * to recharge batteries ...
426 #define DEV_CONFIG_VALUE 1 /* cdc or subset */
427 #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
429 static struct usb_device_descriptor
431 .bLength = sizeof device_desc,
432 .bDescriptorType = USB_DT_DEVICE,
434 .bcdUSB = __constant_cpu_to_le16 (0x0200),
436 .bDeviceClass = USB_CLASS_COMM,
437 .bDeviceSubClass = 0,
438 .bDeviceProtocol = 0,
440 .idVendor = __constant_cpu_to_le16 (CDC_VENDOR_NUM),
441 .idProduct = __constant_cpu_to_le16 (CDC_PRODUCT_NUM),
442 .iManufacturer = STRING_MANUFACTURER,
443 .iProduct = STRING_PRODUCT,
444 .bNumConfigurations = 1,
447 static struct usb_otg_descriptor
449 .bLength = sizeof otg_descriptor,
450 .bDescriptorType = USB_DT_OTG,
452 .bmAttributes = USB_OTG_SRP,
455 static struct usb_config_descriptor
457 .bLength = sizeof eth_config,
458 .bDescriptorType = USB_DT_CONFIG,
460 /* compute wTotalLength on the fly */
462 .bConfigurationValue = DEV_CONFIG_VALUE,
463 .iConfiguration = STRING_CDC,
464 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
468 #ifdef CONFIG_USB_ETH_RNDIS
469 static struct usb_config_descriptor
471 .bLength = sizeof rndis_config,
472 .bDescriptorType = USB_DT_CONFIG,
474 /* compute wTotalLength on the fly */
476 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
477 .iConfiguration = STRING_RNDIS,
478 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
484 * Compared to the simple CDC subset, the full CDC Ethernet model adds
485 * three class descriptors, two interface descriptors, optional status
486 * endpoint. Both have a "data" interface and two bulk endpoints.
487 * There are also differences in how control requests are handled.
489 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of
490 * the CDC-ACM (modem) spec.
493 #ifdef DEV_CONFIG_CDC
494 static struct usb_interface_descriptor
496 .bLength = sizeof control_intf,
497 .bDescriptorType = USB_DT_INTERFACE,
499 .bInterfaceNumber = 0,
500 /* status endpoint is optional; this may be patched later */
502 .bInterfaceClass = USB_CLASS_COMM,
503 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
504 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
505 .iInterface = STRING_CONTROL,
509 #ifdef CONFIG_USB_ETH_RNDIS
510 static const struct usb_interface_descriptor
511 rndis_control_intf = {
512 .bLength = sizeof rndis_control_intf,
513 .bDescriptorType = USB_DT_INTERFACE,
515 .bInterfaceNumber = 0,
517 .bInterfaceClass = USB_CLASS_COMM,
518 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
519 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
520 .iInterface = STRING_RNDIS_CONTROL,
524 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
526 static const struct usb_cdc_header_desc header_desc = {
527 .bLength = sizeof header_desc,
528 .bDescriptorType = USB_DT_CS_INTERFACE,
529 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
531 .bcdCDC = __constant_cpu_to_le16 (0x0110),
534 static const struct usb_cdc_union_desc union_desc = {
535 .bLength = sizeof union_desc,
536 .bDescriptorType = USB_DT_CS_INTERFACE,
537 .bDescriptorSubType = USB_CDC_UNION_TYPE,
539 .bMasterInterface0 = 0, /* index of control interface */
540 .bSlaveInterface0 = 1, /* index of DATA interface */
543 #endif /* CDC || RNDIS */
545 #ifdef CONFIG_USB_ETH_RNDIS
547 static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
548 .bLength = sizeof call_mgmt_descriptor,
549 .bDescriptorType = USB_DT_CS_INTERFACE,
550 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
552 .bmCapabilities = 0x00,
553 .bDataInterface = 0x01,
556 static const struct usb_cdc_acm_descriptor acm_descriptor = {
557 .bLength = sizeof acm_descriptor,
558 .bDescriptorType = USB_DT_CS_INTERFACE,
559 .bDescriptorSubType = USB_CDC_ACM_TYPE,
561 .bmCapabilities = 0x00,
566 #ifdef DEV_CONFIG_CDC
568 static const struct usb_cdc_ether_desc ether_desc = {
569 .bLength = sizeof ether_desc,
570 .bDescriptorType = USB_DT_CS_INTERFACE,
571 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
573 /* this descriptor actually adds value, surprise! */
574 .iMACAddress = STRING_ETHADDR,
575 .bmEthernetStatistics = __constant_cpu_to_le32 (0), /* no statistics */
576 .wMaxSegmentSize = __constant_cpu_to_le16 (ETH_FRAME_LEN),
577 .wNumberMCFilters = __constant_cpu_to_le16 (0),
578 .bNumberPowerFilters = 0,
583 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
585 /* include the status endpoint if we can, even where it's optional.
586 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
587 * packet, to simplify cancellation; and a big transfer interval, to
588 * waste less bandwidth.
590 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
591 * if they ignore the connect/disconnect notifications that real aether
592 * can provide. more advanced cdc configurations might want to support
593 * encapsulated commands (vendor-specific, using control-OUT).
595 * RNDIS requires the status endpoint, since it uses that encapsulation
596 * mechanism for its funky RPC scheme.
599 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
600 #define STATUS_BYTECOUNT 16 /* 8 byte header + data */
602 static struct usb_endpoint_descriptor
604 .bLength = USB_DT_ENDPOINT_SIZE,
605 .bDescriptorType = USB_DT_ENDPOINT,
607 .bEndpointAddress = USB_DIR_IN,
608 .bmAttributes = USB_ENDPOINT_XFER_INT,
609 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
610 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
614 #ifdef DEV_CONFIG_CDC
616 /* the default data interface has no endpoints ... */
618 static const struct usb_interface_descriptor
620 .bLength = sizeof data_nop_intf,
621 .bDescriptorType = USB_DT_INTERFACE,
623 .bInterfaceNumber = 1,
624 .bAlternateSetting = 0,
626 .bInterfaceClass = USB_CLASS_CDC_DATA,
627 .bInterfaceSubClass = 0,
628 .bInterfaceProtocol = 0,
631 /* ... but the "real" data interface has two bulk endpoints */
633 static const struct usb_interface_descriptor
635 .bLength = sizeof data_intf,
636 .bDescriptorType = USB_DT_INTERFACE,
638 .bInterfaceNumber = 1,
639 .bAlternateSetting = 1,
641 .bInterfaceClass = USB_CLASS_CDC_DATA,
642 .bInterfaceSubClass = 0,
643 .bInterfaceProtocol = 0,
644 .iInterface = STRING_DATA,
649 #ifdef CONFIG_USB_ETH_RNDIS
651 /* RNDIS doesn't activate by changing to the "real" altsetting */
653 static const struct usb_interface_descriptor
655 .bLength = sizeof rndis_data_intf,
656 .bDescriptorType = USB_DT_INTERFACE,
658 .bInterfaceNumber = 1,
659 .bAlternateSetting = 0,
661 .bInterfaceClass = USB_CLASS_CDC_DATA,
662 .bInterfaceSubClass = 0,
663 .bInterfaceProtocol = 0,
664 .iInterface = STRING_DATA,
669 #ifdef DEV_CONFIG_SUBSET
672 * "Simple" CDC-subset option is a simple vendor-neutral model that most
673 * full speed controllers can handle: one interface, two bulk endpoints.
676 static const struct usb_interface_descriptor
678 .bLength = sizeof subset_data_intf,
679 .bDescriptorType = USB_DT_INTERFACE,
681 .bInterfaceNumber = 0,
682 .bAlternateSetting = 0,
684 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
685 .bInterfaceSubClass = 0,
686 .bInterfaceProtocol = 0,
687 .iInterface = STRING_DATA,
693 static struct usb_endpoint_descriptor
695 .bLength = USB_DT_ENDPOINT_SIZE,
696 .bDescriptorType = USB_DT_ENDPOINT,
698 .bEndpointAddress = USB_DIR_IN,
699 .bmAttributes = USB_ENDPOINT_XFER_BULK,
702 static struct usb_endpoint_descriptor
704 .bLength = USB_DT_ENDPOINT_SIZE,
705 .bDescriptorType = USB_DT_ENDPOINT,
707 .bEndpointAddress = USB_DIR_OUT,
708 .bmAttributes = USB_ENDPOINT_XFER_BULK,
711 static const struct usb_descriptor_header *fs_eth_function [11] = {
712 (struct usb_descriptor_header *) &otg_descriptor,
713 #ifdef DEV_CONFIG_CDC
714 /* "cdc" mode descriptors */
715 (struct usb_descriptor_header *) &control_intf,
716 (struct usb_descriptor_header *) &header_desc,
717 (struct usb_descriptor_header *) &union_desc,
718 (struct usb_descriptor_header *) ðer_desc,
719 /* NOTE: status endpoint may need to be removed */
720 (struct usb_descriptor_header *) &fs_status_desc,
721 /* data interface, with altsetting */
722 (struct usb_descriptor_header *) &data_nop_intf,
723 (struct usb_descriptor_header *) &data_intf,
724 (struct usb_descriptor_header *) &fs_source_desc,
725 (struct usb_descriptor_header *) &fs_sink_desc,
727 #endif /* DEV_CONFIG_CDC */
730 static inline void __init fs_subset_descriptors(void)
732 #ifdef DEV_CONFIG_SUBSET
733 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
734 fs_eth_function[2] = (struct usb_descriptor_header *) &fs_source_desc;
735 fs_eth_function[3] = (struct usb_descriptor_header *) &fs_sink_desc;
736 fs_eth_function[4] = NULL;
738 fs_eth_function[1] = NULL;
742 #ifdef CONFIG_USB_ETH_RNDIS
743 static const struct usb_descriptor_header *fs_rndis_function [] = {
744 (struct usb_descriptor_header *) &otg_descriptor,
745 /* control interface matches ACM, not Ethernet */
746 (struct usb_descriptor_header *) &rndis_control_intf,
747 (struct usb_descriptor_header *) &header_desc,
748 (struct usb_descriptor_header *) &call_mgmt_descriptor,
749 (struct usb_descriptor_header *) &acm_descriptor,
750 (struct usb_descriptor_header *) &union_desc,
751 (struct usb_descriptor_header *) &fs_status_desc,
752 /* data interface has no altsetting */
753 (struct usb_descriptor_header *) &rndis_data_intf,
754 (struct usb_descriptor_header *) &fs_source_desc,
755 (struct usb_descriptor_header *) &fs_sink_desc,
760 #ifdef CONFIG_USB_GADGET_DUALSPEED
763 * usb 2.0 devices need to expose both high speed and full speed
764 * descriptors, unless they only run at full speed.
767 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
768 static struct usb_endpoint_descriptor
770 .bLength = USB_DT_ENDPOINT_SIZE,
771 .bDescriptorType = USB_DT_ENDPOINT,
773 .bmAttributes = USB_ENDPOINT_XFER_INT,
774 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
775 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
777 #endif /* DEV_CONFIG_CDC */
779 static struct usb_endpoint_descriptor
781 .bLength = USB_DT_ENDPOINT_SIZE,
782 .bDescriptorType = USB_DT_ENDPOINT,
784 .bmAttributes = USB_ENDPOINT_XFER_BULK,
785 .wMaxPacketSize = __constant_cpu_to_le16 (512),
788 static struct usb_endpoint_descriptor
790 .bLength = USB_DT_ENDPOINT_SIZE,
791 .bDescriptorType = USB_DT_ENDPOINT,
793 .bmAttributes = USB_ENDPOINT_XFER_BULK,
794 .wMaxPacketSize = __constant_cpu_to_le16 (512),
797 static struct usb_qualifier_descriptor
799 .bLength = sizeof dev_qualifier,
800 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
802 .bcdUSB = __constant_cpu_to_le16 (0x0200),
803 .bDeviceClass = USB_CLASS_COMM,
805 .bNumConfigurations = 1,
808 static const struct usb_descriptor_header *hs_eth_function [11] = {
809 (struct usb_descriptor_header *) &otg_descriptor,
810 #ifdef DEV_CONFIG_CDC
811 /* "cdc" mode descriptors */
812 (struct usb_descriptor_header *) &control_intf,
813 (struct usb_descriptor_header *) &header_desc,
814 (struct usb_descriptor_header *) &union_desc,
815 (struct usb_descriptor_header *) ðer_desc,
816 /* NOTE: status endpoint may need to be removed */
817 (struct usb_descriptor_header *) &hs_status_desc,
818 /* data interface, with altsetting */
819 (struct usb_descriptor_header *) &data_nop_intf,
820 (struct usb_descriptor_header *) &data_intf,
821 (struct usb_descriptor_header *) &hs_source_desc,
822 (struct usb_descriptor_header *) &hs_sink_desc,
824 #endif /* DEV_CONFIG_CDC */
827 static inline void __init hs_subset_descriptors(void)
829 #ifdef DEV_CONFIG_SUBSET
830 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
831 hs_eth_function[2] = (struct usb_descriptor_header *) &fs_source_desc;
832 hs_eth_function[3] = (struct usb_descriptor_header *) &fs_sink_desc;
833 hs_eth_function[4] = NULL;
835 hs_eth_function[1] = NULL;
839 #ifdef CONFIG_USB_ETH_RNDIS
840 static const struct usb_descriptor_header *hs_rndis_function [] = {
841 (struct usb_descriptor_header *) &otg_descriptor,
842 /* control interface matches ACM, not Ethernet */
843 (struct usb_descriptor_header *) &rndis_control_intf,
844 (struct usb_descriptor_header *) &header_desc,
845 (struct usb_descriptor_header *) &call_mgmt_descriptor,
846 (struct usb_descriptor_header *) &acm_descriptor,
847 (struct usb_descriptor_header *) &union_desc,
848 (struct usb_descriptor_header *) &hs_status_desc,
849 /* data interface has no altsetting */
850 (struct usb_descriptor_header *) &rndis_data_intf,
851 (struct usb_descriptor_header *) &hs_source_desc,
852 (struct usb_descriptor_header *) &hs_sink_desc,
858 /* maxpacket and other transfer characteristics vary by speed. */
859 #define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
863 /* if there's no high speed support, maxpacket doesn't change. */
864 #define ep_desc(g,hs,fs) (((void)(g)), (fs))
866 static inline void __init hs_subset_descriptors(void)
870 #endif /* !CONFIG_USB_GADGET_DUALSPEED */
872 /*-------------------------------------------------------------------------*/
874 /* descriptors that are built on-demand */
876 static char manufacturer [50];
877 static char product_desc [40] = DRIVER_DESC;
878 static char serial_number [20];
880 #ifdef DEV_CONFIG_CDC
881 /* address that the host will use ... usually assigned at random */
882 static char ethaddr [2 * ETH_ALEN + 1];
885 /* static strings, in UTF-8 */
886 static struct usb_string strings [] = {
887 { STRING_MANUFACTURER, manufacturer, },
888 { STRING_PRODUCT, product_desc, },
889 { STRING_SERIALNUMBER, serial_number, },
890 { STRING_DATA, "Ethernet Data", },
891 #ifdef DEV_CONFIG_CDC
892 { STRING_CDC, "CDC Ethernet", },
893 { STRING_ETHADDR, ethaddr, },
894 { STRING_CONTROL, "CDC Communications Control", },
896 #ifdef DEV_CONFIG_SUBSET
897 { STRING_SUBSET, "CDC Ethernet Subset", },
899 #ifdef CONFIG_USB_ETH_RNDIS
900 { STRING_RNDIS, "RNDIS", },
901 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
903 { } /* end of list */
906 static struct usb_gadget_strings stringtab = {
907 .language = 0x0409, /* en-us */
912 * one config, two interfaces: control, data.
913 * complications: class descriptors, and an altsetting.
916 config_buf (enum usb_device_speed speed,
918 unsigned index, int is_otg)
921 const struct usb_config_descriptor *config;
922 const struct usb_descriptor_header **function;
923 #ifdef CONFIG_USB_GADGET_DUALSPEED
924 int hs = (speed == USB_SPEED_HIGH);
926 if (type == USB_DT_OTHER_SPEED_CONFIG)
928 #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
930 #define which_fn(t) (fs_ ## t ## _function)
933 if (index >= device_desc.bNumConfigurations)
936 #ifdef CONFIG_USB_ETH_RNDIS
937 /* list the RNDIS config first, to make Microsoft's drivers
938 * happy. DOCSIS 1.0 needs this too.
940 if (device_desc.bNumConfigurations == 2 && index == 0) {
941 config = &rndis_config;
942 function = which_fn (rndis);
946 config = ð_config;
947 function = which_fn (eth);
950 /* for now, don't advertise srp-only devices */
954 len = usb_gadget_config_buf (config, buf, USB_BUFSIZ, function);
957 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
961 /*-------------------------------------------------------------------------*/
963 static void eth_start (struct eth_dev *dev, gfp_t gfp_flags);
964 static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
967 set_ether_config (struct eth_dev *dev, gfp_t gfp_flags)
970 struct usb_gadget *gadget = dev->gadget;
972 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
973 /* status endpoint used for RNDIS and (optionally) CDC */
974 if (!subset_active(dev) && dev->status_ep) {
975 dev->status = ep_desc (gadget, &hs_status_desc,
977 dev->status_ep->driver_data = dev;
979 result = usb_ep_enable (dev->status_ep, dev->status);
981 DEBUG (dev, "enable %s --> %d\n",
982 dev->status_ep->name, result);
988 dev->in = ep_desc (dev->gadget, &hs_source_desc, &fs_source_desc);
989 dev->in_ep->driver_data = dev;
991 dev->out = ep_desc (dev->gadget, &hs_sink_desc, &fs_sink_desc);
992 dev->out_ep->driver_data = dev;
994 /* With CDC, the host isn't allowed to use these two data
995 * endpoints in the default altsetting for the interface.
996 * so we don't activate them yet. Reset from SET_INTERFACE.
998 * Strictly speaking RNDIS should work the same: activation is
999 * a side effect of setting a packet filter. Deactivation is
1000 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
1002 if (!cdc_active(dev)) {
1003 result = usb_ep_enable (dev->in_ep, dev->in);
1005 DEBUG(dev, "enable %s --> %d\n",
1006 dev->in_ep->name, result);
1010 result = usb_ep_enable (dev->out_ep, dev->out);
1012 DEBUG (dev, "enable %s --> %d\n",
1013 dev->in_ep->name, result);
1020 result = alloc_requests (dev, qlen (gadget), gfp_flags);
1022 /* on error, disable any endpoints */
1024 if (!subset_active(dev))
1025 (void) usb_ep_disable (dev->status_ep);
1027 (void) usb_ep_disable (dev->in_ep);
1028 (void) usb_ep_disable (dev->out_ep);
1033 /* activate non-CDC configs right away
1034 * this isn't strictly according to the RNDIS spec
1036 if (!cdc_active (dev)) {
1037 netif_carrier_on (dev->net);
1038 if (netif_running (dev->net)) {
1039 spin_unlock (&dev->lock);
1040 eth_start (dev, GFP_ATOMIC);
1041 spin_lock (&dev->lock);
1046 DEBUG (dev, "qlen %d\n", qlen (gadget));
1048 /* caller is responsible for cleanup on error */
1052 static void eth_reset_config (struct eth_dev *dev)
1054 struct usb_request *req;
1056 if (dev->config == 0)
1059 DEBUG (dev, "%s\n", __FUNCTION__);
1061 netif_stop_queue (dev->net);
1062 netif_carrier_off (dev->net);
1063 rndis_uninit(dev->rndis_config);
1065 /* disable endpoints, forcing (synchronous) completion of
1066 * pending i/o. then free the requests.
1069 usb_ep_disable (dev->in_ep);
1070 while (likely (!list_empty (&dev->tx_reqs))) {
1071 req = container_of (dev->tx_reqs.next,
1072 struct usb_request, list);
1073 list_del (&req->list);
1074 usb_ep_free_request (dev->in_ep, req);
1078 usb_ep_disable (dev->out_ep);
1079 while (likely (!list_empty (&dev->rx_reqs))) {
1080 req = container_of (dev->rx_reqs.next,
1081 struct usb_request, list);
1082 list_del (&req->list);
1083 usb_ep_free_request (dev->out_ep, req);
1088 usb_ep_disable (dev->status_ep);
1091 dev->cdc_filter = 0;
1095 /* change our operational config. must agree with the code
1096 * that returns config descriptors, and altsetting code.
1099 eth_set_config (struct eth_dev *dev, unsigned number, gfp_t gfp_flags)
1102 struct usb_gadget *gadget = dev->gadget;
1104 if (gadget_is_sa1100 (gadget)
1106 && atomic_read (&dev->tx_qlen) != 0) {
1107 /* tx fifo is full, but we can't clear it...*/
1108 INFO (dev, "can't change configurations\n");
1111 eth_reset_config (dev);
1114 case DEV_CONFIG_VALUE:
1115 result = set_ether_config (dev, gfp_flags);
1117 #ifdef CONFIG_USB_ETH_RNDIS
1118 case DEV_RNDIS_CONFIG_VALUE:
1120 result = set_ether_config (dev, gfp_flags);
1132 eth_reset_config (dev);
1133 usb_gadget_vbus_draw(dev->gadget,
1134 dev->gadget->is_otg ? 8 : 100);
1139 power = 2 * eth_config.bMaxPower;
1140 usb_gadget_vbus_draw(dev->gadget, power);
1142 switch (gadget->speed) {
1143 case USB_SPEED_FULL: speed = "full"; break;
1144 #ifdef CONFIG_USB_GADGET_DUALSPEED
1145 case USB_SPEED_HIGH: speed = "high"; break;
1147 default: speed = "?"; break;
1150 dev->config = number;
1151 INFO (dev, "%s speed config #%d: %d mA, %s, using %s\n",
1152 speed, number, power, driver_desc,
1157 : "CDC Ethernet Subset"));
1162 /*-------------------------------------------------------------------------*/
1164 #ifdef DEV_CONFIG_CDC
1166 /* The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
1167 * only to notify the host about link status changes (which we support) or
1168 * report completion of some encapsulated command (as used in RNDIS). Since
1169 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1170 * command mechanism; and only one status request is ever queued.
1173 static void eth_status_complete (struct usb_ep *ep, struct usb_request *req)
1175 struct usb_cdc_notification *event = req->buf;
1176 int value = req->status;
1177 struct eth_dev *dev = ep->driver_data;
1179 /* issue the second notification if host reads the first */
1180 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1182 __le32 *data = req->buf + sizeof *event;
1184 event->bmRequestType = 0xA1;
1185 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
1186 event->wValue = __constant_cpu_to_le16 (0);
1187 event->wIndex = __constant_cpu_to_le16 (1);
1188 event->wLength = __constant_cpu_to_le16 (8);
1190 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1191 data [0] = data [1] = cpu_to_le32 (BITRATE (dev->gadget));
1193 req->length = STATUS_BYTECOUNT;
1194 value = usb_ep_queue (ep, req, GFP_ATOMIC);
1195 DEBUG (dev, "send SPEED_CHANGE --> %d\n", value);
1198 } else if (value != -ECONNRESET)
1199 DEBUG (dev, "event %02x --> %d\n",
1200 event->bNotificationType, value);
1201 req->context = NULL;
1204 static void issue_start_status (struct eth_dev *dev)
1206 struct usb_request *req = dev->stat_req;
1207 struct usb_cdc_notification *event;
1210 DEBUG (dev, "%s, flush old status first\n", __FUNCTION__);
1214 * FIXME ugly idiom, maybe we'd be better with just
1215 * a "cancel the whole queue" primitive since any
1216 * unlink-one primitive has way too many error modes.
1217 * here, we "know" toggle is already clear...
1219 * FIXME iff req->context != null just dequeue it
1221 usb_ep_disable (dev->status_ep);
1222 usb_ep_enable (dev->status_ep, dev->status);
1224 /* 3.8.1 says to issue first NETWORK_CONNECTION, then
1225 * a SPEED_CHANGE. could be useful in some configs.
1228 event->bmRequestType = 0xA1;
1229 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
1230 event->wValue = __constant_cpu_to_le16 (1); /* connected */
1231 event->wIndex = __constant_cpu_to_le16 (1);
1234 req->length = sizeof *event;
1235 req->complete = eth_status_complete;
1238 value = usb_ep_queue (dev->status_ep, req, GFP_ATOMIC);
1240 DEBUG (dev, "status buf queue --> %d\n", value);
1245 /*-------------------------------------------------------------------------*/
1247 static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req)
1249 if (req->status || req->actual != req->length)
1250 DEBUG ((struct eth_dev *) ep->driver_data,
1251 "setup complete --> %d, %d/%d\n",
1252 req->status, req->actual, req->length);
1255 #ifdef CONFIG_USB_ETH_RNDIS
1257 static void rndis_response_complete (struct usb_ep *ep, struct usb_request *req)
1259 if (req->status || req->actual != req->length)
1260 DEBUG ((struct eth_dev *) ep->driver_data,
1261 "rndis response complete --> %d, %d/%d\n",
1262 req->status, req->actual, req->length);
1264 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1267 static void rndis_command_complete (struct usb_ep *ep, struct usb_request *req)
1269 struct eth_dev *dev = ep->driver_data;
1272 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1273 spin_lock(&dev->lock);
1274 status = rndis_msg_parser (dev->rndis_config, (u8 *) req->buf);
1276 ERROR(dev, "%s: rndis parse error %d\n", __FUNCTION__, status);
1277 spin_unlock(&dev->lock);
1283 * The setup() callback implements all the ep0 functionality that's not
1284 * handled lower down. CDC has a number of less-common features:
1286 * - two interfaces: control, and ethernet data
1287 * - Ethernet data interface has two altsettings: default, and active
1288 * - class-specific descriptors for the control interface
1289 * - class-specific control requests
1292 eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1294 struct eth_dev *dev = get_gadget_data (gadget);
1295 struct usb_request *req = dev->req;
1296 int value = -EOPNOTSUPP;
1297 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1298 u16 wValue = le16_to_cpu(ctrl->wValue);
1299 u16 wLength = le16_to_cpu(ctrl->wLength);
1301 /* descriptors just go into the pre-allocated ep0 buffer,
1302 * while config change events may enable network traffic.
1304 req->complete = eth_setup_complete;
1305 switch (ctrl->bRequest) {
1307 case USB_REQ_GET_DESCRIPTOR:
1308 if (ctrl->bRequestType != USB_DIR_IN)
1310 switch (wValue >> 8) {
1313 value = min (wLength, (u16) sizeof device_desc);
1314 memcpy (req->buf, &device_desc, value);
1316 #ifdef CONFIG_USB_GADGET_DUALSPEED
1317 case USB_DT_DEVICE_QUALIFIER:
1318 if (!gadget->is_dualspeed)
1320 value = min (wLength, (u16) sizeof dev_qualifier);
1321 memcpy (req->buf, &dev_qualifier, value);
1324 case USB_DT_OTHER_SPEED_CONFIG:
1325 if (!gadget->is_dualspeed)
1328 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1330 value = config_buf (gadget->speed, req->buf,
1335 value = min (wLength, (u16) value);
1339 value = usb_gadget_get_string (&stringtab,
1340 wValue & 0xff, req->buf);
1342 value = min (wLength, (u16) value);
1347 case USB_REQ_SET_CONFIGURATION:
1348 if (ctrl->bRequestType != 0)
1350 if (gadget->a_hnp_support)
1351 DEBUG (dev, "HNP available\n");
1352 else if (gadget->a_alt_hnp_support)
1353 DEBUG (dev, "HNP needs a different root port\n");
1354 spin_lock (&dev->lock);
1355 value = eth_set_config (dev, wValue, GFP_ATOMIC);
1356 spin_unlock (&dev->lock);
1358 case USB_REQ_GET_CONFIGURATION:
1359 if (ctrl->bRequestType != USB_DIR_IN)
1361 *(u8 *)req->buf = dev->config;
1362 value = min (wLength, (u16) 1);
1365 case USB_REQ_SET_INTERFACE:
1366 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1370 if (!cdc_active(dev) && wIndex != 0)
1372 spin_lock (&dev->lock);
1374 /* PXA hardware partially handles SET_INTERFACE;
1375 * we need to kluge around that interference.
1377 if (gadget_is_pxa (gadget)) {
1378 value = eth_set_config (dev, DEV_CONFIG_VALUE,
1383 #ifdef DEV_CONFIG_CDC
1385 case 0: /* control/master intf */
1389 usb_ep_disable (dev->status_ep);
1390 usb_ep_enable (dev->status_ep, dev->status);
1394 case 1: /* data intf */
1397 usb_ep_disable (dev->in_ep);
1398 usb_ep_disable (dev->out_ep);
1400 /* CDC requires the data transfers not be done from
1401 * the default interface setting ... also, setting
1402 * the non-default interface resets filters etc.
1405 if (!cdc_active (dev))
1407 usb_ep_enable (dev->in_ep, dev->in);
1408 usb_ep_enable (dev->out_ep, dev->out);
1409 dev->cdc_filter = DEFAULT_FILTER;
1410 netif_carrier_on (dev->net);
1412 issue_start_status (dev);
1413 if (netif_running (dev->net)) {
1414 spin_unlock (&dev->lock);
1415 eth_start (dev, GFP_ATOMIC);
1416 spin_lock (&dev->lock);
1419 netif_stop_queue (dev->net);
1420 netif_carrier_off (dev->net);
1426 /* FIXME this is wrong, as is the assumption that
1427 * all non-PXA hardware talks real CDC ...
1429 dev_warn (&gadget->dev, "set_interface ignored!\n");
1430 #endif /* DEV_CONFIG_CDC */
1433 spin_unlock (&dev->lock);
1435 case USB_REQ_GET_INTERFACE:
1436 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1440 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
1443 /* for CDC, iff carrier is on, data interface is active. */
1444 if (rndis_active(dev) || wIndex != 1)
1445 *(u8 *)req->buf = 0;
1447 *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0;
1448 value = min (wLength, (u16) 1);
1451 #ifdef DEV_CONFIG_CDC
1452 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
1453 /* see 6.2.30: no data, wIndex = interface,
1454 * wValue = packet filter bitmap
1456 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1461 DEBUG (dev, "packet filter %02x\n", wValue);
1462 dev->cdc_filter = wValue;
1467 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1468 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1469 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1470 * case USB_CDC_GET_ETHERNET_STATISTIC:
1473 #endif /* DEV_CONFIG_CDC */
1475 #ifdef CONFIG_USB_ETH_RNDIS
1476 /* RNDIS uses the CDC command encapsulation mechanism to implement
1477 * an RPC scheme, with much getting/setting of attributes by OID.
1479 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1480 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1481 || !rndis_active(dev)
1482 || wLength > USB_BUFSIZ
1484 || rndis_control_intf.bInterfaceNumber
1487 /* read the request, then process it */
1489 req->complete = rndis_command_complete;
1490 /* later, rndis_control_ack () sends a notification */
1493 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1494 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1495 == ctrl->bRequestType
1496 && rndis_active(dev)
1497 // && wLength >= 0x0400
1499 && rndis_control_intf.bInterfaceNumber
1503 /* return the result */
1504 buf = rndis_get_next_response (dev->rndis_config,
1507 memcpy (req->buf, buf, value);
1508 req->complete = rndis_response_complete;
1509 rndis_free_response(dev->rndis_config, buf);
1511 /* else stalls ... spec says to avoid that */
1518 "unknown control req%02x.%02x v%04x i%04x l%d\n",
1519 ctrl->bRequestType, ctrl->bRequest,
1520 wValue, wIndex, wLength);
1523 /* respond with data transfer before status phase? */
1525 req->length = value;
1526 req->zero = value < wLength
1527 && (value % gadget->ep0->maxpacket) == 0;
1528 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1530 DEBUG (dev, "ep_queue --> %d\n", value);
1532 eth_setup_complete (gadget->ep0, req);
1536 /* host either stalls (value < 0) or reports success */
1541 eth_disconnect (struct usb_gadget *gadget)
1543 struct eth_dev *dev = get_gadget_data (gadget);
1544 unsigned long flags;
1546 spin_lock_irqsave (&dev->lock, flags);
1547 netif_stop_queue (dev->net);
1548 netif_carrier_off (dev->net);
1549 eth_reset_config (dev);
1550 spin_unlock_irqrestore (&dev->lock, flags);
1552 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1554 /* next we may get setup() calls to enumerate new connections;
1555 * or an unbind() during shutdown (including removing module).
1559 /*-------------------------------------------------------------------------*/
1561 /* NETWORK DRIVER HOOKUP (to the layer above this driver) */
1563 static int eth_change_mtu (struct net_device *net, int new_mtu)
1565 struct eth_dev *dev = netdev_priv(net);
1570 if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN)
1572 /* no zero-length packet read wanted after mtu-sized packets */
1573 if (((new_mtu + sizeof (struct ethhdr)) % dev->in_ep->maxpacket) == 0)
1579 static struct net_device_stats *eth_get_stats (struct net_device *net)
1581 return &((struct eth_dev *)netdev_priv(net))->stats;
1584 static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
1586 struct eth_dev *dev = netdev_priv(net);
1587 strlcpy(p->driver, shortname, sizeof p->driver);
1588 strlcpy(p->version, DRIVER_VERSION, sizeof p->version);
1589 strlcpy(p->fw_version, dev->gadget->name, sizeof p->fw_version);
1590 strlcpy (p->bus_info, dev->gadget->dev.bus_id, sizeof p->bus_info);
1593 static u32 eth_get_link(struct net_device *net)
1595 struct eth_dev *dev = netdev_priv(net);
1596 return dev->gadget->speed != USB_SPEED_UNKNOWN;
1599 static struct ethtool_ops ops = {
1600 .get_drvinfo = eth_get_drvinfo,
1601 .get_link = eth_get_link
1604 static void defer_kevent (struct eth_dev *dev, int flag)
1606 if (test_and_set_bit (flag, &dev->todo))
1608 if (!schedule_work (&dev->work))
1609 ERROR (dev, "kevent %d may have been dropped\n", flag);
1611 DEBUG (dev, "kevent %d scheduled\n", flag);
1614 static void rx_complete (struct usb_ep *ep, struct usb_request *req);
1617 rx_submit (struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
1619 struct sk_buff *skb;
1620 int retval = -ENOMEM;
1623 /* Padding up to RX_EXTRA handles minor disagreements with host.
1624 * Normally we use the USB "terminate on short read" convention;
1625 * so allow up to (N*maxpacket), since that memory is normally
1626 * already allocated. Some hardware doesn't deal well with short
1627 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1628 * byte off the end (to force hardware errors on overflow).
1630 * RNDIS uses internal framing, and explicitly allows senders to
1631 * pad to end-of-packet. That's potentially nice for speed,
1632 * but means receivers can't recover synch on their own.
1634 size = (sizeof (struct ethhdr) + dev->net->mtu + RX_EXTRA);
1635 size += dev->out_ep->maxpacket - 1;
1636 if (rndis_active(dev))
1637 size += sizeof (struct rndis_packet_msg_type);
1638 size -= size % dev->out_ep->maxpacket;
1640 if ((skb = alloc_skb (size + NET_IP_ALIGN, gfp_flags)) == 0) {
1641 DEBUG (dev, "no rx skb\n");
1645 /* Some platforms perform better when IP packets are aligned,
1646 * but on at least one, checksumming fails otherwise. Note:
1647 * RNDIS headers involve variable numbers of LE32 values.
1649 skb_reserve(skb, NET_IP_ALIGN);
1651 req->buf = skb->data;
1653 req->complete = rx_complete;
1656 retval = usb_ep_queue (dev->out_ep, req, gfp_flags);
1657 if (retval == -ENOMEM)
1659 defer_kevent (dev, WORK_RX_MEMORY);
1661 DEBUG (dev, "rx submit --> %d\n", retval);
1662 dev_kfree_skb_any (skb);
1663 spin_lock (&dev->lock);
1664 list_add (&req->list, &dev->rx_reqs);
1665 spin_unlock (&dev->lock);
1670 static void rx_complete (struct usb_ep *ep, struct usb_request *req)
1672 struct sk_buff *skb = req->context;
1673 struct eth_dev *dev = ep->driver_data;
1674 int status = req->status;
1678 /* normal completion */
1680 skb_put (skb, req->actual);
1681 /* we know MaxPacketsPerTransfer == 1 here */
1682 if (rndis_active(dev))
1683 status = rndis_rm_hdr (skb);
1685 || ETH_HLEN > skb->len
1686 || skb->len > ETH_FRAME_LEN) {
1687 dev->stats.rx_errors++;
1688 dev->stats.rx_length_errors++;
1689 DEBUG (dev, "rx length %d\n", skb->len);
1693 skb->dev = dev->net;
1694 skb->protocol = eth_type_trans (skb, dev->net);
1695 dev->stats.rx_packets++;
1696 dev->stats.rx_bytes += skb->len;
1698 /* no buffer copies needed, unless hardware can't
1701 status = netif_rx (skb);
1705 /* software-driven interface shutdown */
1706 case -ECONNRESET: // unlink
1707 case -ESHUTDOWN: // disconnect etc
1708 VDEBUG (dev, "rx shutdown, code %d\n", status);
1711 /* for hardware automagic (such as pxa) */
1712 case -ECONNABORTED: // endpoint reset
1713 DEBUG (dev, "rx %s reset\n", ep->name);
1714 defer_kevent (dev, WORK_RX_MEMORY);
1716 dev_kfree_skb_any (skb);
1721 dev->stats.rx_over_errors++;
1725 dev->stats.rx_errors++;
1726 DEBUG (dev, "rx status %d\n", status);
1731 dev_kfree_skb_any (skb);
1732 if (!netif_running (dev->net)) {
1734 /* nobody reading rx_reqs, so no dev->lock */
1735 list_add (&req->list, &dev->rx_reqs);
1739 rx_submit (dev, req, GFP_ATOMIC);
1742 static int prealloc (struct list_head *list, struct usb_ep *ep,
1743 unsigned n, gfp_t gfp_flags)
1746 struct usb_request *req;
1751 /* queue/recycle up to N requests */
1753 list_for_each_entry (req, list, list) {
1758 req = usb_ep_alloc_request (ep, gfp_flags);
1760 return list_empty (list) ? -ENOMEM : 0;
1761 list_add (&req->list, list);
1768 struct list_head *next;
1770 next = req->list.next;
1771 list_del (&req->list);
1772 usb_ep_free_request (ep, req);
1777 req = container_of (next, struct usb_request, list);
1782 static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
1786 status = prealloc (&dev->tx_reqs, dev->in_ep, n, gfp_flags);
1789 status = prealloc (&dev->rx_reqs, dev->out_ep, n, gfp_flags);
1794 DEBUG (dev, "can't alloc requests\n");
1798 static void rx_fill (struct eth_dev *dev, gfp_t gfp_flags)
1800 struct usb_request *req;
1801 unsigned long flags;
1803 /* fill unused rxq slots with some skb */
1804 spin_lock_irqsave (&dev->lock, flags);
1805 while (!list_empty (&dev->rx_reqs)) {
1806 req = container_of (dev->rx_reqs.next,
1807 struct usb_request, list);
1808 list_del_init (&req->list);
1809 spin_unlock_irqrestore (&dev->lock, flags);
1811 if (rx_submit (dev, req, gfp_flags) < 0) {
1812 defer_kevent (dev, WORK_RX_MEMORY);
1816 spin_lock_irqsave (&dev->lock, flags);
1818 spin_unlock_irqrestore (&dev->lock, flags);
1821 static void eth_work (void *_dev)
1823 struct eth_dev *dev = _dev;
1825 if (test_and_clear_bit (WORK_RX_MEMORY, &dev->todo)) {
1826 if (netif_running (dev->net))
1827 rx_fill (dev, GFP_KERNEL);
1831 DEBUG (dev, "work done, flags = 0x%lx\n", dev->todo);
1834 static void tx_complete (struct usb_ep *ep, struct usb_request *req)
1836 struct sk_buff *skb = req->context;
1837 struct eth_dev *dev = ep->driver_data;
1839 switch (req->status) {
1841 dev->stats.tx_errors++;
1842 VDEBUG (dev, "tx err %d\n", req->status);
1844 case -ECONNRESET: // unlink
1845 case -ESHUTDOWN: // disconnect etc
1848 dev->stats.tx_bytes += skb->len;
1850 dev->stats.tx_packets++;
1852 spin_lock (&dev->lock);
1853 list_add (&req->list, &dev->tx_reqs);
1854 spin_unlock (&dev->lock);
1855 dev_kfree_skb_any (skb);
1857 atomic_dec (&dev->tx_qlen);
1858 if (netif_carrier_ok (dev->net))
1859 netif_wake_queue (dev->net);
1862 static inline int eth_is_promisc (struct eth_dev *dev)
1864 /* no filters for the CDC subset; always promisc */
1865 if (subset_active (dev))
1867 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1870 static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1872 struct eth_dev *dev = netdev_priv(net);
1873 int length = skb->len;
1875 struct usb_request *req = NULL;
1876 unsigned long flags;
1878 /* apply outgoing CDC or RNDIS filters */
1879 if (!eth_is_promisc (dev)) {
1880 u8 *dest = skb->data;
1882 if (dest [0] & 0x01) {
1885 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1886 * SET_ETHERNET_MULTICAST_FILTERS requests
1888 if (memcmp (dest, net->broadcast, ETH_ALEN) == 0)
1889 type = USB_CDC_PACKET_TYPE_BROADCAST;
1891 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1892 if (!(dev->cdc_filter & type)) {
1893 dev_kfree_skb_any (skb);
1897 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1900 spin_lock_irqsave (&dev->lock, flags);
1901 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1902 list_del (&req->list);
1903 if (list_empty (&dev->tx_reqs))
1904 netif_stop_queue (net);
1905 spin_unlock_irqrestore (&dev->lock, flags);
1907 /* no buffer copies needed, unless the network stack did it
1908 * or the hardware can't use skb buffers.
1909 * or there's not enough space for any RNDIS headers we need
1911 if (rndis_active(dev)) {
1912 struct sk_buff *skb_rndis;
1914 skb_rndis = skb_realloc_headroom (skb,
1915 sizeof (struct rndis_packet_msg_type));
1919 dev_kfree_skb_any (skb);
1921 rndis_add_hdr (skb);
1924 req->buf = skb->data;
1926 req->complete = tx_complete;
1928 /* use zlp framing on tx for strict CDC-Ether conformance,
1929 * though any robust network rx path ignores extra padding.
1930 * and some hardware doesn't like to write zlps.
1933 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1936 req->length = length;
1938 #ifdef CONFIG_USB_GADGET_DUALSPEED
1939 /* throttle highspeed IRQ rate back slightly */
1940 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1941 ? ((atomic_read (&dev->tx_qlen) % TX_DELAY) != 0)
1945 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1948 DEBUG (dev, "tx queue err %d\n", retval);
1951 net->trans_start = jiffies;
1952 atomic_inc (&dev->tx_qlen);
1957 dev->stats.tx_dropped++;
1958 dev_kfree_skb_any (skb);
1959 spin_lock_irqsave (&dev->lock, flags);
1960 if (list_empty (&dev->tx_reqs))
1961 netif_start_queue (net);
1962 list_add (&req->list, &dev->tx_reqs);
1963 spin_unlock_irqrestore (&dev->lock, flags);
1968 /*-------------------------------------------------------------------------*/
1970 #ifdef CONFIG_USB_ETH_RNDIS
1972 /* The interrupt endpoint is used in RNDIS to notify the host when messages
1973 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1974 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1975 * REMOTE_NDIS_KEEPALIVE_MSG.
1977 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1978 * normally just one notification will be queued.
1981 static struct usb_request *eth_req_alloc (struct usb_ep *, unsigned, gfp_t);
1982 static void eth_req_free (struct usb_ep *ep, struct usb_request *req);
1985 rndis_control_ack_complete (struct usb_ep *ep, struct usb_request *req)
1987 struct eth_dev *dev = ep->driver_data;
1989 if (req->status || req->actual != req->length)
1991 "rndis control ack complete --> %d, %d/%d\n",
1992 req->status, req->actual, req->length);
1993 req->context = NULL;
1995 if (req != dev->stat_req)
1996 eth_req_free(ep, req);
1999 static int rndis_control_ack (struct net_device *net)
2001 struct eth_dev *dev = netdev_priv(net);
2003 struct usb_request *resp = dev->stat_req;
2005 /* in case RNDIS calls this after disconnect */
2007 DEBUG (dev, "status ENODEV\n");
2011 /* in case queue length > 1 */
2012 if (resp->context) {
2013 resp = eth_req_alloc (dev->status_ep, 8, GFP_ATOMIC);
2018 /* Send RNDIS RESPONSE_AVAILABLE notification;
2019 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
2022 resp->complete = rndis_control_ack_complete;
2023 resp->context = dev;
2025 *((__le32 *) resp->buf) = __constant_cpu_to_le32 (1);
2026 *((__le32 *) resp->buf + 1) = __constant_cpu_to_le32 (0);
2028 length = usb_ep_queue (dev->status_ep, resp, GFP_ATOMIC);
2031 rndis_control_ack_complete (dev->status_ep, resp);
2039 #define rndis_control_ack NULL
2043 static void eth_start (struct eth_dev *dev, gfp_t gfp_flags)
2045 DEBUG (dev, "%s\n", __FUNCTION__);
2047 /* fill the rx queue */
2048 rx_fill (dev, gfp_flags);
2050 /* and open the tx floodgates */
2051 atomic_set (&dev->tx_qlen, 0);
2052 netif_wake_queue (dev->net);
2053 if (rndis_active(dev)) {
2054 rndis_set_param_medium (dev->rndis_config,
2056 BITRATE(dev->gadget)/100);
2057 (void) rndis_signal_connect (dev->rndis_config);
2061 static int eth_open (struct net_device *net)
2063 struct eth_dev *dev = netdev_priv(net);
2065 DEBUG (dev, "%s\n", __FUNCTION__);
2066 if (netif_carrier_ok (dev->net))
2067 eth_start (dev, GFP_KERNEL);
2071 static int eth_stop (struct net_device *net)
2073 struct eth_dev *dev = netdev_priv(net);
2075 VDEBUG (dev, "%s\n", __FUNCTION__);
2076 netif_stop_queue (net);
2078 DEBUG (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
2079 dev->stats.rx_packets, dev->stats.tx_packets,
2080 dev->stats.rx_errors, dev->stats.tx_errors
2083 /* ensure there are no more active requests */
2085 usb_ep_disable (dev->in_ep);
2086 usb_ep_disable (dev->out_ep);
2087 if (netif_carrier_ok (dev->net)) {
2088 DEBUG (dev, "host still using in/out endpoints\n");
2089 // FIXME idiom may leave toggle wrong here
2090 usb_ep_enable (dev->in_ep, dev->in);
2091 usb_ep_enable (dev->out_ep, dev->out);
2093 if (dev->status_ep) {
2094 usb_ep_disable (dev->status_ep);
2095 usb_ep_enable (dev->status_ep, dev->status);
2099 if (rndis_active(dev)) {
2100 rndis_set_param_medium (dev->rndis_config,
2101 NDIS_MEDIUM_802_3, 0);
2102 (void) rndis_signal_disconnect (dev->rndis_config);
2108 /*-------------------------------------------------------------------------*/
2110 static struct usb_request *
2111 eth_req_alloc (struct usb_ep *ep, unsigned size, gfp_t gfp_flags)
2113 struct usb_request *req;
2115 req = usb_ep_alloc_request (ep, gfp_flags);
2119 req->buf = kmalloc (size, gfp_flags);
2121 usb_ep_free_request (ep, req);
2128 eth_req_free (struct usb_ep *ep, struct usb_request *req)
2131 usb_ep_free_request (ep, req);
2136 eth_unbind (struct usb_gadget *gadget)
2138 struct eth_dev *dev = get_gadget_data (gadget);
2140 DEBUG (dev, "unbind\n");
2141 rndis_deregister (dev->rndis_config);
2144 /* we've already been disconnected ... no i/o is active */
2146 eth_req_free (gadget->ep0, dev->req);
2149 if (dev->stat_req) {
2150 eth_req_free (dev->status_ep, dev->stat_req);
2151 dev->stat_req = NULL;
2154 unregister_netdev (dev->net);
2155 free_netdev(dev->net);
2157 /* assuming we used keventd, it must quiesce too */
2158 flush_scheduled_work ();
2159 set_gadget_data (gadget, NULL);
2162 static u8 __init nibble (unsigned char c)
2164 if (likely (isdigit (c)))
2167 if (likely (isxdigit (c)))
2168 return 10 + c - 'A';
2172 static int __init get_ether_addr(const char *str, u8 *dev_addr)
2177 for (i = 0; i < 6; i++) {
2180 if((*str == '.') || (*str == ':'))
2182 num = nibble(*str++) << 4;
2183 num |= (nibble(*str++));
2186 if (is_valid_ether_addr (dev_addr))
2189 random_ether_addr(dev_addr);
2194 eth_bind (struct usb_gadget *gadget)
2196 struct eth_dev *dev;
2197 struct net_device *net;
2198 u8 cdc = 1, zlp = 1, rndis = 1;
2199 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
2200 int status = -ENOMEM;
2203 /* these flags are only ever cleared; compiler take note */
2204 #ifndef DEV_CONFIG_CDC
2207 #ifndef CONFIG_USB_ETH_RNDIS
2211 /* Because most host side USB stacks handle CDC Ethernet, that
2212 * standard protocol is _strongly_ preferred for interop purposes.
2213 * (By everyone except Microsoft.)
2215 if (gadget_is_pxa (gadget)) {
2216 /* pxa doesn't support altsettings */
2218 } else if (gadget_is_sh(gadget)) {
2219 /* sh doesn't support multiple interfaces or configs */
2222 } else if (gadget_is_sa1100 (gadget)) {
2223 /* hardware can't write zlps */
2225 /* sa1100 CAN do CDC, without status endpoint ... we use
2226 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2231 gcnum = usb_gadget_controller_number (gadget);
2233 device_desc.bcdDevice = cpu_to_le16 (0x0200 + gcnum);
2235 /* can't assume CDC works. don't want to default to
2236 * anything less functional on CDC-capable hardware,
2237 * so we fail in this case.
2239 dev_err (&gadget->dev,
2240 "controller '%s' not recognized\n",
2244 snprintf (manufacturer, sizeof manufacturer, "%s %s/%s",
2245 system_utsname.sysname, system_utsname.release,
2248 /* If there's an RNDIS configuration, that's what Windows wants to
2249 * be using ... so use these product IDs here and in the "linux.inf"
2250 * needed to install MSFT drivers. Current Linux kernels will use
2251 * the second configuration if it's CDC Ethernet, and need some help
2252 * to choose the right configuration otherwise.
2255 device_desc.idVendor =
2256 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2257 device_desc.idProduct =
2258 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2259 snprintf (product_desc, sizeof product_desc,
2260 "RNDIS/%s", driver_desc);
2262 /* CDC subset ... recognized by Linux since 2.4.10, but Windows
2263 * drivers aren't widely available.
2266 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2267 device_desc.idVendor =
2268 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2269 device_desc.idProduct =
2270 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2273 /* support optional vendor/distro customization */
2276 dev_err (&gadget->dev, "idVendor needs idProduct!\n");
2279 device_desc.idVendor = cpu_to_le16(idVendor);
2280 device_desc.idProduct = cpu_to_le16(idProduct);
2282 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2285 strlcpy (manufacturer, iManufacturer, sizeof manufacturer);
2287 strlcpy (product_desc, iProduct, sizeof product_desc);
2288 if (iSerialNumber) {
2289 device_desc.iSerialNumber = STRING_SERIALNUMBER,
2290 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
2293 /* all we really need is bulk IN/OUT */
2294 usb_ep_autoconfig_reset (gadget);
2295 in_ep = usb_ep_autoconfig (gadget, &fs_source_desc);
2298 dev_err (&gadget->dev,
2299 "can't autoconfigure on %s\n",
2303 in_ep->driver_data = in_ep; /* claim */
2305 out_ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
2308 out_ep->driver_data = out_ep; /* claim */
2310 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2311 /* CDC Ethernet control interface doesn't require a status endpoint.
2312 * Since some hosts expect one, try to allocate one anyway.
2315 status_ep = usb_ep_autoconfig (gadget, &fs_status_desc);
2317 status_ep->driver_data = status_ep; /* claim */
2319 dev_err (&gadget->dev,
2320 "can't run RNDIS on %s\n",
2323 #ifdef DEV_CONFIG_CDC
2324 /* pxa25x only does CDC subset; often used with RNDIS */
2326 control_intf.bNumEndpoints = 0;
2327 /* FIXME remove endpoint from descriptor list */
2333 /* one config: cdc, else minimal subset */
2335 eth_config.bNumInterfaces = 1;
2336 eth_config.iConfiguration = STRING_SUBSET;
2337 fs_subset_descriptors();
2338 hs_subset_descriptors();
2341 /* For now RNDIS is always a second config */
2343 device_desc.bNumConfigurations = 2;
2345 #ifdef CONFIG_USB_GADGET_DUALSPEED
2347 dev_qualifier.bNumConfigurations = 2;
2349 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2351 /* assumes ep0 uses the same value for both speeds ... */
2352 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2354 /* and that all endpoints are dual-speed */
2355 hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
2356 hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
2357 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2359 hs_status_desc.bEndpointAddress =
2360 fs_status_desc.bEndpointAddress;
2362 #endif /* DUALSPEED */
2364 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
2365 usb_gadget_set_selfpowered (gadget);
2367 if (gadget->is_otg) {
2368 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2369 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2370 eth_config.bMaxPower = 4;
2371 #ifdef CONFIG_USB_ETH_RNDIS
2372 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2373 rndis_config.bMaxPower = 4;
2377 net = alloc_etherdev (sizeof *dev);
2380 dev = netdev_priv(net);
2381 spin_lock_init (&dev->lock);
2382 INIT_WORK (&dev->work, eth_work, dev);
2383 INIT_LIST_HEAD (&dev->tx_reqs);
2384 INIT_LIST_HEAD (&dev->rx_reqs);
2386 /* network device setup */
2388 SET_MODULE_OWNER (net);
2389 strcpy (net->name, "usb%d");
2394 dev->out_ep = out_ep;
2395 dev->status_ep = status_ep;
2397 /* Module params for these addresses should come from ID proms.
2398 * The host side address is used with CDC and RNDIS, and commonly
2399 * ends up in a persistent config database.
2401 if (get_ether_addr(dev_addr, net->dev_addr))
2402 dev_warn(&gadget->dev,
2403 "using random %s ethernet address\n", "self");
2405 if (get_ether_addr(host_addr, dev->host_mac))
2406 dev_warn(&gadget->dev,
2407 "using random %s ethernet address\n", "host");
2408 #ifdef DEV_CONFIG_CDC
2409 snprintf (ethaddr, sizeof ethaddr, "%02X%02X%02X%02X%02X%02X",
2410 dev->host_mac [0], dev->host_mac [1],
2411 dev->host_mac [2], dev->host_mac [3],
2412 dev->host_mac [4], dev->host_mac [5]);
2417 status = rndis_init();
2419 dev_err (&gadget->dev, "can't init RNDIS, %d\n",
2425 net->change_mtu = eth_change_mtu;
2426 net->get_stats = eth_get_stats;
2427 net->hard_start_xmit = eth_start_xmit;
2428 net->open = eth_open;
2429 net->stop = eth_stop;
2430 // watchdog_timeo, tx_timeout ...
2431 // set_multicast_list
2432 SET_ETHTOOL_OPS(net, &ops);
2434 /* preallocate control message data and buffer */
2435 dev->req = eth_req_alloc (gadget->ep0, USB_BUFSIZ, GFP_KERNEL);
2438 dev->req->complete = eth_setup_complete;
2440 /* ... and maybe likewise for status transfer */
2441 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2442 if (dev->status_ep) {
2443 dev->stat_req = eth_req_alloc (dev->status_ep,
2444 STATUS_BYTECOUNT, GFP_KERNEL);
2445 if (!dev->stat_req) {
2446 eth_req_free (gadget->ep0, dev->req);
2449 dev->stat_req->context = NULL;
2453 /* finish hookup to lower layer ... */
2454 dev->gadget = gadget;
2455 set_gadget_data (gadget, dev);
2456 gadget->ep0->driver_data = dev;
2458 /* two kinds of host-initiated state changes:
2459 * - iff DATA transfer is active, carrier is "on"
2460 * - tx queueing enabled if open *and* carrier is "on"
2462 netif_stop_queue (dev->net);
2463 netif_carrier_off (dev->net);
2465 SET_NETDEV_DEV (dev->net, &gadget->dev);
2466 status = register_netdev (dev->net);
2470 INFO (dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
2471 INFO (dev, "using %s, OUT %s IN %s%s%s\n", gadget->name,
2472 out_ep->name, in_ep->name,
2473 status_ep ? " STATUS " : "",
2474 status_ep ? status_ep->name : ""
2476 INFO (dev, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2477 net->dev_addr [0], net->dev_addr [1],
2478 net->dev_addr [2], net->dev_addr [3],
2479 net->dev_addr [4], net->dev_addr [5]);
2482 INFO (dev, "HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2483 dev->host_mac [0], dev->host_mac [1],
2484 dev->host_mac [2], dev->host_mac [3],
2485 dev->host_mac [4], dev->host_mac [5]);
2490 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2492 dev->rndis_config = rndis_register (rndis_control_ack);
2493 if (dev->rndis_config < 0) {
2495 unregister_netdev (dev->net);
2500 /* these set up a lot of the OIDs that RNDIS needs */
2501 rndis_set_host_mac (dev->rndis_config, dev->host_mac);
2502 if (rndis_set_param_dev (dev->rndis_config, dev->net,
2503 &dev->stats, &dev->cdc_filter))
2505 if (rndis_set_param_vendor (dev->rndis_config, vendorID,
2508 if (rndis_set_param_medium (dev->rndis_config,
2512 INFO (dev, "RNDIS ready\n");
2518 dev_dbg(&gadget->dev, "register_netdev failed, %d\n", status);
2520 eth_unbind (gadget);
2524 /*-------------------------------------------------------------------------*/
2527 eth_suspend (struct usb_gadget *gadget)
2529 struct eth_dev *dev = get_gadget_data (gadget);
2531 DEBUG (dev, "suspend\n");
2536 eth_resume (struct usb_gadget *gadget)
2538 struct eth_dev *dev = get_gadget_data (gadget);
2540 DEBUG (dev, "resume\n");
2544 /*-------------------------------------------------------------------------*/
2546 static struct usb_gadget_driver eth_driver = {
2549 .function = (char *) driver_desc,
2551 .unbind = __exit_p(eth_unbind),
2554 .disconnect = eth_disconnect,
2556 .suspend = eth_suspend,
2557 .resume = eth_resume,
2560 .name = (char *) shortname,
2561 .owner = THIS_MODULE,
2565 MODULE_DESCRIPTION (DRIVER_DESC);
2566 MODULE_AUTHOR ("David Brownell, Benedikt Spanger");
2567 MODULE_LICENSE ("GPL");
2570 static int __init init (void)
2572 return usb_gadget_register_driver (ð_driver);
2576 static void __exit cleanup (void)
2578 usb_gadget_unregister_driver (ð_driver);
2580 module_exit (cleanup);