tests/acceptance/virtiofs_submounts: use workdir property
[qemu/ar7.git] / net / can / can_core.c
blob0115d787941ee6d8a8175a5efb28510f58bfdc22
1 /*
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
25 * THE SOFTWARE.
28 #include "qemu/osdep.h"
29 #include "chardev/char.h"
30 #include "qemu/module.h"
31 #include "qemu/sockets.h"
32 #include "qapi/error.h"
33 #include "net/can_emu.h"
34 #include "qom/object_interfaces.h"
36 /* CAN DLC to real data length conversion helpers */
38 static const uint8_t dlc2len[] = {
39 0, 1, 2, 3, 4, 5, 6, 7,
40 8, 12, 16, 20, 24, 32, 48, 64
43 /* get data length from can_dlc with sanitized can_dlc */
44 uint8_t can_dlc2len(uint8_t can_dlc)
46 return dlc2len[can_dlc & 0x0F];
49 static const uint8_t len2dlc[] = {
50 0, 1, 2, 3, 4, 5, 6, 7, 8, /* 0 - 8 */
51 9, 9, 9, 9, /* 9 - 12 */
52 10, 10, 10, 10, /* 13 - 16 */
53 11, 11, 11, 11, /* 17 - 20 */
54 12, 12, 12, 12, /* 21 - 24 */
55 13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */
56 14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */
57 14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */
58 15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */
59 15, 15, 15, 15, 15, 15, 15, 15 /* 57 - 64 */
62 /* map the sanitized data length to an appropriate data length code */
63 uint8_t can_len2dlc(uint8_t len)
65 if (unlikely(len > 64)) {
66 return 0xF;
69 return len2dlc[len];
72 struct CanBusState {
73 Object object;
75 QTAILQ_HEAD(, CanBusClientState) clients;
78 static void can_bus_instance_init(Object *object)
80 CanBusState *bus = (CanBusState *)object;
82 QTAILQ_INIT(&bus->clients);
85 int can_bus_insert_client(CanBusState *bus, CanBusClientState *client)
87 client->bus = bus;
88 QTAILQ_INSERT_TAIL(&bus->clients, client, next);
89 return 0;
92 int can_bus_remove_client(CanBusClientState *client)
94 CanBusState *bus = client->bus;
95 if (bus == NULL) {
96 return 0;
99 QTAILQ_REMOVE(&bus->clients, client, next);
100 client->bus = NULL;
101 return 1;
104 ssize_t can_bus_client_send(CanBusClientState *client,
105 const struct qemu_can_frame *frames, size_t frames_cnt)
107 int ret = 0;
108 CanBusState *bus = client->bus;
109 CanBusClientState *peer;
110 if (bus == NULL) {
111 return -1;
114 QTAILQ_FOREACH(peer, &bus->clients, next) {
115 if (peer->info->can_receive(peer)) {
116 if (peer == client) {
117 /* No loopback support for now */
118 continue;
120 if (peer->info->receive(peer, frames, frames_cnt) > 0) {
121 ret = 1;
126 return ret;
129 int can_bus_filter_match(struct qemu_can_filter *filter, qemu_canid_t can_id)
131 int m;
132 if (((can_id | filter->can_mask) & QEMU_CAN_ERR_FLAG)) {
133 return (filter->can_mask & QEMU_CAN_ERR_FLAG) != 0;
135 m = (can_id & filter->can_mask) == (filter->can_id & filter->can_mask);
136 return filter->can_id & QEMU_CAN_INV_FILTER ? !m : m;
139 int can_bus_client_set_filters(CanBusClientState *client,
140 const struct qemu_can_filter *filters, size_t filters_cnt)
142 return 0;
146 static bool can_bus_can_be_deleted(UserCreatable *uc)
148 return false;
151 static void can_bus_class_init(ObjectClass *klass,
152 void *class_data G_GNUC_UNUSED)
154 UserCreatableClass *uc_klass = USER_CREATABLE_CLASS(klass);
156 uc_klass->can_be_deleted = can_bus_can_be_deleted;
159 static const TypeInfo can_bus_info = {
160 .parent = TYPE_OBJECT,
161 .name = TYPE_CAN_BUS,
162 .instance_size = sizeof(CanBusState),
163 .instance_init = can_bus_instance_init,
164 .class_init = can_bus_class_init,
165 .interfaces = (InterfaceInfo[]) {
166 { TYPE_USER_CREATABLE },
171 static void can_bus_register_types(void)
173 type_register_static(&can_bus_info);
176 type_init(can_bus_register_types);