[PATCH] USB: remove .owner field from struct usb_driver
[linux-2.6/btrfs-unstable.git] / drivers / usb / net / cdc_ether.c
blob63f1f3ba8e0b3576f2cee0918f369c433f9d8dc6
1 /*
2 * CDC Ethernet based networking peripherals
3 * Copyright (C) 2003-2005 by David Brownell
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.
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.
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
20 // #define DEBUG // error path messages, extra info
21 // #define VERBOSE // more; success messages
23 #include <linux/config.h>
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"
40 * probes control interface, claims data interface, collects the bulk
41 * endpoints, activates data interface (if needed), maybe sets MTU.
42 * all pure cdc, except for certain firmware workarounds, and knowing
43 * that rndis uses one different rule.
45 int usbnet_generic_cdc_bind(struct usbnet *dev, struct usb_interface *intf)
47 u8 *buf = intf->cur_altsetting->extra;
48 int len = intf->cur_altsetting->extralen;
49 struct usb_interface_descriptor *d;
50 struct cdc_state *info = (void *) &dev->data;
51 int status;
52 int rndis;
53 struct usb_driver *driver = driver_of(intf);
55 if (sizeof dev->data < sizeof *info)
56 return -EDOM;
58 /* expect strict spec conformance for the descriptors, but
59 * cope with firmware which stores them in the wrong place
61 if (len == 0 && dev->udev->actconfig->extralen) {
62 /* Motorola SB4100 (and others: Brad Hards says it's
63 * from a Broadcom design) put CDC descriptors here
65 buf = dev->udev->actconfig->extra;
66 len = dev->udev->actconfig->extralen;
67 if (len)
68 dev_dbg(&intf->dev,
69 "CDC descriptors on config\n");
72 /* this assumes that if there's a non-RNDIS vendor variant
73 * of cdc-acm, it'll fail RNDIS requests cleanly.
75 rndis = (intf->cur_altsetting->desc.bInterfaceProtocol == 0xff);
77 memset(info, 0, sizeof *info);
78 info->control = intf;
79 while (len > 3) {
80 if (buf [1] != USB_DT_CS_INTERFACE)
81 goto next_desc;
83 /* use bDescriptorSubType to identify the CDC descriptors.
84 * We expect devices with CDC header and union descriptors.
85 * For CDC Ethernet we need the ethernet descriptor.
86 * For RNDIS, ignore two (pointless) CDC modem descriptors
87 * in favor of a complicated OID-based RPC scheme doing what
88 * CDC Ethernet achieves with a simple descriptor.
90 switch (buf [2]) {
91 case USB_CDC_HEADER_TYPE:
92 if (info->header) {
93 dev_dbg(&intf->dev, "extra CDC header\n");
94 goto bad_desc;
96 info->header = (void *) buf;
97 if (info->header->bLength != sizeof *info->header) {
98 dev_dbg(&intf->dev, "CDC header len %u\n",
99 info->header->bLength);
100 goto bad_desc;
102 break;
103 case USB_CDC_UNION_TYPE:
104 if (info->u) {
105 dev_dbg(&intf->dev, "extra CDC union\n");
106 goto bad_desc;
108 info->u = (void *) buf;
109 if (info->u->bLength != sizeof *info->u) {
110 dev_dbg(&intf->dev, "CDC union len %u\n",
111 info->u->bLength);
112 goto bad_desc;
115 /* we need a master/control interface (what we're
116 * probed with) and a slave/data interface; union
117 * descriptors sort this all out.
119 info->control = usb_ifnum_to_if(dev->udev,
120 info->u->bMasterInterface0);
121 info->data = usb_ifnum_to_if(dev->udev,
122 info->u->bSlaveInterface0);
123 if (!info->control || !info->data) {
124 dev_dbg(&intf->dev,
125 "master #%u/%p slave #%u/%p\n",
126 info->u->bMasterInterface0,
127 info->control,
128 info->u->bSlaveInterface0,
129 info->data);
130 goto bad_desc;
132 if (info->control != intf) {
133 dev_dbg(&intf->dev, "bogus CDC Union\n");
134 /* Ambit USB Cable Modem (and maybe others)
135 * interchanges master and slave interface.
137 if (info->data == intf) {
138 info->data = info->control;
139 info->control = intf;
140 } else
141 goto bad_desc;
144 /* a data interface altsetting does the real i/o */
145 d = &info->data->cur_altsetting->desc;
146 if (d->bInterfaceClass != USB_CLASS_CDC_DATA) {
147 dev_dbg(&intf->dev, "slave class %u\n",
148 d->bInterfaceClass);
149 goto bad_desc;
151 break;
152 case USB_CDC_ETHERNET_TYPE:
153 if (info->ether) {
154 dev_dbg(&intf->dev, "extra CDC ether\n");
155 goto bad_desc;
157 info->ether = (void *) buf;
158 if (info->ether->bLength != sizeof *info->ether) {
159 dev_dbg(&intf->dev, "CDC ether len %u\n",
160 info->ether->bLength);
161 goto bad_desc;
163 dev->hard_mtu = le16_to_cpu(
164 info->ether->wMaxSegmentSize);
165 /* because of Zaurus, we may be ignoring the host
166 * side link address we were given.
168 break;
170 next_desc:
171 len -= buf [0]; /* bLength */
172 buf += buf [0];
175 if (!info->header || !info->u || (!rndis && !info->ether)) {
176 dev_dbg(&intf->dev, "missing cdc %s%s%sdescriptor\n",
177 info->header ? "" : "header ",
178 info->u ? "" : "union ",
179 info->ether ? "" : "ether ");
180 goto bad_desc;
183 /* claim data interface and set it up ... with side effects.
184 * network traffic can't flow until an altsetting is enabled.
186 status = usb_driver_claim_interface(driver, info->data, dev);
187 if (status < 0)
188 return status;
189 status = usbnet_get_endpoints(dev, info->data);
190 if (status < 0) {
191 /* ensure immediate exit from usbnet_disconnect */
192 usb_set_intfdata(info->data, NULL);
193 usb_driver_release_interface(driver, info->data);
194 return status;
197 /* status endpoint: optional for CDC Ethernet, not RNDIS (or ACM) */
198 dev->status = NULL;
199 if (info->control->cur_altsetting->desc.bNumEndpoints == 1) {
200 struct usb_endpoint_descriptor *desc;
202 dev->status = &info->control->cur_altsetting->endpoint [0];
203 desc = &dev->status->desc;
204 if (desc->bmAttributes != USB_ENDPOINT_XFER_INT
205 || !(desc->bEndpointAddress & USB_DIR_IN)
206 || (le16_to_cpu(desc->wMaxPacketSize)
207 < sizeof(struct usb_cdc_notification))
208 || !desc->bInterval) {
209 dev_dbg(&intf->dev, "bad notification endpoint\n");
210 dev->status = NULL;
213 if (rndis && !dev->status) {
214 dev_dbg(&intf->dev, "missing RNDIS status endpoint\n");
215 usb_set_intfdata(info->data, NULL);
216 usb_driver_release_interface(driver, info->data);
217 return -ENODEV;
219 return 0;
221 bad_desc:
222 dev_info(&dev->udev->dev, "bad CDC descriptors\n");
223 return -ENODEV;
225 EXPORT_SYMBOL_GPL(usbnet_generic_cdc_bind);
227 void usbnet_cdc_unbind(struct usbnet *dev, struct usb_interface *intf)
229 struct cdc_state *info = (void *) &dev->data;
230 struct usb_driver *driver = driver_of(intf);
232 /* disconnect master --> disconnect slave */
233 if (intf == info->control && info->data) {
234 /* ensure immediate exit from usbnet_disconnect */
235 usb_set_intfdata(info->data, NULL);
236 usb_driver_release_interface(driver, info->data);
237 info->data = NULL;
240 /* and vice versa (just in case) */
241 else if (intf == info->data && info->control) {
242 /* ensure immediate exit from usbnet_disconnect */
243 usb_set_intfdata(info->control, NULL);
244 usb_driver_release_interface(driver, info->control);
245 info->control = NULL;
248 EXPORT_SYMBOL_GPL(usbnet_cdc_unbind);
251 /*-------------------------------------------------------------------------
253 * Communications Device Class, Ethernet Control model
255 * Takes two interfaces. The DATA interface is inactive till an altsetting
256 * is selected. Configuration data includes class descriptors. There's
257 * an optional status endpoint on the control interface.
259 * This should interop with whatever the 2.4 "CDCEther.c" driver
260 * (by Brad Hards) talked with, with more functionality.
262 *-------------------------------------------------------------------------*/
264 static void dumpspeed(struct usbnet *dev, __le32 *speeds)
266 if (netif_msg_timer(dev))
267 devinfo(dev, "link speeds: %u kbps up, %u kbps down",
268 __le32_to_cpu(speeds[0]) / 1000,
269 __le32_to_cpu(speeds[1]) / 1000);
272 static void cdc_status(struct usbnet *dev, struct urb *urb)
274 struct usb_cdc_notification *event;
276 if (urb->actual_length < sizeof *event)
277 return;
279 /* SPEED_CHANGE can get split into two 8-byte packets */
280 if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
281 dumpspeed(dev, (__le32 *) urb->transfer_buffer);
282 return;
285 event = urb->transfer_buffer;
286 switch (event->bNotificationType) {
287 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
288 if (netif_msg_timer(dev))
289 devdbg(dev, "CDC: carrier %s",
290 event->wValue ? "on" : "off");
291 if (event->wValue)
292 netif_carrier_on(dev->net);
293 else
294 netif_carrier_off(dev->net);
295 break;
296 case USB_CDC_NOTIFY_SPEED_CHANGE: /* tx/rx rates */
297 if (netif_msg_timer(dev))
298 devdbg(dev, "CDC: speed change (len %d)",
299 urb->actual_length);
300 if (urb->actual_length != (sizeof *event + 8))
301 set_bit(EVENT_STS_SPLIT, &dev->flags);
302 else
303 dumpspeed(dev, (__le32 *) &event[1]);
304 break;
305 /* USB_CDC_NOTIFY_RESPONSE_AVAILABLE can happen too (e.g. RNDIS),
306 * but there are no standard formats for the response data.
308 default:
309 deverr(dev, "CDC: unexpected notification %02x!",
310 event->bNotificationType);
311 break;
315 static u8 nibble(unsigned char c)
317 if (likely(isdigit(c)))
318 return c - '0';
319 c = toupper(c);
320 if (likely(isxdigit(c)))
321 return 10 + c - 'A';
322 return 0;
325 static inline int
326 get_ethernet_addr(struct usbnet *dev, struct usb_cdc_ether_desc *e)
328 int tmp, i;
329 unsigned char buf [13];
331 tmp = usb_string(dev->udev, e->iMACAddress, buf, sizeof buf);
332 if (tmp != 12) {
333 dev_dbg(&dev->udev->dev,
334 "bad MAC string %d fetch, %d\n", e->iMACAddress, tmp);
335 if (tmp >= 0)
336 tmp = -EINVAL;
337 return tmp;
339 for (i = tmp = 0; i < 6; i++, tmp += 2)
340 dev->net->dev_addr [i] =
341 (nibble(buf [tmp]) << 4) + nibble(buf [tmp + 1]);
342 return 0;
345 static int cdc_bind(struct usbnet *dev, struct usb_interface *intf)
347 int status;
348 struct cdc_state *info = (void *) &dev->data;
350 status = usbnet_generic_cdc_bind(dev, intf);
351 if (status < 0)
352 return status;
354 status = get_ethernet_addr(dev, info->ether);
355 if (status < 0) {
356 usb_set_intfdata(info->data, NULL);
357 usb_driver_release_interface(driver_of(intf), info->data);
358 return status;
361 /* FIXME cdc-ether has some multicast code too, though it complains
362 * in routine cases. info->ether describes the multicast support.
363 * Implement that here, manipulating the cdc filter as needed.
365 return 0;
368 static const struct driver_info cdc_info = {
369 .description = "CDC Ethernet Device",
370 .flags = FLAG_ETHER,
371 // .check_connect = cdc_check_connect,
372 .bind = cdc_bind,
373 .unbind = usbnet_cdc_unbind,
374 .status = cdc_status,
377 /*-------------------------------------------------------------------------*/
380 static const struct usb_device_id products [] = {
382 * BLACKLIST !!
384 * First blacklist any products that are egregiously nonconformant
385 * with the CDC Ethernet specs. Minor braindamage we cope with; when
386 * they're not even trying, needing a separate driver is only the first
387 * of the differences to show up.
390 #define ZAURUS_MASTER_INTERFACE \
391 .bInterfaceClass = USB_CLASS_COMM, \
392 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
393 .bInterfaceProtocol = USB_CDC_PROTO_NONE
395 /* SA-1100 based Sharp Zaurus ("collie"), or compatible;
396 * wire-incompatible with true CDC Ethernet implementations.
397 * (And, it seems, needlessly so...)
400 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
401 | USB_DEVICE_ID_MATCH_DEVICE,
402 .idVendor = 0x04DD,
403 .idProduct = 0x8004,
404 ZAURUS_MASTER_INTERFACE,
405 .driver_info = 0,
408 /* PXA-25x based Sharp Zaurii. Note that it seems some of these
409 * (later models especially) may have shipped only with firmware
410 * advertising false "CDC MDLM" compatibility ... but we're not
411 * clear which models did that, so for now let's assume the worst.
414 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
415 | USB_DEVICE_ID_MATCH_DEVICE,
416 .idVendor = 0x04DD,
417 .idProduct = 0x8005, /* A-300 */
418 ZAURUS_MASTER_INTERFACE,
419 .driver_info = 0,
420 }, {
421 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
422 | USB_DEVICE_ID_MATCH_DEVICE,
423 .idVendor = 0x04DD,
424 .idProduct = 0x8006, /* B-500/SL-5600 */
425 ZAURUS_MASTER_INTERFACE,
426 .driver_info = 0,
427 }, {
428 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
429 | USB_DEVICE_ID_MATCH_DEVICE,
430 .idVendor = 0x04DD,
431 .idProduct = 0x8007, /* C-700 */
432 ZAURUS_MASTER_INTERFACE,
433 .driver_info = 0,
434 }, {
435 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
436 | USB_DEVICE_ID_MATCH_DEVICE,
437 .idVendor = 0x04DD,
438 .idProduct = 0x9031, /* C-750 C-760 */
439 ZAURUS_MASTER_INTERFACE,
440 .driver_info = 0,
441 }, {
442 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
443 | USB_DEVICE_ID_MATCH_DEVICE,
444 .idVendor = 0x04DD,
445 .idProduct = 0x9032, /* SL-6000 */
446 ZAURUS_MASTER_INTERFACE,
447 .driver_info = 0,
448 }, {
449 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
450 | USB_DEVICE_ID_MATCH_DEVICE,
451 .idVendor = 0x04DD,
452 /* reported with some C860 units */
453 .idProduct = 0x9050, /* C-860 */
454 ZAURUS_MASTER_INTERFACE,
455 .driver_info = 0,
459 * WHITELIST!!!
461 * CDC Ether uses two interfaces, not necessarily consecutive.
462 * We match the main interface, ignoring the optional device
463 * class so we could handle devices that aren't exclusively
464 * CDC ether.
466 * NOTE: this match must come AFTER entries blacklisting devices
467 * because of bugs/quirks in a given product (like Zaurus, above).
470 USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ETHERNET,
471 USB_CDC_PROTO_NONE),
472 .driver_info = (unsigned long) &cdc_info,
474 { }, // END
476 MODULE_DEVICE_TABLE(usb, products);
478 static struct usb_driver cdc_driver = {
479 .name = "cdc_ether",
480 .id_table = products,
481 .probe = usbnet_probe,
482 .disconnect = usbnet_disconnect,
483 .suspend = usbnet_suspend,
484 .resume = usbnet_resume,
488 static int __init cdc_init(void)
490 BUG_ON((sizeof(((struct usbnet *)0)->data)
491 < sizeof(struct cdc_state)));
493 return usb_register(&cdc_driver);
495 module_init(cdc_init);
497 static void __exit cdc_exit(void)
499 usb_deregister(&cdc_driver);
501 module_exit(cdc_exit);
503 MODULE_AUTHOR("David Brownell");
504 MODULE_DESCRIPTION("USB CDC Ethernet devices");
505 MODULE_LICENSE("GPL");