xref: /qemu/hw/core/qdev.c (revision 494c271784a5e360523e874be9f67259932ea68c)
1 /*
2  *  Dynamic device configuration and creation.
3  *
4  *  Copyright (c) 2009 CodeSourcery
5  *
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.
10  *
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.
15  *
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/>.
18  */
19 
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.  */
27 
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 
36 int qdev_hotplug = 0;
37 static bool qdev_hot_added = false;
38 static bool qdev_hot_removed = false;
39 
40 const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
41 {
42     DeviceClass *dc = DEVICE_GET_CLASS(dev);
43     return dc->vmsd;
44 }
45 
46 const char *qdev_fw_name(DeviceState *dev)
47 {
48     DeviceClass *dc = DEVICE_GET_CLASS(dev);
49 
50     if (dc->fw_name) {
51         return dc->fw_name;
52     }
53 
54     return object_get_typename(OBJECT(dev));
55 }
56 
57 static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
58                                      Error **errp);
59 
60 static void bus_remove_child(BusState *bus, DeviceState *child)
61 {
62     BusChild *kid;
63 
64     QTAILQ_FOREACH(kid, &bus->children, sibling) {
65         if (kid->child == child) {
66             char name[32];
67 
68             snprintf(name, sizeof(name), "child[%d]", kid->index);
69             QTAILQ_REMOVE(&bus->children, kid, sibling);
70 
71             /* This gives back ownership of kid->child back to us.  */
72             object_property_del(OBJECT(bus), name, NULL);
73             object_unref(OBJECT(kid->child));
74             g_free(kid);
75             return;
76         }
77     }
78 }
79 
80 static void bus_add_child(BusState *bus, DeviceState *child)
81 {
82     char name[32];
83     BusChild *kid = g_malloc0(sizeof(*kid));
84 
85     if (qdev_hotplug) {
86         assert(bus->allow_hotplug);
87     }
88 
89     kid->index = bus->max_index++;
90     kid->child = child;
91     object_ref(OBJECT(kid->child));
92 
93     QTAILQ_INSERT_HEAD(&bus->children, kid, sibling);
94 
95     /* This transfers ownership of kid->child to the property.  */
96     snprintf(name, sizeof(name), "child[%d]", kid->index);
97     object_property_add_link(OBJECT(bus), name,
98                              object_get_typename(OBJECT(child)),
99                              (Object **)&kid->child,
100                              NULL);
101 }
102 
103 void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
104 {
105     dev->parent_bus = bus;
106     object_ref(OBJECT(bus));
107     bus_add_child(bus, dev);
108 }
109 
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)
114 {
115     DeviceState *dev;
116 
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);
124         }
125         abort();
126     }
127 
128     return dev;
129 }
130 
131 DeviceState *qdev_try_create(BusState *bus, const char *type)
132 {
133     DeviceState *dev;
134 
135     if (object_class_by_name(type) == NULL) {
136         return NULL;
137     }
138     dev = DEVICE(object_new(type));
139     if (!dev) {
140         return NULL;
141     }
142 
143     if (!bus) {
144         bus = sysbus_get_default();
145     }
146 
147     qdev_set_parent_bus(dev, bus);
148     object_unref(OBJECT(dev));
149     return dev;
150 }
151 
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)
158 {
159     Error *local_err = NULL;
160 
161     assert(!dev->realized);
162 
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;
169     }
170     return 0;
171 }
172 
173 static void device_realize(DeviceState *dev, Error **err)
174 {
175     DeviceClass *dc = DEVICE_GET_CLASS(dev);
176 
177     if (dc->init) {
178         int rc = dc->init(dev);
179         if (rc < 0) {
180             error_setg(err, "Device initialization failed.");
181             return;
182         }
183     }
184 }
185 
186 static void device_unrealize(DeviceState *dev, Error **errp)
187 {
188     DeviceClass *dc = DEVICE_GET_CLASS(dev);
189 
190     if (dc->exit) {
191         int rc = dc->exit(dev);
192         if (rc < 0) {
193             error_setg(errp, "Device exit failed.");
194             return;
195         }
196     }
197 }
198 
199 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
200                                  int required_for_version)
201 {
202     assert(!dev->realized);
203     dev->instance_id_alias = alias_id;
204     dev->alias_required_for_version = required_for_version;
205 }
206 
207 void qdev_unplug(DeviceState *dev, Error **errp)
208 {
209     DeviceClass *dc = DEVICE_GET_CLASS(dev);
210 
211     if (dev->parent_bus && !dev->parent_bus->allow_hotplug) {
212         error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
213         return;
214     }
215     assert(dc->unplug != NULL);
216 
217     qdev_hot_removed = true;
218 
219     if (dc->unplug(dev) < 0) {
220         error_set(errp, QERR_UNDEFINED_ERROR);
221         return;
222     }
223 }
224 
225 static int qdev_reset_one(DeviceState *dev, void *opaque)
226 {
227     device_reset(dev);
228 
229     return 0;
230 }
231 
232 static int qbus_reset_one(BusState *bus, void *opaque)
233 {
234     BusClass *bc = BUS_GET_CLASS(bus);
235     if (bc->reset) {
236         return bc->reset(bus);
237     }
238     return 0;
239 }
240 
241 void qdev_reset_all(DeviceState *dev)
242 {
243     qdev_walk_children(dev, qdev_reset_one, qbus_reset_one, NULL);
244 }
245 
246 void qbus_reset_all(BusState *bus)
247 {
248     qbus_walk_children(bus, qdev_reset_one, qbus_reset_one, NULL);
249 }
250 
251 void qbus_reset_all_fn(void *opaque)
252 {
253     BusState *bus = opaque;
254     qbus_reset_all(bus);
255 }
256 
257 /* can be used as ->unplug() callback for the simple cases */
258 int qdev_simple_unplug_cb(DeviceState *dev)
259 {
260     /* just zap it */
261     object_unparent(OBJECT(dev));
262     return 0;
263 }
264 
265 
266 /* Like qdev_init(), but terminate program via error_report() instead of
267    returning an error value.  This is okay during machine creation.
268    Don't use for hotplug, because there callers need to recover from
269    failure.  Exception: if you know the device's init() callback can't
270    fail, then qdev_init_nofail() can't fail either, and is therefore
271    usable even then.  But relying on the device implementation that
272    way is somewhat unclean, and best avoided.  */
273 void qdev_init_nofail(DeviceState *dev)
274 {
275     const char *typename = object_get_typename(OBJECT(dev));
276 
277     if (qdev_init(dev) < 0) {
278         error_report("Initialization of device %s failed", typename);
279         exit(1);
280     }
281 }
282 
283 void qdev_machine_creation_done(void)
284 {
285     /*
286      * ok, initial machine setup is done, starting from now we can
287      * only create hotpluggable devices
288      */
289     qdev_hotplug = 1;
290 }
291 
292 bool qdev_machine_modified(void)
293 {
294     return qdev_hot_added || qdev_hot_removed;
295 }
296 
297 BusState *qdev_get_parent_bus(DeviceState *dev)
298 {
299     return dev->parent_bus;
300 }
301 
302 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
303 {
304     dev->gpio_in = qemu_extend_irqs(dev->gpio_in, dev->num_gpio_in, handler,
305                                         dev, n);
306     dev->num_gpio_in += n;
307 }
308 
309 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
310 {
311     assert(dev->num_gpio_out == 0);
312     dev->num_gpio_out = n;
313     dev->gpio_out = pins;
314 }
315 
316 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
317 {
318     assert(n >= 0 && n < dev->num_gpio_in);
319     return dev->gpio_in[n];
320 }
321 
322 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
323 {
324     assert(n >= 0 && n < dev->num_gpio_out);
325     dev->gpio_out[n] = pin;
326 }
327 
328 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
329 {
330     BusState *bus;
331 
332     QLIST_FOREACH(bus, &dev->child_bus, sibling) {
333         if (strcmp(name, bus->name) == 0) {
334             return bus;
335         }
336     }
337     return NULL;
338 }
339 
340 int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
341                        qbus_walkerfn *busfn, void *opaque)
342 {
343     BusChild *kid;
344     int err;
345 
346     if (busfn) {
347         err = busfn(bus, opaque);
348         if (err) {
349             return err;
350         }
351     }
352 
353     QTAILQ_FOREACH(kid, &bus->children, sibling) {
354         err = qdev_walk_children(kid->child, devfn, busfn, opaque);
355         if (err < 0) {
356             return err;
357         }
358     }
359 
360     return 0;
361 }
362 
363 int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
364                        qbus_walkerfn *busfn, void *opaque)
365 {
366     BusState *bus;
367     int err;
368 
369     if (devfn) {
370         err = devfn(dev, opaque);
371         if (err) {
372             return err;
373         }
374     }
375 
376     QLIST_FOREACH(bus, &dev->child_bus, sibling) {
377         err = qbus_walk_children(bus, devfn, busfn, opaque);
378         if (err < 0) {
379             return err;
380         }
381     }
382 
383     return 0;
384 }
385 
386 DeviceState *qdev_find_recursive(BusState *bus, const char *id)
387 {
388     BusChild *kid;
389     DeviceState *ret;
390     BusState *child;
391 
392     QTAILQ_FOREACH(kid, &bus->children, sibling) {
393         DeviceState *dev = kid->child;
394 
395         if (dev->id && strcmp(dev->id, id) == 0) {
396             return dev;
397         }
398 
399         QLIST_FOREACH(child, &dev->child_bus, sibling) {
400             ret = qdev_find_recursive(child, id);
401             if (ret) {
402                 return ret;
403             }
404         }
405     }
406     return NULL;
407 }
408 
409 static void qbus_realize(BusState *bus, DeviceState *parent, const char *name)
410 {
411     const char *typename = object_get_typename(OBJECT(bus));
412     char *buf;
413     int i,len;
414 
415     bus->parent = parent;
416 
417     if (name) {
418         bus->name = g_strdup(name);
419     } else if (bus->parent && bus->parent->id) {
420         /* parent device has id -> use it for bus name */
421         len = strlen(bus->parent->id) + 16;
422         buf = g_malloc(len);
423         snprintf(buf, len, "%s.%d", bus->parent->id, bus->parent->num_child_bus);
424         bus->name = buf;
425     } else {
426         /* no id -> use lowercase bus type for bus name */
427         len = strlen(typename) + 16;
428         buf = g_malloc(len);
429         len = snprintf(buf, len, "%s.%d", typename,
430                        bus->parent ? bus->parent->num_child_bus : 0);
431         for (i = 0; i < len; i++)
432             buf[i] = qemu_tolower(buf[i]);
433         bus->name = buf;
434     }
435 
436     if (bus->parent) {
437         QLIST_INSERT_HEAD(&bus->parent->child_bus, bus, sibling);
438         bus->parent->num_child_bus++;
439         object_property_add_child(OBJECT(bus->parent), bus->name, OBJECT(bus), NULL);
440         object_unref(OBJECT(bus));
441     } else if (bus != sysbus_get_default()) {
442         /* TODO: once all bus devices are qdevified,
443            only reset handler for main_system_bus should be registered here. */
444         qemu_register_reset(qbus_reset_all_fn, bus);
445     }
446 }
447 
448 static void bus_unparent(Object *obj)
449 {
450     BusState *bus = BUS(obj);
451     BusChild *kid;
452 
453     while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
454         DeviceState *dev = kid->child;
455         object_unparent(OBJECT(dev));
456     }
457     if (bus->parent) {
458         QLIST_REMOVE(bus, sibling);
459         bus->parent->num_child_bus--;
460         bus->parent = NULL;
461     } else {
462         assert(bus != sysbus_get_default()); /* main_system_bus is never freed */
463         qemu_unregister_reset(qbus_reset_all_fn, bus);
464     }
465 }
466 
467 void qbus_create_inplace(void *bus, size_t size, const char *typename,
468                          DeviceState *parent, const char *name)
469 {
470     object_initialize(bus, size, typename);
471     qbus_realize(bus, parent, name);
472 }
473 
474 BusState *qbus_create(const char *typename, DeviceState *parent, const char *name)
475 {
476     BusState *bus;
477 
478     bus = BUS(object_new(typename));
479     qbus_realize(bus, parent, name);
480 
481     return bus;
482 }
483 
484 static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
485 {
486     BusClass *bc = BUS_GET_CLASS(bus);
487 
488     if (bc->get_fw_dev_path) {
489         return bc->get_fw_dev_path(dev);
490     }
491 
492     return NULL;
493 }
494 
495 static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
496 {
497     int l = 0;
498 
499     if (dev && dev->parent_bus) {
500         char *d;
501         l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
502         d = bus_get_fw_dev_path(dev->parent_bus, dev);
503         if (d) {
504             l += snprintf(p + l, size - l, "%s", d);
505             g_free(d);
506         } else {
507             return l;
508         }
509     }
510     l += snprintf(p + l , size - l, "/");
511 
512     return l;
513 }
514 
515 char* qdev_get_fw_dev_path(DeviceState *dev)
516 {
517     char path[128];
518     int l;
519 
520     l = qdev_get_fw_dev_path_helper(dev, path, 128);
521 
522     path[l-1] = '\0';
523 
524     return g_strdup(path);
525 }
526 
527 char *qdev_get_dev_path(DeviceState *dev)
528 {
529     BusClass *bc;
530 
531     if (!dev || !dev->parent_bus) {
532         return NULL;
533     }
534 
535     bc = BUS_GET_CLASS(dev->parent_bus);
536     if (bc->get_dev_path) {
537         return bc->get_dev_path(dev);
538     }
539 
540     return NULL;
541 }
542 
543 /**
544  * Legacy property handling
545  */
546 
547 static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque,
548                                      const char *name, Error **errp)
549 {
550     DeviceState *dev = DEVICE(obj);
551     Property *prop = opaque;
552 
553     char buffer[1024];
554     char *ptr = buffer;
555 
556     prop->info->print(dev, prop, buffer, sizeof(buffer));
557     visit_type_str(v, &ptr, name, errp);
558 }
559 
560 static void qdev_set_legacy_property(Object *obj, Visitor *v, void *opaque,
561                                      const char *name, Error **errp)
562 {
563     DeviceState *dev = DEVICE(obj);
564     Property *prop = opaque;
565     Error *local_err = NULL;
566     char *ptr = NULL;
567     int ret;
568 
569     if (dev->realized) {
570         qdev_prop_set_after_realize(dev, name, errp);
571         return;
572     }
573 
574     visit_type_str(v, &ptr, name, &local_err);
575     if (local_err) {
576         error_propagate(errp, local_err);
577         return;
578     }
579 
580     ret = prop->info->parse(dev, prop, ptr);
581     error_set_from_qdev_prop_error(errp, ret, dev, prop, ptr);
582     g_free(ptr);
583 }
584 
585 /**
586  * @qdev_add_legacy_property - adds a legacy property
587  *
588  * Do not use this is new code!  Properties added through this interface will
589  * be given names and types in the "legacy" namespace.
590  *
591  * Legacy properties are string versions of other OOM properties.  The format
592  * of the string depends on the property type.
593  */
594 void qdev_property_add_legacy(DeviceState *dev, Property *prop,
595                               Error **errp)
596 {
597     gchar *name, *type;
598 
599     /* Register pointer properties as legacy properties */
600     if (!prop->info->print && !prop->info->parse &&
601         (prop->info->set || prop->info->get)) {
602         return;
603     }
604 
605     name = g_strdup_printf("legacy-%s", prop->name);
606     type = g_strdup_printf("legacy<%s>",
607                            prop->info->legacy_name ?: prop->info->name);
608 
609     object_property_add(OBJECT(dev), name, type,
610                         prop->info->print ? qdev_get_legacy_property : prop->info->get,
611                         prop->info->parse ? qdev_set_legacy_property : prop->info->set,
612                         NULL,
613                         prop, errp);
614 
615     g_free(type);
616     g_free(name);
617 }
618 
619 /**
620  * @qdev_property_add_static - add a @Property to a device.
621  *
622  * Static properties access data in a struct.  The actual type of the
623  * property and the field depends on the property type.
624  */
625 void qdev_property_add_static(DeviceState *dev, Property *prop,
626                               Error **errp)
627 {
628     Error *local_err = NULL;
629     Object *obj = OBJECT(dev);
630 
631     /*
632      * TODO qdev_prop_ptr does not have getters or setters.  It must
633      * go now that it can be replaced with links.  The test should be
634      * removed along with it: all static properties are read/write.
635      */
636     if (!prop->info->get && !prop->info->set) {
637         return;
638     }
639 
640     object_property_add(obj, prop->name, prop->info->name,
641                         prop->info->get, prop->info->set,
642                         prop->info->release,
643                         prop, &local_err);
644 
645     if (local_err) {
646         error_propagate(errp, local_err);
647         return;
648     }
649     if (prop->qtype == QTYPE_NONE) {
650         return;
651     }
652 
653     if (prop->qtype == QTYPE_QBOOL) {
654         object_property_set_bool(obj, prop->defval, prop->name, &local_err);
655     } else if (prop->info->enum_table) {
656         object_property_set_str(obj, prop->info->enum_table[prop->defval],
657                                 prop->name, &local_err);
658     } else if (prop->qtype == QTYPE_QINT) {
659         object_property_set_int(obj, prop->defval, prop->name, &local_err);
660     }
661     assert_no_error(local_err);
662 }
663 
664 static bool device_get_realized(Object *obj, Error **err)
665 {
666     DeviceState *dev = DEVICE(obj);
667     return dev->realized;
668 }
669 
670 static void device_set_realized(Object *obj, bool value, Error **err)
671 {
672     DeviceState *dev = DEVICE(obj);
673     DeviceClass *dc = DEVICE_GET_CLASS(dev);
674     Error *local_err = NULL;
675 
676     if (value && !dev->realized) {
677         if (!obj->parent && local_err == NULL) {
678             static int unattached_count;
679             gchar *name = g_strdup_printf("device[%d]", unattached_count++);
680 
681             object_property_add_child(container_get(qdev_get_machine(),
682                                                     "/unattached"),
683                                       name, obj, &local_err);
684             g_free(name);
685         }
686 
687         if (dc->realize) {
688             dc->realize(dev, &local_err);
689         }
690 
691         if (qdev_get_vmsd(dev) && local_err == NULL) {
692             vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
693                                            dev->instance_id_alias,
694                                            dev->alias_required_for_version);
695         }
696         if (dev->hotplugged && local_err == NULL) {
697             device_reset(dev);
698         }
699     } else if (!value && dev->realized) {
700         if (qdev_get_vmsd(dev)) {
701             vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
702         }
703         if (dc->unrealize) {
704             dc->unrealize(dev, &local_err);
705         }
706     }
707 
708     if (local_err != NULL) {
709         error_propagate(err, local_err);
710         return;
711     }
712 
713     dev->realized = value;
714 }
715 
716 static void device_initfn(Object *obj)
717 {
718     DeviceState *dev = DEVICE(obj);
719     ObjectClass *class;
720     Property *prop;
721     Error *err = NULL;
722 
723     if (qdev_hotplug) {
724         dev->hotplugged = 1;
725         qdev_hot_added = true;
726     }
727 
728     dev->instance_id_alias = -1;
729     dev->realized = false;
730 
731     object_property_add_bool(obj, "realized",
732                              device_get_realized, device_set_realized, NULL);
733 
734     class = object_get_class(OBJECT(dev));
735     do {
736         for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
737             qdev_property_add_legacy(dev, prop, &err);
738             assert_no_error(err);
739             qdev_property_add_static(dev, prop, &err);
740             assert_no_error(err);
741         }
742         class = object_class_get_parent(class);
743     } while (class != object_class_by_name(TYPE_DEVICE));
744     if (err != NULL) {
745         qerror_report_err(err);
746         error_free(err);
747         exit(1);
748     }
749 
750     object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
751                              (Object **)&dev->parent_bus, &err);
752     assert_no_error(err);
753 }
754 
755 static void device_post_init(Object *obj)
756 {
757     DeviceState *dev = DEVICE(obj);
758     Error *err = NULL;
759 
760     qdev_prop_set_globals(dev, &err);
761     assert_no_error(err);
762 }
763 
764 /* Unlink device from bus and free the structure.  */
765 static void device_finalize(Object *obj)
766 {
767     DeviceState *dev = DEVICE(obj);
768     if (dev->opts) {
769         qemu_opts_del(dev->opts);
770     }
771 }
772 
773 static void device_class_base_init(ObjectClass *class, void *data)
774 {
775     DeviceClass *klass = DEVICE_CLASS(class);
776 
777     /* We explicitly look up properties in the superclasses,
778      * so do not propagate them to the subclasses.
779      */
780     klass->props = NULL;
781 }
782 
783 static void device_unparent(Object *obj)
784 {
785     DeviceState *dev = DEVICE(obj);
786     BusState *bus;
787     QObject *event_data;
788     bool have_realized = dev->realized;
789 
790     while (dev->num_child_bus) {
791         bus = QLIST_FIRST(&dev->child_bus);
792         object_unparent(OBJECT(bus));
793     }
794     if (dev->realized) {
795         object_property_set_bool(obj, false, "realized", NULL);
796     }
797     if (dev->parent_bus) {
798         bus_remove_child(dev->parent_bus, dev);
799         object_unref(OBJECT(dev->parent_bus));
800         dev->parent_bus = NULL;
801     }
802 
803     /* Only send event if the device had been completely realized */
804     if (have_realized) {
805         gchar *path = object_get_canonical_path(OBJECT(dev));
806 
807         if (dev->id) {
808             event_data = qobject_from_jsonf("{ 'device': %s, 'path': %s }",
809                                             dev->id, path);
810         } else {
811             event_data = qobject_from_jsonf("{ 'path': %s }", path);
812         }
813         monitor_protocol_event(QEVENT_DEVICE_DELETED, event_data);
814         qobject_decref(event_data);
815         g_free(path);
816     }
817 }
818 
819 static void device_class_init(ObjectClass *class, void *data)
820 {
821     DeviceClass *dc = DEVICE_CLASS(class);
822 
823     class->unparent = device_unparent;
824     dc->realize = device_realize;
825     dc->unrealize = device_unrealize;
826 }
827 
828 void device_reset(DeviceState *dev)
829 {
830     DeviceClass *klass = DEVICE_GET_CLASS(dev);
831 
832     if (klass->reset) {
833         klass->reset(dev);
834     }
835 }
836 
837 Object *qdev_get_machine(void)
838 {
839     static Object *dev;
840 
841     if (dev == NULL) {
842         dev = container_get(object_get_root(), "/machine");
843     }
844 
845     return dev;
846 }
847 
848 static const TypeInfo device_type_info = {
849     .name = TYPE_DEVICE,
850     .parent = TYPE_OBJECT,
851     .instance_size = sizeof(DeviceState),
852     .instance_init = device_initfn,
853     .instance_post_init = device_post_init,
854     .instance_finalize = device_finalize,
855     .class_base_init = device_class_base_init,
856     .class_init = device_class_init,
857     .abstract = true,
858     .class_size = sizeof(DeviceClass),
859 };
860 
861 static void qbus_initfn(Object *obj)
862 {
863     BusState *bus = BUS(obj);
864 
865     QTAILQ_INIT(&bus->children);
866 }
867 
868 static char *default_bus_get_fw_dev_path(DeviceState *dev)
869 {
870     return g_strdup(object_get_typename(OBJECT(dev)));
871 }
872 
873 static void bus_class_init(ObjectClass *class, void *data)
874 {
875     BusClass *bc = BUS_CLASS(class);
876 
877     class->unparent = bus_unparent;
878     bc->get_fw_dev_path = default_bus_get_fw_dev_path;
879 }
880 
881 static void qbus_finalize(Object *obj)
882 {
883     BusState *bus = BUS(obj);
884 
885     g_free((char *)bus->name);
886 }
887 
888 static const TypeInfo bus_info = {
889     .name = TYPE_BUS,
890     .parent = TYPE_OBJECT,
891     .instance_size = sizeof(BusState),
892     .abstract = true,
893     .class_size = sizeof(BusClass),
894     .instance_init = qbus_initfn,
895     .instance_finalize = qbus_finalize,
896     .class_init = bus_class_init,
897 };
898 
899 static void qdev_register_types(void)
900 {
901     type_register_static(&bus_info);
902     type_register_static(&device_type_info);
903 }
904 
905 type_init(qdev_register_types)
906