aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Charvat <charvj10@fel.cvut.cz>2020-09-14 10:13:38 +0200
committerPaolo Bonzini <pbonzini@redhat.com>2020-09-30 19:11:37 +0200
commitad0c6740d1085a67f319ed1432598e3f3912a707 (patch)
treed3792d8595fd52c1428badb2fd0008532723b001
parent46b25fe08b37ea87b7c576a37c27c608f4d7621e (diff)
net/can: Add can_dlc2len and can_len2dlc for CAN FD.
Signed-off-by: Jan Charvat <charvj10@fel.cvut.cz> Signed-off-by: Pavel Pisa <pisa@cmp.felk.cvut.cz> Reviewed-by: Vikram Garhwal <fnu.vikram@xilinx.com> Message-Id: <0a2efc6ef9c458505952ed230e49ae25cad7f324.1600069689.git.pisa@cmp.felk.cvut.cz> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
-rw-r--r--include/net/can_emu.h4
-rw-r--r--net/can/can_core.c36
2 files changed, 40 insertions, 0 deletions
diff --git a/include/net/can_emu.h b/include/net/can_emu.h
index cab98ee8ec..6f9b206bb5 100644
--- a/include/net/can_emu.h
+++ b/include/net/can_emu.h
@@ -122,4 +122,8 @@ int can_bus_client_set_filters(CanBusClientState *,
const struct qemu_can_filter *filters,
size_t filters_cnt);
+uint8_t can_dlc2len(uint8_t can_dlc);
+
+uint8_t can_len2dlc(uint8_t len);
+
#endif
diff --git a/net/can/can_core.c b/net/can/can_core.c
index 90f4d8576a..0115d78794 100644
--- a/net/can/can_core.c
+++ b/net/can/can_core.c
@@ -33,6 +33,42 @@
#include "net/can_emu.h"
#include "qom/object_interfaces.h"
+/* CAN DLC to real data length conversion helpers */
+
+static const uint8_t dlc2len[] = {
+ 0, 1, 2, 3, 4, 5, 6, 7,
+ 8, 12, 16, 20, 24, 32, 48, 64
+};
+
+/* get data length from can_dlc with sanitized can_dlc */
+uint8_t can_dlc2len(uint8_t can_dlc)
+{
+ return dlc2len[can_dlc & 0x0F];
+}
+
+static const uint8_t len2dlc[] = {
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, /* 0 - 8 */
+ 9, 9, 9, 9, /* 9 - 12 */
+ 10, 10, 10, 10, /* 13 - 16 */
+ 11, 11, 11, 11, /* 17 - 20 */
+ 12, 12, 12, 12, /* 21 - 24 */
+ 13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */
+ 14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */
+ 14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */
+ 15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */
+ 15, 15, 15, 15, 15, 15, 15, 15 /* 57 - 64 */
+};
+
+/* map the sanitized data length to an appropriate data length code */
+uint8_t can_len2dlc(uint8_t len)
+{
+ if (unlikely(len > 64)) {
+ return 0xF;
+ }
+
+ return len2dlc[len];
+}
+
struct CanBusState {
Object object;