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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#![warn(missing_debug_implementations)]
#![warn(missing_docs)]
#![warn(rust_2018_idioms)]
#![deny(clippy::all)]
#![deny(clippy::pedantic)]
#![deny(clippy::nursery)]
#![deny(clippy::cargo)]
#![allow(clippy::cast_possible_wrap)]
#![allow(clippy::cast_sign_loss)]
use std::{convert::TryInto, num::TryFromIntError, os::unix::io::RawFd};
use ioctl::{
dma_buf_begin_cpu_read_access, dma_buf_begin_cpu_readwrite_access,
dma_buf_begin_cpu_write_access, dma_buf_end_cpu_read_access, dma_buf_end_cpu_readwrite_access,
dma_buf_end_cpu_write_access,
};
use log::debug;
use memmap::MmapMut;
use nix::sys::stat::fstat;
mod ioctl;
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("Closure Error")]
Closure,
#[error("Integer Conversion Error")]
IntegerCast(#[from] TryFromIntError),
#[error("System Error")]
System(#[from] nix::Error),
#[error("Io Error")]
MMap(#[from] std::io::Error),
}
#[derive(Debug)]
pub struct DmaBuf {
fd: RawFd,
}
impl DmaBuf {
pub fn memory_map(self) -> Result<MappedDmaBuf, Error> {
debug!("Mapping DMA-Buf buffer with File Descriptor {}", self.fd);
let stat = fstat(self.fd)?;
let len = stat.st_size.try_into()?;
debug!("Valid buffer, size {}", len);
let mmap = unsafe { MmapMut::map_mut(self.fd)? };
debug!("Memory Mapping Done");
Ok(MappedDmaBuf {
buf: self,
len,
mmap,
})
}
}
pub struct MappedDmaBuf {
buf: DmaBuf,
len: usize,
mmap: MmapMut,
}
impl MappedDmaBuf {
pub fn read<A, F, R>(&self, f: F, arg: Option<A>) -> Result<R, Error>
where
F: Fn(&[u8], Option<A>) -> Result<R, Error>,
{
debug!("Preparing the buffer for read access");
dma_buf_begin_cpu_read_access(self.buf.fd)?;
debug!("Accessing the buffer");
let ret = f(&self.mmap, arg);
if ret.is_ok() {
debug!("Closure done without error");
} else {
debug!("Closure encountered an error")
}
dma_buf_end_cpu_read_access(self.buf.fd)?;
debug!("Buffer access done");
ret
}
pub fn readwrite<A, F, R>(&mut self, f: F, arg: Option<A>) -> Result<R, Error>
where
F: Fn(&mut [u8], Option<A>) -> Result<R, Error>,
{
debug!("Preparing the buffer for read/write access");
dma_buf_begin_cpu_readwrite_access(self.buf.fd)?;
debug!("Accessing the buffer");
let ret = f(&mut self.mmap, arg);
if ret.is_ok() {
debug!("Closure done without error");
} else {
debug!("Closure encountered an error")
}
dma_buf_end_cpu_readwrite_access(self.buf.fd)?;
debug!("Buffer access done");
ret
}
pub fn write<A, F>(&mut self, f: F, arg: Option<A>) -> Result<(), Error>
where
F: Fn(&mut [u8], Option<A>) -> Result<(), Error>,
{
debug!("Preparing the buffer for write access");
dma_buf_begin_cpu_write_access(self.buf.fd)?;
debug!("Accessing the buffer");
let ret = f(&mut self.mmap, arg);
if ret.is_ok() {
debug!("Closure done without error");
} else {
debug!("Closure encountered an error")
}
dma_buf_end_cpu_write_access(self.buf.fd)?;
debug!("Buffer access done");
ret
}
}
impl std::os::unix::io::AsRawFd for DmaBuf {
fn as_raw_fd(&self) -> RawFd {
self.fd
}
}
impl std::os::unix::io::AsRawFd for MappedDmaBuf {
fn as_raw_fd(&self) -> RawFd {
self.buf.fd
}
}
impl std::os::unix::io::FromRawFd for DmaBuf {
unsafe fn from_raw_fd(fd: RawFd) -> Self {
debug!("Importing DMABuf from File Descriptor {}", fd);
Self { fd }
}
}
impl std::fmt::Debug for MappedDmaBuf {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("MappedDmaBuf")
.field("DmaBuf", &self.buf)
.field("len", &self.len)
.field("address", &self.mmap.as_ptr())
.finish()
}
}
impl Drop for DmaBuf {
fn drop(&mut self) {
debug!("Closing buffer {}", self.fd);
nix::unistd::close(self.fd).unwrap();
}
}