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