]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
rust: platform: add resource accessors
authorDaniel Almeida <daniel.almeida@collabora.com>
Thu, 17 Jul 2025 15:55:24 +0000 (12:55 -0300)
committerDanilo Krummrich <dakr@kernel.org>
Sun, 20 Jul 2025 17:43:14 +0000 (19:43 +0200)
The previous patches have added the abstractions for Resources and the
ability to map them and therefore read and write the underlying memory .

The only thing missing to make this accessible for platform devices is
to provide accessors that return instances of IoRequest<'a>. These
ensure that the resource are valid only for the lifetime of the platform
device, and that the platform device is in the Bound state.

Therefore, add these accessors. Also make it possible to retrieve
resources from platform devices in Rust using either a name or an index.

Acked-by: Miguel Ojeda <ojeda@kernel.org>
Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
Link: https://lore.kernel.org/r/20250717-topics-tyr-platform_iomem-v15-3-beca780b77e3@collabora.com
[ Remove #[expect(dead_code)] from IoRequest::new() and move SAFETY
  comments right on top of unsafe blocks to avoid clippy warnings for
  some (older) clippy versions. - Danilo ]
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
rust/kernel/io/mem.rs
rust/kernel/platform.rs

index 8e4dfd79079e6eb9e8dad19b3c4771910c7f3e5f..50d4ec3eb62378d991530803d4b12beb8c491094 100644 (file)
@@ -28,7 +28,6 @@ impl<'a> IoRequest<'a> {
     ///
     /// Callers must ensure that `resource` is valid for `device` during the
     /// lifetime `'a`.
-    #[expect(dead_code)]
     pub(crate) unsafe fn new(device: &'a Device<Bound>, resource: &'a Resource) -> Self {
         IoRequest { device, resource }
     }
index 3c0c507c243266b78c534d46f5a0360a5ca7b62e..b4d3087aff52fe196fc72b14050b1aca7200d37b 100644 (file)
@@ -5,8 +5,11 @@
 //! C header: [`include/linux/platform_device.h`](srctree/include/linux/platform_device.h)
 
 use crate::{
-    acpi, bindings, container_of, device, driver,
+    acpi, bindings, container_of,
+    device::{self, Bound},
+    driver,
     error::{from_result, to_result, Result},
+    io::{mem::IoRequest, Resource},
     of,
     prelude::*,
     types::Opaque,
@@ -224,6 +227,61 @@ impl<Ctx: device::DeviceContext> Device<Ctx> {
     fn as_raw(&self) -> *mut bindings::platform_device {
         self.0.get()
     }
+
+    /// Returns the resource at `index`, if any.
+    pub fn resource_by_index(&self, index: u32) -> Option<&Resource> {
+        // SAFETY: `self.as_raw()` returns a valid pointer to a `struct platform_device`.
+        let resource = unsafe {
+            bindings::platform_get_resource(self.as_raw(), bindings::IORESOURCE_MEM, index)
+        };
+
+        if resource.is_null() {
+            return None;
+        }
+
+        // SAFETY: `resource` is a valid pointer to a `struct resource` as
+        // returned by `platform_get_resource`.
+        Some(unsafe { Resource::from_raw(resource) })
+    }
+
+    /// Returns the resource with a given `name`, if any.
+    pub fn resource_by_name(&self, name: &CStr) -> Option<&Resource> {
+        // SAFETY: `self.as_raw()` returns a valid pointer to a `struct
+        // platform_device` and `name` points to a valid C string.
+        let resource = unsafe {
+            bindings::platform_get_resource_byname(
+                self.as_raw(),
+                bindings::IORESOURCE_MEM,
+                name.as_char_ptr(),
+            )
+        };
+
+        if resource.is_null() {
+            return None;
+        }
+
+        // SAFETY: `resource` is a valid pointer to a `struct resource` as
+        // returned by `platform_get_resource`.
+        Some(unsafe { Resource::from_raw(resource) })
+    }
+}
+
+impl Device<Bound> {
+    /// Returns an `IoRequest` for the resource at `index`, if any.
+    pub fn io_request_by_index(&self, index: u32) -> Option<IoRequest<'_>> {
+        self.resource_by_index(index)
+            // SAFETY: `resource` is a valid resource for `&self` during the
+            // lifetime of the `IoRequest`.
+            .map(|resource| unsafe { IoRequest::new(self.as_ref(), resource) })
+    }
+
+    /// Returns an `IoRequest` for the resource with a given `name`, if any.
+    pub fn io_request_by_name(&self, name: &CStr) -> Option<IoRequest<'_>> {
+        self.resource_by_name(name)
+            // SAFETY: `resource` is a valid resource for `&self` during the
+            // lifetime of the `IoRequest`.
+            .map(|resource| unsafe { IoRequest::new(self.as_ref(), resource) })
+    }
 }
 
 // SAFETY: `Device` is a transparent wrapper of a type that doesn't depend on `Device`'s generic