5 #include "qemu-queue.h"
7 #include "qemu-option.h"
8 #include "qapi/qapi-visit-core.h"
10 typedef struct Property Property;
12 typedef struct PropertyInfo PropertyInfo;
14 typedef struct CompatProperty CompatProperty;
16 typedef struct DeviceInfo DeviceInfo;
18 typedef struct BusState BusState;
20 typedef struct BusInfo BusInfo;
23 DEV_STATE_CREATED = 1,
24 DEV_STATE_INITIALIZED,
28 DEV_NVECTORS_UNSPECIFIED = -1,
32 * @DevicePropertyAccessor - called when trying to get/set a property
34 * @dev the device that owns the property
35 * @v the visitor that contains the property data
36 * @opaque the device property opaque
37 * @name the name of the property
38 * @errp a pointer to an Error that is filled if getting/setting fails.
40 typedef void (DevicePropertyAccessor)(DeviceState *dev,
47 * @DevicePropertyRelease - called when a property is removed from a device
49 * @dev the device that owns the property
50 * @name the name of the property
51 * @opaque the opaque registered with the property
53 typedef void (DevicePropertyRelease)(DeviceState *dev,
57 typedef struct DeviceProperty
61 DevicePropertyAccessor *get;
62 DevicePropertyAccessor *set;
63 DevicePropertyRelease *release;
66 QTAILQ_ENTRY(DeviceProperty) node;
69 /* This structure should not be accessed directly. We declare it here
70 so that it can be embedded in individual device state structures. */
82 QLIST_HEAD(, BusState) child_bus;
84 QTAILQ_ENTRY(DeviceState) sibling;
85 int instance_id_alias;
86 int alias_required_for_version;
89 * This tracks the number of references between devices. See @qdev_ref for
94 QTAILQ_HEAD(, DeviceProperty) properties;
97 typedef void (*bus_dev_printfn)(Monitor *mon, DeviceState *dev, int indent);
98 typedef char *(*bus_get_dev_path)(DeviceState *dev);
100 * This callback is used to create Open Firmware device path in accordance with
101 * OF spec http://forthworks.com/standards/of1275.pdf. Indicidual bus bindings
102 * can be found here http://playground.sun.com/1275/bindings/.
104 typedef char *(*bus_get_fw_dev_path)(DeviceState *dev);
105 typedef int (qbus_resetfn)(BusState *bus);
110 bus_dev_printfn print_dev;
111 bus_get_dev_path get_dev_path;
112 bus_get_fw_dev_path get_fw_dev_path;
123 QTAILQ_HEAD(ChildrenHead, DeviceState) children;
124 QLIST_ENTRY(BusState) sibling;
136 PROP_TYPE_UNSPEC = 0,
153 struct PropertyInfo {
156 enum PropertyType type;
157 int (*parse)(DeviceState *dev, Property *prop, const char *str);
158 int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
159 void (*free)(DeviceState *dev, Property *prop);
162 typedef struct GlobalProperty {
164 const char *property;
166 QTAILQ_ENTRY(GlobalProperty) next;
169 /*** Board API. This should go away once we have a machine config file. ***/
171 DeviceState *qdev_create(BusState *bus, const char *name);
172 DeviceState *qdev_try_create(BusState *bus, const char *name);
173 int qdev_device_help(QemuOpts *opts);
174 DeviceState *qdev_device_add(QemuOpts *opts);
175 int qdev_init(DeviceState *dev) QEMU_WARN_UNUSED_RESULT;
176 void qdev_init_nofail(DeviceState *dev);
177 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
178 int required_for_version);
179 int qdev_unplug(DeviceState *dev);
180 void qdev_free(DeviceState *dev);
181 int qdev_simple_unplug_cb(DeviceState *dev);
182 void qdev_machine_creation_done(void);
183 bool qdev_machine_modified(void);
185 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
186 void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
188 BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
190 /*** Device API. ***/
192 typedef int (*qdev_initfn)(DeviceState *dev, DeviceInfo *info);
193 typedef int (*qdev_event)(DeviceState *dev);
194 typedef void (*qdev_resetfn)(DeviceState *dev);
209 const VMStateDescription *vmsd;
211 /* Private to qdev / bus. */
216 struct DeviceInfo *next;
218 extern DeviceInfo *device_info_list;
220 void qdev_register(DeviceInfo *info);
222 /* Register device properties. */
223 /* GPIO inputs also double as IRQ sinks. */
224 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
225 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
227 CharDriverState *qdev_init_chardev(DeviceState *dev);
229 BusState *qdev_get_parent_bus(DeviceState *dev);
233 DeviceState *qdev_find_recursive(BusState *bus, const char *id);
235 /* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
236 typedef int (qbus_walkerfn)(BusState *bus, void *opaque);
237 typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque);
239 void qbus_create_inplace(BusState *bus, BusInfo *info,
240 DeviceState *parent, const char *name);
241 BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name);
242 /* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
243 * < 0 if either devfn or busfn terminate walk somewhere in cursion,
245 int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
246 qbus_walkerfn *busfn, void *opaque);
247 int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
248 qbus_walkerfn *busfn, void *opaque);
249 void qdev_reset_all(DeviceState *dev);
250 void qbus_reset_all_fn(void *opaque);
252 void qbus_free(BusState *bus);
254 #define FROM_QBUS(type, dev) DO_UPCAST(type, qbus, dev)
256 /* This should go away once we get rid of the NULL bus hack */
257 BusState *sysbus_get_default(void);
259 /*** monitor commands ***/
261 void do_info_qtree(Monitor *mon);
262 void do_info_qdm(Monitor *mon);
263 int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data);
264 int do_device_del(Monitor *mon, const QDict *qdict, QObject **ret_data);
266 /*** qdev-properties.c ***/
268 extern PropertyInfo qdev_prop_bit;
269 extern PropertyInfo qdev_prop_uint8;
270 extern PropertyInfo qdev_prop_uint16;
271 extern PropertyInfo qdev_prop_uint32;
272 extern PropertyInfo qdev_prop_int32;
273 extern PropertyInfo qdev_prop_uint64;
274 extern PropertyInfo qdev_prop_hex8;
275 extern PropertyInfo qdev_prop_hex32;
276 extern PropertyInfo qdev_prop_hex64;
277 extern PropertyInfo qdev_prop_string;
278 extern PropertyInfo qdev_prop_chr;
279 extern PropertyInfo qdev_prop_ptr;
280 extern PropertyInfo qdev_prop_macaddr;
281 extern PropertyInfo qdev_prop_drive;
282 extern PropertyInfo qdev_prop_netdev;
283 extern PropertyInfo qdev_prop_vlan;
284 extern PropertyInfo qdev_prop_pci_devfn;
286 #define DEFINE_PROP(_name, _state, _field, _prop, _type) { \
289 .offset = offsetof(_state, _field) \
290 + type_check(_type,typeof_field(_state, _field)), \
292 #define DEFINE_PROP_DEFAULT(_name, _state, _field, _defval, _prop, _type) { \
295 .offset = offsetof(_state, _field) \
296 + type_check(_type,typeof_field(_state, _field)), \
297 .defval = (_type[]) { _defval }, \
299 #define DEFINE_PROP_BIT(_name, _state, _field, _bit, _defval) { \
301 .info = &(qdev_prop_bit), \
303 .offset = offsetof(_state, _field) \
304 + type_check(uint32_t,typeof_field(_state, _field)), \
305 .defval = (bool[]) { (_defval) }, \
308 #define DEFINE_PROP_UINT8(_n, _s, _f, _d) \
309 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint8, uint8_t)
310 #define DEFINE_PROP_UINT16(_n, _s, _f, _d) \
311 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint16, uint16_t)
312 #define DEFINE_PROP_UINT32(_n, _s, _f, _d) \
313 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint32, uint32_t)
314 #define DEFINE_PROP_INT32(_n, _s, _f, _d) \
315 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_int32, int32_t)
316 #define DEFINE_PROP_UINT64(_n, _s, _f, _d) \
317 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint64, uint64_t)
318 #define DEFINE_PROP_HEX8(_n, _s, _f, _d) \
319 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex8, uint8_t)
320 #define DEFINE_PROP_HEX32(_n, _s, _f, _d) \
321 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex32, uint32_t)
322 #define DEFINE_PROP_HEX64(_n, _s, _f, _d) \
323 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex64, uint64_t)
324 #define DEFINE_PROP_PCI_DEVFN(_n, _s, _f, _d) \
325 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_pci_devfn, uint32_t)
327 #define DEFINE_PROP_PTR(_n, _s, _f) \
328 DEFINE_PROP(_n, _s, _f, qdev_prop_ptr, void*)
329 #define DEFINE_PROP_CHR(_n, _s, _f) \
330 DEFINE_PROP(_n, _s, _f, qdev_prop_chr, CharDriverState*)
331 #define DEFINE_PROP_STRING(_n, _s, _f) \
332 DEFINE_PROP(_n, _s, _f, qdev_prop_string, char*)
333 #define DEFINE_PROP_NETDEV(_n, _s, _f) \
334 DEFINE_PROP(_n, _s, _f, qdev_prop_netdev, VLANClientState*)
335 #define DEFINE_PROP_VLAN(_n, _s, _f) \
336 DEFINE_PROP(_n, _s, _f, qdev_prop_vlan, VLANState*)
337 #define DEFINE_PROP_DRIVE(_n, _s, _f) \
338 DEFINE_PROP(_n, _s, _f, qdev_prop_drive, BlockDriverState *)
339 #define DEFINE_PROP_MACADDR(_n, _s, _f) \
340 DEFINE_PROP(_n, _s, _f, qdev_prop_macaddr, MACAddr)
342 #define DEFINE_PROP_END_OF_LIST() \
345 /* Set properties between creation and init. */
346 void *qdev_get_prop_ptr(DeviceState *dev, Property *prop);
347 int qdev_prop_exists(DeviceState *dev, const char *name);
348 int qdev_prop_parse(DeviceState *dev, const char *name, const char *value);
349 void qdev_prop_set(DeviceState *dev, const char *name, void *src, enum PropertyType type);
350 void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value);
351 void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value);
352 void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value);
353 void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value);
354 void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value);
355 void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value);
356 void qdev_prop_set_string(DeviceState *dev, const char *name, char *value);
357 void qdev_prop_set_chr(DeviceState *dev, const char *name, CharDriverState *value);
358 void qdev_prop_set_netdev(DeviceState *dev, const char *name, VLANClientState *value);
359 void qdev_prop_set_vlan(DeviceState *dev, const char *name, VLANState *value);
360 int qdev_prop_set_drive(DeviceState *dev, const char *name, BlockDriverState *value) QEMU_WARN_UNUSED_RESULT;
361 void qdev_prop_set_drive_nofail(DeviceState *dev, const char *name, BlockDriverState *value);
362 void qdev_prop_set_macaddr(DeviceState *dev, const char *name, uint8_t *value);
363 /* FIXME: Remove opaque pointer properties. */
364 void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value);
365 void qdev_prop_set_defaults(DeviceState *dev, Property *props);
367 void qdev_prop_register_global_list(GlobalProperty *props);
368 void qdev_prop_set_globals(DeviceState *dev);
370 static inline const char *qdev_fw_name(DeviceState *dev)
372 return dev->info->fw_name ? : dev->info->alias ? : dev->info->name;
375 char *qdev_get_fw_dev_path(DeviceState *dev);
376 /* This is a nasty hack to allow passing a NULL bus to qdev_create. */
377 extern struct BusInfo system_bus_info;
382 * Increase the reference count of a device. A device cannot be freed as long
383 * as its reference count is greater than zero.
387 void qdev_ref(DeviceState *dev);
392 * Decrease the reference count of a device. A device cannot be freed as long
393 * as its reference count is greater than zero.
397 void qdev_unref(DeviceState *dev);
400 * @qdev_property_add - add a new property to a device
402 * @dev - the device to add a property to
404 * @name - the name of the property. This can contain any character except for
405 * a forward slash. In general, you should use hyphens '-' instead of
406 * underscores '_' when naming properties.
408 * @type - the type name of the property. This namespace is pretty loosely
409 * defined. Sub namespaces are constructed by using a prefix and then
410 * to angle brackets. For instance, the type 'virtio-net-pci' in the
411 * 'link' namespace would be 'link<virtio-net-pci>'.
413 * @get - the getter to be called to read a property. If this is NULL, then
414 * the property cannot be read.
416 * @set - the setter to be called to write a property. If this is NULL,
417 * then the property cannot be written.
419 * @release - called when the property is removed from the device. This is
420 * meant to allow a property to free its opaque upon device
421 * destruction. This may be NULL.
423 * @opaque - an opaque pointer to pass to the callbacks for the property
425 * @errp - returns an error if this function fails
427 void qdev_property_add(DeviceState *dev, const char *name, const char *type,
428 DevicePropertyAccessor *get, DevicePropertyAccessor *set,
429 DevicePropertyRelease *release,
430 void *opaque, Error **errp);
433 * @qdev_property_get - reads a property from a device
437 * @v - the visitor that will receive the property value. This should be an
438 * Output visitor and the data will be written with @name as the name.
440 * @name - the name of the property
442 * @errp - returns an error if this function fails
444 void qdev_property_get(DeviceState *dev, Visitor *v, const char *name,
448 * @qdev_property_set - writes a property to a device
452 * @v - the visitor that will be used to write the property value. This should
453 * be an Input visitor and the data will be first read with @name as the
454 * name and then written as the property value.
456 * @name - the name of the property
458 * @errp - returns an error if this function fails
460 void qdev_property_set(DeviceState *dev, Visitor *v, const char *name,
464 * @qdev_property_get_type - returns the type of a property
468 * @name - the name of the property
470 * @errp - returns an error if this function fails
473 * The type name of the property.
475 const char *qdev_property_get_type(DeviceState *dev, const char *name,
479 * @qdev_property_add_legacy - add a legacy @Property to a device
481 * DO NOT USE THIS IN NEW CODE!
483 void qdev_property_add_legacy(DeviceState *dev, Property *prop, Error **errp);