summaryrefslogtreecommitdiff
path: root/hw/qdev.c
diff options
context:
space:
mode:
Diffstat (limited to 'hw/qdev.c')
-rw-r--r--hw/qdev.c28
1 files changed, 23 insertions, 5 deletions
diff --git a/hw/qdev.c b/hw/qdev.c
index b2d11cebb..906e89755 100644
--- a/hw/qdev.c
+++ b/hw/qdev.c
@@ -30,9 +30,9 @@
#include "sysemu.h"
#include "monitor.h"
-/* This is a nasty hack to allow passing a NULL bus to qdev_create. */
static int qdev_hotplug = 0;
+/* This is a nasty hack to allow passing a NULL bus to qdev_create. */
static BusState *main_system_bus;
static DeviceInfo *device_info_list;
@@ -214,9 +214,8 @@ DeviceState *qdev_device_add(QemuOpts *opts)
qdev_free(qdev);
return NULL;
}
- if (qdev_init(qdev) != 0) {
+ if (qdev_init(qdev) < 0) {
qemu_error("Error initializing device %s\n", driver);
- qdev_free(qdev);
return NULL;
}
qdev->opts = opts;
@@ -232,15 +231,19 @@ static void qdev_reset(void *opaque)
/* Initialize a device. Device properties should be set before calling
this function. IRQs and MMIO regions should be connected/mapped after
- calling this function. */
+ calling this function.
+ On failure, destroy the device and return negative value.
+ Return 0 on success. */
int qdev_init(DeviceState *dev)
{
int rc;
assert(dev->state == DEV_STATE_CREATED);
rc = dev->info->init(dev, dev->info);
- if (rc < 0)
+ if (rc < 0) {
+ qdev_free(dev);
return rc;
+ }
qemu_register_reset(qdev_reset, dev);
if (dev->info->vmsd)
vmstate_register(-1, dev->info->vmsd, dev);
@@ -266,6 +269,21 @@ int qdev_simple_unplug_cb(DeviceState *dev)
return 0;
}
+/* Like qdev_init(), but terminate program via hw_error() instead of
+ returning an error value. This is okay during machine creation.
+ Don't use for hotplug, because there callers need to recover from
+ failure. Exception: if you know the device's init() callback can't
+ fail, then qdev_init_nofail() can't fail either, and is therefore
+ usable even then. But relying on the device implementation that
+ way is somewhat unclean, and best avoided. */
+void qdev_init_nofail(DeviceState *dev)
+{
+ DeviceInfo *info = dev->info;
+
+ if (qdev_init(dev) < 0)
+ hw_error("Initialization of device %s failed\n", info->name);
+}
+
/* Unlink device from bus and free the structure. */
void qdev_free(DeviceState *dev)
{