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