3 #include "qapi/qmp/qerror.h"
4 #include "sysemu/blockdev.h"
5 #include "hw/block/block.h"
7 #include "qapi/visitor.h"
8 #include "sysemu/char.h"
10 void qdev_prop_set_after_realize(DeviceState
*dev
, const char *name
,
14 error_setg(errp
, "Attempt to set property '%s' on device '%s' "
15 "(type '%s') after it was realized", name
, dev
->id
,
16 object_get_typename(OBJECT(dev
)));
18 error_setg(errp
, "Attempt to set property '%s' on anonymous device "
19 "(type '%s') after it was realized", name
,
20 object_get_typename(OBJECT(dev
)));
24 void *qdev_get_prop_ptr(DeviceState
*dev
, Property
*prop
)
31 static void get_enum(Object
*obj
, Visitor
*v
, void *opaque
,
32 const char *name
, Error
**errp
)
34 DeviceState
*dev
= DEVICE(obj
);
35 Property
*prop
= opaque
;
36 int *ptr
= qdev_get_prop_ptr(dev
, prop
);
38 visit_type_enum(v
, ptr
, prop
->info
->enum_table
,
39 prop
->info
->name
, prop
->name
, errp
);
42 static void set_enum(Object
*obj
, Visitor
*v
, void *opaque
,
43 const char *name
, Error
**errp
)
45 DeviceState
*dev
= DEVICE(obj
);
46 Property
*prop
= opaque
;
47 int *ptr
= qdev_get_prop_ptr(dev
, prop
);
50 qdev_prop_set_after_realize(dev
, name
, errp
);
54 visit_type_enum(v
, ptr
, prop
->info
->enum_table
,
55 prop
->info
->name
, prop
->name
, errp
);
60 static uint32_t qdev_get_prop_mask(Property
*prop
)
62 assert(prop
->info
== &qdev_prop_bit
);
63 return 0x1 << prop
->bitnr
;
66 static void bit_prop_set(DeviceState
*dev
, Property
*props
, bool val
)
68 uint32_t *p
= qdev_get_prop_ptr(dev
, props
);
69 uint32_t mask
= qdev_get_prop_mask(props
);
77 static int prop_print_bit(DeviceState
*dev
, Property
*prop
, char *dest
,
80 uint32_t *p
= qdev_get_prop_ptr(dev
, prop
);
81 return snprintf(dest
, len
, (*p
& qdev_get_prop_mask(prop
)) ? "on" : "off");
84 static void prop_get_bit(Object
*obj
, Visitor
*v
, void *opaque
,
85 const char *name
, Error
**errp
)
87 DeviceState
*dev
= DEVICE(obj
);
88 Property
*prop
= opaque
;
89 uint32_t *p
= qdev_get_prop_ptr(dev
, prop
);
90 bool value
= (*p
& qdev_get_prop_mask(prop
)) != 0;
92 visit_type_bool(v
, &value
, name
, errp
);
95 static void prop_set_bit(Object
*obj
, Visitor
*v
, void *opaque
,
96 const char *name
, Error
**errp
)
98 DeviceState
*dev
= DEVICE(obj
);
99 Property
*prop
= opaque
;
100 Error
*local_err
= NULL
;
104 qdev_prop_set_after_realize(dev
, name
, errp
);
108 visit_type_bool(v
, &value
, name
, &local_err
);
110 error_propagate(errp
, local_err
);
113 bit_prop_set(dev
, prop
, value
);
116 PropertyInfo qdev_prop_bit
= {
118 .legacy_name
= "on/off",
119 .print
= prop_print_bit
,
126 static void get_bool(Object
*obj
, Visitor
*v
, void *opaque
,
127 const char *name
, Error
**errp
)
129 DeviceState
*dev
= DEVICE(obj
);
130 Property
*prop
= opaque
;
131 bool *ptr
= qdev_get_prop_ptr(dev
, prop
);
133 visit_type_bool(v
, ptr
, name
, errp
);
136 static void set_bool(Object
*obj
, Visitor
*v
, void *opaque
,
137 const char *name
, Error
**errp
)
139 DeviceState
*dev
= DEVICE(obj
);
140 Property
*prop
= opaque
;
141 bool *ptr
= qdev_get_prop_ptr(dev
, prop
);
144 qdev_prop_set_after_realize(dev
, name
, errp
);
148 visit_type_bool(v
, ptr
, name
, errp
);
151 PropertyInfo qdev_prop_bool
= {
157 /* --- 8bit integer --- */
159 static void get_uint8(Object
*obj
, Visitor
*v
, void *opaque
,
160 const char *name
, Error
**errp
)
162 DeviceState
*dev
= DEVICE(obj
);
163 Property
*prop
= opaque
;
164 uint8_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
166 visit_type_uint8(v
, ptr
, name
, errp
);
169 static void set_uint8(Object
*obj
, Visitor
*v
, void *opaque
,
170 const char *name
, Error
**errp
)
172 DeviceState
*dev
= DEVICE(obj
);
173 Property
*prop
= opaque
;
174 uint8_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
177 qdev_prop_set_after_realize(dev
, name
, errp
);
181 visit_type_uint8(v
, ptr
, name
, errp
);
184 PropertyInfo qdev_prop_uint8
= {
190 /* --- 8bit hex value --- */
192 static int parse_hex8(DeviceState
*dev
, Property
*prop
, const char *str
)
194 uint8_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
197 if (str
[0] != '0' || str
[1] != 'x') {
201 *ptr
= strtoul(str
, &end
, 16);
202 if ((*end
!= '\0') || (end
== str
)) {
209 static int print_hex8(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
211 uint8_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
212 return snprintf(dest
, len
, "0x%" PRIx8
, *ptr
);
215 PropertyInfo qdev_prop_hex8
= {
217 .legacy_name
= "hex8",
224 /* --- 16bit integer --- */
226 static void get_uint16(Object
*obj
, Visitor
*v
, void *opaque
,
227 const char *name
, Error
**errp
)
229 DeviceState
*dev
= DEVICE(obj
);
230 Property
*prop
= opaque
;
231 uint16_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
233 visit_type_uint16(v
, ptr
, name
, errp
);
236 static void set_uint16(Object
*obj
, Visitor
*v
, void *opaque
,
237 const char *name
, Error
**errp
)
239 DeviceState
*dev
= DEVICE(obj
);
240 Property
*prop
= opaque
;
241 uint16_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
244 qdev_prop_set_after_realize(dev
, name
, errp
);
248 visit_type_uint16(v
, ptr
, name
, errp
);
251 PropertyInfo qdev_prop_uint16
= {
257 /* --- 32bit integer --- */
259 static void get_uint32(Object
*obj
, Visitor
*v
, void *opaque
,
260 const char *name
, Error
**errp
)
262 DeviceState
*dev
= DEVICE(obj
);
263 Property
*prop
= opaque
;
264 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
266 visit_type_uint32(v
, ptr
, name
, errp
);
269 static void set_uint32(Object
*obj
, Visitor
*v
, void *opaque
,
270 const char *name
, Error
**errp
)
272 DeviceState
*dev
= DEVICE(obj
);
273 Property
*prop
= opaque
;
274 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
277 qdev_prop_set_after_realize(dev
, name
, errp
);
281 visit_type_uint32(v
, ptr
, name
, errp
);
284 static void get_int32(Object
*obj
, Visitor
*v
, void *opaque
,
285 const char *name
, Error
**errp
)
287 DeviceState
*dev
= DEVICE(obj
);
288 Property
*prop
= opaque
;
289 int32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
291 visit_type_int32(v
, ptr
, name
, errp
);
294 static void set_int32(Object
*obj
, Visitor
*v
, void *opaque
,
295 const char *name
, Error
**errp
)
297 DeviceState
*dev
= DEVICE(obj
);
298 Property
*prop
= opaque
;
299 int32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
302 qdev_prop_set_after_realize(dev
, name
, errp
);
306 visit_type_int32(v
, ptr
, name
, errp
);
309 PropertyInfo qdev_prop_uint32
= {
315 PropertyInfo qdev_prop_int32
= {
321 /* --- 32bit hex value --- */
323 static int parse_hex32(DeviceState
*dev
, Property
*prop
, const char *str
)
325 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
328 if (str
[0] != '0' || str
[1] != 'x') {
332 *ptr
= strtoul(str
, &end
, 16);
333 if ((*end
!= '\0') || (end
== str
)) {
340 static int print_hex32(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
342 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
343 return snprintf(dest
, len
, "0x%" PRIx32
, *ptr
);
346 PropertyInfo qdev_prop_hex32
= {
348 .legacy_name
= "hex32",
349 .parse
= parse_hex32
,
350 .print
= print_hex32
,
355 /* --- 64bit integer --- */
357 static void get_uint64(Object
*obj
, Visitor
*v
, void *opaque
,
358 const char *name
, Error
**errp
)
360 DeviceState
*dev
= DEVICE(obj
);
361 Property
*prop
= opaque
;
362 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
364 visit_type_uint64(v
, ptr
, name
, errp
);
367 static void set_uint64(Object
*obj
, Visitor
*v
, void *opaque
,
368 const char *name
, Error
**errp
)
370 DeviceState
*dev
= DEVICE(obj
);
371 Property
*prop
= opaque
;
372 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
375 qdev_prop_set_after_realize(dev
, name
, errp
);
379 visit_type_uint64(v
, ptr
, name
, errp
);
382 PropertyInfo qdev_prop_uint64
= {
388 /* --- 64bit hex value --- */
390 static int parse_hex64(DeviceState
*dev
, Property
*prop
, const char *str
)
392 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
395 if (str
[0] != '0' || str
[1] != 'x') {
399 *ptr
= strtoull(str
, &end
, 16);
400 if ((*end
!= '\0') || (end
== str
)) {
407 static int print_hex64(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
409 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
410 return snprintf(dest
, len
, "0x%" PRIx64
, *ptr
);
413 PropertyInfo qdev_prop_hex64
= {
415 .legacy_name
= "hex64",
416 .parse
= parse_hex64
,
417 .print
= print_hex64
,
424 static void release_string(Object
*obj
, const char *name
, void *opaque
)
426 Property
*prop
= opaque
;
427 g_free(*(char **)qdev_get_prop_ptr(DEVICE(obj
), prop
));
430 static int print_string(DeviceState
*dev
, Property
*prop
, char *dest
,
433 char **ptr
= qdev_get_prop_ptr(dev
, prop
);
435 return snprintf(dest
, len
, "<null>");
437 return snprintf(dest
, len
, "\"%s\"", *ptr
);
440 static void get_string(Object
*obj
, Visitor
*v
, void *opaque
,
441 const char *name
, Error
**errp
)
443 DeviceState
*dev
= DEVICE(obj
);
444 Property
*prop
= opaque
;
445 char **ptr
= qdev_get_prop_ptr(dev
, prop
);
448 char *str
= (char *)"";
449 visit_type_str(v
, &str
, name
, errp
);
451 visit_type_str(v
, ptr
, name
, errp
);
455 static void set_string(Object
*obj
, Visitor
*v
, void *opaque
,
456 const char *name
, Error
**errp
)
458 DeviceState
*dev
= DEVICE(obj
);
459 Property
*prop
= opaque
;
460 char **ptr
= qdev_get_prop_ptr(dev
, prop
);
461 Error
*local_err
= NULL
;
465 qdev_prop_set_after_realize(dev
, name
, errp
);
469 visit_type_str(v
, &str
, name
, &local_err
);
471 error_propagate(errp
, local_err
);
480 PropertyInfo qdev_prop_string
= {
482 .print
= print_string
,
483 .release
= release_string
,
488 /* --- pointer --- */
490 /* Not a proper property, just for dirty hacks. TODO Remove it! */
491 PropertyInfo qdev_prop_ptr
= {
495 /* --- mac address --- */
498 * accepted syntax versions:
502 static void get_mac(Object
*obj
, Visitor
*v
, void *opaque
,
503 const char *name
, Error
**errp
)
505 DeviceState
*dev
= DEVICE(obj
);
506 Property
*prop
= opaque
;
507 MACAddr
*mac
= qdev_get_prop_ptr(dev
, prop
);
508 char buffer
[2 * 6 + 5 + 1];
511 snprintf(buffer
, sizeof(buffer
), "%02x:%02x:%02x:%02x:%02x:%02x",
512 mac
->a
[0], mac
->a
[1], mac
->a
[2],
513 mac
->a
[3], mac
->a
[4], mac
->a
[5]);
515 visit_type_str(v
, &p
, name
, errp
);
518 static void set_mac(Object
*obj
, Visitor
*v
, void *opaque
,
519 const char *name
, Error
**errp
)
521 DeviceState
*dev
= DEVICE(obj
);
522 Property
*prop
= opaque
;
523 MACAddr
*mac
= qdev_get_prop_ptr(dev
, prop
);
524 Error
*local_err
= NULL
;
529 qdev_prop_set_after_realize(dev
, name
, errp
);
533 visit_type_str(v
, &str
, name
, &local_err
);
535 error_propagate(errp
, local_err
);
539 for (i
= 0, pos
= 0; i
< 6; i
++, pos
+= 3) {
540 if (!qemu_isxdigit(str
[pos
])) {
543 if (!qemu_isxdigit(str
[pos
+1])) {
547 if (str
[pos
+2] != '\0') {
551 if (str
[pos
+2] != ':' && str
[pos
+2] != '-') {
555 mac
->a
[i
] = strtol(str
+pos
, &p
, 16);
561 error_set_from_qdev_prop_error(errp
, EINVAL
, dev
, prop
, str
);
565 PropertyInfo qdev_prop_macaddr
= {
571 /* --- lost tick policy --- */
573 static const char *lost_tick_policy_table
[LOST_TICK_MAX
+1] = {
574 [LOST_TICK_DISCARD
] = "discard",
575 [LOST_TICK_DELAY
] = "delay",
576 [LOST_TICK_MERGE
] = "merge",
577 [LOST_TICK_SLEW
] = "slew",
578 [LOST_TICK_MAX
] = NULL
,
581 QEMU_BUILD_BUG_ON(sizeof(LostTickPolicy
) != sizeof(int));
583 PropertyInfo qdev_prop_losttickpolicy
= {
584 .name
= "LostTickPolicy",
585 .enum_table
= lost_tick_policy_table
,
590 /* --- BIOS CHS translation */
592 static const char *bios_chs_trans_table
[] = {
593 [BIOS_ATA_TRANSLATION_AUTO
] = "auto",
594 [BIOS_ATA_TRANSLATION_NONE
] = "none",
595 [BIOS_ATA_TRANSLATION_LBA
] = "lba",
598 PropertyInfo qdev_prop_bios_chs_trans
= {
599 .name
= "bios-chs-trans",
600 .enum_table
= bios_chs_trans_table
,
605 /* --- pci address --- */
608 * bus-local address, i.e. "$slot" or "$slot.$fn"
610 static void set_pci_devfn(Object
*obj
, Visitor
*v
, void *opaque
,
611 const char *name
, Error
**errp
)
613 DeviceState
*dev
= DEVICE(obj
);
614 Property
*prop
= opaque
;
615 int32_t value
, *ptr
= qdev_get_prop_ptr(dev
, prop
);
616 unsigned int slot
, fn
, n
;
617 Error
*local_err
= NULL
;
621 qdev_prop_set_after_realize(dev
, name
, errp
);
625 visit_type_str(v
, &str
, name
, &local_err
);
627 error_free(local_err
);
629 visit_type_int32(v
, &value
, name
, &local_err
);
631 error_propagate(errp
, local_err
);
632 } else if (value
< -1 || value
> 255) {
633 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, name
? name
: "null",
641 if (sscanf(str
, "%x.%x%n", &slot
, &fn
, &n
) != 2) {
643 if (sscanf(str
, "%x%n", &slot
, &n
) != 1) {
647 if (str
[n
] != '\0' || fn
> 7 || slot
> 31) {
650 *ptr
= slot
<< 3 | fn
;
655 error_set_from_qdev_prop_error(errp
, EINVAL
, dev
, prop
, str
);
659 static int print_pci_devfn(DeviceState
*dev
, Property
*prop
, char *dest
,
662 int32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
665 return snprintf(dest
, len
, "<unset>");
667 return snprintf(dest
, len
, "%02x.%x", *ptr
>> 3, *ptr
& 7);
671 PropertyInfo qdev_prop_pci_devfn
= {
673 .legacy_name
= "pci-devfn",
674 .print
= print_pci_devfn
,
676 .set
= set_pci_devfn
,
679 /* --- blocksize --- */
681 static void set_blocksize(Object
*obj
, Visitor
*v
, void *opaque
,
682 const char *name
, Error
**errp
)
684 DeviceState
*dev
= DEVICE(obj
);
685 Property
*prop
= opaque
;
686 uint16_t value
, *ptr
= qdev_get_prop_ptr(dev
, prop
);
687 Error
*local_err
= NULL
;
688 const int64_t min
= 512;
689 const int64_t max
= 32768;
692 qdev_prop_set_after_realize(dev
, name
, errp
);
696 visit_type_uint16(v
, &value
, name
, &local_err
);
698 error_propagate(errp
, local_err
);
701 if (value
< min
|| value
> max
) {
702 error_set(errp
, QERR_PROPERTY_VALUE_OUT_OF_RANGE
,
703 dev
->id
?:"", name
, (int64_t)value
, min
, max
);
707 /* We rely on power-of-2 blocksizes for bitmasks */
708 if ((value
& (value
- 1)) != 0) {
709 error_set(errp
, QERR_PROPERTY_VALUE_NOT_POWER_OF_2
,
710 dev
->id
?:"", name
, (int64_t)value
);
717 PropertyInfo qdev_prop_blocksize
= {
720 .set
= set_blocksize
,
723 /* --- pci host address --- */
725 static void get_pci_host_devaddr(Object
*obj
, Visitor
*v
, void *opaque
,
726 const char *name
, Error
**errp
)
728 DeviceState
*dev
= DEVICE(obj
);
729 Property
*prop
= opaque
;
730 PCIHostDeviceAddress
*addr
= qdev_get_prop_ptr(dev
, prop
);
731 char buffer
[] = "xxxx:xx:xx.x";
735 rc
= snprintf(buffer
, sizeof(buffer
), "%04x:%02x:%02x.%d",
736 addr
->domain
, addr
->bus
, addr
->slot
, addr
->function
);
737 assert(rc
== sizeof(buffer
) - 1);
739 visit_type_str(v
, &p
, name
, errp
);
743 * Parse [<domain>:]<bus>:<slot>.<func>
744 * if <domain> is not supplied, it's assumed to be 0.
746 static void set_pci_host_devaddr(Object
*obj
, Visitor
*v
, void *opaque
,
747 const char *name
, Error
**errp
)
749 DeviceState
*dev
= DEVICE(obj
);
750 Property
*prop
= opaque
;
751 PCIHostDeviceAddress
*addr
= qdev_get_prop_ptr(dev
, prop
);
752 Error
*local_err
= NULL
;
756 unsigned long dom
= 0, bus
= 0;
757 unsigned int slot
= 0, func
= 0;
760 qdev_prop_set_after_realize(dev
, name
, errp
);
764 visit_type_str(v
, &str
, name
, &local_err
);
766 error_propagate(errp
, local_err
);
771 val
= strtoul(p
, &e
, 16);
772 if (e
== p
|| *e
!= ':') {
778 val
= strtoul(p
, &e
, 16);
786 val
= strtoul(p
, &e
, 16);
797 val
= strtoul(p
, &e
, 10);
803 if (dom
> 0xffff || bus
> 0xff || slot
> 0x1f || func
> 7) {
814 addr
->function
= func
;
820 error_set_from_qdev_prop_error(errp
, EINVAL
, dev
, prop
, str
);
824 PropertyInfo qdev_prop_pci_host_devaddr
= {
825 .name
= "pci-host-devaddr",
826 .get
= get_pci_host_devaddr
,
827 .set
= set_pci_host_devaddr
,
830 /* --- support for array properties --- */
832 /* Used as an opaque for the object properties we add for each
833 * array element. Note that the struct Property must be first
834 * in the struct so that a pointer to this works as the opaque
835 * for the underlying element's property hooks as well as for
836 * our own release callback.
839 struct Property prop
;
841 ObjectPropertyRelease
*release
;
842 } ArrayElementProperty
;
844 /* object property release callback for array element properties:
845 * we call the underlying element's property release hook, and
846 * then free the memory we allocated when we added the property.
848 static void array_element_release(Object
*obj
, const char *name
, void *opaque
)
850 ArrayElementProperty
*p
= opaque
;
852 p
->release(obj
, name
, opaque
);
858 static void set_prop_arraylen(Object
*obj
, Visitor
*v
, void *opaque
,
859 const char *name
, Error
**errp
)
861 /* Setter for the property which defines the length of a
862 * variable-sized property array. As well as actually setting the
863 * array-length field in the device struct, we have to create the
864 * array itself and dynamically add the corresponding properties.
866 DeviceState
*dev
= DEVICE(obj
);
867 Property
*prop
= opaque
;
868 uint32_t *alenptr
= qdev_get_prop_ptr(dev
, prop
);
869 void **arrayptr
= (void *)dev
+ prop
->arrayoffset
;
871 const char *arrayname
;
875 qdev_prop_set_after_realize(dev
, name
, errp
);
879 error_setg(errp
, "array size property %s may not be set more than once",
883 visit_type_uint32(v
, alenptr
, name
, errp
);
884 if (error_is_set(errp
)) {
891 /* DEFINE_PROP_ARRAY guarantees that name should start with this prefix;
892 * strip it off so we can get the name of the array itself.
894 assert(strncmp(name
, PROP_ARRAY_LEN_PREFIX
,
895 strlen(PROP_ARRAY_LEN_PREFIX
)) == 0);
896 arrayname
= name
+ strlen(PROP_ARRAY_LEN_PREFIX
);
898 /* Note that it is the responsibility of the individual device's deinit
899 * to free the array proper.
901 *arrayptr
= eltptr
= g_malloc0(*alenptr
* prop
->arrayfieldsize
);
902 for (i
= 0; i
< *alenptr
; i
++, eltptr
+= prop
->arrayfieldsize
) {
903 char *propname
= g_strdup_printf("%s[%d]", arrayname
, i
);
904 ArrayElementProperty
*arrayprop
= g_new0(ArrayElementProperty
, 1);
905 arrayprop
->release
= prop
->arrayinfo
->release
;
906 arrayprop
->propname
= propname
;
907 arrayprop
->prop
.info
= prop
->arrayinfo
;
908 arrayprop
->prop
.name
= propname
;
909 /* This ugly piece of pointer arithmetic sets up the offset so
910 * that when the underlying get/set hooks call qdev_get_prop_ptr
911 * they get the right answer despite the array element not actually
912 * being inside the device struct.
914 arrayprop
->prop
.offset
= eltptr
- (void *)dev
;
915 assert(qdev_get_prop_ptr(dev
, &arrayprop
->prop
) == eltptr
);
916 object_property_add(obj
, propname
,
917 arrayprop
->prop
.info
->name
,
918 arrayprop
->prop
.info
->get
,
919 arrayprop
->prop
.info
->set
,
920 array_element_release
,
922 if (error_is_set(errp
)) {
928 PropertyInfo qdev_prop_arraylen
= {
931 .set
= set_prop_arraylen
,
934 /* --- public helpers --- */
936 static Property
*qdev_prop_walk(Property
*props
, const char *name
)
941 while (props
->name
) {
942 if (strcmp(props
->name
, name
) == 0) {
950 static Property
*qdev_prop_find(DeviceState
*dev
, const char *name
)
955 /* device properties */
956 class = object_get_class(OBJECT(dev
));
958 prop
= qdev_prop_walk(DEVICE_CLASS(class)->props
, name
);
962 class = object_class_get_parent(class);
963 } while (class != object_class_by_name(TYPE_DEVICE
));
968 void error_set_from_qdev_prop_error(Error
**errp
, int ret
, DeviceState
*dev
,
969 Property
*prop
, const char *value
)
973 error_set(errp
, QERR_PROPERTY_VALUE_IN_USE
,
974 object_get_typename(OBJECT(dev
)), prop
->name
, value
);
978 error_set(errp
, QERR_PROPERTY_VALUE_BAD
,
979 object_get_typename(OBJECT(dev
)), prop
->name
, value
);
982 error_set(errp
, QERR_PROPERTY_VALUE_NOT_FOUND
,
983 object_get_typename(OBJECT(dev
)), prop
->name
, value
);
990 void qdev_prop_parse(DeviceState
*dev
, const char *name
, const char *value
,
995 legacy_name
= g_strdup_printf("legacy-%s", name
);
996 if (object_property_get_type(OBJECT(dev
), legacy_name
, NULL
)) {
997 object_property_parse(OBJECT(dev
), value
, legacy_name
, errp
);
999 object_property_parse(OBJECT(dev
), value
, name
, errp
);
1001 g_free(legacy_name
);
1004 void qdev_prop_set_bit(DeviceState
*dev
, const char *name
, bool value
)
1006 object_property_set_bool(OBJECT(dev
), value
, name
, &error_abort
);
1009 void qdev_prop_set_uint8(DeviceState
*dev
, const char *name
, uint8_t value
)
1011 object_property_set_int(OBJECT(dev
), value
, name
, &error_abort
);
1014 void qdev_prop_set_uint16(DeviceState
*dev
, const char *name
, uint16_t value
)
1016 object_property_set_int(OBJECT(dev
), value
, name
, &error_abort
);
1019 void qdev_prop_set_uint32(DeviceState
*dev
, const char *name
, uint32_t value
)
1021 object_property_set_int(OBJECT(dev
), value
, name
, &error_abort
);
1024 void qdev_prop_set_int32(DeviceState
*dev
, const char *name
, int32_t value
)
1026 object_property_set_int(OBJECT(dev
), value
, name
, &error_abort
);
1029 void qdev_prop_set_uint64(DeviceState
*dev
, const char *name
, uint64_t value
)
1031 object_property_set_int(OBJECT(dev
), value
, name
, &error_abort
);
1034 void qdev_prop_set_string(DeviceState
*dev
, const char *name
, const char *value
)
1036 object_property_set_str(OBJECT(dev
), value
, name
, &error_abort
);
1039 void qdev_prop_set_macaddr(DeviceState
*dev
, const char *name
, uint8_t *value
)
1041 char str
[2 * 6 + 5 + 1];
1042 snprintf(str
, sizeof(str
), "%02x:%02x:%02x:%02x:%02x:%02x",
1043 value
[0], value
[1], value
[2], value
[3], value
[4], value
[5]);
1045 object_property_set_str(OBJECT(dev
), str
, name
, &error_abort
);
1048 void qdev_prop_set_enum(DeviceState
*dev
, const char *name
, int value
)
1052 prop
= qdev_prop_find(dev
, name
);
1053 object_property_set_str(OBJECT(dev
), prop
->info
->enum_table
[value
],
1054 name
, &error_abort
);
1057 void qdev_prop_set_ptr(DeviceState
*dev
, const char *name
, void *value
)
1062 prop
= qdev_prop_find(dev
, name
);
1063 assert(prop
&& prop
->info
== &qdev_prop_ptr
);
1064 ptr
= qdev_get_prop_ptr(dev
, prop
);
1068 static QTAILQ_HEAD(, GlobalProperty
) global_props
=
1069 QTAILQ_HEAD_INITIALIZER(global_props
);
1071 void qdev_prop_register_global(GlobalProperty
*prop
)
1073 QTAILQ_INSERT_TAIL(&global_props
, prop
, next
);
1076 void qdev_prop_register_global_list(GlobalProperty
*props
)
1080 for (i
= 0; props
[i
].driver
!= NULL
; i
++) {
1081 qdev_prop_register_global(props
+i
);
1085 void qdev_prop_set_globals_for_type(DeviceState
*dev
, const char *typename
,
1088 GlobalProperty
*prop
;
1090 QTAILQ_FOREACH(prop
, &global_props
, next
) {
1093 if (strcmp(typename
, prop
->driver
) != 0) {
1096 qdev_prop_parse(dev
, prop
->property
, prop
->value
, &err
);
1098 error_propagate(errp
, err
);
1104 void qdev_prop_set_globals(DeviceState
*dev
, Error
**errp
)
1106 ObjectClass
*class = object_get_class(OBJECT(dev
));
1111 qdev_prop_set_globals_for_type(dev
, object_class_get_name(class),
1114 error_propagate(errp
, err
);
1117 class = object_class_get_parent(class);
1121 /* --- 64bit unsigned int 'size' type --- */
1123 static void get_size(Object
*obj
, Visitor
*v
, void *opaque
,
1124 const char *name
, Error
**errp
)
1126 DeviceState
*dev
= DEVICE(obj
);
1127 Property
*prop
= opaque
;
1128 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
1130 visit_type_size(v
, ptr
, name
, errp
);
1133 static void set_size(Object
*obj
, Visitor
*v
, void *opaque
,
1134 const char *name
, Error
**errp
)
1136 DeviceState
*dev
= DEVICE(obj
);
1137 Property
*prop
= opaque
;
1138 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
1140 visit_type_size(v
, ptr
, name
, errp
);
1143 static int parse_size(DeviceState
*dev
, Property
*prop
, const char *str
)
1145 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
1148 parse_option_size(prop
->name
, str
, ptr
, &error_abort
);
1153 static int print_size(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
1155 static const char suffixes
[] = { 'B', 'K', 'M', 'G', 'T' };
1156 uint64_t div
, val
= *(uint64_t *)qdev_get_prop_ptr(dev
, prop
);
1159 /* Compute floor(log2(val)). */
1160 i
= 64 - clz64(val
);
1162 /* Find the power of 1024 that we'll display as the units. */
1164 if (i
>= ARRAY_SIZE(suffixes
)) {
1165 i
= ARRAY_SIZE(suffixes
) - 1;
1167 div
= 1ULL << (i
* 10);
1169 return snprintf(dest
, len
, "%0.03f%c", (double)val
/div
, suffixes
[i
]);
1172 PropertyInfo qdev_prop_size
= {
1174 .parse
= parse_size
,
1175 .print
= print_size
,