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