blob: 2cb5600d635402aeee9d60313ea8ad2c341d0cb6 [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
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010020#include "hw/qdev.h"
Andreas Färber2f7bd822013-04-16 03:50:21 +020021#include "hw/sysbus.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010022#include "monitor/monitor.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010023#include "monitor/qdev.h"
Luiz Capitulinoa15fef22012-03-29 12:38:50 -030024#include "qmp-commands.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010025#include "sysemu/arch_init.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010026#include "qemu/config-file.h"
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060027
28/*
29 * Aliases were a bad idea from the start. Let's keep them
30 * from spreading further.
31 */
32typedef struct QDevAlias
33{
34 const char *typename;
35 const char *alias;
Alexander Graf5f629d92012-05-18 02:36:26 +020036 uint32_t arch_mask;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060037} QDevAlias;
38
39static const QDevAlias qdev_alias_table[] = {
Alexander Graf5f629d92012-05-18 02:36:26 +020040 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
41 { "virtio-net-pci", "virtio-net", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
42 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
43 { "virtio-balloon-pci", "virtio-balloon",
44 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
45 { "virtio-blk-s390", "virtio-blk", QEMU_ARCH_S390X },
46 { "virtio-net-s390", "virtio-net", QEMU_ARCH_S390X },
47 { "virtio-serial-s390", "virtio-serial", QEMU_ARCH_S390X },
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060048 { "lsi53c895a", "lsi" },
49 { "ich9-ahci", "ahci" },
Jan Kiszkac3ebd3b2012-08-30 20:30:00 +020050 { "kvm-pci-assign", "pci-assign" },
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060051 { }
52};
53
54static const char *qdev_class_get_alias(DeviceClass *dc)
55{
56 const char *typename = object_class_get_name(OBJECT_CLASS(dc));
57 int i;
58
59 for (i = 0; qdev_alias_table[i].typename; i++) {
Alexander Graf5f629d92012-05-18 02:36:26 +020060 if (qdev_alias_table[i].arch_mask &&
61 !(qdev_alias_table[i].arch_mask & arch_type)) {
62 continue;
63 }
64
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060065 if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
66 return qdev_alias_table[i].alias;
67 }
68 }
69
70 return NULL;
71}
72
73static bool qdev_class_has_alias(DeviceClass *dc)
74{
75 return (qdev_class_get_alias(dc) != NULL);
76}
77
78static void qdev_print_devinfo(ObjectClass *klass, void *opaque)
79{
80 DeviceClass *dc;
81 bool *show_no_user = opaque;
82
83 dc = (DeviceClass *)object_class_dynamic_cast(klass, TYPE_DEVICE);
84
85 if (!dc || (show_no_user && !*show_no_user && dc->no_user)) {
86 return;
87 }
88
89 error_printf("name \"%s\"", object_class_get_name(klass));
Anthony Liguori0d936922012-05-02 09:00:20 +020090 if (dc->bus_type) {
91 error_printf(", bus %s", dc->bus_type);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060092 }
93 if (qdev_class_has_alias(dc)) {
94 error_printf(", alias \"%s\"", qdev_class_get_alias(dc));
95 }
96 if (dc->desc) {
97 error_printf(", desc \"%s\"", dc->desc);
98 }
99 if (dc->no_user) {
100 error_printf(", no-user");
101 }
102 error_printf("\n");
103}
104
105static int set_property(const char *name, const char *value, void *opaque)
106{
107 DeviceState *dev = opaque;
108
109 if (strcmp(name, "driver") == 0)
110 return 0;
111 if (strcmp(name, "bus") == 0)
112 return 0;
113
114 if (qdev_prop_parse(dev, name, value) == -1) {
115 return -1;
116 }
117 return 0;
118}
119
120static const char *find_typename_by_alias(const char *alias)
121{
122 int i;
123
124 for (i = 0; qdev_alias_table[i].alias; i++) {
Alexander Graf5f629d92012-05-18 02:36:26 +0200125 if (qdev_alias_table[i].arch_mask &&
126 !(qdev_alias_table[i].arch_mask & arch_type)) {
127 continue;
128 }
129
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600130 if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
131 return qdev_alias_table[i].typename;
132 }
133 }
134
135 return NULL;
136}
137
138int qdev_device_help(QemuOpts *opts)
139{
140 const char *driver;
141 Property *prop;
142 ObjectClass *klass;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600143
144 driver = qemu_opt_get(opts, "driver");
Peter Maydellc8057f92012-08-02 13:45:54 +0100145 if (driver && is_help_option(driver)) {
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600146 bool show_no_user = false;
147 object_class_foreach(qdev_print_devinfo, TYPE_DEVICE, false, &show_no_user);
148 return 1;
149 }
150
Peter Maydellc8057f92012-08-02 13:45:54 +0100151 if (!driver || !qemu_opt_has_help_opt(opts)) {
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600152 return 0;
153 }
154
155 klass = object_class_by_name(driver);
156 if (!klass) {
157 const char *typename = find_typename_by_alias(driver);
158
159 if (typename) {
160 driver = typename;
161 klass = object_class_by_name(driver);
162 }
163 }
164
165 if (!klass) {
166 return 0;
167 }
Paolo Bonzinibce54472012-03-28 18:12:47 +0200168 do {
169 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
170 /*
171 * TODO Properties without a parser are just for dirty hacks.
172 * qdev_prop_ptr is the only such PropertyInfo. It's marked
173 * for removal. This conditional should be removed along with
174 * it.
175 */
Paolo Bonzini90ca64a2012-05-02 13:30:59 +0200176 if (!prop->info->set) {
Anthony Liguorid03d6b42011-12-23 08:16:58 -0600177 continue; /* no way to set it, don't show */
178 }
179 error_printf("%s.%s=%s\n", driver, prop->name,
180 prop->info->legacy_name ?: prop->info->name);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600181 }
Paolo Bonzinibce54472012-03-28 18:12:47 +0200182 klass = object_class_get_parent(klass);
183 } while (klass != object_class_by_name(TYPE_DEVICE));
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600184 return 1;
185}
186
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600187static Object *qdev_get_peripheral(void)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600188{
Anthony Liguori8b45d442011-12-23 09:08:05 -0600189 static Object *dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600190
191 if (dev == NULL) {
Andreas Färberdfe47e72012-04-05 13:21:46 +0200192 dev = container_get(qdev_get_machine(), "/peripheral");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600193 }
194
Anthony Liguori8b45d442011-12-23 09:08:05 -0600195 return dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600196}
197
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600198static Object *qdev_get_peripheral_anon(void)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600199{
Anthony Liguori8b45d442011-12-23 09:08:05 -0600200 static Object *dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600201
202 if (dev == NULL) {
Andreas Färberdfe47e72012-04-05 13:21:46 +0200203 dev = container_get(qdev_get_machine(), "/peripheral-anon");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600204 }
205
Anthony Liguori8b45d442011-12-23 09:08:05 -0600206 return dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600207}
208
209static void qbus_list_bus(DeviceState *dev)
210{
211 BusState *child;
212 const char *sep = " ";
213
214 error_printf("child busses at \"%s\":",
215 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
216 QLIST_FOREACH(child, &dev->child_bus, sibling) {
217 error_printf("%s\"%s\"", sep, child->name);
218 sep = ", ";
219 }
220 error_printf("\n");
221}
222
223static void qbus_list_dev(BusState *bus)
224{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600225 BusChild *kid;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600226 const char *sep = " ";
227
228 error_printf("devices at \"%s\":", bus->name);
Anthony Liguori0866aca2011-12-23 15:34:39 -0600229 QTAILQ_FOREACH(kid, &bus->children, sibling) {
230 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600231 error_printf("%s\"%s\"", sep, object_get_typename(OBJECT(dev)));
232 if (dev->id)
233 error_printf("/\"%s\"", dev->id);
234 sep = ", ";
235 }
236 error_printf("\n");
237}
238
239static BusState *qbus_find_bus(DeviceState *dev, char *elem)
240{
241 BusState *child;
242
243 QLIST_FOREACH(child, &dev->child_bus, sibling) {
244 if (strcmp(child->name, elem) == 0) {
245 return child;
246 }
247 }
248 return NULL;
249}
250
251static DeviceState *qbus_find_dev(BusState *bus, char *elem)
252{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600253 BusChild *kid;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600254
255 /*
256 * try to match in order:
257 * (1) instance id, if present
258 * (2) driver name
259 * (3) driver alias, if present
260 */
Anthony Liguori0866aca2011-12-23 15:34:39 -0600261 QTAILQ_FOREACH(kid, &bus->children, sibling) {
262 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600263 if (dev->id && strcmp(dev->id, elem) == 0) {
264 return dev;
265 }
266 }
Anthony Liguori0866aca2011-12-23 15:34:39 -0600267 QTAILQ_FOREACH(kid, &bus->children, sibling) {
268 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600269 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
270 return dev;
271 }
272 }
Anthony Liguori0866aca2011-12-23 15:34:39 -0600273 QTAILQ_FOREACH(kid, &bus->children, sibling) {
274 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600275 DeviceClass *dc = DEVICE_GET_CLASS(dev);
276
277 if (qdev_class_has_alias(dc) &&
278 strcmp(qdev_class_get_alias(dc), elem) == 0) {
279 return dev;
280 }
281 }
282 return NULL;
283}
284
285static BusState *qbus_find_recursive(BusState *bus, const char *name,
Anthony Liguori0d936922012-05-02 09:00:20 +0200286 const char *bus_typename)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600287{
KONRAD Frederic1395af62013-01-15 00:08:00 +0100288 BusClass *bus_class = BUS_GET_CLASS(bus);
Anthony Liguori0866aca2011-12-23 15:34:39 -0600289 BusChild *kid;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600290 BusState *child, *ret;
291 int match = 1;
292
293 if (name && (strcmp(bus->name, name) != 0)) {
294 match = 0;
Alexey Kardashevskiy95e2af92013-04-17 17:49:00 +1000295 } else if (bus_typename && !object_dynamic_cast(OBJECT(bus), bus_typename)) {
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600296 match = 0;
Alexey Kardashevskiy95e2af92013-04-17 17:49:00 +1000297 } else if ((bus_class->max_dev != 0) && (bus_class->max_dev <= bus->max_index)) {
KONRAD Frederic1395af62013-01-15 00:08:00 +0100298 if (name != NULL) {
299 /* bus was explicitly specified: return an error. */
300 qerror_report(ERROR_CLASS_GENERIC_ERROR, "Bus '%s' is full",
301 bus->name);
302 return NULL;
303 } else {
304 /* bus was not specified: try to find another one. */
305 match = 0;
306 }
307 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600308 if (match) {
309 return bus;
310 }
311
Anthony Liguori0866aca2011-12-23 15:34:39 -0600312 QTAILQ_FOREACH(kid, &bus->children, sibling) {
313 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600314 QLIST_FOREACH(child, &dev->child_bus, sibling) {
Anthony Liguori0d936922012-05-02 09:00:20 +0200315 ret = qbus_find_recursive(child, name, bus_typename);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600316 if (ret) {
317 return ret;
318 }
319 }
320 }
321 return NULL;
322}
323
324static BusState *qbus_find(const char *path)
325{
326 DeviceState *dev;
327 BusState *bus;
328 char elem[128];
329 int pos, len;
330
331 /* find start element */
332 if (path[0] == '/') {
333 bus = sysbus_get_default();
334 pos = 0;
335 } else {
336 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
337 assert(!path[0]);
338 elem[0] = len = 0;
339 }
340 bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
341 if (!bus) {
342 qerror_report(QERR_BUS_NOT_FOUND, elem);
343 return NULL;
344 }
345 pos = len;
346 }
347
348 for (;;) {
349 assert(path[pos] == '/' || !path[pos]);
350 while (path[pos] == '/') {
351 pos++;
352 }
353 if (path[pos] == '\0') {
354 return bus;
355 }
356
357 /* find device */
358 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
359 assert(0);
360 elem[0] = len = 0;
361 }
362 pos += len;
363 dev = qbus_find_dev(bus, elem);
364 if (!dev) {
365 qerror_report(QERR_DEVICE_NOT_FOUND, elem);
366 if (!monitor_cur_is_qmp()) {
367 qbus_list_dev(bus);
368 }
369 return NULL;
370 }
371
372 assert(path[pos] == '/' || !path[pos]);
373 while (path[pos] == '/') {
374 pos++;
375 }
376 if (path[pos] == '\0') {
377 /* last specified element is a device. If it has exactly
378 * one child bus accept it nevertheless */
379 switch (dev->num_child_bus) {
380 case 0:
381 qerror_report(QERR_DEVICE_NO_BUS, elem);
382 return NULL;
383 case 1:
384 return QLIST_FIRST(&dev->child_bus);
385 default:
386 qerror_report(QERR_DEVICE_MULTIPLE_BUSSES, elem);
387 if (!monitor_cur_is_qmp()) {
388 qbus_list_bus(dev);
389 }
390 return NULL;
391 }
392 }
393
394 /* find bus */
395 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
396 assert(0);
397 elem[0] = len = 0;
398 }
399 pos += len;
400 bus = qbus_find_bus(dev, elem);
401 if (!bus) {
402 qerror_report(QERR_BUS_NOT_FOUND, elem);
403 if (!monitor_cur_is_qmp()) {
404 qbus_list_bus(dev);
405 }
406 return NULL;
407 }
408 }
409}
410
411DeviceState *qdev_device_add(QemuOpts *opts)
412{
413 ObjectClass *obj;
414 DeviceClass *k;
415 const char *driver, *path, *id;
416 DeviceState *qdev;
Andreas Färber2f7bd822013-04-16 03:50:21 +0200417 BusState *bus = NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600418
419 driver = qemu_opt_get(opts, "driver");
420 if (!driver) {
421 qerror_report(QERR_MISSING_PARAMETER, "driver");
422 return NULL;
423 }
424
425 /* find driver */
426 obj = object_class_by_name(driver);
427 if (!obj) {
428 const char *typename = find_typename_by_alias(driver);
429
430 if (typename) {
431 driver = typename;
432 obj = object_class_by_name(driver);
433 }
434 }
435
436 if (!obj) {
437 qerror_report(QERR_INVALID_PARAMETER_VALUE, "driver", "device type");
438 return NULL;
439 }
440
441 k = DEVICE_CLASS(obj);
442
443 /* find bus */
444 path = qemu_opt_get(opts, "bus");
445 if (path != NULL) {
446 bus = qbus_find(path);
447 if (!bus) {
448 return NULL;
449 }
Anthony Liguorie912c962012-11-29 07:46:23 -0600450 if (!object_dynamic_cast(OBJECT(bus), k->bus_type)) {
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600451 qerror_report(QERR_BAD_BUS_FOR_DEVICE,
Anthony Liguori0d936922012-05-02 09:00:20 +0200452 driver, object_get_typename(OBJECT(bus)));
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600453 return NULL;
454 }
Andreas Färber2f7bd822013-04-16 03:50:21 +0200455 } else if (k->bus_type != NULL) {
Anthony Liguori0d936922012-05-02 09:00:20 +0200456 bus = qbus_find_recursive(sysbus_get_default(), NULL, k->bus_type);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600457 if (!bus) {
458 qerror_report(QERR_NO_BUS_FOR_DEVICE,
Alberto Garciac3594ed2012-08-14 14:41:28 +0300459 k->bus_type, driver);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600460 return NULL;
461 }
462 }
Andreas Färber2f7bd822013-04-16 03:50:21 +0200463 if (qdev_hotplug && bus && !bus->allow_hotplug) {
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600464 qerror_report(QERR_BUS_NO_HOTPLUG, bus->name);
465 return NULL;
466 }
467
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600468 /* create device, set properties */
469 qdev = DEVICE(object_new(driver));
Andreas Färber2f7bd822013-04-16 03:50:21 +0200470
471 if (bus) {
472 qdev_set_parent_bus(qdev, bus);
473 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600474
475 id = qemu_opts_id(opts);
476 if (id) {
477 qdev->id = id;
Anthony Liguorib2d4b3f2012-02-12 11:36:24 -0600478 }
479 if (qemu_opt_foreach(opts, set_property, qdev, 1) != 0) {
480 qdev_free(qdev);
481 return NULL;
482 }
Anthony Liguorib2d4b3f2012-02-12 11:36:24 -0600483 if (qdev->id) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600484 object_property_add_child(qdev_get_peripheral(), qdev->id,
485 OBJECT(qdev), NULL);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600486 } else {
487 static int anon_count;
488 gchar *name = g_strdup_printf("device[%d]", anon_count++);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600489 object_property_add_child(qdev_get_peripheral_anon(), name,
490 OBJECT(qdev), NULL);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600491 g_free(name);
492 }
Paolo Bonzinif424d5c2012-03-27 18:38:46 +0200493 if (qdev_init(qdev) < 0) {
494 qerror_report(QERR_DEVICE_INIT_FAILED, driver);
495 return NULL;
496 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600497 qdev->opts = opts;
498 return qdev;
499}
500
501
502#define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
503static void qbus_print(Monitor *mon, BusState *bus, int indent);
504
505static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
Paolo Bonzinibce54472012-03-28 18:12:47 +0200506 int indent)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600507{
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600508 if (!props)
509 return;
Paolo Bonzinid8229792012-02-02 09:47:13 +0100510 for (; props->name; props++) {
511 Error *err = NULL;
512 char *value;
513 char *legacy_name = g_strdup_printf("legacy-%s", props->name);
514 if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
515 value = object_property_get_str(OBJECT(dev), legacy_name, &err);
516 } else {
Paolo Bonzini8185bfc2012-05-02 13:31:00 +0200517 value = object_property_print(OBJECT(dev), props->name, &err);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600518 }
Paolo Bonzinid8229792012-02-02 09:47:13 +0100519 g_free(legacy_name);
520
521 if (err) {
522 error_free(err);
523 continue;
524 }
Paolo Bonzinibce54472012-03-28 18:12:47 +0200525 qdev_printf("%s = %s\n", props->name,
Paolo Bonzinid8229792012-02-02 09:47:13 +0100526 value && *value ? value : "<null>");
527 g_free(value);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600528 }
529}
530
Anthony Liguori0d936922012-05-02 09:00:20 +0200531static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
532{
533 BusClass *bc = BUS_GET_CLASS(bus);
534
535 if (bc->print_dev) {
536 bc->print_dev(mon, dev, indent);
537 }
538}
539
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600540static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
541{
Paolo Bonzinibce54472012-03-28 18:12:47 +0200542 ObjectClass *class;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600543 BusState *child;
544 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
545 dev->id ? dev->id : "");
546 indent += 2;
547 if (dev->num_gpio_in) {
548 qdev_printf("gpio-in %d\n", dev->num_gpio_in);
549 }
550 if (dev->num_gpio_out) {
551 qdev_printf("gpio-out %d\n", dev->num_gpio_out);
552 }
Paolo Bonzinibce54472012-03-28 18:12:47 +0200553 class = object_get_class(OBJECT(dev));
554 do {
555 qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent);
556 class = object_class_get_parent(class);
557 } while (class != object_class_by_name(TYPE_DEVICE));
Gerd Hoffmannda9fbe72012-07-11 12:21:23 +0200558 bus_print_dev(dev->parent_bus, mon, dev, indent);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600559 QLIST_FOREACH(child, &dev->child_bus, sibling) {
560 qbus_print(mon, child, indent);
561 }
562}
563
564static void qbus_print(Monitor *mon, BusState *bus, int indent)
565{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600566 BusChild *kid;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600567
568 qdev_printf("bus: %s\n", bus->name);
569 indent += 2;
Anthony Liguori0d936922012-05-02 09:00:20 +0200570 qdev_printf("type %s\n", object_get_typename(OBJECT(bus)));
Anthony Liguori0866aca2011-12-23 15:34:39 -0600571 QTAILQ_FOREACH(kid, &bus->children, sibling) {
572 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600573 qdev_print(mon, dev, indent);
574 }
575}
576#undef qdev_printf
577
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800578void do_info_qtree(Monitor *mon, const QDict *qdict)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600579{
580 if (sysbus_get_default())
581 qbus_print(mon, sysbus_get_default(), 0);
582}
583
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800584void do_info_qdm(Monitor *mon, const QDict *qdict)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600585{
586 object_class_foreach(qdev_print_devinfo, TYPE_DEVICE, false, NULL);
587}
588
589int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
590{
Luiz Capitulino4e899782012-04-18 17:24:01 -0300591 Error *local_err = NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600592 QemuOpts *opts;
Paolo Bonzinib09995a2013-01-25 14:12:37 +0100593 DeviceState *dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600594
Luiz Capitulino4e899782012-04-18 17:24:01 -0300595 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, &local_err);
596 if (error_is_set(&local_err)) {
597 qerror_report_err(local_err);
598 error_free(local_err);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600599 return -1;
600 }
601 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
602 qemu_opts_del(opts);
603 return 0;
604 }
Paolo Bonzinib09995a2013-01-25 14:12:37 +0100605 dev = qdev_device_add(opts);
606 if (!dev) {
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600607 qemu_opts_del(opts);
608 return -1;
609 }
Paolo Bonzinib09995a2013-01-25 14:12:37 +0100610 object_unref(OBJECT(dev));
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600611 return 0;
612}
613
Luiz Capitulinoa15fef22012-03-29 12:38:50 -0300614void qmp_device_del(const char *id, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600615{
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600616 DeviceState *dev;
617
618 dev = qdev_find_recursive(sysbus_get_default(), id);
619 if (NULL == dev) {
Luiz Capitulinoa15fef22012-03-29 12:38:50 -0300620 error_set(errp, QERR_DEVICE_NOT_FOUND, id);
621 return;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600622 }
Luiz Capitulino56f91072012-03-14 17:37:38 -0300623
Luiz Capitulinoa15fef22012-03-29 12:38:50 -0300624 qdev_unplug(dev, errp);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600625}
626
627void qdev_machine_init(void)
628{
629 qdev_get_peripheral_anon();
630 qdev_get_peripheral();
631}
Paolo Bonzini4d454572012-11-26 16:03:42 +0100632
633QemuOptsList qemu_device_opts = {
634 .name = "device",
635 .implied_opt_name = "driver",
636 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
637 .desc = {
638 /*
639 * no elements => accept any
640 * sanity checking will happen later
641 * when setting device properties
642 */
643 { /* end of list */ }
644 },
645};
646
647QemuOptsList qemu_global_opts = {
648 .name = "global",
649 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
650 .desc = {
651 {
652 .name = "driver",
653 .type = QEMU_OPT_STRING,
654 },{
655 .name = "property",
656 .type = QEMU_OPT_STRING,
657 },{
658 .name = "value",
659 .type = QEMU_OPT_STRING,
660 },
661 { /* end of list */ }
662 },
663};
664
665int qemu_global_option(const char *str)
666{
667 char driver[64], property[64];
668 QemuOpts *opts;
669 int rc, offset;
670
671 rc = sscanf(str, "%63[^.].%63[^=]%n", driver, property, &offset);
672 if (rc < 2 || str[offset] != '=') {
673 error_report("can't parse: \"%s\"", str);
674 return -1;
675 }
676
677 opts = qemu_opts_create_nofail(&qemu_global_opts);
678 qemu_opt_set(opts, "driver", driver);
679 qemu_opt_set(opts, "property", property);
680 qemu_opt_set(opts, "value", str+offset+1);
681 return 0;
682}