cdc_mbim.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012 Smith Micro Software, Inc.
  4. * Copyright (c) 2012 Bjørn Mork <bjorn@mork.no>
  5. *
  6. * This driver is based on and reuse most of cdc_ncm, which is
  7. * Copyright (C) ST-Ericsson 2010-2012
  8. */
  9. #include <linux/module.h>
  10. #include <linux/netdevice.h>
  11. #include <linux/ethtool.h>
  12. #include <linux/if_vlan.h>
  13. #include <linux/ip.h>
  14. #include <linux/mii.h>
  15. #include <linux/usb.h>
  16. #include <linux/usb/cdc.h>
  17. #include <linux/usb/usbnet.h>
  18. #include <linux/usb/cdc-wdm.h>
  19. #include <linux/usb/cdc_ncm.h>
  20. #include <net/ipv6.h>
  21. #include <net/addrconf.h>
  22. #include <net/ipv6_stubs.h>
  23. /* alternative VLAN for IP session 0 if not untagged */
  24. #define MBIM_IPS0_VID 4094
  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. unsigned long _unused;
  31. unsigned long flags;
  32. };
  33. /* flags for the cdc_mbim_state.flags field */
  34. enum cdc_mbim_flags {
  35. FLAG_IPS0_VLAN = 1 << 0, /* IP session 0 is tagged */
  36. };
  37. /* using a counter to merge subdriver requests with our own into a combined state */
  38. static int cdc_mbim_manage_power(struct usbnet *dev, int on)
  39. {
  40. struct cdc_mbim_state *info = (void *)&dev->data;
  41. int rv = 0;
  42. dev_dbg(&dev->intf->dev, "%s() pmcount=%d, on=%d\n", __func__, atomic_read(&info->pmcount), on);
  43. if ((on && atomic_add_return(1, &info->pmcount) == 1) || (!on && atomic_dec_and_test(&info->pmcount))) {
  44. /* need autopm_get/put here to ensure the usbcore sees the new value */
  45. rv = usb_autopm_get_interface(dev->intf);
  46. dev->intf->needs_remote_wakeup = on;
  47. if (!rv)
  48. usb_autopm_put_interface(dev->intf);
  49. }
  50. return 0;
  51. }
  52. static int cdc_mbim_wdm_manage_power(struct usb_interface *intf, int status)
  53. {
  54. struct usbnet *dev = usb_get_intfdata(intf);
  55. /* can be called while disconnecting */
  56. if (!dev)
  57. return 0;
  58. return cdc_mbim_manage_power(dev, status);
  59. }
  60. static int cdc_mbim_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
  61. {
  62. struct usbnet *dev = netdev_priv(netdev);
  63. struct cdc_mbim_state *info = (void *)&dev->data;
  64. /* creation of this VLAN is a request to tag IP session 0 */
  65. if (vid == MBIM_IPS0_VID)
  66. info->flags |= FLAG_IPS0_VLAN;
  67. else
  68. if (vid >= 512) /* we don't map these to MBIM session */
  69. return -EINVAL;
  70. return 0;
  71. }
  72. static int cdc_mbim_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
  73. {
  74. struct usbnet *dev = netdev_priv(netdev);
  75. struct cdc_mbim_state *info = (void *)&dev->data;
  76. /* this is a request for an untagged IP session 0 */
  77. if (vid == MBIM_IPS0_VID)
  78. info->flags &= ~FLAG_IPS0_VLAN;
  79. return 0;
  80. }
  81. static const struct net_device_ops cdc_mbim_netdev_ops = {
  82. .ndo_open = usbnet_open,
  83. .ndo_stop = usbnet_stop,
  84. .ndo_start_xmit = usbnet_start_xmit,
  85. .ndo_tx_timeout = usbnet_tx_timeout,
  86. .ndo_get_stats64 = usbnet_get_stats64,
  87. .ndo_change_mtu = cdc_ncm_change_mtu,
  88. .ndo_set_mac_address = eth_mac_addr,
  89. .ndo_validate_addr = eth_validate_addr,
  90. .ndo_vlan_rx_add_vid = cdc_mbim_rx_add_vid,
  91. .ndo_vlan_rx_kill_vid = cdc_mbim_rx_kill_vid,
  92. };
  93. /* Change the control interface altsetting and update the .driver_info
  94. * pointer if the matching entry after changing class codes points to
  95. * a different struct
  96. */
  97. static int cdc_mbim_set_ctrlalt(struct usbnet *dev, struct usb_interface *intf, u8 alt)
  98. {
  99. struct usb_driver *driver = to_usb_driver(intf->dev.driver);
  100. const struct usb_device_id *id;
  101. struct driver_info *info;
  102. int ret;
  103. ret = usb_set_interface(dev->udev,
  104. intf->cur_altsetting->desc.bInterfaceNumber,
  105. alt);
  106. if (ret)
  107. return ret;
  108. id = usb_match_id(intf, driver->id_table);
  109. if (!id)
  110. return -ENODEV;
  111. info = (struct driver_info *)id->driver_info;
  112. if (info != dev->driver_info) {
  113. dev_dbg(&intf->dev, "driver_info updated to '%s'\n",
  114. info->description);
  115. dev->driver_info = info;
  116. }
  117. return 0;
  118. }
  119. static int cdc_mbim_bind(struct usbnet *dev, struct usb_interface *intf)
  120. {
  121. struct cdc_ncm_ctx *ctx;
  122. struct usb_driver *subdriver = ERR_PTR(-ENODEV);
  123. int ret = -ENODEV;
  124. u8 data_altsetting = 1;
  125. struct cdc_mbim_state *info = (void *)&dev->data;
  126. /* should we change control altsetting on a NCM/MBIM function? */
  127. if (cdc_ncm_select_altsetting(intf) == CDC_NCM_COMM_ALTSETTING_MBIM) {
  128. data_altsetting = CDC_NCM_DATA_ALTSETTING_MBIM;
  129. ret = cdc_mbim_set_ctrlalt(dev, intf, CDC_NCM_COMM_ALTSETTING_MBIM);
  130. if (ret)
  131. goto err;
  132. ret = -ENODEV;
  133. }
  134. /* we will hit this for NCM/MBIM functions if prefer_mbim is false */
  135. if (!cdc_ncm_comm_intf_is_mbim(intf->cur_altsetting))
  136. goto err;
  137. ret = cdc_ncm_bind_common(dev, intf, data_altsetting, dev->driver_info->data);
  138. if (ret)
  139. goto err;
  140. ctx = info->ctx;
  141. /* The MBIM descriptor and the status endpoint are required */
  142. if (ctx->mbim_desc && dev->status)
  143. subdriver = usb_cdc_wdm_register(ctx->control,
  144. &dev->status->desc,
  145. le16_to_cpu(ctx->mbim_desc->wMaxControlMessage),
  146. cdc_mbim_wdm_manage_power);
  147. if (IS_ERR(subdriver)) {
  148. ret = PTR_ERR(subdriver);
  149. cdc_ncm_unbind(dev, intf);
  150. goto err;
  151. }
  152. /* can't let usbnet use the interrupt endpoint */
  153. dev->status = NULL;
  154. info->subdriver = subdriver;
  155. /* MBIM cannot do ARP */
  156. dev->net->flags |= IFF_NOARP;
  157. /* no need to put the VLAN tci in the packet headers */
  158. dev->net->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_FILTER;
  159. /* monitor VLAN additions and removals */
  160. dev->net->netdev_ops = &cdc_mbim_netdev_ops;
  161. err:
  162. return ret;
  163. }
  164. static void cdc_mbim_unbind(struct usbnet *dev, struct usb_interface *intf)
  165. {
  166. struct cdc_mbim_state *info = (void *)&dev->data;
  167. struct cdc_ncm_ctx *ctx = info->ctx;
  168. /* disconnect subdriver from control interface */
  169. if (info->subdriver && info->subdriver->disconnect)
  170. info->subdriver->disconnect(ctx->control);
  171. info->subdriver = NULL;
  172. /* let NCM unbind clean up both control and data interface */
  173. cdc_ncm_unbind(dev, intf);
  174. }
  175. /* verify that the ethernet protocol is IPv4 or IPv6 */
  176. static bool is_ip_proto(__be16 proto)
  177. {
  178. switch (proto) {
  179. case htons(ETH_P_IP):
  180. case htons(ETH_P_IPV6):
  181. return true;
  182. }
  183. return false;
  184. }
  185. static struct sk_buff *cdc_mbim_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
  186. {
  187. struct sk_buff *skb_out;
  188. struct cdc_mbim_state *info = (void *)&dev->data;
  189. struct cdc_ncm_ctx *ctx = info->ctx;
  190. __le32 sign = cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN);
  191. u16 tci = 0;
  192. bool is_ip;
  193. u8 *c;
  194. if (!ctx)
  195. goto error;
  196. if (skb) {
  197. if (skb->len <= ETH_HLEN)
  198. goto error;
  199. /* Some applications using e.g. packet sockets will
  200. * bypass the VLAN acceleration and create tagged
  201. * ethernet frames directly. We primarily look for
  202. * the accelerated out-of-band tag, but fall back if
  203. * required
  204. */
  205. skb_reset_mac_header(skb);
  206. if (vlan_get_tag(skb, &tci) < 0 && skb->len > VLAN_ETH_HLEN &&
  207. __vlan_get_tag(skb, &tci) == 0) {
  208. is_ip = is_ip_proto(vlan_eth_hdr(skb)->h_vlan_encapsulated_proto);
  209. skb_pull(skb, VLAN_ETH_HLEN);
  210. } else {
  211. is_ip = is_ip_proto(eth_hdr(skb)->h_proto);
  212. skb_pull(skb, ETH_HLEN);
  213. }
  214. /* Is IP session <0> tagged too? */
  215. if (info->flags & FLAG_IPS0_VLAN) {
  216. /* drop all untagged packets */
  217. if (!tci)
  218. goto error;
  219. /* map MBIM_IPS0_VID to IPS<0> */
  220. if (tci == MBIM_IPS0_VID)
  221. tci = 0;
  222. }
  223. /* mapping VLANs to MBIM sessions:
  224. * no tag => IPS session <0> if !FLAG_IPS0_VLAN
  225. * 1 - 255 => IPS session <vlanid>
  226. * 256 - 511 => DSS session <vlanid - 256>
  227. * 512 - 4093 => unsupported, drop
  228. * 4094 => IPS session <0> if FLAG_IPS0_VLAN
  229. */
  230. switch (tci & 0x0f00) {
  231. case 0x0000: /* VLAN ID 0 - 255 */
  232. if (!is_ip)
  233. goto error;
  234. c = (u8 *)&sign;
  235. c[3] = tci;
  236. break;
  237. case 0x0100: /* VLAN ID 256 - 511 */
  238. if (is_ip)
  239. goto error;
  240. sign = cpu_to_le32(USB_CDC_MBIM_NDP16_DSS_SIGN);
  241. c = (u8 *)&sign;
  242. c[3] = tci;
  243. break;
  244. default:
  245. netif_err(dev, tx_err, dev->net,
  246. "unsupported tci=0x%04x\n", tci);
  247. goto error;
  248. }
  249. }
  250. spin_lock_bh(&ctx->mtx);
  251. skb_out = cdc_ncm_fill_tx_frame(dev, skb, sign);
  252. spin_unlock_bh(&ctx->mtx);
  253. return skb_out;
  254. error:
  255. if (skb)
  256. dev_kfree_skb_any(skb);
  257. return NULL;
  258. }
  259. /* Some devices are known to send Neigbor Solicitation messages and
  260. * require Neigbor Advertisement replies. The IPv6 core will not
  261. * respond since IFF_NOARP is set, so we must handle them ourselves.
  262. */
  263. static void do_neigh_solicit(struct usbnet *dev, u8 *buf, u16 tci)
  264. {
  265. struct ipv6hdr *iph = (void *)buf;
  266. struct nd_msg *msg = (void *)(iph + 1);
  267. struct net_device *netdev;
  268. struct inet6_dev *in6_dev;
  269. bool is_router;
  270. /* we'll only respond to requests from unicast addresses to
  271. * our solicited node addresses.
  272. */
  273. if (!ipv6_addr_is_solict_mult(&iph->daddr) ||
  274. !(ipv6_addr_type(&iph->saddr) & IPV6_ADDR_UNICAST))
  275. return;
  276. /* need to send the NA on the VLAN dev, if any */
  277. rcu_read_lock();
  278. if (tci) {
  279. netdev = __vlan_find_dev_deep_rcu(dev->net, htons(ETH_P_8021Q),
  280. tci);
  281. if (!netdev) {
  282. rcu_read_unlock();
  283. return;
  284. }
  285. } else {
  286. netdev = dev->net;
  287. }
  288. dev_hold(netdev);
  289. rcu_read_unlock();
  290. in6_dev = in6_dev_get(netdev);
  291. if (!in6_dev)
  292. goto out;
  293. is_router = !!in6_dev->cnf.forwarding;
  294. in6_dev_put(in6_dev);
  295. /* ipv6_stub != NULL if in6_dev_get returned an inet6_dev */
  296. ipv6_stub->ndisc_send_na(netdev, &iph->saddr, &msg->target,
  297. is_router /* router */,
  298. true /* solicited */,
  299. false /* override */,
  300. true /* inc_opt */);
  301. out:
  302. dev_put(netdev);
  303. }
  304. static bool is_neigh_solicit(u8 *buf, size_t len)
  305. {
  306. struct ipv6hdr *iph = (void *)buf;
  307. struct nd_msg *msg = (void *)(iph + 1);
  308. return (len >= sizeof(struct ipv6hdr) + sizeof(struct nd_msg) &&
  309. iph->nexthdr == IPPROTO_ICMPV6 &&
  310. msg->icmph.icmp6_code == 0 &&
  311. msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION);
  312. }
  313. static struct sk_buff *cdc_mbim_process_dgram(struct usbnet *dev, u8 *buf, size_t len, u16 tci)
  314. {
  315. __be16 proto = htons(ETH_P_802_3);
  316. struct sk_buff *skb = NULL;
  317. if (tci < 256 || tci == MBIM_IPS0_VID) { /* IPS session? */
  318. if (len < sizeof(struct iphdr))
  319. goto err;
  320. switch (*buf & 0xf0) {
  321. case 0x40:
  322. proto = htons(ETH_P_IP);
  323. break;
  324. case 0x60:
  325. if (is_neigh_solicit(buf, len))
  326. do_neigh_solicit(dev, buf, tci);
  327. proto = htons(ETH_P_IPV6);
  328. break;
  329. default:
  330. goto err;
  331. }
  332. }
  333. skb = netdev_alloc_skb_ip_align(dev->net, len + ETH_HLEN);
  334. if (!skb)
  335. goto err;
  336. /* add an ethernet header */
  337. skb_put(skb, ETH_HLEN);
  338. skb_reset_mac_header(skb);
  339. eth_hdr(skb)->h_proto = proto;
  340. eth_zero_addr(eth_hdr(skb)->h_source);
  341. memcpy(eth_hdr(skb)->h_dest, dev->net->dev_addr, ETH_ALEN);
  342. /* add datagram */
  343. skb_put_data(skb, buf, len);
  344. /* map MBIM session to VLAN */
  345. if (tci)
  346. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), tci);
  347. err:
  348. return skb;
  349. }
  350. static int cdc_mbim_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
  351. {
  352. struct sk_buff *skb;
  353. struct cdc_mbim_state *info = (void *)&dev->data;
  354. struct cdc_ncm_ctx *ctx = info->ctx;
  355. int len;
  356. int nframes;
  357. int x;
  358. int offset;
  359. struct usb_cdc_ncm_ndp16 *ndp16;
  360. struct usb_cdc_ncm_dpe16 *dpe16;
  361. int ndpoffset;
  362. int loopcount = 50; /* arbitrary max preventing infinite loop */
  363. u32 payload = 0;
  364. u8 *c;
  365. u16 tci;
  366. ndpoffset = cdc_ncm_rx_verify_nth16(ctx, skb_in);
  367. if (ndpoffset < 0)
  368. goto error;
  369. next_ndp:
  370. nframes = cdc_ncm_rx_verify_ndp16(skb_in, ndpoffset);
  371. if (nframes < 0)
  372. goto error;
  373. ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
  374. switch (ndp16->dwSignature & cpu_to_le32(0x00ffffff)) {
  375. case cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN):
  376. c = (u8 *)&ndp16->dwSignature;
  377. tci = c[3];
  378. /* tag IPS<0> packets too if MBIM_IPS0_VID exists */
  379. if (!tci && info->flags & FLAG_IPS0_VLAN)
  380. tci = MBIM_IPS0_VID;
  381. break;
  382. case cpu_to_le32(USB_CDC_MBIM_NDP16_DSS_SIGN):
  383. c = (u8 *)&ndp16->dwSignature;
  384. tci = c[3] + 256;
  385. break;
  386. default:
  387. netif_dbg(dev, rx_err, dev->net,
  388. "unsupported NDP signature <0x%08x>\n",
  389. le32_to_cpu(ndp16->dwSignature));
  390. goto err_ndp;
  391. }
  392. dpe16 = ndp16->dpe16;
  393. for (x = 0; x < nframes; x++, dpe16++) {
  394. offset = le16_to_cpu(dpe16->wDatagramIndex);
  395. len = le16_to_cpu(dpe16->wDatagramLength);
  396. /*
  397. * CDC NCM ch. 3.7
  398. * All entries after first NULL entry are to be ignored
  399. */
  400. if ((offset == 0) || (len == 0)) {
  401. if (!x)
  402. goto err_ndp; /* empty NTB */
  403. break;
  404. }
  405. /* sanity checking */
  406. if (((offset + len) > skb_in->len) || (len > ctx->rx_max)) {
  407. netif_dbg(dev, rx_err, dev->net,
  408. "invalid frame detected (ignored) offset[%u]=%u, length=%u, skb=%p\n",
  409. x, offset, len, skb_in);
  410. if (!x)
  411. goto err_ndp;
  412. break;
  413. } else {
  414. skb = cdc_mbim_process_dgram(dev, skb_in->data + offset, len, tci);
  415. if (!skb)
  416. goto error;
  417. usbnet_skb_return(dev, skb);
  418. payload += len; /* count payload bytes in this NTB */
  419. }
  420. }
  421. err_ndp:
  422. /* are there more NDPs to process? */
  423. ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
  424. if (ndpoffset && loopcount--)
  425. goto next_ndp;
  426. /* update stats */
  427. ctx->rx_overhead += skb_in->len - payload;
  428. ctx->rx_ntbs++;
  429. return 1;
  430. error:
  431. return 0;
  432. }
  433. static int cdc_mbim_suspend(struct usb_interface *intf, pm_message_t message)
  434. {
  435. int ret = -ENODEV;
  436. struct usbnet *dev = usb_get_intfdata(intf);
  437. struct cdc_mbim_state *info = (void *)&dev->data;
  438. struct cdc_ncm_ctx *ctx = info->ctx;
  439. if (!ctx)
  440. goto error;
  441. /*
  442. * Both usbnet_suspend() and subdriver->suspend() MUST return 0
  443. * in system sleep context, otherwise, the resume callback has
  444. * to recover device from previous suspend failure.
  445. */
  446. ret = usbnet_suspend(intf, message);
  447. if (ret < 0)
  448. goto error;
  449. if (intf == ctx->control && info->subdriver && info->subdriver->suspend)
  450. ret = info->subdriver->suspend(intf, message);
  451. if (ret < 0)
  452. usbnet_resume(intf);
  453. error:
  454. return ret;
  455. }
  456. static int cdc_mbim_resume(struct usb_interface *intf)
  457. {
  458. int ret = 0;
  459. struct usbnet *dev = usb_get_intfdata(intf);
  460. struct cdc_mbim_state *info = (void *)&dev->data;
  461. struct cdc_ncm_ctx *ctx = info->ctx;
  462. bool callsub = (intf == ctx->control && info->subdriver && info->subdriver->resume);
  463. if (callsub)
  464. ret = info->subdriver->resume(intf);
  465. if (ret < 0)
  466. goto err;
  467. ret = usbnet_resume(intf);
  468. if (ret < 0 && callsub)
  469. info->subdriver->suspend(intf, PMSG_SUSPEND);
  470. err:
  471. return ret;
  472. }
  473. static const struct driver_info cdc_mbim_info = {
  474. .description = "CDC MBIM",
  475. .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET | FLAG_WWAN,
  476. .bind = cdc_mbim_bind,
  477. .unbind = cdc_mbim_unbind,
  478. .manage_power = cdc_mbim_manage_power,
  479. .rx_fixup = cdc_mbim_rx_fixup,
  480. .tx_fixup = cdc_mbim_tx_fixup,
  481. };
  482. /* MBIM and NCM devices should not need a ZLP after NTBs with
  483. * dwNtbOutMaxSize length. Nevertheless, a number of devices from
  484. * different vendor IDs will fail unless we send ZLPs, forcing us
  485. * to make this the default.
  486. *
  487. * This default may cause a performance penalty for spec conforming
  488. * devices wanting to take advantage of optimizations possible without
  489. * ZLPs. A whitelist is added in an attempt to avoid this for devices
  490. * known to conform to the MBIM specification.
  491. *
  492. * All known devices supporting NCM compatibility mode are also
  493. * conforming to the NCM and MBIM specifications. For this reason, the
  494. * NCM subclass entry is also in the ZLP whitelist.
  495. */
  496. static const struct driver_info cdc_mbim_info_zlp = {
  497. .description = "CDC MBIM",
  498. .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET | FLAG_WWAN | FLAG_SEND_ZLP,
  499. .bind = cdc_mbim_bind,
  500. .unbind = cdc_mbim_unbind,
  501. .manage_power = cdc_mbim_manage_power,
  502. .rx_fixup = cdc_mbim_rx_fixup,
  503. .tx_fixup = cdc_mbim_tx_fixup,
  504. };
  505. /* The spefication explicitly allows NDPs to be placed anywhere in the
  506. * frame, but some devices fail unless the NDP is placed after the IP
  507. * packets. Using the CDC_NCM_FLAG_NDP_TO_END flags to force this
  508. * behaviour.
  509. *
  510. * Note: The current implementation of this feature restricts each NTB
  511. * to a single NDP, implying that multiplexed sessions cannot share an
  512. * NTB. This might affect performace for multiplexed sessions.
  513. */
  514. static const struct driver_info cdc_mbim_info_ndp_to_end = {
  515. .description = "CDC MBIM",
  516. .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET | FLAG_WWAN,
  517. .bind = cdc_mbim_bind,
  518. .unbind = cdc_mbim_unbind,
  519. .manage_power = cdc_mbim_manage_power,
  520. .rx_fixup = cdc_mbim_rx_fixup,
  521. .tx_fixup = cdc_mbim_tx_fixup,
  522. .data = CDC_NCM_FLAG_NDP_TO_END,
  523. };
  524. /* Some modems (e.g. Telit LE922A6) do not work properly with altsetting
  525. * toggle done in cdc_ncm_bind_common. CDC_MBIM_FLAG_AVOID_ALTSETTING_TOGGLE
  526. * flag is used to avoid this procedure.
  527. */
  528. static const struct driver_info cdc_mbim_info_avoid_altsetting_toggle = {
  529. .description = "CDC MBIM",
  530. .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET | FLAG_WWAN | FLAG_SEND_ZLP,
  531. .bind = cdc_mbim_bind,
  532. .unbind = cdc_mbim_unbind,
  533. .manage_power = cdc_mbim_manage_power,
  534. .rx_fixup = cdc_mbim_rx_fixup,
  535. .tx_fixup = cdc_mbim_tx_fixup,
  536. .data = CDC_MBIM_FLAG_AVOID_ALTSETTING_TOGGLE,
  537. };
  538. static const struct usb_device_id mbim_devs[] = {
  539. /* This duplicate NCM entry is intentional. MBIM devices can
  540. * be disguised as NCM by default, and this is necessary to
  541. * allow us to bind the correct driver_info to such devices.
  542. *
  543. * bind() will sort out this for us, selecting the correct
  544. * entry and reject the other
  545. */
  546. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
  547. .driver_info = (unsigned long)&cdc_mbim_info,
  548. },
  549. /* ZLP conformance whitelist: All Ericsson MBIM devices */
  550. { USB_VENDOR_AND_INTERFACE_INFO(0x0bdb, USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
  551. .driver_info = (unsigned long)&cdc_mbim_info,
  552. },
  553. /* Some Huawei devices, ME906s-158 (12d1:15c1) and E3372
  554. * (12d1:157d), are known to fail unless the NDP is placed
  555. * after the IP packets. Applying the quirk to all Huawei
  556. * devices is broader than necessary, but harmless.
  557. */
  558. { USB_VENDOR_AND_INTERFACE_INFO(0x12d1, USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
  559. .driver_info = (unsigned long)&cdc_mbim_info_ndp_to_end,
  560. },
  561. /* The HP lt4132 (03f0:a31d) is a rebranded Huawei ME906s-158,
  562. * therefore it too requires the above "NDP to end" quirk.
  563. */
  564. { USB_DEVICE_AND_INTERFACE_INFO(0x03f0, 0xa31d, USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
  565. .driver_info = (unsigned long)&cdc_mbim_info_ndp_to_end,
  566. },
  567. /* Telit LE922A6 in MBIM composition */
  568. { USB_DEVICE_AND_INTERFACE_INFO(0x1bc7, 0x1041, USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
  569. .driver_info = (unsigned long)&cdc_mbim_info_avoid_altsetting_toggle,
  570. },
  571. /* Telit LN920 */
  572. { USB_DEVICE_AND_INTERFACE_INFO(0x1bc7, 0x1061, USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
  573. .driver_info = (unsigned long)&cdc_mbim_info_avoid_altsetting_toggle,
  574. },
  575. /* default entry */
  576. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
  577. .driver_info = (unsigned long)&cdc_mbim_info_zlp,
  578. },
  579. {
  580. },
  581. };
  582. MODULE_DEVICE_TABLE(usb, mbim_devs);
  583. static struct usb_driver cdc_mbim_driver = {
  584. .name = "cdc_mbim",
  585. .id_table = mbim_devs,
  586. .probe = usbnet_probe,
  587. .disconnect = usbnet_disconnect,
  588. .suspend = cdc_mbim_suspend,
  589. .resume = cdc_mbim_resume,
  590. .reset_resume = cdc_mbim_resume,
  591. .supports_autosuspend = 1,
  592. .disable_hub_initiated_lpm = 1,
  593. };
  594. module_usb_driver(cdc_mbim_driver);
  595. MODULE_AUTHOR("Greg Suarez <gsuarez@smithmicro.com>");
  596. MODULE_AUTHOR("Bjørn Mork <bjorn@mork.no>");
  597. MODULE_DESCRIPTION("USB CDC MBIM host driver");
  598. MODULE_LICENSE("GPL");