Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

can: dev: add can_state_get_by_berr_counter() to return the CAN state based on the current error counters

Some CAN controllers do not have a register that contains the current
CAN state, but only a register that contains the error counters.

Introduce a new function can_state_get_by_berr_counter() that returns
the current TX and RX state depending on the provided CAN bit error
counters.

Link: https://lore.kernel.org/all/20231005-at91_can-rx_offload-v2-1-9987d53600e0@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>

+26
+22
drivers/net/can/dev/dev.c
··· 90 90 } 91 91 EXPORT_SYMBOL_GPL(can_get_state_str); 92 92 93 + static enum can_state can_state_err_to_state(u16 err) 94 + { 95 + if (err < CAN_ERROR_WARNING_THRESHOLD) 96 + return CAN_STATE_ERROR_ACTIVE; 97 + if (err < CAN_ERROR_PASSIVE_THRESHOLD) 98 + return CAN_STATE_ERROR_WARNING; 99 + if (err < CAN_BUS_OFF_THRESHOLD) 100 + return CAN_STATE_ERROR_PASSIVE; 101 + 102 + return CAN_STATE_BUS_OFF; 103 + } 104 + 105 + void can_state_get_by_berr_counter(const struct net_device *dev, 106 + const struct can_berr_counter *bec, 107 + enum can_state *tx_state, 108 + enum can_state *rx_state) 109 + { 110 + *tx_state = can_state_err_to_state(bec->txerr); 111 + *rx_state = can_state_err_to_state(bec->rxerr); 112 + } 113 + EXPORT_SYMBOL_GPL(can_state_get_by_berr_counter); 114 + 93 115 void can_change_state(struct net_device *dev, struct can_frame *cf, 94 116 enum can_state tx_state, enum can_state rx_state) 95 117 {
+4
include/linux/can/dev.h
··· 195 195 void can_bus_off(struct net_device *dev); 196 196 197 197 const char *can_get_state_str(const enum can_state state); 198 + void can_state_get_by_berr_counter(const struct net_device *dev, 199 + const struct can_berr_counter *bec, 200 + enum can_state *tx_state, 201 + enum can_state *rx_state); 198 202 void can_change_state(struct net_device *dev, struct can_frame *cf, 199 203 enum can_state tx_state, enum can_state rx_state); 200 204