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>
34 #include <linux/usb/usbnet.h>
37 #if defined(CONFIG_USB_NET_RNDIS_HOST) || defined(CONFIG_USB_NET_RNDIS_HOST_MODULE)
39 static int is_rndis(struct usb_interface_descriptor
*desc
)
41 return desc
->bInterfaceClass
== USB_CLASS_COMM
42 && desc
->bInterfaceSubClass
== 2
43 && desc
->bInterfaceProtocol
== 0xff;
46 static int is_activesync(struct usb_interface_descriptor
*desc
)
48 return desc
->bInterfaceClass
== USB_CLASS_MISC
49 && desc
->bInterfaceSubClass
== 1
50 && desc
->bInterfaceProtocol
== 1;
55 #define is_rndis(desc) 0
56 #define is_activesync(desc) 0
61 * probes control interface, claims data interface, collects the bulk
62 * endpoints, activates data interface (if needed), maybe sets MTU.
63 * all pure cdc, except for certain firmware workarounds, and knowing
64 * that rndis uses one different rule.
66 int usbnet_generic_cdc_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
68 u8
*buf
= intf
->cur_altsetting
->extra
;
69 int len
= intf
->cur_altsetting
->extralen
;
70 struct usb_interface_descriptor
*d
;
71 struct cdc_state
*info
= (void *) &dev
->data
;
74 struct usb_driver
*driver
= driver_of(intf
);
76 if (sizeof dev
->data
< sizeof *info
)
79 /* expect strict spec conformance for the descriptors, but
80 * cope with firmware which stores them in the wrong place
82 if (len
== 0 && dev
->udev
->actconfig
->extralen
) {
83 /* Motorola SB4100 (and others: Brad Hards says it's
84 * from a Broadcom design) put CDC descriptors here
86 buf
= dev
->udev
->actconfig
->extra
;
87 len
= dev
->udev
->actconfig
->extralen
;
90 "CDC descriptors on config\n");
93 /* Maybe CDC descriptors are after the endpoint? This bug has
94 * been seen on some 2Wire Inc RNDIS-ish products.
97 struct usb_host_endpoint
*hep
;
99 hep
= intf
->cur_altsetting
->endpoint
;
106 "CDC descriptors on endpoint\n");
109 /* this assumes that if there's a non-RNDIS vendor variant
110 * of cdc-acm, it'll fail RNDIS requests cleanly.
112 rndis
= is_rndis(&intf
->cur_altsetting
->desc
)
113 || is_activesync(&intf
->cur_altsetting
->desc
);
115 memset(info
, 0, sizeof *info
);
116 info
->control
= intf
;
118 if (buf
[1] != USB_DT_CS_INTERFACE
)
121 /* use bDescriptorSubType to identify the CDC descriptors.
122 * We expect devices with CDC header and union descriptors.
123 * For CDC Ethernet we need the ethernet descriptor.
124 * For RNDIS, ignore two (pointless) CDC modem descriptors
125 * in favor of a complicated OID-based RPC scheme doing what
126 * CDC Ethernet achieves with a simple descriptor.
129 case USB_CDC_HEADER_TYPE
:
131 dev_dbg(&intf
->dev
, "extra CDC header\n");
134 info
->header
= (void *) buf
;
135 if (info
->header
->bLength
!= sizeof *info
->header
) {
136 dev_dbg(&intf
->dev
, "CDC header len %u\n",
137 info
->header
->bLength
);
141 case USB_CDC_ACM_TYPE
:
142 /* paranoia: disambiguate a "real" vendor-specific
143 * modem interface from an RNDIS non-modem.
146 struct usb_cdc_acm_descriptor
*acm
;
149 if (acm
->bmCapabilities
) {
151 "ACM capabilities %02x, "
152 "not really RNDIS?\n",
153 acm
->bmCapabilities
);
158 case USB_CDC_UNION_TYPE
:
160 dev_dbg(&intf
->dev
, "extra CDC union\n");
163 info
->u
= (void *) buf
;
164 if (info
->u
->bLength
!= sizeof *info
->u
) {
165 dev_dbg(&intf
->dev
, "CDC union len %u\n",
170 /* we need a master/control interface (what we're
171 * probed with) and a slave/data interface; union
172 * descriptors sort this all out.
174 info
->control
= usb_ifnum_to_if(dev
->udev
,
175 info
->u
->bMasterInterface0
);
176 info
->data
= usb_ifnum_to_if(dev
->udev
,
177 info
->u
->bSlaveInterface0
);
178 if (!info
->control
|| !info
->data
) {
180 "master #%u/%p slave #%u/%p\n",
181 info
->u
->bMasterInterface0
,
183 info
->u
->bSlaveInterface0
,
187 if (info
->control
!= intf
) {
188 dev_dbg(&intf
->dev
, "bogus CDC Union\n");
189 /* Ambit USB Cable Modem (and maybe others)
190 * interchanges master and slave interface.
192 if (info
->data
== intf
) {
193 info
->data
= info
->control
;
194 info
->control
= intf
;
199 /* a data interface altsetting does the real i/o */
200 d
= &info
->data
->cur_altsetting
->desc
;
201 if (d
->bInterfaceClass
!= USB_CLASS_CDC_DATA
) {
202 dev_dbg(&intf
->dev
, "slave class %u\n",
207 case USB_CDC_ETHERNET_TYPE
:
209 dev_dbg(&intf
->dev
, "extra CDC ether\n");
212 info
->ether
= (void *) buf
;
213 if (info
->ether
->bLength
!= sizeof *info
->ether
) {
214 dev_dbg(&intf
->dev
, "CDC ether len %u\n",
215 info
->ether
->bLength
);
218 dev
->hard_mtu
= le16_to_cpu(
219 info
->ether
->wMaxSegmentSize
);
220 /* because of Zaurus, we may be ignoring the host
221 * side link address we were given.
226 len
-= buf
[0]; /* bLength */
230 /* Microsoft ActiveSync based and some regular RNDIS devices lack the
231 * CDC descriptors, so we'll hard-wire the interfaces and not check
234 if (rndis
&& !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 "rndis: 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
);
319 /*-------------------------------------------------------------------------
321 * Communications Device Class, Ethernet Control model
323 * Takes two interfaces. The DATA interface is inactive till an altsetting
324 * is selected. Configuration data includes class descriptors. There's
325 * an optional status endpoint on the control interface.
327 * This should interop with whatever the 2.4 "CDCEther.c" driver
328 * (by Brad Hards) talked with, with more functionality.
330 *-------------------------------------------------------------------------*/
332 static void dumpspeed(struct usbnet
*dev
, __le32
*speeds
)
334 if (netif_msg_timer(dev
))
335 devinfo(dev
, "link speeds: %u kbps up, %u kbps down",
336 __le32_to_cpu(speeds
[0]) / 1000,
337 __le32_to_cpu(speeds
[1]) / 1000);
340 static void cdc_status(struct usbnet
*dev
, struct urb
*urb
)
342 struct usb_cdc_notification
*event
;
344 if (urb
->actual_length
< sizeof *event
)
347 /* SPEED_CHANGE can get split into two 8-byte packets */
348 if (test_and_clear_bit(EVENT_STS_SPLIT
, &dev
->flags
)) {
349 dumpspeed(dev
, (__le32
*) urb
->transfer_buffer
);
353 event
= urb
->transfer_buffer
;
354 switch (event
->bNotificationType
) {
355 case USB_CDC_NOTIFY_NETWORK_CONNECTION
:
356 if (netif_msg_timer(dev
))
357 devdbg(dev
, "CDC: carrier %s",
358 event
->wValue
? "on" : "off");
360 netif_carrier_on(dev
->net
);
362 netif_carrier_off(dev
->net
);
364 case USB_CDC_NOTIFY_SPEED_CHANGE
: /* tx/rx rates */
365 if (netif_msg_timer(dev
))
366 devdbg(dev
, "CDC: speed change (len %d)",
368 if (urb
->actual_length
!= (sizeof *event
+ 8))
369 set_bit(EVENT_STS_SPLIT
, &dev
->flags
);
371 dumpspeed(dev
, (__le32
*) &event
[1]);
373 /* USB_CDC_NOTIFY_RESPONSE_AVAILABLE can happen too (e.g. RNDIS),
374 * but there are no standard formats for the response data.
377 deverr(dev
, "CDC: unexpected notification %02x!",
378 event
->bNotificationType
);
383 static u8
nibble(unsigned char c
)
385 if (likely(isdigit(c
)))
388 if (likely(isxdigit(c
)))
394 get_ethernet_addr(struct usbnet
*dev
, struct usb_cdc_ether_desc
*e
)
397 unsigned char buf
[13];
399 tmp
= usb_string(dev
->udev
, e
->iMACAddress
, buf
, sizeof buf
);
401 dev_dbg(&dev
->udev
->dev
,
402 "bad MAC string %d fetch, %d\n", e
->iMACAddress
, tmp
);
407 for (i
= tmp
= 0; i
< 6; i
++, tmp
+= 2)
408 dev
->net
->dev_addr
[i
] =
409 (nibble(buf
[tmp
]) << 4) + nibble(buf
[tmp
+ 1]);
413 static int cdc_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
416 struct cdc_state
*info
= (void *) &dev
->data
;
418 status
= usbnet_generic_cdc_bind(dev
, intf
);
422 status
= get_ethernet_addr(dev
, info
->ether
);
424 usb_set_intfdata(info
->data
, NULL
);
425 usb_driver_release_interface(driver_of(intf
), info
->data
);
429 /* FIXME cdc-ether has some multicast code too, though it complains
430 * in routine cases. info->ether describes the multicast support.
431 * Implement that here, manipulating the cdc filter as needed.
436 static const struct driver_info cdc_info
= {
437 .description
= "CDC Ethernet Device",
439 // .check_connect = cdc_check_connect,
441 .unbind
= usbnet_cdc_unbind
,
442 .status
= cdc_status
,
445 /*-------------------------------------------------------------------------*/
448 static const struct usb_device_id products
[] = {
452 * First blacklist any products that are egregiously nonconformant
453 * with the CDC Ethernet specs. Minor braindamage we cope with; when
454 * they're not even trying, needing a separate driver is only the first
455 * of the differences to show up.
458 #define ZAURUS_MASTER_INTERFACE \
459 .bInterfaceClass = USB_CLASS_COMM, \
460 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
461 .bInterfaceProtocol = USB_CDC_PROTO_NONE
463 /* SA-1100 based Sharp Zaurus ("collie"), or compatible;
464 * wire-incompatible with true CDC Ethernet implementations.
465 * (And, it seems, needlessly so...)
468 .match_flags
= USB_DEVICE_ID_MATCH_INT_INFO
469 | USB_DEVICE_ID_MATCH_DEVICE
,
472 ZAURUS_MASTER_INTERFACE
,
476 /* PXA-25x based Sharp Zaurii. Note that it seems some of these
477 * (later models especially) may have shipped only with firmware
478 * advertising false "CDC MDLM" compatibility ... but we're not
479 * clear which models did that, so for now let's assume the worst.
482 .match_flags
= USB_DEVICE_ID_MATCH_INT_INFO
483 | USB_DEVICE_ID_MATCH_DEVICE
,
485 .idProduct
= 0x8005, /* A-300 */
486 ZAURUS_MASTER_INTERFACE
,
489 .match_flags
= USB_DEVICE_ID_MATCH_INT_INFO
490 | USB_DEVICE_ID_MATCH_DEVICE
,
492 .idProduct
= 0x8006, /* B-500/SL-5600 */
493 ZAURUS_MASTER_INTERFACE
,
496 .match_flags
= USB_DEVICE_ID_MATCH_INT_INFO
497 | USB_DEVICE_ID_MATCH_DEVICE
,
499 .idProduct
= 0x8007, /* C-700 */
500 ZAURUS_MASTER_INTERFACE
,
503 .match_flags
= USB_DEVICE_ID_MATCH_INT_INFO
504 | USB_DEVICE_ID_MATCH_DEVICE
,
506 .idProduct
= 0x9031, /* C-750 C-760 */
507 ZAURUS_MASTER_INTERFACE
,
510 .match_flags
= USB_DEVICE_ID_MATCH_INT_INFO
511 | USB_DEVICE_ID_MATCH_DEVICE
,
513 .idProduct
= 0x9032, /* SL-6000 */
514 ZAURUS_MASTER_INTERFACE
,
517 .match_flags
= USB_DEVICE_ID_MATCH_INT_INFO
518 | USB_DEVICE_ID_MATCH_DEVICE
,
520 /* reported with some C860 units */
521 .idProduct
= 0x9050, /* C-860 */
522 ZAURUS_MASTER_INTERFACE
,
526 /* Olympus has some models with a Zaurus-compatible option.
527 * R-1000 uses a FreeScale i.MXL cpu (ARMv4T)
530 .match_flags
= USB_DEVICE_ID_MATCH_INT_INFO
531 | USB_DEVICE_ID_MATCH_DEVICE
,
533 .idProduct
= 0x0F02, /* R-1000 */
534 ZAURUS_MASTER_INTERFACE
,
541 * CDC Ether uses two interfaces, not necessarily consecutive.
542 * We match the main interface, ignoring the optional device
543 * class so we could handle devices that aren't exclusively
546 * NOTE: this match must come AFTER entries blacklisting devices
547 * because of bugs/quirks in a given product (like Zaurus, above).
550 USB_INTERFACE_INFO(USB_CLASS_COMM
, USB_CDC_SUBCLASS_ETHERNET
,
552 .driver_info
= (unsigned long) &cdc_info
,
556 MODULE_DEVICE_TABLE(usb
, products
);
558 static struct usb_driver cdc_driver
= {
560 .id_table
= products
,
561 .probe
= usbnet_probe
,
562 .disconnect
= usbnet_disconnect
,
563 .suspend
= usbnet_suspend
,
564 .resume
= usbnet_resume
,
568 static int __init
cdc_init(void)
570 BUILD_BUG_ON((sizeof(((struct usbnet
*)0)->data
)
571 < sizeof(struct cdc_state
)));
573 return usb_register(&cdc_driver
);
575 module_init(cdc_init
);
577 static void __exit
cdc_exit(void)
579 usb_deregister(&cdc_driver
);
581 module_exit(cdc_exit
);
583 MODULE_AUTHOR("David Brownell");
584 MODULE_DESCRIPTION("USB CDC Ethernet devices");
585 MODULE_LICENSE("GPL");