OpenCores
URL https://opencores.org/ocsvn/test_project/test_project/trunk

Subversion Repositories test_project

[/] [test_project/] [trunk/] [linux_sd_driver/] [drivers/] [net/] [usb/] [cdc_ether.c] - Blame information for rev 62

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.