6 void *qdev_get_prop_ptr(DeviceState
*dev
, Property
*prop
)
13 static uint32_t qdev_get_prop_mask(Property
*prop
)
15 assert(prop
->info
->type
== PROP_TYPE_BIT
);
16 return 0x1 << prop
->bitnr
;
19 static void bit_prop_set(DeviceState
*dev
, Property
*props
, bool val
)
21 uint32_t *p
= qdev_get_prop_ptr(dev
, props
);
22 uint32_t mask
= qdev_get_prop_mask(props
);
29 static void qdev_prop_cpy(DeviceState
*dev
, Property
*props
, void *src
)
31 if (props
->info
->type
== PROP_TYPE_BIT
) {
33 bit_prop_set(dev
, props
, *defval
);
35 char *dst
= qdev_get_prop_ptr(dev
, props
);
36 memcpy(dst
, src
, props
->info
->size
);
41 static int parse_bit(DeviceState
*dev
, Property
*prop
, const char *str
)
43 if (!strncasecmp(str
, "on", 2))
44 bit_prop_set(dev
, prop
, true);
45 else if (!strncasecmp(str
, "off", 3))
46 bit_prop_set(dev
, prop
, false);
52 static int print_bit(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
54 uint32_t *p
= qdev_get_prop_ptr(dev
, prop
);
55 return snprintf(dest
, len
, (*p
& qdev_get_prop_mask(prop
)) ? "on" : "off");
58 static void get_bit(DeviceState
*dev
, Visitor
*v
, void *opaque
,
59 const char *name
, Error
**errp
)
61 Property
*prop
= opaque
;
62 uint32_t *p
= qdev_get_prop_ptr(dev
, prop
);
63 bool value
= (*p
& qdev_get_prop_mask(prop
)) != 0;
65 visit_type_bool(v
, &value
, name
, errp
);
68 static void set_bit(DeviceState
*dev
, Visitor
*v
, void *opaque
,
69 const char *name
, Error
**errp
)
71 Property
*prop
= opaque
;
72 Error
*local_err
= NULL
;
75 if (dev
->state
!= DEV_STATE_CREATED
) {
76 error_set(errp
, QERR_PERMISSION_DENIED
);
80 visit_type_bool(v
, &value
, name
, &local_err
);
82 error_propagate(errp
, local_err
);
85 bit_prop_set(dev
, prop
, value
);
88 PropertyInfo qdev_prop_bit
= {
90 .legacy_name
= "on/off",
91 .type
= PROP_TYPE_BIT
,
92 .size
= sizeof(uint32_t),
99 /* --- 8bit integer --- */
101 static int parse_uint8(DeviceState
*dev
, Property
*prop
, const char *str
)
103 uint8_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
106 /* accept both hex and decimal */
107 *ptr
= strtoul(str
, &end
, 0);
108 if ((*end
!= '\0') || (end
== str
)) {
115 static int print_uint8(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
117 uint8_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
118 return snprintf(dest
, len
, "%" PRIu8
, *ptr
);
121 static void get_int8(DeviceState
*dev
, Visitor
*v
, void *opaque
,
122 const char *name
, Error
**errp
)
124 Property
*prop
= opaque
;
125 int8_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
129 visit_type_int(v
, &value
, name
, errp
);
132 static void set_int8(DeviceState
*dev
, Visitor
*v
, void *opaque
,
133 const char *name
, Error
**errp
)
135 Property
*prop
= opaque
;
136 int8_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
137 Error
*local_err
= NULL
;
140 if (dev
->state
!= DEV_STATE_CREATED
) {
141 error_set(errp
, QERR_PERMISSION_DENIED
);
145 visit_type_int(v
, &value
, name
, &local_err
);
147 error_propagate(errp
, local_err
);
150 if (value
> prop
->info
->min
&& value
<= prop
->info
->max
) {
153 error_set(errp
, QERR_PROPERTY_VALUE_OUT_OF_RANGE
,
154 dev
->id
?:"", name
, value
, prop
->info
->min
,
159 PropertyInfo qdev_prop_uint8
= {
161 .type
= PROP_TYPE_UINT8
,
162 .size
= sizeof(uint8_t),
163 .parse
= parse_uint8
,
164 .print
= print_uint8
,
171 /* --- 8bit hex value --- */
173 static int parse_hex8(DeviceState
*dev
, Property
*prop
, const char *str
)
175 uint8_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
178 *ptr
= strtoul(str
, &end
, 16);
179 if ((*end
!= '\0') || (end
== str
)) {
186 static int print_hex8(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
188 uint8_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
189 return snprintf(dest
, len
, "0x%" PRIx8
, *ptr
);
192 PropertyInfo qdev_prop_hex8
= {
194 .legacy_name
= "hex8",
195 .type
= PROP_TYPE_UINT8
,
196 .size
= sizeof(uint8_t),
205 /* --- 16bit integer --- */
207 static int parse_uint16(DeviceState
*dev
, Property
*prop
, const char *str
)
209 uint16_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
212 /* accept both hex and decimal */
213 *ptr
= strtoul(str
, &end
, 0);
214 if ((*end
!= '\0') || (end
== str
)) {
221 static int print_uint16(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
223 uint16_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
224 return snprintf(dest
, len
, "%" PRIu16
, *ptr
);
227 static void get_int16(DeviceState
*dev
, Visitor
*v
, void *opaque
,
228 const char *name
, Error
**errp
)
230 Property
*prop
= opaque
;
231 int16_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
235 visit_type_int(v
, &value
, name
, errp
);
238 static void set_int16(DeviceState
*dev
, Visitor
*v
, void *opaque
,
239 const char *name
, Error
**errp
)
241 Property
*prop
= opaque
;
242 int16_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
243 Error
*local_err
= NULL
;
246 if (dev
->state
!= DEV_STATE_CREATED
) {
247 error_set(errp
, QERR_PERMISSION_DENIED
);
251 visit_type_int(v
, &value
, name
, &local_err
);
253 error_propagate(errp
, local_err
);
256 if (value
> prop
->info
->min
&& value
<= prop
->info
->max
) {
259 error_set(errp
, QERR_PROPERTY_VALUE_OUT_OF_RANGE
,
260 dev
->id
?:"", name
, value
, prop
->info
->min
,
265 PropertyInfo qdev_prop_uint16
= {
267 .type
= PROP_TYPE_UINT16
,
268 .size
= sizeof(uint16_t),
269 .parse
= parse_uint16
,
270 .print
= print_uint16
,
277 /* --- 32bit integer --- */
279 static int parse_uint32(DeviceState
*dev
, Property
*prop
, const char *str
)
281 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
284 /* accept both hex and decimal */
285 *ptr
= strtoul(str
, &end
, 0);
286 if ((*end
!= '\0') || (end
== str
)) {
293 static int print_uint32(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
295 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
296 return snprintf(dest
, len
, "%" PRIu32
, *ptr
);
299 static void get_int32(DeviceState
*dev
, Visitor
*v
, void *opaque
,
300 const char *name
, Error
**errp
)
302 Property
*prop
= opaque
;
303 int32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
307 visit_type_int(v
, &value
, name
, errp
);
310 static void set_int32(DeviceState
*dev
, Visitor
*v
, void *opaque
,
311 const char *name
, Error
**errp
)
313 Property
*prop
= opaque
;
314 int32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
315 Error
*local_err
= NULL
;
318 if (dev
->state
!= DEV_STATE_CREATED
) {
319 error_set(errp
, QERR_PERMISSION_DENIED
);
323 visit_type_int(v
, &value
, name
, &local_err
);
325 error_propagate(errp
, local_err
);
328 if (value
> prop
->info
->min
&& value
<= prop
->info
->max
) {
331 error_set(errp
, QERR_PROPERTY_VALUE_OUT_OF_RANGE
,
332 dev
->id
?:"", name
, value
, prop
->info
->min
,
337 PropertyInfo qdev_prop_uint32
= {
339 .type
= PROP_TYPE_UINT32
,
340 .size
= sizeof(uint32_t),
341 .parse
= parse_uint32
,
342 .print
= print_uint32
,
346 .max
= 0xFFFFFFFFULL
,
349 static int parse_int32(DeviceState
*dev
, Property
*prop
, const char *str
)
351 int32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
354 *ptr
= strtol(str
, &end
, 10);
355 if ((*end
!= '\0') || (end
== str
)) {
362 static int print_int32(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
364 int32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
365 return snprintf(dest
, len
, "%" PRId32
, *ptr
);
368 PropertyInfo qdev_prop_int32
= {
370 .type
= PROP_TYPE_INT32
,
371 .size
= sizeof(int32_t),
372 .parse
= parse_int32
,
373 .print
= print_int32
,
376 .min
= -0x80000000LL
,
380 /* --- 32bit hex value --- */
382 static int parse_hex32(DeviceState
*dev
, Property
*prop
, const char *str
)
384 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
387 *ptr
= strtoul(str
, &end
, 16);
388 if ((*end
!= '\0') || (end
== str
)) {
395 static int print_hex32(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
397 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
398 return snprintf(dest
, len
, "0x%" PRIx32
, *ptr
);
401 PropertyInfo qdev_prop_hex32
= {
403 .legacy_name
= "hex32",
404 .type
= PROP_TYPE_UINT32
,
405 .size
= sizeof(uint32_t),
406 .parse
= parse_hex32
,
407 .print
= print_hex32
,
411 .max
= 0xFFFFFFFFULL
,
414 /* --- 64bit integer --- */
416 static int parse_uint64(DeviceState
*dev
, Property
*prop
, const char *str
)
418 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
421 /* accept both hex and decimal */
422 *ptr
= strtoull(str
, &end
, 0);
423 if ((*end
!= '\0') || (end
== str
)) {
430 static int print_uint64(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
432 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
433 return snprintf(dest
, len
, "%" PRIu64
, *ptr
);
436 static void get_int64(DeviceState
*dev
, Visitor
*v
, void *opaque
,
437 const char *name
, Error
**errp
)
439 Property
*prop
= opaque
;
440 int64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
442 visit_type_int(v
, ptr
, name
, errp
);
445 static void set_int64(DeviceState
*dev
, Visitor
*v
, void *opaque
,
446 const char *name
, Error
**errp
)
448 Property
*prop
= opaque
;
449 int64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
451 if (dev
->state
!= DEV_STATE_CREATED
) {
452 error_set(errp
, QERR_PERMISSION_DENIED
);
456 visit_type_int(v
, ptr
, name
, errp
);
459 PropertyInfo qdev_prop_uint64
= {
461 .type
= PROP_TYPE_UINT64
,
462 .size
= sizeof(uint64_t),
463 .parse
= parse_uint64
,
464 .print
= print_uint64
,
469 /* --- 64bit hex value --- */
471 static int parse_hex64(DeviceState
*dev
, Property
*prop
, const char *str
)
473 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
476 *ptr
= strtoull(str
, &end
, 16);
477 if ((*end
!= '\0') || (end
== str
)) {
484 static int print_hex64(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
486 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
487 return snprintf(dest
, len
, "0x%" PRIx64
, *ptr
);
490 PropertyInfo qdev_prop_hex64
= {
492 .legacy_name
= "hex64",
493 .type
= PROP_TYPE_UINT64
,
494 .size
= sizeof(uint64_t),
495 .parse
= parse_hex64
,
496 .print
= print_hex64
,
503 static int parse_string(DeviceState
*dev
, Property
*prop
, const char *str
)
505 char **ptr
= qdev_get_prop_ptr(dev
, prop
);
509 *ptr
= g_strdup(str
);
513 static void free_string(DeviceState
*dev
, Property
*prop
)
515 g_free(*(char **)qdev_get_prop_ptr(dev
, prop
));
518 static int print_string(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
520 char **ptr
= qdev_get_prop_ptr(dev
, prop
);
522 return snprintf(dest
, len
, "<null>");
523 return snprintf(dest
, len
, "\"%s\"", *ptr
);
526 static void get_string(DeviceState
*dev
, Visitor
*v
, void *opaque
,
527 const char *name
, Error
**errp
)
529 Property
*prop
= opaque
;
530 char **ptr
= qdev_get_prop_ptr(dev
, prop
);
533 char *str
= (char *)"";
534 visit_type_str(v
, &str
, name
, errp
);
536 visit_type_str(v
, ptr
, name
, errp
);
540 static void set_string(DeviceState
*dev
, Visitor
*v
, void *opaque
,
541 const char *name
, Error
**errp
)
543 Property
*prop
= opaque
;
544 char **ptr
= qdev_get_prop_ptr(dev
, prop
);
545 Error
*local_err
= NULL
;
548 if (dev
->state
!= DEV_STATE_CREATED
) {
549 error_set(errp
, QERR_PERMISSION_DENIED
);
553 visit_type_str(v
, &str
, name
, &local_err
);
555 error_propagate(errp
, local_err
);
568 PropertyInfo qdev_prop_string
= {
570 .type
= PROP_TYPE_STRING
,
571 .size
= sizeof(char*),
572 .parse
= parse_string
,
573 .print
= print_string
,
581 static int parse_drive(DeviceState
*dev
, Property
*prop
, const char *str
)
583 BlockDriverState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
584 BlockDriverState
*bs
;
589 if (bdrv_attach_dev(bs
, dev
) < 0)
595 static void free_drive(DeviceState
*dev
, Property
*prop
)
597 BlockDriverState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
600 bdrv_detach_dev(*ptr
, dev
);
601 blockdev_auto_del(*ptr
);
605 static int print_drive(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
607 BlockDriverState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
608 return snprintf(dest
, len
, "%s",
609 *ptr
? bdrv_get_device_name(*ptr
) : "<null>");
612 static void get_generic(DeviceState
*dev
, Visitor
*v
, void *opaque
,
613 const char *name
, Error
**errp
)
615 Property
*prop
= opaque
;
616 void **ptr
= qdev_get_prop_ptr(dev
, prop
);
622 prop
->info
->print(dev
, prop
, buffer
, sizeof(buffer
));
624 visit_type_str(v
, &p
, name
, errp
);
627 static void set_generic(DeviceState
*dev
, Visitor
*v
, void *opaque
,
628 const char *name
, Error
**errp
)
630 Property
*prop
= opaque
;
631 Error
*local_err
= NULL
;
635 if (dev
->state
!= DEV_STATE_CREATED
) {
636 error_set(errp
, QERR_PERMISSION_DENIED
);
640 visit_type_str(v
, &str
, name
, &local_err
);
642 error_propagate(errp
, local_err
);
647 error_set_from_qdev_prop_error(errp
, EINVAL
, dev
, prop
, str
);
650 ret
= prop
->info
->parse(dev
, prop
, str
);
651 error_set_from_qdev_prop_error(errp
, ret
, dev
, prop
, str
);
655 PropertyInfo qdev_prop_drive
= {
657 .type
= PROP_TYPE_DRIVE
,
658 .size
= sizeof(BlockDriverState
*),
659 .parse
= parse_drive
,
660 .print
= print_drive
,
666 /* --- character device --- */
668 static int parse_chr(DeviceState
*dev
, Property
*prop
, const char *str
)
670 CharDriverState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
672 *ptr
= qemu_chr_find(str
);
676 if ((*ptr
)->avail_connections
< 1) {
679 --(*ptr
)->avail_connections
;
683 static void free_chr(DeviceState
*dev
, Property
*prop
)
685 CharDriverState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
688 qemu_chr_add_handlers(*ptr
, NULL
, NULL
, NULL
, NULL
);
693 static int print_chr(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
695 CharDriverState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
697 if (*ptr
&& (*ptr
)->label
) {
698 return snprintf(dest
, len
, "%s", (*ptr
)->label
);
700 return snprintf(dest
, len
, "<null>");
704 PropertyInfo qdev_prop_chr
= {
706 .type
= PROP_TYPE_CHR
,
707 .size
= sizeof(CharDriverState
*),
715 /* --- netdev device --- */
717 static int parse_netdev(DeviceState
*dev
, Property
*prop
, const char *str
)
719 VLANClientState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
721 *ptr
= qemu_find_netdev(str
);
730 static int print_netdev(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
732 VLANClientState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
734 if (*ptr
&& (*ptr
)->name
) {
735 return snprintf(dest
, len
, "%s", (*ptr
)->name
);
737 return snprintf(dest
, len
, "<null>");
741 PropertyInfo qdev_prop_netdev
= {
743 .type
= PROP_TYPE_NETDEV
,
744 .size
= sizeof(VLANClientState
*),
745 .parse
= parse_netdev
,
746 .print
= print_netdev
,
753 static int parse_vlan(DeviceState
*dev
, Property
*prop
, const char *str
)
755 VLANState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
758 if (sscanf(str
, "%d", &id
) != 1)
760 *ptr
= qemu_find_vlan(id
, 1);
766 static int print_vlan(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
768 VLANState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
771 return snprintf(dest
, len
, "%d", (*ptr
)->id
);
773 return snprintf(dest
, len
, "<null>");
777 static void get_vlan(DeviceState
*dev
, Visitor
*v
, void *opaque
,
778 const char *name
, Error
**errp
)
780 Property
*prop
= opaque
;
781 VLANState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
784 id
= *ptr
? (*ptr
)->id
: -1;
785 visit_type_int(v
, &id
, name
, errp
);
788 static void set_vlan(DeviceState
*dev
, Visitor
*v
, void *opaque
,
789 const char *name
, Error
**errp
)
791 Property
*prop
= opaque
;
792 VLANState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
793 Error
*local_err
= NULL
;
797 if (dev
->state
!= DEV_STATE_CREATED
) {
798 error_set(errp
, QERR_PERMISSION_DENIED
);
802 visit_type_int(v
, &id
, name
, &local_err
);
804 error_propagate(errp
, local_err
);
811 vlan
= qemu_find_vlan(id
, 1);
813 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
,
814 name
, prop
->info
->name
);
820 PropertyInfo qdev_prop_vlan
= {
822 .type
= PROP_TYPE_VLAN
,
823 .size
= sizeof(VLANClientState
*),
830 /* --- pointer --- */
832 /* Not a proper property, just for dirty hacks. TODO Remove it! */
833 PropertyInfo qdev_prop_ptr
= {
835 .type
= PROP_TYPE_PTR
,
836 .size
= sizeof(void*),
839 /* --- mac address --- */
842 * accepted syntax versions:
846 static int parse_mac(DeviceState
*dev
, Property
*prop
, const char *str
)
848 MACAddr
*mac
= qdev_get_prop_ptr(dev
, prop
);
852 for (i
= 0, pos
= 0; i
< 6; i
++, pos
+= 3) {
853 if (!qemu_isxdigit(str
[pos
]))
855 if (!qemu_isxdigit(str
[pos
+1]))
858 if (str
[pos
+2] != '\0')
861 if (str
[pos
+2] != ':' && str
[pos
+2] != '-')
864 mac
->a
[i
] = strtol(str
+pos
, &p
, 16);
869 static int print_mac(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
871 MACAddr
*mac
= qdev_get_prop_ptr(dev
, prop
);
873 return snprintf(dest
, len
, "%02x:%02x:%02x:%02x:%02x:%02x",
874 mac
->a
[0], mac
->a
[1], mac
->a
[2],
875 mac
->a
[3], mac
->a
[4], mac
->a
[5]);
878 PropertyInfo qdev_prop_macaddr
= {
880 .type
= PROP_TYPE_MACADDR
,
881 .size
= sizeof(MACAddr
),
888 /* --- pci address --- */
891 * bus-local address, i.e. "$slot" or "$slot.$fn"
893 static int parse_pci_devfn(DeviceState
*dev
, Property
*prop
, const char *str
)
895 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
896 unsigned int slot
, fn
, n
;
898 if (sscanf(str
, "%x.%x%n", &slot
, &fn
, &n
) != 2) {
900 if (sscanf(str
, "%x%n", &slot
, &n
) != 1) {
910 *ptr
= slot
<< 3 | fn
;
914 static int print_pci_devfn(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
916 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
919 return snprintf(dest
, len
, "<unset>");
921 return snprintf(dest
, len
, "%02x.%x", *ptr
>> 3, *ptr
& 7);
925 static void get_pci_devfn(DeviceState
*dev
, Visitor
*v
, void *opaque
,
926 const char *name
, Error
**errp
)
928 Property
*prop
= opaque
;
929 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
935 snprintf(buffer
, sizeof(buffer
), "%02x.%x", *ptr
>> 3, *ptr
& 7);
937 visit_type_str(v
, &p
, name
, errp
);
940 PropertyInfo qdev_prop_pci_devfn
= {
942 .type
= PROP_TYPE_UINT32
,
943 .size
= sizeof(uint32_t),
944 .parse
= parse_pci_devfn
,
945 .print
= print_pci_devfn
,
946 .get
= get_pci_devfn
,
950 /* --- public helpers --- */
952 static Property
*qdev_prop_walk(Property
*props
, const char *name
)
956 while (props
->name
) {
957 if (strcmp(props
->name
, name
) == 0)
964 static Property
*qdev_prop_find(DeviceState
*dev
, const char *name
)
968 /* device properties */
969 prop
= qdev_prop_walk(dev
->info
->props
, name
);
974 prop
= qdev_prop_walk(dev
->parent_bus
->info
->props
, name
);
981 int qdev_prop_exists(DeviceState
*dev
, const char *name
)
983 return qdev_prop_find(dev
, name
) ? true : false;
986 void error_set_from_qdev_prop_error(Error
**errp
, int ret
, DeviceState
*dev
,
987 Property
*prop
, const char *value
)
991 error_set(errp
, QERR_PROPERTY_VALUE_IN_USE
,
992 dev
->info
->name
, prop
->name
, value
);
996 error_set(errp
, QERR_PROPERTY_VALUE_BAD
,
997 dev
->info
->name
, prop
->name
, value
);
1000 error_set(errp
, QERR_PROPERTY_VALUE_NOT_FOUND
,
1001 dev
->info
->name
, prop
->name
, value
);
1008 int qdev_prop_parse(DeviceState
*dev
, const char *name
, const char *value
)
1013 prop
= qdev_prop_find(dev
, name
);
1015 * TODO Properties without a parse method are just for dirty
1016 * hacks. qdev_prop_ptr is the only such PropertyInfo. It's
1017 * marked for removal. The test !prop->info->parse should be
1018 * removed along with it.
1020 if (!prop
|| !prop
->info
->parse
) {
1021 qerror_report(QERR_PROPERTY_NOT_FOUND
, dev
->info
->name
, name
);
1024 ret
= prop
->info
->parse(dev
, prop
, value
);
1027 error_set_from_qdev_prop_error(&err
, ret
, dev
, prop
, value
);
1028 qerror_report_err(err
);
1035 void qdev_prop_set(DeviceState
*dev
, const char *name
, void *src
, enum PropertyType type
)
1039 prop
= qdev_prop_find(dev
, name
);
1041 fprintf(stderr
, "%s: property \"%s.%s\" not found\n",
1042 __FUNCTION__
, dev
->info
->name
, name
);
1045 if (prop
->info
->type
!= type
) {
1046 fprintf(stderr
, "%s: property \"%s.%s\" type mismatch\n",
1047 __FUNCTION__
, dev
->info
->name
, name
);
1050 qdev_prop_cpy(dev
, prop
, src
);
1053 void qdev_prop_set_bit(DeviceState
*dev
, const char *name
, bool value
)
1055 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_BIT
);
1058 void qdev_prop_set_uint8(DeviceState
*dev
, const char *name
, uint8_t value
)
1060 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_UINT8
);
1063 void qdev_prop_set_uint16(DeviceState
*dev
, const char *name
, uint16_t value
)
1065 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_UINT16
);
1068 void qdev_prop_set_uint32(DeviceState
*dev
, const char *name
, uint32_t value
)
1070 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_UINT32
);
1073 void qdev_prop_set_int32(DeviceState
*dev
, const char *name
, int32_t value
)
1075 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_INT32
);
1078 void qdev_prop_set_uint64(DeviceState
*dev
, const char *name
, uint64_t value
)
1080 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_UINT64
);
1083 void qdev_prop_set_string(DeviceState
*dev
, const char *name
, char *value
)
1085 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_STRING
);
1088 int qdev_prop_set_drive(DeviceState
*dev
, const char *name
, BlockDriverState
*value
)
1092 res
= bdrv_attach_dev(value
, dev
);
1094 error_report("Can't attach drive %s to %s.%s: %s",
1095 bdrv_get_device_name(value
),
1096 dev
->id
? dev
->id
: dev
->info
->name
,
1097 name
, strerror(-res
));
1100 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_DRIVE
);
1104 void qdev_prop_set_drive_nofail(DeviceState
*dev
, const char *name
, BlockDriverState
*value
)
1106 if (qdev_prop_set_drive(dev
, name
, value
) < 0) {
1110 void qdev_prop_set_chr(DeviceState
*dev
, const char *name
, CharDriverState
*value
)
1112 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_CHR
);
1115 void qdev_prop_set_netdev(DeviceState
*dev
, const char *name
, VLANClientState
*value
)
1117 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_NETDEV
);
1120 void qdev_prop_set_vlan(DeviceState
*dev
, const char *name
, VLANState
*value
)
1122 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_VLAN
);
1125 void qdev_prop_set_macaddr(DeviceState
*dev
, const char *name
, uint8_t *value
)
1127 qdev_prop_set(dev
, name
, value
, PROP_TYPE_MACADDR
);
1130 void qdev_prop_set_ptr(DeviceState
*dev
, const char *name
, void *value
)
1132 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_PTR
);
1135 void qdev_prop_set_defaults(DeviceState
*dev
, Property
*props
)
1139 while (props
->name
) {
1140 if (props
->defval
) {
1141 qdev_prop_cpy(dev
, props
, props
->defval
);
1147 static QTAILQ_HEAD(, GlobalProperty
) global_props
= QTAILQ_HEAD_INITIALIZER(global_props
);
1149 static void qdev_prop_register_global(GlobalProperty
*prop
)
1151 QTAILQ_INSERT_TAIL(&global_props
, prop
, next
);
1154 void qdev_prop_register_global_list(GlobalProperty
*props
)
1158 for (i
= 0; props
[i
].driver
!= NULL
; i
++) {
1159 qdev_prop_register_global(props
+i
);
1163 void qdev_prop_set_globals(DeviceState
*dev
)
1165 GlobalProperty
*prop
;
1167 QTAILQ_FOREACH(prop
, &global_props
, next
) {
1168 if (strcmp(dev
->info
->name
, prop
->driver
) != 0 &&
1169 strcmp(dev
->info
->bus_info
->name
, prop
->driver
) != 0) {
1172 if (qdev_prop_parse(dev
, prop
->property
, prop
->value
) != 0) {
1178 static int qdev_add_one_global(QemuOpts
*opts
, void *opaque
)
1182 g
= g_malloc0(sizeof(*g
));
1183 g
->driver
= qemu_opt_get(opts
, "driver");
1184 g
->property
= qemu_opt_get(opts
, "property");
1185 g
->value
= qemu_opt_get(opts
, "value");
1186 qdev_prop_register_global(g
);
1190 void qemu_add_globals(void)
1192 qemu_opts_foreach(qemu_find_opts("global"), qdev_add_one_global
, NULL
, 0);