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