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;
53 static int is_wireless_rndis(struct usb_interface_descriptor *desc)
55 return desc->bInterfaceClass == USB_CLASS_WIRELESS_CONTROLLER
56 && desc->bInterfaceSubClass == 1
57 && desc->bInterfaceProtocol == 3;
62 #define is_rndis(desc) 0
63 #define is_activesync(desc) 0
64 #define is_wireless_rndis(desc) 0
69 * probes control interface, claims data interface, collects the bulk
70 * endpoints, activates data interface (if needed), maybe sets MTU.
71 * all pure cdc, except for certain firmware workarounds, and knowing
72 * that rndis uses one different rule.
74 int usbnet_generic_cdc_bind(struct usbnet *dev, struct usb_interface *intf)
76 u8 *buf = intf->cur_altsetting->extra;
77 int len = intf->cur_altsetting->extralen;
78 struct usb_interface_descriptor *d;
79 struct cdc_state *info = (void *) &dev->data;
82 struct usb_driver *driver = driver_of(intf);
84 if (sizeof dev->data < sizeof *info)
87 /* expect strict spec conformance for the descriptors, but
88 * cope with firmware which stores them in the wrong place
90 if (len == 0 && dev->udev->actconfig->extralen) {
91 /* Motorola SB4100 (and others: Brad Hards says it's
92 * from a Broadcom design) put CDC descriptors here
94 buf = dev->udev->actconfig->extra;
95 len = dev->udev->actconfig->extralen;
98 "CDC descriptors on config\n");
101 /* Maybe CDC descriptors are after the endpoint? This bug has
102 * been seen on some 2Wire Inc RNDIS-ish products.
105 struct usb_host_endpoint *hep;
107 hep = intf->cur_altsetting->endpoint;
114 "CDC descriptors on endpoint\n");
117 /* this assumes that if there's a non-RNDIS vendor variant
118 * of cdc-acm, it'll fail RNDIS requests cleanly.
120 rndis = is_rndis(&intf->cur_altsetting->desc)
121 || is_activesync(&intf->cur_altsetting->desc)
122 || is_wireless_rndis(&intf->cur_altsetting->desc);
124 memset(info, 0, sizeof *info);
125 info->control = intf;
127 if (buf [1] != USB_DT_CS_INTERFACE)
130 /* use bDescriptorSubType to identify the CDC descriptors.
131 * We expect devices with CDC header and union descriptors.
132 * For CDC Ethernet we need the ethernet descriptor.
133 * For RNDIS, ignore two (pointless) CDC modem descriptors
134 * in favor of a complicated OID-based RPC scheme doing what
135 * CDC Ethernet achieves with a simple descriptor.
138 case USB_CDC_HEADER_TYPE:
140 dev_dbg(&intf->dev, "extra CDC header\n");
143 info->header = (void *) buf;
144 if (info->header->bLength != sizeof *info->header) {
145 dev_dbg(&intf->dev, "CDC header len %u\n",
146 info->header->bLength);
150 case USB_CDC_ACM_TYPE:
151 /* paranoia: disambiguate a "real" vendor-specific
152 * modem interface from an RNDIS non-modem.
155 struct usb_cdc_acm_descriptor *acm;
158 if (acm->bmCapabilities) {
160 "ACM capabilities %02x, "
161 "not really RNDIS?\n",
162 acm->bmCapabilities);
167 case USB_CDC_UNION_TYPE:
169 dev_dbg(&intf->dev, "extra CDC union\n");
172 info->u = (void *) buf;
173 if (info->u->bLength != sizeof *info->u) {
174 dev_dbg(&intf->dev, "CDC union len %u\n",
179 /* we need a master/control interface (what we're
180 * probed with) and a slave/data interface; union
181 * descriptors sort this all out.
183 info->control = usb_ifnum_to_if(dev->udev,
184 info->u->bMasterInterface0);
185 info->data = usb_ifnum_to_if(dev->udev,
186 info->u->bSlaveInterface0);
187 if (!info->control || !info->data) {
189 "master #%u/%p slave #%u/%p\n",
190 info->u->bMasterInterface0,
192 info->u->bSlaveInterface0,
196 if (info->control != intf) {
197 dev_dbg(&intf->dev, "bogus CDC Union\n");
198 /* Ambit USB Cable Modem (and maybe others)
199 * interchanges master and slave interface.
201 if (info->data == intf) {
202 info->data = info->control;
203 info->control = intf;
208 /* a data interface altsetting does the real i/o */
209 d = &info->data->cur_altsetting->desc;
210 if (d->bInterfaceClass != USB_CLASS_CDC_DATA) {
211 dev_dbg(&intf->dev, "slave class %u\n",
216 case USB_CDC_ETHERNET_TYPE:
218 dev_dbg(&intf->dev, "extra CDC ether\n");
221 info->ether = (void *) buf;
222 if (info->ether->bLength != sizeof *info->ether) {
223 dev_dbg(&intf->dev, "CDC ether len %u\n",
224 info->ether->bLength);
227 dev->hard_mtu = le16_to_cpu(
228 info->ether->wMaxSegmentSize);
229 /* because of Zaurus, we may be ignoring the host
230 * side link address we were given.
235 len -= buf [0]; /* bLength */
239 /* Microsoft ActiveSync based and some regular RNDIS devices lack the
240 * CDC descriptors, so we'll hard-wire the interfaces and not check
243 if (rndis && !info->u) {
244 info->control = usb_ifnum_to_if(dev->udev, 0);
245 info->data = usb_ifnum_to_if(dev->udev, 1);
246 if (!info->control || !info->data) {
248 "rndis: master #0/%p slave #1/%p\n",
254 } else if (!info->header || !info->u || (!rndis && !info->ether)) {
255 dev_dbg(&intf->dev, "missing cdc %s%s%sdescriptor\n",
256 info->header ? "" : "header ",
257 info->u ? "" : "union ",
258 info->ether ? "" : "ether ");
262 /* claim data interface and set it up ... with side effects.
263 * network traffic can't flow until an altsetting is enabled.
265 status = usb_driver_claim_interface(driver, info->data, dev);
268 status = usbnet_get_endpoints(dev, info->data);
270 /* ensure immediate exit from usbnet_disconnect */
271 usb_set_intfdata(info->data, NULL);
272 usb_driver_release_interface(driver, info->data);
276 /* status endpoint: optional for CDC Ethernet, not RNDIS (or ACM) */
278 if (info->control->cur_altsetting->desc.bNumEndpoints == 1) {
279 struct usb_endpoint_descriptor *desc;
281 dev->status = &info->control->cur_altsetting->endpoint [0];
282 desc = &dev->status->desc;
283 if (!usb_endpoint_is_int_in(desc)
284 || (le16_to_cpu(desc->wMaxPacketSize)
285 < sizeof(struct usb_cdc_notification))
286 || !desc->bInterval) {
287 dev_dbg(&intf->dev, "bad notification endpoint\n");
291 if (rndis && !dev->status) {
292 dev_dbg(&intf->dev, "missing RNDIS status endpoint\n");
293 usb_set_intfdata(info->data, NULL);
294 usb_driver_release_interface(driver, info->data);
300 dev_info(&dev->udev->dev, "bad CDC descriptors\n");
303 EXPORT_SYMBOL_GPL(usbnet_generic_cdc_bind);
305 void usbnet_cdc_unbind(struct usbnet *dev, struct usb_interface *intf)
307 struct cdc_state *info = (void *) &dev->data;
308 struct usb_driver *driver = driver_of(intf);
310 /* disconnect master --> disconnect slave */
311 if (intf == info->control && info->data) {
312 /* ensure immediate exit from usbnet_disconnect */
313 usb_set_intfdata(info->data, NULL);
314 usb_driver_release_interface(driver, info->data);
318 /* and vice versa (just in case) */
319 else if (intf == info->data && info->control) {
320 /* ensure immediate exit from usbnet_disconnect */
321 usb_set_intfdata(info->control, NULL);
322 usb_driver_release_interface(driver, info->control);
323 info->control = NULL;
326 EXPORT_SYMBOL_GPL(usbnet_cdc_unbind);
328 /*-------------------------------------------------------------------------
330 * Communications Device Class, Ethernet Control model
332 * Takes two interfaces. The DATA interface is inactive till an altsetting
333 * is selected. Configuration data includes class descriptors. There's
334 * an optional status endpoint on the control interface.
336 * This should interop with whatever the 2.4 "CDCEther.c" driver
337 * (by Brad Hards) talked with, with more functionality.
339 *-------------------------------------------------------------------------*/
341 static void dumpspeed(struct usbnet *dev, __le32 *speeds)
343 if (netif_msg_timer(dev))
344 devinfo(dev, "link speeds: %u kbps up, %u kbps down",
345 __le32_to_cpu(speeds[0]) / 1000,
346 __le32_to_cpu(speeds[1]) / 1000);
349 static void cdc_status(struct usbnet *dev, struct urb *urb)
351 struct usb_cdc_notification *event;
353 if (urb->actual_length < sizeof *event)
356 /* SPEED_CHANGE can get split into two 8-byte packets */
357 if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
358 dumpspeed(dev, (__le32 *) urb->transfer_buffer);
362 event = urb->transfer_buffer;
363 switch (event->bNotificationType) {
364 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
365 if (netif_msg_timer(dev))
366 devdbg(dev, "CDC: carrier %s",
367 event->wValue ? "on" : "off");
369 netif_carrier_on(dev->net);
371 netif_carrier_off(dev->net);
373 case USB_CDC_NOTIFY_SPEED_CHANGE: /* tx/rx rates */
374 if (netif_msg_timer(dev))
375 devdbg(dev, "CDC: speed change (len %d)",
377 if (urb->actual_length != (sizeof *event + 8))
378 set_bit(EVENT_STS_SPLIT, &dev->flags);
380 dumpspeed(dev, (__le32 *) &event[1]);
382 /* USB_CDC_NOTIFY_RESPONSE_AVAILABLE can happen too (e.g. RNDIS),
383 * but there are no standard formats for the response data.
386 deverr(dev, "CDC: unexpected notification %02x!",
387 event->bNotificationType);
392 static u8 nibble(unsigned char c)
394 if (likely(isdigit(c)))
397 if (likely(isxdigit(c)))
403 get_ethernet_addr(struct usbnet *dev, struct usb_cdc_ether_desc *e)
406 unsigned char buf [13];
408 tmp = usb_string(dev->udev, e->iMACAddress, buf, sizeof buf);
410 dev_dbg(&dev->udev->dev,
411 "bad MAC string %d fetch, %d\n", e->iMACAddress, tmp);
416 for (i = tmp = 0; i < 6; i++, tmp += 2)
417 dev->net->dev_addr [i] =
418 (nibble(buf [tmp]) << 4) + nibble(buf [tmp + 1]);
422 static int cdc_bind(struct usbnet *dev, struct usb_interface *intf)
425 struct cdc_state *info = (void *) &dev->data;
427 status = usbnet_generic_cdc_bind(dev, intf);
431 status = get_ethernet_addr(dev, info->ether);
433 usb_set_intfdata(info->data, NULL);
434 usb_driver_release_interface(driver_of(intf), info->data);
438 /* FIXME cdc-ether has some multicast code too, though it complains
439 * in routine cases. info->ether describes the multicast support.
440 * Implement that here, manipulating the cdc filter as needed.
445 static const struct driver_info cdc_info = {
446 .description = "CDC Ethernet Device",
448 // .check_connect = cdc_check_connect,
450 .unbind = usbnet_cdc_unbind,
451 .status = cdc_status,
454 /*-------------------------------------------------------------------------*/
457 static const struct usb_device_id products [] = {
461 * First blacklist any products that are egregiously nonconformant
462 * with the CDC Ethernet specs. Minor braindamage we cope with; when
463 * they're not even trying, needing a separate driver is only the first
464 * of the differences to show up.
467 #define ZAURUS_MASTER_INTERFACE \
468 .bInterfaceClass = USB_CLASS_COMM, \
469 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
470 .bInterfaceProtocol = USB_CDC_PROTO_NONE
472 /* SA-1100 based Sharp Zaurus ("collie"), or compatible;
473 * wire-incompatible with true CDC Ethernet implementations.
474 * (And, it seems, needlessly so...)
477 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
478 | USB_DEVICE_ID_MATCH_DEVICE,
481 ZAURUS_MASTER_INTERFACE,
485 /* PXA-25x based Sharp Zaurii. Note that it seems some of these
486 * (later models especially) may have shipped only with firmware
487 * advertising false "CDC MDLM" compatibility ... but we're not
488 * clear which models did that, so for now let's assume the worst.
491 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
492 | USB_DEVICE_ID_MATCH_DEVICE,
494 .idProduct = 0x8005, /* A-300 */
495 ZAURUS_MASTER_INTERFACE,
498 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
499 | USB_DEVICE_ID_MATCH_DEVICE,
501 .idProduct = 0x8006, /* B-500/SL-5600 */
502 ZAURUS_MASTER_INTERFACE,
505 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
506 | USB_DEVICE_ID_MATCH_DEVICE,
508 .idProduct = 0x8007, /* C-700 */
509 ZAURUS_MASTER_INTERFACE,
512 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
513 | USB_DEVICE_ID_MATCH_DEVICE,
515 .idProduct = 0x9031, /* C-750 C-760 */
516 ZAURUS_MASTER_INTERFACE,
519 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
520 | USB_DEVICE_ID_MATCH_DEVICE,
522 .idProduct = 0x9032, /* SL-6000 */
523 ZAURUS_MASTER_INTERFACE,
526 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
527 | USB_DEVICE_ID_MATCH_DEVICE,
529 /* reported with some C860 units */
530 .idProduct = 0x9050, /* C-860 */
531 ZAURUS_MASTER_INTERFACE,
535 /* Olympus has some models with a Zaurus-compatible option.
536 * R-1000 uses a FreeScale i.MXL cpu (ARMv4T)
539 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
540 | USB_DEVICE_ID_MATCH_DEVICE,
542 .idProduct = 0x0F02, /* R-1000 */
543 ZAURUS_MASTER_INTERFACE,
550 * CDC Ether uses two interfaces, not necessarily consecutive.
551 * We match the main interface, ignoring the optional device
552 * class so we could handle devices that aren't exclusively
555 * NOTE: this match must come AFTER entries blacklisting devices
556 * because of bugs/quirks in a given product (like Zaurus, above).
559 USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ETHERNET,
561 .driver_info = (unsigned long) &cdc_info,
565 MODULE_DEVICE_TABLE(usb, products);
567 static struct usb_driver cdc_driver = {
569 .id_table = products,
570 .probe = usbnet_probe,
571 .disconnect = usbnet_disconnect,
572 .suspend = usbnet_suspend,
573 .resume = usbnet_resume,
577 static int __init cdc_init(void)
579 BUILD_BUG_ON((sizeof(((struct usbnet *)0)->data)
580 < sizeof(struct cdc_state)));
582 return usb_register(&cdc_driver);
584 module_init(cdc_init);
586 static void __exit cdc_exit(void)
588 usb_deregister(&cdc_driver);
590 module_exit(cdc_exit);
592 MODULE_AUTHOR("David Brownell");
593 MODULE_DESCRIPTION("USB CDC Ethernet devices");
594 MODULE_LICENSE("GPL");