4 * Copyright (C) ST-Ericsson 2010-2012
5 * Contact: Alexey Orishko <alexey.orishko@stericsson.com>
6 * Original author: Hans Petter Selasky <hans.petter.selasky@stericsson.com>
8 * USB Host Driver for Network Control Model (NCM)
9 * http://www.usb.org/developers/devclass_docs/NCM10.zip
11 * The NCM encoding, decoding and initialization logic
12 * derives from FreeBSD 8.x. if_cdce.c and if_cdcereg.h
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:
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
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.
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
41 #include <linux/module.h>
42 #include <linux/netdevice.h>
43 #include <linux/ctype.h>
44 #include <linux/ethtool.h>
45 #include <linux/workqueue.h>
46 #include <linux/mii.h>
47 #include <linux/crc32.h>
48 #include <linux/usb.h>
49 #include <linux/hrtimer.h>
50 #include <linux/atomic.h>
51 #include <linux/usb/usbnet.h>
52 #include <linux/usb/cdc.h>
53 #include <linux/usb/cdc_ncm.h>
55 #if IS_ENABLED(CONFIG_USB_NET_CDC_MBIM)
56 static bool prefer_mbim = true;
58 static bool prefer_mbim;
60 module_param(prefer_mbim, bool, S_IRUGO | S_IWUSR);
61 MODULE_PARM_DESC(prefer_mbim, "Prefer MBIM setting on dual NCM/MBIM functions");
63 static void cdc_ncm_txpath_bh(unsigned long param);
64 static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx);
65 static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *hr_timer);
66 static struct usb_driver cdc_ncm_driver;
68 struct cdc_ncm_stats {
69 char stat_string[ETH_GSTRING_LEN];
74 #define CDC_NCM_STAT(str, m) { \
76 .sizeof_stat = sizeof(((struct cdc_ncm_ctx *)0)->m), \
77 .stat_offset = offsetof(struct cdc_ncm_ctx, m) }
78 #define CDC_NCM_SIMPLE_STAT(m) CDC_NCM_STAT(__stringify(m), m)
80 static const struct cdc_ncm_stats cdc_ncm_gstrings_stats[] = {
81 CDC_NCM_SIMPLE_STAT(tx_reason_ntb_full),
82 CDC_NCM_SIMPLE_STAT(tx_reason_ndp_full),
83 CDC_NCM_SIMPLE_STAT(tx_reason_timeout),
84 CDC_NCM_SIMPLE_STAT(tx_reason_max_datagram),
85 CDC_NCM_SIMPLE_STAT(tx_overhead),
86 CDC_NCM_SIMPLE_STAT(tx_ntbs),
87 CDC_NCM_SIMPLE_STAT(rx_overhead),
88 CDC_NCM_SIMPLE_STAT(rx_ntbs),
91 static int cdc_ncm_get_sset_count(struct net_device __always_unused *netdev, int sset)
95 return ARRAY_SIZE(cdc_ncm_gstrings_stats);
101 static void cdc_ncm_get_ethtool_stats(struct net_device *netdev,
102 struct ethtool_stats __always_unused *stats,
105 struct usbnet *dev = netdev_priv(netdev);
106 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
110 for (i = 0; i < ARRAY_SIZE(cdc_ncm_gstrings_stats); i++) {
111 p = (char *)ctx + cdc_ncm_gstrings_stats[i].stat_offset;
112 data[i] = (cdc_ncm_gstrings_stats[i].sizeof_stat == sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
116 static void cdc_ncm_get_strings(struct net_device __always_unused *netdev, u32 stringset, u8 *data)
123 for (i = 0; i < ARRAY_SIZE(cdc_ncm_gstrings_stats); i++) {
124 memcpy(p, cdc_ncm_gstrings_stats[i].stat_string, ETH_GSTRING_LEN);
125 p += ETH_GSTRING_LEN;
130 static int cdc_ncm_get_coalesce(struct net_device *netdev,
131 struct ethtool_coalesce *ec)
133 struct usbnet *dev = netdev_priv(netdev);
134 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
136 /* assuming maximum sized dgrams and ignoring NDPs */
137 ec->rx_max_coalesced_frames = ctx->rx_max / ctx->max_datagram_size;
138 ec->tx_max_coalesced_frames = ctx->tx_max / ctx->max_datagram_size;
140 /* the timer will fire CDC_NCM_TIMER_PENDING_CNT times in a row */
141 ec->tx_coalesce_usecs = ctx->timer_interval / (NSEC_PER_USEC / CDC_NCM_TIMER_PENDING_CNT);
145 static void cdc_ncm_update_rxtx_max(struct usbnet *dev, u32 new_rx, u32 new_tx);
147 static int cdc_ncm_set_coalesce(struct net_device *netdev,
148 struct ethtool_coalesce *ec)
150 struct usbnet *dev = netdev_priv(netdev);
151 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
152 u32 new_rx_max = ctx->rx_max;
153 u32 new_tx_max = ctx->tx_max;
155 /* assuming maximum sized dgrams and a single NDP */
156 if (ec->rx_max_coalesced_frames)
157 new_rx_max = ec->rx_max_coalesced_frames * ctx->max_datagram_size;
158 if (ec->tx_max_coalesced_frames)
159 new_tx_max = ec->tx_max_coalesced_frames * ctx->max_datagram_size;
161 if (ec->tx_coalesce_usecs &&
162 (ec->tx_coalesce_usecs < CDC_NCM_TIMER_INTERVAL_MIN * CDC_NCM_TIMER_PENDING_CNT ||
163 ec->tx_coalesce_usecs > CDC_NCM_TIMER_INTERVAL_MAX * CDC_NCM_TIMER_PENDING_CNT))
166 spin_lock_bh(&ctx->mtx);
167 ctx->timer_interval = ec->tx_coalesce_usecs * (NSEC_PER_USEC / CDC_NCM_TIMER_PENDING_CNT);
168 if (!ctx->timer_interval)
169 ctx->tx_timer_pending = 0;
170 spin_unlock_bh(&ctx->mtx);
172 /* inform device of new values */
173 if (new_rx_max != ctx->rx_max || new_tx_max != ctx->tx_max)
174 cdc_ncm_update_rxtx_max(dev, new_rx_max, new_tx_max);
178 static const struct ethtool_ops cdc_ncm_ethtool_ops = {
179 .get_settings = usbnet_get_settings,
180 .set_settings = usbnet_set_settings,
181 .get_link = usbnet_get_link,
182 .nway_reset = usbnet_nway_reset,
183 .get_drvinfo = usbnet_get_drvinfo,
184 .get_msglevel = usbnet_get_msglevel,
185 .set_msglevel = usbnet_set_msglevel,
186 .get_ts_info = ethtool_op_get_ts_info,
187 .get_sset_count = cdc_ncm_get_sset_count,
188 .get_strings = cdc_ncm_get_strings,
189 .get_ethtool_stats = cdc_ncm_get_ethtool_stats,
190 .get_coalesce = cdc_ncm_get_coalesce,
191 .set_coalesce = cdc_ncm_set_coalesce,
194 /* handle rx_max and tx_max changes */
195 static void cdc_ncm_update_rxtx_max(struct usbnet *dev, u32 new_rx, u32 new_tx)
197 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
198 u8 iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber;
201 /* clamp new_rx to sane values */
202 min = USB_CDC_NCM_NTB_MIN_IN_SIZE;
203 max = min_t(u32, CDC_NCM_NTB_MAX_SIZE_RX, le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize));
205 /* dwNtbInMaxSize spec violation? Use MIN size for both limits */
207 dev_warn(&dev->intf->dev, "dwNtbInMaxSize=%u is too small. Using %u\n",
208 le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize), min);
212 val = clamp_t(u32, new_rx, min, max);
214 dev_dbg(&dev->intf->dev, "rx_max must be in the [%u, %u] range. Using %u\n",
218 /* usbnet use these values for sizing rx queues */
219 dev->rx_urb_size = val;
221 /* inform device about NTB input size changes */
222 if (val != ctx->rx_max) {
223 __le32 dwNtbInMaxSize = cpu_to_le32(val);
225 dev_info(&dev->intf->dev, "setting rx_max = %u\n", val);
227 /* need to unlink rx urbs before increasing buffer size */
228 if (netif_running(dev->net) && dev->rx_urb_size > ctx->rx_max)
229 usbnet_unlink_rx_urbs(dev);
231 /* tell device to use new size */
232 if (usbnet_write_cmd(dev, USB_CDC_SET_NTB_INPUT_SIZE,
233 USB_TYPE_CLASS | USB_DIR_OUT
234 | USB_RECIP_INTERFACE,
235 0, iface_no, &dwNtbInMaxSize, 4) < 0)
236 dev_dbg(&dev->intf->dev, "Setting NTB Input Size failed\n");
241 /* clamp new_tx to sane values */
242 min = ctx->max_datagram_size + ctx->max_ndp_size + sizeof(struct usb_cdc_ncm_nth16);
243 max = min_t(u32, CDC_NCM_NTB_MAX_SIZE_TX, le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize));
245 /* some devices set dwNtbOutMaxSize too low for the above default */
248 val = clamp_t(u32, new_tx, min, max);
250 dev_dbg(&dev->intf->dev, "tx_max must be in the [%u, %u] range. Using %u\n",
253 if (val != ctx->tx_max)
254 dev_info(&dev->intf->dev, "setting tx_max = %u\n", val);
256 /* Adding a pad byte here if necessary simplifies the handling
257 * in cdc_ncm_fill_tx_frame, making tx_max always represent
258 * the real skb max size.
260 * We cannot use dev->maxpacket here because this is called from
261 * .bind which is called before usbnet sets up dev->maxpacket
263 if (val != le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize) &&
264 val % usb_maxpacket(dev->udev, dev->out, 1) == 0)
267 /* we might need to flush any pending tx buffers if running */
268 if (netif_running(dev->net) && val > ctx->tx_max) {
269 netif_tx_lock_bh(dev->net);
270 usbnet_start_xmit(NULL, dev->net);
271 /* make sure tx_curr_skb is reallocated if it was empty */
272 if (ctx->tx_curr_skb) {
273 dev_kfree_skb_any(ctx->tx_curr_skb);
274 ctx->tx_curr_skb = NULL;
277 netif_tx_unlock_bh(dev->net);
282 dev->hard_mtu = ctx->tx_max;
284 /* max qlen depend on hard_mtu and rx_urb_size */
285 usbnet_update_max_qlen(dev);
287 /* never pad more than 3 full USB packets per transfer */
288 ctx->min_tx_pkt = clamp_t(u16, ctx->tx_max - 3 * usb_maxpacket(dev->udev, dev->out, 1),
289 CDC_NCM_MIN_TX_PKT, ctx->tx_max);
292 /* helpers for NCM and MBIM differences */
293 static u8 cdc_ncm_flags(struct usbnet *dev)
295 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
297 if (cdc_ncm_comm_intf_is_mbim(dev->intf->cur_altsetting) && ctx->mbim_desc)
298 return ctx->mbim_desc->bmNetworkCapabilities;
300 return ctx->func_desc->bmNetworkCapabilities;
304 static int cdc_ncm_eth_hlen(struct usbnet *dev)
306 if (cdc_ncm_comm_intf_is_mbim(dev->intf->cur_altsetting))
311 static u32 cdc_ncm_min_dgram_size(struct usbnet *dev)
313 if (cdc_ncm_comm_intf_is_mbim(dev->intf->cur_altsetting))
314 return CDC_MBIM_MIN_DATAGRAM_SIZE;
315 return CDC_NCM_MIN_DATAGRAM_SIZE;
318 static u32 cdc_ncm_max_dgram_size(struct usbnet *dev)
320 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
322 if (cdc_ncm_comm_intf_is_mbim(dev->intf->cur_altsetting) && ctx->mbim_desc)
323 return le16_to_cpu(ctx->mbim_desc->wMaxSegmentSize);
325 return le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
326 return CDC_NCM_MAX_DATAGRAM_SIZE;
329 /* initial one-time device setup. MUST be called with the data interface
332 static int cdc_ncm_init(struct usbnet *dev)
334 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
335 u8 iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber;
338 err = usbnet_read_cmd(dev, USB_CDC_GET_NTB_PARAMETERS,
339 USB_TYPE_CLASS | USB_DIR_IN
340 |USB_RECIP_INTERFACE,
341 0, iface_no, &ctx->ncm_parm,
342 sizeof(ctx->ncm_parm));
344 dev_err(&dev->intf->dev, "failed GET_NTB_PARAMETERS\n");
345 return err; /* GET_NTB_PARAMETERS is required */
349 if (cdc_ncm_flags(dev) & USB_CDC_NCM_NCAP_CRC_MODE) {
350 dev_dbg(&dev->intf->dev, "Setting CRC mode off\n");
351 err = usbnet_write_cmd(dev, USB_CDC_SET_CRC_MODE,
352 USB_TYPE_CLASS | USB_DIR_OUT
353 | USB_RECIP_INTERFACE,
354 USB_CDC_NCM_CRC_NOT_APPENDED,
357 dev_err(&dev->intf->dev, "SET_CRC_MODE failed\n");
360 /* set NTB format, if both formats are supported.
362 * "The host shall only send this command while the NCM Data
363 * Interface is in alternate setting 0."
365 if (le16_to_cpu(ctx->ncm_parm.bmNtbFormatsSupported) &
366 USB_CDC_NCM_NTB32_SUPPORTED) {
367 dev_dbg(&dev->intf->dev, "Setting NTB format to 16-bit\n");
368 err = usbnet_write_cmd(dev, USB_CDC_SET_NTB_FORMAT,
369 USB_TYPE_CLASS | USB_DIR_OUT
370 | USB_RECIP_INTERFACE,
371 USB_CDC_NCM_NTB16_FORMAT,
374 dev_err(&dev->intf->dev, "SET_NTB_FORMAT failed\n");
377 /* set initial device values */
378 ctx->rx_max = le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize);
379 ctx->tx_max = le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize);
380 ctx->tx_remainder = le16_to_cpu(ctx->ncm_parm.wNdpOutPayloadRemainder);
381 ctx->tx_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutDivisor);
382 ctx->tx_ndp_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutAlignment);
383 /* devices prior to NCM Errata shall set this field to zero */
384 ctx->tx_max_datagrams = le16_to_cpu(ctx->ncm_parm.wNtbOutMaxDatagrams);
386 dev_dbg(&dev->intf->dev,
387 "dwNtbInMaxSize=%u dwNtbOutMaxSize=%u wNdpOutPayloadRemainder=%u wNdpOutDivisor=%u wNdpOutAlignment=%u wNtbOutMaxDatagrams=%u flags=0x%x\n",
388 ctx->rx_max, ctx->tx_max, ctx->tx_remainder, ctx->tx_modulus,
389 ctx->tx_ndp_modulus, ctx->tx_max_datagrams, cdc_ncm_flags(dev));
391 /* max count of tx datagrams */
392 if ((ctx->tx_max_datagrams == 0) ||
393 (ctx->tx_max_datagrams > CDC_NCM_DPT_DATAGRAMS_MAX))
394 ctx->tx_max_datagrams = CDC_NCM_DPT_DATAGRAMS_MAX;
396 /* set up maximum NDP size */
397 ctx->max_ndp_size = sizeof(struct usb_cdc_ncm_ndp16) + (ctx->tx_max_datagrams + 1) * sizeof(struct usb_cdc_ncm_dpe16);
399 /* initial coalescing timer interval */
400 ctx->timer_interval = CDC_NCM_TIMER_INTERVAL_USEC * NSEC_PER_USEC;
405 /* set a new max datagram size */
406 static void cdc_ncm_set_dgram_size(struct usbnet *dev, int new_size)
408 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
409 u8 iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber;
410 __le16 max_datagram_size;
414 /* set default based on descriptors */
415 ctx->max_datagram_size = clamp_t(u32, new_size,
416 cdc_ncm_min_dgram_size(dev),
417 CDC_NCM_MAX_DATAGRAM_SIZE);
419 /* inform the device about the selected Max Datagram Size? */
420 if (!(cdc_ncm_flags(dev) & USB_CDC_NCM_NCAP_MAX_DATAGRAM_SIZE))
423 /* read current mtu value from device */
424 err = usbnet_read_cmd(dev, USB_CDC_GET_MAX_DATAGRAM_SIZE,
425 USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE,
426 0, iface_no, &max_datagram_size, 2);
428 dev_dbg(&dev->intf->dev, "GET_MAX_DATAGRAM_SIZE failed\n");
432 if (le16_to_cpu(max_datagram_size) == ctx->max_datagram_size)
435 max_datagram_size = cpu_to_le16(ctx->max_datagram_size);
436 err = usbnet_write_cmd(dev, USB_CDC_SET_MAX_DATAGRAM_SIZE,
437 USB_TYPE_CLASS | USB_DIR_OUT | USB_RECIP_INTERFACE,
438 0, iface_no, &max_datagram_size, 2);
440 dev_dbg(&dev->intf->dev, "SET_MAX_DATAGRAM_SIZE failed\n");
443 /* set MTU to max supported by the device if necessary */
444 dev->net->mtu = min_t(int, dev->net->mtu, ctx->max_datagram_size - cdc_ncm_eth_hlen(dev));
446 /* do not exceed operater preferred MTU */
447 if (ctx->mbim_extended_desc) {
448 mbim_mtu = le16_to_cpu(ctx->mbim_extended_desc->wMTU);
449 if (mbim_mtu != 0 && mbim_mtu < dev->net->mtu)
450 dev->net->mtu = mbim_mtu;
454 static void cdc_ncm_fix_modulus(struct usbnet *dev)
456 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
460 * verify that the structure alignment is:
462 * - not greater than the maximum transmit length
463 * - not less than four bytes
465 val = ctx->tx_ndp_modulus;
467 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
468 (val != ((-val) & val)) || (val >= ctx->tx_max)) {
469 dev_dbg(&dev->intf->dev, "Using default alignment: 4 bytes\n");
470 ctx->tx_ndp_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
474 * verify that the payload alignment is:
476 * - not greater than the maximum transmit length
477 * - not less than four bytes
479 val = ctx->tx_modulus;
481 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
482 (val != ((-val) & val)) || (val >= ctx->tx_max)) {
483 dev_dbg(&dev->intf->dev, "Using default transmit modulus: 4 bytes\n");
484 ctx->tx_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
487 /* verify the payload remainder */
488 if (ctx->tx_remainder >= ctx->tx_modulus) {
489 dev_dbg(&dev->intf->dev, "Using default transmit remainder: 0 bytes\n");
490 ctx->tx_remainder = 0;
493 /* adjust TX-remainder according to NCM specification. */
494 ctx->tx_remainder = ((ctx->tx_remainder - cdc_ncm_eth_hlen(dev)) &
495 (ctx->tx_modulus - 1));
498 static int cdc_ncm_setup(struct usbnet *dev)
500 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
503 /* be conservative when selecting intial buffer size to
504 * increase the number of hosts this will work for
506 def_rx = min_t(u32, CDC_NCM_NTB_DEF_SIZE_RX,
507 le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize));
508 def_tx = min_t(u32, CDC_NCM_NTB_DEF_SIZE_TX,
509 le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize));
511 /* clamp rx_max and tx_max and inform device */
512 cdc_ncm_update_rxtx_max(dev, def_rx, def_tx);
514 /* sanitize the modulus and remainder values */
515 cdc_ncm_fix_modulus(dev);
517 /* set max datagram size */
518 cdc_ncm_set_dgram_size(dev, cdc_ncm_max_dgram_size(dev));
523 cdc_ncm_find_endpoints(struct usbnet *dev, struct usb_interface *intf)
525 struct usb_host_endpoint *e, *in = NULL, *out = NULL;
528 for (ep = 0; ep < intf->cur_altsetting->desc.bNumEndpoints; ep++) {
530 e = intf->cur_altsetting->endpoint + ep;
531 switch (e->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
532 case USB_ENDPOINT_XFER_INT:
533 if (usb_endpoint_dir_in(&e->desc)) {
539 case USB_ENDPOINT_XFER_BULK:
540 if (usb_endpoint_dir_in(&e->desc)) {
554 dev->in = usb_rcvbulkpipe(dev->udev,
555 in->desc.bEndpointAddress &
556 USB_ENDPOINT_NUMBER_MASK);
557 if (out && !dev->out)
558 dev->out = usb_sndbulkpipe(dev->udev,
559 out->desc.bEndpointAddress &
560 USB_ENDPOINT_NUMBER_MASK);
563 static void cdc_ncm_free(struct cdc_ncm_ctx *ctx)
568 if (ctx->tx_rem_skb != NULL) {
569 dev_kfree_skb_any(ctx->tx_rem_skb);
570 ctx->tx_rem_skb = NULL;
573 if (ctx->tx_curr_skb != NULL) {
574 dev_kfree_skb_any(ctx->tx_curr_skb);
575 ctx->tx_curr_skb = NULL;
581 int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_altsetting)
583 const struct usb_cdc_union_desc *union_desc = NULL;
584 struct cdc_ncm_ctx *ctx;
585 struct usb_driver *driver;
591 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
595 hrtimer_init(&ctx->tx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
596 ctx->tx_timer.function = &cdc_ncm_tx_timer_cb;
597 ctx->bh.data = (unsigned long)dev;
598 ctx->bh.func = cdc_ncm_txpath_bh;
599 atomic_set(&ctx->stop, 0);
600 spin_lock_init(&ctx->mtx);
602 /* store ctx pointer in device data field */
603 dev->data[0] = (unsigned long)ctx;
605 /* only the control interface can be successfully probed */
608 /* get some pointers */
609 driver = driver_of(intf);
610 buf = intf->cur_altsetting->extra;
611 len = intf->cur_altsetting->extralen;
613 /* parse through descriptors associated with control interface */
614 while ((len > 0) && (buf[0] > 2) && (buf[0] <= len)) {
616 if (buf[1] != USB_DT_CS_INTERFACE)
620 case USB_CDC_UNION_TYPE:
621 if (buf[0] < sizeof(*union_desc))
624 union_desc = (const struct usb_cdc_union_desc *)buf;
625 /* the master must be the interface we are probing */
626 if (intf->cur_altsetting->desc.bInterfaceNumber !=
627 union_desc->bMasterInterface0) {
628 dev_dbg(&intf->dev, "bogus CDC Union\n");
631 ctx->data = usb_ifnum_to_if(dev->udev,
632 union_desc->bSlaveInterface0);
635 case USB_CDC_ETHERNET_TYPE:
636 if (buf[0] < sizeof(*(ctx->ether_desc)))
640 (const struct usb_cdc_ether_desc *)buf;
643 case USB_CDC_NCM_TYPE:
644 if (buf[0] < sizeof(*(ctx->func_desc)))
647 ctx->func_desc = (const struct usb_cdc_ncm_desc *)buf;
650 case USB_CDC_MBIM_TYPE:
651 if (buf[0] < sizeof(*(ctx->mbim_desc)))
654 ctx->mbim_desc = (const struct usb_cdc_mbim_desc *)buf;
657 case USB_CDC_MBIM_EXTENDED_TYPE:
658 if (buf[0] < sizeof(*(ctx->mbim_extended_desc)))
661 ctx->mbim_extended_desc =
662 (const struct usb_cdc_mbim_extended_desc *)buf;
669 /* advance to next descriptor */
675 /* some buggy devices have an IAD but no CDC Union */
676 if (!union_desc && intf->intf_assoc && intf->intf_assoc->bInterfaceCount == 2) {
677 ctx->data = usb_ifnum_to_if(dev->udev, intf->cur_altsetting->desc.bInterfaceNumber + 1);
678 dev_dbg(&intf->dev, "CDC Union missing - got slave from IAD\n");
681 /* check if we got everything */
683 dev_dbg(&intf->dev, "CDC Union missing and no IAD found\n");
686 if (cdc_ncm_comm_intf_is_mbim(intf->cur_altsetting)) {
687 if (!ctx->mbim_desc) {
688 dev_dbg(&intf->dev, "MBIM functional descriptor missing\n");
692 if (!ctx->ether_desc || !ctx->func_desc) {
693 dev_dbg(&intf->dev, "NCM or ECM functional descriptors missing\n");
698 /* claim data interface, if different from control */
699 if (ctx->data != ctx->control) {
700 temp = usb_driver_claim_interface(driver, ctx->data, dev);
702 dev_dbg(&intf->dev, "failed to claim data intf\n");
707 iface_no = ctx->data->cur_altsetting->desc.bInterfaceNumber;
709 /* reset data interface */
710 temp = usb_set_interface(dev->udev, iface_no, 0);
712 dev_dbg(&intf->dev, "set interface failed\n");
716 /* initialize basic device settings */
717 if (cdc_ncm_init(dev))
720 /* configure data interface */
721 temp = usb_set_interface(dev->udev, iface_no, data_altsetting);
723 dev_dbg(&intf->dev, "set interface failed\n");
727 cdc_ncm_find_endpoints(dev, ctx->data);
728 cdc_ncm_find_endpoints(dev, ctx->control);
729 if (!dev->in || !dev->out || !dev->status) {
730 dev_dbg(&intf->dev, "failed to collect endpoints\n");
734 usb_set_intfdata(ctx->data, dev);
735 usb_set_intfdata(ctx->control, dev);
737 if (ctx->ether_desc) {
738 temp = usbnet_get_ethernet_addr(dev, ctx->ether_desc->iMACAddress);
740 dev_dbg(&intf->dev, "failed to get mac address\n");
743 dev_info(&intf->dev, "MAC-Address: %pM\n", dev->net->dev_addr);
746 /* finish setting up the device specific data */
749 /* override ethtool_ops */
750 dev->net->ethtool_ops = &cdc_ncm_ethtool_ops;
755 usb_set_intfdata(ctx->control, NULL);
756 usb_set_intfdata(ctx->data, NULL);
757 if (ctx->data != ctx->control)
758 usb_driver_release_interface(driver, ctx->data);
760 cdc_ncm_free((struct cdc_ncm_ctx *)dev->data[0]);
762 dev_info(&intf->dev, "bind() failure\n");
765 EXPORT_SYMBOL_GPL(cdc_ncm_bind_common);
767 void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf)
769 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
770 struct usb_driver *driver = driver_of(intf);
773 return; /* no setup */
775 atomic_set(&ctx->stop, 1);
777 if (hrtimer_active(&ctx->tx_timer))
778 hrtimer_cancel(&ctx->tx_timer);
780 tasklet_kill(&ctx->bh);
782 /* handle devices with combined control and data interface */
783 if (ctx->control == ctx->data)
786 /* disconnect master --> disconnect slave */
787 if (intf == ctx->control && ctx->data) {
788 usb_set_intfdata(ctx->data, NULL);
789 usb_driver_release_interface(driver, ctx->data);
792 } else if (intf == ctx->data && ctx->control) {
793 usb_set_intfdata(ctx->control, NULL);
794 usb_driver_release_interface(driver, ctx->control);
798 usb_set_intfdata(intf, NULL);
801 EXPORT_SYMBOL_GPL(cdc_ncm_unbind);
803 /* Return the number of the MBIM control interface altsetting iff it
804 * is preferred and available,
806 u8 cdc_ncm_select_altsetting(struct usb_interface *intf)
808 struct usb_host_interface *alt;
810 /* The MBIM spec defines a NCM compatible default altsetting,
811 * which we may have matched:
813 * "Functions that implement both NCM 1.0 and MBIM (an
814 * “NCM/MBIM function”) according to this recommendation
815 * shall provide two alternate settings for the
816 * Communication Interface. Alternate setting 0, and the
817 * associated class and endpoint descriptors, shall be
818 * constructed according to the rules given for the
819 * Communication Interface in section 5 of [USBNCM10].
820 * Alternate setting 1, and the associated class and
821 * endpoint descriptors, shall be constructed according to
822 * the rules given in section 6 (USB Device Model) of this
825 if (intf->num_altsetting < 2)
826 return intf->cur_altsetting->desc.bAlternateSetting;
829 alt = usb_altnum_to_altsetting(intf, CDC_NCM_COMM_ALTSETTING_MBIM);
830 if (alt && cdc_ncm_comm_intf_is_mbim(alt))
831 return CDC_NCM_COMM_ALTSETTING_MBIM;
833 return CDC_NCM_COMM_ALTSETTING_NCM;
835 EXPORT_SYMBOL_GPL(cdc_ncm_select_altsetting);
837 static int cdc_ncm_bind(struct usbnet *dev, struct usb_interface *intf)
841 /* MBIM backwards compatible function? */
842 if (cdc_ncm_select_altsetting(intf) != CDC_NCM_COMM_ALTSETTING_NCM)
845 /* The NCM data altsetting is fixed */
846 ret = cdc_ncm_bind_common(dev, intf, CDC_NCM_DATA_ALTSETTING_NCM);
849 * We should get an event when network connection is "connected" or
850 * "disconnected". Set network connection in "disconnected" state
851 * (carrier is OFF) during attach, so the IP network stack does not
852 * start IPv6 negotiation and more.
854 usbnet_link_change(dev, 0, 0);
858 static void cdc_ncm_align_tail(struct sk_buff *skb, size_t modulus, size_t remainder, size_t max)
860 size_t align = ALIGN(skb->len, modulus) - skb->len + remainder;
862 if (skb->len + align > max)
863 align = max - skb->len;
864 if (align && skb_tailroom(skb) >= align)
865 memset(skb_put(skb, align), 0, align);
868 /* return a pointer to a valid struct usb_cdc_ncm_ndp16 of type sign, possibly
869 * allocating a new one within skb
871 static struct usb_cdc_ncm_ndp16 *cdc_ncm_ndp(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign, size_t reserve)
873 struct usb_cdc_ncm_ndp16 *ndp16 = NULL;
874 struct usb_cdc_ncm_nth16 *nth16 = (void *)skb->data;
875 size_t ndpoffset = le16_to_cpu(nth16->wNdpIndex);
877 /* follow the chain of NDPs, looking for a match */
879 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb->data + ndpoffset);
880 if (ndp16->dwSignature == sign)
882 ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
886 cdc_ncm_align_tail(skb, ctx->tx_ndp_modulus, 0, ctx->tx_max);
888 /* verify that there is room for the NDP and the datagram (reserve) */
889 if ((ctx->tx_max - skb->len - reserve) < ctx->max_ndp_size)
894 ndp16->wNextNdpIndex = cpu_to_le16(skb->len);
896 nth16->wNdpIndex = cpu_to_le16(skb->len);
898 /* push a new empty NDP */
899 ndp16 = (struct usb_cdc_ncm_ndp16 *)memset(skb_put(skb, ctx->max_ndp_size), 0, ctx->max_ndp_size);
900 ndp16->dwSignature = sign;
901 ndp16->wLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_ndp16) + sizeof(struct usb_cdc_ncm_dpe16));
906 cdc_ncm_fill_tx_frame(struct usbnet *dev, struct sk_buff *skb, __le32 sign)
908 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
909 struct usb_cdc_ncm_nth16 *nth16;
910 struct usb_cdc_ncm_ndp16 *ndp16;
911 struct sk_buff *skb_out;
912 u16 n = 0, index, ndplen;
915 /* if there is a remaining skb, it gets priority */
917 swap(skb, ctx->tx_rem_skb);
918 swap(sign, ctx->tx_rem_sign);
923 /* check if we are resuming an OUT skb */
924 skb_out = ctx->tx_curr_skb;
926 /* allocate a new OUT skb */
928 skb_out = alloc_skb(ctx->tx_max, GFP_ATOMIC);
929 if (skb_out == NULL) {
931 dev_kfree_skb_any(skb);
932 dev->net->stats.tx_dropped++;
936 /* fill out the initial 16-bit NTB header */
937 nth16 = (struct usb_cdc_ncm_nth16 *)memset(skb_put(skb_out, sizeof(struct usb_cdc_ncm_nth16)), 0, sizeof(struct usb_cdc_ncm_nth16));
938 nth16->dwSignature = cpu_to_le32(USB_CDC_NCM_NTH16_SIGN);
939 nth16->wHeaderLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_nth16));
940 nth16->wSequence = cpu_to_le16(ctx->tx_seq++);
942 /* count total number of frames in this NTB */
943 ctx->tx_curr_frame_num = 0;
945 /* recent payload counter for this skb_out */
946 ctx->tx_curr_frame_payload = 0;
949 for (n = ctx->tx_curr_frame_num; n < ctx->tx_max_datagrams; n++) {
950 /* send any remaining skb first */
952 skb = ctx->tx_rem_skb;
953 sign = ctx->tx_rem_sign;
954 ctx->tx_rem_skb = NULL;
956 /* check for end of skb */
961 /* get the appropriate NDP for this skb */
962 ndp16 = cdc_ncm_ndp(ctx, skb_out, sign, skb->len + ctx->tx_modulus + ctx->tx_remainder);
964 /* align beginning of next frame */
965 cdc_ncm_align_tail(skb_out, ctx->tx_modulus, ctx->tx_remainder, ctx->tx_max);
967 /* check if we had enough room left for both NDP and frame */
968 if (!ndp16 || skb_out->len + skb->len > ctx->tx_max) {
970 /* won't fit, MTU problem? */
971 dev_kfree_skb_any(skb);
973 dev->net->stats.tx_dropped++;
975 /* no room for skb - store for later */
976 if (ctx->tx_rem_skb != NULL) {
977 dev_kfree_skb_any(ctx->tx_rem_skb);
978 dev->net->stats.tx_dropped++;
980 ctx->tx_rem_skb = skb;
981 ctx->tx_rem_sign = sign;
984 ctx->tx_reason_ntb_full++; /* count reason for transmitting */
989 /* calculate frame number withing this NDP */
990 ndplen = le16_to_cpu(ndp16->wLength);
991 index = (ndplen - sizeof(struct usb_cdc_ncm_ndp16)) / sizeof(struct usb_cdc_ncm_dpe16) - 1;
993 /* OK, add this skb */
994 ndp16->dpe16[index].wDatagramLength = cpu_to_le16(skb->len);
995 ndp16->dpe16[index].wDatagramIndex = cpu_to_le16(skb_out->len);
996 ndp16->wLength = cpu_to_le16(ndplen + sizeof(struct usb_cdc_ncm_dpe16));
997 memcpy(skb_put(skb_out, skb->len), skb->data, skb->len);
998 ctx->tx_curr_frame_payload += skb->len; /* count real tx payload data */
999 dev_kfree_skb_any(skb);
1002 /* send now if this NDP is full */
1003 if (index >= CDC_NCM_DPT_DATAGRAMS_MAX) {
1005 ctx->tx_reason_ndp_full++; /* count reason for transmitting */
1010 /* free up any dangling skb */
1012 dev_kfree_skb_any(skb);
1014 dev->net->stats.tx_dropped++;
1017 ctx->tx_curr_frame_num = n;
1020 /* wait for more frames */
1021 /* push variables */
1022 ctx->tx_curr_skb = skb_out;
1025 } else if ((n < ctx->tx_max_datagrams) && (ready2send == 0) && (ctx->timer_interval > 0)) {
1026 /* wait for more frames */
1027 /* push variables */
1028 ctx->tx_curr_skb = skb_out;
1029 /* set the pending count */
1030 if (n < CDC_NCM_RESTART_TIMER_DATAGRAM_CNT)
1031 ctx->tx_timer_pending = CDC_NCM_TIMER_PENDING_CNT;
1035 if (n == ctx->tx_max_datagrams)
1036 ctx->tx_reason_max_datagram++; /* count reason for transmitting */
1037 /* frame goes out */
1038 /* variables will be reset at next call */
1041 /* If collected data size is less or equal ctx->min_tx_pkt
1042 * bytes, we send buffers as it is. If we get more data, it
1043 * would be more efficient for USB HS mobile device with DMA
1044 * engine to receive a full size NTB, than canceling DMA
1045 * transfer and receiving a short packet.
1047 * This optimization support is pointless if we end up sending
1048 * a ZLP after full sized NTBs.
1050 if (!(dev->driver_info->flags & FLAG_SEND_ZLP) &&
1051 skb_out->len > ctx->min_tx_pkt)
1052 memset(skb_put(skb_out, ctx->tx_max - skb_out->len), 0,
1053 ctx->tx_max - skb_out->len);
1054 else if (skb_out->len < ctx->tx_max && (skb_out->len % dev->maxpacket) == 0)
1055 *skb_put(skb_out, 1) = 0; /* force short packet */
1057 /* set final frame length */
1058 nth16 = (struct usb_cdc_ncm_nth16 *)skb_out->data;
1059 nth16->wBlockLength = cpu_to_le16(skb_out->len);
1062 ctx->tx_curr_skb = NULL;
1063 dev->net->stats.tx_packets += ctx->tx_curr_frame_num;
1065 /* keep private stats: framing overhead and number of NTBs */
1066 ctx->tx_overhead += skb_out->len - ctx->tx_curr_frame_payload;
1069 /* usbnet has already counted all the framing overhead.
1070 * Adjust the stats so that the tx_bytes counter show real
1071 * payload data instead.
1073 dev->net->stats.tx_bytes -= skb_out->len - ctx->tx_curr_frame_payload;
1078 /* Start timer, if there is a remaining non-empty skb */
1079 if (ctx->tx_curr_skb != NULL && n > 0)
1080 cdc_ncm_tx_timeout_start(ctx);
1083 EXPORT_SYMBOL_GPL(cdc_ncm_fill_tx_frame);
1085 static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx)
1087 /* start timer, if not already started */
1088 if (!(hrtimer_active(&ctx->tx_timer) || atomic_read(&ctx->stop)))
1089 hrtimer_start(&ctx->tx_timer,
1090 ktime_set(0, ctx->timer_interval),
1094 static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *timer)
1096 struct cdc_ncm_ctx *ctx =
1097 container_of(timer, struct cdc_ncm_ctx, tx_timer);
1099 if (!atomic_read(&ctx->stop))
1100 tasklet_schedule(&ctx->bh);
1101 return HRTIMER_NORESTART;
1104 static void cdc_ncm_txpath_bh(unsigned long param)
1106 struct usbnet *dev = (struct usbnet *)param;
1107 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
1109 spin_lock_bh(&ctx->mtx);
1110 if (ctx->tx_timer_pending != 0) {
1111 ctx->tx_timer_pending--;
1112 cdc_ncm_tx_timeout_start(ctx);
1113 spin_unlock_bh(&ctx->mtx);
1114 } else if (dev->net != NULL) {
1115 ctx->tx_reason_timeout++; /* count reason for transmitting */
1116 spin_unlock_bh(&ctx->mtx);
1117 netif_tx_lock_bh(dev->net);
1118 usbnet_start_xmit(NULL, dev->net);
1119 netif_tx_unlock_bh(dev->net);
1121 spin_unlock_bh(&ctx->mtx);
1126 cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
1128 struct sk_buff *skb_out;
1129 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
1132 * The Ethernet API we are using does not support transmitting
1133 * multiple Ethernet frames in a single call. This driver will
1134 * accumulate multiple Ethernet frames and send out a larger
1135 * USB frame when the USB buffer is full or when a single jiffies
1141 spin_lock_bh(&ctx->mtx);
1142 skb_out = cdc_ncm_fill_tx_frame(dev, skb, cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN));
1143 spin_unlock_bh(&ctx->mtx);
1148 dev_kfree_skb_any(skb);
1152 EXPORT_SYMBOL_GPL(cdc_ncm_tx_fixup);
1154 /* verify NTB header and return offset of first NDP, or negative error */
1155 int cdc_ncm_rx_verify_nth16(struct cdc_ncm_ctx *ctx, struct sk_buff *skb_in)
1157 struct usbnet *dev = netdev_priv(skb_in->dev);
1158 struct usb_cdc_ncm_nth16 *nth16;
1165 if (skb_in->len < (sizeof(struct usb_cdc_ncm_nth16) +
1166 sizeof(struct usb_cdc_ncm_ndp16))) {
1167 netif_dbg(dev, rx_err, dev->net, "frame too short\n");
1171 nth16 = (struct usb_cdc_ncm_nth16 *)skb_in->data;
1173 if (nth16->dwSignature != cpu_to_le32(USB_CDC_NCM_NTH16_SIGN)) {
1174 netif_dbg(dev, rx_err, dev->net,
1175 "invalid NTH16 signature <%#010x>\n",
1176 le32_to_cpu(nth16->dwSignature));
1180 len = le16_to_cpu(nth16->wBlockLength);
1181 if (len > ctx->rx_max) {
1182 netif_dbg(dev, rx_err, dev->net,
1183 "unsupported NTB block length %u/%u\n", len,
1188 if ((ctx->rx_seq + 1) != le16_to_cpu(nth16->wSequence) &&
1189 (ctx->rx_seq || le16_to_cpu(nth16->wSequence)) &&
1190 !((ctx->rx_seq == 0xffff) && !le16_to_cpu(nth16->wSequence))) {
1191 netif_dbg(dev, rx_err, dev->net,
1192 "sequence number glitch prev=%d curr=%d\n",
1193 ctx->rx_seq, le16_to_cpu(nth16->wSequence));
1195 ctx->rx_seq = le16_to_cpu(nth16->wSequence);
1197 ret = le16_to_cpu(nth16->wNdpIndex);
1201 EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_nth16);
1203 /* verify NDP header and return number of datagrams, or negative error */
1204 int cdc_ncm_rx_verify_ndp16(struct sk_buff *skb_in, int ndpoffset)
1206 struct usbnet *dev = netdev_priv(skb_in->dev);
1207 struct usb_cdc_ncm_ndp16 *ndp16;
1210 if ((ndpoffset + sizeof(struct usb_cdc_ncm_ndp16)) > skb_in->len) {
1211 netif_dbg(dev, rx_err, dev->net, "invalid NDP offset <%u>\n",
1215 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
1217 if (le16_to_cpu(ndp16->wLength) < USB_CDC_NCM_NDP16_LENGTH_MIN) {
1218 netif_dbg(dev, rx_err, dev->net, "invalid DPT16 length <%u>\n",
1219 le16_to_cpu(ndp16->wLength));
1223 ret = ((le16_to_cpu(ndp16->wLength) -
1224 sizeof(struct usb_cdc_ncm_ndp16)) /
1225 sizeof(struct usb_cdc_ncm_dpe16));
1226 ret--; /* we process NDP entries except for the last one */
1228 if ((sizeof(struct usb_cdc_ncm_ndp16) +
1229 ret * (sizeof(struct usb_cdc_ncm_dpe16))) > skb_in->len) {
1230 netif_dbg(dev, rx_err, dev->net, "Invalid nframes = %d\n", ret);
1237 EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_ndp16);
1239 int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
1241 struct sk_buff *skb;
1242 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
1247 struct usb_cdc_ncm_ndp16 *ndp16;
1248 struct usb_cdc_ncm_dpe16 *dpe16;
1250 int loopcount = 50; /* arbitrary max preventing infinite loop */
1253 ndpoffset = cdc_ncm_rx_verify_nth16(ctx, skb_in);
1258 nframes = cdc_ncm_rx_verify_ndp16(skb_in, ndpoffset);
1262 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
1264 if (ndp16->dwSignature != cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN)) {
1265 netif_dbg(dev, rx_err, dev->net,
1266 "invalid DPT16 signature <%#010x>\n",
1267 le32_to_cpu(ndp16->dwSignature));
1270 dpe16 = ndp16->dpe16;
1272 for (x = 0; x < nframes; x++, dpe16++) {
1273 offset = le16_to_cpu(dpe16->wDatagramIndex);
1274 len = le16_to_cpu(dpe16->wDatagramLength);
1278 * All entries after first NULL entry are to be ignored
1280 if ((offset == 0) || (len == 0)) {
1282 goto err_ndp; /* empty NTB */
1286 /* sanity checking */
1287 if (((offset + len) > skb_in->len) ||
1288 (len > ctx->rx_max) || (len < ETH_HLEN)) {
1289 netif_dbg(dev, rx_err, dev->net,
1290 "invalid frame detected (ignored) offset[%u]=%u, length=%u, skb=%p\n",
1291 x, offset, len, skb_in);
1297 /* create a fresh copy to reduce truesize */
1298 skb = netdev_alloc_skb_ip_align(dev->net, len);
1301 memcpy(skb_put(skb, len), skb_in->data + offset, len);
1302 usbnet_skb_return(dev, skb);
1303 payload += len; /* count payload bytes in this NTB */
1307 /* are there more NDPs to process? */
1308 ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
1309 if (ndpoffset && loopcount--)
1313 ctx->rx_overhead += skb_in->len - payload;
1320 EXPORT_SYMBOL_GPL(cdc_ncm_rx_fixup);
1323 cdc_ncm_speed_change(struct usbnet *dev,
1324 struct usb_cdc_speed_change *data)
1326 uint32_t rx_speed = le32_to_cpu(data->DLBitRRate);
1327 uint32_t tx_speed = le32_to_cpu(data->ULBitRate);
1330 * Currently the USB-NET API does not support reporting the actual
1331 * device speed. Do print it instead.
1333 if ((tx_speed > 1000000) && (rx_speed > 1000000)) {
1334 netif_info(dev, link, dev->net,
1335 "%u mbit/s downlink %u mbit/s uplink\n",
1336 (unsigned int)(rx_speed / 1000000U),
1337 (unsigned int)(tx_speed / 1000000U));
1339 netif_info(dev, link, dev->net,
1340 "%u kbit/s downlink %u kbit/s uplink\n",
1341 (unsigned int)(rx_speed / 1000U),
1342 (unsigned int)(tx_speed / 1000U));
1346 static void cdc_ncm_status(struct usbnet *dev, struct urb *urb)
1348 struct cdc_ncm_ctx *ctx;
1349 struct usb_cdc_notification *event;
1351 ctx = (struct cdc_ncm_ctx *)dev->data[0];
1353 if (urb->actual_length < sizeof(*event))
1356 /* test for split data in 8-byte chunks */
1357 if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
1358 cdc_ncm_speed_change(dev,
1359 (struct usb_cdc_speed_change *)urb->transfer_buffer);
1363 event = urb->transfer_buffer;
1365 switch (event->bNotificationType) {
1366 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
1368 * According to the CDC NCM specification ch.7.1
1369 * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be
1370 * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE.
1372 netif_info(dev, link, dev->net,
1373 "network connection: %sconnected\n",
1374 !!event->wValue ? "" : "dis");
1375 usbnet_link_change(dev, !!event->wValue, 0);
1378 case USB_CDC_NOTIFY_SPEED_CHANGE:
1379 if (urb->actual_length < (sizeof(*event) +
1380 sizeof(struct usb_cdc_speed_change)))
1381 set_bit(EVENT_STS_SPLIT, &dev->flags);
1383 cdc_ncm_speed_change(dev,
1384 (struct usb_cdc_speed_change *)&event[1]);
1388 dev_dbg(&dev->udev->dev,
1389 "NCM: unexpected notification 0x%02x!\n",
1390 event->bNotificationType);
1395 static const struct driver_info cdc_ncm_info = {
1396 .description = "CDC NCM",
1397 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET,
1398 .bind = cdc_ncm_bind,
1399 .unbind = cdc_ncm_unbind,
1400 .manage_power = usbnet_manage_power,
1401 .status = cdc_ncm_status,
1402 .rx_fixup = cdc_ncm_rx_fixup,
1403 .tx_fixup = cdc_ncm_tx_fixup,
1406 /* Same as cdc_ncm_info, but with FLAG_WWAN */
1407 static const struct driver_info wwan_info = {
1408 .description = "Mobile Broadband Network Device",
1409 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
1411 .bind = cdc_ncm_bind,
1412 .unbind = cdc_ncm_unbind,
1413 .manage_power = usbnet_manage_power,
1414 .status = cdc_ncm_status,
1415 .rx_fixup = cdc_ncm_rx_fixup,
1416 .tx_fixup = cdc_ncm_tx_fixup,
1419 /* Same as wwan_info, but with FLAG_NOARP */
1420 static const struct driver_info wwan_noarp_info = {
1421 .description = "Mobile Broadband Network Device (NO ARP)",
1422 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
1423 | FLAG_WWAN | FLAG_NOARP,
1424 .bind = cdc_ncm_bind,
1425 .unbind = cdc_ncm_unbind,
1426 .manage_power = usbnet_manage_power,
1427 .status = cdc_ncm_status,
1428 .rx_fixup = cdc_ncm_rx_fixup,
1429 .tx_fixup = cdc_ncm_tx_fixup,
1432 static const struct usb_device_id cdc_devs[] = {
1433 /* Ericsson MBM devices like F5521gw */
1434 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1435 | USB_DEVICE_ID_MATCH_VENDOR,
1437 .bInterfaceClass = USB_CLASS_COMM,
1438 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1439 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1440 .driver_info = (unsigned long) &wwan_info,
1443 /* Dell branded MBM devices like DW5550 */
1444 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1445 | USB_DEVICE_ID_MATCH_VENDOR,
1447 .bInterfaceClass = USB_CLASS_COMM,
1448 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1449 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1450 .driver_info = (unsigned long) &wwan_info,
1453 /* Toshiba branded MBM devices */
1454 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1455 | USB_DEVICE_ID_MATCH_VENDOR,
1457 .bInterfaceClass = USB_CLASS_COMM,
1458 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1459 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1460 .driver_info = (unsigned long) &wwan_info,
1463 /* tag Huawei devices as wwan */
1464 { USB_VENDOR_AND_INTERFACE_INFO(0x12d1,
1466 USB_CDC_SUBCLASS_NCM,
1467 USB_CDC_PROTO_NONE),
1468 .driver_info = (unsigned long)&wwan_info,
1471 /* Infineon(now Intel) HSPA Modem platform */
1472 { USB_DEVICE_AND_INTERFACE_INFO(0x1519, 0x0443,
1474 USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
1475 .driver_info = (unsigned long)&wwan_noarp_info,
1478 /* Generic CDC-NCM devices */
1479 { USB_INTERFACE_INFO(USB_CLASS_COMM,
1480 USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
1481 .driver_info = (unsigned long)&cdc_ncm_info,
1486 MODULE_DEVICE_TABLE(usb, cdc_devs);
1488 static struct usb_driver cdc_ncm_driver = {
1490 .id_table = cdc_devs,
1491 .probe = usbnet_probe,
1492 .disconnect = usbnet_disconnect,
1493 .suspend = usbnet_suspend,
1494 .resume = usbnet_resume,
1495 .reset_resume = usbnet_resume,
1496 .supports_autosuspend = 1,
1497 .disable_hub_initiated_lpm = 1,
1500 module_usb_driver(cdc_ncm_driver);
1502 MODULE_AUTHOR("Hans Petter Selasky");
1503 MODULE_DESCRIPTION("USB CDC NCM host driver");
1504 MODULE_LICENSE("Dual BSD/GPL");