1/* SPDX-License-Identifier: GPL-2.0 */
2
3/* Driver for ETAS GmbH ES58X USB CAN(-FD) Bus Interfaces.
4 *
5 * File es581_4.h: Definitions and declarations specific to ETAS
6 * ES581.4.
7 *
8 * Copyright (c) 2019 Robert Bosch Engineering and Business Solutions. All rights reserved.
9 * Copyright (c) 2020 ETAS K.K.. All rights reserved.
10 * Copyright (c) 2020, 2021 Vincent Mailhol <mailhol.vincent@wanadoo.fr>
11 */
12
13#ifndef __ES581_4_H__
14#define __ES581_4_H__
15
16#include <linux/types.h>
17
18#define ES581_4_NUM_CAN_CH 2
19#define ES581_4_CHANNEL_IDX_OFFSET 1
20
21#define ES581_4_TX_BULK_MAX 25
22#define ES581_4_RX_BULK_MAX 30
23#define ES581_4_ECHO_BULK_MAX 30
24
25enum es581_4_cmd_type {
26	ES581_4_CAN_COMMAND_TYPE = 0x45
27};
28
29enum es581_4_cmd_id {
30	ES581_4_CMD_ID_OPEN_CHANNEL = 0x01,
31	ES581_4_CMD_ID_CLOSE_CHANNEL = 0x02,
32	ES581_4_CMD_ID_SET_BITTIMING = 0x03,
33	ES581_4_CMD_ID_ENABLE_CHANNEL = 0x04,
34	ES581_4_CMD_ID_TX_MSG = 0x05,
35	ES581_4_CMD_ID_RX_MSG = 0x06,
36	ES581_4_CMD_ID_RESET_RX = 0x0A,
37	ES581_4_CMD_ID_RESET_TX = 0x0B,
38	ES581_4_CMD_ID_DISABLE_CHANNEL = 0x0C,
39	ES581_4_CMD_ID_TIMESTAMP = 0x0E,
40	ES581_4_CMD_ID_RESET_DEVICE = 0x28,
41	ES581_4_CMD_ID_ECHO = 0x71,
42	ES581_4_CMD_ID_DEVICE_ERR = 0x72
43};
44
45enum es581_4_rx_type {
46	ES581_4_RX_TYPE_MESSAGE = 1,
47	ES581_4_RX_TYPE_ERROR = 3,
48	ES581_4_RX_TYPE_EVENT = 4
49};
50
51/**
52 * struct es581_4_tx_conf_msg - Channel configuration.
53 * @bitrate: Bitrate.
54 * @sample_point: Sample point is in percent [0..100].
55 * @samples_per_bit: type enum es58x_samples_per_bit.
56 * @bit_time: Number of time quanta in one bit.
57 * @sjw: Synchronization Jump Width.
58 * @sync_edge: type enum es58x_sync_edge.
59 * @physical_layer: type enum es58x_physical_layer.
60 * @echo_mode: type enum es58x_echo_mode.
61 * @channel_no: Channel number, starting from 1. Not to be confused
62 *	with channed_idx of the ES58X FD which starts from 0.
63 */
64struct es581_4_tx_conf_msg {
65	__le32 bitrate;
66	__le32 sample_point;
67	__le32 samples_per_bit;
68	__le32 bit_time;
69	__le32 sjw;
70	__le32 sync_edge;
71	__le32 physical_layer;
72	__le32 echo_mode;
73	u8 channel_no;
74} __packed;
75
76struct es581_4_tx_can_msg {
77	__le32 can_id;
78	__le32 packet_idx;
79	__le16 flags;
80	u8 channel_no;
81	u8 dlc;
82	u8 data[CAN_MAX_DLEN];
83} __packed;
84
85/* The ES581.4 allows bulk transfer.  */
86struct es581_4_bulk_tx_can_msg {
87	u8 num_can_msg;
88	/* Using type "u8[]" instead of "struct es581_4_tx_can_msg[]"
89	 * for tx_msg_buf because each member has a flexible size.
90	 */
91	u8 tx_can_msg_buf[ES581_4_TX_BULK_MAX *
92			  sizeof(struct es581_4_tx_can_msg)];
93} __packed;
94
95struct es581_4_echo_msg {
96	__le64 timestamp;
97	__le32 packet_idx;
98} __packed;
99
100struct es581_4_bulk_echo_msg {
101	u8 channel_no;
102	struct es581_4_echo_msg echo_msg[ES581_4_ECHO_BULK_MAX];
103} __packed;
104
105/* Normal Rx CAN Message */
106struct es581_4_rx_can_msg {
107	__le64 timestamp;
108	u8 rx_type;		/* type enum es581_4_rx_type */
109	u8 flags;		/* type enum es58x_flag */
110	u8 channel_no;
111	u8 dlc;
112	__le32 can_id;
113	u8 data[CAN_MAX_DLEN];
114} __packed;
115
116struct es581_4_rx_err_msg {
117	__le64 timestamp;
118	__le16 rx_type;		/* type enum es581_4_rx_type */
119	__le16 flags;		/* type enum es58x_flag */
120	u8 channel_no;
121	u8 __padding[2];
122	u8 dlc;
123	__le32 tag;		/* Related to the CAN filtering. Unused in this module */
124	__le32 can_id;
125	__le32 error;		/* type enum es58x_error */
126	__le32 destination;	/* Unused in this module */
127} __packed;
128
129struct es581_4_rx_event_msg {
130	__le64 timestamp;
131	__le16 rx_type;		/* type enum es581_4_rx_type */
132	u8 channel_no;
133	u8 __padding;
134	__le32 tag;		/* Related to the CAN filtering. Unused in this module */
135	__le32 event;		/* type enum es58x_event */
136	__le32 destination;	/* Unused in this module */
137} __packed;
138
139struct es581_4_tx_ack_msg {
140	__le16 tx_free_entries;	/* Number of remaining free entries in the device TX queue */
141	u8 channel_no;
142	u8 rx_cmd_ret_u8;	/* type enum es58x_cmd_ret_code_u8 */
143} __packed;
144
145struct es581_4_rx_cmd_ret {
146	__le32 rx_cmd_ret_le32;
147	u8 channel_no;
148	u8 __padding[3];
149} __packed;
150
151/**
152 * struct es581_4_urb_cmd - Commands received from or sent to the
153 *	ES581.4 device.
154 * @SOF: Start of Frame.
155 * @cmd_type: Command Type (type: enum es581_4_cmd_type). The CRC
156 *	calculation starts at this position.
157 * @cmd_id: Command ID (type: enum es581_4_cmd_id).
158 * @msg_len: Length of the message, excluding CRC (i.e. length of the
159 *	union).
160 * @tx_conf_msg: Channel configuration.
161 * @bulk_tx_can_msg: Tx messages.
162 * @rx_can_msg: Array of Rx messages.
163 * @bulk_echo_msg: Tx message being looped back.
164 * @rx_err_msg: Error message.
165 * @rx_event_msg: Event message.
166 * @tx_ack_msg: Tx acknowledgment message.
167 * @rx_cmd_ret: Command return code.
168 * @timestamp: Timestamp reply.
169 * @rx_cmd_ret_u8: Rx 8 bits return code (type: enum
170 *	es58x_cmd_ret_code_u8).
171 * @raw_msg: Message raw payload.
172 * @reserved_for_crc16_do_not_use: The structure ends with a
173 *	CRC16. Because the structures in above union are of variable
174 *	lengths, we can not predict the offset of the CRC in
175 *	advance. Use functions es58x_get_crc() and es58x_set_crc() to
176 *	manipulate it.
177 */
178struct es581_4_urb_cmd {
179	__le16 SOF;
180	u8 cmd_type;
181	u8 cmd_id;
182	__le16 msg_len;
183
184	union {
185		struct es581_4_tx_conf_msg tx_conf_msg;
186		struct es581_4_bulk_tx_can_msg bulk_tx_can_msg;
187		struct es581_4_rx_can_msg rx_can_msg[ES581_4_RX_BULK_MAX];
188		struct es581_4_bulk_echo_msg bulk_echo_msg;
189		struct es581_4_rx_err_msg rx_err_msg;
190		struct es581_4_rx_event_msg rx_event_msg;
191		struct es581_4_tx_ack_msg tx_ack_msg;
192		struct es581_4_rx_cmd_ret rx_cmd_ret;
193		__le64 timestamp;
194		u8 rx_cmd_ret_u8;
195		DECLARE_FLEX_ARRAY(u8, raw_msg);
196	} __packed;
197
198	__le16 reserved_for_crc16_do_not_use;
199} __packed;
200
201#define ES581_4_URB_CMD_HEADER_LEN (offsetof(struct es581_4_urb_cmd, raw_msg))
202#define ES581_4_TX_URB_CMD_MAX_LEN					\
203	ES58X_SIZEOF_URB_CMD(struct es581_4_urb_cmd, bulk_tx_can_msg)
204#define ES581_4_RX_URB_CMD_MAX_LEN					\
205	ES58X_SIZEOF_URB_CMD(struct es581_4_urb_cmd, rx_can_msg)
206
207#endif /* __ES581_4_H__ */
208