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