2 * Copyright (c) 2012 Smith Micro Software, Inc.
5 * This driver is based on and reuse most of cdc_ncm, which is
6 * Copyright (C) ST-Ericsson 2010-2012
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.
13 #include <linux/module.h>
14 #include <linux/netdevice.h>
15 #include <linux/ethtool.h>
16 #include <linux/if_vlan.h>
18 #include <linux/mii.h>
19 #include <linux/usb.h>
20 #include <linux/usb/cdc.h>
21 #include <linux/usb/usbnet.h>
22 #include <linux/usb/cdc-wdm.h>
23 #include <linux/usb/cdc_ncm.h>
25 #include <net/addrconf.h>
27 /* driver specific data - must match cdc_ncm usage */
28 struct cdc_mbim_state {
29 struct cdc_ncm_ctx *ctx;
31 struct usb_driver *subdriver;
32 struct usb_interface *control;
33 struct usb_interface *data;
36 /* using a counter to merge subdriver requests with our own into a combined state */
37 static int cdc_mbim_manage_power(struct usbnet *dev, int on)
39 struct cdc_mbim_state *info = (void *)&dev->data;
42 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;
49 usb_autopm_put_interface(dev->intf);
54 static int cdc_mbim_wdm_manage_power(struct usb_interface *intf, int status)
56 struct usbnet *dev = usb_get_intfdata(intf);
58 /* can be called while disconnecting */
62 return cdc_mbim_manage_power(dev, status);
66 static int cdc_mbim_bind(struct usbnet *dev, struct usb_interface *intf)
68 struct cdc_ncm_ctx *ctx;
69 struct usb_driver *subdriver = ERR_PTR(-ENODEV);
71 u8 data_altsetting = cdc_ncm_select_altsetting(dev, intf);
72 struct cdc_mbim_state *info = (void *)&dev->data;
74 /* Probably NCM, defer for cdc_ncm_bind */
75 if (!cdc_ncm_comm_intf_is_mbim(intf->cur_altsetting))
78 ret = cdc_ncm_bind_common(dev, intf, data_altsetting);
84 /* The MBIM descriptor and the status endpoint are required */
85 if (ctx->mbim_desc && dev->status)
86 subdriver = usb_cdc_wdm_register(ctx->control,
88 le16_to_cpu(ctx->mbim_desc->wMaxControlMessage),
89 cdc_mbim_wdm_manage_power);
90 if (IS_ERR(subdriver)) {
91 ret = PTR_ERR(subdriver);
92 cdc_ncm_unbind(dev, intf);
96 /* can't let usbnet use the interrupt endpoint */
98 info->subdriver = subdriver;
100 /* MBIM cannot do ARP */
101 dev->net->flags |= IFF_NOARP;
103 /* no need to put the VLAN tci in the packet headers */
104 dev->net->features |= NETIF_F_HW_VLAN_CTAG_TX;
109 static void cdc_mbim_unbind(struct usbnet *dev, struct usb_interface *intf)
111 struct cdc_mbim_state *info = (void *)&dev->data;
112 struct cdc_ncm_ctx *ctx = info->ctx;
114 /* disconnect subdriver from control interface */
115 if (info->subdriver && info->subdriver->disconnect)
116 info->subdriver->disconnect(ctx->control);
117 info->subdriver = NULL;
119 /* let NCM unbind clean up both control and data interface */
120 cdc_ncm_unbind(dev, intf);
124 static struct sk_buff *cdc_mbim_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
126 struct sk_buff *skb_out;
127 struct cdc_mbim_state *info = (void *)&dev->data;
128 struct cdc_ncm_ctx *ctx = info->ctx;
129 __le32 sign = cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN);
137 if (skb->len <= ETH_HLEN)
140 /* mapping VLANs to MBIM sessions:
141 * no tag => IPS session <0>
142 * 1 - 255 => IPS session <vlanid>
143 * 256 - 511 => DSS session <vlanid - 256>
144 * 512 - 4095 => unsupported, drop
146 vlan_get_tag(skb, &tci);
148 switch (tci & 0x0f00) {
149 case 0x0000: /* VLAN ID 0 - 255 */
150 /* verify that datagram is IPv4 or IPv6 */
151 skb_reset_mac_header(skb);
152 switch (eth_hdr(skb)->h_proto) {
153 case htons(ETH_P_IP):
154 case htons(ETH_P_IPV6):
162 case 0x0100: /* VLAN ID 256 - 511 */
163 sign = cpu_to_le32(USB_CDC_MBIM_NDP16_DSS_SIGN);
168 netif_err(dev, tx_err, dev->net,
169 "unsupported tci=0x%04x\n", tci);
172 skb_pull(skb, ETH_HLEN);
175 spin_lock_bh(&ctx->mtx);
176 skb_out = cdc_ncm_fill_tx_frame(dev, skb, sign);
177 spin_unlock_bh(&ctx->mtx);
182 dev_kfree_skb_any(skb);
187 /* Some devices are known to send Neigbor Solicitation messages and
188 * require Neigbor Advertisement replies. The IPv6 core will not
189 * respond since IFF_NOARP is set, so we must handle them ourselves.
191 static void do_neigh_solicit(struct usbnet *dev, u8 *buf, u16 tci)
193 struct ipv6hdr *iph = (void *)buf;
194 struct nd_msg *msg = (void *)(iph + 1);
195 struct net_device *netdev;
196 struct inet6_dev *in6_dev;
199 /* we'll only respond to requests from unicast addresses to
200 * our solicited node addresses.
202 if (!ipv6_addr_is_solict_mult(&iph->daddr) ||
203 !(ipv6_addr_type(&iph->saddr) & IPV6_ADDR_UNICAST))
206 /* need to send the NA on the VLAN dev, if any */
208 netdev = __vlan_find_dev_deep(dev->net, htons(ETH_P_8021Q),
215 in6_dev = in6_dev_get(netdev);
218 is_router = !!in6_dev->cnf.forwarding;
219 in6_dev_put(in6_dev);
221 /* ipv6_stub != NULL if in6_dev_get returned an inet6_dev */
222 ipv6_stub->ndisc_send_na(netdev, NULL, &iph->saddr, &msg->target,
223 is_router /* router */,
224 true /* solicited */,
225 false /* override */,
229 static bool is_neigh_solicit(u8 *buf, size_t len)
231 struct ipv6hdr *iph = (void *)buf;
232 struct nd_msg *msg = (void *)(iph + 1);
234 return (len >= sizeof(struct ipv6hdr) + sizeof(struct nd_msg) &&
235 iph->nexthdr == IPPROTO_ICMPV6 &&
236 msg->icmph.icmp6_code == 0 &&
237 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION);
241 static struct sk_buff *cdc_mbim_process_dgram(struct usbnet *dev, u8 *buf, size_t len, u16 tci)
243 __be16 proto = htons(ETH_P_802_3);
244 struct sk_buff *skb = NULL;
246 if (tci < 256) { /* IPS session? */
247 if (len < sizeof(struct iphdr))
250 switch (*buf & 0xf0) {
252 proto = htons(ETH_P_IP);
255 if (is_neigh_solicit(buf, len))
256 do_neigh_solicit(dev, buf, tci);
257 proto = htons(ETH_P_IPV6);
264 skb = netdev_alloc_skb_ip_align(dev->net, len + ETH_HLEN);
268 /* add an ethernet header */
269 skb_put(skb, ETH_HLEN);
270 skb_reset_mac_header(skb);
271 eth_hdr(skb)->h_proto = proto;
272 memset(eth_hdr(skb)->h_source, 0, ETH_ALEN);
273 memcpy(eth_hdr(skb)->h_dest, dev->net->dev_addr, ETH_ALEN);
276 memcpy(skb_put(skb, len), buf, len);
278 /* map MBIM session to VLAN */
280 vlan_put_tag(skb, htons(ETH_P_8021Q), tci);
285 static int cdc_mbim_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
288 struct cdc_mbim_state *info = (void *)&dev->data;
289 struct cdc_ncm_ctx *ctx = info->ctx;
294 struct usb_cdc_ncm_ndp16 *ndp16;
295 struct usb_cdc_ncm_dpe16 *dpe16;
297 int loopcount = 50; /* arbitrary max preventing infinite loop */
301 ndpoffset = cdc_ncm_rx_verify_nth16(ctx, skb_in);
306 nframes = cdc_ncm_rx_verify_ndp16(skb_in, ndpoffset);
310 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
312 switch (ndp16->dwSignature & cpu_to_le32(0x00ffffff)) {
313 case cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN):
314 c = (u8 *)&ndp16->dwSignature;
317 case cpu_to_le32(USB_CDC_MBIM_NDP16_DSS_SIGN):
318 c = (u8 *)&ndp16->dwSignature;
322 netif_dbg(dev, rx_err, dev->net,
323 "unsupported NDP signature <0x%08x>\n",
324 le32_to_cpu(ndp16->dwSignature));
329 dpe16 = ndp16->dpe16;
330 for (x = 0; x < nframes; x++, dpe16++) {
331 offset = le16_to_cpu(dpe16->wDatagramIndex);
332 len = le16_to_cpu(dpe16->wDatagramLength);
336 * All entries after first NULL entry are to be ignored
338 if ((offset == 0) || (len == 0)) {
340 goto err_ndp; /* empty NTB */
344 /* sanity checking */
345 if (((offset + len) > skb_in->len) || (len > ctx->rx_max)) {
346 netif_dbg(dev, rx_err, dev->net,
347 "invalid frame detected (ignored) offset[%u]=%u, length=%u, skb=%p\n",
348 x, offset, len, skb_in);
353 skb = cdc_mbim_process_dgram(dev, skb_in->data + offset, len, tci);
356 usbnet_skb_return(dev, skb);
360 /* are there more NDPs to process? */
361 ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
362 if (ndpoffset && loopcount--)
370 static int cdc_mbim_suspend(struct usb_interface *intf, pm_message_t message)
373 struct usbnet *dev = usb_get_intfdata(intf);
374 struct cdc_mbim_state *info = (void *)&dev->data;
375 struct cdc_ncm_ctx *ctx = info->ctx;
381 * Both usbnet_suspend() and subdriver->suspend() MUST return 0
382 * in system sleep context, otherwise, the resume callback has
383 * to recover device from previous suspend failure.
385 ret = usbnet_suspend(intf, message);
389 if (intf == ctx->control && info->subdriver && info->subdriver->suspend)
390 ret = info->subdriver->suspend(intf, message);
398 static int cdc_mbim_resume(struct usb_interface *intf)
401 struct usbnet *dev = usb_get_intfdata(intf);
402 struct cdc_mbim_state *info = (void *)&dev->data;
403 struct cdc_ncm_ctx *ctx = info->ctx;
404 bool callsub = (intf == ctx->control && info->subdriver && info->subdriver->resume);
407 ret = info->subdriver->resume(intf);
410 ret = usbnet_resume(intf);
411 if (ret < 0 && callsub)
412 info->subdriver->suspend(intf, PMSG_SUSPEND);
417 static const struct driver_info cdc_mbim_info = {
418 .description = "CDC MBIM",
419 .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET | FLAG_WWAN,
420 .bind = cdc_mbim_bind,
421 .unbind = cdc_mbim_unbind,
422 .manage_power = cdc_mbim_manage_power,
423 .rx_fixup = cdc_mbim_rx_fixup,
424 .tx_fixup = cdc_mbim_tx_fixup,
427 /* MBIM and NCM devices should not need a ZLP after NTBs with
428 * dwNtbOutMaxSize length. Nevertheless, a number of devices from
429 * different vendor IDs will fail unless we send ZLPs, forcing us
430 * to make this the default.
432 * This default may cause a performance penalty for spec conforming
433 * devices wanting to take advantage of optimizations possible without
434 * ZLPs. A whitelist is added in an attempt to avoid this for devices
435 * known to conform to the MBIM specification.
437 * All known devices supporting NCM compatibility mode are also
438 * conforming to the NCM and MBIM specifications. For this reason, the
439 * NCM subclass entry is also in the ZLP whitelist.
441 static const struct driver_info cdc_mbim_info_zlp = {
442 .description = "CDC MBIM",
443 .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET | FLAG_WWAN | FLAG_SEND_ZLP,
444 .bind = cdc_mbim_bind,
445 .unbind = cdc_mbim_unbind,
446 .manage_power = cdc_mbim_manage_power,
447 .rx_fixup = cdc_mbim_rx_fixup,
448 .tx_fixup = cdc_mbim_tx_fixup,
451 static const struct usb_device_id mbim_devs[] = {
452 /* This duplicate NCM entry is intentional. MBIM devices can
453 * be disguised as NCM by default, and this is necessary to
454 * allow us to bind the correct driver_info to such devices.
456 * bind() will sort out this for us, selecting the correct
457 * entry and reject the other
459 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
460 .driver_info = (unsigned long)&cdc_mbim_info,
462 /* ZLP conformance whitelist: All Ericsson MBIM devices */
463 { USB_VENDOR_AND_INTERFACE_INFO(0x0bdb, USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
464 .driver_info = (unsigned long)&cdc_mbim_info,
467 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
468 .driver_info = (unsigned long)&cdc_mbim_info_zlp,
473 MODULE_DEVICE_TABLE(usb, mbim_devs);
475 static struct usb_driver cdc_mbim_driver = {
477 .id_table = mbim_devs,
478 .probe = usbnet_probe,
479 .disconnect = usbnet_disconnect,
480 .suspend = cdc_mbim_suspend,
481 .resume = cdc_mbim_resume,
482 .reset_resume = cdc_mbim_resume,
483 .supports_autosuspend = 1,
484 .disable_hub_initiated_lpm = 1,
486 module_usb_driver(cdc_mbim_driver);
490 MODULE_DESCRIPTION("USB CDC MBIM host driver");
491 MODULE_LICENSE("GPL");