Skip to content

Commit e13b84f

Browse files
author
Maciej Falkowski
committed
rust: platform: add ioremap_resource and get_resource methods
This patch adds a logic similar to `devm_platform_ioremap_resource` function adding: - `IoResource` enumerated type that groups the `IORESOURCE_*` macros. - `get_resource()` method that is a binding of `platform_get_resource` - `ioremap_resource` that is newly written method similar to `devm_platform_ioremap_resource`. Signed-off-by: Maciej Falkowski <[email protected]>
1 parent 97a7383 commit e13b84f

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

rust/kernel/io_mem.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
use crate::{bindings, Error, Result};
1010
use core::convert::TryInto;
1111

12+
/// The type of `Resource`.
13+
pub enum IoResource {
14+
/// i/o memory
15+
Mem = bindings::IORESOURCE_MEM as _,
16+
}
17+
1218
/// Represents a memory resource.
1319
pub struct Resource {
1420
offset: bindings::resource_size_t,

rust/kernel/platform.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use crate::{
1010
bindings, c_types,
1111
device::{self, RawDevice},
1212
driver,
13-
error::{from_kernel_result, Result},
13+
error::{from_kernel_result, Error, Result},
14+
io_mem::{IoMem, IoResource, Resource},
1415
of,
1516
str::CStr,
1617
to_result,
@@ -180,6 +181,36 @@ impl Device {
180181
// SAFETY: By the type invariants, we know that `self.ptr` is non-null and valid.
181182
unsafe { (*self.ptr).id }
182183
}
184+
185+
/// Gets a system resources of a platform device.
186+
pub fn get_resource(&self, rtype: IoResource, num: usize) -> Option<Resource> {
187+
// SAFETY: `self.ptr` is valid by the type invariant.
188+
let res = unsafe { bindings::platform_get_resource(self.ptr, rtype as _, num as _) };
189+
if res.is_null() {
190+
return None;
191+
}
192+
193+
// SAFETY: The pointer `res` is returned from `bindings::platform_get_resource`
194+
// above and checked if it is not a NULL.
195+
unsafe { Resource::new((*res).start, (*res).end) }
196+
}
197+
198+
/// Ioremaps resources of a platform device.
199+
///
200+
/// # Safety
201+
///
202+
/// Satisfy a safety requirements of the `IoMem<SIZE>::try_new` method.
203+
/// Callers must ensure that either (a) the resulting interface cannot be used to initiate DMA
204+
/// operations, or (b) that DMA operations initiated via the returned interface use DMA handles
205+
/// allocated through the `dma` module.
206+
pub unsafe fn ioremap_resource<const SIZE: usize>(&self, index: usize) -> Result<IoMem<SIZE>> {
207+
if let Some(res) = self.get_resource(IoResource::Mem, index) {
208+
// SAFETY: Valid by the safety contract.
209+
unsafe { IoMem::<SIZE>::try_new(res) }
210+
} else {
211+
Err(Error::EINVAL)
212+
}
213+
}
183214
}
184215

185216
// SAFETY: The device returned by `raw_device` is the raw platform device.

0 commit comments

Comments
 (0)