Merge branch 'master' of git://git.qemu.org/qemu
[qemu/mdroth.git] / hw / qdev-properties.c
blob0c0c29212d5a3ce8ef4d88b3d695022a21c1fe0a
1 #include "net.h"
2 #include "qdev.h"
3 #include "qerror.h"
4 #include "blockdev.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 uint32_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 char *end;
73 /* accept both hex and decimal */
74 *ptr = strtoul(str, &end, 0);
75 if ((*end != '\0') || (end == str)) {
76 return -EINVAL;
79 return 0;
82 static int print_uint8(DeviceState *dev, Property *prop, char *dest, size_t len)
84 uint8_t *ptr = qdev_get_prop_ptr(dev, prop);
85 return snprintf(dest, len, "%" PRIu8, *ptr);
88 PropertyInfo qdev_prop_uint8 = {
89 .name = "uint8",
90 .type = PROP_TYPE_UINT8,
91 .size = sizeof(uint8_t),
92 .parse = parse_uint8,
93 .print = print_uint8,
96 /* --- 16bit integer --- */
98 static int parse_uint16(DeviceState *dev, Property *prop, const char *str)
100 uint16_t *ptr = qdev_get_prop_ptr(dev, prop);
101 char *end;
103 /* accept both hex and decimal */
104 *ptr = strtoul(str, &end, 0);
105 if ((*end != '\0') || (end == str)) {
106 return -EINVAL;
109 return 0;
112 static int print_uint16(DeviceState *dev, Property *prop, char *dest, size_t len)
114 uint16_t *ptr = qdev_get_prop_ptr(dev, prop);
115 return snprintf(dest, len, "%" PRIu16, *ptr);
118 PropertyInfo qdev_prop_uint16 = {
119 .name = "uint16",
120 .type = PROP_TYPE_UINT16,
121 .size = sizeof(uint16_t),
122 .parse = parse_uint16,
123 .print = print_uint16,
126 /* --- 32bit integer --- */
128 static int parse_uint32(DeviceState *dev, Property *prop, const char *str)
130 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
131 char *end;
133 /* accept both hex and decimal */
134 *ptr = strtoul(str, &end, 0);
135 if ((*end != '\0') || (end == str)) {
136 return -EINVAL;
139 return 0;
142 static int print_uint32(DeviceState *dev, Property *prop, char *dest, size_t len)
144 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
145 return snprintf(dest, len, "%" PRIu32, *ptr);
148 PropertyInfo qdev_prop_uint32 = {
149 .name = "uint32",
150 .type = PROP_TYPE_UINT32,
151 .size = sizeof(uint32_t),
152 .parse = parse_uint32,
153 .print = print_uint32,
156 static int parse_int32(DeviceState *dev, Property *prop, const char *str)
158 int32_t *ptr = qdev_get_prop_ptr(dev, prop);
159 char *end;
161 *ptr = strtol(str, &end, 10);
162 if ((*end != '\0') || (end == str)) {
163 return -EINVAL;
166 return 0;
169 static int print_int32(DeviceState *dev, Property *prop, char *dest, size_t len)
171 int32_t *ptr = qdev_get_prop_ptr(dev, prop);
172 return snprintf(dest, len, "%" PRId32, *ptr);
175 PropertyInfo qdev_prop_int32 = {
176 .name = "int32",
177 .type = PROP_TYPE_INT32,
178 .size = sizeof(int32_t),
179 .parse = parse_int32,
180 .print = print_int32,
183 /* --- 32bit hex value --- */
185 static int parse_hex32(DeviceState *dev, Property *prop, const char *str)
187 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
188 char *end;
190 *ptr = strtoul(str, &end, 16);
191 if ((*end != '\0') || (end == str)) {
192 return -EINVAL;
195 return 0;
198 static int print_hex32(DeviceState *dev, Property *prop, char *dest, size_t len)
200 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
201 return snprintf(dest, len, "0x%" PRIx32, *ptr);
204 PropertyInfo qdev_prop_hex32 = {
205 .name = "hex32",
206 .type = PROP_TYPE_UINT32,
207 .size = sizeof(uint32_t),
208 .parse = parse_hex32,
209 .print = print_hex32,
212 /* --- 64bit integer --- */
214 static int parse_uint64(DeviceState *dev, Property *prop, const char *str)
216 uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
217 char *end;
219 /* accept both hex and decimal */
220 *ptr = strtoull(str, &end, 0);
221 if ((*end != '\0') || (end == str)) {
222 return -EINVAL;
225 return 0;
228 static int print_uint64(DeviceState *dev, Property *prop, char *dest, size_t len)
230 uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
231 return snprintf(dest, len, "%" PRIu64, *ptr);
234 PropertyInfo qdev_prop_uint64 = {
235 .name = "uint64",
236 .type = PROP_TYPE_UINT64,
237 .size = sizeof(uint64_t),
238 .parse = parse_uint64,
239 .print = print_uint64,
242 /* --- 64bit hex value --- */
244 static int parse_hex64(DeviceState *dev, Property *prop, const char *str)
246 uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
247 char *end;
249 *ptr = strtoull(str, &end, 16);
250 if ((*end != '\0') || (end == str)) {
251 return -EINVAL;
254 return 0;
257 static int print_hex64(DeviceState *dev, Property *prop, char *dest, size_t len)
259 uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
260 return snprintf(dest, len, "0x%" PRIx64, *ptr);
263 PropertyInfo qdev_prop_hex64 = {
264 .name = "hex64",
265 .type = PROP_TYPE_UINT64,
266 .size = sizeof(uint64_t),
267 .parse = parse_hex64,
268 .print = print_hex64,
271 /* --- string --- */
273 static int parse_string(DeviceState *dev, Property *prop, const char *str)
275 char **ptr = qdev_get_prop_ptr(dev, prop);
277 if (*ptr)
278 g_free(*ptr);
279 *ptr = g_strdup(str);
280 return 0;
283 static void free_string(DeviceState *dev, Property *prop)
285 g_free(*(char **)qdev_get_prop_ptr(dev, prop));
288 static int print_string(DeviceState *dev, Property *prop, char *dest, size_t len)
290 char **ptr = qdev_get_prop_ptr(dev, prop);
291 if (!*ptr)
292 return snprintf(dest, len, "<null>");
293 return snprintf(dest, len, "\"%s\"", *ptr);
296 PropertyInfo qdev_prop_string = {
297 .name = "string",
298 .type = PROP_TYPE_STRING,
299 .size = sizeof(char*),
300 .parse = parse_string,
301 .print = print_string,
302 .free = free_string,
305 /* --- drive --- */
307 static int parse_drive(DeviceState *dev, Property *prop, const char *str)
309 BlockDriverState **ptr = qdev_get_prop_ptr(dev, prop);
310 BlockDriverState *bs;
312 bs = bdrv_find(str);
313 if (bs == NULL)
314 return -ENOENT;
315 if (bdrv_attach(bs, dev) < 0)
316 return -EEXIST;
317 *ptr = bs;
318 return 0;
321 static void free_drive(DeviceState *dev, Property *prop)
323 BlockDriverState **ptr = qdev_get_prop_ptr(dev, prop);
325 if (*ptr) {
326 bdrv_detach(*ptr, dev);
327 blockdev_auto_del(*ptr);
331 static int print_drive(DeviceState *dev, Property *prop, char *dest, size_t len)
333 BlockDriverState **ptr = qdev_get_prop_ptr(dev, prop);
334 return snprintf(dest, len, "%s",
335 *ptr ? bdrv_get_device_name(*ptr) : "<null>");
338 PropertyInfo qdev_prop_drive = {
339 .name = "drive",
340 .type = PROP_TYPE_DRIVE,
341 .size = sizeof(BlockDriverState *),
342 .parse = parse_drive,
343 .print = print_drive,
344 .free = free_drive,
347 /* --- character device --- */
349 static int parse_chr(DeviceState *dev, Property *prop, const char *str)
351 CharDriverState **ptr = qdev_get_prop_ptr(dev, prop);
353 *ptr = qemu_chr_find(str);
354 if (*ptr == NULL) {
355 return -ENOENT;
357 if ((*ptr)->avail_connections < 1) {
358 return -EEXIST;
360 --(*ptr)->avail_connections;
361 return 0;
364 static int print_chr(DeviceState *dev, Property *prop, char *dest, size_t len)
366 CharDriverState **ptr = qdev_get_prop_ptr(dev, prop);
368 if (*ptr && (*ptr)->label) {
369 return snprintf(dest, len, "%s", (*ptr)->label);
370 } else {
371 return snprintf(dest, len, "<null>");
375 PropertyInfo qdev_prop_chr = {
376 .name = "chr",
377 .type = PROP_TYPE_CHR,
378 .size = sizeof(CharDriverState*),
379 .parse = parse_chr,
380 .print = print_chr,
383 /* --- netdev device --- */
385 static int parse_netdev(DeviceState *dev, Property *prop, const char *str)
387 VLANClientState **ptr = qdev_get_prop_ptr(dev, prop);
389 *ptr = qemu_find_netdev(str);
390 if (*ptr == NULL)
391 return -ENOENT;
392 if ((*ptr)->peer) {
393 return -EEXIST;
395 return 0;
398 static int print_netdev(DeviceState *dev, Property *prop, char *dest, size_t len)
400 VLANClientState **ptr = qdev_get_prop_ptr(dev, prop);
402 if (*ptr && (*ptr)->name) {
403 return snprintf(dest, len, "%s", (*ptr)->name);
404 } else {
405 return snprintf(dest, len, "<null>");
409 PropertyInfo qdev_prop_netdev = {
410 .name = "netdev",
411 .type = PROP_TYPE_NETDEV,
412 .size = sizeof(VLANClientState*),
413 .parse = parse_netdev,
414 .print = print_netdev,
417 /* --- vlan --- */
419 static int parse_vlan(DeviceState *dev, Property *prop, const char *str)
421 VLANState **ptr = qdev_get_prop_ptr(dev, prop);
422 int id;
424 if (sscanf(str, "%d", &id) != 1)
425 return -EINVAL;
426 *ptr = qemu_find_vlan(id, 1);
427 if (*ptr == NULL)
428 return -ENOENT;
429 return 0;
432 static int print_vlan(DeviceState *dev, Property *prop, char *dest, size_t len)
434 VLANState **ptr = qdev_get_prop_ptr(dev, prop);
436 if (*ptr) {
437 return snprintf(dest, len, "%d", (*ptr)->id);
438 } else {
439 return snprintf(dest, len, "<null>");
443 PropertyInfo qdev_prop_vlan = {
444 .name = "vlan",
445 .type = PROP_TYPE_VLAN,
446 .size = sizeof(VLANClientState*),
447 .parse = parse_vlan,
448 .print = print_vlan,
451 /* --- pointer --- */
453 /* Not a proper property, just for dirty hacks. TODO Remove it! */
454 PropertyInfo qdev_prop_ptr = {
455 .name = "ptr",
456 .type = PROP_TYPE_PTR,
457 .size = sizeof(void*),
460 /* --- mac address --- */
463 * accepted syntax versions:
464 * 01:02:03:04:05:06
465 * 01-02-03-04-05-06
467 static int parse_mac(DeviceState *dev, Property *prop, const char *str)
469 MACAddr *mac = qdev_get_prop_ptr(dev, prop);
470 int i, pos;
471 char *p;
473 for (i = 0, pos = 0; i < 6; i++, pos += 3) {
474 if (!qemu_isxdigit(str[pos]))
475 return -EINVAL;
476 if (!qemu_isxdigit(str[pos+1]))
477 return -EINVAL;
478 if (i == 5) {
479 if (str[pos+2] != '\0')
480 return -EINVAL;
481 } else {
482 if (str[pos+2] != ':' && str[pos+2] != '-')
483 return -EINVAL;
485 mac->a[i] = strtol(str+pos, &p, 16);
487 return 0;
490 static int print_mac(DeviceState *dev, Property *prop, char *dest, size_t len)
492 MACAddr *mac = qdev_get_prop_ptr(dev, prop);
494 return snprintf(dest, len, "%02x:%02x:%02x:%02x:%02x:%02x",
495 mac->a[0], mac->a[1], mac->a[2],
496 mac->a[3], mac->a[4], mac->a[5]);
499 PropertyInfo qdev_prop_macaddr = {
500 .name = "macaddr",
501 .type = PROP_TYPE_MACADDR,
502 .size = sizeof(MACAddr),
503 .parse = parse_mac,
504 .print = print_mac,
507 /* --- pci address --- */
510 * bus-local address, i.e. "$slot" or "$slot.$fn"
512 static int parse_pci_devfn(DeviceState *dev, Property *prop, const char *str)
514 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
515 unsigned int slot, fn, n;
517 if (sscanf(str, "%x.%x%n", &slot, &fn, &n) != 2) {
518 fn = 0;
519 if (sscanf(str, "%x%n", &slot, &n) != 1) {
520 return -EINVAL;
523 if (str[n] != '\0')
524 return -EINVAL;
525 if (fn > 7)
526 return -EINVAL;
527 *ptr = slot << 3 | fn;
528 return 0;
531 static int print_pci_devfn(DeviceState *dev, Property *prop, char *dest, size_t len)
533 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
535 if (*ptr == -1) {
536 return snprintf(dest, len, "<unset>");
537 } else {
538 return snprintf(dest, len, "%02x.%x", *ptr >> 3, *ptr & 7);
542 PropertyInfo qdev_prop_pci_devfn = {
543 .name = "pci-devfn",
544 .type = PROP_TYPE_UINT32,
545 .size = sizeof(uint32_t),
546 .parse = parse_pci_devfn,
547 .print = print_pci_devfn,
550 /* --- public helpers --- */
552 static Property *qdev_prop_walk(Property *props, const char *name)
554 if (!props)
555 return NULL;
556 while (props->name) {
557 if (strcmp(props->name, name) == 0)
558 return props;
559 props++;
561 return NULL;
564 static Property *qdev_prop_find(DeviceState *dev, const char *name)
566 Property *prop;
568 /* device properties */
569 prop = qdev_prop_walk(dev->info->props, name);
570 if (prop)
571 return prop;
573 /* bus properties */
574 prop = qdev_prop_walk(dev->parent_bus->info->props, name);
575 if (prop)
576 return prop;
578 return NULL;
581 int qdev_prop_exists(DeviceState *dev, const char *name)
583 return qdev_prop_find(dev, name) ? true : false;
586 int qdev_prop_parse(DeviceState *dev, const char *name, const char *value)
588 Property *prop;
589 int ret;
591 prop = qdev_prop_find(dev, name);
593 * TODO Properties without a parse method are just for dirty
594 * hacks. qdev_prop_ptr is the only such PropertyInfo. It's
595 * marked for removal. The test !prop->info->parse should be
596 * removed along with it.
598 if (!prop || !prop->info->parse) {
599 qerror_report(QERR_PROPERTY_NOT_FOUND, dev->info->name, name);
600 return -1;
602 ret = prop->info->parse(dev, prop, value);
603 if (ret < 0) {
604 switch (ret) {
605 case -EEXIST:
606 qerror_report(QERR_PROPERTY_VALUE_IN_USE,
607 dev->info->name, name, value);
608 break;
609 default:
610 case -EINVAL:
611 qerror_report(QERR_PROPERTY_VALUE_BAD,
612 dev->info->name, name, value);
613 break;
614 case -ENOENT:
615 qerror_report(QERR_PROPERTY_VALUE_NOT_FOUND,
616 dev->info->name, name, value);
617 break;
619 return -1;
621 return 0;
624 void qdev_prop_set(DeviceState *dev, const char *name, void *src, enum PropertyType type)
626 Property *prop;
628 prop = qdev_prop_find(dev, name);
629 if (!prop) {
630 fprintf(stderr, "%s: property \"%s.%s\" not found\n",
631 __FUNCTION__, dev->info->name, name);
632 abort();
634 if (prop->info->type != type) {
635 fprintf(stderr, "%s: property \"%s.%s\" type mismatch\n",
636 __FUNCTION__, dev->info->name, name);
637 abort();
639 qdev_prop_cpy(dev, prop, src);
642 void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value)
644 qdev_prop_set(dev, name, &value, PROP_TYPE_BIT);
647 void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value)
649 qdev_prop_set(dev, name, &value, PROP_TYPE_UINT8);
652 void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value)
654 qdev_prop_set(dev, name, &value, PROP_TYPE_UINT16);
657 void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value)
659 qdev_prop_set(dev, name, &value, PROP_TYPE_UINT32);
662 void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value)
664 qdev_prop_set(dev, name, &value, PROP_TYPE_INT32);
667 void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value)
669 qdev_prop_set(dev, name, &value, PROP_TYPE_UINT64);
672 void qdev_prop_set_string(DeviceState *dev, const char *name, char *value)
674 qdev_prop_set(dev, name, &value, PROP_TYPE_STRING);
677 int qdev_prop_set_drive(DeviceState *dev, const char *name, BlockDriverState *value)
679 int res;
681 res = bdrv_attach(value, dev);
682 if (res < 0) {
683 error_report("Can't attach drive %s to %s.%s: %s",
684 bdrv_get_device_name(value),
685 dev->id ? dev->id : dev->info->name,
686 name, strerror(-res));
687 return -1;
689 qdev_prop_set(dev, name, &value, PROP_TYPE_DRIVE);
690 return 0;
693 void qdev_prop_set_drive_nofail(DeviceState *dev, const char *name, BlockDriverState *value)
695 if (qdev_prop_set_drive(dev, name, value) < 0) {
696 exit(1);
699 void qdev_prop_set_chr(DeviceState *dev, const char *name, CharDriverState *value)
701 qdev_prop_set(dev, name, &value, PROP_TYPE_CHR);
704 void qdev_prop_set_netdev(DeviceState *dev, const char *name, VLANClientState *value)
706 qdev_prop_set(dev, name, &value, PROP_TYPE_NETDEV);
709 void qdev_prop_set_vlan(DeviceState *dev, const char *name, VLANState *value)
711 qdev_prop_set(dev, name, &value, PROP_TYPE_VLAN);
714 void qdev_prop_set_macaddr(DeviceState *dev, const char *name, uint8_t *value)
716 qdev_prop_set(dev, name, value, PROP_TYPE_MACADDR);
719 void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value)
721 qdev_prop_set(dev, name, &value, PROP_TYPE_PTR);
724 void qdev_prop_set_defaults(DeviceState *dev, Property *props)
726 if (!props)
727 return;
728 while (props->name) {
729 if (props->defval) {
730 qdev_prop_cpy(dev, props, props->defval);
732 props++;
736 static QTAILQ_HEAD(, GlobalProperty) global_props = QTAILQ_HEAD_INITIALIZER(global_props);
738 static void qdev_prop_register_global(GlobalProperty *prop)
740 QTAILQ_INSERT_TAIL(&global_props, prop, next);
743 void qdev_prop_register_global_list(GlobalProperty *props)
745 int i;
747 for (i = 0; props[i].driver != NULL; i++) {
748 qdev_prop_register_global(props+i);
752 void qdev_prop_set_globals(DeviceState *dev)
754 GlobalProperty *prop;
756 QTAILQ_FOREACH(prop, &global_props, next) {
757 if (strcmp(dev->info->name, prop->driver) != 0 &&
758 strcmp(dev->info->bus_info->name, prop->driver) != 0) {
759 continue;
761 if (qdev_prop_parse(dev, prop->property, prop->value) != 0) {
762 exit(1);
767 static int qdev_add_one_global(QemuOpts *opts, void *opaque)
769 GlobalProperty *g;
771 g = g_malloc0(sizeof(*g));
772 g->driver = qemu_opt_get(opts, "driver");
773 g->property = qemu_opt_get(opts, "property");
774 g->value = qemu_opt_get(opts, "value");
775 qdev_prop_register_global(g);
776 return 0;
779 void qemu_add_globals(void)
781 qemu_opts_foreach(qemu_find_opts("global"), qdev_add_one_global, NULL, 0);