1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 /* Driver for ETAS GmbH ES58X USB CAN(-FD) Bus Interfaces.
4  *
5  * File es58x_core.h: All common definitions and declarations.
6  *
7  * Copyright (c) 2019 Robert Bosch Engineering and Business Solutions. All rights reserved.
8  * Copyright (c) 2020 ETAS K.K.. All rights reserved.
9  * Copyright (c) 2020, 2021 Vincent Mailhol <mailhol.vincent@wanadoo.fr>
10  */
11 
12 #ifndef __ES58X_COMMON_H__
13 #define __ES58X_COMMON_H__
14 
15 #include <linux/types.h>
16 #include <linux/usb.h>
17 #include <linux/netdevice.h>
18 #include <linux/can.h>
19 #include <linux/can/dev.h>
20 
21 #include "es581_4.h"
22 #include "es58x_fd.h"
23 
24 /* Driver constants */
25 #define ES58X_RX_URBS_MAX 5	/* Empirical value */
26 #define ES58X_TX_URBS_MAX 6	/* Empirical value */
27 
28 #define ES58X_MAX(param)				\
29 	(ES581_4_##param > ES58X_FD_##param ?		\
30 		ES581_4_##param : ES58X_FD_##param)
31 #define ES58X_TX_BULK_MAX ES58X_MAX(TX_BULK_MAX)
32 #define ES58X_RX_BULK_MAX ES58X_MAX(RX_BULK_MAX)
33 #define ES58X_ECHO_BULK_MAX ES58X_MAX(ECHO_BULK_MAX)
34 #define ES58X_NUM_CAN_CH_MAX ES58X_MAX(NUM_CAN_CH)
35 
36 /* Use this when channel index is irrelevant (e.g. device
37  * timestamp).
38  */
39 #define ES58X_CHANNEL_IDX_NA 0xFF
40 #define ES58X_EMPTY_MSG NULL
41 
42 /* Threshold on consecutive CAN_STATE_ERROR_PASSIVE. If we receive
43  * ES58X_CONSECUTIVE_ERR_PASSIVE_MAX times the event
44  * ES58X_ERR_CRTL_PASSIVE in a row without any successful RX or TX,
45  * we force the device to switch to CAN_STATE_BUS_OFF state.
46  */
47 #define ES58X_CONSECUTIVE_ERR_PASSIVE_MAX 254
48 
49 /* A magic number sent by the ES581.4 to inform it is alive. */
50 #define ES58X_HEARTBEAT 0x11
51 
52 /**
53  * enum es58x_driver_info - Quirks of the device.
54  * @ES58X_DUAL_CHANNEL: Device has two CAN channels. If this flag is
55  *	not set, it is implied that the device has only one CAN
56  *	channel.
57  * @ES58X_FD_FAMILY: Device is CAN-FD capable. If this flag is not
58  *	set, the device only supports classical CAN.
59  */
60 enum es58x_driver_info {
61 	ES58X_DUAL_CHANNEL = BIT(0),
62 	ES58X_FD_FAMILY = BIT(1)
63 };
64 
65 enum es58x_echo {
66 	ES58X_ECHO_OFF = 0,
67 	ES58X_ECHO_ON = 1
68 };
69 
70 /**
71  * enum es58x_physical_layer - Type of the physical layer.
72  * @ES58X_PHYSICAL_LAYER_HIGH_SPEED: High-speed CAN (c.f. ISO
73  *	11898-2).
74  *
75  * Some products of the ETAS portfolio also support low-speed CAN
76  * (c.f. ISO 11898-3). However, all the devices in scope of this
77  * driver do not support the option, thus, the enum has only one
78  * member.
79  */
80 enum es58x_physical_layer {
81 	ES58X_PHYSICAL_LAYER_HIGH_SPEED = 1
82 };
83 
84 enum es58x_samples_per_bit {
85 	ES58X_SAMPLES_PER_BIT_ONE = 1,
86 	ES58X_SAMPLES_PER_BIT_THREE = 2
87 };
88 
89 /**
90  * enum es58x_sync_edge - Synchronization method.
91  * @ES58X_SYNC_EDGE_SINGLE: ISO CAN specification defines the use of a
92  *	single edge synchronization.  The synchronization should be
93  *	done on recessive to dominant level change.
94  *
95  * For information, ES582.1 and ES584.1 also support a double
96  * synchronization, requiring both recessive to dominant then dominant
97  * to recessive level change. However, this is not supported in
98  * SocketCAN framework, thus, the enum has only one member.
99  */
100 enum es58x_sync_edge {
101 	ES58X_SYNC_EDGE_SINGLE = 1
102 };
103 
104 /**
105  * enum es58x_flag - CAN flags for RX/TX messages.
106  * @ES58X_FLAG_EFF: Extended Frame Format (EFF).
107  * @ES58X_FLAG_RTR: Remote Transmission Request (RTR).
108  * @ES58X_FLAG_FD_BRS: Bit rate switch (BRS): second bitrate for
109  *	payload data.
110  * @ES58X_FLAG_FD_ESI: Error State Indicator (ESI): tell if the
111  *	transmitting node is in error passive mode.
112  * @ES58X_FLAG_FD_DATA: CAN FD frame.
113  */
114 enum es58x_flag {
115 	ES58X_FLAG_EFF = BIT(0),
116 	ES58X_FLAG_RTR = BIT(1),
117 	ES58X_FLAG_FD_BRS = BIT(3),
118 	ES58X_FLAG_FD_ESI = BIT(5),
119 	ES58X_FLAG_FD_DATA = BIT(6)
120 };
121 
122 /**
123  * enum es58x_err - CAN error detection.
124  * @ES58X_ERR_OK: No errors.
125  * @ES58X_ERR_PROT_STUFF: Bit stuffing error: more than 5 consecutive
126  *	equal bits.
127  * @ES58X_ERR_PROT_FORM: Frame format error.
128  * @ES58X_ERR_ACK: Received no ACK on transmission.
129  * @ES58X_ERR_PROT_BIT: Single bit error.
130  * @ES58X_ERR_PROT_CRC: Incorrect 15, 17 or 21 bits CRC.
131  * @ES58X_ERR_PROT_BIT1: Unable to send recessive bit: tried to send
132  *	recessive bit 1 but monitored dominant bit 0.
133  * @ES58X_ERR_PROT_BIT0: Unable to send dominant bit: tried to send
134  *	dominant bit 0 but monitored recessive bit 1.
135  * @ES58X_ERR_PROT_OVERLOAD: Bus overload.
136  * @ES58X_ERR_PROT_UNSPEC: Unspecified.
137  *
138  * Please refer to ISO 11898-1:2015, section 10.11 "Error detection"
139  * and section 10.13 "Overload signaling" for additional details.
140  */
141 enum es58x_err {
142 	ES58X_ERR_OK = 0,
143 	ES58X_ERR_PROT_STUFF = BIT(0),
144 	ES58X_ERR_PROT_FORM = BIT(1),
145 	ES58X_ERR_ACK = BIT(2),
146 	ES58X_ERR_PROT_BIT = BIT(3),
147 	ES58X_ERR_PROT_CRC = BIT(4),
148 	ES58X_ERR_PROT_BIT1 = BIT(5),
149 	ES58X_ERR_PROT_BIT0 = BIT(6),
150 	ES58X_ERR_PROT_OVERLOAD = BIT(7),
151 	ES58X_ERR_PROT_UNSPEC = BIT(31)
152 };
153 
154 /**
155  * enum es58x_event - CAN error codes returned by the device.
156  * @ES58X_EVENT_OK: No errors.
157  * @ES58X_EVENT_CRTL_ACTIVE: Active state: both TR and RX error count
158  *	is less than 128.
159  * @ES58X_EVENT_CRTL_PASSIVE: Passive state: either TX or RX error
160  *	count is greater than 127.
161  * @ES58X_EVENT_CRTL_WARNING: Warning state: either TX or RX error
162  *	count is greater than 96.
163  * @ES58X_EVENT_BUSOFF: Bus off.
164  * @ES58X_EVENT_SINGLE_WIRE: Lost connection on either CAN high or CAN
165  *	low.
166  *
167  * Please refer to ISO 11898-1:2015, section 12.1.4 "Rules of fault
168  * confinement" for additional details.
169  */
170 enum es58x_event {
171 	ES58X_EVENT_OK = 0,
172 	ES58X_EVENT_CRTL_ACTIVE = BIT(0),
173 	ES58X_EVENT_CRTL_PASSIVE = BIT(1),
174 	ES58X_EVENT_CRTL_WARNING = BIT(2),
175 	ES58X_EVENT_BUSOFF = BIT(3),
176 	ES58X_EVENT_SINGLE_WIRE = BIT(4)
177 };
178 
179 /* enum es58x_ret_u8 - Device return error codes, 8 bit format.
180  *
181  * Specific to ES581.4.
182  */
183 enum es58x_ret_u8 {
184 	ES58X_RET_U8_OK = 0x00,
185 	ES58X_RET_U8_ERR_UNSPECIFIED_FAILURE = 0x80,
186 	ES58X_RET_U8_ERR_NO_MEM = 0x81,
187 	ES58X_RET_U8_ERR_BAD_CRC = 0x99
188 };
189 
190 /* enum es58x_ret_u32 - Device return error codes, 32 bit format.
191  */
192 enum es58x_ret_u32 {
193 	ES58X_RET_U32_OK = 0x00000000UL,
194 	ES58X_RET_U32_ERR_UNSPECIFIED_FAILURE = 0x80000000UL,
195 	ES58X_RET_U32_ERR_NO_MEM = 0x80004001UL,
196 	ES58X_RET_U32_WARN_PARAM_ADJUSTED = 0x40004000UL,
197 	ES58X_RET_U32_WARN_TX_MAYBE_REORDER = 0x40004001UL,
198 	ES58X_RET_U32_ERR_TIMEDOUT = 0x80000008UL,
199 	ES58X_RET_U32_ERR_FIFO_FULL = 0x80003002UL,
200 	ES58X_RET_U32_ERR_BAD_CONFIG = 0x80004000UL,
201 	ES58X_RET_U32_ERR_NO_RESOURCE = 0x80004002UL
202 };
203 
204 /* enum es58x_ret_type - Type of the command returned by the ES58X
205  *	device.
206  */
207 enum es58x_ret_type {
208 	ES58X_RET_TYPE_SET_BITTIMING,
209 	ES58X_RET_TYPE_ENABLE_CHANNEL,
210 	ES58X_RET_TYPE_DISABLE_CHANNEL,
211 	ES58X_RET_TYPE_TX_MSG,
212 	ES58X_RET_TYPE_RESET_RX,
213 	ES58X_RET_TYPE_RESET_TX,
214 	ES58X_RET_TYPE_DEVICE_ERR
215 };
216 
217 union es58x_urb_cmd {
218 	struct es581_4_urb_cmd es581_4_urb_cmd;
219 	struct es58x_fd_urb_cmd es58x_fd_urb_cmd;
220 	struct {		/* Common header parts of all variants */
221 		__le16 sof;
222 		u8 cmd_type;
223 		u8 cmd_id;
224 	} __packed;
225 	u8 raw_cmd[0];
226 };
227 
228 /**
229  * struct es58x_priv - All information specific to a CAN channel.
230  * @can: struct can_priv must be the first member (Socket CAN relies
231  *	on the fact that function netdev_priv() returns a pointer to
232  *	a struct can_priv).
233  * @es58x_dev: pointer to the corresponding ES58X device.
234  * @tx_urb: Used as a buffer to concatenate the TX messages and to do
235  *	a bulk send. Please refer to es58x_start_xmit() for more
236  *	details.
237  * @tx_tail: Index of the oldest packet still pending for
238  *	completion. @tx_tail & echo_skb_mask represents the beginning
239  *	of the echo skb FIFO, i.e. index of the first element.
240  * @tx_head: Index of the next packet to be sent to the
241  *	device. @tx_head & echo_skb_mask represents the end of the
242  *	echo skb FIFO plus one, i.e. the first free index.
243  * @tx_can_msg_cnt: Number of messages in @tx_urb.
244  * @tx_can_msg_is_fd: false: all messages in @tx_urb are Classical
245  *	CAN, true: all messages in @tx_urb are CAN FD. Rationale:
246  *	ES58X FD devices do not allow to mix Classical CAN and FD CAN
247  *	frames in one single bulk transmission.
248  * @err_passive_before_rtx_success: The ES58X device might enter in a
249  *	state in which it keeps alternating between error passive
250  *	and active states. This counter keeps track of the number of
251  *	error passive and if it gets bigger than
252  *	ES58X_CONSECUTIVE_ERR_PASSIVE_MAX, es58x_rx_err_msg() will
253  *	force the status to bus-off.
254  * @channel_idx: Channel index, starts at zero.
255  */
256 struct es58x_priv {
257 	struct can_priv can;
258 	struct es58x_device *es58x_dev;
259 	struct urb *tx_urb;
260 
261 	u32 tx_tail;
262 	u32 tx_head;
263 
264 	u8 tx_can_msg_cnt;
265 	bool tx_can_msg_is_fd;
266 
267 	u8 err_passive_before_rtx_success;
268 
269 	u8 channel_idx;
270 };
271 
272 /**
273  * struct es58x_parameters - Constant parameters of a given hardware
274  *	variant.
275  * @bittiming_const: Nominal bittimming constant parameters.
276  * @data_bittiming_const: Data bittiming constant parameters.
277  * @tdc_const: Transmission Delay Compensation constant parameters.
278  * @bitrate_max: Maximum bitrate supported by the device.
279  * @clock: CAN clock parameters.
280  * @ctrlmode_supported: List of supported modes. Please refer to
281  *	can/netlink.h file for additional details.
282  * @tx_start_of_frame: Magic number at the beginning of each TX URB
283  *	command.
284  * @rx_start_of_frame: Magic number at the beginning of each RX URB
285  *	command.
286  * @tx_urb_cmd_max_len: Maximum length of a TX URB command.
287  * @rx_urb_cmd_max_len: Maximum length of a RX URB command.
288  * @fifo_mask: Bit mask to quickly convert the tx_tail and tx_head
289  *	field of the struct es58x_priv into echo_skb
290  *	indexes. Properties: @fifo_mask = echo_skb_max - 1 where
291  *	echo_skb_max must be a power of two. Also, echo_skb_max must
292  *	not exceed the maximum size of the device internal TX FIFO
293  *	length. This parameter is used to control the network queue
294  *	wake/stop logic.
295  * @dql_min_limit: Dynamic Queue Limits (DQL) absolute minimum limit
296  *	of bytes allowed to be queued on this network device transmit
297  *	queue. Used by the Byte Queue Limits (BQL) to determine how
298  *	frequently the xmit_more flag will be set to true in
299  *	es58x_start_xmit(). Set this value higher to optimize for
300  *	throughput but be aware that it might have a negative impact
301  *	on the latency! This value can also be set dynamically. Please
302  *	refer to Documentation/ABI/testing/sysfs-class-net-queues for
303  *	more details.
304  * @tx_bulk_max: Maximum number of TX messages that can be sent in one
305  *	single URB packet.
306  * @urb_cmd_header_len: Length of the URB command header.
307  * @rx_urb_max: Number of RX URB to be allocated during device probe.
308  * @tx_urb_max: Number of TX URB to be allocated during device probe.
309  */
310 struct es58x_parameters {
311 	const struct can_bittiming_const *bittiming_const;
312 	const struct can_bittiming_const *data_bittiming_const;
313 	const struct can_tdc_const *tdc_const;
314 	u32 bitrate_max;
315 	struct can_clock clock;
316 	u32 ctrlmode_supported;
317 	u16 tx_start_of_frame;
318 	u16 rx_start_of_frame;
319 	u16 tx_urb_cmd_max_len;
320 	u16 rx_urb_cmd_max_len;
321 	u16 fifo_mask;
322 	u16 dql_min_limit;
323 	u8 tx_bulk_max;
324 	u8 urb_cmd_header_len;
325 	u8 rx_urb_max;
326 	u8 tx_urb_max;
327 };
328 
329 /**
330  * struct es58x_operators - Function pointers used to encode/decode
331  *	the TX/RX messages.
332  * @get_msg_len: Get field msg_len of the urb_cmd. The offset of
333  *	msg_len inside urb_cmd depends of the device model.
334  * @handle_urb_cmd: Decode the URB command received from the device
335  *	and dispatch it to the relevant sub function.
336  * @fill_urb_header: Fill the header of urb_cmd.
337  * @tx_can_msg: Encode a TX CAN message and add it to the bulk buffer
338  *	cmd_buf of es58x_dev.
339  * @enable_channel: Start the CAN channel.
340  * @disable_channel: Stop the CAN channel.
341  * @reset_device: Full reset of the device. N.B: this feature is only
342  *	present on the ES581.4. For ES58X FD devices, this field is
343  *	set to NULL.
344  * @get_timestamp: Request a timestamp from the ES58X device.
345  */
346 struct es58x_operators {
347 	u16 (*get_msg_len)(const union es58x_urb_cmd *urb_cmd);
348 	int (*handle_urb_cmd)(struct es58x_device *es58x_dev,
349 			      const union es58x_urb_cmd *urb_cmd);
350 	void (*fill_urb_header)(union es58x_urb_cmd *urb_cmd, u8 cmd_type,
351 				u8 cmd_id, u8 channel_idx, u16 cmd_len);
352 	int (*tx_can_msg)(struct es58x_priv *priv, const struct sk_buff *skb);
353 	int (*enable_channel)(struct es58x_priv *priv);
354 	int (*disable_channel)(struct es58x_priv *priv);
355 	int (*reset_device)(struct es58x_device *es58x_dev);
356 	int (*get_timestamp)(struct es58x_device *es58x_dev);
357 };
358 
359 /**
360  * struct es58x_device - All information specific to an ES58X device.
361  * @dev: Device information.
362  * @udev: USB device information.
363  * @netdev: Array of our CAN channels.
364  * @param: The constant parameters.
365  * @ops: Operators.
366  * @rx_pipe: USB reception pipe.
367  * @tx_pipe: USB transmission pipe.
368  * @rx_urbs: Anchor for received URBs.
369  * @tx_urbs_busy: Anchor for TX URBs which were send to the device.
370  * @tx_urbs_idle: Anchor for TX USB which are idle. This driver
371  *	allocates the memory for the URBs during the probe. When a TX
372  *	URB is needed, it can be taken from this anchor. The network
373  *	queue wake/stop logic should prevent this URB from getting
374  *	empty. Please refer to es58x_get_tx_urb() for more details.
375  * @tx_urbs_idle_cnt: number of urbs in @tx_urbs_idle.
376  * @opened_channel_cnt: number of channels opened (c.f. es58x_open()
377  *	and es58x_stop()).
378  * @ktime_req_ns: kernel timestamp when es58x_set_realtime_diff_ns()
379  *	was called.
380  * @realtime_diff_ns: difference in nanoseconds between the clocks of
381  *	the ES58X device and the kernel.
382  * @timestamps: a temporary buffer to store the time stamps before
383  *	feeding them to es58x_can_get_echo_skb(). Can only be used
384  *	in RX branches.
385  * @rx_max_packet_size: Maximum length of bulk-in URB.
386  * @num_can_ch: Number of CAN channel (i.e. number of elements of @netdev).
387  * @rx_cmd_buf_len: Length of @rx_cmd_buf.
388  * @rx_cmd_buf: The device might split the URB commands in an
389  *	arbitrary amount of pieces. This buffer is used to concatenate
390  *	all those pieces. Can only be used in RX branches. This field
391  *	has to be the last one of the structure because it is has a
392  *	flexible size (c.f. es58x_sizeof_es58x_device() function).
393  */
394 struct es58x_device {
395 	struct device *dev;
396 	struct usb_device *udev;
397 	struct net_device *netdev[ES58X_NUM_CAN_CH_MAX];
398 
399 	const struct es58x_parameters *param;
400 	const struct es58x_operators *ops;
401 
402 	int rx_pipe;
403 	int tx_pipe;
404 
405 	struct usb_anchor rx_urbs;
406 	struct usb_anchor tx_urbs_busy;
407 	struct usb_anchor tx_urbs_idle;
408 	atomic_t tx_urbs_idle_cnt;
409 	atomic_t opened_channel_cnt;
410 
411 	u64 ktime_req_ns;
412 	s64 realtime_diff_ns;
413 
414 	u64 timestamps[ES58X_ECHO_BULK_MAX];
415 
416 	u16 rx_max_packet_size;
417 	u8 num_can_ch;
418 
419 	u16 rx_cmd_buf_len;
420 	union es58x_urb_cmd rx_cmd_buf;
421 };
422 
423 /**
424  * es58x_sizeof_es58x_device() - Calculate the maximum length of
425  *	struct es58x_device.
426  * @es58x_dev_param: The constant parameters of the device.
427  *
428  * The length of struct es58x_device depends on the length of its last
429  * field: rx_cmd_buf. This macro allows to optimize the memory
430  * allocation.
431  *
432  * Return: length of struct es58x_device.
433  */
434 static inline size_t es58x_sizeof_es58x_device(const struct es58x_parameters
435 					       *es58x_dev_param)
436 {
437 	return offsetof(struct es58x_device, rx_cmd_buf) +
438 		es58x_dev_param->rx_urb_cmd_max_len;
439 }
440 
441 static inline int __es58x_check_msg_len(const struct device *dev,
442 					const char *stringified_msg,
443 					size_t actual_len, size_t expected_len)
444 {
445 	if (expected_len != actual_len) {
446 		dev_err(dev,
447 			"Length of %s is %zu but received command is %zu.\n",
448 			stringified_msg, expected_len, actual_len);
449 		return -EMSGSIZE;
450 	}
451 	return 0;
452 }
453 
454 /**
455  * es58x_check_msg_len() - Check the size of a received message.
456  * @dev: Device, used to print error messages.
457  * @msg: Received message, must not be a pointer.
458  * @actual_len: Length of the message as advertised in the command header.
459  *
460  * Must be a macro in order to accept the different types of messages
461  * as an input. Can be use with any of the messages which have a fixed
462  * length. Check for an exact match of the size.
463  *
464  * Return: zero on success, -EMSGSIZE if @actual_len differs from the
465  * expected length.
466  */
467 #define es58x_check_msg_len(dev, msg, actual_len)			\
468 	__es58x_check_msg_len(dev, __stringify(msg),			\
469 			      actual_len, sizeof(msg))
470 
471 static inline int __es58x_check_msg_max_len(const struct device *dev,
472 					    const char *stringified_msg,
473 					    size_t actual_len,
474 					    size_t expected_len)
475 {
476 	if (actual_len > expected_len) {
477 		dev_err(dev,
478 			"Maximum length for %s is %zu but received command is %zu.\n",
479 			stringified_msg, expected_len, actual_len);
480 		return -EOVERFLOW;
481 	}
482 	return 0;
483 }
484 
485 /**
486  * es58x_check_msg_max_len() - Check the maximum size of a received message.
487  * @dev: Device, used to print error messages.
488  * @msg: Received message, must not be a pointer.
489  * @actual_len: Length of the message as advertised in the command header.
490  *
491  * Must be a macro in order to accept the different types of messages
492  * as an input. To be used with the messages of variable sizes. Only
493  * check that the message is not bigger than the maximum expected
494  * size.
495  *
496  * Return: zero on success, -EOVERFLOW if @actual_len is greater than
497  * the expected length.
498  */
499 #define es58x_check_msg_max_len(dev, msg, actual_len)			\
500 	__es58x_check_msg_max_len(dev, __stringify(msg),		\
501 				  actual_len, sizeof(msg))
502 
503 static inline int __es58x_msg_num_element(const struct device *dev,
504 					  const char *stringified_msg,
505 					  size_t actual_len, size_t msg_len,
506 					  size_t elem_len)
507 {
508 	size_t actual_num_elem = actual_len / elem_len;
509 	size_t expected_num_elem = msg_len / elem_len;
510 
511 	if (actual_num_elem == 0) {
512 		dev_err(dev,
513 			"Minimum length for %s is %zu but received command is %zu.\n",
514 			stringified_msg, elem_len, actual_len);
515 		return -EMSGSIZE;
516 	} else if ((actual_len % elem_len) != 0) {
517 		dev_err(dev,
518 			"Received command length: %zu is not a multiple of %s[0]: %zu\n",
519 			actual_len, stringified_msg, elem_len);
520 		return -EMSGSIZE;
521 	} else if (actual_num_elem > expected_num_elem) {
522 		dev_err(dev,
523 			"Array %s is supposed to have %zu elements each of size %zu...\n",
524 			stringified_msg, expected_num_elem, elem_len);
525 		dev_err(dev,
526 			"... But received command has %zu elements (total length %zu).\n",
527 			actual_num_elem, actual_len);
528 		return -EOVERFLOW;
529 	}
530 	return actual_num_elem;
531 }
532 
533 /**
534  * es58x_msg_num_element() - Check size and give the number of
535  *	elements in a message of array type.
536  * @dev: Device, used to print error messages.
537  * @msg: Received message, must be an array.
538  * @actual_len: Length of the message as advertised in the command
539  *	header.
540  *
541  * Must be a macro in order to accept the different types of messages
542  * as an input. To be used on message of array type. Array's element
543  * has to be of fixed size (else use es58x_check_msg_max_len()). Check
544  * that the total length is an exact multiple of the length of a
545  * single element.
546  *
547  * Return: number of elements in the array on success, -EOVERFLOW if
548  * @actual_len is greater than the expected length, -EMSGSIZE if
549  * @actual_len is not a multiple of a single element.
550  */
551 #define es58x_msg_num_element(dev, msg, actual_len)			\
552 ({									\
553 	size_t __elem_len = sizeof((msg)[0]) + __must_be_array(msg);	\
554 	__es58x_msg_num_element(dev, __stringify(msg), actual_len,	\
555 				sizeof(msg), __elem_len);		\
556 })
557 
558 /**
559  * es58x_priv() - Get the priv member and cast it to struct es58x_priv.
560  * @netdev: CAN network device.
561  *
562  * Return: ES58X device.
563  */
564 static inline struct es58x_priv *es58x_priv(struct net_device *netdev)
565 {
566 	return (struct es58x_priv *)netdev_priv(netdev);
567 }
568 
569 /**
570  * ES58X_SIZEOF_URB_CMD() - Calculate the maximum length of an urb
571  *	command for a given message field name.
572  * @es58x_urb_cmd_type: type (either "struct es581_4_urb_cmd" or
573  *	"struct es58x_fd_urb_cmd").
574  * @msg_field: name of the message field.
575  *
576  * Must be a macro in order to accept the different command types as
577  * an input.
578  *
579  * Return: length of the urb command.
580  */
581 #define ES58X_SIZEOF_URB_CMD(es58x_urb_cmd_type, msg_field)		\
582 	(offsetof(es58x_urb_cmd_type, raw_msg)				\
583 		+ sizeof_field(es58x_urb_cmd_type, msg_field)		\
584 		+ sizeof_field(es58x_urb_cmd_type,			\
585 			       reserved_for_crc16_do_not_use))
586 
587 /**
588  * es58x_get_urb_cmd_len() - Calculate the actual length of an urb
589  *	command for a given message length.
590  * @es58x_dev: ES58X device.
591  * @msg_len: Length of the message.
592  *
593  * Add the header and CRC lengths to the message length.
594  *
595  * Return: length of the urb command.
596  */
597 static inline size_t es58x_get_urb_cmd_len(struct es58x_device *es58x_dev,
598 					   u16 msg_len)
599 {
600 	return es58x_dev->param->urb_cmd_header_len + msg_len + sizeof(u16);
601 }
602 
603 /**
604  * es58x_get_netdev() - Get the network device.
605  * @es58x_dev: ES58X device.
606  * @channel_no: The channel number as advertised in the urb command.
607  * @channel_idx_offset: Some of the ES58x starts channel numbering
608  *	from 0 (ES58X FD), others from 1 (ES581.4).
609  * @netdev: CAN network device.
610  *
611  * Do a sanity check on the index provided by the device.
612  *
613  * Return: zero on success, -ECHRNG if the received channel number is
614  *	out of range and -ENODEV if the network device is not yet
615  *	configured.
616  */
617 static inline int es58x_get_netdev(struct es58x_device *es58x_dev,
618 				   int channel_no, int channel_idx_offset,
619 				   struct net_device **netdev)
620 {
621 	int channel_idx = channel_no - channel_idx_offset;
622 
623 	*netdev = NULL;
624 	if (channel_idx < 0 || channel_idx >= es58x_dev->num_can_ch)
625 		return -ECHRNG;
626 
627 	*netdev = es58x_dev->netdev[channel_idx];
628 	if (!*netdev || !netif_device_present(*netdev))
629 		return -ENODEV;
630 
631 	return 0;
632 }
633 
634 /**
635  * es58x_get_raw_can_id() - Get the CAN ID.
636  * @cf: CAN frame.
637  *
638  * Mask the CAN ID in order to only keep the significant bits.
639  *
640  * Return: the raw value of the CAN ID.
641  */
642 static inline int es58x_get_raw_can_id(const struct can_frame *cf)
643 {
644 	if (cf->can_id & CAN_EFF_FLAG)
645 		return cf->can_id & CAN_EFF_MASK;
646 	else
647 		return cf->can_id & CAN_SFF_MASK;
648 }
649 
650 /**
651  * es58x_get_flags() - Get the CAN flags.
652  * @skb: socket buffer of a CAN message.
653  *
654  * Return: the CAN flag as an enum es58x_flag.
655  */
656 static inline enum es58x_flag es58x_get_flags(const struct sk_buff *skb)
657 {
658 	struct canfd_frame *cf = (struct canfd_frame *)skb->data;
659 	enum es58x_flag es58x_flags = 0;
660 
661 	if (cf->can_id & CAN_EFF_FLAG)
662 		es58x_flags |= ES58X_FLAG_EFF;
663 
664 	if (can_is_canfd_skb(skb)) {
665 		es58x_flags |= ES58X_FLAG_FD_DATA;
666 		if (cf->flags & CANFD_BRS)
667 			es58x_flags |= ES58X_FLAG_FD_BRS;
668 		if (cf->flags & CANFD_ESI)
669 			es58x_flags |= ES58X_FLAG_FD_ESI;
670 	} else if (cf->can_id & CAN_RTR_FLAG)
671 		/* Remote frames are only defined in Classical CAN frames */
672 		es58x_flags |= ES58X_FLAG_RTR;
673 
674 	return es58x_flags;
675 }
676 
677 int es58x_can_get_echo_skb(struct net_device *netdev, u32 packet_idx,
678 			   u64 *tstamps, unsigned int pkts);
679 int es58x_tx_ack_msg(struct net_device *netdev, u16 tx_free_entries,
680 		     enum es58x_ret_u32 rx_cmd_ret_u32);
681 int es58x_rx_can_msg(struct net_device *netdev, u64 timestamp, const u8 *data,
682 		     canid_t can_id, enum es58x_flag es58x_flags, u8 dlc);
683 int es58x_rx_err_msg(struct net_device *netdev, enum es58x_err error,
684 		     enum es58x_event event, u64 timestamp);
685 void es58x_rx_timestamp(struct es58x_device *es58x_dev, u64 timestamp);
686 int es58x_rx_cmd_ret_u8(struct device *dev, enum es58x_ret_type cmd_ret_type,
687 			enum es58x_ret_u8 rx_cmd_ret_u8);
688 int es58x_rx_cmd_ret_u32(struct net_device *netdev,
689 			 enum es58x_ret_type cmd_ret_type,
690 			 enum es58x_ret_u32 rx_cmd_ret_u32);
691 int es58x_send_msg(struct es58x_device *es58x_dev, u8 cmd_type, u8 cmd_id,
692 		   const void *msg, u16 cmd_len, int channel_idx);
693 
694 extern const struct es58x_parameters es581_4_param;
695 extern const struct es58x_operators es581_4_ops;
696 
697 extern const struct es58x_parameters es58x_fd_param;
698 extern const struct es58x_operators es58x_fd_ops;
699 
700 #endif /* __ES58X_COMMON_H__ */
701