blob: 2d0caf1eea2503f4ed17a58a15b83533f7cce3fa [file] [log] [blame]
Alexey Orishko900d4952010-11-29 23:23:28 +00001/*
2 * cdc_ncm.c
3 *
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +00004 * Copyright (C) ST-Ericsson 2010-2012
Alexey Orishko900d4952010-11-29 23:23:28 +00005 * Contact: Alexey Orishko <alexey.orishko@stericsson.com>
6 * Original author: Hans Petter Selasky <hans.petter.selasky@stericsson.com>
7 *
8 * USB Host Driver for Network Control Model (NCM)
9 * http://www.usb.org/developers/devclass_docs/NCM10.zip
10 *
11 * The NCM encoding, decoding and initialization logic
12 * derives from FreeBSD 8.x. if_cdce.c and if_cdcereg.h
13 *
14 * This software is available to you under a choice of one of two
15 * licenses. You may choose this file to be licensed under the terms
16 * of the GNU General Public License (GPL) Version 2 or the 2-clause
17 * BSD license listed below:
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 */
40
41#include <linux/module.h>
Alexey Orishko900d4952010-11-29 23:23:28 +000042#include <linux/netdevice.h>
43#include <linux/ctype.h>
44#include <linux/ethtool.h>
45#include <linux/workqueue.h>
46#include <linux/mii.h>
47#include <linux/crc32.h>
48#include <linux/usb.h>
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +000049#include <linux/hrtimer.h>
Alexey Orishko900d4952010-11-29 23:23:28 +000050#include <linux/atomic.h>
51#include <linux/usb/usbnet.h>
52#include <linux/usb/cdc.h>
Bjørn Morkc91ce3b2012-10-22 10:56:35 +000053#include <linux/usb/cdc_ncm.h>
Alexey Orishko900d4952010-11-29 23:23:28 +000054
Bjørn Mork1e8bbe62013-03-14 01:05:13 +000055#if IS_ENABLED(CONFIG_USB_NET_CDC_MBIM)
56static bool prefer_mbim = true;
57#else
58static bool prefer_mbim;
59#endif
60module_param(prefer_mbim, bool, S_IRUGO | S_IWUSR);
61MODULE_PARM_DESC(prefer_mbim, "Prefer MBIM setting on dual NCM/MBIM functions");
62
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +000063static void cdc_ncm_txpath_bh(unsigned long param);
64static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx);
65static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *hr_timer);
Alexey Orishko900d4952010-11-29 23:23:28 +000066static struct usb_driver cdc_ncm_driver;
Alexey Orishko900d4952010-11-29 23:23:28 +000067
Bjørn Morkbeeecd42014-05-16 21:48:25 +020068struct cdc_ncm_stats {
69 char stat_string[ETH_GSTRING_LEN];
70 int sizeof_stat;
71 int stat_offset;
72};
73
74#define CDC_NCM_STAT(str, m) { \
75 .stat_string = str, \
76 .sizeof_stat = sizeof(((struct cdc_ncm_ctx *)0)->m), \
77 .stat_offset = offsetof(struct cdc_ncm_ctx, m) }
78#define CDC_NCM_SIMPLE_STAT(m) CDC_NCM_STAT(__stringify(m), m)
79
80static const struct cdc_ncm_stats cdc_ncm_gstrings_stats[] = {
81 CDC_NCM_SIMPLE_STAT(tx_reason_ntb_full),
82 CDC_NCM_SIMPLE_STAT(tx_reason_ndp_full),
83 CDC_NCM_SIMPLE_STAT(tx_reason_timeout),
84 CDC_NCM_SIMPLE_STAT(tx_reason_max_datagram),
85 CDC_NCM_SIMPLE_STAT(tx_overhead),
86 CDC_NCM_SIMPLE_STAT(tx_ntbs),
87 CDC_NCM_SIMPLE_STAT(rx_overhead),
88 CDC_NCM_SIMPLE_STAT(rx_ntbs),
89};
90
91static int cdc_ncm_get_sset_count(struct net_device __always_unused *netdev, int sset)
92{
93 switch (sset) {
94 case ETH_SS_STATS:
95 return ARRAY_SIZE(cdc_ncm_gstrings_stats);
96 default:
97 return -EOPNOTSUPP;
98 }
99}
100
101static void cdc_ncm_get_ethtool_stats(struct net_device *netdev,
102 struct ethtool_stats __always_unused *stats,
103 u64 *data)
104{
105 struct usbnet *dev = netdev_priv(netdev);
106 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
107 int i;
108 char *p = NULL;
109
110 for (i = 0; i < ARRAY_SIZE(cdc_ncm_gstrings_stats); i++) {
111 p = (char *)ctx + cdc_ncm_gstrings_stats[i].stat_offset;
112 data[i] = (cdc_ncm_gstrings_stats[i].sizeof_stat == sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
113 }
114}
115
116static void cdc_ncm_get_strings(struct net_device __always_unused *netdev, u32 stringset, u8 *data)
117{
118 u8 *p = data;
119 int i;
120
121 switch (stringset) {
122 case ETH_SS_STATS:
123 for (i = 0; i < ARRAY_SIZE(cdc_ncm_gstrings_stats); i++) {
124 memcpy(p, cdc_ncm_gstrings_stats[i].stat_string, ETH_GSTRING_LEN);
125 p += ETH_GSTRING_LEN;
126 }
127 }
128}
129
Bjørn Mork6c4e5482014-05-16 21:48:22 +0200130static int cdc_ncm_get_coalesce(struct net_device *netdev,
131 struct ethtool_coalesce *ec)
132{
133 struct usbnet *dev = netdev_priv(netdev);
134 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
135
136 /* assuming maximum sized dgrams and ignoring NDPs */
137 ec->rx_max_coalesced_frames = ctx->rx_max / ctx->max_datagram_size;
138 ec->tx_max_coalesced_frames = ctx->tx_max / ctx->max_datagram_size;
139
140 /* the timer will fire CDC_NCM_TIMER_PENDING_CNT times in a row */
141 ec->tx_coalesce_usecs = (ctx->timer_interval * CDC_NCM_TIMER_PENDING_CNT) / NSEC_PER_USEC;
142 return 0;
143}
144
145static void cdc_ncm_update_rxtx_max(struct usbnet *dev, u32 new_rx, u32 new_tx);
146
147static int cdc_ncm_set_coalesce(struct net_device *netdev,
148 struct ethtool_coalesce *ec)
149{
150 struct usbnet *dev = netdev_priv(netdev);
151 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
152 u32 new_rx_max = ctx->rx_max;
153 u32 new_tx_max = ctx->tx_max;
154
155 /* assuming maximum sized dgrams and a single NDP */
156 if (ec->rx_max_coalesced_frames)
157 new_rx_max = ec->rx_max_coalesced_frames * ctx->max_datagram_size;
158 if (ec->tx_max_coalesced_frames)
159 new_tx_max = ec->tx_max_coalesced_frames * ctx->max_datagram_size;
160
161 if (ec->tx_coalesce_usecs &&
162 (ec->tx_coalesce_usecs < CDC_NCM_TIMER_INTERVAL_MIN * CDC_NCM_TIMER_PENDING_CNT ||
163 ec->tx_coalesce_usecs > CDC_NCM_TIMER_INTERVAL_MAX * CDC_NCM_TIMER_PENDING_CNT))
164 return -EINVAL;
165
166 spin_lock_bh(&ctx->mtx);
167 ctx->timer_interval = ec->tx_coalesce_usecs * NSEC_PER_USEC / CDC_NCM_TIMER_PENDING_CNT;
168 if (!ctx->timer_interval)
169 ctx->tx_timer_pending = 0;
170 spin_unlock_bh(&ctx->mtx);
171
172 /* inform device of new values */
173 if (new_rx_max != ctx->rx_max || new_tx_max != ctx->tx_max)
174 cdc_ncm_update_rxtx_max(dev, new_rx_max, new_tx_max);
175 return 0;
176}
177
178static const struct ethtool_ops cdc_ncm_ethtool_ops = {
179 .get_settings = usbnet_get_settings,
180 .set_settings = usbnet_set_settings,
181 .get_link = usbnet_get_link,
182 .nway_reset = usbnet_nway_reset,
183 .get_drvinfo = usbnet_get_drvinfo,
184 .get_msglevel = usbnet_get_msglevel,
185 .set_msglevel = usbnet_set_msglevel,
186 .get_ts_info = ethtool_op_get_ts_info,
Bjørn Morkbeeecd42014-05-16 21:48:25 +0200187 .get_sset_count = cdc_ncm_get_sset_count,
188 .get_strings = cdc_ncm_get_strings,
189 .get_ethtool_stats = cdc_ncm_get_ethtool_stats,
Bjørn Mork6c4e5482014-05-16 21:48:22 +0200190 .get_coalesce = cdc_ncm_get_coalesce,
191 .set_coalesce = cdc_ncm_set_coalesce,
192};
193
Bjørn Mork5aa73d52014-05-16 21:48:18 +0200194/* handle rx_max and tx_max changes */
195static void cdc_ncm_update_rxtx_max(struct usbnet *dev, u32 new_rx, u32 new_tx)
196{
197 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
198 u8 iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber;
199 u32 val, max, min;
200
201 /* clamp new_rx to sane values */
202 min = USB_CDC_NCM_NTB_MIN_IN_SIZE;
203 max = min_t(u32, CDC_NCM_NTB_MAX_SIZE_RX, le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize));
204
205 /* dwNtbInMaxSize spec violation? Use MIN size for both limits */
206 if (max < min) {
207 dev_warn(&dev->intf->dev, "dwNtbInMaxSize=%u is too small. Using %u\n",
208 le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize), min);
209 max = min;
210 }
211
212 val = clamp_t(u32, new_rx, min, max);
213 if (val != new_rx) {
214 dev_dbg(&dev->intf->dev, "rx_max must be in the [%u, %u] range. Using %u\n",
215 min, max, val);
216 }
217
Bjørn Mork68864ab2014-05-16 21:48:21 +0200218 /* usbnet use these values for sizing rx queues */
219 dev->rx_urb_size = val;
220
Bjørn Mork5aa73d52014-05-16 21:48:18 +0200221 /* inform device about NTB input size changes */
222 if (val != ctx->rx_max) {
223 __le32 dwNtbInMaxSize = cpu_to_le32(val);
224
225 dev_info(&dev->intf->dev, "setting rx_max = %u\n", val);
Bjørn Mork68864ab2014-05-16 21:48:21 +0200226
227 /* need to unlink rx urbs before increasing buffer size */
228 if (netif_running(dev->net) && dev->rx_urb_size > ctx->rx_max)
229 usbnet_unlink_rx_urbs(dev);
230
231 /* tell device to use new size */
Bjørn Mork5aa73d52014-05-16 21:48:18 +0200232 if (usbnet_write_cmd(dev, USB_CDC_SET_NTB_INPUT_SIZE,
233 USB_TYPE_CLASS | USB_DIR_OUT
234 | USB_RECIP_INTERFACE,
235 0, iface_no, &dwNtbInMaxSize, 4) < 0)
236 dev_dbg(&dev->intf->dev, "Setting NTB Input Size failed\n");
237 else
238 ctx->rx_max = val;
239 }
240
241 /* clamp new_tx to sane values */
Bjørn Mork70559b82014-05-16 21:48:23 +0200242 min = ctx->max_datagram_size + ctx->max_ndp_size + sizeof(struct usb_cdc_ncm_nth16);
Bjørn Mork5aa73d52014-05-16 21:48:18 +0200243 max = min_t(u32, CDC_NCM_NTB_MAX_SIZE_TX, le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize));
244
245 /* some devices set dwNtbOutMaxSize too low for the above default */
246 min = min(min, max);
247
248 val = clamp_t(u32, new_tx, min, max);
249 if (val != new_tx) {
250 dev_dbg(&dev->intf->dev, "tx_max must be in the [%u, %u] range. Using %u\n",
251 min, max, val);
252 }
253 if (val != ctx->tx_max)
254 dev_info(&dev->intf->dev, "setting tx_max = %u\n", val);
Bjørn Mork08c74fc2014-05-16 21:48:20 +0200255
256 /* Adding a pad byte here if necessary simplifies the handling
257 * in cdc_ncm_fill_tx_frame, making tx_max always represent
258 * the real skb max size.
259 *
260 * We cannot use dev->maxpacket here because this is called from
261 * .bind which is called before usbnet sets up dev->maxpacket
262 */
Bjørn Mork68864ab2014-05-16 21:48:21 +0200263 if (val != le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize) &&
264 val % usb_maxpacket(dev->udev, dev->out, 1) == 0)
265 val++;
Bjørn Mork08c74fc2014-05-16 21:48:20 +0200266
Bjørn Mork68864ab2014-05-16 21:48:21 +0200267 /* we might need to flush any pending tx buffers if running */
268 if (netif_running(dev->net) && val > ctx->tx_max) {
269 netif_tx_lock_bh(dev->net);
270 usbnet_start_xmit(NULL, dev->net);
271 ctx->tx_max = val;
272 netif_tx_unlock_bh(dev->net);
273 } else {
274 ctx->tx_max = val;
275 }
276
Bjørn Mork08c74fc2014-05-16 21:48:20 +0200277 dev->hard_mtu = ctx->tx_max;
Bjørn Mork68864ab2014-05-16 21:48:21 +0200278
279 /* max qlen depend on hard_mtu and rx_urb_size */
280 usbnet_update_max_qlen(dev);
Bjørn Mork43e4c6d2014-05-16 21:48:24 +0200281
282 /* never pad more than 3 full USB packets per transfer */
283 ctx->min_tx_pkt = clamp_t(u16, ctx->tx_max - 3 * usb_maxpacket(dev->udev, dev->out, 1),
284 CDC_NCM_MIN_TX_PKT, ctx->tx_max);
Bjørn Mork5aa73d52014-05-16 21:48:18 +0200285}
286
Bjørn Morkf8afb732014-05-16 21:48:19 +0200287/* helpers for NCM and MBIM differences */
288static u8 cdc_ncm_flags(struct usbnet *dev)
Alexey Orishko900d4952010-11-29 23:23:28 +0000289{
Bjørn Morkbed6f762013-11-01 11:16:42 +0100290 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
Alexey Orishko900d4952010-11-29 23:23:28 +0000291
Bjørn Morkf8afb732014-05-16 21:48:19 +0200292 if (cdc_ncm_comm_intf_is_mbim(dev->intf->cur_altsetting) && ctx->mbim_desc)
293 return ctx->mbim_desc->bmNetworkCapabilities;
294 if (ctx->func_desc)
295 return ctx->func_desc->bmNetworkCapabilities;
296 return 0;
297}
298
299static int cdc_ncm_eth_hlen(struct usbnet *dev)
300{
301 if (cdc_ncm_comm_intf_is_mbim(dev->intf->cur_altsetting))
302 return 0;
303 return ETH_HLEN;
304}
305
306static u32 cdc_ncm_min_dgram_size(struct usbnet *dev)
307{
308 if (cdc_ncm_comm_intf_is_mbim(dev->intf->cur_altsetting))
309 return CDC_MBIM_MIN_DATAGRAM_SIZE;
310 return CDC_NCM_MIN_DATAGRAM_SIZE;
311}
312
313static u32 cdc_ncm_max_dgram_size(struct usbnet *dev)
314{
315 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
316
317 if (cdc_ncm_comm_intf_is_mbim(dev->intf->cur_altsetting) && ctx->mbim_desc)
318 return le16_to_cpu(ctx->mbim_desc->wMaxSegmentSize);
319 if (ctx->ether_desc)
320 return le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
321 return CDC_NCM_MAX_DATAGRAM_SIZE;
322}
323
324/* initial one-time device setup. MUST be called with the data interface
325 * in altsetting 0
326 */
327static int cdc_ncm_init(struct usbnet *dev)
328{
329 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
330 u8 iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber;
331 int err;
Alexey Orishko900d4952010-11-29 23:23:28 +0000332
Ming Lei90b8b032012-10-24 19:46:56 +0000333 err = usbnet_read_cmd(dev, USB_CDC_GET_NTB_PARAMETERS,
334 USB_TYPE_CLASS | USB_DIR_IN
335 |USB_RECIP_INTERFACE,
Bjørn Morkff0992e92014-03-17 16:25:18 +0100336 0, iface_no, &ctx->ncm_parm,
337 sizeof(ctx->ncm_parm));
Giuseppe Scrivano36c35412011-08-03 22:10:29 +0000338 if (err < 0) {
Bjørn Mork59ede312013-11-01 11:16:59 +0100339 dev_err(&dev->intf->dev, "failed GET_NTB_PARAMETERS\n");
340 return err; /* GET_NTB_PARAMETERS is required */
Alexey Orishko900d4952010-11-29 23:23:28 +0000341 }
342
Bjørn Morkf8afb732014-05-16 21:48:19 +0200343 /* set CRC Mode */
344 if (cdc_ncm_flags(dev) & USB_CDC_NCM_NCAP_CRC_MODE) {
345 dev_dbg(&dev->intf->dev, "Setting CRC mode off\n");
346 err = usbnet_write_cmd(dev, USB_CDC_SET_CRC_MODE,
347 USB_TYPE_CLASS | USB_DIR_OUT
348 | USB_RECIP_INTERFACE,
349 USB_CDC_NCM_CRC_NOT_APPENDED,
350 iface_no, NULL, 0);
351 if (err < 0)
352 dev_err(&dev->intf->dev, "SET_CRC_MODE failed\n");
353 }
354
355 /* set NTB format, if both formats are supported.
356 *
357 * "The host shall only send this command while the NCM Data
358 * Interface is in alternate setting 0."
359 */
360 if (le16_to_cpu(ctx->ncm_parm.bmNtbFormatsSupported) & USB_CDC_NCM_NTH32_SIGN) {
361 dev_dbg(&dev->intf->dev, "Setting NTB format to 16-bit\n");
362 err = usbnet_write_cmd(dev, USB_CDC_SET_NTB_FORMAT,
363 USB_TYPE_CLASS | USB_DIR_OUT
364 | USB_RECIP_INTERFACE,
365 USB_CDC_NCM_NTB16_FORMAT,
366 iface_no, NULL, 0);
367 if (err < 0)
368 dev_err(&dev->intf->dev, "SET_NTB_FORMAT failed\n");
369 }
370
371 /* set initial device values */
Bjørn Morkff0992e92014-03-17 16:25:18 +0100372 ctx->rx_max = le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize);
373 ctx->tx_max = le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize);
374 ctx->tx_remainder = le16_to_cpu(ctx->ncm_parm.wNdpOutPayloadRemainder);
375 ctx->tx_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutDivisor);
376 ctx->tx_ndp_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutAlignment);
Alexey Orishko84e77a82011-02-07 09:45:10 +0000377 /* devices prior to NCM Errata shall set this field to zero */
Bjørn Morkff0992e92014-03-17 16:25:18 +0100378 ctx->tx_max_datagrams = le16_to_cpu(ctx->ncm_parm.wNtbOutMaxDatagrams);
Bjørn Mork47175e52013-11-01 11:16:58 +0100379
Bjørn Morkae223cd2013-11-01 11:16:54 +0100380 dev_dbg(&dev->intf->dev,
381 "dwNtbInMaxSize=%u dwNtbOutMaxSize=%u wNdpOutPayloadRemainder=%u wNdpOutDivisor=%u wNdpOutAlignment=%u wNtbOutMaxDatagrams=%u flags=0x%x\n",
382 ctx->rx_max, ctx->tx_max, ctx->tx_remainder, ctx->tx_modulus,
Bjørn Morkf8afb732014-05-16 21:48:19 +0200383 ctx->tx_ndp_modulus, ctx->tx_max_datagrams, cdc_ncm_flags(dev));
Alexey Orishko900d4952010-11-29 23:23:28 +0000384
Alexey Orishko84e77a82011-02-07 09:45:10 +0000385 /* max count of tx datagrams */
386 if ((ctx->tx_max_datagrams == 0) ||
387 (ctx->tx_max_datagrams > CDC_NCM_DPT_DATAGRAMS_MAX))
388 ctx->tx_max_datagrams = CDC_NCM_DPT_DATAGRAMS_MAX;
Alexey Orishko900d4952010-11-29 23:23:28 +0000389
Bjørn Mork70559b82014-05-16 21:48:23 +0200390 /* set up maximum NDP size */
391 ctx->max_ndp_size = sizeof(struct usb_cdc_ncm_ndp16) + (ctx->tx_max_datagrams + 1) * sizeof(struct usb_cdc_ncm_dpe16);
392
Bjørn Mork6c4e5482014-05-16 21:48:22 +0200393 /* initial coalescing timer interval */
394 ctx->timer_interval = CDC_NCM_TIMER_INTERVAL_USEC * NSEC_PER_USEC;
395
Bjørn Morkf8afb732014-05-16 21:48:19 +0200396 return 0;
397}
398
399/* set a new max datagram size */
400static void cdc_ncm_set_dgram_size(struct usbnet *dev, int new_size)
401{
402 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
403 u8 iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber;
404 __le16 max_datagram_size;
405 u16 mbim_mtu;
406 int err;
407
408 /* set default based on descriptors */
409 ctx->max_datagram_size = clamp_t(u32, new_size,
410 cdc_ncm_min_dgram_size(dev),
411 CDC_NCM_MAX_DATAGRAM_SIZE);
412
413 /* inform the device about the selected Max Datagram Size? */
414 if (!(cdc_ncm_flags(dev) & USB_CDC_NCM_NCAP_MAX_DATAGRAM_SIZE))
415 goto out;
416
417 /* read current mtu value from device */
418 err = usbnet_read_cmd(dev, USB_CDC_GET_MAX_DATAGRAM_SIZE,
419 USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE,
420 0, iface_no, &max_datagram_size, 2);
421 if (err < 0) {
422 dev_dbg(&dev->intf->dev, "GET_MAX_DATAGRAM_SIZE failed\n");
423 goto out;
424 }
425
426 if (le16_to_cpu(max_datagram_size) == ctx->max_datagram_size)
427 goto out;
428
429 max_datagram_size = cpu_to_le16(ctx->max_datagram_size);
430 err = usbnet_write_cmd(dev, USB_CDC_SET_MAX_DATAGRAM_SIZE,
431 USB_TYPE_CLASS | USB_DIR_OUT | USB_RECIP_INTERFACE,
432 0, iface_no, &max_datagram_size, 2);
433 if (err < 0)
434 dev_dbg(&dev->intf->dev, "SET_MAX_DATAGRAM_SIZE failed\n");
435
436out:
437 /* set MTU to max supported by the device if necessary */
438 dev->net->mtu = min_t(int, dev->net->mtu, ctx->max_datagram_size - cdc_ncm_eth_hlen(dev));
439
440 /* do not exceed operater preferred MTU */
441 if (ctx->mbim_extended_desc) {
442 mbim_mtu = le16_to_cpu(ctx->mbim_extended_desc->wMTU);
443 if (mbim_mtu != 0 && mbim_mtu < dev->net->mtu)
444 dev->net->mtu = mbim_mtu;
445 }
446}
447
448static void cdc_ncm_fix_modulus(struct usbnet *dev)
449{
450 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
451 u32 val;
Alexey Orishko900d4952010-11-29 23:23:28 +0000452
453 /*
454 * verify that the structure alignment is:
455 * - power of two
456 * - not greater than the maximum transmit length
457 * - not less than four bytes
458 */
459 val = ctx->tx_ndp_modulus;
460
461 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
462 (val != ((-val) & val)) || (val >= ctx->tx_max)) {
Bjørn Morkae223cd2013-11-01 11:16:54 +0100463 dev_dbg(&dev->intf->dev, "Using default alignment: 4 bytes\n");
Alexey Orishko900d4952010-11-29 23:23:28 +0000464 ctx->tx_ndp_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
465 }
466
467 /*
468 * verify that the payload alignment is:
469 * - power of two
470 * - not greater than the maximum transmit length
471 * - not less than four bytes
472 */
473 val = ctx->tx_modulus;
474
475 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
476 (val != ((-val) & val)) || (val >= ctx->tx_max)) {
Bjørn Morkae223cd2013-11-01 11:16:54 +0100477 dev_dbg(&dev->intf->dev, "Using default transmit modulus: 4 bytes\n");
Alexey Orishko900d4952010-11-29 23:23:28 +0000478 ctx->tx_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
479 }
480
481 /* verify the payload remainder */
482 if (ctx->tx_remainder >= ctx->tx_modulus) {
Bjørn Morkae223cd2013-11-01 11:16:54 +0100483 dev_dbg(&dev->intf->dev, "Using default transmit remainder: 0 bytes\n");
Alexey Orishko900d4952010-11-29 23:23:28 +0000484 ctx->tx_remainder = 0;
485 }
486
487 /* adjust TX-remainder according to NCM specification. */
Bjørn Morkf8afb732014-05-16 21:48:19 +0200488 ctx->tx_remainder = ((ctx->tx_remainder - cdc_ncm_eth_hlen(dev)) &
Greg Suarez2d1c4312012-10-22 10:56:30 +0000489 (ctx->tx_modulus - 1));
Bjørn Morkf8afb732014-05-16 21:48:19 +0200490}
Alexey Orishko900d4952010-11-29 23:23:28 +0000491
Bjørn Morkf8afb732014-05-16 21:48:19 +0200492static int cdc_ncm_setup(struct usbnet *dev)
493{
494 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
Bjørn Mork50f1cb12014-05-16 21:48:26 +0200495 u32 def_rx, def_tx;
496
497 /* be conservative when selecting intial buffer size to
498 * increase the number of hosts this will work for
499 */
500 def_rx = min_t(u32, CDC_NCM_NTB_DEF_SIZE_RX,
501 le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize));
502 def_tx = min_t(u32, CDC_NCM_NTB_DEF_SIZE_TX,
503 le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize));
Alexey Orishko900d4952010-11-29 23:23:28 +0000504
Bjørn Morkf8afb732014-05-16 21:48:19 +0200505 /* clamp rx_max and tx_max and inform device */
Bjørn Mork50f1cb12014-05-16 21:48:26 +0200506 cdc_ncm_update_rxtx_max(dev, def_rx, def_tx);
Alexey Orishko84e77a82011-02-07 09:45:10 +0000507
Bjørn Morkf8afb732014-05-16 21:48:19 +0200508 /* sanitize the modulus and remainder values */
509 cdc_ncm_fix_modulus(dev);
Alexey Orishko900d4952010-11-29 23:23:28 +0000510
Bjørn Morkf8afb732014-05-16 21:48:19 +0200511 /* set max datagram size */
512 cdc_ncm_set_dgram_size(dev, cdc_ncm_max_dgram_size(dev));
Alexey Orishko900d4952010-11-29 23:23:28 +0000513 return 0;
514}
515
516static void
Bjørn Morkff1632a2013-11-01 11:16:41 +0100517cdc_ncm_find_endpoints(struct usbnet *dev, struct usb_interface *intf)
Alexey Orishko900d4952010-11-29 23:23:28 +0000518{
Bjørn Morkff1632a2013-11-01 11:16:41 +0100519 struct usb_host_endpoint *e, *in = NULL, *out = NULL;
Alexey Orishko900d4952010-11-29 23:23:28 +0000520 u8 ep;
521
522 for (ep = 0; ep < intf->cur_altsetting->desc.bNumEndpoints; ep++) {
523
524 e = intf->cur_altsetting->endpoint + ep;
525 switch (e->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
526 case USB_ENDPOINT_XFER_INT:
527 if (usb_endpoint_dir_in(&e->desc)) {
Bjørn Morkff1632a2013-11-01 11:16:41 +0100528 if (!dev->status)
529 dev->status = e;
Alexey Orishko900d4952010-11-29 23:23:28 +0000530 }
531 break;
532
533 case USB_ENDPOINT_XFER_BULK:
534 if (usb_endpoint_dir_in(&e->desc)) {
Bjørn Morkff1632a2013-11-01 11:16:41 +0100535 if (!in)
536 in = e;
Alexey Orishko900d4952010-11-29 23:23:28 +0000537 } else {
Bjørn Morkff1632a2013-11-01 11:16:41 +0100538 if (!out)
539 out = e;
Alexey Orishko900d4952010-11-29 23:23:28 +0000540 }
541 break;
542
543 default:
544 break;
545 }
546 }
Bjørn Morkff1632a2013-11-01 11:16:41 +0100547 if (in && !dev->in)
548 dev->in = usb_rcvbulkpipe(dev->udev,
549 in->desc.bEndpointAddress &
550 USB_ENDPOINT_NUMBER_MASK);
551 if (out && !dev->out)
552 dev->out = usb_sndbulkpipe(dev->udev,
553 out->desc.bEndpointAddress &
554 USB_ENDPOINT_NUMBER_MASK);
Alexey Orishko900d4952010-11-29 23:23:28 +0000555}
556
557static void cdc_ncm_free(struct cdc_ncm_ctx *ctx)
558{
559 if (ctx == NULL)
560 return;
561
Alexey Orishko900d4952010-11-29 23:23:28 +0000562 if (ctx->tx_rem_skb != NULL) {
563 dev_kfree_skb_any(ctx->tx_rem_skb);
564 ctx->tx_rem_skb = NULL;
565 }
566
567 if (ctx->tx_curr_skb != NULL) {
568 dev_kfree_skb_any(ctx->tx_curr_skb);
569 ctx->tx_curr_skb = NULL;
570 }
571
572 kfree(ctx);
573}
574
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000575int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_altsetting)
Alexey Orishko900d4952010-11-29 23:23:28 +0000576{
Bjørn Mork83292232013-11-01 11:16:47 +0100577 const struct usb_cdc_union_desc *union_desc = NULL;
Alexey Orishko900d4952010-11-29 23:23:28 +0000578 struct cdc_ncm_ctx *ctx;
579 struct usb_driver *driver;
580 u8 *buf;
581 int len;
582 int temp;
583 u8 iface_no;
584
Thomas Meyerc7969842011-11-17 12:43:40 +0000585 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
Devendra Naga8f0923c2013-03-29 23:03:22 +0000586 if (!ctx)
587 return -ENOMEM;
Alexey Orishko900d4952010-11-29 23:23:28 +0000588
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000589 hrtimer_init(&ctx->tx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
590 ctx->tx_timer.function = &cdc_ncm_tx_timer_cb;
Bjørn Morkbed6f762013-11-01 11:16:42 +0100591 ctx->bh.data = (unsigned long)dev;
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000592 ctx->bh.func = cdc_ncm_txpath_bh;
593 atomic_set(&ctx->stop, 0);
Alexey Orishko900d4952010-11-29 23:23:28 +0000594 spin_lock_init(&ctx->mtx);
Alexey Orishko900d4952010-11-29 23:23:28 +0000595
596 /* store ctx pointer in device data field */
597 dev->data[0] = (unsigned long)ctx;
598
Bjørn Mork9fe02342013-11-01 11:16:48 +0100599 /* only the control interface can be successfully probed */
600 ctx->control = intf;
601
Alexey Orishko900d4952010-11-29 23:23:28 +0000602 /* get some pointers */
603 driver = driver_of(intf);
604 buf = intf->cur_altsetting->extra;
605 len = intf->cur_altsetting->extralen;
606
Alexey Orishko900d4952010-11-29 23:23:28 +0000607 /* parse through descriptors associated with control interface */
608 while ((len > 0) && (buf[0] > 2) && (buf[0] <= len)) {
609
610 if (buf[1] != USB_DT_CS_INTERFACE)
611 goto advance;
612
613 switch (buf[2]) {
614 case USB_CDC_UNION_TYPE:
Bjørn Mork83292232013-11-01 11:16:47 +0100615 if (buf[0] < sizeof(*union_desc))
Alexey Orishko900d4952010-11-29 23:23:28 +0000616 break;
617
Bjørn Mork83292232013-11-01 11:16:47 +0100618 union_desc = (const struct usb_cdc_union_desc *)buf;
Bjørn Mork9fe02342013-11-01 11:16:48 +0100619 /* the master must be the interface we are probing */
620 if (intf->cur_altsetting->desc.bInterfaceNumber !=
Bjørn Mork296e81f2013-11-01 11:17:00 +0100621 union_desc->bMasterInterface0) {
622 dev_dbg(&intf->dev, "bogus CDC Union\n");
Bjørn Mork9fe02342013-11-01 11:16:48 +0100623 goto error;
Bjørn Mork296e81f2013-11-01 11:17:00 +0100624 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000625 ctx->data = usb_ifnum_to_if(dev->udev,
Bjørn Mork83292232013-11-01 11:16:47 +0100626 union_desc->bSlaveInterface0);
Alexey Orishko900d4952010-11-29 23:23:28 +0000627 break;
628
629 case USB_CDC_ETHERNET_TYPE:
630 if (buf[0] < sizeof(*(ctx->ether_desc)))
631 break;
632
633 ctx->ether_desc =
634 (const struct usb_cdc_ether_desc *)buf;
Alexey Orishko900d4952010-11-29 23:23:28 +0000635 break;
636
637 case USB_CDC_NCM_TYPE:
638 if (buf[0] < sizeof(*(ctx->func_desc)))
639 break;
640
641 ctx->func_desc = (const struct usb_cdc_ncm_desc *)buf;
642 break;
643
Greg Suarez38396e42012-10-22 10:56:31 +0000644 case USB_CDC_MBIM_TYPE:
645 if (buf[0] < sizeof(*(ctx->mbim_desc)))
646 break;
647
648 ctx->mbim_desc = (const struct usb_cdc_mbim_desc *)buf;
649 break;
650
Ben Chan259fef02014-03-19 14:00:06 -0700651 case USB_CDC_MBIM_EXTENDED_TYPE:
652 if (buf[0] < sizeof(*(ctx->mbim_extended_desc)))
653 break;
654
655 ctx->mbim_extended_desc =
656 (const struct usb_cdc_mbim_extended_desc *)buf;
657 break;
658
Alexey Orishko900d4952010-11-29 23:23:28 +0000659 default:
660 break;
661 }
662advance:
663 /* advance to next descriptor */
664 temp = buf[0];
665 buf += temp;
666 len -= temp;
667 }
668
Bjørn Mork9992c2e2013-01-21 05:50:38 +0000669 /* some buggy devices have an IAD but no CDC Union */
Bjørn Mork83292232013-11-01 11:16:47 +0100670 if (!union_desc && intf->intf_assoc && intf->intf_assoc->bInterfaceCount == 2) {
Bjørn Mork56a666d2013-01-25 23:36:59 +0000671 ctx->data = usb_ifnum_to_if(dev->udev, intf->cur_altsetting->desc.bInterfaceNumber + 1);
672 dev_dbg(&intf->dev, "CDC Union missing - got slave from IAD\n");
Bjørn Mork9992c2e2013-01-21 05:50:38 +0000673 }
674
Alexey Orishko900d4952010-11-29 23:23:28 +0000675 /* check if we got everything */
Bjørn Morkf8afb732014-05-16 21:48:19 +0200676 if (!ctx->data) {
677 dev_dbg(&intf->dev, "CDC Union missing and no IAD found\n");
Alexey Orishko900d4952010-11-29 23:23:28 +0000678 goto error;
Bjørn Mork296e81f2013-11-01 11:17:00 +0100679 }
Bjørn Morkf8afb732014-05-16 21:48:19 +0200680 if (cdc_ncm_comm_intf_is_mbim(intf->cur_altsetting)) {
681 if (!ctx->mbim_desc) {
682 dev_dbg(&intf->dev, "MBIM functional descriptor missing\n");
683 goto error;
684 }
685 } else {
686 if (!ctx->ether_desc || !ctx->func_desc) {
687 dev_dbg(&intf->dev, "NCM or ECM functional descriptors missing\n");
688 goto error;
689 }
690 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000691
Bjørn Morkbbc8d922012-11-13 03:19:43 +0000692 /* claim data interface, if different from control */
693 if (ctx->data != ctx->control) {
694 temp = usb_driver_claim_interface(driver, ctx->data, dev);
Bjørn Mork296e81f2013-11-01 11:17:00 +0100695 if (temp) {
696 dev_dbg(&intf->dev, "failed to claim data intf\n");
Bjørn Morkbbc8d922012-11-13 03:19:43 +0000697 goto error;
Bjørn Mork296e81f2013-11-01 11:17:00 +0100698 }
Bjørn Morkbbc8d922012-11-13 03:19:43 +0000699 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000700
701 iface_no = ctx->data->cur_altsetting->desc.bInterfaceNumber;
702
703 /* reset data interface */
704 temp = usb_set_interface(dev->udev, iface_no, 0);
Bjørn Mork296e81f2013-11-01 11:17:00 +0100705 if (temp) {
706 dev_dbg(&intf->dev, "set interface failed\n");
Alexey Orishko19694ac2011-05-24 05:26:13 +0000707 goto error2;
Bjørn Mork296e81f2013-11-01 11:17:00 +0100708 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000709
Bjørn Mork08c74fc2014-05-16 21:48:20 +0200710 /* initialize basic device settings */
711 if (cdc_ncm_init(dev))
Bjørn Morkff0992e92014-03-17 16:25:18 +0100712 goto error2;
713
Alexey Orishko900d4952010-11-29 23:23:28 +0000714 /* configure data interface */
Greg Suarez38396e42012-10-22 10:56:31 +0000715 temp = usb_set_interface(dev->udev, iface_no, data_altsetting);
Bjørn Mork296e81f2013-11-01 11:17:00 +0100716 if (temp) {
717 dev_dbg(&intf->dev, "set interface failed\n");
Alexey Orishko19694ac2011-05-24 05:26:13 +0000718 goto error2;
Bjørn Mork296e81f2013-11-01 11:17:00 +0100719 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000720
Bjørn Morkff1632a2013-11-01 11:16:41 +0100721 cdc_ncm_find_endpoints(dev, ctx->data);
722 cdc_ncm_find_endpoints(dev, ctx->control);
Bjørn Mork296e81f2013-11-01 11:17:00 +0100723 if (!dev->in || !dev->out || !dev->status) {
724 dev_dbg(&intf->dev, "failed to collect endpoints\n");
Alexey Orishko19694ac2011-05-24 05:26:13 +0000725 goto error2;
Bjørn Mork296e81f2013-11-01 11:17:00 +0100726 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000727
Alexey Orishko900d4952010-11-29 23:23:28 +0000728 usb_set_intfdata(ctx->data, dev);
729 usb_set_intfdata(ctx->control, dev);
Alexey Orishko900d4952010-11-29 23:23:28 +0000730
Greg Suarez38396e42012-10-22 10:56:31 +0000731 if (ctx->ether_desc) {
732 temp = usbnet_get_ethernet_addr(dev, ctx->ether_desc->iMACAddress);
Bjørn Mork296e81f2013-11-01 11:17:00 +0100733 if (temp) {
734 dev_dbg(&intf->dev, "failed to get mac address\n");
Greg Suarez38396e42012-10-22 10:56:31 +0000735 goto error2;
Bjørn Mork296e81f2013-11-01 11:17:00 +0100736 }
737 dev_info(&intf->dev, "MAC-Address: %pM\n", dev->net->dev_addr);
Greg Suarez38396e42012-10-22 10:56:31 +0000738 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000739
Bjørn Mork08c74fc2014-05-16 21:48:20 +0200740 /* finish setting up the device specific data */
741 cdc_ncm_setup(dev);
Bjørn Morkff0992e92014-03-17 16:25:18 +0100742
Bjørn Mork6c4e5482014-05-16 21:48:22 +0200743 /* override ethtool_ops */
744 dev->net->ethtool_ops = &cdc_ncm_ethtool_ops;
745
Alexey Orishko900d4952010-11-29 23:23:28 +0000746 return 0;
747
Alexey Orishko19694ac2011-05-24 05:26:13 +0000748error2:
749 usb_set_intfdata(ctx->control, NULL);
750 usb_set_intfdata(ctx->data, NULL);
Bjørn Mork6b4ef602013-01-21 05:50:40 +0000751 if (ctx->data != ctx->control)
752 usb_driver_release_interface(driver, ctx->data);
Alexey Orishko900d4952010-11-29 23:23:28 +0000753error:
754 cdc_ncm_free((struct cdc_ncm_ctx *)dev->data[0]);
755 dev->data[0] = 0;
Bjørn Mork296e81f2013-11-01 11:17:00 +0100756 dev_info(&intf->dev, "bind() failure\n");
Alexey Orishko900d4952010-11-29 23:23:28 +0000757 return -ENODEV;
758}
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000759EXPORT_SYMBOL_GPL(cdc_ncm_bind_common);
Alexey Orishko900d4952010-11-29 23:23:28 +0000760
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000761void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf)
Alexey Orishko900d4952010-11-29 23:23:28 +0000762{
763 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
Alexey Orishko19694ac2011-05-24 05:26:13 +0000764 struct usb_driver *driver = driver_of(intf);
Alexey Orishko900d4952010-11-29 23:23:28 +0000765
766 if (ctx == NULL)
767 return; /* no setup */
768
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000769 atomic_set(&ctx->stop, 1);
770
771 if (hrtimer_active(&ctx->tx_timer))
772 hrtimer_cancel(&ctx->tx_timer);
773
774 tasklet_kill(&ctx->bh);
775
Bjørn Morkbbc8d922012-11-13 03:19:43 +0000776 /* handle devices with combined control and data interface */
777 if (ctx->control == ctx->data)
778 ctx->data = NULL;
779
Alexey Orishko19694ac2011-05-24 05:26:13 +0000780 /* disconnect master --> disconnect slave */
781 if (intf == ctx->control && ctx->data) {
782 usb_set_intfdata(ctx->data, NULL);
Alexey Orishko900d4952010-11-29 23:23:28 +0000783 usb_driver_release_interface(driver, ctx->data);
Alexey Orishko19694ac2011-05-24 05:26:13 +0000784 ctx->data = NULL;
Alexey Orishko900d4952010-11-29 23:23:28 +0000785
Alexey Orishko19694ac2011-05-24 05:26:13 +0000786 } else if (intf == ctx->data && ctx->control) {
787 usb_set_intfdata(ctx->control, NULL);
Alexey Orishko900d4952010-11-29 23:23:28 +0000788 usb_driver_release_interface(driver, ctx->control);
Alexey Orishko19694ac2011-05-24 05:26:13 +0000789 ctx->control = NULL;
Alexey Orishko900d4952010-11-29 23:23:28 +0000790 }
791
Bjørn Mork3e515662013-11-01 11:16:40 +0100792 usb_set_intfdata(intf, NULL);
Alexey Orishko900d4952010-11-29 23:23:28 +0000793 cdc_ncm_free(ctx);
794}
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000795EXPORT_SYMBOL_GPL(cdc_ncm_unbind);
Alexey Orishko900d4952010-11-29 23:23:28 +0000796
Bjørn Mork50a0ffa2014-05-11 10:47:15 +0200797/* Return the number of the MBIM control interface altsetting iff it
798 * is preferred and available,
Bjørn Mork1e8bbe62013-03-14 01:05:13 +0000799 */
Bjørn Mork50a0ffa2014-05-11 10:47:15 +0200800u8 cdc_ncm_select_altsetting(struct usb_interface *intf)
Greg Suarez38396e42012-10-22 10:56:31 +0000801{
Bjørn Mork1e8bbe62013-03-14 01:05:13 +0000802 struct usb_host_interface *alt;
Greg Suarez38396e42012-10-22 10:56:31 +0000803
Bjørn Morkbd329e12012-10-22 10:56:38 +0000804 /* The MBIM spec defines a NCM compatible default altsetting,
805 * which we may have matched:
806 *
807 * "Functions that implement both NCM 1.0 and MBIM (an
808 * “NCM/MBIM function”) according to this recommendation
809 * shall provide two alternate settings for the
810 * Communication Interface. Alternate setting 0, and the
811 * associated class and endpoint descriptors, shall be
812 * constructed according to the rules given for the
813 * Communication Interface in section 5 of [USBNCM10].
814 * Alternate setting 1, and the associated class and
815 * endpoint descriptors, shall be constructed according to
816 * the rules given in section 6 (USB Device Model) of this
817 * specification."
Bjørn Morkbd329e12012-10-22 10:56:38 +0000818 */
Bjørn Mork50a0ffa2014-05-11 10:47:15 +0200819 if (intf->num_altsetting < 2)
820 return intf->cur_altsetting->desc.bAlternateSetting;
821
822 if (prefer_mbim) {
Bjørn Mork1e8bbe62013-03-14 01:05:13 +0000823 alt = usb_altnum_to_altsetting(intf, CDC_NCM_COMM_ALTSETTING_MBIM);
Bjørn Mork50a0ffa2014-05-11 10:47:15 +0200824 if (alt && cdc_ncm_comm_intf_is_mbim(alt))
825 return CDC_NCM_COMM_ALTSETTING_MBIM;
Bjørn Morkf350ca02013-02-13 12:09:52 +0000826 }
Bjørn Mork50a0ffa2014-05-11 10:47:15 +0200827 return CDC_NCM_COMM_ALTSETTING_NCM;
Bjørn Mork1e8bbe62013-03-14 01:05:13 +0000828}
829EXPORT_SYMBOL_GPL(cdc_ncm_select_altsetting);
830
831static int cdc_ncm_bind(struct usbnet *dev, struct usb_interface *intf)
832{
833 int ret;
834
835 /* MBIM backwards compatible function? */
Bjørn Mork50a0ffa2014-05-11 10:47:15 +0200836 if (cdc_ncm_select_altsetting(intf) != CDC_NCM_COMM_ALTSETTING_NCM)
Bjørn Mork1e8bbe62013-03-14 01:05:13 +0000837 return -ENODEV;
Bjørn Morkbd329e12012-10-22 10:56:38 +0000838
Bjørn Mork50a0ffa2014-05-11 10:47:15 +0200839 /* The NCM data altsetting is fixed */
840 ret = cdc_ncm_bind_common(dev, intf, CDC_NCM_DATA_ALTSETTING_NCM);
Greg Suarez38396e42012-10-22 10:56:31 +0000841
842 /*
843 * We should get an event when network connection is "connected" or
844 * "disconnected". Set network connection in "disconnected" state
845 * (carrier is OFF) during attach, so the IP network stack does not
846 * start IPv6 negotiation and more.
847 */
Ming Lei8a34b0a2013-04-11 04:40:33 +0000848 usbnet_link_change(dev, 0, 0);
Greg Suarez38396e42012-10-22 10:56:31 +0000849 return ret;
850}
851
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000852static void cdc_ncm_align_tail(struct sk_buff *skb, size_t modulus, size_t remainder, size_t max)
Alexey Orishko900d4952010-11-29 23:23:28 +0000853{
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000854 size_t align = ALIGN(skb->len, modulus) - skb->len + remainder;
855
856 if (skb->len + align > max)
857 align = max - skb->len;
858 if (align && skb_tailroom(skb) >= align)
859 memset(skb_put(skb, align), 0, align);
860}
861
862/* return a pointer to a valid struct usb_cdc_ncm_ndp16 of type sign, possibly
863 * allocating a new one within skb
864 */
865static struct usb_cdc_ncm_ndp16 *cdc_ncm_ndp(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign, size_t reserve)
866{
867 struct usb_cdc_ncm_ndp16 *ndp16 = NULL;
868 struct usb_cdc_ncm_nth16 *nth16 = (void *)skb->data;
869 size_t ndpoffset = le16_to_cpu(nth16->wNdpIndex);
870
871 /* follow the chain of NDPs, looking for a match */
872 while (ndpoffset) {
873 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb->data + ndpoffset);
874 if (ndp16->dwSignature == sign)
875 return ndp16;
876 ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
877 }
878
879 /* align new NDP */
880 cdc_ncm_align_tail(skb, ctx->tx_ndp_modulus, 0, ctx->tx_max);
881
882 /* verify that there is room for the NDP and the datagram (reserve) */
Bjørn Mork70559b82014-05-16 21:48:23 +0200883 if ((ctx->tx_max - skb->len - reserve) < ctx->max_ndp_size)
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000884 return NULL;
885
886 /* link to it */
887 if (ndp16)
888 ndp16->wNextNdpIndex = cpu_to_le16(skb->len);
889 else
890 nth16->wNdpIndex = cpu_to_le16(skb->len);
891
892 /* push a new empty NDP */
Bjørn Mork70559b82014-05-16 21:48:23 +0200893 ndp16 = (struct usb_cdc_ncm_ndp16 *)memset(skb_put(skb, ctx->max_ndp_size), 0, ctx->max_ndp_size);
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000894 ndp16->dwSignature = sign;
895 ndp16->wLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_ndp16) + sizeof(struct usb_cdc_ncm_dpe16));
896 return ndp16;
Alexey Orishko900d4952010-11-29 23:23:28 +0000897}
898
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000899struct sk_buff *
Bjørn Morkbed6f762013-11-01 11:16:42 +0100900cdc_ncm_fill_tx_frame(struct usbnet *dev, struct sk_buff *skb, __le32 sign)
Alexey Orishko900d4952010-11-29 23:23:28 +0000901{
Bjørn Morkbed6f762013-11-01 11:16:42 +0100902 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000903 struct usb_cdc_ncm_nth16 *nth16;
904 struct usb_cdc_ncm_ndp16 *ndp16;
Alexey Orishko900d4952010-11-29 23:23:28 +0000905 struct sk_buff *skb_out;
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000906 u16 n = 0, index, ndplen;
Alexey Orishko84e77a82011-02-07 09:45:10 +0000907 u8 ready2send = 0;
Alexey Orishko900d4952010-11-29 23:23:28 +0000908
909 /* if there is a remaining skb, it gets priority */
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000910 if (skb != NULL) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000911 swap(skb, ctx->tx_rem_skb);
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000912 swap(sign, ctx->tx_rem_sign);
913 } else {
Alexey Orishko84e77a82011-02-07 09:45:10 +0000914 ready2send = 1;
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000915 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000916
917 /* check if we are resuming an OUT skb */
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000918 skb_out = ctx->tx_curr_skb;
Alexey Orishko900d4952010-11-29 23:23:28 +0000919
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000920 /* allocate a new OUT skb */
921 if (!skb_out) {
Bjørn Mork20572222013-11-01 11:16:38 +0100922 skb_out = alloc_skb(ctx->tx_max, GFP_ATOMIC);
Alexey Orishko900d4952010-11-29 23:23:28 +0000923 if (skb_out == NULL) {
924 if (skb != NULL) {
925 dev_kfree_skb_any(skb);
Bjørn Morkbed6f762013-11-01 11:16:42 +0100926 dev->net->stats.tx_dropped++;
Alexey Orishko900d4952010-11-29 23:23:28 +0000927 }
928 goto exit_no_skb;
929 }
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000930 /* fill out the initial 16-bit NTB header */
931 nth16 = (struct usb_cdc_ncm_nth16 *)memset(skb_put(skb_out, sizeof(struct usb_cdc_ncm_nth16)), 0, sizeof(struct usb_cdc_ncm_nth16));
932 nth16->dwSignature = cpu_to_le32(USB_CDC_NCM_NTH16_SIGN);
933 nth16->wHeaderLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_nth16));
934 nth16->wSequence = cpu_to_le16(ctx->tx_seq++);
Alexey Orishko900d4952010-11-29 23:23:28 +0000935
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000936 /* count total number of frames in this NTB */
Alexey Orishko900d4952010-11-29 23:23:28 +0000937 ctx->tx_curr_frame_num = 0;
Bjørn Morkbeeecd42014-05-16 21:48:25 +0200938
939 /* recent payload counter for this skb_out */
940 ctx->tx_curr_frame_payload = 0;
Alexey Orishko900d4952010-11-29 23:23:28 +0000941 }
942
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000943 for (n = ctx->tx_curr_frame_num; n < ctx->tx_max_datagrams; n++) {
944 /* send any remaining skb first */
Alexey Orishko900d4952010-11-29 23:23:28 +0000945 if (skb == NULL) {
946 skb = ctx->tx_rem_skb;
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000947 sign = ctx->tx_rem_sign;
Alexey Orishko900d4952010-11-29 23:23:28 +0000948 ctx->tx_rem_skb = NULL;
949
950 /* check for end of skb */
951 if (skb == NULL)
952 break;
953 }
954
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000955 /* get the appropriate NDP for this skb */
956 ndp16 = cdc_ncm_ndp(ctx, skb_out, sign, skb->len + ctx->tx_modulus + ctx->tx_remainder);
957
958 /* align beginning of next frame */
959 cdc_ncm_align_tail(skb_out, ctx->tx_modulus, ctx->tx_remainder, ctx->tx_max);
960
961 /* check if we had enough room left for both NDP and frame */
962 if (!ndp16 || skb_out->len + skb->len > ctx->tx_max) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000963 if (n == 0) {
964 /* won't fit, MTU problem? */
965 dev_kfree_skb_any(skb);
966 skb = NULL;
Bjørn Morkbed6f762013-11-01 11:16:42 +0100967 dev->net->stats.tx_dropped++;
Alexey Orishko900d4952010-11-29 23:23:28 +0000968 } else {
969 /* no room for skb - store for later */
970 if (ctx->tx_rem_skb != NULL) {
971 dev_kfree_skb_any(ctx->tx_rem_skb);
Bjørn Morkbed6f762013-11-01 11:16:42 +0100972 dev->net->stats.tx_dropped++;
Alexey Orishko900d4952010-11-29 23:23:28 +0000973 }
974 ctx->tx_rem_skb = skb;
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000975 ctx->tx_rem_sign = sign;
Alexey Orishko900d4952010-11-29 23:23:28 +0000976 skb = NULL;
Alexey Orishko84e77a82011-02-07 09:45:10 +0000977 ready2send = 1;
Bjørn Morkbeeecd42014-05-16 21:48:25 +0200978 ctx->tx_reason_ntb_full++; /* count reason for transmitting */
Alexey Orishko900d4952010-11-29 23:23:28 +0000979 }
980 break;
981 }
982
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000983 /* calculate frame number withing this NDP */
984 ndplen = le16_to_cpu(ndp16->wLength);
985 index = (ndplen - sizeof(struct usb_cdc_ncm_ndp16)) / sizeof(struct usb_cdc_ncm_dpe16) - 1;
Alexey Orishko900d4952010-11-29 23:23:28 +0000986
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000987 /* OK, add this skb */
988 ndp16->dpe16[index].wDatagramLength = cpu_to_le16(skb->len);
989 ndp16->dpe16[index].wDatagramIndex = cpu_to_le16(skb_out->len);
990 ndp16->wLength = cpu_to_le16(ndplen + sizeof(struct usb_cdc_ncm_dpe16));
991 memcpy(skb_put(skb_out, skb->len), skb->data, skb->len);
Bjørn Morkbeeecd42014-05-16 21:48:25 +0200992 ctx->tx_curr_frame_payload += skb->len; /* count real tx payload data */
Alexey Orishko900d4952010-11-29 23:23:28 +0000993 dev_kfree_skb_any(skb);
994 skb = NULL;
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000995
996 /* send now if this NDP is full */
997 if (index >= CDC_NCM_DPT_DATAGRAMS_MAX) {
998 ready2send = 1;
Bjørn Morkbeeecd42014-05-16 21:48:25 +0200999 ctx->tx_reason_ndp_full++; /* count reason for transmitting */
Bjørn Morkc78b7c52012-10-22 10:56:34 +00001000 break;
1001 }
Alexey Orishko900d4952010-11-29 23:23:28 +00001002 }
1003
1004 /* free up any dangling skb */
1005 if (skb != NULL) {
1006 dev_kfree_skb_any(skb);
1007 skb = NULL;
Bjørn Morkbed6f762013-11-01 11:16:42 +01001008 dev->net->stats.tx_dropped++;
Alexey Orishko900d4952010-11-29 23:23:28 +00001009 }
1010
1011 ctx->tx_curr_frame_num = n;
1012
1013 if (n == 0) {
1014 /* wait for more frames */
1015 /* push variables */
1016 ctx->tx_curr_skb = skb_out;
Alexey Orishko900d4952010-11-29 23:23:28 +00001017 goto exit_no_skb;
1018
Bjørn Mork6c4e5482014-05-16 21:48:22 +02001019 } else if ((n < ctx->tx_max_datagrams) && (ready2send == 0) && (ctx->timer_interval > 0)) {
Alexey Orishko900d4952010-11-29 23:23:28 +00001020 /* wait for more frames */
1021 /* push variables */
1022 ctx->tx_curr_skb = skb_out;
Alexey Orishko900d4952010-11-29 23:23:28 +00001023 /* set the pending count */
1024 if (n < CDC_NCM_RESTART_TIMER_DATAGRAM_CNT)
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +00001025 ctx->tx_timer_pending = CDC_NCM_TIMER_PENDING_CNT;
Alexey Orishko900d4952010-11-29 23:23:28 +00001026 goto exit_no_skb;
1027
1028 } else {
Bjørn Morkbeeecd42014-05-16 21:48:25 +02001029 if (n == ctx->tx_max_datagrams)
1030 ctx->tx_reason_max_datagram++; /* count reason for transmitting */
Alexey Orishko900d4952010-11-29 23:23:28 +00001031 /* frame goes out */
1032 /* variables will be reset at next call */
1033 }
1034
Bjørn Mork43e4c6d2014-05-16 21:48:24 +02001035 /* If collected data size is less or equal ctx->min_tx_pkt
Bjørn Mork20572222013-11-01 11:16:38 +01001036 * bytes, we send buffers as it is. If we get more data, it
1037 * would be more efficient for USB HS mobile device with DMA
1038 * engine to receive a full size NTB, than canceling DMA
1039 * transfer and receiving a short packet.
Bjørn Mork4d619f62013-11-01 11:16:49 +01001040 *
1041 * This optimization support is pointless if we end up sending
1042 * a ZLP after full sized NTBs.
Alexey Orishko900d4952010-11-29 23:23:28 +00001043 */
Bjørn Mork4d619f62013-11-01 11:16:49 +01001044 if (!(dev->driver_info->flags & FLAG_SEND_ZLP) &&
Bjørn Mork43e4c6d2014-05-16 21:48:24 +02001045 skb_out->len > ctx->min_tx_pkt)
Bjørn Mork20572222013-11-01 11:16:38 +01001046 memset(skb_put(skb_out, ctx->tx_max - skb_out->len), 0,
1047 ctx->tx_max - skb_out->len);
Bjørn Mork9becd702014-05-02 23:27:00 +02001048 else if (skb_out->len < ctx->tx_max && (skb_out->len % dev->maxpacket) == 0)
Bjørn Morkc78b7c52012-10-22 10:56:34 +00001049 *skb_put(skb_out, 1) = 0; /* force short packet */
Alexey Orishko900d4952010-11-29 23:23:28 +00001050
Bjørn Morkc78b7c52012-10-22 10:56:34 +00001051 /* set final frame length */
1052 nth16 = (struct usb_cdc_ncm_nth16 *)skb_out->data;
1053 nth16->wBlockLength = cpu_to_le16(skb_out->len);
Alexey Orishko900d4952010-11-29 23:23:28 +00001054
1055 /* return skb */
1056 ctx->tx_curr_skb = NULL;
Bjørn Morkbed6f762013-11-01 11:16:42 +01001057 dev->net->stats.tx_packets += ctx->tx_curr_frame_num;
Bjørn Morkbeeecd42014-05-16 21:48:25 +02001058
1059 /* keep private stats: framing overhead and number of NTBs */
1060 ctx->tx_overhead += skb_out->len - ctx->tx_curr_frame_payload;
1061 ctx->tx_ntbs++;
1062
1063 /* usbnet has already counted all the framing overhead.
1064 * Adjust the stats so that the tx_bytes counter show real
1065 * payload data instead.
1066 */
1067 dev->net->stats.tx_bytes -= skb_out->len - ctx->tx_curr_frame_payload;
1068
Alexey Orishko900d4952010-11-29 23:23:28 +00001069 return skb_out;
1070
1071exit_no_skb:
Bjørn Mork046c6592014-05-16 21:48:29 +02001072 /* Start timer, if there is a remaining non-empty skb */
1073 if (ctx->tx_curr_skb != NULL && n > 0)
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +00001074 cdc_ncm_tx_timeout_start(ctx);
Alexey Orishko900d4952010-11-29 23:23:28 +00001075 return NULL;
1076}
Bjørn Morkc91ce3b2012-10-22 10:56:35 +00001077EXPORT_SYMBOL_GPL(cdc_ncm_fill_tx_frame);
Alexey Orishko900d4952010-11-29 23:23:28 +00001078
1079static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx)
1080{
1081 /* start timer, if not already started */
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +00001082 if (!(hrtimer_active(&ctx->tx_timer) || atomic_read(&ctx->stop)))
1083 hrtimer_start(&ctx->tx_timer,
Bjørn Mork6c4e5482014-05-16 21:48:22 +02001084 ktime_set(0, ctx->timer_interval),
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +00001085 HRTIMER_MODE_REL);
Alexey Orishko900d4952010-11-29 23:23:28 +00001086}
1087
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +00001088static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *timer)
Alexey Orishko900d4952010-11-29 23:23:28 +00001089{
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +00001090 struct cdc_ncm_ctx *ctx =
1091 container_of(timer, struct cdc_ncm_ctx, tx_timer);
Alexey Orishko900d4952010-11-29 23:23:28 +00001092
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +00001093 if (!atomic_read(&ctx->stop))
1094 tasklet_schedule(&ctx->bh);
1095 return HRTIMER_NORESTART;
1096}
1097
1098static void cdc_ncm_txpath_bh(unsigned long param)
1099{
Bjørn Morkbed6f762013-11-01 11:16:42 +01001100 struct usbnet *dev = (struct usbnet *)param;
1101 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +00001102
1103 spin_lock_bh(&ctx->mtx);
Alexey Orishko900d4952010-11-29 23:23:28 +00001104 if (ctx->tx_timer_pending != 0) {
1105 ctx->tx_timer_pending--;
Alexey Orishko900d4952010-11-29 23:23:28 +00001106 cdc_ncm_tx_timeout_start(ctx);
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +00001107 spin_unlock_bh(&ctx->mtx);
Bjørn Morkbed6f762013-11-01 11:16:42 +01001108 } else if (dev->net != NULL) {
Bjørn Morkbeeecd42014-05-16 21:48:25 +02001109 ctx->tx_reason_timeout++; /* count reason for transmitting */
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +00001110 spin_unlock_bh(&ctx->mtx);
Bjørn Morkbed6f762013-11-01 11:16:42 +01001111 netif_tx_lock_bh(dev->net);
1112 usbnet_start_xmit(NULL, dev->net);
1113 netif_tx_unlock_bh(dev->net);
Bjørn Mork7b1e0cb2012-10-25 21:44:09 +00001114 } else {
1115 spin_unlock_bh(&ctx->mtx);
Alexey Orishkof742aa82011-01-17 07:07:25 +00001116 }
Alexey Orishko900d4952010-11-29 23:23:28 +00001117}
1118
Enrico Mioso2f69702c2013-11-04 09:50:47 +01001119struct sk_buff *
Alexey Orishko900d4952010-11-29 23:23:28 +00001120cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
1121{
1122 struct sk_buff *skb_out;
1123 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
Alexey Orishko900d4952010-11-29 23:23:28 +00001124
1125 /*
1126 * The Ethernet API we are using does not support transmitting
1127 * multiple Ethernet frames in a single call. This driver will
1128 * accumulate multiple Ethernet frames and send out a larger
1129 * USB frame when the USB buffer is full or when a single jiffies
1130 * timeout happens.
1131 */
1132 if (ctx == NULL)
1133 goto error;
1134
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +00001135 spin_lock_bh(&ctx->mtx);
Bjørn Morkbed6f762013-11-01 11:16:42 +01001136 skb_out = cdc_ncm_fill_tx_frame(dev, skb, cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN));
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +00001137 spin_unlock_bh(&ctx->mtx);
Alexey Orishko900d4952010-11-29 23:23:28 +00001138 return skb_out;
1139
1140error:
1141 if (skb != NULL)
1142 dev_kfree_skb_any(skb);
1143
1144 return NULL;
1145}
Enrico Mioso2f69702c2013-11-04 09:50:47 +01001146EXPORT_SYMBOL_GPL(cdc_ncm_tx_fixup);
Alexey Orishko900d4952010-11-29 23:23:28 +00001147
Bjørn Morkff06ab12012-10-22 10:56:33 +00001148/* verify NTB header and return offset of first NDP, or negative error */
Bjørn Morkc91ce3b2012-10-22 10:56:35 +00001149int cdc_ncm_rx_verify_nth16(struct cdc_ncm_ctx *ctx, struct sk_buff *skb_in)
Alexey Orishko900d4952010-11-29 23:23:28 +00001150{
Bjørn Morkae223cd2013-11-01 11:16:54 +01001151 struct usbnet *dev = netdev_priv(skb_in->dev);
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +00001152 struct usb_cdc_ncm_nth16 *nth16;
Bjørn Morkff06ab12012-10-22 10:56:33 +00001153 int len;
1154 int ret = -EINVAL;
Alexey Orishko900d4952010-11-29 23:23:28 +00001155
Alexey Orishko900d4952010-11-29 23:23:28 +00001156 if (ctx == NULL)
1157 goto error;
1158
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +00001159 if (skb_in->len < (sizeof(struct usb_cdc_ncm_nth16) +
1160 sizeof(struct usb_cdc_ncm_ndp16))) {
Bjørn Morkae223cd2013-11-01 11:16:54 +01001161 netif_dbg(dev, rx_err, dev->net, "frame too short\n");
Alexey Orishko900d4952010-11-29 23:23:28 +00001162 goto error;
1163 }
1164
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +00001165 nth16 = (struct usb_cdc_ncm_nth16 *)skb_in->data;
Alexey Orishko900d4952010-11-29 23:23:28 +00001166
Bjørn Mork986e10d2013-11-01 11:16:56 +01001167 if (nth16->dwSignature != cpu_to_le32(USB_CDC_NCM_NTH16_SIGN)) {
Bjørn Mork5448d752013-11-01 11:16:55 +01001168 netif_dbg(dev, rx_err, dev->net,
1169 "invalid NTH16 signature <%#010x>\n",
1170 le32_to_cpu(nth16->dwSignature));
Alexey Orishko900d4952010-11-29 23:23:28 +00001171 goto error;
1172 }
1173
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +00001174 len = le16_to_cpu(nth16->wBlockLength);
1175 if (len > ctx->rx_max) {
Bjørn Morkae223cd2013-11-01 11:16:54 +01001176 netif_dbg(dev, rx_err, dev->net,
1177 "unsupported NTB block length %u/%u\n", len,
1178 ctx->rx_max);
Alexey Orishko900d4952010-11-29 23:23:28 +00001179 goto error;
1180 }
1181
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +00001182 if ((ctx->rx_seq + 1) != le16_to_cpu(nth16->wSequence) &&
Bjørn Morkae223cd2013-11-01 11:16:54 +01001183 (ctx->rx_seq || le16_to_cpu(nth16->wSequence)) &&
1184 !((ctx->rx_seq == 0xffff) && !le16_to_cpu(nth16->wSequence))) {
1185 netif_dbg(dev, rx_err, dev->net,
1186 "sequence number glitch prev=%d curr=%d\n",
1187 ctx->rx_seq, le16_to_cpu(nth16->wSequence));
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +00001188 }
1189 ctx->rx_seq = le16_to_cpu(nth16->wSequence);
1190
Bjørn Morkff06ab12012-10-22 10:56:33 +00001191 ret = le16_to_cpu(nth16->wNdpIndex);
1192error:
1193 return ret;
1194}
Bjørn Morkc91ce3b2012-10-22 10:56:35 +00001195EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_nth16);
Bjørn Morkff06ab12012-10-22 10:56:33 +00001196
1197/* verify NDP header and return number of datagrams, or negative error */
Bjørn Morkc91ce3b2012-10-22 10:56:35 +00001198int cdc_ncm_rx_verify_ndp16(struct sk_buff *skb_in, int ndpoffset)
Bjørn Morkff06ab12012-10-22 10:56:33 +00001199{
Bjørn Morkae223cd2013-11-01 11:16:54 +01001200 struct usbnet *dev = netdev_priv(skb_in->dev);
Bjørn Morkff06ab12012-10-22 10:56:33 +00001201 struct usb_cdc_ncm_ndp16 *ndp16;
1202 int ret = -EINVAL;
1203
Bjørn Mork75d67d32012-10-22 10:56:32 +00001204 if ((ndpoffset + sizeof(struct usb_cdc_ncm_ndp16)) > skb_in->len) {
Bjørn Morkae223cd2013-11-01 11:16:54 +01001205 netif_dbg(dev, rx_err, dev->net, "invalid NDP offset <%u>\n",
1206 ndpoffset);
Alexey Orishko900d4952010-11-29 23:23:28 +00001207 goto error;
1208 }
Bjørn Mork75d67d32012-10-22 10:56:32 +00001209 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
Alexey Orishko900d4952010-11-29 23:23:28 +00001210
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +00001211 if (le16_to_cpu(ndp16->wLength) < USB_CDC_NCM_NDP16_LENGTH_MIN) {
Bjørn Morkae223cd2013-11-01 11:16:54 +01001212 netif_dbg(dev, rx_err, dev->net, "invalid DPT16 length <%u>\n",
1213 le16_to_cpu(ndp16->wLength));
Bjørn Morkff06ab12012-10-22 10:56:33 +00001214 goto error;
Alexey Orishko900d4952010-11-29 23:23:28 +00001215 }
1216
Bjørn Morkff06ab12012-10-22 10:56:33 +00001217 ret = ((le16_to_cpu(ndp16->wLength) -
Alexey Orishko900d4952010-11-29 23:23:28 +00001218 sizeof(struct usb_cdc_ncm_ndp16)) /
1219 sizeof(struct usb_cdc_ncm_dpe16));
Bjørn Morkff06ab12012-10-22 10:56:33 +00001220 ret--; /* we process NDP entries except for the last one */
Alexey Orishko900d4952010-11-29 23:23:28 +00001221
Bjørn Morkae223cd2013-11-01 11:16:54 +01001222 if ((sizeof(struct usb_cdc_ncm_ndp16) +
1223 ret * (sizeof(struct usb_cdc_ncm_dpe16))) > skb_in->len) {
1224 netif_dbg(dev, rx_err, dev->net, "Invalid nframes = %d\n", ret);
Bjørn Morkff06ab12012-10-22 10:56:33 +00001225 ret = -EINVAL;
Alexey Orishko900d4952010-11-29 23:23:28 +00001226 }
1227
Bjørn Morkff06ab12012-10-22 10:56:33 +00001228error:
1229 return ret;
1230}
Bjørn Morkc91ce3b2012-10-22 10:56:35 +00001231EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_ndp16);
Bjørn Morkff06ab12012-10-22 10:56:33 +00001232
Enrico Mioso2f69702c2013-11-04 09:50:47 +01001233int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
Bjørn Morkff06ab12012-10-22 10:56:33 +00001234{
1235 struct sk_buff *skb;
1236 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
1237 int len;
1238 int nframes;
1239 int x;
1240 int offset;
1241 struct usb_cdc_ncm_ndp16 *ndp16;
1242 struct usb_cdc_ncm_dpe16 *dpe16;
1243 int ndpoffset;
1244 int loopcount = 50; /* arbitrary max preventing infinite loop */
Bjørn Morkbeeecd42014-05-16 21:48:25 +02001245 u32 payload = 0;
Bjørn Morkff06ab12012-10-22 10:56:33 +00001246
1247 ndpoffset = cdc_ncm_rx_verify_nth16(ctx, skb_in);
1248 if (ndpoffset < 0)
1249 goto error;
1250
1251next_ndp:
1252 nframes = cdc_ncm_rx_verify_ndp16(skb_in, ndpoffset);
1253 if (nframes < 0)
1254 goto error;
1255
1256 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
1257
Bjørn Mork986e10d2013-11-01 11:16:56 +01001258 if (ndp16->dwSignature != cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN)) {
Bjørn Mork5448d752013-11-01 11:16:55 +01001259 netif_dbg(dev, rx_err, dev->net,
1260 "invalid DPT16 signature <%#010x>\n",
1261 le32_to_cpu(ndp16->dwSignature));
Bjørn Morkff06ab12012-10-22 10:56:33 +00001262 goto err_ndp;
1263 }
1264 dpe16 = ndp16->dpe16;
Alexey Orishko900d4952010-11-29 23:23:28 +00001265
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +00001266 for (x = 0; x < nframes; x++, dpe16++) {
1267 offset = le16_to_cpu(dpe16->wDatagramIndex);
1268 len = le16_to_cpu(dpe16->wDatagramLength);
Alexey Orishko900d4952010-11-29 23:23:28 +00001269
1270 /*
1271 * CDC NCM ch. 3.7
1272 * All entries after first NULL entry are to be ignored
1273 */
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +00001274 if ((offset == 0) || (len == 0)) {
Alexey Orishko900d4952010-11-29 23:23:28 +00001275 if (!x)
Bjørn Mork75d67d32012-10-22 10:56:32 +00001276 goto err_ndp; /* empty NTB */
Alexey Orishko900d4952010-11-29 23:23:28 +00001277 break;
1278 }
1279
1280 /* sanity checking */
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +00001281 if (((offset + len) > skb_in->len) ||
1282 (len > ctx->rx_max) || (len < ETH_HLEN)) {
Bjørn Morkae223cd2013-11-01 11:16:54 +01001283 netif_dbg(dev, rx_err, dev->net,
1284 "invalid frame detected (ignored) offset[%u]=%u, length=%u, skb=%p\n",
1285 x, offset, len, skb_in);
Alexey Orishko900d4952010-11-29 23:23:28 +00001286 if (!x)
Bjørn Mork75d67d32012-10-22 10:56:32 +00001287 goto err_ndp;
Alexey Orishko900d4952010-11-29 23:23:28 +00001288 break;
1289
1290 } else {
1291 skb = skb_clone(skb_in, GFP_ATOMIC);
Jesper Juhl9e567902011-01-13 11:40:11 +00001292 if (!skb)
1293 goto error;
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +00001294 skb->len = len;
Alexey Orishko900d4952010-11-29 23:23:28 +00001295 skb->data = ((u8 *)skb_in->data) + offset;
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +00001296 skb_set_tail_pointer(skb, len);
Alexey Orishko900d4952010-11-29 23:23:28 +00001297 usbnet_skb_return(dev, skb);
Bjørn Morkbeeecd42014-05-16 21:48:25 +02001298 payload += len; /* count payload bytes in this NTB */
Alexey Orishko900d4952010-11-29 23:23:28 +00001299 }
1300 }
Bjørn Mork75d67d32012-10-22 10:56:32 +00001301err_ndp:
1302 /* are there more NDPs to process? */
1303 ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
1304 if (ndpoffset && loopcount--)
1305 goto next_ndp;
1306
Bjørn Morkbeeecd42014-05-16 21:48:25 +02001307 /* update stats */
1308 ctx->rx_overhead += skb_in->len - payload;
1309 ctx->rx_ntbs++;
1310
Alexey Orishko900d4952010-11-29 23:23:28 +00001311 return 1;
1312error:
1313 return 0;
1314}
Enrico Mioso2f69702c2013-11-04 09:50:47 +01001315EXPORT_SYMBOL_GPL(cdc_ncm_rx_fixup);
Alexey Orishko900d4952010-11-29 23:23:28 +00001316
1317static void
Bjørn Morkbed6f762013-11-01 11:16:42 +01001318cdc_ncm_speed_change(struct usbnet *dev,
Alexey Orishko84e77a82011-02-07 09:45:10 +00001319 struct usb_cdc_speed_change *data)
Alexey Orishko900d4952010-11-29 23:23:28 +00001320{
Alexey Orishko84e77a82011-02-07 09:45:10 +00001321 uint32_t rx_speed = le32_to_cpu(data->DLBitRRate);
1322 uint32_t tx_speed = le32_to_cpu(data->ULBitRate);
Alexey Orishko900d4952010-11-29 23:23:28 +00001323
1324 /*
1325 * Currently the USB-NET API does not support reporting the actual
1326 * device speed. Do print it instead.
1327 */
Bjørn Morkf3028c52013-11-01 11:16:44 +01001328 if ((tx_speed > 1000000) && (rx_speed > 1000000)) {
Bjørn Morkae223cd2013-11-01 11:16:54 +01001329 netif_info(dev, link, dev->net,
Bjørn Mork916f7642014-05-16 21:48:27 +02001330 "%u mbit/s downlink %u mbit/s uplink\n",
1331 (unsigned int)(rx_speed / 1000000U),
1332 (unsigned int)(tx_speed / 1000000U));
Bjørn Morkf3028c52013-11-01 11:16:44 +01001333 } else {
Bjørn Morkae223cd2013-11-01 11:16:54 +01001334 netif_info(dev, link, dev->net,
Bjørn Mork916f7642014-05-16 21:48:27 +02001335 "%u kbit/s downlink %u kbit/s uplink\n",
1336 (unsigned int)(rx_speed / 1000U),
1337 (unsigned int)(tx_speed / 1000U));
Alexey Orishko900d4952010-11-29 23:23:28 +00001338 }
1339}
1340
1341static void cdc_ncm_status(struct usbnet *dev, struct urb *urb)
1342{
1343 struct cdc_ncm_ctx *ctx;
1344 struct usb_cdc_notification *event;
1345
1346 ctx = (struct cdc_ncm_ctx *)dev->data[0];
1347
1348 if (urb->actual_length < sizeof(*event))
1349 return;
1350
1351 /* test for split data in 8-byte chunks */
1352 if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
Bjørn Morkbed6f762013-11-01 11:16:42 +01001353 cdc_ncm_speed_change(dev,
Alexey Orishko84e77a82011-02-07 09:45:10 +00001354 (struct usb_cdc_speed_change *)urb->transfer_buffer);
Alexey Orishko900d4952010-11-29 23:23:28 +00001355 return;
1356 }
1357
1358 event = urb->transfer_buffer;
1359
1360 switch (event->bNotificationType) {
1361 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
1362 /*
1363 * According to the CDC NCM specification ch.7.1
1364 * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be
1365 * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE.
1366 */
Bjørn Morkae223cd2013-11-01 11:16:54 +01001367 netif_info(dev, link, dev->net,
1368 "network connection: %sconnected\n",
Bjørn Morkfa83dbe2014-05-16 21:48:28 +02001369 !!event->wValue ? "" : "dis");
1370 usbnet_link_change(dev, !!event->wValue, 0);
Alexey Orishko900d4952010-11-29 23:23:28 +00001371 break;
1372
1373 case USB_CDC_NOTIFY_SPEED_CHANGE:
Alexey Orishko84e77a82011-02-07 09:45:10 +00001374 if (urb->actual_length < (sizeof(*event) +
1375 sizeof(struct usb_cdc_speed_change)))
Alexey Orishko900d4952010-11-29 23:23:28 +00001376 set_bit(EVENT_STS_SPLIT, &dev->flags);
1377 else
Bjørn Morkbed6f762013-11-01 11:16:42 +01001378 cdc_ncm_speed_change(dev,
1379 (struct usb_cdc_speed_change *)&event[1]);
Alexey Orishko900d4952010-11-29 23:23:28 +00001380 break;
1381
1382 default:
Bjørn Mork67a36602013-04-08 08:26:23 +00001383 dev_dbg(&dev->udev->dev,
1384 "NCM: unexpected notification 0x%02x!\n",
1385 event->bNotificationType);
Alexey Orishko900d4952010-11-29 23:23:28 +00001386 break;
1387 }
1388}
1389
Alexey Orishko900d4952010-11-29 23:23:28 +00001390static const struct driver_info cdc_ncm_info = {
1391 .description = "CDC NCM",
Arnd Bergmannc2613442011-04-01 20:12:02 -07001392 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET,
Alexey Orishko900d4952010-11-29 23:23:28 +00001393 .bind = cdc_ncm_bind,
1394 .unbind = cdc_ncm_unbind,
Oliver Neukuma5e40702012-12-18 04:46:12 +00001395 .manage_power = usbnet_manage_power,
Alexey Orishko900d4952010-11-29 23:23:28 +00001396 .status = cdc_ncm_status,
1397 .rx_fixup = cdc_ncm_rx_fixup,
1398 .tx_fixup = cdc_ncm_tx_fixup,
1399};
1400
Dan Williamsf94898e2012-07-24 08:43:22 +00001401/* Same as cdc_ncm_info, but with FLAG_WWAN */
1402static const struct driver_info wwan_info = {
1403 .description = "Mobile Broadband Network Device",
1404 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
1405 | FLAG_WWAN,
1406 .bind = cdc_ncm_bind,
1407 .unbind = cdc_ncm_unbind,
Oliver Neukuma5e40702012-12-18 04:46:12 +00001408 .manage_power = usbnet_manage_power,
Dan Williamsf94898e2012-07-24 08:43:22 +00001409 .status = cdc_ncm_status,
1410 .rx_fixup = cdc_ncm_rx_fixup,
1411 .tx_fixup = cdc_ncm_tx_fixup,
1412};
1413
Wei Shuai2f62d5a2013-01-21 06:00:32 +00001414/* Same as wwan_info, but with FLAG_NOARP */
1415static const struct driver_info wwan_noarp_info = {
1416 .description = "Mobile Broadband Network Device (NO ARP)",
1417 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
1418 | FLAG_WWAN | FLAG_NOARP,
1419 .bind = cdc_ncm_bind,
1420 .unbind = cdc_ncm_unbind,
Wei Shuai2f62d5a2013-01-21 06:00:32 +00001421 .manage_power = usbnet_manage_power,
1422 .status = cdc_ncm_status,
1423 .rx_fixup = cdc_ncm_rx_fixup,
1424 .tx_fixup = cdc_ncm_tx_fixup,
1425};
1426
Dan Williamsf94898e2012-07-24 08:43:22 +00001427static const struct usb_device_id cdc_devs[] = {
1428 /* Ericsson MBM devices like F5521gw */
1429 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1430 | USB_DEVICE_ID_MATCH_VENDOR,
1431 .idVendor = 0x0bdb,
1432 .bInterfaceClass = USB_CLASS_COMM,
1433 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1434 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1435 .driver_info = (unsigned long) &wwan_info,
1436 },
1437
Peter Meiserf3a1ef92012-08-02 02:30:20 +00001438 /* Dell branded MBM devices like DW5550 */
1439 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1440 | USB_DEVICE_ID_MATCH_VENDOR,
1441 .idVendor = 0x413c,
1442 .bInterfaceClass = USB_CLASS_COMM,
1443 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1444 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1445 .driver_info = (unsigned long) &wwan_info,
1446 },
1447
1448 /* Toshiba branded MBM devices */
1449 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1450 | USB_DEVICE_ID_MATCH_VENDOR,
1451 .idVendor = 0x0930,
1452 .bInterfaceClass = USB_CLASS_COMM,
1453 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1454 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1455 .driver_info = (unsigned long) &wwan_info,
1456 },
1457
Bjørn Mork1f84eab2013-02-27 03:08:58 +00001458 /* tag Huawei devices as wwan */
1459 { USB_VENDOR_AND_INTERFACE_INFO(0x12d1,
1460 USB_CLASS_COMM,
1461 USB_CDC_SUBCLASS_NCM,
1462 USB_CDC_PROTO_NONE),
1463 .driver_info = (unsigned long)&wwan_info,
1464 },
1465
Wei Shuai2f62d5a2013-01-21 06:00:32 +00001466 /* Infineon(now Intel) HSPA Modem platform */
1467 { USB_DEVICE_AND_INTERFACE_INFO(0x1519, 0x0443,
1468 USB_CLASS_COMM,
1469 USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
1470 .driver_info = (unsigned long)&wwan_noarp_info,
1471 },
1472
Dan Williamsf94898e2012-07-24 08:43:22 +00001473 /* Generic CDC-NCM devices */
1474 { USB_INTERFACE_INFO(USB_CLASS_COMM,
1475 USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
1476 .driver_info = (unsigned long)&cdc_ncm_info,
1477 },
1478 {
1479 },
1480};
1481MODULE_DEVICE_TABLE(usb, cdc_devs);
1482
Alexey Orishko900d4952010-11-29 23:23:28 +00001483static struct usb_driver cdc_ncm_driver = {
1484 .name = "cdc_ncm",
1485 .id_table = cdc_devs,
Bjørn Mork085e50e2013-11-01 11:16:50 +01001486 .probe = usbnet_probe,
1487 .disconnect = usbnet_disconnect,
Alexey Orishko900d4952010-11-29 23:23:28 +00001488 .suspend = usbnet_suspend,
1489 .resume = usbnet_resume,
Stefan Metzmacher85e3c652011-06-01 02:01:41 +00001490 .reset_resume = usbnet_resume,
Alexey Orishko900d4952010-11-29 23:23:28 +00001491 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07001492 .disable_hub_initiated_lpm = 1,
Alexey Orishko900d4952010-11-29 23:23:28 +00001493};
1494
Greg Kroah-Hartmand632eb12011-11-18 09:44:20 -08001495module_usb_driver(cdc_ncm_driver);
Alexey Orishko900d4952010-11-29 23:23:28 +00001496
1497MODULE_AUTHOR("Hans Petter Selasky");
1498MODULE_DESCRIPTION("USB CDC NCM host driver");
1499MODULE_LICENSE("Dual BSD/GPL");