1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2012 Smith Micro Software, Inc.
4 * Copyright (c) 2012 Bjørn Mork <bjorn@mork.no>
6 * This driver is based on and reuse most of cdc_ncm, which is
7 * Copyright (C) ST-Ericsson 2010-2012
10 #include <linux/module.h>
11 #include <linux/netdevice.h>
12 #include <linux/ethtool.h>
13 #include <linux/if_vlan.h>
15 #include <linux/mii.h>
16 #include <linux/usb.h>
17 #include <linux/usb/cdc.h>
18 #include <linux/usb/usbnet.h>
19 #include <linux/usb/cdc-wdm.h>
20 #include <linux/usb/cdc_ncm.h>
22 #include <net/addrconf.h>
23 #include <net/ipv6_stubs.h>
25 /* alternative VLAN for IP session 0 if not untagged */
26 #define MBIM_IPS0_VID 4094
28 /* driver specific data - must match cdc_ncm usage */
29 struct cdc_mbim_state
{
30 struct cdc_ncm_ctx
*ctx
;
32 struct usb_driver
*subdriver
;
33 unsigned long _unused
;
37 /* flags for the cdc_mbim_state.flags field */
39 FLAG_IPS0_VLAN
= 1 << 0, /* IP session 0 is tagged */
42 /* using a counter to merge subdriver requests with our own into a combined state */
43 static int cdc_mbim_manage_power(struct usbnet
*dev
, int on
)
45 struct cdc_mbim_state
*info
= (void *)&dev
->data
;
48 dev_dbg(&dev
->intf
->dev
, "%s() pmcount=%d, on=%d\n", __func__
, atomic_read(&info
->pmcount
), on
);
50 if ((on
&& atomic_add_return(1, &info
->pmcount
) == 1) || (!on
&& atomic_dec_and_test(&info
->pmcount
))) {
51 /* need autopm_get/put here to ensure the usbcore sees the new value */
52 rv
= usb_autopm_get_interface(dev
->intf
);
53 dev
->intf
->needs_remote_wakeup
= on
;
55 usb_autopm_put_interface(dev
->intf
);
60 static int cdc_mbim_wdm_manage_power(struct usb_interface
*intf
, int status
)
62 struct usbnet
*dev
= usb_get_intfdata(intf
);
64 /* can be called while disconnecting */
68 return cdc_mbim_manage_power(dev
, status
);
71 static int cdc_mbim_rx_add_vid(struct net_device
*netdev
, __be16 proto
, u16 vid
)
73 struct usbnet
*dev
= netdev_priv(netdev
);
74 struct cdc_mbim_state
*info
= (void *)&dev
->data
;
76 /* creation of this VLAN is a request to tag IP session 0 */
77 if (vid
== MBIM_IPS0_VID
)
78 info
->flags
|= FLAG_IPS0_VLAN
;
80 if (vid
>= 512) /* we don't map these to MBIM session */
85 static int cdc_mbim_rx_kill_vid(struct net_device
*netdev
, __be16 proto
, u16 vid
)
87 struct usbnet
*dev
= netdev_priv(netdev
);
88 struct cdc_mbim_state
*info
= (void *)&dev
->data
;
90 /* this is a request for an untagged IP session 0 */
91 if (vid
== MBIM_IPS0_VID
)
92 info
->flags
&= ~FLAG_IPS0_VLAN
;
96 static const struct net_device_ops cdc_mbim_netdev_ops
= {
97 .ndo_open
= usbnet_open
,
98 .ndo_stop
= usbnet_stop
,
99 .ndo_start_xmit
= usbnet_start_xmit
,
100 .ndo_tx_timeout
= usbnet_tx_timeout
,
101 .ndo_get_stats64
= usbnet_get_stats64
,
102 .ndo_change_mtu
= cdc_ncm_change_mtu
,
103 .ndo_set_mac_address
= eth_mac_addr
,
104 .ndo_validate_addr
= eth_validate_addr
,
105 .ndo_vlan_rx_add_vid
= cdc_mbim_rx_add_vid
,
106 .ndo_vlan_rx_kill_vid
= cdc_mbim_rx_kill_vid
,
109 /* Change the control interface altsetting and update the .driver_info
110 * pointer if the matching entry after changing class codes points to
113 static int cdc_mbim_set_ctrlalt(struct usbnet
*dev
, struct usb_interface
*intf
, u8 alt
)
115 struct usb_driver
*driver
= to_usb_driver(intf
->dev
.driver
);
116 const struct usb_device_id
*id
;
117 struct driver_info
*info
;
120 ret
= usb_set_interface(dev
->udev
,
121 intf
->cur_altsetting
->desc
.bInterfaceNumber
,
126 id
= usb_match_id(intf
, driver
->id_table
);
130 info
= (struct driver_info
*)id
->driver_info
;
131 if (info
!= dev
->driver_info
) {
132 dev_dbg(&intf
->dev
, "driver_info updated to '%s'\n",
134 dev
->driver_info
= info
;
139 static int cdc_mbim_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
141 struct cdc_ncm_ctx
*ctx
;
142 struct usb_driver
*subdriver
= ERR_PTR(-ENODEV
);
144 u8 data_altsetting
= 1;
145 struct cdc_mbim_state
*info
= (void *)&dev
->data
;
147 /* should we change control altsetting on a NCM/MBIM function? */
148 if (cdc_ncm_select_altsetting(intf
) == CDC_NCM_COMM_ALTSETTING_MBIM
) {
149 data_altsetting
= CDC_NCM_DATA_ALTSETTING_MBIM
;
150 ret
= cdc_mbim_set_ctrlalt(dev
, intf
, CDC_NCM_COMM_ALTSETTING_MBIM
);
156 /* we will hit this for NCM/MBIM functions if prefer_mbim is false */
157 if (!cdc_ncm_comm_intf_is_mbim(intf
->cur_altsetting
))
160 ret
= cdc_ncm_bind_common(dev
, intf
, data_altsetting
, dev
->driver_info
->data
);
166 /* The MBIM descriptor and the status endpoint are required */
167 if (ctx
->mbim_desc
&& dev
->status
)
168 subdriver
= usb_cdc_wdm_register(ctx
->control
,
170 le16_to_cpu(ctx
->mbim_desc
->wMaxControlMessage
),
171 cdc_mbim_wdm_manage_power
);
172 if (IS_ERR(subdriver
)) {
173 ret
= PTR_ERR(subdriver
);
174 cdc_ncm_unbind(dev
, intf
);
178 /* can't let usbnet use the interrupt endpoint */
180 info
->subdriver
= subdriver
;
182 /* MBIM cannot do ARP */
183 dev
->net
->flags
|= IFF_NOARP
;
185 /* no need to put the VLAN tci in the packet headers */
186 dev
->net
->features
|= NETIF_F_HW_VLAN_CTAG_TX
| NETIF_F_HW_VLAN_CTAG_FILTER
;
188 /* monitor VLAN additions and removals */
189 dev
->net
->netdev_ops
= &cdc_mbim_netdev_ops
;
194 static void cdc_mbim_unbind(struct usbnet
*dev
, struct usb_interface
*intf
)
196 struct cdc_mbim_state
*info
= (void *)&dev
->data
;
197 struct cdc_ncm_ctx
*ctx
= info
->ctx
;
199 /* disconnect subdriver from control interface */
200 if (info
->subdriver
&& info
->subdriver
->disconnect
)
201 info
->subdriver
->disconnect(ctx
->control
);
202 info
->subdriver
= NULL
;
204 /* let NCM unbind clean up both control and data interface */
205 cdc_ncm_unbind(dev
, intf
);
208 /* verify that the ethernet protocol is IPv4 or IPv6 */
209 static bool is_ip_proto(__be16 proto
)
212 case htons(ETH_P_IP
):
213 case htons(ETH_P_IPV6
):
219 static struct sk_buff
*cdc_mbim_tx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
, gfp_t flags
)
221 struct sk_buff
*skb_out
;
222 struct cdc_mbim_state
*info
= (void *)&dev
->data
;
223 struct cdc_ncm_ctx
*ctx
= info
->ctx
;
224 __le32 sign
= cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN
);
233 if (skb
->len
<= ETH_HLEN
)
236 /* Some applications using e.g. packet sockets will
237 * bypass the VLAN acceleration and create tagged
238 * ethernet frames directly. We primarily look for
239 * the accelerated out-of-band tag, but fall back if
242 skb_reset_mac_header(skb
);
243 if (vlan_get_tag(skb
, &tci
) < 0 && skb
->len
> VLAN_ETH_HLEN
&&
244 __vlan_get_tag(skb
, &tci
) == 0) {
245 is_ip
= is_ip_proto(vlan_eth_hdr(skb
)->h_vlan_encapsulated_proto
);
246 skb_pull(skb
, VLAN_ETH_HLEN
);
248 is_ip
= is_ip_proto(eth_hdr(skb
)->h_proto
);
249 skb_pull(skb
, ETH_HLEN
);
252 /* Is IP session <0> tagged too? */
253 if (info
->flags
& FLAG_IPS0_VLAN
) {
254 /* drop all untagged packets */
257 /* map MBIM_IPS0_VID to IPS<0> */
258 if (tci
== MBIM_IPS0_VID
)
262 /* mapping VLANs to MBIM sessions:
263 * no tag => IPS session <0> if !FLAG_IPS0_VLAN
264 * 1 - 255 => IPS session <vlanid>
265 * 256 - 511 => DSS session <vlanid - 256>
266 * 512 - 4093 => unsupported, drop
267 * 4094 => IPS session <0> if FLAG_IPS0_VLAN
270 switch (tci
& 0x0f00) {
271 case 0x0000: /* VLAN ID 0 - 255 */
277 case 0x0100: /* VLAN ID 256 - 511 */
280 sign
= cpu_to_le32(USB_CDC_MBIM_NDP16_DSS_SIGN
);
285 netif_err(dev
, tx_err
, dev
->net
,
286 "unsupported tci=0x%04x\n", tci
);
291 spin_lock_bh(&ctx
->mtx
);
292 skb_out
= cdc_ncm_fill_tx_frame(dev
, skb
, sign
);
293 spin_unlock_bh(&ctx
->mtx
);
298 dev_kfree_skb_any(skb
);
303 /* Some devices are known to send Neigbor Solicitation messages and
304 * require Neigbor Advertisement replies. The IPv6 core will not
305 * respond since IFF_NOARP is set, so we must handle them ourselves.
307 static void do_neigh_solicit(struct usbnet
*dev
, u8
*buf
, u16 tci
)
309 struct ipv6hdr
*iph
= (void *)buf
;
310 struct nd_msg
*msg
= (void *)(iph
+ 1);
311 struct net_device
*netdev
;
312 struct inet6_dev
*in6_dev
;
315 /* we'll only respond to requests from unicast addresses to
316 * our solicited node addresses.
318 if (!ipv6_addr_is_solict_mult(&iph
->daddr
) ||
319 !(ipv6_addr_type(&iph
->saddr
) & IPV6_ADDR_UNICAST
))
322 /* need to send the NA on the VLAN dev, if any */
325 netdev
= __vlan_find_dev_deep_rcu(dev
->net
, htons(ETH_P_8021Q
),
337 in6_dev
= in6_dev_get(netdev
);
340 is_router
= !!in6_dev
->cnf
.forwarding
;
341 in6_dev_put(in6_dev
);
343 /* ipv6_stub != NULL if in6_dev_get returned an inet6_dev */
344 ipv6_stub
->ndisc_send_na(netdev
, &iph
->saddr
, &msg
->target
,
345 is_router
/* router */,
346 true /* solicited */,
347 false /* override */,
353 static bool is_neigh_solicit(u8
*buf
, size_t len
)
355 struct ipv6hdr
*iph
= (void *)buf
;
356 struct nd_msg
*msg
= (void *)(iph
+ 1);
358 return (len
>= sizeof(struct ipv6hdr
) + sizeof(struct nd_msg
) &&
359 iph
->nexthdr
== IPPROTO_ICMPV6
&&
360 msg
->icmph
.icmp6_code
== 0 &&
361 msg
->icmph
.icmp6_type
== NDISC_NEIGHBOUR_SOLICITATION
);
365 static struct sk_buff
*cdc_mbim_process_dgram(struct usbnet
*dev
, u8
*buf
, size_t len
, u16 tci
)
367 __be16 proto
= htons(ETH_P_802_3
);
368 struct sk_buff
*skb
= NULL
;
370 if (tci
< 256 || tci
== MBIM_IPS0_VID
) { /* IPS session? */
371 if (len
< sizeof(struct iphdr
))
374 switch (*buf
& 0xf0) {
376 proto
= htons(ETH_P_IP
);
379 if (is_neigh_solicit(buf
, len
))
380 do_neigh_solicit(dev
, buf
, tci
);
381 proto
= htons(ETH_P_IPV6
);
388 skb
= netdev_alloc_skb_ip_align(dev
->net
, len
+ ETH_HLEN
);
392 /* add an ethernet header */
393 skb_put(skb
, ETH_HLEN
);
394 skb_reset_mac_header(skb
);
395 eth_hdr(skb
)->h_proto
= proto
;
396 eth_zero_addr(eth_hdr(skb
)->h_source
);
397 memcpy(eth_hdr(skb
)->h_dest
, dev
->net
->dev_addr
, ETH_ALEN
);
400 skb_put_data(skb
, buf
, len
);
402 /* map MBIM session to VLAN */
404 __vlan_hwaccel_put_tag(skb
, htons(ETH_P_8021Q
), tci
);
409 static int cdc_mbim_rx_fixup(struct usbnet
*dev
, struct sk_buff
*skb_in
)
412 struct cdc_mbim_state
*info
= (void *)&dev
->data
;
413 struct cdc_ncm_ctx
*ctx
= info
->ctx
;
418 struct usb_cdc_ncm_ndp16
*ndp16
;
419 struct usb_cdc_ncm_dpe16
*dpe16
;
421 int loopcount
= 50; /* arbitrary max preventing infinite loop */
426 ndpoffset
= cdc_ncm_rx_verify_nth16(ctx
, skb_in
);
431 nframes
= cdc_ncm_rx_verify_ndp16(skb_in
, ndpoffset
);
435 ndp16
= (struct usb_cdc_ncm_ndp16
*)(skb_in
->data
+ ndpoffset
);
437 switch (ndp16
->dwSignature
& cpu_to_le32(0x00ffffff)) {
438 case cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN
):
439 c
= (u8
*)&ndp16
->dwSignature
;
441 /* tag IPS<0> packets too if MBIM_IPS0_VID exists */
442 if (!tci
&& info
->flags
& FLAG_IPS0_VLAN
)
445 case cpu_to_le32(USB_CDC_MBIM_NDP16_DSS_SIGN
):
446 c
= (u8
*)&ndp16
->dwSignature
;
450 netif_dbg(dev
, rx_err
, dev
->net
,
451 "unsupported NDP signature <0x%08x>\n",
452 le32_to_cpu(ndp16
->dwSignature
));
457 dpe16
= ndp16
->dpe16
;
458 for (x
= 0; x
< nframes
; x
++, dpe16
++) {
459 offset
= le16_to_cpu(dpe16
->wDatagramIndex
);
460 len
= le16_to_cpu(dpe16
->wDatagramLength
);
464 * All entries after first NULL entry are to be ignored
466 if ((offset
== 0) || (len
== 0)) {
468 goto err_ndp
; /* empty NTB */
472 /* sanity checking */
473 if (((offset
+ len
) > skb_in
->len
) || (len
> ctx
->rx_max
)) {
474 netif_dbg(dev
, rx_err
, dev
->net
,
475 "invalid frame detected (ignored) offset[%u]=%u, length=%u, skb=%p\n",
476 x
, offset
, len
, skb_in
);
481 skb
= cdc_mbim_process_dgram(dev
, skb_in
->data
+ offset
, len
, tci
);
484 usbnet_skb_return(dev
, skb
);
485 payload
+= len
; /* count payload bytes in this NTB */
489 /* are there more NDPs to process? */
490 ndpoffset
= le16_to_cpu(ndp16
->wNextNdpIndex
);
491 if (ndpoffset
&& loopcount
--)
495 ctx
->rx_overhead
+= skb_in
->len
- payload
;
503 static int cdc_mbim_suspend(struct usb_interface
*intf
, pm_message_t message
)
506 struct usbnet
*dev
= usb_get_intfdata(intf
);
507 struct cdc_mbim_state
*info
= (void *)&dev
->data
;
508 struct cdc_ncm_ctx
*ctx
= info
->ctx
;
514 * Both usbnet_suspend() and subdriver->suspend() MUST return 0
515 * in system sleep context, otherwise, the resume callback has
516 * to recover device from previous suspend failure.
518 ret
= usbnet_suspend(intf
, message
);
522 if (intf
== ctx
->control
&& info
->subdriver
&& info
->subdriver
->suspend
)
523 ret
= info
->subdriver
->suspend(intf
, message
);
531 static int cdc_mbim_resume(struct usb_interface
*intf
)
534 struct usbnet
*dev
= usb_get_intfdata(intf
);
535 struct cdc_mbim_state
*info
= (void *)&dev
->data
;
536 struct cdc_ncm_ctx
*ctx
= info
->ctx
;
537 bool callsub
= (intf
== ctx
->control
&& info
->subdriver
&& info
->subdriver
->resume
);
540 ret
= info
->subdriver
->resume(intf
);
543 ret
= usbnet_resume(intf
);
544 if (ret
< 0 && callsub
)
545 info
->subdriver
->suspend(intf
, PMSG_SUSPEND
);
550 static const struct driver_info cdc_mbim_info
= {
551 .description
= "CDC MBIM",
552 .flags
= FLAG_NO_SETINT
| FLAG_MULTI_PACKET
| FLAG_WWAN
,
553 .bind
= cdc_mbim_bind
,
554 .unbind
= cdc_mbim_unbind
,
555 .manage_power
= cdc_mbim_manage_power
,
556 .rx_fixup
= cdc_mbim_rx_fixup
,
557 .tx_fixup
= cdc_mbim_tx_fixup
,
560 /* MBIM and NCM devices should not need a ZLP after NTBs with
561 * dwNtbOutMaxSize length. Nevertheless, a number of devices from
562 * different vendor IDs will fail unless we send ZLPs, forcing us
563 * to make this the default.
565 * This default may cause a performance penalty for spec conforming
566 * devices wanting to take advantage of optimizations possible without
567 * ZLPs. A whitelist is added in an attempt to avoid this for devices
568 * known to conform to the MBIM specification.
570 * All known devices supporting NCM compatibility mode are also
571 * conforming to the NCM and MBIM specifications. For this reason, the
572 * NCM subclass entry is also in the ZLP whitelist.
574 static const struct driver_info cdc_mbim_info_zlp
= {
575 .description
= "CDC MBIM",
576 .flags
= FLAG_NO_SETINT
| FLAG_MULTI_PACKET
| FLAG_WWAN
| FLAG_SEND_ZLP
,
577 .bind
= cdc_mbim_bind
,
578 .unbind
= cdc_mbim_unbind
,
579 .manage_power
= cdc_mbim_manage_power
,
580 .rx_fixup
= cdc_mbim_rx_fixup
,
581 .tx_fixup
= cdc_mbim_tx_fixup
,
584 /* The spefication explicitly allows NDPs to be placed anywhere in the
585 * frame, but some devices fail unless the NDP is placed after the IP
586 * packets. Using the CDC_NCM_FLAG_NDP_TO_END flags to force this
589 * Note: The current implementation of this feature restricts each NTB
590 * to a single NDP, implying that multiplexed sessions cannot share an
591 * NTB. This might affect performace for multiplexed sessions.
593 static const struct driver_info cdc_mbim_info_ndp_to_end
= {
594 .description
= "CDC MBIM",
595 .flags
= FLAG_NO_SETINT
| FLAG_MULTI_PACKET
| FLAG_WWAN
,
596 .bind
= cdc_mbim_bind
,
597 .unbind
= cdc_mbim_unbind
,
598 .manage_power
= cdc_mbim_manage_power
,
599 .rx_fixup
= cdc_mbim_rx_fixup
,
600 .tx_fixup
= cdc_mbim_tx_fixup
,
601 .data
= CDC_NCM_FLAG_NDP_TO_END
,
604 /* Some modems (e.g. Telit LE922A6) do not work properly with altsetting
605 * toggle done in cdc_ncm_bind_common. CDC_MBIM_FLAG_AVOID_ALTSETTING_TOGGLE
606 * flag is used to avoid this procedure.
608 static const struct driver_info cdc_mbim_info_avoid_altsetting_toggle
= {
609 .description
= "CDC MBIM",
610 .flags
= FLAG_NO_SETINT
| FLAG_MULTI_PACKET
| FLAG_WWAN
| FLAG_SEND_ZLP
,
611 .bind
= cdc_mbim_bind
,
612 .unbind
= cdc_mbim_unbind
,
613 .manage_power
= cdc_mbim_manage_power
,
614 .rx_fixup
= cdc_mbim_rx_fixup
,
615 .tx_fixup
= cdc_mbim_tx_fixup
,
616 .data
= CDC_MBIM_FLAG_AVOID_ALTSETTING_TOGGLE
,
619 static const struct usb_device_id mbim_devs
[] = {
620 /* This duplicate NCM entry is intentional. MBIM devices can
621 * be disguised as NCM by default, and this is necessary to
622 * allow us to bind the correct driver_info to such devices.
624 * bind() will sort out this for us, selecting the correct
625 * entry and reject the other
627 { USB_INTERFACE_INFO(USB_CLASS_COMM
, USB_CDC_SUBCLASS_NCM
, USB_CDC_PROTO_NONE
),
628 .driver_info
= (unsigned long)&cdc_mbim_info
,
630 /* ZLP conformance whitelist: All Ericsson MBIM devices */
631 { USB_VENDOR_AND_INTERFACE_INFO(0x0bdb, USB_CLASS_COMM
, USB_CDC_SUBCLASS_MBIM
, USB_CDC_PROTO_NONE
),
632 .driver_info
= (unsigned long)&cdc_mbim_info
,
635 /* Some Huawei devices, ME906s-158 (12d1:15c1) and E3372
636 * (12d1:157d), are known to fail unless the NDP is placed
637 * after the IP packets. Applying the quirk to all Huawei
638 * devices is broader than necessary, but harmless.
640 { USB_VENDOR_AND_INTERFACE_INFO(0x12d1, USB_CLASS_COMM
, USB_CDC_SUBCLASS_MBIM
, USB_CDC_PROTO_NONE
),
641 .driver_info
= (unsigned long)&cdc_mbim_info_ndp_to_end
,
644 /* The HP lt4132 (03f0:a31d) is a rebranded Huawei ME906s-158,
645 * therefore it too requires the above "NDP to end" quirk.
647 { USB_DEVICE_AND_INTERFACE_INFO(0x03f0, 0xa31d, USB_CLASS_COMM
, USB_CDC_SUBCLASS_MBIM
, USB_CDC_PROTO_NONE
),
648 .driver_info
= (unsigned long)&cdc_mbim_info_ndp_to_end
,
651 /* Telit LE922A6 in MBIM composition */
652 { USB_DEVICE_AND_INTERFACE_INFO(0x1bc7, 0x1041, USB_CLASS_COMM
, USB_CDC_SUBCLASS_MBIM
, USB_CDC_PROTO_NONE
),
653 .driver_info
= (unsigned long)&cdc_mbim_info_avoid_altsetting_toggle
,
657 { USB_INTERFACE_INFO(USB_CLASS_COMM
, USB_CDC_SUBCLASS_MBIM
, USB_CDC_PROTO_NONE
),
658 .driver_info
= (unsigned long)&cdc_mbim_info_zlp
,
663 MODULE_DEVICE_TABLE(usb
, mbim_devs
);
665 static struct usb_driver cdc_mbim_driver
= {
667 .id_table
= mbim_devs
,
668 .probe
= usbnet_probe
,
669 .disconnect
= usbnet_disconnect
,
670 .suspend
= cdc_mbim_suspend
,
671 .resume
= cdc_mbim_resume
,
672 .reset_resume
= cdc_mbim_resume
,
673 .supports_autosuspend
= 1,
674 .disable_hub_initiated_lpm
= 1,
676 module_usb_driver(cdc_mbim_driver
);
678 MODULE_AUTHOR("Greg Suarez <gsuarez@smithmicro.com>");
679 MODULE_AUTHOR("Bjørn Mork <bjorn@mork.no>");
680 MODULE_DESCRIPTION("USB CDC MBIM host driver");
681 MODULE_LICENSE("GPL");