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