aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/usb/qmi_wwan.c
diff options
context:
space:
mode:
authorBjørn Mork <bjorn@mork.no>2012-01-19 10:37:22 -0500
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2012-03-09 16:09:17 -0500
commit423ce8caab7ea2b13f4a29ce0839369528aafaeb (patch)
tree40c93b8b65c4a8beff83c565d14eccc9bb658c0d /drivers/net/usb/qmi_wwan.c
parent47594d5528f28a4c025c2955c68104c75815637c (diff)
net: usb: qmi_wwan: New driver for Huawei QMI based WWAN devices
Some WWAN LTE/3G devices based on chipsets from Qualcomm provide near standard CDC ECM interfaces in addition to the usual serial interfaces. The Huawei E392/E398 are examples of such devices. These typically cannot be fully configured using AT commands over a serial interface. It is necessary to speak the proprietary Qualcomm MSM Interface (QMI) protocol to the device to enable the ethernet proxy functionality. The devices embed the QMI protocol in CDC on the control interface, using standard CDC commands and notifications. The do not otherwise use CDC commands for the ethernet function. This driver does therefore not need access to any other aspects of the control interface than the descriptors attached to it. Another driver, cdc-wdm, will provide userspace access to the QMI protocol independently of this driver. To facilitate this, this driver avoids binding to the control interface, and uses only the associated data interface after parsing the common CDC functional descriptors on the control interface. You will want both the cdc-wdm and option drivers as companions to this driver, to have full access to all interfaces and protocols exported by the device. Signed-off-by: Bjørn Mork <bjorn@mork.no> Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'drivers/net/usb/qmi_wwan.c')
-rw-r--r--drivers/net/usb/qmi_wwan.c228
1 files changed, 228 insertions, 0 deletions
diff --git a/drivers/net/usb/qmi_wwan.c b/drivers/net/usb/qmi_wwan.c
new file mode 100644
index 000000000000..739e6de7abcb
--- /dev/null
+++ b/drivers/net/usb/qmi_wwan.c
@@ -0,0 +1,228 @@
1/*
2 * Copyright (c) 2012 Bjørn Mork <bjorn@mork.no>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * version 2 as published by the Free Software Foundation.
7 */
8
9#include <linux/module.h>
10#include <linux/netdevice.h>
11#include <linux/ethtool.h>
12#include <linux/mii.h>
13#include <linux/usb.h>
14#include <linux/usb/cdc.h>
15#include <linux/usb/usbnet.h>
16
17/* The name of the CDC Device Management driver */
18#define DM_DRIVER "cdc_wdm"
19
20/*
21 * This driver supports wwan (3G/LTE/?) devices using a vendor
22 * specific management protocol called Qualcomm MSM Interface (QMI) -
23 * in addition to the more common AT commands over serial interface
24 * management
25 *
26 * QMI is wrapped in CDC, using CDC encapsulated commands on the
27 * control ("master") interface of a two-interface CDC Union
28 * resembling standard CDC ECM. The devices do not use the control
29 * interface for any other CDC messages. Most likely because the
30 * management protocol is used in place of the standard CDC
31 * notifications NOTIFY_NETWORK_CONNECTION and NOTIFY_SPEED_CHANGE
32 *
33 * Handling a protocol like QMI is out of the scope for any driver.
34 * It can be exported as a character device using the cdc-wdm driver,
35 * which will enable userspace applications ("modem managers") to
36 * handle it. This may be required to use the network interface
37 * provided by the driver.
38 *
39 * These devices may alternatively/additionally be configured using AT
40 * commands on any of the serial interfaces driven by the option driver
41 *
42 * This driver binds only to the data ("slave") interface to enable
43 * the cdc-wdm driver to bind to the control interface. It still
44 * parses the CDC functional descriptors on the control interface to
45 * a) verify that this is indeed a handled interface (CDC Union
46 * header lists it as slave)
47 * b) get MAC address and other ethernet config from the CDC Ethernet
48 * header
49 * c) enable user bind requests against the control interface, which
50 * is the common way to bind to CDC Ethernet Control Model type
51 * interfaces
52 * d) provide a hint to the user about which interface is the
53 * corresponding management interface
54 */
55
56static int qmi_wwan_bind(struct usbnet *dev, struct usb_interface *intf)
57{
58 int status = -1;
59 struct usb_interface *control = NULL;
60 u8 *buf = intf->cur_altsetting->extra;
61 int len = intf->cur_altsetting->extralen;
62 struct usb_interface_descriptor *desc = &intf->cur_altsetting->desc;
63 struct usb_cdc_union_desc *cdc_union = NULL;
64 struct usb_cdc_ether_desc *cdc_ether = NULL;
65 u32 required = 1 << USB_CDC_HEADER_TYPE | 1 << USB_CDC_UNION_TYPE;
66 u32 found = 0;
67
68 /*
69 * assume a data interface has no additional descriptors and
70 * that the control and data interface are numbered
71 * consecutively - this holds for the Huawei device at least
72 */
73 if (len == 0 && desc->bInterfaceNumber > 0) {
74 control = usb_ifnum_to_if(dev->udev, desc->bInterfaceNumber - 1);
75 if (!control)
76 goto err;
77
78 buf = control->cur_altsetting->extra;
79 len = control->cur_altsetting->extralen;
80 dev_dbg(&intf->dev, "guessing \"control\" => %s, \"data\" => this\n",
81 dev_name(&control->dev));
82 }
83
84 while (len > 3) {
85 struct usb_descriptor_header *h = (void *)buf;
86
87 /* ignore any misplaced descriptors */
88 if (h->bDescriptorType != USB_DT_CS_INTERFACE)
89 goto next_desc;
90
91 /* buf[2] is CDC descriptor subtype */
92 switch (buf[2]) {
93 case USB_CDC_HEADER_TYPE:
94 if (found & 1 << USB_CDC_HEADER_TYPE) {
95 dev_dbg(&intf->dev, "extra CDC header\n");
96 goto err;
97 }
98 if (h->bLength != sizeof(struct usb_cdc_header_desc)) {
99 dev_dbg(&intf->dev, "CDC header len %u\n", h->bLength);
100 goto err;
101 }
102 break;
103 case USB_CDC_UNION_TYPE:
104 if (found & 1 << USB_CDC_UNION_TYPE) {
105 dev_dbg(&intf->dev, "extra CDC union\n");
106 goto err;
107 }
108 if (h->bLength != sizeof(struct usb_cdc_union_desc)) {
109 dev_dbg(&intf->dev, "CDC union len %u\n", h->bLength);
110 goto err;
111 }
112 cdc_union = (struct usb_cdc_union_desc *)buf;
113 break;
114 case USB_CDC_ETHERNET_TYPE:
115 if (found & 1 << USB_CDC_ETHERNET_TYPE) {
116 dev_dbg(&intf->dev, "extra CDC ether\n");
117 goto err;
118 }
119 if (h->bLength != sizeof(struct usb_cdc_ether_desc)) {
120 dev_dbg(&intf->dev, "CDC ether len %u\n", h->bLength);
121 goto err;
122 }
123 cdc_ether = (struct usb_cdc_ether_desc *)buf;
124 break;
125 }
126
127 /*
128 * Remember which CDC functional descriptors we've seen. Works
129 * for all types we care about, of which USB_CDC_ETHERNET_TYPE
130 * (0x0f) is the highest numbered
131 */
132 if (buf[2] < 32)
133 found |= 1 << buf[2];
134
135next_desc:
136 len -= h->bLength;
137 buf += h->bLength;
138 }
139
140 /* did we find all the required ones? */
141 if ((found & required) != required) {
142 dev_err(&intf->dev, "CDC functional descriptors missing\n");
143 goto err;
144 }
145
146 /* give the user a helpful hint if trying to bind to the wrong interface */
147 if (cdc_union && desc->bInterfaceNumber == cdc_union->bMasterInterface0) {
148 dev_err(&intf->dev, "leaving \"control\" interface for " DM_DRIVER " - try binding to %s instead!\n",
149 dev_name(&usb_ifnum_to_if(dev->udev, cdc_union->bSlaveInterface0)->dev));
150 goto err;
151 }
152
153 /* errors aren't fatal - we can live with the dynamic address */
154 if (cdc_ether) {
155 dev->hard_mtu = le16_to_cpu(cdc_ether->wMaxSegmentSize);
156 usbnet_get_ethernet_addr(dev, cdc_ether->iMACAddress);
157 }
158
159 /* success! point the user to the management interface */
160 if (control)
161 dev_info(&intf->dev, "Use \"" DM_DRIVER "\" for QMI interface %s\n",
162 dev_name(&control->dev));
163
164 /* XXX: add a sysfs symlink somewhere to help management applications find it? */
165
166 /* collect bulk endpoints now that we know intf == "data" interface */
167 status = usbnet_get_endpoints(dev, intf);
168
169err:
170 return status;
171}
172
173/* stolen from cdc_ether.c */
174static int qmi_wwan_manage_power(struct usbnet *dev, int on)
175{
176 dev->intf->needs_remote_wakeup = on;
177 return 0;
178}
179
180static const struct driver_info qmi_wwan_info = {
181 .description = "QMI speaking wwan device",
182 .flags = FLAG_WWAN,
183 .bind = qmi_wwan_bind,
184 .manage_power = qmi_wwan_manage_power,
185};
186
187#define HUAWEI_VENDOR_ID 0x12D1
188
189static const struct usb_device_id products[] = {
190{
191 /* Huawei E392, E398 and possibly others sharing both device id and more... */
192 .match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_INT_INFO,
193 .idVendor = HUAWEI_VENDOR_ID,
194 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
195 .bInterfaceSubClass = 1,
196 .bInterfaceProtocol = 8, /* NOTE: This is the *slave* interface of the CDC Union! */
197 .driver_info = (unsigned long)&qmi_wwan_info,
198}, {
199}, /* END */
200};
201MODULE_DEVICE_TABLE(usb, products);
202
203static struct usb_driver qmi_wwan_driver = {
204 .name = "qmi_wwan",
205 .id_table = products,
206 .probe = usbnet_probe,
207 .disconnect = usbnet_disconnect,
208 .suspend = usbnet_suspend,
209 .resume = usbnet_resume,
210 .reset_resume = usbnet_resume,
211 .supports_autosuspend = 1,
212};
213
214static int __init qmi_wwan_init(void)
215{
216 return usb_register(&qmi_wwan_driver);
217}
218module_init(qmi_wwan_init);
219
220static void __exit qmi_wwan_exit(void)
221{
222 usb_deregister(&qmi_wwan_driver);
223}
224module_exit(qmi_wwan_exit);
225
226MODULE_AUTHOR("Bjørn Mork <bjorn@mork.no>");
227MODULE_DESCRIPTION("Qualcomm MSM Interface (QMI) WWAN driver");
228MODULE_LICENSE("GPL");