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