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