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