]> Pileus Git - ~andy/linux/blob - drivers/net/usb/cdc_ncm.c
net: cdc_ncm: remove tx_speed and rx_speed fields
[~andy/linux] / drivers / net / usb / cdc_ncm.c
1 /*
2  * cdc_ncm.c
3  *
4  * Copyright (C) ST-Ericsson 2010-2012
5  * Contact: Alexey Orishko <alexey.orishko@stericsson.com>
6  * Original author: Hans Petter Selasky <hans.petter.selasky@stericsson.com>
7  *
8  * USB Host Driver for Network Control Model (NCM)
9  * http://www.usb.org/developers/devclass_docs/NCM10.zip
10  *
11  * The NCM encoding, decoding and initialization logic
12  * derives from FreeBSD 8.x. if_cdce.c and if_cdcereg.h
13  *
14  * This software is available to you under a choice of one of two
15  * licenses. You may choose this file to be licensed under the terms
16  * of the GNU General Public License (GPL) Version 2 or the 2-clause
17  * BSD license listed below:
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions
21  * are met:
22  * 1. Redistributions of source code must retain the above copyright
23  *    notice, this list of conditions and the following disclaimer.
24  * 2. Redistributions in binary form must reproduce the above copyright
25  *    notice, this list of conditions and the following disclaimer in the
26  *    documentation and/or other materials provided with the distribution.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  */
40
41 #include <linux/module.h>
42 #include <linux/init.h>
43 #include <linux/netdevice.h>
44 #include <linux/ctype.h>
45 #include <linux/ethtool.h>
46 #include <linux/workqueue.h>
47 #include <linux/mii.h>
48 #include <linux/crc32.h>
49 #include <linux/usb.h>
50 #include <linux/hrtimer.h>
51 #include <linux/atomic.h>
52 #include <linux/usb/usbnet.h>
53 #include <linux/usb/cdc.h>
54 #include <linux/usb/cdc_ncm.h>
55
56 #define DRIVER_VERSION                          "14-Mar-2012"
57
58 #if IS_ENABLED(CONFIG_USB_NET_CDC_MBIM)
59 static bool prefer_mbim = true;
60 #else
61 static bool prefer_mbim;
62 #endif
63 module_param(prefer_mbim, bool, S_IRUGO | S_IWUSR);
64 MODULE_PARM_DESC(prefer_mbim, "Prefer MBIM setting on dual NCM/MBIM functions");
65
66 static void cdc_ncm_txpath_bh(unsigned long param);
67 static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx);
68 static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *hr_timer);
69 static struct usb_driver cdc_ncm_driver;
70
71 static void
72 cdc_ncm_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
73 {
74         struct usbnet *dev = netdev_priv(net);
75
76         strlcpy(info->driver, dev->driver_name, sizeof(info->driver));
77         strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
78         strlcpy(info->fw_version, dev->driver_info->description,
79                 sizeof(info->fw_version));
80         usb_make_path(dev->udev, info->bus_info, sizeof(info->bus_info));
81 }
82
83 static u8 cdc_ncm_setup(struct usbnet *dev)
84 {
85         struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
86         u32 val;
87         u8 flags;
88         u8 iface_no;
89         int err;
90         int eth_hlen;
91         u16 ntb_fmt_supported;
92         u32 min_dgram_size;
93         u32 min_hdr_size;
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 (dev->net->mtu != (ctx->max_datagram_size - eth_hlen))
289                 dev->net->mtu = ctx->max_datagram_size - eth_hlen;
290
291         return 0;
292 }
293
294 static void
295 cdc_ncm_find_endpoints(struct usbnet *dev, struct usb_interface *intf)
296 {
297         struct usb_host_endpoint *e, *in = NULL, *out = NULL;
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 (!dev->status)
307                                         dev->status = e;
308                         }
309                         break;
310
311                 case USB_ENDPOINT_XFER_BULK:
312                         if (usb_endpoint_dir_in(&e->desc)) {
313                                 if (!in)
314                                         in = e;
315                         } else {
316                                 if (!out)
317                                         out = e;
318                         }
319                         break;
320
321                 default:
322                         break;
323                 }
324         }
325         if (in && !dev->in)
326                 dev->in = usb_rcvbulkpipe(dev->udev,
327                                           in->desc.bEndpointAddress &
328                                           USB_ENDPOINT_NUMBER_MASK);
329         if (out && !dev->out)
330                 dev->out = usb_sndbulkpipe(dev->udev,
331                                            out->desc.bEndpointAddress &
332                                            USB_ENDPOINT_NUMBER_MASK);
333 }
334
335 static void cdc_ncm_free(struct cdc_ncm_ctx *ctx)
336 {
337         if (ctx == NULL)
338                 return;
339
340         if (ctx->tx_rem_skb != NULL) {
341                 dev_kfree_skb_any(ctx->tx_rem_skb);
342                 ctx->tx_rem_skb = NULL;
343         }
344
345         if (ctx->tx_curr_skb != NULL) {
346                 dev_kfree_skb_any(ctx->tx_curr_skb);
347                 ctx->tx_curr_skb = NULL;
348         }
349
350         kfree(ctx);
351 }
352
353 static const struct ethtool_ops cdc_ncm_ethtool_ops = {
354         .get_drvinfo = cdc_ncm_get_drvinfo,
355         .get_link = usbnet_get_link,
356         .get_msglevel = usbnet_get_msglevel,
357         .set_msglevel = usbnet_set_msglevel,
358         .get_settings = usbnet_get_settings,
359         .set_settings = usbnet_set_settings,
360         .nway_reset = usbnet_nway_reset,
361 };
362
363 int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_altsetting)
364 {
365         struct cdc_ncm_ctx *ctx;
366         struct usb_driver *driver;
367         u8 *buf;
368         int len;
369         int temp;
370         u8 iface_no;
371
372         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
373         if (!ctx)
374                 return -ENOMEM;
375
376         hrtimer_init(&ctx->tx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
377         ctx->tx_timer.function = &cdc_ncm_tx_timer_cb;
378         ctx->bh.data = (unsigned long)dev;
379         ctx->bh.func = cdc_ncm_txpath_bh;
380         atomic_set(&ctx->stop, 0);
381         spin_lock_init(&ctx->mtx);
382
383         /* store ctx pointer in device data field */
384         dev->data[0] = (unsigned long)ctx;
385
386         /* get some pointers */
387         driver = driver_of(intf);
388         buf = intf->cur_altsetting->extra;
389         len = intf->cur_altsetting->extralen;
390
391         /* parse through descriptors associated with control interface */
392         while ((len > 0) && (buf[0] > 2) && (buf[0] <= len)) {
393
394                 if (buf[1] != USB_DT_CS_INTERFACE)
395                         goto advance;
396
397                 switch (buf[2]) {
398                 case USB_CDC_UNION_TYPE:
399                         if (buf[0] < sizeof(*(ctx->union_desc)))
400                                 break;
401
402                         ctx->union_desc =
403                                         (const struct usb_cdc_union_desc *)buf;
404
405                         ctx->control = usb_ifnum_to_if(dev->udev,
406                                         ctx->union_desc->bMasterInterface0);
407                         ctx->data = usb_ifnum_to_if(dev->udev,
408                                         ctx->union_desc->bSlaveInterface0);
409                         break;
410
411                 case USB_CDC_ETHERNET_TYPE:
412                         if (buf[0] < sizeof(*(ctx->ether_desc)))
413                                 break;
414
415                         ctx->ether_desc =
416                                         (const struct usb_cdc_ether_desc *)buf;
417                         dev->hard_mtu =
418                                 le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
419
420                         if (dev->hard_mtu < CDC_NCM_MIN_DATAGRAM_SIZE)
421                                 dev->hard_mtu = CDC_NCM_MIN_DATAGRAM_SIZE;
422                         else if (dev->hard_mtu > CDC_NCM_MAX_DATAGRAM_SIZE)
423                                 dev->hard_mtu = CDC_NCM_MAX_DATAGRAM_SIZE;
424                         break;
425
426                 case USB_CDC_NCM_TYPE:
427                         if (buf[0] < sizeof(*(ctx->func_desc)))
428                                 break;
429
430                         ctx->func_desc = (const struct usb_cdc_ncm_desc *)buf;
431                         break;
432
433                 case USB_CDC_MBIM_TYPE:
434                         if (buf[0] < sizeof(*(ctx->mbim_desc)))
435                                 break;
436
437                         ctx->mbim_desc = (const struct usb_cdc_mbim_desc *)buf;
438                         break;
439
440                 default:
441                         break;
442                 }
443 advance:
444                 /* advance to next descriptor */
445                 temp = buf[0];
446                 buf += temp;
447                 len -= temp;
448         }
449
450         /* some buggy devices have an IAD but no CDC Union */
451         if (!ctx->union_desc && intf->intf_assoc && intf->intf_assoc->bInterfaceCount == 2) {
452                 ctx->control = intf;
453                 ctx->data = usb_ifnum_to_if(dev->udev, intf->cur_altsetting->desc.bInterfaceNumber + 1);
454                 dev_dbg(&intf->dev, "CDC Union missing - got slave from IAD\n");
455         }
456
457         /* check if we got everything */
458         if ((ctx->control == NULL) || (ctx->data == NULL) ||
459             ((!ctx->mbim_desc) && ((ctx->ether_desc == NULL) || (ctx->control != intf))))
460                 goto error;
461
462         /* claim data interface, if different from control */
463         if (ctx->data != ctx->control) {
464                 temp = usb_driver_claim_interface(driver, ctx->data, dev);
465                 if (temp)
466                         goto error;
467         }
468
469         iface_no = ctx->data->cur_altsetting->desc.bInterfaceNumber;
470
471         /* reset data interface */
472         temp = usb_set_interface(dev->udev, iface_no, 0);
473         if (temp)
474                 goto error2;
475
476         /* initialize data interface */
477         if (cdc_ncm_setup(dev))
478                 goto error2;
479
480         /* configure data interface */
481         temp = usb_set_interface(dev->udev, iface_no, data_altsetting);
482         if (temp)
483                 goto error2;
484
485         cdc_ncm_find_endpoints(dev, ctx->data);
486         cdc_ncm_find_endpoints(dev, ctx->control);
487         if (!dev->in || !dev->out || !dev->status)
488                 goto error2;
489
490         dev->net->ethtool_ops = &cdc_ncm_ethtool_ops;
491
492         usb_set_intfdata(ctx->data, dev);
493         usb_set_intfdata(ctx->control, dev);
494
495         if (ctx->ether_desc) {
496                 temp = usbnet_get_ethernet_addr(dev, ctx->ether_desc->iMACAddress);
497                 if (temp)
498                         goto error2;
499                 dev_info(&dev->udev->dev, "MAC-Address: %pM\n", dev->net->dev_addr);
500         }
501
502         dev->rx_urb_size = ctx->rx_max;
503
504         /* cdc_ncm_setup will override dwNtbOutMaxSize if it is
505          * outside the sane range. Adding a pad byte here if necessary
506          * simplifies the handling in cdc_ncm_fill_tx_frame, making
507          * tx_max always represent the real skb max size.
508          */
509         if (ctx->tx_max != le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize) &&
510             ctx->tx_max % usb_maxpacket(dev->udev, dev->out, 1) == 0)
511                 ctx->tx_max++;
512
513         return 0;
514
515 error2:
516         usb_set_intfdata(ctx->control, NULL);
517         usb_set_intfdata(ctx->data, NULL);
518         if (ctx->data != ctx->control)
519                 usb_driver_release_interface(driver, ctx->data);
520 error:
521         cdc_ncm_free((struct cdc_ncm_ctx *)dev->data[0]);
522         dev->data[0] = 0;
523         dev_info(&dev->udev->dev, "bind() failure\n");
524         return -ENODEV;
525 }
526 EXPORT_SYMBOL_GPL(cdc_ncm_bind_common);
527
528 void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf)
529 {
530         struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
531         struct usb_driver *driver = driver_of(intf);
532
533         if (ctx == NULL)
534                 return;         /* no setup */
535
536         atomic_set(&ctx->stop, 1);
537
538         if (hrtimer_active(&ctx->tx_timer))
539                 hrtimer_cancel(&ctx->tx_timer);
540
541         tasklet_kill(&ctx->bh);
542
543         /* handle devices with combined control and data interface */
544         if (ctx->control == ctx->data)
545                 ctx->data = NULL;
546
547         /* disconnect master --> disconnect slave */
548         if (intf == ctx->control && ctx->data) {
549                 usb_set_intfdata(ctx->data, NULL);
550                 usb_driver_release_interface(driver, ctx->data);
551                 ctx->data = NULL;
552
553         } else if (intf == ctx->data && ctx->control) {
554                 usb_set_intfdata(ctx->control, NULL);
555                 usb_driver_release_interface(driver, ctx->control);
556                 ctx->control = NULL;
557         }
558
559         usb_set_intfdata(intf, NULL);
560         cdc_ncm_free(ctx);
561 }
562 EXPORT_SYMBOL_GPL(cdc_ncm_unbind);
563
564 /* Select the MBIM altsetting iff it is preferred and available,
565  * returning the number of the corresponding data interface altsetting
566  */
567 u8 cdc_ncm_select_altsetting(struct usbnet *dev, struct usb_interface *intf)
568 {
569         struct usb_host_interface *alt;
570
571         /* The MBIM spec defines a NCM compatible default altsetting,
572          * which we may have matched:
573          *
574          *  "Functions that implement both NCM 1.0 and MBIM (an
575          *   “NCM/MBIM function”) according to this recommendation
576          *   shall provide two alternate settings for the
577          *   Communication Interface.  Alternate setting 0, and the
578          *   associated class and endpoint descriptors, shall be
579          *   constructed according to the rules given for the
580          *   Communication Interface in section 5 of [USBNCM10].
581          *   Alternate setting 1, and the associated class and
582          *   endpoint descriptors, shall be constructed according to
583          *   the rules given in section 6 (USB Device Model) of this
584          *   specification."
585          */
586         if (prefer_mbim && intf->num_altsetting == 2) {
587                 alt = usb_altnum_to_altsetting(intf, CDC_NCM_COMM_ALTSETTING_MBIM);
588                 if (alt && cdc_ncm_comm_intf_is_mbim(alt) &&
589                     !usb_set_interface(dev->udev,
590                                        intf->cur_altsetting->desc.bInterfaceNumber,
591                                        CDC_NCM_COMM_ALTSETTING_MBIM))
592                         return CDC_NCM_DATA_ALTSETTING_MBIM;
593         }
594         return CDC_NCM_DATA_ALTSETTING_NCM;
595 }
596 EXPORT_SYMBOL_GPL(cdc_ncm_select_altsetting);
597
598 static int cdc_ncm_bind(struct usbnet *dev, struct usb_interface *intf)
599 {
600         int ret;
601
602         /* MBIM backwards compatible function? */
603         cdc_ncm_select_altsetting(dev, intf);
604         if (cdc_ncm_comm_intf_is_mbim(intf->cur_altsetting))
605                 return -ENODEV;
606
607         /* NCM data altsetting is always 1 */
608         ret = cdc_ncm_bind_common(dev, intf, 1);
609
610         /*
611          * We should get an event when network connection is "connected" or
612          * "disconnected". Set network connection in "disconnected" state
613          * (carrier is OFF) during attach, so the IP network stack does not
614          * start IPv6 negotiation and more.
615          */
616         usbnet_link_change(dev, 0, 0);
617         return ret;
618 }
619
620 static void cdc_ncm_align_tail(struct sk_buff *skb, size_t modulus, size_t remainder, size_t max)
621 {
622         size_t align = ALIGN(skb->len, modulus) - skb->len + remainder;
623
624         if (skb->len + align > max)
625                 align = max - skb->len;
626         if (align && skb_tailroom(skb) >= align)
627                 memset(skb_put(skb, align), 0, align);
628 }
629
630 /* return a pointer to a valid struct usb_cdc_ncm_ndp16 of type sign, possibly
631  * allocating a new one within skb
632  */
633 static struct usb_cdc_ncm_ndp16 *cdc_ncm_ndp(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign, size_t reserve)
634 {
635         struct usb_cdc_ncm_ndp16 *ndp16 = NULL;
636         struct usb_cdc_ncm_nth16 *nth16 = (void *)skb->data;
637         size_t ndpoffset = le16_to_cpu(nth16->wNdpIndex);
638
639         /* follow the chain of NDPs, looking for a match */
640         while (ndpoffset) {
641                 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb->data + ndpoffset);
642                 if  (ndp16->dwSignature == sign)
643                         return ndp16;
644                 ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
645         }
646
647         /* align new NDP */
648         cdc_ncm_align_tail(skb, ctx->tx_ndp_modulus, 0, ctx->tx_max);
649
650         /* verify that there is room for the NDP and the datagram (reserve) */
651         if ((ctx->tx_max - skb->len - reserve) < CDC_NCM_NDP_SIZE)
652                 return NULL;
653
654         /* link to it */
655         if (ndp16)
656                 ndp16->wNextNdpIndex = cpu_to_le16(skb->len);
657         else
658                 nth16->wNdpIndex = cpu_to_le16(skb->len);
659
660         /* push a new empty NDP */
661         ndp16 = (struct usb_cdc_ncm_ndp16 *)memset(skb_put(skb, CDC_NCM_NDP_SIZE), 0, CDC_NCM_NDP_SIZE);
662         ndp16->dwSignature = sign;
663         ndp16->wLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_ndp16) + sizeof(struct usb_cdc_ncm_dpe16));
664         return ndp16;
665 }
666
667 struct sk_buff *
668 cdc_ncm_fill_tx_frame(struct usbnet *dev, struct sk_buff *skb, __le32 sign)
669 {
670         struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
671         struct usb_cdc_ncm_nth16 *nth16;
672         struct usb_cdc_ncm_ndp16 *ndp16;
673         struct sk_buff *skb_out;
674         u16 n = 0, index, ndplen;
675         u8 ready2send = 0;
676
677         /* if there is a remaining skb, it gets priority */
678         if (skb != NULL) {
679                 swap(skb, ctx->tx_rem_skb);
680                 swap(sign, ctx->tx_rem_sign);
681         } else {
682                 ready2send = 1;
683         }
684
685         /* check if we are resuming an OUT skb */
686         skb_out = ctx->tx_curr_skb;
687
688         /* allocate a new OUT skb */
689         if (!skb_out) {
690                 skb_out = alloc_skb(ctx->tx_max, GFP_ATOMIC);
691                 if (skb_out == NULL) {
692                         if (skb != NULL) {
693                                 dev_kfree_skb_any(skb);
694                                 dev->net->stats.tx_dropped++;
695                         }
696                         goto exit_no_skb;
697                 }
698                 /* fill out the initial 16-bit NTB header */
699                 nth16 = (struct usb_cdc_ncm_nth16 *)memset(skb_put(skb_out, sizeof(struct usb_cdc_ncm_nth16)), 0, sizeof(struct usb_cdc_ncm_nth16));
700                 nth16->dwSignature = cpu_to_le32(USB_CDC_NCM_NTH16_SIGN);
701                 nth16->wHeaderLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_nth16));
702                 nth16->wSequence = cpu_to_le16(ctx->tx_seq++);
703
704                 /* count total number of frames in this NTB */
705                 ctx->tx_curr_frame_num = 0;
706         }
707
708         for (n = ctx->tx_curr_frame_num; n < ctx->tx_max_datagrams; n++) {
709                 /* send any remaining skb first */
710                 if (skb == NULL) {
711                         skb = ctx->tx_rem_skb;
712                         sign = ctx->tx_rem_sign;
713                         ctx->tx_rem_skb = NULL;
714
715                         /* check for end of skb */
716                         if (skb == NULL)
717                                 break;
718                 }
719
720                 /* get the appropriate NDP for this skb */
721                 ndp16 = cdc_ncm_ndp(ctx, skb_out, sign, skb->len + ctx->tx_modulus + ctx->tx_remainder);
722
723                 /* align beginning of next frame */
724                 cdc_ncm_align_tail(skb_out,  ctx->tx_modulus, ctx->tx_remainder, ctx->tx_max);
725
726                 /* check if we had enough room left for both NDP and frame */
727                 if (!ndp16 || skb_out->len + skb->len > ctx->tx_max) {
728                         if (n == 0) {
729                                 /* won't fit, MTU problem? */
730                                 dev_kfree_skb_any(skb);
731                                 skb = NULL;
732                                 dev->net->stats.tx_dropped++;
733                         } else {
734                                 /* no room for skb - store for later */
735                                 if (ctx->tx_rem_skb != NULL) {
736                                         dev_kfree_skb_any(ctx->tx_rem_skb);
737                                         dev->net->stats.tx_dropped++;
738                                 }
739                                 ctx->tx_rem_skb = skb;
740                                 ctx->tx_rem_sign = sign;
741                                 skb = NULL;
742                                 ready2send = 1;
743                         }
744                         break;
745                 }
746
747                 /* calculate frame number withing this NDP */
748                 ndplen = le16_to_cpu(ndp16->wLength);
749                 index = (ndplen - sizeof(struct usb_cdc_ncm_ndp16)) / sizeof(struct usb_cdc_ncm_dpe16) - 1;
750
751                 /* OK, add this skb */
752                 ndp16->dpe16[index].wDatagramLength = cpu_to_le16(skb->len);
753                 ndp16->dpe16[index].wDatagramIndex = cpu_to_le16(skb_out->len);
754                 ndp16->wLength = cpu_to_le16(ndplen + sizeof(struct usb_cdc_ncm_dpe16));
755                 memcpy(skb_put(skb_out, skb->len), skb->data, skb->len);
756                 dev_kfree_skb_any(skb);
757                 skb = NULL;
758
759                 /* send now if this NDP is full */
760                 if (index >= CDC_NCM_DPT_DATAGRAMS_MAX) {
761                         ready2send = 1;
762                         break;
763                 }
764         }
765
766         /* free up any dangling skb */
767         if (skb != NULL) {
768                 dev_kfree_skb_any(skb);
769                 skb = NULL;
770                 dev->net->stats.tx_dropped++;
771         }
772
773         ctx->tx_curr_frame_num = n;
774
775         if (n == 0) {
776                 /* wait for more frames */
777                 /* push variables */
778                 ctx->tx_curr_skb = skb_out;
779                 goto exit_no_skb;
780
781         } else if ((n < ctx->tx_max_datagrams) && (ready2send == 0)) {
782                 /* wait for more frames */
783                 /* push variables */
784                 ctx->tx_curr_skb = skb_out;
785                 /* set the pending count */
786                 if (n < CDC_NCM_RESTART_TIMER_DATAGRAM_CNT)
787                         ctx->tx_timer_pending = CDC_NCM_TIMER_PENDING_CNT;
788                 goto exit_no_skb;
789
790         } else {
791                 /* frame goes out */
792                 /* variables will be reset at next call */
793         }
794
795         /* If collected data size is less or equal CDC_NCM_MIN_TX_PKT
796          * bytes, we send buffers as it is. If we get more data, it
797          * would be more efficient for USB HS mobile device with DMA
798          * engine to receive a full size NTB, than canceling DMA
799          * transfer and receiving a short packet.
800          */
801         if (skb_out->len > CDC_NCM_MIN_TX_PKT)
802                 memset(skb_put(skb_out, ctx->tx_max - skb_out->len), 0,
803                        ctx->tx_max - skb_out->len);
804         else if ((skb_out->len % dev->maxpacket) == 0)
805                 *skb_put(skb_out, 1) = 0;       /* force short packet */
806
807         /* set final frame length */
808         nth16 = (struct usb_cdc_ncm_nth16 *)skb_out->data;
809         nth16->wBlockLength = cpu_to_le16(skb_out->len);
810
811         /* return skb */
812         ctx->tx_curr_skb = NULL;
813         dev->net->stats.tx_packets += ctx->tx_curr_frame_num;
814         return skb_out;
815
816 exit_no_skb:
817         /* Start timer, if there is a remaining skb */
818         if (ctx->tx_curr_skb != NULL)
819                 cdc_ncm_tx_timeout_start(ctx);
820         return NULL;
821 }
822 EXPORT_SYMBOL_GPL(cdc_ncm_fill_tx_frame);
823
824 static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx)
825 {
826         /* start timer, if not already started */
827         if (!(hrtimer_active(&ctx->tx_timer) || atomic_read(&ctx->stop)))
828                 hrtimer_start(&ctx->tx_timer,
829                                 ktime_set(0, CDC_NCM_TIMER_INTERVAL),
830                                 HRTIMER_MODE_REL);
831 }
832
833 static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *timer)
834 {
835         struct cdc_ncm_ctx *ctx =
836                         container_of(timer, struct cdc_ncm_ctx, tx_timer);
837
838         if (!atomic_read(&ctx->stop))
839                 tasklet_schedule(&ctx->bh);
840         return HRTIMER_NORESTART;
841 }
842
843 static void cdc_ncm_txpath_bh(unsigned long param)
844 {
845         struct usbnet *dev = (struct usbnet *)param;
846         struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
847
848         spin_lock_bh(&ctx->mtx);
849         if (ctx->tx_timer_pending != 0) {
850                 ctx->tx_timer_pending--;
851                 cdc_ncm_tx_timeout_start(ctx);
852                 spin_unlock_bh(&ctx->mtx);
853         } else if (dev->net != NULL) {
854                 spin_unlock_bh(&ctx->mtx);
855                 netif_tx_lock_bh(dev->net);
856                 usbnet_start_xmit(NULL, dev->net);
857                 netif_tx_unlock_bh(dev->net);
858         } else {
859                 spin_unlock_bh(&ctx->mtx);
860         }
861 }
862
863 static struct sk_buff *
864 cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
865 {
866         struct sk_buff *skb_out;
867         struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
868
869         /*
870          * The Ethernet API we are using does not support transmitting
871          * multiple Ethernet frames in a single call. This driver will
872          * accumulate multiple Ethernet frames and send out a larger
873          * USB frame when the USB buffer is full or when a single jiffies
874          * timeout happens.
875          */
876         if (ctx == NULL)
877                 goto error;
878
879         spin_lock_bh(&ctx->mtx);
880         skb_out = cdc_ncm_fill_tx_frame(dev, skb, cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN));
881         spin_unlock_bh(&ctx->mtx);
882         return skb_out;
883
884 error:
885         if (skb != NULL)
886                 dev_kfree_skb_any(skb);
887
888         return NULL;
889 }
890
891 /* verify NTB header and return offset of first NDP, or negative error */
892 int cdc_ncm_rx_verify_nth16(struct cdc_ncm_ctx *ctx, struct sk_buff *skb_in)
893 {
894         struct usb_cdc_ncm_nth16 *nth16;
895         int len;
896         int ret = -EINVAL;
897
898         if (ctx == NULL)
899                 goto error;
900
901         if (skb_in->len < (sizeof(struct usb_cdc_ncm_nth16) +
902                                         sizeof(struct usb_cdc_ncm_ndp16))) {
903                 pr_debug("frame too short\n");
904                 goto error;
905         }
906
907         nth16 = (struct usb_cdc_ncm_nth16 *)skb_in->data;
908
909         if (le32_to_cpu(nth16->dwSignature) != USB_CDC_NCM_NTH16_SIGN) {
910                 pr_debug("invalid NTH16 signature <%u>\n",
911                                         le32_to_cpu(nth16->dwSignature));
912                 goto error;
913         }
914
915         len = le16_to_cpu(nth16->wBlockLength);
916         if (len > ctx->rx_max) {
917                 pr_debug("unsupported NTB block length %u/%u\n", len,
918                                                                 ctx->rx_max);
919                 goto error;
920         }
921
922         if ((ctx->rx_seq + 1) != le16_to_cpu(nth16->wSequence) &&
923                 (ctx->rx_seq || le16_to_cpu(nth16->wSequence)) &&
924                 !((ctx->rx_seq == 0xffff) && !le16_to_cpu(nth16->wSequence))) {
925                 pr_debug("sequence number glitch prev=%d curr=%d\n",
926                                 ctx->rx_seq, le16_to_cpu(nth16->wSequence));
927         }
928         ctx->rx_seq = le16_to_cpu(nth16->wSequence);
929
930         ret = le16_to_cpu(nth16->wNdpIndex);
931 error:
932         return ret;
933 }
934 EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_nth16);
935
936 /* verify NDP header and return number of datagrams, or negative error */
937 int cdc_ncm_rx_verify_ndp16(struct sk_buff *skb_in, int ndpoffset)
938 {
939         struct usb_cdc_ncm_ndp16 *ndp16;
940         int ret = -EINVAL;
941
942         if ((ndpoffset + sizeof(struct usb_cdc_ncm_ndp16)) > skb_in->len) {
943                 pr_debug("invalid NDP offset  <%u>\n", ndpoffset);
944                 goto error;
945         }
946         ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
947
948         if (le16_to_cpu(ndp16->wLength) < USB_CDC_NCM_NDP16_LENGTH_MIN) {
949                 pr_debug("invalid DPT16 length <%u>\n",
950                                         le32_to_cpu(ndp16->dwSignature));
951                 goto error;
952         }
953
954         ret = ((le16_to_cpu(ndp16->wLength) -
955                                         sizeof(struct usb_cdc_ncm_ndp16)) /
956                                         sizeof(struct usb_cdc_ncm_dpe16));
957         ret--; /* we process NDP entries except for the last one */
958
959         if ((sizeof(struct usb_cdc_ncm_ndp16) + ret * (sizeof(struct usb_cdc_ncm_dpe16))) >
960                                                                 skb_in->len) {
961                 pr_debug("Invalid nframes = %d\n", ret);
962                 ret = -EINVAL;
963         }
964
965 error:
966         return ret;
967 }
968 EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_ndp16);
969
970 static int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
971 {
972         struct sk_buff *skb;
973         struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
974         int len;
975         int nframes;
976         int x;
977         int offset;
978         struct usb_cdc_ncm_ndp16 *ndp16;
979         struct usb_cdc_ncm_dpe16 *dpe16;
980         int ndpoffset;
981         int loopcount = 50; /* arbitrary max preventing infinite loop */
982
983         ndpoffset = cdc_ncm_rx_verify_nth16(ctx, skb_in);
984         if (ndpoffset < 0)
985                 goto error;
986
987 next_ndp:
988         nframes = cdc_ncm_rx_verify_ndp16(skb_in, ndpoffset);
989         if (nframes < 0)
990                 goto error;
991
992         ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
993
994         if (le32_to_cpu(ndp16->dwSignature) != USB_CDC_NCM_NDP16_NOCRC_SIGN) {
995                 pr_debug("invalid DPT16 signature <%u>\n",
996                          le32_to_cpu(ndp16->dwSignature));
997                 goto err_ndp;
998         }
999         dpe16 = ndp16->dpe16;
1000
1001         for (x = 0; x < nframes; x++, dpe16++) {
1002                 offset = le16_to_cpu(dpe16->wDatagramIndex);
1003                 len = le16_to_cpu(dpe16->wDatagramLength);
1004
1005                 /*
1006                  * CDC NCM ch. 3.7
1007                  * All entries after first NULL entry are to be ignored
1008                  */
1009                 if ((offset == 0) || (len == 0)) {
1010                         if (!x)
1011                                 goto err_ndp; /* empty NTB */
1012                         break;
1013                 }
1014
1015                 /* sanity checking */
1016                 if (((offset + len) > skb_in->len) ||
1017                                 (len > ctx->rx_max) || (len < ETH_HLEN)) {
1018                         pr_debug("invalid frame detected (ignored)"
1019                                         "offset[%u]=%u, length=%u, skb=%p\n",
1020                                         x, offset, len, skb_in);
1021                         if (!x)
1022                                 goto err_ndp;
1023                         break;
1024
1025                 } else {
1026                         skb = skb_clone(skb_in, GFP_ATOMIC);
1027                         if (!skb)
1028                                 goto error;
1029                         skb->len = len;
1030                         skb->data = ((u8 *)skb_in->data) + offset;
1031                         skb_set_tail_pointer(skb, len);
1032                         usbnet_skb_return(dev, skb);
1033                 }
1034         }
1035 err_ndp:
1036         /* are there more NDPs to process? */
1037         ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
1038         if (ndpoffset && loopcount--)
1039                 goto next_ndp;
1040
1041         return 1;
1042 error:
1043         return 0;
1044 }
1045
1046 static void
1047 cdc_ncm_speed_change(struct usbnet *dev,
1048                      struct usb_cdc_speed_change *data)
1049 {
1050         uint32_t rx_speed = le32_to_cpu(data->DLBitRRate);
1051         uint32_t tx_speed = le32_to_cpu(data->ULBitRate);
1052
1053         /*
1054          * Currently the USB-NET API does not support reporting the actual
1055          * device speed. Do print it instead.
1056          */
1057         if ((tx_speed > 1000000) && (rx_speed > 1000000)) {
1058                 printk(KERN_INFO KBUILD_MODNAME
1059                        ": %s: %u mbit/s downlink "
1060                        "%u mbit/s uplink\n",
1061                        dev->net->name,
1062                        (unsigned int)(rx_speed / 1000000U),
1063                        (unsigned int)(tx_speed / 1000000U));
1064         } else {
1065                 printk(KERN_INFO KBUILD_MODNAME
1066                        ": %s: %u kbit/s downlink "
1067                        "%u kbit/s uplink\n",
1068                        dev->net->name,
1069                        (unsigned int)(rx_speed / 1000U),
1070                        (unsigned int)(tx_speed / 1000U));
1071         }
1072 }
1073
1074 static void cdc_ncm_status(struct usbnet *dev, struct urb *urb)
1075 {
1076         struct cdc_ncm_ctx *ctx;
1077         struct usb_cdc_notification *event;
1078
1079         ctx = (struct cdc_ncm_ctx *)dev->data[0];
1080
1081         if (urb->actual_length < sizeof(*event))
1082                 return;
1083
1084         /* test for split data in 8-byte chunks */
1085         if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
1086                 cdc_ncm_speed_change(dev,
1087                       (struct usb_cdc_speed_change *)urb->transfer_buffer);
1088                 return;
1089         }
1090
1091         event = urb->transfer_buffer;
1092
1093         switch (event->bNotificationType) {
1094         case USB_CDC_NOTIFY_NETWORK_CONNECTION:
1095                 /*
1096                  * According to the CDC NCM specification ch.7.1
1097                  * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be
1098                  * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE.
1099                  */
1100                 ctx->connected = le16_to_cpu(event->wValue);
1101
1102                 printk(KERN_INFO KBUILD_MODNAME ": %s: network connection:"
1103                        " %sconnected\n",
1104                        dev->net->name, ctx->connected ? "" : "dis");
1105
1106                 usbnet_link_change(dev, ctx->connected, 0);
1107                 break;
1108
1109         case USB_CDC_NOTIFY_SPEED_CHANGE:
1110                 if (urb->actual_length < (sizeof(*event) +
1111                                         sizeof(struct usb_cdc_speed_change)))
1112                         set_bit(EVENT_STS_SPLIT, &dev->flags);
1113                 else
1114                         cdc_ncm_speed_change(dev,
1115                                              (struct usb_cdc_speed_change *)&event[1]);
1116                 break;
1117
1118         default:
1119                 dev_dbg(&dev->udev->dev,
1120                         "NCM: unexpected notification 0x%02x!\n",
1121                         event->bNotificationType);
1122                 break;
1123         }
1124 }
1125
1126 static int cdc_ncm_check_connect(struct usbnet *dev)
1127 {
1128         struct cdc_ncm_ctx *ctx;
1129
1130         ctx = (struct cdc_ncm_ctx *)dev->data[0];
1131         if (ctx == NULL)
1132                 return 1;       /* disconnected */
1133
1134         return !ctx->connected;
1135 }
1136
1137 static int
1138 cdc_ncm_probe(struct usb_interface *udev, const struct usb_device_id *prod)
1139 {
1140         return usbnet_probe(udev, prod);
1141 }
1142
1143 static void cdc_ncm_disconnect(struct usb_interface *intf)
1144 {
1145         struct usbnet *dev = usb_get_intfdata(intf);
1146
1147         if (dev == NULL)
1148                 return;         /* already disconnected */
1149
1150         usbnet_disconnect(intf);
1151 }
1152
1153 static const struct driver_info cdc_ncm_info = {
1154         .description = "CDC NCM",
1155         .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET,
1156         .bind = cdc_ncm_bind,
1157         .unbind = cdc_ncm_unbind,
1158         .check_connect = cdc_ncm_check_connect,
1159         .manage_power = usbnet_manage_power,
1160         .status = cdc_ncm_status,
1161         .rx_fixup = cdc_ncm_rx_fixup,
1162         .tx_fixup = cdc_ncm_tx_fixup,
1163 };
1164
1165 /* Same as cdc_ncm_info, but with FLAG_WWAN */
1166 static const struct driver_info wwan_info = {
1167         .description = "Mobile Broadband Network Device",
1168         .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
1169                         | FLAG_WWAN,
1170         .bind = cdc_ncm_bind,
1171         .unbind = cdc_ncm_unbind,
1172         .check_connect = cdc_ncm_check_connect,
1173         .manage_power = usbnet_manage_power,
1174         .status = cdc_ncm_status,
1175         .rx_fixup = cdc_ncm_rx_fixup,
1176         .tx_fixup = cdc_ncm_tx_fixup,
1177 };
1178
1179 /* Same as wwan_info, but with FLAG_NOARP  */
1180 static const struct driver_info wwan_noarp_info = {
1181         .description = "Mobile Broadband Network Device (NO ARP)",
1182         .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
1183                         | FLAG_WWAN | FLAG_NOARP,
1184         .bind = cdc_ncm_bind,
1185         .unbind = cdc_ncm_unbind,
1186         .check_connect = cdc_ncm_check_connect,
1187         .manage_power = usbnet_manage_power,
1188         .status = cdc_ncm_status,
1189         .rx_fixup = cdc_ncm_rx_fixup,
1190         .tx_fixup = cdc_ncm_tx_fixup,
1191 };
1192
1193 static const struct usb_device_id cdc_devs[] = {
1194         /* Ericsson MBM devices like F5521gw */
1195         { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1196                 | USB_DEVICE_ID_MATCH_VENDOR,
1197           .idVendor = 0x0bdb,
1198           .bInterfaceClass = USB_CLASS_COMM,
1199           .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1200           .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1201           .driver_info = (unsigned long) &wwan_info,
1202         },
1203
1204         /* Dell branded MBM devices like DW5550 */
1205         { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1206                 | USB_DEVICE_ID_MATCH_VENDOR,
1207           .idVendor = 0x413c,
1208           .bInterfaceClass = USB_CLASS_COMM,
1209           .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1210           .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1211           .driver_info = (unsigned long) &wwan_info,
1212         },
1213
1214         /* Toshiba branded MBM devices */
1215         { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1216                 | USB_DEVICE_ID_MATCH_VENDOR,
1217           .idVendor = 0x0930,
1218           .bInterfaceClass = USB_CLASS_COMM,
1219           .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1220           .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1221           .driver_info = (unsigned long) &wwan_info,
1222         },
1223
1224         /* tag Huawei devices as wwan */
1225         { USB_VENDOR_AND_INTERFACE_INFO(0x12d1,
1226                                         USB_CLASS_COMM,
1227                                         USB_CDC_SUBCLASS_NCM,
1228                                         USB_CDC_PROTO_NONE),
1229           .driver_info = (unsigned long)&wwan_info,
1230         },
1231
1232         /* Huawei NCM devices disguised as vendor specific */
1233         { USB_VENDOR_AND_INTERFACE_INFO(0x12d1, 0xff, 0x02, 0x16),
1234           .driver_info = (unsigned long)&wwan_info,
1235         },
1236         { USB_VENDOR_AND_INTERFACE_INFO(0x12d1, 0xff, 0x02, 0x46),
1237           .driver_info = (unsigned long)&wwan_info,
1238         },
1239         { USB_VENDOR_AND_INTERFACE_INFO(0x12d1, 0xff, 0x02, 0x76),
1240           .driver_info = (unsigned long)&wwan_info,
1241         },
1242
1243         /* Infineon(now Intel) HSPA Modem platform */
1244         { USB_DEVICE_AND_INTERFACE_INFO(0x1519, 0x0443,
1245                 USB_CLASS_COMM,
1246                 USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
1247           .driver_info = (unsigned long)&wwan_noarp_info,
1248         },
1249
1250         /* Generic CDC-NCM devices */
1251         { USB_INTERFACE_INFO(USB_CLASS_COMM,
1252                 USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
1253                 .driver_info = (unsigned long)&cdc_ncm_info,
1254         },
1255         {
1256         },
1257 };
1258 MODULE_DEVICE_TABLE(usb, cdc_devs);
1259
1260 static struct usb_driver cdc_ncm_driver = {
1261         .name = "cdc_ncm",
1262         .id_table = cdc_devs,
1263         .probe = cdc_ncm_probe,
1264         .disconnect = cdc_ncm_disconnect,
1265         .suspend = usbnet_suspend,
1266         .resume = usbnet_resume,
1267         .reset_resume = usbnet_resume,
1268         .supports_autosuspend = 1,
1269         .disable_hub_initiated_lpm = 1,
1270 };
1271
1272 module_usb_driver(cdc_ncm_driver);
1273
1274 MODULE_AUTHOR("Hans Petter Selasky");
1275 MODULE_DESCRIPTION("USB CDC NCM host driver");
1276 MODULE_LICENSE("Dual BSD/GPL");