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