cdc_mbim.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * Copyright (c) 2012 Smith Micro Software, Inc.
  3. * Copyright (c) 2012 Bjørn Mork <bjorn@mork.no>
  4. *
  5. * This driver is based on and reuse most of cdc_ncm, which is
  6. * Copyright (C) ST-Ericsson 2010-2012
  7. *
  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.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/ethtool.h>
  15. #include <linux/if_vlan.h>
  16. #include <linux/ip.h>
  17. #include <linux/mii.h>
  18. #include <linux/usb.h>
  19. #include <linux/usb/cdc.h>
  20. #include <linux/usb/usbnet.h>
  21. #include <linux/usb/cdc-wdm.h>
  22. #include <linux/usb/cdc_ncm.h>
  23. /* driver specific data - must match cdc_ncm usage */
  24. struct cdc_mbim_state {
  25. struct cdc_ncm_ctx *ctx;
  26. atomic_t pmcount;
  27. struct usb_driver *subdriver;
  28. struct usb_interface *control;
  29. struct usb_interface *data;
  30. };
  31. /* using a counter to merge subdriver requests with our own into a combined state */
  32. static int cdc_mbim_manage_power(struct usbnet *dev, int on)
  33. {
  34. struct cdc_mbim_state *info = (void *)&dev->data;
  35. int rv = 0;
  36. dev_dbg(&dev->intf->dev, "%s() pmcount=%d, on=%d\n", __func__, atomic_read(&info->pmcount), on);
  37. if ((on && atomic_add_return(1, &info->pmcount) == 1) || (!on && atomic_dec_and_test(&info->pmcount))) {
  38. /* need autopm_get/put here to ensure the usbcore sees the new value */
  39. rv = usb_autopm_get_interface(dev->intf);
  40. if (rv < 0)
  41. goto err;
  42. dev->intf->needs_remote_wakeup = on;
  43. usb_autopm_put_interface(dev->intf);
  44. }
  45. err:
  46. return rv;
  47. }
  48. static int cdc_mbim_wdm_manage_power(struct usb_interface *intf, int status)
  49. {
  50. struct usbnet *dev = usb_get_intfdata(intf);
  51. /* can be called while disconnecting */
  52. if (!dev)
  53. return 0;
  54. return cdc_mbim_manage_power(dev, status);
  55. }
  56. static int cdc_mbim_bind(struct usbnet *dev, struct usb_interface *intf)
  57. {
  58. struct cdc_ncm_ctx *ctx;
  59. struct usb_driver *subdriver = ERR_PTR(-ENODEV);
  60. int ret = -ENODEV;
  61. u8 data_altsetting = CDC_NCM_DATA_ALTSETTING_NCM;
  62. struct cdc_mbim_state *info = (void *)&dev->data;
  63. /* see if interface supports MBIM alternate setting */
  64. if (intf->num_altsetting == 2) {
  65. if (!cdc_ncm_comm_intf_is_mbim(intf->cur_altsetting))
  66. usb_set_interface(dev->udev,
  67. intf->cur_altsetting->desc.bInterfaceNumber,
  68. CDC_NCM_COMM_ALTSETTING_MBIM);
  69. data_altsetting = CDC_NCM_DATA_ALTSETTING_MBIM;
  70. }
  71. /* Probably NCM, defer for cdc_ncm_bind */
  72. if (!cdc_ncm_comm_intf_is_mbim(intf->cur_altsetting))
  73. goto err;
  74. ret = cdc_ncm_bind_common(dev, intf, data_altsetting);
  75. if (ret)
  76. goto err;
  77. ctx = info->ctx;
  78. /* The MBIM descriptor and the status endpoint are required */
  79. if (ctx->mbim_desc && dev->status)
  80. subdriver = usb_cdc_wdm_register(ctx->control,
  81. &dev->status->desc,
  82. le16_to_cpu(ctx->mbim_desc->wMaxControlMessage),
  83. cdc_mbim_wdm_manage_power);
  84. if (IS_ERR(subdriver)) {
  85. ret = PTR_ERR(subdriver);
  86. cdc_ncm_unbind(dev, intf);
  87. goto err;
  88. }
  89. /* can't let usbnet use the interrupt endpoint */
  90. dev->status = NULL;
  91. info->subdriver = subdriver;
  92. /* MBIM cannot do ARP */
  93. dev->net->flags |= IFF_NOARP;
  94. /* no need to put the VLAN tci in the packet headers */
  95. dev->net->features |= NETIF_F_HW_VLAN_TX;
  96. err:
  97. return ret;
  98. }
  99. static void cdc_mbim_unbind(struct usbnet *dev, struct usb_interface *intf)
  100. {
  101. struct cdc_mbim_state *info = (void *)&dev->data;
  102. struct cdc_ncm_ctx *ctx = info->ctx;
  103. /* disconnect subdriver from control interface */
  104. if (info->subdriver && info->subdriver->disconnect)
  105. info->subdriver->disconnect(ctx->control);
  106. info->subdriver = NULL;
  107. /* let NCM unbind clean up both control and data interface */
  108. cdc_ncm_unbind(dev, intf);
  109. }
  110. static struct sk_buff *cdc_mbim_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
  111. {
  112. struct sk_buff *skb_out;
  113. struct cdc_mbim_state *info = (void *)&dev->data;
  114. struct cdc_ncm_ctx *ctx = info->ctx;
  115. __le32 sign = cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN);
  116. u16 tci = 0;
  117. u8 *c;
  118. if (!ctx)
  119. goto error;
  120. if (skb) {
  121. if (skb->len <= sizeof(ETH_HLEN))
  122. goto error;
  123. /* mapping VLANs to MBIM sessions:
  124. * no tag => IPS session <0>
  125. * 1 - 255 => IPS session <vlanid>
  126. * 256 - 4095 => unsupported, drop
  127. */
  128. vlan_get_tag(skb, &tci);
  129. switch (tci & 0x0f00) {
  130. case 0x0000: /* VLAN ID 0 - 255 */
  131. c = (u8 *)&sign;
  132. c[3] = tci;
  133. break;
  134. default:
  135. netif_err(dev, tx_err, dev->net,
  136. "unsupported tci=0x%04x\n", tci);
  137. goto error;
  138. }
  139. skb_reset_mac_header(skb);
  140. switch (eth_hdr(skb)->h_proto) {
  141. case htons(ETH_P_IP):
  142. case htons(ETH_P_IPV6):
  143. skb_pull(skb, ETH_HLEN);
  144. break;
  145. default:
  146. goto error;
  147. }
  148. }
  149. spin_lock_bh(&ctx->mtx);
  150. skb_out = cdc_ncm_fill_tx_frame(ctx, skb, sign);
  151. spin_unlock_bh(&ctx->mtx);
  152. return skb_out;
  153. error:
  154. if (skb)
  155. dev_kfree_skb_any(skb);
  156. return NULL;
  157. }
  158. static struct sk_buff *cdc_mbim_process_dgram(struct usbnet *dev, u8 *buf, size_t len, u16 tci)
  159. {
  160. __be16 proto;
  161. struct sk_buff *skb = NULL;
  162. if (len < sizeof(struct iphdr))
  163. goto err;
  164. switch (*buf & 0xf0) {
  165. case 0x40:
  166. proto = htons(ETH_P_IP);
  167. break;
  168. case 0x60:
  169. proto = htons(ETH_P_IPV6);
  170. break;
  171. default:
  172. goto err;
  173. }
  174. skb = netdev_alloc_skb_ip_align(dev->net, len + ETH_HLEN);
  175. if (!skb)
  176. goto err;
  177. /* add an ethernet header */
  178. skb_put(skb, ETH_HLEN);
  179. skb_reset_mac_header(skb);
  180. eth_hdr(skb)->h_proto = proto;
  181. memset(eth_hdr(skb)->h_source, 0, ETH_ALEN);
  182. memcpy(eth_hdr(skb)->h_dest, dev->net->dev_addr, ETH_ALEN);
  183. /* add datagram */
  184. memcpy(skb_put(skb, len), buf, len);
  185. /* map MBIM session to VLAN */
  186. if (tci)
  187. vlan_put_tag(skb, tci);
  188. err:
  189. return skb;
  190. }
  191. static int cdc_mbim_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
  192. {
  193. struct sk_buff *skb;
  194. struct cdc_mbim_state *info = (void *)&dev->data;
  195. struct cdc_ncm_ctx *ctx = info->ctx;
  196. int len;
  197. int nframes;
  198. int x;
  199. int offset;
  200. struct usb_cdc_ncm_ndp16 *ndp16;
  201. struct usb_cdc_ncm_dpe16 *dpe16;
  202. int ndpoffset;
  203. int loopcount = 50; /* arbitrary max preventing infinite loop */
  204. u8 *c;
  205. u16 tci;
  206. ndpoffset = cdc_ncm_rx_verify_nth16(ctx, skb_in);
  207. if (ndpoffset < 0)
  208. goto error;
  209. next_ndp:
  210. nframes = cdc_ncm_rx_verify_ndp16(skb_in, ndpoffset);
  211. if (nframes < 0)
  212. goto error;
  213. ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
  214. switch (ndp16->dwSignature & cpu_to_le32(0x00ffffff)) {
  215. case cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN):
  216. c = (u8 *)&ndp16->dwSignature;
  217. tci = c[3];
  218. break;
  219. default:
  220. netif_dbg(dev, rx_err, dev->net,
  221. "unsupported NDP signature <0x%08x>\n",
  222. le32_to_cpu(ndp16->dwSignature));
  223. goto err_ndp;
  224. }
  225. dpe16 = ndp16->dpe16;
  226. for (x = 0; x < nframes; x++, dpe16++) {
  227. offset = le16_to_cpu(dpe16->wDatagramIndex);
  228. len = le16_to_cpu(dpe16->wDatagramLength);
  229. /*
  230. * CDC NCM ch. 3.7
  231. * All entries after first NULL entry are to be ignored
  232. */
  233. if ((offset == 0) || (len == 0)) {
  234. if (!x)
  235. goto err_ndp; /* empty NTB */
  236. break;
  237. }
  238. /* sanity checking */
  239. if (((offset + len) > skb_in->len) || (len > ctx->rx_max)) {
  240. netif_dbg(dev, rx_err, dev->net,
  241. "invalid frame detected (ignored) offset[%u]=%u, length=%u, skb=%p\n",
  242. x, offset, len, skb_in);
  243. if (!x)
  244. goto err_ndp;
  245. break;
  246. } else {
  247. skb = cdc_mbim_process_dgram(dev, skb_in->data + offset, len, tci);
  248. if (!skb)
  249. goto error;
  250. usbnet_skb_return(dev, skb);
  251. }
  252. }
  253. err_ndp:
  254. /* are there more NDPs to process? */
  255. ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
  256. if (ndpoffset && loopcount--)
  257. goto next_ndp;
  258. return 1;
  259. error:
  260. return 0;
  261. }
  262. static int cdc_mbim_suspend(struct usb_interface *intf, pm_message_t message)
  263. {
  264. int ret = 0;
  265. struct usbnet *dev = usb_get_intfdata(intf);
  266. struct cdc_mbim_state *info = (void *)&dev->data;
  267. struct cdc_ncm_ctx *ctx = info->ctx;
  268. if (ctx == NULL) {
  269. ret = -1;
  270. goto error;
  271. }
  272. ret = usbnet_suspend(intf, message);
  273. if (ret < 0)
  274. goto error;
  275. if (intf == ctx->control && info->subdriver && info->subdriver->suspend)
  276. ret = info->subdriver->suspend(intf, message);
  277. if (ret < 0)
  278. usbnet_resume(intf);
  279. error:
  280. return ret;
  281. }
  282. static int cdc_mbim_resume(struct usb_interface *intf)
  283. {
  284. int ret = 0;
  285. struct usbnet *dev = usb_get_intfdata(intf);
  286. struct cdc_mbim_state *info = (void *)&dev->data;
  287. struct cdc_ncm_ctx *ctx = info->ctx;
  288. bool callsub = (intf == ctx->control && info->subdriver && info->subdriver->resume);
  289. if (callsub)
  290. ret = info->subdriver->resume(intf);
  291. if (ret < 0)
  292. goto err;
  293. ret = usbnet_resume(intf);
  294. if (ret < 0 && callsub && info->subdriver->suspend)
  295. info->subdriver->suspend(intf, PMSG_SUSPEND);
  296. err:
  297. return ret;
  298. }
  299. static const struct driver_info cdc_mbim_info = {
  300. .description = "CDC MBIM",
  301. .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET | FLAG_WWAN,
  302. .bind = cdc_mbim_bind,
  303. .unbind = cdc_mbim_unbind,
  304. .manage_power = cdc_mbim_manage_power,
  305. .rx_fixup = cdc_mbim_rx_fixup,
  306. .tx_fixup = cdc_mbim_tx_fixup,
  307. };
  308. static const struct usb_device_id mbim_devs[] = {
  309. /* This duplicate NCM entry is intentional. MBIM devices can
  310. * be disguised as NCM by default, and this is necessary to
  311. * allow us to bind the correct driver_info to such devices.
  312. *
  313. * bind() will sort out this for us, selecting the correct
  314. * entry and reject the other
  315. */
  316. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
  317. .driver_info = (unsigned long)&cdc_mbim_info,
  318. },
  319. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
  320. .driver_info = (unsigned long)&cdc_mbim_info,
  321. },
  322. {
  323. },
  324. };
  325. MODULE_DEVICE_TABLE(usb, mbim_devs);
  326. static struct usb_driver cdc_mbim_driver = {
  327. .name = "cdc_mbim",
  328. .id_table = mbim_devs,
  329. .probe = usbnet_probe,
  330. .disconnect = usbnet_disconnect,
  331. .suspend = cdc_mbim_suspend,
  332. .resume = cdc_mbim_resume,
  333. .reset_resume = cdc_mbim_resume,
  334. .supports_autosuspend = 1,
  335. .disable_hub_initiated_lpm = 1,
  336. };
  337. module_usb_driver(cdc_mbim_driver);
  338. MODULE_AUTHOR("Greg Suarez <gsuarez@smithmicro.com>");
  339. MODULE_AUTHOR("Bjørn Mork <bjorn@mork.no>");
  340. MODULE_DESCRIPTION("USB CDC MBIM host driver");
  341. MODULE_LICENSE("GPL");