1*4882a593Smuzhiyun /* SPDX-License-Identifier: ((GPL-2.0-only WITH Linux-syscall-note) OR BSD-3-Clause) */ 2*4882a593Smuzhiyun /* 3*4882a593Smuzhiyun * linux/can.h 4*4882a593Smuzhiyun * 5*4882a593Smuzhiyun * Definitions for CAN network layer (socket addr / CAN frame / CAN filter) 6*4882a593Smuzhiyun * 7*4882a593Smuzhiyun * Authors: Oliver Hartkopp <oliver.hartkopp@volkswagen.de> 8*4882a593Smuzhiyun * Urs Thuermann <urs.thuermann@volkswagen.de> 9*4882a593Smuzhiyun * Copyright (c) 2002-2007 Volkswagen Group Electronic Research 10*4882a593Smuzhiyun * All rights reserved. 11*4882a593Smuzhiyun * 12*4882a593Smuzhiyun * Redistribution and use in source and binary forms, with or without 13*4882a593Smuzhiyun * modification, are permitted provided that the following conditions 14*4882a593Smuzhiyun * are met: 15*4882a593Smuzhiyun * 1. Redistributions of source code must retain the above copyright 16*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer. 17*4882a593Smuzhiyun * 2. Redistributions in binary form must reproduce the above copyright 18*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer in the 19*4882a593Smuzhiyun * documentation and/or other materials provided with the distribution. 20*4882a593Smuzhiyun * 3. Neither the name of Volkswagen nor the names of its contributors 21*4882a593Smuzhiyun * may be used to endorse or promote products derived from this software 22*4882a593Smuzhiyun * without specific prior written permission. 23*4882a593Smuzhiyun * 24*4882a593Smuzhiyun * Alternatively, provided that this notice is retained in full, this 25*4882a593Smuzhiyun * software may be distributed under the terms of the GNU General 26*4882a593Smuzhiyun * Public License ("GPL") version 2, in which case the provisions of the 27*4882a593Smuzhiyun * GPL apply INSTEAD OF those given above. 28*4882a593Smuzhiyun * 29*4882a593Smuzhiyun * The provided data structures and external interfaces from this code 30*4882a593Smuzhiyun * are not restricted to be used by modules with a GPL compatible license. 31*4882a593Smuzhiyun * 32*4882a593Smuzhiyun * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 33*4882a593Smuzhiyun * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 34*4882a593Smuzhiyun * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 35*4882a593Smuzhiyun * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 36*4882a593Smuzhiyun * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 37*4882a593Smuzhiyun * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 38*4882a593Smuzhiyun * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 39*4882a593Smuzhiyun * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 40*4882a593Smuzhiyun * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 41*4882a593Smuzhiyun * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 42*4882a593Smuzhiyun * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 43*4882a593Smuzhiyun * DAMAGE. 44*4882a593Smuzhiyun */ 45*4882a593Smuzhiyun 46*4882a593Smuzhiyun #ifndef _UAPI_CAN_H 47*4882a593Smuzhiyun #define _UAPI_CAN_H 48*4882a593Smuzhiyun 49*4882a593Smuzhiyun #include <linux/types.h> 50*4882a593Smuzhiyun #include <linux/socket.h> 51*4882a593Smuzhiyun 52*4882a593Smuzhiyun /* controller area network (CAN) kernel definitions */ 53*4882a593Smuzhiyun 54*4882a593Smuzhiyun /* special address description flags for the CAN_ID */ 55*4882a593Smuzhiyun #define CAN_EFF_FLAG 0x80000000U /* EFF/SFF is set in the MSB */ 56*4882a593Smuzhiyun #define CAN_RTR_FLAG 0x40000000U /* remote transmission request */ 57*4882a593Smuzhiyun #define CAN_ERR_FLAG 0x20000000U /* error message frame */ 58*4882a593Smuzhiyun 59*4882a593Smuzhiyun /* valid bits in CAN ID for frame formats */ 60*4882a593Smuzhiyun #define CAN_SFF_MASK 0x000007FFU /* standard frame format (SFF) */ 61*4882a593Smuzhiyun #define CAN_EFF_MASK 0x1FFFFFFFU /* extended frame format (EFF) */ 62*4882a593Smuzhiyun #define CAN_ERR_MASK 0x1FFFFFFFU /* omit EFF, RTR, ERR flags */ 63*4882a593Smuzhiyun 64*4882a593Smuzhiyun /* 65*4882a593Smuzhiyun * Controller Area Network Identifier structure 66*4882a593Smuzhiyun * 67*4882a593Smuzhiyun * bit 0-28 : CAN identifier (11/29 bit) 68*4882a593Smuzhiyun * bit 29 : error message frame flag (0 = data frame, 1 = error message) 69*4882a593Smuzhiyun * bit 30 : remote transmission request flag (1 = rtr frame) 70*4882a593Smuzhiyun * bit 31 : frame format flag (0 = standard 11 bit, 1 = extended 29 bit) 71*4882a593Smuzhiyun */ 72*4882a593Smuzhiyun typedef __u32 canid_t; 73*4882a593Smuzhiyun 74*4882a593Smuzhiyun #define CAN_SFF_ID_BITS 11 75*4882a593Smuzhiyun #define CAN_EFF_ID_BITS 29 76*4882a593Smuzhiyun 77*4882a593Smuzhiyun /* 78*4882a593Smuzhiyun * Controller Area Network Error Message Frame Mask structure 79*4882a593Smuzhiyun * 80*4882a593Smuzhiyun * bit 0-28 : error class mask (see include/uapi/linux/can/error.h) 81*4882a593Smuzhiyun * bit 29-31 : set to zero 82*4882a593Smuzhiyun */ 83*4882a593Smuzhiyun typedef __u32 can_err_mask_t; 84*4882a593Smuzhiyun 85*4882a593Smuzhiyun /* CAN payload length and DLC definitions according to ISO 11898-1 */ 86*4882a593Smuzhiyun #define CAN_MAX_DLC 8 87*4882a593Smuzhiyun #define CAN_MAX_DLEN 8 88*4882a593Smuzhiyun 89*4882a593Smuzhiyun /* CAN FD payload length and DLC definitions according to ISO 11898-7 */ 90*4882a593Smuzhiyun #define CANFD_MAX_DLC 15 91*4882a593Smuzhiyun #define CANFD_MAX_DLEN 64 92*4882a593Smuzhiyun 93*4882a593Smuzhiyun /** 94*4882a593Smuzhiyun * struct can_frame - basic CAN frame structure 95*4882a593Smuzhiyun * @can_id: CAN ID of the frame and CAN_*_FLAG flags, see canid_t definition 96*4882a593Smuzhiyun * @can_dlc: frame payload length in byte (0 .. 8) aka data length code 97*4882a593Smuzhiyun * N.B. the DLC field from ISO 11898-1 Chapter 8.4.2.3 has a 1:1 98*4882a593Smuzhiyun * mapping of the 'data length code' to the real payload length 99*4882a593Smuzhiyun * @__pad: padding 100*4882a593Smuzhiyun * @__res0: reserved / padding 101*4882a593Smuzhiyun * @__res1: reserved / padding 102*4882a593Smuzhiyun * @data: CAN frame payload (up to 8 byte) 103*4882a593Smuzhiyun */ 104*4882a593Smuzhiyun struct can_frame { 105*4882a593Smuzhiyun canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */ 106*4882a593Smuzhiyun __u8 can_dlc; /* frame payload length in byte (0 .. CAN_MAX_DLEN) */ 107*4882a593Smuzhiyun __u8 __pad; /* padding */ 108*4882a593Smuzhiyun __u8 __res0; /* reserved / padding */ 109*4882a593Smuzhiyun __u8 __res1; /* reserved / padding */ 110*4882a593Smuzhiyun __u8 data[CAN_MAX_DLEN] __attribute__((aligned(8))); 111*4882a593Smuzhiyun }; 112*4882a593Smuzhiyun 113*4882a593Smuzhiyun /* 114*4882a593Smuzhiyun * defined bits for canfd_frame.flags 115*4882a593Smuzhiyun * 116*4882a593Smuzhiyun * The use of struct canfd_frame implies the Extended Data Length (EDL) bit to 117*4882a593Smuzhiyun * be set in the CAN frame bitstream on the wire. The EDL bit switch turns 118*4882a593Smuzhiyun * the CAN controllers bitstream processor into the CAN FD mode which creates 119*4882a593Smuzhiyun * two new options within the CAN FD frame specification: 120*4882a593Smuzhiyun * 121*4882a593Smuzhiyun * Bit Rate Switch - to indicate a second bitrate is/was used for the payload 122*4882a593Smuzhiyun * Error State Indicator - represents the error state of the transmitting node 123*4882a593Smuzhiyun * 124*4882a593Smuzhiyun * As the CANFD_ESI bit is internally generated by the transmitting CAN 125*4882a593Smuzhiyun * controller only the CANFD_BRS bit is relevant for real CAN controllers when 126*4882a593Smuzhiyun * building a CAN FD frame for transmission. Setting the CANFD_ESI bit can make 127*4882a593Smuzhiyun * sense for virtual CAN interfaces to test applications with echoed frames. 128*4882a593Smuzhiyun */ 129*4882a593Smuzhiyun #define CANFD_BRS 0x01 /* bit rate switch (second bitrate for payload data) */ 130*4882a593Smuzhiyun #define CANFD_ESI 0x02 /* error state indicator of the transmitting node */ 131*4882a593Smuzhiyun 132*4882a593Smuzhiyun /** 133*4882a593Smuzhiyun * struct canfd_frame - CAN flexible data rate frame structure 134*4882a593Smuzhiyun * @can_id: CAN ID of the frame and CAN_*_FLAG flags, see canid_t definition 135*4882a593Smuzhiyun * @len: frame payload length in byte (0 .. CANFD_MAX_DLEN) 136*4882a593Smuzhiyun * @flags: additional flags for CAN FD 137*4882a593Smuzhiyun * @__res0: reserved / padding 138*4882a593Smuzhiyun * @__res1: reserved / padding 139*4882a593Smuzhiyun * @data: CAN FD frame payload (up to CANFD_MAX_DLEN byte) 140*4882a593Smuzhiyun */ 141*4882a593Smuzhiyun struct canfd_frame { 142*4882a593Smuzhiyun canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */ 143*4882a593Smuzhiyun __u8 len; /* frame payload length in byte */ 144*4882a593Smuzhiyun __u8 flags; /* additional flags for CAN FD */ 145*4882a593Smuzhiyun __u8 __res0; /* reserved / padding */ 146*4882a593Smuzhiyun __u8 __res1; /* reserved / padding */ 147*4882a593Smuzhiyun __u8 data[CANFD_MAX_DLEN] __attribute__((aligned(8))); 148*4882a593Smuzhiyun }; 149*4882a593Smuzhiyun 150*4882a593Smuzhiyun #define CAN_MTU (sizeof(struct can_frame)) 151*4882a593Smuzhiyun #define CANFD_MTU (sizeof(struct canfd_frame)) 152*4882a593Smuzhiyun 153*4882a593Smuzhiyun /* particular protocols of the protocol family PF_CAN */ 154*4882a593Smuzhiyun #define CAN_RAW 1 /* RAW sockets */ 155*4882a593Smuzhiyun #define CAN_BCM 2 /* Broadcast Manager */ 156*4882a593Smuzhiyun #define CAN_TP16 3 /* VAG Transport Protocol v1.6 */ 157*4882a593Smuzhiyun #define CAN_TP20 4 /* VAG Transport Protocol v2.0 */ 158*4882a593Smuzhiyun #define CAN_MCNET 5 /* Bosch MCNet */ 159*4882a593Smuzhiyun #define CAN_ISOTP 6 /* ISO 15765-2 Transport Protocol */ 160*4882a593Smuzhiyun #define CAN_J1939 7 /* SAE J1939 */ 161*4882a593Smuzhiyun #define CAN_NPROTO 8 162*4882a593Smuzhiyun 163*4882a593Smuzhiyun #define SOL_CAN_BASE 100 164*4882a593Smuzhiyun 165*4882a593Smuzhiyun /** 166*4882a593Smuzhiyun * struct sockaddr_can - the sockaddr structure for CAN sockets 167*4882a593Smuzhiyun * @can_family: address family number AF_CAN. 168*4882a593Smuzhiyun * @can_ifindex: CAN network interface index. 169*4882a593Smuzhiyun * @can_addr: protocol specific address information 170*4882a593Smuzhiyun */ 171*4882a593Smuzhiyun struct sockaddr_can { 172*4882a593Smuzhiyun __kernel_sa_family_t can_family; 173*4882a593Smuzhiyun int can_ifindex; 174*4882a593Smuzhiyun union { 175*4882a593Smuzhiyun /* transport protocol class address information (e.g. ISOTP) */ 176*4882a593Smuzhiyun struct { canid_t rx_id, tx_id; } tp; 177*4882a593Smuzhiyun 178*4882a593Smuzhiyun /* J1939 address information */ 179*4882a593Smuzhiyun struct { 180*4882a593Smuzhiyun /* 8 byte name when using dynamic addressing */ 181*4882a593Smuzhiyun __u64 name; 182*4882a593Smuzhiyun 183*4882a593Smuzhiyun /* pgn: 184*4882a593Smuzhiyun * 8 bit: PS in PDU2 case, else 0 185*4882a593Smuzhiyun * 8 bit: PF 186*4882a593Smuzhiyun * 1 bit: DP 187*4882a593Smuzhiyun * 1 bit: reserved 188*4882a593Smuzhiyun */ 189*4882a593Smuzhiyun __u32 pgn; 190*4882a593Smuzhiyun 191*4882a593Smuzhiyun /* 1 byte address */ 192*4882a593Smuzhiyun __u8 addr; 193*4882a593Smuzhiyun } j1939; 194*4882a593Smuzhiyun 195*4882a593Smuzhiyun /* reserved for future CAN protocols address information */ 196*4882a593Smuzhiyun } can_addr; 197*4882a593Smuzhiyun }; 198*4882a593Smuzhiyun 199*4882a593Smuzhiyun /** 200*4882a593Smuzhiyun * struct can_filter - CAN ID based filter in can_register(). 201*4882a593Smuzhiyun * @can_id: relevant bits of CAN ID which are not masked out. 202*4882a593Smuzhiyun * @can_mask: CAN mask (see description) 203*4882a593Smuzhiyun * 204*4882a593Smuzhiyun * Description: 205*4882a593Smuzhiyun * A filter matches, when 206*4882a593Smuzhiyun * 207*4882a593Smuzhiyun * <received_can_id> & mask == can_id & mask 208*4882a593Smuzhiyun * 209*4882a593Smuzhiyun * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can 210*4882a593Smuzhiyun * filter for error message frames (CAN_ERR_FLAG bit set in mask). 211*4882a593Smuzhiyun */ 212*4882a593Smuzhiyun struct can_filter { 213*4882a593Smuzhiyun canid_t can_id; 214*4882a593Smuzhiyun canid_t can_mask; 215*4882a593Smuzhiyun }; 216*4882a593Smuzhiyun 217*4882a593Smuzhiyun #define CAN_INV_FILTER 0x20000000U /* to be set in can_filter.can_id */ 218*4882a593Smuzhiyun #define CAN_RAW_FILTER_MAX 512 /* maximum number of can_filter set via setsockopt() */ 219*4882a593Smuzhiyun 220*4882a593Smuzhiyun #endif /* !_UAPI_CAN_H */ 221