Skip to content

Commit 957c64f

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 2cbcfb9 commit 957c64f

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
@@ -273,6 +273,27 @@ impl<const SIZE: usize> Io<SIZE> {
273273
Ok(())
274274
}
275275

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

0 commit comments

Comments
 (0)