Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | * cdc_ncm.c |
| 3 | * |
Alexey Orishko | c84ff1d | 2012-03-14 11:26:10 +0000 | [diff] [blame] | 4 | * Copyright (C) ST-Ericsson 2010-2012 |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 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> |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 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> |
Alexey Orishko | c84ff1d | 2012-03-14 11:26:10 +0000 | [diff] [blame] | 49 | #include <linux/hrtimer.h> |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 50 | #include <linux/atomic.h> |
| 51 | #include <linux/usb/usbnet.h> |
| 52 | #include <linux/usb/cdc.h> |
Bjørn Mork | c91ce3b | 2012-10-22 10:56:35 +0000 | [diff] [blame] | 53 | #include <linux/usb/cdc_ncm.h> |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 54 | |
Bjørn Mork | 1e8bbe6 | 2013-03-14 01:05:13 +0000 | [diff] [blame] | 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 | |
Alexey Orishko | c84ff1d | 2012-03-14 11:26:10 +0000 | [diff] [blame] | 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); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 66 | static struct usb_driver cdc_ncm_driver; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 67 | |
Bjørn Mork | beeecd4 | 2014-05-16 21:48:25 +0200 | [diff] [blame^] | 68 | struct cdc_ncm_stats { |
| 69 | char stat_string[ETH_GSTRING_LEN]; |
| 70 | int sizeof_stat; |
| 71 | int stat_offset; |
| 72 | }; |
| 73 | |
| 74 | #define CDC_NCM_STAT(str, m) { \ |
| 75 | .stat_string = str, \ |
| 76 | .sizeof_stat = sizeof(((struct cdc_ncm_ctx *)0)->m), \ |
| 77 | .stat_offset = offsetof(struct cdc_ncm_ctx, m) } |
| 78 | #define CDC_NCM_SIMPLE_STAT(m) CDC_NCM_STAT(__stringify(m), m) |
| 79 | |
| 80 | static const struct cdc_ncm_stats cdc_ncm_gstrings_stats[] = { |
| 81 | CDC_NCM_SIMPLE_STAT(tx_reason_ntb_full), |
| 82 | CDC_NCM_SIMPLE_STAT(tx_reason_ndp_full), |
| 83 | CDC_NCM_SIMPLE_STAT(tx_reason_timeout), |
| 84 | CDC_NCM_SIMPLE_STAT(tx_reason_max_datagram), |
| 85 | CDC_NCM_SIMPLE_STAT(tx_overhead), |
| 86 | CDC_NCM_SIMPLE_STAT(tx_ntbs), |
| 87 | CDC_NCM_SIMPLE_STAT(rx_overhead), |
| 88 | CDC_NCM_SIMPLE_STAT(rx_ntbs), |
| 89 | }; |
| 90 | |
| 91 | static int cdc_ncm_get_sset_count(struct net_device __always_unused *netdev, int sset) |
| 92 | { |
| 93 | switch (sset) { |
| 94 | case ETH_SS_STATS: |
| 95 | return ARRAY_SIZE(cdc_ncm_gstrings_stats); |
| 96 | default: |
| 97 | return -EOPNOTSUPP; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | static void cdc_ncm_get_ethtool_stats(struct net_device *netdev, |
| 102 | struct ethtool_stats __always_unused *stats, |
| 103 | u64 *data) |
| 104 | { |
| 105 | struct usbnet *dev = netdev_priv(netdev); |
| 106 | struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0]; |
| 107 | int i; |
| 108 | char *p = NULL; |
| 109 | |
| 110 | for (i = 0; i < ARRAY_SIZE(cdc_ncm_gstrings_stats); i++) { |
| 111 | p = (char *)ctx + cdc_ncm_gstrings_stats[i].stat_offset; |
| 112 | data[i] = (cdc_ncm_gstrings_stats[i].sizeof_stat == sizeof(u64)) ? *(u64 *)p : *(u32 *)p; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | static void cdc_ncm_get_strings(struct net_device __always_unused *netdev, u32 stringset, u8 *data) |
| 117 | { |
| 118 | u8 *p = data; |
| 119 | int i; |
| 120 | |
| 121 | switch (stringset) { |
| 122 | case ETH_SS_STATS: |
| 123 | for (i = 0; i < ARRAY_SIZE(cdc_ncm_gstrings_stats); i++) { |
| 124 | memcpy(p, cdc_ncm_gstrings_stats[i].stat_string, ETH_GSTRING_LEN); |
| 125 | p += ETH_GSTRING_LEN; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
Bjørn Mork | 6c4e548 | 2014-05-16 21:48:22 +0200 | [diff] [blame] | 130 | static int cdc_ncm_get_coalesce(struct net_device *netdev, |
| 131 | struct ethtool_coalesce *ec) |
| 132 | { |
| 133 | struct usbnet *dev = netdev_priv(netdev); |
| 134 | struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0]; |
| 135 | |
| 136 | /* assuming maximum sized dgrams and ignoring NDPs */ |
| 137 | ec->rx_max_coalesced_frames = ctx->rx_max / ctx->max_datagram_size; |
| 138 | ec->tx_max_coalesced_frames = ctx->tx_max / ctx->max_datagram_size; |
| 139 | |
| 140 | /* the timer will fire CDC_NCM_TIMER_PENDING_CNT times in a row */ |
| 141 | ec->tx_coalesce_usecs = (ctx->timer_interval * CDC_NCM_TIMER_PENDING_CNT) / NSEC_PER_USEC; |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static void cdc_ncm_update_rxtx_max(struct usbnet *dev, u32 new_rx, u32 new_tx); |
| 146 | |
| 147 | static int cdc_ncm_set_coalesce(struct net_device *netdev, |
| 148 | struct ethtool_coalesce *ec) |
| 149 | { |
| 150 | struct usbnet *dev = netdev_priv(netdev); |
| 151 | struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0]; |
| 152 | u32 new_rx_max = ctx->rx_max; |
| 153 | u32 new_tx_max = ctx->tx_max; |
| 154 | |
| 155 | /* assuming maximum sized dgrams and a single NDP */ |
| 156 | if (ec->rx_max_coalesced_frames) |
| 157 | new_rx_max = ec->rx_max_coalesced_frames * ctx->max_datagram_size; |
| 158 | if (ec->tx_max_coalesced_frames) |
| 159 | new_tx_max = ec->tx_max_coalesced_frames * ctx->max_datagram_size; |
| 160 | |
| 161 | if (ec->tx_coalesce_usecs && |
| 162 | (ec->tx_coalesce_usecs < CDC_NCM_TIMER_INTERVAL_MIN * CDC_NCM_TIMER_PENDING_CNT || |
| 163 | ec->tx_coalesce_usecs > CDC_NCM_TIMER_INTERVAL_MAX * CDC_NCM_TIMER_PENDING_CNT)) |
| 164 | return -EINVAL; |
| 165 | |
| 166 | spin_lock_bh(&ctx->mtx); |
| 167 | ctx->timer_interval = ec->tx_coalesce_usecs * NSEC_PER_USEC / CDC_NCM_TIMER_PENDING_CNT; |
| 168 | if (!ctx->timer_interval) |
| 169 | ctx->tx_timer_pending = 0; |
| 170 | spin_unlock_bh(&ctx->mtx); |
| 171 | |
| 172 | /* inform device of new values */ |
| 173 | if (new_rx_max != ctx->rx_max || new_tx_max != ctx->tx_max) |
| 174 | cdc_ncm_update_rxtx_max(dev, new_rx_max, new_tx_max); |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static const struct ethtool_ops cdc_ncm_ethtool_ops = { |
| 179 | .get_settings = usbnet_get_settings, |
| 180 | .set_settings = usbnet_set_settings, |
| 181 | .get_link = usbnet_get_link, |
| 182 | .nway_reset = usbnet_nway_reset, |
| 183 | .get_drvinfo = usbnet_get_drvinfo, |
| 184 | .get_msglevel = usbnet_get_msglevel, |
| 185 | .set_msglevel = usbnet_set_msglevel, |
| 186 | .get_ts_info = ethtool_op_get_ts_info, |
Bjørn Mork | beeecd4 | 2014-05-16 21:48:25 +0200 | [diff] [blame^] | 187 | .get_sset_count = cdc_ncm_get_sset_count, |
| 188 | .get_strings = cdc_ncm_get_strings, |
| 189 | .get_ethtool_stats = cdc_ncm_get_ethtool_stats, |
Bjørn Mork | 6c4e548 | 2014-05-16 21:48:22 +0200 | [diff] [blame] | 190 | .get_coalesce = cdc_ncm_get_coalesce, |
| 191 | .set_coalesce = cdc_ncm_set_coalesce, |
| 192 | }; |
| 193 | |
Bjørn Mork | 5aa73d5 | 2014-05-16 21:48:18 +0200 | [diff] [blame] | 194 | /* handle rx_max and tx_max changes */ |
| 195 | static void cdc_ncm_update_rxtx_max(struct usbnet *dev, u32 new_rx, u32 new_tx) |
| 196 | { |
| 197 | struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0]; |
| 198 | u8 iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber; |
| 199 | u32 val, max, min; |
| 200 | |
| 201 | /* clamp new_rx to sane values */ |
| 202 | min = USB_CDC_NCM_NTB_MIN_IN_SIZE; |
| 203 | max = min_t(u32, CDC_NCM_NTB_MAX_SIZE_RX, le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize)); |
| 204 | |
| 205 | /* dwNtbInMaxSize spec violation? Use MIN size for both limits */ |
| 206 | if (max < min) { |
| 207 | dev_warn(&dev->intf->dev, "dwNtbInMaxSize=%u is too small. Using %u\n", |
| 208 | le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize), min); |
| 209 | max = min; |
| 210 | } |
| 211 | |
| 212 | val = clamp_t(u32, new_rx, min, max); |
| 213 | if (val != new_rx) { |
| 214 | dev_dbg(&dev->intf->dev, "rx_max must be in the [%u, %u] range. Using %u\n", |
| 215 | min, max, val); |
| 216 | } |
| 217 | |
Bjørn Mork | 68864ab | 2014-05-16 21:48:21 +0200 | [diff] [blame] | 218 | /* usbnet use these values for sizing rx queues */ |
| 219 | dev->rx_urb_size = val; |
| 220 | |
Bjørn Mork | 5aa73d5 | 2014-05-16 21:48:18 +0200 | [diff] [blame] | 221 | /* inform device about NTB input size changes */ |
| 222 | if (val != ctx->rx_max) { |
| 223 | __le32 dwNtbInMaxSize = cpu_to_le32(val); |
| 224 | |
| 225 | dev_info(&dev->intf->dev, "setting rx_max = %u\n", val); |
Bjørn Mork | 68864ab | 2014-05-16 21:48:21 +0200 | [diff] [blame] | 226 | |
| 227 | /* need to unlink rx urbs before increasing buffer size */ |
| 228 | if (netif_running(dev->net) && dev->rx_urb_size > ctx->rx_max) |
| 229 | usbnet_unlink_rx_urbs(dev); |
| 230 | |
| 231 | /* tell device to use new size */ |
Bjørn Mork | 5aa73d5 | 2014-05-16 21:48:18 +0200 | [diff] [blame] | 232 | if (usbnet_write_cmd(dev, USB_CDC_SET_NTB_INPUT_SIZE, |
| 233 | USB_TYPE_CLASS | USB_DIR_OUT |
| 234 | | USB_RECIP_INTERFACE, |
| 235 | 0, iface_no, &dwNtbInMaxSize, 4) < 0) |
| 236 | dev_dbg(&dev->intf->dev, "Setting NTB Input Size failed\n"); |
| 237 | else |
| 238 | ctx->rx_max = val; |
| 239 | } |
| 240 | |
| 241 | /* clamp new_tx to sane values */ |
Bjørn Mork | 70559b8 | 2014-05-16 21:48:23 +0200 | [diff] [blame] | 242 | min = ctx->max_datagram_size + ctx->max_ndp_size + sizeof(struct usb_cdc_ncm_nth16); |
Bjørn Mork | 5aa73d5 | 2014-05-16 21:48:18 +0200 | [diff] [blame] | 243 | max = min_t(u32, CDC_NCM_NTB_MAX_SIZE_TX, le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize)); |
| 244 | |
| 245 | /* some devices set dwNtbOutMaxSize too low for the above default */ |
| 246 | min = min(min, max); |
| 247 | |
| 248 | val = clamp_t(u32, new_tx, min, max); |
| 249 | if (val != new_tx) { |
| 250 | dev_dbg(&dev->intf->dev, "tx_max must be in the [%u, %u] range. Using %u\n", |
| 251 | min, max, val); |
| 252 | } |
| 253 | if (val != ctx->tx_max) |
| 254 | dev_info(&dev->intf->dev, "setting tx_max = %u\n", val); |
Bjørn Mork | 08c74fc | 2014-05-16 21:48:20 +0200 | [diff] [blame] | 255 | |
| 256 | /* Adding a pad byte here if necessary simplifies the handling |
| 257 | * in cdc_ncm_fill_tx_frame, making tx_max always represent |
| 258 | * the real skb max size. |
| 259 | * |
| 260 | * We cannot use dev->maxpacket here because this is called from |
| 261 | * .bind which is called before usbnet sets up dev->maxpacket |
| 262 | */ |
Bjørn Mork | 68864ab | 2014-05-16 21:48:21 +0200 | [diff] [blame] | 263 | if (val != le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize) && |
| 264 | val % usb_maxpacket(dev->udev, dev->out, 1) == 0) |
| 265 | val++; |
Bjørn Mork | 08c74fc | 2014-05-16 21:48:20 +0200 | [diff] [blame] | 266 | |
Bjørn Mork | 68864ab | 2014-05-16 21:48:21 +0200 | [diff] [blame] | 267 | /* we might need to flush any pending tx buffers if running */ |
| 268 | if (netif_running(dev->net) && val > ctx->tx_max) { |
| 269 | netif_tx_lock_bh(dev->net); |
| 270 | usbnet_start_xmit(NULL, dev->net); |
| 271 | ctx->tx_max = val; |
| 272 | netif_tx_unlock_bh(dev->net); |
| 273 | } else { |
| 274 | ctx->tx_max = val; |
| 275 | } |
| 276 | |
Bjørn Mork | 08c74fc | 2014-05-16 21:48:20 +0200 | [diff] [blame] | 277 | dev->hard_mtu = ctx->tx_max; |
Bjørn Mork | 68864ab | 2014-05-16 21:48:21 +0200 | [diff] [blame] | 278 | |
| 279 | /* max qlen depend on hard_mtu and rx_urb_size */ |
| 280 | usbnet_update_max_qlen(dev); |
Bjørn Mork | 43e4c6d | 2014-05-16 21:48:24 +0200 | [diff] [blame] | 281 | |
| 282 | /* never pad more than 3 full USB packets per transfer */ |
| 283 | ctx->min_tx_pkt = clamp_t(u16, ctx->tx_max - 3 * usb_maxpacket(dev->udev, dev->out, 1), |
| 284 | CDC_NCM_MIN_TX_PKT, ctx->tx_max); |
Bjørn Mork | 5aa73d5 | 2014-05-16 21:48:18 +0200 | [diff] [blame] | 285 | } |
| 286 | |
Bjørn Mork | f8afb73 | 2014-05-16 21:48:19 +0200 | [diff] [blame] | 287 | /* helpers for NCM and MBIM differences */ |
| 288 | static u8 cdc_ncm_flags(struct usbnet *dev) |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 289 | { |
Bjørn Mork | bed6f76 | 2013-11-01 11:16:42 +0100 | [diff] [blame] | 290 | struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0]; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 291 | |
Bjørn Mork | f8afb73 | 2014-05-16 21:48:19 +0200 | [diff] [blame] | 292 | if (cdc_ncm_comm_intf_is_mbim(dev->intf->cur_altsetting) && ctx->mbim_desc) |
| 293 | return ctx->mbim_desc->bmNetworkCapabilities; |
| 294 | if (ctx->func_desc) |
| 295 | return ctx->func_desc->bmNetworkCapabilities; |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | static int cdc_ncm_eth_hlen(struct usbnet *dev) |
| 300 | { |
| 301 | if (cdc_ncm_comm_intf_is_mbim(dev->intf->cur_altsetting)) |
| 302 | return 0; |
| 303 | return ETH_HLEN; |
| 304 | } |
| 305 | |
| 306 | static u32 cdc_ncm_min_dgram_size(struct usbnet *dev) |
| 307 | { |
| 308 | if (cdc_ncm_comm_intf_is_mbim(dev->intf->cur_altsetting)) |
| 309 | return CDC_MBIM_MIN_DATAGRAM_SIZE; |
| 310 | return CDC_NCM_MIN_DATAGRAM_SIZE; |
| 311 | } |
| 312 | |
| 313 | static u32 cdc_ncm_max_dgram_size(struct usbnet *dev) |
| 314 | { |
| 315 | struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0]; |
| 316 | |
| 317 | if (cdc_ncm_comm_intf_is_mbim(dev->intf->cur_altsetting) && ctx->mbim_desc) |
| 318 | return le16_to_cpu(ctx->mbim_desc->wMaxSegmentSize); |
| 319 | if (ctx->ether_desc) |
| 320 | return le16_to_cpu(ctx->ether_desc->wMaxSegmentSize); |
| 321 | return CDC_NCM_MAX_DATAGRAM_SIZE; |
| 322 | } |
| 323 | |
| 324 | /* initial one-time device setup. MUST be called with the data interface |
| 325 | * in altsetting 0 |
| 326 | */ |
| 327 | static int cdc_ncm_init(struct usbnet *dev) |
| 328 | { |
| 329 | struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0]; |
| 330 | u8 iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber; |
| 331 | int err; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 332 | |
Ming Lei | 90b8b03 | 2012-10-24 19:46:56 +0000 | [diff] [blame] | 333 | err = usbnet_read_cmd(dev, USB_CDC_GET_NTB_PARAMETERS, |
| 334 | USB_TYPE_CLASS | USB_DIR_IN |
| 335 | |USB_RECIP_INTERFACE, |
Bjørn Mork | ff0992e9 | 2014-03-17 16:25:18 +0100 | [diff] [blame] | 336 | 0, iface_no, &ctx->ncm_parm, |
| 337 | sizeof(ctx->ncm_parm)); |
Giuseppe Scrivano | 36c3541 | 2011-08-03 22:10:29 +0000 | [diff] [blame] | 338 | if (err < 0) { |
Bjørn Mork | 59ede31 | 2013-11-01 11:16:59 +0100 | [diff] [blame] | 339 | dev_err(&dev->intf->dev, "failed GET_NTB_PARAMETERS\n"); |
| 340 | return err; /* GET_NTB_PARAMETERS is required */ |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Bjørn Mork | f8afb73 | 2014-05-16 21:48:19 +0200 | [diff] [blame] | 343 | /* set CRC Mode */ |
| 344 | if (cdc_ncm_flags(dev) & USB_CDC_NCM_NCAP_CRC_MODE) { |
| 345 | dev_dbg(&dev->intf->dev, "Setting CRC mode off\n"); |
| 346 | err = usbnet_write_cmd(dev, USB_CDC_SET_CRC_MODE, |
| 347 | USB_TYPE_CLASS | USB_DIR_OUT |
| 348 | | USB_RECIP_INTERFACE, |
| 349 | USB_CDC_NCM_CRC_NOT_APPENDED, |
| 350 | iface_no, NULL, 0); |
| 351 | if (err < 0) |
| 352 | dev_err(&dev->intf->dev, "SET_CRC_MODE failed\n"); |
| 353 | } |
| 354 | |
| 355 | /* set NTB format, if both formats are supported. |
| 356 | * |
| 357 | * "The host shall only send this command while the NCM Data |
| 358 | * Interface is in alternate setting 0." |
| 359 | */ |
| 360 | if (le16_to_cpu(ctx->ncm_parm.bmNtbFormatsSupported) & USB_CDC_NCM_NTH32_SIGN) { |
| 361 | dev_dbg(&dev->intf->dev, "Setting NTB format to 16-bit\n"); |
| 362 | err = usbnet_write_cmd(dev, USB_CDC_SET_NTB_FORMAT, |
| 363 | USB_TYPE_CLASS | USB_DIR_OUT |
| 364 | | USB_RECIP_INTERFACE, |
| 365 | USB_CDC_NCM_NTB16_FORMAT, |
| 366 | iface_no, NULL, 0); |
| 367 | if (err < 0) |
| 368 | dev_err(&dev->intf->dev, "SET_NTB_FORMAT failed\n"); |
| 369 | } |
| 370 | |
| 371 | /* set initial device values */ |
Bjørn Mork | ff0992e9 | 2014-03-17 16:25:18 +0100 | [diff] [blame] | 372 | ctx->rx_max = le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize); |
| 373 | ctx->tx_max = le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize); |
| 374 | ctx->tx_remainder = le16_to_cpu(ctx->ncm_parm.wNdpOutPayloadRemainder); |
| 375 | ctx->tx_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutDivisor); |
| 376 | ctx->tx_ndp_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutAlignment); |
Alexey Orishko | 84e77a8 | 2011-02-07 09:45:10 +0000 | [diff] [blame] | 377 | /* devices prior to NCM Errata shall set this field to zero */ |
Bjørn Mork | ff0992e9 | 2014-03-17 16:25:18 +0100 | [diff] [blame] | 378 | ctx->tx_max_datagrams = le16_to_cpu(ctx->ncm_parm.wNtbOutMaxDatagrams); |
Bjørn Mork | 47175e5 | 2013-11-01 11:16:58 +0100 | [diff] [blame] | 379 | |
Bjørn Mork | ae223cd | 2013-11-01 11:16:54 +0100 | [diff] [blame] | 380 | dev_dbg(&dev->intf->dev, |
| 381 | "dwNtbInMaxSize=%u dwNtbOutMaxSize=%u wNdpOutPayloadRemainder=%u wNdpOutDivisor=%u wNdpOutAlignment=%u wNtbOutMaxDatagrams=%u flags=0x%x\n", |
| 382 | ctx->rx_max, ctx->tx_max, ctx->tx_remainder, ctx->tx_modulus, |
Bjørn Mork | f8afb73 | 2014-05-16 21:48:19 +0200 | [diff] [blame] | 383 | ctx->tx_ndp_modulus, ctx->tx_max_datagrams, cdc_ncm_flags(dev)); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 384 | |
Alexey Orishko | 84e77a8 | 2011-02-07 09:45:10 +0000 | [diff] [blame] | 385 | /* max count of tx datagrams */ |
| 386 | if ((ctx->tx_max_datagrams == 0) || |
| 387 | (ctx->tx_max_datagrams > CDC_NCM_DPT_DATAGRAMS_MAX)) |
| 388 | ctx->tx_max_datagrams = CDC_NCM_DPT_DATAGRAMS_MAX; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 389 | |
Bjørn Mork | 70559b8 | 2014-05-16 21:48:23 +0200 | [diff] [blame] | 390 | /* set up maximum NDP size */ |
| 391 | ctx->max_ndp_size = sizeof(struct usb_cdc_ncm_ndp16) + (ctx->tx_max_datagrams + 1) * sizeof(struct usb_cdc_ncm_dpe16); |
| 392 | |
Bjørn Mork | 6c4e548 | 2014-05-16 21:48:22 +0200 | [diff] [blame] | 393 | /* initial coalescing timer interval */ |
| 394 | ctx->timer_interval = CDC_NCM_TIMER_INTERVAL_USEC * NSEC_PER_USEC; |
| 395 | |
Bjørn Mork | f8afb73 | 2014-05-16 21:48:19 +0200 | [diff] [blame] | 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | /* set a new max datagram size */ |
| 400 | static void cdc_ncm_set_dgram_size(struct usbnet *dev, int new_size) |
| 401 | { |
| 402 | struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0]; |
| 403 | u8 iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber; |
| 404 | __le16 max_datagram_size; |
| 405 | u16 mbim_mtu; |
| 406 | int err; |
| 407 | |
| 408 | /* set default based on descriptors */ |
| 409 | ctx->max_datagram_size = clamp_t(u32, new_size, |
| 410 | cdc_ncm_min_dgram_size(dev), |
| 411 | CDC_NCM_MAX_DATAGRAM_SIZE); |
| 412 | |
| 413 | /* inform the device about the selected Max Datagram Size? */ |
| 414 | if (!(cdc_ncm_flags(dev) & USB_CDC_NCM_NCAP_MAX_DATAGRAM_SIZE)) |
| 415 | goto out; |
| 416 | |
| 417 | /* read current mtu value from device */ |
| 418 | err = usbnet_read_cmd(dev, USB_CDC_GET_MAX_DATAGRAM_SIZE, |
| 419 | USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE, |
| 420 | 0, iface_no, &max_datagram_size, 2); |
| 421 | if (err < 0) { |
| 422 | dev_dbg(&dev->intf->dev, "GET_MAX_DATAGRAM_SIZE failed\n"); |
| 423 | goto out; |
| 424 | } |
| 425 | |
| 426 | if (le16_to_cpu(max_datagram_size) == ctx->max_datagram_size) |
| 427 | goto out; |
| 428 | |
| 429 | max_datagram_size = cpu_to_le16(ctx->max_datagram_size); |
| 430 | err = usbnet_write_cmd(dev, USB_CDC_SET_MAX_DATAGRAM_SIZE, |
| 431 | USB_TYPE_CLASS | USB_DIR_OUT | USB_RECIP_INTERFACE, |
| 432 | 0, iface_no, &max_datagram_size, 2); |
| 433 | if (err < 0) |
| 434 | dev_dbg(&dev->intf->dev, "SET_MAX_DATAGRAM_SIZE failed\n"); |
| 435 | |
| 436 | out: |
| 437 | /* set MTU to max supported by the device if necessary */ |
| 438 | dev->net->mtu = min_t(int, dev->net->mtu, ctx->max_datagram_size - cdc_ncm_eth_hlen(dev)); |
| 439 | |
| 440 | /* do not exceed operater preferred MTU */ |
| 441 | if (ctx->mbim_extended_desc) { |
| 442 | mbim_mtu = le16_to_cpu(ctx->mbim_extended_desc->wMTU); |
| 443 | if (mbim_mtu != 0 && mbim_mtu < dev->net->mtu) |
| 444 | dev->net->mtu = mbim_mtu; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | static void cdc_ncm_fix_modulus(struct usbnet *dev) |
| 449 | { |
| 450 | struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0]; |
| 451 | u32 val; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 452 | |
| 453 | /* |
| 454 | * verify that the structure alignment is: |
| 455 | * - power of two |
| 456 | * - not greater than the maximum transmit length |
| 457 | * - not less than four bytes |
| 458 | */ |
| 459 | val = ctx->tx_ndp_modulus; |
| 460 | |
| 461 | if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) || |
| 462 | (val != ((-val) & val)) || (val >= ctx->tx_max)) { |
Bjørn Mork | ae223cd | 2013-11-01 11:16:54 +0100 | [diff] [blame] | 463 | dev_dbg(&dev->intf->dev, "Using default alignment: 4 bytes\n"); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 464 | ctx->tx_ndp_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE; |
| 465 | } |
| 466 | |
| 467 | /* |
| 468 | * verify that the payload alignment is: |
| 469 | * - power of two |
| 470 | * - not greater than the maximum transmit length |
| 471 | * - not less than four bytes |
| 472 | */ |
| 473 | val = ctx->tx_modulus; |
| 474 | |
| 475 | if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) || |
| 476 | (val != ((-val) & val)) || (val >= ctx->tx_max)) { |
Bjørn Mork | ae223cd | 2013-11-01 11:16:54 +0100 | [diff] [blame] | 477 | dev_dbg(&dev->intf->dev, "Using default transmit modulus: 4 bytes\n"); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 478 | ctx->tx_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE; |
| 479 | } |
| 480 | |
| 481 | /* verify the payload remainder */ |
| 482 | if (ctx->tx_remainder >= ctx->tx_modulus) { |
Bjørn Mork | ae223cd | 2013-11-01 11:16:54 +0100 | [diff] [blame] | 483 | dev_dbg(&dev->intf->dev, "Using default transmit remainder: 0 bytes\n"); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 484 | ctx->tx_remainder = 0; |
| 485 | } |
| 486 | |
| 487 | /* adjust TX-remainder according to NCM specification. */ |
Bjørn Mork | f8afb73 | 2014-05-16 21:48:19 +0200 | [diff] [blame] | 488 | ctx->tx_remainder = ((ctx->tx_remainder - cdc_ncm_eth_hlen(dev)) & |
Greg Suarez | 2d1c431 | 2012-10-22 10:56:30 +0000 | [diff] [blame] | 489 | (ctx->tx_modulus - 1)); |
Bjørn Mork | f8afb73 | 2014-05-16 21:48:19 +0200 | [diff] [blame] | 490 | } |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 491 | |
Bjørn Mork | f8afb73 | 2014-05-16 21:48:19 +0200 | [diff] [blame] | 492 | static int cdc_ncm_setup(struct usbnet *dev) |
| 493 | { |
| 494 | struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0]; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 495 | |
Bjørn Mork | f8afb73 | 2014-05-16 21:48:19 +0200 | [diff] [blame] | 496 | /* clamp rx_max and tx_max and inform device */ |
| 497 | cdc_ncm_update_rxtx_max(dev, le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize), |
| 498 | le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize)); |
Alexey Orishko | 84e77a8 | 2011-02-07 09:45:10 +0000 | [diff] [blame] | 499 | |
Bjørn Mork | f8afb73 | 2014-05-16 21:48:19 +0200 | [diff] [blame] | 500 | /* sanitize the modulus and remainder values */ |
| 501 | cdc_ncm_fix_modulus(dev); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 502 | |
Bjørn Mork | f8afb73 | 2014-05-16 21:48:19 +0200 | [diff] [blame] | 503 | /* set max datagram size */ |
| 504 | cdc_ncm_set_dgram_size(dev, cdc_ncm_max_dgram_size(dev)); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | static void |
Bjørn Mork | ff1632a | 2013-11-01 11:16:41 +0100 | [diff] [blame] | 509 | cdc_ncm_find_endpoints(struct usbnet *dev, struct usb_interface *intf) |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 510 | { |
Bjørn Mork | ff1632a | 2013-11-01 11:16:41 +0100 | [diff] [blame] | 511 | struct usb_host_endpoint *e, *in = NULL, *out = NULL; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 512 | u8 ep; |
| 513 | |
| 514 | for (ep = 0; ep < intf->cur_altsetting->desc.bNumEndpoints; ep++) { |
| 515 | |
| 516 | e = intf->cur_altsetting->endpoint + ep; |
| 517 | switch (e->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) { |
| 518 | case USB_ENDPOINT_XFER_INT: |
| 519 | if (usb_endpoint_dir_in(&e->desc)) { |
Bjørn Mork | ff1632a | 2013-11-01 11:16:41 +0100 | [diff] [blame] | 520 | if (!dev->status) |
| 521 | dev->status = e; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 522 | } |
| 523 | break; |
| 524 | |
| 525 | case USB_ENDPOINT_XFER_BULK: |
| 526 | if (usb_endpoint_dir_in(&e->desc)) { |
Bjørn Mork | ff1632a | 2013-11-01 11:16:41 +0100 | [diff] [blame] | 527 | if (!in) |
| 528 | in = e; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 529 | } else { |
Bjørn Mork | ff1632a | 2013-11-01 11:16:41 +0100 | [diff] [blame] | 530 | if (!out) |
| 531 | out = e; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 532 | } |
| 533 | break; |
| 534 | |
| 535 | default: |
| 536 | break; |
| 537 | } |
| 538 | } |
Bjørn Mork | ff1632a | 2013-11-01 11:16:41 +0100 | [diff] [blame] | 539 | if (in && !dev->in) |
| 540 | dev->in = usb_rcvbulkpipe(dev->udev, |
| 541 | in->desc.bEndpointAddress & |
| 542 | USB_ENDPOINT_NUMBER_MASK); |
| 543 | if (out && !dev->out) |
| 544 | dev->out = usb_sndbulkpipe(dev->udev, |
| 545 | out->desc.bEndpointAddress & |
| 546 | USB_ENDPOINT_NUMBER_MASK); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | static void cdc_ncm_free(struct cdc_ncm_ctx *ctx) |
| 550 | { |
| 551 | if (ctx == NULL) |
| 552 | return; |
| 553 | |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 554 | if (ctx->tx_rem_skb != NULL) { |
| 555 | dev_kfree_skb_any(ctx->tx_rem_skb); |
| 556 | ctx->tx_rem_skb = NULL; |
| 557 | } |
| 558 | |
| 559 | if (ctx->tx_curr_skb != NULL) { |
| 560 | dev_kfree_skb_any(ctx->tx_curr_skb); |
| 561 | ctx->tx_curr_skb = NULL; |
| 562 | } |
| 563 | |
| 564 | kfree(ctx); |
| 565 | } |
| 566 | |
Bjørn Mork | c91ce3b | 2012-10-22 10:56:35 +0000 | [diff] [blame] | 567 | int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_altsetting) |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 568 | { |
Bjørn Mork | 8329223 | 2013-11-01 11:16:47 +0100 | [diff] [blame] | 569 | const struct usb_cdc_union_desc *union_desc = NULL; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 570 | struct cdc_ncm_ctx *ctx; |
| 571 | struct usb_driver *driver; |
| 572 | u8 *buf; |
| 573 | int len; |
| 574 | int temp; |
| 575 | u8 iface_no; |
| 576 | |
Thomas Meyer | c796984 | 2011-11-17 12:43:40 +0000 | [diff] [blame] | 577 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
Devendra Naga | 8f0923c | 2013-03-29 23:03:22 +0000 | [diff] [blame] | 578 | if (!ctx) |
| 579 | return -ENOMEM; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 580 | |
Alexey Orishko | c84ff1d | 2012-03-14 11:26:10 +0000 | [diff] [blame] | 581 | hrtimer_init(&ctx->tx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 582 | ctx->tx_timer.function = &cdc_ncm_tx_timer_cb; |
Bjørn Mork | bed6f76 | 2013-11-01 11:16:42 +0100 | [diff] [blame] | 583 | ctx->bh.data = (unsigned long)dev; |
Alexey Orishko | c84ff1d | 2012-03-14 11:26:10 +0000 | [diff] [blame] | 584 | ctx->bh.func = cdc_ncm_txpath_bh; |
| 585 | atomic_set(&ctx->stop, 0); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 586 | spin_lock_init(&ctx->mtx); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 587 | |
| 588 | /* store ctx pointer in device data field */ |
| 589 | dev->data[0] = (unsigned long)ctx; |
| 590 | |
Bjørn Mork | 9fe0234 | 2013-11-01 11:16:48 +0100 | [diff] [blame] | 591 | /* only the control interface can be successfully probed */ |
| 592 | ctx->control = intf; |
| 593 | |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 594 | /* get some pointers */ |
| 595 | driver = driver_of(intf); |
| 596 | buf = intf->cur_altsetting->extra; |
| 597 | len = intf->cur_altsetting->extralen; |
| 598 | |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 599 | /* parse through descriptors associated with control interface */ |
| 600 | while ((len > 0) && (buf[0] > 2) && (buf[0] <= len)) { |
| 601 | |
| 602 | if (buf[1] != USB_DT_CS_INTERFACE) |
| 603 | goto advance; |
| 604 | |
| 605 | switch (buf[2]) { |
| 606 | case USB_CDC_UNION_TYPE: |
Bjørn Mork | 8329223 | 2013-11-01 11:16:47 +0100 | [diff] [blame] | 607 | if (buf[0] < sizeof(*union_desc)) |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 608 | break; |
| 609 | |
Bjørn Mork | 8329223 | 2013-11-01 11:16:47 +0100 | [diff] [blame] | 610 | union_desc = (const struct usb_cdc_union_desc *)buf; |
Bjørn Mork | 9fe0234 | 2013-11-01 11:16:48 +0100 | [diff] [blame] | 611 | /* the master must be the interface we are probing */ |
| 612 | if (intf->cur_altsetting->desc.bInterfaceNumber != |
Bjørn Mork | 296e81f | 2013-11-01 11:17:00 +0100 | [diff] [blame] | 613 | union_desc->bMasterInterface0) { |
| 614 | dev_dbg(&intf->dev, "bogus CDC Union\n"); |
Bjørn Mork | 9fe0234 | 2013-11-01 11:16:48 +0100 | [diff] [blame] | 615 | goto error; |
Bjørn Mork | 296e81f | 2013-11-01 11:17:00 +0100 | [diff] [blame] | 616 | } |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 617 | ctx->data = usb_ifnum_to_if(dev->udev, |
Bjørn Mork | 8329223 | 2013-11-01 11:16:47 +0100 | [diff] [blame] | 618 | union_desc->bSlaveInterface0); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 619 | break; |
| 620 | |
| 621 | case USB_CDC_ETHERNET_TYPE: |
| 622 | if (buf[0] < sizeof(*(ctx->ether_desc))) |
| 623 | break; |
| 624 | |
| 625 | ctx->ether_desc = |
| 626 | (const struct usb_cdc_ether_desc *)buf; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 627 | break; |
| 628 | |
| 629 | case USB_CDC_NCM_TYPE: |
| 630 | if (buf[0] < sizeof(*(ctx->func_desc))) |
| 631 | break; |
| 632 | |
| 633 | ctx->func_desc = (const struct usb_cdc_ncm_desc *)buf; |
| 634 | break; |
| 635 | |
Greg Suarez | 38396e4 | 2012-10-22 10:56:31 +0000 | [diff] [blame] | 636 | case USB_CDC_MBIM_TYPE: |
| 637 | if (buf[0] < sizeof(*(ctx->mbim_desc))) |
| 638 | break; |
| 639 | |
| 640 | ctx->mbim_desc = (const struct usb_cdc_mbim_desc *)buf; |
| 641 | break; |
| 642 | |
Ben Chan | 259fef0 | 2014-03-19 14:00:06 -0700 | [diff] [blame] | 643 | case USB_CDC_MBIM_EXTENDED_TYPE: |
| 644 | if (buf[0] < sizeof(*(ctx->mbim_extended_desc))) |
| 645 | break; |
| 646 | |
| 647 | ctx->mbim_extended_desc = |
| 648 | (const struct usb_cdc_mbim_extended_desc *)buf; |
| 649 | break; |
| 650 | |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 651 | default: |
| 652 | break; |
| 653 | } |
| 654 | advance: |
| 655 | /* advance to next descriptor */ |
| 656 | temp = buf[0]; |
| 657 | buf += temp; |
| 658 | len -= temp; |
| 659 | } |
| 660 | |
Bjørn Mork | 9992c2e | 2013-01-21 05:50:38 +0000 | [diff] [blame] | 661 | /* some buggy devices have an IAD but no CDC Union */ |
Bjørn Mork | 8329223 | 2013-11-01 11:16:47 +0100 | [diff] [blame] | 662 | if (!union_desc && intf->intf_assoc && intf->intf_assoc->bInterfaceCount == 2) { |
Bjørn Mork | 56a666d | 2013-01-25 23:36:59 +0000 | [diff] [blame] | 663 | ctx->data = usb_ifnum_to_if(dev->udev, intf->cur_altsetting->desc.bInterfaceNumber + 1); |
| 664 | dev_dbg(&intf->dev, "CDC Union missing - got slave from IAD\n"); |
Bjørn Mork | 9992c2e | 2013-01-21 05:50:38 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 667 | /* check if we got everything */ |
Bjørn Mork | f8afb73 | 2014-05-16 21:48:19 +0200 | [diff] [blame] | 668 | if (!ctx->data) { |
| 669 | dev_dbg(&intf->dev, "CDC Union missing and no IAD found\n"); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 670 | goto error; |
Bjørn Mork | 296e81f | 2013-11-01 11:17:00 +0100 | [diff] [blame] | 671 | } |
Bjørn Mork | f8afb73 | 2014-05-16 21:48:19 +0200 | [diff] [blame] | 672 | if (cdc_ncm_comm_intf_is_mbim(intf->cur_altsetting)) { |
| 673 | if (!ctx->mbim_desc) { |
| 674 | dev_dbg(&intf->dev, "MBIM functional descriptor missing\n"); |
| 675 | goto error; |
| 676 | } |
| 677 | } else { |
| 678 | if (!ctx->ether_desc || !ctx->func_desc) { |
| 679 | dev_dbg(&intf->dev, "NCM or ECM functional descriptors missing\n"); |
| 680 | goto error; |
| 681 | } |
| 682 | } |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 683 | |
Bjørn Mork | bbc8d92 | 2012-11-13 03:19:43 +0000 | [diff] [blame] | 684 | /* claim data interface, if different from control */ |
| 685 | if (ctx->data != ctx->control) { |
| 686 | temp = usb_driver_claim_interface(driver, ctx->data, dev); |
Bjørn Mork | 296e81f | 2013-11-01 11:17:00 +0100 | [diff] [blame] | 687 | if (temp) { |
| 688 | dev_dbg(&intf->dev, "failed to claim data intf\n"); |
Bjørn Mork | bbc8d92 | 2012-11-13 03:19:43 +0000 | [diff] [blame] | 689 | goto error; |
Bjørn Mork | 296e81f | 2013-11-01 11:17:00 +0100 | [diff] [blame] | 690 | } |
Bjørn Mork | bbc8d92 | 2012-11-13 03:19:43 +0000 | [diff] [blame] | 691 | } |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 692 | |
| 693 | iface_no = ctx->data->cur_altsetting->desc.bInterfaceNumber; |
| 694 | |
| 695 | /* reset data interface */ |
| 696 | temp = usb_set_interface(dev->udev, iface_no, 0); |
Bjørn Mork | 296e81f | 2013-11-01 11:17:00 +0100 | [diff] [blame] | 697 | if (temp) { |
| 698 | dev_dbg(&intf->dev, "set interface failed\n"); |
Alexey Orishko | 19694ac | 2011-05-24 05:26:13 +0000 | [diff] [blame] | 699 | goto error2; |
Bjørn Mork | 296e81f | 2013-11-01 11:17:00 +0100 | [diff] [blame] | 700 | } |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 701 | |
Bjørn Mork | 08c74fc | 2014-05-16 21:48:20 +0200 | [diff] [blame] | 702 | /* initialize basic device settings */ |
| 703 | if (cdc_ncm_init(dev)) |
Bjørn Mork | ff0992e9 | 2014-03-17 16:25:18 +0100 | [diff] [blame] | 704 | goto error2; |
| 705 | |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 706 | /* configure data interface */ |
Greg Suarez | 38396e4 | 2012-10-22 10:56:31 +0000 | [diff] [blame] | 707 | temp = usb_set_interface(dev->udev, iface_no, data_altsetting); |
Bjørn Mork | 296e81f | 2013-11-01 11:17:00 +0100 | [diff] [blame] | 708 | if (temp) { |
| 709 | dev_dbg(&intf->dev, "set interface failed\n"); |
Alexey Orishko | 19694ac | 2011-05-24 05:26:13 +0000 | [diff] [blame] | 710 | goto error2; |
Bjørn Mork | 296e81f | 2013-11-01 11:17:00 +0100 | [diff] [blame] | 711 | } |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 712 | |
Bjørn Mork | ff1632a | 2013-11-01 11:16:41 +0100 | [diff] [blame] | 713 | cdc_ncm_find_endpoints(dev, ctx->data); |
| 714 | cdc_ncm_find_endpoints(dev, ctx->control); |
Bjørn Mork | 296e81f | 2013-11-01 11:17:00 +0100 | [diff] [blame] | 715 | if (!dev->in || !dev->out || !dev->status) { |
| 716 | dev_dbg(&intf->dev, "failed to collect endpoints\n"); |
Alexey Orishko | 19694ac | 2011-05-24 05:26:13 +0000 | [diff] [blame] | 717 | goto error2; |
Bjørn Mork | 296e81f | 2013-11-01 11:17:00 +0100 | [diff] [blame] | 718 | } |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 719 | |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 720 | usb_set_intfdata(ctx->data, dev); |
| 721 | usb_set_intfdata(ctx->control, dev); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 722 | |
Greg Suarez | 38396e4 | 2012-10-22 10:56:31 +0000 | [diff] [blame] | 723 | if (ctx->ether_desc) { |
| 724 | temp = usbnet_get_ethernet_addr(dev, ctx->ether_desc->iMACAddress); |
Bjørn Mork | 296e81f | 2013-11-01 11:17:00 +0100 | [diff] [blame] | 725 | if (temp) { |
| 726 | dev_dbg(&intf->dev, "failed to get mac address\n"); |
Greg Suarez | 38396e4 | 2012-10-22 10:56:31 +0000 | [diff] [blame] | 727 | goto error2; |
Bjørn Mork | 296e81f | 2013-11-01 11:17:00 +0100 | [diff] [blame] | 728 | } |
| 729 | dev_info(&intf->dev, "MAC-Address: %pM\n", dev->net->dev_addr); |
Greg Suarez | 38396e4 | 2012-10-22 10:56:31 +0000 | [diff] [blame] | 730 | } |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 731 | |
Bjørn Mork | 08c74fc | 2014-05-16 21:48:20 +0200 | [diff] [blame] | 732 | /* finish setting up the device specific data */ |
| 733 | cdc_ncm_setup(dev); |
Bjørn Mork | ff0992e9 | 2014-03-17 16:25:18 +0100 | [diff] [blame] | 734 | |
Bjørn Mork | 6c4e548 | 2014-05-16 21:48:22 +0200 | [diff] [blame] | 735 | /* override ethtool_ops */ |
| 736 | dev->net->ethtool_ops = &cdc_ncm_ethtool_ops; |
| 737 | |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 738 | return 0; |
| 739 | |
Alexey Orishko | 19694ac | 2011-05-24 05:26:13 +0000 | [diff] [blame] | 740 | error2: |
| 741 | usb_set_intfdata(ctx->control, NULL); |
| 742 | usb_set_intfdata(ctx->data, NULL); |
Bjørn Mork | 6b4ef60 | 2013-01-21 05:50:40 +0000 | [diff] [blame] | 743 | if (ctx->data != ctx->control) |
| 744 | usb_driver_release_interface(driver, ctx->data); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 745 | error: |
| 746 | cdc_ncm_free((struct cdc_ncm_ctx *)dev->data[0]); |
| 747 | dev->data[0] = 0; |
Bjørn Mork | 296e81f | 2013-11-01 11:17:00 +0100 | [diff] [blame] | 748 | dev_info(&intf->dev, "bind() failure\n"); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 749 | return -ENODEV; |
| 750 | } |
Bjørn Mork | c91ce3b | 2012-10-22 10:56:35 +0000 | [diff] [blame] | 751 | EXPORT_SYMBOL_GPL(cdc_ncm_bind_common); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 752 | |
Bjørn Mork | c91ce3b | 2012-10-22 10:56:35 +0000 | [diff] [blame] | 753 | void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf) |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 754 | { |
| 755 | struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0]; |
Alexey Orishko | 19694ac | 2011-05-24 05:26:13 +0000 | [diff] [blame] | 756 | struct usb_driver *driver = driver_of(intf); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 757 | |
| 758 | if (ctx == NULL) |
| 759 | return; /* no setup */ |
| 760 | |
Alexey Orishko | c84ff1d | 2012-03-14 11:26:10 +0000 | [diff] [blame] | 761 | atomic_set(&ctx->stop, 1); |
| 762 | |
| 763 | if (hrtimer_active(&ctx->tx_timer)) |
| 764 | hrtimer_cancel(&ctx->tx_timer); |
| 765 | |
| 766 | tasklet_kill(&ctx->bh); |
| 767 | |
Bjørn Mork | bbc8d92 | 2012-11-13 03:19:43 +0000 | [diff] [blame] | 768 | /* handle devices with combined control and data interface */ |
| 769 | if (ctx->control == ctx->data) |
| 770 | ctx->data = NULL; |
| 771 | |
Alexey Orishko | 19694ac | 2011-05-24 05:26:13 +0000 | [diff] [blame] | 772 | /* disconnect master --> disconnect slave */ |
| 773 | if (intf == ctx->control && ctx->data) { |
| 774 | usb_set_intfdata(ctx->data, NULL); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 775 | usb_driver_release_interface(driver, ctx->data); |
Alexey Orishko | 19694ac | 2011-05-24 05:26:13 +0000 | [diff] [blame] | 776 | ctx->data = NULL; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 777 | |
Alexey Orishko | 19694ac | 2011-05-24 05:26:13 +0000 | [diff] [blame] | 778 | } else if (intf == ctx->data && ctx->control) { |
| 779 | usb_set_intfdata(ctx->control, NULL); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 780 | usb_driver_release_interface(driver, ctx->control); |
Alexey Orishko | 19694ac | 2011-05-24 05:26:13 +0000 | [diff] [blame] | 781 | ctx->control = NULL; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Bjørn Mork | 3e51566 | 2013-11-01 11:16:40 +0100 | [diff] [blame] | 784 | usb_set_intfdata(intf, NULL); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 785 | cdc_ncm_free(ctx); |
| 786 | } |
Bjørn Mork | c91ce3b | 2012-10-22 10:56:35 +0000 | [diff] [blame] | 787 | EXPORT_SYMBOL_GPL(cdc_ncm_unbind); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 788 | |
Bjørn Mork | 50a0ffa | 2014-05-11 10:47:15 +0200 | [diff] [blame] | 789 | /* Return the number of the MBIM control interface altsetting iff it |
| 790 | * is preferred and available, |
Bjørn Mork | 1e8bbe6 | 2013-03-14 01:05:13 +0000 | [diff] [blame] | 791 | */ |
Bjørn Mork | 50a0ffa | 2014-05-11 10:47:15 +0200 | [diff] [blame] | 792 | u8 cdc_ncm_select_altsetting(struct usb_interface *intf) |
Greg Suarez | 38396e4 | 2012-10-22 10:56:31 +0000 | [diff] [blame] | 793 | { |
Bjørn Mork | 1e8bbe6 | 2013-03-14 01:05:13 +0000 | [diff] [blame] | 794 | struct usb_host_interface *alt; |
Greg Suarez | 38396e4 | 2012-10-22 10:56:31 +0000 | [diff] [blame] | 795 | |
Bjørn Mork | bd329e1 | 2012-10-22 10:56:38 +0000 | [diff] [blame] | 796 | /* The MBIM spec defines a NCM compatible default altsetting, |
| 797 | * which we may have matched: |
| 798 | * |
| 799 | * "Functions that implement both NCM 1.0 and MBIM (an |
| 800 | * “NCM/MBIM function”) according to this recommendation |
| 801 | * shall provide two alternate settings for the |
| 802 | * Communication Interface. Alternate setting 0, and the |
| 803 | * associated class and endpoint descriptors, shall be |
| 804 | * constructed according to the rules given for the |
| 805 | * Communication Interface in section 5 of [USBNCM10]. |
| 806 | * Alternate setting 1, and the associated class and |
| 807 | * endpoint descriptors, shall be constructed according to |
| 808 | * the rules given in section 6 (USB Device Model) of this |
| 809 | * specification." |
Bjørn Mork | bd329e1 | 2012-10-22 10:56:38 +0000 | [diff] [blame] | 810 | */ |
Bjørn Mork | 50a0ffa | 2014-05-11 10:47:15 +0200 | [diff] [blame] | 811 | if (intf->num_altsetting < 2) |
| 812 | return intf->cur_altsetting->desc.bAlternateSetting; |
| 813 | |
| 814 | if (prefer_mbim) { |
Bjørn Mork | 1e8bbe6 | 2013-03-14 01:05:13 +0000 | [diff] [blame] | 815 | alt = usb_altnum_to_altsetting(intf, CDC_NCM_COMM_ALTSETTING_MBIM); |
Bjørn Mork | 50a0ffa | 2014-05-11 10:47:15 +0200 | [diff] [blame] | 816 | if (alt && cdc_ncm_comm_intf_is_mbim(alt)) |
| 817 | return CDC_NCM_COMM_ALTSETTING_MBIM; |
Bjørn Mork | f350ca0 | 2013-02-13 12:09:52 +0000 | [diff] [blame] | 818 | } |
Bjørn Mork | 50a0ffa | 2014-05-11 10:47:15 +0200 | [diff] [blame] | 819 | return CDC_NCM_COMM_ALTSETTING_NCM; |
Bjørn Mork | 1e8bbe6 | 2013-03-14 01:05:13 +0000 | [diff] [blame] | 820 | } |
| 821 | EXPORT_SYMBOL_GPL(cdc_ncm_select_altsetting); |
| 822 | |
| 823 | static int cdc_ncm_bind(struct usbnet *dev, struct usb_interface *intf) |
| 824 | { |
| 825 | int ret; |
| 826 | |
| 827 | /* MBIM backwards compatible function? */ |
Bjørn Mork | 50a0ffa | 2014-05-11 10:47:15 +0200 | [diff] [blame] | 828 | if (cdc_ncm_select_altsetting(intf) != CDC_NCM_COMM_ALTSETTING_NCM) |
Bjørn Mork | 1e8bbe6 | 2013-03-14 01:05:13 +0000 | [diff] [blame] | 829 | return -ENODEV; |
Bjørn Mork | bd329e1 | 2012-10-22 10:56:38 +0000 | [diff] [blame] | 830 | |
Bjørn Mork | 50a0ffa | 2014-05-11 10:47:15 +0200 | [diff] [blame] | 831 | /* The NCM data altsetting is fixed */ |
| 832 | ret = cdc_ncm_bind_common(dev, intf, CDC_NCM_DATA_ALTSETTING_NCM); |
Greg Suarez | 38396e4 | 2012-10-22 10:56:31 +0000 | [diff] [blame] | 833 | |
| 834 | /* |
| 835 | * We should get an event when network connection is "connected" or |
| 836 | * "disconnected". Set network connection in "disconnected" state |
| 837 | * (carrier is OFF) during attach, so the IP network stack does not |
| 838 | * start IPv6 negotiation and more. |
| 839 | */ |
Ming Lei | 8a34b0a | 2013-04-11 04:40:33 +0000 | [diff] [blame] | 840 | usbnet_link_change(dev, 0, 0); |
Greg Suarez | 38396e4 | 2012-10-22 10:56:31 +0000 | [diff] [blame] | 841 | return ret; |
| 842 | } |
| 843 | |
Bjørn Mork | c78b7c5 | 2012-10-22 10:56:34 +0000 | [diff] [blame] | 844 | static void cdc_ncm_align_tail(struct sk_buff *skb, size_t modulus, size_t remainder, size_t max) |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 845 | { |
Bjørn Mork | c78b7c5 | 2012-10-22 10:56:34 +0000 | [diff] [blame] | 846 | size_t align = ALIGN(skb->len, modulus) - skb->len + remainder; |
| 847 | |
| 848 | if (skb->len + align > max) |
| 849 | align = max - skb->len; |
| 850 | if (align && skb_tailroom(skb) >= align) |
| 851 | memset(skb_put(skb, align), 0, align); |
| 852 | } |
| 853 | |
| 854 | /* return a pointer to a valid struct usb_cdc_ncm_ndp16 of type sign, possibly |
| 855 | * allocating a new one within skb |
| 856 | */ |
| 857 | static struct usb_cdc_ncm_ndp16 *cdc_ncm_ndp(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign, size_t reserve) |
| 858 | { |
| 859 | struct usb_cdc_ncm_ndp16 *ndp16 = NULL; |
| 860 | struct usb_cdc_ncm_nth16 *nth16 = (void *)skb->data; |
| 861 | size_t ndpoffset = le16_to_cpu(nth16->wNdpIndex); |
| 862 | |
| 863 | /* follow the chain of NDPs, looking for a match */ |
| 864 | while (ndpoffset) { |
| 865 | ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb->data + ndpoffset); |
| 866 | if (ndp16->dwSignature == sign) |
| 867 | return ndp16; |
| 868 | ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex); |
| 869 | } |
| 870 | |
| 871 | /* align new NDP */ |
| 872 | cdc_ncm_align_tail(skb, ctx->tx_ndp_modulus, 0, ctx->tx_max); |
| 873 | |
| 874 | /* verify that there is room for the NDP and the datagram (reserve) */ |
Bjørn Mork | 70559b8 | 2014-05-16 21:48:23 +0200 | [diff] [blame] | 875 | if ((ctx->tx_max - skb->len - reserve) < ctx->max_ndp_size) |
Bjørn Mork | c78b7c5 | 2012-10-22 10:56:34 +0000 | [diff] [blame] | 876 | return NULL; |
| 877 | |
| 878 | /* link to it */ |
| 879 | if (ndp16) |
| 880 | ndp16->wNextNdpIndex = cpu_to_le16(skb->len); |
| 881 | else |
| 882 | nth16->wNdpIndex = cpu_to_le16(skb->len); |
| 883 | |
| 884 | /* push a new empty NDP */ |
Bjørn Mork | 70559b8 | 2014-05-16 21:48:23 +0200 | [diff] [blame] | 885 | ndp16 = (struct usb_cdc_ncm_ndp16 *)memset(skb_put(skb, ctx->max_ndp_size), 0, ctx->max_ndp_size); |
Bjørn Mork | c78b7c5 | 2012-10-22 10:56:34 +0000 | [diff] [blame] | 886 | ndp16->dwSignature = sign; |
| 887 | ndp16->wLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_ndp16) + sizeof(struct usb_cdc_ncm_dpe16)); |
| 888 | return ndp16; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 889 | } |
| 890 | |
Bjørn Mork | c91ce3b | 2012-10-22 10:56:35 +0000 | [diff] [blame] | 891 | struct sk_buff * |
Bjørn Mork | bed6f76 | 2013-11-01 11:16:42 +0100 | [diff] [blame] | 892 | cdc_ncm_fill_tx_frame(struct usbnet *dev, struct sk_buff *skb, __le32 sign) |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 893 | { |
Bjørn Mork | bed6f76 | 2013-11-01 11:16:42 +0100 | [diff] [blame] | 894 | struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0]; |
Bjørn Mork | c78b7c5 | 2012-10-22 10:56:34 +0000 | [diff] [blame] | 895 | struct usb_cdc_ncm_nth16 *nth16; |
| 896 | struct usb_cdc_ncm_ndp16 *ndp16; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 897 | struct sk_buff *skb_out; |
Bjørn Mork | c78b7c5 | 2012-10-22 10:56:34 +0000 | [diff] [blame] | 898 | u16 n = 0, index, ndplen; |
Alexey Orishko | 84e77a8 | 2011-02-07 09:45:10 +0000 | [diff] [blame] | 899 | u8 ready2send = 0; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 900 | |
| 901 | /* if there is a remaining skb, it gets priority */ |
Bjørn Mork | c78b7c5 | 2012-10-22 10:56:34 +0000 | [diff] [blame] | 902 | if (skb != NULL) { |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 903 | swap(skb, ctx->tx_rem_skb); |
Bjørn Mork | c78b7c5 | 2012-10-22 10:56:34 +0000 | [diff] [blame] | 904 | swap(sign, ctx->tx_rem_sign); |
| 905 | } else { |
Alexey Orishko | 84e77a8 | 2011-02-07 09:45:10 +0000 | [diff] [blame] | 906 | ready2send = 1; |
Bjørn Mork | c78b7c5 | 2012-10-22 10:56:34 +0000 | [diff] [blame] | 907 | } |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 908 | |
| 909 | /* check if we are resuming an OUT skb */ |
Bjørn Mork | c78b7c5 | 2012-10-22 10:56:34 +0000 | [diff] [blame] | 910 | skb_out = ctx->tx_curr_skb; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 911 | |
Bjørn Mork | c78b7c5 | 2012-10-22 10:56:34 +0000 | [diff] [blame] | 912 | /* allocate a new OUT skb */ |
| 913 | if (!skb_out) { |
Bjørn Mork | 2057222 | 2013-11-01 11:16:38 +0100 | [diff] [blame] | 914 | skb_out = alloc_skb(ctx->tx_max, GFP_ATOMIC); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 915 | if (skb_out == NULL) { |
| 916 | if (skb != NULL) { |
| 917 | dev_kfree_skb_any(skb); |
Bjørn Mork | bed6f76 | 2013-11-01 11:16:42 +0100 | [diff] [blame] | 918 | dev->net->stats.tx_dropped++; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 919 | } |
| 920 | goto exit_no_skb; |
| 921 | } |
Bjørn Mork | c78b7c5 | 2012-10-22 10:56:34 +0000 | [diff] [blame] | 922 | /* fill out the initial 16-bit NTB header */ |
| 923 | nth16 = (struct usb_cdc_ncm_nth16 *)memset(skb_put(skb_out, sizeof(struct usb_cdc_ncm_nth16)), 0, sizeof(struct usb_cdc_ncm_nth16)); |
| 924 | nth16->dwSignature = cpu_to_le32(USB_CDC_NCM_NTH16_SIGN); |
| 925 | nth16->wHeaderLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_nth16)); |
| 926 | nth16->wSequence = cpu_to_le16(ctx->tx_seq++); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 927 | |
Bjørn Mork | c78b7c5 | 2012-10-22 10:56:34 +0000 | [diff] [blame] | 928 | /* count total number of frames in this NTB */ |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 929 | ctx->tx_curr_frame_num = 0; |
Bjørn Mork | beeecd4 | 2014-05-16 21:48:25 +0200 | [diff] [blame^] | 930 | |
| 931 | /* recent payload counter for this skb_out */ |
| 932 | ctx->tx_curr_frame_payload = 0; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 933 | } |
| 934 | |
Bjørn Mork | c78b7c5 | 2012-10-22 10:56:34 +0000 | [diff] [blame] | 935 | for (n = ctx->tx_curr_frame_num; n < ctx->tx_max_datagrams; n++) { |
| 936 | /* send any remaining skb first */ |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 937 | if (skb == NULL) { |
| 938 | skb = ctx->tx_rem_skb; |
Bjørn Mork | c78b7c5 | 2012-10-22 10:56:34 +0000 | [diff] [blame] | 939 | sign = ctx->tx_rem_sign; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 940 | ctx->tx_rem_skb = NULL; |
| 941 | |
| 942 | /* check for end of skb */ |
| 943 | if (skb == NULL) |
| 944 | break; |
| 945 | } |
| 946 | |
Bjørn Mork | c78b7c5 | 2012-10-22 10:56:34 +0000 | [diff] [blame] | 947 | /* get the appropriate NDP for this skb */ |
| 948 | ndp16 = cdc_ncm_ndp(ctx, skb_out, sign, skb->len + ctx->tx_modulus + ctx->tx_remainder); |
| 949 | |
| 950 | /* align beginning of next frame */ |
| 951 | cdc_ncm_align_tail(skb_out, ctx->tx_modulus, ctx->tx_remainder, ctx->tx_max); |
| 952 | |
| 953 | /* check if we had enough room left for both NDP and frame */ |
| 954 | if (!ndp16 || skb_out->len + skb->len > ctx->tx_max) { |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 955 | if (n == 0) { |
| 956 | /* won't fit, MTU problem? */ |
| 957 | dev_kfree_skb_any(skb); |
| 958 | skb = NULL; |
Bjørn Mork | bed6f76 | 2013-11-01 11:16:42 +0100 | [diff] [blame] | 959 | dev->net->stats.tx_dropped++; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 960 | } else { |
| 961 | /* no room for skb - store for later */ |
| 962 | if (ctx->tx_rem_skb != NULL) { |
| 963 | dev_kfree_skb_any(ctx->tx_rem_skb); |
Bjørn Mork | bed6f76 | 2013-11-01 11:16:42 +0100 | [diff] [blame] | 964 | dev->net->stats.tx_dropped++; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 965 | } |
| 966 | ctx->tx_rem_skb = skb; |
Bjørn Mork | c78b7c5 | 2012-10-22 10:56:34 +0000 | [diff] [blame] | 967 | ctx->tx_rem_sign = sign; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 968 | skb = NULL; |
Alexey Orishko | 84e77a8 | 2011-02-07 09:45:10 +0000 | [diff] [blame] | 969 | ready2send = 1; |
Bjørn Mork | beeecd4 | 2014-05-16 21:48:25 +0200 | [diff] [blame^] | 970 | ctx->tx_reason_ntb_full++; /* count reason for transmitting */ |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 971 | } |
| 972 | break; |
| 973 | } |
| 974 | |
Bjørn Mork | c78b7c5 | 2012-10-22 10:56:34 +0000 | [diff] [blame] | 975 | /* calculate frame number withing this NDP */ |
| 976 | ndplen = le16_to_cpu(ndp16->wLength); |
| 977 | index = (ndplen - sizeof(struct usb_cdc_ncm_ndp16)) / sizeof(struct usb_cdc_ncm_dpe16) - 1; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 978 | |
Bjørn Mork | c78b7c5 | 2012-10-22 10:56:34 +0000 | [diff] [blame] | 979 | /* OK, add this skb */ |
| 980 | ndp16->dpe16[index].wDatagramLength = cpu_to_le16(skb->len); |
| 981 | ndp16->dpe16[index].wDatagramIndex = cpu_to_le16(skb_out->len); |
| 982 | ndp16->wLength = cpu_to_le16(ndplen + sizeof(struct usb_cdc_ncm_dpe16)); |
| 983 | memcpy(skb_put(skb_out, skb->len), skb->data, skb->len); |
Bjørn Mork | beeecd4 | 2014-05-16 21:48:25 +0200 | [diff] [blame^] | 984 | ctx->tx_curr_frame_payload += skb->len; /* count real tx payload data */ |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 985 | dev_kfree_skb_any(skb); |
| 986 | skb = NULL; |
Bjørn Mork | c78b7c5 | 2012-10-22 10:56:34 +0000 | [diff] [blame] | 987 | |
| 988 | /* send now if this NDP is full */ |
| 989 | if (index >= CDC_NCM_DPT_DATAGRAMS_MAX) { |
| 990 | ready2send = 1; |
Bjørn Mork | beeecd4 | 2014-05-16 21:48:25 +0200 | [diff] [blame^] | 991 | ctx->tx_reason_ndp_full++; /* count reason for transmitting */ |
Bjørn Mork | c78b7c5 | 2012-10-22 10:56:34 +0000 | [diff] [blame] | 992 | break; |
| 993 | } |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | /* free up any dangling skb */ |
| 997 | if (skb != NULL) { |
| 998 | dev_kfree_skb_any(skb); |
| 999 | skb = NULL; |
Bjørn Mork | bed6f76 | 2013-11-01 11:16:42 +0100 | [diff] [blame] | 1000 | dev->net->stats.tx_dropped++; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | ctx->tx_curr_frame_num = n; |
| 1004 | |
| 1005 | if (n == 0) { |
| 1006 | /* wait for more frames */ |
| 1007 | /* push variables */ |
| 1008 | ctx->tx_curr_skb = skb_out; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1009 | goto exit_no_skb; |
| 1010 | |
Bjørn Mork | 6c4e548 | 2014-05-16 21:48:22 +0200 | [diff] [blame] | 1011 | } else if ((n < ctx->tx_max_datagrams) && (ready2send == 0) && (ctx->timer_interval > 0)) { |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1012 | /* wait for more frames */ |
| 1013 | /* push variables */ |
| 1014 | ctx->tx_curr_skb = skb_out; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1015 | /* set the pending count */ |
| 1016 | if (n < CDC_NCM_RESTART_TIMER_DATAGRAM_CNT) |
Alexey Orishko | c84ff1d | 2012-03-14 11:26:10 +0000 | [diff] [blame] | 1017 | ctx->tx_timer_pending = CDC_NCM_TIMER_PENDING_CNT; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1018 | goto exit_no_skb; |
| 1019 | |
| 1020 | } else { |
Bjørn Mork | beeecd4 | 2014-05-16 21:48:25 +0200 | [diff] [blame^] | 1021 | if (n == ctx->tx_max_datagrams) |
| 1022 | ctx->tx_reason_max_datagram++; /* count reason for transmitting */ |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1023 | /* frame goes out */ |
| 1024 | /* variables will be reset at next call */ |
| 1025 | } |
| 1026 | |
Bjørn Mork | 43e4c6d | 2014-05-16 21:48:24 +0200 | [diff] [blame] | 1027 | /* If collected data size is less or equal ctx->min_tx_pkt |
Bjørn Mork | 2057222 | 2013-11-01 11:16:38 +0100 | [diff] [blame] | 1028 | * bytes, we send buffers as it is. If we get more data, it |
| 1029 | * would be more efficient for USB HS mobile device with DMA |
| 1030 | * engine to receive a full size NTB, than canceling DMA |
| 1031 | * transfer and receiving a short packet. |
Bjørn Mork | 4d619f6 | 2013-11-01 11:16:49 +0100 | [diff] [blame] | 1032 | * |
| 1033 | * This optimization support is pointless if we end up sending |
| 1034 | * a ZLP after full sized NTBs. |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1035 | */ |
Bjørn Mork | 4d619f6 | 2013-11-01 11:16:49 +0100 | [diff] [blame] | 1036 | if (!(dev->driver_info->flags & FLAG_SEND_ZLP) && |
Bjørn Mork | 43e4c6d | 2014-05-16 21:48:24 +0200 | [diff] [blame] | 1037 | skb_out->len > ctx->min_tx_pkt) |
Bjørn Mork | 2057222 | 2013-11-01 11:16:38 +0100 | [diff] [blame] | 1038 | memset(skb_put(skb_out, ctx->tx_max - skb_out->len), 0, |
| 1039 | ctx->tx_max - skb_out->len); |
Bjørn Mork | 9becd70 | 2014-05-02 23:27:00 +0200 | [diff] [blame] | 1040 | else if (skb_out->len < ctx->tx_max && (skb_out->len % dev->maxpacket) == 0) |
Bjørn Mork | c78b7c5 | 2012-10-22 10:56:34 +0000 | [diff] [blame] | 1041 | *skb_put(skb_out, 1) = 0; /* force short packet */ |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1042 | |
Bjørn Mork | c78b7c5 | 2012-10-22 10:56:34 +0000 | [diff] [blame] | 1043 | /* set final frame length */ |
| 1044 | nth16 = (struct usb_cdc_ncm_nth16 *)skb_out->data; |
| 1045 | nth16->wBlockLength = cpu_to_le16(skb_out->len); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1046 | |
| 1047 | /* return skb */ |
| 1048 | ctx->tx_curr_skb = NULL; |
Bjørn Mork | bed6f76 | 2013-11-01 11:16:42 +0100 | [diff] [blame] | 1049 | dev->net->stats.tx_packets += ctx->tx_curr_frame_num; |
Bjørn Mork | beeecd4 | 2014-05-16 21:48:25 +0200 | [diff] [blame^] | 1050 | |
| 1051 | /* keep private stats: framing overhead and number of NTBs */ |
| 1052 | ctx->tx_overhead += skb_out->len - ctx->tx_curr_frame_payload; |
| 1053 | ctx->tx_ntbs++; |
| 1054 | |
| 1055 | /* usbnet has already counted all the framing overhead. |
| 1056 | * Adjust the stats so that the tx_bytes counter show real |
| 1057 | * payload data instead. |
| 1058 | */ |
| 1059 | dev->net->stats.tx_bytes -= skb_out->len - ctx->tx_curr_frame_payload; |
| 1060 | |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1061 | return skb_out; |
| 1062 | |
| 1063 | exit_no_skb: |
Alexey Orishko | c84ff1d | 2012-03-14 11:26:10 +0000 | [diff] [blame] | 1064 | /* Start timer, if there is a remaining skb */ |
| 1065 | if (ctx->tx_curr_skb != NULL) |
| 1066 | cdc_ncm_tx_timeout_start(ctx); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1067 | return NULL; |
| 1068 | } |
Bjørn Mork | c91ce3b | 2012-10-22 10:56:35 +0000 | [diff] [blame] | 1069 | EXPORT_SYMBOL_GPL(cdc_ncm_fill_tx_frame); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1070 | |
| 1071 | static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx) |
| 1072 | { |
| 1073 | /* start timer, if not already started */ |
Alexey Orishko | c84ff1d | 2012-03-14 11:26:10 +0000 | [diff] [blame] | 1074 | if (!(hrtimer_active(&ctx->tx_timer) || atomic_read(&ctx->stop))) |
| 1075 | hrtimer_start(&ctx->tx_timer, |
Bjørn Mork | 6c4e548 | 2014-05-16 21:48:22 +0200 | [diff] [blame] | 1076 | ktime_set(0, ctx->timer_interval), |
Alexey Orishko | c84ff1d | 2012-03-14 11:26:10 +0000 | [diff] [blame] | 1077 | HRTIMER_MODE_REL); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
Alexey Orishko | c84ff1d | 2012-03-14 11:26:10 +0000 | [diff] [blame] | 1080 | static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *timer) |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1081 | { |
Alexey Orishko | c84ff1d | 2012-03-14 11:26:10 +0000 | [diff] [blame] | 1082 | struct cdc_ncm_ctx *ctx = |
| 1083 | container_of(timer, struct cdc_ncm_ctx, tx_timer); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1084 | |
Alexey Orishko | c84ff1d | 2012-03-14 11:26:10 +0000 | [diff] [blame] | 1085 | if (!atomic_read(&ctx->stop)) |
| 1086 | tasklet_schedule(&ctx->bh); |
| 1087 | return HRTIMER_NORESTART; |
| 1088 | } |
| 1089 | |
| 1090 | static void cdc_ncm_txpath_bh(unsigned long param) |
| 1091 | { |
Bjørn Mork | bed6f76 | 2013-11-01 11:16:42 +0100 | [diff] [blame] | 1092 | struct usbnet *dev = (struct usbnet *)param; |
| 1093 | struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0]; |
Alexey Orishko | c84ff1d | 2012-03-14 11:26:10 +0000 | [diff] [blame] | 1094 | |
| 1095 | spin_lock_bh(&ctx->mtx); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1096 | if (ctx->tx_timer_pending != 0) { |
| 1097 | ctx->tx_timer_pending--; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1098 | cdc_ncm_tx_timeout_start(ctx); |
Alexey Orishko | c84ff1d | 2012-03-14 11:26:10 +0000 | [diff] [blame] | 1099 | spin_unlock_bh(&ctx->mtx); |
Bjørn Mork | bed6f76 | 2013-11-01 11:16:42 +0100 | [diff] [blame] | 1100 | } else if (dev->net != NULL) { |
Bjørn Mork | beeecd4 | 2014-05-16 21:48:25 +0200 | [diff] [blame^] | 1101 | ctx->tx_reason_timeout++; /* count reason for transmitting */ |
Alexey Orishko | c84ff1d | 2012-03-14 11:26:10 +0000 | [diff] [blame] | 1102 | spin_unlock_bh(&ctx->mtx); |
Bjørn Mork | bed6f76 | 2013-11-01 11:16:42 +0100 | [diff] [blame] | 1103 | netif_tx_lock_bh(dev->net); |
| 1104 | usbnet_start_xmit(NULL, dev->net); |
| 1105 | netif_tx_unlock_bh(dev->net); |
Bjørn Mork | 7b1e0cb | 2012-10-25 21:44:09 +0000 | [diff] [blame] | 1106 | } else { |
| 1107 | spin_unlock_bh(&ctx->mtx); |
Alexey Orishko | f742aa8 | 2011-01-17 07:07:25 +0000 | [diff] [blame] | 1108 | } |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
Enrico Mioso | 2f69702c | 2013-11-04 09:50:47 +0100 | [diff] [blame] | 1111 | struct sk_buff * |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1112 | cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags) |
| 1113 | { |
| 1114 | struct sk_buff *skb_out; |
| 1115 | struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0]; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1116 | |
| 1117 | /* |
| 1118 | * The Ethernet API we are using does not support transmitting |
| 1119 | * multiple Ethernet frames in a single call. This driver will |
| 1120 | * accumulate multiple Ethernet frames and send out a larger |
| 1121 | * USB frame when the USB buffer is full or when a single jiffies |
| 1122 | * timeout happens. |
| 1123 | */ |
| 1124 | if (ctx == NULL) |
| 1125 | goto error; |
| 1126 | |
Alexey Orishko | c84ff1d | 2012-03-14 11:26:10 +0000 | [diff] [blame] | 1127 | spin_lock_bh(&ctx->mtx); |
Bjørn Mork | bed6f76 | 2013-11-01 11:16:42 +0100 | [diff] [blame] | 1128 | skb_out = cdc_ncm_fill_tx_frame(dev, skb, cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN)); |
Alexey Orishko | c84ff1d | 2012-03-14 11:26:10 +0000 | [diff] [blame] | 1129 | spin_unlock_bh(&ctx->mtx); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1130 | return skb_out; |
| 1131 | |
| 1132 | error: |
| 1133 | if (skb != NULL) |
| 1134 | dev_kfree_skb_any(skb); |
| 1135 | |
| 1136 | return NULL; |
| 1137 | } |
Enrico Mioso | 2f69702c | 2013-11-04 09:50:47 +0100 | [diff] [blame] | 1138 | EXPORT_SYMBOL_GPL(cdc_ncm_tx_fixup); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1139 | |
Bjørn Mork | ff06ab1 | 2012-10-22 10:56:33 +0000 | [diff] [blame] | 1140 | /* verify NTB header and return offset of first NDP, or negative error */ |
Bjørn Mork | c91ce3b | 2012-10-22 10:56:35 +0000 | [diff] [blame] | 1141 | int cdc_ncm_rx_verify_nth16(struct cdc_ncm_ctx *ctx, struct sk_buff *skb_in) |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1142 | { |
Bjørn Mork | ae223cd | 2013-11-01 11:16:54 +0100 | [diff] [blame] | 1143 | struct usbnet *dev = netdev_priv(skb_in->dev); |
Alexey Orishko | d5ddb4a | 2012-03-14 11:26:12 +0000 | [diff] [blame] | 1144 | struct usb_cdc_ncm_nth16 *nth16; |
Bjørn Mork | ff06ab1 | 2012-10-22 10:56:33 +0000 | [diff] [blame] | 1145 | int len; |
| 1146 | int ret = -EINVAL; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1147 | |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1148 | if (ctx == NULL) |
| 1149 | goto error; |
| 1150 | |
Alexey Orishko | d5ddb4a | 2012-03-14 11:26:12 +0000 | [diff] [blame] | 1151 | if (skb_in->len < (sizeof(struct usb_cdc_ncm_nth16) + |
| 1152 | sizeof(struct usb_cdc_ncm_ndp16))) { |
Bjørn Mork | ae223cd | 2013-11-01 11:16:54 +0100 | [diff] [blame] | 1153 | netif_dbg(dev, rx_err, dev->net, "frame too short\n"); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1154 | goto error; |
| 1155 | } |
| 1156 | |
Alexey Orishko | d5ddb4a | 2012-03-14 11:26:12 +0000 | [diff] [blame] | 1157 | nth16 = (struct usb_cdc_ncm_nth16 *)skb_in->data; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1158 | |
Bjørn Mork | 986e10d | 2013-11-01 11:16:56 +0100 | [diff] [blame] | 1159 | if (nth16->dwSignature != cpu_to_le32(USB_CDC_NCM_NTH16_SIGN)) { |
Bjørn Mork | 5448d75 | 2013-11-01 11:16:55 +0100 | [diff] [blame] | 1160 | netif_dbg(dev, rx_err, dev->net, |
| 1161 | "invalid NTH16 signature <%#010x>\n", |
| 1162 | le32_to_cpu(nth16->dwSignature)); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1163 | goto error; |
| 1164 | } |
| 1165 | |
Alexey Orishko | d5ddb4a | 2012-03-14 11:26:12 +0000 | [diff] [blame] | 1166 | len = le16_to_cpu(nth16->wBlockLength); |
| 1167 | if (len > ctx->rx_max) { |
Bjørn Mork | ae223cd | 2013-11-01 11:16:54 +0100 | [diff] [blame] | 1168 | netif_dbg(dev, rx_err, dev->net, |
| 1169 | "unsupported NTB block length %u/%u\n", len, |
| 1170 | ctx->rx_max); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1171 | goto error; |
| 1172 | } |
| 1173 | |
Alexey Orishko | d5ddb4a | 2012-03-14 11:26:12 +0000 | [diff] [blame] | 1174 | if ((ctx->rx_seq + 1) != le16_to_cpu(nth16->wSequence) && |
Bjørn Mork | ae223cd | 2013-11-01 11:16:54 +0100 | [diff] [blame] | 1175 | (ctx->rx_seq || le16_to_cpu(nth16->wSequence)) && |
| 1176 | !((ctx->rx_seq == 0xffff) && !le16_to_cpu(nth16->wSequence))) { |
| 1177 | netif_dbg(dev, rx_err, dev->net, |
| 1178 | "sequence number glitch prev=%d curr=%d\n", |
| 1179 | ctx->rx_seq, le16_to_cpu(nth16->wSequence)); |
Alexey Orishko | d5ddb4a | 2012-03-14 11:26:12 +0000 | [diff] [blame] | 1180 | } |
| 1181 | ctx->rx_seq = le16_to_cpu(nth16->wSequence); |
| 1182 | |
Bjørn Mork | ff06ab1 | 2012-10-22 10:56:33 +0000 | [diff] [blame] | 1183 | ret = le16_to_cpu(nth16->wNdpIndex); |
| 1184 | error: |
| 1185 | return ret; |
| 1186 | } |
Bjørn Mork | c91ce3b | 2012-10-22 10:56:35 +0000 | [diff] [blame] | 1187 | EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_nth16); |
Bjørn Mork | ff06ab1 | 2012-10-22 10:56:33 +0000 | [diff] [blame] | 1188 | |
| 1189 | /* verify NDP header and return number of datagrams, or negative error */ |
Bjørn Mork | c91ce3b | 2012-10-22 10:56:35 +0000 | [diff] [blame] | 1190 | int cdc_ncm_rx_verify_ndp16(struct sk_buff *skb_in, int ndpoffset) |
Bjørn Mork | ff06ab1 | 2012-10-22 10:56:33 +0000 | [diff] [blame] | 1191 | { |
Bjørn Mork | ae223cd | 2013-11-01 11:16:54 +0100 | [diff] [blame] | 1192 | struct usbnet *dev = netdev_priv(skb_in->dev); |
Bjørn Mork | ff06ab1 | 2012-10-22 10:56:33 +0000 | [diff] [blame] | 1193 | struct usb_cdc_ncm_ndp16 *ndp16; |
| 1194 | int ret = -EINVAL; |
| 1195 | |
Bjørn Mork | 75d67d3 | 2012-10-22 10:56:32 +0000 | [diff] [blame] | 1196 | if ((ndpoffset + sizeof(struct usb_cdc_ncm_ndp16)) > skb_in->len) { |
Bjørn Mork | ae223cd | 2013-11-01 11:16:54 +0100 | [diff] [blame] | 1197 | netif_dbg(dev, rx_err, dev->net, "invalid NDP offset <%u>\n", |
| 1198 | ndpoffset); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1199 | goto error; |
| 1200 | } |
Bjørn Mork | 75d67d3 | 2012-10-22 10:56:32 +0000 | [diff] [blame] | 1201 | ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1202 | |
Alexey Orishko | d5ddb4a | 2012-03-14 11:26:12 +0000 | [diff] [blame] | 1203 | if (le16_to_cpu(ndp16->wLength) < USB_CDC_NCM_NDP16_LENGTH_MIN) { |
Bjørn Mork | ae223cd | 2013-11-01 11:16:54 +0100 | [diff] [blame] | 1204 | netif_dbg(dev, rx_err, dev->net, "invalid DPT16 length <%u>\n", |
| 1205 | le16_to_cpu(ndp16->wLength)); |
Bjørn Mork | ff06ab1 | 2012-10-22 10:56:33 +0000 | [diff] [blame] | 1206 | goto error; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
Bjørn Mork | ff06ab1 | 2012-10-22 10:56:33 +0000 | [diff] [blame] | 1209 | ret = ((le16_to_cpu(ndp16->wLength) - |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1210 | sizeof(struct usb_cdc_ncm_ndp16)) / |
| 1211 | sizeof(struct usb_cdc_ncm_dpe16)); |
Bjørn Mork | ff06ab1 | 2012-10-22 10:56:33 +0000 | [diff] [blame] | 1212 | ret--; /* we process NDP entries except for the last one */ |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1213 | |
Bjørn Mork | ae223cd | 2013-11-01 11:16:54 +0100 | [diff] [blame] | 1214 | if ((sizeof(struct usb_cdc_ncm_ndp16) + |
| 1215 | ret * (sizeof(struct usb_cdc_ncm_dpe16))) > skb_in->len) { |
| 1216 | netif_dbg(dev, rx_err, dev->net, "Invalid nframes = %d\n", ret); |
Bjørn Mork | ff06ab1 | 2012-10-22 10:56:33 +0000 | [diff] [blame] | 1217 | ret = -EINVAL; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
Bjørn Mork | ff06ab1 | 2012-10-22 10:56:33 +0000 | [diff] [blame] | 1220 | error: |
| 1221 | return ret; |
| 1222 | } |
Bjørn Mork | c91ce3b | 2012-10-22 10:56:35 +0000 | [diff] [blame] | 1223 | EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_ndp16); |
Bjørn Mork | ff06ab1 | 2012-10-22 10:56:33 +0000 | [diff] [blame] | 1224 | |
Enrico Mioso | 2f69702c | 2013-11-04 09:50:47 +0100 | [diff] [blame] | 1225 | int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in) |
Bjørn Mork | ff06ab1 | 2012-10-22 10:56:33 +0000 | [diff] [blame] | 1226 | { |
| 1227 | struct sk_buff *skb; |
| 1228 | struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0]; |
| 1229 | int len; |
| 1230 | int nframes; |
| 1231 | int x; |
| 1232 | int offset; |
| 1233 | struct usb_cdc_ncm_ndp16 *ndp16; |
| 1234 | struct usb_cdc_ncm_dpe16 *dpe16; |
| 1235 | int ndpoffset; |
| 1236 | int loopcount = 50; /* arbitrary max preventing infinite loop */ |
Bjørn Mork | beeecd4 | 2014-05-16 21:48:25 +0200 | [diff] [blame^] | 1237 | u32 payload = 0; |
Bjørn Mork | ff06ab1 | 2012-10-22 10:56:33 +0000 | [diff] [blame] | 1238 | |
| 1239 | ndpoffset = cdc_ncm_rx_verify_nth16(ctx, skb_in); |
| 1240 | if (ndpoffset < 0) |
| 1241 | goto error; |
| 1242 | |
| 1243 | next_ndp: |
| 1244 | nframes = cdc_ncm_rx_verify_ndp16(skb_in, ndpoffset); |
| 1245 | if (nframes < 0) |
| 1246 | goto error; |
| 1247 | |
| 1248 | ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset); |
| 1249 | |
Bjørn Mork | 986e10d | 2013-11-01 11:16:56 +0100 | [diff] [blame] | 1250 | if (ndp16->dwSignature != cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN)) { |
Bjørn Mork | 5448d75 | 2013-11-01 11:16:55 +0100 | [diff] [blame] | 1251 | netif_dbg(dev, rx_err, dev->net, |
| 1252 | "invalid DPT16 signature <%#010x>\n", |
| 1253 | le32_to_cpu(ndp16->dwSignature)); |
Bjørn Mork | ff06ab1 | 2012-10-22 10:56:33 +0000 | [diff] [blame] | 1254 | goto err_ndp; |
| 1255 | } |
| 1256 | dpe16 = ndp16->dpe16; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1257 | |
Alexey Orishko | d5ddb4a | 2012-03-14 11:26:12 +0000 | [diff] [blame] | 1258 | for (x = 0; x < nframes; x++, dpe16++) { |
| 1259 | offset = le16_to_cpu(dpe16->wDatagramIndex); |
| 1260 | len = le16_to_cpu(dpe16->wDatagramLength); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1261 | |
| 1262 | /* |
| 1263 | * CDC NCM ch. 3.7 |
| 1264 | * All entries after first NULL entry are to be ignored |
| 1265 | */ |
Alexey Orishko | d5ddb4a | 2012-03-14 11:26:12 +0000 | [diff] [blame] | 1266 | if ((offset == 0) || (len == 0)) { |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1267 | if (!x) |
Bjørn Mork | 75d67d3 | 2012-10-22 10:56:32 +0000 | [diff] [blame] | 1268 | goto err_ndp; /* empty NTB */ |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1269 | break; |
| 1270 | } |
| 1271 | |
| 1272 | /* sanity checking */ |
Alexey Orishko | d5ddb4a | 2012-03-14 11:26:12 +0000 | [diff] [blame] | 1273 | if (((offset + len) > skb_in->len) || |
| 1274 | (len > ctx->rx_max) || (len < ETH_HLEN)) { |
Bjørn Mork | ae223cd | 2013-11-01 11:16:54 +0100 | [diff] [blame] | 1275 | netif_dbg(dev, rx_err, dev->net, |
| 1276 | "invalid frame detected (ignored) offset[%u]=%u, length=%u, skb=%p\n", |
| 1277 | x, offset, len, skb_in); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1278 | if (!x) |
Bjørn Mork | 75d67d3 | 2012-10-22 10:56:32 +0000 | [diff] [blame] | 1279 | goto err_ndp; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1280 | break; |
| 1281 | |
| 1282 | } else { |
| 1283 | skb = skb_clone(skb_in, GFP_ATOMIC); |
Jesper Juhl | 9e56790 | 2011-01-13 11:40:11 +0000 | [diff] [blame] | 1284 | if (!skb) |
| 1285 | goto error; |
Alexey Orishko | d5ddb4a | 2012-03-14 11:26:12 +0000 | [diff] [blame] | 1286 | skb->len = len; |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1287 | skb->data = ((u8 *)skb_in->data) + offset; |
Alexey Orishko | d5ddb4a | 2012-03-14 11:26:12 +0000 | [diff] [blame] | 1288 | skb_set_tail_pointer(skb, len); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1289 | usbnet_skb_return(dev, skb); |
Bjørn Mork | beeecd4 | 2014-05-16 21:48:25 +0200 | [diff] [blame^] | 1290 | payload += len; /* count payload bytes in this NTB */ |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1291 | } |
| 1292 | } |
Bjørn Mork | 75d67d3 | 2012-10-22 10:56:32 +0000 | [diff] [blame] | 1293 | err_ndp: |
| 1294 | /* are there more NDPs to process? */ |
| 1295 | ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex); |
| 1296 | if (ndpoffset && loopcount--) |
| 1297 | goto next_ndp; |
| 1298 | |
Bjørn Mork | beeecd4 | 2014-05-16 21:48:25 +0200 | [diff] [blame^] | 1299 | /* update stats */ |
| 1300 | ctx->rx_overhead += skb_in->len - payload; |
| 1301 | ctx->rx_ntbs++; |
| 1302 | |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1303 | return 1; |
| 1304 | error: |
| 1305 | return 0; |
| 1306 | } |
Enrico Mioso | 2f69702c | 2013-11-04 09:50:47 +0100 | [diff] [blame] | 1307 | EXPORT_SYMBOL_GPL(cdc_ncm_rx_fixup); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1308 | |
| 1309 | static void |
Bjørn Mork | bed6f76 | 2013-11-01 11:16:42 +0100 | [diff] [blame] | 1310 | cdc_ncm_speed_change(struct usbnet *dev, |
Alexey Orishko | 84e77a8 | 2011-02-07 09:45:10 +0000 | [diff] [blame] | 1311 | struct usb_cdc_speed_change *data) |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1312 | { |
Alexey Orishko | 84e77a8 | 2011-02-07 09:45:10 +0000 | [diff] [blame] | 1313 | uint32_t rx_speed = le32_to_cpu(data->DLBitRRate); |
| 1314 | uint32_t tx_speed = le32_to_cpu(data->ULBitRate); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1315 | |
| 1316 | /* |
| 1317 | * Currently the USB-NET API does not support reporting the actual |
| 1318 | * device speed. Do print it instead. |
| 1319 | */ |
Bjørn Mork | f3028c5 | 2013-11-01 11:16:44 +0100 | [diff] [blame] | 1320 | if ((tx_speed > 1000000) && (rx_speed > 1000000)) { |
Bjørn Mork | ae223cd | 2013-11-01 11:16:54 +0100 | [diff] [blame] | 1321 | netif_info(dev, link, dev->net, |
| 1322 | "%u mbit/s downlink %u mbit/s uplink\n", |
Bjørn Mork | f3028c5 | 2013-11-01 11:16:44 +0100 | [diff] [blame] | 1323 | (unsigned int)(rx_speed / 1000000U), |
| 1324 | (unsigned int)(tx_speed / 1000000U)); |
| 1325 | } else { |
Bjørn Mork | ae223cd | 2013-11-01 11:16:54 +0100 | [diff] [blame] | 1326 | netif_info(dev, link, dev->net, |
| 1327 | "%u kbit/s downlink %u kbit/s uplink\n", |
Bjørn Mork | f3028c5 | 2013-11-01 11:16:44 +0100 | [diff] [blame] | 1328 | (unsigned int)(rx_speed / 1000U), |
| 1329 | (unsigned int)(tx_speed / 1000U)); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1330 | } |
| 1331 | } |
| 1332 | |
| 1333 | static void cdc_ncm_status(struct usbnet *dev, struct urb *urb) |
| 1334 | { |
| 1335 | struct cdc_ncm_ctx *ctx; |
| 1336 | struct usb_cdc_notification *event; |
| 1337 | |
| 1338 | ctx = (struct cdc_ncm_ctx *)dev->data[0]; |
| 1339 | |
| 1340 | if (urb->actual_length < sizeof(*event)) |
| 1341 | return; |
| 1342 | |
| 1343 | /* test for split data in 8-byte chunks */ |
| 1344 | if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) { |
Bjørn Mork | bed6f76 | 2013-11-01 11:16:42 +0100 | [diff] [blame] | 1345 | cdc_ncm_speed_change(dev, |
Alexey Orishko | 84e77a8 | 2011-02-07 09:45:10 +0000 | [diff] [blame] | 1346 | (struct usb_cdc_speed_change *)urb->transfer_buffer); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1347 | return; |
| 1348 | } |
| 1349 | |
| 1350 | event = urb->transfer_buffer; |
| 1351 | |
| 1352 | switch (event->bNotificationType) { |
| 1353 | case USB_CDC_NOTIFY_NETWORK_CONNECTION: |
| 1354 | /* |
| 1355 | * According to the CDC NCM specification ch.7.1 |
| 1356 | * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be |
| 1357 | * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE. |
| 1358 | */ |
Bjørn Mork | 1a7c6cc | 2012-10-25 21:44:08 +0000 | [diff] [blame] | 1359 | ctx->connected = le16_to_cpu(event->wValue); |
Bjørn Mork | ae223cd | 2013-11-01 11:16:54 +0100 | [diff] [blame] | 1360 | netif_info(dev, link, dev->net, |
| 1361 | "network connection: %sconnected\n", |
| 1362 | ctx->connected ? "" : "dis"); |
Ming Lei | 8a34b0a | 2013-04-11 04:40:33 +0000 | [diff] [blame] | 1363 | usbnet_link_change(dev, ctx->connected, 0); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1364 | break; |
| 1365 | |
| 1366 | case USB_CDC_NOTIFY_SPEED_CHANGE: |
Alexey Orishko | 84e77a8 | 2011-02-07 09:45:10 +0000 | [diff] [blame] | 1367 | if (urb->actual_length < (sizeof(*event) + |
| 1368 | sizeof(struct usb_cdc_speed_change))) |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1369 | set_bit(EVENT_STS_SPLIT, &dev->flags); |
| 1370 | else |
Bjørn Mork | bed6f76 | 2013-11-01 11:16:42 +0100 | [diff] [blame] | 1371 | cdc_ncm_speed_change(dev, |
| 1372 | (struct usb_cdc_speed_change *)&event[1]); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1373 | break; |
| 1374 | |
| 1375 | default: |
Bjørn Mork | 67a3660 | 2013-04-08 08:26:23 +0000 | [diff] [blame] | 1376 | dev_dbg(&dev->udev->dev, |
| 1377 | "NCM: unexpected notification 0x%02x!\n", |
| 1378 | event->bNotificationType); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1379 | break; |
| 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | static int cdc_ncm_check_connect(struct usbnet *dev) |
| 1384 | { |
| 1385 | struct cdc_ncm_ctx *ctx; |
| 1386 | |
| 1387 | ctx = (struct cdc_ncm_ctx *)dev->data[0]; |
| 1388 | if (ctx == NULL) |
| 1389 | return 1; /* disconnected */ |
| 1390 | |
| 1391 | return !ctx->connected; |
| 1392 | } |
| 1393 | |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1394 | static const struct driver_info cdc_ncm_info = { |
| 1395 | .description = "CDC NCM", |
Arnd Bergmann | c261344 | 2011-04-01 20:12:02 -0700 | [diff] [blame] | 1396 | .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET, |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1397 | .bind = cdc_ncm_bind, |
| 1398 | .unbind = cdc_ncm_unbind, |
| 1399 | .check_connect = cdc_ncm_check_connect, |
Oliver Neukum | a5e4070 | 2012-12-18 04:46:12 +0000 | [diff] [blame] | 1400 | .manage_power = usbnet_manage_power, |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1401 | .status = cdc_ncm_status, |
| 1402 | .rx_fixup = cdc_ncm_rx_fixup, |
| 1403 | .tx_fixup = cdc_ncm_tx_fixup, |
| 1404 | }; |
| 1405 | |
Dan Williams | f94898e | 2012-07-24 08:43:22 +0000 | [diff] [blame] | 1406 | /* Same as cdc_ncm_info, but with FLAG_WWAN */ |
| 1407 | static const struct driver_info wwan_info = { |
| 1408 | .description = "Mobile Broadband Network Device", |
| 1409 | .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET |
| 1410 | | FLAG_WWAN, |
| 1411 | .bind = cdc_ncm_bind, |
| 1412 | .unbind = cdc_ncm_unbind, |
| 1413 | .check_connect = cdc_ncm_check_connect, |
Oliver Neukum | a5e4070 | 2012-12-18 04:46:12 +0000 | [diff] [blame] | 1414 | .manage_power = usbnet_manage_power, |
Dan Williams | f94898e | 2012-07-24 08:43:22 +0000 | [diff] [blame] | 1415 | .status = cdc_ncm_status, |
| 1416 | .rx_fixup = cdc_ncm_rx_fixup, |
| 1417 | .tx_fixup = cdc_ncm_tx_fixup, |
| 1418 | }; |
| 1419 | |
Wei Shuai | 2f62d5a | 2013-01-21 06:00:32 +0000 | [diff] [blame] | 1420 | /* Same as wwan_info, but with FLAG_NOARP */ |
| 1421 | static const struct driver_info wwan_noarp_info = { |
| 1422 | .description = "Mobile Broadband Network Device (NO ARP)", |
| 1423 | .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET |
| 1424 | | FLAG_WWAN | FLAG_NOARP, |
| 1425 | .bind = cdc_ncm_bind, |
| 1426 | .unbind = cdc_ncm_unbind, |
| 1427 | .check_connect = cdc_ncm_check_connect, |
| 1428 | .manage_power = usbnet_manage_power, |
| 1429 | .status = cdc_ncm_status, |
| 1430 | .rx_fixup = cdc_ncm_rx_fixup, |
| 1431 | .tx_fixup = cdc_ncm_tx_fixup, |
| 1432 | }; |
| 1433 | |
Dan Williams | f94898e | 2012-07-24 08:43:22 +0000 | [diff] [blame] | 1434 | static const struct usb_device_id cdc_devs[] = { |
| 1435 | /* Ericsson MBM devices like F5521gw */ |
| 1436 | { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO |
| 1437 | | USB_DEVICE_ID_MATCH_VENDOR, |
| 1438 | .idVendor = 0x0bdb, |
| 1439 | .bInterfaceClass = USB_CLASS_COMM, |
| 1440 | .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM, |
| 1441 | .bInterfaceProtocol = USB_CDC_PROTO_NONE, |
| 1442 | .driver_info = (unsigned long) &wwan_info, |
| 1443 | }, |
| 1444 | |
Peter Meiser | f3a1ef9 | 2012-08-02 02:30:20 +0000 | [diff] [blame] | 1445 | /* Dell branded MBM devices like DW5550 */ |
| 1446 | { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO |
| 1447 | | USB_DEVICE_ID_MATCH_VENDOR, |
| 1448 | .idVendor = 0x413c, |
| 1449 | .bInterfaceClass = USB_CLASS_COMM, |
| 1450 | .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM, |
| 1451 | .bInterfaceProtocol = USB_CDC_PROTO_NONE, |
| 1452 | .driver_info = (unsigned long) &wwan_info, |
| 1453 | }, |
| 1454 | |
| 1455 | /* Toshiba branded MBM devices */ |
| 1456 | { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO |
| 1457 | | USB_DEVICE_ID_MATCH_VENDOR, |
| 1458 | .idVendor = 0x0930, |
| 1459 | .bInterfaceClass = USB_CLASS_COMM, |
| 1460 | .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM, |
| 1461 | .bInterfaceProtocol = USB_CDC_PROTO_NONE, |
| 1462 | .driver_info = (unsigned long) &wwan_info, |
| 1463 | }, |
| 1464 | |
Bjørn Mork | 1f84eab | 2013-02-27 03:08:58 +0000 | [diff] [blame] | 1465 | /* tag Huawei devices as wwan */ |
| 1466 | { USB_VENDOR_AND_INTERFACE_INFO(0x12d1, |
| 1467 | USB_CLASS_COMM, |
| 1468 | USB_CDC_SUBCLASS_NCM, |
| 1469 | USB_CDC_PROTO_NONE), |
| 1470 | .driver_info = (unsigned long)&wwan_info, |
| 1471 | }, |
| 1472 | |
Wei Shuai | 2f62d5a | 2013-01-21 06:00:32 +0000 | [diff] [blame] | 1473 | /* Infineon(now Intel) HSPA Modem platform */ |
| 1474 | { USB_DEVICE_AND_INTERFACE_INFO(0x1519, 0x0443, |
| 1475 | USB_CLASS_COMM, |
| 1476 | USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE), |
| 1477 | .driver_info = (unsigned long)&wwan_noarp_info, |
| 1478 | }, |
| 1479 | |
Dan Williams | f94898e | 2012-07-24 08:43:22 +0000 | [diff] [blame] | 1480 | /* Generic CDC-NCM devices */ |
| 1481 | { USB_INTERFACE_INFO(USB_CLASS_COMM, |
| 1482 | USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE), |
| 1483 | .driver_info = (unsigned long)&cdc_ncm_info, |
| 1484 | }, |
| 1485 | { |
| 1486 | }, |
| 1487 | }; |
| 1488 | MODULE_DEVICE_TABLE(usb, cdc_devs); |
| 1489 | |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1490 | static struct usb_driver cdc_ncm_driver = { |
| 1491 | .name = "cdc_ncm", |
| 1492 | .id_table = cdc_devs, |
Bjørn Mork | 085e50e | 2013-11-01 11:16:50 +0100 | [diff] [blame] | 1493 | .probe = usbnet_probe, |
| 1494 | .disconnect = usbnet_disconnect, |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1495 | .suspend = usbnet_suspend, |
| 1496 | .resume = usbnet_resume, |
Stefan Metzmacher | 85e3c65 | 2011-06-01 02:01:41 +0000 | [diff] [blame] | 1497 | .reset_resume = usbnet_resume, |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1498 | .supports_autosuspend = 1, |
Sarah Sharp | e1f12eb | 2012-04-23 10:08:51 -0700 | [diff] [blame] | 1499 | .disable_hub_initiated_lpm = 1, |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1500 | }; |
| 1501 | |
Greg Kroah-Hartman | d632eb1 | 2011-11-18 09:44:20 -0800 | [diff] [blame] | 1502 | module_usb_driver(cdc_ncm_driver); |
Alexey Orishko | 900d495 | 2010-11-29 23:23:28 +0000 | [diff] [blame] | 1503 | |
| 1504 | MODULE_AUTHOR("Hans Petter Selasky"); |
| 1505 | MODULE_DESCRIPTION("USB CDC NCM host driver"); |
| 1506 | MODULE_LICENSE("Dual BSD/GPL"); |