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