]> git.proxmox.com Git - qemu.git/blame - hw/qdev.h
qdev: rework device properties.
[qemu.git] / hw / qdev.h
CommitLineData
aae9460e
PB
1#ifndef QDEV_H
2#define QDEV_H
3
4#include "hw.h"
02e2da45 5#include "sys-queue.h"
aae9460e 6
ee6847d1
GH
7typedef struct Property Property;
8
9typedef struct PropertyInfo PropertyInfo;
aae9460e 10
ee6847d1 11typedef struct DeviceInfo DeviceInfo;
aae9460e 12
02e2da45 13typedef struct BusState BusState;
4d6ae674 14
10c4c98a
GH
15typedef struct BusInfo BusInfo;
16
aae9460e
PB
17/* This structure should not be accessed directly. We declare it here
18 so that it can be embedded in individual device state structures. */
02e2da45 19struct DeviceState {
042f84d0 20 DeviceInfo *info;
02e2da45 21 BusState *parent_bus;
aae9460e
PB
22 int num_gpio_out;
23 qemu_irq *gpio_out;
24 int num_gpio_in;
25 qemu_irq *gpio_in;
02e2da45 26 LIST_HEAD(, BusState) child_bus;
9d07d757 27 NICInfo *nd;
02e2da45
PB
28 LIST_ENTRY(DeviceState) sibling;
29};
30
10c4c98a
GH
31typedef void (*bus_dev_printfn)(Monitor *mon, DeviceState *dev, int indent);
32struct BusInfo {
33 const char *name;
34 size_t size;
35 bus_dev_printfn print_dev;
ee6847d1 36 Property *props;
10c4c98a 37};
02e2da45
PB
38
39struct BusState {
40 DeviceState *parent;
10c4c98a 41 BusInfo *info;
02e2da45 42 const char *name;
02e2da45
PB
43 LIST_HEAD(, DeviceState) children;
44 LIST_ENTRY(BusState) sibling;
aae9460e
PB
45};
46
ee6847d1
GH
47struct Property {
48 const char *name;
49 PropertyInfo *info;
50 int offset;
51 void *defval;
52};
53
54enum PropertyType {
55 PROP_TYPE_UNSPEC = 0,
56 PROP_TYPE_UINT16,
57 PROP_TYPE_UINT32,
58 PROP_TYPE_TADDR,
59 PROP_TYPE_MACADDR,
60 PROP_TYPE_PTR,
61};
62
63struct PropertyInfo {
64 const char *name;
65 size_t size;
66 enum PropertyType type;
67 int (*parse)(DeviceState *dev, Property *prop, const char *str);
68 int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
69};
70
aae9460e
PB
71/*** Board API. This should go away once we have a machine config file. ***/
72
02e2da45 73DeviceState *qdev_create(BusState *bus, const char *name);
aae9460e 74void qdev_init(DeviceState *dev);
02e2da45 75void qdev_free(DeviceState *dev);
aae9460e 76
aae9460e
PB
77qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
78void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
79
02e2da45 80BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
4d6ae674 81
aae9460e
PB
82/*** Device API. ***/
83
02e2da45 84typedef void (*qdev_initfn)(DeviceState *dev, DeviceInfo *info);
6f68ecb2
PB
85typedef void (*SCSIAttachFn)(DeviceState *host, BlockDriverState *bdrv,
86 int unit);
aae9460e 87
02e2da45 88struct DeviceInfo {
074f2fff
GH
89 const char *name;
90 size_t size;
ee6847d1 91 Property *props;
074f2fff
GH
92
93 /* Private to qdev / bus. */
02e2da45 94 qdev_initfn init;
10c4c98a 95 BusInfo *bus_info;
042f84d0 96 struct DeviceInfo *next;
02e2da45
PB
97};
98
074f2fff 99void qdev_register(DeviceInfo *info);
aae9460e
PB
100
101/* Register device properties. */
067a3ddc 102/* GPIO inputs also double as IRQ sinks. */
aae9460e
PB
103void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
104void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
105
6f68ecb2
PB
106void scsi_bus_new(DeviceState *host, SCSIAttachFn attach);
107
aae9460e
PB
108CharDriverState *qdev_init_chardev(DeviceState *dev);
109
02e2da45 110BusState *qdev_get_parent_bus(DeviceState *dev);
aae9460e
PB
111
112/* Convery from a base type to a parent type, with compile time checking. */
113#ifdef __GNUC__
114#define DO_UPCAST(type, field, dev) ( __extension__ ( { \
115 char __attribute__((unused)) offset_must_be_zero[ \
116 -offsetof(type, field)]; \
117 container_of(dev, type, field);}))
118#else
119#define DO_UPCAST(type, field, dev) container_of(dev, type, field)
120#endif
121
02e2da45
PB
122/*** BUS API. ***/
123
10c4c98a 124BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name);
02e2da45
PB
125
126#define FROM_QBUS(type, dev) DO_UPCAST(type, qbus, dev)
127
cae4956e
GH
128/*** monitor commands ***/
129
130void do_info_qtree(Monitor *mon);
cae4956e 131
ee6847d1
GH
132/*** qdev-properties.c ***/
133
134extern PropertyInfo qdev_prop_uint16;
135extern PropertyInfo qdev_prop_uint32;
136extern PropertyInfo qdev_prop_hex32;
137extern PropertyInfo qdev_prop_ptr;
138extern PropertyInfo qdev_prop_macaddr;
139
140/* Set properties between creation and init. */
141void *qdev_get_prop_ptr(DeviceState *dev, Property *prop);
142int qdev_prop_parse(DeviceState *dev, const char *name, const char *value);
143void qdev_prop_set(DeviceState *dev, const char *name, void *src, enum PropertyType type);
144void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value);
145void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value);
146/* FIXME: Remove opaque pointer properties. */
147void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value);
148void qdev_prop_set_defaults(DeviceState *dev, Property *props);
149
aae9460e 150#endif