]> git.proxmox.com Git - qemu.git/blame - hw/qdev.c
vmware_vga: refactor device creation
[qemu.git] / hw / qdev.c
CommitLineData
aae9460e
PB
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
8167ee88 17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
aae9460e
PB
18 */
19
20/* The theory here is that it should be possible to create a machine without
21 knowledge of specific devices. Historically board init routines have
22 passed a bunch of arguments to each device, requiring the board know
23 exactly which device it is dealing with. This file provides an abstract
24 API for device configuration and initialization. Devices will generally
25 inherit from a particular bus (e.g. PCI or I2C) rather than
26 this API directly. */
27
9d07d757 28#include "net.h"
aae9460e
PB
29#include "qdev.h"
30#include "sysemu.h"
cae4956e 31#include "monitor.h"
aae9460e 32
3418bd25 33static int qdev_hotplug = 0;
0ac8ef71
AW
34static bool qdev_hot_added = false;
35static bool qdev_hot_removed = false;
3418bd25 36
cdaed7c7 37/* This is a nasty hack to allow passing a NULL bus to qdev_create. */
b9aaf7f8 38static BusState *main_system_bus;
4d6ae674 39
0958b4cc 40DeviceInfo *device_info_list;
aae9460e 41
8ffb1bcf
GH
42static BusState *qbus_find_recursive(BusState *bus, const char *name,
43 const BusInfo *info);
44static BusState *qbus_find(const char *path);
45
aae9460e 46/* Register a new device type. */
074f2fff 47void qdev_register(DeviceInfo *info)
aae9460e 48{
074f2fff 49 assert(info->size >= sizeof(DeviceState));
042f84d0 50 assert(!info->next);
aae9460e 51
042f84d0
GH
52 info->next = device_info_list;
53 device_info_list = info;
aae9460e
PB
54}
55
81ebb98b
GH
56static DeviceInfo *qdev_find_info(BusInfo *bus_info, const char *name)
57{
58 DeviceInfo *info;
59
3320e56e 60 /* first check device names */
81ebb98b
GH
61 for (info = device_info_list; info != NULL; info = info->next) {
62 if (bus_info && info->bus_info != bus_info)
63 continue;
64 if (strcmp(info->name, name) != 0)
65 continue;
66 return info;
67 }
3320e56e
GH
68
69 /* failing that check the aliases */
70 for (info = device_info_list; info != NULL; info = info->next) {
71 if (bus_info && info->bus_info != bus_info)
72 continue;
73 if (!info->alias)
74 continue;
75 if (strcmp(info->alias, name) != 0)
76 continue;
77 return info;
78 }
81ebb98b
GH
79 return NULL;
80}
81
0c17542d
MA
82static DeviceState *qdev_create_from_info(BusState *bus, DeviceInfo *info)
83{
84 DeviceState *dev;
85
86 assert(bus->info == info->bus_info);
87 dev = qemu_mallocz(info->size);
88 dev->info = info;
89 dev->parent_bus = bus;
90 qdev_prop_set_defaults(dev, dev->info->props);
91 qdev_prop_set_defaults(dev, dev->parent_bus->info->props);
92 qdev_prop_set_globals(dev);
93 QLIST_INSERT_HEAD(&bus->children, dev, sibling);
94 if (qdev_hotplug) {
95 assert(bus->allow_hotplug);
96 dev->hotplugged = 1;
0ac8ef71 97 qdev_hot_added = true;
0c17542d 98 }
4d2ffa08 99 dev->instance_id_alias = -1;
0c17542d
MA
100 dev->state = DEV_STATE_CREATED;
101 return dev;
102}
103
aae9460e
PB
104/* Create a new device. This only initializes the device state structure
105 and allows properties to be set. qdev_init should be called to
106 initialize the actual device emulation. */
02e2da45 107DeviceState *qdev_create(BusState *bus, const char *name)
aae9460e 108{
042f84d0 109 DeviceInfo *info;
aae9460e 110
10c4c98a 111 if (!bus) {
68694897 112 bus = sysbus_get_default();
10c4c98a
GH
113 }
114
81ebb98b 115 info = qdev_find_info(bus->info, name);
042f84d0 116 if (!info) {
10c4c98a 117 hw_error("Unknown device '%s' for bus '%s'\n", name, bus->info->name);
aae9460e
PB
118 }
119
0c17542d 120 return qdev_create_from_info(bus, info);
aae9460e
PB
121}
122
8a9662ca 123static void qdev_print_devinfo(DeviceInfo *info)
1b524b04 124{
8a9662ca
MA
125 error_printf("name \"%s\", bus %s",
126 info->name, info->bus_info->name);
22f2e344 127 if (info->alias) {
8a9662ca 128 error_printf(", alias \"%s\"", info->alias);
22f2e344
GH
129 }
130 if (info->desc) {
8a9662ca 131 error_printf(", desc \"%s\"", info->desc);
22f2e344
GH
132 }
133 if (info->no_user) {
8a9662ca 134 error_printf(", no-user");
22f2e344 135 }
8a9662ca 136 error_printf("\n");
1b524b04
GH
137}
138
f31d07d1 139static int set_property(const char *name, const char *value, void *opaque)
8ffb1bcf 140{
f31d07d1
GH
141 DeviceState *dev = opaque;
142
143 if (strcmp(name, "driver") == 0)
144 return 0;
145 if (strcmp(name, "bus") == 0)
146 return 0;
147
3df04ac3 148 if (qdev_prop_parse(dev, name, value) == -1) {
f31d07d1
GH
149 return -1;
150 }
151 return 0;
152}
153
ff952ba2
MA
154int qdev_device_help(QemuOpts *opts)
155{
156 const char *driver;
157 DeviceInfo *info;
08350cf0 158 Property *prop;
ff952ba2
MA
159
160 driver = qemu_opt_get(opts, "driver");
161 if (driver && !strcmp(driver, "?")) {
162 for (info = device_info_list; info != NULL; info = info->next) {
c64eafaf
MA
163 if (info->no_user) {
164 continue; /* not available, don't show */
165 }
8a9662ca 166 qdev_print_devinfo(info);
ff952ba2
MA
167 }
168 return 1;
169 }
170
08350cf0
MA
171 if (!qemu_opt_get(opts, "?")) {
172 return 0;
173 }
174
175 info = qdev_find_info(NULL, driver);
176 if (!info) {
177 return 0;
178 }
179
180 for (prop = info->props; prop && prop->name; prop++) {
036f7166
MA
181 /*
182 * TODO Properties without a parser are just for dirty hacks.
183 * qdev_prop_ptr is the only such PropertyInfo. It's marked
184 * for removal. This conditional should be removed along with
185 * it.
186 */
187 if (!prop->info->parse) {
188 continue; /* no way to set it, don't show */
189 }
8a9662ca 190 error_printf("%s.%s=%s\n", info->name, prop->name, prop->info->name);
08350cf0
MA
191 }
192 return 1;
ff952ba2
MA
193}
194
f31d07d1
GH
195DeviceState *qdev_device_add(QemuOpts *opts)
196{
197 const char *driver, *path, *id;
8ffb1bcf
GH
198 DeviceInfo *info;
199 DeviceState *qdev;
200 BusState *bus;
8ffb1bcf 201
f31d07d1
GH
202 driver = qemu_opt_get(opts, "driver");
203 if (!driver) {
0204276b 204 qerror_report(QERR_MISSING_PARAMETER, "driver");
8ffb1bcf
GH
205 return NULL;
206 }
f31d07d1
GH
207
208 /* find driver */
8ffb1bcf 209 info = qdev_find_info(NULL, driver);
c64eafaf 210 if (!info || info->no_user) {
e17ba87c 211 qerror_report(QERR_INVALID_PARAMETER_VALUE, "driver", "a driver name");
0204276b 212 error_printf_unless_qmp("Try with argument '?' for a list.\n");
8ffb1bcf
GH
213 return NULL;
214 }
8ffb1bcf 215
f31d07d1
GH
216 /* find bus */
217 path = qemu_opt_get(opts, "bus");
218 if (path != NULL) {
8ffb1bcf 219 bus = qbus_find(path);
ac8dae67
MA
220 if (!bus) {
221 return NULL;
222 }
223 if (bus->info != info->bus_info) {
0204276b
MA
224 qerror_report(QERR_BAD_BUS_FOR_DEVICE,
225 driver, bus->info->name);
327867b6
MA
226 return NULL;
227 }
8ffb1bcf
GH
228 } else {
229 bus = qbus_find_recursive(main_system_bus, NULL, info->bus_info);
ac8dae67 230 if (!bus) {
0204276b
MA
231 qerror_report(QERR_NO_BUS_FOR_DEVICE,
232 info->name, info->bus_info->name);
ac8dae67
MA
233 return NULL;
234 }
75570088 235 }
3418bd25 236 if (qdev_hotplug && !bus->allow_hotplug) {
0204276b 237 qerror_report(QERR_BUS_NO_HOTPLUG, bus->name);
3418bd25
GH
238 return NULL;
239 }
8ffb1bcf 240
f31d07d1 241 /* create device, set properties */
0c17542d 242 qdev = qdev_create_from_info(bus, info);
f31d07d1
GH
243 id = qemu_opts_id(opts);
244 if (id) {
245 qdev->id = id;
246 }
247 if (qemu_opt_foreach(opts, set_property, qdev, 1) != 0) {
248 qdev_free(qdev);
249 return NULL;
8ffb1bcf 250 }
5c17ca25 251 if (qdev_init(qdev) < 0) {
0204276b 252 qerror_report(QERR_DEVICE_INIT_FAILED, driver);
81a322d4
GH
253 return NULL;
254 }
ef80b466 255 qdev->opts = opts;
8ffb1bcf
GH
256 return qdev;
257}
258
aae9460e
PB
259/* Initialize a device. Device properties should be set before calling
260 this function. IRQs and MMIO regions should be connected/mapped after
18cfeb52
MA
261 calling this function.
262 On failure, destroy the device and return negative value.
263 Return 0 on success. */
81a322d4 264int qdev_init(DeviceState *dev)
aae9460e 265{
959f733a
GH
266 int rc;
267
131ec1bd 268 assert(dev->state == DEV_STATE_CREATED);
959f733a 269 rc = dev->info->init(dev, dev->info);
18cfeb52
MA
270 if (rc < 0) {
271 qdev_free(dev);
959f733a 272 return rc;
18cfeb52 273 }
4d2ffa08 274 if (dev->info->vmsd) {
0be71e32 275 vmstate_register_with_alias_id(dev, -1, dev->info->vmsd, dev,
4d2ffa08
JK
276 dev->instance_id_alias,
277 dev->alias_required_for_version);
278 }
131ec1bd 279 dev->state = DEV_STATE_INITIALIZED;
959f733a 280 return 0;
02e2da45
PB
281}
282
4d2ffa08
JK
283void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
284 int required_for_version)
285{
286 assert(dev->state == DEV_STATE_CREATED);
287 dev->instance_id_alias = alias_id;
288 dev->alias_required_for_version = required_for_version;
289}
290
3418bd25
GH
291int qdev_unplug(DeviceState *dev)
292{
293 if (!dev->parent_bus->allow_hotplug) {
cc601cb7 294 qerror_report(QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
3418bd25
GH
295 return -1;
296 }
593831de
AS
297 assert(dev->info->unplug != NULL);
298
0ac8ef71
AW
299 qdev_hot_removed = true;
300
3418bd25
GH
301 return dev->info->unplug(dev);
302}
303
ec990eb6
AL
304static int qdev_reset_one(DeviceState *dev, void *opaque)
305{
306 if (dev->info->reset) {
307 dev->info->reset(dev);
308 }
309
310 return 0;
311}
312
313BusState *sysbus_get_default(void)
314{
68694897
SW
315 if (!main_system_bus) {
316 main_system_bus = qbus_create(&system_bus_info, NULL,
317 "main-system-bus");
318 }
ec990eb6
AL
319 return main_system_bus;
320}
321
b4694b7c
IY
322static int qbus_reset_one(BusState *bus, void *opaque)
323{
324 if (bus->info->reset) {
325 return bus->info->reset(bus);
326 }
327 return 0;
328}
329
5af0a04b
IY
330void qdev_reset_all(DeviceState *dev)
331{
332 qdev_walk_children(dev, qdev_reset_one, qbus_reset_one, NULL);
333}
334
80376c3f
IY
335void qbus_reset_all_fn(void *opaque)
336{
337 BusState *bus = opaque;
f530cce3 338 qbus_walk_children(bus, qdev_reset_one, qbus_reset_one, NULL);
80376c3f
IY
339}
340
3418bd25
GH
341/* can be used as ->unplug() callback for the simple cases */
342int qdev_simple_unplug_cb(DeviceState *dev)
343{
344 /* just zap it */
345 qdev_free(dev);
346 return 0;
347}
348
e23a1b33
MA
349/* Like qdev_init(), but terminate program via hw_error() instead of
350 returning an error value. This is okay during machine creation.
351 Don't use for hotplug, because there callers need to recover from
352 failure. Exception: if you know the device's init() callback can't
353 fail, then qdev_init_nofail() can't fail either, and is therefore
354 usable even then. But relying on the device implementation that
355 way is somewhat unclean, and best avoided. */
356void qdev_init_nofail(DeviceState *dev)
357{
358 DeviceInfo *info = dev->info;
359
bd6c9a61
MA
360 if (qdev_init(dev) < 0) {
361 error_report("Initialization of device %s failed\n", info->name);
362 exit(1);
363 }
e23a1b33
MA
364}
365
02e2da45
PB
366/* Unlink device from bus and free the structure. */
367void qdev_free(DeviceState *dev)
368{
131ec1bd 369 BusState *bus;
d21357df 370 Property *prop;
131ec1bd
GH
371
372 if (dev->state == DEV_STATE_INITIALIZED) {
373 while (dev->num_child_bus) {
374 bus = QLIST_FIRST(&dev->child_bus);
375 qbus_free(bus);
376 }
131ec1bd 377 if (dev->info->vmsd)
0be71e32 378 vmstate_unregister(dev, dev->info->vmsd, dev);
d29275f1
GH
379 if (dev->info->exit)
380 dev->info->exit(dev);
ef80b466
GH
381 if (dev->opts)
382 qemu_opts_del(dev->opts);
131ec1bd 383 }
72cf2d4f 384 QLIST_REMOVE(dev, sibling);
d21357df
MA
385 for (prop = dev->info->props; prop && prop->name; prop++) {
386 if (prop->info->free) {
387 prop->info->free(dev, prop);
388 }
389 }
ccb63de3 390 qemu_free(dev);
aae9460e
PB
391}
392
3418bd25
GH
393void qdev_machine_creation_done(void)
394{
395 /*
396 * ok, initial machine setup is done, starting from now we can
397 * only create hotpluggable devices
398 */
399 qdev_hotplug = 1;
400}
401
0ac8ef71
AW
402bool qdev_machine_modified(void)
403{
404 return qdev_hot_added || qdev_hot_removed;
405}
406
aae9460e
PB
407/* Get a character (serial) device interface. */
408CharDriverState *qdev_init_chardev(DeviceState *dev)
409{
410 static int next_serial;
98b19252
AS
411
412 /* FIXME: This function needs to go away: use chardev properties! */
413 return serial_hds[next_serial++];
aae9460e
PB
414}
415
02e2da45 416BusState *qdev_get_parent_bus(DeviceState *dev)
aae9460e 417{
02e2da45 418 return dev->parent_bus;
aae9460e
PB
419}
420
aae9460e
PB
421void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
422{
423 assert(dev->num_gpio_in == 0);
424 dev->num_gpio_in = n;
425 dev->gpio_in = qemu_allocate_irqs(handler, dev, n);
426}
427
428void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
429{
430 assert(dev->num_gpio_out == 0);
431 dev->num_gpio_out = n;
432 dev->gpio_out = pins;
433}
434
435qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
436{
437 assert(n >= 0 && n < dev->num_gpio_in);
438 return dev->gpio_in[n];
439}
440
441void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
442{
443 assert(n >= 0 && n < dev->num_gpio_out);
444 dev->gpio_out[n] = pin;
445}
446
ed16ab5a
GH
447void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd)
448{
449 qdev_prop_set_macaddr(dev, "mac", nd->macaddr);
450 if (nd->vlan)
451 qdev_prop_set_vlan(dev, "vlan", nd->vlan);
452 if (nd->netdev)
453 qdev_prop_set_netdev(dev, "netdev", nd->netdev);
75422b0d 454 if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
97b15621
GH
455 qdev_prop_exists(dev, "vectors")) {
456 qdev_prop_set_uint32(dev, "vectors", nd->nvectors);
457 }
ed16ab5a
GH
458}
459
02e2da45 460BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
4d6ae674 461{
02e2da45 462 BusState *bus;
4d6ae674 463
72cf2d4f 464 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
4d6ae674 465 if (strcmp(name, bus->name) == 0) {
02e2da45 466 return bus;
4d6ae674
PB
467 }
468 }
469 return NULL;
470}
471
81699d8a
AL
472int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
473 qbus_walkerfn *busfn, void *opaque)
474{
475 DeviceState *dev;
476 int err;
477
478 if (busfn) {
479 err = busfn(bus, opaque);
480 if (err) {
481 return err;
482 }
483 }
484
485 QLIST_FOREACH(dev, &bus->children, sibling) {
486 err = qdev_walk_children(dev, devfn, busfn, opaque);
487 if (err < 0) {
488 return err;
489 }
490 }
491
492 return 0;
493}
494
495int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
496 qbus_walkerfn *busfn, void *opaque)
497{
498 BusState *bus;
499 int err;
500
501 if (devfn) {
502 err = devfn(dev, opaque);
503 if (err) {
504 return err;
505 }
506 }
507
508 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
509 err = qbus_walk_children(bus, devfn, busfn, opaque);
510 if (err < 0) {
511 return err;
512 }
513 }
514
515 return 0;
516}
517
8ffb1bcf
GH
518static BusState *qbus_find_recursive(BusState *bus, const char *name,
519 const BusInfo *info)
520{
521 DeviceState *dev;
522 BusState *child, *ret;
523 int match = 1;
524
525 if (name && (strcmp(bus->name, name) != 0)) {
526 match = 0;
527 }
528 if (info && (bus->info != info)) {
529 match = 0;
530 }
531 if (match) {
532 return bus;
533 }
534
72cf2d4f
BS
535 QLIST_FOREACH(dev, &bus->children, sibling) {
536 QLIST_FOREACH(child, &dev->child_bus, sibling) {
8ffb1bcf
GH
537 ret = qbus_find_recursive(child, name, info);
538 if (ret) {
539 return ret;
540 }
541 }
542 }
543 return NULL;
544}
545
a2ee6b4f 546DeviceState *qdev_find_recursive(BusState *bus, const char *id)
3418bd25
GH
547{
548 DeviceState *dev, *ret;
549 BusState *child;
550
551 QLIST_FOREACH(dev, &bus->children, sibling) {
552 if (dev->id && strcmp(dev->id, id) == 0)
553 return dev;
554 QLIST_FOREACH(child, &dev->child_bus, sibling) {
555 ret = qdev_find_recursive(child, id);
556 if (ret) {
557 return ret;
558 }
559 }
560 }
561 return NULL;
562}
563
53db16b5 564static void qbus_list_bus(DeviceState *dev)
8ffb1bcf
GH
565{
566 BusState *child;
567 const char *sep = " ";
8ffb1bcf 568
53db16b5
MA
569 error_printf("child busses at \"%s\":",
570 dev->id ? dev->id : dev->info->name);
72cf2d4f 571 QLIST_FOREACH(child, &dev->child_bus, sibling) {
53db16b5 572 error_printf("%s\"%s\"", sep, child->name);
8ffb1bcf
GH
573 sep = ", ";
574 }
53db16b5 575 error_printf("\n");
8ffb1bcf
GH
576}
577
53db16b5 578static void qbus_list_dev(BusState *bus)
8ffb1bcf
GH
579{
580 DeviceState *dev;
581 const char *sep = " ";
8ffb1bcf 582
53db16b5 583 error_printf("devices at \"%s\":", bus->name);
72cf2d4f 584 QLIST_FOREACH(dev, &bus->children, sibling) {
53db16b5 585 error_printf("%s\"%s\"", sep, dev->info->name);
8ffb1bcf 586 if (dev->id)
53db16b5 587 error_printf("/\"%s\"", dev->id);
8ffb1bcf
GH
588 sep = ", ";
589 }
53db16b5 590 error_printf("\n");
8ffb1bcf
GH
591}
592
593static BusState *qbus_find_bus(DeviceState *dev, char *elem)
594{
595 BusState *child;
596
72cf2d4f 597 QLIST_FOREACH(child, &dev->child_bus, sibling) {
8ffb1bcf
GH
598 if (strcmp(child->name, elem) == 0) {
599 return child;
600 }
601 }
602 return NULL;
603}
604
605static DeviceState *qbus_find_dev(BusState *bus, char *elem)
606{
607 DeviceState *dev;
608
609 /*
610 * try to match in order:
611 * (1) instance id, if present
612 * (2) driver name
613 * (3) driver alias, if present
614 */
72cf2d4f 615 QLIST_FOREACH(dev, &bus->children, sibling) {
8ffb1bcf
GH
616 if (dev->id && strcmp(dev->id, elem) == 0) {
617 return dev;
618 }
619 }
72cf2d4f 620 QLIST_FOREACH(dev, &bus->children, sibling) {
8ffb1bcf
GH
621 if (strcmp(dev->info->name, elem) == 0) {
622 return dev;
623 }
624 }
72cf2d4f 625 QLIST_FOREACH(dev, &bus->children, sibling) {
8ffb1bcf
GH
626 if (dev->info->alias && strcmp(dev->info->alias, elem) == 0) {
627 return dev;
628 }
629 }
630 return NULL;
631}
632
633static BusState *qbus_find(const char *path)
634{
635 DeviceState *dev;
636 BusState *bus;
53db16b5 637 char elem[128];
8ffb1bcf
GH
638 int pos, len;
639
640 /* find start element */
641 if (path[0] == '/') {
642 bus = main_system_bus;
643 pos = 0;
644 } else {
645 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
fc98eb43
MA
646 assert(!path[0]);
647 elem[0] = len = 0;
8ffb1bcf
GH
648 }
649 bus = qbus_find_recursive(main_system_bus, elem, NULL);
650 if (!bus) {
ac8dae67 651 qerror_report(QERR_BUS_NOT_FOUND, elem);
8ffb1bcf
GH
652 return NULL;
653 }
654 pos = len;
655 }
656
657 for (;;) {
fc98eb43
MA
658 assert(path[pos] == '/' || !path[pos]);
659 while (path[pos] == '/') {
660 pos++;
661 }
8ffb1bcf 662 if (path[pos] == '\0') {
8ffb1bcf
GH
663 return bus;
664 }
665
666 /* find device */
fc98eb43
MA
667 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
668 assert(0);
669 elem[0] = len = 0;
8ffb1bcf
GH
670 }
671 pos += len;
672 dev = qbus_find_dev(bus, elem);
673 if (!dev) {
ac8dae67 674 qerror_report(QERR_DEVICE_NOT_FOUND, elem);
8bc27249
MA
675 if (!monitor_cur_is_qmp()) {
676 qbus_list_dev(bus);
677 }
8ffb1bcf
GH
678 return NULL;
679 }
fc98eb43
MA
680
681 assert(path[pos] == '/' || !path[pos]);
682 while (path[pos] == '/') {
683 pos++;
684 }
8ffb1bcf
GH
685 if (path[pos] == '\0') {
686 /* last specified element is a device. If it has exactly
687 * one child bus accept it nevertheless */
688 switch (dev->num_child_bus) {
689 case 0:
ac8dae67 690 qerror_report(QERR_DEVICE_NO_BUS, elem);
8ffb1bcf
GH
691 return NULL;
692 case 1:
72cf2d4f 693 return QLIST_FIRST(&dev->child_bus);
8ffb1bcf 694 default:
ac8dae67 695 qerror_report(QERR_DEVICE_MULTIPLE_BUSSES, elem);
8bc27249
MA
696 if (!monitor_cur_is_qmp()) {
697 qbus_list_bus(dev);
698 }
8ffb1bcf
GH
699 return NULL;
700 }
701 }
702
703 /* find bus */
fc98eb43
MA
704 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
705 assert(0);
706 elem[0] = len = 0;
8ffb1bcf
GH
707 }
708 pos += len;
709 bus = qbus_find_bus(dev, elem);
710 if (!bus) {
ac8dae67 711 qerror_report(QERR_BUS_NOT_FOUND, elem);
8bc27249
MA
712 if (!monitor_cur_is_qmp()) {
713 qbus_list_bus(dev);
714 }
8ffb1bcf
GH
715 return NULL;
716 }
717 }
718}
719
cd739fb6
GH
720void qbus_create_inplace(BusState *bus, BusInfo *info,
721 DeviceState *parent, const char *name)
02e2da45 722{
d271de9f
GH
723 char *buf;
724 int i,len;
02e2da45 725
10c4c98a 726 bus->info = info;
02e2da45 727 bus->parent = parent;
d271de9f
GH
728
729 if (name) {
730 /* use supplied name */
731 bus->name = qemu_strdup(name);
732 } else if (parent && parent->id) {
733 /* parent device has id -> use it for bus name */
734 len = strlen(parent->id) + 16;
735 buf = qemu_malloc(len);
736 snprintf(buf, len, "%s.%d", parent->id, parent->num_child_bus);
737 bus->name = buf;
738 } else {
739 /* no id -> use lowercase bus type for bus name */
740 len = strlen(info->name) + 16;
741 buf = qemu_malloc(len);
742 len = snprintf(buf, len, "%s.%d", info->name,
743 parent ? parent->num_child_bus : 0);
744 for (i = 0; i < len; i++)
bb87ece5 745 buf[i] = qemu_tolower(buf[i]);
d271de9f
GH
746 bus->name = buf;
747 }
748
72cf2d4f 749 QLIST_INIT(&bus->children);
02e2da45 750 if (parent) {
72cf2d4f 751 QLIST_INSERT_HEAD(&parent->child_bus, bus, sibling);
d271de9f 752 parent->num_child_bus++;
80376c3f
IY
753 } else if (bus != main_system_bus) {
754 /* TODO: once all bus devices are qdevified,
755 only reset handler for main_system_bus should be registered here. */
756 qemu_register_reset(qbus_reset_all_fn, bus);
02e2da45 757 }
cd739fb6
GH
758}
759
760BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name)
761{
762 BusState *bus;
763
764 bus = qemu_mallocz(info->size);
765 bus->qdev_allocated = 1;
766 qbus_create_inplace(bus, info, parent, name);
02e2da45
PB
767 return bus;
768}
cae4956e 769
131ec1bd
GH
770void qbus_free(BusState *bus)
771{
772 DeviceState *dev;
773
774 while ((dev = QLIST_FIRST(&bus->children)) != NULL) {
775 qdev_free(dev);
776 }
777 if (bus->parent) {
778 QLIST_REMOVE(bus, sibling);
779 bus->parent->num_child_bus--;
80376c3f
IY
780 } else {
781 assert(bus != main_system_bus); /* main_system_bus is never freed */
782 qemu_unregister_reset(qbus_reset_all_fn, bus);
131ec1bd 783 }
e163ae7b 784 qemu_free((void*)bus->name);
131ec1bd
GH
785 if (bus->qdev_allocated) {
786 qemu_free(bus);
787 }
788}
789
cae4956e
GH
790#define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
791static void qbus_print(Monitor *mon, BusState *bus, int indent);
792
ee6847d1
GH
793static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
794 const char *prefix, int indent)
795{
796 char buf[64];
797
798 if (!props)
799 return;
800 while (props->name) {
036f7166
MA
801 /*
802 * TODO Properties without a print method are just for dirty
803 * hacks. qdev_prop_ptr is the only such PropertyInfo. It's
804 * marked for removal. The test props->info->print should be
805 * removed along with it.
806 */
ee6847d1
GH
807 if (props->info->print) {
808 props->info->print(dev, props, buf, sizeof(buf));
809 qdev_printf("%s-prop: %s = %s\n", prefix, props->name, buf);
810 }
811 props++;
812 }
813}
814
cae4956e
GH
815static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
816{
cae4956e 817 BusState *child;
ccb63de3
GH
818 qdev_printf("dev: %s, id \"%s\"\n", dev->info->name,
819 dev->id ? dev->id : "");
cae4956e
GH
820 indent += 2;
821 if (dev->num_gpio_in) {
822 qdev_printf("gpio-in %d\n", dev->num_gpio_in);
823 }
824 if (dev->num_gpio_out) {
825 qdev_printf("gpio-out %d\n", dev->num_gpio_out);
826 }
ee6847d1
GH
827 qdev_print_props(mon, dev, dev->info->props, "dev", indent);
828 qdev_print_props(mon, dev, dev->parent_bus->info->props, "bus", indent);
10c4c98a
GH
829 if (dev->parent_bus->info->print_dev)
830 dev->parent_bus->info->print_dev(mon, dev, indent);
72cf2d4f 831 QLIST_FOREACH(child, &dev->child_bus, sibling) {
cae4956e
GH
832 qbus_print(mon, child, indent);
833 }
834}
835
836static void qbus_print(Monitor *mon, BusState *bus, int indent)
837{
838 struct DeviceState *dev;
839
840 qdev_printf("bus: %s\n", bus->name);
841 indent += 2;
10c4c98a 842 qdev_printf("type %s\n", bus->info->name);
72cf2d4f 843 QLIST_FOREACH(dev, &bus->children, sibling) {
cae4956e
GH
844 qdev_print(mon, dev, indent);
845 }
846}
847#undef qdev_printf
848
849void do_info_qtree(Monitor *mon)
850{
851 if (main_system_bus)
852 qbus_print(mon, main_system_bus, 0);
853}
9316d30f 854
f6c64e0e 855void do_info_qdm(Monitor *mon)
9316d30f
GH
856{
857 DeviceInfo *info;
9316d30f
GH
858
859 for (info = device_info_list; info != NULL; info = info->next) {
8a9662ca 860 qdev_print_devinfo(info);
9316d30f
GH
861 }
862}
3418bd25 863
8bc27249 864int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
3418bd25
GH
865{
866 QemuOpts *opts;
867
3329f07b 868 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict);
8bc27249
MA
869 if (!opts) {
870 return -1;
871 }
872 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
873 qemu_opts_del(opts);
874 return 0;
0f853a38 875 }
8bc27249
MA
876 if (!qdev_device_add(opts)) {
877 qemu_opts_del(opts);
878 return -1;
879 }
880 return 0;
3418bd25
GH
881}
882
17a38eaa 883int do_device_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
3418bd25
GH
884{
885 const char *id = qdict_get_str(qdict, "id");
886 DeviceState *dev;
887
888 dev = qdev_find_recursive(main_system_bus, id);
889 if (NULL == dev) {
17a38eaa
MA
890 qerror_report(QERR_DEVICE_NOT_FOUND, id);
891 return -1;
3418bd25 892 }
17a38eaa 893 return qdev_unplug(dev);
3418bd25 894}
1ca4d09a
GN
895
896static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
897{
898 int l = 0;
899
900 if (dev && dev->parent_bus) {
901 char *d;
902 l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
903 if (dev->parent_bus->info->get_fw_dev_path) {
904 d = dev->parent_bus->info->get_fw_dev_path(dev);
905 l += snprintf(p + l, size - l, "%s", d);
906 qemu_free(d);
907 } else {
908 l += snprintf(p + l, size - l, "%s", dev->info->name);
909 }
910 }
911 l += snprintf(p + l , size - l, "/");
912
913 return l;
914}
915
916char* qdev_get_fw_dev_path(DeviceState *dev)
917{
918 char path[128];
919 int l;
920
921 l = qdev_get_fw_dev_path_helper(dev, path, 128);
922
923 path[l-1] = '\0';
924
925 return strdup(path);
926}