PPC: e500mc: add missing IVORs to bitmap
[qemu.git] / hw / qdev.c
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 "net.h"
29 #include "qdev.h"
30 #include "sysemu.h"
31 #include "monitor.h"
32
33 static int qdev_hotplug = 0;
34 static bool qdev_hot_added = false;
35 static bool qdev_hot_removed = false;
36
37 /* This is a nasty hack to allow passing a NULL bus to qdev_create. */
38 static BusState *main_system_bus;
39 static void main_system_bus_create(void);
40
41 DeviceInfo *device_info_list;
42
43 static BusState *qbus_find_recursive(BusState *bus, const char *name,
44 const BusInfo *info);
45 static BusState *qbus_find(const char *path);
46
47 /* Register a new device type. */
48 static void qdev_subclass_init(ObjectClass *klass, void *data)
49 {
50 DeviceClass *dc = DEVICE_CLASS(klass);
51
52 dc->info = data;
53 dc->reset = dc->info->reset;
54
55 /* Poison to try to detect future uses */
56 dc->info->reset = NULL;
57
58 if (dc->info->class_init) {
59 dc->info->class_init(klass, data);
60 }
61 }
62
63 DeviceInfo *qdev_get_info(DeviceState *dev)
64 {
65 return DEVICE_GET_CLASS(dev)->info;
66 }
67
68 void qdev_register_subclass(DeviceInfo *info, const char *parent)
69 {
70 TypeInfo type_info = {};
71
72 assert(info->size >= sizeof(DeviceState));
73 assert(!info->next);
74
75 type_info.name = info->name;
76 type_info.parent = parent;
77 type_info.instance_size = info->size;
78 type_info.class_init = qdev_subclass_init;
79 type_info.class_data = info;
80
81 type_register_static(&type_info);
82
83 info->next = device_info_list;
84 device_info_list = info;
85 }
86
87 void qdev_register(DeviceInfo *info)
88 {
89 qdev_register_subclass(info, TYPE_DEVICE);
90 }
91
92 static DeviceInfo *qdev_find_info(BusInfo *bus_info, const char *name)
93 {
94 DeviceInfo *info;
95
96 /* first check device names */
97 for (info = device_info_list; info != NULL; info = info->next) {
98 if (bus_info && info->bus_info != bus_info)
99 continue;
100 if (strcmp(info->name, name) != 0)
101 continue;
102 return info;
103 }
104
105 /* failing that check the aliases */
106 for (info = device_info_list; info != NULL; info = info->next) {
107 if (bus_info && info->bus_info != bus_info)
108 continue;
109 if (!info->alias)
110 continue;
111 if (strcmp(info->alias, name) != 0)
112 continue;
113 return info;
114 }
115 return NULL;
116 }
117
118 bool qdev_exists(const char *name)
119 {
120 return !!qdev_find_info(NULL, name);
121 }
122
123 static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
124 Error **errp);
125
126 static DeviceState *qdev_create_from_info(BusState *bus, DeviceInfo *info)
127 {
128 DeviceState *dev;
129 Property *prop;
130
131 assert(bus->info == info->bus_info);
132 dev = DEVICE(object_new(info->name));
133 dev->parent_bus = bus;
134 qdev_prop_set_defaults(dev, qdev_get_info(dev)->props);
135 qdev_prop_set_defaults(dev, dev->parent_bus->info->props);
136 qdev_prop_set_globals(dev);
137 QTAILQ_INSERT_HEAD(&bus->children, dev, sibling);
138 if (qdev_hotplug) {
139 assert(bus->allow_hotplug);
140 dev->hotplugged = 1;
141 qdev_hot_added = true;
142 }
143 dev->instance_id_alias = -1;
144 QTAILQ_INIT(&dev->properties);
145 dev->state = DEV_STATE_CREATED;
146
147 for (prop = qdev_get_info(dev)->props; prop && prop->name; prop++) {
148 qdev_property_add_legacy(dev, prop, NULL);
149 qdev_property_add_static(dev, prop, NULL);
150 }
151
152 for (prop = qdev_get_info(dev)->bus_info->props; prop && prop->name; prop++) {
153 qdev_property_add_legacy(dev, prop, NULL);
154 qdev_property_add_static(dev, prop, NULL);
155 }
156
157 qdev_property_add_str(dev, "type", qdev_get_type, NULL, NULL);
158
159 return dev;
160 }
161
162 /* Create a new device. This only initializes the device state structure
163 and allows properties to be set. qdev_init should be called to
164 initialize the actual device emulation. */
165 DeviceState *qdev_create(BusState *bus, const char *name)
166 {
167 DeviceState *dev;
168
169 dev = qdev_try_create(bus, name);
170 if (!dev) {
171 if (bus) {
172 hw_error("Unknown device '%s' for bus '%s'\n", name,
173 bus->info->name);
174 } else {
175 hw_error("Unknown device '%s' for default sysbus\n", name);
176 }
177 }
178
179 return dev;
180 }
181
182 DeviceState *qdev_try_create(BusState *bus, const char *name)
183 {
184 DeviceInfo *info;
185
186 if (!bus) {
187 bus = sysbus_get_default();
188 }
189
190 info = qdev_find_info(bus->info, name);
191 if (!info) {
192 return NULL;
193 }
194
195 return qdev_create_from_info(bus, info);
196 }
197
198 static void qdev_print_devinfo(DeviceInfo *info)
199 {
200 error_printf("name \"%s\", bus %s",
201 info->name, info->bus_info->name);
202 if (info->alias) {
203 error_printf(", alias \"%s\"", info->alias);
204 }
205 if (info->desc) {
206 error_printf(", desc \"%s\"", info->desc);
207 }
208 if (info->no_user) {
209 error_printf(", no-user");
210 }
211 error_printf("\n");
212 }
213
214 static int set_property(const char *name, const char *value, void *opaque)
215 {
216 DeviceState *dev = opaque;
217
218 if (strcmp(name, "driver") == 0)
219 return 0;
220 if (strcmp(name, "bus") == 0)
221 return 0;
222
223 if (qdev_prop_parse(dev, name, value) == -1) {
224 return -1;
225 }
226 return 0;
227 }
228
229 int qdev_device_help(QemuOpts *opts)
230 {
231 const char *driver;
232 DeviceInfo *info;
233 Property *prop;
234
235 driver = qemu_opt_get(opts, "driver");
236 if (driver && !strcmp(driver, "?")) {
237 for (info = device_info_list; info != NULL; info = info->next) {
238 if (info->no_user) {
239 continue; /* not available, don't show */
240 }
241 qdev_print_devinfo(info);
242 }
243 return 1;
244 }
245
246 if (!driver || !qemu_opt_get(opts, "?")) {
247 return 0;
248 }
249
250 info = qdev_find_info(NULL, driver);
251 if (!info) {
252 return 0;
253 }
254
255 for (prop = info->props; prop && prop->name; prop++) {
256 /*
257 * TODO Properties without a parser are just for dirty hacks.
258 * qdev_prop_ptr is the only such PropertyInfo. It's marked
259 * for removal. This conditional should be removed along with
260 * it.
261 */
262 if (!prop->info->parse) {
263 continue; /* no way to set it, don't show */
264 }
265 error_printf("%s.%s=%s\n", info->name, prop->name,
266 prop->info->legacy_name ?: prop->info->name);
267 }
268 for (prop = info->bus_info->props; prop && prop->name; prop++) {
269 if (!prop->info->parse) {
270 continue; /* no way to set it, don't show */
271 }
272 error_printf("%s.%s=%s\n", info->name, prop->name,
273 prop->info->legacy_name ?: prop->info->name);
274 }
275 return 1;
276 }
277
278 static DeviceState *qdev_get_peripheral(void)
279 {
280 static DeviceState *dev;
281
282 if (dev == NULL) {
283 dev = qdev_create(NULL, "container");
284 qdev_property_add_child(qdev_get_root(), "peripheral", dev, NULL);
285 qdev_init_nofail(dev);
286 }
287
288 return dev;
289 }
290
291 static DeviceState *qdev_get_peripheral_anon(void)
292 {
293 static DeviceState *dev;
294
295 if (dev == NULL) {
296 dev = qdev_create(NULL, "container");
297 qdev_property_add_child(qdev_get_root(), "peripheral-anon", dev, NULL);
298 qdev_init_nofail(dev);
299 }
300
301 return dev;
302 }
303
304 DeviceState *qdev_device_add(QemuOpts *opts)
305 {
306 const char *driver, *path, *id;
307 DeviceInfo *info;
308 DeviceState *qdev;
309 BusState *bus;
310
311 driver = qemu_opt_get(opts, "driver");
312 if (!driver) {
313 qerror_report(QERR_MISSING_PARAMETER, "driver");
314 return NULL;
315 }
316
317 /* find driver */
318 info = qdev_find_info(NULL, driver);
319 if (!info || info->no_user) {
320 qerror_report(QERR_INVALID_PARAMETER_VALUE, "driver", "a driver name");
321 error_printf_unless_qmp("Try with argument '?' for a list.\n");
322 return NULL;
323 }
324
325 /* find bus */
326 path = qemu_opt_get(opts, "bus");
327 if (path != NULL) {
328 bus = qbus_find(path);
329 if (!bus) {
330 return NULL;
331 }
332 if (bus->info != info->bus_info) {
333 qerror_report(QERR_BAD_BUS_FOR_DEVICE,
334 driver, bus->info->name);
335 return NULL;
336 }
337 } else {
338 bus = qbus_find_recursive(main_system_bus, NULL, info->bus_info);
339 if (!bus) {
340 qerror_report(QERR_NO_BUS_FOR_DEVICE,
341 info->name, info->bus_info->name);
342 return NULL;
343 }
344 }
345 if (qdev_hotplug && !bus->allow_hotplug) {
346 qerror_report(QERR_BUS_NO_HOTPLUG, bus->name);
347 return NULL;
348 }
349
350 /* create device, set properties */
351 qdev = qdev_create_from_info(bus, info);
352 id = qemu_opts_id(opts);
353 if (id) {
354 qdev->id = id;
355 qdev_property_add_child(qdev_get_peripheral(), qdev->id, qdev, NULL);
356 } else {
357 static int anon_count;
358 gchar *name = g_strdup_printf("device[%d]", anon_count++);
359 qdev_property_add_child(qdev_get_peripheral_anon(), name,
360 qdev, NULL);
361 g_free(name);
362 }
363 if (qemu_opt_foreach(opts, set_property, qdev, 1) != 0) {
364 qdev_free(qdev);
365 return NULL;
366 }
367 if (qdev_init(qdev) < 0) {
368 qerror_report(QERR_DEVICE_INIT_FAILED, driver);
369 return NULL;
370 }
371 qdev->opts = opts;
372 return qdev;
373 }
374
375 /* Initialize a device. Device properties should be set before calling
376 this function. IRQs and MMIO regions should be connected/mapped after
377 calling this function.
378 On failure, destroy the device and return negative value.
379 Return 0 on success. */
380 int qdev_init(DeviceState *dev)
381 {
382 int rc;
383
384 assert(dev->state == DEV_STATE_CREATED);
385 rc = qdev_get_info(dev)->init(dev, qdev_get_info(dev));
386 if (rc < 0) {
387 qdev_free(dev);
388 return rc;
389 }
390 if (qdev_get_info(dev)->vmsd) {
391 vmstate_register_with_alias_id(dev, -1, qdev_get_info(dev)->vmsd, dev,
392 dev->instance_id_alias,
393 dev->alias_required_for_version);
394 }
395 dev->state = DEV_STATE_INITIALIZED;
396 if (dev->hotplugged) {
397 device_reset(dev);
398 }
399 return 0;
400 }
401
402 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
403 int required_for_version)
404 {
405 assert(dev->state == DEV_STATE_CREATED);
406 dev->instance_id_alias = alias_id;
407 dev->alias_required_for_version = required_for_version;
408 }
409
410 int qdev_unplug(DeviceState *dev)
411 {
412 if (!dev->parent_bus->allow_hotplug) {
413 qerror_report(QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
414 return -1;
415 }
416 assert(qdev_get_info(dev)->unplug != NULL);
417
418 qdev_hot_removed = true;
419
420 return qdev_get_info(dev)->unplug(dev);
421 }
422
423 static int qdev_reset_one(DeviceState *dev, void *opaque)
424 {
425 device_reset(dev);
426
427 return 0;
428 }
429
430 BusState *sysbus_get_default(void)
431 {
432 if (!main_system_bus) {
433 main_system_bus_create();
434 }
435 return main_system_bus;
436 }
437
438 static int qbus_reset_one(BusState *bus, void *opaque)
439 {
440 if (bus->info->reset) {
441 return bus->info->reset(bus);
442 }
443 return 0;
444 }
445
446 void qdev_reset_all(DeviceState *dev)
447 {
448 qdev_walk_children(dev, qdev_reset_one, qbus_reset_one, NULL);
449 }
450
451 void qbus_reset_all_fn(void *opaque)
452 {
453 BusState *bus = opaque;
454 qbus_walk_children(bus, qdev_reset_one, qbus_reset_one, NULL);
455 }
456
457 /* can be used as ->unplug() callback for the simple cases */
458 int qdev_simple_unplug_cb(DeviceState *dev)
459 {
460 /* just zap it */
461 qdev_free(dev);
462 return 0;
463 }
464
465
466 /* Like qdev_init(), but terminate program via error_report() instead of
467 returning an error value. This is okay during machine creation.
468 Don't use for hotplug, because there callers need to recover from
469 failure. Exception: if you know the device's init() callback can't
470 fail, then qdev_init_nofail() can't fail either, and is therefore
471 usable even then. But relying on the device implementation that
472 way is somewhat unclean, and best avoided. */
473 void qdev_init_nofail(DeviceState *dev)
474 {
475 DeviceInfo *info = qdev_get_info(dev);
476
477 if (qdev_init(dev) < 0) {
478 error_report("Initialization of device %s failed", info->name);
479 exit(1);
480 }
481 }
482
483 static void qdev_property_del_all(DeviceState *dev)
484 {
485 while (!QTAILQ_EMPTY(&dev->properties)) {
486 DeviceProperty *prop = QTAILQ_FIRST(&dev->properties);
487
488 QTAILQ_REMOVE(&dev->properties, prop, node);
489
490 if (prop->release) {
491 prop->release(dev, prop->name, prop->opaque);
492 }
493
494 g_free(prop->name);
495 g_free(prop->type);
496 g_free(prop);
497 }
498 }
499
500 static void qdev_property_del_child(DeviceState *dev, DeviceState *child, Error **errp)
501 {
502 DeviceProperty *prop;
503
504 QTAILQ_FOREACH(prop, &dev->properties, node) {
505 if (strstart(prop->type, "child<", NULL) && prop->opaque == child) {
506 break;
507 }
508 }
509
510 g_assert(prop != NULL);
511
512 QTAILQ_REMOVE(&dev->properties, prop, node);
513
514 if (prop->release) {
515 prop->release(dev, prop->name, prop->opaque);
516 }
517
518 g_free(prop->name);
519 g_free(prop->type);
520 g_free(prop);
521 }
522
523 /* Unlink device from bus and free the structure. */
524 void qdev_free(DeviceState *dev)
525 {
526 BusState *bus;
527 Property *prop;
528
529 qdev_property_del_all(dev);
530
531 if (dev->state == DEV_STATE_INITIALIZED) {
532 while (dev->num_child_bus) {
533 bus = QLIST_FIRST(&dev->child_bus);
534 qbus_free(bus);
535 }
536 if (qdev_get_info(dev)->vmsd)
537 vmstate_unregister(dev, qdev_get_info(dev)->vmsd, dev);
538 if (qdev_get_info(dev)->exit)
539 qdev_get_info(dev)->exit(dev);
540 if (dev->opts)
541 qemu_opts_del(dev->opts);
542 }
543 QTAILQ_REMOVE(&dev->parent_bus->children, dev, sibling);
544 for (prop = qdev_get_info(dev)->props; prop && prop->name; prop++) {
545 if (prop->info->free) {
546 prop->info->free(dev, prop);
547 }
548 }
549 if (dev->parent) {
550 qdev_property_del_child(dev->parent, dev, NULL);
551 }
552 if (dev->ref != 0) {
553 qerror_report(QERR_DEVICE_IN_USE, dev->id?:"");
554 }
555 object_delete(OBJECT(dev));
556 }
557
558 void qdev_machine_creation_done(void)
559 {
560 /*
561 * ok, initial machine setup is done, starting from now we can
562 * only create hotpluggable devices
563 */
564 qdev_hotplug = 1;
565 }
566
567 bool qdev_machine_modified(void)
568 {
569 return qdev_hot_added || qdev_hot_removed;
570 }
571
572 /* Get a character (serial) device interface. */
573 CharDriverState *qdev_init_chardev(DeviceState *dev)
574 {
575 static int next_serial;
576
577 /* FIXME: This function needs to go away: use chardev properties! */
578 return serial_hds[next_serial++];
579 }
580
581 BusState *qdev_get_parent_bus(DeviceState *dev)
582 {
583 return dev->parent_bus;
584 }
585
586 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
587 {
588 assert(dev->num_gpio_in == 0);
589 dev->num_gpio_in = n;
590 dev->gpio_in = qemu_allocate_irqs(handler, dev, n);
591 }
592
593 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
594 {
595 assert(dev->num_gpio_out == 0);
596 dev->num_gpio_out = n;
597 dev->gpio_out = pins;
598 }
599
600 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
601 {
602 assert(n >= 0 && n < dev->num_gpio_in);
603 return dev->gpio_in[n];
604 }
605
606 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
607 {
608 assert(n >= 0 && n < dev->num_gpio_out);
609 dev->gpio_out[n] = pin;
610 }
611
612 void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd)
613 {
614 qdev_prop_set_macaddr(dev, "mac", nd->macaddr.a);
615 if (nd->vlan)
616 qdev_prop_set_vlan(dev, "vlan", nd->vlan);
617 if (nd->netdev)
618 qdev_prop_set_netdev(dev, "netdev", nd->netdev);
619 if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
620 qdev_prop_exists(dev, "vectors")) {
621 qdev_prop_set_uint32(dev, "vectors", nd->nvectors);
622 }
623 nd->instantiated = 1;
624 }
625
626 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
627 {
628 BusState *bus;
629
630 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
631 if (strcmp(name, bus->name) == 0) {
632 return bus;
633 }
634 }
635 return NULL;
636 }
637
638 int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
639 qbus_walkerfn *busfn, void *opaque)
640 {
641 DeviceState *dev;
642 int err;
643
644 if (busfn) {
645 err = busfn(bus, opaque);
646 if (err) {
647 return err;
648 }
649 }
650
651 QTAILQ_FOREACH(dev, &bus->children, sibling) {
652 err = qdev_walk_children(dev, devfn, busfn, opaque);
653 if (err < 0) {
654 return err;
655 }
656 }
657
658 return 0;
659 }
660
661 int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
662 qbus_walkerfn *busfn, void *opaque)
663 {
664 BusState *bus;
665 int err;
666
667 if (devfn) {
668 err = devfn(dev, opaque);
669 if (err) {
670 return err;
671 }
672 }
673
674 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
675 err = qbus_walk_children(bus, devfn, busfn, opaque);
676 if (err < 0) {
677 return err;
678 }
679 }
680
681 return 0;
682 }
683
684 static BusState *qbus_find_recursive(BusState *bus, const char *name,
685 const BusInfo *info)
686 {
687 DeviceState *dev;
688 BusState *child, *ret;
689 int match = 1;
690
691 if (name && (strcmp(bus->name, name) != 0)) {
692 match = 0;
693 }
694 if (info && (bus->info != info)) {
695 match = 0;
696 }
697 if (match) {
698 return bus;
699 }
700
701 QTAILQ_FOREACH(dev, &bus->children, sibling) {
702 QLIST_FOREACH(child, &dev->child_bus, sibling) {
703 ret = qbus_find_recursive(child, name, info);
704 if (ret) {
705 return ret;
706 }
707 }
708 }
709 return NULL;
710 }
711
712 DeviceState *qdev_find_recursive(BusState *bus, const char *id)
713 {
714 DeviceState *dev, *ret;
715 BusState *child;
716
717 QTAILQ_FOREACH(dev, &bus->children, sibling) {
718 if (dev->id && strcmp(dev->id, id) == 0)
719 return dev;
720 QLIST_FOREACH(child, &dev->child_bus, sibling) {
721 ret = qdev_find_recursive(child, id);
722 if (ret) {
723 return ret;
724 }
725 }
726 }
727 return NULL;
728 }
729
730 static void qbus_list_bus(DeviceState *dev)
731 {
732 BusState *child;
733 const char *sep = " ";
734
735 error_printf("child busses at \"%s\":",
736 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
737 QLIST_FOREACH(child, &dev->child_bus, sibling) {
738 error_printf("%s\"%s\"", sep, child->name);
739 sep = ", ";
740 }
741 error_printf("\n");
742 }
743
744 static void qbus_list_dev(BusState *bus)
745 {
746 DeviceState *dev;
747 const char *sep = " ";
748
749 error_printf("devices at \"%s\":", bus->name);
750 QTAILQ_FOREACH(dev, &bus->children, sibling) {
751 error_printf("%s\"%s\"", sep, object_get_typename(OBJECT(dev)));
752 if (dev->id)
753 error_printf("/\"%s\"", dev->id);
754 sep = ", ";
755 }
756 error_printf("\n");
757 }
758
759 static BusState *qbus_find_bus(DeviceState *dev, char *elem)
760 {
761 BusState *child;
762
763 QLIST_FOREACH(child, &dev->child_bus, sibling) {
764 if (strcmp(child->name, elem) == 0) {
765 return child;
766 }
767 }
768 return NULL;
769 }
770
771 static DeviceState *qbus_find_dev(BusState *bus, char *elem)
772 {
773 DeviceState *dev;
774
775 /*
776 * try to match in order:
777 * (1) instance id, if present
778 * (2) driver name
779 * (3) driver alias, if present
780 */
781 QTAILQ_FOREACH(dev, &bus->children, sibling) {
782 if (dev->id && strcmp(dev->id, elem) == 0) {
783 return dev;
784 }
785 }
786 QTAILQ_FOREACH(dev, &bus->children, sibling) {
787 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
788 return dev;
789 }
790 }
791 QTAILQ_FOREACH(dev, &bus->children, sibling) {
792 if (qdev_get_info(dev)->alias && strcmp(qdev_get_info(dev)->alias, elem) == 0) {
793 return dev;
794 }
795 }
796 return NULL;
797 }
798
799 static BusState *qbus_find(const char *path)
800 {
801 DeviceState *dev;
802 BusState *bus;
803 char elem[128];
804 int pos, len;
805
806 /* find start element */
807 if (path[0] == '/') {
808 bus = main_system_bus;
809 pos = 0;
810 } else {
811 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
812 assert(!path[0]);
813 elem[0] = len = 0;
814 }
815 bus = qbus_find_recursive(main_system_bus, elem, NULL);
816 if (!bus) {
817 qerror_report(QERR_BUS_NOT_FOUND, elem);
818 return NULL;
819 }
820 pos = len;
821 }
822
823 for (;;) {
824 assert(path[pos] == '/' || !path[pos]);
825 while (path[pos] == '/') {
826 pos++;
827 }
828 if (path[pos] == '\0') {
829 return bus;
830 }
831
832 /* find device */
833 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
834 assert(0);
835 elem[0] = len = 0;
836 }
837 pos += len;
838 dev = qbus_find_dev(bus, elem);
839 if (!dev) {
840 qerror_report(QERR_DEVICE_NOT_FOUND, elem);
841 if (!monitor_cur_is_qmp()) {
842 qbus_list_dev(bus);
843 }
844 return NULL;
845 }
846
847 assert(path[pos] == '/' || !path[pos]);
848 while (path[pos] == '/') {
849 pos++;
850 }
851 if (path[pos] == '\0') {
852 /* last specified element is a device. If it has exactly
853 * one child bus accept it nevertheless */
854 switch (dev->num_child_bus) {
855 case 0:
856 qerror_report(QERR_DEVICE_NO_BUS, elem);
857 return NULL;
858 case 1:
859 return QLIST_FIRST(&dev->child_bus);
860 default:
861 qerror_report(QERR_DEVICE_MULTIPLE_BUSSES, elem);
862 if (!monitor_cur_is_qmp()) {
863 qbus_list_bus(dev);
864 }
865 return NULL;
866 }
867 }
868
869 /* find bus */
870 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
871 assert(0);
872 elem[0] = len = 0;
873 }
874 pos += len;
875 bus = qbus_find_bus(dev, elem);
876 if (!bus) {
877 qerror_report(QERR_BUS_NOT_FOUND, elem);
878 if (!monitor_cur_is_qmp()) {
879 qbus_list_bus(dev);
880 }
881 return NULL;
882 }
883 }
884 }
885
886 void qbus_create_inplace(BusState *bus, BusInfo *info,
887 DeviceState *parent, const char *name)
888 {
889 char *buf;
890 int i,len;
891
892 bus->info = info;
893 bus->parent = parent;
894
895 if (name) {
896 /* use supplied name */
897 bus->name = g_strdup(name);
898 } else if (parent && parent->id) {
899 /* parent device has id -> use it for bus name */
900 len = strlen(parent->id) + 16;
901 buf = g_malloc(len);
902 snprintf(buf, len, "%s.%d", parent->id, parent->num_child_bus);
903 bus->name = buf;
904 } else {
905 /* no id -> use lowercase bus type for bus name */
906 len = strlen(info->name) + 16;
907 buf = g_malloc(len);
908 len = snprintf(buf, len, "%s.%d", info->name,
909 parent ? parent->num_child_bus : 0);
910 for (i = 0; i < len; i++)
911 buf[i] = qemu_tolower(buf[i]);
912 bus->name = buf;
913 }
914
915 QTAILQ_INIT(&bus->children);
916 if (parent) {
917 QLIST_INSERT_HEAD(&parent->child_bus, bus, sibling);
918 parent->num_child_bus++;
919 } else if (bus != main_system_bus) {
920 /* TODO: once all bus devices are qdevified,
921 only reset handler for main_system_bus should be registered here. */
922 qemu_register_reset(qbus_reset_all_fn, bus);
923 }
924 }
925
926 BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name)
927 {
928 BusState *bus;
929
930 bus = g_malloc0(info->size);
931 bus->qdev_allocated = 1;
932 qbus_create_inplace(bus, info, parent, name);
933 return bus;
934 }
935
936 static void main_system_bus_create(void)
937 {
938 /* assign main_system_bus before qbus_create_inplace()
939 * in order to make "if (bus != main_system_bus)" work */
940 main_system_bus = g_malloc0(system_bus_info.size);
941 main_system_bus->qdev_allocated = 1;
942 qbus_create_inplace(main_system_bus, &system_bus_info, NULL,
943 "main-system-bus");
944 }
945
946 void qbus_free(BusState *bus)
947 {
948 DeviceState *dev;
949
950 while ((dev = QTAILQ_FIRST(&bus->children)) != NULL) {
951 qdev_free(dev);
952 }
953 if (bus->parent) {
954 QLIST_REMOVE(bus, sibling);
955 bus->parent->num_child_bus--;
956 } else {
957 assert(bus != main_system_bus); /* main_system_bus is never freed */
958 qemu_unregister_reset(qbus_reset_all_fn, bus);
959 }
960 g_free((void*)bus->name);
961 if (bus->qdev_allocated) {
962 g_free(bus);
963 }
964 }
965
966 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
967 static void qbus_print(Monitor *mon, BusState *bus, int indent);
968
969 static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
970 const char *prefix, int indent)
971 {
972 char buf[64];
973
974 if (!props)
975 return;
976 while (props->name) {
977 /*
978 * TODO Properties without a print method are just for dirty
979 * hacks. qdev_prop_ptr is the only such PropertyInfo. It's
980 * marked for removal. The test props->info->print should be
981 * removed along with it.
982 */
983 if (props->info->print) {
984 props->info->print(dev, props, buf, sizeof(buf));
985 qdev_printf("%s-prop: %s = %s\n", prefix, props->name, buf);
986 }
987 props++;
988 }
989 }
990
991 static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
992 {
993 BusState *child;
994 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
995 dev->id ? dev->id : "");
996 indent += 2;
997 if (dev->num_gpio_in) {
998 qdev_printf("gpio-in %d\n", dev->num_gpio_in);
999 }
1000 if (dev->num_gpio_out) {
1001 qdev_printf("gpio-out %d\n", dev->num_gpio_out);
1002 }
1003 qdev_print_props(mon, dev, qdev_get_info(dev)->props, "dev", indent);
1004 qdev_print_props(mon, dev, dev->parent_bus->info->props, "bus", indent);
1005 if (dev->parent_bus->info->print_dev)
1006 dev->parent_bus->info->print_dev(mon, dev, indent);
1007 QLIST_FOREACH(child, &dev->child_bus, sibling) {
1008 qbus_print(mon, child, indent);
1009 }
1010 }
1011
1012 static void qbus_print(Monitor *mon, BusState *bus, int indent)
1013 {
1014 struct DeviceState *dev;
1015
1016 qdev_printf("bus: %s\n", bus->name);
1017 indent += 2;
1018 qdev_printf("type %s\n", bus->info->name);
1019 QTAILQ_FOREACH(dev, &bus->children, sibling) {
1020 qdev_print(mon, dev, indent);
1021 }
1022 }
1023 #undef qdev_printf
1024
1025 void do_info_qtree(Monitor *mon)
1026 {
1027 if (main_system_bus)
1028 qbus_print(mon, main_system_bus, 0);
1029 }
1030
1031 void do_info_qdm(Monitor *mon)
1032 {
1033 DeviceInfo *info;
1034
1035 for (info = device_info_list; info != NULL; info = info->next) {
1036 qdev_print_devinfo(info);
1037 }
1038 }
1039
1040 int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
1041 {
1042 QemuOpts *opts;
1043
1044 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict);
1045 if (!opts) {
1046 return -1;
1047 }
1048 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
1049 qemu_opts_del(opts);
1050 return 0;
1051 }
1052 if (!qdev_device_add(opts)) {
1053 qemu_opts_del(opts);
1054 return -1;
1055 }
1056 return 0;
1057 }
1058
1059 int do_device_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
1060 {
1061 const char *id = qdict_get_str(qdict, "id");
1062 DeviceState *dev;
1063
1064 dev = qdev_find_recursive(main_system_bus, id);
1065 if (NULL == dev) {
1066 qerror_report(QERR_DEVICE_NOT_FOUND, id);
1067 return -1;
1068 }
1069 return qdev_unplug(dev);
1070 }
1071
1072 static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
1073 {
1074 int l = 0;
1075
1076 if (dev && dev->parent_bus) {
1077 char *d;
1078 l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
1079 if (dev->parent_bus->info->get_fw_dev_path) {
1080 d = dev->parent_bus->info->get_fw_dev_path(dev);
1081 l += snprintf(p + l, size - l, "%s", d);
1082 g_free(d);
1083 } else {
1084 l += snprintf(p + l, size - l, "%s", object_get_typename(OBJECT(dev)));
1085 }
1086 }
1087 l += snprintf(p + l , size - l, "/");
1088
1089 return l;
1090 }
1091
1092 char* qdev_get_fw_dev_path(DeviceState *dev)
1093 {
1094 char path[128];
1095 int l;
1096
1097 l = qdev_get_fw_dev_path_helper(dev, path, 128);
1098
1099 path[l-1] = '\0';
1100
1101 return strdup(path);
1102 }
1103
1104 char *qdev_get_type(DeviceState *dev, Error **errp)
1105 {
1106 return g_strdup(object_get_typename(OBJECT(dev)));
1107 }
1108
1109 void qdev_ref(DeviceState *dev)
1110 {
1111 dev->ref++;
1112 }
1113
1114 void qdev_unref(DeviceState *dev)
1115 {
1116 g_assert(dev->ref > 0);
1117 dev->ref--;
1118 }
1119
1120 void qdev_property_add(DeviceState *dev, const char *name, const char *type,
1121 DevicePropertyAccessor *get, DevicePropertyAccessor *set,
1122 DevicePropertyRelease *release,
1123 void *opaque, Error **errp)
1124 {
1125 DeviceProperty *prop = g_malloc0(sizeof(*prop));
1126
1127 prop->name = g_strdup(name);
1128 prop->type = g_strdup(type);
1129
1130 prop->get = get;
1131 prop->set = set;
1132 prop->release = release;
1133 prop->opaque = opaque;
1134
1135 QTAILQ_INSERT_TAIL(&dev->properties, prop, node);
1136 }
1137
1138 static DeviceProperty *qdev_property_find(DeviceState *dev, const char *name)
1139 {
1140 DeviceProperty *prop;
1141
1142 QTAILQ_FOREACH(prop, &dev->properties, node) {
1143 if (strcmp(prop->name, name) == 0) {
1144 return prop;
1145 }
1146 }
1147
1148 return NULL;
1149 }
1150
1151 void qdev_property_get(DeviceState *dev, Visitor *v, const char *name,
1152 Error **errp)
1153 {
1154 DeviceProperty *prop = qdev_property_find(dev, name);
1155
1156 if (prop == NULL) {
1157 error_set(errp, QERR_PROPERTY_NOT_FOUND, dev->id?:"", name);
1158 return;
1159 }
1160
1161 if (!prop->get) {
1162 error_set(errp, QERR_PERMISSION_DENIED);
1163 } else {
1164 prop->get(dev, v, prop->opaque, name, errp);
1165 }
1166 }
1167
1168 void qdev_property_set(DeviceState *dev, Visitor *v, const char *name,
1169 Error **errp)
1170 {
1171 DeviceProperty *prop = qdev_property_find(dev, name);
1172
1173 if (prop == NULL) {
1174 error_set(errp, QERR_PROPERTY_NOT_FOUND, dev->id?:"", name);
1175 return;
1176 }
1177
1178 if (!prop->set) {
1179 error_set(errp, QERR_PERMISSION_DENIED);
1180 } else {
1181 prop->set(dev, v, prop->opaque, name, errp);
1182 }
1183 }
1184
1185 const char *qdev_property_get_type(DeviceState *dev, const char *name, Error **errp)
1186 {
1187 DeviceProperty *prop = qdev_property_find(dev, name);
1188
1189 if (prop == NULL) {
1190 error_set(errp, QERR_PROPERTY_NOT_FOUND, dev->id?:"", name);
1191 return NULL;
1192 }
1193
1194 return prop->type;
1195 }
1196
1197 /**
1198 * Legacy property handling
1199 */
1200
1201 static void qdev_get_legacy_property(DeviceState *dev, Visitor *v, void *opaque,
1202 const char *name, Error **errp)
1203 {
1204 Property *prop = opaque;
1205
1206 char buffer[1024];
1207 char *ptr = buffer;
1208
1209 prop->info->print(dev, prop, buffer, sizeof(buffer));
1210 visit_type_str(v, &ptr, name, errp);
1211 }
1212
1213 static void qdev_set_legacy_property(DeviceState *dev, Visitor *v, void *opaque,
1214 const char *name, Error **errp)
1215 {
1216 Property *prop = opaque;
1217 Error *local_err = NULL;
1218 char *ptr = NULL;
1219 int ret;
1220
1221 if (dev->state != DEV_STATE_CREATED) {
1222 error_set(errp, QERR_PERMISSION_DENIED);
1223 return;
1224 }
1225
1226 visit_type_str(v, &ptr, name, &local_err);
1227 if (local_err) {
1228 error_propagate(errp, local_err);
1229 return;
1230 }
1231
1232 ret = prop->info->parse(dev, prop, ptr);
1233 error_set_from_qdev_prop_error(errp, ret, dev, prop, ptr);
1234 g_free(ptr);
1235 }
1236
1237 /**
1238 * @qdev_add_legacy_property - adds a legacy property
1239 *
1240 * Do not use this is new code! Properties added through this interface will
1241 * be given names and types in the "legacy" namespace.
1242 *
1243 * Legacy properties are always processed as strings. The format of the string
1244 * depends on the property type.
1245 */
1246 void qdev_property_add_legacy(DeviceState *dev, Property *prop,
1247 Error **errp)
1248 {
1249 gchar *name, *type;
1250
1251 name = g_strdup_printf("legacy-%s", prop->name);
1252 type = g_strdup_printf("legacy<%s>",
1253 prop->info->legacy_name ?: prop->info->name);
1254
1255 qdev_property_add(dev, name, type,
1256 prop->info->print ? qdev_get_legacy_property : NULL,
1257 prop->info->parse ? qdev_set_legacy_property : NULL,
1258 NULL,
1259 prop, errp);
1260
1261 g_free(type);
1262 g_free(name);
1263 }
1264
1265 /**
1266 * @qdev_property_add_static - add a @Property to a device.
1267 *
1268 * Static properties access data in a struct. The actual type of the
1269 * property and the field depends on the property type.
1270 */
1271 void qdev_property_add_static(DeviceState *dev, Property *prop,
1272 Error **errp)
1273 {
1274 qdev_property_add(dev, prop->name, prop->info->name,
1275 prop->info->get, prop->info->set,
1276 NULL,
1277 prop, errp);
1278 }
1279
1280 DeviceState *qdev_get_root(void)
1281 {
1282 static DeviceState *qdev_root;
1283
1284 if (!qdev_root) {
1285 qdev_root = qdev_create(NULL, "container");
1286 qdev_init_nofail(qdev_root);
1287 }
1288
1289 return qdev_root;
1290 }
1291
1292 static void qdev_get_child_property(DeviceState *dev, Visitor *v, void *opaque,
1293 const char *name, Error **errp)
1294 {
1295 DeviceState *child = opaque;
1296 gchar *path;
1297
1298 path = qdev_get_canonical_path(child);
1299 visit_type_str(v, &path, name, errp);
1300 g_free(path);
1301 }
1302
1303 static void qdev_release_child_property(DeviceState *dev, const char *name,
1304 void *opaque)
1305 {
1306 DeviceState *child = opaque;
1307
1308 qdev_unref(child);
1309 }
1310
1311 void qdev_property_add_child(DeviceState *dev, const char *name,
1312 DeviceState *child, Error **errp)
1313 {
1314 gchar *type;
1315
1316 type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child)));
1317
1318 qdev_property_add(dev, name, type, qdev_get_child_property,
1319 NULL, qdev_release_child_property,
1320 child, errp);
1321
1322 qdev_ref(child);
1323 g_assert(child->parent == NULL);
1324 child->parent = dev;
1325
1326 g_free(type);
1327 }
1328
1329 static void qdev_get_link_property(DeviceState *dev, Visitor *v, void *opaque,
1330 const char *name, Error **errp)
1331 {
1332 DeviceState **child = opaque;
1333 gchar *path;
1334
1335 if (*child) {
1336 path = qdev_get_canonical_path(*child);
1337 visit_type_str(v, &path, name, errp);
1338 g_free(path);
1339 } else {
1340 path = (gchar *)"";
1341 visit_type_str(v, &path, name, errp);
1342 }
1343 }
1344
1345 static void qdev_set_link_property(DeviceState *dev, Visitor *v, void *opaque,
1346 const char *name, Error **errp)
1347 {
1348 DeviceState **child = opaque;
1349 bool ambiguous = false;
1350 const char *type;
1351 char *path;
1352
1353 type = qdev_property_get_type(dev, name, NULL);
1354
1355 visit_type_str(v, &path, name, errp);
1356
1357 if (*child) {
1358 qdev_unref(*child);
1359 }
1360
1361 if (strcmp(path, "") != 0) {
1362 DeviceState *target;
1363
1364 target = qdev_resolve_path(path, &ambiguous);
1365 if (target) {
1366 gchar *target_type;
1367
1368 target_type = g_strdup_printf("link<%s>", object_get_typename(OBJECT(target)));
1369 if (strcmp(target_type, type) == 0) {
1370 *child = target;
1371 qdev_ref(target);
1372 } else {
1373 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, type);
1374 }
1375
1376 g_free(target_type);
1377 } else {
1378 error_set(errp, QERR_DEVICE_NOT_FOUND, path);
1379 }
1380 } else {
1381 *child = NULL;
1382 }
1383
1384 g_free(path);
1385 }
1386
1387 void qdev_property_add_link(DeviceState *dev, const char *name,
1388 const char *type, DeviceState **child,
1389 Error **errp)
1390 {
1391 gchar *full_type;
1392
1393 full_type = g_strdup_printf("link<%s>", type);
1394
1395 qdev_property_add(dev, name, full_type,
1396 qdev_get_link_property,
1397 qdev_set_link_property,
1398 NULL, child, errp);
1399
1400 g_free(full_type);
1401 }
1402
1403 gchar *qdev_get_canonical_path(DeviceState *dev)
1404 {
1405 DeviceState *root = qdev_get_root();
1406 char *newpath = NULL, *path = NULL;
1407
1408 while (dev != root) {
1409 DeviceProperty *prop = NULL;
1410
1411 g_assert(dev->parent != NULL);
1412
1413 QTAILQ_FOREACH(prop, &dev->parent->properties, node) {
1414 if (!strstart(prop->type, "child<", NULL)) {
1415 continue;
1416 }
1417
1418 if (prop->opaque == dev) {
1419 if (path) {
1420 newpath = g_strdup_printf("%s/%s", prop->name, path);
1421 g_free(path);
1422 path = newpath;
1423 } else {
1424 path = g_strdup(prop->name);
1425 }
1426 break;
1427 }
1428 }
1429
1430 g_assert(prop != NULL);
1431
1432 dev = dev->parent;
1433 }
1434
1435 newpath = g_strdup_printf("/%s", path);
1436 g_free(path);
1437
1438 return newpath;
1439 }
1440
1441 static DeviceState *qdev_resolve_abs_path(DeviceState *parent,
1442 gchar **parts,
1443 int index)
1444 {
1445 DeviceProperty *prop;
1446 DeviceState *child;
1447
1448 if (parts[index] == NULL) {
1449 return parent;
1450 }
1451
1452 if (strcmp(parts[index], "") == 0) {
1453 return qdev_resolve_abs_path(parent, parts, index + 1);
1454 }
1455
1456 prop = qdev_property_find(parent, parts[index]);
1457 if (prop == NULL) {
1458 return NULL;
1459 }
1460
1461 child = NULL;
1462 if (strstart(prop->type, "link<", NULL)) {
1463 DeviceState **pchild = prop->opaque;
1464 if (*pchild) {
1465 child = *pchild;
1466 }
1467 } else if (strstart(prop->type, "child<", NULL)) {
1468 child = prop->opaque;
1469 }
1470
1471 if (!child) {
1472 return NULL;
1473 }
1474
1475 return qdev_resolve_abs_path(child, parts, index + 1);
1476 }
1477
1478 static DeviceState *qdev_resolve_partial_path(DeviceState *parent,
1479 gchar **parts,
1480 bool *ambiguous)
1481 {
1482 DeviceState *dev;
1483 DeviceProperty *prop;
1484
1485 dev = qdev_resolve_abs_path(parent, parts, 0);
1486
1487 QTAILQ_FOREACH(prop, &parent->properties, node) {
1488 DeviceState *found;
1489
1490 if (!strstart(prop->type, "child<", NULL)) {
1491 continue;
1492 }
1493
1494 found = qdev_resolve_partial_path(prop->opaque, parts, ambiguous);
1495 if (found) {
1496 if (dev) {
1497 if (ambiguous) {
1498 *ambiguous = true;
1499 }
1500 return NULL;
1501 }
1502 dev = found;
1503 }
1504
1505 if (ambiguous && *ambiguous) {
1506 return NULL;
1507 }
1508 }
1509
1510 return dev;
1511 }
1512
1513 DeviceState *qdev_resolve_path(const char *path, bool *ambiguous)
1514 {
1515 bool partial_path = true;
1516 DeviceState *dev;
1517 gchar **parts;
1518
1519 parts = g_strsplit(path, "/", 0);
1520 if (parts == NULL || parts[0] == NULL) {
1521 g_strfreev(parts);
1522 return qdev_get_root();
1523 }
1524
1525 if (strcmp(parts[0], "") == 0) {
1526 partial_path = false;
1527 }
1528
1529 if (partial_path) {
1530 if (ambiguous) {
1531 *ambiguous = false;
1532 }
1533 dev = qdev_resolve_partial_path(qdev_get_root(), parts, ambiguous);
1534 } else {
1535 dev = qdev_resolve_abs_path(qdev_get_root(), parts, 1);
1536 }
1537
1538 g_strfreev(parts);
1539
1540 return dev;
1541 }
1542
1543 typedef struct StringProperty
1544 {
1545 char *(*get)(DeviceState *, Error **);
1546 void (*set)(DeviceState *, const char *, Error **);
1547 } StringProperty;
1548
1549 static void qdev_property_get_str(DeviceState *dev, Visitor *v, void *opaque,
1550 const char *name, Error **errp)
1551 {
1552 StringProperty *prop = opaque;
1553 char *value;
1554
1555 value = prop->get(dev, errp);
1556 if (value) {
1557 visit_type_str(v, &value, name, errp);
1558 g_free(value);
1559 }
1560 }
1561
1562 static void qdev_property_set_str(DeviceState *dev, Visitor *v, void *opaque,
1563 const char *name, Error **errp)
1564 {
1565 StringProperty *prop = opaque;
1566 char *value;
1567 Error *local_err = NULL;
1568
1569 visit_type_str(v, &value, name, &local_err);
1570 if (local_err) {
1571 error_propagate(errp, local_err);
1572 return;
1573 }
1574
1575 prop->set(dev, value, errp);
1576 g_free(value);
1577 }
1578
1579 static void qdev_property_release_str(DeviceState *dev, const char *name,
1580 void *opaque)
1581 {
1582 StringProperty *prop = opaque;
1583 g_free(prop);
1584 }
1585
1586 void qdev_property_add_str(DeviceState *dev, const char *name,
1587 char *(*get)(DeviceState *, Error **),
1588 void (*set)(DeviceState *, const char *, Error **),
1589 Error **errp)
1590 {
1591 StringProperty *prop = g_malloc0(sizeof(*prop));
1592
1593 prop->get = get;
1594 prop->set = set;
1595
1596 qdev_property_add(dev, name, "string",
1597 get ? qdev_property_get_str : NULL,
1598 set ? qdev_property_set_str : NULL,
1599 qdev_property_release_str,
1600 prop, errp);
1601 }
1602
1603 void qdev_machine_init(void)
1604 {
1605 qdev_get_peripheral_anon();
1606 qdev_get_peripheral();
1607 }
1608
1609 void device_reset(DeviceState *dev)
1610 {
1611 DeviceClass *klass = DEVICE_GET_CLASS(dev);
1612
1613 if (klass->reset) {
1614 klass->reset(dev);
1615 }
1616 }
1617
1618 static TypeInfo device_type_info = {
1619 .name = TYPE_DEVICE,
1620 .parent = TYPE_OBJECT,
1621 .instance_size = sizeof(DeviceState),
1622 .abstract = true,
1623 .class_size = sizeof(DeviceClass),
1624 };
1625
1626 static void init_qdev(void)
1627 {
1628 type_register_static(&device_type_info);
1629 }
1630
1631 device_init(init_qdev);