]>
Commit | Line | Data |
---|---|---|
aae9460e PB |
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 "qdev.h" | |
30 | #include "sysemu.h" | |
31 | ||
32 | struct DeviceProperty { | |
33 | const char *name; | |
34 | union { | |
35 | int i; | |
36 | void *ptr; | |
37 | } value; | |
38 | DeviceProperty *next; | |
39 | }; | |
40 | ||
41 | struct DeviceType { | |
42 | const char *name; | |
43 | qdev_initfn init; | |
44 | void *opaque; | |
45 | int size; | |
46 | DeviceType *next; | |
47 | }; | |
48 | ||
4d6ae674 PB |
49 | struct ChildBusList { |
50 | const char *name; | |
51 | void *ptr; | |
52 | ChildBusList *next; | |
53 | }; | |
54 | ||
aae9460e PB |
55 | static DeviceType *device_type_list; |
56 | ||
57 | /* Register a new device type. */ | |
58 | DeviceType *qdev_register(const char *name, int size, qdev_initfn init, | |
59 | void *opaque) | |
60 | { | |
61 | DeviceType *t; | |
62 | ||
63 | assert(size >= sizeof(DeviceState)); | |
64 | ||
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; | |
72 | ||
73 | return t; | |
74 | } | |
75 | ||
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) | |
80 | { | |
81 | DeviceType *t; | |
82 | DeviceState *dev; | |
83 | ||
84 | for (t = device_type_list; t; t = t->next) { | |
85 | if (strcmp(t->name, name) == 0) { | |
86 | break; | |
87 | } | |
88 | } | |
89 | if (!t) { | |
90 | fprintf(stderr, "Unknown device '%s'\n", name); | |
91 | exit(1); | |
92 | } | |
93 | ||
94 | dev = qemu_mallocz(t->size); | |
95 | dev->name = name; | |
96 | dev->type = t; | |
97 | dev->bus = bus; | |
98 | return dev; | |
99 | } | |
100 | ||
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) | |
105 | { | |
106 | dev->type->init(dev, dev->type->opaque); | |
107 | } | |
108 | ||
109 | static DeviceProperty *create_prop(DeviceState *dev, const char *name) | |
110 | { | |
111 | DeviceProperty *prop; | |
112 | ||
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; | |
118 | ||
119 | return prop; | |
120 | } | |
121 | ||
122 | void qdev_set_prop_int(DeviceState *dev, const char *name, int value) | |
123 | { | |
124 | DeviceProperty *prop; | |
125 | ||
126 | prop = create_prop(dev, name); | |
127 | prop->value.i = value; | |
128 | } | |
129 | ||
130 | void qdev_set_prop_ptr(DeviceState *dev, const char *name, void *value) | |
131 | { | |
132 | DeviceProperty *prop; | |
133 | ||
134 | prop = create_prop(dev, name); | |
135 | prop->value.ptr = value; | |
136 | } | |
137 | ||
138 | ||
139 | qemu_irq qdev_get_irq_sink(DeviceState *dev, int n) | |
140 | { | |
141 | assert(n >= 0 && n < dev->num_irq_sink); | |
142 | return dev->irq_sink[n]; | |
143 | } | |
144 | ||
145 | /* Register device IRQ sinks. */ | |
146 | void qdev_init_irq_sink(DeviceState *dev, qemu_irq_handler handler, int nirq) | |
147 | { | |
148 | dev->num_irq_sink = nirq; | |
149 | dev->irq_sink = qemu_allocate_irqs(handler, dev, nirq); | |
150 | } | |
151 | ||
152 | /* Get a character (serial) device interface. */ | |
153 | CharDriverState *qdev_init_chardev(DeviceState *dev) | |
154 | { | |
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++]; | |
162 | } | |
163 | } | |
164 | ||
165 | void *qdev_get_bus(DeviceState *dev) | |
166 | { | |
167 | return dev->bus; | |
168 | } | |
169 | ||
170 | static DeviceProperty *find_prop(DeviceState *dev, const char *name) | |
171 | { | |
172 | DeviceProperty *prop; | |
173 | ||
174 | for (prop = dev->props; prop; prop = prop->next) { | |
175 | if (strcmp(prop->name, name) == 0) { | |
176 | return prop; | |
177 | } | |
178 | } | |
179 | return NULL; | |
180 | } | |
181 | ||
182 | uint64_t qdev_get_prop_int(DeviceState *dev, const char *name, uint64_t def) | |
183 | { | |
184 | DeviceProperty *prop; | |
185 | ||
186 | prop = find_prop(dev, name); | |
187 | if (!prop) | |
188 | return def; | |
189 | ||
190 | return prop->value.i; | |
191 | } | |
192 | ||
193 | void *qdev_get_prop_ptr(DeviceState *dev, const char *name) | |
194 | { | |
195 | DeviceProperty *prop; | |
196 | ||
197 | prop = find_prop(dev, name); | |
198 | assert(prop); | |
199 | return prop->value.ptr; | |
200 | } | |
201 | ||
202 | void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n) | |
203 | { | |
204 | assert(dev->num_gpio_in == 0); | |
205 | dev->num_gpio_in = n; | |
206 | dev->gpio_in = qemu_allocate_irqs(handler, dev, n); | |
207 | } | |
208 | ||
209 | void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n) | |
210 | { | |
211 | assert(dev->num_gpio_out == 0); | |
212 | dev->num_gpio_out = n; | |
213 | dev->gpio_out = pins; | |
214 | } | |
215 | ||
216 | qemu_irq qdev_get_gpio_in(DeviceState *dev, int n) | |
217 | { | |
218 | assert(n >= 0 && n < dev->num_gpio_in); | |
219 | return dev->gpio_in[n]; | |
220 | } | |
221 | ||
222 | void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin) | |
223 | { | |
224 | assert(n >= 0 && n < dev->num_gpio_out); | |
225 | dev->gpio_out[n] = pin; | |
226 | } | |
227 | ||
228 | static int next_block_unit[IF_COUNT]; | |
229 | ||
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) | |
234 | { | |
235 | int unit = next_block_unit[type]++; | |
236 | int index; | |
237 | ||
238 | index = drive_get_index(type, 0, unit); | |
239 | if (index == -1) { | |
240 | return NULL; | |
241 | } | |
242 | return drives_table[index].bdrv; | |
243 | } | |
4d6ae674 PB |
244 | |
245 | void *qdev_get_child_bus(DeviceState *dev, const char *name) | |
246 | { | |
247 | ChildBusList *bus; | |
248 | ||
249 | for (bus = dev->child_bus; bus; bus = bus->next) { | |
250 | if (strcmp(name, bus->name) == 0) { | |
251 | return bus->ptr; | |
252 | } | |
253 | } | |
254 | return NULL; | |
255 | } | |
256 | ||
257 | void qdev_attach_child_bus(DeviceState *dev, const char *name, void *bus) | |
258 | { | |
259 | ChildBusList *p; | |
260 | ||
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; | |
267 | } | |
6f68ecb2 PB |
268 | |
269 | static int next_scsi_bus; | |
270 | ||
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) | |
274 | { | |
275 | int bus = next_scsi_bus++; | |
276 | int unit; | |
277 | int index; | |
278 | ||
279 | for (unit = 0; unit < MAX_SCSI_DEVS; unit++) { | |
280 | index = drive_get_index(IF_SCSI, bus, unit); | |
281 | if (index == -1) { | |
282 | continue; | |
283 | } | |
284 | attach(host, drives_table[index].bdrv, unit); | |
285 | } | |
286 | } |