linux/include/linux/can/length.h
<<
>>
Prefs
   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 */
   5
   6#ifndef _CAN_LENGTH_H
   7#define _CAN_LENGTH_H
   8
   9/*
  10 * Size of a Classical CAN Standard Frame
  11 *
  12 * Name of Field                        Bits
  13 * ---------------------------------------------------------
  14 * Start-of-frame                       1
  15 * Identifier                           11
  16 * Remote transmission request (RTR)    1
  17 * Identifier extension bit (IDE)       1
  18 * Reserved bit (r0)                    1
  19 * Data length code (DLC)               4
  20 * Data field                           0...64
  21 * CRC                                  15
  22 * CRC delimiter                        1
  23 * ACK slot                             1
  24 * ACK delimiter                        1
  25 * End-of-frame (EOF)                   7
  26 * Inter frame spacing                  3
  27 *
  28 * rounded up and ignoring bitstuffing
  29 */
  30#define CAN_FRAME_OVERHEAD_SFF DIV_ROUND_UP(47, 8)
  31
  32/*
  33 * Size of a Classical CAN Extended Frame
  34 *
  35 * Name of Field                        Bits
  36 * ---------------------------------------------------------
  37 * Start-of-frame                       1
  38 * Identifier A                         11
  39 * Substitute remote request (SRR)      1
  40 * Identifier extension bit (IDE)       1
  41 * Identifier B                         18
  42 * Remote transmission request (RTR)    1
  43 * Reserved bits (r1, r0)               2
  44 * Data length code (DLC)               4
  45 * Data field                           0...64
  46 * CRC                                  15
  47 * CRC delimiter                        1
  48 * ACK slot                             1
  49 * ACK delimiter                        1
  50 * End-of-frame (EOF)                   7
  51 * Inter frame spacing                  3
  52 *
  53 * rounded up and ignoring bitstuffing
  54 */
  55#define CAN_FRAME_OVERHEAD_EFF DIV_ROUND_UP(67, 8)
  56
  57/*
  58 * Size of a CAN-FD Standard Frame
  59 *
  60 * Name of Field                        Bits
  61 * ---------------------------------------------------------
  62 * Start-of-frame                       1
  63 * Identifier                           11
  64 * Reserved bit (r1)                    1
  65 * Identifier extension bit (IDE)       1
  66 * Flexible data rate format (FDF)      1
  67 * Reserved bit (r0)                    1
  68 * Bit Rate Switch (BRS)                1
  69 * Error Status Indicator (ESI)         1
  70 * Data length code (DLC)               4
  71 * Data field                           0...512
  72 * Stuff Bit Count (SBC)                0...16: 4 20...64:5
  73 * CRC                                  0...16: 17 20...64:21
  74 * CRC delimiter (CD)                   1
  75 * ACK slot (AS)                        1
  76 * ACK delimiter (AD)                   1
  77 * End-of-frame (EOF)                   7
  78 * Inter frame spacing                  3
  79 *
  80 * assuming CRC21, rounded up and ignoring bitstuffing
  81 */
  82#define CANFD_FRAME_OVERHEAD_SFF DIV_ROUND_UP(61, 8)
  83
  84/*
  85 * Size of a CAN-FD Extended Frame
  86 *
  87 * Name of Field                        Bits
  88 * ---------------------------------------------------------
  89 * Start-of-frame                       1
  90 * Identifier A                         11
  91 * Substitute remote request (SRR)      1
  92 * Identifier extension bit (IDE)       1
  93 * Identifier B                         18
  94 * Reserved bit (r1)                    1
  95 * Flexible data rate format (FDF)      1
  96 * Reserved bit (r0)                    1
  97 * Bit Rate Switch (BRS)                1
  98 * Error Status Indicator (ESI)         1
  99 * Data length code (DLC)               4
 100 * Data field                           0...512
 101 * Stuff Bit Count (SBC)                0...16: 4 20...64:5
 102 * CRC                                  0...16: 17 20...64:21
 103 * CRC delimiter (CD)                   1
 104 * ACK slot (AS)                        1
 105 * ACK delimiter (AD)                   1
 106 * End-of-frame (EOF)                   7
 107 * Inter frame spacing                  3
 108 *
 109 * assuming CRC21, rounded up and ignoring bitstuffing
 110 */
 111#define CANFD_FRAME_OVERHEAD_EFF DIV_ROUND_UP(80, 8)
 112
 113/*
 114 * Maximum size of a Classical CAN frame
 115 * (rounded up and ignoring bitstuffing)
 116 */
 117#define CAN_FRAME_LEN_MAX (CAN_FRAME_OVERHEAD_EFF + CAN_MAX_DLEN)
 118
 119/*
 120 * Maximum size of a CAN-FD frame
 121 * (rounded up and ignoring bitstuffing)
 122 */
 123#define CANFD_FRAME_LEN_MAX (CANFD_FRAME_OVERHEAD_EFF + CANFD_MAX_DLEN)
 124
 125/*
 126 * can_cc_dlc2len(value) - convert a given data length code (dlc) of a
 127 * Classical CAN frame into a valid data length of max. 8 bytes.
 128 *
 129 * To be used in the CAN netdriver receive path to ensure conformance with
 130 * ISO 11898-1 Chapter 8.4.2.3 (DLC field)
 131 */
 132#define can_cc_dlc2len(dlc)     (min_t(u8, (dlc), CAN_MAX_DLEN))
 133
 134/* helper to get the data length code (DLC) for Classical CAN raw DLC access */
 135static inline u8 can_get_cc_dlc(const struct can_frame *cf, const u32 ctrlmode)
 136{
 137        /* return len8_dlc as dlc value only if all conditions apply */
 138        if ((ctrlmode & CAN_CTRLMODE_CC_LEN8_DLC) &&
 139            (cf->len == CAN_MAX_DLEN) &&
 140            (cf->len8_dlc > CAN_MAX_DLEN && cf->len8_dlc <= CAN_MAX_RAW_DLC))
 141                return cf->len8_dlc;
 142
 143        /* return the payload length as dlc value */
 144        return cf->len;
 145}
 146
 147/* helper to set len and len8_dlc value for Classical CAN raw DLC access */
 148static inline void can_frame_set_cc_len(struct can_frame *cf, const u8 dlc,
 149                                        const u32 ctrlmode)
 150{
 151        /* the caller already ensured that dlc is a value from 0 .. 15 */
 152        if (ctrlmode & CAN_CTRLMODE_CC_LEN8_DLC && dlc > CAN_MAX_DLEN)
 153                cf->len8_dlc = dlc;
 154
 155        /* limit the payload length 'len' to CAN_MAX_DLEN */
 156        cf->len = can_cc_dlc2len(dlc);
 157}
 158
 159/* get data length from raw data length code (DLC) */
 160u8 can_fd_dlc2len(u8 dlc);
 161
 162/* map the sanitized data length to an appropriate data length code */
 163u8 can_fd_len2dlc(u8 len);
 164
 165/* calculate the CAN Frame length in bytes of a given skb */
 166unsigned int can_skb_get_frame_len(const struct sk_buff *skb);
 167
 168/* map the data length to an appropriate data link layer length */
 169static inline u8 canfd_sanitize_len(u8 len)
 170{
 171        return can_fd_dlc2len(can_fd_len2dlc(len));
 172}
 173
 174#endif /* !_CAN_LENGTH_H */
 175