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