xref: /openbmc/qemu/net/can/can_host.c (revision 0ea0538f)
1 /*
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  */
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 "qom/object_interfaces.h"
34 #include "net/can_emu.h"
35 #include "net/can_host.h"
36 
37 static void can_host_disconnect(CanHostState *ch)
38 {
39     CanHostClass *chc = CAN_HOST_GET_CLASS(ch);
40 
41     can_bus_remove_client(&ch->bus_client);
42     chc->disconnect(ch);
43 }
44 
45 static void can_host_connect(CanHostState *ch, Error **errp)
46 {
47     CanHostClass *chc = CAN_HOST_GET_CLASS(ch);
48     Error *local_err = NULL;
49 
50     if (ch->bus == NULL) {
51         error_setg(errp, "'canbus' property not set");
52         return;
53     }
54 
55     chc->connect(ch, &local_err);
56     if (local_err) {
57         error_propagate(errp, local_err);
58         return;
59     }
60 
61     can_bus_insert_client(ch->bus, &ch->bus_client);
62 }
63 
64 static void can_host_unparent(Object *obj)
65 {
66     can_host_disconnect(CAN_HOST(obj));
67 }
68 
69 static void can_host_complete(UserCreatable *uc, Error **errp)
70 {
71     can_host_connect(CAN_HOST(uc), errp);
72 }
73 
74 static void can_host_class_init(ObjectClass *klass,
75                                 void *class_data G_GNUC_UNUSED)
76 {
77     UserCreatableClass *uc_klass = USER_CREATABLE_CLASS(klass);
78 
79     object_class_property_add_link(klass, "canbus", TYPE_CAN_BUS,
80                                    offsetof(CanHostState, bus),
81                                    object_property_allow_set_link,
82                                    OBJ_PROP_LINK_STRONG);
83 
84     klass->unparent = can_host_unparent;
85     uc_klass->complete = can_host_complete;
86 }
87 
88 static const TypeInfo can_host_info = {
89     .parent = TYPE_OBJECT,
90     .name = TYPE_CAN_HOST,
91     .instance_size = sizeof(CanHostState),
92     .class_size = sizeof(CanHostClass),
93     .abstract = true,
94     .class_init = can_host_class_init,
95     .interfaces = (InterfaceInfo[]) {
96         { TYPE_USER_CREATABLE },
97         { }
98     }
99 };
100 
101 static void can_host_register_types(void)
102 {
103     type_register_static(&can_host_info);
104 }
105 
106 type_init(can_host_register_types);
107