]>
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 | |
8167ee88 | 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
aae9460e PB |
18 | */ |
19 | ||
20 | /* The theory here is that it should be possible to create a machine without | |
21 | knowledge of specific devices. Historically board init routines have | |
22 | passed a bunch of arguments to each device, requiring the board know | |
23 | exactly which device it is dealing with. This file provides an abstract | |
24 | API for device configuration and initialization. Devices will generally | |
25 | inherit from a particular bus (e.g. PCI or I2C) rather than | |
26 | this API directly. */ | |
27 | ||
9d07d757 | 28 | #include "net.h" |
aae9460e PB |
29 | #include "qdev.h" |
30 | #include "sysemu.h" | |
56f9107e | 31 | #include "error.h" |
aae9460e | 32 | |
ee46d8a5 | 33 | int qdev_hotplug = 0; |
0ac8ef71 AW |
34 | static bool qdev_hot_added = false; |
35 | static bool qdev_hot_removed = false; | |
3418bd25 | 36 | |
cdaed7c7 | 37 | /* This is a nasty hack to allow passing a NULL bus to qdev_create. */ |
b9aaf7f8 | 38 | static BusState *main_system_bus; |
2da8bb92 | 39 | static void main_system_bus_create(void); |
4d6ae674 | 40 | |
aae9460e | 41 | /* Register a new device type. */ |
4be9f0d1 AL |
42 | const VMStateDescription *qdev_get_vmsd(DeviceState *dev) |
43 | { | |
6e008585 AL |
44 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
45 | return dc->vmsd; | |
4be9f0d1 AL |
46 | } |
47 | ||
4be9f0d1 AL |
48 | const char *qdev_fw_name(DeviceState *dev) |
49 | { | |
6e008585 | 50 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
4be9f0d1 | 51 | |
6e008585 AL |
52 | if (dc->fw_name) { |
53 | return dc->fw_name; | |
4be9f0d1 AL |
54 | } |
55 | ||
56 | return object_get_typename(OBJECT(dev)); | |
57 | } | |
58 | ||
a369da5f BS |
59 | bool qdev_exists(const char *name) |
60 | { | |
212ad111 | 61 | return !!object_class_by_name(name); |
a369da5f | 62 | } |
40021f08 | 63 | |
ca2cc788 PB |
64 | static void qdev_property_add_legacy(DeviceState *dev, Property *prop, |
65 | Error **errp); | |
66 | ||
9fbe6127 | 67 | void qdev_set_parent_bus(DeviceState *dev, BusState *bus) |
0c17542d | 68 | { |
0c17542d MA |
69 | if (qdev_hotplug) { |
70 | assert(bus->allow_hotplug); | |
0c17542d | 71 | } |
a5296ca9 | 72 | |
9fbe6127 | 73 | dev->parent_bus = bus; |
9674bfe4 | 74 | QTAILQ_INSERT_HEAD(&bus->children, dev, sibling); |
0c17542d MA |
75 | } |
76 | ||
aae9460e PB |
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. */ | |
02e2da45 | 80 | DeviceState *qdev_create(BusState *bus, const char *name) |
0bcdeda7 BS |
81 | { |
82 | DeviceState *dev; | |
83 | ||
84 | dev = qdev_try_create(bus, name); | |
85 | if (!dev) { | |
e92714c7 PM |
86 | if (bus) { |
87 | hw_error("Unknown device '%s' for bus '%s'\n", name, | |
88 | bus->info->name); | |
89 | } else { | |
90 | hw_error("Unknown device '%s' for default sysbus\n", name); | |
91 | } | |
0bcdeda7 BS |
92 | } |
93 | ||
94 | return dev; | |
95 | } | |
96 | ||
da57febf | 97 | DeviceState *qdev_try_create(BusState *bus, const char *type) |
aae9460e | 98 | { |
9fbe6127 AL |
99 | DeviceState *dev; |
100 | ||
da57febf | 101 | if (object_class_by_name(type) == NULL) { |
4ed658ca AF |
102 | return NULL; |
103 | } | |
da57febf | 104 | dev = DEVICE(object_new(type)); |
9fbe6127 AL |
105 | if (!dev) { |
106 | return NULL; | |
107 | } | |
108 | ||
10c4c98a | 109 | if (!bus) { |
68694897 | 110 | bus = sysbus_get_default(); |
10c4c98a GH |
111 | } |
112 | ||
9fbe6127 | 113 | qdev_set_parent_bus(dev, bus); |
9fbe6127 AL |
114 | |
115 | return dev; | |
aae9460e PB |
116 | } |
117 | ||
118 | /* Initialize a device. Device properties should be set before calling | |
119 | this function. IRQs and MMIO regions should be connected/mapped after | |
18cfeb52 MA |
120 | calling this function. |
121 | On failure, destroy the device and return negative value. | |
122 | Return 0 on success. */ | |
81a322d4 | 123 | int qdev_init(DeviceState *dev) |
aae9460e | 124 | { |
6e008585 | 125 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
959f733a GH |
126 | int rc; |
127 | ||
131ec1bd | 128 | assert(dev->state == DEV_STATE_CREATED); |
6e008585 | 129 | |
d307af79 | 130 | rc = dc->init(dev); |
18cfeb52 MA |
131 | if (rc < 0) { |
132 | qdev_free(dev); | |
959f733a | 133 | return rc; |
18cfeb52 | 134 | } |
da57febf PB |
135 | |
136 | if (!OBJECT(dev)->parent) { | |
137 | static int unattached_count = 0; | |
138 | gchar *name = g_strdup_printf("device[%d]", unattached_count++); | |
139 | ||
dfe47e70 AF |
140 | object_property_add_child(container_get(qdev_get_machine(), |
141 | "/unattached"), | |
142 | name, OBJECT(dev), NULL); | |
da57febf PB |
143 | g_free(name); |
144 | } | |
145 | ||
6e008585 AL |
146 | if (qdev_get_vmsd(dev)) { |
147 | vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev, | |
4d2ffa08 JK |
148 | dev->instance_id_alias, |
149 | dev->alias_required_for_version); | |
150 | } | |
131ec1bd | 151 | dev->state = DEV_STATE_INITIALIZED; |
94afdadc AL |
152 | if (dev->hotplugged) { |
153 | device_reset(dev); | |
5ab28c83 | 154 | } |
959f733a | 155 | return 0; |
02e2da45 PB |
156 | } |
157 | ||
4d2ffa08 JK |
158 | void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id, |
159 | int required_for_version) | |
160 | { | |
161 | assert(dev->state == DEV_STATE_CREATED); | |
162 | dev->instance_id_alias = alias_id; | |
163 | dev->alias_required_for_version = required_for_version; | |
164 | } | |
165 | ||
56f9107e | 166 | void qdev_unplug(DeviceState *dev, Error **errp) |
3418bd25 | 167 | { |
6e008585 AL |
168 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
169 | ||
3418bd25 | 170 | if (!dev->parent_bus->allow_hotplug) { |
56f9107e LC |
171 | error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name); |
172 | return; | |
3418bd25 | 173 | } |
6e008585 | 174 | assert(dc->unplug != NULL); |
593831de | 175 | |
0ac8ef71 AW |
176 | qdev_hot_removed = true; |
177 | ||
56f9107e LC |
178 | if (dc->unplug(dev) < 0) { |
179 | error_set(errp, QERR_UNDEFINED_ERROR); | |
180 | return; | |
181 | } | |
3418bd25 GH |
182 | } |
183 | ||
ec990eb6 AL |
184 | static int qdev_reset_one(DeviceState *dev, void *opaque) |
185 | { | |
94afdadc | 186 | device_reset(dev); |
ec990eb6 AL |
187 | |
188 | return 0; | |
189 | } | |
190 | ||
191 | BusState *sysbus_get_default(void) | |
192 | { | |
68694897 | 193 | if (!main_system_bus) { |
2da8bb92 | 194 | main_system_bus_create(); |
68694897 | 195 | } |
ec990eb6 AL |
196 | return main_system_bus; |
197 | } | |
198 | ||
b4694b7c IY |
199 | static int qbus_reset_one(BusState *bus, void *opaque) |
200 | { | |
201 | if (bus->info->reset) { | |
202 | return bus->info->reset(bus); | |
203 | } | |
204 | return 0; | |
205 | } | |
206 | ||
5af0a04b IY |
207 | void qdev_reset_all(DeviceState *dev) |
208 | { | |
209 | qdev_walk_children(dev, qdev_reset_one, qbus_reset_one, NULL); | |
210 | } | |
211 | ||
80376c3f IY |
212 | void qbus_reset_all_fn(void *opaque) |
213 | { | |
214 | BusState *bus = opaque; | |
f530cce3 | 215 | qbus_walk_children(bus, qdev_reset_one, qbus_reset_one, NULL); |
80376c3f IY |
216 | } |
217 | ||
3418bd25 GH |
218 | /* can be used as ->unplug() callback for the simple cases */ |
219 | int qdev_simple_unplug_cb(DeviceState *dev) | |
220 | { | |
221 | /* just zap it */ | |
57c9fafe | 222 | object_unparent(OBJECT(dev)); |
3418bd25 GH |
223 | qdev_free(dev); |
224 | return 0; | |
225 | } | |
226 | ||
3b29a101 MT |
227 | |
228 | /* Like qdev_init(), but terminate program via error_report() instead of | |
e23a1b33 MA |
229 | returning an error value. This is okay during machine creation. |
230 | Don't use for hotplug, because there callers need to recover from | |
231 | failure. Exception: if you know the device's init() callback can't | |
232 | fail, then qdev_init_nofail() can't fail either, and is therefore | |
233 | usable even then. But relying on the device implementation that | |
234 | way is somewhat unclean, and best avoided. */ | |
235 | void qdev_init_nofail(DeviceState *dev) | |
236 | { | |
bd6c9a61 | 237 | if (qdev_init(dev) < 0) { |
6e008585 AL |
238 | error_report("Initialization of device %s failed", |
239 | object_get_typename(OBJECT(dev))); | |
bd6c9a61 MA |
240 | exit(1); |
241 | } | |
e23a1b33 MA |
242 | } |
243 | ||
02e2da45 PB |
244 | /* Unlink device from bus and free the structure. */ |
245 | void qdev_free(DeviceState *dev) | |
246 | { | |
32fea402 | 247 | object_delete(OBJECT(dev)); |
aae9460e PB |
248 | } |
249 | ||
3418bd25 GH |
250 | void qdev_machine_creation_done(void) |
251 | { | |
252 | /* | |
253 | * ok, initial machine setup is done, starting from now we can | |
254 | * only create hotpluggable devices | |
255 | */ | |
256 | qdev_hotplug = 1; | |
257 | } | |
258 | ||
0ac8ef71 AW |
259 | bool qdev_machine_modified(void) |
260 | { | |
261 | return qdev_hot_added || qdev_hot_removed; | |
262 | } | |
263 | ||
02e2da45 | 264 | BusState *qdev_get_parent_bus(DeviceState *dev) |
aae9460e | 265 | { |
02e2da45 | 266 | return dev->parent_bus; |
aae9460e PB |
267 | } |
268 | ||
aae9460e PB |
269 | void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n) |
270 | { | |
271 | assert(dev->num_gpio_in == 0); | |
272 | dev->num_gpio_in = n; | |
273 | dev->gpio_in = qemu_allocate_irqs(handler, dev, n); | |
274 | } | |
275 | ||
276 | void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n) | |
277 | { | |
278 | assert(dev->num_gpio_out == 0); | |
279 | dev->num_gpio_out = n; | |
280 | dev->gpio_out = pins; | |
281 | } | |
282 | ||
283 | qemu_irq qdev_get_gpio_in(DeviceState *dev, int n) | |
284 | { | |
285 | assert(n >= 0 && n < dev->num_gpio_in); | |
286 | return dev->gpio_in[n]; | |
287 | } | |
288 | ||
289 | void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin) | |
290 | { | |
291 | assert(n >= 0 && n < dev->num_gpio_out); | |
292 | dev->gpio_out[n] = pin; | |
293 | } | |
294 | ||
ed16ab5a GH |
295 | void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd) |
296 | { | |
6eed1856 | 297 | qdev_prop_set_macaddr(dev, "mac", nd->macaddr.a); |
ed16ab5a GH |
298 | if (nd->vlan) |
299 | qdev_prop_set_vlan(dev, "vlan", nd->vlan); | |
300 | if (nd->netdev) | |
301 | qdev_prop_set_netdev(dev, "netdev", nd->netdev); | |
75422b0d | 302 | if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED && |
97b15621 GH |
303 | qdev_prop_exists(dev, "vectors")) { |
304 | qdev_prop_set_uint32(dev, "vectors", nd->nvectors); | |
305 | } | |
48e2faf2 | 306 | nd->instantiated = 1; |
ed16ab5a GH |
307 | } |
308 | ||
02e2da45 | 309 | BusState *qdev_get_child_bus(DeviceState *dev, const char *name) |
4d6ae674 | 310 | { |
02e2da45 | 311 | BusState *bus; |
4d6ae674 | 312 | |
72cf2d4f | 313 | QLIST_FOREACH(bus, &dev->child_bus, sibling) { |
4d6ae674 | 314 | if (strcmp(name, bus->name) == 0) { |
02e2da45 | 315 | return bus; |
4d6ae674 PB |
316 | } |
317 | } | |
318 | return NULL; | |
319 | } | |
320 | ||
81699d8a AL |
321 | int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn, |
322 | qbus_walkerfn *busfn, void *opaque) | |
323 | { | |
324 | DeviceState *dev; | |
325 | int err; | |
326 | ||
327 | if (busfn) { | |
328 | err = busfn(bus, opaque); | |
329 | if (err) { | |
330 | return err; | |
331 | } | |
332 | } | |
333 | ||
d8bb00d6 | 334 | QTAILQ_FOREACH(dev, &bus->children, sibling) { |
81699d8a AL |
335 | err = qdev_walk_children(dev, devfn, busfn, opaque); |
336 | if (err < 0) { | |
337 | return err; | |
338 | } | |
339 | } | |
340 | ||
341 | return 0; | |
342 | } | |
343 | ||
344 | int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn, | |
345 | qbus_walkerfn *busfn, void *opaque) | |
346 | { | |
347 | BusState *bus; | |
348 | int err; | |
349 | ||
350 | if (devfn) { | |
351 | err = devfn(dev, opaque); | |
352 | if (err) { | |
353 | return err; | |
354 | } | |
355 | } | |
356 | ||
357 | QLIST_FOREACH(bus, &dev->child_bus, sibling) { | |
358 | err = qbus_walk_children(bus, devfn, busfn, opaque); | |
359 | if (err < 0) { | |
360 | return err; | |
361 | } | |
362 | } | |
363 | ||
364 | return 0; | |
365 | } | |
366 | ||
a2ee6b4f | 367 | DeviceState *qdev_find_recursive(BusState *bus, const char *id) |
3418bd25 GH |
368 | { |
369 | DeviceState *dev, *ret; | |
370 | BusState *child; | |
371 | ||
d8bb00d6 | 372 | QTAILQ_FOREACH(dev, &bus->children, sibling) { |
3418bd25 GH |
373 | if (dev->id && strcmp(dev->id, id) == 0) |
374 | return dev; | |
375 | QLIST_FOREACH(child, &dev->child_bus, sibling) { | |
376 | ret = qdev_find_recursive(child, id); | |
377 | if (ret) { | |
378 | return ret; | |
379 | } | |
380 | } | |
381 | } | |
382 | return NULL; | |
383 | } | |
384 | ||
cd739fb6 GH |
385 | void qbus_create_inplace(BusState *bus, BusInfo *info, |
386 | DeviceState *parent, const char *name) | |
02e2da45 | 387 | { |
d271de9f GH |
388 | char *buf; |
389 | int i,len; | |
02e2da45 | 390 | |
10c4c98a | 391 | bus->info = info; |
02e2da45 | 392 | bus->parent = parent; |
d271de9f GH |
393 | |
394 | if (name) { | |
395 | /* use supplied name */ | |
7267c094 | 396 | bus->name = g_strdup(name); |
d271de9f GH |
397 | } else if (parent && parent->id) { |
398 | /* parent device has id -> use it for bus name */ | |
399 | len = strlen(parent->id) + 16; | |
7267c094 | 400 | buf = g_malloc(len); |
d271de9f GH |
401 | snprintf(buf, len, "%s.%d", parent->id, parent->num_child_bus); |
402 | bus->name = buf; | |
403 | } else { | |
404 | /* no id -> use lowercase bus type for bus name */ | |
405 | len = strlen(info->name) + 16; | |
7267c094 | 406 | buf = g_malloc(len); |
d271de9f GH |
407 | len = snprintf(buf, len, "%s.%d", info->name, |
408 | parent ? parent->num_child_bus : 0); | |
409 | for (i = 0; i < len; i++) | |
bb87ece5 | 410 | buf[i] = qemu_tolower(buf[i]); |
d271de9f GH |
411 | bus->name = buf; |
412 | } | |
413 | ||
d8bb00d6 | 414 | QTAILQ_INIT(&bus->children); |
02e2da45 | 415 | if (parent) { |
72cf2d4f | 416 | QLIST_INSERT_HEAD(&parent->child_bus, bus, sibling); |
d271de9f | 417 | parent->num_child_bus++; |
80376c3f IY |
418 | } else if (bus != main_system_bus) { |
419 | /* TODO: once all bus devices are qdevified, | |
420 | only reset handler for main_system_bus should be registered here. */ | |
421 | qemu_register_reset(qbus_reset_all_fn, bus); | |
02e2da45 | 422 | } |
cd739fb6 GH |
423 | } |
424 | ||
425 | BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name) | |
426 | { | |
427 | BusState *bus; | |
428 | ||
7267c094 | 429 | bus = g_malloc0(info->size); |
cd739fb6 GH |
430 | bus->qdev_allocated = 1; |
431 | qbus_create_inplace(bus, info, parent, name); | |
02e2da45 PB |
432 | return bus; |
433 | } | |
cae4956e | 434 | |
2da8bb92 IY |
435 | static void main_system_bus_create(void) |
436 | { | |
437 | /* assign main_system_bus before qbus_create_inplace() | |
438 | * in order to make "if (bus != main_system_bus)" work */ | |
7267c094 | 439 | main_system_bus = g_malloc0(system_bus_info.size); |
2da8bb92 IY |
440 | main_system_bus->qdev_allocated = 1; |
441 | qbus_create_inplace(main_system_bus, &system_bus_info, NULL, | |
442 | "main-system-bus"); | |
443 | } | |
444 | ||
131ec1bd GH |
445 | void qbus_free(BusState *bus) |
446 | { | |
447 | DeviceState *dev; | |
448 | ||
d8bb00d6 | 449 | while ((dev = QTAILQ_FIRST(&bus->children)) != NULL) { |
131ec1bd GH |
450 | qdev_free(dev); |
451 | } | |
452 | if (bus->parent) { | |
453 | QLIST_REMOVE(bus, sibling); | |
454 | bus->parent->num_child_bus--; | |
80376c3f IY |
455 | } else { |
456 | assert(bus != main_system_bus); /* main_system_bus is never freed */ | |
457 | qemu_unregister_reset(qbus_reset_all_fn, bus); | |
131ec1bd | 458 | } |
7267c094 | 459 | g_free((void*)bus->name); |
131ec1bd | 460 | if (bus->qdev_allocated) { |
7267c094 | 461 | g_free(bus); |
131ec1bd GH |
462 | } |
463 | } | |
464 | ||
1ca4d09a GN |
465 | static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size) |
466 | { | |
467 | int l = 0; | |
468 | ||
469 | if (dev && dev->parent_bus) { | |
470 | char *d; | |
471 | l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size); | |
472 | if (dev->parent_bus->info->get_fw_dev_path) { | |
473 | d = dev->parent_bus->info->get_fw_dev_path(dev); | |
474 | l += snprintf(p + l, size - l, "%s", d); | |
7267c094 | 475 | g_free(d); |
1ca4d09a | 476 | } else { |
f79f2bfc | 477 | l += snprintf(p + l, size - l, "%s", object_get_typename(OBJECT(dev))); |
1ca4d09a GN |
478 | } |
479 | } | |
480 | l += snprintf(p + l , size - l, "/"); | |
481 | ||
482 | return l; | |
483 | } | |
484 | ||
485 | char* qdev_get_fw_dev_path(DeviceState *dev) | |
486 | { | |
487 | char path[128]; | |
488 | int l; | |
489 | ||
490 | l = qdev_get_fw_dev_path_helper(dev, path, 128); | |
491 | ||
492 | path[l-1] = '\0'; | |
493 | ||
494 | return strdup(path); | |
495 | } | |
85ed303b | 496 | |
09e5ab63 AL |
497 | char *qdev_get_dev_path(DeviceState *dev) |
498 | { | |
499 | BusInfo *businfo; | |
500 | ||
501 | if (!dev || !dev->parent_bus) { | |
502 | return NULL; | |
503 | } | |
504 | ||
505 | businfo = dev->parent_bus->info; | |
506 | if (businfo->get_dev_path) { | |
507 | return businfo->get_dev_path(dev); | |
508 | } | |
509 | ||
510 | return NULL; | |
511 | } | |
512 | ||
a5296ca9 AL |
513 | /** |
514 | * Legacy property handling | |
515 | */ | |
516 | ||
57c9fafe | 517 | static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque, |
a5296ca9 AL |
518 | const char *name, Error **errp) |
519 | { | |
57c9fafe | 520 | DeviceState *dev = DEVICE(obj); |
a5296ca9 AL |
521 | Property *prop = opaque; |
522 | ||
e3cb6ba6 PB |
523 | char buffer[1024]; |
524 | char *ptr = buffer; | |
a5296ca9 | 525 | |
e3cb6ba6 PB |
526 | prop->info->print(dev, prop, buffer, sizeof(buffer)); |
527 | visit_type_str(v, &ptr, name, errp); | |
a5296ca9 AL |
528 | } |
529 | ||
57c9fafe | 530 | static void qdev_set_legacy_property(Object *obj, Visitor *v, void *opaque, |
a5296ca9 AL |
531 | const char *name, Error **errp) |
532 | { | |
57c9fafe | 533 | DeviceState *dev = DEVICE(obj); |
a5296ca9 | 534 | Property *prop = opaque; |
e3cb6ba6 PB |
535 | Error *local_err = NULL; |
536 | char *ptr = NULL; | |
537 | int ret; | |
a5296ca9 AL |
538 | |
539 | if (dev->state != DEV_STATE_CREATED) { | |
540 | error_set(errp, QERR_PERMISSION_DENIED); | |
541 | return; | |
542 | } | |
543 | ||
e3cb6ba6 PB |
544 | visit_type_str(v, &ptr, name, &local_err); |
545 | if (local_err) { | |
546 | error_propagate(errp, local_err); | |
547 | return; | |
548 | } | |
a5296ca9 | 549 | |
e3cb6ba6 | 550 | ret = prop->info->parse(dev, prop, ptr); |
7db4c4e8 | 551 | error_set_from_qdev_prop_error(errp, ret, dev, prop, ptr); |
e3cb6ba6 | 552 | g_free(ptr); |
a5296ca9 AL |
553 | } |
554 | ||
555 | /** | |
556 | * @qdev_add_legacy_property - adds a legacy property | |
557 | * | |
558 | * Do not use this is new code! Properties added through this interface will | |
ca2cc788 | 559 | * be given names and types in the "legacy" namespace. |
a5296ca9 | 560 | * |
68ee3569 PB |
561 | * Legacy properties are string versions of other OOM properties. The format |
562 | * of the string depends on the property type. | |
a5296ca9 AL |
563 | */ |
564 | void qdev_property_add_legacy(DeviceState *dev, Property *prop, | |
565 | Error **errp) | |
566 | { | |
ca2cc788 | 567 | gchar *name, *type; |
a5296ca9 | 568 | |
f3be016d AL |
569 | /* Register pointer properties as legacy properties */ |
570 | if (!prop->info->print && !prop->info->parse && | |
571 | (prop->info->set || prop->info->get)) { | |
68ee3569 PB |
572 | return; |
573 | } | |
f3be016d | 574 | |
ca2cc788 | 575 | name = g_strdup_printf("legacy-%s", prop->name); |
cafe5bdb PB |
576 | type = g_strdup_printf("legacy<%s>", |
577 | prop->info->legacy_name ?: prop->info->name); | |
a5296ca9 | 578 | |
57c9fafe | 579 | object_property_add(OBJECT(dev), name, type, |
68ee3569 PB |
580 | prop->info->print ? qdev_get_legacy_property : prop->info->get, |
581 | prop->info->parse ? qdev_set_legacy_property : prop->info->set, | |
57c9fafe AL |
582 | NULL, |
583 | prop, errp); | |
a5296ca9 AL |
584 | |
585 | g_free(type); | |
ca2cc788 PB |
586 | g_free(name); |
587 | } | |
588 | ||
589 | /** | |
590 | * @qdev_property_add_static - add a @Property to a device. | |
591 | * | |
592 | * Static properties access data in a struct. The actual type of the | |
593 | * property and the field depends on the property type. | |
594 | */ | |
595 | void qdev_property_add_static(DeviceState *dev, Property *prop, | |
596 | Error **errp) | |
597 | { | |
fdae245f PB |
598 | Error *local_err = NULL; |
599 | Object *obj = OBJECT(dev); | |
600 | ||
d822979b PB |
601 | /* |
602 | * TODO qdev_prop_ptr does not have getters or setters. It must | |
603 | * go now that it can be replaced with links. The test should be | |
604 | * removed along with it: all static properties are read/write. | |
605 | */ | |
606 | if (!prop->info->get && !prop->info->set) { | |
607 | return; | |
608 | } | |
609 | ||
fdae245f | 610 | object_property_add(obj, prop->name, prop->info->name, |
57c9fafe | 611 | prop->info->get, prop->info->set, |
dd0ba250 | 612 | prop->info->release, |
fdae245f PB |
613 | prop, &local_err); |
614 | ||
615 | if (local_err) { | |
616 | error_propagate(errp, local_err); | |
617 | return; | |
618 | } | |
619 | if (prop->qtype == QTYPE_NONE) { | |
620 | return; | |
621 | } | |
622 | ||
623 | if (prop->qtype == QTYPE_QBOOL) { | |
624 | object_property_set_bool(obj, prop->defval, prop->name, &local_err); | |
625 | } else if (prop->info->enum_table) { | |
626 | object_property_set_str(obj, prop->info->enum_table[prop->defval], | |
627 | prop->name, &local_err); | |
628 | } else if (prop->qtype == QTYPE_QINT) { | |
629 | object_property_set_int(obj, prop->defval, prop->name, &local_err); | |
630 | } | |
631 | assert_no_error(local_err); | |
6a146eba | 632 | } |
1de81d28 | 633 | |
9674bfe4 AL |
634 | static void device_initfn(Object *obj) |
635 | { | |
636 | DeviceState *dev = DEVICE(obj); | |
bce54474 | 637 | ObjectClass *class; |
9674bfe4 AL |
638 | Property *prop; |
639 | ||
640 | if (qdev_hotplug) { | |
641 | dev->hotplugged = 1; | |
642 | qdev_hot_added = true; | |
643 | } | |
644 | ||
645 | dev->instance_id_alias = -1; | |
9674bfe4 AL |
646 | dev->state = DEV_STATE_CREATED; |
647 | ||
bce54474 PB |
648 | class = object_get_class(OBJECT(dev)); |
649 | do { | |
650 | for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) { | |
651 | qdev_property_add_legacy(dev, prop, NULL); | |
652 | qdev_property_add_static(dev, prop, NULL); | |
653 | } | |
bce54474 PB |
654 | class = object_class_get_parent(class); |
655 | } while (class != object_class_by_name(TYPE_DEVICE)); | |
4b3582b0 | 656 | qdev_prop_set_globals(dev); |
9674bfe4 AL |
657 | } |
658 | ||
60adba37 AL |
659 | /* Unlink device from bus and free the structure. */ |
660 | static void device_finalize(Object *obj) | |
661 | { | |
662 | DeviceState *dev = DEVICE(obj); | |
663 | BusState *bus; | |
60adba37 AL |
664 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
665 | ||
666 | if (dev->state == DEV_STATE_INITIALIZED) { | |
667 | while (dev->num_child_bus) { | |
668 | bus = QLIST_FIRST(&dev->child_bus); | |
669 | qbus_free(bus); | |
670 | } | |
671 | if (qdev_get_vmsd(dev)) { | |
672 | vmstate_unregister(dev, qdev_get_vmsd(dev), dev); | |
673 | } | |
674 | if (dc->exit) { | |
675 | dc->exit(dev); | |
676 | } | |
677 | if (dev->opts) { | |
678 | qemu_opts_del(dev->opts); | |
679 | } | |
680 | } | |
681 | QTAILQ_REMOVE(&dev->parent_bus->children, dev, sibling); | |
60adba37 AL |
682 | } |
683 | ||
bce54474 PB |
684 | static void device_class_base_init(ObjectClass *class, void *data) |
685 | { | |
686 | DeviceClass *klass = DEVICE_CLASS(class); | |
687 | ||
688 | /* We explicitly look up properties in the superclasses, | |
689 | * so do not propagate them to the subclasses. | |
690 | */ | |
691 | klass->props = NULL; | |
692 | } | |
693 | ||
94afdadc AL |
694 | void device_reset(DeviceState *dev) |
695 | { | |
696 | DeviceClass *klass = DEVICE_GET_CLASS(dev); | |
697 | ||
698 | if (klass->reset) { | |
699 | klass->reset(dev); | |
700 | } | |
701 | } | |
702 | ||
f05f6b4a PB |
703 | Object *qdev_get_machine(void) |
704 | { | |
705 | static Object *dev; | |
706 | ||
707 | if (dev == NULL) { | |
dfe47e70 | 708 | dev = container_get(object_get_root(), "/machine"); |
f05f6b4a PB |
709 | } |
710 | ||
711 | return dev; | |
712 | } | |
713 | ||
32fea402 AL |
714 | static TypeInfo device_type_info = { |
715 | .name = TYPE_DEVICE, | |
716 | .parent = TYPE_OBJECT, | |
717 | .instance_size = sizeof(DeviceState), | |
9674bfe4 | 718 | .instance_init = device_initfn, |
60adba37 | 719 | .instance_finalize = device_finalize, |
bce54474 | 720 | .class_base_init = device_class_base_init, |
32fea402 AL |
721 | .abstract = true, |
722 | .class_size = sizeof(DeviceClass), | |
723 | }; | |
724 | ||
83f7d43a | 725 | static void qdev_register_types(void) |
32fea402 AL |
726 | { |
727 | type_register_static(&device_type_info); | |
728 | } | |
729 | ||
83f7d43a | 730 | type_init(qdev_register_types) |