4 * Copyright (C) ST-Ericsson 2010-2011
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 "06-May-2011"
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 /* Maximum amount of IN datagrams in NTB */
81 #define CDC_NCM_DPT_DATAGRAMS_IN_MAX 0 /* unlimited */
83 /* Restart the timer, if amount of datagrams is less than given value */
84 #define CDC_NCM_RESTART_TIMER_DATAGRAM_CNT 3
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))
92 struct usb_cdc_ncm_nth16 nth16
;
93 struct usb_cdc_ncm_ndp16 ndp16
;
94 struct usb_cdc_ncm_dpe16 dpe16
[CDC_NCM_DPT_DATAGRAMS_MAX
+ 1];
98 struct cdc_ncm_data rx_ncm
;
99 struct cdc_ncm_data tx_ncm
;
100 struct usb_cdc_ncm_ntb_parameters ncm_parm
;
101 struct timer_list tx_timer
;
103 const struct usb_cdc_ncm_desc
*func_desc
;
104 const struct usb_cdc_header_desc
*header_desc
;
105 const struct usb_cdc_union_desc
*union_desc
;
106 const struct usb_cdc_ether_desc
*ether_desc
;
108 struct net_device
*netdev
;
109 struct usb_device
*udev
;
110 struct usb_host_endpoint
*in_ep
;
111 struct usb_host_endpoint
*out_ep
;
112 struct usb_host_endpoint
*status_ep
;
113 struct usb_interface
*intf
;
114 struct usb_interface
*control
;
115 struct usb_interface
*data
;
117 struct sk_buff
*tx_curr_skb
;
118 struct sk_buff
*tx_rem_skb
;
122 u32 tx_timer_pending
;
124 u32 tx_curr_last_offset
;
125 u32 tx_curr_frame_num
;
130 u32 max_datagram_size
;
131 u16 tx_max_datagrams
;
141 static void cdc_ncm_tx_timeout(unsigned long arg
);
142 static const struct driver_info cdc_ncm_info
;
143 static struct usb_driver cdc_ncm_driver
;
144 static struct ethtool_ops cdc_ncm_ethtool_ops
;
146 static const struct usb_device_id cdc_devs
[] = {
147 { USB_INTERFACE_INFO(USB_CLASS_COMM
,
148 USB_CDC_SUBCLASS_NCM
, USB_CDC_PROTO_NONE
),
149 .driver_info
= (unsigned long)&cdc_ncm_info
,
155 MODULE_DEVICE_TABLE(usb
, cdc_devs
);
158 cdc_ncm_get_drvinfo(struct net_device
*net
, struct ethtool_drvinfo
*info
)
160 struct usbnet
*dev
= netdev_priv(net
);
162 strncpy(info
->driver
, dev
->driver_name
, sizeof(info
->driver
));
163 strncpy(info
->version
, DRIVER_VERSION
, sizeof(info
->version
));
164 strncpy(info
->fw_version
, dev
->driver_info
->description
,
165 sizeof(info
->fw_version
));
166 usb_make_path(dev
->udev
, info
->bus_info
, sizeof(info
->bus_info
));
170 cdc_ncm_do_request(struct cdc_ncm_ctx
*ctx
, struct usb_cdc_notification
*req
,
171 void *data
, u16 flags
, u16
*actlen
, u16 timeout
)
175 err
= usb_control_msg(ctx
->udev
, (req
->bmRequestType
& USB_DIR_IN
) ?
176 usb_rcvctrlpipe(ctx
->udev
, 0) :
177 usb_sndctrlpipe(ctx
->udev
, 0),
178 req
->bNotificationType
, req
->bmRequestType
,
181 req
->wLength
, timeout
);
195 static u8
cdc_ncm_setup(struct cdc_ncm_ctx
*ctx
)
197 struct usb_cdc_notification req
;
202 u16 ntb_fmt_supported
;
204 iface_no
= ctx
->control
->cur_altsetting
->desc
.bInterfaceNumber
;
206 req
.bmRequestType
= USB_TYPE_CLASS
| USB_DIR_IN
| USB_RECIP_INTERFACE
;
207 req
.bNotificationType
= USB_CDC_GET_NTB_PARAMETERS
;
209 req
.wIndex
= cpu_to_le16(iface_no
);
210 req
.wLength
= cpu_to_le16(sizeof(ctx
->ncm_parm
));
212 err
= cdc_ncm_do_request(ctx
, &req
, &ctx
->ncm_parm
, 0, NULL
, 1000);
214 pr_debug("failed GET_NTB_PARAMETERS\n");
218 /* read correct set of parameters according to device mode */
219 ctx
->rx_max
= le32_to_cpu(ctx
->ncm_parm
.dwNtbInMaxSize
);
220 ctx
->tx_max
= le32_to_cpu(ctx
->ncm_parm
.dwNtbOutMaxSize
);
221 ctx
->tx_remainder
= le16_to_cpu(ctx
->ncm_parm
.wNdpOutPayloadRemainder
);
222 ctx
->tx_modulus
= le16_to_cpu(ctx
->ncm_parm
.wNdpOutDivisor
);
223 ctx
->tx_ndp_modulus
= le16_to_cpu(ctx
->ncm_parm
.wNdpOutAlignment
);
224 /* devices prior to NCM Errata shall set this field to zero */
225 ctx
->tx_max_datagrams
= le16_to_cpu(ctx
->ncm_parm
.wNtbOutMaxDatagrams
);
226 ntb_fmt_supported
= le16_to_cpu(ctx
->ncm_parm
.bmNtbFormatsSupported
);
228 if (ctx
->func_desc
!= NULL
)
229 flags
= ctx
->func_desc
->bmNetworkCapabilities
;
233 pr_debug("dwNtbInMaxSize=%u dwNtbOutMaxSize=%u "
234 "wNdpOutPayloadRemainder=%u wNdpOutDivisor=%u "
235 "wNdpOutAlignment=%u wNtbOutMaxDatagrams=%u flags=0x%x\n",
236 ctx
->rx_max
, ctx
->tx_max
, ctx
->tx_remainder
, ctx
->tx_modulus
,
237 ctx
->tx_ndp_modulus
, ctx
->tx_max_datagrams
, flags
);
239 /* max count of tx datagrams */
240 if ((ctx
->tx_max_datagrams
== 0) ||
241 (ctx
->tx_max_datagrams
> CDC_NCM_DPT_DATAGRAMS_MAX
))
242 ctx
->tx_max_datagrams
= CDC_NCM_DPT_DATAGRAMS_MAX
;
244 /* verify maximum size of received NTB in bytes */
245 if (ctx
->rx_max
< USB_CDC_NCM_NTB_MIN_IN_SIZE
) {
246 pr_debug("Using min receive length=%d\n",
247 USB_CDC_NCM_NTB_MIN_IN_SIZE
);
248 ctx
->rx_max
= USB_CDC_NCM_NTB_MIN_IN_SIZE
;
251 if (ctx
->rx_max
> CDC_NCM_NTB_MAX_SIZE_RX
) {
252 pr_debug("Using default maximum receive length=%d\n",
253 CDC_NCM_NTB_MAX_SIZE_RX
);
254 ctx
->rx_max
= CDC_NCM_NTB_MAX_SIZE_RX
;
257 /* inform device about NTB input size changes */
258 if (ctx
->rx_max
!= le32_to_cpu(ctx
->ncm_parm
.dwNtbInMaxSize
)) {
259 req
.bmRequestType
= USB_TYPE_CLASS
| USB_DIR_OUT
|
261 req
.bNotificationType
= USB_CDC_SET_NTB_INPUT_SIZE
;
263 req
.wIndex
= cpu_to_le16(iface_no
);
265 if (flags
& USB_CDC_NCM_NCAP_NTB_INPUT_SIZE
) {
266 struct usb_cdc_ncm_ndp_input_size ndp_in_sz
;
269 ndp_in_sz
.dwNtbInMaxSize
= cpu_to_le32(ctx
->rx_max
);
270 ndp_in_sz
.wNtbInMaxDatagrams
=
271 cpu_to_le16(CDC_NCM_DPT_DATAGRAMS_MAX
);
272 ndp_in_sz
.wReserved
= 0;
273 err
= cdc_ncm_do_request(ctx
, &req
, &ndp_in_sz
, 0, NULL
,
276 __le32 dwNtbInMaxSize
= cpu_to_le32(ctx
->rx_max
);
279 err
= cdc_ncm_do_request(ctx
, &req
, &dwNtbInMaxSize
, 0,
284 pr_debug("Setting NTB Input Size failed\n");
287 /* verify maximum size of transmitted NTB in bytes */
289 (CDC_NCM_MIN_HDR_SIZE
+ CDC_NCM_MIN_DATAGRAM_SIZE
)) ||
290 (ctx
->tx_max
> CDC_NCM_NTB_MAX_SIZE_TX
)) {
291 pr_debug("Using default maximum transmit length=%d\n",
292 CDC_NCM_NTB_MAX_SIZE_TX
);
293 ctx
->tx_max
= CDC_NCM_NTB_MAX_SIZE_TX
;
297 * verify that the structure alignment is:
299 * - not greater than the maximum transmit length
300 * - not less than four bytes
302 val
= ctx
->tx_ndp_modulus
;
304 if ((val
< USB_CDC_NCM_NDP_ALIGN_MIN_SIZE
) ||
305 (val
!= ((-val
) & val
)) || (val
>= ctx
->tx_max
)) {
306 pr_debug("Using default alignment: 4 bytes\n");
307 ctx
->tx_ndp_modulus
= USB_CDC_NCM_NDP_ALIGN_MIN_SIZE
;
311 * verify that the payload alignment is:
313 * - not greater than the maximum transmit length
314 * - not less than four bytes
316 val
= ctx
->tx_modulus
;
318 if ((val
< USB_CDC_NCM_NDP_ALIGN_MIN_SIZE
) ||
319 (val
!= ((-val
) & val
)) || (val
>= ctx
->tx_max
)) {
320 pr_debug("Using default transmit modulus: 4 bytes\n");
321 ctx
->tx_modulus
= USB_CDC_NCM_NDP_ALIGN_MIN_SIZE
;
324 /* verify the payload remainder */
325 if (ctx
->tx_remainder
>= ctx
->tx_modulus
) {
326 pr_debug("Using default transmit remainder: 0 bytes\n");
327 ctx
->tx_remainder
= 0;
330 /* adjust TX-remainder according to NCM specification. */
331 ctx
->tx_remainder
= ((ctx
->tx_remainder
- ETH_HLEN
) &
332 (ctx
->tx_modulus
- 1));
334 /* additional configuration */
337 if (flags
& USB_CDC_NCM_NCAP_CRC_MODE
) {
338 req
.bmRequestType
= USB_TYPE_CLASS
| USB_DIR_OUT
|
340 req
.bNotificationType
= USB_CDC_SET_CRC_MODE
;
341 req
.wValue
= cpu_to_le16(USB_CDC_NCM_CRC_NOT_APPENDED
);
342 req
.wIndex
= cpu_to_le16(iface_no
);
345 err
= cdc_ncm_do_request(ctx
, &req
, NULL
, 0, NULL
, 1000);
347 pr_debug("Setting CRC mode off failed\n");
350 /* set NTB format, if both formats are supported */
351 if (ntb_fmt_supported
& USB_CDC_NCM_NTH32_SIGN
) {
352 req
.bmRequestType
= USB_TYPE_CLASS
| USB_DIR_OUT
|
354 req
.bNotificationType
= USB_CDC_SET_NTB_FORMAT
;
355 req
.wValue
= cpu_to_le16(USB_CDC_NCM_NTB16_FORMAT
);
356 req
.wIndex
= cpu_to_le16(iface_no
);
359 err
= cdc_ncm_do_request(ctx
, &req
, NULL
, 0, NULL
, 1000);
361 pr_debug("Setting NTB format to 16-bit failed\n");
364 ctx
->max_datagram_size
= CDC_NCM_MIN_DATAGRAM_SIZE
;
366 /* set Max Datagram Size (MTU) */
367 if (flags
& USB_CDC_NCM_NCAP_MAX_DATAGRAM_SIZE
) {
368 __le16 max_datagram_size
;
369 u16 eth_max_sz
= le16_to_cpu(ctx
->ether_desc
->wMaxSegmentSize
);
371 req
.bmRequestType
= USB_TYPE_CLASS
| USB_DIR_IN
|
373 req
.bNotificationType
= USB_CDC_GET_MAX_DATAGRAM_SIZE
;
375 req
.wIndex
= cpu_to_le16(iface_no
);
376 req
.wLength
= cpu_to_le16(2);
378 err
= cdc_ncm_do_request(ctx
, &req
, &max_datagram_size
, 0, NULL
,
381 pr_debug("GET_MAX_DATAGRAM_SIZE failed, use size=%u\n",
382 CDC_NCM_MIN_DATAGRAM_SIZE
);
384 ctx
->max_datagram_size
= le16_to_cpu(max_datagram_size
);
385 /* Check Eth descriptor value */
386 if (eth_max_sz
< CDC_NCM_MAX_DATAGRAM_SIZE
) {
387 if (ctx
->max_datagram_size
> eth_max_sz
)
388 ctx
->max_datagram_size
= eth_max_sz
;
390 if (ctx
->max_datagram_size
>
391 CDC_NCM_MAX_DATAGRAM_SIZE
)
392 ctx
->max_datagram_size
=
393 CDC_NCM_MAX_DATAGRAM_SIZE
;
396 if (ctx
->max_datagram_size
< CDC_NCM_MIN_DATAGRAM_SIZE
)
397 ctx
->max_datagram_size
=
398 CDC_NCM_MIN_DATAGRAM_SIZE
;
400 /* if value changed, update device */
401 req
.bmRequestType
= USB_TYPE_CLASS
| USB_DIR_OUT
|
403 req
.bNotificationType
= USB_CDC_SET_MAX_DATAGRAM_SIZE
;
405 req
.wIndex
= cpu_to_le16(iface_no
);
407 max_datagram_size
= cpu_to_le16(ctx
->max_datagram_size
);
409 err
= cdc_ncm_do_request(ctx
, &req
, &max_datagram_size
,
412 pr_debug("SET_MAX_DATAGRAM_SIZE failed\n");
417 if (ctx
->netdev
->mtu
!= (ctx
->max_datagram_size
- ETH_HLEN
))
418 ctx
->netdev
->mtu
= ctx
->max_datagram_size
- ETH_HLEN
;
424 cdc_ncm_find_endpoints(struct cdc_ncm_ctx
*ctx
, struct usb_interface
*intf
)
426 struct usb_host_endpoint
*e
;
429 for (ep
= 0; ep
< intf
->cur_altsetting
->desc
.bNumEndpoints
; ep
++) {
431 e
= intf
->cur_altsetting
->endpoint
+ ep
;
432 switch (e
->desc
.bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) {
433 case USB_ENDPOINT_XFER_INT
:
434 if (usb_endpoint_dir_in(&e
->desc
)) {
435 if (ctx
->status_ep
== NULL
)
440 case USB_ENDPOINT_XFER_BULK
:
441 if (usb_endpoint_dir_in(&e
->desc
)) {
442 if (ctx
->in_ep
== NULL
)
445 if (ctx
->out_ep
== NULL
)
456 static void cdc_ncm_free(struct cdc_ncm_ctx
*ctx
)
461 del_timer_sync(&ctx
->tx_timer
);
463 if (ctx
->data_claimed
) {
464 usb_set_intfdata(ctx
->data
, NULL
);
465 usb_driver_release_interface(driver_of(ctx
->intf
), ctx
->data
);
468 if (ctx
->control_claimed
) {
469 usb_set_intfdata(ctx
->control
, NULL
);
470 usb_driver_release_interface(driver_of(ctx
->intf
),
474 if (ctx
->tx_rem_skb
!= NULL
) {
475 dev_kfree_skb_any(ctx
->tx_rem_skb
);
476 ctx
->tx_rem_skb
= NULL
;
479 if (ctx
->tx_curr_skb
!= NULL
) {
480 dev_kfree_skb_any(ctx
->tx_curr_skb
);
481 ctx
->tx_curr_skb
= NULL
;
487 static int cdc_ncm_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
489 struct cdc_ncm_ctx
*ctx
;
490 struct usb_driver
*driver
;
496 ctx
= kmalloc(sizeof(*ctx
), GFP_KERNEL
);
500 memset(ctx
, 0, sizeof(*ctx
));
502 init_timer(&ctx
->tx_timer
);
503 spin_lock_init(&ctx
->mtx
);
504 ctx
->netdev
= dev
->net
;
506 /* store ctx pointer in device data field */
507 dev
->data
[0] = (unsigned long)ctx
;
509 /* get some pointers */
510 driver
= driver_of(intf
);
511 buf
= intf
->cur_altsetting
->extra
;
512 len
= intf
->cur_altsetting
->extralen
;
514 ctx
->udev
= dev
->udev
;
517 /* parse through descriptors associated with control interface */
518 while ((len
> 0) && (buf
[0] > 2) && (buf
[0] <= len
)) {
520 if (buf
[1] != USB_DT_CS_INTERFACE
)
524 case USB_CDC_UNION_TYPE
:
525 if (buf
[0] < sizeof(*(ctx
->union_desc
)))
529 (const struct usb_cdc_union_desc
*)buf
;
531 ctx
->control
= usb_ifnum_to_if(dev
->udev
,
532 ctx
->union_desc
->bMasterInterface0
);
533 ctx
->data
= usb_ifnum_to_if(dev
->udev
,
534 ctx
->union_desc
->bSlaveInterface0
);
537 case USB_CDC_ETHERNET_TYPE
:
538 if (buf
[0] < sizeof(*(ctx
->ether_desc
)))
542 (const struct usb_cdc_ether_desc
*)buf
;
544 le16_to_cpu(ctx
->ether_desc
->wMaxSegmentSize
);
546 if (dev
->hard_mtu
< CDC_NCM_MIN_DATAGRAM_SIZE
)
547 dev
->hard_mtu
= CDC_NCM_MIN_DATAGRAM_SIZE
;
548 else if (dev
->hard_mtu
> CDC_NCM_MAX_DATAGRAM_SIZE
)
549 dev
->hard_mtu
= CDC_NCM_MAX_DATAGRAM_SIZE
;
552 case USB_CDC_NCM_TYPE
:
553 if (buf
[0] < sizeof(*(ctx
->func_desc
)))
556 ctx
->func_desc
= (const struct usb_cdc_ncm_desc
*)buf
;
563 /* advance to next descriptor */
569 /* check if we got everything */
570 if ((ctx
->control
== NULL
) || (ctx
->data
== NULL
) ||
571 (ctx
->ether_desc
== NULL
))
574 /* claim interfaces, if any */
575 if (ctx
->data
!= intf
) {
576 temp
= usb_driver_claim_interface(driver
, ctx
->data
, dev
);
579 ctx
->data_claimed
= 1;
582 if (ctx
->control
!= intf
) {
583 temp
= usb_driver_claim_interface(driver
, ctx
->control
, dev
);
586 ctx
->control_claimed
= 1;
589 iface_no
= ctx
->data
->cur_altsetting
->desc
.bInterfaceNumber
;
591 /* reset data interface */
592 temp
= usb_set_interface(dev
->udev
, iface_no
, 0);
596 /* initialize data interface */
597 if (cdc_ncm_setup(ctx
))
600 /* configure data interface */
601 temp
= usb_set_interface(dev
->udev
, iface_no
, 1);
605 cdc_ncm_find_endpoints(ctx
, ctx
->data
);
606 cdc_ncm_find_endpoints(ctx
, ctx
->control
);
608 if ((ctx
->in_ep
== NULL
) || (ctx
->out_ep
== NULL
) ||
609 (ctx
->status_ep
== NULL
))
612 dev
->net
->ethtool_ops
= &cdc_ncm_ethtool_ops
;
614 usb_set_intfdata(ctx
->data
, dev
);
615 usb_set_intfdata(ctx
->control
, dev
);
616 usb_set_intfdata(ctx
->intf
, dev
);
618 temp
= usbnet_get_ethernet_addr(dev
, ctx
->ether_desc
->iMACAddress
);
622 dev_info(&dev
->udev
->dev
, "MAC-Address: "
623 "0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x\n",
624 dev
->net
->dev_addr
[0], dev
->net
->dev_addr
[1],
625 dev
->net
->dev_addr
[2], dev
->net
->dev_addr
[3],
626 dev
->net
->dev_addr
[4], dev
->net
->dev_addr
[5]);
628 dev
->in
= usb_rcvbulkpipe(dev
->udev
,
629 ctx
->in_ep
->desc
.bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
630 dev
->out
= usb_sndbulkpipe(dev
->udev
,
631 ctx
->out_ep
->desc
.bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
632 dev
->status
= ctx
->status_ep
;
633 dev
->rx_urb_size
= ctx
->rx_max
;
636 * We should get an event when network connection is "connected" or
637 * "disconnected". Set network connection in "disconnected" state
638 * (carrier is OFF) during attach, so the IP network stack does not
639 * start IPv6 negotiation and more.
641 netif_carrier_off(dev
->net
);
642 ctx
->tx_speed
= ctx
->rx_speed
= 0;
646 cdc_ncm_free((struct cdc_ncm_ctx
*)dev
->data
[0]);
648 dev_info(&dev
->udev
->dev
, "Descriptor failure\n");
652 static void cdc_ncm_unbind(struct usbnet
*dev
, struct usb_interface
*intf
)
654 struct cdc_ncm_ctx
*ctx
= (struct cdc_ncm_ctx
*)dev
->data
[0];
655 struct usb_driver
*driver
;
658 return; /* no setup */
660 driver
= driver_of(intf
);
662 usb_set_intfdata(ctx
->data
, NULL
);
663 usb_set_intfdata(ctx
->control
, NULL
);
664 usb_set_intfdata(ctx
->intf
, NULL
);
666 /* release interfaces, if any */
667 if (ctx
->data_claimed
) {
668 usb_driver_release_interface(driver
, ctx
->data
);
669 ctx
->data_claimed
= 0;
672 if (ctx
->control_claimed
) {
673 usb_driver_release_interface(driver
, ctx
->control
);
674 ctx
->control_claimed
= 0;
680 static void cdc_ncm_zero_fill(u8
*ptr
, u32 first
, u32 end
, u32 max
)
688 memset(ptr
+ first
, 0, end
- first
);
691 static struct sk_buff
*
692 cdc_ncm_fill_tx_frame(struct cdc_ncm_ctx
*ctx
, struct sk_buff
*skb
)
694 struct sk_buff
*skb_out
;
701 /* if there is a remaining skb, it gets priority */
703 swap(skb
, ctx
->tx_rem_skb
);
715 /* check if we are resuming an OUT skb */
716 if (ctx
->tx_curr_skb
!= NULL
) {
718 skb_out
= ctx
->tx_curr_skb
;
719 offset
= ctx
->tx_curr_offset
;
720 last_offset
= ctx
->tx_curr_last_offset
;
721 n
= ctx
->tx_curr_frame_num
;
724 /* reset variables */
725 skb_out
= alloc_skb((ctx
->tx_max
+ 1), GFP_ATOMIC
);
726 if (skb_out
== NULL
) {
728 dev_kfree_skb_any(skb
);
729 ctx
->netdev
->stats
.tx_dropped
++;
734 /* make room for NTH and NDP */
735 offset
= ALIGN(sizeof(struct usb_cdc_ncm_nth16
),
736 ctx
->tx_ndp_modulus
) +
737 sizeof(struct usb_cdc_ncm_ndp16
) +
738 (ctx
->tx_max_datagrams
+ 1) *
739 sizeof(struct usb_cdc_ncm_dpe16
);
741 /* store last valid offset before alignment */
742 last_offset
= offset
;
743 /* align first Datagram offset correctly */
744 offset
= ALIGN(offset
, ctx
->tx_modulus
) + ctx
->tx_remainder
;
745 /* zero buffer till the first IP datagram */
746 cdc_ncm_zero_fill(skb_out
->data
, 0, offset
, offset
);
748 ctx
->tx_curr_frame_num
= 0;
751 for (; n
< ctx
->tx_max_datagrams
; n
++) {
752 /* check if end of transmit buffer is reached */
753 if (offset
>= ctx
->tx_max
) {
757 /* compute maximum buffer size */
758 rem
= ctx
->tx_max
- offset
;
761 skb
= ctx
->tx_rem_skb
;
762 ctx
->tx_rem_skb
= NULL
;
764 /* check for end of skb */
769 if (skb
->len
> rem
) {
771 /* won't fit, MTU problem? */
772 dev_kfree_skb_any(skb
);
774 ctx
->netdev
->stats
.tx_dropped
++;
776 /* no room for skb - store for later */
777 if (ctx
->tx_rem_skb
!= NULL
) {
778 dev_kfree_skb_any(ctx
->tx_rem_skb
);
779 ctx
->netdev
->stats
.tx_dropped
++;
781 ctx
->tx_rem_skb
= skb
;
788 memcpy(((u8
*)skb_out
->data
) + offset
, skb
->data
, skb
->len
);
790 ctx
->tx_ncm
.dpe16
[n
].wDatagramLength
= cpu_to_le16(skb
->len
);
791 ctx
->tx_ncm
.dpe16
[n
].wDatagramIndex
= cpu_to_le16(offset
);
796 /* store last valid offset before alignment */
797 last_offset
= offset
;
799 /* align offset correctly */
800 offset
= ALIGN(offset
, ctx
->tx_modulus
) + ctx
->tx_remainder
;
803 cdc_ncm_zero_fill(skb_out
->data
, last_offset
, offset
,
805 dev_kfree_skb_any(skb
);
809 /* free up any dangling skb */
811 dev_kfree_skb_any(skb
);
813 ctx
->netdev
->stats
.tx_dropped
++;
816 ctx
->tx_curr_frame_num
= n
;
819 /* wait for more frames */
821 ctx
->tx_curr_skb
= skb_out
;
822 ctx
->tx_curr_offset
= offset
;
823 ctx
->tx_curr_last_offset
= last_offset
;
826 } else if ((n
< ctx
->tx_max_datagrams
) && (ready2send
== 0)) {
827 /* wait for more frames */
829 ctx
->tx_curr_skb
= skb_out
;
830 ctx
->tx_curr_offset
= offset
;
831 ctx
->tx_curr_last_offset
= last_offset
;
832 /* set the pending count */
833 if (n
< CDC_NCM_RESTART_TIMER_DATAGRAM_CNT
)
834 ctx
->tx_timer_pending
= 2;
839 /* variables will be reset at next call */
842 /* check for overflow */
843 if (last_offset
> ctx
->tx_max
)
844 last_offset
= ctx
->tx_max
;
847 offset
= last_offset
;
850 * If collected data size is less or equal CDC_NCM_MIN_TX_PKT bytes,
851 * we send buffers as it is. If we get more data, it would be more
852 * efficient for USB HS mobile device with DMA engine to receive a full
853 * size NTB, than canceling DMA transfer and receiving a short packet.
855 if (offset
> CDC_NCM_MIN_TX_PKT
)
856 offset
= ctx
->tx_max
;
858 /* final zero padding */
859 cdc_ncm_zero_fill(skb_out
->data
, last_offset
, offset
, ctx
->tx_max
);
861 /* store last offset */
862 last_offset
= offset
;
864 if (((last_offset
< ctx
->tx_max
) && ((last_offset
%
865 le16_to_cpu(ctx
->out_ep
->desc
.wMaxPacketSize
)) == 0)) ||
866 (((last_offset
== ctx
->tx_max
) && ((ctx
->tx_max
%
867 le16_to_cpu(ctx
->out_ep
->desc
.wMaxPacketSize
)) == 0)) &&
868 (ctx
->tx_max
< le32_to_cpu(ctx
->ncm_parm
.dwNtbOutMaxSize
)))) {
869 /* force short packet */
870 *(((u8
*)skb_out
->data
) + last_offset
) = 0;
874 /* zero the rest of the DPEs plus the last NULL entry */
875 for (; n
<= CDC_NCM_DPT_DATAGRAMS_MAX
; n
++) {
876 ctx
->tx_ncm
.dpe16
[n
].wDatagramLength
= 0;
877 ctx
->tx_ncm
.dpe16
[n
].wDatagramIndex
= 0;
880 /* fill out 16-bit NTB header */
881 ctx
->tx_ncm
.nth16
.dwSignature
= cpu_to_le32(USB_CDC_NCM_NTH16_SIGN
);
882 ctx
->tx_ncm
.nth16
.wHeaderLength
=
883 cpu_to_le16(sizeof(ctx
->tx_ncm
.nth16
));
884 ctx
->tx_ncm
.nth16
.wSequence
= cpu_to_le16(ctx
->tx_seq
);
885 ctx
->tx_ncm
.nth16
.wBlockLength
= cpu_to_le16(last_offset
);
886 ctx
->tx_ncm
.nth16
.wNdpIndex
= ALIGN(sizeof(struct usb_cdc_ncm_nth16
),
887 ctx
->tx_ndp_modulus
);
889 memcpy(skb_out
->data
, &(ctx
->tx_ncm
.nth16
), sizeof(ctx
->tx_ncm
.nth16
));
892 /* fill out 16-bit NDP table */
893 ctx
->tx_ncm
.ndp16
.dwSignature
=
894 cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN
);
895 rem
= sizeof(ctx
->tx_ncm
.ndp16
) + ((ctx
->tx_curr_frame_num
+ 1) *
896 sizeof(struct usb_cdc_ncm_dpe16
));
897 ctx
->tx_ncm
.ndp16
.wLength
= cpu_to_le16(rem
);
898 ctx
->tx_ncm
.ndp16
.wNextNdpIndex
= 0; /* reserved */
900 memcpy(((u8
*)skb_out
->data
) + ctx
->tx_ncm
.nth16
.wNdpIndex
,
901 &(ctx
->tx_ncm
.ndp16
),
902 sizeof(ctx
->tx_ncm
.ndp16
));
904 memcpy(((u8
*)skb_out
->data
) + ctx
->tx_ncm
.nth16
.wNdpIndex
+
905 sizeof(ctx
->tx_ncm
.ndp16
),
906 &(ctx
->tx_ncm
.dpe16
),
907 (ctx
->tx_curr_frame_num
+ 1) *
908 sizeof(struct usb_cdc_ncm_dpe16
));
910 /* set frame length */
911 skb_put(skb_out
, last_offset
);
914 ctx
->tx_curr_skb
= NULL
;
921 static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx
*ctx
)
923 /* start timer, if not already started */
924 if (timer_pending(&ctx
->tx_timer
) == 0) {
925 ctx
->tx_timer
.function
= &cdc_ncm_tx_timeout
;
926 ctx
->tx_timer
.data
= (unsigned long)ctx
;
927 ctx
->tx_timer
.expires
= jiffies
+ ((HZ
+ 999) / 1000);
928 add_timer(&ctx
->tx_timer
);
932 static void cdc_ncm_tx_timeout(unsigned long arg
)
934 struct cdc_ncm_ctx
*ctx
= (struct cdc_ncm_ctx
*)arg
;
937 spin_lock(&ctx
->mtx
);
938 if (ctx
->tx_timer_pending
!= 0) {
939 ctx
->tx_timer_pending
--;
945 spin_unlock(&ctx
->mtx
);
948 spin_lock(&ctx
->mtx
);
949 cdc_ncm_tx_timeout_start(ctx
);
950 spin_unlock(&ctx
->mtx
);
951 } else if (ctx
->netdev
!= NULL
) {
952 usbnet_start_xmit(NULL
, ctx
->netdev
);
956 static struct sk_buff
*
957 cdc_ncm_tx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
, gfp_t flags
)
959 struct sk_buff
*skb_out
;
960 struct cdc_ncm_ctx
*ctx
= (struct cdc_ncm_ctx
*)dev
->data
[0];
964 * The Ethernet API we are using does not support transmitting
965 * multiple Ethernet frames in a single call. This driver will
966 * accumulate multiple Ethernet frames and send out a larger
967 * USB frame when the USB buffer is full or when a single jiffies
973 spin_lock(&ctx
->mtx
);
974 skb_out
= cdc_ncm_fill_tx_frame(ctx
, skb
);
975 if (ctx
->tx_curr_skb
!= NULL
)
978 /* Start timer, if there is a remaining skb */
980 cdc_ncm_tx_timeout_start(ctx
);
983 dev
->net
->stats
.tx_packets
+= ctx
->tx_curr_frame_num
;
985 spin_unlock(&ctx
->mtx
);
990 dev_kfree_skb_any(skb
);
995 static int cdc_ncm_rx_fixup(struct usbnet
*dev
, struct sk_buff
*skb_in
)
998 struct cdc_ncm_ctx
*ctx
;
1006 ctx
= (struct cdc_ncm_ctx
*)dev
->data
[0];
1010 actlen
= skb_in
->len
;
1011 sumlen
= CDC_NCM_NTB_MAX_SIZE_RX
;
1013 if (actlen
< (sizeof(ctx
->rx_ncm
.nth16
) + sizeof(ctx
->rx_ncm
.ndp16
))) {
1014 pr_debug("frame too short\n");
1018 memcpy(&(ctx
->rx_ncm
.nth16
), ((u8
*)skb_in
->data
),
1019 sizeof(ctx
->rx_ncm
.nth16
));
1021 if (le32_to_cpu(ctx
->rx_ncm
.nth16
.dwSignature
) !=
1022 USB_CDC_NCM_NTH16_SIGN
) {
1023 pr_debug("invalid NTH16 signature <%u>\n",
1024 le32_to_cpu(ctx
->rx_ncm
.nth16
.dwSignature
));
1028 temp
= le16_to_cpu(ctx
->rx_ncm
.nth16
.wBlockLength
);
1029 if (temp
> sumlen
) {
1030 pr_debug("unsupported NTB block length %u/%u\n", temp
, sumlen
);
1034 temp
= le16_to_cpu(ctx
->rx_ncm
.nth16
.wNdpIndex
);
1035 if ((temp
+ sizeof(ctx
->rx_ncm
.ndp16
)) > actlen
) {
1036 pr_debug("invalid DPT16 index\n");
1040 memcpy(&(ctx
->rx_ncm
.ndp16
), ((u8
*)skb_in
->data
) + temp
,
1041 sizeof(ctx
->rx_ncm
.ndp16
));
1043 if (le32_to_cpu(ctx
->rx_ncm
.ndp16
.dwSignature
) !=
1044 USB_CDC_NCM_NDP16_NOCRC_SIGN
) {
1045 pr_debug("invalid DPT16 signature <%u>\n",
1046 le32_to_cpu(ctx
->rx_ncm
.ndp16
.dwSignature
));
1050 if (le16_to_cpu(ctx
->rx_ncm
.ndp16
.wLength
) <
1051 USB_CDC_NCM_NDP16_LENGTH_MIN
) {
1052 pr_debug("invalid DPT16 length <%u>\n",
1053 le32_to_cpu(ctx
->rx_ncm
.ndp16
.dwSignature
));
1057 nframes
= ((le16_to_cpu(ctx
->rx_ncm
.ndp16
.wLength
) -
1058 sizeof(struct usb_cdc_ncm_ndp16
)) /
1059 sizeof(struct usb_cdc_ncm_dpe16
));
1060 nframes
--; /* we process NDP entries except for the last one */
1062 pr_debug("nframes = %u\n", nframes
);
1064 temp
+= sizeof(ctx
->rx_ncm
.ndp16
);
1066 if ((temp
+ nframes
* (sizeof(struct usb_cdc_ncm_dpe16
))) > actlen
) {
1067 pr_debug("Invalid nframes = %d\n", nframes
);
1071 if (nframes
> CDC_NCM_DPT_DATAGRAMS_MAX
) {
1072 pr_debug("Truncating number of frames from %u to %u\n",
1073 nframes
, CDC_NCM_DPT_DATAGRAMS_MAX
);
1074 nframes
= CDC_NCM_DPT_DATAGRAMS_MAX
;
1077 memcpy(&(ctx
->rx_ncm
.dpe16
), ((u8
*)skb_in
->data
) + temp
,
1078 nframes
* (sizeof(struct usb_cdc_ncm_dpe16
)));
1080 for (x
= 0; x
< nframes
; x
++) {
1081 offset
= le16_to_cpu(ctx
->rx_ncm
.dpe16
[x
].wDatagramIndex
);
1082 temp
= le16_to_cpu(ctx
->rx_ncm
.dpe16
[x
].wDatagramLength
);
1086 * All entries after first NULL entry are to be ignored
1088 if ((offset
== 0) || (temp
== 0)) {
1090 goto error
; /* empty NTB */
1094 /* sanity checking */
1095 if (((offset
+ temp
) > actlen
) ||
1096 (temp
> CDC_NCM_MAX_DATAGRAM_SIZE
) || (temp
< ETH_HLEN
)) {
1097 pr_debug("invalid frame detected (ignored)"
1098 "offset[%u]=%u, length=%u, skb=%p\n",
1099 x
, offset
, temp
, skb_in
);
1105 skb
= skb_clone(skb_in
, GFP_ATOMIC
);
1109 skb
->data
= ((u8
*)skb_in
->data
) + offset
;
1110 skb_set_tail_pointer(skb
, temp
);
1111 usbnet_skb_return(dev
, skb
);
1120 cdc_ncm_speed_change(struct cdc_ncm_ctx
*ctx
,
1121 struct usb_cdc_speed_change
*data
)
1123 uint32_t rx_speed
= le32_to_cpu(data
->DLBitRRate
);
1124 uint32_t tx_speed
= le32_to_cpu(data
->ULBitRate
);
1127 * Currently the USB-NET API does not support reporting the actual
1128 * device speed. Do print it instead.
1130 if ((tx_speed
!= ctx
->tx_speed
) || (rx_speed
!= ctx
->rx_speed
)) {
1131 ctx
->tx_speed
= tx_speed
;
1132 ctx
->rx_speed
= rx_speed
;
1134 if ((tx_speed
> 1000000) && (rx_speed
> 1000000)) {
1135 printk(KERN_INFO KBUILD_MODNAME
1136 ": %s: %u mbit/s downlink "
1137 "%u mbit/s uplink\n",
1139 (unsigned int)(rx_speed
/ 1000000U),
1140 (unsigned int)(tx_speed
/ 1000000U));
1142 printk(KERN_INFO KBUILD_MODNAME
1143 ": %s: %u kbit/s downlink "
1144 "%u kbit/s uplink\n",
1146 (unsigned int)(rx_speed
/ 1000U),
1147 (unsigned int)(tx_speed
/ 1000U));
1152 static void cdc_ncm_status(struct usbnet
*dev
, struct urb
*urb
)
1154 struct cdc_ncm_ctx
*ctx
;
1155 struct usb_cdc_notification
*event
;
1157 ctx
= (struct cdc_ncm_ctx
*)dev
->data
[0];
1159 if (urb
->actual_length
< sizeof(*event
))
1162 /* test for split data in 8-byte chunks */
1163 if (test_and_clear_bit(EVENT_STS_SPLIT
, &dev
->flags
)) {
1164 cdc_ncm_speed_change(ctx
,
1165 (struct usb_cdc_speed_change
*)urb
->transfer_buffer
);
1169 event
= urb
->transfer_buffer
;
1171 switch (event
->bNotificationType
) {
1172 case USB_CDC_NOTIFY_NETWORK_CONNECTION
:
1174 * According to the CDC NCM specification ch.7.1
1175 * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be
1176 * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE.
1178 ctx
->connected
= event
->wValue
;
1180 printk(KERN_INFO KBUILD_MODNAME
": %s: network connection:"
1182 ctx
->netdev
->name
, ctx
->connected
? "" : "dis");
1185 netif_carrier_on(dev
->net
);
1187 netif_carrier_off(dev
->net
);
1188 ctx
->tx_speed
= ctx
->rx_speed
= 0;
1192 case USB_CDC_NOTIFY_SPEED_CHANGE
:
1193 if (urb
->actual_length
< (sizeof(*event
) +
1194 sizeof(struct usb_cdc_speed_change
)))
1195 set_bit(EVENT_STS_SPLIT
, &dev
->flags
);
1197 cdc_ncm_speed_change(ctx
,
1198 (struct usb_cdc_speed_change
*) &event
[1]);
1202 dev_err(&dev
->udev
->dev
, "NCM: unexpected "
1203 "notification 0x%02x!\n", event
->bNotificationType
);
1208 static int cdc_ncm_check_connect(struct usbnet
*dev
)
1210 struct cdc_ncm_ctx
*ctx
;
1212 ctx
= (struct cdc_ncm_ctx
*)dev
->data
[0];
1214 return 1; /* disconnected */
1216 return !ctx
->connected
;
1220 cdc_ncm_probe(struct usb_interface
*udev
, const struct usb_device_id
*prod
)
1222 return usbnet_probe(udev
, prod
);
1225 static void cdc_ncm_disconnect(struct usb_interface
*intf
)
1227 struct usbnet
*dev
= usb_get_intfdata(intf
);
1230 return; /* already disconnected */
1232 usbnet_disconnect(intf
);
1235 static int cdc_ncm_manage_power(struct usbnet
*dev
, int status
)
1237 dev
->intf
->needs_remote_wakeup
= status
;
1241 static const struct driver_info cdc_ncm_info
= {
1242 .description
= "CDC NCM",
1243 .flags
= FLAG_POINTTOPOINT
| FLAG_NO_SETINT
| FLAG_MULTI_PACKET
,
1244 .bind
= cdc_ncm_bind
,
1245 .unbind
= cdc_ncm_unbind
,
1246 .check_connect
= cdc_ncm_check_connect
,
1247 .manage_power
= cdc_ncm_manage_power
,
1248 .status
= cdc_ncm_status
,
1249 .rx_fixup
= cdc_ncm_rx_fixup
,
1250 .tx_fixup
= cdc_ncm_tx_fixup
,
1253 static struct usb_driver cdc_ncm_driver
= {
1255 .id_table
= cdc_devs
,
1256 .probe
= cdc_ncm_probe
,
1257 .disconnect
= cdc_ncm_disconnect
,
1258 .suspend
= usbnet_suspend
,
1259 .resume
= usbnet_resume
,
1260 .supports_autosuspend
= 1,
1263 static struct ethtool_ops cdc_ncm_ethtool_ops
= {
1264 .get_drvinfo
= cdc_ncm_get_drvinfo
,
1265 .get_link
= usbnet_get_link
,
1266 .get_msglevel
= usbnet_get_msglevel
,
1267 .set_msglevel
= usbnet_set_msglevel
,
1268 .get_settings
= usbnet_get_settings
,
1269 .set_settings
= usbnet_set_settings
,
1270 .nway_reset
= usbnet_nway_reset
,
1273 static int __init
cdc_ncm_init(void)
1275 printk(KERN_INFO KBUILD_MODNAME
": " DRIVER_VERSION
"\n");
1276 return usb_register(&cdc_ncm_driver
);
1279 module_init(cdc_ncm_init
);
1281 static void __exit
cdc_ncm_exit(void)
1283 usb_deregister(&cdc_ncm_driver
);
1286 module_exit(cdc_ncm_exit
);
1288 MODULE_AUTHOR("Hans Petter Selasky");
1289 MODULE_DESCRIPTION("USB CDC NCM host driver");
1290 MODULE_LICENSE("Dual BSD/GPL");