blob: a3c84028c61fd40855ced5df4de3a9e2d4cead03 [file] [log] [blame]
Pavel Pisad18957d2018-01-27 17:41:07 +01001/*
2 * CAN generic CAN host connection 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 */
Markus Armbruster0b8fa322019-05-23 16:35:07 +020027
Pavel Pisad18957d2018-01-27 17:41:07 +010028#include "qemu/osdep.h"
29#include "chardev/char.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020030#include "qemu/module.h"
Pavel Pisad18957d2018-01-27 17:41:07 +010031#include "qemu/sockets.h"
32#include "qapi/error.h"
33#include "qom/object_interfaces.h"
34#include "net/can_emu.h"
35#include "net/can_host.h"
36
37struct CanBusState {
38 Object object;
39
40 QTAILQ_HEAD(, CanBusClientState) clients;
41};
42
43static void can_host_disconnect(CanHostState *ch)
44{
45 CanHostClass *chc = CAN_HOST_GET_CLASS(ch);
46
47 can_bus_remove_client(&ch->bus_client);
48 chc->disconnect(ch);
49}
50
51static void can_host_connect(CanHostState *ch, Error **errp)
52{
53 CanHostClass *chc = CAN_HOST_GET_CLASS(ch);
54 Error *local_err = NULL;
55
Kevin Wolf7cc25f62020-11-30 11:56:15 +010056 if (ch->bus == NULL) {
57 error_setg(errp, "'canbus' property not set");
58 return;
59 }
60
Pavel Pisad18957d2018-01-27 17:41:07 +010061 chc->connect(ch, &local_err);
62 if (local_err) {
63 error_propagate(errp, local_err);
64 return;
65 }
66
67 can_bus_insert_client(ch->bus, &ch->bus_client);
68}
69
70static void can_host_unparent(Object *obj)
71{
72 can_host_disconnect(CAN_HOST(obj));
73}
74
75static void can_host_complete(UserCreatable *uc, Error **errp)
76{
77 can_host_connect(CAN_HOST(uc), errp);
78}
79
Pavel Pisad18957d2018-01-27 17:41:07 +010080static void can_host_class_init(ObjectClass *klass,
81 void *class_data G_GNUC_UNUSED)
82{
83 UserCreatableClass *uc_klass = USER_CREATABLE_CLASS(klass);
84
Eduardo Habkost70b75662020-11-11 13:38:19 -050085 object_class_property_add_link(klass, "canbus", TYPE_CAN_BUS,
86 offsetof(CanHostState, bus),
87 object_property_allow_set_link,
88 OBJ_PROP_LINK_STRONG);
89
Pavel Pisad18957d2018-01-27 17:41:07 +010090 klass->unparent = can_host_unparent;
91 uc_klass->complete = can_host_complete;
92}
93
94static const TypeInfo can_host_info = {
95 .parent = TYPE_OBJECT,
96 .name = TYPE_CAN_HOST,
97 .instance_size = sizeof(CanHostState),
98 .class_size = sizeof(CanHostClass),
99 .abstract = true,
Pavel Pisad18957d2018-01-27 17:41:07 +0100100 .class_init = can_host_class_init,
101 .interfaces = (InterfaceInfo[]) {
102 { TYPE_USER_CREATABLE },
103 { }
104 }
105};
106
107static void can_host_register_types(void)
108{
109 type_register_static(&can_host_info);
110}
111
112type_init(can_host_register_types);