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 /* this assumes that if there's a non-RNDIS vendor variant
95 * of cdc-acm, it'll fail RNDIS requests cleanly.
97 rndis = is_rndis(&intf->cur_altsetting->desc)
98 || is_activesync(&intf->cur_altsetting->desc);
100 memset(info, 0, sizeof *info);
101 info->control = intf;
103 if (buf [1] != USB_DT_CS_INTERFACE)
106 /* use bDescriptorSubType to identify the CDC descriptors.
107 * We expect devices with CDC header and union descriptors.
108 * For CDC Ethernet we need the ethernet descriptor.
109 * For RNDIS, ignore two (pointless) CDC modem descriptors
110 * in favor of a complicated OID-based RPC scheme doing what
111 * CDC Ethernet achieves with a simple descriptor.
114 case USB_CDC_HEADER_TYPE:
116 dev_dbg(&intf->dev, "extra CDC header\n");
119 info->header = (void *) buf;
120 if (info->header->bLength != sizeof *info->header) {
121 dev_dbg(&intf->dev, "CDC header len %u\n",
122 info->header->bLength);
126 case USB_CDC_ACM_TYPE:
127 /* paranoia: disambiguate a "real" vendor-specific
128 * modem interface from an RNDIS non-modem.
131 struct usb_cdc_acm_descriptor *d;
134 if (d->bmCapabilities) {
136 "ACM capabilities %02x, "
137 "not really RNDIS?\n",
143 case USB_CDC_UNION_TYPE:
145 dev_dbg(&intf->dev, "extra CDC union\n");
148 info->u = (void *) buf;
149 if (info->u->bLength != sizeof *info->u) {
150 dev_dbg(&intf->dev, "CDC union len %u\n",
155 /* we need a master/control interface (what we're
156 * probed with) and a slave/data interface; union
157 * descriptors sort this all out.
159 info->control = usb_ifnum_to_if(dev->udev,
160 info->u->bMasterInterface0);
161 info->data = usb_ifnum_to_if(dev->udev,
162 info->u->bSlaveInterface0);
163 if (!info->control || !info->data) {
165 "master #%u/%p slave #%u/%p\n",
166 info->u->bMasterInterface0,
168 info->u->bSlaveInterface0,
172 if (info->control != intf) {
173 dev_dbg(&intf->dev, "bogus CDC Union\n");
174 /* Ambit USB Cable Modem (and maybe others)
175 * interchanges master and slave interface.
177 if (info->data == intf) {
178 info->data = info->control;
179 info->control = intf;
184 /* a data interface altsetting does the real i/o */
185 d = &info->data->cur_altsetting->desc;
186 if (d->bInterfaceClass != USB_CLASS_CDC_DATA) {
187 dev_dbg(&intf->dev, "slave class %u\n",
192 case USB_CDC_ETHERNET_TYPE:
194 dev_dbg(&intf->dev, "extra CDC ether\n");
197 info->ether = (void *) buf;
198 if (info->ether->bLength != sizeof *info->ether) {
199 dev_dbg(&intf->dev, "CDC ether len %u\n",
200 info->ether->bLength);
203 dev->hard_mtu = le16_to_cpu(
204 info->ether->wMaxSegmentSize);
205 /* because of Zaurus, we may be ignoring the host
206 * side link address we were given.
211 len -= buf [0]; /* bLength */
215 /* Microsoft ActiveSync based RNDIS devices lack the CDC descriptors,
216 * so we'll hard-wire the interfaces and not check for descriptors.
218 if (is_activesync(&intf->cur_altsetting->desc) && !info->u) {
219 info->control = usb_ifnum_to_if(dev->udev, 0);
220 info->data = usb_ifnum_to_if(dev->udev, 1);
221 if (!info->control || !info->data) {
223 "activesync: master #0/%p slave #1/%p\n",
229 } else if (!info->header || !info->u || (!rndis && !info->ether)) {
230 dev_dbg(&intf->dev, "missing cdc %s%s%sdescriptor\n",
231 info->header ? "" : "header ",
232 info->u ? "" : "union ",
233 info->ether ? "" : "ether ");
237 /* claim data interface and set it up ... with side effects.
238 * network traffic can't flow until an altsetting is enabled.
240 status = usb_driver_claim_interface(driver, info->data, dev);
243 status = usbnet_get_endpoints(dev, info->data);
245 /* ensure immediate exit from usbnet_disconnect */
246 usb_set_intfdata(info->data, NULL);
247 usb_driver_release_interface(driver, info->data);
251 /* status endpoint: optional for CDC Ethernet, not RNDIS (or ACM) */
253 if (info->control->cur_altsetting->desc.bNumEndpoints == 1) {
254 struct usb_endpoint_descriptor *desc;
256 dev->status = &info->control->cur_altsetting->endpoint [0];
257 desc = &dev->status->desc;
258 if (!usb_endpoint_is_int_in(desc)
259 || (le16_to_cpu(desc->wMaxPacketSize)
260 < sizeof(struct usb_cdc_notification))
261 || !desc->bInterval) {
262 dev_dbg(&intf->dev, "bad notification endpoint\n");
266 if (rndis && !dev->status) {
267 dev_dbg(&intf->dev, "missing RNDIS status endpoint\n");
268 usb_set_intfdata(info->data, NULL);
269 usb_driver_release_interface(driver, info->data);
275 dev_info(&dev->udev->dev, "bad CDC descriptors\n");
278 EXPORT_SYMBOL_GPL(usbnet_generic_cdc_bind);
280 void usbnet_cdc_unbind(struct usbnet *dev, struct usb_interface *intf)
282 struct cdc_state *info = (void *) &dev->data;
283 struct usb_driver *driver = driver_of(intf);
285 /* disconnect master --> disconnect slave */
286 if (intf == info->control && info->data) {
287 /* ensure immediate exit from usbnet_disconnect */
288 usb_set_intfdata(info->data, NULL);
289 usb_driver_release_interface(driver, info->data);
293 /* and vice versa (just in case) */
294 else if (intf == info->data && info->control) {
295 /* ensure immediate exit from usbnet_disconnect */
296 usb_set_intfdata(info->control, NULL);
297 usb_driver_release_interface(driver, info->control);
298 info->control = NULL;
301 EXPORT_SYMBOL_GPL(usbnet_cdc_unbind);
304 /*-------------------------------------------------------------------------
306 * Communications Device Class, Ethernet Control model
308 * Takes two interfaces. The DATA interface is inactive till an altsetting
309 * is selected. Configuration data includes class descriptors. There's
310 * an optional status endpoint on the control interface.
312 * This should interop with whatever the 2.4 "CDCEther.c" driver
313 * (by Brad Hards) talked with, with more functionality.
315 *-------------------------------------------------------------------------*/
317 static void dumpspeed(struct usbnet *dev, __le32 *speeds)
319 if (netif_msg_timer(dev))
320 devinfo(dev, "link speeds: %u kbps up, %u kbps down",
321 __le32_to_cpu(speeds[0]) / 1000,
322 __le32_to_cpu(speeds[1]) / 1000);
325 static void cdc_status(struct usbnet *dev, struct urb *urb)
327 struct usb_cdc_notification *event;
329 if (urb->actual_length < sizeof *event)
332 /* SPEED_CHANGE can get split into two 8-byte packets */
333 if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
334 dumpspeed(dev, (__le32 *) urb->transfer_buffer);
338 event = urb->transfer_buffer;
339 switch (event->bNotificationType) {
340 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
341 if (netif_msg_timer(dev))
342 devdbg(dev, "CDC: carrier %s",
343 event->wValue ? "on" : "off");
345 netif_carrier_on(dev->net);
347 netif_carrier_off(dev->net);
349 case USB_CDC_NOTIFY_SPEED_CHANGE: /* tx/rx rates */
350 if (netif_msg_timer(dev))
351 devdbg(dev, "CDC: speed change (len %d)",
353 if (urb->actual_length != (sizeof *event + 8))
354 set_bit(EVENT_STS_SPLIT, &dev->flags);
356 dumpspeed(dev, (__le32 *) &event[1]);
358 /* USB_CDC_NOTIFY_RESPONSE_AVAILABLE can happen too (e.g. RNDIS),
359 * but there are no standard formats for the response data.
362 deverr(dev, "CDC: unexpected notification %02x!",
363 event->bNotificationType);
368 static u8 nibble(unsigned char c)
370 if (likely(isdigit(c)))
373 if (likely(isxdigit(c)))
379 get_ethernet_addr(struct usbnet *dev, struct usb_cdc_ether_desc *e)
382 unsigned char buf [13];
384 tmp = usb_string(dev->udev, e->iMACAddress, buf, sizeof buf);
386 dev_dbg(&dev->udev->dev,
387 "bad MAC string %d fetch, %d\n", e->iMACAddress, tmp);
392 for (i = tmp = 0; i < 6; i++, tmp += 2)
393 dev->net->dev_addr [i] =
394 (nibble(buf [tmp]) << 4) + nibble(buf [tmp + 1]);
398 static int cdc_bind(struct usbnet *dev, struct usb_interface *intf)
401 struct cdc_state *info = (void *) &dev->data;
403 status = usbnet_generic_cdc_bind(dev, intf);
407 status = get_ethernet_addr(dev, info->ether);
409 usb_set_intfdata(info->data, NULL);
410 usb_driver_release_interface(driver_of(intf), info->data);
414 /* FIXME cdc-ether has some multicast code too, though it complains
415 * in routine cases. info->ether describes the multicast support.
416 * Implement that here, manipulating the cdc filter as needed.
421 static const struct driver_info cdc_info = {
422 .description = "CDC Ethernet Device",
424 // .check_connect = cdc_check_connect,
426 .unbind = usbnet_cdc_unbind,
427 .status = cdc_status,
430 /*-------------------------------------------------------------------------*/
433 static const struct usb_device_id products [] = {
437 * First blacklist any products that are egregiously nonconformant
438 * with the CDC Ethernet specs. Minor braindamage we cope with; when
439 * they're not even trying, needing a separate driver is only the first
440 * of the differences to show up.
443 #define ZAURUS_MASTER_INTERFACE \
444 .bInterfaceClass = USB_CLASS_COMM, \
445 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
446 .bInterfaceProtocol = USB_CDC_PROTO_NONE
448 /* SA-1100 based Sharp Zaurus ("collie"), or compatible;
449 * wire-incompatible with true CDC Ethernet implementations.
450 * (And, it seems, needlessly so...)
453 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
454 | USB_DEVICE_ID_MATCH_DEVICE,
457 ZAURUS_MASTER_INTERFACE,
461 /* PXA-25x based Sharp Zaurii. Note that it seems some of these
462 * (later models especially) may have shipped only with firmware
463 * advertising false "CDC MDLM" compatibility ... but we're not
464 * clear which models did that, so for now let's assume the worst.
467 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
468 | USB_DEVICE_ID_MATCH_DEVICE,
470 .idProduct = 0x8005, /* A-300 */
471 ZAURUS_MASTER_INTERFACE,
474 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
475 | USB_DEVICE_ID_MATCH_DEVICE,
477 .idProduct = 0x8006, /* B-500/SL-5600 */
478 ZAURUS_MASTER_INTERFACE,
481 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
482 | USB_DEVICE_ID_MATCH_DEVICE,
484 .idProduct = 0x8007, /* C-700 */
485 ZAURUS_MASTER_INTERFACE,
488 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
489 | USB_DEVICE_ID_MATCH_DEVICE,
491 .idProduct = 0x9031, /* C-750 C-760 */
492 ZAURUS_MASTER_INTERFACE,
495 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
496 | USB_DEVICE_ID_MATCH_DEVICE,
498 .idProduct = 0x9032, /* SL-6000 */
499 ZAURUS_MASTER_INTERFACE,
502 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
503 | USB_DEVICE_ID_MATCH_DEVICE,
505 /* reported with some C860 units */
506 .idProduct = 0x9050, /* C-860 */
507 ZAURUS_MASTER_INTERFACE,
511 /* Olympus has some models with a Zaurus-compatible option.
512 * R-1000 uses a FreeScale i.MXL cpu (ARMv4T)
515 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
516 | USB_DEVICE_ID_MATCH_DEVICE,
518 .idProduct = 0x0F02, /* R-1000 */
519 ZAURUS_MASTER_INTERFACE,
526 * CDC Ether uses two interfaces, not necessarily consecutive.
527 * We match the main interface, ignoring the optional device
528 * class so we could handle devices that aren't exclusively
531 * NOTE: this match must come AFTER entries blacklisting devices
532 * because of bugs/quirks in a given product (like Zaurus, above).
535 USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ETHERNET,
537 .driver_info = (unsigned long) &cdc_info,
541 MODULE_DEVICE_TABLE(usb, products);
543 static struct usb_driver cdc_driver = {
545 .id_table = products,
546 .probe = usbnet_probe,
547 .disconnect = usbnet_disconnect,
548 .suspend = usbnet_suspend,
549 .resume = usbnet_resume,
553 static int __init cdc_init(void)
555 BUILD_BUG_ON((sizeof(((struct usbnet *)0)->data)
556 < sizeof(struct cdc_state)));
558 return usb_register(&cdc_driver);
560 module_init(cdc_init);
562 static void __exit cdc_exit(void)
564 usb_deregister(&cdc_driver);
566 module_exit(cdc_exit);
568 MODULE_AUTHOR("David Brownell");
569 MODULE_DESCRIPTION("USB CDC Ethernet devices");
570 MODULE_LICENSE("GPL");