Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / usb / gadget / ether.c
blob191278b7dfe3e3825578f4fdaacb72119b4a4447
1 /*
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
22 /* #define VERBOSE_DEBUG */
24 #include <linux/kernel.h>
25 #include <linux/utsname.h>
26 #include <linux/device.h>
27 #include <linux/ctype.h>
28 #include <linux/etherdevice.h>
29 #include <linux/ethtool.h>
31 #include <linux/usb/ch9.h>
32 #include <linux/usb/cdc.h>
33 #include <linux/usb/gadget.h>
35 #include "gadget_chips.h"
37 /*-------------------------------------------------------------------------*/
40 * Ethernet gadget driver -- with CDC and non-CDC options
41 * Builds on hardware support for a full duplex link.
43 * CDC Ethernet is the standard USB solution for sending Ethernet frames
44 * using USB. Real hardware tends to use the same framing protocol but look
45 * different for control features. This driver strongly prefers to use
46 * this USB-IF standard as its open-systems interoperability solution;
47 * most host side USB stacks (except from Microsoft) support it.
49 * There's some hardware that can't talk CDC. We make that hardware
50 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
51 * link-level setup only requires activating the configuration. Only the
52 * endpoint descriptors, and product/vendor IDs, are relevant; no control
53 * operations are available. Linux supports it, but other host operating
54 * systems may not. (This is a subset of CDC Ethernet.)
56 * It turns out that if you add a few descriptors to that "CDC Subset",
57 * (Windows) host side drivers from MCCI can treat it as one submode of
58 * a proprietary scheme called "SAFE" ... without needing to know about
59 * specific product/vendor IDs. So we do that, making it easier to use
60 * those MS-Windows drivers. Those added descriptors make it resemble a
61 * CDC MDLM device, but they don't change device behavior at all. (See
62 * MCCI Engineering report 950198 "SAFE Networking Functions".)
64 * A third option is also in use. Rather than CDC Ethernet, or something
65 * simpler, Microsoft pushes their own approach: RNDIS. The published
66 * RNDIS specs are ambiguous and appear to be incomplete, and are also
67 * needlessly complex.
70 #define DRIVER_DESC "Ethernet Gadget"
71 #define DRIVER_VERSION "May Day 2005"
73 static const char shortname [] = "ether";
74 static const char driver_desc [] = DRIVER_DESC;
76 #define RX_EXTRA 20 /* guard against rx overflows */
78 #include "rndis.h"
80 #ifndef CONFIG_USB_ETH_RNDIS
81 #define rndis_uninit(x) do{}while(0)
82 #define rndis_deregister(c) do{}while(0)
83 #define rndis_exit() do{}while(0)
84 #endif
86 /* CDC and RNDIS support the same host-chosen outgoing packet filters. */
87 #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
88 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
89 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
90 |USB_CDC_PACKET_TYPE_DIRECTED)
93 /*-------------------------------------------------------------------------*/
95 struct eth_dev {
96 spinlock_t lock;
97 struct usb_gadget *gadget;
98 struct usb_request *req; /* for control responses */
99 struct usb_request *stat_req; /* for cdc & rndis status */
101 u8 config;
102 struct usb_ep *in_ep, *out_ep, *status_ep;
103 const struct usb_endpoint_descriptor
104 *in, *out, *status;
106 spinlock_t req_lock;
107 struct list_head tx_reqs, rx_reqs;
109 struct net_device *net;
110 struct net_device_stats stats;
111 atomic_t tx_qlen;
113 struct work_struct work;
114 unsigned zlp:1;
115 unsigned cdc:1;
116 unsigned rndis:1;
117 unsigned suspended:1;
118 u16 cdc_filter;
119 unsigned long todo;
120 #define WORK_RX_MEMORY 0
121 int rndis_config;
122 u8 host_mac [ETH_ALEN];
125 /* This version autoconfigures as much as possible at run-time.
127 * It also ASSUMES a self-powered device, without remote wakeup,
128 * although remote wakeup support would make sense.
131 /*-------------------------------------------------------------------------*/
133 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
134 * Instead: allocate your own, using normal USB-IF procedures.
137 /* Thanks to NetChip Technologies for donating this product ID.
138 * It's for devices with only CDC Ethernet configurations.
140 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
141 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
143 /* For hardware that can't talk CDC, we use the same vendor ID that
144 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
145 * with pxa250. We're protocol-compatible, if the host-side drivers
146 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
147 * drivers that need to hard-wire endpoint numbers have a hook.
149 * The protocol is a minimal subset of CDC Ether, which works on any bulk
150 * hardware that's not deeply broken ... even on hardware that can't talk
151 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
152 * doesn't handle control-OUT).
154 #define SIMPLE_VENDOR_NUM 0x049f
155 #define SIMPLE_PRODUCT_NUM 0x505a
157 /* For hardware that can talk RNDIS and either of the above protocols,
158 * use this ID ... the windows INF files will know it. Unless it's
159 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
160 * the non-RNDIS configuration.
162 #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
163 #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
166 /* Some systems will want different product identifers published in the
167 * device descriptor, either numbers or strings or both. These string
168 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
171 static ushort idVendor;
172 module_param(idVendor, ushort, S_IRUGO);
173 MODULE_PARM_DESC(idVendor, "USB Vendor ID");
175 static ushort idProduct;
176 module_param(idProduct, ushort, S_IRUGO);
177 MODULE_PARM_DESC(idProduct, "USB Product ID");
179 static ushort bcdDevice;
180 module_param(bcdDevice, ushort, S_IRUGO);
181 MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
183 static char *iManufacturer;
184 module_param(iManufacturer, charp, S_IRUGO);
185 MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
187 static char *iProduct;
188 module_param(iProduct, charp, S_IRUGO);
189 MODULE_PARM_DESC(iProduct, "USB Product string");
191 static char *iSerialNumber;
192 module_param(iSerialNumber, charp, S_IRUGO);
193 MODULE_PARM_DESC(iSerialNumber, "SerialNumber");
195 /* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */
196 static char *dev_addr;
197 module_param(dev_addr, charp, S_IRUGO);
198 MODULE_PARM_DESC(dev_addr, "Device Ethernet Address");
200 /* this address is invisible to ifconfig */
201 static char *host_addr;
202 module_param(host_addr, charp, S_IRUGO);
203 MODULE_PARM_DESC(host_addr, "Host Ethernet Address");
206 /*-------------------------------------------------------------------------*/
208 /* Include CDC support if we could run on CDC-capable hardware. */
210 #ifdef CONFIG_USB_GADGET_NET2280
211 #define DEV_CONFIG_CDC
212 #endif
214 #ifdef CONFIG_USB_GADGET_DUMMY_HCD
215 #define DEV_CONFIG_CDC
216 #endif
218 #ifdef CONFIG_USB_GADGET_GOKU
219 #define DEV_CONFIG_CDC
220 #endif
222 #ifdef CONFIG_USB_GADGET_LH7A40X
223 #define DEV_CONFIG_CDC
224 #endif
226 #ifdef CONFIG_USB_GADGET_MQ11XX
227 #define DEV_CONFIG_CDC
228 #endif
230 #ifdef CONFIG_USB_GADGET_OMAP
231 #define DEV_CONFIG_CDC
232 #endif
234 #ifdef CONFIG_USB_GADGET_N9604
235 #define DEV_CONFIG_CDC
236 #endif
238 #ifdef CONFIG_USB_GADGET_PXA27X
239 #define DEV_CONFIG_CDC
240 #endif
242 #ifdef CONFIG_USB_GADGET_S3C2410
243 #define DEV_CONFIG_CDC
244 #endif
246 #ifdef CONFIG_USB_GADGET_AT91
247 #define DEV_CONFIG_CDC
248 #endif
250 #ifdef CONFIG_USB_GADGET_MUSBHSFC
251 #define DEV_CONFIG_CDC
252 #endif
254 #ifdef CONFIG_USB_GADGET_MUSB_HDRC
255 #define DEV_CONFIG_CDC
256 #endif
258 #ifdef CONFIG_USB_GADGET_ATMEL_USBA
259 #define DEV_CONFIG_CDC
260 #endif
262 #ifdef CONFIG_USB_GADGET_FSL_USB2
263 #define DEV_CONFIG_CDC
264 #endif
266 /* For CDC-incapable hardware, choose the simple cdc subset.
267 * Anything that talks bulk (without notable bugs) can do this.
269 #ifdef CONFIG_USB_GADGET_PXA2XX
270 #define DEV_CONFIG_SUBSET
271 #endif
273 #ifdef CONFIG_USB_GADGET_SUPERH
274 #define DEV_CONFIG_SUBSET
275 #endif
277 #ifdef CONFIG_USB_GADGET_SA1100
278 /* use non-CDC for backwards compatibility */
279 #define DEV_CONFIG_SUBSET
280 #endif
282 #ifdef CONFIG_USB_GADGET_M66592
283 #define DEV_CONFIG_CDC
284 #endif
286 #ifdef CONFIG_USB_GADGET_AMD5536UDC
287 #define DEV_CONFIG_CDC
288 #endif
291 /*-------------------------------------------------------------------------*/
293 /* "main" config is either CDC, or its simple subset */
294 static inline int is_cdc(struct eth_dev *dev)
296 #if !defined(DEV_CONFIG_SUBSET)
297 return 1; /* only cdc possible */
298 #elif !defined (DEV_CONFIG_CDC)
299 return 0; /* only subset possible */
300 #else
301 return dev->cdc; /* depends on what hardware we found */
302 #endif
305 /* "secondary" RNDIS config may sometimes be activated */
306 static inline int rndis_active(struct eth_dev *dev)
308 #ifdef CONFIG_USB_ETH_RNDIS
309 return dev->rndis;
310 #else
311 return 0;
312 #endif
315 #define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
316 #define cdc_active(dev) ( is_cdc(dev) && !rndis_active(dev))
320 #define DEFAULT_QLEN 2 /* double buffering by default */
322 /* peak bulk transfer bits-per-second */
323 #define HS_BPS (13 * 512 * 8 * 1000 * 8)
324 #define FS_BPS (19 * 64 * 1 * 1000 * 8)
326 #ifdef CONFIG_USB_GADGET_DUALSPEED
327 #define DEVSPEED USB_SPEED_HIGH
329 static unsigned qmult = 5;
330 module_param (qmult, uint, S_IRUGO|S_IWUSR);
333 /* for dual-speed hardware, use deeper queues at highspeed */
334 #define qlen(gadget) \
335 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
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) */
344 #define qmult 1
346 #define DEVSPEED USB_SPEED_FULL
348 #define qlen(gadget) DEFAULT_QLEN
350 static inline int BITRATE(struct usb_gadget *g)
352 return FS_BPS;
354 #endif
357 /*-------------------------------------------------------------------------*/
359 #define xprintk(d,level,fmt,args...) \
360 printk(level "%s: " fmt , (d)->net->name , ## args)
362 #ifdef DEBUG
363 #undef DEBUG
364 #define DEBUG(dev,fmt,args...) \
365 xprintk(dev , KERN_DEBUG , fmt , ## args)
366 #else
367 #define DEBUG(dev,fmt,args...) \
368 do { } while (0)
369 #endif /* DEBUG */
371 #ifdef VERBOSE_DEBUG
372 #define VDEBUG DEBUG
373 #else
374 #define VDEBUG(dev,fmt,args...) \
375 do { } while (0)
376 #endif /* DEBUG */
378 #define ERROR(dev,fmt,args...) \
379 xprintk(dev , KERN_ERR , fmt , ## args)
380 #define WARN(dev,fmt,args...) \
381 xprintk(dev , KERN_WARNING , fmt , ## args)
382 #define INFO(dev,fmt,args...) \
383 xprintk(dev , KERN_INFO , fmt , ## args)
385 /*-------------------------------------------------------------------------*/
387 /* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
388 * ep0 implementation: descriptors, config management, setup().
389 * also optional class-specific notification interrupt transfer.
393 * DESCRIPTORS ... most are static, but strings and (full) configuration
394 * descriptors are built on demand. For now we do either full CDC, or
395 * our simple subset, with RNDIS as an optional second configuration.
397 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
398 * the class descriptors match a modem (they're ignored; it's really just
399 * Ethernet functionality), they don't need the NOP altsetting, and the
400 * status transfer endpoint isn't optional.
403 #define STRING_MANUFACTURER 1
404 #define STRING_PRODUCT 2
405 #define STRING_ETHADDR 3
406 #define STRING_DATA 4
407 #define STRING_CONTROL 5
408 #define STRING_RNDIS_CONTROL 6
409 #define STRING_CDC 7
410 #define STRING_SUBSET 8
411 #define STRING_RNDIS 9
412 #define STRING_SERIALNUMBER 10
414 /* holds our biggest descriptor (or RNDIS response) */
415 #define USB_BUFSIZ 256
418 * This device advertises one configuration, eth_config, unless RNDIS
419 * is enabled (rndis_config) on hardware supporting at least two configs.
421 * NOTE: Controllers like superh_udc should probably be able to use
422 * an RNDIS-only configuration.
424 * FIXME define some higher-powered configurations to make it easier
425 * to recharge batteries ...
428 #define DEV_CONFIG_VALUE 1 /* cdc or subset */
429 #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
431 static struct usb_device_descriptor
432 device_desc = {
433 .bLength = sizeof device_desc,
434 .bDescriptorType = USB_DT_DEVICE,
436 .bcdUSB = __constant_cpu_to_le16 (0x0200),
438 .bDeviceClass = USB_CLASS_COMM,
439 .bDeviceSubClass = 0,
440 .bDeviceProtocol = 0,
442 .idVendor = __constant_cpu_to_le16 (CDC_VENDOR_NUM),
443 .idProduct = __constant_cpu_to_le16 (CDC_PRODUCT_NUM),
444 .iManufacturer = STRING_MANUFACTURER,
445 .iProduct = STRING_PRODUCT,
446 .bNumConfigurations = 1,
449 static struct usb_otg_descriptor
450 otg_descriptor = {
451 .bLength = sizeof otg_descriptor,
452 .bDescriptorType = USB_DT_OTG,
454 .bmAttributes = USB_OTG_SRP,
457 static struct usb_config_descriptor
458 eth_config = {
459 .bLength = sizeof eth_config,
460 .bDescriptorType = USB_DT_CONFIG,
462 /* compute wTotalLength on the fly */
463 .bNumInterfaces = 2,
464 .bConfigurationValue = DEV_CONFIG_VALUE,
465 .iConfiguration = STRING_CDC,
466 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
467 .bMaxPower = 50,
470 #ifdef CONFIG_USB_ETH_RNDIS
471 static struct usb_config_descriptor
472 rndis_config = {
473 .bLength = sizeof rndis_config,
474 .bDescriptorType = USB_DT_CONFIG,
476 /* compute wTotalLength on the fly */
477 .bNumInterfaces = 2,
478 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
479 .iConfiguration = STRING_RNDIS,
480 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
481 .bMaxPower = 50,
483 #endif
486 * Compared to the simple CDC subset, the full CDC Ethernet model adds
487 * three class descriptors, two interface descriptors, optional status
488 * endpoint. Both have a "data" interface and two bulk endpoints.
489 * There are also differences in how control requests are handled.
491 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
492 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
493 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
494 * work around those bugs) are unlikely to ever come from MSFT, you may
495 * wish to avoid using RNDIS.
497 * MCCI offers an alternative to RNDIS if you need to connect to Windows
498 * but have hardware that can't support CDC Ethernet. We add descriptors
499 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
500 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
501 * get those drivers from MCCI, or bundled with various products.
504 #ifdef DEV_CONFIG_CDC
505 static struct usb_interface_descriptor
506 control_intf = {
507 .bLength = sizeof control_intf,
508 .bDescriptorType = USB_DT_INTERFACE,
510 .bInterfaceNumber = 0,
511 /* status endpoint is optional; this may be patched later */
512 .bNumEndpoints = 1,
513 .bInterfaceClass = USB_CLASS_COMM,
514 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
515 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
516 .iInterface = STRING_CONTROL,
518 #endif
520 #ifdef CONFIG_USB_ETH_RNDIS
521 static const struct usb_interface_descriptor
522 rndis_control_intf = {
523 .bLength = sizeof rndis_control_intf,
524 .bDescriptorType = USB_DT_INTERFACE,
526 .bInterfaceNumber = 0,
527 .bNumEndpoints = 1,
528 .bInterfaceClass = USB_CLASS_COMM,
529 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
530 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
531 .iInterface = STRING_RNDIS_CONTROL,
533 #endif
535 static const struct usb_cdc_header_desc header_desc = {
536 .bLength = sizeof header_desc,
537 .bDescriptorType = USB_DT_CS_INTERFACE,
538 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
540 .bcdCDC = __constant_cpu_to_le16 (0x0110),
543 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
545 static const struct usb_cdc_union_desc union_desc = {
546 .bLength = sizeof union_desc,
547 .bDescriptorType = USB_DT_CS_INTERFACE,
548 .bDescriptorSubType = USB_CDC_UNION_TYPE,
550 .bMasterInterface0 = 0, /* index of control interface */
551 .bSlaveInterface0 = 1, /* index of DATA interface */
554 #endif /* CDC || RNDIS */
556 #ifdef CONFIG_USB_ETH_RNDIS
558 static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
559 .bLength = sizeof call_mgmt_descriptor,
560 .bDescriptorType = USB_DT_CS_INTERFACE,
561 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
563 .bmCapabilities = 0x00,
564 .bDataInterface = 0x01,
567 static const struct usb_cdc_acm_descriptor acm_descriptor = {
568 .bLength = sizeof acm_descriptor,
569 .bDescriptorType = USB_DT_CS_INTERFACE,
570 .bDescriptorSubType = USB_CDC_ACM_TYPE,
572 .bmCapabilities = 0x00,
575 #endif
577 #ifndef DEV_CONFIG_CDC
579 /* "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
580 * ways: data endpoints live in the control interface, there's no data
581 * interface, and it's not used to talk to a cell phone radio.
584 static const struct usb_cdc_mdlm_desc mdlm_desc = {
585 .bLength = sizeof mdlm_desc,
586 .bDescriptorType = USB_DT_CS_INTERFACE,
587 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
589 .bcdVersion = __constant_cpu_to_le16(0x0100),
590 .bGUID = {
591 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
592 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
596 /* since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
597 * can't really use its struct. All we do here is say that we're using
598 * the submode of "SAFE" which directly matches the CDC Subset.
600 static const u8 mdlm_detail_desc[] = {
602 USB_DT_CS_INTERFACE,
603 USB_CDC_MDLM_DETAIL_TYPE,
605 0, /* "SAFE" */
606 0, /* network control capabilities (none) */
607 0, /* network data capabilities ("raw" encapsulation) */
610 #endif
612 static const struct usb_cdc_ether_desc ether_desc = {
613 .bLength = sizeof ether_desc,
614 .bDescriptorType = USB_DT_CS_INTERFACE,
615 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
617 /* this descriptor actually adds value, surprise! */
618 .iMACAddress = STRING_ETHADDR,
619 .bmEthernetStatistics = __constant_cpu_to_le32 (0), /* no statistics */
620 .wMaxSegmentSize = __constant_cpu_to_le16 (ETH_FRAME_LEN),
621 .wNumberMCFilters = __constant_cpu_to_le16 (0),
622 .bNumberPowerFilters = 0,
626 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
628 /* include the status endpoint if we can, even where it's optional.
629 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
630 * packet, to simplify cancellation; and a big transfer interval, to
631 * waste less bandwidth.
633 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
634 * if they ignore the connect/disconnect notifications that real aether
635 * can provide. more advanced cdc configurations might want to support
636 * encapsulated commands (vendor-specific, using control-OUT).
638 * RNDIS requires the status endpoint, since it uses that encapsulation
639 * mechanism for its funky RPC scheme.
642 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
643 #define STATUS_BYTECOUNT 16 /* 8 byte header + data */
645 static struct usb_endpoint_descriptor
646 fs_status_desc = {
647 .bLength = USB_DT_ENDPOINT_SIZE,
648 .bDescriptorType = USB_DT_ENDPOINT,
650 .bEndpointAddress = USB_DIR_IN,
651 .bmAttributes = USB_ENDPOINT_XFER_INT,
652 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
653 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
655 #endif
657 #ifdef DEV_CONFIG_CDC
659 /* the default data interface has no endpoints ... */
661 static const struct usb_interface_descriptor
662 data_nop_intf = {
663 .bLength = sizeof data_nop_intf,
664 .bDescriptorType = USB_DT_INTERFACE,
666 .bInterfaceNumber = 1,
667 .bAlternateSetting = 0,
668 .bNumEndpoints = 0,
669 .bInterfaceClass = USB_CLASS_CDC_DATA,
670 .bInterfaceSubClass = 0,
671 .bInterfaceProtocol = 0,
674 /* ... but the "real" data interface has two bulk endpoints */
676 static const struct usb_interface_descriptor
677 data_intf = {
678 .bLength = sizeof data_intf,
679 .bDescriptorType = USB_DT_INTERFACE,
681 .bInterfaceNumber = 1,
682 .bAlternateSetting = 1,
683 .bNumEndpoints = 2,
684 .bInterfaceClass = USB_CLASS_CDC_DATA,
685 .bInterfaceSubClass = 0,
686 .bInterfaceProtocol = 0,
687 .iInterface = STRING_DATA,
690 #endif
692 #ifdef CONFIG_USB_ETH_RNDIS
694 /* RNDIS doesn't activate by changing to the "real" altsetting */
696 static const struct usb_interface_descriptor
697 rndis_data_intf = {
698 .bLength = sizeof rndis_data_intf,
699 .bDescriptorType = USB_DT_INTERFACE,
701 .bInterfaceNumber = 1,
702 .bAlternateSetting = 0,
703 .bNumEndpoints = 2,
704 .bInterfaceClass = USB_CLASS_CDC_DATA,
705 .bInterfaceSubClass = 0,
706 .bInterfaceProtocol = 0,
707 .iInterface = STRING_DATA,
710 #endif
712 #ifdef DEV_CONFIG_SUBSET
715 * "Simple" CDC-subset option is a simple vendor-neutral model that most
716 * full speed controllers can handle: one interface, two bulk endpoints.
718 * To assist host side drivers, we fancy it up a bit, and add descriptors
719 * so some host side drivers will understand it as a "SAFE" variant.
722 static const struct usb_interface_descriptor
723 subset_data_intf = {
724 .bLength = sizeof subset_data_intf,
725 .bDescriptorType = USB_DT_INTERFACE,
727 .bInterfaceNumber = 0,
728 .bAlternateSetting = 0,
729 .bNumEndpoints = 2,
730 .bInterfaceClass = USB_CLASS_COMM,
731 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
732 .bInterfaceProtocol = 0,
733 .iInterface = STRING_DATA,
736 #endif /* SUBSET */
739 static struct usb_endpoint_descriptor
740 fs_source_desc = {
741 .bLength = USB_DT_ENDPOINT_SIZE,
742 .bDescriptorType = USB_DT_ENDPOINT,
744 .bEndpointAddress = USB_DIR_IN,
745 .bmAttributes = USB_ENDPOINT_XFER_BULK,
748 static struct usb_endpoint_descriptor
749 fs_sink_desc = {
750 .bLength = USB_DT_ENDPOINT_SIZE,
751 .bDescriptorType = USB_DT_ENDPOINT,
753 .bEndpointAddress = USB_DIR_OUT,
754 .bmAttributes = USB_ENDPOINT_XFER_BULK,
757 static const struct usb_descriptor_header *fs_eth_function [11] = {
758 (struct usb_descriptor_header *) &otg_descriptor,
759 #ifdef DEV_CONFIG_CDC
760 /* "cdc" mode descriptors */
761 (struct usb_descriptor_header *) &control_intf,
762 (struct usb_descriptor_header *) &header_desc,
763 (struct usb_descriptor_header *) &union_desc,
764 (struct usb_descriptor_header *) &ether_desc,
765 /* NOTE: status endpoint may need to be removed */
766 (struct usb_descriptor_header *) &fs_status_desc,
767 /* data interface, with altsetting */
768 (struct usb_descriptor_header *) &data_nop_intf,
769 (struct usb_descriptor_header *) &data_intf,
770 (struct usb_descriptor_header *) &fs_source_desc,
771 (struct usb_descriptor_header *) &fs_sink_desc,
772 NULL,
773 #endif /* DEV_CONFIG_CDC */
776 static inline void __init fs_subset_descriptors(void)
778 #ifdef DEV_CONFIG_SUBSET
779 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
780 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
781 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
782 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
783 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
784 fs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
785 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
786 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
787 fs_eth_function[8] = NULL;
788 #else
789 fs_eth_function[1] = NULL;
790 #endif
793 #ifdef CONFIG_USB_ETH_RNDIS
794 static const struct usb_descriptor_header *fs_rndis_function [] = {
795 (struct usb_descriptor_header *) &otg_descriptor,
796 /* control interface matches ACM, not Ethernet */
797 (struct usb_descriptor_header *) &rndis_control_intf,
798 (struct usb_descriptor_header *) &header_desc,
799 (struct usb_descriptor_header *) &call_mgmt_descriptor,
800 (struct usb_descriptor_header *) &acm_descriptor,
801 (struct usb_descriptor_header *) &union_desc,
802 (struct usb_descriptor_header *) &fs_status_desc,
803 /* data interface has no altsetting */
804 (struct usb_descriptor_header *) &rndis_data_intf,
805 (struct usb_descriptor_header *) &fs_source_desc,
806 (struct usb_descriptor_header *) &fs_sink_desc,
807 NULL,
809 #endif
812 * usb 2.0 devices need to expose both high speed and full speed
813 * descriptors, unless they only run at full speed.
816 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
817 static struct usb_endpoint_descriptor
818 hs_status_desc = {
819 .bLength = USB_DT_ENDPOINT_SIZE,
820 .bDescriptorType = USB_DT_ENDPOINT,
822 .bmAttributes = USB_ENDPOINT_XFER_INT,
823 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
824 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
826 #endif /* DEV_CONFIG_CDC */
828 static struct usb_endpoint_descriptor
829 hs_source_desc = {
830 .bLength = USB_DT_ENDPOINT_SIZE,
831 .bDescriptorType = USB_DT_ENDPOINT,
833 .bmAttributes = USB_ENDPOINT_XFER_BULK,
834 .wMaxPacketSize = __constant_cpu_to_le16 (512),
837 static struct usb_endpoint_descriptor
838 hs_sink_desc = {
839 .bLength = USB_DT_ENDPOINT_SIZE,
840 .bDescriptorType = USB_DT_ENDPOINT,
842 .bmAttributes = USB_ENDPOINT_XFER_BULK,
843 .wMaxPacketSize = __constant_cpu_to_le16 (512),
846 static struct usb_qualifier_descriptor
847 dev_qualifier = {
848 .bLength = sizeof dev_qualifier,
849 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
851 .bcdUSB = __constant_cpu_to_le16 (0x0200),
852 .bDeviceClass = USB_CLASS_COMM,
854 .bNumConfigurations = 1,
857 static const struct usb_descriptor_header *hs_eth_function [11] = {
858 (struct usb_descriptor_header *) &otg_descriptor,
859 #ifdef DEV_CONFIG_CDC
860 /* "cdc" mode descriptors */
861 (struct usb_descriptor_header *) &control_intf,
862 (struct usb_descriptor_header *) &header_desc,
863 (struct usb_descriptor_header *) &union_desc,
864 (struct usb_descriptor_header *) &ether_desc,
865 /* NOTE: status endpoint may need to be removed */
866 (struct usb_descriptor_header *) &hs_status_desc,
867 /* data interface, with altsetting */
868 (struct usb_descriptor_header *) &data_nop_intf,
869 (struct usb_descriptor_header *) &data_intf,
870 (struct usb_descriptor_header *) &hs_source_desc,
871 (struct usb_descriptor_header *) &hs_sink_desc,
872 NULL,
873 #endif /* DEV_CONFIG_CDC */
876 static inline void __init hs_subset_descriptors(void)
878 #ifdef DEV_CONFIG_SUBSET
879 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
880 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
881 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
882 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
883 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
884 hs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
885 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
886 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
887 hs_eth_function[8] = NULL;
888 #else
889 hs_eth_function[1] = NULL;
890 #endif
893 #ifdef CONFIG_USB_ETH_RNDIS
894 static const struct usb_descriptor_header *hs_rndis_function [] = {
895 (struct usb_descriptor_header *) &otg_descriptor,
896 /* control interface matches ACM, not Ethernet */
897 (struct usb_descriptor_header *) &rndis_control_intf,
898 (struct usb_descriptor_header *) &header_desc,
899 (struct usb_descriptor_header *) &call_mgmt_descriptor,
900 (struct usb_descriptor_header *) &acm_descriptor,
901 (struct usb_descriptor_header *) &union_desc,
902 (struct usb_descriptor_header *) &hs_status_desc,
903 /* data interface has no altsetting */
904 (struct usb_descriptor_header *) &rndis_data_intf,
905 (struct usb_descriptor_header *) &hs_source_desc,
906 (struct usb_descriptor_header *) &hs_sink_desc,
907 NULL,
909 #endif
912 /* maxpacket and other transfer characteristics vary by speed. */
913 static inline struct usb_endpoint_descriptor *
914 ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
915 struct usb_endpoint_descriptor *fs)
917 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
918 return hs;
919 return fs;
923 /*-------------------------------------------------------------------------*/
925 /* descriptors that are built on-demand */
927 static char manufacturer [50];
928 static char product_desc [40] = DRIVER_DESC;
929 static char serial_number [20];
931 /* address that the host will use ... usually assigned at random */
932 static char ethaddr [2 * ETH_ALEN + 1];
934 /* static strings, in UTF-8 */
935 static struct usb_string strings [] = {
936 { STRING_MANUFACTURER, manufacturer, },
937 { STRING_PRODUCT, product_desc, },
938 { STRING_SERIALNUMBER, serial_number, },
939 { STRING_DATA, "Ethernet Data", },
940 { STRING_ETHADDR, ethaddr, },
941 #ifdef DEV_CONFIG_CDC
942 { STRING_CDC, "CDC Ethernet", },
943 { STRING_CONTROL, "CDC Communications Control", },
944 #endif
945 #ifdef DEV_CONFIG_SUBSET
946 { STRING_SUBSET, "CDC Ethernet Subset", },
947 #endif
948 #ifdef CONFIG_USB_ETH_RNDIS
949 { STRING_RNDIS, "RNDIS", },
950 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
951 #endif
952 { } /* end of list */
955 static struct usb_gadget_strings stringtab = {
956 .language = 0x0409, /* en-us */
957 .strings = strings,
961 * one config, two interfaces: control, data.
962 * complications: class descriptors, and an altsetting.
964 static int
965 config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
967 int len;
968 const struct usb_config_descriptor *config;
969 const struct usb_descriptor_header **function;
970 int hs = 0;
972 if (gadget_is_dualspeed(g)) {
973 hs = (g->speed == USB_SPEED_HIGH);
974 if (type == USB_DT_OTHER_SPEED_CONFIG)
975 hs = !hs;
977 #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
979 if (index >= device_desc.bNumConfigurations)
980 return -EINVAL;
982 #ifdef CONFIG_USB_ETH_RNDIS
983 /* list the RNDIS config first, to make Microsoft's drivers
984 * happy. DOCSIS 1.0 needs this too.
986 if (device_desc.bNumConfigurations == 2 && index == 0) {
987 config = &rndis_config;
988 function = which_fn (rndis);
989 } else
990 #endif
992 config = &eth_config;
993 function = which_fn (eth);
996 /* for now, don't advertise srp-only devices */
997 if (!is_otg)
998 function++;
1000 len = usb_gadget_config_buf (config, buf, USB_BUFSIZ, function);
1001 if (len < 0)
1002 return len;
1003 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
1004 return len;
1007 /*-------------------------------------------------------------------------*/
1009 static void eth_start (struct eth_dev *dev, gfp_t gfp_flags);
1010 static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
1012 static int
1013 set_ether_config (struct eth_dev *dev, gfp_t gfp_flags)
1015 int result = 0;
1016 struct usb_gadget *gadget = dev->gadget;
1018 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
1019 /* status endpoint used for RNDIS and (optionally) CDC */
1020 if (!subset_active(dev) && dev->status_ep) {
1021 dev->status = ep_desc (gadget, &hs_status_desc,
1022 &fs_status_desc);
1023 dev->status_ep->driver_data = dev;
1025 result = usb_ep_enable (dev->status_ep, dev->status);
1026 if (result != 0) {
1027 DEBUG (dev, "enable %s --> %d\n",
1028 dev->status_ep->name, result);
1029 goto done;
1032 #endif
1034 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
1035 dev->in_ep->driver_data = dev;
1037 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
1038 dev->out_ep->driver_data = dev;
1040 /* With CDC, the host isn't allowed to use these two data
1041 * endpoints in the default altsetting for the interface.
1042 * so we don't activate them yet. Reset from SET_INTERFACE.
1044 * Strictly speaking RNDIS should work the same: activation is
1045 * a side effect of setting a packet filter. Deactivation is
1046 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
1048 if (!cdc_active(dev)) {
1049 result = usb_ep_enable (dev->in_ep, dev->in);
1050 if (result != 0) {
1051 DEBUG(dev, "enable %s --> %d\n",
1052 dev->in_ep->name, result);
1053 goto done;
1056 result = usb_ep_enable (dev->out_ep, dev->out);
1057 if (result != 0) {
1058 DEBUG (dev, "enable %s --> %d\n",
1059 dev->out_ep->name, result);
1060 goto done;
1064 done:
1065 if (result == 0)
1066 result = alloc_requests (dev, qlen (gadget), gfp_flags);
1068 /* on error, disable any endpoints */
1069 if (result < 0) {
1070 if (!subset_active(dev) && dev->status_ep)
1071 (void) usb_ep_disable (dev->status_ep);
1072 dev->status = NULL;
1073 (void) usb_ep_disable (dev->in_ep);
1074 (void) usb_ep_disable (dev->out_ep);
1075 dev->in = NULL;
1076 dev->out = NULL;
1079 /* activate non-CDC configs right away
1080 * this isn't strictly according to the RNDIS spec
1082 else if (!cdc_active (dev)) {
1083 netif_carrier_on (dev->net);
1084 if (netif_running (dev->net)) {
1085 spin_unlock (&dev->lock);
1086 eth_start (dev, GFP_ATOMIC);
1087 spin_lock (&dev->lock);
1091 if (result == 0)
1092 DEBUG (dev, "qlen %d\n", qlen (gadget));
1094 /* caller is responsible for cleanup on error */
1095 return result;
1098 static void eth_reset_config (struct eth_dev *dev)
1100 struct usb_request *req;
1102 if (dev->config == 0)
1103 return;
1105 DEBUG (dev, "%s\n", __FUNCTION__);
1107 netif_stop_queue (dev->net);
1108 netif_carrier_off (dev->net);
1109 rndis_uninit(dev->rndis_config);
1111 /* disable endpoints, forcing (synchronous) completion of
1112 * pending i/o. then free the requests.
1114 if (dev->in) {
1115 usb_ep_disable (dev->in_ep);
1116 spin_lock(&dev->req_lock);
1117 while (likely (!list_empty (&dev->tx_reqs))) {
1118 req = container_of (dev->tx_reqs.next,
1119 struct usb_request, list);
1120 list_del (&req->list);
1122 spin_unlock(&dev->req_lock);
1123 usb_ep_free_request (dev->in_ep, req);
1124 spin_lock(&dev->req_lock);
1126 spin_unlock(&dev->req_lock);
1128 if (dev->out) {
1129 usb_ep_disable (dev->out_ep);
1130 spin_lock(&dev->req_lock);
1131 while (likely (!list_empty (&dev->rx_reqs))) {
1132 req = container_of (dev->rx_reqs.next,
1133 struct usb_request, list);
1134 list_del (&req->list);
1136 spin_unlock(&dev->req_lock);
1137 usb_ep_free_request (dev->out_ep, req);
1138 spin_lock(&dev->req_lock);
1140 spin_unlock(&dev->req_lock);
1143 if (dev->status) {
1144 usb_ep_disable (dev->status_ep);
1146 dev->rndis = 0;
1147 dev->cdc_filter = 0;
1148 dev->config = 0;
1151 /* change our operational config. must agree with the code
1152 * that returns config descriptors, and altsetting code.
1154 static int
1155 eth_set_config (struct eth_dev *dev, unsigned number, gfp_t gfp_flags)
1157 int result = 0;
1158 struct usb_gadget *gadget = dev->gadget;
1160 if (gadget_is_sa1100 (gadget)
1161 && dev->config
1162 && atomic_read (&dev->tx_qlen) != 0) {
1163 /* tx fifo is full, but we can't clear it...*/
1164 INFO (dev, "can't change configurations\n");
1165 return -ESPIPE;
1167 eth_reset_config (dev);
1169 switch (number) {
1170 case DEV_CONFIG_VALUE:
1171 result = set_ether_config (dev, gfp_flags);
1172 break;
1173 #ifdef CONFIG_USB_ETH_RNDIS
1174 case DEV_RNDIS_CONFIG_VALUE:
1175 dev->rndis = 1;
1176 result = set_ether_config (dev, gfp_flags);
1177 break;
1178 #endif
1179 default:
1180 result = -EINVAL;
1181 /* FALL THROUGH */
1182 case 0:
1183 break;
1186 if (result) {
1187 if (number)
1188 eth_reset_config (dev);
1189 usb_gadget_vbus_draw(dev->gadget,
1190 gadget_is_otg(dev->gadget) ? 8 : 100);
1191 } else {
1192 char *speed;
1193 unsigned power;
1195 power = 2 * eth_config.bMaxPower;
1196 usb_gadget_vbus_draw(dev->gadget, power);
1198 switch (gadget->speed) {
1199 case USB_SPEED_FULL: speed = "full"; break;
1200 #ifdef CONFIG_USB_GADGET_DUALSPEED
1201 case USB_SPEED_HIGH: speed = "high"; break;
1202 #endif
1203 default: speed = "?"; break;
1206 dev->config = number;
1207 INFO (dev, "%s speed config #%d: %d mA, %s, using %s\n",
1208 speed, number, power, driver_desc,
1209 rndis_active(dev)
1210 ? "RNDIS"
1211 : (cdc_active(dev)
1212 ? "CDC Ethernet"
1213 : "CDC Ethernet Subset"));
1215 return result;
1218 /*-------------------------------------------------------------------------*/
1220 #ifdef DEV_CONFIG_CDC
1222 /* The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
1223 * only to notify the host about link status changes (which we support) or
1224 * report completion of some encapsulated command (as used in RNDIS). Since
1225 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1226 * command mechanism; and only one status request is ever queued.
1229 static void eth_status_complete (struct usb_ep *ep, struct usb_request *req)
1231 struct usb_cdc_notification *event = req->buf;
1232 int value = req->status;
1233 struct eth_dev *dev = ep->driver_data;
1235 /* issue the second notification if host reads the first */
1236 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1237 && value == 0) {
1238 __le32 *data = req->buf + sizeof *event;
1240 event->bmRequestType = 0xA1;
1241 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
1242 event->wValue = __constant_cpu_to_le16 (0);
1243 event->wIndex = __constant_cpu_to_le16 (1);
1244 event->wLength = __constant_cpu_to_le16 (8);
1246 /* SPEED_CHANGE data is up/down speeds in bits/sec */
1247 data [0] = data [1] = cpu_to_le32 (BITRATE (dev->gadget));
1249 req->length = STATUS_BYTECOUNT;
1250 value = usb_ep_queue (ep, req, GFP_ATOMIC);
1251 DEBUG (dev, "send SPEED_CHANGE --> %d\n", value);
1252 if (value == 0)
1253 return;
1254 } else if (value != -ECONNRESET)
1255 DEBUG (dev, "event %02x --> %d\n",
1256 event->bNotificationType, value);
1257 req->context = NULL;
1260 static void issue_start_status (struct eth_dev *dev)
1262 struct usb_request *req = dev->stat_req;
1263 struct usb_cdc_notification *event;
1264 int value;
1266 DEBUG (dev, "%s, flush old status first\n", __FUNCTION__);
1268 /* flush old status
1270 * FIXME ugly idiom, maybe we'd be better with just
1271 * a "cancel the whole queue" primitive since any
1272 * unlink-one primitive has way too many error modes.
1273 * here, we "know" toggle is already clear...
1275 * FIXME iff req->context != null just dequeue it
1277 usb_ep_disable (dev->status_ep);
1278 usb_ep_enable (dev->status_ep, dev->status);
1280 /* 3.8.1 says to issue first NETWORK_CONNECTION, then
1281 * a SPEED_CHANGE. could be useful in some configs.
1283 event = req->buf;
1284 event->bmRequestType = 0xA1;
1285 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
1286 event->wValue = __constant_cpu_to_le16 (1); /* connected */
1287 event->wIndex = __constant_cpu_to_le16 (1);
1288 event->wLength = 0;
1290 req->length = sizeof *event;
1291 req->complete = eth_status_complete;
1292 req->context = dev;
1294 value = usb_ep_queue (dev->status_ep, req, GFP_ATOMIC);
1295 if (value < 0)
1296 DEBUG (dev, "status buf queue --> %d\n", value);
1299 #endif
1301 /*-------------------------------------------------------------------------*/
1303 static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req)
1305 if (req->status || req->actual != req->length)
1306 DEBUG ((struct eth_dev *) ep->driver_data,
1307 "setup complete --> %d, %d/%d\n",
1308 req->status, req->actual, req->length);
1311 #ifdef CONFIG_USB_ETH_RNDIS
1313 static void rndis_response_complete (struct usb_ep *ep, struct usb_request *req)
1315 if (req->status || req->actual != req->length)
1316 DEBUG ((struct eth_dev *) ep->driver_data,
1317 "rndis response complete --> %d, %d/%d\n",
1318 req->status, req->actual, req->length);
1320 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1323 static void rndis_command_complete (struct usb_ep *ep, struct usb_request *req)
1325 struct eth_dev *dev = ep->driver_data;
1326 int status;
1328 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1329 spin_lock(&dev->lock);
1330 status = rndis_msg_parser (dev->rndis_config, (u8 *) req->buf);
1331 if (status < 0)
1332 ERROR(dev, "%s: rndis parse error %d\n", __FUNCTION__, status);
1333 spin_unlock(&dev->lock);
1336 #endif /* RNDIS */
1339 * The setup() callback implements all the ep0 functionality that's not
1340 * handled lower down. CDC has a number of less-common features:
1342 * - two interfaces: control, and ethernet data
1343 * - Ethernet data interface has two altsettings: default, and active
1344 * - class-specific descriptors for the control interface
1345 * - class-specific control requests
1347 static int
1348 eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1350 struct eth_dev *dev = get_gadget_data (gadget);
1351 struct usb_request *req = dev->req;
1352 int value = -EOPNOTSUPP;
1353 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1354 u16 wValue = le16_to_cpu(ctrl->wValue);
1355 u16 wLength = le16_to_cpu(ctrl->wLength);
1357 /* descriptors just go into the pre-allocated ep0 buffer,
1358 * while config change events may enable network traffic.
1360 req->complete = eth_setup_complete;
1361 switch (ctrl->bRequest) {
1363 case USB_REQ_GET_DESCRIPTOR:
1364 if (ctrl->bRequestType != USB_DIR_IN)
1365 break;
1366 switch (wValue >> 8) {
1368 case USB_DT_DEVICE:
1369 value = min (wLength, (u16) sizeof device_desc);
1370 memcpy (req->buf, &device_desc, value);
1371 break;
1372 case USB_DT_DEVICE_QUALIFIER:
1373 if (!gadget_is_dualspeed(gadget))
1374 break;
1375 value = min (wLength, (u16) sizeof dev_qualifier);
1376 memcpy (req->buf, &dev_qualifier, value);
1377 break;
1379 case USB_DT_OTHER_SPEED_CONFIG:
1380 if (!gadget_is_dualspeed(gadget))
1381 break;
1382 // FALLTHROUGH
1383 case USB_DT_CONFIG:
1384 value = config_buf(gadget, req->buf,
1385 wValue >> 8,
1386 wValue & 0xff,
1387 gadget_is_otg(gadget));
1388 if (value >= 0)
1389 value = min (wLength, (u16) value);
1390 break;
1392 case USB_DT_STRING:
1393 value = usb_gadget_get_string (&stringtab,
1394 wValue & 0xff, req->buf);
1395 if (value >= 0)
1396 value = min (wLength, (u16) value);
1397 break;
1399 break;
1401 case USB_REQ_SET_CONFIGURATION:
1402 if (ctrl->bRequestType != 0)
1403 break;
1404 if (gadget->a_hnp_support)
1405 DEBUG (dev, "HNP available\n");
1406 else if (gadget->a_alt_hnp_support)
1407 DEBUG (dev, "HNP needs a different root port\n");
1408 spin_lock (&dev->lock);
1409 value = eth_set_config (dev, wValue, GFP_ATOMIC);
1410 spin_unlock (&dev->lock);
1411 break;
1412 case USB_REQ_GET_CONFIGURATION:
1413 if (ctrl->bRequestType != USB_DIR_IN)
1414 break;
1415 *(u8 *)req->buf = dev->config;
1416 value = min (wLength, (u16) 1);
1417 break;
1419 case USB_REQ_SET_INTERFACE:
1420 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1421 || !dev->config
1422 || wIndex > 1)
1423 break;
1424 if (!cdc_active(dev) && wIndex != 0)
1425 break;
1426 spin_lock (&dev->lock);
1428 /* PXA hardware partially handles SET_INTERFACE;
1429 * we need to kluge around that interference.
1431 if (gadget_is_pxa (gadget)) {
1432 value = eth_set_config (dev, DEV_CONFIG_VALUE,
1433 GFP_ATOMIC);
1434 goto done_set_intf;
1437 #ifdef DEV_CONFIG_CDC
1438 switch (wIndex) {
1439 case 0: /* control/master intf */
1440 if (wValue != 0)
1441 break;
1442 if (dev->status) {
1443 usb_ep_disable (dev->status_ep);
1444 usb_ep_enable (dev->status_ep, dev->status);
1446 value = 0;
1447 break;
1448 case 1: /* data intf */
1449 if (wValue > 1)
1450 break;
1451 usb_ep_disable (dev->in_ep);
1452 usb_ep_disable (dev->out_ep);
1454 /* CDC requires the data transfers not be done from
1455 * the default interface setting ... also, setting
1456 * the non-default interface resets filters etc.
1458 if (wValue == 1) {
1459 if (!cdc_active (dev))
1460 break;
1461 usb_ep_enable (dev->in_ep, dev->in);
1462 usb_ep_enable (dev->out_ep, dev->out);
1463 dev->cdc_filter = DEFAULT_FILTER;
1464 netif_carrier_on (dev->net);
1465 if (dev->status)
1466 issue_start_status (dev);
1467 if (netif_running (dev->net)) {
1468 spin_unlock (&dev->lock);
1469 eth_start (dev, GFP_ATOMIC);
1470 spin_lock (&dev->lock);
1472 } else {
1473 netif_stop_queue (dev->net);
1474 netif_carrier_off (dev->net);
1476 value = 0;
1477 break;
1479 #else
1480 /* FIXME this is wrong, as is the assumption that
1481 * all non-PXA hardware talks real CDC ...
1483 dev_warn (&gadget->dev, "set_interface ignored!\n");
1484 #endif /* DEV_CONFIG_CDC */
1486 done_set_intf:
1487 spin_unlock (&dev->lock);
1488 break;
1489 case USB_REQ_GET_INTERFACE:
1490 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1491 || !dev->config
1492 || wIndex > 1)
1493 break;
1494 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
1495 break;
1497 /* for CDC, iff carrier is on, data interface is active. */
1498 if (rndis_active(dev) || wIndex != 1)
1499 *(u8 *)req->buf = 0;
1500 else
1501 *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0;
1502 value = min (wLength, (u16) 1);
1503 break;
1505 #ifdef DEV_CONFIG_CDC
1506 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
1507 /* see 6.2.30: no data, wIndex = interface,
1508 * wValue = packet filter bitmap
1510 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1511 || !cdc_active(dev)
1512 || wLength != 0
1513 || wIndex > 1)
1514 break;
1515 DEBUG (dev, "packet filter %02x\n", wValue);
1516 dev->cdc_filter = wValue;
1517 value = 0;
1518 break;
1520 /* and potentially:
1521 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1522 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1523 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1524 * case USB_CDC_GET_ETHERNET_STATISTIC:
1527 #endif /* DEV_CONFIG_CDC */
1529 #ifdef CONFIG_USB_ETH_RNDIS
1530 /* RNDIS uses the CDC command encapsulation mechanism to implement
1531 * an RPC scheme, with much getting/setting of attributes by OID.
1533 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1534 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1535 || !rndis_active(dev)
1536 || wLength > USB_BUFSIZ
1537 || wValue
1538 || rndis_control_intf.bInterfaceNumber
1539 != wIndex)
1540 break;
1541 /* read the request, then process it */
1542 value = wLength;
1543 req->complete = rndis_command_complete;
1544 /* later, rndis_control_ack () sends a notification */
1545 break;
1547 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1548 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1549 == ctrl->bRequestType
1550 && rndis_active(dev)
1551 // && wLength >= 0x0400
1552 && !wValue
1553 && rndis_control_intf.bInterfaceNumber
1554 == wIndex) {
1555 u8 *buf;
1556 u32 n;
1558 /* return the result */
1559 buf = rndis_get_next_response(dev->rndis_config, &n);
1560 if (buf) {
1561 memcpy(req->buf, buf, n);
1562 req->complete = rndis_response_complete;
1563 rndis_free_response(dev->rndis_config, buf);
1564 <<<<<<< HEAD:drivers/usb/gadget/ether.c
1565 =======
1566 value = n;
1567 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/usb/gadget/ether.c
1569 /* else stalls ... spec says to avoid that */
1571 break;
1572 #endif /* RNDIS */
1574 default:
1575 VDEBUG (dev,
1576 "unknown control req%02x.%02x v%04x i%04x l%d\n",
1577 ctrl->bRequestType, ctrl->bRequest,
1578 wValue, wIndex, wLength);
1581 /* respond with data transfer before status phase? */
1582 if (value >= 0) {
1583 req->length = value;
1584 req->zero = value < wLength
1585 && (value % gadget->ep0->maxpacket) == 0;
1586 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1587 if (value < 0) {
1588 DEBUG (dev, "ep_queue --> %d\n", value);
1589 req->status = 0;
1590 eth_setup_complete (gadget->ep0, req);
1594 /* host either stalls (value < 0) or reports success */
1595 return value;
1598 static void
1599 eth_disconnect (struct usb_gadget *gadget)
1601 struct eth_dev *dev = get_gadget_data (gadget);
1602 unsigned long flags;
1604 spin_lock_irqsave (&dev->lock, flags);
1605 netif_stop_queue (dev->net);
1606 netif_carrier_off (dev->net);
1607 eth_reset_config (dev);
1608 spin_unlock_irqrestore (&dev->lock, flags);
1610 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
1612 /* next we may get setup() calls to enumerate new connections;
1613 * or an unbind() during shutdown (including removing module).
1617 /*-------------------------------------------------------------------------*/
1619 /* NETWORK DRIVER HOOKUP (to the layer above this driver) */
1621 static int eth_change_mtu (struct net_device *net, int new_mtu)
1623 struct eth_dev *dev = netdev_priv(net);
1625 if (dev->rndis)
1626 return -EBUSY;
1628 if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN)
1629 return -ERANGE;
1630 /* no zero-length packet read wanted after mtu-sized packets */
1631 if (((new_mtu + sizeof (struct ethhdr)) % dev->in_ep->maxpacket) == 0)
1632 return -EDOM;
1633 net->mtu = new_mtu;
1634 return 0;
1637 static struct net_device_stats *eth_get_stats (struct net_device *net)
1639 return &((struct eth_dev *)netdev_priv(net))->stats;
1642 static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
1644 struct eth_dev *dev = netdev_priv(net);
1645 strlcpy(p->driver, shortname, sizeof p->driver);
1646 strlcpy(p->version, DRIVER_VERSION, sizeof p->version);
1647 strlcpy(p->fw_version, dev->gadget->name, sizeof p->fw_version);
1648 strlcpy (p->bus_info, dev->gadget->dev.bus_id, sizeof p->bus_info);
1651 static u32 eth_get_link(struct net_device *net)
1653 struct eth_dev *dev = netdev_priv(net);
1654 return dev->gadget->speed != USB_SPEED_UNKNOWN;
1657 static struct ethtool_ops ops = {
1658 .get_drvinfo = eth_get_drvinfo,
1659 .get_link = eth_get_link
1662 static void defer_kevent (struct eth_dev *dev, int flag)
1664 if (test_and_set_bit (flag, &dev->todo))
1665 return;
1666 if (!schedule_work (&dev->work))
1667 ERROR (dev, "kevent %d may have been dropped\n", flag);
1668 else
1669 DEBUG (dev, "kevent %d scheduled\n", flag);
1672 static void rx_complete (struct usb_ep *ep, struct usb_request *req);
1674 static int
1675 rx_submit (struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
1677 struct sk_buff *skb;
1678 int retval = -ENOMEM;
1679 size_t size;
1681 /* Padding up to RX_EXTRA handles minor disagreements with host.
1682 * Normally we use the USB "terminate on short read" convention;
1683 * so allow up to (N*maxpacket), since that memory is normally
1684 * already allocated. Some hardware doesn't deal well with short
1685 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1686 * byte off the end (to force hardware errors on overflow).
1688 * RNDIS uses internal framing, and explicitly allows senders to
1689 * pad to end-of-packet. That's potentially nice for speed,
1690 * but means receivers can't recover synch on their own.
1692 size = (sizeof (struct ethhdr) + dev->net->mtu + RX_EXTRA);
1693 size += dev->out_ep->maxpacket - 1;
1694 if (rndis_active(dev))
1695 size += sizeof (struct rndis_packet_msg_type);
1696 size -= size % dev->out_ep->maxpacket;
1698 skb = alloc_skb(size + NET_IP_ALIGN, gfp_flags);
1699 if (skb == NULL) {
1700 DEBUG (dev, "no rx skb\n");
1701 goto enomem;
1704 /* Some platforms perform better when IP packets are aligned,
1705 * but on at least one, checksumming fails otherwise. Note:
1706 * RNDIS headers involve variable numbers of LE32 values.
1708 skb_reserve(skb, NET_IP_ALIGN);
1710 req->buf = skb->data;
1711 req->length = size;
1712 req->complete = rx_complete;
1713 req->context = skb;
1715 retval = usb_ep_queue (dev->out_ep, req, gfp_flags);
1716 if (retval == -ENOMEM)
1717 enomem:
1718 defer_kevent (dev, WORK_RX_MEMORY);
1719 if (retval) {
1720 DEBUG (dev, "rx submit --> %d\n", retval);
1721 if (skb)
1722 dev_kfree_skb_any(skb);
1723 spin_lock(&dev->req_lock);
1724 list_add (&req->list, &dev->rx_reqs);
1725 spin_unlock(&dev->req_lock);
1727 return retval;
1730 static void rx_complete (struct usb_ep *ep, struct usb_request *req)
1732 struct sk_buff *skb = req->context;
1733 struct eth_dev *dev = ep->driver_data;
1734 int status = req->status;
1736 switch (status) {
1738 /* normal completion */
1739 case 0:
1740 skb_put (skb, req->actual);
1741 /* we know MaxPacketsPerTransfer == 1 here */
1742 if (rndis_active(dev))
1743 status = rndis_rm_hdr (skb);
1744 if (status < 0
1745 || ETH_HLEN > skb->len
1746 || skb->len > ETH_FRAME_LEN) {
1747 dev->stats.rx_errors++;
1748 dev->stats.rx_length_errors++;
1749 DEBUG (dev, "rx length %d\n", skb->len);
1750 break;
1753 skb->protocol = eth_type_trans (skb, dev->net);
1754 dev->stats.rx_packets++;
1755 dev->stats.rx_bytes += skb->len;
1757 /* no buffer copies needed, unless hardware can't
1758 * use skb buffers.
1760 status = netif_rx (skb);
1761 skb = NULL;
1762 break;
1764 /* software-driven interface shutdown */
1765 case -ECONNRESET: // unlink
1766 case -ESHUTDOWN: // disconnect etc
1767 VDEBUG (dev, "rx shutdown, code %d\n", status);
1768 goto quiesce;
1770 /* for hardware automagic (such as pxa) */
1771 case -ECONNABORTED: // endpoint reset
1772 DEBUG (dev, "rx %s reset\n", ep->name);
1773 defer_kevent (dev, WORK_RX_MEMORY);
1774 quiesce:
1775 dev_kfree_skb_any (skb);
1776 goto clean;
1778 /* data overrun */
1779 case -EOVERFLOW:
1780 dev->stats.rx_over_errors++;
1781 // FALLTHROUGH
1783 default:
1784 dev->stats.rx_errors++;
1785 DEBUG (dev, "rx status %d\n", status);
1786 break;
1789 if (skb)
1790 dev_kfree_skb_any (skb);
1791 if (!netif_running (dev->net)) {
1792 clean:
1793 spin_lock(&dev->req_lock);
1794 list_add (&req->list, &dev->rx_reqs);
1795 spin_unlock(&dev->req_lock);
1796 req = NULL;
1798 if (req)
1799 rx_submit (dev, req, GFP_ATOMIC);
1802 static int prealloc (struct list_head *list, struct usb_ep *ep,
1803 unsigned n, gfp_t gfp_flags)
1805 unsigned i;
1806 struct usb_request *req;
1808 if (!n)
1809 return -ENOMEM;
1811 /* queue/recycle up to N requests */
1812 i = n;
1813 list_for_each_entry (req, list, list) {
1814 if (i-- == 0)
1815 goto extra;
1817 while (i--) {
1818 req = usb_ep_alloc_request (ep, gfp_flags);
1819 if (!req)
1820 return list_empty (list) ? -ENOMEM : 0;
1821 list_add (&req->list, list);
1823 return 0;
1825 extra:
1826 /* free extras */
1827 for (;;) {
1828 struct list_head *next;
1830 next = req->list.next;
1831 list_del (&req->list);
1832 usb_ep_free_request (ep, req);
1834 if (next == list)
1835 break;
1837 req = container_of (next, struct usb_request, list);
1839 return 0;
1842 static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
1844 int status;
1846 spin_lock(&dev->req_lock);
1847 status = prealloc (&dev->tx_reqs, dev->in_ep, n, gfp_flags);
1848 if (status < 0)
1849 goto fail;
1850 status = prealloc (&dev->rx_reqs, dev->out_ep, n, gfp_flags);
1851 if (status < 0)
1852 goto fail;
1853 goto done;
1854 fail:
1855 DEBUG (dev, "can't alloc requests\n");
1856 done:
1857 spin_unlock(&dev->req_lock);
1858 return status;
1861 static void rx_fill (struct eth_dev *dev, gfp_t gfp_flags)
1863 struct usb_request *req;
1864 unsigned long flags;
1866 /* fill unused rxq slots with some skb */
1867 spin_lock_irqsave(&dev->req_lock, flags);
1868 while (!list_empty (&dev->rx_reqs)) {
1869 req = container_of (dev->rx_reqs.next,
1870 struct usb_request, list);
1871 list_del_init (&req->list);
1872 spin_unlock_irqrestore(&dev->req_lock, flags);
1874 if (rx_submit (dev, req, gfp_flags) < 0) {
1875 defer_kevent (dev, WORK_RX_MEMORY);
1876 return;
1879 spin_lock_irqsave(&dev->req_lock, flags);
1881 spin_unlock_irqrestore(&dev->req_lock, flags);
1884 static void eth_work (struct work_struct *work)
1886 struct eth_dev *dev = container_of(work, struct eth_dev, work);
1888 if (test_and_clear_bit (WORK_RX_MEMORY, &dev->todo)) {
1889 if (netif_running (dev->net))
1890 rx_fill (dev, GFP_KERNEL);
1893 if (dev->todo)
1894 DEBUG (dev, "work done, flags = 0x%lx\n", dev->todo);
1897 static void tx_complete (struct usb_ep *ep, struct usb_request *req)
1899 struct sk_buff *skb = req->context;
1900 struct eth_dev *dev = ep->driver_data;
1902 switch (req->status) {
1903 default:
1904 dev->stats.tx_errors++;
1905 VDEBUG (dev, "tx err %d\n", req->status);
1906 /* FALLTHROUGH */
1907 case -ECONNRESET: // unlink
1908 case -ESHUTDOWN: // disconnect etc
1909 break;
1910 case 0:
1911 dev->stats.tx_bytes += skb->len;
1913 dev->stats.tx_packets++;
1915 spin_lock(&dev->req_lock);
1916 list_add (&req->list, &dev->tx_reqs);
1917 spin_unlock(&dev->req_lock);
1918 dev_kfree_skb_any (skb);
1920 atomic_dec (&dev->tx_qlen);
1921 if (netif_carrier_ok (dev->net))
1922 netif_wake_queue (dev->net);
1925 static inline int eth_is_promisc (struct eth_dev *dev)
1927 /* no filters for the CDC subset; always promisc */
1928 if (subset_active (dev))
1929 return 1;
1930 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1933 static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1935 struct eth_dev *dev = netdev_priv(net);
1936 int length = skb->len;
1937 int retval;
1938 struct usb_request *req = NULL;
1939 unsigned long flags;
1941 /* apply outgoing CDC or RNDIS filters */
1942 if (!eth_is_promisc (dev)) {
1943 u8 *dest = skb->data;
1945 if (is_multicast_ether_addr(dest)) {
1946 u16 type;
1948 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1949 * SET_ETHERNET_MULTICAST_FILTERS requests
1951 if (is_broadcast_ether_addr(dest))
1952 type = USB_CDC_PACKET_TYPE_BROADCAST;
1953 else
1954 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1955 if (!(dev->cdc_filter & type)) {
1956 dev_kfree_skb_any (skb);
1957 return 0;
1960 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1963 spin_lock_irqsave(&dev->req_lock, flags);
1965 * this freelist can be empty if an interrupt triggered disconnect()
1966 * and reconfigured the gadget (shutting down this queue) after the
1967 * network stack decided to xmit but before we got the spinlock.
1969 if (list_empty(&dev->tx_reqs)) {
1970 spin_unlock_irqrestore(&dev->req_lock, flags);
1971 return 1;
1974 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1975 list_del (&req->list);
1977 /* temporarily stop TX queue when the freelist empties */
1978 if (list_empty (&dev->tx_reqs))
1979 netif_stop_queue (net);
1980 spin_unlock_irqrestore(&dev->req_lock, flags);
1982 /* no buffer copies needed, unless the network stack did it
1983 * or the hardware can't use skb buffers.
1984 * or there's not enough space for any RNDIS headers we need
1986 if (rndis_active(dev)) {
1987 struct sk_buff *skb_rndis;
1989 skb_rndis = skb_realloc_headroom (skb,
1990 sizeof (struct rndis_packet_msg_type));
1991 if (!skb_rndis)
1992 goto drop;
1994 dev_kfree_skb_any (skb);
1995 skb = skb_rndis;
1996 rndis_add_hdr (skb);
1997 length = skb->len;
1999 req->buf = skb->data;
2000 req->context = skb;
2001 req->complete = tx_complete;
2003 /* use zlp framing on tx for strict CDC-Ether conformance,
2004 * though any robust network rx path ignores extra padding.
2005 * and some hardware doesn't like to write zlps.
2007 req->zero = 1;
2008 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2009 length++;
2011 req->length = length;
2013 /* throttle highspeed IRQ rate back slightly */
2014 if (gadget_is_dualspeed(dev->gadget))
2015 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2016 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
2017 : 0;
2019 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
2020 switch (retval) {
2021 default:
2022 DEBUG (dev, "tx queue err %d\n", retval);
2023 break;
2024 case 0:
2025 net->trans_start = jiffies;
2026 atomic_inc (&dev->tx_qlen);
2029 if (retval) {
2030 drop:
2031 dev->stats.tx_dropped++;
2032 dev_kfree_skb_any (skb);
2033 spin_lock_irqsave(&dev->req_lock, flags);
2034 if (list_empty (&dev->tx_reqs))
2035 netif_start_queue (net);
2036 list_add (&req->list, &dev->tx_reqs);
2037 spin_unlock_irqrestore(&dev->req_lock, flags);
2039 return 0;
2042 /*-------------------------------------------------------------------------*/
2044 #ifdef CONFIG_USB_ETH_RNDIS
2046 /* The interrupt endpoint is used in RNDIS to notify the host when messages
2047 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
2048 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
2049 * REMOTE_NDIS_KEEPALIVE_MSG.
2051 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
2052 * normally just one notification will be queued.
2055 static struct usb_request *eth_req_alloc (struct usb_ep *, unsigned, gfp_t);
2056 static void eth_req_free (struct usb_ep *ep, struct usb_request *req);
2058 static void
2059 rndis_control_ack_complete (struct usb_ep *ep, struct usb_request *req)
2061 struct eth_dev *dev = ep->driver_data;
2063 if (req->status || req->actual != req->length)
2064 DEBUG (dev,
2065 "rndis control ack complete --> %d, %d/%d\n",
2066 req->status, req->actual, req->length);
2067 req->context = NULL;
2069 if (req != dev->stat_req)
2070 eth_req_free(ep, req);
2073 static int rndis_control_ack (struct net_device *net)
2075 struct eth_dev *dev = netdev_priv(net);
2076 int length;
2077 struct usb_request *resp = dev->stat_req;
2079 /* in case RNDIS calls this after disconnect */
2080 if (!dev->status) {
2081 DEBUG (dev, "status ENODEV\n");
2082 return -ENODEV;
2085 /* in case queue length > 1 */
2086 if (resp->context) {
2087 resp = eth_req_alloc (dev->status_ep, 8, GFP_ATOMIC);
2088 if (!resp)
2089 return -ENOMEM;
2092 /* Send RNDIS RESPONSE_AVAILABLE notification;
2093 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
2095 resp->length = 8;
2096 resp->complete = rndis_control_ack_complete;
2097 resp->context = dev;
2099 *((__le32 *) resp->buf) = __constant_cpu_to_le32 (1);
2100 *((__le32 *) resp->buf + 1) = __constant_cpu_to_le32 (0);
2102 length = usb_ep_queue (dev->status_ep, resp, GFP_ATOMIC);
2103 if (length < 0) {
2104 resp->status = 0;
2105 rndis_control_ack_complete (dev->status_ep, resp);
2108 return 0;
2111 #else
2113 #define rndis_control_ack NULL
2115 #endif /* RNDIS */
2117 static void eth_start (struct eth_dev *dev, gfp_t gfp_flags)
2119 DEBUG (dev, "%s\n", __FUNCTION__);
2121 /* fill the rx queue */
2122 rx_fill (dev, gfp_flags);
2124 /* and open the tx floodgates */
2125 atomic_set (&dev->tx_qlen, 0);
2126 netif_wake_queue (dev->net);
2127 if (rndis_active(dev)) {
2128 rndis_set_param_medium (dev->rndis_config,
2129 NDIS_MEDIUM_802_3,
2130 BITRATE(dev->gadget)/100);
2131 (void) rndis_signal_connect (dev->rndis_config);
2135 static int eth_open (struct net_device *net)
2137 struct eth_dev *dev = netdev_priv(net);
2139 DEBUG (dev, "%s\n", __FUNCTION__);
2140 if (netif_carrier_ok (dev->net))
2141 eth_start (dev, GFP_KERNEL);
2142 return 0;
2145 static int eth_stop (struct net_device *net)
2147 struct eth_dev *dev = netdev_priv(net);
2149 VDEBUG (dev, "%s\n", __FUNCTION__);
2150 netif_stop_queue (net);
2152 DEBUG (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
2153 dev->stats.rx_packets, dev->stats.tx_packets,
2154 dev->stats.rx_errors, dev->stats.tx_errors
2157 /* ensure there are no more active requests */
2158 if (dev->config) {
2159 usb_ep_disable (dev->in_ep);
2160 usb_ep_disable (dev->out_ep);
2161 if (netif_carrier_ok (dev->net)) {
2162 DEBUG (dev, "host still using in/out endpoints\n");
2163 // FIXME idiom may leave toggle wrong here
2164 usb_ep_enable (dev->in_ep, dev->in);
2165 usb_ep_enable (dev->out_ep, dev->out);
2167 if (dev->status_ep) {
2168 usb_ep_disable (dev->status_ep);
2169 usb_ep_enable (dev->status_ep, dev->status);
2173 if (rndis_active(dev)) {
2174 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
2175 (void) rndis_signal_disconnect (dev->rndis_config);
2178 return 0;
2181 /*-------------------------------------------------------------------------*/
2183 static struct usb_request *
2184 eth_req_alloc (struct usb_ep *ep, unsigned size, gfp_t gfp_flags)
2186 struct usb_request *req;
2188 req = usb_ep_alloc_request (ep, gfp_flags);
2189 if (!req)
2190 return NULL;
2192 req->buf = kmalloc (size, gfp_flags);
2193 if (!req->buf) {
2194 usb_ep_free_request (ep, req);
2195 req = NULL;
2197 return req;
2200 static void
2201 eth_req_free (struct usb_ep *ep, struct usb_request *req)
2203 kfree (req->buf);
2204 usb_ep_free_request (ep, req);
2208 static void /* __init_or_exit */
2209 eth_unbind (struct usb_gadget *gadget)
2211 struct eth_dev *dev = get_gadget_data (gadget);
2213 DEBUG (dev, "unbind\n");
2214 rndis_deregister (dev->rndis_config);
2215 rndis_exit ();
2217 /* we've already been disconnected ... no i/o is active */
2218 if (dev->req) {
2219 eth_req_free (gadget->ep0, dev->req);
2220 dev->req = NULL;
2222 if (dev->stat_req) {
2223 eth_req_free (dev->status_ep, dev->stat_req);
2224 dev->stat_req = NULL;
2227 unregister_netdev (dev->net);
2228 free_netdev(dev->net);
2230 /* assuming we used keventd, it must quiesce too */
2231 flush_scheduled_work ();
2232 set_gadget_data (gadget, NULL);
2235 static u8 __devinit nibble (unsigned char c)
2237 if (likely (isdigit (c)))
2238 return c - '0';
2239 c = toupper (c);
2240 if (likely (isxdigit (c)))
2241 return 10 + c - 'A';
2242 return 0;
2245 static int __devinit get_ether_addr(const char *str, u8 *dev_addr)
2247 if (str) {
2248 unsigned i;
2250 for (i = 0; i < 6; i++) {
2251 unsigned char num;
2253 if((*str == '.') || (*str == ':'))
2254 str++;
2255 num = nibble(*str++) << 4;
2256 num |= (nibble(*str++));
2257 dev_addr [i] = num;
2259 if (is_valid_ether_addr (dev_addr))
2260 return 0;
2262 random_ether_addr(dev_addr);
2263 return 1;
2266 static int __devinit
2267 eth_bind (struct usb_gadget *gadget)
2269 struct eth_dev *dev;
2270 struct net_device *net;
2271 u8 cdc = 1, zlp = 1, rndis = 1;
2272 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
2273 int status = -ENOMEM;
2274 int gcnum;
2276 /* these flags are only ever cleared; compiler take note */
2277 #ifndef DEV_CONFIG_CDC
2278 cdc = 0;
2279 #endif
2280 #ifndef CONFIG_USB_ETH_RNDIS
2281 rndis = 0;
2282 #endif
2284 /* Because most host side USB stacks handle CDC Ethernet, that
2285 * standard protocol is _strongly_ preferred for interop purposes.
2286 * (By everyone except Microsoft.)
2288 if (gadget_is_pxa (gadget)) {
2289 /* pxa doesn't support altsettings */
2290 cdc = 0;
2291 } else if (gadget_is_musbhdrc(gadget)) {
2292 /* reduce tx dma overhead by avoiding special cases */
2293 zlp = 0;
2294 } else if (gadget_is_sh(gadget)) {
2295 /* sh doesn't support multiple interfaces or configs */
2296 cdc = 0;
2297 rndis = 0;
2298 } else if (gadget_is_sa1100 (gadget)) {
2299 /* hardware can't write zlps */
2300 zlp = 0;
2301 /* sa1100 CAN do CDC, without status endpoint ... we use
2302 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2304 cdc = 0;
2307 gcnum = usb_gadget_controller_number (gadget);
2308 if (gcnum >= 0)
2309 device_desc.bcdDevice = cpu_to_le16 (0x0200 + gcnum);
2310 else {
2311 /* can't assume CDC works. don't want to default to
2312 * anything less functional on CDC-capable hardware,
2313 * so we fail in this case.
2315 dev_err (&gadget->dev,
2316 "controller '%s' not recognized\n",
2317 gadget->name);
2318 return -ENODEV;
2320 snprintf (manufacturer, sizeof manufacturer, "%s %s/%s",
2321 init_utsname()->sysname, init_utsname()->release,
2322 gadget->name);
2324 /* If there's an RNDIS configuration, that's what Windows wants to
2325 * be using ... so use these product IDs here and in the "linux.inf"
2326 * needed to install MSFT drivers. Current Linux kernels will use
2327 * the second configuration if it's CDC Ethernet, and need some help
2328 * to choose the right configuration otherwise.
2330 if (rndis) {
2331 device_desc.idVendor =
2332 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2333 device_desc.idProduct =
2334 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2335 snprintf (product_desc, sizeof product_desc,
2336 "RNDIS/%s", driver_desc);
2338 /* CDC subset ... recognized by Linux since 2.4.10, but Windows
2339 * drivers aren't widely available. (That may be improved by
2340 * supporting one submode of the "SAFE" variant of MDLM.)
2342 } else if (!cdc) {
2343 device_desc.idVendor =
2344 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2345 device_desc.idProduct =
2346 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2349 /* support optional vendor/distro customization */
2350 if (idVendor) {
2351 if (!idProduct) {
2352 dev_err (&gadget->dev, "idVendor needs idProduct!\n");
2353 return -ENODEV;
2355 device_desc.idVendor = cpu_to_le16(idVendor);
2356 device_desc.idProduct = cpu_to_le16(idProduct);
2357 if (bcdDevice)
2358 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2360 if (iManufacturer)
2361 strlcpy (manufacturer, iManufacturer, sizeof manufacturer);
2362 if (iProduct)
2363 strlcpy (product_desc, iProduct, sizeof product_desc);
2364 if (iSerialNumber) {
2365 device_desc.iSerialNumber = STRING_SERIALNUMBER,
2366 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
2369 /* all we really need is bulk IN/OUT */
2370 usb_ep_autoconfig_reset (gadget);
2371 in_ep = usb_ep_autoconfig (gadget, &fs_source_desc);
2372 if (!in_ep) {
2373 autoconf_fail:
2374 dev_err (&gadget->dev,
2375 "can't autoconfigure on %s\n",
2376 gadget->name);
2377 return -ENODEV;
2379 in_ep->driver_data = in_ep; /* claim */
2381 out_ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
2382 if (!out_ep)
2383 goto autoconf_fail;
2384 out_ep->driver_data = out_ep; /* claim */
2386 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2387 /* CDC Ethernet control interface doesn't require a status endpoint.
2388 * Since some hosts expect one, try to allocate one anyway.
2390 if (cdc || rndis) {
2391 status_ep = usb_ep_autoconfig (gadget, &fs_status_desc);
2392 if (status_ep) {
2393 status_ep->driver_data = status_ep; /* claim */
2394 } else if (rndis) {
2395 dev_err (&gadget->dev,
2396 "can't run RNDIS on %s\n",
2397 gadget->name);
2398 return -ENODEV;
2399 #ifdef DEV_CONFIG_CDC
2400 /* pxa25x only does CDC subset; often used with RNDIS */
2401 } else if (cdc) {
2402 control_intf.bNumEndpoints = 0;
2403 /* FIXME remove endpoint from descriptor list */
2404 #endif
2407 #endif
2409 /* one config: cdc, else minimal subset */
2410 if (!cdc) {
2411 eth_config.bNumInterfaces = 1;
2412 eth_config.iConfiguration = STRING_SUBSET;
2414 /* use functions to set these up, in case we're built to work
2415 * with multiple controllers and must override CDC Ethernet.
2417 fs_subset_descriptors();
2418 hs_subset_descriptors();
2421 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
2422 usb_gadget_set_selfpowered (gadget);
2424 /* For now RNDIS is always a second config */
2425 if (rndis)
2426 device_desc.bNumConfigurations = 2;
2428 if (gadget_is_dualspeed(gadget)) {
2429 if (rndis)
2430 dev_qualifier.bNumConfigurations = 2;
2431 else if (!cdc)
2432 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2434 /* assumes ep0 uses the same value for both speeds ... */
2435 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2437 /* and that all endpoints are dual-speed */
2438 hs_source_desc.bEndpointAddress =
2439 fs_source_desc.bEndpointAddress;
2440 hs_sink_desc.bEndpointAddress =
2441 fs_sink_desc.bEndpointAddress;
2442 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2443 if (status_ep)
2444 hs_status_desc.bEndpointAddress =
2445 fs_status_desc.bEndpointAddress;
2446 #endif
2449 if (gadget_is_otg(gadget)) {
2450 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2451 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2452 eth_config.bMaxPower = 4;
2453 #ifdef CONFIG_USB_ETH_RNDIS
2454 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2455 rndis_config.bMaxPower = 4;
2456 #endif
2459 net = alloc_etherdev (sizeof *dev);
2460 if (!net)
2461 return status;
2462 dev = netdev_priv(net);
2463 spin_lock_init (&dev->lock);
2464 spin_lock_init (&dev->req_lock);
2465 INIT_WORK (&dev->work, eth_work);
2466 INIT_LIST_HEAD (&dev->tx_reqs);
2467 INIT_LIST_HEAD (&dev->rx_reqs);
2469 /* network device setup */
2470 dev->net = net;
2471 strcpy (net->name, "usb%d");
2472 dev->cdc = cdc;
2473 dev->zlp = zlp;
2475 dev->in_ep = in_ep;
2476 dev->out_ep = out_ep;
2477 dev->status_ep = status_ep;
2479 /* Module params for these addresses should come from ID proms.
2480 * The host side address is used with CDC and RNDIS, and commonly
2481 * ends up in a persistent config database. It's not clear if
2482 * host side code for the SAFE thing cares -- its original BLAN
2483 * thing didn't, Sharp never assigned those addresses on Zaurii.
2485 if (get_ether_addr(dev_addr, net->dev_addr))
2486 dev_warn(&gadget->dev,
2487 "using random %s ethernet address\n", "self");
2488 if (get_ether_addr(host_addr, dev->host_mac))
2489 dev_warn(&gadget->dev,
2490 "using random %s ethernet address\n", "host");
2491 snprintf (ethaddr, sizeof ethaddr, "%02X%02X%02X%02X%02X%02X",
2492 dev->host_mac [0], dev->host_mac [1],
2493 dev->host_mac [2], dev->host_mac [3],
2494 dev->host_mac [4], dev->host_mac [5]);
2496 if (rndis) {
2497 status = rndis_init();
2498 if (status < 0) {
2499 dev_err (&gadget->dev, "can't init RNDIS, %d\n",
2500 status);
2501 goto fail;
2505 net->change_mtu = eth_change_mtu;
2506 net->get_stats = eth_get_stats;
2507 net->hard_start_xmit = eth_start_xmit;
2508 net->open = eth_open;
2509 net->stop = eth_stop;
2510 // watchdog_timeo, tx_timeout ...
2511 // set_multicast_list
2512 SET_ETHTOOL_OPS(net, &ops);
2514 /* preallocate control message data and buffer */
2515 dev->req = eth_req_alloc (gadget->ep0, USB_BUFSIZ, GFP_KERNEL);
2516 if (!dev->req)
2517 goto fail;
2518 dev->req->complete = eth_setup_complete;
2520 /* ... and maybe likewise for status transfer */
2521 #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2522 if (dev->status_ep) {
2523 dev->stat_req = eth_req_alloc (dev->status_ep,
2524 STATUS_BYTECOUNT, GFP_KERNEL);
2525 if (!dev->stat_req) {
2526 eth_req_free (gadget->ep0, dev->req);
2527 goto fail;
2529 dev->stat_req->context = NULL;
2531 #endif
2533 /* finish hookup to lower layer ... */
2534 dev->gadget = gadget;
2535 set_gadget_data (gadget, dev);
2536 gadget->ep0->driver_data = dev;
2538 /* two kinds of host-initiated state changes:
2539 * - iff DATA transfer is active, carrier is "on"
2540 * - tx queueing enabled if open *and* carrier is "on"
2542 netif_stop_queue (dev->net);
2543 netif_carrier_off (dev->net);
2545 SET_NETDEV_DEV (dev->net, &gadget->dev);
2546 status = register_netdev (dev->net);
2547 if (status < 0)
2548 goto fail1;
2550 INFO (dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
2551 INFO (dev, "using %s, OUT %s IN %s%s%s\n", gadget->name,
2552 out_ep->name, in_ep->name,
2553 status_ep ? " STATUS " : "",
2554 status_ep ? status_ep->name : ""
2556 INFO (dev, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2557 net->dev_addr [0], net->dev_addr [1],
2558 net->dev_addr [2], net->dev_addr [3],
2559 net->dev_addr [4], net->dev_addr [5]);
2561 if (cdc || rndis)
2562 INFO (dev, "HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2563 dev->host_mac [0], dev->host_mac [1],
2564 dev->host_mac [2], dev->host_mac [3],
2565 dev->host_mac [4], dev->host_mac [5]);
2567 if (rndis) {
2568 u32 vendorID = 0;
2570 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2572 dev->rndis_config = rndis_register (rndis_control_ack);
2573 if (dev->rndis_config < 0) {
2574 fail0:
2575 unregister_netdev (dev->net);
2576 status = -ENODEV;
2577 goto fail;
2580 /* these set up a lot of the OIDs that RNDIS needs */
2581 rndis_set_host_mac (dev->rndis_config, dev->host_mac);
2582 if (rndis_set_param_dev (dev->rndis_config, dev->net,
2583 &dev->stats, &dev->cdc_filter))
2584 goto fail0;
2585 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2586 manufacturer))
2587 goto fail0;
2588 if (rndis_set_param_medium(dev->rndis_config,
2589 NDIS_MEDIUM_802_3, 0))
2590 goto fail0;
2591 INFO (dev, "RNDIS ready\n");
2594 return status;
2596 fail1:
2597 dev_dbg(&gadget->dev, "register_netdev failed, %d\n", status);
2598 fail:
2599 eth_unbind (gadget);
2600 return status;
2603 /*-------------------------------------------------------------------------*/
2605 static void
2606 eth_suspend (struct usb_gadget *gadget)
2608 struct eth_dev *dev = get_gadget_data (gadget);
2610 DEBUG (dev, "suspend\n");
2611 dev->suspended = 1;
2614 static void
2615 eth_resume (struct usb_gadget *gadget)
2617 struct eth_dev *dev = get_gadget_data (gadget);
2619 DEBUG (dev, "resume\n");
2620 dev->suspended = 0;
2623 /*-------------------------------------------------------------------------*/
2625 static struct usb_gadget_driver eth_driver = {
2626 .speed = DEVSPEED,
2628 .function = (char *) driver_desc,
2629 .bind = eth_bind,
2630 .unbind = eth_unbind,
2632 .setup = eth_setup,
2633 .disconnect = eth_disconnect,
2635 .suspend = eth_suspend,
2636 .resume = eth_resume,
2638 .driver = {
2639 .name = (char *) shortname,
2640 .owner = THIS_MODULE,
2644 MODULE_DESCRIPTION (DRIVER_DESC);
2645 MODULE_AUTHOR ("David Brownell, Benedikt Spanger");
2646 MODULE_LICENSE ("GPL");
2649 static int __init init (void)
2651 return usb_gadget_register_driver (&eth_driver);
2653 module_init (init);
2655 static void __exit cleanup (void)
2657 usb_gadget_unregister_driver (&eth_driver);
2659 module_exit (cleanup);