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/sched.h>
31 #include <linux/slab.h>
32 #include <linux/smp_lock.h>
33 #include <linux/errno.h>
34 #include <linux/init.h>
35 #include <linux/timer.h>
36 #include <linux/list.h>
37 #include <linux/interrupt.h>
38 #include <linux/utsname.h>
39 #include <linux/device.h>
40 #include <linux/moduleparam.h>
41 #include <linux/ctype.h>
43 #include <asm/byteorder.h>
46 #include <asm/system.h>
47 #include <asm/uaccess.h>
48 #include <asm/unaligned.h>
50 #include <linux/usb_ch9.h>
51 #include <linux/usb/cdc.h>
52 #include <linux/usb_gadget.h>
54 #include <linux/random.h>
55 #include <linux/netdevice.h>
56 #include <linux/etherdevice.h>
57 #include <linux/ethtool.h>
59 #include "gadget_chips.h"
61 /*-------------------------------------------------------------------------*/
64 * Ethernet gadget driver -- with CDC and non-CDC options
65 * Builds on hardware support for a full duplex link.
67 * CDC Ethernet is the standard USB solution for sending Ethernet frames
68 * using USB. Real hardware tends to use the same framing protocol but look
69 * different for control features. This driver strongly prefers to use
70 * this USB-IF standard as its open-systems interoperability solution;
71 * most host side USB stacks (except from Microsoft) support it.
73 * There's some hardware that can't talk CDC. We make that hardware
74 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
75 * link-level setup only requires activating the configuration.
76 * Linux supports it, but other host operating systems may not.
77 * (This is a subset of CDC Ethernet.)
79 * A third option is also in use. Rather than CDC Ethernet, or something
80 * simpler, Microsoft pushes their own approach: RNDIS. The published
81 * RNDIS specs are ambiguous and appear to be incomplete, and are also
85 #define DRIVER_DESC "Ethernet Gadget"
86 #define DRIVER_VERSION "May Day 2005"
88 static const char shortname [] = "ether";
89 static const char driver_desc [] = DRIVER_DESC;
91 #define RX_EXTRA 20 /* guard against rx overflows */
95 #ifndef CONFIG_USB_ETH_RNDIS
96 #define rndis_uninit(x) do{}while(0)
97 #define rndis_deregister(c) do{}while(0)
98 #define rndis_exit() do{}while(0)
101 /* CDC and RNDIS support the same host-chosen outgoing packet filters. */
102 #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
103 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
104 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
105 |USB_CDC_PACKET_TYPE_DIRECTED)
108 /*-------------------------------------------------------------------------*/
112 struct usb_gadget *gadget;
113 struct usb_request *req; /* for control responses */
114 struct usb_request *stat_req; /* for cdc & rndis status */
117 struct usb_ep *in_ep, *out_ep, *status_ep;
118 const struct usb_endpoint_descriptor
122 struct list_head tx_reqs, rx_reqs;
124 struct net_device *net;
125 struct net_device_stats stats;
128 struct work_struct work;
132 unsigned suspended:1;
135 #define WORK_RX_MEMORY 0
137 u8 host_mac [ETH_ALEN];
140 /* This version autoconfigures as much as possible at run-time.
142 * It also ASSUMES a self-powered device, without remote wakeup,
143 * although remote wakeup support would make sense.
146 /*-------------------------------------------------------------------------*/
148 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
149 * Instead: allocate your own, using normal USB-IF procedures.
152 /* Thanks to NetChip Technologies for donating this product ID.
153 * It's for devices with only CDC Ethernet configurations.
155 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
156 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
158 /* For hardware that can't talk CDC, we use the same vendor ID that
159 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
160 * with pxa250. We're protocol-compatible, if the host-side drivers
161 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
162 * drivers that need to hard-wire endpoint numbers have a hook.
164 * The protocol is a minimal subset of CDC Ether, which works on any bulk
165 * hardware that's not deeply broken ... even on hardware that can't talk
166 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
167 * doesn't handle control-OUT).
169 #define SIMPLE_VENDOR_NUM 0x049f
170 #define SIMPLE_PRODUCT_NUM 0x505a
172 /* For hardware that can talk RNDIS and either of the above protocols,
173 * use this ID ... the windows INF files will know it. Unless it's
174 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
175 * the non-RNDIS configuration.
177 #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
178 #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
181 /* Some systems will want different product identifers published in the
182 * device descriptor, either numbers or strings or both. These string
183 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
186 static ushort idVendor;
187 module_param(idVendor, ushort, S_IRUGO);
188 MODULE_PARM_DESC(idVendor, "USB Vendor ID");
190 static ushort idProduct;
191 module_param(idProduct, ushort, S_IRUGO);
192 MODULE_PARM_DESC(idProduct, "USB Product ID");
194 static ushort bcdDevice;
195 module_param(bcdDevice, ushort, S_IRUGO);
196 MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
198 static char *iManufacturer;
199 module_param(iManufacturer, charp, S_IRUGO);
200 MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
202 static char *iProduct;
203 module_param(iProduct, charp, S_IRUGO);
204 MODULE_PARM_DESC(iProduct, "USB Product string");
206 static char *iSerialNumber;
207 module_param(iSerialNumber, charp, S_IRUGO);
208 MODULE_PARM_DESC(iSerialNumber, "SerialNumber");
210 /* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */
211 static char *dev_addr;
212 module_param(dev_addr, charp, S_IRUGO);
213 MODULE_PARM_DESC(dev_addr, "Device Ethernet Address");
215 /* this address is invisible to ifconfig */
216 static char *host_addr;
217 module_param(host_addr, charp, S_IRUGO);
218 MODULE_PARM_DESC(host_addr, "Host Ethernet Address");
221 /*-------------------------------------------------------------------------*/
223 /* Include CDC support if we could run on CDC-capable hardware. */
225 #ifdef CONFIG_USB_GADGET_NET2280
226 #define DEV_CONFIG_CDC
229 #ifdef CONFIG_USB_GADGET_DUMMY_HCD
230 #define DEV_CONFIG_CDC
233 #ifdef CONFIG_USB_GADGET_GOKU
234 #define DEV_CONFIG_CDC
237 #ifdef CONFIG_USB_GADGET_LH7A40X
238 #define DEV_CONFIG_CDC
241 #ifdef CONFIG_USB_GADGET_MQ11XX
242 #define DEV_CONFIG_CDC
245 #ifdef CONFIG_USB_GADGET_OMAP
246 #define DEV_CONFIG_CDC
249 #ifdef CONFIG_USB_GADGET_N9604
250 #define DEV_CONFIG_CDC
253 #ifdef CONFIG_USB_GADGET_PXA27X
254 #define DEV_CONFIG_CDC
257 #ifdef CONFIG_USB_GADGET_AT91
258 #define DEV_CONFIG_CDC
261 #ifdef CONFIG_USB_GADGET_MUSBHSFC
262 #define DEV_CONFIG_CDC
265 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
266 #define DEV_CONFIG_CDC
270 /* For CDC-incapable hardware, choose the simple cdc subset.
271 * Anything that talks bulk (without notable bugs) can do this.
273 #ifdef CONFIG_USB_GADGET_PXA2XX
274 #define DEV_CONFIG_SUBSET
277 #ifdef CONFIG_USB_GADGET_SH
278 #define DEV_CONFIG_SUBSET
281 #ifdef CONFIG_USB_GADGET_SA1100
282 /* use non-CDC for backwards compatibility */
283 #define DEV_CONFIG_SUBSET
286 #ifdef CONFIG_USB_GADGET_S3C2410
287 #define DEV_CONFIG_CDC
290 /*-------------------------------------------------------------------------*/
292 /* "main" config is either CDC, or its simple subset */
293 static inline int is_cdc(struct eth_dev *dev)
295 #if !defined(DEV_CONFIG_SUBSET)
296 return 1; /* only cdc possible */
297 #elif !defined (DEV_CONFIG_CDC)
298 return 0; /* only subset possible */
300 return dev->cdc; /* depends on what hardware we found */
304 /* "secondary" RNDIS config may sometimes be activated */
305 static inline int rndis_active(struct eth_dev *dev)
307 #ifdef CONFIG_USB_ETH_RNDIS
314 #define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
315 #define cdc_active(dev) ( is_cdc(dev) && !rndis_active(dev))
319 #define DEFAULT_QLEN 2 /* double buffering by default */
321 /* peak bulk transfer bits-per-second */
322 #define HS_BPS (13 * 512 * 8 * 1000 * 8)
323 #define FS_BPS (19 * 64 * 1 * 1000 * 8)
325 #ifdef CONFIG_USB_GADGET_DUALSPEED
326 #define DEVSPEED USB_SPEED_HIGH
328 static unsigned qmult = 5;
329 module_param (qmult, uint, S_IRUGO|S_IWUSR);
332 /* for dual-speed hardware, use deeper queues at highspeed */
333 #define qlen(gadget) \
334 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
336 /* also defer IRQs on highspeed TX */
337 #define TX_DELAY qmult
339 static inline int BITRATE(struct usb_gadget *g)
341 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
344 #else /* full speed (low speed doesn't do bulk) */
345 #define DEVSPEED USB_SPEED_FULL
347 #define qlen(gadget) DEFAULT_QLEN
349 static inline int BITRATE(struct usb_gadget *g)
356 /*-------------------------------------------------------------------------*/
358 #define xprintk(d,level,fmt,args...) \
359 printk(level "%s: " fmt , (d)->net->name , ## args)
363 #define DEBUG(dev,fmt,args...) \
364 xprintk(dev , KERN_DEBUG , fmt , ## args)
366 #define DEBUG(dev,fmt,args...) \
373 #define VDEBUG(dev,fmt,args...) \
377 #define ERROR(dev,fmt,args...) \
378 xprintk(dev , KERN_ERR , fmt , ## args)
379 #define WARN(dev,fmt,args...) \
380 xprintk(dev , KERN_WARNING , fmt , ## args)
381 #define INFO(dev,fmt,args...) \
382 xprintk(dev , KERN_INFO , fmt , ## args)
384 /*-------------------------------------------------------------------------*/
386 /* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
387 * ep0 implementation: descriptors, config management, setup().
388 * also optional class-specific notification interrupt transfer.
392 * DESCRIPTORS ... most are static, but strings and (full) configuration
393 * descriptors are built on demand. For now we do either full CDC, or
394 * our simple subset, with RNDIS as an optional second configuration.
396 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
397 * the class descriptors match a modem (they're ignored; it's really just
398 * Ethernet functionality), they don't need the NOP altsetting, and the
399 * status transfer endpoint isn't optional.
402 #define STRING_MANUFACTURER 1
403 #define STRING_PRODUCT 2
404 #define STRING_ETHADDR 3
405 #define STRING_DATA 4
406 #define STRING_CONTROL 5
407 #define STRING_RNDIS_CONTROL 6
409 #define STRING_SUBSET 8
410 #define STRING_RNDIS 9
411 #define STRING_SERIALNUMBER 10
413 /* holds our biggest descriptor (or RNDIS response) */
414 #define USB_BUFSIZ 256
417 * This device advertises one configuration, eth_config, unless RNDIS
418 * is enabled (rndis_config) on hardware supporting at least two configs.
420 * NOTE: Controllers like superh_udc should probably be able to use
421 * an RNDIS-only configuration.
423 * FIXME define some higher-powered configurations to make it easier
424 * to recharge batteries ...
427 #define DEV_CONFIG_VALUE 1 /* cdc or subset */
428 #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
430 static struct usb_device_descriptor
432 .bLength = sizeof device_desc,
433 .bDescriptorType = USB_DT_DEVICE,
435 .bcdUSB = __constant_cpu_to_le16 (0x0200),
437 .bDeviceClass = USB_CLASS_COMM,
438 .bDeviceSubClass = 0,
439 .bDeviceProtocol = 0,
441 .idVendor = __constant_cpu_to_le16 (CDC_VENDOR_NUM),
442 .idProduct = __constant_cpu_to_le16 (CDC_PRODUCT_NUM),
443 .iManufacturer = STRING_MANUFACTURER,
444 .iProduct = STRING_PRODUCT,
445 .bNumConfigurations = 1,
448 static struct usb_otg_descriptor
450 .bLength = sizeof otg_descriptor,
451 .bDescriptorType = USB_DT_OTG,
453 .bmAttributes = USB_OTG_SRP,
456 static struct usb_config_descriptor
458 .bLength = sizeof eth_config,
459 .bDescriptorType = USB_DT_CONFIG,
461 /* compute wTotalLength on the fly */
463 .bConfigurationValue = DEV_CONFIG_VALUE,
464 .iConfiguration = STRING_CDC,
465 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
469 #ifdef CONFIG_USB_ETH_RNDIS
470 static struct usb_config_descriptor
472 .bLength = sizeof rndis_config,
473 .bDescriptorType = USB_DT_CONFIG,
475 /* compute wTotalLength on the fly */
477 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
478 .iConfiguration = STRING_RNDIS,
479 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
485 * Compared to the simple CDC subset, the full CDC Ethernet model adds
486 * three class descriptors, two interface descriptors, optional status
487 * endpoint. Both have a "data" interface and two bulk endpoints.
488 * There are also differences in how control requests are handled.
490 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of
491 * the CDC-ACM (modem) spec.
494 #ifdef DEV_CONFIG_CDC
495 static struct usb_interface_descriptor
497 .bLength = sizeof control_intf,
498 .bDescriptorType = USB_DT_INTERFACE,
500 .bInterfaceNumber = 0,
501 /* status endpoint is optional; this may be patched later */
503 .bInterfaceClass = USB_CLASS_COMM,
504 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
505 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
506 .iInterface = STRING_CONTROL,
510 #ifdef CONFIG_USB_ETH_RNDIS
511 static const struct usb_interface_descriptor
512 rndis_control_intf = {
513 .bLength = sizeof rndis_control_intf,
514 .bDescriptorType = USB_DT_INTERFACE,
516 .bInterfaceNumber = 0,
518 .bInterfaceClass = USB_CLASS_COMM,
519 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
520 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
521 .iInterface = STRING_RNDIS_CONTROL,
525 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
527 static const struct usb_cdc_header_desc header_desc = {
528 .bLength = sizeof header_desc,
529 .bDescriptorType = USB_DT_CS_INTERFACE,
530 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
532 .bcdCDC = __constant_cpu_to_le16 (0x0110),
535 static const struct usb_cdc_union_desc union_desc = {
536 .bLength = sizeof union_desc,
537 .bDescriptorType = USB_DT_CS_INTERFACE,
538 .bDescriptorSubType = USB_CDC_UNION_TYPE,
540 .bMasterInterface0 = 0, /* index of control interface */
541 .bSlaveInterface0 = 1, /* index of DATA interface */
544 #endif /* CDC || RNDIS */
546 #ifdef CONFIG_USB_ETH_RNDIS
548 static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
549 .bLength = sizeof call_mgmt_descriptor,
550 .bDescriptorType = USB_DT_CS_INTERFACE,
551 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
553 .bmCapabilities = 0x00,
554 .bDataInterface = 0x01,
557 static const struct usb_cdc_acm_descriptor acm_descriptor = {
558 .bLength = sizeof acm_descriptor,
559 .bDescriptorType = USB_DT_CS_INTERFACE,
560 .bDescriptorSubType = USB_CDC_ACM_TYPE,
562 .bmCapabilities = 0x00,
567 #ifdef DEV_CONFIG_CDC
569 static const struct usb_cdc_ether_desc ether_desc = {
570 .bLength = sizeof ether_desc,
571 .bDescriptorType = USB_DT_CS_INTERFACE,
572 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
574 /* this descriptor actually adds value, surprise! */
575 .iMACAddress = STRING_ETHADDR,
576 .bmEthernetStatistics = __constant_cpu_to_le32 (0), /* no statistics */
577 .wMaxSegmentSize = __constant_cpu_to_le16 (ETH_FRAME_LEN),
578 .wNumberMCFilters = __constant_cpu_to_le16 (0),
579 .bNumberPowerFilters = 0,
584 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
586 /* include the status endpoint if we can, even where it's optional.
587 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
588 * packet, to simplify cancellation; and a big transfer interval, to
589 * waste less bandwidth.
591 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
592 * if they ignore the connect/disconnect notifications that real aether
593 * can provide. more advanced cdc configurations might want to support
594 * encapsulated commands (vendor-specific, using control-OUT).
596 * RNDIS requires the status endpoint, since it uses that encapsulation
597 * mechanism for its funky RPC scheme.
600 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
601 #define STATUS_BYTECOUNT 16 /* 8 byte header + data */
603 static struct usb_endpoint_descriptor
605 .bLength = USB_DT_ENDPOINT_SIZE,
606 .bDescriptorType = USB_DT_ENDPOINT,
608 .bEndpointAddress = USB_DIR_IN,
609 .bmAttributes = USB_ENDPOINT_XFER_INT,
610 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
611 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
615 #ifdef DEV_CONFIG_CDC
617 /* the default data interface has no endpoints ... */
619 static const struct usb_interface_descriptor
621 .bLength = sizeof data_nop_intf,
622 .bDescriptorType = USB_DT_INTERFACE,
624 .bInterfaceNumber = 1,
625 .bAlternateSetting = 0,
627 .bInterfaceClass = USB_CLASS_CDC_DATA,
628 .bInterfaceSubClass = 0,
629 .bInterfaceProtocol = 0,
632 /* ... but the "real" data interface has two bulk endpoints */
634 static const struct usb_interface_descriptor
636 .bLength = sizeof data_intf,
637 .bDescriptorType = USB_DT_INTERFACE,
639 .bInterfaceNumber = 1,
640 .bAlternateSetting = 1,
642 .bInterfaceClass = USB_CLASS_CDC_DATA,
643 .bInterfaceSubClass = 0,
644 .bInterfaceProtocol = 0,
645 .iInterface = STRING_DATA,
650 #ifdef CONFIG_USB_ETH_RNDIS
652 /* RNDIS doesn't activate by changing to the "real" altsetting */
654 static const struct usb_interface_descriptor
656 .bLength = sizeof rndis_data_intf,
657 .bDescriptorType = USB_DT_INTERFACE,
659 .bInterfaceNumber = 1,
660 .bAlternateSetting = 0,
662 .bInterfaceClass = USB_CLASS_CDC_DATA,
663 .bInterfaceSubClass = 0,
664 .bInterfaceProtocol = 0,
665 .iInterface = STRING_DATA,
670 #ifdef DEV_CONFIG_SUBSET
673 * "Simple" CDC-subset option is a simple vendor-neutral model that most
674 * full speed controllers can handle: one interface, two bulk endpoints.
677 static const struct usb_interface_descriptor
679 .bLength = sizeof subset_data_intf,
680 .bDescriptorType = USB_DT_INTERFACE,
682 .bInterfaceNumber = 0,
683 .bAlternateSetting = 0,
685 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
686 .bInterfaceSubClass = 0,
687 .bInterfaceProtocol = 0,
688 .iInterface = STRING_DATA,
694 static struct usb_endpoint_descriptor
696 .bLength = USB_DT_ENDPOINT_SIZE,
697 .bDescriptorType = USB_DT_ENDPOINT,
699 .bEndpointAddress = USB_DIR_IN,
700 .bmAttributes = USB_ENDPOINT_XFER_BULK,
703 static struct usb_endpoint_descriptor
705 .bLength = USB_DT_ENDPOINT_SIZE,
706 .bDescriptorType = USB_DT_ENDPOINT,
708 .bEndpointAddress = USB_DIR_OUT,
709 .bmAttributes = USB_ENDPOINT_XFER_BULK,
712 static const struct usb_descriptor_header *fs_eth_function [11] = {
713 (struct usb_descriptor_header *) &otg_descriptor,
714 #ifdef DEV_CONFIG_CDC
715 /* "cdc" mode descriptors */
716 (struct usb_descriptor_header *) &control_intf,
717 (struct usb_descriptor_header *) &header_desc,
718 (struct usb_descriptor_header *) &union_desc,
719 (struct usb_descriptor_header *) ðer_desc,
720 /* NOTE: status endpoint may need to be removed */
721 (struct usb_descriptor_header *) &fs_status_desc,
722 /* data interface, with altsetting */
723 (struct usb_descriptor_header *) &data_nop_intf,
724 (struct usb_descriptor_header *) &data_intf,
725 (struct usb_descriptor_header *) &fs_source_desc,
726 (struct usb_descriptor_header *) &fs_sink_desc,
728 #endif /* DEV_CONFIG_CDC */
731 static inline void __init fs_subset_descriptors(void)
733 #ifdef DEV_CONFIG_SUBSET
734 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
735 fs_eth_function[2] = (struct usb_descriptor_header *) &fs_source_desc;
736 fs_eth_function[3] = (struct usb_descriptor_header *) &fs_sink_desc;
737 fs_eth_function[4] = NULL;
739 fs_eth_function[1] = NULL;
743 #ifdef CONFIG_USB_ETH_RNDIS
744 static const struct usb_descriptor_header *fs_rndis_function [] = {
745 (struct usb_descriptor_header *) &otg_descriptor,
746 /* control interface matches ACM, not Ethernet */
747 (struct usb_descriptor_header *) &rndis_control_intf,
748 (struct usb_descriptor_header *) &header_desc,
749 (struct usb_descriptor_header *) &call_mgmt_descriptor,
750 (struct usb_descriptor_header *) &acm_descriptor,
751 (struct usb_descriptor_header *) &union_desc,
752 (struct usb_descriptor_header *) &fs_status_desc,
753 /* data interface has no altsetting */
754 (struct usb_descriptor_header *) &rndis_data_intf,
755 (struct usb_descriptor_header *) &fs_source_desc,
756 (struct usb_descriptor_header *) &fs_sink_desc,
761 #ifdef CONFIG_USB_GADGET_DUALSPEED
764 * usb 2.0 devices need to expose both high speed and full speed
765 * descriptors, unless they only run at full speed.
768 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
769 static struct usb_endpoint_descriptor
771 .bLength = USB_DT_ENDPOINT_SIZE,
772 .bDescriptorType = USB_DT_ENDPOINT,
774 .bmAttributes = USB_ENDPOINT_XFER_INT,
775 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
776 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
778 #endif /* DEV_CONFIG_CDC */
780 static struct usb_endpoint_descriptor
782 .bLength = USB_DT_ENDPOINT_SIZE,
783 .bDescriptorType = USB_DT_ENDPOINT,
785 .bmAttributes = USB_ENDPOINT_XFER_BULK,
786 .wMaxPacketSize = __constant_cpu_to_le16 (512),
789 static struct usb_endpoint_descriptor
791 .bLength = USB_DT_ENDPOINT_SIZE,
792 .bDescriptorType = USB_DT_ENDPOINT,
794 .bmAttributes = USB_ENDPOINT_XFER_BULK,
795 .wMaxPacketSize = __constant_cpu_to_le16 (512),
798 static struct usb_qualifier_descriptor
800 .bLength = sizeof dev_qualifier,
801 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
803 .bcdUSB = __constant_cpu_to_le16 (0x0200),
804 .bDeviceClass = USB_CLASS_COMM,
806 .bNumConfigurations = 1,
809 static const struct usb_descriptor_header *hs_eth_function [11] = {
810 (struct usb_descriptor_header *) &otg_descriptor,
811 #ifdef DEV_CONFIG_CDC
812 /* "cdc" mode descriptors */
813 (struct usb_descriptor_header *) &control_intf,
814 (struct usb_descriptor_header *) &header_desc,
815 (struct usb_descriptor_header *) &union_desc,
816 (struct usb_descriptor_header *) ðer_desc,
817 /* NOTE: status endpoint may need to be removed */
818 (struct usb_descriptor_header *) &hs_status_desc,
819 /* data interface, with altsetting */
820 (struct usb_descriptor_header *) &data_nop_intf,
821 (struct usb_descriptor_header *) &data_intf,
822 (struct usb_descriptor_header *) &hs_source_desc,
823 (struct usb_descriptor_header *) &hs_sink_desc,
825 #endif /* DEV_CONFIG_CDC */
828 static inline void __init hs_subset_descriptors(void)
830 #ifdef DEV_CONFIG_SUBSET
831 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
832 hs_eth_function[2] = (struct usb_descriptor_header *) &fs_source_desc;
833 hs_eth_function[3] = (struct usb_descriptor_header *) &fs_sink_desc;
834 hs_eth_function[4] = NULL;
836 hs_eth_function[1] = NULL;
840 #ifdef CONFIG_USB_ETH_RNDIS
841 static const struct usb_descriptor_header *hs_rndis_function [] = {
842 (struct usb_descriptor_header *) &otg_descriptor,
843 /* control interface matches ACM, not Ethernet */
844 (struct usb_descriptor_header *) &rndis_control_intf,
845 (struct usb_descriptor_header *) &header_desc,
846 (struct usb_descriptor_header *) &call_mgmt_descriptor,
847 (struct usb_descriptor_header *) &acm_descriptor,
848 (struct usb_descriptor_header *) &union_desc,
849 (struct usb_descriptor_header *) &hs_status_desc,
850 /* data interface has no altsetting */
851 (struct usb_descriptor_header *) &rndis_data_intf,
852 (struct usb_descriptor_header *) &hs_source_desc,
853 (struct usb_descriptor_header *) &hs_sink_desc,
859 /* maxpacket and other transfer characteristics vary by speed. */
860 #define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
864 /* if there's no high speed support, maxpacket doesn't change. */
865 #define ep_desc(g,hs,fs) (((void)(g)), (fs))
867 static inline void __init hs_subset_descriptors(void)
871 #endif /* !CONFIG_USB_GADGET_DUALSPEED */
873 /*-------------------------------------------------------------------------*/
875 /* descriptors that are built on-demand */
877 static char manufacturer [50];
878 static char product_desc [40] = DRIVER_DESC;
879 static char serial_number [20];
881 #ifdef DEV_CONFIG_CDC
882 /* address that the host will use ... usually assigned at random */
883 static char ethaddr [2 * ETH_ALEN + 1];
886 /* static strings, in UTF-8 */
887 static struct usb_string strings [] = {
888 { STRING_MANUFACTURER, manufacturer, },
889 { STRING_PRODUCT, product_desc, },
890 { STRING_SERIALNUMBER, serial_number, },
891 { STRING_DATA, "Ethernet Data", },
892 #ifdef DEV_CONFIG_CDC
893 { STRING_CDC, "CDC Ethernet", },
894 { STRING_ETHADDR, ethaddr, },
895 { STRING_CONTROL, "CDC Communications Control", },
897 #ifdef DEV_CONFIG_SUBSET
898 { STRING_SUBSET, "CDC Ethernet Subset", },
900 #ifdef CONFIG_USB_ETH_RNDIS
901 { STRING_RNDIS, "RNDIS", },
902 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
904 { } /* end of list */
907 static struct usb_gadget_strings stringtab = {
908 .language = 0x0409, /* en-us */
913 * one config, two interfaces: control, data.
914 * complications: class descriptors, and an altsetting.
917 config_buf (enum usb_device_speed speed,
919 unsigned index, int is_otg)
922 const struct usb_config_descriptor *config;
923 const struct usb_descriptor_header **function;
924 #ifdef CONFIG_USB_GADGET_DUALSPEED
925 int hs = (speed == USB_SPEED_HIGH);
927 if (type == USB_DT_OTHER_SPEED_CONFIG)
929 #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
931 #define which_fn(t) (fs_ ## t ## _function)
934 if (index >= device_desc.bNumConfigurations)
937 #ifdef CONFIG_USB_ETH_RNDIS
938 /* list the RNDIS config first, to make Microsoft's drivers
939 * happy. DOCSIS 1.0 needs this too.
941 if (device_desc.bNumConfigurations == 2 && index == 0) {
942 config = &rndis_config;
943 function = which_fn (rndis);
947 config = ð_config;
948 function = which_fn (eth);
951 /* for now, don't advertise srp-only devices */
955 len = usb_gadget_config_buf (config, buf, USB_BUFSIZ, function);
958 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
962 /*-------------------------------------------------------------------------*/
964 static void eth_start (struct eth_dev *dev, gfp_t gfp_flags);
965 static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
968 set_ether_config (struct eth_dev *dev, gfp_t gfp_flags)
971 struct usb_gadget *gadget = dev->gadget;
973 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
974 /* status endpoint used for RNDIS and (optionally) CDC */
975 if (!subset_active(dev) && dev->status_ep) {
976 dev->status = ep_desc (gadget, &hs_status_desc,
978 dev->status_ep->driver_data = dev;
980 result = usb_ep_enable (dev->status_ep, dev->status);
982 DEBUG (dev, "enable %s --> %d\n",
983 dev->status_ep->name, result);
989 dev->in = ep_desc (dev->gadget, &hs_source_desc, &fs_source_desc);
990 dev->in_ep->driver_data = dev;
992 dev->out = ep_desc (dev->gadget, &hs_sink_desc, &fs_sink_desc);
993 dev->out_ep->driver_data = dev;
995 /* With CDC, the host isn't allowed to use these two data
996 * endpoints in the default altsetting for the interface.
997 * so we don't activate them yet. Reset from SET_INTERFACE.
999 * Strictly speaking RNDIS should work the same: activation is
1000 * a side effect of setting a packet filter. Deactivation is
1001 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
1003 if (!cdc_active(dev)) {
1004 result = usb_ep_enable (dev->in_ep, dev->in);
1006 DEBUG(dev, "enable %s --> %d\n",
1007 dev->in_ep->name, result);
1011 result = usb_ep_enable (dev->out_ep, dev->out);
1013 DEBUG (dev, "enable %s --> %d\n",
1014 dev->out_ep->name, result);
1021 result = alloc_requests (dev, qlen (gadget), gfp_flags);
1023 /* on error, disable any endpoints */
1025 if (!subset_active(dev))
1026 (void) usb_ep_disable (dev->status_ep);
1028 (void) usb_ep_disable (dev->in_ep);
1029 (void) usb_ep_disable (dev->out_ep);
1034 /* activate non-CDC configs right away
1035 * this isn't strictly according to the RNDIS spec
1037 if (!cdc_active (dev)) {
1038 netif_carrier_on (dev->net);
1039 if (netif_running (dev->net)) {
1040 spin_unlock (&dev->lock);
1041 eth_start (dev, GFP_ATOMIC);
1042 spin_lock (&dev->lock);
1047 DEBUG (dev, "qlen %d\n", qlen (gadget));
1049 /* caller is responsible for cleanup on error */
1053 static void eth_reset_config (struct eth_dev *dev)
1055 struct usb_request *req;
1057 if (dev->config == 0)
1060 DEBUG (dev, "%s\n", __FUNCTION__);
1062 netif_stop_queue (dev->net);
1063 netif_carrier_off (dev->net);
1064 rndis_uninit(dev->rndis_config);
1066 /* disable endpoints, forcing (synchronous) completion of
1067 * pending i/o. then free the requests.
1070 usb_ep_disable (dev->in_ep);
1071 spin_lock(&dev->req_lock);
1072 while (likely (!list_empty (&dev->tx_reqs))) {
1073 req = container_of (dev->tx_reqs.next,
1074 struct usb_request, list);
1075 list_del (&req->list);
1077 spin_unlock(&dev->req_lock);
1078 usb_ep_free_request (dev->in_ep, req);
1079 spin_lock(&dev->req_lock);
1081 spin_unlock(&dev->req_lock);
1084 usb_ep_disable (dev->out_ep);
1085 spin_lock(&dev->req_lock);
1086 while (likely (!list_empty (&dev->rx_reqs))) {
1087 req = container_of (dev->rx_reqs.next,
1088 struct usb_request, list);
1089 list_del (&req->list);
1091 spin_unlock(&dev->req_lock);
1092 usb_ep_free_request (dev->out_ep, req);
1093 spin_lock(&dev->req_lock);
1095 spin_unlock(&dev->req_lock);
1099 usb_ep_disable (dev->status_ep);
1102 dev->cdc_filter = 0;
1106 /* change our operational config. must agree with the code
1107 * that returns config descriptors, and altsetting code.
1110 eth_set_config (struct eth_dev *dev, unsigned number, gfp_t gfp_flags)
1113 struct usb_gadget *gadget = dev->gadget;
1115 if (gadget_is_sa1100 (gadget)
1117 && atomic_read (&dev->tx_qlen) != 0) {
1118 /* tx fifo is full, but we can't clear it...*/
1119 INFO (dev, "can't change configurations\n");
1122 eth_reset_config (dev);
1125 case DEV_CONFIG_VALUE:
1126 result = set_ether_config (dev, gfp_flags);
1128 #ifdef CONFIG_USB_ETH_RNDIS
1129 case DEV_RNDIS_CONFIG_VALUE:
1131 result = set_ether_config (dev, gfp_flags);
1143 eth_reset_config (dev);
1144 usb_gadget_vbus_draw(dev->gadget,
1145 dev->gadget->is_otg ? 8 : 100);
1150 power = 2 * eth_config.bMaxPower;
1151 usb_gadget_vbus_draw(dev->gadget, power);
1153 switch (gadget->speed) {
1154 case USB_SPEED_FULL: speed = "full"; break;
1155 #ifdef CONFIG_USB_GADGET_DUALSPEED
1156 case USB_SPEED_HIGH: speed = "high"; break;
1158 default: speed = "?"; break;
1161 dev->config = number;
1162 INFO (dev, "%s speed config #%d: %d mA, %s, using %s\n",
1163 speed, number, power, driver_desc,
1168 : "CDC Ethernet Subset"));
1173 /*-------------------------------------------------------------------------*/
1175 #ifdef DEV_CONFIG_CDC
1177 /* The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
1178 * only to notify the host about link status changes (which we support) or
1179 * report completion of some encapsulated command (as used in RNDIS). Since
1180 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1181 * command mechanism; and only one status request is ever queued.
1184 static void eth_status_complete (struct usb_ep *ep, struct usb_request *req)
1186 struct usb_cdc_notification *event = req->buf;
1187 int value = req->status;
1188 struct eth_dev *dev = ep->driver_data;
1190 /* issue the second notification if host reads the first */
1191 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1193 __le32 *data = req->buf + sizeof *event;
1195 event->bmRequestType = 0xA1;
1196 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
1197 event->wValue = __constant_cpu_to_le16 (0);
1198 event->wIndex = __constant_cpu_to_le16 (1);
1199 event->wLength = __constant_cpu_to_le16 (8);
1201 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1202 data [0] = data [1] = cpu_to_le32 (BITRATE (dev->gadget));
1204 req->length = STATUS_BYTECOUNT;
1205 value = usb_ep_queue (ep, req, GFP_ATOMIC);
1206 DEBUG (dev, "send SPEED_CHANGE --> %d\n", value);
1209 } else if (value != -ECONNRESET)
1210 DEBUG (dev, "event %02x --> %d\n",
1211 event->bNotificationType, value);
1212 req->context = NULL;
1215 static void issue_start_status (struct eth_dev *dev)
1217 struct usb_request *req = dev->stat_req;
1218 struct usb_cdc_notification *event;
1221 DEBUG (dev, "%s, flush old status first\n", __FUNCTION__);
1225 * FIXME ugly idiom, maybe we'd be better with just
1226 * a "cancel the whole queue" primitive since any
1227 * unlink-one primitive has way too many error modes.
1228 * here, we "know" toggle is already clear...
1230 * FIXME iff req->context != null just dequeue it
1232 usb_ep_disable (dev->status_ep);
1233 usb_ep_enable (dev->status_ep, dev->status);
1235 /* 3.8.1 says to issue first NETWORK_CONNECTION, then
1236 * a SPEED_CHANGE. could be useful in some configs.
1239 event->bmRequestType = 0xA1;
1240 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
1241 event->wValue = __constant_cpu_to_le16 (1); /* connected */
1242 event->wIndex = __constant_cpu_to_le16 (1);
1245 req->length = sizeof *event;
1246 req->complete = eth_status_complete;
1249 value = usb_ep_queue (dev->status_ep, req, GFP_ATOMIC);
1251 DEBUG (dev, "status buf queue --> %d\n", value);
1256 /*-------------------------------------------------------------------------*/
1258 static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req)
1260 if (req->status || req->actual != req->length)
1261 DEBUG ((struct eth_dev *) ep->driver_data,
1262 "setup complete --> %d, %d/%d\n",
1263 req->status, req->actual, req->length);
1266 #ifdef CONFIG_USB_ETH_RNDIS
1268 static void rndis_response_complete (struct usb_ep *ep, struct usb_request *req)
1270 if (req->status || req->actual != req->length)
1271 DEBUG ((struct eth_dev *) ep->driver_data,
1272 "rndis response complete --> %d, %d/%d\n",
1273 req->status, req->actual, req->length);
1275 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1278 static void rndis_command_complete (struct usb_ep *ep, struct usb_request *req)
1280 struct eth_dev *dev = ep->driver_data;
1283 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1284 spin_lock(&dev->lock);
1285 status = rndis_msg_parser (dev->rndis_config, (u8 *) req->buf);
1287 ERROR(dev, "%s: rndis parse error %d\n", __FUNCTION__, status);
1288 spin_unlock(&dev->lock);
1294 * The setup() callback implements all the ep0 functionality that's not
1295 * handled lower down. CDC has a number of less-common features:
1297 * - two interfaces: control, and ethernet data
1298 * - Ethernet data interface has two altsettings: default, and active
1299 * - class-specific descriptors for the control interface
1300 * - class-specific control requests
1303 eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1305 struct eth_dev *dev = get_gadget_data (gadget);
1306 struct usb_request *req = dev->req;
1307 int value = -EOPNOTSUPP;
1308 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1309 u16 wValue = le16_to_cpu(ctrl->wValue);
1310 u16 wLength = le16_to_cpu(ctrl->wLength);
1312 /* descriptors just go into the pre-allocated ep0 buffer,
1313 * while config change events may enable network traffic.
1315 req->complete = eth_setup_complete;
1316 switch (ctrl->bRequest) {
1318 case USB_REQ_GET_DESCRIPTOR:
1319 if (ctrl->bRequestType != USB_DIR_IN)
1321 switch (wValue >> 8) {
1324 value = min (wLength, (u16) sizeof device_desc);
1325 memcpy (req->buf, &device_desc, value);
1327 #ifdef CONFIG_USB_GADGET_DUALSPEED
1328 case USB_DT_DEVICE_QUALIFIER:
1329 if (!gadget->is_dualspeed)
1331 value = min (wLength, (u16) sizeof dev_qualifier);
1332 memcpy (req->buf, &dev_qualifier, value);
1335 case USB_DT_OTHER_SPEED_CONFIG:
1336 if (!gadget->is_dualspeed)
1339 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1341 value = config_buf (gadget->speed, req->buf,
1346 value = min (wLength, (u16) value);
1350 value = usb_gadget_get_string (&stringtab,
1351 wValue & 0xff, req->buf);
1353 value = min (wLength, (u16) value);
1358 case USB_REQ_SET_CONFIGURATION:
1359 if (ctrl->bRequestType != 0)
1361 if (gadget->a_hnp_support)
1362 DEBUG (dev, "HNP available\n");
1363 else if (gadget->a_alt_hnp_support)
1364 DEBUG (dev, "HNP needs a different root port\n");
1365 spin_lock (&dev->lock);
1366 value = eth_set_config (dev, wValue, GFP_ATOMIC);
1367 spin_unlock (&dev->lock);
1369 case USB_REQ_GET_CONFIGURATION:
1370 if (ctrl->bRequestType != USB_DIR_IN)
1372 *(u8 *)req->buf = dev->config;
1373 value = min (wLength, (u16) 1);
1376 case USB_REQ_SET_INTERFACE:
1377 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1381 if (!cdc_active(dev) && wIndex != 0)
1383 spin_lock (&dev->lock);
1385 /* PXA hardware partially handles SET_INTERFACE;
1386 * we need to kluge around that interference.
1388 if (gadget_is_pxa (gadget)) {
1389 value = eth_set_config (dev, DEV_CONFIG_VALUE,
1394 #ifdef DEV_CONFIG_CDC
1396 case 0: /* control/master intf */
1400 usb_ep_disable (dev->status_ep);
1401 usb_ep_enable (dev->status_ep, dev->status);
1405 case 1: /* data intf */
1408 usb_ep_disable (dev->in_ep);
1409 usb_ep_disable (dev->out_ep);
1411 /* CDC requires the data transfers not be done from
1412 * the default interface setting ... also, setting
1413 * the non-default interface resets filters etc.
1416 if (!cdc_active (dev))
1418 usb_ep_enable (dev->in_ep, dev->in);
1419 usb_ep_enable (dev->out_ep, dev->out);
1420 dev->cdc_filter = DEFAULT_FILTER;
1421 netif_carrier_on (dev->net);
1423 issue_start_status (dev);
1424 if (netif_running (dev->net)) {
1425 spin_unlock (&dev->lock);
1426 eth_start (dev, GFP_ATOMIC);
1427 spin_lock (&dev->lock);
1430 netif_stop_queue (dev->net);
1431 netif_carrier_off (dev->net);
1437 /* FIXME this is wrong, as is the assumption that
1438 * all non-PXA hardware talks real CDC ...
1440 dev_warn (&gadget->dev, "set_interface ignored!\n");
1441 #endif /* DEV_CONFIG_CDC */
1444 spin_unlock (&dev->lock);
1446 case USB_REQ_GET_INTERFACE:
1447 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1451 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
1454 /* for CDC, iff carrier is on, data interface is active. */
1455 if (rndis_active(dev) || wIndex != 1)
1456 *(u8 *)req->buf = 0;
1458 *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0;
1459 value = min (wLength, (u16) 1);
1462 #ifdef DEV_CONFIG_CDC
1463 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
1464 /* see 6.2.30: no data, wIndex = interface,
1465 * wValue = packet filter bitmap
1467 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1472 DEBUG (dev, "packet filter %02x\n", wValue);
1473 dev->cdc_filter = wValue;
1478 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1479 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1480 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1481 * case USB_CDC_GET_ETHERNET_STATISTIC:
1484 #endif /* DEV_CONFIG_CDC */
1486 #ifdef CONFIG_USB_ETH_RNDIS
1487 /* RNDIS uses the CDC command encapsulation mechanism to implement
1488 * an RPC scheme, with much getting/setting of attributes by OID.
1490 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1491 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1492 || !rndis_active(dev)
1493 || wLength > USB_BUFSIZ
1495 || rndis_control_intf.bInterfaceNumber
1498 /* read the request, then process it */
1500 req->complete = rndis_command_complete;
1501 /* later, rndis_control_ack () sends a notification */
1504 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1505 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1506 == ctrl->bRequestType
1507 && rndis_active(dev)
1508 // && wLength >= 0x0400
1510 && rndis_control_intf.bInterfaceNumber
1514 /* return the result */
1515 buf = rndis_get_next_response (dev->rndis_config,
1518 memcpy (req->buf, buf, value);
1519 req->complete = rndis_response_complete;
1520 rndis_free_response(dev->rndis_config, buf);
1522 /* else stalls ... spec says to avoid that */
1529 "unknown control req%02x.%02x v%04x i%04x l%d\n",
1530 ctrl->bRequestType, ctrl->bRequest,
1531 wValue, wIndex, wLength);
1534 /* respond with data transfer before status phase? */
1536 req->length = value;
1537 req->zero = value < wLength
1538 && (value % gadget->ep0->maxpacket) == 0;
1539 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1541 DEBUG (dev, "ep_queue --> %d\n", value);
1543 eth_setup_complete (gadget->ep0, req);
1547 /* host either stalls (value < 0) or reports success */
1552 eth_disconnect (struct usb_gadget *gadget)
1554 struct eth_dev *dev = get_gadget_data (gadget);
1555 unsigned long flags;
1557 spin_lock_irqsave (&dev->lock, flags);
1558 netif_stop_queue (dev->net);
1559 netif_carrier_off (dev->net);
1560 eth_reset_config (dev);
1561 spin_unlock_irqrestore (&dev->lock, flags);
1563 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1565 /* next we may get setup() calls to enumerate new connections;
1566 * or an unbind() during shutdown (including removing module).
1570 /*-------------------------------------------------------------------------*/
1572 /* NETWORK DRIVER HOOKUP (to the layer above this driver) */
1574 static int eth_change_mtu (struct net_device *net, int new_mtu)
1576 struct eth_dev *dev = netdev_priv(net);
1581 if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN)
1583 /* no zero-length packet read wanted after mtu-sized packets */
1584 if (((new_mtu + sizeof (struct ethhdr)) % dev->in_ep->maxpacket) == 0)
1590 static struct net_device_stats *eth_get_stats (struct net_device *net)
1592 return &((struct eth_dev *)netdev_priv(net))->stats;
1595 static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
1597 struct eth_dev *dev = netdev_priv(net);
1598 strlcpy(p->driver, shortname, sizeof p->driver);
1599 strlcpy(p->version, DRIVER_VERSION, sizeof p->version);
1600 strlcpy(p->fw_version, dev->gadget->name, sizeof p->fw_version);
1601 strlcpy (p->bus_info, dev->gadget->dev.bus_id, sizeof p->bus_info);
1604 static u32 eth_get_link(struct net_device *net)
1606 struct eth_dev *dev = netdev_priv(net);
1607 return dev->gadget->speed != USB_SPEED_UNKNOWN;
1610 static struct ethtool_ops ops = {
1611 .get_drvinfo = eth_get_drvinfo,
1612 .get_link = eth_get_link
1615 static void defer_kevent (struct eth_dev *dev, int flag)
1617 if (test_and_set_bit (flag, &dev->todo))
1619 if (!schedule_work (&dev->work))
1620 ERROR (dev, "kevent %d may have been dropped\n", flag);
1622 DEBUG (dev, "kevent %d scheduled\n", flag);
1625 static void rx_complete (struct usb_ep *ep, struct usb_request *req);
1628 rx_submit (struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
1630 struct sk_buff *skb;
1631 int retval = -ENOMEM;
1634 /* Padding up to RX_EXTRA handles minor disagreements with host.
1635 * Normally we use the USB "terminate on short read" convention;
1636 * so allow up to (N*maxpacket), since that memory is normally
1637 * already allocated. Some hardware doesn't deal well with short
1638 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1639 * byte off the end (to force hardware errors on overflow).
1641 * RNDIS uses internal framing, and explicitly allows senders to
1642 * pad to end-of-packet. That's potentially nice for speed,
1643 * but means receivers can't recover synch on their own.
1645 size = (sizeof (struct ethhdr) + dev->net->mtu + RX_EXTRA);
1646 size += dev->out_ep->maxpacket - 1;
1647 if (rndis_active(dev))
1648 size += sizeof (struct rndis_packet_msg_type);
1649 size -= size % dev->out_ep->maxpacket;
1651 if ((skb = alloc_skb (size + NET_IP_ALIGN, gfp_flags)) == 0) {
1652 DEBUG (dev, "no rx skb\n");
1656 /* Some platforms perform better when IP packets are aligned,
1657 * but on at least one, checksumming fails otherwise. Note:
1658 * RNDIS headers involve variable numbers of LE32 values.
1660 skb_reserve(skb, NET_IP_ALIGN);
1662 req->buf = skb->data;
1664 req->complete = rx_complete;
1667 retval = usb_ep_queue (dev->out_ep, req, gfp_flags);
1668 if (retval == -ENOMEM)
1670 defer_kevent (dev, WORK_RX_MEMORY);
1672 DEBUG (dev, "rx submit --> %d\n", retval);
1673 dev_kfree_skb_any (skb);
1674 spin_lock(&dev->req_lock);
1675 list_add (&req->list, &dev->rx_reqs);
1676 spin_unlock(&dev->req_lock);
1681 static void rx_complete (struct usb_ep *ep, struct usb_request *req)
1683 struct sk_buff *skb = req->context;
1684 struct eth_dev *dev = ep->driver_data;
1685 int status = req->status;
1689 /* normal completion */
1691 skb_put (skb, req->actual);
1692 /* we know MaxPacketsPerTransfer == 1 here */
1693 if (rndis_active(dev))
1694 status = rndis_rm_hdr (skb);
1696 || ETH_HLEN > skb->len
1697 || skb->len > ETH_FRAME_LEN) {
1698 dev->stats.rx_errors++;
1699 dev->stats.rx_length_errors++;
1700 DEBUG (dev, "rx length %d\n", skb->len);
1704 skb->dev = dev->net;
1705 skb->protocol = eth_type_trans (skb, dev->net);
1706 dev->stats.rx_packets++;
1707 dev->stats.rx_bytes += skb->len;
1709 /* no buffer copies needed, unless hardware can't
1712 status = netif_rx (skb);
1716 /* software-driven interface shutdown */
1717 case -ECONNRESET: // unlink
1718 case -ESHUTDOWN: // disconnect etc
1719 VDEBUG (dev, "rx shutdown, code %d\n", status);
1722 /* for hardware automagic (such as pxa) */
1723 case -ECONNABORTED: // endpoint reset
1724 DEBUG (dev, "rx %s reset\n", ep->name);
1725 defer_kevent (dev, WORK_RX_MEMORY);
1727 dev_kfree_skb_any (skb);
1732 dev->stats.rx_over_errors++;
1736 dev->stats.rx_errors++;
1737 DEBUG (dev, "rx status %d\n", status);
1742 dev_kfree_skb_any (skb);
1743 if (!netif_running (dev->net)) {
1745 spin_lock(&dev->req_lock);
1746 list_add (&req->list, &dev->rx_reqs);
1747 spin_unlock(&dev->req_lock);
1751 rx_submit (dev, req, GFP_ATOMIC);
1754 static int prealloc (struct list_head *list, struct usb_ep *ep,
1755 unsigned n, gfp_t gfp_flags)
1758 struct usb_request *req;
1763 /* queue/recycle up to N requests */
1765 list_for_each_entry (req, list, list) {
1770 req = usb_ep_alloc_request (ep, gfp_flags);
1772 return list_empty (list) ? -ENOMEM : 0;
1773 list_add (&req->list, list);
1780 struct list_head *next;
1782 next = req->list.next;
1783 list_del (&req->list);
1784 usb_ep_free_request (ep, req);
1789 req = container_of (next, struct usb_request, list);
1794 static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
1798 spin_lock(&dev->req_lock);
1799 status = prealloc (&dev->tx_reqs, dev->in_ep, n, gfp_flags);
1802 status = prealloc (&dev->rx_reqs, dev->out_ep, n, gfp_flags);
1807 DEBUG (dev, "can't alloc requests\n");
1809 spin_unlock(&dev->req_lock);
1813 static void rx_fill (struct eth_dev *dev, gfp_t gfp_flags)
1815 struct usb_request *req;
1816 unsigned long flags;
1818 /* fill unused rxq slots with some skb */
1819 spin_lock_irqsave(&dev->req_lock, flags);
1820 while (!list_empty (&dev->rx_reqs)) {
1821 req = container_of (dev->rx_reqs.next,
1822 struct usb_request, list);
1823 list_del_init (&req->list);
1824 spin_unlock_irqrestore(&dev->req_lock, flags);
1826 if (rx_submit (dev, req, gfp_flags) < 0) {
1827 defer_kevent (dev, WORK_RX_MEMORY);
1831 spin_lock_irqsave(&dev->req_lock, flags);
1833 spin_unlock_irqrestore(&dev->req_lock, flags);
1836 static void eth_work (void *_dev)
1838 struct eth_dev *dev = _dev;
1840 if (test_and_clear_bit (WORK_RX_MEMORY, &dev->todo)) {
1841 if (netif_running (dev->net))
1842 rx_fill (dev, GFP_KERNEL);
1846 DEBUG (dev, "work done, flags = 0x%lx\n", dev->todo);
1849 static void tx_complete (struct usb_ep *ep, struct usb_request *req)
1851 struct sk_buff *skb = req->context;
1852 struct eth_dev *dev = ep->driver_data;
1854 switch (req->status) {
1856 dev->stats.tx_errors++;
1857 VDEBUG (dev, "tx err %d\n", req->status);
1859 case -ECONNRESET: // unlink
1860 case -ESHUTDOWN: // disconnect etc
1863 dev->stats.tx_bytes += skb->len;
1865 dev->stats.tx_packets++;
1867 spin_lock(&dev->req_lock);
1868 list_add (&req->list, &dev->tx_reqs);
1869 spin_unlock(&dev->req_lock);
1870 dev_kfree_skb_any (skb);
1872 atomic_dec (&dev->tx_qlen);
1873 if (netif_carrier_ok (dev->net))
1874 netif_wake_queue (dev->net);
1877 static inline int eth_is_promisc (struct eth_dev *dev)
1879 /* no filters for the CDC subset; always promisc */
1880 if (subset_active (dev))
1882 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1885 static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1887 struct eth_dev *dev = netdev_priv(net);
1888 int length = skb->len;
1890 struct usb_request *req = NULL;
1891 unsigned long flags;
1893 /* apply outgoing CDC or RNDIS filters */
1894 if (!eth_is_promisc (dev)) {
1895 u8 *dest = skb->data;
1897 if (dest [0] & 0x01) {
1900 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1901 * SET_ETHERNET_MULTICAST_FILTERS requests
1903 if (memcmp (dest, net->broadcast, ETH_ALEN) == 0)
1904 type = USB_CDC_PACKET_TYPE_BROADCAST;
1906 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1907 if (!(dev->cdc_filter & type)) {
1908 dev_kfree_skb_any (skb);
1912 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1915 spin_lock_irqsave(&dev->req_lock, flags);
1916 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1917 list_del (&req->list);
1918 if (list_empty (&dev->tx_reqs))
1919 netif_stop_queue (net);
1920 spin_unlock_irqrestore(&dev->req_lock, flags);
1922 /* no buffer copies needed, unless the network stack did it
1923 * or the hardware can't use skb buffers.
1924 * or there's not enough space for any RNDIS headers we need
1926 if (rndis_active(dev)) {
1927 struct sk_buff *skb_rndis;
1929 skb_rndis = skb_realloc_headroom (skb,
1930 sizeof (struct rndis_packet_msg_type));
1934 dev_kfree_skb_any (skb);
1936 rndis_add_hdr (skb);
1939 req->buf = skb->data;
1941 req->complete = tx_complete;
1943 /* use zlp framing on tx for strict CDC-Ether conformance,
1944 * though any robust network rx path ignores extra padding.
1945 * and some hardware doesn't like to write zlps.
1948 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1951 req->length = length;
1953 #ifdef CONFIG_USB_GADGET_DUALSPEED
1954 /* throttle highspeed IRQ rate back slightly */
1955 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1956 ? ((atomic_read (&dev->tx_qlen) % TX_DELAY) != 0)
1960 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1963 DEBUG (dev, "tx queue err %d\n", retval);
1966 net->trans_start = jiffies;
1967 atomic_inc (&dev->tx_qlen);
1972 dev->stats.tx_dropped++;
1973 dev_kfree_skb_any (skb);
1974 spin_lock_irqsave(&dev->req_lock, flags);
1975 if (list_empty (&dev->tx_reqs))
1976 netif_start_queue (net);
1977 list_add (&req->list, &dev->tx_reqs);
1978 spin_unlock_irqrestore(&dev->req_lock, flags);
1983 /*-------------------------------------------------------------------------*/
1985 #ifdef CONFIG_USB_ETH_RNDIS
1987 /* The interrupt endpoint is used in RNDIS to notify the host when messages
1988 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1989 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1990 * REMOTE_NDIS_KEEPALIVE_MSG.
1992 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1993 * normally just one notification will be queued.
1996 static struct usb_request *eth_req_alloc (struct usb_ep *, unsigned, gfp_t);
1997 static void eth_req_free (struct usb_ep *ep, struct usb_request *req);
2000 rndis_control_ack_complete (struct usb_ep *ep, struct usb_request *req)
2002 struct eth_dev *dev = ep->driver_data;
2004 if (req->status || req->actual != req->length)
2006 "rndis control ack complete --> %d, %d/%d\n",
2007 req->status, req->actual, req->length);
2008 req->context = NULL;
2010 if (req != dev->stat_req)
2011 eth_req_free(ep, req);
2014 static int rndis_control_ack (struct net_device *net)
2016 struct eth_dev *dev = netdev_priv(net);
2018 struct usb_request *resp = dev->stat_req;
2020 /* in case RNDIS calls this after disconnect */
2022 DEBUG (dev, "status ENODEV\n");
2026 /* in case queue length > 1 */
2027 if (resp->context) {
2028 resp = eth_req_alloc (dev->status_ep, 8, GFP_ATOMIC);
2033 /* Send RNDIS RESPONSE_AVAILABLE notification;
2034 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
2037 resp->complete = rndis_control_ack_complete;
2038 resp->context = dev;
2040 *((__le32 *) resp->buf) = __constant_cpu_to_le32 (1);
2041 *((__le32 *) resp->buf + 1) = __constant_cpu_to_le32 (0);
2043 length = usb_ep_queue (dev->status_ep, resp, GFP_ATOMIC);
2046 rndis_control_ack_complete (dev->status_ep, resp);
2054 #define rndis_control_ack NULL
2058 static void eth_start (struct eth_dev *dev, gfp_t gfp_flags)
2060 DEBUG (dev, "%s\n", __FUNCTION__);
2062 /* fill the rx queue */
2063 rx_fill (dev, gfp_flags);
2065 /* and open the tx floodgates */
2066 atomic_set (&dev->tx_qlen, 0);
2067 netif_wake_queue (dev->net);
2068 if (rndis_active(dev)) {
2069 rndis_set_param_medium (dev->rndis_config,
2071 BITRATE(dev->gadget)/100);
2072 (void) rndis_signal_connect (dev->rndis_config);
2076 static int eth_open (struct net_device *net)
2078 struct eth_dev *dev = netdev_priv(net);
2080 DEBUG (dev, "%s\n", __FUNCTION__);
2081 if (netif_carrier_ok (dev->net))
2082 eth_start (dev, GFP_KERNEL);
2086 static int eth_stop (struct net_device *net)
2088 struct eth_dev *dev = netdev_priv(net);
2090 VDEBUG (dev, "%s\n", __FUNCTION__);
2091 netif_stop_queue (net);
2093 DEBUG (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
2094 dev->stats.rx_packets, dev->stats.tx_packets,
2095 dev->stats.rx_errors, dev->stats.tx_errors
2098 /* ensure there are no more active requests */
2100 usb_ep_disable (dev->in_ep);
2101 usb_ep_disable (dev->out_ep);
2102 if (netif_carrier_ok (dev->net)) {
2103 DEBUG (dev, "host still using in/out endpoints\n");
2104 // FIXME idiom may leave toggle wrong here
2105 usb_ep_enable (dev->in_ep, dev->in);
2106 usb_ep_enable (dev->out_ep, dev->out);
2108 if (dev->status_ep) {
2109 usb_ep_disable (dev->status_ep);
2110 usb_ep_enable (dev->status_ep, dev->status);
2114 if (rndis_active(dev)) {
2115 rndis_set_param_medium (dev->rndis_config,
2116 NDIS_MEDIUM_802_3, 0);
2117 (void) rndis_signal_disconnect (dev->rndis_config);
2123 /*-------------------------------------------------------------------------*/
2125 static struct usb_request *
2126 eth_req_alloc (struct usb_ep *ep, unsigned size, gfp_t gfp_flags)
2128 struct usb_request *req;
2130 req = usb_ep_alloc_request (ep, gfp_flags);
2134 req->buf = kmalloc (size, gfp_flags);
2136 usb_ep_free_request (ep, req);
2143 eth_req_free (struct usb_ep *ep, struct usb_request *req)
2146 usb_ep_free_request (ep, req);
2150 static void /* __init_or_exit */
2151 eth_unbind (struct usb_gadget *gadget)
2153 struct eth_dev *dev = get_gadget_data (gadget);
2155 DEBUG (dev, "unbind\n");
2156 rndis_deregister (dev->rndis_config);
2159 /* we've already been disconnected ... no i/o is active */
2161 eth_req_free (gadget->ep0, dev->req);
2164 if (dev->stat_req) {
2165 eth_req_free (dev->status_ep, dev->stat_req);
2166 dev->stat_req = NULL;
2169 unregister_netdev (dev->net);
2170 free_netdev(dev->net);
2172 /* assuming we used keventd, it must quiesce too */
2173 flush_scheduled_work ();
2174 set_gadget_data (gadget, NULL);
2177 static u8 __devinit nibble (unsigned char c)
2179 if (likely (isdigit (c)))
2182 if (likely (isxdigit (c)))
2183 return 10 + c - 'A';
2187 static int __devinit get_ether_addr(const char *str, u8 *dev_addr)
2192 for (i = 0; i < 6; i++) {
2195 if((*str == '.') || (*str == ':'))
2197 num = nibble(*str++) << 4;
2198 num |= (nibble(*str++));
2201 if (is_valid_ether_addr (dev_addr))
2204 random_ether_addr(dev_addr);
2208 static int __devinit
2209 eth_bind (struct usb_gadget *gadget)
2211 struct eth_dev *dev;
2212 struct net_device *net;
2213 u8 cdc = 1, zlp = 1, rndis = 1;
2214 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
2215 int status = -ENOMEM;
2218 /* these flags are only ever cleared; compiler take note */
2219 #ifndef DEV_CONFIG_CDC
2222 #ifndef CONFIG_USB_ETH_RNDIS
2226 /* Because most host side USB stacks handle CDC Ethernet, that
2227 * standard protocol is _strongly_ preferred for interop purposes.
2228 * (By everyone except Microsoft.)
2230 if (gadget_is_pxa (gadget)) {
2231 /* pxa doesn't support altsettings */
2233 } else if (gadget_is_musbhdrc(gadget)) {
2234 /* reduce tx dma overhead by avoiding special cases */
2236 } else if (gadget_is_sh(gadget)) {
2237 /* sh doesn't support multiple interfaces or configs */
2240 } else if (gadget_is_sa1100 (gadget)) {
2241 /* hardware can't write zlps */
2243 /* sa1100 CAN do CDC, without status endpoint ... we use
2244 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2249 gcnum = usb_gadget_controller_number (gadget);
2251 device_desc.bcdDevice = cpu_to_le16 (0x0200 + gcnum);
2253 /* can't assume CDC works. don't want to default to
2254 * anything less functional on CDC-capable hardware,
2255 * so we fail in this case.
2257 dev_err (&gadget->dev,
2258 "controller '%s' not recognized\n",
2262 snprintf (manufacturer, sizeof manufacturer, "%s %s/%s",
2263 init_utsname()->sysname, init_utsname()->release,
2266 /* If there's an RNDIS configuration, that's what Windows wants to
2267 * be using ... so use these product IDs here and in the "linux.inf"
2268 * needed to install MSFT drivers. Current Linux kernels will use
2269 * the second configuration if it's CDC Ethernet, and need some help
2270 * to choose the right configuration otherwise.
2273 device_desc.idVendor =
2274 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2275 device_desc.idProduct =
2276 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2277 snprintf (product_desc, sizeof product_desc,
2278 "RNDIS/%s", driver_desc);
2280 /* CDC subset ... recognized by Linux since 2.4.10, but Windows
2281 * drivers aren't widely available.
2284 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2285 device_desc.idVendor =
2286 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2287 device_desc.idProduct =
2288 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2291 /* support optional vendor/distro customization */
2294 dev_err (&gadget->dev, "idVendor needs idProduct!\n");
2297 device_desc.idVendor = cpu_to_le16(idVendor);
2298 device_desc.idProduct = cpu_to_le16(idProduct);
2300 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2303 strlcpy (manufacturer, iManufacturer, sizeof manufacturer);
2305 strlcpy (product_desc, iProduct, sizeof product_desc);
2306 if (iSerialNumber) {
2307 device_desc.iSerialNumber = STRING_SERIALNUMBER,
2308 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
2311 /* all we really need is bulk IN/OUT */
2312 usb_ep_autoconfig_reset (gadget);
2313 in_ep = usb_ep_autoconfig (gadget, &fs_source_desc);
2316 dev_err (&gadget->dev,
2317 "can't autoconfigure on %s\n",
2321 in_ep->driver_data = in_ep; /* claim */
2323 out_ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
2326 out_ep->driver_data = out_ep; /* claim */
2328 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2329 /* CDC Ethernet control interface doesn't require a status endpoint.
2330 * Since some hosts expect one, try to allocate one anyway.
2333 status_ep = usb_ep_autoconfig (gadget, &fs_status_desc);
2335 status_ep->driver_data = status_ep; /* claim */
2337 dev_err (&gadget->dev,
2338 "can't run RNDIS on %s\n",
2341 #ifdef DEV_CONFIG_CDC
2342 /* pxa25x only does CDC subset; often used with RNDIS */
2344 control_intf.bNumEndpoints = 0;
2345 /* FIXME remove endpoint from descriptor list */
2351 /* one config: cdc, else minimal subset */
2353 eth_config.bNumInterfaces = 1;
2354 eth_config.iConfiguration = STRING_SUBSET;
2355 fs_subset_descriptors();
2356 hs_subset_descriptors();
2359 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
2360 usb_gadget_set_selfpowered (gadget);
2362 /* For now RNDIS is always a second config */
2364 device_desc.bNumConfigurations = 2;
2366 #ifdef CONFIG_USB_GADGET_DUALSPEED
2368 dev_qualifier.bNumConfigurations = 2;
2370 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2372 /* assumes ep0 uses the same value for both speeds ... */
2373 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2375 /* and that all endpoints are dual-speed */
2376 hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
2377 hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
2378 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2380 hs_status_desc.bEndpointAddress =
2381 fs_status_desc.bEndpointAddress;
2383 #endif /* DUALSPEED */
2385 if (gadget->is_otg) {
2386 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2387 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2388 eth_config.bMaxPower = 4;
2389 #ifdef CONFIG_USB_ETH_RNDIS
2390 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2391 rndis_config.bMaxPower = 4;
2395 net = alloc_etherdev (sizeof *dev);
2398 dev = netdev_priv(net);
2399 spin_lock_init (&dev->lock);
2400 spin_lock_init (&dev->req_lock);
2401 INIT_WORK (&dev->work, eth_work, dev);
2402 INIT_LIST_HEAD (&dev->tx_reqs);
2403 INIT_LIST_HEAD (&dev->rx_reqs);
2405 /* network device setup */
2407 SET_MODULE_OWNER (net);
2408 strcpy (net->name, "usb%d");
2413 dev->out_ep = out_ep;
2414 dev->status_ep = status_ep;
2416 /* Module params for these addresses should come from ID proms.
2417 * The host side address is used with CDC and RNDIS, and commonly
2418 * ends up in a persistent config database.
2420 if (get_ether_addr(dev_addr, net->dev_addr))
2421 dev_warn(&gadget->dev,
2422 "using random %s ethernet address\n", "self");
2424 if (get_ether_addr(host_addr, dev->host_mac))
2425 dev_warn(&gadget->dev,
2426 "using random %s ethernet address\n", "host");
2427 #ifdef DEV_CONFIG_CDC
2428 snprintf (ethaddr, sizeof ethaddr, "%02X%02X%02X%02X%02X%02X",
2429 dev->host_mac [0], dev->host_mac [1],
2430 dev->host_mac [2], dev->host_mac [3],
2431 dev->host_mac [4], dev->host_mac [5]);
2436 status = rndis_init();
2438 dev_err (&gadget->dev, "can't init RNDIS, %d\n",
2444 net->change_mtu = eth_change_mtu;
2445 net->get_stats = eth_get_stats;
2446 net->hard_start_xmit = eth_start_xmit;
2447 net->open = eth_open;
2448 net->stop = eth_stop;
2449 // watchdog_timeo, tx_timeout ...
2450 // set_multicast_list
2451 SET_ETHTOOL_OPS(net, &ops);
2453 /* preallocate control message data and buffer */
2454 dev->req = eth_req_alloc (gadget->ep0, USB_BUFSIZ, GFP_KERNEL);
2457 dev->req->complete = eth_setup_complete;
2459 /* ... and maybe likewise for status transfer */
2460 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2461 if (dev->status_ep) {
2462 dev->stat_req = eth_req_alloc (dev->status_ep,
2463 STATUS_BYTECOUNT, GFP_KERNEL);
2464 if (!dev->stat_req) {
2465 eth_req_free (gadget->ep0, dev->req);
2468 dev->stat_req->context = NULL;
2472 /* finish hookup to lower layer ... */
2473 dev->gadget = gadget;
2474 set_gadget_data (gadget, dev);
2475 gadget->ep0->driver_data = dev;
2477 /* two kinds of host-initiated state changes:
2478 * - iff DATA transfer is active, carrier is "on"
2479 * - tx queueing enabled if open *and* carrier is "on"
2481 netif_stop_queue (dev->net);
2482 netif_carrier_off (dev->net);
2484 SET_NETDEV_DEV (dev->net, &gadget->dev);
2485 status = register_netdev (dev->net);
2489 INFO (dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
2490 INFO (dev, "using %s, OUT %s IN %s%s%s\n", gadget->name,
2491 out_ep->name, in_ep->name,
2492 status_ep ? " STATUS " : "",
2493 status_ep ? status_ep->name : ""
2495 INFO (dev, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2496 net->dev_addr [0], net->dev_addr [1],
2497 net->dev_addr [2], net->dev_addr [3],
2498 net->dev_addr [4], net->dev_addr [5]);
2501 INFO (dev, "HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2502 dev->host_mac [0], dev->host_mac [1],
2503 dev->host_mac [2], dev->host_mac [3],
2504 dev->host_mac [4], dev->host_mac [5]);
2509 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2511 dev->rndis_config = rndis_register (rndis_control_ack);
2512 if (dev->rndis_config < 0) {
2514 unregister_netdev (dev->net);
2519 /* these set up a lot of the OIDs that RNDIS needs */
2520 rndis_set_host_mac (dev->rndis_config, dev->host_mac);
2521 if (rndis_set_param_dev (dev->rndis_config, dev->net,
2522 &dev->stats, &dev->cdc_filter))
2524 if (rndis_set_param_vendor (dev->rndis_config, vendorID,
2527 if (rndis_set_param_medium (dev->rndis_config,
2531 INFO (dev, "RNDIS ready\n");
2537 dev_dbg(&gadget->dev, "register_netdev failed, %d\n", status);
2539 eth_unbind (gadget);
2543 /*-------------------------------------------------------------------------*/
2546 eth_suspend (struct usb_gadget *gadget)
2548 struct eth_dev *dev = get_gadget_data (gadget);
2550 DEBUG (dev, "suspend\n");
2555 eth_resume (struct usb_gadget *gadget)
2557 struct eth_dev *dev = get_gadget_data (gadget);
2559 DEBUG (dev, "resume\n");
2563 /*-------------------------------------------------------------------------*/
2565 static struct usb_gadget_driver eth_driver = {
2568 .function = (char *) driver_desc,
2570 .unbind = eth_unbind,
2573 .disconnect = eth_disconnect,
2575 .suspend = eth_suspend,
2576 .resume = eth_resume,
2579 .name = (char *) shortname,
2580 .owner = THIS_MODULE,
2584 MODULE_DESCRIPTION (DRIVER_DESC);
2585 MODULE_AUTHOR ("David Brownell, Benedikt Spanger");
2586 MODULE_LICENSE ("GPL");
2589 static int __init init (void)
2591 return usb_gadget_register_driver (ð_driver);
2595 static void __exit cleanup (void)
2597 usb_gadget_unregister_driver (ð_driver);
2599 module_exit (cleanup);