1#ifndef QDEV_CORE_H
2#define QDEV_CORE_H
3
4#include "qemu/queue.h"
5#include "qemu/option.h"
6#include "qemu/typedefs.h"
7#include "qemu/bitmap.h"
8#include "qom/object.h"
9#include "hw/irq.h"
10#include "qapi/error.h"
11
12enum {
13 DEV_NVECTORS_UNSPECIFIED = -1,
14};
15
16#define TYPE_DEVICE "device"
17#define DEVICE(obj) OBJECT_CHECK(DeviceState, (obj), TYPE_DEVICE)
18#define DEVICE_CLASS(klass) OBJECT_CLASS_CHECK(DeviceClass, (klass), TYPE_DEVICE)
19#define DEVICE_GET_CLASS(obj) OBJECT_GET_CLASS(DeviceClass, (obj), TYPE_DEVICE)
20
21typedef enum DeviceCategory {
22 DEVICE_CATEGORY_BRIDGE,
23 DEVICE_CATEGORY_USB,
24 DEVICE_CATEGORY_STORAGE,
25 DEVICE_CATEGORY_NETWORK,
26 DEVICE_CATEGORY_INPUT,
27 DEVICE_CATEGORY_DISPLAY,
28 DEVICE_CATEGORY_SOUND,
29 DEVICE_CATEGORY_MISC,
30 DEVICE_CATEGORY_MAX
31} DeviceCategory;
32
33static inline const char *qdev_category_get_name(DeviceCategory category)
34{
35 static const char *category_names[DEVICE_CATEGORY_MAX] = {
36 [DEVICE_CATEGORY_BRIDGE] = "Controller/Bridge/Hub",
37 [DEVICE_CATEGORY_USB] = "USB",
38 [DEVICE_CATEGORY_STORAGE] = "Storage",
39 [DEVICE_CATEGORY_NETWORK] = "Network",
40 [DEVICE_CATEGORY_INPUT] = "Input",
41 [DEVICE_CATEGORY_DISPLAY] = "Display",
42 [DEVICE_CATEGORY_SOUND] = "Sound",
43 [DEVICE_CATEGORY_MISC] = "Misc",
44 };
45
46 return category_names[category];
47};
48
49typedef int (*qdev_initfn)(DeviceState *dev);
50typedef int (*qdev_event)(DeviceState *dev);
51typedef void (*qdev_resetfn)(DeviceState *dev);
52typedef void (*DeviceRealize)(DeviceState *dev, Error **errp);
53typedef void (*DeviceUnrealize)(DeviceState *dev, Error **errp);
54
55struct VMStateDescription;
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107typedef struct DeviceClass {
108
109 ObjectClass parent_class;
110
111
112 DECLARE_BITMAP(categories, DEVICE_CATEGORY_MAX);
113 const char *fw_name;
114 const char *desc;
115 Property *props;
116 int no_user;
117
118
119 void (*reset)(DeviceState *dev);
120 DeviceRealize realize;
121 DeviceUnrealize unrealize;
122
123
124 const struct VMStateDescription *vmsd;
125
126
127 qdev_initfn init;
128 qdev_event unplug;
129 qdev_event exit;
130 const char *bus_type;
131} DeviceClass;
132
133
134
135
136
137
138
139
140struct DeviceState {
141
142 Object parent_obj;
143
144
145 const char *id;
146 bool realized;
147 QemuOpts *opts;
148 int hotplugged;
149 BusState *parent_bus;
150 int num_gpio_out;
151 qemu_irq *gpio_out;
152 int num_gpio_in;
153 qemu_irq *gpio_in;
154 QLIST_HEAD(, BusState) child_bus;
155 int num_child_bus;
156 int instance_id_alias;
157 int alias_required_for_version;
158};
159
160#define TYPE_BUS "bus"
161#define BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_BUS)
162#define BUS_CLASS(klass) OBJECT_CLASS_CHECK(BusClass, (klass), TYPE_BUS)
163#define BUS_GET_CLASS(obj) OBJECT_GET_CLASS(BusClass, (obj), TYPE_BUS)
164
165struct BusClass {
166 ObjectClass parent_class;
167
168
169 void (*print_dev)(Monitor *mon, DeviceState *dev, int indent);
170 char *(*get_dev_path)(DeviceState *dev);
171
172
173
174
175
176 char *(*get_fw_dev_path)(DeviceState *dev);
177 int (*reset)(BusState *bus);
178
179 int max_dev;
180};
181
182typedef struct BusChild {
183 DeviceState *child;
184 int index;
185 QTAILQ_ENTRY(BusChild) sibling;
186} BusChild;
187
188
189
190
191struct BusState {
192 Object obj;
193 DeviceState *parent;
194 const char *name;
195 int allow_hotplug;
196 int max_index;
197 QTAILQ_HEAD(ChildrenHead, BusChild) children;
198 QLIST_ENTRY(BusState) sibling;
199};
200
201struct Property {
202 const char *name;
203 PropertyInfo *info;
204 int offset;
205 uint8_t bitnr;
206 uint8_t qtype;
207 int64_t defval;
208 int arrayoffset;
209 PropertyInfo *arrayinfo;
210 int arrayfieldsize;
211};
212
213struct PropertyInfo {
214 const char *name;
215 const char *legacy_name;
216 const char **enum_table;
217 int (*parse)(DeviceState *dev, Property *prop, const char *str);
218 int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
219 ObjectPropertyAccessor *get;
220 ObjectPropertyAccessor *set;
221 ObjectPropertyRelease *release;
222};
223
224typedef struct GlobalProperty {
225 const char *driver;
226 const char *property;
227 const char *value;
228 QTAILQ_ENTRY(GlobalProperty) next;
229} GlobalProperty;
230
231
232
233DeviceState *qdev_create(BusState *bus, const char *name);
234DeviceState *qdev_try_create(BusState *bus, const char *name);
235int qdev_init(DeviceState *dev) QEMU_WARN_UNUSED_RESULT;
236void qdev_init_nofail(DeviceState *dev);
237void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
238 int required_for_version);
239void qdev_unplug(DeviceState *dev, Error **errp);
240void qdev_free(DeviceState *dev);
241int qdev_simple_unplug_cb(DeviceState *dev);
242void qdev_machine_creation_done(void);
243bool qdev_machine_modified(void);
244
245qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
246void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
247
248BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
249
250
251
252
253
254void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
255void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
256
257BusState *qdev_get_parent_bus(DeviceState *dev);
258
259
260
261DeviceState *qdev_find_recursive(BusState *bus, const char *id);
262
263
264typedef int (qbus_walkerfn)(BusState *bus, void *opaque);
265typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque);
266
267void qbus_create_inplace(void *bus, const char *typename,
268 DeviceState *parent, const char *name);
269BusState *qbus_create(const char *typename, DeviceState *parent, const char *name);
270
271
272
273int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
274 qbus_walkerfn *busfn, void *opaque);
275int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
276 qbus_walkerfn *busfn, void *opaque);
277void qdev_reset_all(DeviceState *dev);
278
279
280
281
282
283
284
285
286
287
288
289void qbus_reset_all(BusState *bus);
290void qbus_reset_all_fn(void *opaque);
291
292void qbus_free(BusState *bus);
293
294
295BusState *sysbus_get_default(void);
296
297char *qdev_get_fw_dev_path(DeviceState *dev);
298
299
300
301
302
303
304
305void qdev_machine_init(void);
306
307
308
309
310
311
312void device_reset(DeviceState *dev);
313
314const struct VMStateDescription *qdev_get_vmsd(DeviceState *dev);
315
316const char *qdev_fw_name(DeviceState *dev);
317
318Object *qdev_get_machine(void);
319
320
321void qdev_set_parent_bus(DeviceState *dev, BusState *bus);
322
323extern int qdev_hotplug;
324
325char *qdev_get_dev_path(DeviceState *dev);
326
327#endif
328