Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [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 |
| 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "qdev.h" |
| 21 | #include "monitor.h" |
Luiz Capitulino | a15fef2 | 2012-03-29 12:38:50 -0300 | [diff] [blame] | 22 | #include "qmp-commands.h" |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 23 | |
| 24 | /* |
| 25 | * Aliases were a bad idea from the start. Let's keep them |
| 26 | * from spreading further. |
| 27 | */ |
| 28 | typedef struct QDevAlias |
| 29 | { |
| 30 | const char *typename; |
| 31 | const char *alias; |
| 32 | } QDevAlias; |
| 33 | |
| 34 | static const QDevAlias qdev_alias_table[] = { |
| 35 | { "virtio-blk-pci", "virtio-blk" }, |
| 36 | { "virtio-net-pci", "virtio-net" }, |
| 37 | { "virtio-serial-pci", "virtio-serial" }, |
| 38 | { "virtio-balloon-pci", "virtio-balloon" }, |
| 39 | { "virtio-blk-s390", "virtio-blk" }, |
| 40 | { "virtio-net-s390", "virtio-net" }, |
| 41 | { "virtio-serial-s390", "virtio-serial" }, |
| 42 | { "lsi53c895a", "lsi" }, |
| 43 | { "ich9-ahci", "ahci" }, |
| 44 | { } |
| 45 | }; |
| 46 | |
| 47 | static const char *qdev_class_get_alias(DeviceClass *dc) |
| 48 | { |
| 49 | const char *typename = object_class_get_name(OBJECT_CLASS(dc)); |
| 50 | int i; |
| 51 | |
| 52 | for (i = 0; qdev_alias_table[i].typename; i++) { |
| 53 | if (strcmp(qdev_alias_table[i].typename, typename) == 0) { |
| 54 | return qdev_alias_table[i].alias; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | return NULL; |
| 59 | } |
| 60 | |
| 61 | static bool qdev_class_has_alias(DeviceClass *dc) |
| 62 | { |
| 63 | return (qdev_class_get_alias(dc) != NULL); |
| 64 | } |
| 65 | |
| 66 | static void qdev_print_devinfo(ObjectClass *klass, void *opaque) |
| 67 | { |
| 68 | DeviceClass *dc; |
| 69 | bool *show_no_user = opaque; |
| 70 | |
| 71 | dc = (DeviceClass *)object_class_dynamic_cast(klass, TYPE_DEVICE); |
| 72 | |
| 73 | if (!dc || (show_no_user && !*show_no_user && dc->no_user)) { |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | error_printf("name \"%s\"", object_class_get_name(klass)); |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 78 | if (dc->bus_type) { |
| 79 | error_printf(", bus %s", dc->bus_type); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 80 | } |
| 81 | if (qdev_class_has_alias(dc)) { |
| 82 | error_printf(", alias \"%s\"", qdev_class_get_alias(dc)); |
| 83 | } |
| 84 | if (dc->desc) { |
| 85 | error_printf(", desc \"%s\"", dc->desc); |
| 86 | } |
| 87 | if (dc->no_user) { |
| 88 | error_printf(", no-user"); |
| 89 | } |
| 90 | error_printf("\n"); |
| 91 | } |
| 92 | |
| 93 | static int set_property(const char *name, const char *value, void *opaque) |
| 94 | { |
| 95 | DeviceState *dev = opaque; |
| 96 | |
| 97 | if (strcmp(name, "driver") == 0) |
| 98 | return 0; |
| 99 | if (strcmp(name, "bus") == 0) |
| 100 | return 0; |
| 101 | |
| 102 | if (qdev_prop_parse(dev, name, value) == -1) { |
| 103 | return -1; |
| 104 | } |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | static const char *find_typename_by_alias(const char *alias) |
| 109 | { |
| 110 | int i; |
| 111 | |
| 112 | for (i = 0; qdev_alias_table[i].alias; i++) { |
| 113 | if (strcmp(qdev_alias_table[i].alias, alias) == 0) { |
| 114 | return qdev_alias_table[i].typename; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | return NULL; |
| 119 | } |
| 120 | |
| 121 | int qdev_device_help(QemuOpts *opts) |
| 122 | { |
| 123 | const char *driver; |
| 124 | Property *prop; |
| 125 | ObjectClass *klass; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 126 | |
| 127 | driver = qemu_opt_get(opts, "driver"); |
| 128 | if (driver && !strcmp(driver, "?")) { |
| 129 | bool show_no_user = false; |
| 130 | object_class_foreach(qdev_print_devinfo, TYPE_DEVICE, false, &show_no_user); |
| 131 | return 1; |
| 132 | } |
| 133 | |
| 134 | if (!driver || !qemu_opt_get(opts, "?")) { |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | klass = object_class_by_name(driver); |
| 139 | if (!klass) { |
| 140 | const char *typename = find_typename_by_alias(driver); |
| 141 | |
| 142 | if (typename) { |
| 143 | driver = typename; |
| 144 | klass = object_class_by_name(driver); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | if (!klass) { |
| 149 | return 0; |
| 150 | } |
Paolo Bonzini | bce5447 | 2012-03-28 18:12:47 +0200 | [diff] [blame] | 151 | do { |
| 152 | for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) { |
| 153 | /* |
| 154 | * TODO Properties without a parser are just for dirty hacks. |
| 155 | * qdev_prop_ptr is the only such PropertyInfo. It's marked |
| 156 | * for removal. This conditional should be removed along with |
| 157 | * it. |
| 158 | */ |
Paolo Bonzini | 90ca64a | 2012-05-02 13:30:59 +0200 | [diff] [blame] | 159 | if (!prop->info->set) { |
Anthony Liguori | d03d6b4 | 2011-12-23 08:16:58 -0600 | [diff] [blame] | 160 | continue; /* no way to set it, don't show */ |
| 161 | } |
| 162 | error_printf("%s.%s=%s\n", driver, prop->name, |
| 163 | prop->info->legacy_name ?: prop->info->name); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 164 | } |
Paolo Bonzini | bce5447 | 2012-03-28 18:12:47 +0200 | [diff] [blame] | 165 | klass = object_class_get_parent(klass); |
| 166 | } while (klass != object_class_by_name(TYPE_DEVICE)); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 167 | return 1; |
| 168 | } |
| 169 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 170 | static Object *qdev_get_peripheral(void) |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 171 | { |
Anthony Liguori | 8b45d44 | 2011-12-23 09:08:05 -0600 | [diff] [blame] | 172 | static Object *dev; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 173 | |
| 174 | if (dev == NULL) { |
Andreas Färber | dfe47e7 | 2012-04-05 13:21:46 +0200 | [diff] [blame] | 175 | dev = container_get(qdev_get_machine(), "/peripheral"); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 176 | } |
| 177 | |
Anthony Liguori | 8b45d44 | 2011-12-23 09:08:05 -0600 | [diff] [blame] | 178 | return dev; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 179 | } |
| 180 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 181 | static Object *qdev_get_peripheral_anon(void) |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 182 | { |
Anthony Liguori | 8b45d44 | 2011-12-23 09:08:05 -0600 | [diff] [blame] | 183 | static Object *dev; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 184 | |
| 185 | if (dev == NULL) { |
Andreas Färber | dfe47e7 | 2012-04-05 13:21:46 +0200 | [diff] [blame] | 186 | dev = container_get(qdev_get_machine(), "/peripheral-anon"); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 187 | } |
| 188 | |
Anthony Liguori | 8b45d44 | 2011-12-23 09:08:05 -0600 | [diff] [blame] | 189 | return dev; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | static void qbus_list_bus(DeviceState *dev) |
| 193 | { |
| 194 | BusState *child; |
| 195 | const char *sep = " "; |
| 196 | |
| 197 | error_printf("child busses at \"%s\":", |
| 198 | dev->id ? dev->id : object_get_typename(OBJECT(dev))); |
| 199 | QLIST_FOREACH(child, &dev->child_bus, sibling) { |
| 200 | error_printf("%s\"%s\"", sep, child->name); |
| 201 | sep = ", "; |
| 202 | } |
| 203 | error_printf("\n"); |
| 204 | } |
| 205 | |
| 206 | static void qbus_list_dev(BusState *bus) |
| 207 | { |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame^] | 208 | BusChild *kid; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 209 | const char *sep = " "; |
| 210 | |
| 211 | error_printf("devices at \"%s\":", bus->name); |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame^] | 212 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
| 213 | DeviceState *dev = kid->child; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 214 | error_printf("%s\"%s\"", sep, object_get_typename(OBJECT(dev))); |
| 215 | if (dev->id) |
| 216 | error_printf("/\"%s\"", dev->id); |
| 217 | sep = ", "; |
| 218 | } |
| 219 | error_printf("\n"); |
| 220 | } |
| 221 | |
| 222 | static BusState *qbus_find_bus(DeviceState *dev, char *elem) |
| 223 | { |
| 224 | BusState *child; |
| 225 | |
| 226 | QLIST_FOREACH(child, &dev->child_bus, sibling) { |
| 227 | if (strcmp(child->name, elem) == 0) { |
| 228 | return child; |
| 229 | } |
| 230 | } |
| 231 | return NULL; |
| 232 | } |
| 233 | |
| 234 | static DeviceState *qbus_find_dev(BusState *bus, char *elem) |
| 235 | { |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame^] | 236 | BusChild *kid; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 237 | |
| 238 | /* |
| 239 | * try to match in order: |
| 240 | * (1) instance id, if present |
| 241 | * (2) driver name |
| 242 | * (3) driver alias, if present |
| 243 | */ |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame^] | 244 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
| 245 | DeviceState *dev = kid->child; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 246 | if (dev->id && strcmp(dev->id, elem) == 0) { |
| 247 | return dev; |
| 248 | } |
| 249 | } |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame^] | 250 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
| 251 | DeviceState *dev = kid->child; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 252 | if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) { |
| 253 | return dev; |
| 254 | } |
| 255 | } |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame^] | 256 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
| 257 | DeviceState *dev = kid->child; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 258 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
| 259 | |
| 260 | if (qdev_class_has_alias(dc) && |
| 261 | strcmp(qdev_class_get_alias(dc), elem) == 0) { |
| 262 | return dev; |
| 263 | } |
| 264 | } |
| 265 | return NULL; |
| 266 | } |
| 267 | |
| 268 | static BusState *qbus_find_recursive(BusState *bus, const char *name, |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 269 | const char *bus_typename) |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 270 | { |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame^] | 271 | BusChild *kid; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 272 | BusState *child, *ret; |
| 273 | int match = 1; |
| 274 | |
| 275 | if (name && (strcmp(bus->name, name) != 0)) { |
| 276 | match = 0; |
| 277 | } |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 278 | if (bus_typename && |
| 279 | (strcmp(object_get_typename(OBJECT(bus)), bus_typename) != 0)) { |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 280 | match = 0; |
| 281 | } |
| 282 | if (match) { |
| 283 | return bus; |
| 284 | } |
| 285 | |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame^] | 286 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
| 287 | DeviceState *dev = kid->child; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 288 | QLIST_FOREACH(child, &dev->child_bus, sibling) { |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 289 | ret = qbus_find_recursive(child, name, bus_typename); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 290 | if (ret) { |
| 291 | return ret; |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | return NULL; |
| 296 | } |
| 297 | |
| 298 | static BusState *qbus_find(const char *path) |
| 299 | { |
| 300 | DeviceState *dev; |
| 301 | BusState *bus; |
| 302 | char elem[128]; |
| 303 | int pos, len; |
| 304 | |
| 305 | /* find start element */ |
| 306 | if (path[0] == '/') { |
| 307 | bus = sysbus_get_default(); |
| 308 | pos = 0; |
| 309 | } else { |
| 310 | if (sscanf(path, "%127[^/]%n", elem, &len) != 1) { |
| 311 | assert(!path[0]); |
| 312 | elem[0] = len = 0; |
| 313 | } |
| 314 | bus = qbus_find_recursive(sysbus_get_default(), elem, NULL); |
| 315 | if (!bus) { |
| 316 | qerror_report(QERR_BUS_NOT_FOUND, elem); |
| 317 | return NULL; |
| 318 | } |
| 319 | pos = len; |
| 320 | } |
| 321 | |
| 322 | for (;;) { |
| 323 | assert(path[pos] == '/' || !path[pos]); |
| 324 | while (path[pos] == '/') { |
| 325 | pos++; |
| 326 | } |
| 327 | if (path[pos] == '\0') { |
| 328 | return bus; |
| 329 | } |
| 330 | |
| 331 | /* find device */ |
| 332 | if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) { |
| 333 | assert(0); |
| 334 | elem[0] = len = 0; |
| 335 | } |
| 336 | pos += len; |
| 337 | dev = qbus_find_dev(bus, elem); |
| 338 | if (!dev) { |
| 339 | qerror_report(QERR_DEVICE_NOT_FOUND, elem); |
| 340 | if (!monitor_cur_is_qmp()) { |
| 341 | qbus_list_dev(bus); |
| 342 | } |
| 343 | return NULL; |
| 344 | } |
| 345 | |
| 346 | assert(path[pos] == '/' || !path[pos]); |
| 347 | while (path[pos] == '/') { |
| 348 | pos++; |
| 349 | } |
| 350 | if (path[pos] == '\0') { |
| 351 | /* last specified element is a device. If it has exactly |
| 352 | * one child bus accept it nevertheless */ |
| 353 | switch (dev->num_child_bus) { |
| 354 | case 0: |
| 355 | qerror_report(QERR_DEVICE_NO_BUS, elem); |
| 356 | return NULL; |
| 357 | case 1: |
| 358 | return QLIST_FIRST(&dev->child_bus); |
| 359 | default: |
| 360 | qerror_report(QERR_DEVICE_MULTIPLE_BUSSES, elem); |
| 361 | if (!monitor_cur_is_qmp()) { |
| 362 | qbus_list_bus(dev); |
| 363 | } |
| 364 | return NULL; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | /* find bus */ |
| 369 | if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) { |
| 370 | assert(0); |
| 371 | elem[0] = len = 0; |
| 372 | } |
| 373 | pos += len; |
| 374 | bus = qbus_find_bus(dev, elem); |
| 375 | if (!bus) { |
| 376 | qerror_report(QERR_BUS_NOT_FOUND, elem); |
| 377 | if (!monitor_cur_is_qmp()) { |
| 378 | qbus_list_bus(dev); |
| 379 | } |
| 380 | return NULL; |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | DeviceState *qdev_device_add(QemuOpts *opts) |
| 386 | { |
| 387 | ObjectClass *obj; |
| 388 | DeviceClass *k; |
| 389 | const char *driver, *path, *id; |
| 390 | DeviceState *qdev; |
| 391 | BusState *bus; |
| 392 | |
| 393 | driver = qemu_opt_get(opts, "driver"); |
| 394 | if (!driver) { |
| 395 | qerror_report(QERR_MISSING_PARAMETER, "driver"); |
| 396 | return NULL; |
| 397 | } |
| 398 | |
| 399 | /* find driver */ |
| 400 | obj = object_class_by_name(driver); |
| 401 | if (!obj) { |
| 402 | const char *typename = find_typename_by_alias(driver); |
| 403 | |
| 404 | if (typename) { |
| 405 | driver = typename; |
| 406 | obj = object_class_by_name(driver); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | if (!obj) { |
| 411 | qerror_report(QERR_INVALID_PARAMETER_VALUE, "driver", "device type"); |
| 412 | return NULL; |
| 413 | } |
| 414 | |
| 415 | k = DEVICE_CLASS(obj); |
| 416 | |
| 417 | /* find bus */ |
| 418 | path = qemu_opt_get(opts, "bus"); |
| 419 | if (path != NULL) { |
| 420 | bus = qbus_find(path); |
| 421 | if (!bus) { |
| 422 | return NULL; |
| 423 | } |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 424 | if (strcmp(object_get_typename(OBJECT(bus)), k->bus_type) != 0) { |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 425 | qerror_report(QERR_BAD_BUS_FOR_DEVICE, |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 426 | driver, object_get_typename(OBJECT(bus))); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 427 | return NULL; |
| 428 | } |
| 429 | } else { |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 430 | bus = qbus_find_recursive(sysbus_get_default(), NULL, k->bus_type); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 431 | if (!bus) { |
| 432 | qerror_report(QERR_NO_BUS_FOR_DEVICE, |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 433 | driver, k->bus_type); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 434 | return NULL; |
| 435 | } |
| 436 | } |
| 437 | if (qdev_hotplug && !bus->allow_hotplug) { |
| 438 | qerror_report(QERR_BUS_NO_HOTPLUG, bus->name); |
| 439 | return NULL; |
| 440 | } |
| 441 | |
| 442 | if (!bus) { |
| 443 | bus = sysbus_get_default(); |
| 444 | } |
| 445 | |
| 446 | /* create device, set properties */ |
| 447 | qdev = DEVICE(object_new(driver)); |
| 448 | qdev_set_parent_bus(qdev, bus); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 449 | |
| 450 | id = qemu_opts_id(opts); |
| 451 | if (id) { |
| 452 | qdev->id = id; |
Anthony Liguori | b2d4b3f | 2012-02-12 11:36:24 -0600 | [diff] [blame] | 453 | } |
| 454 | if (qemu_opt_foreach(opts, set_property, qdev, 1) != 0) { |
| 455 | qdev_free(qdev); |
| 456 | return NULL; |
| 457 | } |
Anthony Liguori | b2d4b3f | 2012-02-12 11:36:24 -0600 | [diff] [blame] | 458 | if (qdev->id) { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 459 | object_property_add_child(qdev_get_peripheral(), qdev->id, |
| 460 | OBJECT(qdev), NULL); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 461 | } else { |
| 462 | static int anon_count; |
| 463 | gchar *name = g_strdup_printf("device[%d]", anon_count++); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 464 | object_property_add_child(qdev_get_peripheral_anon(), name, |
| 465 | OBJECT(qdev), NULL); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 466 | g_free(name); |
| 467 | } |
Paolo Bonzini | f424d5c | 2012-03-27 18:38:46 +0200 | [diff] [blame] | 468 | if (qdev_init(qdev) < 0) { |
| 469 | qerror_report(QERR_DEVICE_INIT_FAILED, driver); |
| 470 | return NULL; |
| 471 | } |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 472 | qdev->opts = opts; |
| 473 | return qdev; |
| 474 | } |
| 475 | |
| 476 | |
| 477 | #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__) |
| 478 | static void qbus_print(Monitor *mon, BusState *bus, int indent); |
| 479 | |
| 480 | static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props, |
Paolo Bonzini | bce5447 | 2012-03-28 18:12:47 +0200 | [diff] [blame] | 481 | int indent) |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 482 | { |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 483 | if (!props) |
| 484 | return; |
Paolo Bonzini | d822979 | 2012-02-02 09:47:13 +0100 | [diff] [blame] | 485 | for (; props->name; props++) { |
| 486 | Error *err = NULL; |
| 487 | char *value; |
| 488 | char *legacy_name = g_strdup_printf("legacy-%s", props->name); |
| 489 | if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) { |
| 490 | value = object_property_get_str(OBJECT(dev), legacy_name, &err); |
| 491 | } else { |
Paolo Bonzini | 8185bfc | 2012-05-02 13:31:00 +0200 | [diff] [blame] | 492 | value = object_property_print(OBJECT(dev), props->name, &err); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 493 | } |
Paolo Bonzini | d822979 | 2012-02-02 09:47:13 +0100 | [diff] [blame] | 494 | g_free(legacy_name); |
| 495 | |
| 496 | if (err) { |
| 497 | error_free(err); |
| 498 | continue; |
| 499 | } |
Paolo Bonzini | bce5447 | 2012-03-28 18:12:47 +0200 | [diff] [blame] | 500 | qdev_printf("%s = %s\n", props->name, |
Paolo Bonzini | d822979 | 2012-02-02 09:47:13 +0100 | [diff] [blame] | 501 | value && *value ? value : "<null>"); |
| 502 | g_free(value); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 506 | static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent) |
| 507 | { |
| 508 | BusClass *bc = BUS_GET_CLASS(bus); |
| 509 | |
| 510 | if (bc->print_dev) { |
| 511 | bc->print_dev(mon, dev, indent); |
| 512 | } |
| 513 | } |
| 514 | |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 515 | static void qdev_print(Monitor *mon, DeviceState *dev, int indent) |
| 516 | { |
Paolo Bonzini | bce5447 | 2012-03-28 18:12:47 +0200 | [diff] [blame] | 517 | ObjectClass *class; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 518 | BusState *child; |
| 519 | qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)), |
| 520 | dev->id ? dev->id : ""); |
| 521 | indent += 2; |
| 522 | if (dev->num_gpio_in) { |
| 523 | qdev_printf("gpio-in %d\n", dev->num_gpio_in); |
| 524 | } |
| 525 | if (dev->num_gpio_out) { |
| 526 | qdev_printf("gpio-out %d\n", dev->num_gpio_out); |
| 527 | } |
Paolo Bonzini | bce5447 | 2012-03-28 18:12:47 +0200 | [diff] [blame] | 528 | class = object_get_class(OBJECT(dev)); |
| 529 | do { |
| 530 | qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent); |
| 531 | class = object_class_get_parent(class); |
| 532 | } while (class != object_class_by_name(TYPE_DEVICE)); |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 533 | bus_print_dev(dev->parent_bus, mon, dev, indent + 2); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 534 | QLIST_FOREACH(child, &dev->child_bus, sibling) { |
| 535 | qbus_print(mon, child, indent); |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | static void qbus_print(Monitor *mon, BusState *bus, int indent) |
| 540 | { |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame^] | 541 | BusChild *kid; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 542 | |
| 543 | qdev_printf("bus: %s\n", bus->name); |
| 544 | indent += 2; |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 545 | qdev_printf("type %s\n", object_get_typename(OBJECT(bus))); |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame^] | 546 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
| 547 | DeviceState *dev = kid->child; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 548 | qdev_print(mon, dev, indent); |
| 549 | } |
| 550 | } |
| 551 | #undef qdev_printf |
| 552 | |
| 553 | void do_info_qtree(Monitor *mon) |
| 554 | { |
| 555 | if (sysbus_get_default()) |
| 556 | qbus_print(mon, sysbus_get_default(), 0); |
| 557 | } |
| 558 | |
| 559 | void do_info_qdm(Monitor *mon) |
| 560 | { |
| 561 | object_class_foreach(qdev_print_devinfo, TYPE_DEVICE, false, NULL); |
| 562 | } |
| 563 | |
| 564 | int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data) |
| 565 | { |
Luiz Capitulino | 4e89978 | 2012-04-18 17:24:01 -0300 | [diff] [blame] | 566 | Error *local_err = NULL; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 567 | QemuOpts *opts; |
| 568 | |
Luiz Capitulino | 4e89978 | 2012-04-18 17:24:01 -0300 | [diff] [blame] | 569 | opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, &local_err); |
| 570 | if (error_is_set(&local_err)) { |
| 571 | qerror_report_err(local_err); |
| 572 | error_free(local_err); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 573 | return -1; |
| 574 | } |
| 575 | if (!monitor_cur_is_qmp() && qdev_device_help(opts)) { |
| 576 | qemu_opts_del(opts); |
| 577 | return 0; |
| 578 | } |
| 579 | if (!qdev_device_add(opts)) { |
| 580 | qemu_opts_del(opts); |
| 581 | return -1; |
| 582 | } |
| 583 | return 0; |
| 584 | } |
| 585 | |
Luiz Capitulino | a15fef2 | 2012-03-29 12:38:50 -0300 | [diff] [blame] | 586 | void qmp_device_del(const char *id, Error **errp) |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 587 | { |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 588 | DeviceState *dev; |
| 589 | |
| 590 | dev = qdev_find_recursive(sysbus_get_default(), id); |
| 591 | if (NULL == dev) { |
Luiz Capitulino | a15fef2 | 2012-03-29 12:38:50 -0300 | [diff] [blame] | 592 | error_set(errp, QERR_DEVICE_NOT_FOUND, id); |
| 593 | return; |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 594 | } |
Luiz Capitulino | 56f9107 | 2012-03-14 17:37:38 -0300 | [diff] [blame] | 595 | |
Luiz Capitulino | a15fef2 | 2012-03-29 12:38:50 -0300 | [diff] [blame] | 596 | qdev_unplug(dev, errp); |
Anthony Liguori | ee46d8a | 2011-12-22 15:24:20 -0600 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | void qdev_machine_init(void) |
| 600 | { |
| 601 | qdev_get_peripheral_anon(); |
| 602 | qdev_get_peripheral(); |
| 603 | } |