Skip to content

Commit bdd2e41

Browse files
can: dev: move length related code into seperate file
This patch moves all CAN frame length related code of the CAN device infrastructure into a separate file. Reviewed-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr> Link: https://lore.kernel.org/r/20210111141930.693847-5-mkl@pengutronix.de Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
1 parent 5a9d5ec commit bdd2e41

File tree

6 files changed

+89
-73
lines changed

6 files changed

+89
-73
lines changed

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3946,6 +3946,7 @@ F: drivers/net/can/
39463946
F: include/linux/can/bittiming.h
39473947
F: include/linux/can/dev.h
39483948
F: include/linux/can/led.h
3949+
F: include/linux/can/length.h
39493950
F: include/linux/can/platform/
39503951
F: include/linux/can/rx-offload.h
39513952
F: include/uapi/linux/can/error.h

drivers/net/can/dev/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
obj-$(CONFIG_CAN_DEV) += can-dev.o
44
can-dev-y += bittiming.o
55
can-dev-y += dev.o
6+
can-dev-y += length.o
67
can-dev-y += rx-offload.o
78

89
can-dev-$(CONFIG_CAN_LEDS) += led.o

drivers/net/can/dev/dev.c

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,39 +25,6 @@ MODULE_DESCRIPTION(MOD_DESC);
2525
MODULE_LICENSE("GPL v2");
2626
MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
2727

28-
/* CAN DLC to real data length conversion helpers */
29-
30-
static const u8 dlc2len[] = {0, 1, 2, 3, 4, 5, 6, 7,
31-
8, 12, 16, 20, 24, 32, 48, 64};
32-
33-
/* get data length from raw data length code (DLC) */
34-
u8 can_fd_dlc2len(u8 dlc)
35-
{
36-
return dlc2len[dlc & 0x0F];
37-
}
38-
EXPORT_SYMBOL_GPL(can_fd_dlc2len);
39-
40-
static const u8 len2dlc[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, /* 0 - 8 */
41-
9, 9, 9, 9, /* 9 - 12 */
42-
10, 10, 10, 10, /* 13 - 16 */
43-
11, 11, 11, 11, /* 17 - 20 */
44-
12, 12, 12, 12, /* 21 - 24 */
45-
13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */
46-
14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */
47-
14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */
48-
15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */
49-
15, 15, 15, 15, 15, 15, 15, 15}; /* 57 - 64 */
50-
51-
/* map the sanitized data length to an appropriate data length code */
52-
u8 can_fd_len2dlc(u8 len)
53-
{
54-
if (unlikely(len > 64))
55-
return 0xF;
56-
57-
return len2dlc[len];
58-
}
59-
EXPORT_SYMBOL_GPL(can_fd_len2dlc);
60-
6128
static void can_update_state_error_stats(struct net_device *dev,
6229
enum can_state new_state)
6330
{

drivers/net/can/dev/length.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/* Copyright (C) 2012, 2020 Oliver Hartkopp <socketcan@hartkopp.net>
3+
*/
4+
5+
#include <linux/can/dev.h>
6+
7+
/* CAN DLC to real data length conversion helpers */
8+
9+
static const u8 dlc2len[] = {0, 1, 2, 3, 4, 5, 6, 7,
10+
8, 12, 16, 20, 24, 32, 48, 64};
11+
12+
/* get data length from raw data length code (DLC) */
13+
u8 can_fd_dlc2len(u8 dlc)
14+
{
15+
return dlc2len[dlc & 0x0F];
16+
}
17+
EXPORT_SYMBOL_GPL(can_fd_dlc2len);
18+
19+
static const u8 len2dlc[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, /* 0 - 8 */
20+
9, 9, 9, 9, /* 9 - 12 */
21+
10, 10, 10, 10, /* 13 - 16 */
22+
11, 11, 11, 11, /* 17 - 20 */
23+
12, 12, 12, 12, /* 21 - 24 */
24+
13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */
25+
14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */
26+
14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */
27+
15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */
28+
15, 15, 15, 15, 15, 15, 15, 15}; /* 57 - 64 */
29+
30+
/* map the sanitized data length to an appropriate data length code */
31+
u8 can_fd_len2dlc(u8 len)
32+
{
33+
if (unlikely(len > 64))
34+
return 0xF;
35+
36+
return len2dlc[len];
37+
}
38+
EXPORT_SYMBOL_GPL(can_fd_len2dlc);

include/linux/can/dev.h

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/can/bittiming.h>
1919
#include <linux/can/error.h>
2020
#include <linux/can/led.h>
21+
#include <linux/can/length.h>
2122
#include <linux/can/netlink.h>
2223
#include <linux/can/skb.h>
2324
#include <linux/netdevice.h>
@@ -83,15 +84,6 @@ struct can_priv {
8384
#endif
8485
};
8586

86-
/*
87-
* can_cc_dlc2len(value) - convert a given data length code (dlc) of a
88-
* Classical CAN frame into a valid data length of max. 8 bytes.
89-
*
90-
* To be used in the CAN netdriver receive path to ensure conformance with
91-
* ISO 11898-1 Chapter 8.4.2.3 (DLC field)
92-
*/
93-
#define can_cc_dlc2len(dlc) (min_t(u8, (dlc), CAN_MAX_DLEN))
94-
9587
/* Check for outgoing skbs that have not been created by the CAN subsystem */
9688
static inline bool can_skb_headroom_valid(struct net_device *dev,
9789
struct sk_buff *skb)
@@ -156,31 +148,6 @@ static inline bool can_is_canfd_skb(const struct sk_buff *skb)
156148
return skb->len == CANFD_MTU;
157149
}
158150

