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 "7-Feb-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
, 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 /* force short packet */
867 *(((u8
*)skb_out
->data
) + last_offset
) = 0;
871 /* zero the rest of the DPEs plus the last NULL entry */
872 for (; n
<= CDC_NCM_DPT_DATAGRAMS_MAX
; n
++) {
873 ctx
->tx_ncm
.dpe16
[n
].wDatagramLength
= 0;
874 ctx
->tx_ncm
.dpe16
[n
].wDatagramIndex
= 0;
877 /* fill out 16-bit NTB header */
878 ctx
->tx_ncm
.nth16
.dwSignature
= cpu_to_le32(USB_CDC_NCM_NTH16_SIGN
);
879 ctx
->tx_ncm
.nth16
.wHeaderLength
=
880 cpu_to_le16(sizeof(ctx
->tx_ncm
.nth16
));
881 ctx
->tx_ncm
.nth16
.wSequence
= cpu_to_le16(ctx
->tx_seq
);
882 ctx
->tx_ncm
.nth16
.wBlockLength
= cpu_to_le16(last_offset
);
883 ctx
->tx_ncm
.nth16
.wNdpIndex
= ALIGN(sizeof(struct usb_cdc_ncm_nth16
),
884 ctx
->tx_ndp_modulus
);
886 memcpy(skb_out
->data
, &(ctx
->tx_ncm
.nth16
), sizeof(ctx
->tx_ncm
.nth16
));
889 /* fill out 16-bit NDP table */
890 ctx
->tx_ncm
.ndp16
.dwSignature
=
891 cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN
);
892 rem
= sizeof(ctx
->tx_ncm
.ndp16
) + ((ctx
->tx_curr_frame_num
+ 1) *
893 sizeof(struct usb_cdc_ncm_dpe16
));
894 ctx
->tx_ncm
.ndp16
.wLength
= cpu_to_le16(rem
);
895 ctx
->tx_ncm
.ndp16
.wNextNdpIndex
= 0; /* reserved */
897 memcpy(((u8
*)skb_out
->data
) + ctx
->tx_ncm
.nth16
.wNdpIndex
,
898 &(ctx
->tx_ncm
.ndp16
),
899 sizeof(ctx
->tx_ncm
.ndp16
));
901 memcpy(((u8
*)skb_out
->data
) + ctx
->tx_ncm
.nth16
.wNdpIndex
+
902 sizeof(ctx
->tx_ncm
.ndp16
),
903 &(ctx
->tx_ncm
.dpe16
),
904 (ctx
->tx_curr_frame_num
+ 1) *
905 sizeof(struct usb_cdc_ncm_dpe16
));
907 /* set frame length */
908 skb_put(skb_out
, last_offset
);
911 ctx
->tx_curr_skb
= NULL
;
918 static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx
*ctx
)
920 /* start timer, if not already started */
921 if (timer_pending(&ctx
->tx_timer
) == 0) {
922 ctx
->tx_timer
.function
= &cdc_ncm_tx_timeout
;
923 ctx
->tx_timer
.data
= (unsigned long)ctx
;
924 ctx
->tx_timer
.expires
= jiffies
+ ((HZ
+ 999) / 1000);
925 add_timer(&ctx
->tx_timer
);
929 static void cdc_ncm_tx_timeout(unsigned long arg
)
931 struct cdc_ncm_ctx
*ctx
= (struct cdc_ncm_ctx
*)arg
;
934 spin_lock(&ctx
->mtx
);
935 if (ctx
->tx_timer_pending
!= 0) {
936 ctx
->tx_timer_pending
--;
942 spin_unlock(&ctx
->mtx
);
945 spin_lock(&ctx
->mtx
);
946 cdc_ncm_tx_timeout_start(ctx
);
947 spin_unlock(&ctx
->mtx
);
948 } else if (ctx
->netdev
!= NULL
) {
949 usbnet_start_xmit(NULL
, ctx
->netdev
);
953 static struct sk_buff
*
954 cdc_ncm_tx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
, gfp_t flags
)
956 struct sk_buff
*skb_out
;
957 struct cdc_ncm_ctx
*ctx
= (struct cdc_ncm_ctx
*)dev
->data
[0];
961 * The Ethernet API we are using does not support transmitting
962 * multiple Ethernet frames in a single call. This driver will
963 * accumulate multiple Ethernet frames and send out a larger
964 * USB frame when the USB buffer is full or when a single jiffies
970 spin_lock(&ctx
->mtx
);
971 skb_out
= cdc_ncm_fill_tx_frame(ctx
, skb
);
972 if (ctx
->tx_curr_skb
!= NULL
)
975 /* Start timer, if there is a remaining skb */
977 cdc_ncm_tx_timeout_start(ctx
);
980 dev
->net
->stats
.tx_packets
+= ctx
->tx_curr_frame_num
;
982 spin_unlock(&ctx
->mtx
);
987 dev_kfree_skb_any(skb
);
992 static int cdc_ncm_rx_fixup(struct usbnet
*dev
, struct sk_buff
*skb_in
)
995 struct cdc_ncm_ctx
*ctx
;
1003 ctx
= (struct cdc_ncm_ctx
*)dev
->data
[0];
1007 actlen
= skb_in
->len
;
1008 sumlen
= CDC_NCM_NTB_MAX_SIZE_RX
;
1010 if (actlen
< (sizeof(ctx
->rx_ncm
.nth16
) + sizeof(ctx
->rx_ncm
.ndp16
))) {
1011 pr_debug("frame too short\n");
1015 memcpy(&(ctx
->rx_ncm
.nth16
), ((u8
*)skb_in
->data
),
1016 sizeof(ctx
->rx_ncm
.nth16
));
1018 if (le32_to_cpu(ctx
->rx_ncm
.nth16
.dwSignature
) !=
1019 USB_CDC_NCM_NTH16_SIGN
) {
1020 pr_debug("invalid NTH16 signature <%u>\n",
1021 le32_to_cpu(ctx
->rx_ncm
.nth16
.dwSignature
));
1025 temp
= le16_to_cpu(ctx
->rx_ncm
.nth16
.wBlockLength
);
1026 if (temp
> sumlen
) {
1027 pr_debug("unsupported NTB block length %u/%u\n", temp
, sumlen
);
1031 temp
= le16_to_cpu(ctx
->rx_ncm
.nth16
.wNdpIndex
);
1032 if ((temp
+ sizeof(ctx
->rx_ncm
.ndp16
)) > actlen
) {
1033 pr_debug("invalid DPT16 index\n");
1037 memcpy(&(ctx
->rx_ncm
.ndp16
), ((u8
*)skb_in
->data
) + temp
,
1038 sizeof(ctx
->rx_ncm
.ndp16
));
1040 if (le32_to_cpu(ctx
->rx_ncm
.ndp16
.dwSignature
) !=
1041 USB_CDC_NCM_NDP16_NOCRC_SIGN
) {
1042 pr_debug("invalid DPT16 signature <%u>\n",
1043 le32_to_cpu(ctx
->rx_ncm
.ndp16
.dwSignature
));
1047 if (le16_to_cpu(ctx
->rx_ncm
.ndp16
.wLength
) <
1048 USB_CDC_NCM_NDP16_LENGTH_MIN
) {
1049 pr_debug("invalid DPT16 length <%u>\n",
1050 le32_to_cpu(ctx
->rx_ncm
.ndp16
.dwSignature
));
1054 nframes
= ((le16_to_cpu(ctx
->rx_ncm
.ndp16
.wLength
) -
1055 sizeof(struct usb_cdc_ncm_ndp16
)) /
1056 sizeof(struct usb_cdc_ncm_dpe16
));
1057 nframes
--; /* we process NDP entries except for the last one */
1059 pr_debug("nframes = %u\n", nframes
);
1061 temp
+= sizeof(ctx
->rx_ncm
.ndp16
);
1063 if ((temp
+ nframes
* (sizeof(struct usb_cdc_ncm_dpe16
))) > actlen
) {
1064 pr_debug("Invalid nframes = %d\n", nframes
);
1068 if (nframes
> CDC_NCM_DPT_DATAGRAMS_MAX
) {
1069 pr_debug("Truncating number of frames from %u to %u\n",
1070 nframes
, CDC_NCM_DPT_DATAGRAMS_MAX
);
1071 nframes
= CDC_NCM_DPT_DATAGRAMS_MAX
;
1074 memcpy(&(ctx
->rx_ncm
.dpe16
), ((u8
*)skb_in
->data
) + temp
,
1075 nframes
* (sizeof(struct usb_cdc_ncm_dpe16
)));
1077 for (x
= 0; x
< nframes
; x
++) {
1078 offset
= le16_to_cpu(ctx
->rx_ncm
.dpe16
[x
].wDatagramIndex
);
1079 temp
= le16_to_cpu(ctx
->rx_ncm
.dpe16
[x
].wDatagramLength
);
1083 * All entries after first NULL entry are to be ignored
1085 if ((offset
== 0) || (temp
== 0)) {
1087 goto error
; /* empty NTB */
1091 /* sanity checking */
1092 if (((offset
+ temp
) > actlen
) ||
1093 (temp
> CDC_NCM_MAX_DATAGRAM_SIZE
) || (temp
< ETH_HLEN
)) {
1094 pr_debug("invalid frame detected (ignored)"
1095 "offset[%u]=%u, length=%u, skb=%p\n",
1096 x
, offset
, temp
, skb_in
);
1102 skb
= skb_clone(skb_in
, GFP_ATOMIC
);
1106 skb
->data
= ((u8
*)skb_in
->data
) + offset
;
1107 skb_set_tail_pointer(skb
, temp
);
1108 usbnet_skb_return(dev
, skb
);
1117 cdc_ncm_speed_change(struct cdc_ncm_ctx
*ctx
,
1118 struct usb_cdc_speed_change
*data
)
1120 uint32_t rx_speed
= le32_to_cpu(data
->DLBitRRate
);
1121 uint32_t tx_speed
= le32_to_cpu(data
->ULBitRate
);
1124 * Currently the USB-NET API does not support reporting the actual
1125 * device speed. Do print it instead.
1127 if ((tx_speed
!= ctx
->tx_speed
) || (rx_speed
!= ctx
->rx_speed
)) {
1128 ctx
->tx_speed
= tx_speed
;
1129 ctx
->rx_speed
= rx_speed
;
1131 if ((tx_speed
> 1000000) && (rx_speed
> 1000000)) {
1132 printk(KERN_INFO KBUILD_MODNAME
1133 ": %s: %u mbit/s downlink "
1134 "%u mbit/s uplink\n",
1136 (unsigned int)(rx_speed
/ 1000000U),
1137 (unsigned int)(tx_speed
/ 1000000U));
1139 printk(KERN_INFO KBUILD_MODNAME
1140 ": %s: %u kbit/s downlink "
1141 "%u kbit/s uplink\n",
1143 (unsigned int)(rx_speed
/ 1000U),
1144 (unsigned int)(tx_speed
/ 1000U));
1149 static void cdc_ncm_status(struct usbnet
*dev
, struct urb
*urb
)
1151 struct cdc_ncm_ctx
*ctx
;
1152 struct usb_cdc_notification
*event
;
1154 ctx
= (struct cdc_ncm_ctx
*)dev
->data
[0];
1156 if (urb
->actual_length
< sizeof(*event
))
1159 /* test for split data in 8-byte chunks */
1160 if (test_and_clear_bit(EVENT_STS_SPLIT
, &dev
->flags
)) {
1161 cdc_ncm_speed_change(ctx
,
1162 (struct usb_cdc_speed_change
*)urb
->transfer_buffer
);
1166 event
= urb
->transfer_buffer
;
1168 switch (event
->bNotificationType
) {
1169 case USB_CDC_NOTIFY_NETWORK_CONNECTION
:
1171 * According to the CDC NCM specification ch.7.1
1172 * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be
1173 * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE.
1175 ctx
->connected
= event
->wValue
;
1177 printk(KERN_INFO KBUILD_MODNAME
": %s: network connection:"
1179 ctx
->netdev
->name
, ctx
->connected
? "" : "dis");
1182 netif_carrier_on(dev
->net
);
1184 netif_carrier_off(dev
->net
);
1185 ctx
->tx_speed
= ctx
->rx_speed
= 0;
1189 case USB_CDC_NOTIFY_SPEED_CHANGE
:
1190 if (urb
->actual_length
< (sizeof(*event
) +
1191 sizeof(struct usb_cdc_speed_change
)))
1192 set_bit(EVENT_STS_SPLIT
, &dev
->flags
);
1194 cdc_ncm_speed_change(ctx
,
1195 (struct usb_cdc_speed_change
*) &event
[1]);
1199 dev_err(&dev
->udev
->dev
, "NCM: unexpected "
1200 "notification 0x%02x!\n", event
->bNotificationType
);
1205 static int cdc_ncm_check_connect(struct usbnet
*dev
)
1207 struct cdc_ncm_ctx
*ctx
;
1209 ctx
= (struct cdc_ncm_ctx
*)dev
->data
[0];
1211 return 1; /* disconnected */
1213 return !ctx
->connected
;
1217 cdc_ncm_probe(struct usb_interface
*udev
, const struct usb_device_id
*prod
)
1219 return usbnet_probe(udev
, prod
);
1222 static void cdc_ncm_disconnect(struct usb_interface
*intf
)
1224 struct usbnet
*dev
= usb_get_intfdata(intf
);
1227 return; /* already disconnected */
1229 usbnet_disconnect(intf
);
1232 static int cdc_ncm_manage_power(struct usbnet
*dev
, int status
)
1234 dev
->intf
->needs_remote_wakeup
= status
;
1238 static const struct driver_info cdc_ncm_info
= {
1239 .description
= "CDC NCM",
1240 .flags
= FLAG_NO_SETINT
| FLAG_MULTI_PACKET
,
1241 .bind
= cdc_ncm_bind
,
1242 .unbind
= cdc_ncm_unbind
,
1243 .check_connect
= cdc_ncm_check_connect
,
1244 .manage_power
= cdc_ncm_manage_power
,
1245 .status
= cdc_ncm_status
,
1246 .rx_fixup
= cdc_ncm_rx_fixup
,
1247 .tx_fixup
= cdc_ncm_tx_fixup
,
1250 static struct usb_driver cdc_ncm_driver
= {
1252 .id_table
= cdc_devs
,
1253 .probe
= cdc_ncm_probe
,
1254 .disconnect
= cdc_ncm_disconnect
,
1255 .suspend
= usbnet_suspend
,
1256 .resume
= usbnet_resume
,
1257 .supports_autosuspend
= 1,
1260 static struct ethtool_ops cdc_ncm_ethtool_ops
= {
1261 .get_drvinfo
= cdc_ncm_get_drvinfo
,
1262 .get_link
= usbnet_get_link
,
1263 .get_msglevel
= usbnet_get_msglevel
,
1264 .set_msglevel
= usbnet_set_msglevel
,
1265 .get_settings
= usbnet_get_settings
,
1266 .set_settings
= usbnet_set_settings
,
1267 .nway_reset
= usbnet_nway_reset
,
1270 static int __init
cdc_ncm_init(void)
1272 printk(KERN_INFO KBUILD_MODNAME
": " DRIVER_VERSION
"\n");
1273 return usb_register(&cdc_ncm_driver
);
1276 module_init(cdc_ncm_init
);
1278 static void __exit
cdc_ncm_exit(void)
1280 usb_deregister(&cdc_ncm_driver
);
1283 module_exit(cdc_ncm_exit
);
1285 MODULE_AUTHOR("Hans Petter Selasky");
1286 MODULE_DESCRIPTION("USB CDC NCM host driver");
1287 MODULE_LICENSE("Dual BSD/GPL");