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