4 * Copyright (C) ST-Ericsson 2010-2012
5 * Contact: Alexey Orishko <alexey.orishko@stericsson.com>
6 * Original author: Hans Petter Selasky <hans.petter.selasky@stericsson.com>
8 * USB Host Driver for Network Control Model (NCM)
9 * http://www.usb.org/developers/devclass_docs/NCM10.zip
11 * The NCM encoding, decoding and initialization logic
12 * derives from FreeBSD 8.x. if_cdce.c and if_cdcereg.h
14 * This software is available to you under a choice of one of two
15 * licenses. You may choose this file to be licensed under the terms
16 * of the GNU General Public License (GPL) Version 2 or the 2-clause
17 * BSD license listed below:
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 #include <linux/module.h>
42 #include <linux/init.h>
43 #include <linux/netdevice.h>
44 #include <linux/ctype.h>
45 #include <linux/ethtool.h>
46 #include <linux/workqueue.h>
47 #include <linux/mii.h>
48 #include <linux/crc32.h>
49 #include <linux/usb.h>
50 #include <linux/hrtimer.h>
51 #include <linux/atomic.h>
52 #include <linux/usb/usbnet.h>
53 #include <linux/usb/cdc.h>
55 #define DRIVER_VERSION "14-Mar-2012"
57 /* CDC NCM subclass 3.2.1 */
58 #define USB_CDC_NCM_NDP16_LENGTH_MIN 0x10
60 /* Maximum NTB length */
61 #define CDC_NCM_NTB_MAX_SIZE_TX 32768 /* bytes */
62 #define CDC_NCM_NTB_MAX_SIZE_RX 32768 /* bytes */
64 /* Minimum value for MaxDatagramSize, ch. 6.2.9 */
65 #define CDC_NCM_MIN_DATAGRAM_SIZE 1514 /* bytes */
67 #define CDC_NCM_MIN_TX_PKT 512 /* bytes */
69 /* Default value for MaxDatagramSize */
70 #define CDC_NCM_MAX_DATAGRAM_SIZE 8192 /* bytes */
73 * Maximum amount of datagrams in NCM Datagram Pointer Table, not counting
74 * the last NULL entry.
76 #define CDC_NCM_DPT_DATAGRAMS_MAX 40
78 /* Restart the timer, if amount of datagrams is less than given value */
79 #define CDC_NCM_RESTART_TIMER_DATAGRAM_CNT 3
80 #define CDC_NCM_TIMER_PENDING_CNT 2
81 #define CDC_NCM_TIMER_INTERVAL (400UL * NSEC_PER_USEC)
83 /* The following macro defines the minimum header space */
84 #define CDC_NCM_MIN_HDR_SIZE \
85 (sizeof(struct usb_cdc_ncm_nth16) + sizeof(struct usb_cdc_ncm_ndp16) + \
86 (CDC_NCM_DPT_DATAGRAMS_MAX + 1) * sizeof(struct usb_cdc_ncm_dpe16))
89 struct usb_cdc_ncm_nth16 nth16
;
90 struct usb_cdc_ncm_ndp16 ndp16
;
91 struct usb_cdc_ncm_dpe16 dpe16
[CDC_NCM_DPT_DATAGRAMS_MAX
+ 1];
95 struct cdc_ncm_data tx_ncm
;
96 struct usb_cdc_ncm_ntb_parameters ncm_parm
;
97 struct hrtimer tx_timer
;
98 struct tasklet_struct bh
;
100 const struct usb_cdc_ncm_desc
*func_desc
;
101 const struct usb_cdc_header_desc
*header_desc
;
102 const struct usb_cdc_union_desc
*union_desc
;
103 const struct usb_cdc_ether_desc
*ether_desc
;
105 struct net_device
*netdev
;
106 struct usb_device
*udev
;
107 struct usb_host_endpoint
*in_ep
;
108 struct usb_host_endpoint
*out_ep
;
109 struct usb_host_endpoint
*status_ep
;
110 struct usb_interface
*intf
;
111 struct usb_interface
*control
;
112 struct usb_interface
*data
;
114 struct sk_buff
*tx_curr_skb
;
115 struct sk_buff
*tx_rem_skb
;
120 u32 tx_timer_pending
;
122 u32 tx_curr_last_offset
;
123 u32 tx_curr_frame_num
;
128 u32 max_datagram_size
;
129 u16 tx_max_datagrams
;
138 static void cdc_ncm_txpath_bh(unsigned long param
);
139 static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx
*ctx
);
140 static enum hrtimer_restart
cdc_ncm_tx_timer_cb(struct hrtimer
*hr_timer
);
141 static struct usb_driver cdc_ncm_driver
;
144 cdc_ncm_get_drvinfo(struct net_device
*net
, struct ethtool_drvinfo
*info
)
146 struct usbnet
*dev
= netdev_priv(net
);
148 strncpy(info
->driver
, dev
->driver_name
, sizeof(info
->driver
));
149 strncpy(info
->version
, DRIVER_VERSION
, sizeof(info
->version
));
150 strncpy(info
->fw_version
, dev
->driver_info
->description
,
151 sizeof(info
->fw_version
));
152 usb_make_path(dev
->udev
, info
->bus_info
, sizeof(info
->bus_info
));
155 static u8
cdc_ncm_setup(struct cdc_ncm_ctx
*ctx
)
161 u16 ntb_fmt_supported
;
163 iface_no
= ctx
->control
->cur_altsetting
->desc
.bInterfaceNumber
;
165 err
= usb_control_msg(ctx
->udev
,
166 usb_rcvctrlpipe(ctx
->udev
, 0),
167 USB_CDC_GET_NTB_PARAMETERS
,
168 USB_TYPE_CLASS
| USB_DIR_IN
169 | USB_RECIP_INTERFACE
,
170 0, iface_no
, &ctx
->ncm_parm
,
171 sizeof(ctx
->ncm_parm
), 10000);
173 pr_debug("failed GET_NTB_PARAMETERS\n");
177 /* read correct set of parameters according to device mode */
178 ctx
->rx_max
= le32_to_cpu(ctx
->ncm_parm
.dwNtbInMaxSize
);
179 ctx
->tx_max
= le32_to_cpu(ctx
->ncm_parm
.dwNtbOutMaxSize
);
180 ctx
->tx_remainder
= le16_to_cpu(ctx
->ncm_parm
.wNdpOutPayloadRemainder
);
181 ctx
->tx_modulus
= le16_to_cpu(ctx
->ncm_parm
.wNdpOutDivisor
);
182 ctx
->tx_ndp_modulus
= le16_to_cpu(ctx
->ncm_parm
.wNdpOutAlignment
);
183 /* devices prior to NCM Errata shall set this field to zero */
184 ctx
->tx_max_datagrams
= le16_to_cpu(ctx
->ncm_parm
.wNtbOutMaxDatagrams
);
185 ntb_fmt_supported
= le16_to_cpu(ctx
->ncm_parm
.bmNtbFormatsSupported
);
187 if (ctx
->func_desc
!= NULL
)
188 flags
= ctx
->func_desc
->bmNetworkCapabilities
;
192 pr_debug("dwNtbInMaxSize=%u dwNtbOutMaxSize=%u "
193 "wNdpOutPayloadRemainder=%u wNdpOutDivisor=%u "
194 "wNdpOutAlignment=%u wNtbOutMaxDatagrams=%u flags=0x%x\n",
195 ctx
->rx_max
, ctx
->tx_max
, ctx
->tx_remainder
, ctx
->tx_modulus
,
196 ctx
->tx_ndp_modulus
, ctx
->tx_max_datagrams
, flags
);
198 /* max count of tx datagrams */
199 if ((ctx
->tx_max_datagrams
== 0) ||
200 (ctx
->tx_max_datagrams
> CDC_NCM_DPT_DATAGRAMS_MAX
))
201 ctx
->tx_max_datagrams
= CDC_NCM_DPT_DATAGRAMS_MAX
;
203 /* verify maximum size of received NTB in bytes */
204 if (ctx
->rx_max
< USB_CDC_NCM_NTB_MIN_IN_SIZE
) {
205 pr_debug("Using min receive length=%d\n",
206 USB_CDC_NCM_NTB_MIN_IN_SIZE
);
207 ctx
->rx_max
= USB_CDC_NCM_NTB_MIN_IN_SIZE
;
210 if (ctx
->rx_max
> CDC_NCM_NTB_MAX_SIZE_RX
) {
211 pr_debug("Using default maximum receive length=%d\n",
212 CDC_NCM_NTB_MAX_SIZE_RX
);
213 ctx
->rx_max
= CDC_NCM_NTB_MAX_SIZE_RX
;
216 /* inform device about NTB input size changes */
217 if (ctx
->rx_max
!= le32_to_cpu(ctx
->ncm_parm
.dwNtbInMaxSize
)) {
219 if (flags
& USB_CDC_NCM_NCAP_NTB_INPUT_SIZE
) {
220 struct usb_cdc_ncm_ndp_input_size
*ndp_in_sz
;
222 ndp_in_sz
= kzalloc(sizeof(*ndp_in_sz
), GFP_KERNEL
);
228 err
= usb_control_msg(ctx
->udev
,
229 usb_sndctrlpipe(ctx
->udev
, 0),
230 USB_CDC_SET_NTB_INPUT_SIZE
,
231 USB_TYPE_CLASS
| USB_DIR_OUT
232 | USB_RECIP_INTERFACE
,
233 0, iface_no
, ndp_in_sz
, 8, 1000);
236 __le32
*dwNtbInMaxSize
;
237 dwNtbInMaxSize
= kzalloc(sizeof(*dwNtbInMaxSize
),
239 if (!dwNtbInMaxSize
) {
243 *dwNtbInMaxSize
= cpu_to_le32(ctx
->rx_max
);
245 err
= usb_control_msg(ctx
->udev
,
246 usb_sndctrlpipe(ctx
->udev
, 0),
247 USB_CDC_SET_NTB_INPUT_SIZE
,
248 USB_TYPE_CLASS
| USB_DIR_OUT
249 | USB_RECIP_INTERFACE
,
250 0, iface_no
, dwNtbInMaxSize
, 4, 1000);
251 kfree(dwNtbInMaxSize
);
255 pr_debug("Setting NTB Input Size failed\n");
258 /* verify maximum size of transmitted NTB in bytes */
260 (CDC_NCM_MIN_HDR_SIZE
+ CDC_NCM_MIN_DATAGRAM_SIZE
)) ||
261 (ctx
->tx_max
> CDC_NCM_NTB_MAX_SIZE_TX
)) {
262 pr_debug("Using default maximum transmit length=%d\n",
263 CDC_NCM_NTB_MAX_SIZE_TX
);
264 ctx
->tx_max
= CDC_NCM_NTB_MAX_SIZE_TX
;
268 * verify that the structure alignment is:
270 * - not greater than the maximum transmit length
271 * - not less than four bytes
273 val
= ctx
->tx_ndp_modulus
;
275 if ((val
< USB_CDC_NCM_NDP_ALIGN_MIN_SIZE
) ||
276 (val
!= ((-val
) & val
)) || (val
>= ctx
->tx_max
)) {
277 pr_debug("Using default alignment: 4 bytes\n");
278 ctx
->tx_ndp_modulus
= USB_CDC_NCM_NDP_ALIGN_MIN_SIZE
;
282 * verify that the payload alignment is:
284 * - not greater than the maximum transmit length
285 * - not less than four bytes
287 val
= ctx
->tx_modulus
;
289 if ((val
< USB_CDC_NCM_NDP_ALIGN_MIN_SIZE
) ||
290 (val
!= ((-val
) & val
)) || (val
>= ctx
->tx_max
)) {
291 pr_debug("Using default transmit modulus: 4 bytes\n");
292 ctx
->tx_modulus
= USB_CDC_NCM_NDP_ALIGN_MIN_SIZE
;
295 /* verify the payload remainder */
296 if (ctx
->tx_remainder
>= ctx
->tx_modulus
) {
297 pr_debug("Using default transmit remainder: 0 bytes\n");
298 ctx
->tx_remainder
= 0;
301 /* adjust TX-remainder according to NCM specification. */
302 ctx
->tx_remainder
= ((ctx
->tx_remainder
- ETH_HLEN
) &
303 (ctx
->tx_modulus
- 1));
305 /* additional configuration */
308 if (flags
& USB_CDC_NCM_NCAP_CRC_MODE
) {
309 err
= usb_control_msg(ctx
->udev
, usb_sndctrlpipe(ctx
->udev
, 0),
310 USB_CDC_SET_CRC_MODE
,
311 USB_TYPE_CLASS
| USB_DIR_OUT
312 | USB_RECIP_INTERFACE
,
313 USB_CDC_NCM_CRC_NOT_APPENDED
,
314 iface_no
, NULL
, 0, 1000);
316 pr_debug("Setting CRC mode off failed\n");
319 /* set NTB format, if both formats are supported */
320 if (ntb_fmt_supported
& USB_CDC_NCM_NTH32_SIGN
) {
321 err
= usb_control_msg(ctx
->udev
, usb_sndctrlpipe(ctx
->udev
, 0),
322 USB_CDC_SET_NTB_FORMAT
, USB_TYPE_CLASS
323 | USB_DIR_OUT
| USB_RECIP_INTERFACE
,
324 USB_CDC_NCM_NTB16_FORMAT
,
325 iface_no
, NULL
, 0, 1000);
327 pr_debug("Setting NTB format to 16-bit failed\n");
330 ctx
->max_datagram_size
= CDC_NCM_MIN_DATAGRAM_SIZE
;
332 /* set Max Datagram Size (MTU) */
333 if (flags
& USB_CDC_NCM_NCAP_MAX_DATAGRAM_SIZE
) {
334 __le16
*max_datagram_size
;
335 u16 eth_max_sz
= le16_to_cpu(ctx
->ether_desc
->wMaxSegmentSize
);
337 max_datagram_size
= kzalloc(sizeof(*max_datagram_size
),
339 if (!max_datagram_size
) {
344 err
= usb_control_msg(ctx
->udev
, usb_rcvctrlpipe(ctx
->udev
, 0),
345 USB_CDC_GET_MAX_DATAGRAM_SIZE
,
346 USB_TYPE_CLASS
| USB_DIR_IN
347 | USB_RECIP_INTERFACE
,
348 0, iface_no
, max_datagram_size
,
351 pr_debug("GET_MAX_DATAGRAM_SIZE failed, use size=%u\n",
352 CDC_NCM_MIN_DATAGRAM_SIZE
);
354 ctx
->max_datagram_size
=
355 le16_to_cpu(*max_datagram_size
);
356 /* Check Eth descriptor value */
357 if (ctx
->max_datagram_size
> eth_max_sz
)
358 ctx
->max_datagram_size
= eth_max_sz
;
360 if (ctx
->max_datagram_size
> CDC_NCM_MAX_DATAGRAM_SIZE
)
361 ctx
->max_datagram_size
=
362 CDC_NCM_MAX_DATAGRAM_SIZE
;
364 if (ctx
->max_datagram_size
< CDC_NCM_MIN_DATAGRAM_SIZE
)
365 ctx
->max_datagram_size
=
366 CDC_NCM_MIN_DATAGRAM_SIZE
;
368 /* if value changed, update device */
369 if (ctx
->max_datagram_size
!=
370 le16_to_cpu(*max_datagram_size
)) {
371 err
= usb_control_msg(ctx
->udev
,
372 usb_sndctrlpipe(ctx
->udev
, 0),
373 USB_CDC_SET_MAX_DATAGRAM_SIZE
,
374 USB_TYPE_CLASS
| USB_DIR_OUT
375 | USB_RECIP_INTERFACE
,
377 iface_no
, max_datagram_size
,
380 pr_debug("SET_MAX_DGRAM_SIZE failed\n");
383 kfree(max_datagram_size
);
387 if (ctx
->netdev
->mtu
!= (ctx
->max_datagram_size
- ETH_HLEN
))
388 ctx
->netdev
->mtu
= ctx
->max_datagram_size
- ETH_HLEN
;
394 cdc_ncm_find_endpoints(struct cdc_ncm_ctx
*ctx
, struct usb_interface
*intf
)
396 struct usb_host_endpoint
*e
;
399 for (ep
= 0; ep
< intf
->cur_altsetting
->desc
.bNumEndpoints
; ep
++) {
401 e
= intf
->cur_altsetting
->endpoint
+ ep
;
402 switch (e
->desc
.bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) {
403 case USB_ENDPOINT_XFER_INT
:
404 if (usb_endpoint_dir_in(&e
->desc
)) {
405 if (ctx
->status_ep
== NULL
)
410 case USB_ENDPOINT_XFER_BULK
:
411 if (usb_endpoint_dir_in(&e
->desc
)) {
412 if (ctx
->in_ep
== NULL
)
415 if (ctx
->out_ep
== NULL
)
426 static void cdc_ncm_free(struct cdc_ncm_ctx
*ctx
)
431 if (ctx
->tx_rem_skb
!= NULL
) {
432 dev_kfree_skb_any(ctx
->tx_rem_skb
);
433 ctx
->tx_rem_skb
= NULL
;
436 if (ctx
->tx_curr_skb
!= NULL
) {
437 dev_kfree_skb_any(ctx
->tx_curr_skb
);
438 ctx
->tx_curr_skb
= NULL
;
444 static const struct ethtool_ops cdc_ncm_ethtool_ops
= {
445 .get_drvinfo
= cdc_ncm_get_drvinfo
,
446 .get_link
= usbnet_get_link
,
447 .get_msglevel
= usbnet_get_msglevel
,
448 .set_msglevel
= usbnet_set_msglevel
,
449 .get_settings
= usbnet_get_settings
,
450 .set_settings
= usbnet_set_settings
,
451 .nway_reset
= usbnet_nway_reset
,
454 static int cdc_ncm_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
456 struct cdc_ncm_ctx
*ctx
;
457 struct usb_driver
*driver
;
463 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
467 hrtimer_init(&ctx
->tx_timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
468 ctx
->tx_timer
.function
= &cdc_ncm_tx_timer_cb
;
469 ctx
->bh
.data
= (unsigned long)ctx
;
470 ctx
->bh
.func
= cdc_ncm_txpath_bh
;
471 atomic_set(&ctx
->stop
, 0);
472 spin_lock_init(&ctx
->mtx
);
473 ctx
->netdev
= dev
->net
;
475 /* store ctx pointer in device data field */
476 dev
->data
[0] = (unsigned long)ctx
;
478 /* get some pointers */
479 driver
= driver_of(intf
);
480 buf
= intf
->cur_altsetting
->extra
;
481 len
= intf
->cur_altsetting
->extralen
;
483 ctx
->udev
= dev
->udev
;
486 /* parse through descriptors associated with control interface */
487 while ((len
> 0) && (buf
[0] > 2) && (buf
[0] <= len
)) {
489 if (buf
[1] != USB_DT_CS_INTERFACE
)
493 case USB_CDC_UNION_TYPE
:
494 if (buf
[0] < sizeof(*(ctx
->union_desc
)))
498 (const struct usb_cdc_union_desc
*)buf
;
500 ctx
->control
= usb_ifnum_to_if(dev
->udev
,
501 ctx
->union_desc
->bMasterInterface0
);
502 ctx
->data
= usb_ifnum_to_if(dev
->udev
,
503 ctx
->union_desc
->bSlaveInterface0
);
506 case USB_CDC_ETHERNET_TYPE
:
507 if (buf
[0] < sizeof(*(ctx
->ether_desc
)))
511 (const struct usb_cdc_ether_desc
*)buf
;
513 le16_to_cpu(ctx
->ether_desc
->wMaxSegmentSize
);
515 if (dev
->hard_mtu
< CDC_NCM_MIN_DATAGRAM_SIZE
)
516 dev
->hard_mtu
= CDC_NCM_MIN_DATAGRAM_SIZE
;
517 else if (dev
->hard_mtu
> CDC_NCM_MAX_DATAGRAM_SIZE
)
518 dev
->hard_mtu
= CDC_NCM_MAX_DATAGRAM_SIZE
;
521 case USB_CDC_NCM_TYPE
:
522 if (buf
[0] < sizeof(*(ctx
->func_desc
)))
525 ctx
->func_desc
= (const struct usb_cdc_ncm_desc
*)buf
;
532 /* advance to next descriptor */
538 /* check if we got everything */
539 if ((ctx
->control
== NULL
) || (ctx
->data
== NULL
) ||
540 (ctx
->ether_desc
== NULL
) || (ctx
->control
!= intf
))
543 /* claim interfaces, if any */
544 temp
= usb_driver_claim_interface(driver
, ctx
->data
, dev
);
548 iface_no
= ctx
->data
->cur_altsetting
->desc
.bInterfaceNumber
;
550 /* reset data interface */
551 temp
= usb_set_interface(dev
->udev
, iface_no
, 0);
555 /* initialize data interface */
556 if (cdc_ncm_setup(ctx
))
559 /* configure data interface */
560 temp
= usb_set_interface(dev
->udev
, iface_no
, 1);
564 cdc_ncm_find_endpoints(ctx
, ctx
->data
);
565 cdc_ncm_find_endpoints(ctx
, ctx
->control
);
567 if ((ctx
->in_ep
== NULL
) || (ctx
->out_ep
== NULL
) ||
568 (ctx
->status_ep
== NULL
))
571 dev
->net
->ethtool_ops
= &cdc_ncm_ethtool_ops
;
573 usb_set_intfdata(ctx
->data
, dev
);
574 usb_set_intfdata(ctx
->control
, dev
);
575 usb_set_intfdata(ctx
->intf
, dev
);
577 temp
= usbnet_get_ethernet_addr(dev
, ctx
->ether_desc
->iMACAddress
);
581 dev_info(&dev
->udev
->dev
, "MAC-Address: %pM\n", dev
->net
->dev_addr
);
583 dev
->in
= usb_rcvbulkpipe(dev
->udev
,
584 ctx
->in_ep
->desc
.bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
585 dev
->out
= usb_sndbulkpipe(dev
->udev
,
586 ctx
->out_ep
->desc
.bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
587 dev
->status
= ctx
->status_ep
;
588 dev
->rx_urb_size
= ctx
->rx_max
;
591 * We should get an event when network connection is "connected" or
592 * "disconnected". Set network connection in "disconnected" state
593 * (carrier is OFF) during attach, so the IP network stack does not
594 * start IPv6 negotiation and more.
596 netif_carrier_off(dev
->net
);
597 ctx
->tx_speed
= ctx
->rx_speed
= 0;
601 usb_set_intfdata(ctx
->control
, NULL
);
602 usb_set_intfdata(ctx
->data
, NULL
);
603 usb_driver_release_interface(driver
, ctx
->data
);
605 cdc_ncm_free((struct cdc_ncm_ctx
*)dev
->data
[0]);
607 dev_info(&dev
->udev
->dev
, "bind() failure\n");
611 static void cdc_ncm_unbind(struct usbnet
*dev
, struct usb_interface
*intf
)
613 struct cdc_ncm_ctx
*ctx
= (struct cdc_ncm_ctx
*)dev
->data
[0];
614 struct usb_driver
*driver
= driver_of(intf
);
617 return; /* no setup */
619 atomic_set(&ctx
->stop
, 1);
621 if (hrtimer_active(&ctx
->tx_timer
))
622 hrtimer_cancel(&ctx
->tx_timer
);
624 tasklet_kill(&ctx
->bh
);
626 /* disconnect master --> disconnect slave */
627 if (intf
== ctx
->control
&& ctx
->data
) {
628 usb_set_intfdata(ctx
->data
, NULL
);
629 usb_driver_release_interface(driver
, ctx
->data
);
632 } else if (intf
== ctx
->data
&& ctx
->control
) {
633 usb_set_intfdata(ctx
->control
, NULL
);
634 usb_driver_release_interface(driver
, ctx
->control
);
638 usb_set_intfdata(ctx
->intf
, NULL
);
642 static void cdc_ncm_zero_fill(u8
*ptr
, u32 first
, u32 end
, u32 max
)
650 memset(ptr
+ first
, 0, end
- first
);
653 static struct sk_buff
*
654 cdc_ncm_fill_tx_frame(struct cdc_ncm_ctx
*ctx
, struct sk_buff
*skb
)
656 struct sk_buff
*skb_out
;
663 /* if there is a remaining skb, it gets priority */
665 swap(skb
, ctx
->tx_rem_skb
);
677 /* check if we are resuming an OUT skb */
678 if (ctx
->tx_curr_skb
!= NULL
) {
680 skb_out
= ctx
->tx_curr_skb
;
681 offset
= ctx
->tx_curr_offset
;
682 last_offset
= ctx
->tx_curr_last_offset
;
683 n
= ctx
->tx_curr_frame_num
;
686 /* reset variables */
687 skb_out
= alloc_skb((ctx
->tx_max
+ 1), GFP_ATOMIC
);
688 if (skb_out
== NULL
) {
690 dev_kfree_skb_any(skb
);
691 ctx
->netdev
->stats
.tx_dropped
++;
696 /* make room for NTH and NDP */
697 offset
= ALIGN(sizeof(struct usb_cdc_ncm_nth16
),
698 ctx
->tx_ndp_modulus
) +
699 sizeof(struct usb_cdc_ncm_ndp16
) +
700 (ctx
->tx_max_datagrams
+ 1) *
701 sizeof(struct usb_cdc_ncm_dpe16
);
703 /* store last valid offset before alignment */
704 last_offset
= offset
;
705 /* align first Datagram offset correctly */
706 offset
= ALIGN(offset
, ctx
->tx_modulus
) + ctx
->tx_remainder
;
707 /* zero buffer till the first IP datagram */
708 cdc_ncm_zero_fill(skb_out
->data
, 0, offset
, offset
);
710 ctx
->tx_curr_frame_num
= 0;
713 for (; n
< ctx
->tx_max_datagrams
; n
++) {
714 /* check if end of transmit buffer is reached */
715 if (offset
>= ctx
->tx_max
) {
719 /* compute maximum buffer size */
720 rem
= ctx
->tx_max
- offset
;
723 skb
= ctx
->tx_rem_skb
;
724 ctx
->tx_rem_skb
= NULL
;
726 /* check for end of skb */
731 if (skb
->len
> rem
) {
733 /* won't fit, MTU problem? */
734 dev_kfree_skb_any(skb
);
736 ctx
->netdev
->stats
.tx_dropped
++;
738 /* no room for skb - store for later */
739 if (ctx
->tx_rem_skb
!= NULL
) {
740 dev_kfree_skb_any(ctx
->tx_rem_skb
);
741 ctx
->netdev
->stats
.tx_dropped
++;
743 ctx
->tx_rem_skb
= skb
;
750 memcpy(((u8
*)skb_out
->data
) + offset
, skb
->data
, skb
->len
);
752 ctx
->tx_ncm
.dpe16
[n
].wDatagramLength
= cpu_to_le16(skb
->len
);
753 ctx
->tx_ncm
.dpe16
[n
].wDatagramIndex
= cpu_to_le16(offset
);
758 /* store last valid offset before alignment */
759 last_offset
= offset
;
761 /* align offset correctly */
762 offset
= ALIGN(offset
, ctx
->tx_modulus
) + ctx
->tx_remainder
;
765 cdc_ncm_zero_fill(skb_out
->data
, last_offset
, offset
,
767 dev_kfree_skb_any(skb
);
771 /* free up any dangling skb */
773 dev_kfree_skb_any(skb
);
775 ctx
->netdev
->stats
.tx_dropped
++;
778 ctx
->tx_curr_frame_num
= n
;
781 /* wait for more frames */
783 ctx
->tx_curr_skb
= skb_out
;
784 ctx
->tx_curr_offset
= offset
;
785 ctx
->tx_curr_last_offset
= last_offset
;
788 } else if ((n
< ctx
->tx_max_datagrams
) && (ready2send
== 0)) {
789 /* wait for more frames */
791 ctx
->tx_curr_skb
= skb_out
;
792 ctx
->tx_curr_offset
= offset
;
793 ctx
->tx_curr_last_offset
= last_offset
;
794 /* set the pending count */
795 if (n
< CDC_NCM_RESTART_TIMER_DATAGRAM_CNT
)
796 ctx
->tx_timer_pending
= CDC_NCM_TIMER_PENDING_CNT
;
801 /* variables will be reset at next call */
804 /* check for overflow */
805 if (last_offset
> ctx
->tx_max
)
806 last_offset
= ctx
->tx_max
;
809 offset
= last_offset
;
812 * If collected data size is less or equal CDC_NCM_MIN_TX_PKT bytes,
813 * we send buffers as it is. If we get more data, it would be more
814 * efficient for USB HS mobile device with DMA engine to receive a full
815 * size NTB, than canceling DMA transfer and receiving a short packet.
817 if (offset
> CDC_NCM_MIN_TX_PKT
)
818 offset
= ctx
->tx_max
;
820 /* final zero padding */
821 cdc_ncm_zero_fill(skb_out
->data
, last_offset
, offset
, ctx
->tx_max
);
823 /* store last offset */
824 last_offset
= offset
;
826 if (((last_offset
< ctx
->tx_max
) && ((last_offset
%
827 le16_to_cpu(ctx
->out_ep
->desc
.wMaxPacketSize
)) == 0)) ||
828 (((last_offset
== ctx
->tx_max
) && ((ctx
->tx_max
%
829 le16_to_cpu(ctx
->out_ep
->desc
.wMaxPacketSize
)) == 0)) &&
830 (ctx
->tx_max
< le32_to_cpu(ctx
->ncm_parm
.dwNtbOutMaxSize
)))) {
831 /* force short packet */
832 *(((u8
*)skb_out
->data
) + last_offset
) = 0;
836 /* zero the rest of the DPEs plus the last NULL entry */
837 for (; n
<= CDC_NCM_DPT_DATAGRAMS_MAX
; n
++) {
838 ctx
->tx_ncm
.dpe16
[n
].wDatagramLength
= 0;
839 ctx
->tx_ncm
.dpe16
[n
].wDatagramIndex
= 0;
842 /* fill out 16-bit NTB header */
843 ctx
->tx_ncm
.nth16
.dwSignature
= cpu_to_le32(USB_CDC_NCM_NTH16_SIGN
);
844 ctx
->tx_ncm
.nth16
.wHeaderLength
=
845 cpu_to_le16(sizeof(ctx
->tx_ncm
.nth16
));
846 ctx
->tx_ncm
.nth16
.wSequence
= cpu_to_le16(ctx
->tx_seq
);
847 ctx
->tx_ncm
.nth16
.wBlockLength
= cpu_to_le16(last_offset
);
848 index
= ALIGN(sizeof(struct usb_cdc_ncm_nth16
), ctx
->tx_ndp_modulus
);
849 ctx
->tx_ncm
.nth16
.wNdpIndex
= cpu_to_le16(index
);
851 memcpy(skb_out
->data
, &(ctx
->tx_ncm
.nth16
), sizeof(ctx
->tx_ncm
.nth16
));
854 /* fill out 16-bit NDP table */
855 ctx
->tx_ncm
.ndp16
.dwSignature
=
856 cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN
);
857 rem
= sizeof(ctx
->tx_ncm
.ndp16
) + ((ctx
->tx_curr_frame_num
+ 1) *
858 sizeof(struct usb_cdc_ncm_dpe16
));
859 ctx
->tx_ncm
.ndp16
.wLength
= cpu_to_le16(rem
);
860 ctx
->tx_ncm
.ndp16
.wNextNdpIndex
= 0; /* reserved */
862 memcpy(((u8
*)skb_out
->data
) + index
,
863 &(ctx
->tx_ncm
.ndp16
),
864 sizeof(ctx
->tx_ncm
.ndp16
));
866 memcpy(((u8
*)skb_out
->data
) + index
+ sizeof(ctx
->tx_ncm
.ndp16
),
867 &(ctx
->tx_ncm
.dpe16
),
868 (ctx
->tx_curr_frame_num
+ 1) *
869 sizeof(struct usb_cdc_ncm_dpe16
));
871 /* set frame length */
872 skb_put(skb_out
, last_offset
);
875 ctx
->tx_curr_skb
= NULL
;
876 ctx
->netdev
->stats
.tx_packets
+= ctx
->tx_curr_frame_num
;
880 /* Start timer, if there is a remaining skb */
881 if (ctx
->tx_curr_skb
!= NULL
)
882 cdc_ncm_tx_timeout_start(ctx
);
886 static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx
*ctx
)
888 /* start timer, if not already started */
889 if (!(hrtimer_active(&ctx
->tx_timer
) || atomic_read(&ctx
->stop
)))
890 hrtimer_start(&ctx
->tx_timer
,
891 ktime_set(0, CDC_NCM_TIMER_INTERVAL
),
895 static enum hrtimer_restart
cdc_ncm_tx_timer_cb(struct hrtimer
*timer
)
897 struct cdc_ncm_ctx
*ctx
=
898 container_of(timer
, struct cdc_ncm_ctx
, tx_timer
);
900 if (!atomic_read(&ctx
->stop
))
901 tasklet_schedule(&ctx
->bh
);
902 return HRTIMER_NORESTART
;
905 static void cdc_ncm_txpath_bh(unsigned long param
)
907 struct cdc_ncm_ctx
*ctx
= (struct cdc_ncm_ctx
*)param
;
909 spin_lock_bh(&ctx
->mtx
);
910 if (ctx
->tx_timer_pending
!= 0) {
911 ctx
->tx_timer_pending
--;
912 cdc_ncm_tx_timeout_start(ctx
);
913 spin_unlock_bh(&ctx
->mtx
);
914 } else if (ctx
->netdev
!= NULL
) {
915 spin_unlock_bh(&ctx
->mtx
);
916 netif_tx_lock_bh(ctx
->netdev
);
917 usbnet_start_xmit(NULL
, ctx
->netdev
);
918 netif_tx_unlock_bh(ctx
->netdev
);
922 static struct sk_buff
*
923 cdc_ncm_tx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
, gfp_t flags
)
925 struct sk_buff
*skb_out
;
926 struct cdc_ncm_ctx
*ctx
= (struct cdc_ncm_ctx
*)dev
->data
[0];
929 * The Ethernet API we are using does not support transmitting
930 * multiple Ethernet frames in a single call. This driver will
931 * accumulate multiple Ethernet frames and send out a larger
932 * USB frame when the USB buffer is full or when a single jiffies
938 spin_lock_bh(&ctx
->mtx
);
939 skb_out
= cdc_ncm_fill_tx_frame(ctx
, skb
);
940 spin_unlock_bh(&ctx
->mtx
);
945 dev_kfree_skb_any(skb
);
950 static int cdc_ncm_rx_fixup(struct usbnet
*dev
, struct sk_buff
*skb_in
)
953 struct cdc_ncm_ctx
*ctx
= (struct cdc_ncm_ctx
*)dev
->data
[0];
958 struct usb_cdc_ncm_nth16
*nth16
;
959 struct usb_cdc_ncm_ndp16
*ndp16
;
960 struct usb_cdc_ncm_dpe16
*dpe16
;
965 if (skb_in
->len
< (sizeof(struct usb_cdc_ncm_nth16
) +
966 sizeof(struct usb_cdc_ncm_ndp16
))) {
967 pr_debug("frame too short\n");
971 nth16
= (struct usb_cdc_ncm_nth16
*)skb_in
->data
;
973 if (le32_to_cpu(nth16
->dwSignature
) != USB_CDC_NCM_NTH16_SIGN
) {
974 pr_debug("invalid NTH16 signature <%u>\n",
975 le32_to_cpu(nth16
->dwSignature
));
979 len
= le16_to_cpu(nth16
->wBlockLength
);
980 if (len
> ctx
->rx_max
) {
981 pr_debug("unsupported NTB block length %u/%u\n", len
,
986 if ((ctx
->rx_seq
+ 1) != le16_to_cpu(nth16
->wSequence
) &&
987 (ctx
->rx_seq
|| le16_to_cpu(nth16
->wSequence
)) &&
988 !((ctx
->rx_seq
== 0xffff) && !le16_to_cpu(nth16
->wSequence
))) {
989 pr_debug("sequence number glitch prev=%d curr=%d\n",
990 ctx
->rx_seq
, le16_to_cpu(nth16
->wSequence
));
992 ctx
->rx_seq
= le16_to_cpu(nth16
->wSequence
);
994 len
= le16_to_cpu(nth16
->wNdpIndex
);
995 if ((len
+ sizeof(struct usb_cdc_ncm_ndp16
)) > skb_in
->len
) {
996 pr_debug("invalid DPT16 index <%u>\n",
997 le16_to_cpu(nth16
->wNdpIndex
));
1001 ndp16
= (struct usb_cdc_ncm_ndp16
*)(((u8
*)skb_in
->data
) + len
);
1003 if (le32_to_cpu(ndp16
->dwSignature
) != USB_CDC_NCM_NDP16_NOCRC_SIGN
) {
1004 pr_debug("invalid DPT16 signature <%u>\n",
1005 le32_to_cpu(ndp16
->dwSignature
));
1009 if (le16_to_cpu(ndp16
->wLength
) < USB_CDC_NCM_NDP16_LENGTH_MIN
) {
1010 pr_debug("invalid DPT16 length <%u>\n",
1011 le32_to_cpu(ndp16
->dwSignature
));
1015 nframes
= ((le16_to_cpu(ndp16
->wLength
) -
1016 sizeof(struct usb_cdc_ncm_ndp16
)) /
1017 sizeof(struct usb_cdc_ncm_dpe16
));
1018 nframes
--; /* we process NDP entries except for the last one */
1020 len
+= sizeof(struct usb_cdc_ncm_ndp16
);
1022 if ((len
+ nframes
* (sizeof(struct usb_cdc_ncm_dpe16
))) >
1024 pr_debug("Invalid nframes = %d\n", nframes
);
1028 dpe16
= (struct usb_cdc_ncm_dpe16
*)(((u8
*)skb_in
->data
) + len
);
1030 for (x
= 0; x
< nframes
; x
++, dpe16
++) {
1031 offset
= le16_to_cpu(dpe16
->wDatagramIndex
);
1032 len
= le16_to_cpu(dpe16
->wDatagramLength
);
1036 * All entries after first NULL entry are to be ignored
1038 if ((offset
== 0) || (len
== 0)) {
1040 goto error
; /* empty NTB */
1044 /* sanity checking */
1045 if (((offset
+ len
) > skb_in
->len
) ||
1046 (len
> ctx
->rx_max
) || (len
< ETH_HLEN
)) {
1047 pr_debug("invalid frame detected (ignored)"
1048 "offset[%u]=%u, length=%u, skb=%p\n",
1049 x
, offset
, len
, skb_in
);
1055 skb
= skb_clone(skb_in
, GFP_ATOMIC
);
1059 skb
->data
= ((u8
*)skb_in
->data
) + offset
;
1060 skb_set_tail_pointer(skb
, len
);
1061 usbnet_skb_return(dev
, skb
);
1070 cdc_ncm_speed_change(struct cdc_ncm_ctx
*ctx
,
1071 struct usb_cdc_speed_change
*data
)
1073 uint32_t rx_speed
= le32_to_cpu(data
->DLBitRRate
);
1074 uint32_t tx_speed
= le32_to_cpu(data
->ULBitRate
);
1077 * Currently the USB-NET API does not support reporting the actual
1078 * device speed. Do print it instead.
1080 if ((tx_speed
!= ctx
->tx_speed
) || (rx_speed
!= ctx
->rx_speed
)) {
1081 ctx
->tx_speed
= tx_speed
;
1082 ctx
->rx_speed
= rx_speed
;
1084 if ((tx_speed
> 1000000) && (rx_speed
> 1000000)) {
1085 printk(KERN_INFO KBUILD_MODNAME
1086 ": %s: %u mbit/s downlink "
1087 "%u mbit/s uplink\n",
1089 (unsigned int)(rx_speed
/ 1000000U),
1090 (unsigned int)(tx_speed
/ 1000000U));
1092 printk(KERN_INFO KBUILD_MODNAME
1093 ": %s: %u kbit/s downlink "
1094 "%u kbit/s uplink\n",
1096 (unsigned int)(rx_speed
/ 1000U),
1097 (unsigned int)(tx_speed
/ 1000U));
1102 static void cdc_ncm_status(struct usbnet
*dev
, struct urb
*urb
)
1104 struct cdc_ncm_ctx
*ctx
;
1105 struct usb_cdc_notification
*event
;
1107 ctx
= (struct cdc_ncm_ctx
*)dev
->data
[0];
1109 if (urb
->actual_length
< sizeof(*event
))
1112 /* test for split data in 8-byte chunks */
1113 if (test_and_clear_bit(EVENT_STS_SPLIT
, &dev
->flags
)) {
1114 cdc_ncm_speed_change(ctx
,
1115 (struct usb_cdc_speed_change
*)urb
->transfer_buffer
);
1119 event
= urb
->transfer_buffer
;
1121 switch (event
->bNotificationType
) {
1122 case USB_CDC_NOTIFY_NETWORK_CONNECTION
:
1124 * According to the CDC NCM specification ch.7.1
1125 * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be
1126 * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE.
1128 ctx
->connected
= event
->wValue
;
1130 printk(KERN_INFO KBUILD_MODNAME
": %s: network connection:"
1132 ctx
->netdev
->name
, ctx
->connected
? "" : "dis");
1135 netif_carrier_on(dev
->net
);
1137 netif_carrier_off(dev
->net
);
1138 ctx
->tx_speed
= ctx
->rx_speed
= 0;
1142 case USB_CDC_NOTIFY_SPEED_CHANGE
:
1143 if (urb
->actual_length
< (sizeof(*event
) +
1144 sizeof(struct usb_cdc_speed_change
)))
1145 set_bit(EVENT_STS_SPLIT
, &dev
->flags
);
1147 cdc_ncm_speed_change(ctx
,
1148 (struct usb_cdc_speed_change
*) &event
[1]);
1152 dev_err(&dev
->udev
->dev
, "NCM: unexpected "
1153 "notification 0x%02x!\n", event
->bNotificationType
);
1158 static int cdc_ncm_check_connect(struct usbnet
*dev
)
1160 struct cdc_ncm_ctx
*ctx
;
1162 ctx
= (struct cdc_ncm_ctx
*)dev
->data
[0];
1164 return 1; /* disconnected */
1166 return !ctx
->connected
;
1170 cdc_ncm_probe(struct usb_interface
*udev
, const struct usb_device_id
*prod
)
1172 return usbnet_probe(udev
, prod
);
1175 static void cdc_ncm_disconnect(struct usb_interface
*intf
)
1177 struct usbnet
*dev
= usb_get_intfdata(intf
);
1180 return; /* already disconnected */
1182 usbnet_disconnect(intf
);
1185 static int cdc_ncm_manage_power(struct usbnet
*dev
, int status
)
1187 dev
->intf
->needs_remote_wakeup
= status
;
1191 static const struct driver_info cdc_ncm_info
= {
1192 .description
= "CDC NCM",
1193 .flags
= FLAG_POINTTOPOINT
| FLAG_NO_SETINT
| FLAG_MULTI_PACKET
,
1194 .bind
= cdc_ncm_bind
,
1195 .unbind
= cdc_ncm_unbind
,
1196 .check_connect
= cdc_ncm_check_connect
,
1197 .manage_power
= cdc_ncm_manage_power
,
1198 .status
= cdc_ncm_status
,
1199 .rx_fixup
= cdc_ncm_rx_fixup
,
1200 .tx_fixup
= cdc_ncm_tx_fixup
,
1203 /* Same as cdc_ncm_info, but with FLAG_WWAN */
1204 static const struct driver_info wwan_info
= {
1205 .description
= "Mobile Broadband Network Device",
1206 .flags
= FLAG_POINTTOPOINT
| FLAG_NO_SETINT
| FLAG_MULTI_PACKET
1208 .bind
= cdc_ncm_bind
,
1209 .unbind
= cdc_ncm_unbind
,
1210 .check_connect
= cdc_ncm_check_connect
,
1211 .manage_power
= cdc_ncm_manage_power
,
1212 .status
= cdc_ncm_status
,
1213 .rx_fixup
= cdc_ncm_rx_fixup
,
1214 .tx_fixup
= cdc_ncm_tx_fixup
,
1217 static const struct usb_device_id cdc_devs
[] = {
1218 /* Ericsson MBM devices like F5521gw */
1219 { .match_flags
= USB_DEVICE_ID_MATCH_INT_INFO
1220 | USB_DEVICE_ID_MATCH_VENDOR
,
1222 .bInterfaceClass
= USB_CLASS_COMM
,
1223 .bInterfaceSubClass
= USB_CDC_SUBCLASS_NCM
,
1224 .bInterfaceProtocol
= USB_CDC_PROTO_NONE
,
1225 .driver_info
= (unsigned long) &wwan_info
,
1228 /* Dell branded MBM devices like DW5550 */
1229 { .match_flags
= USB_DEVICE_ID_MATCH_INT_INFO
1230 | USB_DEVICE_ID_MATCH_VENDOR
,
1232 .bInterfaceClass
= USB_CLASS_COMM
,
1233 .bInterfaceSubClass
= USB_CDC_SUBCLASS_NCM
,
1234 .bInterfaceProtocol
= USB_CDC_PROTO_NONE
,
1235 .driver_info
= (unsigned long) &wwan_info
,
1238 /* Toshiba branded MBM devices */
1239 { .match_flags
= USB_DEVICE_ID_MATCH_INT_INFO
1240 | USB_DEVICE_ID_MATCH_VENDOR
,
1242 .bInterfaceClass
= USB_CLASS_COMM
,
1243 .bInterfaceSubClass
= USB_CDC_SUBCLASS_NCM
,
1244 .bInterfaceProtocol
= USB_CDC_PROTO_NONE
,
1245 .driver_info
= (unsigned long) &wwan_info
,
1248 /* Generic CDC-NCM devices */
1249 { USB_INTERFACE_INFO(USB_CLASS_COMM
,
1250 USB_CDC_SUBCLASS_NCM
, USB_CDC_PROTO_NONE
),
1251 .driver_info
= (unsigned long)&cdc_ncm_info
,
1256 MODULE_DEVICE_TABLE(usb
, cdc_devs
);
1258 static struct usb_driver cdc_ncm_driver
= {
1260 .id_table
= cdc_devs
,
1261 .probe
= cdc_ncm_probe
,
1262 .disconnect
= cdc_ncm_disconnect
,
1263 .suspend
= usbnet_suspend
,
1264 .resume
= usbnet_resume
,
1265 .reset_resume
= usbnet_resume
,
1266 .supports_autosuspend
= 1,
1267 .disable_hub_initiated_lpm
= 1,
1270 module_usb_driver(cdc_ncm_driver
);
1272 MODULE_AUTHOR("Hans Petter Selasky");
1273 MODULE_DESCRIPTION("USB CDC NCM host driver");
1274 MODULE_LICENSE("Dual BSD/GPL");