1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2012, 2020 Oliver Hartkopp <socketcan@hartkopp.net>
5 #include <linux/can/dev.h>
7 /* CAN DLC to real data length conversion helpers */
9 static const u8 dlc2len
[] = {
10 0, 1, 2, 3, 4, 5, 6, 7,
11 8, 12, 16, 20, 24, 32, 48, 64
14 /* get data length from raw data length code (DLC) */
15 u8
can_fd_dlc2len(u8 dlc
)
17 return dlc2len
[dlc
& 0x0F];
19 EXPORT_SYMBOL_GPL(can_fd_dlc2len
);
21 static const u8 len2dlc
[] = {
22 0, 1, 2, 3, 4, 5, 6, 7, 8, /* 0 - 8 */
23 9, 9, 9, 9, /* 9 - 12 */
24 10, 10, 10, 10, /* 13 - 16 */
25 11, 11, 11, 11, /* 17 - 20 */
26 12, 12, 12, 12, /* 21 - 24 */
27 13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */
28 14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */
29 14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */
30 15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */
31 15, 15, 15, 15, 15, 15, 15, 15 /* 57 - 64 */
34 /* map the sanitized data length to an appropriate data length code */
35 u8
can_fd_len2dlc(u8 len
)
37 /* check for length mapping table size at build time */
38 BUILD_BUG_ON(ARRAY_SIZE(len2dlc
) != CANFD_MAX_DLEN
+ 1);
40 if (unlikely(len
> CANFD_MAX_DLEN
))
45 EXPORT_SYMBOL_GPL(can_fd_len2dlc
);
48 * can_skb_get_frame_len() - Calculate the CAN Frame length in bytes
50 * @skb: socket buffer of a CAN message.
52 * Do a rough calculation: bit stuffing is ignored and length in bits
53 * is rounded up to a length in bytes.
55 * Rationale: this function is to be used for the BQL functions
56 * (netdev_sent_queue() and netdev_completed_queue()) which expect a
57 * value in bytes. Just using skb->len is insufficient because it will
58 * return the constant value of CAN(FD)_MTU. Doing the bit stuffing
59 * calculation would be too expensive in term of computing resources
60 * for no noticeable gain.
62 * Remarks: The payload of CAN FD frames with BRS flag are sent at a
63 * different bitrate. Currently, the can-utils canbusload tool does
64 * not support CAN-FD yet and so we could not run any benchmark to
65 * measure the impact. There might be possible improvement here.
67 * Return: length in bytes.
69 unsigned int can_skb_get_frame_len(const struct sk_buff
*skb
)
71 const struct canfd_frame
*cf
= (const struct canfd_frame
*)skb
->data
;
74 if (can_is_canfd_skb(skb
))
75 len
= canfd_sanitize_len(cf
->len
);
76 else if (cf
->can_id
& CAN_RTR_FLAG
)
81 return can_frame_bytes(can_is_canfd_skb(skb
), cf
->can_id
& CAN_EFF_FLAG
,
84 EXPORT_SYMBOL_GPL(can_skb_get_frame_len
);