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