qdev scsi bus infrastructure
[qemu/mini2440.git] / hw / qdev.c
bloba97f42589c0eb94a2219cbb3a9130a604cb0ba10
1 /*
2 * Dynamic device configuration and creation.
4 * Copyright (c) 2009 CodeSourcery
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.
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.
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
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. */
29 #include "qdev.h"
30 #include "sysemu.h"
32 struct DeviceProperty {
33 const char *name;
34 union {
35 int i;
36 void *ptr;
37 } value;
38 DeviceProperty *next;
41 struct DeviceType {
42 const char *name;
43 qdev_initfn init;
44 void *opaque;
45 int size;
46 DeviceType *next;
49 struct ChildBusList {
50 const char *name;
51 void *ptr;
52 ChildBusList *next;
55 static DeviceType *device_type_list;
57 /* Register a new device type. */
58 DeviceType *qdev_register(const char *name, int size, qdev_initfn init,
59 void *opaque)
61 DeviceType *t;
63 assert(size >= sizeof(DeviceState));
65 t = qemu_mallocz(sizeof(DeviceType));
66 t->next = device_type_list;
67 device_type_list = t;
68 t->name = qemu_strdup(name);
69 t->size = size;
70 t->init = init;
71 t->opaque = opaque;
73 return t;
76 /* Create a new device. This only initializes the device state structure
77 and allows properties to be set. qdev_init should be called to
78 initialize the actual device emulation. */
79 DeviceState *qdev_create(void *bus, const char *name)
81 DeviceType *t;
82 DeviceState *dev;
84 for (t = device_type_list; t; t = t->next) {
85 if (strcmp(t->name, name) == 0) {
86 break;
89 if (!t) {
90 fprintf(stderr, "Unknown device '%s'\n", name);
91 exit(1);
94 dev = qemu_mallocz(t->size);
95 dev->name = name;
96 dev->type = t;
97 dev->bus = bus;
98 return dev;
101 /* Initialize a device. Device properties should be set before calling
102 this function. IRQs and MMIO regions should be connected/mapped after
103 calling this function. */
104 void qdev_init(DeviceState *dev)
106 dev->type->init(dev, dev->type->opaque);
109 static DeviceProperty *create_prop(DeviceState *dev, const char *name)
111 DeviceProperty *prop;
113 /* TODO: Check for duplicate properties. */
114 prop = qemu_mallocz(sizeof(*prop));
115 prop->name = qemu_strdup(name);
116 prop->next = dev->props;
117 dev->props = prop;
119 return prop;
122 void qdev_set_prop_int(DeviceState *dev, const char *name, int value)
124 DeviceProperty *prop;
126 prop = create_prop(dev, name);
127 prop->value.i = value;
130 void qdev_set_prop_ptr(DeviceState *dev, const char *name, void *value)
132 DeviceProperty *prop;
134 prop = create_prop(dev, name);
135 prop->value.ptr = value;
139 qemu_irq qdev_get_irq_sink(DeviceState *dev, int n)
141 assert(n >= 0 && n < dev->num_irq_sink);
142 return dev->irq_sink[n];
145 /* Register device IRQ sinks. */
146 void qdev_init_irq_sink(DeviceState *dev, qemu_irq_handler handler, int nirq)
148 dev->num_irq_sink = nirq;
149 dev->irq_sink = qemu_allocate_irqs(handler, dev, nirq);
152 /* Get a character (serial) device interface. */
153 CharDriverState *qdev_init_chardev(DeviceState *dev)
155 static int next_serial;
156 static int next_virtconsole;
157 /* FIXME: This is a nasty hack that needs to go away. */
158 if (strncmp(dev->name, "virtio", 6) == 0) {
159 return virtcon_hds[next_virtconsole++];
160 } else {
161 return serial_hds[next_serial++];
165 void *qdev_get_bus(DeviceState *dev)
167 return dev->bus;
170 static DeviceProperty *find_prop(DeviceState *dev, const char *name)
172 DeviceProperty *prop;
174 for (prop = dev->props; prop; prop = prop->next) {
175 if (strcmp(prop->name, name) == 0) {
176 return prop;
179 return NULL;
182 uint64_t qdev_get_prop_int(DeviceState *dev, const char *name, uint64_t def)
184 DeviceProperty *prop;
186 prop = find_prop(dev, name);
187 if (!prop)
188 return def;
190 return prop->value.i;
193 void *qdev_get_prop_ptr(DeviceState *dev, const char *name)
195 DeviceProperty *prop;
197 prop = find_prop(dev, name);
198 assert(prop);
199 return prop->value.ptr;
202 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
204 assert(dev->num_gpio_in == 0);
205 dev->num_gpio_in = n;
206 dev->gpio_in = qemu_allocate_irqs(handler, dev, n);
209 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
211 assert(dev->num_gpio_out == 0);
212 dev->num_gpio_out = n;
213 dev->gpio_out = pins;
216 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
218 assert(n >= 0 && n < dev->num_gpio_in);
219 return dev->gpio_in[n];
222 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
224 assert(n >= 0 && n < dev->num_gpio_out);
225 dev->gpio_out[n] = pin;
228 static int next_block_unit[IF_COUNT];
230 /* Get a block device. This should only be used for single-drive devices
231 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
232 appropriate bus. */
233 BlockDriverState *qdev_init_bdrv(DeviceState *dev, BlockInterfaceType type)
235 int unit = next_block_unit[type]++;
236 int index;
238 index = drive_get_index(type, 0, unit);
239 if (index == -1) {
240 return NULL;
242 return drives_table[index].bdrv;
245 void *qdev_get_child_bus(DeviceState *dev, const char *name)
247 ChildBusList *bus;
249 for (bus = dev->child_bus; bus; bus = bus->next) {
250 if (strcmp(name, bus->name) == 0) {
251 return bus->ptr;
254 return NULL;
257 void qdev_attach_child_bus(DeviceState *dev, const char *name, void *bus)
259 ChildBusList *p;
261 assert(!qdev_get_child_bus(dev, name));
262 p = qemu_mallocz(sizeof(*p));
263 p->name = qemu_strdup(name);
264 p->ptr = bus;
265 p->next = dev->child_bus;
266 dev->child_bus = p;
269 static int next_scsi_bus;
271 /* Create a scsi bus, and attach devices to it. */
272 /* TODO: Actually create a scsi bus for hotplug to use. */
273 void scsi_bus_new(DeviceState *host, SCSIAttachFn attach)
275 int bus = next_scsi_bus++;
276 int unit;
277 int index;
279 for (unit = 0; unit < MAX_SCSI_DEVS; unit++) {
280 index = drive_get_index(IF_SCSI, bus, unit);
281 if (index == -1) {
282 continue;
284 attach(host, drives_table[index].bdrv, unit);