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>
55 #define DRIVER_VERSION "14-Mar-2012"
57 /* CDC NCM subclass 3.2.1 */
58 #define USB_CDC_NCM_NDP16_LENGTH_MIN 0x10
60 /* Maximum NTB length */
61 #define CDC_NCM_NTB_MAX_SIZE_TX 32768 /* bytes */
62 #define CDC_NCM_NTB_MAX_SIZE_RX 32768 /* bytes */
64 /* Minimum value for MaxDatagramSize, ch. 6.2.9 */
65 #define CDC_NCM_MIN_DATAGRAM_SIZE 1514 /* bytes */
67 /* Minimum value for MaxDatagramSize, ch. 8.1.3 */
68 #define CDC_MBIM_MIN_DATAGRAM_SIZE 2048 /* bytes */
70 #define CDC_NCM_MIN_TX_PKT 512 /* bytes */
72 /* Default value for MaxDatagramSize */
73 #define CDC_NCM_MAX_DATAGRAM_SIZE 8192 /* bytes */
76 * Maximum amount of datagrams in NCM Datagram Pointer Table, not counting
77 * the last NULL entry.
79 #define CDC_NCM_DPT_DATAGRAMS_MAX 40
81 /* Restart the timer, if amount of datagrams is less than given value */
82 #define CDC_NCM_RESTART_TIMER_DATAGRAM_CNT 3
83 #define CDC_NCM_TIMER_PENDING_CNT 2
84 #define CDC_NCM_TIMER_INTERVAL (400UL * NSEC_PER_USEC)
86 /* The following macro defines the minimum header space */
87 #define CDC_NCM_MIN_HDR_SIZE \
88 (sizeof(struct usb_cdc_ncm_nth16) + sizeof(struct usb_cdc_ncm_ndp16) + \
89 (CDC_NCM_DPT_DATAGRAMS_MAX + 1) * sizeof(struct usb_cdc_ncm_dpe16))
91 #define CDC_NCM_NDP_SIZE \
92 (sizeof(struct usb_cdc_ncm_ndp16) + \
93 (CDC_NCM_DPT_DATAGRAMS_MAX + 1) * sizeof(struct usb_cdc_ncm_dpe16))
96 struct usb_cdc_ncm_ntb_parameters ncm_parm;
97 struct hrtimer tx_timer;
98 struct tasklet_struct bh;
100 const struct usb_cdc_ncm_desc *func_desc;
101 const struct usb_cdc_mbim_desc *mbim_desc;
102 const struct usb_cdc_header_desc *header_desc;
103 const struct usb_cdc_union_desc *union_desc;
104 const struct usb_cdc_ether_desc *ether_desc;
106 struct net_device *netdev;
107 struct usb_device *udev;
108 struct usb_host_endpoint *in_ep;
109 struct usb_host_endpoint *out_ep;
110 struct usb_host_endpoint *status_ep;
111 struct usb_interface *intf;
112 struct usb_interface *control;
113 struct usb_interface *data;
115 struct sk_buff *tx_curr_skb;
116 struct sk_buff *tx_rem_skb;
122 u32 tx_timer_pending;
123 u32 tx_curr_frame_num;
128 u32 max_datagram_size;
129 u16 tx_max_datagrams;
138 static void cdc_ncm_txpath_bh(unsigned long param);
139 static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx);
140 static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *hr_timer);
141 static struct usb_driver cdc_ncm_driver;
144 cdc_ncm_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
146 struct usbnet *dev = netdev_priv(net);
148 strncpy(info->driver, dev->driver_name, sizeof(info->driver));
149 strncpy(info->version, DRIVER_VERSION, sizeof(info->version));
150 strncpy(info->fw_version, dev->driver_info->description,
151 sizeof(info->fw_version));
152 usb_make_path(dev->udev, info->bus_info, sizeof(info->bus_info));
155 static u8 cdc_ncm_setup(struct cdc_ncm_ctx *ctx)
162 u16 ntb_fmt_supported;
166 iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber;
168 err = usb_control_msg(ctx->udev,
169 usb_rcvctrlpipe(ctx->udev, 0),
170 USB_CDC_GET_NTB_PARAMETERS,
171 USB_TYPE_CLASS | USB_DIR_IN
172 | USB_RECIP_INTERFACE,
173 0, iface_no, &ctx->ncm_parm,
174 sizeof(ctx->ncm_parm), 10000);
176 pr_debug("failed GET_NTB_PARAMETERS\n");
180 /* read correct set of parameters according to device mode */
181 ctx->rx_max = le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize);
182 ctx->tx_max = le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize);
183 ctx->tx_remainder = le16_to_cpu(ctx->ncm_parm.wNdpOutPayloadRemainder);
184 ctx->tx_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutDivisor);
185 ctx->tx_ndp_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutAlignment);
186 /* devices prior to NCM Errata shall set this field to zero */
187 ctx->tx_max_datagrams = le16_to_cpu(ctx->ncm_parm.wNtbOutMaxDatagrams);
188 ntb_fmt_supported = le16_to_cpu(ctx->ncm_parm.bmNtbFormatsSupported);
191 min_dgram_size = CDC_NCM_MIN_DATAGRAM_SIZE;
192 min_hdr_size = CDC_NCM_MIN_HDR_SIZE;
193 if (ctx->mbim_desc != NULL) {
194 flags = ctx->mbim_desc->bmNetworkCapabilities;
196 min_dgram_size = CDC_MBIM_MIN_DATAGRAM_SIZE;
198 } else if (ctx->func_desc != NULL) {
199 flags = ctx->func_desc->bmNetworkCapabilities;
204 pr_debug("dwNtbInMaxSize=%u dwNtbOutMaxSize=%u "
205 "wNdpOutPayloadRemainder=%u wNdpOutDivisor=%u "
206 "wNdpOutAlignment=%u wNtbOutMaxDatagrams=%u flags=0x%x\n",
207 ctx->rx_max, ctx->tx_max, ctx->tx_remainder, ctx->tx_modulus,
208 ctx->tx_ndp_modulus, ctx->tx_max_datagrams, flags);
210 /* max count of tx datagrams */
211 if ((ctx->tx_max_datagrams == 0) ||
212 (ctx->tx_max_datagrams > CDC_NCM_DPT_DATAGRAMS_MAX))
213 ctx->tx_max_datagrams = CDC_NCM_DPT_DATAGRAMS_MAX;
215 /* verify maximum size of received NTB in bytes */
216 if (ctx->rx_max < USB_CDC_NCM_NTB_MIN_IN_SIZE) {
217 pr_debug("Using min receive length=%d\n",
218 USB_CDC_NCM_NTB_MIN_IN_SIZE);
219 ctx->rx_max = USB_CDC_NCM_NTB_MIN_IN_SIZE;
222 if (ctx->rx_max > CDC_NCM_NTB_MAX_SIZE_RX) {
223 pr_debug("Using default maximum receive length=%d\n",
224 CDC_NCM_NTB_MAX_SIZE_RX);
225 ctx->rx_max = CDC_NCM_NTB_MAX_SIZE_RX;
228 /* inform device about NTB input size changes */
229 if (ctx->rx_max != le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize)) {
230 __le32 *dwNtbInMaxSize;
232 dwNtbInMaxSize = kzalloc(sizeof(*dwNtbInMaxSize), GFP_KERNEL);
233 if (!dwNtbInMaxSize) {
237 *dwNtbInMaxSize = cpu_to_le32(ctx->rx_max);
238 err = usb_control_msg(ctx->udev,
239 usb_sndctrlpipe(ctx->udev, 0),
240 USB_CDC_SET_NTB_INPUT_SIZE,
241 USB_TYPE_CLASS | USB_DIR_OUT
242 | USB_RECIP_INTERFACE,
243 0, iface_no, dwNtbInMaxSize, 4, 1000);
244 kfree(dwNtbInMaxSize);
247 pr_debug("Setting NTB Input Size failed\n");
250 /* verify maximum size of transmitted NTB in bytes */
252 (min_hdr_size + min_dgram_size)) ||
253 (ctx->tx_max > CDC_NCM_NTB_MAX_SIZE_TX)) {
254 pr_debug("Using default maximum transmit length=%d\n",
255 CDC_NCM_NTB_MAX_SIZE_TX);
256 ctx->tx_max = CDC_NCM_NTB_MAX_SIZE_TX;
260 * verify that the structure alignment is:
262 * - not greater than the maximum transmit length
263 * - not less than four bytes
265 val = ctx->tx_ndp_modulus;
267 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
268 (val != ((-val) & val)) || (val >= ctx->tx_max)) {
269 pr_debug("Using default alignment: 4 bytes\n");
270 ctx->tx_ndp_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
274 * verify that the payload alignment is:
276 * - not greater than the maximum transmit length
277 * - not less than four bytes
279 val = ctx->tx_modulus;
281 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
282 (val != ((-val) & val)) || (val >= ctx->tx_max)) {
283 pr_debug("Using default transmit modulus: 4 bytes\n");
284 ctx->tx_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
287 /* verify the payload remainder */
288 if (ctx->tx_remainder >= ctx->tx_modulus) {
289 pr_debug("Using default transmit remainder: 0 bytes\n");
290 ctx->tx_remainder = 0;
293 /* adjust TX-remainder according to NCM specification. */
294 ctx->tx_remainder = ((ctx->tx_remainder - eth_hlen) &
295 (ctx->tx_modulus - 1));
297 /* additional configuration */
300 if (flags & USB_CDC_NCM_NCAP_CRC_MODE) {
301 err = usb_control_msg(ctx->udev, usb_sndctrlpipe(ctx->udev, 0),
302 USB_CDC_SET_CRC_MODE,
303 USB_TYPE_CLASS | USB_DIR_OUT
304 | USB_RECIP_INTERFACE,
305 USB_CDC_NCM_CRC_NOT_APPENDED,
306 iface_no, NULL, 0, 1000);
308 pr_debug("Setting CRC mode off failed\n");
311 /* set NTB format, if both formats are supported */
312 if (ntb_fmt_supported & USB_CDC_NCM_NTH32_SIGN) {
313 err = usb_control_msg(ctx->udev, usb_sndctrlpipe(ctx->udev, 0),
314 USB_CDC_SET_NTB_FORMAT, USB_TYPE_CLASS
315 | USB_DIR_OUT | USB_RECIP_INTERFACE,
316 USB_CDC_NCM_NTB16_FORMAT,
317 iface_no, NULL, 0, 1000);
319 pr_debug("Setting NTB format to 16-bit failed\n");
322 ctx->max_datagram_size = min_dgram_size;
324 /* set Max Datagram Size (MTU) */
325 if (flags & USB_CDC_NCM_NCAP_MAX_DATAGRAM_SIZE) {
326 __le16 *max_datagram_size;
328 if (ctx->ether_desc != NULL)
329 eth_max_sz = le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
330 else if (ctx->mbim_desc != NULL)
331 eth_max_sz = le16_to_cpu(ctx->mbim_desc->wMaxSegmentSize);
335 max_datagram_size = kzalloc(sizeof(*max_datagram_size),
337 if (!max_datagram_size) {
342 err = usb_control_msg(ctx->udev, usb_rcvctrlpipe(ctx->udev, 0),
343 USB_CDC_GET_MAX_DATAGRAM_SIZE,
344 USB_TYPE_CLASS | USB_DIR_IN
345 | USB_RECIP_INTERFACE,
346 0, iface_no, max_datagram_size,
349 pr_debug("GET_MAX_DATAGRAM_SIZE failed, use size=%u\n",
352 ctx->max_datagram_size =
353 le16_to_cpu(*max_datagram_size);
354 /* Check Eth descriptor value */
355 if (ctx->max_datagram_size > eth_max_sz)
356 ctx->max_datagram_size = eth_max_sz;
358 if (ctx->max_datagram_size > CDC_NCM_MAX_DATAGRAM_SIZE)
359 ctx->max_datagram_size = CDC_NCM_MAX_DATAGRAM_SIZE;
361 if (ctx->max_datagram_size < min_dgram_size)
362 ctx->max_datagram_size = min_dgram_size;
364 /* if value changed, update device */
365 if (ctx->max_datagram_size !=
366 le16_to_cpu(*max_datagram_size)) {
367 err = usb_control_msg(ctx->udev,
368 usb_sndctrlpipe(ctx->udev, 0),
369 USB_CDC_SET_MAX_DATAGRAM_SIZE,
370 USB_TYPE_CLASS | USB_DIR_OUT
371 | USB_RECIP_INTERFACE,
373 iface_no, max_datagram_size,
376 pr_debug("SET_MAX_DGRAM_SIZE failed\n");
379 kfree(max_datagram_size);
383 if (ctx->netdev->mtu != (ctx->max_datagram_size - eth_hlen))
384 ctx->netdev->mtu = ctx->max_datagram_size - eth_hlen;
390 cdc_ncm_find_endpoints(struct cdc_ncm_ctx *ctx, struct usb_interface *intf)
392 struct usb_host_endpoint *e;
395 for (ep = 0; ep < intf->cur_altsetting->desc.bNumEndpoints; ep++) {
397 e = intf->cur_altsetting->endpoint + ep;
398 switch (e->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
399 case USB_ENDPOINT_XFER_INT:
400 if (usb_endpoint_dir_in(&e->desc)) {
401 if (ctx->status_ep == NULL)
406 case USB_ENDPOINT_XFER_BULK:
407 if (usb_endpoint_dir_in(&e->desc)) {
408 if (ctx->in_ep == NULL)
411 if (ctx->out_ep == NULL)
422 static void cdc_ncm_free(struct cdc_ncm_ctx *ctx)
427 if (ctx->tx_rem_skb != NULL) {
428 dev_kfree_skb_any(ctx->tx_rem_skb);
429 ctx->tx_rem_skb = NULL;
432 if (ctx->tx_curr_skb != NULL) {
433 dev_kfree_skb_any(ctx->tx_curr_skb);
434 ctx->tx_curr_skb = NULL;
440 static const struct ethtool_ops cdc_ncm_ethtool_ops = {
441 .get_drvinfo = cdc_ncm_get_drvinfo,
442 .get_link = usbnet_get_link,
443 .get_msglevel = usbnet_get_msglevel,
444 .set_msglevel = usbnet_set_msglevel,
445 .get_settings = usbnet_get_settings,
446 .set_settings = usbnet_set_settings,
447 .nway_reset = usbnet_nway_reset,
450 static int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_altsetting)
452 struct cdc_ncm_ctx *ctx;
453 struct usb_driver *driver;
459 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
463 hrtimer_init(&ctx->tx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
464 ctx->tx_timer.function = &cdc_ncm_tx_timer_cb;
465 ctx->bh.data = (unsigned long)ctx;
466 ctx->bh.func = cdc_ncm_txpath_bh;
467 atomic_set(&ctx->stop, 0);
468 spin_lock_init(&ctx->mtx);
469 ctx->netdev = dev->net;
471 /* store ctx pointer in device data field */
472 dev->data[0] = (unsigned long)ctx;
474 /* get some pointers */
475 driver = driver_of(intf);
476 buf = intf->cur_altsetting->extra;
477 len = intf->cur_altsetting->extralen;
479 ctx->udev = dev->udev;
482 /* parse through descriptors associated with control interface */
483 while ((len > 0) && (buf[0] > 2) && (buf[0] <= len)) {
485 if (buf[1] != USB_DT_CS_INTERFACE)
489 case USB_CDC_UNION_TYPE:
490 if (buf[0] < sizeof(*(ctx->union_desc)))
494 (const struct usb_cdc_union_desc *)buf;
496 ctx->control = usb_ifnum_to_if(dev->udev,
497 ctx->union_desc->bMasterInterface0);
498 ctx->data = usb_ifnum_to_if(dev->udev,
499 ctx->union_desc->bSlaveInterface0);
502 case USB_CDC_ETHERNET_TYPE:
503 if (buf[0] < sizeof(*(ctx->ether_desc)))
507 (const struct usb_cdc_ether_desc *)buf;
509 le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
511 if (dev->hard_mtu < CDC_NCM_MIN_DATAGRAM_SIZE)
512 dev->hard_mtu = CDC_NCM_MIN_DATAGRAM_SIZE;
513 else if (dev->hard_mtu > CDC_NCM_MAX_DATAGRAM_SIZE)
514 dev->hard_mtu = CDC_NCM_MAX_DATAGRAM_SIZE;
517 case USB_CDC_NCM_TYPE:
518 if (buf[0] < sizeof(*(ctx->func_desc)))
521 ctx->func_desc = (const struct usb_cdc_ncm_desc *)buf;
524 case USB_CDC_MBIM_TYPE:
525 if (buf[0] < sizeof(*(ctx->mbim_desc)))
528 ctx->mbim_desc = (const struct usb_cdc_mbim_desc *)buf;
535 /* advance to next descriptor */
541 /* check if we got everything */
542 if ((ctx->control == NULL) || (ctx->data == NULL) ||
543 ((!ctx->mbim_desc) && ((ctx->ether_desc == NULL) || (ctx->control != intf))))
546 /* claim interfaces, if any */
547 temp = usb_driver_claim_interface(driver, ctx->data, dev);
551 iface_no = ctx->data->cur_altsetting->desc.bInterfaceNumber;
553 /* reset data interface */
554 temp = usb_set_interface(dev->udev, iface_no, 0);
558 /* initialize data interface */
559 if (cdc_ncm_setup(ctx))
562 /* configure data interface */
563 temp = usb_set_interface(dev->udev, iface_no, data_altsetting);
567 cdc_ncm_find_endpoints(ctx, ctx->data);
568 cdc_ncm_find_endpoints(ctx, ctx->control);
570 if ((ctx->in_ep == NULL) || (ctx->out_ep == NULL) ||
571 (ctx->status_ep == NULL))
574 dev->net->ethtool_ops = &cdc_ncm_ethtool_ops;
576 usb_set_intfdata(ctx->data, dev);
577 usb_set_intfdata(ctx->control, dev);
578 usb_set_intfdata(ctx->intf, dev);
580 if (ctx->ether_desc) {
581 temp = usbnet_get_ethernet_addr(dev, ctx->ether_desc->iMACAddress);
584 dev_info(&dev->udev->dev, "MAC-Address: %pM\n", dev->net->dev_addr);
588 dev->in = usb_rcvbulkpipe(dev->udev,
589 ctx->in_ep->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
590 dev->out = usb_sndbulkpipe(dev->udev,
591 ctx->out_ep->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
592 dev->status = ctx->status_ep;
593 dev->rx_urb_size = ctx->rx_max;
595 ctx->tx_speed = ctx->rx_speed = 0;
599 usb_set_intfdata(ctx->control, NULL);
600 usb_set_intfdata(ctx->data, NULL);
601 usb_driver_release_interface(driver, ctx->data);
603 cdc_ncm_free((struct cdc_ncm_ctx *)dev->data[0]);
605 dev_info(&dev->udev->dev, "bind() failure\n");
609 static void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf)
611 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
612 struct usb_driver *driver = driver_of(intf);
615 return; /* no setup */
617 atomic_set(&ctx->stop, 1);
619 if (hrtimer_active(&ctx->tx_timer))
620 hrtimer_cancel(&ctx->tx_timer);
622 tasklet_kill(&ctx->bh);
624 /* disconnect master --> disconnect slave */
625 if (intf == ctx->control && ctx->data) {
626 usb_set_intfdata(ctx->data, NULL);
627 usb_driver_release_interface(driver, ctx->data);
630 } else if (intf == ctx->data && ctx->control) {
631 usb_set_intfdata(ctx->control, NULL);
632 usb_driver_release_interface(driver, ctx->control);
636 usb_set_intfdata(ctx->intf, NULL);
640 static int cdc_ncm_bind(struct usbnet *dev, struct usb_interface *intf)
644 /* NCM data altsetting is always 1 */
645 ret = cdc_ncm_bind_common(dev, intf, 1);
648 * We should get an event when network connection is "connected" or
649 * "disconnected". Set network connection in "disconnected" state
650 * (carrier is OFF) during attach, so the IP network stack does not
651 * start IPv6 negotiation and more.
653 netif_carrier_off(dev->net);
657 static void cdc_ncm_align_tail(struct sk_buff *skb, size_t modulus, size_t remainder, size_t max)
659 size_t align = ALIGN(skb->len, modulus) - skb->len + remainder;
661 if (skb->len + align > max)
662 align = max - skb->len;
663 if (align && skb_tailroom(skb) >= align)
664 memset(skb_put(skb, align), 0, align);
667 /* return a pointer to a valid struct usb_cdc_ncm_ndp16 of type sign, possibly
668 * allocating a new one within skb
670 static struct usb_cdc_ncm_ndp16 *cdc_ncm_ndp(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign, size_t reserve)
672 struct usb_cdc_ncm_ndp16 *ndp16 = NULL;
673 struct usb_cdc_ncm_nth16 *nth16 = (void *)skb->data;
674 size_t ndpoffset = le16_to_cpu(nth16->wNdpIndex);
676 /* follow the chain of NDPs, looking for a match */
678 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb->data + ndpoffset);
679 if (ndp16->dwSignature == sign)
681 ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
685 cdc_ncm_align_tail(skb, ctx->tx_ndp_modulus, 0, ctx->tx_max);
687 /* verify that there is room for the NDP and the datagram (reserve) */
688 if ((ctx->tx_max - skb->len - reserve) < CDC_NCM_NDP_SIZE)
693 ndp16->wNextNdpIndex = cpu_to_le16(skb->len);
695 nth16->wNdpIndex = cpu_to_le16(skb->len);
697 /* push a new empty NDP */
698 ndp16 = (struct usb_cdc_ncm_ndp16 *)memset(skb_put(skb, CDC_NCM_NDP_SIZE), 0, CDC_NCM_NDP_SIZE);
699 ndp16->dwSignature = sign;
700 ndp16->wLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_ndp16) + sizeof(struct usb_cdc_ncm_dpe16));
704 static struct sk_buff *
705 cdc_ncm_fill_tx_frame(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign)
707 struct usb_cdc_ncm_nth16 *nth16;
708 struct usb_cdc_ncm_ndp16 *ndp16;
709 struct sk_buff *skb_out;
710 u16 n = 0, index, ndplen;
713 /* if there is a remaining skb, it gets priority */
715 swap(skb, ctx->tx_rem_skb);
716 swap(sign, ctx->tx_rem_sign);
721 /* check if we are resuming an OUT skb */
722 skb_out = ctx->tx_curr_skb;
724 /* allocate a new OUT skb */
726 skb_out = alloc_skb((ctx->tx_max + 1), GFP_ATOMIC);
727 if (skb_out == NULL) {
729 dev_kfree_skb_any(skb);
730 ctx->netdev->stats.tx_dropped++;
734 /* fill out the initial 16-bit NTB header */
735 nth16 = (struct usb_cdc_ncm_nth16 *)memset(skb_put(skb_out, sizeof(struct usb_cdc_ncm_nth16)), 0, sizeof(struct usb_cdc_ncm_nth16));
736 nth16->dwSignature = cpu_to_le32(USB_CDC_NCM_NTH16_SIGN);
737 nth16->wHeaderLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_nth16));
738 nth16->wSequence = cpu_to_le16(ctx->tx_seq++);
740 /* count total number of frames in this NTB */
741 ctx->tx_curr_frame_num = 0;
744 for (n = ctx->tx_curr_frame_num; n < ctx->tx_max_datagrams; n++) {
745 /* send any remaining skb first */
747 skb = ctx->tx_rem_skb;
748 sign = ctx->tx_rem_sign;
749 ctx->tx_rem_skb = NULL;
751 /* check for end of skb */
756 /* get the appropriate NDP for this skb */
757 ndp16 = cdc_ncm_ndp(ctx, skb_out, sign, skb->len + ctx->tx_modulus + ctx->tx_remainder);
759 /* align beginning of next frame */
760 cdc_ncm_align_tail(skb_out, ctx->tx_modulus, ctx->tx_remainder, ctx->tx_max);
762 /* check if we had enough room left for both NDP and frame */
763 if (!ndp16 || skb_out->len + skb->len > ctx->tx_max) {
765 /* won't fit, MTU problem? */
766 dev_kfree_skb_any(skb);
768 ctx->netdev->stats.tx_dropped++;
770 /* no room for skb - store for later */
771 if (ctx->tx_rem_skb != NULL) {
772 dev_kfree_skb_any(ctx->tx_rem_skb);
773 ctx->netdev->stats.tx_dropped++;
775 ctx->tx_rem_skb = skb;
776 ctx->tx_rem_sign = sign;
783 /* calculate frame number withing this NDP */
784 ndplen = le16_to_cpu(ndp16->wLength);
785 index = (ndplen - sizeof(struct usb_cdc_ncm_ndp16)) / sizeof(struct usb_cdc_ncm_dpe16) - 1;
787 /* OK, add this skb */
788 ndp16->dpe16[index].wDatagramLength = cpu_to_le16(skb->len);
789 ndp16->dpe16[index].wDatagramIndex = cpu_to_le16(skb_out->len);
790 ndp16->wLength = cpu_to_le16(ndplen + sizeof(struct usb_cdc_ncm_dpe16));
791 memcpy(skb_put(skb_out, skb->len), skb->data, skb->len);
792 dev_kfree_skb_any(skb);
795 /* send now if this NDP is full */
796 if (index >= CDC_NCM_DPT_DATAGRAMS_MAX) {
802 /* free up any dangling skb */
804 dev_kfree_skb_any(skb);
806 ctx->netdev->stats.tx_dropped++;
809 ctx->tx_curr_frame_num = n;
812 /* wait for more frames */
814 ctx->tx_curr_skb = skb_out;
817 } else if ((n < ctx->tx_max_datagrams) && (ready2send == 0)) {
818 /* wait for more frames */
820 ctx->tx_curr_skb = skb_out;
821 /* set the pending count */
822 if (n < CDC_NCM_RESTART_TIMER_DATAGRAM_CNT)
823 ctx->tx_timer_pending = CDC_NCM_TIMER_PENDING_CNT;
828 /* variables will be reset at next call */
832 * If collected data size is less or equal CDC_NCM_MIN_TX_PKT bytes,
833 * we send buffers as it is. If we get more data, it would be more
834 * efficient for USB HS mobile device with DMA engine to receive a full
835 * size NTB, than canceling DMA transfer and receiving a short packet.
837 if (skb_out->len > CDC_NCM_MIN_TX_PKT)
838 /* final zero padding */
839 memset(skb_put(skb_out, ctx->tx_max - skb_out->len), 0, ctx->tx_max - skb_out->len);
841 /* do we need to prevent a ZLP? */
842 if (((skb_out->len % le16_to_cpu(ctx->out_ep->desc.wMaxPacketSize)) == 0) &&
843 (skb_out->len < le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize)) && skb_tailroom(skb_out))
844 *skb_put(skb_out, 1) = 0; /* force short packet */
846 /* set final frame length */
847 nth16 = (struct usb_cdc_ncm_nth16 *)skb_out->data;
848 nth16->wBlockLength = cpu_to_le16(skb_out->len);
851 ctx->tx_curr_skb = NULL;
852 ctx->netdev->stats.tx_packets += ctx->tx_curr_frame_num;
856 /* Start timer, if there is a remaining skb */
857 if (ctx->tx_curr_skb != NULL)
858 cdc_ncm_tx_timeout_start(ctx);
862 static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx)
864 /* start timer, if not already started */
865 if (!(hrtimer_active(&ctx->tx_timer) || atomic_read(&ctx->stop)))
866 hrtimer_start(&ctx->tx_timer,
867 ktime_set(0, CDC_NCM_TIMER_INTERVAL),
871 static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *timer)
873 struct cdc_ncm_ctx *ctx =
874 container_of(timer, struct cdc_ncm_ctx, tx_timer);
876 if (!atomic_read(&ctx->stop))
877 tasklet_schedule(&ctx->bh);
878 return HRTIMER_NORESTART;
881 static void cdc_ncm_txpath_bh(unsigned long param)
883 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)param;
885 spin_lock_bh(&ctx->mtx);
886 if (ctx->tx_timer_pending != 0) {
887 ctx->tx_timer_pending--;
888 cdc_ncm_tx_timeout_start(ctx);
889 spin_unlock_bh(&ctx->mtx);
890 } else if (ctx->netdev != NULL) {
891 spin_unlock_bh(&ctx->mtx);
892 netif_tx_lock_bh(ctx->netdev);
893 usbnet_start_xmit(NULL, ctx->netdev);
894 netif_tx_unlock_bh(ctx->netdev);
898 static struct sk_buff *
899 cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
901 struct sk_buff *skb_out;
902 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
905 * The Ethernet API we are using does not support transmitting
906 * multiple Ethernet frames in a single call. This driver will
907 * accumulate multiple Ethernet frames and send out a larger
908 * USB frame when the USB buffer is full or when a single jiffies
914 spin_lock_bh(&ctx->mtx);
915 skb_out = cdc_ncm_fill_tx_frame(ctx, skb, cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN));
916 spin_unlock_bh(&ctx->mtx);
921 dev_kfree_skb_any(skb);
926 /* verify NTB header and return offset of first NDP, or negative error */
927 static int cdc_ncm_rx_verify_nth16(struct cdc_ncm_ctx *ctx, struct sk_buff *skb_in)
929 struct usb_cdc_ncm_nth16 *nth16;
936 if (skb_in->len < (sizeof(struct usb_cdc_ncm_nth16) +
937 sizeof(struct usb_cdc_ncm_ndp16))) {
938 pr_debug("frame too short\n");
942 nth16 = (struct usb_cdc_ncm_nth16 *)skb_in->data;
944 if (le32_to_cpu(nth16->dwSignature) != USB_CDC_NCM_NTH16_SIGN) {
945 pr_debug("invalid NTH16 signature <%u>\n",
946 le32_to_cpu(nth16->dwSignature));
950 len = le16_to_cpu(nth16->wBlockLength);
951 if (len > ctx->rx_max) {
952 pr_debug("unsupported NTB block length %u/%u\n", len,
957 if ((ctx->rx_seq + 1) != le16_to_cpu(nth16->wSequence) &&
958 (ctx->rx_seq || le16_to_cpu(nth16->wSequence)) &&
959 !((ctx->rx_seq == 0xffff) && !le16_to_cpu(nth16->wSequence))) {
960 pr_debug("sequence number glitch prev=%d curr=%d\n",
961 ctx->rx_seq, le16_to_cpu(nth16->wSequence));
963 ctx->rx_seq = le16_to_cpu(nth16->wSequence);
965 ret = le16_to_cpu(nth16->wNdpIndex);
970 /* verify NDP header and return number of datagrams, or negative error */
971 static int cdc_ncm_rx_verify_ndp16(struct sk_buff *skb_in, int ndpoffset)
973 struct usb_cdc_ncm_ndp16 *ndp16;
976 if ((ndpoffset + sizeof(struct usb_cdc_ncm_ndp16)) > skb_in->len) {
977 pr_debug("invalid NDP offset <%u>\n", ndpoffset);
980 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
982 if (le16_to_cpu(ndp16->wLength) < USB_CDC_NCM_NDP16_LENGTH_MIN) {
983 pr_debug("invalid DPT16 length <%u>\n",
984 le32_to_cpu(ndp16->dwSignature));
988 ret = ((le16_to_cpu(ndp16->wLength) -
989 sizeof(struct usb_cdc_ncm_ndp16)) /
990 sizeof(struct usb_cdc_ncm_dpe16));
991 ret--; /* we process NDP entries except for the last one */
993 if ((sizeof(struct usb_cdc_ncm_ndp16) + ret * (sizeof(struct usb_cdc_ncm_dpe16))) >
995 pr_debug("Invalid nframes = %d\n", ret);
1003 static int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
1005 struct sk_buff *skb;
1006 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
1011 struct usb_cdc_ncm_ndp16 *ndp16;
1012 struct usb_cdc_ncm_dpe16 *dpe16;
1014 int loopcount = 50; /* arbitrary max preventing infinite loop */
1016 ndpoffset = cdc_ncm_rx_verify_nth16(ctx, skb_in);
1021 nframes = cdc_ncm_rx_verify_ndp16(skb_in, ndpoffset);
1025 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
1027 if (le32_to_cpu(ndp16->dwSignature) != USB_CDC_NCM_NDP16_NOCRC_SIGN) {
1028 pr_debug("invalid DPT16 signature <%u>\n",
1029 le32_to_cpu(ndp16->dwSignature));
1032 dpe16 = ndp16->dpe16;
1034 for (x = 0; x < nframes; x++, dpe16++) {
1035 offset = le16_to_cpu(dpe16->wDatagramIndex);
1036 len = le16_to_cpu(dpe16->wDatagramLength);
1040 * All entries after first NULL entry are to be ignored
1042 if ((offset == 0) || (len == 0)) {
1044 goto err_ndp; /* empty NTB */
1048 /* sanity checking */
1049 if (((offset + len) > skb_in->len) ||
1050 (len > ctx->rx_max) || (len < ETH_HLEN)) {
1051 pr_debug("invalid frame detected (ignored)"
1052 "offset[%u]=%u, length=%u, skb=%p\n",
1053 x, offset, len, skb_in);
1059 skb = skb_clone(skb_in, GFP_ATOMIC);
1063 skb->data = ((u8 *)skb_in->data) + offset;
1064 skb_set_tail_pointer(skb, len);
1065 usbnet_skb_return(dev, skb);
1069 /* are there more NDPs to process? */
1070 ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
1071 if (ndpoffset && loopcount--)
1080 cdc_ncm_speed_change(struct cdc_ncm_ctx *ctx,
1081 struct usb_cdc_speed_change *data)
1083 uint32_t rx_speed = le32_to_cpu(data->DLBitRRate);
1084 uint32_t tx_speed = le32_to_cpu(data->ULBitRate);
1087 * Currently the USB-NET API does not support reporting the actual
1088 * device speed. Do print it instead.
1090 if ((tx_speed != ctx->tx_speed) || (rx_speed != ctx->rx_speed)) {
1091 ctx->tx_speed = tx_speed;
1092 ctx->rx_speed = rx_speed;
1094 if ((tx_speed > 1000000) && (rx_speed > 1000000)) {
1095 printk(KERN_INFO KBUILD_MODNAME
1096 ": %s: %u mbit/s downlink "
1097 "%u mbit/s uplink\n",
1099 (unsigned int)(rx_speed / 1000000U),
1100 (unsigned int)(tx_speed / 1000000U));
1102 printk(KERN_INFO KBUILD_MODNAME
1103 ": %s: %u kbit/s downlink "
1104 "%u kbit/s uplink\n",
1106 (unsigned int)(rx_speed / 1000U),
1107 (unsigned int)(tx_speed / 1000U));
1112 static void cdc_ncm_status(struct usbnet *dev, struct urb *urb)
1114 struct cdc_ncm_ctx *ctx;
1115 struct usb_cdc_notification *event;
1117 ctx = (struct cdc_ncm_ctx *)dev->data[0];
1119 if (urb->actual_length < sizeof(*event))
1122 /* test for split data in 8-byte chunks */
1123 if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
1124 cdc_ncm_speed_change(ctx,
1125 (struct usb_cdc_speed_change *)urb->transfer_buffer);
1129 event = urb->transfer_buffer;
1131 switch (event->bNotificationType) {
1132 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
1134 * According to the CDC NCM specification ch.7.1
1135 * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be
1136 * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE.
1138 ctx->connected = event->wValue;
1140 printk(KERN_INFO KBUILD_MODNAME ": %s: network connection:"
1142 ctx->netdev->name, ctx->connected ? "" : "dis");
1145 netif_carrier_on(dev->net);
1147 netif_carrier_off(dev->net);
1148 ctx->tx_speed = ctx->rx_speed = 0;
1152 case USB_CDC_NOTIFY_SPEED_CHANGE:
1153 if (urb->actual_length < (sizeof(*event) +
1154 sizeof(struct usb_cdc_speed_change)))
1155 set_bit(EVENT_STS_SPLIT, &dev->flags);
1157 cdc_ncm_speed_change(ctx,
1158 (struct usb_cdc_speed_change *) &event[1]);
1162 dev_err(&dev->udev->dev, "NCM: unexpected "
1163 "notification 0x%02x!\n", event->bNotificationType);
1168 static int cdc_ncm_check_connect(struct usbnet *dev)
1170 struct cdc_ncm_ctx *ctx;
1172 ctx = (struct cdc_ncm_ctx *)dev->data[0];
1174 return 1; /* disconnected */
1176 return !ctx->connected;
1180 cdc_ncm_probe(struct usb_interface *udev, const struct usb_device_id *prod)
1182 return usbnet_probe(udev, prod);
1185 static void cdc_ncm_disconnect(struct usb_interface *intf)
1187 struct usbnet *dev = usb_get_intfdata(intf);
1190 return; /* already disconnected */
1192 usbnet_disconnect(intf);
1195 static int cdc_ncm_manage_power(struct usbnet *dev, int status)
1197 dev->intf->needs_remote_wakeup = status;
1201 static const struct driver_info cdc_ncm_info = {
1202 .description = "CDC NCM",
1203 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET,
1204 .bind = cdc_ncm_bind,
1205 .unbind = cdc_ncm_unbind,
1206 .check_connect = cdc_ncm_check_connect,
1207 .manage_power = cdc_ncm_manage_power,
1208 .status = cdc_ncm_status,
1209 .rx_fixup = cdc_ncm_rx_fixup,
1210 .tx_fixup = cdc_ncm_tx_fixup,
1213 /* Same as cdc_ncm_info, but with FLAG_WWAN */
1214 static const struct driver_info wwan_info = {
1215 .description = "Mobile Broadband Network Device",
1216 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
1218 .bind = cdc_ncm_bind,
1219 .unbind = cdc_ncm_unbind,
1220 .check_connect = cdc_ncm_check_connect,
1221 .manage_power = cdc_ncm_manage_power,
1222 .status = cdc_ncm_status,
1223 .rx_fixup = cdc_ncm_rx_fixup,
1224 .tx_fixup = cdc_ncm_tx_fixup,
1227 static const struct usb_device_id cdc_devs[] = {
1228 /* Ericsson MBM devices like F5521gw */
1229 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1230 | USB_DEVICE_ID_MATCH_VENDOR,
1232 .bInterfaceClass = USB_CLASS_COMM,
1233 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1234 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1235 .driver_info = (unsigned long) &wwan_info,
1238 /* Dell branded MBM devices like DW5550 */
1239 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1240 | USB_DEVICE_ID_MATCH_VENDOR,
1242 .bInterfaceClass = USB_CLASS_COMM,
1243 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1244 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1245 .driver_info = (unsigned long) &wwan_info,
1248 /* Toshiba branded MBM devices */
1249 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1250 | USB_DEVICE_ID_MATCH_VENDOR,
1252 .bInterfaceClass = USB_CLASS_COMM,
1253 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1254 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1255 .driver_info = (unsigned long) &wwan_info,
1258 /* Generic CDC-NCM devices */
1259 { USB_INTERFACE_INFO(USB_CLASS_COMM,
1260 USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
1261 .driver_info = (unsigned long)&cdc_ncm_info,
1266 MODULE_DEVICE_TABLE(usb, cdc_devs);
1268 static struct usb_driver cdc_ncm_driver = {
1270 .id_table = cdc_devs,
1271 .probe = cdc_ncm_probe,
1272 .disconnect = cdc_ncm_disconnect,
1273 .suspend = usbnet_suspend,
1274 .resume = usbnet_resume,
1275 .reset_resume = usbnet_resume,
1276 .supports_autosuspend = 1,
1277 .disable_hub_initiated_lpm = 1,
1280 module_usb_driver(cdc_ncm_driver);
1282 MODULE_AUTHOR("Hans Petter Selasky");
1283 MODULE_DESCRIPTION("USB CDC NCM host driver");
1284 MODULE_LICENSE("Dual BSD/GPL");