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 PropertyInfo qdev_prop_bit
= {
60 .type
= PROP_TYPE_BIT
,
61 .size
= sizeof(uint32_t),
66 /* --- 8bit integer --- */
68 static int parse_uint8(DeviceState
*dev
, Property
*prop
, const char *str
)
70 uint8_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
73 /* accept both hex and decimal */
74 *ptr
= strtoul(str
, &end
, 0);
75 if ((*end
!= '\0') || (end
== str
)) {
82 static int print_uint8(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
84 uint8_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
85 return snprintf(dest
, len
, "%" PRIu8
, *ptr
);
88 PropertyInfo qdev_prop_uint8
= {
90 .type
= PROP_TYPE_UINT8
,
91 .size
= sizeof(uint8_t),
96 /* --- 8bit hex value --- */
98 static int parse_hex8(DeviceState
*dev
, Property
*prop
, const char *str
)
100 uint8_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
103 *ptr
= strtoul(str
, &end
, 16);
104 if ((*end
!= '\0') || (end
== str
)) {
111 static int print_hex8(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
113 uint8_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
114 return snprintf(dest
, len
, "0x%" PRIx8
, *ptr
);
117 PropertyInfo qdev_prop_hex8
= {
119 .type
= PROP_TYPE_UINT8
,
120 .size
= sizeof(uint8_t),
125 /* --- 16bit integer --- */
127 static int parse_uint16(DeviceState
*dev
, Property
*prop
, const char *str
)
129 uint16_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
132 /* accept both hex and decimal */
133 *ptr
= strtoul(str
, &end
, 0);
134 if ((*end
!= '\0') || (end
== str
)) {
141 static int print_uint16(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
143 uint16_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
144 return snprintf(dest
, len
, "%" PRIu16
, *ptr
);
147 PropertyInfo qdev_prop_uint16
= {
149 .type
= PROP_TYPE_UINT16
,
150 .size
= sizeof(uint16_t),
151 .parse
= parse_uint16
,
152 .print
= print_uint16
,
155 /* --- 32bit integer --- */
157 static int parse_uint32(DeviceState
*dev
, Property
*prop
, const char *str
)
159 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
162 /* accept both hex and decimal */
163 *ptr
= strtoul(str
, &end
, 0);
164 if ((*end
!= '\0') || (end
== str
)) {
171 static int print_uint32(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
173 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
174 return snprintf(dest
, len
, "%" PRIu32
, *ptr
);
177 PropertyInfo qdev_prop_uint32
= {
179 .type
= PROP_TYPE_UINT32
,
180 .size
= sizeof(uint32_t),
181 .parse
= parse_uint32
,
182 .print
= print_uint32
,
185 static int parse_int32(DeviceState
*dev
, Property
*prop
, const char *str
)
187 int32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
190 *ptr
= strtol(str
, &end
, 10);
191 if ((*end
!= '\0') || (end
== str
)) {
198 static int print_int32(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
200 int32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
201 return snprintf(dest
, len
, "%" PRId32
, *ptr
);
204 PropertyInfo qdev_prop_int32
= {
206 .type
= PROP_TYPE_INT32
,
207 .size
= sizeof(int32_t),
208 .parse
= parse_int32
,
209 .print
= print_int32
,
212 /* --- 32bit hex value --- */
214 static int parse_hex32(DeviceState
*dev
, Property
*prop
, const char *str
)
216 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
219 *ptr
= strtoul(str
, &end
, 16);
220 if ((*end
!= '\0') || (end
== str
)) {
227 static int print_hex32(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
229 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
230 return snprintf(dest
, len
, "0x%" PRIx32
, *ptr
);
233 PropertyInfo qdev_prop_hex32
= {
235 .type
= PROP_TYPE_UINT32
,
236 .size
= sizeof(uint32_t),
237 .parse
= parse_hex32
,
238 .print
= print_hex32
,
241 /* --- 64bit integer --- */
243 static int parse_uint64(DeviceState
*dev
, Property
*prop
, const char *str
)
245 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
248 /* accept both hex and decimal */
249 *ptr
= strtoull(str
, &end
, 0);
250 if ((*end
!= '\0') || (end
== str
)) {
257 static int print_uint64(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
259 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
260 return snprintf(dest
, len
, "%" PRIu64
, *ptr
);
263 PropertyInfo qdev_prop_uint64
= {
265 .type
= PROP_TYPE_UINT64
,
266 .size
= sizeof(uint64_t),
267 .parse
= parse_uint64
,
268 .print
= print_uint64
,
271 /* --- 64bit hex value --- */
273 static int parse_hex64(DeviceState
*dev
, Property
*prop
, const char *str
)
275 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
278 *ptr
= strtoull(str
, &end
, 16);
279 if ((*end
!= '\0') || (end
== str
)) {
286 static int print_hex64(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
288 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
289 return snprintf(dest
, len
, "0x%" PRIx64
, *ptr
);
292 PropertyInfo qdev_prop_hex64
= {
294 .type
= PROP_TYPE_UINT64
,
295 .size
= sizeof(uint64_t),
296 .parse
= parse_hex64
,
297 .print
= print_hex64
,
302 static int parse_string(DeviceState
*dev
, Property
*prop
, const char *str
)
304 char **ptr
= qdev_get_prop_ptr(dev
, prop
);
308 *ptr
= g_strdup(str
);
312 static void free_string(DeviceState
*dev
, Property
*prop
)
314 g_free(*(char **)qdev_get_prop_ptr(dev
, prop
));
317 static int print_string(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
319 char **ptr
= qdev_get_prop_ptr(dev
, prop
);
321 return snprintf(dest
, len
, "<null>");
322 return snprintf(dest
, len
, "\"%s\"", *ptr
);
325 PropertyInfo qdev_prop_string
= {
327 .type
= PROP_TYPE_STRING
,
328 .size
= sizeof(char*),
329 .parse
= parse_string
,
330 .print
= print_string
,
336 static int parse_drive(DeviceState
*dev
, Property
*prop
, const char *str
)
338 BlockDriverState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
339 BlockDriverState
*bs
;
344 if (bdrv_attach_dev(bs
, dev
) < 0)
350 static void free_drive(DeviceState
*dev
, Property
*prop
)
352 BlockDriverState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
355 bdrv_detach_dev(*ptr
, dev
);
356 blockdev_auto_del(*ptr
);
360 static int print_drive(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
362 BlockDriverState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
363 return snprintf(dest
, len
, "%s",
364 *ptr
? bdrv_get_device_name(*ptr
) : "<null>");
367 PropertyInfo qdev_prop_drive
= {
369 .type
= PROP_TYPE_DRIVE
,
370 .size
= sizeof(BlockDriverState
*),
371 .parse
= parse_drive
,
372 .print
= print_drive
,
376 /* --- character device --- */
378 static int parse_chr(DeviceState
*dev
, Property
*prop
, const char *str
)
380 CharDriverState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
382 *ptr
= qemu_chr_find(str
);
386 if ((*ptr
)->avail_connections
< 1) {
389 --(*ptr
)->avail_connections
;
393 static int print_chr(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
395 CharDriverState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
397 if (*ptr
&& (*ptr
)->label
) {
398 return snprintf(dest
, len
, "%s", (*ptr
)->label
);
400 return snprintf(dest
, len
, "<null>");
404 PropertyInfo qdev_prop_chr
= {
406 .type
= PROP_TYPE_CHR
,
407 .size
= sizeof(CharDriverState
*),
412 /* --- netdev device --- */
414 static int parse_netdev(DeviceState
*dev
, Property
*prop
, const char *str
)
416 VLANClientState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
418 *ptr
= qemu_find_netdev(str
);
427 static int print_netdev(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
429 VLANClientState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
431 if (*ptr
&& (*ptr
)->name
) {
432 return snprintf(dest
, len
, "%s", (*ptr
)->name
);
434 return snprintf(dest
, len
, "<null>");
438 PropertyInfo qdev_prop_netdev
= {
440 .type
= PROP_TYPE_NETDEV
,
441 .size
= sizeof(VLANClientState
*),
442 .parse
= parse_netdev
,
443 .print
= print_netdev
,
448 static int parse_vlan(DeviceState
*dev
, Property
*prop
, const char *str
)
450 VLANState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
453 if (sscanf(str
, "%d", &id
) != 1)
455 *ptr
= qemu_find_vlan(id
, 1);
461 static int print_vlan(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
463 VLANState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
466 return snprintf(dest
, len
, "%d", (*ptr
)->id
);
468 return snprintf(dest
, len
, "<null>");
472 PropertyInfo qdev_prop_vlan
= {
474 .type
= PROP_TYPE_VLAN
,
475 .size
= sizeof(VLANClientState
*),
480 /* --- pointer --- */
482 /* Not a proper property, just for dirty hacks. TODO Remove it! */
483 PropertyInfo qdev_prop_ptr
= {
485 .type
= PROP_TYPE_PTR
,
486 .size
= sizeof(void*),
489 /* --- mac address --- */
492 * accepted syntax versions:
496 static int parse_mac(DeviceState
*dev
, Property
*prop
, const char *str
)
498 MACAddr
*mac
= qdev_get_prop_ptr(dev
, prop
);
502 for (i
= 0, pos
= 0; i
< 6; i
++, pos
+= 3) {
503 if (!qemu_isxdigit(str
[pos
]))
505 if (!qemu_isxdigit(str
[pos
+1]))
508 if (str
[pos
+2] != '\0')
511 if (str
[pos
+2] != ':' && str
[pos
+2] != '-')
514 mac
->a
[i
] = strtol(str
+pos
, &p
, 16);
519 static int print_mac(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
521 MACAddr
*mac
= qdev_get_prop_ptr(dev
, prop
);
523 return snprintf(dest
, len
, "%02x:%02x:%02x:%02x:%02x:%02x",
524 mac
->a
[0], mac
->a
[1], mac
->a
[2],
525 mac
->a
[3], mac
->a
[4], mac
->a
[5]);
528 PropertyInfo qdev_prop_macaddr
= {
530 .type
= PROP_TYPE_MACADDR
,
531 .size
= sizeof(MACAddr
),
536 /* --- pci address --- */
539 * bus-local address, i.e. "$slot" or "$slot.$fn"
541 static int parse_pci_devfn(DeviceState
*dev
, Property
*prop
, const char *str
)
543 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
544 unsigned int slot
, fn
, n
;
546 if (sscanf(str
, "%x.%x%n", &slot
, &fn
, &n
) != 2) {
548 if (sscanf(str
, "%x%n", &slot
, &n
) != 1) {
558 *ptr
= slot
<< 3 | fn
;
562 static int print_pci_devfn(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
564 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
567 return snprintf(dest
, len
, "<unset>");
569 return snprintf(dest
, len
, "%02x.%x", *ptr
>> 3, *ptr
& 7);
573 PropertyInfo qdev_prop_pci_devfn
= {
575 .type
= PROP_TYPE_UINT32
,
576 .size
= sizeof(uint32_t),
577 .parse
= parse_pci_devfn
,
578 .print
= print_pci_devfn
,
581 /* --- public helpers --- */
583 static Property
*qdev_prop_walk(Property
*props
, const char *name
)
587 while (props
->name
) {
588 if (strcmp(props
->name
, name
) == 0)
595 static Property
*qdev_prop_find(DeviceState
*dev
, const char *name
)
599 /* device properties */
600 prop
= qdev_prop_walk(dev
->info
->props
, name
);
605 prop
= qdev_prop_walk(dev
->parent_bus
->info
->props
, name
);
612 int qdev_prop_exists(DeviceState
*dev
, const char *name
)
614 return qdev_prop_find(dev
, name
) ? true : false;
617 int qdev_prop_parse(DeviceState
*dev
, const char *name
, const char *value
)
622 prop
= qdev_prop_find(dev
, name
);
624 * TODO Properties without a parse method are just for dirty
625 * hacks. qdev_prop_ptr is the only such PropertyInfo. It's
626 * marked for removal. The test !prop->info->parse should be
627 * removed along with it.
629 if (!prop
|| !prop
->info
->parse
) {
630 qerror_report(QERR_PROPERTY_NOT_FOUND
, dev
->info
->name
, name
);
633 ret
= prop
->info
->parse(dev
, prop
, value
);
637 qerror_report(QERR_PROPERTY_VALUE_IN_USE
,
638 dev
->info
->name
, name
, value
);
642 qerror_report(QERR_PROPERTY_VALUE_BAD
,
643 dev
->info
->name
, name
, value
);
646 qerror_report(QERR_PROPERTY_VALUE_NOT_FOUND
,
647 dev
->info
->name
, name
, value
);
655 void qdev_prop_set(DeviceState
*dev
, const char *name
, void *src
, enum PropertyType type
)
659 prop
= qdev_prop_find(dev
, name
);
661 fprintf(stderr
, "%s: property \"%s.%s\" not found\n",
662 __FUNCTION__
, dev
->info
->name
, name
);
665 if (prop
->info
->type
!= type
) {
666 fprintf(stderr
, "%s: property \"%s.%s\" type mismatch\n",
667 __FUNCTION__
, dev
->info
->name
, name
);
670 qdev_prop_cpy(dev
, prop
, src
);
673 void qdev_prop_set_bit(DeviceState
*dev
, const char *name
, bool value
)
675 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_BIT
);
678 void qdev_prop_set_uint8(DeviceState
*dev
, const char *name
, uint8_t value
)
680 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_UINT8
);
683 void qdev_prop_set_uint16(DeviceState
*dev
, const char *name
, uint16_t value
)
685 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_UINT16
);
688 void qdev_prop_set_uint32(DeviceState
*dev
, const char *name
, uint32_t value
)
690 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_UINT32
);
693 void qdev_prop_set_int32(DeviceState
*dev
, const char *name
, int32_t value
)
695 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_INT32
);
698 void qdev_prop_set_uint64(DeviceState
*dev
, const char *name
, uint64_t value
)
700 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_UINT64
);
703 void qdev_prop_set_string(DeviceState
*dev
, const char *name
, char *value
)
705 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_STRING
);
708 int qdev_prop_set_drive(DeviceState
*dev
, const char *name
, BlockDriverState
*value
)
712 res
= bdrv_attach_dev(value
, dev
);
714 error_report("Can't attach drive %s to %s.%s: %s",
715 bdrv_get_device_name(value
),
716 dev
->id
? dev
->id
: dev
->info
->name
,
717 name
, strerror(-res
));
720 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_DRIVE
);
724 void qdev_prop_set_drive_nofail(DeviceState
*dev
, const char *name
, BlockDriverState
*value
)
726 if (qdev_prop_set_drive(dev
, name
, value
) < 0) {
730 void qdev_prop_set_chr(DeviceState
*dev
, const char *name
, CharDriverState
*value
)
732 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_CHR
);
735 void qdev_prop_set_netdev(DeviceState
*dev
, const char *name
, VLANClientState
*value
)
737 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_NETDEV
);
740 void qdev_prop_set_vlan(DeviceState
*dev
, const char *name
, VLANState
*value
)
742 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_VLAN
);
745 void qdev_prop_set_macaddr(DeviceState
*dev
, const char *name
, uint8_t *value
)
747 qdev_prop_set(dev
, name
, value
, PROP_TYPE_MACADDR
);
750 void qdev_prop_set_ptr(DeviceState
*dev
, const char *name
, void *value
)
752 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_PTR
);
755 void qdev_prop_set_defaults(DeviceState
*dev
, Property
*props
)
759 while (props
->name
) {
761 qdev_prop_cpy(dev
, props
, props
->defval
);
767 static QTAILQ_HEAD(, GlobalProperty
) global_props
= QTAILQ_HEAD_INITIALIZER(global_props
);
769 static void qdev_prop_register_global(GlobalProperty
*prop
)
771 QTAILQ_INSERT_TAIL(&global_props
, prop
, next
);
774 void qdev_prop_register_global_list(GlobalProperty
*props
)
778 for (i
= 0; props
[i
].driver
!= NULL
; i
++) {
779 qdev_prop_register_global(props
+i
);
783 void qdev_prop_set_globals(DeviceState
*dev
)
785 GlobalProperty
*prop
;
787 QTAILQ_FOREACH(prop
, &global_props
, next
) {
788 if (strcmp(dev
->info
->name
, prop
->driver
) != 0 &&
789 strcmp(dev
->info
->bus_info
->name
, prop
->driver
) != 0) {
792 if (qdev_prop_parse(dev
, prop
->property
, prop
->value
) != 0) {
798 static int qdev_add_one_global(QemuOpts
*opts
, void *opaque
)
802 g
= g_malloc0(sizeof(*g
));
803 g
->driver
= qemu_opt_get(opts
, "driver");
804 g
->property
= qemu_opt_get(opts
, "property");
805 g
->value
= qemu_opt_get(opts
, "value");
806 qdev_prop_register_global(g
);
810 void qemu_add_globals(void)
812 qemu_opts_foreach(qemu_find_opts("global"), qdev_add_one_global
, NULL
, 0);