cdc_mbim.c 19 KB

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