xref: /linux/include/uapi/linux/can.h (revision 100c85421b52e41269ada88f7d71a6b8a06c7a11)
13926a3a0SYegor Yefremov /* SPDX-License-Identifier: ((GPL-2.0-only WITH Linux-syscall-note) OR BSD-3-Clause) */
2607ca46eSDavid Howells /*
3607ca46eSDavid Howells  * linux/can.h
4607ca46eSDavid Howells  *
5607ca46eSDavid Howells  * Definitions for CAN network layer (socket addr / CAN frame / CAN filter)
6607ca46eSDavid Howells  *
7607ca46eSDavid Howells  * Authors: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
8607ca46eSDavid Howells  *          Urs Thuermann   <urs.thuermann@volkswagen.de>
9607ca46eSDavid Howells  * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
10607ca46eSDavid Howells  * All rights reserved.
11607ca46eSDavid Howells  *
1216f6b87aSUwe Kleine-König  * Redistribution and use in source and binary forms, with or without
1316f6b87aSUwe Kleine-König  * modification, are permitted provided that the following conditions
1416f6b87aSUwe Kleine-König  * are met:
1516f6b87aSUwe Kleine-König  * 1. Redistributions of source code must retain the above copyright
1616f6b87aSUwe Kleine-König  *    notice, this list of conditions and the following disclaimer.
1716f6b87aSUwe Kleine-König  * 2. Redistributions in binary form must reproduce the above copyright
1816f6b87aSUwe Kleine-König  *    notice, this list of conditions and the following disclaimer in the
1916f6b87aSUwe Kleine-König  *    documentation and/or other materials provided with the distribution.
2016f6b87aSUwe Kleine-König  * 3. Neither the name of Volkswagen nor the names of its contributors
2116f6b87aSUwe Kleine-König  *    may be used to endorse or promote products derived from this software
2216f6b87aSUwe Kleine-König  *    without specific prior written permission.
2316f6b87aSUwe Kleine-König  *
2416f6b87aSUwe Kleine-König  * Alternatively, provided that this notice is retained in full, this
2516f6b87aSUwe Kleine-König  * software may be distributed under the terms of the GNU General
2616f6b87aSUwe Kleine-König  * Public License ("GPL") version 2, in which case the provisions of the
2716f6b87aSUwe Kleine-König  * GPL apply INSTEAD OF those given above.
2816f6b87aSUwe Kleine-König  *
2916f6b87aSUwe Kleine-König  * The provided data structures and external interfaces from this code
3016f6b87aSUwe Kleine-König  * are not restricted to be used by modules with a GPL compatible license.
3116f6b87aSUwe Kleine-König  *
3216f6b87aSUwe Kleine-König  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3316f6b87aSUwe Kleine-König  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3416f6b87aSUwe Kleine-König  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3516f6b87aSUwe Kleine-König  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3616f6b87aSUwe Kleine-König  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3716f6b87aSUwe Kleine-König  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3816f6b87aSUwe Kleine-König  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3916f6b87aSUwe Kleine-König  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
4016f6b87aSUwe Kleine-König  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
4116f6b87aSUwe Kleine-König  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
4216f6b87aSUwe Kleine-König  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
4316f6b87aSUwe Kleine-König  * DAMAGE.
44607ca46eSDavid Howells  */
45607ca46eSDavid Howells 
4642193e3eSOliver Hartkopp #ifndef _UAPI_CAN_H
4742193e3eSOliver Hartkopp #define _UAPI_CAN_H
48607ca46eSDavid Howells 
49607ca46eSDavid Howells #include <linux/types.h>
50607ca46eSDavid Howells #include <linux/socket.h>
511a3e3034SOliver Hartkopp #include <linux/stddef.h> /* for offsetof */
52607ca46eSDavid Howells 
53607ca46eSDavid Howells /* controller area network (CAN) kernel definitions */
54607ca46eSDavid Howells 
55607ca46eSDavid Howells /* special address description flags for the CAN_ID */
56607ca46eSDavid Howells #define CAN_EFF_FLAG 0x80000000U /* EFF/SFF is set in the MSB */
57607ca46eSDavid Howells #define CAN_RTR_FLAG 0x40000000U /* remote transmission request */
58607ca46eSDavid Howells #define CAN_ERR_FLAG 0x20000000U /* error message frame */
59607ca46eSDavid Howells 
60607ca46eSDavid Howells /* valid bits in CAN ID for frame formats */
61607ca46eSDavid Howells #define CAN_SFF_MASK 0x000007FFU /* standard frame format (SFF) */
62607ca46eSDavid Howells #define CAN_EFF_MASK 0x1FFFFFFFU /* extended frame format (EFF) */
63607ca46eSDavid Howells #define CAN_ERR_MASK 0x1FFFFFFFU /* omit EFF, RTR, ERR flags */
641a3e3034SOliver Hartkopp #define CANXL_PRIO_MASK CAN_SFF_MASK /* 11 bit priority mask */
65607ca46eSDavid Howells 
66607ca46eSDavid Howells /*
67607ca46eSDavid Howells  * Controller Area Network Identifier structure
68607ca46eSDavid Howells  *
69607ca46eSDavid Howells  * bit 0-28	: CAN identifier (11/29 bit)
70607ca46eSDavid Howells  * bit 29	: error message frame flag (0 = data frame, 1 = error message)
71607ca46eSDavid Howells  * bit 30	: remote transmission request flag (1 = rtr frame)
72607ca46eSDavid Howells  * bit 31	: frame format flag (0 = standard 11 bit, 1 = extended 29 bit)
73607ca46eSDavid Howells  */
74607ca46eSDavid Howells typedef __u32 canid_t;
75607ca46eSDavid Howells 
76607ca46eSDavid Howells #define CAN_SFF_ID_BITS		11
77607ca46eSDavid Howells #define CAN_EFF_ID_BITS		29
781a3e3034SOliver Hartkopp #define CANXL_PRIO_BITS		CAN_SFF_ID_BITS
79607ca46eSDavid Howells 
80607ca46eSDavid Howells /*
81607ca46eSDavid Howells  * Controller Area Network Error Message Frame Mask structure
82607ca46eSDavid Howells  *
833570a008SDan Murphy  * bit 0-28	: error class mask (see include/uapi/linux/can/error.h)
84607ca46eSDavid Howells  * bit 29-31	: set to zero
85607ca46eSDavid Howells  */
86607ca46eSDavid Howells typedef __u32 can_err_mask_t;
87607ca46eSDavid Howells 
88607ca46eSDavid Howells /* CAN payload length and DLC definitions according to ISO 11898-1 */
89607ca46eSDavid Howells #define CAN_MAX_DLC 8
90ea780056SOliver Hartkopp #define CAN_MAX_RAW_DLC 15
91607ca46eSDavid Howells #define CAN_MAX_DLEN 8
92607ca46eSDavid Howells 
93607ca46eSDavid Howells /* CAN FD payload length and DLC definitions according to ISO 11898-7 */
94607ca46eSDavid Howells #define CANFD_MAX_DLC 15
95607ca46eSDavid Howells #define CANFD_MAX_DLEN 64
96607ca46eSDavid Howells 
971a3e3034SOliver Hartkopp /*
981a3e3034SOliver Hartkopp  * CAN XL payload length and DLC definitions according to ISO 11898-1
991a3e3034SOliver Hartkopp  * CAN XL DLC ranges from 0 .. 2047 => data length from 1 .. 2048 byte
1001a3e3034SOliver Hartkopp  */
1011a3e3034SOliver Hartkopp #define CANXL_MIN_DLC 0
1021a3e3034SOliver Hartkopp #define CANXL_MAX_DLC 2047
1031a3e3034SOliver Hartkopp #define CANXL_MAX_DLC_MASK 0x07FF
1041a3e3034SOliver Hartkopp #define CANXL_MIN_DLEN 1
1051a3e3034SOliver Hartkopp #define CANXL_MAX_DLEN 2048
1061a3e3034SOliver Hartkopp 
107607ca46eSDavid Howells /**
108ea780056SOliver Hartkopp  * struct can_frame - Classical CAN frame structure (aka CAN 2.0B)
109607ca46eSDavid Howells  * @can_id:   CAN ID of the frame and CAN_*_FLAG flags, see canid_t definition
110ea780056SOliver Hartkopp  * @len:      CAN frame payload length in byte (0 .. 8)
111ea780056SOliver Hartkopp  * @can_dlc:  deprecated name for CAN frame payload length in byte (0 .. 8)
112a2f11835SShawn Landden  * @__pad:    padding
113a2f11835SShawn Landden  * @__res0:   reserved / padding
114ea780056SOliver Hartkopp  * @len8_dlc: optional DLC value (9 .. 15) at 8 byte payload length
115ea780056SOliver Hartkopp  *            len8_dlc contains values from 9 .. 15 when the payload length is
116ea780056SOliver Hartkopp  *            8 bytes but the DLC value (see ISO 11898-1) is greater then 8.
117ea780056SOliver Hartkopp  *            CAN_CTRLMODE_CC_LEN8_DLC flag has to be enabled in CAN driver.
118607ca46eSDavid Howells  * @data:     CAN frame payload (up to 8 byte)
119607ca46eSDavid Howells  */
120607ca46eSDavid Howells struct can_frame {
121607ca46eSDavid Howells 	canid_t can_id;  /* 32 bit CAN_ID + EFF/RTR/ERR flags */
122ea780056SOliver Hartkopp 	union {
123ea780056SOliver Hartkopp 		/* CAN frame payload length in byte (0 .. CAN_MAX_DLEN)
124ea780056SOliver Hartkopp 		 * was previously named can_dlc so we need to carry that
125ea780056SOliver Hartkopp 		 * name for legacy support
126ea780056SOliver Hartkopp 		 */
127ea780056SOliver Hartkopp 		__u8 len;
128ea780056SOliver Hartkopp 		__u8 can_dlc; /* deprecated */
129f5076c6bSMarc Kleine-Budde 	} __attribute__((packed)); /* disable padding added in some ABIs */
130a2f11835SShawn Landden 	__u8 __pad; /* padding */
131a2f11835SShawn Landden 	__u8 __res0; /* reserved / padding */
132ea780056SOliver Hartkopp 	__u8 len8_dlc; /* optional DLC for 8 byte payload length (9 .. 15) */
133607ca46eSDavid Howells 	__u8 data[CAN_MAX_DLEN] __attribute__((aligned(8)));
134607ca46eSDavid Howells };
135607ca46eSDavid Howells 
136607ca46eSDavid Howells /*
137607ca46eSDavid Howells  * defined bits for canfd_frame.flags
138607ca46eSDavid Howells  *
1397e97d274SMarc Kleine-Budde  * The use of struct canfd_frame implies the FD Frame (FDF) bit to
1407e97d274SMarc Kleine-Budde  * be set in the CAN frame bitstream on the wire. The FDF bit switch turns
141607ca46eSDavid Howells  * the CAN controllers bitstream processor into the CAN FD mode which creates
142607ca46eSDavid Howells  * two new options within the CAN FD frame specification:
143607ca46eSDavid Howells  *
144607ca46eSDavid Howells  * Bit Rate Switch - to indicate a second bitrate is/was used for the payload
145607ca46eSDavid Howells  * Error State Indicator - represents the error state of the transmitting node
146607ca46eSDavid Howells  *
147607ca46eSDavid Howells  * As the CANFD_ESI bit is internally generated by the transmitting CAN
148607ca46eSDavid Howells  * controller only the CANFD_BRS bit is relevant for real CAN controllers when
149607ca46eSDavid Howells  * building a CAN FD frame for transmission. Setting the CANFD_ESI bit can make
150607ca46eSDavid Howells  * sense for virtual CAN interfaces to test applications with echoed frames.
15102546884SOliver Hartkopp  *
15202546884SOliver Hartkopp  * The struct can_frame and struct canfd_frame intentionally share the same
15302546884SOliver Hartkopp  * layout to be able to write CAN frame content into a CAN FD frame structure.
15402546884SOliver Hartkopp  * When this is done the former differentiation via CAN_MTU / CANFD_MTU gets
15502546884SOliver Hartkopp  * lost. CANFD_FDF allows programmers to mark CAN FD frames in the case of
15602546884SOliver Hartkopp  * using struct canfd_frame for mixed CAN / CAN FD content (dual use).
15706183462SOliver Hartkopp  * Since the introduction of CAN XL the CANFD_FDF flag is set in all CAN FD
15806183462SOliver Hartkopp  * frame structures provided by the CAN subsystem of the Linux kernel.
159607ca46eSDavid Howells  */
160607ca46eSDavid Howells #define CANFD_BRS 0x01 /* bit rate switch (second bitrate for payload data) */
161607ca46eSDavid Howells #define CANFD_ESI 0x02 /* error state indicator of the transmitting node */
16202546884SOliver Hartkopp #define CANFD_FDF 0x04 /* mark CAN FD for dual use of struct canfd_frame */
163607ca46eSDavid Howells 
164607ca46eSDavid Howells /**
165607ca46eSDavid Howells  * struct canfd_frame - CAN flexible data rate frame structure
166607ca46eSDavid Howells  * @can_id: CAN ID of the frame and CAN_*_FLAG flags, see canid_t definition
167607ca46eSDavid Howells  * @len:    frame payload length in byte (0 .. CANFD_MAX_DLEN)
168607ca46eSDavid Howells  * @flags:  additional flags for CAN FD
169607ca46eSDavid Howells  * @__res0: reserved / padding
170607ca46eSDavid Howells  * @__res1: reserved / padding
171607ca46eSDavid Howells  * @data:   CAN FD frame payload (up to CANFD_MAX_DLEN byte)
172607ca46eSDavid Howells  */
173607ca46eSDavid Howells struct canfd_frame {
174607ca46eSDavid Howells 	canid_t can_id;  /* 32 bit CAN_ID + EFF/RTR/ERR flags */
175607ca46eSDavid Howells 	__u8    len;     /* frame payload length in byte */
176607ca46eSDavid Howells 	__u8    flags;   /* additional flags for CAN FD */
177607ca46eSDavid Howells 	__u8    __res0;  /* reserved / padding */
178607ca46eSDavid Howells 	__u8    __res1;  /* reserved / padding */
179607ca46eSDavid Howells 	__u8    data[CANFD_MAX_DLEN] __attribute__((aligned(8)));
180607ca46eSDavid Howells };
181607ca46eSDavid Howells 
1821a3e3034SOliver Hartkopp /*
1831a3e3034SOliver Hartkopp  * defined bits for canxl_frame.flags
1841a3e3034SOliver Hartkopp  *
1851a3e3034SOliver Hartkopp  * The canxl_frame.flags element contains two bits CANXL_XLF and CANXL_SEC
1861a3e3034SOliver Hartkopp  * and shares the relative position of the struct can[fd]_frame.len element.
1871a3e3034SOliver Hartkopp  * The CANXL_XLF bit ALWAYS needs to be set to indicate a valid CAN XL frame.
1881a3e3034SOliver Hartkopp  * As a side effect setting this bit intentionally breaks the length checks
1891a3e3034SOliver Hartkopp  * for Classical CAN and CAN FD frames.
1901a3e3034SOliver Hartkopp  *
1911a3e3034SOliver Hartkopp  * Undefined bits in canxl_frame.flags are reserved and shall be set to zero.
1921a3e3034SOliver Hartkopp  */
1931a3e3034SOliver Hartkopp #define CANXL_XLF 0x80 /* mandatory CAN XL frame flag (must always be set!) */
1941a3e3034SOliver Hartkopp #define CANXL_SEC 0x01 /* Simple Extended Content (security/segmentation) */
1951a3e3034SOliver Hartkopp 
196*c83c22ecSOliver Hartkopp /* the 8-bit VCID is optionally placed in the canxl_frame.prio element */
197*c83c22ecSOliver Hartkopp #define CANXL_VCID_OFFSET 16 /* bit offset of VCID in prio element */
198*c83c22ecSOliver Hartkopp #define CANXL_VCID_VAL_MASK 0xFFUL /* VCID is an 8-bit value */
199*c83c22ecSOliver Hartkopp #define CANXL_VCID_MASK (CANXL_VCID_VAL_MASK << CANXL_VCID_OFFSET)
200*c83c22ecSOliver Hartkopp 
2011a3e3034SOliver Hartkopp /**
2021a3e3034SOliver Hartkopp  * struct canxl_frame - CAN with e'X'tended frame 'L'ength frame structure
203*c83c22ecSOliver Hartkopp  * @prio:  11 bit arbitration priority with zero'ed CAN_*_FLAG flags / VCID
2041a3e3034SOliver Hartkopp  * @flags: additional flags for CAN XL
2051a3e3034SOliver Hartkopp  * @sdt:   SDU (service data unit) type
2061a3e3034SOliver Hartkopp  * @len:   frame payload length in byte (CANXL_MIN_DLEN .. CANXL_MAX_DLEN)
2071a3e3034SOliver Hartkopp  * @af:    acceptance field
2081a3e3034SOliver Hartkopp  * @data:  CAN XL frame payload (CANXL_MIN_DLEN .. CANXL_MAX_DLEN byte)
2091a3e3034SOliver Hartkopp  *
2101a3e3034SOliver Hartkopp  * @prio shares the same position as @can_id from struct can[fd]_frame.
2111a3e3034SOliver Hartkopp  */
2121a3e3034SOliver Hartkopp struct canxl_frame {
213*c83c22ecSOliver Hartkopp 	canid_t prio;  /* 11 bit priority for arbitration / 8 bit VCID */
2141a3e3034SOliver Hartkopp 	__u8    flags; /* additional flags for CAN XL */
2151a3e3034SOliver Hartkopp 	__u8    sdt;   /* SDU (service data unit) type */
2161a3e3034SOliver Hartkopp 	__u16   len;   /* frame payload length in byte */
2171a3e3034SOliver Hartkopp 	__u32   af;    /* acceptance field */
2181a3e3034SOliver Hartkopp 	__u8    data[CANXL_MAX_DLEN];
2191a3e3034SOliver Hartkopp };
2201a3e3034SOliver Hartkopp 
221607ca46eSDavid Howells #define CAN_MTU		(sizeof(struct can_frame))
222607ca46eSDavid Howells #define CANFD_MTU	(sizeof(struct canfd_frame))
2231a3e3034SOliver Hartkopp #define CANXL_MTU	(sizeof(struct canxl_frame))
2241a3e3034SOliver Hartkopp #define CANXL_HDR_SIZE	(offsetof(struct canxl_frame, data))
2251a3e3034SOliver Hartkopp #define CANXL_MIN_MTU	(CANXL_HDR_SIZE + 64)
2261a3e3034SOliver Hartkopp #define CANXL_MAX_MTU	CANXL_MTU
227607ca46eSDavid Howells 
228607ca46eSDavid Howells /* particular protocols of the protocol family PF_CAN */
229607ca46eSDavid Howells #define CAN_RAW		1 /* RAW sockets */
230607ca46eSDavid Howells #define CAN_BCM		2 /* Broadcast Manager */
231607ca46eSDavid Howells #define CAN_TP16	3 /* VAG Transport Protocol v1.6 */
232607ca46eSDavid Howells #define CAN_TP20	4 /* VAG Transport Protocol v2.0 */
233607ca46eSDavid Howells #define CAN_MCNET	5 /* Bosch MCNet */
234607ca46eSDavid Howells #define CAN_ISOTP	6 /* ISO 15765-2 Transport Protocol */
2352a0c9aaaSKurt Van Dijck #define CAN_J1939	7 /* SAE J1939 */
2362a0c9aaaSKurt Van Dijck #define CAN_NPROTO	8
237607ca46eSDavid Howells 
238607ca46eSDavid Howells #define SOL_CAN_BASE 100
239607ca46eSDavid Howells 
240607ca46eSDavid Howells /**
241607ca46eSDavid Howells  * struct sockaddr_can - the sockaddr structure for CAN sockets
242607ca46eSDavid Howells  * @can_family:  address family number AF_CAN.
243607ca46eSDavid Howells  * @can_ifindex: CAN network interface index.
244607ca46eSDavid Howells  * @can_addr:    protocol specific address information
245607ca46eSDavid Howells  */
246607ca46eSDavid Howells struct sockaddr_can {
247607ca46eSDavid Howells 	__kernel_sa_family_t can_family;
248607ca46eSDavid Howells 	int         can_ifindex;
249607ca46eSDavid Howells 	union {
250607ca46eSDavid Howells 		/* transport protocol class address information (e.g. ISOTP) */
251607ca46eSDavid Howells 		struct { canid_t rx_id, tx_id; } tp;
252607ca46eSDavid Howells 
253f5223e9eSKurt Van Dijck 		/* J1939 address information */
254f5223e9eSKurt Van Dijck 		struct {
255f5223e9eSKurt Van Dijck 			/* 8 byte name when using dynamic addressing */
256f5223e9eSKurt Van Dijck 			__u64 name;
257f5223e9eSKurt Van Dijck 
258f5223e9eSKurt Van Dijck 			/* pgn:
259f5223e9eSKurt Van Dijck 			 * 8 bit: PS in PDU2 case, else 0
260f5223e9eSKurt Van Dijck 			 * 8 bit: PF
261f5223e9eSKurt Van Dijck 			 * 1 bit: DP
262f5223e9eSKurt Van Dijck 			 * 1 bit: reserved
263f5223e9eSKurt Van Dijck 			 */
264f5223e9eSKurt Van Dijck 			__u32 pgn;
265f5223e9eSKurt Van Dijck 
266f5223e9eSKurt Van Dijck 			/* 1 byte address */
267f5223e9eSKurt Van Dijck 			__u8 addr;
268f5223e9eSKurt Van Dijck 		} j1939;
269f5223e9eSKurt Van Dijck 
270607ca46eSDavid Howells 		/* reserved for future CAN protocols address information */
271607ca46eSDavid Howells 	} can_addr;
272607ca46eSDavid Howells };
273607ca46eSDavid Howells 
274607ca46eSDavid Howells /**
275607ca46eSDavid Howells  * struct can_filter - CAN ID based filter in can_register().
276607ca46eSDavid Howells  * @can_id:   relevant bits of CAN ID which are not masked out.
277607ca46eSDavid Howells  * @can_mask: CAN mask (see description)
278607ca46eSDavid Howells  *
279607ca46eSDavid Howells  * Description:
280607ca46eSDavid Howells  * A filter matches, when
281607ca46eSDavid Howells  *
282607ca46eSDavid Howells  *          <received_can_id> & mask == can_id & mask
283607ca46eSDavid Howells  *
284607ca46eSDavid Howells  * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
285607ca46eSDavid Howells  * filter for error message frames (CAN_ERR_FLAG bit set in mask).
286607ca46eSDavid Howells  */
287607ca46eSDavid Howells struct can_filter {
288607ca46eSDavid Howells 	canid_t can_id;
289607ca46eSDavid Howells 	canid_t can_mask;
290607ca46eSDavid Howells };
291607ca46eSDavid Howells 
292607ca46eSDavid Howells #define CAN_INV_FILTER 0x20000000U /* to be set in can_filter.can_id */
293607ca46eSDavid Howells 
29442193e3eSOliver Hartkopp #endif /* !_UAPI_CAN_H */
295