qapi: Improve specificity of type/member descriptions
[qemu/armbru.git] / hw / core / qdev-properties.c
blob357b8761b546162a256a53e0c065242832ec8877
1 #include "qemu/osdep.h"
2 #include "hw/qdev-properties.h"
3 #include "qapi/error.h"
4 #include "qapi/qapi-types-misc.h"
5 #include "qapi/qmp/qerror.h"
6 #include "qemu/ctype.h"
7 #include "qemu/error-report.h"
8 #include "qapi/visitor.h"
9 #include "qemu/units.h"
10 #include "qemu/cutils.h"
11 #include "qdev-prop-internal.h"
13 void qdev_prop_set_after_realize(DeviceState *dev, const char *name,
14 Error **errp)
16 if (dev->id) {
17 error_setg(errp, "Attempt to set property '%s' on device '%s' "
18 "(type '%s') after it was realized", name, dev->id,
19 object_get_typename(OBJECT(dev)));
20 } else {
21 error_setg(errp, "Attempt to set property '%s' on anonymous device "
22 "(type '%s') after it was realized", name,
23 object_get_typename(OBJECT(dev)));
27 /* returns: true if property is allowed to be set, false otherwise */
28 static bool qdev_prop_allow_set(Object *obj, const char *name,
29 const PropertyInfo *info, Error **errp)
31 DeviceState *dev = DEVICE(obj);
33 if (dev->realized && !info->realized_set_allowed) {
34 qdev_prop_set_after_realize(dev, name, errp);
35 return false;
37 return true;
40 void qdev_prop_allow_set_link_before_realize(const Object *obj,
41 const char *name,
42 Object *val, Error **errp)
44 DeviceState *dev = DEVICE(obj);
46 if (dev->realized) {
47 error_setg(errp, "Attempt to set link property '%s' on device '%s' "
48 "(type '%s') after it was realized",
49 name, dev->id, object_get_typename(obj));
53 void *object_field_prop_ptr(Object *obj, Property *prop)
55 void *ptr = obj;
56 ptr += prop->offset;
57 return ptr;
60 static void field_prop_get(Object *obj, Visitor *v, const char *name,
61 void *opaque, Error **errp)
63 Property *prop = opaque;
64 return prop->info->get(obj, v, name, opaque, errp);
67 /**
68 * field_prop_getter: Return getter function to be used for property
70 * Return value can be NULL if @info has no getter function.
72 static ObjectPropertyAccessor *field_prop_getter(const PropertyInfo *info)
74 return info->get ? field_prop_get : NULL;
77 static void field_prop_set(Object *obj, Visitor *v, const char *name,
78 void *opaque, Error **errp)
80 Property *prop = opaque;
82 if (!qdev_prop_allow_set(obj, name, prop->info, errp)) {
83 return;
86 return prop->info->set(obj, v, name, opaque, errp);
89 /**
90 * field_prop_setter: Return setter function to be used for property
92 * Return value can be NULL if @info has not setter function.
94 static ObjectPropertyAccessor *field_prop_setter(const PropertyInfo *info)
96 return info->set ? field_prop_set : NULL;
99 void qdev_propinfo_get_enum(Object *obj, Visitor *v, const char *name,
100 void *opaque, Error **errp)
102 Property *prop = opaque;
103 int *ptr = object_field_prop_ptr(obj, prop);
105 visit_type_enum(v, name, ptr, prop->info->enum_table, errp);
108 void qdev_propinfo_set_enum(Object *obj, Visitor *v, const char *name,
109 void *opaque, Error **errp)
111 Property *prop = opaque;
112 int *ptr = object_field_prop_ptr(obj, prop);
114 visit_type_enum(v, name, ptr, prop->info->enum_table, errp);
117 void qdev_propinfo_set_default_value_enum(ObjectProperty *op,
118 const Property *prop)
120 object_property_set_default_str(op,
121 qapi_enum_lookup(prop->info->enum_table, prop->defval.i));
124 const PropertyInfo qdev_prop_enum = {
125 .name = "enum",
126 .get = qdev_propinfo_get_enum,
127 .set = qdev_propinfo_set_enum,
128 .set_default_value = qdev_propinfo_set_default_value_enum,
131 /* Bit */
133 static uint32_t qdev_get_prop_mask(Property *prop)
135 assert(prop->info == &qdev_prop_bit);
136 return 0x1 << prop->bitnr;
139 static void bit_prop_set(Object *obj, Property *props, bool val)
141 uint32_t *p = object_field_prop_ptr(obj, props);
142 uint32_t mask = qdev_get_prop_mask(props);
143 if (val) {
144 *p |= mask;
145 } else {
146 *p &= ~mask;
150 static void prop_get_bit(Object *obj, Visitor *v, const char *name,
151 void *opaque, Error **errp)
153 Property *prop = opaque;
154 uint32_t *p = object_field_prop_ptr(obj, prop);
155 bool value = (*p & qdev_get_prop_mask(prop)) != 0;
157 visit_type_bool(v, name, &value, errp);
160 static void prop_set_bit(Object *obj, Visitor *v, const char *name,
161 void *opaque, Error **errp)
163 Property *prop = opaque;
164 bool value;
166 if (!visit_type_bool(v, name, &value, errp)) {
167 return;
169 bit_prop_set(obj, prop, value);
172 static void set_default_value_bool(ObjectProperty *op, const Property *prop)
174 object_property_set_default_bool(op, prop->defval.u);
177 const PropertyInfo qdev_prop_bit = {
178 .name = "bool",
179 .description = "on/off",
180 .get = prop_get_bit,
181 .set = prop_set_bit,
182 .set_default_value = set_default_value_bool,
185 /* Bit64 */
187 static uint64_t qdev_get_prop_mask64(Property *prop)
189 assert(prop->info == &qdev_prop_bit64);
190 return 0x1ull << prop->bitnr;
193 static void bit64_prop_set(Object *obj, Property *props, bool val)
195 uint64_t *p = object_field_prop_ptr(obj, props);
196 uint64_t mask = qdev_get_prop_mask64(props);
197 if (val) {
198 *p |= mask;
199 } else {
200 *p &= ~mask;
204 static void prop_get_bit64(Object *obj, Visitor *v, const char *name,
205 void *opaque, Error **errp)
207 Property *prop = opaque;
208 uint64_t *p = object_field_prop_ptr(obj, prop);
209 bool value = (*p & qdev_get_prop_mask64(prop)) != 0;
211 visit_type_bool(v, name, &value, errp);
214 static void prop_set_bit64(Object *obj, Visitor *v, const char *name,
215 void *opaque, Error **errp)
217 Property *prop = opaque;
218 bool value;
220 if (!visit_type_bool(v, name, &value, errp)) {
221 return;
223 bit64_prop_set(obj, prop, value);
226 const PropertyInfo qdev_prop_bit64 = {
227 .name = "bool",
228 .description = "on/off",
229 .get = prop_get_bit64,
230 .set = prop_set_bit64,
231 .set_default_value = set_default_value_bool,
234 /* --- bool --- */
236 static void get_bool(Object *obj, Visitor *v, const char *name, void *opaque,
237 Error **errp)
239 Property *prop = opaque;
240 bool *ptr = object_field_prop_ptr(obj, prop);
242 visit_type_bool(v, name, ptr, errp);
245 static void set_bool(Object *obj, Visitor *v, const char *name, void *opaque,
246 Error **errp)
248 Property *prop = opaque;
249 bool *ptr = object_field_prop_ptr(obj, prop);
251 visit_type_bool(v, name, ptr, errp);
254 const PropertyInfo qdev_prop_bool = {
255 .name = "bool",
256 .get = get_bool,
257 .set = set_bool,
258 .set_default_value = set_default_value_bool,
261 /* --- 8bit integer --- */
263 static void get_uint8(Object *obj, Visitor *v, const char *name, void *opaque,
264 Error **errp)
266 Property *prop = opaque;
267 uint8_t *ptr = object_field_prop_ptr(obj, prop);
269 visit_type_uint8(v, name, ptr, errp);
272 static void set_uint8(Object *obj, Visitor *v, const char *name, void *opaque,
273 Error **errp)
275 Property *prop = opaque;
276 uint8_t *ptr = object_field_prop_ptr(obj, prop);
278 visit_type_uint8(v, name, ptr, errp);
281 void qdev_propinfo_set_default_value_int(ObjectProperty *op,
282 const Property *prop)
284 object_property_set_default_int(op, prop->defval.i);
287 void qdev_propinfo_set_default_value_uint(ObjectProperty *op,
288 const Property *prop)
290 object_property_set_default_uint(op, prop->defval.u);
293 const PropertyInfo qdev_prop_uint8 = {
294 .name = "uint8",
295 .get = get_uint8,
296 .set = set_uint8,
297 .set_default_value = qdev_propinfo_set_default_value_uint,
300 /* --- 16bit integer --- */
302 static void get_uint16(Object *obj, Visitor *v, const char *name,
303 void *opaque, Error **errp)
305 Property *prop = opaque;
306 uint16_t *ptr = object_field_prop_ptr(obj, prop);
308 visit_type_uint16(v, name, ptr, errp);
311 static void set_uint16(Object *obj, Visitor *v, const char *name,
312 void *opaque, Error **errp)
314 Property *prop = opaque;
315 uint16_t *ptr = object_field_prop_ptr(obj, prop);
317 visit_type_uint16(v, name, ptr, errp);
320 const PropertyInfo qdev_prop_uint16 = {
321 .name = "uint16",
322 .get = get_uint16,
323 .set = set_uint16,
324 .set_default_value = qdev_propinfo_set_default_value_uint,
327 /* --- 32bit integer --- */
329 static void get_uint32(Object *obj, Visitor *v, const char *name,
330 void *opaque, Error **errp)
332 Property *prop = opaque;
333 uint32_t *ptr = object_field_prop_ptr(obj, prop);
335 visit_type_uint32(v, name, ptr, errp);
338 static void set_uint32(Object *obj, Visitor *v, const char *name,
339 void *opaque, Error **errp)
341 Property *prop = opaque;
342 uint32_t *ptr = object_field_prop_ptr(obj, prop);
344 visit_type_uint32(v, name, ptr, errp);
347 void qdev_propinfo_get_int32(Object *obj, Visitor *v, const char *name,
348 void *opaque, Error **errp)
350 Property *prop = opaque;
351 int32_t *ptr = object_field_prop_ptr(obj, prop);
353 visit_type_int32(v, name, ptr, errp);
356 static void set_int32(Object *obj, Visitor *v, const char *name, void *opaque,
357 Error **errp)
359 Property *prop = opaque;
360 int32_t *ptr = object_field_prop_ptr(obj, prop);
362 visit_type_int32(v, name, ptr, errp);
365 const PropertyInfo qdev_prop_uint32 = {
366 .name = "uint32",
367 .get = get_uint32,
368 .set = set_uint32,
369 .set_default_value = qdev_propinfo_set_default_value_uint,
372 const PropertyInfo qdev_prop_int32 = {
373 .name = "int32",
374 .get = qdev_propinfo_get_int32,
375 .set = set_int32,
376 .set_default_value = qdev_propinfo_set_default_value_int,
379 /* --- 64bit integer --- */
381 static void get_uint64(Object *obj, Visitor *v, const char *name,
382 void *opaque, Error **errp)
384 Property *prop = opaque;
385 uint64_t *ptr = object_field_prop_ptr(obj, prop);
387 visit_type_uint64(v, name, ptr, errp);
390 static void set_uint64(Object *obj, Visitor *v, const char *name,
391 void *opaque, Error **errp)
393 Property *prop = opaque;
394 uint64_t *ptr = object_field_prop_ptr(obj, prop);
396 visit_type_uint64(v, name, ptr, errp);
399 static void get_int64(Object *obj, Visitor *v, const char *name,
400 void *opaque, Error **errp)
402 Property *prop = opaque;
403 int64_t *ptr = object_field_prop_ptr(obj, prop);
405 visit_type_int64(v, name, ptr, errp);
408 static void set_int64(Object *obj, Visitor *v, const char *name,
409 void *opaque, Error **errp)
411 Property *prop = opaque;
412 int64_t *ptr = object_field_prop_ptr(obj, prop);
414 visit_type_int64(v, name, ptr, errp);
417 const PropertyInfo qdev_prop_uint64 = {
418 .name = "uint64",
419 .get = get_uint64,
420 .set = set_uint64,
421 .set_default_value = qdev_propinfo_set_default_value_uint,
424 const PropertyInfo qdev_prop_int64 = {
425 .name = "int64",
426 .get = get_int64,
427 .set = set_int64,
428 .set_default_value = qdev_propinfo_set_default_value_int,
431 static void set_uint64_checkmask(Object *obj, Visitor *v, const char *name,
432 void *opaque, Error **errp)
434 Property *prop = opaque;
435 uint64_t *ptr = object_field_prop_ptr(obj, prop);
437 visit_type_uint64(v, name, ptr, errp);
438 if (*ptr & ~prop->bitmask) {
439 error_setg(errp, "Property value for '%s' has bits outside mask '0x%" PRIx64 "'",
440 name, prop->bitmask);
444 const PropertyInfo qdev_prop_uint64_checkmask = {
445 .name = "uint64",
446 .get = get_uint64,
447 .set = set_uint64_checkmask,
450 /* --- string --- */
452 static void release_string(Object *obj, const char *name, void *opaque)
454 Property *prop = opaque;
455 g_free(*(char **)object_field_prop_ptr(obj, prop));
458 static void get_string(Object *obj, Visitor *v, const char *name,
459 void *opaque, Error **errp)
461 Property *prop = opaque;
462 char **ptr = object_field_prop_ptr(obj, prop);
464 if (!*ptr) {
465 char *str = (char *)"";
466 visit_type_str(v, name, &str, errp);
467 } else {
468 visit_type_str(v, name, ptr, errp);
472 static void set_string(Object *obj, Visitor *v, const char *name,
473 void *opaque, Error **errp)
475 Property *prop = opaque;
476 char **ptr = object_field_prop_ptr(obj, prop);
477 char *str;
479 if (!visit_type_str(v, name, &str, errp)) {
480 return;
482 g_free(*ptr);
483 *ptr = str;
486 const PropertyInfo qdev_prop_string = {
487 .name = "str",
488 .release = release_string,
489 .get = get_string,
490 .set = set_string,
493 /* --- on/off/auto --- */
495 const PropertyInfo qdev_prop_on_off_auto = {
496 .name = "OnOffAuto",
497 .description = "on/off/auto",
498 .enum_table = &OnOffAuto_lookup,
499 .get = qdev_propinfo_get_enum,
500 .set = qdev_propinfo_set_enum,
501 .set_default_value = qdev_propinfo_set_default_value_enum,
504 /* --- 32bit unsigned int 'size' type --- */
506 void qdev_propinfo_get_size32(Object *obj, Visitor *v, const char *name,
507 void *opaque, Error **errp)
509 Property *prop = opaque;
510 uint32_t *ptr = object_field_prop_ptr(obj, prop);
511 uint64_t value = *ptr;
513 visit_type_size(v, name, &value, errp);
516 static void set_size32(Object *obj, Visitor *v, const char *name, void *opaque,
517 Error **errp)
519 Property *prop = opaque;
520 uint32_t *ptr = object_field_prop_ptr(obj, prop);
521 uint64_t value;
523 if (!visit_type_size(v, name, &value, errp)) {
524 return;
527 if (value > UINT32_MAX) {
528 error_setg(errp,
529 "Property %s.%s doesn't take value %" PRIu64
530 " (maximum: %u)",
531 object_get_typename(obj), name, value, UINT32_MAX);
532 return;
535 *ptr = value;
538 const PropertyInfo qdev_prop_size32 = {
539 .name = "size",
540 .get = qdev_propinfo_get_size32,
541 .set = set_size32,
542 .set_default_value = qdev_propinfo_set_default_value_uint,
545 /* --- support for array properties --- */
547 /* Used as an opaque for the object properties we add for each
548 * array element. Note that the struct Property must be first
549 * in the struct so that a pointer to this works as the opaque
550 * for the underlying element's property hooks as well as for
551 * our own release callback.
553 typedef struct {
554 struct Property prop;
555 char *propname;
556 ObjectPropertyRelease *release;
557 } ArrayElementProperty;
559 /* object property release callback for array element properties:
560 * we call the underlying element's property release hook, and
561 * then free the memory we allocated when we added the property.
563 static void array_element_release(Object *obj, const char *name, void *opaque)
565 ArrayElementProperty *p = opaque;
566 if (p->release) {
567 p->release(obj, name, opaque);
569 g_free(p->propname);
570 g_free(p);
573 static void set_prop_arraylen(Object *obj, Visitor *v, const char *name,
574 void *opaque, Error **errp)
576 /* Setter for the property which defines the length of a
577 * variable-sized property array. As well as actually setting the
578 * array-length field in the device struct, we have to create the
579 * array itself and dynamically add the corresponding properties.
581 Property *prop = opaque;
582 uint32_t *alenptr = object_field_prop_ptr(obj, prop);
583 void **arrayptr = (void *)obj + prop->arrayoffset;
584 void *eltptr;
585 const char *arrayname;
586 int i;
588 if (*alenptr) {
589 error_setg(errp, "array size property %s may not be set more than once",
590 name);
591 return;
593 if (!visit_type_uint32(v, name, alenptr, errp)) {
594 return;
596 if (!*alenptr) {
597 return;
600 /* DEFINE_PROP_ARRAY guarantees that name should start with this prefix;
601 * strip it off so we can get the name of the array itself.
603 assert(strncmp(name, PROP_ARRAY_LEN_PREFIX,
604 strlen(PROP_ARRAY_LEN_PREFIX)) == 0);
605 arrayname = name + strlen(PROP_ARRAY_LEN_PREFIX);
607 /* Note that it is the responsibility of the individual device's deinit
608 * to free the array proper.
610 *arrayptr = eltptr = g_malloc0(*alenptr * prop->arrayfieldsize);
611 for (i = 0; i < *alenptr; i++, eltptr += prop->arrayfieldsize) {
612 char *propname = g_strdup_printf("%s[%d]", arrayname, i);
613 ArrayElementProperty *arrayprop = g_new0(ArrayElementProperty, 1);
614 arrayprop->release = prop->arrayinfo->release;
615 arrayprop->propname = propname;
616 arrayprop->prop.info = prop->arrayinfo;
617 arrayprop->prop.name = propname;
618 /* This ugly piece of pointer arithmetic sets up the offset so
619 * that when the underlying get/set hooks call qdev_get_prop_ptr
620 * they get the right answer despite the array element not actually
621 * being inside the device struct.
623 arrayprop->prop.offset = eltptr - (void *)obj;
624 assert(object_field_prop_ptr(obj, &arrayprop->prop) == eltptr);
625 object_property_add(obj, propname,
626 arrayprop->prop.info->name,
627 field_prop_getter(arrayprop->prop.info),
628 field_prop_setter(arrayprop->prop.info),
629 array_element_release,
630 arrayprop);
634 const PropertyInfo qdev_prop_arraylen = {
635 .name = "uint32",
636 .get = get_uint32,
637 .set = set_prop_arraylen,
638 .set_default_value = qdev_propinfo_set_default_value_uint,
641 /* --- public helpers --- */
643 static Property *qdev_prop_walk(Property *props, const char *name)
645 if (!props) {
646 return NULL;
648 while (props->name) {
649 if (strcmp(props->name, name) == 0) {
650 return props;
652 props++;
654 return NULL;
657 static Property *qdev_prop_find(DeviceState *dev, const char *name)
659 ObjectClass *class;
660 Property *prop;
662 /* device properties */
663 class = object_get_class(OBJECT(dev));
664 do {
665 prop = qdev_prop_walk(DEVICE_CLASS(class)->props_, name);
666 if (prop) {
667 return prop;
669 class = object_class_get_parent(class);
670 } while (class != object_class_by_name(TYPE_DEVICE));
672 return NULL;
675 void error_set_from_qdev_prop_error(Error **errp, int ret, Object *obj,
676 const char *name, const char *value)
678 switch (ret) {
679 case -EEXIST:
680 error_setg(errp, "Property '%s.%s' can't take value '%s', it's in use",
681 object_get_typename(obj), name, value);
682 break;
683 default:
684 case -EINVAL:
685 error_setg(errp, QERR_PROPERTY_VALUE_BAD,
686 object_get_typename(obj), name, value);
687 break;
688 case -ENOENT:
689 error_setg(errp, "Property '%s.%s' can't find value '%s'",
690 object_get_typename(obj), name, value);
691 break;
692 case 0:
693 break;
697 void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value)
699 object_property_set_bool(OBJECT(dev), name, value, &error_abort);
702 void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value)
704 object_property_set_int(OBJECT(dev), name, value, &error_abort);
707 void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value)
709 object_property_set_int(OBJECT(dev), name, value, &error_abort);
712 void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value)
714 object_property_set_int(OBJECT(dev), name, value, &error_abort);
717 void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value)
719 object_property_set_int(OBJECT(dev), name, value, &error_abort);
722 void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value)
724 object_property_set_int(OBJECT(dev), name, value, &error_abort);
727 void qdev_prop_set_string(DeviceState *dev, const char *name, const char *value)
729 object_property_set_str(OBJECT(dev), name, value, &error_abort);
732 void qdev_prop_set_enum(DeviceState *dev, const char *name, int value)
734 Property *prop;
736 prop = qdev_prop_find(dev, name);
737 object_property_set_str(OBJECT(dev), name,
738 qapi_enum_lookup(prop->info->enum_table, value),
739 &error_abort);
742 static GPtrArray *global_props(void)
744 static GPtrArray *gp;
746 if (!gp) {
747 gp = g_ptr_array_new();
750 return gp;
753 void qdev_prop_register_global(GlobalProperty *prop)
755 g_ptr_array_add(global_props(), prop);
758 const GlobalProperty *qdev_find_global_prop(Object *obj,
759 const char *name)
761 GPtrArray *props = global_props();
762 const GlobalProperty *p;
763 int i;
765 for (i = 0; i < props->len; i++) {
766 p = g_ptr_array_index(props, i);
767 if (object_dynamic_cast(obj, p->driver)
768 && !strcmp(p->property, name)) {
769 return p;
772 return NULL;
775 int qdev_prop_check_globals(void)
777 int i, ret = 0;
779 for (i = 0; i < global_props()->len; i++) {
780 GlobalProperty *prop;
781 ObjectClass *oc;
782 DeviceClass *dc;
784 prop = g_ptr_array_index(global_props(), i);
785 if (prop->used) {
786 continue;
788 oc = object_class_by_name(prop->driver);
789 oc = object_class_dynamic_cast(oc, TYPE_DEVICE);
790 if (!oc) {
791 warn_report("global %s.%s has invalid class name",
792 prop->driver, prop->property);
793 ret = 1;
794 continue;
796 dc = DEVICE_CLASS(oc);
797 if (!dc->hotpluggable && !prop->used) {
798 warn_report("global %s.%s=%s not used",
799 prop->driver, prop->property, prop->value);
800 ret = 1;
801 continue;
804 return ret;
807 void qdev_prop_set_globals(DeviceState *dev)
809 object_apply_global_props(OBJECT(dev), global_props(),
810 dev->hotplugged ? NULL : &error_fatal);
813 /* --- 64bit unsigned int 'size' type --- */
815 static void get_size(Object *obj, Visitor *v, const char *name, void *opaque,
816 Error **errp)
818 Property *prop = opaque;
819 uint64_t *ptr = object_field_prop_ptr(obj, prop);
821 visit_type_size(v, name, ptr, errp);
824 static void set_size(Object *obj, Visitor *v, const char *name, void *opaque,
825 Error **errp)
827 Property *prop = opaque;
828 uint64_t *ptr = object_field_prop_ptr(obj, prop);
830 visit_type_size(v, name, ptr, errp);
833 const PropertyInfo qdev_prop_size = {
834 .name = "size",
835 .get = get_size,
836 .set = set_size,
837 .set_default_value = qdev_propinfo_set_default_value_uint,
840 /* --- object link property --- */
842 static ObjectProperty *create_link_property(ObjectClass *oc, const char *name,
843 Property *prop)
845 return object_class_property_add_link(oc, name, prop->link_type,
846 prop->offset,
847 qdev_prop_allow_set_link_before_realize,
848 OBJ_PROP_LINK_STRONG);
851 const PropertyInfo qdev_prop_link = {
852 .name = "link",
853 .create = create_link_property,
856 void qdev_property_add_static(DeviceState *dev, Property *prop)
858 Object *obj = OBJECT(dev);
859 ObjectProperty *op;
861 assert(!prop->info->create);
863 op = object_property_add(obj, prop->name, prop->info->name,
864 field_prop_getter(prop->info),
865 field_prop_setter(prop->info),
866 prop->info->release,
867 prop);
869 object_property_set_description(obj, prop->name,
870 prop->info->description);
872 if (prop->set_default) {
873 prop->info->set_default_value(op, prop);
874 if (op->init) {
875 op->init(obj, op);
880 static void qdev_class_add_property(DeviceClass *klass, const char *name,
881 Property *prop)
883 ObjectClass *oc = OBJECT_CLASS(klass);
884 ObjectProperty *op;
886 if (prop->info->create) {
887 op = prop->info->create(oc, name, prop);
888 } else {
889 op = object_class_property_add(oc,
890 name, prop->info->name,
891 field_prop_getter(prop->info),
892 field_prop_setter(prop->info),
893 prop->info->release,
894 prop);
896 if (prop->set_default) {
897 prop->info->set_default_value(op, prop);
899 object_class_property_set_description(oc, name, prop->info->description);
903 * Legacy property handling
906 static void qdev_get_legacy_property(Object *obj, Visitor *v,
907 const char *name, void *opaque,
908 Error **errp)
910 Property *prop = opaque;
912 char buffer[1024];
913 char *ptr = buffer;
915 prop->info->print(obj, prop, buffer, sizeof(buffer));
916 visit_type_str(v, name, &ptr, errp);
920 * qdev_class_add_legacy_property:
921 * @dev: Device to add the property to.
922 * @prop: The qdev property definition.
924 * Add a legacy QOM property to @dev for qdev property @prop.
926 * Legacy properties are string versions of QOM properties. The format of
927 * the string depends on the property type. Legacy properties are only
928 * needed for "info qtree".
930 * Do not use this in new code! QOM Properties added through this interface
931 * will be given names in the "legacy" namespace.
933 static void qdev_class_add_legacy_property(DeviceClass *dc, Property *prop)
935 g_autofree char *name = NULL;
937 /* Register pointer properties as legacy properties */
938 if (!prop->info->print && prop->info->get) {
939 return;
942 name = g_strdup_printf("legacy-%s", prop->name);
943 object_class_property_add(OBJECT_CLASS(dc), name, "str",
944 prop->info->print ? qdev_get_legacy_property : prop->info->get,
945 NULL, NULL, prop);
948 void device_class_set_props(DeviceClass *dc, Property *props)
950 Property *prop;
952 dc->props_ = props;
953 for (prop = props; prop && prop->name; prop++) {
954 qdev_class_add_legacy_property(dc, prop);
955 qdev_class_add_property(dc, prop->name, prop);
959 void qdev_alias_all_properties(DeviceState *target, Object *source)
961 ObjectClass *class;
962 Property *prop;
964 class = object_get_class(OBJECT(target));
965 do {
966 DeviceClass *dc = DEVICE_CLASS(class);
968 for (prop = dc->props_; prop && prop->name; prop++) {
969 object_property_add_alias(source, prop->name,
970 OBJECT(target), prop->name);
972 class = object_class_get_parent(class);
973 } while (class != object_class_by_name(TYPE_DEVICE));