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/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>
50 #include <linux/hrtimer.h>
51 #include <linux/atomic.h>
52 #include <linux/usb/usbnet.h>
53 #include <linux/usb/cdc.h>
54 #include <linux/usb/cdc_ncm.h>
56 #if IS_ENABLED(CONFIG_USB_NET_CDC_MBIM)
57 static bool prefer_mbim = true;
59 static bool prefer_mbim;
61 module_param(prefer_mbim, bool, S_IRUGO | S_IWUSR);
62 MODULE_PARM_DESC(prefer_mbim, "Prefer MBIM setting on dual NCM/MBIM functions");
64 static void cdc_ncm_txpath_bh(unsigned long param);
65 static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx);
66 static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *hr_timer);
67 static struct usb_driver cdc_ncm_driver;
69 static u8 cdc_ncm_setup(struct usbnet *dev)
71 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
72 struct usb_cdc_ncm_ntb_parameters ncm_parm;
78 u16 ntb_fmt_supported;
82 iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber;
84 err = usbnet_read_cmd(dev, USB_CDC_GET_NTB_PARAMETERS,
85 USB_TYPE_CLASS | USB_DIR_IN
87 0, iface_no, &ncm_parm,
90 pr_debug("failed GET_NTB_PARAMETERS\n");
94 /* read correct set of parameters according to device mode */
95 ctx->rx_max = le32_to_cpu(ncm_parm.dwNtbInMaxSize);
96 ctx->tx_max = le32_to_cpu(ncm_parm.dwNtbOutMaxSize);
97 ctx->tx_remainder = le16_to_cpu(ncm_parm.wNdpOutPayloadRemainder);
98 ctx->tx_modulus = le16_to_cpu(ncm_parm.wNdpOutDivisor);
99 ctx->tx_ndp_modulus = le16_to_cpu(ncm_parm.wNdpOutAlignment);
100 /* devices prior to NCM Errata shall set this field to zero */
101 ctx->tx_max_datagrams = le16_to_cpu(ncm_parm.wNtbOutMaxDatagrams);
102 ntb_fmt_supported = le16_to_cpu(ncm_parm.bmNtbFormatsSupported);
105 min_dgram_size = CDC_NCM_MIN_DATAGRAM_SIZE;
106 min_hdr_size = CDC_NCM_MIN_HDR_SIZE;
107 if (ctx->mbim_desc != NULL) {
108 flags = ctx->mbim_desc->bmNetworkCapabilities;
110 min_dgram_size = CDC_MBIM_MIN_DATAGRAM_SIZE;
112 } else if (ctx->func_desc != NULL) {
113 flags = ctx->func_desc->bmNetworkCapabilities;
118 pr_debug("dwNtbInMaxSize=%u dwNtbOutMaxSize=%u "
119 "wNdpOutPayloadRemainder=%u wNdpOutDivisor=%u "
120 "wNdpOutAlignment=%u wNtbOutMaxDatagrams=%u flags=0x%x\n",
121 ctx->rx_max, ctx->tx_max, ctx->tx_remainder, ctx->tx_modulus,
122 ctx->tx_ndp_modulus, ctx->tx_max_datagrams, flags);
124 /* max count of tx datagrams */
125 if ((ctx->tx_max_datagrams == 0) ||
126 (ctx->tx_max_datagrams > CDC_NCM_DPT_DATAGRAMS_MAX))
127 ctx->tx_max_datagrams = CDC_NCM_DPT_DATAGRAMS_MAX;
129 /* verify maximum size of received NTB in bytes */
130 if (ctx->rx_max < USB_CDC_NCM_NTB_MIN_IN_SIZE) {
131 pr_debug("Using min receive length=%d\n",
132 USB_CDC_NCM_NTB_MIN_IN_SIZE);
133 ctx->rx_max = USB_CDC_NCM_NTB_MIN_IN_SIZE;
136 if (ctx->rx_max > CDC_NCM_NTB_MAX_SIZE_RX) {
137 pr_debug("Using default maximum receive length=%d\n",
138 CDC_NCM_NTB_MAX_SIZE_RX);
139 ctx->rx_max = CDC_NCM_NTB_MAX_SIZE_RX;
142 /* inform device about NTB input size changes */
143 if (ctx->rx_max != le32_to_cpu(ncm_parm.dwNtbInMaxSize)) {
144 __le32 dwNtbInMaxSize = cpu_to_le32(ctx->rx_max);
146 err = usbnet_write_cmd(dev, USB_CDC_SET_NTB_INPUT_SIZE,
147 USB_TYPE_CLASS | USB_DIR_OUT
148 | USB_RECIP_INTERFACE,
149 0, iface_no, &dwNtbInMaxSize, 4);
151 pr_debug("Setting NTB Input Size failed\n");
154 /* verify maximum size of transmitted NTB in bytes */
156 (min_hdr_size + min_dgram_size)) ||
157 (ctx->tx_max > CDC_NCM_NTB_MAX_SIZE_TX)) {
158 pr_debug("Using default maximum transmit length=%d\n",
159 CDC_NCM_NTB_MAX_SIZE_TX);
160 ctx->tx_max = CDC_NCM_NTB_MAX_SIZE_TX;
162 /* Adding a pad byte here simplifies the handling in
163 * cdc_ncm_fill_tx_frame, by making tx_max always
164 * represent the real skb max size.
166 if (ctx->tx_max % usb_maxpacket(dev->udev, dev->out, 1) == 0)
172 * verify that the structure alignment is:
174 * - not greater than the maximum transmit length
175 * - not less than four bytes
177 val = ctx->tx_ndp_modulus;
179 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
180 (val != ((-val) & val)) || (val >= ctx->tx_max)) {
181 pr_debug("Using default alignment: 4 bytes\n");
182 ctx->tx_ndp_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
186 * verify that the payload alignment is:
188 * - not greater than the maximum transmit length
189 * - not less than four bytes
191 val = ctx->tx_modulus;
193 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
194 (val != ((-val) & val)) || (val >= ctx->tx_max)) {
195 pr_debug("Using default transmit modulus: 4 bytes\n");
196 ctx->tx_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
199 /* verify the payload remainder */
200 if (ctx->tx_remainder >= ctx->tx_modulus) {
201 pr_debug("Using default transmit remainder: 0 bytes\n");
202 ctx->tx_remainder = 0;
205 /* adjust TX-remainder according to NCM specification. */
206 ctx->tx_remainder = ((ctx->tx_remainder - eth_hlen) &
207 (ctx->tx_modulus - 1));
209 /* additional configuration */
212 if (flags & USB_CDC_NCM_NCAP_CRC_MODE) {
213 err = usbnet_write_cmd(dev, USB_CDC_SET_CRC_MODE,
214 USB_TYPE_CLASS | USB_DIR_OUT
215 | USB_RECIP_INTERFACE,
216 USB_CDC_NCM_CRC_NOT_APPENDED,
219 pr_debug("Setting CRC mode off failed\n");
222 /* set NTB format, if both formats are supported */
223 if (ntb_fmt_supported & USB_CDC_NCM_NTH32_SIGN) {
224 err = usbnet_write_cmd(dev, USB_CDC_SET_NTB_FORMAT,
225 USB_TYPE_CLASS | USB_DIR_OUT
226 | USB_RECIP_INTERFACE,
227 USB_CDC_NCM_NTB16_FORMAT,
230 pr_debug("Setting NTB format to 16-bit failed\n");
233 ctx->max_datagram_size = min_dgram_size;
235 /* set Max Datagram Size (MTU) */
236 if (flags & USB_CDC_NCM_NCAP_MAX_DATAGRAM_SIZE) {
237 __le16 max_datagram_size;
239 if (ctx->ether_desc != NULL)
240 eth_max_sz = le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
241 else if (ctx->mbim_desc != NULL)
242 eth_max_sz = le16_to_cpu(ctx->mbim_desc->wMaxSegmentSize);
246 err = usbnet_read_cmd(dev, USB_CDC_GET_MAX_DATAGRAM_SIZE,
247 USB_TYPE_CLASS | USB_DIR_IN
248 | USB_RECIP_INTERFACE,
249 0, iface_no, &max_datagram_size, 2);
251 pr_debug("GET_MAX_DATAGRAM_SIZE failed, use size=%u\n",
254 ctx->max_datagram_size =
255 le16_to_cpu(max_datagram_size);
256 /* Check Eth descriptor value */
257 if (ctx->max_datagram_size > eth_max_sz)
258 ctx->max_datagram_size = eth_max_sz;
260 if (ctx->max_datagram_size > CDC_NCM_MAX_DATAGRAM_SIZE)
261 ctx->max_datagram_size = CDC_NCM_MAX_DATAGRAM_SIZE;
263 if (ctx->max_datagram_size < min_dgram_size)
264 ctx->max_datagram_size = min_dgram_size;
266 /* if value changed, update device */
267 if (ctx->max_datagram_size !=
268 le16_to_cpu(max_datagram_size)) {
269 max_datagram_size = cpu_to_le16(ctx->max_datagram_size);
270 err = usbnet_write_cmd(dev,
271 USB_CDC_SET_MAX_DATAGRAM_SIZE,
272 USB_TYPE_CLASS | USB_DIR_OUT
273 | USB_RECIP_INTERFACE,
275 iface_no, &max_datagram_size,
278 pr_debug("SET_MAX_DGRAM_SIZE failed\n");
284 if (dev->net->mtu != (ctx->max_datagram_size - eth_hlen))
285 dev->net->mtu = ctx->max_datagram_size - eth_hlen;
291 cdc_ncm_find_endpoints(struct usbnet *dev, struct usb_interface *intf)
293 struct usb_host_endpoint *e, *in = NULL, *out = NULL;
296 for (ep = 0; ep < intf->cur_altsetting->desc.bNumEndpoints; ep++) {
298 e = intf->cur_altsetting->endpoint + ep;
299 switch (e->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
300 case USB_ENDPOINT_XFER_INT:
301 if (usb_endpoint_dir_in(&e->desc)) {
307 case USB_ENDPOINT_XFER_BULK:
308 if (usb_endpoint_dir_in(&e->desc)) {
322 dev->in = usb_rcvbulkpipe(dev->udev,
323 in->desc.bEndpointAddress &
324 USB_ENDPOINT_NUMBER_MASK);
325 if (out && !dev->out)
326 dev->out = usb_sndbulkpipe(dev->udev,
327 out->desc.bEndpointAddress &
328 USB_ENDPOINT_NUMBER_MASK);
331 static void cdc_ncm_free(struct cdc_ncm_ctx *ctx)
336 if (ctx->tx_rem_skb != NULL) {
337 dev_kfree_skb_any(ctx->tx_rem_skb);
338 ctx->tx_rem_skb = NULL;
341 if (ctx->tx_curr_skb != NULL) {
342 dev_kfree_skb_any(ctx->tx_curr_skb);
343 ctx->tx_curr_skb = NULL;
349 int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_altsetting)
351 const struct usb_cdc_union_desc *union_desc = NULL;
352 struct cdc_ncm_ctx *ctx;
353 struct usb_driver *driver;
359 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
363 hrtimer_init(&ctx->tx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
364 ctx->tx_timer.function = &cdc_ncm_tx_timer_cb;
365 ctx->bh.data = (unsigned long)dev;
366 ctx->bh.func = cdc_ncm_txpath_bh;
367 atomic_set(&ctx->stop, 0);
368 spin_lock_init(&ctx->mtx);
370 /* store ctx pointer in device data field */
371 dev->data[0] = (unsigned long)ctx;
373 /* only the control interface can be successfully probed */
376 /* get some pointers */
377 driver = driver_of(intf);
378 buf = intf->cur_altsetting->extra;
379 len = intf->cur_altsetting->extralen;
381 /* parse through descriptors associated with control interface */
382 while ((len > 0) && (buf[0] > 2) && (buf[0] <= len)) {
384 if (buf[1] != USB_DT_CS_INTERFACE)
388 case USB_CDC_UNION_TYPE:
389 if (buf[0] < sizeof(*union_desc))
392 union_desc = (const struct usb_cdc_union_desc *)buf;
393 /* the master must be the interface we are probing */
394 if (intf->cur_altsetting->desc.bInterfaceNumber !=
395 union_desc->bMasterInterface0)
397 ctx->data = usb_ifnum_to_if(dev->udev,
398 union_desc->bSlaveInterface0);
401 case USB_CDC_ETHERNET_TYPE:
402 if (buf[0] < sizeof(*(ctx->ether_desc)))
406 (const struct usb_cdc_ether_desc *)buf;
409 case USB_CDC_NCM_TYPE:
410 if (buf[0] < sizeof(*(ctx->func_desc)))
413 ctx->func_desc = (const struct usb_cdc_ncm_desc *)buf;
416 case USB_CDC_MBIM_TYPE:
417 if (buf[0] < sizeof(*(ctx->mbim_desc)))
420 ctx->mbim_desc = (const struct usb_cdc_mbim_desc *)buf;
427 /* advance to next descriptor */
433 /* some buggy devices have an IAD but no CDC Union */
434 if (!union_desc && intf->intf_assoc && intf->intf_assoc->bInterfaceCount == 2) {
435 ctx->data = usb_ifnum_to_if(dev->udev, intf->cur_altsetting->desc.bInterfaceNumber + 1);
436 dev_dbg(&intf->dev, "CDC Union missing - got slave from IAD\n");
439 /* check if we got everything */
440 if (!ctx->data || (!ctx->mbim_desc && !ctx->ether_desc))
443 /* claim data interface, if different from control */
444 if (ctx->data != ctx->control) {
445 temp = usb_driver_claim_interface(driver, ctx->data, dev);
450 iface_no = ctx->data->cur_altsetting->desc.bInterfaceNumber;
452 /* reset data interface */
453 temp = usb_set_interface(dev->udev, iface_no, 0);
457 /* configure data interface */
458 temp = usb_set_interface(dev->udev, iface_no, data_altsetting);
462 cdc_ncm_find_endpoints(dev, ctx->data);
463 cdc_ncm_find_endpoints(dev, ctx->control);
464 if (!dev->in || !dev->out || !dev->status)
467 /* initialize data interface */
468 if (cdc_ncm_setup(dev))
471 usb_set_intfdata(ctx->data, dev);
472 usb_set_intfdata(ctx->control, dev);
474 if (ctx->ether_desc) {
475 temp = usbnet_get_ethernet_addr(dev, ctx->ether_desc->iMACAddress);
478 dev_info(&dev->udev->dev, "MAC-Address: %pM\n", dev->net->dev_addr);
481 /* usbnet use these values for sizing tx/rx queues */
482 dev->hard_mtu = ctx->tx_max;
483 dev->rx_urb_size = ctx->rx_max;
488 usb_set_intfdata(ctx->control, NULL);
489 usb_set_intfdata(ctx->data, NULL);
490 if (ctx->data != ctx->control)
491 usb_driver_release_interface(driver, ctx->data);
493 cdc_ncm_free((struct cdc_ncm_ctx *)dev->data[0]);
495 dev_info(&dev->udev->dev, "bind() failure\n");
498 EXPORT_SYMBOL_GPL(cdc_ncm_bind_common);
500 void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf)
502 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
503 struct usb_driver *driver = driver_of(intf);
506 return; /* no setup */
508 atomic_set(&ctx->stop, 1);
510 if (hrtimer_active(&ctx->tx_timer))
511 hrtimer_cancel(&ctx->tx_timer);
513 tasklet_kill(&ctx->bh);
515 /* handle devices with combined control and data interface */
516 if (ctx->control == ctx->data)
519 /* disconnect master --> disconnect slave */
520 if (intf == ctx->control && ctx->data) {
521 usb_set_intfdata(ctx->data, NULL);
522 usb_driver_release_interface(driver, ctx->data);
525 } else if (intf == ctx->data && ctx->control) {
526 usb_set_intfdata(ctx->control, NULL);
527 usb_driver_release_interface(driver, ctx->control);
531 usb_set_intfdata(intf, NULL);
534 EXPORT_SYMBOL_GPL(cdc_ncm_unbind);
536 /* Select the MBIM altsetting iff it is preferred and available,
537 * returning the number of the corresponding data interface altsetting
539 u8 cdc_ncm_select_altsetting(struct usbnet *dev, struct usb_interface *intf)
541 struct usb_host_interface *alt;
543 /* The MBIM spec defines a NCM compatible default altsetting,
544 * which we may have matched:
546 * "Functions that implement both NCM 1.0 and MBIM (an
547 * “NCM/MBIM function”) according to this recommendation
548 * shall provide two alternate settings for the
549 * Communication Interface. Alternate setting 0, and the
550 * associated class and endpoint descriptors, shall be
551 * constructed according to the rules given for the
552 * Communication Interface in section 5 of [USBNCM10].
553 * Alternate setting 1, and the associated class and
554 * endpoint descriptors, shall be constructed according to
555 * the rules given in section 6 (USB Device Model) of this
558 if (prefer_mbim && intf->num_altsetting == 2) {
559 alt = usb_altnum_to_altsetting(intf, CDC_NCM_COMM_ALTSETTING_MBIM);
560 if (alt && cdc_ncm_comm_intf_is_mbim(alt) &&
561 !usb_set_interface(dev->udev,
562 intf->cur_altsetting->desc.bInterfaceNumber,
563 CDC_NCM_COMM_ALTSETTING_MBIM))
564 return CDC_NCM_DATA_ALTSETTING_MBIM;
566 return CDC_NCM_DATA_ALTSETTING_NCM;
568 EXPORT_SYMBOL_GPL(cdc_ncm_select_altsetting);
570 static int cdc_ncm_bind(struct usbnet *dev, struct usb_interface *intf)
574 /* MBIM backwards compatible function? */
575 cdc_ncm_select_altsetting(dev, intf);
576 if (cdc_ncm_comm_intf_is_mbim(intf->cur_altsetting))
579 /* NCM data altsetting is always 1 */
580 ret = cdc_ncm_bind_common(dev, intf, 1);
583 * We should get an event when network connection is "connected" or
584 * "disconnected". Set network connection in "disconnected" state
585 * (carrier is OFF) during attach, so the IP network stack does not
586 * start IPv6 negotiation and more.
588 usbnet_link_change(dev, 0, 0);
592 static void cdc_ncm_align_tail(struct sk_buff *skb, size_t modulus, size_t remainder, size_t max)
594 size_t align = ALIGN(skb->len, modulus) - skb->len + remainder;
596 if (skb->len + align > max)
597 align = max - skb->len;
598 if (align && skb_tailroom(skb) >= align)
599 memset(skb_put(skb, align), 0, align);
602 /* return a pointer to a valid struct usb_cdc_ncm_ndp16 of type sign, possibly
603 * allocating a new one within skb
605 static struct usb_cdc_ncm_ndp16 *cdc_ncm_ndp(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign, size_t reserve)
607 struct usb_cdc_ncm_ndp16 *ndp16 = NULL;
608 struct usb_cdc_ncm_nth16 *nth16 = (void *)skb->data;
609 size_t ndpoffset = le16_to_cpu(nth16->wNdpIndex);
611 /* follow the chain of NDPs, looking for a match */
613 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb->data + ndpoffset);
614 if (ndp16->dwSignature == sign)
616 ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
620 cdc_ncm_align_tail(skb, ctx->tx_ndp_modulus, 0, ctx->tx_max);
622 /* verify that there is room for the NDP and the datagram (reserve) */
623 if ((ctx->tx_max - skb->len - reserve) < CDC_NCM_NDP_SIZE)
628 ndp16->wNextNdpIndex = cpu_to_le16(skb->len);
630 nth16->wNdpIndex = cpu_to_le16(skb->len);
632 /* push a new empty NDP */
633 ndp16 = (struct usb_cdc_ncm_ndp16 *)memset(skb_put(skb, CDC_NCM_NDP_SIZE), 0, CDC_NCM_NDP_SIZE);
634 ndp16->dwSignature = sign;
635 ndp16->wLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_ndp16) + sizeof(struct usb_cdc_ncm_dpe16));
640 cdc_ncm_fill_tx_frame(struct usbnet *dev, struct sk_buff *skb, __le32 sign)
642 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
643 struct usb_cdc_ncm_nth16 *nth16;
644 struct usb_cdc_ncm_ndp16 *ndp16;
645 struct sk_buff *skb_out;
646 u16 n = 0, index, ndplen;
649 /* if there is a remaining skb, it gets priority */
651 swap(skb, ctx->tx_rem_skb);
652 swap(sign, ctx->tx_rem_sign);
657 /* check if we are resuming an OUT skb */
658 skb_out = ctx->tx_curr_skb;
660 /* allocate a new OUT skb */
662 skb_out = alloc_skb(ctx->tx_max, GFP_ATOMIC);
663 if (skb_out == NULL) {
665 dev_kfree_skb_any(skb);
666 dev->net->stats.tx_dropped++;
670 /* fill out the initial 16-bit NTB header */
671 nth16 = (struct usb_cdc_ncm_nth16 *)memset(skb_put(skb_out, sizeof(struct usb_cdc_ncm_nth16)), 0, sizeof(struct usb_cdc_ncm_nth16));
672 nth16->dwSignature = cpu_to_le32(USB_CDC_NCM_NTH16_SIGN);
673 nth16->wHeaderLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_nth16));
674 nth16->wSequence = cpu_to_le16(ctx->tx_seq++);
676 /* count total number of frames in this NTB */
677 ctx->tx_curr_frame_num = 0;
680 for (n = ctx->tx_curr_frame_num; n < ctx->tx_max_datagrams; n++) {
681 /* send any remaining skb first */
683 skb = ctx->tx_rem_skb;
684 sign = ctx->tx_rem_sign;
685 ctx->tx_rem_skb = NULL;
687 /* check for end of skb */
692 /* get the appropriate NDP for this skb */
693 ndp16 = cdc_ncm_ndp(ctx, skb_out, sign, skb->len + ctx->tx_modulus + ctx->tx_remainder);
695 /* align beginning of next frame */
696 cdc_ncm_align_tail(skb_out, ctx->tx_modulus, ctx->tx_remainder, ctx->tx_max);
698 /* check if we had enough room left for both NDP and frame */
699 if (!ndp16 || skb_out->len + skb->len > ctx->tx_max) {
701 /* won't fit, MTU problem? */
702 dev_kfree_skb_any(skb);
704 dev->net->stats.tx_dropped++;
706 /* no room for skb - store for later */
707 if (ctx->tx_rem_skb != NULL) {
708 dev_kfree_skb_any(ctx->tx_rem_skb);
709 dev->net->stats.tx_dropped++;
711 ctx->tx_rem_skb = skb;
712 ctx->tx_rem_sign = sign;
719 /* calculate frame number withing this NDP */
720 ndplen = le16_to_cpu(ndp16->wLength);
721 index = (ndplen - sizeof(struct usb_cdc_ncm_ndp16)) / sizeof(struct usb_cdc_ncm_dpe16) - 1;
723 /* OK, add this skb */
724 ndp16->dpe16[index].wDatagramLength = cpu_to_le16(skb->len);
725 ndp16->dpe16[index].wDatagramIndex = cpu_to_le16(skb_out->len);
726 ndp16->wLength = cpu_to_le16(ndplen + sizeof(struct usb_cdc_ncm_dpe16));
727 memcpy(skb_put(skb_out, skb->len), skb->data, skb->len);
728 dev_kfree_skb_any(skb);
731 /* send now if this NDP is full */
732 if (index >= CDC_NCM_DPT_DATAGRAMS_MAX) {
738 /* free up any dangling skb */
740 dev_kfree_skb_any(skb);
742 dev->net->stats.tx_dropped++;
745 ctx->tx_curr_frame_num = n;
748 /* wait for more frames */
750 ctx->tx_curr_skb = skb_out;
753 } else if ((n < ctx->tx_max_datagrams) && (ready2send == 0)) {
754 /* wait for more frames */
756 ctx->tx_curr_skb = skb_out;
757 /* set the pending count */
758 if (n < CDC_NCM_RESTART_TIMER_DATAGRAM_CNT)
759 ctx->tx_timer_pending = CDC_NCM_TIMER_PENDING_CNT;
764 /* variables will be reset at next call */
767 /* If collected data size is less or equal CDC_NCM_MIN_TX_PKT
768 * bytes, we send buffers as it is. If we get more data, it
769 * would be more efficient for USB HS mobile device with DMA
770 * engine to receive a full size NTB, than canceling DMA
771 * transfer and receiving a short packet.
773 * This optimization support is pointless if we end up sending
774 * a ZLP after full sized NTBs.
776 if (!(dev->driver_info->flags & FLAG_SEND_ZLP) &&
777 skb_out->len > CDC_NCM_MIN_TX_PKT)
778 memset(skb_put(skb_out, ctx->tx_max - skb_out->len), 0,
779 ctx->tx_max - skb_out->len);
780 else if ((skb_out->len % dev->maxpacket) == 0)
781 *skb_put(skb_out, 1) = 0; /* force short packet */
783 /* set final frame length */
784 nth16 = (struct usb_cdc_ncm_nth16 *)skb_out->data;
785 nth16->wBlockLength = cpu_to_le16(skb_out->len);
788 ctx->tx_curr_skb = NULL;
789 dev->net->stats.tx_packets += ctx->tx_curr_frame_num;
793 /* Start timer, if there is a remaining skb */
794 if (ctx->tx_curr_skb != NULL)
795 cdc_ncm_tx_timeout_start(ctx);
798 EXPORT_SYMBOL_GPL(cdc_ncm_fill_tx_frame);
800 static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx)
802 /* start timer, if not already started */
803 if (!(hrtimer_active(&ctx->tx_timer) || atomic_read(&ctx->stop)))
804 hrtimer_start(&ctx->tx_timer,
805 ktime_set(0, CDC_NCM_TIMER_INTERVAL),
809 static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *timer)
811 struct cdc_ncm_ctx *ctx =
812 container_of(timer, struct cdc_ncm_ctx, tx_timer);
814 if (!atomic_read(&ctx->stop))
815 tasklet_schedule(&ctx->bh);
816 return HRTIMER_NORESTART;
819 static void cdc_ncm_txpath_bh(unsigned long param)
821 struct usbnet *dev = (struct usbnet *)param;
822 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
824 spin_lock_bh(&ctx->mtx);
825 if (ctx->tx_timer_pending != 0) {
826 ctx->tx_timer_pending--;
827 cdc_ncm_tx_timeout_start(ctx);
828 spin_unlock_bh(&ctx->mtx);
829 } else if (dev->net != NULL) {
830 spin_unlock_bh(&ctx->mtx);
831 netif_tx_lock_bh(dev->net);
832 usbnet_start_xmit(NULL, dev->net);
833 netif_tx_unlock_bh(dev->net);
835 spin_unlock_bh(&ctx->mtx);
839 static struct sk_buff *
840 cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
842 struct sk_buff *skb_out;
843 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
846 * The Ethernet API we are using does not support transmitting
847 * multiple Ethernet frames in a single call. This driver will
848 * accumulate multiple Ethernet frames and send out a larger
849 * USB frame when the USB buffer is full or when a single jiffies
855 spin_lock_bh(&ctx->mtx);
856 skb_out = cdc_ncm_fill_tx_frame(dev, skb, cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN));
857 spin_unlock_bh(&ctx->mtx);
862 dev_kfree_skb_any(skb);
867 /* verify NTB header and return offset of first NDP, or negative error */
868 int cdc_ncm_rx_verify_nth16(struct cdc_ncm_ctx *ctx, struct sk_buff *skb_in)
870 struct usb_cdc_ncm_nth16 *nth16;
877 if (skb_in->len < (sizeof(struct usb_cdc_ncm_nth16) +
878 sizeof(struct usb_cdc_ncm_ndp16))) {
879 pr_debug("frame too short\n");
883 nth16 = (struct usb_cdc_ncm_nth16 *)skb_in->data;
885 if (le32_to_cpu(nth16->dwSignature) != USB_CDC_NCM_NTH16_SIGN) {
886 pr_debug("invalid NTH16 signature <%u>\n",
887 le32_to_cpu(nth16->dwSignature));
891 len = le16_to_cpu(nth16->wBlockLength);
892 if (len > ctx->rx_max) {
893 pr_debug("unsupported NTB block length %u/%u\n", len,
898 if ((ctx->rx_seq + 1) != le16_to_cpu(nth16->wSequence) &&
899 (ctx->rx_seq || le16_to_cpu(nth16->wSequence)) &&
900 !((ctx->rx_seq == 0xffff) && !le16_to_cpu(nth16->wSequence))) {
901 pr_debug("sequence number glitch prev=%d curr=%d\n",
902 ctx->rx_seq, le16_to_cpu(nth16->wSequence));
904 ctx->rx_seq = le16_to_cpu(nth16->wSequence);
906 ret = le16_to_cpu(nth16->wNdpIndex);
910 EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_nth16);
912 /* verify NDP header and return number of datagrams, or negative error */
913 int cdc_ncm_rx_verify_ndp16(struct sk_buff *skb_in, int ndpoffset)
915 struct usb_cdc_ncm_ndp16 *ndp16;
918 if ((ndpoffset + sizeof(struct usb_cdc_ncm_ndp16)) > skb_in->len) {
919 pr_debug("invalid NDP offset <%u>\n", ndpoffset);
922 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
924 if (le16_to_cpu(ndp16->wLength) < USB_CDC_NCM_NDP16_LENGTH_MIN) {
925 pr_debug("invalid DPT16 length <%u>\n",
926 le16_to_cpu(ndp16->wLength));
930 ret = ((le16_to_cpu(ndp16->wLength) -
931 sizeof(struct usb_cdc_ncm_ndp16)) /
932 sizeof(struct usb_cdc_ncm_dpe16));
933 ret--; /* we process NDP entries except for the last one */
935 if ((sizeof(struct usb_cdc_ncm_ndp16) + ret * (sizeof(struct usb_cdc_ncm_dpe16))) >
937 pr_debug("Invalid nframes = %d\n", ret);
944 EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_ndp16);
946 static int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
949 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
954 struct usb_cdc_ncm_ndp16 *ndp16;
955 struct usb_cdc_ncm_dpe16 *dpe16;
957 int loopcount = 50; /* arbitrary max preventing infinite loop */
959 ndpoffset = cdc_ncm_rx_verify_nth16(ctx, skb_in);
964 nframes = cdc_ncm_rx_verify_ndp16(skb_in, ndpoffset);
968 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
970 if (le32_to_cpu(ndp16->dwSignature) != USB_CDC_NCM_NDP16_NOCRC_SIGN) {
971 pr_debug("invalid DPT16 signature <%u>\n",
972 le32_to_cpu(ndp16->dwSignature));
975 dpe16 = ndp16->dpe16;
977 for (x = 0; x < nframes; x++, dpe16++) {
978 offset = le16_to_cpu(dpe16->wDatagramIndex);
979 len = le16_to_cpu(dpe16->wDatagramLength);
983 * All entries after first NULL entry are to be ignored
985 if ((offset == 0) || (len == 0)) {
987 goto err_ndp; /* empty NTB */
991 /* sanity checking */
992 if (((offset + len) > skb_in->len) ||
993 (len > ctx->rx_max) || (len < ETH_HLEN)) {
994 pr_debug("invalid frame detected (ignored)"
995 "offset[%u]=%u, length=%u, skb=%p\n",
996 x, offset, len, skb_in);
1002 skb = skb_clone(skb_in, GFP_ATOMIC);
1006 skb->data = ((u8 *)skb_in->data) + offset;
1007 skb_set_tail_pointer(skb, len);
1008 usbnet_skb_return(dev, skb);
1012 /* are there more NDPs to process? */
1013 ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
1014 if (ndpoffset && loopcount--)
1023 cdc_ncm_speed_change(struct usbnet *dev,
1024 struct usb_cdc_speed_change *data)
1026 uint32_t rx_speed = le32_to_cpu(data->DLBitRRate);
1027 uint32_t tx_speed = le32_to_cpu(data->ULBitRate);
1030 * Currently the USB-NET API does not support reporting the actual
1031 * device speed. Do print it instead.
1033 if ((tx_speed > 1000000) && (rx_speed > 1000000)) {
1034 printk(KERN_INFO KBUILD_MODNAME
1035 ": %s: %u mbit/s downlink "
1036 "%u mbit/s uplink\n",
1038 (unsigned int)(rx_speed / 1000000U),
1039 (unsigned int)(tx_speed / 1000000U));
1041 printk(KERN_INFO KBUILD_MODNAME
1042 ": %s: %u kbit/s downlink "
1043 "%u kbit/s uplink\n",
1045 (unsigned int)(rx_speed / 1000U),
1046 (unsigned int)(tx_speed / 1000U));
1050 static void cdc_ncm_status(struct usbnet *dev, struct urb *urb)
1052 struct cdc_ncm_ctx *ctx;
1053 struct usb_cdc_notification *event;
1055 ctx = (struct cdc_ncm_ctx *)dev->data[0];
1057 if (urb->actual_length < sizeof(*event))
1060 /* test for split data in 8-byte chunks */
1061 if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
1062 cdc_ncm_speed_change(dev,
1063 (struct usb_cdc_speed_change *)urb->transfer_buffer);
1067 event = urb->transfer_buffer;
1069 switch (event->bNotificationType) {
1070 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
1072 * According to the CDC NCM specification ch.7.1
1073 * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be
1074 * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE.
1076 ctx->connected = le16_to_cpu(event->wValue);
1078 printk(KERN_INFO KBUILD_MODNAME ": %s: network connection:"
1080 dev->net->name, ctx->connected ? "" : "dis");
1082 usbnet_link_change(dev, ctx->connected, 0);
1085 case USB_CDC_NOTIFY_SPEED_CHANGE:
1086 if (urb->actual_length < (sizeof(*event) +
1087 sizeof(struct usb_cdc_speed_change)))
1088 set_bit(EVENT_STS_SPLIT, &dev->flags);
1090 cdc_ncm_speed_change(dev,
1091 (struct usb_cdc_speed_change *)&event[1]);
1095 dev_dbg(&dev->udev->dev,
1096 "NCM: unexpected notification 0x%02x!\n",
1097 event->bNotificationType);
1102 static int cdc_ncm_check_connect(struct usbnet *dev)
1104 struct cdc_ncm_ctx *ctx;
1106 ctx = (struct cdc_ncm_ctx *)dev->data[0];
1108 return 1; /* disconnected */
1110 return !ctx->connected;
1113 static const struct driver_info cdc_ncm_info = {
1114 .description = "CDC NCM",
1115 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET,
1116 .bind = cdc_ncm_bind,
1117 .unbind = cdc_ncm_unbind,
1118 .check_connect = cdc_ncm_check_connect,
1119 .manage_power = usbnet_manage_power,
1120 .status = cdc_ncm_status,
1121 .rx_fixup = cdc_ncm_rx_fixup,
1122 .tx_fixup = cdc_ncm_tx_fixup,
1125 /* Same as cdc_ncm_info, but with FLAG_WWAN */
1126 static const struct driver_info wwan_info = {
1127 .description = "Mobile Broadband Network Device",
1128 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
1130 .bind = cdc_ncm_bind,
1131 .unbind = cdc_ncm_unbind,
1132 .check_connect = cdc_ncm_check_connect,
1133 .manage_power = usbnet_manage_power,
1134 .status = cdc_ncm_status,
1135 .rx_fixup = cdc_ncm_rx_fixup,
1136 .tx_fixup = cdc_ncm_tx_fixup,
1139 /* Same as wwan_info, but with FLAG_NOARP */
1140 static const struct driver_info wwan_noarp_info = {
1141 .description = "Mobile Broadband Network Device (NO ARP)",
1142 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
1143 | FLAG_WWAN | FLAG_NOARP,
1144 .bind = cdc_ncm_bind,
1145 .unbind = cdc_ncm_unbind,
1146 .check_connect = cdc_ncm_check_connect,
1147 .manage_power = usbnet_manage_power,
1148 .status = cdc_ncm_status,
1149 .rx_fixup = cdc_ncm_rx_fixup,
1150 .tx_fixup = cdc_ncm_tx_fixup,
1153 static const struct usb_device_id cdc_devs[] = {
1154 /* Ericsson MBM devices like F5521gw */
1155 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1156 | USB_DEVICE_ID_MATCH_VENDOR,
1158 .bInterfaceClass = USB_CLASS_COMM,
1159 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1160 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1161 .driver_info = (unsigned long) &wwan_info,
1164 /* Dell branded MBM devices like DW5550 */
1165 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1166 | USB_DEVICE_ID_MATCH_VENDOR,
1168 .bInterfaceClass = USB_CLASS_COMM,
1169 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1170 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1171 .driver_info = (unsigned long) &wwan_info,
1174 /* Toshiba branded MBM devices */
1175 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1176 | USB_DEVICE_ID_MATCH_VENDOR,
1178 .bInterfaceClass = USB_CLASS_COMM,
1179 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1180 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1181 .driver_info = (unsigned long) &wwan_info,
1184 /* tag Huawei devices as wwan */
1185 { USB_VENDOR_AND_INTERFACE_INFO(0x12d1,
1187 USB_CDC_SUBCLASS_NCM,
1188 USB_CDC_PROTO_NONE),
1189 .driver_info = (unsigned long)&wwan_info,
1192 /* Huawei NCM devices disguised as vendor specific */
1193 { USB_VENDOR_AND_INTERFACE_INFO(0x12d1, 0xff, 0x02, 0x16),
1194 .driver_info = (unsigned long)&wwan_info,
1196 { USB_VENDOR_AND_INTERFACE_INFO(0x12d1, 0xff, 0x02, 0x46),
1197 .driver_info = (unsigned long)&wwan_info,
1199 { USB_VENDOR_AND_INTERFACE_INFO(0x12d1, 0xff, 0x02, 0x76),
1200 .driver_info = (unsigned long)&wwan_info,
1203 /* Infineon(now Intel) HSPA Modem platform */
1204 { USB_DEVICE_AND_INTERFACE_INFO(0x1519, 0x0443,
1206 USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
1207 .driver_info = (unsigned long)&wwan_noarp_info,
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,
1218 MODULE_DEVICE_TABLE(usb, cdc_devs);
1220 static struct usb_driver cdc_ncm_driver = {
1222 .id_table = cdc_devs,
1223 .probe = usbnet_probe,
1224 .disconnect = usbnet_disconnect,
1225 .suspend = usbnet_suspend,
1226 .resume = usbnet_resume,
1227 .reset_resume = usbnet_resume,
1228 .supports_autosuspend = 1,
1229 .disable_hub_initiated_lpm = 1,
1232 module_usb_driver(cdc_ncm_driver);
1234 MODULE_AUTHOR("Hans Petter Selasky");
1235 MODULE_DESCRIPTION("USB CDC NCM host driver");
1236 MODULE_LICENSE("Dual BSD/GPL");