suspend: make acpi timer wakeup the guest.
[qemu/wangdongxu.git] / hw / qdev-properties.c
blob0423af1c311b1d7a9d284f5250d214b17538f475
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 == &qdev_prop_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 /* Bit */
31 static int print_bit(DeviceState *dev, Property *prop, char *dest, size_t len)
33 uint32_t *p = qdev_get_prop_ptr(dev, prop);
34 return snprintf(dest, len, (*p & qdev_get_prop_mask(prop)) ? "on" : "off");
37 static void get_bit(Object *obj, Visitor *v, void *opaque,
38 const char *name, Error **errp)
40 DeviceState *dev = DEVICE(obj);
41 Property *prop = opaque;
42 uint32_t *p = qdev_get_prop_ptr(dev, prop);
43 bool value = (*p & qdev_get_prop_mask(prop)) != 0;
45 visit_type_bool(v, &value, name, errp);
48 static void set_bit(Object *obj, Visitor *v, void *opaque,
49 const char *name, Error **errp)
51 DeviceState *dev = DEVICE(obj);
52 Property *prop = opaque;
53 Error *local_err = NULL;
54 bool value;
56 if (dev->state != DEV_STATE_CREATED) {
57 error_set(errp, QERR_PERMISSION_DENIED);
58 return;
61 visit_type_bool(v, &value, name, &local_err);
62 if (local_err) {
63 error_propagate(errp, local_err);
64 return;
66 bit_prop_set(dev, prop, value);
69 PropertyInfo qdev_prop_bit = {
70 .name = "boolean",
71 .legacy_name = "on/off",
72 .print = print_bit,
73 .get = get_bit,
74 .set = set_bit,
77 /* --- 8bit integer --- */
79 static void get_int8(Object *obj, Visitor *v, void *opaque,
80 const char *name, Error **errp)
82 DeviceState *dev = DEVICE(obj);
83 Property *prop = opaque;
84 int8_t *ptr = qdev_get_prop_ptr(dev, prop);
85 int64_t value;
87 value = *ptr;
88 visit_type_int(v, &value, name, errp);
91 static void set_int8(Object *obj, Visitor *v, void *opaque,
92 const char *name, Error **errp)
94 DeviceState *dev = DEVICE(obj);
95 Property *prop = opaque;
96 int8_t *ptr = qdev_get_prop_ptr(dev, prop);
97 Error *local_err = NULL;
98 int64_t value;
100 if (dev->state != DEV_STATE_CREATED) {
101 error_set(errp, QERR_PERMISSION_DENIED);
102 return;
105 visit_type_int(v, &value, name, &local_err);
106 if (local_err) {
107 error_propagate(errp, local_err);
108 return;
110 if (value >= prop->info->min && value <= prop->info->max) {
111 *ptr = value;
112 } else {
113 error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE,
114 dev->id?:"", name, value, prop->info->min,
115 prop->info->max);
119 PropertyInfo qdev_prop_uint8 = {
120 .name = "uint8",
121 .get = get_int8,
122 .set = set_int8,
123 .min = 0,
124 .max = 255,
127 /* --- 8bit hex value --- */
129 static int parse_hex8(DeviceState *dev, Property *prop, const char *str)
131 uint8_t *ptr = qdev_get_prop_ptr(dev, prop);
132 char *end;
134 if (str[0] != '0' || str[1] != 'x') {
135 return -EINVAL;
138 *ptr = strtoul(str, &end, 16);
139 if ((*end != '\0') || (end == str)) {
140 return -EINVAL;
143 return 0;
146 static int print_hex8(DeviceState *dev, Property *prop, char *dest, size_t len)
148 uint8_t *ptr = qdev_get_prop_ptr(dev, prop);
149 return snprintf(dest, len, "0x%" PRIx8, *ptr);
152 PropertyInfo qdev_prop_hex8 = {
153 .name = "uint8",
154 .legacy_name = "hex8",
155 .parse = parse_hex8,
156 .print = print_hex8,
157 .get = get_int8,
158 .set = set_int8,
159 .min = 0,
160 .max = 255,
163 /* --- 16bit integer --- */
165 static void get_int16(Object *obj, Visitor *v, void *opaque,
166 const char *name, Error **errp)
168 DeviceState *dev = DEVICE(obj);
169 Property *prop = opaque;
170 int16_t *ptr = qdev_get_prop_ptr(dev, prop);
171 int64_t value;
173 value = *ptr;
174 visit_type_int(v, &value, name, errp);
177 static void set_int16(Object *obj, Visitor *v, void *opaque,
178 const char *name, Error **errp)
180 DeviceState *dev = DEVICE(obj);
181 Property *prop = opaque;
182 int16_t *ptr = qdev_get_prop_ptr(dev, prop);
183 Error *local_err = NULL;
184 int64_t value;
186 if (dev->state != DEV_STATE_CREATED) {
187 error_set(errp, QERR_PERMISSION_DENIED);
188 return;
191 visit_type_int(v, &value, name, &local_err);
192 if (local_err) {
193 error_propagate(errp, local_err);
194 return;
196 if (value >= prop->info->min && value <= prop->info->max) {
197 *ptr = value;
198 } else {
199 error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE,
200 dev->id?:"", name, value, prop->info->min,
201 prop->info->max);
205 PropertyInfo qdev_prop_uint16 = {
206 .name = "uint16",
207 .get = get_int16,
208 .set = set_int16,
209 .min = 0,
210 .max = 65535,
213 /* --- 32bit integer --- */
215 static void get_int32(Object *obj, Visitor *v, void *opaque,
216 const char *name, Error **errp)
218 DeviceState *dev = DEVICE(obj);
219 Property *prop = opaque;
220 int32_t *ptr = qdev_get_prop_ptr(dev, prop);
221 int64_t value;
223 value = *ptr;
224 visit_type_int(v, &value, name, errp);
227 static void set_int32(Object *obj, Visitor *v, void *opaque,
228 const char *name, Error **errp)
230 DeviceState *dev = DEVICE(obj);
231 Property *prop = opaque;
232 int32_t *ptr = qdev_get_prop_ptr(dev, prop);
233 Error *local_err = NULL;
234 int64_t value;
236 if (dev->state != DEV_STATE_CREATED) {
237 error_set(errp, QERR_PERMISSION_DENIED);
238 return;
241 visit_type_int(v, &value, name, &local_err);
242 if (local_err) {
243 error_propagate(errp, local_err);
244 return;
246 if (value >= prop->info->min && value <= prop->info->max) {
247 *ptr = value;
248 } else {
249 error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE,
250 dev->id?:"", name, value, prop->info->min,
251 prop->info->max);
255 PropertyInfo qdev_prop_uint32 = {
256 .name = "uint32",
257 .get = get_int32,
258 .set = set_int32,
259 .min = 0,
260 .max = 0xFFFFFFFFULL,
263 PropertyInfo qdev_prop_int32 = {
264 .name = "int32",
265 .get = get_int32,
266 .set = set_int32,
267 .min = -0x80000000LL,
268 .max = 0x7FFFFFFFLL,
271 /* --- 32bit hex value --- */
273 static int parse_hex32(DeviceState *dev, Property *prop, const char *str)
275 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
276 char *end;
278 if (str[0] != '0' || str[1] != 'x') {
279 return -EINVAL;
282 *ptr = strtoul(str, &end, 16);
283 if ((*end != '\0') || (end == str)) {
284 return -EINVAL;
287 return 0;
290 static int print_hex32(DeviceState *dev, Property *prop, char *dest, size_t len)
292 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
293 return snprintf(dest, len, "0x%" PRIx32, *ptr);
296 PropertyInfo qdev_prop_hex32 = {
297 .name = "uint32",
298 .legacy_name = "hex32",
299 .parse = parse_hex32,
300 .print = print_hex32,
301 .get = get_int32,
302 .set = set_int32,
303 .min = 0,
304 .max = 0xFFFFFFFFULL,
307 /* --- 64bit integer --- */
309 static void get_int64(Object *obj, Visitor *v, void *opaque,
310 const char *name, Error **errp)
312 DeviceState *dev = DEVICE(obj);
313 Property *prop = opaque;
314 int64_t *ptr = qdev_get_prop_ptr(dev, prop);
316 visit_type_int(v, ptr, name, errp);
319 static void set_int64(Object *obj, Visitor *v, void *opaque,
320 const char *name, Error **errp)
322 DeviceState *dev = DEVICE(obj);
323 Property *prop = opaque;
324 int64_t *ptr = qdev_get_prop_ptr(dev, prop);
326 if (dev->state != DEV_STATE_CREATED) {
327 error_set(errp, QERR_PERMISSION_DENIED);
328 return;
331 visit_type_int(v, ptr, name, errp);
334 PropertyInfo qdev_prop_uint64 = {
335 .name = "uint64",
336 .get = get_int64,
337 .set = set_int64,
340 /* --- 64bit hex value --- */
342 static int parse_hex64(DeviceState *dev, Property *prop, const char *str)
344 uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
345 char *end;
347 if (str[0] != '0' || str[1] != 'x') {
348 return -EINVAL;
351 *ptr = strtoull(str, &end, 16);
352 if ((*end != '\0') || (end == str)) {
353 return -EINVAL;
356 return 0;
359 static int print_hex64(DeviceState *dev, Property *prop, char *dest, size_t len)
361 uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
362 return snprintf(dest, len, "0x%" PRIx64, *ptr);
365 PropertyInfo qdev_prop_hex64 = {
366 .name = "uint64",
367 .legacy_name = "hex64",
368 .parse = parse_hex64,
369 .print = print_hex64,
370 .get = get_int64,
371 .set = set_int64,
374 /* --- string --- */
376 static void release_string(Object *obj, const char *name, void *opaque)
378 Property *prop = opaque;
379 g_free(*(char **)qdev_get_prop_ptr(DEVICE(obj), prop));
382 static int print_string(DeviceState *dev, Property *prop, char *dest, size_t len)
384 char **ptr = qdev_get_prop_ptr(dev, prop);
385 if (!*ptr)
386 return snprintf(dest, len, "<null>");
387 return snprintf(dest, len, "\"%s\"", *ptr);
390 static void get_string(Object *obj, Visitor *v, void *opaque,
391 const char *name, Error **errp)
393 DeviceState *dev = DEVICE(obj);
394 Property *prop = opaque;
395 char **ptr = qdev_get_prop_ptr(dev, prop);
397 if (!*ptr) {
398 char *str = (char *)"";
399 visit_type_str(v, &str, name, errp);
400 } else {
401 visit_type_str(v, ptr, name, errp);
405 static void set_string(Object *obj, Visitor *v, void *opaque,
406 const char *name, Error **errp)
408 DeviceState *dev = DEVICE(obj);
409 Property *prop = opaque;
410 char **ptr = qdev_get_prop_ptr(dev, prop);
411 Error *local_err = NULL;
412 char *str;
414 if (dev->state != DEV_STATE_CREATED) {
415 error_set(errp, QERR_PERMISSION_DENIED);
416 return;
419 visit_type_str(v, &str, name, &local_err);
420 if (local_err) {
421 error_propagate(errp, local_err);
422 return;
424 if (!*str) {
425 g_free(str);
426 str = NULL;
428 if (*ptr) {
429 g_free(*ptr);
431 *ptr = str;
434 PropertyInfo qdev_prop_string = {
435 .name = "string",
436 .print = print_string,
437 .release = release_string,
438 .get = get_string,
439 .set = set_string,
442 /* --- drive --- */
444 static int parse_drive(DeviceState *dev, const char *str, void **ptr)
446 BlockDriverState *bs;
448 bs = bdrv_find(str);
449 if (bs == NULL)
450 return -ENOENT;
451 if (bdrv_attach_dev(bs, dev) < 0)
452 return -EEXIST;
453 *ptr = bs;
454 return 0;
457 static void release_drive(Object *obj, const char *name, void *opaque)
459 DeviceState *dev = DEVICE(obj);
460 Property *prop = opaque;
461 BlockDriverState **ptr = qdev_get_prop_ptr(dev, prop);
463 if (*ptr) {
464 bdrv_detach_dev(*ptr, dev);
465 blockdev_auto_del(*ptr);
469 static const char *print_drive(void *ptr)
471 return bdrv_get_device_name(ptr);
474 static void get_pointer(Object *obj, Visitor *v, Property *prop,
475 const char *(*print)(void *ptr),
476 const char *name, Error **errp)
478 DeviceState *dev = DEVICE(obj);
479 void **ptr = qdev_get_prop_ptr(dev, prop);
480 char *p;
482 p = (char *) (*ptr ? print(*ptr) : "");
483 visit_type_str(v, &p, name, errp);
486 static void set_pointer(Object *obj, Visitor *v, Property *prop,
487 int (*parse)(DeviceState *dev, const char *str, void **ptr),
488 const char *name, Error **errp)
490 DeviceState *dev = DEVICE(obj);
491 Error *local_err = NULL;
492 void **ptr = qdev_get_prop_ptr(dev, prop);
493 char *str;
494 int ret;
496 if (dev->state != DEV_STATE_CREATED) {
497 error_set(errp, QERR_PERMISSION_DENIED);
498 return;
501 visit_type_str(v, &str, name, &local_err);
502 if (local_err) {
503 error_propagate(errp, local_err);
504 return;
506 if (!*str) {
507 g_free(str);
508 *ptr = NULL;
509 return;
511 ret = parse(dev, str, ptr);
512 error_set_from_qdev_prop_error(errp, ret, dev, prop, str);
513 g_free(str);
516 static void get_drive(Object *obj, Visitor *v, void *opaque,
517 const char *name, Error **errp)
519 get_pointer(obj, v, opaque, print_drive, name, errp);
522 static void set_drive(Object *obj, Visitor *v, void *opaque,
523 const char *name, Error **errp)
525 set_pointer(obj, v, opaque, parse_drive, name, errp);
528 PropertyInfo qdev_prop_drive = {
529 .name = "drive",
530 .get = get_drive,
531 .set = set_drive,
532 .release = release_drive,
535 /* --- character device --- */
537 static int parse_chr(DeviceState *dev, const char *str, void **ptr)
539 CharDriverState *chr = qemu_chr_find(str);
540 if (chr == NULL) {
541 return -ENOENT;
543 if (chr->avail_connections < 1) {
544 return -EEXIST;
546 *ptr = chr;
547 --chr->avail_connections;
548 return 0;
551 static void release_chr(Object *obj, const char *name, void *opaque)
553 DeviceState *dev = DEVICE(obj);
554 Property *prop = opaque;
555 CharDriverState **ptr = qdev_get_prop_ptr(dev, prop);
557 if (*ptr) {
558 qemu_chr_add_handlers(*ptr, NULL, NULL, NULL, NULL);
563 static const char *print_chr(void *ptr)
565 CharDriverState *chr = ptr;
567 return chr->label ? chr->label : "";
570 static void get_chr(Object *obj, Visitor *v, void *opaque,
571 const char *name, Error **errp)
573 get_pointer(obj, v, opaque, print_chr, name, errp);
576 static void set_chr(Object *obj, Visitor *v, void *opaque,
577 const char *name, Error **errp)
579 set_pointer(obj, v, opaque, parse_chr, name, errp);
582 PropertyInfo qdev_prop_chr = {
583 .name = "chr",
584 .get = get_chr,
585 .set = set_chr,
586 .release = release_chr,
589 /* --- netdev device --- */
591 static int parse_netdev(DeviceState *dev, const char *str, void **ptr)
593 VLANClientState *netdev = qemu_find_netdev(str);
595 if (netdev == NULL) {
596 return -ENOENT;
598 if (netdev->peer) {
599 return -EEXIST;
601 *ptr = netdev;
602 return 0;
605 static const char *print_netdev(void *ptr)
607 VLANClientState *netdev = ptr;
609 return netdev->name ? netdev->name : "";
612 static void get_netdev(Object *obj, Visitor *v, void *opaque,
613 const char *name, Error **errp)
615 get_pointer(obj, v, opaque, print_netdev, name, errp);
618 static void set_netdev(Object *obj, Visitor *v, void *opaque,
619 const char *name, Error **errp)
621 set_pointer(obj, v, opaque, parse_netdev, name, errp);
624 PropertyInfo qdev_prop_netdev = {
625 .name = "netdev",
626 .get = get_netdev,
627 .set = set_netdev,
630 /* --- vlan --- */
632 static int print_vlan(DeviceState *dev, Property *prop, char *dest, size_t len)
634 VLANState **ptr = qdev_get_prop_ptr(dev, prop);
636 if (*ptr) {
637 return snprintf(dest, len, "%d", (*ptr)->id);
638 } else {
639 return snprintf(dest, len, "<null>");
643 static void get_vlan(Object *obj, Visitor *v, void *opaque,
644 const char *name, Error **errp)
646 DeviceState *dev = DEVICE(obj);
647 Property *prop = opaque;
648 VLANState **ptr = qdev_get_prop_ptr(dev, prop);
649 int64_t id;
651 id = *ptr ? (*ptr)->id : -1;
652 visit_type_int(v, &id, name, errp);
655 static void set_vlan(Object *obj, Visitor *v, void *opaque,
656 const char *name, Error **errp)
658 DeviceState *dev = DEVICE(obj);
659 Property *prop = opaque;
660 VLANState **ptr = qdev_get_prop_ptr(dev, prop);
661 Error *local_err = NULL;
662 int64_t id;
663 VLANState *vlan;
665 if (dev->state != DEV_STATE_CREATED) {
666 error_set(errp, QERR_PERMISSION_DENIED);
667 return;
670 visit_type_int(v, &id, name, &local_err);
671 if (local_err) {
672 error_propagate(errp, local_err);
673 return;
675 if (id == -1) {
676 *ptr = NULL;
677 return;
679 vlan = qemu_find_vlan(id, 1);
680 if (!vlan) {
681 error_set(errp, QERR_INVALID_PARAMETER_VALUE,
682 name, prop->info->name);
683 return;
685 *ptr = vlan;
688 PropertyInfo qdev_prop_vlan = {
689 .name = "vlan",
690 .print = print_vlan,
691 .get = get_vlan,
692 .set = set_vlan,
695 /* --- pointer --- */
697 /* Not a proper property, just for dirty hacks. TODO Remove it! */
698 PropertyInfo qdev_prop_ptr = {
699 .name = "ptr",
702 /* --- mac address --- */
705 * accepted syntax versions:
706 * 01:02:03:04:05:06
707 * 01-02-03-04-05-06
709 static void get_mac(Object *obj, Visitor *v, void *opaque,
710 const char *name, Error **errp)
712 DeviceState *dev = DEVICE(obj);
713 Property *prop = opaque;
714 MACAddr *mac = qdev_get_prop_ptr(dev, prop);
715 char buffer[2 * 6 + 5 + 1];
716 char *p = buffer;
718 snprintf(buffer, sizeof(buffer), "%02x:%02x:%02x:%02x:%02x:%02x",
719 mac->a[0], mac->a[1], mac->a[2],
720 mac->a[3], mac->a[4], mac->a[5]);
722 visit_type_str(v, &p, name, errp);
725 static void set_mac(Object *obj, Visitor *v, void *opaque,
726 const char *name, Error **errp)
728 DeviceState *dev = DEVICE(obj);
729 Property *prop = opaque;
730 MACAddr *mac = qdev_get_prop_ptr(dev, prop);
731 Error *local_err = NULL;
732 int i, pos;
733 char *str, *p;
735 if (dev->state != DEV_STATE_CREATED) {
736 error_set(errp, QERR_PERMISSION_DENIED);
737 return;
740 visit_type_str(v, &str, name, &local_err);
741 if (local_err) {
742 error_propagate(errp, local_err);
743 return;
746 for (i = 0, pos = 0; i < 6; i++, pos += 3) {
747 if (!qemu_isxdigit(str[pos]))
748 goto inval;
749 if (!qemu_isxdigit(str[pos+1]))
750 goto inval;
751 if (i == 5) {
752 if (str[pos+2] != '\0')
753 goto inval;
754 } else {
755 if (str[pos+2] != ':' && str[pos+2] != '-')
756 goto inval;
758 mac->a[i] = strtol(str+pos, &p, 16);
760 return;
762 inval:
763 error_set_from_qdev_prop_error(errp, EINVAL, dev, prop, str);
766 PropertyInfo qdev_prop_macaddr = {
767 .name = "macaddr",
768 .get = get_mac,
769 .set = set_mac,
773 /* --- lost tick policy --- */
775 static const char *lost_tick_policy_table[LOST_TICK_MAX+1] = {
776 [LOST_TICK_DISCARD] = "discard",
777 [LOST_TICK_DELAY] = "delay",
778 [LOST_TICK_MERGE] = "merge",
779 [LOST_TICK_SLEW] = "slew",
780 [LOST_TICK_MAX] = NULL,
783 QEMU_BUILD_BUG_ON(sizeof(LostTickPolicy) != sizeof(int));
785 static void get_enum(Object *obj, Visitor *v, void *opaque,
786 const char *name, Error **errp)
788 DeviceState *dev = DEVICE(obj);
789 Property *prop = opaque;
790 int *ptr = qdev_get_prop_ptr(dev, prop);
792 visit_type_enum(v, ptr, prop->info->enum_table,
793 prop->info->name, prop->name, errp);
796 static void set_enum(Object *obj, Visitor *v, void *opaque,
797 const char *name, Error **errp)
799 DeviceState *dev = DEVICE(obj);
800 Property *prop = opaque;
801 int *ptr = qdev_get_prop_ptr(dev, prop);
803 if (dev->state != DEV_STATE_CREATED) {
804 error_set(errp, QERR_PERMISSION_DENIED);
805 return;
808 visit_type_enum(v, ptr, prop->info->enum_table,
809 prop->info->name, prop->name, errp);
812 PropertyInfo qdev_prop_losttickpolicy = {
813 .name = "LostTickPolicy",
814 .enum_table = lost_tick_policy_table,
815 .get = get_enum,
816 .set = set_enum,
819 /* --- pci address --- */
822 * bus-local address, i.e. "$slot" or "$slot.$fn"
824 static void set_pci_devfn(Object *obj, Visitor *v, void *opaque,
825 const char *name, Error **errp)
827 DeviceState *dev = DEVICE(obj);
828 Property *prop = opaque;
829 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
830 unsigned int slot, fn, n;
831 Error *local_err = NULL;
832 char *str = (char *)"";
834 if (dev->state != DEV_STATE_CREATED) {
835 error_set(errp, QERR_PERMISSION_DENIED);
836 return;
839 visit_type_str(v, &str, name, &local_err);
840 if (local_err) {
841 return set_int32(obj, v, opaque, name, errp);
844 if (sscanf(str, "%x.%x%n", &slot, &fn, &n) != 2) {
845 fn = 0;
846 if (sscanf(str, "%x%n", &slot, &n) != 1) {
847 goto invalid;
850 if (str[n] != '\0' || fn > 7 || slot > 31) {
851 goto invalid;
853 *ptr = slot << 3 | fn;
854 return;
856 invalid:
857 error_set_from_qdev_prop_error(errp, EINVAL, dev, prop, str);
860 static int print_pci_devfn(DeviceState *dev, Property *prop, char *dest, size_t len)
862 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
864 if (*ptr == -1) {
865 return snprintf(dest, len, "<unset>");
866 } else {
867 return snprintf(dest, len, "%02x.%x", *ptr >> 3, *ptr & 7);
871 PropertyInfo qdev_prop_pci_devfn = {
872 .name = "int32",
873 .legacy_name = "pci-devfn",
874 .print = print_pci_devfn,
875 .get = get_int32,
876 .set = set_pci_devfn,
877 /* FIXME: this should be -1...255, but the address is stored
878 * into an uint32_t rather than int32_t.
880 .min = 0,
881 .max = 0xFFFFFFFFULL,
884 /* --- public helpers --- */
886 static Property *qdev_prop_walk(Property *props, const char *name)
888 if (!props)
889 return NULL;
890 while (props->name) {
891 if (strcmp(props->name, name) == 0)
892 return props;
893 props++;
895 return NULL;
898 static Property *qdev_prop_find(DeviceState *dev, const char *name)
900 Property *prop;
902 /* device properties */
903 prop = qdev_prop_walk(qdev_get_props(dev), name);
904 if (prop)
905 return prop;
907 /* bus properties */
908 prop = qdev_prop_walk(dev->parent_bus->info->props, name);
909 if (prop)
910 return prop;
912 return NULL;
915 int qdev_prop_exists(DeviceState *dev, const char *name)
917 return qdev_prop_find(dev, name) ? true : false;
920 void error_set_from_qdev_prop_error(Error **errp, int ret, DeviceState *dev,
921 Property *prop, const char *value)
923 switch (ret) {
924 case -EEXIST:
925 error_set(errp, QERR_PROPERTY_VALUE_IN_USE,
926 object_get_typename(OBJECT(dev)), prop->name, value);
927 break;
928 default:
929 case -EINVAL:
930 error_set(errp, QERR_PROPERTY_VALUE_BAD,
931 object_get_typename(OBJECT(dev)), prop->name, value);
932 break;
933 case -ENOENT:
934 error_set(errp, QERR_PROPERTY_VALUE_NOT_FOUND,
935 object_get_typename(OBJECT(dev)), prop->name, value);
936 break;
937 case 0:
938 break;
942 int qdev_prop_parse(DeviceState *dev, const char *name, const char *value)
944 char *legacy_name;
945 Error *err = NULL;
947 legacy_name = g_strdup_printf("legacy-%s", name);
948 if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
949 object_property_parse(OBJECT(dev), value, legacy_name, &err);
950 } else {
951 object_property_parse(OBJECT(dev), value, name, &err);
953 g_free(legacy_name);
955 if (err) {
956 qerror_report_err(err);
957 error_free(err);
958 return -1;
960 return 0;
963 void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value)
965 Error *errp = NULL;
966 object_property_set_bool(OBJECT(dev), value, name, &errp);
967 assert_no_error(errp);
970 void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value)
972 Error *errp = NULL;
973 object_property_set_int(OBJECT(dev), value, name, &errp);
974 assert_no_error(errp);
977 void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value)
979 Error *errp = NULL;
980 object_property_set_int(OBJECT(dev), value, name, &errp);
981 assert_no_error(errp);
984 void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value)
986 Error *errp = NULL;
987 object_property_set_int(OBJECT(dev), value, name, &errp);
988 assert_no_error(errp);
991 void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value)
993 Error *errp = NULL;
994 object_property_set_int(OBJECT(dev), value, name, &errp);
995 assert_no_error(errp);
998 void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value)
1000 Error *errp = NULL;
1001 object_property_set_int(OBJECT(dev), value, name, &errp);
1002 assert_no_error(errp);
1005 void qdev_prop_set_string(DeviceState *dev, const char *name, char *value)
1007 Error *errp = NULL;
1008 object_property_set_str(OBJECT(dev), value, name, &errp);
1009 assert_no_error(errp);
1012 int qdev_prop_set_drive(DeviceState *dev, const char *name, BlockDriverState *value)
1014 Error *errp = NULL;
1015 const char *bdrv_name = value ? bdrv_get_device_name(value) : "";
1016 object_property_set_str(OBJECT(dev), bdrv_name,
1017 name, &errp);
1018 if (errp) {
1019 qerror_report_err(errp);
1020 error_free(errp);
1021 return -1;
1023 return 0;
1026 void qdev_prop_set_drive_nofail(DeviceState *dev, const char *name, BlockDriverState *value)
1028 if (qdev_prop_set_drive(dev, name, value) < 0) {
1029 exit(1);
1032 void qdev_prop_set_chr(DeviceState *dev, const char *name, CharDriverState *value)
1034 Error *errp = NULL;
1035 assert(!value || value->label);
1036 object_property_set_str(OBJECT(dev),
1037 value ? value->label : "", name, &errp);
1038 assert_no_error(errp);
1041 void qdev_prop_set_netdev(DeviceState *dev, const char *name, VLANClientState *value)
1043 Error *errp = NULL;
1044 assert(!value || value->name);
1045 object_property_set_str(OBJECT(dev),
1046 value ? value->name : "", name, &errp);
1047 assert_no_error(errp);
1050 void qdev_prop_set_vlan(DeviceState *dev, const char *name, VLANState *value)
1052 Error *errp = NULL;
1053 object_property_set_int(OBJECT(dev), value ? value->id : -1, name, &errp);
1054 assert_no_error(errp);
1057 void qdev_prop_set_macaddr(DeviceState *dev, const char *name, uint8_t *value)
1059 Error *errp = NULL;
1060 char str[2 * 6 + 5 + 1];
1061 snprintf(str, sizeof(str), "%02x:%02x:%02x:%02x:%02x:%02x",
1062 value[0], value[1], value[2], value[3], value[4], value[5]);
1064 object_property_set_str(OBJECT(dev), str, name, &errp);
1065 assert_no_error(errp);
1068 void qdev_prop_set_enum(DeviceState *dev, const char *name, int value)
1070 Property *prop;
1071 Error *errp = NULL;
1073 prop = qdev_prop_find(dev, name);
1074 object_property_set_str(OBJECT(dev), prop->info->enum_table[value],
1075 name, &errp);
1076 assert_no_error(errp);
1079 void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value)
1081 Property *prop;
1082 void **ptr;
1084 prop = qdev_prop_find(dev, name);
1085 assert(prop && prop->info == &qdev_prop_ptr);
1086 ptr = qdev_get_prop_ptr(dev, prop);
1087 *ptr = value;
1090 void qdev_prop_set_defaults(DeviceState *dev, Property *props)
1092 Object *obj = OBJECT(dev);
1093 if (!props)
1094 return;
1095 for (; props->name; props++) {
1096 Error *errp = NULL;
1097 if (props->qtype == QTYPE_NONE) {
1098 continue;
1100 if (props->qtype == QTYPE_QBOOL) {
1101 object_property_set_bool(obj, props->defval, props->name, &errp);
1102 } else if (props->info->enum_table) {
1103 object_property_set_str(obj, props->info->enum_table[props->defval],
1104 props->name, &errp);
1105 } else if (props->qtype == QTYPE_QINT) {
1106 object_property_set_int(obj, props->defval, props->name, &errp);
1108 assert_no_error(errp);
1112 static QTAILQ_HEAD(, GlobalProperty) global_props = QTAILQ_HEAD_INITIALIZER(global_props);
1114 static void qdev_prop_register_global(GlobalProperty *prop)
1116 QTAILQ_INSERT_TAIL(&global_props, prop, next);
1119 void qdev_prop_register_global_list(GlobalProperty *props)
1121 int i;
1123 for (i = 0; props[i].driver != NULL; i++) {
1124 qdev_prop_register_global(props+i);
1128 void qdev_prop_set_globals(DeviceState *dev)
1130 GlobalProperty *prop;
1132 QTAILQ_FOREACH(prop, &global_props, next) {
1133 if (strcmp(object_get_typename(OBJECT(dev)), prop->driver) != 0 &&
1134 strcmp(qdev_get_bus_info(dev)->name, prop->driver) != 0) {
1135 continue;
1137 if (qdev_prop_parse(dev, prop->property, prop->value) != 0) {
1138 exit(1);
1143 static int qdev_add_one_global(QemuOpts *opts, void *opaque)
1145 GlobalProperty *g;
1147 g = g_malloc0(sizeof(*g));
1148 g->driver = qemu_opt_get(opts, "driver");
1149 g->property = qemu_opt_get(opts, "property");
1150 g->value = qemu_opt_get(opts, "value");
1151 qdev_prop_register_global(g);
1152 return 0;
1155 void qemu_add_globals(void)
1157 qemu_opts_foreach(qemu_find_opts("global"), qdev_add_one_global, NULL, 0);