5 #include "hw/block-common.h"
8 void *qdev_get_prop_ptr(DeviceState
*dev
, Property
*prop
)
15 static void get_pointer(Object
*obj
, Visitor
*v
, Property
*prop
,
16 const char *(*print
)(void *ptr
),
17 const char *name
, Error
**errp
)
19 DeviceState
*dev
= DEVICE(obj
);
20 void **ptr
= qdev_get_prop_ptr(dev
, prop
);
23 p
= (char *) (*ptr
? print(*ptr
) : "");
24 visit_type_str(v
, &p
, name
, errp
);
27 static void set_pointer(Object
*obj
, Visitor
*v
, Property
*prop
,
28 int (*parse
)(DeviceState
*dev
, const char *str
,
30 const char *name
, Error
**errp
)
32 DeviceState
*dev
= DEVICE(obj
);
33 Error
*local_err
= NULL
;
34 void **ptr
= qdev_get_prop_ptr(dev
, prop
);
38 if (dev
->state
!= DEV_STATE_CREATED
) {
39 error_set(errp
, QERR_PERMISSION_DENIED
);
43 visit_type_str(v
, &str
, name
, &local_err
);
45 error_propagate(errp
, local_err
);
53 ret
= parse(dev
, str
, ptr
);
54 error_set_from_qdev_prop_error(errp
, ret
, dev
, prop
, str
);
58 static void get_enum(Object
*obj
, Visitor
*v
, void *opaque
,
59 const char *name
, Error
**errp
)
61 DeviceState
*dev
= DEVICE(obj
);
62 Property
*prop
= opaque
;
63 int *ptr
= qdev_get_prop_ptr(dev
, prop
);
65 visit_type_enum(v
, ptr
, prop
->info
->enum_table
,
66 prop
->info
->name
, prop
->name
, errp
);
69 static void set_enum(Object
*obj
, Visitor
*v
, void *opaque
,
70 const char *name
, Error
**errp
)
72 DeviceState
*dev
= DEVICE(obj
);
73 Property
*prop
= opaque
;
74 int *ptr
= qdev_get_prop_ptr(dev
, prop
);
76 if (dev
->state
!= DEV_STATE_CREATED
) {
77 error_set(errp
, QERR_PERMISSION_DENIED
);
81 visit_type_enum(v
, ptr
, prop
->info
->enum_table
,
82 prop
->info
->name
, prop
->name
, errp
);
87 static uint32_t qdev_get_prop_mask(Property
*prop
)
89 assert(prop
->info
== &qdev_prop_bit
);
90 return 0x1 << prop
->bitnr
;
93 static void bit_prop_set(DeviceState
*dev
, Property
*props
, bool val
)
95 uint32_t *p
= qdev_get_prop_ptr(dev
, props
);
96 uint32_t mask
= qdev_get_prop_mask(props
);
103 static int print_bit(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
105 uint32_t *p
= qdev_get_prop_ptr(dev
, prop
);
106 return snprintf(dest
, len
, (*p
& qdev_get_prop_mask(prop
)) ? "on" : "off");
109 static void get_bit(Object
*obj
, Visitor
*v
, void *opaque
,
110 const char *name
, Error
**errp
)
112 DeviceState
*dev
= DEVICE(obj
);
113 Property
*prop
= opaque
;
114 uint32_t *p
= qdev_get_prop_ptr(dev
, prop
);
115 bool value
= (*p
& qdev_get_prop_mask(prop
)) != 0;
117 visit_type_bool(v
, &value
, name
, errp
);
120 static void set_bit(Object
*obj
, Visitor
*v
, void *opaque
,
121 const char *name
, Error
**errp
)
123 DeviceState
*dev
= DEVICE(obj
);
124 Property
*prop
= opaque
;
125 Error
*local_err
= NULL
;
128 if (dev
->state
!= DEV_STATE_CREATED
) {
129 error_set(errp
, QERR_PERMISSION_DENIED
);
133 visit_type_bool(v
, &value
, name
, &local_err
);
135 error_propagate(errp
, local_err
);
138 bit_prop_set(dev
, prop
, value
);
141 PropertyInfo qdev_prop_bit
= {
143 .legacy_name
= "on/off",
149 /* --- 8bit integer --- */
151 static void get_uint8(Object
*obj
, Visitor
*v
, void *opaque
,
152 const char *name
, Error
**errp
)
154 DeviceState
*dev
= DEVICE(obj
);
155 Property
*prop
= opaque
;
156 uint8_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
158 visit_type_uint8(v
, ptr
, name
, errp
);
161 static void set_uint8(Object
*obj
, Visitor
*v
, void *opaque
,
162 const char *name
, Error
**errp
)
164 DeviceState
*dev
= DEVICE(obj
);
165 Property
*prop
= opaque
;
166 uint8_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
168 if (dev
->state
!= DEV_STATE_CREATED
) {
169 error_set(errp
, QERR_PERMISSION_DENIED
);
173 visit_type_uint8(v
, ptr
, name
, errp
);
176 PropertyInfo qdev_prop_uint8
= {
182 /* --- 8bit hex value --- */
184 static int parse_hex8(DeviceState
*dev
, Property
*prop
, const char *str
)
186 uint8_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
189 if (str
[0] != '0' || str
[1] != 'x') {
193 *ptr
= strtoul(str
, &end
, 16);
194 if ((*end
!= '\0') || (end
== str
)) {
201 static int print_hex8(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
203 uint8_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
204 return snprintf(dest
, len
, "0x%" PRIx8
, *ptr
);
207 PropertyInfo qdev_prop_hex8
= {
209 .legacy_name
= "hex8",
216 /* --- 16bit integer --- */
218 static void get_uint16(Object
*obj
, Visitor
*v
, void *opaque
,
219 const char *name
, Error
**errp
)
221 DeviceState
*dev
= DEVICE(obj
);
222 Property
*prop
= opaque
;
223 uint16_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
225 visit_type_uint16(v
, ptr
, name
, errp
);
228 static void set_uint16(Object
*obj
, Visitor
*v
, void *opaque
,
229 const char *name
, Error
**errp
)
231 DeviceState
*dev
= DEVICE(obj
);
232 Property
*prop
= opaque
;
233 uint16_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
235 if (dev
->state
!= DEV_STATE_CREATED
) {
236 error_set(errp
, QERR_PERMISSION_DENIED
);
240 visit_type_uint16(v
, ptr
, name
, errp
);
243 PropertyInfo qdev_prop_uint16
= {
249 /* --- 32bit integer --- */
251 static void get_uint32(Object
*obj
, Visitor
*v
, void *opaque
,
252 const char *name
, Error
**errp
)
254 DeviceState
*dev
= DEVICE(obj
);
255 Property
*prop
= opaque
;
256 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
258 visit_type_uint32(v
, ptr
, name
, errp
);
261 static void set_uint32(Object
*obj
, Visitor
*v
, void *opaque
,
262 const char *name
, Error
**errp
)
264 DeviceState
*dev
= DEVICE(obj
);
265 Property
*prop
= opaque
;
266 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
268 if (dev
->state
!= DEV_STATE_CREATED
) {
269 error_set(errp
, QERR_PERMISSION_DENIED
);
273 visit_type_uint32(v
, ptr
, name
, errp
);
276 static void get_int32(Object
*obj
, Visitor
*v
, void *opaque
,
277 const char *name
, Error
**errp
)
279 DeviceState
*dev
= DEVICE(obj
);
280 Property
*prop
= opaque
;
281 int32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
283 visit_type_int32(v
, ptr
, name
, errp
);
286 static void set_int32(Object
*obj
, Visitor
*v
, void *opaque
,
287 const char *name
, Error
**errp
)
289 DeviceState
*dev
= DEVICE(obj
);
290 Property
*prop
= opaque
;
291 int32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
293 if (dev
->state
!= DEV_STATE_CREATED
) {
294 error_set(errp
, QERR_PERMISSION_DENIED
);
298 visit_type_int32(v
, ptr
, name
, errp
);
301 PropertyInfo qdev_prop_uint32
= {
307 PropertyInfo qdev_prop_int32
= {
313 /* --- 32bit hex value --- */
315 static int parse_hex32(DeviceState
*dev
, Property
*prop
, const char *str
)
317 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
320 if (str
[0] != '0' || str
[1] != 'x') {
324 *ptr
= strtoul(str
, &end
, 16);
325 if ((*end
!= '\0') || (end
== str
)) {
332 static int print_hex32(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
334 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
335 return snprintf(dest
, len
, "0x%" PRIx32
, *ptr
);
338 PropertyInfo qdev_prop_hex32
= {
340 .legacy_name
= "hex32",
341 .parse
= parse_hex32
,
342 .print
= print_hex32
,
347 /* --- 64bit integer --- */
349 static void get_uint64(Object
*obj
, Visitor
*v
, void *opaque
,
350 const char *name
, Error
**errp
)
352 DeviceState
*dev
= DEVICE(obj
);
353 Property
*prop
= opaque
;
354 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
356 visit_type_uint64(v
, ptr
, name
, errp
);
359 static void set_uint64(Object
*obj
, Visitor
*v
, void *opaque
,
360 const char *name
, Error
**errp
)
362 DeviceState
*dev
= DEVICE(obj
);
363 Property
*prop
= opaque
;
364 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
366 if (dev
->state
!= DEV_STATE_CREATED
) {
367 error_set(errp
, QERR_PERMISSION_DENIED
);
371 visit_type_uint64(v
, ptr
, name
, errp
);
374 PropertyInfo qdev_prop_uint64
= {
380 /* --- 64bit hex value --- */
382 static int parse_hex64(DeviceState
*dev
, Property
*prop
, const char *str
)
384 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
387 if (str
[0] != '0' || str
[1] != 'x') {
391 *ptr
= strtoull(str
, &end
, 16);
392 if ((*end
!= '\0') || (end
== str
)) {
399 static int print_hex64(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
401 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
402 return snprintf(dest
, len
, "0x%" PRIx64
, *ptr
);
405 PropertyInfo qdev_prop_hex64
= {
407 .legacy_name
= "hex64",
408 .parse
= parse_hex64
,
409 .print
= print_hex64
,
416 static void release_string(Object
*obj
, const char *name
, void *opaque
)
418 Property
*prop
= opaque
;
419 g_free(*(char **)qdev_get_prop_ptr(DEVICE(obj
), prop
));
422 static int print_string(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
424 char **ptr
= qdev_get_prop_ptr(dev
, prop
);
426 return snprintf(dest
, len
, "<null>");
427 return snprintf(dest
, len
, "\"%s\"", *ptr
);
430 static void get_string(Object
*obj
, Visitor
*v
, void *opaque
,
431 const char *name
, Error
**errp
)
433 DeviceState
*dev
= DEVICE(obj
);
434 Property
*prop
= opaque
;
435 char **ptr
= qdev_get_prop_ptr(dev
, prop
);
438 char *str
= (char *)"";
439 visit_type_str(v
, &str
, name
, errp
);
441 visit_type_str(v
, ptr
, name
, errp
);
445 static void set_string(Object
*obj
, Visitor
*v
, void *opaque
,
446 const char *name
, Error
**errp
)
448 DeviceState
*dev
= DEVICE(obj
);
449 Property
*prop
= opaque
;
450 char **ptr
= qdev_get_prop_ptr(dev
, prop
);
451 Error
*local_err
= NULL
;
454 if (dev
->state
!= DEV_STATE_CREATED
) {
455 error_set(errp
, QERR_PERMISSION_DENIED
);
459 visit_type_str(v
, &str
, name
, &local_err
);
461 error_propagate(errp
, local_err
);
470 PropertyInfo qdev_prop_string
= {
472 .print
= print_string
,
473 .release
= release_string
,
480 static int parse_drive(DeviceState
*dev
, const char *str
, void **ptr
)
482 BlockDriverState
*bs
;
487 if (bdrv_attach_dev(bs
, dev
) < 0)
493 static void release_drive(Object
*obj
, const char *name
, void *opaque
)
495 DeviceState
*dev
= DEVICE(obj
);
496 Property
*prop
= opaque
;
497 BlockDriverState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
500 bdrv_detach_dev(*ptr
, dev
);
501 blockdev_auto_del(*ptr
);
505 static const char *print_drive(void *ptr
)
507 return bdrv_get_device_name(ptr
);
510 static void get_drive(Object
*obj
, Visitor
*v
, void *opaque
,
511 const char *name
, Error
**errp
)
513 get_pointer(obj
, v
, opaque
, print_drive
, name
, errp
);
516 static void set_drive(Object
*obj
, Visitor
*v
, void *opaque
,
517 const char *name
, Error
**errp
)
519 set_pointer(obj
, v
, opaque
, parse_drive
, name
, errp
);
522 PropertyInfo qdev_prop_drive
= {
526 .release
= release_drive
,
529 /* --- character device --- */
531 static int parse_chr(DeviceState
*dev
, const char *str
, void **ptr
)
533 CharDriverState
*chr
= qemu_chr_find(str
);
537 if (chr
->avail_connections
< 1) {
541 --chr
->avail_connections
;
545 static void release_chr(Object
*obj
, const char *name
, void *opaque
)
547 DeviceState
*dev
= DEVICE(obj
);
548 Property
*prop
= opaque
;
549 CharDriverState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
552 qemu_chr_add_handlers(*ptr
, NULL
, NULL
, NULL
, NULL
);
557 static const char *print_chr(void *ptr
)
559 CharDriverState
*chr
= ptr
;
561 return chr
->label
? chr
->label
: "";
564 static void get_chr(Object
*obj
, Visitor
*v
, void *opaque
,
565 const char *name
, Error
**errp
)
567 get_pointer(obj
, v
, opaque
, print_chr
, name
, errp
);
570 static void set_chr(Object
*obj
, Visitor
*v
, void *opaque
,
571 const char *name
, Error
**errp
)
573 set_pointer(obj
, v
, opaque
, parse_chr
, name
, errp
);
576 PropertyInfo qdev_prop_chr
= {
580 .release
= release_chr
,
583 /* --- netdev device --- */
585 static int parse_netdev(DeviceState
*dev
, const char *str
, void **ptr
)
587 NetClientState
*netdev
= qemu_find_netdev(str
);
589 if (netdev
== NULL
) {
599 static const char *print_netdev(void *ptr
)
601 NetClientState
*netdev
= ptr
;
603 return netdev
->name
? netdev
->name
: "";
606 static void get_netdev(Object
*obj
, Visitor
*v
, void *opaque
,
607 const char *name
, Error
**errp
)
609 get_pointer(obj
, v
, opaque
, print_netdev
, name
, errp
);
612 static void set_netdev(Object
*obj
, Visitor
*v
, void *opaque
,
613 const char *name
, Error
**errp
)
615 set_pointer(obj
, v
, opaque
, parse_netdev
, name
, errp
);
618 PropertyInfo qdev_prop_netdev
= {
626 static int print_vlan(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
628 NetClientState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
632 if (!net_hub_id_for_client(*ptr
, &id
)) {
633 return snprintf(dest
, len
, "%d", id
);
637 return snprintf(dest
, len
, "<null>");
640 static void get_vlan(Object
*obj
, Visitor
*v
, void *opaque
,
641 const char *name
, Error
**errp
)
643 DeviceState
*dev
= DEVICE(obj
);
644 Property
*prop
= opaque
;
645 NetClientState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
650 if (!net_hub_id_for_client(*ptr
, &hub_id
)) {
655 visit_type_int32(v
, &id
, name
, errp
);
658 static void set_vlan(Object
*obj
, Visitor
*v
, void *opaque
,
659 const char *name
, Error
**errp
)
661 DeviceState
*dev
= DEVICE(obj
);
662 Property
*prop
= opaque
;
663 NetClientState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
664 Error
*local_err
= NULL
;
666 NetClientState
*hubport
;
668 if (dev
->state
!= DEV_STATE_CREATED
) {
669 error_set(errp
, QERR_PERMISSION_DENIED
);
673 visit_type_int32(v
, &id
, name
, &local_err
);
675 error_propagate(errp
, local_err
);
683 hubport
= net_hub_port_find(id
);
685 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
,
686 name
, prop
->info
->name
);
692 PropertyInfo qdev_prop_vlan
= {
699 /* --- pointer --- */
701 /* Not a proper property, just for dirty hacks. TODO Remove it! */
702 PropertyInfo qdev_prop_ptr
= {
706 /* --- mac address --- */
709 * accepted syntax versions:
713 static void get_mac(Object
*obj
, Visitor
*v
, void *opaque
,
714 const char *name
, Error
**errp
)
716 DeviceState
*dev
= DEVICE(obj
);
717 Property
*prop
= opaque
;
718 MACAddr
*mac
= qdev_get_prop_ptr(dev
, prop
);
719 char buffer
[2 * 6 + 5 + 1];
722 snprintf(buffer
, sizeof(buffer
), "%02x:%02x:%02x:%02x:%02x:%02x",
723 mac
->a
[0], mac
->a
[1], mac
->a
[2],
724 mac
->a
[3], mac
->a
[4], mac
->a
[5]);
726 visit_type_str(v
, &p
, name
, errp
);
729 static void set_mac(Object
*obj
, Visitor
*v
, void *opaque
,
730 const char *name
, Error
**errp
)
732 DeviceState
*dev
= DEVICE(obj
);
733 Property
*prop
= opaque
;
734 MACAddr
*mac
= qdev_get_prop_ptr(dev
, prop
);
735 Error
*local_err
= NULL
;
739 if (dev
->state
!= DEV_STATE_CREATED
) {
740 error_set(errp
, QERR_PERMISSION_DENIED
);
744 visit_type_str(v
, &str
, name
, &local_err
);
746 error_propagate(errp
, local_err
);
750 for (i
= 0, pos
= 0; i
< 6; i
++, pos
+= 3) {
751 if (!qemu_isxdigit(str
[pos
]))
753 if (!qemu_isxdigit(str
[pos
+1]))
756 if (str
[pos
+2] != '\0')
759 if (str
[pos
+2] != ':' && str
[pos
+2] != '-')
762 mac
->a
[i
] = strtol(str
+pos
, &p
, 16);
768 error_set_from_qdev_prop_error(errp
, EINVAL
, dev
, prop
, str
);
772 PropertyInfo qdev_prop_macaddr
= {
778 /* --- lost tick policy --- */
780 static const char *lost_tick_policy_table
[LOST_TICK_MAX
+1] = {
781 [LOST_TICK_DISCARD
] = "discard",
782 [LOST_TICK_DELAY
] = "delay",
783 [LOST_TICK_MERGE
] = "merge",
784 [LOST_TICK_SLEW
] = "slew",
785 [LOST_TICK_MAX
] = NULL
,
788 QEMU_BUILD_BUG_ON(sizeof(LostTickPolicy
) != sizeof(int));
790 PropertyInfo qdev_prop_losttickpolicy
= {
791 .name
= "LostTickPolicy",
792 .enum_table
= lost_tick_policy_table
,
797 /* --- BIOS CHS translation */
799 static const char *bios_chs_trans_table
[] = {
800 [BIOS_ATA_TRANSLATION_AUTO
] = "auto",
801 [BIOS_ATA_TRANSLATION_NONE
] = "none",
802 [BIOS_ATA_TRANSLATION_LBA
] = "lba",
805 PropertyInfo qdev_prop_bios_chs_trans
= {
806 .name
= "bios-chs-trans",
807 .enum_table
= bios_chs_trans_table
,
812 /* --- pci address --- */
815 * bus-local address, i.e. "$slot" or "$slot.$fn"
817 static void set_pci_devfn(Object
*obj
, Visitor
*v
, void *opaque
,
818 const char *name
, Error
**errp
)
820 DeviceState
*dev
= DEVICE(obj
);
821 Property
*prop
= opaque
;
822 int32_t value
, *ptr
= qdev_get_prop_ptr(dev
, prop
);
823 unsigned int slot
, fn
, n
;
824 Error
*local_err
= NULL
;
827 if (dev
->state
!= DEV_STATE_CREATED
) {
828 error_set(errp
, QERR_PERMISSION_DENIED
);
832 visit_type_str(v
, &str
, name
, &local_err
);
834 error_free(local_err
);
836 visit_type_int32(v
, &value
, name
, &local_err
);
838 error_propagate(errp
, local_err
);
839 } else if (value
< -1 || value
> 255) {
840 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, name
? name
: "null",
848 if (sscanf(str
, "%x.%x%n", &slot
, &fn
, &n
) != 2) {
850 if (sscanf(str
, "%x%n", &slot
, &n
) != 1) {
854 if (str
[n
] != '\0' || fn
> 7 || slot
> 31) {
857 *ptr
= slot
<< 3 | fn
;
862 error_set_from_qdev_prop_error(errp
, EINVAL
, dev
, prop
, str
);
866 static int print_pci_devfn(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
868 int32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
871 return snprintf(dest
, len
, "<unset>");
873 return snprintf(dest
, len
, "%02x.%x", *ptr
>> 3, *ptr
& 7);
877 PropertyInfo qdev_prop_pci_devfn
= {
879 .legacy_name
= "pci-devfn",
880 .print
= print_pci_devfn
,
882 .set
= set_pci_devfn
,
885 /* --- blocksize --- */
887 static void set_blocksize(Object
*obj
, Visitor
*v
, void *opaque
,
888 const char *name
, Error
**errp
)
890 DeviceState
*dev
= DEVICE(obj
);
891 Property
*prop
= opaque
;
892 uint16_t value
, *ptr
= qdev_get_prop_ptr(dev
, prop
);
893 Error
*local_err
= NULL
;
894 const int64_t min
= 512;
895 const int64_t max
= 32768;
897 if (dev
->state
!= DEV_STATE_CREATED
) {
898 error_set(errp
, QERR_PERMISSION_DENIED
);
902 visit_type_uint16(v
, &value
, name
, &local_err
);
904 error_propagate(errp
, local_err
);
907 if (value
< min
|| value
> max
) {
908 error_set(errp
, QERR_PROPERTY_VALUE_OUT_OF_RANGE
,
909 dev
->id
?:"", name
, (int64_t)value
, min
, max
);
913 /* We rely on power-of-2 blocksizes for bitmasks */
914 if ((value
& (value
- 1)) != 0) {
915 error_set(errp
, QERR_PROPERTY_VALUE_NOT_POWER_OF_2
,
916 dev
->id
?:"", name
, (int64_t)value
);
923 PropertyInfo qdev_prop_blocksize
= {
926 .set
= set_blocksize
,
929 /* --- pci host address --- */
931 static void get_pci_host_devaddr(Object
*obj
, Visitor
*v
, void *opaque
,
932 const char *name
, Error
**errp
)
934 DeviceState
*dev
= DEVICE(obj
);
935 Property
*prop
= opaque
;
936 PCIHostDeviceAddress
*addr
= qdev_get_prop_ptr(dev
, prop
);
937 char buffer
[] = "xxxx:xx:xx.x";
941 rc
= snprintf(buffer
, sizeof(buffer
), "%04x:%02x:%02x.%d",
942 addr
->domain
, addr
->bus
, addr
->slot
, addr
->function
);
943 assert(rc
== sizeof(buffer
) - 1);
945 visit_type_str(v
, &p
, name
, errp
);
949 * Parse [<domain>:]<bus>:<slot>.<func>
950 * if <domain> is not supplied, it's assumed to be 0.
952 static void set_pci_host_devaddr(Object
*obj
, Visitor
*v
, void *opaque
,
953 const char *name
, Error
**errp
)
955 DeviceState
*dev
= DEVICE(obj
);
956 Property
*prop
= opaque
;
957 PCIHostDeviceAddress
*addr
= qdev_get_prop_ptr(dev
, prop
);
958 Error
*local_err
= NULL
;
962 unsigned long dom
= 0, bus
= 0;
963 unsigned int slot
= 0, func
= 0;
965 if (dev
->state
!= DEV_STATE_CREATED
) {
966 error_set(errp
, QERR_PERMISSION_DENIED
);
970 visit_type_str(v
, &str
, name
, &local_err
);
972 error_propagate(errp
, local_err
);
977 val
= strtoul(p
, &e
, 16);
978 if (e
== p
|| *e
!= ':') {
984 val
= strtoul(p
, &e
, 16);
992 val
= strtoul(p
, &e
, 16);
1003 val
= strtoul(p
, &e
, 10);
1009 if (dom
> 0xffff || bus
> 0xff || slot
> 0x1f || func
> 7) {
1020 addr
->function
= func
;
1026 error_set_from_qdev_prop_error(errp
, EINVAL
, dev
, prop
, str
);
1030 PropertyInfo qdev_prop_pci_host_devaddr
= {
1031 .name
= "pci-host-devaddr",
1032 .get
= get_pci_host_devaddr
,
1033 .set
= set_pci_host_devaddr
,
1036 /* --- public helpers --- */
1038 static Property
*qdev_prop_walk(Property
*props
, const char *name
)
1042 while (props
->name
) {
1043 if (strcmp(props
->name
, name
) == 0)
1050 static Property
*qdev_prop_find(DeviceState
*dev
, const char *name
)
1055 /* device properties */
1056 class = object_get_class(OBJECT(dev
));
1058 prop
= qdev_prop_walk(DEVICE_CLASS(class)->props
, name
);
1062 class = object_class_get_parent(class);
1063 } while (class != object_class_by_name(TYPE_DEVICE
));
1068 void error_set_from_qdev_prop_error(Error
**errp
, int ret
, DeviceState
*dev
,
1069 Property
*prop
, const char *value
)
1073 error_set(errp
, QERR_PROPERTY_VALUE_IN_USE
,
1074 object_get_typename(OBJECT(dev
)), prop
->name
, value
);
1078 error_set(errp
, QERR_PROPERTY_VALUE_BAD
,
1079 object_get_typename(OBJECT(dev
)), prop
->name
, value
);
1082 error_set(errp
, QERR_PROPERTY_VALUE_NOT_FOUND
,
1083 object_get_typename(OBJECT(dev
)), prop
->name
, value
);
1090 int qdev_prop_parse(DeviceState
*dev
, const char *name
, const char *value
)
1095 legacy_name
= g_strdup_printf("legacy-%s", name
);
1096 if (object_property_get_type(OBJECT(dev
), legacy_name
, NULL
)) {
1097 object_property_parse(OBJECT(dev
), value
, legacy_name
, &err
);
1099 object_property_parse(OBJECT(dev
), value
, name
, &err
);
1101 g_free(legacy_name
);
1104 qerror_report_err(err
);
1111 void qdev_prop_set_bit(DeviceState
*dev
, const char *name
, bool value
)
1114 object_property_set_bool(OBJECT(dev
), value
, name
, &errp
);
1115 assert_no_error(errp
);
1118 void qdev_prop_set_uint8(DeviceState
*dev
, const char *name
, uint8_t value
)
1121 object_property_set_int(OBJECT(dev
), value
, name
, &errp
);
1122 assert_no_error(errp
);
1125 void qdev_prop_set_uint16(DeviceState
*dev
, const char *name
, uint16_t value
)
1128 object_property_set_int(OBJECT(dev
), value
, name
, &errp
);
1129 assert_no_error(errp
);
1132 void qdev_prop_set_uint32(DeviceState
*dev
, const char *name
, uint32_t value
)
1135 object_property_set_int(OBJECT(dev
), value
, name
, &errp
);
1136 assert_no_error(errp
);
1139 void qdev_prop_set_int32(DeviceState
*dev
, const char *name
, int32_t value
)
1142 object_property_set_int(OBJECT(dev
), value
, name
, &errp
);
1143 assert_no_error(errp
);
1146 void qdev_prop_set_uint64(DeviceState
*dev
, const char *name
, uint64_t value
)
1149 object_property_set_int(OBJECT(dev
), value
, name
, &errp
);
1150 assert_no_error(errp
);
1153 void qdev_prop_set_string(DeviceState
*dev
, const char *name
, const char *value
)
1156 object_property_set_str(OBJECT(dev
), value
, name
, &errp
);
1157 assert_no_error(errp
);
1160 int qdev_prop_set_drive(DeviceState
*dev
, const char *name
, BlockDriverState
*value
)
1163 const char *bdrv_name
= value
? bdrv_get_device_name(value
) : "";
1164 object_property_set_str(OBJECT(dev
), bdrv_name
,
1167 qerror_report_err(errp
);
1174 void qdev_prop_set_drive_nofail(DeviceState
*dev
, const char *name
, BlockDriverState
*value
)
1176 if (qdev_prop_set_drive(dev
, name
, value
) < 0) {
1180 void qdev_prop_set_chr(DeviceState
*dev
, const char *name
, CharDriverState
*value
)
1183 assert(!value
|| value
->label
);
1184 object_property_set_str(OBJECT(dev
),
1185 value
? value
->label
: "", name
, &errp
);
1186 assert_no_error(errp
);
1189 void qdev_prop_set_netdev(DeviceState
*dev
, const char *name
, NetClientState
*value
)
1192 assert(!value
|| value
->name
);
1193 object_property_set_str(OBJECT(dev
),
1194 value
? value
->name
: "", name
, &errp
);
1195 assert_no_error(errp
);
1198 void qdev_prop_set_macaddr(DeviceState
*dev
, const char *name
, uint8_t *value
)
1201 char str
[2 * 6 + 5 + 1];
1202 snprintf(str
, sizeof(str
), "%02x:%02x:%02x:%02x:%02x:%02x",
1203 value
[0], value
[1], value
[2], value
[3], value
[4], value
[5]);
1205 object_property_set_str(OBJECT(dev
), str
, name
, &errp
);
1206 assert_no_error(errp
);
1209 void qdev_prop_set_enum(DeviceState
*dev
, const char *name
, int value
)
1214 prop
= qdev_prop_find(dev
, name
);
1215 object_property_set_str(OBJECT(dev
), prop
->info
->enum_table
[value
],
1217 assert_no_error(errp
);
1220 void qdev_prop_set_ptr(DeviceState
*dev
, const char *name
, void *value
)
1225 prop
= qdev_prop_find(dev
, name
);
1226 assert(prop
&& prop
->info
== &qdev_prop_ptr
);
1227 ptr
= qdev_get_prop_ptr(dev
, prop
);
1231 static QTAILQ_HEAD(, GlobalProperty
) global_props
= QTAILQ_HEAD_INITIALIZER(global_props
);
1233 static void qdev_prop_register_global(GlobalProperty
*prop
)
1235 QTAILQ_INSERT_TAIL(&global_props
, prop
, next
);
1238 void qdev_prop_register_global_list(GlobalProperty
*props
)
1242 for (i
= 0; props
[i
].driver
!= NULL
; i
++) {
1243 qdev_prop_register_global(props
+i
);
1247 void qdev_prop_set_globals(DeviceState
*dev
)
1249 ObjectClass
*class = object_get_class(OBJECT(dev
));
1252 GlobalProperty
*prop
;
1253 QTAILQ_FOREACH(prop
, &global_props
, next
) {
1254 if (strcmp(object_class_get_name(class), prop
->driver
) != 0) {
1257 if (qdev_prop_parse(dev
, prop
->property
, prop
->value
) != 0) {
1261 class = object_class_get_parent(class);
1265 static int qdev_add_one_global(QemuOpts
*opts
, void *opaque
)
1269 g
= g_malloc0(sizeof(*g
));
1270 g
->driver
= qemu_opt_get(opts
, "driver");
1271 g
->property
= qemu_opt_get(opts
, "property");
1272 g
->value
= qemu_opt_get(opts
, "value");
1273 qdev_prop_register_global(g
);
1277 void qemu_add_globals(void)
1279 qemu_opts_foreach(qemu_find_opts("global"), qdev_add_one_global
, NULL
, 0);