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