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
{
56 static DeviceType
*device_type_list
;
58 /* Register a new device type. */
59 DeviceType
*qdev_register(const char *name
, int size
, qdev_initfn init
,
64 assert(size
>= sizeof(DeviceState
));
66 t
= qemu_mallocz(sizeof(DeviceType
));
67 t
->next
= device_type_list
;
69 t
->name
= qemu_strdup(name
);
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
)
85 for (t
= device_type_list
; t
; t
= t
->next
) {
86 if (strcmp(t
->name
, name
) == 0) {
91 fprintf(stderr
, "Unknown device '%s'\n", name
);
95 dev
= qemu_mallocz(t
->size
);
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
)
107 dev
->type
->init(dev
, dev
->type
->opaque
);
110 static DeviceProperty
*create_prop(DeviceState
*dev
, const char *name
)
112 DeviceProperty
*prop
;
114 /* TODO: Check for duplicate properties. */
115 prop
= qemu_mallocz(sizeof(*prop
));
116 prop
->name
= qemu_strdup(name
);
117 prop
->next
= dev
->props
;
123 void qdev_set_prop_int(DeviceState
*dev
, const char *name
, int value
)
125 DeviceProperty
*prop
;
127 prop
= create_prop(dev
, name
);
128 prop
->value
.i
= value
;
131 void qdev_set_prop_ptr(DeviceState
*dev
, const char *name
, void *value
)
133 DeviceProperty
*prop
;
135 prop
= create_prop(dev
, name
);
136 prop
->value
.ptr
= value
;
139 void qdev_set_netdev(DeviceState
*dev
, NICInfo
*nd
)
146 qemu_irq
qdev_get_irq_sink(DeviceState
*dev
, int n
)
148 assert(n
>= 0 && n
< dev
->num_irq_sink
);
149 return dev
->irq_sink
[n
];
152 /* Register device IRQ sinks. */
153 void qdev_init_irq_sink(DeviceState
*dev
, qemu_irq_handler handler
, int nirq
)
155 dev
->num_irq_sink
= nirq
;
156 dev
->irq_sink
= qemu_allocate_irqs(handler
, dev
, nirq
);
159 /* Get a character (serial) device interface. */
160 CharDriverState
*qdev_init_chardev(DeviceState
*dev
)
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
++];
168 return serial_hds
[next_serial
++];
172 void *qdev_get_bus(DeviceState
*dev
)
177 static DeviceProperty
*find_prop(DeviceState
*dev
, const char *name
)
179 DeviceProperty
*prop
;
181 for (prop
= dev
->props
; prop
; prop
= prop
->next
) {
182 if (strcmp(prop
->name
, name
) == 0) {
189 uint64_t qdev_get_prop_int(DeviceState
*dev
, const char *name
, uint64_t def
)
191 DeviceProperty
*prop
;
193 prop
= find_prop(dev
, name
);
197 return prop
->value
.i
;
200 void *qdev_get_prop_ptr(DeviceState
*dev
, const char *name
)
202 DeviceProperty
*prop
;
204 prop
= find_prop(dev
, name
);
206 return prop
->value
.ptr
;
209 void qdev_init_gpio_in(DeviceState
*dev
, qemu_irq_handler handler
, int n
)
211 assert(dev
->num_gpio_in
== 0);
212 dev
->num_gpio_in
= n
;
213 dev
->gpio_in
= qemu_allocate_irqs(handler
, dev
, n
);
216 void qdev_init_gpio_out(DeviceState
*dev
, qemu_irq
*pins
, int n
)
218 assert(dev
->num_gpio_out
== 0);
219 dev
->num_gpio_out
= n
;
220 dev
->gpio_out
= pins
;
223 qemu_irq
qdev_get_gpio_in(DeviceState
*dev
, int n
)
225 assert(n
>= 0 && n
< dev
->num_gpio_in
);
226 return dev
->gpio_in
[n
];
229 void qdev_connect_gpio_out(DeviceState
* dev
, int n
, qemu_irq pin
)
231 assert(n
>= 0 && n
< dev
->num_gpio_out
);
232 dev
->gpio_out
[n
] = pin
;
235 VLANClientState
*qdev_get_vlan_client(DeviceState
*dev
,
236 IOReadHandler
*fd_read
,
237 IOCanRWHandler
*fd_can_read
,
241 NICInfo
*nd
= dev
->nd
;
243 return qemu_new_vlan_client(nd
->vlan
, nd
->model
, nd
->name
,
244 fd_read
, fd_can_read
, cleanup
, opaque
);
248 void qdev_get_macaddr(DeviceState
*dev
, uint8_t *macaddr
)
250 memcpy(macaddr
, dev
->nd
->macaddr
, 6);
253 static int next_block_unit
[IF_COUNT
];
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
258 BlockDriverState
*qdev_init_bdrv(DeviceState
*dev
, BlockInterfaceType type
)
260 int unit
= next_block_unit
[type
]++;
263 index
= drive_get_index(type
, 0, unit
);
267 return drives_table
[index
].bdrv
;
270 void *qdev_get_child_bus(DeviceState
*dev
, const char *name
)
274 for (bus
= dev
->child_bus
; bus
; bus
= bus
->next
) {
275 if (strcmp(name
, bus
->name
) == 0) {
282 void qdev_attach_child_bus(DeviceState
*dev
, const char *name
, void *bus
)
286 assert(!qdev_get_child_bus(dev
, name
));
287 p
= qemu_mallocz(sizeof(*p
));
288 p
->name
= qemu_strdup(name
);
290 p
->next
= dev
->child_bus
;
294 static int next_scsi_bus
;
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
)
300 int bus
= next_scsi_bus
++;
304 for (unit
= 0; unit
< MAX_SCSI_DEVS
; unit
++) {
305 index
= drive_get_index(IF_SCSI
, bus
, unit
);
309 attach(host
, drives_table
[index
].bdrv
, unit
);