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