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 | * |
Oliver Hartkopp | 0d66548 | 2007-11-16 15:52:17 -0800 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #ifndef CAN_H |
| 14 | #define CAN_H |
| 15 | |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/socket.h> |
| 18 | |
| 19 | /* controller area network (CAN) kernel definitions */ |
| 20 | |
| 21 | /* special address description flags for the CAN_ID */ |
| 22 | #define CAN_EFF_FLAG 0x80000000U /* EFF/SFF is set in the MSB */ |
| 23 | #define CAN_RTR_FLAG 0x40000000U /* remote transmission request */ |
Oliver Hartkopp | d6e640f | 2012-05-08 22:20:33 +0200 | [diff] [blame] | 24 | #define CAN_ERR_FLAG 0x20000000U /* error message frame */ |
Oliver Hartkopp | 0d66548 | 2007-11-16 15:52:17 -0800 | [diff] [blame] | 25 | |
| 26 | /* valid bits in CAN ID for frame formats */ |
| 27 | #define CAN_SFF_MASK 0x000007FFU /* standard frame format (SFF) */ |
| 28 | #define CAN_EFF_MASK 0x1FFFFFFFU /* extended frame format (EFF) */ |
| 29 | #define CAN_ERR_MASK 0x1FFFFFFFU /* omit EFF, RTR, ERR flags */ |
| 30 | |
| 31 | /* |
| 32 | * Controller Area Network Identifier structure |
| 33 | * |
| 34 | * bit 0-28 : CAN identifier (11/29 bit) |
Oliver Hartkopp | d6e640f | 2012-05-08 22:20:33 +0200 | [diff] [blame] | 35 | * bit 29 : error message frame flag (0 = data frame, 1 = error message) |
Oliver Hartkopp | 0d66548 | 2007-11-16 15:52:17 -0800 | [diff] [blame] | 36 | * bit 30 : remote transmission request flag (1 = rtr frame) |
| 37 | * bit 31 : frame format flag (0 = standard 11 bit, 1 = extended 29 bit) |
| 38 | */ |
| 39 | typedef __u32 canid_t; |
| 40 | |
Rostislav Lisovy | f057bbb | 2012-07-04 05:32:03 +0200 | [diff] [blame] | 41 | #define CAN_SFF_ID_BITS 11 |
| 42 | #define CAN_EFF_ID_BITS 29 |
| 43 | |
Oliver Hartkopp | 0d66548 | 2007-11-16 15:52:17 -0800 | [diff] [blame] | 44 | /* |
Oliver Hartkopp | d6e640f | 2012-05-08 22:20:33 +0200 | [diff] [blame] | 45 | * Controller Area Network Error Message Frame Mask structure |
Oliver Hartkopp | 0d66548 | 2007-11-16 15:52:17 -0800 | [diff] [blame] | 46 | * |
| 47 | * bit 0-28 : error class mask (see include/linux/can/error.h) |
| 48 | * bit 29-31 : set to zero |
| 49 | */ |
| 50 | typedef __u32 can_err_mask_t; |
| 51 | |
Oliver Hartkopp | 7c94163 | 2012-06-13 20:04:33 +0200 | [diff] [blame] | 52 | /* CAN payload length and DLC definitions according to ISO 11898-1 */ |
| 53 | #define CAN_MAX_DLC 8 |
| 54 | #define CAN_MAX_DLEN 8 |
| 55 | |
| 56 | /* CAN FD payload length and DLC definitions according to ISO 11898-7 */ |
| 57 | #define CANFD_MAX_DLC 15 |
| 58 | #define CANFD_MAX_DLEN 64 |
| 59 | |
Oliver Hartkopp | 0d66548 | 2007-11-16 15:52:17 -0800 | [diff] [blame] | 60 | /** |
| 61 | * struct can_frame - basic CAN frame structure |
Oliver Hartkopp | 7c94163 | 2012-06-13 20:04:33 +0200 | [diff] [blame] | 62 | * @can_id: CAN ID of the frame and CAN_*_FLAG flags, see canid_t definition |
| 63 | * @can_dlc: frame payload length in byte (0 .. 8) aka data length code |
| 64 | * N.B. the DLC field from ISO 11898-1 Chapter 8.4.2.3 has a 1:1 |
| 65 | * mapping of the 'data length code' to the real payload length |
| 66 | * @data: CAN frame payload (up to 8 byte) |
Oliver Hartkopp | 0d66548 | 2007-11-16 15:52:17 -0800 | [diff] [blame] | 67 | */ |
| 68 | struct can_frame { |
| 69 | canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */ |
Oliver Hartkopp | 7c94163 | 2012-06-13 20:04:33 +0200 | [diff] [blame] | 70 | __u8 can_dlc; /* frame payload length in byte (0 .. CAN_MAX_DLEN) */ |
| 71 | __u8 data[CAN_MAX_DLEN] __attribute__((aligned(8))); |
Oliver Hartkopp | 0d66548 | 2007-11-16 15:52:17 -0800 | [diff] [blame] | 72 | }; |
| 73 | |
Oliver Hartkopp | 7c94163 | 2012-06-13 20:04:33 +0200 | [diff] [blame] | 74 | /* |
| 75 | * defined bits for canfd_frame.flags |
| 76 | * |
| 77 | * As the default for CAN FD should be to support the high data rate in the |
| 78 | * payload section of the frame (HDR) and to support up to 64 byte in the |
| 79 | * data section (EDL) the bits are only set in the non-default case. |
| 80 | * Btw. as long as there's no real implementation for CAN FD network driver |
| 81 | * these bits are only preliminary. |
| 82 | * |
| 83 | * RX: NOHDR/NOEDL - info about received CAN FD frame |
| 84 | * ESI - bit from originating CAN controller |
| 85 | * TX: NOHDR/NOEDL - control per-frame settings if supported by CAN controller |
| 86 | * ESI - bit is set by local CAN controller |
| 87 | */ |
| 88 | #define CANFD_NOHDR 0x01 /* frame without high data rate */ |
| 89 | #define CANFD_NOEDL 0x02 /* frame without extended data length */ |
| 90 | #define CANFD_ESI 0x04 /* error state indicator */ |
| 91 | |
| 92 | /** |
| 93 | * struct canfd_frame - CAN flexible data rate frame structure |
| 94 | * @can_id: CAN ID of the frame and CAN_*_FLAG flags, see canid_t definition |
| 95 | * @len: frame payload length in byte (0 .. CANFD_MAX_DLEN) |
| 96 | * @flags: additional flags for CAN FD |
| 97 | * @__res0: reserved / padding |
| 98 | * @__res1: reserved / padding |
| 99 | * @data: CAN FD frame payload (up to CANFD_MAX_DLEN byte) |
| 100 | */ |
| 101 | struct canfd_frame { |
| 102 | canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */ |
| 103 | __u8 len; /* frame payload length in byte */ |
| 104 | __u8 flags; /* additional flags for CAN FD */ |
| 105 | __u8 __res0; /* reserved / padding */ |
| 106 | __u8 __res1; /* reserved / padding */ |
| 107 | __u8 data[CANFD_MAX_DLEN] __attribute__((aligned(8))); |
| 108 | }; |
| 109 | |
| 110 | #define CAN_MTU (sizeof(struct can_frame)) |
| 111 | #define CANFD_MTU (sizeof(struct canfd_frame)) |
| 112 | |
Oliver Hartkopp | 0d66548 | 2007-11-16 15:52:17 -0800 | [diff] [blame] | 113 | /* particular protocols of the protocol family PF_CAN */ |
| 114 | #define CAN_RAW 1 /* RAW sockets */ |
| 115 | #define CAN_BCM 2 /* Broadcast Manager */ |
| 116 | #define CAN_TP16 3 /* VAG Transport Protocol v1.6 */ |
| 117 | #define CAN_TP20 4 /* VAG Transport Protocol v2.0 */ |
| 118 | #define CAN_MCNET 5 /* Bosch MCNet */ |
| 119 | #define CAN_ISOTP 6 /* ISO 15765-2 Transport Protocol */ |
| 120 | #define CAN_NPROTO 7 |
| 121 | |
| 122 | #define SOL_CAN_BASE 100 |
| 123 | |
| 124 | /** |
| 125 | * struct sockaddr_can - the sockaddr structure for CAN sockets |
| 126 | * @can_family: address family number AF_CAN. |
| 127 | * @can_ifindex: CAN network interface index. |
| 128 | * @can_addr: protocol specific address information |
| 129 | */ |
| 130 | struct sockaddr_can { |
Ben Hutchings | bcb949b | 2011-08-24 18:43:55 +0000 | [diff] [blame] | 131 | __kernel_sa_family_t can_family; |
Oliver Hartkopp | 0d66548 | 2007-11-16 15:52:17 -0800 | [diff] [blame] | 132 | int can_ifindex; |
| 133 | union { |
| 134 | /* transport protocol class address information (e.g. ISOTP) */ |
| 135 | struct { canid_t rx_id, tx_id; } tp; |
| 136 | |
| 137 | /* reserved for future CAN protocols address information */ |
| 138 | } can_addr; |
| 139 | }; |
| 140 | |
| 141 | /** |
| 142 | * struct can_filter - CAN ID based filter in can_register(). |
| 143 | * @can_id: relevant bits of CAN ID which are not masked out. |
| 144 | * @can_mask: CAN mask (see description) |
| 145 | * |
| 146 | * Description: |
| 147 | * A filter matches, when |
| 148 | * |
| 149 | * <received_can_id> & mask == can_id & mask |
| 150 | * |
| 151 | * 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] | 152 | * filter for error message frames (CAN_ERR_FLAG bit set in mask). |
Oliver Hartkopp | 0d66548 | 2007-11-16 15:52:17 -0800 | [diff] [blame] | 153 | */ |
| 154 | struct can_filter { |
| 155 | canid_t can_id; |
| 156 | canid_t can_mask; |
| 157 | }; |
| 158 | |
| 159 | #define CAN_INV_FILTER 0x20000000U /* to be set in can_filter.can_id */ |
| 160 | |
| 161 | #endif /* CAN_H */ |