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/timer.h>
51 #include <linux/spinlock.h>
52 #include <linux/atomic.h>
53 #include <linux/usb/usbnet.h>
54 #include <linux/usb/cdc.h>
56 #define DRIVER_VERSION "01-June-2011"
58 /* CDC NCM subclass 3.2.1 */
59 #define USB_CDC_NCM_NDP16_LENGTH_MIN 0x10
61 /* Maximum NTB length */
62 #define CDC_NCM_NTB_MAX_SIZE_TX 16384 /* bytes */
63 #define CDC_NCM_NTB_MAX_SIZE_RX 16384 /* bytes */
65 /* Minimum value for MaxDatagramSize, ch. 6.2.9 */
66 #define CDC_NCM_MIN_DATAGRAM_SIZE 1514 /* bytes */
68 #define CDC_NCM_MIN_TX_PKT 512 /* bytes */
70 /* Default value for MaxDatagramSize */
71 #define CDC_NCM_MAX_DATAGRAM_SIZE 2048 /* bytes */
74 * Maximum amount of datagrams in NCM Datagram Pointer Table, not counting
75 * the last NULL entry. Any additional datagrams in NTB would be discarded.
77 #define CDC_NCM_DPT_DATAGRAMS_MAX 32
79 /* Maximum amount of IN datagrams in NTB */
80 #define CDC_NCM_DPT_DATAGRAMS_IN_MAX 0 /* unlimited */
82 /* Restart the timer, if amount of datagrams is less than given value */
83 #define CDC_NCM_RESTART_TIMER_DATAGRAM_CNT 3
85 /* The following macro defines the minimum header space */
86 #define CDC_NCM_MIN_HDR_SIZE \
87 (sizeof(struct usb_cdc_ncm_nth16) + sizeof(struct usb_cdc_ncm_ndp16) + \
88 (CDC_NCM_DPT_DATAGRAMS_MAX + 1) * sizeof(struct usb_cdc_ncm_dpe16))
91 struct usb_cdc_ncm_nth16 nth16
;
92 struct usb_cdc_ncm_ndp16 ndp16
;
93 struct usb_cdc_ncm_dpe16 dpe16
[CDC_NCM_DPT_DATAGRAMS_MAX
+ 1];
97 struct cdc_ncm_data rx_ncm
;
98 struct cdc_ncm_data tx_ncm
;
99 struct usb_cdc_ncm_ntb_parameters ncm_parm
;
100 struct timer_list tx_timer
;
102 const struct usb_cdc_ncm_desc
*func_desc
;
103 const struct usb_cdc_header_desc
*header_desc
;
104 const struct usb_cdc_union_desc
*union_desc
;
105 const struct usb_cdc_ether_desc
*ether_desc
;
107 struct net_device
*netdev
;
108 struct usb_device
*udev
;
109 struct usb_host_endpoint
*in_ep
;
110 struct usb_host_endpoint
*out_ep
;
111 struct usb_host_endpoint
*status_ep
;
112 struct usb_interface
*intf
;
113 struct usb_interface
*control
;
114 struct usb_interface
*data
;
116 struct sk_buff
*tx_curr_skb
;
117 struct sk_buff
*tx_rem_skb
;
121 u32 tx_timer_pending
;
123 u32 tx_curr_last_offset
;
124 u32 tx_curr_frame_num
;
129 u32 max_datagram_size
;
130 u16 tx_max_datagrams
;
138 static void cdc_ncm_tx_timeout(unsigned long arg
);
139 static const struct driver_info cdc_ncm_info
;
140 static struct usb_driver cdc_ncm_driver
;
141 static struct ethtool_ops cdc_ncm_ethtool_ops
;
143 static const struct usb_device_id cdc_devs
[] = {
144 { USB_INTERFACE_INFO(USB_CLASS_COMM
,
145 USB_CDC_SUBCLASS_NCM
, USB_CDC_PROTO_NONE
),
146 .driver_info
= (unsigned long)&cdc_ncm_info
,
152 MODULE_DEVICE_TABLE(usb
, cdc_devs
);
155 cdc_ncm_get_drvinfo(struct net_device
*net
, struct ethtool_drvinfo
*info
)
157 struct usbnet
*dev
= netdev_priv(net
);
159 strncpy(info
->driver
, dev
->driver_name
, sizeof(info
->driver
));
160 strncpy(info
->version
, DRIVER_VERSION
, sizeof(info
->version
));
161 strncpy(info
->fw_version
, dev
->driver_info
->description
,
162 sizeof(info
->fw_version
));
163 usb_make_path(dev
->udev
, info
->bus_info
, sizeof(info
->bus_info
));
167 cdc_ncm_do_request(struct cdc_ncm_ctx
*ctx
, struct usb_cdc_notification
*req
,
168 void *data
, u16 flags
, u16
*actlen
, u16 timeout
)
172 err
= usb_control_msg(ctx
->udev
, (req
->bmRequestType
& USB_DIR_IN
) ?
173 usb_rcvctrlpipe(ctx
->udev
, 0) :
174 usb_sndctrlpipe(ctx
->udev
, 0),
175 req
->bNotificationType
, req
->bmRequestType
,
178 req
->wLength
, timeout
);
192 static u8
cdc_ncm_setup(struct cdc_ncm_ctx
*ctx
)
194 struct usb_cdc_notification req
;
199 u16 ntb_fmt_supported
;
201 iface_no
= ctx
->control
->cur_altsetting
->desc
.bInterfaceNumber
;
203 req
.bmRequestType
= USB_TYPE_CLASS
| USB_DIR_IN
| USB_RECIP_INTERFACE
;
204 req
.bNotificationType
= USB_CDC_GET_NTB_PARAMETERS
;
206 req
.wIndex
= cpu_to_le16(iface_no
);
207 req
.wLength
= cpu_to_le16(sizeof(ctx
->ncm_parm
));
209 err
= cdc_ncm_do_request(ctx
, &req
, &ctx
->ncm_parm
, 0, NULL
, 1000);
211 pr_debug("failed GET_NTB_PARAMETERS\n");
215 /* read correct set of parameters according to device mode */
216 ctx
->rx_max
= le32_to_cpu(ctx
->ncm_parm
.dwNtbInMaxSize
);
217 ctx
->tx_max
= le32_to_cpu(ctx
->ncm_parm
.dwNtbOutMaxSize
);
218 ctx
->tx_remainder
= le16_to_cpu(ctx
->ncm_parm
.wNdpOutPayloadRemainder
);
219 ctx
->tx_modulus
= le16_to_cpu(ctx
->ncm_parm
.wNdpOutDivisor
);
220 ctx
->tx_ndp_modulus
= le16_to_cpu(ctx
->ncm_parm
.wNdpOutAlignment
);
221 /* devices prior to NCM Errata shall set this field to zero */
222 ctx
->tx_max_datagrams
= le16_to_cpu(ctx
->ncm_parm
.wNtbOutMaxDatagrams
);
223 ntb_fmt_supported
= le16_to_cpu(ctx
->ncm_parm
.bmNtbFormatsSupported
);
225 if (ctx
->func_desc
!= NULL
)
226 flags
= ctx
->func_desc
->bmNetworkCapabilities
;
230 pr_debug("dwNtbInMaxSize=%u dwNtbOutMaxSize=%u "
231 "wNdpOutPayloadRemainder=%u wNdpOutDivisor=%u "
232 "wNdpOutAlignment=%u wNtbOutMaxDatagrams=%u flags=0x%x\n",
233 ctx
->rx_max
, ctx
->tx_max
, ctx
->tx_remainder
, ctx
->tx_modulus
,
234 ctx
->tx_ndp_modulus
, ctx
->tx_max_datagrams
, flags
);
236 /* max count of tx datagrams */
237 if ((ctx
->tx_max_datagrams
== 0) ||
238 (ctx
->tx_max_datagrams
> CDC_NCM_DPT_DATAGRAMS_MAX
))
239 ctx
->tx_max_datagrams
= CDC_NCM_DPT_DATAGRAMS_MAX
;
241 /* verify maximum size of received NTB in bytes */
242 if (ctx
->rx_max
< USB_CDC_NCM_NTB_MIN_IN_SIZE
) {
243 pr_debug("Using min receive length=%d\n",
244 USB_CDC_NCM_NTB_MIN_IN_SIZE
);
245 ctx
->rx_max
= USB_CDC_NCM_NTB_MIN_IN_SIZE
;
248 if (ctx
->rx_max
> CDC_NCM_NTB_MAX_SIZE_RX
) {
249 pr_debug("Using default maximum receive length=%d\n",
250 CDC_NCM_NTB_MAX_SIZE_RX
);
251 ctx
->rx_max
= CDC_NCM_NTB_MAX_SIZE_RX
;
254 /* inform device about NTB input size changes */
255 if (ctx
->rx_max
!= le32_to_cpu(ctx
->ncm_parm
.dwNtbInMaxSize
)) {
256 req
.bmRequestType
= USB_TYPE_CLASS
| USB_DIR_OUT
|
258 req
.bNotificationType
= USB_CDC_SET_NTB_INPUT_SIZE
;
260 req
.wIndex
= cpu_to_le16(iface_no
);
262 if (flags
& USB_CDC_NCM_NCAP_NTB_INPUT_SIZE
) {
263 struct usb_cdc_ncm_ndp_input_size ndp_in_sz
;
266 ndp_in_sz
.dwNtbInMaxSize
= cpu_to_le32(ctx
->rx_max
);
267 ndp_in_sz
.wNtbInMaxDatagrams
=
268 cpu_to_le16(CDC_NCM_DPT_DATAGRAMS_MAX
);
269 ndp_in_sz
.wReserved
= 0;
270 err
= cdc_ncm_do_request(ctx
, &req
, &ndp_in_sz
, 0, NULL
,
273 __le32 dwNtbInMaxSize
= cpu_to_le32(ctx
->rx_max
);
276 err
= cdc_ncm_do_request(ctx
, &req
, &dwNtbInMaxSize
, 0,
281 pr_debug("Setting NTB Input Size failed\n");
284 /* verify maximum size of transmitted NTB in bytes */
286 (CDC_NCM_MIN_HDR_SIZE
+ CDC_NCM_MIN_DATAGRAM_SIZE
)) ||
287 (ctx
->tx_max
> CDC_NCM_NTB_MAX_SIZE_TX
)) {
288 pr_debug("Using default maximum transmit length=%d\n",
289 CDC_NCM_NTB_MAX_SIZE_TX
);
290 ctx
->tx_max
= CDC_NCM_NTB_MAX_SIZE_TX
;
294 * verify that the structure alignment is:
296 * - not greater than the maximum transmit length
297 * - not less than four bytes
299 val
= ctx
->tx_ndp_modulus
;
301 if ((val
< USB_CDC_NCM_NDP_ALIGN_MIN_SIZE
) ||
302 (val
!= ((-val
) & val
)) || (val
>= ctx
->tx_max
)) {
303 pr_debug("Using default alignment: 4 bytes\n");
304 ctx
->tx_ndp_modulus
= USB_CDC_NCM_NDP_ALIGN_MIN_SIZE
;
308 * verify that the payload alignment is:
310 * - not greater than the maximum transmit length
311 * - not less than four bytes
313 val
= ctx
->tx_modulus
;
315 if ((val
< USB_CDC_NCM_NDP_ALIGN_MIN_SIZE
) ||
316 (val
!= ((-val
) & val
)) || (val
>= ctx
->tx_max
)) {
317 pr_debug("Using default transmit modulus: 4 bytes\n");
318 ctx
->tx_modulus
= USB_CDC_NCM_NDP_ALIGN_MIN_SIZE
;
321 /* verify the payload remainder */
322 if (ctx
->tx_remainder
>= ctx
->tx_modulus
) {
323 pr_debug("Using default transmit remainder: 0 bytes\n");
324 ctx
->tx_remainder
= 0;
327 /* adjust TX-remainder according to NCM specification. */
328 ctx
->tx_remainder
= ((ctx
->tx_remainder
- ETH_HLEN
) &
329 (ctx
->tx_modulus
- 1));
331 /* additional configuration */
334 if (flags
& USB_CDC_NCM_NCAP_CRC_MODE
) {
335 req
.bmRequestType
= USB_TYPE_CLASS
| USB_DIR_OUT
|
337 req
.bNotificationType
= USB_CDC_SET_CRC_MODE
;
338 req
.wValue
= cpu_to_le16(USB_CDC_NCM_CRC_NOT_APPENDED
);
339 req
.wIndex
= cpu_to_le16(iface_no
);
342 err
= cdc_ncm_do_request(ctx
, &req
, NULL
, 0, NULL
, 1000);
344 pr_debug("Setting CRC mode off failed\n");
347 /* set NTB format, if both formats are supported */
348 if (ntb_fmt_supported
& USB_CDC_NCM_NTH32_SIGN
) {
349 req
.bmRequestType
= USB_TYPE_CLASS
| USB_DIR_OUT
|
351 req
.bNotificationType
= USB_CDC_SET_NTB_FORMAT
;
352 req
.wValue
= cpu_to_le16(USB_CDC_NCM_NTB16_FORMAT
);
353 req
.wIndex
= cpu_to_le16(iface_no
);
356 err
= cdc_ncm_do_request(ctx
, &req
, NULL
, 0, NULL
, 1000);
358 pr_debug("Setting NTB format to 16-bit failed\n");
361 ctx
->max_datagram_size
= CDC_NCM_MIN_DATAGRAM_SIZE
;
363 /* set Max Datagram Size (MTU) */
364 if (flags
& USB_CDC_NCM_NCAP_MAX_DATAGRAM_SIZE
) {
365 __le16 max_datagram_size
;
366 u16 eth_max_sz
= le16_to_cpu(ctx
->ether_desc
->wMaxSegmentSize
);
368 req
.bmRequestType
= USB_TYPE_CLASS
| USB_DIR_IN
|
370 req
.bNotificationType
= USB_CDC_GET_MAX_DATAGRAM_SIZE
;
372 req
.wIndex
= cpu_to_le16(iface_no
);
373 req
.wLength
= cpu_to_le16(2);
375 err
= cdc_ncm_do_request(ctx
, &req
, &max_datagram_size
, 0, NULL
,
378 pr_debug("GET_MAX_DATAGRAM_SIZE failed, use size=%u\n",
379 CDC_NCM_MIN_DATAGRAM_SIZE
);
381 ctx
->max_datagram_size
= le16_to_cpu(max_datagram_size
);
382 /* Check Eth descriptor value */
383 if (eth_max_sz
< CDC_NCM_MAX_DATAGRAM_SIZE
) {
384 if (ctx
->max_datagram_size
> eth_max_sz
)
385 ctx
->max_datagram_size
= eth_max_sz
;
387 if (ctx
->max_datagram_size
>
388 CDC_NCM_MAX_DATAGRAM_SIZE
)
389 ctx
->max_datagram_size
=
390 CDC_NCM_MAX_DATAGRAM_SIZE
;
393 if (ctx
->max_datagram_size
< CDC_NCM_MIN_DATAGRAM_SIZE
)
394 ctx
->max_datagram_size
=
395 CDC_NCM_MIN_DATAGRAM_SIZE
;
397 /* if value changed, update device */
398 req
.bmRequestType
= USB_TYPE_CLASS
| USB_DIR_OUT
|
400 req
.bNotificationType
= USB_CDC_SET_MAX_DATAGRAM_SIZE
;
402 req
.wIndex
= cpu_to_le16(iface_no
);
404 max_datagram_size
= cpu_to_le16(ctx
->max_datagram_size
);
406 err
= cdc_ncm_do_request(ctx
, &req
, &max_datagram_size
,
409 pr_debug("SET_MAX_DATAGRAM_SIZE failed\n");
414 if (ctx
->netdev
->mtu
!= (ctx
->max_datagram_size
- ETH_HLEN
))
415 ctx
->netdev
->mtu
= ctx
->max_datagram_size
- ETH_HLEN
;
421 cdc_ncm_find_endpoints(struct cdc_ncm_ctx
*ctx
, struct usb_interface
*intf
)
423 struct usb_host_endpoint
*e
;
426 for (ep
= 0; ep
< intf
->cur_altsetting
->desc
.bNumEndpoints
; ep
++) {
428 e
= intf
->cur_altsetting
->endpoint
+ ep
;
429 switch (e
->desc
.bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) {
430 case USB_ENDPOINT_XFER_INT
:
431 if (usb_endpoint_dir_in(&e
->desc
)) {
432 if (ctx
->status_ep
== NULL
)
437 case USB_ENDPOINT_XFER_BULK
:
438 if (usb_endpoint_dir_in(&e
->desc
)) {
439 if (ctx
->in_ep
== NULL
)
442 if (ctx
->out_ep
== NULL
)
453 static void cdc_ncm_free(struct cdc_ncm_ctx
*ctx
)
458 del_timer_sync(&ctx
->tx_timer
);
460 if (ctx
->tx_rem_skb
!= NULL
) {
461 dev_kfree_skb_any(ctx
->tx_rem_skb
);
462 ctx
->tx_rem_skb
= NULL
;
465 if (ctx
->tx_curr_skb
!= NULL
) {
466 dev_kfree_skb_any(ctx
->tx_curr_skb
);
467 ctx
->tx_curr_skb
= NULL
;
473 static int cdc_ncm_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
475 struct cdc_ncm_ctx
*ctx
;
476 struct usb_driver
*driver
;
482 ctx
= kmalloc(sizeof(*ctx
), GFP_KERNEL
);
486 memset(ctx
, 0, sizeof(*ctx
));
488 init_timer(&ctx
->tx_timer
);
489 spin_lock_init(&ctx
->mtx
);
490 ctx
->netdev
= dev
->net
;
492 /* store ctx pointer in device data field */
493 dev
->data
[0] = (unsigned long)ctx
;
495 /* get some pointers */
496 driver
= driver_of(intf
);
497 buf
= intf
->cur_altsetting
->extra
;
498 len
= intf
->cur_altsetting
->extralen
;
500 ctx
->udev
= dev
->udev
;
503 /* parse through descriptors associated with control interface */
504 while ((len
> 0) && (buf
[0] > 2) && (buf
[0] <= len
)) {
506 if (buf
[1] != USB_DT_CS_INTERFACE
)
510 case USB_CDC_UNION_TYPE
:
511 if (buf
[0] < sizeof(*(ctx
->union_desc
)))
515 (const struct usb_cdc_union_desc
*)buf
;
517 ctx
->control
= usb_ifnum_to_if(dev
->udev
,
518 ctx
->union_desc
->bMasterInterface0
);
519 ctx
->data
= usb_ifnum_to_if(dev
->udev
,
520 ctx
->union_desc
->bSlaveInterface0
);
523 case USB_CDC_ETHERNET_TYPE
:
524 if (buf
[0] < sizeof(*(ctx
->ether_desc
)))
528 (const struct usb_cdc_ether_desc
*)buf
;
530 le16_to_cpu(ctx
->ether_desc
->wMaxSegmentSize
);
532 if (dev
->hard_mtu
< CDC_NCM_MIN_DATAGRAM_SIZE
)
533 dev
->hard_mtu
= CDC_NCM_MIN_DATAGRAM_SIZE
;
534 else if (dev
->hard_mtu
> CDC_NCM_MAX_DATAGRAM_SIZE
)
535 dev
->hard_mtu
= CDC_NCM_MAX_DATAGRAM_SIZE
;
538 case USB_CDC_NCM_TYPE
:
539 if (buf
[0] < sizeof(*(ctx
->func_desc
)))
542 ctx
->func_desc
= (const struct usb_cdc_ncm_desc
*)buf
;
549 /* advance to next descriptor */
555 /* check if we got everything */
556 if ((ctx
->control
== NULL
) || (ctx
->data
== NULL
) ||
557 (ctx
->ether_desc
== NULL
) || (ctx
->control
!= intf
))
560 /* claim interfaces, if any */
561 temp
= usb_driver_claim_interface(driver
, ctx
->data
, dev
);
565 iface_no
= ctx
->data
->cur_altsetting
->desc
.bInterfaceNumber
;
567 /* reset data interface */
568 temp
= usb_set_interface(dev
->udev
, iface_no
, 0);
572 /* initialize data interface */
573 if (cdc_ncm_setup(ctx
))
576 /* configure data interface */
577 temp
= usb_set_interface(dev
->udev
, iface_no
, 1);
581 cdc_ncm_find_endpoints(ctx
, ctx
->data
);
582 cdc_ncm_find_endpoints(ctx
, ctx
->control
);
584 if ((ctx
->in_ep
== NULL
) || (ctx
->out_ep
== NULL
) ||
585 (ctx
->status_ep
== NULL
))
588 dev
->net
->ethtool_ops
= &cdc_ncm_ethtool_ops
;
590 usb_set_intfdata(ctx
->data
, dev
);
591 usb_set_intfdata(ctx
->control
, dev
);
592 usb_set_intfdata(ctx
->intf
, dev
);
594 temp
= usbnet_get_ethernet_addr(dev
, ctx
->ether_desc
->iMACAddress
);
598 dev_info(&dev
->udev
->dev
, "MAC-Address: "
599 "0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x\n",
600 dev
->net
->dev_addr
[0], dev
->net
->dev_addr
[1],
601 dev
->net
->dev_addr
[2], dev
->net
->dev_addr
[3],
602 dev
->net
->dev_addr
[4], dev
->net
->dev_addr
[5]);
604 dev
->in
= usb_rcvbulkpipe(dev
->udev
,
605 ctx
->in_ep
->desc
.bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
606 dev
->out
= usb_sndbulkpipe(dev
->udev
,
607 ctx
->out_ep
->desc
.bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
608 dev
->status
= ctx
->status_ep
;
609 dev
->rx_urb_size
= ctx
->rx_max
;
612 * We should get an event when network connection is "connected" or
613 * "disconnected". Set network connection in "disconnected" state
614 * (carrier is OFF) during attach, so the IP network stack does not
615 * start IPv6 negotiation and more.
617 netif_carrier_off(dev
->net
);
618 ctx
->tx_speed
= ctx
->rx_speed
= 0;
622 usb_set_intfdata(ctx
->control
, NULL
);
623 usb_set_intfdata(ctx
->data
, NULL
);
624 usb_driver_release_interface(driver
, ctx
->data
);
626 cdc_ncm_free((struct cdc_ncm_ctx
*)dev
->data
[0]);
628 dev_info(&dev
->udev
->dev
, "bind() failure\n");
632 static void cdc_ncm_unbind(struct usbnet
*dev
, struct usb_interface
*intf
)
634 struct cdc_ncm_ctx
*ctx
= (struct cdc_ncm_ctx
*)dev
->data
[0];
635 struct usb_driver
*driver
= driver_of(intf
);
638 return; /* no setup */
640 /* disconnect master --> disconnect slave */
641 if (intf
== ctx
->control
&& ctx
->data
) {
642 usb_set_intfdata(ctx
->data
, NULL
);
643 usb_driver_release_interface(driver
, ctx
->data
);
646 } else if (intf
== ctx
->data
&& ctx
->control
) {
647 usb_set_intfdata(ctx
->control
, NULL
);
648 usb_driver_release_interface(driver
, ctx
->control
);
652 usb_set_intfdata(ctx
->intf
, NULL
);
656 static void cdc_ncm_zero_fill(u8
*ptr
, u32 first
, u32 end
, u32 max
)
664 memset(ptr
+ first
, 0, end
- first
);
667 static struct sk_buff
*
668 cdc_ncm_fill_tx_frame(struct cdc_ncm_ctx
*ctx
, struct sk_buff
*skb
)
670 struct sk_buff
*skb_out
;
677 /* if there is a remaining skb, it gets priority */
679 swap(skb
, ctx
->tx_rem_skb
);
691 /* check if we are resuming an OUT skb */
692 if (ctx
->tx_curr_skb
!= NULL
) {
694 skb_out
= ctx
->tx_curr_skb
;
695 offset
= ctx
->tx_curr_offset
;
696 last_offset
= ctx
->tx_curr_last_offset
;
697 n
= ctx
->tx_curr_frame_num
;
700 /* reset variables */
701 skb_out
= alloc_skb((ctx
->tx_max
+ 1), GFP_ATOMIC
);
702 if (skb_out
== NULL
) {
704 dev_kfree_skb_any(skb
);
705 ctx
->netdev
->stats
.tx_dropped
++;
710 /* make room for NTH and NDP */
711 offset
= ALIGN(sizeof(struct usb_cdc_ncm_nth16
),
712 ctx
->tx_ndp_modulus
) +
713 sizeof(struct usb_cdc_ncm_ndp16
) +
714 (ctx
->tx_max_datagrams
+ 1) *
715 sizeof(struct usb_cdc_ncm_dpe16
);
717 /* store last valid offset before alignment */
718 last_offset
= offset
;
719 /* align first Datagram offset correctly */
720 offset
= ALIGN(offset
, ctx
->tx_modulus
) + ctx
->tx_remainder
;
721 /* zero buffer till the first IP datagram */
722 cdc_ncm_zero_fill(skb_out
->data
, 0, offset
, offset
);
724 ctx
->tx_curr_frame_num
= 0;
727 for (; n
< ctx
->tx_max_datagrams
; n
++) {
728 /* check if end of transmit buffer is reached */
729 if (offset
>= ctx
->tx_max
) {
733 /* compute maximum buffer size */
734 rem
= ctx
->tx_max
- offset
;
737 skb
= ctx
->tx_rem_skb
;
738 ctx
->tx_rem_skb
= NULL
;
740 /* check for end of skb */
745 if (skb
->len
> rem
) {
747 /* won't fit, MTU problem? */
748 dev_kfree_skb_any(skb
);
750 ctx
->netdev
->stats
.tx_dropped
++;
752 /* no room for skb - store for later */
753 if (ctx
->tx_rem_skb
!= NULL
) {
754 dev_kfree_skb_any(ctx
->tx_rem_skb
);
755 ctx
->netdev
->stats
.tx_dropped
++;
757 ctx
->tx_rem_skb
= skb
;
764 memcpy(((u8
*)skb_out
->data
) + offset
, skb
->data
, skb
->len
);
766 ctx
->tx_ncm
.dpe16
[n
].wDatagramLength
= cpu_to_le16(skb
->len
);
767 ctx
->tx_ncm
.dpe16
[n
].wDatagramIndex
= cpu_to_le16(offset
);
772 /* store last valid offset before alignment */
773 last_offset
= offset
;
775 /* align offset correctly */
776 offset
= ALIGN(offset
, ctx
->tx_modulus
) + ctx
->tx_remainder
;
779 cdc_ncm_zero_fill(skb_out
->data
, last_offset
, offset
,
781 dev_kfree_skb_any(skb
);
785 /* free up any dangling skb */
787 dev_kfree_skb_any(skb
);
789 ctx
->netdev
->stats
.tx_dropped
++;
792 ctx
->tx_curr_frame_num
= n
;
795 /* wait for more frames */
797 ctx
->tx_curr_skb
= skb_out
;
798 ctx
->tx_curr_offset
= offset
;
799 ctx
->tx_curr_last_offset
= last_offset
;
802 } else if ((n
< ctx
->tx_max_datagrams
) && (ready2send
== 0)) {
803 /* wait for more frames */
805 ctx
->tx_curr_skb
= skb_out
;
806 ctx
->tx_curr_offset
= offset
;
807 ctx
->tx_curr_last_offset
= last_offset
;
808 /* set the pending count */
809 if (n
< CDC_NCM_RESTART_TIMER_DATAGRAM_CNT
)
810 ctx
->tx_timer_pending
= 2;
815 /* variables will be reset at next call */
818 /* check for overflow */
819 if (last_offset
> ctx
->tx_max
)
820 last_offset
= ctx
->tx_max
;
823 offset
= last_offset
;
826 * If collected data size is less or equal CDC_NCM_MIN_TX_PKT bytes,
827 * we send buffers as it is. If we get more data, it would be more
828 * efficient for USB HS mobile device with DMA engine to receive a full
829 * size NTB, than canceling DMA transfer and receiving a short packet.
831 if (offset
> CDC_NCM_MIN_TX_PKT
)
832 offset
= ctx
->tx_max
;
834 /* final zero padding */
835 cdc_ncm_zero_fill(skb_out
->data
, last_offset
, offset
, ctx
->tx_max
);
837 /* store last offset */
838 last_offset
= offset
;
840 if (((last_offset
< ctx
->tx_max
) && ((last_offset
%
841 le16_to_cpu(ctx
->out_ep
->desc
.wMaxPacketSize
)) == 0)) ||
842 (((last_offset
== ctx
->tx_max
) && ((ctx
->tx_max
%
843 le16_to_cpu(ctx
->out_ep
->desc
.wMaxPacketSize
)) == 0)) &&
844 (ctx
->tx_max
< le32_to_cpu(ctx
->ncm_parm
.dwNtbOutMaxSize
)))) {
845 /* force short packet */
846 *(((u8
*)skb_out
->data
) + last_offset
) = 0;
850 /* zero the rest of the DPEs plus the last NULL entry */
851 for (; n
<= CDC_NCM_DPT_DATAGRAMS_MAX
; n
++) {
852 ctx
->tx_ncm
.dpe16
[n
].wDatagramLength
= 0;
853 ctx
->tx_ncm
.dpe16
[n
].wDatagramIndex
= 0;
856 /* fill out 16-bit NTB header */
857 ctx
->tx_ncm
.nth16
.dwSignature
= cpu_to_le32(USB_CDC_NCM_NTH16_SIGN
);
858 ctx
->tx_ncm
.nth16
.wHeaderLength
=
859 cpu_to_le16(sizeof(ctx
->tx_ncm
.nth16
));
860 ctx
->tx_ncm
.nth16
.wSequence
= cpu_to_le16(ctx
->tx_seq
);
861 ctx
->tx_ncm
.nth16
.wBlockLength
= cpu_to_le16(last_offset
);
862 ctx
->tx_ncm
.nth16
.wNdpIndex
= ALIGN(sizeof(struct usb_cdc_ncm_nth16
),
863 ctx
->tx_ndp_modulus
);
865 memcpy(skb_out
->data
, &(ctx
->tx_ncm
.nth16
), sizeof(ctx
->tx_ncm
.nth16
));
868 /* fill out 16-bit NDP table */
869 ctx
->tx_ncm
.ndp16
.dwSignature
=
870 cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN
);
871 rem
= sizeof(ctx
->tx_ncm
.ndp16
) + ((ctx
->tx_curr_frame_num
+ 1) *
872 sizeof(struct usb_cdc_ncm_dpe16
));
873 ctx
->tx_ncm
.ndp16
.wLength
= cpu_to_le16(rem
);
874 ctx
->tx_ncm
.ndp16
.wNextNdpIndex
= 0; /* reserved */
876 memcpy(((u8
*)skb_out
->data
) + ctx
->tx_ncm
.nth16
.wNdpIndex
,
877 &(ctx
->tx_ncm
.ndp16
),
878 sizeof(ctx
->tx_ncm
.ndp16
));
880 memcpy(((u8
*)skb_out
->data
) + ctx
->tx_ncm
.nth16
.wNdpIndex
+
881 sizeof(ctx
->tx_ncm
.ndp16
),
882 &(ctx
->tx_ncm
.dpe16
),
883 (ctx
->tx_curr_frame_num
+ 1) *
884 sizeof(struct usb_cdc_ncm_dpe16
));
886 /* set frame length */
887 skb_put(skb_out
, last_offset
);
890 ctx
->tx_curr_skb
= NULL
;
897 static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx
*ctx
)
899 /* start timer, if not already started */
900 if (timer_pending(&ctx
->tx_timer
) == 0) {
901 ctx
->tx_timer
.function
= &cdc_ncm_tx_timeout
;
902 ctx
->tx_timer
.data
= (unsigned long)ctx
;
903 ctx
->tx_timer
.expires
= jiffies
+ ((HZ
+ 999) / 1000);
904 add_timer(&ctx
->tx_timer
);
908 static void cdc_ncm_tx_timeout(unsigned long arg
)
910 struct cdc_ncm_ctx
*ctx
= (struct cdc_ncm_ctx
*)arg
;
913 spin_lock(&ctx
->mtx
);
914 if (ctx
->tx_timer_pending
!= 0) {
915 ctx
->tx_timer_pending
--;
921 spin_unlock(&ctx
->mtx
);
924 spin_lock(&ctx
->mtx
);
925 cdc_ncm_tx_timeout_start(ctx
);
926 spin_unlock(&ctx
->mtx
);
927 } else if (ctx
->netdev
!= NULL
) {
928 usbnet_start_xmit(NULL
, ctx
->netdev
);
932 static struct sk_buff
*
933 cdc_ncm_tx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
, gfp_t flags
)
935 struct sk_buff
*skb_out
;
936 struct cdc_ncm_ctx
*ctx
= (struct cdc_ncm_ctx
*)dev
->data
[0];
940 * The Ethernet API we are using does not support transmitting
941 * multiple Ethernet frames in a single call. This driver will
942 * accumulate multiple Ethernet frames and send out a larger
943 * USB frame when the USB buffer is full or when a single jiffies
949 spin_lock(&ctx
->mtx
);
950 skb_out
= cdc_ncm_fill_tx_frame(ctx
, skb
);
951 if (ctx
->tx_curr_skb
!= NULL
)
954 /* Start timer, if there is a remaining skb */
956 cdc_ncm_tx_timeout_start(ctx
);
959 dev
->net
->stats
.tx_packets
+= ctx
->tx_curr_frame_num
;
961 spin_unlock(&ctx
->mtx
);
966 dev_kfree_skb_any(skb
);
971 static int cdc_ncm_rx_fixup(struct usbnet
*dev
, struct sk_buff
*skb_in
)
974 struct cdc_ncm_ctx
*ctx
;
982 ctx
= (struct cdc_ncm_ctx
*)dev
->data
[0];
986 actlen
= skb_in
->len
;
987 sumlen
= CDC_NCM_NTB_MAX_SIZE_RX
;
989 if (actlen
< (sizeof(ctx
->rx_ncm
.nth16
) + sizeof(ctx
->rx_ncm
.ndp16
))) {
990 pr_debug("frame too short\n");
994 memcpy(&(ctx
->rx_ncm
.nth16
), ((u8
*)skb_in
->data
),
995 sizeof(ctx
->rx_ncm
.nth16
));
997 if (le32_to_cpu(ctx
->rx_ncm
.nth16
.dwSignature
) !=
998 USB_CDC_NCM_NTH16_SIGN
) {
999 pr_debug("invalid NTH16 signature <%u>\n",
1000 le32_to_cpu(ctx
->rx_ncm
.nth16
.dwSignature
));
1004 temp
= le16_to_cpu(ctx
->rx_ncm
.nth16
.wBlockLength
);
1005 if (temp
> sumlen
) {
1006 pr_debug("unsupported NTB block length %u/%u\n", temp
, sumlen
);
1010 temp
= le16_to_cpu(ctx
->rx_ncm
.nth16
.wNdpIndex
);
1011 if ((temp
+ sizeof(ctx
->rx_ncm
.ndp16
)) > actlen
) {
1012 pr_debug("invalid DPT16 index\n");
1016 memcpy(&(ctx
->rx_ncm
.ndp16
), ((u8
*)skb_in
->data
) + temp
,
1017 sizeof(ctx
->rx_ncm
.ndp16
));
1019 if (le32_to_cpu(ctx
->rx_ncm
.ndp16
.dwSignature
) !=
1020 USB_CDC_NCM_NDP16_NOCRC_SIGN
) {
1021 pr_debug("invalid DPT16 signature <%u>\n",
1022 le32_to_cpu(ctx
->rx_ncm
.ndp16
.dwSignature
));
1026 if (le16_to_cpu(ctx
->rx_ncm
.ndp16
.wLength
) <
1027 USB_CDC_NCM_NDP16_LENGTH_MIN
) {
1028 pr_debug("invalid DPT16 length <%u>\n",
1029 le32_to_cpu(ctx
->rx_ncm
.ndp16
.dwSignature
));
1033 nframes
= ((le16_to_cpu(ctx
->rx_ncm
.ndp16
.wLength
) -
1034 sizeof(struct usb_cdc_ncm_ndp16
)) /
1035 sizeof(struct usb_cdc_ncm_dpe16
));
1036 nframes
--; /* we process NDP entries except for the last one */
1038 pr_debug("nframes = %u\n", nframes
);
1040 temp
+= sizeof(ctx
->rx_ncm
.ndp16
);
1042 if ((temp
+ nframes
* (sizeof(struct usb_cdc_ncm_dpe16
))) > actlen
) {
1043 pr_debug("Invalid nframes = %d\n", nframes
);
1047 if (nframes
> CDC_NCM_DPT_DATAGRAMS_MAX
) {
1048 pr_debug("Truncating number of frames from %u to %u\n",
1049 nframes
, CDC_NCM_DPT_DATAGRAMS_MAX
);
1050 nframes
= CDC_NCM_DPT_DATAGRAMS_MAX
;
1053 memcpy(&(ctx
->rx_ncm
.dpe16
), ((u8
*)skb_in
->data
) + temp
,
1054 nframes
* (sizeof(struct usb_cdc_ncm_dpe16
)));
1056 for (x
= 0; x
< nframes
; x
++) {
1057 offset
= le16_to_cpu(ctx
->rx_ncm
.dpe16
[x
].wDatagramIndex
);
1058 temp
= le16_to_cpu(ctx
->rx_ncm
.dpe16
[x
].wDatagramLength
);
1062 * All entries after first NULL entry are to be ignored
1064 if ((offset
== 0) || (temp
== 0)) {
1066 goto error
; /* empty NTB */
1070 /* sanity checking */
1071 if (((offset
+ temp
) > actlen
) ||
1072 (temp
> CDC_NCM_MAX_DATAGRAM_SIZE
) || (temp
< ETH_HLEN
)) {
1073 pr_debug("invalid frame detected (ignored)"
1074 "offset[%u]=%u, length=%u, skb=%p\n",
1075 x
, offset
, temp
, skb_in
);
1081 skb
= skb_clone(skb_in
, GFP_ATOMIC
);
1085 skb
->data
= ((u8
*)skb_in
->data
) + offset
;
1086 skb_set_tail_pointer(skb
, temp
);
1087 usbnet_skb_return(dev
, skb
);
1096 cdc_ncm_speed_change(struct cdc_ncm_ctx
*ctx
,
1097 struct usb_cdc_speed_change
*data
)
1099 uint32_t rx_speed
= le32_to_cpu(data
->DLBitRRate
);
1100 uint32_t tx_speed
= le32_to_cpu(data
->ULBitRate
);
1103 * Currently the USB-NET API does not support reporting the actual
1104 * device speed. Do print it instead.
1106 if ((tx_speed
!= ctx
->tx_speed
) || (rx_speed
!= ctx
->rx_speed
)) {
1107 ctx
->tx_speed
= tx_speed
;
1108 ctx
->rx_speed
= rx_speed
;
1110 if ((tx_speed
> 1000000) && (rx_speed
> 1000000)) {
1111 printk(KERN_INFO KBUILD_MODNAME
1112 ": %s: %u mbit/s downlink "
1113 "%u mbit/s uplink\n",
1115 (unsigned int)(rx_speed
/ 1000000U),
1116 (unsigned int)(tx_speed
/ 1000000U));
1118 printk(KERN_INFO KBUILD_MODNAME
1119 ": %s: %u kbit/s downlink "
1120 "%u kbit/s uplink\n",
1122 (unsigned int)(rx_speed
/ 1000U),
1123 (unsigned int)(tx_speed
/ 1000U));
1128 static void cdc_ncm_status(struct usbnet
*dev
, struct urb
*urb
)
1130 struct cdc_ncm_ctx
*ctx
;
1131 struct usb_cdc_notification
*event
;
1133 ctx
= (struct cdc_ncm_ctx
*)dev
->data
[0];
1135 if (urb
->actual_length
< sizeof(*event
))
1138 /* test for split data in 8-byte chunks */
1139 if (test_and_clear_bit(EVENT_STS_SPLIT
, &dev
->flags
)) {
1140 cdc_ncm_speed_change(ctx
,
1141 (struct usb_cdc_speed_change
*)urb
->transfer_buffer
);
1145 event
= urb
->transfer_buffer
;
1147 switch (event
->bNotificationType
) {
1148 case USB_CDC_NOTIFY_NETWORK_CONNECTION
:
1150 * According to the CDC NCM specification ch.7.1
1151 * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be
1152 * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE.
1154 ctx
->connected
= event
->wValue
;
1156 printk(KERN_INFO KBUILD_MODNAME
": %s: network connection:"
1158 ctx
->netdev
->name
, ctx
->connected
? "" : "dis");
1161 netif_carrier_on(dev
->net
);
1163 netif_carrier_off(dev
->net
);
1164 ctx
->tx_speed
= ctx
->rx_speed
= 0;
1168 case USB_CDC_NOTIFY_SPEED_CHANGE
:
1169 if (urb
->actual_length
< (sizeof(*event
) +
1170 sizeof(struct usb_cdc_speed_change
)))
1171 set_bit(EVENT_STS_SPLIT
, &dev
->flags
);
1173 cdc_ncm_speed_change(ctx
,
1174 (struct usb_cdc_speed_change
*) &event
[1]);
1178 dev_err(&dev
->udev
->dev
, "NCM: unexpected "
1179 "notification 0x%02x!\n", event
->bNotificationType
);
1184 static int cdc_ncm_check_connect(struct usbnet
*dev
)
1186 struct cdc_ncm_ctx
*ctx
;
1188 ctx
= (struct cdc_ncm_ctx
*)dev
->data
[0];
1190 return 1; /* disconnected */
1192 return !ctx
->connected
;
1196 cdc_ncm_probe(struct usb_interface
*udev
, const struct usb_device_id
*prod
)
1198 return usbnet_probe(udev
, prod
);
1201 static void cdc_ncm_disconnect(struct usb_interface
*intf
)
1203 struct usbnet
*dev
= usb_get_intfdata(intf
);
1206 return; /* already disconnected */
1208 usbnet_disconnect(intf
);
1211 static int cdc_ncm_manage_power(struct usbnet
*dev
, int status
)
1213 dev
->intf
->needs_remote_wakeup
= status
;
1217 static const struct driver_info cdc_ncm_info
= {
1218 .description
= "CDC NCM",
1219 .flags
= FLAG_POINTTOPOINT
| FLAG_NO_SETINT
| FLAG_MULTI_PACKET
,
1220 .bind
= cdc_ncm_bind
,
1221 .unbind
= cdc_ncm_unbind
,
1222 .check_connect
= cdc_ncm_check_connect
,
1223 .manage_power
= cdc_ncm_manage_power
,
1224 .status
= cdc_ncm_status
,
1225 .rx_fixup
= cdc_ncm_rx_fixup
,
1226 .tx_fixup
= cdc_ncm_tx_fixup
,
1229 static struct usb_driver cdc_ncm_driver
= {
1231 .id_table
= cdc_devs
,
1232 .probe
= cdc_ncm_probe
,
1233 .disconnect
= cdc_ncm_disconnect
,
1234 .suspend
= usbnet_suspend
,
1235 .resume
= usbnet_resume
,
1236 .reset_resume
= usbnet_resume
,
1237 .supports_autosuspend
= 1,
1240 static struct ethtool_ops cdc_ncm_ethtool_ops
= {
1241 .get_drvinfo
= cdc_ncm_get_drvinfo
,
1242 .get_link
= usbnet_get_link
,
1243 .get_msglevel
= usbnet_get_msglevel
,
1244 .set_msglevel
= usbnet_set_msglevel
,
1245 .get_settings
= usbnet_get_settings
,
1246 .set_settings
= usbnet_set_settings
,
1247 .nway_reset
= usbnet_nway_reset
,
1250 static int __init
cdc_ncm_init(void)
1252 printk(KERN_INFO KBUILD_MODNAME
": " DRIVER_VERSION
"\n");
1253 return usb_register(&cdc_ncm_driver
);
1256 module_init(cdc_ncm_init
);
1258 static void __exit
cdc_ncm_exit(void)
1260 usb_deregister(&cdc_ncm_driver
);
1263 module_exit(cdc_ncm_exit
);
1265 MODULE_AUTHOR("Hans Petter Selasky");
1266 MODULE_DESCRIPTION("USB CDC NCM host driver");
1267 MODULE_LICENSE("Dual BSD/GPL");