MAINTAINERS: Add PCI-PCI bridge to New World Mac machine
[qemu/opensuse.git] / hw / qdev-properties.c
blob02f0dae0bccaf351aa59366a096c3590059c900e
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 static void get_bit(DeviceState *dev, Visitor *v, void *opaque,
59 const char *name, Error **errp)
61 Property *prop = opaque;
62 uint32_t *p = qdev_get_prop_ptr(dev, prop);
63 bool value = (*p & qdev_get_prop_mask(prop)) != 0;
65 visit_type_bool(v, &value, name, errp);
68 static void set_bit(DeviceState *dev, Visitor *v, void *opaque,
69 const char *name, Error **errp)
71 Property *prop = opaque;
72 Error *local_err = NULL;
73 bool value;
75 if (dev->state != DEV_STATE_CREATED) {
76 error_set(errp, QERR_PERMISSION_DENIED);
77 return;
80 visit_type_bool(v, &value, name, &local_err);
81 if (local_err) {
82 error_propagate(errp, local_err);
83 return;
85 bit_prop_set(dev, prop, value);
88 PropertyInfo qdev_prop_bit = {
89 .name = "boolean",
90 .legacy_name = "on/off",
91 .type = PROP_TYPE_BIT,
92 .size = sizeof(uint32_t),
93 .parse = parse_bit,
94 .print = print_bit,
95 .get = get_bit,
96 .set = set_bit,
99 /* --- 8bit integer --- */
101 static int parse_uint8(DeviceState *dev, Property *prop, const char *str)
103 uint8_t *ptr = qdev_get_prop_ptr(dev, prop);
104 char *end;
106 /* accept both hex and decimal */
107 *ptr = strtoul(str, &end, 0);
108 if ((*end != '\0') || (end == str)) {
109 return -EINVAL;
112 return 0;
115 static int print_uint8(DeviceState *dev, Property *prop, char *dest, size_t len)
117 uint8_t *ptr = qdev_get_prop_ptr(dev, prop);
118 return snprintf(dest, len, "%" PRIu8, *ptr);
121 static void get_int8(DeviceState *dev, Visitor *v, void *opaque,
122 const char *name, Error **errp)
124 Property *prop = opaque;
125 int8_t *ptr = qdev_get_prop_ptr(dev, prop);
126 int64_t value;
128 value = *ptr;
129 visit_type_int(v, &value, name, errp);
132 static void set_int8(DeviceState *dev, Visitor *v, void *opaque,
133 const char *name, Error **errp)
135 Property *prop = opaque;
136 int8_t *ptr = qdev_get_prop_ptr(dev, prop);
137 Error *local_err = NULL;
138 int64_t value;
140 if (dev->state != DEV_STATE_CREATED) {
141 error_set(errp, QERR_PERMISSION_DENIED);
142 return;
145 visit_type_int(v, &value, name, &local_err);
146 if (local_err) {
147 error_propagate(errp, local_err);
148 return;
150 if (value > prop->info->min && value <= prop->info->max) {
151 *ptr = value;
152 } else {
153 error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE,
154 dev->id?:"", name, value, prop->info->min,
155 prop->info->max);
159 PropertyInfo qdev_prop_uint8 = {
160 .name = "uint8",
161 .type = PROP_TYPE_UINT8,
162 .size = sizeof(uint8_t),
163 .parse = parse_uint8,
164 .print = print_uint8,
165 .get = get_int8,
166 .set = set_int8,
167 .min = 0,
168 .max = 255,
171 /* --- 8bit hex value --- */
173 static int parse_hex8(DeviceState *dev, Property *prop, const char *str)
175 uint8_t *ptr = qdev_get_prop_ptr(dev, prop);
176 char *end;
178 *ptr = strtoul(str, &end, 16);
179 if ((*end != '\0') || (end == str)) {
180 return -EINVAL;
183 return 0;
186 static int print_hex8(DeviceState *dev, Property *prop, char *dest, size_t len)
188 uint8_t *ptr = qdev_get_prop_ptr(dev, prop);
189 return snprintf(dest, len, "0x%" PRIx8, *ptr);
192 PropertyInfo qdev_prop_hex8 = {
193 .name = "uint8",
194 .legacy_name = "hex8",
195 .type = PROP_TYPE_UINT8,
196 .size = sizeof(uint8_t),
197 .parse = parse_hex8,
198 .print = print_hex8,
199 .get = get_int8,
200 .set = set_int8,
201 .min = 0,
202 .max = 255,
205 /* --- 16bit integer --- */
207 static int parse_uint16(DeviceState *dev, Property *prop, const char *str)
209 uint16_t *ptr = qdev_get_prop_ptr(dev, prop);
210 char *end;
212 /* accept both hex and decimal */
213 *ptr = strtoul(str, &end, 0);
214 if ((*end != '\0') || (end == str)) {
215 return -EINVAL;
218 return 0;
221 static int print_uint16(DeviceState *dev, Property *prop, char *dest, size_t len)
223 uint16_t *ptr = qdev_get_prop_ptr(dev, prop);
224 return snprintf(dest, len, "%" PRIu16, *ptr);
227 static void get_int16(DeviceState *dev, Visitor *v, void *opaque,
228 const char *name, Error **errp)
230 Property *prop = opaque;
231 int16_t *ptr = qdev_get_prop_ptr(dev, prop);
232 int64_t value;
234 value = *ptr;
235 visit_type_int(v, &value, name, errp);
238 static void set_int16(DeviceState *dev, Visitor *v, void *opaque,
239 const char *name, Error **errp)
241 Property *prop = opaque;
242 int16_t *ptr = qdev_get_prop_ptr(dev, prop);
243 Error *local_err = NULL;
244 int64_t value;
246 if (dev->state != DEV_STATE_CREATED) {
247 error_set(errp, QERR_PERMISSION_DENIED);
248 return;
251 visit_type_int(v, &value, name, &local_err);
252 if (local_err) {
253 error_propagate(errp, local_err);
254 return;
256 if (value > prop->info->min && value <= prop->info->max) {
257 *ptr = value;
258 } else {
259 error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE,
260 dev->id?:"", name, value, prop->info->min,
261 prop->info->max);
265 PropertyInfo qdev_prop_uint16 = {
266 .name = "uint16",
267 .type = PROP_TYPE_UINT16,
268 .size = sizeof(uint16_t),
269 .parse = parse_uint16,
270 .print = print_uint16,
271 .get = get_int16,
272 .set = set_int16,
273 .min = 0,
274 .max = 65535,
277 /* --- 32bit integer --- */
279 static int parse_uint32(DeviceState *dev, Property *prop, const char *str)
281 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
282 char *end;
284 /* accept both hex and decimal */
285 *ptr = strtoul(str, &end, 0);
286 if ((*end != '\0') || (end == str)) {
287 return -EINVAL;
290 return 0;
293 static int print_uint32(DeviceState *dev, Property *prop, char *dest, size_t len)
295 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
296 return snprintf(dest, len, "%" PRIu32, *ptr);
299 static void get_int32(DeviceState *dev, Visitor *v, void *opaque,
300 const char *name, Error **errp)
302 Property *prop = opaque;
303 int32_t *ptr = qdev_get_prop_ptr(dev, prop);
304 int64_t value;
306 value = *ptr;
307 visit_type_int(v, &value, name, errp);
310 static void set_int32(DeviceState *dev, Visitor *v, void *opaque,
311 const char *name, Error **errp)
313 Property *prop = opaque;
314 int32_t *ptr = qdev_get_prop_ptr(dev, prop);
315 Error *local_err = NULL;
316 int64_t value;
318 if (dev->state != DEV_STATE_CREATED) {
319 error_set(errp, QERR_PERMISSION_DENIED);
320 return;
323 visit_type_int(v, &value, name, &local_err);
324 if (local_err) {
325 error_propagate(errp, local_err);
326 return;
328 if (value > prop->info->min && value <= prop->info->max) {
329 *ptr = value;
330 } else {
331 error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE,
332 dev->id?:"", name, value, prop->info->min,
333 prop->info->max);
337 PropertyInfo qdev_prop_uint32 = {
338 .name = "uint32",
339 .type = PROP_TYPE_UINT32,
340 .size = sizeof(uint32_t),
341 .parse = parse_uint32,
342 .print = print_uint32,
343 .get = get_int32,
344 .set = set_int32,
345 .min = 0,
346 .max = 0xFFFFFFFFULL,
349 static int parse_int32(DeviceState *dev, Property *prop, const char *str)
351 int32_t *ptr = qdev_get_prop_ptr(dev, prop);
352 char *end;
354 *ptr = strtol(str, &end, 10);
355 if ((*end != '\0') || (end == str)) {
356 return -EINVAL;
359 return 0;
362 static int print_int32(DeviceState *dev, Property *prop, char *dest, size_t len)
364 int32_t *ptr = qdev_get_prop_ptr(dev, prop);
365 return snprintf(dest, len, "%" PRId32, *ptr);
368 PropertyInfo qdev_prop_int32 = {
369 .name = "int32",
370 .type = PROP_TYPE_INT32,
371 .size = sizeof(int32_t),
372 .parse = parse_int32,
373 .print = print_int32,
374 .get = get_int32,
375 .set = set_int32,
376 .min = -0x80000000LL,
377 .max = 0x7FFFFFFFLL,
380 /* --- 32bit hex value --- */
382 static int parse_hex32(DeviceState *dev, Property *prop, const char *str)
384 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
385 char *end;
387 *ptr = strtoul(str, &end, 16);
388 if ((*end != '\0') || (end == str)) {
389 return -EINVAL;
392 return 0;
395 static int print_hex32(DeviceState *dev, Property *prop, char *dest, size_t len)
397 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
398 return snprintf(dest, len, "0x%" PRIx32, *ptr);
401 PropertyInfo qdev_prop_hex32 = {
402 .name = "uint32",
403 .legacy_name = "hex32",
404 .type = PROP_TYPE_UINT32,
405 .size = sizeof(uint32_t),
406 .parse = parse_hex32,
407 .print = print_hex32,
408 .get = get_int32,
409 .set = set_int32,
410 .min = 0,
411 .max = 0xFFFFFFFFULL,
414 /* --- 64bit integer --- */
416 static int parse_uint64(DeviceState *dev, Property *prop, const char *str)
418 uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
419 char *end;
421 /* accept both hex and decimal */
422 *ptr = strtoull(str, &end, 0);
423 if ((*end != '\0') || (end == str)) {
424 return -EINVAL;
427 return 0;
430 static int print_uint64(DeviceState *dev, Property *prop, char *dest, size_t len)
432 uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
433 return snprintf(dest, len, "%" PRIu64, *ptr);
436 static void get_int64(DeviceState *dev, Visitor *v, void *opaque,
437 const char *name, Error **errp)
439 Property *prop = opaque;
440 int64_t *ptr = qdev_get_prop_ptr(dev, prop);
442 visit_type_int(v, ptr, name, errp);
445 static void set_int64(DeviceState *dev, Visitor *v, void *opaque,
446 const char *name, Error **errp)
448 Property *prop = opaque;
449 int64_t *ptr = qdev_get_prop_ptr(dev, prop);
451 if (dev->state != DEV_STATE_CREATED) {
452 error_set(errp, QERR_PERMISSION_DENIED);
453 return;
456 visit_type_int(v, ptr, name, errp);
459 PropertyInfo qdev_prop_uint64 = {
460 .name = "uint64",
461 .type = PROP_TYPE_UINT64,
462 .size = sizeof(uint64_t),
463 .parse = parse_uint64,
464 .print = print_uint64,
465 .get = get_int64,
466 .set = set_int64,
469 /* --- 64bit hex value --- */
471 static int parse_hex64(DeviceState *dev, Property *prop, const char *str)
473 uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
474 char *end;
476 *ptr = strtoull(str, &end, 16);
477 if ((*end != '\0') || (end == str)) {
478 return -EINVAL;
481 return 0;
484 static int print_hex64(DeviceState *dev, Property *prop, char *dest, size_t len)
486 uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
487 return snprintf(dest, len, "0x%" PRIx64, *ptr);
490 PropertyInfo qdev_prop_hex64 = {
491 .name = "uint64",
492 .legacy_name = "hex64",
493 .type = PROP_TYPE_UINT64,
494 .size = sizeof(uint64_t),
495 .parse = parse_hex64,
496 .print = print_hex64,
497 .get = get_int64,
498 .set = set_int64,
501 /* --- string --- */
503 static int parse_string(DeviceState *dev, Property *prop, const char *str)
505 char **ptr = qdev_get_prop_ptr(dev, prop);
507 if (*ptr)
508 g_free(*ptr);
509 *ptr = g_strdup(str);
510 return 0;
513 static void free_string(DeviceState *dev, Property *prop)
515 g_free(*(char **)qdev_get_prop_ptr(dev, prop));
518 static int print_string(DeviceState *dev, Property *prop, char *dest, size_t len)
520 char **ptr = qdev_get_prop_ptr(dev, prop);
521 if (!*ptr)
522 return snprintf(dest, len, "<null>");
523 return snprintf(dest, len, "\"%s\"", *ptr);
526 static void get_string(DeviceState *dev, Visitor *v, void *opaque,
527 const char *name, Error **errp)
529 Property *prop = opaque;
530 char **ptr = qdev_get_prop_ptr(dev, prop);
532 if (!*ptr) {
533 char *str = (char *)"";
534 visit_type_str(v, &str, name, errp);
535 } else {
536 visit_type_str(v, ptr, name, errp);
540 static void set_string(DeviceState *dev, Visitor *v, void *opaque,
541 const char *name, Error **errp)
543 Property *prop = opaque;
544 char **ptr = qdev_get_prop_ptr(dev, prop);
545 Error *local_err = NULL;
546 char *str;
548 if (dev->state != DEV_STATE_CREATED) {
549 error_set(errp, QERR_PERMISSION_DENIED);
550 return;
553 visit_type_str(v, &str, name, &local_err);
554 if (local_err) {
555 error_propagate(errp, local_err);
556 return;
558 if (!*str) {
559 g_free(str);
560 str = NULL;
562 if (*ptr) {
563 g_free(*ptr);
565 *ptr = str;
568 PropertyInfo qdev_prop_string = {
569 .name = "string",
570 .type = PROP_TYPE_STRING,
571 .size = sizeof(char*),
572 .parse = parse_string,
573 .print = print_string,
574 .free = free_string,
575 .get = get_string,
576 .set = set_string,
579 /* --- drive --- */
581 static int parse_drive(DeviceState *dev, Property *prop, const char *str)
583 BlockDriverState **ptr = qdev_get_prop_ptr(dev, prop);
584 BlockDriverState *bs;
586 bs = bdrv_find(str);
587 if (bs == NULL)
588 return -ENOENT;
589 if (bdrv_attach_dev(bs, dev) < 0)
590 return -EEXIST;
591 *ptr = bs;
592 return 0;
595 static void free_drive(DeviceState *dev, Property *prop)
597 BlockDriverState **ptr = qdev_get_prop_ptr(dev, prop);
599 if (*ptr) {
600 bdrv_detach_dev(*ptr, dev);
601 blockdev_auto_del(*ptr);
605 static int print_drive(DeviceState *dev, Property *prop, char *dest, size_t len)
607 BlockDriverState **ptr = qdev_get_prop_ptr(dev, prop);
608 return snprintf(dest, len, "%s",
609 *ptr ? bdrv_get_device_name(*ptr) : "<null>");
612 static void get_generic(DeviceState *dev, Visitor *v, void *opaque,
613 const char *name, Error **errp)
615 Property *prop = opaque;
616 void **ptr = qdev_get_prop_ptr(dev, prop);
617 char buffer[1024];
618 char *p = buffer;
620 buffer[0] = 0;
621 if (*ptr) {
622 prop->info->print(dev, prop, buffer, sizeof(buffer));
624 visit_type_str(v, &p, name, errp);
627 static void set_generic(DeviceState *dev, Visitor *v, void *opaque,
628 const char *name, Error **errp)
630 Property *prop = opaque;
631 Error *local_err = NULL;
632 char *str;
633 int ret;
635 if (dev->state != DEV_STATE_CREATED) {
636 error_set(errp, QERR_PERMISSION_DENIED);
637 return;
640 visit_type_str(v, &str, name, &local_err);
641 if (local_err) {
642 error_propagate(errp, local_err);
643 return;
645 if (!*str) {
646 g_free(str);
647 error_set_from_qdev_prop_error(errp, EINVAL, dev, prop, str);
648 return;
650 ret = prop->info->parse(dev, prop, str);
651 error_set_from_qdev_prop_error(errp, ret, dev, prop, str);
652 g_free(str);
655 PropertyInfo qdev_prop_drive = {
656 .name = "drive",
657 .type = PROP_TYPE_DRIVE,
658 .size = sizeof(BlockDriverState *),
659 .parse = parse_drive,
660 .print = print_drive,
661 .get = get_generic,
662 .set = set_generic,
663 .free = free_drive,
666 /* --- character device --- */
668 static int parse_chr(DeviceState *dev, Property *prop, const char *str)
670 CharDriverState **ptr = qdev_get_prop_ptr(dev, prop);
672 *ptr = qemu_chr_find(str);
673 if (*ptr == NULL) {
674 return -ENOENT;
676 if ((*ptr)->avail_connections < 1) {
677 return -EEXIST;
679 --(*ptr)->avail_connections;
680 return 0;
683 static void free_chr(DeviceState *dev, Property *prop)
685 CharDriverState **ptr = qdev_get_prop_ptr(dev, prop);
687 if (*ptr) {
688 qemu_chr_add_handlers(*ptr, NULL, NULL, NULL, NULL);
693 static int print_chr(DeviceState *dev, Property *prop, char *dest, size_t len)
695 CharDriverState **ptr = qdev_get_prop_ptr(dev, prop);
697 if (*ptr && (*ptr)->label) {
698 return snprintf(dest, len, "%s", (*ptr)->label);
699 } else {
700 return snprintf(dest, len, "<null>");
704 PropertyInfo qdev_prop_chr = {
705 .name = "chr",
706 .type = PROP_TYPE_CHR,
707 .size = sizeof(CharDriverState*),
708 .parse = parse_chr,
709 .print = print_chr,
710 .get = get_generic,
711 .set = set_generic,
712 .free = free_chr,
715 /* --- netdev device --- */
717 static int parse_netdev(DeviceState *dev, Property *prop, const char *str)
719 VLANClientState **ptr = qdev_get_prop_ptr(dev, prop);
721 *ptr = qemu_find_netdev(str);
722 if (*ptr == NULL)
723 return -ENOENT;
724 if ((*ptr)->peer) {
725 return -EEXIST;
727 return 0;
730 static int print_netdev(DeviceState *dev, Property *prop, char *dest, size_t len)
732 VLANClientState **ptr = qdev_get_prop_ptr(dev, prop);
734 if (*ptr && (*ptr)->name) {
735 return snprintf(dest, len, "%s", (*ptr)->name);
736 } else {
737 return snprintf(dest, len, "<null>");
741 PropertyInfo qdev_prop_netdev = {
742 .name = "netdev",
743 .type = PROP_TYPE_NETDEV,
744 .size = sizeof(VLANClientState*),
745 .parse = parse_netdev,
746 .print = print_netdev,
747 .get = get_generic,
748 .set = set_generic,
751 /* --- vlan --- */
753 static int parse_vlan(DeviceState *dev, Property *prop, const char *str)
755 VLANState **ptr = qdev_get_prop_ptr(dev, prop);
756 int id;
758 if (sscanf(str, "%d", &id) != 1)
759 return -EINVAL;
760 *ptr = qemu_find_vlan(id, 1);
761 if (*ptr == NULL)
762 return -ENOENT;
763 return 0;
766 static int print_vlan(DeviceState *dev, Property *prop, char *dest, size_t len)
768 VLANState **ptr = qdev_get_prop_ptr(dev, prop);
770 if (*ptr) {
771 return snprintf(dest, len, "%d", (*ptr)->id);
772 } else {
773 return snprintf(dest, len, "<null>");
777 static void get_vlan(DeviceState *dev, Visitor *v, void *opaque,
778 const char *name, Error **errp)
780 Property *prop = opaque;
781 VLANState **ptr = qdev_get_prop_ptr(dev, prop);
782 int64_t id;
784 id = *ptr ? (*ptr)->id : -1;
785 visit_type_int(v, &id, name, errp);
788 static void set_vlan(DeviceState *dev, Visitor *v, void *opaque,
789 const char *name, Error **errp)
791 Property *prop = opaque;
792 VLANState **ptr = qdev_get_prop_ptr(dev, prop);
793 Error *local_err = NULL;
794 int64_t id;
795 VLANState *vlan;
797 if (dev->state != DEV_STATE_CREATED) {
798 error_set(errp, QERR_PERMISSION_DENIED);
799 return;
802 visit_type_int(v, &id, name, &local_err);
803 if (local_err) {
804 error_propagate(errp, local_err);
805 return;
807 if (id == -1) {
808 *ptr = NULL;
809 return;
811 vlan = qemu_find_vlan(id, 1);
812 if (!vlan) {
813 error_set(errp, QERR_INVALID_PARAMETER_VALUE,
814 name, prop->info->name);
815 return;
817 *ptr = vlan;
820 PropertyInfo qdev_prop_vlan = {
821 .name = "vlan",
822 .type = PROP_TYPE_VLAN,
823 .size = sizeof(VLANClientState*),
824 .parse = parse_vlan,
825 .print = print_vlan,
826 .get = get_vlan,
827 .set = set_vlan,
830 /* --- pointer --- */
832 /* Not a proper property, just for dirty hacks. TODO Remove it! */
833 PropertyInfo qdev_prop_ptr = {
834 .name = "ptr",
835 .type = PROP_TYPE_PTR,
836 .size = sizeof(void*),
839 /* --- mac address --- */
842 * accepted syntax versions:
843 * 01:02:03:04:05:06
844 * 01-02-03-04-05-06
846 static int parse_mac(DeviceState *dev, Property *prop, const char *str)
848 MACAddr *mac = qdev_get_prop_ptr(dev, prop);
849 int i, pos;
850 char *p;
852 for (i = 0, pos = 0; i < 6; i++, pos += 3) {
853 if (!qemu_isxdigit(str[pos]))
854 return -EINVAL;
855 if (!qemu_isxdigit(str[pos+1]))
856 return -EINVAL;
857 if (i == 5) {
858 if (str[pos+2] != '\0')
859 return -EINVAL;
860 } else {
861 if (str[pos+2] != ':' && str[pos+2] != '-')
862 return -EINVAL;
864 mac->a[i] = strtol(str+pos, &p, 16);
866 return 0;
869 static int print_mac(DeviceState *dev, Property *prop, char *dest, size_t len)
871 MACAddr *mac = qdev_get_prop_ptr(dev, prop);
873 return snprintf(dest, len, "%02x:%02x:%02x:%02x:%02x:%02x",
874 mac->a[0], mac->a[1], mac->a[2],
875 mac->a[3], mac->a[4], mac->a[5]);
878 PropertyInfo qdev_prop_macaddr = {
879 .name = "macaddr",
880 .type = PROP_TYPE_MACADDR,
881 .size = sizeof(MACAddr),
882 .parse = parse_mac,
883 .print = print_mac,
884 .get = get_generic,
885 .set = set_generic,
888 /* --- pci address --- */
891 * bus-local address, i.e. "$slot" or "$slot.$fn"
893 static int parse_pci_devfn(DeviceState *dev, Property *prop, const char *str)
895 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
896 unsigned int slot, fn, n;
898 if (sscanf(str, "%x.%x%n", &slot, &fn, &n) != 2) {
899 fn = 0;
900 if (sscanf(str, "%x%n", &slot, &n) != 1) {
901 return -EINVAL;
904 if (str[n] != '\0')
905 return -EINVAL;
906 if (fn > 7)
907 return -EINVAL;
908 if (slot > 31)
909 return -EINVAL;
910 *ptr = slot << 3 | fn;
911 return 0;
914 static int print_pci_devfn(DeviceState *dev, Property *prop, char *dest, size_t len)
916 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
918 if (*ptr == -1) {
919 return snprintf(dest, len, "<unset>");
920 } else {
921 return snprintf(dest, len, "%02x.%x", *ptr >> 3, *ptr & 7);
925 static void get_pci_devfn(DeviceState *dev, Visitor *v, void *opaque,
926 const char *name, Error **errp)
928 Property *prop = opaque;
929 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
930 char buffer[32];
931 char *p = buffer;
933 buffer[0] = 0;
934 if (*ptr != -1) {
935 snprintf(buffer, sizeof(buffer), "%02x.%x", *ptr >> 3, *ptr & 7);
937 visit_type_str(v, &p, name, errp);
940 PropertyInfo qdev_prop_pci_devfn = {
941 .name = "pci-devfn",
942 .type = PROP_TYPE_UINT32,
943 .size = sizeof(uint32_t),
944 .parse = parse_pci_devfn,
945 .print = print_pci_devfn,
946 .get = get_pci_devfn,
947 .set = set_generic,
950 /* --- public helpers --- */
952 static Property *qdev_prop_walk(Property *props, const char *name)
954 if (!props)
955 return NULL;
956 while (props->name) {
957 if (strcmp(props->name, name) == 0)
958 return props;
959 props++;
961 return NULL;
964 static Property *qdev_prop_find(DeviceState *dev, const char *name)
966 Property *prop;
968 /* device properties */
969 prop = qdev_prop_walk(dev->info->props, name);
970 if (prop)
971 return prop;
973 /* bus properties */
974 prop = qdev_prop_walk(dev->parent_bus->info->props, name);
975 if (prop)
976 return prop;
978 return NULL;
981 int qdev_prop_exists(DeviceState *dev, const char *name)
983 return qdev_prop_find(dev, name) ? true : false;
986 void error_set_from_qdev_prop_error(Error **errp, int ret, DeviceState *dev,
987 Property *prop, const char *value)
989 switch (ret) {
990 case -EEXIST:
991 error_set(errp, QERR_PROPERTY_VALUE_IN_USE,
992 dev->info->name, prop->name, value);
993 break;
994 default:
995 case -EINVAL:
996 error_set(errp, QERR_PROPERTY_VALUE_BAD,
997 dev->info->name, prop->name, value);
998 break;
999 case -ENOENT:
1000 error_set(errp, QERR_PROPERTY_VALUE_NOT_FOUND,
1001 dev->info->name, prop->name, value);
1002 break;
1003 case 0:
1004 break;
1008 int qdev_prop_parse(DeviceState *dev, const char *name, const char *value)
1010 Property *prop;
1011 int ret;
1013 prop = qdev_prop_find(dev, name);
1015 * TODO Properties without a parse method are just for dirty
1016 * hacks. qdev_prop_ptr is the only such PropertyInfo. It's
1017 * marked for removal. The test !prop->info->parse should be
1018 * removed along with it.
1020 if (!prop || !prop->info->parse) {
1021 qerror_report(QERR_PROPERTY_NOT_FOUND, dev->info->name, name);
1022 return -1;
1024 ret = prop->info->parse(dev, prop, value);
1025 if (ret < 0) {
1026 Error *err;
1027 error_set_from_qdev_prop_error(&err, ret, dev, prop, value);
1028 qerror_report_err(err);
1029 error_free(err);
1030 return -1;
1032 return 0;
1035 void qdev_prop_set(DeviceState *dev, const char *name, void *src, enum PropertyType type)
1037 Property *prop;
1039 prop = qdev_prop_find(dev, name);
1040 if (!prop) {
1041 fprintf(stderr, "%s: property \"%s.%s\" not found\n",
1042 __FUNCTION__, dev->info->name, name);
1043 abort();
1045 if (prop->info->type != type) {
1046 fprintf(stderr, "%s: property \"%s.%s\" type mismatch\n",
1047 __FUNCTION__, dev->info->name, name);
1048 abort();
1050 qdev_prop_cpy(dev, prop, src);
1053 void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value)
1055 qdev_prop_set(dev, name, &value, PROP_TYPE_BIT);
1058 void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value)
1060 qdev_prop_set(dev, name, &value, PROP_TYPE_UINT8);
1063 void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value)
1065 qdev_prop_set(dev, name, &value, PROP_TYPE_UINT16);
1068 void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value)
1070 qdev_prop_set(dev, name, &value, PROP_TYPE_UINT32);
1073 void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value)
1075 qdev_prop_set(dev, name, &value, PROP_TYPE_INT32);
1078 void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value)
1080 qdev_prop_set(dev, name, &value, PROP_TYPE_UINT64);
1083 void qdev_prop_set_string(DeviceState *dev, const char *name, char *value)
1085 qdev_prop_set(dev, name, &value, PROP_TYPE_STRING);
1088 int qdev_prop_set_drive(DeviceState *dev, const char *name, BlockDriverState *value)
1090 int res;
1092 res = bdrv_attach_dev(value, dev);
1093 if (res < 0) {
1094 error_report("Can't attach drive %s to %s.%s: %s",
1095 bdrv_get_device_name(value),
1096 dev->id ? dev->id : dev->info->name,
1097 name, strerror(-res));
1098 return -1;
1100 qdev_prop_set(dev, name, &value, PROP_TYPE_DRIVE);
1101 return 0;
1104 void qdev_prop_set_drive_nofail(DeviceState *dev, const char *name, BlockDriverState *value)
1106 if (qdev_prop_set_drive(dev, name, value) < 0) {
1107 exit(1);
1110 void qdev_prop_set_chr(DeviceState *dev, const char *name, CharDriverState *value)
1112 qdev_prop_set(dev, name, &value, PROP_TYPE_CHR);
1115 void qdev_prop_set_netdev(DeviceState *dev, const char *name, VLANClientState *value)
1117 qdev_prop_set(dev, name, &value, PROP_TYPE_NETDEV);
1120 void qdev_prop_set_vlan(DeviceState *dev, const char *name, VLANState *value)
1122 qdev_prop_set(dev, name, &value, PROP_TYPE_VLAN);
1125 void qdev_prop_set_macaddr(DeviceState *dev, const char *name, uint8_t *value)
1127 qdev_prop_set(dev, name, value, PROP_TYPE_MACADDR);
1130 void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value)
1132 qdev_prop_set(dev, name, &value, PROP_TYPE_PTR);
1135 void qdev_prop_set_defaults(DeviceState *dev, Property *props)
1137 if (!props)
1138 return;
1139 while (props->name) {
1140 if (props->defval) {
1141 qdev_prop_cpy(dev, props, props->defval);
1143 props++;
1147 static QTAILQ_HEAD(, GlobalProperty) global_props = QTAILQ_HEAD_INITIALIZER(global_props);
1149 static void qdev_prop_register_global(GlobalProperty *prop)
1151 QTAILQ_INSERT_TAIL(&global_props, prop, next);
1154 void qdev_prop_register_global_list(GlobalProperty *props)
1156 int i;
1158 for (i = 0; props[i].driver != NULL; i++) {
1159 qdev_prop_register_global(props+i);
1163 void qdev_prop_set_globals(DeviceState *dev)
1165 GlobalProperty *prop;
1167 QTAILQ_FOREACH(prop, &global_props, next) {
1168 if (strcmp(dev->info->name, prop->driver) != 0 &&
1169 strcmp(dev->info->bus_info->name, prop->driver) != 0) {
1170 continue;
1172 if (qdev_prop_parse(dev, prop->property, prop->value) != 0) {
1173 exit(1);
1178 static int qdev_add_one_global(QemuOpts *opts, void *opaque)
1180 GlobalProperty *g;
1182 g = g_malloc0(sizeof(*g));
1183 g->driver = qemu_opt_get(opts, "driver");
1184 g->property = qemu_opt_get(opts, "property");
1185 g->value = qemu_opt_get(opts, "value");
1186 qdev_prop_register_global(g);
1187 return 0;
1190 void qemu_add_globals(void)
1192 qemu_opts_foreach(qemu_find_opts("global"), qdev_add_one_global, NULL, 0);