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
120 struct list_head tx_reqs, rx_reqs;
122 struct net_device *net;
123 struct net_device_stats stats;
126 struct work_struct work;
130 unsigned suspended:1;
133 #define WORK_RX_MEMORY 0
135 u8 host_mac [ETH_ALEN];
138 /* This version autoconfigures as much as possible at run-time.
140 * It also ASSUMES a self-powered device, without remote wakeup,
141 * although remote wakeup support would make sense.
144 /*-------------------------------------------------------------------------*/
146 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
147 * Instead: allocate your own, using normal USB-IF procedures.
150 /* Thanks to NetChip Technologies for donating this product ID.
151 * It's for devices with only CDC Ethernet configurations.
153 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
154 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
156 /* For hardware that can't talk CDC, we use the same vendor ID that
157 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
158 * with pxa250. We're protocol-compatible, if the host-side drivers
159 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
160 * drivers that need to hard-wire endpoint numbers have a hook.
162 * The protocol is a minimal subset of CDC Ether, which works on any bulk
163 * hardware that's not deeply broken ... even on hardware that can't talk
164 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
165 * doesn't handle control-OUT).
167 #define SIMPLE_VENDOR_NUM 0x049f
168 #define SIMPLE_PRODUCT_NUM 0x505a
170 /* For hardware that can talk RNDIS and either of the above protocols,
171 * use this ID ... the windows INF files will know it. Unless it's
172 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
173 * the non-RNDIS configuration.
175 #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
176 #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
179 /* Some systems will want different product identifers published in the
180 * device descriptor, either numbers or strings or both. These string
181 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
184 static ushort idVendor;
185 module_param(idVendor, ushort, S_IRUGO);
186 MODULE_PARM_DESC(idVendor, "USB Vendor ID");
188 static ushort idProduct;
189 module_param(idProduct, ushort, S_IRUGO);
190 MODULE_PARM_DESC(idProduct, "USB Product ID");
192 static ushort bcdDevice;
193 module_param(bcdDevice, ushort, S_IRUGO);
194 MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
196 static char *iManufacturer;
197 module_param(iManufacturer, charp, S_IRUGO);
198 MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
200 static char *iProduct;
201 module_param(iProduct, charp, S_IRUGO);
202 MODULE_PARM_DESC(iProduct, "USB Product string");
204 static char *iSerialNumber;
205 module_param(iSerialNumber, charp, S_IRUGO);
206 MODULE_PARM_DESC(iSerialNumber, "SerialNumber");
208 /* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */
209 static char *dev_addr;
210 module_param(dev_addr, charp, S_IRUGO);
211 MODULE_PARM_DESC(dev_addr, "Device Ethernet Address");
213 /* this address is invisible to ifconfig */
214 static char *host_addr;
215 module_param(host_addr, charp, S_IRUGO);
216 MODULE_PARM_DESC(host_addr, "Host Ethernet Address");
219 /*-------------------------------------------------------------------------*/
221 /* Include CDC support if we could run on CDC-capable hardware. */
223 #ifdef CONFIG_USB_GADGET_NET2280
224 #define DEV_CONFIG_CDC
227 #ifdef CONFIG_USB_GADGET_DUMMY_HCD
228 #define DEV_CONFIG_CDC
231 #ifdef CONFIG_USB_GADGET_GOKU
232 #define DEV_CONFIG_CDC
235 #ifdef CONFIG_USB_GADGET_LH7A40X
236 #define DEV_CONFIG_CDC
239 #ifdef CONFIG_USB_GADGET_MQ11XX
240 #define DEV_CONFIG_CDC
243 #ifdef CONFIG_USB_GADGET_OMAP
244 #define DEV_CONFIG_CDC
247 #ifdef CONFIG_USB_GADGET_N9604
248 #define DEV_CONFIG_CDC
251 #ifdef CONFIG_USB_GADGET_PXA27X
252 #define DEV_CONFIG_CDC
255 #ifdef CONFIG_USB_GADGET_AT91
256 #define DEV_CONFIG_CDC
259 #ifdef CONFIG_USB_GADGET_MUSBHSFC
260 #define DEV_CONFIG_CDC
263 #ifdef CONFIG_USB_GADGET_MUSBHDRC
264 #define DEV_CONFIG_CDC
268 /* For CDC-incapable hardware, choose the simple cdc subset.
269 * Anything that talks bulk (without notable bugs) can do this.
271 #ifdef CONFIG_USB_GADGET_PXA2XX
272 #define DEV_CONFIG_SUBSET
275 #ifdef CONFIG_USB_GADGET_SH
276 #define DEV_CONFIG_SUBSET
279 #ifdef CONFIG_USB_GADGET_SA1100
280 /* use non-CDC for backwards compatibility */
281 #define DEV_CONFIG_SUBSET
284 #ifdef CONFIG_USB_GADGET_S3C2410
285 #define DEV_CONFIG_CDC
288 /*-------------------------------------------------------------------------*/
290 /* "main" config is either CDC, or its simple subset */
291 static inline int is_cdc(struct eth_dev *dev)
293 #if !defined(DEV_CONFIG_SUBSET)
294 return 1; /* only cdc possible */
295 #elif !defined (DEV_CONFIG_CDC)
296 return 0; /* only subset possible */
298 return dev->cdc; /* depends on what hardware we found */
302 /* "secondary" RNDIS config may sometimes be activated */
303 static inline int rndis_active(struct eth_dev *dev)
305 #ifdef CONFIG_USB_ETH_RNDIS
312 #define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
313 #define cdc_active(dev) ( is_cdc(dev) && !rndis_active(dev))
317 #define DEFAULT_QLEN 2 /* double buffering by default */
319 /* peak bulk transfer bits-per-second */
320 #define HS_BPS (13 * 512 * 8 * 1000 * 8)
321 #define FS_BPS (19 * 64 * 1 * 1000 * 8)
323 #ifdef CONFIG_USB_GADGET_DUALSPEED
324 #define DEVSPEED USB_SPEED_HIGH
326 static unsigned qmult = 5;
327 module_param (qmult, uint, S_IRUGO|S_IWUSR);
330 /* for dual-speed hardware, use deeper queues at highspeed */
331 #define qlen(gadget) \
332 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
334 /* also defer IRQs on highspeed TX */
335 #define TX_DELAY qmult
337 static inline int BITRATE(struct usb_gadget *g)
339 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
342 #else /* full speed (low speed doesn't do bulk) */
343 #define DEVSPEED USB_SPEED_FULL
345 #define qlen(gadget) DEFAULT_QLEN
347 static inline int BITRATE(struct usb_gadget *g)
354 /*-------------------------------------------------------------------------*/
356 #define xprintk(d,level,fmt,args...) \
357 printk(level "%s: " fmt , (d)->net->name , ## args)
361 #define DEBUG(dev,fmt,args...) \
362 xprintk(dev , KERN_DEBUG , fmt , ## args)
364 #define DEBUG(dev,fmt,args...) \
371 #define VDEBUG(dev,fmt,args...) \
375 #define ERROR(dev,fmt,args...) \
376 xprintk(dev , KERN_ERR , fmt , ## args)
377 #define WARN(dev,fmt,args...) \
378 xprintk(dev , KERN_WARNING , fmt , ## args)
379 #define INFO(dev,fmt,args...) \
380 xprintk(dev , KERN_INFO , fmt , ## args)
382 /*-------------------------------------------------------------------------*/
384 /* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
385 * ep0 implementation: descriptors, config management, setup().
386 * also optional class-specific notification interrupt transfer.
390 * DESCRIPTORS ... most are static, but strings and (full) configuration
391 * descriptors are built on demand. For now we do either full CDC, or
392 * our simple subset, with RNDIS as an optional second configuration.
394 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
395 * the class descriptors match a modem (they're ignored; it's really just
396 * Ethernet functionality), they don't need the NOP altsetting, and the
397 * status transfer endpoint isn't optional.
400 #define STRING_MANUFACTURER 1
401 #define STRING_PRODUCT 2
402 #define STRING_ETHADDR 3
403 #define STRING_DATA 4
404 #define STRING_CONTROL 5
405 #define STRING_RNDIS_CONTROL 6
407 #define STRING_SUBSET 8
408 #define STRING_RNDIS 9
409 #define STRING_SERIALNUMBER 10
411 /* holds our biggest descriptor (or RNDIS response) */
412 #define USB_BUFSIZ 256
415 * This device advertises one configuration, eth_config, unless RNDIS
416 * is enabled (rndis_config) on hardware supporting at least two configs.
418 * NOTE: Controllers like superh_udc should probably be able to use
419 * an RNDIS-only configuration.
421 * FIXME define some higher-powered configurations to make it easier
422 * to recharge batteries ...
425 #define DEV_CONFIG_VALUE 1 /* cdc or subset */
426 #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
428 static struct usb_device_descriptor
430 .bLength = sizeof device_desc,
431 .bDescriptorType = USB_DT_DEVICE,
433 .bcdUSB = __constant_cpu_to_le16 (0x0200),
435 .bDeviceClass = USB_CLASS_COMM,
436 .bDeviceSubClass = 0,
437 .bDeviceProtocol = 0,
439 .idVendor = __constant_cpu_to_le16 (CDC_VENDOR_NUM),
440 .idProduct = __constant_cpu_to_le16 (CDC_PRODUCT_NUM),
441 .iManufacturer = STRING_MANUFACTURER,
442 .iProduct = STRING_PRODUCT,
443 .bNumConfigurations = 1,
446 static struct usb_otg_descriptor
448 .bLength = sizeof otg_descriptor,
449 .bDescriptorType = USB_DT_OTG,
451 .bmAttributes = USB_OTG_SRP,
454 static struct usb_config_descriptor
456 .bLength = sizeof eth_config,
457 .bDescriptorType = USB_DT_CONFIG,
459 /* compute wTotalLength on the fly */
461 .bConfigurationValue = DEV_CONFIG_VALUE,
462 .iConfiguration = STRING_CDC,
463 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
467 #ifdef CONFIG_USB_ETH_RNDIS
468 static struct usb_config_descriptor
470 .bLength = sizeof rndis_config,
471 .bDescriptorType = USB_DT_CONFIG,
473 /* compute wTotalLength on the fly */
475 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
476 .iConfiguration = STRING_RNDIS,
477 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
483 * Compared to the simple CDC subset, the full CDC Ethernet model adds
484 * three class descriptors, two interface descriptors, optional status
485 * endpoint. Both have a "data" interface and two bulk endpoints.
486 * There are also differences in how control requests are handled.
488 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of
489 * the CDC-ACM (modem) spec.
492 #ifdef DEV_CONFIG_CDC
493 static struct usb_interface_descriptor
495 .bLength = sizeof control_intf,
496 .bDescriptorType = USB_DT_INTERFACE,
498 .bInterfaceNumber = 0,
499 /* status endpoint is optional; this may be patched later */
501 .bInterfaceClass = USB_CLASS_COMM,
502 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
503 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
504 .iInterface = STRING_CONTROL,
508 #ifdef CONFIG_USB_ETH_RNDIS
509 static const struct usb_interface_descriptor
510 rndis_control_intf = {
511 .bLength = sizeof rndis_control_intf,
512 .bDescriptorType = USB_DT_INTERFACE,
514 .bInterfaceNumber = 0,
516 .bInterfaceClass = USB_CLASS_COMM,
517 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
518 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
519 .iInterface = STRING_RNDIS_CONTROL,
523 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
525 static const struct usb_cdc_header_desc header_desc = {
526 .bLength = sizeof header_desc,
527 .bDescriptorType = USB_DT_CS_INTERFACE,
528 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
530 .bcdCDC = __constant_cpu_to_le16 (0x0110),
533 static const struct usb_cdc_union_desc union_desc = {
534 .bLength = sizeof union_desc,
535 .bDescriptorType = USB_DT_CS_INTERFACE,
536 .bDescriptorSubType = USB_CDC_UNION_TYPE,
538 .bMasterInterface0 = 0, /* index of control interface */
539 .bSlaveInterface0 = 1, /* index of DATA interface */
542 #endif /* CDC || RNDIS */
544 #ifdef CONFIG_USB_ETH_RNDIS
546 static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
547 .bLength = sizeof call_mgmt_descriptor,
548 .bDescriptorType = USB_DT_CS_INTERFACE,
549 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
551 .bmCapabilities = 0x00,
552 .bDataInterface = 0x01,
555 static const struct usb_cdc_acm_descriptor acm_descriptor = {
556 .bLength = sizeof acm_descriptor,
557 .bDescriptorType = USB_DT_CS_INTERFACE,
558 .bDescriptorSubType = USB_CDC_ACM_TYPE,
560 .bmCapabilities = 0x00,
565 #ifdef DEV_CONFIG_CDC
567 static const struct usb_cdc_ether_desc ether_desc = {
568 .bLength = sizeof ether_desc,
569 .bDescriptorType = USB_DT_CS_INTERFACE,
570 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
572 /* this descriptor actually adds value, surprise! */
573 .iMACAddress = STRING_ETHADDR,
574 .bmEthernetStatistics = __constant_cpu_to_le32 (0), /* no statistics */
575 .wMaxSegmentSize = __constant_cpu_to_le16 (ETH_FRAME_LEN),
576 .wNumberMCFilters = __constant_cpu_to_le16 (0),
577 .bNumberPowerFilters = 0,
582 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
584 /* include the status endpoint if we can, even where it's optional.
585 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
586 * packet, to simplify cancellation; and a big transfer interval, to
587 * waste less bandwidth.
589 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
590 * if they ignore the connect/disconnect notifications that real aether
591 * can provide. more advanced cdc configurations might want to support
592 * encapsulated commands (vendor-specific, using control-OUT).
594 * RNDIS requires the status endpoint, since it uses that encapsulation
595 * mechanism for its funky RPC scheme.
598 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
599 #define STATUS_BYTECOUNT 16 /* 8 byte header + data */
601 static struct usb_endpoint_descriptor
603 .bLength = USB_DT_ENDPOINT_SIZE,
604 .bDescriptorType = USB_DT_ENDPOINT,
606 .bEndpointAddress = USB_DIR_IN,
607 .bmAttributes = USB_ENDPOINT_XFER_INT,
608 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
609 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
613 #ifdef DEV_CONFIG_CDC
615 /* the default data interface has no endpoints ... */
617 static const struct usb_interface_descriptor
619 .bLength = sizeof data_nop_intf,
620 .bDescriptorType = USB_DT_INTERFACE,
622 .bInterfaceNumber = 1,
623 .bAlternateSetting = 0,
625 .bInterfaceClass = USB_CLASS_CDC_DATA,
626 .bInterfaceSubClass = 0,
627 .bInterfaceProtocol = 0,
630 /* ... but the "real" data interface has two bulk endpoints */
632 static const struct usb_interface_descriptor
634 .bLength = sizeof data_intf,
635 .bDescriptorType = USB_DT_INTERFACE,
637 .bInterfaceNumber = 1,
638 .bAlternateSetting = 1,
640 .bInterfaceClass = USB_CLASS_CDC_DATA,
641 .bInterfaceSubClass = 0,
642 .bInterfaceProtocol = 0,
643 .iInterface = STRING_DATA,
648 #ifdef CONFIG_USB_ETH_RNDIS
650 /* RNDIS doesn't activate by changing to the "real" altsetting */
652 static const struct usb_interface_descriptor
654 .bLength = sizeof rndis_data_intf,
655 .bDescriptorType = USB_DT_INTERFACE,
657 .bInterfaceNumber = 1,
658 .bAlternateSetting = 0,
660 .bInterfaceClass = USB_CLASS_CDC_DATA,
661 .bInterfaceSubClass = 0,
662 .bInterfaceProtocol = 0,
663 .iInterface = STRING_DATA,
668 #ifdef DEV_CONFIG_SUBSET
671 * "Simple" CDC-subset option is a simple vendor-neutral model that most
672 * full speed controllers can handle: one interface, two bulk endpoints.
675 static const struct usb_interface_descriptor
677 .bLength = sizeof subset_data_intf,
678 .bDescriptorType = USB_DT_INTERFACE,
680 .bInterfaceNumber = 0,
681 .bAlternateSetting = 0,
683 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
684 .bInterfaceSubClass = 0,
685 .bInterfaceProtocol = 0,
686 .iInterface = STRING_DATA,
692 static struct usb_endpoint_descriptor
694 .bLength = USB_DT_ENDPOINT_SIZE,
695 .bDescriptorType = USB_DT_ENDPOINT,
697 .bEndpointAddress = USB_DIR_IN,
698 .bmAttributes = USB_ENDPOINT_XFER_BULK,
701 static struct usb_endpoint_descriptor
703 .bLength = USB_DT_ENDPOINT_SIZE,
704 .bDescriptorType = USB_DT_ENDPOINT,
706 .bEndpointAddress = USB_DIR_OUT,
707 .bmAttributes = USB_ENDPOINT_XFER_BULK,
710 static const struct usb_descriptor_header *fs_eth_function [11] = {
711 (struct usb_descriptor_header *) &otg_descriptor,
712 #ifdef DEV_CONFIG_CDC
713 /* "cdc" mode descriptors */
714 (struct usb_descriptor_header *) &control_intf,
715 (struct usb_descriptor_header *) &header_desc,
716 (struct usb_descriptor_header *) &union_desc,
717 (struct usb_descriptor_header *) ðer_desc,
718 /* NOTE: status endpoint may need to be removed */
719 (struct usb_descriptor_header *) &fs_status_desc,
720 /* data interface, with altsetting */
721 (struct usb_descriptor_header *) &data_nop_intf,
722 (struct usb_descriptor_header *) &data_intf,
723 (struct usb_descriptor_header *) &fs_source_desc,
724 (struct usb_descriptor_header *) &fs_sink_desc,
726 #endif /* DEV_CONFIG_CDC */
729 static inline void __init fs_subset_descriptors(void)
731 #ifdef DEV_CONFIG_SUBSET
732 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
733 fs_eth_function[2] = (struct usb_descriptor_header *) &fs_source_desc;
734 fs_eth_function[3] = (struct usb_descriptor_header *) &fs_sink_desc;
735 fs_eth_function[4] = NULL;
737 fs_eth_function[1] = NULL;
741 #ifdef CONFIG_USB_ETH_RNDIS
742 static const struct usb_descriptor_header *fs_rndis_function [] = {
743 (struct usb_descriptor_header *) &otg_descriptor,
744 /* control interface matches ACM, not Ethernet */
745 (struct usb_descriptor_header *) &rndis_control_intf,
746 (struct usb_descriptor_header *) &header_desc,
747 (struct usb_descriptor_header *) &call_mgmt_descriptor,
748 (struct usb_descriptor_header *) &acm_descriptor,
749 (struct usb_descriptor_header *) &union_desc,
750 (struct usb_descriptor_header *) &fs_status_desc,
751 /* data interface has no altsetting */
752 (struct usb_descriptor_header *) &rndis_data_intf,
753 (struct usb_descriptor_header *) &fs_source_desc,
754 (struct usb_descriptor_header *) &fs_sink_desc,
759 #ifdef CONFIG_USB_GADGET_DUALSPEED
762 * usb 2.0 devices need to expose both high speed and full speed
763 * descriptors, unless they only run at full speed.
766 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
767 static struct usb_endpoint_descriptor
769 .bLength = USB_DT_ENDPOINT_SIZE,
770 .bDescriptorType = USB_DT_ENDPOINT,
772 .bmAttributes = USB_ENDPOINT_XFER_INT,
773 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
774 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
776 #endif /* DEV_CONFIG_CDC */
778 static struct usb_endpoint_descriptor
780 .bLength = USB_DT_ENDPOINT_SIZE,
781 .bDescriptorType = USB_DT_ENDPOINT,
783 .bmAttributes = USB_ENDPOINT_XFER_BULK,
784 .wMaxPacketSize = __constant_cpu_to_le16 (512),
787 static struct usb_endpoint_descriptor
789 .bLength = USB_DT_ENDPOINT_SIZE,
790 .bDescriptorType = USB_DT_ENDPOINT,
792 .bmAttributes = USB_ENDPOINT_XFER_BULK,
793 .wMaxPacketSize = __constant_cpu_to_le16 (512),
796 static struct usb_qualifier_descriptor
798 .bLength = sizeof dev_qualifier,
799 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
801 .bcdUSB = __constant_cpu_to_le16 (0x0200),
802 .bDeviceClass = USB_CLASS_COMM,
804 .bNumConfigurations = 1,
807 static const struct usb_descriptor_header *hs_eth_function [11] = {
808 (struct usb_descriptor_header *) &otg_descriptor,
809 #ifdef DEV_CONFIG_CDC
810 /* "cdc" mode descriptors */
811 (struct usb_descriptor_header *) &control_intf,
812 (struct usb_descriptor_header *) &header_desc,
813 (struct usb_descriptor_header *) &union_desc,
814 (struct usb_descriptor_header *) ðer_desc,
815 /* NOTE: status endpoint may need to be removed */
816 (struct usb_descriptor_header *) &hs_status_desc,
817 /* data interface, with altsetting */
818 (struct usb_descriptor_header *) &data_nop_intf,
819 (struct usb_descriptor_header *) &data_intf,
820 (struct usb_descriptor_header *) &hs_source_desc,
821 (struct usb_descriptor_header *) &hs_sink_desc,
823 #endif /* DEV_CONFIG_CDC */
826 static inline void __init hs_subset_descriptors(void)
828 #ifdef DEV_CONFIG_SUBSET
829 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
830 hs_eth_function[2] = (struct usb_descriptor_header *) &fs_source_desc;
831 hs_eth_function[3] = (struct usb_descriptor_header *) &fs_sink_desc;
832 hs_eth_function[4] = NULL;
834 hs_eth_function[1] = NULL;
838 #ifdef CONFIG_USB_ETH_RNDIS
839 static const struct usb_descriptor_header *hs_rndis_function [] = {
840 (struct usb_descriptor_header *) &otg_descriptor,
841 /* control interface matches ACM, not Ethernet */
842 (struct usb_descriptor_header *) &rndis_control_intf,
843 (struct usb_descriptor_header *) &header_desc,
844 (struct usb_descriptor_header *) &call_mgmt_descriptor,
845 (struct usb_descriptor_header *) &acm_descriptor,
846 (struct usb_descriptor_header *) &union_desc,
847 (struct usb_descriptor_header *) &hs_status_desc,
848 /* data interface has no altsetting */
849 (struct usb_descriptor_header *) &rndis_data_intf,
850 (struct usb_descriptor_header *) &hs_source_desc,
851 (struct usb_descriptor_header *) &hs_sink_desc,
857 /* maxpacket and other transfer characteristics vary by speed. */
858 #define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
862 /* if there's no high speed support, maxpacket doesn't change. */
863 #define ep_desc(g,hs,fs) (((void)(g)), (fs))
865 static inline void __init hs_subset_descriptors(void)
869 #endif /* !CONFIG_USB_GADGET_DUALSPEED */
871 /*-------------------------------------------------------------------------*/
873 /* descriptors that are built on-demand */
875 static char manufacturer [50];
876 static char product_desc [40] = DRIVER_DESC;
877 static char serial_number [20];
879 #ifdef DEV_CONFIG_CDC
880 /* address that the host will use ... usually assigned at random */
881 static char ethaddr [2 * ETH_ALEN + 1];
884 /* static strings, in UTF-8 */
885 static struct usb_string strings [] = {
886 { STRING_MANUFACTURER, manufacturer, },
887 { STRING_PRODUCT, product_desc, },
888 { STRING_SERIALNUMBER, serial_number, },
889 { STRING_DATA, "Ethernet Data", },
890 #ifdef DEV_CONFIG_CDC
891 { STRING_CDC, "CDC Ethernet", },
892 { STRING_ETHADDR, ethaddr, },
893 { STRING_CONTROL, "CDC Communications Control", },
895 #ifdef DEV_CONFIG_SUBSET
896 { STRING_SUBSET, "CDC Ethernet Subset", },
898 #ifdef CONFIG_USB_ETH_RNDIS
899 { STRING_RNDIS, "RNDIS", },
900 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
902 { } /* end of list */
905 static struct usb_gadget_strings stringtab = {
906 .language = 0x0409, /* en-us */
911 * one config, two interfaces: control, data.
912 * complications: class descriptors, and an altsetting.
915 config_buf (enum usb_device_speed speed,
917 unsigned index, int is_otg)
920 const struct usb_config_descriptor *config;
921 const struct usb_descriptor_header **function;
922 #ifdef CONFIG_USB_GADGET_DUALSPEED
923 int hs = (speed == USB_SPEED_HIGH);
925 if (type == USB_DT_OTHER_SPEED_CONFIG)
927 #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
929 #define which_fn(t) (fs_ ## t ## _function)
932 if (index >= device_desc.bNumConfigurations)
935 #ifdef CONFIG_USB_ETH_RNDIS
936 /* list the RNDIS config first, to make Microsoft's drivers
937 * happy. DOCSIS 1.0 needs this too.
939 if (device_desc.bNumConfigurations == 2 && index == 0) {
940 config = &rndis_config;
941 function = which_fn (rndis);
945 config = ð_config;
946 function = which_fn (eth);
949 /* for now, don't advertise srp-only devices */
953 len = usb_gadget_config_buf (config, buf, USB_BUFSIZ, function);
956 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
960 /*-------------------------------------------------------------------------*/
962 static void eth_start (struct eth_dev *dev, gfp_t gfp_flags);
963 static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
966 set_ether_config (struct eth_dev *dev, gfp_t gfp_flags)
969 struct usb_gadget *gadget = dev->gadget;
971 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
972 /* status endpoint used for RNDIS and (optionally) CDC */
973 if (!subset_active(dev) && dev->status_ep) {
974 dev->status = ep_desc (gadget, &hs_status_desc,
976 dev->status_ep->driver_data = dev;
978 result = usb_ep_enable (dev->status_ep, dev->status);
980 DEBUG (dev, "enable %s --> %d\n",
981 dev->status_ep->name, result);
987 dev->in = ep_desc (dev->gadget, &hs_source_desc, &fs_source_desc);
988 dev->in_ep->driver_data = dev;
990 dev->out = ep_desc (dev->gadget, &hs_sink_desc, &fs_sink_desc);
991 dev->out_ep->driver_data = dev;
993 /* With CDC, the host isn't allowed to use these two data
994 * endpoints in the default altsetting for the interface.
995 * so we don't activate them yet. Reset from SET_INTERFACE.
997 * Strictly speaking RNDIS should work the same: activation is
998 * a side effect of setting a packet filter. Deactivation is
999 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
1001 if (!cdc_active(dev)) {
1002 result = usb_ep_enable (dev->in_ep, dev->in);
1004 DEBUG(dev, "enable %s --> %d\n",
1005 dev->in_ep->name, result);
1009 result = usb_ep_enable (dev->out_ep, dev->out);
1011 DEBUG (dev, "enable %s --> %d\n",
1012 dev->out_ep->name, result);
1019 result = alloc_requests (dev, qlen (gadget), gfp_flags);
1021 /* on error, disable any endpoints */
1023 if (!subset_active(dev))
1024 (void) usb_ep_disable (dev->status_ep);
1026 (void) usb_ep_disable (dev->in_ep);
1027 (void) usb_ep_disable (dev->out_ep);
1032 /* activate non-CDC configs right away
1033 * this isn't strictly according to the RNDIS spec
1035 if (!cdc_active (dev)) {
1036 netif_carrier_on (dev->net);
1037 if (netif_running (dev->net)) {
1038 spin_unlock (&dev->lock);
1039 eth_start (dev, GFP_ATOMIC);
1040 spin_lock (&dev->lock);
1045 DEBUG (dev, "qlen %d\n", qlen (gadget));
1047 /* caller is responsible for cleanup on error */
1051 static void eth_reset_config (struct eth_dev *dev)
1053 struct usb_request *req;
1055 if (dev->config == 0)
1058 DEBUG (dev, "%s\n", __FUNCTION__);
1060 netif_stop_queue (dev->net);
1061 netif_carrier_off (dev->net);
1062 rndis_uninit(dev->rndis_config);
1064 /* disable endpoints, forcing (synchronous) completion of
1065 * pending i/o. then free the requests.
1068 usb_ep_disable (dev->in_ep);
1069 while (likely (!list_empty (&dev->tx_reqs))) {
1070 req = container_of (dev->tx_reqs.next,
1071 struct usb_request, list);
1072 list_del (&req->list);
1073 usb_ep_free_request (dev->in_ep, req);
1077 usb_ep_disable (dev->out_ep);
1078 while (likely (!list_empty (&dev->rx_reqs))) {
1079 req = container_of (dev->rx_reqs.next,
1080 struct usb_request, list);
1081 list_del (&req->list);
1082 usb_ep_free_request (dev->out_ep, req);
1087 usb_ep_disable (dev->status_ep);
1090 dev->cdc_filter = 0;
1094 /* change our operational config. must agree with the code
1095 * that returns config descriptors, and altsetting code.
1098 eth_set_config (struct eth_dev *dev, unsigned number, gfp_t gfp_flags)
1101 struct usb_gadget *gadget = dev->gadget;
1103 if (gadget_is_sa1100 (gadget)
1105 && atomic_read (&dev->tx_qlen) != 0) {
1106 /* tx fifo is full, but we can't clear it...*/
1107 INFO (dev, "can't change configurations\n");
1110 eth_reset_config (dev);
1113 case DEV_CONFIG_VALUE:
1114 result = set_ether_config (dev, gfp_flags);
1116 #ifdef CONFIG_USB_ETH_RNDIS
1117 case DEV_RNDIS_CONFIG_VALUE:
1119 result = set_ether_config (dev, gfp_flags);
1131 eth_reset_config (dev);
1132 usb_gadget_vbus_draw(dev->gadget,
1133 dev->gadget->is_otg ? 8 : 100);
1138 power = 2 * eth_config.bMaxPower;
1139 usb_gadget_vbus_draw(dev->gadget, power);
1141 switch (gadget->speed) {
1142 case USB_SPEED_FULL: speed = "full"; break;
1143 #ifdef CONFIG_USB_GADGET_DUALSPEED
1144 case USB_SPEED_HIGH: speed = "high"; break;
1146 default: speed = "?"; break;
1149 dev->config = number;
1150 INFO (dev, "%s speed config #%d: %d mA, %s, using %s\n",
1151 speed, number, power, driver_desc,
1156 : "CDC Ethernet Subset"));
1161 /*-------------------------------------------------------------------------*/
1163 #ifdef DEV_CONFIG_CDC
1165 /* The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
1166 * only to notify the host about link status changes (which we support) or
1167 * report completion of some encapsulated command (as used in RNDIS). Since
1168 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1169 * command mechanism; and only one status request is ever queued.
1172 static void eth_status_complete (struct usb_ep *ep, struct usb_request *req)
1174 struct usb_cdc_notification *event = req->buf;
1175 int value = req->status;
1176 struct eth_dev *dev = ep->driver_data;
1178 /* issue the second notification if host reads the first */
1179 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1181 __le32 *data = req->buf + sizeof *event;
1183 event->bmRequestType = 0xA1;
1184 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
1185 event->wValue = __constant_cpu_to_le16 (0);
1186 event->wIndex = __constant_cpu_to_le16 (1);
1187 event->wLength = __constant_cpu_to_le16 (8);
1189 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1190 data [0] = data [1] = cpu_to_le32 (BITRATE (dev->gadget));
1192 req->length = STATUS_BYTECOUNT;
1193 value = usb_ep_queue (ep, req, GFP_ATOMIC);
1194 DEBUG (dev, "send SPEED_CHANGE --> %d\n", value);
1197 } else if (value != -ECONNRESET)
1198 DEBUG (dev, "event %02x --> %d\n",
1199 event->bNotificationType, value);
1200 req->context = NULL;
1203 static void issue_start_status (struct eth_dev *dev)
1205 struct usb_request *req = dev->stat_req;
1206 struct usb_cdc_notification *event;
1209 DEBUG (dev, "%s, flush old status first\n", __FUNCTION__);
1213 * FIXME ugly idiom, maybe we'd be better with just
1214 * a "cancel the whole queue" primitive since any
1215 * unlink-one primitive has way too many error modes.
1216 * here, we "know" toggle is already clear...
1218 * FIXME iff req->context != null just dequeue it
1220 usb_ep_disable (dev->status_ep);
1221 usb_ep_enable (dev->status_ep, dev->status);
1223 /* 3.8.1 says to issue first NETWORK_CONNECTION, then
1224 * a SPEED_CHANGE. could be useful in some configs.
1227 event->bmRequestType = 0xA1;
1228 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
1229 event->wValue = __constant_cpu_to_le16 (1); /* connected */
1230 event->wIndex = __constant_cpu_to_le16 (1);
1233 req->length = sizeof *event;
1234 req->complete = eth_status_complete;
1237 value = usb_ep_queue (dev->status_ep, req, GFP_ATOMIC);
1239 DEBUG (dev, "status buf queue --> %d\n", value);
1244 /*-------------------------------------------------------------------------*/
1246 static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req)
1248 if (req->status || req->actual != req->length)
1249 DEBUG ((struct eth_dev *) ep->driver_data,
1250 "setup complete --> %d, %d/%d\n",
1251 req->status, req->actual, req->length);
1254 #ifdef CONFIG_USB_ETH_RNDIS
1256 static void rndis_response_complete (struct usb_ep *ep, struct usb_request *req)
1258 if (req->status || req->actual != req->length)
1259 DEBUG ((struct eth_dev *) ep->driver_data,
1260 "rndis response complete --> %d, %d/%d\n",
1261 req->status, req->actual, req->length);
1263 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1266 static void rndis_command_complete (struct usb_ep *ep, struct usb_request *req)
1268 struct eth_dev *dev = ep->driver_data;
1271 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1272 spin_lock(&dev->lock);
1273 status = rndis_msg_parser (dev->rndis_config, (u8 *) req->buf);
1275 ERROR(dev, "%s: rndis parse error %d\n", __FUNCTION__, status);
1276 spin_unlock(&dev->lock);
1282 * The setup() callback implements all the ep0 functionality that's not
1283 * handled lower down. CDC has a number of less-common features:
1285 * - two interfaces: control, and ethernet data
1286 * - Ethernet data interface has two altsettings: default, and active
1287 * - class-specific descriptors for the control interface
1288 * - class-specific control requests
1291 eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1293 struct eth_dev *dev = get_gadget_data (gadget);
1294 struct usb_request *req = dev->req;
1295 int value = -EOPNOTSUPP;
1296 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1297 u16 wValue = le16_to_cpu(ctrl->wValue);
1298 u16 wLength = le16_to_cpu(ctrl->wLength);
1300 /* descriptors just go into the pre-allocated ep0 buffer,
1301 * while config change events may enable network traffic.
1303 req->complete = eth_setup_complete;
1304 switch (ctrl->bRequest) {
1306 case USB_REQ_GET_DESCRIPTOR:
1307 if (ctrl->bRequestType != USB_DIR_IN)
1309 switch (wValue >> 8) {
1312 value = min (wLength, (u16) sizeof device_desc);
1313 memcpy (req->buf, &device_desc, value);
1315 #ifdef CONFIG_USB_GADGET_DUALSPEED
1316 case USB_DT_DEVICE_QUALIFIER:
1317 if (!gadget->is_dualspeed)
1319 value = min (wLength, (u16) sizeof dev_qualifier);
1320 memcpy (req->buf, &dev_qualifier, value);
1323 case USB_DT_OTHER_SPEED_CONFIG:
1324 if (!gadget->is_dualspeed)
1327 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1329 value = config_buf (gadget->speed, req->buf,
1334 value = min (wLength, (u16) value);
1338 value = usb_gadget_get_string (&stringtab,
1339 wValue & 0xff, req->buf);
1341 value = min (wLength, (u16) value);
1346 case USB_REQ_SET_CONFIGURATION:
1347 if (ctrl->bRequestType != 0)
1349 if (gadget->a_hnp_support)
1350 DEBUG (dev, "HNP available\n");
1351 else if (gadget->a_alt_hnp_support)
1352 DEBUG (dev, "HNP needs a different root port\n");
1353 spin_lock (&dev->lock);
1354 value = eth_set_config (dev, wValue, GFP_ATOMIC);
1355 spin_unlock (&dev->lock);
1357 case USB_REQ_GET_CONFIGURATION:
1358 if (ctrl->bRequestType != USB_DIR_IN)
1360 *(u8 *)req->buf = dev->config;
1361 value = min (wLength, (u16) 1);
1364 case USB_REQ_SET_INTERFACE:
1365 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1369 if (!cdc_active(dev) && wIndex != 0)
1371 spin_lock (&dev->lock);
1373 /* PXA hardware partially handles SET_INTERFACE;
1374 * we need to kluge around that interference.
1376 if (gadget_is_pxa (gadget)) {
1377 value = eth_set_config (dev, DEV_CONFIG_VALUE,
1382 #ifdef DEV_CONFIG_CDC
1384 case 0: /* control/master intf */
1388 usb_ep_disable (dev->status_ep);
1389 usb_ep_enable (dev->status_ep, dev->status);
1393 case 1: /* data intf */
1396 usb_ep_disable (dev->in_ep);
1397 usb_ep_disable (dev->out_ep);
1399 /* CDC requires the data transfers not be done from
1400 * the default interface setting ... also, setting
1401 * the non-default interface resets filters etc.
1404 if (!cdc_active (dev))
1406 usb_ep_enable (dev->in_ep, dev->in);
1407 usb_ep_enable (dev->out_ep, dev->out);
1408 dev->cdc_filter = DEFAULT_FILTER;
1409 netif_carrier_on (dev->net);
1411 issue_start_status (dev);
1412 if (netif_running (dev->net)) {
1413 spin_unlock (&dev->lock);
1414 eth_start (dev, GFP_ATOMIC);
1415 spin_lock (&dev->lock);
1418 netif_stop_queue (dev->net);
1419 netif_carrier_off (dev->net);
1425 /* FIXME this is wrong, as is the assumption that
1426 * all non-PXA hardware talks real CDC ...
1428 dev_warn (&gadget->dev, "set_interface ignored!\n");
1429 #endif /* DEV_CONFIG_CDC */
1432 spin_unlock (&dev->lock);
1434 case USB_REQ_GET_INTERFACE:
1435 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1439 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
1442 /* for CDC, iff carrier is on, data interface is active. */
1443 if (rndis_active(dev) || wIndex != 1)
1444 *(u8 *)req->buf = 0;
1446 *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0;
1447 value = min (wLength, (u16) 1);
1450 #ifdef DEV_CONFIG_CDC
1451 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
1452 /* see 6.2.30: no data, wIndex = interface,
1453 * wValue = packet filter bitmap
1455 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1460 DEBUG (dev, "packet filter %02x\n", wValue);
1461 dev->cdc_filter = wValue;
1466 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1467 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1468 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1469 * case USB_CDC_GET_ETHERNET_STATISTIC:
1472 #endif /* DEV_CONFIG_CDC */
1474 #ifdef CONFIG_USB_ETH_RNDIS
1475 /* RNDIS uses the CDC command encapsulation mechanism to implement
1476 * an RPC scheme, with much getting/setting of attributes by OID.
1478 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1479 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1480 || !rndis_active(dev)
1481 || wLength > USB_BUFSIZ
1483 || rndis_control_intf.bInterfaceNumber
1486 /* read the request, then process it */
1488 req->complete = rndis_command_complete;
1489 /* later, rndis_control_ack () sends a notification */
1492 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1493 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1494 == ctrl->bRequestType
1495 && rndis_active(dev)
1496 // && wLength >= 0x0400
1498 && rndis_control_intf.bInterfaceNumber
1502 /* return the result */
1503 buf = rndis_get_next_response (dev->rndis_config,
1506 memcpy (req->buf, buf, value);
1507 req->complete = rndis_response_complete;
1508 rndis_free_response(dev->rndis_config, buf);
1510 /* else stalls ... spec says to avoid that */
1517 "unknown control req%02x.%02x v%04x i%04x l%d\n",
1518 ctrl->bRequestType, ctrl->bRequest,
1519 wValue, wIndex, wLength);
1522 /* respond with data transfer before status phase? */
1524 req->length = value;
1525 req->zero = value < wLength
1526 && (value % gadget->ep0->maxpacket) == 0;
1527 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1529 DEBUG (dev, "ep_queue --> %d\n", value);
1531 eth_setup_complete (gadget->ep0, req);
1535 /* host either stalls (value < 0) or reports success */
1540 eth_disconnect (struct usb_gadget *gadget)
1542 struct eth_dev *dev = get_gadget_data (gadget);
1543 unsigned long flags;
1545 spin_lock_irqsave (&dev->lock, flags);
1546 netif_stop_queue (dev->net);
1547 netif_carrier_off (dev->net);
1548 eth_reset_config (dev);
1549 spin_unlock_irqrestore (&dev->lock, flags);
1551 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1553 /* next we may get setup() calls to enumerate new connections;
1554 * or an unbind() during shutdown (including removing module).
1558 /*-------------------------------------------------------------------------*/
1560 /* NETWORK DRIVER HOOKUP (to the layer above this driver) */
1562 static int eth_change_mtu (struct net_device *net, int new_mtu)
1564 struct eth_dev *dev = netdev_priv(net);
1569 if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN)
1571 /* no zero-length packet read wanted after mtu-sized packets */
1572 if (((new_mtu + sizeof (struct ethhdr)) % dev->in_ep->maxpacket) == 0)
1578 static struct net_device_stats *eth_get_stats (struct net_device *net)
1580 return &((struct eth_dev *)netdev_priv(net))->stats;
1583 static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
1585 struct eth_dev *dev = netdev_priv(net);
1586 strlcpy(p->driver, shortname, sizeof p->driver);
1587 strlcpy(p->version, DRIVER_VERSION, sizeof p->version);
1588 strlcpy(p->fw_version, dev->gadget->name, sizeof p->fw_version);
1589 strlcpy (p->bus_info, dev->gadget->dev.bus_id, sizeof p->bus_info);
1592 static u32 eth_get_link(struct net_device *net)
1594 struct eth_dev *dev = netdev_priv(net);
1595 return dev->gadget->speed != USB_SPEED_UNKNOWN;
1598 static struct ethtool_ops ops = {
1599 .get_drvinfo = eth_get_drvinfo,
1600 .get_link = eth_get_link
1603 static void defer_kevent (struct eth_dev *dev, int flag)
1605 if (test_and_set_bit (flag, &dev->todo))
1607 if (!schedule_work (&dev->work))
1608 ERROR (dev, "kevent %d may have been dropped\n", flag);
1610 DEBUG (dev, "kevent %d scheduled\n", flag);
1613 static void rx_complete (struct usb_ep *ep, struct usb_request *req);
1616 rx_submit (struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
1618 struct sk_buff *skb;
1619 int retval = -ENOMEM;
1622 /* Padding up to RX_EXTRA handles minor disagreements with host.
1623 * Normally we use the USB "terminate on short read" convention;
1624 * so allow up to (N*maxpacket), since that memory is normally
1625 * already allocated. Some hardware doesn't deal well with short
1626 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1627 * byte off the end (to force hardware errors on overflow).
1629 * RNDIS uses internal framing, and explicitly allows senders to
1630 * pad to end-of-packet. That's potentially nice for speed,
1631 * but means receivers can't recover synch on their own.
1633 size = (sizeof (struct ethhdr) + dev->net->mtu + RX_EXTRA);
1634 size += dev->out_ep->maxpacket - 1;
1635 if (rndis_active(dev))
1636 size += sizeof (struct rndis_packet_msg_type);
1637 size -= size % dev->out_ep->maxpacket;
1639 if ((skb = alloc_skb (size + NET_IP_ALIGN, gfp_flags)) == 0) {
1640 DEBUG (dev, "no rx skb\n");
1644 /* Some platforms perform better when IP packets are aligned,
1645 * but on at least one, checksumming fails otherwise. Note:
1646 * RNDIS headers involve variable numbers of LE32 values.
1648 skb_reserve(skb, NET_IP_ALIGN);
1650 req->buf = skb->data;
1652 req->complete = rx_complete;
1655 retval = usb_ep_queue (dev->out_ep, req, gfp_flags);
1656 if (retval == -ENOMEM)
1658 defer_kevent (dev, WORK_RX_MEMORY);
1660 DEBUG (dev, "rx submit --> %d\n", retval);
1661 dev_kfree_skb_any (skb);
1662 spin_lock (&dev->lock);
1663 list_add (&req->list, &dev->rx_reqs);
1664 spin_unlock (&dev->lock);
1669 static void rx_complete (struct usb_ep *ep, struct usb_request *req)
1671 struct sk_buff *skb = req->context;
1672 struct eth_dev *dev = ep->driver_data;
1673 int status = req->status;
1677 /* normal completion */
1679 skb_put (skb, req->actual);
1680 /* we know MaxPacketsPerTransfer == 1 here */
1681 if (rndis_active(dev))
1682 status = rndis_rm_hdr (skb);
1684 || ETH_HLEN > skb->len
1685 || skb->len > ETH_FRAME_LEN) {
1686 dev->stats.rx_errors++;
1687 dev->stats.rx_length_errors++;
1688 DEBUG (dev, "rx length %d\n", skb->len);
1692 skb->dev = dev->net;
1693 skb->protocol = eth_type_trans (skb, dev->net);
1694 dev->stats.rx_packets++;
1695 dev->stats.rx_bytes += skb->len;
1697 /* no buffer copies needed, unless hardware can't
1700 status = netif_rx (skb);
1704 /* software-driven interface shutdown */
1705 case -ECONNRESET: // unlink
1706 case -ESHUTDOWN: // disconnect etc
1707 VDEBUG (dev, "rx shutdown, code %d\n", status);
1710 /* for hardware automagic (such as pxa) */
1711 case -ECONNABORTED: // endpoint reset
1712 DEBUG (dev, "rx %s reset\n", ep->name);
1713 defer_kevent (dev, WORK_RX_MEMORY);
1715 dev_kfree_skb_any (skb);
1720 dev->stats.rx_over_errors++;
1724 dev->stats.rx_errors++;
1725 DEBUG (dev, "rx status %d\n", status);
1730 dev_kfree_skb_any (skb);
1731 if (!netif_running (dev->net)) {
1733 /* nobody reading rx_reqs, so no dev->lock */
1734 list_add (&req->list, &dev->rx_reqs);
1738 rx_submit (dev, req, GFP_ATOMIC);
1741 static int prealloc (struct list_head *list, struct usb_ep *ep,
1742 unsigned n, gfp_t gfp_flags)
1745 struct usb_request *req;
1750 /* queue/recycle up to N requests */
1752 list_for_each_entry (req, list, list) {
1757 req = usb_ep_alloc_request (ep, gfp_flags);
1759 return list_empty (list) ? -ENOMEM : 0;
1760 list_add (&req->list, list);
1767 struct list_head *next;
1769 next = req->list.next;
1770 list_del (&req->list);
1771 usb_ep_free_request (ep, req);
1776 req = container_of (next, struct usb_request, list);
1781 static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
1785 status = prealloc (&dev->tx_reqs, dev->in_ep, n, gfp_flags);
1788 status = prealloc (&dev->rx_reqs, dev->out_ep, n, gfp_flags);
1793 DEBUG (dev, "can't alloc requests\n");
1797 static void rx_fill (struct eth_dev *dev, gfp_t gfp_flags)
1799 struct usb_request *req;
1800 unsigned long flags;
1802 /* fill unused rxq slots with some skb */
1803 spin_lock_irqsave (&dev->lock, flags);
1804 while (!list_empty (&dev->rx_reqs)) {
1805 req = container_of (dev->rx_reqs.next,
1806 struct usb_request, list);
1807 list_del_init (&req->list);
1808 spin_unlock_irqrestore (&dev->lock, flags);
1810 if (rx_submit (dev, req, gfp_flags) < 0) {
1811 defer_kevent (dev, WORK_RX_MEMORY);
1815 spin_lock_irqsave (&dev->lock, flags);
1817 spin_unlock_irqrestore (&dev->lock, flags);
1820 static void eth_work (void *_dev)
1822 struct eth_dev *dev = _dev;
1824 if (test_and_clear_bit (WORK_RX_MEMORY, &dev->todo)) {
1825 if (netif_running (dev->net))
1826 rx_fill (dev, GFP_KERNEL);
1830 DEBUG (dev, "work done, flags = 0x%lx\n", dev->todo);
1833 static void tx_complete (struct usb_ep *ep, struct usb_request *req)
1835 struct sk_buff *skb = req->context;
1836 struct eth_dev *dev = ep->driver_data;
1838 switch (req->status) {
1840 dev->stats.tx_errors++;
1841 VDEBUG (dev, "tx err %d\n", req->status);
1843 case -ECONNRESET: // unlink
1844 case -ESHUTDOWN: // disconnect etc
1847 dev->stats.tx_bytes += skb->len;
1849 dev->stats.tx_packets++;
1851 spin_lock (&dev->lock);
1852 list_add (&req->list, &dev->tx_reqs);
1853 spin_unlock (&dev->lock);
1854 dev_kfree_skb_any (skb);
1856 atomic_dec (&dev->tx_qlen);
1857 if (netif_carrier_ok (dev->net))
1858 netif_wake_queue (dev->net);
1861 static inline int eth_is_promisc (struct eth_dev *dev)
1863 /* no filters for the CDC subset; always promisc */
1864 if (subset_active (dev))
1866 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1869 static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1871 struct eth_dev *dev = netdev_priv(net);
1872 int length = skb->len;
1874 struct usb_request *req = NULL;
1875 unsigned long flags;
1877 /* apply outgoing CDC or RNDIS filters */
1878 if (!eth_is_promisc (dev)) {
1879 u8 *dest = skb->data;
1881 if (dest [0] & 0x01) {
1884 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1885 * SET_ETHERNET_MULTICAST_FILTERS requests
1887 if (memcmp (dest, net->broadcast, ETH_ALEN) == 0)
1888 type = USB_CDC_PACKET_TYPE_BROADCAST;
1890 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1891 if (!(dev->cdc_filter & type)) {
1892 dev_kfree_skb_any (skb);
1896 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1899 spin_lock_irqsave (&dev->lock, flags);
1900 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1901 list_del (&req->list);
1902 if (list_empty (&dev->tx_reqs))
1903 netif_stop_queue (net);
1904 spin_unlock_irqrestore (&dev->lock, flags);
1906 /* no buffer copies needed, unless the network stack did it
1907 * or the hardware can't use skb buffers.
1908 * or there's not enough space for any RNDIS headers we need
1910 if (rndis_active(dev)) {
1911 struct sk_buff *skb_rndis;
1913 skb_rndis = skb_realloc_headroom (skb,
1914 sizeof (struct rndis_packet_msg_type));
1918 dev_kfree_skb_any (skb);
1920 rndis_add_hdr (skb);
1923 req->buf = skb->data;
1925 req->complete = tx_complete;
1927 /* use zlp framing on tx for strict CDC-Ether conformance,
1928 * though any robust network rx path ignores extra padding.
1929 * and some hardware doesn't like to write zlps.
1932 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1935 req->length = length;
1937 #ifdef CONFIG_USB_GADGET_DUALSPEED
1938 /* throttle highspeed IRQ rate back slightly */
1939 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1940 ? ((atomic_read (&dev->tx_qlen) % TX_DELAY) != 0)
1944 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1947 DEBUG (dev, "tx queue err %d\n", retval);
1950 net->trans_start = jiffies;
1951 atomic_inc (&dev->tx_qlen);
1956 dev->stats.tx_dropped++;
1957 dev_kfree_skb_any (skb);
1958 spin_lock_irqsave (&dev->lock, flags);
1959 if (list_empty (&dev->tx_reqs))
1960 netif_start_queue (net);
1961 list_add (&req->list, &dev->tx_reqs);
1962 spin_unlock_irqrestore (&dev->lock, flags);
1967 /*-------------------------------------------------------------------------*/
1969 #ifdef CONFIG_USB_ETH_RNDIS
1971 /* The interrupt endpoint is used in RNDIS to notify the host when messages
1972 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1973 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1974 * REMOTE_NDIS_KEEPALIVE_MSG.
1976 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1977 * normally just one notification will be queued.
1980 static struct usb_request *eth_req_alloc (struct usb_ep *, unsigned, gfp_t);
1981 static void eth_req_free (struct usb_ep *ep, struct usb_request *req);
1984 rndis_control_ack_complete (struct usb_ep *ep, struct usb_request *req)
1986 struct eth_dev *dev = ep->driver_data;
1988 if (req->status || req->actual != req->length)
1990 "rndis control ack complete --> %d, %d/%d\n",
1991 req->status, req->actual, req->length);
1992 req->context = NULL;
1994 if (req != dev->stat_req)
1995 eth_req_free(ep, req);
1998 static int rndis_control_ack (struct net_device *net)
2000 struct eth_dev *dev = netdev_priv(net);
2002 struct usb_request *resp = dev->stat_req;
2004 /* in case RNDIS calls this after disconnect */
2006 DEBUG (dev, "status ENODEV\n");
2010 /* in case queue length > 1 */
2011 if (resp->context) {
2012 resp = eth_req_alloc (dev->status_ep, 8, GFP_ATOMIC);
2017 /* Send RNDIS RESPONSE_AVAILABLE notification;
2018 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
2021 resp->complete = rndis_control_ack_complete;
2022 resp->context = dev;
2024 *((__le32 *) resp->buf) = __constant_cpu_to_le32 (1);
2025 *((__le32 *) resp->buf + 1) = __constant_cpu_to_le32 (0);
2027 length = usb_ep_queue (dev->status_ep, resp, GFP_ATOMIC);
2030 rndis_control_ack_complete (dev->status_ep, resp);
2038 #define rndis_control_ack NULL
2042 static void eth_start (struct eth_dev *dev, gfp_t gfp_flags)
2044 DEBUG (dev, "%s\n", __FUNCTION__);
2046 /* fill the rx queue */
2047 rx_fill (dev, gfp_flags);
2049 /* and open the tx floodgates */
2050 atomic_set (&dev->tx_qlen, 0);
2051 netif_wake_queue (dev->net);
2052 if (rndis_active(dev)) {
2053 rndis_set_param_medium (dev->rndis_config,
2055 BITRATE(dev->gadget)/100);
2056 (void) rndis_signal_connect (dev->rndis_config);
2060 static int eth_open (struct net_device *net)
2062 struct eth_dev *dev = netdev_priv(net);
2064 DEBUG (dev, "%s\n", __FUNCTION__);
2065 if (netif_carrier_ok (dev->net))
2066 eth_start (dev, GFP_KERNEL);
2070 static int eth_stop (struct net_device *net)
2072 struct eth_dev *dev = netdev_priv(net);
2074 VDEBUG (dev, "%s\n", __FUNCTION__);
2075 netif_stop_queue (net);
2077 DEBUG (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
2078 dev->stats.rx_packets, dev->stats.tx_packets,
2079 dev->stats.rx_errors, dev->stats.tx_errors
2082 /* ensure there are no more active requests */
2084 usb_ep_disable (dev->in_ep);
2085 usb_ep_disable (dev->out_ep);
2086 if (netif_carrier_ok (dev->net)) {
2087 DEBUG (dev, "host still using in/out endpoints\n");
2088 // FIXME idiom may leave toggle wrong here
2089 usb_ep_enable (dev->in_ep, dev->in);
2090 usb_ep_enable (dev->out_ep, dev->out);
2092 if (dev->status_ep) {
2093 usb_ep_disable (dev->status_ep);
2094 usb_ep_enable (dev->status_ep, dev->status);
2098 if (rndis_active(dev)) {
2099 rndis_set_param_medium (dev->rndis_config,
2100 NDIS_MEDIUM_802_3, 0);
2101 (void) rndis_signal_disconnect (dev->rndis_config);
2107 /*-------------------------------------------------------------------------*/
2109 static struct usb_request *
2110 eth_req_alloc (struct usb_ep *ep, unsigned size, gfp_t gfp_flags)
2112 struct usb_request *req;
2114 req = usb_ep_alloc_request (ep, gfp_flags);
2118 req->buf = kmalloc (size, gfp_flags);
2120 usb_ep_free_request (ep, req);
2127 eth_req_free (struct usb_ep *ep, struct usb_request *req)
2130 usb_ep_free_request (ep, req);
2134 static void /* __init_or_exit */
2135 eth_unbind (struct usb_gadget *gadget)
2137 struct eth_dev *dev = get_gadget_data (gadget);
2139 DEBUG (dev, "unbind\n");
2140 rndis_deregister (dev->rndis_config);
2143 /* we've already been disconnected ... no i/o is active */
2145 eth_req_free (gadget->ep0, dev->req);
2148 if (dev->stat_req) {
2149 eth_req_free (dev->status_ep, dev->stat_req);
2150 dev->stat_req = NULL;
2153 unregister_netdev (dev->net);
2154 free_netdev(dev->net);
2156 /* assuming we used keventd, it must quiesce too */
2157 flush_scheduled_work ();
2158 set_gadget_data (gadget, NULL);
2161 static u8 __devinit nibble (unsigned char c)
2163 if (likely (isdigit (c)))
2166 if (likely (isxdigit (c)))
2167 return 10 + c - 'A';
2171 static int __devinit get_ether_addr(const char *str, u8 *dev_addr)
2176 for (i = 0; i < 6; i++) {
2179 if((*str == '.') || (*str == ':'))
2181 num = nibble(*str++) << 4;
2182 num |= (nibble(*str++));
2185 if (is_valid_ether_addr (dev_addr))
2188 random_ether_addr(dev_addr);
2192 static int __devinit
2193 eth_bind (struct usb_gadget *gadget)
2195 struct eth_dev *dev;
2196 struct net_device *net;
2197 u8 cdc = 1, zlp = 1, rndis = 1;
2198 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
2199 int status = -ENOMEM;
2202 /* these flags are only ever cleared; compiler take note */
2203 #ifndef DEV_CONFIG_CDC
2206 #ifndef CONFIG_USB_ETH_RNDIS
2210 /* Because most host side USB stacks handle CDC Ethernet, that
2211 * standard protocol is _strongly_ preferred for interop purposes.
2212 * (By everyone except Microsoft.)
2214 if (gadget_is_pxa (gadget)) {
2215 /* pxa doesn't support altsettings */
2217 } else if (gadget_is_sh(gadget)) {
2218 /* sh doesn't support multiple interfaces or configs */
2221 } else if (gadget_is_sa1100 (gadget)) {
2222 /* hardware can't write zlps */
2224 /* sa1100 CAN do CDC, without status endpoint ... we use
2225 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2230 gcnum = usb_gadget_controller_number (gadget);
2232 device_desc.bcdDevice = cpu_to_le16 (0x0200 + gcnum);
2234 /* can't assume CDC works. don't want to default to
2235 * anything less functional on CDC-capable hardware,
2236 * so we fail in this case.
2238 dev_err (&gadget->dev,
2239 "controller '%s' not recognized\n",
2243 snprintf (manufacturer, sizeof manufacturer, "%s %s/%s",
2244 system_utsname.sysname, system_utsname.release,
2247 /* If there's an RNDIS configuration, that's what Windows wants to
2248 * be using ... so use these product IDs here and in the "linux.inf"
2249 * needed to install MSFT drivers. Current Linux kernels will use
2250 * the second configuration if it's CDC Ethernet, and need some help
2251 * to choose the right configuration otherwise.
2254 device_desc.idVendor =
2255 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2256 device_desc.idProduct =
2257 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2258 snprintf (product_desc, sizeof product_desc,
2259 "RNDIS/%s", driver_desc);
2261 /* CDC subset ... recognized by Linux since 2.4.10, but Windows
2262 * drivers aren't widely available.
2265 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2266 device_desc.idVendor =
2267 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2268 device_desc.idProduct =
2269 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2272 /* support optional vendor/distro customization */
2275 dev_err (&gadget->dev, "idVendor needs idProduct!\n");
2278 device_desc.idVendor = cpu_to_le16(idVendor);
2279 device_desc.idProduct = cpu_to_le16(idProduct);
2281 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2284 strlcpy (manufacturer, iManufacturer, sizeof manufacturer);
2286 strlcpy (product_desc, iProduct, sizeof product_desc);
2287 if (iSerialNumber) {
2288 device_desc.iSerialNumber = STRING_SERIALNUMBER,
2289 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
2292 /* all we really need is bulk IN/OUT */
2293 usb_ep_autoconfig_reset (gadget);
2294 in_ep = usb_ep_autoconfig (gadget, &fs_source_desc);
2297 dev_err (&gadget->dev,
2298 "can't autoconfigure on %s\n",
2302 in_ep->driver_data = in_ep; /* claim */
2304 out_ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
2307 out_ep->driver_data = out_ep; /* claim */
2309 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2310 /* CDC Ethernet control interface doesn't require a status endpoint.
2311 * Since some hosts expect one, try to allocate one anyway.
2314 status_ep = usb_ep_autoconfig (gadget, &fs_status_desc);
2316 status_ep->driver_data = status_ep; /* claim */
2318 dev_err (&gadget->dev,
2319 "can't run RNDIS on %s\n",
2322 #ifdef DEV_CONFIG_CDC
2323 /* pxa25x only does CDC subset; often used with RNDIS */
2325 control_intf.bNumEndpoints = 0;
2326 /* FIXME remove endpoint from descriptor list */
2332 /* one config: cdc, else minimal subset */
2334 eth_config.bNumInterfaces = 1;
2335 eth_config.iConfiguration = STRING_SUBSET;
2336 fs_subset_descriptors();
2337 hs_subset_descriptors();
2340 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
2341 usb_gadget_set_selfpowered (gadget);
2343 /* For now RNDIS is always a second config */
2345 device_desc.bNumConfigurations = 2;
2347 #ifdef CONFIG_USB_GADGET_DUALSPEED
2349 dev_qualifier.bNumConfigurations = 2;
2351 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2353 /* assumes ep0 uses the same value for both speeds ... */
2354 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2356 /* and that all endpoints are dual-speed */
2357 hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
2358 hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
2359 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2361 hs_status_desc.bEndpointAddress =
2362 fs_status_desc.bEndpointAddress;
2364 #endif /* DUALSPEED */
2366 if (gadget->is_otg) {
2367 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2368 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2369 eth_config.bMaxPower = 4;
2370 #ifdef CONFIG_USB_ETH_RNDIS
2371 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2372 rndis_config.bMaxPower = 4;
2376 net = alloc_etherdev (sizeof *dev);
2379 dev = netdev_priv(net);
2380 spin_lock_init (&dev->lock);
2381 INIT_WORK (&dev->work, eth_work, dev);
2382 INIT_LIST_HEAD (&dev->tx_reqs);
2383 INIT_LIST_HEAD (&dev->rx_reqs);
2385 /* network device setup */
2387 SET_MODULE_OWNER (net);
2388 strcpy (net->name, "usb%d");
2393 dev->out_ep = out_ep;
2394 dev->status_ep = status_ep;
2396 /* Module params for these addresses should come from ID proms.
2397 * The host side address is used with CDC and RNDIS, and commonly
2398 * ends up in a persistent config database.
2400 if (get_ether_addr(dev_addr, net->dev_addr))
2401 dev_warn(&gadget->dev,
2402 "using random %s ethernet address\n", "self");
2404 if (get_ether_addr(host_addr, dev->host_mac))
2405 dev_warn(&gadget->dev,
2406 "using random %s ethernet address\n", "host");
2407 #ifdef DEV_CONFIG_CDC
2408 snprintf (ethaddr, sizeof ethaddr, "%02X%02X%02X%02X%02X%02X",
2409 dev->host_mac [0], dev->host_mac [1],
2410 dev->host_mac [2], dev->host_mac [3],
2411 dev->host_mac [4], dev->host_mac [5]);
2416 status = rndis_init();
2418 dev_err (&gadget->dev, "can't init RNDIS, %d\n",
2424 net->change_mtu = eth_change_mtu;
2425 net->get_stats = eth_get_stats;
2426 net->hard_start_xmit = eth_start_xmit;
2427 net->open = eth_open;
2428 net->stop = eth_stop;
2429 // watchdog_timeo, tx_timeout ...
2430 // set_multicast_list
2431 SET_ETHTOOL_OPS(net, &ops);
2433 /* preallocate control message data and buffer */
2434 dev->req = eth_req_alloc (gadget->ep0, USB_BUFSIZ, GFP_KERNEL);
2437 dev->req->complete = eth_setup_complete;
2439 /* ... and maybe likewise for status transfer */
2440 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2441 if (dev->status_ep) {
2442 dev->stat_req = eth_req_alloc (dev->status_ep,
2443 STATUS_BYTECOUNT, GFP_KERNEL);
2444 if (!dev->stat_req) {
2445 eth_req_free (gadget->ep0, dev->req);
2448 dev->stat_req->context = NULL;
2452 /* finish hookup to lower layer ... */
2453 dev->gadget = gadget;
2454 set_gadget_data (gadget, dev);
2455 gadget->ep0->driver_data = dev;
2457 /* two kinds of host-initiated state changes:
2458 * - iff DATA transfer is active, carrier is "on"
2459 * - tx queueing enabled if open *and* carrier is "on"
2461 netif_stop_queue (dev->net);
2462 netif_carrier_off (dev->net);
2464 SET_NETDEV_DEV (dev->net, &gadget->dev);
2465 status = register_netdev (dev->net);
2469 INFO (dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
2470 INFO (dev, "using %s, OUT %s IN %s%s%s\n", gadget->name,
2471 out_ep->name, in_ep->name,
2472 status_ep ? " STATUS " : "",
2473 status_ep ? status_ep->name : ""
2475 INFO (dev, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2476 net->dev_addr [0], net->dev_addr [1],
2477 net->dev_addr [2], net->dev_addr [3],
2478 net->dev_addr [4], net->dev_addr [5]);
2481 INFO (dev, "HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2482 dev->host_mac [0], dev->host_mac [1],
2483 dev->host_mac [2], dev->host_mac [3],
2484 dev->host_mac [4], dev->host_mac [5]);
2489 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2491 dev->rndis_config = rndis_register (rndis_control_ack);
2492 if (dev->rndis_config < 0) {
2494 unregister_netdev (dev->net);
2499 /* these set up a lot of the OIDs that RNDIS needs */
2500 rndis_set_host_mac (dev->rndis_config, dev->host_mac);
2501 if (rndis_set_param_dev (dev->rndis_config, dev->net,
2502 &dev->stats, &dev->cdc_filter))
2504 if (rndis_set_param_vendor (dev->rndis_config, vendorID,
2507 if (rndis_set_param_medium (dev->rndis_config,
2511 INFO (dev, "RNDIS ready\n");
2517 dev_dbg(&gadget->dev, "register_netdev failed, %d\n", status);
2519 eth_unbind (gadget);
2523 /*-------------------------------------------------------------------------*/
2526 eth_suspend (struct usb_gadget *gadget)
2528 struct eth_dev *dev = get_gadget_data (gadget);
2530 DEBUG (dev, "suspend\n");
2535 eth_resume (struct usb_gadget *gadget)
2537 struct eth_dev *dev = get_gadget_data (gadget);
2539 DEBUG (dev, "resume\n");
2543 /*-------------------------------------------------------------------------*/
2545 static struct usb_gadget_driver eth_driver = {
2548 .function = (char *) driver_desc,
2550 .unbind = __exit_p(eth_unbind),
2553 .disconnect = eth_disconnect,
2555 .suspend = eth_suspend,
2556 .resume = eth_resume,
2559 .name = (char *) shortname,
2560 .owner = THIS_MODULE,
2564 MODULE_DESCRIPTION (DRIVER_DESC);
2565 MODULE_AUTHOR ("David Brownell, Benedikt Spanger");
2566 MODULE_LICENSE ("GPL");
2569 static int __init init (void)
2571 return usb_gadget_register_driver (ð_driver);
2575 static void __exit cleanup (void)
2577 usb_gadget_unregister_driver (ð_driver);
2579 module_exit (cleanup);