cdc_mbim.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. #include <net/ipv6.h>
  24. #include <net/addrconf.h>
  25. /* driver specific data - must match cdc_ncm usage */
  26. struct cdc_mbim_state {
  27. struct cdc_ncm_ctx *ctx;
  28. atomic_t pmcount;
  29. struct usb_driver *subdriver;
  30. struct usb_interface *control;
  31. struct usb_interface *data;
  32. };
  33. /* using a counter to merge subdriver requests with our own into a combined state */
  34. static int cdc_mbim_manage_power(struct usbnet *dev, int on)
  35. {
  36. struct cdc_mbim_state *info = (void *)&dev->data;
  37. int rv = 0;
  38. dev_dbg(&dev->intf->dev, "%s() pmcount=%d, on=%d\n", __func__, atomic_read(&info->pmcount), on);
  39. if ((on && atomic_add_return(1, &info->pmcount) == 1) || (!on && atomic_dec_and_test(&info->pmcount))) {
  40. /* need autopm_get/put here to ensure the usbcore sees the new value */
  41. rv = usb_autopm_get_interface(dev->intf);
  42. if (rv < 0)
  43. goto err;
  44. dev->intf->needs_remote_wakeup = on;
  45. usb_autopm_put_interface(dev->intf);
  46. }
  47. err:
  48. return rv;
  49. }
  50. static int cdc_mbim_wdm_manage_power(struct usb_interface *intf, int status)
  51. {
  52. struct usbnet *dev = usb_get_intfdata(intf);
  53. /* can be called while disconnecting */
  54. if (!dev)
  55. return 0;
  56. return cdc_mbim_manage_power(dev, status);
  57. }
  58. static int cdc_mbim_bind(struct usbnet *dev, struct usb_interface *intf)
  59. {
  60. struct cdc_ncm_ctx *ctx;
  61. struct usb_driver *subdriver = ERR_PTR(-ENODEV);
  62. int ret = -ENODEV;
  63. u8 data_altsetting = cdc_ncm_select_altsetting(dev, intf);
  64. struct cdc_mbim_state *info = (void *)&dev->data;
  65. /* Probably NCM, defer for cdc_ncm_bind */
  66. if (!cdc_ncm_comm_intf_is_mbim(intf->cur_altsetting))
  67. goto err;
  68. ret = cdc_ncm_bind_common(dev, intf, data_altsetting);
  69. if (ret)
  70. goto err;
  71. ctx = info->ctx;
  72. /* The MBIM descriptor and the status endpoint are required */
  73. if (ctx->mbim_desc && dev->status)
  74. subdriver = usb_cdc_wdm_register(ctx->control,
  75. &dev->status->desc,
  76. le16_to_cpu(ctx->mbim_desc->wMaxControlMessage),
  77. cdc_mbim_wdm_manage_power);
  78. if (IS_ERR(subdriver)) {
  79. ret = PTR_ERR(subdriver);
  80. cdc_ncm_unbind(dev, intf);
  81. goto err;
  82. }
  83. /* can't let usbnet use the interrupt endpoint */
  84. dev->status = NULL;
  85. info->subdriver = subdriver;
  86. /* MBIM cannot do ARP */
  87. dev->net->flags |= IFF_NOARP;
  88. /* no need to put the VLAN tci in the packet headers */
  89. dev->net->features |= NETIF_F_HW_VLAN_CTAG_TX;
  90. err:
  91. return ret;
  92. }
  93. static void cdc_mbim_unbind(struct usbnet *dev, struct usb_interface *intf)
  94. {
  95. struct cdc_mbim_state *info = (void *)&dev->data;
  96. struct cdc_ncm_ctx *ctx = info->ctx;
  97. /* disconnect subdriver from control interface */
  98. if (info->subdriver && info->subdriver->disconnect)
  99. info->subdriver->disconnect(ctx->control);
  100. info->subdriver = NULL;
  101. /* let NCM unbind clean up both control and data interface */
  102. cdc_ncm_unbind(dev, intf);
  103. }
  104. static struct sk_buff *cdc_mbim_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
  105. {
  106. struct sk_buff *skb_out;
  107. struct cdc_mbim_state *info = (void *)&dev->data;
  108. struct cdc_ncm_ctx *ctx = info->ctx;
  109. __le32 sign = cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN);
  110. u16 tci = 0;
  111. u8 *c;
  112. if (!ctx)
  113. goto error;
  114. if (skb) {
  115. if (skb->len <= ETH_HLEN)
  116. goto error;
  117. /* mapping VLANs to MBIM sessions:
  118. * no tag => IPS session <0>
  119. * 1 - 255 => IPS session <vlanid>
  120. * 256 - 511 => DSS session <vlanid - 256>
  121. * 512 - 4095 => unsupported, drop
  122. */
  123. vlan_get_tag(skb, &tci);
  124. switch (tci & 0x0f00) {
  125. case 0x0000: /* VLAN ID 0 - 255 */
  126. /* verify that datagram is IPv4 or IPv6 */
  127. skb_reset_mac_header(skb);
  128. switch (eth_hdr(skb)->h_proto) {
  129. case htons(ETH_P_IP):
  130. case htons(ETH_P_IPV6):
  131. break;
  132. default:
  133. goto error;
  134. }
  135. c = (u8 *)&sign;
  136. c[3] = tci;
  137. break;
  138. case 0x0100: /* VLAN ID 256 - 511 */
  139. sign = cpu_to_le32(USB_CDC_MBIM_NDP16_DSS_SIGN);
  140. c = (u8 *)&sign;
  141. c[3] = tci;
  142. break;
  143. default:
  144. netif_err(dev, tx_err, dev->net,
  145. "unsupported tci=0x%04x\n", tci);
  146. goto error;
  147. }
  148. skb_pull(skb, ETH_HLEN);
  149. }
  150. spin_lock_bh(&ctx->mtx);
  151. skb_out = cdc_ncm_fill_tx_frame(ctx, skb, sign);
  152. spin_unlock_bh(&ctx->mtx);
  153. return skb_out;
  154. error:
  155. if (skb)
  156. dev_kfree_skb_any(skb);
  157. return NULL;
  158. }
  159. /* Some devices are known to send Neigbor Solicitation messages and
  160. * require Neigbor Advertisement replies. The IPv6 core will not
  161. * respond since IFF_NOARP is set, so we must handle them ourselves.
  162. */
  163. static void do_neigh_solicit(struct usbnet *dev, u8 *buf, u16 tci)
  164. {
  165. struct ipv6hdr *iph = (void *)buf;
  166. struct nd_msg *msg = (void *)(iph + 1);
  167. struct net_device *netdev;
  168. struct inet6_dev *in6_dev;
  169. bool is_router;
  170. /* we'll only respond to requests from unicast addresses to
  171. * our solicited node addresses.
  172. */
  173. if (!ipv6_addr_is_solict_mult(&iph->daddr) ||
  174. !(ipv6_addr_type(&iph->saddr) & IPV6_ADDR_UNICAST))
  175. return;
  176. /* need to send the NA on the VLAN dev, if any */
  177. if (tci)
  178. netdev = __vlan_find_dev_deep(dev->net, htons(ETH_P_8021Q),
  179. tci);
  180. else
  181. netdev = dev->net;
  182. if (!netdev)
  183. return;
  184. in6_dev = in6_dev_get(netdev);
  185. if (!in6_dev)
  186. return;
  187. is_router = !!in6_dev->cnf.forwarding;
  188. in6_dev_put(in6_dev);
  189. /* ipv6_stub != NULL if in6_dev_get returned an inet6_dev */
  190. ipv6_stub->ndisc_send_na(netdev, NULL, &iph->saddr, &msg->target,
  191. is_router /* router */,
  192. true /* solicited */,
  193. false /* override */,
  194. true /* inc_opt */);
  195. }
  196. static bool is_neigh_solicit(u8 *buf, size_t len)
  197. {
  198. struct ipv6hdr *iph = (void *)buf;
  199. struct nd_msg *msg = (void *)(iph + 1);
  200. return (len >= sizeof(struct ipv6hdr) + sizeof(struct nd_msg) &&
  201. iph->nexthdr == IPPROTO_ICMPV6 &&
  202. msg->icmph.icmp6_code == 0 &&
  203. msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION);
  204. }
  205. static struct sk_buff *cdc_mbim_process_dgram(struct usbnet *dev, u8 *buf, size_t len, u16 tci)
  206. {
  207. __be16 proto = htons(ETH_P_802_3);
  208. struct sk_buff *skb = NULL;
  209. if (tci < 256) { /* IPS session? */
  210. if (len < sizeof(struct iphdr))
  211. goto err;
  212. switch (*buf & 0xf0) {
  213. case 0x40:
  214. proto = htons(ETH_P_IP);
  215. break;
  216. case 0x60:
  217. if (is_neigh_solicit(buf, len))
  218. do_neigh_solicit(dev, buf, tci);
  219. proto = htons(ETH_P_IPV6);
  220. break;
  221. default:
  222. goto err;
  223. }
  224. }
  225. skb = netdev_alloc_skb_ip_align(dev->net, len + ETH_HLEN);
  226. if (!skb)
  227. goto err;
  228. /* add an ethernet header */
  229. skb_put(skb, ETH_HLEN);
  230. skb_reset_mac_header(skb);
  231. eth_hdr(skb)->h_proto = proto;
  232. memset(eth_hdr(skb)->h_source, 0, ETH_ALEN);
  233. memcpy(eth_hdr(skb)->h_dest, dev->net->dev_addr, ETH_ALEN);
  234. /* add datagram */
  235. memcpy(skb_put(skb, len), buf, len);
  236. /* map MBIM session to VLAN */
  237. if (tci)
  238. vlan_put_tag(skb, htons(ETH_P_8021Q), tci);
  239. err:
  240. return skb;
  241. }
  242. static int cdc_mbim_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
  243. {
  244. struct sk_buff *skb;
  245. struct cdc_mbim_state *info = (void *)&dev->data;
  246. struct cdc_ncm_ctx *ctx = info->ctx;
  247. int len;
  248. int nframes;
  249. int x;
  250. int offset;
  251. struct usb_cdc_ncm_ndp16 *ndp16;
  252. struct usb_cdc_ncm_dpe16 *dpe16;
  253. int ndpoffset;
  254. int loopcount = 50; /* arbitrary max preventing infinite loop */
  255. u8 *c;
  256. u16 tci;
  257. ndpoffset = cdc_ncm_rx_verify_nth16(ctx, skb_in);
  258. if (ndpoffset < 0)
  259. goto error;
  260. next_ndp:
  261. nframes = cdc_ncm_rx_verify_ndp16(skb_in, ndpoffset);
  262. if (nframes < 0)
  263. goto error;
  264. ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
  265. switch (ndp16->dwSignature & cpu_to_le32(0x00ffffff)) {
  266. case cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN):
  267. c = (u8 *)&ndp16->dwSignature;
  268. tci = c[3];
  269. break;
  270. case cpu_to_le32(USB_CDC_MBIM_NDP16_DSS_SIGN):
  271. c = (u8 *)&ndp16->dwSignature;
  272. tci = c[3] + 256;
  273. break;
  274. default:
  275. netif_dbg(dev, rx_err, dev->net,
  276. "unsupported NDP signature <0x%08x>\n",
  277. le32_to_cpu(ndp16->dwSignature));
  278. goto err_ndp;
  279. }
  280. dpe16 = ndp16->dpe16;
  281. for (x = 0; x < nframes; x++, dpe16++) {
  282. offset = le16_to_cpu(dpe16->wDatagramIndex);
  283. len = le16_to_cpu(dpe16->wDatagramLength);
  284. /*
  285. * CDC NCM ch. 3.7
  286. * All entries after first NULL entry are to be ignored
  287. */
  288. if ((offset == 0) || (len == 0)) {
  289. if (!x)
  290. goto err_ndp; /* empty NTB */
  291. break;
  292. }
  293. /* sanity checking */
  294. if (((offset + len) > skb_in->len) || (len > ctx->rx_max)) {
  295. netif_dbg(dev, rx_err, dev->net,
  296. "invalid frame detected (ignored) offset[%u]=%u, length=%u, skb=%p\n",
  297. x, offset, len, skb_in);
  298. if (!x)
  299. goto err_ndp;
  300. break;
  301. } else {
  302. skb = cdc_mbim_process_dgram(dev, skb_in->data + offset, len, tci);
  303. if (!skb)
  304. goto error;
  305. usbnet_skb_return(dev, skb);
  306. }
  307. }
  308. err_ndp:
  309. /* are there more NDPs to process? */
  310. ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
  311. if (ndpoffset && loopcount--)
  312. goto next_ndp;
  313. return 1;
  314. error:
  315. return 0;
  316. }
  317. static int cdc_mbim_suspend(struct usb_interface *intf, pm_message_t message)
  318. {
  319. int ret = 0;
  320. struct usbnet *dev = usb_get_intfdata(intf);
  321. struct cdc_mbim_state *info = (void *)&dev->data;
  322. struct cdc_ncm_ctx *ctx = info->ctx;
  323. if (ctx == NULL) {
  324. ret = -1;
  325. goto error;
  326. }
  327. /*
  328. * Both usbnet_suspend() and subdriver->suspend() MUST return 0
  329. * in system sleep context, otherwise, the resume callback has
  330. * to recover device from previous suspend failure.
  331. */
  332. ret = usbnet_suspend(intf, message);
  333. if (ret < 0)
  334. goto error;
  335. if (intf == ctx->control && info->subdriver && info->subdriver->suspend)
  336. ret = info->subdriver->suspend(intf, message);
  337. if (ret < 0)
  338. usbnet_resume(intf);
  339. error:
  340. return ret;
  341. }
  342. static int cdc_mbim_resume(struct usb_interface *intf)
  343. {
  344. int ret = 0;
  345. struct usbnet *dev = usb_get_intfdata(intf);
  346. struct cdc_mbim_state *info = (void *)&dev->data;
  347. struct cdc_ncm_ctx *ctx = info->ctx;
  348. bool callsub = (intf == ctx->control && info->subdriver && info->subdriver->resume);
  349. if (callsub)
  350. ret = info->subdriver->resume(intf);
  351. if (ret < 0)
  352. goto err;
  353. ret = usbnet_resume(intf);
  354. if (ret < 0 && callsub && info->subdriver->suspend)
  355. info->subdriver->suspend(intf, PMSG_SUSPEND);
  356. err:
  357. return ret;
  358. }
  359. static const struct driver_info cdc_mbim_info = {
  360. .description = "CDC MBIM",
  361. .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET | FLAG_WWAN,
  362. .bind = cdc_mbim_bind,
  363. .unbind = cdc_mbim_unbind,
  364. .manage_power = cdc_mbim_manage_power,
  365. .rx_fixup = cdc_mbim_rx_fixup,
  366. .tx_fixup = cdc_mbim_tx_fixup,
  367. };
  368. /* MBIM and NCM devices should not need a ZLP after NTBs with
  369. * dwNtbOutMaxSize length. This driver_info is for the exceptional
  370. * devices requiring it anyway, allowing them to be supported without
  371. * forcing the performance penalty on all the sane devices.
  372. */
  373. static const struct driver_info cdc_mbim_info_zlp = {
  374. .description = "CDC MBIM",
  375. .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET | FLAG_WWAN | FLAG_SEND_ZLP,
  376. .bind = cdc_mbim_bind,
  377. .unbind = cdc_mbim_unbind,
  378. .manage_power = cdc_mbim_manage_power,
  379. .rx_fixup = cdc_mbim_rx_fixup,
  380. .tx_fixup = cdc_mbim_tx_fixup,
  381. };
  382. static const struct usb_device_id mbim_devs[] = {
  383. /* This duplicate NCM entry is intentional. MBIM devices can
  384. * be disguised as NCM by default, and this is necessary to
  385. * allow us to bind the correct driver_info to such devices.
  386. *
  387. * bind() will sort out this for us, selecting the correct
  388. * entry and reject the other
  389. */
  390. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
  391. .driver_info = (unsigned long)&cdc_mbim_info,
  392. },
  393. /* Sierra Wireless MC7710 need ZLPs */
  394. { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x68a2, USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
  395. .driver_info = (unsigned long)&cdc_mbim_info_zlp,
  396. },
  397. /* HP hs2434 Mobile Broadband Module needs ZLPs */
  398. { USB_DEVICE_AND_INTERFACE_INFO(0x3f0, 0x4b1d, USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
  399. .driver_info = (unsigned long)&cdc_mbim_info_zlp,
  400. },
  401. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
  402. .driver_info = (unsigned long)&cdc_mbim_info,
  403. },
  404. {
  405. },
  406. };
  407. MODULE_DEVICE_TABLE(usb, mbim_devs);
  408. static struct usb_driver cdc_mbim_driver = {
  409. .name = "cdc_mbim",
  410. .id_table = mbim_devs,
  411. .probe = usbnet_probe,
  412. .disconnect = usbnet_disconnect,
  413. .suspend = cdc_mbim_suspend,
  414. .resume = cdc_mbim_resume,
  415. .reset_resume = cdc_mbim_resume,
  416. .supports_autosuspend = 1,
  417. .disable_hub_initiated_lpm = 1,
  418. };
  419. module_usb_driver(cdc_mbim_driver);
  420. MODULE_AUTHOR("Greg Suarez <gsuarez@smithmicro.com>");
  421. MODULE_AUTHOR("Bjørn Mork <bjorn@mork.no>");
  422. MODULE_DESCRIPTION("USB CDC MBIM host driver");
  423. MODULE_LICENSE("GPL");