cdc_mbim.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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_select_altsetting(dev, intf);
  62. struct cdc_mbim_state *info = (void *)&dev->data;
  63. /* Probably NCM, defer for cdc_ncm_bind */
  64. if (!cdc_ncm_comm_intf_is_mbim(intf->cur_altsetting))
  65. goto err;
  66. ret = cdc_ncm_bind_common(dev, intf, data_altsetting);
  67. if (ret)
  68. goto err;
  69. ctx = info->ctx;
  70. /* The MBIM descriptor and the status endpoint are required */
  71. if (ctx->mbim_desc && dev->status)
  72. subdriver = usb_cdc_wdm_register(ctx->control,
  73. &dev->status->desc,
  74. le16_to_cpu(ctx->mbim_desc->wMaxControlMessage),
  75. cdc_mbim_wdm_manage_power);
  76. if (IS_ERR(subdriver)) {
  77. ret = PTR_ERR(subdriver);
  78. cdc_ncm_unbind(dev, intf);
  79. goto err;
  80. }
  81. /* can't let usbnet use the interrupt endpoint */
  82. dev->status = NULL;
  83. info->subdriver = subdriver;
  84. /* MBIM cannot do ARP */
  85. dev->net->flags |= IFF_NOARP;
  86. /* no need to put the VLAN tci in the packet headers */
  87. dev->net->features |= NETIF_F_HW_VLAN_CTAG_TX;
  88. err:
  89. return ret;
  90. }
  91. static void cdc_mbim_unbind(struct usbnet *dev, struct usb_interface *intf)
  92. {
  93. struct cdc_mbim_state *info = (void *)&dev->data;
  94. struct cdc_ncm_ctx *ctx = info->ctx;
  95. /* disconnect subdriver from control interface */
  96. if (info->subdriver && info->subdriver->disconnect)
  97. info->subdriver->disconnect(ctx->control);
  98. info->subdriver = NULL;
  99. /* let NCM unbind clean up both control and data interface */
  100. cdc_ncm_unbind(dev, intf);
  101. }
  102. static struct sk_buff *cdc_mbim_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
  103. {
  104. struct sk_buff *skb_out;
  105. struct cdc_mbim_state *info = (void *)&dev->data;
  106. struct cdc_ncm_ctx *ctx = info->ctx;
  107. __le32 sign = cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN);
  108. u16 tci = 0;
  109. u8 *c;
  110. if (!ctx)
  111. goto error;
  112. if (skb) {
  113. if (skb->len <= ETH_HLEN)
  114. goto error;
  115. /* mapping VLANs to MBIM sessions:
  116. * no tag => IPS session <0>
  117. * 1 - 255 => IPS session <vlanid>
  118. * 256 - 511 => DSS session <vlanid - 256>
  119. * 512 - 4095 => unsupported, drop
  120. */
  121. vlan_get_tag(skb, &tci);
  122. switch (tci & 0x0f00) {
  123. case 0x0000: /* VLAN ID 0 - 255 */
  124. /* verify that datagram is IPv4 or IPv6 */
  125. skb_reset_mac_header(skb);
  126. switch (eth_hdr(skb)->h_proto) {
  127. case htons(ETH_P_IP):
  128. case htons(ETH_P_IPV6):
  129. break;
  130. default:
  131. goto error;
  132. }
  133. c = (u8 *)&sign;
  134. c[3] = tci;
  135. break;
  136. case 0x0100: /* VLAN ID 256 - 511 */
  137. sign = cpu_to_le32(USB_CDC_MBIM_NDP16_DSS_SIGN);
  138. c = (u8 *)&sign;
  139. c[3] = tci;
  140. break;
  141. default:
  142. netif_err(dev, tx_err, dev->net,
  143. "unsupported tci=0x%04x\n", tci);
  144. goto error;
  145. }
  146. skb_pull(skb, ETH_HLEN);
  147. }
  148. spin_lock_bh(&ctx->mtx);
  149. skb_out = cdc_ncm_fill_tx_frame(ctx, skb, sign);
  150. spin_unlock_bh(&ctx->mtx);
  151. return skb_out;
  152. error:
  153. if (skb)
  154. dev_kfree_skb_any(skb);
  155. return NULL;
  156. }
  157. static struct sk_buff *cdc_mbim_process_dgram(struct usbnet *dev, u8 *buf, size_t len, u16 tci)
  158. {
  159. __be16 proto = htons(ETH_P_802_3);
  160. struct sk_buff *skb = NULL;
  161. if (tci < 256) { /* IPS session? */
  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. }
  175. skb = netdev_alloc_skb_ip_align(dev->net, len + ETH_HLEN);
  176. if (!skb)
  177. goto err;
  178. /* add an ethernet header */
  179. skb_put(skb, ETH_HLEN);
  180. skb_reset_mac_header(skb);
  181. eth_hdr(skb)->h_proto = proto;
  182. memset(eth_hdr(skb)->h_source, 0, ETH_ALEN);
  183. memcpy(eth_hdr(skb)->h_dest, dev->net->dev_addr, ETH_ALEN);
  184. /* add datagram */
  185. memcpy(skb_put(skb, len), buf, len);
  186. /* map MBIM session to VLAN */
  187. if (tci)
  188. vlan_put_tag(skb, htons(ETH_P_8021Q), tci);
  189. err:
  190. return skb;
  191. }
  192. static int cdc_mbim_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
  193. {
  194. struct sk_buff *skb;
  195. struct cdc_mbim_state *info = (void *)&dev->data;
  196. struct cdc_ncm_ctx *ctx = info->ctx;
  197. int len;
  198. int nframes;
  199. int x;
  200. int offset;
  201. struct usb_cdc_ncm_ndp16 *ndp16;
  202. struct usb_cdc_ncm_dpe16 *dpe16;
  203. int ndpoffset;
  204. int loopcount = 50; /* arbitrary max preventing infinite loop */
  205. u8 *c;
  206. u16 tci;
  207. ndpoffset = cdc_ncm_rx_verify_nth16(ctx, skb_in);
  208. if (ndpoffset < 0)
  209. goto error;
  210. next_ndp:
  211. nframes = cdc_ncm_rx_verify_ndp16(skb_in, ndpoffset);
  212. if (nframes < 0)
  213. goto error;
  214. ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
  215. switch (ndp16->dwSignature & cpu_to_le32(0x00ffffff)) {
  216. case cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN):
  217. c = (u8 *)&ndp16->dwSignature;
  218. tci = c[3];
  219. break;
  220. case cpu_to_le32(USB_CDC_MBIM_NDP16_DSS_SIGN):
  221. c = (u8 *)&ndp16->dwSignature;
  222. tci = c[3] + 256;
  223. break;
  224. default:
  225. netif_dbg(dev, rx_err, dev->net,
  226. "unsupported NDP signature <0x%08x>\n",
  227. le32_to_cpu(ndp16->dwSignature));
  228. goto err_ndp;
  229. }
  230. dpe16 = ndp16->dpe16;
  231. for (x = 0; x < nframes; x++, dpe16++) {
  232. offset = le16_to_cpu(dpe16->wDatagramIndex);
  233. len = le16_to_cpu(dpe16->wDatagramLength);
  234. /*
  235. * CDC NCM ch. 3.7
  236. * All entries after first NULL entry are to be ignored
  237. */
  238. if ((offset == 0) || (len == 0)) {
  239. if (!x)
  240. goto err_ndp; /* empty NTB */
  241. break;
  242. }
  243. /* sanity checking */
  244. if (((offset + len) > skb_in->len) || (len > ctx->rx_max)) {
  245. netif_dbg(dev, rx_err, dev->net,
  246. "invalid frame detected (ignored) offset[%u]=%u, length=%u, skb=%p\n",
  247. x, offset, len, skb_in);
  248. if (!x)
  249. goto err_ndp;
  250. break;
  251. } else {
  252. skb = cdc_mbim_process_dgram(dev, skb_in->data + offset, len, tci);
  253. if (!skb)
  254. goto error;
  255. usbnet_skb_return(dev, skb);
  256. }
  257. }
  258. err_ndp:
  259. /* are there more NDPs to process? */
  260. ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
  261. if (ndpoffset && loopcount--)
  262. goto next_ndp;
  263. return 1;
  264. error:
  265. return 0;
  266. }
  267. static int cdc_mbim_suspend(struct usb_interface *intf, pm_message_t message)
  268. {
  269. int ret = 0;
  270. struct usbnet *dev = usb_get_intfdata(intf);
  271. struct cdc_mbim_state *info = (void *)&dev->data;
  272. struct cdc_ncm_ctx *ctx = info->ctx;
  273. if (ctx == NULL) {
  274. ret = -1;
  275. goto error;
  276. }
  277. /*
  278. * Both usbnet_suspend() and subdriver->suspend() MUST return 0
  279. * in system sleep context, otherwise, the resume callback has
  280. * to recover device from previous suspend failure.
  281. */
  282. ret = usbnet_suspend(intf, message);
  283. if (ret < 0)
  284. goto error;
  285. if (intf == ctx->control && info->subdriver && info->subdriver->suspend)
  286. ret = info->subdriver->suspend(intf, message);
  287. if (ret < 0)
  288. usbnet_resume(intf);
  289. error:
  290. return ret;
  291. }
  292. static int cdc_mbim_resume(struct usb_interface *intf)
  293. {
  294. int ret = 0;
  295. struct usbnet *dev = usb_get_intfdata(intf);
  296. struct cdc_mbim_state *info = (void *)&dev->data;
  297. struct cdc_ncm_ctx *ctx = info->ctx;
  298. bool callsub = (intf == ctx->control && info->subdriver && info->subdriver->resume);
  299. if (callsub)
  300. ret = info->subdriver->resume(intf);
  301. if (ret < 0)
  302. goto err;
  303. ret = usbnet_resume(intf);
  304. if (ret < 0 && callsub && info->subdriver->suspend)
  305. info->subdriver->suspend(intf, PMSG_SUSPEND);
  306. err:
  307. return ret;
  308. }
  309. static const struct driver_info cdc_mbim_info = {
  310. .description = "CDC MBIM",
  311. .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET | FLAG_WWAN,
  312. .bind = cdc_mbim_bind,
  313. .unbind = cdc_mbim_unbind,
  314. .manage_power = cdc_mbim_manage_power,
  315. .rx_fixup = cdc_mbim_rx_fixup,
  316. .tx_fixup = cdc_mbim_tx_fixup,
  317. };
  318. /* MBIM and NCM devices should not need a ZLP after NTBs with
  319. * dwNtbOutMaxSize length. This driver_info is for the exceptional
  320. * devices requiring it anyway, allowing them to be supported without
  321. * forcing the performance penalty on all the sane devices.
  322. */
  323. static const struct driver_info cdc_mbim_info_zlp = {
  324. .description = "CDC MBIM",
  325. .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET | FLAG_WWAN | FLAG_SEND_ZLP,
  326. .bind = cdc_mbim_bind,
  327. .unbind = cdc_mbim_unbind,
  328. .manage_power = cdc_mbim_manage_power,
  329. .rx_fixup = cdc_mbim_rx_fixup,
  330. .tx_fixup = cdc_mbim_tx_fixup,
  331. };
  332. static const struct usb_device_id mbim_devs[] = {
  333. /* This duplicate NCM entry is intentional. MBIM devices can
  334. * be disguised as NCM by default, and this is necessary to
  335. * allow us to bind the correct driver_info to such devices.
  336. *
  337. * bind() will sort out this for us, selecting the correct
  338. * entry and reject the other
  339. */
  340. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
  341. .driver_info = (unsigned long)&cdc_mbim_info,
  342. },
  343. /* Sierra Wireless MC7710 need ZLPs */
  344. { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x68a2, USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
  345. .driver_info = (unsigned long)&cdc_mbim_info_zlp,
  346. },
  347. /* HP hs2434 Mobile Broadband Module needs ZLPs */
  348. { USB_DEVICE_AND_INTERFACE_INFO(0x3f0, 0x4b1d, USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
  349. .driver_info = (unsigned long)&cdc_mbim_info_zlp,
  350. },
  351. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
  352. .driver_info = (unsigned long)&cdc_mbim_info,
  353. },
  354. {
  355. },
  356. };
  357. MODULE_DEVICE_TABLE(usb, mbim_devs);
  358. static struct usb_driver cdc_mbim_driver = {
  359. .name = "cdc_mbim",
  360. .id_table = mbim_devs,
  361. .probe = usbnet_probe,
  362. .disconnect = usbnet_disconnect,
  363. .suspend = cdc_mbim_suspend,
  364. .resume = cdc_mbim_resume,
  365. .reset_resume = cdc_mbim_resume,
  366. .supports_autosuspend = 1,
  367. .disable_hub_initiated_lpm = 1,
  368. };
  369. module_usb_driver(cdc_mbim_driver);
  370. MODULE_AUTHOR("Greg Suarez <gsuarez@smithmicro.com>");
  371. MODULE_AUTHOR("Bjørn Mork <bjorn@mork.no>");
  372. MODULE_DESCRIPTION("USB CDC MBIM host driver");
  373. MODULE_LICENSE("GPL");