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