summaryrefslogtreecommitdiff
path: root/drivers/net/can/dev/length.c
blob: 64673a8d1234e03af4a13fb72f8e9284c4eda6a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// SPDX-License-Identifier: GPL-2.0-only
/* Copyright (C) 2012, 2020 Oliver Hartkopp <socketcan@hartkopp.net>
 */

#include <linux/can/dev.h>

/* CAN DLC to real data length conversion helpers */

static const u8 dlc2len[] = {
	0, 1, 2, 3, 4, 5, 6, 7,
	8, 12, 16, 20, 24, 32, 48, 64
};

/* get data length from raw data length code (DLC) */
u8 can_fd_dlc2len(u8 dlc)
{
	return dlc2len[dlc & 0x0F];
}
EXPORT_SYMBOL_GPL(can_fd_dlc2len);

static const u8 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 */
};

/* map the sanitized data length to an appropriate data length code */
u8 can_fd_len2dlc(u8 len)
{
	if (len >= ARRAY_SIZE(len2dlc))
		return CANFD_MAX_DLC;

	return len2dlc[len];
}
EXPORT_SYMBOL_GPL(can_fd_len2dlc);