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