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