|
| 1 | +/* |
| 2 | + * Copyright (c) 2012 Bjørn Mork <[email protected]> |
| 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 | + |
| 56 | +static 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 | + |
| 135 | +next_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 | + |
| 169 | +err: |
| 170 | + return status; |
| 171 | +} |
| 172 | + |
| 173 | +/* stolen from cdc_ether.c */ |
| 174 | +static int qmi_wwan_manage_power(struct usbnet *dev, int on) |
| 175 | +{ |
| 176 | + dev->intf->needs_remote_wakeup = on; |
| 177 | + return 0; |
| 178 | +} |
| 179 | + |
| 180 | +static 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 | + |
| 189 | +static 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 | +}; |
| 201 | +MODULE_DEVICE_TABLE(usb, products); |
| 202 | + |
| 203 | +static 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 | + |
| 214 | +static int __init qmi_wwan_init(void) |
| 215 | +{ |
| 216 | + return usb_register(&qmi_wwan_driver); |
| 217 | +} |
| 218 | +module_init(qmi_wwan_init); |
| 219 | + |
| 220 | +static void __exit qmi_wwan_exit(void) |
| 221 | +{ |
| 222 | + usb_deregister(&qmi_wwan_driver); |
| 223 | +} |
| 224 | +module_exit(qmi_wwan_exit); |
| 225 | + |
| 226 | +MODULE_AUTHOR( "Bjørn Mork <[email protected]>"); |
| 227 | +MODULE_DESCRIPTION("Qualcomm MSM Interface (QMI) WWAN driver"); |
| 228 | +MODULE_LICENSE("GPL"); |
0 commit comments