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 /* handle rx_max and tx_max changes */
69 static void cdc_ncm_update_rxtx_max(struct usbnet *dev, u32 new_rx, u32 new_tx)
71 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
72 u8 iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber;
75 /* clamp new_rx to sane values */
76 min = USB_CDC_NCM_NTB_MIN_IN_SIZE;
77 max = min_t(u32, CDC_NCM_NTB_MAX_SIZE_RX, le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize));
79 /* dwNtbInMaxSize spec violation? Use MIN size for both limits */
81 dev_warn(&dev->intf->dev, "dwNtbInMaxSize=%u is too small. Using %u\n",
82 le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize), min);
86 val = clamp_t(u32, new_rx, min, max);
88 dev_dbg(&dev->intf->dev, "rx_max must be in the [%u, %u] range. Using %u\n",
92 /* inform device about NTB input size changes */
93 if (val != ctx->rx_max) {
94 __le32 dwNtbInMaxSize = cpu_to_le32(val);
96 dev_info(&dev->intf->dev, "setting rx_max = %u\n", val);
97 if (usbnet_write_cmd(dev, USB_CDC_SET_NTB_INPUT_SIZE,
98 USB_TYPE_CLASS | USB_DIR_OUT
99 | USB_RECIP_INTERFACE,
100 0, iface_no, &dwNtbInMaxSize, 4) < 0)
101 dev_dbg(&dev->intf->dev, "Setting NTB Input Size failed\n");
106 /* clamp new_tx to sane values */
107 min = CDC_NCM_MIN_HDR_SIZE + ctx->max_datagram_size;
108 max = min_t(u32, CDC_NCM_NTB_MAX_SIZE_TX, le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize));
110 /* some devices set dwNtbOutMaxSize too low for the above default */
113 val = clamp_t(u32, new_tx, min, max);
115 dev_dbg(&dev->intf->dev, "tx_max must be in the [%u, %u] range. Using %u\n",
118 if (val != ctx->tx_max)
119 dev_info(&dev->intf->dev, "setting tx_max = %u\n", val);
123 static int cdc_ncm_setup(struct usbnet *dev)
125 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
132 u16 ntb_fmt_supported;
133 __le16 max_datagram_size;
135 iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber;
137 err = usbnet_read_cmd(dev, USB_CDC_GET_NTB_PARAMETERS,
138 USB_TYPE_CLASS | USB_DIR_IN
139 |USB_RECIP_INTERFACE,
140 0, iface_no, &ctx->ncm_parm,
141 sizeof(ctx->ncm_parm));
143 dev_err(&dev->intf->dev, "failed GET_NTB_PARAMETERS\n");
144 return err; /* GET_NTB_PARAMETERS is required */
147 /* read correct set of parameters according to device mode */
148 ctx->rx_max = le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize);
149 ctx->tx_max = le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize);
150 ctx->tx_remainder = le16_to_cpu(ctx->ncm_parm.wNdpOutPayloadRemainder);
151 ctx->tx_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutDivisor);
152 ctx->tx_ndp_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutAlignment);
153 /* devices prior to NCM Errata shall set this field to zero */
154 ctx->tx_max_datagrams = le16_to_cpu(ctx->ncm_parm.wNtbOutMaxDatagrams);
155 ntb_fmt_supported = le16_to_cpu(ctx->ncm_parm.bmNtbFormatsSupported);
157 /* there are some minor differences in NCM and MBIM defaults */
158 if (cdc_ncm_comm_intf_is_mbim(ctx->control->cur_altsetting)) {
162 flags = ctx->mbim_desc->bmNetworkCapabilities;
163 ctx->max_datagram_size = le16_to_cpu(ctx->mbim_desc->wMaxSegmentSize);
164 if (ctx->max_datagram_size < CDC_MBIM_MIN_DATAGRAM_SIZE)
165 ctx->max_datagram_size = CDC_MBIM_MIN_DATAGRAM_SIZE;
170 flags = ctx->func_desc->bmNetworkCapabilities;
171 ctx->max_datagram_size = le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
172 if (ctx->max_datagram_size < CDC_NCM_MIN_DATAGRAM_SIZE)
173 ctx->max_datagram_size = CDC_NCM_MIN_DATAGRAM_SIZE;
176 /* common absolute max for NCM and MBIM */
177 if (ctx->max_datagram_size > CDC_NCM_MAX_DATAGRAM_SIZE)
178 ctx->max_datagram_size = CDC_NCM_MAX_DATAGRAM_SIZE;
180 dev_dbg(&dev->intf->dev,
181 "dwNtbInMaxSize=%u dwNtbOutMaxSize=%u wNdpOutPayloadRemainder=%u wNdpOutDivisor=%u wNdpOutAlignment=%u wNtbOutMaxDatagrams=%u flags=0x%x\n",
182 ctx->rx_max, ctx->tx_max, ctx->tx_remainder, ctx->tx_modulus,
183 ctx->tx_ndp_modulus, ctx->tx_max_datagrams, flags);
185 /* max count of tx datagrams */
186 if ((ctx->tx_max_datagrams == 0) ||
187 (ctx->tx_max_datagrams > CDC_NCM_DPT_DATAGRAMS_MAX))
188 ctx->tx_max_datagrams = CDC_NCM_DPT_DATAGRAMS_MAX;
190 /* clamp rx_max and tx_max and inform device */
191 cdc_ncm_update_rxtx_max(dev, le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize), le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize));
194 * verify that the structure alignment is:
196 * - not greater than the maximum transmit length
197 * - not less than four bytes
199 val = ctx->tx_ndp_modulus;
201 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
202 (val != ((-val) & val)) || (val >= ctx->tx_max)) {
203 dev_dbg(&dev->intf->dev, "Using default alignment: 4 bytes\n");
204 ctx->tx_ndp_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
208 * verify that the payload alignment is:
210 * - not greater than the maximum transmit length
211 * - not less than four bytes
213 val = ctx->tx_modulus;
215 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
216 (val != ((-val) & val)) || (val >= ctx->tx_max)) {
217 dev_dbg(&dev->intf->dev, "Using default transmit modulus: 4 bytes\n");
218 ctx->tx_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
221 /* verify the payload remainder */
222 if (ctx->tx_remainder >= ctx->tx_modulus) {
223 dev_dbg(&dev->intf->dev, "Using default transmit remainder: 0 bytes\n");
224 ctx->tx_remainder = 0;
227 /* adjust TX-remainder according to NCM specification. */
228 ctx->tx_remainder = ((ctx->tx_remainder - eth_hlen) &
229 (ctx->tx_modulus - 1));
231 /* additional configuration */
234 if (flags & USB_CDC_NCM_NCAP_CRC_MODE) {
235 err = usbnet_write_cmd(dev, USB_CDC_SET_CRC_MODE,
236 USB_TYPE_CLASS | USB_DIR_OUT
237 | USB_RECIP_INTERFACE,
238 USB_CDC_NCM_CRC_NOT_APPENDED,
241 dev_dbg(&dev->intf->dev, "Setting CRC mode off failed\n");
244 /* set NTB format, if both formats are supported */
245 if (ntb_fmt_supported & USB_CDC_NCM_NTH32_SIGN) {
246 err = usbnet_write_cmd(dev, USB_CDC_SET_NTB_FORMAT,
247 USB_TYPE_CLASS | USB_DIR_OUT
248 | USB_RECIP_INTERFACE,
249 USB_CDC_NCM_NTB16_FORMAT,
252 dev_dbg(&dev->intf->dev, "Setting NTB format to 16-bit failed\n");
255 /* inform the device about the selected Max Datagram Size */
256 if (!(flags & USB_CDC_NCM_NCAP_MAX_DATAGRAM_SIZE))
259 /* read current mtu value from device */
260 err = usbnet_read_cmd(dev, USB_CDC_GET_MAX_DATAGRAM_SIZE,
261 USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE,
262 0, iface_no, &max_datagram_size, 2);
264 dev_dbg(&dev->intf->dev, "GET_MAX_DATAGRAM_SIZE failed\n");
268 if (le16_to_cpu(max_datagram_size) == ctx->max_datagram_size)
271 max_datagram_size = cpu_to_le16(ctx->max_datagram_size);
272 err = usbnet_write_cmd(dev, USB_CDC_SET_MAX_DATAGRAM_SIZE,
273 USB_TYPE_CLASS | USB_DIR_OUT | USB_RECIP_INTERFACE,
274 0, iface_no, &max_datagram_size, 2);
276 dev_dbg(&dev->intf->dev, "SET_MAX_DATAGRAM_SIZE failed\n");
279 /* set MTU to max supported by the device if necessary */
280 if (dev->net->mtu > ctx->max_datagram_size - eth_hlen)
281 dev->net->mtu = ctx->max_datagram_size - eth_hlen;
283 /* do not exceed operater preferred MTU */
284 if (ctx->mbim_extended_desc) {
285 mbim_mtu = le16_to_cpu(ctx->mbim_extended_desc->wMTU);
286 if (mbim_mtu != 0 && mbim_mtu < dev->net->mtu)
287 dev->net->mtu = mbim_mtu;
294 cdc_ncm_find_endpoints(struct usbnet *dev, struct usb_interface *intf)
296 struct usb_host_endpoint *e, *in = NULL, *out = NULL;
299 for (ep = 0; ep < intf->cur_altsetting->desc.bNumEndpoints; ep++) {
301 e = intf->cur_altsetting->endpoint + ep;
302 switch (e->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
303 case USB_ENDPOINT_XFER_INT:
304 if (usb_endpoint_dir_in(&e->desc)) {
310 case USB_ENDPOINT_XFER_BULK:
311 if (usb_endpoint_dir_in(&e->desc)) {
325 dev->in = usb_rcvbulkpipe(dev->udev,
326 in->desc.bEndpointAddress &
327 USB_ENDPOINT_NUMBER_MASK);
328 if (out && !dev->out)
329 dev->out = usb_sndbulkpipe(dev->udev,
330 out->desc.bEndpointAddress &
331 USB_ENDPOINT_NUMBER_MASK);
334 static void cdc_ncm_free(struct cdc_ncm_ctx *ctx)
339 if (ctx->tx_rem_skb != NULL) {
340 dev_kfree_skb_any(ctx->tx_rem_skb);
341 ctx->tx_rem_skb = NULL;
344 if (ctx->tx_curr_skb != NULL) {
345 dev_kfree_skb_any(ctx->tx_curr_skb);
346 ctx->tx_curr_skb = NULL;
352 int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_altsetting)
354 const struct usb_cdc_union_desc *union_desc = NULL;
355 struct cdc_ncm_ctx *ctx;
356 struct usb_driver *driver;
362 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
366 hrtimer_init(&ctx->tx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
367 ctx->tx_timer.function = &cdc_ncm_tx_timer_cb;
368 ctx->bh.data = (unsigned long)dev;
369 ctx->bh.func = cdc_ncm_txpath_bh;
370 atomic_set(&ctx->stop, 0);
371 spin_lock_init(&ctx->mtx);
373 /* store ctx pointer in device data field */
374 dev->data[0] = (unsigned long)ctx;
376 /* only the control interface can be successfully probed */
379 /* get some pointers */
380 driver = driver_of(intf);
381 buf = intf->cur_altsetting->extra;
382 len = intf->cur_altsetting->extralen;
384 /* parse through descriptors associated with control interface */
385 while ((len > 0) && (buf[0] > 2) && (buf[0] <= len)) {
387 if (buf[1] != USB_DT_CS_INTERFACE)
391 case USB_CDC_UNION_TYPE:
392 if (buf[0] < sizeof(*union_desc))
395 union_desc = (const struct usb_cdc_union_desc *)buf;
396 /* the master must be the interface we are probing */
397 if (intf->cur_altsetting->desc.bInterfaceNumber !=
398 union_desc->bMasterInterface0) {
399 dev_dbg(&intf->dev, "bogus CDC Union\n");
402 ctx->data = usb_ifnum_to_if(dev->udev,
403 union_desc->bSlaveInterface0);
406 case USB_CDC_ETHERNET_TYPE:
407 if (buf[0] < sizeof(*(ctx->ether_desc)))
411 (const struct usb_cdc_ether_desc *)buf;
414 case USB_CDC_NCM_TYPE:
415 if (buf[0] < sizeof(*(ctx->func_desc)))
418 ctx->func_desc = (const struct usb_cdc_ncm_desc *)buf;
421 case USB_CDC_MBIM_TYPE:
422 if (buf[0] < sizeof(*(ctx->mbim_desc)))
425 ctx->mbim_desc = (const struct usb_cdc_mbim_desc *)buf;
428 case USB_CDC_MBIM_EXTENDED_TYPE:
429 if (buf[0] < sizeof(*(ctx->mbim_extended_desc)))
432 ctx->mbim_extended_desc =
433 (const struct usb_cdc_mbim_extended_desc *)buf;
440 /* advance to next descriptor */
446 /* some buggy devices have an IAD but no CDC Union */
447 if (!union_desc && intf->intf_assoc && intf->intf_assoc->bInterfaceCount == 2) {
448 ctx->data = usb_ifnum_to_if(dev->udev, intf->cur_altsetting->desc.bInterfaceNumber + 1);
449 dev_dbg(&intf->dev, "CDC Union missing - got slave from IAD\n");
452 /* check if we got everything */
453 if (!ctx->data || (!ctx->mbim_desc && !ctx->ether_desc)) {
454 dev_dbg(&intf->dev, "CDC descriptors missing\n");
458 /* claim data interface, if different from control */
459 if (ctx->data != ctx->control) {
460 temp = usb_driver_claim_interface(driver, ctx->data, dev);
462 dev_dbg(&intf->dev, "failed to claim data intf\n");
467 iface_no = ctx->data->cur_altsetting->desc.bInterfaceNumber;
469 /* reset data interface */
470 temp = usb_set_interface(dev->udev, iface_no, 0);
472 dev_dbg(&intf->dev, "set interface failed\n");
476 /* initialize data interface */
477 if (cdc_ncm_setup(dev))
480 /* configure data interface */
481 temp = usb_set_interface(dev->udev, iface_no, data_altsetting);
483 dev_dbg(&intf->dev, "set interface failed\n");
487 cdc_ncm_find_endpoints(dev, ctx->data);
488 cdc_ncm_find_endpoints(dev, ctx->control);
489 if (!dev->in || !dev->out || !dev->status) {
490 dev_dbg(&intf->dev, "failed to collect endpoints\n");
494 usb_set_intfdata(ctx->data, dev);
495 usb_set_intfdata(ctx->control, dev);
497 if (ctx->ether_desc) {
498 temp = usbnet_get_ethernet_addr(dev, ctx->ether_desc->iMACAddress);
500 dev_dbg(&intf->dev, "failed to get mac address\n");
503 dev_info(&intf->dev, "MAC-Address: %pM\n", dev->net->dev_addr);
506 /* usbnet use these values for sizing tx/rx queues */
507 dev->hard_mtu = ctx->tx_max;
508 dev->rx_urb_size = ctx->rx_max;
510 /* cdc_ncm_setup will override dwNtbOutMaxSize if it is
511 * outside the sane range. Adding a pad byte here if necessary
512 * simplifies the handling in cdc_ncm_fill_tx_frame, making
513 * tx_max always represent the real skb max size.
515 if (ctx->tx_max != le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize) &&
516 ctx->tx_max % usb_maxpacket(dev->udev, dev->out, 1) == 0)
522 usb_set_intfdata(ctx->control, NULL);
523 usb_set_intfdata(ctx->data, NULL);
524 if (ctx->data != ctx->control)
525 usb_driver_release_interface(driver, ctx->data);
527 cdc_ncm_free((struct cdc_ncm_ctx *)dev->data[0]);
529 dev_info(&intf->dev, "bind() failure\n");
532 EXPORT_SYMBOL_GPL(cdc_ncm_bind_common);
534 void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf)
536 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
537 struct usb_driver *driver = driver_of(intf);
540 return; /* no setup */
542 atomic_set(&ctx->stop, 1);
544 if (hrtimer_active(&ctx->tx_timer))
545 hrtimer_cancel(&ctx->tx_timer);
547 tasklet_kill(&ctx->bh);
549 /* handle devices with combined control and data interface */
550 if (ctx->control == ctx->data)
553 /* disconnect master --> disconnect slave */
554 if (intf == ctx->control && ctx->data) {
555 usb_set_intfdata(ctx->data, NULL);
556 usb_driver_release_interface(driver, ctx->data);
559 } else if (intf == ctx->data && ctx->control) {
560 usb_set_intfdata(ctx->control, NULL);
561 usb_driver_release_interface(driver, ctx->control);
565 usb_set_intfdata(intf, NULL);
568 EXPORT_SYMBOL_GPL(cdc_ncm_unbind);
570 /* Return the number of the MBIM control interface altsetting iff it
571 * is preferred and available,
573 u8 cdc_ncm_select_altsetting(struct usb_interface *intf)
575 struct usb_host_interface *alt;
577 /* The MBIM spec defines a NCM compatible default altsetting,
578 * which we may have matched:
580 * "Functions that implement both NCM 1.0 and MBIM (an
581 * “NCM/MBIM function”) according to this recommendation
582 * shall provide two alternate settings for the
583 * Communication Interface. Alternate setting 0, and the
584 * associated class and endpoint descriptors, shall be
585 * constructed according to the rules given for the
586 * Communication Interface in section 5 of [USBNCM10].
587 * Alternate setting 1, and the associated class and
588 * endpoint descriptors, shall be constructed according to
589 * the rules given in section 6 (USB Device Model) of this
592 if (intf->num_altsetting < 2)
593 return intf->cur_altsetting->desc.bAlternateSetting;
596 alt = usb_altnum_to_altsetting(intf, CDC_NCM_COMM_ALTSETTING_MBIM);
597 if (alt && cdc_ncm_comm_intf_is_mbim(alt))
598 return CDC_NCM_COMM_ALTSETTING_MBIM;
600 return CDC_NCM_COMM_ALTSETTING_NCM;
602 EXPORT_SYMBOL_GPL(cdc_ncm_select_altsetting);
604 static int cdc_ncm_bind(struct usbnet *dev, struct usb_interface *intf)
608 /* MBIM backwards compatible function? */
609 if (cdc_ncm_select_altsetting(intf) != CDC_NCM_COMM_ALTSETTING_NCM)
612 /* The NCM data altsetting is fixed */
613 ret = cdc_ncm_bind_common(dev, intf, CDC_NCM_DATA_ALTSETTING_NCM);
616 * We should get an event when network connection is "connected" or
617 * "disconnected". Set network connection in "disconnected" state
618 * (carrier is OFF) during attach, so the IP network stack does not
619 * start IPv6 negotiation and more.
621 usbnet_link_change(dev, 0, 0);
625 static void cdc_ncm_align_tail(struct sk_buff *skb, size_t modulus, size_t remainder, size_t max)
627 size_t align = ALIGN(skb->len, modulus) - skb->len + remainder;
629 if (skb->len + align > max)
630 align = max - skb->len;
631 if (align && skb_tailroom(skb) >= align)
632 memset(skb_put(skb, align), 0, align);
635 /* return a pointer to a valid struct usb_cdc_ncm_ndp16 of type sign, possibly
636 * allocating a new one within skb
638 static struct usb_cdc_ncm_ndp16 *cdc_ncm_ndp(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign, size_t reserve)
640 struct usb_cdc_ncm_ndp16 *ndp16 = NULL;
641 struct usb_cdc_ncm_nth16 *nth16 = (void *)skb->data;
642 size_t ndpoffset = le16_to_cpu(nth16->wNdpIndex);
644 /* follow the chain of NDPs, looking for a match */
646 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb->data + ndpoffset);
647 if (ndp16->dwSignature == sign)
649 ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
653 cdc_ncm_align_tail(skb, ctx->tx_ndp_modulus, 0, ctx->tx_max);
655 /* verify that there is room for the NDP and the datagram (reserve) */
656 if ((ctx->tx_max - skb->len - reserve) < CDC_NCM_NDP_SIZE)
661 ndp16->wNextNdpIndex = cpu_to_le16(skb->len);
663 nth16->wNdpIndex = cpu_to_le16(skb->len);
665 /* push a new empty NDP */
666 ndp16 = (struct usb_cdc_ncm_ndp16 *)memset(skb_put(skb, CDC_NCM_NDP_SIZE), 0, CDC_NCM_NDP_SIZE);
667 ndp16->dwSignature = sign;
668 ndp16->wLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_ndp16) + sizeof(struct usb_cdc_ncm_dpe16));
673 cdc_ncm_fill_tx_frame(struct usbnet *dev, struct sk_buff *skb, __le32 sign)
675 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
676 struct usb_cdc_ncm_nth16 *nth16;
677 struct usb_cdc_ncm_ndp16 *ndp16;
678 struct sk_buff *skb_out;
679 u16 n = 0, index, ndplen;
682 /* if there is a remaining skb, it gets priority */
684 swap(skb, ctx->tx_rem_skb);
685 swap(sign, ctx->tx_rem_sign);
690 /* check if we are resuming an OUT skb */
691 skb_out = ctx->tx_curr_skb;
693 /* allocate a new OUT skb */
695 skb_out = alloc_skb(ctx->tx_max, GFP_ATOMIC);
696 if (skb_out == NULL) {
698 dev_kfree_skb_any(skb);
699 dev->net->stats.tx_dropped++;
703 /* fill out the initial 16-bit NTB header */
704 nth16 = (struct usb_cdc_ncm_nth16 *)memset(skb_put(skb_out, sizeof(struct usb_cdc_ncm_nth16)), 0, sizeof(struct usb_cdc_ncm_nth16));
705 nth16->dwSignature = cpu_to_le32(USB_CDC_NCM_NTH16_SIGN);
706 nth16->wHeaderLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_nth16));
707 nth16->wSequence = cpu_to_le16(ctx->tx_seq++);
709 /* count total number of frames in this NTB */
710 ctx->tx_curr_frame_num = 0;
713 for (n = ctx->tx_curr_frame_num; n < ctx->tx_max_datagrams; n++) {
714 /* send any remaining skb first */
716 skb = ctx->tx_rem_skb;
717 sign = ctx->tx_rem_sign;
718 ctx->tx_rem_skb = NULL;
720 /* check for end of skb */
725 /* get the appropriate NDP for this skb */
726 ndp16 = cdc_ncm_ndp(ctx, skb_out, sign, skb->len + ctx->tx_modulus + ctx->tx_remainder);
728 /* align beginning of next frame */
729 cdc_ncm_align_tail(skb_out, ctx->tx_modulus, ctx->tx_remainder, ctx->tx_max);
731 /* check if we had enough room left for both NDP and frame */
732 if (!ndp16 || skb_out->len + skb->len > ctx->tx_max) {
734 /* won't fit, MTU problem? */
735 dev_kfree_skb_any(skb);
737 dev->net->stats.tx_dropped++;
739 /* no room for skb - store for later */
740 if (ctx->tx_rem_skb != NULL) {
741 dev_kfree_skb_any(ctx->tx_rem_skb);
742 dev->net->stats.tx_dropped++;
744 ctx->tx_rem_skb = skb;
745 ctx->tx_rem_sign = sign;
752 /* calculate frame number withing this NDP */
753 ndplen = le16_to_cpu(ndp16->wLength);
754 index = (ndplen - sizeof(struct usb_cdc_ncm_ndp16)) / sizeof(struct usb_cdc_ncm_dpe16) - 1;
756 /* OK, add this skb */
757 ndp16->dpe16[index].wDatagramLength = cpu_to_le16(skb->len);
758 ndp16->dpe16[index].wDatagramIndex = cpu_to_le16(skb_out->len);
759 ndp16->wLength = cpu_to_le16(ndplen + sizeof(struct usb_cdc_ncm_dpe16));
760 memcpy(skb_put(skb_out, skb->len), skb->data, skb->len);
761 dev_kfree_skb_any(skb);
764 /* send now if this NDP is full */
765 if (index >= CDC_NCM_DPT_DATAGRAMS_MAX) {
771 /* free up any dangling skb */
773 dev_kfree_skb_any(skb);
775 dev->net->stats.tx_dropped++;
778 ctx->tx_curr_frame_num = n;
781 /* wait for more frames */
783 ctx->tx_curr_skb = skb_out;
786 } else if ((n < ctx->tx_max_datagrams) && (ready2send == 0)) {
787 /* wait for more frames */
789 ctx->tx_curr_skb = skb_out;
790 /* set the pending count */
791 if (n < CDC_NCM_RESTART_TIMER_DATAGRAM_CNT)
792 ctx->tx_timer_pending = CDC_NCM_TIMER_PENDING_CNT;
797 /* variables will be reset at next call */
800 /* If collected data size is less or equal CDC_NCM_MIN_TX_PKT
801 * bytes, we send buffers as it is. If we get more data, it
802 * would be more efficient for USB HS mobile device with DMA
803 * engine to receive a full size NTB, than canceling DMA
804 * transfer and receiving a short packet.
806 * This optimization support is pointless if we end up sending
807 * a ZLP after full sized NTBs.
809 if (!(dev->driver_info->flags & FLAG_SEND_ZLP) &&
810 skb_out->len > CDC_NCM_MIN_TX_PKT)
811 memset(skb_put(skb_out, ctx->tx_max - skb_out->len), 0,
812 ctx->tx_max - skb_out->len);
813 else if (skb_out->len < ctx->tx_max && (skb_out->len % dev->maxpacket) == 0)
814 *skb_put(skb_out, 1) = 0; /* force short packet */
816 /* set final frame length */
817 nth16 = (struct usb_cdc_ncm_nth16 *)skb_out->data;
818 nth16->wBlockLength = cpu_to_le16(skb_out->len);
821 ctx->tx_curr_skb = NULL;
822 dev->net->stats.tx_packets += ctx->tx_curr_frame_num;
826 /* Start timer, if there is a remaining skb */
827 if (ctx->tx_curr_skb != NULL)
828 cdc_ncm_tx_timeout_start(ctx);
831 EXPORT_SYMBOL_GPL(cdc_ncm_fill_tx_frame);
833 static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx)
835 /* start timer, if not already started */
836 if (!(hrtimer_active(&ctx->tx_timer) || atomic_read(&ctx->stop)))
837 hrtimer_start(&ctx->tx_timer,
838 ktime_set(0, CDC_NCM_TIMER_INTERVAL),
842 static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *timer)
844 struct cdc_ncm_ctx *ctx =
845 container_of(timer, struct cdc_ncm_ctx, tx_timer);
847 if (!atomic_read(&ctx->stop))
848 tasklet_schedule(&ctx->bh);
849 return HRTIMER_NORESTART;
852 static void cdc_ncm_txpath_bh(unsigned long param)
854 struct usbnet *dev = (struct usbnet *)param;
855 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
857 spin_lock_bh(&ctx->mtx);
858 if (ctx->tx_timer_pending != 0) {
859 ctx->tx_timer_pending--;
860 cdc_ncm_tx_timeout_start(ctx);
861 spin_unlock_bh(&ctx->mtx);
862 } else if (dev->net != NULL) {
863 spin_unlock_bh(&ctx->mtx);
864 netif_tx_lock_bh(dev->net);
865 usbnet_start_xmit(NULL, dev->net);
866 netif_tx_unlock_bh(dev->net);
868 spin_unlock_bh(&ctx->mtx);
873 cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
875 struct sk_buff *skb_out;
876 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
879 * The Ethernet API we are using does not support transmitting
880 * multiple Ethernet frames in a single call. This driver will
881 * accumulate multiple Ethernet frames and send out a larger
882 * USB frame when the USB buffer is full or when a single jiffies
888 spin_lock_bh(&ctx->mtx);
889 skb_out = cdc_ncm_fill_tx_frame(dev, skb, cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN));
890 spin_unlock_bh(&ctx->mtx);
895 dev_kfree_skb_any(skb);
899 EXPORT_SYMBOL_GPL(cdc_ncm_tx_fixup);
901 /* verify NTB header and return offset of first NDP, or negative error */
902 int cdc_ncm_rx_verify_nth16(struct cdc_ncm_ctx *ctx, struct sk_buff *skb_in)
904 struct usbnet *dev = netdev_priv(skb_in->dev);
905 struct usb_cdc_ncm_nth16 *nth16;
912 if (skb_in->len < (sizeof(struct usb_cdc_ncm_nth16) +
913 sizeof(struct usb_cdc_ncm_ndp16))) {
914 netif_dbg(dev, rx_err, dev->net, "frame too short\n");
918 nth16 = (struct usb_cdc_ncm_nth16 *)skb_in->data;
920 if (nth16->dwSignature != cpu_to_le32(USB_CDC_NCM_NTH16_SIGN)) {
921 netif_dbg(dev, rx_err, dev->net,
922 "invalid NTH16 signature <%#010x>\n",
923 le32_to_cpu(nth16->dwSignature));
927 len = le16_to_cpu(nth16->wBlockLength);
928 if (len > ctx->rx_max) {
929 netif_dbg(dev, rx_err, dev->net,
930 "unsupported NTB block length %u/%u\n", len,
935 if ((ctx->rx_seq + 1) != le16_to_cpu(nth16->wSequence) &&
936 (ctx->rx_seq || le16_to_cpu(nth16->wSequence)) &&
937 !((ctx->rx_seq == 0xffff) && !le16_to_cpu(nth16->wSequence))) {
938 netif_dbg(dev, rx_err, dev->net,
939 "sequence number glitch prev=%d curr=%d\n",
940 ctx->rx_seq, le16_to_cpu(nth16->wSequence));
942 ctx->rx_seq = le16_to_cpu(nth16->wSequence);
944 ret = le16_to_cpu(nth16->wNdpIndex);
948 EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_nth16);
950 /* verify NDP header and return number of datagrams, or negative error */
951 int cdc_ncm_rx_verify_ndp16(struct sk_buff *skb_in, int ndpoffset)
953 struct usbnet *dev = netdev_priv(skb_in->dev);
954 struct usb_cdc_ncm_ndp16 *ndp16;
957 if ((ndpoffset + sizeof(struct usb_cdc_ncm_ndp16)) > skb_in->len) {
958 netif_dbg(dev, rx_err, dev->net, "invalid NDP offset <%u>\n",
962 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
964 if (le16_to_cpu(ndp16->wLength) < USB_CDC_NCM_NDP16_LENGTH_MIN) {
965 netif_dbg(dev, rx_err, dev->net, "invalid DPT16 length <%u>\n",
966 le16_to_cpu(ndp16->wLength));
970 ret = ((le16_to_cpu(ndp16->wLength) -
971 sizeof(struct usb_cdc_ncm_ndp16)) /
972 sizeof(struct usb_cdc_ncm_dpe16));
973 ret--; /* we process NDP entries except for the last one */
975 if ((sizeof(struct usb_cdc_ncm_ndp16) +
976 ret * (sizeof(struct usb_cdc_ncm_dpe16))) > skb_in->len) {
977 netif_dbg(dev, rx_err, dev->net, "Invalid nframes = %d\n", ret);
984 EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_ndp16);
986 int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
989 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
994 struct usb_cdc_ncm_ndp16 *ndp16;
995 struct usb_cdc_ncm_dpe16 *dpe16;
997 int loopcount = 50; /* arbitrary max preventing infinite loop */
999 ndpoffset = cdc_ncm_rx_verify_nth16(ctx, skb_in);
1004 nframes = cdc_ncm_rx_verify_ndp16(skb_in, ndpoffset);
1008 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
1010 if (ndp16->dwSignature != cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN)) {
1011 netif_dbg(dev, rx_err, dev->net,
1012 "invalid DPT16 signature <%#010x>\n",
1013 le32_to_cpu(ndp16->dwSignature));
1016 dpe16 = ndp16->dpe16;
1018 for (x = 0; x < nframes; x++, dpe16++) {
1019 offset = le16_to_cpu(dpe16->wDatagramIndex);
1020 len = le16_to_cpu(dpe16->wDatagramLength);
1024 * All entries after first NULL entry are to be ignored
1026 if ((offset == 0) || (len == 0)) {
1028 goto err_ndp; /* empty NTB */
1032 /* sanity checking */
1033 if (((offset + len) > skb_in->len) ||
1034 (len > ctx->rx_max) || (len < ETH_HLEN)) {
1035 netif_dbg(dev, rx_err, dev->net,
1036 "invalid frame detected (ignored) offset[%u]=%u, length=%u, skb=%p\n",
1037 x, offset, len, skb_in);
1043 skb = skb_clone(skb_in, GFP_ATOMIC);
1047 skb->data = ((u8 *)skb_in->data) + offset;
1048 skb_set_tail_pointer(skb, len);
1049 usbnet_skb_return(dev, skb);
1053 /* are there more NDPs to process? */
1054 ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
1055 if (ndpoffset && loopcount--)
1062 EXPORT_SYMBOL_GPL(cdc_ncm_rx_fixup);
1065 cdc_ncm_speed_change(struct usbnet *dev,
1066 struct usb_cdc_speed_change *data)
1068 uint32_t rx_speed = le32_to_cpu(data->DLBitRRate);
1069 uint32_t tx_speed = le32_to_cpu(data->ULBitRate);
1072 * Currently the USB-NET API does not support reporting the actual
1073 * device speed. Do print it instead.
1075 if ((tx_speed > 1000000) && (rx_speed > 1000000)) {
1076 netif_info(dev, link, dev->net,
1077 "%u mbit/s downlink %u mbit/s uplink\n",
1078 (unsigned int)(rx_speed / 1000000U),
1079 (unsigned int)(tx_speed / 1000000U));
1081 netif_info(dev, link, dev->net,
1082 "%u kbit/s downlink %u kbit/s uplink\n",
1083 (unsigned int)(rx_speed / 1000U),
1084 (unsigned int)(tx_speed / 1000U));
1088 static void cdc_ncm_status(struct usbnet *dev, struct urb *urb)
1090 struct cdc_ncm_ctx *ctx;
1091 struct usb_cdc_notification *event;
1093 ctx = (struct cdc_ncm_ctx *)dev->data[0];
1095 if (urb->actual_length < sizeof(*event))
1098 /* test for split data in 8-byte chunks */
1099 if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
1100 cdc_ncm_speed_change(dev,
1101 (struct usb_cdc_speed_change *)urb->transfer_buffer);
1105 event = urb->transfer_buffer;
1107 switch (event->bNotificationType) {
1108 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
1110 * According to the CDC NCM specification ch.7.1
1111 * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be
1112 * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE.
1114 ctx->connected = le16_to_cpu(event->wValue);
1115 netif_info(dev, link, dev->net,
1116 "network connection: %sconnected\n",
1117 ctx->connected ? "" : "dis");
1118 usbnet_link_change(dev, ctx->connected, 0);
1121 case USB_CDC_NOTIFY_SPEED_CHANGE:
1122 if (urb->actual_length < (sizeof(*event) +
1123 sizeof(struct usb_cdc_speed_change)))
1124 set_bit(EVENT_STS_SPLIT, &dev->flags);
1126 cdc_ncm_speed_change(dev,
1127 (struct usb_cdc_speed_change *)&event[1]);
1131 dev_dbg(&dev->udev->dev,
1132 "NCM: unexpected notification 0x%02x!\n",
1133 event->bNotificationType);
1138 static int cdc_ncm_check_connect(struct usbnet *dev)
1140 struct cdc_ncm_ctx *ctx;
1142 ctx = (struct cdc_ncm_ctx *)dev->data[0];
1144 return 1; /* disconnected */
1146 return !ctx->connected;
1149 static const struct driver_info cdc_ncm_info = {
1150 .description = "CDC NCM",
1151 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET,
1152 .bind = cdc_ncm_bind,
1153 .unbind = cdc_ncm_unbind,
1154 .check_connect = cdc_ncm_check_connect,
1155 .manage_power = usbnet_manage_power,
1156 .status = cdc_ncm_status,
1157 .rx_fixup = cdc_ncm_rx_fixup,
1158 .tx_fixup = cdc_ncm_tx_fixup,
1161 /* Same as cdc_ncm_info, but with FLAG_WWAN */
1162 static const struct driver_info wwan_info = {
1163 .description = "Mobile Broadband Network Device",
1164 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
1166 .bind = cdc_ncm_bind,
1167 .unbind = cdc_ncm_unbind,
1168 .check_connect = cdc_ncm_check_connect,
1169 .manage_power = usbnet_manage_power,
1170 .status = cdc_ncm_status,
1171 .rx_fixup = cdc_ncm_rx_fixup,
1172 .tx_fixup = cdc_ncm_tx_fixup,
1175 /* Same as wwan_info, but with FLAG_NOARP */
1176 static const struct driver_info wwan_noarp_info = {
1177 .description = "Mobile Broadband Network Device (NO ARP)",
1178 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
1179 | FLAG_WWAN | FLAG_NOARP,
1180 .bind = cdc_ncm_bind,
1181 .unbind = cdc_ncm_unbind,
1182 .check_connect = cdc_ncm_check_connect,
1183 .manage_power = usbnet_manage_power,
1184 .status = cdc_ncm_status,
1185 .rx_fixup = cdc_ncm_rx_fixup,
1186 .tx_fixup = cdc_ncm_tx_fixup,
1189 static const struct usb_device_id cdc_devs[] = {
1190 /* Ericsson MBM devices like F5521gw */
1191 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1192 | USB_DEVICE_ID_MATCH_VENDOR,
1194 .bInterfaceClass = USB_CLASS_COMM,
1195 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1196 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1197 .driver_info = (unsigned long) &wwan_info,
1200 /* Dell branded MBM devices like DW5550 */
1201 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1202 | USB_DEVICE_ID_MATCH_VENDOR,
1204 .bInterfaceClass = USB_CLASS_COMM,
1205 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1206 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1207 .driver_info = (unsigned long) &wwan_info,
1210 /* Toshiba branded MBM devices */
1211 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1212 | USB_DEVICE_ID_MATCH_VENDOR,
1214 .bInterfaceClass = USB_CLASS_COMM,
1215 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1216 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1217 .driver_info = (unsigned long) &wwan_info,
1220 /* tag Huawei devices as wwan */
1221 { USB_VENDOR_AND_INTERFACE_INFO(0x12d1,
1223 USB_CDC_SUBCLASS_NCM,
1224 USB_CDC_PROTO_NONE),
1225 .driver_info = (unsigned long)&wwan_info,
1228 /* Infineon(now Intel) HSPA Modem platform */
1229 { USB_DEVICE_AND_INTERFACE_INFO(0x1519, 0x0443,
1231 USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
1232 .driver_info = (unsigned long)&wwan_noarp_info,
1235 /* Generic CDC-NCM devices */
1236 { USB_INTERFACE_INFO(USB_CLASS_COMM,
1237 USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
1238 .driver_info = (unsigned long)&cdc_ncm_info,
1243 MODULE_DEVICE_TABLE(usb, cdc_devs);
1245 static struct usb_driver cdc_ncm_driver = {
1247 .id_table = cdc_devs,
1248 .probe = usbnet_probe,
1249 .disconnect = usbnet_disconnect,
1250 .suspend = usbnet_suspend,
1251 .resume = usbnet_resume,
1252 .reset_resume = usbnet_resume,
1253 .supports_autosuspend = 1,
1254 .disable_hub_initiated_lpm = 1,
1257 module_usb_driver(cdc_ncm_driver);
1259 MODULE_AUTHOR("Hans Petter Selasky");
1260 MODULE_DESCRIPTION("USB CDC NCM host driver");
1261 MODULE_LICENSE("Dual BSD/GPL");