Skip to content

Commit 8a15975

Browse files
WhatAmISupposedToPutHerejannau
authored andcommitted
rust: io: Add helper for memcpy_toio
Signed-off-by: Sasha Finkelstein <fnkl.kernel@gmail.com> Signed-off-by: Janne Grunau <j@jannau.net>
1 parent 92fe4db commit 8a15975

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

rust/helpers/io.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ void rust_helper_memcpy_fromio(void *to, const void __iomem *from, long count)
2323
memcpy_fromio(to, from, count);
2424
}
2525

26+
void rust_helper_memcpy_toio(void __iomem *to, const void *from, size_t count)
27+
{
28+
memcpy_toio(to, from, count);
29+
}
30+
2631
u8 rust_helper_readb(const void __iomem *addr)
2732
{
2833
return readb(addr);

rust/kernel/io.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,27 @@ impl<const SIZE: usize> Io<SIZE> {
298298
Ok(())
299299
}
300300

301+
/// Copy memory block to i/o memory from the specified buffer.
302+
pub fn try_memcpy_toio(&self, offset: usize, buffer: &[u8]) -> Result {
303+
if buffer.len() == 0 || !Self::length_valid(offset, buffer.len(), self.maxsize()) {
304+
return Err(EINVAL);
305+
}
306+
// no need to check since offset + buffer.len() - 1 is valid
307+
let addr = self.io_addr::<crate::ffi::c_char>(offset)?;
308+
309+
// SAFETY:
310+
// - The type invariants guarantee that `adr` is a valid pointer.
311+
// - The bounds of `buffer` are checked with a call to `length_valid`.
312+
unsafe {
313+
bindings::memcpy_toio(
314+
addr as *mut _,
315+
buffer.as_ptr() as *const _,
316+
buffer.len() as _,
317+
)
318+
};
319+
Ok(())
320+
}
321+
301322
define_read!(read8, try_read8, readb -> u8);
302323
define_read!(read16, try_read16, readw -> u16);
303324
define_read!(read32, try_read32, readl -> u32);

0 commit comments

Comments
 (0)