5 void *qdev_get_prop_ptr(DeviceState
*dev
, Property
*prop
)
12 static uint32_t qdev_get_prop_mask(Property
*prop
)
14 assert(prop
->info
->type
== PROP_TYPE_BIT
);
15 return 0x1 << prop
->bitnr
;
18 static void bit_prop_set(DeviceState
*dev
, Property
*props
, bool val
)
20 uint32_t *p
= qdev_get_prop_ptr(dev
, props
);
21 uint32_t mask
= qdev_get_prop_mask(props
);
28 static void qdev_prop_cpy(DeviceState
*dev
, Property
*props
, void *src
)
30 if (props
->info
->type
== PROP_TYPE_BIT
) {
32 bit_prop_set(dev
, props
, *defval
);
34 char *dst
= qdev_get_prop_ptr(dev
, props
);
35 memcpy(dst
, src
, props
->info
->size
);
40 static int parse_bit(DeviceState
*dev
, Property
*prop
, const char *str
)
42 if (!strncasecmp(str
, "on", 2))
43 bit_prop_set(dev
, prop
, true);
44 else if (!strncasecmp(str
, "off", 3))
45 bit_prop_set(dev
, prop
, false);
51 static int print_bit(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
53 uint8_t *p
= qdev_get_prop_ptr(dev
, prop
);
54 return snprintf(dest
, len
, (*p
& qdev_get_prop_mask(prop
)) ? "on" : "off");
57 PropertyInfo qdev_prop_bit
= {
59 .type
= PROP_TYPE_BIT
,
60 .size
= sizeof(uint32_t),
65 /* --- 8bit integer --- */
67 static int parse_uint8(DeviceState
*dev
, Property
*prop
, const char *str
)
69 uint8_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
72 /* accept both hex and decimal */
73 fmt
= strncasecmp(str
, "0x",2) == 0 ? "%" PRIx8
: "%" PRIu8
;
74 if (sscanf(str
, fmt
, ptr
) != 1)
79 static int print_uint8(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
81 uint8_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
82 return snprintf(dest
, len
, "%" PRIu8
, *ptr
);
85 PropertyInfo qdev_prop_uint8
= {
87 .type
= PROP_TYPE_UINT8
,
88 .size
= sizeof(uint8_t),
93 /* --- 16bit integer --- */
95 static int parse_uint16(DeviceState
*dev
, Property
*prop
, const char *str
)
97 uint16_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
100 /* accept both hex and decimal */
101 fmt
= strncasecmp(str
, "0x",2) == 0 ? "%" PRIx16
: "%" PRIu16
;
102 if (sscanf(str
, fmt
, ptr
) != 1)
107 static int print_uint16(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
109 uint16_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
110 return snprintf(dest
, len
, "%" PRIu16
, *ptr
);
113 PropertyInfo qdev_prop_uint16
= {
115 .type
= PROP_TYPE_UINT16
,
116 .size
= sizeof(uint16_t),
117 .parse
= parse_uint16
,
118 .print
= print_uint16
,
121 /* --- 32bit integer --- */
123 static int parse_uint32(DeviceState
*dev
, Property
*prop
, const char *str
)
125 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
128 /* accept both hex and decimal */
129 fmt
= strncasecmp(str
, "0x",2) == 0 ? "%" PRIx32
: "%" PRIu32
;
130 if (sscanf(str
, fmt
, ptr
) != 1)
135 static int print_uint32(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
137 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
138 return snprintf(dest
, len
, "%" PRIu32
, *ptr
);
141 PropertyInfo qdev_prop_uint32
= {
143 .type
= PROP_TYPE_UINT32
,
144 .size
= sizeof(uint32_t),
145 .parse
= parse_uint32
,
146 .print
= print_uint32
,
149 static int parse_int32(DeviceState
*dev
, Property
*prop
, const char *str
)
151 int32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
153 if (sscanf(str
, "%" PRId32
, ptr
) != 1)
158 static int print_int32(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
160 int32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
161 return snprintf(dest
, len
, "%" PRId32
, *ptr
);
164 PropertyInfo qdev_prop_int32
= {
166 .type
= PROP_TYPE_INT32
,
167 .size
= sizeof(int32_t),
168 .parse
= parse_int32
,
169 .print
= print_int32
,
172 /* --- 32bit hex value --- */
174 static int parse_hex32(DeviceState
*dev
, Property
*prop
, const char *str
)
176 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
178 if (sscanf(str
, "%" PRIx32
, ptr
) != 1)
183 static int print_hex32(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
185 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
186 return snprintf(dest
, len
, "0x%" PRIx32
, *ptr
);
189 PropertyInfo qdev_prop_hex32
= {
191 .type
= PROP_TYPE_UINT32
,
192 .size
= sizeof(uint32_t),
193 .parse
= parse_hex32
,
194 .print
= print_hex32
,
197 /* --- 64bit integer --- */
199 static int parse_uint64(DeviceState
*dev
, Property
*prop
, const char *str
)
201 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
204 /* accept both hex and decimal */
205 fmt
= strncasecmp(str
, "0x",2) == 0 ? "%" PRIx64
: "%" PRIu64
;
206 if (sscanf(str
, fmt
, ptr
) != 1)
211 static int print_uint64(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
213 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
214 return snprintf(dest
, len
, "%" PRIu64
, *ptr
);
217 PropertyInfo qdev_prop_uint64
= {
219 .type
= PROP_TYPE_UINT64
,
220 .size
= sizeof(uint64_t),
221 .parse
= parse_uint64
,
222 .print
= print_uint64
,
225 /* --- 64bit hex value --- */
227 static int parse_hex64(DeviceState
*dev
, Property
*prop
, const char *str
)
229 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
231 if (sscanf(str
, "%" PRIx64
, ptr
) != 1)
236 static int print_hex64(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
238 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
239 return snprintf(dest
, len
, "0x%" PRIx64
, *ptr
);
242 PropertyInfo qdev_prop_hex64
= {
244 .type
= PROP_TYPE_UINT64
,
245 .size
= sizeof(uint64_t),
246 .parse
= parse_hex64
,
247 .print
= print_hex64
,
252 static int parse_string(DeviceState
*dev
, Property
*prop
, const char *str
)
254 char **ptr
= qdev_get_prop_ptr(dev
, prop
);
258 *ptr
= qemu_strdup(str
);
262 static int print_string(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
264 char **ptr
= qdev_get_prop_ptr(dev
, prop
);
266 return snprintf(dest
, len
, "<null>");
267 return snprintf(dest
, len
, "\"%s\"", *ptr
);
270 PropertyInfo qdev_prop_string
= {
272 .type
= PROP_TYPE_STRING
,
273 .size
= sizeof(char*),
274 .parse
= parse_string
,
275 .print
= print_string
,
280 static int parse_drive(DeviceState
*dev
, Property
*prop
, const char *str
)
282 DriveInfo
**ptr
= qdev_get_prop_ptr(dev
, prop
);
284 *ptr
= drive_get_by_id(str
);
290 static int print_drive(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
292 DriveInfo
**ptr
= qdev_get_prop_ptr(dev
, prop
);
293 return snprintf(dest
, len
, "%s", (*ptr
) ? (*ptr
)->id
: "<null>");
296 PropertyInfo qdev_prop_drive
= {
298 .type
= PROP_TYPE_DRIVE
,
299 .size
= sizeof(DriveInfo
*),
300 .parse
= parse_drive
,
301 .print
= print_drive
,
304 /* --- character device --- */
306 static int parse_chr(DeviceState
*dev
, Property
*prop
, const char *str
)
308 CharDriverState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
310 *ptr
= qemu_chr_find(str
);
316 static int print_chr(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
318 CharDriverState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
320 if (*ptr
&& (*ptr
)->label
) {
321 return snprintf(dest
, len
, "%s", (*ptr
)->label
);
323 return snprintf(dest
, len
, "<null>");
327 PropertyInfo qdev_prop_chr
= {
329 .type
= PROP_TYPE_CHR
,
330 .size
= sizeof(CharDriverState
*),
335 /* --- netdev device --- */
337 static int parse_netdev(DeviceState
*dev
, Property
*prop
, const char *str
)
339 VLANClientState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
341 *ptr
= qemu_find_netdev(str
);
350 static int print_netdev(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
352 VLANClientState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
354 if (*ptr
&& (*ptr
)->name
) {
355 return snprintf(dest
, len
, "%s", (*ptr
)->name
);
357 return snprintf(dest
, len
, "<null>");
361 PropertyInfo qdev_prop_netdev
= {
363 .type
= PROP_TYPE_NETDEV
,
364 .size
= sizeof(VLANClientState
*),
365 .parse
= parse_netdev
,
366 .print
= print_netdev
,
371 static int parse_vlan(DeviceState
*dev
, Property
*prop
, const char *str
)
373 VLANState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
376 if (sscanf(str
, "%d", &id
) != 1)
378 *ptr
= qemu_find_vlan(id
, 1);
384 static int print_vlan(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
386 VLANState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
389 return snprintf(dest
, len
, "%d", (*ptr
)->id
);
391 return snprintf(dest
, len
, "<null>");
395 PropertyInfo qdev_prop_vlan
= {
397 .type
= PROP_TYPE_VLAN
,
398 .size
= sizeof(VLANClientState
*),
403 /* --- pointer --- */
405 /* Not a proper property, just for dirty hacks. TODO Remove it! */
406 PropertyInfo qdev_prop_ptr
= {
408 .type
= PROP_TYPE_PTR
,
409 .size
= sizeof(void*),
412 /* --- mac address --- */
415 * accepted syntax versions:
419 static int parse_mac(DeviceState
*dev
, Property
*prop
, const char *str
)
421 MACAddr
*mac
= qdev_get_prop_ptr(dev
, prop
);
425 for (i
= 0, pos
= 0; i
< 6; i
++, pos
+= 3) {
426 if (!qemu_isxdigit(str
[pos
]))
428 if (!qemu_isxdigit(str
[pos
+1]))
431 if (str
[pos
+2] != '\0')
434 if (str
[pos
+2] != ':' && str
[pos
+2] != '-')
437 mac
->a
[i
] = strtol(str
+pos
, &p
, 16);
442 static int print_mac(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
444 MACAddr
*mac
= qdev_get_prop_ptr(dev
, prop
);
446 return snprintf(dest
, len
, "%02x:%02x:%02x:%02x:%02x:%02x",
447 mac
->a
[0], mac
->a
[1], mac
->a
[2],
448 mac
->a
[3], mac
->a
[4], mac
->a
[5]);
451 PropertyInfo qdev_prop_macaddr
= {
453 .type
= PROP_TYPE_MACADDR
,
454 .size
= sizeof(MACAddr
),
459 /* --- pci address --- */
462 * bus-local address, i.e. "$slot" or "$slot.$fn"
464 static int parse_pci_devfn(DeviceState
*dev
, Property
*prop
, const char *str
)
466 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
467 unsigned int slot
, fn
, n
;
469 if (sscanf(str
, "%x.%x%n", &slot
, &fn
, &n
) != 2) {
471 if (sscanf(str
, "%x%n", &slot
, &n
) != 1) {
479 *ptr
= slot
<< 3 | fn
;
483 static int print_pci_devfn(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
485 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
488 return snprintf(dest
, len
, "<unset>");
490 return snprintf(dest
, len
, "%02x.%x", *ptr
>> 3, *ptr
& 7);
494 PropertyInfo qdev_prop_pci_devfn
= {
496 .type
= PROP_TYPE_UINT32
,
497 .size
= sizeof(uint32_t),
498 .parse
= parse_pci_devfn
,
499 .print
= print_pci_devfn
,
502 /* --- public helpers --- */
504 static Property
*qdev_prop_walk(Property
*props
, const char *name
)
508 while (props
->name
) {
509 if (strcmp(props
->name
, name
) == 0)
516 static Property
*qdev_prop_find(DeviceState
*dev
, const char *name
)
520 /* device properties */
521 prop
= qdev_prop_walk(dev
->info
->props
, name
);
526 prop
= qdev_prop_walk(dev
->parent_bus
->info
->props
, name
);
533 int qdev_prop_exists(DeviceState
*dev
, const char *name
)
535 return qdev_prop_find(dev
, name
) ? true : false;
538 int qdev_prop_parse(DeviceState
*dev
, const char *name
, const char *value
)
543 prop
= qdev_prop_find(dev
, name
);
545 * TODO Properties without a parse method are just for dirty
546 * hacks. qdev_prop_ptr is the only such PropertyInfo. It's
547 * marked for removal. The test !prop->info->parse should be
548 * removed along with it.
550 if (!prop
|| !prop
->info
->parse
) {
551 qerror_report(QERR_PROPERTY_NOT_FOUND
, dev
->info
->name
, name
);
554 ret
= prop
->info
->parse(dev
, prop
, value
);
558 qerror_report(QERR_PROPERTY_VALUE_IN_USE
,
559 dev
->info
->name
, name
, value
);
563 qerror_report(QERR_PROPERTY_VALUE_BAD
,
564 dev
->info
->name
, name
, value
);
567 qerror_report(QERR_PROPERTY_VALUE_NOT_FOUND
,
568 dev
->info
->name
, name
, value
);
576 void qdev_prop_set(DeviceState
*dev
, const char *name
, void *src
, enum PropertyType type
)
580 prop
= qdev_prop_find(dev
, name
);
582 fprintf(stderr
, "%s: property \"%s.%s\" not found\n",
583 __FUNCTION__
, dev
->info
->name
, name
);
586 if (prop
->info
->type
!= type
) {
587 fprintf(stderr
, "%s: property \"%s.%s\" type mismatch\n",
588 __FUNCTION__
, dev
->info
->name
, name
);
591 qdev_prop_cpy(dev
, prop
, src
);
594 void qdev_prop_set_uint8(DeviceState
*dev
, const char *name
, uint8_t value
)
596 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_UINT8
);
599 void qdev_prop_set_uint16(DeviceState
*dev
, const char *name
, uint16_t value
)
601 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_UINT16
);
604 void qdev_prop_set_uint32(DeviceState
*dev
, const char *name
, uint32_t value
)
606 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_UINT32
);
609 void qdev_prop_set_int32(DeviceState
*dev
, const char *name
, int32_t value
)
611 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_INT32
);
614 void qdev_prop_set_uint64(DeviceState
*dev
, const char *name
, uint64_t value
)
616 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_UINT64
);
619 void qdev_prop_set_drive(DeviceState
*dev
, const char *name
, DriveInfo
*value
)
621 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_DRIVE
);
624 void qdev_prop_set_chr(DeviceState
*dev
, const char *name
, CharDriverState
*value
)
626 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_CHR
);
629 void qdev_prop_set_netdev(DeviceState
*dev
, const char *name
, VLANClientState
*value
)
631 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_NETDEV
);
634 void qdev_prop_set_vlan(DeviceState
*dev
, const char *name
, VLANState
*value
)
636 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_VLAN
);
639 void qdev_prop_set_macaddr(DeviceState
*dev
, const char *name
, uint8_t *value
)
641 qdev_prop_set(dev
, name
, value
, PROP_TYPE_MACADDR
);
644 void qdev_prop_set_ptr(DeviceState
*dev
, const char *name
, void *value
)
646 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_PTR
);
649 void qdev_prop_set_defaults(DeviceState
*dev
, Property
*props
)
653 while (props
->name
) {
655 qdev_prop_cpy(dev
, props
, props
->defval
);
661 static QTAILQ_HEAD(, GlobalProperty
) global_props
= QTAILQ_HEAD_INITIALIZER(global_props
);
663 void qdev_prop_register_global(GlobalProperty
*prop
)
665 QTAILQ_INSERT_TAIL(&global_props
, prop
, next
);
668 void qdev_prop_register_global_list(GlobalProperty
*props
)
672 for (i
= 0; props
[i
].driver
!= NULL
; i
++) {
673 qdev_prop_register_global(props
+i
);
677 void qdev_prop_set_globals(DeviceState
*dev
)
679 GlobalProperty
*prop
;
681 QTAILQ_FOREACH(prop
, &global_props
, next
) {
682 if (strcmp(dev
->info
->name
, prop
->driver
) != 0 &&
683 strcmp(dev
->info
->bus_info
->name
, prop
->driver
) != 0) {
686 if (qdev_prop_parse(dev
, prop
->property
, prop
->value
) != 0) {