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 void qdev_register(DeviceInfo
*info
)
50 assert(info
->size
>= sizeof(DeviceState
));
53 info
->next
= device_info_list
;
54 device_info_list
= info
;
57 static DeviceInfo
*qdev_find_info(BusInfo
*bus_info
, const char *name
)
61 /* first check device names */
62 for (info
= device_info_list
; info
!= NULL
; info
= info
->next
) {
63 if (bus_info
&& info
->bus_info
!= bus_info
)
65 if (strcmp(info
->name
, name
) != 0)
70 /* failing that check the aliases */
71 for (info
= device_info_list
; info
!= NULL
; info
= info
->next
) {
72 if (bus_info
&& info
->bus_info
!= bus_info
)
76 if (strcmp(info
->alias
, name
) != 0)
83 static void qdev_property_add_legacy(DeviceState
*dev
, Property
*prop
,
86 static DeviceState
*qdev_create_from_info(BusState
*bus
, DeviceInfo
*info
)
91 assert(bus
->info
== info
->bus_info
);
92 dev
= g_malloc0(info
->size
);
94 dev
->parent_bus
= bus
;
95 qdev_prop_set_defaults(dev
, dev
->info
->props
);
96 qdev_prop_set_defaults(dev
, dev
->parent_bus
->info
->props
);
97 qdev_prop_set_globals(dev
);
98 QTAILQ_INSERT_HEAD(&bus
->children
, dev
, sibling
);
100 assert(bus
->allow_hotplug
);
102 qdev_hot_added
= true;
104 dev
->instance_id_alias
= -1;
105 QTAILQ_INIT(&dev
->properties
);
106 dev
->state
= DEV_STATE_CREATED
;
108 for (prop
= dev
->info
->props
; prop
&& prop
->name
; prop
++) {
109 qdev_property_add_legacy(dev
, prop
, NULL
);
110 qdev_property_add_static(dev
, prop
, NULL
);
113 for (prop
= dev
->info
->bus_info
->props
; prop
&& prop
->name
; prop
++) {
114 qdev_property_add_legacy(dev
, prop
, NULL
);
115 qdev_property_add_static(dev
, prop
, NULL
);
118 qdev_property_add_str(dev
, "type", qdev_get_type
, NULL
, NULL
);
123 /* Create a new device. This only initializes the device state structure
124 and allows properties to be set. qdev_init should be called to
125 initialize the actual device emulation. */
126 DeviceState
*qdev_create(BusState
*bus
, const char *name
)
130 dev
= qdev_try_create(bus
, name
);
133 hw_error("Unknown device '%s' for bus '%s'\n", name
,
136 hw_error("Unknown device '%s' for default sysbus\n", name
);
143 DeviceState
*qdev_try_create(BusState
*bus
, const char *name
)
148 bus
= sysbus_get_default();
151 info
= qdev_find_info(bus
->info
, name
);
156 return qdev_create_from_info(bus
, info
);
159 static void qdev_print_devinfo(DeviceInfo
*info
)
161 error_printf("name \"%s\", bus %s",
162 info
->name
, info
->bus_info
->name
);
164 error_printf(", alias \"%s\"", info
->alias
);
167 error_printf(", desc \"%s\"", info
->desc
);
170 error_printf(", no-user");
175 static int set_property(const char *name
, const char *value
, void *opaque
)
177 DeviceState
*dev
= opaque
;
179 if (strcmp(name
, "driver") == 0)
181 if (strcmp(name
, "bus") == 0)
184 if (qdev_prop_parse(dev
, name
, value
) == -1) {
190 int qdev_device_help(QemuOpts
*opts
)
196 driver
= qemu_opt_get(opts
, "driver");
197 if (driver
&& !strcmp(driver
, "?")) {
198 for (info
= device_info_list
; info
!= NULL
; info
= info
->next
) {
200 continue; /* not available, don't show */
202 qdev_print_devinfo(info
);
207 if (!driver
|| !qemu_opt_get(opts
, "?")) {
211 info
= qdev_find_info(NULL
, driver
);
216 for (prop
= info
->props
; prop
&& prop
->name
; prop
++) {
218 * TODO Properties without a parser are just for dirty hacks.
219 * qdev_prop_ptr is the only such PropertyInfo. It's marked
220 * for removal. This conditional should be removed along with
223 if (!prop
->info
->parse
) {
224 continue; /* no way to set it, don't show */
226 error_printf("%s.%s=%s\n", info
->name
, prop
->name
,
227 prop
->info
->legacy_name
?: prop
->info
->name
);
229 for (prop
= info
->bus_info
->props
; prop
&& prop
->name
; prop
++) {
230 if (!prop
->info
->parse
) {
231 continue; /* no way to set it, don't show */
233 error_printf("%s.%s=%s\n", info
->name
, prop
->name
,
234 prop
->info
->legacy_name
?: prop
->info
->name
);
239 static DeviceState
*qdev_get_peripheral(void)
241 static DeviceState
*dev
;
244 dev
= qdev_create(NULL
, "container");
245 qdev_property_add_child(qdev_get_root(), "peripheral", dev
, NULL
);
246 qdev_init_nofail(dev
);
252 static DeviceState
*qdev_get_peripheral_anon(void)
254 static DeviceState
*dev
;
257 dev
= qdev_create(NULL
, "container");
258 qdev_property_add_child(qdev_get_root(), "peripheral-anon", dev
, NULL
);
259 qdev_init_nofail(dev
);
265 DeviceState
*qdev_device_add(QemuOpts
*opts
)
267 const char *driver
, *path
, *id
;
272 driver
= qemu_opt_get(opts
, "driver");
274 qerror_report(QERR_MISSING_PARAMETER
, "driver");
279 info
= qdev_find_info(NULL
, driver
);
280 if (!info
|| info
->no_user
) {
281 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "driver", "a driver name");
282 error_printf_unless_qmp("Try with argument '?' for a list.\n");
287 path
= qemu_opt_get(opts
, "bus");
289 bus
= qbus_find(path
);
293 if (bus
->info
!= info
->bus_info
) {
294 qerror_report(QERR_BAD_BUS_FOR_DEVICE
,
295 driver
, bus
->info
->name
);
299 bus
= qbus_find_recursive(main_system_bus
, NULL
, info
->bus_info
);
301 qerror_report(QERR_NO_BUS_FOR_DEVICE
,
302 info
->name
, info
->bus_info
->name
);
306 if (qdev_hotplug
&& !bus
->allow_hotplug
) {
307 qerror_report(QERR_BUS_NO_HOTPLUG
, bus
->name
);
311 /* create device, set properties */
312 qdev
= qdev_create_from_info(bus
, info
);
313 id
= qemu_opts_id(opts
);
316 qdev_property_add_child(qdev_get_peripheral(), qdev
->id
, qdev
, NULL
);
318 static int anon_count
;
319 gchar
*name
= g_strdup_printf("device[%d]", anon_count
++);
320 qdev_property_add_child(qdev_get_peripheral_anon(), name
,
324 if (qemu_opt_foreach(opts
, set_property
, qdev
, 1) != 0) {
328 if (qdev_init(qdev
) < 0) {
329 qerror_report(QERR_DEVICE_INIT_FAILED
, driver
);
336 /* Initialize a device. Device properties should be set before calling
337 this function. IRQs and MMIO regions should be connected/mapped after
338 calling this function.
339 On failure, destroy the device and return negative value.
340 Return 0 on success. */
341 int qdev_init(DeviceState
*dev
)
345 assert(dev
->state
== DEV_STATE_CREATED
);
346 rc
= dev
->info
->init(dev
, dev
->info
);
351 if (dev
->info
->vmsd
) {
352 vmstate_register_with_alias_id(dev
, -1, dev
->info
->vmsd
, dev
,
353 dev
->instance_id_alias
,
354 dev
->alias_required_for_version
);
356 dev
->state
= DEV_STATE_INITIALIZED
;
357 if (dev
->hotplugged
&& dev
->info
->reset
) {
358 dev
->info
->reset(dev
);
363 void qdev_set_legacy_instance_id(DeviceState
*dev
, int alias_id
,
364 int required_for_version
)
366 assert(dev
->state
== DEV_STATE_CREATED
);
367 dev
->instance_id_alias
= alias_id
;
368 dev
->alias_required_for_version
= required_for_version
;
371 int qdev_unplug(DeviceState
*dev
)
373 if (!dev
->parent_bus
->allow_hotplug
) {
374 qerror_report(QERR_BUS_NO_HOTPLUG
, dev
->parent_bus
->name
);
377 assert(dev
->info
->unplug
!= NULL
);
380 qerror_report(QERR_DEVICE_IN_USE
, dev
->id
?:"");
384 qdev_hot_removed
= true;
386 return dev
->info
->unplug(dev
);
389 static int qdev_reset_one(DeviceState
*dev
, void *opaque
)
391 if (dev
->info
->reset
) {
392 dev
->info
->reset(dev
);
398 BusState
*sysbus_get_default(void)
400 if (!main_system_bus
) {
401 main_system_bus_create();
403 return main_system_bus
;
406 static int qbus_reset_one(BusState
*bus
, void *opaque
)
408 if (bus
->info
->reset
) {
409 return bus
->info
->reset(bus
);
414 void qdev_reset_all(DeviceState
*dev
)
416 qdev_walk_children(dev
, qdev_reset_one
, qbus_reset_one
, NULL
);
419 void qbus_reset_all_fn(void *opaque
)
421 BusState
*bus
= opaque
;
422 qbus_walk_children(bus
, qdev_reset_one
, qbus_reset_one
, NULL
);
425 /* can be used as ->unplug() callback for the simple cases */
426 int qdev_simple_unplug_cb(DeviceState
*dev
)
434 /* Like qdev_init(), but terminate program via error_report() instead of
435 returning an error value. This is okay during machine creation.
436 Don't use for hotplug, because there callers need to recover from
437 failure. Exception: if you know the device's init() callback can't
438 fail, then qdev_init_nofail() can't fail either, and is therefore
439 usable even then. But relying on the device implementation that
440 way is somewhat unclean, and best avoided. */
441 void qdev_init_nofail(DeviceState
*dev
)
443 DeviceInfo
*info
= dev
->info
;
445 if (qdev_init(dev
) < 0) {
446 error_report("Initialization of device %s failed", info
->name
);
451 static void qdev_property_del_all(DeviceState
*dev
)
453 while (!QTAILQ_EMPTY(&dev
->properties
)) {
454 DeviceProperty
*prop
= QTAILQ_FIRST(&dev
->properties
);
456 QTAILQ_REMOVE(&dev
->properties
, prop
, node
);
459 prop
->release(dev
, prop
->name
, prop
->opaque
);
468 /* Unlink device from bus and free the structure. */
469 void qdev_free(DeviceState
*dev
)
474 qdev_property_del_all(dev
);
476 if (dev
->state
== DEV_STATE_INITIALIZED
) {
477 while (dev
->num_child_bus
) {
478 bus
= QLIST_FIRST(&dev
->child_bus
);
482 vmstate_unregister(dev
, dev
->info
->vmsd
, dev
);
484 dev
->info
->exit(dev
);
486 qemu_opts_del(dev
->opts
);
488 QTAILQ_REMOVE(&dev
->parent_bus
->children
, dev
, sibling
);
489 for (prop
= dev
->info
->props
; prop
&& prop
->name
; prop
++) {
490 if (prop
->info
->free
) {
491 prop
->info
->free(dev
, prop
);
497 void qdev_machine_creation_done(void)
500 * ok, initial machine setup is done, starting from now we can
501 * only create hotpluggable devices
506 bool qdev_machine_modified(void)
508 return qdev_hot_added
|| qdev_hot_removed
;
511 /* Get a character (serial) device interface. */
512 CharDriverState
*qdev_init_chardev(DeviceState
*dev
)
514 static int next_serial
;
516 /* FIXME: This function needs to go away: use chardev properties! */
517 return serial_hds
[next_serial
++];
520 BusState
*qdev_get_parent_bus(DeviceState
*dev
)
522 return dev
->parent_bus
;
525 void qdev_init_gpio_in(DeviceState
*dev
, qemu_irq_handler handler
, int n
)
527 assert(dev
->num_gpio_in
== 0);
528 dev
->num_gpio_in
= n
;
529 dev
->gpio_in
= qemu_allocate_irqs(handler
, dev
, n
);
532 void qdev_init_gpio_out(DeviceState
*dev
, qemu_irq
*pins
, int n
)
534 assert(dev
->num_gpio_out
== 0);
535 dev
->num_gpio_out
= n
;
536 dev
->gpio_out
= pins
;
539 qemu_irq
qdev_get_gpio_in(DeviceState
*dev
, int n
)
541 assert(n
>= 0 && n
< dev
->num_gpio_in
);
542 return dev
->gpio_in
[n
];
545 void qdev_connect_gpio_out(DeviceState
* dev
, int n
, qemu_irq pin
)
547 assert(n
>= 0 && n
< dev
->num_gpio_out
);
548 dev
->gpio_out
[n
] = pin
;
551 void qdev_set_nic_properties(DeviceState
*dev
, NICInfo
*nd
)
553 qdev_prop_set_macaddr(dev
, "mac", nd
->macaddr
.a
);
555 qdev_prop_set_vlan(dev
, "vlan", nd
->vlan
);
557 qdev_prop_set_netdev(dev
, "netdev", nd
->netdev
);
558 if (nd
->nvectors
!= DEV_NVECTORS_UNSPECIFIED
&&
559 qdev_prop_exists(dev
, "vectors")) {
560 qdev_prop_set_uint32(dev
, "vectors", nd
->nvectors
);
562 nd
->instantiated
= 1;
565 BusState
*qdev_get_child_bus(DeviceState
*dev
, const char *name
)
569 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
570 if (strcmp(name
, bus
->name
) == 0) {
577 int qbus_walk_children(BusState
*bus
, qdev_walkerfn
*devfn
,
578 qbus_walkerfn
*busfn
, void *opaque
)
584 err
= busfn(bus
, opaque
);
590 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
591 err
= qdev_walk_children(dev
, devfn
, busfn
, opaque
);
600 int qdev_walk_children(DeviceState
*dev
, qdev_walkerfn
*devfn
,
601 qbus_walkerfn
*busfn
, void *opaque
)
607 err
= devfn(dev
, opaque
);
613 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
614 err
= qbus_walk_children(bus
, devfn
, busfn
, opaque
);
623 static BusState
*qbus_find_recursive(BusState
*bus
, const char *name
,
627 BusState
*child
, *ret
;
630 if (name
&& (strcmp(bus
->name
, name
) != 0)) {
633 if (info
&& (bus
->info
!= info
)) {
640 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
641 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
642 ret
= qbus_find_recursive(child
, name
, info
);
651 DeviceState
*qdev_find_recursive(BusState
*bus
, const char *id
)
653 DeviceState
*dev
, *ret
;
656 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
657 if (dev
->id
&& strcmp(dev
->id
, id
) == 0)
659 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
660 ret
= qdev_find_recursive(child
, id
);
669 static void qbus_list_bus(DeviceState
*dev
)
672 const char *sep
= " ";
674 error_printf("child busses at \"%s\":",
675 dev
->id
? dev
->id
: dev
->info
->name
);
676 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
677 error_printf("%s\"%s\"", sep
, child
->name
);
683 static void qbus_list_dev(BusState
*bus
)
686 const char *sep
= " ";
688 error_printf("devices at \"%s\":", bus
->name
);
689 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
690 error_printf("%s\"%s\"", sep
, dev
->info
->name
);
692 error_printf("/\"%s\"", dev
->id
);
698 static BusState
*qbus_find_bus(DeviceState
*dev
, char *elem
)
702 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
703 if (strcmp(child
->name
, elem
) == 0) {
710 static DeviceState
*qbus_find_dev(BusState
*bus
, char *elem
)
715 * try to match in order:
716 * (1) instance id, if present
718 * (3) driver alias, if present
720 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
721 if (dev
->id
&& strcmp(dev
->id
, elem
) == 0) {
725 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
726 if (strcmp(dev
->info
->name
, elem
) == 0) {
730 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
731 if (dev
->info
->alias
&& strcmp(dev
->info
->alias
, elem
) == 0) {
738 static BusState
*qbus_find(const char *path
)
745 /* find start element */
746 if (path
[0] == '/') {
747 bus
= main_system_bus
;
750 if (sscanf(path
, "%127[^/]%n", elem
, &len
) != 1) {
754 bus
= qbus_find_recursive(main_system_bus
, elem
, NULL
);
756 qerror_report(QERR_BUS_NOT_FOUND
, elem
);
763 assert(path
[pos
] == '/' || !path
[pos
]);
764 while (path
[pos
] == '/') {
767 if (path
[pos
] == '\0') {
772 if (sscanf(path
+pos
, "%127[^/]%n", elem
, &len
) != 1) {
777 dev
= qbus_find_dev(bus
, elem
);
779 qerror_report(QERR_DEVICE_NOT_FOUND
, elem
);
780 if (!monitor_cur_is_qmp()) {
786 assert(path
[pos
] == '/' || !path
[pos
]);
787 while (path
[pos
] == '/') {
790 if (path
[pos
] == '\0') {
791 /* last specified element is a device. If it has exactly
792 * one child bus accept it nevertheless */
793 switch (dev
->num_child_bus
) {
795 qerror_report(QERR_DEVICE_NO_BUS
, elem
);
798 return QLIST_FIRST(&dev
->child_bus
);
800 qerror_report(QERR_DEVICE_MULTIPLE_BUSSES
, elem
);
801 if (!monitor_cur_is_qmp()) {
809 if (sscanf(path
+pos
, "%127[^/]%n", elem
, &len
) != 1) {
814 bus
= qbus_find_bus(dev
, elem
);
816 qerror_report(QERR_BUS_NOT_FOUND
, elem
);
817 if (!monitor_cur_is_qmp()) {
825 void qbus_create_inplace(BusState
*bus
, BusInfo
*info
,
826 DeviceState
*parent
, const char *name
)
832 bus
->parent
= parent
;
835 /* use supplied name */
836 bus
->name
= g_strdup(name
);
837 } else if (parent
&& parent
->id
) {
838 /* parent device has id -> use it for bus name */
839 len
= strlen(parent
->id
) + 16;
841 snprintf(buf
, len
, "%s.%d", parent
->id
, parent
->num_child_bus
);
844 /* no id -> use lowercase bus type for bus name */
845 len
= strlen(info
->name
) + 16;
847 len
= snprintf(buf
, len
, "%s.%d", info
->name
,
848 parent
? parent
->num_child_bus
: 0);
849 for (i
= 0; i
< len
; i
++)
850 buf
[i
] = qemu_tolower(buf
[i
]);
854 QTAILQ_INIT(&bus
->children
);
856 QLIST_INSERT_HEAD(&parent
->child_bus
, bus
, sibling
);
857 parent
->num_child_bus
++;
858 } else if (bus
!= main_system_bus
) {
859 /* TODO: once all bus devices are qdevified,
860 only reset handler for main_system_bus should be registered here. */
861 qemu_register_reset(qbus_reset_all_fn
, bus
);
865 BusState
*qbus_create(BusInfo
*info
, DeviceState
*parent
, const char *name
)
869 bus
= g_malloc0(info
->size
);
870 bus
->qdev_allocated
= 1;
871 qbus_create_inplace(bus
, info
, parent
, name
);
875 static void main_system_bus_create(void)
877 /* assign main_system_bus before qbus_create_inplace()
878 * in order to make "if (bus != main_system_bus)" work */
879 main_system_bus
= g_malloc0(system_bus_info
.size
);
880 main_system_bus
->qdev_allocated
= 1;
881 qbus_create_inplace(main_system_bus
, &system_bus_info
, NULL
,
885 void qbus_free(BusState
*bus
)
889 while ((dev
= QTAILQ_FIRST(&bus
->children
)) != NULL
) {
893 QLIST_REMOVE(bus
, sibling
);
894 bus
->parent
->num_child_bus
--;
896 assert(bus
!= main_system_bus
); /* main_system_bus is never freed */
897 qemu_unregister_reset(qbus_reset_all_fn
, bus
);
899 g_free((void*)bus
->name
);
900 if (bus
->qdev_allocated
) {
905 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
906 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
);
908 static void qdev_print_props(Monitor
*mon
, DeviceState
*dev
, Property
*props
,
909 const char *prefix
, int indent
)
915 while (props
->name
) {
917 * TODO Properties without a print method are just for dirty
918 * hacks. qdev_prop_ptr is the only such PropertyInfo. It's
919 * marked for removal. The test props->info->print should be
920 * removed along with it.
922 if (props
->info
->print
) {
923 props
->info
->print(dev
, props
, buf
, sizeof(buf
));
924 qdev_printf("%s-prop: %s = %s\n", prefix
, props
->name
, buf
);
930 static void qdev_print(Monitor
*mon
, DeviceState
*dev
, int indent
)
933 qdev_printf("dev: %s, id \"%s\"\n", dev
->info
->name
,
934 dev
->id
? dev
->id
: "");
936 if (dev
->num_gpio_in
) {
937 qdev_printf("gpio-in %d\n", dev
->num_gpio_in
);
939 if (dev
->num_gpio_out
) {
940 qdev_printf("gpio-out %d\n", dev
->num_gpio_out
);
942 qdev_print_props(mon
, dev
, dev
->info
->props
, "dev", indent
);
943 qdev_print_props(mon
, dev
, dev
->parent_bus
->info
->props
, "bus", indent
);
944 if (dev
->parent_bus
->info
->print_dev
)
945 dev
->parent_bus
->info
->print_dev(mon
, dev
, indent
);
946 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
947 qbus_print(mon
, child
, indent
);
951 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
)
953 struct DeviceState
*dev
;
955 qdev_printf("bus: %s\n", bus
->name
);
957 qdev_printf("type %s\n", bus
->info
->name
);
958 QTAILQ_FOREACH(dev
, &bus
->children
, sibling
) {
959 qdev_print(mon
, dev
, indent
);
964 void do_info_qtree(Monitor
*mon
)
967 qbus_print(mon
, main_system_bus
, 0);
970 void do_info_qdm(Monitor
*mon
)
974 for (info
= device_info_list
; info
!= NULL
; info
= info
->next
) {
975 qdev_print_devinfo(info
);
979 int do_device_add(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
983 opts
= qemu_opts_from_qdict(qemu_find_opts("device"), qdict
);
987 if (!monitor_cur_is_qmp() && qdev_device_help(opts
)) {
991 if (!qdev_device_add(opts
)) {
998 int do_device_del(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1000 const char *id
= qdict_get_str(qdict
, "id");
1003 dev
= qdev_find_recursive(main_system_bus
, id
);
1005 qerror_report(QERR_DEVICE_NOT_FOUND
, id
);
1008 return qdev_unplug(dev
);
1011 static int qdev_get_fw_dev_path_helper(DeviceState
*dev
, char *p
, int size
)
1015 if (dev
&& dev
->parent_bus
) {
1017 l
= qdev_get_fw_dev_path_helper(dev
->parent_bus
->parent
, p
, size
);
1018 if (dev
->parent_bus
->info
->get_fw_dev_path
) {
1019 d
= dev
->parent_bus
->info
->get_fw_dev_path(dev
);
1020 l
+= snprintf(p
+ l
, size
- l
, "%s", d
);
1023 l
+= snprintf(p
+ l
, size
- l
, "%s", dev
->info
->name
);
1026 l
+= snprintf(p
+ l
, size
- l
, "/");
1031 char* qdev_get_fw_dev_path(DeviceState
*dev
)
1036 l
= qdev_get_fw_dev_path_helper(dev
, path
, 128);
1040 return strdup(path
);
1043 char *qdev_get_type(DeviceState
*dev
, Error
**errp
)
1045 return g_strdup(dev
->info
->name
);
1048 void qdev_ref(DeviceState
*dev
)
1053 void qdev_unref(DeviceState
*dev
)
1055 g_assert(dev
->ref
> 0);
1059 void qdev_property_add(DeviceState
*dev
, const char *name
, const char *type
,
1060 DevicePropertyAccessor
*get
, DevicePropertyAccessor
*set
,
1061 DevicePropertyRelease
*release
,
1062 void *opaque
, Error
**errp
)
1064 DeviceProperty
*prop
= g_malloc0(sizeof(*prop
));
1066 prop
->name
= g_strdup(name
);
1067 prop
->type
= g_strdup(type
);
1071 prop
->release
= release
;
1072 prop
->opaque
= opaque
;
1074 QTAILQ_INSERT_TAIL(&dev
->properties
, prop
, node
);
1077 static DeviceProperty
*qdev_property_find(DeviceState
*dev
, const char *name
)
1079 DeviceProperty
*prop
;
1081 QTAILQ_FOREACH(prop
, &dev
->properties
, node
) {
1082 if (strcmp(prop
->name
, name
) == 0) {
1090 void qdev_property_get(DeviceState
*dev
, Visitor
*v
, const char *name
,
1093 DeviceProperty
*prop
= qdev_property_find(dev
, name
);
1096 error_set(errp
, QERR_PROPERTY_NOT_FOUND
, dev
->id
?:"", name
);
1101 error_set(errp
, QERR_PERMISSION_DENIED
);
1103 prop
->get(dev
, v
, prop
->opaque
, name
, errp
);
1107 void qdev_property_set(DeviceState
*dev
, Visitor
*v
, const char *name
,
1110 DeviceProperty
*prop
= qdev_property_find(dev
, name
);
1113 error_set(errp
, QERR_PROPERTY_NOT_FOUND
, dev
->id
?:"", name
);
1118 error_set(errp
, QERR_PERMISSION_DENIED
);
1120 prop
->set(dev
, v
, prop
->opaque
, name
, errp
);
1124 const char *qdev_property_get_type(DeviceState
*dev
, const char *name
, Error
**errp
)
1126 DeviceProperty
*prop
= qdev_property_find(dev
, name
);
1129 error_set(errp
, QERR_PROPERTY_NOT_FOUND
, dev
->id
?:"", name
);
1137 * Legacy property handling
1140 static void qdev_get_legacy_property(DeviceState
*dev
, Visitor
*v
, void *opaque
,
1141 const char *name
, Error
**errp
)
1143 Property
*prop
= opaque
;
1148 prop
->info
->print(dev
, prop
, buffer
, sizeof(buffer
));
1149 visit_type_str(v
, &ptr
, name
, errp
);
1152 static void qdev_set_legacy_property(DeviceState
*dev
, Visitor
*v
, void *opaque
,
1153 const char *name
, Error
**errp
)
1155 Property
*prop
= opaque
;
1156 Error
*local_err
= NULL
;
1160 if (dev
->state
!= DEV_STATE_CREATED
) {
1161 error_set(errp
, QERR_PERMISSION_DENIED
);
1165 visit_type_str(v
, &ptr
, name
, &local_err
);
1167 error_propagate(errp
, local_err
);
1171 ret
= prop
->info
->parse(dev
, prop
, ptr
);
1172 error_set_from_qdev_prop_error(errp
, ret
, dev
, prop
, ptr
);
1177 * @qdev_add_legacy_property - adds a legacy property
1179 * Do not use this is new code! Properties added through this interface will
1180 * be given names and types in the "legacy" namespace.
1182 * Legacy properties are always processed as strings. The format of the string
1183 * depends on the property type.
1185 void qdev_property_add_legacy(DeviceState
*dev
, Property
*prop
,
1190 name
= g_strdup_printf("legacy-%s", prop
->name
);
1191 type
= g_strdup_printf("legacy<%s>",
1192 prop
->info
->legacy_name
?: prop
->info
->name
);
1194 qdev_property_add(dev
, name
, type
,
1195 prop
->info
->print
? qdev_get_legacy_property
: NULL
,
1196 prop
->info
->parse
? qdev_set_legacy_property
: NULL
,
1205 * @qdev_property_add_static - add a @Property to a device.
1207 * Static properties access data in a struct. The actual type of the
1208 * property and the field depends on the property type.
1210 void qdev_property_add_static(DeviceState
*dev
, Property
*prop
,
1213 qdev_property_add(dev
, prop
->name
, prop
->info
->name
,
1214 prop
->info
->get
, prop
->info
->set
,
1219 DeviceState
*qdev_get_root(void)
1221 static DeviceState
*qdev_root
;
1224 qdev_root
= qdev_create(NULL
, "container");
1225 qdev_init_nofail(qdev_root
);
1231 static void qdev_get_child_property(DeviceState
*dev
, Visitor
*v
, void *opaque
,
1232 const char *name
, Error
**errp
)
1234 DeviceState
*child
= opaque
;
1237 path
= qdev_get_canonical_path(child
);
1238 visit_type_str(v
, &path
, name
, errp
);
1242 void qdev_property_add_child(DeviceState
*dev
, const char *name
,
1243 DeviceState
*child
, Error
**errp
)
1247 type
= g_strdup_printf("child<%s>", child
->info
->name
);
1249 qdev_property_add(dev
, name
, type
, qdev_get_child_property
,
1250 NULL
, NULL
, child
, errp
);
1253 g_assert(child
->parent
== NULL
);
1254 child
->parent
= dev
;
1259 static void qdev_get_link_property(DeviceState
*dev
, Visitor
*v
, void *opaque
,
1260 const char *name
, Error
**errp
)
1262 DeviceState
**child
= opaque
;
1266 path
= qdev_get_canonical_path(*child
);
1267 visit_type_str(v
, &path
, name
, errp
);
1271 visit_type_str(v
, &path
, name
, errp
);
1275 static void qdev_set_link_property(DeviceState
*dev
, Visitor
*v
, void *opaque
,
1276 const char *name
, Error
**errp
)
1278 DeviceState
**child
= opaque
;
1279 bool ambiguous
= false;
1283 type
= qdev_property_get_type(dev
, name
, NULL
);
1285 visit_type_str(v
, &path
, name
, errp
);
1291 if (strcmp(path
, "") != 0) {
1292 DeviceState
*target
;
1294 target
= qdev_resolve_path(path
, &ambiguous
);
1298 target_type
= g_strdup_printf("link<%s>", target
->info
->name
);
1299 if (strcmp(target_type
, type
) == 0) {
1303 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, type
);
1306 g_free(target_type
);
1308 error_set(errp
, QERR_DEVICE_NOT_FOUND
, path
);
1317 void qdev_property_add_link(DeviceState
*dev
, const char *name
,
1318 const char *type
, DeviceState
**child
,
1323 full_type
= g_strdup_printf("link<%s>", type
);
1325 qdev_property_add(dev
, name
, full_type
,
1326 qdev_get_link_property
,
1327 qdev_set_link_property
,
1333 gchar
*qdev_get_canonical_path(DeviceState
*dev
)
1335 DeviceState
*root
= qdev_get_root();
1336 char *newpath
= NULL
, *path
= NULL
;
1338 while (dev
!= root
) {
1339 DeviceProperty
*prop
= NULL
;
1341 g_assert(dev
->parent
!= NULL
);
1343 QTAILQ_FOREACH(prop
, &dev
->parent
->properties
, node
) {
1344 if (!strstart(prop
->type
, "child<", NULL
)) {
1348 if (prop
->opaque
== dev
) {
1350 newpath
= g_strdup_printf("%s/%s", prop
->name
, path
);
1354 path
= g_strdup(prop
->name
);
1360 g_assert(prop
!= NULL
);
1365 newpath
= g_strdup_printf("/%s", path
);
1371 static DeviceState
*qdev_resolve_abs_path(DeviceState
*parent
,
1375 DeviceProperty
*prop
;
1378 if (parts
[index
] == NULL
) {
1382 if (strcmp(parts
[index
], "") == 0) {
1383 return qdev_resolve_abs_path(parent
, parts
, index
+ 1);
1386 prop
= qdev_property_find(parent
, parts
[index
]);
1392 if (strstart(prop
->type
, "link<", NULL
)) {
1393 DeviceState
**pchild
= prop
->opaque
;
1397 } else if (strstart(prop
->type
, "child<", NULL
)) {
1398 child
= prop
->opaque
;
1405 return qdev_resolve_abs_path(child
, parts
, index
+ 1);
1408 static DeviceState
*qdev_resolve_partial_path(DeviceState
*parent
,
1413 DeviceProperty
*prop
;
1415 dev
= qdev_resolve_abs_path(parent
, parts
, 0);
1417 QTAILQ_FOREACH(prop
, &parent
->properties
, node
) {
1420 if (!strstart(prop
->type
, "child<", NULL
)) {
1424 found
= qdev_resolve_partial_path(prop
->opaque
, parts
, ambiguous
);
1435 if (ambiguous
&& *ambiguous
) {
1443 DeviceState
*qdev_resolve_path(const char *path
, bool *ambiguous
)
1445 bool partial_path
= true;
1449 parts
= g_strsplit(path
, "/", 0);
1450 if (parts
== NULL
|| parts
[0] == NULL
) {
1452 return qdev_get_root();
1455 if (strcmp(parts
[0], "") == 0) {
1456 partial_path
= false;
1463 dev
= qdev_resolve_partial_path(qdev_get_root(), parts
, ambiguous
);
1465 dev
= qdev_resolve_abs_path(qdev_get_root(), parts
, 1);
1473 typedef struct StringProperty
1475 char *(*get
)(DeviceState
*, Error
**);
1476 void (*set
)(DeviceState
*, const char *, Error
**);
1479 static void qdev_property_get_str(DeviceState
*dev
, Visitor
*v
, void *opaque
,
1480 const char *name
, Error
**errp
)
1482 StringProperty
*prop
= opaque
;
1485 value
= prop
->get(dev
, errp
);
1487 visit_type_str(v
, &value
, name
, errp
);
1492 static void qdev_property_set_str(DeviceState
*dev
, Visitor
*v
, void *opaque
,
1493 const char *name
, Error
**errp
)
1495 StringProperty
*prop
= opaque
;
1497 Error
*local_err
= NULL
;
1499 visit_type_str(v
, &value
, name
, &local_err
);
1501 error_propagate(errp
, local_err
);
1505 prop
->set(dev
, value
, errp
);
1509 static void qdev_property_release_str(DeviceState
*dev
, const char *name
,
1512 StringProperty
*prop
= opaque
;
1516 void qdev_property_add_str(DeviceState
*dev
, const char *name
,
1517 char *(*get
)(DeviceState
*, Error
**),
1518 void (*set
)(DeviceState
*, const char *, Error
**),
1521 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
1526 qdev_property_add(dev
, name
, "string",
1527 get
? qdev_property_get_str
: NULL
,
1528 set
? qdev_property_set_str
: NULL
,
1529 qdev_property_release_str
,
1533 void qdev_machine_init(void)
1535 qdev_get_peripheral_anon();
1536 qdev_get_peripheral();