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