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
);
53 dc
->reset
= dc
->info
->reset
;
55 /* Poison to try to detect future uses */
56 dc
->info
->reset
= NULL
;
58 if (dc
->info
->class_init
) {
59 dc
->info
->class_init(klass
, data
);
63 DeviceInfo
*qdev_get_info(DeviceState
*dev
)
65 return DEVICE_GET_CLASS(dev
)->info
;
68 void qdev_register_subclass(DeviceInfo
*info
, const char *parent
)
70 TypeInfo type_info
= {};
72 assert(info
->size
>= sizeof(DeviceState
));
75 type_info
.name
= info
->name
;
76 type_info
.parent
= parent
;
77 type_info
.instance_size
= info
->size
;
78 type_info
.class_init
= qdev_subclass_init
;
79 type_info
.class_data
= info
;
81 type_register_static(&type_info
);
83 info
->next
= device_info_list
;
84 device_info_list
= info
;
87 void qdev_register(DeviceInfo
*info
)
89 qdev_register_subclass(info
, TYPE_DEVICE
);
92 static DeviceInfo
*qdev_find_info(BusInfo
*bus_info
, const char *name
)
96 /* first check device names */
97 for (info
= device_info_list
; info
!= NULL
; info
= info
->next
) {
98 if (bus_info
&& info
->bus_info
!= bus_info
)
100 if (strcmp(info
->name
, name
) != 0)
105 /* failing that check the aliases */
106 for (info
= device_info_list
; info
!= NULL
; info
= info
->next
) {
107 if (bus_info
&& info
->bus_info
!= bus_info
)
111 if (strcmp(info
->alias
, name
) != 0)
118 bool qdev_exists(const char *name
)
120 return !!qdev_find_info(NULL
, name
);
123 static void qdev_property_add_legacy(DeviceState
*dev
, Property
*prop
,
126 static DeviceState
*qdev_create_from_info(BusState
*bus
, DeviceInfo
*info
)
131 assert(bus
->info
== info
->bus_info
);
132 dev
= DEVICE(object_new(info
->name
));
133 dev
->parent_bus
= bus
;
134 qdev_prop_set_defaults(dev
, qdev_get_info(dev
)->props
);
135 qdev_prop_set_defaults(dev
, dev
->parent_bus
->info
->props
);
136 qdev_prop_set_globals(dev
);
137 QTAILQ_INSERT_HEAD(&bus
->children
, dev
, sibling
);
139 assert(bus
->allow_hotplug
);
141 qdev_hot_added
= true;
143 dev
->instance_id_alias
= -1;
144 QTAILQ_INIT(&dev
->properties
);
145 dev
->state
= DEV_STATE_CREATED
;
147 for (prop
= qdev_get_info(dev
)->props
; prop
&& prop
->name
; prop
++) {
148 qdev_property_add_legacy(dev
, prop
, NULL
);
149 qdev_property_add_static(dev
, prop
, NULL
);
152 for (prop
= qdev_get_info(dev
)->bus_info
->props
; prop
&& prop
->name
; prop
++) {
153 qdev_property_add_legacy(dev
, prop
, NULL
);
154 qdev_property_add_static(dev
, prop
, NULL
);
157 qdev_property_add_str(dev
, "type", qdev_get_type
, NULL
, NULL
);
162 /* Create a new device. This only initializes the device state structure
163 and allows properties to be set. qdev_init should be called to
164 initialize the actual device emulation. */
165 DeviceState
*qdev_create(BusState
*bus
, const char *name
)
169 dev
= qdev_try_create(bus
, name
);
172 hw_error("Unknown device '%s' for bus '%s'\n", name
,
175 hw_error("Unknown device '%s' for default sysbus\n", name
);
182 DeviceState
*qdev_try_create(BusState
*bus
, const char *name
)
187 bus
= sysbus_get_default();
190 info
= qdev_find_info(bus
->info
, name
);
195 return qdev_create_from_info(bus
, info
);
198 static void qdev_print_devinfo(DeviceInfo
*info
)
200 error_printf("name \"%s\", bus %s",
201 info
->name
, info
->bus_info
->name
);
203 error_printf(", alias \"%s\"", info
->alias
);
206 error_printf(", desc \"%s\"", info
->desc
);
209 error_printf(", no-user");
214 static int set_property(const char *name
, const char *value
, void *opaque
)
216 DeviceState
*dev
= opaque
;
218 if (strcmp(name
, "driver") == 0)
220 if (strcmp(name
, "bus") == 0)
223 if (qdev_prop_parse(dev
, name
, value
) == -1) {
229 int qdev_device_help(QemuOpts
*opts
)
235 driver
= qemu_opt_get(opts
, "driver");
236 if (driver
&& !strcmp(driver
, "?")) {
237 for (info
= device_info_list
; info
!= NULL
; info
= info
->next
) {
239 continue; /* not available, don't show */
241 qdev_print_devinfo(info
);
246 if (!driver
|| !qemu_opt_get(opts
, "?")) {
250 info
= qdev_find_info(NULL
, driver
);
255 for (prop
= info
->props
; prop
&& prop
->name
; prop
++) {
257 * TODO Properties without a parser are just for dirty hacks.
258 * qdev_prop_ptr is the only such PropertyInfo. It's marked
259 * for removal. This conditional should be removed along with
262 if (!prop
->info
->parse
) {
263 continue; /* no way to set it, don't show */
265 error_printf("%s.%s=%s\n", info
->name
, prop
->name
,
266 prop
->info
->legacy_name
?: prop
->info
->name
);
268 for (prop
= info
->bus_info
->props
; prop
&& prop
->name
; prop
++) {
269 if (!prop
->info
->parse
) {
270 continue; /* no way to set it, don't show */
272 error_printf("%s.%s=%s\n", info
->name
, prop
->name
,
273 prop
->info
->legacy_name
?: prop
->info
->name
);
278 static DeviceState
*qdev_get_peripheral(void)
280 static DeviceState
*dev
;
283 dev
= qdev_create(NULL
, "container");
284 qdev_property_add_child(qdev_get_root(), "peripheral", dev
, NULL
);
285 qdev_init_nofail(dev
);
291 static DeviceState
*qdev_get_peripheral_anon(void)
293 static DeviceState
*dev
;
296 dev
= qdev_create(NULL
, "container");
297 qdev_property_add_child(qdev_get_root(), "peripheral-anon", dev
, NULL
);
298 qdev_init_nofail(dev
);
304 DeviceState
*qdev_device_add(QemuOpts
*opts
)
306 const char *driver
, *path
, *id
;
311 driver
= qemu_opt_get(opts
, "driver");
313 qerror_report(QERR_MISSING_PARAMETER
, "driver");
318 info
= qdev_find_info(NULL
, driver
);
319 if (!info
|| info
->no_user
) {
320 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "driver", "a driver name");
321 error_printf_unless_qmp("Try with argument '?' for a list.\n");
326 path
= qemu_opt_get(opts
, "bus");
328 bus
= qbus_find(path
);
332 if (bus
->info
!= info
->bus_info
) {
333 qerror_report(QERR_BAD_BUS_FOR_DEVICE
,
334 driver
, bus
->info
->name
);
338 bus
= qbus_find_recursive(main_system_bus
, NULL
, info
->bus_info
);
340 qerror_report(QERR_NO_BUS_FOR_DEVICE
,
341 info
->name
, info
->bus_info
->name
);
345 if (qdev_hotplug
&& !bus
->allow_hotplug
) {
346 qerror_report(QERR_BUS_NO_HOTPLUG
, bus
->name
);
350 /* create device, set properties */
351 qdev
= qdev_create_from_info(bus
, info
);
352 id
= qemu_opts_id(opts
);
355 qdev_property_add_child(qdev_get_peripheral(), qdev
->id
, qdev
, NULL
);
357 static int anon_count
;
358 gchar
*name
= g_strdup_printf("device[%d]", anon_count
++);
359 qdev_property_add_child(qdev_get_peripheral_anon(), name
,
363 if (qemu_opt_foreach(opts
, set_property
, qdev
, 1) != 0) {
367 if (qdev_init(qdev
) < 0) {
368 qerror_report(QERR_DEVICE_INIT_FAILED
, driver
);
375 /* Initialize a device. Device properties should be set before calling
376 this function. IRQs and MMIO regions should be connected/mapped after
377 calling this function.
378 On failure, destroy the device and return negative value.
379 Return 0 on success. */
380 int qdev_init(DeviceState
*dev
)
384 assert(dev
->state
== DEV_STATE_CREATED
);
385 rc
= qdev_get_info(dev
)->init(dev
, qdev_get_info(dev
));
390 if (qdev_get_info(dev
)->vmsd
) {
391 vmstate_register_with_alias_id(dev
, -1, qdev_get_info(dev
)->vmsd
, dev
,
392 dev
->instance_id_alias
,
393 dev
->alias_required_for_version
);
395 dev
->state
= DEV_STATE_INITIALIZED
;
396 if (dev
->hotplugged
) {
402 void qdev_set_legacy_instance_id(DeviceState
*dev
, int alias_id
,
403 int required_for_version
)
405 assert(dev
->state
== DEV_STATE_CREATED
);
406 dev
->instance_id_alias
= alias_id
;
407 dev
->alias_required_for_version
= required_for_version
;
410 int qdev_unplug(DeviceState
*dev
)
412 if (!dev
->parent_bus
->allow_hotplug
) {
413 qerror_report(QERR_BUS_NO_HOTPLUG
, dev
->parent_bus
->name
);
416 assert(qdev_get_info(dev
)->unplug
!= NULL
);
418 qdev_hot_removed
= true;
420 return qdev_get_info(dev
)->unplug(dev
);
423 static int qdev_reset_one(DeviceState
*dev
, void *opaque
)
430 BusState
*sysbus_get_default(void)
432 if (!main_system_bus
) {
433 main_system_bus_create();
435 return main_system_bus
;
438 static int qbus_reset_one(BusState
*bus
, void *opaque
)
440 if (bus
->info
->reset
) {
441 return bus
->info
->reset(bus
);
446 void qdev_reset_all(DeviceState
*dev
)
448 qdev_walk_children(dev
, qdev_reset_one
, qbus_reset_one
, NULL
);
451 void qbus_reset_all_fn(void *opaque
)
453 BusState
*bus
= opaque
;
454 qbus_walk_children(bus
, qdev_reset_one
, qbus_reset_one
, NULL
);
457 /* can be used as ->unplug() callback for the simple cases */
458 int qdev_simple_unplug_cb(DeviceState
*dev
)
466 /* Like qdev_init(), but terminate program via error_report() instead of
467 returning an error value. This is okay during machine creation.
468 Don't use for hotplug, because there callers need to recover from
469 failure. Exception: if you know the device's init() callback can't
470 fail, then qdev_init_nofail() can't fail either, and is therefore
471 usable even then. But relying on the device implementation that
472 way is somewhat unclean, and best avoided. */
473 void qdev_init_nofail(DeviceState
*dev
)
475 DeviceInfo
*info
= qdev_get_info(dev
);
477 if (qdev_init(dev
) < 0) {
478 error_report("Initialization of device %s failed", info
->name
);
483 static void qdev_property_del_all(DeviceState
*dev
)
485 while (!QTAILQ_EMPTY(&dev
->properties
)) {
486 DeviceProperty
*prop
= QTAILQ_FIRST(&dev
->properties
);
488 QTAILQ_REMOVE(&dev
->properties
, prop
, node
);
491 prop
->release(dev
, prop
->name
, prop
->opaque
);
500 static void qdev_property_del_child(DeviceState
*dev
, DeviceState
*child
, Error
**errp
)
502 DeviceProperty
*prop
;
504 QTAILQ_FOREACH(prop
, &dev
->properties
, node
) {
505 if (strstart(prop
->type
, "child<", NULL
) && prop
->opaque
== child
) {
510 g_assert(prop
!= NULL
);
512 QTAILQ_REMOVE(&dev
->properties
, prop
, node
);
515 prop
->release(dev
, prop
->name
, prop
->opaque
);
523 /* Unlink device from bus and free the structure. */
524 void qdev_free(DeviceState
*dev
)
529 qdev_property_del_all(dev
);
531 if (dev
->state
== DEV_STATE_INITIALIZED
) {
532 while (dev
->num_child_bus
) {
533 bus
= QLIST_FIRST(&dev
->child_bus
);
536 if (qdev_get_info(dev
)->vmsd
)
537 vmstate_unregister(dev
, qdev_get_info(dev
)->vmsd
, dev
);
538 if (qdev_get_info(dev
)->exit
)
539 qdev_get_info(dev
)->exit(dev
);
541 qemu_opts_del(dev
->opts
);
543 QTAILQ_REMOVE(&dev
->parent_bus
->children
, dev
, sibling
);
544 for (prop
= qdev_get_info(dev
)->props
; prop
&& prop
->name
; prop
++) {
545 if (prop
->info
->free
) {
546 prop
->info
->free(dev
, prop
);
550 qdev_property_del_child(dev
->parent
, dev
, NULL
);
553 qerror_report(QERR_DEVICE_IN_USE
, dev
->id
?:"");
555 object_delete(OBJECT(dev
));
558 void qdev_machine_creation_done(void)
561 * ok, initial machine setup is done, starting from now we can
562 * only create hotpluggable devices
567 bool qdev_machine_modified(void)
569 return qdev_hot_added
|| qdev_hot_removed
;
572 /* Get a character (serial) device interface. */
573 CharDriverState
*qdev_init_chardev(DeviceState
*dev
)
575 static int next_serial
;
577 /* FIXME: This function needs to go away: use chardev properties! */
578 return serial_hds
[next_serial
++];
581 BusState
*qdev_get_parent_bus(DeviceState
*dev
)
583 return dev
->parent_bus
;
586 void qdev_init_gpio_in(DeviceState
*dev
, qemu_irq_handler handler
, int n
)
588 assert(dev
->num_gpio_in
== 0);
589 dev
->num_gpio_in
= n
;
590 dev
->gpio_in
= qemu_allocate_irqs(handler
, dev
, n
);
593 void qdev_init_gpio_out(DeviceState
*dev
, qemu_irq
*pins
, int n
)
595 assert(dev
->num_gpio_out
== 0);
596 dev
->num_gpio_out
= n
;
597 dev
->gpio_out
= pins
;
600 qemu_irq
qdev_get_gpio_in(DeviceState
*dev
, int n
)
602 assert(n
>= 0 && n
< dev
->num_gpio_in
);
603 return dev
->gpio_in
[n
];
606 void qdev_connect_gpio_out(DeviceState
* dev
, int n
, qemu_irq pin
)
608 assert(n
>= 0 && n
< dev
->num_gpio_out
);
609 dev
->gpio_out
[n
] = pin
;
612 void qdev_set_nic_properties(DeviceState
*dev
, NICInfo
*nd
)
614 qdev_prop_set_macaddr(dev
, "mac", nd
->macaddr
.a
);
616 qdev_prop_set_vlan(dev
, "vlan", nd
->vlan
);
618 qdev_prop_set_netdev(dev
, "netdev", nd
->netdev
);
619 if (nd
->nvectors
!= DEV_NVECTORS_UNSPECIFIED
&&
620 qdev_prop_exists(dev
, "vectors")) {
621 qdev_prop_set_uint32(dev
, "vectors", nd
->nvectors
);
623 nd
->instantiated
= 1;
626 BusState
*qdev_get_child_bus(DeviceState
*dev
, const char *name
)
630 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
631 if (strcmp(name
, bus
->name
) == 0) {
638 int qbus_walk_children(BusState
*bus
, qdev_walkerfn
*devfn
,
639 qbus_walkerfn
*busfn
, void *opaque
)
645 err
= busfn(bus
, opaque
);
651 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
652 err
= qdev_walk_children(dev
, devfn
, busfn
, opaque
);
661 int qdev_walk_children(DeviceState
*dev
, qdev_walkerfn
*devfn
,
662 qbus_walkerfn
*busfn
, void *opaque
)
668 err
= devfn(dev
, opaque
);
674 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
675 err
= qbus_walk_children(bus
, devfn
, busfn
, opaque
);
684 static BusState
*qbus_find_recursive(BusState
*bus
, const char *name
,
688 BusState
*child
, *ret
;
691 if (name
&& (strcmp(bus
->name
, name
) != 0)) {
694 if (info
&& (bus
->info
!= info
)) {
701 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
702 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
703 ret
= qbus_find_recursive(child
, name
, info
);
712 DeviceState
*qdev_find_recursive(BusState
*bus
, const char *id
)
714 DeviceState
*dev
, *ret
;
717 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
718 if (dev
->id
&& strcmp(dev
->id
, id
) == 0)
720 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
721 ret
= qdev_find_recursive(child
, id
);
730 static void qbus_list_bus(DeviceState
*dev
)
733 const char *sep
= " ";
735 error_printf("child busses at \"%s\":",
736 dev
->id
? dev
->id
: object_get_typename(OBJECT(dev
)));
737 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
738 error_printf("%s\"%s\"", sep
, child
->name
);
744 static void qbus_list_dev(BusState
*bus
)
747 const char *sep
= " ";
749 error_printf("devices at \"%s\":", bus
->name
);
750 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
751 error_printf("%s\"%s\"", sep
, object_get_typename(OBJECT(dev
)));
753 error_printf("/\"%s\"", dev
->id
);
759 static BusState
*qbus_find_bus(DeviceState
*dev
, char *elem
)
763 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
764 if (strcmp(child
->name
, elem
) == 0) {
771 static DeviceState
*qbus_find_dev(BusState
*bus
, char *elem
)
776 * try to match in order:
777 * (1) instance id, if present
779 * (3) driver alias, if present
781 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
782 if (dev
->id
&& strcmp(dev
->id
, elem
) == 0) {
786 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
787 if (strcmp(object_get_typename(OBJECT(dev
)), elem
) == 0) {
791 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
792 if (qdev_get_info(dev
)->alias
&& strcmp(qdev_get_info(dev
)->alias
, elem
) == 0) {
799 static BusState
*qbus_find(const char *path
)
806 /* find start element */
807 if (path
[0] == '/') {
808 bus
= main_system_bus
;
811 if (sscanf(path
, "%127[^/]%n", elem
, &len
) != 1) {
815 bus
= qbus_find_recursive(main_system_bus
, elem
, NULL
);
817 qerror_report(QERR_BUS_NOT_FOUND
, elem
);
824 assert(path
[pos
] == '/' || !path
[pos
]);
825 while (path
[pos
] == '/') {
828 if (path
[pos
] == '\0') {
833 if (sscanf(path
+pos
, "%127[^/]%n", elem
, &len
) != 1) {
838 dev
= qbus_find_dev(bus
, elem
);
840 qerror_report(QERR_DEVICE_NOT_FOUND
, elem
);
841 if (!monitor_cur_is_qmp()) {
847 assert(path
[pos
] == '/' || !path
[pos
]);
848 while (path
[pos
] == '/') {
851 if (path
[pos
] == '\0') {
852 /* last specified element is a device. If it has exactly
853 * one child bus accept it nevertheless */
854 switch (dev
->num_child_bus
) {
856 qerror_report(QERR_DEVICE_NO_BUS
, elem
);
859 return QLIST_FIRST(&dev
->child_bus
);
861 qerror_report(QERR_DEVICE_MULTIPLE_BUSSES
, elem
);
862 if (!monitor_cur_is_qmp()) {
870 if (sscanf(path
+pos
, "%127[^/]%n", elem
, &len
) != 1) {
875 bus
= qbus_find_bus(dev
, elem
);
877 qerror_report(QERR_BUS_NOT_FOUND
, elem
);
878 if (!monitor_cur_is_qmp()) {
886 void qbus_create_inplace(BusState
*bus
, BusInfo
*info
,
887 DeviceState
*parent
, const char *name
)
893 bus
->parent
= parent
;
896 /* use supplied name */
897 bus
->name
= g_strdup(name
);
898 } else if (parent
&& parent
->id
) {
899 /* parent device has id -> use it for bus name */
900 len
= strlen(parent
->id
) + 16;
902 snprintf(buf
, len
, "%s.%d", parent
->id
, parent
->num_child_bus
);
905 /* no id -> use lowercase bus type for bus name */
906 len
= strlen(info
->name
) + 16;
908 len
= snprintf(buf
, len
, "%s.%d", info
->name
,
909 parent
? parent
->num_child_bus
: 0);
910 for (i
= 0; i
< len
; i
++)
911 buf
[i
] = qemu_tolower(buf
[i
]);
915 QTAILQ_INIT(&bus
->children
);
917 QLIST_INSERT_HEAD(&parent
->child_bus
, bus
, sibling
);
918 parent
->num_child_bus
++;
919 } else if (bus
!= main_system_bus
) {
920 /* TODO: once all bus devices are qdevified,
921 only reset handler for main_system_bus should be registered here. */
922 qemu_register_reset(qbus_reset_all_fn
, bus
);
926 BusState
*qbus_create(BusInfo
*info
, DeviceState
*parent
, const char *name
)
930 bus
= g_malloc0(info
->size
);
931 bus
->qdev_allocated
= 1;
932 qbus_create_inplace(bus
, info
, parent
, name
);
936 static void main_system_bus_create(void)
938 /* assign main_system_bus before qbus_create_inplace()
939 * in order to make "if (bus != main_system_bus)" work */
940 main_system_bus
= g_malloc0(system_bus_info
.size
);
941 main_system_bus
->qdev_allocated
= 1;
942 qbus_create_inplace(main_system_bus
, &system_bus_info
, NULL
,
946 void qbus_free(BusState
*bus
)
950 while ((dev
= QTAILQ_FIRST(&bus
->children
)) != NULL
) {
954 QLIST_REMOVE(bus
, sibling
);
955 bus
->parent
->num_child_bus
--;
957 assert(bus
!= main_system_bus
); /* main_system_bus is never freed */
958 qemu_unregister_reset(qbus_reset_all_fn
, bus
);
960 g_free((void*)bus
->name
);
961 if (bus
->qdev_allocated
) {
966 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
967 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
);
969 static void qdev_print_props(Monitor
*mon
, DeviceState
*dev
, Property
*props
,
970 const char *prefix
, int indent
)
976 while (props
->name
) {
978 * TODO Properties without a print method are just for dirty
979 * hacks. qdev_prop_ptr is the only such PropertyInfo. It's
980 * marked for removal. The test props->info->print should be
981 * removed along with it.
983 if (props
->info
->print
) {
984 props
->info
->print(dev
, props
, buf
, sizeof(buf
));
985 qdev_printf("%s-prop: %s = %s\n", prefix
, props
->name
, buf
);
991 static void qdev_print(Monitor
*mon
, DeviceState
*dev
, int indent
)
994 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev
)),
995 dev
->id
? dev
->id
: "");
997 if (dev
->num_gpio_in
) {
998 qdev_printf("gpio-in %d\n", dev
->num_gpio_in
);
1000 if (dev
->num_gpio_out
) {
1001 qdev_printf("gpio-out %d\n", dev
->num_gpio_out
);
1003 qdev_print_props(mon
, dev
, qdev_get_info(dev
)->props
, "dev", indent
);
1004 qdev_print_props(mon
, dev
, dev
->parent_bus
->info
->props
, "bus", indent
);
1005 if (dev
->parent_bus
->info
->print_dev
)
1006 dev
->parent_bus
->info
->print_dev(mon
, dev
, indent
);
1007 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
1008 qbus_print(mon
, child
, indent
);
1012 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
)
1014 struct DeviceState
*dev
;
1016 qdev_printf("bus: %s\n", bus
->name
);
1018 qdev_printf("type %s\n", bus
->info
->name
);
1019 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
1020 qdev_print(mon
, dev
, indent
);
1025 void do_info_qtree(Monitor
*mon
)
1027 if (main_system_bus
)
1028 qbus_print(mon
, main_system_bus
, 0);
1031 void do_info_qdm(Monitor
*mon
)
1035 for (info
= device_info_list
; info
!= NULL
; info
= info
->next
) {
1036 qdev_print_devinfo(info
);
1040 int do_device_add(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1044 opts
= qemu_opts_from_qdict(qemu_find_opts("device"), qdict
);
1048 if (!monitor_cur_is_qmp() && qdev_device_help(opts
)) {
1049 qemu_opts_del(opts
);
1052 if (!qdev_device_add(opts
)) {
1053 qemu_opts_del(opts
);
1059 int do_device_del(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1061 const char *id
= qdict_get_str(qdict
, "id");
1064 dev
= qdev_find_recursive(main_system_bus
, id
);
1066 qerror_report(QERR_DEVICE_NOT_FOUND
, id
);
1069 return qdev_unplug(dev
);
1072 static int qdev_get_fw_dev_path_helper(DeviceState
*dev
, char *p
, int size
)
1076 if (dev
&& dev
->parent_bus
) {
1078 l
= qdev_get_fw_dev_path_helper(dev
->parent_bus
->parent
, p
, size
);
1079 if (dev
->parent_bus
->info
->get_fw_dev_path
) {
1080 d
= dev
->parent_bus
->info
->get_fw_dev_path(dev
);
1081 l
+= snprintf(p
+ l
, size
- l
, "%s", d
);
1084 l
+= snprintf(p
+ l
, size
- l
, "%s", object_get_typename(OBJECT(dev
)));
1087 l
+= snprintf(p
+ l
, size
- l
, "/");
1092 char* qdev_get_fw_dev_path(DeviceState
*dev
)
1097 l
= qdev_get_fw_dev_path_helper(dev
, path
, 128);
1101 return strdup(path
);
1104 char *qdev_get_type(DeviceState
*dev
, Error
**errp
)
1106 return g_strdup(object_get_typename(OBJECT(dev
)));
1109 void qdev_ref(DeviceState
*dev
)
1114 void qdev_unref(DeviceState
*dev
)
1116 g_assert(dev
->ref
> 0);
1120 void qdev_property_add(DeviceState
*dev
, const char *name
, const char *type
,
1121 DevicePropertyAccessor
*get
, DevicePropertyAccessor
*set
,
1122 DevicePropertyRelease
*release
,
1123 void *opaque
, Error
**errp
)
1125 DeviceProperty
*prop
= g_malloc0(sizeof(*prop
));
1127 prop
->name
= g_strdup(name
);
1128 prop
->type
= g_strdup(type
);
1132 prop
->release
= release
;
1133 prop
->opaque
= opaque
;
1135 QTAILQ_INSERT_TAIL(&dev
->properties
, prop
, node
);
1138 static DeviceProperty
*qdev_property_find(DeviceState
*dev
, const char *name
)
1140 DeviceProperty
*prop
;
1142 QTAILQ_FOREACH(prop
, &dev
->properties
, node
) {
1143 if (strcmp(prop
->name
, name
) == 0) {
1151 void qdev_property_get(DeviceState
*dev
, Visitor
*v
, const char *name
,
1154 DeviceProperty
*prop
= qdev_property_find(dev
, name
);
1157 error_set(errp
, QERR_PROPERTY_NOT_FOUND
, dev
->id
?:"", name
);
1162 error_set(errp
, QERR_PERMISSION_DENIED
);
1164 prop
->get(dev
, v
, prop
->opaque
, name
, errp
);
1168 void qdev_property_set(DeviceState
*dev
, Visitor
*v
, const char *name
,
1171 DeviceProperty
*prop
= qdev_property_find(dev
, name
);
1174 error_set(errp
, QERR_PROPERTY_NOT_FOUND
, dev
->id
?:"", name
);
1179 error_set(errp
, QERR_PERMISSION_DENIED
);
1181 prop
->set(dev
, v
, prop
->opaque
, name
, errp
);
1185 const char *qdev_property_get_type(DeviceState
*dev
, const char *name
, Error
**errp
)
1187 DeviceProperty
*prop
= qdev_property_find(dev
, name
);
1190 error_set(errp
, QERR_PROPERTY_NOT_FOUND
, dev
->id
?:"", name
);
1198 * Legacy property handling
1201 static void qdev_get_legacy_property(DeviceState
*dev
, Visitor
*v
, void *opaque
,
1202 const char *name
, Error
**errp
)
1204 Property
*prop
= opaque
;
1209 prop
->info
->print(dev
, prop
, buffer
, sizeof(buffer
));
1210 visit_type_str(v
, &ptr
, name
, errp
);
1213 static void qdev_set_legacy_property(DeviceState
*dev
, Visitor
*v
, void *opaque
,
1214 const char *name
, Error
**errp
)
1216 Property
*prop
= opaque
;
1217 Error
*local_err
= NULL
;
1221 if (dev
->state
!= DEV_STATE_CREATED
) {
1222 error_set(errp
, QERR_PERMISSION_DENIED
);
1226 visit_type_str(v
, &ptr
, name
, &local_err
);
1228 error_propagate(errp
, local_err
);
1232 ret
= prop
->info
->parse(dev
, prop
, ptr
);
1233 error_set_from_qdev_prop_error(errp
, ret
, dev
, prop
, ptr
);
1238 * @qdev_add_legacy_property - adds a legacy property
1240 * Do not use this is new code! Properties added through this interface will
1241 * be given names and types in the "legacy" namespace.
1243 * Legacy properties are always processed as strings. The format of the string
1244 * depends on the property type.
1246 void qdev_property_add_legacy(DeviceState
*dev
, Property
*prop
,
1251 name
= g_strdup_printf("legacy-%s", prop
->name
);
1252 type
= g_strdup_printf("legacy<%s>",
1253 prop
->info
->legacy_name
?: prop
->info
->name
);
1255 qdev_property_add(dev
, name
, type
,
1256 prop
->info
->print
? qdev_get_legacy_property
: NULL
,
1257 prop
->info
->parse
? qdev_set_legacy_property
: NULL
,
1266 * @qdev_property_add_static - add a @Property to a device.
1268 * Static properties access data in a struct. The actual type of the
1269 * property and the field depends on the property type.
1271 void qdev_property_add_static(DeviceState
*dev
, Property
*prop
,
1274 qdev_property_add(dev
, prop
->name
, prop
->info
->name
,
1275 prop
->info
->get
, prop
->info
->set
,
1280 DeviceState
*qdev_get_root(void)
1282 static DeviceState
*qdev_root
;
1285 qdev_root
= qdev_create(NULL
, "container");
1286 qdev_init_nofail(qdev_root
);
1292 static void qdev_get_child_property(DeviceState
*dev
, Visitor
*v
, void *opaque
,
1293 const char *name
, Error
**errp
)
1295 DeviceState
*child
= opaque
;
1298 path
= qdev_get_canonical_path(child
);
1299 visit_type_str(v
, &path
, name
, errp
);
1303 static void qdev_release_child_property(DeviceState
*dev
, const char *name
,
1306 DeviceState
*child
= opaque
;
1311 void qdev_property_add_child(DeviceState
*dev
, const char *name
,
1312 DeviceState
*child
, Error
**errp
)
1316 type
= g_strdup_printf("child<%s>", object_get_typename(OBJECT(child
)));
1318 qdev_property_add(dev
, name
, type
, qdev_get_child_property
,
1319 NULL
, qdev_release_child_property
,
1323 g_assert(child
->parent
== NULL
);
1324 child
->parent
= dev
;
1329 static void qdev_get_link_property(DeviceState
*dev
, Visitor
*v
, void *opaque
,
1330 const char *name
, Error
**errp
)
1332 DeviceState
**child
= opaque
;
1336 path
= qdev_get_canonical_path(*child
);
1337 visit_type_str(v
, &path
, name
, errp
);
1341 visit_type_str(v
, &path
, name
, errp
);
1345 static void qdev_set_link_property(DeviceState
*dev
, Visitor
*v
, void *opaque
,
1346 const char *name
, Error
**errp
)
1348 DeviceState
**child
= opaque
;
1349 bool ambiguous
= false;
1353 type
= qdev_property_get_type(dev
, name
, NULL
);
1355 visit_type_str(v
, &path
, name
, errp
);
1361 if (strcmp(path
, "") != 0) {
1362 DeviceState
*target
;
1364 target
= qdev_resolve_path(path
, &ambiguous
);
1368 target_type
= g_strdup_printf("link<%s>", object_get_typename(OBJECT(target
)));
1369 if (strcmp(target_type
, type
) == 0) {
1373 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, type
);
1376 g_free(target_type
);
1378 error_set(errp
, QERR_DEVICE_NOT_FOUND
, path
);
1387 void qdev_property_add_link(DeviceState
*dev
, const char *name
,
1388 const char *type
, DeviceState
**child
,
1393 full_type
= g_strdup_printf("link<%s>", type
);
1395 qdev_property_add(dev
, name
, full_type
,
1396 qdev_get_link_property
,
1397 qdev_set_link_property
,
1403 gchar
*qdev_get_canonical_path(DeviceState
*dev
)
1405 DeviceState
*root
= qdev_get_root();
1406 char *newpath
= NULL
, *path
= NULL
;
1408 while (dev
!= root
) {
1409 DeviceProperty
*prop
= NULL
;
1411 g_assert(dev
->parent
!= NULL
);
1413 QTAILQ_FOREACH(prop
, &dev
->parent
->properties
, node
) {
1414 if (!strstart(prop
->type
, "child<", NULL
)) {
1418 if (prop
->opaque
== dev
) {
1420 newpath
= g_strdup_printf("%s/%s", prop
->name
, path
);
1424 path
= g_strdup(prop
->name
);
1430 g_assert(prop
!= NULL
);
1435 newpath
= g_strdup_printf("/%s", path
);
1441 static DeviceState
*qdev_resolve_abs_path(DeviceState
*parent
,
1445 DeviceProperty
*prop
;
1448 if (parts
[index
] == NULL
) {
1452 if (strcmp(parts
[index
], "") == 0) {
1453 return qdev_resolve_abs_path(parent
, parts
, index
+ 1);
1456 prop
= qdev_property_find(parent
, parts
[index
]);
1462 if (strstart(prop
->type
, "link<", NULL
)) {
1463 DeviceState
**pchild
= prop
->opaque
;
1467 } else if (strstart(prop
->type
, "child<", NULL
)) {
1468 child
= prop
->opaque
;
1475 return qdev_resolve_abs_path(child
, parts
, index
+ 1);
1478 static DeviceState
*qdev_resolve_partial_path(DeviceState
*parent
,
1483 DeviceProperty
*prop
;
1485 dev
= qdev_resolve_abs_path(parent
, parts
, 0);
1487 QTAILQ_FOREACH(prop
, &parent
->properties
, node
) {
1490 if (!strstart(prop
->type
, "child<", NULL
)) {
1494 found
= qdev_resolve_partial_path(prop
->opaque
, parts
, ambiguous
);
1505 if (ambiguous
&& *ambiguous
) {
1513 DeviceState
*qdev_resolve_path(const char *path
, bool *ambiguous
)
1515 bool partial_path
= true;
1519 parts
= g_strsplit(path
, "/", 0);
1520 if (parts
== NULL
|| parts
[0] == NULL
) {
1522 return qdev_get_root();
1525 if (strcmp(parts
[0], "") == 0) {
1526 partial_path
= false;
1533 dev
= qdev_resolve_partial_path(qdev_get_root(), parts
, ambiguous
);
1535 dev
= qdev_resolve_abs_path(qdev_get_root(), parts
, 1);
1543 typedef struct StringProperty
1545 char *(*get
)(DeviceState
*, Error
**);
1546 void (*set
)(DeviceState
*, const char *, Error
**);
1549 static void qdev_property_get_str(DeviceState
*dev
, Visitor
*v
, void *opaque
,
1550 const char *name
, Error
**errp
)
1552 StringProperty
*prop
= opaque
;
1555 value
= prop
->get(dev
, errp
);
1557 visit_type_str(v
, &value
, name
, errp
);
1562 static void qdev_property_set_str(DeviceState
*dev
, Visitor
*v
, void *opaque
,
1563 const char *name
, Error
**errp
)
1565 StringProperty
*prop
= opaque
;
1567 Error
*local_err
= NULL
;
1569 visit_type_str(v
, &value
, name
, &local_err
);
1571 error_propagate(errp
, local_err
);
1575 prop
->set(dev
, value
, errp
);
1579 static void qdev_property_release_str(DeviceState
*dev
, const char *name
,
1582 StringProperty
*prop
= opaque
;
1586 void qdev_property_add_str(DeviceState
*dev
, const char *name
,
1587 char *(*get
)(DeviceState
*, Error
**),
1588 void (*set
)(DeviceState
*, const char *, Error
**),
1591 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
1596 qdev_property_add(dev
, name
, "string",
1597 get
? qdev_property_get_str
: NULL
,
1598 set
? qdev_property_set_str
: NULL
,
1599 qdev_property_release_str
,
1603 void qdev_machine_init(void)
1605 qdev_get_peripheral_anon();
1606 qdev_get_peripheral();
1609 void device_reset(DeviceState
*dev
)
1611 DeviceClass
*klass
= DEVICE_GET_CLASS(dev
);
1618 static TypeInfo device_type_info
= {
1619 .name
= TYPE_DEVICE
,
1620 .parent
= TYPE_OBJECT
,
1621 .instance_size
= sizeof(DeviceState
),
1623 .class_size
= sizeof(DeviceClass
),
1626 static void init_qdev(void)
1628 type_register_static(&device_type_info
);
1631 device_init(init_qdev
);