2 * Copyright (c) 2012 Bjørn Mork <bjorn@mork.no>
4 * The probing code is heavily inspired by cdc_ether, which is:
5 * Copyright (C) 2003-2005 by David Brownell
6 * Copyright (C) 2006 by Ole Andre Vadla Ravnas (ActiveSync)
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/netdevice.h>
15 #include <linux/ethtool.h>
16 #include <linux/mii.h>
17 #include <linux/usb.h>
18 #include <linux/usb/cdc.h>
19 #include <linux/usb/usbnet.h>
20 #include <linux/usb/cdc-wdm.h>
22 /* This driver supports wwan (3G/LTE/?) devices using a vendor
23 * specific management protocol called Qualcomm MSM Interface (QMI) -
24 * in addition to the more common AT commands over serial interface
27 * QMI is wrapped in CDC, using CDC encapsulated commands on the
28 * control ("master") interface of a two-interface CDC Union
29 * resembling standard CDC ECM. The devices do not use the control
30 * interface for any other CDC messages. Most likely because the
31 * management protocol is used in place of the standard CDC
32 * notifications NOTIFY_NETWORK_CONNECTION and NOTIFY_SPEED_CHANGE
34 * Alternatively, control and data functions can be combined in a
35 * single USB interface.
37 * Handling a protocol like QMI is out of the scope for any driver.
38 * It is exported as a character device using the cdc-wdm driver as
39 * a subdriver, enabling userspace applications ("modem managers") to
42 * These devices may alternatively/additionally be configured using AT
43 * commands on a serial interface
46 /* driver specific data */
47 struct qmi_wwan_state {
48 struct usb_driver *subdriver;
51 struct usb_interface *control;
52 struct usb_interface *data;
55 /* using a counter to merge subdriver requests with our own into a combined state */
56 static int qmi_wwan_manage_power(struct usbnet *dev, int on)
58 struct qmi_wwan_state *info = (void *)&dev->data;
61 dev_dbg(&dev->intf->dev, "%s() pmcount=%d, on=%d\n", __func__, atomic_read(&info->pmcount), on);
63 if ((on && atomic_add_return(1, &info->pmcount) == 1) || (!on && atomic_dec_and_test(&info->pmcount))) {
64 /* need autopm_get/put here to ensure the usbcore sees the new value */
65 rv = usb_autopm_get_interface(dev->intf);
68 dev->intf->needs_remote_wakeup = on;
69 usb_autopm_put_interface(dev->intf);
75 static int qmi_wwan_cdc_wdm_manage_power(struct usb_interface *intf, int on)
77 struct usbnet *dev = usb_get_intfdata(intf);
78 return qmi_wwan_manage_power(dev, on);
81 /* collect all three endpoints and register subdriver */
82 static int qmi_wwan_register_subdriver(struct usbnet *dev)
85 struct usb_driver *subdriver = NULL;
86 struct qmi_wwan_state *info = (void *)&dev->data;
88 /* collect bulk endpoints */
89 rv = usbnet_get_endpoints(dev, info->data);
93 /* update status endpoint if separate control interface */
94 if (info->control != info->data)
95 dev->status = &info->control->cur_altsetting->endpoint[0];
97 /* require interrupt endpoint for subdriver */
103 /* for subdriver power management */
104 atomic_set(&info->pmcount, 0);
106 /* register subdriver */
107 subdriver = usb_cdc_wdm_register(info->control, &dev->status->desc, 512, &qmi_wwan_cdc_wdm_manage_power);
108 if (IS_ERR(subdriver)) {
109 dev_err(&info->control->dev, "subdriver registration failed\n");
110 rv = PTR_ERR(subdriver);
114 /* prevent usbnet from using status endpoint */
117 /* save subdriver struct for suspend/resume wrappers */
118 info->subdriver = subdriver;
124 static int qmi_wwan_bind(struct usbnet *dev, struct usb_interface *intf)
127 u8 *buf = intf->cur_altsetting->extra;
128 int len = intf->cur_altsetting->extralen;
129 struct usb_interface_descriptor *desc = &intf->cur_altsetting->desc;
130 struct usb_cdc_union_desc *cdc_union = NULL;
131 struct usb_cdc_ether_desc *cdc_ether = NULL;
132 u32 required = 1 << USB_CDC_HEADER_TYPE | 1 << USB_CDC_UNION_TYPE;
134 struct usb_driver *driver = driver_of(intf);
135 struct qmi_wwan_state *info = (void *)&dev->data;
137 BUILD_BUG_ON((sizeof(((struct usbnet *)0)->data) < sizeof(struct qmi_wwan_state)));
139 /* require a single interrupt status endpoint for subdriver */
140 if (intf->cur_altsetting->desc.bNumEndpoints != 1)
144 struct usb_descriptor_header *h = (void *)buf;
146 /* ignore any misplaced descriptors */
147 if (h->bDescriptorType != USB_DT_CS_INTERFACE)
150 /* buf[2] is CDC descriptor subtype */
152 case USB_CDC_HEADER_TYPE:
153 if (found & 1 << USB_CDC_HEADER_TYPE) {
154 dev_dbg(&intf->dev, "extra CDC header\n");
157 if (h->bLength != sizeof(struct usb_cdc_header_desc)) {
158 dev_dbg(&intf->dev, "CDC header len %u\n", h->bLength);
162 case USB_CDC_UNION_TYPE:
163 if (found & 1 << USB_CDC_UNION_TYPE) {
164 dev_dbg(&intf->dev, "extra CDC union\n");
167 if (h->bLength != sizeof(struct usb_cdc_union_desc)) {
168 dev_dbg(&intf->dev, "CDC union len %u\n", h->bLength);
171 cdc_union = (struct usb_cdc_union_desc *)buf;
173 case USB_CDC_ETHERNET_TYPE:
174 if (found & 1 << USB_CDC_ETHERNET_TYPE) {
175 dev_dbg(&intf->dev, "extra CDC ether\n");
178 if (h->bLength != sizeof(struct usb_cdc_ether_desc)) {
179 dev_dbg(&intf->dev, "CDC ether len %u\n", h->bLength);
182 cdc_ether = (struct usb_cdc_ether_desc *)buf;
187 * Remember which CDC functional descriptors we've seen. Works
188 * for all types we care about, of which USB_CDC_ETHERNET_TYPE
189 * (0x0f) is the highest numbered
192 found |= 1 << buf[2];
199 /* did we find all the required ones? */
200 if ((found & required) != required) {
201 dev_err(&intf->dev, "CDC functional descriptors missing\n");
205 /* verify CDC Union */
206 if (desc->bInterfaceNumber != cdc_union->bMasterInterface0) {
207 dev_err(&intf->dev, "bogus CDC Union: master=%u\n", cdc_union->bMasterInterface0);
211 /* need to save these for unbind */
212 info->control = intf;
213 info->data = usb_ifnum_to_if(dev->udev, cdc_union->bSlaveInterface0);
215 dev_err(&intf->dev, "bogus CDC Union: slave=%u\n", cdc_union->bSlaveInterface0);
219 /* errors aren't fatal - we can live with the dynamic address */
221 dev->hard_mtu = le16_to_cpu(cdc_ether->wMaxSegmentSize);
222 usbnet_get_ethernet_addr(dev, cdc_ether->iMACAddress);
225 /* claim data interface and set it up */
226 status = usb_driver_claim_interface(driver, info->data, dev);
230 status = qmi_wwan_register_subdriver(dev);
232 usb_set_intfdata(info->data, NULL);
233 usb_driver_release_interface(driver, info->data);
240 /* Some devices combine the "control" and "data" functions into a
241 * single interface with all three endpoints: interrupt + bulk in and
244 static int qmi_wwan_bind_shared(struct usbnet *dev, struct usb_interface *intf)
247 struct qmi_wwan_state *info = (void *)&dev->data;
249 /* ZTE makes devices where the interface descriptors and endpoint
250 * configurations of two or more interfaces are identical, even
251 * though the functions are completely different. If set, then
252 * driver_info->data is a bitmap of acceptable interface numbers
253 * allowing us to bind to one such interface without binding to
256 if (dev->driver_info->data &&
257 !test_bit(intf->cur_altsetting->desc.bInterfaceNumber, &dev->driver_info->data)) {
258 dev_info(&intf->dev, "not on our whitelist - ignored");
263 /* control and data is shared */
264 info->control = intf;
266 rv = qmi_wwan_register_subdriver(dev);
272 /* Gobi devices uses identical class/protocol codes for all interfaces regardless
273 * of function. Some of these are CDC ACM like and have the exact same endpoints
274 * we are looking for. This leaves two possible strategies for identifying the
276 * a) hardcoding interface number, or
277 * b) use the fact that the wwan interface is the only one lacking additional
278 * (CDC functional) descriptors
280 * Let's see if we can get away with the generic b) solution.
282 static int qmi_wwan_bind_gobi(struct usbnet *dev, struct usb_interface *intf)
286 /* ignore any interface with additional descriptors */
287 if (intf->cur_altsetting->extralen)
290 rv = qmi_wwan_bind_shared(dev, intf);
295 static void qmi_wwan_unbind(struct usbnet *dev, struct usb_interface *intf)
297 struct qmi_wwan_state *info = (void *)&dev->data;
298 struct usb_driver *driver = driver_of(intf);
299 struct usb_interface *other;
301 if (info->subdriver && info->subdriver->disconnect)
302 info->subdriver->disconnect(info->control);
304 /* allow user to unbind using either control or data */
305 if (intf == info->control)
308 other = info->control;
310 /* only if not shared */
311 if (other && intf != other) {
312 usb_set_intfdata(other, NULL);
313 usb_driver_release_interface(driver, other);
316 info->subdriver = NULL;
318 info->control = NULL;
321 /* suspend/resume wrappers calling both usbnet and the cdc-wdm
322 * subdriver if present.
324 * NOTE: cdc-wdm also supports pre/post_reset, but we cannot provide
325 * wrappers for those without adding usbnet reset support first.
327 static int qmi_wwan_suspend(struct usb_interface *intf, pm_message_t message)
329 struct usbnet *dev = usb_get_intfdata(intf);
330 struct qmi_wwan_state *info = (void *)&dev->data;
333 ret = usbnet_suspend(intf, message);
337 if (info->subdriver && info->subdriver->suspend)
338 ret = info->subdriver->suspend(intf, message);
345 static int qmi_wwan_resume(struct usb_interface *intf)
347 struct usbnet *dev = usb_get_intfdata(intf);
348 struct qmi_wwan_state *info = (void *)&dev->data;
351 if (info->subdriver && info->subdriver->resume)
352 ret = info->subdriver->resume(intf);
355 ret = usbnet_resume(intf);
356 if (ret < 0 && info->subdriver && info->subdriver->resume && info->subdriver->suspend)
357 info->subdriver->suspend(intf, PMSG_SUSPEND);
362 static const struct driver_info qmi_wwan_info = {
363 .description = "QMI speaking wwan device",
365 .bind = qmi_wwan_bind,
366 .unbind = qmi_wwan_unbind,
367 .manage_power = qmi_wwan_manage_power,
370 static const struct driver_info qmi_wwan_shared = {
371 .description = "QMI speaking wwan device with combined interface",
373 .bind = qmi_wwan_bind_shared,
374 .unbind = qmi_wwan_unbind,
375 .manage_power = qmi_wwan_manage_power,
378 static const struct driver_info qmi_wwan_gobi = {
379 .description = "Qualcomm Gobi wwan/QMI device",
381 .bind = qmi_wwan_bind_gobi,
382 .unbind = qmi_wwan_unbind,
383 .manage_power = qmi_wwan_manage_power,
386 /* ZTE suck at making USB descriptors */
387 static const struct driver_info qmi_wwan_force_int1 = {
388 .description = "Qualcomm WWAN/QMI device",
390 .bind = qmi_wwan_bind_shared,
391 .unbind = qmi_wwan_unbind,
392 .manage_power = qmi_wwan_manage_power,
393 .data = BIT(1), /* interface whitelist bitmap */
396 static const struct driver_info qmi_wwan_force_int4 = {
397 .description = "Qualcomm WWAN/QMI device",
399 .bind = qmi_wwan_bind_shared,
400 .unbind = qmi_wwan_unbind,
401 .manage_power = qmi_wwan_manage_power,
402 .data = BIT(4), /* interface whitelist bitmap */
405 /* Sierra Wireless provide equally useless interface descriptors
406 * Devices in QMI mode can be switched between two different
408 * a) USB interface #8 is QMI/wwan
409 * b) USB interfaces #8, #19 and #20 are QMI/wwan
411 * Both configurations provide a number of other interfaces (serial++),
412 * some of which have the same endpoint configuration as we expect, so
413 * a whitelist or blacklist is necessary.
415 * FIXME: The below whitelist should include BIT(20). It does not
416 * because I cannot get it to work...
418 static const struct driver_info qmi_wwan_sierra = {
419 .description = "Sierra Wireless wwan/QMI device",
421 .bind = qmi_wwan_bind_gobi,
422 .unbind = qmi_wwan_unbind,
423 .manage_power = qmi_wwan_manage_power,
424 .data = BIT(8) | BIT(19), /* interface whitelist bitmap */
427 #define HUAWEI_VENDOR_ID 0x12D1
428 #define QMI_GOBI_DEVICE(vend, prod) \
429 USB_DEVICE(vend, prod), \
430 .driver_info = (unsigned long)&qmi_wwan_gobi
432 static const struct usb_device_id products[] = {
433 { /* Huawei E392, E398 and possibly others sharing both device id and more... */
434 .match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_INT_INFO,
435 .idVendor = HUAWEI_VENDOR_ID,
436 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
437 .bInterfaceSubClass = 1,
438 .bInterfaceProtocol = 9, /* CDC Ethernet *control* interface */
439 .driver_info = (unsigned long)&qmi_wwan_info,
441 { /* Vodafone/Huawei K5005 (12d1:14c8) and similar modems */
442 .match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_INT_INFO,
443 .idVendor = HUAWEI_VENDOR_ID,
444 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
445 .bInterfaceSubClass = 1,
446 .bInterfaceProtocol = 57, /* CDC Ethernet *control* interface */
447 .driver_info = (unsigned long)&qmi_wwan_info,
449 { /* Huawei E392, E398 and possibly others in "Windows mode"
450 * using a combined control and data interface without any CDC
451 * functional descriptors
453 .match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_INT_INFO,
454 .idVendor = HUAWEI_VENDOR_ID,
455 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
456 .bInterfaceSubClass = 1,
457 .bInterfaceProtocol = 17,
458 .driver_info = (unsigned long)&qmi_wwan_shared,
460 { /* Pantech UML290 */
461 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_INT_INFO,
464 .bInterfaceClass = 0xff,
465 .bInterfaceSubClass = 0xf0,
466 .bInterfaceProtocol = 0xff,
467 .driver_info = (unsigned long)&qmi_wwan_shared,
470 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_INT_INFO,
473 .bInterfaceClass = 0xff,
474 .bInterfaceSubClass = 0xff,
475 .bInterfaceProtocol = 0xff,
476 .driver_info = (unsigned long)&qmi_wwan_force_int4,
478 { /* ZTE (Vodafone) K3520-Z */
479 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_INT_INFO,
482 .bInterfaceClass = 0xff,
483 .bInterfaceSubClass = 0xff,
484 .bInterfaceProtocol = 0xff,
485 .driver_info = (unsigned long)&qmi_wwan_force_int1,
487 { /* ZTE (Vodafone) K3565-Z */
488 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_INT_INFO,
491 .bInterfaceClass = 0xff,
492 .bInterfaceSubClass = 0xff,
493 .bInterfaceProtocol = 0xff,
494 .driver_info = (unsigned long)&qmi_wwan_force_int4,
496 { /* ZTE (Vodafone) K3570-Z */
497 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_INT_INFO,
500 .bInterfaceClass = 0xff,
501 .bInterfaceSubClass = 0xff,
502 .bInterfaceProtocol = 0xff,
503 .driver_info = (unsigned long)&qmi_wwan_force_int4,
505 { /* ZTE (Vodafone) K3571-Z */
506 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_INT_INFO,
509 .bInterfaceClass = 0xff,
510 .bInterfaceSubClass = 0xff,
511 .bInterfaceProtocol = 0xff,
512 .driver_info = (unsigned long)&qmi_wwan_force_int4,
514 { /* ZTE (Vodafone) K3765-Z */
515 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_INT_INFO,
518 .bInterfaceClass = 0xff,
519 .bInterfaceSubClass = 0xff,
520 .bInterfaceProtocol = 0xff,
521 .driver_info = (unsigned long)&qmi_wwan_force_int4,
523 { /* ZTE (Vodafone) K4505-Z */
524 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_INT_INFO,
527 .bInterfaceClass = 0xff,
528 .bInterfaceSubClass = 0xff,
529 .bInterfaceProtocol = 0xff,
530 .driver_info = (unsigned long)&qmi_wwan_force_int4,
532 { /* Sierra Wireless MC77xx in QMI mode */
533 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_INT_INFO,
536 .bInterfaceClass = 0xff,
537 .bInterfaceSubClass = 0xff,
538 .bInterfaceProtocol = 0xff,
539 .driver_info = (unsigned long)&qmi_wwan_sierra,
541 {QMI_GOBI_DEVICE(0x05c6, 0x9212)}, /* Acer Gobi Modem Device */
542 {QMI_GOBI_DEVICE(0x03f0, 0x1f1d)}, /* HP un2400 Gobi Modem Device */
543 {QMI_GOBI_DEVICE(0x03f0, 0x371d)}, /* HP un2430 Mobile Broadband Module */
544 {QMI_GOBI_DEVICE(0x04da, 0x250d)}, /* Panasonic Gobi Modem device */
545 {QMI_GOBI_DEVICE(0x413c, 0x8172)}, /* Dell Gobi Modem device */
546 {QMI_GOBI_DEVICE(0x1410, 0xa001)}, /* Novatel Gobi Modem device */
547 {QMI_GOBI_DEVICE(0x0b05, 0x1776)}, /* Asus Gobi Modem device */
548 {QMI_GOBI_DEVICE(0x19d2, 0xfff3)}, /* ONDA Gobi Modem device */
549 {QMI_GOBI_DEVICE(0x05c6, 0x9001)}, /* Generic Gobi Modem device */
550 {QMI_GOBI_DEVICE(0x05c6, 0x9002)}, /* Generic Gobi Modem device */
551 {QMI_GOBI_DEVICE(0x05c6, 0x9202)}, /* Generic Gobi Modem device */
552 {QMI_GOBI_DEVICE(0x05c6, 0x9203)}, /* Generic Gobi Modem device */
553 {QMI_GOBI_DEVICE(0x05c6, 0x9222)}, /* Generic Gobi Modem device */
554 {QMI_GOBI_DEVICE(0x05c6, 0x9009)}, /* Generic Gobi Modem device */
555 {QMI_GOBI_DEVICE(0x413c, 0x8186)}, /* Dell Gobi 2000 Modem device (N0218, VU936) */
556 {QMI_GOBI_DEVICE(0x05c6, 0x920b)}, /* Generic Gobi 2000 Modem device */
557 {QMI_GOBI_DEVICE(0x05c6, 0x9225)}, /* Sony Gobi 2000 Modem device (N0279, VU730) */
558 {QMI_GOBI_DEVICE(0x05c6, 0x9245)}, /* Samsung Gobi 2000 Modem device (VL176) */
559 {QMI_GOBI_DEVICE(0x03f0, 0x251d)}, /* HP Gobi 2000 Modem device (VP412) */
560 {QMI_GOBI_DEVICE(0x05c6, 0x9215)}, /* Acer Gobi 2000 Modem device (VP413) */
561 {QMI_GOBI_DEVICE(0x05c6, 0x9265)}, /* Asus Gobi 2000 Modem device (VR305) */
562 {QMI_GOBI_DEVICE(0x05c6, 0x9235)}, /* Top Global Gobi 2000 Modem device (VR306) */
563 {QMI_GOBI_DEVICE(0x05c6, 0x9275)}, /* iRex Technologies Gobi 2000 Modem device (VR307) */
564 {QMI_GOBI_DEVICE(0x1199, 0x9001)}, /* Sierra Wireless Gobi 2000 Modem device (VT773) */
565 {QMI_GOBI_DEVICE(0x1199, 0x9002)}, /* Sierra Wireless Gobi 2000 Modem device (VT773) */
566 {QMI_GOBI_DEVICE(0x1199, 0x9003)}, /* Sierra Wireless Gobi 2000 Modem device (VT773) */
567 {QMI_GOBI_DEVICE(0x1199, 0x9004)}, /* Sierra Wireless Gobi 2000 Modem device (VT773) */
568 {QMI_GOBI_DEVICE(0x1199, 0x9005)}, /* Sierra Wireless Gobi 2000 Modem device (VT773) */
569 {QMI_GOBI_DEVICE(0x1199, 0x9006)}, /* Sierra Wireless Gobi 2000 Modem device (VT773) */
570 {QMI_GOBI_DEVICE(0x1199, 0x9007)}, /* Sierra Wireless Gobi 2000 Modem device (VT773) */
571 {QMI_GOBI_DEVICE(0x1199, 0x9008)}, /* Sierra Wireless Gobi 2000 Modem device (VT773) */
572 {QMI_GOBI_DEVICE(0x1199, 0x9009)}, /* Sierra Wireless Gobi 2000 Modem device (VT773) */
573 {QMI_GOBI_DEVICE(0x1199, 0x900a)}, /* Sierra Wireless Gobi 2000 Modem device (VT773) */
574 {QMI_GOBI_DEVICE(0x1199, 0x9011)}, /* Sierra Wireless Gobi 2000 Modem device (MC8305) */
575 {QMI_GOBI_DEVICE(0x16d8, 0x8002)}, /* CMDTech Gobi 2000 Modem device (VU922) */
576 {QMI_GOBI_DEVICE(0x05c6, 0x9205)}, /* Gobi 2000 Modem device */
577 {QMI_GOBI_DEVICE(0x1199, 0x9013)}, /* Sierra Wireless Gobi 3000 Modem device (MC8355) */
578 {QMI_GOBI_DEVICE(0x1199, 0x9015)}, /* Sierra Wireless Gobi 3000 Modem device */
579 {QMI_GOBI_DEVICE(0x1199, 0x9019)}, /* Sierra Wireless Gobi 3000 Modem device */
582 MODULE_DEVICE_TABLE(usb, products);
584 static struct usb_driver qmi_wwan_driver = {
586 .id_table = products,
587 .probe = usbnet_probe,
588 .disconnect = usbnet_disconnect,
589 .suspend = qmi_wwan_suspend,
590 .resume = qmi_wwan_resume,
591 .reset_resume = qmi_wwan_resume,
592 .supports_autosuspend = 1,
595 static int __init qmi_wwan_init(void)
597 return usb_register(&qmi_wwan_driver);
599 module_init(qmi_wwan_init);
601 static void __exit qmi_wwan_exit(void)
603 usb_deregister(&qmi_wwan_driver);
605 module_exit(qmi_wwan_exit);
607 MODULE_AUTHOR("Bjørn Mork <bjorn@mork.no>");
608 MODULE_DESCRIPTION("Qualcomm MSM Interface (QMI) WWAN driver");
609 MODULE_LICENSE("GPL");