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