can.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. *
  11. */
  12. #ifndef CAN_H
  13. #define CAN_H
  14. #include <linux/types.h>
  15. #include <linux/socket.h>
  16. /* controller area network (CAN) kernel definitions */
  17. /* special address description flags for the CAN_ID */
  18. #define CAN_EFF_FLAG 0x80000000U /* EFF/SFF is set in the MSB */
  19. #define CAN_RTR_FLAG 0x40000000U /* remote transmission request */
  20. #define CAN_ERR_FLAG 0x20000000U /* error message frame */
  21. /* valid bits in CAN ID for frame formats */
  22. #define CAN_SFF_MASK 0x000007FFU /* standard frame format (SFF) */
  23. #define CAN_EFF_MASK 0x1FFFFFFFU /* extended frame format (EFF) */
  24. #define CAN_ERR_MASK 0x1FFFFFFFU /* omit EFF, RTR, ERR flags */
  25. /*
  26. * Controller Area Network Identifier structure
  27. *
  28. * bit 0-28 : CAN identifier (11/29 bit)
  29. * bit 29 : error message frame flag (0 = data frame, 1 = error message)
  30. * bit 30 : remote transmission request flag (1 = rtr frame)
  31. * bit 31 : frame format flag (0 = standard 11 bit, 1 = extended 29 bit)
  32. */
  33. typedef __u32 canid_t;
  34. #define CAN_SFF_ID_BITS 11
  35. #define CAN_EFF_ID_BITS 29
  36. /*
  37. * Controller Area Network Error Message Frame Mask structure
  38. *
  39. * bit 0-28 : error class mask (see include/linux/can/error.h)
  40. * bit 29-31 : set to zero
  41. */
  42. typedef __u32 can_err_mask_t;
  43. /* CAN payload length and DLC definitions according to ISO 11898-1 */
  44. #define CAN_MAX_DLC 8
  45. #define CAN_MAX_DLEN 8
  46. /* CAN FD payload length and DLC definitions according to ISO 11898-7 */
  47. #define CANFD_MAX_DLC 15
  48. #define CANFD_MAX_DLEN 64
  49. /**
  50. * struct can_frame - basic CAN frame structure
  51. * @can_id: CAN ID of the frame and CAN_*_FLAG flags, see canid_t definition
  52. * @can_dlc: frame payload length in byte (0 .. 8) aka data length code
  53. * N.B. the DLC field from ISO 11898-1 Chapter 8.4.2.3 has a 1:1
  54. * mapping of the 'data length code' to the real payload length
  55. * @data: CAN frame payload (up to 8 byte)
  56. */
  57. struct can_frame {
  58. canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */
  59. __u8 can_dlc; /* frame payload length in byte (0 .. CAN_MAX_DLEN) */
  60. __u8 data[CAN_MAX_DLEN] __attribute__((aligned(8)));
  61. };
  62. /*
  63. * defined bits for canfd_frame.flags
  64. *
  65. * The use of struct canfd_frame implies the Extended Data Length (EDL) bit to
  66. * be set in the CAN frame bitstream on the wire. The EDL bit switch turns
  67. * the CAN controllers bitstream processor into the CAN FD mode which creates
  68. * two new options within the CAN FD frame specification:
  69. *
  70. * Bit Rate Switch - to indicate a second bitrate is/was used for the payload
  71. * Error State Indicator - represents the error state of the transmitting node
  72. *
  73. * As the CANFD_ESI bit is internally generated by the transmitting CAN
  74. * controller only the CANFD_BRS bit is relevant for real CAN controllers when
  75. * building a CAN FD frame for transmission. Setting the CANFD_ESI bit can make
  76. * sense for virtual CAN interfaces to test applications with echoed frames.
  77. */
  78. #define CANFD_BRS 0x01 /* bit rate switch (second bitrate for payload data) */
  79. #define CANFD_ESI 0x02 /* error state indicator of the transmitting node */
  80. /**
  81. * struct canfd_frame - CAN flexible data rate frame structure
  82. * @can_id: CAN ID of the frame and CAN_*_FLAG flags, see canid_t definition
  83. * @len: frame payload length in byte (0 .. CANFD_MAX_DLEN)
  84. * @flags: additional flags for CAN FD
  85. * @__res0: reserved / padding
  86. * @__res1: reserved / padding
  87. * @data: CAN FD frame payload (up to CANFD_MAX_DLEN byte)
  88. */
  89. struct canfd_frame {
  90. canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */
  91. __u8 len; /* frame payload length in byte */
  92. __u8 flags; /* additional flags for CAN FD */
  93. __u8 __res0; /* reserved / padding */
  94. __u8 __res1; /* reserved / padding */
  95. __u8 data[CANFD_MAX_DLEN] __attribute__((aligned(8)));
  96. };
  97. #define CAN_MTU (sizeof(struct can_frame))
  98. #define CANFD_MTU (sizeof(struct canfd_frame))
  99. /* particular protocols of the protocol family PF_CAN */
  100. #define CAN_RAW 1 /* RAW sockets */
  101. #define CAN_BCM 2 /* Broadcast Manager */
  102. #define CAN_TP16 3 /* VAG Transport Protocol v1.6 */
  103. #define CAN_TP20 4 /* VAG Transport Protocol v2.0 */
  104. #define CAN_MCNET 5 /* Bosch MCNet */
  105. #define CAN_ISOTP 6 /* ISO 15765-2 Transport Protocol */
  106. #define CAN_NPROTO 7
  107. #define SOL_CAN_BASE 100
  108. /**
  109. * struct sockaddr_can - the sockaddr structure for CAN sockets
  110. * @can_family: address family number AF_CAN.
  111. * @can_ifindex: CAN network interface index.
  112. * @can_addr: protocol specific address information
  113. */
  114. struct sockaddr_can {
  115. __kernel_sa_family_t can_family;
  116. int can_ifindex;
  117. union {
  118. /* transport protocol class address information (e.g. ISOTP) */
  119. struct { canid_t rx_id, tx_id; } tp;
  120. /* reserved for future CAN protocols address information */
  121. } can_addr;
  122. };
  123. /**
  124. * struct can_filter - CAN ID based filter in can_register().
  125. * @can_id: relevant bits of CAN ID which are not masked out.
  126. * @can_mask: CAN mask (see description)
  127. *
  128. * Description:
  129. * A filter matches, when
  130. *
  131. * <received_can_id> & mask == can_id & mask
  132. *
  133. * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
  134. * filter for error message frames (CAN_ERR_FLAG bit set in mask).
  135. */
  136. struct can_filter {
  137. canid_t can_id;
  138. canid_t can_mask;
  139. };
  140. #define CAN_INV_FILTER 0x20000000U /* to be set in can_filter.can_id */
  141. #endif /* CAN_H */