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