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