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