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