brintos

brintos / linux-shallow public Read only

0
0
Text · 8.9 KiB · abc978b Raw
307 lines · c
1/* SPDX-License-Identifier: GPL-2.0 */2/* Copyright (C) 2020 Oliver Hartkopp <socketcan@hartkopp.net>3 * Copyright (C) 2020 Marc Kleine-Budde <kernel@pengutronix.de>4 * Copyright (C) 2020, 2023 Vincent Mailhol <mailhol.vincent@wanadoo.fr>5 */6 7#ifndef _CAN_LENGTH_H8#define _CAN_LENGTH_H9 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 header in bits17 *18 * Name of Field				Bits19 * ---------------------------------------------------------20 * Start Of Frame (SOF)				121 * Arbitration field:22 *	base ID					1123 *	Remote Transmission Request (RTR)	124 * Control field:25 *	IDentifier Extension bit (IDE)		126 *	FD Format indicator (FDF)		127 *	Data Length Code (DLC)			428 *29 * including all fields preceding the data field, ignoring bitstuffing30 */31#define CAN_FRAME_HEADER_SFF_BITS 1932 33/*34 * Size of a Classical CAN Extended Frame header in bits35 *36 * Name of Field				Bits37 * ---------------------------------------------------------38 * Start Of Frame (SOF)				139 * Arbitration field:40 *	base ID					1141 *	Substitute Remote Request (SRR)		142 *	IDentifier Extension bit (IDE)		143 *	ID extension				1844 *	Remote Transmission Request (RTR)	145 * Control field:46 *	FD Format indicator (FDF)		147 *	Reserved bit (r0)			148 *	Data length code (DLC)			449 *50 * including all fields preceding the data field, ignoring bitstuffing51 */52#define CAN_FRAME_HEADER_EFF_BITS 3953 54/*55 * Size of a CAN-FD Standard Frame in bits56 *57 * Name of Field				Bits58 * ---------------------------------------------------------59 * Start Of Frame (SOF)				160 * Arbitration field:61 *	base ID					1162 *	Remote Request Substitution (RRS)	163 * Control field:64 *	IDentifier Extension bit (IDE)		165 *	FD Format indicator (FDF)		166 *	Reserved bit (res)			167 *	Bit Rate Switch (BRS)			168 *	Error Status Indicator (ESI)		169 *	Data length code (DLC)			470 *71 * including all fields preceding the data field, ignoring bitstuffing72 */73#define CANFD_FRAME_HEADER_SFF_BITS 2274 75/*76 * Size of a CAN-FD Extended Frame in bits77 *78 * Name of Field				Bits79 * ---------------------------------------------------------80 * Start Of Frame (SOF)				181 * Arbitration field:82 *	base ID					1183 *	Substitute Remote Request (SRR)		184 *	IDentifier Extension bit (IDE)		185 *	ID extension				1886 *	Remote Request Substitution (RRS)	187 * Control field:88 *	FD Format indicator (FDF)		189 *	Reserved bit (res)			190 *	Bit Rate Switch (BRS)			191 *	Error Status Indicator (ESI)		192 *	Data length code (DLC)			493 *94 * including all fields preceding the data field, ignoring bitstuffing95 */96#define CANFD_FRAME_HEADER_EFF_BITS 4197 98/*99 * Size of a CAN CRC Field in bits100 *101 * Name of Field			Bits102 * ---------------------------------------------------------103 * CRC sequence (CRC15)			15104 * CRC Delimiter			1105 *106 * ignoring bitstuffing107 */108#define CAN_FRAME_CRC_FIELD_BITS 16109 110/*111 * Size of a CAN-FD CRC17 Field in bits (length: 0..16)112 *113 * Name of Field			Bits114 * ---------------------------------------------------------115 * Stuff Count				4116 * CRC Sequence (CRC17)			17117 * CRC Delimiter			1118 * Fixed stuff bits			6119 */120#define CANFD_FRAME_CRC17_FIELD_BITS 28121 122/*123 * Size of a CAN-FD CRC21 Field in bits (length: 20..64)124 *125 * Name of Field			Bits126 * ---------------------------------------------------------127 * Stuff Count				4128 * CRC sequence (CRC21)			21129 * CRC Delimiter			1130 * Fixed stuff bits			7131 */132#define CANFD_FRAME_CRC21_FIELD_BITS 33133 134/*135 * Size of a CAN(-FD) Frame footer in bits136 *137 * Name of Field			Bits138 * ---------------------------------------------------------139 * ACK slot				1140 * ACK delimiter			1141 * End Of Frame (EOF)			7142 *143 * including all fields following the CRC field144 */145#define CAN_FRAME_FOOTER_BITS 9146 147/*148 * First part of the Inter Frame Space149 * (a.k.a. IMF - intermission field)150 */151#define CAN_INTERMISSION_BITS 3152 153/**154 * can_bitstuffing_len() - Calculate the maximum length with bitstuffing155 * @destuffed_len: length of a destuffed bit stream156 *157 * The worst bit stuffing case is a sequence in which dominant and158 * recessive bits alternate every four bits:159 *160 *   Destuffed: 1 1111  0000  1111  0000  1111161 *   Stuffed:   1 1111o 0000i 1111o 0000i 1111o162 *163 * Nomenclature164 *165 *  - "0": dominant bit166 *  - "o": dominant stuff bit167 *  - "1": recessive bit168 *  - "i": recessive stuff bit169 *170 * Aside from the first bit, one stuff bit is added every four bits.171 *172 * Return: length of the stuffed bit stream in the worst case scenario.173 */174#define can_bitstuffing_len(destuffed_len)			\175	(destuffed_len + (destuffed_len - 1) / 4)176 177#define __can_bitstuffing_len(bitstuffing, destuffed_len)	\178	(bitstuffing ? can_bitstuffing_len(destuffed_len) :	\179		       destuffed_len)180 181#define __can_cc_frame_bits(is_eff, bitstuffing,		\182			    intermission, data_len)		\183(								\184	__can_bitstuffing_len(bitstuffing,			\185		(is_eff ? CAN_FRAME_HEADER_EFF_BITS :		\186			  CAN_FRAME_HEADER_SFF_BITS) +		\187		(data_len) * BITS_PER_BYTE +			\188		CAN_FRAME_CRC_FIELD_BITS) +			\189	CAN_FRAME_FOOTER_BITS +					\190	(intermission ? CAN_INTERMISSION_BITS : 0)		\191)192 193#define __can_fd_frame_bits(is_eff, bitstuffing,		\194			    intermission, data_len)		\195(								\196	__can_bitstuffing_len(bitstuffing,			\197		(is_eff ? CANFD_FRAME_HEADER_EFF_BITS :		\198			  CANFD_FRAME_HEADER_SFF_BITS) +	\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 : 0)		\205)206 207/**208 * can_frame_bits() - Calculate the number of bits on the wire in a209 *	CAN frame210 * @is_fd: true: CAN-FD frame; false: Classical CAN frame.211 * @is_eff: true: Extended frame; false: Standard frame.212 * @bitstuffing: true: calculate the bitstuffing worst case; false:213 *	calculate the bitstuffing best case (no dynamic214 *	bitstuffing). CAN-FD's fixed stuff bits are always included.215 * @intermission: if and only if true, include the inter frame space216 *	assuming no bus idle (i.e. only the intermission). Strictly217 *	speaking, the inter frame space is not part of the218 *	frame. However, it is needed when calculating the delay219 *	between the Start Of Frame of two consecutive frames.220 * @data_len: length of the data field in bytes. Correspond to221 *	can(fd)_frame->len. Should be zero for remote frames. No222 *	sanitization is done on @data_len and it shall have no side223 *	effects.224 *225 * Return: the numbers of bits on the wire of a CAN frame.226 */227#define can_frame_bits(is_fd, is_eff, bitstuffing,		\228		       intermission, data_len)			\229(								\230	is_fd ? __can_fd_frame_bits(is_eff, bitstuffing,	\231				    intermission, data_len) :	\232		__can_cc_frame_bits(is_eff, bitstuffing,	\233				    intermission, data_len)	\234)235 236/*237 * Number of bytes in a CAN frame238 * (rounded up, including intermission)239 */240#define can_frame_bytes(is_fd, is_eff, bitstuffing, data_len)	\241	DIV_ROUND_UP(can_frame_bits(is_fd, is_eff, bitstuffing,	\242				    true, data_len),		\243		     BITS_PER_BYTE)244 245/*246 * Maximum size of a Classical CAN frame247 * (rounded up, ignoring bitstuffing but including intermission)248 */249#define CAN_FRAME_LEN_MAX can_frame_bytes(false, true, false, CAN_MAX_DLEN)250 251/*252 * Maximum size of a CAN-FD frame253 * (rounded up, ignoring dynamic bitstuffing but including intermission)254 */255#define CANFD_FRAME_LEN_MAX can_frame_bytes(true, true, false, CANFD_MAX_DLEN)256 257/*258 * can_cc_dlc2len(value) - convert a given data length code (dlc) of a259 * Classical CAN frame into a valid data length of max. 8 bytes.260 *261 * To be used in the CAN netdriver receive path to ensure conformance with262 * ISO 11898-1 Chapter 8.4.2.3 (DLC field)263 */264#define can_cc_dlc2len(dlc)	(min_t(u8, (dlc), CAN_MAX_DLEN))265 266/* helper to get the data length code (DLC) for Classical CAN raw DLC access */267static inline u8 can_get_cc_dlc(const struct can_frame *cf, const u32 ctrlmode)268{269	/* return len8_dlc as dlc value only if all conditions apply */270	if ((ctrlmode & CAN_CTRLMODE_CC_LEN8_DLC) &&271	    (cf->len == CAN_MAX_DLEN) &&272	    (cf->len8_dlc > CAN_MAX_DLEN && cf->len8_dlc <= CAN_MAX_RAW_DLC))273		return cf->len8_dlc;274 275	/* return the payload length as dlc value */276	return cf->len;277}278 279/* helper to set len and len8_dlc value for Classical CAN raw DLC access */280static inline void can_frame_set_cc_len(struct can_frame *cf, const u8 dlc,281					const u32 ctrlmode)282{283	/* the caller already ensured that dlc is a value from 0 .. 15 */284	if (ctrlmode & CAN_CTRLMODE_CC_LEN8_DLC && dlc > CAN_MAX_DLEN)285		cf->len8_dlc = dlc;286 287	/* limit the payload length 'len' to CAN_MAX_DLEN */288	cf->len = can_cc_dlc2len(dlc);289}290 291/* get data length from raw data length code (DLC) */292u8 can_fd_dlc2len(u8 dlc);293 294/* map the sanitized data length to an appropriate data length code */295u8 can_fd_len2dlc(u8 len);296 297/* calculate the CAN Frame length in bytes of a given skb */298unsigned int can_skb_get_frame_len(const struct sk_buff *skb);299 300/* map the data length to an appropriate data link layer length */301static inline u8 canfd_sanitize_len(u8 len)302{303	return can_fd_dlc2len(can_fd_len2dlc(len));304}305 306#endif /* !_CAN_LENGTH_H */307