keymaps: regenerate keymaps
[qemu/ar7.git] / hw / core / bus.c
blobe09843f6abea0e24f8adf2506771745e22f21e69
1 /*
2 * Dynamic device configuration and creation -- buses.
4 * Copyright (c) 2009 CodeSourcery
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
21 #include "qemu-common.h"
22 #include "hw/qdev.h"
23 #include "qapi/error.h"
25 void qbus_set_hotplug_handler(BusState *bus, Object *handler, Error **errp)
27 object_property_set_link(OBJECT(bus), OBJECT(handler),
28 QDEV_HOTPLUG_HANDLER_PROPERTY, errp);
31 void qbus_set_bus_hotplug_handler(BusState *bus, Error **errp)
33 qbus_set_hotplug_handler(bus, OBJECT(bus), errp);
36 int qbus_walk_children(BusState *bus,
37 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
38 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
39 void *opaque)
41 BusChild *kid;
42 int err;
44 if (pre_busfn) {
45 err = pre_busfn(bus, opaque);
46 if (err) {
47 return err;
51 QTAILQ_FOREACH(kid, &bus->children, sibling) {
52 err = qdev_walk_children(kid->child,
53 pre_devfn, pre_busfn,
54 post_devfn, post_busfn, opaque);
55 if (err < 0) {
56 return err;
60 if (post_busfn) {
61 err = post_busfn(bus, opaque);
62 if (err) {
63 return err;
67 return 0;
70 static void qbus_realize(BusState *bus, DeviceState *parent, const char *name)
72 const char *typename = object_get_typename(OBJECT(bus));
73 BusClass *bc;
74 int i, bus_id;
76 bus->parent = parent;
78 if (name) {
79 bus->name = g_strdup(name);
80 } else if (bus->parent && bus->parent->id) {
81 /* parent device has id -> use it plus parent-bus-id for bus name */
82 bus_id = bus->parent->num_child_bus;
83 bus->name = g_strdup_printf("%s.%d", bus->parent->id, bus_id);
84 } else {
85 /* no id -> use lowercase bus type plus global bus-id for bus name */
86 bc = BUS_GET_CLASS(bus);
87 bus_id = bc->automatic_ids++;
88 bus->name = g_strdup_printf("%s.%d", typename, bus_id);
89 for (i = 0; bus->name[i]; i++) {
90 bus->name[i] = qemu_tolower(bus->name[i]);
94 if (bus->parent) {
95 QLIST_INSERT_HEAD(&bus->parent->child_bus, bus, sibling);
96 bus->parent->num_child_bus++;
97 object_property_add_child(OBJECT(bus->parent), bus->name, OBJECT(bus), NULL);
98 object_unref(OBJECT(bus));
99 } else if (bus != sysbus_get_default()) {
100 /* TODO: once all bus devices are qdevified,
101 only reset handler for main_system_bus should be registered here. */
102 qemu_register_reset(qbus_reset_all_fn, bus);
106 static void bus_unparent(Object *obj)
108 BusState *bus = BUS(obj);
109 BusChild *kid;
111 while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
112 DeviceState *dev = kid->child;
113 object_unparent(OBJECT(dev));
115 if (bus->parent) {
116 QLIST_REMOVE(bus, sibling);
117 bus->parent->num_child_bus--;
118 bus->parent = NULL;
119 } else {
120 assert(bus != sysbus_get_default()); /* main_system_bus is never freed */
121 qemu_unregister_reset(qbus_reset_all_fn, bus);
125 void qbus_create_inplace(void *bus, size_t size, const char *typename,
126 DeviceState *parent, const char *name)
128 object_initialize(bus, size, typename);
129 qbus_realize(bus, parent, name);
132 BusState *qbus_create(const char *typename, DeviceState *parent, const char *name)
134 BusState *bus;
136 bus = BUS(object_new(typename));
137 qbus_realize(bus, parent, name);
139 return bus;
142 static bool bus_get_realized(Object *obj, Error **errp)
144 BusState *bus = BUS(obj);
146 return bus->realized;
149 static void bus_set_realized(Object *obj, bool value, Error **errp)
151 BusState *bus = BUS(obj);
152 BusClass *bc = BUS_GET_CLASS(bus);
153 BusChild *kid;
154 Error *local_err = NULL;
156 if (value && !bus->realized) {
157 if (bc->realize) {
158 bc->realize(bus, &local_err);
161 /* TODO: recursive realization */
162 } else if (!value && bus->realized) {
163 QTAILQ_FOREACH(kid, &bus->children, sibling) {
164 DeviceState *dev = kid->child;
165 object_property_set_bool(OBJECT(dev), false, "realized",
166 &local_err);
167 if (local_err != NULL) {
168 break;
171 if (bc->unrealize && local_err == NULL) {
172 bc->unrealize(bus, &local_err);
176 if (local_err != NULL) {
177 error_propagate(errp, local_err);
178 return;
181 bus->realized = value;
184 static void qbus_initfn(Object *obj)
186 BusState *bus = BUS(obj);
188 QTAILQ_INIT(&bus->children);
189 object_property_add_link(obj, QDEV_HOTPLUG_HANDLER_PROPERTY,
190 TYPE_HOTPLUG_HANDLER,
191 (Object **)&bus->hotplug_handler,
192 object_property_allow_set_link,
194 NULL);
195 object_property_add_bool(obj, "realized",
196 bus_get_realized, bus_set_realized, NULL);
199 static char *default_bus_get_fw_dev_path(DeviceState *dev)
201 return g_strdup(object_get_typename(OBJECT(dev)));
204 static void bus_class_init(ObjectClass *class, void *data)
206 BusClass *bc = BUS_CLASS(class);
208 class->unparent = bus_unparent;
209 bc->get_fw_dev_path = default_bus_get_fw_dev_path;
212 static void qbus_finalize(Object *obj)
214 BusState *bus = BUS(obj);
216 g_free(bus->name);
219 static const TypeInfo bus_info = {
220 .name = TYPE_BUS,
221 .parent = TYPE_OBJECT,
222 .instance_size = sizeof(BusState),
223 .abstract = true,
224 .class_size = sizeof(BusClass),
225 .instance_init = qbus_initfn,
226 .instance_finalize = qbus_finalize,
227 .class_init = bus_class_init,
230 static void bus_register_types(void)
232 type_register_static(&bus_info);
235 type_init(bus_register_types)