blob: c73410c02ea28c3d675f7167a28646742a9d3d68 [file] [log] [blame]
Anthony Liguoriee46d8a2011-12-22 15:24:20 -06001/*
2 * Dynamic device configuration and creation.
3 *
4 * Copyright (c) 2009 CodeSourcery
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
Peter Maydelld38ea872016-01-29 17:50:05 +000020#include "qemu/osdep.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010021#include "hw/qdev.h"
Andreas Färber2f7bd822013-04-16 03:50:21 +020022#include "hw/sysbus.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010023#include "monitor/monitor.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010024#include "monitor/qdev.h"
Luiz Capitulinoa15fef22012-03-29 12:38:50 -030025#include "qmp-commands.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010026#include "sysemu/arch_init.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010027#include "qapi/qmp/qerror.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010028#include "qemu/config-file.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010029#include "qemu/error-report.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020030#include "qemu/help_option.h"
Kevin Wolf9680cae2016-09-20 13:38:42 +020031#include "sysemu/block-backend.h"
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060032
33/*
34 * Aliases were a bad idea from the start. Let's keep them
35 * from spreading further.
36 */
37typedef struct QDevAlias
38{
39 const char *typename;
40 const char *alias;
Alexander Graf5f629d92012-05-18 02:36:26 +020041 uint32_t arch_mask;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060042} QDevAlias;
43
Sascha Silbe36e99162016-02-18 22:44:13 +010044/* Please keep this table sorted by typename. */
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060045static const QDevAlias qdev_alias_table[] = {
Sascha Silbe36e99162016-02-18 22:44:13 +010046 { "e1000", "e1000-82540em" },
47 { "ich9-ahci", "ahci" },
48 { "kvm-pci-assign", "pci-assign" },
49 { "lsi53c895a", "lsi" },
Sascha Silbe588c36c2016-02-18 22:44:14 +010050 { "virtio-9p-ccw", "virtio-9p", QEMU_ARCH_S390X },
51 { "virtio-9p-pci", "virtio-9p", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
52 { "virtio-balloon-ccw", "virtio-balloon", QEMU_ARCH_S390X },
Alexander Graf5f629d92012-05-18 02:36:26 +020053 { "virtio-balloon-pci", "virtio-balloon",
54 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
Alexander Graf1f68f1d2015-06-16 23:06:33 +020055 { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_S390X },
Sascha Silbe36e99162016-02-18 22:44:13 +010056 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
Sascha Silbe588c36c2016-02-18 22:44:14 +010057 { "virtio-gpu-ccw", "virtio-gpu", QEMU_ARCH_S390X },
58 { "virtio-gpu-pci", "virtio-gpu", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
59 { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_S390X },
60 { "virtio-input-host-pci", "virtio-input-host",
61 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
62 { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_S390X },
63 { "virtio-keyboard-pci", "virtio-keyboard",
64 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
65 { "virtio-mouse-ccw", "virtio-mouse", QEMU_ARCH_S390X },
66 { "virtio-mouse-pci", "virtio-mouse", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
Alexander Graf1f68f1d2015-06-16 23:06:33 +020067 { "virtio-net-ccw", "virtio-net", QEMU_ARCH_S390X },
Sascha Silbe36e99162016-02-18 22:44:13 +010068 { "virtio-net-pci", "virtio-net", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
Sascha Silbe588c36c2016-02-18 22:44:14 +010069 { "virtio-rng-ccw", "virtio-rng", QEMU_ARCH_S390X },
70 { "virtio-rng-pci", "virtio-rng", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
71 { "virtio-scsi-ccw", "virtio-scsi", QEMU_ARCH_S390X },
72 { "virtio-scsi-pci", "virtio-scsi", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
Alexander Graf1f68f1d2015-06-16 23:06:33 +020073 { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_S390X },
Sascha Silbe36e99162016-02-18 22:44:13 +010074 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
Sascha Silbe588c36c2016-02-18 22:44:14 +010075 { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_S390X },
76 { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060077 { }
78};
79
80static const char *qdev_class_get_alias(DeviceClass *dc)
81{
82 const char *typename = object_class_get_name(OBJECT_CLASS(dc));
83 int i;
84
85 for (i = 0; qdev_alias_table[i].typename; i++) {
Alexander Graf5f629d92012-05-18 02:36:26 +020086 if (qdev_alias_table[i].arch_mask &&
87 !(qdev_alias_table[i].arch_mask & arch_type)) {
88 continue;
89 }
90
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060091 if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
92 return qdev_alias_table[i].alias;
93 }
94 }
95
96 return NULL;
97}
98
99static bool qdev_class_has_alias(DeviceClass *dc)
100{
101 return (qdev_class_get_alias(dc) != NULL);
102}
103
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200104static void qdev_print_devinfo(DeviceClass *dc)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600105{
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200106 error_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc)));
Anthony Liguori0d936922012-05-02 09:00:20 +0200107 if (dc->bus_type) {
108 error_printf(", bus %s", dc->bus_type);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600109 }
110 if (qdev_class_has_alias(dc)) {
111 error_printf(", alias \"%s\"", qdev_class_get_alias(dc));
112 }
113 if (dc->desc) {
114 error_printf(", desc \"%s\"", dc->desc);
115 }
Markus Armbrusterefec3dd2013-11-28 17:26:54 +0100116 if (dc->cannot_instantiate_with_device_add_yet) {
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600117 error_printf(", no-user");
118 }
119 error_printf("\n");
120}
121
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200122static gint devinfo_cmp(gconstpointer a, gconstpointer b)
123{
124 return strcasecmp(object_class_get_name((ObjectClass *)a),
125 object_class_get_name((ObjectClass *)b));
126}
127
128static void qdev_print_devinfos(bool show_no_user)
129{
130 static const char *cat_name[DEVICE_CATEGORY_MAX + 1] = {
131 [DEVICE_CATEGORY_BRIDGE] = "Controller/Bridge/Hub",
132 [DEVICE_CATEGORY_USB] = "USB",
133 [DEVICE_CATEGORY_STORAGE] = "Storage",
134 [DEVICE_CATEGORY_NETWORK] = "Network",
135 [DEVICE_CATEGORY_INPUT] = "Input",
136 [DEVICE_CATEGORY_DISPLAY] = "Display",
137 [DEVICE_CATEGORY_SOUND] = "Sound",
138 [DEVICE_CATEGORY_MISC] = "Misc",
139 [DEVICE_CATEGORY_MAX] = "Uncategorized",
140 };
141 GSList *list, *elt;
142 int i;
143 bool cat_printed;
144
145 list = g_slist_sort(object_class_get_list(TYPE_DEVICE, false),
146 devinfo_cmp);
147
148 for (i = 0; i <= DEVICE_CATEGORY_MAX; i++) {
149 cat_printed = false;
150 for (elt = list; elt; elt = elt->next) {
151 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
152 TYPE_DEVICE);
153 if ((i < DEVICE_CATEGORY_MAX
154 ? !test_bit(i, dc->categories)
155 : !bitmap_empty(dc->categories, DEVICE_CATEGORY_MAX))
Markus Armbrusterefec3dd2013-11-28 17:26:54 +0100156 || (!show_no_user
157 && dc->cannot_instantiate_with_device_add_yet)) {
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200158 continue;
159 }
160 if (!cat_printed) {
161 error_printf("%s%s devices:\n", i ? "\n" : "",
162 cat_name[i]);
163 cat_printed = true;
164 }
165 qdev_print_devinfo(dc);
166 }
167 }
168
169 g_slist_free(list);
170}
171
Markus Armbruster71df1d82015-03-12 08:40:25 +0100172static int set_property(void *opaque, const char *name, const char *value,
173 Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600174{
Paolo Bonzini98a65282014-02-08 11:01:49 +0100175 Object *obj = opaque;
Andreas Färberb1fe9bc2013-05-01 16:10:24 +0200176 Error *err = NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600177
178 if (strcmp(name, "driver") == 0)
179 return 0;
180 if (strcmp(name, "bus") == 0)
181 return 0;
182
Paolo Bonzini98a65282014-02-08 11:01:49 +0100183 object_property_parse(obj, value, name, &err);
Andreas Färberb1fe9bc2013-05-01 16:10:24 +0200184 if (err != NULL) {
Markus Armbruster4caa4892015-03-12 13:58:02 +0100185 error_propagate(errp, err);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600186 return -1;
187 }
188 return 0;
189}
190
191static const char *find_typename_by_alias(const char *alias)
192{
193 int i;
194
195 for (i = 0; qdev_alias_table[i].alias; i++) {
Alexander Graf5f629d92012-05-18 02:36:26 +0200196 if (qdev_alias_table[i].arch_mask &&
197 !(qdev_alias_table[i].arch_mask & arch_type)) {
198 continue;
199 }
200
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600201 if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
202 return qdev_alias_table[i].typename;
203 }
204 }
205
206 return NULL;
207}
208
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200209static DeviceClass *qdev_get_device_class(const char **driver, Error **errp)
210{
211 ObjectClass *oc;
212 DeviceClass *dc;
Sascha Silbef6b53192016-02-18 22:44:12 +0100213 const char *original_name = *driver;
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200214
215 oc = object_class_by_name(*driver);
216 if (!oc) {
217 const char *typename = find_typename_by_alias(*driver);
218
219 if (typename) {
220 *driver = typename;
221 oc = object_class_by_name(*driver);
222 }
223 }
224
225 if (!object_class_dynamic_cast(oc, TYPE_DEVICE)) {
Sascha Silbef6b53192016-02-18 22:44:12 +0100226 if (*driver != original_name) {
227 error_setg(errp, "'%s' (alias '%s') is not a valid device model"
228 " name", original_name, *driver);
229 } else {
230 error_setg(errp, "'%s' is not a valid device model name", *driver);
231 }
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200232 return NULL;
233 }
234
235 if (object_class_is_abstract(oc)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100236 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
237 "non-abstract device type");
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200238 return NULL;
239 }
240
241 dc = DEVICE_CLASS(oc);
242 if (dc->cannot_instantiate_with_device_add_yet ||
243 (qdev_hotplug && !dc->hotpluggable)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100244 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
245 "pluggable device type");
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200246 return NULL;
247 }
248
249 return dc;
250}
251
252
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600253int qdev_device_help(QemuOpts *opts)
254{
Stefan Hajnoczief523582014-07-09 14:01:32 +0200255 Error *local_err = NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600256 const char *driver;
Stefan Hajnoczief523582014-07-09 14:01:32 +0200257 DevicePropertyInfoList *prop_list;
258 DevicePropertyInfoList *prop;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600259
260 driver = qemu_opt_get(opts, "driver");
Peter Maydellc8057f92012-08-02 13:45:54 +0100261 if (driver && is_help_option(driver)) {
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200262 qdev_print_devinfos(false);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600263 return 1;
264 }
265
Peter Maydellc8057f92012-08-02 13:45:54 +0100266 if (!driver || !qemu_opt_has_help_opt(opts)) {
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600267 return 0;
268 }
269
Markus Armbruster33fe9682015-10-01 10:59:59 +0200270 if (!object_class_by_name(driver)) {
271 const char *typename = find_typename_by_alias(driver);
272
273 if (typename) {
274 driver = typename;
275 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600276 }
277
Stefan Hajnoczief523582014-07-09 14:01:32 +0200278 prop_list = qmp_device_list_properties(driver, &local_err);
Gonglei0722eba2014-09-16 10:19:33 +0800279 if (local_err) {
Eduardo Habkost5185f0e2014-11-01 13:56:10 -0200280 goto error;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600281 }
Stefan Hajnoczief523582014-07-09 14:01:32 +0200282
283 for (prop = prop_list; prop; prop = prop->next) {
Gonglei07d09c52014-10-07 14:33:23 +0800284 error_printf("%s.%s=%s", driver,
Stefan Hajnoczief523582014-07-09 14:01:32 +0200285 prop->value->name,
286 prop->value->type);
Gonglei07d09c52014-10-07 14:33:23 +0800287 if (prop->value->has_description) {
288 error_printf(" (%s)\n", prop->value->description);
289 } else {
290 error_printf("\n");
291 }
Stefan Hajnoczief523582014-07-09 14:01:32 +0200292 }
293
294 qapi_free_DevicePropertyInfoList(prop_list);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600295 return 1;
Eduardo Habkost5185f0e2014-11-01 13:56:10 -0200296
297error:
Markus Armbruster78288672015-12-18 16:35:07 +0100298 error_report_err(local_err);
Eduardo Habkost5185f0e2014-11-01 13:56:10 -0200299 return 1;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600300}
301
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600302static Object *qdev_get_peripheral(void)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600303{
Anthony Liguori8b45d442011-12-23 09:08:05 -0600304 static Object *dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600305
306 if (dev == NULL) {
Andreas Färberdfe47e72012-04-05 13:21:46 +0200307 dev = container_get(qdev_get_machine(), "/peripheral");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600308 }
309
Anthony Liguori8b45d442011-12-23 09:08:05 -0600310 return dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600311}
312
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600313static Object *qdev_get_peripheral_anon(void)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600314{
Anthony Liguori8b45d442011-12-23 09:08:05 -0600315 static Object *dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600316
317 if (dev == NULL) {
Andreas Färberdfe47e72012-04-05 13:21:46 +0200318 dev = container_get(qdev_get_machine(), "/peripheral-anon");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600319 }
320
Anthony Liguori8b45d442011-12-23 09:08:05 -0600321 return dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600322}
323
Eric Blake50b7b002015-09-10 10:19:16 -0600324static void qbus_list_bus(DeviceState *dev, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600325{
326 BusState *child;
327 const char *sep = " ";
328
Eric Blake50b7b002015-09-10 10:19:16 -0600329 error_append_hint(errp, "child buses at \"%s\":",
330 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600331 QLIST_FOREACH(child, &dev->child_bus, sibling) {
Eric Blake50b7b002015-09-10 10:19:16 -0600332 error_append_hint(errp, "%s\"%s\"", sep, child->name);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600333 sep = ", ";
334 }
Markus Armbruster543202c2015-12-17 17:35:14 +0100335 error_append_hint(errp, "\n");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600336}
337
Eric Blake50b7b002015-09-10 10:19:16 -0600338static void qbus_list_dev(BusState *bus, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600339{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600340 BusChild *kid;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600341 const char *sep = " ";
342
Eric Blake50b7b002015-09-10 10:19:16 -0600343 error_append_hint(errp, "devices at \"%s\":", bus->name);
Anthony Liguori0866aca2011-12-23 15:34:39 -0600344 QTAILQ_FOREACH(kid, &bus->children, sibling) {
345 DeviceState *dev = kid->child;
Eric Blake50b7b002015-09-10 10:19:16 -0600346 error_append_hint(errp, "%s\"%s\"", sep,
347 object_get_typename(OBJECT(dev)));
348 if (dev->id) {
349 error_append_hint(errp, "/\"%s\"", dev->id);
350 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600351 sep = ", ";
352 }
Markus Armbruster543202c2015-12-17 17:35:14 +0100353 error_append_hint(errp, "\n");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600354}
355
356static BusState *qbus_find_bus(DeviceState *dev, char *elem)
357{
358 BusState *child;
359
360 QLIST_FOREACH(child, &dev->child_bus, sibling) {
361 if (strcmp(child->name, elem) == 0) {
362 return child;
363 }
364 }
365 return NULL;
366}
367
368static DeviceState *qbus_find_dev(BusState *bus, char *elem)
369{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600370 BusChild *kid;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600371
372 /*
373 * try to match in order:
374 * (1) instance id, if present
375 * (2) driver name
376 * (3) driver alias, if present
377 */
Anthony Liguori0866aca2011-12-23 15:34:39 -0600378 QTAILQ_FOREACH(kid, &bus->children, sibling) {
379 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600380 if (dev->id && strcmp(dev->id, elem) == 0) {
381 return dev;
382 }
383 }
Anthony Liguori0866aca2011-12-23 15:34:39 -0600384 QTAILQ_FOREACH(kid, &bus->children, sibling) {
385 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600386 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
387 return dev;
388 }
389 }
Anthony Liguori0866aca2011-12-23 15:34:39 -0600390 QTAILQ_FOREACH(kid, &bus->children, sibling) {
391 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600392 DeviceClass *dc = DEVICE_GET_CLASS(dev);
393
394 if (qdev_class_has_alias(dc) &&
395 strcmp(qdev_class_get_alias(dc), elem) == 0) {
396 return dev;
397 }
398 }
399 return NULL;
400}
401
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100402static inline bool qbus_is_full(BusState *bus)
403{
404 BusClass *bus_class = BUS_GET_CLASS(bus);
405 return bus_class->max_dev && bus->max_index >= bus_class->max_dev;
406}
407
408/*
409 * Search the tree rooted at @bus for a bus.
410 * If @name, search for a bus with that name. Note that bus names
411 * need not be unique. Yes, that's screwed up.
412 * Else search for a bus that is a subtype of @bus_typename.
413 * If more than one exists, prefer one that can take another device.
414 * Return the bus if found, else %NULL.
415 */
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600416static BusState *qbus_find_recursive(BusState *bus, const char *name,
Anthony Liguori0d936922012-05-02 09:00:20 +0200417 const char *bus_typename)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600418{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600419 BusChild *kid;
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100420 BusState *pick, *child, *ret;
421 bool match;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600422
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100423 assert(name || bus_typename);
424 if (name) {
425 match = !strcmp(bus->name, name);
426 } else {
427 match = !!object_dynamic_cast(OBJECT(bus), bus_typename);
KONRAD Frederic1395af62013-01-15 00:08:00 +0100428 }
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100429
430 if (match && !qbus_is_full(bus)) {
431 return bus; /* root matches and isn't full */
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600432 }
433
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100434 pick = match ? bus : NULL;
435
Anthony Liguori0866aca2011-12-23 15:34:39 -0600436 QTAILQ_FOREACH(kid, &bus->children, sibling) {
437 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600438 QLIST_FOREACH(child, &dev->child_bus, sibling) {
Anthony Liguori0d936922012-05-02 09:00:20 +0200439 ret = qbus_find_recursive(child, name, bus_typename);
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100440 if (ret && !qbus_is_full(ret)) {
441 return ret; /* a descendant matches and isn't full */
442 }
443 if (ret && !pick) {
444 pick = ret;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600445 }
446 }
447 }
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100448
449 /* root or a descendant matches, but is full */
450 return pick;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600451}
452
Markus Armbrusterd2828422015-03-11 19:16:04 +0100453static BusState *qbus_find(const char *path, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600454{
455 DeviceState *dev;
456 BusState *bus;
457 char elem[128];
458 int pos, len;
459
460 /* find start element */
461 if (path[0] == '/') {
462 bus = sysbus_get_default();
463 pos = 0;
464 } else {
465 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
466 assert(!path[0]);
467 elem[0] = len = 0;
468 }
469 bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
470 if (!bus) {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100471 error_setg(errp, "Bus '%s' not found", elem);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600472 return NULL;
473 }
474 pos = len;
475 }
476
477 for (;;) {
478 assert(path[pos] == '/' || !path[pos]);
479 while (path[pos] == '/') {
480 pos++;
481 }
482 if (path[pos] == '\0') {
Markus Armbrustered238ba2015-03-11 18:39:16 +0100483 break;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600484 }
485
486 /* find device */
487 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
Stefan Weildfc6f862013-07-25 18:21:28 +0200488 g_assert_not_reached();
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600489 elem[0] = len = 0;
490 }
491 pos += len;
492 dev = qbus_find_dev(bus, elem);
493 if (!dev) {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100494 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
495 "Device '%s' not found", elem);
Eric Blake50b7b002015-09-10 10:19:16 -0600496 qbus_list_dev(bus, errp);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600497 return NULL;
498 }
499
500 assert(path[pos] == '/' || !path[pos]);
501 while (path[pos] == '/') {
502 pos++;
503 }
504 if (path[pos] == '\0') {
505 /* last specified element is a device. If it has exactly
506 * one child bus accept it nevertheless */
Markus Armbrustered238ba2015-03-11 18:39:16 +0100507 if (dev->num_child_bus == 1) {
508 bus = QLIST_FIRST(&dev->child_bus);
509 break;
510 }
511 if (dev->num_child_bus) {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100512 error_setg(errp, "Device '%s' has multiple child buses",
513 elem);
Eric Blake50b7b002015-09-10 10:19:16 -0600514 qbus_list_bus(dev, errp);
Markus Armbrustered238ba2015-03-11 18:39:16 +0100515 } else {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100516 error_setg(errp, "Device '%s' has no child bus", elem);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600517 }
Markus Armbrustered238ba2015-03-11 18:39:16 +0100518 return NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600519 }
520
521 /* find bus */
522 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
Stefan Weildfc6f862013-07-25 18:21:28 +0200523 g_assert_not_reached();
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600524 elem[0] = len = 0;
525 }
526 pos += len;
527 bus = qbus_find_bus(dev, elem);
528 if (!bus) {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100529 error_setg(errp, "Bus '%s' not found", elem);
Eric Blake50b7b002015-09-10 10:19:16 -0600530 qbus_list_bus(dev, errp);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600531 return NULL;
532 }
533 }
Markus Armbrustered238ba2015-03-11 18:39:16 +0100534
535 if (qbus_is_full(bus)) {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100536 error_setg(errp, "Bus '%s' is full", path);
Markus Armbrustered238ba2015-03-11 18:39:16 +0100537 return NULL;
538 }
539 return bus;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600540}
541
Juergen Grossce49b732016-11-22 07:10:57 +0100542void qdev_set_id(DeviceState *dev, const char *id)
543{
544 if (id) {
545 dev->id = id;
546 }
547
548 if (dev->id) {
549 object_property_add_child(qdev_get_peripheral(), dev->id,
550 OBJECT(dev), NULL);
551 } else {
552 static int anon_count;
553 gchar *name = g_strdup_printf("device[%d]", anon_count++);
554 object_property_add_child(qdev_get_peripheral_anon(), name,
555 OBJECT(dev), NULL);
556 g_free(name);
557 }
558}
559
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100560DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600561{
Andreas Färberf4d85792013-08-24 01:21:22 +0200562 DeviceClass *dc;
Juergen Grossce49b732016-11-22 07:10:57 +0100563 const char *driver, *path;
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200564 DeviceState *dev;
Andreas Färber2f7bd822013-04-16 03:50:21 +0200565 BusState *bus = NULL;
Andreas Färber852e2c52013-10-07 16:42:34 +0200566 Error *err = NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600567
568 driver = qemu_opt_get(opts, "driver");
569 if (!driver) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100570 error_setg(errp, QERR_MISSING_PARAMETER, "driver");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600571 return NULL;
572 }
573
574 /* find driver */
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100575 dc = qdev_get_device_class(&driver, errp);
576 if (!dc) {
Markus Armbruster7ea5e782013-11-28 17:27:03 +0100577 return NULL;
578 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600579
580 /* find bus */
581 path = qemu_opt_get(opts, "bus");
582 if (path != NULL) {
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100583 bus = qbus_find(path, errp);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600584 if (!bus) {
585 return NULL;
586 }
Andreas Färberf4d85792013-08-24 01:21:22 +0200587 if (!object_dynamic_cast(OBJECT(bus), dc->bus_type)) {
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100588 error_setg(errp, "Device '%s' can't go on %s bus",
589 driver, object_get_typename(OBJECT(bus)));
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600590 return NULL;
591 }
Andreas Färberf4d85792013-08-24 01:21:22 +0200592 } else if (dc->bus_type != NULL) {
593 bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100594 if (!bus || qbus_is_full(bus)) {
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100595 error_setg(errp, "No '%s' bus found for device '%s'",
596 dc->bus_type, driver);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600597 return NULL;
598 }
599 }
Igor Mammedov39b888b2014-09-26 09:28:17 +0000600 if (qdev_hotplug && bus && !qbus_is_hotpluggable(bus)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100601 error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600602 return NULL;
603 }
604
Amos Kong7b030942014-03-03 15:57:55 +0800605 /* create device */
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200606 dev = DEVICE(object_new(driver));
Andreas Färber2f7bd822013-04-16 03:50:21 +0200607
608 if (bus) {
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200609 qdev_set_parent_bus(dev, bus);
Andreas Färber2f7bd822013-04-16 03:50:21 +0200610 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600611
Juergen Grossce49b732016-11-22 07:10:57 +0100612 qdev_set_id(dev, qemu_opts_id(opts));
Bandan Das52aa17c2014-02-26 10:32:40 -0700613
Amos Kong7b030942014-03-03 15:57:55 +0800614 /* set properties */
Markus Armbruster4caa4892015-03-12 13:58:02 +0100615 if (qemu_opt_foreach(opts, set_property, dev, &err)) {
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100616 error_propagate(errp, err);
Amos Kong7b030942014-03-03 15:57:55 +0800617 object_unparent(OBJECT(dev));
618 object_unref(OBJECT(dev));
619 return NULL;
620 }
621
Bandan Das52aa17c2014-02-26 10:32:40 -0700622 dev->opts = opts;
Andreas Färber852e2c52013-10-07 16:42:34 +0200623 object_property_set_bool(OBJECT(dev), true, "realized", &err);
624 if (err != NULL) {
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100625 error_propagate(errp, err);
Bandan Das52aa17c2014-02-26 10:32:40 -0700626 dev->opts = NULL;
Andreas Färber852e2c52013-10-07 16:42:34 +0200627 object_unparent(OBJECT(dev));
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200628 object_unref(OBJECT(dev));
Paolo Bonzinif424d5c2012-03-27 18:38:46 +0200629 return NULL;
630 }
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200631 return dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600632}
633
634
635#define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
636static void qbus_print(Monitor *mon, BusState *bus, int indent);
637
638static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
Paolo Bonzinibce54472012-03-28 18:12:47 +0200639 int indent)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600640{
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600641 if (!props)
642 return;
Paolo Bonzinid8229792012-02-02 09:47:13 +0100643 for (; props->name; props++) {
644 Error *err = NULL;
645 char *value;
646 char *legacy_name = g_strdup_printf("legacy-%s", props->name);
647 if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
648 value = object_property_get_str(OBJECT(dev), legacy_name, &err);
649 } else {
Paolo Bonzinidae3bda2014-02-08 11:01:51 +0100650 value = object_property_print(OBJECT(dev), props->name, true, &err);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600651 }
Paolo Bonzinid8229792012-02-02 09:47:13 +0100652 g_free(legacy_name);
653
654 if (err) {
655 error_free(err);
656 continue;
657 }
Paolo Bonzinibce54472012-03-28 18:12:47 +0200658 qdev_printf("%s = %s\n", props->name,
Paolo Bonzinid8229792012-02-02 09:47:13 +0100659 value && *value ? value : "<null>");
660 g_free(value);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600661 }
662}
663
Anthony Liguori0d936922012-05-02 09:00:20 +0200664static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
665{
666 BusClass *bc = BUS_GET_CLASS(bus);
667
668 if (bc->print_dev) {
669 bc->print_dev(mon, dev, indent);
670 }
671}
672
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600673static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
674{
Paolo Bonzinibce54472012-03-28 18:12:47 +0200675 ObjectClass *class;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600676 BusState *child;
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700677 NamedGPIOList *ngl;
678
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600679 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
680 dev->id ? dev->id : "");
681 indent += 2;
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700682 QLIST_FOREACH(ngl, &dev->gpios, node) {
683 if (ngl->num_in) {
684 qdev_printf("gpio-in \"%s\" %d\n", ngl->name ? ngl->name : "",
685 ngl->num_in);
686 }
687 if (ngl->num_out) {
688 qdev_printf("gpio-out \"%s\" %d\n", ngl->name ? ngl->name : "",
689 ngl->num_out);
690 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600691 }
Paolo Bonzinibce54472012-03-28 18:12:47 +0200692 class = object_get_class(OBJECT(dev));
693 do {
694 qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent);
695 class = object_class_get_parent(class);
696 } while (class != object_class_by_name(TYPE_DEVICE));
Gerd Hoffmannda9fbe72012-07-11 12:21:23 +0200697 bus_print_dev(dev->parent_bus, mon, dev, indent);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600698 QLIST_FOREACH(child, &dev->child_bus, sibling) {
699 qbus_print(mon, child, indent);
700 }
701}
702
703static void qbus_print(Monitor *mon, BusState *bus, int indent)
704{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600705 BusChild *kid;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600706
707 qdev_printf("bus: %s\n", bus->name);
708 indent += 2;
Anthony Liguori0d936922012-05-02 09:00:20 +0200709 qdev_printf("type %s\n", object_get_typename(OBJECT(bus)));
Anthony Liguori0866aca2011-12-23 15:34:39 -0600710 QTAILQ_FOREACH(kid, &bus->children, sibling) {
711 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600712 qdev_print(mon, dev, indent);
713 }
714}
715#undef qdev_printf
716
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100717void hmp_info_qtree(Monitor *mon, const QDict *qdict)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600718{
719 if (sysbus_get_default())
720 qbus_print(mon, sysbus_get_default(), 0);
721}
722
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100723void hmp_info_qdm(Monitor *mon, const QDict *qdict)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600724{
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200725 qdev_print_devinfos(true);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600726}
727
Andreas Färbera01ff752014-05-07 17:03:18 +0200728typedef struct QOMCompositionState {
729 Monitor *mon;
730 int indent;
731} QOMCompositionState;
732
733static void print_qom_composition(Monitor *mon, Object *obj, int indent);
734
735static int print_qom_composition_child(Object *obj, void *opaque)
736{
737 QOMCompositionState *s = opaque;
738
739 print_qom_composition(s->mon, obj, s->indent);
740
741 return 0;
742}
743
744static void print_qom_composition(Monitor *mon, Object *obj, int indent)
745{
746 QOMCompositionState s = {
747 .mon = mon,
748 .indent = indent + 2,
749 };
750 char *name;
751
752 if (obj == object_get_root()) {
753 name = g_strdup("");
754 } else {
755 name = object_get_canonical_path_component(obj);
756 }
757 monitor_printf(mon, "%*s/%s (%s)\n", indent, "", name,
758 object_get_typename(obj));
759 g_free(name);
760 object_child_foreach(obj, print_qom_composition_child, &s);
761}
762
763void hmp_info_qom_tree(Monitor *mon, const QDict *dict)
764{
765 const char *path = qdict_get_try_str(dict, "path");
766 Object *obj;
767 bool ambiguous = false;
768
769 if (path) {
770 obj = object_resolve_path(path, &ambiguous);
771 if (!obj) {
772 monitor_printf(mon, "Path '%s' could not be resolved.\n", path);
773 return;
774 }
775 if (ambiguous) {
776 monitor_printf(mon, "Warning: Path '%s' is ambiguous.\n", path);
777 return;
778 }
779 } else {
780 obj = qdev_get_machine();
781 }
782 print_qom_composition(mon, obj, 0);
783}
784
Markus Armbruster485febc2015-03-13 17:25:50 +0100785void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600786{
Luiz Capitulino4e899782012-04-18 17:24:01 -0300787 Error *local_err = NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600788 QemuOpts *opts;
Paolo Bonzinib09995a2013-01-25 14:12:37 +0100789 DeviceState *dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600790
Luiz Capitulino4e899782012-04-18 17:24:01 -0300791 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100792 if (local_err) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100793 error_propagate(errp, local_err);
794 return;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600795 }
796 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
797 qemu_opts_del(opts);
Markus Armbruster485febc2015-03-13 17:25:50 +0100798 return;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600799 }
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100800 dev = qdev_device_add(opts, &local_err);
Paolo Bonzinib09995a2013-01-25 14:12:37 +0100801 if (!dev) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100802 error_propagate(errp, local_err);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600803 qemu_opts_del(opts);
Markus Armbruster485febc2015-03-13 17:25:50 +0100804 return;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600805 }
Paolo Bonzinib09995a2013-01-25 14:12:37 +0100806 object_unref(OBJECT(dev));
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600807}
808
Kevin Wolf6c1db522016-09-20 13:38:41 +0200809static DeviceState *find_device_state(const char *id, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600810{
Igor Mammedovb6cc36a2014-10-02 10:08:45 +0000811 Object *obj;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600812
Daniel P. Berrange6287d822015-09-11 13:33:56 +0100813 if (id[0] == '/') {
814 obj = object_resolve_path(id, NULL);
815 } else {
816 char *root_path = object_get_canonical_path(qdev_get_peripheral());
817 char *path = g_strdup_printf("%s/%s", root_path, id);
818
819 g_free(root_path);
820 obj = object_resolve_path_type(path, TYPE_DEVICE, NULL);
821 g_free(path);
822 }
Igor Mammedovb6cc36a2014-10-02 10:08:45 +0000823
824 if (!obj) {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100825 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
826 "Device '%s' not found", id);
Kevin Wolf6c1db522016-09-20 13:38:41 +0200827 return NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600828 }
Luiz Capitulino56f91072012-03-14 17:37:38 -0300829
Daniel P. Berrange6287d822015-09-11 13:33:56 +0100830 if (!object_dynamic_cast(obj, TYPE_DEVICE)) {
831 error_setg(errp, "%s is not a hotpluggable device", id);
Kevin Wolf6c1db522016-09-20 13:38:41 +0200832 return NULL;
Daniel P. Berrange6287d822015-09-11 13:33:56 +0100833 }
834
Kevin Wolf6c1db522016-09-20 13:38:41 +0200835 return DEVICE(obj);
836}
837
838void qmp_device_del(const char *id, Error **errp)
839{
840 DeviceState *dev = find_device_state(id, errp);
841 if (dev != NULL) {
842 qdev_unplug(dev, errp);
843 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600844}
845
Kevin Wolf9680cae2016-09-20 13:38:42 +0200846BlockBackend *blk_by_qdev_id(const char *id, Error **errp)
847{
848 DeviceState *dev;
849 BlockBackend *blk;
850
851 dev = find_device_state(id, errp);
852 if (dev == NULL) {
853 return NULL;
854 }
855
856 blk = blk_by_dev(dev);
857 if (!blk) {
858 error_setg(errp, "Device does not have a block device backend");
859 }
860 return blk;
861}
862
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600863void qdev_machine_init(void)
864{
865 qdev_get_peripheral_anon();
866 qdev_get_peripheral();
867}
Paolo Bonzini4d454572012-11-26 16:03:42 +0100868
869QemuOptsList qemu_device_opts = {
870 .name = "device",
871 .implied_opt_name = "driver",
872 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
873 .desc = {
874 /*
875 * no elements => accept any
876 * sanity checking will happen later
877 * when setting device properties
878 */
879 { /* end of list */ }
880 },
881};
882
883QemuOptsList qemu_global_opts = {
884 .name = "global",
885 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
886 .desc = {
887 {
888 .name = "driver",
889 .type = QEMU_OPT_STRING,
890 },{
891 .name = "property",
892 .type = QEMU_OPT_STRING,
893 },{
894 .name = "value",
895 .type = QEMU_OPT_STRING,
896 },
897 { /* end of list */ }
898 },
899};
900
901int qemu_global_option(const char *str)
902{
903 char driver[64], property[64];
904 QemuOpts *opts;
905 int rc, offset;
906
Paolo Bonzini3751d7c2015-04-09 14:16:19 +0200907 rc = sscanf(str, "%63[^.=].%63[^=]%n", driver, property, &offset);
908 if (rc == 2 && str[offset] == '=') {
909 opts = qemu_opts_create(&qemu_global_opts, NULL, 0, &error_abort);
910 qemu_opt_set(opts, "driver", driver, &error_abort);
911 qemu_opt_set(opts, "property", property, &error_abort);
912 qemu_opt_set(opts, "value", str + offset + 1, &error_abort);
913 return 0;
914 }
915
Markus Armbruster70b94332015-02-13 12:50:26 +0100916 opts = qemu_opts_parse_noisily(&qemu_global_opts, str, false);
Paolo Bonzini3751d7c2015-04-09 14:16:19 +0200917 if (!opts) {
Paolo Bonzini4d454572012-11-26 16:03:42 +0100918 return -1;
919 }
920
Paolo Bonzini4d454572012-11-26 16:03:42 +0100921 return 0;
922}