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
);
79 /* can be called while disconnecting */
82 return qmi_wwan_manage_power(dev
, on
);
85 /* collect all three endpoints and register subdriver */
86 static int qmi_wwan_register_subdriver(struct usbnet
*dev
)
89 struct usb_driver
*subdriver
= NULL
;
90 struct qmi_wwan_state
*info
= (void *)&dev
->data
;
92 /* collect bulk endpoints */
93 rv
= usbnet_get_endpoints(dev
, info
->data
);
97 /* update status endpoint if separate control interface */
98 if (info
->control
!= info
->data
)
99 dev
->status
= &info
->control
->cur_altsetting
->endpoint
[0];
101 /* require interrupt endpoint for subdriver */
107 /* for subdriver power management */
108 atomic_set(&info
->pmcount
, 0);
110 /* register subdriver */
111 subdriver
= usb_cdc_wdm_register(info
->control
, &dev
->status
->desc
, 4096, &qmi_wwan_cdc_wdm_manage_power
);
112 if (IS_ERR(subdriver
)) {
113 dev_err(&info
->control
->dev
, "subdriver registration failed\n");
114 rv
= PTR_ERR(subdriver
);
118 /* prevent usbnet from using status endpoint */
121 /* save subdriver struct for suspend/resume wrappers */
122 info
->subdriver
= subdriver
;
128 static int qmi_wwan_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
131 u8
*buf
= intf
->cur_altsetting
->extra
;
132 int len
= intf
->cur_altsetting
->extralen
;
133 struct usb_interface_descriptor
*desc
= &intf
->cur_altsetting
->desc
;
134 struct usb_cdc_union_desc
*cdc_union
= NULL
;
135 struct usb_cdc_ether_desc
*cdc_ether
= NULL
;
137 struct usb_driver
*driver
= driver_of(intf
);
138 struct qmi_wwan_state
*info
= (void *)&dev
->data
;
140 BUILD_BUG_ON((sizeof(((struct usbnet
*)0)->data
) < sizeof(struct qmi_wwan_state
)));
142 /* control and data is shared? */
143 if (intf
->cur_altsetting
->desc
.bNumEndpoints
== 3) {
144 info
->control
= intf
;
149 /* else require a single interrupt status endpoint on control intf */
150 if (intf
->cur_altsetting
->desc
.bNumEndpoints
!= 1)
153 /* and a number of CDC descriptors */
155 struct usb_descriptor_header
*h
= (void *)buf
;
157 /* ignore any misplaced descriptors */
158 if (h
->bDescriptorType
!= USB_DT_CS_INTERFACE
)
161 /* buf[2] is CDC descriptor subtype */
163 case USB_CDC_HEADER_TYPE
:
164 if (found
& 1 << USB_CDC_HEADER_TYPE
) {
165 dev_dbg(&intf
->dev
, "extra CDC header\n");
168 if (h
->bLength
!= sizeof(struct usb_cdc_header_desc
)) {
169 dev_dbg(&intf
->dev
, "CDC header len %u\n", h
->bLength
);
173 case USB_CDC_UNION_TYPE
:
174 if (found
& 1 << USB_CDC_UNION_TYPE
) {
175 dev_dbg(&intf
->dev
, "extra CDC union\n");
178 if (h
->bLength
!= sizeof(struct usb_cdc_union_desc
)) {
179 dev_dbg(&intf
->dev
, "CDC union len %u\n", h
->bLength
);
182 cdc_union
= (struct usb_cdc_union_desc
*)buf
;
184 case USB_CDC_ETHERNET_TYPE
:
185 if (found
& 1 << USB_CDC_ETHERNET_TYPE
) {
186 dev_dbg(&intf
->dev
, "extra CDC ether\n");
189 if (h
->bLength
!= sizeof(struct usb_cdc_ether_desc
)) {
190 dev_dbg(&intf
->dev
, "CDC ether len %u\n", h
->bLength
);
193 cdc_ether
= (struct usb_cdc_ether_desc
*)buf
;
198 * Remember which CDC functional descriptors we've seen. Works
199 * for all types we care about, of which USB_CDC_ETHERNET_TYPE
200 * (0x0f) is the highest numbered
203 found
|= 1 << buf
[2];
210 /* did we find all the required ones? */
211 if (!(found
& (1 << USB_CDC_HEADER_TYPE
)) ||
212 !(found
& (1 << USB_CDC_UNION_TYPE
))) {
213 dev_err(&intf
->dev
, "CDC functional descriptors missing\n");
217 /* verify CDC Union */
218 if (desc
->bInterfaceNumber
!= cdc_union
->bMasterInterface0
) {
219 dev_err(&intf
->dev
, "bogus CDC Union: master=%u\n", cdc_union
->bMasterInterface0
);
223 /* need to save these for unbind */
224 info
->control
= intf
;
225 info
->data
= usb_ifnum_to_if(dev
->udev
, cdc_union
->bSlaveInterface0
);
227 dev_err(&intf
->dev
, "bogus CDC Union: slave=%u\n", cdc_union
->bSlaveInterface0
);
231 /* errors aren't fatal - we can live with the dynamic address */
233 dev
->hard_mtu
= le16_to_cpu(cdc_ether
->wMaxSegmentSize
);
234 usbnet_get_ethernet_addr(dev
, cdc_ether
->iMACAddress
);
237 /* claim data interface and set it up */
238 status
= usb_driver_claim_interface(driver
, info
->data
, dev
);
243 status
= qmi_wwan_register_subdriver(dev
);
244 if (status
< 0 && info
->control
!= info
->data
) {
245 usb_set_intfdata(info
->data
, NULL
);
246 usb_driver_release_interface(driver
, info
->data
);
253 static void qmi_wwan_unbind(struct usbnet
*dev
, struct usb_interface
*intf
)
255 struct qmi_wwan_state
*info
= (void *)&dev
->data
;
256 struct usb_driver
*driver
= driver_of(intf
);
257 struct usb_interface
*other
;
259 if (info
->subdriver
&& info
->subdriver
->disconnect
)
260 info
->subdriver
->disconnect(info
->control
);
262 /* allow user to unbind using either control or data */
263 if (intf
== info
->control
)
266 other
= info
->control
;
268 /* only if not shared */
269 if (other
&& intf
!= other
) {
270 usb_set_intfdata(other
, NULL
);
271 usb_driver_release_interface(driver
, other
);
274 info
->subdriver
= NULL
;
276 info
->control
= NULL
;
279 /* suspend/resume wrappers calling both usbnet and the cdc-wdm
280 * subdriver if present.
282 * NOTE: cdc-wdm also supports pre/post_reset, but we cannot provide
283 * wrappers for those without adding usbnet reset support first.
285 static int qmi_wwan_suspend(struct usb_interface
*intf
, pm_message_t message
)
287 struct usbnet
*dev
= usb_get_intfdata(intf
);
288 struct qmi_wwan_state
*info
= (void *)&dev
->data
;
291 ret
= usbnet_suspend(intf
, message
);
295 if (intf
== info
->control
&& info
->subdriver
&& info
->subdriver
->suspend
)
296 ret
= info
->subdriver
->suspend(intf
, message
);
303 static int qmi_wwan_resume(struct usb_interface
*intf
)
305 struct usbnet
*dev
= usb_get_intfdata(intf
);
306 struct qmi_wwan_state
*info
= (void *)&dev
->data
;
308 bool callsub
= (intf
== info
->control
&& info
->subdriver
&& info
->subdriver
->resume
);
311 ret
= info
->subdriver
->resume(intf
);
314 ret
= usbnet_resume(intf
);
315 if (ret
< 0 && callsub
&& info
->subdriver
->suspend
)
316 info
->subdriver
->suspend(intf
, PMSG_SUSPEND
);
321 static const struct driver_info qmi_wwan_info
= {
322 .description
= "WWAN/QMI device",
324 .bind
= qmi_wwan_bind
,
325 .unbind
= qmi_wwan_unbind
,
326 .manage_power
= qmi_wwan_manage_power
,
329 #define HUAWEI_VENDOR_ID 0x12D1
331 /* map QMI/wwan function by a fixed interface number */
332 #define QMI_FIXED_INTF(vend, prod, num) \
333 USB_DEVICE_INTERFACE_NUMBER(vend, prod, num), \
334 .driver_info = (unsigned long)&qmi_wwan_info
336 /* Gobi 1000 QMI/wwan interface number is 3 according to qcserial */
337 #define QMI_GOBI1K_DEVICE(vend, prod) \
338 QMI_FIXED_INTF(vend, prod, 3)
340 /* Gobi 2000/3000 QMI/wwan interface number is 0 according to qcserial */
341 #define QMI_GOBI_DEVICE(vend, prod) \
342 QMI_FIXED_INTF(vend, prod, 0)
344 static const struct usb_device_id products
[] = {
345 /* 1. CDC ECM like devices match on the control interface */
346 { /* Huawei E392, E398 and possibly others sharing both device id and more... */
347 USB_VENDOR_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID
, USB_CLASS_VENDOR_SPEC
, 1, 9),
348 .driver_info
= (unsigned long)&qmi_wwan_info
,
350 { /* Vodafone/Huawei K5005 (12d1:14c8) and similar modems */
351 USB_VENDOR_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID
, USB_CLASS_VENDOR_SPEC
, 1, 57),
352 .driver_info
= (unsigned long)&qmi_wwan_info
,
355 /* 2. Combined interface devices matching on class+protocol */
356 { /* Huawei E392, E398 and possibly others in "Windows mode" */
357 USB_VENDOR_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID
, USB_CLASS_VENDOR_SPEC
, 1, 17),
358 .driver_info
= (unsigned long)&qmi_wwan_info
,
360 { /* Pantech UML290 */
361 USB_DEVICE_AND_INTERFACE_INFO(0x106c, 0x3718, USB_CLASS_VENDOR_SPEC
, 0xf0, 0xff),
362 .driver_info
= (unsigned long)&qmi_wwan_info
,
364 { /* Pantech UML290 - newer firmware */
365 USB_DEVICE_AND_INTERFACE_INFO(0x106c, 0x3718, USB_CLASS_VENDOR_SPEC
, 0xf1, 0xff),
366 .driver_info
= (unsigned long)&qmi_wwan_info
,
369 /* 3. Combined interface devices matching on interface number */
370 {QMI_FIXED_INTF(0x19d2, 0x0055, 1)}, /* ZTE (Vodafone) K3520-Z */
371 {QMI_FIXED_INTF(0x19d2, 0x0063, 4)}, /* ZTE (Vodafone) K3565-Z */
372 {QMI_FIXED_INTF(0x19d2, 0x0104, 4)}, /* ZTE (Vodafone) K4505-Z */
373 {QMI_FIXED_INTF(0x19d2, 0x0167, 4)}, /* ZTE MF820D */
374 {QMI_FIXED_INTF(0x19d2, 0x0326, 4)}, /* ZTE MF821D */
375 {QMI_FIXED_INTF(0x19d2, 0x1008, 4)}, /* ZTE (Vodafone) K3570-Z */
376 {QMI_FIXED_INTF(0x19d2, 0x1010, 4)}, /* ZTE (Vodafone) K3571-Z */
377 {QMI_FIXED_INTF(0x19d2, 0x1018, 3)}, /* ZTE (Vodafone) K5006-Z */
378 {QMI_FIXED_INTF(0x19d2, 0x1402, 2)}, /* ZTE MF60 */
379 {QMI_FIXED_INTF(0x19d2, 0x2002, 4)}, /* ZTE (Vodafone) K3765-Z */
380 {QMI_FIXED_INTF(0x0f3d, 0x68a2, 8)}, /* Sierra Wireless MC7700 */
381 {QMI_FIXED_INTF(0x114f, 0x68a2, 8)}, /* Sierra Wireless MC7750 */
382 {QMI_FIXED_INTF(0x1199, 0x68a2, 8)}, /* Sierra Wireless MC7710 in QMI mode */
383 {QMI_FIXED_INTF(0x1199, 0x68a2, 19)}, /* Sierra Wireless MC7710 in QMI mode */
384 {QMI_FIXED_INTF(0x1199, 0x901c, 8)}, /* Sierra Wireless EM7700 */
386 /* 4. Gobi 1000 devices */
387 {QMI_GOBI1K_DEVICE(0x05c6, 0x9212)}, /* Acer Gobi Modem Device */
388 {QMI_GOBI1K_DEVICE(0x03f0, 0x1f1d)}, /* HP un2400 Gobi Modem Device */
389 {QMI_GOBI1K_DEVICE(0x04da, 0x250d)}, /* Panasonic Gobi Modem device */
390 {QMI_GOBI1K_DEVICE(0x413c, 0x8172)}, /* Dell Gobi Modem device */
391 {QMI_GOBI1K_DEVICE(0x1410, 0xa001)}, /* Novatel Gobi Modem device */
392 {QMI_GOBI1K_DEVICE(0x0b05, 0x1776)}, /* Asus Gobi Modem device */
393 {QMI_GOBI1K_DEVICE(0x19d2, 0xfff3)}, /* ONDA Gobi Modem device */
394 {QMI_GOBI1K_DEVICE(0x05c6, 0x9001)}, /* Generic Gobi Modem device */
395 {QMI_GOBI1K_DEVICE(0x05c6, 0x9002)}, /* Generic Gobi Modem device */
396 {QMI_GOBI1K_DEVICE(0x05c6, 0x9202)}, /* Generic Gobi Modem device */
397 {QMI_GOBI1K_DEVICE(0x05c6, 0x9203)}, /* Generic Gobi Modem device */
398 {QMI_GOBI1K_DEVICE(0x05c6, 0x9222)}, /* Generic Gobi Modem device */
399 {QMI_GOBI1K_DEVICE(0x05c6, 0x9009)}, /* Generic Gobi Modem device */
401 /* 5. Gobi 2000 and 3000 devices */
402 {QMI_GOBI_DEVICE(0x413c, 0x8186)}, /* Dell Gobi 2000 Modem device (N0218, VU936) */
403 {QMI_GOBI_DEVICE(0x413c, 0x8194)}, /* Dell Gobi 3000 Composite */
404 {QMI_GOBI_DEVICE(0x05c6, 0x920b)}, /* Generic Gobi 2000 Modem device */
405 {QMI_GOBI_DEVICE(0x05c6, 0x920d)}, /* Gobi 3000 Composite */
406 {QMI_GOBI_DEVICE(0x05c6, 0x9225)}, /* Sony Gobi 2000 Modem device (N0279, VU730) */
407 {QMI_GOBI_DEVICE(0x05c6, 0x9245)}, /* Samsung Gobi 2000 Modem device (VL176) */
408 {QMI_GOBI_DEVICE(0x03f0, 0x251d)}, /* HP Gobi 2000 Modem device (VP412) */
409 {QMI_GOBI_DEVICE(0x05c6, 0x9215)}, /* Acer Gobi 2000 Modem device (VP413) */
410 {QMI_GOBI_DEVICE(0x05c6, 0x9265)}, /* Asus Gobi 2000 Modem device (VR305) */
411 {QMI_GOBI_DEVICE(0x05c6, 0x9235)}, /* Top Global Gobi 2000 Modem device (VR306) */
412 {QMI_GOBI_DEVICE(0x05c6, 0x9275)}, /* iRex Technologies Gobi 2000 Modem device (VR307) */
413 {QMI_GOBI_DEVICE(0x1199, 0x68a5)}, /* Sierra Wireless Modem */
414 {QMI_GOBI_DEVICE(0x1199, 0x68a9)}, /* Sierra Wireless Modem */
415 {QMI_GOBI_DEVICE(0x1199, 0x9001)}, /* Sierra Wireless Gobi 2000 Modem device (VT773) */
416 {QMI_GOBI_DEVICE(0x1199, 0x9002)}, /* Sierra Wireless Gobi 2000 Modem device (VT773) */
417 {QMI_GOBI_DEVICE(0x1199, 0x9003)}, /* Sierra Wireless Gobi 2000 Modem device (VT773) */
418 {QMI_GOBI_DEVICE(0x1199, 0x9004)}, /* Sierra Wireless Gobi 2000 Modem device (VT773) */
419 {QMI_GOBI_DEVICE(0x1199, 0x9005)}, /* Sierra Wireless Gobi 2000 Modem device (VT773) */
420 {QMI_GOBI_DEVICE(0x1199, 0x9006)}, /* Sierra Wireless Gobi 2000 Modem device (VT773) */
421 {QMI_GOBI_DEVICE(0x1199, 0x9007)}, /* Sierra Wireless Gobi 2000 Modem device (VT773) */
422 {QMI_GOBI_DEVICE(0x1199, 0x9008)}, /* Sierra Wireless Gobi 2000 Modem device (VT773) */
423 {QMI_GOBI_DEVICE(0x1199, 0x9009)}, /* Sierra Wireless Gobi 2000 Modem device (VT773) */
424 {QMI_GOBI_DEVICE(0x1199, 0x900a)}, /* Sierra Wireless Gobi 2000 Modem device (VT773) */
425 {QMI_GOBI_DEVICE(0x1199, 0x9011)}, /* Sierra Wireless Gobi 2000 Modem device (MC8305) */
426 {QMI_FIXED_INTF(0x1199, 0x9011, 5)}, /* alternate interface number!? */
427 {QMI_GOBI_DEVICE(0x16d8, 0x8002)}, /* CMDTech Gobi 2000 Modem device (VU922) */
428 {QMI_GOBI_DEVICE(0x05c6, 0x9205)}, /* Gobi 2000 Modem device */
429 {QMI_GOBI_DEVICE(0x1199, 0x9013)}, /* Sierra Wireless Gobi 3000 Modem device (MC8355) */
430 {QMI_GOBI_DEVICE(0x03f0, 0x371d)}, /* HP un2430 Mobile Broadband Module */
431 {QMI_GOBI_DEVICE(0x1199, 0x9015)}, /* Sierra Wireless Gobi 3000 Modem device */
432 {QMI_GOBI_DEVICE(0x1199, 0x9019)}, /* Sierra Wireless Gobi 3000 Modem device */
433 {QMI_GOBI_DEVICE(0x1199, 0x901b)}, /* Sierra Wireless MC7770 */
434 {QMI_GOBI_DEVICE(0x12d1, 0x14f1)}, /* Sony Gobi 3000 Composite */
435 {QMI_GOBI_DEVICE(0x1410, 0xa021)}, /* Foxconn Gobi 3000 Modem device (Novatel E396) */
439 MODULE_DEVICE_TABLE(usb
, products
);
441 static int qmi_wwan_probe(struct usb_interface
*intf
, const struct usb_device_id
*prod
)
443 struct usb_device_id
*id
= (struct usb_device_id
*)prod
;
445 /* Workaround to enable dynamic IDs. This disables usbnet
446 * blacklisting functionality. Which, if required, can be
447 * reimplemented here by using a magic "blacklist" value
448 * instead of 0 in the static device id table
450 if (!id
->driver_info
) {
451 dev_dbg(&intf
->dev
, "setting defaults for dynamic device id\n");
452 id
->driver_info
= (unsigned long)&qmi_wwan_info
;
455 return usbnet_probe(intf
, id
);
458 static struct usb_driver qmi_wwan_driver
= {
460 .id_table
= products
,
461 .probe
= qmi_wwan_probe
,
462 .disconnect
= usbnet_disconnect
,
463 .suspend
= qmi_wwan_suspend
,
464 .resume
= qmi_wwan_resume
,
465 .reset_resume
= qmi_wwan_resume
,
466 .supports_autosuspend
= 1,
467 .disable_hub_initiated_lpm
= 1,
470 module_usb_driver(qmi_wwan_driver
);
472 MODULE_AUTHOR("Bjørn Mork <bjorn@mork.no>");
473 MODULE_DESCRIPTION("Qualcomm MSM Interface (QMI) WWAN driver");
474 MODULE_LICENSE("GPL");