virtio-9p: getattr server implementation for 9P2000.L protocol.
[qemu/stefanha.git] / hw / qdev.h
blob579328afc2c5e20a54cc6628972bd114cfd2f67c
1 #ifndef QDEV_H
2 #define QDEV_H
4 #include "hw.h"
5 #include "qemu-queue.h"
6 #include "qemu-char.h"
7 #include "qemu-option.h"
9 typedef struct Property Property;
11 typedef struct PropertyInfo PropertyInfo;
13 typedef struct CompatProperty CompatProperty;
15 typedef struct DeviceInfo DeviceInfo;
17 typedef struct BusState BusState;
19 typedef struct BusInfo BusInfo;
21 enum DevState {
22 DEV_STATE_CREATED = 1,
23 DEV_STATE_INITIALIZED,
26 enum {
27 DEV_NVECTORS_UNSPECIFIED = -1,
30 /* This structure should not be accessed directly. We declare it here
31 so that it can be embedded in individual device state structures. */
32 struct DeviceState {
33 const char *id;
34 enum DevState state;
35 QemuOpts *opts;
36 int hotplugged;
37 DeviceInfo *info;
38 BusState *parent_bus;
39 int num_gpio_out;
40 qemu_irq *gpio_out;
41 int num_gpio_in;
42 qemu_irq *gpio_in;
43 QLIST_HEAD(, BusState) child_bus;
44 int num_child_bus;
45 QLIST_ENTRY(DeviceState) sibling;
46 int instance_id_alias;
47 int alias_required_for_version;
50 typedef void (*bus_dev_printfn)(Monitor *mon, DeviceState *dev, int indent);
51 typedef char *(*bus_get_dev_path)(DeviceState *dev);
53 struct BusInfo {
54 const char *name;
55 size_t size;
56 bus_dev_printfn print_dev;
57 bus_get_dev_path get_dev_path;
58 Property *props;
61 struct BusState {
62 DeviceState *parent;
63 BusInfo *info;
64 const char *name;
65 int allow_hotplug;
66 int qdev_allocated;
67 QLIST_HEAD(, DeviceState) children;
68 QLIST_ENTRY(BusState) sibling;
71 struct Property {
72 const char *name;
73 PropertyInfo *info;
74 int offset;
75 int bitnr;
76 void *defval;
79 enum PropertyType {
80 PROP_TYPE_UNSPEC = 0,
81 PROP_TYPE_UINT8,
82 PROP_TYPE_UINT16,
83 PROP_TYPE_UINT32,
84 PROP_TYPE_INT32,
85 PROP_TYPE_UINT64,
86 PROP_TYPE_TADDR,
87 PROP_TYPE_MACADDR,
88 PROP_TYPE_DRIVE,
89 PROP_TYPE_CHR,
90 PROP_TYPE_STRING,
91 PROP_TYPE_NETDEV,
92 PROP_TYPE_VLAN,
93 PROP_TYPE_PTR,
94 PROP_TYPE_BIT,
97 struct PropertyInfo {
98 const char *name;
99 size_t size;
100 enum PropertyType type;
101 int (*parse)(DeviceState *dev, Property *prop, const char *str);
102 int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
103 void (*free)(DeviceState *dev, Property *prop);
106 typedef struct GlobalProperty {
107 const char *driver;
108 const char *property;
109 const char *value;
110 QTAILQ_ENTRY(GlobalProperty) next;
111 } GlobalProperty;
113 /*** Board API. This should go away once we have a machine config file. ***/
115 DeviceState *qdev_create(BusState *bus, const char *name);
116 int qdev_device_help(QemuOpts *opts);
117 DeviceState *qdev_device_add(QemuOpts *opts);
118 int qdev_init(DeviceState *dev) QEMU_WARN_UNUSED_RESULT;
119 void qdev_init_nofail(DeviceState *dev);
120 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
121 int required_for_version);
122 int qdev_unplug(DeviceState *dev);
123 void qdev_free(DeviceState *dev);
124 int qdev_simple_unplug_cb(DeviceState *dev);
125 void qdev_machine_creation_done(void);
127 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
128 void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
130 BlockDriverState *qdev_init_bdrv(DeviceState *dev, BlockInterfaceType type);
132 BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
134 /*** Device API. ***/
136 typedef int (*qdev_initfn)(DeviceState *dev, DeviceInfo *info);
137 typedef int (*qdev_event)(DeviceState *dev);
138 typedef void (*qdev_resetfn)(DeviceState *dev);
140 struct DeviceInfo {
141 const char *name;
142 const char *alias;
143 const char *desc;
144 size_t size;
145 Property *props;
146 int no_user;
148 /* callbacks */
149 qdev_resetfn reset;
151 /* device state */
152 const VMStateDescription *vmsd;
154 /* Private to qdev / bus. */
155 qdev_initfn init;
156 qdev_event unplug;
157 qdev_event exit;
158 BusInfo *bus_info;
159 struct DeviceInfo *next;
161 extern DeviceInfo *device_info_list;
163 void qdev_register(DeviceInfo *info);
165 /* Register device properties. */
166 /* GPIO inputs also double as IRQ sinks. */
167 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
168 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
170 CharDriverState *qdev_init_chardev(DeviceState *dev);
172 BusState *qdev_get_parent_bus(DeviceState *dev);
174 /*** BUS API. ***/
176 void qbus_create_inplace(BusState *bus, BusInfo *info,
177 DeviceState *parent, const char *name);
178 BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name);
179 void qbus_free(BusState *bus);
181 #define FROM_QBUS(type, dev) DO_UPCAST(type, qbus, dev)
183 /*** monitor commands ***/
185 void do_info_qtree(Monitor *mon);
186 void do_info_qdm(Monitor *mon);
187 int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data);
188 int do_device_del(Monitor *mon, const QDict *qdict, QObject **ret_data);
190 /*** qdev-properties.c ***/
192 extern PropertyInfo qdev_prop_bit;
193 extern PropertyInfo qdev_prop_uint8;
194 extern PropertyInfo qdev_prop_uint16;
195 extern PropertyInfo qdev_prop_uint32;
196 extern PropertyInfo qdev_prop_int32;
197 extern PropertyInfo qdev_prop_uint64;
198 extern PropertyInfo qdev_prop_hex32;
199 extern PropertyInfo qdev_prop_hex64;
200 extern PropertyInfo qdev_prop_string;
201 extern PropertyInfo qdev_prop_chr;
202 extern PropertyInfo qdev_prop_ptr;
203 extern PropertyInfo qdev_prop_macaddr;
204 extern PropertyInfo qdev_prop_drive;
205 extern PropertyInfo qdev_prop_netdev;
206 extern PropertyInfo qdev_prop_vlan;
207 extern PropertyInfo qdev_prop_pci_devfn;
209 #define DEFINE_PROP(_name, _state, _field, _prop, _type) { \
210 .name = (_name), \
211 .info = &(_prop), \
212 .offset = offsetof(_state, _field) \
213 + type_check(_type,typeof_field(_state, _field)), \
215 #define DEFINE_PROP_DEFAULT(_name, _state, _field, _defval, _prop, _type) { \
216 .name = (_name), \
217 .info = &(_prop), \
218 .offset = offsetof(_state, _field) \
219 + type_check(_type,typeof_field(_state, _field)), \
220 .defval = (_type[]) { _defval }, \
222 #define DEFINE_PROP_BIT(_name, _state, _field, _bit, _defval) { \
223 .name = (_name), \
224 .info = &(qdev_prop_bit), \
225 .bitnr = (_bit), \
226 .offset = offsetof(_state, _field) \
227 + type_check(uint32_t,typeof_field(_state, _field)), \
228 .defval = (bool[]) { (_defval) }, \
231 #define DEFINE_PROP_UINT8(_n, _s, _f, _d) \
232 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint8, uint8_t)
233 #define DEFINE_PROP_UINT16(_n, _s, _f, _d) \
234 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint16, uint16_t)
235 #define DEFINE_PROP_UINT32(_n, _s, _f, _d) \
236 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint32, uint32_t)
237 #define DEFINE_PROP_INT32(_n, _s, _f, _d) \
238 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_int32, int32_t)
239 #define DEFINE_PROP_UINT64(_n, _s, _f, _d) \
240 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint64, uint64_t)
241 #define DEFINE_PROP_HEX32(_n, _s, _f, _d) \
242 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex32, uint32_t)
243 #define DEFINE_PROP_HEX64(_n, _s, _f, _d) \
244 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex64, uint64_t)
245 #define DEFINE_PROP_PCI_DEVFN(_n, _s, _f, _d) \
246 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_pci_devfn, uint32_t)
248 #define DEFINE_PROP_PTR(_n, _s, _f) \
249 DEFINE_PROP(_n, _s, _f, qdev_prop_ptr, void*)
250 #define DEFINE_PROP_CHR(_n, _s, _f) \
251 DEFINE_PROP(_n, _s, _f, qdev_prop_chr, CharDriverState*)
252 #define DEFINE_PROP_STRING(_n, _s, _f) \
253 DEFINE_PROP(_n, _s, _f, qdev_prop_string, char*)
254 #define DEFINE_PROP_NETDEV(_n, _s, _f) \
255 DEFINE_PROP(_n, _s, _f, qdev_prop_netdev, VLANClientState*)
256 #define DEFINE_PROP_VLAN(_n, _s, _f) \
257 DEFINE_PROP(_n, _s, _f, qdev_prop_vlan, VLANState*)
258 #define DEFINE_PROP_DRIVE(_n, _s, _f) \
259 DEFINE_PROP(_n, _s, _f, qdev_prop_drive, BlockDriverState *)
260 #define DEFINE_PROP_MACADDR(_n, _s, _f) \
261 DEFINE_PROP(_n, _s, _f, qdev_prop_macaddr, MACAddr)
263 #define DEFINE_PROP_END_OF_LIST() \
266 /* Set properties between creation and init. */
267 void *qdev_get_prop_ptr(DeviceState *dev, Property *prop);
268 int qdev_prop_exists(DeviceState *dev, const char *name);
269 int qdev_prop_parse(DeviceState *dev, const char *name, const char *value);
270 void qdev_prop_set(DeviceState *dev, const char *name, void *src, enum PropertyType type);
271 void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value);
272 void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value);
273 void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value);
274 void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value);
275 void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value);
276 void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value);
277 void qdev_prop_set_string(DeviceState *dev, const char *name, char *value);
278 void qdev_prop_set_chr(DeviceState *dev, const char *name, CharDriverState *value);
279 void qdev_prop_set_netdev(DeviceState *dev, const char *name, VLANClientState *value);
280 void qdev_prop_set_vlan(DeviceState *dev, const char *name, VLANState *value);
281 int qdev_prop_set_drive(DeviceState *dev, const char *name, BlockDriverState *value) QEMU_WARN_UNUSED_RESULT;
282 void qdev_prop_set_drive_nofail(DeviceState *dev, const char *name, BlockDriverState *value);
283 void qdev_prop_set_macaddr(DeviceState *dev, const char *name, uint8_t *value);
284 /* FIXME: Remove opaque pointer properties. */
285 void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value);
286 void qdev_prop_set_defaults(DeviceState *dev, Property *props);
288 void qdev_prop_register_global_list(GlobalProperty *props);
289 void qdev_prop_set_globals(DeviceState *dev);
291 /* This is a nasty hack to allow passing a NULL bus to qdev_create. */
292 extern struct BusInfo system_bus_info;
294 #endif