#include "qdev.h"
#include "sysemu.h"
#include "monitor.h"
-#include "qerror.h"
static int qdev_hotplug = 0;
+static bool qdev_hot_added = false;
+static bool qdev_hot_removed = false;
/* This is a nasty hack to allow passing a NULL bus to qdev_create. */
static BusState *main_system_bus;
+static void main_system_bus_create(void);
DeviceInfo *device_info_list;
return NULL;
}
-/* Create a new device. This only initializes the device state structure
- and allows properties to be set. qdev_init should be called to
- initialize the actual device emulation. */
-DeviceState *qdev_create(BusState *bus, const char *name)
+static DeviceState *qdev_create_from_info(BusState *bus, DeviceInfo *info)
{
- DeviceInfo *info;
DeviceState *dev;
- if (!bus) {
- if (!main_system_bus) {
- main_system_bus = qbus_create(&system_bus_info, NULL, "main-system-bus");
- }
- bus = main_system_bus;
- }
-
- info = qdev_find_info(bus->info, name);
- if (!info) {
- hw_error("Unknown device '%s' for bus '%s'\n", name, bus->info->name);
- }
-
+ assert(bus->info == info->bus_info);
dev = qemu_mallocz(info->size);
dev->info = info;
dev->parent_bus = bus;
if (qdev_hotplug) {
assert(bus->allow_hotplug);
dev->hotplugged = 1;
+ qdev_hot_added = true;
}
+ dev->instance_id_alias = -1;
dev->state = DEV_STATE_CREATED;
return dev;
}
-static int qdev_print_devinfo(DeviceInfo *info, char *dest, int len)
+/* Create a new device. This only initializes the device state structure
+ and allows properties to be set. qdev_init should be called to
+ initialize the actual device emulation. */
+DeviceState *qdev_create(BusState *bus, const char *name)
{
- int pos = 0;
- int ret;
+ DeviceState *dev;
- ret = snprintf(dest+pos, len-pos, "name \"%s\", bus %s",
- info->name, info->bus_info->name);
- pos += MIN(len-pos,ret);
+ dev = qdev_try_create(bus, name);
+ if (!dev) {
+ hw_error("Unknown device '%s' for bus '%s'\n", name, bus->info->name);
+ }
+
+ return dev;
+}
+
+DeviceState *qdev_try_create(BusState *bus, const char *name)
+{
+ DeviceInfo *info;
+
+ if (!bus) {
+ bus = sysbus_get_default();
+ }
+
+ info = qdev_find_info(bus->info, name);
+ if (!info) {
+ return NULL;
+ }
+
+ return qdev_create_from_info(bus, info);
+}
+
+static void qdev_print_devinfo(DeviceInfo *info)
+{
+ error_printf("name \"%s\", bus %s",
+ info->name, info->bus_info->name);
if (info->alias) {
- ret = snprintf(dest+pos, len-pos, ", alias \"%s\"", info->alias);
- pos += MIN(len-pos,ret);
+ error_printf(", alias \"%s\"", info->alias);
}
if (info->desc) {
- ret = snprintf(dest+pos, len-pos, ", desc \"%s\"", info->desc);
- pos += MIN(len-pos,ret);
+ error_printf(", desc \"%s\"", info->desc);
}
if (info->no_user) {
- ret = snprintf(dest+pos, len-pos, ", no-user");
- pos += MIN(len-pos,ret);
+ error_printf(", no-user");
}
- return pos;
+ error_printf("\n");
}
static int set_property(const char *name, const char *value, void *opaque)
return 0;
if (qdev_prop_parse(dev, name, value) == -1) {
- qemu_error("can't set property \"%s\" to \"%s\" for \"%s\"\n",
- name, value, dev->info->name);
return -1;
}
return 0;
{
const char *driver;
DeviceInfo *info;
- char msg[256];
Property *prop;
driver = qemu_opt_get(opts, "driver");
if (driver && !strcmp(driver, "?")) {
for (info = device_info_list; info != NULL; info = info->next) {
- qdev_print_devinfo(info, msg, sizeof(msg));
- qemu_error("%s\n", msg);
+ if (info->no_user) {
+ continue; /* not available, don't show */
+ }
+ qdev_print_devinfo(info);
}
return 1;
}
}
for (prop = info->props; prop && prop->name; prop++) {
- qemu_error("%s.%s=%s\n", info->name, prop->name, prop->info->name);
+ /*
+ * TODO Properties without a parser are just for dirty hacks.
+ * qdev_prop_ptr is the only such PropertyInfo. It's marked
+ * for removal. This conditional should be removed along with
+ * it.
+ */
+ if (!prop->info->parse) {
+ continue; /* no way to set it, don't show */
+ }
+ error_printf("%s.%s=%s\n", info->name, prop->name, prop->info->name);
}
return 1;
}
driver = qemu_opt_get(opts, "driver");
if (!driver) {
- qemu_error("-device: no driver specified\n");
+ qerror_report(QERR_MISSING_PARAMETER, "driver");
return NULL;
}
/* find driver */
info = qdev_find_info(NULL, driver);
- if (!info) {
- qemu_error_new(QERR_DEVICE_NOT_FOUND, driver);
- return NULL;
- }
- if (info->no_user) {
- qemu_error("device \"%s\" can't be added via command line\n",
- info->name);
+ if (!info || info->no_user) {
+ qerror_report(QERR_INVALID_PARAMETER_VALUE, "driver", "a driver name");
+ error_printf_unless_qmp("Try with argument '?' for a list.\n");
return NULL;
}
path = qemu_opt_get(opts, "bus");
if (path != NULL) {
bus = qbus_find(path);
+ if (!bus) {
+ return NULL;
+ }
+ if (bus->info != info->bus_info) {
+ qerror_report(QERR_BAD_BUS_FOR_DEVICE,
+ driver, bus->info->name);
+ return NULL;
+ }
} else {
bus = qbus_find_recursive(main_system_bus, NULL, info->bus_info);
- }
- if (!bus) {
- qemu_error("Did not find %s bus for %s\n",
- path ? path : info->bus_info->name, info->name);
- return NULL;
+ if (!bus) {
+ qerror_report(QERR_NO_BUS_FOR_DEVICE,
+ info->name, info->bus_info->name);
+ return NULL;
+ }
}
if (qdev_hotplug && !bus->allow_hotplug) {
- qemu_error("Bus %s does not support hotplugging\n",
- bus->name);
+ qerror_report(QERR_BUS_NO_HOTPLUG, bus->name);
return NULL;
}
/* create device, set properties */
- qdev = qdev_create(bus, driver);
+ qdev = qdev_create_from_info(bus, info);
id = qemu_opts_id(opts);
if (id) {
qdev->id = id;
return NULL;
}
if (qdev_init(qdev) < 0) {
- qemu_error("Error initializing device %s\n", driver);
+ qerror_report(QERR_DEVICE_INIT_FAILED, driver);
return NULL;
}
qdev->opts = opts;
return qdev;
}
-static void qdev_reset(void *opaque)
-{
- DeviceState *dev = opaque;
- if (dev->info->reset)
- dev->info->reset(dev);
-}
-
/* Initialize a device. Device properties should be set before calling
this function. IRQs and MMIO regions should be connected/mapped after
calling this function.
qdev_free(dev);
return rc;
}
- qemu_register_reset(qdev_reset, dev);
- if (dev->info->vmsd)
- vmstate_register(-1, dev->info->vmsd, dev);
+ if (dev->info->vmsd) {
+ vmstate_register_with_alias_id(dev, -1, dev->info->vmsd, dev,
+ dev->instance_id_alias,
+ dev->alias_required_for_version);
+ }
dev->state = DEV_STATE_INITIALIZED;
+ if (dev->hotplugged && dev->info->reset) {
+ dev->info->reset(dev);
+ }
return 0;
}
+void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
+ int required_for_version)
+{
+ assert(dev->state == DEV_STATE_CREATED);
+ dev->instance_id_alias = alias_id;
+ dev->alias_required_for_version = required_for_version;
+}
+
int qdev_unplug(DeviceState *dev)
{
if (!dev->parent_bus->allow_hotplug) {
- qemu_error("Bus %s does not support hotplugging\n",
- dev->parent_bus->name);
+ qerror_report(QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
return -1;
}
assert(dev->info->unplug != NULL);
+ qdev_hot_removed = true;
+
return dev->info->unplug(dev);
}
+static int qdev_reset_one(DeviceState *dev, void *opaque)
+{
+ if (dev->info->reset) {
+ dev->info->reset(dev);
+ }
+
+ return 0;
+}
+
+BusState *sysbus_get_default(void)
+{
+ if (!main_system_bus) {
+ main_system_bus_create();
+ }
+ return main_system_bus;
+}
+
+static int qbus_reset_one(BusState *bus, void *opaque)
+{
+ if (bus->info->reset) {
+ return bus->info->reset(bus);
+ }
+ return 0;
+}
+
+void qdev_reset_all(DeviceState *dev)
+{
+ qdev_walk_children(dev, qdev_reset_one, qbus_reset_one, NULL);
+}
+
+void qbus_reset_all_fn(void *opaque)
+{
+ BusState *bus = opaque;
+ qbus_walk_children(bus, qdev_reset_one, qbus_reset_one, NULL);
+}
+
/* can be used as ->unplug() callback for the simple cases */
int qdev_simple_unplug_cb(DeviceState *dev)
{
return 0;
}
-/* Like qdev_init(), but terminate program via hw_error() instead of
+
+/* Like qdev_init(), but terminate program via error_report() 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
{
DeviceInfo *info = dev->info;
- if (qdev_init(dev) < 0)
- hw_error("Initialization of device %s failed\n", info->name);
+ if (qdev_init(dev) < 0) {
+ error_report("Initialization of device %s failed", info->name);
+ exit(1);
+ }
}
/* Unlink device from bus and free the structure. */
void qdev_free(DeviceState *dev)
{
BusState *bus;
+ Property *prop;
if (dev->state == DEV_STATE_INITIALIZED) {
while (dev->num_child_bus) {
qbus_free(bus);
}
if (dev->info->vmsd)
- vmstate_unregister(dev->info->vmsd, dev);
+ vmstate_unregister(dev, dev->info->vmsd, dev);
if (dev->info->exit)
dev->info->exit(dev);
if (dev->opts)
qemu_opts_del(dev->opts);
}
- qemu_unregister_reset(qdev_reset, dev);
QLIST_REMOVE(dev, sibling);
+ for (prop = dev->info->props; prop && prop->name; prop++) {
+ if (prop->info->free) {
+ prop->info->free(dev, prop);
+ }
+ }
qemu_free(dev);
}
qdev_hotplug = 1;
}
+bool qdev_machine_modified(void)
+{
+ return qdev_hot_added || qdev_hot_removed;
+}
+
/* Get a character (serial) device interface. */
CharDriverState *qdev_init_chardev(DeviceState *dev)
{
void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd)
{
- qdev_prop_set_macaddr(dev, "mac", nd->macaddr);
+ qdev_prop_set_macaddr(dev, "mac", nd->macaddr.a);
if (nd->vlan)
qdev_prop_set_vlan(dev, "vlan", nd->vlan);
if (nd->netdev)
qdev_prop_exists(dev, "vectors")) {
qdev_prop_set_uint32(dev, "vectors", nd->nvectors);
}
+ nd->instantiated = 1;
}
-static int next_block_unit[IF_COUNT];
+BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
+{
+ BusState *bus;
-/* Get a block device. This should only be used for single-drive devices
- (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
- appropriate bus. */
-BlockDriverState *qdev_init_bdrv(DeviceState *dev, BlockInterfaceType type)
+ QLIST_FOREACH(bus, &dev->child_bus, sibling) {
+ if (strcmp(name, bus->name) == 0) {
+ return bus;
+ }
+ }
+ return NULL;
+}
+
+int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
+ qbus_walkerfn *busfn, void *opaque)
{
- int unit = next_block_unit[type]++;
- DriveInfo *dinfo;
+ DeviceState *dev;
+ int err;
+
+ if (busfn) {
+ err = busfn(bus, opaque);
+ if (err) {
+ return err;
+ }
+ }
+
+ QLIST_FOREACH(dev, &bus->children, sibling) {
+ err = qdev_walk_children(dev, devfn, busfn, opaque);
+ if (err < 0) {
+ return err;
+ }
+ }
- dinfo = drive_get(type, 0, unit);
- return dinfo ? dinfo->bdrv : NULL;
+ return 0;
}
-BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
+int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
+ qbus_walkerfn *busfn, void *opaque)
{
BusState *bus;
+ int err;
+
+ if (devfn) {
+ err = devfn(dev, opaque);
+ if (err) {
+ return err;
+ }
+ }
QLIST_FOREACH(bus, &dev->child_bus, sibling) {
- if (strcmp(name, bus->name) == 0) {
- return bus;
+ err = qbus_walk_children(bus, devfn, busfn, opaque);
+ if (err < 0) {
+ return err;
}
}
- return NULL;
+
+ return 0;
}
static BusState *qbus_find_recursive(BusState *bus, const char *name,
return NULL;
}
-static DeviceState *qdev_find_recursive(BusState *bus, const char *id)
+DeviceState *qdev_find_recursive(BusState *bus, const char *id)
{
DeviceState *dev, *ret;
BusState *child;
return NULL;
}
-static void qbus_list_bus(DeviceState *dev, char *dest, int len)
+static void qbus_list_bus(DeviceState *dev)
{
BusState *child;
const char *sep = " ";
- int pos = 0;
- pos += snprintf(dest+pos, len-pos,"child busses at \"%s\":",
- dev->id ? dev->id : dev->info->name);
+ error_printf("child busses at \"%s\":",
+ dev->id ? dev->id : dev->info->name);
QLIST_FOREACH(child, &dev->child_bus, sibling) {
- pos += snprintf(dest+pos, len-pos, "%s\"%s\"", sep, child->name);
+ error_printf("%s\"%s\"", sep, child->name);
sep = ", ";
}
+ error_printf("\n");
}
-static void qbus_list_dev(BusState *bus, char *dest, int len)
+static void qbus_list_dev(BusState *bus)
{
DeviceState *dev;
const char *sep = " ";
- int pos = 0;
- pos += snprintf(dest+pos, len-pos, "devices at \"%s\":",
- bus->name);
+ error_printf("devices at \"%s\":", bus->name);
QLIST_FOREACH(dev, &bus->children, sibling) {
- pos += snprintf(dest+pos, len-pos, "%s\"%s\"",
- sep, dev->info->name);
+ error_printf("%s\"%s\"", sep, dev->info->name);
if (dev->id)
- pos += snprintf(dest+pos, len-pos, "/\"%s\"", dev->id);
+ error_printf("/\"%s\"", dev->id);
sep = ", ";
}
+ error_printf("\n");
}
static BusState *qbus_find_bus(DeviceState *dev, char *elem)
{
DeviceState *dev;
BusState *bus;
- char elem[128], msg[256];
+ char elem[128];
int pos, len;
/* find start element */
pos = 0;
} else {
if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
- qemu_error("path parse error (\"%s\")\n", path);
- return NULL;
+ assert(!path[0]);
+ elem[0] = len = 0;
}
bus = qbus_find_recursive(main_system_bus, elem, NULL);
if (!bus) {
- qemu_error("bus \"%s\" not found\n", elem);
+ qerror_report(QERR_BUS_NOT_FOUND, elem);
return NULL;
}
pos = len;
}
for (;;) {
+ assert(path[pos] == '/' || !path[pos]);
+ while (path[pos] == '/') {
+ pos++;
+ }
if (path[pos] == '\0') {
- /* we are done */
return bus;
}
/* find device */
- if (sscanf(path+pos, "/%127[^/]%n", elem, &len) != 1) {
- qemu_error("path parse error (\"%s\" pos %d)\n", path, pos);
- return NULL;
+ if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
+ assert(0);
+ elem[0] = len = 0;
}
pos += len;
dev = qbus_find_dev(bus, elem);
if (!dev) {
- qbus_list_dev(bus, msg, sizeof(msg));
- qemu_error("device \"%s\" not found\n%s\n", elem, msg);
+ qerror_report(QERR_DEVICE_NOT_FOUND, elem);
+ if (!monitor_cur_is_qmp()) {
+ qbus_list_dev(bus);
+ }
return NULL;
}
+
+ assert(path[pos] == '/' || !path[pos]);
+ while (path[pos] == '/') {
+ pos++;
+ }
if (path[pos] == '\0') {
/* last specified element is a device. If it has exactly
* one child bus accept it nevertheless */
switch (dev->num_child_bus) {
case 0:
- qemu_error("device has no child bus (%s)\n", path);
+ qerror_report(QERR_DEVICE_NO_BUS, elem);
return NULL;
case 1:
return QLIST_FIRST(&dev->child_bus);
default:
- qbus_list_bus(dev, msg, sizeof(msg));
- qemu_error("device has multiple child busses (%s)\n%s\n",
- path, msg);
+ qerror_report(QERR_DEVICE_MULTIPLE_BUSSES, elem);
+ if (!monitor_cur_is_qmp()) {
+ qbus_list_bus(dev);
+ }
return NULL;
}
}
/* find bus */
- if (sscanf(path+pos, "/%127[^/]%n", elem, &len) != 1) {
- qemu_error("path parse error (\"%s\" pos %d)\n", path, pos);
- return NULL;
+ if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
+ assert(0);
+ elem[0] = len = 0;
}
pos += len;
bus = qbus_find_bus(dev, elem);
if (!bus) {
- qbus_list_bus(dev, msg, sizeof(msg));
- qemu_error("child bus \"%s\" not found\n%s\n", elem, msg);
+ qerror_report(QERR_BUS_NOT_FOUND, elem);
+ if (!monitor_cur_is_qmp()) {
+ qbus_list_bus(dev);
+ }
return NULL;
}
}
if (parent) {
QLIST_INSERT_HEAD(&parent->child_bus, bus, sibling);
parent->num_child_bus++;
+ } else if (bus != main_system_bus) {
+ /* TODO: once all bus devices are qdevified,
+ only reset handler for main_system_bus should be registered here. */
+ qemu_register_reset(qbus_reset_all_fn, bus);
}
-
}
BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name)
return bus;
}
+static void main_system_bus_create(void)
+{
+ /* assign main_system_bus before qbus_create_inplace()
+ * in order to make "if (bus != main_system_bus)" work */
+ main_system_bus = qemu_mallocz(system_bus_info.size);
+ main_system_bus->qdev_allocated = 1;
+ qbus_create_inplace(main_system_bus, &system_bus_info, NULL,
+ "main-system-bus");
+}
+
void qbus_free(BusState *bus)
{
DeviceState *dev;
if (bus->parent) {
QLIST_REMOVE(bus, sibling);
bus->parent->num_child_bus--;
+ } else {
+ assert(bus != main_system_bus); /* main_system_bus is never freed */
+ qemu_unregister_reset(qbus_reset_all_fn, bus);
}
+ qemu_free((void*)bus->name);
if (bus->qdev_allocated) {
qemu_free(bus);
}
if (!props)
return;
while (props->name) {
+ /*
+ * TODO Properties without a print method are just for dirty
+ * hacks. qdev_prop_ptr is the only such PropertyInfo. It's
+ * marked for removal. The test props->info->print should be
+ * removed along with it.
+ */
if (props->info->print) {
props->info->print(dev, props, buf, sizeof(buf));
qdev_printf("%s-prop: %s = %s\n", prefix, props->name, buf);
void do_info_qdm(Monitor *mon)
{
DeviceInfo *info;
- char msg[256];
for (info = device_info_list; info != NULL; info = info->next) {
- qdev_print_devinfo(info, msg, sizeof(msg));
- monitor_printf(mon, "%s\n", msg);
+ qdev_print_devinfo(info);
}
}
-void do_device_add(Monitor *mon, const QDict *qdict)
+int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
{
QemuOpts *opts;
- opts = qemu_opts_parse(&qemu_device_opts,
- qdict_get_str(qdict, "config"), "driver");
- if (opts) {
- if (qdev_device_help(opts) || qdev_device_add(opts) == NULL) {
- qemu_opts_del(opts);
- }
+ opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict);
+ if (!opts) {
+ return -1;
}
+ if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
+ qemu_opts_del(opts);
+ return 0;
+ }
+ if (!qdev_device_add(opts)) {
+ qemu_opts_del(opts);
+ return -1;
+ }
+ return 0;
}
-void do_device_del(Monitor *mon, const QDict *qdict)
+int do_device_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
{
const char *id = qdict_get_str(qdict, "id");
DeviceState *dev;
dev = qdev_find_recursive(main_system_bus, id);
if (NULL == dev) {
- qemu_error("Device '%s' not found\n", id);
- return;
+ qerror_report(QERR_DEVICE_NOT_FOUND, id);
+ return -1;
+ }
+ return qdev_unplug(dev);
+}
+
+static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
+{
+ int l = 0;
+
+ if (dev && dev->parent_bus) {
+ char *d;
+ l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
+ if (dev->parent_bus->info->get_fw_dev_path) {
+ d = dev->parent_bus->info->get_fw_dev_path(dev);
+ l += snprintf(p + l, size - l, "%s", d);
+ qemu_free(d);
+ } else {
+ l += snprintf(p + l, size - l, "%s", dev->info->name);
+ }
}
- qdev_unplug(dev);
+ l += snprintf(p + l , size - l, "/");
+
+ return l;
+}
+
+char* qdev_get_fw_dev_path(DeviceState *dev)
+{
+ char path[128];
+ int l;
+
+ l = qdev_get_fw_dev_path_helper(dev, path, 128);
+
+ path[l-1] = '\0';
+
+ return strdup(path);
}