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 static int cdc_ncm_setup(struct usbnet *dev)
70 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
76 u16 ntb_fmt_supported;
77 __le16 max_datagram_size;
79 iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber;
81 err = usbnet_read_cmd(dev, USB_CDC_GET_NTB_PARAMETERS,
82 USB_TYPE_CLASS | USB_DIR_IN
84 0, iface_no, &ctx->ncm_parm,
85 sizeof(ctx->ncm_parm));
87 dev_err(&dev->intf->dev, "failed GET_NTB_PARAMETERS\n");
88 return err; /* GET_NTB_PARAMETERS is required */
91 /* read correct set of parameters according to device mode */
92 ctx->rx_max = le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize);
93 ctx->tx_max = le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize);
94 ctx->tx_remainder = le16_to_cpu(ctx->ncm_parm.wNdpOutPayloadRemainder);
95 ctx->tx_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutDivisor);
96 ctx->tx_ndp_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutAlignment);
97 /* devices prior to NCM Errata shall set this field to zero */
98 ctx->tx_max_datagrams = le16_to_cpu(ctx->ncm_parm.wNtbOutMaxDatagrams);
99 ntb_fmt_supported = le16_to_cpu(ctx->ncm_parm.bmNtbFormatsSupported);
101 /* there are some minor differences in NCM and MBIM defaults */
102 if (cdc_ncm_comm_intf_is_mbim(ctx->control->cur_altsetting)) {
106 flags = ctx->mbim_desc->bmNetworkCapabilities;
107 ctx->max_datagram_size = le16_to_cpu(ctx->mbim_desc->wMaxSegmentSize);
108 if (ctx->max_datagram_size < CDC_MBIM_MIN_DATAGRAM_SIZE)
109 ctx->max_datagram_size = CDC_MBIM_MIN_DATAGRAM_SIZE;
114 flags = ctx->func_desc->bmNetworkCapabilities;
115 ctx->max_datagram_size = le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
116 if (ctx->max_datagram_size < CDC_NCM_MIN_DATAGRAM_SIZE)
117 ctx->max_datagram_size = CDC_NCM_MIN_DATAGRAM_SIZE;
120 /* common absolute max for NCM and MBIM */
121 if (ctx->max_datagram_size > CDC_NCM_MAX_DATAGRAM_SIZE)
122 ctx->max_datagram_size = CDC_NCM_MAX_DATAGRAM_SIZE;
124 dev_dbg(&dev->intf->dev,
125 "dwNtbInMaxSize=%u dwNtbOutMaxSize=%u wNdpOutPayloadRemainder=%u wNdpOutDivisor=%u wNdpOutAlignment=%u wNtbOutMaxDatagrams=%u flags=0x%x\n",
126 ctx->rx_max, ctx->tx_max, ctx->tx_remainder, ctx->tx_modulus,
127 ctx->tx_ndp_modulus, ctx->tx_max_datagrams, flags);
129 /* max count of tx datagrams */
130 if ((ctx->tx_max_datagrams == 0) ||
131 (ctx->tx_max_datagrams > CDC_NCM_DPT_DATAGRAMS_MAX))
132 ctx->tx_max_datagrams = CDC_NCM_DPT_DATAGRAMS_MAX;
134 /* verify maximum size of received NTB in bytes */
135 if (ctx->rx_max < USB_CDC_NCM_NTB_MIN_IN_SIZE) {
136 dev_dbg(&dev->intf->dev, "Using min receive length=%d\n",
137 USB_CDC_NCM_NTB_MIN_IN_SIZE);
138 ctx->rx_max = USB_CDC_NCM_NTB_MIN_IN_SIZE;
141 if (ctx->rx_max > CDC_NCM_NTB_MAX_SIZE_RX) {
142 dev_dbg(&dev->intf->dev, "Using default maximum receive length=%d\n",
143 CDC_NCM_NTB_MAX_SIZE_RX);
144 ctx->rx_max = CDC_NCM_NTB_MAX_SIZE_RX;
147 /* inform device about NTB input size changes */
148 if (ctx->rx_max != le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize)) {
149 __le32 dwNtbInMaxSize = cpu_to_le32(ctx->rx_max);
151 err = usbnet_write_cmd(dev, USB_CDC_SET_NTB_INPUT_SIZE,
152 USB_TYPE_CLASS | USB_DIR_OUT
153 | USB_RECIP_INTERFACE,
154 0, iface_no, &dwNtbInMaxSize, 4);
156 dev_dbg(&dev->intf->dev, "Setting NTB Input Size failed\n");
159 /* verify maximum size of transmitted NTB in bytes */
160 if (ctx->tx_max > CDC_NCM_NTB_MAX_SIZE_TX) {
161 dev_dbg(&dev->intf->dev, "Using default maximum transmit length=%d\n",
162 CDC_NCM_NTB_MAX_SIZE_TX);
163 ctx->tx_max = CDC_NCM_NTB_MAX_SIZE_TX;
167 * verify that the structure alignment is:
169 * - not greater than the maximum transmit length
170 * - not less than four bytes
172 val = ctx->tx_ndp_modulus;
174 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
175 (val != ((-val) & val)) || (val >= ctx->tx_max)) {
176 dev_dbg(&dev->intf->dev, "Using default alignment: 4 bytes\n");
177 ctx->tx_ndp_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
181 * verify that the payload alignment is:
183 * - not greater than the maximum transmit length
184 * - not less than four bytes
186 val = ctx->tx_modulus;
188 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
189 (val != ((-val) & val)) || (val >= ctx->tx_max)) {
190 dev_dbg(&dev->intf->dev, "Using default transmit modulus: 4 bytes\n");
191 ctx->tx_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
194 /* verify the payload remainder */
195 if (ctx->tx_remainder >= ctx->tx_modulus) {
196 dev_dbg(&dev->intf->dev, "Using default transmit remainder: 0 bytes\n");
197 ctx->tx_remainder = 0;
200 /* adjust TX-remainder according to NCM specification. */
201 ctx->tx_remainder = ((ctx->tx_remainder - eth_hlen) &
202 (ctx->tx_modulus - 1));
204 /* additional configuration */
207 if (flags & USB_CDC_NCM_NCAP_CRC_MODE) {
208 err = usbnet_write_cmd(dev, USB_CDC_SET_CRC_MODE,
209 USB_TYPE_CLASS | USB_DIR_OUT
210 | USB_RECIP_INTERFACE,
211 USB_CDC_NCM_CRC_NOT_APPENDED,
214 dev_dbg(&dev->intf->dev, "Setting CRC mode off failed\n");
217 /* set NTB format, if both formats are supported */
218 if (ntb_fmt_supported & USB_CDC_NCM_NTH32_SIGN) {
219 err = usbnet_write_cmd(dev, USB_CDC_SET_NTB_FORMAT,
220 USB_TYPE_CLASS | USB_DIR_OUT
221 | USB_RECIP_INTERFACE,
222 USB_CDC_NCM_NTB16_FORMAT,
225 dev_dbg(&dev->intf->dev, "Setting NTB format to 16-bit failed\n");
228 /* inform the device about the selected Max Datagram Size */
229 if (!(flags & USB_CDC_NCM_NCAP_MAX_DATAGRAM_SIZE))
232 /* read current mtu value from device */
233 err = usbnet_read_cmd(dev, USB_CDC_GET_MAX_DATAGRAM_SIZE,
234 USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE,
235 0, iface_no, &max_datagram_size, 2);
237 dev_dbg(&dev->intf->dev, "GET_MAX_DATAGRAM_SIZE failed\n");
241 if (le16_to_cpu(max_datagram_size) == ctx->max_datagram_size)
244 max_datagram_size = cpu_to_le16(ctx->max_datagram_size);
245 err = usbnet_write_cmd(dev, USB_CDC_SET_MAX_DATAGRAM_SIZE,
246 USB_TYPE_CLASS | USB_DIR_OUT | USB_RECIP_INTERFACE,
247 0, iface_no, &max_datagram_size, 2);
249 dev_dbg(&dev->intf->dev, "SET_MAX_DATAGRAM_SIZE failed\n");
252 /* set MTU to max supported by the device if necessary */
253 if (dev->net->mtu > ctx->max_datagram_size - eth_hlen)
254 dev->net->mtu = ctx->max_datagram_size - eth_hlen;
259 cdc_ncm_find_endpoints(struct usbnet *dev, struct usb_interface *intf)
261 struct usb_host_endpoint *e, *in = NULL, *out = NULL;
264 for (ep = 0; ep < intf->cur_altsetting->desc.bNumEndpoints; ep++) {
266 e = intf->cur_altsetting->endpoint + ep;
267 switch (e->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
268 case USB_ENDPOINT_XFER_INT:
269 if (usb_endpoint_dir_in(&e->desc)) {
275 case USB_ENDPOINT_XFER_BULK:
276 if (usb_endpoint_dir_in(&e->desc)) {
290 dev->in = usb_rcvbulkpipe(dev->udev,
291 in->desc.bEndpointAddress &
292 USB_ENDPOINT_NUMBER_MASK);
293 if (out && !dev->out)
294 dev->out = usb_sndbulkpipe(dev->udev,
295 out->desc.bEndpointAddress &
296 USB_ENDPOINT_NUMBER_MASK);
299 static void cdc_ncm_free(struct cdc_ncm_ctx *ctx)
304 if (ctx->tx_rem_skb != NULL) {
305 dev_kfree_skb_any(ctx->tx_rem_skb);
306 ctx->tx_rem_skb = NULL;
309 if (ctx->tx_curr_skb != NULL) {
310 dev_kfree_skb_any(ctx->tx_curr_skb);
311 ctx->tx_curr_skb = NULL;
317 int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_altsetting)
319 const struct usb_cdc_union_desc *union_desc = NULL;
320 struct cdc_ncm_ctx *ctx;
321 struct usb_driver *driver;
327 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
331 hrtimer_init(&ctx->tx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
332 ctx->tx_timer.function = &cdc_ncm_tx_timer_cb;
333 ctx->bh.data = (unsigned long)dev;
334 ctx->bh.func = cdc_ncm_txpath_bh;
335 atomic_set(&ctx->stop, 0);
336 spin_lock_init(&ctx->mtx);
338 /* store ctx pointer in device data field */
339 dev->data[0] = (unsigned long)ctx;
341 /* only the control interface can be successfully probed */
344 /* get some pointers */
345 driver = driver_of(intf);
346 buf = intf->cur_altsetting->extra;
347 len = intf->cur_altsetting->extralen;
349 /* parse through descriptors associated with control interface */
350 while ((len > 0) && (buf[0] > 2) && (buf[0] <= len)) {
352 if (buf[1] != USB_DT_CS_INTERFACE)
356 case USB_CDC_UNION_TYPE:
357 if (buf[0] < sizeof(*union_desc))
360 union_desc = (const struct usb_cdc_union_desc *)buf;
361 /* the master must be the interface we are probing */
362 if (intf->cur_altsetting->desc.bInterfaceNumber !=
363 union_desc->bMasterInterface0) {
364 dev_dbg(&intf->dev, "bogus CDC Union\n");
367 ctx->data = usb_ifnum_to_if(dev->udev,
368 union_desc->bSlaveInterface0);
371 case USB_CDC_ETHERNET_TYPE:
372 if (buf[0] < sizeof(*(ctx->ether_desc)))
376 (const struct usb_cdc_ether_desc *)buf;
379 case USB_CDC_NCM_TYPE:
380 if (buf[0] < sizeof(*(ctx->func_desc)))
383 ctx->func_desc = (const struct usb_cdc_ncm_desc *)buf;
386 case USB_CDC_MBIM_TYPE:
387 if (buf[0] < sizeof(*(ctx->mbim_desc)))
390 ctx->mbim_desc = (const struct usb_cdc_mbim_desc *)buf;
397 /* advance to next descriptor */
403 /* some buggy devices have an IAD but no CDC Union */
404 if (!union_desc && intf->intf_assoc && intf->intf_assoc->bInterfaceCount == 2) {
405 ctx->data = usb_ifnum_to_if(dev->udev, intf->cur_altsetting->desc.bInterfaceNumber + 1);
406 dev_dbg(&intf->dev, "CDC Union missing - got slave from IAD\n");
409 /* check if we got everything */
410 if (!ctx->data || (!ctx->mbim_desc && !ctx->ether_desc)) {
411 dev_dbg(&intf->dev, "CDC descriptors missing\n");
415 /* claim data interface, if different from control */
416 if (ctx->data != ctx->control) {
417 temp = usb_driver_claim_interface(driver, ctx->data, dev);
419 dev_dbg(&intf->dev, "failed to claim data intf\n");
424 iface_no = ctx->data->cur_altsetting->desc.bInterfaceNumber;
426 /* reset data interface */
427 temp = usb_set_interface(dev->udev, iface_no, 0);
429 dev_dbg(&intf->dev, "set interface failed\n");
433 /* initialize data interface */
434 if (cdc_ncm_setup(dev))
437 /* configure data interface */
438 temp = usb_set_interface(dev->udev, iface_no, data_altsetting);
440 dev_dbg(&intf->dev, "set interface failed\n");
444 cdc_ncm_find_endpoints(dev, ctx->data);
445 cdc_ncm_find_endpoints(dev, ctx->control);
446 if (!dev->in || !dev->out || !dev->status) {
447 dev_dbg(&intf->dev, "failed to collect endpoints\n");
451 usb_set_intfdata(ctx->data, dev);
452 usb_set_intfdata(ctx->control, dev);
454 if (ctx->ether_desc) {
455 temp = usbnet_get_ethernet_addr(dev, ctx->ether_desc->iMACAddress);
457 dev_dbg(&intf->dev, "failed to get mac address\n");
460 dev_info(&intf->dev, "MAC-Address: %pM\n", dev->net->dev_addr);
463 /* usbnet use these values for sizing tx/rx queues */
464 dev->hard_mtu = ctx->tx_max;
465 dev->rx_urb_size = ctx->rx_max;
467 /* cdc_ncm_setup will override dwNtbOutMaxSize if it is
468 * outside the sane range. Adding a pad byte here if necessary
469 * simplifies the handling in cdc_ncm_fill_tx_frame, making
470 * tx_max always represent the real skb max size.
472 if (ctx->tx_max != le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize) &&
473 ctx->tx_max % usb_maxpacket(dev->udev, dev->out, 1) == 0)
479 usb_set_intfdata(ctx->control, NULL);
480 usb_set_intfdata(ctx->data, NULL);
481 if (ctx->data != ctx->control)
482 usb_driver_release_interface(driver, ctx->data);
484 cdc_ncm_free((struct cdc_ncm_ctx *)dev->data[0]);
486 dev_info(&intf->dev, "bind() failure\n");
489 EXPORT_SYMBOL_GPL(cdc_ncm_bind_common);
491 void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf)
493 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
494 struct usb_driver *driver = driver_of(intf);
497 return; /* no setup */
499 atomic_set(&ctx->stop, 1);
501 if (hrtimer_active(&ctx->tx_timer))
502 hrtimer_cancel(&ctx->tx_timer);
504 tasklet_kill(&ctx->bh);
506 /* handle devices with combined control and data interface */
507 if (ctx->control == ctx->data)
510 /* disconnect master --> disconnect slave */
511 if (intf == ctx->control && ctx->data) {
512 usb_set_intfdata(ctx->data, NULL);
513 usb_driver_release_interface(driver, ctx->data);
516 } else if (intf == ctx->data && ctx->control) {
517 usb_set_intfdata(ctx->control, NULL);
518 usb_driver_release_interface(driver, ctx->control);
522 usb_set_intfdata(intf, NULL);
525 EXPORT_SYMBOL_GPL(cdc_ncm_unbind);
527 /* Select the MBIM altsetting iff it is preferred and available,
528 * returning the number of the corresponding data interface altsetting
530 u8 cdc_ncm_select_altsetting(struct usbnet *dev, struct usb_interface *intf)
532 struct usb_host_interface *alt;
534 /* The MBIM spec defines a NCM compatible default altsetting,
535 * which we may have matched:
537 * "Functions that implement both NCM 1.0 and MBIM (an
538 * “NCM/MBIM function”) according to this recommendation
539 * shall provide two alternate settings for the
540 * Communication Interface. Alternate setting 0, and the
541 * associated class and endpoint descriptors, shall be
542 * constructed according to the rules given for the
543 * Communication Interface in section 5 of [USBNCM10].
544 * Alternate setting 1, and the associated class and
545 * endpoint descriptors, shall be constructed according to
546 * the rules given in section 6 (USB Device Model) of this
549 if (prefer_mbim && intf->num_altsetting == 2) {
550 alt = usb_altnum_to_altsetting(intf, CDC_NCM_COMM_ALTSETTING_MBIM);
551 if (alt && cdc_ncm_comm_intf_is_mbim(alt) &&
552 !usb_set_interface(dev->udev,
553 intf->cur_altsetting->desc.bInterfaceNumber,
554 CDC_NCM_COMM_ALTSETTING_MBIM))
555 return CDC_NCM_DATA_ALTSETTING_MBIM;
557 return CDC_NCM_DATA_ALTSETTING_NCM;
559 EXPORT_SYMBOL_GPL(cdc_ncm_select_altsetting);
561 static int cdc_ncm_bind(struct usbnet *dev, struct usb_interface *intf)
565 /* MBIM backwards compatible function? */
566 cdc_ncm_select_altsetting(dev, intf);
567 if (cdc_ncm_comm_intf_is_mbim(intf->cur_altsetting))
570 /* NCM data altsetting is always 1 */
571 ret = cdc_ncm_bind_common(dev, intf, 1);
574 * We should get an event when network connection is "connected" or
575 * "disconnected". Set network connection in "disconnected" state
576 * (carrier is OFF) during attach, so the IP network stack does not
577 * start IPv6 negotiation and more.
579 usbnet_link_change(dev, 0, 0);
583 static void cdc_ncm_align_tail(struct sk_buff *skb, size_t modulus, size_t remainder, size_t max)
585 size_t align = ALIGN(skb->len, modulus) - skb->len + remainder;
587 if (skb->len + align > max)
588 align = max - skb->len;
589 if (align && skb_tailroom(skb) >= align)
590 memset(skb_put(skb, align), 0, align);
593 /* return a pointer to a valid struct usb_cdc_ncm_ndp16 of type sign, possibly
594 * allocating a new one within skb
596 static struct usb_cdc_ncm_ndp16 *cdc_ncm_ndp(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign, size_t reserve)
598 struct usb_cdc_ncm_ndp16 *ndp16 = NULL;
599 struct usb_cdc_ncm_nth16 *nth16 = (void *)skb->data;
600 size_t ndpoffset = le16_to_cpu(nth16->wNdpIndex);
602 /* follow the chain of NDPs, looking for a match */
604 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb->data + ndpoffset);
605 if (ndp16->dwSignature == sign)
607 ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
611 cdc_ncm_align_tail(skb, ctx->tx_ndp_modulus, 0, ctx->tx_max);
613 /* verify that there is room for the NDP and the datagram (reserve) */
614 if ((ctx->tx_max - skb->len - reserve) < CDC_NCM_NDP_SIZE)
619 ndp16->wNextNdpIndex = cpu_to_le16(skb->len);
621 nth16->wNdpIndex = cpu_to_le16(skb->len);
623 /* push a new empty NDP */
624 ndp16 = (struct usb_cdc_ncm_ndp16 *)memset(skb_put(skb, CDC_NCM_NDP_SIZE), 0, CDC_NCM_NDP_SIZE);
625 ndp16->dwSignature = sign;
626 ndp16->wLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_ndp16) + sizeof(struct usb_cdc_ncm_dpe16));
631 cdc_ncm_fill_tx_frame(struct usbnet *dev, struct sk_buff *skb, __le32 sign)
633 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
634 struct usb_cdc_ncm_nth16 *nth16;
635 struct usb_cdc_ncm_ndp16 *ndp16;
636 struct sk_buff *skb_out;
637 u16 n = 0, index, ndplen;
640 /* if there is a remaining skb, it gets priority */
642 swap(skb, ctx->tx_rem_skb);
643 swap(sign, ctx->tx_rem_sign);
648 /* check if we are resuming an OUT skb */
649 skb_out = ctx->tx_curr_skb;
651 /* allocate a new OUT skb */
653 skb_out = alloc_skb(ctx->tx_max, GFP_ATOMIC);
654 if (skb_out == NULL) {
656 dev_kfree_skb_any(skb);
657 dev->net->stats.tx_dropped++;
661 /* fill out the initial 16-bit NTB header */
662 nth16 = (struct usb_cdc_ncm_nth16 *)memset(skb_put(skb_out, sizeof(struct usb_cdc_ncm_nth16)), 0, sizeof(struct usb_cdc_ncm_nth16));
663 nth16->dwSignature = cpu_to_le32(USB_CDC_NCM_NTH16_SIGN);
664 nth16->wHeaderLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_nth16));
665 nth16->wSequence = cpu_to_le16(ctx->tx_seq++);
667 /* count total number of frames in this NTB */
668 ctx->tx_curr_frame_num = 0;
671 for (n = ctx->tx_curr_frame_num; n < ctx->tx_max_datagrams; n++) {
672 /* send any remaining skb first */
674 skb = ctx->tx_rem_skb;
675 sign = ctx->tx_rem_sign;
676 ctx->tx_rem_skb = NULL;
678 /* check for end of skb */
683 /* get the appropriate NDP for this skb */
684 ndp16 = cdc_ncm_ndp(ctx, skb_out, sign, skb->len + ctx->tx_modulus + ctx->tx_remainder);
686 /* align beginning of next frame */
687 cdc_ncm_align_tail(skb_out, ctx->tx_modulus, ctx->tx_remainder, ctx->tx_max);
689 /* check if we had enough room left for both NDP and frame */
690 if (!ndp16 || skb_out->len + skb->len > ctx->tx_max) {
692 /* won't fit, MTU problem? */
693 dev_kfree_skb_any(skb);
695 dev->net->stats.tx_dropped++;
697 /* no room for skb - store for later */
698 if (ctx->tx_rem_skb != NULL) {
699 dev_kfree_skb_any(ctx->tx_rem_skb);
700 dev->net->stats.tx_dropped++;
702 ctx->tx_rem_skb = skb;
703 ctx->tx_rem_sign = sign;
710 /* calculate frame number withing this NDP */
711 ndplen = le16_to_cpu(ndp16->wLength);
712 index = (ndplen - sizeof(struct usb_cdc_ncm_ndp16)) / sizeof(struct usb_cdc_ncm_dpe16) - 1;
714 /* OK, add this skb */
715 ndp16->dpe16[index].wDatagramLength = cpu_to_le16(skb->len);
716 ndp16->dpe16[index].wDatagramIndex = cpu_to_le16(skb_out->len);
717 ndp16->wLength = cpu_to_le16(ndplen + sizeof(struct usb_cdc_ncm_dpe16));
718 memcpy(skb_put(skb_out, skb->len), skb->data, skb->len);
719 dev_kfree_skb_any(skb);
722 /* send now if this NDP is full */
723 if (index >= CDC_NCM_DPT_DATAGRAMS_MAX) {
729 /* free up any dangling skb */
731 dev_kfree_skb_any(skb);
733 dev->net->stats.tx_dropped++;
736 ctx->tx_curr_frame_num = n;
739 /* wait for more frames */
741 ctx->tx_curr_skb = skb_out;
744 } else if ((n < ctx->tx_max_datagrams) && (ready2send == 0)) {
745 /* wait for more frames */
747 ctx->tx_curr_skb = skb_out;
748 /* set the pending count */
749 if (n < CDC_NCM_RESTART_TIMER_DATAGRAM_CNT)
750 ctx->tx_timer_pending = CDC_NCM_TIMER_PENDING_CNT;
755 /* variables will be reset at next call */
758 /* If collected data size is less or equal CDC_NCM_MIN_TX_PKT
759 * bytes, we send buffers as it is. If we get more data, it
760 * would be more efficient for USB HS mobile device with DMA
761 * engine to receive a full size NTB, than canceling DMA
762 * transfer and receiving a short packet.
764 * This optimization support is pointless if we end up sending
765 * a ZLP after full sized NTBs.
767 if (!(dev->driver_info->flags & FLAG_SEND_ZLP) &&
768 skb_out->len > CDC_NCM_MIN_TX_PKT)
769 memset(skb_put(skb_out, ctx->tx_max - skb_out->len), 0,
770 ctx->tx_max - skb_out->len);
771 else if ((skb_out->len % dev->maxpacket) == 0)
772 *skb_put(skb_out, 1) = 0; /* force short packet */
774 /* set final frame length */
775 nth16 = (struct usb_cdc_ncm_nth16 *)skb_out->data;
776 nth16->wBlockLength = cpu_to_le16(skb_out->len);
779 ctx->tx_curr_skb = NULL;
780 dev->net->stats.tx_packets += ctx->tx_curr_frame_num;
784 /* Start timer, if there is a remaining skb */
785 if (ctx->tx_curr_skb != NULL)
786 cdc_ncm_tx_timeout_start(ctx);
789 EXPORT_SYMBOL_GPL(cdc_ncm_fill_tx_frame);
791 static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx)
793 /* start timer, if not already started */
794 if (!(hrtimer_active(&ctx->tx_timer) || atomic_read(&ctx->stop)))
795 hrtimer_start(&ctx->tx_timer,
796 ktime_set(0, CDC_NCM_TIMER_INTERVAL),
800 static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *timer)
802 struct cdc_ncm_ctx *ctx =
803 container_of(timer, struct cdc_ncm_ctx, tx_timer);
805 if (!atomic_read(&ctx->stop))
806 tasklet_schedule(&ctx->bh);
807 return HRTIMER_NORESTART;
810 static void cdc_ncm_txpath_bh(unsigned long param)
812 struct usbnet *dev = (struct usbnet *)param;
813 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
815 spin_lock_bh(&ctx->mtx);
816 if (ctx->tx_timer_pending != 0) {
817 ctx->tx_timer_pending--;
818 cdc_ncm_tx_timeout_start(ctx);
819 spin_unlock_bh(&ctx->mtx);
820 } else if (dev->net != NULL) {
821 spin_unlock_bh(&ctx->mtx);
822 netif_tx_lock_bh(dev->net);
823 usbnet_start_xmit(NULL, dev->net);
824 netif_tx_unlock_bh(dev->net);
826 spin_unlock_bh(&ctx->mtx);
831 cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
833 struct sk_buff *skb_out;
834 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
837 * The Ethernet API we are using does not support transmitting
838 * multiple Ethernet frames in a single call. This driver will
839 * accumulate multiple Ethernet frames and send out a larger
840 * USB frame when the USB buffer is full or when a single jiffies
846 spin_lock_bh(&ctx->mtx);
847 skb_out = cdc_ncm_fill_tx_frame(dev, skb, cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN));
848 spin_unlock_bh(&ctx->mtx);
853 dev_kfree_skb_any(skb);
857 EXPORT_SYMBOL_GPL(cdc_ncm_tx_fixup);
859 /* verify NTB header and return offset of first NDP, or negative error */
860 int cdc_ncm_rx_verify_nth16(struct cdc_ncm_ctx *ctx, struct sk_buff *skb_in)
862 struct usbnet *dev = netdev_priv(skb_in->dev);
863 struct usb_cdc_ncm_nth16 *nth16;
870 if (skb_in->len < (sizeof(struct usb_cdc_ncm_nth16) +
871 sizeof(struct usb_cdc_ncm_ndp16))) {
872 netif_dbg(dev, rx_err, dev->net, "frame too short\n");
876 nth16 = (struct usb_cdc_ncm_nth16 *)skb_in->data;
878 if (nth16->dwSignature != cpu_to_le32(USB_CDC_NCM_NTH16_SIGN)) {
879 netif_dbg(dev, rx_err, dev->net,
880 "invalid NTH16 signature <%#010x>\n",
881 le32_to_cpu(nth16->dwSignature));
885 len = le16_to_cpu(nth16->wBlockLength);
886 if (len > ctx->rx_max) {
887 netif_dbg(dev, rx_err, dev->net,
888 "unsupported NTB block length %u/%u\n", len,
893 if ((ctx->rx_seq + 1) != le16_to_cpu(nth16->wSequence) &&
894 (ctx->rx_seq || le16_to_cpu(nth16->wSequence)) &&
895 !((ctx->rx_seq == 0xffff) && !le16_to_cpu(nth16->wSequence))) {
896 netif_dbg(dev, rx_err, dev->net,
897 "sequence number glitch prev=%d curr=%d\n",
898 ctx->rx_seq, le16_to_cpu(nth16->wSequence));
900 ctx->rx_seq = le16_to_cpu(nth16->wSequence);
902 ret = le16_to_cpu(nth16->wNdpIndex);
906 EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_nth16);
908 /* verify NDP header and return number of datagrams, or negative error */
909 int cdc_ncm_rx_verify_ndp16(struct sk_buff *skb_in, int ndpoffset)
911 struct usbnet *dev = netdev_priv(skb_in->dev);
912 struct usb_cdc_ncm_ndp16 *ndp16;
915 if ((ndpoffset + sizeof(struct usb_cdc_ncm_ndp16)) > skb_in->len) {
916 netif_dbg(dev, rx_err, dev->net, "invalid NDP offset <%u>\n",
920 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
922 if (le16_to_cpu(ndp16->wLength) < USB_CDC_NCM_NDP16_LENGTH_MIN) {
923 netif_dbg(dev, rx_err, dev->net, "invalid DPT16 length <%u>\n",
924 le16_to_cpu(ndp16->wLength));
928 ret = ((le16_to_cpu(ndp16->wLength) -
929 sizeof(struct usb_cdc_ncm_ndp16)) /
930 sizeof(struct usb_cdc_ncm_dpe16));
931 ret--; /* we process NDP entries except for the last one */
933 if ((sizeof(struct usb_cdc_ncm_ndp16) +
934 ret * (sizeof(struct usb_cdc_ncm_dpe16))) > skb_in->len) {
935 netif_dbg(dev, rx_err, dev->net, "Invalid nframes = %d\n", ret);
942 EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_ndp16);
944 int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
947 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
952 struct usb_cdc_ncm_ndp16 *ndp16;
953 struct usb_cdc_ncm_dpe16 *dpe16;
955 int loopcount = 50; /* arbitrary max preventing infinite loop */
957 ndpoffset = cdc_ncm_rx_verify_nth16(ctx, skb_in);
962 nframes = cdc_ncm_rx_verify_ndp16(skb_in, ndpoffset);
966 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
968 if (ndp16->dwSignature != cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN)) {
969 netif_dbg(dev, rx_err, dev->net,
970 "invalid DPT16 signature <%#010x>\n",
971 le32_to_cpu(ndp16->dwSignature));
974 dpe16 = ndp16->dpe16;
976 for (x = 0; x < nframes; x++, dpe16++) {
977 offset = le16_to_cpu(dpe16->wDatagramIndex);
978 len = le16_to_cpu(dpe16->wDatagramLength);
982 * All entries after first NULL entry are to be ignored
984 if ((offset == 0) || (len == 0)) {
986 goto err_ndp; /* empty NTB */
990 /* sanity checking */
991 if (((offset + len) > skb_in->len) ||
992 (len > ctx->rx_max) || (len < ETH_HLEN)) {
993 netif_dbg(dev, rx_err, dev->net,
994 "invalid frame detected (ignored) offset[%u]=%u, length=%u, skb=%p\n",
995 x, offset, len, skb_in);
1001 skb = skb_clone(skb_in, GFP_ATOMIC);
1005 skb->data = ((u8 *)skb_in->data) + offset;
1006 skb_set_tail_pointer(skb, len);
1007 usbnet_skb_return(dev, skb);
1011 /* are there more NDPs to process? */
1012 ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
1013 if (ndpoffset && loopcount--)
1020 EXPORT_SYMBOL_GPL(cdc_ncm_rx_fixup);
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 netif_info(dev, link, dev->net,
1035 "%u mbit/s downlink %u mbit/s uplink\n",
1036 (unsigned int)(rx_speed / 1000000U),
1037 (unsigned int)(tx_speed / 1000000U));
1039 netif_info(dev, link, dev->net,
1040 "%u kbit/s downlink %u kbit/s uplink\n",
1041 (unsigned int)(rx_speed / 1000U),
1042 (unsigned int)(tx_speed / 1000U));
1046 static void cdc_ncm_status(struct usbnet *dev, struct urb *urb)
1048 struct cdc_ncm_ctx *ctx;
1049 struct usb_cdc_notification *event;
1051 ctx = (struct cdc_ncm_ctx *)dev->data[0];
1053 if (urb->actual_length < sizeof(*event))
1056 /* test for split data in 8-byte chunks */
1057 if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
1058 cdc_ncm_speed_change(dev,
1059 (struct usb_cdc_speed_change *)urb->transfer_buffer);
1063 event = urb->transfer_buffer;
1065 switch (event->bNotificationType) {
1066 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
1068 * According to the CDC NCM specification ch.7.1
1069 * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be
1070 * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE.
1072 ctx->connected = le16_to_cpu(event->wValue);
1073 netif_info(dev, link, dev->net,
1074 "network connection: %sconnected\n",
1075 ctx->connected ? "" : "dis");
1076 usbnet_link_change(dev, ctx->connected, 0);
1079 case USB_CDC_NOTIFY_SPEED_CHANGE:
1080 if (urb->actual_length < (sizeof(*event) +
1081 sizeof(struct usb_cdc_speed_change)))
1082 set_bit(EVENT_STS_SPLIT, &dev->flags);
1084 cdc_ncm_speed_change(dev,
1085 (struct usb_cdc_speed_change *)&event[1]);
1089 dev_dbg(&dev->udev->dev,
1090 "NCM: unexpected notification 0x%02x!\n",
1091 event->bNotificationType);
1096 static int cdc_ncm_check_connect(struct usbnet *dev)
1098 struct cdc_ncm_ctx *ctx;
1100 ctx = (struct cdc_ncm_ctx *)dev->data[0];
1102 return 1; /* disconnected */
1104 return !ctx->connected;
1107 static const struct driver_info cdc_ncm_info = {
1108 .description = "CDC NCM",
1109 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET,
1110 .bind = cdc_ncm_bind,
1111 .unbind = cdc_ncm_unbind,
1112 .check_connect = cdc_ncm_check_connect,
1113 .manage_power = usbnet_manage_power,
1114 .status = cdc_ncm_status,
1115 .rx_fixup = cdc_ncm_rx_fixup,
1116 .tx_fixup = cdc_ncm_tx_fixup,
1119 /* Same as cdc_ncm_info, but with FLAG_WWAN */
1120 static const struct driver_info wwan_info = {
1121 .description = "Mobile Broadband Network Device",
1122 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
1124 .bind = cdc_ncm_bind,
1125 .unbind = cdc_ncm_unbind,
1126 .check_connect = cdc_ncm_check_connect,
1127 .manage_power = usbnet_manage_power,
1128 .status = cdc_ncm_status,
1129 .rx_fixup = cdc_ncm_rx_fixup,
1130 .tx_fixup = cdc_ncm_tx_fixup,
1133 /* Same as wwan_info, but with FLAG_NOARP */
1134 static const struct driver_info wwan_noarp_info = {
1135 .description = "Mobile Broadband Network Device (NO ARP)",
1136 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
1137 | FLAG_WWAN | FLAG_NOARP,
1138 .bind = cdc_ncm_bind,
1139 .unbind = cdc_ncm_unbind,
1140 .check_connect = cdc_ncm_check_connect,
1141 .manage_power = usbnet_manage_power,
1142 .status = cdc_ncm_status,
1143 .rx_fixup = cdc_ncm_rx_fixup,
1144 .tx_fixup = cdc_ncm_tx_fixup,
1147 static const struct usb_device_id cdc_devs[] = {
1148 /* Ericsson MBM devices like F5521gw */
1149 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1150 | USB_DEVICE_ID_MATCH_VENDOR,
1152 .bInterfaceClass = USB_CLASS_COMM,
1153 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1154 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1155 .driver_info = (unsigned long) &wwan_info,
1158 /* Dell branded MBM devices like DW5550 */
1159 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1160 | USB_DEVICE_ID_MATCH_VENDOR,
1162 .bInterfaceClass = USB_CLASS_COMM,
1163 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1164 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1165 .driver_info = (unsigned long) &wwan_info,
1168 /* Toshiba branded MBM devices */
1169 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1170 | USB_DEVICE_ID_MATCH_VENDOR,
1172 .bInterfaceClass = USB_CLASS_COMM,
1173 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1174 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1175 .driver_info = (unsigned long) &wwan_info,
1178 /* tag Huawei devices as wwan */
1179 { USB_VENDOR_AND_INTERFACE_INFO(0x12d1,
1181 USB_CDC_SUBCLASS_NCM,
1182 USB_CDC_PROTO_NONE),
1183 .driver_info = (unsigned long)&wwan_info,
1186 /* Infineon(now Intel) HSPA Modem platform */
1187 { USB_DEVICE_AND_INTERFACE_INFO(0x1519, 0x0443,
1189 USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
1190 .driver_info = (unsigned long)&wwan_noarp_info,
1193 /* Generic CDC-NCM devices */
1194 { USB_INTERFACE_INFO(USB_CLASS_COMM,
1195 USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
1196 .driver_info = (unsigned long)&cdc_ncm_info,
1201 MODULE_DEVICE_TABLE(usb, cdc_devs);
1203 static struct usb_driver cdc_ncm_driver = {
1205 .id_table = cdc_devs,
1206 .probe = usbnet_probe,
1207 .disconnect = usbnet_disconnect,
1208 .suspend = usbnet_suspend,
1209 .resume = usbnet_resume,
1210 .reset_resume = usbnet_resume,
1211 .supports_autosuspend = 1,
1212 .disable_hub_initiated_lpm = 1,
1215 module_usb_driver(cdc_ncm_driver);
1217 MODULE_AUTHOR("Hans Petter Selasky");
1218 MODULE_DESCRIPTION("USB CDC NCM host driver");
1219 MODULE_LICENSE("Dual BSD/GPL");