usb: Add an usb_device_ep_stopped USBDevice method
[qemu/ar7.git] / hw / qdev.c
blobf2c248451c0a8b6fc6ea3ed5a0646dde84e3e2e4
1 /*
2 * Dynamic device configuration and creation.
4 * Copyright (c) 2009 CodeSourcery
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.
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.
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/>.
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. */
28 #include "qdev.h"
29 #include "sysemu/sysemu.h"
30 #include "qapi/error.h"
31 #include "qapi/visitor.h"
33 int qdev_hotplug = 0;
34 static bool qdev_hot_added = false;
35 static bool qdev_hot_removed = false;
37 const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
39 DeviceClass *dc = DEVICE_GET_CLASS(dev);
40 return dc->vmsd;
43 const char *qdev_fw_name(DeviceState *dev)
45 DeviceClass *dc = DEVICE_GET_CLASS(dev);
47 if (dc->fw_name) {
48 return dc->fw_name;
51 return object_get_typename(OBJECT(dev));
54 static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
55 Error **errp);
57 static void bus_remove_child(BusState *bus, DeviceState *child)
59 BusChild *kid;
61 QTAILQ_FOREACH(kid, &bus->children, sibling) {
62 if (kid->child == child) {
63 char name[32];
65 snprintf(name, sizeof(name), "child[%d]", kid->index);
66 QTAILQ_REMOVE(&bus->children, kid, sibling);
67 object_property_del(OBJECT(bus), name, NULL);
68 g_free(kid);
69 return;
74 static void bus_add_child(BusState *bus, DeviceState *child)
76 char name[32];
77 BusChild *kid = g_malloc0(sizeof(*kid));
79 if (qdev_hotplug) {
80 assert(bus->allow_hotplug);
83 kid->index = bus->max_index++;
84 kid->child = child;
86 QTAILQ_INSERT_HEAD(&bus->children, kid, sibling);
88 snprintf(name, sizeof(name), "child[%d]", kid->index);
89 object_property_add_link(OBJECT(bus), name,
90 object_get_typename(OBJECT(child)),
91 (Object **)&kid->child,
92 NULL);
95 void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
97 dev->parent_bus = bus;
98 bus_add_child(bus, dev);
101 /* Create a new device. This only initializes the device state structure
102 and allows properties to be set. qdev_init should be called to
103 initialize the actual device emulation. */
104 DeviceState *qdev_create(BusState *bus, const char *name)
106 DeviceState *dev;
108 dev = qdev_try_create(bus, name);
109 if (!dev) {
110 if (bus) {
111 error_report("Unknown device '%s' for bus '%s'\n", name,
112 object_get_typename(OBJECT(bus)));
113 abort();
114 } else {
115 error_report("Unknown device '%s' for default sysbus\n", name);
116 abort();
120 return dev;
123 DeviceState *qdev_try_create(BusState *bus, const char *type)
125 DeviceState *dev;
127 if (object_class_by_name(type) == NULL) {
128 return NULL;
130 dev = DEVICE(object_new(type));
131 if (!dev) {
132 return NULL;
135 if (!bus) {
136 bus = sysbus_get_default();
139 qdev_set_parent_bus(dev, bus);
141 return dev;
144 /* Initialize a device. Device properties should be set before calling
145 this function. IRQs and MMIO regions should be connected/mapped after
146 calling this function.
147 On failure, destroy the device and return negative value.
148 Return 0 on success. */
149 int qdev_init(DeviceState *dev)
151 DeviceClass *dc = DEVICE_GET_CLASS(dev);
152 int rc;
154 assert(dev->state == DEV_STATE_CREATED);
156 rc = dc->init(dev);
157 if (rc < 0) {
158 qdev_free(dev);
159 return rc;
162 if (!OBJECT(dev)->parent) {
163 static int unattached_count = 0;
164 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
166 object_property_add_child(container_get(qdev_get_machine(),
167 "/unattached"),
168 name, OBJECT(dev), NULL);
169 g_free(name);
172 if (qdev_get_vmsd(dev)) {
173 vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
174 dev->instance_id_alias,
175 dev->alias_required_for_version);
177 dev->state = DEV_STATE_INITIALIZED;
178 if (dev->hotplugged) {
179 device_reset(dev);
181 return 0;
184 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
185 int required_for_version)
187 assert(dev->state == DEV_STATE_CREATED);
188 dev->instance_id_alias = alias_id;
189 dev->alias_required_for_version = required_for_version;
192 void qdev_unplug(DeviceState *dev, Error **errp)
194 DeviceClass *dc = DEVICE_GET_CLASS(dev);
196 if (!dev->parent_bus->allow_hotplug) {
197 error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
198 return;
200 assert(dc->unplug != NULL);
202 qdev_hot_removed = true;
204 if (dc->unplug(dev) < 0) {
205 error_set(errp, QERR_UNDEFINED_ERROR);
206 return;
210 static int qdev_reset_one(DeviceState *dev, void *opaque)
212 device_reset(dev);
214 return 0;
217 static int qbus_reset_one(BusState *bus, void *opaque)
219 BusClass *bc = BUS_GET_CLASS(bus);
220 if (bc->reset) {
221 return bc->reset(bus);
223 return 0;
226 void qdev_reset_all(DeviceState *dev)
228 qdev_walk_children(dev, qdev_reset_one, qbus_reset_one, NULL);
231 void qbus_reset_all_fn(void *opaque)
233 BusState *bus = opaque;
234 qbus_walk_children(bus, qdev_reset_one, qbus_reset_one, NULL);
237 /* can be used as ->unplug() callback for the simple cases */
238 int qdev_simple_unplug_cb(DeviceState *dev)
240 /* just zap it */
241 qdev_free(dev);
242 return 0;
246 /* Like qdev_init(), but terminate program via error_report() instead of
247 returning an error value. This is okay during machine creation.
248 Don't use for hotplug, because there callers need to recover from
249 failure. Exception: if you know the device's init() callback can't
250 fail, then qdev_init_nofail() can't fail either, and is therefore
251 usable even then. But relying on the device implementation that
252 way is somewhat unclean, and best avoided. */
253 void qdev_init_nofail(DeviceState *dev)
255 const char *typename = object_get_typename(OBJECT(dev));
257 if (qdev_init(dev) < 0) {
258 error_report("Initialization of device %s failed", typename);
259 exit(1);
263 /* Unlink device from bus and free the structure. */
264 void qdev_free(DeviceState *dev)
266 object_delete(OBJECT(dev));
269 void qdev_machine_creation_done(void)
272 * ok, initial machine setup is done, starting from now we can
273 * only create hotpluggable devices
275 qdev_hotplug = 1;
278 bool qdev_machine_modified(void)
280 return qdev_hot_added || qdev_hot_removed;
283 BusState *qdev_get_parent_bus(DeviceState *dev)
285 return dev->parent_bus;
288 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
290 dev->gpio_in = qemu_extend_irqs(dev->gpio_in, dev->num_gpio_in, handler,
291 dev, n);
292 dev->num_gpio_in += n;
295 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
297 assert(dev->num_gpio_out == 0);
298 dev->num_gpio_out = n;
299 dev->gpio_out = pins;
302 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
304 assert(n >= 0 && n < dev->num_gpio_in);
305 return dev->gpio_in[n];
308 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
310 assert(n >= 0 && n < dev->num_gpio_out);
311 dev->gpio_out[n] = pin;
314 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
316 BusState *bus;
318 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
319 if (strcmp(name, bus->name) == 0) {
320 return bus;
323 return NULL;
326 int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
327 qbus_walkerfn *busfn, void *opaque)
329 BusChild *kid;
330 int err;
332 if (busfn) {
333 err = busfn(bus, opaque);
334 if (err) {
335 return err;
339 QTAILQ_FOREACH(kid, &bus->children, sibling) {
340 err = qdev_walk_children(kid->child, devfn, busfn, opaque);
341 if (err < 0) {
342 return err;
346 return 0;
349 int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
350 qbus_walkerfn *busfn, void *opaque)
352 BusState *bus;
353 int err;
355 if (devfn) {
356 err = devfn(dev, opaque);
357 if (err) {
358 return err;
362 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
363 err = qbus_walk_children(bus, devfn, busfn, opaque);
364 if (err < 0) {
365 return err;
369 return 0;
372 DeviceState *qdev_find_recursive(BusState *bus, const char *id)
374 BusChild *kid;
375 DeviceState *ret;
376 BusState *child;
378 QTAILQ_FOREACH(kid, &bus->children, sibling) {
379 DeviceState *dev = kid->child;
381 if (dev->id && strcmp(dev->id, id) == 0) {
382 return dev;
385 QLIST_FOREACH(child, &dev->child_bus, sibling) {
386 ret = qdev_find_recursive(child, id);
387 if (ret) {
388 return ret;
392 return NULL;
395 static void qbus_realize(BusState *bus)
397 const char *typename = object_get_typename(OBJECT(bus));
398 char *buf;
399 int i,len;
401 if (bus->name) {
402 /* use supplied name */
403 } else if (bus->parent && bus->parent->id) {
404 /* parent device has id -> use it for bus name */
405 len = strlen(bus->parent->id) + 16;
406 buf = g_malloc(len);
407 snprintf(buf, len, "%s.%d", bus->parent->id, bus->parent->num_child_bus);
408 bus->name = buf;
409 } else {
410 /* no id -> use lowercase bus type for bus name */
411 len = strlen(typename) + 16;
412 buf = g_malloc(len);
413 len = snprintf(buf, len, "%s.%d", typename,
414 bus->parent ? bus->parent->num_child_bus : 0);
415 for (i = 0; i < len; i++)
416 buf[i] = qemu_tolower(buf[i]);
417 bus->name = buf;
420 if (bus->parent) {
421 QLIST_INSERT_HEAD(&bus->parent->child_bus, bus, sibling);
422 bus->parent->num_child_bus++;
423 object_property_add_child(OBJECT(bus->parent), bus->name, OBJECT(bus), NULL);
424 } else if (bus != sysbus_get_default()) {
425 /* TODO: once all bus devices are qdevified,
426 only reset handler for main_system_bus should be registered here. */
427 qemu_register_reset(qbus_reset_all_fn, bus);
431 void qbus_create_inplace(BusState *bus, const char *typename,
432 DeviceState *parent, const char *name)
434 object_initialize(bus, typename);
436 bus->parent = parent;
437 bus->name = name ? g_strdup(name) : NULL;
438 qbus_realize(bus);
441 BusState *qbus_create(const char *typename, DeviceState *parent, const char *name)
443 BusState *bus;
445 bus = BUS(object_new(typename));
447 bus->parent = parent;
448 bus->name = name ? g_strdup(name) : NULL;
449 qbus_realize(bus);
451 return bus;
454 void qbus_free(BusState *bus)
456 object_delete(OBJECT(bus));
459 static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
461 BusClass *bc = BUS_GET_CLASS(bus);
463 if (bc->get_fw_dev_path) {
464 return bc->get_fw_dev_path(dev);
467 return NULL;
470 static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
472 int l = 0;
474 if (dev && dev->parent_bus) {
475 char *d;
476 l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
477 d = bus_get_fw_dev_path(dev->parent_bus, dev);
478 if (d) {
479 l += snprintf(p + l, size - l, "%s", d);
480 g_free(d);
481 } else {
482 l += snprintf(p + l, size - l, "%s", object_get_typename(OBJECT(dev)));
485 l += snprintf(p + l , size - l, "/");
487 return l;
490 char* qdev_get_fw_dev_path(DeviceState *dev)
492 char path[128];
493 int l;
495 l = qdev_get_fw_dev_path_helper(dev, path, 128);
497 path[l-1] = '\0';
499 return g_strdup(path);
502 char *qdev_get_dev_path(DeviceState *dev)
504 BusClass *bc;
506 if (!dev || !dev->parent_bus) {
507 return NULL;
510 bc = BUS_GET_CLASS(dev->parent_bus);
511 if (bc->get_dev_path) {
512 return bc->get_dev_path(dev);
515 return NULL;
519 * Legacy property handling
522 static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque,
523 const char *name, Error **errp)
525 DeviceState *dev = DEVICE(obj);
526 Property *prop = opaque;
528 char buffer[1024];
529 char *ptr = buffer;
531 prop->info->print(dev, prop, buffer, sizeof(buffer));
532 visit_type_str(v, &ptr, name, errp);
535 static void qdev_set_legacy_property(Object *obj, Visitor *v, void *opaque,
536 const char *name, Error **errp)
538 DeviceState *dev = DEVICE(obj);
539 Property *prop = opaque;
540 Error *local_err = NULL;
541 char *ptr = NULL;
542 int ret;
544 if (dev->state != DEV_STATE_CREATED) {
545 error_set(errp, QERR_PERMISSION_DENIED);
546 return;
549 visit_type_str(v, &ptr, name, &local_err);
550 if (local_err) {
551 error_propagate(errp, local_err);
552 return;
555 ret = prop->info->parse(dev, prop, ptr);
556 error_set_from_qdev_prop_error(errp, ret, dev, prop, ptr);
557 g_free(ptr);
561 * @qdev_add_legacy_property - adds a legacy property
563 * Do not use this is new code! Properties added through this interface will
564 * be given names and types in the "legacy" namespace.
566 * Legacy properties are string versions of other OOM properties. The format
567 * of the string depends on the property type.
569 void qdev_property_add_legacy(DeviceState *dev, Property *prop,
570 Error **errp)
572 gchar *name, *type;
574 /* Register pointer properties as legacy properties */
575 if (!prop->info->print && !prop->info->parse &&
576 (prop->info->set || prop->info->get)) {
577 return;
580 name = g_strdup_printf("legacy-%s", prop->name);
581 type = g_strdup_printf("legacy<%s>",
582 prop->info->legacy_name ?: prop->info->name);
584 object_property_add(OBJECT(dev), name, type,
585 prop->info->print ? qdev_get_legacy_property : prop->info->get,
586 prop->info->parse ? qdev_set_legacy_property : prop->info->set,
587 NULL,
588 prop, errp);
590 g_free(type);
591 g_free(name);
595 * @qdev_property_add_static - add a @Property to a device.
597 * Static properties access data in a struct. The actual type of the
598 * property and the field depends on the property type.
600 void qdev_property_add_static(DeviceState *dev, Property *prop,
601 Error **errp)
603 Error *local_err = NULL;
604 Object *obj = OBJECT(dev);
607 * TODO qdev_prop_ptr does not have getters or setters. It must
608 * go now that it can be replaced with links. The test should be
609 * removed along with it: all static properties are read/write.
611 if (!prop->info->get && !prop->info->set) {
612 return;
615 object_property_add(obj, prop->name, prop->info->name,
616 prop->info->get, prop->info->set,
617 prop->info->release,
618 prop, &local_err);
620 if (local_err) {
621 error_propagate(errp, local_err);
622 return;
624 if (prop->qtype == QTYPE_NONE) {
625 return;
628 if (prop->qtype == QTYPE_QBOOL) {
629 object_property_set_bool(obj, prop->defval, prop->name, &local_err);
630 } else if (prop->info->enum_table) {
631 object_property_set_str(obj, prop->info->enum_table[prop->defval],
632 prop->name, &local_err);
633 } else if (prop->qtype == QTYPE_QINT) {
634 object_property_set_int(obj, prop->defval, prop->name, &local_err);
636 assert_no_error(local_err);
639 static void device_initfn(Object *obj)
641 DeviceState *dev = DEVICE(obj);
642 ObjectClass *class;
643 Property *prop;
645 if (qdev_hotplug) {
646 dev->hotplugged = 1;
647 qdev_hot_added = true;
650 dev->instance_id_alias = -1;
651 dev->state = DEV_STATE_CREATED;
653 class = object_get_class(OBJECT(dev));
654 do {
655 for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
656 qdev_property_add_legacy(dev, prop, NULL);
657 qdev_property_add_static(dev, prop, NULL);
659 class = object_class_get_parent(class);
660 } while (class != object_class_by_name(TYPE_DEVICE));
661 qdev_prop_set_globals(dev);
663 object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
664 (Object **)&dev->parent_bus, NULL);
667 /* Unlink device from bus and free the structure. */
668 static void device_finalize(Object *obj)
670 DeviceState *dev = DEVICE(obj);
671 BusState *bus;
672 DeviceClass *dc = DEVICE_GET_CLASS(dev);
674 if (dev->state == DEV_STATE_INITIALIZED) {
675 while (dev->num_child_bus) {
676 bus = QLIST_FIRST(&dev->child_bus);
677 qbus_free(bus);
679 if (qdev_get_vmsd(dev)) {
680 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
682 if (dc->exit) {
683 dc->exit(dev);
685 if (dev->opts) {
686 qemu_opts_del(dev->opts);
691 static void device_class_base_init(ObjectClass *class, void *data)
693 DeviceClass *klass = DEVICE_CLASS(class);
695 /* We explicitly look up properties in the superclasses,
696 * so do not propagate them to the subclasses.
698 klass->props = NULL;
701 static void qdev_remove_from_bus(Object *obj)
703 DeviceState *dev = DEVICE(obj);
705 bus_remove_child(dev->parent_bus, dev);
708 static void device_class_init(ObjectClass *class, void *data)
710 class->unparent = qdev_remove_from_bus;
713 void device_reset(DeviceState *dev)
715 DeviceClass *klass = DEVICE_GET_CLASS(dev);
717 if (klass->reset) {
718 klass->reset(dev);
722 Object *qdev_get_machine(void)
724 static Object *dev;
726 if (dev == NULL) {
727 dev = container_get(object_get_root(), "/machine");
730 return dev;
733 static TypeInfo device_type_info = {
734 .name = TYPE_DEVICE,
735 .parent = TYPE_OBJECT,
736 .instance_size = sizeof(DeviceState),
737 .instance_init = device_initfn,
738 .instance_finalize = device_finalize,
739 .class_base_init = device_class_base_init,
740 .class_init = device_class_init,
741 .abstract = true,
742 .class_size = sizeof(DeviceClass),
745 static void qbus_initfn(Object *obj)
747 BusState *bus = BUS(obj);
749 QTAILQ_INIT(&bus->children);
752 static void qbus_finalize(Object *obj)
754 BusState *bus = BUS(obj);
755 BusChild *kid;
757 while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
758 DeviceState *dev = kid->child;
759 qdev_free(dev);
761 if (bus->parent) {
762 QLIST_REMOVE(bus, sibling);
763 bus->parent->num_child_bus--;
764 } else {
765 assert(bus != sysbus_get_default()); /* main_system_bus is never freed */
766 qemu_unregister_reset(qbus_reset_all_fn, bus);
768 g_free((char *)bus->name);
771 static const TypeInfo bus_info = {
772 .name = TYPE_BUS,
773 .parent = TYPE_OBJECT,
774 .instance_size = sizeof(BusState),
775 .abstract = true,
776 .class_size = sizeof(BusClass),
777 .instance_init = qbus_initfn,
778 .instance_finalize = qbus_finalize,
781 static void qdev_register_types(void)
783 type_register_static(&bus_info);
784 type_register_static(&device_type_info);
787 type_init(qdev_register_types)