qom: Make QOM link property unref optional
[qemu/kevin.git] / hw / core / qdev.c
bloba182917222fcffd4a3ea2dcca702560fe94a37e3
1 /*
2 * Dynamic device configuration and creation.
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 /* The theory here is that it should be possible to create a machine without
21 knowledge of specific devices. Historically board init routines have
22 passed a bunch of arguments to each device, requiring the board know
23 exactly which device it is dealing with. This file provides an abstract
24 API for device configuration and initialization. Devices will generally
25 inherit from a particular bus (e.g. PCI or I2C) rather than
26 this API directly. */
28 #include "hw/qdev.h"
29 #include "sysemu/sysemu.h"
30 #include "qapi/error.h"
31 #include "qapi/qmp/qerror.h"
32 #include "qapi/visitor.h"
33 #include "qapi/qmp/qjson.h"
34 #include "monitor/monitor.h"
35 #include "hw/hotplug.h"
37 int qdev_hotplug = 0;
38 static bool qdev_hot_added = false;
39 static bool qdev_hot_removed = false;
41 const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
43 DeviceClass *dc = DEVICE_GET_CLASS(dev);
44 return dc->vmsd;
47 const char *qdev_fw_name(DeviceState *dev)
49 DeviceClass *dc = DEVICE_GET_CLASS(dev);
51 if (dc->fw_name) {
52 return dc->fw_name;
55 return object_get_typename(OBJECT(dev));
58 static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
59 Error **errp);
61 static void bus_remove_child(BusState *bus, DeviceState *child)
63 BusChild *kid;
65 QTAILQ_FOREACH(kid, &bus->children, sibling) {
66 if (kid->child == child) {
67 char name[32];
69 snprintf(name, sizeof(name), "child[%d]", kid->index);
70 QTAILQ_REMOVE(&bus->children, kid, sibling);
72 /* This gives back ownership of kid->child back to us. */
73 object_property_del(OBJECT(bus), name, NULL);
74 object_unref(OBJECT(kid->child));
75 g_free(kid);
76 return;
81 static void bus_add_child(BusState *bus, DeviceState *child)
83 char name[32];
84 BusChild *kid = g_malloc0(sizeof(*kid));
86 if (qdev_hotplug) {
87 assert(bus->allow_hotplug);
90 kid->index = bus->max_index++;
91 kid->child = child;
92 object_ref(OBJECT(kid->child));
94 QTAILQ_INSERT_HEAD(&bus->children, kid, sibling);
96 /* This transfers ownership of kid->child to the property. */
97 snprintf(name, sizeof(name), "child[%d]", kid->index);
98 object_property_add_link(OBJECT(bus), name,
99 object_get_typename(OBJECT(child)),
100 (Object **)&kid->child, 0, NULL);
103 void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
105 dev->parent_bus = bus;
106 object_ref(OBJECT(bus));
107 bus_add_child(bus, dev);
110 /* Create a new device. This only initializes the device state structure
111 and allows properties to be set. qdev_init should be called to
112 initialize the actual device emulation. */
113 DeviceState *qdev_create(BusState *bus, const char *name)
115 DeviceState *dev;
117 dev = qdev_try_create(bus, name);
118 if (!dev) {
119 if (bus) {
120 error_report("Unknown device '%s' for bus '%s'", name,
121 object_get_typename(OBJECT(bus)));
122 } else {
123 error_report("Unknown device '%s' for default sysbus", name);
125 abort();
128 return dev;
131 DeviceState *qdev_try_create(BusState *bus, const char *type)
133 DeviceState *dev;
135 if (object_class_by_name(type) == NULL) {
136 return NULL;
138 dev = DEVICE(object_new(type));
139 if (!dev) {
140 return NULL;
143 if (!bus) {
144 bus = sysbus_get_default();
147 qdev_set_parent_bus(dev, bus);
148 object_unref(OBJECT(dev));
149 return dev;
152 /* Initialize a device. Device properties should be set before calling
153 this function. IRQs and MMIO regions should be connected/mapped after
154 calling this function.
155 On failure, destroy the device and return negative value.
156 Return 0 on success. */
157 int qdev_init(DeviceState *dev)
159 Error *local_err = NULL;
161 assert(!dev->realized);
163 object_property_set_bool(OBJECT(dev), true, "realized", &local_err);
164 if (local_err != NULL) {
165 qerror_report_err(local_err);
166 error_free(local_err);
167 object_unparent(OBJECT(dev));
168 return -1;
170 return 0;
173 static void device_realize(DeviceState *dev, Error **err)
175 DeviceClass *dc = DEVICE_GET_CLASS(dev);
177 if (dc->init) {
178 int rc = dc->init(dev);
179 if (rc < 0) {
180 error_setg(err, "Device initialization failed.");
181 return;
186 static void device_unrealize(DeviceState *dev, Error **errp)
188 DeviceClass *dc = DEVICE_GET_CLASS(dev);
190 if (dc->exit) {
191 int rc = dc->exit(dev);
192 if (rc < 0) {
193 error_setg(errp, "Device exit failed.");
194 return;
199 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
200 int required_for_version)
202 assert(!dev->realized);
203 dev->instance_id_alias = alias_id;
204 dev->alias_required_for_version = required_for_version;
207 void qdev_unplug(DeviceState *dev, Error **errp)
209 DeviceClass *dc = DEVICE_GET_CLASS(dev);
211 if (dev->parent_bus && !dev->parent_bus->allow_hotplug) {
212 error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
213 return;
216 if (!dc->hotpluggable) {
217 error_set(errp, QERR_DEVICE_NO_HOTPLUG,
218 object_get_typename(OBJECT(dev)));
219 return;
222 qdev_hot_removed = true;
224 if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
225 hotplug_handler_unplug(dev->parent_bus->hotplug_handler, dev, errp);
226 } else {
227 assert(dc->unplug != NULL);
228 if (dc->unplug(dev) < 0) { /* legacy handler */
229 error_set(errp, QERR_UNDEFINED_ERROR);
234 static int qdev_reset_one(DeviceState *dev, void *opaque)
236 device_reset(dev);
238 return 0;
241 static int qbus_reset_one(BusState *bus, void *opaque)
243 BusClass *bc = BUS_GET_CLASS(bus);
244 if (bc->reset) {
245 bc->reset(bus);
247 return 0;
250 void qdev_reset_all(DeviceState *dev)
252 qdev_walk_children(dev, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
255 void qbus_reset_all(BusState *bus)
257 qbus_walk_children(bus, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
260 void qbus_reset_all_fn(void *opaque)
262 BusState *bus = opaque;
263 qbus_reset_all(bus);
266 /* can be used as ->unplug() callback for the simple cases */
267 int qdev_simple_unplug_cb(DeviceState *dev)
269 /* just zap it */
270 object_unparent(OBJECT(dev));
271 return 0;
275 /* Like qdev_init(), but terminate program via error_report() instead of
276 returning an error value. This is okay during machine creation.
277 Don't use for hotplug, because there callers need to recover from
278 failure. Exception: if you know the device's init() callback can't
279 fail, then qdev_init_nofail() can't fail either, and is therefore
280 usable even then. But relying on the device implementation that
281 way is somewhat unclean, and best avoided. */
282 void qdev_init_nofail(DeviceState *dev)
284 const char *typename = object_get_typename(OBJECT(dev));
286 if (qdev_init(dev) < 0) {
287 error_report("Initialization of device %s failed", typename);
288 exit(1);
292 void qdev_machine_creation_done(void)
295 * ok, initial machine setup is done, starting from now we can
296 * only create hotpluggable devices
298 qdev_hotplug = 1;
301 bool qdev_machine_modified(void)
303 return qdev_hot_added || qdev_hot_removed;
306 BusState *qdev_get_parent_bus(DeviceState *dev)
308 return dev->parent_bus;
311 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
313 dev->gpio_in = qemu_extend_irqs(dev->gpio_in, dev->num_gpio_in, handler,
314 dev, n);
315 dev->num_gpio_in += n;
318 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
320 assert(dev->num_gpio_out == 0);
321 dev->num_gpio_out = n;
322 dev->gpio_out = pins;
325 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
327 assert(n >= 0 && n < dev->num_gpio_in);
328 return dev->gpio_in[n];
331 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
333 assert(n >= 0 && n < dev->num_gpio_out);
334 dev->gpio_out[n] = pin;
337 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
339 BusState *bus;
341 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
342 if (strcmp(name, bus->name) == 0) {
343 return bus;
346 return NULL;
349 int qbus_walk_children(BusState *bus,
350 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
351 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
352 void *opaque)
354 BusChild *kid;
355 int err;
357 if (pre_busfn) {
358 err = pre_busfn(bus, opaque);
359 if (err) {
360 return err;
364 QTAILQ_FOREACH(kid, &bus->children, sibling) {
365 err = qdev_walk_children(kid->child,
366 pre_devfn, pre_busfn,
367 post_devfn, post_busfn, opaque);
368 if (err < 0) {
369 return err;
373 if (post_busfn) {
374 err = post_busfn(bus, opaque);
375 if (err) {
376 return err;
380 return 0;
383 int qdev_walk_children(DeviceState *dev,
384 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
385 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
386 void *opaque)
388 BusState *bus;
389 int err;
391 if (pre_devfn) {
392 err = pre_devfn(dev, opaque);
393 if (err) {
394 return err;
398 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
399 err = qbus_walk_children(bus, pre_devfn, pre_busfn,
400 post_devfn, post_busfn, opaque);
401 if (err < 0) {
402 return err;
406 if (post_devfn) {
407 err = post_devfn(dev, opaque);
408 if (err) {
409 return err;
413 return 0;
416 DeviceState *qdev_find_recursive(BusState *bus, const char *id)
418 BusChild *kid;
419 DeviceState *ret;
420 BusState *child;
422 QTAILQ_FOREACH(kid, &bus->children, sibling) {
423 DeviceState *dev = kid->child;
425 if (dev->id && strcmp(dev->id, id) == 0) {
426 return dev;
429 QLIST_FOREACH(child, &dev->child_bus, sibling) {
430 ret = qdev_find_recursive(child, id);
431 if (ret) {
432 return ret;
436 return NULL;
439 static void qbus_realize(BusState *bus, DeviceState *parent, const char *name)
441 const char *typename = object_get_typename(OBJECT(bus));
442 BusClass *bc;
443 char *buf;
444 int i, len, bus_id;
446 bus->parent = parent;
448 if (name) {
449 bus->name = g_strdup(name);
450 } else if (bus->parent && bus->parent->id) {
451 /* parent device has id -> use it plus parent-bus-id for bus name */
452 bus_id = bus->parent->num_child_bus;
454 len = strlen(bus->parent->id) + 16;
455 buf = g_malloc(len);
456 snprintf(buf, len, "%s.%d", bus->parent->id, bus_id);
457 bus->name = buf;
458 } else {
459 /* no id -> use lowercase bus type plus global bus-id for bus name */
460 bc = BUS_GET_CLASS(bus);
461 bus_id = bc->automatic_ids++;
463 len = strlen(typename) + 16;
464 buf = g_malloc(len);
465 len = snprintf(buf, len, "%s.%d", typename, bus_id);
466 for (i = 0; i < len; i++) {
467 buf[i] = qemu_tolower(buf[i]);
469 bus->name = buf;
472 if (bus->parent) {
473 QLIST_INSERT_HEAD(&bus->parent->child_bus, bus, sibling);
474 bus->parent->num_child_bus++;
475 object_property_add_child(OBJECT(bus->parent), bus->name, OBJECT(bus), NULL);
476 object_unref(OBJECT(bus));
477 } else if (bus != sysbus_get_default()) {
478 /* TODO: once all bus devices are qdevified,
479 only reset handler for main_system_bus should be registered here. */
480 qemu_register_reset(qbus_reset_all_fn, bus);
484 static void bus_unparent(Object *obj)
486 BusState *bus = BUS(obj);
487 BusChild *kid;
489 while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
490 DeviceState *dev = kid->child;
491 object_unparent(OBJECT(dev));
493 if (bus->parent) {
494 QLIST_REMOVE(bus, sibling);
495 bus->parent->num_child_bus--;
496 bus->parent = NULL;
497 } else {
498 assert(bus != sysbus_get_default()); /* main_system_bus is never freed */
499 qemu_unregister_reset(qbus_reset_all_fn, bus);
503 static bool bus_get_realized(Object *obj, Error **err)
505 BusState *bus = BUS(obj);
507 return bus->realized;
510 static void bus_set_realized(Object *obj, bool value, Error **err)
512 BusState *bus = BUS(obj);
513 BusClass *bc = BUS_GET_CLASS(bus);
514 Error *local_err = NULL;
516 if (value && !bus->realized) {
517 if (bc->realize) {
518 bc->realize(bus, &local_err);
520 if (local_err != NULL) {
521 goto error;
525 } else if (!value && bus->realized) {
526 if (bc->unrealize) {
527 bc->unrealize(bus, &local_err);
529 if (local_err != NULL) {
530 goto error;
535 bus->realized = value;
536 return;
538 error:
539 error_propagate(err, local_err);
542 void qbus_create_inplace(void *bus, size_t size, const char *typename,
543 DeviceState *parent, const char *name)
545 object_initialize(bus, size, typename);
546 qbus_realize(bus, parent, name);
549 BusState *qbus_create(const char *typename, DeviceState *parent, const char *name)
551 BusState *bus;
553 bus = BUS(object_new(typename));
554 qbus_realize(bus, parent, name);
556 return bus;
559 static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
561 BusClass *bc = BUS_GET_CLASS(bus);
563 if (bc->get_fw_dev_path) {
564 return bc->get_fw_dev_path(dev);
567 return NULL;
570 static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
572 int l = 0;
574 if (dev && dev->parent_bus) {
575 char *d;
576 l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
577 d = bus_get_fw_dev_path(dev->parent_bus, dev);
578 if (d) {
579 l += snprintf(p + l, size - l, "%s", d);
580 g_free(d);
581 } else {
582 return l;
585 l += snprintf(p + l , size - l, "/");
587 return l;
590 char* qdev_get_fw_dev_path(DeviceState *dev)
592 char path[128];
593 int l;
595 l = qdev_get_fw_dev_path_helper(dev, path, 128);
597 path[l-1] = '\0';
599 return g_strdup(path);
602 char *qdev_get_dev_path(DeviceState *dev)
604 BusClass *bc;
606 if (!dev || !dev->parent_bus) {
607 return NULL;
610 bc = BUS_GET_CLASS(dev->parent_bus);
611 if (bc->get_dev_path) {
612 return bc->get_dev_path(dev);
615 return NULL;
619 * Legacy property handling
622 static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque,
623 const char *name, Error **errp)
625 DeviceState *dev = DEVICE(obj);
626 Property *prop = opaque;
628 char buffer[1024];
629 char *ptr = buffer;
631 prop->info->print(dev, prop, buffer, sizeof(buffer));
632 visit_type_str(v, &ptr, name, errp);
636 * @qdev_add_legacy_property - adds a legacy property
638 * Do not use this is new code! Properties added through this interface will
639 * be given names and types in the "legacy" namespace.
641 * Legacy properties are string versions of other OOM properties. The format
642 * of the string depends on the property type.
644 void qdev_property_add_legacy(DeviceState *dev, Property *prop,
645 Error **errp)
647 gchar *name;
649 /* Register pointer properties as legacy properties */
650 if (!prop->info->print && prop->info->get) {
651 return;
654 name = g_strdup_printf("legacy-%s", prop->name);
655 object_property_add(OBJECT(dev), name, "str",
656 prop->info->print ? qdev_get_legacy_property : prop->info->get,
657 NULL,
658 NULL,
659 prop, errp);
661 g_free(name);
665 * @qdev_property_add_static - add a @Property to a device.
667 * Static properties access data in a struct. The actual type of the
668 * property and the field depends on the property type.
670 void qdev_property_add_static(DeviceState *dev, Property *prop,
671 Error **errp)
673 Error *local_err = NULL;
674 Object *obj = OBJECT(dev);
677 * TODO qdev_prop_ptr does not have getters or setters. It must
678 * go now that it can be replaced with links. The test should be
679 * removed along with it: all static properties are read/write.
681 if (!prop->info->get && !prop->info->set) {
682 return;
685 object_property_add(obj, prop->name, prop->info->name,
686 prop->info->get, prop->info->set,
687 prop->info->release,
688 prop, &local_err);
690 if (local_err) {
691 error_propagate(errp, local_err);
692 return;
694 if (prop->qtype == QTYPE_NONE) {
695 return;
698 if (prop->qtype == QTYPE_QBOOL) {
699 object_property_set_bool(obj, prop->defval, prop->name, &error_abort);
700 } else if (prop->info->enum_table) {
701 object_property_set_str(obj, prop->info->enum_table[prop->defval],
702 prop->name, &error_abort);
703 } else if (prop->qtype == QTYPE_QINT) {
704 object_property_set_int(obj, prop->defval, prop->name, &error_abort);
708 static bool device_get_realized(Object *obj, Error **err)
710 DeviceState *dev = DEVICE(obj);
711 return dev->realized;
714 static void device_set_realized(Object *obj, bool value, Error **err)
716 DeviceState *dev = DEVICE(obj);
717 DeviceClass *dc = DEVICE_GET_CLASS(dev);
718 BusState *bus;
719 Error *local_err = NULL;
721 if (dev->hotplugged && !dc->hotpluggable) {
722 error_set(err, QERR_DEVICE_NO_HOTPLUG, object_get_typename(obj));
723 return;
726 if (value && !dev->realized) {
727 if (!obj->parent && local_err == NULL) {
728 static int unattached_count;
729 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
731 object_property_add_child(container_get(qdev_get_machine(),
732 "/unattached"),
733 name, obj, &local_err);
734 g_free(name);
737 if (dc->realize) {
738 dc->realize(dev, &local_err);
741 if (dev->parent_bus && dev->parent_bus->hotplug_handler &&
742 local_err == NULL) {
743 hotplug_handler_plug(dev->parent_bus->hotplug_handler,
744 dev, &local_err);
747 if (qdev_get_vmsd(dev) && local_err == NULL) {
748 vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
749 dev->instance_id_alias,
750 dev->alias_required_for_version);
752 if (local_err == NULL) {
753 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
754 object_property_set_bool(OBJECT(bus), true, "realized",
755 &local_err);
756 if (local_err != NULL) {
757 break;
761 if (dev->hotplugged && local_err == NULL) {
762 device_reset(dev);
764 } else if (!value && dev->realized) {
765 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
766 object_property_set_bool(OBJECT(bus), false, "realized",
767 &local_err);
768 if (local_err != NULL) {
769 break;
772 if (qdev_get_vmsd(dev) && local_err == NULL) {
773 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
775 if (dc->unrealize && local_err == NULL) {
776 dc->unrealize(dev, &local_err);
780 if (local_err != NULL) {
781 error_propagate(err, local_err);
782 return;
785 dev->realized = value;
788 static bool device_get_hotpluggable(Object *obj, Error **err)
790 DeviceClass *dc = DEVICE_GET_CLASS(obj);
791 DeviceState *dev = DEVICE(obj);
793 return dc->hotpluggable && (dev->parent_bus == NULL ||
794 dev->parent_bus->allow_hotplug);
797 static void device_initfn(Object *obj)
799 DeviceState *dev = DEVICE(obj);
800 ObjectClass *class;
801 Property *prop;
803 if (qdev_hotplug) {
804 dev->hotplugged = 1;
805 qdev_hot_added = true;
808 dev->instance_id_alias = -1;
809 dev->realized = false;
811 object_property_add_bool(obj, "realized",
812 device_get_realized, device_set_realized, NULL);
813 object_property_add_bool(obj, "hotpluggable",
814 device_get_hotpluggable, NULL, NULL);
816 class = object_get_class(OBJECT(dev));
817 do {
818 for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
819 qdev_property_add_legacy(dev, prop, &error_abort);
820 qdev_property_add_static(dev, prop, &error_abort);
822 class = object_class_get_parent(class);
823 } while (class != object_class_by_name(TYPE_DEVICE));
825 object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
826 (Object **)&dev->parent_bus, 0,
827 &error_abort);
830 static void device_post_init(Object *obj)
832 qdev_prop_set_globals(DEVICE(obj), &error_abort);
835 /* Unlink device from bus and free the structure. */
836 static void device_finalize(Object *obj)
838 DeviceState *dev = DEVICE(obj);
839 if (dev->opts) {
840 qemu_opts_del(dev->opts);
844 static void device_class_base_init(ObjectClass *class, void *data)
846 DeviceClass *klass = DEVICE_CLASS(class);
848 /* We explicitly look up properties in the superclasses,
849 * so do not propagate them to the subclasses.
851 klass->props = NULL;
854 static void device_unparent(Object *obj)
856 DeviceState *dev = DEVICE(obj);
857 BusState *bus;
858 QObject *event_data;
859 bool have_realized = dev->realized;
861 if (dev->realized) {
862 object_property_set_bool(obj, false, "realized", NULL);
864 while (dev->num_child_bus) {
865 bus = QLIST_FIRST(&dev->child_bus);
866 object_unparent(OBJECT(bus));
868 if (dev->parent_bus) {
869 bus_remove_child(dev->parent_bus, dev);
870 object_unref(OBJECT(dev->parent_bus));
871 dev->parent_bus = NULL;
874 /* Only send event if the device had been completely realized */
875 if (have_realized) {
876 gchar *path = object_get_canonical_path(OBJECT(dev));
878 if (dev->id) {
879 event_data = qobject_from_jsonf("{ 'device': %s, 'path': %s }",
880 dev->id, path);
881 } else {
882 event_data = qobject_from_jsonf("{ 'path': %s }", path);
884 monitor_protocol_event(QEVENT_DEVICE_DELETED, event_data);
885 qobject_decref(event_data);
886 g_free(path);
890 static void device_class_init(ObjectClass *class, void *data)
892 DeviceClass *dc = DEVICE_CLASS(class);
894 class->unparent = device_unparent;
895 dc->realize = device_realize;
896 dc->unrealize = device_unrealize;
898 /* by default all devices were considered as hotpluggable,
899 * so with intent to check it in generic qdev_unplug() /
900 * device_set_realized() functions make every device
901 * hotpluggable. Devices that shouldn't be hotpluggable,
902 * should override it in their class_init()
904 dc->hotpluggable = true;
907 void device_reset(DeviceState *dev)
909 DeviceClass *klass = DEVICE_GET_CLASS(dev);
911 if (klass->reset) {
912 klass->reset(dev);
916 Object *qdev_get_machine(void)
918 static Object *dev;
920 if (dev == NULL) {
921 dev = container_get(object_get_root(), "/machine");
924 return dev;
927 static const TypeInfo device_type_info = {
928 .name = TYPE_DEVICE,
929 .parent = TYPE_OBJECT,
930 .instance_size = sizeof(DeviceState),
931 .instance_init = device_initfn,
932 .instance_post_init = device_post_init,
933 .instance_finalize = device_finalize,
934 .class_base_init = device_class_base_init,
935 .class_init = device_class_init,
936 .abstract = true,
937 .class_size = sizeof(DeviceClass),
940 static void qbus_initfn(Object *obj)
942 BusState *bus = BUS(obj);
944 QTAILQ_INIT(&bus->children);
945 object_property_add_link(obj, QDEV_HOTPLUG_HANDLER_PROPERTY,
946 TYPE_HOTPLUG_HANDLER,
947 (Object **)&bus->hotplug_handler,
948 OBJ_PROP_LINK_UNREF_ON_RELEASE,
949 NULL);
950 object_property_add_bool(obj, "realized",
951 bus_get_realized, bus_set_realized, NULL);
954 static char *default_bus_get_fw_dev_path(DeviceState *dev)
956 return g_strdup(object_get_typename(OBJECT(dev)));
959 static void bus_class_init(ObjectClass *class, void *data)
961 BusClass *bc = BUS_CLASS(class);
963 class->unparent = bus_unparent;
964 bc->get_fw_dev_path = default_bus_get_fw_dev_path;
967 static void qbus_finalize(Object *obj)
969 BusState *bus = BUS(obj);
971 g_free((char *)bus->name);
974 static const TypeInfo bus_info = {
975 .name = TYPE_BUS,
976 .parent = TYPE_OBJECT,
977 .instance_size = sizeof(BusState),
978 .abstract = true,
979 .class_size = sizeof(BusClass),
980 .instance_init = qbus_initfn,
981 .instance_finalize = qbus_finalize,
982 .class_init = bus_class_init,
985 static void qdev_register_types(void)
987 type_register_static(&bus_info);
988 type_register_static(&device_type_info);
991 type_init(qdev_register_types)