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 #define DRIVER_VERSION "14-Mar-2012"
58 static void cdc_ncm_txpath_bh(unsigned long param);
59 static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx);
60 static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *hr_timer);
61 static struct usb_driver cdc_ncm_driver;
64 cdc_ncm_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
66 struct usbnet *dev = netdev_priv(net);
68 strlcpy(info->driver, dev->driver_name, sizeof(info->driver));
69 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
70 strlcpy(info->fw_version, dev->driver_info->description,
71 sizeof(info->fw_version));
72 usb_make_path(dev->udev, info->bus_info, sizeof(info->bus_info));
75 static u8 cdc_ncm_setup(struct cdc_ncm_ctx *ctx)
82 u16 ntb_fmt_supported;
85 struct usbnet *dev = netdev_priv(ctx->netdev);
87 iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber;
89 err = usbnet_read_cmd(dev, USB_CDC_GET_NTB_PARAMETERS,
90 USB_TYPE_CLASS | USB_DIR_IN
92 0, iface_no, &ctx->ncm_parm,
93 sizeof(ctx->ncm_parm));
95 pr_debug("failed GET_NTB_PARAMETERS\n");
99 /* read correct set of parameters according to device mode */
100 ctx->rx_max = le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize);
101 ctx->tx_max = le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize);
102 ctx->tx_remainder = le16_to_cpu(ctx->ncm_parm.wNdpOutPayloadRemainder);
103 ctx->tx_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutDivisor);
104 ctx->tx_ndp_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutAlignment);
105 /* devices prior to NCM Errata shall set this field to zero */
106 ctx->tx_max_datagrams = le16_to_cpu(ctx->ncm_parm.wNtbOutMaxDatagrams);
107 ntb_fmt_supported = le16_to_cpu(ctx->ncm_parm.bmNtbFormatsSupported);
110 min_dgram_size = CDC_NCM_MIN_DATAGRAM_SIZE;
111 min_hdr_size = CDC_NCM_MIN_HDR_SIZE;
112 if (ctx->mbim_desc != NULL) {
113 flags = ctx->mbim_desc->bmNetworkCapabilities;
115 min_dgram_size = CDC_MBIM_MIN_DATAGRAM_SIZE;
117 } else if (ctx->func_desc != NULL) {
118 flags = ctx->func_desc->bmNetworkCapabilities;
123 pr_debug("dwNtbInMaxSize=%u dwNtbOutMaxSize=%u "
124 "wNdpOutPayloadRemainder=%u wNdpOutDivisor=%u "
125 "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 pr_debug("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 pr_debug("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 pr_debug("Setting NTB Input Size failed\n");
159 /* verify maximum size of transmitted NTB in bytes */
161 (min_hdr_size + min_dgram_size)) ||
162 (ctx->tx_max > CDC_NCM_NTB_MAX_SIZE_TX)) {
163 pr_debug("Using default maximum transmit length=%d\n",
164 CDC_NCM_NTB_MAX_SIZE_TX);
165 ctx->tx_max = CDC_NCM_NTB_MAX_SIZE_TX;
169 * verify that the structure alignment is:
171 * - not greater than the maximum transmit length
172 * - not less than four bytes
174 val = ctx->tx_ndp_modulus;
176 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
177 (val != ((-val) & val)) || (val >= ctx->tx_max)) {
178 pr_debug("Using default alignment: 4 bytes\n");
179 ctx->tx_ndp_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
183 * verify that the payload alignment is:
185 * - not greater than the maximum transmit length
186 * - not less than four bytes
188 val = ctx->tx_modulus;
190 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
191 (val != ((-val) & val)) || (val >= ctx->tx_max)) {
192 pr_debug("Using default transmit modulus: 4 bytes\n");
193 ctx->tx_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
196 /* verify the payload remainder */
197 if (ctx->tx_remainder >= ctx->tx_modulus) {
198 pr_debug("Using default transmit remainder: 0 bytes\n");
199 ctx->tx_remainder = 0;
202 /* adjust TX-remainder according to NCM specification. */
203 ctx->tx_remainder = ((ctx->tx_remainder - eth_hlen) &
204 (ctx->tx_modulus - 1));
206 /* additional configuration */
209 if (flags & USB_CDC_NCM_NCAP_CRC_MODE) {
210 err = usbnet_write_cmd(dev, USB_CDC_SET_CRC_MODE,
211 USB_TYPE_CLASS | USB_DIR_OUT
212 | USB_RECIP_INTERFACE,
213 USB_CDC_NCM_CRC_NOT_APPENDED,
216 pr_debug("Setting CRC mode off failed\n");
219 /* set NTB format, if both formats are supported */
220 if (ntb_fmt_supported & USB_CDC_NCM_NTH32_SIGN) {
221 err = usbnet_write_cmd(dev, USB_CDC_SET_NTB_FORMAT,
222 USB_TYPE_CLASS | USB_DIR_OUT
223 | USB_RECIP_INTERFACE,
224 USB_CDC_NCM_NTB16_FORMAT,
227 pr_debug("Setting NTB format to 16-bit failed\n");
230 ctx->max_datagram_size = min_dgram_size;
232 /* set Max Datagram Size (MTU) */
233 if (flags & USB_CDC_NCM_NCAP_MAX_DATAGRAM_SIZE) {
234 __le16 max_datagram_size;
236 if (ctx->ether_desc != NULL)
237 eth_max_sz = le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
238 else if (ctx->mbim_desc != NULL)
239 eth_max_sz = le16_to_cpu(ctx->mbim_desc->wMaxSegmentSize);
243 err = usbnet_read_cmd(dev, USB_CDC_GET_MAX_DATAGRAM_SIZE,
244 USB_TYPE_CLASS | USB_DIR_IN
245 | USB_RECIP_INTERFACE,
246 0, iface_no, &max_datagram_size, 2);
248 pr_debug("GET_MAX_DATAGRAM_SIZE failed, use size=%u\n",
251 ctx->max_datagram_size =
252 le16_to_cpu(max_datagram_size);
253 /* Check Eth descriptor value */
254 if (ctx->max_datagram_size > eth_max_sz)
255 ctx->max_datagram_size = eth_max_sz;
257 if (ctx->max_datagram_size > CDC_NCM_MAX_DATAGRAM_SIZE)
258 ctx->max_datagram_size = CDC_NCM_MAX_DATAGRAM_SIZE;
260 if (ctx->max_datagram_size < min_dgram_size)
261 ctx->max_datagram_size = min_dgram_size;
263 /* if value changed, update device */
264 if (ctx->max_datagram_size !=
265 le16_to_cpu(max_datagram_size)) {
266 err = usbnet_write_cmd(dev,
267 USB_CDC_SET_MAX_DATAGRAM_SIZE,
268 USB_TYPE_CLASS | USB_DIR_OUT
269 | USB_RECIP_INTERFACE,
271 iface_no, &max_datagram_size,
274 pr_debug("SET_MAX_DGRAM_SIZE failed\n");
280 if (ctx->netdev->mtu != (ctx->max_datagram_size - eth_hlen))
281 ctx->netdev->mtu = ctx->max_datagram_size - eth_hlen;
287 cdc_ncm_find_endpoints(struct cdc_ncm_ctx *ctx, struct usb_interface *intf)
289 struct usb_host_endpoint *e;
292 for (ep = 0; ep < intf->cur_altsetting->desc.bNumEndpoints; ep++) {
294 e = intf->cur_altsetting->endpoint + ep;
295 switch (e->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
296 case USB_ENDPOINT_XFER_INT:
297 if (usb_endpoint_dir_in(&e->desc)) {
298 if (ctx->status_ep == NULL)
303 case USB_ENDPOINT_XFER_BULK:
304 if (usb_endpoint_dir_in(&e->desc)) {
305 if (ctx->in_ep == NULL)
308 if (ctx->out_ep == NULL)
319 static void cdc_ncm_free(struct cdc_ncm_ctx *ctx)
324 if (ctx->tx_rem_skb != NULL) {
325 dev_kfree_skb_any(ctx->tx_rem_skb);
326 ctx->tx_rem_skb = NULL;
329 if (ctx->tx_curr_skb != NULL) {
330 dev_kfree_skb_any(ctx->tx_curr_skb);
331 ctx->tx_curr_skb = NULL;
337 static const struct ethtool_ops cdc_ncm_ethtool_ops = {
338 .get_drvinfo = cdc_ncm_get_drvinfo,
339 .get_link = usbnet_get_link,
340 .get_msglevel = usbnet_get_msglevel,
341 .set_msglevel = usbnet_set_msglevel,
342 .get_settings = usbnet_get_settings,
343 .set_settings = usbnet_set_settings,
344 .nway_reset = usbnet_nway_reset,
347 int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_altsetting)
349 struct cdc_ncm_ctx *ctx;
350 struct usb_driver *driver;
356 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
360 hrtimer_init(&ctx->tx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
361 ctx->tx_timer.function = &cdc_ncm_tx_timer_cb;
362 ctx->bh.data = (unsigned long)ctx;
363 ctx->bh.func = cdc_ncm_txpath_bh;
364 atomic_set(&ctx->stop, 0);
365 spin_lock_init(&ctx->mtx);
366 ctx->netdev = dev->net;
368 /* store ctx pointer in device data field */
369 dev->data[0] = (unsigned long)ctx;
371 /* get some pointers */
372 driver = driver_of(intf);
373 buf = intf->cur_altsetting->extra;
374 len = intf->cur_altsetting->extralen;
376 ctx->udev = dev->udev;
379 /* parse through descriptors associated with control interface */
380 while ((len > 0) && (buf[0] > 2) && (buf[0] <= len)) {
382 if (buf[1] != USB_DT_CS_INTERFACE)
386 case USB_CDC_UNION_TYPE:
387 if (buf[0] < sizeof(*(ctx->union_desc)))
391 (const struct usb_cdc_union_desc *)buf;
393 ctx->control = usb_ifnum_to_if(dev->udev,
394 ctx->union_desc->bMasterInterface0);
395 ctx->data = usb_ifnum_to_if(dev->udev,
396 ctx->union_desc->bSlaveInterface0);
399 case USB_CDC_ETHERNET_TYPE:
400 if (buf[0] < sizeof(*(ctx->ether_desc)))
404 (const struct usb_cdc_ether_desc *)buf;
406 le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
408 if (dev->hard_mtu < CDC_NCM_MIN_DATAGRAM_SIZE)
409 dev->hard_mtu = CDC_NCM_MIN_DATAGRAM_SIZE;
410 else if (dev->hard_mtu > CDC_NCM_MAX_DATAGRAM_SIZE)
411 dev->hard_mtu = CDC_NCM_MAX_DATAGRAM_SIZE;
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;
432 /* advance to next descriptor */
438 /* some buggy devices have an IAD but no CDC Union */
439 if (!ctx->union_desc && intf->intf_assoc && intf->intf_assoc->bInterfaceCount == 2) {
441 ctx->data = usb_ifnum_to_if(dev->udev, intf->cur_altsetting->desc.bInterfaceNumber + 1);
442 dev_dbg(&intf->dev, "CDC Union missing - got slave from IAD\n");
445 /* check if we got everything */
446 if ((ctx->control == NULL) || (ctx->data == NULL) ||
447 ((!ctx->mbim_desc) && ((ctx->ether_desc == NULL) || (ctx->control != intf))))
450 /* claim data interface, if different from control */
451 if (ctx->data != ctx->control) {
452 temp = usb_driver_claim_interface(driver, ctx->data, dev);
457 iface_no = ctx->data->cur_altsetting->desc.bInterfaceNumber;
459 /* reset data interface */
460 temp = usb_set_interface(dev->udev, iface_no, 0);
464 /* initialize data interface */
465 if (cdc_ncm_setup(ctx))
468 /* configure data interface */
469 temp = usb_set_interface(dev->udev, iface_no, data_altsetting);
473 cdc_ncm_find_endpoints(ctx, ctx->data);
474 cdc_ncm_find_endpoints(ctx, ctx->control);
476 if ((ctx->in_ep == NULL) || (ctx->out_ep == NULL) ||
477 (ctx->status_ep == NULL))
480 dev->net->ethtool_ops = &cdc_ncm_ethtool_ops;
482 usb_set_intfdata(ctx->data, dev);
483 usb_set_intfdata(ctx->control, dev);
484 usb_set_intfdata(ctx->intf, dev);
486 if (ctx->ether_desc) {
487 temp = usbnet_get_ethernet_addr(dev, ctx->ether_desc->iMACAddress);
490 dev_info(&dev->udev->dev, "MAC-Address: %pM\n", dev->net->dev_addr);
494 dev->in = usb_rcvbulkpipe(dev->udev,
495 ctx->in_ep->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
496 dev->out = usb_sndbulkpipe(dev->udev,
497 ctx->out_ep->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
498 dev->status = ctx->status_ep;
499 dev->rx_urb_size = ctx->rx_max;
501 ctx->tx_speed = ctx->rx_speed = 0;
505 usb_set_intfdata(ctx->control, NULL);
506 usb_set_intfdata(ctx->data, NULL);
507 if (ctx->data != ctx->control)
508 usb_driver_release_interface(driver, ctx->data);
510 cdc_ncm_free((struct cdc_ncm_ctx *)dev->data[0]);
512 dev_info(&dev->udev->dev, "bind() failure\n");
515 EXPORT_SYMBOL_GPL(cdc_ncm_bind_common);
517 void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf)
519 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
520 struct usb_driver *driver = driver_of(intf);
523 return; /* no setup */
525 atomic_set(&ctx->stop, 1);
527 if (hrtimer_active(&ctx->tx_timer))
528 hrtimer_cancel(&ctx->tx_timer);
530 tasklet_kill(&ctx->bh);
532 /* handle devices with combined control and data interface */
533 if (ctx->control == ctx->data)
536 /* disconnect master --> disconnect slave */
537 if (intf == ctx->control && ctx->data) {
538 usb_set_intfdata(ctx->data, NULL);
539 usb_driver_release_interface(driver, ctx->data);
542 } else if (intf == ctx->data && ctx->control) {
543 usb_set_intfdata(ctx->control, NULL);
544 usb_driver_release_interface(driver, ctx->control);
548 usb_set_intfdata(ctx->intf, NULL);
551 EXPORT_SYMBOL_GPL(cdc_ncm_unbind);
553 static int cdc_ncm_bind(struct usbnet *dev, struct usb_interface *intf)
557 /* The MBIM spec defines a NCM compatible default altsetting,
558 * which we may have matched:
560 * "Functions that implement both NCM 1.0 and MBIM (an
561 * “NCM/MBIM function”) according to this recommendation
562 * shall provide two alternate settings for the
563 * Communication Interface. Alternate setting 0, and the
564 * associated class and endpoint descriptors, shall be
565 * constructed according to the rules given for the
566 * Communication Interface in section 5 of [USBNCM10].
567 * Alternate setting 1, and the associated class and
568 * endpoint descriptors, shall be constructed according to
569 * the rules given in section 6 (USB Device Model) of this
572 * Do not bind to such interfaces, allowing cdc_mbim to handle
575 #if IS_ENABLED(CONFIG_USB_NET_CDC_MBIM)
576 if ((intf->num_altsetting == 2) &&
577 !usb_set_interface(dev->udev,
578 intf->cur_altsetting->desc.bInterfaceNumber,
579 CDC_NCM_COMM_ALTSETTING_MBIM)) {
580 if (cdc_ncm_comm_intf_is_mbim(intf->cur_altsetting))
583 usb_set_interface(dev->udev,
584 intf->cur_altsetting->desc.bInterfaceNumber,
585 CDC_NCM_COMM_ALTSETTING_NCM);
589 /* NCM data altsetting is always 1 */
590 ret = cdc_ncm_bind_common(dev, intf, 1);
593 * We should get an event when network connection is "connected" or
594 * "disconnected". Set network connection in "disconnected" state
595 * (carrier is OFF) during attach, so the IP network stack does not
596 * start IPv6 negotiation and more.
598 netif_carrier_off(dev->net);
602 static void cdc_ncm_align_tail(struct sk_buff *skb, size_t modulus, size_t remainder, size_t max)
604 size_t align = ALIGN(skb->len, modulus) - skb->len + remainder;
606 if (skb->len + align > max)
607 align = max - skb->len;
608 if (align && skb_tailroom(skb) >= align)
609 memset(skb_put(skb, align), 0, align);
612 /* return a pointer to a valid struct usb_cdc_ncm_ndp16 of type sign, possibly
613 * allocating a new one within skb
615 static struct usb_cdc_ncm_ndp16 *cdc_ncm_ndp(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign, size_t reserve)
617 struct usb_cdc_ncm_ndp16 *ndp16 = NULL;
618 struct usb_cdc_ncm_nth16 *nth16 = (void *)skb->data;
619 size_t ndpoffset = le16_to_cpu(nth16->wNdpIndex);
621 /* follow the chain of NDPs, looking for a match */
623 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb->data + ndpoffset);
624 if (ndp16->dwSignature == sign)
626 ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
630 cdc_ncm_align_tail(skb, ctx->tx_ndp_modulus, 0, ctx->tx_max);
632 /* verify that there is room for the NDP and the datagram (reserve) */
633 if ((ctx->tx_max - skb->len - reserve) < CDC_NCM_NDP_SIZE)
638 ndp16->wNextNdpIndex = cpu_to_le16(skb->len);
640 nth16->wNdpIndex = cpu_to_le16(skb->len);
642 /* push a new empty NDP */
643 ndp16 = (struct usb_cdc_ncm_ndp16 *)memset(skb_put(skb, CDC_NCM_NDP_SIZE), 0, CDC_NCM_NDP_SIZE);
644 ndp16->dwSignature = sign;
645 ndp16->wLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_ndp16) + sizeof(struct usb_cdc_ncm_dpe16));
650 cdc_ncm_fill_tx_frame(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign)
652 struct usb_cdc_ncm_nth16 *nth16;
653 struct usb_cdc_ncm_ndp16 *ndp16;
654 struct sk_buff *skb_out;
655 u16 n = 0, index, ndplen;
658 /* if there is a remaining skb, it gets priority */
660 swap(skb, ctx->tx_rem_skb);
661 swap(sign, ctx->tx_rem_sign);
666 /* check if we are resuming an OUT skb */
667 skb_out = ctx->tx_curr_skb;
669 /* allocate a new OUT skb */
671 skb_out = alloc_skb((ctx->tx_max + 1), GFP_ATOMIC);
672 if (skb_out == NULL) {
674 dev_kfree_skb_any(skb);
675 ctx->netdev->stats.tx_dropped++;
679 /* fill out the initial 16-bit NTB header */
680 nth16 = (struct usb_cdc_ncm_nth16 *)memset(skb_put(skb_out, sizeof(struct usb_cdc_ncm_nth16)), 0, sizeof(struct usb_cdc_ncm_nth16));
681 nth16->dwSignature = cpu_to_le32(USB_CDC_NCM_NTH16_SIGN);
682 nth16->wHeaderLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_nth16));
683 nth16->wSequence = cpu_to_le16(ctx->tx_seq++);
685 /* count total number of frames in this NTB */
686 ctx->tx_curr_frame_num = 0;
689 for (n = ctx->tx_curr_frame_num; n < ctx->tx_max_datagrams; n++) {
690 /* send any remaining skb first */
692 skb = ctx->tx_rem_skb;
693 sign = ctx->tx_rem_sign;
694 ctx->tx_rem_skb = NULL;
696 /* check for end of skb */
701 /* get the appropriate NDP for this skb */
702 ndp16 = cdc_ncm_ndp(ctx, skb_out, sign, skb->len + ctx->tx_modulus + ctx->tx_remainder);
704 /* align beginning of next frame */
705 cdc_ncm_align_tail(skb_out, ctx->tx_modulus, ctx->tx_remainder, ctx->tx_max);
707 /* check if we had enough room left for both NDP and frame */
708 if (!ndp16 || skb_out->len + skb->len > ctx->tx_max) {
710 /* won't fit, MTU problem? */
711 dev_kfree_skb_any(skb);
713 ctx->netdev->stats.tx_dropped++;
715 /* no room for skb - store for later */
716 if (ctx->tx_rem_skb != NULL) {
717 dev_kfree_skb_any(ctx->tx_rem_skb);
718 ctx->netdev->stats.tx_dropped++;
720 ctx->tx_rem_skb = skb;
721 ctx->tx_rem_sign = sign;
728 /* calculate frame number withing this NDP */
729 ndplen = le16_to_cpu(ndp16->wLength);
730 index = (ndplen - sizeof(struct usb_cdc_ncm_ndp16)) / sizeof(struct usb_cdc_ncm_dpe16) - 1;
732 /* OK, add this skb */
733 ndp16->dpe16[index].wDatagramLength = cpu_to_le16(skb->len);
734 ndp16->dpe16[index].wDatagramIndex = cpu_to_le16(skb_out->len);
735 ndp16->wLength = cpu_to_le16(ndplen + sizeof(struct usb_cdc_ncm_dpe16));
736 memcpy(skb_put(skb_out, skb->len), skb->data, skb->len);
737 dev_kfree_skb_any(skb);
740 /* send now if this NDP is full */
741 if (index >= CDC_NCM_DPT_DATAGRAMS_MAX) {
747 /* free up any dangling skb */
749 dev_kfree_skb_any(skb);
751 ctx->netdev->stats.tx_dropped++;
754 ctx->tx_curr_frame_num = n;
757 /* wait for more frames */
759 ctx->tx_curr_skb = skb_out;
762 } else if ((n < ctx->tx_max_datagrams) && (ready2send == 0)) {
763 /* wait for more frames */
765 ctx->tx_curr_skb = skb_out;
766 /* set the pending count */
767 if (n < CDC_NCM_RESTART_TIMER_DATAGRAM_CNT)
768 ctx->tx_timer_pending = CDC_NCM_TIMER_PENDING_CNT;
773 /* variables will be reset at next call */
777 * If collected data size is less or equal CDC_NCM_MIN_TX_PKT bytes,
778 * we send buffers as it is. If we get more data, it would be more
779 * efficient for USB HS mobile device with DMA engine to receive a full
780 * size NTB, than canceling DMA transfer and receiving a short packet.
782 if (skb_out->len > CDC_NCM_MIN_TX_PKT)
783 /* final zero padding */
784 memset(skb_put(skb_out, ctx->tx_max - skb_out->len), 0, ctx->tx_max - skb_out->len);
786 /* do we need to prevent a ZLP? */
787 if (((skb_out->len % le16_to_cpu(ctx->out_ep->desc.wMaxPacketSize)) == 0) &&
788 (skb_out->len < le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize)) && skb_tailroom(skb_out))
789 *skb_put(skb_out, 1) = 0; /* force short packet */
791 /* set final frame length */
792 nth16 = (struct usb_cdc_ncm_nth16 *)skb_out->data;
793 nth16->wBlockLength = cpu_to_le16(skb_out->len);
796 ctx->tx_curr_skb = NULL;
797 ctx->netdev->stats.tx_packets += ctx->tx_curr_frame_num;
801 /* Start timer, if there is a remaining skb */
802 if (ctx->tx_curr_skb != NULL)
803 cdc_ncm_tx_timeout_start(ctx);
806 EXPORT_SYMBOL_GPL(cdc_ncm_fill_tx_frame);
808 static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx)
810 /* start timer, if not already started */
811 if (!(hrtimer_active(&ctx->tx_timer) || atomic_read(&ctx->stop)))
812 hrtimer_start(&ctx->tx_timer,
813 ktime_set(0, CDC_NCM_TIMER_INTERVAL),
817 static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *timer)
819 struct cdc_ncm_ctx *ctx =
820 container_of(timer, struct cdc_ncm_ctx, tx_timer);
822 if (!atomic_read(&ctx->stop))
823 tasklet_schedule(&ctx->bh);
824 return HRTIMER_NORESTART;
827 static void cdc_ncm_txpath_bh(unsigned long param)
829 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)param;
831 spin_lock_bh(&ctx->mtx);
832 if (ctx->tx_timer_pending != 0) {
833 ctx->tx_timer_pending--;
834 cdc_ncm_tx_timeout_start(ctx);
835 spin_unlock_bh(&ctx->mtx);
836 } else if (ctx->netdev != NULL) {
837 spin_unlock_bh(&ctx->mtx);
838 netif_tx_lock_bh(ctx->netdev);
839 usbnet_start_xmit(NULL, ctx->netdev);
840 netif_tx_unlock_bh(ctx->netdev);
842 spin_unlock_bh(&ctx->mtx);
846 static struct sk_buff *
847 cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
849 struct sk_buff *skb_out;
850 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
853 * The Ethernet API we are using does not support transmitting
854 * multiple Ethernet frames in a single call. This driver will
855 * accumulate multiple Ethernet frames and send out a larger
856 * USB frame when the USB buffer is full or when a single jiffies
862 spin_lock_bh(&ctx->mtx);
863 skb_out = cdc_ncm_fill_tx_frame(ctx, skb, cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN));
864 spin_unlock_bh(&ctx->mtx);
869 dev_kfree_skb_any(skb);
874 /* verify NTB header and return offset of first NDP, or negative error */
875 int cdc_ncm_rx_verify_nth16(struct cdc_ncm_ctx *ctx, struct sk_buff *skb_in)
877 struct usb_cdc_ncm_nth16 *nth16;
884 if (skb_in->len < (sizeof(struct usb_cdc_ncm_nth16) +
885 sizeof(struct usb_cdc_ncm_ndp16))) {
886 pr_debug("frame too short\n");
890 nth16 = (struct usb_cdc_ncm_nth16 *)skb_in->data;
892 if (le32_to_cpu(nth16->dwSignature) != USB_CDC_NCM_NTH16_SIGN) {
893 pr_debug("invalid NTH16 signature <%u>\n",
894 le32_to_cpu(nth16->dwSignature));
898 len = le16_to_cpu(nth16->wBlockLength);
899 if (len > ctx->rx_max) {
900 pr_debug("unsupported NTB block length %u/%u\n", len,
905 if ((ctx->rx_seq + 1) != le16_to_cpu(nth16->wSequence) &&
906 (ctx->rx_seq || le16_to_cpu(nth16->wSequence)) &&
907 !((ctx->rx_seq == 0xffff) && !le16_to_cpu(nth16->wSequence))) {
908 pr_debug("sequence number glitch prev=%d curr=%d\n",
909 ctx->rx_seq, le16_to_cpu(nth16->wSequence));
911 ctx->rx_seq = le16_to_cpu(nth16->wSequence);
913 ret = le16_to_cpu(nth16->wNdpIndex);
917 EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_nth16);
919 /* verify NDP header and return number of datagrams, or negative error */
920 int cdc_ncm_rx_verify_ndp16(struct sk_buff *skb_in, int ndpoffset)
922 struct usb_cdc_ncm_ndp16 *ndp16;
925 if ((ndpoffset + sizeof(struct usb_cdc_ncm_ndp16)) > skb_in->len) {
926 pr_debug("invalid NDP offset <%u>\n", ndpoffset);
929 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
931 if (le16_to_cpu(ndp16->wLength) < USB_CDC_NCM_NDP16_LENGTH_MIN) {
932 pr_debug("invalid DPT16 length <%u>\n",
933 le32_to_cpu(ndp16->dwSignature));
937 ret = ((le16_to_cpu(ndp16->wLength) -
938 sizeof(struct usb_cdc_ncm_ndp16)) /
939 sizeof(struct usb_cdc_ncm_dpe16));
940 ret--; /* we process NDP entries except for the last one */
942 if ((sizeof(struct usb_cdc_ncm_ndp16) + ret * (sizeof(struct usb_cdc_ncm_dpe16))) >
944 pr_debug("Invalid nframes = %d\n", ret);
951 EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_ndp16);
953 static int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
956 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
961 struct usb_cdc_ncm_ndp16 *ndp16;
962 struct usb_cdc_ncm_dpe16 *dpe16;
964 int loopcount = 50; /* arbitrary max preventing infinite loop */
966 ndpoffset = cdc_ncm_rx_verify_nth16(ctx, skb_in);
971 nframes = cdc_ncm_rx_verify_ndp16(skb_in, ndpoffset);
975 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
977 if (le32_to_cpu(ndp16->dwSignature) != USB_CDC_NCM_NDP16_NOCRC_SIGN) {
978 pr_debug("invalid DPT16 signature <%u>\n",
979 le32_to_cpu(ndp16->dwSignature));
982 dpe16 = ndp16->dpe16;
984 for (x = 0; x < nframes; x++, dpe16++) {
985 offset = le16_to_cpu(dpe16->wDatagramIndex);
986 len = le16_to_cpu(dpe16->wDatagramLength);
990 * All entries after first NULL entry are to be ignored
992 if ((offset == 0) || (len == 0)) {
994 goto err_ndp; /* empty NTB */
998 /* sanity checking */
999 if (((offset + len) > skb_in->len) ||
1000 (len > ctx->rx_max) || (len < ETH_HLEN)) {
1001 pr_debug("invalid frame detected (ignored)"
1002 "offset[%u]=%u, length=%u, skb=%p\n",
1003 x, offset, len, skb_in);
1009 skb = skb_clone(skb_in, GFP_ATOMIC);
1013 skb->data = ((u8 *)skb_in->data) + offset;
1014 skb_set_tail_pointer(skb, len);
1015 usbnet_skb_return(dev, skb);
1019 /* are there more NDPs to process? */
1020 ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
1021 if (ndpoffset && loopcount--)
1030 cdc_ncm_speed_change(struct cdc_ncm_ctx *ctx,
1031 struct usb_cdc_speed_change *data)
1033 uint32_t rx_speed = le32_to_cpu(data->DLBitRRate);
1034 uint32_t tx_speed = le32_to_cpu(data->ULBitRate);
1037 * Currently the USB-NET API does not support reporting the actual
1038 * device speed. Do print it instead.
1040 if ((tx_speed != ctx->tx_speed) || (rx_speed != ctx->rx_speed)) {
1041 ctx->tx_speed = tx_speed;
1042 ctx->rx_speed = rx_speed;
1044 if ((tx_speed > 1000000) && (rx_speed > 1000000)) {
1045 printk(KERN_INFO KBUILD_MODNAME
1046 ": %s: %u mbit/s downlink "
1047 "%u mbit/s uplink\n",
1049 (unsigned int)(rx_speed / 1000000U),
1050 (unsigned int)(tx_speed / 1000000U));
1052 printk(KERN_INFO KBUILD_MODNAME
1053 ": %s: %u kbit/s downlink "
1054 "%u kbit/s uplink\n",
1056 (unsigned int)(rx_speed / 1000U),
1057 (unsigned int)(tx_speed / 1000U));
1062 static void cdc_ncm_status(struct usbnet *dev, struct urb *urb)
1064 struct cdc_ncm_ctx *ctx;
1065 struct usb_cdc_notification *event;
1067 ctx = (struct cdc_ncm_ctx *)dev->data[0];
1069 if (urb->actual_length < sizeof(*event))
1072 /* test for split data in 8-byte chunks */
1073 if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
1074 cdc_ncm_speed_change(ctx,
1075 (struct usb_cdc_speed_change *)urb->transfer_buffer);
1079 event = urb->transfer_buffer;
1081 switch (event->bNotificationType) {
1082 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
1084 * According to the CDC NCM specification ch.7.1
1085 * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be
1086 * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE.
1088 ctx->connected = le16_to_cpu(event->wValue);
1090 printk(KERN_INFO KBUILD_MODNAME ": %s: network connection:"
1092 ctx->netdev->name, ctx->connected ? "" : "dis");
1095 netif_carrier_on(dev->net);
1097 netif_carrier_off(dev->net);
1098 ctx->tx_speed = ctx->rx_speed = 0;
1102 case USB_CDC_NOTIFY_SPEED_CHANGE:
1103 if (urb->actual_length < (sizeof(*event) +
1104 sizeof(struct usb_cdc_speed_change)))
1105 set_bit(EVENT_STS_SPLIT, &dev->flags);
1107 cdc_ncm_speed_change(ctx,
1108 (struct usb_cdc_speed_change *) &event[1]);
1112 dev_err(&dev->udev->dev, "NCM: unexpected "
1113 "notification 0x%02x!\n", event->bNotificationType);
1118 static int cdc_ncm_check_connect(struct usbnet *dev)
1120 struct cdc_ncm_ctx *ctx;
1122 ctx = (struct cdc_ncm_ctx *)dev->data[0];
1124 return 1; /* disconnected */
1126 return !ctx->connected;
1130 cdc_ncm_probe(struct usb_interface *udev, const struct usb_device_id *prod)
1132 return usbnet_probe(udev, prod);
1135 static void cdc_ncm_disconnect(struct usb_interface *intf)
1137 struct usbnet *dev = usb_get_intfdata(intf);
1140 return; /* already disconnected */
1142 usbnet_disconnect(intf);
1145 static const struct driver_info cdc_ncm_info = {
1146 .description = "CDC NCM",
1147 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET,
1148 .bind = cdc_ncm_bind,
1149 .unbind = cdc_ncm_unbind,
1150 .check_connect = cdc_ncm_check_connect,
1151 .manage_power = usbnet_manage_power,
1152 .status = cdc_ncm_status,
1153 .rx_fixup = cdc_ncm_rx_fixup,
1154 .tx_fixup = cdc_ncm_tx_fixup,
1157 /* Same as cdc_ncm_info, but with FLAG_WWAN */
1158 static const struct driver_info wwan_info = {
1159 .description = "Mobile Broadband Network Device",
1160 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
1162 .bind = cdc_ncm_bind,
1163 .unbind = cdc_ncm_unbind,
1164 .check_connect = cdc_ncm_check_connect,
1165 .manage_power = usbnet_manage_power,
1166 .status = cdc_ncm_status,
1167 .rx_fixup = cdc_ncm_rx_fixup,
1168 .tx_fixup = cdc_ncm_tx_fixup,
1171 /* Same as wwan_info, but with FLAG_NOARP */
1172 static const struct driver_info wwan_noarp_info = {
1173 .description = "Mobile Broadband Network Device (NO ARP)",
1174 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
1175 | FLAG_WWAN | FLAG_NOARP,
1176 .bind = cdc_ncm_bind,
1177 .unbind = cdc_ncm_unbind,
1178 .check_connect = cdc_ncm_check_connect,
1179 .manage_power = usbnet_manage_power,
1180 .status = cdc_ncm_status,
1181 .rx_fixup = cdc_ncm_rx_fixup,
1182 .tx_fixup = cdc_ncm_tx_fixup,
1185 static const struct usb_device_id cdc_devs[] = {
1186 /* Ericsson MBM devices like F5521gw */
1187 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1188 | USB_DEVICE_ID_MATCH_VENDOR,
1190 .bInterfaceClass = USB_CLASS_COMM,
1191 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1192 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1193 .driver_info = (unsigned long) &wwan_info,
1196 /* Dell branded MBM devices like DW5550 */
1197 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1198 | USB_DEVICE_ID_MATCH_VENDOR,
1200 .bInterfaceClass = USB_CLASS_COMM,
1201 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1202 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1203 .driver_info = (unsigned long) &wwan_info,
1206 /* Toshiba branded MBM devices */
1207 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1208 | USB_DEVICE_ID_MATCH_VENDOR,
1210 .bInterfaceClass = USB_CLASS_COMM,
1211 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1212 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1213 .driver_info = (unsigned long) &wwan_info,
1216 /* tag Huawei devices as wwan */
1217 { USB_VENDOR_AND_INTERFACE_INFO(0x12d1,
1219 USB_CDC_SUBCLASS_NCM,
1220 USB_CDC_PROTO_NONE),
1221 .driver_info = (unsigned long)&wwan_info,
1224 /* Huawei NCM devices disguised as vendor specific */
1225 { USB_VENDOR_AND_INTERFACE_INFO(0x12d1, 0xff, 0x02, 0x16),
1226 .driver_info = (unsigned long)&wwan_info,
1228 { USB_VENDOR_AND_INTERFACE_INFO(0x12d1, 0xff, 0x02, 0x46),
1229 .driver_info = (unsigned long)&wwan_info,
1231 { USB_VENDOR_AND_INTERFACE_INFO(0x12d1, 0xff, 0x02, 0x76),
1232 .driver_info = (unsigned long)&wwan_info,
1235 /* Infineon(now Intel) HSPA Modem platform */
1236 { USB_DEVICE_AND_INTERFACE_INFO(0x1519, 0x0443,
1238 USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
1239 .driver_info = (unsigned long)&wwan_noarp_info,
1242 /* Generic CDC-NCM devices */
1243 { USB_INTERFACE_INFO(USB_CLASS_COMM,
1244 USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
1245 .driver_info = (unsigned long)&cdc_ncm_info,
1250 MODULE_DEVICE_TABLE(usb, cdc_devs);
1252 static struct usb_driver cdc_ncm_driver = {
1254 .id_table = cdc_devs,
1255 .probe = cdc_ncm_probe,
1256 .disconnect = cdc_ncm_disconnect,
1257 .suspend = usbnet_suspend,
1258 .resume = usbnet_resume,
1259 .reset_resume = usbnet_resume,
1260 .supports_autosuspend = 1,
1261 .disable_hub_initiated_lpm = 1,
1264 module_usb_driver(cdc_ncm_driver);
1266 MODULE_AUTHOR("Hans Petter Selasky");
1267 MODULE_DESCRIPTION("USB CDC NCM host driver");
1268 MODULE_LICENSE("Dual BSD/GPL");