3 void *qdev_get_prop_ptr(DeviceState
*dev
, Property
*prop
)
10 /* --- 16bit integer --- */
12 static int parse_uint16(DeviceState
*dev
, Property
*prop
, const char *str
)
14 uint16_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
17 /* accept both hex and decimal */
18 fmt
= strncasecmp(str
, "0x",2) == 0 ? "%" PRIx16
: "%" PRIu16
;
19 if (sscanf(str
, fmt
, ptr
) != 1)
24 static int print_uint16(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
26 uint16_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
27 return snprintf(dest
, len
, "%" PRIu16
, *ptr
);
30 PropertyInfo qdev_prop_uint16
= {
32 .type
= PROP_TYPE_UINT16
,
33 .size
= sizeof(uint16_t),
34 .parse
= parse_uint16
,
35 .print
= print_uint16
,
38 /* --- 32bit integer --- */
40 static int parse_uint32(DeviceState
*dev
, Property
*prop
, const char *str
)
42 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
45 /* accept both hex and decimal */
46 fmt
= strncasecmp(str
, "0x",2) == 0 ? "%" PRIx32
: "%" PRIu32
;
47 if (sscanf(str
, fmt
, ptr
) != 1)
52 static int print_uint32(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
54 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
55 return snprintf(dest
, len
, "%" PRIu32
, *ptr
);
58 PropertyInfo qdev_prop_uint32
= {
60 .type
= PROP_TYPE_UINT32
,
61 .size
= sizeof(uint32_t),
62 .parse
= parse_uint32
,
63 .print
= print_uint32
,
66 /* --- 32bit hex value --- */
68 static int parse_hex32(DeviceState
*dev
, Property
*prop
, const char *str
)
70 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
72 if (sscanf(str
, "%" PRIx32
, ptr
) != 1)
77 static int print_hex32(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
79 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
80 return snprintf(dest
, len
, "0x%" PRIx32
, *ptr
);
83 PropertyInfo qdev_prop_hex32
= {
85 .type
= PROP_TYPE_UINT32
,
86 .size
= sizeof(uint32_t),
93 static int print_ptr(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
95 void **ptr
= qdev_get_prop_ptr(dev
, prop
);
96 return snprintf(dest
, len
, "<%p>", *ptr
);
99 PropertyInfo qdev_prop_ptr
= {
101 .type
= PROP_TYPE_PTR
,
102 .size
= sizeof(void*),
106 /* --- mac address --- */
109 * accepted syntax versions:
113 static int parse_mac(DeviceState
*dev
, Property
*prop
, const char *str
)
115 uint8_t *mac
= qdev_get_prop_ptr(dev
, prop
);
119 for (i
= 0, pos
= 0; i
< 6; i
++, pos
+= 3) {
120 if (!qemu_isxdigit(str
[pos
]))
122 if (!qemu_isxdigit(str
[pos
+1]))
124 if (i
== 5 && str
[pos
+2] != '\0')
126 if (str
[pos
+2] != ':' && str
[pos
+2] != '-')
128 mac
[i
] = strtol(str
+pos
, &p
, 16);
133 static int print_mac(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
135 uint8_t *mac
= qdev_get_prop_ptr(dev
, prop
);
136 return snprintf(dest
, len
, "%02x:%02x:%02x:%02x:%02x:%02x",
137 mac
[0], mac
[1], mac
[2], mac
[3], mac
[4], mac
[5]);
140 PropertyInfo qdev_prop_macaddr
= {
142 .type
= PROP_TYPE_MACADDR
,
148 /* --- public helpers --- */
150 static Property
*qdev_prop_walk(Property
*props
, const char *name
)
154 while (props
->name
) {
155 if (strcmp(props
->name
, name
) == 0)
162 static Property
*qdev_prop_find(DeviceState
*dev
, const char *name
)
166 /* device properties */
167 prop
= qdev_prop_walk(dev
->info
->props
, name
);
172 prop
= qdev_prop_walk(dev
->parent_bus
->info
->props
, name
);
179 int qdev_prop_parse(DeviceState
*dev
, const char *name
, const char *value
)
183 prop
= qdev_prop_find(dev
, name
);
185 fprintf(stderr
, "property \"%s.%s\" not found\n",
186 dev
->info
->name
, name
);
189 if (!prop
->info
->parse
) {
190 fprintf(stderr
, "property \"%s.%s\" has no parser\n",
191 dev
->info
->name
, name
);
194 return prop
->info
->parse(dev
, prop
, value
);
197 void qdev_prop_set(DeviceState
*dev
, const char *name
, void *src
, enum PropertyType type
)
202 prop
= qdev_prop_find(dev
, name
);
204 fprintf(stderr
, "%s: property \"%s.%s\" not found\n",
205 __FUNCTION__
, dev
->info
->name
, name
);
208 if (prop
->info
->type
!= type
) {
209 fprintf(stderr
, "%s: property \"%s.%s\" type mismatch\n",
210 __FUNCTION__
, dev
->info
->name
, name
);
213 dst
= qdev_get_prop_ptr(dev
, prop
);
214 memcpy(dst
, src
, prop
->info
->size
);
217 void qdev_prop_set_uint16(DeviceState
*dev
, const char *name
, uint16_t value
)
219 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_UINT16
);
222 void qdev_prop_set_uint32(DeviceState
*dev
, const char *name
, uint32_t value
)
224 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_UINT32
);
227 void qdev_prop_set_ptr(DeviceState
*dev
, const char *name
, void *value
)
229 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_PTR
);
232 void qdev_prop_set_defaults(DeviceState
*dev
, Property
*props
)
238 while (props
->name
) {
240 dst
= qdev_get_prop_ptr(dev
, props
);
241 memcpy(dst
, props
->defval
, props
->info
->size
);
247 static CompatProperty
*compat_props
;
249 void qdev_prop_register_compat(CompatProperty
*props
)
251 compat_props
= props
;
254 void qdev_prop_set_compat(DeviceState
*dev
)
256 CompatProperty
*prop
;
261 for (prop
= compat_props
; prop
->driver
!= NULL
; prop
++) {
262 if (strcmp(dev
->info
->name
, prop
->driver
) != 0) {
265 if (qdev_prop_parse(dev
, prop
->property
, prop
->value
) != 0) {