1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use alloc::{boxed::Box, slice::from_raw_parts_mut};
use crate::{
bindings, error::code::*, error::from_kernel_result, str::CString, to_result,
types::ForeignOwnable, Result, ScopeGuard,
};
use macros::vtable;
use core::{cell::UnsafeCell, fmt, marker::PhantomData, pin::Pin};
#[vtable]
pub trait Operations {
type Data: ForeignOwnable + Send + Sync = ();
fn init(_data: <Self::Data as ForeignOwnable>::Borrowed<'_>) -> Result {
Err(EINVAL)
}
fn cleanup(_data: Self::Data) {}
fn read(
data: <Self::Data as ForeignOwnable>::Borrowed<'_>,
buffer: &mut [u8],
wait: bool,
) -> Result<u32>;
}
pub struct Registration<T: Operations> {
hwrng: UnsafeCell<bindings::hwrng>,
name: Option<CString>,
registered: bool,
_p: PhantomData<T>,
}
impl<T: Operations> Registration<T> {
pub fn new() -> Self {
Self {
hwrng: UnsafeCell::new(bindings::hwrng::default()),
name: None,
registered: false,
_p: PhantomData,
}
}
pub fn new_pinned(
name: fmt::Arguments<'_>,
quality: u16,
data: T::Data,
) -> Result<Pin<Box<Self>>> {
let mut reg = Pin::from(Box::try_new(Self::new())?);
reg.as_mut().register(name, quality, data)?;
Ok(reg)
}
pub fn register(
self: Pin<&mut Self>,
name: fmt::Arguments<'_>,
quality: u16,
data: T::Data,
) -> Result {
let this = unsafe { self.get_unchecked_mut() };
if this.registered {
return Err(EINVAL);
}
let data_pointer = data.into_foreign();
let guard = ScopeGuard::new(|| unsafe {
T::Data::from_foreign(data_pointer);
});
let name = CString::try_from_fmt(name)?;
Self::init_hwrng(
unsafe { &mut *this.hwrng.get() },
&name,
quality,
data_pointer,
);
to_result(unsafe { bindings::hwrng_register(this.hwrng.get()) })?;
this.registered = true;
this.name = Some(name);
guard.dismiss();
Ok(())
}
fn init_hwrng(
hwrng: &mut bindings::hwrng,
name: &CString,
quality: u16,
data: *const core::ffi::c_void,
) {
hwrng.name = name.as_char_ptr();
hwrng.init = if T::HAS_INIT {
Some(Self::init_callback)
} else {
None
};
hwrng.cleanup = if T::HAS_CLEANUP {
Some(Self::cleanup_callback)
} else {
None
};
hwrng.data_present = None;
hwrng.data_read = None;
hwrng.read = Some(Self::read_callback);
hwrng.priv_ = data as _;
hwrng.quality = quality;
}
unsafe extern "C" fn init_callback(rng: *mut bindings::hwrng) -> core::ffi::c_int {
from_kernel_result! {
let data = unsafe { T::Data::borrow((*rng).priv_ as *const _) };
T::init(data)?;
Ok(0)
}
}
unsafe extern "C" fn cleanup_callback(rng: *mut bindings::hwrng) {
let data = unsafe { T::Data::from_foreign((*rng).priv_ as *const _) };
T::cleanup(data);
}
unsafe extern "C" fn read_callback(
rng: *mut bindings::hwrng,
data: *mut core::ffi::c_void,
max: usize,
wait: bindings::bool_,
) -> core::ffi::c_int {
from_kernel_result! {
let drv_data = unsafe { T::Data::borrow((*rng).priv_ as *const _) };
let buffer = unsafe { from_raw_parts_mut(data as *mut u8, max) };
let ret = T::read(drv_data, buffer, wait)?;
Ok(ret as _)
}
}
}
impl<T: Operations> Default for Registration<T> {
fn default() -> Self {
Self::new()
}
}
unsafe impl<T: Operations> Sync for Registration<T> {}
#[allow(clippy::non_send_fields_in_send_ty)]
unsafe impl<T: Operations> Send for Registration<T> {}
impl<T: Operations> Drop for Registration<T> {
fn drop(&mut self) {
if self.registered {
unsafe { bindings::hwrng_unregister(self.hwrng.get()) };
}
}
}