Skip to content

Commit d5e3862

Browse files
author
Maciej Falkowski
committed
rust: platdev: add platform_ioremap_resource function
Signed-off-by: Maciej Falkowski <[email protected]>
1 parent 4a13570 commit d5e3862

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

rust/kernel/error.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -488,17 +488,12 @@ pub(crate) use from_kernel_result;
488488
/// # use kernel::from_kernel_err_ptr;
489489
/// # use kernel::c_types;
490490
/// # use kernel::bindings;
491-
/// fn devm_platform_ioremap_resource(
491+
/// pub fn devm_platform_ioremap_resource(
492492
/// pdev: &mut PlatformDevice,
493493
/// index: u32,
494494
/// ) -> Result<*mut c_types::c_void> {
495-
/// // SAFETY: FFI call.
496-
/// unsafe {
497-
/// from_kernel_err_ptr(bindings::devm_platform_ioremap_resource(
498-
/// pdev.to_ptr(),
499-
/// index,
500-
/// ))
501-
/// }
495+
/// // SAFETY: `pdev` is valid by the type invariant.
496+
/// unsafe { from_kernel_err_ptr(bindings::devm_platform_ioremap_resource(pdev.0, index)) }
502497
/// }
503498
/// ```
504499
// TODO: remove `dead_code` marker once an in-kernel client is available.

rust/kernel/io_mem.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
77
#![allow(dead_code)]
88

9-
use crate::{bindings, Error, Result};
9+
use crate::{
10+
bindings
11+
platdev::{self, PlatformDevice},
12+
Error, Result,
13+
};
1014
use core::convert::TryInto;
1115

1216
/// Represents a memory resource.
@@ -164,6 +168,14 @@ impl<const SIZE: usize> IoMem<SIZE> {
164168
}
165169
}
166170

171+
// TODO: Handle Drop properly
172+
pub fn try_platform_ioremap_resource(pdev: &mut PlatformDevice, index: u32) -> Result<Self> {
173+
let raw_ptr = platdev::devm_platform_ioremap_resource(pdev, index)?;
174+
Ok(Self {
175+
ptr: raw_ptr as usize,
176+
})
177+
}
178+
167179
const fn offset_ok<T>(offset: usize) -> bool {
168180
let type_size = core::mem::size_of::<T>();
169181
if let Some(end) = offset.checked_add(type_size) {

rust/kernel/platdev.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
99
use crate::{
1010
bindings, c_types, device,
11-
error::{from_kernel_result, Error, Result},
11+
error::{from_kernel_err_ptr, from_kernel_result, Error, Result},
1212
of::OfMatchTable,
1313
str::CStr,
1414
types::PointerWrapper,
@@ -32,6 +32,15 @@ unsafe impl device::RawDevice for PlatformDevice {
3232
}
3333
}
3434

35+
/// Ioremaps resources of a platform device.
36+
pub fn devm_platform_ioremap_resource(
37+
pdev: &mut PlatformDevice,
38+
index: u32,
39+
) -> Result<*mut c_types::c_void> {
40+
// SAFETY: `pdev` is valid by the type invariant.
41+
unsafe { from_kernel_err_ptr(bindings::devm_platform_ioremap_resource(pdev.0, index)) }
42+
}
43+
3544
/// A registration of a platform device.
3645
#[derive(Default)]
3746
pub struct Registration {

0 commit comments

Comments
 (0)