0931bc19d64326a75f5e1056b05b53a907b87c81
[qemu] / hw / qdev.h
1 #ifndef QDEV_H
2 #define QDEV_H
3
4 #include "hw.h"
5 #include "sys-queue.h"
6
7 typedef struct DeviceType DeviceType;
8
9 typedef struct DeviceProperty DeviceProperty;
10
11 typedef struct BusState BusState;
12
13 /* This structure should not be accessed directly.  We declare it here
14    so that it can be embedded in individual device state structures.  */
15 struct DeviceState {
16     const char *name;
17     DeviceType *type;
18     BusState *parent_bus;
19     DeviceProperty *props;
20     int num_irq_sink;
21     qemu_irq *irq_sink;
22     int num_gpio_out;
23     qemu_irq *gpio_out;
24     int num_gpio_in;
25     qemu_irq *gpio_in;
26     LIST_HEAD(, BusState) child_bus;
27     NICInfo *nd;
28     LIST_ENTRY(DeviceState) sibling;
29 };
30
31 typedef enum {
32     BUS_TYPE_SYSTEM,
33     BUS_TYPE_PCI,
34     BUS_TYPE_SCSI,
35     BUS_TYPE_I2C,
36     BUS_TYPE_SSI
37 } BusType;
38
39 struct BusState {
40     DeviceState *parent;
41     const char *name;
42     BusType type;
43     LIST_HEAD(, DeviceState) children;
44     LIST_ENTRY(BusState) sibling;
45 };
46
47 /*** Board API.  This should go away once we have a machine config file.  ***/
48
49 DeviceState *qdev_create(BusState *bus, const char *name);
50 void qdev_init(DeviceState *dev);
51 void qdev_free(DeviceState *dev);
52
53 /* Set properties between creation and init.  */
54 void qdev_set_prop_int(DeviceState *dev, const char *name, uint64_t value);
55 void qdev_set_prop_ptr(DeviceState *dev, const char *name, void *value);
56 void qdev_set_netdev(DeviceState *dev, NICInfo *nd);
57
58 qemu_irq qdev_get_irq_sink(DeviceState *dev, int n);
59 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
60 void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
61
62 BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
63
64 /*** Device API.  ***/
65
66 typedef struct DeviceInfo DeviceInfo;
67
68 typedef void (*qdev_initfn)(DeviceState *dev, DeviceInfo *info);
69 typedef void (*SCSIAttachFn)(DeviceState *host, BlockDriverState *bdrv,
70               int unit);
71
72 struct DeviceInfo {
73     qdev_initfn init;
74     BusType bus_type;
75 };
76
77 void qdev_register(const char *name, int size, DeviceInfo *info);
78
79 /* Register device properties.  */
80 void qdev_init_irq_sink(DeviceState *dev, qemu_irq_handler handler, int nirq);
81 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
82 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
83
84 void scsi_bus_new(DeviceState *host, SCSIAttachFn attach);
85
86 CharDriverState *qdev_init_chardev(DeviceState *dev);
87
88 BusState *qdev_get_parent_bus(DeviceState *dev);
89 uint64_t qdev_get_prop_int(DeviceState *dev, const char *name, uint64_t def);
90 void *qdev_get_prop_ptr(DeviceState *dev, const char *name);
91
92 /* Convery from a base type to a parent type, with compile time checking.  */
93 #ifdef __GNUC__
94 #define DO_UPCAST(type, field, dev) ( __extension__ ( { \
95     char __attribute__((unused)) offset_must_be_zero[ \
96         -offsetof(type, field)]; \
97     container_of(dev, type, field);}))
98 #else
99 #define DO_UPCAST(type, field, dev) container_of(dev, type, field)
100 #endif
101
102 /*** BUS API. ***/
103
104 BusState *qbus_create(BusType type, size_t size,
105                       DeviceState *parent, const char *name);
106
107 #define FROM_QBUS(type, dev) DO_UPCAST(type, qbus, dev)
108
109 #endif