]>
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 | ||
9d07d757 | 29 | #include "net.h" |
aae9460e PB |
30 | #include "qdev.h" |
31 | #include "sysemu.h" | |
32 | ||
33 | struct DeviceProperty { | |
34 | const char *name; | |
35 | union { | |
89a740e1 | 36 | uint64_t i; |
aae9460e PB |
37 | void *ptr; |
38 | } value; | |
39 | DeviceProperty *next; | |
40 | }; | |
41 | ||
42 | struct DeviceType { | |
43 | const char *name; | |
02e2da45 | 44 | DeviceInfo *info; |
aae9460e PB |
45 | int size; |
46 | DeviceType *next; | |
47 | }; | |
48 | ||
02e2da45 PB |
49 | /* This is a nasty hack to allow passing a NULL bus to qdev_create. */ |
50 | BusState *main_system_bus; | |
4d6ae674 | 51 | |
aae9460e PB |
52 | static DeviceType *device_type_list; |
53 | ||
54 | /* Register a new device type. */ | |
02e2da45 | 55 | void qdev_register(const char *name, int size, DeviceInfo *info) |
aae9460e PB |
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; | |
02e2da45 | 66 | t->info = info; |
aae9460e PB |
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. */ | |
02e2da45 | 72 | DeviceState *qdev_create(BusState *bus, const char *name) |
aae9460e PB |
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) { | |
02e2da45 | 83 | hw_error("Unknown device '%s'\n", name); |
aae9460e PB |
84 | } |
85 | ||
86 | dev = qemu_mallocz(t->size); | |
87 | dev->name = name; | |
88 | dev->type = t; | |
02e2da45 PB |
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); | |
aae9460e PB |
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 | { | |
02e2da45 PB |
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); | |
aae9460e PB |
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 | ||
89a740e1 | 136 | void qdev_set_prop_int(DeviceState *dev, const char *name, uint64_t value) |
aae9460e PB |
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 | ||
9d07d757 PB |
152 | void qdev_set_netdev(DeviceState *dev, NICInfo *nd) |
153 | { | |
154 | assert(!dev->nd); | |
155 | dev->nd = nd; | |
156 | } | |
157 | ||
aae9460e | 158 | |
aae9460e PB |
159 | /* Get a character (serial) device interface. */ |
160 | CharDriverState *qdev_init_chardev(DeviceState *dev) | |
161 | { | |
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++]; | |
167 | } else { | |
168 | return serial_hds[next_serial++]; | |
169 | } | |
170 | } | |
171 | ||
02e2da45 | 172 | BusState *qdev_get_parent_bus(DeviceState *dev) |
aae9460e | 173 | { |
02e2da45 | 174 | return dev->parent_bus; |
aae9460e PB |
175 | } |
176 | ||
177 | static DeviceProperty *find_prop(DeviceState *dev, const char *name) | |
178 | { | |
179 | DeviceProperty *prop; | |
180 | ||
181 | for (prop = dev->props; prop; prop = prop->next) { | |
182 | if (strcmp(prop->name, name) == 0) { | |
183 | return prop; | |
184 | } | |
185 | } | |
186 | return NULL; | |
187 | } | |
188 | ||
189 | uint64_t qdev_get_prop_int(DeviceState *dev, const char *name, uint64_t def) | |
190 | { | |
191 | DeviceProperty *prop; | |
192 | ||
193 | prop = find_prop(dev, name); | |
194 | if (!prop) | |
195 | return def; | |
196 | ||
197 | return prop->value.i; | |
198 | } | |
199 | ||
200 | void *qdev_get_prop_ptr(DeviceState *dev, const char *name) | |
201 | { | |
202 | DeviceProperty *prop; | |
203 | ||
204 | prop = find_prop(dev, name); | |
205 | assert(prop); | |
206 | return prop->value.ptr; | |
207 | } | |
208 | ||
209 | void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n) | |
210 | { | |
211 | assert(dev->num_gpio_in == 0); | |
212 | dev->num_gpio_in = n; | |
213 | dev->gpio_in = qemu_allocate_irqs(handler, dev, n); | |
214 | } | |
215 | ||
216 | void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n) | |
217 | { | |
218 | assert(dev->num_gpio_out == 0); | |
219 | dev->num_gpio_out = n; | |
220 | dev->gpio_out = pins; | |
221 | } | |
222 | ||
223 | qemu_irq qdev_get_gpio_in(DeviceState *dev, int n) | |
224 | { | |
225 | assert(n >= 0 && n < dev->num_gpio_in); | |
226 | return dev->gpio_in[n]; | |
227 | } | |
228 | ||
229 | void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin) | |
230 | { | |
231 | assert(n >= 0 && n < dev->num_gpio_out); | |
232 | dev->gpio_out[n] = pin; | |
233 | } | |
234 | ||
9d07d757 PB |
235 | VLANClientState *qdev_get_vlan_client(DeviceState *dev, |
236 | IOReadHandler *fd_read, | |
237 | IOCanRWHandler *fd_can_read, | |
238 | NetCleanup *cleanup, | |
239 | void *opaque) | |
240 | { | |
241 | NICInfo *nd = dev->nd; | |
242 | assert(nd); | |
243 | return qemu_new_vlan_client(nd->vlan, nd->model, nd->name, | |
244 | fd_read, fd_can_read, cleanup, opaque); | |
245 | } | |
246 | ||
247 | ||
248 | void qdev_get_macaddr(DeviceState *dev, uint8_t *macaddr) | |
249 | { | |
250 | memcpy(macaddr, dev->nd->macaddr, 6); | |
251 | } | |
252 | ||
aae9460e PB |
253 | static int next_block_unit[IF_COUNT]; |
254 | ||
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 | |
257 | appropriate bus. */ | |
258 | BlockDriverState *qdev_init_bdrv(DeviceState *dev, BlockInterfaceType type) | |
259 | { | |
260 | int unit = next_block_unit[type]++; | |
261 | int index; | |
262 | ||
263 | index = drive_get_index(type, 0, unit); | |
264 | if (index == -1) { | |
265 | return NULL; | |
266 | } | |
267 | return drives_table[index].bdrv; | |
268 | } | |
4d6ae674 | 269 | |
02e2da45 | 270 | BusState *qdev_get_child_bus(DeviceState *dev, const char *name) |
4d6ae674 | 271 | { |
02e2da45 | 272 | BusState *bus; |
4d6ae674 | 273 | |
02e2da45 | 274 | LIST_FOREACH(bus, &dev->child_bus, sibling) { |
4d6ae674 | 275 | if (strcmp(name, bus->name) == 0) { |
02e2da45 | 276 | return bus; |
4d6ae674 PB |
277 | } |
278 | } | |
279 | return NULL; | |
280 | } | |
281 | ||
6f68ecb2 PB |
282 | static int next_scsi_bus; |
283 | ||
284 | /* Create a scsi bus, and attach devices to it. */ | |
285 | /* TODO: Actually create a scsi bus for hotplug to use. */ | |
286 | void scsi_bus_new(DeviceState *host, SCSIAttachFn attach) | |
287 | { | |
288 | int bus = next_scsi_bus++; | |
289 | int unit; | |
290 | int index; | |
291 | ||
292 | for (unit = 0; unit < MAX_SCSI_DEVS; unit++) { | |
293 | index = drive_get_index(IF_SCSI, bus, unit); | |
294 | if (index == -1) { | |
295 | continue; | |
296 | } | |
297 | attach(host, drives_table[index].bdrv, unit); | |
298 | } | |
299 | } | |
02e2da45 PB |
300 | |
301 | BusState *qbus_create(BusType type, size_t size, | |
302 | DeviceState *parent, const char *name) | |
303 | { | |
304 | BusState *bus; | |
305 | ||
306 | bus = qemu_mallocz(size); | |
307 | bus->type = type; | |
308 | bus->parent = parent; | |
309 | bus->name = qemu_strdup(name); | |
310 | LIST_INIT(&bus->children); | |
311 | if (parent) { | |
312 | LIST_INSERT_HEAD(&parent->child_bus, bus, sibling); | |
313 | } | |
314 | return bus; | |
315 | } |