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