at v2.6.20-rc2 515 lines 15 kB view raw
1/* 2 * CDC Ethernet based networking peripherals 3 * Copyright (C) 2003-2005 by David Brownell 4 * 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. 9 * 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. 14 * 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 18 */ 19 20// #define DEBUG // error path messages, extra info 21// #define VERBOSE // more; success messages 22 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> 34 35#include "usbnet.h" 36 37 38/* 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. 43 */ 44int usbnet_generic_cdc_bind(struct usbnet *dev, struct usb_interface *intf) 45{ 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; 50 int status; 51 int rndis; 52 struct usb_driver *driver = driver_of(intf); 53 54 if (sizeof dev->data < sizeof *info) 55 return -EDOM; 56 57 /* expect strict spec conformance for the descriptors, but 58 * cope with firmware which stores them in the wrong place 59 */ 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 63 */ 64 buf = dev->udev->actconfig->extra; 65 len = dev->udev->actconfig->extralen; 66 if (len) 67 dev_dbg(&intf->dev, 68 "CDC descriptors on config\n"); 69 } 70 71 /* this assumes that if there's a non-RNDIS vendor variant 72 * of cdc-acm, it'll fail RNDIS requests cleanly. 73 */ 74 rndis = (intf->cur_altsetting->desc.bInterfaceProtocol == 0xff); 75 76 memset(info, 0, sizeof *info); 77 info->control = intf; 78 while (len > 3) { 79 if (buf [1] != USB_DT_CS_INTERFACE) 80 goto next_desc; 81 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. 88 */ 89 switch (buf [2]) { 90 case USB_CDC_HEADER_TYPE: 91 if (info->header) { 92 dev_dbg(&intf->dev, "extra CDC header\n"); 93 goto bad_desc; 94 } 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); 99 goto bad_desc; 100 } 101 break; 102 case USB_CDC_UNION_TYPE: 103 if (info->u) { 104 dev_dbg(&intf->dev, "extra CDC union\n"); 105 goto bad_desc; 106 } 107 info->u = (void *) buf; 108 if (info->u->bLength != sizeof *info->u) { 109 dev_dbg(&intf->dev, "CDC union len %u\n", 110 info->u->bLength); 111 goto bad_desc; 112 } 113 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. 117 */ 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) { 123 dev_dbg(&intf->dev, 124 "master #%u/%p slave #%u/%p\n", 125 info->u->bMasterInterface0, 126 info->control, 127 info->u->bSlaveInterface0, 128 info->data); 129 goto bad_desc; 130 } 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. 135 */ 136 if (info->data == intf) { 137 info->data = info->control; 138 info->control = intf; 139 } else 140 goto bad_desc; 141 } 142 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", 147 d->bInterfaceClass); 148 goto bad_desc; 149 } 150 break; 151 case USB_CDC_ETHERNET_TYPE: 152 if (info->ether) { 153 dev_dbg(&intf->dev, "extra CDC ether\n"); 154 goto bad_desc; 155 } 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); 160 goto bad_desc; 161 } 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. 166 */ 167 break; 168 } 169next_desc: 170 len -= buf [0]; /* bLength */ 171 buf += buf [0]; 172 } 173 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 "); 179 goto bad_desc; 180 } 181 182 /* claim data interface and set it up ... with side effects. 183 * network traffic can't flow until an altsetting is enabled. 184 */ 185 status = usb_driver_claim_interface(driver, info->data, dev); 186 if (status < 0) 187 return status; 188 status = usbnet_get_endpoints(dev, info->data); 189 if (status < 0) { 190 /* ensure immediate exit from usbnet_disconnect */ 191 usb_set_intfdata(info->data, NULL); 192 usb_driver_release_interface(driver, info->data); 193 return status; 194 } 195 196 /* status endpoint: optional for CDC Ethernet, not RNDIS (or ACM) */ 197 dev->status = NULL; 198 if (info->control->cur_altsetting->desc.bNumEndpoints == 1) { 199 struct usb_endpoint_descriptor *desc; 200 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"); 208 dev->status = NULL; 209 } 210 } 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); 215 return -ENODEV; 216 } 217 return 0; 218 219bad_desc: 220 dev_info(&dev->udev->dev, "bad CDC descriptors\n"); 221 return -ENODEV; 222} 223EXPORT_SYMBOL_GPL(usbnet_generic_cdc_bind); 224 225void usbnet_cdc_unbind(struct usbnet *dev, struct usb_interface *intf) 226{ 227 struct cdc_state *info = (void *) &dev->data; 228 struct usb_driver *driver = driver_of(intf); 229 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); 235 info->data = NULL; 236 } 237 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; 244 } 245} 246EXPORT_SYMBOL_GPL(usbnet_cdc_unbind); 247 248 249/*------------------------------------------------------------------------- 250 * 251 * Communications Device Class, Ethernet Control model 252 * 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. 256 * 257 * This should interop with whatever the 2.4 "CDCEther.c" driver 258 * (by Brad Hards) talked with, with more functionality. 259 * 260 *-------------------------------------------------------------------------*/ 261 262static void dumpspeed(struct usbnet *dev, __le32 *speeds) 263{ 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); 268} 269 270static void cdc_status(struct usbnet *dev, struct urb *urb) 271{ 272 struct usb_cdc_notification *event; 273 274 if (urb->actual_length < sizeof *event) 275 return; 276 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); 280 return; 281 } 282 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"); 289 if (event->wValue) 290 netif_carrier_on(dev->net); 291 else 292 netif_carrier_off(dev->net); 293 break; 294 case USB_CDC_NOTIFY_SPEED_CHANGE: /* tx/rx rates */ 295 if (netif_msg_timer(dev)) 296 devdbg(dev, "CDC: speed change (len %d)", 297 urb->actual_length); 298 if (urb->actual_length != (sizeof *event + 8)) 299 set_bit(EVENT_STS_SPLIT, &dev->flags); 300 else 301 dumpspeed(dev, (__le32 *) &event[1]); 302 break; 303 /* USB_CDC_NOTIFY_RESPONSE_AVAILABLE can happen too (e.g. RNDIS), 304 * but there are no standard formats for the response data. 305 */ 306 default: 307 deverr(dev, "CDC: unexpected notification %02x!", 308 event->bNotificationType); 309 break; 310 } 311} 312 313static u8 nibble(unsigned char c) 314{ 315 if (likely(isdigit(c))) 316 return c - '0'; 317 c = toupper(c); 318 if (likely(isxdigit(c))) 319 return 10 + c - 'A'; 320 return 0; 321} 322 323static inline int 324get_ethernet_addr(struct usbnet *dev, struct usb_cdc_ether_desc *e) 325{ 326 int tmp, i; 327 unsigned char buf [13]; 328 329 tmp = usb_string(dev->udev, e->iMACAddress, buf, sizeof buf); 330 if (tmp != 12) { 331 dev_dbg(&dev->udev->dev, 332 "bad MAC string %d fetch, %d\n", e->iMACAddress, tmp); 333 if (tmp >= 0) 334 tmp = -EINVAL; 335 return tmp; 336 } 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]); 340 return 0; 341} 342 343static int cdc_bind(struct usbnet *dev, struct usb_interface *intf) 344{ 345 int status; 346 struct cdc_state *info = (void *) &dev->data; 347 348 status = usbnet_generic_cdc_bind(dev, intf); 349 if (status < 0) 350 return status; 351 352 status = get_ethernet_addr(dev, info->ether); 353 if (status < 0) { 354 usb_set_intfdata(info->data, NULL); 355 usb_driver_release_interface(driver_of(intf), info->data); 356 return status; 357 } 358 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. 362 */ 363 return 0; 364} 365 366static const struct driver_info cdc_info = { 367 .description = "CDC Ethernet Device", 368 .flags = FLAG_ETHER, 369 // .check_connect = cdc_check_connect, 370 .bind = cdc_bind, 371 .unbind = usbnet_cdc_unbind, 372 .status = cdc_status, 373}; 374 375/*-------------------------------------------------------------------------*/ 376 377 378static const struct usb_device_id products [] = { 379/* 380 * BLACKLIST !! 381 * 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. 386 */ 387 388#define ZAURUS_MASTER_INTERFACE \ 389 .bInterfaceClass = USB_CLASS_COMM, \ 390 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \ 391 .bInterfaceProtocol = USB_CDC_PROTO_NONE 392 393/* SA-1100 based Sharp Zaurus ("collie"), or compatible; 394 * wire-incompatible with true CDC Ethernet implementations. 395 * (And, it seems, needlessly so...) 396 */ 397{ 398 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO 399 | USB_DEVICE_ID_MATCH_DEVICE, 400 .idVendor = 0x04DD, 401 .idProduct = 0x8004, 402 ZAURUS_MASTER_INTERFACE, 403 .driver_info = 0, 404}, 405 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. 410 */ 411{ 412 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO 413 | USB_DEVICE_ID_MATCH_DEVICE, 414 .idVendor = 0x04DD, 415 .idProduct = 0x8005, /* A-300 */ 416 ZAURUS_MASTER_INTERFACE, 417 .driver_info = 0, 418}, { 419 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO 420 | USB_DEVICE_ID_MATCH_DEVICE, 421 .idVendor = 0x04DD, 422 .idProduct = 0x8006, /* B-500/SL-5600 */ 423 ZAURUS_MASTER_INTERFACE, 424 .driver_info = 0, 425}, { 426 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO 427 | USB_DEVICE_ID_MATCH_DEVICE, 428 .idVendor = 0x04DD, 429 .idProduct = 0x8007, /* C-700 */ 430 ZAURUS_MASTER_INTERFACE, 431 .driver_info = 0, 432}, { 433 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO 434 | USB_DEVICE_ID_MATCH_DEVICE, 435 .idVendor = 0x04DD, 436 .idProduct = 0x9031, /* C-750 C-760 */ 437 ZAURUS_MASTER_INTERFACE, 438 .driver_info = 0, 439}, { 440 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO 441 | USB_DEVICE_ID_MATCH_DEVICE, 442 .idVendor = 0x04DD, 443 .idProduct = 0x9032, /* SL-6000 */ 444 ZAURUS_MASTER_INTERFACE, 445 .driver_info = 0, 446}, { 447 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO 448 | USB_DEVICE_ID_MATCH_DEVICE, 449 .idVendor = 0x04DD, 450 /* reported with some C860 units */ 451 .idProduct = 0x9050, /* C-860 */ 452 ZAURUS_MASTER_INTERFACE, 453 .driver_info = 0, 454}, 455 456/* Olympus has some models with a Zaurus-compatible option. 457 * R-1000 uses a FreeScale i.MXL cpu (ARMv4T) 458 */ 459{ 460 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO 461 | USB_DEVICE_ID_MATCH_DEVICE, 462 .idVendor = 0x07B4, 463 .idProduct = 0x0F02, /* R-1000 */ 464 ZAURUS_MASTER_INTERFACE, 465 .driver_info = 0, 466}, 467 468/* 469 * WHITELIST!!! 470 * 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 474 * CDC ether. 475 * 476 * NOTE: this match must come AFTER entries blacklisting devices 477 * because of bugs/quirks in a given product (like Zaurus, above). 478 */ 479{ 480 USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ETHERNET, 481 USB_CDC_PROTO_NONE), 482 .driver_info = (unsigned long) &cdc_info, 483}, 484 { }, // END 485}; 486MODULE_DEVICE_TABLE(usb, products); 487 488static struct usb_driver cdc_driver = { 489 .name = "cdc_ether", 490 .id_table = products, 491 .probe = usbnet_probe, 492 .disconnect = usbnet_disconnect, 493 .suspend = usbnet_suspend, 494 .resume = usbnet_resume, 495}; 496 497 498static int __init cdc_init(void) 499{ 500 BUILD_BUG_ON((sizeof(((struct usbnet *)0)->data) 501 < sizeof(struct cdc_state))); 502 503 return usb_register(&cdc_driver); 504} 505module_init(cdc_init); 506 507static void __exit cdc_exit(void) 508{ 509 usb_deregister(&cdc_driver); 510} 511module_exit(cdc_exit); 512 513MODULE_AUTHOR("David Brownell"); 514MODULE_DESCRIPTION("USB CDC Ethernet devices"); 515MODULE_LICENSE("GPL");