Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 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 |
Blue Swirl | 8167ee8 | 2009-07-16 20:47:01 +0000 | [diff] [blame] | 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 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 | |
Paul Brook | 9d07d75 | 2009-05-14 22:35:07 +0100 | [diff] [blame] | 28 | #include "net.h" |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 29 | #include "qdev.h" |
| 30 | #include "sysemu.h" |
| 31 | |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame^] | 32 | int qdev_hotplug = 0; |
Alex Williamson | 0ac8ef7 | 2011-01-04 12:37:50 -0700 | [diff] [blame] | 33 | static bool qdev_hot_added = false; |
| 34 | static bool qdev_hot_removed = false; |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 35 | |
Gerd Hoffmann | cdaed7c | 2009-10-06 21:17:52 +0200 | [diff] [blame] | 36 | /* This is a nasty hack to allow passing a NULL bus to qdev_create. */ |
Blue Swirl | b9aaf7f | 2009-06-09 18:38:51 +0000 | [diff] [blame] | 37 | static BusState *main_system_bus; |
Isaku Yamahata | 2da8bb9 | 2011-08-02 10:59:13 +0900 | [diff] [blame] | 38 | static void main_system_bus_create(void); |
Paul Brook | 4d6ae67 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 39 | |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 40 | /* Register a new device type. */ |
Anthony Liguori | 4be9f0d | 2011-12-09 10:51:49 -0600 | [diff] [blame] | 41 | const VMStateDescription *qdev_get_vmsd(DeviceState *dev) |
| 42 | { |
Anthony Liguori | 6e00858 | 2011-12-09 11:06:57 -0600 | [diff] [blame] | 43 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
| 44 | return dc->vmsd; |
Anthony Liguori | 4be9f0d | 2011-12-09 10:51:49 -0600 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | BusInfo *qdev_get_bus_info(DeviceState *dev) |
| 48 | { |
Anthony Liguori | 6e00858 | 2011-12-09 11:06:57 -0600 | [diff] [blame] | 49 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
| 50 | return dc->bus_info; |
Anthony Liguori | 4be9f0d | 2011-12-09 10:51:49 -0600 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | Property *qdev_get_props(DeviceState *dev) |
| 54 | { |
Anthony Liguori | 6e00858 | 2011-12-09 11:06:57 -0600 | [diff] [blame] | 55 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
| 56 | return dc->props; |
Anthony Liguori | 4be9f0d | 2011-12-09 10:51:49 -0600 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | const char *qdev_fw_name(DeviceState *dev) |
| 60 | { |
Anthony Liguori | 6e00858 | 2011-12-09 11:06:57 -0600 | [diff] [blame] | 61 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
Anthony Liguori | 4be9f0d | 2011-12-09 10:51:49 -0600 | [diff] [blame] | 62 | |
Anthony Liguori | 6e00858 | 2011-12-09 11:06:57 -0600 | [diff] [blame] | 63 | if (dc->fw_name) { |
| 64 | return dc->fw_name; |
Anthony Liguori | 4be9f0d | 2011-12-09 10:51:49 -0600 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | return object_get_typename(OBJECT(dev)); |
| 68 | } |
| 69 | |
Blue Swirl | a369da5 | 2011-09-27 19:15:42 +0000 | [diff] [blame] | 70 | bool qdev_exists(const char *name) |
| 71 | { |
Anthony Liguori | 212ad11 | 2012-02-01 09:34:28 -0600 | [diff] [blame] | 72 | return !!object_class_by_name(name); |
Blue Swirl | a369da5 | 2011-09-27 19:15:42 +0000 | [diff] [blame] | 73 | } |
Anthony Liguori | 40021f0 | 2011-12-04 12:22:06 -0600 | [diff] [blame] | 74 | |
Paolo Bonzini | ca2cc78 | 2011-12-18 17:05:11 +0100 | [diff] [blame] | 75 | static void qdev_property_add_legacy(DeviceState *dev, Property *prop, |
| 76 | Error **errp); |
| 77 | |
Anthony Liguori | 9fbe612 | 2011-12-22 15:14:27 -0600 | [diff] [blame] | 78 | void qdev_set_parent_bus(DeviceState *dev, BusState *bus) |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 79 | { |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 80 | Property *prop; |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 81 | |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 82 | if (qdev_hotplug) { |
| 83 | assert(bus->allow_hotplug); |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 84 | } |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 85 | |
Anthony Liguori | 9fbe612 | 2011-12-22 15:14:27 -0600 | [diff] [blame] | 86 | dev->parent_bus = bus; |
Anthony Liguori | 9674bfe | 2011-12-22 15:06:37 -0600 | [diff] [blame] | 87 | QTAILQ_INSERT_HEAD(&bus->children, dev, sibling); |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 88 | |
Anthony Liguori | 9fbe612 | 2011-12-22 15:14:27 -0600 | [diff] [blame] | 89 | qdev_prop_set_defaults(dev, dev->parent_bus->info->props); |
Anthony Liguori | 6e00858 | 2011-12-09 11:06:57 -0600 | [diff] [blame] | 90 | for (prop = qdev_get_bus_info(dev)->props; prop && prop->name; prop++) { |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 91 | qdev_property_add_legacy(dev, prop, NULL); |
Paolo Bonzini | ca2cc78 | 2011-12-18 17:05:11 +0100 | [diff] [blame] | 92 | qdev_property_add_static(dev, prop, NULL); |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 93 | } |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 94 | } |
| 95 | |
Markus Armbruster | 0c17542 | 2010-02-19 19:12:18 +0100 | [diff] [blame] | 96 | /* Create a new device. This only initializes the device state structure |
| 97 | and allows properties to be set. qdev_init should be called to |
| 98 | initialize the actual device emulation. */ |
| 99 | DeviceState *qdev_create(BusState *bus, const char *name) |
| 100 | { |
Blue Swirl | 0bcdeda | 2011-02-05 14:34:25 +0000 | [diff] [blame] | 101 | DeviceState *dev; |
| 102 | |
| 103 | dev = qdev_try_create(bus, name); |
| 104 | if (!dev) { |
Peter Maydell | e92714c | 2011-08-03 23:49:04 +0100 | [diff] [blame] | 105 | if (bus) { |
| 106 | hw_error("Unknown device '%s' for bus '%s'\n", name, |
| 107 | bus->info->name); |
| 108 | } else { |
| 109 | hw_error("Unknown device '%s' for default sysbus\n", name); |
| 110 | } |
Blue Swirl | 0bcdeda | 2011-02-05 14:34:25 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | return dev; |
| 114 | } |
| 115 | |
| 116 | DeviceState *qdev_try_create(BusState *bus, const char *name) |
| 117 | { |
Anthony Liguori | 9fbe612 | 2011-12-22 15:14:27 -0600 | [diff] [blame] | 118 | DeviceState *dev; |
| 119 | |
| 120 | dev = DEVICE(object_new(name)); |
| 121 | if (!dev) { |
| 122 | return NULL; |
| 123 | } |
| 124 | |
Markus Armbruster | 0c17542 | 2010-02-19 19:12:18 +0100 | [diff] [blame] | 125 | if (!bus) { |
Stefan Weil | 6869489 | 2010-12-16 19:33:22 +0100 | [diff] [blame] | 126 | bus = sysbus_get_default(); |
Markus Armbruster | 0c17542 | 2010-02-19 19:12:18 +0100 | [diff] [blame] | 127 | } |
| 128 | |
Anthony Liguori | 9fbe612 | 2011-12-22 15:14:27 -0600 | [diff] [blame] | 129 | qdev_set_parent_bus(dev, bus); |
| 130 | qdev_prop_set_globals(dev); |
| 131 | |
| 132 | return dev; |
Markus Armbruster | 0c17542 | 2010-02-19 19:12:18 +0100 | [diff] [blame] | 133 | } |
| 134 | |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 135 | /* Initialize a device. Device properties should be set before calling |
| 136 | this function. IRQs and MMIO regions should be connected/mapped after |
Markus Armbruster | 18cfeb5 | 2009-10-07 01:15:56 +0200 | [diff] [blame] | 137 | calling this function. |
| 138 | On failure, destroy the device and return negative value. |
| 139 | Return 0 on success. */ |
Gerd Hoffmann | 81a322d | 2009-08-14 10:36:05 +0200 | [diff] [blame] | 140 | int qdev_init(DeviceState *dev) |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 141 | { |
Anthony Liguori | 6e00858 | 2011-12-09 11:06:57 -0600 | [diff] [blame] | 142 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
Gerd Hoffmann | 959f733 | 2009-09-01 09:56:12 +0200 | [diff] [blame] | 143 | int rc; |
| 144 | |
Gerd Hoffmann | 131ec1b | 2009-09-25 21:42:34 +0200 | [diff] [blame] | 145 | assert(dev->state == DEV_STATE_CREATED); |
Anthony Liguori | 6e00858 | 2011-12-09 11:06:57 -0600 | [diff] [blame] | 146 | |
Anthony Liguori | d307af7 | 2011-12-09 15:02:56 -0600 | [diff] [blame] | 147 | rc = dc->init(dev); |
Markus Armbruster | 18cfeb5 | 2009-10-07 01:15:56 +0200 | [diff] [blame] | 148 | if (rc < 0) { |
| 149 | qdev_free(dev); |
Gerd Hoffmann | 959f733 | 2009-09-01 09:56:12 +0200 | [diff] [blame] | 150 | return rc; |
Markus Armbruster | 18cfeb5 | 2009-10-07 01:15:56 +0200 | [diff] [blame] | 151 | } |
Anthony Liguori | 6e00858 | 2011-12-09 11:06:57 -0600 | [diff] [blame] | 152 | if (qdev_get_vmsd(dev)) { |
| 153 | vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev, |
Jan Kiszka | 4d2ffa0 | 2010-05-15 13:32:40 +0200 | [diff] [blame] | 154 | dev->instance_id_alias, |
| 155 | dev->alias_required_for_version); |
| 156 | } |
Gerd Hoffmann | 131ec1b | 2009-09-25 21:42:34 +0200 | [diff] [blame] | 157 | dev->state = DEV_STATE_INITIALIZED; |
Anthony Liguori | 94afdad | 2011-12-04 11:36:01 -0600 | [diff] [blame] | 158 | if (dev->hotplugged) { |
| 159 | device_reset(dev); |
Jan Kiszka | 5ab28c8 | 2011-07-24 19:38:36 +0200 | [diff] [blame] | 160 | } |
Gerd Hoffmann | 959f733 | 2009-09-01 09:56:12 +0200 | [diff] [blame] | 161 | return 0; |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 162 | } |
| 163 | |
Jan Kiszka | 4d2ffa0 | 2010-05-15 13:32:40 +0200 | [diff] [blame] | 164 | void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id, |
| 165 | int required_for_version) |
| 166 | { |
| 167 | assert(dev->state == DEV_STATE_CREATED); |
| 168 | dev->instance_id_alias = alias_id; |
| 169 | dev->alias_required_for_version = required_for_version; |
| 170 | } |
| 171 | |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 172 | int qdev_unplug(DeviceState *dev) |
| 173 | { |
Anthony Liguori | 6e00858 | 2011-12-09 11:06:57 -0600 | [diff] [blame] | 174 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
| 175 | |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 176 | if (!dev->parent_bus->allow_hotplug) { |
Markus Armbruster | cc601cb | 2010-03-22 11:38:13 +0100 | [diff] [blame] | 177 | qerror_report(QERR_BUS_NO_HOTPLUG, dev->parent_bus->name); |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 178 | return -1; |
| 179 | } |
Anthony Liguori | 6e00858 | 2011-12-09 11:06:57 -0600 | [diff] [blame] | 180 | assert(dc->unplug != NULL); |
Amit Shah | 593831d | 2009-11-02 14:56:41 +0530 | [diff] [blame] | 181 | |
Alex Williamson | 0ac8ef7 | 2011-01-04 12:37:50 -0700 | [diff] [blame] | 182 | qdev_hot_removed = true; |
| 183 | |
Anthony Liguori | 6e00858 | 2011-12-09 11:06:57 -0600 | [diff] [blame] | 184 | return dc->unplug(dev); |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 185 | } |
| 186 | |
Anthony Liguori | ec990eb | 2010-11-19 18:55:59 +0900 | [diff] [blame] | 187 | static int qdev_reset_one(DeviceState *dev, void *opaque) |
| 188 | { |
Anthony Liguori | 94afdad | 2011-12-04 11:36:01 -0600 | [diff] [blame] | 189 | device_reset(dev); |
Anthony Liguori | ec990eb | 2010-11-19 18:55:59 +0900 | [diff] [blame] | 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | BusState *sysbus_get_default(void) |
| 195 | { |
Stefan Weil | 6869489 | 2010-12-16 19:33:22 +0100 | [diff] [blame] | 196 | if (!main_system_bus) { |
Isaku Yamahata | 2da8bb9 | 2011-08-02 10:59:13 +0900 | [diff] [blame] | 197 | main_system_bus_create(); |
Stefan Weil | 6869489 | 2010-12-16 19:33:22 +0100 | [diff] [blame] | 198 | } |
Anthony Liguori | ec990eb | 2010-11-19 18:55:59 +0900 | [diff] [blame] | 199 | return main_system_bus; |
| 200 | } |
| 201 | |
Isaku Yamahata | b4694b7 | 2010-11-19 18:56:00 +0900 | [diff] [blame] | 202 | static int qbus_reset_one(BusState *bus, void *opaque) |
| 203 | { |
| 204 | if (bus->info->reset) { |
| 205 | return bus->info->reset(bus); |
| 206 | } |
| 207 | return 0; |
| 208 | } |
| 209 | |
Isaku Yamahata | 5af0a04 | 2010-11-19 18:56:01 +0900 | [diff] [blame] | 210 | void qdev_reset_all(DeviceState *dev) |
| 211 | { |
| 212 | qdev_walk_children(dev, qdev_reset_one, qbus_reset_one, NULL); |
| 213 | } |
| 214 | |
Isaku Yamahata | 80376c3 | 2010-12-20 14:33:35 +0900 | [diff] [blame] | 215 | void qbus_reset_all_fn(void *opaque) |
| 216 | { |
| 217 | BusState *bus = opaque; |
Michael S. Tsirkin | f530cce | 2010-12-20 15:17:10 +0200 | [diff] [blame] | 218 | qbus_walk_children(bus, qdev_reset_one, qbus_reset_one, NULL); |
Isaku Yamahata | 80376c3 | 2010-12-20 14:33:35 +0900 | [diff] [blame] | 219 | } |
| 220 | |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 221 | /* can be used as ->unplug() callback for the simple cases */ |
| 222 | int qdev_simple_unplug_cb(DeviceState *dev) |
| 223 | { |
| 224 | /* just zap it */ |
| 225 | qdev_free(dev); |
| 226 | return 0; |
| 227 | } |
| 228 | |
Michael Tokarev | 3b29a10 | 2011-04-06 17:51:59 +0400 | [diff] [blame] | 229 | |
| 230 | /* Like qdev_init(), but terminate program via error_report() instead of |
Markus Armbruster | e23a1b3 | 2009-10-07 01:15:58 +0200 | [diff] [blame] | 231 | returning an error value. This is okay during machine creation. |
| 232 | Don't use for hotplug, because there callers need to recover from |
| 233 | failure. Exception: if you know the device's init() callback can't |
| 234 | fail, then qdev_init_nofail() can't fail either, and is therefore |
| 235 | usable even then. But relying on the device implementation that |
| 236 | way is somewhat unclean, and best avoided. */ |
| 237 | void qdev_init_nofail(DeviceState *dev) |
| 238 | { |
Markus Armbruster | bd6c9a6 | 2010-05-27 21:23:08 +0200 | [diff] [blame] | 239 | if (qdev_init(dev) < 0) { |
Anthony Liguori | 6e00858 | 2011-12-09 11:06:57 -0600 | [diff] [blame] | 240 | error_report("Initialization of device %s failed", |
| 241 | object_get_typename(OBJECT(dev))); |
Markus Armbruster | bd6c9a6 | 2010-05-27 21:23:08 +0200 | [diff] [blame] | 242 | exit(1); |
| 243 | } |
Markus Armbruster | e23a1b3 | 2009-10-07 01:15:58 +0200 | [diff] [blame] | 244 | } |
| 245 | |
Anthony Liguori | 44677de | 2011-12-12 14:29:26 -0600 | [diff] [blame] | 246 | static void qdev_property_del_all(DeviceState *dev) |
| 247 | { |
| 248 | while (!QTAILQ_EMPTY(&dev->properties)) { |
| 249 | DeviceProperty *prop = QTAILQ_FIRST(&dev->properties); |
| 250 | |
| 251 | QTAILQ_REMOVE(&dev->properties, prop, node); |
| 252 | |
| 253 | if (prop->release) { |
| 254 | prop->release(dev, prop->name, prop->opaque); |
| 255 | } |
| 256 | |
| 257 | g_free(prop->name); |
| 258 | g_free(prop->type); |
| 259 | g_free(prop); |
| 260 | } |
| 261 | } |
| 262 | |
Anthony Liguori | 024a6fb | 2012-01-13 07:45:55 -0600 | [diff] [blame] | 263 | static void qdev_property_del_child(DeviceState *dev, DeviceState *child, Error **errp) |
| 264 | { |
| 265 | DeviceProperty *prop; |
| 266 | |
| 267 | QTAILQ_FOREACH(prop, &dev->properties, node) { |
| 268 | if (strstart(prop->type, "child<", NULL) && prop->opaque == child) { |
| 269 | break; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | g_assert(prop != NULL); |
| 274 | |
| 275 | QTAILQ_REMOVE(&dev->properties, prop, node); |
| 276 | |
| 277 | if (prop->release) { |
| 278 | prop->release(dev, prop->name, prop->opaque); |
| 279 | } |
| 280 | |
| 281 | g_free(prop->name); |
| 282 | g_free(prop->type); |
| 283 | g_free(prop); |
| 284 | } |
| 285 | |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 286 | /* Unlink device from bus and free the structure. */ |
| 287 | void qdev_free(DeviceState *dev) |
| 288 | { |
Gerd Hoffmann | 131ec1b | 2009-09-25 21:42:34 +0200 | [diff] [blame] | 289 | BusState *bus; |
Markus Armbruster | d21357d | 2010-06-01 20:32:31 +0200 | [diff] [blame] | 290 | Property *prop; |
Anthony Liguori | 6e00858 | 2011-12-09 11:06:57 -0600 | [diff] [blame] | 291 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
Gerd Hoffmann | 131ec1b | 2009-09-25 21:42:34 +0200 | [diff] [blame] | 292 | |
Anthony Liguori | 44677de | 2011-12-12 14:29:26 -0600 | [diff] [blame] | 293 | qdev_property_del_all(dev); |
| 294 | |
Gerd Hoffmann | 131ec1b | 2009-09-25 21:42:34 +0200 | [diff] [blame] | 295 | if (dev->state == DEV_STATE_INITIALIZED) { |
| 296 | while (dev->num_child_bus) { |
| 297 | bus = QLIST_FIRST(&dev->child_bus); |
| 298 | qbus_free(bus); |
| 299 | } |
Anthony Liguori | 6e00858 | 2011-12-09 11:06:57 -0600 | [diff] [blame] | 300 | if (qdev_get_vmsd(dev)) { |
| 301 | vmstate_unregister(dev, qdev_get_vmsd(dev), dev); |
| 302 | } |
| 303 | if (dc->exit) { |
| 304 | dc->exit(dev); |
| 305 | } |
| 306 | if (dev->opts) { |
Gerd Hoffmann | ef80b46 | 2009-09-25 21:42:49 +0200 | [diff] [blame] | 307 | qemu_opts_del(dev->opts); |
Anthony Liguori | 6e00858 | 2011-12-09 11:06:57 -0600 | [diff] [blame] | 308 | } |
Gerd Hoffmann | 131ec1b | 2009-09-25 21:42:34 +0200 | [diff] [blame] | 309 | } |
Paolo Bonzini | d8bb00d | 2011-09-14 09:28:06 +0200 | [diff] [blame] | 310 | QTAILQ_REMOVE(&dev->parent_bus->children, dev, sibling); |
Anthony Liguori | 6e00858 | 2011-12-09 11:06:57 -0600 | [diff] [blame] | 311 | for (prop = qdev_get_props(dev); prop && prop->name; prop++) { |
Markus Armbruster | d21357d | 2010-06-01 20:32:31 +0200 | [diff] [blame] | 312 | if (prop->info->free) { |
| 313 | prop->info->free(dev, prop); |
| 314 | } |
| 315 | } |
Anthony Liguori | 024a6fb | 2012-01-13 07:45:55 -0600 | [diff] [blame] | 316 | if (dev->parent) { |
| 317 | qdev_property_del_child(dev->parent, dev, NULL); |
| 318 | } |
| 319 | if (dev->ref != 0) { |
| 320 | qerror_report(QERR_DEVICE_IN_USE, dev->id?:""); |
| 321 | } |
Anthony Liguori | 32fea40 | 2011-12-16 14:34:46 -0600 | [diff] [blame] | 322 | object_delete(OBJECT(dev)); |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 323 | } |
| 324 | |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 325 | void qdev_machine_creation_done(void) |
| 326 | { |
| 327 | /* |
| 328 | * ok, initial machine setup is done, starting from now we can |
| 329 | * only create hotpluggable devices |
| 330 | */ |
| 331 | qdev_hotplug = 1; |
| 332 | } |
| 333 | |
Alex Williamson | 0ac8ef7 | 2011-01-04 12:37:50 -0700 | [diff] [blame] | 334 | bool qdev_machine_modified(void) |
| 335 | { |
| 336 | return qdev_hot_added || qdev_hot_removed; |
| 337 | } |
| 338 | |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 339 | /* Get a character (serial) device interface. */ |
| 340 | CharDriverState *qdev_init_chardev(DeviceState *dev) |
| 341 | { |
| 342 | static int next_serial; |
Amit Shah | 98b1925 | 2010-01-20 00:36:52 +0530 | [diff] [blame] | 343 | |
| 344 | /* FIXME: This function needs to go away: use chardev properties! */ |
| 345 | return serial_hds[next_serial++]; |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 346 | } |
| 347 | |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 348 | BusState *qdev_get_parent_bus(DeviceState *dev) |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 349 | { |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 350 | return dev->parent_bus; |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 351 | } |
| 352 | |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 353 | void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n) |
| 354 | { |
| 355 | assert(dev->num_gpio_in == 0); |
| 356 | dev->num_gpio_in = n; |
| 357 | dev->gpio_in = qemu_allocate_irqs(handler, dev, n); |
| 358 | } |
| 359 | |
| 360 | void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n) |
| 361 | { |
| 362 | assert(dev->num_gpio_out == 0); |
| 363 | dev->num_gpio_out = n; |
| 364 | dev->gpio_out = pins; |
| 365 | } |
| 366 | |
| 367 | qemu_irq qdev_get_gpio_in(DeviceState *dev, int n) |
| 368 | { |
| 369 | assert(n >= 0 && n < dev->num_gpio_in); |
| 370 | return dev->gpio_in[n]; |
| 371 | } |
| 372 | |
| 373 | void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin) |
| 374 | { |
| 375 | assert(n >= 0 && n < dev->num_gpio_out); |
| 376 | dev->gpio_out[n] = pin; |
| 377 | } |
| 378 | |
Gerd Hoffmann | ed16ab5 | 2009-10-21 15:25:26 +0200 | [diff] [blame] | 379 | void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd) |
| 380 | { |
Jan Kiszka | 6eed185 | 2011-07-20 12:20:22 +0200 | [diff] [blame] | 381 | qdev_prop_set_macaddr(dev, "mac", nd->macaddr.a); |
Gerd Hoffmann | ed16ab5 | 2009-10-21 15:25:26 +0200 | [diff] [blame] | 382 | if (nd->vlan) |
| 383 | qdev_prop_set_vlan(dev, "vlan", nd->vlan); |
| 384 | if (nd->netdev) |
| 385 | qdev_prop_set_netdev(dev, "netdev", nd->netdev); |
Amit Shah | 75422b0 | 2010-02-25 17:24:43 +0530 | [diff] [blame] | 386 | if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED && |
Gerd Hoffmann | 97b1562 | 2009-10-21 15:25:35 +0200 | [diff] [blame] | 387 | qdev_prop_exists(dev, "vectors")) { |
| 388 | qdev_prop_set_uint32(dev, "vectors", nd->nvectors); |
| 389 | } |
Peter Maydell | 48e2faf | 2011-05-20 16:50:01 +0100 | [diff] [blame] | 390 | nd->instantiated = 1; |
Gerd Hoffmann | ed16ab5 | 2009-10-21 15:25:26 +0200 | [diff] [blame] | 391 | } |
| 392 | |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 393 | BusState *qdev_get_child_bus(DeviceState *dev, const char *name) |
Paul Brook | 4d6ae67 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 394 | { |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 395 | BusState *bus; |
Paul Brook | 4d6ae67 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 396 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 397 | QLIST_FOREACH(bus, &dev->child_bus, sibling) { |
Paul Brook | 4d6ae67 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 398 | if (strcmp(name, bus->name) == 0) { |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 399 | return bus; |
Paul Brook | 4d6ae67 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | return NULL; |
| 403 | } |
| 404 | |
Anthony Liguori | 81699d8 | 2010-11-19 18:55:58 +0900 | [diff] [blame] | 405 | int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn, |
| 406 | qbus_walkerfn *busfn, void *opaque) |
| 407 | { |
| 408 | DeviceState *dev; |
| 409 | int err; |
| 410 | |
| 411 | if (busfn) { |
| 412 | err = busfn(bus, opaque); |
| 413 | if (err) { |
| 414 | return err; |
| 415 | } |
| 416 | } |
| 417 | |
Paolo Bonzini | d8bb00d | 2011-09-14 09:28:06 +0200 | [diff] [blame] | 418 | QTAILQ_FOREACH(dev, &bus->children, sibling) { |
Anthony Liguori | 81699d8 | 2010-11-19 18:55:58 +0900 | [diff] [blame] | 419 | err = qdev_walk_children(dev, devfn, busfn, opaque); |
| 420 | if (err < 0) { |
| 421 | return err; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn, |
| 429 | qbus_walkerfn *busfn, void *opaque) |
| 430 | { |
| 431 | BusState *bus; |
| 432 | int err; |
| 433 | |
| 434 | if (devfn) { |
| 435 | err = devfn(dev, opaque); |
| 436 | if (err) { |
| 437 | return err; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | QLIST_FOREACH(bus, &dev->child_bus, sibling) { |
| 442 | err = qbus_walk_children(bus, devfn, busfn, opaque); |
| 443 | if (err < 0) { |
| 444 | return err; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | return 0; |
| 449 | } |
| 450 | |
Isaku Yamahata | a2ee6b4 | 2010-12-24 12:14:12 +0900 | [diff] [blame] | 451 | DeviceState *qdev_find_recursive(BusState *bus, const char *id) |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 452 | { |
| 453 | DeviceState *dev, *ret; |
| 454 | BusState *child; |
| 455 | |
Paolo Bonzini | d8bb00d | 2011-09-14 09:28:06 +0200 | [diff] [blame] | 456 | QTAILQ_FOREACH(dev, &bus->children, sibling) { |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 457 | if (dev->id && strcmp(dev->id, id) == 0) |
| 458 | return dev; |
| 459 | QLIST_FOREACH(child, &dev->child_bus, sibling) { |
| 460 | ret = qdev_find_recursive(child, id); |
| 461 | if (ret) { |
| 462 | return ret; |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | return NULL; |
| 467 | } |
| 468 | |
Gerd Hoffmann | cd739fb | 2009-09-16 22:25:27 +0200 | [diff] [blame] | 469 | void qbus_create_inplace(BusState *bus, BusInfo *info, |
| 470 | DeviceState *parent, const char *name) |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 471 | { |
Gerd Hoffmann | d271de9 | 2009-07-15 13:59:24 +0200 | [diff] [blame] | 472 | char *buf; |
| 473 | int i,len; |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 474 | |
Gerd Hoffmann | 10c4c98 | 2009-06-30 14:12:08 +0200 | [diff] [blame] | 475 | bus->info = info; |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 476 | bus->parent = parent; |
Gerd Hoffmann | d271de9 | 2009-07-15 13:59:24 +0200 | [diff] [blame] | 477 | |
| 478 | if (name) { |
| 479 | /* use supplied name */ |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 480 | bus->name = g_strdup(name); |
Gerd Hoffmann | d271de9 | 2009-07-15 13:59:24 +0200 | [diff] [blame] | 481 | } else if (parent && parent->id) { |
| 482 | /* parent device has id -> use it for bus name */ |
| 483 | len = strlen(parent->id) + 16; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 484 | buf = g_malloc(len); |
Gerd Hoffmann | d271de9 | 2009-07-15 13:59:24 +0200 | [diff] [blame] | 485 | snprintf(buf, len, "%s.%d", parent->id, parent->num_child_bus); |
| 486 | bus->name = buf; |
| 487 | } else { |
| 488 | /* no id -> use lowercase bus type for bus name */ |
| 489 | len = strlen(info->name) + 16; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 490 | buf = g_malloc(len); |
Gerd Hoffmann | d271de9 | 2009-07-15 13:59:24 +0200 | [diff] [blame] | 491 | len = snprintf(buf, len, "%s.%d", info->name, |
| 492 | parent ? parent->num_child_bus : 0); |
| 493 | for (i = 0; i < len; i++) |
Christoph Egger | bb87ece | 2009-07-30 15:28:45 +0200 | [diff] [blame] | 494 | buf[i] = qemu_tolower(buf[i]); |
Gerd Hoffmann | d271de9 | 2009-07-15 13:59:24 +0200 | [diff] [blame] | 495 | bus->name = buf; |
| 496 | } |
| 497 | |
Paolo Bonzini | d8bb00d | 2011-09-14 09:28:06 +0200 | [diff] [blame] | 498 | QTAILQ_INIT(&bus->children); |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 499 | if (parent) { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 500 | QLIST_INSERT_HEAD(&parent->child_bus, bus, sibling); |
Gerd Hoffmann | d271de9 | 2009-07-15 13:59:24 +0200 | [diff] [blame] | 501 | parent->num_child_bus++; |
Isaku Yamahata | 80376c3 | 2010-12-20 14:33:35 +0900 | [diff] [blame] | 502 | } else if (bus != main_system_bus) { |
| 503 | /* TODO: once all bus devices are qdevified, |
| 504 | only reset handler for main_system_bus should be registered here. */ |
| 505 | qemu_register_reset(qbus_reset_all_fn, bus); |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 506 | } |
Gerd Hoffmann | cd739fb | 2009-09-16 22:25:27 +0200 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name) |
| 510 | { |
| 511 | BusState *bus; |
| 512 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 513 | bus = g_malloc0(info->size); |
Gerd Hoffmann | cd739fb | 2009-09-16 22:25:27 +0200 | [diff] [blame] | 514 | bus->qdev_allocated = 1; |
| 515 | qbus_create_inplace(bus, info, parent, name); |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 516 | return bus; |
| 517 | } |
Gerd Hoffmann | cae4956 | 2009-06-05 15:53:17 +0100 | [diff] [blame] | 518 | |
Isaku Yamahata | 2da8bb9 | 2011-08-02 10:59:13 +0900 | [diff] [blame] | 519 | static void main_system_bus_create(void) |
| 520 | { |
| 521 | /* assign main_system_bus before qbus_create_inplace() |
| 522 | * in order to make "if (bus != main_system_bus)" work */ |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 523 | main_system_bus = g_malloc0(system_bus_info.size); |
Isaku Yamahata | 2da8bb9 | 2011-08-02 10:59:13 +0900 | [diff] [blame] | 524 | main_system_bus->qdev_allocated = 1; |
| 525 | qbus_create_inplace(main_system_bus, &system_bus_info, NULL, |
| 526 | "main-system-bus"); |
| 527 | } |
| 528 | |
Gerd Hoffmann | 131ec1b | 2009-09-25 21:42:34 +0200 | [diff] [blame] | 529 | void qbus_free(BusState *bus) |
| 530 | { |
| 531 | DeviceState *dev; |
| 532 | |
Paolo Bonzini | d8bb00d | 2011-09-14 09:28:06 +0200 | [diff] [blame] | 533 | while ((dev = QTAILQ_FIRST(&bus->children)) != NULL) { |
Gerd Hoffmann | 131ec1b | 2009-09-25 21:42:34 +0200 | [diff] [blame] | 534 | qdev_free(dev); |
| 535 | } |
| 536 | if (bus->parent) { |
| 537 | QLIST_REMOVE(bus, sibling); |
| 538 | bus->parent->num_child_bus--; |
Isaku Yamahata | 80376c3 | 2010-12-20 14:33:35 +0900 | [diff] [blame] | 539 | } else { |
| 540 | assert(bus != main_system_bus); /* main_system_bus is never freed */ |
| 541 | qemu_unregister_reset(qbus_reset_all_fn, bus); |
Gerd Hoffmann | 131ec1b | 2009-09-25 21:42:34 +0200 | [diff] [blame] | 542 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 543 | g_free((void*)bus->name); |
Gerd Hoffmann | 131ec1b | 2009-09-25 21:42:34 +0200 | [diff] [blame] | 544 | if (bus->qdev_allocated) { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 545 | g_free(bus); |
Gerd Hoffmann | 131ec1b | 2009-09-25 21:42:34 +0200 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
Gleb Natapov | 1ca4d09 | 2010-12-08 13:35:05 +0200 | [diff] [blame] | 549 | static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size) |
| 550 | { |
| 551 | int l = 0; |
| 552 | |
| 553 | if (dev && dev->parent_bus) { |
| 554 | char *d; |
| 555 | l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size); |
| 556 | if (dev->parent_bus->info->get_fw_dev_path) { |
| 557 | d = dev->parent_bus->info->get_fw_dev_path(dev); |
| 558 | l += snprintf(p + l, size - l, "%s", d); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 559 | g_free(d); |
Gleb Natapov | 1ca4d09 | 2010-12-08 13:35:05 +0200 | [diff] [blame] | 560 | } else { |
Anthony Liguori | f79f2bf | 2011-12-04 11:17:51 -0600 | [diff] [blame] | 561 | l += snprintf(p + l, size - l, "%s", object_get_typename(OBJECT(dev))); |
Gleb Natapov | 1ca4d09 | 2010-12-08 13:35:05 +0200 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | l += snprintf(p + l , size - l, "/"); |
| 565 | |
| 566 | return l; |
| 567 | } |
| 568 | |
| 569 | char* qdev_get_fw_dev_path(DeviceState *dev) |
| 570 | { |
| 571 | char path[128]; |
| 572 | int l; |
| 573 | |
| 574 | l = qdev_get_fw_dev_path_helper(dev, path, 128); |
| 575 | |
| 576 | path[l-1] = '\0'; |
| 577 | |
| 578 | return strdup(path); |
| 579 | } |
Anthony Liguori | 85ed303 | 2011-12-12 14:29:25 -0600 | [diff] [blame] | 580 | |
Anthony Liguori | cd34d66 | 2011-12-12 14:29:43 -0600 | [diff] [blame] | 581 | char *qdev_get_type(DeviceState *dev, Error **errp) |
| 582 | { |
Anthony Liguori | f79f2bf | 2011-12-04 11:17:51 -0600 | [diff] [blame] | 583 | return g_strdup(object_get_typename(OBJECT(dev))); |
Anthony Liguori | cd34d66 | 2011-12-12 14:29:43 -0600 | [diff] [blame] | 584 | } |
| 585 | |
Anthony Liguori | 85ed303 | 2011-12-12 14:29:25 -0600 | [diff] [blame] | 586 | void qdev_ref(DeviceState *dev) |
| 587 | { |
| 588 | dev->ref++; |
| 589 | } |
| 590 | |
| 591 | void qdev_unref(DeviceState *dev) |
| 592 | { |
| 593 | g_assert(dev->ref > 0); |
| 594 | dev->ref--; |
| 595 | } |
Anthony Liguori | 44677de | 2011-12-12 14:29:26 -0600 | [diff] [blame] | 596 | |
| 597 | void qdev_property_add(DeviceState *dev, const char *name, const char *type, |
| 598 | DevicePropertyAccessor *get, DevicePropertyAccessor *set, |
| 599 | DevicePropertyRelease *release, |
| 600 | void *opaque, Error **errp) |
| 601 | { |
| 602 | DeviceProperty *prop = g_malloc0(sizeof(*prop)); |
| 603 | |
| 604 | prop->name = g_strdup(name); |
| 605 | prop->type = g_strdup(type); |
| 606 | |
| 607 | prop->get = get; |
| 608 | prop->set = set; |
| 609 | prop->release = release; |
| 610 | prop->opaque = opaque; |
| 611 | |
| 612 | QTAILQ_INSERT_TAIL(&dev->properties, prop, node); |
| 613 | } |
| 614 | |
| 615 | static DeviceProperty *qdev_property_find(DeviceState *dev, const char *name) |
| 616 | { |
| 617 | DeviceProperty *prop; |
| 618 | |
| 619 | QTAILQ_FOREACH(prop, &dev->properties, node) { |
| 620 | if (strcmp(prop->name, name) == 0) { |
| 621 | return prop; |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | return NULL; |
| 626 | } |
| 627 | |
| 628 | void qdev_property_get(DeviceState *dev, Visitor *v, const char *name, |
| 629 | Error **errp) |
| 630 | { |
| 631 | DeviceProperty *prop = qdev_property_find(dev, name); |
| 632 | |
| 633 | if (prop == NULL) { |
| 634 | error_set(errp, QERR_PROPERTY_NOT_FOUND, dev->id?:"", name); |
| 635 | return; |
| 636 | } |
| 637 | |
| 638 | if (!prop->get) { |
| 639 | error_set(errp, QERR_PERMISSION_DENIED); |
| 640 | } else { |
| 641 | prop->get(dev, v, prop->opaque, name, errp); |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | void qdev_property_set(DeviceState *dev, Visitor *v, const char *name, |
| 646 | Error **errp) |
| 647 | { |
| 648 | DeviceProperty *prop = qdev_property_find(dev, name); |
| 649 | |
| 650 | if (prop == NULL) { |
| 651 | error_set(errp, QERR_PROPERTY_NOT_FOUND, dev->id?:"", name); |
| 652 | return; |
| 653 | } |
| 654 | |
| 655 | if (!prop->set) { |
| 656 | error_set(errp, QERR_PERMISSION_DENIED); |
| 657 | } else { |
Paolo Bonzini | 0d41d9a | 2011-12-18 17:05:05 +0100 | [diff] [blame] | 658 | prop->set(dev, v, prop->opaque, name, errp); |
Anthony Liguori | 44677de | 2011-12-12 14:29:26 -0600 | [diff] [blame] | 659 | } |
| 660 | } |
| 661 | |
| 662 | const char *qdev_property_get_type(DeviceState *dev, const char *name, Error **errp) |
| 663 | { |
| 664 | DeviceProperty *prop = qdev_property_find(dev, name); |
| 665 | |
| 666 | if (prop == NULL) { |
| 667 | error_set(errp, QERR_PROPERTY_NOT_FOUND, dev->id?:"", name); |
| 668 | return NULL; |
| 669 | } |
| 670 | |
| 671 | return prop->type; |
| 672 | } |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 673 | |
| 674 | /** |
| 675 | * Legacy property handling |
| 676 | */ |
| 677 | |
| 678 | static void qdev_get_legacy_property(DeviceState *dev, Visitor *v, void *opaque, |
| 679 | const char *name, Error **errp) |
| 680 | { |
| 681 | Property *prop = opaque; |
| 682 | |
Paolo Bonzini | e3cb6ba | 2011-12-18 17:05:06 +0100 | [diff] [blame] | 683 | char buffer[1024]; |
| 684 | char *ptr = buffer; |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 685 | |
Paolo Bonzini | e3cb6ba | 2011-12-18 17:05:06 +0100 | [diff] [blame] | 686 | prop->info->print(dev, prop, buffer, sizeof(buffer)); |
| 687 | visit_type_str(v, &ptr, name, errp); |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | static void qdev_set_legacy_property(DeviceState *dev, Visitor *v, void *opaque, |
| 691 | const char *name, Error **errp) |
| 692 | { |
| 693 | Property *prop = opaque; |
Paolo Bonzini | e3cb6ba | 2011-12-18 17:05:06 +0100 | [diff] [blame] | 694 | Error *local_err = NULL; |
| 695 | char *ptr = NULL; |
| 696 | int ret; |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 697 | |
| 698 | if (dev->state != DEV_STATE_CREATED) { |
| 699 | error_set(errp, QERR_PERMISSION_DENIED); |
| 700 | return; |
| 701 | } |
| 702 | |
Paolo Bonzini | e3cb6ba | 2011-12-18 17:05:06 +0100 | [diff] [blame] | 703 | visit_type_str(v, &ptr, name, &local_err); |
| 704 | if (local_err) { |
| 705 | error_propagate(errp, local_err); |
| 706 | return; |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 707 | } |
Paolo Bonzini | e3cb6ba | 2011-12-18 17:05:06 +0100 | [diff] [blame] | 708 | |
| 709 | ret = prop->info->parse(dev, prop, ptr); |
Paolo Bonzini | 7db4c4e | 2011-12-18 17:05:07 +0100 | [diff] [blame] | 710 | error_set_from_qdev_prop_error(errp, ret, dev, prop, ptr); |
Paolo Bonzini | e3cb6ba | 2011-12-18 17:05:06 +0100 | [diff] [blame] | 711 | g_free(ptr); |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | /** |
| 715 | * @qdev_add_legacy_property - adds a legacy property |
| 716 | * |
| 717 | * Do not use this is new code! Properties added through this interface will |
Paolo Bonzini | ca2cc78 | 2011-12-18 17:05:11 +0100 | [diff] [blame] | 718 | * be given names and types in the "legacy" namespace. |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 719 | * |
| 720 | * Legacy properties are always processed as strings. The format of the string |
| 721 | * depends on the property type. |
| 722 | */ |
| 723 | void qdev_property_add_legacy(DeviceState *dev, Property *prop, |
| 724 | Error **errp) |
| 725 | { |
Paolo Bonzini | ca2cc78 | 2011-12-18 17:05:11 +0100 | [diff] [blame] | 726 | gchar *name, *type; |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 727 | |
Paolo Bonzini | ca2cc78 | 2011-12-18 17:05:11 +0100 | [diff] [blame] | 728 | name = g_strdup_printf("legacy-%s", prop->name); |
Paolo Bonzini | cafe5bd | 2011-12-18 17:05:10 +0100 | [diff] [blame] | 729 | type = g_strdup_printf("legacy<%s>", |
| 730 | prop->info->legacy_name ?: prop->info->name); |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 731 | |
Paolo Bonzini | ca2cc78 | 2011-12-18 17:05:11 +0100 | [diff] [blame] | 732 | qdev_property_add(dev, name, type, |
Paolo Bonzini | e3cb6ba | 2011-12-18 17:05:06 +0100 | [diff] [blame] | 733 | prop->info->print ? qdev_get_legacy_property : NULL, |
| 734 | prop->info->parse ? qdev_set_legacy_property : NULL, |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 735 | NULL, |
| 736 | prop, errp); |
| 737 | |
| 738 | g_free(type); |
Paolo Bonzini | ca2cc78 | 2011-12-18 17:05:11 +0100 | [diff] [blame] | 739 | g_free(name); |
| 740 | } |
| 741 | |
| 742 | /** |
| 743 | * @qdev_property_add_static - add a @Property to a device. |
| 744 | * |
| 745 | * Static properties access data in a struct. The actual type of the |
| 746 | * property and the field depends on the property type. |
| 747 | */ |
| 748 | void qdev_property_add_static(DeviceState *dev, Property *prop, |
| 749 | Error **errp) |
| 750 | { |
| 751 | qdev_property_add(dev, prop->name, prop->info->name, |
| 752 | prop->info->get, prop->info->set, |
| 753 | NULL, |
| 754 | prop, errp); |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 755 | } |
Anthony Liguori | a10f07a | 2011-12-12 14:29:28 -0600 | [diff] [blame] | 756 | |
| 757 | DeviceState *qdev_get_root(void) |
| 758 | { |
| 759 | static DeviceState *qdev_root; |
| 760 | |
| 761 | if (!qdev_root) { |
| 762 | qdev_root = qdev_create(NULL, "container"); |
| 763 | qdev_init_nofail(qdev_root); |
| 764 | } |
| 765 | |
| 766 | return qdev_root; |
| 767 | } |
Anthony Liguori | f9fbd2f | 2011-12-12 14:29:29 -0600 | [diff] [blame] | 768 | |
Anthony Liguori | 3de1c3e | 2011-12-12 14:29:31 -0600 | [diff] [blame] | 769 | static void qdev_get_child_property(DeviceState *dev, Visitor *v, void *opaque, |
| 770 | const char *name, Error **errp) |
| 771 | { |
| 772 | DeviceState *child = opaque; |
| 773 | gchar *path; |
| 774 | |
| 775 | path = qdev_get_canonical_path(child); |
| 776 | visit_type_str(v, &path, name, errp); |
| 777 | g_free(path); |
| 778 | } |
| 779 | |
Anthony Liguori | 024a6fb | 2012-01-13 07:45:55 -0600 | [diff] [blame] | 780 | static void qdev_release_child_property(DeviceState *dev, const char *name, |
| 781 | void *opaque) |
| 782 | { |
| 783 | DeviceState *child = opaque; |
| 784 | |
| 785 | qdev_unref(child); |
| 786 | } |
| 787 | |
Anthony Liguori | 3de1c3e | 2011-12-12 14:29:31 -0600 | [diff] [blame] | 788 | void qdev_property_add_child(DeviceState *dev, const char *name, |
| 789 | DeviceState *child, Error **errp) |
| 790 | { |
| 791 | gchar *type; |
| 792 | |
Anthony Liguori | f79f2bf | 2011-12-04 11:17:51 -0600 | [diff] [blame] | 793 | type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child))); |
Anthony Liguori | 3de1c3e | 2011-12-12 14:29:31 -0600 | [diff] [blame] | 794 | |
| 795 | qdev_property_add(dev, name, type, qdev_get_child_property, |
Anthony Liguori | 024a6fb | 2012-01-13 07:45:55 -0600 | [diff] [blame] | 796 | NULL, qdev_release_child_property, |
| 797 | child, errp); |
Anthony Liguori | 3de1c3e | 2011-12-12 14:29:31 -0600 | [diff] [blame] | 798 | |
| 799 | qdev_ref(child); |
Anthony Liguori | b2b6c39 | 2011-12-12 14:29:40 -0600 | [diff] [blame] | 800 | g_assert(child->parent == NULL); |
| 801 | child->parent = dev; |
Anthony Liguori | 3de1c3e | 2011-12-12 14:29:31 -0600 | [diff] [blame] | 802 | |
| 803 | g_free(type); |
| 804 | } |
| 805 | |
Anthony Liguori | 83e94fb | 2011-12-12 14:29:32 -0600 | [diff] [blame] | 806 | static void qdev_get_link_property(DeviceState *dev, Visitor *v, void *opaque, |
| 807 | const char *name, Error **errp) |
| 808 | { |
| 809 | DeviceState **child = opaque; |
| 810 | gchar *path; |
| 811 | |
| 812 | if (*child) { |
| 813 | path = qdev_get_canonical_path(*child); |
| 814 | visit_type_str(v, &path, name, errp); |
| 815 | g_free(path); |
| 816 | } else { |
| 817 | path = (gchar *)""; |
| 818 | visit_type_str(v, &path, name, errp); |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | static void qdev_set_link_property(DeviceState *dev, Visitor *v, void *opaque, |
| 823 | const char *name, Error **errp) |
| 824 | { |
| 825 | DeviceState **child = opaque; |
| 826 | bool ambiguous = false; |
| 827 | const char *type; |
| 828 | char *path; |
| 829 | |
| 830 | type = qdev_property_get_type(dev, name, NULL); |
| 831 | |
| 832 | visit_type_str(v, &path, name, errp); |
| 833 | |
| 834 | if (*child) { |
| 835 | qdev_unref(*child); |
| 836 | } |
| 837 | |
| 838 | if (strcmp(path, "") != 0) { |
| 839 | DeviceState *target; |
| 840 | |
| 841 | target = qdev_resolve_path(path, &ambiguous); |
| 842 | if (target) { |
| 843 | gchar *target_type; |
| 844 | |
Anthony Liguori | f79f2bf | 2011-12-04 11:17:51 -0600 | [diff] [blame] | 845 | target_type = g_strdup_printf("link<%s>", object_get_typename(OBJECT(target))); |
Anthony Liguori | 83e94fb | 2011-12-12 14:29:32 -0600 | [diff] [blame] | 846 | if (strcmp(target_type, type) == 0) { |
| 847 | *child = target; |
| 848 | qdev_ref(target); |
| 849 | } else { |
| 850 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, type); |
| 851 | } |
| 852 | |
| 853 | g_free(target_type); |
| 854 | } else { |
| 855 | error_set(errp, QERR_DEVICE_NOT_FOUND, path); |
| 856 | } |
| 857 | } else { |
| 858 | *child = NULL; |
| 859 | } |
| 860 | |
| 861 | g_free(path); |
| 862 | } |
| 863 | |
| 864 | void qdev_property_add_link(DeviceState *dev, const char *name, |
| 865 | const char *type, DeviceState **child, |
| 866 | Error **errp) |
| 867 | { |
| 868 | gchar *full_type; |
| 869 | |
| 870 | full_type = g_strdup_printf("link<%s>", type); |
| 871 | |
| 872 | qdev_property_add(dev, name, full_type, |
| 873 | qdev_get_link_property, |
| 874 | qdev_set_link_property, |
| 875 | NULL, child, errp); |
| 876 | |
| 877 | g_free(full_type); |
| 878 | } |
| 879 | |
Anthony Liguori | f9fbd2f | 2011-12-12 14:29:29 -0600 | [diff] [blame] | 880 | gchar *qdev_get_canonical_path(DeviceState *dev) |
| 881 | { |
Anthony Liguori | b2b6c39 | 2011-12-12 14:29:40 -0600 | [diff] [blame] | 882 | DeviceState *root = qdev_get_root(); |
| 883 | char *newpath = NULL, *path = NULL; |
Anthony Liguori | f9fbd2f | 2011-12-12 14:29:29 -0600 | [diff] [blame] | 884 | |
Anthony Liguori | b2b6c39 | 2011-12-12 14:29:40 -0600 | [diff] [blame] | 885 | while (dev != root) { |
| 886 | DeviceProperty *prop = NULL; |
| 887 | |
| 888 | g_assert(dev->parent != NULL); |
| 889 | |
| 890 | QTAILQ_FOREACH(prop, &dev->parent->properties, node) { |
| 891 | if (!strstart(prop->type, "child<", NULL)) { |
| 892 | continue; |
| 893 | } |
| 894 | |
| 895 | if (prop->opaque == dev) { |
| 896 | if (path) { |
| 897 | newpath = g_strdup_printf("%s/%s", prop->name, path); |
| 898 | g_free(path); |
| 899 | path = newpath; |
| 900 | } else { |
| 901 | path = g_strdup(prop->name); |
| 902 | } |
| 903 | break; |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | g_assert(prop != NULL); |
| 908 | |
| 909 | dev = dev->parent; |
| 910 | } |
Anthony Liguori | f9fbd2f | 2011-12-12 14:29:29 -0600 | [diff] [blame] | 911 | |
| 912 | newpath = g_strdup_printf("/%s", path); |
| 913 | g_free(path); |
| 914 | |
| 915 | return newpath; |
| 916 | } |
Anthony Liguori | dc45c21 | 2011-12-12 14:29:30 -0600 | [diff] [blame] | 917 | |
| 918 | static DeviceState *qdev_resolve_abs_path(DeviceState *parent, |
| 919 | gchar **parts, |
| 920 | int index) |
| 921 | { |
| 922 | DeviceProperty *prop; |
| 923 | DeviceState *child; |
| 924 | |
| 925 | if (parts[index] == NULL) { |
| 926 | return parent; |
| 927 | } |
| 928 | |
| 929 | if (strcmp(parts[index], "") == 0) { |
| 930 | return qdev_resolve_abs_path(parent, parts, index + 1); |
| 931 | } |
| 932 | |
| 933 | prop = qdev_property_find(parent, parts[index]); |
| 934 | if (prop == NULL) { |
| 935 | return NULL; |
| 936 | } |
| 937 | |
| 938 | child = NULL; |
| 939 | if (strstart(prop->type, "link<", NULL)) { |
| 940 | DeviceState **pchild = prop->opaque; |
| 941 | if (*pchild) { |
| 942 | child = *pchild; |
| 943 | } |
| 944 | } else if (strstart(prop->type, "child<", NULL)) { |
| 945 | child = prop->opaque; |
| 946 | } |
| 947 | |
| 948 | if (!child) { |
| 949 | return NULL; |
| 950 | } |
| 951 | |
| 952 | return qdev_resolve_abs_path(child, parts, index + 1); |
| 953 | } |
| 954 | |
| 955 | static DeviceState *qdev_resolve_partial_path(DeviceState *parent, |
| 956 | gchar **parts, |
| 957 | bool *ambiguous) |
| 958 | { |
| 959 | DeviceState *dev; |
| 960 | DeviceProperty *prop; |
| 961 | |
| 962 | dev = qdev_resolve_abs_path(parent, parts, 0); |
| 963 | |
| 964 | QTAILQ_FOREACH(prop, &parent->properties, node) { |
| 965 | DeviceState *found; |
| 966 | |
| 967 | if (!strstart(prop->type, "child<", NULL)) { |
| 968 | continue; |
| 969 | } |
| 970 | |
| 971 | found = qdev_resolve_partial_path(prop->opaque, parts, ambiguous); |
| 972 | if (found) { |
| 973 | if (dev) { |
| 974 | if (ambiguous) { |
| 975 | *ambiguous = true; |
| 976 | } |
| 977 | return NULL; |
| 978 | } |
| 979 | dev = found; |
| 980 | } |
| 981 | |
| 982 | if (ambiguous && *ambiguous) { |
| 983 | return NULL; |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | return dev; |
| 988 | } |
| 989 | |
| 990 | DeviceState *qdev_resolve_path(const char *path, bool *ambiguous) |
| 991 | { |
| 992 | bool partial_path = true; |
| 993 | DeviceState *dev; |
| 994 | gchar **parts; |
| 995 | |
| 996 | parts = g_strsplit(path, "/", 0); |
| 997 | if (parts == NULL || parts[0] == NULL) { |
| 998 | g_strfreev(parts); |
| 999 | return qdev_get_root(); |
| 1000 | } |
| 1001 | |
| 1002 | if (strcmp(parts[0], "") == 0) { |
| 1003 | partial_path = false; |
| 1004 | } |
| 1005 | |
| 1006 | if (partial_path) { |
| 1007 | if (ambiguous) { |
| 1008 | *ambiguous = false; |
| 1009 | } |
| 1010 | dev = qdev_resolve_partial_path(qdev_get_root(), parts, ambiguous); |
| 1011 | } else { |
| 1012 | dev = qdev_resolve_abs_path(qdev_get_root(), parts, 1); |
| 1013 | } |
| 1014 | |
| 1015 | g_strfreev(parts); |
| 1016 | |
| 1017 | return dev; |
| 1018 | } |
| 1019 | |
Anthony Liguori | 6a146eb | 2011-12-12 14:29:42 -0600 | [diff] [blame] | 1020 | typedef struct StringProperty |
| 1021 | { |
| 1022 | char *(*get)(DeviceState *, Error **); |
| 1023 | void (*set)(DeviceState *, const char *, Error **); |
| 1024 | } StringProperty; |
| 1025 | |
| 1026 | static void qdev_property_get_str(DeviceState *dev, Visitor *v, void *opaque, |
| 1027 | const char *name, Error **errp) |
| 1028 | { |
| 1029 | StringProperty *prop = opaque; |
| 1030 | char *value; |
| 1031 | |
| 1032 | value = prop->get(dev, errp); |
| 1033 | if (value) { |
| 1034 | visit_type_str(v, &value, name, errp); |
| 1035 | g_free(value); |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | static void qdev_property_set_str(DeviceState *dev, Visitor *v, void *opaque, |
| 1040 | const char *name, Error **errp) |
| 1041 | { |
| 1042 | StringProperty *prop = opaque; |
| 1043 | char *value; |
| 1044 | Error *local_err = NULL; |
| 1045 | |
| 1046 | visit_type_str(v, &value, name, &local_err); |
| 1047 | if (local_err) { |
| 1048 | error_propagate(errp, local_err); |
| 1049 | return; |
| 1050 | } |
| 1051 | |
| 1052 | prop->set(dev, value, errp); |
| 1053 | g_free(value); |
| 1054 | } |
| 1055 | |
| 1056 | static void qdev_property_release_str(DeviceState *dev, const char *name, |
| 1057 | void *opaque) |
| 1058 | { |
| 1059 | StringProperty *prop = opaque; |
| 1060 | g_free(prop); |
| 1061 | } |
| 1062 | |
| 1063 | void qdev_property_add_str(DeviceState *dev, const char *name, |
| 1064 | char *(*get)(DeviceState *, Error **), |
| 1065 | void (*set)(DeviceState *, const char *, Error **), |
| 1066 | Error **errp) |
| 1067 | { |
| 1068 | StringProperty *prop = g_malloc0(sizeof(*prop)); |
| 1069 | |
| 1070 | prop->get = get; |
| 1071 | prop->set = set; |
| 1072 | |
| 1073 | qdev_property_add(dev, name, "string", |
| 1074 | get ? qdev_property_get_str : NULL, |
| 1075 | set ? qdev_property_set_str : NULL, |
| 1076 | qdev_property_release_str, |
| 1077 | prop, errp); |
| 1078 | } |
Anthony Liguori | 1de81d2 | 2011-12-19 16:37:46 -0600 | [diff] [blame] | 1079 | |
Anthony Liguori | 9674bfe | 2011-12-22 15:06:37 -0600 | [diff] [blame] | 1080 | static void device_initfn(Object *obj) |
| 1081 | { |
| 1082 | DeviceState *dev = DEVICE(obj); |
| 1083 | Property *prop; |
| 1084 | |
| 1085 | if (qdev_hotplug) { |
| 1086 | dev->hotplugged = 1; |
| 1087 | qdev_hot_added = true; |
| 1088 | } |
| 1089 | |
| 1090 | dev->instance_id_alias = -1; |
| 1091 | QTAILQ_INIT(&dev->properties); |
| 1092 | dev->state = DEV_STATE_CREATED; |
| 1093 | |
| 1094 | qdev_prop_set_defaults(dev, qdev_get_props(dev)); |
| 1095 | for (prop = qdev_get_props(dev); prop && prop->name; prop++) { |
| 1096 | qdev_property_add_legacy(dev, prop, NULL); |
| 1097 | qdev_property_add_static(dev, prop, NULL); |
| 1098 | } |
| 1099 | |
| 1100 | qdev_property_add_str(dev, "type", qdev_get_type, NULL, NULL); |
| 1101 | } |
| 1102 | |
Anthony Liguori | 94afdad | 2011-12-04 11:36:01 -0600 | [diff] [blame] | 1103 | void device_reset(DeviceState *dev) |
| 1104 | { |
| 1105 | DeviceClass *klass = DEVICE_GET_CLASS(dev); |
| 1106 | |
| 1107 | if (klass->reset) { |
| 1108 | klass->reset(dev); |
| 1109 | } |
| 1110 | } |
| 1111 | |
Anthony Liguori | 32fea40 | 2011-12-16 14:34:46 -0600 | [diff] [blame] | 1112 | static TypeInfo device_type_info = { |
| 1113 | .name = TYPE_DEVICE, |
| 1114 | .parent = TYPE_OBJECT, |
| 1115 | .instance_size = sizeof(DeviceState), |
Anthony Liguori | 9674bfe | 2011-12-22 15:06:37 -0600 | [diff] [blame] | 1116 | .instance_init = device_initfn, |
Anthony Liguori | 32fea40 | 2011-12-16 14:34:46 -0600 | [diff] [blame] | 1117 | .abstract = true, |
| 1118 | .class_size = sizeof(DeviceClass), |
| 1119 | }; |
| 1120 | |
| 1121 | static void init_qdev(void) |
| 1122 | { |
| 1123 | type_register_static(&device_type_info); |
| 1124 | } |
| 1125 | |
| 1126 | device_init(init_qdev); |