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 "04-Aug-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
;
139 static void cdc_ncm_tx_timeout(unsigned long arg
);
140 static const struct driver_info cdc_ncm_info
;
141 static struct usb_driver cdc_ncm_driver
;
142 static struct ethtool_ops cdc_ncm_ethtool_ops
;
144 static const struct usb_device_id cdc_devs
[] = {
145 { USB_INTERFACE_INFO(USB_CLASS_COMM
,
146 USB_CDC_SUBCLASS_NCM
, USB_CDC_PROTO_NONE
),
147 .driver_info
= (unsigned long)&cdc_ncm_info
,
153 MODULE_DEVICE_TABLE(usb
, cdc_devs
);
156 cdc_ncm_get_drvinfo(struct net_device
*net
, struct ethtool_drvinfo
*info
)
158 struct usbnet
*dev
= netdev_priv(net
);
160 strncpy(info
->driver
, dev
->driver_name
, sizeof(info
->driver
));
161 strncpy(info
->version
, DRIVER_VERSION
, sizeof(info
->version
));
162 strncpy(info
->fw_version
, dev
->driver_info
->description
,
163 sizeof(info
->fw_version
));
164 usb_make_path(dev
->udev
, info
->bus_info
, sizeof(info
->bus_info
));
167 static u8
cdc_ncm_setup(struct cdc_ncm_ctx
*ctx
)
173 u16 ntb_fmt_supported
;
175 iface_no
= ctx
->control
->cur_altsetting
->desc
.bInterfaceNumber
;
177 err
= usb_control_msg(ctx
->udev
,
178 usb_rcvctrlpipe(ctx
->udev
, 0),
179 USB_CDC_GET_NTB_PARAMETERS
,
180 USB_TYPE_CLASS
| USB_DIR_IN
181 | USB_RECIP_INTERFACE
,
182 0, iface_no
, &ctx
->ncm_parm
,
183 sizeof(ctx
->ncm_parm
), 10000);
185 pr_debug("failed GET_NTB_PARAMETERS\n");
189 /* read correct set of parameters according to device mode */
190 ctx
->rx_max
= le32_to_cpu(ctx
->ncm_parm
.dwNtbInMaxSize
);
191 ctx
->tx_max
= le32_to_cpu(ctx
->ncm_parm
.dwNtbOutMaxSize
);
192 ctx
->tx_remainder
= le16_to_cpu(ctx
->ncm_parm
.wNdpOutPayloadRemainder
);
193 ctx
->tx_modulus
= le16_to_cpu(ctx
->ncm_parm
.wNdpOutDivisor
);
194 ctx
->tx_ndp_modulus
= le16_to_cpu(ctx
->ncm_parm
.wNdpOutAlignment
);
195 /* devices prior to NCM Errata shall set this field to zero */
196 ctx
->tx_max_datagrams
= le16_to_cpu(ctx
->ncm_parm
.wNtbOutMaxDatagrams
);
197 ntb_fmt_supported
= le16_to_cpu(ctx
->ncm_parm
.bmNtbFormatsSupported
);
199 if (ctx
->func_desc
!= NULL
)
200 flags
= ctx
->func_desc
->bmNetworkCapabilities
;
204 pr_debug("dwNtbInMaxSize=%u dwNtbOutMaxSize=%u "
205 "wNdpOutPayloadRemainder=%u wNdpOutDivisor=%u "
206 "wNdpOutAlignment=%u wNtbOutMaxDatagrams=%u flags=0x%x\n",
207 ctx
->rx_max
, ctx
->tx_max
, ctx
->tx_remainder
, ctx
->tx_modulus
,
208 ctx
->tx_ndp_modulus
, ctx
->tx_max_datagrams
, flags
);
210 /* max count of tx datagrams */
211 if ((ctx
->tx_max_datagrams
== 0) ||
212 (ctx
->tx_max_datagrams
> CDC_NCM_DPT_DATAGRAMS_MAX
))
213 ctx
->tx_max_datagrams
= CDC_NCM_DPT_DATAGRAMS_MAX
;
215 /* verify maximum size of received NTB in bytes */
216 if (ctx
->rx_max
< USB_CDC_NCM_NTB_MIN_IN_SIZE
) {
217 pr_debug("Using min receive length=%d\n",
218 USB_CDC_NCM_NTB_MIN_IN_SIZE
);
219 ctx
->rx_max
= USB_CDC_NCM_NTB_MIN_IN_SIZE
;
222 if (ctx
->rx_max
> CDC_NCM_NTB_MAX_SIZE_RX
) {
223 pr_debug("Using default maximum receive length=%d\n",
224 CDC_NCM_NTB_MAX_SIZE_RX
);
225 ctx
->rx_max
= CDC_NCM_NTB_MAX_SIZE_RX
;
228 /* inform device about NTB input size changes */
229 if (ctx
->rx_max
!= le32_to_cpu(ctx
->ncm_parm
.dwNtbInMaxSize
)) {
231 if (flags
& USB_CDC_NCM_NCAP_NTB_INPUT_SIZE
) {
232 struct usb_cdc_ncm_ndp_input_size
*ndp_in_sz
;
234 ndp_in_sz
= kzalloc(sizeof(*ndp_in_sz
), GFP_KERNEL
);
240 err
= usb_control_msg(ctx
->udev
,
241 usb_sndctrlpipe(ctx
->udev
, 0),
242 USB_CDC_SET_NTB_INPUT_SIZE
,
243 USB_TYPE_CLASS
| USB_DIR_OUT
244 | USB_RECIP_INTERFACE
,
245 0, iface_no
, ndp_in_sz
, 8, 1000);
248 __le32
*dwNtbInMaxSize
;
249 dwNtbInMaxSize
= kzalloc(sizeof(*dwNtbInMaxSize
),
251 if (!dwNtbInMaxSize
) {
255 *dwNtbInMaxSize
= cpu_to_le32(ctx
->rx_max
);
257 err
= usb_control_msg(ctx
->udev
,
258 usb_sndctrlpipe(ctx
->udev
, 0),
259 USB_CDC_SET_NTB_INPUT_SIZE
,
260 USB_TYPE_CLASS
| USB_DIR_OUT
261 | USB_RECIP_INTERFACE
,
262 0, iface_no
, dwNtbInMaxSize
, 4, 1000);
263 kfree(dwNtbInMaxSize
);
267 pr_debug("Setting NTB Input Size failed\n");
270 /* verify maximum size of transmitted NTB in bytes */
272 (CDC_NCM_MIN_HDR_SIZE
+ CDC_NCM_MIN_DATAGRAM_SIZE
)) ||
273 (ctx
->tx_max
> CDC_NCM_NTB_MAX_SIZE_TX
)) {
274 pr_debug("Using default maximum transmit length=%d\n",
275 CDC_NCM_NTB_MAX_SIZE_TX
);
276 ctx
->tx_max
= CDC_NCM_NTB_MAX_SIZE_TX
;
280 * verify that the structure alignment is:
282 * - not greater than the maximum transmit length
283 * - not less than four bytes
285 val
= ctx
->tx_ndp_modulus
;
287 if ((val
< USB_CDC_NCM_NDP_ALIGN_MIN_SIZE
) ||
288 (val
!= ((-val
) & val
)) || (val
>= ctx
->tx_max
)) {
289 pr_debug("Using default alignment: 4 bytes\n");
290 ctx
->tx_ndp_modulus
= USB_CDC_NCM_NDP_ALIGN_MIN_SIZE
;
294 * verify that the payload alignment is:
296 * - not greater than the maximum transmit length
297 * - not less than four bytes
299 val
= ctx
->tx_modulus
;
301 if ((val
< USB_CDC_NCM_NDP_ALIGN_MIN_SIZE
) ||
302 (val
!= ((-val
) & val
)) || (val
>= ctx
->tx_max
)) {
303 pr_debug("Using default transmit modulus: 4 bytes\n");
304 ctx
->tx_modulus
= USB_CDC_NCM_NDP_ALIGN_MIN_SIZE
;
307 /* verify the payload remainder */
308 if (ctx
->tx_remainder
>= ctx
->tx_modulus
) {
309 pr_debug("Using default transmit remainder: 0 bytes\n");
310 ctx
->tx_remainder
= 0;
313 /* adjust TX-remainder according to NCM specification. */
314 ctx
->tx_remainder
= ((ctx
->tx_remainder
- ETH_HLEN
) &
315 (ctx
->tx_modulus
- 1));
317 /* additional configuration */
320 if (flags
& USB_CDC_NCM_NCAP_CRC_MODE
) {
321 err
= usb_control_msg(ctx
->udev
, usb_sndctrlpipe(ctx
->udev
, 0),
322 USB_CDC_SET_CRC_MODE
,
323 USB_TYPE_CLASS
| USB_DIR_OUT
324 | USB_RECIP_INTERFACE
,
325 USB_CDC_NCM_CRC_NOT_APPENDED
,
326 iface_no
, NULL
, 0, 1000);
328 pr_debug("Setting CRC mode off failed\n");
331 /* set NTB format, if both formats are supported */
332 if (ntb_fmt_supported
& USB_CDC_NCM_NTH32_SIGN
) {
333 err
= usb_control_msg(ctx
->udev
, usb_sndctrlpipe(ctx
->udev
, 0),
334 USB_CDC_SET_NTB_FORMAT
, USB_TYPE_CLASS
335 | USB_DIR_OUT
| USB_RECIP_INTERFACE
,
336 USB_CDC_NCM_NTB16_FORMAT
,
337 iface_no
, NULL
, 0, 1000);
339 pr_debug("Setting NTB format to 16-bit failed\n");
342 ctx
->max_datagram_size
= CDC_NCM_MIN_DATAGRAM_SIZE
;
344 /* set Max Datagram Size (MTU) */
345 if (flags
& USB_CDC_NCM_NCAP_MAX_DATAGRAM_SIZE
) {
346 __le16
*max_datagram_size
;
347 u16 eth_max_sz
= le16_to_cpu(ctx
->ether_desc
->wMaxSegmentSize
);
349 max_datagram_size
= kzalloc(sizeof(*max_datagram_size
),
351 if (!max_datagram_size
) {
356 err
= usb_control_msg(ctx
->udev
, usb_rcvctrlpipe(ctx
->udev
, 0),
357 USB_CDC_GET_MAX_DATAGRAM_SIZE
,
358 USB_TYPE_CLASS
| USB_DIR_IN
359 | USB_RECIP_INTERFACE
,
360 0, iface_no
, max_datagram_size
,
363 pr_debug("GET_MAX_DATAGRAM_SIZE failed, use size=%u\n",
364 CDC_NCM_MIN_DATAGRAM_SIZE
);
365 kfree(max_datagram_size
);
367 ctx
->max_datagram_size
=
368 le16_to_cpu(*max_datagram_size
);
369 /* Check Eth descriptor value */
370 if (eth_max_sz
< CDC_NCM_MAX_DATAGRAM_SIZE
) {
371 if (ctx
->max_datagram_size
> eth_max_sz
)
372 ctx
->max_datagram_size
= eth_max_sz
;
374 if (ctx
->max_datagram_size
>
375 CDC_NCM_MAX_DATAGRAM_SIZE
)
376 ctx
->max_datagram_size
=
377 CDC_NCM_MAX_DATAGRAM_SIZE
;
380 if (ctx
->max_datagram_size
< CDC_NCM_MIN_DATAGRAM_SIZE
)
381 ctx
->max_datagram_size
=
382 CDC_NCM_MIN_DATAGRAM_SIZE
;
384 /* if value changed, update device */
385 err
= usb_control_msg(ctx
->udev
,
386 usb_sndctrlpipe(ctx
->udev
, 0),
387 USB_CDC_SET_MAX_DATAGRAM_SIZE
,
388 USB_TYPE_CLASS
| USB_DIR_OUT
389 | USB_RECIP_INTERFACE
,
391 iface_no
, max_datagram_size
,
393 kfree(max_datagram_size
);
396 pr_debug("SET_MAX_DATAGRAM_SIZE failed\n");
401 if (ctx
->netdev
->mtu
!= (ctx
->max_datagram_size
- ETH_HLEN
))
402 ctx
->netdev
->mtu
= ctx
->max_datagram_size
- ETH_HLEN
;
408 cdc_ncm_find_endpoints(struct cdc_ncm_ctx
*ctx
, struct usb_interface
*intf
)
410 struct usb_host_endpoint
*e
;
413 for (ep
= 0; ep
< intf
->cur_altsetting
->desc
.bNumEndpoints
; ep
++) {
415 e
= intf
->cur_altsetting
->endpoint
+ ep
;
416 switch (e
->desc
.bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) {
417 case USB_ENDPOINT_XFER_INT
:
418 if (usb_endpoint_dir_in(&e
->desc
)) {
419 if (ctx
->status_ep
== NULL
)
424 case USB_ENDPOINT_XFER_BULK
:
425 if (usb_endpoint_dir_in(&e
->desc
)) {
426 if (ctx
->in_ep
== NULL
)
429 if (ctx
->out_ep
== NULL
)
440 static void cdc_ncm_free(struct cdc_ncm_ctx
*ctx
)
445 del_timer_sync(&ctx
->tx_timer
);
447 if (ctx
->tx_rem_skb
!= NULL
) {
448 dev_kfree_skb_any(ctx
->tx_rem_skb
);
449 ctx
->tx_rem_skb
= NULL
;
452 if (ctx
->tx_curr_skb
!= NULL
) {
453 dev_kfree_skb_any(ctx
->tx_curr_skb
);
454 ctx
->tx_curr_skb
= NULL
;
460 static int cdc_ncm_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
462 struct cdc_ncm_ctx
*ctx
;
463 struct usb_driver
*driver
;
469 ctx
= kmalloc(sizeof(*ctx
), GFP_KERNEL
);
473 memset(ctx
, 0, sizeof(*ctx
));
475 init_timer(&ctx
->tx_timer
);
476 spin_lock_init(&ctx
->mtx
);
477 ctx
->netdev
= dev
->net
;
479 /* store ctx pointer in device data field */
480 dev
->data
[0] = (unsigned long)ctx
;
482 /* get some pointers */
483 driver
= driver_of(intf
);
484 buf
= intf
->cur_altsetting
->extra
;
485 len
= intf
->cur_altsetting
->extralen
;
487 ctx
->udev
= dev
->udev
;
490 /* parse through descriptors associated with control interface */
491 while ((len
> 0) && (buf
[0] > 2) && (buf
[0] <= len
)) {
493 if (buf
[1] != USB_DT_CS_INTERFACE
)
497 case USB_CDC_UNION_TYPE
:
498 if (buf
[0] < sizeof(*(ctx
->union_desc
)))
502 (const struct usb_cdc_union_desc
*)buf
;
504 ctx
->control
= usb_ifnum_to_if(dev
->udev
,
505 ctx
->union_desc
->bMasterInterface0
);
506 ctx
->data
= usb_ifnum_to_if(dev
->udev
,
507 ctx
->union_desc
->bSlaveInterface0
);
510 case USB_CDC_ETHERNET_TYPE
:
511 if (buf
[0] < sizeof(*(ctx
->ether_desc
)))
515 (const struct usb_cdc_ether_desc
*)buf
;
517 le16_to_cpu(ctx
->ether_desc
->wMaxSegmentSize
);
519 if (dev
->hard_mtu
< CDC_NCM_MIN_DATAGRAM_SIZE
)
520 dev
->hard_mtu
= CDC_NCM_MIN_DATAGRAM_SIZE
;
521 else if (dev
->hard_mtu
> CDC_NCM_MAX_DATAGRAM_SIZE
)
522 dev
->hard_mtu
= CDC_NCM_MAX_DATAGRAM_SIZE
;
525 case USB_CDC_NCM_TYPE
:
526 if (buf
[0] < sizeof(*(ctx
->func_desc
)))
529 ctx
->func_desc
= (const struct usb_cdc_ncm_desc
*)buf
;
536 /* advance to next descriptor */
542 /* check if we got everything */
543 if ((ctx
->control
== NULL
) || (ctx
->data
== NULL
) ||
544 (ctx
->ether_desc
== NULL
) || (ctx
->control
!= intf
))
547 /* claim interfaces, if any */
548 temp
= usb_driver_claim_interface(driver
, ctx
->data
, dev
);
552 iface_no
= ctx
->data
->cur_altsetting
->desc
.bInterfaceNumber
;
554 /* reset data interface */
555 temp
= usb_set_interface(dev
->udev
, iface_no
, 0);
559 /* initialize data interface */
560 if (cdc_ncm_setup(ctx
))
563 /* configure data interface */
564 temp
= usb_set_interface(dev
->udev
, iface_no
, 1);
568 cdc_ncm_find_endpoints(ctx
, ctx
->data
);
569 cdc_ncm_find_endpoints(ctx
, ctx
->control
);
571 if ((ctx
->in_ep
== NULL
) || (ctx
->out_ep
== NULL
) ||
572 (ctx
->status_ep
== NULL
))
575 dev
->net
->ethtool_ops
= &cdc_ncm_ethtool_ops
;
577 usb_set_intfdata(ctx
->data
, dev
);
578 usb_set_intfdata(ctx
->control
, dev
);
579 usb_set_intfdata(ctx
->intf
, dev
);
581 temp
= usbnet_get_ethernet_addr(dev
, ctx
->ether_desc
->iMACAddress
);
585 dev_info(&dev
->udev
->dev
, "MAC-Address: "
586 "0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x\n",
587 dev
->net
->dev_addr
[0], dev
->net
->dev_addr
[1],
588 dev
->net
->dev_addr
[2], dev
->net
->dev_addr
[3],
589 dev
->net
->dev_addr
[4], dev
->net
->dev_addr
[5]);
591 dev
->in
= usb_rcvbulkpipe(dev
->udev
,
592 ctx
->in_ep
->desc
.bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
593 dev
->out
= usb_sndbulkpipe(dev
->udev
,
594 ctx
->out_ep
->desc
.bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
595 dev
->status
= ctx
->status_ep
;
596 dev
->rx_urb_size
= ctx
->rx_max
;
599 * We should get an event when network connection is "connected" or
600 * "disconnected". Set network connection in "disconnected" state
601 * (carrier is OFF) during attach, so the IP network stack does not
602 * start IPv6 negotiation and more.
604 netif_carrier_off(dev
->net
);
605 ctx
->tx_speed
= ctx
->rx_speed
= 0;
609 usb_set_intfdata(ctx
->control
, NULL
);
610 usb_set_intfdata(ctx
->data
, NULL
);
611 usb_driver_release_interface(driver
, ctx
->data
);
613 cdc_ncm_free((struct cdc_ncm_ctx
*)dev
->data
[0]);
615 dev_info(&dev
->udev
->dev
, "bind() failure\n");
619 static void cdc_ncm_unbind(struct usbnet
*dev
, struct usb_interface
*intf
)
621 struct cdc_ncm_ctx
*ctx
= (struct cdc_ncm_ctx
*)dev
->data
[0];
622 struct usb_driver
*driver
= driver_of(intf
);
625 return; /* no setup */
627 /* disconnect master --> disconnect slave */
628 if (intf
== ctx
->control
&& ctx
->data
) {
629 usb_set_intfdata(ctx
->data
, NULL
);
630 usb_driver_release_interface(driver
, ctx
->data
);
633 } else if (intf
== ctx
->data
&& ctx
->control
) {
634 usb_set_intfdata(ctx
->control
, NULL
);
635 usb_driver_release_interface(driver
, ctx
->control
);
639 usb_set_intfdata(ctx
->intf
, NULL
);
643 static void cdc_ncm_zero_fill(u8
*ptr
, u32 first
, u32 end
, u32 max
)
651 memset(ptr
+ first
, 0, end
- first
);
654 static struct sk_buff
*
655 cdc_ncm_fill_tx_frame(struct cdc_ncm_ctx
*ctx
, struct sk_buff
*skb
)
657 struct sk_buff
*skb_out
;
664 /* if there is a remaining skb, it gets priority */
666 swap(skb
, ctx
->tx_rem_skb
);
678 /* check if we are resuming an OUT skb */
679 if (ctx
->tx_curr_skb
!= NULL
) {
681 skb_out
= ctx
->tx_curr_skb
;
682 offset
= ctx
->tx_curr_offset
;
683 last_offset
= ctx
->tx_curr_last_offset
;
684 n
= ctx
->tx_curr_frame_num
;
687 /* reset variables */
688 skb_out
= alloc_skb((ctx
->tx_max
+ 1), GFP_ATOMIC
);
689 if (skb_out
== NULL
) {
691 dev_kfree_skb_any(skb
);
692 ctx
->netdev
->stats
.tx_dropped
++;
697 /* make room for NTH and NDP */
698 offset
= ALIGN(sizeof(struct usb_cdc_ncm_nth16
),
699 ctx
->tx_ndp_modulus
) +
700 sizeof(struct usb_cdc_ncm_ndp16
) +
701 (ctx
->tx_max_datagrams
+ 1) *
702 sizeof(struct usb_cdc_ncm_dpe16
);
704 /* store last valid offset before alignment */
705 last_offset
= offset
;
706 /* align first Datagram offset correctly */
707 offset
= ALIGN(offset
, ctx
->tx_modulus
) + ctx
->tx_remainder
;
708 /* zero buffer till the first IP datagram */
709 cdc_ncm_zero_fill(skb_out
->data
, 0, offset
, offset
);
711 ctx
->tx_curr_frame_num
= 0;
714 for (; n
< ctx
->tx_max_datagrams
; n
++) {
715 /* check if end of transmit buffer is reached */
716 if (offset
>= ctx
->tx_max
) {
720 /* compute maximum buffer size */
721 rem
= ctx
->tx_max
- offset
;
724 skb
= ctx
->tx_rem_skb
;
725 ctx
->tx_rem_skb
= NULL
;
727 /* check for end of skb */
732 if (skb
->len
> rem
) {
734 /* won't fit, MTU problem? */
735 dev_kfree_skb_any(skb
);
737 ctx
->netdev
->stats
.tx_dropped
++;
739 /* no room for skb - store for later */
740 if (ctx
->tx_rem_skb
!= NULL
) {
741 dev_kfree_skb_any(ctx
->tx_rem_skb
);
742 ctx
->netdev
->stats
.tx_dropped
++;
744 ctx
->tx_rem_skb
= skb
;
751 memcpy(((u8
*)skb_out
->data
) + offset
, skb
->data
, skb
->len
);
753 ctx
->tx_ncm
.dpe16
[n
].wDatagramLength
= cpu_to_le16(skb
->len
);
754 ctx
->tx_ncm
.dpe16
[n
].wDatagramIndex
= cpu_to_le16(offset
);
759 /* store last valid offset before alignment */
760 last_offset
= offset
;
762 /* align offset correctly */
763 offset
= ALIGN(offset
, ctx
->tx_modulus
) + ctx
->tx_remainder
;
766 cdc_ncm_zero_fill(skb_out
->data
, last_offset
, offset
,
768 dev_kfree_skb_any(skb
);
772 /* free up any dangling skb */
774 dev_kfree_skb_any(skb
);
776 ctx
->netdev
->stats
.tx_dropped
++;
779 ctx
->tx_curr_frame_num
= n
;
782 /* wait for more frames */
784 ctx
->tx_curr_skb
= skb_out
;
785 ctx
->tx_curr_offset
= offset
;
786 ctx
->tx_curr_last_offset
= last_offset
;
789 } else if ((n
< ctx
->tx_max_datagrams
) && (ready2send
== 0)) {
790 /* wait for more frames */
792 ctx
->tx_curr_skb
= skb_out
;
793 ctx
->tx_curr_offset
= offset
;
794 ctx
->tx_curr_last_offset
= last_offset
;
795 /* set the pending count */
796 if (n
< CDC_NCM_RESTART_TIMER_DATAGRAM_CNT
)
797 ctx
->tx_timer_pending
= 2;
802 /* variables will be reset at next call */
805 /* check for overflow */
806 if (last_offset
> ctx
->tx_max
)
807 last_offset
= ctx
->tx_max
;
810 offset
= last_offset
;
813 * If collected data size is less or equal CDC_NCM_MIN_TX_PKT bytes,
814 * we send buffers as it is. If we get more data, it would be more
815 * efficient for USB HS mobile device with DMA engine to receive a full
816 * size NTB, than canceling DMA transfer and receiving a short packet.
818 if (offset
> CDC_NCM_MIN_TX_PKT
)
819 offset
= ctx
->tx_max
;
821 /* final zero padding */
822 cdc_ncm_zero_fill(skb_out
->data
, last_offset
, offset
, ctx
->tx_max
);
824 /* store last offset */
825 last_offset
= offset
;
827 if (((last_offset
< ctx
->tx_max
) && ((last_offset
%
828 le16_to_cpu(ctx
->out_ep
->desc
.wMaxPacketSize
)) == 0)) ||
829 (((last_offset
== ctx
->tx_max
) && ((ctx
->tx_max
%
830 le16_to_cpu(ctx
->out_ep
->desc
.wMaxPacketSize
)) == 0)) &&
831 (ctx
->tx_max
< le32_to_cpu(ctx
->ncm_parm
.dwNtbOutMaxSize
)))) {
832 /* force short packet */
833 *(((u8
*)skb_out
->data
) + last_offset
) = 0;
837 /* zero the rest of the DPEs plus the last NULL entry */
838 for (; n
<= CDC_NCM_DPT_DATAGRAMS_MAX
; n
++) {
839 ctx
->tx_ncm
.dpe16
[n
].wDatagramLength
= 0;
840 ctx
->tx_ncm
.dpe16
[n
].wDatagramIndex
= 0;
843 /* fill out 16-bit NTB header */
844 ctx
->tx_ncm
.nth16
.dwSignature
= cpu_to_le32(USB_CDC_NCM_NTH16_SIGN
);
845 ctx
->tx_ncm
.nth16
.wHeaderLength
=
846 cpu_to_le16(sizeof(ctx
->tx_ncm
.nth16
));
847 ctx
->tx_ncm
.nth16
.wSequence
= cpu_to_le16(ctx
->tx_seq
);
848 ctx
->tx_ncm
.nth16
.wBlockLength
= cpu_to_le16(last_offset
);
849 index
= ALIGN(sizeof(struct usb_cdc_ncm_nth16
), ctx
->tx_ndp_modulus
);
850 ctx
->tx_ncm
.nth16
.wNdpIndex
= cpu_to_le16(index
);
852 memcpy(skb_out
->data
, &(ctx
->tx_ncm
.nth16
), sizeof(ctx
->tx_ncm
.nth16
));
855 /* fill out 16-bit NDP table */
856 ctx
->tx_ncm
.ndp16
.dwSignature
=
857 cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN
);
858 rem
= sizeof(ctx
->tx_ncm
.ndp16
) + ((ctx
->tx_curr_frame_num
+ 1) *
859 sizeof(struct usb_cdc_ncm_dpe16
));
860 ctx
->tx_ncm
.ndp16
.wLength
= cpu_to_le16(rem
);
861 ctx
->tx_ncm
.ndp16
.wNextNdpIndex
= 0; /* reserved */
863 memcpy(((u8
*)skb_out
->data
) + index
,
864 &(ctx
->tx_ncm
.ndp16
),
865 sizeof(ctx
->tx_ncm
.ndp16
));
867 memcpy(((u8
*)skb_out
->data
) + index
+ sizeof(ctx
->tx_ncm
.ndp16
),
868 &(ctx
->tx_ncm
.dpe16
),
869 (ctx
->tx_curr_frame_num
+ 1) *
870 sizeof(struct usb_cdc_ncm_dpe16
));
872 /* set frame length */
873 skb_put(skb_out
, last_offset
);
876 ctx
->tx_curr_skb
= NULL
;
883 static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx
*ctx
)
885 /* start timer, if not already started */
886 if (timer_pending(&ctx
->tx_timer
) == 0) {
887 ctx
->tx_timer
.function
= &cdc_ncm_tx_timeout
;
888 ctx
->tx_timer
.data
= (unsigned long)ctx
;
889 ctx
->tx_timer
.expires
= jiffies
+ ((HZ
+ 999) / 1000);
890 add_timer(&ctx
->tx_timer
);
894 static void cdc_ncm_tx_timeout(unsigned long arg
)
896 struct cdc_ncm_ctx
*ctx
= (struct cdc_ncm_ctx
*)arg
;
899 spin_lock(&ctx
->mtx
);
900 if (ctx
->tx_timer_pending
!= 0) {
901 ctx
->tx_timer_pending
--;
907 spin_unlock(&ctx
->mtx
);
910 spin_lock(&ctx
->mtx
);
911 cdc_ncm_tx_timeout_start(ctx
);
912 spin_unlock(&ctx
->mtx
);
913 } else if (ctx
->netdev
!= NULL
) {
914 usbnet_start_xmit(NULL
, ctx
->netdev
);
918 static struct sk_buff
*
919 cdc_ncm_tx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
, gfp_t flags
)
921 struct sk_buff
*skb_out
;
922 struct cdc_ncm_ctx
*ctx
= (struct cdc_ncm_ctx
*)dev
->data
[0];
926 * The Ethernet API we are using does not support transmitting
927 * multiple Ethernet frames in a single call. This driver will
928 * accumulate multiple Ethernet frames and send out a larger
929 * USB frame when the USB buffer is full or when a single jiffies
935 spin_lock(&ctx
->mtx
);
936 skb_out
= cdc_ncm_fill_tx_frame(ctx
, skb
);
937 if (ctx
->tx_curr_skb
!= NULL
)
940 /* Start timer, if there is a remaining skb */
942 cdc_ncm_tx_timeout_start(ctx
);
945 dev
->net
->stats
.tx_packets
+= ctx
->tx_curr_frame_num
;
947 spin_unlock(&ctx
->mtx
);
952 dev_kfree_skb_any(skb
);
957 static int cdc_ncm_rx_fixup(struct usbnet
*dev
, struct sk_buff
*skb_in
)
960 struct cdc_ncm_ctx
*ctx
;
968 ctx
= (struct cdc_ncm_ctx
*)dev
->data
[0];
972 actlen
= skb_in
->len
;
973 sumlen
= CDC_NCM_NTB_MAX_SIZE_RX
;
975 if (actlen
< (sizeof(ctx
->rx_ncm
.nth16
) + sizeof(ctx
->rx_ncm
.ndp16
))) {
976 pr_debug("frame too short\n");
980 memcpy(&(ctx
->rx_ncm
.nth16
), ((u8
*)skb_in
->data
),
981 sizeof(ctx
->rx_ncm
.nth16
));
983 if (le32_to_cpu(ctx
->rx_ncm
.nth16
.dwSignature
) !=
984 USB_CDC_NCM_NTH16_SIGN
) {
985 pr_debug("invalid NTH16 signature <%u>\n",
986 le32_to_cpu(ctx
->rx_ncm
.nth16
.dwSignature
));
990 temp
= le16_to_cpu(ctx
->rx_ncm
.nth16
.wBlockLength
);
992 pr_debug("unsupported NTB block length %u/%u\n", temp
, sumlen
);
996 temp
= le16_to_cpu(ctx
->rx_ncm
.nth16
.wNdpIndex
);
997 if ((temp
+ sizeof(ctx
->rx_ncm
.ndp16
)) > actlen
) {
998 pr_debug("invalid DPT16 index\n");
1002 memcpy(&(ctx
->rx_ncm
.ndp16
), ((u8
*)skb_in
->data
) + temp
,
1003 sizeof(ctx
->rx_ncm
.ndp16
));
1005 if (le32_to_cpu(ctx
->rx_ncm
.ndp16
.dwSignature
) !=
1006 USB_CDC_NCM_NDP16_NOCRC_SIGN
) {
1007 pr_debug("invalid DPT16 signature <%u>\n",
1008 le32_to_cpu(ctx
->rx_ncm
.ndp16
.dwSignature
));
1012 if (le16_to_cpu(ctx
->rx_ncm
.ndp16
.wLength
) <
1013 USB_CDC_NCM_NDP16_LENGTH_MIN
) {
1014 pr_debug("invalid DPT16 length <%u>\n",
1015 le32_to_cpu(ctx
->rx_ncm
.ndp16
.dwSignature
));
1019 nframes
= ((le16_to_cpu(ctx
->rx_ncm
.ndp16
.wLength
) -
1020 sizeof(struct usb_cdc_ncm_ndp16
)) /
1021 sizeof(struct usb_cdc_ncm_dpe16
));
1022 nframes
--; /* we process NDP entries except for the last one */
1024 pr_debug("nframes = %u\n", nframes
);
1026 temp
+= sizeof(ctx
->rx_ncm
.ndp16
);
1028 if ((temp
+ nframes
* (sizeof(struct usb_cdc_ncm_dpe16
))) > actlen
) {
1029 pr_debug("Invalid nframes = %d\n", nframes
);
1033 if (nframes
> CDC_NCM_DPT_DATAGRAMS_MAX
) {
1034 pr_debug("Truncating number of frames from %u to %u\n",
1035 nframes
, CDC_NCM_DPT_DATAGRAMS_MAX
);
1036 nframes
= CDC_NCM_DPT_DATAGRAMS_MAX
;
1039 memcpy(&(ctx
->rx_ncm
.dpe16
), ((u8
*)skb_in
->data
) + temp
,
1040 nframes
* (sizeof(struct usb_cdc_ncm_dpe16
)));
1042 for (x
= 0; x
< nframes
; x
++) {
1043 offset
= le16_to_cpu(ctx
->rx_ncm
.dpe16
[x
].wDatagramIndex
);
1044 temp
= le16_to_cpu(ctx
->rx_ncm
.dpe16
[x
].wDatagramLength
);
1048 * All entries after first NULL entry are to be ignored
1050 if ((offset
== 0) || (temp
== 0)) {
1052 goto error
; /* empty NTB */
1056 /* sanity checking */
1057 if (((offset
+ temp
) > actlen
) ||
1058 (temp
> CDC_NCM_MAX_DATAGRAM_SIZE
) || (temp
< ETH_HLEN
)) {
1059 pr_debug("invalid frame detected (ignored)"
1060 "offset[%u]=%u, length=%u, skb=%p\n",
1061 x
, offset
, temp
, skb_in
);
1067 skb
= skb_clone(skb_in
, GFP_ATOMIC
);
1071 skb
->data
= ((u8
*)skb_in
->data
) + offset
;
1072 skb_set_tail_pointer(skb
, temp
);
1073 usbnet_skb_return(dev
, skb
);
1082 cdc_ncm_speed_change(struct cdc_ncm_ctx
*ctx
,
1083 struct usb_cdc_speed_change
*data
)
1085 uint32_t rx_speed
= le32_to_cpu(data
->DLBitRRate
);
1086 uint32_t tx_speed
= le32_to_cpu(data
->ULBitRate
);
1089 * Currently the USB-NET API does not support reporting the actual
1090 * device speed. Do print it instead.
1092 if ((tx_speed
!= ctx
->tx_speed
) || (rx_speed
!= ctx
->rx_speed
)) {
1093 ctx
->tx_speed
= tx_speed
;
1094 ctx
->rx_speed
= rx_speed
;
1096 if ((tx_speed
> 1000000) && (rx_speed
> 1000000)) {
1097 printk(KERN_INFO KBUILD_MODNAME
1098 ": %s: %u mbit/s downlink "
1099 "%u mbit/s uplink\n",
1101 (unsigned int)(rx_speed
/ 1000000U),
1102 (unsigned int)(tx_speed
/ 1000000U));
1104 printk(KERN_INFO KBUILD_MODNAME
1105 ": %s: %u kbit/s downlink "
1106 "%u kbit/s uplink\n",
1108 (unsigned int)(rx_speed
/ 1000U),
1109 (unsigned int)(tx_speed
/ 1000U));
1114 static void cdc_ncm_status(struct usbnet
*dev
, struct urb
*urb
)
1116 struct cdc_ncm_ctx
*ctx
;
1117 struct usb_cdc_notification
*event
;
1119 ctx
= (struct cdc_ncm_ctx
*)dev
->data
[0];
1121 if (urb
->actual_length
< sizeof(*event
))
1124 /* test for split data in 8-byte chunks */
1125 if (test_and_clear_bit(EVENT_STS_SPLIT
, &dev
->flags
)) {
1126 cdc_ncm_speed_change(ctx
,
1127 (struct usb_cdc_speed_change
*)urb
->transfer_buffer
);
1131 event
= urb
->transfer_buffer
;
1133 switch (event
->bNotificationType
) {
1134 case USB_CDC_NOTIFY_NETWORK_CONNECTION
:
1136 * According to the CDC NCM specification ch.7.1
1137 * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be
1138 * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE.
1140 ctx
->connected
= event
->wValue
;
1142 printk(KERN_INFO KBUILD_MODNAME
": %s: network connection:"
1144 ctx
->netdev
->name
, ctx
->connected
? "" : "dis");
1147 netif_carrier_on(dev
->net
);
1149 netif_carrier_off(dev
->net
);
1150 ctx
->tx_speed
= ctx
->rx_speed
= 0;
1154 case USB_CDC_NOTIFY_SPEED_CHANGE
:
1155 if (urb
->actual_length
< (sizeof(*event
) +
1156 sizeof(struct usb_cdc_speed_change
)))
1157 set_bit(EVENT_STS_SPLIT
, &dev
->flags
);
1159 cdc_ncm_speed_change(ctx
,
1160 (struct usb_cdc_speed_change
*) &event
[1]);
1164 dev_err(&dev
->udev
->dev
, "NCM: unexpected "
1165 "notification 0x%02x!\n", event
->bNotificationType
);
1170 static int cdc_ncm_check_connect(struct usbnet
*dev
)
1172 struct cdc_ncm_ctx
*ctx
;
1174 ctx
= (struct cdc_ncm_ctx
*)dev
->data
[0];
1176 return 1; /* disconnected */
1178 return !ctx
->connected
;
1182 cdc_ncm_probe(struct usb_interface
*udev
, const struct usb_device_id
*prod
)
1184 return usbnet_probe(udev
, prod
);
1187 static void cdc_ncm_disconnect(struct usb_interface
*intf
)
1189 struct usbnet
*dev
= usb_get_intfdata(intf
);
1192 return; /* already disconnected */
1194 usbnet_disconnect(intf
);
1197 static int cdc_ncm_manage_power(struct usbnet
*dev
, int status
)
1199 dev
->intf
->needs_remote_wakeup
= status
;
1203 static const struct driver_info cdc_ncm_info
= {
1204 .description
= "CDC NCM",
1205 .flags
= FLAG_POINTTOPOINT
| FLAG_NO_SETINT
| FLAG_MULTI_PACKET
,
1206 .bind
= cdc_ncm_bind
,
1207 .unbind
= cdc_ncm_unbind
,
1208 .check_connect
= cdc_ncm_check_connect
,
1209 .manage_power
= cdc_ncm_manage_power
,
1210 .status
= cdc_ncm_status
,
1211 .rx_fixup
= cdc_ncm_rx_fixup
,
1212 .tx_fixup
= cdc_ncm_tx_fixup
,
1215 static struct usb_driver cdc_ncm_driver
= {
1217 .id_table
= cdc_devs
,
1218 .probe
= cdc_ncm_probe
,
1219 .disconnect
= cdc_ncm_disconnect
,
1220 .suspend
= usbnet_suspend
,
1221 .resume
= usbnet_resume
,
1222 .reset_resume
= usbnet_resume
,
1223 .supports_autosuspend
= 1,
1226 static struct ethtool_ops cdc_ncm_ethtool_ops
= {
1227 .get_drvinfo
= cdc_ncm_get_drvinfo
,
1228 .get_link
= usbnet_get_link
,
1229 .get_msglevel
= usbnet_get_msglevel
,
1230 .set_msglevel
= usbnet_set_msglevel
,
1231 .get_settings
= usbnet_get_settings
,
1232 .set_settings
= usbnet_set_settings
,
1233 .nway_reset
= usbnet_nway_reset
,
1236 static int __init
cdc_ncm_init(void)
1238 printk(KERN_INFO KBUILD_MODNAME
": " DRIVER_VERSION
"\n");
1239 return usb_register(&cdc_ncm_driver
);
1242 module_init(cdc_ncm_init
);
1244 static void __exit
cdc_ncm_exit(void)
1246 usb_deregister(&cdc_ncm_driver
);
1249 module_exit(cdc_ncm_exit
);
1251 MODULE_AUTHOR("Hans Petter Selasky");
1252 MODULE_DESCRIPTION("USB CDC NCM host driver");
1253 MODULE_LICENSE("Dual BSD/GPL");