2 * CDC Ethernet based networking peripherals
3 * Copyright (C) 2003-2005 by David Brownell
4 * Copyright (C) 2006 by Ole Andre Vadla Ravnas (ActiveSync)
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 // #define DEBUG // error path messages, extra info
22 // #define VERBOSE // more; success messages
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/netdevice.h>
27 #include <linux/etherdevice.h>
28 #include <linux/ctype.h>
29 #include <linux/ethtool.h>
30 #include <linux/workqueue.h>
31 #include <linux/mii.h>
32 #include <linux/usb.h>
33 #include <linux/usb/cdc.h>
38 #if defined(CONFIG_USB_NET_RNDIS_HOST) || defined(CONFIG_USB_NET_RNDIS_HOST_MODULE)
40 static int is_rndis(struct usb_interface_descriptor
*desc
)
42 return desc
->bInterfaceClass
== USB_CLASS_COMM
43 && desc
->bInterfaceSubClass
== 2
44 && desc
->bInterfaceProtocol
== 0xff;
47 static int is_activesync(struct usb_interface_descriptor
*desc
)
49 return desc
->bInterfaceClass
== USB_CLASS_MISC
50 && desc
->bInterfaceSubClass
== 1
51 && desc
->bInterfaceProtocol
== 1;
56 #define is_rndis(desc) 0
57 #define is_activesync(desc) 0
62 * probes control interface, claims data interface, collects the bulk
63 * endpoints, activates data interface (if needed), maybe sets MTU.
64 * all pure cdc, except for certain firmware workarounds, and knowing
65 * that rndis uses one different rule.
67 int usbnet_generic_cdc_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
69 u8
*buf
= intf
->cur_altsetting
->extra
;
70 int len
= intf
->cur_altsetting
->extralen
;
71 struct usb_interface_descriptor
*d
;
72 struct cdc_state
*info
= (void *) &dev
->data
;
75 struct usb_driver
*driver
= driver_of(intf
);
77 if (sizeof dev
->data
< sizeof *info
)
80 /* expect strict spec conformance for the descriptors, but
81 * cope with firmware which stores them in the wrong place
83 if (len
== 0 && dev
->udev
->actconfig
->extralen
) {
84 /* Motorola SB4100 (and others: Brad Hards says it's
85 * from a Broadcom design) put CDC descriptors here
87 buf
= dev
->udev
->actconfig
->extra
;
88 len
= dev
->udev
->actconfig
->extralen
;
91 "CDC descriptors on config\n");
94 /* Maybe CDC descriptors are after the endpoint? This bug has
95 * been seen on some 2Wire Inc RNDIS-ish products.
98 struct usb_host_endpoint
*hep
;
100 hep
= intf
->cur_altsetting
->endpoint
;
107 "CDC descriptors on endpoint\n");
110 /* this assumes that if there's a non-RNDIS vendor variant
111 * of cdc-acm, it'll fail RNDIS requests cleanly.
113 rndis
= is_rndis(&intf
->cur_altsetting
->desc
)
114 || is_activesync(&intf
->cur_altsetting
->desc
);
116 memset(info
, 0, sizeof *info
);
117 info
->control
= intf
;
119 if (buf
[1] != USB_DT_CS_INTERFACE
)
122 /* use bDescriptorSubType to identify the CDC descriptors.
123 * We expect devices with CDC header and union descriptors.
124 * For CDC Ethernet we need the ethernet descriptor.
125 * For RNDIS, ignore two (pointless) CDC modem descriptors
126 * in favor of a complicated OID-based RPC scheme doing what
127 * CDC Ethernet achieves with a simple descriptor.
130 case USB_CDC_HEADER_TYPE
:
132 dev_dbg(&intf
->dev
, "extra CDC header\n");
135 info
->header
= (void *) buf
;
136 if (info
->header
->bLength
!= sizeof *info
->header
) {
137 dev_dbg(&intf
->dev
, "CDC header len %u\n",
138 info
->header
->bLength
);
142 case USB_CDC_ACM_TYPE
:
143 /* paranoia: disambiguate a "real" vendor-specific
144 * modem interface from an RNDIS non-modem.
147 struct usb_cdc_acm_descriptor
*acm
;
150 if (acm
->bmCapabilities
) {
152 "ACM capabilities %02x, "
153 "not really RNDIS?\n",
154 acm
->bmCapabilities
);
159 case USB_CDC_UNION_TYPE
:
161 dev_dbg(&intf
->dev
, "extra CDC union\n");
164 info
->u
= (void *) buf
;
165 if (info
->u
->bLength
!= sizeof *info
->u
) {
166 dev_dbg(&intf
->dev
, "CDC union len %u\n",
171 /* we need a master/control interface (what we're
172 * probed with) and a slave/data interface; union
173 * descriptors sort this all out.
175 info
->control
= usb_ifnum_to_if(dev
->udev
,
176 info
->u
->bMasterInterface0
);
177 info
->data
= usb_ifnum_to_if(dev
->udev
,
178 info
->u
->bSlaveInterface0
);
179 if (!info
->control
|| !info
->data
) {
181 "master #%u/%p slave #%u/%p\n",
182 info
->u
->bMasterInterface0
,
184 info
->u
->bSlaveInterface0
,
188 if (info
->control
!= intf
) {
189 dev_dbg(&intf
->dev
, "bogus CDC Union\n");
190 /* Ambit USB Cable Modem (and maybe others)
191 * interchanges master and slave interface.
193 if (info
->data
== intf
) {
194 info
->data
= info
->control
;
195 info
->control
= intf
;
200 /* a data interface altsetting does the real i/o */
201 d
= &info
->data
->cur_altsetting
->desc
;
202 if (d
->bInterfaceClass
!= USB_CLASS_CDC_DATA
) {
203 dev_dbg(&intf
->dev
, "slave class %u\n",
208 case USB_CDC_ETHERNET_TYPE
:
210 dev_dbg(&intf
->dev
, "extra CDC ether\n");
213 info
->ether
= (void *) buf
;
214 if (info
->ether
->bLength
!= sizeof *info
->ether
) {
215 dev_dbg(&intf
->dev
, "CDC ether len %u\n",
216 info
->ether
->bLength
);
219 dev
->hard_mtu
= le16_to_cpu(
220 info
->ether
->wMaxSegmentSize
);
221 /* because of Zaurus, we may be ignoring the host
222 * side link address we were given.
227 len
-= buf
[0]; /* bLength */
231 /* Microsoft ActiveSync based RNDIS devices lack the CDC descriptors,
232 * so we'll hard-wire the interfaces and not check for descriptors.
234 if (is_activesync(&intf
->cur_altsetting
->desc
) && !info
->u
) {
235 info
->control
= usb_ifnum_to_if(dev
->udev
, 0);
236 info
->data
= usb_ifnum_to_if(dev
->udev
, 1);
237 if (!info
->control
|| !info
->data
) {
239 "activesync: master #0/%p slave #1/%p\n",
245 } else if (!info
->header
|| !info
->u
|| (!rndis
&& !info
->ether
)) {
246 dev_dbg(&intf
->dev
, "missing cdc %s%s%sdescriptor\n",
247 info
->header
? "" : "header ",
248 info
->u
? "" : "union ",
249 info
->ether
? "" : "ether ");
253 /* claim data interface and set it up ... with side effects.
254 * network traffic can't flow until an altsetting is enabled.
256 status
= usb_driver_claim_interface(driver
, info
->data
, dev
);
259 status
= usbnet_get_endpoints(dev
, info
->data
);
261 /* ensure immediate exit from usbnet_disconnect */
262 usb_set_intfdata(info
->data
, NULL
);
263 usb_driver_release_interface(driver
, info
->data
);
267 /* status endpoint: optional for CDC Ethernet, not RNDIS (or ACM) */
269 if (info
->control
->cur_altsetting
->desc
.bNumEndpoints
== 1) {
270 struct usb_endpoint_descriptor
*desc
;
272 dev
->status
= &info
->control
->cur_altsetting
->endpoint
[0];
273 desc
= &dev
->status
->desc
;
274 if (!usb_endpoint_is_int_in(desc
)
275 || (le16_to_cpu(desc
->wMaxPacketSize
)
276 < sizeof(struct usb_cdc_notification
))
277 || !desc
->bInterval
) {
278 dev_dbg(&intf
->dev
, "bad notification endpoint\n");
282 if (rndis
&& !dev
->status
) {
283 dev_dbg(&intf
->dev
, "missing RNDIS status endpoint\n");
284 usb_set_intfdata(info
->data
, NULL
);
285 usb_driver_release_interface(driver
, info
->data
);
291 dev_info(&dev
->udev
->dev
, "bad CDC descriptors\n");
294 EXPORT_SYMBOL_GPL(usbnet_generic_cdc_bind
);
296 void usbnet_cdc_unbind(struct usbnet
*dev
, struct usb_interface
*intf
)
298 struct cdc_state
*info
= (void *) &dev
->data
;
299 struct usb_driver
*driver
= driver_of(intf
);
301 /* disconnect master --> disconnect slave */
302 if (intf
== info
->control
&& info
->data
) {
303 /* ensure immediate exit from usbnet_disconnect */
304 usb_set_intfdata(info
->data
, NULL
);
305 usb_driver_release_interface(driver
, info
->data
);
309 /* and vice versa (just in case) */
310 else if (intf
== info
->data
&& info
->control
) {
311 /* ensure immediate exit from usbnet_disconnect */
312 usb_set_intfdata(info
->control
, NULL
);
313 usb_driver_release_interface(driver
, info
->control
);
314 info
->control
= NULL
;
317 EXPORT_SYMBOL_GPL(usbnet_cdc_unbind
);
320 /*-------------------------------------------------------------------------
322 * Communications Device Class, Ethernet Control model
324 * Takes two interfaces. The DATA interface is inactive till an altsetting
325 * is selected. Configuration data includes class descriptors. There's
326 * an optional status endpoint on the control interface.
328 * This should interop with whatever the 2.4 "CDCEther.c" driver
329 * (by Brad Hards) talked with, with more functionality.
331 *-------------------------------------------------------------------------*/
333 static void dumpspeed(struct usbnet
*dev
, __le32
*speeds
)
335 if (netif_msg_timer(dev
))
336 devinfo(dev
, "link speeds: %u kbps up, %u kbps down",
337 __le32_to_cpu(speeds
[0]) / 1000,
338 __le32_to_cpu(speeds
[1]) / 1000);
341 static void cdc_status(struct usbnet
*dev
, struct urb
*urb
)
343 struct usb_cdc_notification
*event
;
345 if (urb
->actual_length
< sizeof *event
)
348 /* SPEED_CHANGE can get split into two 8-byte packets */
349 if (test_and_clear_bit(EVENT_STS_SPLIT
, &dev
->flags
)) {
350 dumpspeed(dev
, (__le32
*) urb
->transfer_buffer
);
354 event
= urb
->transfer_buffer
;
355 switch (event
->bNotificationType
) {
356 case USB_CDC_NOTIFY_NETWORK_CONNECTION
:
357 if (netif_msg_timer(dev
))
358 devdbg(dev
, "CDC: carrier %s",
359 event
->wValue
? "on" : "off");
361 netif_carrier_on(dev
->net
);
363 netif_carrier_off(dev
->net
);
365 case USB_CDC_NOTIFY_SPEED_CHANGE
: /* tx/rx rates */
366 if (netif_msg_timer(dev
))
367 devdbg(dev
, "CDC: speed change (len %d)",
369 if (urb
->actual_length
!= (sizeof *event
+ 8))
370 set_bit(EVENT_STS_SPLIT
, &dev
->flags
);
372 dumpspeed(dev
, (__le32
*) &event
[1]);
374 /* USB_CDC_NOTIFY_RESPONSE_AVAILABLE can happen too (e.g. RNDIS),
375 * but there are no standard formats for the response data.
378 deverr(dev
, "CDC: unexpected notification %02x!",
379 event
->bNotificationType
);
384 static u8
nibble(unsigned char c
)
386 if (likely(isdigit(c
)))
389 if (likely(isxdigit(c
)))
395 get_ethernet_addr(struct usbnet
*dev
, struct usb_cdc_ether_desc
*e
)
398 unsigned char buf
[13];
400 tmp
= usb_string(dev
->udev
, e
->iMACAddress
, buf
, sizeof buf
);
402 dev_dbg(&dev
->udev
->dev
,
403 "bad MAC string %d fetch, %d\n", e
->iMACAddress
, tmp
);
408 for (i
= tmp
= 0; i
< 6; i
++, tmp
+= 2)
409 dev
->net
->dev_addr
[i
] =
410 (nibble(buf
[tmp
]) << 4) + nibble(buf
[tmp
+ 1]);
414 static int cdc_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
417 struct cdc_state
*info
= (void *) &dev
->data
;
419 status
= usbnet_generic_cdc_bind(dev
, intf
);
423 status
= get_ethernet_addr(dev
, info
->ether
);
425 usb_set_intfdata(info
->data
, NULL
);
426 usb_driver_release_interface(driver_of(intf
), info
->data
);
430 /* FIXME cdc-ether has some multicast code too, though it complains
431 * in routine cases. info->ether describes the multicast support.
432 * Implement that here, manipulating the cdc filter as needed.
437 static const struct driver_info cdc_info
= {
438 .description
= "CDC Ethernet Device",
440 // .check_connect = cdc_check_connect,
442 .unbind
= usbnet_cdc_unbind
,
443 .status
= cdc_status
,
446 /*-------------------------------------------------------------------------*/
449 static const struct usb_device_id products
[] = {
453 * First blacklist any products that are egregiously nonconformant
454 * with the CDC Ethernet specs. Minor braindamage we cope with; when
455 * they're not even trying, needing a separate driver is only the first
456 * of the differences to show up.
459 #define ZAURUS_MASTER_INTERFACE \
460 .bInterfaceClass = USB_CLASS_COMM, \
461 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
462 .bInterfaceProtocol = USB_CDC_PROTO_NONE
464 /* SA-1100 based Sharp Zaurus ("collie"), or compatible;
465 * wire-incompatible with true CDC Ethernet implementations.
466 * (And, it seems, needlessly so...)
469 .match_flags
= USB_DEVICE_ID_MATCH_INT_INFO
470 | USB_DEVICE_ID_MATCH_DEVICE
,
473 ZAURUS_MASTER_INTERFACE
,
477 /* PXA-25x based Sharp Zaurii. Note that it seems some of these
478 * (later models especially) may have shipped only with firmware
479 * advertising false "CDC MDLM" compatibility ... but we're not
480 * clear which models did that, so for now let's assume the worst.
483 .match_flags
= USB_DEVICE_ID_MATCH_INT_INFO
484 | USB_DEVICE_ID_MATCH_DEVICE
,
486 .idProduct
= 0x8005, /* A-300 */
487 ZAURUS_MASTER_INTERFACE
,
490 .match_flags
= USB_DEVICE_ID_MATCH_INT_INFO
491 | USB_DEVICE_ID_MATCH_DEVICE
,
493 .idProduct
= 0x8006, /* B-500/SL-5600 */
494 ZAURUS_MASTER_INTERFACE
,
497 .match_flags
= USB_DEVICE_ID_MATCH_INT_INFO
498 | USB_DEVICE_ID_MATCH_DEVICE
,
500 .idProduct
= 0x8007, /* C-700 */
501 ZAURUS_MASTER_INTERFACE
,
504 .match_flags
= USB_DEVICE_ID_MATCH_INT_INFO
505 | USB_DEVICE_ID_MATCH_DEVICE
,
507 .idProduct
= 0x9031, /* C-750 C-760 */
508 ZAURUS_MASTER_INTERFACE
,
511 .match_flags
= USB_DEVICE_ID_MATCH_INT_INFO
512 | USB_DEVICE_ID_MATCH_DEVICE
,
514 .idProduct
= 0x9032, /* SL-6000 */
515 ZAURUS_MASTER_INTERFACE
,
518 .match_flags
= USB_DEVICE_ID_MATCH_INT_INFO
519 | USB_DEVICE_ID_MATCH_DEVICE
,
521 /* reported with some C860 units */
522 .idProduct
= 0x9050, /* C-860 */
523 ZAURUS_MASTER_INTERFACE
,
527 /* Olympus has some models with a Zaurus-compatible option.
528 * R-1000 uses a FreeScale i.MXL cpu (ARMv4T)
531 .match_flags
= USB_DEVICE_ID_MATCH_INT_INFO
532 | USB_DEVICE_ID_MATCH_DEVICE
,
534 .idProduct
= 0x0F02, /* R-1000 */
535 ZAURUS_MASTER_INTERFACE
,
542 * CDC Ether uses two interfaces, not necessarily consecutive.
543 * We match the main interface, ignoring the optional device
544 * class so we could handle devices that aren't exclusively
547 * NOTE: this match must come AFTER entries blacklisting devices
548 * because of bugs/quirks in a given product (like Zaurus, above).
551 USB_INTERFACE_INFO(USB_CLASS_COMM
, USB_CDC_SUBCLASS_ETHERNET
,
553 .driver_info
= (unsigned long) &cdc_info
,
557 MODULE_DEVICE_TABLE(usb
, products
);
559 static struct usb_driver cdc_driver
= {
561 .id_table
= products
,
562 .probe
= usbnet_probe
,
563 .disconnect
= usbnet_disconnect
,
564 .suspend
= usbnet_suspend
,
565 .resume
= usbnet_resume
,
569 static int __init
cdc_init(void)
571 BUILD_BUG_ON((sizeof(((struct usbnet
*)0)->data
)
572 < sizeof(struct cdc_state
)));
574 return usb_register(&cdc_driver
);
576 module_init(cdc_init
);
578 static void __exit
cdc_exit(void)
580 usb_deregister(&cdc_driver
);
582 module_exit(cdc_exit
);
584 MODULE_AUTHOR("David Brownell");
585 MODULE_DESCRIPTION("USB CDC Ethernet devices");
586 MODULE_LICENSE("GPL");