1 /* SPDX-License-Identifier: GPL-2.0 */ 1 2 /* Copyright (C) 2020 Oliver Hartkopp <socketc 3 * Copyright (C) 2020 Marc Kleine-Budde <kerne 4 * Copyright (C) 2020, 2023 Vincent Mailhol <m 5 */ 6 7 #ifndef _CAN_LENGTH_H 8 #define _CAN_LENGTH_H 9 10 #include <linux/bits.h> 11 #include <linux/can.h> 12 #include <linux/can/netlink.h> 13 #include <linux/math.h> 14 15 /* 16 * Size of a Classical CAN Standard Frame head 17 * 18 * Name of Field 19 * ------------------------------------------- 20 * Start Of Frame (SOF) 21 * Arbitration field: 22 * base ID 23 * Remote Transmission Request (RTR) 24 * Control field: 25 * IDentifier Extension bit (IDE) 26 * FD Format indicator (FDF) 27 * Data Length Code (DLC) 28 * 29 * including all fields preceding the data fie 30 */ 31 #define CAN_FRAME_HEADER_SFF_BITS 19 32 33 /* 34 * Size of a Classical CAN Extended Frame head 35 * 36 * Name of Field 37 * ------------------------------------------- 38 * Start Of Frame (SOF) 39 * Arbitration field: 40 * base ID 41 * Substitute Remote Request (SRR) 42 * IDentifier Extension bit (IDE) 43 * ID extension 44 * Remote Transmission Request (RTR) 45 * Control field: 46 * FD Format indicator (FDF) 47 * Reserved bit (r0) 48 * Data length code (DLC) 49 * 50 * including all fields preceding the data fie 51 */ 52 #define CAN_FRAME_HEADER_EFF_BITS 39 53 54 /* 55 * Size of a CAN-FD Standard Frame in bits 56 * 57 * Name of Field 58 * ------------------------------------------- 59 * Start Of Frame (SOF) 60 * Arbitration field: 61 * base ID 62 * Remote Request Substitution (RRS) 63 * Control field: 64 * IDentifier Extension bit (IDE) 65 * FD Format indicator (FDF) 66 * Reserved bit (res) 67 * Bit Rate Switch (BRS) 68 * Error Status Indicator (ESI) 69 * Data length code (DLC) 70 * 71 * including all fields preceding the data fie 72 */ 73 #define CANFD_FRAME_HEADER_SFF_BITS 22 74 75 /* 76 * Size of a CAN-FD Extended Frame in bits 77 * 78 * Name of Field 79 * ------------------------------------------- 80 * Start Of Frame (SOF) 81 * Arbitration field: 82 * base ID 83 * Substitute Remote Request (SRR) 84 * IDentifier Extension bit (IDE) 85 * ID extension 86 * Remote Request Substitution (RRS) 87 * Control field: 88 * FD Format indicator (FDF) 89 * Reserved bit (res) 90 * Bit Rate Switch (BRS) 91 * Error Status Indicator (ESI) 92 * Data length code (DLC) 93 * 94 * including all fields preceding the data fie 95 */ 96 #define CANFD_FRAME_HEADER_EFF_BITS 41 97 98 /* 99 * Size of a CAN CRC Field in bits 100 * 101 * Name of Field Bits 102 * ------------------------------------------- 103 * CRC sequence (CRC15) 15 104 * CRC Delimiter 1 105 * 106 * ignoring bitstuffing 107 */ 108 #define CAN_FRAME_CRC_FIELD_BITS 16 109 110 /* 111 * Size of a CAN-FD CRC17 Field in bits (lengt 112 * 113 * Name of Field Bits 114 * ------------------------------------------- 115 * Stuff Count 4 116 * CRC Sequence (CRC17) 17 117 * CRC Delimiter 1 118 * Fixed stuff bits 6 119 */ 120 #define CANFD_FRAME_CRC17_FIELD_BITS 28 121 122 /* 123 * Size of a CAN-FD CRC21 Field in bits (lengt 124 * 125 * Name of Field Bits 126 * ------------------------------------------- 127 * Stuff Count 4 128 * CRC sequence (CRC21) 21 129 * CRC Delimiter 1 130 * Fixed stuff bits 7 131 */ 132 #define CANFD_FRAME_CRC21_FIELD_BITS 33 133 134 /* 135 * Size of a CAN(-FD) Frame footer in bits 136 * 137 * Name of Field Bits 138 * ------------------------------------------- 139 * ACK slot 1 140 * ACK delimiter 1 141 * End Of Frame (EOF) 7 142 * 143 * including all fields following the CRC fiel 144 */ 145 #define CAN_FRAME_FOOTER_BITS 9 146 147 /* 148 * First part of the Inter Frame Space 149 * (a.k.a. IMF - intermission field) 150 */ 151 #define CAN_INTERMISSION_BITS 3 152 153 /** 154 * can_bitstuffing_len() - Calculate the maxim 155 * @destuffed_len: length of a destuffed bit s 156 * 157 * The worst bit stuffing case is a sequence i 158 * recessive bits alternate every four bits: 159 * 160 * Destuffed: 1 1111 0000 1111 0000 1111 161 * Stuffed: 1 1111o 0000i 1111o 0000i 1111 162 * 163 * Nomenclature 164 * 165 * - "": dominant bit 166 * - "o": dominant stuff bit 167 * - "1": recessive bit 168 * - "i": recessive stuff bit 169 * 170 * Aside from the first bit, one stuff bit is 171 * 172 * Return: length of the stuffed bit stream in 173 */ 174 #define can_bitstuffing_len(destuffed_len) 175 (destuffed_len + (destuffed_len - 1) / 176 177 #define __can_bitstuffing_len(bitstuffing, des 178 (bitstuffing ? can_bitstuffing_len(des 179 destuffed_len) 180 181 #define __can_cc_frame_bits(is_eff, bitstuffin 182 intermission, data 183 ( 184 __can_bitstuffing_len(bitstuffing, 185 (is_eff ? CAN_FRAME_HEADER_EFF 186 CAN_FRAME_HEADER_SFF 187 (data_len) * BITS_PER_BYTE + 188 CAN_FRAME_CRC_FIELD_BITS) + 189 CAN_FRAME_FOOTER_BITS + 190 (intermission ? CAN_INTERMISSION_BITS 191 ) 192 193 #define __can_fd_frame_bits(is_eff, bitstuffin 194 intermission, data 195 ( 196 __can_bitstuffing_len(bitstuffing, 197 (is_eff ? CANFD_FRAME_HEADER_E 198 CANFD_FRAME_HEADER_S 199 (data_len) * BITS_PER_BYTE) + 200 ((data_len) <= 16 ? 201 CANFD_FRAME_CRC17_FIELD_BITS : 202 CANFD_FRAME_CRC21_FIELD_BITS) 203 CAN_FRAME_FOOTER_BITS + 204 (intermission ? CAN_INTERMISSION_BITS 205 ) 206 207 /** 208 * can_frame_bits() - Calculate the number of 209 * CAN frame 210 * @is_fd: true: CAN-FD frame; false: Classica 211 * @is_eff: true: Extended frame; false: Stand 212 * @bitstuffing: true: calculate the bitstuffi 213 * calculate the bitstuffing best case (n 214 * bitstuffing). CAN-FD's fixed stuff bit 215 * @intermission: if and only if true, include 216 * assuming no bus idle (i.e. only the in 217 * speaking, the inter frame space is not 218 * frame. However, it is needed when calc 219 * between the Start Of Frame of two cons 220 * @data_len: length of the data field in byte 221 * can(fd)_frame->len. Should be zero for 222 * sanitization is done on @data_len and 223 * effects. 224 * 225 * Return: the numbers of bits on the wire of 226 */ 227 #define can_frame_bits(is_fd, is_eff, bitstuff 228 intermission, data_len) 229 ( 230 is_fd ? __can_fd_frame_bits(is_eff, bi 231 intermissi 232 __can_cc_frame_bits(is_eff, bi 233 intermissi 234 ) 235 236 /* 237 * Number of bytes in a CAN frame 238 * (rounded up, including intermission) 239 */ 240 #define can_frame_bytes(is_fd, is_eff, bitstuf 241 DIV_ROUND_UP(can_frame_bits(is_fd, is_ 242 true, data 243 BITS_PER_BYTE) 244 245 /* 246 * Maximum size of a Classical CAN frame 247 * (rounded up, ignoring bitstuffing but inclu 248 */ 249 #define CAN_FRAME_LEN_MAX can_frame_bytes(fals 250 251 /* 252 * Maximum size of a CAN-FD frame 253 * (rounded up, ignoring dynamic bitstuffing b 254 */ 255 #define CANFD_FRAME_LEN_MAX can_frame_bytes(tr 256 257 /* 258 * can_cc_dlc2len(value) - convert a given dat 259 * Classical CAN frame into a valid data lengt 260 * 261 * To be used in the CAN netdriver receive pat 262 * ISO 11898-1 Chapter 8.4.2.3 (DLC field) 263 */ 264 #define can_cc_dlc2len(dlc) (min_t(u8, (dl 265 266 /* helper to get the data length code (DLC) fo 267 static inline u8 can_get_cc_dlc(const struct c 268 { 269 /* return len8_dlc as dlc value only i 270 if ((ctrlmode & CAN_CTRLMODE_CC_LEN8_D 271 (cf->len == CAN_MAX_DLEN) && 272 (cf->len8_dlc > CAN_MAX_DLEN && cf 273 return cf->len8_dlc; 274 275 /* return the payload length as dlc va 276 return cf->len; 277 } 278 279 /* helper to set len and len8_dlc value for Cl 280 static inline void can_frame_set_cc_len(struct 281 const 282 { 283 /* the caller already ensured that dlc 284 if (ctrlmode & CAN_CTRLMODE_CC_LEN8_DL 285 cf->len8_dlc = dlc; 286 287 /* limit the payload length 'len' to C 288 cf->len = can_cc_dlc2len(dlc); 289 } 290 291 /* get data length from raw data length code ( 292 u8 can_fd_dlc2len(u8 dlc); 293 294 /* map the sanitized data length to an appropr 295 u8 can_fd_len2dlc(u8 len); 296 297 /* calculate the CAN Frame length in bytes of 298 unsigned int can_skb_get_frame_len(const struc 299 300 /* map the data length to an appropriate data 301 static inline u8 canfd_sanitize_len(u8 len) 302 { 303 return can_fd_dlc2len(can_fd_len2dlc(l 304 } 305 306 #endif /* !_CAN_LENGTH_H */ 307
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.