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