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
33 struct DeviceProperty {
49 /* This is a nasty hack to allow passing a NULL bus to qdev_create. */
50 BusState *main_system_bus;
52 static DeviceType *device_type_list;
54 /* Register a new device type. */
55 void qdev_register(const char *name, int size, DeviceInfo *info)
59 assert(size >= sizeof(DeviceState));
61 t = qemu_mallocz(sizeof(DeviceType));
62 t->next = device_type_list;
64 t->name = qemu_strdup(name);
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)
77 for (t = device_type_list; t; t = t->next) {
78 if (strcmp(t->name, name) == 0) {
83 hw_error("Unknown device '%s'\n", name);
86 dev = qemu_mallocz(t->size);
90 /* ???: This assumes system busses have no additional state. */
91 if (!main_system_bus) {
92 main_system_bus = qbus_create(BUS_TYPE_SYSTEM, sizeof(BusState),
93 NULL, "main-system-bus");
95 bus = main_system_bus;
97 if (t->info->bus_type != bus->type) {
98 /* TODO: Print bus type names. */
99 hw_error("Device '%s' on wrong bus type (%d/%d)", name,
100 t->info->bus_type, bus->type);
102 dev->parent_bus = bus;
103 LIST_INSERT_HEAD(&bus->children, dev, sibling);
107 /* Initialize a device. Device properties should be set before calling
108 this function. IRQs and MMIO regions should be connected/mapped after
109 calling this function. */
110 void qdev_init(DeviceState *dev)
112 dev->type->info->init(dev, dev->type->info);
115 /* Unlink device from bus and free the structure. */
116 void qdev_free(DeviceState *dev)
118 LIST_REMOVE(dev, sibling);
122 static DeviceProperty *create_prop(DeviceState *dev, const char *name)
124 DeviceProperty *prop;
126 /* TODO: Check for duplicate properties. */
127 prop = qemu_mallocz(sizeof(*prop));
128 prop->name = qemu_strdup(name);
129 prop->next = dev->props;
135 void qdev_set_prop_int(DeviceState *dev, const char *name, uint64_t value)
137 DeviceProperty *prop;
139 prop = create_prop(dev, name);
140 prop->value.i = value;
143 void qdev_set_prop_ptr(DeviceState *dev, const char *name, void *value)
145 DeviceProperty *prop;
147 prop = create_prop(dev, name);
148 prop->value.ptr = value;
151 void qdev_set_netdev(DeviceState *dev, NICInfo *nd)
158 /* Get a character (serial) device interface. */
159 CharDriverState *qdev_init_chardev(DeviceState *dev)
161 static int next_serial;
162 static int next_virtconsole;
163 /* FIXME: This is a nasty hack that needs to go away. */
164 if (strncmp(dev->type->name, "virtio", 6) == 0) {
165 return virtcon_hds[next_virtconsole++];
167 return serial_hds[next_serial++];
171 BusState *qdev_get_parent_bus(DeviceState *dev)
173 return dev->parent_bus;
176 static DeviceProperty *find_prop(DeviceState *dev, const char *name)
178 DeviceProperty *prop;
180 for (prop = dev->props; prop; prop = prop->next) {
181 if (strcmp(prop->name, name) == 0) {
188 uint64_t qdev_get_prop_int(DeviceState *dev, const char *name, uint64_t def)
190 DeviceProperty *prop;
192 prop = find_prop(dev, name);
196 return prop->value.i;
199 void *qdev_get_prop_ptr(DeviceState *dev, const char *name)
201 DeviceProperty *prop;
203 prop = find_prop(dev, name);
205 return prop->value.ptr;
208 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
210 assert(dev->num_gpio_in == 0);
211 dev->num_gpio_in = n;
212 dev->gpio_in = qemu_allocate_irqs(handler, dev, n);
215 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
217 assert(dev->num_gpio_out == 0);
218 dev->num_gpio_out = n;
219 dev->gpio_out = pins;
222 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
224 assert(n >= 0 && n < dev->num_gpio_in);
225 return dev->gpio_in[n];
228 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
230 assert(n >= 0 && n < dev->num_gpio_out);
231 dev->gpio_out[n] = pin;
234 VLANClientState *qdev_get_vlan_client(DeviceState *dev,
235 IOReadHandler *fd_read,
236 IOCanRWHandler *fd_can_read,
240 NICInfo *nd = dev->nd;
242 return qemu_new_vlan_client(nd->vlan, nd->model, nd->name,
243 fd_read, fd_can_read, cleanup, opaque);
247 void qdev_get_macaddr(DeviceState *dev, uint8_t *macaddr)
249 memcpy(macaddr, dev->nd->macaddr, 6);
252 static int next_block_unit[IF_COUNT];
254 /* Get a block device. This should only be used for single-drive devices
255 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
257 BlockDriverState *qdev_init_bdrv(DeviceState *dev, BlockInterfaceType type)
259 int unit = next_block_unit[type]++;
262 index = drive_get_index(type, 0, unit);
266 return drives_table[index].bdrv;
269 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
273 LIST_FOREACH(bus, &dev->child_bus, sibling) {
274 if (strcmp(name, bus->name) == 0) {
281 static int next_scsi_bus;
283 /* Create a scsi bus, and attach devices to it. */
284 /* TODO: Actually create a scsi bus for hotplug to use. */
285 void scsi_bus_new(DeviceState *host, SCSIAttachFn attach)
287 int bus = next_scsi_bus++;
291 for (unit = 0; unit < MAX_SCSI_DEVS; unit++) {
292 index = drive_get_index(IF_SCSI, bus, unit);
296 attach(host, drives_table[index].bdrv, unit);
300 BusState *qbus_create(BusType type, size_t size,
301 DeviceState *parent, const char *name)
305 bus = qemu_mallocz(size);
307 bus->parent = parent;
308 bus->name = qemu_strdup(name);
309 LIST_INIT(&bus->children);
311 LIST_INSERT_HEAD(&parent->child_bus, bus, sibling);