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