]> git.proxmox.com Git - mirror_qemu.git/blame - include/hw/qdev-properties.h
qdev: wrap default property value in an union
[mirror_qemu.git] / include / hw / qdev-properties.h
CommitLineData
074a86fc
AL
1#ifndef QEMU_QDEV_PROPERTIES_H
2#define QEMU_QDEV_PROPERTIES_H
3
83c9f4ca 4#include "hw/qdev-core.h"
074a86fc
AL
5
6/*** qdev-properties.c ***/
7
8extern PropertyInfo qdev_prop_bit;
fdba6d96 9extern PropertyInfo qdev_prop_bit64;
72cc5137 10extern PropertyInfo qdev_prop_bool;
074a86fc
AL
11extern PropertyInfo qdev_prop_uint8;
12extern PropertyInfo qdev_prop_uint16;
13extern PropertyInfo qdev_prop_uint32;
14extern PropertyInfo qdev_prop_int32;
15extern PropertyInfo qdev_prop_uint64;
e8cd45c7 16extern PropertyInfo qdev_prop_size;
074a86fc
AL
17extern PropertyInfo qdev_prop_string;
18extern PropertyInfo qdev_prop_chr;
19extern PropertyInfo qdev_prop_ptr;
20extern PropertyInfo qdev_prop_macaddr;
55e8a154 21extern PropertyInfo qdev_prop_on_off_auto;
074a86fc 22extern PropertyInfo qdev_prop_losttickpolicy;
8c398252 23extern PropertyInfo qdev_prop_blockdev_on_error;
074a86fc 24extern PropertyInfo qdev_prop_bios_chs_trans;
a73275dd 25extern PropertyInfo qdev_prop_fdc_drive_type;
074a86fc
AL
26extern PropertyInfo qdev_prop_drive;
27extern PropertyInfo qdev_prop_netdev;
28extern PropertyInfo qdev_prop_vlan;
29extern PropertyInfo qdev_prop_pci_devfn;
30extern PropertyInfo qdev_prop_blocksize;
31extern PropertyInfo qdev_prop_pci_host_devaddr;
0be6bfac 32extern PropertyInfo qdev_prop_arraylen;
074a86fc
AL
33
34#define DEFINE_PROP(_name, _state, _field, _prop, _type) { \
35 .name = (_name), \
36 .info = &(_prop), \
37 .offset = offsetof(_state, _field) \
1ceef9f2 38 + type_check(_type, typeof_field(_state, _field)), \
074a86fc 39 }
85bbd1e7 40#define DEFINE_PROP_SIGNED(_name, _state, _field, _defval, _prop, _type) { \
074a86fc
AL
41 .name = (_name), \
42 .info = &(_prop), \
43 .offset = offsetof(_state, _field) \
44 + type_check(_type,typeof_field(_state, _field)), \
76318657 45 .defval.i = (_type)_defval, \
074a86fc
AL
46 }
47#define DEFINE_PROP_BIT(_name, _state, _field, _bit, _defval) { \
48 .name = (_name), \
49 .info = &(qdev_prop_bit), \
50 .bitnr = (_bit), \
51 .offset = offsetof(_state, _field) \
52 + type_check(uint32_t,typeof_field(_state, _field)), \
76318657 53 .defval.i = (bool)_defval, \
074a86fc 54 }
fdba6d96
GH
55#define DEFINE_PROP_BIT64(_name, _state, _field, _bit, _defval) { \
56 .name = (_name), \
8aedc369 57 .info = &(qdev_prop_bit64), \
fdba6d96
GH
58 .bitnr = (_bit), \
59 .offset = offsetof(_state, _field) \
60 + type_check(uint64_t, typeof_field(_state, _field)), \
76318657 61 .defval.i = (bool)_defval, \
fdba6d96 62 }
074a86fc 63
72cc5137
IM
64#define DEFINE_PROP_BOOL(_name, _state, _field, _defval) { \
65 .name = (_name), \
66 .info = &(qdev_prop_bool), \
67 .offset = offsetof(_state, _field) \
68 + type_check(bool, typeof_field(_state, _field)), \
76318657 69 .defval.i = (bool)_defval, \
72cc5137
IM
70 }
71
0be6bfac
PM
72#define PROP_ARRAY_LEN_PREFIX "len-"
73
74/**
75 * DEFINE_PROP_ARRAY:
76 * @_name: name of the array
77 * @_state: name of the device state structure type
78 * @_field: uint32_t field in @_state to hold the array length
79 * @_arrayfield: field in @_state (of type '@_arraytype *') which
80 * will point to the array
81 * @_arrayprop: PropertyInfo defining what property the array elements have
82 * @_arraytype: C type of the array elements
83 *
84 * Define device properties for a variable-length array _name. A
85 * static property "len-arrayname" is defined. When the device creator
86 * sets this property to the desired length of array, further dynamic
87 * properties "arrayname[0]", "arrayname[1]", ... are defined so the
88 * device creator can set the array element values. Setting the
89 * "len-arrayname" property more than once is an error.
90 *
91 * When the array length is set, the @_field member of the device
92 * struct is set to the array length, and @_arrayfield is set to point
93 * to (zero-initialised) memory allocated for the array. For a zero
94 * length array, @_field will be set to 0 and @_arrayfield to NULL.
95 * It is the responsibility of the device deinit code to free the
96 * @_arrayfield memory.
97 */
98#define DEFINE_PROP_ARRAY(_name, _state, _field, \
99 _arrayfield, _arrayprop, _arraytype) { \
100 .name = (PROP_ARRAY_LEN_PREFIX _name), \
101 .info = &(qdev_prop_arraylen), \
102 .offset = offsetof(_state, _field) \
103 + type_check(uint32_t, typeof_field(_state, _field)), \
0be6bfac
PM
104 .arrayinfo = &(_arrayprop), \
105 .arrayfieldsize = sizeof(_arraytype), \
106 .arrayoffset = offsetof(_state, _arrayfield), \
107 }
108
074a86fc 109#define DEFINE_PROP_UINT8(_n, _s, _f, _d) \
85bbd1e7 110 DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_uint8, uint8_t)
074a86fc 111#define DEFINE_PROP_UINT16(_n, _s, _f, _d) \
85bbd1e7 112 DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_uint16, uint16_t)
074a86fc 113#define DEFINE_PROP_UINT32(_n, _s, _f, _d) \
85bbd1e7 114 DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_uint32, uint32_t)
074a86fc 115#define DEFINE_PROP_INT32(_n, _s, _f, _d) \
85bbd1e7 116 DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_int32, int32_t)
074a86fc 117#define DEFINE_PROP_UINT64(_n, _s, _f, _d) \
85bbd1e7 118 DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_uint64, uint64_t)
e8cd45c7 119#define DEFINE_PROP_SIZE(_n, _s, _f, _d) \
85bbd1e7 120 DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_size, uint64_t)
074a86fc 121#define DEFINE_PROP_PCI_DEVFN(_n, _s, _f, _d) \
85bbd1e7 122 DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_pci_devfn, int32_t)
074a86fc 123
c272758f
MA
124/*
125 * Please avoid pointer properties. If you must use them, you must
126 * cover them in their device's class init function as follows:
127 *
128 * - If the property must be set, the device cannot be used with
129 * device_add, so add code like this:
130 * |* Reason: pointer property "NAME-OF-YOUR-PROP" *|
131 * DeviceClass *dc = DEVICE_CLASS(class);
e90f2a8c 132 * dc->user_creatable = false;
c272758f
MA
133 *
134 * - If the property may safely remain null, document it like this:
135 * |*
136 * * Note: pointer property "interrupt_vector" may remain null, thus
e90f2a8c 137 * * no need for dc->user_creatable = false;
c272758f
MA
138 * *|
139 */
074a86fc
AL
140#define DEFINE_PROP_PTR(_n, _s, _f) \
141 DEFINE_PROP(_n, _s, _f, qdev_prop_ptr, void*)
c272758f 142
074a86fc 143#define DEFINE_PROP_CHR(_n, _s, _f) \
becdfa00 144 DEFINE_PROP(_n, _s, _f, qdev_prop_chr, CharBackend)
074a86fc
AL
145#define DEFINE_PROP_STRING(_n, _s, _f) \
146 DEFINE_PROP(_n, _s, _f, qdev_prop_string, char*)
147#define DEFINE_PROP_NETDEV(_n, _s, _f) \
1ceef9f2 148 DEFINE_PROP(_n, _s, _f, qdev_prop_netdev, NICPeers)
074a86fc 149#define DEFINE_PROP_VLAN(_n, _s, _f) \
1ceef9f2 150 DEFINE_PROP(_n, _s, _f, qdev_prop_vlan, NICPeers)
074a86fc 151#define DEFINE_PROP_DRIVE(_n, _s, _f) \
4be74634 152 DEFINE_PROP(_n, _s, _f, qdev_prop_drive, BlockBackend *)
074a86fc
AL
153#define DEFINE_PROP_MACADDR(_n, _s, _f) \
154 DEFINE_PROP(_n, _s, _f, qdev_prop_macaddr, MACAddr)
55e8a154 155#define DEFINE_PROP_ON_OFF_AUTO(_n, _s, _f, _d) \
85bbd1e7 156 DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_on_off_auto, OnOffAuto)
074a86fc 157#define DEFINE_PROP_LOSTTICKPOLICY(_n, _s, _f, _d) \
85bbd1e7 158 DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_losttickpolicy, \
074a86fc 159 LostTickPolicy)
8c398252 160#define DEFINE_PROP_BLOCKDEV_ON_ERROR(_n, _s, _f, _d) \
85bbd1e7 161 DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_blockdev_on_error, \
8c398252 162 BlockdevOnError)
074a86fc 163#define DEFINE_PROP_BIOS_CHS_TRANS(_n, _s, _f, _d) \
85bbd1e7 164 DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_bios_chs_trans, int)
0eb28a42 165#define DEFINE_PROP_BLOCKSIZE(_n, _s, _f) \
85bbd1e7 166 DEFINE_PROP_SIGNED(_n, _s, _f, 0, qdev_prop_blocksize, uint16_t)
074a86fc
AL
167#define DEFINE_PROP_PCI_HOST_DEVADDR(_n, _s, _f) \
168 DEFINE_PROP(_n, _s, _f, qdev_prop_pci_host_devaddr, PCIHostDeviceAddress)
169
170#define DEFINE_PROP_END_OF_LIST() \
171 {}
172
173/* Set properties between creation and init. */
174void *qdev_get_prop_ptr(DeviceState *dev, Property *prop);
074a86fc
AL
175void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value);
176void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value);
177void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value);
178void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value);
179void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value);
180void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value);
181void qdev_prop_set_string(DeviceState *dev, const char *name, const char *value);
0ec7b3e7 182void qdev_prop_set_chr(DeviceState *dev, const char *name, Chardev *value);
074a86fc 183void qdev_prop_set_netdev(DeviceState *dev, const char *name, NetClientState *value);
9b3d111a
MA
184void qdev_prop_set_drive(DeviceState *dev, const char *name,
185 BlockBackend *value, Error **errp);
606fd0e2
KK
186void qdev_prop_set_macaddr(DeviceState *dev, const char *name,
187 const uint8_t *value);
074a86fc
AL
188void qdev_prop_set_enum(DeviceState *dev, const char *name, int value);
189/* FIXME: Remove opaque pointer properties. */
190void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value);
191
a404b612 192void qdev_prop_register_global(GlobalProperty *prop);
074a86fc 193void qdev_prop_register_global_list(GlobalProperty *props);
d828c430 194int qdev_prop_check_globals(void);
25f8dd96 195void qdev_prop_set_globals(DeviceState *dev);
074a86fc
AL
196void error_set_from_qdev_prop_error(Error **errp, int ret, DeviceState *dev,
197 Property *prop, const char *value);
198
199/**
d9d8d452
C
200 * qdev_property_add_static:
201 * @dev: Device to add the property to.
202 * @prop: The qdev property definition.
203 * @errp: location to store error information.
204 *
205 * Add a static QOM property to @dev for qdev property @prop.
206 * On error, store error in @errp. Static properties access data in a struct.
207 * The type of the QOM property is derived from prop->info.
074a86fc
AL
208 */
209void qdev_property_add_static(DeviceState *dev, Property *prop, Error **errp);
210
67cc7e0a
SH
211void qdev_alias_all_properties(DeviceState *target, Object *source);
212
b000dfbd
PM
213/**
214 * @qdev_prop_set_after_realize:
215 * @dev: device
216 * @name: name of property
217 * @errp: indirect pointer to Error to be set
218 * Set the Error object to report that an attempt was made to set a property
219 * on a device after it has already been realized. This is a utility function
220 * which allows property-setter functions to easily report the error in
221 * a friendly format identifying both the device and the property.
222 */
223void qdev_prop_set_after_realize(DeviceState *dev, const char *name,
224 Error **errp);
39f72ef9
SH
225
226/**
227 * qdev_prop_allow_set_link_before_realize:
228 *
229 * Set the #Error object if an attempt is made to set the link after realize.
230 * This function should be used as the check() argument to
231 * object_property_add_link().
232 */
233void qdev_prop_allow_set_link_before_realize(Object *obj, const char *name,
234 Object *val, Error **errp);
235
074a86fc 236#endif