hw/char: QOM'ify lm32_juart.c
[qemu/ar7.git] / qdev-monitor.c
blobe19617fa8b33c9eb200037734a8504993d1693c7
1 /*
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 #include "qemu/osdep.h"
21 #include "hw/qdev.h"
22 #include "hw/sysbus.h"
23 #include "monitor/monitor.h"
24 #include "monitor/qdev.h"
25 #include "qmp-commands.h"
26 #include "sysemu/arch_init.h"
27 #include "qapi/qmp/qerror.h"
28 #include "qemu/config-file.h"
29 #include "qemu/error-report.h"
30 #include "qemu/help_option.h"
33 * Aliases were a bad idea from the start. Let's keep them
34 * from spreading further.
36 typedef struct QDevAlias
38 const char *typename;
39 const char *alias;
40 uint32_t arch_mask;
41 } QDevAlias;
43 /* Please keep this table sorted by typename. */
44 static const QDevAlias qdev_alias_table[] = {
45 { "e1000", "e1000-82540em" },
46 { "ich9-ahci", "ahci" },
47 { "kvm-pci-assign", "pci-assign" },
48 { "lsi53c895a", "lsi" },
49 { "virtio-9p-ccw", "virtio-9p", QEMU_ARCH_S390X },
50 { "virtio-9p-pci", "virtio-9p", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
51 { "virtio-balloon-ccw", "virtio-balloon", QEMU_ARCH_S390X },
52 { "virtio-balloon-pci", "virtio-balloon",
53 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
54 { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_S390X },
55 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
56 { "virtio-gpu-ccw", "virtio-gpu", QEMU_ARCH_S390X },
57 { "virtio-gpu-pci", "virtio-gpu", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
58 { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_S390X },
59 { "virtio-input-host-pci", "virtio-input-host",
60 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
61 { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_S390X },
62 { "virtio-keyboard-pci", "virtio-keyboard",
63 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
64 { "virtio-mouse-ccw", "virtio-mouse", QEMU_ARCH_S390X },
65 { "virtio-mouse-pci", "virtio-mouse", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
66 { "virtio-net-ccw", "virtio-net", QEMU_ARCH_S390X },
67 { "virtio-net-pci", "virtio-net", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
68 { "virtio-rng-ccw", "virtio-rng", QEMU_ARCH_S390X },
69 { "virtio-rng-pci", "virtio-rng", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
70 { "virtio-scsi-ccw", "virtio-scsi", QEMU_ARCH_S390X },
71 { "virtio-scsi-pci", "virtio-scsi", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
72 { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_S390X },
73 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
74 { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_S390X },
75 { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
76 { }
79 static const char *qdev_class_get_alias(DeviceClass *dc)
81 const char *typename = object_class_get_name(OBJECT_CLASS(dc));
82 int i;
84 for (i = 0; qdev_alias_table[i].typename; i++) {
85 if (qdev_alias_table[i].arch_mask &&
86 !(qdev_alias_table[i].arch_mask & arch_type)) {
87 continue;
90 if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
91 return qdev_alias_table[i].alias;
95 return NULL;
98 static bool qdev_class_has_alias(DeviceClass *dc)
100 return (qdev_class_get_alias(dc) != NULL);
103 static void qdev_print_devinfo(DeviceClass *dc)
105 error_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc)));
106 if (dc->bus_type) {
107 error_printf(", bus %s", dc->bus_type);
109 if (qdev_class_has_alias(dc)) {
110 error_printf(", alias \"%s\"", qdev_class_get_alias(dc));
112 if (dc->desc) {
113 error_printf(", desc \"%s\"", dc->desc);
115 if (dc->cannot_instantiate_with_device_add_yet) {
116 error_printf(", no-user");
118 error_printf("\n");
121 static gint devinfo_cmp(gconstpointer a, gconstpointer b)
123 return strcasecmp(object_class_get_name((ObjectClass *)a),
124 object_class_get_name((ObjectClass *)b));
127 static void qdev_print_devinfos(bool show_no_user)
129 static const char *cat_name[DEVICE_CATEGORY_MAX + 1] = {
130 [DEVICE_CATEGORY_BRIDGE] = "Controller/Bridge/Hub",
131 [DEVICE_CATEGORY_USB] = "USB",
132 [DEVICE_CATEGORY_STORAGE] = "Storage",
133 [DEVICE_CATEGORY_NETWORK] = "Network",
134 [DEVICE_CATEGORY_INPUT] = "Input",
135 [DEVICE_CATEGORY_DISPLAY] = "Display",
136 [DEVICE_CATEGORY_SOUND] = "Sound",
137 [DEVICE_CATEGORY_MISC] = "Misc",
138 [DEVICE_CATEGORY_MAX] = "Uncategorized",
140 GSList *list, *elt;
141 int i;
142 bool cat_printed;
144 list = g_slist_sort(object_class_get_list(TYPE_DEVICE, false),
145 devinfo_cmp);
147 for (i = 0; i <= DEVICE_CATEGORY_MAX; i++) {
148 cat_printed = false;
149 for (elt = list; elt; elt = elt->next) {
150 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
151 TYPE_DEVICE);
152 if ((i < DEVICE_CATEGORY_MAX
153 ? !test_bit(i, dc->categories)
154 : !bitmap_empty(dc->categories, DEVICE_CATEGORY_MAX))
155 || (!show_no_user
156 && dc->cannot_instantiate_with_device_add_yet)) {
157 continue;
159 if (!cat_printed) {
160 error_printf("%s%s devices:\n", i ? "\n" : "",
161 cat_name[i]);
162 cat_printed = true;
164 qdev_print_devinfo(dc);
168 g_slist_free(list);
171 static int set_property(void *opaque, const char *name, const char *value,
172 Error **errp)
174 Object *obj = opaque;
175 Error *err = NULL;
177 if (strcmp(name, "driver") == 0)
178 return 0;
179 if (strcmp(name, "bus") == 0)
180 return 0;
182 object_property_parse(obj, value, name, &err);
183 if (err != NULL) {
184 error_propagate(errp, err);
185 return -1;
187 return 0;
190 static const char *find_typename_by_alias(const char *alias)
192 int i;
194 for (i = 0; qdev_alias_table[i].alias; i++) {
195 if (qdev_alias_table[i].arch_mask &&
196 !(qdev_alias_table[i].arch_mask & arch_type)) {
197 continue;
200 if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
201 return qdev_alias_table[i].typename;
205 return NULL;
208 static DeviceClass *qdev_get_device_class(const char **driver, Error **errp)
210 ObjectClass *oc;
211 DeviceClass *dc;
212 const char *original_name = *driver;
214 oc = object_class_by_name(*driver);
215 if (!oc) {
216 const char *typename = find_typename_by_alias(*driver);
218 if (typename) {
219 *driver = typename;
220 oc = object_class_by_name(*driver);
224 if (!object_class_dynamic_cast(oc, TYPE_DEVICE)) {
225 if (*driver != original_name) {
226 error_setg(errp, "'%s' (alias '%s') is not a valid device model"
227 " name", original_name, *driver);
228 } else {
229 error_setg(errp, "'%s' is not a valid device model name", *driver);
231 return NULL;
234 if (object_class_is_abstract(oc)) {
235 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
236 "non-abstract device type");
237 return NULL;
240 dc = DEVICE_CLASS(oc);
241 if (dc->cannot_instantiate_with_device_add_yet ||
242 (qdev_hotplug && !dc->hotpluggable)) {
243 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
244 "pluggable device type");
245 return NULL;
248 return dc;
252 int qdev_device_help(QemuOpts *opts)
254 Error *local_err = NULL;
255 const char *driver;
256 DevicePropertyInfoList *prop_list;
257 DevicePropertyInfoList *prop;
259 driver = qemu_opt_get(opts, "driver");
260 if (driver && is_help_option(driver)) {
261 qdev_print_devinfos(false);
262 return 1;
265 if (!driver || !qemu_opt_has_help_opt(opts)) {
266 return 0;
269 if (!object_class_by_name(driver)) {
270 const char *typename = find_typename_by_alias(driver);
272 if (typename) {
273 driver = typename;
277 prop_list = qmp_device_list_properties(driver, &local_err);
278 if (local_err) {
279 goto error;
282 for (prop = prop_list; prop; prop = prop->next) {
283 error_printf("%s.%s=%s", driver,
284 prop->value->name,
285 prop->value->type);
286 if (prop->value->has_description) {
287 error_printf(" (%s)\n", prop->value->description);
288 } else {
289 error_printf("\n");
293 qapi_free_DevicePropertyInfoList(prop_list);
294 return 1;
296 error:
297 error_report_err(local_err);
298 return 1;
301 static Object *qdev_get_peripheral(void)
303 static Object *dev;
305 if (dev == NULL) {
306 dev = container_get(qdev_get_machine(), "/peripheral");
309 return dev;
312 static Object *qdev_get_peripheral_anon(void)
314 static Object *dev;
316 if (dev == NULL) {
317 dev = container_get(qdev_get_machine(), "/peripheral-anon");
320 return dev;
323 static void qbus_list_bus(DeviceState *dev, Error **errp)
325 BusState *child;
326 const char *sep = " ";
328 error_append_hint(errp, "child buses at \"%s\":",
329 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
330 QLIST_FOREACH(child, &dev->child_bus, sibling) {
331 error_append_hint(errp, "%s\"%s\"", sep, child->name);
332 sep = ", ";
334 error_append_hint(errp, "\n");
337 static void qbus_list_dev(BusState *bus, Error **errp)
339 BusChild *kid;
340 const char *sep = " ";
342 error_append_hint(errp, "devices at \"%s\":", bus->name);
343 QTAILQ_FOREACH(kid, &bus->children, sibling) {
344 DeviceState *dev = kid->child;
345 error_append_hint(errp, "%s\"%s\"", sep,
346 object_get_typename(OBJECT(dev)));
347 if (dev->id) {
348 error_append_hint(errp, "/\"%s\"", dev->id);
350 sep = ", ";
352 error_append_hint(errp, "\n");
355 static BusState *qbus_find_bus(DeviceState *dev, char *elem)
357 BusState *child;
359 QLIST_FOREACH(child, &dev->child_bus, sibling) {
360 if (strcmp(child->name, elem) == 0) {
361 return child;
364 return NULL;
367 static DeviceState *qbus_find_dev(BusState *bus, char *elem)
369 BusChild *kid;
372 * try to match in order:
373 * (1) instance id, if present
374 * (2) driver name
375 * (3) driver alias, if present
377 QTAILQ_FOREACH(kid, &bus->children, sibling) {
378 DeviceState *dev = kid->child;
379 if (dev->id && strcmp(dev->id, elem) == 0) {
380 return dev;
383 QTAILQ_FOREACH(kid, &bus->children, sibling) {
384 DeviceState *dev = kid->child;
385 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
386 return dev;
389 QTAILQ_FOREACH(kid, &bus->children, sibling) {
390 DeviceState *dev = kid->child;
391 DeviceClass *dc = DEVICE_GET_CLASS(dev);
393 if (qdev_class_has_alias(dc) &&
394 strcmp(qdev_class_get_alias(dc), elem) == 0) {
395 return dev;
398 return NULL;
401 static inline bool qbus_is_full(BusState *bus)
403 BusClass *bus_class = BUS_GET_CLASS(bus);
404 return bus_class->max_dev && bus->max_index >= bus_class->max_dev;
408 * Search the tree rooted at @bus for a bus.
409 * If @name, search for a bus with that name. Note that bus names
410 * need not be unique. Yes, that's screwed up.
411 * Else search for a bus that is a subtype of @bus_typename.
412 * If more than one exists, prefer one that can take another device.
413 * Return the bus if found, else %NULL.
415 static BusState *qbus_find_recursive(BusState *bus, const char *name,
416 const char *bus_typename)
418 BusChild *kid;
419 BusState *pick, *child, *ret;
420 bool match;
422 assert(name || bus_typename);
423 if (name) {
424 match = !strcmp(bus->name, name);
425 } else {
426 match = !!object_dynamic_cast(OBJECT(bus), bus_typename);
429 if (match && !qbus_is_full(bus)) {
430 return bus; /* root matches and isn't full */
433 pick = match ? bus : NULL;
435 QTAILQ_FOREACH(kid, &bus->children, sibling) {
436 DeviceState *dev = kid->child;
437 QLIST_FOREACH(child, &dev->child_bus, sibling) {
438 ret = qbus_find_recursive(child, name, bus_typename);
439 if (ret && !qbus_is_full(ret)) {
440 return ret; /* a descendant matches and isn't full */
442 if (ret && !pick) {
443 pick = ret;
448 /* root or a descendant matches, but is full */
449 return pick;
452 static BusState *qbus_find(const char *path, Error **errp)
454 DeviceState *dev;
455 BusState *bus;
456 char elem[128];
457 int pos, len;
459 /* find start element */
460 if (path[0] == '/') {
461 bus = sysbus_get_default();
462 pos = 0;
463 } else {
464 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
465 assert(!path[0]);
466 elem[0] = len = 0;
468 bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
469 if (!bus) {
470 error_setg(errp, "Bus '%s' not found", elem);
471 return NULL;
473 pos = len;
476 for (;;) {
477 assert(path[pos] == '/' || !path[pos]);
478 while (path[pos] == '/') {
479 pos++;
481 if (path[pos] == '\0') {
482 break;
485 /* find device */
486 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
487 g_assert_not_reached();
488 elem[0] = len = 0;
490 pos += len;
491 dev = qbus_find_dev(bus, elem);
492 if (!dev) {
493 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
494 "Device '%s' not found", elem);
495 qbus_list_dev(bus, errp);
496 return NULL;
499 assert(path[pos] == '/' || !path[pos]);
500 while (path[pos] == '/') {
501 pos++;
503 if (path[pos] == '\0') {
504 /* last specified element is a device. If it has exactly
505 * one child bus accept it nevertheless */
506 if (dev->num_child_bus == 1) {
507 bus = QLIST_FIRST(&dev->child_bus);
508 break;
510 if (dev->num_child_bus) {
511 error_setg(errp, "Device '%s' has multiple child buses",
512 elem);
513 qbus_list_bus(dev, errp);
514 } else {
515 error_setg(errp, "Device '%s' has no child bus", elem);
517 return NULL;
520 /* find bus */
521 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
522 g_assert_not_reached();
523 elem[0] = len = 0;
525 pos += len;
526 bus = qbus_find_bus(dev, elem);
527 if (!bus) {
528 error_setg(errp, "Bus '%s' not found", elem);
529 qbus_list_bus(dev, errp);
530 return NULL;
534 if (qbus_is_full(bus)) {
535 error_setg(errp, "Bus '%s' is full", path);
536 return NULL;
538 return bus;
541 DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
543 DeviceClass *dc;
544 const char *driver, *path, *id;
545 DeviceState *dev;
546 BusState *bus = NULL;
547 Error *err = NULL;
549 driver = qemu_opt_get(opts, "driver");
550 if (!driver) {
551 error_setg(errp, QERR_MISSING_PARAMETER, "driver");
552 return NULL;
555 /* find driver */
556 dc = qdev_get_device_class(&driver, errp);
557 if (!dc) {
558 return NULL;
561 /* find bus */
562 path = qemu_opt_get(opts, "bus");
563 if (path != NULL) {
564 bus = qbus_find(path, errp);
565 if (!bus) {
566 return NULL;
568 if (!object_dynamic_cast(OBJECT(bus), dc->bus_type)) {
569 error_setg(errp, "Device '%s' can't go on %s bus",
570 driver, object_get_typename(OBJECT(bus)));
571 return NULL;
573 } else if (dc->bus_type != NULL) {
574 bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
575 if (!bus || qbus_is_full(bus)) {
576 error_setg(errp, "No '%s' bus found for device '%s'",
577 dc->bus_type, driver);
578 return NULL;
581 if (qdev_hotplug && bus && !qbus_is_hotpluggable(bus)) {
582 error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
583 return NULL;
586 /* create device */
587 dev = DEVICE(object_new(driver));
589 if (bus) {
590 qdev_set_parent_bus(dev, bus);
593 id = qemu_opts_id(opts);
594 if (id) {
595 dev->id = id;
598 if (dev->id) {
599 object_property_add_child(qdev_get_peripheral(), dev->id,
600 OBJECT(dev), NULL);
601 } else {
602 static int anon_count;
603 gchar *name = g_strdup_printf("device[%d]", anon_count++);
604 object_property_add_child(qdev_get_peripheral_anon(), name,
605 OBJECT(dev), NULL);
606 g_free(name);
609 /* set properties */
610 if (qemu_opt_foreach(opts, set_property, dev, &err)) {
611 error_propagate(errp, err);
612 object_unparent(OBJECT(dev));
613 object_unref(OBJECT(dev));
614 return NULL;
617 dev->opts = opts;
618 object_property_set_bool(OBJECT(dev), true, "realized", &err);
619 if (err != NULL) {
620 error_propagate(errp, err);
621 dev->opts = NULL;
622 object_unparent(OBJECT(dev));
623 object_unref(OBJECT(dev));
624 return NULL;
626 return dev;
630 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
631 static void qbus_print(Monitor *mon, BusState *bus, int indent);
633 static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
634 int indent)
636 if (!props)
637 return;
638 for (; props->name; props++) {
639 Error *err = NULL;
640 char *value;
641 char *legacy_name = g_strdup_printf("legacy-%s", props->name);
642 if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
643 value = object_property_get_str(OBJECT(dev), legacy_name, &err);
644 } else {
645 value = object_property_print(OBJECT(dev), props->name, true, &err);
647 g_free(legacy_name);
649 if (err) {
650 error_free(err);
651 continue;
653 qdev_printf("%s = %s\n", props->name,
654 value && *value ? value : "<null>");
655 g_free(value);
659 static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
661 BusClass *bc = BUS_GET_CLASS(bus);
663 if (bc->print_dev) {
664 bc->print_dev(mon, dev, indent);
668 static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
670 ObjectClass *class;
671 BusState *child;
672 NamedGPIOList *ngl;
674 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
675 dev->id ? dev->id : "");
676 indent += 2;
677 QLIST_FOREACH(ngl, &dev->gpios, node) {
678 if (ngl->num_in) {
679 qdev_printf("gpio-in \"%s\" %d\n", ngl->name ? ngl->name : "",
680 ngl->num_in);
682 if (ngl->num_out) {
683 qdev_printf("gpio-out \"%s\" %d\n", ngl->name ? ngl->name : "",
684 ngl->num_out);
687 class = object_get_class(OBJECT(dev));
688 do {
689 qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent);
690 class = object_class_get_parent(class);
691 } while (class != object_class_by_name(TYPE_DEVICE));
692 bus_print_dev(dev->parent_bus, mon, dev, indent);
693 QLIST_FOREACH(child, &dev->child_bus, sibling) {
694 qbus_print(mon, child, indent);
698 static void qbus_print(Monitor *mon, BusState *bus, int indent)
700 BusChild *kid;
702 qdev_printf("bus: %s\n", bus->name);
703 indent += 2;
704 qdev_printf("type %s\n", object_get_typename(OBJECT(bus)));
705 QTAILQ_FOREACH(kid, &bus->children, sibling) {
706 DeviceState *dev = kid->child;
707 qdev_print(mon, dev, indent);
710 #undef qdev_printf
712 void hmp_info_qtree(Monitor *mon, const QDict *qdict)
714 if (sysbus_get_default())
715 qbus_print(mon, sysbus_get_default(), 0);
718 void hmp_info_qdm(Monitor *mon, const QDict *qdict)
720 qdev_print_devinfos(true);
723 typedef struct QOMCompositionState {
724 Monitor *mon;
725 int indent;
726 } QOMCompositionState;
728 static void print_qom_composition(Monitor *mon, Object *obj, int indent);
730 static int print_qom_composition_child(Object *obj, void *opaque)
732 QOMCompositionState *s = opaque;
734 print_qom_composition(s->mon, obj, s->indent);
736 return 0;
739 static void print_qom_composition(Monitor *mon, Object *obj, int indent)
741 QOMCompositionState s = {
742 .mon = mon,
743 .indent = indent + 2,
745 char *name;
747 if (obj == object_get_root()) {
748 name = g_strdup("");
749 } else {
750 name = object_get_canonical_path_component(obj);
752 monitor_printf(mon, "%*s/%s (%s)\n", indent, "", name,
753 object_get_typename(obj));
754 g_free(name);
755 object_child_foreach(obj, print_qom_composition_child, &s);
758 void hmp_info_qom_tree(Monitor *mon, const QDict *dict)
760 const char *path = qdict_get_try_str(dict, "path");
761 Object *obj;
762 bool ambiguous = false;
764 if (path) {
765 obj = object_resolve_path(path, &ambiguous);
766 if (!obj) {
767 monitor_printf(mon, "Path '%s' could not be resolved.\n", path);
768 return;
770 if (ambiguous) {
771 monitor_printf(mon, "Warning: Path '%s' is ambiguous.\n", path);
772 return;
774 } else {
775 obj = qdev_get_machine();
777 print_qom_composition(mon, obj, 0);
780 void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
782 Error *local_err = NULL;
783 QemuOpts *opts;
784 DeviceState *dev;
786 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, &local_err);
787 if (local_err) {
788 error_propagate(errp, local_err);
789 return;
791 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
792 qemu_opts_del(opts);
793 return;
795 dev = qdev_device_add(opts, &local_err);
796 if (!dev) {
797 error_propagate(errp, local_err);
798 qemu_opts_del(opts);
799 return;
801 object_unref(OBJECT(dev));
804 void qmp_device_del(const char *id, Error **errp)
806 Object *obj;
808 if (id[0] == '/') {
809 obj = object_resolve_path(id, NULL);
810 } else {
811 char *root_path = object_get_canonical_path(qdev_get_peripheral());
812 char *path = g_strdup_printf("%s/%s", root_path, id);
814 g_free(root_path);
815 obj = object_resolve_path_type(path, TYPE_DEVICE, NULL);
816 g_free(path);
819 if (!obj) {
820 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
821 "Device '%s' not found", id);
822 return;
825 if (!object_dynamic_cast(obj, TYPE_DEVICE)) {
826 error_setg(errp, "%s is not a hotpluggable device", id);
827 return;
830 qdev_unplug(DEVICE(obj), errp);
833 void qdev_machine_init(void)
835 qdev_get_peripheral_anon();
836 qdev_get_peripheral();
839 QemuOptsList qemu_device_opts = {
840 .name = "device",
841 .implied_opt_name = "driver",
842 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
843 .desc = {
845 * no elements => accept any
846 * sanity checking will happen later
847 * when setting device properties
849 { /* end of list */ }
853 QemuOptsList qemu_global_opts = {
854 .name = "global",
855 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
856 .desc = {
858 .name = "driver",
859 .type = QEMU_OPT_STRING,
861 .name = "property",
862 .type = QEMU_OPT_STRING,
864 .name = "value",
865 .type = QEMU_OPT_STRING,
867 { /* end of list */ }
871 int qemu_global_option(const char *str)
873 char driver[64], property[64];
874 QemuOpts *opts;
875 int rc, offset;
877 rc = sscanf(str, "%63[^.=].%63[^=]%n", driver, property, &offset);
878 if (rc == 2 && str[offset] == '=') {
879 opts = qemu_opts_create(&qemu_global_opts, NULL, 0, &error_abort);
880 qemu_opt_set(opts, "driver", driver, &error_abort);
881 qemu_opt_set(opts, "property", property, &error_abort);
882 qemu_opt_set(opts, "value", str + offset + 1, &error_abort);
883 return 0;
886 opts = qemu_opts_parse_noisily(&qemu_global_opts, str, false);
887 if (!opts) {
888 return -1;
891 return 0;