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