[RFC,3/6] can: dev: add helper macros to setup an error frame

Message ID 20241014152431.2045377-4-dario.binacchi@amarulasolutions.com
State New
Headers show
Series
  • This series originates from some tests I ran on a CAN communication for
Related show

Commit Message

Dario Binacchi Oct. 14, 2024, 3:24 p.m. UTC
These helpers can prevent errors and code duplication when setting up a
CAN error frame.

Signed-off-by: Dario Binacchi <dario.binacchi@amarulasolutions.com>
---

 include/linux/can/dev.h | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

Comments

Marc Kleine-Budde Oct. 14, 2024, 8 p.m. UTC | #1
On 14.10.2024 17:24:18, Dario Binacchi wrote:
> These helpers can prevent errors and code duplication when setting up a
> CAN error frame.

I personally don't like the ideas of using macros here. Is there a
reason not to use static inline functions?

Marc
Dario Binacchi Oct. 15, 2024, 7:47 a.m. UTC | #2
Hello Marc,

On Mon, Oct 14, 2024 at 10:00 PM Marc Kleine-Budde <mkl@pengutronix.de> wrote:
>
> On 14.10.2024 17:24:18, Dario Binacchi wrote:
> > These helpers can prevent errors and code duplication when setting up a
> > CAN error frame.
>
> I personally don't like the ideas of using macros here. Is there a
> reason not to use static inline functions?

I thought that the use of macros would certainly not introduce
additional overhead
compared to the previous version. In version 2, I will replace the
macros with inline
functions.

I noticed that the ACK error is handled differently by the drivers.

bxcan, flexcan, slcan, rcar_can.c, and xilinx_can, for example:
cf->can_id |= CAN_ERR_ACK;
cf->data[3] = CAN_ERR_PROT_LOC_ACK;

at91_can, mcp251xfd-core:
cf->can_id |= CAN_ERR_ACK;
cf->data[2] |= CAN_ERR_PROT_TX;

cc770, kvaser_pciefd and es58x_core only
cf->can_id |= CAN_ERR_ACK;

So, what is the correct/best way to notify a CAN frame error from an ACK?

Thanks and regards,
Dario

>
> Marc
>
> --
> Pengutronix e.K.                 | Marc Kleine-Budde          |
> Embedded Linux                   | https://www.pengutronix.de |
> Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
> Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |

Patch

diff --git a/include/linux/can/dev.h b/include/linux/can/dev.h
index 0977656b366d..0202526be6b0 100644
--- a/include/linux/can/dev.h
+++ b/include/linux/can/dev.h
@@ -23,6 +23,33 @@ 
 #include <linux/ethtool.h>
 #include <linux/netdevice.h>
 
+#define CAN_FRAME_ERROR_INIT(cf) \
+	((cf)->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR)
+
+#define CAN_FRAME_SET_ERR_BIT0(cf) \
+	((cf)->data[2] |= CAN_ERR_PROT_BIT0)
+
+#define CAN_FRAME_SET_ERR_BIT1(cf) \
+	((cf)->data[2] |= CAN_ERR_PROT_BIT1)
+
+#define CAN_FRAME_SET_ERR_ACK(cf) \
+	do { \
+		((cf)->can_id |= CAN_ERR_ACK); \
+		((cf)->data[3] = CAN_ERR_PROT_LOC_ACK); \
+	} while (0)
+
+#define CAN_FRAME_SET_ERR_CRC(cf) \
+	do { \
+		((cf)->data[2] |= CAN_ERR_PROT_BIT); \
+		((cf)->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ); \
+	} while (0)
+
+#define CAN_FRAME_SET_ERR_FORM(cf) \
+	((cf)->data[2] |= CAN_ERR_PROT_FORM)
+
+#define CAN_FRAME_SET_ERR_STUFF(cf) \
+	((cf)->data[2] |= CAN_ERR_PROT_STUFF)
+
 /*
  * CAN mode
  */