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
);
347 static int print_netdev(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
349 VLANClientState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
351 if (*ptr
&& (*ptr
)->name
) {
352 return snprintf(dest
, len
, "%s", (*ptr
)->name
);
354 return snprintf(dest
, len
, "<null>");
358 PropertyInfo qdev_prop_netdev
= {
360 .type
= PROP_TYPE_NETDEV
,
361 .size
= sizeof(VLANClientState
*),
362 .parse
= parse_netdev
,
363 .print
= print_netdev
,
368 static int parse_vlan(DeviceState
*dev
, Property
*prop
, const char *str
)
370 VLANState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
373 if (sscanf(str
, "%d", &id
) != 1)
375 *ptr
= qemu_find_vlan(id
, 1);
381 static int print_vlan(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
383 VLANState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
386 return snprintf(dest
, len
, "%d", (*ptr
)->id
);
388 return snprintf(dest
, len
, "<null>");
392 PropertyInfo qdev_prop_vlan
= {
394 .type
= PROP_TYPE_VLAN
,
395 .size
= sizeof(VLANClientState
*),
400 /* --- pointer --- */
402 static int print_ptr(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
404 void **ptr
= qdev_get_prop_ptr(dev
, prop
);
405 return snprintf(dest
, len
, "<%p>", *ptr
);
408 PropertyInfo qdev_prop_ptr
= {
410 .type
= PROP_TYPE_PTR
,
411 .size
= sizeof(void*),
415 /* --- mac address --- */
418 * accepted syntax versions:
422 static int parse_mac(DeviceState
*dev
, Property
*prop
, const char *str
)
424 MACAddr
*mac
= qdev_get_prop_ptr(dev
, prop
);
428 for (i
= 0, pos
= 0; i
< 6; i
++, pos
+= 3) {
429 if (!qemu_isxdigit(str
[pos
]))
431 if (!qemu_isxdigit(str
[pos
+1]))
434 if (str
[pos
+2] != '\0')
437 if (str
[pos
+2] != ':' && str
[pos
+2] != '-')
440 mac
->a
[i
] = strtol(str
+pos
, &p
, 16);
445 static int print_mac(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
447 MACAddr
*mac
= qdev_get_prop_ptr(dev
, prop
);
449 return snprintf(dest
, len
, "%02x:%02x:%02x:%02x:%02x:%02x",
450 mac
->a
[0], mac
->a
[1], mac
->a
[2],
451 mac
->a
[3], mac
->a
[4], mac
->a
[5]);
454 PropertyInfo qdev_prop_macaddr
= {
456 .type
= PROP_TYPE_MACADDR
,
457 .size
= sizeof(MACAddr
),
462 /* --- pci address --- */
465 * bus-local address, i.e. "$slot" or "$slot.$fn"
467 static int parse_pci_devfn(DeviceState
*dev
, Property
*prop
, const char *str
)
469 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
470 unsigned int slot
, fn
, n
;
472 if (sscanf(str
, "%x.%x%n", &slot
, &fn
, &n
) != 2) {
474 if (sscanf(str
, "%x%n", &slot
, &n
) != 1) {
482 *ptr
= slot
<< 3 | fn
;
486 static int print_pci_devfn(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
488 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
491 return snprintf(dest
, len
, "<unset>");
493 return snprintf(dest
, len
, "%02x.%x", *ptr
>> 3, *ptr
& 7);
497 PropertyInfo qdev_prop_pci_devfn
= {
499 .type
= PROP_TYPE_UINT32
,
500 .size
= sizeof(uint32_t),
501 .parse
= parse_pci_devfn
,
502 .print
= print_pci_devfn
,
505 /* --- public helpers --- */
507 static Property
*qdev_prop_walk(Property
*props
, const char *name
)
511 while (props
->name
) {
512 if (strcmp(props
->name
, name
) == 0)
519 static Property
*qdev_prop_find(DeviceState
*dev
, const char *name
)
523 /* device properties */
524 prop
= qdev_prop_walk(dev
->info
->props
, name
);
529 prop
= qdev_prop_walk(dev
->parent_bus
->info
->props
, name
);
536 int qdev_prop_exists(DeviceState
*dev
, const char *name
)
538 return qdev_prop_find(dev
, name
) ? true : false;
541 int qdev_prop_parse(DeviceState
*dev
, const char *name
, const char *value
)
545 prop
= qdev_prop_find(dev
, name
);
547 fprintf(stderr
, "property \"%s.%s\" not found\n",
548 dev
->info
->name
, name
);
551 if (!prop
->info
->parse
) {
552 fprintf(stderr
, "property \"%s.%s\" has no parser\n",
553 dev
->info
->name
, name
);
556 if (prop
->info
->parse(dev
, prop
, value
) != 0) {
557 fprintf(stderr
, "property \"%s.%s\": failed to parse \"%s\"\n",
558 dev
->info
->name
, name
, value
);
564 void qdev_prop_set(DeviceState
*dev
, const char *name
, void *src
, enum PropertyType type
)
568 prop
= qdev_prop_find(dev
, name
);
570 fprintf(stderr
, "%s: property \"%s.%s\" not found\n",
571 __FUNCTION__
, dev
->info
->name
, name
);
574 if (prop
->info
->type
!= type
) {
575 fprintf(stderr
, "%s: property \"%s.%s\" type mismatch\n",
576 __FUNCTION__
, dev
->info
->name
, name
);
579 qdev_prop_cpy(dev
, prop
, src
);
582 void qdev_prop_set_uint8(DeviceState
*dev
, const char *name
, uint8_t value
)
584 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_UINT8
);
587 void qdev_prop_set_uint16(DeviceState
*dev
, const char *name
, uint16_t value
)
589 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_UINT16
);
592 void qdev_prop_set_uint32(DeviceState
*dev
, const char *name
, uint32_t value
)
594 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_UINT32
);
597 void qdev_prop_set_int32(DeviceState
*dev
, const char *name
, int32_t value
)
599 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_INT32
);
602 void qdev_prop_set_uint64(DeviceState
*dev
, const char *name
, uint64_t value
)
604 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_UINT64
);
607 void qdev_prop_set_drive(DeviceState
*dev
, const char *name
, DriveInfo
*value
)
609 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_DRIVE
);
612 void qdev_prop_set_chr(DeviceState
*dev
, const char *name
, CharDriverState
*value
)
614 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_CHR
);
617 void qdev_prop_set_netdev(DeviceState
*dev
, const char *name
, VLANClientState
*value
)
619 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_NETDEV
);
622 void qdev_prop_set_vlan(DeviceState
*dev
, const char *name
, VLANState
*value
)
624 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_VLAN
);
627 void qdev_prop_set_macaddr(DeviceState
*dev
, const char *name
, uint8_t *value
)
629 qdev_prop_set(dev
, name
, value
, PROP_TYPE_MACADDR
);
632 void qdev_prop_set_ptr(DeviceState
*dev
, const char *name
, void *value
)
634 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_PTR
);
637 void qdev_prop_set_defaults(DeviceState
*dev
, Property
*props
)
641 while (props
->name
) {
643 qdev_prop_cpy(dev
, props
, props
->defval
);
649 static QTAILQ_HEAD(, GlobalProperty
) global_props
= QTAILQ_HEAD_INITIALIZER(global_props
);
651 void qdev_prop_register_global(GlobalProperty
*prop
)
653 QTAILQ_INSERT_TAIL(&global_props
, prop
, next
);
656 void qdev_prop_register_global_list(GlobalProperty
*props
)
660 for (i
= 0; props
[i
].driver
!= NULL
; i
++) {
661 qdev_prop_register_global(props
+i
);
665 void qdev_prop_set_globals(DeviceState
*dev
)
667 GlobalProperty
*prop
;
669 QTAILQ_FOREACH(prop
, &global_props
, next
) {
670 if (strcmp(dev
->info
->name
, prop
->driver
) != 0 &&
671 strcmp(dev
->info
->bus_info
->name
, prop
->driver
) != 0) {
674 if (qdev_prop_parse(dev
, prop
->property
, prop
->value
) != 0) {