qdev: Introduce FWPathProvider interface
[qemu.git] / hw / core / qdev.c
blobcd09cd4d95a29087d6ca1dfd64badaaee4a8842f
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 "hw/fw-path-provider.h"
30 #include "sysemu/sysemu.h"
31 #include "qapi/error.h"
32 #include "qapi/qmp/qerror.h"
33 #include "qapi/visitor.h"
34 #include "qapi/qmp/qjson.h"
35 #include "monitor/monitor.h"
36 #include "hw/hotplug.h"
38 int qdev_hotplug = 0;
39 static bool qdev_hot_added = false;
40 static bool qdev_hot_removed = false;
42 const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
44 DeviceClass *dc = DEVICE_GET_CLASS(dev);
45 return dc->vmsd;
48 const char *qdev_fw_name(DeviceState *dev)
50 DeviceClass *dc = DEVICE_GET_CLASS(dev);
52 if (dc->fw_name) {
53 return dc->fw_name;
56 return object_get_typename(OBJECT(dev));
59 static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
60 Error **errp);
62 static void bus_remove_child(BusState *bus, DeviceState *child)
64 BusChild *kid;
66 QTAILQ_FOREACH(kid, &bus->children, sibling) {
67 if (kid->child == child) {
68 char name[32];
70 snprintf(name, sizeof(name), "child[%d]", kid->index);
71 QTAILQ_REMOVE(&bus->children, kid, sibling);
73 /* This gives back ownership of kid->child back to us. */
74 object_property_del(OBJECT(bus), name, NULL);
75 object_unref(OBJECT(kid->child));
76 g_free(kid);
77 return;
82 static void bus_add_child(BusState *bus, DeviceState *child)
84 char name[32];
85 BusChild *kid = g_malloc0(sizeof(*kid));
87 if (qdev_hotplug) {
88 assert(bus->allow_hotplug);
91 kid->index = bus->max_index++;
92 kid->child = child;
93 object_ref(OBJECT(kid->child));
95 QTAILQ_INSERT_HEAD(&bus->children, kid, sibling);
97 /* This transfers ownership of kid->child to the property. */
98 snprintf(name, sizeof(name), "child[%d]", kid->index);
99 object_property_add_link(OBJECT(bus), name,
100 object_get_typename(OBJECT(child)),
101 (Object **)&kid->child,
102 NULL);
105 void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
107 dev->parent_bus = bus;
108 object_ref(OBJECT(bus));
109 bus_add_child(bus, dev);
112 /* Create a new device. This only initializes the device state structure
113 and allows properties to be set. qdev_init should be called to
114 initialize the actual device emulation. */
115 DeviceState *qdev_create(BusState *bus, const char *name)
117 DeviceState *dev;
119 dev = qdev_try_create(bus, name);
120 if (!dev) {
121 if (bus) {
122 error_report("Unknown device '%s' for bus '%s'", name,
123 object_get_typename(OBJECT(bus)));
124 } else {
125 error_report("Unknown device '%s' for default sysbus", name);
127 abort();
130 return dev;
133 DeviceState *qdev_try_create(BusState *bus, const char *type)
135 DeviceState *dev;
137 if (object_class_by_name(type) == NULL) {
138 return NULL;
140 dev = DEVICE(object_new(type));
141 if (!dev) {
142 return NULL;
145 if (!bus) {
146 bus = sysbus_get_default();
149 qdev_set_parent_bus(dev, bus);
150 object_unref(OBJECT(dev));
151 return dev;
154 /* Initialize a device. Device properties should be set before calling
155 this function. IRQs and MMIO regions should be connected/mapped after
156 calling this function.
157 On failure, destroy the device and return negative value.
158 Return 0 on success. */
159 int qdev_init(DeviceState *dev)
161 Error *local_err = NULL;
163 assert(!dev->realized);
165 object_property_set_bool(OBJECT(dev), true, "realized", &local_err);
166 if (local_err != NULL) {
167 qerror_report_err(local_err);
168 error_free(local_err);
169 object_unparent(OBJECT(dev));
170 return -1;
172 return 0;
175 static void device_realize(DeviceState *dev, Error **err)
177 DeviceClass *dc = DEVICE_GET_CLASS(dev);
179 if (dc->init) {
180 int rc = dc->init(dev);
181 if (rc < 0) {
182 error_setg(err, "Device initialization failed.");
183 return;
188 static void device_unrealize(DeviceState *dev, Error **errp)
190 DeviceClass *dc = DEVICE_GET_CLASS(dev);
192 if (dc->exit) {
193 int rc = dc->exit(dev);
194 if (rc < 0) {
195 error_setg(errp, "Device exit failed.");
196 return;
201 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
202 int required_for_version)
204 assert(!dev->realized);
205 dev->instance_id_alias = alias_id;
206 dev->alias_required_for_version = required_for_version;
209 void qdev_unplug(DeviceState *dev, Error **errp)
211 DeviceClass *dc = DEVICE_GET_CLASS(dev);
213 if (dev->parent_bus && !dev->parent_bus->allow_hotplug) {
214 error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
215 return;
218 if (!dc->hotpluggable) {
219 error_set(errp, QERR_DEVICE_NO_HOTPLUG,
220 object_get_typename(OBJECT(dev)));
221 return;
224 qdev_hot_removed = true;
226 if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
227 hotplug_handler_unplug(dev->parent_bus->hotplug_handler, dev, errp);
228 } else {
229 assert(dc->unplug != NULL);
230 if (dc->unplug(dev) < 0) { /* legacy handler */
231 error_set(errp, QERR_UNDEFINED_ERROR);
236 static int qdev_reset_one(DeviceState *dev, void *opaque)
238 device_reset(dev);
240 return 0;
243 static int qbus_reset_one(BusState *bus, void *opaque)
245 BusClass *bc = BUS_GET_CLASS(bus);
246 if (bc->reset) {
247 bc->reset(bus);
249 return 0;
252 void qdev_reset_all(DeviceState *dev)
254 qdev_walk_children(dev, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
257 void qbus_reset_all(BusState *bus)
259 qbus_walk_children(bus, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
262 void qbus_reset_all_fn(void *opaque)
264 BusState *bus = opaque;
265 qbus_reset_all(bus);
268 /* can be used as ->unplug() callback for the simple cases */
269 int qdev_simple_unplug_cb(DeviceState *dev)
271 /* just zap it */
272 object_unparent(OBJECT(dev));
273 return 0;
277 /* Like qdev_init(), but terminate program via error_report() instead of
278 returning an error value. This is okay during machine creation.
279 Don't use for hotplug, because there callers need to recover from
280 failure. Exception: if you know the device's init() callback can't
281 fail, then qdev_init_nofail() can't fail either, and is therefore
282 usable even then. But relying on the device implementation that
283 way is somewhat unclean, and best avoided. */
284 void qdev_init_nofail(DeviceState *dev)
286 const char *typename = object_get_typename(OBJECT(dev));
288 if (qdev_init(dev) < 0) {
289 error_report("Initialization of device %s failed", typename);
290 exit(1);
294 void qdev_machine_creation_done(void)
297 * ok, initial machine setup is done, starting from now we can
298 * only create hotpluggable devices
300 qdev_hotplug = 1;
303 bool qdev_machine_modified(void)
305 return qdev_hot_added || qdev_hot_removed;
308 BusState *qdev_get_parent_bus(DeviceState *dev)
310 return dev->parent_bus;
313 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
315 dev->gpio_in = qemu_extend_irqs(dev->gpio_in, dev->num_gpio_in, handler,
316 dev, n);
317 dev->num_gpio_in += n;
320 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
322 assert(dev->num_gpio_out == 0);
323 dev->num_gpio_out = n;
324 dev->gpio_out = pins;
327 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
329 assert(n >= 0 && n < dev->num_gpio_in);
330 return dev->gpio_in[n];
333 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
335 assert(n >= 0 && n < dev->num_gpio_out);
336 dev->gpio_out[n] = pin;
339 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
341 BusState *bus;
343 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
344 if (strcmp(name, bus->name) == 0) {
345 return bus;
348 return NULL;
351 int qbus_walk_children(BusState *bus,
352 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
353 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
354 void *opaque)
356 BusChild *kid;
357 int err;
359 if (pre_busfn) {
360 err = pre_busfn(bus, opaque);
361 if (err) {
362 return err;
366 QTAILQ_FOREACH(kid, &bus->children, sibling) {
367 err = qdev_walk_children(kid->child,
368 pre_devfn, pre_busfn,
369 post_devfn, post_busfn, opaque);
370 if (err < 0) {
371 return err;
375 if (post_busfn) {
376 err = post_busfn(bus, opaque);
377 if (err) {
378 return err;
382 return 0;
385 int qdev_walk_children(DeviceState *dev,
386 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
387 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
388 void *opaque)
390 BusState *bus;
391 int err;
393 if (pre_devfn) {
394 err = pre_devfn(dev, opaque);
395 if (err) {
396 return err;
400 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
401 err = qbus_walk_children(bus, pre_devfn, pre_busfn,
402 post_devfn, post_busfn, opaque);
403 if (err < 0) {
404 return err;
408 if (post_devfn) {
409 err = post_devfn(dev, opaque);
410 if (err) {
411 return err;
415 return 0;
418 DeviceState *qdev_find_recursive(BusState *bus, const char *id)
420 BusChild *kid;
421 DeviceState *ret;
422 BusState *child;
424 QTAILQ_FOREACH(kid, &bus->children, sibling) {
425 DeviceState *dev = kid->child;
427 if (dev->id && strcmp(dev->id, id) == 0) {
428 return dev;
431 QLIST_FOREACH(child, &dev->child_bus, sibling) {
432 ret = qdev_find_recursive(child, id);
433 if (ret) {
434 return ret;
438 return NULL;
441 static void qbus_realize(BusState *bus, DeviceState *parent, const char *name)
443 const char *typename = object_get_typename(OBJECT(bus));
444 BusClass *bc;
445 char *buf;
446 int i, len, bus_id;
448 bus->parent = parent;
450 if (name) {
451 bus->name = g_strdup(name);
452 } else if (bus->parent && bus->parent->id) {
453 /* parent device has id -> use it plus parent-bus-id for bus name */
454 bus_id = bus->parent->num_child_bus;
456 len = strlen(bus->parent->id) + 16;
457 buf = g_malloc(len);
458 snprintf(buf, len, "%s.%d", bus->parent->id, bus_id);
459 bus->name = buf;
460 } else {
461 /* no id -> use lowercase bus type plus global bus-id for bus name */
462 bc = BUS_GET_CLASS(bus);
463 bus_id = bc->automatic_ids++;
465 len = strlen(typename) + 16;
466 buf = g_malloc(len);
467 len = snprintf(buf, len, "%s.%d", typename, bus_id);
468 for (i = 0; i < len; i++) {
469 buf[i] = qemu_tolower(buf[i]);
471 bus->name = buf;
474 if (bus->parent) {
475 QLIST_INSERT_HEAD(&bus->parent->child_bus, bus, sibling);
476 bus->parent->num_child_bus++;
477 object_property_add_child(OBJECT(bus->parent), bus->name, OBJECT(bus), NULL);
478 object_unref(OBJECT(bus));
479 } else if (bus != sysbus_get_default()) {
480 /* TODO: once all bus devices are qdevified,
481 only reset handler for main_system_bus should be registered here. */
482 qemu_register_reset(qbus_reset_all_fn, bus);
486 static void bus_unparent(Object *obj)
488 BusState *bus = BUS(obj);
489 BusChild *kid;
491 while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
492 DeviceState *dev = kid->child;
493 object_unparent(OBJECT(dev));
495 if (bus->parent) {
496 QLIST_REMOVE(bus, sibling);
497 bus->parent->num_child_bus--;
498 bus->parent = NULL;
499 } else {
500 assert(bus != sysbus_get_default()); /* main_system_bus is never freed */
501 qemu_unregister_reset(qbus_reset_all_fn, bus);
505 static bool bus_get_realized(Object *obj, Error **err)
507 BusState *bus = BUS(obj);
509 return bus->realized;
512 static void bus_set_realized(Object *obj, bool value, Error **err)
514 BusState *bus = BUS(obj);
515 BusClass *bc = BUS_GET_CLASS(bus);
516 Error *local_err = NULL;
518 if (value && !bus->realized) {
519 if (bc->realize) {
520 bc->realize(bus, &local_err);
522 if (local_err != NULL) {
523 goto error;
527 } else if (!value && bus->realized) {
528 if (bc->unrealize) {
529 bc->unrealize(bus, &local_err);
531 if (local_err != NULL) {
532 goto error;
537 bus->realized = value;
538 return;
540 error:
541 error_propagate(err, local_err);
544 void qbus_create_inplace(void *bus, size_t size, const char *typename,
545 DeviceState *parent, const char *name)
547 object_initialize(bus, size, typename);
548 qbus_realize(bus, parent, name);
551 BusState *qbus_create(const char *typename, DeviceState *parent, const char *name)
553 BusState *bus;
555 bus = BUS(object_new(typename));
556 qbus_realize(bus, parent, name);
558 return bus;
561 static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
563 BusClass *bc = BUS_GET_CLASS(bus);
565 if (bc->get_fw_dev_path) {
566 return bc->get_fw_dev_path(dev);
569 return NULL;
572 static char *qdev_get_fw_dev_path_from_handler(BusState *bus, DeviceState *dev)
574 Object *obj = OBJECT(dev);
575 char *d = NULL;
577 while (!d && obj->parent) {
578 obj = obj->parent;
579 d = fw_path_provider_try_get_dev_path(obj, bus, dev);
581 return d;
584 static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
586 int l = 0;
588 if (dev && dev->parent_bus) {
589 char *d;
590 l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
591 d = qdev_get_fw_dev_path_from_handler(dev->parent_bus, dev);
592 if (!d) {
593 d = bus_get_fw_dev_path(dev->parent_bus, dev);
595 if (d) {
596 l += snprintf(p + l, size - l, "%s", d);
597 g_free(d);
598 } else {
599 return l;
602 l += snprintf(p + l , size - l, "/");
604 return l;
607 char* qdev_get_fw_dev_path(DeviceState *dev)
609 char path[128];
610 int l;
612 l = qdev_get_fw_dev_path_helper(dev, path, 128);
614 path[l-1] = '\0';
616 return g_strdup(path);
619 char *qdev_get_dev_path(DeviceState *dev)
621 BusClass *bc;
623 if (!dev || !dev->parent_bus) {
624 return NULL;
627 bc = BUS_GET_CLASS(dev->parent_bus);
628 if (bc->get_dev_path) {
629 return bc->get_dev_path(dev);
632 return NULL;
636 * Legacy property handling
639 static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque,
640 const char *name, Error **errp)
642 DeviceState *dev = DEVICE(obj);
643 Property *prop = opaque;
645 char buffer[1024];
646 char *ptr = buffer;
648 prop->info->print(dev, prop, buffer, sizeof(buffer));
649 visit_type_str(v, &ptr, name, errp);
653 * @qdev_add_legacy_property - adds a legacy property
655 * Do not use this is new code! Properties added through this interface will
656 * be given names and types in the "legacy" namespace.
658 * Legacy properties are string versions of other OOM properties. The format
659 * of the string depends on the property type.
661 void qdev_property_add_legacy(DeviceState *dev, Property *prop,
662 Error **errp)
664 gchar *name;
666 /* Register pointer properties as legacy properties */
667 if (!prop->info->print && prop->info->get) {
668 return;
671 name = g_strdup_printf("legacy-%s", prop->name);
672 object_property_add(OBJECT(dev), name, "str",
673 prop->info->print ? qdev_get_legacy_property : prop->info->get,
674 NULL,
675 NULL,
676 prop, errp);
678 g_free(name);
682 * @qdev_property_add_static - add a @Property to a device.
684 * Static properties access data in a struct. The actual type of the
685 * property and the field depends on the property type.
687 void qdev_property_add_static(DeviceState *dev, Property *prop,
688 Error **errp)
690 Error *local_err = NULL;
691 Object *obj = OBJECT(dev);
694 * TODO qdev_prop_ptr does not have getters or setters. It must
695 * go now that it can be replaced with links. The test should be
696 * removed along with it: all static properties are read/write.
698 if (!prop->info->get && !prop->info->set) {
699 return;
702 object_property_add(obj, prop->name, prop->info->name,
703 prop->info->get, prop->info->set,
704 prop->info->release,
705 prop, &local_err);
707 if (local_err) {
708 error_propagate(errp, local_err);
709 return;
711 if (prop->qtype == QTYPE_NONE) {
712 return;
715 if (prop->qtype == QTYPE_QBOOL) {
716 object_property_set_bool(obj, prop->defval, prop->name, &error_abort);
717 } else if (prop->info->enum_table) {
718 object_property_set_str(obj, prop->info->enum_table[prop->defval],
719 prop->name, &error_abort);
720 } else if (prop->qtype == QTYPE_QINT) {
721 object_property_set_int(obj, prop->defval, prop->name, &error_abort);
725 static bool device_get_realized(Object *obj, Error **err)
727 DeviceState *dev = DEVICE(obj);
728 return dev->realized;
731 static void device_set_realized(Object *obj, bool value, Error **err)
733 DeviceState *dev = DEVICE(obj);
734 DeviceClass *dc = DEVICE_GET_CLASS(dev);
735 BusState *bus;
736 Error *local_err = NULL;
738 if (dev->hotplugged && !dc->hotpluggable) {
739 error_set(err, QERR_DEVICE_NO_HOTPLUG, object_get_typename(obj));
740 return;
743 if (value && !dev->realized) {
744 if (!obj->parent && local_err == NULL) {
745 static int unattached_count;
746 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
748 object_property_add_child(container_get(qdev_get_machine(),
749 "/unattached"),
750 name, obj, &local_err);
751 g_free(name);
754 if (dc->realize) {
755 dc->realize(dev, &local_err);
758 if (dev->parent_bus && dev->parent_bus->hotplug_handler &&
759 local_err == NULL) {
760 hotplug_handler_plug(dev->parent_bus->hotplug_handler,
761 dev, &local_err);
764 if (qdev_get_vmsd(dev) && local_err == NULL) {
765 vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
766 dev->instance_id_alias,
767 dev->alias_required_for_version);
769 if (local_err == NULL) {
770 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
771 object_property_set_bool(OBJECT(bus), true, "realized",
772 &local_err);
773 if (local_err != NULL) {
774 break;
778 if (dev->hotplugged && local_err == NULL) {
779 device_reset(dev);
781 } else if (!value && dev->realized) {
782 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
783 object_property_set_bool(OBJECT(bus), false, "realized",
784 &local_err);
785 if (local_err != NULL) {
786 break;
789 if (qdev_get_vmsd(dev) && local_err == NULL) {
790 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
792 if (dc->unrealize && local_err == NULL) {
793 dc->unrealize(dev, &local_err);
797 if (local_err != NULL) {
798 error_propagate(err, local_err);
799 return;
802 dev->realized = value;
805 static bool device_get_hotpluggable(Object *obj, Error **err)
807 DeviceClass *dc = DEVICE_GET_CLASS(obj);
808 DeviceState *dev = DEVICE(obj);
810 return dc->hotpluggable && (dev->parent_bus == NULL ||
811 dev->parent_bus->allow_hotplug);
814 static void device_initfn(Object *obj)
816 DeviceState *dev = DEVICE(obj);
817 ObjectClass *class;
818 Property *prop;
820 if (qdev_hotplug) {
821 dev->hotplugged = 1;
822 qdev_hot_added = true;
825 dev->instance_id_alias = -1;
826 dev->realized = false;
828 object_property_add_bool(obj, "realized",
829 device_get_realized, device_set_realized, NULL);
830 object_property_add_bool(obj, "hotpluggable",
831 device_get_hotpluggable, NULL, NULL);
833 class = object_get_class(OBJECT(dev));
834 do {
835 for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
836 qdev_property_add_legacy(dev, prop, &error_abort);
837 qdev_property_add_static(dev, prop, &error_abort);
839 class = object_class_get_parent(class);
840 } while (class != object_class_by_name(TYPE_DEVICE));
842 object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
843 (Object **)&dev->parent_bus, &error_abort);
846 static void device_post_init(Object *obj)
848 qdev_prop_set_globals(DEVICE(obj), &error_abort);
851 /* Unlink device from bus and free the structure. */
852 static void device_finalize(Object *obj)
854 DeviceState *dev = DEVICE(obj);
855 if (dev->opts) {
856 qemu_opts_del(dev->opts);
860 static void device_class_base_init(ObjectClass *class, void *data)
862 DeviceClass *klass = DEVICE_CLASS(class);
864 /* We explicitly look up properties in the superclasses,
865 * so do not propagate them to the subclasses.
867 klass->props = NULL;
870 static void device_unparent(Object *obj)
872 DeviceState *dev = DEVICE(obj);
873 BusState *bus;
874 QObject *event_data;
875 bool have_realized = dev->realized;
877 if (dev->realized) {
878 object_property_set_bool(obj, false, "realized", NULL);
880 while (dev->num_child_bus) {
881 bus = QLIST_FIRST(&dev->child_bus);
882 object_unparent(OBJECT(bus));
884 if (dev->parent_bus) {
885 bus_remove_child(dev->parent_bus, dev);
886 object_unref(OBJECT(dev->parent_bus));
887 dev->parent_bus = NULL;
890 /* Only send event if the device had been completely realized */
891 if (have_realized) {
892 gchar *path = object_get_canonical_path(OBJECT(dev));
894 if (dev->id) {
895 event_data = qobject_from_jsonf("{ 'device': %s, 'path': %s }",
896 dev->id, path);
897 } else {
898 event_data = qobject_from_jsonf("{ 'path': %s }", path);
900 monitor_protocol_event(QEVENT_DEVICE_DELETED, event_data);
901 qobject_decref(event_data);
902 g_free(path);
906 static void device_class_init(ObjectClass *class, void *data)
908 DeviceClass *dc = DEVICE_CLASS(class);
910 class->unparent = device_unparent;
911 dc->realize = device_realize;
912 dc->unrealize = device_unrealize;
914 /* by default all devices were considered as hotpluggable,
915 * so with intent to check it in generic qdev_unplug() /
916 * device_set_realized() functions make every device
917 * hotpluggable. Devices that shouldn't be hotpluggable,
918 * should override it in their class_init()
920 dc->hotpluggable = true;
923 void device_reset(DeviceState *dev)
925 DeviceClass *klass = DEVICE_GET_CLASS(dev);
927 if (klass->reset) {
928 klass->reset(dev);
932 Object *qdev_get_machine(void)
934 static Object *dev;
936 if (dev == NULL) {
937 dev = container_get(object_get_root(), "/machine");
940 return dev;
943 static const TypeInfo device_type_info = {
944 .name = TYPE_DEVICE,
945 .parent = TYPE_OBJECT,
946 .instance_size = sizeof(DeviceState),
947 .instance_init = device_initfn,
948 .instance_post_init = device_post_init,
949 .instance_finalize = device_finalize,
950 .class_base_init = device_class_base_init,
951 .class_init = device_class_init,
952 .abstract = true,
953 .class_size = sizeof(DeviceClass),
956 static void qbus_initfn(Object *obj)
958 BusState *bus = BUS(obj);
960 QTAILQ_INIT(&bus->children);
961 object_property_add_link(obj, QDEV_HOTPLUG_HANDLER_PROPERTY,
962 TYPE_HOTPLUG_HANDLER,
963 (Object **)&bus->hotplug_handler, NULL);
964 object_property_add_bool(obj, "realized",
965 bus_get_realized, bus_set_realized, NULL);
968 static char *default_bus_get_fw_dev_path(DeviceState *dev)
970 return g_strdup(object_get_typename(OBJECT(dev)));
973 static void bus_class_init(ObjectClass *class, void *data)
975 BusClass *bc = BUS_CLASS(class);
977 class->unparent = bus_unparent;
978 bc->get_fw_dev_path = default_bus_get_fw_dev_path;
981 static void qbus_finalize(Object *obj)
983 BusState *bus = BUS(obj);
985 g_free((char *)bus->name);
988 static const TypeInfo bus_info = {
989 .name = TYPE_BUS,
990 .parent = TYPE_OBJECT,
991 .instance_size = sizeof(BusState),
992 .abstract = true,
993 .class_size = sizeof(BusClass),
994 .instance_init = qbus_initfn,
995 .instance_finalize = qbus_finalize,
996 .class_init = bus_class_init,
999 static void qdev_register_types(void)
1001 type_register_static(&bus_info);
1002 type_register_static(&device_type_info);
1005 type_init(qdev_register_types)