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