1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/netdevice.h>
27#include <linux/etherdevice.h>
28#include <linux/ethtool.h>
29#include <linux/workqueue.h>
30#include <linux/mii.h>
31#include <linux/usb.h>
32#include <linux/usb/cdc.h>
33#include <linux/usb/usbnet.h>
34
35
36#if defined(CONFIG_USB_NET_RNDIS_HOST) || defined(CONFIG_USB_NET_RNDIS_HOST_MODULE)
37
38static int is_rndis(struct usb_interface_descriptor *desc)
39{
40 return desc->bInterfaceClass == USB_CLASS_COMM
41 && desc->bInterfaceSubClass == 2
42 && desc->bInterfaceProtocol == 0xff;
43}
44
45static int is_activesync(struct usb_interface_descriptor *desc)
46{
47 return desc->bInterfaceClass == USB_CLASS_MISC
48 && desc->bInterfaceSubClass == 1
49 && desc->bInterfaceProtocol == 1;
50}
51
52static int is_wireless_rndis(struct usb_interface_descriptor *desc)
53{
54 return desc->bInterfaceClass == USB_CLASS_WIRELESS_CONTROLLER
55 && desc->bInterfaceSubClass == 1
56 && desc->bInterfaceProtocol == 3;
57}
58
59#else
60
61#define is_rndis(desc) 0
62#define is_activesync(desc) 0
63#define is_wireless_rndis(desc) 0
64
65#endif
66
67
68
69
70
71
72
73int usbnet_generic_cdc_bind(struct usbnet *dev, struct usb_interface *intf)
74{
75 u8 *buf = intf->cur_altsetting->extra;
76 int len = intf->cur_altsetting->extralen;
77 struct usb_interface_descriptor *d;
78 struct cdc_state *info = (void *) &dev->data;
79 int status;
80 int rndis;
81 struct usb_driver *driver = driver_of(intf);
82
83 if (sizeof dev->data < sizeof *info)
84 return -EDOM;
85
86
87
88
89 if (len == 0 && dev->udev->actconfig->extralen) {
90
91
92
93 buf = dev->udev->actconfig->extra;
94 len = dev->udev->actconfig->extralen;
95 if (len)
96 dev_dbg(&intf->dev,
97 "CDC descriptors on config\n");
98 }
99
100
101
102
103 if (len == 0) {
104 struct usb_host_endpoint *hep;
105
106 hep = intf->cur_altsetting->endpoint;
107 if (hep) {
108 buf = hep->extra;
109 len = hep->extralen;
110 }
111 if (len)
112 dev_dbg(&intf->dev,
113 "CDC descriptors on endpoint\n");
114 }
115
116
117
118
119 rndis = is_rndis(&intf->cur_altsetting->desc)
120 || is_activesync(&intf->cur_altsetting->desc)
121 || is_wireless_rndis(&intf->cur_altsetting->desc);
122
123 memset(info, 0, sizeof *info);
124 info->control = intf;
125 while (len > 3) {
126 if (buf [1] != USB_DT_CS_INTERFACE)
127 goto next_desc;
128
129
130
131
132
133
134
135
136 switch (buf [2]) {
137 case USB_CDC_HEADER_TYPE:
138 if (info->header) {
139 dev_dbg(&intf->dev, "extra CDC header\n");
140 goto bad_desc;
141 }
142 info->header = (void *) buf;
143 if (info->header->bLength != sizeof *info->header) {
144 dev_dbg(&intf->dev, "CDC header len %u\n",
145 info->header->bLength);
146 goto bad_desc;
147 }
148 break;
149 case USB_CDC_ACM_TYPE:
150
151
152
153 if (rndis) {
154 struct usb_cdc_acm_descriptor *acm;
155
156 acm = (void *) buf;
157 if (acm->bmCapabilities) {
158 dev_dbg(&intf->dev,
159 "ACM capabilities %02x, "
160 "not really RNDIS?\n",
161 acm->bmCapabilities);
162 goto bad_desc;
163 }
164 }
165 break;
166 case USB_CDC_UNION_TYPE:
167 if (info->u) {
168 dev_dbg(&intf->dev, "extra CDC union\n");
169 goto bad_desc;
170 }
171 info->u = (void *) buf;
172 if (info->u->bLength != sizeof *info->u) {
173 dev_dbg(&intf->dev, "CDC union len %u\n",
174 info->u->bLength);
175 goto bad_desc;
176 }
177
178
179
180
181
182 info->control = usb_ifnum_to_if(dev->udev,
183 info->u->bMasterInterface0);
184 info->data = usb_ifnum_to_if(dev->udev,
185 info->u->bSlaveInterface0);
186 if (!info->control || !info->data) {
187 dev_dbg(&intf->dev,
188 "master #%u/%p slave #%u/%p\n",
189 info->u->bMasterInterface0,
190 info->control,
191 info->u->bSlaveInterface0,
192 info->data);
193 goto bad_desc;
194 }
195 if (info->control != intf) {
196 dev_dbg(&intf->dev, "bogus CDC Union\n");
197
198
199
200 if (info->data == intf) {
201 info->data = info->control;
202 info->control = intf;
203 } else
204 goto bad_desc;
205 }
206
207
208 d = &info->data->cur_altsetting->desc;
209 if (d->bInterfaceClass != USB_CLASS_CDC_DATA) {
210 dev_dbg(&intf->dev, "slave class %u\n",
211 d->bInterfaceClass);
212 goto bad_desc;
213 }
214 break;
215 case USB_CDC_ETHERNET_TYPE:
216 if (info->ether) {
217 dev_dbg(&intf->dev, "extra CDC ether\n");
218 goto bad_desc;
219 }
220 info->ether = (void *) buf;
221 if (info->ether->bLength != sizeof *info->ether) {
222 dev_dbg(&intf->dev, "CDC ether len %u\n",
223 info->ether->bLength);
224 goto bad_desc;
225 }
226 dev->hard_mtu = le16_to_cpu(
227 info->ether->wMaxSegmentSize);
228
229
230
231 break;
232 }
233next_desc:
234 len -= buf [0];
235 buf += buf [0];
236 }
237
238
239
240
241
242 if (rndis && !info->u) {
243 info->control = usb_ifnum_to_if(dev->udev, 0);
244 info->data = usb_ifnum_to_if(dev->udev, 1);
245 if (!info->control || !info->data) {
246 dev_dbg(&intf->dev,
247 "rndis: master #0/%p slave #1/%p\n",
248 info->control,
249 info->data);
250 goto bad_desc;
251 }
252
253 } else if (!info->header || !info->u || (!rndis && !info->ether)) {
254 dev_dbg(&intf->dev, "missing cdc %s%s%sdescriptor\n",
255 info->header ? "" : "header ",
256 info->u ? "" : "union ",
257 info->ether ? "" : "ether ");
258 goto bad_desc;
259 }
260
261
262
263
264 status = usb_driver_claim_interface(driver, info->data, dev);
265 if (status < 0)
266 return status;
267 status = usbnet_get_endpoints(dev, info->data);
268 if (status < 0) {
269
270 usb_set_intfdata(info->data, NULL);
271 usb_driver_release_interface(driver, info->data);
272 return status;
273 }
274
275
276 dev->status = NULL;
277 if (info->control->cur_altsetting->desc.bNumEndpoints == 1) {
278 struct usb_endpoint_descriptor *desc;
279
280 dev->status = &info->control->cur_altsetting->endpoint [0];
281 desc = &dev->status->desc;
282 if (!usb_endpoint_is_int_in(desc)
283 || (le16_to_cpu(desc->wMaxPacketSize)
284 < sizeof(struct usb_cdc_notification))
285 || !desc->bInterval) {
286 dev_dbg(&intf->dev, "bad notification endpoint\n");
287 dev->status = NULL;
288 }
289 }
290 if (rndis && !dev->status) {
291 dev_dbg(&intf->dev, "missing RNDIS status endpoint\n");
292 usb_set_intfdata(info->data, NULL);
293 usb_driver_release_interface(driver, info->data);
294 return -ENODEV;
295 }
296 return 0;
297
298bad_desc:
299 dev_info(&dev->udev->dev, "bad CDC descriptors\n");
300 return -ENODEV;
301}
302EXPORT_SYMBOL_GPL(usbnet_generic_cdc_bind);
303
304void usbnet_cdc_unbind(struct usbnet *dev, struct usb_interface *intf)
305{
306 struct cdc_state *info = (void *) &dev->data;
307 struct usb_driver *driver = driver_of(intf);
308
309
310 if (intf == info->control && info->data) {
311
312 usb_set_intfdata(info->data, NULL);
313 usb_driver_release_interface(driver, info->data);
314 info->data = NULL;
315 }
316
317
318 else if (intf == info->data && info->control) {
319
320 usb_set_intfdata(info->control, NULL);
321 usb_driver_release_interface(driver, info->control);
322 info->control = NULL;
323 }
324}
325EXPORT_SYMBOL_GPL(usbnet_cdc_unbind);
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340static void dumpspeed(struct usbnet *dev, __le32 *speeds)
341{
342 if (netif_msg_timer(dev))
343 devinfo(dev, "link speeds: %u kbps up, %u kbps down",
344 __le32_to_cpu(speeds[0]) / 1000,
345 __le32_to_cpu(speeds[1]) / 1000);
346}
347
348static void cdc_status(struct usbnet *dev, struct urb *urb)
349{
350 struct usb_cdc_notification *event;
351
352 if (urb->actual_length < sizeof *event)
353 return;
354
355
356 if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
357 dumpspeed(dev, (__le32 *) urb->transfer_buffer);
358 return;
359 }
360
361 event = urb->transfer_buffer;
362 switch (event->bNotificationType) {
363 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
364 if (netif_msg_timer(dev))
365 devdbg(dev, "CDC: carrier %s",
366 event->wValue ? "on" : "off");
367 if (event->wValue)
368 netif_carrier_on(dev->net);
369 else
370 netif_carrier_off(dev->net);
371 break;
372 case USB_CDC_NOTIFY_SPEED_CHANGE:
373 if (netif_msg_timer(dev))
374 devdbg(dev, "CDC: speed change (len %d)",
375 urb->actual_length);
376 if (urb->actual_length != (sizeof *event + 8))
377 set_bit(EVENT_STS_SPLIT, &dev->flags);
378 else
379 dumpspeed(dev, (__le32 *) &event[1]);
380 break;
381
382
383
384 default:
385 deverr(dev, "CDC: unexpected notification %02x!",
386 event->bNotificationType);
387 break;
388 }
389}
390
391static int cdc_bind(struct usbnet *dev, struct usb_interface *intf)
392{
393 int status;
394 struct cdc_state *info = (void *) &dev->data;
395
396 status = usbnet_generic_cdc_bind(dev, intf);
397 if (status < 0)
398 return status;
399
400 status = usbnet_get_ethernet_addr(dev, info->ether->iMACAddress);
401 if (status < 0) {
402 usb_set_intfdata(info->data, NULL);
403 usb_driver_release_interface(driver_of(intf), info->data);
404 return status;
405 }
406
407
408
409
410
411 return 0;
412}
413
414static const struct driver_info cdc_info = {
415 .description = "CDC Ethernet Device",
416 .flags = FLAG_ETHER,
417
418 .bind = cdc_bind,
419 .unbind = usbnet_cdc_unbind,
420 .status = cdc_status,
421};
422
423
424
425
426static const struct usb_device_id products [] = {
427
428
429
430
431
432
433
434
435
436#define ZAURUS_MASTER_INTERFACE \
437 .bInterfaceClass = USB_CLASS_COMM, \
438 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
439 .bInterfaceProtocol = USB_CDC_PROTO_NONE
440
441
442
443
444
445{
446 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
447 | USB_DEVICE_ID_MATCH_DEVICE,
448 .idVendor = 0x04DD,
449 .idProduct = 0x8004,
450 ZAURUS_MASTER_INTERFACE,
451 .driver_info = 0,
452},
453
454
455
456
457
458
459{
460 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
461 | USB_DEVICE_ID_MATCH_DEVICE,
462 .idVendor = 0x04DD,
463 .idProduct = 0x8005,
464 ZAURUS_MASTER_INTERFACE,
465 .driver_info = 0,
466}, {
467 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
468 | USB_DEVICE_ID_MATCH_DEVICE,
469 .idVendor = 0x04DD,
470 .idProduct = 0x8006,
471 ZAURUS_MASTER_INTERFACE,
472 .driver_info = 0,
473}, {
474 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
475 | USB_DEVICE_ID_MATCH_DEVICE,
476 .idVendor = 0x04DD,
477 .idProduct = 0x8007,
478 ZAURUS_MASTER_INTERFACE,
479 .driver_info = 0,
480}, {
481 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
482 | USB_DEVICE_ID_MATCH_DEVICE,
483 .idVendor = 0x04DD,
484 .idProduct = 0x9031,
485 ZAURUS_MASTER_INTERFACE,
486 .driver_info = 0,
487}, {
488 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
489 | USB_DEVICE_ID_MATCH_DEVICE,
490 .idVendor = 0x04DD,
491 .idProduct = 0x9032,
492 ZAURUS_MASTER_INTERFACE,
493 .driver_info = 0,
494}, {
495 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
496 | USB_DEVICE_ID_MATCH_DEVICE,
497 .idVendor = 0x04DD,
498
499 .idProduct = 0x9050,
500 ZAURUS_MASTER_INTERFACE,
501 .driver_info = 0,
502},
503
504
505
506
507{
508 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
509 | USB_DEVICE_ID_MATCH_DEVICE,
510 .idVendor = 0x07B4,
511 .idProduct = 0x0F02,
512 ZAURUS_MASTER_INTERFACE,
513 .driver_info = 0,
514},
515
516
517
518
519
520
521
522
523
524
525
526
527{
528 USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ETHERNET,
529 USB_CDC_PROTO_NONE),
530 .driver_info = (unsigned long) &cdc_info,
531}, {
532
533 USB_DEVICE_AND_INTERFACE_INFO(0x0bdb, 0x1900, USB_CLASS_COMM,
534 USB_CDC_SUBCLASS_MDLM, USB_CDC_PROTO_NONE),
535 .driver_info = (unsigned long) &cdc_info,
536}, {
537
538 USB_DEVICE_AND_INTERFACE_INFO(0x0bdb, 0x1902, USB_CLASS_COMM,
539 USB_CDC_SUBCLASS_MDLM, USB_CDC_PROTO_NONE),
540 .driver_info = (unsigned long) &cdc_info,
541}, {
542
543 USB_DEVICE_AND_INTERFACE_INFO(0x0bdb, 0x1904, USB_CLASS_COMM,
544 USB_CDC_SUBCLASS_MDLM, USB_CDC_PROTO_NONE),
545 .driver_info = (unsigned long) &cdc_info,
546}, {
547
548 USB_DEVICE_AND_INTERFACE_INFO(0x0bdb, 0x1905, USB_CLASS_COMM,
549 USB_CDC_SUBCLASS_MDLM, USB_CDC_PROTO_NONE),
550 .driver_info = (unsigned long) &cdc_info,
551}, {
552
553 USB_DEVICE_AND_INTERFACE_INFO(0x0bdb, 0x1906, USB_CLASS_COMM,
554 USB_CDC_SUBCLASS_MDLM, USB_CDC_PROTO_NONE),
555 .driver_info = (unsigned long) &cdc_info,
556}, {
557
558 USB_DEVICE_AND_INTERFACE_INFO(0x0bdb, 0x190a, USB_CLASS_COMM,
559 USB_CDC_SUBCLASS_MDLM, USB_CDC_PROTO_NONE),
560 .driver_info = (unsigned long) &cdc_info,
561}, {
562
563 USB_DEVICE_AND_INTERFACE_INFO(0x0bdb, 0x1909, USB_CLASS_COMM,
564 USB_CDC_SUBCLASS_MDLM, USB_CDC_PROTO_NONE),
565 .driver_info = (unsigned long) &cdc_info,
566}, {
567
568 USB_DEVICE_AND_INTERFACE_INFO(0x0bdb, 0x1049, USB_CLASS_COMM,
569 USB_CDC_SUBCLASS_MDLM, USB_CDC_PROTO_NONE),
570 .driver_info = (unsigned long) &cdc_info,
571}, {
572
573 USB_DEVICE_AND_INTERFACE_INFO(0x0930, 0x130b, USB_CLASS_COMM,
574 USB_CDC_SUBCLASS_MDLM, USB_CDC_PROTO_NONE),
575 .driver_info = (unsigned long) &cdc_info,
576}, {
577
578 USB_DEVICE_AND_INTERFACE_INFO(0x0930, 0x130c, USB_CLASS_COMM,
579 USB_CDC_SUBCLASS_MDLM, USB_CDC_PROTO_NONE),
580 .driver_info = (unsigned long) &cdc_info,
581}, {
582
583 USB_DEVICE_AND_INTERFACE_INFO(0x0930, 0x1311, USB_CLASS_COMM,
584 USB_CDC_SUBCLASS_MDLM, USB_CDC_PROTO_NONE),
585 .driver_info = (unsigned long) &cdc_info,
586}, {
587
588 USB_DEVICE_AND_INTERFACE_INFO(0x413c, 0x8147, USB_CLASS_COMM,
589 USB_CDC_SUBCLASS_MDLM, USB_CDC_PROTO_NONE),
590 .driver_info = (unsigned long) &cdc_info,
591}, {
592
593 USB_DEVICE_AND_INTERFACE_INFO(0x413c, 0x8183, USB_CLASS_COMM,
594 USB_CDC_SUBCLASS_MDLM, USB_CDC_PROTO_NONE),
595 .driver_info = (unsigned long) &cdc_info,
596}, {
597
598 USB_DEVICE_AND_INTERFACE_INFO(0x413c, 0x8184, USB_CLASS_COMM,
599 USB_CDC_SUBCLASS_MDLM, USB_CDC_PROTO_NONE),
600 .driver_info = (unsigned long) &cdc_info,
601},
602 { },
603};
604MODULE_DEVICE_TABLE(usb, products);
605
606static struct usb_driver cdc_driver = {
607 .name = "cdc_ether",
608 .id_table = products,
609 .probe = usbnet_probe,
610 .disconnect = usbnet_disconnect,
611 .suspend = usbnet_suspend,
612 .resume = usbnet_resume,
613};
614
615
616static int __init cdc_init(void)
617{
618 BUILD_BUG_ON((sizeof(((struct usbnet *)0)->data)
619 < sizeof(struct cdc_state)));
620
621 return usb_register(&cdc_driver);
622}
623module_init(cdc_init);
624
625static void __exit cdc_exit(void)
626{
627 usb_deregister(&cdc_driver);
628}
629module_exit(cdc_exit);
630
631MODULE_AUTHOR("David Brownell");
632MODULE_DESCRIPTION("USB CDC Ethernet devices");
633MODULE_LICENSE("GPL");
634