xref: /openbmc/qemu/net/can/can_core.c (revision 05caa062)
1 /*
2  * CAN common CAN bus emulation support
3  *
4  * Copyright (c) 2013-2014 Jin Yang
5  * Copyright (c) 2014-2018 Pavel Pisa
6  *
7  * Initial development supported by Google GSoC 2013 from RTEMS project slot
8  *
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:
15  *
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  *
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.
26  */
27 
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"
35 
36 /* CAN DLC to real data length conversion helpers */
37 
38 static const uint8_t dlc2len[] = {
39     0, 1, 2, 3, 4, 5, 6, 7,
40     8, 12, 16, 20, 24, 32, 48, 64
41 };
42 
43 /* get data length from can_dlc with sanitized can_dlc */
44 uint8_t can_dlc2len(uint8_t can_dlc)
45 {
46     return dlc2len[can_dlc & 0x0F];
47 }
48 
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 */
60 };
61 
62 /* map the sanitized data length to an appropriate data length code */
63 uint8_t can_len2dlc(uint8_t len)
64 {
65     if (unlikely(len > 64)) {
66         return 0xF;
67     }
68 
69     return len2dlc[len];
70 }
71 
72 struct CanBusState {
73     Object object;
74 
75     QTAILQ_HEAD(, CanBusClientState) clients;
76 };
77 
78 static void can_bus_instance_init(Object *object)
79 {
80     CanBusState *bus = (CanBusState *)object;
81 
82     QTAILQ_INIT(&bus->clients);
83 }
84 
85 int can_bus_insert_client(CanBusState *bus, CanBusClientState *client)
86 {
87     client->bus = bus;
88     QTAILQ_INSERT_TAIL(&bus->clients, client, next);
89     return 0;
90 }
91 
92 int can_bus_remove_client(CanBusClientState *client)
93 {
94     CanBusState *bus = client->bus;
95     if (bus == NULL) {
96         return 0;
97     }
98 
99     QTAILQ_REMOVE(&bus->clients, client, next);
100     client->bus = NULL;
101     return 1;
102 }
103 
104 ssize_t can_bus_client_send(CanBusClientState *client,
105              const struct qemu_can_frame *frames, size_t frames_cnt)
106 {
107     int ret = 0;
108     CanBusState *bus = client->bus;
109     CanBusClientState *peer;
110     if (bus == NULL) {
111         return -1;
112     }
113 
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;
119             }
120             if (peer->info->receive(peer, frames, frames_cnt) > 0) {
121                 ret = 1;
122             }
123         }
124     }
125 
126     return ret;
127 }
128 
129 int can_bus_filter_match(struct qemu_can_filter *filter, qemu_canid_t can_id)
130 {
131     int m;
132     if (((can_id | filter->can_mask) & QEMU_CAN_ERR_FLAG)) {
133         return (filter->can_mask & QEMU_CAN_ERR_FLAG) != 0;
134     }
135     m = (can_id & filter->can_mask) == (filter->can_id & filter->can_mask);
136     return filter->can_id & QEMU_CAN_INV_FILTER ? !m : m;
137 }
138 
139 int can_bus_client_set_filters(CanBusClientState *client,
140              const struct qemu_can_filter *filters, size_t filters_cnt)
141 {
142     return 0;
143 }
144 
145 
146 static bool can_bus_can_be_deleted(UserCreatable *uc)
147 {
148     return false;
149 }
150 
151 static void can_bus_class_init(ObjectClass *klass,
152                                 void *class_data G_GNUC_UNUSED)
153 {
154     UserCreatableClass *uc_klass = USER_CREATABLE_CLASS(klass);
155 
156     uc_klass->can_be_deleted = can_bus_can_be_deleted;
157 }
158 
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 },
167         { }
168     }
169 };
170 
171 static void can_bus_register_types(void)
172 {
173     type_register_static(&can_bus_info);
174 }
175 
176 type_init(can_bus_register_types);
177