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