Add dummy command to submakefiles
[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         uint64_t i;
37         void *ptr;
38     } value;
39     DeviceProperty *next;
40 };
41
42 struct DeviceType {
43     const char *name;
44     DeviceInfo *info;
45     int size;
46     DeviceType *next;
47 };
48
49 /* This is a nasty hack to allow passing a NULL bus to qdev_create.  */
50 BusState *main_system_bus;
51
52 static DeviceType *device_type_list;
53
54 /* Register a new device type.  */
55 void qdev_register(const char *name, int size, DeviceInfo *info)
56 {
57     DeviceType *t;
58
59     assert(size >= sizeof(DeviceState));
60
61     t = qemu_mallocz(sizeof(DeviceType));
62     t->next = device_type_list;
63     device_type_list = t;
64     t->name = qemu_strdup(name);
65     t->size = size;
66     t->info = info;
67 }
68
69 /* Create a new device.  This only initializes the device state structure
70    and allows properties to be set.  qdev_init should be called to
71    initialize the actual device emulation.  */
72 DeviceState *qdev_create(BusState *bus, const char *name)
73 {
74     DeviceType *t;
75     DeviceState *dev;
76
77     for (t = device_type_list; t; t = t->next) {
78         if (strcmp(t->name, name) == 0) {
79             break;
80         }
81     }
82     if (!t) {
83         hw_error("Unknown device '%s'\n", name);
84     }
85
86     dev = qemu_mallocz(t->size);
87     dev->name = name;
88     dev->type = t;
89
90     if (!bus) {
91         /* ???: This assumes system busses have no additional state.  */
92         if (!main_system_bus) {
93             main_system_bus = qbus_create(BUS_TYPE_SYSTEM, sizeof(BusState),
94                                           NULL, "main-system-bus");
95         }
96         bus = main_system_bus;
97     }
98     if (t->info->bus_type != bus->type) {
99         /* TODO: Print bus type names.  */
100         hw_error("Device '%s' on wrong bus type (%d/%d)", name,
101                  t->info->bus_type, bus->type);
102     }
103     dev->parent_bus = bus;
104     LIST_INSERT_HEAD(&bus->children, dev, sibling);
105     return dev;
106 }
107
108 /* Initialize a device.  Device properties should be set before calling
109    this function.  IRQs and MMIO regions should be connected/mapped after
110    calling this function.  */
111 void qdev_init(DeviceState *dev)
112 {
113     dev->type->info->init(dev, dev->type->info);
114 }
115
116 /* Unlink device from bus and free the structure.  */
117 void qdev_free(DeviceState *dev)
118 {
119     LIST_REMOVE(dev, sibling);
120     free(dev);
121 }
122
123 static DeviceProperty *create_prop(DeviceState *dev, const char *name)
124 {
125     DeviceProperty *prop;
126
127     /* TODO: Check for duplicate properties.  */
128     prop = qemu_mallocz(sizeof(*prop));
129     prop->name = qemu_strdup(name);
130     prop->next = dev->props;
131     dev->props = prop;
132
133     return prop;
134 }
135
136 void qdev_set_prop_int(DeviceState *dev, const char *name, uint64_t value)
137 {
138     DeviceProperty *prop;
139
140     prop = create_prop(dev, name);
141     prop->value.i = value;
142 }
143
144 void qdev_set_prop_ptr(DeviceState *dev, const char *name, void *value)
145 {
146     DeviceProperty *prop;
147
148     prop = create_prop(dev, name);
149     prop->value.ptr = value;
150 }
151
152 void qdev_set_netdev(DeviceState *dev, NICInfo *nd)
153 {
154     assert(!dev->nd);
155     dev->nd = nd;
156 }
157
158
159 qemu_irq qdev_get_irq_sink(DeviceState *dev, int n)
160 {
161     assert(n >= 0 && n < dev->num_irq_sink);
162     return dev->irq_sink[n];
163 }
164
165 /* Register device IRQ sinks.  */
166 void qdev_init_irq_sink(DeviceState *dev, qemu_irq_handler handler, int nirq)
167 {
168     dev->num_irq_sink = nirq;
169     dev->irq_sink = qemu_allocate_irqs(handler, dev, nirq);
170 }
171
172 /* Get a character (serial) device interface.  */
173 CharDriverState *qdev_init_chardev(DeviceState *dev)
174 {
175     static int next_serial;
176     static int next_virtconsole;
177     /* FIXME: This is a nasty hack that needs to go away.  */
178     if (strncmp(dev->name, "virtio", 6) == 0) {
179         return virtcon_hds[next_virtconsole++];
180     } else {
181         return serial_hds[next_serial++];
182     }
183 }
184
185 BusState *qdev_get_parent_bus(DeviceState *dev)
186 {
187     return dev->parent_bus;
188 }
189
190 static DeviceProperty *find_prop(DeviceState *dev, const char *name)
191 {
192     DeviceProperty *prop;
193
194     for (prop = dev->props; prop; prop = prop->next) {
195         if (strcmp(prop->name, name) == 0) {
196             return prop;
197         }
198     }
199     return NULL;
200 }
201
202 uint64_t qdev_get_prop_int(DeviceState *dev, const char *name, uint64_t def)
203 {
204     DeviceProperty *prop;
205
206     prop = find_prop(dev, name);
207     if (!prop)
208         return def;
209
210     return prop->value.i;
211 }
212
213 void *qdev_get_prop_ptr(DeviceState *dev, const char *name)
214 {
215     DeviceProperty *prop;
216
217     prop = find_prop(dev, name);
218     assert(prop);
219     return prop->value.ptr;
220 }
221
222 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
223 {
224     assert(dev->num_gpio_in == 0);
225     dev->num_gpio_in = n;
226     dev->gpio_in = qemu_allocate_irqs(handler, dev, n);
227 }
228
229 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
230 {
231     assert(dev->num_gpio_out == 0);
232     dev->num_gpio_out = n;
233     dev->gpio_out = pins;
234 }
235
236 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
237 {
238     assert(n >= 0 && n < dev->num_gpio_in);
239     return dev->gpio_in[n];
240 }
241
242 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
243 {
244     assert(n >= 0 && n < dev->num_gpio_out);
245     dev->gpio_out[n] = pin;
246 }
247
248 VLANClientState *qdev_get_vlan_client(DeviceState *dev,
249                                       IOReadHandler *fd_read,
250                                       IOCanRWHandler *fd_can_read,
251                                       NetCleanup *cleanup,
252                                       void *opaque)
253 {
254     NICInfo *nd = dev->nd;
255     assert(nd);
256     return qemu_new_vlan_client(nd->vlan, nd->model, nd->name,
257                                 fd_read, fd_can_read, cleanup, opaque);
258 }
259
260
261 void qdev_get_macaddr(DeviceState *dev, uint8_t *macaddr)
262 {
263     memcpy(macaddr, dev->nd->macaddr, 6);
264 }
265
266 static int next_block_unit[IF_COUNT];
267
268 /* Get a block device.  This should only be used for single-drive devices
269    (e.g. SD/Floppy/MTD).  Multi-disk devices (scsi/ide) should use the
270    appropriate bus.  */
271 BlockDriverState *qdev_init_bdrv(DeviceState *dev, BlockInterfaceType type)
272 {
273     int unit = next_block_unit[type]++;
274     int index;
275
276     index = drive_get_index(type, 0, unit);
277     if (index == -1) {
278         return NULL;
279     }
280     return drives_table[index].bdrv;
281 }
282
283 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
284 {
285     BusState *bus;
286
287     LIST_FOREACH(bus, &dev->child_bus, sibling) {
288         if (strcmp(name, bus->name) == 0) {
289             return bus;
290         }
291     }
292     return NULL;
293 }
294
295 static int next_scsi_bus;
296
297 /* Create a scsi bus, and attach devices to it.  */
298 /* TODO: Actually create a scsi bus for hotplug to use.  */
299 void scsi_bus_new(DeviceState *host, SCSIAttachFn attach)
300 {
301    int bus = next_scsi_bus++;
302    int unit;
303    int index;
304
305    for (unit = 0; unit < MAX_SCSI_DEVS; unit++) {
306        index = drive_get_index(IF_SCSI, bus, unit);
307        if (index == -1) {
308            continue;
309        }
310        attach(host, drives_table[index].bdrv, unit);
311    }
312 }
313
314 BusState *qbus_create(BusType type, size_t size,
315                       DeviceState *parent, const char *name)
316 {
317     BusState *bus;
318
319     bus = qemu_mallocz(size);
320     bus->type = type;
321     bus->parent = parent;
322     bus->name = qemu_strdup(name);
323     LIST_INIT(&bus->children);
324     if (parent) {
325         LIST_INSERT_HEAD(&parent->child_bus, bus, sibling);
326     }
327     return bus;
328 }