159-
/* helper to get the data length code (DLC) for Classical CAN raw DLC access */
160-
static inline u8 can_get_cc_dlc(const struct can_frame *cf, const u32 ctrlmode)
161-
{
162-
/* return len8_dlc as dlc value only if all conditions apply */
163-
if ((ctrlmode & CAN_CTRLMODE_CC_LEN8_DLC) &&
164-
(cf->len == CAN_MAX_DLEN) &&
165-
(cf->len8_dlc > CAN_MAX_DLEN && cf->len8_dlc <= CAN_MAX_RAW_DLC))
166-
return cf->len8_dlc;
167-
168-
/* return the payload length as dlc value */
169-
return cf->len;
170-
}
171-
172-
/* helper to set len and len8_dlc value for Classical CAN raw DLC access */
173-
static inline void can_frame_set_cc_len(struct can_frame *cf, const u8 dlc,
174-
const u32 ctrlmode)
175-
{
176-
/* the caller already ensured that dlc is a value from 0 .. 15 */
177-
if (ctrlmode & CAN_CTRLMODE_CC_LEN8_DLC && dlc > CAN_MAX_DLEN)
178-
cf->len8_dlc = dlc;
179-
180-
/* limit the payload length 'len' to CAN_MAX_DLEN */
181-
cf->len = can_cc_dlc2len(dlc);
182-
}
183-
184151
/* helper to define static CAN controller features at device creation time */
185152
static inline void can_set_static_ctrlmode(struct net_device *dev,
186153
u32 static_mode)
@@ -196,12 +163,6 @@ static inline void can_set_static_ctrlmode(struct net_device *dev,
196163
dev->mtu = CANFD_MTU;
197164
}
198165

199-
/* get data length from raw data length code (DLC) */
200-
u8 can_fd_dlc2len(u8 dlc);
201-
202-
/* map the sanitized data length to an appropriate data length code */
203-
u8 can_fd_len2dlc(u8 len);
204-
205166
struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
206167
unsigned int txqs, unsigned int rxqs);
207168
#define alloc_candev(sizeof_priv, echo_skb_max) \

include/linux/can/length.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/* Copyright (C) 2020 Oliver Hartkopp <socketcan@hartkopp.net>
3+
*/
4+
5+
#ifndef _CAN_LENGTH_H
6+
#define _CAN_LENGTH_H
7+
8+
/*
9+
* can_cc_dlc2len(value) - convert a given data length code (dlc) of a
10+
* Classical CAN frame into a valid data length of max. 8 bytes.
11+
*
12+
* To be used in the CAN netdriver receive path to ensure conformance with
13+
* ISO 11898-1 Chapter 8.4.2.3 (DLC field)
14+
*/
15+
#define can_cc_dlc2len(dlc) (min_t(u8, (dlc), CAN_MAX_DLEN))
16+
17+
/* helper to get the data length code (DLC) for Classical CAN raw DLC access */
18+
static inline u8 can_get_cc_dlc(const struct can_frame *cf, const u32 ctrlmode)
19+
{
20+
/* return len8_dlc as dlc value only if all conditions apply */
21+
if ((ctrlmode & CAN_CTRLMODE_CC_LEN8_DLC) &&
22+
(cf->len == CAN_MAX_DLEN) &&
23+
(cf->len8_dlc > CAN_MAX_DLEN && cf->len8_dlc <= CAN_MAX_RAW_DLC))
24+
return cf->len8_dlc;
25+
26+
/* return the payload length as dlc value */
27+
return cf->len;
28+
}
29+
30+
/* helper to set len and len8_dlc value for Classical CAN raw DLC access */
31+
static inline void can_frame_set_cc_len(struct can_frame *cf, const u8 dlc,
32+
const u32 ctrlmode)
33+
{
34+
/* the caller already ensured that dlc is a value from 0 .. 15 */
35+
if (ctrlmode & CAN_CTRLMODE_CC_LEN8_DLC && dlc > CAN_MAX_DLEN)
36+
cf->len8_dlc = dlc;
37+
38+
/* limit the payload length 'len' to CAN_MAX_DLEN */
39+
cf->len = can_cc_dlc2len(dlc);
40+
}
41+
42+
/* get data length from raw data length code (DLC) */
43+
u8 can_fd_dlc2len(u8 dlc);
44+
45+
/* map the sanitized data length to an appropriate data length code */
46+
u8 can_fd_len2dlc(u8 len);
47+
48+
#endif /* !_CAN_LENGTH_H */

0 commit comments

Comments
 (0)