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 (!usb_endpoint_is_int_in(desc
)
204 || (le16_to_cpu(desc
->wMaxPacketSize
)
205 < sizeof(struct usb_cdc_notification
))
206 || !desc
->bInterval
) {
207 dev_dbg(&intf
->dev
, "bad notification endpoint\n");
211 if (rndis
&& !dev
->status
) {
212 dev_dbg(&intf
->dev
, "missing RNDIS status endpoint\n");
213 usb_set_intfdata(info
->data
, NULL
);
214 usb_driver_release_interface(driver
, info
->data
);
220 dev_info(&dev
->udev
->dev
, "bad CDC descriptors\n");
223 EXPORT_SYMBOL_GPL(usbnet_generic_cdc_bind
);
225 void usbnet_cdc_unbind(struct usbnet
*dev
, struct usb_interface
*intf
)
227 struct cdc_state
*info
= (void *) &dev
->data
;
228 struct usb_driver
*driver
= driver_of(intf
);
230 /* disconnect master --> disconnect slave */
231 if (intf
== info
->control
&& info
->data
) {
232 /* ensure immediate exit from usbnet_disconnect */
233 usb_set_intfdata(info
->data
, NULL
);
234 usb_driver_release_interface(driver
, info
->data
);
238 /* and vice versa (just in case) */
239 else if (intf
== info
->data
&& info
->control
) {
240 /* ensure immediate exit from usbnet_disconnect */
241 usb_set_intfdata(info
->control
, NULL
);
242 usb_driver_release_interface(driver
, info
->control
);
243 info
->control
= NULL
;
246 EXPORT_SYMBOL_GPL(usbnet_cdc_unbind
);
249 /*-------------------------------------------------------------------------
251 * Communications Device Class, Ethernet Control model
253 * Takes two interfaces. The DATA interface is inactive till an altsetting
254 * is selected. Configuration data includes class descriptors. There's
255 * an optional status endpoint on the control interface.
257 * This should interop with whatever the 2.4 "CDCEther.c" driver
258 * (by Brad Hards) talked with, with more functionality.
260 *-------------------------------------------------------------------------*/
262 static void dumpspeed(struct usbnet
*dev
, __le32
*speeds
)
264 if (netif_msg_timer(dev
))
265 devinfo(dev
, "link speeds: %u kbps up, %u kbps down",
266 __le32_to_cpu(speeds
[0]) / 1000,
267 __le32_to_cpu(speeds
[1]) / 1000);
270 static void cdc_status(struct usbnet
*dev
, struct urb
*urb
)
272 struct usb_cdc_notification
*event
;
274 if (urb
->actual_length
< sizeof *event
)
277 /* SPEED_CHANGE can get split into two 8-byte packets */
278 if (test_and_clear_bit(EVENT_STS_SPLIT
, &dev
->flags
)) {
279 dumpspeed(dev
, (__le32
*) urb
->transfer_buffer
);
283 event
= urb
->transfer_buffer
;
284 switch (event
->bNotificationType
) {
285 case USB_CDC_NOTIFY_NETWORK_CONNECTION
:
286 if (netif_msg_timer(dev
))
287 devdbg(dev
, "CDC: carrier %s",
288 event
->wValue
? "on" : "off");
290 netif_carrier_on(dev
->net
);
292 netif_carrier_off(dev
->net
);
294 case USB_CDC_NOTIFY_SPEED_CHANGE
: /* tx/rx rates */
295 if (netif_msg_timer(dev
))
296 devdbg(dev
, "CDC: speed change (len %d)",
298 if (urb
->actual_length
!= (sizeof *event
+ 8))
299 set_bit(EVENT_STS_SPLIT
, &dev
->flags
);
301 dumpspeed(dev
, (__le32
*) &event
[1]);
303 /* USB_CDC_NOTIFY_RESPONSE_AVAILABLE can happen too (e.g. RNDIS),
304 * but there are no standard formats for the response data.
307 deverr(dev
, "CDC: unexpected notification %02x!",
308 event
->bNotificationType
);
313 static u8
nibble(unsigned char c
)
315 if (likely(isdigit(c
)))
318 if (likely(isxdigit(c
)))
324 get_ethernet_addr(struct usbnet
*dev
, struct usb_cdc_ether_desc
*e
)
327 unsigned char buf
[13];
329 tmp
= usb_string(dev
->udev
, e
->iMACAddress
, buf
, sizeof buf
);
331 dev_dbg(&dev
->udev
->dev
,
332 "bad MAC string %d fetch, %d\n", e
->iMACAddress
, tmp
);
337 for (i
= tmp
= 0; i
< 6; i
++, tmp
+= 2)
338 dev
->net
->dev_addr
[i
] =
339 (nibble(buf
[tmp
]) << 4) + nibble(buf
[tmp
+ 1]);
343 static int cdc_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
346 struct cdc_state
*info
= (void *) &dev
->data
;
348 status
= usbnet_generic_cdc_bind(dev
, intf
);
352 status
= get_ethernet_addr(dev
, info
->ether
);
354 usb_set_intfdata(info
->data
, NULL
);
355 usb_driver_release_interface(driver_of(intf
), info
->data
);
359 /* FIXME cdc-ether has some multicast code too, though it complains
360 * in routine cases. info->ether describes the multicast support.
361 * Implement that here, manipulating the cdc filter as needed.
366 static const struct driver_info cdc_info
= {
367 .description
= "CDC Ethernet Device",
369 // .check_connect = cdc_check_connect,
371 .unbind
= usbnet_cdc_unbind
,
372 .status
= cdc_status
,
375 /*-------------------------------------------------------------------------*/
378 static const struct usb_device_id products
[] = {
382 * First blacklist any products that are egregiously nonconformant
383 * with the CDC Ethernet specs. Minor braindamage we cope with; when
384 * they're not even trying, needing a separate driver is only the first
385 * of the differences to show up.
388 #define ZAURUS_MASTER_INTERFACE \
389 .bInterfaceClass = USB_CLASS_COMM, \
390 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
391 .bInterfaceProtocol = USB_CDC_PROTO_NONE
393 /* SA-1100 based Sharp Zaurus ("collie"), or compatible;
394 * wire-incompatible with true CDC Ethernet implementations.
395 * (And, it seems, needlessly so...)
398 .match_flags
= USB_DEVICE_ID_MATCH_INT_INFO
399 | USB_DEVICE_ID_MATCH_DEVICE
,
402 ZAURUS_MASTER_INTERFACE
,
406 /* PXA-25x based Sharp Zaurii. Note that it seems some of these
407 * (later models especially) may have shipped only with firmware
408 * advertising false "CDC MDLM" compatibility ... but we're not
409 * clear which models did that, so for now let's assume the worst.
412 .match_flags
= USB_DEVICE_ID_MATCH_INT_INFO
413 | USB_DEVICE_ID_MATCH_DEVICE
,
415 .idProduct
= 0x8005, /* A-300 */
416 ZAURUS_MASTER_INTERFACE
,
419 .match_flags
= USB_DEVICE_ID_MATCH_INT_INFO
420 | USB_DEVICE_ID_MATCH_DEVICE
,
422 .idProduct
= 0x8006, /* B-500/SL-5600 */
423 ZAURUS_MASTER_INTERFACE
,
426 .match_flags
= USB_DEVICE_ID_MATCH_INT_INFO
427 | USB_DEVICE_ID_MATCH_DEVICE
,
429 .idProduct
= 0x8007, /* C-700 */
430 ZAURUS_MASTER_INTERFACE
,
433 .match_flags
= USB_DEVICE_ID_MATCH_INT_INFO
434 | USB_DEVICE_ID_MATCH_DEVICE
,
436 .idProduct
= 0x9031, /* C-750 C-760 */
437 ZAURUS_MASTER_INTERFACE
,
440 .match_flags
= USB_DEVICE_ID_MATCH_INT_INFO
441 | USB_DEVICE_ID_MATCH_DEVICE
,
443 .idProduct
= 0x9032, /* SL-6000 */
444 ZAURUS_MASTER_INTERFACE
,
447 .match_flags
= USB_DEVICE_ID_MATCH_INT_INFO
448 | USB_DEVICE_ID_MATCH_DEVICE
,
450 /* reported with some C860 units */
451 .idProduct
= 0x9050, /* C-860 */
452 ZAURUS_MASTER_INTERFACE
,
456 /* Olympus has some models with a Zaurus-compatible option.
457 * R-1000 uses a FreeScale i.MXL cpu (ARMv4T)
460 .match_flags
= USB_DEVICE_ID_MATCH_INT_INFO
461 | USB_DEVICE_ID_MATCH_DEVICE
,
463 .idProduct
= 0x0F02, /* R-1000 */
464 ZAURUS_MASTER_INTERFACE
,
471 * CDC Ether uses two interfaces, not necessarily consecutive.
472 * We match the main interface, ignoring the optional device
473 * class so we could handle devices that aren't exclusively
476 * NOTE: this match must come AFTER entries blacklisting devices
477 * because of bugs/quirks in a given product (like Zaurus, above).
480 USB_INTERFACE_INFO(USB_CLASS_COMM
, USB_CDC_SUBCLASS_ETHERNET
,
482 .driver_info
= (unsigned long) &cdc_info
,
486 MODULE_DEVICE_TABLE(usb
, products
);
488 static struct usb_driver cdc_driver
= {
490 .id_table
= products
,
491 .probe
= usbnet_probe
,
492 .disconnect
= usbnet_disconnect
,
493 .suspend
= usbnet_suspend
,
494 .resume
= usbnet_resume
,
498 static int __init
cdc_init(void)
500 BUILD_BUG_ON((sizeof(((struct usbnet
*)0)->data
)
501 < sizeof(struct cdc_state
)));
503 return usb_register(&cdc_driver
);
505 module_init(cdc_init
);
507 static void __exit
cdc_exit(void)
509 usb_deregister(&cdc_driver
);
511 module_exit(cdc_exit
);
513 MODULE_AUTHOR("David Brownell");
514 MODULE_DESCRIPTION("USB CDC Ethernet devices");
515 MODULE_LICENSE("GPL");