summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVincent Mailhol <mailhol@kernel.org>2025-09-23 15:58:44 +0900
committerMarc Kleine-Budde <mkl@pengutronix.de>2025-09-24 17:09:47 +0200
commit7de54546fff11cb0a53f47847d62f7b1a5792d17 (patch)
treee9212e051468f81c5962dda623beae78f872b29f
parent6ffc1230d3a728e07d7d2464f388ad4bbefe90c2 (diff)
can: dev: add can_get_ctrlmode_str()
In an effort to give more human readable messages when errors occur because of conflicting options, it can be useful to convert the CAN control mode flags into text. Add a function which converts the first set CAN control mode into a human readable string. The reason to only convert the first one is to simplify edge cases: imagine that there are several invalid control modes, we would just return the first invalid one to the user, thus not having to handle complex string concatenation. The user can then solve the first problem, call the netlink interface again and see the next issue. People who wish to enumerate all the control modes can still do so by, for example, using this new function in a for_each_set_bit() loop. Signed-off-by: Vincent Mailhol <mailhol@kernel.org> Link: https://patch.msgid.link/20250923-canxl-netlink-prep-v4-19-e720d28f66fe@kernel.org Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
-rw-r--r--drivers/net/can/dev/dev.c33
-rw-r--r--include/linux/can/dev.h2
2 files changed, 35 insertions, 0 deletions
diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c
index befdeb4c54c2..15ccedbb3f8d 100644
--- a/drivers/net/can/dev/dev.c
+++ b/drivers/net/can/dev/dev.c
@@ -88,6 +88,39 @@ const char *can_get_state_str(const enum can_state state)
}
EXPORT_SYMBOL_GPL(can_get_state_str);
+const char *can_get_ctrlmode_str(u32 ctrlmode)
+{
+ switch (ctrlmode & ~(ctrlmode - 1)) {
+ case 0:
+ return "none";
+ case CAN_CTRLMODE_LOOPBACK:
+ return "loopback";
+ case CAN_CTRLMODE_LISTENONLY:
+ return "listen-only";
+ case CAN_CTRLMODE_3_SAMPLES:
+ return "triple-sampling";
+ case CAN_CTRLMODE_ONE_SHOT:
+ return "one-shot";
+ case CAN_CTRLMODE_BERR_REPORTING:
+ return "berr-reporting";
+ case CAN_CTRLMODE_FD:
+ return "fd";
+ case CAN_CTRLMODE_PRESUME_ACK:
+ return "presume-ack";
+ case CAN_CTRLMODE_FD_NON_ISO:
+ return "fd-non-iso";
+ case CAN_CTRLMODE_CC_LEN8_DLC:
+ return "cc-len8-dlc";
+ case CAN_CTRLMODE_TDC_AUTO:
+ return "fd-tdc-auto";
+ case CAN_CTRLMODE_TDC_MANUAL:
+ return "fd-tdc-manual";
+ default:
+ return "<unknown>";
+ }
+}
+EXPORT_SYMBOL_GPL(can_get_ctrlmode_str);
+
static enum can_state can_state_err_to_state(u16 err)
{
if (err < CAN_ERROR_WARNING_THRESHOLD)
diff --git a/include/linux/can/dev.h b/include/linux/can/dev.h
index 8e75e9b3830a..a2229a61ccde 100644
--- a/include/linux/can/dev.h
+++ b/include/linux/can/dev.h
@@ -141,6 +141,8 @@ int can_restart_now(struct net_device *dev);
void can_bus_off(struct net_device *dev);
const char *can_get_state_str(const enum can_state state);
+const char *can_get_ctrlmode_str(u32 ctrlmode);
+
void can_state_get_by_berr_counter(const struct net_device *dev,
const struct can_berr_counter *bec,
enum can_state *tx_state,