2 * ether.c -- Ethernet gadget driver, with CDC and non-CDC options
4 * Copyright (C) 2003-2005 David Brownell
5 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <linux/config.h>
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/delay.h>
30 #include <linux/ioport.h>
31 #include <linux/sched.h>
32 #include <linux/slab.h>
33 #include <linux/smp_lock.h>
34 #include <linux/errno.h>
35 #include <linux/init.h>
36 #include <linux/timer.h>
37 #include <linux/list.h>
38 #include <linux/interrupt.h>
39 #include <linux/utsname.h>
40 #include <linux/device.h>
41 #include <linux/moduleparam.h>
42 #include <linux/ctype.h>
44 #include <asm/byteorder.h>
47 #include <asm/system.h>
48 #include <asm/uaccess.h>
49 #include <asm/unaligned.h>
51 #include <linux/usb_ch9.h>
52 #include <linux/usb_cdc.h>
53 #include <linux/usb_gadget.h>
55 #include <linux/random.h>
56 #include <linux/netdevice.h>
57 #include <linux/etherdevice.h>
58 #include <linux/ethtool.h>
60 #include "gadget_chips.h"
62 /*-------------------------------------------------------------------------*/
65 * Ethernet gadget driver -- with CDC and non-CDC options
66 * Builds on hardware support for a full duplex link.
68 * CDC Ethernet is the standard USB solution for sending Ethernet frames
69 * using USB. Real hardware tends to use the same framing protocol but look
70 * different for control features. This driver strongly prefers to use
71 * this USB-IF standard as its open-systems interoperability solution;
72 * most host side USB stacks (except from Microsoft) support it.
74 * There's some hardware that can't talk CDC. We make that hardware
75 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
76 * link-level setup only requires activating the configuration.
77 * Linux supports it, but other host operating systems may not.
78 * (This is a subset of CDC Ethernet.)
80 * A third option is also in use. Rather than CDC Ethernet, or something
81 * simpler, Microsoft pushes their own approach: RNDIS. The published
82 * RNDIS specs are ambiguous and appear to be incomplete, and are also
86 #define DRIVER_DESC "Ethernet Gadget"
87 #define DRIVER_VERSION "May Day 2005"
89 static const char shortname [] = "ether";
90 static const char driver_desc [] = DRIVER_DESC;
92 #define RX_EXTRA 20 /* guard against rx overflows */
96 #ifndef CONFIG_USB_ETH_RNDIS
97 #define rndis_uninit(x) do{}while(0)
98 #define rndis_deregister(c) do{}while(0)
99 #define rndis_exit() do{}while(0)
102 /* CDC and RNDIS support the same host-chosen outgoing packet filters. */
103 #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
104 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
105 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
106 |USB_CDC_PACKET_TYPE_DIRECTED)
109 /*-------------------------------------------------------------------------*/
113 struct usb_gadget *gadget;
114 struct usb_request *req; /* for control responses */
115 struct usb_request *stat_req; /* for cdc & rndis status */
118 struct usb_ep *in_ep, *out_ep, *status_ep;
119 const struct usb_endpoint_descriptor
121 struct list_head tx_reqs, rx_reqs;
123 struct net_device *net;
124 struct net_device_stats stats;
127 struct work_struct work;
131 unsigned suspended:1;
134 #define WORK_RX_MEMORY 0
136 u8 host_mac [ETH_ALEN];
139 /* This version autoconfigures as much as possible at run-time.
141 * It also ASSUMES a self-powered device, without remote wakeup,
142 * although remote wakeup support would make sense.
145 /*-------------------------------------------------------------------------*/
147 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
148 * Instead: allocate your own, using normal USB-IF procedures.
151 /* Thanks to NetChip Technologies for donating this product ID.
152 * It's for devices with only CDC Ethernet configurations.
154 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
155 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
157 /* For hardware that can't talk CDC, we use the same vendor ID that
158 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
159 * with pxa250. We're protocol-compatible, if the host-side drivers
160 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
161 * drivers that need to hard-wire endpoint numbers have a hook.
163 * The protocol is a minimal subset of CDC Ether, which works on any bulk
164 * hardware that's not deeply broken ... even on hardware that can't talk
165 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
166 * doesn't handle control-OUT).
168 #define SIMPLE_VENDOR_NUM 0x049f
169 #define SIMPLE_PRODUCT_NUM 0x505a
171 /* For hardware that can talk RNDIS and either of the above protocols,
172 * use this ID ... the windows INF files will know it. Unless it's
173 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
174 * the non-RNDIS configuration.
176 #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
177 #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
180 /* Some systems will want different product identifers published in the
181 * device descriptor, either numbers or strings or both. These string
182 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
185 static ushort __initdata idVendor;
186 module_param(idVendor, ushort, S_IRUGO);
187 MODULE_PARM_DESC(idVendor, "USB Vendor ID");
189 static ushort __initdata idProduct;
190 module_param(idProduct, ushort, S_IRUGO);
191 MODULE_PARM_DESC(idProduct, "USB Product ID");
193 static ushort __initdata bcdDevice;
194 module_param(bcdDevice, ushort, S_IRUGO);
195 MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
197 static char *__initdata iManufacturer;
198 module_param(iManufacturer, charp, S_IRUGO);
199 MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
201 static char *__initdata iProduct;
202 module_param(iProduct, charp, S_IRUGO);
203 MODULE_PARM_DESC(iProduct, "USB Product string");
205 /* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */
206 static char *__initdata dev_addr;
207 module_param(dev_addr, charp, S_IRUGO);
208 MODULE_PARM_DESC(dev_addr, "Device Ethernet Address");
210 /* this address is invisible to ifconfig */
211 static char *__initdata host_addr;
212 module_param(host_addr, charp, S_IRUGO);
213 MODULE_PARM_DESC(host_addr, "Host Ethernet Address");
216 /*-------------------------------------------------------------------------*/
218 /* Include CDC support if we could run on CDC-capable hardware. */
220 #ifdef CONFIG_USB_GADGET_NET2280
221 #define DEV_CONFIG_CDC
224 #ifdef CONFIG_USB_GADGET_DUMMY_HCD
225 #define DEV_CONFIG_CDC
228 #ifdef CONFIG_USB_GADGET_GOKU
229 #define DEV_CONFIG_CDC
232 #ifdef CONFIG_USB_GADGET_LH7A40X
233 #define DEV_CONFIG_CDC
236 #ifdef CONFIG_USB_GADGET_MQ11XX
237 #define DEV_CONFIG_CDC
240 #ifdef CONFIG_USB_GADGET_OMAP
241 #define DEV_CONFIG_CDC
244 #ifdef CONFIG_USB_GADGET_N9604
245 #define DEV_CONFIG_CDC
248 #ifdef CONFIG_USB_GADGET_PXA27X
249 #define DEV_CONFIG_CDC
252 #ifdef CONFIG_USB_GADGET_AT91
253 #define DEV_CONFIG_CDC
256 #ifdef CONFIG_USB_GADGET_MUSBHSFC
257 #define DEV_CONFIG_CDC
260 #ifdef CONFIG_USB_GADGET_MUSBHDRC
261 #define DEV_CONFIG_CDC
265 /* For CDC-incapable hardware, choose the simple cdc subset.
266 * Anything that talks bulk (without notable bugs) can do this.
268 #ifdef CONFIG_USB_GADGET_PXA2XX
269 #define DEV_CONFIG_SUBSET
272 #ifdef CONFIG_USB_GADGET_SH
273 #define DEV_CONFIG_SUBSET
276 #ifdef CONFIG_USB_GADGET_SA1100
277 /* use non-CDC for backwards compatibility */
278 #define DEV_CONFIG_SUBSET
281 #ifdef CONFIG_USB_GADGET_S3C2410
282 #define DEV_CONFIG_CDC
285 /*-------------------------------------------------------------------------*/
287 /* "main" config is either CDC, or its simple subset */
288 static inline int is_cdc(struct eth_dev *dev)
290 #if !defined(DEV_CONFIG_SUBSET)
291 return 1; /* only cdc possible */
292 #elif !defined (DEV_CONFIG_CDC)
293 return 0; /* only subset possible */
295 return dev->cdc; /* depends on what hardware we found */
299 /* "secondary" RNDIS config may sometimes be activated */
300 static inline int rndis_active(struct eth_dev *dev)
302 #ifdef CONFIG_USB_ETH_RNDIS
309 #define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
310 #define cdc_active(dev) ( is_cdc(dev) && !rndis_active(dev))
314 #define DEFAULT_QLEN 2 /* double buffering by default */
316 /* peak bulk transfer bits-per-second */
317 #define HS_BPS (13 * 512 * 8 * 1000 * 8)
318 #define FS_BPS (19 * 64 * 1 * 1000 * 8)
320 #ifdef CONFIG_USB_GADGET_DUALSPEED
321 #define DEVSPEED USB_SPEED_HIGH
323 static unsigned qmult = 5;
324 module_param (qmult, uint, S_IRUGO|S_IWUSR);
327 /* for dual-speed hardware, use deeper queues at highspeed */
328 #define qlen(gadget) \
329 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
331 /* also defer IRQs on highspeed TX */
332 #define TX_DELAY qmult
334 static inline int BITRATE(struct usb_gadget *g)
336 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
339 #else /* full speed (low speed doesn't do bulk) */
340 #define DEVSPEED USB_SPEED_FULL
342 #define qlen(gadget) DEFAULT_QLEN
344 static inline int BITRATE(struct usb_gadget *g)
351 /*-------------------------------------------------------------------------*/
353 #define xprintk(d,level,fmt,args...) \
354 printk(level "%s: " fmt , (d)->net->name , ## args)
358 #define DEBUG(dev,fmt,args...) \
359 xprintk(dev , KERN_DEBUG , fmt , ## args)
361 #define DEBUG(dev,fmt,args...) \
368 #define VDEBUG(dev,fmt,args...) \
372 #define ERROR(dev,fmt,args...) \
373 xprintk(dev , KERN_ERR , fmt , ## args)
374 #define WARN(dev,fmt,args...) \
375 xprintk(dev , KERN_WARNING , fmt , ## args)
376 #define INFO(dev,fmt,args...) \
377 xprintk(dev , KERN_INFO , fmt , ## args)
379 /*-------------------------------------------------------------------------*/
381 /* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
382 * ep0 implementation: descriptors, config management, setup().
383 * also optional class-specific notification interrupt transfer.
387 * DESCRIPTORS ... most are static, but strings and (full) configuration
388 * descriptors are built on demand. For now we do either full CDC, or
389 * our simple subset, with RNDIS as an optional second configuration.
391 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
392 * the class descriptors match a modem (they're ignored; it's really just
393 * Ethernet functionality), they don't need the NOP altsetting, and the
394 * status transfer endpoint isn't optional.
397 #define STRING_MANUFACTURER 1
398 #define STRING_PRODUCT 2
399 #define STRING_ETHADDR 3
400 #define STRING_DATA 4
401 #define STRING_CONTROL 5
402 #define STRING_RNDIS_CONTROL 6
404 #define STRING_SUBSET 8
405 #define STRING_RNDIS 9
407 /* holds our biggest descriptor (or RNDIS response) */
408 #define USB_BUFSIZ 256
411 * This device advertises one configuration, eth_config, unless RNDIS
412 * is enabled (rndis_config) on hardware supporting at least two configs.
414 * NOTE: Controllers like superh_udc should probably be able to use
415 * an RNDIS-only configuration.
417 * FIXME define some higher-powered configurations to make it easier
418 * to recharge batteries ...
421 #define DEV_CONFIG_VALUE 1 /* cdc or subset */
422 #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
424 static struct usb_device_descriptor
426 .bLength = sizeof device_desc,
427 .bDescriptorType = USB_DT_DEVICE,
429 .bcdUSB = __constant_cpu_to_le16 (0x0200),
431 .bDeviceClass = USB_CLASS_COMM,
432 .bDeviceSubClass = 0,
433 .bDeviceProtocol = 0,
435 .idVendor = __constant_cpu_to_le16 (CDC_VENDOR_NUM),
436 .idProduct = __constant_cpu_to_le16 (CDC_PRODUCT_NUM),
437 .iManufacturer = STRING_MANUFACTURER,
438 .iProduct = STRING_PRODUCT,
439 .bNumConfigurations = 1,
442 static struct usb_otg_descriptor
444 .bLength = sizeof otg_descriptor,
445 .bDescriptorType = USB_DT_OTG,
447 .bmAttributes = USB_OTG_SRP,
450 static struct usb_config_descriptor
452 .bLength = sizeof eth_config,
453 .bDescriptorType = USB_DT_CONFIG,
455 /* compute wTotalLength on the fly */
457 .bConfigurationValue = DEV_CONFIG_VALUE,
458 .iConfiguration = STRING_CDC,
459 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
463 #ifdef CONFIG_USB_ETH_RNDIS
464 static struct usb_config_descriptor
466 .bLength = sizeof rndis_config,
467 .bDescriptorType = USB_DT_CONFIG,
469 /* compute wTotalLength on the fly */
471 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
472 .iConfiguration = STRING_RNDIS,
473 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
479 * Compared to the simple CDC subset, the full CDC Ethernet model adds
480 * three class descriptors, two interface descriptors, optional status
481 * endpoint. Both have a "data" interface and two bulk endpoints.
482 * There are also differences in how control requests are handled.
484 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of
485 * the CDC-ACM (modem) spec.
488 #ifdef DEV_CONFIG_CDC
489 static struct usb_interface_descriptor
491 .bLength = sizeof control_intf,
492 .bDescriptorType = USB_DT_INTERFACE,
494 .bInterfaceNumber = 0,
495 /* status endpoint is optional; this may be patched later */
497 .bInterfaceClass = USB_CLASS_COMM,
498 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
499 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
500 .iInterface = STRING_CONTROL,
504 #ifdef CONFIG_USB_ETH_RNDIS
505 static const struct usb_interface_descriptor
506 rndis_control_intf = {
507 .bLength = sizeof rndis_control_intf,
508 .bDescriptorType = USB_DT_INTERFACE,
510 .bInterfaceNumber = 0,
512 .bInterfaceClass = USB_CLASS_COMM,
513 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
514 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
515 .iInterface = STRING_RNDIS_CONTROL,
519 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
521 static const struct usb_cdc_header_desc header_desc = {
522 .bLength = sizeof header_desc,
523 .bDescriptorType = USB_DT_CS_INTERFACE,
524 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
526 .bcdCDC = __constant_cpu_to_le16 (0x0110),
529 static const struct usb_cdc_union_desc union_desc = {
530 .bLength = sizeof union_desc,
531 .bDescriptorType = USB_DT_CS_INTERFACE,
532 .bDescriptorSubType = USB_CDC_UNION_TYPE,
534 .bMasterInterface0 = 0, /* index of control interface */
535 .bSlaveInterface0 = 1, /* index of DATA interface */
538 #endif /* CDC || RNDIS */
540 #ifdef CONFIG_USB_ETH_RNDIS
542 static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
543 .bLength = sizeof call_mgmt_descriptor,
544 .bDescriptorType = USB_DT_CS_INTERFACE,
545 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
547 .bmCapabilities = 0x00,
548 .bDataInterface = 0x01,
551 static const struct usb_cdc_acm_descriptor acm_descriptor = {
552 .bLength = sizeof acm_descriptor,
553 .bDescriptorType = USB_DT_CS_INTERFACE,
554 .bDescriptorSubType = USB_CDC_ACM_TYPE,
556 .bmCapabilities = 0x00,
561 #ifdef DEV_CONFIG_CDC
563 static const struct usb_cdc_ether_desc ether_desc = {
564 .bLength = sizeof ether_desc,
565 .bDescriptorType = USB_DT_CS_INTERFACE,
566 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
568 /* this descriptor actually adds value, surprise! */
569 .iMACAddress = STRING_ETHADDR,
570 .bmEthernetStatistics = __constant_cpu_to_le32 (0), /* no statistics */
571 .wMaxSegmentSize = __constant_cpu_to_le16 (ETH_FRAME_LEN),
572 .wNumberMCFilters = __constant_cpu_to_le16 (0),
573 .bNumberPowerFilters = 0,
578 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
580 /* include the status endpoint if we can, even where it's optional.
581 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
582 * packet, to simplify cancellation; and a big transfer interval, to
583 * waste less bandwidth.
585 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
586 * if they ignore the connect/disconnect notifications that real aether
587 * can provide. more advanced cdc configurations might want to support
588 * encapsulated commands (vendor-specific, using control-OUT).
590 * RNDIS requires the status endpoint, since it uses that encapsulation
591 * mechanism for its funky RPC scheme.
594 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
595 #define STATUS_BYTECOUNT 16 /* 8 byte header + data */
597 static struct usb_endpoint_descriptor
599 .bLength = USB_DT_ENDPOINT_SIZE,
600 .bDescriptorType = USB_DT_ENDPOINT,
602 .bEndpointAddress = USB_DIR_IN,
603 .bmAttributes = USB_ENDPOINT_XFER_INT,
604 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
605 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
609 #ifdef DEV_CONFIG_CDC
611 /* the default data interface has no endpoints ... */
613 static const struct usb_interface_descriptor
615 .bLength = sizeof data_nop_intf,
616 .bDescriptorType = USB_DT_INTERFACE,
618 .bInterfaceNumber = 1,
619 .bAlternateSetting = 0,
621 .bInterfaceClass = USB_CLASS_CDC_DATA,
622 .bInterfaceSubClass = 0,
623 .bInterfaceProtocol = 0,
626 /* ... but the "real" data interface has two bulk endpoints */
628 static const struct usb_interface_descriptor
630 .bLength = sizeof data_intf,
631 .bDescriptorType = USB_DT_INTERFACE,
633 .bInterfaceNumber = 1,
634 .bAlternateSetting = 1,
636 .bInterfaceClass = USB_CLASS_CDC_DATA,
637 .bInterfaceSubClass = 0,
638 .bInterfaceProtocol = 0,
639 .iInterface = STRING_DATA,
644 #ifdef CONFIG_USB_ETH_RNDIS
646 /* RNDIS doesn't activate by changing to the "real" altsetting */
648 static const struct usb_interface_descriptor
650 .bLength = sizeof rndis_data_intf,
651 .bDescriptorType = USB_DT_INTERFACE,
653 .bInterfaceNumber = 1,
654 .bAlternateSetting = 0,
656 .bInterfaceClass = USB_CLASS_CDC_DATA,
657 .bInterfaceSubClass = 0,
658 .bInterfaceProtocol = 0,
659 .iInterface = STRING_DATA,
664 #ifdef DEV_CONFIG_SUBSET
667 * "Simple" CDC-subset option is a simple vendor-neutral model that most
668 * full speed controllers can handle: one interface, two bulk endpoints.
671 static const struct usb_interface_descriptor
673 .bLength = sizeof subset_data_intf,
674 .bDescriptorType = USB_DT_INTERFACE,
676 .bInterfaceNumber = 0,
677 .bAlternateSetting = 0,
679 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
680 .bInterfaceSubClass = 0,
681 .bInterfaceProtocol = 0,
682 .iInterface = STRING_DATA,
688 static struct usb_endpoint_descriptor
690 .bLength = USB_DT_ENDPOINT_SIZE,
691 .bDescriptorType = USB_DT_ENDPOINT,
693 .bEndpointAddress = USB_DIR_IN,
694 .bmAttributes = USB_ENDPOINT_XFER_BULK,
697 static struct usb_endpoint_descriptor
699 .bLength = USB_DT_ENDPOINT_SIZE,
700 .bDescriptorType = USB_DT_ENDPOINT,
702 .bEndpointAddress = USB_DIR_OUT,
703 .bmAttributes = USB_ENDPOINT_XFER_BULK,
706 static const struct usb_descriptor_header *fs_eth_function [11] = {
707 (struct usb_descriptor_header *) &otg_descriptor,
708 #ifdef DEV_CONFIG_CDC
709 /* "cdc" mode descriptors */
710 (struct usb_descriptor_header *) &control_intf,
711 (struct usb_descriptor_header *) &header_desc,
712 (struct usb_descriptor_header *) &union_desc,
713 (struct usb_descriptor_header *) ðer_desc,
714 /* NOTE: status endpoint may need to be removed */
715 (struct usb_descriptor_header *) &fs_status_desc,
716 /* data interface, with altsetting */
717 (struct usb_descriptor_header *) &data_nop_intf,
718 (struct usb_descriptor_header *) &data_intf,
719 (struct usb_descriptor_header *) &fs_source_desc,
720 (struct usb_descriptor_header *) &fs_sink_desc,
722 #endif /* DEV_CONFIG_CDC */
725 static inline void __init fs_subset_descriptors(void)
727 #ifdef DEV_CONFIG_SUBSET
728 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
729 fs_eth_function[2] = (struct usb_descriptor_header *) &fs_source_desc;
730 fs_eth_function[3] = (struct usb_descriptor_header *) &fs_sink_desc;
731 fs_eth_function[4] = NULL;
733 fs_eth_function[1] = NULL;
737 #ifdef CONFIG_USB_ETH_RNDIS
738 static const struct usb_descriptor_header *fs_rndis_function [] = {
739 (struct usb_descriptor_header *) &otg_descriptor,
740 /* control interface matches ACM, not Ethernet */
741 (struct usb_descriptor_header *) &rndis_control_intf,
742 (struct usb_descriptor_header *) &header_desc,
743 (struct usb_descriptor_header *) &call_mgmt_descriptor,
744 (struct usb_descriptor_header *) &acm_descriptor,
745 (struct usb_descriptor_header *) &union_desc,
746 (struct usb_descriptor_header *) &fs_status_desc,
747 /* data interface has no altsetting */
748 (struct usb_descriptor_header *) &rndis_data_intf,
749 (struct usb_descriptor_header *) &fs_source_desc,
750 (struct usb_descriptor_header *) &fs_sink_desc,
755 #ifdef CONFIG_USB_GADGET_DUALSPEED
758 * usb 2.0 devices need to expose both high speed and full speed
759 * descriptors, unless they only run at full speed.
762 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
763 static struct usb_endpoint_descriptor
765 .bLength = USB_DT_ENDPOINT_SIZE,
766 .bDescriptorType = USB_DT_ENDPOINT,
768 .bmAttributes = USB_ENDPOINT_XFER_INT,
769 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
770 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
772 #endif /* DEV_CONFIG_CDC */
774 static struct usb_endpoint_descriptor
776 .bLength = USB_DT_ENDPOINT_SIZE,
777 .bDescriptorType = USB_DT_ENDPOINT,
779 .bmAttributes = USB_ENDPOINT_XFER_BULK,
780 .wMaxPacketSize = __constant_cpu_to_le16 (512),
783 static struct usb_endpoint_descriptor
785 .bLength = USB_DT_ENDPOINT_SIZE,
786 .bDescriptorType = USB_DT_ENDPOINT,
788 .bmAttributes = USB_ENDPOINT_XFER_BULK,
789 .wMaxPacketSize = __constant_cpu_to_le16 (512),
792 static struct usb_qualifier_descriptor
794 .bLength = sizeof dev_qualifier,
795 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
797 .bcdUSB = __constant_cpu_to_le16 (0x0200),
798 .bDeviceClass = USB_CLASS_COMM,
800 .bNumConfigurations = 1,
803 static const struct usb_descriptor_header *hs_eth_function [11] = {
804 (struct usb_descriptor_header *) &otg_descriptor,
805 #ifdef DEV_CONFIG_CDC
806 /* "cdc" mode descriptors */
807 (struct usb_descriptor_header *) &control_intf,
808 (struct usb_descriptor_header *) &header_desc,
809 (struct usb_descriptor_header *) &union_desc,
810 (struct usb_descriptor_header *) ðer_desc,
811 /* NOTE: status endpoint may need to be removed */
812 (struct usb_descriptor_header *) &hs_status_desc,
813 /* data interface, with altsetting */
814 (struct usb_descriptor_header *) &data_nop_intf,
815 (struct usb_descriptor_header *) &data_intf,
816 (struct usb_descriptor_header *) &hs_source_desc,
817 (struct usb_descriptor_header *) &hs_sink_desc,
819 #endif /* DEV_CONFIG_CDC */
822 static inline void __init hs_subset_descriptors(void)
824 #ifdef DEV_CONFIG_SUBSET
825 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
826 hs_eth_function[2] = (struct usb_descriptor_header *) &fs_source_desc;
827 hs_eth_function[3] = (struct usb_descriptor_header *) &fs_sink_desc;
828 hs_eth_function[4] = NULL;
830 hs_eth_function[1] = NULL;
834 #ifdef CONFIG_USB_ETH_RNDIS
835 static const struct usb_descriptor_header *hs_rndis_function [] = {
836 (struct usb_descriptor_header *) &otg_descriptor,
837 /* control interface matches ACM, not Ethernet */
838 (struct usb_descriptor_header *) &rndis_control_intf,
839 (struct usb_descriptor_header *) &header_desc,
840 (struct usb_descriptor_header *) &call_mgmt_descriptor,
841 (struct usb_descriptor_header *) &acm_descriptor,
842 (struct usb_descriptor_header *) &union_desc,
843 (struct usb_descriptor_header *) &hs_status_desc,
844 /* data interface has no altsetting */
845 (struct usb_descriptor_header *) &rndis_data_intf,
846 (struct usb_descriptor_header *) &hs_source_desc,
847 (struct usb_descriptor_header *) &hs_sink_desc,
853 /* maxpacket and other transfer characteristics vary by speed. */
854 #define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
858 /* if there's no high speed support, maxpacket doesn't change. */
859 #define ep_desc(g,hs,fs) (((void)(g)), (fs))
861 static inline void __init hs_subset_descriptors(void)
865 #endif /* !CONFIG_USB_GADGET_DUALSPEED */
867 /*-------------------------------------------------------------------------*/
869 /* descriptors that are built on-demand */
871 static char manufacturer [50];
872 static char product_desc [40] = DRIVER_DESC;
874 #ifdef DEV_CONFIG_CDC
875 /* address that the host will use ... usually assigned at random */
876 static char ethaddr [2 * ETH_ALEN + 1];
879 /* static strings, in UTF-8 */
880 static struct usb_string strings [] = {
881 { STRING_MANUFACTURER, manufacturer, },
882 { STRING_PRODUCT, product_desc, },
883 { STRING_DATA, "Ethernet Data", },
884 #ifdef DEV_CONFIG_CDC
885 { STRING_CDC, "CDC Ethernet", },
886 { STRING_ETHADDR, ethaddr, },
887 { STRING_CONTROL, "CDC Communications Control", },
889 #ifdef DEV_CONFIG_SUBSET
890 { STRING_SUBSET, "CDC Ethernet Subset", },
892 #ifdef CONFIG_USB_ETH_RNDIS
893 { STRING_RNDIS, "RNDIS", },
894 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
896 { } /* end of list */
899 static struct usb_gadget_strings stringtab = {
900 .language = 0x0409, /* en-us */
905 * one config, two interfaces: control, data.
906 * complications: class descriptors, and an altsetting.
909 config_buf (enum usb_device_speed speed,
911 unsigned index, int is_otg)
914 const struct usb_config_descriptor *config;
915 const struct usb_descriptor_header **function;
916 #ifdef CONFIG_USB_GADGET_DUALSPEED
917 int hs = (speed == USB_SPEED_HIGH);
919 if (type == USB_DT_OTHER_SPEED_CONFIG)
921 #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
923 #define which_fn(t) (fs_ ## t ## _function)
926 if (index >= device_desc.bNumConfigurations)
929 #ifdef CONFIG_USB_ETH_RNDIS
930 /* list the RNDIS config first, to make Microsoft's drivers
931 * happy. DOCSIS 1.0 needs this too.
933 if (device_desc.bNumConfigurations == 2 && index == 0) {
934 config = &rndis_config;
935 function = which_fn (rndis);
939 config = ð_config;
940 function = which_fn (eth);
943 /* for now, don't advertise srp-only devices */
947 len = usb_gadget_config_buf (config, buf, USB_BUFSIZ, function);
950 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
954 /*-------------------------------------------------------------------------*/
956 static void eth_start (struct eth_dev *dev, gfp_t gfp_flags);
957 static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
960 set_ether_config (struct eth_dev *dev, gfp_t gfp_flags)
963 struct usb_gadget *gadget = dev->gadget;
965 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
966 /* status endpoint used for RNDIS and (optionally) CDC */
967 if (!subset_active(dev) && dev->status_ep) {
968 dev->status = ep_desc (gadget, &hs_status_desc,
970 dev->status_ep->driver_data = dev;
972 result = usb_ep_enable (dev->status_ep, dev->status);
974 DEBUG (dev, "enable %s --> %d\n",
975 dev->status_ep->name, result);
981 dev->in = ep_desc (dev->gadget, &hs_source_desc, &fs_source_desc);
982 dev->in_ep->driver_data = dev;
984 dev->out = ep_desc (dev->gadget, &hs_sink_desc, &fs_sink_desc);
985 dev->out_ep->driver_data = dev;
987 /* With CDC, the host isn't allowed to use these two data
988 * endpoints in the default altsetting for the interface.
989 * so we don't activate them yet. Reset from SET_INTERFACE.
991 * Strictly speaking RNDIS should work the same: activation is
992 * a side effect of setting a packet filter. Deactivation is
993 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
995 if (!cdc_active(dev)) {
996 result = usb_ep_enable (dev->in_ep, dev->in);
998 DEBUG(dev, "enable %s --> %d\n",
999 dev->in_ep->name, result);
1003 result = usb_ep_enable (dev->out_ep, dev->out);
1005 DEBUG (dev, "enable %s --> %d\n",
1006 dev->in_ep->name, result);
1013 result = alloc_requests (dev, qlen (gadget), gfp_flags);
1015 /* on error, disable any endpoints */
1017 if (!subset_active(dev))
1018 (void) usb_ep_disable (dev->status_ep);
1020 (void) usb_ep_disable (dev->in_ep);
1021 (void) usb_ep_disable (dev->out_ep);
1026 /* activate non-CDC configs right away
1027 * this isn't strictly according to the RNDIS spec
1029 if (!cdc_active (dev)) {
1030 netif_carrier_on (dev->net);
1031 if (netif_running (dev->net)) {
1032 spin_unlock (&dev->lock);
1033 eth_start (dev, GFP_ATOMIC);
1034 spin_lock (&dev->lock);
1039 DEBUG (dev, "qlen %d\n", qlen (gadget));
1041 /* caller is responsible for cleanup on error */
1045 static void eth_reset_config (struct eth_dev *dev)
1047 struct usb_request *req;
1049 if (dev->config == 0)
1052 DEBUG (dev, "%s\n", __FUNCTION__);
1054 netif_stop_queue (dev->net);
1055 netif_carrier_off (dev->net);
1056 rndis_uninit(dev->rndis_config);
1058 /* disable endpoints, forcing (synchronous) completion of
1059 * pending i/o. then free the requests.
1062 usb_ep_disable (dev->in_ep);
1063 while (likely (!list_empty (&dev->tx_reqs))) {
1064 req = container_of (dev->tx_reqs.next,
1065 struct usb_request, list);
1066 list_del (&req->list);
1067 usb_ep_free_request (dev->in_ep, req);
1071 usb_ep_disable (dev->out_ep);
1072 while (likely (!list_empty (&dev->rx_reqs))) {
1073 req = container_of (dev->rx_reqs.next,
1074 struct usb_request, list);
1075 list_del (&req->list);
1076 usb_ep_free_request (dev->out_ep, req);
1081 usb_ep_disable (dev->status_ep);
1084 dev->cdc_filter = 0;
1088 /* change our operational config. must agree with the code
1089 * that returns config descriptors, and altsetting code.
1092 eth_set_config (struct eth_dev *dev, unsigned number, gfp_t gfp_flags)
1095 struct usb_gadget *gadget = dev->gadget;
1097 if (gadget_is_sa1100 (gadget)
1099 && atomic_read (&dev->tx_qlen) != 0) {
1100 /* tx fifo is full, but we can't clear it...*/
1101 INFO (dev, "can't change configurations\n");
1104 eth_reset_config (dev);
1107 case DEV_CONFIG_VALUE:
1108 result = set_ether_config (dev, gfp_flags);
1110 #ifdef CONFIG_USB_ETH_RNDIS
1111 case DEV_RNDIS_CONFIG_VALUE:
1113 result = set_ether_config (dev, gfp_flags);
1125 eth_reset_config (dev);
1126 usb_gadget_vbus_draw(dev->gadget,
1127 dev->gadget->is_otg ? 8 : 100);
1132 power = 2 * eth_config.bMaxPower;
1133 usb_gadget_vbus_draw(dev->gadget, power);
1135 switch (gadget->speed) {
1136 case USB_SPEED_FULL: speed = "full"; break;
1137 #ifdef CONFIG_USB_GADGET_DUALSPEED
1138 case USB_SPEED_HIGH: speed = "high"; break;
1140 default: speed = "?"; break;
1143 dev->config = number;
1144 INFO (dev, "%s speed config #%d: %d mA, %s, using %s\n",
1145 speed, number, power, driver_desc,
1150 : "CDC Ethernet Subset"));
1155 /*-------------------------------------------------------------------------*/
1157 #ifdef DEV_CONFIG_CDC
1159 /* The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
1160 * only to notify the host about link status changes (which we support) or
1161 * report completion of some encapsulated command (as used in RNDIS). Since
1162 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1163 * command mechanism; and only one status request is ever queued.
1166 static void eth_status_complete (struct usb_ep *ep, struct usb_request *req)
1168 struct usb_cdc_notification *event = req->buf;
1169 int value = req->status;
1170 struct eth_dev *dev = ep->driver_data;
1172 /* issue the second notification if host reads the first */
1173 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1175 __le32 *data = req->buf + sizeof *event;
1177 event->bmRequestType = 0xA1;
1178 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
1179 event->wValue = __constant_cpu_to_le16 (0);
1180 event->wIndex = __constant_cpu_to_le16 (1);
1181 event->wLength = __constant_cpu_to_le16 (8);
1183 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1184 data [0] = data [1] = cpu_to_le32 (BITRATE (dev->gadget));
1186 req->length = STATUS_BYTECOUNT;
1187 value = usb_ep_queue (ep, req, GFP_ATOMIC);
1188 DEBUG (dev, "send SPEED_CHANGE --> %d\n", value);
1191 } else if (value != -ECONNRESET)
1192 DEBUG (dev, "event %02x --> %d\n",
1193 event->bNotificationType, value);
1194 req->context = NULL;
1197 static void issue_start_status (struct eth_dev *dev)
1199 struct usb_request *req = dev->stat_req;
1200 struct usb_cdc_notification *event;
1203 DEBUG (dev, "%s, flush old status first\n", __FUNCTION__);
1207 * FIXME ugly idiom, maybe we'd be better with just
1208 * a "cancel the whole queue" primitive since any
1209 * unlink-one primitive has way too many error modes.
1210 * here, we "know" toggle is already clear...
1212 * FIXME iff req->context != null just dequeue it
1214 usb_ep_disable (dev->status_ep);
1215 usb_ep_enable (dev->status_ep, dev->status);
1217 /* 3.8.1 says to issue first NETWORK_CONNECTION, then
1218 * a SPEED_CHANGE. could be useful in some configs.
1221 event->bmRequestType = 0xA1;
1222 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
1223 event->wValue = __constant_cpu_to_le16 (1); /* connected */
1224 event->wIndex = __constant_cpu_to_le16 (1);
1227 req->length = sizeof *event;
1228 req->complete = eth_status_complete;
1231 value = usb_ep_queue (dev->status_ep, req, GFP_ATOMIC);
1233 DEBUG (dev, "status buf queue --> %d\n", value);
1238 /*-------------------------------------------------------------------------*/
1240 static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req)
1242 if (req->status || req->actual != req->length)
1243 DEBUG ((struct eth_dev *) ep->driver_data,
1244 "setup complete --> %d, %d/%d\n",
1245 req->status, req->actual, req->length);
1248 #ifdef CONFIG_USB_ETH_RNDIS
1250 static void rndis_response_complete (struct usb_ep *ep, struct usb_request *req)
1252 if (req->status || req->actual != req->length)
1253 DEBUG ((struct eth_dev *) ep->driver_data,
1254 "rndis response complete --> %d, %d/%d\n",
1255 req->status, req->actual, req->length);
1257 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1260 static void rndis_command_complete (struct usb_ep *ep, struct usb_request *req)
1262 struct eth_dev *dev = ep->driver_data;
1265 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1266 spin_lock(&dev->lock);
1267 status = rndis_msg_parser (dev->rndis_config, (u8 *) req->buf);
1269 ERROR(dev, "%s: rndis parse error %d\n", __FUNCTION__, status);
1270 spin_unlock(&dev->lock);
1276 * The setup() callback implements all the ep0 functionality that's not
1277 * handled lower down. CDC has a number of less-common features:
1279 * - two interfaces: control, and ethernet data
1280 * - Ethernet data interface has two altsettings: default, and active
1281 * - class-specific descriptors for the control interface
1282 * - class-specific control requests
1285 eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1287 struct eth_dev *dev = get_gadget_data (gadget);
1288 struct usb_request *req = dev->req;
1289 int value = -EOPNOTSUPP;
1290 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1291 u16 wValue = le16_to_cpu(ctrl->wValue);
1292 u16 wLength = le16_to_cpu(ctrl->wLength);
1294 /* descriptors just go into the pre-allocated ep0 buffer,
1295 * while config change events may enable network traffic.
1297 req->complete = eth_setup_complete;
1298 switch (ctrl->bRequest) {
1300 case USB_REQ_GET_DESCRIPTOR:
1301 if (ctrl->bRequestType != USB_DIR_IN)
1303 switch (wValue >> 8) {
1306 value = min (wLength, (u16) sizeof device_desc);
1307 memcpy (req->buf, &device_desc, value);
1309 #ifdef CONFIG_USB_GADGET_DUALSPEED
1310 case USB_DT_DEVICE_QUALIFIER:
1311 if (!gadget->is_dualspeed)
1313 value = min (wLength, (u16) sizeof dev_qualifier);
1314 memcpy (req->buf, &dev_qualifier, value);
1317 case USB_DT_OTHER_SPEED_CONFIG:
1318 if (!gadget->is_dualspeed)
1321 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1323 value = config_buf (gadget->speed, req->buf,
1328 value = min (wLength, (u16) value);
1332 value = usb_gadget_get_string (&stringtab,
1333 wValue & 0xff, req->buf);
1335 value = min (wLength, (u16) value);
1340 case USB_REQ_SET_CONFIGURATION:
1341 if (ctrl->bRequestType != 0)
1343 if (gadget->a_hnp_support)
1344 DEBUG (dev, "HNP available\n");
1345 else if (gadget->a_alt_hnp_support)
1346 DEBUG (dev, "HNP needs a different root port\n");
1347 spin_lock (&dev->lock);
1348 value = eth_set_config (dev, wValue, GFP_ATOMIC);
1349 spin_unlock (&dev->lock);
1351 case USB_REQ_GET_CONFIGURATION:
1352 if (ctrl->bRequestType != USB_DIR_IN)
1354 *(u8 *)req->buf = dev->config;
1355 value = min (wLength, (u16) 1);
1358 case USB_REQ_SET_INTERFACE:
1359 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1363 if (!cdc_active(dev) && wIndex != 0)
1365 spin_lock (&dev->lock);
1367 /* PXA hardware partially handles SET_INTERFACE;
1368 * we need to kluge around that interference.
1370 if (gadget_is_pxa (gadget)) {
1371 value = eth_set_config (dev, DEV_CONFIG_VALUE,
1376 #ifdef DEV_CONFIG_CDC
1378 case 0: /* control/master intf */
1382 usb_ep_disable (dev->status_ep);
1383 usb_ep_enable (dev->status_ep, dev->status);
1387 case 1: /* data intf */
1390 usb_ep_disable (dev->in_ep);
1391 usb_ep_disable (dev->out_ep);
1393 /* CDC requires the data transfers not be done from
1394 * the default interface setting ... also, setting
1395 * the non-default interface resets filters etc.
1398 if (!cdc_active (dev))
1400 usb_ep_enable (dev->in_ep, dev->in);
1401 usb_ep_enable (dev->out_ep, dev->out);
1402 dev->cdc_filter = DEFAULT_FILTER;
1403 netif_carrier_on (dev->net);
1405 issue_start_status (dev);
1406 if (netif_running (dev->net)) {
1407 spin_unlock (&dev->lock);
1408 eth_start (dev, GFP_ATOMIC);
1409 spin_lock (&dev->lock);
1412 netif_stop_queue (dev->net);
1413 netif_carrier_off (dev->net);
1419 /* FIXME this is wrong, as is the assumption that
1420 * all non-PXA hardware talks real CDC ...
1422 dev_warn (&gadget->dev, "set_interface ignored!\n");
1423 #endif /* DEV_CONFIG_CDC */
1426 spin_unlock (&dev->lock);
1428 case USB_REQ_GET_INTERFACE:
1429 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1433 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
1436 /* for CDC, iff carrier is on, data interface is active. */
1437 if (rndis_active(dev) || wIndex != 1)
1438 *(u8 *)req->buf = 0;
1440 *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0;
1441 value = min (wLength, (u16) 1);
1444 #ifdef DEV_CONFIG_CDC
1445 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
1446 /* see 6.2.30: no data, wIndex = interface,
1447 * wValue = packet filter bitmap
1449 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1454 DEBUG (dev, "packet filter %02x\n", wValue);
1455 dev->cdc_filter = wValue;
1460 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1461 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1462 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1463 * case USB_CDC_GET_ETHERNET_STATISTIC:
1466 #endif /* DEV_CONFIG_CDC */
1468 #ifdef CONFIG_USB_ETH_RNDIS
1469 /* RNDIS uses the CDC command encapsulation mechanism to implement
1470 * an RPC scheme, with much getting/setting of attributes by OID.
1472 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1473 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1474 || !rndis_active(dev)
1475 || wLength > USB_BUFSIZ
1477 || rndis_control_intf.bInterfaceNumber
1480 /* read the request, then process it */
1482 req->complete = rndis_command_complete;
1483 /* later, rndis_control_ack () sends a notification */
1486 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1487 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1488 == ctrl->bRequestType
1489 && rndis_active(dev)
1490 // && wLength >= 0x0400
1492 && rndis_control_intf.bInterfaceNumber
1496 /* return the result */
1497 buf = rndis_get_next_response (dev->rndis_config,
1500 memcpy (req->buf, buf, value);
1501 req->complete = rndis_response_complete;
1502 rndis_free_response(dev->rndis_config, buf);
1504 /* else stalls ... spec says to avoid that */
1511 "unknown control req%02x.%02x v%04x i%04x l%d\n",
1512 ctrl->bRequestType, ctrl->bRequest,
1513 wValue, wIndex, wLength);
1516 /* respond with data transfer before status phase? */
1518 req->length = value;
1519 req->zero = value < wLength
1520 && (value % gadget->ep0->maxpacket) == 0;
1521 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1523 DEBUG (dev, "ep_queue --> %d\n", value);
1525 eth_setup_complete (gadget->ep0, req);
1529 /* host either stalls (value < 0) or reports success */
1534 eth_disconnect (struct usb_gadget *gadget)
1536 struct eth_dev *dev = get_gadget_data (gadget);
1537 unsigned long flags;
1539 spin_lock_irqsave (&dev->lock, flags);
1540 netif_stop_queue (dev->net);
1541 netif_carrier_off (dev->net);
1542 eth_reset_config (dev);
1543 spin_unlock_irqrestore (&dev->lock, flags);
1545 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1547 /* next we may get setup() calls to enumerate new connections;
1548 * or an unbind() during shutdown (including removing module).
1552 /*-------------------------------------------------------------------------*/
1554 /* NETWORK DRIVER HOOKUP (to the layer above this driver) */
1556 static int eth_change_mtu (struct net_device *net, int new_mtu)
1558 struct eth_dev *dev = netdev_priv(net);
1563 if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN)
1565 /* no zero-length packet read wanted after mtu-sized packets */
1566 if (((new_mtu + sizeof (struct ethhdr)) % dev->in_ep->maxpacket) == 0)
1572 static struct net_device_stats *eth_get_stats (struct net_device *net)
1574 return &((struct eth_dev *)netdev_priv(net))->stats;
1577 static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
1579 struct eth_dev *dev = netdev_priv(net);
1580 strlcpy(p->driver, shortname, sizeof p->driver);
1581 strlcpy(p->version, DRIVER_VERSION, sizeof p->version);
1582 strlcpy(p->fw_version, dev->gadget->name, sizeof p->fw_version);
1583 strlcpy (p->bus_info, dev->gadget->dev.bus_id, sizeof p->bus_info);
1586 static u32 eth_get_link(struct net_device *net)
1588 struct eth_dev *dev = netdev_priv(net);
1589 return dev->gadget->speed != USB_SPEED_UNKNOWN;
1592 static struct ethtool_ops ops = {
1593 .get_drvinfo = eth_get_drvinfo,
1594 .get_link = eth_get_link
1597 static void defer_kevent (struct eth_dev *dev, int flag)
1599 if (test_and_set_bit (flag, &dev->todo))
1601 if (!schedule_work (&dev->work))
1602 ERROR (dev, "kevent %d may have been dropped\n", flag);
1604 DEBUG (dev, "kevent %d scheduled\n", flag);
1607 static void rx_complete (struct usb_ep *ep, struct usb_request *req);
1610 rx_submit (struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
1612 struct sk_buff *skb;
1613 int retval = -ENOMEM;
1616 /* Padding up to RX_EXTRA handles minor disagreements with host.
1617 * Normally we use the USB "terminate on short read" convention;
1618 * so allow up to (N*maxpacket), since that memory is normally
1619 * already allocated. Some hardware doesn't deal well with short
1620 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1621 * byte off the end (to force hardware errors on overflow).
1623 * RNDIS uses internal framing, and explicitly allows senders to
1624 * pad to end-of-packet. That's potentially nice for speed,
1625 * but means receivers can't recover synch on their own.
1627 size = (sizeof (struct ethhdr) + dev->net->mtu + RX_EXTRA);
1628 size += dev->out_ep->maxpacket - 1;
1629 if (rndis_active(dev))
1630 size += sizeof (struct rndis_packet_msg_type);
1631 size -= size % dev->out_ep->maxpacket;
1633 if ((skb = alloc_skb (size + NET_IP_ALIGN, gfp_flags)) == 0) {
1634 DEBUG (dev, "no rx skb\n");
1638 /* Some platforms perform better when IP packets are aligned,
1639 * but on at least one, checksumming fails otherwise. Note:
1640 * RNDIS headers involve variable numbers of LE32 values.
1642 skb_reserve(skb, NET_IP_ALIGN);
1644 req->buf = skb->data;
1646 req->complete = rx_complete;
1649 retval = usb_ep_queue (dev->out_ep, req, gfp_flags);
1650 if (retval == -ENOMEM)
1652 defer_kevent (dev, WORK_RX_MEMORY);
1654 DEBUG (dev, "rx submit --> %d\n", retval);
1655 dev_kfree_skb_any (skb);
1656 spin_lock (&dev->lock);
1657 list_add (&req->list, &dev->rx_reqs);
1658 spin_unlock (&dev->lock);
1663 static void rx_complete (struct usb_ep *ep, struct usb_request *req)
1665 struct sk_buff *skb = req->context;
1666 struct eth_dev *dev = ep->driver_data;
1667 int status = req->status;
1671 /* normal completion */
1673 skb_put (skb, req->actual);
1674 /* we know MaxPacketsPerTransfer == 1 here */
1675 if (rndis_active(dev))
1676 status = rndis_rm_hdr (skb);
1678 || ETH_HLEN > skb->len
1679 || skb->len > ETH_FRAME_LEN) {
1680 dev->stats.rx_errors++;
1681 dev->stats.rx_length_errors++;
1682 DEBUG (dev, "rx length %d\n", skb->len);
1686 skb->dev = dev->net;
1687 skb->protocol = eth_type_trans (skb, dev->net);
1688 dev->stats.rx_packets++;
1689 dev->stats.rx_bytes += skb->len;
1691 /* no buffer copies needed, unless hardware can't
1694 status = netif_rx (skb);
1698 /* software-driven interface shutdown */
1699 case -ECONNRESET: // unlink
1700 case -ESHUTDOWN: // disconnect etc
1701 VDEBUG (dev, "rx shutdown, code %d\n", status);
1704 /* for hardware automagic (such as pxa) */
1705 case -ECONNABORTED: // endpoint reset
1706 DEBUG (dev, "rx %s reset\n", ep->name);
1707 defer_kevent (dev, WORK_RX_MEMORY);
1709 dev_kfree_skb_any (skb);
1714 dev->stats.rx_over_errors++;
1718 dev->stats.rx_errors++;
1719 DEBUG (dev, "rx status %d\n", status);
1724 dev_kfree_skb_any (skb);
1725 if (!netif_running (dev->net)) {
1727 /* nobody reading rx_reqs, so no dev->lock */
1728 list_add (&req->list, &dev->rx_reqs);
1732 rx_submit (dev, req, GFP_ATOMIC);
1735 static int prealloc (struct list_head *list, struct usb_ep *ep,
1736 unsigned n, gfp_t gfp_flags)
1739 struct usb_request *req;
1744 /* queue/recycle up to N requests */
1746 list_for_each_entry (req, list, list) {
1751 req = usb_ep_alloc_request (ep, gfp_flags);
1753 return list_empty (list) ? -ENOMEM : 0;
1754 list_add (&req->list, list);
1761 struct list_head *next;
1763 next = req->list.next;
1764 list_del (&req->list);
1765 usb_ep_free_request (ep, req);
1770 req = container_of (next, struct usb_request, list);
1775 static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
1779 status = prealloc (&dev->tx_reqs, dev->in_ep, n, gfp_flags);
1782 status = prealloc (&dev->rx_reqs, dev->out_ep, n, gfp_flags);
1787 DEBUG (dev, "can't alloc requests\n");
1791 static void rx_fill (struct eth_dev *dev, gfp_t gfp_flags)
1793 struct usb_request *req;
1794 unsigned long flags;
1796 /* fill unused rxq slots with some skb */
1797 spin_lock_irqsave (&dev->lock, flags);
1798 while (!list_empty (&dev->rx_reqs)) {
1799 req = container_of (dev->rx_reqs.next,
1800 struct usb_request, list);
1801 list_del_init (&req->list);
1802 spin_unlock_irqrestore (&dev->lock, flags);
1804 if (rx_submit (dev, req, gfp_flags) < 0) {
1805 defer_kevent (dev, WORK_RX_MEMORY);
1809 spin_lock_irqsave (&dev->lock, flags);
1811 spin_unlock_irqrestore (&dev->lock, flags);
1814 static void eth_work (void *_dev)
1816 struct eth_dev *dev = _dev;
1818 if (test_and_clear_bit (WORK_RX_MEMORY, &dev->todo)) {
1819 if (netif_running (dev->net))
1820 rx_fill (dev, GFP_KERNEL);
1824 DEBUG (dev, "work done, flags = 0x%lx\n", dev->todo);
1827 static void tx_complete (struct usb_ep *ep, struct usb_request *req)
1829 struct sk_buff *skb = req->context;
1830 struct eth_dev *dev = ep->driver_data;
1832 switch (req->status) {
1834 dev->stats.tx_errors++;
1835 VDEBUG (dev, "tx err %d\n", req->status);
1837 case -ECONNRESET: // unlink
1838 case -ESHUTDOWN: // disconnect etc
1841 dev->stats.tx_bytes += skb->len;
1843 dev->stats.tx_packets++;
1845 spin_lock (&dev->lock);
1846 list_add (&req->list, &dev->tx_reqs);
1847 spin_unlock (&dev->lock);
1848 dev_kfree_skb_any (skb);
1850 atomic_dec (&dev->tx_qlen);
1851 if (netif_carrier_ok (dev->net))
1852 netif_wake_queue (dev->net);
1855 static inline int eth_is_promisc (struct eth_dev *dev)
1857 /* no filters for the CDC subset; always promisc */
1858 if (subset_active (dev))
1860 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1863 static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1865 struct eth_dev *dev = netdev_priv(net);
1866 int length = skb->len;
1868 struct usb_request *req = NULL;
1869 unsigned long flags;
1871 /* apply outgoing CDC or RNDIS filters */
1872 if (!eth_is_promisc (dev)) {
1873 u8 *dest = skb->data;
1875 if (dest [0] & 0x01) {
1878 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1879 * SET_ETHERNET_MULTICAST_FILTERS requests
1881 if (memcmp (dest, net->broadcast, ETH_ALEN) == 0)
1882 type = USB_CDC_PACKET_TYPE_BROADCAST;
1884 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1885 if (!(dev->cdc_filter & type)) {
1886 dev_kfree_skb_any (skb);
1890 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1893 spin_lock_irqsave (&dev->lock, flags);
1894 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1895 list_del (&req->list);
1896 if (list_empty (&dev->tx_reqs))
1897 netif_stop_queue (net);
1898 spin_unlock_irqrestore (&dev->lock, flags);
1900 /* no buffer copies needed, unless the network stack did it
1901 * or the hardware can't use skb buffers.
1902 * or there's not enough space for any RNDIS headers we need
1904 if (rndis_active(dev)) {
1905 struct sk_buff *skb_rndis;
1907 skb_rndis = skb_realloc_headroom (skb,
1908 sizeof (struct rndis_packet_msg_type));
1912 dev_kfree_skb_any (skb);
1914 rndis_add_hdr (skb);
1917 req->buf = skb->data;
1919 req->complete = tx_complete;
1921 /* use zlp framing on tx for strict CDC-Ether conformance,
1922 * though any robust network rx path ignores extra padding.
1923 * and some hardware doesn't like to write zlps.
1926 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1929 req->length = length;
1931 #ifdef CONFIG_USB_GADGET_DUALSPEED
1932 /* throttle highspeed IRQ rate back slightly */
1933 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1934 ? ((atomic_read (&dev->tx_qlen) % TX_DELAY) != 0)
1938 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1941 DEBUG (dev, "tx queue err %d\n", retval);
1944 net->trans_start = jiffies;
1945 atomic_inc (&dev->tx_qlen);
1950 dev->stats.tx_dropped++;
1951 dev_kfree_skb_any (skb);
1952 spin_lock_irqsave (&dev->lock, flags);
1953 if (list_empty (&dev->tx_reqs))
1954 netif_start_queue (net);
1955 list_add (&req->list, &dev->tx_reqs);
1956 spin_unlock_irqrestore (&dev->lock, flags);
1961 /*-------------------------------------------------------------------------*/
1963 #ifdef CONFIG_USB_ETH_RNDIS
1965 /* The interrupt endpoint is used in RNDIS to notify the host when messages
1966 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1967 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1968 * REMOTE_NDIS_KEEPALIVE_MSG.
1970 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1971 * normally just one notification will be queued.
1974 static struct usb_request *eth_req_alloc (struct usb_ep *, unsigned, gfp_t);
1975 static void eth_req_free (struct usb_ep *ep, struct usb_request *req);
1978 rndis_control_ack_complete (struct usb_ep *ep, struct usb_request *req)
1980 struct eth_dev *dev = ep->driver_data;
1982 if (req->status || req->actual != req->length)
1984 "rndis control ack complete --> %d, %d/%d\n",
1985 req->status, req->actual, req->length);
1986 req->context = NULL;
1988 if (req != dev->stat_req)
1989 eth_req_free(ep, req);
1992 static int rndis_control_ack (struct net_device *net)
1994 struct eth_dev *dev = netdev_priv(net);
1996 struct usb_request *resp = dev->stat_req;
1998 /* in case RNDIS calls this after disconnect */
2000 DEBUG (dev, "status ENODEV\n");
2004 /* in case queue length > 1 */
2005 if (resp->context) {
2006 resp = eth_req_alloc (dev->status_ep, 8, GFP_ATOMIC);
2011 /* Send RNDIS RESPONSE_AVAILABLE notification;
2012 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
2015 resp->complete = rndis_control_ack_complete;
2016 resp->context = dev;
2018 *((__le32 *) resp->buf) = __constant_cpu_to_le32 (1);
2019 *((__le32 *) resp->buf + 1) = __constant_cpu_to_le32 (0);
2021 length = usb_ep_queue (dev->status_ep, resp, GFP_ATOMIC);
2024 rndis_control_ack_complete (dev->status_ep, resp);
2032 #define rndis_control_ack NULL
2036 static void eth_start (struct eth_dev *dev, gfp_t gfp_flags)
2038 DEBUG (dev, "%s\n", __FUNCTION__);
2040 /* fill the rx queue */
2041 rx_fill (dev, gfp_flags);
2043 /* and open the tx floodgates */
2044 atomic_set (&dev->tx_qlen, 0);
2045 netif_wake_queue (dev->net);
2046 if (rndis_active(dev)) {
2047 rndis_set_param_medium (dev->rndis_config,
2049 BITRATE(dev->gadget)/100);
2050 (void) rndis_signal_connect (dev->rndis_config);
2054 static int eth_open (struct net_device *net)
2056 struct eth_dev *dev = netdev_priv(net);
2058 DEBUG (dev, "%s\n", __FUNCTION__);
2059 if (netif_carrier_ok (dev->net))
2060 eth_start (dev, GFP_KERNEL);
2064 static int eth_stop (struct net_device *net)
2066 struct eth_dev *dev = netdev_priv(net);
2068 VDEBUG (dev, "%s\n", __FUNCTION__);
2069 netif_stop_queue (net);
2071 DEBUG (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
2072 dev->stats.rx_packets, dev->stats.tx_packets,
2073 dev->stats.rx_errors, dev->stats.tx_errors
2076 /* ensure there are no more active requests */
2078 usb_ep_disable (dev->in_ep);
2079 usb_ep_disable (dev->out_ep);
2080 if (netif_carrier_ok (dev->net)) {
2081 DEBUG (dev, "host still using in/out endpoints\n");
2082 // FIXME idiom may leave toggle wrong here
2083 usb_ep_enable (dev->in_ep, dev->in);
2084 usb_ep_enable (dev->out_ep, dev->out);
2086 if (dev->status_ep) {
2087 usb_ep_disable (dev->status_ep);
2088 usb_ep_enable (dev->status_ep, dev->status);
2092 if (rndis_active(dev)) {
2093 rndis_set_param_medium (dev->rndis_config,
2094 NDIS_MEDIUM_802_3, 0);
2095 (void) rndis_signal_disconnect (dev->rndis_config);
2101 /*-------------------------------------------------------------------------*/
2103 static struct usb_request *
2104 eth_req_alloc (struct usb_ep *ep, unsigned size, gfp_t gfp_flags)
2106 struct usb_request *req;
2108 req = usb_ep_alloc_request (ep, gfp_flags);
2112 req->buf = kmalloc (size, gfp_flags);
2114 usb_ep_free_request (ep, req);
2121 eth_req_free (struct usb_ep *ep, struct usb_request *req)
2124 usb_ep_free_request (ep, req);
2129 eth_unbind (struct usb_gadget *gadget)
2131 struct eth_dev *dev = get_gadget_data (gadget);
2133 DEBUG (dev, "unbind\n");
2134 rndis_deregister (dev->rndis_config);
2137 /* we've already been disconnected ... no i/o is active */
2139 eth_req_free (gadget->ep0, dev->req);
2142 if (dev->stat_req) {
2143 eth_req_free (dev->status_ep, dev->stat_req);
2144 dev->stat_req = NULL;
2147 unregister_netdev (dev->net);
2148 free_netdev(dev->net);
2150 /* assuming we used keventd, it must quiesce too */
2151 flush_scheduled_work ();
2152 set_gadget_data (gadget, NULL);
2155 static u8 __init nibble (unsigned char c)
2157 if (likely (isdigit (c)))
2160 if (likely (isxdigit (c)))
2161 return 10 + c - 'A';
2165 static void __init get_ether_addr (const char *str, u8 *dev_addr)
2170 for (i = 0; i < 6; i++) {
2173 if((*str == '.') || (*str == ':'))
2175 num = nibble(*str++) << 4;
2176 num |= (nibble(*str++));
2179 if (is_valid_ether_addr (dev_addr))
2182 random_ether_addr(dev_addr);
2186 eth_bind (struct usb_gadget *gadget)
2188 struct eth_dev *dev;
2189 struct net_device *net;
2190 u8 cdc = 1, zlp = 1, rndis = 1;
2191 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
2192 int status = -ENOMEM;
2195 /* these flags are only ever cleared; compiler take note */
2196 #ifndef DEV_CONFIG_CDC
2199 #ifndef CONFIG_USB_ETH_RNDIS
2203 /* Because most host side USB stacks handle CDC Ethernet, that
2204 * standard protocol is _strongly_ preferred for interop purposes.
2205 * (By everyone except Microsoft.)
2207 if (gadget_is_pxa (gadget)) {
2208 /* pxa doesn't support altsettings */
2210 } else if (gadget_is_sh(gadget)) {
2211 /* sh doesn't support multiple interfaces or configs */
2214 } else if (gadget_is_sa1100 (gadget)) {
2215 /* hardware can't write zlps */
2217 /* sa1100 CAN do CDC, without status endpoint ... we use
2218 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2223 gcnum = usb_gadget_controller_number (gadget);
2225 device_desc.bcdDevice = cpu_to_le16 (0x0200 + gcnum);
2227 /* can't assume CDC works. don't want to default to
2228 * anything less functional on CDC-capable hardware,
2229 * so we fail in this case.
2231 dev_err (&gadget->dev,
2232 "controller '%s' not recognized\n",
2236 snprintf (manufacturer, sizeof manufacturer, "%s %s/%s",
2237 system_utsname.sysname, system_utsname.release,
2240 /* If there's an RNDIS configuration, that's what Windows wants to
2241 * be using ... so use these product IDs here and in the "linux.inf"
2242 * needed to install MSFT drivers. Current Linux kernels will use
2243 * the second configuration if it's CDC Ethernet, and need some help
2244 * to choose the right configuration otherwise.
2247 device_desc.idVendor =
2248 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2249 device_desc.idProduct =
2250 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2251 snprintf (product_desc, sizeof product_desc,
2252 "RNDIS/%s", driver_desc);
2254 /* CDC subset ... recognized by Linux since 2.4.10, but Windows
2255 * drivers aren't widely available.
2258 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2259 device_desc.idVendor =
2260 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2261 device_desc.idProduct =
2262 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2265 /* support optional vendor/distro customization */
2268 dev_err (&gadget->dev, "idVendor needs idProduct!\n");
2271 device_desc.idVendor = cpu_to_le16(idVendor);
2272 device_desc.idProduct = cpu_to_le16(idProduct);
2274 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2277 strlcpy (manufacturer, iManufacturer, sizeof manufacturer);
2279 strlcpy (product_desc, iProduct, sizeof product_desc);
2281 /* all we really need is bulk IN/OUT */
2282 usb_ep_autoconfig_reset (gadget);
2283 in_ep = usb_ep_autoconfig (gadget, &fs_source_desc);
2286 dev_err (&gadget->dev,
2287 "can't autoconfigure on %s\n",
2291 in_ep->driver_data = in_ep; /* claim */
2293 out_ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
2296 out_ep->driver_data = out_ep; /* claim */
2298 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2299 /* CDC Ethernet control interface doesn't require a status endpoint.
2300 * Since some hosts expect one, try to allocate one anyway.
2303 status_ep = usb_ep_autoconfig (gadget, &fs_status_desc);
2305 status_ep->driver_data = status_ep; /* claim */
2307 dev_err (&gadget->dev,
2308 "can't run RNDIS on %s\n",
2311 #ifdef DEV_CONFIG_CDC
2312 /* pxa25x only does CDC subset; often used with RNDIS */
2314 control_intf.bNumEndpoints = 0;
2315 /* FIXME remove endpoint from descriptor list */
2321 /* one config: cdc, else minimal subset */
2323 eth_config.bNumInterfaces = 1;
2324 eth_config.iConfiguration = STRING_SUBSET;
2325 fs_subset_descriptors();
2326 hs_subset_descriptors();
2329 /* For now RNDIS is always a second config */
2331 device_desc.bNumConfigurations = 2;
2333 #ifdef CONFIG_USB_GADGET_DUALSPEED
2335 dev_qualifier.bNumConfigurations = 2;
2337 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2339 /* assumes ep0 uses the same value for both speeds ... */
2340 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2342 /* and that all endpoints are dual-speed */
2343 hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
2344 hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
2345 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2347 hs_status_desc.bEndpointAddress =
2348 fs_status_desc.bEndpointAddress;
2350 #endif /* DUALSPEED */
2352 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
2353 usb_gadget_set_selfpowered (gadget);
2355 if (gadget->is_otg) {
2356 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2357 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2358 eth_config.bMaxPower = 4;
2359 #ifdef CONFIG_USB_ETH_RNDIS
2360 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2361 rndis_config.bMaxPower = 4;
2365 net = alloc_etherdev (sizeof *dev);
2368 dev = netdev_priv(net);
2369 spin_lock_init (&dev->lock);
2370 INIT_WORK (&dev->work, eth_work, dev);
2371 INIT_LIST_HEAD (&dev->tx_reqs);
2372 INIT_LIST_HEAD (&dev->rx_reqs);
2374 /* network device setup */
2376 SET_MODULE_OWNER (net);
2377 strcpy (net->name, "usb%d");
2382 dev->out_ep = out_ep;
2383 dev->status_ep = status_ep;
2385 /* Module params for these addresses should come from ID proms.
2386 * The host side address is used with CDC and RNDIS, and commonly
2387 * ends up in a persistent config database.
2389 get_ether_addr(dev_addr, net->dev_addr);
2391 get_ether_addr(host_addr, dev->host_mac);
2392 #ifdef DEV_CONFIG_CDC
2393 snprintf (ethaddr, sizeof ethaddr, "%02X%02X%02X%02X%02X%02X",
2394 dev->host_mac [0], dev->host_mac [1],
2395 dev->host_mac [2], dev->host_mac [3],
2396 dev->host_mac [4], dev->host_mac [5]);
2401 status = rndis_init();
2403 dev_err (&gadget->dev, "can't init RNDIS, %d\n",
2409 net->change_mtu = eth_change_mtu;
2410 net->get_stats = eth_get_stats;
2411 net->hard_start_xmit = eth_start_xmit;
2412 net->open = eth_open;
2413 net->stop = eth_stop;
2414 // watchdog_timeo, tx_timeout ...
2415 // set_multicast_list
2416 SET_ETHTOOL_OPS(net, &ops);
2418 /* preallocate control message data and buffer */
2419 dev->req = eth_req_alloc (gadget->ep0, USB_BUFSIZ, GFP_KERNEL);
2422 dev->req->complete = eth_setup_complete;
2424 /* ... and maybe likewise for status transfer */
2425 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2426 if (dev->status_ep) {
2427 dev->stat_req = eth_req_alloc (dev->status_ep,
2428 STATUS_BYTECOUNT, GFP_KERNEL);
2429 if (!dev->stat_req) {
2430 eth_req_free (gadget->ep0, dev->req);
2433 dev->stat_req->context = NULL;
2437 /* finish hookup to lower layer ... */
2438 dev->gadget = gadget;
2439 set_gadget_data (gadget, dev);
2440 gadget->ep0->driver_data = dev;
2442 /* two kinds of host-initiated state changes:
2443 * - iff DATA transfer is active, carrier is "on"
2444 * - tx queueing enabled if open *and* carrier is "on"
2446 netif_stop_queue (dev->net);
2447 netif_carrier_off (dev->net);
2449 SET_NETDEV_DEV (dev->net, &gadget->dev);
2450 status = register_netdev (dev->net);
2454 INFO (dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
2455 INFO (dev, "using %s, OUT %s IN %s%s%s\n", gadget->name,
2456 out_ep->name, in_ep->name,
2457 status_ep ? " STATUS " : "",
2458 status_ep ? status_ep->name : ""
2460 INFO (dev, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2461 net->dev_addr [0], net->dev_addr [1],
2462 net->dev_addr [2], net->dev_addr [3],
2463 net->dev_addr [4], net->dev_addr [5]);
2466 INFO (dev, "HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2467 dev->host_mac [0], dev->host_mac [1],
2468 dev->host_mac [2], dev->host_mac [3],
2469 dev->host_mac [4], dev->host_mac [5]);
2474 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2476 dev->rndis_config = rndis_register (rndis_control_ack);
2477 if (dev->rndis_config < 0) {
2479 unregister_netdev (dev->net);
2484 /* these set up a lot of the OIDs that RNDIS needs */
2485 rndis_set_host_mac (dev->rndis_config, dev->host_mac);
2486 if (rndis_set_param_dev (dev->rndis_config, dev->net,
2487 &dev->stats, &dev->cdc_filter))
2489 if (rndis_set_param_vendor (dev->rndis_config, vendorID,
2492 if (rndis_set_param_medium (dev->rndis_config,
2496 INFO (dev, "RNDIS ready\n");
2502 dev_dbg(&gadget->dev, "register_netdev failed, %d\n", status);
2504 eth_unbind (gadget);
2508 /*-------------------------------------------------------------------------*/
2511 eth_suspend (struct usb_gadget *gadget)
2513 struct eth_dev *dev = get_gadget_data (gadget);
2515 DEBUG (dev, "suspend\n");
2520 eth_resume (struct usb_gadget *gadget)
2522 struct eth_dev *dev = get_gadget_data (gadget);
2524 DEBUG (dev, "resume\n");
2528 /*-------------------------------------------------------------------------*/
2530 static struct usb_gadget_driver eth_driver = {
2533 .function = (char *) driver_desc,
2535 .unbind = eth_unbind,
2538 .disconnect = eth_disconnect,
2540 .suspend = eth_suspend,
2541 .resume = eth_resume,
2544 .name = (char *) shortname,
2545 .owner = THIS_MODULE,
2549 MODULE_DESCRIPTION (DRIVER_DESC);
2550 MODULE_AUTHOR ("David Brownell, Benedikt Spanger");
2551 MODULE_LICENSE ("GPL");
2554 static int __init init (void)
2556 return usb_gadget_register_driver (ð_driver);
2560 static void __exit cleanup (void)
2562 usb_gadget_unregister_driver (ð_driver);
2564 module_exit (cleanup);