2 * CAN common CAN bus emulation support
4 * Copyright (c) 2013-2014 Jin Yang
5 * Copyright (c) 2014-2018 Pavel Pisa
7 * Initial development supported by Google GSoC 2013 from RTEMS project slot
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31 #include "qom/object.h"
33 /* NOTE: the following two structures is copied from <linux/can.h>. */
36 * Controller Area Network Identifier structure
38 * bit 0-28 : CAN identifier (11/29 bit)
39 * bit 29 : error frame flag (0 = data frame, 1 = error frame)
40 * bit 30 : remote transmission request flag (1 = rtr frame)
41 * bit 31 : frame format flag (0 = standard 11 bit, 1 = extended 29 bit)
43 typedef uint32_t qemu_canid_t
;
45 typedef struct qemu_can_frame
{
46 qemu_canid_t can_id
; /* 32 bit CAN_ID + EFF/RTR/ERR flags */
47 uint8_t can_dlc
; /* data length code: 0 .. 8 */
48 uint8_t data
[8] QEMU_ALIGNED(8);
51 /* Keep defines for QEMU separate from Linux ones for now */
53 #define QEMU_CAN_EFF_FLAG 0x80000000U /* EFF/SFF is set in the MSB */
54 #define QEMU_CAN_RTR_FLAG 0x40000000U /* remote transmission request */
55 #define QEMU_CAN_ERR_FLAG 0x20000000U /* error message frame */
57 #define QEMU_CAN_SFF_MASK 0x000007FFU /* standard frame format (SFF) */
58 #define QEMU_CAN_EFF_MASK 0x1FFFFFFFU /* extended frame format (EFF) */
61 * struct qemu_can_filter - CAN ID based filter in can_register().
62 * @can_id: relevant bits of CAN ID which are not masked out.
63 * @can_mask: CAN mask (see description)
66 * A filter matches, when
68 * <received_can_id> & mask == can_id & mask
70 * The filter can be inverted (QEMU_CAN_INV_FILTER bit set in can_id) or it can
71 * filter for error message frames (QEMU_CAN_ERR_FLAG bit set in mask).
73 typedef struct qemu_can_filter
{
75 qemu_canid_t can_mask
;
78 /* QEMU_CAN_INV_FILTER can be set in qemu_can_filter.can_id */
79 #define QEMU_CAN_INV_FILTER 0x20000000U
81 typedef struct CanBusClientState CanBusClientState
;
82 typedef struct CanBusState CanBusState
;
84 typedef struct CanBusClientInfo
{
85 int (*can_receive
)(CanBusClientState
*);
86 ssize_t (*receive
)(CanBusClientState
*,
87 const struct qemu_can_frame
*frames
, size_t frames_cnt
);
90 struct CanBusClientState
{
91 CanBusClientInfo
*info
;
94 QTAILQ_ENTRY(CanBusClientState
) next
;
95 CanBusClientState
*peer
;
98 void (*destructor
)(CanBusClientState
*);
101 #define TYPE_CAN_BUS "can-bus"
102 #define CAN_BUS_CLASS(klass) \
103 OBJECT_CLASS_CHECK(CanBusClass, (klass), TYPE_CAN_BUS)
104 #define CAN_BUS_GET_CLASS(obj) \
105 OBJECT_GET_CLASS(CanBusClass, (obj), TYPE_CAN_BUS)
106 #define CAN_BUS(obj) \
107 OBJECT_CHECK(CanBusState, (obj), TYPE_CAN_BUS)
109 int can_bus_filter_match(struct qemu_can_filter
*filter
, qemu_canid_t can_id
);
111 int can_bus_insert_client(CanBusState
*bus
, CanBusClientState
*client
);
113 int can_bus_remove_client(CanBusClientState
*client
);
115 ssize_t
can_bus_client_send(CanBusClientState
*,
116 const struct qemu_can_frame
*frames
,
119 int can_bus_client_set_filters(CanBusClientState
*,
120 const struct qemu_can_filter
*filters
,