1 1.3 bouyer /* $NetBSD: can.h,v 1.3 2017/05/30 13:30:51 bouyer Exp $ */ 2 1.2 bouyer 3 1.2 bouyer /*- 4 1.2 bouyer * Copyright (c) 2003, 2017 The NetBSD Foundation, Inc. 5 1.2 bouyer * All rights reserved. 6 1.2 bouyer * 7 1.2 bouyer * This code is derived from software contributed to The NetBSD Foundation 8 1.2 bouyer * by Robert Swindells and Manuel Bouyer 9 1.2 bouyer * 10 1.2 bouyer * Redistribution and use in source and binary forms, with or without 11 1.2 bouyer * modification, are permitted provided that the following conditions 12 1.2 bouyer * are met: 13 1.2 bouyer * 1. Redistributions of source code must retain the above copyright 14 1.2 bouyer * notice, this list of conditions and the following disclaimer. 15 1.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright 16 1.2 bouyer * notice, this list of conditions and the following disclaimer in the 17 1.2 bouyer * documentation and/or other materials provided with the distribution. 18 1.2 bouyer * 19 1.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.2 bouyer * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.2 bouyer * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.2 bouyer * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.2 bouyer * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.2 bouyer * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.2 bouyer * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.2 bouyer * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.2 bouyer * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.2 bouyer * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.2 bouyer * POSSIBILITY OF SUCH DAMAGE. 30 1.2 bouyer */ 31 1.2 bouyer 32 1.2 bouyer #ifndef _NETCAN_CAN_H 33 1.2 bouyer #define _NETCAN_CAN_H 34 1.2 bouyer 35 1.2 bouyer #include <sys/featuretest.h> 36 1.3 bouyer #include <sys/types.h> 37 1.2 bouyer 38 1.2 bouyer 39 1.2 bouyer /* Definitions compatible (as much as possible) with socketCAN */ 40 1.2 bouyer 41 1.2 bouyer /* 42 1.2 bouyer * CAN id structure 43 1.2 bouyer * bits 0-28 : CAN identifier (11/29 bits, see bit 31) 44 1.2 bouyer * bit2 29-31 : see below 45 1.2 bouyer */ 46 1.2 bouyer 47 1.2 bouyer typedef uint32_t canid_t; 48 1.2 bouyer typedef uint32_t can_err_mask_t; 49 1.2 bouyer 50 1.2 bouyer /* canid_t bits 29-31 descriptions */ 51 1.2 bouyer #define CAN_EFF_FLAG 0x80000000U /* extended frame format */ 52 1.2 bouyer #define CAN_RTR_FLAG 0x40000000U /* remote transmission request */ 53 1.2 bouyer #define CAN_ERR_FLAG 0x20000000U /* error message frame */ 54 1.2 bouyer 55 1.2 bouyer /* valid bits in CAN ID for frame formats */ 56 1.2 bouyer #define CAN_SFF_MASK 0x000007FFU /* standard frame format (SFF) */ 57 1.2 bouyer #define CAN_EFF_MASK 0x1FFFFFFFU /* extended frame format (EFF) */ 58 1.2 bouyer #define CAN_ERR_MASK 0x1FFFFFFFU /* error frame format */ 59 1.2 bouyer 60 1.2 bouyer /* CAN payload length and DLC definitions according to ISO 11898-1 */ 61 1.2 bouyer #define CAN_MAX_DLC 8 62 1.2 bouyer #define CAN_MAX_DLEN 8 63 1.2 bouyer 64 1.2 bouyer /* CAN frame */ 65 1.2 bouyer struct can_frame { 66 1.2 bouyer canid_t can_id; /* ID + EFF/RTR/ERR flags */ 67 1.2 bouyer uint8_t can_dlc; /* frame payload length in byte (0 .. CAN_MAX_DLEN) */ 68 1.2 bouyer uint8_t __pad; 69 1.2 bouyer uint8_t __res0; 70 1.2 bouyer uint8_t __res1; 71 1.2 bouyer uint8_t data[CAN_MAX_DLEN] __aligned(8); 72 1.2 bouyer }; 73 1.2 bouyer 74 1.2 bouyer #define CAN_MTU (sizeof(struct can_frame)) 75 1.2 bouyer 76 1.2 bouyer /* protocols */ 77 1.2 bouyer #define CAN_RAW 1 /* RAW sockets */ 78 1.2 bouyer #define CAN_NPROTO 2 79 1.2 bouyer 80 1.2 bouyer /* 81 1.2 bouyer * Socket address, CAN style 82 1.2 bouyer */ 83 1.2 bouyer struct sockaddr_can { 84 1.2 bouyer u_int8_t can_len; 85 1.2 bouyer sa_family_t can_family; 86 1.2 bouyer int can_ifindex; 87 1.2 bouyer union { 88 1.2 bouyer /* transport protocol class address information (e.g. ISOTP) */ 89 1.2 bouyer struct { canid_t rx_id, tx_id; } tp; 90 1.2 bouyer /* reserved for future CAN protocols address information */ 91 1.2 bouyer } can_addr; 92 1.2 bouyer }; 93 1.2 bouyer 94 1.2 bouyer /* 95 1.2 bouyer * Options for use with [gs]etsockopt for raw sockets 96 1.2 bouyer * First word of comment is data type; bool is stored in int. 97 1.2 bouyer */ 98 1.2 bouyer #define SOL_CAN_RAW CAN_RAW 99 1.2 bouyer 100 1.2 bouyer #define CAN_RAW_FILTER 1 /* struct can_filter: set filter */ 101 1.2 bouyer #define CAN_RAW_LOOPBACK 4 /* bool: loopback to local sockets (default:on) */ 102 1.2 bouyer #define CAN_RAW_RECV_OWN_MSGS 5 /* bool: receive my own msgs (default:off) */ 103 1.2 bouyer 104 1.2 bouyer /* 105 1.2 bouyer * CAN ID based filter 106 1.2 bouyer * checks received can_id & can_filter.can_mask against 107 1.2 bouyer * can_filter.can_id & can_filter.can_mask 108 1.2 bouyer * valid flags for can_id: 109 1.2 bouyer * CAN_INV_FILTER: invert filter 110 1.2 bouyer * valid flags for can_mask: 111 1.2 bouyer * CAN_ERR_FLAG: filter for error message frames 112 1.2 bouyer */ 113 1.2 bouyer struct can_filter { 114 1.2 bouyer canid_t can_id; 115 1.2 bouyer canid_t can_mask; 116 1.2 bouyer }; 117 1.2 bouyer 118 1.2 bouyer #define CAN_INV_FILTER 0x20000000U 119 1.2 bouyer 120 1.2 bouyer #ifdef _NETBSD_SOURCE 121 1.2 bouyer #ifdef _KERNEL 122 1.2 bouyer 123 1.2 bouyer #define satoscan(sa) ((struct sockaddr_can *)(sa)) 124 1.2 bouyer #define scantosa(scan) ((struct sockaddr *)(scan)) 125 1.2 bouyer 126 1.2 bouyer #endif /* _KERNEL */ 127 1.2 bouyer #endif /* _NETBSD_SOURCE */ 128 1.2 bouyer #endif /* _NETCAN_CAN_H */ 129