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 /* 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 28 #include "hw/qdev.h" 29 #include "sysemu/sysemu.h" 30 #include "qapi/error.h" 31 #include "qapi/qmp/qerror.h" 32 #include "qapi/visitor.h" 33 #include "qapi/qmp/qjson.h" 34 #include "monitor/monitor.h" 35 #include "hw/hotplug.h" 36 37 int qdev_hotplug = 0; 38 static bool qdev_hot_added = false; 39 static bool qdev_hot_removed = false; 40 41 const VMStateDescription *qdev_get_vmsd(DeviceState *dev) 42 { 43 DeviceClass *dc = DEVICE_GET_CLASS(dev); 44 return dc->vmsd; 45 } 46 47 const char *qdev_fw_name(DeviceState *dev) 48 { 49 DeviceClass *dc = DEVICE_GET_CLASS(dev); 50 51 if (dc->fw_name) { 52 return dc->fw_name; 53 } 54 55 return object_get_typename(OBJECT(dev)); 56 } 57 58 static void qdev_property_add_legacy(DeviceState *dev, Property *prop, 59 Error **errp); 60 61 static void bus_remove_child(BusState *bus, DeviceState *child) 62 { 63 BusChild *kid; 64 65 QTAILQ_FOREACH(kid, &bus->children, sibling) { 66 if (kid->child == child) { 67 char name[32]; 68 69 snprintf(name, sizeof(name), "child[%d]", kid->index); 70 QTAILQ_REMOVE(&bus->children, kid, sibling); 71 72 /* This gives back ownership of kid->child back to us. */ 73 object_property_del(OBJECT(bus), name, NULL); 74 object_unref(OBJECT(kid->child)); 75 g_free(kid); 76 return; 77 } 78 } 79 } 80 81 static void bus_add_child(BusState *bus, DeviceState *child) 82 { 83 char name[32]; 84 BusChild *kid = g_malloc0(sizeof(*kid)); 85 86 if (qdev_hotplug) { 87 assert(bus->allow_hotplug); 88 } 89 90 kid->index = bus->max_index++; 91 kid->child = child; 92 object_ref(OBJECT(kid->child)); 93 94 QTAILQ_INSERT_HEAD(&bus->children, kid, sibling); 95 96 /* This transfers ownership of kid->child to the property. */ 97 snprintf(name, sizeof(name), "child[%d]", kid->index); 98 object_property_add_link(OBJECT(bus), name, 99 object_get_typename(OBJECT(child)), 100 (Object **)&kid->child, 101 NULL); 102 } 103 104 void qdev_set_parent_bus(DeviceState *dev, BusState *bus) 105 { 106 dev->parent_bus = bus; 107 object_ref(OBJECT(bus)); 108 bus_add_child(bus, dev); 109 } 110 111 /* Create a new device. This only initializes the device state structure 112 and allows properties to be set. qdev_init should be called to 113 initialize the actual device emulation. */ 114 DeviceState *qdev_create(BusState *bus, const char *name) 115 { 116 DeviceState *dev; 117 118 dev = qdev_try_create(bus, name); 119 if (!dev) { 120 if (bus) { 121 error_report("Unknown device '%s' for bus '%s'", name, 122 object_get_typename(OBJECT(bus))); 123 } else { 124 error_report("Unknown device '%s' for default sysbus", name); 125 } 126 abort(); 127 } 128 129 return dev; 130 } 131 132 DeviceState *qdev_try_create(BusState *bus, const char *type) 133 { 134 DeviceState *dev; 135 136 if (object_class_by_name(type) == NULL) { 137 return NULL; 138 } 139 dev = DEVICE(object_new(type)); 140 if (!dev) { 141 return NULL; 142 } 143 144 if (!bus) { 145 bus = sysbus_get_default(); 146 } 147 148 qdev_set_parent_bus(dev, bus); 149 object_unref(OBJECT(dev)); 150 return dev; 151 } 152 153 /* Initialize a device. Device properties should be set before calling 154 this function. IRQs and MMIO regions should be connected/mapped after 155 calling this function. 156 On failure, destroy the device and return negative value. 157 Return 0 on success. */ 158 int qdev_init(DeviceState *dev) 159 { 160 Error *local_err = NULL; 161 162 assert(!dev->realized); 163 164 object_property_set_bool(OBJECT(dev), true, "realized", &local_err); 165 if (local_err != NULL) { 166 qerror_report_err(local_err); 167 error_free(local_err); 168 object_unparent(OBJECT(dev)); 169 return -1; 170 } 171 return 0; 172 } 173 174 static void device_realize(DeviceState *dev, Error **err) 175 { 176 DeviceClass *dc = DEVICE_GET_CLASS(dev); 177 178 if (dc->init) { 179 int rc = dc->init(dev); 180 if (rc < 0) { 181 error_setg(err, "Device initialization failed."); 182 return; 183 } 184 } 185 } 186 187 static void device_unrealize(DeviceState *dev, Error **errp) 188 { 189 DeviceClass *dc = DEVICE_GET_CLASS(dev); 190 191 if (dc->exit) { 192 int rc = dc->exit(dev); 193 if (rc < 0) { 194 error_setg(errp, "Device exit failed."); 195 return; 196 } 197 } 198 } 199 200 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id, 201 int required_for_version) 202 { 203 assert(!dev->realized); 204 dev->instance_id_alias = alias_id; 205 dev->alias_required_for_version = required_for_version; 206 } 207 208 void qdev_unplug(DeviceState *dev, Error **errp) 209 { 210 DeviceClass *dc = DEVICE_GET_CLASS(dev); 211 212 if (dev->parent_bus && !dev->parent_bus->allow_hotplug) { 213 error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name); 214 return; 215 } 216 217 if (!dc->hotpluggable) { 218 error_set(errp, QERR_DEVICE_NO_HOTPLUG, 219 object_get_typename(OBJECT(dev))); 220 return; 221 } 222 223 qdev_hot_removed = true; 224 225 if (dev->parent_bus && dev->parent_bus->hotplug_handler) { 226 hotplug_handler_unplug(dev->parent_bus->hotplug_handler, dev, errp); 227 } else { 228 assert(dc->unplug != NULL); 229 if (dc->unplug(dev) < 0) { /* legacy handler */ 230 error_set(errp, QERR_UNDEFINED_ERROR); 231 } 232 } 233 } 234 235 static int qdev_reset_one(DeviceState *dev, void *opaque) 236 { 237 device_reset(dev); 238 239 return 0; 240 } 241 242 static int qbus_reset_one(BusState *bus, void *opaque) 243 { 244 BusClass *bc = BUS_GET_CLASS(bus); 245 if (bc->reset) { 246 bc->reset(bus); 247 } 248 return 0; 249 } 250 251 void qdev_reset_all(DeviceState *dev) 252 { 253 qdev_walk_children(dev, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL); 254 } 255 256 void qbus_reset_all(BusState *bus) 257 { 258 qbus_walk_children(bus, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL); 259 } 260 261 void qbus_reset_all_fn(void *opaque) 262 { 263 BusState *bus = opaque; 264 qbus_reset_all(bus); 265 } 266 267 /* can be used as ->unplug() callback for the simple cases */ 268 int qdev_simple_unplug_cb(DeviceState *dev) 269 { 270 /* just zap it */ 271 object_unparent(OBJECT(dev)); 272 return 0; 273 } 274 275 276 /* Like qdev_init(), but terminate program via error_report() instead of 277 returning an error value. This is okay during machine creation. 278 Don't use for hotplug, because there callers need to recover from 279 failure. Exception: if you know the device's init() callback can't 280 fail, then qdev_init_nofail() can't fail either, and is therefore 281 usable even then. But relying on the device implementation that 282 way is somewhat unclean, and best avoided. */ 283 void qdev_init_nofail(DeviceState *dev) 284 { 285 const char *typename = object_get_typename(OBJECT(dev)); 286 287 if (qdev_init(dev) < 0) { 288 error_report("Initialization of device %s failed", typename); 289 exit(1); 290 } 291 } 292 293 void qdev_machine_creation_done(void) 294 { 295 /* 296 * ok, initial machine setup is done, starting from now we can 297 * only create hotpluggable devices 298 */ 299 qdev_hotplug = 1; 300 } 301 302 bool qdev_machine_modified(void) 303 { 304 return qdev_hot_added || qdev_hot_removed; 305 } 306 307 BusState *qdev_get_parent_bus(DeviceState *dev) 308 { 309 return dev->parent_bus; 310 } 311 312 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n) 313 { 314 dev->gpio_in = qemu_extend_irqs(dev->gpio_in, dev->num_gpio_in, handler, 315 dev, n); 316 dev->num_gpio_in += n; 317 } 318 319 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n) 320 { 321 assert(dev->num_gpio_out == 0); 322 dev->num_gpio_out = n; 323 dev->gpio_out = pins; 324 } 325 326 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n) 327 { 328 assert(n >= 0 && n < dev->num_gpio_in); 329 return dev->gpio_in[n]; 330 } 331 332 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin) 333 { 334 assert(n >= 0 && n < dev->num_gpio_out); 335 dev->gpio_out[n] = pin; 336 } 337 338 BusState *qdev_get_child_bus(DeviceState *dev, const char *name) 339 { 340 BusState *bus; 341 342 QLIST_FOREACH(bus, &dev->child_bus, sibling) { 343 if (strcmp(name, bus->name) == 0) { 344 return bus; 345 } 346 } 347 return NULL; 348 } 349 350 int qbus_walk_children(BusState *bus, 351 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn, 352 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn, 353 void *opaque) 354 { 355 BusChild *kid; 356 int err; 357 358 if (pre_busfn) { 359 err = pre_busfn(bus, opaque); 360 if (err) { 361 return err; 362 } 363 } 364 365 QTAILQ_FOREACH(kid, &bus->children, sibling) { 366 err = qdev_walk_children(kid->child, 367 pre_devfn, pre_busfn, 368 post_devfn, post_busfn, opaque); 369 if (err < 0) { 370 return err; 371 } 372 } 373 374 if (post_busfn) { 375 err = post_busfn(bus, opaque); 376 if (err) { 377 return err; 378 } 379 } 380 381 return 0; 382 } 383 384 int qdev_walk_children(DeviceState *dev, 385 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn, 386 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn, 387 void *opaque) 388 { 389 BusState *bus; 390 int err; 391 392 if (pre_devfn) { 393 err = pre_devfn(dev, opaque); 394 if (err) { 395 return err; 396 } 397 } 398 399 QLIST_FOREACH(bus, &dev->child_bus, sibling) { 400 err = qbus_walk_children(bus, pre_devfn, pre_busfn, 401 post_devfn, post_busfn, opaque); 402 if (err < 0) { 403 return err; 404 } 405 } 406 407 if (post_devfn) { 408 err = post_devfn(dev, opaque); 409 if (err) { 410 return err; 411 } 412 } 413 414 return 0; 415 } 416 417 DeviceState *qdev_find_recursive(BusState *bus, const char *id) 418 { 419 BusChild *kid; 420 DeviceState *ret; 421 BusState *child; 422 423 QTAILQ_FOREACH(kid, &bus->children, sibling) { 424 DeviceState *dev = kid->child; 425 426 if (dev->id && strcmp(dev->id, id) == 0) { 427 return dev; 428 } 429 430 QLIST_FOREACH(child, &dev->child_bus, sibling) { 431 ret = qdev_find_recursive(child, id); 432 if (ret) { 433 return ret; 434 } 435 } 436 } 437 return NULL; 438 } 439 440 static void qbus_realize(BusState *bus, DeviceState *parent, const char *name) 441 { 442 const char *typename = object_get_typename(OBJECT(bus)); 443 char *buf; 444 int i,len; 445 446 bus->parent = parent; 447 448 if (name) { 449 bus->name = g_strdup(name); 450 } else if (bus->parent && bus->parent->id) { 451 /* parent device has id -> use it for bus name */ 452 len = strlen(bus->parent->id) + 16; 453 buf = g_malloc(len); 454 snprintf(buf, len, "%s.%d", bus->parent->id, bus->parent->num_child_bus); 455 bus->name = buf; 456 } else { 457 /* no id -> use lowercase bus type for bus name */ 458 len = strlen(typename) + 16; 459 buf = g_malloc(len); 460 len = snprintf(buf, len, "%s.%d", typename, 461 bus->parent ? bus->parent->num_child_bus : 0); 462 for (i = 0; i < len; i++) 463 buf[i] = qemu_tolower(buf[i]); 464 bus->name = buf; 465 } 466 467 if (bus->parent) { 468 QLIST_INSERT_HEAD(&bus->parent->child_bus, bus, sibling); 469 bus->parent->num_child_bus++; 470 object_property_add_child(OBJECT(bus->parent), bus->name, OBJECT(bus), NULL); 471 object_unref(OBJECT(bus)); 472 } else if (bus != sysbus_get_default()) { 473 /* TODO: once all bus devices are qdevified, 474 only reset handler for main_system_bus should be registered here. */ 475 qemu_register_reset(qbus_reset_all_fn, bus); 476 } 477 } 478 479 static void bus_unparent(Object *obj) 480 { 481 BusState *bus = BUS(obj); 482 BusChild *kid; 483 484 while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) { 485 DeviceState *dev = kid->child; 486 object_unparent(OBJECT(dev)); 487 } 488 if (bus->parent) { 489 QLIST_REMOVE(bus, sibling); 490 bus->parent->num_child_bus--; 491 bus->parent = NULL; 492 } else { 493 assert(bus != sysbus_get_default()); /* main_system_bus is never freed */ 494 qemu_unregister_reset(qbus_reset_all_fn, bus); 495 } 496 } 497 498 void qbus_create_inplace(void *bus, size_t size, const char *typename, 499 DeviceState *parent, const char *name) 500 { 501 object_initialize(bus, size, typename); 502 qbus_realize(bus, parent, name); 503 } 504 505 BusState *qbus_create(const char *typename, DeviceState *parent, const char *name) 506 { 507 BusState *bus; 508 509 bus = BUS(object_new(typename)); 510 qbus_realize(bus, parent, name); 511 512 return bus; 513 } 514 515 static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev) 516 { 517 BusClass *bc = BUS_GET_CLASS(bus); 518 519 if (bc->get_fw_dev_path) { 520 return bc->get_fw_dev_path(dev); 521 } 522 523 return NULL; 524 } 525 526 static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size) 527 { 528 int l = 0; 529 530 if (dev && dev->parent_bus) { 531 char *d; 532 l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size); 533 d = bus_get_fw_dev_path(dev->parent_bus, dev); 534 if (d) { 535 l += snprintf(p + l, size - l, "%s", d); 536 g_free(d); 537 } else { 538 return l; 539 } 540 } 541 l += snprintf(p + l , size - l, "/"); 542 543 return l; 544 } 545 546 char* qdev_get_fw_dev_path(DeviceState *dev) 547 { 548 char path[128]; 549 int l; 550 551 l = qdev_get_fw_dev_path_helper(dev, path, 128); 552 553 path[l-1] = '\0'; 554 555 return g_strdup(path); 556 } 557 558 char *qdev_get_dev_path(DeviceState *dev) 559 { 560 BusClass *bc; 561 562 if (!dev || !dev->parent_bus) { 563 return NULL; 564 } 565 566 bc = BUS_GET_CLASS(dev->parent_bus); 567 if (bc->get_dev_path) { 568 return bc->get_dev_path(dev); 569 } 570 571 return NULL; 572 } 573 574 /** 575 * Legacy property handling 576 */ 577 578 static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque, 579 const char *name, Error **errp) 580 { 581 DeviceState *dev = DEVICE(obj); 582 Property *prop = opaque; 583 584 char buffer[1024]; 585 char *ptr = buffer; 586 587 prop->info->print(dev, prop, buffer, sizeof(buffer)); 588 visit_type_str(v, &ptr, name, errp); 589 } 590 591 static void qdev_set_legacy_property(Object *obj, Visitor *v, void *opaque, 592 const char *name, Error **errp) 593 { 594 DeviceState *dev = DEVICE(obj); 595 Property *prop = opaque; 596 Error *local_err = NULL; 597 char *ptr = NULL; 598 int ret; 599 600 if (dev->realized) { 601 qdev_prop_set_after_realize(dev, name, errp); 602 return; 603 } 604 605 visit_type_str(v, &ptr, name, &local_err); 606 if (local_err) { 607 error_propagate(errp, local_err); 608 return; 609 } 610 611 ret = prop->info->parse(dev, prop, ptr); 612 error_set_from_qdev_prop_error(errp, ret, dev, prop, ptr); 613 g_free(ptr); 614 } 615 616 /** 617 * @qdev_add_legacy_property - adds a legacy property 618 * 619 * Do not use this is new code! Properties added through this interface will 620 * be given names and types in the "legacy" namespace. 621 * 622 * Legacy properties are string versions of other OOM properties. The format 623 * of the string depends on the property type. 624 */ 625 void qdev_property_add_legacy(DeviceState *dev, Property *prop, 626 Error **errp) 627 { 628 gchar *name, *type; 629 630 /* Register pointer properties as legacy properties */ 631 if (!prop->info->print && !prop->info->parse && 632 (prop->info->set || prop->info->get)) { 633 return; 634 } 635 636 name = g_strdup_printf("legacy-%s", prop->name); 637 type = g_strdup_printf("legacy<%s>", 638 prop->info->legacy_name ?: prop->info->name); 639 640 object_property_add(OBJECT(dev), name, type, 641 prop->info->print ? qdev_get_legacy_property : prop->info->get, 642 prop->info->parse ? qdev_set_legacy_property : prop->info->set, 643 NULL, 644 prop, errp); 645 646 g_free(type); 647 g_free(name); 648 } 649 650 /** 651 * @qdev_property_add_static - add a @Property to a device. 652 * 653 * Static properties access data in a struct. The actual type of the 654 * property and the field depends on the property type. 655 */ 656 void qdev_property_add_static(DeviceState *dev, Property *prop, 657 Error **errp) 658 { 659 Error *local_err = NULL; 660 Object *obj = OBJECT(dev); 661 662 /* 663 * TODO qdev_prop_ptr does not have getters or setters. It must 664 * go now that it can be replaced with links. The test should be 665 * removed along with it: all static properties are read/write. 666 */ 667 if (!prop->info->get && !prop->info->set) { 668 return; 669 } 670 671 object_property_add(obj, prop->name, prop->info->name, 672 prop->info->get, prop->info->set, 673 prop->info->release, 674 prop, &local_err); 675 676 if (local_err) { 677 error_propagate(errp, local_err); 678 return; 679 } 680 if (prop->qtype == QTYPE_NONE) { 681 return; 682 } 683 684 if (prop->qtype == QTYPE_QBOOL) { 685 object_property_set_bool(obj, prop->defval, prop->name, &error_abort); 686 } else if (prop->info->enum_table) { 687 object_property_set_str(obj, prop->info->enum_table[prop->defval], 688 prop->name, &error_abort); 689 } else if (prop->qtype == QTYPE_QINT) { 690 object_property_set_int(obj, prop->defval, prop->name, &error_abort); 691 } 692 } 693 694 static bool device_get_realized(Object *obj, Error **err) 695 { 696 DeviceState *dev = DEVICE(obj); 697 return dev->realized; 698 } 699 700 static void device_set_realized(Object *obj, bool value, Error **err) 701 { 702 DeviceState *dev = DEVICE(obj); 703 DeviceClass *dc = DEVICE_GET_CLASS(dev); 704 Error *local_err = NULL; 705 706 if (dev->hotplugged && !dc->hotpluggable) { 707 error_set(err, QERR_DEVICE_NO_HOTPLUG, object_get_typename(obj)); 708 return; 709 } 710 711 if (value && !dev->realized) { 712 if (!obj->parent && local_err == NULL) { 713 static int unattached_count; 714 gchar *name = g_strdup_printf("device[%d]", unattached_count++); 715 716 object_property_add_child(container_get(qdev_get_machine(), 717 "/unattached"), 718 name, obj, &local_err); 719 g_free(name); 720 } 721 722 if (dc->realize) { 723 dc->realize(dev, &local_err); 724 } 725 726 if (dev->parent_bus && dev->parent_bus->hotplug_handler && 727 local_err == NULL) { 728 hotplug_handler_plug(dev->parent_bus->hotplug_handler, 729 dev, &local_err); 730 } 731 732 if (qdev_get_vmsd(dev) && local_err == NULL) { 733 vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev, 734 dev->instance_id_alias, 735 dev->alias_required_for_version); 736 } 737 if (dev->hotplugged && local_err == NULL) { 738 device_reset(dev); 739 } 740 } else if (!value && dev->realized) { 741 if (qdev_get_vmsd(dev)) { 742 vmstate_unregister(dev, qdev_get_vmsd(dev), dev); 743 } 744 if (dc->unrealize) { 745 dc->unrealize(dev, &local_err); 746 } 747 } 748 749 if (local_err != NULL) { 750 error_propagate(err, local_err); 751 return; 752 } 753 754 dev->realized = value; 755 } 756 757 static bool device_get_hotpluggable(Object *obj, Error **err) 758 { 759 DeviceClass *dc = DEVICE_GET_CLASS(obj); 760 DeviceState *dev = DEVICE(obj); 761 762 return dc->hotpluggable && dev->parent_bus->allow_hotplug; 763 } 764 765 static void device_initfn(Object *obj) 766 { 767 DeviceState *dev = DEVICE(obj); 768 ObjectClass *class; 769 Property *prop; 770 771 if (qdev_hotplug) { 772 dev->hotplugged = 1; 773 qdev_hot_added = true; 774 } 775 776 dev->instance_id_alias = -1; 777 dev->realized = false; 778 779 object_property_add_bool(obj, "realized", 780 device_get_realized, device_set_realized, NULL); 781 object_property_add_bool(obj, "hotpluggable", 782 device_get_hotpluggable, NULL, NULL); 783 784 class = object_get_class(OBJECT(dev)); 785 do { 786 for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) { 787 qdev_property_add_legacy(dev, prop, &error_abort); 788 qdev_property_add_static(dev, prop, &error_abort); 789 } 790 class = object_class_get_parent(class); 791 } while (class != object_class_by_name(TYPE_DEVICE)); 792 793 object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS, 794 (Object **)&dev->parent_bus, &error_abort); 795 } 796 797 static void device_post_init(Object *obj) 798 { 799 qdev_prop_set_globals(DEVICE(obj), &error_abort); 800 } 801 802 /* Unlink device from bus and free the structure. */ 803 static void device_finalize(Object *obj) 804 { 805 DeviceState *dev = DEVICE(obj); 806 if (dev->opts) { 807 qemu_opts_del(dev->opts); 808 } 809 } 810 811 static void device_class_base_init(ObjectClass *class, void *data) 812 { 813 DeviceClass *klass = DEVICE_CLASS(class); 814 815 /* We explicitly look up properties in the superclasses, 816 * so do not propagate them to the subclasses. 817 */ 818 klass->props = NULL; 819 820 /* by default all devices were considered as hotpluggable, 821 * so with intent to check it in generic qdev_unplug() / 822 * device_set_realized() functions make every device 823 * hotpluggable. Devices that shouldn't be hotpluggable, 824 * should override it in their class_init() 825 */ 826 klass->hotpluggable = true; 827 } 828 829 static void device_unparent(Object *obj) 830 { 831 DeviceState *dev = DEVICE(obj); 832 BusState *bus; 833 QObject *event_data; 834 bool have_realized = dev->realized; 835 836 while (dev->num_child_bus) { 837 bus = QLIST_FIRST(&dev->child_bus); 838 object_unparent(OBJECT(bus)); 839 } 840 if (dev->realized) { 841 object_property_set_bool(obj, false, "realized", NULL); 842 } 843 if (dev->parent_bus) { 844 bus_remove_child(dev->parent_bus, dev); 845 object_unref(OBJECT(dev->parent_bus)); 846 dev->parent_bus = NULL; 847 } 848 849 /* Only send event if the device had been completely realized */ 850 if (have_realized) { 851 gchar *path = object_get_canonical_path(OBJECT(dev)); 852 853 if (dev->id) { 854 event_data = qobject_from_jsonf("{ 'device': %s, 'path': %s }", 855 dev->id, path); 856 } else { 857 event_data = qobject_from_jsonf("{ 'path': %s }", path); 858 } 859 monitor_protocol_event(QEVENT_DEVICE_DELETED, event_data); 860 qobject_decref(event_data); 861 g_free(path); 862 } 863 } 864 865 static void device_class_init(ObjectClass *class, void *data) 866 { 867 DeviceClass *dc = DEVICE_CLASS(class); 868 869 class->unparent = device_unparent; 870 dc->realize = device_realize; 871 dc->unrealize = device_unrealize; 872 } 873 874 void device_reset(DeviceState *dev) 875 { 876 DeviceClass *klass = DEVICE_GET_CLASS(dev); 877 878 if (klass->reset) { 879 klass->reset(dev); 880 } 881 } 882 883 Object *qdev_get_machine(void) 884 { 885 static Object *dev; 886 887 if (dev == NULL) { 888 dev = container_get(object_get_root(), "/machine"); 889 } 890 891 return dev; 892 } 893 894 static const TypeInfo device_type_info = { 895 .name = TYPE_DEVICE, 896 .parent = TYPE_OBJECT, 897 .instance_size = sizeof(DeviceState), 898 .instance_init = device_initfn, 899 .instance_post_init = device_post_init, 900 .instance_finalize = device_finalize, 901 .class_base_init = device_class_base_init, 902 .class_init = device_class_init, 903 .abstract = true, 904 .class_size = sizeof(DeviceClass), 905 }; 906 907 static void qbus_initfn(Object *obj) 908 { 909 BusState *bus = BUS(obj); 910 911 QTAILQ_INIT(&bus->children); 912 object_property_add_link(obj, QDEV_HOTPLUG_HANDLER_PROPERTY, 913 TYPE_HOTPLUG_HANDLER, 914 (Object **)&bus->hotplug_handler, NULL); 915 } 916 917 static char *default_bus_get_fw_dev_path(DeviceState *dev) 918 { 919 return g_strdup(object_get_typename(OBJECT(dev))); 920 } 921 922 static void bus_class_init(ObjectClass *class, void *data) 923 { 924 BusClass *bc = BUS_CLASS(class); 925 926 class->unparent = bus_unparent; 927 bc->get_fw_dev_path = default_bus_get_fw_dev_path; 928 } 929 930 static void qbus_finalize(Object *obj) 931 { 932 BusState *bus = BUS(obj); 933 934 g_free((char *)bus->name); 935 } 936 937 static const TypeInfo bus_info = { 938 .name = TYPE_BUS, 939 .parent = TYPE_OBJECT, 940 .instance_size = sizeof(BusState), 941 .abstract = true, 942 .class_size = sizeof(BusClass), 943 .instance_init = qbus_initfn, 944 .instance_finalize = qbus_finalize, 945 .class_init = bus_class_init, 946 }; 947 948 static void qdev_register_types(void) 949 { 950 type_register_static(&bus_info); 951 type_register_static(&device_type_info); 952 } 953 954 type_init(qdev_register_types) 955