update/add new 3G modem modules
[tomato.git] / release / src-rt / linux / linux-2.6 / drivers / net / usb / cdc_ether.c
blob718c320761eff56e24358c062cb40d7ca217c134
1 /*
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/ethtool.h>
29 #include <linux/workqueue.h>
30 #include <linux/mii.h>
31 #include <linux/usb.h>
32 #include <linux/usb/cdc.h>
34 #include "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;
60 #else
62 #define is_rndis(desc) 0
63 #define is_activesync(desc) 0
64 #define is_wireless_rndis(desc) 0
66 #endif
68 static const u8 mbm_guid[16] = {
69 0xa3, 0x17, 0xa8, 0x8b, 0x04, 0x5e, 0x4f, 0x01,
70 0xa6, 0x07, 0xc0, 0xff, 0xcb, 0x7e, 0x39, 0x2a,
74 * probes control interface, claims data interface, collects the bulk
75 * endpoints, activates data interface (if needed), maybe sets MTU.
76 * all pure cdc, except for certain firmware workarounds, and knowing
77 * that rndis uses one different rule.
79 int usbnet_generic_cdc_bind(struct usbnet *dev, struct usb_interface *intf)
81 u8 *buf = intf->cur_altsetting->extra;
82 int len = intf->cur_altsetting->extralen;
83 struct usb_interface_descriptor *d;
84 struct cdc_state *info = (void *) &dev->data;
85 int status;
86 int rndis;
87 bool android_rndis_quirk = false;
88 struct usb_driver *driver = driver_of(intf);
89 struct usb_cdc_mdlm_desc *desc = NULL;
90 struct usb_cdc_mdlm_detail_desc *detail = NULL;
92 if (sizeof dev->data < sizeof *info)
93 return -EDOM;
95 /* expect strict spec conformance for the descriptors, but
96 * cope with firmware which stores them in the wrong place
98 if (len == 0 && dev->udev->actconfig->extralen) {
99 /* Motorola SB4100 (and others: Brad Hards says it's
100 * from a Broadcom design) put CDC descriptors here
102 buf = dev->udev->actconfig->extra;
103 len = dev->udev->actconfig->extralen;
104 if (len)
105 dev_dbg(&intf->dev,
106 "CDC descriptors on config\n");
109 /* Maybe CDC descriptors are after the endpoint? This bug has
110 * been seen on some 2Wire Inc RNDIS-ish products.
112 if (len == 0) {
113 struct usb_host_endpoint *hep;
115 hep = intf->cur_altsetting->endpoint;
116 if (hep) {
117 buf = hep->extra;
118 len = hep->extralen;
120 if (len)
121 dev_dbg(&intf->dev,
122 "CDC descriptors on endpoint\n");
125 /* this assumes that if there's a non-RNDIS vendor variant
126 * of cdc-acm, it'll fail RNDIS requests cleanly.
128 rndis = is_rndis(&intf->cur_altsetting->desc)
129 || is_activesync(&intf->cur_altsetting->desc)
130 || is_wireless_rndis(&intf->cur_altsetting->desc);
132 memset(info, 0, sizeof *info);
133 info->control = intf;
134 while (len > 3) {
135 if (buf [1] != USB_DT_CS_INTERFACE)
136 goto next_desc;
138 /* use bDescriptorSubType to identify the CDC descriptors.
139 * We expect devices with CDC header and union descriptors.
140 * For CDC Ethernet we need the ethernet descriptor.
141 * For RNDIS, ignore two (pointless) CDC modem descriptors
142 * in favor of a complicated OID-based RPC scheme doing what
143 * CDC Ethernet achieves with a simple descriptor.
145 switch (buf [2]) {
146 case USB_CDC_HEADER_TYPE:
147 if (info->header) {
148 dev_dbg(&intf->dev, "extra CDC header\n");
149 goto bad_desc;
151 info->header = (void *) buf;
152 if (info->header->bLength != sizeof *info->header) {
153 dev_dbg(&intf->dev, "CDC header len %u\n",
154 info->header->bLength);
155 goto bad_desc;
157 break;
158 case USB_CDC_ACM_TYPE:
159 /* paranoia: disambiguate a "real" vendor-specific
160 * modem interface from an RNDIS non-modem.
162 if (rndis) {
163 struct usb_cdc_acm_descriptor *acm;
165 acm = (void *) buf;
166 if (acm->bmCapabilities) {
167 dev_dbg(&intf->dev,
168 "ACM capabilities %02x, "
169 "not really RNDIS?\n",
170 acm->bmCapabilities);
171 goto bad_desc;
174 break;
175 case USB_CDC_UNION_TYPE:
176 if (info->u) {
177 dev_dbg(&intf->dev, "extra CDC union\n");
178 goto bad_desc;
180 info->u = (void *) buf;
181 if (info->u->bLength != sizeof *info->u) {
182 dev_dbg(&intf->dev, "CDC union len %u\n",
183 info->u->bLength);
184 goto bad_desc;
187 /* we need a master/control interface (what we're
188 * probed with) and a slave/data interface; union
189 * descriptors sort this all out.
191 info->control = usb_ifnum_to_if(dev->udev,
192 info->u->bMasterInterface0);
193 info->data = usb_ifnum_to_if(dev->udev,
194 info->u->bSlaveInterface0);
195 if (!info->control || !info->data) {
196 dev_dbg(&intf->dev,
197 "master #%u/%p slave #%u/%p\n",
198 info->u->bMasterInterface0,
199 info->control,
200 info->u->bSlaveInterface0,
201 info->data);
202 /* fall back to hard-wiring for RNDIS */
203 if (rndis) {
204 android_rndis_quirk = true;
205 goto next_desc;
207 goto bad_desc;
209 if (info->control != intf) {
210 dev_dbg(&intf->dev, "bogus CDC Union\n");
211 /* Ambit USB Cable Modem (and maybe others)
212 * interchanges master and slave interface.
214 if (info->data == intf) {
215 info->data = info->control;
216 info->control = intf;
217 } else
218 goto bad_desc;
221 /* some devices merge these - skip class check */
222 if (info->control == info->data)
223 goto next_desc;
225 /* a data interface altsetting does the real i/o */
226 d = &info->data->cur_altsetting->desc;
227 if (d->bInterfaceClass != USB_CLASS_CDC_DATA) {
228 dev_dbg(&intf->dev, "slave class %u\n",
229 d->bInterfaceClass);
230 goto bad_desc;
232 break;
233 case USB_CDC_ETHERNET_TYPE:
234 if (info->ether) {
235 dev_dbg(&intf->dev, "extra CDC ether\n");
236 goto bad_desc;
238 info->ether = (void *) buf;
239 if (info->ether->bLength != sizeof *info->ether) {
240 dev_dbg(&intf->dev, "CDC ether len %u\n",
241 info->ether->bLength);
242 goto bad_desc;
244 dev->hard_mtu = le16_to_cpu(
245 info->ether->wMaxSegmentSize);
246 /* because of Zaurus, we may be ignoring the host
247 * side link address we were given.
249 break;
250 case USB_CDC_MDLM_TYPE:
251 if (desc) {
252 dev_dbg(&intf->dev, "extra MDLM descriptor\n");
253 goto bad_desc;
256 desc = (void *)buf;
258 if (desc->bLength != sizeof(*desc))
259 goto bad_desc;
261 if (memcmp(&desc->bGUID, mbm_guid, 16))
262 goto bad_desc;
263 break;
264 case USB_CDC_MDLM_DETAIL_TYPE:
265 if (detail) {
266 dev_dbg(&intf->dev, "extra MDLM detail descriptor\n");
267 goto bad_desc;
270 detail = (void *)buf;
272 if (detail->bGuidDescriptorType == 0) {
273 if (detail->bLength < (sizeof(*detail) + 1))
274 goto bad_desc;
275 } else
276 goto bad_desc;
277 break;
279 next_desc:
280 len -= buf [0]; /* bLength */
281 buf += buf [0];
284 /* Microsoft ActiveSync based and some regular RNDIS devices lack the
285 * CDC descriptors, so we'll hard-wire the interfaces and not check
286 * for descriptors.
288 * Some Android RNDIS devices have a CDC Union descriptor pointing
289 * to non-existing interfaces. Ignore that and attempt the same
290 * hard-wired 0 and 1 interfaces.
292 if (rndis && (!info->u || android_rndis_quirk)) {
293 info->control = usb_ifnum_to_if(dev->udev, 0);
294 info->data = usb_ifnum_to_if(dev->udev, 1);
295 if (!info->control || !info->data || info->control != intf) {
296 dev_dbg(&intf->dev,
297 "rndis: master #0/%p slave #1/%p\n",
298 info->control,
299 info->data);
300 goto bad_desc;
303 } else if (!info->header || !info->u || (!rndis && !info->ether)) {
304 dev_dbg(&intf->dev, "missing cdc %s%s%sdescriptor\n",
305 info->header ? "" : "header ",
306 info->u ? "" : "union ",
307 info->ether ? "" : "ether ");
308 goto bad_desc;
311 /* claim data interface and set it up ... with side effects.
312 * network traffic can't flow until an altsetting is enabled.
314 if (info->data != info->control) {
315 status = usb_driver_claim_interface(driver, info->data, dev);
316 if (status < 0)
317 return status;
319 status = usbnet_get_endpoints(dev, info->data);
320 if (status < 0) {
321 /* ensure immediate exit from usbnet_disconnect */
322 usb_set_intfdata(info->data, NULL);
323 if (info->data != info->control)
324 usb_driver_release_interface(driver, info->data);
325 return status;
328 /* status endpoint: optional for CDC Ethernet, not RNDIS (or ACM) */
329 if (info->data != info->control)
330 dev->status = NULL;
331 if (info->control->cur_altsetting->desc.bNumEndpoints == 1) {
332 struct usb_endpoint_descriptor *desc;
334 dev->status = &info->control->cur_altsetting->endpoint [0];
335 desc = &dev->status->desc;
336 if (!usb_endpoint_is_int_in(desc)
337 || (le16_to_cpu(desc->wMaxPacketSize)
338 < sizeof(struct usb_cdc_notification))
339 || !desc->bInterval) {
340 dev_dbg(&intf->dev, "bad notification endpoint\n");
341 dev->status = NULL;
344 if (rndis && !dev->status) {
345 dev_dbg(&intf->dev, "missing RNDIS status endpoint\n");
346 usb_set_intfdata(info->data, NULL);
347 usb_driver_release_interface(driver, info->data);
348 return -ENODEV;
350 return 0;
352 bad_desc:
353 dev_info(&dev->udev->dev, "bad CDC descriptors\n");
354 return -ENODEV;
356 EXPORT_SYMBOL_GPL(usbnet_generic_cdc_bind);
358 void usbnet_cdc_unbind(struct usbnet *dev, struct usb_interface *intf)
360 struct cdc_state *info = (void *) &dev->data;
361 struct usb_driver *driver = driver_of(intf);
363 /* combined interface - nothing to do */
364 if (info->data == info->control)
365 return;
367 /* disconnect master --> disconnect slave */
368 if (intf == info->control && info->data) {
369 /* ensure immediate exit from usbnet_disconnect */
370 usb_set_intfdata(info->data, NULL);
371 usb_driver_release_interface(driver, info->data);
372 info->data = NULL;
375 /* and vice versa (just in case) */
376 else if (intf == info->data && info->control) {
377 /* ensure immediate exit from usbnet_disconnect */
378 usb_set_intfdata(info->control, NULL);
379 usb_driver_release_interface(driver, info->control);
380 info->control = NULL;
383 EXPORT_SYMBOL_GPL(usbnet_cdc_unbind);
385 /*-------------------------------------------------------------------------
387 * Communications Device Class, Ethernet Control model
389 * Takes two interfaces. The DATA interface is inactive till an altsetting
390 * is selected. Configuration data includes class descriptors. There's
391 * an optional status endpoint on the control interface.
393 * This should interop with whatever the 2.4 "CDCEther.c" driver
394 * (by Brad Hards) talked with, with more functionality.
396 *-------------------------------------------------------------------------*/
398 static void dumpspeed(struct usbnet *dev, __le32 *speeds)
400 if (netif_msg_timer(dev))
401 devinfo(dev, "link speeds: %u kbps up, %u kbps down",
402 __le32_to_cpu(speeds[0]) / 1000,
403 __le32_to_cpu(speeds[1]) / 1000);
406 static void cdc_status(struct usbnet *dev, struct urb *urb)
408 struct usb_cdc_notification *event;
410 if (urb->actual_length < sizeof *event)
411 return;
413 /* SPEED_CHANGE can get split into two 8-byte packets */
414 if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
415 dumpspeed(dev, (__le32 *) urb->transfer_buffer);
416 return;
419 event = urb->transfer_buffer;
420 switch (event->bNotificationType) {
421 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
422 if (netif_msg_timer(dev))
423 devdbg(dev, "CDC: carrier %s",
424 event->wValue ? "on" : "off");
425 if (event->wValue)
426 netif_carrier_on(dev->net);
427 else
428 netif_carrier_off(dev->net);
429 break;
430 case USB_CDC_NOTIFY_SPEED_CHANGE: /* tx/rx rates */
431 if (netif_msg_timer(dev))
432 devdbg(dev, "CDC: speed change (len %d)",
433 urb->actual_length);
434 if (urb->actual_length != (sizeof *event + 8))
435 set_bit(EVENT_STS_SPLIT, &dev->flags);
436 else
437 dumpspeed(dev, (__le32 *) &event[1]);
438 break;
439 /* USB_CDC_NOTIFY_RESPONSE_AVAILABLE can happen too (e.g. RNDIS),
440 * but there are no standard formats for the response data.
442 default:
443 deverr(dev, "CDC: unexpected notification %02x!",
444 event->bNotificationType);
445 break;
449 static int cdc_bind(struct usbnet *dev, struct usb_interface *intf)
451 int status;
452 struct cdc_state *info = (void *) &dev->data;
454 status = usbnet_generic_cdc_bind(dev, intf);
455 if (status < 0)
456 return status;
458 status = usbnet_get_ethernet_addr(dev, info->ether->iMACAddress);
459 if (status < 0) {
460 usb_set_intfdata(info->data, NULL);
461 usb_driver_release_interface(driver_of(intf), info->data);
462 return status;
465 /* FIXME cdc-ether has some multicast code too, though it complains
466 * in routine cases. info->ether describes the multicast support.
467 * Implement that here, manipulating the cdc filter as needed.
469 return 0;
472 static const struct driver_info cdc_info = {
473 .description = "CDC Ethernet Device",
474 .flags = FLAG_ETHER,
475 // .check_connect = cdc_check_connect,
476 .bind = cdc_bind,
477 .unbind = usbnet_cdc_unbind,
478 .status = cdc_status,
481 /*-------------------------------------------------------------------------*/
483 #define HUAWEI_VENDOR_ID 0x12D1
484 #define NOVATEL_VENDOR_ID 0x1410
485 #define DELL_VENDOR_ID 0x413C
487 static const struct usb_device_id products [] = {
489 * BLACKLIST !!
491 * First blacklist any products that are egregiously nonconformant
492 * with the CDC Ethernet specs. Minor braindamage we cope with; when
493 * they're not even trying, needing a separate driver is only the first
494 * of the differences to show up.
497 #define ZAURUS_MASTER_INTERFACE \
498 .bInterfaceClass = USB_CLASS_COMM, \
499 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
500 .bInterfaceProtocol = USB_CDC_PROTO_NONE
502 /* SA-1100 based Sharp Zaurus ("collie"), or compatible;
503 * wire-incompatible with true CDC Ethernet implementations.
504 * (And, it seems, needlessly so...)
507 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
508 | USB_DEVICE_ID_MATCH_DEVICE,
509 .idVendor = 0x04DD,
510 .idProduct = 0x8004,
511 ZAURUS_MASTER_INTERFACE,
512 .driver_info = 0,
515 /* PXA-25x based Sharp Zaurii. Note that it seems some of these
516 * (later models especially) may have shipped only with firmware
517 * advertising false "CDC MDLM" compatibility ... but we're not
518 * clear which models did that, so for now let's assume the worst.
521 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
522 | USB_DEVICE_ID_MATCH_DEVICE,
523 .idVendor = 0x04DD,
524 .idProduct = 0x8005, /* A-300 */
525 ZAURUS_MASTER_INTERFACE,
526 .driver_info = 0,
527 }, {
528 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
529 | USB_DEVICE_ID_MATCH_DEVICE,
530 .idVendor = 0x04DD,
531 .idProduct = 0x8006, /* B-500/SL-5600 */
532 ZAURUS_MASTER_INTERFACE,
533 .driver_info = 0,
534 }, {
535 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
536 | USB_DEVICE_ID_MATCH_DEVICE,
537 .idVendor = 0x04DD,
538 .idProduct = 0x8007, /* C-700 */
539 ZAURUS_MASTER_INTERFACE,
540 .driver_info = 0,
541 }, {
542 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
543 | USB_DEVICE_ID_MATCH_DEVICE,
544 .idVendor = 0x04DD,
545 .idProduct = 0x9031, /* C-750 C-760 */
546 ZAURUS_MASTER_INTERFACE,
547 .driver_info = 0,
548 }, {
549 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
550 | USB_DEVICE_ID_MATCH_DEVICE,
551 .idVendor = 0x04DD,
552 .idProduct = 0x9032, /* SL-6000 */
553 ZAURUS_MASTER_INTERFACE,
554 .driver_info = 0,
555 }, {
556 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
557 | USB_DEVICE_ID_MATCH_DEVICE,
558 .idVendor = 0x04DD,
559 /* reported with some C860 units */
560 .idProduct = 0x9050, /* C-860 */
561 ZAURUS_MASTER_INTERFACE,
562 .driver_info = 0,
565 /* Olympus has some models with a Zaurus-compatible option.
566 * R-1000 uses a FreeScale i.MXL cpu (ARMv4T)
569 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
570 | USB_DEVICE_ID_MATCH_DEVICE,
571 .idVendor = 0x07B4,
572 .idProduct = 0x0F02, /* R-1000 */
573 ZAURUS_MASTER_INTERFACE,
574 .driver_info = 0,
577 /* Novatel USB551L and MC551 - handled by qmi_wwan */
579 USB_DEVICE_AND_INTERFACE_INFO(NOVATEL_VENDOR_ID, 0xB001, USB_CLASS_COMM,
580 USB_CDC_SUBCLASS_ETHERNET, USB_CDC_PROTO_NONE),
581 .driver_info = 0,
584 /* Novatel E362 - handled by qmi_wwan */
586 USB_DEVICE_AND_INTERFACE_INFO(NOVATEL_VENDOR_ID, 0x9010, USB_CLASS_COMM,
587 USB_CDC_SUBCLASS_ETHERNET, USB_CDC_PROTO_NONE),
588 .driver_info = 0,
591 /* Dell Wireless 5800 (Novatel E362) - handled by qmi_wwan */
593 USB_DEVICE_AND_INTERFACE_INFO(DELL_VENDOR_ID, 0x8195, USB_CLASS_COMM,
594 USB_CDC_SUBCLASS_ETHERNET, USB_CDC_PROTO_NONE),
595 .driver_info = 0,
598 /* Dell Wireless 5800 (Novatel E362) - handled by qmi_wwan */
600 USB_DEVICE_AND_INTERFACE_INFO(DELL_VENDOR_ID, 0x8196, USB_CLASS_COMM,
601 USB_CDC_SUBCLASS_ETHERNET, USB_CDC_PROTO_NONE),
602 .driver_info = 0,
606 * WHITELIST!!!
608 * CDC Ether uses two interfaces, not necessarily consecutive.
609 * We match the main interface, ignoring the optional device
610 * class so we could handle devices that aren't exclusively
611 * CDC ether.
613 * NOTE: this match must come AFTER entries blacklisting devices
614 * because of bugs/quirks in a given product (like Zaurus, above).
617 USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ETHERNET,
618 USB_CDC_PROTO_NONE),
619 .driver_info = (unsigned long) &cdc_info,
620 }, {
621 USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_MDLM,
622 USB_CDC_PROTO_NONE),
623 .driver_info = (unsigned long) &cdc_info,
624 }, {
625 /* Various Huawei modems with a network port like the UMG1831 */
626 USB_VENDOR_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID, USB_CLASS_COMM,
627 USB_CDC_SUBCLASS_ETHERNET, 255),
628 .driver_info = (unsigned long) &cdc_info,
630 { }, // END
632 MODULE_DEVICE_TABLE(usb, products);
634 static struct usb_driver cdc_driver = {
635 .name = "cdc_ether",
636 .id_table = products,
637 .probe = usbnet_probe,
638 .disconnect = usbnet_disconnect,
639 .suspend = usbnet_suspend,
640 .resume = usbnet_resume,
641 .reset_resume = usbnet_resume,
645 static int __init cdc_init(void)
647 BUILD_BUG_ON((sizeof(((struct usbnet *)0)->data)
648 < sizeof(struct cdc_state)));
650 return usb_register(&cdc_driver);
652 module_init(cdc_init);
654 static void __exit cdc_exit(void)
656 usb_deregister(&cdc_driver);
658 module_exit(cdc_exit);
660 MODULE_AUTHOR("David Brownell");
661 MODULE_DESCRIPTION("USB CDC Ethernet devices");
662 MODULE_LICENSE("GPL");