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
33 static int qdev_hotplug = 0;
34 static bool qdev_hot_added = false;
35 static bool qdev_hot_removed = false;
37 /* This is a nasty hack to allow passing a NULL bus to qdev_create. */
38 static BusState *main_system_bus;
39 static void main_system_bus_create(void);
41 DeviceInfo *device_info_list;
43 static BusState *qbus_find_recursive(BusState *bus, const char *name,
45 static BusState *qbus_find(const char *path);
47 /* Register a new device type. */
48 static void qdev_subclass_init(ObjectClass *klass, void *data)
50 DeviceClass *dc = DEVICE_CLASS(klass);
51 DeviceInfo *info = data;
53 dc->fw_name = info->fw_name;
54 dc->alias = info->alias;
55 dc->desc = info->desc;
56 dc->props = info->props;
57 dc->no_user = info->no_user;
59 dc->reset = info->reset;
61 dc->vmsd = info->vmsd;
63 dc->init = info->init;
64 dc->unplug = info->unplug;
65 dc->exit = info->exit;
66 dc->bus_info = info->bus_info;
68 if (info->class_init) {
69 info->class_init(klass, data);
73 const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
75 DeviceClass *dc = DEVICE_GET_CLASS(dev);
79 BusInfo *qdev_get_bus_info(DeviceState *dev)
81 DeviceClass *dc = DEVICE_GET_CLASS(dev);
85 Property *qdev_get_props(DeviceState *dev)
87 DeviceClass *dc = DEVICE_GET_CLASS(dev);
91 const char *qdev_fw_name(DeviceState *dev)
93 DeviceClass *dc = DEVICE_GET_CLASS(dev);
97 } else if (dc->alias) {
101 return object_get_typename(OBJECT(dev));
104 void qdev_register_subclass(DeviceInfo *info, const char *parent)
106 TypeInfo type_info = {};
108 assert(info->size >= sizeof(DeviceState));
111 type_info.name = info->name;
112 type_info.parent = parent;
113 type_info.instance_size = info->size;
114 type_info.class_init = qdev_subclass_init;
115 type_info.class_data = info;
117 type_register_static(&type_info);
119 info->next = device_info_list;
120 device_info_list = info;
123 void qdev_register(DeviceInfo *info)
125 qdev_register_subclass(info, TYPE_DEVICE);
128 static DeviceInfo *qdev_find_info(BusInfo *bus_info, const char *name)
132 /* first check device names */
133 for (info = device_info_list; info != NULL; info = info->next) {
134 if (bus_info && info->bus_info != bus_info)
136 if (strcmp(info->name, name) != 0)
141 /* failing that check the aliases */
142 for (info = device_info_list; info != NULL; info = info->next) {
143 if (bus_info && info->bus_info != bus_info)
147 if (strcmp(info->alias, name) != 0)
154 bool qdev_exists(const char *name)
156 return !!qdev_find_info(NULL, name);
159 static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
162 static DeviceState *qdev_create_from_info(BusState *bus, DeviceInfo *info)
167 assert(bus->info == info->bus_info);
168 dev = DEVICE(object_new(info->name));
169 dev->parent_bus = bus;
170 qdev_prop_set_defaults(dev, qdev_get_props(dev));
171 qdev_prop_set_defaults(dev, dev->parent_bus->info->props);
172 qdev_prop_set_globals(dev);
173 QTAILQ_INSERT_HEAD(&bus->children, dev, sibling);
175 assert(bus->allow_hotplug);
177 qdev_hot_added = true;
179 dev->instance_id_alias = -1;
180 QTAILQ_INIT(&dev->properties);
181 dev->state = DEV_STATE_CREATED;
183 for (prop = qdev_get_props(dev); prop && prop->name; prop++) {
184 qdev_property_add_legacy(dev, prop, NULL);
185 qdev_property_add_static(dev, prop, NULL);
188 for (prop = qdev_get_bus_info(dev)->props; prop && prop->name; prop++) {
189 qdev_property_add_legacy(dev, prop, NULL);
190 qdev_property_add_static(dev, prop, NULL);
193 qdev_property_add_str(dev, "type", qdev_get_type, NULL, NULL);
198 /* Create a new device. This only initializes the device state structure
199 and allows properties to be set. qdev_init should be called to
200 initialize the actual device emulation. */
201 DeviceState *qdev_create(BusState *bus, const char *name)
205 dev = qdev_try_create(bus, name);
208 hw_error("Unknown device '%s' for bus '%s'\n", name,
211 hw_error("Unknown device '%s' for default sysbus\n", name);
218 DeviceState *qdev_try_create(BusState *bus, const char *name)
223 bus = sysbus_get_default();
226 info = qdev_find_info(bus->info, name);
231 return qdev_create_from_info(bus, info);
234 static void qdev_print_devinfo(DeviceInfo *info)
236 error_printf("name \"%s\", bus %s",
237 info->name, info->bus_info->name);
239 error_printf(", alias \"%s\"", info->alias);
242 error_printf(", desc \"%s\"", info->desc);
245 error_printf(", no-user");
250 static int set_property(const char *name, const char *value, void *opaque)
252 DeviceState *dev = opaque;
254 if (strcmp(name, "driver") == 0)
256 if (strcmp(name, "bus") == 0)
259 if (qdev_prop_parse(dev, name, value) == -1) {
265 int qdev_device_help(QemuOpts *opts)
271 driver = qemu_opt_get(opts, "driver");
272 if (driver && !strcmp(driver, "?")) {
273 for (info = device_info_list; info != NULL; info = info->next) {
275 continue; /* not available, don't show */
277 qdev_print_devinfo(info);
282 if (!driver || !qemu_opt_get(opts, "?")) {
286 info = qdev_find_info(NULL, driver);
291 for (prop = info->props; prop && prop->name; prop++) {
293 * TODO Properties without a parser are just for dirty hacks.
294 * qdev_prop_ptr is the only such PropertyInfo. It's marked
295 * for removal. This conditional should be removed along with
298 if (!prop->info->parse) {
299 continue; /* no way to set it, don't show */
301 error_printf("%s.%s=%s\n", info->name, prop->name,
302 prop->info->legacy_name ?: prop->info->name);
304 for (prop = info->bus_info->props; prop && prop->name; prop++) {
305 if (!prop->info->parse) {
306 continue; /* no way to set it, don't show */
308 error_printf("%s.%s=%s\n", info->name, prop->name,
309 prop->info->legacy_name ?: prop->info->name);
314 static DeviceState *qdev_get_peripheral(void)
316 static DeviceState *dev;
319 dev = qdev_create(NULL, "container");
320 qdev_property_add_child(qdev_get_root(), "peripheral", dev, NULL);
321 qdev_init_nofail(dev);
327 static DeviceState *qdev_get_peripheral_anon(void)
329 static DeviceState *dev;
332 dev = qdev_create(NULL, "container");
333 qdev_property_add_child(qdev_get_root(), "peripheral-anon", dev, NULL);
334 qdev_init_nofail(dev);
340 DeviceState *qdev_device_add(QemuOpts *opts)
342 const char *driver, *path, *id;
347 driver = qemu_opt_get(opts, "driver");
349 qerror_report(QERR_MISSING_PARAMETER, "driver");
354 info = qdev_find_info(NULL, driver);
355 if (!info || info->no_user) {
356 qerror_report(QERR_INVALID_PARAMETER_VALUE, "driver", "a driver name");
357 error_printf_unless_qmp("Try with argument '?' for a list.\n");
362 path = qemu_opt_get(opts, "bus");
364 bus = qbus_find(path);
368 if (bus->info != info->bus_info) {
369 qerror_report(QERR_BAD_BUS_FOR_DEVICE,
370 driver, bus->info->name);
374 bus = qbus_find_recursive(main_system_bus, NULL, info->bus_info);
376 qerror_report(QERR_NO_BUS_FOR_DEVICE,
377 info->name, info->bus_info->name);
381 if (qdev_hotplug && !bus->allow_hotplug) {
382 qerror_report(QERR_BUS_NO_HOTPLUG, bus->name);
386 /* create device, set properties */
387 qdev = qdev_create_from_info(bus, info);
388 id = qemu_opts_id(opts);
391 qdev_property_add_child(qdev_get_peripheral(), qdev->id, qdev, NULL);
393 static int anon_count;
394 gchar *name = g_strdup_printf("device[%d]", anon_count++);
395 qdev_property_add_child(qdev_get_peripheral_anon(), name,
399 if (qemu_opt_foreach(opts, set_property, qdev, 1) != 0) {
403 if (qdev_init(qdev) < 0) {
404 qerror_report(QERR_DEVICE_INIT_FAILED, driver);
411 /* Initialize a device. Device properties should be set before calling
412 this function. IRQs and MMIO regions should be connected/mapped after
413 calling this function.
414 On failure, destroy the device and return negative value.
415 Return 0 on success. */
416 int qdev_init(DeviceState *dev)
418 DeviceClass *dc = DEVICE_GET_CLASS(dev);
421 assert(dev->state == DEV_STATE_CREATED);
423 /* FIXME hopefully this doesn't break anything */
424 rc = dc->init(dev, NULL);
429 if (qdev_get_vmsd(dev)) {
430 vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
431 dev->instance_id_alias,
432 dev->alias_required_for_version);
434 dev->state = DEV_STATE_INITIALIZED;
435 if (dev->hotplugged) {
441 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
442 int required_for_version)
444 assert(dev->state == DEV_STATE_CREATED);
445 dev->instance_id_alias = alias_id;
446 dev->alias_required_for_version = required_for_version;
449 int qdev_unplug(DeviceState *dev)
451 DeviceClass *dc = DEVICE_GET_CLASS(dev);
453 if (!dev->parent_bus->allow_hotplug) {
454 qerror_report(QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
457 assert(dc->unplug != NULL);
459 qdev_hot_removed = true;
461 return dc->unplug(dev);
464 static int qdev_reset_one(DeviceState *dev, void *opaque)
471 BusState *sysbus_get_default(void)
473 if (!main_system_bus) {
474 main_system_bus_create();
476 return main_system_bus;
479 static int qbus_reset_one(BusState *bus, void *opaque)
481 if (bus->info->reset) {
482 return bus->info->reset(bus);
487 void qdev_reset_all(DeviceState *dev)
489 qdev_walk_children(dev, qdev_reset_one, qbus_reset_one, NULL);
492 void qbus_reset_all_fn(void *opaque)
494 BusState *bus = opaque;
495 qbus_walk_children(bus, qdev_reset_one, qbus_reset_one, NULL);
498 /* can be used as ->unplug() callback for the simple cases */
499 int qdev_simple_unplug_cb(DeviceState *dev)
507 /* Like qdev_init(), but terminate program via error_report() instead of
508 returning an error value. This is okay during machine creation.
509 Don't use for hotplug, because there callers need to recover from
510 failure. Exception: if you know the device's init() callback can't
511 fail, then qdev_init_nofail() can't fail either, and is therefore
512 usable even then. But relying on the device implementation that
513 way is somewhat unclean, and best avoided. */
514 void qdev_init_nofail(DeviceState *dev)
516 if (qdev_init(dev) < 0) {
517 error_report("Initialization of device %s failed",
518 object_get_typename(OBJECT(dev)));
523 static void qdev_property_del_all(DeviceState *dev)
525 while (!QTAILQ_EMPTY(&dev->properties)) {
526 DeviceProperty *prop = QTAILQ_FIRST(&dev->properties);
528 QTAILQ_REMOVE(&dev->properties, prop, node);
531 prop->release(dev, prop->name, prop->opaque);
540 static void qdev_property_del_child(DeviceState *dev, DeviceState *child, Error **errp)
542 DeviceProperty *prop;
544 QTAILQ_FOREACH(prop, &dev->properties, node) {
545 if (strstart(prop->type, "child<", NULL) && prop->opaque == child) {
550 g_assert(prop != NULL);
552 QTAILQ_REMOVE(&dev->properties, prop, node);
555 prop->release(dev, prop->name, prop->opaque);
563 /* Unlink device from bus and free the structure. */
564 void qdev_free(DeviceState *dev)
568 DeviceClass *dc = DEVICE_GET_CLASS(dev);
570 qdev_property_del_all(dev);
572 if (dev->state == DEV_STATE_INITIALIZED) {
573 while (dev->num_child_bus) {
574 bus = QLIST_FIRST(&dev->child_bus);
577 if (qdev_get_vmsd(dev)) {
578 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
584 qemu_opts_del(dev->opts);
587 QTAILQ_REMOVE(&dev->parent_bus->children, dev, sibling);
588 for (prop = qdev_get_props(dev); prop && prop->name; prop++) {
589 if (prop->info->free) {
590 prop->info->free(dev, prop);
594 qdev_property_del_child(dev->parent, dev, NULL);
597 qerror_report(QERR_DEVICE_IN_USE, dev->id?:"");
599 object_delete(OBJECT(dev));
602 void qdev_machine_creation_done(void)
605 * ok, initial machine setup is done, starting from now we can
606 * only create hotpluggable devices
611 bool qdev_machine_modified(void)
613 return qdev_hot_added || qdev_hot_removed;
616 /* Get a character (serial) device interface. */
617 CharDriverState *qdev_init_chardev(DeviceState *dev)
619 static int next_serial;
621 /* FIXME: This function needs to go away: use chardev properties! */
622 return serial_hds[next_serial++];
625 BusState *qdev_get_parent_bus(DeviceState *dev)
627 return dev->parent_bus;
630 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
632 assert(dev->num_gpio_in == 0);
633 dev->num_gpio_in = n;
634 dev->gpio_in = qemu_allocate_irqs(handler, dev, n);
637 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
639 assert(dev->num_gpio_out == 0);
640 dev->num_gpio_out = n;
641 dev->gpio_out = pins;
644 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
646 assert(n >= 0 && n < dev->num_gpio_in);
647 return dev->gpio_in[n];
650 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
652 assert(n >= 0 && n < dev->num_gpio_out);
653 dev->gpio_out[n] = pin;
656 void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd)
658 qdev_prop_set_macaddr(dev, "mac", nd->macaddr.a);
660 qdev_prop_set_vlan(dev, "vlan", nd->vlan);
662 qdev_prop_set_netdev(dev, "netdev", nd->netdev);
663 if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
664 qdev_prop_exists(dev, "vectors")) {
665 qdev_prop_set_uint32(dev, "vectors", nd->nvectors);
667 nd->instantiated = 1;
670 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
674 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
675 if (strcmp(name, bus->name) == 0) {
682 int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
683 qbus_walkerfn *busfn, void *opaque)
689 err = busfn(bus, opaque);
695 QTAILQ_FOREACH(dev, &bus->children, sibling) {
696 err = qdev_walk_children(dev, devfn, busfn, opaque);
705 int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
706 qbus_walkerfn *busfn, void *opaque)
712 err = devfn(dev, opaque);
718 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
719 err = qbus_walk_children(bus, devfn, busfn, opaque);
728 static BusState *qbus_find_recursive(BusState *bus, const char *name,
732 BusState *child, *ret;
735 if (name && (strcmp(bus->name, name) != 0)) {
738 if (info && (bus->info != info)) {
745 QTAILQ_FOREACH(dev, &bus->children, sibling) {
746 QLIST_FOREACH(child, &dev->child_bus, sibling) {
747 ret = qbus_find_recursive(child, name, info);
756 DeviceState *qdev_find_recursive(BusState *bus, const char *id)
758 DeviceState *dev, *ret;
761 QTAILQ_FOREACH(dev, &bus->children, sibling) {
762 if (dev->id && strcmp(dev->id, id) == 0)
764 QLIST_FOREACH(child, &dev->child_bus, sibling) {
765 ret = qdev_find_recursive(child, id);
774 static void qbus_list_bus(DeviceState *dev)
777 const char *sep = " ";
779 error_printf("child busses at \"%s\":",
780 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
781 QLIST_FOREACH(child, &dev->child_bus, sibling) {
782 error_printf("%s\"%s\"", sep, child->name);
788 static void qbus_list_dev(BusState *bus)
791 const char *sep = " ";
793 error_printf("devices at \"%s\":", bus->name);
794 QTAILQ_FOREACH(dev, &bus->children, sibling) {
795 error_printf("%s\"%s\"", sep, object_get_typename(OBJECT(dev)));
797 error_printf("/\"%s\"", dev->id);
803 static BusState *qbus_find_bus(DeviceState *dev, char *elem)
807 QLIST_FOREACH(child, &dev->child_bus, sibling) {
808 if (strcmp(child->name, elem) == 0) {
815 static DeviceState *qbus_find_dev(BusState *bus, char *elem)
820 * try to match in order:
821 * (1) instance id, if present
823 * (3) driver alias, if present
825 QTAILQ_FOREACH(dev, &bus->children, sibling) {
826 if (dev->id && strcmp(dev->id, elem) == 0) {
830 QTAILQ_FOREACH(dev, &bus->children, sibling) {
831 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
835 QTAILQ_FOREACH(dev, &bus->children, sibling) {
836 DeviceClass *dc = DEVICE_GET_CLASS(dev);
838 if (dc->alias && strcmp(dc->alias, elem) == 0) {
845 static BusState *qbus_find(const char *path)
852 /* find start element */
853 if (path[0] == '/') {
854 bus = main_system_bus;
857 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
861 bus = qbus_find_recursive(main_system_bus, elem, NULL);
863 qerror_report(QERR_BUS_NOT_FOUND, elem);
870 assert(path[pos] == '/' || !path[pos]);
871 while (path[pos] == '/') {
874 if (path[pos] == '\0') {
879 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
884 dev = qbus_find_dev(bus, elem);
886 qerror_report(QERR_DEVICE_NOT_FOUND, elem);
887 if (!monitor_cur_is_qmp()) {
893 assert(path[pos] == '/' || !path[pos]);
894 while (path[pos] == '/') {
897 if (path[pos] == '\0') {
898 /* last specified element is a device. If it has exactly
899 * one child bus accept it nevertheless */
900 switch (dev->num_child_bus) {
902 qerror_report(QERR_DEVICE_NO_BUS, elem);
905 return QLIST_FIRST(&dev->child_bus);
907 qerror_report(QERR_DEVICE_MULTIPLE_BUSSES, elem);
908 if (!monitor_cur_is_qmp()) {
916 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
921 bus = qbus_find_bus(dev, elem);
923 qerror_report(QERR_BUS_NOT_FOUND, elem);
924 if (!monitor_cur_is_qmp()) {
932 void qbus_create_inplace(BusState *bus, BusInfo *info,
933 DeviceState *parent, const char *name)
939 bus->parent = parent;
942 /* use supplied name */
943 bus->name = g_strdup(name);
944 } else if (parent && parent->id) {
945 /* parent device has id -> use it for bus name */
946 len = strlen(parent->id) + 16;
948 snprintf(buf, len, "%s.%d", parent->id, parent->num_child_bus);
951 /* no id -> use lowercase bus type for bus name */
952 len = strlen(info->name) + 16;
954 len = snprintf(buf, len, "%s.%d", info->name,
955 parent ? parent->num_child_bus : 0);
956 for (i = 0; i < len; i++)
957 buf[i] = qemu_tolower(buf[i]);
961 QTAILQ_INIT(&bus->children);
963 QLIST_INSERT_HEAD(&parent->child_bus, bus, sibling);
964 parent->num_child_bus++;
965 } else if (bus != main_system_bus) {
966 /* TODO: once all bus devices are qdevified,
967 only reset handler for main_system_bus should be registered here. */
968 qemu_register_reset(qbus_reset_all_fn, bus);
972 BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name)
976 bus = g_malloc0(info->size);
977 bus->qdev_allocated = 1;
978 qbus_create_inplace(bus, info, parent, name);
982 static void main_system_bus_create(void)
984 /* assign main_system_bus before qbus_create_inplace()
985 * in order to make "if (bus != main_system_bus)" work */
986 main_system_bus = g_malloc0(system_bus_info.size);
987 main_system_bus->qdev_allocated = 1;
988 qbus_create_inplace(main_system_bus, &system_bus_info, NULL,
992 void qbus_free(BusState *bus)
996 while ((dev = QTAILQ_FIRST(&bus->children)) != NULL) {
1000 QLIST_REMOVE(bus, sibling);
1001 bus->parent->num_child_bus--;
1003 assert(bus != main_system_bus); /* main_system_bus is never freed */
1004 qemu_unregister_reset(qbus_reset_all_fn, bus);
1006 g_free((void*)bus->name);
1007 if (bus->qdev_allocated) {
1012 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
1013 static void qbus_print(Monitor *mon, BusState *bus, int indent);
1015 static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
1016 const char *prefix, int indent)
1022 while (props->name) {
1024 * TODO Properties without a print method are just for dirty
1025 * hacks. qdev_prop_ptr is the only such PropertyInfo. It's
1026 * marked for removal. The test props->info->print should be
1027 * removed along with it.
1029 if (props->info->print) {
1030 props->info->print(dev, props, buf, sizeof(buf));
1031 qdev_printf("%s-prop: %s = %s\n", prefix, props->name, buf);
1037 static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
1040 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
1041 dev->id ? dev->id : "");
1043 if (dev->num_gpio_in) {
1044 qdev_printf("gpio-in %d\n", dev->num_gpio_in);
1046 if (dev->num_gpio_out) {
1047 qdev_printf("gpio-out %d\n", dev->num_gpio_out);
1049 qdev_print_props(mon, dev, qdev_get_props(dev), "dev", indent);
1050 qdev_print_props(mon, dev, dev->parent_bus->info->props, "bus", indent);
1051 if (dev->parent_bus->info->print_dev)
1052 dev->parent_bus->info->print_dev(mon, dev, indent);
1053 QLIST_FOREACH(child, &dev->child_bus, sibling) {
1054 qbus_print(mon, child, indent);
1058 static void qbus_print(Monitor *mon, BusState *bus, int indent)
1060 struct DeviceState *dev;
1062 qdev_printf("bus: %s\n", bus->name);
1064 qdev_printf("type %s\n", bus->info->name);
1065 QTAILQ_FOREACH(dev, &bus->children, sibling) {
1066 qdev_print(mon, dev, indent);
1071 void do_info_qtree(Monitor *mon)
1073 if (main_system_bus)
1074 qbus_print(mon, main_system_bus, 0);
1077 void do_info_qdm(Monitor *mon)
1081 for (info = device_info_list; info != NULL; info = info->next) {
1082 qdev_print_devinfo(info);
1086 int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
1090 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict);
1094 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
1095 qemu_opts_del(opts);
1098 if (!qdev_device_add(opts)) {
1099 qemu_opts_del(opts);
1105 int do_device_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
1107 const char *id = qdict_get_str(qdict, "id");
1110 dev = qdev_find_recursive(main_system_bus, id);
1112 qerror_report(QERR_DEVICE_NOT_FOUND, id);
1115 return qdev_unplug(dev);
1118 static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
1122 if (dev && dev->parent_bus) {
1124 l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
1125 if (dev->parent_bus->info->get_fw_dev_path) {
1126 d = dev->parent_bus->info->get_fw_dev_path(dev);
1127 l += snprintf(p + l, size - l, "%s", d);
1130 l += snprintf(p + l, size - l, "%s", object_get_typename(OBJECT(dev)));
1133 l += snprintf(p + l , size - l, "/");
1138 char* qdev_get_fw_dev_path(DeviceState *dev)
1143 l = qdev_get_fw_dev_path_helper(dev, path, 128);
1147 return strdup(path);
1150 char *qdev_get_type(DeviceState *dev, Error **errp)
1152 return g_strdup(object_get_typename(OBJECT(dev)));
1155 void qdev_ref(DeviceState *dev)
1160 void qdev_unref(DeviceState *dev)
1162 g_assert(dev->ref > 0);
1166 void qdev_property_add(DeviceState *dev, const char *name, const char *type,
1167 DevicePropertyAccessor *get, DevicePropertyAccessor *set,
1168 DevicePropertyRelease *release,
1169 void *opaque, Error **errp)
1171 DeviceProperty *prop = g_malloc0(sizeof(*prop));
1173 prop->name = g_strdup(name);
1174 prop->type = g_strdup(type);
1178 prop->release = release;
1179 prop->opaque = opaque;
1181 QTAILQ_INSERT_TAIL(&dev->properties, prop, node);
1184 static DeviceProperty *qdev_property_find(DeviceState *dev, const char *name)
1186 DeviceProperty *prop;
1188 QTAILQ_FOREACH(prop, &dev->properties, node) {
1189 if (strcmp(prop->name, name) == 0) {
1197 void qdev_property_get(DeviceState *dev, Visitor *v, const char *name,
1200 DeviceProperty *prop = qdev_property_find(dev, name);
1203 error_set(errp, QERR_PROPERTY_NOT_FOUND, dev->id?:"", name);
1208 error_set(errp, QERR_PERMISSION_DENIED);
1210 prop->get(dev, v, prop->opaque, name, errp);
1214 void qdev_property_set(DeviceState *dev, Visitor *v, const char *name,
1217 DeviceProperty *prop = qdev_property_find(dev, name);
1220 error_set(errp, QERR_PROPERTY_NOT_FOUND, dev->id?:"", name);
1225 error_set(errp, QERR_PERMISSION_DENIED);
1227 prop->set(dev, v, prop->opaque, name, errp);
1231 const char *qdev_property_get_type(DeviceState *dev, const char *name, Error **errp)
1233 DeviceProperty *prop = qdev_property_find(dev, name);
1236 error_set(errp, QERR_PROPERTY_NOT_FOUND, dev->id?:"", name);
1244 * Legacy property handling
1247 static void qdev_get_legacy_property(DeviceState *dev, Visitor *v, void *opaque,
1248 const char *name, Error **errp)
1250 Property *prop = opaque;
1255 prop->info->print(dev, prop, buffer, sizeof(buffer));
1256 visit_type_str(v, &ptr, name, errp);
1259 static void qdev_set_legacy_property(DeviceState *dev, Visitor *v, void *opaque,
1260 const char *name, Error **errp)
1262 Property *prop = opaque;
1263 Error *local_err = NULL;
1267 if (dev->state != DEV_STATE_CREATED) {
1268 error_set(errp, QERR_PERMISSION_DENIED);
1272 visit_type_str(v, &ptr, name, &local_err);
1274 error_propagate(errp, local_err);
1278 ret = prop->info->parse(dev, prop, ptr);
1279 error_set_from_qdev_prop_error(errp, ret, dev, prop, ptr);
1284 * @qdev_add_legacy_property - adds a legacy property
1286 * Do not use this is new code! Properties added through this interface will
1287 * be given names and types in the "legacy" namespace.
1289 * Legacy properties are always processed as strings. The format of the string
1290 * depends on the property type.
1292 void qdev_property_add_legacy(DeviceState *dev, Property *prop,
1297 name = g_strdup_printf("legacy-%s", prop->name);
1298 type = g_strdup_printf("legacy<%s>",
1299 prop->info->legacy_name ?: prop->info->name);
1301 qdev_property_add(dev, name, type,
1302 prop->info->print ? qdev_get_legacy_property : NULL,
1303 prop->info->parse ? qdev_set_legacy_property : NULL,
1312 * @qdev_property_add_static - add a @Property to a device.
1314 * Static properties access data in a struct. The actual type of the
1315 * property and the field depends on the property type.
1317 void qdev_property_add_static(DeviceState *dev, Property *prop,
1320 qdev_property_add(dev, prop->name, prop->info->name,
1321 prop->info->get, prop->info->set,
1326 DeviceState *qdev_get_root(void)
1328 static DeviceState *qdev_root;
1331 qdev_root = qdev_create(NULL, "container");
1332 qdev_init_nofail(qdev_root);
1338 static void qdev_get_child_property(DeviceState *dev, Visitor *v, void *opaque,
1339 const char *name, Error **errp)
1341 DeviceState *child = opaque;
1344 path = qdev_get_canonical_path(child);
1345 visit_type_str(v, &path, name, errp);
1349 static void qdev_release_child_property(DeviceState *dev, const char *name,
1352 DeviceState *child = opaque;
1357 void qdev_property_add_child(DeviceState *dev, const char *name,
1358 DeviceState *child, Error **errp)
1362 type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child)));
1364 qdev_property_add(dev, name, type, qdev_get_child_property,
1365 NULL, qdev_release_child_property,
1369 g_assert(child->parent == NULL);
1370 child->parent = dev;
1375 static void qdev_get_link_property(DeviceState *dev, Visitor *v, void *opaque,
1376 const char *name, Error **errp)
1378 DeviceState **child = opaque;
1382 path = qdev_get_canonical_path(*child);
1383 visit_type_str(v, &path, name, errp);
1387 visit_type_str(v, &path, name, errp);
1391 static void qdev_set_link_property(DeviceState *dev, Visitor *v, void *opaque,
1392 const char *name, Error **errp)
1394 DeviceState **child = opaque;
1395 bool ambiguous = false;
1399 type = qdev_property_get_type(dev, name, NULL);
1401 visit_type_str(v, &path, name, errp);
1407 if (strcmp(path, "") != 0) {
1408 DeviceState *target;
1410 target = qdev_resolve_path(path, &ambiguous);
1414 target_type = g_strdup_printf("link<%s>", object_get_typename(OBJECT(target)));
1415 if (strcmp(target_type, type) == 0) {
1419 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, type);
1422 g_free(target_type);
1424 error_set(errp, QERR_DEVICE_NOT_FOUND, path);
1433 void qdev_property_add_link(DeviceState *dev, const char *name,
1434 const char *type, DeviceState **child,
1439 full_type = g_strdup_printf("link<%s>", type);
1441 qdev_property_add(dev, name, full_type,
1442 qdev_get_link_property,
1443 qdev_set_link_property,
1449 gchar *qdev_get_canonical_path(DeviceState *dev)
1451 DeviceState *root = qdev_get_root();
1452 char *newpath = NULL, *path = NULL;
1454 while (dev != root) {
1455 DeviceProperty *prop = NULL;
1457 g_assert(dev->parent != NULL);
1459 QTAILQ_FOREACH(prop, &dev->parent->properties, node) {
1460 if (!strstart(prop->type, "child<", NULL)) {
1464 if (prop->opaque == dev) {
1466 newpath = g_strdup_printf("%s/%s", prop->name, path);
1470 path = g_strdup(prop->name);
1476 g_assert(prop != NULL);
1481 newpath = g_strdup_printf("/%s", path);
1487 static DeviceState *qdev_resolve_abs_path(DeviceState *parent,
1491 DeviceProperty *prop;
1494 if (parts[index] == NULL) {
1498 if (strcmp(parts[index], "") == 0) {
1499 return qdev_resolve_abs_path(parent, parts, index + 1);
1502 prop = qdev_property_find(parent, parts[index]);
1508 if (strstart(prop->type, "link<", NULL)) {
1509 DeviceState **pchild = prop->opaque;
1513 } else if (strstart(prop->type, "child<", NULL)) {
1514 child = prop->opaque;
1521 return qdev_resolve_abs_path(child, parts, index + 1);
1524 static DeviceState *qdev_resolve_partial_path(DeviceState *parent,
1529 DeviceProperty *prop;
1531 dev = qdev_resolve_abs_path(parent, parts, 0);
1533 QTAILQ_FOREACH(prop, &parent->properties, node) {
1536 if (!strstart(prop->type, "child<", NULL)) {
1540 found = qdev_resolve_partial_path(prop->opaque, parts, ambiguous);
1551 if (ambiguous && *ambiguous) {
1559 DeviceState *qdev_resolve_path(const char *path, bool *ambiguous)
1561 bool partial_path = true;
1565 parts = g_strsplit(path, "/", 0);
1566 if (parts == NULL || parts[0] == NULL) {
1568 return qdev_get_root();
1571 if (strcmp(parts[0], "") == 0) {
1572 partial_path = false;
1579 dev = qdev_resolve_partial_path(qdev_get_root(), parts, ambiguous);
1581 dev = qdev_resolve_abs_path(qdev_get_root(), parts, 1);
1589 typedef struct StringProperty
1591 char *(*get)(DeviceState *, Error **);
1592 void (*set)(DeviceState *, const char *, Error **);
1595 static void qdev_property_get_str(DeviceState *dev, Visitor *v, void *opaque,
1596 const char *name, Error **errp)
1598 StringProperty *prop = opaque;
1601 value = prop->get(dev, errp);
1603 visit_type_str(v, &value, name, errp);
1608 static void qdev_property_set_str(DeviceState *dev, Visitor *v, void *opaque,
1609 const char *name, Error **errp)
1611 StringProperty *prop = opaque;
1613 Error *local_err = NULL;
1615 visit_type_str(v, &value, name, &local_err);
1617 error_propagate(errp, local_err);
1621 prop->set(dev, value, errp);
1625 static void qdev_property_release_str(DeviceState *dev, const char *name,
1628 StringProperty *prop = opaque;
1632 void qdev_property_add_str(DeviceState *dev, const char *name,
1633 char *(*get)(DeviceState *, Error **),
1634 void (*set)(DeviceState *, const char *, Error **),
1637 StringProperty *prop = g_malloc0(sizeof(*prop));
1642 qdev_property_add(dev, name, "string",
1643 get ? qdev_property_get_str : NULL,
1644 set ? qdev_property_set_str : NULL,
1645 qdev_property_release_str,
1649 void qdev_machine_init(void)
1651 qdev_get_peripheral_anon();
1652 qdev_get_peripheral();
1655 void device_reset(DeviceState *dev)
1657 DeviceClass *klass = DEVICE_GET_CLASS(dev);
1664 static TypeInfo device_type_info = {
1665 .name = TYPE_DEVICE,
1666 .parent = TYPE_OBJECT,
1667 .instance_size = sizeof(DeviceState),
1669 .class_size = sizeof(DeviceClass),
1672 static void init_qdev(void)
1674 type_register_static(&device_type_info);
1677 device_init(init_qdev);