]>
Commit | Line | Data |
---|---|---|
ee46d8a5 AL |
1 | /* |
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 | ||
b4a42f81 | 20 | #include "hw/qdev.h" |
2f7bd829 | 21 | #include "hw/sysbus.h" |
83c9089e | 22 | #include "monitor/monitor.h" |
b4a42f81 | 23 | #include "monitor/qdev.h" |
a15fef21 | 24 | #include "qmp-commands.h" |
9c17d615 | 25 | #include "sysemu/arch_init.h" |
1de7afc9 | 26 | #include "qemu/config-file.h" |
ee46d8a5 AL |
27 | |
28 | /* | |
29 | * Aliases were a bad idea from the start. Let's keep them | |
30 | * from spreading further. | |
31 | */ | |
32 | typedef struct QDevAlias | |
33 | { | |
34 | const char *typename; | |
35 | const char *alias; | |
5f629d94 | 36 | uint32_t arch_mask; |
ee46d8a5 AL |
37 | } QDevAlias; |
38 | ||
39 | static const QDevAlias qdev_alias_table[] = { | |
5f629d94 AG |
40 | { "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 }, | |
ee46d8a5 AL |
48 | { "lsi53c895a", "lsi" }, |
49 | { "ich9-ahci", "ahci" }, | |
c3ebd3ba | 50 | { "kvm-pci-assign", "pci-assign" }, |
ee46d8a5 AL |
51 | { } |
52 | }; | |
53 | ||
54 | static 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++) { | |
5f629d94 AG |
60 | if (qdev_alias_table[i].arch_mask && |
61 | !(qdev_alias_table[i].arch_mask & arch_type)) { | |
62 | continue; | |
63 | } | |
64 | ||
ee46d8a5 AL |
65 | if (strcmp(qdev_alias_table[i].typename, typename) == 0) { |
66 | return qdev_alias_table[i].alias; | |
67 | } | |
68 | } | |
69 | ||
70 | return NULL; | |
71 | } | |
72 | ||
73 | static bool qdev_class_has_alias(DeviceClass *dc) | |
74 | { | |
75 | return (qdev_class_get_alias(dc) != NULL); | |
76 | } | |
77 | ||
78 | static 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)); | |
0d936928 AL |
90 | if (dc->bus_type) { |
91 | error_printf(", bus %s", dc->bus_type); | |
ee46d8a5 AL |
92 | } |
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 | ||
105 | static 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 | ||
120 | static const char *find_typename_by_alias(const char *alias) | |
121 | { | |
122 | int i; | |
123 | ||
124 | for (i = 0; qdev_alias_table[i].alias; i++) { | |
5f629d94 AG |
125 | if (qdev_alias_table[i].arch_mask && |
126 | !(qdev_alias_table[i].arch_mask & arch_type)) { | |
127 | continue; | |
128 | } | |
129 | ||
ee46d8a5 AL |
130 | if (strcmp(qdev_alias_table[i].alias, alias) == 0) { |
131 | return qdev_alias_table[i].typename; | |
132 | } | |
133 | } | |
134 | ||
135 | return NULL; | |
136 | } | |
137 | ||
138 | int qdev_device_help(QemuOpts *opts) | |
139 | { | |
140 | const char *driver; | |
141 | Property *prop; | |
142 | ObjectClass *klass; | |
ee46d8a5 AL |
143 | |
144 | driver = qemu_opt_get(opts, "driver"); | |
c8057f95 | 145 | if (driver && is_help_option(driver)) { |
ee46d8a5 AL |
146 | bool show_no_user = false; |
147 | object_class_foreach(qdev_print_devinfo, TYPE_DEVICE, false, &show_no_user); | |
148 | return 1; | |
149 | } | |
150 | ||
c8057f95 | 151 | if (!driver || !qemu_opt_has_help_opt(opts)) { |
ee46d8a5 AL |
152 | 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 | } | |
bce54474 PB |
168 | 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 | */ | |
90ca64a9 | 176 | if (!prop->info->set) { |
d03d6b4e AL |
177 | 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); | |
ee46d8a5 | 181 | } |
bce54474 PB |
182 | klass = object_class_get_parent(klass); |
183 | } while (klass != object_class_by_name(TYPE_DEVICE)); | |
ee46d8a5 AL |
184 | return 1; |
185 | } | |
186 | ||
57c9fafe | 187 | static Object *qdev_get_peripheral(void) |
ee46d8a5 | 188 | { |
8b45d447 | 189 | static Object *dev; |
ee46d8a5 AL |
190 | |
191 | if (dev == NULL) { | |
dfe47e70 | 192 | dev = container_get(qdev_get_machine(), "/peripheral"); |
ee46d8a5 AL |
193 | } |
194 | ||
8b45d447 | 195 | return dev; |
ee46d8a5 AL |
196 | } |
197 | ||
57c9fafe | 198 | static Object *qdev_get_peripheral_anon(void) |
ee46d8a5 | 199 | { |
8b45d447 | 200 | static Object *dev; |
ee46d8a5 AL |
201 | |
202 | if (dev == NULL) { | |
dfe47e70 | 203 | dev = container_get(qdev_get_machine(), "/peripheral-anon"); |
ee46d8a5 AL |
204 | } |
205 | ||
8b45d447 | 206 | return dev; |
ee46d8a5 AL |
207 | } |
208 | ||
209 | static 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 | ||
223 | static void qbus_list_dev(BusState *bus) | |
224 | { | |
0866aca1 | 225 | BusChild *kid; |
ee46d8a5 AL |
226 | const char *sep = " "; |
227 | ||
228 | error_printf("devices at \"%s\":", bus->name); | |
0866aca1 AL |
229 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
230 | DeviceState *dev = kid->child; | |
ee46d8a5 AL |
231 | 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 | ||
239 | static 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 | ||
251 | static DeviceState *qbus_find_dev(BusState *bus, char *elem) | |
252 | { | |
0866aca1 | 253 | BusChild *kid; |
ee46d8a5 AL |
254 | |
255 | /* | |
256 | * try to match in order: | |
257 | * (1) instance id, if present | |
258 | * (2) driver name | |
259 | * (3) driver alias, if present | |
260 | */ | |
0866aca1 AL |
261 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
262 | DeviceState *dev = kid->child; | |
ee46d8a5 AL |
263 | if (dev->id && strcmp(dev->id, elem) == 0) { |
264 | return dev; | |
265 | } | |
266 | } | |
0866aca1 AL |
267 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
268 | DeviceState *dev = kid->child; | |
ee46d8a5 AL |
269 | if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) { |
270 | return dev; | |
271 | } | |
272 | } | |
0866aca1 AL |
273 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
274 | DeviceState *dev = kid->child; | |
ee46d8a5 AL |
275 | 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 | ||
285 | static BusState *qbus_find_recursive(BusState *bus, const char *name, | |
0d936928 | 286 | const char *bus_typename) |
ee46d8a5 | 287 | { |
1395af6f | 288 | BusClass *bus_class = BUS_GET_CLASS(bus); |
0866aca1 | 289 | BusChild *kid; |
ee46d8a5 AL |
290 | BusState *child, *ret; |
291 | int match = 1; | |
292 | ||
293 | if (name && (strcmp(bus->name, name) != 0)) { | |
294 | match = 0; | |
95e2af98 | 295 | } else if (bus_typename && !object_dynamic_cast(OBJECT(bus), bus_typename)) { |
ee46d8a5 | 296 | match = 0; |
95e2af98 | 297 | } else if ((bus_class->max_dev != 0) && (bus_class->max_dev <= bus->max_index)) { |
1395af6f FK |
298 | 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 | } | |
ee46d8a5 AL |
308 | if (match) { |
309 | return bus; | |
310 | } | |
311 | ||
0866aca1 AL |
312 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
313 | DeviceState *dev = kid->child; | |
ee46d8a5 | 314 | QLIST_FOREACH(child, &dev->child_bus, sibling) { |
0d936928 | 315 | ret = qbus_find_recursive(child, name, bus_typename); |
ee46d8a5 AL |
316 | if (ret) { |
317 | return ret; | |
318 | } | |
319 | } | |
320 | } | |
321 | return NULL; | |
322 | } | |
323 | ||
324 | static 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 | ||
411 | DeviceState *qdev_device_add(QemuOpts *opts) | |
412 | { | |
413 | ObjectClass *obj; | |
414 | DeviceClass *k; | |
415 | const char *driver, *path, *id; | |
416 | DeviceState *qdev; | |
2f7bd829 | 417 | BusState *bus = NULL; |
ee46d8a5 AL |
418 | |
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 | } | |
e912c96f | 450 | if (!object_dynamic_cast(OBJECT(bus), k->bus_type)) { |
ee46d8a5 | 451 | qerror_report(QERR_BAD_BUS_FOR_DEVICE, |
0d936928 | 452 | driver, object_get_typename(OBJECT(bus))); |
ee46d8a5 AL |
453 | return NULL; |
454 | } | |
2f7bd829 | 455 | } else if (k->bus_type != NULL) { |
0d936928 | 456 | bus = qbus_find_recursive(sysbus_get_default(), NULL, k->bus_type); |
ee46d8a5 AL |
457 | if (!bus) { |
458 | qerror_report(QERR_NO_BUS_FOR_DEVICE, | |
c3594ed7 | 459 | k->bus_type, driver); |
ee46d8a5 AL |
460 | return NULL; |
461 | } | |
462 | } | |
2f7bd829 | 463 | if (qdev_hotplug && bus && !bus->allow_hotplug) { |
ee46d8a5 AL |
464 | qerror_report(QERR_BUS_NO_HOTPLUG, bus->name); |
465 | return NULL; | |
466 | } | |
467 | ||
ee46d8a5 AL |
468 | /* create device, set properties */ |
469 | qdev = DEVICE(object_new(driver)); | |
2f7bd829 AF |
470 | |
471 | if (bus) { | |
472 | qdev_set_parent_bus(qdev, bus); | |
473 | } | |
ee46d8a5 AL |
474 | |
475 | id = qemu_opts_id(opts); | |
476 | if (id) { | |
477 | qdev->id = id; | |
b2d4b3f7 AL |
478 | } |
479 | if (qemu_opt_foreach(opts, set_property, qdev, 1) != 0) { | |
480 | qdev_free(qdev); | |
481 | return NULL; | |
482 | } | |
b2d4b3f7 | 483 | if (qdev->id) { |
57c9fafe AL |
484 | object_property_add_child(qdev_get_peripheral(), qdev->id, |
485 | OBJECT(qdev), NULL); | |
ee46d8a5 AL |
486 | } else { |
487 | static int anon_count; | |
488 | gchar *name = g_strdup_printf("device[%d]", anon_count++); | |
57c9fafe AL |
489 | object_property_add_child(qdev_get_peripheral_anon(), name, |
490 | OBJECT(qdev), NULL); | |
ee46d8a5 AL |
491 | g_free(name); |
492 | } | |
f424d5c4 PB |
493 | if (qdev_init(qdev) < 0) { |
494 | qerror_report(QERR_DEVICE_INIT_FAILED, driver); | |
495 | return NULL; | |
496 | } | |
ee46d8a5 AL |
497 | qdev->opts = opts; |
498 | return qdev; | |
499 | } | |
500 | ||
501 | ||
502 | #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__) | |
503 | static void qbus_print(Monitor *mon, BusState *bus, int indent); | |
504 | ||
505 | static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props, | |
bce54474 | 506 | int indent) |
ee46d8a5 | 507 | { |
ee46d8a5 AL |
508 | if (!props) |
509 | return; | |
d822979b PB |
510 | 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 { | |
8185bfc1 | 517 | value = object_property_print(OBJECT(dev), props->name, &err); |
d822979b PB |
518 | } |
519 | g_free(legacy_name); | |
520 | ||
521 | if (err) { | |
522 | error_free(err); | |
523 | continue; | |
ee46d8a5 | 524 | } |
bce54474 | 525 | qdev_printf("%s = %s\n", props->name, |
d822979b PB |
526 | value && *value ? value : "<null>"); |
527 | g_free(value); | |
ee46d8a5 AL |
528 | } |
529 | } | |
530 | ||
0d936928 AL |
531 | static 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 | ||
ee46d8a5 AL |
540 | static void qdev_print(Monitor *mon, DeviceState *dev, int indent) |
541 | { | |
bce54474 | 542 | ObjectClass *class; |
ee46d8a5 AL |
543 | 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 | } | |
bce54474 PB |
553 | 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)); | |
da9fbe76 | 558 | bus_print_dev(dev->parent_bus, mon, dev, indent); |
ee46d8a5 AL |
559 | QLIST_FOREACH(child, &dev->child_bus, sibling) { |
560 | qbus_print(mon, child, indent); | |
561 | } | |
562 | } | |
563 | ||
564 | static void qbus_print(Monitor *mon, BusState *bus, int indent) | |
565 | { | |
0866aca1 | 566 | BusChild *kid; |
ee46d8a5 AL |
567 | |
568 | qdev_printf("bus: %s\n", bus->name); | |
569 | indent += 2; | |
0d936928 | 570 | qdev_printf("type %s\n", object_get_typename(OBJECT(bus))); |
0866aca1 AL |
571 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
572 | DeviceState *dev = kid->child; | |
ee46d8a5 AL |
573 | qdev_print(mon, dev, indent); |
574 | } | |
575 | } | |
576 | #undef qdev_printf | |
577 | ||
84f2d0ea | 578 | void do_info_qtree(Monitor *mon, const QDict *qdict) |
ee46d8a5 AL |
579 | { |
580 | if (sysbus_get_default()) | |
581 | qbus_print(mon, sysbus_get_default(), 0); | |
582 | } | |
583 | ||
84f2d0ea | 584 | void do_info_qdm(Monitor *mon, const QDict *qdict) |
ee46d8a5 AL |
585 | { |
586 | object_class_foreach(qdev_print_devinfo, TYPE_DEVICE, false, NULL); | |
587 | } | |
588 | ||
589 | int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data) | |
590 | { | |
4e89978e | 591 | Error *local_err = NULL; |
ee46d8a5 | 592 | QemuOpts *opts; |
b09995ae | 593 | DeviceState *dev; |
ee46d8a5 | 594 | |
4e89978e LC |
595 | 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); | |
ee46d8a5 AL |
599 | return -1; |
600 | } | |
601 | if (!monitor_cur_is_qmp() && qdev_device_help(opts)) { | |
602 | qemu_opts_del(opts); | |
603 | return 0; | |
604 | } | |
b09995ae PB |
605 | dev = qdev_device_add(opts); |
606 | if (!dev) { | |
ee46d8a5 AL |
607 | qemu_opts_del(opts); |
608 | return -1; | |
609 | } | |
b09995ae | 610 | object_unref(OBJECT(dev)); |
ee46d8a5 AL |
611 | return 0; |
612 | } | |
613 | ||
a15fef21 | 614 | void qmp_device_del(const char *id, Error **errp) |
ee46d8a5 | 615 | { |
ee46d8a5 AL |
616 | DeviceState *dev; |
617 | ||
618 | dev = qdev_find_recursive(sysbus_get_default(), id); | |
619 | if (NULL == dev) { | |
a15fef21 LC |
620 | error_set(errp, QERR_DEVICE_NOT_FOUND, id); |
621 | return; | |
56f9107e LC |
622 | } |
623 | ||
a15fef21 | 624 | qdev_unplug(dev, errp); |
ee46d8a5 AL |
625 | } |
626 | ||
627 | void qdev_machine_init(void) | |
628 | { | |
629 | qdev_get_peripheral_anon(); | |
630 | qdev_get_peripheral(); | |
631 | } | |
4d454574 PB |
632 | |
633 | QemuOptsList 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 | ||
647 | QemuOptsList 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 | ||
665 | int 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 | } |