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