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