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 /* --- 16bit integer --- */
98 static int parse_uint16(DeviceState
*dev
, Property
*prop
, const char *str
)
100 uint16_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
103 /* accept both hex and decimal */
104 *ptr
= strtoul(str
, &end
, 0);
105 if ((*end
!= '\0') || (end
== str
)) {
112 static int print_uint16(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
114 uint16_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
115 return snprintf(dest
, len
, "%" PRIu16
, *ptr
);
118 PropertyInfo qdev_prop_uint16
= {
120 .type
= PROP_TYPE_UINT16
,
121 .size
= sizeof(uint16_t),
122 .parse
= parse_uint16
,
123 .print
= print_uint16
,
126 /* --- 32bit integer --- */
128 static int parse_uint32(DeviceState
*dev
, Property
*prop
, const char *str
)
130 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
133 /* accept both hex and decimal */
134 *ptr
= strtoul(str
, &end
, 0);
135 if ((*end
!= '\0') || (end
== str
)) {
142 static int print_uint32(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
144 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
145 return snprintf(dest
, len
, "%" PRIu32
, *ptr
);
148 PropertyInfo qdev_prop_uint32
= {
150 .type
= PROP_TYPE_UINT32
,
151 .size
= sizeof(uint32_t),
152 .parse
= parse_uint32
,
153 .print
= print_uint32
,
156 static int parse_int32(DeviceState
*dev
, Property
*prop
, const char *str
)
158 int32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
161 *ptr
= strtol(str
, &end
, 10);
162 if ((*end
!= '\0') || (end
== str
)) {
169 static int print_int32(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
171 int32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
172 return snprintf(dest
, len
, "%" PRId32
, *ptr
);
175 PropertyInfo qdev_prop_int32
= {
177 .type
= PROP_TYPE_INT32
,
178 .size
= sizeof(int32_t),
179 .parse
= parse_int32
,
180 .print
= print_int32
,
183 /* --- 32bit hex value --- */
185 static int parse_hex32(DeviceState
*dev
, Property
*prop
, const char *str
)
187 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
190 *ptr
= strtoul(str
, &end
, 16);
191 if ((*end
!= '\0') || (end
== str
)) {
198 static int print_hex32(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
200 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
201 return snprintf(dest
, len
, "0x%" PRIx32
, *ptr
);
204 PropertyInfo qdev_prop_hex32
= {
206 .type
= PROP_TYPE_UINT32
,
207 .size
= sizeof(uint32_t),
208 .parse
= parse_hex32
,
209 .print
= print_hex32
,
212 /* --- 64bit integer --- */
214 static int parse_uint64(DeviceState
*dev
, Property
*prop
, const char *str
)
216 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
219 /* accept both hex and decimal */
220 *ptr
= strtoull(str
, &end
, 0);
221 if ((*end
!= '\0') || (end
== str
)) {
228 static int print_uint64(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
230 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
231 return snprintf(dest
, len
, "%" PRIu64
, *ptr
);
234 PropertyInfo qdev_prop_uint64
= {
236 .type
= PROP_TYPE_UINT64
,
237 .size
= sizeof(uint64_t),
238 .parse
= parse_uint64
,
239 .print
= print_uint64
,
242 /* --- 64bit hex value --- */
244 static int parse_hex64(DeviceState
*dev
, Property
*prop
, const char *str
)
246 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
249 *ptr
= strtoull(str
, &end
, 16);
250 if ((*end
!= '\0') || (end
== str
)) {
257 static int print_hex64(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
259 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
260 return snprintf(dest
, len
, "0x%" PRIx64
, *ptr
);
263 PropertyInfo qdev_prop_hex64
= {
265 .type
= PROP_TYPE_UINT64
,
266 .size
= sizeof(uint64_t),
267 .parse
= parse_hex64
,
268 .print
= print_hex64
,
273 static int parse_string(DeviceState
*dev
, Property
*prop
, const char *str
)
275 char **ptr
= qdev_get_prop_ptr(dev
, prop
);
279 *ptr
= g_strdup(str
);
283 static void free_string(DeviceState
*dev
, Property
*prop
)
285 g_free(*(char **)qdev_get_prop_ptr(dev
, prop
));
288 static int print_string(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
290 char **ptr
= qdev_get_prop_ptr(dev
, prop
);
292 return snprintf(dest
, len
, "<null>");
293 return snprintf(dest
, len
, "\"%s\"", *ptr
);
296 PropertyInfo qdev_prop_string
= {
298 .type
= PROP_TYPE_STRING
,
299 .size
= sizeof(char*),
300 .parse
= parse_string
,
301 .print
= print_string
,
307 static int parse_drive(DeviceState
*dev
, Property
*prop
, const char *str
)
309 BlockDriverState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
310 BlockDriverState
*bs
;
315 if (bdrv_attach(bs
, dev
) < 0)
321 static void free_drive(DeviceState
*dev
, Property
*prop
)
323 BlockDriverState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
326 bdrv_detach(*ptr
, dev
);
327 blockdev_auto_del(*ptr
);
331 static int print_drive(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
333 BlockDriverState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
334 return snprintf(dest
, len
, "%s",
335 *ptr
? bdrv_get_device_name(*ptr
) : "<null>");
338 PropertyInfo qdev_prop_drive
= {
340 .type
= PROP_TYPE_DRIVE
,
341 .size
= sizeof(BlockDriverState
*),
342 .parse
= parse_drive
,
343 .print
= print_drive
,
347 /* --- character device --- */
349 static int parse_chr(DeviceState
*dev
, Property
*prop
, const char *str
)
351 CharDriverState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
353 *ptr
= qemu_chr_find(str
);
357 if ((*ptr
)->avail_connections
< 1) {
360 --(*ptr
)->avail_connections
;
364 static int print_chr(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
366 CharDriverState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
368 if (*ptr
&& (*ptr
)->label
) {
369 return snprintf(dest
, len
, "%s", (*ptr
)->label
);
371 return snprintf(dest
, len
, "<null>");
375 PropertyInfo qdev_prop_chr
= {
377 .type
= PROP_TYPE_CHR
,
378 .size
= sizeof(CharDriverState
*),
383 /* --- netdev device --- */
385 static int parse_netdev(DeviceState
*dev
, Property
*prop
, const char *str
)
387 VLANClientState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
389 *ptr
= qemu_find_netdev(str
);
398 static int print_netdev(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
400 VLANClientState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
402 if (*ptr
&& (*ptr
)->name
) {
403 return snprintf(dest
, len
, "%s", (*ptr
)->name
);
405 return snprintf(dest
, len
, "<null>");
409 PropertyInfo qdev_prop_netdev
= {
411 .type
= PROP_TYPE_NETDEV
,
412 .size
= sizeof(VLANClientState
*),
413 .parse
= parse_netdev
,
414 .print
= print_netdev
,
419 static int parse_vlan(DeviceState
*dev
, Property
*prop
, const char *str
)
421 VLANState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
424 if (sscanf(str
, "%d", &id
) != 1)
426 *ptr
= qemu_find_vlan(id
, 1);
432 static int print_vlan(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
434 VLANState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
437 return snprintf(dest
, len
, "%d", (*ptr
)->id
);
439 return snprintf(dest
, len
, "<null>");
443 PropertyInfo qdev_prop_vlan
= {
445 .type
= PROP_TYPE_VLAN
,
446 .size
= sizeof(VLANClientState
*),
451 /* --- pointer --- */
453 /* Not a proper property, just for dirty hacks. TODO Remove it! */
454 PropertyInfo qdev_prop_ptr
= {
456 .type
= PROP_TYPE_PTR
,
457 .size
= sizeof(void*),
460 /* --- mac address --- */
463 * accepted syntax versions:
467 static int parse_mac(DeviceState
*dev
, Property
*prop
, const char *str
)
469 MACAddr
*mac
= qdev_get_prop_ptr(dev
, prop
);
473 for (i
= 0, pos
= 0; i
< 6; i
++, pos
+= 3) {
474 if (!qemu_isxdigit(str
[pos
]))
476 if (!qemu_isxdigit(str
[pos
+1]))
479 if (str
[pos
+2] != '\0')
482 if (str
[pos
+2] != ':' && str
[pos
+2] != '-')
485 mac
->a
[i
] = strtol(str
+pos
, &p
, 16);
490 static int print_mac(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
492 MACAddr
*mac
= qdev_get_prop_ptr(dev
, prop
);
494 return snprintf(dest
, len
, "%02x:%02x:%02x:%02x:%02x:%02x",
495 mac
->a
[0], mac
->a
[1], mac
->a
[2],
496 mac
->a
[3], mac
->a
[4], mac
->a
[5]);
499 PropertyInfo qdev_prop_macaddr
= {
501 .type
= PROP_TYPE_MACADDR
,
502 .size
= sizeof(MACAddr
),
507 /* --- pci address --- */
510 * bus-local address, i.e. "$slot" or "$slot.$fn"
512 static int parse_pci_devfn(DeviceState
*dev
, Property
*prop
, const char *str
)
514 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
515 unsigned int slot
, fn
, n
;
517 if (sscanf(str
, "%x.%x%n", &slot
, &fn
, &n
) != 2) {
519 if (sscanf(str
, "%x%n", &slot
, &n
) != 1) {
527 *ptr
= slot
<< 3 | fn
;
531 static int print_pci_devfn(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
533 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
536 return snprintf(dest
, len
, "<unset>");
538 return snprintf(dest
, len
, "%02x.%x", *ptr
>> 3, *ptr
& 7);
542 PropertyInfo qdev_prop_pci_devfn
= {
544 .type
= PROP_TYPE_UINT32
,
545 .size
= sizeof(uint32_t),
546 .parse
= parse_pci_devfn
,
547 .print
= print_pci_devfn
,
550 /* --- public helpers --- */
552 static Property
*qdev_prop_walk(Property
*props
, const char *name
)
556 while (props
->name
) {
557 if (strcmp(props
->name
, name
) == 0)
564 static Property
*qdev_prop_find(DeviceState
*dev
, const char *name
)
568 /* device properties */
569 prop
= qdev_prop_walk(dev
->info
->props
, name
);
574 prop
= qdev_prop_walk(dev
->parent_bus
->info
->props
, name
);
581 int qdev_prop_exists(DeviceState
*dev
, const char *name
)
583 return qdev_prop_find(dev
, name
) ? true : false;
586 int qdev_prop_parse(DeviceState
*dev
, const char *name
, const char *value
)
591 prop
= qdev_prop_find(dev
, name
);
593 * TODO Properties without a parse method are just for dirty
594 * hacks. qdev_prop_ptr is the only such PropertyInfo. It's
595 * marked for removal. The test !prop->info->parse should be
596 * removed along with it.
598 if (!prop
|| !prop
->info
->parse
) {
599 qerror_report(QERR_PROPERTY_NOT_FOUND
, dev
->info
->name
, name
);
602 ret
= prop
->info
->parse(dev
, prop
, value
);
606 qerror_report(QERR_PROPERTY_VALUE_IN_USE
,
607 dev
->info
->name
, name
, value
);
611 qerror_report(QERR_PROPERTY_VALUE_BAD
,
612 dev
->info
->name
, name
, value
);
615 qerror_report(QERR_PROPERTY_VALUE_NOT_FOUND
,
616 dev
->info
->name
, name
, value
);
624 void qdev_prop_set(DeviceState
*dev
, const char *name
, void *src
, enum PropertyType type
)
628 prop
= qdev_prop_find(dev
, name
);
630 fprintf(stderr
, "%s: property \"%s.%s\" not found\n",
631 __FUNCTION__
, dev
->info
->name
, name
);
634 if (prop
->info
->type
!= type
) {
635 fprintf(stderr
, "%s: property \"%s.%s\" type mismatch\n",
636 __FUNCTION__
, dev
->info
->name
, name
);
639 qdev_prop_cpy(dev
, prop
, src
);
642 void qdev_prop_set_bit(DeviceState
*dev
, const char *name
, bool value
)
644 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_BIT
);
647 void qdev_prop_set_uint8(DeviceState
*dev
, const char *name
, uint8_t value
)
649 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_UINT8
);
652 void qdev_prop_set_uint16(DeviceState
*dev
, const char *name
, uint16_t value
)
654 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_UINT16
);
657 void qdev_prop_set_uint32(DeviceState
*dev
, const char *name
, uint32_t value
)
659 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_UINT32
);
662 void qdev_prop_set_int32(DeviceState
*dev
, const char *name
, int32_t value
)
664 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_INT32
);
667 void qdev_prop_set_uint64(DeviceState
*dev
, const char *name
, uint64_t value
)
669 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_UINT64
);
672 void qdev_prop_set_string(DeviceState
*dev
, const char *name
, char *value
)
674 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_STRING
);
677 int qdev_prop_set_drive(DeviceState
*dev
, const char *name
, BlockDriverState
*value
)
681 res
= bdrv_attach(value
, dev
);
683 error_report("Can't attach drive %s to %s.%s: %s",
684 bdrv_get_device_name(value
),
685 dev
->id
? dev
->id
: dev
->info
->name
,
686 name
, strerror(-res
));
689 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_DRIVE
);
693 void qdev_prop_set_drive_nofail(DeviceState
*dev
, const char *name
, BlockDriverState
*value
)
695 if (qdev_prop_set_drive(dev
, name
, value
) < 0) {
699 void qdev_prop_set_chr(DeviceState
*dev
, const char *name
, CharDriverState
*value
)
701 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_CHR
);
704 void qdev_prop_set_netdev(DeviceState
*dev
, const char *name
, VLANClientState
*value
)
706 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_NETDEV
);
709 void qdev_prop_set_vlan(DeviceState
*dev
, const char *name
, VLANState
*value
)
711 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_VLAN
);
714 void qdev_prop_set_macaddr(DeviceState
*dev
, const char *name
, uint8_t *value
)
716 qdev_prop_set(dev
, name
, value
, PROP_TYPE_MACADDR
);
719 void qdev_prop_set_ptr(DeviceState
*dev
, const char *name
, void *value
)
721 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_PTR
);
724 void qdev_prop_set_defaults(DeviceState
*dev
, Property
*props
)
728 while (props
->name
) {
730 qdev_prop_cpy(dev
, props
, props
->defval
);
736 static QTAILQ_HEAD(, GlobalProperty
) global_props
= QTAILQ_HEAD_INITIALIZER(global_props
);
738 static void qdev_prop_register_global(GlobalProperty
*prop
)
740 QTAILQ_INSERT_TAIL(&global_props
, prop
, next
);
743 void qdev_prop_register_global_list(GlobalProperty
*props
)
747 for (i
= 0; props
[i
].driver
!= NULL
; i
++) {
748 qdev_prop_register_global(props
+i
);
752 void qdev_prop_set_globals(DeviceState
*dev
)
754 GlobalProperty
*prop
;
756 QTAILQ_FOREACH(prop
, &global_props
, next
) {
757 if (strcmp(dev
->info
->name
, prop
->driver
) != 0 &&
758 strcmp(dev
->info
->bus_info
->name
, prop
->driver
) != 0) {
761 if (qdev_prop_parse(dev
, prop
->property
, prop
->value
) != 0) {
767 static int qdev_add_one_global(QemuOpts
*opts
, void *opaque
)
771 g
= g_malloc0(sizeof(*g
));
772 g
->driver
= qemu_opt_get(opts
, "driver");
773 g
->property
= qemu_opt_get(opts
, "property");
774 g
->value
= qemu_opt_get(opts
, "value");
775 qdev_prop_register_global(g
);
779 void qemu_add_globals(void)
781 qemu_opts_foreach(qemu_find_opts("global"), qdev_add_one_global
, NULL
, 0);