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> |
| 24 | |
| 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 | struct usb_interface *control; |
| 31 | struct usb_interface *data; |
| 32 | }; |
| 33 | |
| 34 | /* using a counter to merge subdriver requests with our own into a combined state */ |
| 35 | static int cdc_mbim_manage_power(struct usbnet *dev, int on) |
| 36 | { |
| 37 | struct cdc_mbim_state *info = (void *)&dev->data; |
| 38 | int rv = 0; |
| 39 | |
| 40 | dev_dbg(&dev->intf->dev, "%s() pmcount=%d, on=%d\n", __func__, atomic_read(&info->pmcount), on); |
| 41 | |
| 42 | if ((on && atomic_add_return(1, &info->pmcount) == 1) || (!on && atomic_dec_and_test(&info->pmcount))) { |
| 43 | /* need autopm_get/put here to ensure the usbcore sees the new value */ |
| 44 | rv = usb_autopm_get_interface(dev->intf); |
| 45 | if (rv < 0) |
| 46 | goto err; |
| 47 | dev->intf->needs_remote_wakeup = on; |
| 48 | usb_autopm_put_interface(dev->intf); |
| 49 | } |
| 50 | err: |
| 51 | return rv; |
| 52 | } |
| 53 | |
| 54 | static int cdc_mbim_wdm_manage_power(struct usb_interface *intf, int status) |
| 55 | { |
| 56 | struct usbnet *dev = usb_get_intfdata(intf); |
| 57 | |
| 58 | /* can be called while disconnecting */ |
| 59 | if (!dev) |
| 60 | return 0; |
| 61 | |
| 62 | return cdc_mbim_manage_power(dev, status); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | static int cdc_mbim_bind(struct usbnet *dev, struct usb_interface *intf) |
| 67 | { |
| 68 | struct cdc_ncm_ctx *ctx; |
| 69 | struct usb_driver *subdriver = ERR_PTR(-ENODEV); |
| 70 | int ret = -ENODEV; |
| 71 | u8 data_altsetting = CDC_NCM_DATA_ALTSETTING_NCM; |
| 72 | struct cdc_mbim_state *info = (void *)&dev->data; |
| 73 | |
| 74 | /* see if interface supports MBIM alternate setting */ |
| 75 | if (intf->num_altsetting == 2) { |
| 76 | if (!cdc_ncm_comm_intf_is_mbim(intf->cur_altsetting)) |
| 77 | usb_set_interface(dev->udev, |
| 78 | intf->cur_altsetting->desc.bInterfaceNumber, |
| 79 | CDC_NCM_COMM_ALTSETTING_MBIM); |
| 80 | data_altsetting = CDC_NCM_DATA_ALTSETTING_MBIM; |
| 81 | } |
| 82 | |
| 83 | /* Probably NCM, defer for cdc_ncm_bind */ |
| 84 | if (!cdc_ncm_comm_intf_is_mbim(intf->cur_altsetting)) |
| 85 | goto err; |
| 86 | |
| 87 | ret = cdc_ncm_bind_common(dev, intf, data_altsetting); |
| 88 | if (ret) |
| 89 | goto err; |
| 90 | |
| 91 | ctx = info->ctx; |
| 92 | |
| 93 | /* The MBIM descriptor and the status endpoint are required */ |
| 94 | if (ctx->mbim_desc && dev->status) |
| 95 | subdriver = usb_cdc_wdm_register(ctx->control, |
| 96 | &dev->status->desc, |
| 97 | le16_to_cpu(ctx->mbim_desc->wMaxControlMessage), |
| 98 | cdc_mbim_wdm_manage_power); |
| 99 | if (IS_ERR(subdriver)) { |
| 100 | ret = PTR_ERR(subdriver); |
| 101 | cdc_ncm_unbind(dev, intf); |
| 102 | goto err; |
| 103 | } |
| 104 | |
| 105 | /* can't let usbnet use the interrupt endpoint */ |
| 106 | dev->status = NULL; |
| 107 | info->subdriver = subdriver; |
| 108 | |
| 109 | /* MBIM cannot do ARP */ |
| 110 | dev->net->flags |= IFF_NOARP; |
Bjørn Mork | a82c7ce | 2012-10-22 10:56:39 +0000 | [diff] [blame^] | 111 | |
| 112 | /* no need to put the VLAN tci in the packet headers */ |
| 113 | dev->net->features |= NETIF_F_HW_VLAN_TX; |
Greg Suarez | 9bf211a3 | 2012-10-22 10:56:36 +0000 | [diff] [blame] | 114 | err: |
| 115 | return ret; |
| 116 | } |
| 117 | |
| 118 | static void cdc_mbim_unbind(struct usbnet *dev, struct usb_interface *intf) |
| 119 | { |
| 120 | struct cdc_mbim_state *info = (void *)&dev->data; |
| 121 | struct cdc_ncm_ctx *ctx = info->ctx; |
| 122 | |
| 123 | /* disconnect subdriver from control interface */ |
| 124 | if (info->subdriver && info->subdriver->disconnect) |
| 125 | info->subdriver->disconnect(ctx->control); |
| 126 | info->subdriver = NULL; |
| 127 | |
| 128 | /* let NCM unbind clean up both control and data interface */ |
| 129 | cdc_ncm_unbind(dev, intf); |
| 130 | } |
| 131 | |
| 132 | |
| 133 | static struct sk_buff *cdc_mbim_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags) |
| 134 | { |
| 135 | struct sk_buff *skb_out; |
| 136 | struct cdc_mbim_state *info = (void *)&dev->data; |
| 137 | struct cdc_ncm_ctx *ctx = info->ctx; |
Bjørn Mork | a82c7ce | 2012-10-22 10:56:39 +0000 | [diff] [blame^] | 138 | __le32 sign = cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN); |
| 139 | u16 tci = 0; |
| 140 | u8 *c; |
Greg Suarez | 9bf211a3 | 2012-10-22 10:56:36 +0000 | [diff] [blame] | 141 | |
| 142 | if (!ctx) |
| 143 | goto error; |
| 144 | |
| 145 | if (skb) { |
| 146 | if (skb->len <= sizeof(ETH_HLEN)) |
| 147 | goto error; |
| 148 | |
Bjørn Mork | a82c7ce | 2012-10-22 10:56:39 +0000 | [diff] [blame^] | 149 | /* mapping VLANs to MBIM sessions: |
| 150 | * no tag => IPS session <0> |
| 151 | * 1 - 255 => IPS session <vlanid> |
| 152 | * 256 - 4095 => unsupported, drop |
| 153 | */ |
| 154 | vlan_get_tag(skb, &tci); |
| 155 | |
| 156 | switch (tci & 0x0f00) { |
| 157 | case 0x0000: /* VLAN ID 0 - 255 */ |
| 158 | c = (u8 *)&sign; |
| 159 | c[3] = tci; |
| 160 | break; |
| 161 | default: |
| 162 | netif_err(dev, tx_err, dev->net, |
| 163 | "unsupported tci=0x%04x\n", tci); |
| 164 | goto error; |
| 165 | } |
| 166 | |
Greg Suarez | 9bf211a3 | 2012-10-22 10:56:36 +0000 | [diff] [blame] | 167 | skb_reset_mac_header(skb); |
| 168 | switch (eth_hdr(skb)->h_proto) { |
| 169 | case htons(ETH_P_IP): |
| 170 | case htons(ETH_P_IPV6): |
| 171 | skb_pull(skb, ETH_HLEN); |
| 172 | break; |
| 173 | default: |
| 174 | goto error; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | spin_lock_bh(&ctx->mtx); |
Bjørn Mork | a82c7ce | 2012-10-22 10:56:39 +0000 | [diff] [blame^] | 179 | skb_out = cdc_ncm_fill_tx_frame(ctx, skb, sign); |
Greg Suarez | 9bf211a3 | 2012-10-22 10:56:36 +0000 | [diff] [blame] | 180 | spin_unlock_bh(&ctx->mtx); |
| 181 | return skb_out; |
| 182 | |
| 183 | error: |
| 184 | if (skb) |
| 185 | dev_kfree_skb_any(skb); |
| 186 | |
| 187 | return NULL; |
| 188 | } |
| 189 | |
Bjørn Mork | a82c7ce | 2012-10-22 10:56:39 +0000 | [diff] [blame^] | 190 | 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] | 191 | { |
| 192 | __be16 proto; |
| 193 | struct sk_buff *skb = NULL; |
| 194 | |
Bjørn Mork | a82c7ce | 2012-10-22 10:56:39 +0000 | [diff] [blame^] | 195 | if (len < sizeof(struct iphdr)) |
| 196 | goto err; |
| 197 | |
Greg Suarez | 9bf211a3 | 2012-10-22 10:56:36 +0000 | [diff] [blame] | 198 | switch (*buf & 0xf0) { |
| 199 | case 0x40: |
| 200 | proto = htons(ETH_P_IP); |
| 201 | break; |
| 202 | case 0x60: |
| 203 | proto = htons(ETH_P_IPV6); |
| 204 | break; |
| 205 | default: |
| 206 | goto err; |
| 207 | } |
| 208 | |
| 209 | skb = netdev_alloc_skb_ip_align(dev->net, len + ETH_HLEN); |
| 210 | if (!skb) |
| 211 | goto err; |
| 212 | |
| 213 | /* add an ethernet header */ |
| 214 | skb_put(skb, ETH_HLEN); |
| 215 | skb_reset_mac_header(skb); |
| 216 | eth_hdr(skb)->h_proto = proto; |
| 217 | memset(eth_hdr(skb)->h_source, 0, ETH_ALEN); |
| 218 | memcpy(eth_hdr(skb)->h_dest, dev->net->dev_addr, ETH_ALEN); |
| 219 | |
| 220 | /* add datagram */ |
| 221 | memcpy(skb_put(skb, len), buf, len); |
Bjørn Mork | a82c7ce | 2012-10-22 10:56:39 +0000 | [diff] [blame^] | 222 | |
| 223 | /* map MBIM session to VLAN */ |
| 224 | if (tci) |
| 225 | vlan_put_tag(skb, tci); |
Greg Suarez | 9bf211a3 | 2012-10-22 10:56:36 +0000 | [diff] [blame] | 226 | err: |
| 227 | return skb; |
| 228 | } |
| 229 | |
| 230 | static int cdc_mbim_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in) |
| 231 | { |
| 232 | struct sk_buff *skb; |
Bjørn Mork | a82c7ce | 2012-10-22 10:56:39 +0000 | [diff] [blame^] | 233 | struct cdc_mbim_state *info = (void *)&dev->data; |
| 234 | struct cdc_ncm_ctx *ctx = info->ctx; |
Greg Suarez | 9bf211a3 | 2012-10-22 10:56:36 +0000 | [diff] [blame] | 235 | int len; |
| 236 | int nframes; |
| 237 | int x; |
| 238 | int offset; |
| 239 | struct usb_cdc_ncm_ndp16 *ndp16; |
| 240 | struct usb_cdc_ncm_dpe16 *dpe16; |
| 241 | int ndpoffset; |
| 242 | int loopcount = 50; /* arbitrary max preventing infinite loop */ |
Bjørn Mork | a82c7ce | 2012-10-22 10:56:39 +0000 | [diff] [blame^] | 243 | u8 *c; |
| 244 | u16 tci; |
Greg Suarez | 9bf211a3 | 2012-10-22 10:56:36 +0000 | [diff] [blame] | 245 | |
| 246 | ndpoffset = cdc_ncm_rx_verify_nth16(ctx, skb_in); |
| 247 | if (ndpoffset < 0) |
| 248 | goto error; |
| 249 | |
| 250 | next_ndp: |
| 251 | nframes = cdc_ncm_rx_verify_ndp16(skb_in, ndpoffset); |
| 252 | if (nframes < 0) |
| 253 | goto error; |
| 254 | |
| 255 | ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset); |
| 256 | |
Bjørn Mork | a82c7ce | 2012-10-22 10:56:39 +0000 | [diff] [blame^] | 257 | switch (ndp16->dwSignature & cpu_to_le32(0x00ffffff)) { |
Greg Suarez | 9bf211a3 | 2012-10-22 10:56:36 +0000 | [diff] [blame] | 258 | case cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN): |
Bjørn Mork | a82c7ce | 2012-10-22 10:56:39 +0000 | [diff] [blame^] | 259 | c = (u8 *)&ndp16->dwSignature; |
| 260 | tci = c[3]; |
Greg Suarez | 9bf211a3 | 2012-10-22 10:56:36 +0000 | [diff] [blame] | 261 | break; |
| 262 | default: |
| 263 | netif_dbg(dev, rx_err, dev->net, |
| 264 | "unsupported NDP signature <0x%08x>\n", |
| 265 | le32_to_cpu(ndp16->dwSignature)); |
| 266 | goto err_ndp; |
| 267 | |
| 268 | } |
| 269 | |
| 270 | dpe16 = ndp16->dpe16; |
| 271 | for (x = 0; x < nframes; x++, dpe16++) { |
| 272 | offset = le16_to_cpu(dpe16->wDatagramIndex); |
| 273 | len = le16_to_cpu(dpe16->wDatagramLength); |
| 274 | |
| 275 | /* |
| 276 | * CDC NCM ch. 3.7 |
| 277 | * All entries after first NULL entry are to be ignored |
| 278 | */ |
| 279 | if ((offset == 0) || (len == 0)) { |
| 280 | if (!x) |
| 281 | goto err_ndp; /* empty NTB */ |
| 282 | break; |
| 283 | } |
| 284 | |
| 285 | /* sanity checking */ |
Bjørn Mork | a82c7ce | 2012-10-22 10:56:39 +0000 | [diff] [blame^] | 286 | if (((offset + len) > skb_in->len) || (len > ctx->rx_max)) { |
Greg Suarez | 9bf211a3 | 2012-10-22 10:56:36 +0000 | [diff] [blame] | 287 | netif_dbg(dev, rx_err, dev->net, |
| 288 | "invalid frame detected (ignored) offset[%u]=%u, length=%u, skb=%p\n", |
| 289 | x, offset, len, skb_in); |
| 290 | if (!x) |
| 291 | goto err_ndp; |
| 292 | break; |
| 293 | } else { |
Bjørn Mork | a82c7ce | 2012-10-22 10:56:39 +0000 | [diff] [blame^] | 294 | skb = cdc_mbim_process_dgram(dev, skb_in->data + offset, len, tci); |
Greg Suarez | 9bf211a3 | 2012-10-22 10:56:36 +0000 | [diff] [blame] | 295 | if (!skb) |
| 296 | goto error; |
| 297 | usbnet_skb_return(dev, skb); |
| 298 | } |
| 299 | } |
| 300 | err_ndp: |
| 301 | /* are there more NDPs to process? */ |
| 302 | ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex); |
| 303 | if (ndpoffset && loopcount--) |
| 304 | goto next_ndp; |
| 305 | |
| 306 | return 1; |
| 307 | error: |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | static int cdc_mbim_suspend(struct usb_interface *intf, pm_message_t message) |
| 312 | { |
| 313 | int ret = 0; |
| 314 | struct usbnet *dev = usb_get_intfdata(intf); |
| 315 | struct cdc_mbim_state *info = (void *)&dev->data; |
| 316 | struct cdc_ncm_ctx *ctx = info->ctx; |
| 317 | |
| 318 | if (ctx == NULL) { |
| 319 | ret = -1; |
| 320 | goto error; |
| 321 | } |
| 322 | |
| 323 | ret = usbnet_suspend(intf, message); |
| 324 | if (ret < 0) |
| 325 | goto error; |
| 326 | |
| 327 | if (intf == ctx->control && info->subdriver && info->subdriver->suspend) |
| 328 | ret = info->subdriver->suspend(intf, message); |
| 329 | if (ret < 0) |
| 330 | usbnet_resume(intf); |
| 331 | |
| 332 | error: |
| 333 | return ret; |
| 334 | } |
| 335 | |
| 336 | static int cdc_mbim_resume(struct usb_interface *intf) |
| 337 | { |
| 338 | int ret = 0; |
| 339 | struct usbnet *dev = usb_get_intfdata(intf); |
| 340 | struct cdc_mbim_state *info = (void *)&dev->data; |
| 341 | struct cdc_ncm_ctx *ctx = info->ctx; |
| 342 | bool callsub = (intf == ctx->control && info->subdriver && info->subdriver->resume); |
| 343 | |
| 344 | if (callsub) |
| 345 | ret = info->subdriver->resume(intf); |
| 346 | if (ret < 0) |
| 347 | goto err; |
| 348 | ret = usbnet_resume(intf); |
| 349 | if (ret < 0 && callsub && info->subdriver->suspend) |
| 350 | info->subdriver->suspend(intf, PMSG_SUSPEND); |
| 351 | err: |
| 352 | return ret; |
| 353 | } |
| 354 | |
| 355 | static const struct driver_info cdc_mbim_info = { |
| 356 | .description = "CDC MBIM", |
| 357 | .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET | FLAG_WWAN, |
| 358 | .bind = cdc_mbim_bind, |
| 359 | .unbind = cdc_mbim_unbind, |
| 360 | .manage_power = cdc_mbim_manage_power, |
| 361 | .rx_fixup = cdc_mbim_rx_fixup, |
| 362 | .tx_fixup = cdc_mbim_tx_fixup, |
| 363 | }; |
| 364 | |
| 365 | static const struct usb_device_id mbim_devs[] = { |
| 366 | /* This duplicate NCM entry is intentional. MBIM devices can |
| 367 | * be disguised as NCM by default, and this is necessary to |
| 368 | * allow us to bind the correct driver_info to such devices. |
| 369 | * |
| 370 | * bind() will sort out this for us, selecting the correct |
| 371 | * entry and reject the other |
| 372 | */ |
| 373 | { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE), |
| 374 | .driver_info = (unsigned long)&cdc_mbim_info, |
| 375 | }, |
| 376 | { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE), |
| 377 | .driver_info = (unsigned long)&cdc_mbim_info, |
| 378 | }, |
| 379 | { |
| 380 | }, |
| 381 | }; |
| 382 | MODULE_DEVICE_TABLE(usb, mbim_devs); |
| 383 | |
| 384 | static struct usb_driver cdc_mbim_driver = { |
| 385 | .name = "cdc_mbim", |
| 386 | .id_table = mbim_devs, |
| 387 | .probe = usbnet_probe, |
| 388 | .disconnect = usbnet_disconnect, |
| 389 | .suspend = cdc_mbim_suspend, |
| 390 | .resume = cdc_mbim_resume, |
| 391 | .reset_resume = cdc_mbim_resume, |
| 392 | .supports_autosuspend = 1, |
| 393 | .disable_hub_initiated_lpm = 1, |
| 394 | }; |
| 395 | module_usb_driver(cdc_mbim_driver); |
| 396 | |
| 397 | MODULE_AUTHOR("Greg Suarez <gsuarez@smithmicro.com>"); |
| 398 | MODULE_AUTHOR("Bjørn Mork <bjorn@mork.no>"); |
| 399 | MODULE_DESCRIPTION("USB CDC MBIM host driver"); |
| 400 | MODULE_LICENSE("GPL"); |