ivshmem-test: Clean up wait for devices to become operational
[qemu/kevin.git] / backends / rng.c
blobe57e2b4b52c8bef6794d229808634a246b1e1b48
1 /*
2 * QEMU Random Number Generator Backend
4 * Copyright IBM, Corp. 2012
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "sysemu/rng.h"
15 #include "qapi/qmp/qerror.h"
16 #include "qom/object_interfaces.h"
18 void rng_backend_request_entropy(RngBackend *s, size_t size,
19 EntropyReceiveFunc *receive_entropy,
20 void *opaque)
22 RngBackendClass *k = RNG_BACKEND_GET_CLASS(s);
23 RngRequest *req;
25 if (k->request_entropy) {
26 req = g_malloc(sizeof(*req));
28 req->offset = 0;
29 req->size = size;
30 req->receive_entropy = receive_entropy;
31 req->opaque = opaque;
32 req->data = g_malloc(req->size);
34 k->request_entropy(s, req);
36 QSIMPLEQ_INSERT_TAIL(&s->requests, req, next);
40 static bool rng_backend_prop_get_opened(Object *obj, Error **errp)
42 RngBackend *s = RNG_BACKEND(obj);
44 return s->opened;
47 static void rng_backend_complete(UserCreatable *uc, Error **errp)
49 object_property_set_bool(OBJECT(uc), true, "opened", errp);
52 static void rng_backend_prop_set_opened(Object *obj, bool value, Error **errp)
54 RngBackend *s = RNG_BACKEND(obj);
55 RngBackendClass *k = RNG_BACKEND_GET_CLASS(s);
56 Error *local_err = NULL;
58 if (value == s->opened) {
59 return;
62 if (!value && s->opened) {
63 error_setg(errp, QERR_PERMISSION_DENIED);
64 return;
67 if (k->opened) {
68 k->opened(s, &local_err);
69 if (local_err) {
70 error_propagate(errp, local_err);
71 return;
75 s->opened = true;
78 static void rng_backend_free_request(RngRequest *req)
80 g_free(req->data);
81 g_free(req);
84 static void rng_backend_free_requests(RngBackend *s)
86 RngRequest *req, *next;
88 QSIMPLEQ_FOREACH_SAFE(req, &s->requests, next, next) {
89 rng_backend_free_request(req);
92 QSIMPLEQ_INIT(&s->requests);
95 void rng_backend_finalize_request(RngBackend *s, RngRequest *req)
97 QSIMPLEQ_REMOVE(&s->requests, req, RngRequest, next);
98 rng_backend_free_request(req);
101 static void rng_backend_init(Object *obj)
103 RngBackend *s = RNG_BACKEND(obj);
105 QSIMPLEQ_INIT(&s->requests);
107 object_property_add_bool(obj, "opened",
108 rng_backend_prop_get_opened,
109 rng_backend_prop_set_opened,
110 NULL);
113 static void rng_backend_finalize(Object *obj)
115 RngBackend *s = RNG_BACKEND(obj);
117 rng_backend_free_requests(s);
120 static void rng_backend_class_init(ObjectClass *oc, void *data)
122 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
124 ucc->complete = rng_backend_complete;
127 static const TypeInfo rng_backend_info = {
128 .name = TYPE_RNG_BACKEND,
129 .parent = TYPE_OBJECT,
130 .instance_size = sizeof(RngBackend),
131 .instance_init = rng_backend_init,
132 .instance_finalize = rng_backend_finalize,
133 .class_size = sizeof(RngBackendClass),
134 .class_init = rng_backend_class_init,
135 .abstract = true,
136 .interfaces = (InterfaceInfo[]) {
137 { TYPE_USER_CREATABLE },
142 static void register_types(void)
144 type_register_static(&rng_backend_info);
147 type_init(register_types);