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