Skip to content

Commit dbac87c

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 b40a3e8 commit dbac87c

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
@@ -270,6 +270,27 @@ impl<const SIZE: usize> Io<SIZE> {
270270
Ok(())
271271
}
272272

273+
/// Copy memory block to i/o memory from the specified buffer.
274+
pub fn try_memcpy_toio(&self, offset: usize, buffer: &[u8]) -> Result {
275+
if buffer.len() == 0 || !Self::length_valid(offset, buffer.len(), self.maxsize()) {
276+
return Err(EINVAL);
277+
}
278+
// no need to check since offset + buffer.len() - 1 is valid
279+
let addr = self.io_addr::<crate::ffi::c_char>(offset)?;
280+
281+
// SAFETY:
282+
// - The type invariants guarantee that `adr` is a valid pointer.
283+
// - The bounds of `buffer` are checked with a call to `length_valid`.
284+
unsafe {
285+
bindings::memcpy_toio(
286+
addr as *mut _,
287+
buffer.as_ptr() as *const _,
288+
buffer.len() as _,
289+
)
290+
};
291+
Ok(())
292+
}
293+
273294
define_read!(read8, try_read8, readb -> u8);
274295
define_read!(read16, try_read16, readw -> u16);
275296
define_read!(read32, try_read32, readl -> u32);

0 commit comments

Comments
 (0)