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/module.h>
24 #include <linux/sched.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>
39 * probes control interface, claims data interface, collects the bulk
40 * endpoints, activates data interface (if needed), maybe sets MTU.
41 * all pure cdc, except for certain firmware workarounds, and knowing
42 * that rndis uses one different rule.
44 int usbnet_generic_cdc_bind(struct usbnet *dev, struct usb_interface *intf)
46 u8 *buf = intf->cur_altsetting->extra;
47 int len = intf->cur_altsetting->extralen;
48 struct usb_interface_descriptor *d;
49 struct cdc_state *info = (void *) &dev->data;
52 struct usb_driver *driver = driver_of(intf);
54 if (sizeof dev->data < sizeof *info)
57 /* expect strict spec conformance for the descriptors, but
58 * cope with firmware which stores them in the wrong place
60 if (len == 0 && dev->udev->actconfig->extralen) {
61 /* Motorola SB4100 (and others: Brad Hards says it's
62 * from a Broadcom design) put CDC descriptors here
64 buf = dev->udev->actconfig->extra;
65 len = dev->udev->actconfig->extralen;
68 "CDC descriptors on config\n");
71 /* this assumes that if there's a non-RNDIS vendor variant
72 * of cdc-acm, it'll fail RNDIS requests cleanly.
74 rndis = (intf->cur_altsetting->desc.bInterfaceProtocol == 0xff);
76 memset(info, 0, sizeof *info);
79 if (buf [1] != USB_DT_CS_INTERFACE)
82 /* use bDescriptorSubType to identify the CDC descriptors.
83 * We expect devices with CDC header and union descriptors.
84 * For CDC Ethernet we need the ethernet descriptor.
85 * For RNDIS, ignore two (pointless) CDC modem descriptors
86 * in favor of a complicated OID-based RPC scheme doing what
87 * CDC Ethernet achieves with a simple descriptor.
90 case USB_CDC_HEADER_TYPE:
92 dev_dbg(&intf->dev, "extra CDC header\n");
95 info->header = (void *) buf;
96 if (info->header->bLength != sizeof *info->header) {
97 dev_dbg(&intf->dev, "CDC header len %u\n",
98 info->header->bLength);
102 case USB_CDC_UNION_TYPE:
104 dev_dbg(&intf->dev, "extra CDC union\n");
107 info->u = (void *) buf;
108 if (info->u->bLength != sizeof *info->u) {
109 dev_dbg(&intf->dev, "CDC union len %u\n",
114 /* we need a master/control interface (what we're
115 * probed with) and a slave/data interface; union
116 * descriptors sort this all out.
118 info->control = usb_ifnum_to_if(dev->udev,
119 info->u->bMasterInterface0);
120 info->data = usb_ifnum_to_if(dev->udev,
121 info->u->bSlaveInterface0);
122 if (!info->control || !info->data) {
124 "master #%u/%p slave #%u/%p\n",
125 info->u->bMasterInterface0,
127 info->u->bSlaveInterface0,
131 if (info->control != intf) {
132 dev_dbg(&intf->dev, "bogus CDC Union\n");
133 /* Ambit USB Cable Modem (and maybe others)
134 * interchanges master and slave interface.
136 if (info->data == intf) {
137 info->data = info->control;
138 info->control = intf;
143 /* a data interface altsetting does the real i/o */
144 d = &info->data->cur_altsetting->desc;
145 if (d->bInterfaceClass != USB_CLASS_CDC_DATA) {
146 dev_dbg(&intf->dev, "slave class %u\n",
151 case USB_CDC_ETHERNET_TYPE:
153 dev_dbg(&intf->dev, "extra CDC ether\n");
156 info->ether = (void *) buf;
157 if (info->ether->bLength != sizeof *info->ether) {
158 dev_dbg(&intf->dev, "CDC ether len %u\n",
159 info->ether->bLength);
162 dev->hard_mtu = le16_to_cpu(
163 info->ether->wMaxSegmentSize);
164 /* because of Zaurus, we may be ignoring the host
165 * side link address we were given.
170 len -= buf [0]; /* bLength */
174 if (!info->header || !info->u || (!rndis && !info->ether)) {
175 dev_dbg(&intf->dev, "missing cdc %s%s%sdescriptor\n",
176 info->header ? "" : "header ",
177 info->u ? "" : "union ",
178 info->ether ? "" : "ether ");
182 /* claim data interface and set it up ... with side effects.
183 * network traffic can't flow until an altsetting is enabled.
185 status = usb_driver_claim_interface(driver, info->data, dev);
188 status = usbnet_get_endpoints(dev, info->data);
190 /* ensure immediate exit from usbnet_disconnect */
191 usb_set_intfdata(info->data, NULL);
192 usb_driver_release_interface(driver, info->data);
196 /* status endpoint: optional for CDC Ethernet, not RNDIS (or ACM) */
198 if (info->control->cur_altsetting->desc.bNumEndpoints == 1) {
199 struct usb_endpoint_descriptor *desc;
201 dev->status = &info->control->cur_altsetting->endpoint [0];
202 desc = &dev->status->desc;
203 if (desc->bmAttributes != USB_ENDPOINT_XFER_INT
204 || !(desc->bEndpointAddress & USB_DIR_IN)
205 || (le16_to_cpu(desc->wMaxPacketSize)
206 < sizeof(struct usb_cdc_notification))
207 || !desc->bInterval) {
208 dev_dbg(&intf->dev, "bad notification endpoint\n");
212 if (rndis && !dev->status) {
213 dev_dbg(&intf->dev, "missing RNDIS status endpoint\n");
214 usb_set_intfdata(info->data, NULL);
215 usb_driver_release_interface(driver, info->data);
221 dev_info(&dev->udev->dev, "bad CDC descriptors\n");
224 EXPORT_SYMBOL_GPL(usbnet_generic_cdc_bind);
226 void usbnet_cdc_unbind(struct usbnet *dev, struct usb_interface *intf)
228 struct cdc_state *info = (void *) &dev->data;
229 struct usb_driver *driver = driver_of(intf);
231 /* disconnect master --> disconnect slave */
232 if (intf == info->control && info->data) {
233 /* ensure immediate exit from usbnet_disconnect */
234 usb_set_intfdata(info->data, NULL);
235 usb_driver_release_interface(driver, info->data);
239 /* and vice versa (just in case) */
240 else if (intf == info->data && info->control) {
241 /* ensure immediate exit from usbnet_disconnect */
242 usb_set_intfdata(info->control, NULL);
243 usb_driver_release_interface(driver, info->control);
244 info->control = NULL;
247 EXPORT_SYMBOL_GPL(usbnet_cdc_unbind);
250 /*-------------------------------------------------------------------------
252 * Communications Device Class, Ethernet Control model
254 * Takes two interfaces. The DATA interface is inactive till an altsetting
255 * is selected. Configuration data includes class descriptors. There's
256 * an optional status endpoint on the control interface.
258 * This should interop with whatever the 2.4 "CDCEther.c" driver
259 * (by Brad Hards) talked with, with more functionality.
261 *-------------------------------------------------------------------------*/
263 static void dumpspeed(struct usbnet *dev, __le32 *speeds)
265 if (netif_msg_timer(dev))
266 devinfo(dev, "link speeds: %u kbps up, %u kbps down",
267 __le32_to_cpu(speeds[0]) / 1000,
268 __le32_to_cpu(speeds[1]) / 1000);
271 static void cdc_status(struct usbnet *dev, struct urb *urb)
273 struct usb_cdc_notification *event;
275 if (urb->actual_length < sizeof *event)
278 /* SPEED_CHANGE can get split into two 8-byte packets */
279 if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
280 dumpspeed(dev, (__le32 *) urb->transfer_buffer);
284 event = urb->transfer_buffer;
285 switch (event->bNotificationType) {
286 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
287 if (netif_msg_timer(dev))
288 devdbg(dev, "CDC: carrier %s",
289 event->wValue ? "on" : "off");
291 netif_carrier_on(dev->net);
293 netif_carrier_off(dev->net);
295 case USB_CDC_NOTIFY_SPEED_CHANGE: /* tx/rx rates */
296 if (netif_msg_timer(dev))
297 devdbg(dev, "CDC: speed change (len %d)",
299 if (urb->actual_length != (sizeof *event + 8))
300 set_bit(EVENT_STS_SPLIT, &dev->flags);
302 dumpspeed(dev, (__le32 *) &event[1]);
304 /* USB_CDC_NOTIFY_RESPONSE_AVAILABLE can happen too (e.g. RNDIS),
305 * but there are no standard formats for the response data.
308 deverr(dev, "CDC: unexpected notification %02x!",
309 event->bNotificationType);
314 static u8 nibble(unsigned char c)
316 if (likely(isdigit(c)))
319 if (likely(isxdigit(c)))
325 get_ethernet_addr(struct usbnet *dev, struct usb_cdc_ether_desc *e)
328 unsigned char buf [13];
330 tmp = usb_string(dev->udev, e->iMACAddress, buf, sizeof buf);
332 dev_dbg(&dev->udev->dev,
333 "bad MAC string %d fetch, %d\n", e->iMACAddress, tmp);
338 for (i = tmp = 0; i < 6; i++, tmp += 2)
339 dev->net->dev_addr [i] =
340 (nibble(buf [tmp]) << 4) + nibble(buf [tmp + 1]);
344 static int cdc_bind(struct usbnet *dev, struct usb_interface *intf)
347 struct cdc_state *info = (void *) &dev->data;
349 status = usbnet_generic_cdc_bind(dev, intf);
353 status = get_ethernet_addr(dev, info->ether);
355 usb_set_intfdata(info->data, NULL);
356 usb_driver_release_interface(driver_of(intf), info->data);
360 /* FIXME cdc-ether has some multicast code too, though it complains
361 * in routine cases. info->ether describes the multicast support.
362 * Implement that here, manipulating the cdc filter as needed.
367 static const struct driver_info cdc_info = {
368 .description = "CDC Ethernet Device",
370 // .check_connect = cdc_check_connect,
372 .unbind = usbnet_cdc_unbind,
373 .status = cdc_status,
376 /*-------------------------------------------------------------------------*/
379 static const struct usb_device_id products [] = {
383 * First blacklist any products that are egregiously nonconformant
384 * with the CDC Ethernet specs. Minor braindamage we cope with; when
385 * they're not even trying, needing a separate driver is only the first
386 * of the differences to show up.
389 #define ZAURUS_MASTER_INTERFACE \
390 .bInterfaceClass = USB_CLASS_COMM, \
391 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
392 .bInterfaceProtocol = USB_CDC_PROTO_NONE
394 /* SA-1100 based Sharp Zaurus ("collie"), or compatible;
395 * wire-incompatible with true CDC Ethernet implementations.
396 * (And, it seems, needlessly so...)
399 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
400 | USB_DEVICE_ID_MATCH_DEVICE,
403 ZAURUS_MASTER_INTERFACE,
407 /* PXA-25x based Sharp Zaurii. Note that it seems some of these
408 * (later models especially) may have shipped only with firmware
409 * advertising false "CDC MDLM" compatibility ... but we're not
410 * clear which models did that, so for now let's assume the worst.
413 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
414 | USB_DEVICE_ID_MATCH_DEVICE,
416 .idProduct = 0x8005, /* A-300 */
417 ZAURUS_MASTER_INTERFACE,
420 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
421 | USB_DEVICE_ID_MATCH_DEVICE,
423 .idProduct = 0x8006, /* B-500/SL-5600 */
424 ZAURUS_MASTER_INTERFACE,
427 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
428 | USB_DEVICE_ID_MATCH_DEVICE,
430 .idProduct = 0x8007, /* C-700 */
431 ZAURUS_MASTER_INTERFACE,
434 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
435 | USB_DEVICE_ID_MATCH_DEVICE,
437 .idProduct = 0x9031, /* C-750 C-760 */
438 ZAURUS_MASTER_INTERFACE,
441 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
442 | USB_DEVICE_ID_MATCH_DEVICE,
444 .idProduct = 0x9032, /* SL-6000 */
445 ZAURUS_MASTER_INTERFACE,
448 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
449 | USB_DEVICE_ID_MATCH_DEVICE,
451 /* reported with some C860 units */
452 .idProduct = 0x9050, /* C-860 */
453 ZAURUS_MASTER_INTERFACE,
457 /* Olympus has some models with a Zaurus-compatible option.
458 * R-1000 uses a FreeScale i.MXL cpu (ARMv4T)
461 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
462 | USB_DEVICE_ID_MATCH_DEVICE,
464 .idProduct = 0x0F02, /* R-1000 */
465 ZAURUS_MASTER_INTERFACE,
472 * CDC Ether uses two interfaces, not necessarily consecutive.
473 * We match the main interface, ignoring the optional device
474 * class so we could handle devices that aren't exclusively
477 * NOTE: this match must come AFTER entries blacklisting devices
478 * because of bugs/quirks in a given product (like Zaurus, above).
481 USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ETHERNET,
483 .driver_info = (unsigned long) &cdc_info,
487 MODULE_DEVICE_TABLE(usb, products);
489 static struct usb_driver cdc_driver = {
491 .id_table = products,
492 .probe = usbnet_probe,
493 .disconnect = usbnet_disconnect,
494 .suspend = usbnet_suspend,
495 .resume = usbnet_resume,
499 static int __init cdc_init(void)
501 BUG_ON((sizeof(((struct usbnet *)0)->data)
502 < sizeof(struct cdc_state)));
504 return usb_register(&cdc_driver);
506 module_init(cdc_init);
508 static void __exit cdc_exit(void)
510 usb_deregister(&cdc_driver);
512 module_exit(cdc_exit);
514 MODULE_AUTHOR("David Brownell");
515 MODULE_DESCRIPTION("USB CDC Ethernet devices");
516 MODULE_LICENSE("GPL");