Merge remote-tracking branch 'remotes/vivier/tags/m68k-for-2.12-pull-request' into...
[qemu/ar7.git] / backends / rng.c
blob398ebe4a7d09f9eabe0a11e3cff7f52d60016702
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/error.h"
16 #include "qapi/qmp/qerror.h"
17 #include "qom/object_interfaces.h"
19 void rng_backend_request_entropy(RngBackend *s, size_t size,
20 EntropyReceiveFunc *receive_entropy,
21 void *opaque)
23 RngBackendClass *k = RNG_BACKEND_GET_CLASS(s);
24 RngRequest *req;
26 if (k->request_entropy) {
27 req = g_malloc(sizeof(*req));
29 req->offset = 0;
30 req->size = size;
31 req->receive_entropy = receive_entropy;
32 req->opaque = opaque;
33 req->data = g_malloc(req->size);
35 k->request_entropy(s, req);
37 QSIMPLEQ_INSERT_TAIL(&s->requests, req, next);
41 static bool rng_backend_prop_get_opened(Object *obj, Error **errp)
43 RngBackend *s = RNG_BACKEND(obj);
45 return s->opened;
48 static void rng_backend_complete(UserCreatable *uc, Error **errp)
50 object_property_set_bool(OBJECT(uc), true, "opened", errp);
53 static void rng_backend_prop_set_opened(Object *obj, bool value, Error **errp)
55 RngBackend *s = RNG_BACKEND(obj);
56 RngBackendClass *k = RNG_BACKEND_GET_CLASS(s);
57 Error *local_err = NULL;
59 if (value == s->opened) {
60 return;
63 if (!value && s->opened) {
64 error_setg(errp, QERR_PERMISSION_DENIED);
65 return;
68 if (k->opened) {
69 k->opened(s, &local_err);
70 if (local_err) {
71 error_propagate(errp, local_err);
72 return;
76 s->opened = true;
79 static void rng_backend_free_request(RngRequest *req)
81 g_free(req->data);
82 g_free(req);
85 static void rng_backend_free_requests(RngBackend *s)
87 RngRequest *req, *next;
89 QSIMPLEQ_FOREACH_SAFE(req, &s->requests, next, next) {
90 rng_backend_free_request(req);
93 QSIMPLEQ_INIT(&s->requests);
96 void rng_backend_finalize_request(RngBackend *s, RngRequest *req)
98 QSIMPLEQ_REMOVE(&s->requests, req, RngRequest, next);
99 rng_backend_free_request(req);
102 static void rng_backend_init(Object *obj)
104 RngBackend *s = RNG_BACKEND(obj);
106 QSIMPLEQ_INIT(&s->requests);
108 object_property_add_bool(obj, "opened",
109 rng_backend_prop_get_opened,
110 rng_backend_prop_set_opened,
111 NULL);
114 static void rng_backend_finalize(Object *obj)
116 RngBackend *s = RNG_BACKEND(obj);
118 rng_backend_free_requests(s);
121 static void rng_backend_class_init(ObjectClass *oc, void *data)
123 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
125 ucc->complete = rng_backend_complete;
128 static const TypeInfo rng_backend_info = {
129 .name = TYPE_RNG_BACKEND,
130 .parent = TYPE_OBJECT,
131 .instance_size = sizeof(RngBackend),
132 .instance_init = rng_backend_init,
133 .instance_finalize = rng_backend_finalize,
134 .class_size = sizeof(RngBackendClass),
135 .class_init = rng_backend_class_init,
136 .abstract = true,
137 .interfaces = (InterfaceInfo[]) {
138 { TYPE_USER_CREATABLE },
143 static void register_types(void)
145 type_register_static(&rng_backend_info);
148 type_init(register_types);