qmi_wwan.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. #include <linux/module.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/ethtool.h>
  11. #include <linux/mii.h>
  12. #include <linux/usb.h>
  13. #include <linux/usb/cdc.h>
  14. #include <linux/usb/usbnet.h>
  15. #include <linux/usb/cdc-wdm.h>
  16. /* The name of the CDC Device Management driver */
  17. #define DM_DRIVER "cdc_wdm"
  18. /*
  19. * This driver supports wwan (3G/LTE/?) devices using a vendor
  20. * specific management protocol called Qualcomm MSM Interface (QMI) -
  21. * in addition to the more common AT commands over serial interface
  22. * management
  23. *
  24. * QMI is wrapped in CDC, using CDC encapsulated commands on the
  25. * control ("master") interface of a two-interface CDC Union
  26. * resembling standard CDC ECM. The devices do not use the control
  27. * interface for any other CDC messages. Most likely because the
  28. * management protocol is used in place of the standard CDC
  29. * notifications NOTIFY_NETWORK_CONNECTION and NOTIFY_SPEED_CHANGE
  30. *
  31. * Handling a protocol like QMI is out of the scope for any driver.
  32. * It can be exported as a character device using the cdc-wdm driver,
  33. * which will enable userspace applications ("modem managers") to
  34. * handle it. This may be required to use the network interface
  35. * provided by the driver.
  36. *
  37. * These devices may alternatively/additionally be configured using AT
  38. * commands on any of the serial interfaces driven by the option driver
  39. *
  40. * This driver binds only to the data ("slave") interface to enable
  41. * the cdc-wdm driver to bind to the control interface. It still
  42. * parses the CDC functional descriptors on the control interface to
  43. * a) verify that this is indeed a handled interface (CDC Union
  44. * header lists it as slave)
  45. * b) get MAC address and other ethernet config from the CDC Ethernet
  46. * header
  47. * c) enable user bind requests against the control interface, which
  48. * is the common way to bind to CDC Ethernet Control Model type
  49. * interfaces
  50. * d) provide a hint to the user about which interface is the
  51. * corresponding management interface
  52. */
  53. static int qmi_wwan_bind(struct usbnet *dev, struct usb_interface *intf)
  54. {
  55. int status = -1;
  56. struct usb_interface *control = NULL;
  57. u8 *buf = intf->cur_altsetting->extra;
  58. int len = intf->cur_altsetting->extralen;
  59. struct usb_interface_descriptor *desc = &intf->cur_altsetting->desc;
  60. struct usb_cdc_union_desc *cdc_union = NULL;
  61. struct usb_cdc_ether_desc *cdc_ether = NULL;
  62. u32 required = 1 << USB_CDC_HEADER_TYPE | 1 << USB_CDC_UNION_TYPE;
  63. u32 found = 0;
  64. atomic_t *pmcount = (void *)&dev->data[1];
  65. atomic_set(pmcount, 0);
  66. /*
  67. * assume a data interface has no additional descriptors and
  68. * that the control and data interface are numbered
  69. * consecutively - this holds for the Huawei device at least
  70. */
  71. if (len == 0 && desc->bInterfaceNumber > 0) {
  72. control = usb_ifnum_to_if(dev->udev, desc->bInterfaceNumber - 1);
  73. if (!control)
  74. goto err;
  75. buf = control->cur_altsetting->extra;
  76. len = control->cur_altsetting->extralen;
  77. dev_dbg(&intf->dev, "guessing \"control\" => %s, \"data\" => this\n",
  78. dev_name(&control->dev));
  79. }
  80. while (len > 3) {
  81. struct usb_descriptor_header *h = (void *)buf;
  82. /* ignore any misplaced descriptors */
  83. if (h->bDescriptorType != USB_DT_CS_INTERFACE)
  84. goto next_desc;
  85. /* buf[2] is CDC descriptor subtype */
  86. switch (buf[2]) {
  87. case USB_CDC_HEADER_TYPE:
  88. if (found & 1 << USB_CDC_HEADER_TYPE) {
  89. dev_dbg(&intf->dev, "extra CDC header\n");
  90. goto err;
  91. }
  92. if (h->bLength != sizeof(struct usb_cdc_header_desc)) {
  93. dev_dbg(&intf->dev, "CDC header len %u\n", h->bLength);
  94. goto err;
  95. }
  96. break;
  97. case USB_CDC_UNION_TYPE:
  98. if (found & 1 << USB_CDC_UNION_TYPE) {
  99. dev_dbg(&intf->dev, "extra CDC union\n");
  100. goto err;
  101. }
  102. if (h->bLength != sizeof(struct usb_cdc_union_desc)) {
  103. dev_dbg(&intf->dev, "CDC union len %u\n", h->bLength);
  104. goto err;
  105. }
  106. cdc_union = (struct usb_cdc_union_desc *)buf;
  107. break;
  108. case USB_CDC_ETHERNET_TYPE:
  109. if (found & 1 << USB_CDC_ETHERNET_TYPE) {
  110. dev_dbg(&intf->dev, "extra CDC ether\n");
  111. goto err;
  112. }
  113. if (h->bLength != sizeof(struct usb_cdc_ether_desc)) {
  114. dev_dbg(&intf->dev, "CDC ether len %u\n", h->bLength);
  115. goto err;
  116. }
  117. cdc_ether = (struct usb_cdc_ether_desc *)buf;
  118. break;
  119. }
  120. /*
  121. * Remember which CDC functional descriptors we've seen. Works
  122. * for all types we care about, of which USB_CDC_ETHERNET_TYPE
  123. * (0x0f) is the highest numbered
  124. */
  125. if (buf[2] < 32)
  126. found |= 1 << buf[2];
  127. next_desc:
  128. len -= h->bLength;
  129. buf += h->bLength;
  130. }
  131. /* did we find all the required ones? */
  132. if ((found & required) != required) {
  133. dev_err(&intf->dev, "CDC functional descriptors missing\n");
  134. goto err;
  135. }
  136. /* give the user a helpful hint if trying to bind to the wrong interface */
  137. if (cdc_union && desc->bInterfaceNumber == cdc_union->bMasterInterface0) {
  138. dev_err(&intf->dev, "leaving \"control\" interface for " DM_DRIVER " - try binding to %s instead!\n",
  139. dev_name(&usb_ifnum_to_if(dev->udev, cdc_union->bSlaveInterface0)->dev));
  140. goto err;
  141. }
  142. /* errors aren't fatal - we can live with the dynamic address */
  143. if (cdc_ether) {
  144. dev->hard_mtu = le16_to_cpu(cdc_ether->wMaxSegmentSize);
  145. usbnet_get_ethernet_addr(dev, cdc_ether->iMACAddress);
  146. }
  147. /* success! point the user to the management interface */
  148. if (control)
  149. dev_info(&intf->dev, "Use \"" DM_DRIVER "\" for QMI interface %s\n",
  150. dev_name(&control->dev));
  151. /* XXX: add a sysfs symlink somewhere to help management applications find it? */
  152. /* collect bulk endpoints now that we know intf == "data" interface */
  153. status = usbnet_get_endpoints(dev, intf);
  154. err:
  155. return status;
  156. }
  157. /* using a counter to merge subdriver requests with our own into a combined state */
  158. static int qmi_wwan_manage_power(struct usbnet *dev, int on)
  159. {
  160. atomic_t *pmcount = (void *)&dev->data[1];
  161. int rv = 0;
  162. dev_dbg(&dev->intf->dev, "%s() pmcount=%d, on=%d\n", __func__, atomic_read(pmcount), on);
  163. if ((on && atomic_add_return(1, pmcount) == 1) || (!on && atomic_dec_and_test(pmcount))) {
  164. /* need autopm_get/put here to ensure the usbcore sees the new value */
  165. rv = usb_autopm_get_interface(dev->intf);
  166. if (rv < 0)
  167. goto err;
  168. dev->intf->needs_remote_wakeup = on;
  169. usb_autopm_put_interface(dev->intf);
  170. }
  171. err:
  172. return rv;
  173. }
  174. static int qmi_wwan_cdc_wdm_manage_power(struct usb_interface *intf, int on)
  175. {
  176. struct usbnet *dev = usb_get_intfdata(intf);
  177. return qmi_wwan_manage_power(dev, on);
  178. }
  179. /* Some devices combine the "control" and "data" functions into a
  180. * single interface with all three endpoints: interrupt + bulk in and
  181. * out
  182. *
  183. * Setting up cdc-wdm as a subdriver owning the interrupt endpoint
  184. * will let it provide userspace access to the encapsulated QMI
  185. * protocol without interfering with the usbnet operations.
  186. */
  187. static int qmi_wwan_bind_shared(struct usbnet *dev, struct usb_interface *intf)
  188. {
  189. int rv;
  190. struct usb_driver *subdriver = NULL;
  191. atomic_t *pmcount = (void *)&dev->data[1];
  192. atomic_set(pmcount, 0);
  193. /* collect all three endpoints */
  194. rv = usbnet_get_endpoints(dev, intf);
  195. if (rv < 0)
  196. goto err;
  197. /* require interrupt endpoint for subdriver */
  198. if (!dev->status) {
  199. rv = -EINVAL;
  200. goto err;
  201. }
  202. subdriver = usb_cdc_wdm_register(intf, &dev->status->desc, 512, &qmi_wwan_cdc_wdm_manage_power);
  203. if (IS_ERR(subdriver)) {
  204. rv = PTR_ERR(subdriver);
  205. goto err;
  206. }
  207. /* can't let usbnet use the interrupt endpoint */
  208. dev->status = NULL;
  209. /* save subdriver struct for suspend/resume wrappers */
  210. dev->data[0] = (unsigned long)subdriver;
  211. err:
  212. return rv;
  213. }
  214. static void qmi_wwan_unbind_shared(struct usbnet *dev, struct usb_interface *intf)
  215. {
  216. struct usb_driver *subdriver = (void *)dev->data[0];
  217. if (subdriver && subdriver->disconnect)
  218. subdriver->disconnect(intf);
  219. dev->data[0] = (unsigned long)NULL;
  220. }
  221. /* suspend/resume wrappers calling both usbnet and the cdc-wdm
  222. * subdriver if present.
  223. *
  224. * NOTE: cdc-wdm also supports pre/post_reset, but we cannot provide
  225. * wrappers for those without adding usbnet reset support first.
  226. */
  227. static int qmi_wwan_suspend(struct usb_interface *intf, pm_message_t message)
  228. {
  229. struct usbnet *dev = usb_get_intfdata(intf);
  230. struct usb_driver *subdriver = (void *)dev->data[0];
  231. int ret;
  232. ret = usbnet_suspend(intf, message);
  233. if (ret < 0)
  234. goto err;
  235. if (subdriver && subdriver->suspend)
  236. ret = subdriver->suspend(intf, message);
  237. if (ret < 0)
  238. usbnet_resume(intf);
  239. err:
  240. return ret;
  241. }
  242. static int qmi_wwan_resume(struct usb_interface *intf)
  243. {
  244. struct usbnet *dev = usb_get_intfdata(intf);
  245. struct usb_driver *subdriver = (void *)dev->data[0];
  246. int ret = 0;
  247. if (subdriver && subdriver->resume)
  248. ret = subdriver->resume(intf);
  249. if (ret < 0)
  250. goto err;
  251. ret = usbnet_resume(intf);
  252. if (ret < 0 && subdriver && subdriver->resume && subdriver->suspend)
  253. subdriver->suspend(intf, PMSG_SUSPEND);
  254. err:
  255. return ret;
  256. }
  257. static const struct driver_info qmi_wwan_info = {
  258. .description = "QMI speaking wwan device",
  259. .flags = FLAG_WWAN,
  260. .bind = qmi_wwan_bind,
  261. .manage_power = qmi_wwan_manage_power,
  262. };
  263. static const struct driver_info qmi_wwan_shared = {
  264. .description = "QMI speaking wwan device with combined interface",
  265. .flags = FLAG_WWAN,
  266. .bind = qmi_wwan_bind_shared,
  267. .unbind = qmi_wwan_unbind_shared,
  268. .manage_power = qmi_wwan_manage_power,
  269. };
  270. #define HUAWEI_VENDOR_ID 0x12D1
  271. static const struct usb_device_id products[] = {
  272. { /* Huawei E392, E398 and possibly others sharing both device id and more... */
  273. .match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_INT_INFO,
  274. .idVendor = HUAWEI_VENDOR_ID,
  275. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  276. .bInterfaceSubClass = 1,
  277. .bInterfaceProtocol = 8, /* NOTE: This is the *slave* interface of the CDC Union! */
  278. .driver_info = (unsigned long)&qmi_wwan_info,
  279. },
  280. { /* Huawei E392, E398 and possibly others in "Windows mode"
  281. * using a combined control and data interface without any CDC
  282. * functional descriptors
  283. */
  284. .match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_INT_INFO,
  285. .idVendor = HUAWEI_VENDOR_ID,
  286. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  287. .bInterfaceSubClass = 1,
  288. .bInterfaceProtocol = 17,
  289. .driver_info = (unsigned long)&qmi_wwan_shared,
  290. },
  291. { } /* END */
  292. };
  293. MODULE_DEVICE_TABLE(usb, products);
  294. static struct usb_driver qmi_wwan_driver = {
  295. .name = "qmi_wwan",
  296. .id_table = products,
  297. .probe = usbnet_probe,
  298. .disconnect = usbnet_disconnect,
  299. .suspend = qmi_wwan_suspend,
  300. .resume = qmi_wwan_resume,
  301. .reset_resume = qmi_wwan_resume,
  302. .supports_autosuspend = 1,
  303. };
  304. static int __init qmi_wwan_init(void)
  305. {
  306. return usb_register(&qmi_wwan_driver);
  307. }
  308. module_init(qmi_wwan_init);
  309. static void __exit qmi_wwan_exit(void)
  310. {
  311. usb_deregister(&qmi_wwan_driver);
  312. }
  313. module_exit(qmi_wwan_exit);
  314. MODULE_AUTHOR("Bjørn Mork <bjorn@mork.no>");
  315. MODULE_DESCRIPTION("Qualcomm MSM Interface (QMI) WWAN driver");
  316. MODULE_LICENSE("GPL");