xref: /openbmc/linux/include/linux/usb/cdc_ncm.h (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
2c91ce3b6SBjørn Mork /*
3c91ce3b6SBjørn Mork  * Copyright (C) ST-Ericsson 2010-2012
4c91ce3b6SBjørn Mork  * Contact: Alexey Orishko <alexey.orishko@stericsson.com>
5c91ce3b6SBjørn Mork  * Original author: Hans Petter Selasky <hans.petter.selasky@stericsson.com>
6c91ce3b6SBjørn Mork  *
7c91ce3b6SBjørn Mork  * USB Host Driver for Network Control Model (NCM)
8c91ce3b6SBjørn Mork  * http://www.usb.org/developers/devclass_docs/NCM10.zip
9c91ce3b6SBjørn Mork  *
10c91ce3b6SBjørn Mork  * The NCM encoding, decoding and initialization logic
11c91ce3b6SBjørn Mork  * derives from FreeBSD 8.x. if_cdce.c and if_cdcereg.h
12c91ce3b6SBjørn Mork  *
13c91ce3b6SBjørn Mork  * This software is available to you under a choice of one of two
14c91ce3b6SBjørn Mork  * licenses. You may choose this file to be licensed under the terms
15c91ce3b6SBjørn Mork  * of the GNU General Public License (GPL) Version 2 or the 2-clause
16c91ce3b6SBjørn Mork  * BSD license listed below:
17c91ce3b6SBjørn Mork  *
18c91ce3b6SBjørn Mork  * Redistribution and use in source and binary forms, with or without
19c91ce3b6SBjørn Mork  * modification, are permitted provided that the following conditions
20c91ce3b6SBjørn Mork  * are met:
21c91ce3b6SBjørn Mork  * 1. Redistributions of source code must retain the above copyright
22c91ce3b6SBjørn Mork  *    notice, this list of conditions and the following disclaimer.
23c91ce3b6SBjørn Mork  * 2. Redistributions in binary form must reproduce the above copyright
24c91ce3b6SBjørn Mork  *    notice, this list of conditions and the following disclaimer in the
25c91ce3b6SBjørn Mork  *    documentation and/or other materials provided with the distribution.
26c91ce3b6SBjørn Mork  *
27c91ce3b6SBjørn Mork  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
28c91ce3b6SBjørn Mork  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29c91ce3b6SBjørn Mork  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30c91ce3b6SBjørn Mork  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
31c91ce3b6SBjørn Mork  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32c91ce3b6SBjørn Mork  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33c91ce3b6SBjørn Mork  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34c91ce3b6SBjørn Mork  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35c91ce3b6SBjørn Mork  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36c91ce3b6SBjørn Mork  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37c91ce3b6SBjørn Mork  * SUCH DAMAGE.
38c91ce3b6SBjørn Mork  */
39c91ce3b6SBjørn Mork 
40f6701d5fSBjørn Mork #ifndef __LINUX_USB_CDC_NCM_H
41f6701d5fSBjørn Mork #define __LINUX_USB_CDC_NCM_H
42f6701d5fSBjørn Mork 
439bf211a3SGreg Suarez #define CDC_NCM_COMM_ALTSETTING_NCM		0
449bf211a3SGreg Suarez #define CDC_NCM_COMM_ALTSETTING_MBIM		1
459bf211a3SGreg Suarez 
469bf211a3SGreg Suarez #define CDC_NCM_DATA_ALTSETTING_NCM		1
479bf211a3SGreg Suarez #define CDC_NCM_DATA_ALTSETTING_MBIM		2
489bf211a3SGreg Suarez 
490fa81b30SAlexander Bersenev /* CDC NCM subclass 3.3.1 */
50c91ce3b6SBjørn Mork #define USB_CDC_NCM_NDP16_LENGTH_MIN		0x10
51c91ce3b6SBjørn Mork 
520fa81b30SAlexander Bersenev /* CDC NCM subclass 3.3.2 */
530fa81b30SAlexander Bersenev #define USB_CDC_NCM_NDP32_LENGTH_MIN		0x20
540fa81b30SAlexander Bersenev 
55c91ce3b6SBjørn Mork /* Maximum NTB length */
56*5588d628SŁukasz Spintzyk #define	CDC_NCM_NTB_MAX_SIZE_TX			65536	/* bytes */
57*5588d628SŁukasz Spintzyk #define	CDC_NCM_NTB_MAX_SIZE_RX			65536	/* bytes */
58c91ce3b6SBjørn Mork 
5950f1cb1cSBjørn Mork /* Initial NTB length */
6050f1cb1cSBjørn Mork #define	CDC_NCM_NTB_DEF_SIZE_TX			16384	/* bytes */
6150f1cb1cSBjørn Mork #define	CDC_NCM_NTB_DEF_SIZE_RX			16384	/* bytes */
6250f1cb1cSBjørn Mork 
63c91ce3b6SBjørn Mork /* Minimum value for MaxDatagramSize, ch. 6.2.9 */
64c91ce3b6SBjørn Mork #define	CDC_NCM_MIN_DATAGRAM_SIZE		1514	/* bytes */
65c91ce3b6SBjørn Mork 
66c91ce3b6SBjørn Mork /* Minimum value for MaxDatagramSize, ch. 8.1.3 */
67c91ce3b6SBjørn Mork #define CDC_MBIM_MIN_DATAGRAM_SIZE		2048	/* bytes */
68c91ce3b6SBjørn Mork 
69c91ce3b6SBjørn Mork #define	CDC_NCM_MIN_TX_PKT			512	/* bytes */
70c91ce3b6SBjørn Mork 
71c91ce3b6SBjørn Mork /* Default value for MaxDatagramSize */
72c91ce3b6SBjørn Mork #define	CDC_NCM_MAX_DATAGRAM_SIZE		8192	/* bytes */
73c91ce3b6SBjørn Mork 
74c91ce3b6SBjørn Mork /*
75c91ce3b6SBjørn Mork  * Maximum amount of datagrams in NCM Datagram Pointer Table, not counting
76c91ce3b6SBjørn Mork  * the last NULL entry.
77c91ce3b6SBjørn Mork  */
78c91ce3b6SBjørn Mork #define	CDC_NCM_DPT_DATAGRAMS_MAX		40
79c91ce3b6SBjørn Mork 
80c91ce3b6SBjørn Mork /* Restart the timer, if amount of datagrams is less than given value */
81c91ce3b6SBjørn Mork #define	CDC_NCM_RESTART_TIMER_DATAGRAM_CNT	3
82c91ce3b6SBjørn Mork #define	CDC_NCM_TIMER_PENDING_CNT		2
836c4e548fSBjørn Mork #define CDC_NCM_TIMER_INTERVAL_USEC		400UL
846c4e548fSBjørn Mork #define CDC_NCM_TIMER_INTERVAL_MIN		5UL
857d10d261SBjørn Mork #define CDC_NCM_TIMER_INTERVAL_MAX		(U32_MAX / NSEC_PER_USEC)
86c91ce3b6SBjørn Mork 
874a0e3e98SEnrico Mioso /* Driver flags */
884a0e3e98SEnrico Mioso #define CDC_NCM_FLAG_NDP_TO_END			0x02	/* NDP is placed at end of frame */
897b8076ceSDaniele Palmas #define CDC_MBIM_FLAG_AVOID_ALTSETTING_TOGGLE	0x04	/* Avoid altsetting toggle during init */
900fa81b30SAlexander Bersenev #define CDC_NCM_FLAG_PREFER_NTB32 0x08	/* prefer NDP32 over NDP16 */
914a0e3e98SEnrico Mioso 
929bf211a3SGreg Suarez #define cdc_ncm_comm_intf_is_mbim(x)  ((x)->desc.bInterfaceSubClass == USB_CDC_SUBCLASS_MBIM && \
939bf211a3SGreg Suarez 				       (x)->desc.bInterfaceProtocol == USB_CDC_PROTO_NONE)
949bf211a3SGreg Suarez #define cdc_ncm_data_intf_is_mbim(x)  ((x)->desc.bInterfaceProtocol == USB_CDC_MBIM_PROTO_NTB)
959bf211a3SGreg Suarez 
96c91ce3b6SBjørn Mork struct cdc_ncm_ctx {
97ff0992e9SBjørn Mork 	struct usb_cdc_ncm_ntb_parameters ncm_parm;
98c91ce3b6SBjørn Mork 	struct hrtimer tx_timer;
99c91ce3b6SBjørn Mork 	struct tasklet_struct bh;
100c91ce3b6SBjørn Mork 
1014f4e5436SEmil Renner Berthing 	struct usbnet *dev;
1024f4e5436SEmil Renner Berthing 
103c91ce3b6SBjørn Mork 	const struct usb_cdc_ncm_desc *func_desc;
104c91ce3b6SBjørn Mork 	const struct usb_cdc_mbim_desc *mbim_desc;
105259fef03SBen Chan 	const struct usb_cdc_mbim_extended_desc *mbim_extended_desc;
106c91ce3b6SBjørn Mork 	const struct usb_cdc_ether_desc *ether_desc;
107c91ce3b6SBjørn Mork 
108c91ce3b6SBjørn Mork 	struct usb_interface *control;
109c91ce3b6SBjørn Mork 	struct usb_interface *data;
110c91ce3b6SBjørn Mork 
111c91ce3b6SBjørn Mork 	struct sk_buff *tx_curr_skb;
112c91ce3b6SBjørn Mork 	struct sk_buff *tx_rem_skb;
113c91ce3b6SBjørn Mork 	__le32 tx_rem_sign;
114c91ce3b6SBjørn Mork 
115c91ce3b6SBjørn Mork 	spinlock_t mtx;
116c91ce3b6SBjørn Mork 	atomic_t stop;
1174a0e3e98SEnrico Mioso 	int drvflags;
118c91ce3b6SBjørn Mork 
1197d10d261SBjørn Mork 	u32 timer_interval;
12070559b89SBjørn Mork 	u32 max_ndp_size;
1210fa81b30SAlexander Bersenev 	u8 is_ndp16;
1220fa81b30SAlexander Bersenev 	union {
1234a0e3e98SEnrico Mioso 		struct usb_cdc_ncm_ndp16 *delayed_ndp16;
1240fa81b30SAlexander Bersenev 		struct usb_cdc_ncm_ndp32 *delayed_ndp32;
1250fa81b30SAlexander Bersenev 	};
1266c4e548fSBjørn Mork 
127c91ce3b6SBjørn Mork 	u32 tx_timer_pending;
128c91ce3b6SBjørn Mork 	u32 tx_curr_frame_num;
129c91ce3b6SBjørn Mork 	u32 rx_max;
130c91ce3b6SBjørn Mork 	u32 tx_max;
131e1069bbfSJim Baxter 	u32 tx_curr_size;
132e1069bbfSJim Baxter 	u32 tx_low_mem_max_cnt;
133e1069bbfSJim Baxter 	u32 tx_low_mem_val;
134c91ce3b6SBjørn Mork 	u32 max_datagram_size;
135c91ce3b6SBjørn Mork 	u16 tx_max_datagrams;
136c91ce3b6SBjørn Mork 	u16 tx_remainder;
137c91ce3b6SBjørn Mork 	u16 tx_modulus;
138c91ce3b6SBjørn Mork 	u16 tx_ndp_modulus;
139c91ce3b6SBjørn Mork 	u16 tx_seq;
140c91ce3b6SBjørn Mork 	u16 rx_seq;
14143e4c6dfSBjørn Mork 	u16 min_tx_pkt;
142beeecd42SBjørn Mork 
143beeecd42SBjørn Mork 	/* statistics */
144beeecd42SBjørn Mork 	u32 tx_curr_frame_payload;
145beeecd42SBjørn Mork 	u32 tx_reason_ntb_full;
146beeecd42SBjørn Mork 	u32 tx_reason_ndp_full;
147beeecd42SBjørn Mork 	u32 tx_reason_timeout;
148beeecd42SBjørn Mork 	u32 tx_reason_max_datagram;
149beeecd42SBjørn Mork 	u64 tx_overhead;
150beeecd42SBjørn Mork 	u64 tx_ntbs;
151beeecd42SBjørn Mork 	u64 rx_overhead;
152beeecd42SBjørn Mork 	u64 rx_ntbs;
153c91ce3b6SBjørn Mork };
154c91ce3b6SBjørn Mork 
15550a0ffafSBjørn Mork u8 cdc_ncm_select_altsetting(struct usb_interface *intf);
1561dfddff5SBjørn Mork int cdc_ncm_change_mtu(struct net_device *net, int new_mtu);
1574a0e3e98SEnrico Mioso int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_altsetting, int drvflags);
1586dd13e83SBjørn Mork void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf);
1596dd13e83SBjørn Mork struct sk_buff *cdc_ncm_fill_tx_frame(struct usbnet *dev, struct sk_buff *skb, __le32 sign);
1606dd13e83SBjørn Mork int cdc_ncm_rx_verify_nth16(struct cdc_ncm_ctx *ctx, struct sk_buff *skb_in);
1616dd13e83SBjørn Mork int cdc_ncm_rx_verify_ndp16(struct sk_buff *skb_in, int ndpoffset);
1620fa81b30SAlexander Bersenev int cdc_ncm_rx_verify_nth32(struct cdc_ncm_ctx *ctx, struct sk_buff *skb_in);
1630fa81b30SAlexander Bersenev int cdc_ncm_rx_verify_ndp32(struct sk_buff *skb_in, int ndpoffset);
1642f69702cSEnrico Mioso struct sk_buff *
1652f69702cSEnrico Mioso cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags);
1662f69702cSEnrico Mioso int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in);
167f6701d5fSBjørn Mork 
168f6701d5fSBjørn Mork #endif /* __LINUX_USB_CDC_NCM_H */
169