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