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