4 void *qdev_get_prop_ptr(DeviceState
*dev
, Property
*prop
)
11 /* --- 16bit integer --- */
13 static int parse_uint16(DeviceState
*dev
, Property
*prop
, const char *str
)
15 uint16_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
18 /* accept both hex and decimal */
19 fmt
= strncasecmp(str
, "0x",2) == 0 ? "%" PRIx16
: "%" PRIu16
;
20 if (sscanf(str
, fmt
, ptr
) != 1)
25 static int print_uint16(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
27 uint16_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
28 return snprintf(dest
, len
, "%" PRIu16
, *ptr
);
31 PropertyInfo qdev_prop_uint16
= {
33 .type
= PROP_TYPE_UINT16
,
34 .size
= sizeof(uint16_t),
35 .parse
= parse_uint16
,
36 .print
= print_uint16
,
39 /* --- 32bit integer --- */
41 static int parse_uint32(DeviceState
*dev
, Property
*prop
, const char *str
)
43 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
46 /* accept both hex and decimal */
47 fmt
= strncasecmp(str
, "0x",2) == 0 ? "%" PRIx32
: "%" PRIu32
;
48 if (sscanf(str
, fmt
, ptr
) != 1)
53 static int print_uint32(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
55 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
56 return snprintf(dest
, len
, "%" PRIu32
, *ptr
);
59 PropertyInfo qdev_prop_uint32
= {
61 .type
= PROP_TYPE_UINT32
,
62 .size
= sizeof(uint32_t),
63 .parse
= parse_uint32
,
64 .print
= print_uint32
,
67 static int parse_int32(DeviceState
*dev
, Property
*prop
, const char *str
)
69 int32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
71 if (sscanf(str
, "%" PRId32
, ptr
) != 1)
76 static int print_int32(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
78 int32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
79 return snprintf(dest
, len
, "%" PRId32
, *ptr
);
82 PropertyInfo qdev_prop_int32
= {
84 .type
= PROP_TYPE_INT32
,
85 .size
= sizeof(int32_t),
90 /* --- 32bit hex value --- */
92 static int parse_hex32(DeviceState
*dev
, Property
*prop
, const char *str
)
94 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
96 if (sscanf(str
, "%" PRIx32
, ptr
) != 1)
101 static int print_hex32(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
103 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
104 return snprintf(dest
, len
, "0x%" PRIx32
, *ptr
);
107 PropertyInfo qdev_prop_hex32
= {
109 .type
= PROP_TYPE_UINT32
,
110 .size
= sizeof(uint32_t),
111 .parse
= parse_hex32
,
112 .print
= print_hex32
,
115 /* --- 64bit integer --- */
117 static int parse_uint64(DeviceState
*dev
, Property
*prop
, const char *str
)
119 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
122 /* accept both hex and decimal */
123 fmt
= strncasecmp(str
, "0x",2) == 0 ? "%" PRIx64
: "%" PRIu64
;
124 if (sscanf(str
, fmt
, ptr
) != 1)
129 static int print_uint64(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
131 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
132 return snprintf(dest
, len
, "%" PRIu64
, *ptr
);
135 PropertyInfo qdev_prop_uint64
= {
137 .type
= PROP_TYPE_UINT64
,
138 .size
= sizeof(uint64_t),
139 .parse
= parse_uint64
,
140 .print
= print_uint64
,
143 /* --- 64bit hex value --- */
145 static int parse_hex64(DeviceState
*dev
, Property
*prop
, const char *str
)
147 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
149 if (sscanf(str
, "%" PRIx64
, ptr
) != 1)
154 static int print_hex64(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
156 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
157 return snprintf(dest
, len
, "0x%" PRIx64
, *ptr
);
160 PropertyInfo qdev_prop_hex64
= {
162 .type
= PROP_TYPE_UINT64
,
163 .size
= sizeof(uint64_t),
164 .parse
= parse_hex64
,
165 .print
= print_hex64
,
170 static int parse_drive(DeviceState
*dev
, Property
*prop
, const char *str
)
172 DriveInfo
**ptr
= qdev_get_prop_ptr(dev
, prop
);
174 *ptr
= drive_get_by_id(str
);
180 static int print_drive(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
182 DriveInfo
**ptr
= qdev_get_prop_ptr(dev
, prop
);
183 return snprintf(dest
, len
, "%s", (*ptr
)->id
);
186 PropertyInfo qdev_prop_drive
= {
188 .type
= PROP_TYPE_DRIVE
,
189 .size
= sizeof(DriveInfo
*),
190 .parse
= parse_drive
,
191 .print
= print_drive
,
194 /* --- character device --- */
196 static int parse_chr(DeviceState
*dev
, Property
*prop
, const char *str
)
198 CharDriverState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
200 *ptr
= qemu_chr_find(str
);
206 static int print_chr(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
208 CharDriverState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
210 if (*ptr
&& (*ptr
)->label
) {
211 return snprintf(dest
, len
, "%s", (*ptr
)->label
);
213 return snprintf(dest
, len
, "<null>");
217 PropertyInfo qdev_prop_chr
= {
219 .type
= PROP_TYPE_CHR
,
220 .size
= sizeof(CharDriverState
*),
225 /* --- pointer --- */
227 static int print_ptr(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
229 void **ptr
= qdev_get_prop_ptr(dev
, prop
);
230 return snprintf(dest
, len
, "<%p>", *ptr
);
233 PropertyInfo qdev_prop_ptr
= {
235 .type
= PROP_TYPE_PTR
,
236 .size
= sizeof(void*),
240 /* --- mac address --- */
243 * accepted syntax versions:
247 static int parse_mac(DeviceState
*dev
, Property
*prop
, const char *str
)
249 uint8_t *mac
= qdev_get_prop_ptr(dev
, prop
);
253 for (i
= 0, pos
= 0; i
< 6; i
++, pos
+= 3) {
254 if (!qemu_isxdigit(str
[pos
]))
256 if (!qemu_isxdigit(str
[pos
+1]))
258 if (i
== 5 && str
[pos
+2] != '\0')
260 if (str
[pos
+2] != ':' && str
[pos
+2] != '-')
262 mac
[i
] = strtol(str
+pos
, &p
, 16);
267 static int print_mac(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
269 uint8_t *mac
= qdev_get_prop_ptr(dev
, prop
);
270 return snprintf(dest
, len
, "%02x:%02x:%02x:%02x:%02x:%02x",
271 mac
[0], mac
[1], mac
[2], mac
[3], mac
[4], mac
[5]);
274 PropertyInfo qdev_prop_macaddr
= {
276 .type
= PROP_TYPE_MACADDR
,
282 /* --- pci address --- */
285 * bus-local address, i.e. "$slot" or "$slot.$fn"
287 static int parse_pci_devfn(DeviceState
*dev
, Property
*prop
, const char *str
)
289 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
290 unsigned int slot
, fn
, n
;
292 if (sscanf(str
, "%x.%x%n", &slot
, &fn
, &n
) != 2) {
294 if (sscanf(str
, "%x%n", &slot
, &n
) != 1) {
302 *ptr
= slot
<< 3 | fn
;
306 static int print_pci_devfn(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
308 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
311 return snprintf(dest
, len
, "<unset>");
313 return snprintf(dest
, len
, "%02x.%x", *ptr
>> 3, *ptr
& 7);
317 PropertyInfo qdev_prop_pci_devfn
= {
319 .type
= PROP_TYPE_UINT32
,
320 .size
= sizeof(uint32_t),
321 .parse
= parse_pci_devfn
,
322 .print
= print_pci_devfn
,
325 /* --- public helpers --- */
327 static Property
*qdev_prop_walk(Property
*props
, const char *name
)
331 while (props
->name
) {
332 if (strcmp(props
->name
, name
) == 0)
339 static Property
*qdev_prop_find(DeviceState
*dev
, const char *name
)
343 /* device properties */
344 prop
= qdev_prop_walk(dev
->info
->props
, name
);
349 prop
= qdev_prop_walk(dev
->parent_bus
->info
->props
, name
);
356 int qdev_prop_parse(DeviceState
*dev
, const char *name
, const char *value
)
360 prop
= qdev_prop_find(dev
, name
);
362 fprintf(stderr
, "property \"%s.%s\" not found\n",
363 dev
->info
->name
, name
);
366 if (!prop
->info
->parse
) {
367 fprintf(stderr
, "property \"%s.%s\" has no parser\n",
368 dev
->info
->name
, name
);
371 return prop
->info
->parse(dev
, prop
, value
);
374 void qdev_prop_set(DeviceState
*dev
, const char *name
, void *src
, enum PropertyType type
)
379 prop
= qdev_prop_find(dev
, name
);
381 fprintf(stderr
, "%s: property \"%s.%s\" not found\n",
382 __FUNCTION__
, dev
->info
->name
, name
);
385 if (prop
->info
->type
!= type
) {
386 fprintf(stderr
, "%s: property \"%s.%s\" type mismatch\n",
387 __FUNCTION__
, dev
->info
->name
, name
);
390 dst
= qdev_get_prop_ptr(dev
, prop
);
391 memcpy(dst
, src
, prop
->info
->size
);
394 void qdev_prop_set_uint16(DeviceState
*dev
, const char *name
, uint16_t value
)
396 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_UINT16
);
399 void qdev_prop_set_uint32(DeviceState
*dev
, const char *name
, uint32_t value
)
401 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_UINT32
);
404 void qdev_prop_set_int32(DeviceState
*dev
, const char *name
, int32_t value
)
406 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_INT32
);
409 void qdev_prop_set_uint64(DeviceState
*dev
, const char *name
, uint64_t value
)
411 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_UINT64
);
414 void qdev_prop_set_drive(DeviceState
*dev
, const char *name
, DriveInfo
*value
)
416 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_DRIVE
);
419 void qdev_prop_set_chr(DeviceState
*dev
, const char *name
, CharDriverState
*value
)
421 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_CHR
);
424 void qdev_prop_set_ptr(DeviceState
*dev
, const char *name
, void *value
)
426 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_PTR
);
429 void qdev_prop_set_defaults(DeviceState
*dev
, Property
*props
)
435 while (props
->name
) {
437 dst
= qdev_get_prop_ptr(dev
, props
);
438 memcpy(dst
, props
->defval
, props
->info
->size
);
444 static CompatProperty
*compat_props
;
446 void qdev_prop_register_compat(CompatProperty
*props
)
448 compat_props
= props
;
451 void qdev_prop_set_compat(DeviceState
*dev
)
453 CompatProperty
*prop
;
458 for (prop
= compat_props
; prop
->driver
!= NULL
; prop
++) {
459 if (strcmp(dev
->info
->name
, prop
->driver
) != 0) {
462 if (qdev_prop_parse(dev
, prop
->property
, prop
->value
) != 0) {