| 1 | /* |
| 2 | * cdc_ncm.c |
| 3 | * |
| 4 | * Copyright (C) ST-Ericsson 2010-2012 |
| 5 | * Contact: Alexey Orishko <alexey.orishko@stericsson.com> |
| 6 | * Original author: Hans Petter Selasky <hans.petter.selasky@stericsson.com> |
| 7 | * |
| 8 | * USB Host Driver for Network Control Model (NCM) |
| 9 | * http://www.usb.org/developers/devclass_docs/NCM10.zip |
| 10 | * |
| 11 | * The NCM encoding, decoding and initialization logic |
| 12 | * derives from FreeBSD 8.x. if_cdce.c and if_cdcereg.h |
| 13 | * |
| 14 | * This software is available to you under a choice of one of two |
| 15 | * licenses. You may choose this file to be licensed under the terms |
| 16 | * of the GNU General Public License (GPL) Version 2 or the 2-clause |
| 17 | * BSD license listed below: |
| 18 | * |
| 19 | * Redistribution and use in source and binary forms, with or without |
| 20 | * modification, are permitted provided that the following conditions |
| 21 | * are met: |
| 22 | * 1. Redistributions of source code must retain the above copyright |
| 23 | * notice, this list of conditions and the following disclaimer. |
| 24 | * 2. Redistributions in binary form must reproduce the above copyright |
| 25 | * notice, this list of conditions and the following disclaimer in the |
| 26 | * documentation and/or other materials provided with the distribution. |
| 27 | * |
| 28 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 29 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 31 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 32 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 33 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 34 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 37 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 38 | * SUCH DAMAGE. |
| 39 | */ |
| 40 | |
| 41 | #include <linux/module.h> |
| 42 | #include <linux/netdevice.h> |
| 43 | #include <linux/ctype.h> |
| 44 | #include <linux/ethtool.h> |
| 45 | #include <linux/workqueue.h> |
| 46 | #include <linux/mii.h> |
| 47 | #include <linux/crc32.h> |
| 48 | #include <linux/usb.h> |
| 49 | #include <linux/hrtimer.h> |
| 50 | #include <linux/atomic.h> |
| 51 | #include <linux/usb/usbnet.h> |
| 52 | #include <linux/usb/cdc.h> |
| 53 | #include <linux/usb/cdc_ncm.h> |
| 54 | |
| 55 | #if IS_ENABLED(CONFIG_USB_NET_CDC_MBIM) |
| 56 | static bool prefer_mbim = true; |
| 57 | #else |
| 58 | static bool prefer_mbim; |
| 59 | #endif |
| 60 | module_param(prefer_mbim, bool, S_IRUGO | S_IWUSR); |
| 61 | MODULE_PARM_DESC(prefer_mbim, "Prefer MBIM setting on dual NCM/MBIM functions"); |
| 62 | |
| 63 | static void cdc_ncm_txpath_bh(unsigned long param); |
| 64 | static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx); |
| 65 | static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *hr_timer); |
| 66 | static struct usb_driver cdc_ncm_driver; |
| 67 | |
| 68 | /* handle rx_max and tx_max changes */ |
| 69 | static void cdc_ncm_update_rxtx_max(struct usbnet *dev, u32 new_rx, u32 new_tx) |
| 70 | { |
| 71 | struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0]; |
| 72 | u8 iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber; |
| 73 | u32 val, max, min; |
| 74 | |
| 75 | /* clamp new_rx to sane values */ |
| 76 | min = USB_CDC_NCM_NTB_MIN_IN_SIZE; |
| 77 | max = min_t(u32, CDC_NCM_NTB_MAX_SIZE_RX, le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize)); |
| 78 | |
| 79 | /* dwNtbInMaxSize spec violation? Use MIN size for both limits */ |
| 80 | if (max < min) { |
| 81 | dev_warn(&dev->intf->dev, "dwNtbInMaxSize=%u is too small. Using %u\n", |
| 82 | le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize), min); |
| 83 | max = min; |
| 84 | } |
| 85 | |
| 86 | val = clamp_t(u32, new_rx, min, max); |
| 87 | if (val != new_rx) { |
| 88 | dev_dbg(&dev->intf->dev, "rx_max must be in the [%u, %u] range. Using %u\n", |
| 89 | min, max, val); |
| 90 | } |
| 91 | |
| 92 | /* inform device about NTB input size changes */ |
| 93 | if (val != ctx->rx_max) { |
| 94 | __le32 dwNtbInMaxSize = cpu_to_le32(val); |
| 95 | |
| 96 | dev_info(&dev->intf->dev, "setting rx_max = %u\n", val); |
| 97 | if (usbnet_write_cmd(dev, USB_CDC_SET_NTB_INPUT_SIZE, |
| 98 | USB_TYPE_CLASS | USB_DIR_OUT |
| 99 | | USB_RECIP_INTERFACE, |
| 100 | 0, iface_no, &dwNtbInMaxSize, 4) < 0) |
| 101 | dev_dbg(&dev->intf->dev, "Setting NTB Input Size failed\n"); |
| 102 | else |
| 103 | ctx->rx_max = val; |
| 104 | } |
| 105 | |
| 106 | /* clamp new_tx to sane values */ |
| 107 | min = CDC_NCM_MIN_HDR_SIZE + ctx->max_datagram_size; |
| 108 | max = min_t(u32, CDC_NCM_NTB_MAX_SIZE_TX, le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize)); |
| 109 | |
| 110 | /* some devices set dwNtbOutMaxSize too low for the above default */ |
| 111 | min = min(min, max); |
| 112 | |
| 113 | val = clamp_t(u32, new_tx, min, max); |
| 114 | if (val != new_tx) { |
| 115 | dev_dbg(&dev->intf->dev, "tx_max must be in the [%u, %u] range. Using %u\n", |
| 116 | min, max, val); |
| 117 | } |
| 118 | if (val != ctx->tx_max) |
| 119 | dev_info(&dev->intf->dev, "setting tx_max = %u\n", val); |
| 120 | ctx->tx_max = val; |
| 121 | } |
| 122 | |
| 123 | static int cdc_ncm_setup(struct usbnet *dev) |
| 124 | { |
| 125 | struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0]; |
| 126 | u32 val; |
| 127 | u8 flags; |
| 128 | u8 iface_no; |
| 129 | int err; |
| 130 | int eth_hlen; |
| 131 | u16 mbim_mtu; |
| 132 | u16 ntb_fmt_supported; |
| 133 | __le16 max_datagram_size; |
| 134 | |
| 135 | iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber; |
| 136 | |
| 137 | err = usbnet_read_cmd(dev, USB_CDC_GET_NTB_PARAMETERS, |
| 138 | USB_TYPE_CLASS | USB_DIR_IN |
| 139 | |USB_RECIP_INTERFACE, |
| 140 | 0, iface_no, &ctx->ncm_parm, |
| 141 | sizeof(ctx->ncm_parm)); |
| 142 | if (err < 0) { |
| 143 | dev_err(&dev->intf->dev, "failed GET_NTB_PARAMETERS\n"); |
| 144 | return err; /* GET_NTB_PARAMETERS is required */ |
| 145 | } |
| 146 | |
| 147 | /* read correct set of parameters according to device mode */ |
| 148 | ctx->rx_max = le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize); |
| 149 | ctx->tx_max = le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize); |
| 150 | ctx->tx_remainder = le16_to_cpu(ctx->ncm_parm.wNdpOutPayloadRemainder); |
| 151 | ctx->tx_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutDivisor); |
| 152 | ctx->tx_ndp_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutAlignment); |
| 153 | /* devices prior to NCM Errata shall set this field to zero */ |
| 154 | ctx->tx_max_datagrams = le16_to_cpu(ctx->ncm_parm.wNtbOutMaxDatagrams); |
| 155 | ntb_fmt_supported = le16_to_cpu(ctx->ncm_parm.bmNtbFormatsSupported); |
| 156 | |
| 157 | /* there are some minor differences in NCM and MBIM defaults */ |
| 158 | if (cdc_ncm_comm_intf_is_mbim(ctx->control->cur_altsetting)) { |
| 159 | if (!ctx->mbim_desc) |
| 160 | return -EINVAL; |
| 161 | eth_hlen = 0; |
| 162 | flags = ctx->mbim_desc->bmNetworkCapabilities; |
| 163 | ctx->max_datagram_size = le16_to_cpu(ctx->mbim_desc->wMaxSegmentSize); |
| 164 | if (ctx->max_datagram_size < CDC_MBIM_MIN_DATAGRAM_SIZE) |
| 165 | ctx->max_datagram_size = CDC_MBIM_MIN_DATAGRAM_SIZE; |
| 166 | } else { |
| 167 | if (!ctx->func_desc) |
| 168 | return -EINVAL; |
| 169 | eth_hlen = ETH_HLEN; |
| 170 | flags = ctx->func_desc->bmNetworkCapabilities; |
| 171 | ctx->max_datagram_size = le16_to_cpu(ctx->ether_desc->wMaxSegmentSize); |
| 172 | if (ctx->max_datagram_size < CDC_NCM_MIN_DATAGRAM_SIZE) |
| 173 | ctx->max_datagram_size = CDC_NCM_MIN_DATAGRAM_SIZE; |
| 174 | } |
| 175 | |
| 176 | /* common absolute max for NCM and MBIM */ |
| 177 | if (ctx->max_datagram_size > CDC_NCM_MAX_DATAGRAM_SIZE) |
| 178 | ctx->max_datagram_size = CDC_NCM_MAX_DATAGRAM_SIZE; |
| 179 | |
| 180 | dev_dbg(&dev->intf->dev, |
| 181 | "dwNtbInMaxSize=%u dwNtbOutMaxSize=%u wNdpOutPayloadRemainder=%u wNdpOutDivisor=%u wNdpOutAlignment=%u wNtbOutMaxDatagrams=%u flags=0x%x\n", |
| 182 | ctx->rx_max, ctx->tx_max, ctx->tx_remainder, ctx->tx_modulus, |
| 183 | ctx->tx_ndp_modulus, ctx->tx_max_datagrams, flags); |
| 184 | |
| 185 | /* max count of tx datagrams */ |
| 186 | if ((ctx->tx_max_datagrams == 0) || |
| 187 | (ctx->tx_max_datagrams > CDC_NCM_DPT_DATAGRAMS_MAX)) |
| 188 | ctx->tx_max_datagrams = CDC_NCM_DPT_DATAGRAMS_MAX; |
| 189 | |
| 190 | /* clamp rx_max and tx_max and inform device */ |
| 191 | cdc_ncm_update_rxtx_max(dev, le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize), le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize)); |
| 192 | |
| 193 | /* |
| 194 | * verify that the structure alignment is: |
| 195 | * - power of two |
| 196 | * - not greater than the maximum transmit length |
| 197 | * - not less than four bytes |
| 198 | */ |
| 199 | val = ctx->tx_ndp_modulus; |
| 200 | |
| 201 | if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) || |
| 202 | (val != ((-val) & val)) || (val >= ctx->tx_max)) { |
| 203 | dev_dbg(&dev->intf->dev, "Using default alignment: 4 bytes\n"); |
| 204 | ctx->tx_ndp_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE; |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * verify that the payload alignment is: |
| 209 | * - power of two |
| 210 | * - not greater than the maximum transmit length |
| 211 | * - not less than four bytes |
| 212 | */ |
| 213 | val = ctx->tx_modulus; |
| 214 | |
| 215 | if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) || |
| 216 | (val != ((-val) & val)) || (val >= ctx->tx_max)) { |
| 217 | dev_dbg(&dev->intf->dev, "Using default transmit modulus: 4 bytes\n"); |
| 218 | ctx->tx_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE; |
| 219 | } |
| 220 | |
| 221 | /* verify the payload remainder */ |
| 222 | if (ctx->tx_remainder >= ctx->tx_modulus) { |
| 223 | dev_dbg(&dev->intf->dev, "Using default transmit remainder: 0 bytes\n"); |
| 224 | ctx->tx_remainder = 0; |
| 225 | } |
| 226 | |
| 227 | /* adjust TX-remainder according to NCM specification. */ |
| 228 | ctx->tx_remainder = ((ctx->tx_remainder - eth_hlen) & |
| 229 | (ctx->tx_modulus - 1)); |
| 230 | |
| 231 | /* additional configuration */ |
| 232 | |
| 233 | /* set CRC Mode */ |
| 234 | if (flags & USB_CDC_NCM_NCAP_CRC_MODE) { |
| 235 | err = usbnet_write_cmd(dev, USB_CDC_SET_CRC_MODE, |
| 236 | USB_TYPE_CLASS | USB_DIR_OUT |
| 237 | | USB_RECIP_INTERFACE, |
| 238 | USB_CDC_NCM_CRC_NOT_APPENDED, |
| 239 | iface_no, NULL, 0); |
| 240 | if (err < 0) |
| 241 | dev_dbg(&dev->intf->dev, "Setting CRC mode off failed\n"); |
| 242 | } |
| 243 | |
| 244 | /* set NTB format, if both formats are supported */ |
| 245 | if (ntb_fmt_supported & USB_CDC_NCM_NTH32_SIGN) { |
| 246 | err = usbnet_write_cmd(dev, USB_CDC_SET_NTB_FORMAT, |
| 247 | USB_TYPE_CLASS | USB_DIR_OUT |
| 248 | | USB_RECIP_INTERFACE, |
| 249 | USB_CDC_NCM_NTB16_FORMAT, |
| 250 | iface_no, NULL, 0); |
| 251 | if (err < 0) |
| 252 | dev_dbg(&dev->intf->dev, "Setting NTB format to 16-bit failed\n"); |
| 253 | } |
| 254 | |
| 255 | /* inform the device about the selected Max Datagram Size */ |
| 256 | if (!(flags & USB_CDC_NCM_NCAP_MAX_DATAGRAM_SIZE)) |
| 257 | goto out; |
| 258 | |
| 259 | /* read current mtu value from device */ |
| 260 | err = usbnet_read_cmd(dev, USB_CDC_GET_MAX_DATAGRAM_SIZE, |
| 261 | USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE, |
| 262 | 0, iface_no, &max_datagram_size, 2); |
| 263 | if (err < 0) { |
| 264 | dev_dbg(&dev->intf->dev, "GET_MAX_DATAGRAM_SIZE failed\n"); |
| 265 | goto out; |
| 266 | } |
| 267 | |
| 268 | if (le16_to_cpu(max_datagram_size) == ctx->max_datagram_size) |
| 269 | goto out; |
| 270 | |
| 271 | max_datagram_size = cpu_to_le16(ctx->max_datagram_size); |
| 272 | err = usbnet_write_cmd(dev, USB_CDC_SET_MAX_DATAGRAM_SIZE, |
| 273 | USB_TYPE_CLASS | USB_DIR_OUT | USB_RECIP_INTERFACE, |
| 274 | 0, iface_no, &max_datagram_size, 2); |
| 275 | if (err < 0) |
| 276 | dev_dbg(&dev->intf->dev, "SET_MAX_DATAGRAM_SIZE failed\n"); |
| 277 | |
| 278 | out: |
| 279 | /* set MTU to max supported by the device if necessary */ |
| 280 | if (dev->net->mtu > ctx->max_datagram_size - eth_hlen) |
| 281 | dev->net->mtu = ctx->max_datagram_size - eth_hlen; |
| 282 | |
| 283 | /* do not exceed operater preferred MTU */ |
| 284 | if (ctx->mbim_extended_desc) { |
| 285 | mbim_mtu = le16_to_cpu(ctx->mbim_extended_desc->wMTU); |
| 286 | if (mbim_mtu != 0 && mbim_mtu < dev->net->mtu) |
| 287 | dev->net->mtu = mbim_mtu; |
| 288 | } |
| 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | static void |
| 294 | cdc_ncm_find_endpoints(struct usbnet *dev, struct usb_interface *intf) |
| 295 | { |
| 296 | struct usb_host_endpoint *e, *in = NULL, *out = NULL; |
| 297 | u8 ep; |
| 298 | |
| 299 | for (ep = 0; ep < intf->cur_altsetting->desc.bNumEndpoints; ep++) { |
| 300 | |
| 301 | e = intf->cur_altsetting->endpoint + ep; |
| 302 | switch (e->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) { |
| 303 | case USB_ENDPOINT_XFER_INT: |
| 304 | if (usb_endpoint_dir_in(&e->desc)) { |
| 305 | if (!dev->status) |
| 306 | dev->status = e; |
| 307 | } |
| 308 | break; |
| 309 | |
| 310 | case USB_ENDPOINT_XFER_BULK: |
| 311 | if (usb_endpoint_dir_in(&e->desc)) { |
| 312 | if (!in) |
| 313 | in = e; |
| 314 | } else { |
| 315 | if (!out) |
| 316 | out = e; |
| 317 | } |
| 318 | break; |
| 319 | |
| 320 | default: |
| 321 | break; |
| 322 | } |
| 323 | } |
| 324 | if (in && !dev->in) |
| 325 | dev->in = usb_rcvbulkpipe(dev->udev, |
| 326 | in->desc.bEndpointAddress & |
| 327 | USB_ENDPOINT_NUMBER_MASK); |
| 328 | if (out && !dev->out) |
| 329 | dev->out = usb_sndbulkpipe(dev->udev, |
| 330 | out->desc.bEndpointAddress & |
| 331 | USB_ENDPOINT_NUMBER_MASK); |
| 332 | } |
| 333 | |
| 334 | static void cdc_ncm_free(struct cdc_ncm_ctx *ctx) |
| 335 | { |
| 336 | if (ctx == NULL) |
| 337 | return; |
| 338 | |
| 339 | if (ctx->tx_rem_skb != NULL) { |
| 340 | dev_kfree_skb_any(ctx->tx_rem_skb); |
| 341 | ctx->tx_rem_skb = NULL; |
| 342 | } |
| 343 | |
| 344 | if (ctx->tx_curr_skb != NULL) { |
| 345 | dev_kfree_skb_any(ctx->tx_curr_skb); |
| 346 | ctx->tx_curr_skb = NULL; |
| 347 | } |
| 348 | |
| 349 | kfree(ctx); |
| 350 | } |
| 351 | |
| 352 | int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_altsetting) |
| 353 | { |
| 354 | const struct usb_cdc_union_desc *union_desc = NULL; |
| 355 | struct cdc_ncm_ctx *ctx; |
| 356 | struct usb_driver *driver; |
| 357 | u8 *buf; |
| 358 | int len; |
| 359 | int temp; |
| 360 | u8 iface_no; |
| 361 | |
| 362 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 363 | if (!ctx) |
| 364 | return -ENOMEM; |
| 365 | |
| 366 | hrtimer_init(&ctx->tx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 367 | ctx->tx_timer.function = &cdc_ncm_tx_timer_cb; |
| 368 | ctx->bh.data = (unsigned long)dev; |
| 369 | ctx->bh.func = cdc_ncm_txpath_bh; |
| 370 | atomic_set(&ctx->stop, 0); |
| 371 | spin_lock_init(&ctx->mtx); |
| 372 | |
| 373 | /* store ctx pointer in device data field */ |
| 374 | dev->data[0] = (unsigned long)ctx; |
| 375 | |
| 376 | /* only the control interface can be successfully probed */ |
| 377 | ctx->control = intf; |
| 378 | |
| 379 | /* get some pointers */ |
| 380 | driver = driver_of(intf); |
| 381 | buf = intf->cur_altsetting->extra; |
| 382 | len = intf->cur_altsetting->extralen; |
| 383 | |
| 384 | /* parse through descriptors associated with control interface */ |
| 385 | while ((len > 0) && (buf[0] > 2) && (buf[0] <= len)) { |
| 386 | |
| 387 | if (buf[1] != USB_DT_CS_INTERFACE) |
| 388 | goto advance; |
| 389 | |
| 390 | switch (buf[2]) { |
| 391 | case USB_CDC_UNION_TYPE: |
| 392 | if (buf[0] < sizeof(*union_desc)) |
| 393 | break; |
| 394 | |
| 395 | union_desc = (const struct usb_cdc_union_desc *)buf; |
| 396 | /* the master must be the interface we are probing */ |
| 397 | if (intf->cur_altsetting->desc.bInterfaceNumber != |
| 398 | union_desc->bMasterInterface0) { |
| 399 | dev_dbg(&intf->dev, "bogus CDC Union\n"); |
| 400 | goto error; |
| 401 | } |
| 402 | ctx->data = usb_ifnum_to_if(dev->udev, |
| 403 | union_desc->bSlaveInterface0); |
| 404 | break; |
| 405 | |
| 406 | case USB_CDC_ETHERNET_TYPE: |
| 407 | if (buf[0] < sizeof(*(ctx->ether_desc))) |
| 408 | break; |
| 409 | |
| 410 | ctx->ether_desc = |
| 411 | (const struct usb_cdc_ether_desc *)buf; |
| 412 | break; |
| 413 | |
| 414 | case USB_CDC_NCM_TYPE: |
| 415 | if (buf[0] < sizeof(*(ctx->func_desc))) |
| 416 | break; |
| 417 | |
| 418 | ctx->func_desc = (const struct usb_cdc_ncm_desc *)buf; |
| 419 | break; |
| 420 | |
| 421 | case USB_CDC_MBIM_TYPE: |
| 422 | if (buf[0] < sizeof(*(ctx->mbim_desc))) |
| 423 | break; |
| 424 | |
| 425 | ctx->mbim_desc = (const struct usb_cdc_mbim_desc *)buf; |
| 426 | break; |
| 427 | |
| 428 | case USB_CDC_MBIM_EXTENDED_TYPE: |
| 429 | if (buf[0] < sizeof(*(ctx->mbim_extended_desc))) |
| 430 | break; |
| 431 | |
| 432 | ctx->mbim_extended_desc = |
| 433 | (const struct usb_cdc_mbim_extended_desc *)buf; |
| 434 | break; |
| 435 | |
| 436 | default: |
| 437 | break; |
| 438 | } |
| 439 | advance: |
| 440 | /* advance to next descriptor */ |
| 441 | temp = buf[0]; |
| 442 | buf += temp; |
| 443 | len -= temp; |
| 444 | } |
| 445 | |
| 446 | /* some buggy devices have an IAD but no CDC Union */ |
| 447 | if (!union_desc && intf->intf_assoc && intf->intf_assoc->bInterfaceCount == 2) { |
| 448 | ctx->data = usb_ifnum_to_if(dev->udev, intf->cur_altsetting->desc.bInterfaceNumber + 1); |
| 449 | dev_dbg(&intf->dev, "CDC Union missing - got slave from IAD\n"); |
| 450 | } |
| 451 | |
| 452 | /* check if we got everything */ |
| 453 | if (!ctx->data || (!ctx->mbim_desc && !ctx->ether_desc)) { |
| 454 | dev_dbg(&intf->dev, "CDC descriptors missing\n"); |
| 455 | goto error; |
| 456 | } |
| 457 | |
| 458 | /* claim data interface, if different from control */ |
| 459 | if (ctx->data != ctx->control) { |
| 460 | temp = usb_driver_claim_interface(driver, ctx->data, dev); |
| 461 | if (temp) { |
| 462 | dev_dbg(&intf->dev, "failed to claim data intf\n"); |
| 463 | goto error; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | iface_no = ctx->data->cur_altsetting->desc.bInterfaceNumber; |
| 468 | |
| 469 | /* reset data interface */ |
| 470 | temp = usb_set_interface(dev->udev, iface_no, 0); |
| 471 | if (temp) { |
| 472 | dev_dbg(&intf->dev, "set interface failed\n"); |
| 473 | goto error2; |
| 474 | } |
| 475 | |
| 476 | /* initialize data interface */ |
| 477 | if (cdc_ncm_setup(dev)) |
| 478 | goto error2; |
| 479 | |
| 480 | /* configure data interface */ |
| 481 | temp = usb_set_interface(dev->udev, iface_no, data_altsetting); |
| 482 | if (temp) { |
| 483 | dev_dbg(&intf->dev, "set interface failed\n"); |
| 484 | goto error2; |
| 485 | } |
| 486 | |
| 487 | cdc_ncm_find_endpoints(dev, ctx->data); |
| 488 | cdc_ncm_find_endpoints(dev, ctx->control); |
| 489 | if (!dev->in || !dev->out || !dev->status) { |
| 490 | dev_dbg(&intf->dev, "failed to collect endpoints\n"); |
| 491 | goto error2; |
| 492 | } |
| 493 | |
| 494 | usb_set_intfdata(ctx->data, dev); |
| 495 | usb_set_intfdata(ctx->control, dev); |
| 496 | |
| 497 | if (ctx->ether_desc) { |
| 498 | temp = usbnet_get_ethernet_addr(dev, ctx->ether_desc->iMACAddress); |
| 499 | if (temp) { |
| 500 | dev_dbg(&intf->dev, "failed to get mac address\n"); |
| 501 | goto error2; |
| 502 | } |
| 503 | dev_info(&intf->dev, "MAC-Address: %pM\n", dev->net->dev_addr); |
| 504 | } |
| 505 | |
| 506 | /* usbnet use these values for sizing tx/rx queues */ |
| 507 | dev->hard_mtu = ctx->tx_max; |
| 508 | dev->rx_urb_size = ctx->rx_max; |
| 509 | |
| 510 | /* cdc_ncm_setup will override dwNtbOutMaxSize if it is |
| 511 | * outside the sane range. Adding a pad byte here if necessary |
| 512 | * simplifies the handling in cdc_ncm_fill_tx_frame, making |
| 513 | * tx_max always represent the real skb max size. |
| 514 | */ |
| 515 | if (ctx->tx_max != le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize) && |
| 516 | ctx->tx_max % usb_maxpacket(dev->udev, dev->out, 1) == 0) |
| 517 | ctx->tx_max++; |
| 518 | |
| 519 | return 0; |
| 520 | |
| 521 | error2: |
| 522 | usb_set_intfdata(ctx->control, NULL); |
| 523 | usb_set_intfdata(ctx->data, NULL); |
| 524 | if (ctx->data != ctx->control) |
| 525 | usb_driver_release_interface(driver, ctx->data); |
| 526 | error: |
| 527 | cdc_ncm_free((struct cdc_ncm_ctx *)dev->data[0]); |
| 528 | dev->data[0] = 0; |
| 529 | dev_info(&intf->dev, "bind() failure\n"); |
| 530 | return -ENODEV; |
| 531 | } |
| 532 | EXPORT_SYMBOL_GPL(cdc_ncm_bind_common); |
| 533 | |
| 534 | void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf) |
| 535 | { |
| 536 | struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0]; |
| 537 | struct usb_driver *driver = driver_of(intf); |
| 538 | |
| 539 | if (ctx == NULL) |
| 540 | return; /* no setup */ |
| 541 | |
| 542 | atomic_set(&ctx->stop, 1); |
| 543 | |
| 544 | if (hrtimer_active(&ctx->tx_timer)) |
| 545 | hrtimer_cancel(&ctx->tx_timer); |
| 546 | |
| 547 | tasklet_kill(&ctx->bh); |
| 548 | |
| 549 | /* handle devices with combined control and data interface */ |
| 550 | if (ctx->control == ctx->data) |
| 551 | ctx->data = NULL; |
| 552 | |
| 553 | /* disconnect master --> disconnect slave */ |
| 554 | if (intf == ctx->control && ctx->data) { |
| 555 | usb_set_intfdata(ctx->data, NULL); |
| 556 | usb_driver_release_interface(driver, ctx->data); |
| 557 | ctx->data = NULL; |
| 558 | |
| 559 | } else if (intf == ctx->data && ctx->control) { |
| 560 | usb_set_intfdata(ctx->control, NULL); |
| 561 | usb_driver_release_interface(driver, ctx->control); |
| 562 | ctx->control = NULL; |
| 563 | } |
| 564 | |
| 565 | usb_set_intfdata(intf, NULL); |
| 566 | cdc_ncm_free(ctx); |
| 567 | } |
| 568 | EXPORT_SYMBOL_GPL(cdc_ncm_unbind); |
| 569 | |
| 570 | /* Return the number of the MBIM control interface altsetting iff it |
| 571 | * is preferred and available, |
| 572 | */ |
| 573 | u8 cdc_ncm_select_altsetting(struct usb_interface *intf) |
| 574 | { |
| 575 | struct usb_host_interface *alt; |
| 576 | |
| 577 | /* The MBIM spec defines a NCM compatible default altsetting, |
| 578 | * which we may have matched: |
| 579 | * |
| 580 | * "Functions that implement both NCM 1.0 and MBIM (an |
| 581 | * “NCM/MBIM function”) according to this recommendation |
| 582 | * shall provide two alternate settings for the |
| 583 | * Communication Interface. Alternate setting 0, and the |
| 584 | * associated class and endpoint descriptors, shall be |
| 585 | * constructed according to the rules given for the |
| 586 | * Communication Interface in section 5 of [USBNCM10]. |
| 587 | * Alternate setting 1, and the associated class and |
| 588 | * endpoint descriptors, shall be constructed according to |
| 589 | * the rules given in section 6 (USB Device Model) of this |
| 590 | * specification." |
| 591 | */ |
| 592 | if (intf->num_altsetting < 2) |
| 593 | return intf->cur_altsetting->desc.bAlternateSetting; |
| 594 | |
| 595 | if (prefer_mbim) { |
| 596 | alt = usb_altnum_to_altsetting(intf, CDC_NCM_COMM_ALTSETTING_MBIM); |
| 597 | if (alt && cdc_ncm_comm_intf_is_mbim(alt)) |
| 598 | return CDC_NCM_COMM_ALTSETTING_MBIM; |
| 599 | } |
| 600 | return CDC_NCM_COMM_ALTSETTING_NCM; |
| 601 | } |
| 602 | EXPORT_SYMBOL_GPL(cdc_ncm_select_altsetting); |
| 603 | |
| 604 | static int cdc_ncm_bind(struct usbnet *dev, struct usb_interface *intf) |
| 605 | { |
| 606 | int ret; |
| 607 | |
| 608 | /* MBIM backwards compatible function? */ |
| 609 | if (cdc_ncm_select_altsetting(intf) != CDC_NCM_COMM_ALTSETTING_NCM) |
| 610 | return -ENODEV; |
| 611 | |
| 612 | /* The NCM data altsetting is fixed */ |
| 613 | ret = cdc_ncm_bind_common(dev, intf, CDC_NCM_DATA_ALTSETTING_NCM); |
| 614 | |
| 615 | /* |
| 616 | * We should get an event when network connection is "connected" or |
| 617 | * "disconnected". Set network connection in "disconnected" state |
| 618 | * (carrier is OFF) during attach, so the IP network stack does not |
| 619 | * start IPv6 negotiation and more. |
| 620 | */ |
| 621 | usbnet_link_change(dev, 0, 0); |
| 622 | return ret; |
| 623 | } |
| 624 | |
| 625 | static void cdc_ncm_align_tail(struct sk_buff *skb, size_t modulus, size_t remainder, size_t max) |
| 626 | { |
| 627 | size_t align = ALIGN(skb->len, modulus) - skb->len + remainder; |
| 628 | |
| 629 | if (skb->len + align > max) |
| 630 | align = max - skb->len; |
| 631 | if (align && skb_tailroom(skb) >= align) |
| 632 | memset(skb_put(skb, align), 0, align); |
| 633 | } |
| 634 | |
| 635 | /* return a pointer to a valid struct usb_cdc_ncm_ndp16 of type sign, possibly |
| 636 | * allocating a new one within skb |
| 637 | */ |
| 638 | static struct usb_cdc_ncm_ndp16 *cdc_ncm_ndp(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign, size_t reserve) |
| 639 | { |
| 640 | struct usb_cdc_ncm_ndp16 *ndp16 = NULL; |
| 641 | struct usb_cdc_ncm_nth16 *nth16 = (void *)skb->data; |
| 642 | size_t ndpoffset = le16_to_cpu(nth16->wNdpIndex); |
| 643 | |
| 644 | /* follow the chain of NDPs, looking for a match */ |
| 645 | while (ndpoffset) { |
| 646 | ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb->data + ndpoffset); |
| 647 | if (ndp16->dwSignature == sign) |
| 648 | return ndp16; |
| 649 | ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex); |
| 650 | } |
| 651 | |
| 652 | /* align new NDP */ |
| 653 | cdc_ncm_align_tail(skb, ctx->tx_ndp_modulus, 0, ctx->tx_max); |
| 654 | |
| 655 | /* verify that there is room for the NDP and the datagram (reserve) */ |
| 656 | if ((ctx->tx_max - skb->len - reserve) < CDC_NCM_NDP_SIZE) |
| 657 | return NULL; |
| 658 | |
| 659 | /* link to it */ |
| 660 | if (ndp16) |
| 661 | ndp16->wNextNdpIndex = cpu_to_le16(skb->len); |
| 662 | else |
| 663 | nth16->wNdpIndex = cpu_to_le16(skb->len); |
| 664 | |
| 665 | /* push a new empty NDP */ |
| 666 | ndp16 = (struct usb_cdc_ncm_ndp16 *)memset(skb_put(skb, CDC_NCM_NDP_SIZE), 0, CDC_NCM_NDP_SIZE); |
| 667 | ndp16->dwSignature = sign; |
| 668 | ndp16->wLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_ndp16) + sizeof(struct usb_cdc_ncm_dpe16)); |
| 669 | return ndp16; |
| 670 | } |
| 671 | |
| 672 | struct sk_buff * |
| 673 | cdc_ncm_fill_tx_frame(struct usbnet *dev, struct sk_buff *skb, __le32 sign) |
| 674 | { |
| 675 | struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0]; |
| 676 | struct usb_cdc_ncm_nth16 *nth16; |
| 677 | struct usb_cdc_ncm_ndp16 *ndp16; |
| 678 | struct sk_buff *skb_out; |
| 679 | u16 n = 0, index, ndplen; |
| 680 | u8 ready2send = 0; |
| 681 | |
| 682 | /* if there is a remaining skb, it gets priority */ |
| 683 | if (skb != NULL) { |
| 684 | swap(skb, ctx->tx_rem_skb); |
| 685 | swap(sign, ctx->tx_rem_sign); |
| 686 | } else { |
| 687 | ready2send = 1; |
| 688 | } |
| 689 | |
| 690 | /* check if we are resuming an OUT skb */ |
| 691 | skb_out = ctx->tx_curr_skb; |
| 692 | |
| 693 | /* allocate a new OUT skb */ |
| 694 | if (!skb_out) { |
| 695 | skb_out = alloc_skb(ctx->tx_max, GFP_ATOMIC); |
| 696 | if (skb_out == NULL) { |
| 697 | if (skb != NULL) { |
| 698 | dev_kfree_skb_any(skb); |
| 699 | dev->net->stats.tx_dropped++; |
| 700 | } |
| 701 | goto exit_no_skb; |
| 702 | } |
| 703 | /* fill out the initial 16-bit NTB header */ |
| 704 | nth16 = (struct usb_cdc_ncm_nth16 *)memset(skb_put(skb_out, sizeof(struct usb_cdc_ncm_nth16)), 0, sizeof(struct usb_cdc_ncm_nth16)); |
| 705 | nth16->dwSignature = cpu_to_le32(USB_CDC_NCM_NTH16_SIGN); |
| 706 | nth16->wHeaderLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_nth16)); |
| 707 | nth16->wSequence = cpu_to_le16(ctx->tx_seq++); |
| 708 | |
| 709 | /* count total number of frames in this NTB */ |
| 710 | ctx->tx_curr_frame_num = 0; |
| 711 | } |
| 712 | |
| 713 | for (n = ctx->tx_curr_frame_num; n < ctx->tx_max_datagrams; n++) { |
| 714 | /* send any remaining skb first */ |
| 715 | if (skb == NULL) { |
| 716 | skb = ctx->tx_rem_skb; |
| 717 | sign = ctx->tx_rem_sign; |
| 718 | ctx->tx_rem_skb = NULL; |
| 719 | |
| 720 | /* check for end of skb */ |
| 721 | if (skb == NULL) |
| 722 | break; |
| 723 | } |
| 724 | |
| 725 | /* get the appropriate NDP for this skb */ |
| 726 | ndp16 = cdc_ncm_ndp(ctx, skb_out, sign, skb->len + ctx->tx_modulus + ctx->tx_remainder); |
| 727 | |
| 728 | /* align beginning of next frame */ |
| 729 | cdc_ncm_align_tail(skb_out, ctx->tx_modulus, ctx->tx_remainder, ctx->tx_max); |
| 730 | |
| 731 | /* check if we had enough room left for both NDP and frame */ |
| 732 | if (!ndp16 || skb_out->len + skb->len > ctx->tx_max) { |
| 733 | if (n == 0) { |
| 734 | /* won't fit, MTU problem? */ |
| 735 | dev_kfree_skb_any(skb); |
| 736 | skb = NULL; |
| 737 | dev->net->stats.tx_dropped++; |
| 738 | } else { |
| 739 | /* no room for skb - store for later */ |
| 740 | if (ctx->tx_rem_skb != NULL) { |
| 741 | dev_kfree_skb_any(ctx->tx_rem_skb); |
| 742 | dev->net->stats.tx_dropped++; |
| 743 | } |
| 744 | ctx->tx_rem_skb = skb; |
| 745 | ctx->tx_rem_sign = sign; |
| 746 | skb = NULL; |
| 747 | ready2send = 1; |
| 748 | } |
| 749 | break; |
| 750 | } |
| 751 | |
| 752 | /* calculate frame number withing this NDP */ |
| 753 | ndplen = le16_to_cpu(ndp16->wLength); |
| 754 | index = (ndplen - sizeof(struct usb_cdc_ncm_ndp16)) / sizeof(struct usb_cdc_ncm_dpe16) - 1; |
| 755 | |
| 756 | /* OK, add this skb */ |
| 757 | ndp16->dpe16[index].wDatagramLength = cpu_to_le16(skb->len); |
| 758 | ndp16->dpe16[index].wDatagramIndex = cpu_to_le16(skb_out->len); |
| 759 | ndp16->wLength = cpu_to_le16(ndplen + sizeof(struct usb_cdc_ncm_dpe16)); |
| 760 | memcpy(skb_put(skb_out, skb->len), skb->data, skb->len); |
| 761 | dev_kfree_skb_any(skb); |
| 762 | skb = NULL; |
| 763 | |
| 764 | /* send now if this NDP is full */ |
| 765 | if (index >= CDC_NCM_DPT_DATAGRAMS_MAX) { |
| 766 | ready2send = 1; |
| 767 | break; |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | /* free up any dangling skb */ |
| 772 | if (skb != NULL) { |
| 773 | dev_kfree_skb_any(skb); |
| 774 | skb = NULL; |
| 775 | dev->net->stats.tx_dropped++; |
| 776 | } |
| 777 | |
| 778 | ctx->tx_curr_frame_num = n; |
| 779 | |
| 780 | if (n == 0) { |
| 781 | /* wait for more frames */ |
| 782 | /* push variables */ |
| 783 | ctx->tx_curr_skb = skb_out; |
| 784 | goto exit_no_skb; |
| 785 | |
| 786 | } else if ((n < ctx->tx_max_datagrams) && (ready2send == 0)) { |
| 787 | /* wait for more frames */ |
| 788 | /* push variables */ |
| 789 | ctx->tx_curr_skb = skb_out; |
| 790 | /* set the pending count */ |
| 791 | if (n < CDC_NCM_RESTART_TIMER_DATAGRAM_CNT) |
| 792 | ctx->tx_timer_pending = CDC_NCM_TIMER_PENDING_CNT; |
| 793 | goto exit_no_skb; |
| 794 | |
| 795 | } else { |
| 796 | /* frame goes out */ |
| 797 | /* variables will be reset at next call */ |
| 798 | } |
| 799 | |
| 800 | /* If collected data size is less or equal CDC_NCM_MIN_TX_PKT |
| 801 | * bytes, we send buffers as it is. If we get more data, it |
| 802 | * would be more efficient for USB HS mobile device with DMA |
| 803 | * engine to receive a full size NTB, than canceling DMA |
| 804 | * transfer and receiving a short packet. |
| 805 | * |
| 806 | * This optimization support is pointless if we end up sending |
| 807 | * a ZLP after full sized NTBs. |
| 808 | */ |
| 809 | if (!(dev->driver_info->flags & FLAG_SEND_ZLP) && |
| 810 | skb_out->len > CDC_NCM_MIN_TX_PKT) |
| 811 | memset(skb_put(skb_out, ctx->tx_max - skb_out->len), 0, |
| 812 | ctx->tx_max - skb_out->len); |
| 813 | else if (skb_out->len < ctx->tx_max && (skb_out->len % dev->maxpacket) == 0) |
| 814 | *skb_put(skb_out, 1) = 0; /* force short packet */ |
| 815 | |
| 816 | /* set final frame length */ |
| 817 | nth16 = (struct usb_cdc_ncm_nth16 *)skb_out->data; |
| 818 | nth16->wBlockLength = cpu_to_le16(skb_out->len); |
| 819 | |
| 820 | /* return skb */ |
| 821 | ctx->tx_curr_skb = NULL; |
| 822 | dev->net->stats.tx_packets += ctx->tx_curr_frame_num; |
| 823 | return skb_out; |
| 824 | |
| 825 | exit_no_skb: |
| 826 | /* Start timer, if there is a remaining skb */ |
| 827 | if (ctx->tx_curr_skb != NULL) |
| 828 | cdc_ncm_tx_timeout_start(ctx); |
| 829 | return NULL; |
| 830 | } |
| 831 | EXPORT_SYMBOL_GPL(cdc_ncm_fill_tx_frame); |
| 832 | |
| 833 | static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx) |
| 834 | { |
| 835 | /* start timer, if not already started */ |
| 836 | if (!(hrtimer_active(&ctx->tx_timer) || atomic_read(&ctx->stop))) |
| 837 | hrtimer_start(&ctx->tx_timer, |
| 838 | ktime_set(0, CDC_NCM_TIMER_INTERVAL), |
| 839 | HRTIMER_MODE_REL); |
| 840 | } |
| 841 | |
| 842 | static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *timer) |
| 843 | { |
| 844 | struct cdc_ncm_ctx *ctx = |
| 845 | container_of(timer, struct cdc_ncm_ctx, tx_timer); |
| 846 | |
| 847 | if (!atomic_read(&ctx->stop)) |
| 848 | tasklet_schedule(&ctx->bh); |
| 849 | return HRTIMER_NORESTART; |
| 850 | } |
| 851 | |
| 852 | static void cdc_ncm_txpath_bh(unsigned long param) |
| 853 | { |
| 854 | struct usbnet *dev = (struct usbnet *)param; |
| 855 | struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0]; |
| 856 | |
| 857 | spin_lock_bh(&ctx->mtx); |
| 858 | if (ctx->tx_timer_pending != 0) { |
| 859 | ctx->tx_timer_pending--; |
| 860 | cdc_ncm_tx_timeout_start(ctx); |
| 861 | spin_unlock_bh(&ctx->mtx); |
| 862 | } else if (dev->net != NULL) { |
| 863 | spin_unlock_bh(&ctx->mtx); |
| 864 | netif_tx_lock_bh(dev->net); |
| 865 | usbnet_start_xmit(NULL, dev->net); |
| 866 | netif_tx_unlock_bh(dev->net); |
| 867 | } else { |
| 868 | spin_unlock_bh(&ctx->mtx); |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | struct sk_buff * |
| 873 | cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags) |
| 874 | { |
| 875 | struct sk_buff *skb_out; |
| 876 | struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0]; |
| 877 | |
| 878 | /* |
| 879 | * The Ethernet API we are using does not support transmitting |
| 880 | * multiple Ethernet frames in a single call. This driver will |
| 881 | * accumulate multiple Ethernet frames and send out a larger |
| 882 | * USB frame when the USB buffer is full or when a single jiffies |
| 883 | * timeout happens. |
| 884 | */ |
| 885 | if (ctx == NULL) |
| 886 | goto error; |
| 887 | |
| 888 | spin_lock_bh(&ctx->mtx); |
| 889 | skb_out = cdc_ncm_fill_tx_frame(dev, skb, cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN)); |
| 890 | spin_unlock_bh(&ctx->mtx); |
| 891 | return skb_out; |
| 892 | |
| 893 | error: |
| 894 | if (skb != NULL) |
| 895 | dev_kfree_skb_any(skb); |
| 896 | |
| 897 | return NULL; |
| 898 | } |
| 899 | EXPORT_SYMBOL_GPL(cdc_ncm_tx_fixup); |
| 900 | |
| 901 | /* verify NTB header and return offset of first NDP, or negative error */ |
| 902 | int cdc_ncm_rx_verify_nth16(struct cdc_ncm_ctx *ctx, struct sk_buff *skb_in) |
| 903 | { |
| 904 | struct usbnet *dev = netdev_priv(skb_in->dev); |
| 905 | struct usb_cdc_ncm_nth16 *nth16; |
| 906 | int len; |
| 907 | int ret = -EINVAL; |
| 908 | |
| 909 | if (ctx == NULL) |
| 910 | goto error; |
| 911 | |
| 912 | if (skb_in->len < (sizeof(struct usb_cdc_ncm_nth16) + |
| 913 | sizeof(struct usb_cdc_ncm_ndp16))) { |
| 914 | netif_dbg(dev, rx_err, dev->net, "frame too short\n"); |
| 915 | goto error; |
| 916 | } |
| 917 | |
| 918 | nth16 = (struct usb_cdc_ncm_nth16 *)skb_in->data; |
| 919 | |
| 920 | if (nth16->dwSignature != cpu_to_le32(USB_CDC_NCM_NTH16_SIGN)) { |
| 921 | netif_dbg(dev, rx_err, dev->net, |
| 922 | "invalid NTH16 signature <%#010x>\n", |
| 923 | le32_to_cpu(nth16->dwSignature)); |
| 924 | goto error; |
| 925 | } |
| 926 | |
| 927 | len = le16_to_cpu(nth16->wBlockLength); |
| 928 | if (len > ctx->rx_max) { |
| 929 | netif_dbg(dev, rx_err, dev->net, |
| 930 | "unsupported NTB block length %u/%u\n", len, |
| 931 | ctx->rx_max); |
| 932 | goto error; |
| 933 | } |
| 934 | |
| 935 | if ((ctx->rx_seq + 1) != le16_to_cpu(nth16->wSequence) && |
| 936 | (ctx->rx_seq || le16_to_cpu(nth16->wSequence)) && |
| 937 | !((ctx->rx_seq == 0xffff) && !le16_to_cpu(nth16->wSequence))) { |
| 938 | netif_dbg(dev, rx_err, dev->net, |
| 939 | "sequence number glitch prev=%d curr=%d\n", |
| 940 | ctx->rx_seq, le16_to_cpu(nth16->wSequence)); |
| 941 | } |
| 942 | ctx->rx_seq = le16_to_cpu(nth16->wSequence); |
| 943 | |
| 944 | ret = le16_to_cpu(nth16->wNdpIndex); |
| 945 | error: |
| 946 | return ret; |
| 947 | } |
| 948 | EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_nth16); |
| 949 | |
| 950 | /* verify NDP header and return number of datagrams, or negative error */ |
| 951 | int cdc_ncm_rx_verify_ndp16(struct sk_buff *skb_in, int ndpoffset) |
| 952 | { |
| 953 | struct usbnet *dev = netdev_priv(skb_in->dev); |
| 954 | struct usb_cdc_ncm_ndp16 *ndp16; |
| 955 | int ret = -EINVAL; |
| 956 | |
| 957 | if ((ndpoffset + sizeof(struct usb_cdc_ncm_ndp16)) > skb_in->len) { |
| 958 | netif_dbg(dev, rx_err, dev->net, "invalid NDP offset <%u>\n", |
| 959 | ndpoffset); |
| 960 | goto error; |
| 961 | } |
| 962 | ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset); |
| 963 | |
| 964 | if (le16_to_cpu(ndp16->wLength) < USB_CDC_NCM_NDP16_LENGTH_MIN) { |
| 965 | netif_dbg(dev, rx_err, dev->net, "invalid DPT16 length <%u>\n", |
| 966 | le16_to_cpu(ndp16->wLength)); |
| 967 | goto error; |
| 968 | } |
| 969 | |
| 970 | ret = ((le16_to_cpu(ndp16->wLength) - |
| 971 | sizeof(struct usb_cdc_ncm_ndp16)) / |
| 972 | sizeof(struct usb_cdc_ncm_dpe16)); |
| 973 | ret--; /* we process NDP entries except for the last one */ |
| 974 | |
| 975 | if ((sizeof(struct usb_cdc_ncm_ndp16) + |
| 976 | ret * (sizeof(struct usb_cdc_ncm_dpe16))) > skb_in->len) { |
| 977 | netif_dbg(dev, rx_err, dev->net, "Invalid nframes = %d\n", ret); |
| 978 | ret = -EINVAL; |
| 979 | } |
| 980 | |
| 981 | error: |
| 982 | return ret; |
| 983 | } |
| 984 | EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_ndp16); |
| 985 | |
| 986 | int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in) |
| 987 | { |
| 988 | struct sk_buff *skb; |
| 989 | struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0]; |
| 990 | int len; |
| 991 | int nframes; |
| 992 | int x; |
| 993 | int offset; |
| 994 | struct usb_cdc_ncm_ndp16 *ndp16; |
| 995 | struct usb_cdc_ncm_dpe16 *dpe16; |
| 996 | int ndpoffset; |
| 997 | int loopcount = 50; /* arbitrary max preventing infinite loop */ |
| 998 | |
| 999 | ndpoffset = cdc_ncm_rx_verify_nth16(ctx, skb_in); |
| 1000 | if (ndpoffset < 0) |
| 1001 | goto error; |
| 1002 | |
| 1003 | next_ndp: |
| 1004 | nframes = cdc_ncm_rx_verify_ndp16(skb_in, ndpoffset); |
| 1005 | if (nframes < 0) |
| 1006 | goto error; |
| 1007 | |
| 1008 | ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset); |
| 1009 | |
| 1010 | if (ndp16->dwSignature != cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN)) { |
| 1011 | netif_dbg(dev, rx_err, dev->net, |
| 1012 | "invalid DPT16 signature <%#010x>\n", |
| 1013 | le32_to_cpu(ndp16->dwSignature)); |
| 1014 | goto err_ndp; |
| 1015 | } |
| 1016 | dpe16 = ndp16->dpe16; |
| 1017 | |
| 1018 | for (x = 0; x < nframes; x++, dpe16++) { |
| 1019 | offset = le16_to_cpu(dpe16->wDatagramIndex); |
| 1020 | len = le16_to_cpu(dpe16->wDatagramLength); |
| 1021 | |
| 1022 | /* |
| 1023 | * CDC NCM ch. 3.7 |
| 1024 | * All entries after first NULL entry are to be ignored |
| 1025 | */ |
| 1026 | if ((offset == 0) || (len == 0)) { |
| 1027 | if (!x) |
| 1028 | goto err_ndp; /* empty NTB */ |
| 1029 | break; |
| 1030 | } |
| 1031 | |
| 1032 | /* sanity checking */ |
| 1033 | if (((offset + len) > skb_in->len) || |
| 1034 | (len > ctx->rx_max) || (len < ETH_HLEN)) { |
| 1035 | netif_dbg(dev, rx_err, dev->net, |
| 1036 | "invalid frame detected (ignored) offset[%u]=%u, length=%u, skb=%p\n", |
| 1037 | x, offset, len, skb_in); |
| 1038 | if (!x) |
| 1039 | goto err_ndp; |
| 1040 | break; |
| 1041 | |
| 1042 | } else { |
| 1043 | skb = skb_clone(skb_in, GFP_ATOMIC); |
| 1044 | if (!skb) |
| 1045 | goto error; |
| 1046 | skb->len = len; |
| 1047 | skb->data = ((u8 *)skb_in->data) + offset; |
| 1048 | skb_set_tail_pointer(skb, len); |
| 1049 | usbnet_skb_return(dev, skb); |
| 1050 | } |
| 1051 | } |
| 1052 | err_ndp: |
| 1053 | /* are there more NDPs to process? */ |
| 1054 | ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex); |
| 1055 | if (ndpoffset && loopcount--) |
| 1056 | goto next_ndp; |
| 1057 | |
| 1058 | return 1; |
| 1059 | error: |
| 1060 | return 0; |
| 1061 | } |
| 1062 | EXPORT_SYMBOL_GPL(cdc_ncm_rx_fixup); |
| 1063 | |
| 1064 | static void |
| 1065 | cdc_ncm_speed_change(struct usbnet *dev, |
| 1066 | struct usb_cdc_speed_change *data) |
| 1067 | { |
| 1068 | uint32_t rx_speed = le32_to_cpu(data->DLBitRRate); |
| 1069 | uint32_t tx_speed = le32_to_cpu(data->ULBitRate); |
| 1070 | |
| 1071 | /* |
| 1072 | * Currently the USB-NET API does not support reporting the actual |
| 1073 | * device speed. Do print it instead. |
| 1074 | */ |
| 1075 | if ((tx_speed > 1000000) && (rx_speed > 1000000)) { |
| 1076 | netif_info(dev, link, dev->net, |
| 1077 | "%u mbit/s downlink %u mbit/s uplink\n", |
| 1078 | (unsigned int)(rx_speed / 1000000U), |
| 1079 | (unsigned int)(tx_speed / 1000000U)); |
| 1080 | } else { |
| 1081 | netif_info(dev, link, dev->net, |
| 1082 | "%u kbit/s downlink %u kbit/s uplink\n", |
| 1083 | (unsigned int)(rx_speed / 1000U), |
| 1084 | (unsigned int)(tx_speed / 1000U)); |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | static void cdc_ncm_status(struct usbnet *dev, struct urb *urb) |
| 1089 | { |
| 1090 | struct cdc_ncm_ctx *ctx; |
| 1091 | struct usb_cdc_notification *event; |
| 1092 | |
| 1093 | ctx = (struct cdc_ncm_ctx *)dev->data[0]; |
| 1094 | |
| 1095 | if (urb->actual_length < sizeof(*event)) |
| 1096 | return; |
| 1097 | |
| 1098 | /* test for split data in 8-byte chunks */ |
| 1099 | if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) { |
| 1100 | cdc_ncm_speed_change(dev, |
| 1101 | (struct usb_cdc_speed_change *)urb->transfer_buffer); |
| 1102 | return; |
| 1103 | } |
| 1104 | |
| 1105 | event = urb->transfer_buffer; |
| 1106 | |
| 1107 | switch (event->bNotificationType) { |
| 1108 | case USB_CDC_NOTIFY_NETWORK_CONNECTION: |
| 1109 | /* |
| 1110 | * According to the CDC NCM specification ch.7.1 |
| 1111 | * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be |
| 1112 | * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE. |
| 1113 | */ |
| 1114 | ctx->connected = le16_to_cpu(event->wValue); |
| 1115 | netif_info(dev, link, dev->net, |
| 1116 | "network connection: %sconnected\n", |
| 1117 | ctx->connected ? "" : "dis"); |
| 1118 | usbnet_link_change(dev, ctx->connected, 0); |
| 1119 | break; |
| 1120 | |
| 1121 | case USB_CDC_NOTIFY_SPEED_CHANGE: |
| 1122 | if (urb->actual_length < (sizeof(*event) + |
| 1123 | sizeof(struct usb_cdc_speed_change))) |
| 1124 | set_bit(EVENT_STS_SPLIT, &dev->flags); |
| 1125 | else |
| 1126 | cdc_ncm_speed_change(dev, |
| 1127 | (struct usb_cdc_speed_change *)&event[1]); |
| 1128 | break; |
| 1129 | |
| 1130 | default: |
| 1131 | dev_dbg(&dev->udev->dev, |
| 1132 | "NCM: unexpected notification 0x%02x!\n", |
| 1133 | event->bNotificationType); |
| 1134 | break; |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | static int cdc_ncm_check_connect(struct usbnet *dev) |
| 1139 | { |
| 1140 | struct cdc_ncm_ctx *ctx; |
| 1141 | |
| 1142 | ctx = (struct cdc_ncm_ctx *)dev->data[0]; |
| 1143 | if (ctx == NULL) |
| 1144 | return 1; /* disconnected */ |
| 1145 | |
| 1146 | return !ctx->connected; |
| 1147 | } |
| 1148 | |
| 1149 | static const struct driver_info cdc_ncm_info = { |
| 1150 | .description = "CDC NCM", |
| 1151 | .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET, |
| 1152 | .bind = cdc_ncm_bind, |
| 1153 | .unbind = cdc_ncm_unbind, |
| 1154 | .check_connect = cdc_ncm_check_connect, |
| 1155 | .manage_power = usbnet_manage_power, |
| 1156 | .status = cdc_ncm_status, |
| 1157 | .rx_fixup = cdc_ncm_rx_fixup, |
| 1158 | .tx_fixup = cdc_ncm_tx_fixup, |
| 1159 | }; |
| 1160 | |
| 1161 | /* Same as cdc_ncm_info, but with FLAG_WWAN */ |
| 1162 | static const struct driver_info wwan_info = { |
| 1163 | .description = "Mobile Broadband Network Device", |
| 1164 | .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET |
| 1165 | | FLAG_WWAN, |
| 1166 | .bind = cdc_ncm_bind, |
| 1167 | .unbind = cdc_ncm_unbind, |
| 1168 | .check_connect = cdc_ncm_check_connect, |
| 1169 | .manage_power = usbnet_manage_power, |
| 1170 | .status = cdc_ncm_status, |
| 1171 | .rx_fixup = cdc_ncm_rx_fixup, |
| 1172 | .tx_fixup = cdc_ncm_tx_fixup, |
| 1173 | }; |
| 1174 | |
| 1175 | /* Same as wwan_info, but with FLAG_NOARP */ |
| 1176 | static const struct driver_info wwan_noarp_info = { |
| 1177 | .description = "Mobile Broadband Network Device (NO ARP)", |
| 1178 | .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET |
| 1179 | | FLAG_WWAN | FLAG_NOARP, |
| 1180 | .bind = cdc_ncm_bind, |
| 1181 | .unbind = cdc_ncm_unbind, |
| 1182 | .check_connect = cdc_ncm_check_connect, |
| 1183 | .manage_power = usbnet_manage_power, |
| 1184 | .status = cdc_ncm_status, |
| 1185 | .rx_fixup = cdc_ncm_rx_fixup, |
| 1186 | .tx_fixup = cdc_ncm_tx_fixup, |
| 1187 | }; |
| 1188 | |
| 1189 | static const struct usb_device_id cdc_devs[] = { |
| 1190 | /* Ericsson MBM devices like F5521gw */ |
| 1191 | { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO |
| 1192 | | USB_DEVICE_ID_MATCH_VENDOR, |
| 1193 | .idVendor = 0x0bdb, |
| 1194 | .bInterfaceClass = USB_CLASS_COMM, |
| 1195 | .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM, |
| 1196 | .bInterfaceProtocol = USB_CDC_PROTO_NONE, |
| 1197 | .driver_info = (unsigned long) &wwan_info, |
| 1198 | }, |
| 1199 | |
| 1200 | /* Dell branded MBM devices like DW5550 */ |
| 1201 | { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO |
| 1202 | | USB_DEVICE_ID_MATCH_VENDOR, |
| 1203 | .idVendor = 0x413c, |
| 1204 | .bInterfaceClass = USB_CLASS_COMM, |
| 1205 | .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM, |
| 1206 | .bInterfaceProtocol = USB_CDC_PROTO_NONE, |
| 1207 | .driver_info = (unsigned long) &wwan_info, |
| 1208 | }, |
| 1209 | |
| 1210 | /* Toshiba branded MBM devices */ |
| 1211 | { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO |
| 1212 | | USB_DEVICE_ID_MATCH_VENDOR, |
| 1213 | .idVendor = 0x0930, |
| 1214 | .bInterfaceClass = USB_CLASS_COMM, |
| 1215 | .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM, |
| 1216 | .bInterfaceProtocol = USB_CDC_PROTO_NONE, |
| 1217 | .driver_info = (unsigned long) &wwan_info, |
| 1218 | }, |
| 1219 | |
| 1220 | /* tag Huawei devices as wwan */ |
| 1221 | { USB_VENDOR_AND_INTERFACE_INFO(0x12d1, |
| 1222 | USB_CLASS_COMM, |
| 1223 | USB_CDC_SUBCLASS_NCM, |
| 1224 | USB_CDC_PROTO_NONE), |
| 1225 | .driver_info = (unsigned long)&wwan_info, |
| 1226 | }, |
| 1227 | |
| 1228 | /* Infineon(now Intel) HSPA Modem platform */ |
| 1229 | { USB_DEVICE_AND_INTERFACE_INFO(0x1519, 0x0443, |
| 1230 | USB_CLASS_COMM, |
| 1231 | USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE), |
| 1232 | .driver_info = (unsigned long)&wwan_noarp_info, |
| 1233 | }, |
| 1234 | |
| 1235 | /* Generic CDC-NCM devices */ |
| 1236 | { USB_INTERFACE_INFO(USB_CLASS_COMM, |
| 1237 | USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE), |
| 1238 | .driver_info = (unsigned long)&cdc_ncm_info, |
| 1239 | }, |
| 1240 | { |
| 1241 | }, |
| 1242 | }; |
| 1243 | MODULE_DEVICE_TABLE(usb, cdc_devs); |
| 1244 | |
| 1245 | static struct usb_driver cdc_ncm_driver = { |
| 1246 | .name = "cdc_ncm", |
| 1247 | .id_table = cdc_devs, |
| 1248 | .probe = usbnet_probe, |
| 1249 | .disconnect = usbnet_disconnect, |
| 1250 | .suspend = usbnet_suspend, |
| 1251 | .resume = usbnet_resume, |
| 1252 | .reset_resume = usbnet_resume, |
| 1253 | .supports_autosuspend = 1, |
| 1254 | .disable_hub_initiated_lpm = 1, |
| 1255 | }; |
| 1256 | |
| 1257 | module_usb_driver(cdc_ncm_driver); |
| 1258 | |
| 1259 | MODULE_AUTHOR("Hans Petter Selasky"); |
| 1260 | MODULE_DESCRIPTION("USB CDC NCM host driver"); |
| 1261 | MODULE_LICENSE("Dual BSD/GPL"); |