move two variable declarations out of vl.c
[qemu/lumag.git] / hw / qdev-properties.c
blob157a11166f393cc89571ae8746bfc70c8c5524cd
1 #include "sysemu.h"
2 #include "net.h"
3 #include "qdev.h"
4 #include "qerror.h"
6 void *qdev_get_prop_ptr(DeviceState *dev, Property *prop)
8 void *ptr = dev;
9 ptr += prop->offset;
10 return ptr;
13 static uint32_t qdev_get_prop_mask(Property *prop)
15 assert(prop->info->type == PROP_TYPE_BIT);
16 return 0x1 << prop->bitnr;
19 static void bit_prop_set(DeviceState *dev, Property *props, bool val)
21 uint32_t *p = qdev_get_prop_ptr(dev, props);
22 uint32_t mask = qdev_get_prop_mask(props);
23 if (val)
24 *p |= mask;
25 else
26 *p &= ~mask;
29 static void qdev_prop_cpy(DeviceState *dev, Property *props, void *src)
31 if (props->info->type == PROP_TYPE_BIT) {
32 bool *defval = src;
33 bit_prop_set(dev, props, *defval);
34 } else {
35 char *dst = qdev_get_prop_ptr(dev, props);
36 memcpy(dst, src, props->info->size);
40 /* Bit */
41 static int parse_bit(DeviceState *dev, Property *prop, const char *str)
43 if (!strncasecmp(str, "on", 2))
44 bit_prop_set(dev, prop, true);
45 else if (!strncasecmp(str, "off", 3))
46 bit_prop_set(dev, prop, false);
47 else
48 return -EINVAL;
49 return 0;
52 static int print_bit(DeviceState *dev, Property *prop, char *dest, size_t len)
54 uint8_t *p = qdev_get_prop_ptr(dev, prop);
55 return snprintf(dest, len, (*p & qdev_get_prop_mask(prop)) ? "on" : "off");
58 PropertyInfo qdev_prop_bit = {
59 .name = "on/off",
60 .type = PROP_TYPE_BIT,
61 .size = sizeof(uint32_t),
62 .parse = parse_bit,
63 .print = print_bit,
66 /* --- 8bit integer --- */
68 static int parse_uint8(DeviceState *dev, Property *prop, const char *str)
70 uint8_t *ptr = qdev_get_prop_ptr(dev, prop);
71 const char *fmt;
73 /* accept both hex and decimal */
74 fmt = strncasecmp(str, "0x",2) == 0 ? "%" PRIx8 : "%" PRIu8;
75 if (sscanf(str, fmt, ptr) != 1)
76 return -EINVAL;
77 return 0;
80 static int print_uint8(DeviceState *dev, Property *prop, char *dest, size_t len)
82 uint8_t *ptr = qdev_get_prop_ptr(dev, prop);
83 return snprintf(dest, len, "%" PRIu8, *ptr);
86 PropertyInfo qdev_prop_uint8 = {
87 .name = "uint8",
88 .type = PROP_TYPE_UINT8,
89 .size = sizeof(uint8_t),
90 .parse = parse_uint8,
91 .print = print_uint8,
94 /* --- 16bit integer --- */
96 static int parse_uint16(DeviceState *dev, Property *prop, const char *str)
98 uint16_t *ptr = qdev_get_prop_ptr(dev, prop);
99 const char *fmt;
101 /* accept both hex and decimal */
102 fmt = strncasecmp(str, "0x",2) == 0 ? "%" PRIx16 : "%" PRIu16;
103 if (sscanf(str, fmt, ptr) != 1)
104 return -EINVAL;
105 return 0;
108 static int print_uint16(DeviceState *dev, Property *prop, char *dest, size_t len)
110 uint16_t *ptr = qdev_get_prop_ptr(dev, prop);
111 return snprintf(dest, len, "%" PRIu16, *ptr);
114 PropertyInfo qdev_prop_uint16 = {
115 .name = "uint16",
116 .type = PROP_TYPE_UINT16,
117 .size = sizeof(uint16_t),
118 .parse = parse_uint16,
119 .print = print_uint16,
122 /* --- 32bit integer --- */
124 static int parse_uint32(DeviceState *dev, Property *prop, const char *str)
126 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
127 const char *fmt;
129 /* accept both hex and decimal */
130 fmt = strncasecmp(str, "0x",2) == 0 ? "%" PRIx32 : "%" PRIu32;
131 if (sscanf(str, fmt, ptr) != 1)
132 return -EINVAL;
133 return 0;
136 static int print_uint32(DeviceState *dev, Property *prop, char *dest, size_t len)
138 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
139 return snprintf(dest, len, "%" PRIu32, *ptr);
142 PropertyInfo qdev_prop_uint32 = {
143 .name = "uint32",
144 .type = PROP_TYPE_UINT32,
145 .size = sizeof(uint32_t),
146 .parse = parse_uint32,
147 .print = print_uint32,
150 static int parse_int32(DeviceState *dev, Property *prop, const char *str)
152 int32_t *ptr = qdev_get_prop_ptr(dev, prop);
154 if (sscanf(str, "%" PRId32, ptr) != 1)
155 return -EINVAL;
156 return 0;
159 static int print_int32(DeviceState *dev, Property *prop, char *dest, size_t len)
161 int32_t *ptr = qdev_get_prop_ptr(dev, prop);
162 return snprintf(dest, len, "%" PRId32, *ptr);
165 PropertyInfo qdev_prop_int32 = {
166 .name = "int32",
167 .type = PROP_TYPE_INT32,
168 .size = sizeof(int32_t),
169 .parse = parse_int32,
170 .print = print_int32,
173 /* --- 32bit hex value --- */
175 static int parse_hex32(DeviceState *dev, Property *prop, const char *str)
177 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
179 if (sscanf(str, "%" PRIx32, ptr) != 1)
180 return -EINVAL;
181 return 0;
184 static int print_hex32(DeviceState *dev, Property *prop, char *dest, size_t len)
186 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
187 return snprintf(dest, len, "0x%" PRIx32, *ptr);
190 PropertyInfo qdev_prop_hex32 = {
191 .name = "hex32",
192 .type = PROP_TYPE_UINT32,
193 .size = sizeof(uint32_t),
194 .parse = parse_hex32,
195 .print = print_hex32,
198 /* --- 64bit integer --- */
200 static int parse_uint64(DeviceState *dev, Property *prop, const char *str)
202 uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
203 const char *fmt;
205 /* accept both hex and decimal */
206 fmt = strncasecmp(str, "0x",2) == 0 ? "%" PRIx64 : "%" PRIu64;
207 if (sscanf(str, fmt, ptr) != 1)
208 return -EINVAL;
209 return 0;
212 static int print_uint64(DeviceState *dev, Property *prop, char *dest, size_t len)
214 uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
215 return snprintf(dest, len, "%" PRIu64, *ptr);
218 PropertyInfo qdev_prop_uint64 = {
219 .name = "uint64",
220 .type = PROP_TYPE_UINT64,
221 .size = sizeof(uint64_t),
222 .parse = parse_uint64,
223 .print = print_uint64,
226 /* --- 64bit hex value --- */
228 static int parse_hex64(DeviceState *dev, Property *prop, const char *str)
230 uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
232 if (sscanf(str, "%" PRIx64, ptr) != 1)
233 return -EINVAL;
234 return 0;
237 static int print_hex64(DeviceState *dev, Property *prop, char *dest, size_t len)
239 uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
240 return snprintf(dest, len, "0x%" PRIx64, *ptr);
243 PropertyInfo qdev_prop_hex64 = {
244 .name = "hex64",
245 .type = PROP_TYPE_UINT64,
246 .size = sizeof(uint64_t),
247 .parse = parse_hex64,
248 .print = print_hex64,
251 /* --- string --- */
253 static int parse_string(DeviceState *dev, Property *prop, const char *str)
255 char **ptr = qdev_get_prop_ptr(dev, prop);
257 if (*ptr)
258 qemu_free(*ptr);
259 *ptr = qemu_strdup(str);
260 return 0;
263 static int print_string(DeviceState *dev, Property *prop, char *dest, size_t len)
265 char **ptr = qdev_get_prop_ptr(dev, prop);
266 if (!*ptr)
267 return snprintf(dest, len, "<null>");
268 return snprintf(dest, len, "\"%s\"", *ptr);
271 PropertyInfo qdev_prop_string = {
272 .name = "string",
273 .type = PROP_TYPE_STRING,
274 .size = sizeof(char*),
275 .parse = parse_string,
276 .print = print_string,
279 /* --- drive --- */
281 static int parse_drive(DeviceState *dev, Property *prop, const char *str)
283 DriveInfo **ptr = qdev_get_prop_ptr(dev, prop);
285 *ptr = drive_get_by_id(str);
286 if (*ptr == NULL)
287 return -ENOENT;
288 return 0;
291 static int print_drive(DeviceState *dev, Property *prop, char *dest, size_t len)
293 DriveInfo **ptr = qdev_get_prop_ptr(dev, prop);
294 return snprintf(dest, len, "%s", (*ptr) ? (*ptr)->id : "<null>");
297 PropertyInfo qdev_prop_drive = {
298 .name = "drive",
299 .type = PROP_TYPE_DRIVE,
300 .size = sizeof(DriveInfo*),
301 .parse = parse_drive,
302 .print = print_drive,
305 /* --- character device --- */
307 static int parse_chr(DeviceState *dev, Property *prop, const char *str)
309 CharDriverState **ptr = qdev_get_prop_ptr(dev, prop);
311 *ptr = qemu_chr_find(str);
312 if (*ptr == NULL)
313 return -ENOENT;
314 return 0;
317 static int print_chr(DeviceState *dev, Property *prop, char *dest, size_t len)
319 CharDriverState **ptr = qdev_get_prop_ptr(dev, prop);
321 if (*ptr && (*ptr)->label) {
322 return snprintf(dest, len, "%s", (*ptr)->label);
323 } else {
324 return snprintf(dest, len, "<null>");
328 PropertyInfo qdev_prop_chr = {
329 .name = "chr",
330 .type = PROP_TYPE_CHR,
331 .size = sizeof(CharDriverState*),
332 .parse = parse_chr,
333 .print = print_chr,
336 /* --- netdev device --- */
338 static int parse_netdev(DeviceState *dev, Property *prop, const char *str)
340 VLANClientState **ptr = qdev_get_prop_ptr(dev, prop);
342 *ptr = qemu_find_netdev(str);
343 if (*ptr == NULL)
344 return -ENOENT;
345 if ((*ptr)->peer) {
346 return -EEXIST;
348 return 0;
351 static int print_netdev(DeviceState *dev, Property *prop, char *dest, size_t len)
353 VLANClientState **ptr = qdev_get_prop_ptr(dev, prop);
355 if (*ptr && (*ptr)->name) {
356 return snprintf(dest, len, "%s", (*ptr)->name);
357 } else {
358 return snprintf(dest, len, "<null>");
362 PropertyInfo qdev_prop_netdev = {
363 .name = "netdev",
364 .type = PROP_TYPE_NETDEV,
365 .size = sizeof(VLANClientState*),
366 .parse = parse_netdev,
367 .print = print_netdev,
370 /* --- vlan --- */
372 static int parse_vlan(DeviceState *dev, Property *prop, const char *str)
374 VLANState **ptr = qdev_get_prop_ptr(dev, prop);
375 int id;
377 if (sscanf(str, "%d", &id) != 1)
378 return -EINVAL;
379 *ptr = qemu_find_vlan(id, 1);
380 if (*ptr == NULL)
381 return -ENOENT;
382 return 0;
385 static int print_vlan(DeviceState *dev, Property *prop, char *dest, size_t len)
387 VLANState **ptr = qdev_get_prop_ptr(dev, prop);
389 if (*ptr) {
390 return snprintf(dest, len, "%d", (*ptr)->id);
391 } else {
392 return snprintf(dest, len, "<null>");
396 PropertyInfo qdev_prop_vlan = {
397 .name = "vlan",
398 .type = PROP_TYPE_VLAN,
399 .size = sizeof(VLANClientState*),
400 .parse = parse_vlan,
401 .print = print_vlan,
404 /* --- pointer --- */
406 /* Not a proper property, just for dirty hacks. TODO Remove it! */
407 PropertyInfo qdev_prop_ptr = {
408 .name = "ptr",
409 .type = PROP_TYPE_PTR,
410 .size = sizeof(void*),
413 /* --- mac address --- */
416 * accepted syntax versions:
417 * 01:02:03:04:05:06
418 * 01-02-03-04-05-06
420 static int parse_mac(DeviceState *dev, Property *prop, const char *str)
422 MACAddr *mac = qdev_get_prop_ptr(dev, prop);
423 int i, pos;
424 char *p;
426 for (i = 0, pos = 0; i < 6; i++, pos += 3) {
427 if (!qemu_isxdigit(str[pos]))
428 return -EINVAL;
429 if (!qemu_isxdigit(str[pos+1]))
430 return -EINVAL;
431 if (i == 5) {
432 if (str[pos+2] != '\0')
433 return -EINVAL;
434 } else {
435 if (str[pos+2] != ':' && str[pos+2] != '-')
436 return -EINVAL;
438 mac->a[i] = strtol(str+pos, &p, 16);
440 return 0;
443 static int print_mac(DeviceState *dev, Property *prop, char *dest, size_t len)
445 MACAddr *mac = qdev_get_prop_ptr(dev, prop);
447 return snprintf(dest, len, "%02x:%02x:%02x:%02x:%02x:%02x",
448 mac->a[0], mac->a[1], mac->a[2],
449 mac->a[3], mac->a[4], mac->a[5]);
452 PropertyInfo qdev_prop_macaddr = {
453 .name = "macaddr",
454 .type = PROP_TYPE_MACADDR,
455 .size = sizeof(MACAddr),
456 .parse = parse_mac,
457 .print = print_mac,
460 /* --- pci address --- */
463 * bus-local address, i.e. "$slot" or "$slot.$fn"
465 static int parse_pci_devfn(DeviceState *dev, Property *prop, const char *str)
467 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
468 unsigned int slot, fn, n;
470 if (sscanf(str, "%x.%x%n", &slot, &fn, &n) != 2) {
471 fn = 0;
472 if (sscanf(str, "%x%n", &slot, &n) != 1) {
473 return -EINVAL;
476 if (str[n] != '\0')
477 return -EINVAL;
478 if (fn > 7)
479 return -EINVAL;
480 *ptr = slot << 3 | fn;
481 return 0;
484 static int print_pci_devfn(DeviceState *dev, Property *prop, char *dest, size_t len)
486 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
488 if (*ptr == -1) {
489 return snprintf(dest, len, "<unset>");
490 } else {
491 return snprintf(dest, len, "%02x.%x", *ptr >> 3, *ptr & 7);
495 PropertyInfo qdev_prop_pci_devfn = {
496 .name = "pci-devfn",
497 .type = PROP_TYPE_UINT32,
498 .size = sizeof(uint32_t),
499 .parse = parse_pci_devfn,
500 .print = print_pci_devfn,
503 /* --- public helpers --- */
505 static Property *qdev_prop_walk(Property *props, const char *name)
507 if (!props)
508 return NULL;
509 while (props->name) {
510 if (strcmp(props->name, name) == 0)
511 return props;
512 props++;
514 return NULL;
517 static Property *qdev_prop_find(DeviceState *dev, const char *name)
519 Property *prop;
521 /* device properties */
522 prop = qdev_prop_walk(dev->info->props, name);
523 if (prop)
524 return prop;
526 /* bus properties */
527 prop = qdev_prop_walk(dev->parent_bus->info->props, name);
528 if (prop)
529 return prop;
531 return NULL;
534 int qdev_prop_exists(DeviceState *dev, const char *name)
536 return qdev_prop_find(dev, name) ? true : false;
539 int qdev_prop_parse(DeviceState *dev, const char *name, const char *value)
541 Property *prop;
542 int ret;
544 prop = qdev_prop_find(dev, name);
546 * TODO Properties without a parse method are just for dirty
547 * hacks. qdev_prop_ptr is the only such PropertyInfo. It's
548 * marked for removal. The test !prop->info->parse should be
549 * removed along with it.
551 if (!prop || !prop->info->parse) {
552 qerror_report(QERR_PROPERTY_NOT_FOUND, dev->info->name, name);
553 return -1;
555 ret = prop->info->parse(dev, prop, value);
556 if (ret < 0) {
557 switch (ret) {
558 case -EEXIST:
559 qerror_report(QERR_PROPERTY_VALUE_IN_USE,
560 dev->info->name, name, value);
561 break;
562 default:
563 case -EINVAL:
564 qerror_report(QERR_PROPERTY_VALUE_BAD,
565 dev->info->name, name, value);
566 break;
567 case -ENOENT:
568 qerror_report(QERR_PROPERTY_VALUE_NOT_FOUND,
569 dev->info->name, name, value);
570 break;
572 return -1;
574 return 0;
577 void qdev_prop_set(DeviceState *dev, const char *name, void *src, enum PropertyType type)
579 Property *prop;
581 prop = qdev_prop_find(dev, name);
582 if (!prop) {
583 fprintf(stderr, "%s: property \"%s.%s\" not found\n",
584 __FUNCTION__, dev->info->name, name);
585 abort();
587 if (prop->info->type != type) {
588 fprintf(stderr, "%s: property \"%s.%s\" type mismatch\n",
589 __FUNCTION__, dev->info->name, name);
590 abort();
592 qdev_prop_cpy(dev, prop, src);
595 void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value)
597 qdev_prop_set(dev, name, &value, PROP_TYPE_UINT8);
600 void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value)
602 qdev_prop_set(dev, name, &value, PROP_TYPE_UINT16);
605 void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value)
607 qdev_prop_set(dev, name, &value, PROP_TYPE_UINT32);
610 void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value)
612 qdev_prop_set(dev, name, &value, PROP_TYPE_INT32);
615 void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value)
617 qdev_prop_set(dev, name, &value, PROP_TYPE_UINT64);
620 void qdev_prop_set_drive(DeviceState *dev, const char *name, DriveInfo *value)
622 qdev_prop_set(dev, name, &value, PROP_TYPE_DRIVE);
625 void qdev_prop_set_chr(DeviceState *dev, const char *name, CharDriverState *value)
627 qdev_prop_set(dev, name, &value, PROP_TYPE_CHR);
630 void qdev_prop_set_netdev(DeviceState *dev, const char *name, VLANClientState *value)
632 qdev_prop_set(dev, name, &value, PROP_TYPE_NETDEV);
635 void qdev_prop_set_vlan(DeviceState *dev, const char *name, VLANState *value)
637 qdev_prop_set(dev, name, &value, PROP_TYPE_VLAN);
640 void qdev_prop_set_macaddr(DeviceState *dev, const char *name, uint8_t *value)
642 qdev_prop_set(dev, name, value, PROP_TYPE_MACADDR);
645 void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value)
647 qdev_prop_set(dev, name, &value, PROP_TYPE_PTR);
650 void qdev_prop_set_defaults(DeviceState *dev, Property *props)
652 if (!props)
653 return;
654 while (props->name) {
655 if (props->defval) {
656 qdev_prop_cpy(dev, props, props->defval);
658 props++;
662 static QTAILQ_HEAD(, GlobalProperty) global_props = QTAILQ_HEAD_INITIALIZER(global_props);
664 void qdev_prop_register_global(GlobalProperty *prop)
666 QTAILQ_INSERT_TAIL(&global_props, prop, next);
669 void qdev_prop_register_global_list(GlobalProperty *props)
671 int i;
673 for (i = 0; props[i].driver != NULL; i++) {
674 qdev_prop_register_global(props+i);
678 void qdev_prop_set_globals(DeviceState *dev)
680 GlobalProperty *prop;
682 QTAILQ_FOREACH(prop, &global_props, next) {
683 if (strcmp(dev->info->name, prop->driver) != 0 &&
684 strcmp(dev->info->bus_info->name, prop->driver) != 0) {
685 continue;
687 if (qdev_prop_parse(dev, prop->property, prop->value) != 0) {
688 exit(1);