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 | 6e00858 | 2011-12-09 11:06:57 -0600 | [diff] [blame] | 89 | for (prop = qdev_get_bus_info(dev)->props; prop && prop->name; prop++) { |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 90 | qdev_property_add_legacy(dev, prop, NULL); |
Paolo Bonzini | ca2cc78 | 2011-12-18 17:05:11 +0100 | [diff] [blame] | 91 | qdev_property_add_static(dev, prop, NULL); |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 92 | } |
Paolo Bonzini | 4f2d3d7 | 2012-02-02 09:43:02 +0100 | [diff] [blame] | 93 | qdev_prop_set_defaults(dev, dev->parent_bus->info->props); |
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 | |
Andreas Färber | 4ed658c | 2012-02-17 02:47:44 +0100 | [diff] [blame] | 120 | if (object_class_by_name(name) == NULL) { |
| 121 | return NULL; |
| 122 | } |
Anthony Liguori | 9fbe612 | 2011-12-22 15:14:27 -0600 | [diff] [blame] | 123 | dev = DEVICE(object_new(name)); |
| 124 | if (!dev) { |
| 125 | return NULL; |
| 126 | } |
| 127 | |
Markus Armbruster | 0c17542 | 2010-02-19 19:12:18 +0100 | [diff] [blame] | 128 | if (!bus) { |
Stefan Weil | 6869489 | 2010-12-16 19:33:22 +0100 | [diff] [blame] | 129 | bus = sysbus_get_default(); |
Markus Armbruster | 0c17542 | 2010-02-19 19:12:18 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Anthony Liguori | 9fbe612 | 2011-12-22 15:14:27 -0600 | [diff] [blame] | 132 | qdev_set_parent_bus(dev, bus); |
| 133 | qdev_prop_set_globals(dev); |
| 134 | |
| 135 | return dev; |
Markus Armbruster | 0c17542 | 2010-02-19 19:12:18 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 138 | /* Initialize a device. Device properties should be set before calling |
| 139 | this function. IRQs and MMIO regions should be connected/mapped after |
Markus Armbruster | 18cfeb5 | 2009-10-07 01:15:56 +0200 | [diff] [blame] | 140 | calling this function. |
| 141 | On failure, destroy the device and return negative value. |
| 142 | Return 0 on success. */ |
Gerd Hoffmann | 81a322d | 2009-08-14 10:36:05 +0200 | [diff] [blame] | 143 | int qdev_init(DeviceState *dev) |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 144 | { |
Anthony Liguori | 6e00858 | 2011-12-09 11:06:57 -0600 | [diff] [blame] | 145 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
Gerd Hoffmann | 959f733 | 2009-09-01 09:56:12 +0200 | [diff] [blame] | 146 | int rc; |
| 147 | |
Gerd Hoffmann | 131ec1b | 2009-09-25 21:42:34 +0200 | [diff] [blame] | 148 | assert(dev->state == DEV_STATE_CREATED); |
Anthony Liguori | 6e00858 | 2011-12-09 11:06:57 -0600 | [diff] [blame] | 149 | |
Anthony Liguori | d307af7 | 2011-12-09 15:02:56 -0600 | [diff] [blame] | 150 | rc = dc->init(dev); |
Markus Armbruster | 18cfeb5 | 2009-10-07 01:15:56 +0200 | [diff] [blame] | 151 | if (rc < 0) { |
| 152 | qdev_free(dev); |
Gerd Hoffmann | 959f733 | 2009-09-01 09:56:12 +0200 | [diff] [blame] | 153 | return rc; |
Markus Armbruster | 18cfeb5 | 2009-10-07 01:15:56 +0200 | [diff] [blame] | 154 | } |
Anthony Liguori | 6e00858 | 2011-12-09 11:06:57 -0600 | [diff] [blame] | 155 | if (qdev_get_vmsd(dev)) { |
| 156 | vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev, |
Jan Kiszka | 4d2ffa0 | 2010-05-15 13:32:40 +0200 | [diff] [blame] | 157 | dev->instance_id_alias, |
| 158 | dev->alias_required_for_version); |
| 159 | } |
Gerd Hoffmann | 131ec1b | 2009-09-25 21:42:34 +0200 | [diff] [blame] | 160 | dev->state = DEV_STATE_INITIALIZED; |
Anthony Liguori | 94afdad | 2011-12-04 11:36:01 -0600 | [diff] [blame] | 161 | if (dev->hotplugged) { |
| 162 | device_reset(dev); |
Jan Kiszka | 5ab28c8 | 2011-07-24 19:38:36 +0200 | [diff] [blame] | 163 | } |
Gerd Hoffmann | 959f733 | 2009-09-01 09:56:12 +0200 | [diff] [blame] | 164 | return 0; |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 165 | } |
| 166 | |
Jan Kiszka | 4d2ffa0 | 2010-05-15 13:32:40 +0200 | [diff] [blame] | 167 | void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id, |
| 168 | int required_for_version) |
| 169 | { |
| 170 | assert(dev->state == DEV_STATE_CREATED); |
| 171 | dev->instance_id_alias = alias_id; |
| 172 | dev->alias_required_for_version = required_for_version; |
| 173 | } |
| 174 | |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 175 | int qdev_unplug(DeviceState *dev) |
| 176 | { |
Anthony Liguori | 6e00858 | 2011-12-09 11:06:57 -0600 | [diff] [blame] | 177 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
| 178 | |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 179 | if (!dev->parent_bus->allow_hotplug) { |
Markus Armbruster | cc601cb | 2010-03-22 11:38:13 +0100 | [diff] [blame] | 180 | qerror_report(QERR_BUS_NO_HOTPLUG, dev->parent_bus->name); |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 181 | return -1; |
| 182 | } |
Anthony Liguori | 6e00858 | 2011-12-09 11:06:57 -0600 | [diff] [blame] | 183 | assert(dc->unplug != NULL); |
Amit Shah | 593831d | 2009-11-02 14:56:41 +0530 | [diff] [blame] | 184 | |
Alex Williamson | 0ac8ef7 | 2011-01-04 12:37:50 -0700 | [diff] [blame] | 185 | qdev_hot_removed = true; |
| 186 | |
Anthony Liguori | 6e00858 | 2011-12-09 11:06:57 -0600 | [diff] [blame] | 187 | return dc->unplug(dev); |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 188 | } |
| 189 | |
Anthony Liguori | ec990eb | 2010-11-19 18:55:59 +0900 | [diff] [blame] | 190 | static int qdev_reset_one(DeviceState *dev, void *opaque) |
| 191 | { |
Anthony Liguori | 94afdad | 2011-12-04 11:36:01 -0600 | [diff] [blame] | 192 | device_reset(dev); |
Anthony Liguori | ec990eb | 2010-11-19 18:55:59 +0900 | [diff] [blame] | 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | BusState *sysbus_get_default(void) |
| 198 | { |
Stefan Weil | 6869489 | 2010-12-16 19:33:22 +0100 | [diff] [blame] | 199 | if (!main_system_bus) { |
Isaku Yamahata | 2da8bb9 | 2011-08-02 10:59:13 +0900 | [diff] [blame] | 200 | main_system_bus_create(); |
Stefan Weil | 6869489 | 2010-12-16 19:33:22 +0100 | [diff] [blame] | 201 | } |
Anthony Liguori | ec990eb | 2010-11-19 18:55:59 +0900 | [diff] [blame] | 202 | return main_system_bus; |
| 203 | } |
| 204 | |
Isaku Yamahata | b4694b7 | 2010-11-19 18:56:00 +0900 | [diff] [blame] | 205 | static int qbus_reset_one(BusState *bus, void *opaque) |
| 206 | { |
| 207 | if (bus->info->reset) { |
| 208 | return bus->info->reset(bus); |
| 209 | } |
| 210 | return 0; |
| 211 | } |
| 212 | |
Isaku Yamahata | 5af0a04 | 2010-11-19 18:56:01 +0900 | [diff] [blame] | 213 | void qdev_reset_all(DeviceState *dev) |
| 214 | { |
| 215 | qdev_walk_children(dev, qdev_reset_one, qbus_reset_one, NULL); |
| 216 | } |
| 217 | |
Isaku Yamahata | 80376c3 | 2010-12-20 14:33:35 +0900 | [diff] [blame] | 218 | void qbus_reset_all_fn(void *opaque) |
| 219 | { |
| 220 | BusState *bus = opaque; |
Michael S. Tsirkin | f530cce | 2010-12-20 15:17:10 +0200 | [diff] [blame] | 221 | qbus_walk_children(bus, qdev_reset_one, qbus_reset_one, NULL); |
Isaku Yamahata | 80376c3 | 2010-12-20 14:33:35 +0900 | [diff] [blame] | 222 | } |
| 223 | |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 224 | /* can be used as ->unplug() callback for the simple cases */ |
| 225 | int qdev_simple_unplug_cb(DeviceState *dev) |
| 226 | { |
| 227 | /* just zap it */ |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 228 | object_unparent(OBJECT(dev)); |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 229 | qdev_free(dev); |
| 230 | return 0; |
| 231 | } |
| 232 | |
Michael Tokarev | 3b29a10 | 2011-04-06 17:51:59 +0400 | [diff] [blame] | 233 | |
| 234 | /* Like qdev_init(), but terminate program via error_report() instead of |
Markus Armbruster | e23a1b3 | 2009-10-07 01:15:58 +0200 | [diff] [blame] | 235 | returning an error value. This is okay during machine creation. |
| 236 | Don't use for hotplug, because there callers need to recover from |
| 237 | failure. Exception: if you know the device's init() callback can't |
| 238 | fail, then qdev_init_nofail() can't fail either, and is therefore |
| 239 | usable even then. But relying on the device implementation that |
| 240 | way is somewhat unclean, and best avoided. */ |
| 241 | void qdev_init_nofail(DeviceState *dev) |
| 242 | { |
Markus Armbruster | bd6c9a6 | 2010-05-27 21:23:08 +0200 | [diff] [blame] | 243 | if (qdev_init(dev) < 0) { |
Anthony Liguori | 6e00858 | 2011-12-09 11:06:57 -0600 | [diff] [blame] | 244 | error_report("Initialization of device %s failed", |
| 245 | object_get_typename(OBJECT(dev))); |
Markus Armbruster | bd6c9a6 | 2010-05-27 21:23:08 +0200 | [diff] [blame] | 246 | exit(1); |
| 247 | } |
Markus Armbruster | e23a1b3 | 2009-10-07 01:15:58 +0200 | [diff] [blame] | 248 | } |
| 249 | |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 250 | /* Unlink device from bus and free the structure. */ |
| 251 | void qdev_free(DeviceState *dev) |
| 252 | { |
Anthony Liguori | 32fea40 | 2011-12-16 14:34:46 -0600 | [diff] [blame] | 253 | object_delete(OBJECT(dev)); |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 254 | } |
| 255 | |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 256 | void qdev_machine_creation_done(void) |
| 257 | { |
| 258 | /* |
| 259 | * ok, initial machine setup is done, starting from now we can |
| 260 | * only create hotpluggable devices |
| 261 | */ |
| 262 | qdev_hotplug = 1; |
| 263 | } |
| 264 | |
Alex Williamson | 0ac8ef7 | 2011-01-04 12:37:50 -0700 | [diff] [blame] | 265 | bool qdev_machine_modified(void) |
| 266 | { |
| 267 | return qdev_hot_added || qdev_hot_removed; |
| 268 | } |
| 269 | |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 270 | BusState *qdev_get_parent_bus(DeviceState *dev) |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 271 | { |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 272 | return dev->parent_bus; |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 273 | } |
| 274 | |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 275 | void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n) |
| 276 | { |
| 277 | assert(dev->num_gpio_in == 0); |
| 278 | dev->num_gpio_in = n; |
| 279 | dev->gpio_in = qemu_allocate_irqs(handler, dev, n); |
| 280 | } |
| 281 | |
| 282 | void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n) |
| 283 | { |
| 284 | assert(dev->num_gpio_out == 0); |
| 285 | dev->num_gpio_out = n; |
| 286 | dev->gpio_out = pins; |
| 287 | } |
| 288 | |
| 289 | qemu_irq qdev_get_gpio_in(DeviceState *dev, int n) |
| 290 | { |
| 291 | assert(n >= 0 && n < dev->num_gpio_in); |
| 292 | return dev->gpio_in[n]; |
| 293 | } |
| 294 | |
| 295 | void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin) |
| 296 | { |
| 297 | assert(n >= 0 && n < dev->num_gpio_out); |
| 298 | dev->gpio_out[n] = pin; |
| 299 | } |
| 300 | |
Gerd Hoffmann | ed16ab5 | 2009-10-21 15:25:26 +0200 | [diff] [blame] | 301 | void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd) |
| 302 | { |
Jan Kiszka | 6eed185 | 2011-07-20 12:20:22 +0200 | [diff] [blame] | 303 | qdev_prop_set_macaddr(dev, "mac", nd->macaddr.a); |
Gerd Hoffmann | ed16ab5 | 2009-10-21 15:25:26 +0200 | [diff] [blame] | 304 | if (nd->vlan) |
| 305 | qdev_prop_set_vlan(dev, "vlan", nd->vlan); |
| 306 | if (nd->netdev) |
| 307 | qdev_prop_set_netdev(dev, "netdev", nd->netdev); |
Amit Shah | 75422b0 | 2010-02-25 17:24:43 +0530 | [diff] [blame] | 308 | if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED && |
Gerd Hoffmann | 97b1562 | 2009-10-21 15:25:35 +0200 | [diff] [blame] | 309 | qdev_prop_exists(dev, "vectors")) { |
| 310 | qdev_prop_set_uint32(dev, "vectors", nd->nvectors); |
| 311 | } |
Peter Maydell | 48e2faf | 2011-05-20 16:50:01 +0100 | [diff] [blame] | 312 | nd->instantiated = 1; |
Gerd Hoffmann | ed16ab5 | 2009-10-21 15:25:26 +0200 | [diff] [blame] | 313 | } |
| 314 | |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 315 | BusState *qdev_get_child_bus(DeviceState *dev, const char *name) |
Paul Brook | 4d6ae67 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 316 | { |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 317 | BusState *bus; |
Paul Brook | 4d6ae67 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 318 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 319 | QLIST_FOREACH(bus, &dev->child_bus, sibling) { |
Paul Brook | 4d6ae67 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 320 | if (strcmp(name, bus->name) == 0) { |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 321 | return bus; |
Paul Brook | 4d6ae67 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | return NULL; |
| 325 | } |
| 326 | |
Anthony Liguori | 81699d8 | 2010-11-19 18:55:58 +0900 | [diff] [blame] | 327 | int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn, |
| 328 | qbus_walkerfn *busfn, void *opaque) |
| 329 | { |
| 330 | DeviceState *dev; |
| 331 | int err; |
| 332 | |
| 333 | if (busfn) { |
| 334 | err = busfn(bus, opaque); |
| 335 | if (err) { |
| 336 | return err; |
| 337 | } |
| 338 | } |
| 339 | |
Paolo Bonzini | d8bb00d | 2011-09-14 09:28:06 +0200 | [diff] [blame] | 340 | QTAILQ_FOREACH(dev, &bus->children, sibling) { |
Anthony Liguori | 81699d8 | 2010-11-19 18:55:58 +0900 | [diff] [blame] | 341 | err = qdev_walk_children(dev, devfn, busfn, opaque); |
| 342 | if (err < 0) { |
| 343 | return err; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn, |
| 351 | qbus_walkerfn *busfn, void *opaque) |
| 352 | { |
| 353 | BusState *bus; |
| 354 | int err; |
| 355 | |
| 356 | if (devfn) { |
| 357 | err = devfn(dev, opaque); |
| 358 | if (err) { |
| 359 | return err; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | QLIST_FOREACH(bus, &dev->child_bus, sibling) { |
| 364 | err = qbus_walk_children(bus, devfn, busfn, opaque); |
| 365 | if (err < 0) { |
| 366 | return err; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | return 0; |
| 371 | } |
| 372 | |
Isaku Yamahata | a2ee6b4 | 2010-12-24 12:14:12 +0900 | [diff] [blame] | 373 | DeviceState *qdev_find_recursive(BusState *bus, const char *id) |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 374 | { |
| 375 | DeviceState *dev, *ret; |
| 376 | BusState *child; |
| 377 | |
Paolo Bonzini | d8bb00d | 2011-09-14 09:28:06 +0200 | [diff] [blame] | 378 | QTAILQ_FOREACH(dev, &bus->children, sibling) { |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 379 | if (dev->id && strcmp(dev->id, id) == 0) |
| 380 | return dev; |
| 381 | QLIST_FOREACH(child, &dev->child_bus, sibling) { |
| 382 | ret = qdev_find_recursive(child, id); |
| 383 | if (ret) { |
| 384 | return ret; |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | return NULL; |
| 389 | } |
| 390 | |
Gerd Hoffmann | cd739fb | 2009-09-16 22:25:27 +0200 | [diff] [blame] | 391 | void qbus_create_inplace(BusState *bus, BusInfo *info, |
| 392 | DeviceState *parent, const char *name) |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 393 | { |
Gerd Hoffmann | d271de9 | 2009-07-15 13:59:24 +0200 | [diff] [blame] | 394 | char *buf; |
| 395 | int i,len; |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 396 | |
Gerd Hoffmann | 10c4c98 | 2009-06-30 14:12:08 +0200 | [diff] [blame] | 397 | bus->info = info; |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 398 | bus->parent = parent; |
Gerd Hoffmann | d271de9 | 2009-07-15 13:59:24 +0200 | [diff] [blame] | 399 | |
| 400 | if (name) { |
| 401 | /* use supplied name */ |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 402 | bus->name = g_strdup(name); |
Gerd Hoffmann | d271de9 | 2009-07-15 13:59:24 +0200 | [diff] [blame] | 403 | } else if (parent && parent->id) { |
| 404 | /* parent device has id -> use it for bus name */ |
| 405 | len = strlen(parent->id) + 16; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 406 | buf = g_malloc(len); |
Gerd Hoffmann | d271de9 | 2009-07-15 13:59:24 +0200 | [diff] [blame] | 407 | snprintf(buf, len, "%s.%d", parent->id, parent->num_child_bus); |
| 408 | bus->name = buf; |
| 409 | } else { |
| 410 | /* no id -> use lowercase bus type for bus name */ |
| 411 | len = strlen(info->name) + 16; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 412 | buf = g_malloc(len); |
Gerd Hoffmann | d271de9 | 2009-07-15 13:59:24 +0200 | [diff] [blame] | 413 | len = snprintf(buf, len, "%s.%d", info->name, |
| 414 | parent ? parent->num_child_bus : 0); |
| 415 | for (i = 0; i < len; i++) |
Christoph Egger | bb87ece | 2009-07-30 15:28:45 +0200 | [diff] [blame] | 416 | buf[i] = qemu_tolower(buf[i]); |
Gerd Hoffmann | d271de9 | 2009-07-15 13:59:24 +0200 | [diff] [blame] | 417 | bus->name = buf; |
| 418 | } |
| 419 | |
Paolo Bonzini | d8bb00d | 2011-09-14 09:28:06 +0200 | [diff] [blame] | 420 | QTAILQ_INIT(&bus->children); |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 421 | if (parent) { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 422 | QLIST_INSERT_HEAD(&parent->child_bus, bus, sibling); |
Gerd Hoffmann | d271de9 | 2009-07-15 13:59:24 +0200 | [diff] [blame] | 423 | parent->num_child_bus++; |
Isaku Yamahata | 80376c3 | 2010-12-20 14:33:35 +0900 | [diff] [blame] | 424 | } else if (bus != main_system_bus) { |
| 425 | /* TODO: once all bus devices are qdevified, |
| 426 | only reset handler for main_system_bus should be registered here. */ |
| 427 | qemu_register_reset(qbus_reset_all_fn, bus); |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 428 | } |
Gerd Hoffmann | cd739fb | 2009-09-16 22:25:27 +0200 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name) |
| 432 | { |
| 433 | BusState *bus; |
| 434 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 435 | bus = g_malloc0(info->size); |
Gerd Hoffmann | cd739fb | 2009-09-16 22:25:27 +0200 | [diff] [blame] | 436 | bus->qdev_allocated = 1; |
| 437 | qbus_create_inplace(bus, info, parent, name); |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 438 | return bus; |
| 439 | } |
Gerd Hoffmann | cae4956 | 2009-06-05 15:53:17 +0100 | [diff] [blame] | 440 | |
Isaku Yamahata | 2da8bb9 | 2011-08-02 10:59:13 +0900 | [diff] [blame] | 441 | static void main_system_bus_create(void) |
| 442 | { |
| 443 | /* assign main_system_bus before qbus_create_inplace() |
| 444 | * in order to make "if (bus != main_system_bus)" work */ |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 445 | main_system_bus = g_malloc0(system_bus_info.size); |
Isaku Yamahata | 2da8bb9 | 2011-08-02 10:59:13 +0900 | [diff] [blame] | 446 | main_system_bus->qdev_allocated = 1; |
| 447 | qbus_create_inplace(main_system_bus, &system_bus_info, NULL, |
| 448 | "main-system-bus"); |
| 449 | } |
| 450 | |
Gerd Hoffmann | 131ec1b | 2009-09-25 21:42:34 +0200 | [diff] [blame] | 451 | void qbus_free(BusState *bus) |
| 452 | { |
| 453 | DeviceState *dev; |
| 454 | |
Paolo Bonzini | d8bb00d | 2011-09-14 09:28:06 +0200 | [diff] [blame] | 455 | while ((dev = QTAILQ_FIRST(&bus->children)) != NULL) { |
Gerd Hoffmann | 131ec1b | 2009-09-25 21:42:34 +0200 | [diff] [blame] | 456 | qdev_free(dev); |
| 457 | } |
| 458 | if (bus->parent) { |
| 459 | QLIST_REMOVE(bus, sibling); |
| 460 | bus->parent->num_child_bus--; |
Isaku Yamahata | 80376c3 | 2010-12-20 14:33:35 +0900 | [diff] [blame] | 461 | } else { |
| 462 | assert(bus != main_system_bus); /* main_system_bus is never freed */ |
| 463 | qemu_unregister_reset(qbus_reset_all_fn, bus); |
Gerd Hoffmann | 131ec1b | 2009-09-25 21:42:34 +0200 | [diff] [blame] | 464 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 465 | g_free((void*)bus->name); |
Gerd Hoffmann | 131ec1b | 2009-09-25 21:42:34 +0200 | [diff] [blame] | 466 | if (bus->qdev_allocated) { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 467 | g_free(bus); |
Gerd Hoffmann | 131ec1b | 2009-09-25 21:42:34 +0200 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | |
Gleb Natapov | 1ca4d09 | 2010-12-08 13:35:05 +0200 | [diff] [blame] | 471 | static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size) |
| 472 | { |
| 473 | int l = 0; |
| 474 | |
| 475 | if (dev && dev->parent_bus) { |
| 476 | char *d; |
| 477 | l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size); |
| 478 | if (dev->parent_bus->info->get_fw_dev_path) { |
| 479 | d = dev->parent_bus->info->get_fw_dev_path(dev); |
| 480 | l += snprintf(p + l, size - l, "%s", d); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 481 | g_free(d); |
Gleb Natapov | 1ca4d09 | 2010-12-08 13:35:05 +0200 | [diff] [blame] | 482 | } else { |
Anthony Liguori | f79f2bf | 2011-12-04 11:17:51 -0600 | [diff] [blame] | 483 | l += snprintf(p + l, size - l, "%s", object_get_typename(OBJECT(dev))); |
Gleb Natapov | 1ca4d09 | 2010-12-08 13:35:05 +0200 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | l += snprintf(p + l , size - l, "/"); |
| 487 | |
| 488 | return l; |
| 489 | } |
| 490 | |
| 491 | char* qdev_get_fw_dev_path(DeviceState *dev) |
| 492 | { |
| 493 | char path[128]; |
| 494 | int l; |
| 495 | |
| 496 | l = qdev_get_fw_dev_path_helper(dev, path, 128); |
| 497 | |
| 498 | path[l-1] = '\0'; |
| 499 | |
| 500 | return strdup(path); |
| 501 | } |
Anthony Liguori | 85ed303 | 2011-12-12 14:29:25 -0600 | [diff] [blame] | 502 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 503 | static char *qdev_get_type(Object *obj, Error **errp) |
Anthony Liguori | cd34d66 | 2011-12-12 14:29:43 -0600 | [diff] [blame] | 504 | { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 505 | return g_strdup(object_get_typename(obj)); |
Anthony Liguori | 44677de | 2011-12-12 14:29:26 -0600 | [diff] [blame] | 506 | } |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 507 | |
| 508 | /** |
| 509 | * Legacy property handling |
| 510 | */ |
| 511 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 512 | static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque, |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 513 | const char *name, Error **errp) |
| 514 | { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 515 | DeviceState *dev = DEVICE(obj); |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 516 | Property *prop = opaque; |
| 517 | |
Paolo Bonzini | e3cb6ba | 2011-12-18 17:05:06 +0100 | [diff] [blame] | 518 | char buffer[1024]; |
| 519 | char *ptr = buffer; |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 520 | |
Paolo Bonzini | e3cb6ba | 2011-12-18 17:05:06 +0100 | [diff] [blame] | 521 | prop->info->print(dev, prop, buffer, sizeof(buffer)); |
| 522 | visit_type_str(v, &ptr, name, errp); |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 523 | } |
| 524 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 525 | static void qdev_set_legacy_property(Object *obj, Visitor *v, void *opaque, |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 526 | const char *name, Error **errp) |
| 527 | { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 528 | DeviceState *dev = DEVICE(obj); |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 529 | Property *prop = opaque; |
Paolo Bonzini | e3cb6ba | 2011-12-18 17:05:06 +0100 | [diff] [blame] | 530 | Error *local_err = NULL; |
| 531 | char *ptr = NULL; |
| 532 | int ret; |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 533 | |
| 534 | if (dev->state != DEV_STATE_CREATED) { |
| 535 | error_set(errp, QERR_PERMISSION_DENIED); |
| 536 | return; |
| 537 | } |
| 538 | |
Paolo Bonzini | e3cb6ba | 2011-12-18 17:05:06 +0100 | [diff] [blame] | 539 | visit_type_str(v, &ptr, name, &local_err); |
| 540 | if (local_err) { |
| 541 | error_propagate(errp, local_err); |
| 542 | return; |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 543 | } |
Paolo Bonzini | e3cb6ba | 2011-12-18 17:05:06 +0100 | [diff] [blame] | 544 | |
| 545 | ret = prop->info->parse(dev, prop, ptr); |
Paolo Bonzini | 7db4c4e | 2011-12-18 17:05:07 +0100 | [diff] [blame] | 546 | error_set_from_qdev_prop_error(errp, ret, dev, prop, ptr); |
Paolo Bonzini | e3cb6ba | 2011-12-18 17:05:06 +0100 | [diff] [blame] | 547 | g_free(ptr); |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | /** |
| 551 | * @qdev_add_legacy_property - adds a legacy property |
| 552 | * |
| 553 | * 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] | 554 | * be given names and types in the "legacy" namespace. |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 555 | * |
Paolo Bonzini | 68ee356 | 2012-02-02 10:17:19 +0100 | [diff] [blame] | 556 | * Legacy properties are string versions of other OOM properties. The format |
| 557 | * of the string depends on the property type. |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 558 | */ |
| 559 | void qdev_property_add_legacy(DeviceState *dev, Property *prop, |
| 560 | Error **errp) |
| 561 | { |
Paolo Bonzini | ca2cc78 | 2011-12-18 17:05:11 +0100 | [diff] [blame] | 562 | gchar *name, *type; |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 563 | |
Paolo Bonzini | 68ee356 | 2012-02-02 10:17:19 +0100 | [diff] [blame] | 564 | if (!prop->info->print && !prop->info->parse) { |
| 565 | return; |
| 566 | } |
Paolo Bonzini | ca2cc78 | 2011-12-18 17:05:11 +0100 | [diff] [blame] | 567 | name = g_strdup_printf("legacy-%s", prop->name); |
Paolo Bonzini | cafe5bd | 2011-12-18 17:05:10 +0100 | [diff] [blame] | 568 | type = g_strdup_printf("legacy<%s>", |
| 569 | prop->info->legacy_name ?: prop->info->name); |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 570 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 571 | object_property_add(OBJECT(dev), name, type, |
Paolo Bonzini | 68ee356 | 2012-02-02 10:17:19 +0100 | [diff] [blame] | 572 | prop->info->print ? qdev_get_legacy_property : prop->info->get, |
| 573 | prop->info->parse ? qdev_set_legacy_property : prop->info->set, |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 574 | NULL, |
| 575 | prop, errp); |
Anthony Liguori | a5296ca | 2011-12-12 14:29:27 -0600 | [diff] [blame] | 576 | |
| 577 | g_free(type); |
Paolo Bonzini | ca2cc78 | 2011-12-18 17:05:11 +0100 | [diff] [blame] | 578 | g_free(name); |
| 579 | } |
| 580 | |
| 581 | /** |
| 582 | * @qdev_property_add_static - add a @Property to a device. |
| 583 | * |
| 584 | * Static properties access data in a struct. The actual type of the |
| 585 | * property and the field depends on the property type. |
| 586 | */ |
| 587 | void qdev_property_add_static(DeviceState *dev, Property *prop, |
| 588 | Error **errp) |
| 589 | { |
Paolo Bonzini | d822979 | 2012-02-02 09:47:13 +0100 | [diff] [blame] | 590 | /* |
| 591 | * TODO qdev_prop_ptr does not have getters or setters. It must |
| 592 | * go now that it can be replaced with links. The test should be |
| 593 | * removed along with it: all static properties are read/write. |
| 594 | */ |
| 595 | if (!prop->info->get && !prop->info->set) { |
| 596 | return; |
| 597 | } |
| 598 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 599 | object_property_add(OBJECT(dev), prop->name, prop->info->name, |
| 600 | prop->info->get, prop->info->set, |
Paolo Bonzini | dd0ba25 | 2012-02-02 13:08:48 +0100 | [diff] [blame] | 601 | prop->info->release, |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 602 | prop, errp); |
Anthony Liguori | 6a146eb | 2011-12-12 14:29:42 -0600 | [diff] [blame] | 603 | } |
Anthony Liguori | 1de81d2 | 2011-12-19 16:37:46 -0600 | [diff] [blame] | 604 | |
Anthony Liguori | 9674bfe | 2011-12-22 15:06:37 -0600 | [diff] [blame] | 605 | static void device_initfn(Object *obj) |
| 606 | { |
| 607 | DeviceState *dev = DEVICE(obj); |
| 608 | Property *prop; |
| 609 | |
| 610 | if (qdev_hotplug) { |
| 611 | dev->hotplugged = 1; |
| 612 | qdev_hot_added = true; |
| 613 | } |
| 614 | |
| 615 | dev->instance_id_alias = -1; |
Anthony Liguori | 9674bfe | 2011-12-22 15:06:37 -0600 | [diff] [blame] | 616 | dev->state = DEV_STATE_CREATED; |
| 617 | |
Anthony Liguori | 9674bfe | 2011-12-22 15:06:37 -0600 | [diff] [blame] | 618 | for (prop = qdev_get_props(dev); prop && prop->name; prop++) { |
| 619 | qdev_property_add_legacy(dev, prop, NULL); |
| 620 | qdev_property_add_static(dev, prop, NULL); |
| 621 | } |
| 622 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 623 | object_property_add_str(OBJECT(dev), "type", qdev_get_type, NULL, NULL); |
Paolo Bonzini | 4f2d3d7 | 2012-02-02 09:43:02 +0100 | [diff] [blame] | 624 | qdev_prop_set_defaults(dev, qdev_get_props(dev)); |
Anthony Liguori | 9674bfe | 2011-12-22 15:06:37 -0600 | [diff] [blame] | 625 | } |
| 626 | |
Anthony Liguori | 60adba3 | 2011-12-23 08:38:56 -0600 | [diff] [blame] | 627 | /* Unlink device from bus and free the structure. */ |
| 628 | static void device_finalize(Object *obj) |
| 629 | { |
| 630 | DeviceState *dev = DEVICE(obj); |
| 631 | BusState *bus; |
Anthony Liguori | 60adba3 | 2011-12-23 08:38:56 -0600 | [diff] [blame] | 632 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
| 633 | |
| 634 | if (dev->state == DEV_STATE_INITIALIZED) { |
| 635 | while (dev->num_child_bus) { |
| 636 | bus = QLIST_FIRST(&dev->child_bus); |
| 637 | qbus_free(bus); |
| 638 | } |
| 639 | if (qdev_get_vmsd(dev)) { |
| 640 | vmstate_unregister(dev, qdev_get_vmsd(dev), dev); |
| 641 | } |
| 642 | if (dc->exit) { |
| 643 | dc->exit(dev); |
| 644 | } |
| 645 | if (dev->opts) { |
| 646 | qemu_opts_del(dev->opts); |
| 647 | } |
| 648 | } |
| 649 | QTAILQ_REMOVE(&dev->parent_bus->children, dev, sibling); |
Anthony Liguori | 60adba3 | 2011-12-23 08:38:56 -0600 | [diff] [blame] | 650 | } |
| 651 | |
Anthony Liguori | 94afdad | 2011-12-04 11:36:01 -0600 | [diff] [blame] | 652 | void device_reset(DeviceState *dev) |
| 653 | { |
| 654 | DeviceClass *klass = DEVICE_GET_CLASS(dev); |
| 655 | |
| 656 | if (klass->reset) { |
| 657 | klass->reset(dev); |
| 658 | } |
| 659 | } |
| 660 | |
Anthony Liguori | 32fea40 | 2011-12-16 14:34:46 -0600 | [diff] [blame] | 661 | static TypeInfo device_type_info = { |
| 662 | .name = TYPE_DEVICE, |
| 663 | .parent = TYPE_OBJECT, |
| 664 | .instance_size = sizeof(DeviceState), |
Anthony Liguori | 9674bfe | 2011-12-22 15:06:37 -0600 | [diff] [blame] | 665 | .instance_init = device_initfn, |
Anthony Liguori | 60adba3 | 2011-12-23 08:38:56 -0600 | [diff] [blame] | 666 | .instance_finalize = device_finalize, |
Anthony Liguori | 32fea40 | 2011-12-16 14:34:46 -0600 | [diff] [blame] | 667 | .abstract = true, |
| 668 | .class_size = sizeof(DeviceClass), |
| 669 | }; |
| 670 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 671 | static void qdev_register_types(void) |
Anthony Liguori | 32fea40 | 2011-12-16 14:34:46 -0600 | [diff] [blame] | 672 | { |
| 673 | type_register_static(&device_type_info); |
| 674 | } |
| 675 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 676 | type_init(qdev_register_types) |