diff mbox series

[1/3] rust: device: implement FwNode::is_compatible()

Message ID 20250616194439.68775-1-dakr@kernel.org
State New
Headers show
Series [1/3] rust: device: implement FwNode::is_compatible() | expand

Commit Message

Danilo Krummrich June 16, 2025, 7:40 p.m. UTC
Implement FwNode::is_compatible() to check whether a given match string
is compatible with a FwNode.

Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
 rust/helpers/property.c        | 6 ++++++
 rust/kernel/device/property.rs | 9 +++++++++
 2 files changed, 15 insertions(+)


base-commit: 2a1ea59de83bf367215e2a4dd9bf8bbd061349b3

Comments

Dirk Behme June 17, 2025, 7:10 a.m. UTC | #1
On 16/06/2025 21:40, Danilo Krummrich wrote:
> In SampleDriver::probe() don't call pdev.as_ref() repeatedly, instead
> introduce a dedicated &Device.
> 
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
> ---
>  samples/rust/rust_driver_platform.rs | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/samples/rust/rust_driver_platform.rs b/samples/rust/rust_driver_platform.rs
> index c0abf78d0683..000bb915af60 100644
> --- a/samples/rust/rust_driver_platform.rs
> +++ b/samples/rust/rust_driver_platform.rs
> @@ -32,13 +32,15 @@ fn probe(
>          pdev: &platform::Device<Core>,
>          info: Option<&Self::IdInfo>,
>      ) -> Result<Pin<KBox<Self>>> {
> -        dev_dbg!(pdev.as_ref(), "Probe Rust Platform driver sample.\n");
> +        let dev = pdev.as_ref();
> +
> +        dev_dbg!(dev, "Probe Rust Platform driver sample.\n");
>  
>          if let Some(info) = info {
> -            dev_info!(pdev.as_ref(), "Probed with info: '{}'.\n", info.0);
> +            dev_info!(dev, "Probed with info: '{}'.\n", info.0);
>          }
>  
> -        Self::properties_parse(pdev.as_ref())?;
> +        Self::properties_parse(dev)?;
>  
>          let drvdata = KBox::new(Self { pdev: pdev.into() }, GFP_KERNEL)?;

Yes!

Reviewed-by: Dirk Behme <dirk.behme@de.bosch.com>

Thanks,

Dirk
diff mbox series

Patch

diff --git a/rust/helpers/property.c b/rust/helpers/property.c
index 08f68e2dac4a..177b9ffd7ba4 100644
--- a/rust/helpers/property.c
+++ b/rust/helpers/property.c
@@ -6,3 +6,9 @@  void rust_helper_fwnode_handle_put(struct fwnode_handle *fwnode)
 {
 	fwnode_handle_put(fwnode);
 }
+
+bool rust_helper_fwnode_device_is_compatible(const struct fwnode_handle *fwnode,
+					     const char *match)
+{
+	return fwnode_device_is_compatible(fwnode, match);
+}
diff --git a/rust/kernel/device/property.rs b/rust/kernel/device/property.rs
index 838509111e57..a946bf8d5571 100644
--- a/rust/kernel/device/property.rs
+++ b/rust/kernel/device/property.rs
@@ -93,6 +93,15 @@  pub fn property_present(&self, name: &CStr) -> bool {
         unsafe { bindings::fwnode_property_present(self.as_raw().cast_const(), name.as_char_ptr()) }
     }
 
+    /// Return `true` if this [`FwNode`] is compatible with `match_str`, `false` otherwise.
+    pub fn is_compatible(&self, match_str: &CStr) -> bool {
+        // SAFETY:
+        // - By the invariant of `CStr`, `name.as_char_ptr()` is valid and null-terminated.
+        // - The type invariant of `Self` guarantees that `self.as_raw() is a pointer to a valid
+        //   `struct fwnode_handle`.
+        unsafe { bindings::fwnode_device_is_compatible(self.as_raw(), match_str.as_char_ptr()) }
+    }
+
     /// Returns firmware property `name` boolean value.
     pub fn property_read_bool(&self, name: &CStr) -> bool {
         // SAFETY: