Merge branch 'master' into raspi
[qemu/ar7.git] / backends / rng.c
blobb7820ef4717defabda59dadb04b5c3159227c57d
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);
24 if (k->request_entropy) {
25 k->request_entropy(s, size, receive_entropy, opaque);
29 void rng_backend_cancel_requests(RngBackend *s)
31 RngBackendClass *k = RNG_BACKEND_GET_CLASS(s);
33 if (k->cancel_requests) {
34 k->cancel_requests(s);
38 static bool rng_backend_prop_get_opened(Object *obj, Error **errp)
40 RngBackend *s = RNG_BACKEND(obj);
42 return s->opened;
45 static void rng_backend_complete(UserCreatable *uc, Error **errp)
47 object_property_set_bool(OBJECT(uc), true, "opened", errp);
50 static void rng_backend_prop_set_opened(Object *obj, bool value, Error **errp)
52 RngBackend *s = RNG_BACKEND(obj);
53 RngBackendClass *k = RNG_BACKEND_GET_CLASS(s);
54 Error *local_err = NULL;
56 if (value == s->opened) {
57 return;
60 if (!value && s->opened) {
61 error_setg(errp, QERR_PERMISSION_DENIED);
62 return;
65 if (k->opened) {
66 k->opened(s, &local_err);
67 if (local_err) {
68 error_propagate(errp, local_err);
69 return;
73 s->opened = true;
76 static void rng_backend_init(Object *obj)
78 object_property_add_bool(obj, "opened",
79 rng_backend_prop_get_opened,
80 rng_backend_prop_set_opened,
81 NULL);
84 static void rng_backend_class_init(ObjectClass *oc, void *data)
86 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
88 ucc->complete = rng_backend_complete;
91 static const TypeInfo rng_backend_info = {
92 .name = TYPE_RNG_BACKEND,
93 .parent = TYPE_OBJECT,
94 .instance_size = sizeof(RngBackend),
95 .instance_init = rng_backend_init,
96 .class_size = sizeof(RngBackendClass),
97 .class_init = rng_backend_class_init,
98 .abstract = true,
99 .interfaces = (InterfaceInfo[]) {
100 { TYPE_USER_CREATABLE },
105 static void register_types(void)
107 type_register_static(&rng_backend_info);
110 type_init(register_types);