Skip to content

Commit 7de5454

Browse files
vincent-mailholmarckleinebudde
authored andcommitted
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 <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Marc Kleine-Budde <[email protected]>
1 parent 6ffc123 commit 7de5454

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

drivers/net/can/dev/dev.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,39 @@ const char *can_get_state_str(const enum can_state state)
8888
}
8989
EXPORT_SYMBOL_GPL(can_get_state_str);
9090

91+
const char *can_get_ctrlmode_str(u32 ctrlmode)
92+
{
93+
switch (ctrlmode & ~(ctrlmode - 1)) {
94+
case 0:
95+
return "none";
96+
case CAN_CTRLMODE_LOOPBACK:
97+
return "loopback";
98+
case CAN_CTRLMODE_LISTENONLY:
99+
return "listen-only";
100+
case CAN_CTRLMODE_3_SAMPLES:
101+
return "triple-sampling";
102+
case CAN_CTRLMODE_ONE_SHOT:
103+
return "one-shot";
104+
case CAN_CTRLMODE_BERR_REPORTING:
105+
return "berr-reporting";
106+
case CAN_CTRLMODE_FD:
107+
return "fd";
108+
case CAN_CTRLMODE_PRESUME_ACK:
109+
return "presume-ack";
110+
case CAN_CTRLMODE_FD_NON_ISO:
111+
return "fd-non-iso";
112+
case CAN_CTRLMODE_CC_LEN8_DLC:
113+
return "cc-len8-dlc";
114+
case CAN_CTRLMODE_TDC_AUTO:
115+
return "fd-tdc-auto";
116+
case CAN_CTRLMODE_TDC_MANUAL:
117+
return "fd-tdc-manual";
118+
default:
119+
return "<unknown>";
120+
}
121+
}
122+
EXPORT_SYMBOL_GPL(can_get_ctrlmode_str);
123+
91124
static enum can_state can_state_err_to_state(u16 err)
92125
{
93126
if (err < CAN_ERROR_WARNING_THRESHOLD)

include/linux/can/dev.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ int can_restart_now(struct net_device *dev);
141141
void can_bus_off(struct net_device *dev);
142142

143143
const char *can_get_state_str(const enum can_state state);
144+
const char *can_get_ctrlmode_str(u32 ctrlmode);
145+
144146
void can_state_get_by_berr_counter(const struct net_device *dev,
145147
const struct can_berr_counter *bec,
146148
enum can_state *tx_state,

0 commit comments

Comments
 (0)