TWL92230 qdev conversion
[qemu] / hw / qdev.c
1 /*
2  *  Dynamic device configuration and creation.
3  *
4  *  Copyright (c) 2009 CodeSourcery
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
19  */
20
21 /* The theory here is that it should be possible to create a machine without
22    knowledge of specific devices.  Historically board init routines have
23    passed a bunch of arguments to each device, requiring the board know
24    exactly which device it is dealing with.  This file provides an abstract
25    API for device configuration and initialization.  Devices will generally
26    inherit from a particular bus (e.g. PCI or I2C) rather than
27    this API directly.  */
28
29 #include "net.h"
30 #include "qdev.h"
31 #include "sysemu.h"
32
33 struct DeviceProperty {
34     const char *name;
35     union {
36         int i;
37         void *ptr;
38     } value;
39     DeviceProperty *next;
40 };
41
42 struct DeviceType {
43     const char *name;
44     qdev_initfn init;
45     void *opaque;
46     int size;
47     DeviceType *next;
48 };
49
50 struct ChildBusList {
51     const char *name;
52     void *ptr;
53     ChildBusList *next;
54 };
55
56 static DeviceType *device_type_list;
57
58 /* Register a new device type.  */
59 DeviceType *qdev_register(const char *name, int size, qdev_initfn init,
60                           void *opaque)
61 {
62     DeviceType *t;
63
64     assert(size >= sizeof(DeviceState));
65
66     t = qemu_mallocz(sizeof(DeviceType));
67     t->next = device_type_list;
68     device_type_list = t;
69     t->name = qemu_strdup(name);
70     t->size = size;
71     t->init = init;
72     t->opaque = opaque;
73
74     return t;
75 }
76
77 /* Create a new device.  This only initializes the device state structure
78    and allows properties to be set.  qdev_init should be called to
79    initialize the actual device emulation.  */
80 DeviceState *qdev_create(void *bus, const char *name)
81 {
82     DeviceType *t;
83     DeviceState *dev;
84
85     for (t = device_type_list; t; t = t->next) {
86         if (strcmp(t->name, name) == 0) {
87             break;
88         }
89     }
90     if (!t) {
91         fprintf(stderr, "Unknown device '%s'\n", name);
92         exit(1);
93     }
94
95     dev = qemu_mallocz(t->size);
96     dev->name = name;
97     dev->type = t;
98     dev->bus = bus;
99     return dev;
100 }
101
102 /* Initialize a device.  Device properties should be set before calling
103    this function.  IRQs and MMIO regions should be connected/mapped after
104    calling this function.  */
105 void qdev_init(DeviceState *dev)
106 {
107     dev->type->init(dev, dev->type->opaque);
108 }
109
110 static DeviceProperty *create_prop(DeviceState *dev, const char *name)
111 {
112     DeviceProperty *prop;
113
114     /* TODO: Check for duplicate properties.  */
115     prop = qemu_mallocz(sizeof(*prop));
116     prop->name = qemu_strdup(name);
117     prop->next = dev->props;
118     dev->props = prop;
119
120     return prop;
121 }
122
123 void qdev_set_prop_int(DeviceState *dev, const char *name, int value)
124 {
125     DeviceProperty *prop;
126
127     prop = create_prop(dev, name);
128     prop->value.i = value;
129 }
130
131 void qdev_set_prop_ptr(DeviceState *dev, const char *name, void *value)
132 {
133     DeviceProperty *prop;
134
135     prop = create_prop(dev, name);
136     prop->value.ptr = value;
137 }
138
139 void qdev_set_netdev(DeviceState *dev, NICInfo *nd)
140 {
141     assert(!dev->nd);
142     dev->nd = nd;
143 }
144
145
146 qemu_irq qdev_get_irq_sink(DeviceState *dev, int n)
147 {
148     assert(n >= 0 && n < dev->num_irq_sink);
149     return dev->irq_sink[n];
150 }
151
152 /* Register device IRQ sinks.  */
153 void qdev_init_irq_sink(DeviceState *dev, qemu_irq_handler handler, int nirq)
154 {
155     dev->num_irq_sink = nirq;
156     dev->irq_sink = qemu_allocate_irqs(handler, dev, nirq);
157 }
158
159 /* Get a character (serial) device interface.  */
160 CharDriverState *qdev_init_chardev(DeviceState *dev)
161 {
162     static int next_serial;
163     static int next_virtconsole;
164     /* FIXME: This is a nasty hack that needs to go away.  */
165     if (strncmp(dev->name, "virtio", 6) == 0) {
166         return virtcon_hds[next_virtconsole++];
167     } else {
168         return serial_hds[next_serial++];
169     }
170 }
171
172 void *qdev_get_bus(DeviceState *dev)
173 {
174     return dev->bus;
175 }
176
177 static DeviceProperty *find_prop(DeviceState *dev, const char *name)
178 {
179     DeviceProperty *prop;
180
181     for (prop = dev->props; prop; prop = prop->next) {
182         if (strcmp(prop->name, name) == 0) {
183             return prop;
184         }
185     }
186     return NULL;
187 }
188
189 uint64_t qdev_get_prop_int(DeviceState *dev, const char *name, uint64_t def)
190 {
191     DeviceProperty *prop;
192
193     prop = find_prop(dev, name);
194     if (!prop)
195         return def;
196
197     return prop->value.i;
198 }
199
200 void *qdev_get_prop_ptr(DeviceState *dev, const char *name)
201 {
202     DeviceProperty *prop;
203
204     prop = find_prop(dev, name);
205     assert(prop);
206     return prop->value.ptr;
207 }
208
209 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
210 {
211     assert(dev->num_gpio_in == 0);
212     dev->num_gpio_in = n;
213     dev->gpio_in = qemu_allocate_irqs(handler, dev, n);
214 }
215
216 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
217 {
218     assert(dev->num_gpio_out == 0);
219     dev->num_gpio_out = n;
220     dev->gpio_out = pins;
221 }
222
223 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
224 {
225     assert(n >= 0 && n < dev->num_gpio_in);
226     return dev->gpio_in[n];
227 }
228
229 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
230 {
231     assert(n >= 0 && n < dev->num_gpio_out);
232     dev->gpio_out[n] = pin;
233 }
234
235 VLANClientState *qdev_get_vlan_client(DeviceState *dev,
236                                       IOReadHandler *fd_read,
237                                       IOCanRWHandler *fd_can_read,
238                                       NetCleanup *cleanup,
239                                       void *opaque)
240 {
241     NICInfo *nd = dev->nd;
242     assert(nd);
243     return qemu_new_vlan_client(nd->vlan, nd->model, nd->name,
244                                 fd_read, fd_can_read, cleanup, opaque);
245 }
246
247
248 void qdev_get_macaddr(DeviceState *dev, uint8_t *macaddr)
249 {
250     memcpy(macaddr, dev->nd->macaddr, 6);
251 }
252
253 static int next_block_unit[IF_COUNT];
254
255 /* Get a block device.  This should only be used for single-drive devices
256    (e.g. SD/Floppy/MTD).  Multi-disk devices (scsi/ide) should use the
257    appropriate bus.  */
258 BlockDriverState *qdev_init_bdrv(DeviceState *dev, BlockInterfaceType type)
259 {
260     int unit = next_block_unit[type]++;
261     int index;
262
263     index = drive_get_index(type, 0, unit);
264     if (index == -1) {
265         return NULL;
266     }
267     return drives_table[index].bdrv;
268 }
269
270 void *qdev_get_child_bus(DeviceState *dev, const char *name)
271 {
272     ChildBusList *bus;
273
274     for (bus = dev->child_bus; bus; bus = bus->next) {
275         if (strcmp(name, bus->name) == 0) {
276             return bus->ptr;
277         }
278     }
279     return NULL;
280 }
281
282 void qdev_attach_child_bus(DeviceState *dev, const char *name, void *bus)
283 {
284     ChildBusList *p;
285
286     assert(!qdev_get_child_bus(dev, name));
287     p = qemu_mallocz(sizeof(*p));
288     p->name = qemu_strdup(name);
289     p->ptr = bus;
290     p->next = dev->child_bus;
291     dev->child_bus = p;
292 }
293
294 static int next_scsi_bus;
295
296 /* Create a scsi bus, and attach devices to it.  */
297 /* TODO: Actually create a scsi bus for hotplug to use.  */
298 void scsi_bus_new(DeviceState *host, SCSIAttachFn attach)
299 {
300    int bus = next_scsi_bus++;
301    int unit;
302    int index;
303
304    for (unit = 0; unit < MAX_SCSI_DEVS; unit++) {
305        index = drive_get_index(IF_SCSI, bus, unit);
306        if (index == -1) {
307            continue;
308        }
309        attach(host, drives_table[index].bdrv, unit);
310    }
311 }