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