2 * CDC Ethernet based networking peripherals
3 * Copyright (C) 2003-2005 by David Brownell
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // #define DEBUG // error path messages, extra info
21 // #define VERBOSE // more; success messages
23 #include <linux/config.h>
24 #ifdef CONFIG_USB_DEBUG
27 #include <linux/module.h>
28 #include <linux/sched.h>
29 #include <linux/init.h>
30 #include <linux/netdevice.h>
31 #include <linux/etherdevice.h>
32 #include <linux/ctype.h>
33 #include <linux/ethtool.h>
34 #include <linux/workqueue.h>
35 #include <linux/mii.h>
36 #include <linux/usb.h>
37 #include <linux/usb_cdc.h>
43 * probes control interface, claims data interface, collects the bulk
44 * endpoints, activates data interface (if needed), maybe sets MTU.
45 * all pure cdc, except for certain firmware workarounds, and knowing
46 * that rndis uses one different rule.
48 int usbnet_generic_cdc_bind(struct usbnet *dev, struct usb_interface *intf)
50 u8 *buf = intf->cur_altsetting->extra;
51 int len = intf->cur_altsetting->extralen;
52 struct usb_interface_descriptor *d;
53 struct cdc_state *info = (void *) &dev->data;
56 struct usb_driver *driver = driver_of(intf);
58 if (sizeof dev->data < sizeof *info)
61 /* expect strict spec conformance for the descriptors, but
62 * cope with firmware which stores them in the wrong place
64 if (len == 0 && dev->udev->actconfig->extralen) {
65 /* Motorola SB4100 (and others: Brad Hards says it's
66 * from a Broadcom design) put CDC descriptors here
68 buf = dev->udev->actconfig->extra;
69 len = dev->udev->actconfig->extralen;
72 "CDC descriptors on config\n");
75 /* this assumes that if there's a non-RNDIS vendor variant
76 * of cdc-acm, it'll fail RNDIS requests cleanly.
78 rndis = (intf->cur_altsetting->desc.bInterfaceProtocol == 0xff);
80 memset(info, 0, sizeof *info);
83 if (buf [1] != USB_DT_CS_INTERFACE)
86 /* use bDescriptorSubType to identify the CDC descriptors.
87 * We expect devices with CDC header and union descriptors.
88 * For CDC Ethernet we need the ethernet descriptor.
89 * For RNDIS, ignore two (pointless) CDC modem descriptors
90 * in favor of a complicated OID-based RPC scheme doing what
91 * CDC Ethernet achieves with a simple descriptor.
94 case USB_CDC_HEADER_TYPE:
96 dev_dbg(&intf->dev, "extra CDC header\n");
99 info->header = (void *) buf;
100 if (info->header->bLength != sizeof *info->header) {
101 dev_dbg(&intf->dev, "CDC header len %u\n",
102 info->header->bLength);
106 case USB_CDC_UNION_TYPE:
108 dev_dbg(&intf->dev, "extra CDC union\n");
111 info->u = (void *) buf;
112 if (info->u->bLength != sizeof *info->u) {
113 dev_dbg(&intf->dev, "CDC union len %u\n",
118 /* we need a master/control interface (what we're
119 * probed with) and a slave/data interface; union
120 * descriptors sort this all out.
122 info->control = usb_ifnum_to_if(dev->udev,
123 info->u->bMasterInterface0);
124 info->data = usb_ifnum_to_if(dev->udev,
125 info->u->bSlaveInterface0);
126 if (!info->control || !info->data) {
128 "master #%u/%p slave #%u/%p\n",
129 info->u->bMasterInterface0,
131 info->u->bSlaveInterface0,
135 if (info->control != intf) {
136 dev_dbg(&intf->dev, "bogus CDC Union\n");
137 /* Ambit USB Cable Modem (and maybe others)
138 * interchanges master and slave interface.
140 if (info->data == intf) {
141 info->data = info->control;
142 info->control = intf;
147 /* a data interface altsetting does the real i/o */
148 d = &info->data->cur_altsetting->desc;
149 if (d->bInterfaceClass != USB_CLASS_CDC_DATA) {
150 dev_dbg(&intf->dev, "slave class %u\n",
155 case USB_CDC_ETHERNET_TYPE:
157 dev_dbg(&intf->dev, "extra CDC ether\n");
160 info->ether = (void *) buf;
161 if (info->ether->bLength != sizeof *info->ether) {
162 dev_dbg(&intf->dev, "CDC ether len %u\n",
163 info->ether->bLength);
166 dev->hard_mtu = le16_to_cpu(
167 info->ether->wMaxSegmentSize);
168 /* because of Zaurus, we may be ignoring the host
169 * side link address we were given.
174 len -= buf [0]; /* bLength */
178 if (!info->header || !info->u || (!rndis && !info->ether)) {
179 dev_dbg(&intf->dev, "missing cdc %s%s%sdescriptor\n",
180 info->header ? "" : "header ",
181 info->u ? "" : "union ",
182 info->ether ? "" : "ether ");
186 /* claim data interface and set it up ... with side effects.
187 * network traffic can't flow until an altsetting is enabled.
189 status = usb_driver_claim_interface(driver, info->data, dev);
192 status = usbnet_get_endpoints(dev, info->data);
194 /* ensure immediate exit from usbnet_disconnect */
195 usb_set_intfdata(info->data, NULL);
196 usb_driver_release_interface(driver, info->data);
200 /* status endpoint: optional for CDC Ethernet, not RNDIS (or ACM) */
202 if (info->control->cur_altsetting->desc.bNumEndpoints == 1) {
203 struct usb_endpoint_descriptor *desc;
205 dev->status = &info->control->cur_altsetting->endpoint [0];
206 desc = &dev->status->desc;
207 if (desc->bmAttributes != USB_ENDPOINT_XFER_INT
208 || !(desc->bEndpointAddress & USB_DIR_IN)
209 || (le16_to_cpu(desc->wMaxPacketSize)
210 < sizeof(struct usb_cdc_notification))
211 || !desc->bInterval) {
212 dev_dbg(&intf->dev, "bad notification endpoint\n");
216 if (rndis && !dev->status) {
217 dev_dbg(&intf->dev, "missing RNDIS status endpoint\n");
218 usb_set_intfdata(info->data, NULL);
219 usb_driver_release_interface(driver, info->data);
225 dev_info(&dev->udev->dev, "bad CDC descriptors\n");
228 EXPORT_SYMBOL_GPL(usbnet_generic_cdc_bind);
230 void usbnet_cdc_unbind(struct usbnet *dev, struct usb_interface *intf)
232 struct cdc_state *info = (void *) &dev->data;
233 struct usb_driver *driver = driver_of(intf);
235 /* disconnect master --> disconnect slave */
236 if (intf == info->control && info->data) {
237 /* ensure immediate exit from usbnet_disconnect */
238 usb_set_intfdata(info->data, NULL);
239 usb_driver_release_interface(driver, info->data);
243 /* and vice versa (just in case) */
244 else if (intf == info->data && info->control) {
245 /* ensure immediate exit from usbnet_disconnect */
246 usb_set_intfdata(info->control, NULL);
247 usb_driver_release_interface(driver, info->control);
248 info->control = NULL;
251 EXPORT_SYMBOL_GPL(usbnet_cdc_unbind);
254 /*-------------------------------------------------------------------------
256 * Communications Device Class, Ethernet Control model
258 * Takes two interfaces. The DATA interface is inactive till an altsetting
259 * is selected. Configuration data includes class descriptors. There's
260 * an optional status endpoint on the control interface.
262 * This should interop with whatever the 2.4 "CDCEther.c" driver
263 * (by Brad Hards) talked with, with more functionality.
265 *-------------------------------------------------------------------------*/
267 static void dumpspeed(struct usbnet *dev, __le32 *speeds)
269 if (netif_msg_timer(dev))
270 devinfo(dev, "link speeds: %u kbps up, %u kbps down",
271 __le32_to_cpu(speeds[0]) / 1000,
272 __le32_to_cpu(speeds[1]) / 1000);
275 static void cdc_status(struct usbnet *dev, struct urb *urb)
277 struct usb_cdc_notification *event;
279 if (urb->actual_length < sizeof *event)
282 /* SPEED_CHANGE can get split into two 8-byte packets */
283 if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
284 dumpspeed(dev, (__le32 *) urb->transfer_buffer);
288 event = urb->transfer_buffer;
289 switch (event->bNotificationType) {
290 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
291 if (netif_msg_timer(dev))
292 devdbg(dev, "CDC: carrier %s",
293 event->wValue ? "on" : "off");
295 netif_carrier_on(dev->net);
297 netif_carrier_off(dev->net);
299 case USB_CDC_NOTIFY_SPEED_CHANGE: /* tx/rx rates */
300 if (netif_msg_timer(dev))
301 devdbg(dev, "CDC: speed change (len %d)",
303 if (urb->actual_length != (sizeof *event + 8))
304 set_bit(EVENT_STS_SPLIT, &dev->flags);
306 dumpspeed(dev, (__le32 *) &event[1]);
308 /* USB_CDC_NOTIFY_RESPONSE_AVAILABLE can happen too (e.g. RNDIS),
309 * but there are no standard formats for the response data.
312 deverr(dev, "CDC: unexpected notification %02x!",
313 event->bNotificationType);
318 static u8 nibble(unsigned char c)
320 if (likely(isdigit(c)))
323 if (likely(isxdigit(c)))
329 get_ethernet_addr(struct usbnet *dev, struct usb_cdc_ether_desc *e)
332 unsigned char buf [13];
334 tmp = usb_string(dev->udev, e->iMACAddress, buf, sizeof buf);
336 dev_dbg(&dev->udev->dev,
337 "bad MAC string %d fetch, %d\n", e->iMACAddress, tmp);
342 for (i = tmp = 0; i < 6; i++, tmp += 2)
343 dev->net->dev_addr [i] =
344 (nibble(buf [tmp]) << 4) + nibble(buf [tmp + 1]);
348 static int cdc_bind(struct usbnet *dev, struct usb_interface *intf)
351 struct cdc_state *info = (void *) &dev->data;
353 status = usbnet_generic_cdc_bind(dev, intf);
357 status = get_ethernet_addr(dev, info->ether);
359 usb_set_intfdata(info->data, NULL);
360 usb_driver_release_interface(driver_of(intf), info->data);
364 /* FIXME cdc-ether has some multicast code too, though it complains
365 * in routine cases. info->ether describes the multicast support.
366 * Implement that here, manipulating the cdc filter as needed.
371 static const struct driver_info cdc_info = {
372 .description = "CDC Ethernet Device",
374 // .check_connect = cdc_check_connect,
376 .unbind = usbnet_cdc_unbind,
377 .status = cdc_status,
380 /*-------------------------------------------------------------------------*/
383 static const struct usb_device_id products [] = {
387 * First blacklist any products that are egregiously nonconformant
388 * with the CDC Ethernet specs. Minor braindamage we cope with; when
389 * they're not even trying, needing a separate driver is only the first
390 * of the differences to show up.
393 #define ZAURUS_MASTER_INTERFACE \
394 .bInterfaceClass = USB_CLASS_COMM, \
395 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
396 .bInterfaceProtocol = USB_CDC_PROTO_NONE
398 /* SA-1100 based Sharp Zaurus ("collie"), or compatible;
399 * wire-incompatible with true CDC Ethernet implementations.
400 * (And, it seems, needlessly so...)
403 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
404 | USB_DEVICE_ID_MATCH_DEVICE,
407 ZAURUS_MASTER_INTERFACE,
411 /* PXA-25x based Sharp Zaurii. Note that it seems some of these
412 * (later models especially) may have shipped only with firmware
413 * advertising false "CDC MDLM" compatibility ... but we're not
414 * clear which models did that, so for now let's assume the worst.
417 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
418 | USB_DEVICE_ID_MATCH_DEVICE,
420 .idProduct = 0x8005, /* A-300 */
421 ZAURUS_MASTER_INTERFACE,
424 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
425 | USB_DEVICE_ID_MATCH_DEVICE,
427 .idProduct = 0x8006, /* B-500/SL-5600 */
428 ZAURUS_MASTER_INTERFACE,
431 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
432 | USB_DEVICE_ID_MATCH_DEVICE,
434 .idProduct = 0x8007, /* C-700 */
435 ZAURUS_MASTER_INTERFACE,
438 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
439 | USB_DEVICE_ID_MATCH_DEVICE,
441 .idProduct = 0x9031, /* C-750 C-760 */
442 ZAURUS_MASTER_INTERFACE,
445 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
446 | USB_DEVICE_ID_MATCH_DEVICE,
448 .idProduct = 0x9032, /* SL-6000 */
449 ZAURUS_MASTER_INTERFACE,
452 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
453 | USB_DEVICE_ID_MATCH_DEVICE,
455 /* reported with some C860 units */
456 .idProduct = 0x9050, /* C-860 */
457 ZAURUS_MASTER_INTERFACE,
464 * CDC Ether uses two interfaces, not necessarily consecutive.
465 * We match the main interface, ignoring the optional device
466 * class so we could handle devices that aren't exclusively
469 * NOTE: this match must come AFTER entries blacklisting devices
470 * because of bugs/quirks in a given product (like Zaurus, above).
473 USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ETHERNET,
475 .driver_info = (unsigned long) &cdc_info,
479 MODULE_DEVICE_TABLE(usb, products);
481 static struct usb_driver cdc_driver = {
482 .owner = THIS_MODULE,
484 .id_table = products,
485 .probe = usbnet_probe,
486 .disconnect = usbnet_disconnect,
487 .suspend = usbnet_suspend,
488 .resume = usbnet_resume,
492 static int __init cdc_init(void)
494 BUG_ON((sizeof(((struct usbnet *)0)->data)
495 < sizeof(struct cdc_state)));
497 return usb_register(&cdc_driver);
499 module_init(cdc_init);
501 static void __exit cdc_exit(void)
503 usb_deregister(&cdc_driver);
505 module_exit(cdc_exit);
507 MODULE_AUTHOR("David Brownell");
508 MODULE_DESCRIPTION("USB CDC Ethernet devices");
509 MODULE_LICENSE("GPL");