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" |
Gerd Hoffmann | cae4956 | 2009-06-05 15:53:17 +0100 | [diff] [blame] | 31 | #include "monitor.h" |
Luiz Capitulino | 3ced9f7 | 2009-11-18 23:05:33 -0200 | [diff] [blame] | 32 | #include "qerror.h" |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 33 | |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 34 | static int qdev_hotplug = 0; |
| 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; |
Paul Brook | 4d6ae67 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 38 | |
Gerd Hoffmann | 0958b4c | 2009-10-26 15:56:45 +0100 | [diff] [blame] | 39 | DeviceInfo *device_info_list; |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 40 | |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 41 | static BusState *qbus_find_recursive(BusState *bus, const char *name, |
| 42 | const BusInfo *info); |
| 43 | static BusState *qbus_find(const char *path); |
| 44 | |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 45 | /* Register a new device type. */ |
Gerd Hoffmann | 074f2ff | 2009-06-10 09:41:42 +0200 | [diff] [blame] | 46 | void qdev_register(DeviceInfo *info) |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 47 | { |
Gerd Hoffmann | 074f2ff | 2009-06-10 09:41:42 +0200 | [diff] [blame] | 48 | assert(info->size >= sizeof(DeviceState)); |
Gerd Hoffmann | 042f84d | 2009-06-30 14:12:09 +0200 | [diff] [blame] | 49 | assert(!info->next); |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 50 | |
Gerd Hoffmann | 042f84d | 2009-06-30 14:12:09 +0200 | [diff] [blame] | 51 | info->next = device_info_list; |
| 52 | device_info_list = info; |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 53 | } |
| 54 | |
Gerd Hoffmann | 81ebb98 | 2009-07-15 13:43:32 +0200 | [diff] [blame] | 55 | static DeviceInfo *qdev_find_info(BusInfo *bus_info, const char *name) |
| 56 | { |
| 57 | DeviceInfo *info; |
| 58 | |
Gerd Hoffmann | 3320e56 | 2009-07-15 13:43:33 +0200 | [diff] [blame] | 59 | /* first check device names */ |
Gerd Hoffmann | 81ebb98 | 2009-07-15 13:43:32 +0200 | [diff] [blame] | 60 | for (info = device_info_list; info != NULL; info = info->next) { |
| 61 | if (bus_info && info->bus_info != bus_info) |
| 62 | continue; |
| 63 | if (strcmp(info->name, name) != 0) |
| 64 | continue; |
| 65 | return info; |
| 66 | } |
Gerd Hoffmann | 3320e56 | 2009-07-15 13:43:33 +0200 | [diff] [blame] | 67 | |
| 68 | /* failing that check the aliases */ |
| 69 | for (info = device_info_list; info != NULL; info = info->next) { |
| 70 | if (bus_info && info->bus_info != bus_info) |
| 71 | continue; |
| 72 | if (!info->alias) |
| 73 | continue; |
| 74 | if (strcmp(info->alias, name) != 0) |
| 75 | continue; |
| 76 | return info; |
| 77 | } |
Gerd Hoffmann | 81ebb98 | 2009-07-15 13:43:32 +0200 | [diff] [blame] | 78 | return NULL; |
| 79 | } |
| 80 | |
Markus Armbruster | 0c17542 | 2010-02-19 19:12:18 +0100 | [diff] [blame] | 81 | static DeviceState *qdev_create_from_info(BusState *bus, DeviceInfo *info) |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 82 | { |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 83 | DeviceState *dev; |
| 84 | |
Markus Armbruster | 0c17542 | 2010-02-19 19:12:18 +0100 | [diff] [blame] | 85 | assert(bus->info == info->bus_info); |
Gerd Hoffmann | 042f84d | 2009-06-30 14:12:09 +0200 | [diff] [blame] | 86 | dev = qemu_mallocz(info->size); |
| 87 | dev->info = info; |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 88 | dev->parent_bus = bus; |
Gerd Hoffmann | ee6847d | 2009-07-15 13:43:31 +0200 | [diff] [blame] | 89 | qdev_prop_set_defaults(dev, dev->info->props); |
| 90 | qdev_prop_set_defaults(dev, dev->parent_bus->info->props); |
Gerd Hoffmann | 458fb67 | 2009-12-08 13:11:33 +0100 | [diff] [blame] | 91 | qdev_prop_set_globals(dev); |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 92 | QLIST_INSERT_HEAD(&bus->children, dev, sibling); |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 93 | if (qdev_hotplug) { |
| 94 | assert(bus->allow_hotplug); |
| 95 | dev->hotplugged = 1; |
| 96 | } |
Gerd Hoffmann | 131ec1b | 2009-09-25 21:42:34 +0200 | [diff] [blame] | 97 | dev->state = DEV_STATE_CREATED; |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 98 | return dev; |
| 99 | } |
| 100 | |
Markus Armbruster | 0c17542 | 2010-02-19 19:12:18 +0100 | [diff] [blame] | 101 | /* Create a new device. This only initializes the device state structure |
| 102 | and allows properties to be set. qdev_init should be called to |
| 103 | initialize the actual device emulation. */ |
| 104 | DeviceState *qdev_create(BusState *bus, const char *name) |
| 105 | { |
| 106 | DeviceInfo *info; |
| 107 | |
| 108 | if (!bus) { |
| 109 | if (!main_system_bus) { |
| 110 | main_system_bus = qbus_create(&system_bus_info, NULL, "main-system-bus"); |
| 111 | } |
| 112 | bus = main_system_bus; |
| 113 | } |
| 114 | |
| 115 | info = qdev_find_info(bus->info, name); |
| 116 | if (!info) { |
| 117 | hw_error("Unknown device '%s' for bus '%s'\n", name, bus->info->name); |
| 118 | } |
| 119 | |
| 120 | return qdev_create_from_info(bus, info); |
| 121 | } |
| 122 | |
Markus Armbruster | 8a9662c | 2010-02-18 18:44:15 +0100 | [diff] [blame] | 123 | static void qdev_print_devinfo(DeviceInfo *info) |
Gerd Hoffmann | 1b524b0 | 2009-07-29 13:12:23 +0200 | [diff] [blame] | 124 | { |
Markus Armbruster | 8a9662c | 2010-02-18 18:44:15 +0100 | [diff] [blame] | 125 | error_printf("name \"%s\", bus %s", |
| 126 | info->name, info->bus_info->name); |
Gerd Hoffmann | 22f2e34 | 2009-08-03 11:26:48 +0200 | [diff] [blame] | 127 | if (info->alias) { |
Markus Armbruster | 8a9662c | 2010-02-18 18:44:15 +0100 | [diff] [blame] | 128 | error_printf(", alias \"%s\"", info->alias); |
Gerd Hoffmann | 22f2e34 | 2009-08-03 11:26:48 +0200 | [diff] [blame] | 129 | } |
| 130 | if (info->desc) { |
Markus Armbruster | 8a9662c | 2010-02-18 18:44:15 +0100 | [diff] [blame] | 131 | error_printf(", desc \"%s\"", info->desc); |
Gerd Hoffmann | 22f2e34 | 2009-08-03 11:26:48 +0200 | [diff] [blame] | 132 | } |
| 133 | if (info->no_user) { |
Markus Armbruster | 8a9662c | 2010-02-18 18:44:15 +0100 | [diff] [blame] | 134 | error_printf(", no-user"); |
Gerd Hoffmann | 22f2e34 | 2009-08-03 11:26:48 +0200 | [diff] [blame] | 135 | } |
Markus Armbruster | 8a9662c | 2010-02-18 18:44:15 +0100 | [diff] [blame] | 136 | error_printf("\n"); |
Gerd Hoffmann | 1b524b0 | 2009-07-29 13:12:23 +0200 | [diff] [blame] | 137 | } |
| 138 | |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 139 | static int set_property(const char *name, const char *value, void *opaque) |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 140 | { |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 141 | DeviceState *dev = opaque; |
| 142 | |
| 143 | if (strcmp(name, "driver") == 0) |
| 144 | return 0; |
| 145 | if (strcmp(name, "bus") == 0) |
| 146 | return 0; |
| 147 | |
Mark McLoughlin | 3df04ac | 2009-09-23 11:24:05 +0100 | [diff] [blame] | 148 | if (qdev_prop_parse(dev, name, value) == -1) { |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 149 | return -1; |
| 150 | } |
| 151 | return 0; |
| 152 | } |
| 153 | |
Markus Armbruster | ff952ba | 2010-01-29 19:48:57 +0100 | [diff] [blame] | 154 | int qdev_device_help(QemuOpts *opts) |
| 155 | { |
| 156 | const char *driver; |
| 157 | DeviceInfo *info; |
Markus Armbruster | 08350cf | 2010-01-29 19:49:00 +0100 | [diff] [blame] | 158 | Property *prop; |
Markus Armbruster | ff952ba | 2010-01-29 19:48:57 +0100 | [diff] [blame] | 159 | |
| 160 | driver = qemu_opt_get(opts, "driver"); |
| 161 | if (driver && !strcmp(driver, "?")) { |
| 162 | for (info = device_info_list; info != NULL; info = info->next) { |
Markus Armbruster | c64eafa | 2010-02-19 13:31:49 +0100 | [diff] [blame] | 163 | if (info->no_user) { |
| 164 | continue; /* not available, don't show */ |
| 165 | } |
Markus Armbruster | 8a9662c | 2010-02-18 18:44:15 +0100 | [diff] [blame] | 166 | qdev_print_devinfo(info); |
Markus Armbruster | ff952ba | 2010-01-29 19:48:57 +0100 | [diff] [blame] | 167 | } |
| 168 | return 1; |
| 169 | } |
| 170 | |
Markus Armbruster | 08350cf | 2010-01-29 19:49:00 +0100 | [diff] [blame] | 171 | if (!qemu_opt_get(opts, "?")) { |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | info = qdev_find_info(NULL, driver); |
| 176 | if (!info) { |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | for (prop = info->props; prop && prop->name; prop++) { |
Markus Armbruster | 036f716 | 2010-02-19 11:47:06 +0100 | [diff] [blame] | 181 | /* |
| 182 | * TODO Properties without a parser are just for dirty hacks. |
| 183 | * qdev_prop_ptr is the only such PropertyInfo. It's marked |
| 184 | * for removal. This conditional should be removed along with |
| 185 | * it. |
| 186 | */ |
| 187 | if (!prop->info->parse) { |
| 188 | continue; /* no way to set it, don't show */ |
| 189 | } |
Markus Armbruster | 8a9662c | 2010-02-18 18:44:15 +0100 | [diff] [blame] | 190 | error_printf("%s.%s=%s\n", info->name, prop->name, prop->info->name); |
Markus Armbruster | 08350cf | 2010-01-29 19:49:00 +0100 | [diff] [blame] | 191 | } |
| 192 | return 1; |
Markus Armbruster | ff952ba | 2010-01-29 19:48:57 +0100 | [diff] [blame] | 193 | } |
| 194 | |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 195 | DeviceState *qdev_device_add(QemuOpts *opts) |
| 196 | { |
| 197 | const char *driver, *path, *id; |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 198 | DeviceInfo *info; |
| 199 | DeviceState *qdev; |
| 200 | BusState *bus; |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 201 | |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 202 | driver = qemu_opt_get(opts, "driver"); |
| 203 | if (!driver) { |
Markus Armbruster | 1ecda02 | 2010-02-18 17:25:24 +0100 | [diff] [blame] | 204 | error_report("-device: no driver specified"); |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 205 | return NULL; |
| 206 | } |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 207 | |
| 208 | /* find driver */ |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 209 | info = qdev_find_info(NULL, driver); |
Markus Armbruster | c64eafa | 2010-02-19 13:31:49 +0100 | [diff] [blame] | 210 | if (!info || info->no_user) { |
Markus Armbruster | ab5b027 | 2010-03-02 18:15:09 +0100 | [diff] [blame] | 211 | qerror_report(QERR_DEVICE_NOT_FOUND, driver); |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 212 | return NULL; |
| 213 | } |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 214 | |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 215 | /* find bus */ |
| 216 | path = qemu_opt_get(opts, "bus"); |
| 217 | if (path != NULL) { |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 218 | bus = qbus_find(path); |
Markus Armbruster | 327867b | 2010-02-19 19:08:45 +0100 | [diff] [blame] | 219 | if (bus && bus->info != info->bus_info) { |
| 220 | error_report("Device '%s' can't go on a %s bus", |
| 221 | driver, bus->info->name); |
| 222 | return NULL; |
| 223 | } |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 224 | } else { |
| 225 | bus = qbus_find_recursive(main_system_bus, NULL, info->bus_info); |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 226 | } |
Gerd Hoffmann | 7557008 | 2009-08-31 14:23:58 +0200 | [diff] [blame] | 227 | if (!bus) { |
Markus Armbruster | 1ecda02 | 2010-02-18 17:25:24 +0100 | [diff] [blame] | 228 | error_report("Did not find %s bus for %s", |
| 229 | path ? path : info->bus_info->name, info->name); |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 230 | return NULL; |
Gerd Hoffmann | 7557008 | 2009-08-31 14:23:58 +0200 | [diff] [blame] | 231 | } |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 232 | if (qdev_hotplug && !bus->allow_hotplug) { |
Markus Armbruster | 1ecda02 | 2010-02-18 17:25:24 +0100 | [diff] [blame] | 233 | error_report("Bus %s does not support hotplugging", |
| 234 | bus->name); |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 235 | return NULL; |
| 236 | } |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 237 | |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 238 | /* create device, set properties */ |
Markus Armbruster | 0c17542 | 2010-02-19 19:12:18 +0100 | [diff] [blame] | 239 | qdev = qdev_create_from_info(bus, info); |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 240 | id = qemu_opts_id(opts); |
| 241 | if (id) { |
| 242 | qdev->id = id; |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 243 | } |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 244 | if (qemu_opt_foreach(opts, set_property, qdev, 1) != 0) { |
| 245 | qdev_free(qdev); |
| 246 | return NULL; |
| 247 | } |
Markus Armbruster | 5c17ca2 | 2009-10-07 01:16:01 +0200 | [diff] [blame] | 248 | if (qdev_init(qdev) < 0) { |
Markus Armbruster | 1ecda02 | 2010-02-18 17:25:24 +0100 | [diff] [blame] | 249 | error_report("Error initializing device %s", driver); |
Gerd Hoffmann | 81a322d | 2009-08-14 10:36:05 +0200 | [diff] [blame] | 250 | return NULL; |
| 251 | } |
Gerd Hoffmann | ef80b46 | 2009-09-25 21:42:49 +0200 | [diff] [blame] | 252 | qdev->opts = opts; |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 253 | return qdev; |
| 254 | } |
| 255 | |
Michael S. Tsirkin | 7f23f81 | 2009-09-16 13:40:27 +0300 | [diff] [blame] | 256 | static void qdev_reset(void *opaque) |
| 257 | { |
| 258 | DeviceState *dev = opaque; |
| 259 | if (dev->info->reset) |
| 260 | dev->info->reset(dev); |
| 261 | } |
| 262 | |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 263 | /* Initialize a device. Device properties should be set before calling |
| 264 | this function. IRQs and MMIO regions should be connected/mapped after |
Markus Armbruster | 18cfeb5 | 2009-10-07 01:15:56 +0200 | [diff] [blame] | 265 | calling this function. |
| 266 | On failure, destroy the device and return negative value. |
| 267 | Return 0 on success. */ |
Gerd Hoffmann | 81a322d | 2009-08-14 10:36:05 +0200 | [diff] [blame] | 268 | int qdev_init(DeviceState *dev) |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 269 | { |
Gerd Hoffmann | 959f733 | 2009-09-01 09:56:12 +0200 | [diff] [blame] | 270 | int rc; |
| 271 | |
Gerd Hoffmann | 131ec1b | 2009-09-25 21:42:34 +0200 | [diff] [blame] | 272 | assert(dev->state == DEV_STATE_CREATED); |
Gerd Hoffmann | 959f733 | 2009-09-01 09:56:12 +0200 | [diff] [blame] | 273 | rc = dev->info->init(dev, dev->info); |
Markus Armbruster | 18cfeb5 | 2009-10-07 01:15:56 +0200 | [diff] [blame] | 274 | if (rc < 0) { |
| 275 | qdev_free(dev); |
Gerd Hoffmann | 959f733 | 2009-09-01 09:56:12 +0200 | [diff] [blame] | 276 | return rc; |
Markus Armbruster | 18cfeb5 | 2009-10-07 01:15:56 +0200 | [diff] [blame] | 277 | } |
Michael S. Tsirkin | 7f23f81 | 2009-09-16 13:40:27 +0300 | [diff] [blame] | 278 | qemu_register_reset(qdev_reset, dev); |
Gerd Hoffmann | 391a079 | 2009-09-01 09:56:14 +0200 | [diff] [blame] | 279 | if (dev->info->vmsd) |
| 280 | vmstate_register(-1, dev->info->vmsd, dev); |
Gerd Hoffmann | 131ec1b | 2009-09-25 21:42:34 +0200 | [diff] [blame] | 281 | dev->state = DEV_STATE_INITIALIZED; |
Gerd Hoffmann | 959f733 | 2009-09-01 09:56:12 +0200 | [diff] [blame] | 282 | return 0; |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 283 | } |
| 284 | |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 285 | int qdev_unplug(DeviceState *dev) |
| 286 | { |
| 287 | if (!dev->parent_bus->allow_hotplug) { |
Markus Armbruster | 1ecda02 | 2010-02-18 17:25:24 +0100 | [diff] [blame] | 288 | error_report("Bus %s does not support hotplugging", |
| 289 | dev->parent_bus->name); |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 290 | return -1; |
| 291 | } |
Amit Shah | 593831d | 2009-11-02 14:56:41 +0530 | [diff] [blame] | 292 | assert(dev->info->unplug != NULL); |
| 293 | |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 294 | return dev->info->unplug(dev); |
| 295 | } |
| 296 | |
| 297 | /* can be used as ->unplug() callback for the simple cases */ |
| 298 | int qdev_simple_unplug_cb(DeviceState *dev) |
| 299 | { |
| 300 | /* just zap it */ |
| 301 | qdev_free(dev); |
| 302 | return 0; |
| 303 | } |
| 304 | |
Markus Armbruster | e23a1b3 | 2009-10-07 01:15:58 +0200 | [diff] [blame] | 305 | /* Like qdev_init(), but terminate program via hw_error() instead of |
| 306 | returning an error value. This is okay during machine creation. |
| 307 | Don't use for hotplug, because there callers need to recover from |
| 308 | failure. Exception: if you know the device's init() callback can't |
| 309 | fail, then qdev_init_nofail() can't fail either, and is therefore |
| 310 | usable even then. But relying on the device implementation that |
| 311 | way is somewhat unclean, and best avoided. */ |
| 312 | void qdev_init_nofail(DeviceState *dev) |
| 313 | { |
| 314 | DeviceInfo *info = dev->info; |
| 315 | |
| 316 | if (qdev_init(dev) < 0) |
| 317 | hw_error("Initialization of device %s failed\n", info->name); |
| 318 | } |
| 319 | |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 320 | /* Unlink device from bus and free the structure. */ |
| 321 | void qdev_free(DeviceState *dev) |
| 322 | { |
Gerd Hoffmann | 131ec1b | 2009-09-25 21:42:34 +0200 | [diff] [blame] | 323 | BusState *bus; |
| 324 | |
| 325 | if (dev->state == DEV_STATE_INITIALIZED) { |
| 326 | while (dev->num_child_bus) { |
| 327 | bus = QLIST_FIRST(&dev->child_bus); |
| 328 | qbus_free(bus); |
| 329 | } |
Gerd Hoffmann | 131ec1b | 2009-09-25 21:42:34 +0200 | [diff] [blame] | 330 | if (dev->info->vmsd) |
| 331 | vmstate_unregister(dev->info->vmsd, dev); |
Gerd Hoffmann | d29275f | 2009-09-25 21:42:35 +0200 | [diff] [blame] | 332 | if (dev->info->exit) |
| 333 | dev->info->exit(dev); |
Gerd Hoffmann | ef80b46 | 2009-09-25 21:42:49 +0200 | [diff] [blame] | 334 | if (dev->opts) |
| 335 | qemu_opts_del(dev->opts); |
Gerd Hoffmann | 131ec1b | 2009-09-25 21:42:34 +0200 | [diff] [blame] | 336 | } |
Michael S. Tsirkin | 7f23f81 | 2009-09-16 13:40:27 +0300 | [diff] [blame] | 337 | qemu_unregister_reset(qdev_reset, dev); |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 338 | QLIST_REMOVE(dev, sibling); |
Gerd Hoffmann | ccb63de | 2009-07-15 13:43:34 +0200 | [diff] [blame] | 339 | qemu_free(dev); |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 340 | } |
| 341 | |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 342 | void qdev_machine_creation_done(void) |
| 343 | { |
| 344 | /* |
| 345 | * ok, initial machine setup is done, starting from now we can |
| 346 | * only create hotpluggable devices |
| 347 | */ |
| 348 | qdev_hotplug = 1; |
| 349 | } |
| 350 | |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 351 | /* Get a character (serial) device interface. */ |
| 352 | CharDriverState *qdev_init_chardev(DeviceState *dev) |
| 353 | { |
| 354 | static int next_serial; |
Amit Shah | 98b1925 | 2010-01-20 00:36:52 +0530 | [diff] [blame] | 355 | |
| 356 | /* FIXME: This function needs to go away: use chardev properties! */ |
| 357 | return serial_hds[next_serial++]; |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 358 | } |
| 359 | |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 360 | BusState *qdev_get_parent_bus(DeviceState *dev) |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 361 | { |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 362 | return dev->parent_bus; |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 363 | } |
| 364 | |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 365 | void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n) |
| 366 | { |
| 367 | assert(dev->num_gpio_in == 0); |
| 368 | dev->num_gpio_in = n; |
| 369 | dev->gpio_in = qemu_allocate_irqs(handler, dev, n); |
| 370 | } |
| 371 | |
| 372 | void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n) |
| 373 | { |
| 374 | assert(dev->num_gpio_out == 0); |
| 375 | dev->num_gpio_out = n; |
| 376 | dev->gpio_out = pins; |
| 377 | } |
| 378 | |
| 379 | qemu_irq qdev_get_gpio_in(DeviceState *dev, int n) |
| 380 | { |
| 381 | assert(n >= 0 && n < dev->num_gpio_in); |
| 382 | return dev->gpio_in[n]; |
| 383 | } |
| 384 | |
| 385 | void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin) |
| 386 | { |
| 387 | assert(n >= 0 && n < dev->num_gpio_out); |
| 388 | dev->gpio_out[n] = pin; |
| 389 | } |
| 390 | |
Gerd Hoffmann | ed16ab5 | 2009-10-21 15:25:26 +0200 | [diff] [blame] | 391 | void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd) |
| 392 | { |
| 393 | qdev_prop_set_macaddr(dev, "mac", nd->macaddr); |
| 394 | if (nd->vlan) |
| 395 | qdev_prop_set_vlan(dev, "vlan", nd->vlan); |
| 396 | if (nd->netdev) |
| 397 | qdev_prop_set_netdev(dev, "netdev", nd->netdev); |
Amit Shah | 75422b0 | 2010-02-25 17:24:43 +0530 | [diff] [blame] | 398 | if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED && |
Gerd Hoffmann | 97b1562 | 2009-10-21 15:25:35 +0200 | [diff] [blame] | 399 | qdev_prop_exists(dev, "vectors")) { |
| 400 | qdev_prop_set_uint32(dev, "vectors", nd->nvectors); |
| 401 | } |
Gerd Hoffmann | ed16ab5 | 2009-10-21 15:25:26 +0200 | [diff] [blame] | 402 | } |
| 403 | |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 404 | static int next_block_unit[IF_COUNT]; |
| 405 | |
| 406 | /* Get a block device. This should only be used for single-drive devices |
| 407 | (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the |
| 408 | appropriate bus. */ |
| 409 | BlockDriverState *qdev_init_bdrv(DeviceState *dev, BlockInterfaceType type) |
| 410 | { |
| 411 | int unit = next_block_unit[type]++; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 412 | DriveInfo *dinfo; |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 413 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 414 | dinfo = drive_get(type, 0, unit); |
| 415 | return dinfo ? dinfo->bdrv : NULL; |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 416 | } |
Paul Brook | 4d6ae67 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 417 | |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 418 | BusState *qdev_get_child_bus(DeviceState *dev, const char *name) |
Paul Brook | 4d6ae67 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 419 | { |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 420 | BusState *bus; |
Paul Brook | 4d6ae67 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 421 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 422 | QLIST_FOREACH(bus, &dev->child_bus, sibling) { |
Paul Brook | 4d6ae67 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 423 | if (strcmp(name, bus->name) == 0) { |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 424 | return bus; |
Paul Brook | 4d6ae67 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | return NULL; |
| 428 | } |
| 429 | |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 430 | static BusState *qbus_find_recursive(BusState *bus, const char *name, |
| 431 | const BusInfo *info) |
| 432 | { |
| 433 | DeviceState *dev; |
| 434 | BusState *child, *ret; |
| 435 | int match = 1; |
| 436 | |
| 437 | if (name && (strcmp(bus->name, name) != 0)) { |
| 438 | match = 0; |
| 439 | } |
| 440 | if (info && (bus->info != info)) { |
| 441 | match = 0; |
| 442 | } |
| 443 | if (match) { |
| 444 | return bus; |
| 445 | } |
| 446 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 447 | QLIST_FOREACH(dev, &bus->children, sibling) { |
| 448 | QLIST_FOREACH(child, &dev->child_bus, sibling) { |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 449 | ret = qbus_find_recursive(child, name, info); |
| 450 | if (ret) { |
| 451 | return ret; |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | return NULL; |
| 456 | } |
| 457 | |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 458 | static DeviceState *qdev_find_recursive(BusState *bus, const char *id) |
| 459 | { |
| 460 | DeviceState *dev, *ret; |
| 461 | BusState *child; |
| 462 | |
| 463 | QLIST_FOREACH(dev, &bus->children, sibling) { |
| 464 | if (dev->id && strcmp(dev->id, id) == 0) |
| 465 | return dev; |
| 466 | QLIST_FOREACH(child, &dev->child_bus, sibling) { |
| 467 | ret = qdev_find_recursive(child, id); |
| 468 | if (ret) { |
| 469 | return ret; |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | return NULL; |
| 474 | } |
| 475 | |
Markus Armbruster | 53db16b | 2010-02-18 18:55:59 +0100 | [diff] [blame] | 476 | static void qbus_list_bus(DeviceState *dev) |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 477 | { |
| 478 | BusState *child; |
| 479 | const char *sep = " "; |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 480 | |
Markus Armbruster | 53db16b | 2010-02-18 18:55:59 +0100 | [diff] [blame] | 481 | error_printf("child busses at \"%s\":", |
| 482 | dev->id ? dev->id : dev->info->name); |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 483 | QLIST_FOREACH(child, &dev->child_bus, sibling) { |
Markus Armbruster | 53db16b | 2010-02-18 18:55:59 +0100 | [diff] [blame] | 484 | error_printf("%s\"%s\"", sep, child->name); |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 485 | sep = ", "; |
| 486 | } |
Markus Armbruster | 53db16b | 2010-02-18 18:55:59 +0100 | [diff] [blame] | 487 | error_printf("\n"); |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 488 | } |
| 489 | |
Markus Armbruster | 53db16b | 2010-02-18 18:55:59 +0100 | [diff] [blame] | 490 | static void qbus_list_dev(BusState *bus) |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 491 | { |
| 492 | DeviceState *dev; |
| 493 | const char *sep = " "; |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 494 | |
Markus Armbruster | 53db16b | 2010-02-18 18:55:59 +0100 | [diff] [blame] | 495 | error_printf("devices at \"%s\":", bus->name); |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 496 | QLIST_FOREACH(dev, &bus->children, sibling) { |
Markus Armbruster | 53db16b | 2010-02-18 18:55:59 +0100 | [diff] [blame] | 497 | error_printf("%s\"%s\"", sep, dev->info->name); |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 498 | if (dev->id) |
Markus Armbruster | 53db16b | 2010-02-18 18:55:59 +0100 | [diff] [blame] | 499 | error_printf("/\"%s\"", dev->id); |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 500 | sep = ", "; |
| 501 | } |
Markus Armbruster | 53db16b | 2010-02-18 18:55:59 +0100 | [diff] [blame] | 502 | error_printf("\n"); |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | static BusState *qbus_find_bus(DeviceState *dev, char *elem) |
| 506 | { |
| 507 | BusState *child; |
| 508 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 509 | QLIST_FOREACH(child, &dev->child_bus, sibling) { |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 510 | if (strcmp(child->name, elem) == 0) { |
| 511 | return child; |
| 512 | } |
| 513 | } |
| 514 | return NULL; |
| 515 | } |
| 516 | |
| 517 | static DeviceState *qbus_find_dev(BusState *bus, char *elem) |
| 518 | { |
| 519 | DeviceState *dev; |
| 520 | |
| 521 | /* |
| 522 | * try to match in order: |
| 523 | * (1) instance id, if present |
| 524 | * (2) driver name |
| 525 | * (3) driver alias, if present |
| 526 | */ |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 527 | QLIST_FOREACH(dev, &bus->children, sibling) { |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 528 | if (dev->id && strcmp(dev->id, elem) == 0) { |
| 529 | return dev; |
| 530 | } |
| 531 | } |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 532 | QLIST_FOREACH(dev, &bus->children, sibling) { |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 533 | if (strcmp(dev->info->name, elem) == 0) { |
| 534 | return dev; |
| 535 | } |
| 536 | } |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 537 | QLIST_FOREACH(dev, &bus->children, sibling) { |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 538 | if (dev->info->alias && strcmp(dev->info->alias, elem) == 0) { |
| 539 | return dev; |
| 540 | } |
| 541 | } |
| 542 | return NULL; |
| 543 | } |
| 544 | |
| 545 | static BusState *qbus_find(const char *path) |
| 546 | { |
| 547 | DeviceState *dev; |
| 548 | BusState *bus; |
Markus Armbruster | 53db16b | 2010-02-18 18:55:59 +0100 | [diff] [blame] | 549 | char elem[128]; |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 550 | int pos, len; |
| 551 | |
| 552 | /* find start element */ |
| 553 | if (path[0] == '/') { |
| 554 | bus = main_system_bus; |
| 555 | pos = 0; |
| 556 | } else { |
| 557 | if (sscanf(path, "%127[^/]%n", elem, &len) != 1) { |
Markus Armbruster | fc98eb4 | 2010-02-19 16:09:25 +0100 | [diff] [blame^] | 558 | assert(!path[0]); |
| 559 | elem[0] = len = 0; |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 560 | } |
| 561 | bus = qbus_find_recursive(main_system_bus, elem, NULL); |
| 562 | if (!bus) { |
Markus Armbruster | 1ecda02 | 2010-02-18 17:25:24 +0100 | [diff] [blame] | 563 | error_report("bus \"%s\" not found", elem); |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 564 | return NULL; |
| 565 | } |
| 566 | pos = len; |
| 567 | } |
| 568 | |
| 569 | for (;;) { |
Markus Armbruster | fc98eb4 | 2010-02-19 16:09:25 +0100 | [diff] [blame^] | 570 | assert(path[pos] == '/' || !path[pos]); |
| 571 | while (path[pos] == '/') { |
| 572 | pos++; |
| 573 | } |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 574 | if (path[pos] == '\0') { |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 575 | return bus; |
| 576 | } |
| 577 | |
| 578 | /* find device */ |
Markus Armbruster | fc98eb4 | 2010-02-19 16:09:25 +0100 | [diff] [blame^] | 579 | if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) { |
| 580 | assert(0); |
| 581 | elem[0] = len = 0; |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 582 | } |
| 583 | pos += len; |
| 584 | dev = qbus_find_dev(bus, elem); |
| 585 | if (!dev) { |
Markus Armbruster | 1ecda02 | 2010-02-18 17:25:24 +0100 | [diff] [blame] | 586 | error_report("device \"%s\" not found", elem); |
Markus Armbruster | 53db16b | 2010-02-18 18:55:59 +0100 | [diff] [blame] | 587 | qbus_list_dev(bus); |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 588 | return NULL; |
| 589 | } |
Markus Armbruster | fc98eb4 | 2010-02-19 16:09:25 +0100 | [diff] [blame^] | 590 | |
| 591 | assert(path[pos] == '/' || !path[pos]); |
| 592 | while (path[pos] == '/') { |
| 593 | pos++; |
| 594 | } |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 595 | if (path[pos] == '\0') { |
| 596 | /* last specified element is a device. If it has exactly |
| 597 | * one child bus accept it nevertheless */ |
| 598 | switch (dev->num_child_bus) { |
| 599 | case 0: |
Markus Armbruster | 1ecda02 | 2010-02-18 17:25:24 +0100 | [diff] [blame] | 600 | error_report("device has no child bus (%s)", path); |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 601 | return NULL; |
| 602 | case 1: |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 603 | return QLIST_FIRST(&dev->child_bus); |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 604 | default: |
Markus Armbruster | 1ecda02 | 2010-02-18 17:25:24 +0100 | [diff] [blame] | 605 | error_report("device has multiple child busses (%s)", path); |
Markus Armbruster | 53db16b | 2010-02-18 18:55:59 +0100 | [diff] [blame] | 606 | qbus_list_bus(dev); |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 607 | return NULL; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | /* find bus */ |
Markus Armbruster | fc98eb4 | 2010-02-19 16:09:25 +0100 | [diff] [blame^] | 612 | if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) { |
| 613 | assert(0); |
| 614 | elem[0] = len = 0; |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 615 | } |
| 616 | pos += len; |
| 617 | bus = qbus_find_bus(dev, elem); |
| 618 | if (!bus) { |
Markus Armbruster | 1ecda02 | 2010-02-18 17:25:24 +0100 | [diff] [blame] | 619 | error_report("child bus \"%s\" not found", elem); |
Markus Armbruster | 53db16b | 2010-02-18 18:55:59 +0100 | [diff] [blame] | 620 | qbus_list_bus(dev); |
Gerd Hoffmann | 8ffb1bc | 2009-07-15 13:59:25 +0200 | [diff] [blame] | 621 | return NULL; |
| 622 | } |
| 623 | } |
| 624 | } |
| 625 | |
Gerd Hoffmann | cd739fb | 2009-09-16 22:25:27 +0200 | [diff] [blame] | 626 | void qbus_create_inplace(BusState *bus, BusInfo *info, |
| 627 | DeviceState *parent, const char *name) |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 628 | { |
Gerd Hoffmann | d271de9 | 2009-07-15 13:59:24 +0200 | [diff] [blame] | 629 | char *buf; |
| 630 | int i,len; |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 631 | |
Gerd Hoffmann | 10c4c98 | 2009-06-30 14:12:08 +0200 | [diff] [blame] | 632 | bus->info = info; |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 633 | bus->parent = parent; |
Gerd Hoffmann | d271de9 | 2009-07-15 13:59:24 +0200 | [diff] [blame] | 634 | |
| 635 | if (name) { |
| 636 | /* use supplied name */ |
| 637 | bus->name = qemu_strdup(name); |
| 638 | } else if (parent && parent->id) { |
| 639 | /* parent device has id -> use it for bus name */ |
| 640 | len = strlen(parent->id) + 16; |
| 641 | buf = qemu_malloc(len); |
| 642 | snprintf(buf, len, "%s.%d", parent->id, parent->num_child_bus); |
| 643 | bus->name = buf; |
| 644 | } else { |
| 645 | /* no id -> use lowercase bus type for bus name */ |
| 646 | len = strlen(info->name) + 16; |
| 647 | buf = qemu_malloc(len); |
| 648 | len = snprintf(buf, len, "%s.%d", info->name, |
| 649 | parent ? parent->num_child_bus : 0); |
| 650 | for (i = 0; i < len; i++) |
Christoph Egger | bb87ece | 2009-07-30 15:28:45 +0200 | [diff] [blame] | 651 | buf[i] = qemu_tolower(buf[i]); |
Gerd Hoffmann | d271de9 | 2009-07-15 13:59:24 +0200 | [diff] [blame] | 652 | bus->name = buf; |
| 653 | } |
| 654 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 655 | QLIST_INIT(&bus->children); |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 656 | if (parent) { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 657 | QLIST_INSERT_HEAD(&parent->child_bus, bus, sibling); |
Gerd Hoffmann | d271de9 | 2009-07-15 13:59:24 +0200 | [diff] [blame] | 658 | parent->num_child_bus++; |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 659 | } |
Gerd Hoffmann | cd739fb | 2009-09-16 22:25:27 +0200 | [diff] [blame] | 660 | |
| 661 | } |
| 662 | |
| 663 | BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name) |
| 664 | { |
| 665 | BusState *bus; |
| 666 | |
| 667 | bus = qemu_mallocz(info->size); |
| 668 | bus->qdev_allocated = 1; |
| 669 | qbus_create_inplace(bus, info, parent, name); |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 670 | return bus; |
| 671 | } |
Gerd Hoffmann | cae4956 | 2009-06-05 15:53:17 +0100 | [diff] [blame] | 672 | |
Gerd Hoffmann | 131ec1b | 2009-09-25 21:42:34 +0200 | [diff] [blame] | 673 | void qbus_free(BusState *bus) |
| 674 | { |
| 675 | DeviceState *dev; |
| 676 | |
| 677 | while ((dev = QLIST_FIRST(&bus->children)) != NULL) { |
| 678 | qdev_free(dev); |
| 679 | } |
| 680 | if (bus->parent) { |
| 681 | QLIST_REMOVE(bus, sibling); |
| 682 | bus->parent->num_child_bus--; |
| 683 | } |
| 684 | if (bus->qdev_allocated) { |
| 685 | qemu_free(bus); |
| 686 | } |
| 687 | } |
| 688 | |
Gerd Hoffmann | cae4956 | 2009-06-05 15:53:17 +0100 | [diff] [blame] | 689 | #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__) |
| 690 | static void qbus_print(Monitor *mon, BusState *bus, int indent); |
| 691 | |
Gerd Hoffmann | ee6847d | 2009-07-15 13:43:31 +0200 | [diff] [blame] | 692 | static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props, |
| 693 | const char *prefix, int indent) |
| 694 | { |
| 695 | char buf[64]; |
| 696 | |
| 697 | if (!props) |
| 698 | return; |
| 699 | while (props->name) { |
Markus Armbruster | 036f716 | 2010-02-19 11:47:06 +0100 | [diff] [blame] | 700 | /* |
| 701 | * TODO Properties without a print method are just for dirty |
| 702 | * hacks. qdev_prop_ptr is the only such PropertyInfo. It's |
| 703 | * marked for removal. The test props->info->print should be |
| 704 | * removed along with it. |
| 705 | */ |
Gerd Hoffmann | ee6847d | 2009-07-15 13:43:31 +0200 | [diff] [blame] | 706 | if (props->info->print) { |
| 707 | props->info->print(dev, props, buf, sizeof(buf)); |
| 708 | qdev_printf("%s-prop: %s = %s\n", prefix, props->name, buf); |
| 709 | } |
| 710 | props++; |
| 711 | } |
| 712 | } |
| 713 | |
Gerd Hoffmann | cae4956 | 2009-06-05 15:53:17 +0100 | [diff] [blame] | 714 | static void qdev_print(Monitor *mon, DeviceState *dev, int indent) |
| 715 | { |
Gerd Hoffmann | cae4956 | 2009-06-05 15:53:17 +0100 | [diff] [blame] | 716 | BusState *child; |
Gerd Hoffmann | ccb63de | 2009-07-15 13:43:34 +0200 | [diff] [blame] | 717 | qdev_printf("dev: %s, id \"%s\"\n", dev->info->name, |
| 718 | dev->id ? dev->id : ""); |
Gerd Hoffmann | cae4956 | 2009-06-05 15:53:17 +0100 | [diff] [blame] | 719 | indent += 2; |
| 720 | if (dev->num_gpio_in) { |
| 721 | qdev_printf("gpio-in %d\n", dev->num_gpio_in); |
| 722 | } |
| 723 | if (dev->num_gpio_out) { |
| 724 | qdev_printf("gpio-out %d\n", dev->num_gpio_out); |
| 725 | } |
Gerd Hoffmann | ee6847d | 2009-07-15 13:43:31 +0200 | [diff] [blame] | 726 | qdev_print_props(mon, dev, dev->info->props, "dev", indent); |
| 727 | qdev_print_props(mon, dev, dev->parent_bus->info->props, "bus", indent); |
Gerd Hoffmann | 10c4c98 | 2009-06-30 14:12:08 +0200 | [diff] [blame] | 728 | if (dev->parent_bus->info->print_dev) |
| 729 | dev->parent_bus->info->print_dev(mon, dev, indent); |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 730 | QLIST_FOREACH(child, &dev->child_bus, sibling) { |
Gerd Hoffmann | cae4956 | 2009-06-05 15:53:17 +0100 | [diff] [blame] | 731 | qbus_print(mon, child, indent); |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | static void qbus_print(Monitor *mon, BusState *bus, int indent) |
| 736 | { |
| 737 | struct DeviceState *dev; |
| 738 | |
| 739 | qdev_printf("bus: %s\n", bus->name); |
| 740 | indent += 2; |
Gerd Hoffmann | 10c4c98 | 2009-06-30 14:12:08 +0200 | [diff] [blame] | 741 | qdev_printf("type %s\n", bus->info->name); |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 742 | QLIST_FOREACH(dev, &bus->children, sibling) { |
Gerd Hoffmann | cae4956 | 2009-06-05 15:53:17 +0100 | [diff] [blame] | 743 | qdev_print(mon, dev, indent); |
| 744 | } |
| 745 | } |
| 746 | #undef qdev_printf |
| 747 | |
| 748 | void do_info_qtree(Monitor *mon) |
| 749 | { |
| 750 | if (main_system_bus) |
| 751 | qbus_print(mon, main_system_bus, 0); |
| 752 | } |
Gerd Hoffmann | 9316d30 | 2009-07-29 13:12:24 +0200 | [diff] [blame] | 753 | |
Gerd Hoffmann | f6c64e0 | 2009-08-03 15:03:09 +0200 | [diff] [blame] | 754 | void do_info_qdm(Monitor *mon) |
Gerd Hoffmann | 9316d30 | 2009-07-29 13:12:24 +0200 | [diff] [blame] | 755 | { |
| 756 | DeviceInfo *info; |
Gerd Hoffmann | 9316d30 | 2009-07-29 13:12:24 +0200 | [diff] [blame] | 757 | |
| 758 | for (info = device_info_list; info != NULL; info = info->next) { |
Markus Armbruster | 8a9662c | 2010-02-18 18:44:15 +0100 | [diff] [blame] | 759 | qdev_print_devinfo(info); |
Gerd Hoffmann | 9316d30 | 2009-07-29 13:12:24 +0200 | [diff] [blame] | 760 | } |
| 761 | } |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 762 | |
| 763 | void do_device_add(Monitor *mon, const QDict *qdict) |
| 764 | { |
| 765 | QemuOpts *opts; |
| 766 | |
| 767 | opts = qemu_opts_parse(&qemu_device_opts, |
| 768 | qdict_get_str(qdict, "config"), "driver"); |
Kevin Wolf | 0f853a3 | 2010-02-16 13:12:38 +0100 | [diff] [blame] | 769 | if (opts) { |
| 770 | if (qdev_device_help(opts) || qdev_device_add(opts) == NULL) { |
| 771 | qemu_opts_del(opts); |
| 772 | } |
| 773 | } |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | void do_device_del(Monitor *mon, const QDict *qdict) |
| 777 | { |
| 778 | const char *id = qdict_get_str(qdict, "id"); |
| 779 | DeviceState *dev; |
| 780 | |
| 781 | dev = qdev_find_recursive(main_system_bus, id); |
| 782 | if (NULL == dev) { |
Markus Armbruster | 1ecda02 | 2010-02-18 17:25:24 +0100 | [diff] [blame] | 783 | error_report("Device '%s' not found", id); |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 784 | return; |
| 785 | } |
| 786 | qdev_unplug(dev); |
| 787 | } |