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 #define CDC_NCM_MIN_TX_PKT 512 /* bytes */
69 /* Default value for MaxDatagramSize */
70 #define CDC_NCM_MAX_DATAGRAM_SIZE 8192 /* bytes */
73 * Maximum amount of datagrams in NCM Datagram Pointer Table, not counting
74 * the last NULL entry.
76 #define CDC_NCM_DPT_DATAGRAMS_MAX 40
78 /* Restart the timer, if amount of datagrams is less than given value */
79 #define CDC_NCM_RESTART_TIMER_DATAGRAM_CNT 3
80 #define CDC_NCM_TIMER_PENDING_CNT 2
81 #define CDC_NCM_TIMER_INTERVAL (400UL * NSEC_PER_USEC)
83 /* The following macro defines the minimum header space */
84 #define CDC_NCM_MIN_HDR_SIZE \
85 (sizeof(struct usb_cdc_ncm_nth16) + sizeof(struct usb_cdc_ncm_ndp16) + \
86 (CDC_NCM_DPT_DATAGRAMS_MAX + 1) * sizeof(struct usb_cdc_ncm_dpe16))
89 struct usb_cdc_ncm_nth16 nth16;
90 struct usb_cdc_ncm_ndp16 ndp16;
91 struct usb_cdc_ncm_dpe16 dpe16[CDC_NCM_DPT_DATAGRAMS_MAX + 1];
95 struct cdc_ncm_data tx_ncm;
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_header_desc *header_desc;
102 const struct usb_cdc_union_desc *union_desc;
103 const struct usb_cdc_ether_desc *ether_desc;
105 struct net_device *netdev;
106 struct usb_device *udev;
107 struct usb_host_endpoint *in_ep;
108 struct usb_host_endpoint *out_ep;
109 struct usb_host_endpoint *status_ep;
110 struct usb_interface *intf;
111 struct usb_interface *control;
112 struct usb_interface *data;
114 struct sk_buff *tx_curr_skb;
115 struct sk_buff *tx_rem_skb;
120 u32 tx_timer_pending;
122 u32 tx_curr_last_offset;
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)
161 u16 ntb_fmt_supported;
163 iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber;
165 err = usb_control_msg(ctx->udev,
166 usb_rcvctrlpipe(ctx->udev, 0),
167 USB_CDC_GET_NTB_PARAMETERS,
168 USB_TYPE_CLASS | USB_DIR_IN
169 | USB_RECIP_INTERFACE,
170 0, iface_no, &ctx->ncm_parm,
171 sizeof(ctx->ncm_parm), 10000);
173 pr_debug("failed GET_NTB_PARAMETERS\n");
177 /* read correct set of parameters according to device mode */
178 ctx->rx_max = le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize);
179 ctx->tx_max = le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize);
180 ctx->tx_remainder = le16_to_cpu(ctx->ncm_parm.wNdpOutPayloadRemainder);
181 ctx->tx_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutDivisor);
182 ctx->tx_ndp_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutAlignment);
183 /* devices prior to NCM Errata shall set this field to zero */
184 ctx->tx_max_datagrams = le16_to_cpu(ctx->ncm_parm.wNtbOutMaxDatagrams);
185 ntb_fmt_supported = le16_to_cpu(ctx->ncm_parm.bmNtbFormatsSupported);
187 if (ctx->func_desc != NULL)
188 flags = ctx->func_desc->bmNetworkCapabilities;
192 pr_debug("dwNtbInMaxSize=%u dwNtbOutMaxSize=%u "
193 "wNdpOutPayloadRemainder=%u wNdpOutDivisor=%u "
194 "wNdpOutAlignment=%u wNtbOutMaxDatagrams=%u flags=0x%x\n",
195 ctx->rx_max, ctx->tx_max, ctx->tx_remainder, ctx->tx_modulus,
196 ctx->tx_ndp_modulus, ctx->tx_max_datagrams, flags);
198 /* max count of tx datagrams */
199 if ((ctx->tx_max_datagrams == 0) ||
200 (ctx->tx_max_datagrams > CDC_NCM_DPT_DATAGRAMS_MAX))
201 ctx->tx_max_datagrams = CDC_NCM_DPT_DATAGRAMS_MAX;
203 /* verify maximum size of received NTB in bytes */
204 if (ctx->rx_max < USB_CDC_NCM_NTB_MIN_IN_SIZE) {
205 pr_debug("Using min receive length=%d\n",
206 USB_CDC_NCM_NTB_MIN_IN_SIZE);
207 ctx->rx_max = USB_CDC_NCM_NTB_MIN_IN_SIZE;
210 if (ctx->rx_max > CDC_NCM_NTB_MAX_SIZE_RX) {
211 pr_debug("Using default maximum receive length=%d\n",
212 CDC_NCM_NTB_MAX_SIZE_RX);
213 ctx->rx_max = CDC_NCM_NTB_MAX_SIZE_RX;
216 /* inform device about NTB input size changes */
217 if (ctx->rx_max != le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize)) {
218 __le32 *dwNtbInMaxSize;
220 dwNtbInMaxSize = kzalloc(sizeof(*dwNtbInMaxSize), GFP_KERNEL);
221 if (!dwNtbInMaxSize) {
225 *dwNtbInMaxSize = cpu_to_le32(ctx->rx_max);
226 err = usb_control_msg(ctx->udev,
227 usb_sndctrlpipe(ctx->udev, 0),
228 USB_CDC_SET_NTB_INPUT_SIZE,
229 USB_TYPE_CLASS | USB_DIR_OUT
230 | USB_RECIP_INTERFACE,
231 0, iface_no, dwNtbInMaxSize, 4, 1000);
232 kfree(dwNtbInMaxSize);
235 pr_debug("Setting NTB Input Size failed\n");
238 /* verify maximum size of transmitted NTB in bytes */
240 (CDC_NCM_MIN_HDR_SIZE + CDC_NCM_MIN_DATAGRAM_SIZE)) ||
241 (ctx->tx_max > CDC_NCM_NTB_MAX_SIZE_TX)) {
242 pr_debug("Using default maximum transmit length=%d\n",
243 CDC_NCM_NTB_MAX_SIZE_TX);
244 ctx->tx_max = CDC_NCM_NTB_MAX_SIZE_TX;
248 * verify that the structure alignment is:
250 * - not greater than the maximum transmit length
251 * - not less than four bytes
253 val = ctx->tx_ndp_modulus;
255 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
256 (val != ((-val) & val)) || (val >= ctx->tx_max)) {
257 pr_debug("Using default alignment: 4 bytes\n");
258 ctx->tx_ndp_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
262 * verify that the payload alignment is:
264 * - not greater than the maximum transmit length
265 * - not less than four bytes
267 val = ctx->tx_modulus;
269 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
270 (val != ((-val) & val)) || (val >= ctx->tx_max)) {
271 pr_debug("Using default transmit modulus: 4 bytes\n");
272 ctx->tx_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
275 /* verify the payload remainder */
276 if (ctx->tx_remainder >= ctx->tx_modulus) {
277 pr_debug("Using default transmit remainder: 0 bytes\n");
278 ctx->tx_remainder = 0;
281 /* adjust TX-remainder according to NCM specification. */
282 ctx->tx_remainder = ((ctx->tx_remainder - ETH_HLEN) &
283 (ctx->tx_modulus - 1));
285 /* additional configuration */
288 if (flags & USB_CDC_NCM_NCAP_CRC_MODE) {
289 err = usb_control_msg(ctx->udev, usb_sndctrlpipe(ctx->udev, 0),
290 USB_CDC_SET_CRC_MODE,
291 USB_TYPE_CLASS | USB_DIR_OUT
292 | USB_RECIP_INTERFACE,
293 USB_CDC_NCM_CRC_NOT_APPENDED,
294 iface_no, NULL, 0, 1000);
296 pr_debug("Setting CRC mode off failed\n");
299 /* set NTB format, if both formats are supported */
300 if (ntb_fmt_supported & USB_CDC_NCM_NTH32_SIGN) {
301 err = usb_control_msg(ctx->udev, usb_sndctrlpipe(ctx->udev, 0),
302 USB_CDC_SET_NTB_FORMAT, USB_TYPE_CLASS
303 | USB_DIR_OUT | USB_RECIP_INTERFACE,
304 USB_CDC_NCM_NTB16_FORMAT,
305 iface_no, NULL, 0, 1000);
307 pr_debug("Setting NTB format to 16-bit failed\n");
310 ctx->max_datagram_size = CDC_NCM_MIN_DATAGRAM_SIZE;
312 /* set Max Datagram Size (MTU) */
313 if (flags & USB_CDC_NCM_NCAP_MAX_DATAGRAM_SIZE) {
314 __le16 *max_datagram_size;
315 u16 eth_max_sz = le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
317 max_datagram_size = kzalloc(sizeof(*max_datagram_size),
319 if (!max_datagram_size) {
324 err = usb_control_msg(ctx->udev, usb_rcvctrlpipe(ctx->udev, 0),
325 USB_CDC_GET_MAX_DATAGRAM_SIZE,
326 USB_TYPE_CLASS | USB_DIR_IN
327 | USB_RECIP_INTERFACE,
328 0, iface_no, max_datagram_size,
331 pr_debug("GET_MAX_DATAGRAM_SIZE failed, use size=%u\n",
332 CDC_NCM_MIN_DATAGRAM_SIZE);
334 ctx->max_datagram_size =
335 le16_to_cpu(*max_datagram_size);
336 /* Check Eth descriptor value */
337 if (ctx->max_datagram_size > eth_max_sz)
338 ctx->max_datagram_size = eth_max_sz;
340 if (ctx->max_datagram_size > CDC_NCM_MAX_DATAGRAM_SIZE)
341 ctx->max_datagram_size =
342 CDC_NCM_MAX_DATAGRAM_SIZE;
344 if (ctx->max_datagram_size < CDC_NCM_MIN_DATAGRAM_SIZE)
345 ctx->max_datagram_size =
346 CDC_NCM_MIN_DATAGRAM_SIZE;
348 /* if value changed, update device */
349 if (ctx->max_datagram_size !=
350 le16_to_cpu(*max_datagram_size)) {
351 err = usb_control_msg(ctx->udev,
352 usb_sndctrlpipe(ctx->udev, 0),
353 USB_CDC_SET_MAX_DATAGRAM_SIZE,
354 USB_TYPE_CLASS | USB_DIR_OUT
355 | USB_RECIP_INTERFACE,
357 iface_no, max_datagram_size,
360 pr_debug("SET_MAX_DGRAM_SIZE failed\n");
363 kfree(max_datagram_size);
367 if (ctx->netdev->mtu != (ctx->max_datagram_size - ETH_HLEN))
368 ctx->netdev->mtu = ctx->max_datagram_size - ETH_HLEN;
374 cdc_ncm_find_endpoints(struct cdc_ncm_ctx *ctx, struct usb_interface *intf)
376 struct usb_host_endpoint *e;
379 for (ep = 0; ep < intf->cur_altsetting->desc.bNumEndpoints; ep++) {
381 e = intf->cur_altsetting->endpoint + ep;
382 switch (e->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
383 case USB_ENDPOINT_XFER_INT:
384 if (usb_endpoint_dir_in(&e->desc)) {
385 if (ctx->status_ep == NULL)
390 case USB_ENDPOINT_XFER_BULK:
391 if (usb_endpoint_dir_in(&e->desc)) {
392 if (ctx->in_ep == NULL)
395 if (ctx->out_ep == NULL)
406 static void cdc_ncm_free(struct cdc_ncm_ctx *ctx)
411 if (ctx->tx_rem_skb != NULL) {
412 dev_kfree_skb_any(ctx->tx_rem_skb);
413 ctx->tx_rem_skb = NULL;
416 if (ctx->tx_curr_skb != NULL) {
417 dev_kfree_skb_any(ctx->tx_curr_skb);
418 ctx->tx_curr_skb = NULL;
424 static const struct ethtool_ops cdc_ncm_ethtool_ops = {
425 .get_drvinfo = cdc_ncm_get_drvinfo,
426 .get_link = usbnet_get_link,
427 .get_msglevel = usbnet_get_msglevel,
428 .set_msglevel = usbnet_set_msglevel,
429 .get_settings = usbnet_get_settings,
430 .set_settings = usbnet_set_settings,
431 .nway_reset = usbnet_nway_reset,
434 static int cdc_ncm_bind(struct usbnet *dev, struct usb_interface *intf)
436 struct cdc_ncm_ctx *ctx;
437 struct usb_driver *driver;
443 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
447 hrtimer_init(&ctx->tx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
448 ctx->tx_timer.function = &cdc_ncm_tx_timer_cb;
449 ctx->bh.data = (unsigned long)ctx;
450 ctx->bh.func = cdc_ncm_txpath_bh;
451 atomic_set(&ctx->stop, 0);
452 spin_lock_init(&ctx->mtx);
453 ctx->netdev = dev->net;
455 /* store ctx pointer in device data field */
456 dev->data[0] = (unsigned long)ctx;
458 /* get some pointers */
459 driver = driver_of(intf);
460 buf = intf->cur_altsetting->extra;
461 len = intf->cur_altsetting->extralen;
463 ctx->udev = dev->udev;
466 /* parse through descriptors associated with control interface */
467 while ((len > 0) && (buf[0] > 2) && (buf[0] <= len)) {
469 if (buf[1] != USB_DT_CS_INTERFACE)
473 case USB_CDC_UNION_TYPE:
474 if (buf[0] < sizeof(*(ctx->union_desc)))
478 (const struct usb_cdc_union_desc *)buf;
480 ctx->control = usb_ifnum_to_if(dev->udev,
481 ctx->union_desc->bMasterInterface0);
482 ctx->data = usb_ifnum_to_if(dev->udev,
483 ctx->union_desc->bSlaveInterface0);
486 case USB_CDC_ETHERNET_TYPE:
487 if (buf[0] < sizeof(*(ctx->ether_desc)))
491 (const struct usb_cdc_ether_desc *)buf;
493 le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
495 if (dev->hard_mtu < CDC_NCM_MIN_DATAGRAM_SIZE)
496 dev->hard_mtu = CDC_NCM_MIN_DATAGRAM_SIZE;
497 else if (dev->hard_mtu > CDC_NCM_MAX_DATAGRAM_SIZE)
498 dev->hard_mtu = CDC_NCM_MAX_DATAGRAM_SIZE;
501 case USB_CDC_NCM_TYPE:
502 if (buf[0] < sizeof(*(ctx->func_desc)))
505 ctx->func_desc = (const struct usb_cdc_ncm_desc *)buf;
512 /* advance to next descriptor */
518 /* check if we got everything */
519 if ((ctx->control == NULL) || (ctx->data == NULL) ||
520 (ctx->ether_desc == NULL) || (ctx->control != intf))
523 /* claim interfaces, if any */
524 temp = usb_driver_claim_interface(driver, ctx->data, dev);
528 iface_no = ctx->data->cur_altsetting->desc.bInterfaceNumber;
530 /* reset data interface */
531 temp = usb_set_interface(dev->udev, iface_no, 0);
535 /* initialize data interface */
536 if (cdc_ncm_setup(ctx))
539 /* configure data interface */
540 temp = usb_set_interface(dev->udev, iface_no, 1);
544 cdc_ncm_find_endpoints(ctx, ctx->data);
545 cdc_ncm_find_endpoints(ctx, ctx->control);
547 if ((ctx->in_ep == NULL) || (ctx->out_ep == NULL) ||
548 (ctx->status_ep == NULL))
551 dev->net->ethtool_ops = &cdc_ncm_ethtool_ops;
553 usb_set_intfdata(ctx->data, dev);
554 usb_set_intfdata(ctx->control, dev);
555 usb_set_intfdata(ctx->intf, dev);
557 temp = usbnet_get_ethernet_addr(dev, ctx->ether_desc->iMACAddress);
561 dev_info(&dev->udev->dev, "MAC-Address: %pM\n", dev->net->dev_addr);
563 dev->in = usb_rcvbulkpipe(dev->udev,
564 ctx->in_ep->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
565 dev->out = usb_sndbulkpipe(dev->udev,
566 ctx->out_ep->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
567 dev->status = ctx->status_ep;
568 dev->rx_urb_size = ctx->rx_max;
571 * We should get an event when network connection is "connected" or
572 * "disconnected". Set network connection in "disconnected" state
573 * (carrier is OFF) during attach, so the IP network stack does not
574 * start IPv6 negotiation and more.
576 netif_carrier_off(dev->net);
577 ctx->tx_speed = ctx->rx_speed = 0;
581 usb_set_intfdata(ctx->control, NULL);
582 usb_set_intfdata(ctx->data, NULL);
583 usb_driver_release_interface(driver, ctx->data);
585 cdc_ncm_free((struct cdc_ncm_ctx *)dev->data[0]);
587 dev_info(&dev->udev->dev, "bind() failure\n");
591 static void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf)
593 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
594 struct usb_driver *driver = driver_of(intf);
597 return; /* no setup */
599 atomic_set(&ctx->stop, 1);
601 if (hrtimer_active(&ctx->tx_timer))
602 hrtimer_cancel(&ctx->tx_timer);
604 tasklet_kill(&ctx->bh);
606 /* disconnect master --> disconnect slave */
607 if (intf == ctx->control && ctx->data) {
608 usb_set_intfdata(ctx->data, NULL);
609 usb_driver_release_interface(driver, ctx->data);
612 } else if (intf == ctx->data && ctx->control) {
613 usb_set_intfdata(ctx->control, NULL);
614 usb_driver_release_interface(driver, ctx->control);
618 usb_set_intfdata(ctx->intf, NULL);
622 static void cdc_ncm_zero_fill(u8 *ptr, u32 first, u32 end, u32 max)
630 memset(ptr + first, 0, end - first);
633 static struct sk_buff *
634 cdc_ncm_fill_tx_frame(struct cdc_ncm_ctx *ctx, struct sk_buff *skb)
636 struct sk_buff *skb_out;
643 /* if there is a remaining skb, it gets priority */
645 swap(skb, ctx->tx_rem_skb);
657 /* check if we are resuming an OUT skb */
658 if (ctx->tx_curr_skb != NULL) {
660 skb_out = ctx->tx_curr_skb;
661 offset = ctx->tx_curr_offset;
662 last_offset = ctx->tx_curr_last_offset;
663 n = ctx->tx_curr_frame_num;
666 /* reset variables */
667 skb_out = alloc_skb((ctx->tx_max + 1), GFP_ATOMIC);
668 if (skb_out == NULL) {
670 dev_kfree_skb_any(skb);
671 ctx->netdev->stats.tx_dropped++;
676 /* make room for NTH and NDP */
677 offset = ALIGN(sizeof(struct usb_cdc_ncm_nth16),
678 ctx->tx_ndp_modulus) +
679 sizeof(struct usb_cdc_ncm_ndp16) +
680 (ctx->tx_max_datagrams + 1) *
681 sizeof(struct usb_cdc_ncm_dpe16);
683 /* store last valid offset before alignment */
684 last_offset = offset;
685 /* align first Datagram offset correctly */
686 offset = ALIGN(offset, ctx->tx_modulus) + ctx->tx_remainder;
687 /* zero buffer till the first IP datagram */
688 cdc_ncm_zero_fill(skb_out->data, 0, offset, offset);
690 ctx->tx_curr_frame_num = 0;
693 for (; n < ctx->tx_max_datagrams; n++) {
694 /* check if end of transmit buffer is reached */
695 if (offset >= ctx->tx_max) {
699 /* compute maximum buffer size */
700 rem = ctx->tx_max - offset;
703 skb = ctx->tx_rem_skb;
704 ctx->tx_rem_skb = NULL;
706 /* check for end of skb */
711 if (skb->len > rem) {
713 /* won't fit, MTU problem? */
714 dev_kfree_skb_any(skb);
716 ctx->netdev->stats.tx_dropped++;
718 /* no room for skb - store for later */
719 if (ctx->tx_rem_skb != NULL) {
720 dev_kfree_skb_any(ctx->tx_rem_skb);
721 ctx->netdev->stats.tx_dropped++;
723 ctx->tx_rem_skb = skb;
730 memcpy(((u8 *)skb_out->data) + offset, skb->data, skb->len);
732 ctx->tx_ncm.dpe16[n].wDatagramLength = cpu_to_le16(skb->len);
733 ctx->tx_ncm.dpe16[n].wDatagramIndex = cpu_to_le16(offset);
738 /* store last valid offset before alignment */
739 last_offset = offset;
741 /* align offset correctly */
742 offset = ALIGN(offset, ctx->tx_modulus) + ctx->tx_remainder;
745 cdc_ncm_zero_fill(skb_out->data, last_offset, offset,
747 dev_kfree_skb_any(skb);
751 /* free up any dangling skb */
753 dev_kfree_skb_any(skb);
755 ctx->netdev->stats.tx_dropped++;
758 ctx->tx_curr_frame_num = n;
761 /* wait for more frames */
763 ctx->tx_curr_skb = skb_out;
764 ctx->tx_curr_offset = offset;
765 ctx->tx_curr_last_offset = last_offset;
768 } else if ((n < ctx->tx_max_datagrams) && (ready2send == 0)) {
769 /* wait for more frames */
771 ctx->tx_curr_skb = skb_out;
772 ctx->tx_curr_offset = offset;
773 ctx->tx_curr_last_offset = last_offset;
774 /* set the pending count */
775 if (n < CDC_NCM_RESTART_TIMER_DATAGRAM_CNT)
776 ctx->tx_timer_pending = CDC_NCM_TIMER_PENDING_CNT;
781 /* variables will be reset at next call */
784 /* check for overflow */
785 if (last_offset > ctx->tx_max)
786 last_offset = ctx->tx_max;
789 offset = last_offset;
792 * If collected data size is less or equal CDC_NCM_MIN_TX_PKT bytes,
793 * we send buffers as it is. If we get more data, it would be more
794 * efficient for USB HS mobile device with DMA engine to receive a full
795 * size NTB, than canceling DMA transfer and receiving a short packet.
797 if (offset > CDC_NCM_MIN_TX_PKT)
798 offset = ctx->tx_max;
800 /* final zero padding */
801 cdc_ncm_zero_fill(skb_out->data, last_offset, offset, ctx->tx_max);
803 /* store last offset */
804 last_offset = offset;
806 if (((last_offset < ctx->tx_max) && ((last_offset %
807 le16_to_cpu(ctx->out_ep->desc.wMaxPacketSize)) == 0)) ||
808 (((last_offset == ctx->tx_max) && ((ctx->tx_max %
809 le16_to_cpu(ctx->out_ep->desc.wMaxPacketSize)) == 0)) &&
810 (ctx->tx_max < le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize)))) {
811 /* force short packet */
812 *(((u8 *)skb_out->data) + last_offset) = 0;
816 /* zero the rest of the DPEs plus the last NULL entry */
817 for (; n <= CDC_NCM_DPT_DATAGRAMS_MAX; n++) {
818 ctx->tx_ncm.dpe16[n].wDatagramLength = 0;
819 ctx->tx_ncm.dpe16[n].wDatagramIndex = 0;
822 /* fill out 16-bit NTB header */
823 ctx->tx_ncm.nth16.dwSignature = cpu_to_le32(USB_CDC_NCM_NTH16_SIGN);
824 ctx->tx_ncm.nth16.wHeaderLength =
825 cpu_to_le16(sizeof(ctx->tx_ncm.nth16));
826 ctx->tx_ncm.nth16.wSequence = cpu_to_le16(ctx->tx_seq);
827 ctx->tx_ncm.nth16.wBlockLength = cpu_to_le16(last_offset);
828 index = ALIGN(sizeof(struct usb_cdc_ncm_nth16), ctx->tx_ndp_modulus);
829 ctx->tx_ncm.nth16.wNdpIndex = cpu_to_le16(index);
831 memcpy(skb_out->data, &(ctx->tx_ncm.nth16), sizeof(ctx->tx_ncm.nth16));
834 /* fill out 16-bit NDP table */
835 ctx->tx_ncm.ndp16.dwSignature =
836 cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN);
837 rem = sizeof(ctx->tx_ncm.ndp16) + ((ctx->tx_curr_frame_num + 1) *
838 sizeof(struct usb_cdc_ncm_dpe16));
839 ctx->tx_ncm.ndp16.wLength = cpu_to_le16(rem);
840 ctx->tx_ncm.ndp16.wNextNdpIndex = 0; /* reserved */
842 memcpy(((u8 *)skb_out->data) + index,
843 &(ctx->tx_ncm.ndp16),
844 sizeof(ctx->tx_ncm.ndp16));
846 memcpy(((u8 *)skb_out->data) + index + sizeof(ctx->tx_ncm.ndp16),
847 &(ctx->tx_ncm.dpe16),
848 (ctx->tx_curr_frame_num + 1) *
849 sizeof(struct usb_cdc_ncm_dpe16));
851 /* set frame length */
852 skb_put(skb_out, last_offset);
855 ctx->tx_curr_skb = NULL;
856 ctx->netdev->stats.tx_packets += ctx->tx_curr_frame_num;
860 /* Start timer, if there is a remaining skb */
861 if (ctx->tx_curr_skb != NULL)
862 cdc_ncm_tx_timeout_start(ctx);
866 static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx)
868 /* start timer, if not already started */
869 if (!(hrtimer_active(&ctx->tx_timer) || atomic_read(&ctx->stop)))
870 hrtimer_start(&ctx->tx_timer,
871 ktime_set(0, CDC_NCM_TIMER_INTERVAL),
875 static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *timer)
877 struct cdc_ncm_ctx *ctx =
878 container_of(timer, struct cdc_ncm_ctx, tx_timer);
880 if (!atomic_read(&ctx->stop))
881 tasklet_schedule(&ctx->bh);
882 return HRTIMER_NORESTART;
885 static void cdc_ncm_txpath_bh(unsigned long param)
887 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)param;
889 spin_lock_bh(&ctx->mtx);
890 if (ctx->tx_timer_pending != 0) {
891 ctx->tx_timer_pending--;
892 cdc_ncm_tx_timeout_start(ctx);
893 spin_unlock_bh(&ctx->mtx);
894 } else if (ctx->netdev != NULL) {
895 spin_unlock_bh(&ctx->mtx);
896 netif_tx_lock_bh(ctx->netdev);
897 usbnet_start_xmit(NULL, ctx->netdev);
898 netif_tx_unlock_bh(ctx->netdev);
902 static struct sk_buff *
903 cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
905 struct sk_buff *skb_out;
906 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
909 * The Ethernet API we are using does not support transmitting
910 * multiple Ethernet frames in a single call. This driver will
911 * accumulate multiple Ethernet frames and send out a larger
912 * USB frame when the USB buffer is full or when a single jiffies
918 spin_lock_bh(&ctx->mtx);
919 skb_out = cdc_ncm_fill_tx_frame(ctx, skb);
920 spin_unlock_bh(&ctx->mtx);
925 dev_kfree_skb_any(skb);
930 static int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
933 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
938 struct usb_cdc_ncm_nth16 *nth16;
939 struct usb_cdc_ncm_ndp16 *ndp16;
940 struct usb_cdc_ncm_dpe16 *dpe16;
945 if (skb_in->len < (sizeof(struct usb_cdc_ncm_nth16) +
946 sizeof(struct usb_cdc_ncm_ndp16))) {
947 pr_debug("frame too short\n");
951 nth16 = (struct usb_cdc_ncm_nth16 *)skb_in->data;
953 if (le32_to_cpu(nth16->dwSignature) != USB_CDC_NCM_NTH16_SIGN) {
954 pr_debug("invalid NTH16 signature <%u>\n",
955 le32_to_cpu(nth16->dwSignature));
959 len = le16_to_cpu(nth16->wBlockLength);
960 if (len > ctx->rx_max) {
961 pr_debug("unsupported NTB block length %u/%u\n", len,
966 if ((ctx->rx_seq + 1) != le16_to_cpu(nth16->wSequence) &&
967 (ctx->rx_seq || le16_to_cpu(nth16->wSequence)) &&
968 !((ctx->rx_seq == 0xffff) && !le16_to_cpu(nth16->wSequence))) {
969 pr_debug("sequence number glitch prev=%d curr=%d\n",
970 ctx->rx_seq, le16_to_cpu(nth16->wSequence));
972 ctx->rx_seq = le16_to_cpu(nth16->wSequence);
974 len = le16_to_cpu(nth16->wNdpIndex);
975 if ((len + sizeof(struct usb_cdc_ncm_ndp16)) > skb_in->len) {
976 pr_debug("invalid DPT16 index <%u>\n",
977 le16_to_cpu(nth16->wNdpIndex));
981 ndp16 = (struct usb_cdc_ncm_ndp16 *)(((u8 *)skb_in->data) + len);
983 if (le32_to_cpu(ndp16->dwSignature) != USB_CDC_NCM_NDP16_NOCRC_SIGN) {
984 pr_debug("invalid DPT16 signature <%u>\n",
985 le32_to_cpu(ndp16->dwSignature));
989 if (le16_to_cpu(ndp16->wLength) < USB_CDC_NCM_NDP16_LENGTH_MIN) {
990 pr_debug("invalid DPT16 length <%u>\n",
991 le32_to_cpu(ndp16->dwSignature));
995 nframes = ((le16_to_cpu(ndp16->wLength) -
996 sizeof(struct usb_cdc_ncm_ndp16)) /
997 sizeof(struct usb_cdc_ncm_dpe16));
998 nframes--; /* we process NDP entries except for the last one */
1000 len += sizeof(struct usb_cdc_ncm_ndp16);
1002 if ((len + nframes * (sizeof(struct usb_cdc_ncm_dpe16))) >
1004 pr_debug("Invalid nframes = %d\n", nframes);
1008 dpe16 = (struct usb_cdc_ncm_dpe16 *)(((u8 *)skb_in->data) + len);
1010 for (x = 0; x < nframes; x++, dpe16++) {
1011 offset = le16_to_cpu(dpe16->wDatagramIndex);
1012 len = le16_to_cpu(dpe16->wDatagramLength);
1016 * All entries after first NULL entry are to be ignored
1018 if ((offset == 0) || (len == 0)) {
1020 goto error; /* empty NTB */
1024 /* sanity checking */
1025 if (((offset + len) > skb_in->len) ||
1026 (len > ctx->rx_max) || (len < ETH_HLEN)) {
1027 pr_debug("invalid frame detected (ignored)"
1028 "offset[%u]=%u, length=%u, skb=%p\n",
1029 x, offset, len, skb_in);
1035 skb = skb_clone(skb_in, GFP_ATOMIC);
1039 skb->data = ((u8 *)skb_in->data) + offset;
1040 skb_set_tail_pointer(skb, len);
1041 usbnet_skb_return(dev, skb);
1050 cdc_ncm_speed_change(struct cdc_ncm_ctx *ctx,
1051 struct usb_cdc_speed_change *data)
1053 uint32_t rx_speed = le32_to_cpu(data->DLBitRRate);
1054 uint32_t tx_speed = le32_to_cpu(data->ULBitRate);
1057 * Currently the USB-NET API does not support reporting the actual
1058 * device speed. Do print it instead.
1060 if ((tx_speed != ctx->tx_speed) || (rx_speed != ctx->rx_speed)) {
1061 ctx->tx_speed = tx_speed;
1062 ctx->rx_speed = rx_speed;
1064 if ((tx_speed > 1000000) && (rx_speed > 1000000)) {
1065 printk(KERN_INFO KBUILD_MODNAME
1066 ": %s: %u mbit/s downlink "
1067 "%u mbit/s uplink\n",
1069 (unsigned int)(rx_speed / 1000000U),
1070 (unsigned int)(tx_speed / 1000000U));
1072 printk(KERN_INFO KBUILD_MODNAME
1073 ": %s: %u kbit/s downlink "
1074 "%u kbit/s uplink\n",
1076 (unsigned int)(rx_speed / 1000U),
1077 (unsigned int)(tx_speed / 1000U));
1082 static void cdc_ncm_status(struct usbnet *dev, struct urb *urb)
1084 struct cdc_ncm_ctx *ctx;
1085 struct usb_cdc_notification *event;
1087 ctx = (struct cdc_ncm_ctx *)dev->data[0];
1089 if (urb->actual_length < sizeof(*event))
1092 /* test for split data in 8-byte chunks */
1093 if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
1094 cdc_ncm_speed_change(ctx,
1095 (struct usb_cdc_speed_change *)urb->transfer_buffer);
1099 event = urb->transfer_buffer;
1101 switch (event->bNotificationType) {
1102 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
1104 * According to the CDC NCM specification ch.7.1
1105 * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be
1106 * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE.
1108 ctx->connected = event->wValue;
1110 printk(KERN_INFO KBUILD_MODNAME ": %s: network connection:"
1112 ctx->netdev->name, ctx->connected ? "" : "dis");
1115 netif_carrier_on(dev->net);
1117 netif_carrier_off(dev->net);
1118 ctx->tx_speed = ctx->rx_speed = 0;
1122 case USB_CDC_NOTIFY_SPEED_CHANGE:
1123 if (urb->actual_length < (sizeof(*event) +
1124 sizeof(struct usb_cdc_speed_change)))
1125 set_bit(EVENT_STS_SPLIT, &dev->flags);
1127 cdc_ncm_speed_change(ctx,
1128 (struct usb_cdc_speed_change *) &event[1]);
1132 dev_err(&dev->udev->dev, "NCM: unexpected "
1133 "notification 0x%02x!\n", event->bNotificationType);
1138 static int cdc_ncm_check_connect(struct usbnet *dev)
1140 struct cdc_ncm_ctx *ctx;
1142 ctx = (struct cdc_ncm_ctx *)dev->data[0];
1144 return 1; /* disconnected */
1146 return !ctx->connected;
1150 cdc_ncm_probe(struct usb_interface *udev, const struct usb_device_id *prod)
1152 return usbnet_probe(udev, prod);
1155 static void cdc_ncm_disconnect(struct usb_interface *intf)
1157 struct usbnet *dev = usb_get_intfdata(intf);
1160 return; /* already disconnected */
1162 usbnet_disconnect(intf);
1165 static int cdc_ncm_manage_power(struct usbnet *dev, int status)
1167 dev->intf->needs_remote_wakeup = status;
1171 static const struct driver_info cdc_ncm_info = {
1172 .description = "CDC NCM",
1173 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET,
1174 .bind = cdc_ncm_bind,
1175 .unbind = cdc_ncm_unbind,
1176 .check_connect = cdc_ncm_check_connect,
1177 .manage_power = cdc_ncm_manage_power,
1178 .status = cdc_ncm_status,
1179 .rx_fixup = cdc_ncm_rx_fixup,
1180 .tx_fixup = cdc_ncm_tx_fixup,
1183 /* Same as cdc_ncm_info, but with FLAG_WWAN */
1184 static const struct driver_info wwan_info = {
1185 .description = "Mobile Broadband Network Device",
1186 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
1188 .bind = cdc_ncm_bind,
1189 .unbind = cdc_ncm_unbind,
1190 .check_connect = cdc_ncm_check_connect,
1191 .manage_power = cdc_ncm_manage_power,
1192 .status = cdc_ncm_status,
1193 .rx_fixup = cdc_ncm_rx_fixup,
1194 .tx_fixup = cdc_ncm_tx_fixup,
1197 static const struct usb_device_id cdc_devs[] = {
1198 /* Ericsson MBM devices like F5521gw */
1199 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1200 | USB_DEVICE_ID_MATCH_VENDOR,
1202 .bInterfaceClass = USB_CLASS_COMM,
1203 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1204 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1205 .driver_info = (unsigned long) &wwan_info,
1208 /* Dell branded MBM devices like DW5550 */
1209 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1210 | USB_DEVICE_ID_MATCH_VENDOR,
1212 .bInterfaceClass = USB_CLASS_COMM,
1213 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1214 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1215 .driver_info = (unsigned long) &wwan_info,
1218 /* Toshiba branded MBM devices */
1219 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1220 | USB_DEVICE_ID_MATCH_VENDOR,
1222 .bInterfaceClass = USB_CLASS_COMM,
1223 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1224 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1225 .driver_info = (unsigned long) &wwan_info,
1228 /* Generic CDC-NCM devices */
1229 { USB_INTERFACE_INFO(USB_CLASS_COMM,
1230 USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
1231 .driver_info = (unsigned long)&cdc_ncm_info,
1236 MODULE_DEVICE_TABLE(usb, cdc_devs);
1238 static struct usb_driver cdc_ncm_driver = {
1240 .id_table = cdc_devs,
1241 .probe = cdc_ncm_probe,
1242 .disconnect = cdc_ncm_disconnect,
1243 .suspend = usbnet_suspend,
1244 .resume = usbnet_resume,
1245 .reset_resume = usbnet_resume,
1246 .supports_autosuspend = 1,
1247 .disable_hub_initiated_lpm = 1,
1250 module_usb_driver(cdc_ncm_driver);
1252 MODULE_AUTHOR("Hans Petter Selasky");
1253 MODULE_DESCRIPTION("USB CDC NCM host driver");
1254 MODULE_LICENSE("Dual BSD/GPL");