![]() |
|
|||
0001 /* SPDX-License-Identifier: GPL-2.0 */ 0002 0003 /* Driver for ETAS GmbH ES58X USB CAN(-FD) Bus Interfaces. 0004 * 0005 * File es58x_core.h: All common definitions and declarations. 0006 * 0007 * Copyright (c) 2019 Robert Bosch Engineering and Business Solutions. All rights reserved. 0008 * Copyright (c) 2020 ETAS K.K.. All rights reserved. 0009 * Copyright (c) 2020, 2021 Vincent Mailhol <mailhol.vincent@wanadoo.fr> 0010 */ 0011 0012 #ifndef __ES58X_COMMON_H__ 0013 #define __ES58X_COMMON_H__ 0014 0015 #include <linux/types.h> 0016 #include <linux/usb.h> 0017 #include <linux/netdevice.h> 0018 #include <linux/can.h> 0019 #include <linux/can/dev.h> 0020 0021 #include "es581_4.h" 0022 #include "es58x_fd.h" 0023 0024 /* Driver constants */ 0025 #define ES58X_RX_URBS_MAX 5 /* Empirical value */ 0026 #define ES58X_TX_URBS_MAX 6 /* Empirical value */ 0027 0028 #define ES58X_MAX(param) \ 0029 (ES581_4_##param > ES58X_FD_##param ? \ 0030 ES581_4_##param : ES58X_FD_##param) 0031 #define ES58X_TX_BULK_MAX ES58X_MAX(TX_BULK_MAX) 0032 #define ES58X_RX_BULK_MAX ES58X_MAX(RX_BULK_MAX) 0033 #define ES58X_ECHO_BULK_MAX ES58X_MAX(ECHO_BULK_MAX) 0034 #define ES58X_NUM_CAN_CH_MAX ES58X_MAX(NUM_CAN_CH) 0035 0036 /* Use this when channel index is irrelevant (e.g. device 0037 * timestamp). 0038 */ 0039 #define ES58X_CHANNEL_IDX_NA 0xFF 0040 #define ES58X_EMPTY_MSG NULL 0041 0042 /* Threshold on consecutive CAN_STATE_ERROR_PASSIVE. If we receive 0043 * ES58X_CONSECUTIVE_ERR_PASSIVE_MAX times the event 0044 * ES58X_ERR_CRTL_PASSIVE in a row without any successful RX or TX, 0045 * we force the device to switch to CAN_STATE_BUS_OFF state. 0046 */ 0047 #define ES58X_CONSECUTIVE_ERR_PASSIVE_MAX 254 0048 0049 /* A magic number sent by the ES581.4 to inform it is alive. */ 0050 #define ES58X_HEARTBEAT 0x11 0051 0052 /** 0053 * enum es58x_driver_info - Quirks of the device. 0054 * @ES58X_DUAL_CHANNEL: Device has two CAN channels. If this flag is 0055 * not set, it is implied that the device has only one CAN 0056 * channel. 0057 * @ES58X_FD_FAMILY: Device is CAN-FD capable. If this flag is not 0058 * set, the device only supports classical CAN. 0059 */ 0060 enum es58x_driver_info { 0061 ES58X_DUAL_CHANNEL = BIT(0), 0062 ES58X_FD_FAMILY = BIT(1) 0063 }; 0064 0065 enum es58x_echo { 0066 ES58X_ECHO_OFF = 0, 0067 ES58X_ECHO_ON = 1 0068 }; 0069 0070 /** 0071 * enum es58x_physical_layer - Type of the physical layer. 0072 * @ES58X_PHYSICAL_LAYER_HIGH_SPEED: High-speed CAN (c.f. ISO 0073 * 11898-2). 0074 * 0075 * Some products of the ETAS portfolio also support low-speed CAN 0076 * (c.f. ISO 11898-3). However, all the devices in scope of this 0077 * driver do not support the option, thus, the enum has only one 0078 * member. 0079 */ 0080 enum es58x_physical_layer { 0081 ES58X_PHYSICAL_LAYER_HIGH_SPEED = 1 0082 }; 0083 0084 enum es58x_samples_per_bit { 0085 ES58X_SAMPLES_PER_BIT_ONE = 1, 0086 ES58X_SAMPLES_PER_BIT_THREE = 2 0087 }; 0088 0089 /** 0090 * enum es58x_sync_edge - Synchronization method. 0091 * @ES58X_SYNC_EDGE_SINGLE: ISO CAN specification defines the use of a 0092 * single edge synchronization. The synchronization should be 0093 * done on recessive to dominant level change. 0094 * 0095 * For information, ES582.1 and ES584.1 also support a double 0096 * synchronization, requiring both recessive to dominant then dominant 0097 * to recessive level change. However, this is not supported in 0098 * SocketCAN framework, thus, the enum has only one member. 0099 */ 0100 enum es58x_sync_edge { 0101 ES58X_SYNC_EDGE_SINGLE = 1 0102 }; 0103 0104 /** 0105 * enum es58x_flag - CAN flags for RX/TX messages. 0106 * @ES58X_FLAG_EFF: Extended Frame Format (EFF). 0107 * @ES58X_FLAG_RTR: Remote Transmission Request (RTR). 0108 * @ES58X_FLAG_FD_BRS: Bit rate switch (BRS): second bitrate for 0109 * payload data. 0110 * @ES58X_FLAG_FD_ESI: Error State Indicator (ESI): tell if the 0111 * transmitting node is in error passive mode. 0112 * @ES58X_FLAG_FD_DATA: CAN FD frame. 0113 */ 0114 enum es58x_flag { 0115 ES58X_FLAG_EFF = BIT(0), 0116 ES58X_FLAG_RTR = BIT(1), 0117 ES58X_FLAG_FD_BRS = BIT(3), 0118 ES58X_FLAG_FD_ESI = BIT(5), 0119 ES58X_FLAG_FD_DATA = BIT(6) 0120 }; 0121 0122 /** 0123 * enum es58x_err - CAN error detection. 0124 * @ES58X_ERR_OK: No errors. 0125 * @ES58X_ERR_PROT_STUFF: Bit stuffing error: more than 5 consecutive 0126 * equal bits. 0127 * @ES58X_ERR_PROT_FORM: Frame format error. 0128 * @ES58X_ERR_ACK: Received no ACK on transmission. 0129 * @ES58X_ERR_PROT_BIT: Single bit error. 0130 * @ES58X_ERR_PROT_CRC: Incorrect 15, 17 or 21 bits CRC. 0131 * @ES58X_ERR_PROT_BIT1: Unable to send recessive bit: tried to send 0132 * recessive bit 1 but monitored dominant bit 0. 0133 * @ES58X_ERR_PROT_BIT0: Unable to send dominant bit: tried to send 0134 * dominant bit 0 but monitored recessive bit 1. 0135 * @ES58X_ERR_PROT_OVERLOAD: Bus overload. 0136 * @ES58X_ERR_PROT_UNSPEC: Unspecified. 0137 * 0138 * Please refer to ISO 11898-1:2015, section 10.11 "Error detection" 0139 * and section 10.13 "Overload signaling" for additional details. 0140 */ 0141 enum es58x_err { 0142 ES58X_ERR_OK = 0, 0143 ES58X_ERR_PROT_STUFF = BIT(0), 0144 ES58X_ERR_PROT_FORM = BIT(1), 0145 ES58X_ERR_ACK = BIT(2), 0146 ES58X_ERR_PROT_BIT = BIT(3), 0147 ES58X_ERR_PROT_CRC = BIT(4), 0148 ES58X_ERR_PROT_BIT1 = BIT(5), 0149 ES58X_ERR_PROT_BIT0 = BIT(6), 0150 ES58X_ERR_PROT_OVERLOAD = BIT(7), 0151 ES58X_ERR_PROT_UNSPEC = BIT(31) 0152 }; 0153 0154 /** 0155 * enum es58x_event - CAN error codes returned by the device. 0156 * @ES58X_EVENT_OK: No errors. 0157 * @ES58X_EVENT_CRTL_ACTIVE: Active state: both TR and RX error count 0158 * is less than 128. 0159 * @ES58X_EVENT_CRTL_PASSIVE: Passive state: either TX or RX error 0160 * count is greater than 127. 0161 * @ES58X_EVENT_CRTL_WARNING: Warning state: either TX or RX error 0162 * count is greater than 96. 0163 * @ES58X_EVENT_BUSOFF: Bus off. 0164 * @ES58X_EVENT_SINGLE_WIRE: Lost connection on either CAN high or CAN 0165 * low. 0166 * 0167 * Please refer to ISO 11898-1:2015, section 12.1.4 "Rules of fault 0168 * confinement" for additional details. 0169 */ 0170 enum es58x_event { 0171 ES58X_EVENT_OK = 0, 0172 ES58X_EVENT_CRTL_ACTIVE = BIT(0), 0173 ES58X_EVENT_CRTL_PASSIVE = BIT(1), 0174 ES58X_EVENT_CRTL_WARNING = BIT(2), 0175 ES58X_EVENT_BUSOFF = BIT(3), 0176 ES58X_EVENT_SINGLE_WIRE = BIT(4) 0177 }; 0178 0179 /* enum es58x_ret_u8 - Device return error codes, 8 bit format. 0180 * 0181 * Specific to ES581.4. 0182 */ 0183 enum es58x_ret_u8 { 0184 ES58X_RET_U8_OK = 0x00, 0185 ES58X_RET_U8_ERR_UNSPECIFIED_FAILURE = 0x80, 0186 ES58X_RET_U8_ERR_NO_MEM = 0x81, 0187 ES58X_RET_U8_ERR_BAD_CRC = 0x99 0188 }; 0189 0190 /* enum es58x_ret_u32 - Device return error codes, 32 bit format. 0191 */ 0192 enum es58x_ret_u32 { 0193 ES58X_RET_U32_OK = 0x00000000UL, 0194 ES58X_RET_U32_ERR_UNSPECIFIED_FAILURE = 0x80000000UL, 0195 ES58X_RET_U32_ERR_NO_MEM = 0x80004001UL, 0196 ES58X_RET_U32_WARN_PARAM_ADJUSTED = 0x40004000UL, 0197 ES58X_RET_U32_WARN_TX_MAYBE_REORDER = 0x40004001UL, 0198 ES58X_RET_U32_ERR_TIMEDOUT = 0x80000008UL, 0199 ES58X_RET_U32_ERR_FIFO_FULL = 0x80003002UL, 0200 ES58X_RET_U32_ERR_BAD_CONFIG = 0x80004000UL, 0201 ES58X_RET_U32_ERR_NO_RESOURCE = 0x80004002UL 0202 }; 0203 0204 /* enum es58x_ret_type - Type of the command returned by the ES58X 0205 * device. 0206 */ 0207 enum es58x_ret_type { 0208 ES58X_RET_TYPE_SET_BITTIMING, 0209 ES58X_RET_TYPE_ENABLE_CHANNEL, 0210 ES58X_RET_TYPE_DISABLE_CHANNEL, 0211 ES58X_RET_TYPE_TX_MSG, 0212 ES58X_RET_TYPE_RESET_RX, 0213 ES58X_RET_TYPE_RESET_TX, 0214 ES58X_RET_TYPE_DEVICE_ERR 0215 }; 0216 0217 union es58x_urb_cmd { 0218 struct es581_4_urb_cmd es581_4_urb_cmd; 0219 struct es58x_fd_urb_cmd es58x_fd_urb_cmd; 0220 struct { /* Common header parts of all variants */ 0221 __le16 sof; 0222 u8 cmd_type; 0223 u8 cmd_id; 0224 } __packed; 0225 u8 raw_cmd[0]; 0226 }; 0227 0228 /** 0229 * struct es58x_priv - All information specific to a CAN channel. 0230 * @can: struct can_priv must be the first member (Socket CAN relies 0231 * on the fact that function netdev_priv() returns a pointer to 0232 * a struct can_priv). 0233 * @es58x_dev: pointer to the corresponding ES58X device. 0234 * @tx_urb: Used as a buffer to concatenate the TX messages and to do 0235 * a bulk send. Please refer to es58x_start_xmit() for more 0236 * details. 0237 * @tx_tail: Index of the oldest packet still pending for 0238 * completion. @tx_tail & echo_skb_mask represents the beginning 0239 * of the echo skb FIFO, i.e. index of the first element. 0240 * @tx_head: Index of the next packet to be sent to the 0241 * device. @tx_head & echo_skb_mask represents the end of the 0242 * echo skb FIFO plus one, i.e. the first free index. 0243 * @tx_can_msg_cnt: Number of messages in @tx_urb. 0244 * @tx_can_msg_is_fd: false: all messages in @tx_urb are Classical 0245 * CAN, true: all messages in @tx_urb are CAN FD. Rationale: 0246 * ES58X FD devices do not allow to mix Classical CAN and FD CAN 0247 * frames in one single bulk transmission. 0248 * @err_passive_before_rtx_success: The ES58X device might enter in a 0249 * state in which it keeps alternating between error passive 0250 * and active states. This counter keeps track of the number of 0251 * error passive and if it gets bigger than 0252 * ES58X_CONSECUTIVE_ERR_PASSIVE_MAX, es58x_rx_err_msg() will 0253 * force the status to bus-off. 0254 * @channel_idx: Channel index, starts at zero. 0255 */ 0256 struct es58x_priv { 0257 struct can_priv can; 0258 struct es58x_device *es58x_dev; 0259 struct urb *tx_urb; 0260 0261 u32 tx_tail; 0262 u32 tx_head; 0263 0264 u8 tx_can_msg_cnt; 0265 bool tx_can_msg_is_fd; 0266 0267 u8 err_passive_before_rtx_success; 0268 0269 u8 channel_idx; 0270 }; 0271 0272 /** 0273 * struct es58x_parameters - Constant parameters of a given hardware 0274 * variant. 0275 * @bittiming_const: Nominal bittimming constant parameters. 0276 * @data_bittiming_const: Data bittiming constant parameters. 0277 * @tdc_const: Transmission Delay Compensation constant parameters. 0278 * @bitrate_max: Maximum bitrate supported by the device. 0279 * @clock: CAN clock parameters. 0280 * @ctrlmode_supported: List of supported modes. Please refer to 0281 * can/netlink.h file for additional details. 0282 * @tx_start_of_frame: Magic number at the beginning of each TX URB 0283 * command. 0284 * @rx_start_of_frame: Magic number at the beginning of each RX URB 0285 * command. 0286 * @tx_urb_cmd_max_len: Maximum length of a TX URB command. 0287 * @rx_urb_cmd_max_len: Maximum length of a RX URB command. 0288 * @fifo_mask: Bit mask to quickly convert the tx_tail and tx_head 0289 * field of the struct es58x_priv into echo_skb 0290 * indexes. Properties: @fifo_mask = echo_skb_max - 1 where 0291 * echo_skb_max must be a power of two. Also, echo_skb_max must 0292 * not exceed the maximum size of the device internal TX FIFO 0293 * length. This parameter is used to control the network queue 0294 * wake/stop logic. 0295 * @dql_min_limit: Dynamic Queue Limits (DQL) absolute minimum limit 0296 * of bytes allowed to be queued on this network device transmit 0297 * queue. Used by the Byte Queue Limits (BQL) to determine how 0298 * frequently the xmit_more flag will be set to true in 0299 * es58x_start_xmit(). Set this value higher to optimize for 0300 * throughput but be aware that it might have a negative impact 0301 * on the latency! This value can also be set dynamically. Please 0302 * refer to Documentation/ABI/testing/sysfs-class-net-queues for 0303 * more details. 0304 * @tx_bulk_max: Maximum number of TX messages that can be sent in one 0305 * single URB packet. 0306 * @urb_cmd_header_len: Length of the URB command header. 0307 * @rx_urb_max: Number of RX URB to be allocated during device probe. 0308 * @tx_urb_max: Number of TX URB to be allocated during device probe. 0309 */ 0310 struct es58x_parameters { 0311 const struct can_bittiming_const *bittiming_const; 0312 const struct can_bittiming_const *data_bittiming_const; 0313 const struct can_tdc_const *tdc_const; 0314 u32 bitrate_max; 0315 struct can_clock clock; 0316 u32 ctrlmode_supported; 0317 u16 tx_start_of_frame; 0318 u16 rx_start_of_frame; 0319 u16 tx_urb_cmd_max_len; 0320 u16 rx_urb_cmd_max_len; 0321 u16 fifo_mask; 0322 u16 dql_min_limit; 0323 u8 tx_bulk_max; 0324 u8 urb_cmd_header_len; 0325 u8 rx_urb_max; 0326 u8 tx_urb_max; 0327 }; 0328 0329 /** 0330 * struct es58x_operators - Function pointers used to encode/decode 0331 * the TX/RX messages. 0332 * @get_msg_len: Get field msg_len of the urb_cmd. The offset of 0333 * msg_len inside urb_cmd depends of the device model. 0334 * @handle_urb_cmd: Decode the URB command received from the device 0335 * and dispatch it to the relevant sub function. 0336 * @fill_urb_header: Fill the header of urb_cmd. 0337 * @tx_can_msg: Encode a TX CAN message and add it to the bulk buffer 0338 * cmd_buf of es58x_dev. 0339 * @enable_channel: Start the CAN channel. 0340 * @disable_channel: Stop the CAN channel. 0341 * @reset_device: Full reset of the device. N.B: this feature is only 0342 * present on the ES581.4. For ES58X FD devices, this field is 0343 * set to NULL. 0344 * @get_timestamp: Request a timestamp from the ES58X device. 0345 */ 0346 struct es58x_operators { 0347 u16 (*get_msg_len)(const union es58x_urb_cmd *urb_cmd); 0348 int (*handle_urb_cmd)(struct es58x_device *es58x_dev, 0349 const union es58x_urb_cmd *urb_cmd); 0350 void (*fill_urb_header)(union es58x_urb_cmd *urb_cmd, u8 cmd_type, 0351 u8 cmd_id, u8 channel_idx, u16 cmd_len); 0352 int (*tx_can_msg)(struct es58x_priv *priv, const struct sk_buff *skb); 0353 int (*enable_channel)(struct es58x_priv *priv); 0354 int (*disable_channel)(struct es58x_priv *priv); 0355 int (*reset_device)(struct es58x_device *es58x_dev); 0356 int (*get_timestamp)(struct es58x_device *es58x_dev); 0357 }; 0358 0359 /** 0360 * struct es58x_device - All information specific to an ES58X device. 0361 * @dev: Device information. 0362 * @udev: USB device information. 0363 * @netdev: Array of our CAN channels. 0364 * @param: The constant parameters. 0365 * @ops: Operators. 0366 * @rx_pipe: USB reception pipe. 0367 * @tx_pipe: USB transmission pipe. 0368 * @rx_urbs: Anchor for received URBs. 0369 * @tx_urbs_busy: Anchor for TX URBs which were send to the device. 0370 * @tx_urbs_idle: Anchor for TX USB which are idle. This driver 0371 * allocates the memory for the URBs during the probe. When a TX 0372 * URB is needed, it can be taken from this anchor. The network 0373 * queue wake/stop logic should prevent this URB from getting 0374 * empty. Please refer to es58x_get_tx_urb() for more details. 0375 * @tx_urbs_idle_cnt: number of urbs in @tx_urbs_idle. 0376 * @ktime_req_ns: kernel timestamp when es58x_set_realtime_diff_ns() 0377 * was called. 0378 * @realtime_diff_ns: difference in nanoseconds between the clocks of 0379 * the ES58X device and the kernel. 0380 * @timestamps: a temporary buffer to store the time stamps before 0381 * feeding them to es58x_can_get_echo_skb(). Can only be used 0382 * in RX branches. 0383 * @num_can_ch: Number of CAN channel (i.e. number of elements of @netdev). 0384 * @opened_channel_cnt: number of channels opened. Free of race 0385 * conditions because its two users (net_device_ops:ndo_open() 0386 * and net_device_ops:ndo_close()) guarantee that the network 0387 * stack big kernel lock (a.k.a. rtnl_mutex) is being hold. 0388 * @rx_cmd_buf_len: Length of @rx_cmd_buf. 0389 * @rx_cmd_buf: The device might split the URB commands in an 0390 * arbitrary amount of pieces. This buffer is used to concatenate 0391 * all those pieces. Can only be used in RX branches. This field 0392 * has to be the last one of the structure because it is has a 0393 * flexible size (c.f. es58x_sizeof_es58x_device() function). 0394 */ 0395 struct es58x_device { 0396 struct device *dev; 0397 struct usb_device *udev; 0398 struct net_device *netdev[ES58X_NUM_CAN_CH_MAX]; 0399 0400 const struct es58x_parameters *param; 0401 const struct es58x_operators *ops; 0402 0403 unsigned int rx_pipe; 0404 unsigned int tx_pipe; 0405 0406 struct usb_anchor rx_urbs; 0407 struct usb_anchor tx_urbs_busy; 0408 struct usb_anchor tx_urbs_idle; 0409 atomic_t tx_urbs_idle_cnt; 0410 0411 u64 ktime_req_ns; 0412 s64 realtime_diff_ns; 0413 0414 u64 timestamps[ES58X_ECHO_BULK_MAX]; 0415 0416 u8 num_can_ch; 0417 u8 opened_channel_cnt; 0418 0419 u16 rx_cmd_buf_len; 0420 union es58x_urb_cmd rx_cmd_buf; 0421 }; 0422 0423 /** 0424 * es58x_sizeof_es58x_device() - Calculate the maximum length of 0425 * struct es58x_device. 0426 * @es58x_dev_param: The constant parameters of the device. 0427 * 0428 * The length of struct es58x_device depends on the length of its last 0429 * field: rx_cmd_buf. This macro allows to optimize the memory 0430 * allocation. 0431 * 0432 * Return: length of struct es58x_device. 0433 */ 0434 static inline size_t es58x_sizeof_es58x_device(const struct es58x_parameters 0435 *es58x_dev_param) 0436 { 0437 return offsetof(struct es58x_device, rx_cmd_buf) + 0438 es58x_dev_param->rx_urb_cmd_max_len; 0439 } 0440 0441 static inline int __es58x_check_msg_len(const struct device *dev, 0442 const char *stringified_msg, 0443 size_t actual_len, size_t expected_len) 0444 { 0445 if (expected_len != actual_len) { 0446 dev_err(dev, 0447 "Length of %s is %zu but received command is %zu.\n", 0448 stringified_msg, expected_len, actual_len); 0449 return -EMSGSIZE; 0450 } 0451 return 0; 0452 } 0453 0454 /** 0455 * es58x_check_msg_len() - Check the size of a received message. 0456 * @dev: Device, used to print error messages. 0457 * @msg: Received message, must not be a pointer. 0458 * @actual_len: Length of the message as advertised in the command header. 0459 * 0460 * Must be a macro in order to accept the different types of messages 0461 * as an input. Can be use with any of the messages which have a fixed 0462 * length. Check for an exact match of the size. 0463 * 0464 * Return: zero on success, -EMSGSIZE if @actual_len differs from the 0465 * expected length. 0466 */ 0467 #define es58x_check_msg_len(dev, msg, actual_len) \ 0468 __es58x_check_msg_len(dev, __stringify(msg), \ 0469 actual_len, sizeof(msg)) 0470 0471 static inline int __es58x_check_msg_max_len(const struct device *dev, 0472 const char *stringified_msg, 0473 size_t actual_len, 0474 size_t expected_len) 0475 { 0476 if (actual_len > expected_len) { 0477 dev_err(dev, 0478 "Maximum length for %s is %zu but received command is %zu.\n", 0479 stringified_msg, expected_len, actual_len); 0480 return -EOVERFLOW; 0481 } 0482 return 0; 0483 } 0484 0485 /** 0486 * es58x_check_msg_max_len() - Check the maximum size of a received message. 0487 * @dev: Device, used to print error messages. 0488 * @msg: Received message, must not be a pointer. 0489 * @actual_len: Length of the message as advertised in the command header. 0490 * 0491 * Must be a macro in order to accept the different types of messages 0492 * as an input. To be used with the messages of variable sizes. Only 0493 * check that the message is not bigger than the maximum expected 0494 * size. 0495 * 0496 * Return: zero on success, -EOVERFLOW if @actual_len is greater than 0497 * the expected length. 0498 */ 0499 #define es58x_check_msg_max_len(dev, msg, actual_len) \ 0500 __es58x_check_msg_max_len(dev, __stringify(msg), \ 0501 actual_len, sizeof(msg)) 0502 0503 static inline int __es58x_msg_num_element(const struct device *dev, 0504 const char *stringified_msg, 0505 size_t actual_len, size_t msg_len, 0506 size_t elem_len) 0507 { 0508 size_t actual_num_elem = actual_len / elem_len; 0509 size_t expected_num_elem = msg_len / elem_len; 0510 0511 if (actual_num_elem == 0) { 0512 dev_err(dev, 0513 "Minimum length for %s is %zu but received command is %zu.\n", 0514 stringified_msg, elem_len, actual_len); 0515 return -EMSGSIZE; 0516 } else if ((actual_len % elem_len) != 0) { 0517 dev_err(dev, 0518 "Received command length: %zu is not a multiple of %s[0]: %zu\n", 0519 actual_len, stringified_msg, elem_len); 0520 return -EMSGSIZE; 0521 } else if (actual_num_elem > expected_num_elem) { 0522 dev_err(dev, 0523 "Array %s is supposed to have %zu elements each of size %zu...\n", 0524 stringified_msg, expected_num_elem, elem_len); 0525 dev_err(dev, 0526 "... But received command has %zu elements (total length %zu).\n", 0527 actual_num_elem, actual_len); 0528 return -EOVERFLOW; 0529 } 0530 return actual_num_elem; 0531 } 0532 0533 /** 0534 * es58x_msg_num_element() - Check size and give the number of 0535 * elements in a message of array type. 0536 * @dev: Device, used to print error messages. 0537 * @msg: Received message, must be an array. 0538 * @actual_len: Length of the message as advertised in the command 0539 * header. 0540 * 0541 * Must be a macro in order to accept the different types of messages 0542 * as an input. To be used on message of array type. Array's element 0543 * has to be of fixed size (else use es58x_check_msg_max_len()). Check 0544 * that the total length is an exact multiple of the length of a 0545 * single element. 0546 * 0547 * Return: number of elements in the array on success, -EOVERFLOW if 0548 * @actual_len is greater than the expected length, -EMSGSIZE if 0549 * @actual_len is not a multiple of a single element. 0550 */ 0551 #define es58x_msg_num_element(dev, msg, actual_len) \ 0552 ({ \ 0553 size_t __elem_len = sizeof((msg)[0]) + __must_be_array(msg); \ 0554 __es58x_msg_num_element(dev, __stringify(msg), actual_len, \ 0555 sizeof(msg), __elem_len); \ 0556 }) 0557 0558 /** 0559 * es58x_priv() - Get the priv member and cast it to struct es58x_priv. 0560 * @netdev: CAN network device. 0561 * 0562 * Return: ES58X device. 0563 */ 0564 static inline struct es58x_priv *es58x_priv(struct net_device *netdev) 0565 { 0566 return (struct es58x_priv *)netdev_priv(netdev); 0567 } 0568 0569 /** 0570 * ES58X_SIZEOF_URB_CMD() - Calculate the maximum length of an urb 0571 * command for a given message field name. 0572 * @es58x_urb_cmd_type: type (either "struct es581_4_urb_cmd" or 0573 * "struct es58x_fd_urb_cmd"). 0574 * @msg_field: name of the message field. 0575 * 0576 * Must be a macro in order to accept the different command types as 0577 * an input. 0578 * 0579 * Return: length of the urb command. 0580 */ 0581 #define ES58X_SIZEOF_URB_CMD(es58x_urb_cmd_type, msg_field) \ 0582 (offsetof(es58x_urb_cmd_type, raw_msg) \ 0583 + sizeof_field(es58x_urb_cmd_type, msg_field) \ 0584 + sizeof_field(es58x_urb_cmd_type, \ 0585 reserved_for_crc16_do_not_use)) 0586 0587 /** 0588 * es58x_get_urb_cmd_len() - Calculate the actual length of an urb 0589 * command for a given message length. 0590 * @es58x_dev: ES58X device. 0591 * @msg_len: Length of the message. 0592 * 0593 * Add the header and CRC lengths to the message length. 0594 * 0595 * Return: length of the urb command. 0596 */ 0597 static inline size_t es58x_get_urb_cmd_len(struct es58x_device *es58x_dev, 0598 u16 msg_len) 0599 { 0600 return es58x_dev->param->urb_cmd_header_len + msg_len + sizeof(u16); 0601 } 0602 0603 /** 0604 * es58x_get_netdev() - Get the network device. 0605 * @es58x_dev: ES58X device. 0606 * @channel_no: The channel number as advertised in the urb command. 0607 * @channel_idx_offset: Some of the ES58x starts channel numbering 0608 * from 0 (ES58X FD), others from 1 (ES581.4). 0609 * @netdev: CAN network device. 0610 * 0611 * Do a sanity check on the index provided by the device. 0612 * 0613 * Return: zero on success, -ECHRNG if the received channel number is 0614 * out of range and -ENODEV if the network device is not yet 0615 * configured. 0616 */ 0617 static inline int es58x_get_netdev(struct es58x_device *es58x_dev, 0618 int channel_no, int channel_idx_offset, 0619 struct net_device **netdev) 0620 { 0621 int channel_idx = channel_no - channel_idx_offset; 0622 0623 *netdev = NULL; 0624 if (channel_idx < 0 || channel_idx >= es58x_dev->num_can_ch) 0625 return -ECHRNG; 0626 0627 *netdev = es58x_dev->netdev[channel_idx]; 0628 if (!*netdev || !netif_device_present(*netdev)) 0629 return -ENODEV; 0630 0631 return 0; 0632 } 0633 0634 /** 0635 * es58x_get_raw_can_id() - Get the CAN ID. 0636 * @cf: CAN frame. 0637 * 0638 * Mask the CAN ID in order to only keep the significant bits. 0639 * 0640 * Return: the raw value of the CAN ID. 0641 */ 0642 static inline int es58x_get_raw_can_id(const struct can_frame *cf) 0643 { 0644 if (cf->can_id & CAN_EFF_FLAG) 0645 return cf->can_id & CAN_EFF_MASK; 0646 else 0647 return cf->can_id & CAN_SFF_MASK; 0648 } 0649 0650 /** 0651 * es58x_get_flags() - Get the CAN flags. 0652 * @skb: socket buffer of a CAN message. 0653 * 0654 * Return: the CAN flag as an enum es58x_flag. 0655 */ 0656 static inline enum es58x_flag es58x_get_flags(const struct sk_buff *skb) 0657 { 0658 struct canfd_frame *cf = (struct canfd_frame *)skb->data; 0659 enum es58x_flag es58x_flags = 0; 0660 0661 if (cf->can_id & CAN_EFF_FLAG) 0662 es58x_flags |= ES58X_FLAG_EFF; 0663 0664 if (can_is_canfd_skb(skb)) { 0665 es58x_flags |= ES58X_FLAG_FD_DATA; 0666 if (cf->flags & CANFD_BRS) 0667 es58x_flags |= ES58X_FLAG_FD_BRS; 0668 if (cf->flags & CANFD_ESI) 0669 es58x_flags |= ES58X_FLAG_FD_ESI; 0670 } else if (cf->can_id & CAN_RTR_FLAG) 0671 /* Remote frames are only defined in Classical CAN frames */ 0672 es58x_flags |= ES58X_FLAG_RTR; 0673 0674 return es58x_flags; 0675 } 0676 0677 int es58x_can_get_echo_skb(struct net_device *netdev, u32 packet_idx, 0678 u64 *tstamps, unsigned int pkts); 0679 int es58x_tx_ack_msg(struct net_device *netdev, u16 tx_free_entries, 0680 enum es58x_ret_u32 rx_cmd_ret_u32); 0681 int es58x_rx_can_msg(struct net_device *netdev, u64 timestamp, const u8 *data, 0682 canid_t can_id, enum es58x_flag es58x_flags, u8 dlc); 0683 int es58x_rx_err_msg(struct net_device *netdev, enum es58x_err error, 0684 enum es58x_event event, u64 timestamp); 0685 void es58x_rx_timestamp(struct es58x_device *es58x_dev, u64 timestamp); 0686 int es58x_rx_cmd_ret_u8(struct device *dev, enum es58x_ret_type cmd_ret_type, 0687 enum es58x_ret_u8 rx_cmd_ret_u8); 0688 int es58x_rx_cmd_ret_u32(struct net_device *netdev, 0689 enum es58x_ret_type cmd_ret_type, 0690 enum es58x_ret_u32 rx_cmd_ret_u32); 0691 int es58x_send_msg(struct es58x_device *es58x_dev, u8 cmd_type, u8 cmd_id, 0692 const void *msg, u16 cmd_len, int channel_idx); 0693 0694 extern const struct es58x_parameters es581_4_param; 0695 extern const struct es58x_operators es581_4_ops; 0696 0697 extern const struct es58x_parameters es58x_fd_param; 0698 extern const struct es58x_operators es58x_fd_ops; 0699 0700 #endif /* __ES58X_COMMON_H__ */
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.1.0 LXR engine. The LXR team |
![]() ![]() |