qemu-option: parse_option_bool(): use error_set()
[qemu/ar7.git] / hw / qdev-monitor.c
blobeed781d2f08b85c521864ffde0424559f15a1b2c
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 "qdev.h"
21 #include "monitor.h"
22 #include "qmp-commands.h"
25 * Aliases were a bad idea from the start. Let's keep them
26 * from spreading further.
28 typedef struct QDevAlias
30 const char *typename;
31 const char *alias;
32 } QDevAlias;
34 static const QDevAlias qdev_alias_table[] = {
35 { "virtio-blk-pci", "virtio-blk" },
36 { "virtio-net-pci", "virtio-net" },
37 { "virtio-serial-pci", "virtio-serial" },
38 { "virtio-balloon-pci", "virtio-balloon" },
39 { "virtio-blk-s390", "virtio-blk" },
40 { "virtio-net-s390", "virtio-net" },
41 { "virtio-serial-s390", "virtio-serial" },
42 { "lsi53c895a", "lsi" },
43 { "ich9-ahci", "ahci" },
44 { }
47 static const char *qdev_class_get_alias(DeviceClass *dc)
49 const char *typename = object_class_get_name(OBJECT_CLASS(dc));
50 int i;
52 for (i = 0; qdev_alias_table[i].typename; i++) {
53 if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
54 return qdev_alias_table[i].alias;
58 return NULL;
61 static bool qdev_class_has_alias(DeviceClass *dc)
63 return (qdev_class_get_alias(dc) != NULL);
66 static void qdev_print_devinfo(ObjectClass *klass, void *opaque)
68 DeviceClass *dc;
69 bool *show_no_user = opaque;
71 dc = (DeviceClass *)object_class_dynamic_cast(klass, TYPE_DEVICE);
73 if (!dc || (show_no_user && !*show_no_user && dc->no_user)) {
74 return;
77 error_printf("name \"%s\"", object_class_get_name(klass));
78 if (dc->bus_info) {
79 error_printf(", bus %s", dc->bus_info->name);
81 if (qdev_class_has_alias(dc)) {
82 error_printf(", alias \"%s\"", qdev_class_get_alias(dc));
84 if (dc->desc) {
85 error_printf(", desc \"%s\"", dc->desc);
87 if (dc->no_user) {
88 error_printf(", no-user");
90 error_printf("\n");
93 static int set_property(const char *name, const char *value, void *opaque)
95 DeviceState *dev = opaque;
97 if (strcmp(name, "driver") == 0)
98 return 0;
99 if (strcmp(name, "bus") == 0)
100 return 0;
102 if (qdev_prop_parse(dev, name, value) == -1) {
103 return -1;
105 return 0;
108 static const char *find_typename_by_alias(const char *alias)
110 int i;
112 for (i = 0; qdev_alias_table[i].alias; i++) {
113 if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
114 return qdev_alias_table[i].typename;
118 return NULL;
121 int qdev_device_help(QemuOpts *opts)
123 const char *driver;
124 Property *prop;
125 ObjectClass *klass;
126 DeviceClass *info;
128 driver = qemu_opt_get(opts, "driver");
129 if (driver && !strcmp(driver, "?")) {
130 bool show_no_user = false;
131 object_class_foreach(qdev_print_devinfo, TYPE_DEVICE, false, &show_no_user);
132 return 1;
135 if (!driver || !qemu_opt_get(opts, "?")) {
136 return 0;
139 klass = object_class_by_name(driver);
140 if (!klass) {
141 const char *typename = find_typename_by_alias(driver);
143 if (typename) {
144 driver = typename;
145 klass = object_class_by_name(driver);
149 if (!klass) {
150 return 0;
152 info = DEVICE_CLASS(klass);
154 for (prop = info->props; prop && prop->name; prop++) {
156 * TODO Properties without a parser are just for dirty hacks.
157 * qdev_prop_ptr is the only such PropertyInfo. It's marked
158 * for removal. This conditional should be removed along with
159 * it.
161 if (!prop->info->set) {
162 continue; /* no way to set it, don't show */
164 error_printf("%s.%s=%s\n", driver, prop->name,
165 prop->info->legacy_name ?: prop->info->name);
167 if (info->bus_info) {
168 for (prop = info->bus_info->props; prop && prop->name; prop++) {
169 if (!prop->info->set) {
170 continue; /* no way to set it, don't show */
172 error_printf("%s.%s=%s\n", driver, prop->name,
173 prop->info->legacy_name ?: prop->info->name);
176 return 1;
179 static Object *qdev_get_peripheral(void)
181 static Object *dev;
183 if (dev == NULL) {
184 dev = container_get(qdev_get_machine(), "/peripheral");
187 return dev;
190 static Object *qdev_get_peripheral_anon(void)
192 static Object *dev;
194 if (dev == NULL) {
195 dev = container_get(qdev_get_machine(), "/peripheral-anon");
198 return dev;
201 static void qbus_list_bus(DeviceState *dev)
203 BusState *child;
204 const char *sep = " ";
206 error_printf("child busses at \"%s\":",
207 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
208 QLIST_FOREACH(child, &dev->child_bus, sibling) {
209 error_printf("%s\"%s\"", sep, child->name);
210 sep = ", ";
212 error_printf("\n");
215 static void qbus_list_dev(BusState *bus)
217 DeviceState *dev;
218 const char *sep = " ";
220 error_printf("devices at \"%s\":", bus->name);
221 QTAILQ_FOREACH(dev, &bus->children, sibling) {
222 error_printf("%s\"%s\"", sep, object_get_typename(OBJECT(dev)));
223 if (dev->id)
224 error_printf("/\"%s\"", dev->id);
225 sep = ", ";
227 error_printf("\n");
230 static BusState *qbus_find_bus(DeviceState *dev, char *elem)
232 BusState *child;
234 QLIST_FOREACH(child, &dev->child_bus, sibling) {
235 if (strcmp(child->name, elem) == 0) {
236 return child;
239 return NULL;
242 static DeviceState *qbus_find_dev(BusState *bus, char *elem)
244 DeviceState *dev;
247 * try to match in order:
248 * (1) instance id, if present
249 * (2) driver name
250 * (3) driver alias, if present
252 QTAILQ_FOREACH(dev, &bus->children, sibling) {
253 if (dev->id && strcmp(dev->id, elem) == 0) {
254 return dev;
257 QTAILQ_FOREACH(dev, &bus->children, sibling) {
258 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
259 return dev;
262 QTAILQ_FOREACH(dev, &bus->children, sibling) {
263 DeviceClass *dc = DEVICE_GET_CLASS(dev);
265 if (qdev_class_has_alias(dc) &&
266 strcmp(qdev_class_get_alias(dc), elem) == 0) {
267 return dev;
270 return NULL;
273 static BusState *qbus_find_recursive(BusState *bus, const char *name,
274 const BusInfo *info)
276 DeviceState *dev;
277 BusState *child, *ret;
278 int match = 1;
280 if (name && (strcmp(bus->name, name) != 0)) {
281 match = 0;
283 if (info && (bus->info != info)) {
284 match = 0;
286 if (match) {
287 return bus;
290 QTAILQ_FOREACH(dev, &bus->children, sibling) {
291 QLIST_FOREACH(child, &dev->child_bus, sibling) {
292 ret = qbus_find_recursive(child, name, info);
293 if (ret) {
294 return ret;
298 return NULL;
301 static BusState *qbus_find(const char *path)
303 DeviceState *dev;
304 BusState *bus;
305 char elem[128];
306 int pos, len;
308 /* find start element */
309 if (path[0] == '/') {
310 bus = sysbus_get_default();
311 pos = 0;
312 } else {
313 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
314 assert(!path[0]);
315 elem[0] = len = 0;
317 bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
318 if (!bus) {
319 qerror_report(QERR_BUS_NOT_FOUND, elem);
320 return NULL;
322 pos = len;
325 for (;;) {
326 assert(path[pos] == '/' || !path[pos]);
327 while (path[pos] == '/') {
328 pos++;
330 if (path[pos] == '\0') {
331 return bus;
334 /* find device */
335 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
336 assert(0);
337 elem[0] = len = 0;
339 pos += len;
340 dev = qbus_find_dev(bus, elem);
341 if (!dev) {
342 qerror_report(QERR_DEVICE_NOT_FOUND, elem);
343 if (!monitor_cur_is_qmp()) {
344 qbus_list_dev(bus);
346 return NULL;
349 assert(path[pos] == '/' || !path[pos]);
350 while (path[pos] == '/') {
351 pos++;
353 if (path[pos] == '\0') {
354 /* last specified element is a device. If it has exactly
355 * one child bus accept it nevertheless */
356 switch (dev->num_child_bus) {
357 case 0:
358 qerror_report(QERR_DEVICE_NO_BUS, elem);
359 return NULL;
360 case 1:
361 return QLIST_FIRST(&dev->child_bus);
362 default:
363 qerror_report(QERR_DEVICE_MULTIPLE_BUSSES, elem);
364 if (!monitor_cur_is_qmp()) {
365 qbus_list_bus(dev);
367 return NULL;
371 /* find bus */
372 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
373 assert(0);
374 elem[0] = len = 0;
376 pos += len;
377 bus = qbus_find_bus(dev, elem);
378 if (!bus) {
379 qerror_report(QERR_BUS_NOT_FOUND, elem);
380 if (!monitor_cur_is_qmp()) {
381 qbus_list_bus(dev);
383 return NULL;
388 DeviceState *qdev_device_add(QemuOpts *opts)
390 ObjectClass *obj;
391 DeviceClass *k;
392 const char *driver, *path, *id;
393 DeviceState *qdev;
394 BusState *bus;
396 driver = qemu_opt_get(opts, "driver");
397 if (!driver) {
398 qerror_report(QERR_MISSING_PARAMETER, "driver");
399 return NULL;
402 /* find driver */
403 obj = object_class_by_name(driver);
404 if (!obj) {
405 const char *typename = find_typename_by_alias(driver);
407 if (typename) {
408 driver = typename;
409 obj = object_class_by_name(driver);
413 if (!obj) {
414 qerror_report(QERR_INVALID_PARAMETER_VALUE, "driver", "device type");
415 return NULL;
418 k = DEVICE_CLASS(obj);
420 /* find bus */
421 path = qemu_opt_get(opts, "bus");
422 if (path != NULL) {
423 bus = qbus_find(path);
424 if (!bus) {
425 return NULL;
427 if (bus->info != k->bus_info) {
428 qerror_report(QERR_BAD_BUS_FOR_DEVICE,
429 driver, bus->info->name);
430 return NULL;
432 } else {
433 bus = qbus_find_recursive(sysbus_get_default(), NULL, k->bus_info);
434 if (!bus) {
435 qerror_report(QERR_NO_BUS_FOR_DEVICE,
436 driver, k->bus_info->name);
437 return NULL;
440 if (qdev_hotplug && !bus->allow_hotplug) {
441 qerror_report(QERR_BUS_NO_HOTPLUG, bus->name);
442 return NULL;
445 if (!bus) {
446 bus = sysbus_get_default();
449 /* create device, set properties */
450 qdev = DEVICE(object_new(driver));
451 qdev_set_parent_bus(qdev, bus);
452 qdev_prop_set_globals(qdev);
454 id = qemu_opts_id(opts);
455 if (id) {
456 qdev->id = id;
458 if (qemu_opt_foreach(opts, set_property, qdev, 1) != 0) {
459 qdev_free(qdev);
460 return NULL;
462 if (qdev->id) {
463 object_property_add_child(qdev_get_peripheral(), qdev->id,
464 OBJECT(qdev), NULL);
465 } else {
466 static int anon_count;
467 gchar *name = g_strdup_printf("device[%d]", anon_count++);
468 object_property_add_child(qdev_get_peripheral_anon(), name,
469 OBJECT(qdev), NULL);
470 g_free(name);
472 if (qdev_init(qdev) < 0) {
473 qerror_report(QERR_DEVICE_INIT_FAILED, driver);
474 return NULL;
476 qdev->opts = opts;
477 return qdev;
481 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
482 static void qbus_print(Monitor *mon, BusState *bus, int indent);
484 static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
485 const char *prefix, int indent)
487 if (!props)
488 return;
489 for (; props->name; props++) {
490 Error *err = NULL;
491 char *value;
492 char *legacy_name = g_strdup_printf("legacy-%s", props->name);
493 if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
494 value = object_property_get_str(OBJECT(dev), legacy_name, &err);
495 } else {
496 value = object_property_print(OBJECT(dev), props->name, &err);
498 g_free(legacy_name);
500 if (err) {
501 error_free(err);
502 continue;
504 qdev_printf("%s-prop: %s = %s\n", prefix, props->name,
505 value && *value ? value : "<null>");
506 g_free(value);
510 static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
512 BusState *child;
513 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
514 dev->id ? dev->id : "");
515 indent += 2;
516 if (dev->num_gpio_in) {
517 qdev_printf("gpio-in %d\n", dev->num_gpio_in);
519 if (dev->num_gpio_out) {
520 qdev_printf("gpio-out %d\n", dev->num_gpio_out);
522 qdev_print_props(mon, dev, qdev_get_props(dev), "dev", indent);
523 qdev_print_props(mon, dev, dev->parent_bus->info->props, "bus", indent);
524 if (dev->parent_bus->info->print_dev)
525 dev->parent_bus->info->print_dev(mon, dev, indent);
526 QLIST_FOREACH(child, &dev->child_bus, sibling) {
527 qbus_print(mon, child, indent);
531 static void qbus_print(Monitor *mon, BusState *bus, int indent)
533 struct DeviceState *dev;
535 qdev_printf("bus: %s\n", bus->name);
536 indent += 2;
537 qdev_printf("type %s\n", bus->info->name);
538 QTAILQ_FOREACH(dev, &bus->children, sibling) {
539 qdev_print(mon, dev, indent);
542 #undef qdev_printf
544 void do_info_qtree(Monitor *mon)
546 if (sysbus_get_default())
547 qbus_print(mon, sysbus_get_default(), 0);
550 void do_info_qdm(Monitor *mon)
552 object_class_foreach(qdev_print_devinfo, TYPE_DEVICE, false, NULL);
555 int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
557 QemuOpts *opts;
559 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict);
560 if (!opts) {
561 return -1;
563 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
564 qemu_opts_del(opts);
565 return 0;
567 if (!qdev_device_add(opts)) {
568 qemu_opts_del(opts);
569 return -1;
571 return 0;
574 void qmp_device_del(const char *id, Error **errp)
576 DeviceState *dev;
578 dev = qdev_find_recursive(sysbus_get_default(), id);
579 if (NULL == dev) {
580 error_set(errp, QERR_DEVICE_NOT_FOUND, id);
581 return;
584 qdev_unplug(dev, errp);
587 void qdev_machine_init(void)
589 qdev_get_peripheral_anon();
590 qdev_get_peripheral();