cdc_mbim.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. dev->intf->needs_remote_wakeup = on;
  43. if (!rv)
  44. usb_autopm_put_interface(dev->intf);
  45. }
  46. return 0;
  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(dev, 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. /* Some devices are known to send Neigbor Solicitation messages and
  158. * require Neigbor Advertisement replies. The IPv6 core will not
  159. * respond since IFF_NOARP is set, so we must handle them ourselves.
  160. */
  161. static void do_neigh_solicit(struct usbnet *dev, u8 *buf, u16 tci)
  162. {
  163. struct ipv6hdr *iph = (void *)buf;
  164. struct nd_msg *msg = (void *)(iph + 1);
  165. struct net_device *netdev;
  166. struct inet6_dev *in6_dev;
  167. bool is_router;
  168. /* we'll only respond to requests from unicast addresses to
  169. * our solicited node addresses.
  170. */
  171. if (!ipv6_addr_is_solict_mult(&iph->daddr) ||
  172. !(ipv6_addr_type(&iph->saddr) & IPV6_ADDR_UNICAST))
  173. return;
  174. /* need to send the NA on the VLAN dev, if any */
  175. if (tci)
  176. netdev = __vlan_find_dev_deep(dev->net, htons(ETH_P_8021Q),
  177. tci);
  178. else
  179. netdev = dev->net;
  180. if (!netdev)
  181. return;
  182. in6_dev = in6_dev_get(netdev);
  183. if (!in6_dev)
  184. return;
  185. is_router = !!in6_dev->cnf.forwarding;
  186. in6_dev_put(in6_dev);
  187. /* ipv6_stub != NULL if in6_dev_get returned an inet6_dev */
  188. ipv6_stub->ndisc_send_na(netdev, NULL, &iph->saddr, &msg->target,
  189. is_router /* router */,
  190. true /* solicited */,
  191. false /* override */,
  192. true /* inc_opt */);
  193. }
  194. static bool is_neigh_solicit(u8 *buf, size_t len)
  195. {
  196. struct ipv6hdr *iph = (void *)buf;
  197. struct nd_msg *msg = (void *)(iph + 1);
  198. return (len >= sizeof(struct ipv6hdr) + sizeof(struct nd_msg) &&
  199. iph->nexthdr == IPPROTO_ICMPV6 &&
  200. msg->icmph.icmp6_code == 0 &&
  201. msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION);
  202. }
  203. static struct sk_buff *cdc_mbim_process_dgram(struct usbnet *dev, u8 *buf, size_t len, u16 tci)
  204. {
  205. __be16 proto = htons(ETH_P_802_3);
  206. struct sk_buff *skb = NULL;
  207. if (tci < 256) { /* IPS session? */
  208. if (len < sizeof(struct iphdr))
  209. goto err;
  210. switch (*buf & 0xf0) {
  211. case 0x40:
  212. proto = htons(ETH_P_IP);
  213. break;
  214. case 0x60:
  215. if (is_neigh_solicit(buf, len))
  216. do_neigh_solicit(dev, buf, tci);
  217. proto = htons(ETH_P_IPV6);
  218. break;
  219. default:
  220. goto err;
  221. }
  222. }
  223. skb = netdev_alloc_skb_ip_align(dev->net, len + ETH_HLEN);
  224. if (!skb)
  225. goto err;
  226. /* add an ethernet header */
  227. skb_put(skb, ETH_HLEN);
  228. skb_reset_mac_header(skb);
  229. eth_hdr(skb)->h_proto = proto;
  230. memset(eth_hdr(skb)->h_source, 0, ETH_ALEN);
  231. memcpy(eth_hdr(skb)->h_dest, dev->net->dev_addr, ETH_ALEN);
  232. /* add datagram */
  233. memcpy(skb_put(skb, len), buf, len);
  234. /* map MBIM session to VLAN */
  235. if (tci)
  236. vlan_put_tag(skb, htons(ETH_P_8021Q), tci);
  237. err:
  238. return skb;
  239. }
  240. static int cdc_mbim_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
  241. {
  242. struct sk_buff *skb;
  243. struct cdc_mbim_state *info = (void *)&dev->data;
  244. struct cdc_ncm_ctx *ctx = info->ctx;
  245. int len;
  246. int nframes;
  247. int x;
  248. int offset;
  249. struct usb_cdc_ncm_ndp16 *ndp16;
  250. struct usb_cdc_ncm_dpe16 *dpe16;
  251. int ndpoffset;
  252. int loopcount = 50; /* arbitrary max preventing infinite loop */
  253. u8 *c;
  254. u16 tci;
  255. ndpoffset = cdc_ncm_rx_verify_nth16(ctx, skb_in);
  256. if (ndpoffset < 0)
  257. goto error;
  258. next_ndp:
  259. nframes = cdc_ncm_rx_verify_ndp16(skb_in, ndpoffset);
  260. if (nframes < 0)
  261. goto error;
  262. ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
  263. switch (ndp16->dwSignature & cpu_to_le32(0x00ffffff)) {
  264. case cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN):
  265. c = (u8 *)&ndp16->dwSignature;
  266. tci = c[3];
  267. break;
  268. case cpu_to_le32(USB_CDC_MBIM_NDP16_DSS_SIGN):
  269. c = (u8 *)&ndp16->dwSignature;
  270. tci = c[3] + 256;
  271. break;
  272. default:
  273. netif_dbg(dev, rx_err, dev->net,
  274. "unsupported NDP signature <0x%08x>\n",
  275. le32_to_cpu(ndp16->dwSignature));
  276. goto err_ndp;
  277. }
  278. dpe16 = ndp16->dpe16;
  279. for (x = 0; x < nframes; x++, dpe16++) {
  280. offset = le16_to_cpu(dpe16->wDatagramIndex);
  281. len = le16_to_cpu(dpe16->wDatagramLength);
  282. /*
  283. * CDC NCM ch. 3.7
  284. * All entries after first NULL entry are to be ignored
  285. */
  286. if ((offset == 0) || (len == 0)) {
  287. if (!x)
  288. goto err_ndp; /* empty NTB */
  289. break;
  290. }
  291. /* sanity checking */
  292. if (((offset + len) > skb_in->len) || (len > ctx->rx_max)) {
  293. netif_dbg(dev, rx_err, dev->net,
  294. "invalid frame detected (ignored) offset[%u]=%u, length=%u, skb=%p\n",
  295. x, offset, len, skb_in);
  296. if (!x)
  297. goto err_ndp;
  298. break;
  299. } else {
  300. skb = cdc_mbim_process_dgram(dev, skb_in->data + offset, len, tci);
  301. if (!skb)
  302. goto error;
  303. usbnet_skb_return(dev, skb);
  304. }
  305. }
  306. err_ndp:
  307. /* are there more NDPs to process? */
  308. ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
  309. if (ndpoffset && loopcount--)
  310. goto next_ndp;
  311. return 1;
  312. error:
  313. return 0;
  314. }
  315. static int cdc_mbim_suspend(struct usb_interface *intf, pm_message_t message)
  316. {
  317. int ret = -ENODEV;
  318. struct usbnet *dev = usb_get_intfdata(intf);
  319. struct cdc_mbim_state *info = (void *)&dev->data;
  320. struct cdc_ncm_ctx *ctx = info->ctx;
  321. if (!ctx)
  322. goto error;
  323. /*
  324. * Both usbnet_suspend() and subdriver->suspend() MUST return 0
  325. * in system sleep context, otherwise, the resume callback has
  326. * to recover device from previous suspend failure.
  327. */
  328. ret = usbnet_suspend(intf, message);
  329. if (ret < 0)
  330. goto error;
  331. if (intf == ctx->control && info->subdriver && info->subdriver->suspend)
  332. ret = info->subdriver->suspend(intf, message);
  333. if (ret < 0)
  334. usbnet_resume(intf);
  335. error:
  336. return ret;
  337. }
  338. static int cdc_mbim_resume(struct usb_interface *intf)
  339. {
  340. int ret = 0;
  341. struct usbnet *dev = usb_get_intfdata(intf);
  342. struct cdc_mbim_state *info = (void *)&dev->data;
  343. struct cdc_ncm_ctx *ctx = info->ctx;
  344. bool callsub = (intf == ctx->control && info->subdriver && info->subdriver->resume);
  345. if (callsub)
  346. ret = info->subdriver->resume(intf);
  347. if (ret < 0)
  348. goto err;
  349. ret = usbnet_resume(intf);
  350. if (ret < 0 && callsub)
  351. info->subdriver->suspend(intf, PMSG_SUSPEND);
  352. err:
  353. return ret;
  354. }
  355. static const struct driver_info cdc_mbim_info = {
  356. .description = "CDC MBIM",
  357. .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET | FLAG_WWAN,
  358. .bind = cdc_mbim_bind,
  359. .unbind = cdc_mbim_unbind,
  360. .manage_power = cdc_mbim_manage_power,
  361. .rx_fixup = cdc_mbim_rx_fixup,
  362. .tx_fixup = cdc_mbim_tx_fixup,
  363. };
  364. /* MBIM and NCM devices should not need a ZLP after NTBs with
  365. * dwNtbOutMaxSize length. Nevertheless, a number of devices from
  366. * different vendor IDs will fail unless we send ZLPs, forcing us
  367. * to make this the default.
  368. *
  369. * This default may cause a performance penalty for spec conforming
  370. * devices wanting to take advantage of optimizations possible without
  371. * ZLPs. A whitelist is added in an attempt to avoid this for devices
  372. * known to conform to the MBIM specification.
  373. *
  374. * All known devices supporting NCM compatibility mode are also
  375. * conforming to the NCM and MBIM specifications. For this reason, the
  376. * NCM subclass entry is also in the ZLP whitelist.
  377. */
  378. static const struct driver_info cdc_mbim_info_zlp = {
  379. .description = "CDC MBIM",
  380. .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET | FLAG_WWAN | FLAG_SEND_ZLP,
  381. .bind = cdc_mbim_bind,
  382. .unbind = cdc_mbim_unbind,
  383. .manage_power = cdc_mbim_manage_power,
  384. .rx_fixup = cdc_mbim_rx_fixup,
  385. .tx_fixup = cdc_mbim_tx_fixup,
  386. };
  387. static const struct usb_device_id mbim_devs[] = {
  388. /* This duplicate NCM entry is intentional. MBIM devices can
  389. * be disguised as NCM by default, and this is necessary to
  390. * allow us to bind the correct driver_info to such devices.
  391. *
  392. * bind() will sort out this for us, selecting the correct
  393. * entry and reject the other
  394. */
  395. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
  396. .driver_info = (unsigned long)&cdc_mbim_info,
  397. },
  398. /* ZLP conformance whitelist: All Ericsson MBIM devices */
  399. { USB_VENDOR_AND_INTERFACE_INFO(0x0bdb, USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
  400. .driver_info = (unsigned long)&cdc_mbim_info,
  401. },
  402. /* default entry */
  403. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
  404. .driver_info = (unsigned long)&cdc_mbim_info_zlp,
  405. },
  406. {
  407. },
  408. };
  409. MODULE_DEVICE_TABLE(usb, mbim_devs);
  410. static struct usb_driver cdc_mbim_driver = {
  411. .name = "cdc_mbim",
  412. .id_table = mbim_devs,
  413. .probe = usbnet_probe,
  414. .disconnect = usbnet_disconnect,
  415. .suspend = cdc_mbim_suspend,
  416. .resume = cdc_mbim_resume,
  417. .reset_resume = cdc_mbim_resume,
  418. .supports_autosuspend = 1,
  419. .disable_hub_initiated_lpm = 1,
  420. };
  421. module_usb_driver(cdc_mbim_driver);
  422. MODULE_AUTHOR("Greg Suarez <gsuarez@smithmicro.com>");
  423. MODULE_AUTHOR("Bjørn Mork <bjorn@mork.no>");
  424. MODULE_DESCRIPTION("USB CDC MBIM host driver");
  425. MODULE_LICENSE("GPL");