]>
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 | ||
20 | #include "qdev.h" | |
83c9089e | 21 | #include "monitor/monitor.h" |
a15fef21 | 22 | #include "qmp-commands.h" |
9c17d615 | 23 | #include "sysemu/arch_init.h" |
1de7afc9 | 24 | #include "qemu/config-file.h" |
ee46d8a5 AL |
25 | |
26 | /* | |
27 | * Aliases were a bad idea from the start. Let's keep them | |
28 | * from spreading further. | |
29 | */ | |
30 | typedef struct QDevAlias | |
31 | { | |
32 | const char *typename; | |
33 | const char *alias; | |
5f629d94 | 34 | uint32_t arch_mask; |
ee46d8a5 AL |
35 | } QDevAlias; |
36 | ||
37 | static const QDevAlias qdev_alias_table[] = { | |
5f629d94 AG |
38 | { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, |
39 | { "virtio-net-pci", "virtio-net", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, | |
40 | { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, | |
41 | { "virtio-balloon-pci", "virtio-balloon", | |
42 | QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, | |
43 | { "virtio-blk-s390", "virtio-blk", QEMU_ARCH_S390X }, | |
44 | { "virtio-net-s390", "virtio-net", QEMU_ARCH_S390X }, | |
45 | { "virtio-serial-s390", "virtio-serial", QEMU_ARCH_S390X }, | |
ee46d8a5 AL |
46 | { "lsi53c895a", "lsi" }, |
47 | { "ich9-ahci", "ahci" }, | |
c3ebd3ba | 48 | { "kvm-pci-assign", "pci-assign" }, |
ee46d8a5 AL |
49 | { } |
50 | }; | |
51 | ||
52 | static const char *qdev_class_get_alias(DeviceClass *dc) | |
53 | { | |
54 | const char *typename = object_class_get_name(OBJECT_CLASS(dc)); | |
55 | int i; | |
56 | ||
57 | for (i = 0; qdev_alias_table[i].typename; i++) { | |
5f629d94 AG |
58 | if (qdev_alias_table[i].arch_mask && |
59 | !(qdev_alias_table[i].arch_mask & arch_type)) { | |
60 | continue; | |
61 | } | |
62 | ||
ee46d8a5 AL |
63 | if (strcmp(qdev_alias_table[i].typename, typename) == 0) { |
64 | return qdev_alias_table[i].alias; | |
65 | } | |
66 | } | |
67 | ||
68 | return NULL; | |
69 | } | |
70 | ||
71 | static bool qdev_class_has_alias(DeviceClass *dc) | |
72 | { | |
73 | return (qdev_class_get_alias(dc) != NULL); | |
74 | } | |
75 | ||
76 | static void qdev_print_devinfo(ObjectClass *klass, void *opaque) | |
77 | { | |
78 | DeviceClass *dc; | |
79 | bool *show_no_user = opaque; | |
80 | ||
81 | dc = (DeviceClass *)object_class_dynamic_cast(klass, TYPE_DEVICE); | |
82 | ||
83 | if (!dc || (show_no_user && !*show_no_user && dc->no_user)) { | |
84 | return; | |
85 | } | |
86 | ||
87 | error_printf("name \"%s\"", object_class_get_name(klass)); | |
0d936928 AL |
88 | if (dc->bus_type) { |
89 | error_printf(", bus %s", dc->bus_type); | |
ee46d8a5 AL |
90 | } |
91 | if (qdev_class_has_alias(dc)) { | |
92 | error_printf(", alias \"%s\"", qdev_class_get_alias(dc)); | |
93 | } | |
94 | if (dc->desc) { | |
95 | error_printf(", desc \"%s\"", dc->desc); | |
96 | } | |
97 | if (dc->no_user) { | |
98 | error_printf(", no-user"); | |
99 | } | |
100 | error_printf("\n"); | |
101 | } | |
102 | ||
103 | static int set_property(const char *name, const char *value, void *opaque) | |
104 | { | |
105 | DeviceState *dev = opaque; | |
106 | ||
107 | if (strcmp(name, "driver") == 0) | |
108 | return 0; | |
109 | if (strcmp(name, "bus") == 0) | |
110 | return 0; | |
111 | ||
112 | if (qdev_prop_parse(dev, name, value) == -1) { | |
113 | return -1; | |
114 | } | |
115 | return 0; | |
116 | } | |
117 | ||
118 | static const char *find_typename_by_alias(const char *alias) | |
119 | { | |
120 | int i; | |
121 | ||
122 | for (i = 0; qdev_alias_table[i].alias; i++) { | |
5f629d94 AG |
123 | if (qdev_alias_table[i].arch_mask && |
124 | !(qdev_alias_table[i].arch_mask & arch_type)) { | |
125 | continue; | |
126 | } | |
127 | ||
ee46d8a5 AL |
128 | if (strcmp(qdev_alias_table[i].alias, alias) == 0) { |
129 | return qdev_alias_table[i].typename; | |
130 | } | |
131 | } | |
132 | ||
133 | return NULL; | |
134 | } | |
135 | ||
136 | int qdev_device_help(QemuOpts *opts) | |
137 | { | |
138 | const char *driver; | |
139 | Property *prop; | |
140 | ObjectClass *klass; | |
ee46d8a5 AL |
141 | |
142 | driver = qemu_opt_get(opts, "driver"); | |
c8057f95 | 143 | if (driver && is_help_option(driver)) { |
ee46d8a5 AL |
144 | bool show_no_user = false; |
145 | object_class_foreach(qdev_print_devinfo, TYPE_DEVICE, false, &show_no_user); | |
146 | return 1; | |
147 | } | |
148 | ||
c8057f95 | 149 | if (!driver || !qemu_opt_has_help_opt(opts)) { |
ee46d8a5 AL |
150 | return 0; |
151 | } | |
152 | ||
153 | klass = object_class_by_name(driver); | |
154 | if (!klass) { | |
155 | const char *typename = find_typename_by_alias(driver); | |
156 | ||
157 | if (typename) { | |
158 | driver = typename; | |
159 | klass = object_class_by_name(driver); | |
160 | } | |
161 | } | |
162 | ||
163 | if (!klass) { | |
164 | return 0; | |
165 | } | |
bce54474 PB |
166 | do { |
167 | for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) { | |
168 | /* | |
169 | * TODO Properties without a parser are just for dirty hacks. | |
170 | * qdev_prop_ptr is the only such PropertyInfo. It's marked | |
171 | * for removal. This conditional should be removed along with | |
172 | * it. | |
173 | */ | |
90ca64a9 | 174 | if (!prop->info->set) { |
d03d6b4e AL |
175 | continue; /* no way to set it, don't show */ |
176 | } | |
177 | error_printf("%s.%s=%s\n", driver, prop->name, | |
178 | prop->info->legacy_name ?: prop->info->name); | |
ee46d8a5 | 179 | } |
bce54474 PB |
180 | klass = object_class_get_parent(klass); |
181 | } while (klass != object_class_by_name(TYPE_DEVICE)); | |
ee46d8a5 AL |
182 | return 1; |
183 | } | |
184 | ||
57c9fafe | 185 | static Object *qdev_get_peripheral(void) |
ee46d8a5 | 186 | { |
8b45d447 | 187 | static Object *dev; |
ee46d8a5 AL |
188 | |
189 | if (dev == NULL) { | |
dfe47e70 | 190 | dev = container_get(qdev_get_machine(), "/peripheral"); |
ee46d8a5 AL |
191 | } |
192 | ||
8b45d447 | 193 | return dev; |
ee46d8a5 AL |
194 | } |
195 | ||
57c9fafe | 196 | static Object *qdev_get_peripheral_anon(void) |
ee46d8a5 | 197 | { |
8b45d447 | 198 | static Object *dev; |
ee46d8a5 AL |
199 | |
200 | if (dev == NULL) { | |
dfe47e70 | 201 | dev = container_get(qdev_get_machine(), "/peripheral-anon"); |
ee46d8a5 AL |
202 | } |
203 | ||
8b45d447 | 204 | return dev; |
ee46d8a5 AL |
205 | } |
206 | ||
207 | static void qbus_list_bus(DeviceState *dev) | |
208 | { | |
209 | BusState *child; | |
210 | const char *sep = " "; | |
211 | ||
212 | error_printf("child busses at \"%s\":", | |
213 | dev->id ? dev->id : object_get_typename(OBJECT(dev))); | |
214 | QLIST_FOREACH(child, &dev->child_bus, sibling) { | |
215 | error_printf("%s\"%s\"", sep, child->name); | |
216 | sep = ", "; | |
217 | } | |
218 | error_printf("\n"); | |
219 | } | |
220 | ||
221 | static void qbus_list_dev(BusState *bus) | |
222 | { | |
0866aca1 | 223 | BusChild *kid; |
ee46d8a5 AL |
224 | const char *sep = " "; |
225 | ||
226 | error_printf("devices at \"%s\":", bus->name); | |
0866aca1 AL |
227 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
228 | DeviceState *dev = kid->child; | |
ee46d8a5 AL |
229 | error_printf("%s\"%s\"", sep, object_get_typename(OBJECT(dev))); |
230 | if (dev->id) | |
231 | error_printf("/\"%s\"", dev->id); | |
232 | sep = ", "; | |
233 | } | |
234 | error_printf("\n"); | |
235 | } | |
236 | ||
237 | static BusState *qbus_find_bus(DeviceState *dev, char *elem) | |
238 | { | |
239 | BusState *child; | |
240 | ||
241 | QLIST_FOREACH(child, &dev->child_bus, sibling) { | |
242 | if (strcmp(child->name, elem) == 0) { | |
243 | return child; | |
244 | } | |
245 | } | |
246 | return NULL; | |
247 | } | |
248 | ||
249 | static DeviceState *qbus_find_dev(BusState *bus, char *elem) | |
250 | { | |
0866aca1 | 251 | BusChild *kid; |
ee46d8a5 AL |
252 | |
253 | /* | |
254 | * try to match in order: | |
255 | * (1) instance id, if present | |
256 | * (2) driver name | |
257 | * (3) driver alias, if present | |
258 | */ | |
0866aca1 AL |
259 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
260 | DeviceState *dev = kid->child; | |
ee46d8a5 AL |
261 | if (dev->id && strcmp(dev->id, elem) == 0) { |
262 | return dev; | |
263 | } | |
264 | } | |
0866aca1 AL |
265 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
266 | DeviceState *dev = kid->child; | |
ee46d8a5 AL |
267 | if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) { |
268 | return dev; | |
269 | } | |
270 | } | |
0866aca1 AL |
271 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
272 | DeviceState *dev = kid->child; | |
ee46d8a5 AL |
273 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
274 | ||
275 | if (qdev_class_has_alias(dc) && | |
276 | strcmp(qdev_class_get_alias(dc), elem) == 0) { | |
277 | return dev; | |
278 | } | |
279 | } | |
280 | return NULL; | |
281 | } | |
282 | ||
283 | static BusState *qbus_find_recursive(BusState *bus, const char *name, | |
0d936928 | 284 | const char *bus_typename) |
ee46d8a5 | 285 | { |
1395af6f | 286 | BusClass *bus_class = BUS_GET_CLASS(bus); |
0866aca1 | 287 | BusChild *kid; |
ee46d8a5 AL |
288 | BusState *child, *ret; |
289 | int match = 1; | |
290 | ||
291 | if (name && (strcmp(bus->name, name) != 0)) { | |
292 | match = 0; | |
293 | } | |
e912c96f | 294 | if (bus_typename && !object_dynamic_cast(OBJECT(bus), bus_typename)) { |
ee46d8a5 AL |
295 | match = 0; |
296 | } | |
1395af6f FK |
297 | if ((bus_class->max_dev != 0) && (bus_class->max_dev <= bus->max_index)) { |
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; | |
417 | BusState *bus; | |
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 | } | |
455 | } else { | |
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 | } | |
463 | if (qdev_hotplug && !bus->allow_hotplug) { | |
464 | qerror_report(QERR_BUS_NO_HOTPLUG, bus->name); | |
465 | return NULL; | |
466 | } | |
467 | ||
468 | if (!bus) { | |
469 | bus = sysbus_get_default(); | |
470 | } | |
471 | ||
472 | /* create device, set properties */ | |
473 | qdev = DEVICE(object_new(driver)); | |
474 | qdev_set_parent_bus(qdev, bus); | |
ee46d8a5 AL |
475 | |
476 | id = qemu_opts_id(opts); | |
477 | if (id) { | |
478 | qdev->id = id; | |
b2d4b3f7 AL |
479 | } |
480 | if (qemu_opt_foreach(opts, set_property, qdev, 1) != 0) { | |
481 | qdev_free(qdev); | |
482 | return NULL; | |
483 | } | |
b2d4b3f7 | 484 | if (qdev->id) { |
57c9fafe AL |
485 | object_property_add_child(qdev_get_peripheral(), qdev->id, |
486 | OBJECT(qdev), NULL); | |
ee46d8a5 AL |
487 | } else { |
488 | static int anon_count; | |
489 | gchar *name = g_strdup_printf("device[%d]", anon_count++); | |
57c9fafe AL |
490 | object_property_add_child(qdev_get_peripheral_anon(), name, |
491 | OBJECT(qdev), NULL); | |
ee46d8a5 AL |
492 | g_free(name); |
493 | } | |
f424d5c4 PB |
494 | if (qdev_init(qdev) < 0) { |
495 | qerror_report(QERR_DEVICE_INIT_FAILED, driver); | |
496 | return NULL; | |
497 | } | |
ee46d8a5 AL |
498 | qdev->opts = opts; |
499 | return qdev; | |
500 | } | |
501 | ||
502 | ||
503 | #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__) | |
504 | static void qbus_print(Monitor *mon, BusState *bus, int indent); | |
505 | ||
506 | static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props, | |
bce54474 | 507 | int indent) |
ee46d8a5 | 508 | { |
ee46d8a5 AL |
509 | if (!props) |
510 | return; | |
d822979b PB |
511 | for (; props->name; props++) { |
512 | Error *err = NULL; | |
513 | char *value; | |
514 | char *legacy_name = g_strdup_printf("legacy-%s", props->name); | |
515 | if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) { | |
516 | value = object_property_get_str(OBJECT(dev), legacy_name, &err); | |
517 | } else { | |
8185bfc1 | 518 | value = object_property_print(OBJECT(dev), props->name, &err); |
d822979b PB |
519 | } |
520 | g_free(legacy_name); | |
521 | ||
522 | if (err) { | |
523 | error_free(err); | |
524 | continue; | |
ee46d8a5 | 525 | } |
bce54474 | 526 | qdev_printf("%s = %s\n", props->name, |
d822979b PB |
527 | value && *value ? value : "<null>"); |
528 | g_free(value); | |
ee46d8a5 AL |
529 | } |
530 | } | |
531 | ||
0d936928 AL |
532 | static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent) |
533 | { | |
534 | BusClass *bc = BUS_GET_CLASS(bus); | |
535 | ||
536 | if (bc->print_dev) { | |
537 | bc->print_dev(mon, dev, indent); | |
538 | } | |
539 | } | |
540 | ||
ee46d8a5 AL |
541 | static void qdev_print(Monitor *mon, DeviceState *dev, int indent) |
542 | { | |
bce54474 | 543 | ObjectClass *class; |
ee46d8a5 AL |
544 | BusState *child; |
545 | qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)), | |
546 | dev->id ? dev->id : ""); | |
547 | indent += 2; | |
548 | if (dev->num_gpio_in) { | |
549 | qdev_printf("gpio-in %d\n", dev->num_gpio_in); | |
550 | } | |
551 | if (dev->num_gpio_out) { | |
552 | qdev_printf("gpio-out %d\n", dev->num_gpio_out); | |
553 | } | |
bce54474 PB |
554 | class = object_get_class(OBJECT(dev)); |
555 | do { | |
556 | qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent); | |
557 | class = object_class_get_parent(class); | |
558 | } while (class != object_class_by_name(TYPE_DEVICE)); | |
da9fbe76 | 559 | bus_print_dev(dev->parent_bus, mon, dev, indent); |
ee46d8a5 AL |
560 | QLIST_FOREACH(child, &dev->child_bus, sibling) { |
561 | qbus_print(mon, child, indent); | |
562 | } | |
563 | } | |
564 | ||
565 | static void qbus_print(Monitor *mon, BusState *bus, int indent) | |
566 | { | |
0866aca1 | 567 | BusChild *kid; |
ee46d8a5 AL |
568 | |
569 | qdev_printf("bus: %s\n", bus->name); | |
570 | indent += 2; | |
0d936928 | 571 | qdev_printf("type %s\n", object_get_typename(OBJECT(bus))); |
0866aca1 AL |
572 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
573 | DeviceState *dev = kid->child; | |
ee46d8a5 AL |
574 | qdev_print(mon, dev, indent); |
575 | } | |
576 | } | |
577 | #undef qdev_printf | |
578 | ||
84f2d0ea | 579 | void do_info_qtree(Monitor *mon, const QDict *qdict) |
ee46d8a5 AL |
580 | { |
581 | if (sysbus_get_default()) | |
582 | qbus_print(mon, sysbus_get_default(), 0); | |
583 | } | |
584 | ||
84f2d0ea | 585 | void do_info_qdm(Monitor *mon, const QDict *qdict) |
ee46d8a5 AL |
586 | { |
587 | object_class_foreach(qdev_print_devinfo, TYPE_DEVICE, false, NULL); | |
588 | } | |
589 | ||
590 | int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data) | |
591 | { | |
4e89978e | 592 | Error *local_err = NULL; |
ee46d8a5 | 593 | QemuOpts *opts; |
b09995ae | 594 | DeviceState *dev; |
ee46d8a5 | 595 | |
4e89978e LC |
596 | opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, &local_err); |
597 | if (error_is_set(&local_err)) { | |
598 | qerror_report_err(local_err); | |
599 | error_free(local_err); | |
ee46d8a5 AL |
600 | return -1; |
601 | } | |
602 | if (!monitor_cur_is_qmp() && qdev_device_help(opts)) { | |
603 | qemu_opts_del(opts); | |
604 | return 0; | |
605 | } | |
b09995ae PB |
606 | dev = qdev_device_add(opts); |
607 | if (!dev) { | |
ee46d8a5 AL |
608 | qemu_opts_del(opts); |
609 | return -1; | |
610 | } | |
b09995ae | 611 | object_unref(OBJECT(dev)); |
ee46d8a5 AL |
612 | return 0; |
613 | } | |
614 | ||
a15fef21 | 615 | void qmp_device_del(const char *id, Error **errp) |
ee46d8a5 | 616 | { |
ee46d8a5 AL |
617 | DeviceState *dev; |
618 | ||
619 | dev = qdev_find_recursive(sysbus_get_default(), id); | |
620 | if (NULL == dev) { | |
a15fef21 LC |
621 | error_set(errp, QERR_DEVICE_NOT_FOUND, id); |
622 | return; | |
56f9107e LC |
623 | } |
624 | ||
a15fef21 | 625 | qdev_unplug(dev, errp); |
ee46d8a5 AL |
626 | } |
627 | ||
628 | void qdev_machine_init(void) | |
629 | { | |
630 | qdev_get_peripheral_anon(); | |
631 | qdev_get_peripheral(); | |
632 | } | |
4d454574 PB |
633 | |
634 | QemuOptsList qemu_device_opts = { | |
635 | .name = "device", | |
636 | .implied_opt_name = "driver", | |
637 | .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head), | |
638 | .desc = { | |
639 | /* | |
640 | * no elements => accept any | |
641 | * sanity checking will happen later | |
642 | * when setting device properties | |
643 | */ | |
644 | { /* end of list */ } | |
645 | }, | |
646 | }; | |
647 | ||
648 | QemuOptsList qemu_global_opts = { | |
649 | .name = "global", | |
650 | .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head), | |
651 | .desc = { | |
652 | { | |
653 | .name = "driver", | |
654 | .type = QEMU_OPT_STRING, | |
655 | },{ | |
656 | .name = "property", | |
657 | .type = QEMU_OPT_STRING, | |
658 | },{ | |
659 | .name = "value", | |
660 | .type = QEMU_OPT_STRING, | |
661 | }, | |
662 | { /* end of list */ } | |
663 | }, | |
664 | }; | |
665 | ||
666 | int qemu_global_option(const char *str) | |
667 | { | |
668 | char driver[64], property[64]; | |
669 | QemuOpts *opts; | |
670 | int rc, offset; | |
671 | ||
672 | rc = sscanf(str, "%63[^.].%63[^=]%n", driver, property, &offset); | |
673 | if (rc < 2 || str[offset] != '=') { | |
674 | error_report("can't parse: \"%s\"", str); | |
675 | return -1; | |
676 | } | |
677 | ||
678 | opts = qemu_opts_create_nofail(&qemu_global_opts); | |
679 | qemu_opt_set(opts, "driver", driver); | |
680 | qemu_opt_set(opts, "property", property); | |
681 | qemu_opt_set(opts, "value", str+offset+1); | |
682 | return 0; | |
683 | } |