]>
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 | ||
a3400aee | 78 | static void qdev_print_devinfo(DeviceClass *dc) |
ee46d8a5 | 79 | { |
a3400aee | 80 | error_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc))); |
0d936928 AL |
81 | if (dc->bus_type) { |
82 | error_printf(", bus %s", dc->bus_type); | |
ee46d8a5 AL |
83 | } |
84 | if (qdev_class_has_alias(dc)) { | |
85 | error_printf(", alias \"%s\"", qdev_class_get_alias(dc)); | |
86 | } | |
87 | if (dc->desc) { | |
88 | error_printf(", desc \"%s\"", dc->desc); | |
89 | } | |
efec3dd6 | 90 | if (dc->cannot_instantiate_with_device_add_yet) { |
ee46d8a5 AL |
91 | error_printf(", no-user"); |
92 | } | |
93 | error_printf("\n"); | |
94 | } | |
95 | ||
a3400aee MA |
96 | static gint devinfo_cmp(gconstpointer a, gconstpointer b) |
97 | { | |
98 | return strcasecmp(object_class_get_name((ObjectClass *)a), | |
99 | object_class_get_name((ObjectClass *)b)); | |
100 | } | |
101 | ||
102 | static void qdev_print_devinfos(bool show_no_user) | |
103 | { | |
104 | static const char *cat_name[DEVICE_CATEGORY_MAX + 1] = { | |
105 | [DEVICE_CATEGORY_BRIDGE] = "Controller/Bridge/Hub", | |
106 | [DEVICE_CATEGORY_USB] = "USB", | |
107 | [DEVICE_CATEGORY_STORAGE] = "Storage", | |
108 | [DEVICE_CATEGORY_NETWORK] = "Network", | |
109 | [DEVICE_CATEGORY_INPUT] = "Input", | |
110 | [DEVICE_CATEGORY_DISPLAY] = "Display", | |
111 | [DEVICE_CATEGORY_SOUND] = "Sound", | |
112 | [DEVICE_CATEGORY_MISC] = "Misc", | |
113 | [DEVICE_CATEGORY_MAX] = "Uncategorized", | |
114 | }; | |
115 | GSList *list, *elt; | |
116 | int i; | |
117 | bool cat_printed; | |
118 | ||
119 | list = g_slist_sort(object_class_get_list(TYPE_DEVICE, false), | |
120 | devinfo_cmp); | |
121 | ||
122 | for (i = 0; i <= DEVICE_CATEGORY_MAX; i++) { | |
123 | cat_printed = false; | |
124 | for (elt = list; elt; elt = elt->next) { | |
125 | DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data, | |
126 | TYPE_DEVICE); | |
127 | if ((i < DEVICE_CATEGORY_MAX | |
128 | ? !test_bit(i, dc->categories) | |
129 | : !bitmap_empty(dc->categories, DEVICE_CATEGORY_MAX)) | |
efec3dd6 MA |
130 | || (!show_no_user |
131 | && dc->cannot_instantiate_with_device_add_yet)) { | |
a3400aee MA |
132 | continue; |
133 | } | |
134 | if (!cat_printed) { | |
135 | error_printf("%s%s devices:\n", i ? "\n" : "", | |
136 | cat_name[i]); | |
137 | cat_printed = true; | |
138 | } | |
139 | qdev_print_devinfo(dc); | |
140 | } | |
141 | } | |
142 | ||
143 | g_slist_free(list); | |
144 | } | |
145 | ||
ee46d8a5 AL |
146 | static int set_property(const char *name, const char *value, void *opaque) |
147 | { | |
98a65284 | 148 | Object *obj = opaque; |
b1fe9bcb | 149 | Error *err = NULL; |
ee46d8a5 AL |
150 | |
151 | if (strcmp(name, "driver") == 0) | |
152 | return 0; | |
153 | if (strcmp(name, "bus") == 0) | |
154 | return 0; | |
155 | ||
98a65284 | 156 | object_property_parse(obj, value, name, &err); |
b1fe9bcb AF |
157 | if (err != NULL) { |
158 | qerror_report_err(err); | |
159 | error_free(err); | |
ee46d8a5 AL |
160 | return -1; |
161 | } | |
162 | return 0; | |
163 | } | |
164 | ||
165 | static const char *find_typename_by_alias(const char *alias) | |
166 | { | |
167 | int i; | |
168 | ||
169 | for (i = 0; qdev_alias_table[i].alias; i++) { | |
5f629d94 AG |
170 | if (qdev_alias_table[i].arch_mask && |
171 | !(qdev_alias_table[i].arch_mask & arch_type)) { | |
172 | continue; | |
173 | } | |
174 | ||
ee46d8a5 AL |
175 | if (strcmp(qdev_alias_table[i].alias, alias) == 0) { |
176 | return qdev_alias_table[i].typename; | |
177 | } | |
178 | } | |
179 | ||
180 | return NULL; | |
181 | } | |
182 | ||
183 | int qdev_device_help(QemuOpts *opts) | |
184 | { | |
ef523587 | 185 | Error *local_err = NULL; |
ee46d8a5 | 186 | const char *driver; |
ef523587 SH |
187 | DevicePropertyInfoList *prop_list; |
188 | DevicePropertyInfoList *prop; | |
ee46d8a5 AL |
189 | |
190 | driver = qemu_opt_get(opts, "driver"); | |
c8057f95 | 191 | if (driver && is_help_option(driver)) { |
a3400aee | 192 | qdev_print_devinfos(false); |
ee46d8a5 AL |
193 | return 1; |
194 | } | |
195 | ||
c8057f95 | 196 | if (!driver || !qemu_opt_has_help_opt(opts)) { |
ee46d8a5 AL |
197 | return 0; |
198 | } | |
199 | ||
ef523587 | 200 | if (!object_class_by_name(driver)) { |
ee46d8a5 AL |
201 | const char *typename = find_typename_by_alias(driver); |
202 | ||
203 | if (typename) { | |
204 | driver = typename; | |
ee46d8a5 AL |
205 | } |
206 | } | |
207 | ||
ef523587 | 208 | prop_list = qmp_device_list_properties(driver, &local_err); |
0722eba9 | 209 | if (local_err) { |
ef523587 SH |
210 | error_printf("%s\n", error_get_pretty(local_err)); |
211 | error_free(local_err); | |
212 | return 1; | |
ee46d8a5 | 213 | } |
ef523587 SH |
214 | |
215 | for (prop = prop_list; prop; prop = prop->next) { | |
216 | error_printf("%s.%s=%s\n", driver, | |
217 | prop->value->name, | |
218 | prop->value->type); | |
219 | } | |
220 | ||
221 | qapi_free_DevicePropertyInfoList(prop_list); | |
ee46d8a5 AL |
222 | return 1; |
223 | } | |
224 | ||
57c9fafe | 225 | static Object *qdev_get_peripheral(void) |
ee46d8a5 | 226 | { |
8b45d447 | 227 | static Object *dev; |
ee46d8a5 AL |
228 | |
229 | if (dev == NULL) { | |
dfe47e70 | 230 | dev = container_get(qdev_get_machine(), "/peripheral"); |
ee46d8a5 AL |
231 | } |
232 | ||
8b45d447 | 233 | return dev; |
ee46d8a5 AL |
234 | } |
235 | ||
57c9fafe | 236 | static Object *qdev_get_peripheral_anon(void) |
ee46d8a5 | 237 | { |
8b45d447 | 238 | static Object *dev; |
ee46d8a5 AL |
239 | |
240 | if (dev == NULL) { | |
dfe47e70 | 241 | dev = container_get(qdev_get_machine(), "/peripheral-anon"); |
ee46d8a5 AL |
242 | } |
243 | ||
8b45d447 | 244 | return dev; |
ee46d8a5 AL |
245 | } |
246 | ||
247 | static void qbus_list_bus(DeviceState *dev) | |
248 | { | |
249 | BusState *child; | |
250 | const char *sep = " "; | |
251 | ||
252 | error_printf("child busses at \"%s\":", | |
253 | dev->id ? dev->id : object_get_typename(OBJECT(dev))); | |
254 | QLIST_FOREACH(child, &dev->child_bus, sibling) { | |
255 | error_printf("%s\"%s\"", sep, child->name); | |
256 | sep = ", "; | |
257 | } | |
258 | error_printf("\n"); | |
259 | } | |
260 | ||
261 | static void qbus_list_dev(BusState *bus) | |
262 | { | |
0866aca1 | 263 | BusChild *kid; |
ee46d8a5 AL |
264 | const char *sep = " "; |
265 | ||
266 | error_printf("devices at \"%s\":", bus->name); | |
0866aca1 AL |
267 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
268 | DeviceState *dev = kid->child; | |
ee46d8a5 AL |
269 | error_printf("%s\"%s\"", sep, object_get_typename(OBJECT(dev))); |
270 | if (dev->id) | |
271 | error_printf("/\"%s\"", dev->id); | |
272 | sep = ", "; | |
273 | } | |
274 | error_printf("\n"); | |
275 | } | |
276 | ||
277 | static BusState *qbus_find_bus(DeviceState *dev, char *elem) | |
278 | { | |
279 | BusState *child; | |
280 | ||
281 | QLIST_FOREACH(child, &dev->child_bus, sibling) { | |
282 | if (strcmp(child->name, elem) == 0) { | |
283 | return child; | |
284 | } | |
285 | } | |
286 | return NULL; | |
287 | } | |
288 | ||
289 | static DeviceState *qbus_find_dev(BusState *bus, char *elem) | |
290 | { | |
0866aca1 | 291 | BusChild *kid; |
ee46d8a5 AL |
292 | |
293 | /* | |
294 | * try to match in order: | |
295 | * (1) instance id, if present | |
296 | * (2) driver name | |
297 | * (3) driver alias, if present | |
298 | */ | |
0866aca1 AL |
299 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
300 | DeviceState *dev = kid->child; | |
ee46d8a5 AL |
301 | if (dev->id && strcmp(dev->id, elem) == 0) { |
302 | return dev; | |
303 | } | |
304 | } | |
0866aca1 AL |
305 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
306 | DeviceState *dev = kid->child; | |
ee46d8a5 AL |
307 | if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) { |
308 | return dev; | |
309 | } | |
310 | } | |
0866aca1 AL |
311 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
312 | DeviceState *dev = kid->child; | |
ee46d8a5 AL |
313 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
314 | ||
315 | if (qdev_class_has_alias(dc) && | |
316 | strcmp(qdev_class_get_alias(dc), elem) == 0) { | |
317 | return dev; | |
318 | } | |
319 | } | |
320 | return NULL; | |
321 | } | |
322 | ||
323 | static BusState *qbus_find_recursive(BusState *bus, const char *name, | |
0d936928 | 324 | const char *bus_typename) |
ee46d8a5 | 325 | { |
1395af6f | 326 | BusClass *bus_class = BUS_GET_CLASS(bus); |
0866aca1 | 327 | BusChild *kid; |
ee46d8a5 AL |
328 | BusState *child, *ret; |
329 | int match = 1; | |
330 | ||
331 | if (name && (strcmp(bus->name, name) != 0)) { | |
332 | match = 0; | |
95e2af98 | 333 | } else if (bus_typename && !object_dynamic_cast(OBJECT(bus), bus_typename)) { |
ee46d8a5 | 334 | match = 0; |
95e2af98 | 335 | } else if ((bus_class->max_dev != 0) && (bus_class->max_dev <= bus->max_index)) { |
1395af6f FK |
336 | if (name != NULL) { |
337 | /* bus was explicitly specified: return an error. */ | |
338 | qerror_report(ERROR_CLASS_GENERIC_ERROR, "Bus '%s' is full", | |
339 | bus->name); | |
340 | return NULL; | |
341 | } else { | |
342 | /* bus was not specified: try to find another one. */ | |
343 | match = 0; | |
344 | } | |
345 | } | |
ee46d8a5 AL |
346 | if (match) { |
347 | return bus; | |
348 | } | |
349 | ||
0866aca1 AL |
350 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
351 | DeviceState *dev = kid->child; | |
ee46d8a5 | 352 | QLIST_FOREACH(child, &dev->child_bus, sibling) { |
0d936928 | 353 | ret = qbus_find_recursive(child, name, bus_typename); |
ee46d8a5 AL |
354 | if (ret) { |
355 | return ret; | |
356 | } | |
357 | } | |
358 | } | |
359 | return NULL; | |
360 | } | |
361 | ||
362 | static BusState *qbus_find(const char *path) | |
363 | { | |
364 | DeviceState *dev; | |
365 | BusState *bus; | |
366 | char elem[128]; | |
367 | int pos, len; | |
368 | ||
369 | /* find start element */ | |
370 | if (path[0] == '/') { | |
371 | bus = sysbus_get_default(); | |
372 | pos = 0; | |
373 | } else { | |
374 | if (sscanf(path, "%127[^/]%n", elem, &len) != 1) { | |
375 | assert(!path[0]); | |
376 | elem[0] = len = 0; | |
377 | } | |
378 | bus = qbus_find_recursive(sysbus_get_default(), elem, NULL); | |
379 | if (!bus) { | |
380 | qerror_report(QERR_BUS_NOT_FOUND, elem); | |
381 | return NULL; | |
382 | } | |
383 | pos = len; | |
384 | } | |
385 | ||
386 | for (;;) { | |
387 | assert(path[pos] == '/' || !path[pos]); | |
388 | while (path[pos] == '/') { | |
389 | pos++; | |
390 | } | |
391 | if (path[pos] == '\0') { | |
392 | return bus; | |
393 | } | |
394 | ||
395 | /* find device */ | |
396 | if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) { | |
dfc6f865 | 397 | g_assert_not_reached(); |
ee46d8a5 AL |
398 | elem[0] = len = 0; |
399 | } | |
400 | pos += len; | |
401 | dev = qbus_find_dev(bus, elem); | |
402 | if (!dev) { | |
403 | qerror_report(QERR_DEVICE_NOT_FOUND, elem); | |
404 | if (!monitor_cur_is_qmp()) { | |
405 | qbus_list_dev(bus); | |
406 | } | |
407 | return NULL; | |
408 | } | |
409 | ||
410 | assert(path[pos] == '/' || !path[pos]); | |
411 | while (path[pos] == '/') { | |
412 | pos++; | |
413 | } | |
414 | if (path[pos] == '\0') { | |
415 | /* last specified element is a device. If it has exactly | |
416 | * one child bus accept it nevertheless */ | |
417 | switch (dev->num_child_bus) { | |
418 | case 0: | |
f231b88d CR |
419 | qerror_report(ERROR_CLASS_GENERIC_ERROR, |
420 | "Device '%s' has no child bus", elem); | |
ee46d8a5 AL |
421 | return NULL; |
422 | case 1: | |
423 | return QLIST_FIRST(&dev->child_bus); | |
424 | default: | |
f231b88d CR |
425 | qerror_report(ERROR_CLASS_GENERIC_ERROR, |
426 | "Device '%s' has multiple child busses", elem); | |
ee46d8a5 AL |
427 | if (!monitor_cur_is_qmp()) { |
428 | qbus_list_bus(dev); | |
429 | } | |
430 | return NULL; | |
431 | } | |
432 | } | |
433 | ||
434 | /* find bus */ | |
435 | if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) { | |
dfc6f865 | 436 | g_assert_not_reached(); |
ee46d8a5 AL |
437 | elem[0] = len = 0; |
438 | } | |
439 | pos += len; | |
440 | bus = qbus_find_bus(dev, elem); | |
441 | if (!bus) { | |
442 | qerror_report(QERR_BUS_NOT_FOUND, elem); | |
443 | if (!monitor_cur_is_qmp()) { | |
444 | qbus_list_bus(dev); | |
445 | } | |
446 | return NULL; | |
447 | } | |
448 | } | |
449 | } | |
450 | ||
451 | DeviceState *qdev_device_add(QemuOpts *opts) | |
452 | { | |
f4d85795 AF |
453 | ObjectClass *oc; |
454 | DeviceClass *dc; | |
ee46d8a5 | 455 | const char *driver, *path, *id; |
2bcb0c62 | 456 | DeviceState *dev; |
2f7bd829 | 457 | BusState *bus = NULL; |
852e2c50 | 458 | Error *err = NULL; |
ee46d8a5 AL |
459 | |
460 | driver = qemu_opt_get(opts, "driver"); | |
461 | if (!driver) { | |
462 | qerror_report(QERR_MISSING_PARAMETER, "driver"); | |
463 | return NULL; | |
464 | } | |
465 | ||
466 | /* find driver */ | |
f4d85795 AF |
467 | oc = object_class_by_name(driver); |
468 | if (!oc) { | |
ee46d8a5 AL |
469 | const char *typename = find_typename_by_alias(driver); |
470 | ||
471 | if (typename) { | |
472 | driver = typename; | |
f4d85795 | 473 | oc = object_class_by_name(driver); |
ee46d8a5 AL |
474 | } |
475 | } | |
476 | ||
061e84f7 | 477 | if (!object_class_dynamic_cast(oc, TYPE_DEVICE)) { |
11c308b1 MA |
478 | qerror_report(ERROR_CLASS_GENERIC_ERROR, |
479 | "'%s' is not a valid device model name", driver); | |
ee46d8a5 AL |
480 | return NULL; |
481 | } | |
482 | ||
2fa4e56d IM |
483 | if (object_class_is_abstract(oc)) { |
484 | qerror_report(QERR_INVALID_PARAMETER_VALUE, "driver", | |
485 | "non-abstract device type"); | |
486 | return NULL; | |
487 | } | |
488 | ||
f4d85795 | 489 | dc = DEVICE_CLASS(oc); |
ce9835e0 IM |
490 | if (dc->cannot_instantiate_with_device_add_yet || |
491 | (qdev_hotplug && !dc->hotpluggable)) { | |
7ea5e78f MA |
492 | qerror_report(QERR_INVALID_PARAMETER_VALUE, "driver", |
493 | "pluggable device type"); | |
494 | return NULL; | |
495 | } | |
ee46d8a5 AL |
496 | |
497 | /* find bus */ | |
498 | path = qemu_opt_get(opts, "bus"); | |
499 | if (path != NULL) { | |
500 | bus = qbus_find(path); | |
501 | if (!bus) { | |
502 | return NULL; | |
503 | } | |
f4d85795 | 504 | if (!object_dynamic_cast(OBJECT(bus), dc->bus_type)) { |
f231b88d CR |
505 | qerror_report(ERROR_CLASS_GENERIC_ERROR, |
506 | "Device '%s' can't go on a %s bus", | |
0d936928 | 507 | driver, object_get_typename(OBJECT(bus))); |
ee46d8a5 AL |
508 | return NULL; |
509 | } | |
f4d85795 AF |
510 | } else if (dc->bus_type != NULL) { |
511 | bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type); | |
ee46d8a5 | 512 | if (!bus) { |
f231b88d CR |
513 | qerror_report(ERROR_CLASS_GENERIC_ERROR, |
514 | "No '%s' bus found for device '%s'", | |
f4d85795 | 515 | dc->bus_type, driver); |
ee46d8a5 AL |
516 | return NULL; |
517 | } | |
518 | } | |
39b888bd | 519 | if (qdev_hotplug && bus && !qbus_is_hotpluggable(bus)) { |
ee46d8a5 AL |
520 | qerror_report(QERR_BUS_NO_HOTPLUG, bus->name); |
521 | return NULL; | |
522 | } | |
523 | ||
7b030949 | 524 | /* create device */ |
2bcb0c62 | 525 | dev = DEVICE(object_new(driver)); |
2f7bd829 AF |
526 | |
527 | if (bus) { | |
2bcb0c62 | 528 | qdev_set_parent_bus(dev, bus); |
2f7bd829 | 529 | } |
ee46d8a5 AL |
530 | |
531 | id = qemu_opts_id(opts); | |
532 | if (id) { | |
2bcb0c62 | 533 | dev->id = id; |
b2d4b3f7 | 534 | } |
7b030949 | 535 | |
2bcb0c62 AF |
536 | if (dev->id) { |
537 | object_property_add_child(qdev_get_peripheral(), dev->id, | |
538 | OBJECT(dev), NULL); | |
ee46d8a5 AL |
539 | } else { |
540 | static int anon_count; | |
541 | gchar *name = g_strdup_printf("device[%d]", anon_count++); | |
57c9fafe | 542 | object_property_add_child(qdev_get_peripheral_anon(), name, |
2bcb0c62 | 543 | OBJECT(dev), NULL); |
ee46d8a5 | 544 | g_free(name); |
2bcb0c62 | 545 | } |
52aa17cb | 546 | |
7b030949 AK |
547 | /* set properties */ |
548 | if (qemu_opt_foreach(opts, set_property, dev, 1) != 0) { | |
549 | object_unparent(OBJECT(dev)); | |
550 | object_unref(OBJECT(dev)); | |
551 | return NULL; | |
552 | } | |
553 | ||
52aa17cb | 554 | dev->opts = opts; |
852e2c50 AF |
555 | object_property_set_bool(OBJECT(dev), true, "realized", &err); |
556 | if (err != NULL) { | |
557 | qerror_report_err(err); | |
558 | error_free(err); | |
52aa17cb | 559 | dev->opts = NULL; |
852e2c50 | 560 | object_unparent(OBJECT(dev)); |
2bcb0c62 | 561 | object_unref(OBJECT(dev)); |
f424d5c4 PB |
562 | qerror_report(QERR_DEVICE_INIT_FAILED, driver); |
563 | return NULL; | |
564 | } | |
2bcb0c62 | 565 | return dev; |
ee46d8a5 AL |
566 | } |
567 | ||
568 | ||
569 | #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__) | |
570 | static void qbus_print(Monitor *mon, BusState *bus, int indent); | |
571 | ||
572 | static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props, | |
bce54474 | 573 | int indent) |
ee46d8a5 | 574 | { |
ee46d8a5 AL |
575 | if (!props) |
576 | return; | |
d822979b PB |
577 | for (; props->name; props++) { |
578 | Error *err = NULL; | |
579 | char *value; | |
580 | char *legacy_name = g_strdup_printf("legacy-%s", props->name); | |
581 | if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) { | |
582 | value = object_property_get_str(OBJECT(dev), legacy_name, &err); | |
583 | } else { | |
dae3bda4 | 584 | value = object_property_print(OBJECT(dev), props->name, true, &err); |
d822979b PB |
585 | } |
586 | g_free(legacy_name); | |
587 | ||
588 | if (err) { | |
589 | error_free(err); | |
590 | continue; | |
ee46d8a5 | 591 | } |
bce54474 | 592 | qdev_printf("%s = %s\n", props->name, |
d822979b PB |
593 | value && *value ? value : "<null>"); |
594 | g_free(value); | |
ee46d8a5 AL |
595 | } |
596 | } | |
597 | ||
0d936928 AL |
598 | static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent) |
599 | { | |
600 | BusClass *bc = BUS_GET_CLASS(bus); | |
601 | ||
602 | if (bc->print_dev) { | |
603 | bc->print_dev(mon, dev, indent); | |
604 | } | |
605 | } | |
606 | ||
ee46d8a5 AL |
607 | static void qdev_print(Monitor *mon, DeviceState *dev, int indent) |
608 | { | |
bce54474 | 609 | ObjectClass *class; |
ee46d8a5 | 610 | BusState *child; |
a5f54290 PC |
611 | NamedGPIOList *ngl; |
612 | ||
ee46d8a5 AL |
613 | qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)), |
614 | dev->id ? dev->id : ""); | |
615 | indent += 2; | |
a5f54290 PC |
616 | QLIST_FOREACH(ngl, &dev->gpios, node) { |
617 | if (ngl->num_in) { | |
618 | qdev_printf("gpio-in \"%s\" %d\n", ngl->name ? ngl->name : "", | |
619 | ngl->num_in); | |
620 | } | |
621 | if (ngl->num_out) { | |
622 | qdev_printf("gpio-out \"%s\" %d\n", ngl->name ? ngl->name : "", | |
623 | ngl->num_out); | |
624 | } | |
ee46d8a5 | 625 | } |
bce54474 PB |
626 | class = object_get_class(OBJECT(dev)); |
627 | do { | |
628 | qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent); | |
629 | class = object_class_get_parent(class); | |
630 | } while (class != object_class_by_name(TYPE_DEVICE)); | |
da9fbe76 | 631 | bus_print_dev(dev->parent_bus, mon, dev, indent); |
ee46d8a5 AL |
632 | QLIST_FOREACH(child, &dev->child_bus, sibling) { |
633 | qbus_print(mon, child, indent); | |
634 | } | |
635 | } | |
636 | ||
637 | static void qbus_print(Monitor *mon, BusState *bus, int indent) | |
638 | { | |
0866aca1 | 639 | BusChild *kid; |
ee46d8a5 AL |
640 | |
641 | qdev_printf("bus: %s\n", bus->name); | |
642 | indent += 2; | |
0d936928 | 643 | qdev_printf("type %s\n", object_get_typename(OBJECT(bus))); |
0866aca1 AL |
644 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
645 | DeviceState *dev = kid->child; | |
ee46d8a5 AL |
646 | qdev_print(mon, dev, indent); |
647 | } | |
648 | } | |
649 | #undef qdev_printf | |
650 | ||
84f2d0ea | 651 | void do_info_qtree(Monitor *mon, const QDict *qdict) |
ee46d8a5 AL |
652 | { |
653 | if (sysbus_get_default()) | |
654 | qbus_print(mon, sysbus_get_default(), 0); | |
655 | } | |
656 | ||
84f2d0ea | 657 | void do_info_qdm(Monitor *mon, const QDict *qdict) |
ee46d8a5 | 658 | { |
a3400aee | 659 | qdev_print_devinfos(true); |
ee46d8a5 AL |
660 | } |
661 | ||
662 | int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data) | |
663 | { | |
4e89978e | 664 | Error *local_err = NULL; |
ee46d8a5 | 665 | QemuOpts *opts; |
b09995ae | 666 | DeviceState *dev; |
ee46d8a5 | 667 | |
4e89978e | 668 | opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, &local_err); |
84d18f06 | 669 | if (local_err) { |
4e89978e LC |
670 | qerror_report_err(local_err); |
671 | error_free(local_err); | |
ee46d8a5 AL |
672 | return -1; |
673 | } | |
674 | if (!monitor_cur_is_qmp() && qdev_device_help(opts)) { | |
675 | qemu_opts_del(opts); | |
676 | return 0; | |
677 | } | |
b09995ae PB |
678 | dev = qdev_device_add(opts); |
679 | if (!dev) { | |
ee46d8a5 AL |
680 | qemu_opts_del(opts); |
681 | return -1; | |
682 | } | |
b09995ae | 683 | object_unref(OBJECT(dev)); |
ee46d8a5 AL |
684 | return 0; |
685 | } | |
686 | ||
a15fef21 | 687 | void qmp_device_del(const char *id, Error **errp) |
ee46d8a5 | 688 | { |
ee46d8a5 AL |
689 | DeviceState *dev; |
690 | ||
691 | dev = qdev_find_recursive(sysbus_get_default(), id); | |
8108fd3e | 692 | if (!dev) { |
a15fef21 LC |
693 | error_set(errp, QERR_DEVICE_NOT_FOUND, id); |
694 | return; | |
56f9107e LC |
695 | } |
696 | ||
a15fef21 | 697 | qdev_unplug(dev, errp); |
ee46d8a5 AL |
698 | } |
699 | ||
700 | void qdev_machine_init(void) | |
701 | { | |
702 | qdev_get_peripheral_anon(); | |
703 | qdev_get_peripheral(); | |
704 | } | |
4d454574 PB |
705 | |
706 | QemuOptsList qemu_device_opts = { | |
707 | .name = "device", | |
708 | .implied_opt_name = "driver", | |
709 | .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head), | |
710 | .desc = { | |
711 | /* | |
712 | * no elements => accept any | |
713 | * sanity checking will happen later | |
714 | * when setting device properties | |
715 | */ | |
716 | { /* end of list */ } | |
717 | }, | |
718 | }; | |
719 | ||
720 | QemuOptsList qemu_global_opts = { | |
721 | .name = "global", | |
722 | .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head), | |
723 | .desc = { | |
724 | { | |
725 | .name = "driver", | |
726 | .type = QEMU_OPT_STRING, | |
727 | },{ | |
728 | .name = "property", | |
729 | .type = QEMU_OPT_STRING, | |
730 | },{ | |
731 | .name = "value", | |
732 | .type = QEMU_OPT_STRING, | |
733 | }, | |
734 | { /* end of list */ } | |
735 | }, | |
736 | }; | |
737 | ||
738 | int qemu_global_option(const char *str) | |
739 | { | |
740 | char driver[64], property[64]; | |
741 | QemuOpts *opts; | |
742 | int rc, offset; | |
743 | ||
744 | rc = sscanf(str, "%63[^.].%63[^=]%n", driver, property, &offset); | |
745 | if (rc < 2 || str[offset] != '=') { | |
746 | error_report("can't parse: \"%s\"", str); | |
747 | return -1; | |
748 | } | |
749 | ||
87ea75d5 | 750 | opts = qemu_opts_create(&qemu_global_opts, NULL, 0, &error_abort); |
4d454574 PB |
751 | qemu_opt_set(opts, "driver", driver); |
752 | qemu_opt_set(opts, "property", property); | |
753 | qemu_opt_set(opts, "value", str+offset+1); | |
754 | return 0; | |
755 | } |