Merge branch 'HEAD' of master.kernel.org:/pub/scm/linux/kernel/git/herbert/crypto-2.6
[pandora-kernel.git] / drivers / usb / net / cdc_ether.c
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)
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
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 #if defined(CONFIG_USB_NET_RNDIS_HOST) || defined(CONFIG_USB_NET_RNDIS_HOST_MODULE)
40
41 static int is_rndis(struct usb_interface_descriptor *desc)
42 {
43         return desc->bInterfaceClass == USB_CLASS_COMM
44                 && desc->bInterfaceSubClass == 2
45                 && desc->bInterfaceProtocol == 0xff;
46 }
47
48 static int is_activesync(struct usb_interface_descriptor *desc)
49 {
50         return desc->bInterfaceClass == USB_CLASS_MISC
51                 && desc->bInterfaceSubClass == 1
52                 && desc->bInterfaceProtocol == 1;
53 }
54
55 #else
56
57 #define is_rndis(desc)          0
58 #define is_activesync(desc)     0
59
60 #endif
61
62 /*
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.
67  */
68 int usbnet_generic_cdc_bind(struct usbnet *dev, struct usb_interface *intf)
69 {
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);
77
78         if (sizeof dev->data < sizeof *info)
79                 return -EDOM;
80
81         /* expect strict spec conformance for the descriptors, but
82          * cope with firmware which stores them in the wrong place
83          */
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
87                  */
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");
93         }
94
95         /* this assumes that if there's a non-RNDIS vendor variant
96          * of cdc-acm, it'll fail RNDIS requests cleanly.
97          */
98         rndis = is_rndis(&intf->cur_altsetting->desc)
99                 || is_activesync(&intf->cur_altsetting->desc);
100
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;
106
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.
113                  */
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;
119                         }
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;
125                         }
126                         break;
127                 case USB_CDC_ACM_TYPE:
128                         /* paranoia:  disambiguate a "real" vendor-specific
129                          * modem interface from an RNDIS non-modem.
130                          */
131                         if (rndis) {
132                                 struct usb_cdc_acm_descriptor *d;
133
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;
141                                 }
142                         }
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;
148                         }
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;
154                         }
155
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.
159                          */
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;
172                         }
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.
177                                  */
178                                 if (info->data == intf) {
179                                         info->data = info->control;
180                                         info->control = intf;
181                                 } else
182                                         goto bad_desc;
183                         }
184
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;
191                         }
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;
197                         }
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;
203                         }
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.
208                          */
209                         break;
210                 }
211 next_desc:
212                 len -= buf [0]; /* bLength */
213                 buf += buf [0];
214         }
215
216         /* Microsoft ActiveSync based RNDIS devices lack the CDC descriptors,
217          * so we'll hard-wire the interfaces and not check for descriptors.
218          */
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;
228                 }
229
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;
236         }
237
238         /* claim data interface and set it up ... with side effects.
239          * network traffic can't flow until an altsetting is enabled.
240          */
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;
250         }
251
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;
256
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;
265                 }
266         }
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;
272         }
273         return 0;
274
275 bad_desc:
276         dev_info(&dev->udev->dev, "bad CDC descriptors\n");
277         return -ENODEV;
278 }
279 EXPORT_SYMBOL_GPL(usbnet_generic_cdc_bind);
280
281 void usbnet_cdc_unbind(struct usbnet *dev, struct usb_interface *intf)
282 {
283         struct cdc_state                *info = (void *) &dev->data;
284         struct usb_driver               *driver = driver_of(intf);
285
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;
292         }
293
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;
300         }
301 }
302 EXPORT_SYMBOL_GPL(usbnet_cdc_unbind);
303
304 \f
305 /*-------------------------------------------------------------------------
306  *
307  * Communications Device Class, Ethernet Control model
308  *
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.
312  *
313  * This should interop with whatever the 2.4 "CDCEther.c" driver
314  * (by Brad Hards) talked with, with more functionality.
315  *
316  *-------------------------------------------------------------------------*/
317
318 static void dumpspeed(struct usbnet *dev, __le32 *speeds)
319 {
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);
324 }
325
326 static void cdc_status(struct usbnet *dev, struct urb *urb)
327 {
328         struct usb_cdc_notification     *event;
329
330         if (urb->actual_length < sizeof *event)
331                 return;
332
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;
337         }
338
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.
361          */
362         default:
363                 deverr(dev, "CDC: unexpected notification %02x!",
364                                  event->bNotificationType);
365                 break;
366         }
367 }
368
369 static u8 nibble(unsigned char c)
370 {
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;
377 }
378
379 static inline int
380 get_ethernet_addr(struct usbnet *dev, struct usb_cdc_ether_desc *e)
381 {
382         int             tmp, i;
383         unsigned char   buf [13];
384
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;
392         }
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;
397 }
398
399 static int cdc_bind(struct usbnet *dev, struct usb_interface *intf)
400 {
401         int                             status;
402         struct cdc_state                *info = (void *) &dev->data;
403
404         status = usbnet_generic_cdc_bind(dev, intf);
405         if (status < 0)
406                 return status;
407
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;
413         }
414
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.
418          */
419         return 0;
420 }
421
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,
429 };
430
431 /*-------------------------------------------------------------------------*/
432
433
434 static const struct usb_device_id       products [] = {
435 /*
436  * BLACKLIST !!
437  *
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.
442  */
443
444 #define ZAURUS_MASTER_INTERFACE \
445         .bInterfaceClass        = USB_CLASS_COMM, \
446         .bInterfaceSubClass     = USB_CDC_SUBCLASS_ETHERNET, \
447         .bInterfaceProtocol     = USB_CDC_PROTO_NONE
448
449 /* SA-1100 based Sharp Zaurus ("collie"), or compatible;
450  * wire-incompatible with true CDC Ethernet implementations.
451  * (And, it seems, needlessly so...)
452  */
453 {
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,
460 },
461
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.
466  */
467 {
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,
510 },
511
512 /* Olympus has some models with a Zaurus-compatible option.
513  * R-1000 uses a FreeScale i.MXL cpu (ARMv4T)
514  */
515 {
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,
522 },
523
524 /*
525  * WHITELIST!!!
526  *
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.
531  *
532  * NOTE:  this match must come AFTER entries blacklisting devices
533  * because of bugs/quirks in a given product (like Zaurus, above).
534  */
535 {
536         USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ETHERNET,
537                         USB_CDC_PROTO_NONE),
538         .driver_info = (unsigned long) &cdc_info,
539 },
540         { },            // END
541 };
542 MODULE_DEVICE_TABLE(usb, products);
543
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,
551 };
552
553
554 static int __init cdc_init(void)
555 {
556         BUILD_BUG_ON((sizeof(((struct usbnet *)0)->data)
557                         < sizeof(struct cdc_state)));
558
559         return usb_register(&cdc_driver);
560 }
561 module_init(cdc_init);
562
563 static void __exit cdc_exit(void)
564 {
565         usb_deregister(&cdc_driver);
566 }
567 module_exit(cdc_exit);
568
569 MODULE_AUTHOR("David Brownell");
570 MODULE_DESCRIPTION("USB CDC Ethernet devices");
571 MODULE_LICENSE("GPL");