4 * Copyright (C) ST-Ericsson 2010
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/version.h>
51 #include <linux/timer.h>
52 #include <linux/spinlock.h>
53 #include <linux/atomic.h>
54 #include <linux/usb/usbnet.h>
55 #include <linux/usb/cdc.h>
57 #define DRIVER_VERSION "30-Nov-2010"
59 /* CDC NCM subclass 3.2.1 */
60 #define USB_CDC_NCM_NDP16_LENGTH_MIN 0x10
62 /* Maximum NTB length */
63 #define CDC_NCM_NTB_MAX_SIZE_TX 16384 /* bytes */
64 #define CDC_NCM_NTB_MAX_SIZE_RX 16384 /* bytes */
66 /* Minimum value for MaxDatagramSize, ch. 6.2.9 */
67 #define CDC_NCM_MIN_DATAGRAM_SIZE 1514 /* bytes */
69 #define CDC_NCM_MIN_TX_PKT 512 /* bytes */
71 /* Default value for MaxDatagramSize */
72 #define CDC_NCM_MAX_DATAGRAM_SIZE 2048 /* bytes */
75 * Maximum amount of datagrams in NCM Datagram Pointer Table, not counting
76 * the last NULL entry. Any additional datagrams in NTB would be discarded.
78 #define CDC_NCM_DPT_DATAGRAMS_MAX 32
80 /* Restart the timer, if amount of datagrams is less than given value */
81 #define CDC_NCM_RESTART_TIMER_DATAGRAM_CNT 3
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))
88 struct connection_speed_change {
89 __le32 USBitRate; /* holds 3GPP downlink value, bits per second */
90 __le32 DSBitRate; /* holds 3GPP uplink value, bits per second */
91 } __attribute__ ((packed));
94 struct usb_cdc_ncm_nth16 nth16;
95 struct usb_cdc_ncm_ndp16 ndp16;
96 struct usb_cdc_ncm_dpe16 dpe16[CDC_NCM_DPT_DATAGRAMS_MAX + 1];
100 struct cdc_ncm_data rx_ncm;
101 struct cdc_ncm_data tx_ncm;
102 struct usb_cdc_ncm_ntb_parameters ncm_parm;
103 struct timer_list tx_timer;
105 const struct usb_cdc_ncm_desc *func_desc;
106 const struct usb_cdc_header_desc *header_desc;
107 const struct usb_cdc_union_desc *union_desc;
108 const struct usb_cdc_ether_desc *ether_desc;
110 struct net_device *netdev;
111 struct usb_device *udev;
112 struct usb_host_endpoint *in_ep;
113 struct usb_host_endpoint *out_ep;
114 struct usb_host_endpoint *status_ep;
115 struct usb_interface *intf;
116 struct usb_interface *control;
117 struct usb_interface *data;
119 struct sk_buff *tx_curr_skb;
120 struct sk_buff *tx_rem_skb;
124 u32 tx_timer_pending;
126 u32 tx_curr_last_offset;
127 u32 tx_curr_frame_num;
132 u32 max_datagram_size;
133 u16 tx_max_datagrams;
143 static void cdc_ncm_tx_timeout(unsigned long arg);
144 static const struct driver_info cdc_ncm_info;
145 static struct usb_driver cdc_ncm_driver;
146 static struct ethtool_ops cdc_ncm_ethtool_ops;
148 static const struct usb_device_id cdc_devs[] = {
149 { USB_INTERFACE_INFO(USB_CLASS_COMM,
150 USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
151 .driver_info = (unsigned long)&cdc_ncm_info,
157 MODULE_DEVICE_TABLE(usb, cdc_devs);
160 cdc_ncm_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
162 struct usbnet *dev = netdev_priv(net);
164 strncpy(info->driver, dev->driver_name, sizeof(info->driver));
165 strncpy(info->version, DRIVER_VERSION, sizeof(info->version));
166 strncpy(info->fw_version, dev->driver_info->description,
167 sizeof(info->fw_version));
168 usb_make_path(dev->udev, info->bus_info, sizeof(info->bus_info));
172 cdc_ncm_do_request(struct cdc_ncm_ctx *ctx, struct usb_cdc_notification *req,
173 void *data, u16 flags, u16 *actlen, u16 timeout)
177 err = usb_control_msg(ctx->udev, (req->bmRequestType & USB_DIR_IN) ?
178 usb_rcvctrlpipe(ctx->udev, 0) :
179 usb_sndctrlpipe(ctx->udev, 0),
180 req->bNotificationType, req->bmRequestType,
183 req->wLength, timeout);
197 static u8 cdc_ncm_setup(struct cdc_ncm_ctx *ctx)
199 struct usb_cdc_notification req;
201 __le16 max_datagram_size;
206 iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber;
208 req.bmRequestType = USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE;
209 req.bNotificationType = USB_CDC_GET_NTB_PARAMETERS;
211 req.wIndex = cpu_to_le16(iface_no);
212 req.wLength = cpu_to_le16(sizeof(ctx->ncm_parm));
214 err = cdc_ncm_do_request(ctx, &req, &ctx->ncm_parm, 0, NULL, 1000);
216 pr_debug("failed GET_NTB_PARAMETERS\n");
220 /* read correct set of parameters according to device mode */
221 ctx->rx_max = le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize);
222 ctx->tx_max = le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize);
223 ctx->tx_remainder = le16_to_cpu(ctx->ncm_parm.wNdpOutPayloadRemainder);
224 ctx->tx_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutDivisor);
225 ctx->tx_ndp_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutAlignment);
227 if (ctx->func_desc != NULL)
228 flags = ctx->func_desc->bmNetworkCapabilities;
232 pr_debug("dwNtbInMaxSize=%u dwNtbOutMaxSize=%u "
233 "wNdpOutPayloadRemainder=%u wNdpOutDivisor=%u "
234 "wNdpOutAlignment=%u flags=0x%x\n",
235 ctx->rx_max, ctx->tx_max, ctx->tx_remainder, ctx->tx_modulus,
236 ctx->tx_ndp_modulus, flags);
238 /* max count of tx datagrams without terminating NULL entry */
239 ctx->tx_max_datagrams = CDC_NCM_DPT_DATAGRAMS_MAX;
241 /* verify maximum size of received NTB in bytes */
243 (CDC_NCM_MIN_HDR_SIZE + CDC_NCM_MIN_DATAGRAM_SIZE)) ||
244 (ctx->rx_max > CDC_NCM_NTB_MAX_SIZE_RX)) {
245 pr_debug("Using default maximum receive length=%d\n",
246 CDC_NCM_NTB_MAX_SIZE_RX);
247 ctx->rx_max = CDC_NCM_NTB_MAX_SIZE_RX;
250 /* verify maximum size of transmitted NTB in bytes */
252 (CDC_NCM_MIN_HDR_SIZE + CDC_NCM_MIN_DATAGRAM_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 req.bmRequestType = USB_TYPE_CLASS | USB_DIR_OUT | USB_RECIP_INTERFACE;
301 req.bNotificationType = USB_CDC_SET_CRC_MODE;
302 req.wValue = cpu_to_le16(USB_CDC_NCM_CRC_NOT_APPENDED);
303 req.wIndex = cpu_to_le16(iface_no);
306 err = cdc_ncm_do_request(ctx, &req, NULL, 0, NULL, 1000);
308 pr_debug("Setting CRC mode off failed\n");
311 req.bmRequestType = USB_TYPE_CLASS | USB_DIR_OUT | USB_RECIP_INTERFACE;
312 req.bNotificationType = USB_CDC_SET_NTB_FORMAT;
313 req.wValue = cpu_to_le16(USB_CDC_NCM_NTB16_FORMAT);
314 req.wIndex = cpu_to_le16(iface_no);
317 err = cdc_ncm_do_request(ctx, &req, NULL, 0, NULL, 1000);
319 pr_debug("Setting NTB format to 16-bit failed\n");
321 /* set Max Datagram Size (MTU) */
322 req.bmRequestType = USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE;
323 req.bNotificationType = USB_CDC_GET_MAX_DATAGRAM_SIZE;
325 req.wIndex = cpu_to_le16(iface_no);
326 req.wLength = cpu_to_le16(2);
328 err = cdc_ncm_do_request(ctx, &req, &max_datagram_size, 0, NULL, 1000);
330 pr_debug(" GET_MAX_DATAGRAM_SIZE failed, using size=%u\n",
331 CDC_NCM_MIN_DATAGRAM_SIZE);
333 ctx->max_datagram_size = CDC_NCM_MIN_DATAGRAM_SIZE;
335 ctx->max_datagram_size = le16_to_cpu(max_datagram_size);
337 if (ctx->max_datagram_size < CDC_NCM_MIN_DATAGRAM_SIZE)
338 ctx->max_datagram_size = CDC_NCM_MIN_DATAGRAM_SIZE;
339 else if (ctx->max_datagram_size > CDC_NCM_MAX_DATAGRAM_SIZE)
340 ctx->max_datagram_size = CDC_NCM_MAX_DATAGRAM_SIZE;
343 if (ctx->netdev->mtu != (ctx->max_datagram_size - ETH_HLEN))
344 ctx->netdev->mtu = ctx->max_datagram_size - ETH_HLEN;
350 cdc_ncm_find_endpoints(struct cdc_ncm_ctx *ctx, struct usb_interface *intf)
352 struct usb_host_endpoint *e;
355 for (ep = 0; ep < intf->cur_altsetting->desc.bNumEndpoints; ep++) {
357 e = intf->cur_altsetting->endpoint + ep;
358 switch (e->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
359 case USB_ENDPOINT_XFER_INT:
360 if (usb_endpoint_dir_in(&e->desc)) {
361 if (ctx->status_ep == NULL)
366 case USB_ENDPOINT_XFER_BULK:
367 if (usb_endpoint_dir_in(&e->desc)) {
368 if (ctx->in_ep == NULL)
371 if (ctx->out_ep == NULL)
382 static void cdc_ncm_free(struct cdc_ncm_ctx *ctx)
387 del_timer_sync(&ctx->tx_timer);
389 if (ctx->data_claimed) {
390 usb_set_intfdata(ctx->data, NULL);
391 usb_driver_release_interface(driver_of(ctx->intf), ctx->data);
394 if (ctx->control_claimed) {
395 usb_set_intfdata(ctx->control, NULL);
396 usb_driver_release_interface(driver_of(ctx->intf),
400 if (ctx->tx_rem_skb != NULL) {
401 dev_kfree_skb_any(ctx->tx_rem_skb);
402 ctx->tx_rem_skb = NULL;
405 if (ctx->tx_curr_skb != NULL) {
406 dev_kfree_skb_any(ctx->tx_curr_skb);
407 ctx->tx_curr_skb = NULL;
413 static int cdc_ncm_bind(struct usbnet *dev, struct usb_interface *intf)
415 struct cdc_ncm_ctx *ctx;
416 struct usb_driver *driver;
422 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
426 memset(ctx, 0, sizeof(*ctx));
428 init_timer(&ctx->tx_timer);
429 spin_lock_init(&ctx->mtx);
430 ctx->netdev = dev->net;
432 /* store ctx pointer in device data field */
433 dev->data[0] = (unsigned long)ctx;
435 /* get some pointers */
436 driver = driver_of(intf);
437 buf = intf->cur_altsetting->extra;
438 len = intf->cur_altsetting->extralen;
440 ctx->udev = dev->udev;
443 /* parse through descriptors associated with control interface */
444 while ((len > 0) && (buf[0] > 2) && (buf[0] <= len)) {
446 if (buf[1] != USB_DT_CS_INTERFACE)
450 case USB_CDC_UNION_TYPE:
451 if (buf[0] < sizeof(*(ctx->union_desc)))
455 (const struct usb_cdc_union_desc *)buf;
457 ctx->control = usb_ifnum_to_if(dev->udev,
458 ctx->union_desc->bMasterInterface0);
459 ctx->data = usb_ifnum_to_if(dev->udev,
460 ctx->union_desc->bSlaveInterface0);
463 case USB_CDC_ETHERNET_TYPE:
464 if (buf[0] < sizeof(*(ctx->ether_desc)))
468 (const struct usb_cdc_ether_desc *)buf;
471 le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
474 (CDC_NCM_MIN_DATAGRAM_SIZE - ETH_HLEN))
476 CDC_NCM_MIN_DATAGRAM_SIZE - ETH_HLEN;
478 else if (dev->hard_mtu >
479 (CDC_NCM_MAX_DATAGRAM_SIZE - ETH_HLEN))
481 CDC_NCM_MAX_DATAGRAM_SIZE - ETH_HLEN;
484 case USB_CDC_NCM_TYPE:
485 if (buf[0] < sizeof(*(ctx->func_desc)))
488 ctx->func_desc = (const struct usb_cdc_ncm_desc *)buf;
495 /* advance to next descriptor */
501 /* check if we got everything */
502 if ((ctx->control == NULL) || (ctx->data == NULL) ||
503 (ctx->ether_desc == NULL))
506 /* claim interfaces, if any */
507 if (ctx->data != intf) {
508 temp = usb_driver_claim_interface(driver, ctx->data, dev);
511 ctx->data_claimed = 1;
514 if (ctx->control != intf) {
515 temp = usb_driver_claim_interface(driver, ctx->control, dev);
518 ctx->control_claimed = 1;
521 iface_no = ctx->data->cur_altsetting->desc.bInterfaceNumber;
523 /* reset data interface */
524 temp = usb_set_interface(dev->udev, iface_no, 0);
528 /* initialize data interface */
529 if (cdc_ncm_setup(ctx))
532 /* configure data interface */
533 temp = usb_set_interface(dev->udev, iface_no, 1);
537 cdc_ncm_find_endpoints(ctx, ctx->data);
538 cdc_ncm_find_endpoints(ctx, ctx->control);
540 if ((ctx->in_ep == NULL) || (ctx->out_ep == NULL) ||
541 (ctx->status_ep == NULL))
544 dev->net->ethtool_ops = &cdc_ncm_ethtool_ops;
546 usb_set_intfdata(ctx->data, dev);
547 usb_set_intfdata(ctx->control, dev);
548 usb_set_intfdata(ctx->intf, dev);
550 temp = usbnet_get_ethernet_addr(dev, ctx->ether_desc->iMACAddress);
554 dev_info(&dev->udev->dev, "MAC-Address: "
555 "0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x\n",
556 dev->net->dev_addr[0], dev->net->dev_addr[1],
557 dev->net->dev_addr[2], dev->net->dev_addr[3],
558 dev->net->dev_addr[4], dev->net->dev_addr[5]);
560 dev->in = usb_rcvbulkpipe(dev->udev,
561 ctx->in_ep->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
562 dev->out = usb_sndbulkpipe(dev->udev,
563 ctx->out_ep->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
564 dev->status = ctx->status_ep;
565 dev->rx_urb_size = ctx->rx_max;
568 * We should get an event when network connection is "connected" or
569 * "disconnected". Set network connection in "disconnected" state
570 * (carrier is OFF) during attach, so the IP network stack does not
571 * start IPv6 negotiation and more.
573 netif_carrier_off(dev->net);
574 ctx->tx_speed = ctx->rx_speed = 0;
578 cdc_ncm_free((struct cdc_ncm_ctx *)dev->data[0]);
580 dev_info(&dev->udev->dev, "Descriptor failure\n");
584 static void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf)
586 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
587 struct usb_driver *driver;
590 return; /* no setup */
592 driver = driver_of(intf);
594 usb_set_intfdata(ctx->data, NULL);
595 usb_set_intfdata(ctx->control, NULL);
596 usb_set_intfdata(ctx->intf, NULL);
598 /* release interfaces, if any */
599 if (ctx->data_claimed) {
600 usb_driver_release_interface(driver, ctx->data);
601 ctx->data_claimed = 0;
604 if (ctx->control_claimed) {
605 usb_driver_release_interface(driver, ctx->control);
606 ctx->control_claimed = 0;
612 static void cdc_ncm_zero_fill(u8 *ptr, u32 first, u32 end, u32 max)
620 memset(ptr + first, 0, end - first);
623 static struct sk_buff *
624 cdc_ncm_fill_tx_frame(struct cdc_ncm_ctx *ctx, struct sk_buff *skb)
626 struct sk_buff *skb_out;
633 /* if there is a remaining skb, it gets priority */
635 swap(skb, ctx->tx_rem_skb);
647 /* check if we are resuming an OUT skb */
648 if (ctx->tx_curr_skb != NULL) {
650 skb_out = ctx->tx_curr_skb;
651 offset = ctx->tx_curr_offset;
652 last_offset = ctx->tx_curr_last_offset;
653 n = ctx->tx_curr_frame_num;
656 /* reset variables */
657 skb_out = alloc_skb(ctx->tx_max, GFP_ATOMIC);
658 if (skb_out == NULL) {
660 dev_kfree_skb_any(skb);
661 ctx->netdev->stats.tx_dropped++;
666 /* make room for NTH and NDP */
667 offset = ALIGN(sizeof(struct usb_cdc_ncm_nth16),
668 ctx->tx_ndp_modulus) +
669 sizeof(struct usb_cdc_ncm_ndp16) +
670 (ctx->tx_max_datagrams + 1) *
671 sizeof(struct usb_cdc_ncm_dpe16);
673 /* store last valid offset before alignment */
674 last_offset = offset;
675 /* align first Datagram offset correctly */
676 offset = ALIGN(offset, ctx->tx_modulus) + ctx->tx_remainder;
677 /* zero buffer till the first IP datagram */
678 cdc_ncm_zero_fill(skb_out->data, 0, offset, offset);
680 ctx->tx_curr_frame_num = 0;
683 for (; n < ctx->tx_max_datagrams; n++) {
684 /* check if end of transmit buffer is reached */
685 if (offset >= ctx->tx_max)
688 /* compute maximum buffer size */
689 rem = ctx->tx_max - offset;
692 skb = ctx->tx_rem_skb;
693 ctx->tx_rem_skb = NULL;
695 /* check for end of skb */
700 if (skb->len > rem) {
702 /* won't fit, MTU problem? */
703 dev_kfree_skb_any(skb);
705 ctx->netdev->stats.tx_dropped++;
707 /* no room for skb - store for later */
708 if (ctx->tx_rem_skb != NULL) {
709 dev_kfree_skb_any(ctx->tx_rem_skb);
710 ctx->netdev->stats.tx_dropped++;
712 ctx->tx_rem_skb = skb;
715 /* loop one more time */
721 memcpy(((u8 *)skb_out->data) + offset, skb->data, skb->len);
723 ctx->tx_ncm.dpe16[n].wDatagramLength = cpu_to_le16(skb->len);
724 ctx->tx_ncm.dpe16[n].wDatagramIndex = cpu_to_le16(offset);
729 /* store last valid offset before alignment */
730 last_offset = offset;
732 /* align offset correctly */
733 offset = ALIGN(offset, ctx->tx_modulus) + ctx->tx_remainder;
736 cdc_ncm_zero_fill(skb_out->data, last_offset, offset,
738 dev_kfree_skb_any(skb);
742 /* free up any dangling skb */
744 dev_kfree_skb_any(skb);
746 ctx->netdev->stats.tx_dropped++;
749 ctx->tx_curr_frame_num = n;
752 /* wait for more frames */
754 ctx->tx_curr_skb = skb_out;
755 ctx->tx_curr_offset = offset;
756 ctx->tx_curr_last_offset = last_offset;
759 } else if ((n < ctx->tx_max_datagrams) && (timeout == 0)) {
760 /* wait for more frames */
762 ctx->tx_curr_skb = skb_out;
763 ctx->tx_curr_offset = offset;
764 ctx->tx_curr_last_offset = last_offset;
765 /* set the pending count */
766 if (n < CDC_NCM_RESTART_TIMER_DATAGRAM_CNT)
767 ctx->tx_timer_pending = 2;
772 /* variables will be reset at next call */
775 /* check for overflow */
776 if (last_offset > ctx->tx_max)
777 last_offset = ctx->tx_max;
780 offset = last_offset;
783 * If collected data size is less or equal CDC_NCM_MIN_TX_PKT bytes,
784 * we send buffers as it is. If we get more data, it would be more
785 * efficient for USB HS mobile device with DMA engine to receive a full
786 * size NTB, than canceling DMA transfer and receiving a short packet.
788 if (offset > CDC_NCM_MIN_TX_PKT)
789 offset = ctx->tx_max;
791 /* final zero padding */
792 cdc_ncm_zero_fill(skb_out->data, last_offset, offset, ctx->tx_max);
794 /* store last offset */
795 last_offset = offset;
797 if ((last_offset < ctx->tx_max) && ((last_offset %
798 le16_to_cpu(ctx->out_ep->desc.wMaxPacketSize)) == 0)) {
799 /* force short packet */
800 *(((u8 *)skb_out->data) + last_offset) = 0;
804 /* zero the rest of the DPEs plus the last NULL entry */
805 for (; n <= CDC_NCM_DPT_DATAGRAMS_MAX; n++) {
806 ctx->tx_ncm.dpe16[n].wDatagramLength = 0;
807 ctx->tx_ncm.dpe16[n].wDatagramIndex = 0;
810 /* fill out 16-bit NTB header */
811 ctx->tx_ncm.nth16.dwSignature = cpu_to_le32(USB_CDC_NCM_NTH16_SIGN);
812 ctx->tx_ncm.nth16.wHeaderLength =
813 cpu_to_le16(sizeof(ctx->tx_ncm.nth16));
814 ctx->tx_ncm.nth16.wSequence = cpu_to_le16(ctx->tx_seq);
815 ctx->tx_ncm.nth16.wBlockLength = cpu_to_le16(last_offset);
816 ctx->tx_ncm.nth16.wFpIndex = ALIGN(sizeof(struct usb_cdc_ncm_nth16),
817 ctx->tx_ndp_modulus);
819 memcpy(skb_out->data, &(ctx->tx_ncm.nth16), sizeof(ctx->tx_ncm.nth16));
822 /* fill out 16-bit NDP table */
823 ctx->tx_ncm.ndp16.dwSignature =
824 cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN);
825 rem = sizeof(ctx->tx_ncm.ndp16) + ((ctx->tx_curr_frame_num + 1) *
826 sizeof(struct usb_cdc_ncm_dpe16));
827 ctx->tx_ncm.ndp16.wLength = cpu_to_le16(rem);
828 ctx->tx_ncm.ndp16.wNextFpIndex = 0; /* reserved */
830 memcpy(((u8 *)skb_out->data) + ctx->tx_ncm.nth16.wFpIndex,
831 &(ctx->tx_ncm.ndp16),
832 sizeof(ctx->tx_ncm.ndp16));
834 memcpy(((u8 *)skb_out->data) + ctx->tx_ncm.nth16.wFpIndex +
835 sizeof(ctx->tx_ncm.ndp16),
836 &(ctx->tx_ncm.dpe16),
837 (ctx->tx_curr_frame_num + 1) *
838 sizeof(struct usb_cdc_ncm_dpe16));
840 /* set frame length */
841 skb_put(skb_out, last_offset);
844 ctx->tx_curr_skb = NULL;
851 static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx)
853 /* start timer, if not already started */
854 if (timer_pending(&ctx->tx_timer) == 0) {
855 ctx->tx_timer.function = &cdc_ncm_tx_timeout;
856 ctx->tx_timer.data = (unsigned long)ctx;
857 ctx->tx_timer.expires = jiffies + ((HZ + 999) / 1000);
858 add_timer(&ctx->tx_timer);
862 static void cdc_ncm_tx_timeout(unsigned long arg)
864 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)arg;
867 spin_lock(&ctx->mtx);
868 if (ctx->tx_timer_pending != 0) {
869 ctx->tx_timer_pending--;
874 spin_unlock(&ctx->mtx);
877 cdc_ncm_tx_timeout_start(ctx);
878 else if (ctx->netdev != NULL)
879 usbnet_start_xmit(NULL, ctx->netdev);
882 static struct sk_buff *
883 cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
885 struct sk_buff *skb_out;
886 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
890 * The Ethernet API we are using does not support transmitting
891 * multiple Ethernet frames in a single call. This driver will
892 * accumulate multiple Ethernet frames and send out a larger
893 * USB frame when the USB buffer is full or when a single jiffies
899 spin_lock(&ctx->mtx);
900 skb_out = cdc_ncm_fill_tx_frame(ctx, skb);
901 if (ctx->tx_curr_skb != NULL)
903 spin_unlock(&ctx->mtx);
905 /* Start timer, if there is a remaining skb */
907 cdc_ncm_tx_timeout_start(ctx);
910 dev->net->stats.tx_packets += ctx->tx_curr_frame_num;
915 dev_kfree_skb_any(skb);
920 static int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
923 struct cdc_ncm_ctx *ctx;
931 ctx = (struct cdc_ncm_ctx *)dev->data[0];
935 actlen = skb_in->len;
936 sumlen = CDC_NCM_NTB_MAX_SIZE_RX;
938 if (actlen < (sizeof(ctx->rx_ncm.nth16) + sizeof(ctx->rx_ncm.ndp16))) {
939 pr_debug("frame too short\n");
943 memcpy(&(ctx->rx_ncm.nth16), ((u8 *)skb_in->data),
944 sizeof(ctx->rx_ncm.nth16));
946 if (le32_to_cpu(ctx->rx_ncm.nth16.dwSignature) !=
947 USB_CDC_NCM_NTH16_SIGN) {
948 pr_debug("invalid NTH16 signature <%u>\n",
949 le32_to_cpu(ctx->rx_ncm.nth16.dwSignature));
953 temp = le16_to_cpu(ctx->rx_ncm.nth16.wBlockLength);
955 pr_debug("unsupported NTB block length %u/%u\n", temp, sumlen);
959 temp = le16_to_cpu(ctx->rx_ncm.nth16.wFpIndex);
960 if ((temp + sizeof(ctx->rx_ncm.ndp16)) > actlen) {
961 pr_debug("invalid DPT16 index\n");
965 memcpy(&(ctx->rx_ncm.ndp16), ((u8 *)skb_in->data) + temp,
966 sizeof(ctx->rx_ncm.ndp16));
968 if (le32_to_cpu(ctx->rx_ncm.ndp16.dwSignature) !=
969 USB_CDC_NCM_NDP16_NOCRC_SIGN) {
970 pr_debug("invalid DPT16 signature <%u>\n",
971 le32_to_cpu(ctx->rx_ncm.ndp16.dwSignature));
975 if (le16_to_cpu(ctx->rx_ncm.ndp16.wLength) <
976 USB_CDC_NCM_NDP16_LENGTH_MIN) {
977 pr_debug("invalid DPT16 length <%u>\n",
978 le32_to_cpu(ctx->rx_ncm.ndp16.dwSignature));
982 nframes = ((le16_to_cpu(ctx->rx_ncm.ndp16.wLength) -
983 sizeof(struct usb_cdc_ncm_ndp16)) /
984 sizeof(struct usb_cdc_ncm_dpe16));
985 nframes--; /* we process NDP entries except for the last one */
987 pr_debug("nframes = %u\n", nframes);
989 temp += sizeof(ctx->rx_ncm.ndp16);
991 if ((temp + nframes * (sizeof(struct usb_cdc_ncm_dpe16))) > actlen) {
992 pr_debug("Invalid nframes = %d\n", nframes);
996 if (nframes > CDC_NCM_DPT_DATAGRAMS_MAX) {
997 pr_debug("Truncating number of frames from %u to %u\n",
998 nframes, CDC_NCM_DPT_DATAGRAMS_MAX);
999 nframes = CDC_NCM_DPT_DATAGRAMS_MAX;
1002 memcpy(&(ctx->rx_ncm.dpe16), ((u8 *)skb_in->data) + temp,
1003 nframes * (sizeof(struct usb_cdc_ncm_dpe16)));
1005 for (x = 0; x < nframes; x++) {
1006 offset = le16_to_cpu(ctx->rx_ncm.dpe16[x].wDatagramIndex);
1007 temp = le16_to_cpu(ctx->rx_ncm.dpe16[x].wDatagramLength);
1011 * All entries after first NULL entry are to be ignored
1013 if ((offset == 0) || (temp == 0)) {
1015 goto error; /* empty NTB */
1019 /* sanity checking */
1020 if (((offset + temp) > actlen) ||
1021 (temp > CDC_NCM_MAX_DATAGRAM_SIZE) || (temp < ETH_HLEN)) {
1022 pr_debug("invalid frame detected (ignored)"
1023 "offset[%u]=%u, length=%u, skb=%p\n",
1024 x, offset, temp, skb_in);
1030 skb = skb_clone(skb_in, GFP_ATOMIC);
1034 skb->data = ((u8 *)skb_in->data) + offset;
1035 skb_set_tail_pointer(skb, temp);
1036 usbnet_skb_return(dev, skb);
1045 cdc_ncm_speed_change(struct cdc_ncm_ctx *ctx,
1046 struct connection_speed_change *data)
1048 uint32_t rx_speed = le32_to_cpu(data->USBitRate);
1049 uint32_t tx_speed = le32_to_cpu(data->DSBitRate);
1052 * Currently the USB-NET API does not support reporting the actual
1053 * device speed. Do print it instead.
1055 if ((tx_speed != ctx->tx_speed) || (rx_speed != ctx->rx_speed)) {
1056 ctx->tx_speed = tx_speed;
1057 ctx->rx_speed = rx_speed;
1059 if ((tx_speed > 1000000) && (rx_speed > 1000000)) {
1060 printk(KERN_INFO KBUILD_MODNAME
1061 ": %s: %u mbit/s downlink "
1062 "%u mbit/s uplink\n",
1064 (unsigned int)(rx_speed / 1000000U),
1065 (unsigned int)(tx_speed / 1000000U));
1067 printk(KERN_INFO KBUILD_MODNAME
1068 ": %s: %u kbit/s downlink "
1069 "%u kbit/s uplink\n",
1071 (unsigned int)(rx_speed / 1000U),
1072 (unsigned int)(tx_speed / 1000U));
1077 static void cdc_ncm_status(struct usbnet *dev, struct urb *urb)
1079 struct cdc_ncm_ctx *ctx;
1080 struct usb_cdc_notification *event;
1082 ctx = (struct cdc_ncm_ctx *)dev->data[0];
1084 if (urb->actual_length < sizeof(*event))
1087 /* test for split data in 8-byte chunks */
1088 if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
1089 cdc_ncm_speed_change(ctx,
1090 (struct connection_speed_change *)urb->transfer_buffer);
1094 event = urb->transfer_buffer;
1096 switch (event->bNotificationType) {
1097 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
1099 * According to the CDC NCM specification ch.7.1
1100 * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be
1101 * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE.
1103 ctx->connected = event->wValue;
1105 printk(KERN_INFO KBUILD_MODNAME ": %s: network connection:"
1107 ctx->netdev->name, ctx->connected ? "" : "dis");
1110 netif_carrier_on(dev->net);
1112 netif_carrier_off(dev->net);
1113 ctx->tx_speed = ctx->rx_speed = 0;
1117 case USB_CDC_NOTIFY_SPEED_CHANGE:
1118 if (urb->actual_length <
1119 (sizeof(*event) + sizeof(struct connection_speed_change)))
1120 set_bit(EVENT_STS_SPLIT, &dev->flags);
1122 cdc_ncm_speed_change(ctx,
1123 (struct connection_speed_change *) &event[1]);
1127 dev_err(&dev->udev->dev, "NCM: unexpected "
1128 "notification 0x%02x!\n", event->bNotificationType);
1133 static int cdc_ncm_check_connect(struct usbnet *dev)
1135 struct cdc_ncm_ctx *ctx;
1137 ctx = (struct cdc_ncm_ctx *)dev->data[0];
1139 return 1; /* disconnected */
1141 return !ctx->connected;
1145 cdc_ncm_probe(struct usb_interface *udev, const struct usb_device_id *prod)
1147 return usbnet_probe(udev, prod);
1150 static void cdc_ncm_disconnect(struct usb_interface *intf)
1152 struct usbnet *dev = usb_get_intfdata(intf);
1155 return; /* already disconnected */
1157 usbnet_disconnect(intf);
1160 static int cdc_ncm_manage_power(struct usbnet *dev, int status)
1162 dev->intf->needs_remote_wakeup = status;
1166 static const struct driver_info cdc_ncm_info = {
1167 .description = "CDC NCM",
1168 .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET,
1169 .bind = cdc_ncm_bind,
1170 .unbind = cdc_ncm_unbind,
1171 .check_connect = cdc_ncm_check_connect,
1172 .manage_power = cdc_ncm_manage_power,
1173 .status = cdc_ncm_status,
1174 .rx_fixup = cdc_ncm_rx_fixup,
1175 .tx_fixup = cdc_ncm_tx_fixup,
1178 static struct usb_driver cdc_ncm_driver = {
1180 .id_table = cdc_devs,
1181 .probe = cdc_ncm_probe,
1182 .disconnect = cdc_ncm_disconnect,
1183 .suspend = usbnet_suspend,
1184 .resume = usbnet_resume,
1185 .supports_autosuspend = 1,
1188 static struct ethtool_ops cdc_ncm_ethtool_ops = {
1189 .get_drvinfo = cdc_ncm_get_drvinfo,
1190 .get_link = usbnet_get_link,
1191 .get_msglevel = usbnet_get_msglevel,
1192 .set_msglevel = usbnet_set_msglevel,
1193 .get_settings = usbnet_get_settings,
1194 .set_settings = usbnet_set_settings,
1195 .nway_reset = usbnet_nway_reset,
1198 static int __init cdc_ncm_init(void)
1200 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION "\n");
1201 return usb_register(&cdc_ncm_driver);
1204 module_init(cdc_ncm_init);
1206 static void __exit cdc_ncm_exit(void)
1208 usb_deregister(&cdc_ncm_driver);
1211 module_exit(cdc_ncm_exit);
1213 MODULE_AUTHOR("Hans Petter Selasky");
1214 MODULE_DESCRIPTION("USB CDC NCM host driver");
1215 MODULE_LICENSE("Dual BSD/GPL");