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