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) 5 * 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. 10 * 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. 15 * 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 19 */ 20 21// #define DEBUG // error path messages, extra info 22// #define VERBOSE // more; success messages 23 24#include <linux/module.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> 34 35#include "usbnet.h" 36 37 38#if defined(CONFIG_USB_NET_RNDIS_HOST) || defined(CONFIG_USB_NET_RNDIS_HOST_MODULE) 39 40static int is_rndis(struct usb_interface_descriptor *desc) 41{ 42 return desc->bInterfaceClass == USB_CLASS_COMM 43 && desc->bInterfaceSubClass == 2 44 && desc->bInterfaceProtocol == 0xff; 45} 46 47static int is_activesync(struct usb_interface_descriptor *desc) 48{ 49 return desc->bInterfaceClass == USB_CLASS_MISC 50 && desc->bInterfaceSubClass == 1 51 && desc->bInterfaceProtocol == 1; 52} 53 54#else 55 56#define is_rndis(desc) 0 57#define is_activesync(desc) 0 58 59#endif 60 61/* 62 * probes control interface, claims data interface, collects the bulk 63 * endpoints, activates data interface (if needed), maybe sets MTU. 64 * all pure cdc, except for certain firmware workarounds, and knowing 65 * that rndis uses one different rule. 66 */ 67int usbnet_generic_cdc_bind(struct usbnet *dev, struct usb_interface *intf) 68{ 69 u8 *buf = intf->cur_altsetting->extra; 70 int len = intf->cur_altsetting->extralen; 71 struct usb_interface_descriptor *d; 72 struct cdc_state *info = (void *) &dev->data; 73 int status; 74 int rndis; 75 struct usb_driver *driver = driver_of(intf); 76 77 if (sizeof dev->data < sizeof *info) 78 return -EDOM; 79 80 /* expect strict spec conformance for the descriptors, but 81 * cope with firmware which stores them in the wrong place 82 */ 83 if (len == 0 && dev->udev->actconfig->extralen) { 84 /* Motorola SB4100 (and others: Brad Hards says it's 85 * from a Broadcom design) put CDC descriptors here 86 */ 87 buf = dev->udev->actconfig->extra; 88 len = dev->udev->actconfig->extralen; 89 if (len) 90 dev_dbg(&intf->dev, 91 "CDC descriptors on config\n"); 92 } 93 94 /* Maybe CDC descriptors are after the endpoint? This bug has 95 * been seen on some 2Wire Inc RNDIS-ish products. 96 */ 97 if (len == 0) { 98 struct usb_host_endpoint *hep; 99 100 hep = intf->cur_altsetting->endpoint; 101 if (hep) { 102 buf = hep->extra; 103 len = hep->extralen; 104 } 105 if (len) 106 dev_dbg(&intf->dev, 107 "CDC descriptors on endpoint\n"); 108 } 109 110 /* this assumes that if there's a non-RNDIS vendor variant 111 * of cdc-acm, it'll fail RNDIS requests cleanly. 112 */ 113 rndis = is_rndis(&intf->cur_altsetting->desc) 114 || is_activesync(&intf->cur_altsetting->desc); 115 116 memset(info, 0, sizeof *info); 117 info->control = intf; 118 while (len > 3) { 119 if (buf [1] != USB_DT_CS_INTERFACE) 120 goto next_desc; 121 122 /* use bDescriptorSubType to identify the CDC descriptors. 123 * We expect devices with CDC header and union descriptors. 124 * For CDC Ethernet we need the ethernet descriptor. 125 * For RNDIS, ignore two (pointless) CDC modem descriptors 126 * in favor of a complicated OID-based RPC scheme doing what 127 * CDC Ethernet achieves with a simple descriptor. 128 */ 129 switch (buf [2]) { 130 case USB_CDC_HEADER_TYPE: 131 if (info->header) { 132 dev_dbg(&intf->dev, "extra CDC header\n"); 133 goto bad_desc; 134 } 135 info->header = (void *) buf; 136 if (info->header->bLength != sizeof *info->header) { 137 dev_dbg(&intf->dev, "CDC header len %u\n", 138 info->header->bLength); 139 goto bad_desc; 140 } 141 break; 142 case USB_CDC_ACM_TYPE: 143 /* paranoia: disambiguate a "real" vendor-specific 144 * modem interface from an RNDIS non-modem. 145 */ 146 if (rndis) { 147 struct usb_cdc_acm_descriptor *acm; 148 149 acm = (void *) buf; 150 if (acm->bmCapabilities) { 151 dev_dbg(&intf->dev, 152 "ACM capabilities %02x, " 153 "not really RNDIS?\n", 154 acm->bmCapabilities); 155 goto bad_desc; 156 } 157 } 158 break; 159 case USB_CDC_UNION_TYPE: 160 if (info->u) { 161 dev_dbg(&intf->dev, "extra CDC union\n"); 162 goto bad_desc; 163 } 164 info->u = (void *) buf; 165 if (info->u->bLength != sizeof *info->u) { 166 dev_dbg(&intf->dev, "CDC union len %u\n", 167 info->u->bLength); 168 goto bad_desc; 169 } 170 171 /* we need a master/control interface (what we're 172 * probed with) and a slave/data interface; union 173 * descriptors sort this all out. 174 */ 175 info->control = usb_ifnum_to_if(dev->udev, 176 info->u->bMasterInterface0); 177 info->data = usb_ifnum_to_if(dev->udev, 178 info->u->bSlaveInterface0); 179 if (!info->control || !info->data) { 180 dev_dbg(&intf->dev, 181 "master #%u/%p slave #%u/%p\n", 182 info->u->bMasterInterface0, 183 info->control, 184 info->u->bSlaveInterface0, 185 info->data); 186 goto bad_desc; 187 } 188 if (info->control != intf) { 189 dev_dbg(&intf->dev, "bogus CDC Union\n"); 190 /* Ambit USB Cable Modem (and maybe others) 191 * interchanges master and slave interface. 192 */ 193 if (info->data == intf) { 194 info->data = info->control; 195 info->control = intf; 196 } else 197 goto bad_desc; 198 } 199 200 /* a data interface altsetting does the real i/o */ 201 d = &info->data->cur_altsetting->desc; 202 if (d->bInterfaceClass != USB_CLASS_CDC_DATA) { 203 dev_dbg(&intf->dev, "slave class %u\n", 204 d->bInterfaceClass); 205 goto bad_desc; 206 } 207 break; 208 case USB_CDC_ETHERNET_TYPE: 209 if (info->ether) { 210 dev_dbg(&intf->dev, "extra CDC ether\n"); 211 goto bad_desc; 212 } 213 info->ether = (void *) buf; 214 if (info->ether->bLength != sizeof *info->ether) { 215 dev_dbg(&intf->dev, "CDC ether len %u\n", 216 info->ether->bLength); 217 goto bad_desc; 218 } 219 dev->hard_mtu = le16_to_cpu( 220 info->ether->wMaxSegmentSize); 221 /* because of Zaurus, we may be ignoring the host 222 * side link address we were given. 223 */ 224 break; 225 } 226next_desc: 227 len -= buf [0]; /* bLength */ 228 buf += buf [0]; 229 } 230 231 /* Microsoft ActiveSync based RNDIS devices lack the CDC descriptors, 232 * so we'll hard-wire the interfaces and not check for descriptors. 233 */ 234 if (is_activesync(&intf->cur_altsetting->desc) && !info->u) { 235 info->control = usb_ifnum_to_if(dev->udev, 0); 236 info->data = usb_ifnum_to_if(dev->udev, 1); 237 if (!info->control || !info->data) { 238 dev_dbg(&intf->dev, 239 "activesync: master #0/%p slave #1/%p\n", 240 info->control, 241 info->data); 242 goto bad_desc; 243 } 244 245 } else if (!info->header || !info->u || (!rndis && !info->ether)) { 246 dev_dbg(&intf->dev, "missing cdc %s%s%sdescriptor\n", 247 info->header ? "" : "header ", 248 info->u ? "" : "union ", 249 info->ether ? "" : "ether "); 250 goto bad_desc; 251 } 252 253 /* claim data interface and set it up ... with side effects. 254 * network traffic can't flow until an altsetting is enabled. 255 */ 256 status = usb_driver_claim_interface(driver, info->data, dev); 257 if (status < 0) 258 return status; 259 status = usbnet_get_endpoints(dev, info->data); 260 if (status < 0) { 261 /* ensure immediate exit from usbnet_disconnect */ 262 usb_set_intfdata(info->data, NULL); 263 usb_driver_release_interface(driver, info->data); 264 return status; 265 } 266 267 /* status endpoint: optional for CDC Ethernet, not RNDIS (or ACM) */ 268 dev->status = NULL; 269 if (info->control->cur_altsetting->desc.bNumEndpoints == 1) { 270 struct usb_endpoint_descriptor *desc; 271 272 dev->status = &info->control->cur_altsetting->endpoint [0]; 273 desc = &dev->status->desc; 274 if (!usb_endpoint_is_int_in(desc) 275 || (le16_to_cpu(desc->wMaxPacketSize) 276 < sizeof(struct usb_cdc_notification)) 277 || !desc->bInterval) { 278 dev_dbg(&intf->dev, "bad notification endpoint\n"); 279 dev->status = NULL; 280 } 281 } 282 if (rndis && !dev->status) { 283 dev_dbg(&intf->dev, "missing RNDIS status endpoint\n"); 284 usb_set_intfdata(info->data, NULL); 285 usb_driver_release_interface(driver, info->data); 286 return -ENODEV; 287 } 288 return 0; 289 290bad_desc: 291 dev_info(&dev->udev->dev, "bad CDC descriptors\n"); 292 return -ENODEV; 293} 294EXPORT_SYMBOL_GPL(usbnet_generic_cdc_bind); 295 296void usbnet_cdc_unbind(struct usbnet *dev, struct usb_interface *intf) 297{ 298 struct cdc_state *info = (void *) &dev->data; 299 struct usb_driver *driver = driver_of(intf); 300 301 /* disconnect master --> disconnect slave */ 302 if (intf == info->control && info->data) { 303 /* ensure immediate exit from usbnet_disconnect */ 304 usb_set_intfdata(info->data, NULL); 305 usb_driver_release_interface(driver, info->data); 306 info->data = NULL; 307 } 308 309 /* and vice versa (just in case) */ 310 else if (intf == info->data && info->control) { 311 /* ensure immediate exit from usbnet_disconnect */ 312 usb_set_intfdata(info->control, NULL); 313 usb_driver_release_interface(driver, info->control); 314 info->control = NULL; 315 } 316} 317EXPORT_SYMBOL_GPL(usbnet_cdc_unbind); 318 319 320/*------------------------------------------------------------------------- 321 * 322 * Communications Device Class, Ethernet Control model 323 * 324 * Takes two interfaces. The DATA interface is inactive till an altsetting 325 * is selected. Configuration data includes class descriptors. There's 326 * an optional status endpoint on the control interface. 327 * 328 * This should interop with whatever the 2.4 "CDCEther.c" driver 329 * (by Brad Hards) talked with, with more functionality. 330 * 331 *-------------------------------------------------------------------------*/ 332 333static void dumpspeed(struct usbnet *dev, __le32 *speeds) 334{ 335 if (netif_msg_timer(dev)) 336 devinfo(dev, "link speeds: %u kbps up, %u kbps down", 337 __le32_to_cpu(speeds[0]) / 1000, 338 __le32_to_cpu(speeds[1]) / 1000); 339} 340 341static void cdc_status(struct usbnet *dev, struct urb *urb) 342{ 343 struct usb_cdc_notification *event; 344 345 if (urb->actual_length < sizeof *event) 346 return; 347 348 /* SPEED_CHANGE can get split into two 8-byte packets */ 349 if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) { 350 dumpspeed(dev, (__le32 *) urb->transfer_buffer); 351 return; 352 } 353 354 event = urb->transfer_buffer; 355 switch (event->bNotificationType) { 356 case USB_CDC_NOTIFY_NETWORK_CONNECTION: 357 if (netif_msg_timer(dev)) 358 devdbg(dev, "CDC: carrier %s", 359 event->wValue ? "on" : "off"); 360 if (event->wValue) 361 netif_carrier_on(dev->net); 362 else 363 netif_carrier_off(dev->net); 364 break; 365 case USB_CDC_NOTIFY_SPEED_CHANGE: /* tx/rx rates */ 366 if (netif_msg_timer(dev)) 367 devdbg(dev, "CDC: speed change (len %d)", 368 urb->actual_length); 369 if (urb->actual_length != (sizeof *event + 8)) 370 set_bit(EVENT_STS_SPLIT, &dev->flags); 371 else 372 dumpspeed(dev, (__le32 *) &event[1]); 373 break; 374 /* USB_CDC_NOTIFY_RESPONSE_AVAILABLE can happen too (e.g. RNDIS), 375 * but there are no standard formats for the response data. 376 */ 377 default: 378 deverr(dev, "CDC: unexpected notification %02x!", 379 event->bNotificationType); 380 break; 381 } 382} 383 384static u8 nibble(unsigned char c) 385{ 386 if (likely(isdigit(c))) 387 return c - '0'; 388 c = toupper(c); 389 if (likely(isxdigit(c))) 390 return 10 + c - 'A'; 391 return 0; 392} 393 394static inline int 395get_ethernet_addr(struct usbnet *dev, struct usb_cdc_ether_desc *e) 396{ 397 int tmp, i; 398 unsigned char buf [13]; 399 400 tmp = usb_string(dev->udev, e->iMACAddress, buf, sizeof buf); 401 if (tmp != 12) { 402 dev_dbg(&dev->udev->dev, 403 "bad MAC string %d fetch, %d\n", e->iMACAddress, tmp); 404 if (tmp >= 0) 405 tmp = -EINVAL; 406 return tmp; 407 } 408 for (i = tmp = 0; i < 6; i++, tmp += 2) 409 dev->net->dev_addr [i] = 410 (nibble(buf [tmp]) << 4) + nibble(buf [tmp + 1]); 411 return 0; 412} 413 414static int cdc_bind(struct usbnet *dev, struct usb_interface *intf) 415{ 416 int status; 417 struct cdc_state *info = (void *) &dev->data; 418 419 status = usbnet_generic_cdc_bind(dev, intf); 420 if (status < 0) 421 return status; 422 423 status = get_ethernet_addr(dev, info->ether); 424 if (status < 0) { 425 usb_set_intfdata(info->data, NULL); 426 usb_driver_release_interface(driver_of(intf), info->data); 427 return status; 428 } 429 430 return 0; 431} 432 433static const struct driver_info cdc_info = { 434 .description = "CDC Ethernet Device", 435 .flags = FLAG_ETHER, 436 // .check_connect = cdc_check_connect, 437 .bind = cdc_bind, 438 .unbind = usbnet_cdc_unbind, 439 .status = cdc_status, 440}; 441 442/*-------------------------------------------------------------------------*/ 443 444 445static const struct usb_device_id products [] = { 446/* 447 * BLACKLIST !! 448 * 449 * First blacklist any products that are egregiously nonconformant 450 * with the CDC Ethernet specs. Minor braindamage we cope with; when 451 * they're not even trying, needing a separate driver is only the first 452 * of the differences to show up. 453 */ 454 455#define ZAURUS_MASTER_INTERFACE \ 456 .bInterfaceClass = USB_CLASS_COMM, \ 457 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \ 458 .bInterfaceProtocol = USB_CDC_PROTO_NONE 459 460/* SA-1100 based Sharp Zaurus ("collie"), or compatible; 461 * wire-incompatible with true CDC Ethernet implementations. 462 * (And, it seems, needlessly so...) 463 */ 464{ 465 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO 466 | USB_DEVICE_ID_MATCH_DEVICE, 467 .idVendor = 0x04DD, 468 .idProduct = 0x8004, 469 ZAURUS_MASTER_INTERFACE, 470 .driver_info = 0, 471}, 472 473/* PXA-25x based Sharp Zaurii. Note that it seems some of these 474 * (later models especially) may have shipped only with firmware 475 * advertising false "CDC MDLM" compatibility ... but we're not 476 * clear which models did that, so for now let's assume the worst. 477 */ 478{ 479 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO 480 | USB_DEVICE_ID_MATCH_DEVICE, 481 .idVendor = 0x04DD, 482 .idProduct = 0x8005, /* A-300 */ 483 ZAURUS_MASTER_INTERFACE, 484 .driver_info = 0, 485}, { 486 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO 487 | USB_DEVICE_ID_MATCH_DEVICE, 488 .idVendor = 0x04DD, 489 .idProduct = 0x8006, /* B-500/SL-5600 */ 490 ZAURUS_MASTER_INTERFACE, 491 .driver_info = 0, 492}, { 493 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO 494 | USB_DEVICE_ID_MATCH_DEVICE, 495 .idVendor = 0x04DD, 496 .idProduct = 0x8007, /* C-700 */ 497 ZAURUS_MASTER_INTERFACE, 498 .driver_info = 0, 499}, { 500 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO 501 | USB_DEVICE_ID_MATCH_DEVICE, 502 .idVendor = 0x04DD, 503 .idProduct = 0x9031, /* C-750 C-760 */ 504 ZAURUS_MASTER_INTERFACE, 505 .driver_info = 0, 506}, { 507 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO 508 | USB_DEVICE_ID_MATCH_DEVICE, 509 .idVendor = 0x04DD, 510 .idProduct = 0x9032, /* SL-6000 */ 511 ZAURUS_MASTER_INTERFACE, 512 .driver_info = 0, 513}, { 514 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO 515 | USB_DEVICE_ID_MATCH_DEVICE, 516 .idVendor = 0x04DD, 517 /* reported with some C860 units */ 518 .idProduct = 0x9050, /* C-860 */ 519 ZAURUS_MASTER_INTERFACE, 520 .driver_info = 0, 521}, 522 523/* Olympus has some models with a Zaurus-compatible option. 524 * R-1000 uses a FreeScale i.MXL cpu (ARMv4T) 525 */ 526{ 527 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO 528 | USB_DEVICE_ID_MATCH_DEVICE, 529 .idVendor = 0x07B4, 530 .idProduct = 0x0F02, /* R-1000 */ 531 ZAURUS_MASTER_INTERFACE, 532 .driver_info = 0, 533}, 534 535/* 536 * WHITELIST!!! 537 * 538 * CDC Ether uses two interfaces, not necessarily consecutive. 539 * We match the main interface, ignoring the optional device 540 * class so we could handle devices that aren't exclusively 541 * CDC ether. 542 * 543 * NOTE: this match must come AFTER entries blacklisting devices 544 * because of bugs/quirks in a given product (like Zaurus, above). 545 */ 546{ 547 USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ETHERNET, 548 USB_CDC_PROTO_NONE), 549 .driver_info = (unsigned long) &cdc_info, 550}, 551 { }, // END 552}; 553MODULE_DEVICE_TABLE(usb, products); 554 555static struct usb_driver cdc_driver = { 556 .name = "cdc_ether", 557 .id_table = products, 558 .probe = usbnet_probe, 559 .disconnect = usbnet_disconnect, 560 .suspend = usbnet_suspend, 561 .resume = usbnet_resume, 562}; 563 564 565static int __init cdc_init(void) 566{ 567 BUILD_BUG_ON((sizeof(((struct usbnet *)0)->data) 568 < sizeof(struct cdc_state))); 569 570 return usb_register(&cdc_driver); 571} 572module_init(cdc_init); 573 574static void __exit cdc_exit(void) 575{ 576 usb_deregister(&cdc_driver); 577} 578module_exit(cdc_exit); 579 580MODULE_AUTHOR("David Brownell"); 581MODULE_DESCRIPTION("USB CDC Ethernet devices"); 582MODULE_LICENSE("GPL"); 583