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