target-ppc: Use NARROW_MODE macro for comparisons
[qemu/agraf.git] / hw / qdev-properties.c
blob247ca6c5b481a1e6fe2143e9a6c02466cb28a4c6
1 #include "net/net.h"
2 #include "hw/qdev.h"
3 #include "qapi/qmp/qerror.h"
4 #include "sysemu/blockdev.h"
5 #include "hw/block-common.h"
6 #include "net/hub.h"
7 #include "qapi/visitor.h"
8 #include "char/char.h"
10 void *qdev_get_prop_ptr(DeviceState *dev, Property *prop)
12 void *ptr = dev;
13 ptr += prop->offset;
14 return ptr;
17 static void get_enum(Object *obj, Visitor *v, void *opaque,
18 const char *name, Error **errp)
20 DeviceState *dev = DEVICE(obj);
21 Property *prop = opaque;
22 int *ptr = qdev_get_prop_ptr(dev, prop);
24 visit_type_enum(v, ptr, prop->info->enum_table,
25 prop->info->name, prop->name, errp);
28 static void set_enum(Object *obj, Visitor *v, void *opaque,
29 const char *name, Error **errp)
31 DeviceState *dev = DEVICE(obj);
32 Property *prop = opaque;
33 int *ptr = qdev_get_prop_ptr(dev, prop);
35 if (dev->realized) {
36 error_set(errp, QERR_PERMISSION_DENIED);
37 return;
40 visit_type_enum(v, ptr, prop->info->enum_table,
41 prop->info->name, prop->name, errp);
44 /* Bit */
46 static uint32_t qdev_get_prop_mask(Property *prop)
48 assert(prop->info == &qdev_prop_bit);
49 return 0x1 << prop->bitnr;
52 static void bit_prop_set(DeviceState *dev, Property *props, bool val)
54 uint32_t *p = qdev_get_prop_ptr(dev, props);
55 uint32_t mask = qdev_get_prop_mask(props);
56 if (val) {
57 *p |= mask;
58 } else {
59 *p &= ~mask;
63 static int print_bit(DeviceState *dev, Property *prop, char *dest, size_t len)
65 uint32_t *p = qdev_get_prop_ptr(dev, prop);
66 return snprintf(dest, len, (*p & qdev_get_prop_mask(prop)) ? "on" : "off");
69 static void get_bit(Object *obj, Visitor *v, void *opaque,
70 const char *name, Error **errp)
72 DeviceState *dev = DEVICE(obj);
73 Property *prop = opaque;
74 uint32_t *p = qdev_get_prop_ptr(dev, prop);
75 bool value = (*p & qdev_get_prop_mask(prop)) != 0;
77 visit_type_bool(v, &value, name, errp);
80 static void set_bit(Object *obj, Visitor *v, void *opaque,
81 const char *name, Error **errp)
83 DeviceState *dev = DEVICE(obj);
84 Property *prop = opaque;
85 Error *local_err = NULL;
86 bool value;
88 if (dev->realized) {
89 error_set(errp, QERR_PERMISSION_DENIED);
90 return;
93 visit_type_bool(v, &value, name, &local_err);
94 if (local_err) {
95 error_propagate(errp, local_err);
96 return;
98 bit_prop_set(dev, prop, value);
101 PropertyInfo qdev_prop_bit = {
102 .name = "boolean",
103 .legacy_name = "on/off",
104 .print = print_bit,
105 .get = get_bit,
106 .set = set_bit,
109 /* --- 8bit integer --- */
111 static void get_uint8(Object *obj, Visitor *v, void *opaque,
112 const char *name, Error **errp)
114 DeviceState *dev = DEVICE(obj);
115 Property *prop = opaque;
116 uint8_t *ptr = qdev_get_prop_ptr(dev, prop);
118 visit_type_uint8(v, ptr, name, errp);
121 static void set_uint8(Object *obj, Visitor *v, void *opaque,
122 const char *name, Error **errp)
124 DeviceState *dev = DEVICE(obj);
125 Property *prop = opaque;
126 uint8_t *ptr = qdev_get_prop_ptr(dev, prop);
128 if (dev->realized) {
129 error_set(errp, QERR_PERMISSION_DENIED);
130 return;
133 visit_type_uint8(v, ptr, name, errp);
136 PropertyInfo qdev_prop_uint8 = {
137 .name = "uint8",
138 .get = get_uint8,
139 .set = set_uint8,
142 /* --- 8bit hex value --- */
144 static int parse_hex8(DeviceState *dev, Property *prop, const char *str)
146 uint8_t *ptr = qdev_get_prop_ptr(dev, prop);
147 char *end;
149 if (str[0] != '0' || str[1] != 'x') {
150 return -EINVAL;
153 *ptr = strtoul(str, &end, 16);
154 if ((*end != '\0') || (end == str)) {
155 return -EINVAL;
158 return 0;
161 static int print_hex8(DeviceState *dev, Property *prop, char *dest, size_t len)
163 uint8_t *ptr = qdev_get_prop_ptr(dev, prop);
164 return snprintf(dest, len, "0x%" PRIx8, *ptr);
167 PropertyInfo qdev_prop_hex8 = {
168 .name = "uint8",
169 .legacy_name = "hex8",
170 .parse = parse_hex8,
171 .print = print_hex8,
172 .get = get_uint8,
173 .set = set_uint8,
176 /* --- 16bit integer --- */
178 static void get_uint16(Object *obj, Visitor *v, void *opaque,
179 const char *name, Error **errp)
181 DeviceState *dev = DEVICE(obj);
182 Property *prop = opaque;
183 uint16_t *ptr = qdev_get_prop_ptr(dev, prop);
185 visit_type_uint16(v, ptr, name, errp);
188 static void set_uint16(Object *obj, Visitor *v, void *opaque,
189 const char *name, Error **errp)
191 DeviceState *dev = DEVICE(obj);
192 Property *prop = opaque;
193 uint16_t *ptr = qdev_get_prop_ptr(dev, prop);
195 if (dev->realized) {
196 error_set(errp, QERR_PERMISSION_DENIED);
197 return;
200 visit_type_uint16(v, ptr, name, errp);
203 PropertyInfo qdev_prop_uint16 = {
204 .name = "uint16",
205 .get = get_uint16,
206 .set = set_uint16,
209 /* --- 32bit integer --- */
211 static void get_uint32(Object *obj, Visitor *v, void *opaque,
212 const char *name, Error **errp)
214 DeviceState *dev = DEVICE(obj);
215 Property *prop = opaque;
216 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
218 visit_type_uint32(v, ptr, name, errp);
221 static void set_uint32(Object *obj, Visitor *v, void *opaque,
222 const char *name, Error **errp)
224 DeviceState *dev = DEVICE(obj);
225 Property *prop = opaque;
226 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
228 if (dev->realized) {
229 error_set(errp, QERR_PERMISSION_DENIED);
230 return;
233 visit_type_uint32(v, ptr, name, errp);
236 static void get_int32(Object *obj, Visitor *v, void *opaque,
237 const char *name, Error **errp)
239 DeviceState *dev = DEVICE(obj);
240 Property *prop = opaque;
241 int32_t *ptr = qdev_get_prop_ptr(dev, prop);
243 visit_type_int32(v, ptr, name, errp);
246 static void set_int32(Object *obj, Visitor *v, void *opaque,
247 const char *name, Error **errp)
249 DeviceState *dev = DEVICE(obj);
250 Property *prop = opaque;
251 int32_t *ptr = qdev_get_prop_ptr(dev, prop);
253 if (dev->realized) {
254 error_set(errp, QERR_PERMISSION_DENIED);
255 return;
258 visit_type_int32(v, ptr, name, errp);
261 PropertyInfo qdev_prop_uint32 = {
262 .name = "uint32",
263 .get = get_uint32,
264 .set = set_uint32,
267 PropertyInfo qdev_prop_int32 = {
268 .name = "int32",
269 .get = get_int32,
270 .set = set_int32,
273 /* --- 32bit hex value --- */
275 static int parse_hex32(DeviceState *dev, Property *prop, const char *str)
277 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
278 char *end;
280 if (str[0] != '0' || str[1] != 'x') {
281 return -EINVAL;
284 *ptr = strtoul(str, &end, 16);
285 if ((*end != '\0') || (end == str)) {
286 return -EINVAL;
289 return 0;
292 static int print_hex32(DeviceState *dev, Property *prop, char *dest, size_t len)
294 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
295 return snprintf(dest, len, "0x%" PRIx32, *ptr);
298 PropertyInfo qdev_prop_hex32 = {
299 .name = "uint32",
300 .legacy_name = "hex32",
301 .parse = parse_hex32,
302 .print = print_hex32,
303 .get = get_uint32,
304 .set = set_uint32,
307 /* --- 64bit integer --- */
309 static void get_uint64(Object *obj, Visitor *v, void *opaque,
310 const char *name, Error **errp)
312 DeviceState *dev = DEVICE(obj);
313 Property *prop = opaque;
314 uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
316 visit_type_uint64(v, ptr, name, errp);
319 static void set_uint64(Object *obj, Visitor *v, void *opaque,
320 const char *name, Error **errp)
322 DeviceState *dev = DEVICE(obj);
323 Property *prop = opaque;
324 uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
326 if (dev->realized) {
327 error_set(errp, QERR_PERMISSION_DENIED);
328 return;
331 visit_type_uint64(v, ptr, name, errp);
334 PropertyInfo qdev_prop_uint64 = {
335 .name = "uint64",
336 .get = get_uint64,
337 .set = set_uint64,
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_uint64,
371 .set = set_uint64,
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,
383 size_t len)
385 char **ptr = qdev_get_prop_ptr(dev, prop);
386 if (!*ptr) {
387 return snprintf(dest, len, "<null>");
389 return snprintf(dest, len, "\"%s\"", *ptr);
392 static void get_string(Object *obj, Visitor *v, void *opaque,
393 const char *name, Error **errp)
395 DeviceState *dev = DEVICE(obj);
396 Property *prop = opaque;
397 char **ptr = qdev_get_prop_ptr(dev, prop);
399 if (!*ptr) {
400 char *str = (char *)"";
401 visit_type_str(v, &str, name, errp);
402 } else {
403 visit_type_str(v, ptr, name, errp);
407 static void set_string(Object *obj, Visitor *v, void *opaque,
408 const char *name, Error **errp)
410 DeviceState *dev = DEVICE(obj);
411 Property *prop = opaque;
412 char **ptr = qdev_get_prop_ptr(dev, prop);
413 Error *local_err = NULL;
414 char *str;
416 if (dev->realized) {
417 error_set(errp, QERR_PERMISSION_DENIED);
418 return;
421 visit_type_str(v, &str, name, &local_err);
422 if (local_err) {
423 error_propagate(errp, local_err);
424 return;
426 if (*ptr) {
427 g_free(*ptr);
429 *ptr = str;
432 PropertyInfo qdev_prop_string = {
433 .name = "string",
434 .print = print_string,
435 .release = release_string,
436 .get = get_string,
437 .set = set_string,
440 /* --- pointer --- */
442 /* Not a proper property, just for dirty hacks. TODO Remove it! */
443 PropertyInfo qdev_prop_ptr = {
444 .name = "ptr",
447 /* --- mac address --- */
450 * accepted syntax versions:
451 * 01:02:03:04:05:06
452 * 01-02-03-04-05-06
454 static void get_mac(Object *obj, Visitor *v, void *opaque,
455 const char *name, Error **errp)
457 DeviceState *dev = DEVICE(obj);
458 Property *prop = opaque;
459 MACAddr *mac = qdev_get_prop_ptr(dev, prop);
460 char buffer[2 * 6 + 5 + 1];
461 char *p = buffer;
463 snprintf(buffer, sizeof(buffer), "%02x:%02x:%02x:%02x:%02x:%02x",
464 mac->a[0], mac->a[1], mac->a[2],
465 mac->a[3], mac->a[4], mac->a[5]);
467 visit_type_str(v, &p, name, errp);
470 static void set_mac(Object *obj, Visitor *v, void *opaque,
471 const char *name, Error **errp)
473 DeviceState *dev = DEVICE(obj);
474 Property *prop = opaque;
475 MACAddr *mac = qdev_get_prop_ptr(dev, prop);
476 Error *local_err = NULL;
477 int i, pos;
478 char *str, *p;
480 if (dev->realized) {
481 error_set(errp, QERR_PERMISSION_DENIED);
482 return;
485 visit_type_str(v, &str, name, &local_err);
486 if (local_err) {
487 error_propagate(errp, local_err);
488 return;
491 for (i = 0, pos = 0; i < 6; i++, pos += 3) {
492 if (!qemu_isxdigit(str[pos])) {
493 goto inval;
495 if (!qemu_isxdigit(str[pos+1])) {
496 goto inval;
498 if (i == 5) {
499 if (str[pos+2] != '\0') {
500 goto inval;
502 } else {
503 if (str[pos+2] != ':' && str[pos+2] != '-') {
504 goto inval;
507 mac->a[i] = strtol(str+pos, &p, 16);
509 g_free(str);
510 return;
512 inval:
513 error_set_from_qdev_prop_error(errp, EINVAL, dev, prop, str);
514 g_free(str);
517 PropertyInfo qdev_prop_macaddr = {
518 .name = "macaddr",
519 .get = get_mac,
520 .set = set_mac,
523 /* --- lost tick policy --- */
525 static const char *lost_tick_policy_table[LOST_TICK_MAX+1] = {
526 [LOST_TICK_DISCARD] = "discard",
527 [LOST_TICK_DELAY] = "delay",
528 [LOST_TICK_MERGE] = "merge",
529 [LOST_TICK_SLEW] = "slew",
530 [LOST_TICK_MAX] = NULL,
533 QEMU_BUILD_BUG_ON(sizeof(LostTickPolicy) != sizeof(int));
535 PropertyInfo qdev_prop_losttickpolicy = {
536 .name = "LostTickPolicy",
537 .enum_table = lost_tick_policy_table,
538 .get = get_enum,
539 .set = set_enum,
542 /* --- BIOS CHS translation */
544 static const char *bios_chs_trans_table[] = {
545 [BIOS_ATA_TRANSLATION_AUTO] = "auto",
546 [BIOS_ATA_TRANSLATION_NONE] = "none",
547 [BIOS_ATA_TRANSLATION_LBA] = "lba",
550 PropertyInfo qdev_prop_bios_chs_trans = {
551 .name = "bios-chs-trans",
552 .enum_table = bios_chs_trans_table,
553 .get = get_enum,
554 .set = set_enum,
557 /* --- pci address --- */
560 * bus-local address, i.e. "$slot" or "$slot.$fn"
562 static void set_pci_devfn(Object *obj, Visitor *v, void *opaque,
563 const char *name, Error **errp)
565 DeviceState *dev = DEVICE(obj);
566 Property *prop = opaque;
567 int32_t value, *ptr = qdev_get_prop_ptr(dev, prop);
568 unsigned int slot, fn, n;
569 Error *local_err = NULL;
570 char *str;
572 if (dev->realized) {
573 error_set(errp, QERR_PERMISSION_DENIED);
574 return;
577 visit_type_str(v, &str, name, &local_err);
578 if (local_err) {
579 error_free(local_err);
580 local_err = NULL;
581 visit_type_int32(v, &value, name, &local_err);
582 if (local_err) {
583 error_propagate(errp, local_err);
584 } else if (value < -1 || value > 255) {
585 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
586 "pci_devfn");
587 } else {
588 *ptr = value;
590 return;
593 if (sscanf(str, "%x.%x%n", &slot, &fn, &n) != 2) {
594 fn = 0;
595 if (sscanf(str, "%x%n", &slot, &n) != 1) {
596 goto invalid;
599 if (str[n] != '\0' || fn > 7 || slot > 31) {
600 goto invalid;
602 *ptr = slot << 3 | fn;
603 g_free(str);
604 return;
606 invalid:
607 error_set_from_qdev_prop_error(errp, EINVAL, dev, prop, str);
608 g_free(str);
611 static int print_pci_devfn(DeviceState *dev, Property *prop, char *dest,
612 size_t len)
614 int32_t *ptr = qdev_get_prop_ptr(dev, prop);
616 if (*ptr == -1) {
617 return snprintf(dest, len, "<unset>");
618 } else {
619 return snprintf(dest, len, "%02x.%x", *ptr >> 3, *ptr & 7);
623 PropertyInfo qdev_prop_pci_devfn = {
624 .name = "int32",
625 .legacy_name = "pci-devfn",
626 .print = print_pci_devfn,
627 .get = get_int32,
628 .set = set_pci_devfn,
631 /* --- blocksize --- */
633 static void set_blocksize(Object *obj, Visitor *v, void *opaque,
634 const char *name, Error **errp)
636 DeviceState *dev = DEVICE(obj);
637 Property *prop = opaque;
638 uint16_t value, *ptr = qdev_get_prop_ptr(dev, prop);
639 Error *local_err = NULL;
640 const int64_t min = 512;
641 const int64_t max = 32768;
643 if (dev->realized) {
644 error_set(errp, QERR_PERMISSION_DENIED);
645 return;
648 visit_type_uint16(v, &value, name, &local_err);
649 if (local_err) {
650 error_propagate(errp, local_err);
651 return;
653 if (value < min || value > max) {
654 error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE,
655 dev->id?:"", name, (int64_t)value, min, max);
656 return;
659 /* We rely on power-of-2 blocksizes for bitmasks */
660 if ((value & (value - 1)) != 0) {
661 error_set(errp, QERR_PROPERTY_VALUE_NOT_POWER_OF_2,
662 dev->id?:"", name, (int64_t)value);
663 return;
666 *ptr = value;
669 PropertyInfo qdev_prop_blocksize = {
670 .name = "blocksize",
671 .get = get_uint16,
672 .set = set_blocksize,
675 /* --- pci host address --- */
677 static void get_pci_host_devaddr(Object *obj, Visitor *v, void *opaque,
678 const char *name, Error **errp)
680 DeviceState *dev = DEVICE(obj);
681 Property *prop = opaque;
682 PCIHostDeviceAddress *addr = qdev_get_prop_ptr(dev, prop);
683 char buffer[] = "xxxx:xx:xx.x";
684 char *p = buffer;
685 int rc = 0;
687 rc = snprintf(buffer, sizeof(buffer), "%04x:%02x:%02x.%d",
688 addr->domain, addr->bus, addr->slot, addr->function);
689 assert(rc == sizeof(buffer) - 1);
691 visit_type_str(v, &p, name, errp);
695 * Parse [<domain>:]<bus>:<slot>.<func>
696 * if <domain> is not supplied, it's assumed to be 0.
698 static void set_pci_host_devaddr(Object *obj, Visitor *v, void *opaque,
699 const char *name, Error **errp)
701 DeviceState *dev = DEVICE(obj);
702 Property *prop = opaque;
703 PCIHostDeviceAddress *addr = qdev_get_prop_ptr(dev, prop);
704 Error *local_err = NULL;
705 char *str, *p;
706 char *e;
707 unsigned long val;
708 unsigned long dom = 0, bus = 0;
709 unsigned int slot = 0, func = 0;
711 if (dev->realized) {
712 error_set(errp, QERR_PERMISSION_DENIED);
713 return;
716 visit_type_str(v, &str, name, &local_err);
717 if (local_err) {
718 error_propagate(errp, local_err);
719 return;
722 p = str;
723 val = strtoul(p, &e, 16);
724 if (e == p || *e != ':') {
725 goto inval;
727 bus = val;
729 p = e + 1;
730 val = strtoul(p, &e, 16);
731 if (e == p) {
732 goto inval;
734 if (*e == ':') {
735 dom = bus;
736 bus = val;
737 p = e + 1;
738 val = strtoul(p, &e, 16);
739 if (e == p) {
740 goto inval;
743 slot = val;
745 if (*e != '.') {
746 goto inval;
748 p = e + 1;
749 val = strtoul(p, &e, 10);
750 if (e == p) {
751 goto inval;
753 func = val;
755 if (dom > 0xffff || bus > 0xff || slot > 0x1f || func > 7) {
756 goto inval;
759 if (*e) {
760 goto inval;
763 addr->domain = dom;
764 addr->bus = bus;
765 addr->slot = slot;
766 addr->function = func;
768 g_free(str);
769 return;
771 inval:
772 error_set_from_qdev_prop_error(errp, EINVAL, dev, prop, str);
773 g_free(str);
776 PropertyInfo qdev_prop_pci_host_devaddr = {
777 .name = "pci-host-devaddr",
778 .get = get_pci_host_devaddr,
779 .set = set_pci_host_devaddr,
782 /* --- support for array properties --- */
784 /* Used as an opaque for the object properties we add for each
785 * array element. Note that the struct Property must be first
786 * in the struct so that a pointer to this works as the opaque
787 * for the underlying element's property hooks as well as for
788 * our own release callback.
790 typedef struct {
791 struct Property prop;
792 char *propname;
793 ObjectPropertyRelease *release;
794 } ArrayElementProperty;
796 /* object property release callback for array element properties:
797 * we call the underlying element's property release hook, and
798 * then free the memory we allocated when we added the property.
800 static void array_element_release(Object *obj, const char *name, void *opaque)
802 ArrayElementProperty *p = opaque;
803 if (p->release) {
804 p->release(obj, name, opaque);
806 g_free(p->propname);
807 g_free(p);
810 static void set_prop_arraylen(Object *obj, Visitor *v, void *opaque,
811 const char *name, Error **errp)
813 /* Setter for the property which defines the length of a
814 * variable-sized property array. As well as actually setting the
815 * array-length field in the device struct, we have to create the
816 * array itself and dynamically add the corresponding properties.
818 DeviceState *dev = DEVICE(obj);
819 Property *prop = opaque;
820 uint32_t *alenptr = qdev_get_prop_ptr(dev, prop);
821 void **arrayptr = (void *)dev + prop->arrayoffset;
822 void *eltptr;
823 const char *arrayname;
824 int i;
826 if (dev->realized) {
827 error_set(errp, QERR_PERMISSION_DENIED);
828 return;
830 if (*alenptr) {
831 error_setg(errp, "array size property %s may not be set more than once",
832 name);
833 return;
835 visit_type_uint32(v, alenptr, name, errp);
836 if (error_is_set(errp)) {
837 return;
839 if (!*alenptr) {
840 return;
843 /* DEFINE_PROP_ARRAY guarantees that name should start with this prefix;
844 * strip it off so we can get the name of the array itself.
846 assert(strncmp(name, PROP_ARRAY_LEN_PREFIX,
847 strlen(PROP_ARRAY_LEN_PREFIX)) == 0);
848 arrayname = name + strlen(PROP_ARRAY_LEN_PREFIX);
850 /* Note that it is the responsibility of the individual device's deinit
851 * to free the array proper.
853 *arrayptr = eltptr = g_malloc0(*alenptr * prop->arrayfieldsize);
854 for (i = 0; i < *alenptr; i++, eltptr += prop->arrayfieldsize) {
855 char *propname = g_strdup_printf("%s[%d]", arrayname, i);
856 ArrayElementProperty *arrayprop = g_new0(ArrayElementProperty, 1);
857 arrayprop->release = prop->arrayinfo->release;
858 arrayprop->propname = propname;
859 arrayprop->prop.info = prop->arrayinfo;
860 arrayprop->prop.name = propname;
861 /* This ugly piece of pointer arithmetic sets up the offset so
862 * that when the underlying get/set hooks call qdev_get_prop_ptr
863 * they get the right answer despite the array element not actually
864 * being inside the device struct.
866 arrayprop->prop.offset = eltptr - (void *)dev;
867 assert(qdev_get_prop_ptr(dev, &arrayprop->prop) == eltptr);
868 object_property_add(obj, propname,
869 arrayprop->prop.info->name,
870 arrayprop->prop.info->get,
871 arrayprop->prop.info->set,
872 array_element_release,
873 arrayprop, errp);
874 if (error_is_set(errp)) {
875 return;
880 PropertyInfo qdev_prop_arraylen = {
881 .name = "uint32",
882 .get = get_uint32,
883 .set = set_prop_arraylen,
886 /* --- public helpers --- */
888 static Property *qdev_prop_walk(Property *props, const char *name)
890 if (!props) {
891 return NULL;
893 while (props->name) {
894 if (strcmp(props->name, name) == 0) {
895 return props;
897 props++;
899 return NULL;
902 static Property *qdev_prop_find(DeviceState *dev, const char *name)
904 ObjectClass *class;
905 Property *prop;
907 /* device properties */
908 class = object_get_class(OBJECT(dev));
909 do {
910 prop = qdev_prop_walk(DEVICE_CLASS(class)->props, name);
911 if (prop) {
912 return prop;
914 class = object_class_get_parent(class);
915 } while (class != object_class_by_name(TYPE_DEVICE));
917 return NULL;
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, const char *value)
1007 Error *errp = NULL;
1008 object_property_set_str(OBJECT(dev), value, name, &errp);
1009 assert_no_error(errp);
1012 void qdev_prop_set_macaddr(DeviceState *dev, const char *name, uint8_t *value)
1014 Error *errp = NULL;
1015 char str[2 * 6 + 5 + 1];
1016 snprintf(str, sizeof(str), "%02x:%02x:%02x:%02x:%02x:%02x",
1017 value[0], value[1], value[2], value[3], value[4], value[5]);
1019 object_property_set_str(OBJECT(dev), str, name, &errp);
1020 assert_no_error(errp);
1023 void qdev_prop_set_enum(DeviceState *dev, const char *name, int value)
1025 Property *prop;
1026 Error *errp = NULL;
1028 prop = qdev_prop_find(dev, name);
1029 object_property_set_str(OBJECT(dev), prop->info->enum_table[value],
1030 name, &errp);
1031 assert_no_error(errp);
1034 void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value)
1036 Property *prop;
1037 void **ptr;
1039 prop = qdev_prop_find(dev, name);
1040 assert(prop && prop->info == &qdev_prop_ptr);
1041 ptr = qdev_get_prop_ptr(dev, prop);
1042 *ptr = value;
1045 static QTAILQ_HEAD(, GlobalProperty) global_props =
1046 QTAILQ_HEAD_INITIALIZER(global_props);
1048 void qdev_prop_register_global(GlobalProperty *prop)
1050 QTAILQ_INSERT_TAIL(&global_props, prop, next);
1053 void qdev_prop_register_global_list(GlobalProperty *props)
1055 int i;
1057 for (i = 0; props[i].driver != NULL; i++) {
1058 qdev_prop_register_global(props+i);
1062 void qdev_prop_set_globals(DeviceState *dev)
1064 ObjectClass *class = object_get_class(OBJECT(dev));
1066 do {
1067 GlobalProperty *prop;
1068 QTAILQ_FOREACH(prop, &global_props, next) {
1069 if (strcmp(object_class_get_name(class), prop->driver) != 0) {
1070 continue;
1072 if (qdev_prop_parse(dev, prop->property, prop->value) != 0) {
1073 exit(1);
1076 class = object_class_get_parent(class);
1077 } while (class);