root / hw / qdev-properties.c @ bfdb3629
History | View | Annotate | Download (8.8 kB)
1 |
#include "qdev.h" |
---|---|
2 |
|
3 |
void *qdev_get_prop_ptr(DeviceState *dev, Property *prop)
|
4 |
{ |
5 |
void *ptr = dev;
|
6 |
ptr += prop->offset; |
7 |
return ptr;
|
8 |
} |
9 |
|
10 |
/* --- 16bit integer --- */
|
11 |
|
12 |
static int parse_uint16(DeviceState *dev, Property *prop, const char *str) |
13 |
{ |
14 |
uint16_t *ptr = qdev_get_prop_ptr(dev, prop); |
15 |
const char *fmt; |
16 |
|
17 |
/* accept both hex and decimal */
|
18 |
fmt = strncasecmp(str, "0x",2) == 0 ? "%" PRIx16 : "%" PRIu16; |
19 |
if (sscanf(str, fmt, ptr) != 1) |
20 |
return -1; |
21 |
return 0; |
22 |
} |
23 |
|
24 |
static int print_uint16(DeviceState *dev, Property *prop, char *dest, size_t len) |
25 |
{ |
26 |
uint16_t *ptr = qdev_get_prop_ptr(dev, prop); |
27 |
return snprintf(dest, len, "%" PRIu16, *ptr); |
28 |
} |
29 |
|
30 |
PropertyInfo qdev_prop_uint16 = { |
31 |
.name = "uint16",
|
32 |
.type = PROP_TYPE_UINT16, |
33 |
.size = sizeof(uint16_t),
|
34 |
.parse = parse_uint16, |
35 |
.print = print_uint16, |
36 |
}; |
37 |
|
38 |
/* --- 32bit integer --- */
|
39 |
|
40 |
static int parse_uint32(DeviceState *dev, Property *prop, const char *str) |
41 |
{ |
42 |
uint32_t *ptr = qdev_get_prop_ptr(dev, prop); |
43 |
const char *fmt; |
44 |
|
45 |
/* accept both hex and decimal */
|
46 |
fmt = strncasecmp(str, "0x",2) == 0 ? "%" PRIx32 : "%" PRIu32; |
47 |
if (sscanf(str, fmt, ptr) != 1) |
48 |
return -1; |
49 |
return 0; |
50 |
} |
51 |
|
52 |
static int print_uint32(DeviceState *dev, Property *prop, char *dest, size_t len) |
53 |
{ |
54 |
uint32_t *ptr = qdev_get_prop_ptr(dev, prop); |
55 |
return snprintf(dest, len, "%" PRIu32, *ptr); |
56 |
} |
57 |
|
58 |
PropertyInfo qdev_prop_uint32 = { |
59 |
.name = "uint32",
|
60 |
.type = PROP_TYPE_UINT32, |
61 |
.size = sizeof(uint32_t),
|
62 |
.parse = parse_uint32, |
63 |
.print = print_uint32, |
64 |
}; |
65 |
|
66 |
/* --- 32bit hex value --- */
|
67 |
|
68 |
static int parse_hex32(DeviceState *dev, Property *prop, const char *str) |
69 |
{ |
70 |
uint32_t *ptr = qdev_get_prop_ptr(dev, prop); |
71 |
|
72 |
if (sscanf(str, "%" PRIx32, ptr) != 1) |
73 |
return -1; |
74 |
return 0; |
75 |
} |
76 |
|
77 |
static int print_hex32(DeviceState *dev, Property *prop, char *dest, size_t len) |
78 |
{ |
79 |
uint32_t *ptr = qdev_get_prop_ptr(dev, prop); |
80 |
return snprintf(dest, len, "0x%" PRIx32, *ptr); |
81 |
} |
82 |
|
83 |
PropertyInfo qdev_prop_hex32 = { |
84 |
.name = "hex32",
|
85 |
.type = PROP_TYPE_UINT32, |
86 |
.size = sizeof(uint32_t),
|
87 |
.parse = parse_hex32, |
88 |
.print = print_hex32, |
89 |
}; |
90 |
|
91 |
/* --- 64bit integer --- */
|
92 |
|
93 |
static int parse_uint64(DeviceState *dev, Property *prop, const char *str) |
94 |
{ |
95 |
uint64_t *ptr = qdev_get_prop_ptr(dev, prop); |
96 |
const char *fmt; |
97 |
|
98 |
/* accept both hex and decimal */
|
99 |
fmt = strncasecmp(str, "0x",2) == 0 ? "%" PRIx64 : "%" PRIu64; |
100 |
if (sscanf(str, fmt, ptr) != 1) |
101 |
return -1; |
102 |
return 0; |
103 |
} |
104 |
|
105 |
static int print_uint64(DeviceState *dev, Property *prop, char *dest, size_t len) |
106 |
{ |
107 |
uint64_t *ptr = qdev_get_prop_ptr(dev, prop); |
108 |
return snprintf(dest, len, "%" PRIu64, *ptr); |
109 |
} |
110 |
|
111 |
PropertyInfo qdev_prop_uint64 = { |
112 |
.name = "uint64",
|
113 |
.type = PROP_TYPE_UINT64, |
114 |
.size = sizeof(uint64_t),
|
115 |
.parse = parse_uint64, |
116 |
.print = print_uint64, |
117 |
}; |
118 |
|
119 |
/* --- 64bit hex value --- */
|
120 |
|
121 |
static int parse_hex64(DeviceState *dev, Property *prop, const char *str) |
122 |
{ |
123 |
uint64_t *ptr = qdev_get_prop_ptr(dev, prop); |
124 |
|
125 |
if (sscanf(str, "%" PRIx64, ptr) != 1) |
126 |
return -1; |
127 |
return 0; |
128 |
} |
129 |
|
130 |
static int print_hex64(DeviceState *dev, Property *prop, char *dest, size_t len) |
131 |
{ |
132 |
uint64_t *ptr = qdev_get_prop_ptr(dev, prop); |
133 |
return snprintf(dest, len, "0x%" PRIx64, *ptr); |
134 |
} |
135 |
|
136 |
PropertyInfo qdev_prop_hex64 = { |
137 |
.name = "hex64",
|
138 |
.type = PROP_TYPE_UINT64, |
139 |
.size = sizeof(uint64_t),
|
140 |
.parse = parse_hex64, |
141 |
.print = print_hex64, |
142 |
}; |
143 |
|
144 |
/* --- pointer --- */
|
145 |
|
146 |
static int print_ptr(DeviceState *dev, Property *prop, char *dest, size_t len) |
147 |
{ |
148 |
void **ptr = qdev_get_prop_ptr(dev, prop);
|
149 |
return snprintf(dest, len, "<%p>", *ptr); |
150 |
} |
151 |
|
152 |
PropertyInfo qdev_prop_ptr = { |
153 |
.name = "ptr",
|
154 |
.type = PROP_TYPE_PTR, |
155 |
.size = sizeof(void*), |
156 |
.print = print_ptr, |
157 |
}; |
158 |
|
159 |
/* --- mac address --- */
|
160 |
|
161 |
/*
|
162 |
* accepted syntax versions:
|
163 |
* 01:02:03:04:05:06
|
164 |
* 01-02-03-04-05-06
|
165 |
*/
|
166 |
static int parse_mac(DeviceState *dev, Property *prop, const char *str) |
167 |
{ |
168 |
uint8_t *mac = qdev_get_prop_ptr(dev, prop); |
169 |
int i, pos;
|
170 |
char *p;
|
171 |
|
172 |
for (i = 0, pos = 0; i < 6; i++, pos += 3) { |
173 |
if (!qemu_isxdigit(str[pos]))
|
174 |
return -1; |
175 |
if (!qemu_isxdigit(str[pos+1])) |
176 |
return -1; |
177 |
if (i == 5 && str[pos+2] != '\0') |
178 |
return -1; |
179 |
if (str[pos+2] != ':' && str[pos+2] != '-') |
180 |
return -1; |
181 |
mac[i] = strtol(str+pos, &p, 16);
|
182 |
} |
183 |
return 0; |
184 |
} |
185 |
|
186 |
static int print_mac(DeviceState *dev, Property *prop, char *dest, size_t len) |
187 |
{ |
188 |
uint8_t *mac = qdev_get_prop_ptr(dev, prop); |
189 |
return snprintf(dest, len, "%02x:%02x:%02x:%02x:%02x:%02x", |
190 |
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); |
191 |
} |
192 |
|
193 |
PropertyInfo qdev_prop_macaddr = { |
194 |
.name = "mac-addr",
|
195 |
.type = PROP_TYPE_MACADDR, |
196 |
.size = 6,
|
197 |
.parse = parse_mac, |
198 |
.print = print_mac, |
199 |
}; |
200 |
|
201 |
/* --- pci address --- */
|
202 |
|
203 |
/*
|
204 |
* bus-local address, i.e. "$slot" or "$slot.$fn"
|
205 |
*/
|
206 |
static int parse_pci_devfn(DeviceState *dev, Property *prop, const char *str) |
207 |
{ |
208 |
uint32_t *ptr = qdev_get_prop_ptr(dev, prop); |
209 |
unsigned int slot, fn, n; |
210 |
|
211 |
if (sscanf(str, "%x.%x%n", &slot, &fn, &n) != 2) { |
212 |
fn = 0;
|
213 |
if (sscanf(str, "%x%n", &slot, &n) != 1) { |
214 |
return -1; |
215 |
} |
216 |
} |
217 |
if (str[n] != '\0') |
218 |
return -1; |
219 |
if (fn > 7) |
220 |
return -1; |
221 |
*ptr = slot << 3 | fn;
|
222 |
return 0; |
223 |
} |
224 |
|
225 |
static int print_pci_devfn(DeviceState *dev, Property *prop, char *dest, size_t len) |
226 |
{ |
227 |
uint32_t *ptr = qdev_get_prop_ptr(dev, prop); |
228 |
|
229 |
if (-1 == *ptr) { |
230 |
return snprintf(dest, len, "<unset>"); |
231 |
} else {
|
232 |
return snprintf(dest, len, "%02x.%x", *ptr >> 3, *ptr & 7); |
233 |
} |
234 |
} |
235 |
|
236 |
PropertyInfo qdev_prop_pci_devfn = { |
237 |
.name = "pci-devfn",
|
238 |
.type = PROP_TYPE_UINT32, |
239 |
.size = sizeof(uint32_t),
|
240 |
.parse = parse_pci_devfn, |
241 |
.print = print_pci_devfn, |
242 |
}; |
243 |
|
244 |
/* --- public helpers --- */
|
245 |
|
246 |
static Property *qdev_prop_walk(Property *props, const char *name) |
247 |
{ |
248 |
if (!props)
|
249 |
return NULL; |
250 |
while (props->name) {
|
251 |
if (strcmp(props->name, name) == 0) |
252 |
return props;
|
253 |
props++; |
254 |
} |
255 |
return NULL; |
256 |
} |
257 |
|
258 |
static Property *qdev_prop_find(DeviceState *dev, const char *name) |
259 |
{ |
260 |
Property *prop; |
261 |
|
262 |
/* device properties */
|
263 |
prop = qdev_prop_walk(dev->info->props, name); |
264 |
if (prop)
|
265 |
return prop;
|
266 |
|
267 |
/* bus properties */
|
268 |
prop = qdev_prop_walk(dev->parent_bus->info->props, name); |
269 |
if (prop)
|
270 |
return prop;
|
271 |
|
272 |
return NULL; |
273 |
} |
274 |
|
275 |
int qdev_prop_parse(DeviceState *dev, const char *name, const char *value) |
276 |
{ |
277 |
Property *prop; |
278 |
|
279 |
prop = qdev_prop_find(dev, name); |
280 |
if (!prop) {
|
281 |
fprintf(stderr, "property \"%s.%s\" not found\n",
|
282 |
dev->info->name, name); |
283 |
return -1; |
284 |
} |
285 |
if (!prop->info->parse) {
|
286 |
fprintf(stderr, "property \"%s.%s\" has no parser\n",
|
287 |
dev->info->name, name); |
288 |
return -1; |
289 |
} |
290 |
return prop->info->parse(dev, prop, value);
|
291 |
} |
292 |
|
293 |
void qdev_prop_set(DeviceState *dev, const char *name, void *src, enum PropertyType type) |
294 |
{ |
295 |
Property *prop; |
296 |
void *dst;
|
297 |
|
298 |
prop = qdev_prop_find(dev, name); |
299 |
if (!prop) {
|
300 |
fprintf(stderr, "%s: property \"%s.%s\" not found\n",
|
301 |
__FUNCTION__, dev->info->name, name); |
302 |
abort(); |
303 |
} |
304 |
if (prop->info->type != type) {
|
305 |
fprintf(stderr, "%s: property \"%s.%s\" type mismatch\n",
|
306 |
__FUNCTION__, dev->info->name, name); |
307 |
abort(); |
308 |
} |
309 |
dst = qdev_get_prop_ptr(dev, prop); |
310 |
memcpy(dst, src, prop->info->size); |
311 |
} |
312 |
|
313 |
void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value) |
314 |
{ |
315 |
qdev_prop_set(dev, name, &value, PROP_TYPE_UINT16); |
316 |
} |
317 |
|
318 |
void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value) |
319 |
{ |
320 |
qdev_prop_set(dev, name, &value, PROP_TYPE_UINT32); |
321 |
} |
322 |
|
323 |
void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value) |
324 |
{ |
325 |
qdev_prop_set(dev, name, &value, PROP_TYPE_UINT64); |
326 |
} |
327 |
|
328 |
void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value) |
329 |
{ |
330 |
qdev_prop_set(dev, name, &value, PROP_TYPE_PTR); |
331 |
} |
332 |
|
333 |
void qdev_prop_set_defaults(DeviceState *dev, Property *props)
|
334 |
{ |
335 |
char *dst;
|
336 |
|
337 |
if (!props)
|
338 |
return;
|
339 |
while (props->name) {
|
340 |
if (props->defval) {
|
341 |
dst = qdev_get_prop_ptr(dev, props); |
342 |
memcpy(dst, props->defval, props->info->size); |
343 |
} |
344 |
props++; |
345 |
} |
346 |
} |
347 |
|
348 |
static CompatProperty *compat_props;
|
349 |
|
350 |
void qdev_prop_register_compat(CompatProperty *props)
|
351 |
{ |
352 |
compat_props = props; |
353 |
} |
354 |
|
355 |
void qdev_prop_set_compat(DeviceState *dev)
|
356 |
{ |
357 |
CompatProperty *prop; |
358 |
|
359 |
if (!compat_props) {
|
360 |
return;
|
361 |
} |
362 |
for (prop = compat_props; prop->driver != NULL; prop++) { |
363 |
if (strcmp(dev->info->name, prop->driver) != 0) { |
364 |
continue;
|
365 |
} |
366 |
if (qdev_prop_parse(dev, prop->property, prop->value) != 0) { |
367 |
abort(); |
368 |
} |
369 |
} |
370 |
} |