c008c981862bfad424f70b7f33e2ff4703332454
[linux-2.6.git] / drivers / usb / net / cdc_ether.c
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/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>
35
36 #include "usbnet.h"
37
38
39 /*
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.
44  */
45 int usbnet_generic_cdc_bind(struct usbnet *dev, struct usb_interface *intf)
46 {
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);
54
55         if (sizeof dev->data < sizeof *info)
56                 return -EDOM;
57
58         /* expect strict spec conformance for the descriptors, but
59          * cope with firmware which stores them in the wrong place
60          */
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
64                  */
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");
70         }
71
72         /* this assumes that if there's a non-RNDIS vendor variant
73          * of cdc-acm, it'll fail RNDIS requests cleanly.
74          */
75         rndis = (intf->cur_altsetting->desc.bInterfaceProtocol == 0xff);
76
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;
82
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.
89                  */
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;
95                         }
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;
101                         }
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;
107                         }
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;
113                         }
114
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.
118                          */
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;
131                         }
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.
136                                  */
137                                 if (info->data == intf) {
138                                         info->data = info->control;
139                                         info->control = intf;
140                                 } else
141                                         goto bad_desc;
142                         }
143
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;
150                         }
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;
156                         }
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;
162                         }
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.
167                          */
168                         break;
169                 }
170 next_desc:
171                 len -= buf [0]; /* bLength */
172                 buf += buf [0];
173         }
174
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;
181         }
182
183         /* claim data interface and set it up ... with side effects.
184          * network traffic can't flow until an altsetting is enabled.
185          */
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;
195         }
196
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;
201
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;
211                 }
212         }
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;
218         }
219         return 0;
220
221 bad_desc:
222         dev_info(&dev->udev->dev, "bad CDC descriptors\n");
223         return -ENODEV;
224 }
225 EXPORT_SYMBOL_GPL(usbnet_generic_cdc_bind);
226
227 void usbnet_cdc_unbind(struct usbnet *dev, struct usb_interface *intf)
228 {
229         struct cdc_state                *info = (void *) &dev->data;
230         struct usb_driver               *driver = driver_of(intf);
231
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;
238         }
239
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;
246         }
247 }
248 EXPORT_SYMBOL_GPL(usbnet_cdc_unbind);
249
250 \f
251 /*-------------------------------------------------------------------------
252  *
253  * Communications Device Class, Ethernet Control model
254  *
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.
258  *
259  * This should interop with whatever the 2.4 "CDCEther.c" driver
260  * (by Brad Hards) talked with, with more functionality.
261  *
262  *-------------------------------------------------------------------------*/
263
264 static void dumpspeed(struct usbnet *dev, __le32 *speeds)
265 {
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);
270 }
271
272 static void cdc_status(struct usbnet *dev, struct urb *urb)
273 {
274         struct usb_cdc_notification     *event;
275
276         if (urb->actual_length < sizeof *event)
277                 return;
278
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;
283         }
284
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.
307          */
308         default:
309                 deverr(dev, "CDC: unexpected notification %02x!",
310                                  event->bNotificationType);
311                 break;
312         }
313 }
314
315 static u8 nibble(unsigned char c)
316 {
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;
323 }
324
325 static inline int
326 get_ethernet_addr(struct usbnet *dev, struct usb_cdc_ether_desc *e)
327 {
328         int             tmp, i;
329         unsigned char   buf [13];
330
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;
338         }
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;
343 }
344
345 static int cdc_bind(struct usbnet *dev, struct usb_interface *intf)
346 {
347         int                             status;
348         struct cdc_state                *info = (void *) &dev->data;
349
350         status = usbnet_generic_cdc_bind(dev, intf);
351         if (status < 0)
352                 return status;
353
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;
359         }
360
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.
364          */
365         return 0;
366 }
367
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,
375 };
376
377 /*-------------------------------------------------------------------------*/
378
379
380 static const struct usb_device_id       products [] = {
381 /*
382  * BLACKLIST !!
383  *
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.
388  */
389
390 #define ZAURUS_MASTER_INTERFACE \
391         .bInterfaceClass        = USB_CLASS_COMM, \
392         .bInterfaceSubClass     = USB_CDC_SUBCLASS_ETHERNET, \
393         .bInterfaceProtocol     = USB_CDC_PROTO_NONE
394
395 /* SA-1100 based Sharp Zaurus ("collie"), or compatible;
396  * wire-incompatible with true CDC Ethernet implementations.
397  * (And, it seems, needlessly so...)
398  */
399 {
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,
406 },
407
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.
412  */
413 {
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,
456 },
457
458 /*
459  * WHITELIST!!!
460  *
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.
465  *
466  * NOTE:  this match must come AFTER entries blacklisting devices
467  * because of bugs/quirks in a given product (like Zaurus, above).
468  */
469 {
470         USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ETHERNET,
471                         USB_CDC_PROTO_NONE),
472         .driver_info = (unsigned long) &cdc_info,
473 },
474         { },            // END
475 };
476 MODULE_DEVICE_TABLE(usb, products);
477
478 static struct usb_driver cdc_driver = {
479         .owner =        THIS_MODULE,
480         .name =         "cdc_ether",
481         .id_table =     products,
482         .probe =        usbnet_probe,
483         .disconnect =   usbnet_disconnect,
484         .suspend =      usbnet_suspend,
485         .resume =       usbnet_resume,
486 };
487
488
489 static int __init cdc_init(void)
490 {
491         BUG_ON((sizeof(((struct usbnet *)0)->data)
492                         < sizeof(struct cdc_state)));
493
494         return usb_register(&cdc_driver);
495 }
496 module_init(cdc_init);
497
498 static void __exit cdc_exit(void)
499 {
500         usb_deregister(&cdc_driver);
501 }
502 module_exit(cdc_exit);
503
504 MODULE_AUTHOR("David Brownell");
505 MODULE_DESCRIPTION("USB CDC Ethernet devices");
506 MODULE_LICENSE("GPL");