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 #include <linux/module.h>
25 #include <linux/sched.h>
26 #include <linux/init.h>
27 #include <linux/netdevice.h>
28 #include <linux/etherdevice.h>
29 #include <linux/ctype.h>
30 #include <linux/ethtool.h>
31 #include <linux/workqueue.h>
32 #include <linux/mii.h>
33 #include <linux/usb.h>
34 #include <linux/usb_cdc.h>
40 * probes control interface, claims data interface, collects the bulk
41 * endpoints, activates data interface (if needed), maybe sets MTU.
42 * all pure cdc, except for certain firmware workarounds, and knowing
43 * that rndis uses one different rule.
45 int usbnet_generic_cdc_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
47 u8
*buf
= intf
->cur_altsetting
->extra
;
48 int len
= intf
->cur_altsetting
->extralen
;
49 struct usb_interface_descriptor
*d
;
50 struct cdc_state
*info
= (void *) &dev
->data
;
53 struct usb_driver
*driver
= driver_of(intf
);
55 if (sizeof dev
->data
< sizeof *info
)
58 /* expect strict spec conformance for the descriptors, but
59 * cope with firmware which stores them in the wrong place
61 if (len
== 0 && dev
->udev
->actconfig
->extralen
) {
62 /* Motorola SB4100 (and others: Brad Hards says it's
63 * from a Broadcom design) put CDC descriptors here
65 buf
= dev
->udev
->actconfig
->extra
;
66 len
= dev
->udev
->actconfig
->extralen
;
69 "CDC descriptors on config\n");
72 /* this assumes that if there's a non-RNDIS vendor variant
73 * of cdc-acm, it'll fail RNDIS requests cleanly.
75 rndis
= (intf
->cur_altsetting
->desc
.bInterfaceProtocol
== 0xff);
77 memset(info
, 0, sizeof *info
);
80 if (buf
[1] != USB_DT_CS_INTERFACE
)
83 /* use bDescriptorSubType to identify the CDC descriptors.
84 * We expect devices with CDC header and union descriptors.
85 * For CDC Ethernet we need the ethernet descriptor.
86 * For RNDIS, ignore two (pointless) CDC modem descriptors
87 * in favor of a complicated OID-based RPC scheme doing what
88 * CDC Ethernet achieves with a simple descriptor.
91 case USB_CDC_HEADER_TYPE
:
93 dev_dbg(&intf
->dev
, "extra CDC header\n");
96 info
->header
= (void *) buf
;
97 if (info
->header
->bLength
!= sizeof *info
->header
) {
98 dev_dbg(&intf
->dev
, "CDC header len %u\n",
99 info
->header
->bLength
);
103 case USB_CDC_UNION_TYPE
:
105 dev_dbg(&intf
->dev
, "extra CDC union\n");
108 info
->u
= (void *) buf
;
109 if (info
->u
->bLength
!= sizeof *info
->u
) {
110 dev_dbg(&intf
->dev
, "CDC union len %u\n",
115 /* we need a master/control interface (what we're
116 * probed with) and a slave/data interface; union
117 * descriptors sort this all out.
119 info
->control
= usb_ifnum_to_if(dev
->udev
,
120 info
->u
->bMasterInterface0
);
121 info
->data
= usb_ifnum_to_if(dev
->udev
,
122 info
->u
->bSlaveInterface0
);
123 if (!info
->control
|| !info
->data
) {
125 "master #%u/%p slave #%u/%p\n",
126 info
->u
->bMasterInterface0
,
128 info
->u
->bSlaveInterface0
,
132 if (info
->control
!= intf
) {
133 dev_dbg(&intf
->dev
, "bogus CDC Union\n");
134 /* Ambit USB Cable Modem (and maybe others)
135 * interchanges master and slave interface.
137 if (info
->data
== intf
) {
138 info
->data
= info
->control
;
139 info
->control
= intf
;
144 /* a data interface altsetting does the real i/o */
145 d
= &info
->data
->cur_altsetting
->desc
;
146 if (d
->bInterfaceClass
!= USB_CLASS_CDC_DATA
) {
147 dev_dbg(&intf
->dev
, "slave class %u\n",
152 case USB_CDC_ETHERNET_TYPE
:
154 dev_dbg(&intf
->dev
, "extra CDC ether\n");
157 info
->ether
= (void *) buf
;
158 if (info
->ether
->bLength
!= sizeof *info
->ether
) {
159 dev_dbg(&intf
->dev
, "CDC ether len %u\n",
160 info
->ether
->bLength
);
163 dev
->hard_mtu
= le16_to_cpu(
164 info
->ether
->wMaxSegmentSize
);
165 /* because of Zaurus, we may be ignoring the host
166 * side link address we were given.
171 len
-= buf
[0]; /* bLength */
175 if (!info
->header
|| !info
->u
|| (!rndis
&& !info
->ether
)) {
176 dev_dbg(&intf
->dev
, "missing cdc %s%s%sdescriptor\n",
177 info
->header
? "" : "header ",
178 info
->u
? "" : "union ",
179 info
->ether
? "" : "ether ");
183 /* claim data interface and set it up ... with side effects.
184 * network traffic can't flow until an altsetting is enabled.
186 status
= usb_driver_claim_interface(driver
, info
->data
, dev
);
189 status
= usbnet_get_endpoints(dev
, info
->data
);
191 /* ensure immediate exit from usbnet_disconnect */
192 usb_set_intfdata(info
->data
, NULL
);
193 usb_driver_release_interface(driver
, info
->data
);
197 /* status endpoint: optional for CDC Ethernet, not RNDIS (or ACM) */
199 if (info
->control
->cur_altsetting
->desc
.bNumEndpoints
== 1) {
200 struct usb_endpoint_descriptor
*desc
;
202 dev
->status
= &info
->control
->cur_altsetting
->endpoint
[0];
203 desc
= &dev
->status
->desc
;
204 if (desc
->bmAttributes
!= USB_ENDPOINT_XFER_INT
205 || !(desc
->bEndpointAddress
& USB_DIR_IN
)
206 || (le16_to_cpu(desc
->wMaxPacketSize
)
207 < sizeof(struct usb_cdc_notification
))
208 || !desc
->bInterval
) {
209 dev_dbg(&intf
->dev
, "bad notification endpoint\n");
213 if (rndis
&& !dev
->status
) {
214 dev_dbg(&intf
->dev
, "missing RNDIS status endpoint\n");
215 usb_set_intfdata(info
->data
, NULL
);
216 usb_driver_release_interface(driver
, info
->data
);
222 dev_info(&dev
->udev
->dev
, "bad CDC descriptors\n");
225 EXPORT_SYMBOL_GPL(usbnet_generic_cdc_bind
);
227 void usbnet_cdc_unbind(struct usbnet
*dev
, struct usb_interface
*intf
)
229 struct cdc_state
*info
= (void *) &dev
->data
;
230 struct usb_driver
*driver
= driver_of(intf
);
232 /* disconnect master --> disconnect slave */
233 if (intf
== info
->control
&& info
->data
) {
234 /* ensure immediate exit from usbnet_disconnect */
235 usb_set_intfdata(info
->data
, NULL
);
236 usb_driver_release_interface(driver
, info
->data
);
240 /* and vice versa (just in case) */
241 else if (intf
== info
->data
&& info
->control
) {
242 /* ensure immediate exit from usbnet_disconnect */
243 usb_set_intfdata(info
->control
, NULL
);
244 usb_driver_release_interface(driver
, info
->control
);
245 info
->control
= NULL
;
248 EXPORT_SYMBOL_GPL(usbnet_cdc_unbind
);
251 /*-------------------------------------------------------------------------
253 * Communications Device Class, Ethernet Control model
255 * Takes two interfaces. The DATA interface is inactive till an altsetting
256 * is selected. Configuration data includes class descriptors. There's
257 * an optional status endpoint on the control interface.
259 * This should interop with whatever the 2.4 "CDCEther.c" driver
260 * (by Brad Hards) talked with, with more functionality.
262 *-------------------------------------------------------------------------*/
264 static void dumpspeed(struct usbnet
*dev
, __le32
*speeds
)
266 if (netif_msg_timer(dev
))
267 devinfo(dev
, "link speeds: %u kbps up, %u kbps down",
268 __le32_to_cpu(speeds
[0]) / 1000,
269 __le32_to_cpu(speeds
[1]) / 1000);
272 static void cdc_status(struct usbnet
*dev
, struct urb
*urb
)
274 struct usb_cdc_notification
*event
;
276 if (urb
->actual_length
< sizeof *event
)
279 /* SPEED_CHANGE can get split into two 8-byte packets */
280 if (test_and_clear_bit(EVENT_STS_SPLIT
, &dev
->flags
)) {
281 dumpspeed(dev
, (__le32
*) urb
->transfer_buffer
);
285 event
= urb
->transfer_buffer
;
286 switch (event
->bNotificationType
) {
287 case USB_CDC_NOTIFY_NETWORK_CONNECTION
:
288 if (netif_msg_timer(dev
))
289 devdbg(dev
, "CDC: carrier %s",
290 event
->wValue
? "on" : "off");
292 netif_carrier_on(dev
->net
);
294 netif_carrier_off(dev
->net
);
296 case USB_CDC_NOTIFY_SPEED_CHANGE
: /* tx/rx rates */
297 if (netif_msg_timer(dev
))
298 devdbg(dev
, "CDC: speed change (len %d)",
300 if (urb
->actual_length
!= (sizeof *event
+ 8))
301 set_bit(EVENT_STS_SPLIT
, &dev
->flags
);
303 dumpspeed(dev
, (__le32
*) &event
[1]);
305 /* USB_CDC_NOTIFY_RESPONSE_AVAILABLE can happen too (e.g. RNDIS),
306 * but there are no standard formats for the response data.
309 deverr(dev
, "CDC: unexpected notification %02x!",
310 event
->bNotificationType
);
315 static u8
nibble(unsigned char c
)
317 if (likely(isdigit(c
)))
320 if (likely(isxdigit(c
)))
326 get_ethernet_addr(struct usbnet
*dev
, struct usb_cdc_ether_desc
*e
)
329 unsigned char buf
[13];
331 tmp
= usb_string(dev
->udev
, e
->iMACAddress
, buf
, sizeof buf
);
333 dev_dbg(&dev
->udev
->dev
,
334 "bad MAC string %d fetch, %d\n", e
->iMACAddress
, tmp
);
339 for (i
= tmp
= 0; i
< 6; i
++, tmp
+= 2)
340 dev
->net
->dev_addr
[i
] =
341 (nibble(buf
[tmp
]) << 4) + nibble(buf
[tmp
+ 1]);
345 static int cdc_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
348 struct cdc_state
*info
= (void *) &dev
->data
;
350 status
= usbnet_generic_cdc_bind(dev
, intf
);
354 status
= get_ethernet_addr(dev
, info
->ether
);
356 usb_set_intfdata(info
->data
, NULL
);
357 usb_driver_release_interface(driver_of(intf
), info
->data
);
361 /* FIXME cdc-ether has some multicast code too, though it complains
362 * in routine cases. info->ether describes the multicast support.
363 * Implement that here, manipulating the cdc filter as needed.
368 static const struct driver_info cdc_info
= {
369 .description
= "CDC Ethernet Device",
371 // .check_connect = cdc_check_connect,
373 .unbind
= usbnet_cdc_unbind
,
374 .status
= cdc_status
,
377 /*-------------------------------------------------------------------------*/
380 static const struct usb_device_id products
[] = {
384 * First blacklist any products that are egregiously nonconformant
385 * with the CDC Ethernet specs. Minor braindamage we cope with; when
386 * they're not even trying, needing a separate driver is only the first
387 * of the differences to show up.
390 #define ZAURUS_MASTER_INTERFACE \
391 .bInterfaceClass = USB_CLASS_COMM, \
392 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
393 .bInterfaceProtocol = USB_CDC_PROTO_NONE
395 /* SA-1100 based Sharp Zaurus ("collie"), or compatible;
396 * wire-incompatible with true CDC Ethernet implementations.
397 * (And, it seems, needlessly so...)
400 .match_flags
= USB_DEVICE_ID_MATCH_INT_INFO
401 | USB_DEVICE_ID_MATCH_DEVICE
,
404 ZAURUS_MASTER_INTERFACE
,
408 /* PXA-25x based Sharp Zaurii. Note that it seems some of these
409 * (later models especially) may have shipped only with firmware
410 * advertising false "CDC MDLM" compatibility ... but we're not
411 * clear which models did that, so for now let's assume the worst.
414 .match_flags
= USB_DEVICE_ID_MATCH_INT_INFO
415 | USB_DEVICE_ID_MATCH_DEVICE
,
417 .idProduct
= 0x8005, /* A-300 */
418 ZAURUS_MASTER_INTERFACE
,
421 .match_flags
= USB_DEVICE_ID_MATCH_INT_INFO
422 | USB_DEVICE_ID_MATCH_DEVICE
,
424 .idProduct
= 0x8006, /* B-500/SL-5600 */
425 ZAURUS_MASTER_INTERFACE
,
428 .match_flags
= USB_DEVICE_ID_MATCH_INT_INFO
429 | USB_DEVICE_ID_MATCH_DEVICE
,
431 .idProduct
= 0x8007, /* C-700 */
432 ZAURUS_MASTER_INTERFACE
,
435 .match_flags
= USB_DEVICE_ID_MATCH_INT_INFO
436 | USB_DEVICE_ID_MATCH_DEVICE
,
438 .idProduct
= 0x9031, /* C-750 C-760 */
439 ZAURUS_MASTER_INTERFACE
,
442 .match_flags
= USB_DEVICE_ID_MATCH_INT_INFO
443 | USB_DEVICE_ID_MATCH_DEVICE
,
445 .idProduct
= 0x9032, /* SL-6000 */
446 ZAURUS_MASTER_INTERFACE
,
449 .match_flags
= USB_DEVICE_ID_MATCH_INT_INFO
450 | USB_DEVICE_ID_MATCH_DEVICE
,
452 /* reported with some C860 units */
453 .idProduct
= 0x9050, /* C-860 */
454 ZAURUS_MASTER_INTERFACE
,
461 * CDC Ether uses two interfaces, not necessarily consecutive.
462 * We match the main interface, ignoring the optional device
463 * class so we could handle devices that aren't exclusively
466 * NOTE: this match must come AFTER entries blacklisting devices
467 * because of bugs/quirks in a given product (like Zaurus, above).
470 USB_INTERFACE_INFO(USB_CLASS_COMM
, USB_CDC_SUBCLASS_ETHERNET
,
472 .driver_info
= (unsigned long) &cdc_info
,
476 MODULE_DEVICE_TABLE(usb
, products
);
478 static struct usb_driver cdc_driver
= {
480 .id_table
= products
,
481 .probe
= usbnet_probe
,
482 .disconnect
= usbnet_disconnect
,
483 .suspend
= usbnet_suspend
,
484 .resume
= usbnet_resume
,
488 static int __init
cdc_init(void)
490 BUG_ON((sizeof(((struct usbnet
*)0)->data
)
491 < sizeof(struct cdc_state
)));
493 return usb_register(&cdc_driver
);
495 module_init(cdc_init
);
497 static void __exit
cdc_exit(void)
499 usb_deregister(&cdc_driver
);
501 module_exit(cdc_exit
);
503 MODULE_AUTHOR("David Brownell");
504 MODULE_DESCRIPTION("USB CDC Ethernet devices");
505 MODULE_LICENSE("GPL");