[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <aCH9f35BJ93ebWiB@pollux>
Date: Mon, 12 May 2025 15:54:07 +0200
From: Danilo Krummrich <dakr@...nel.org>
To: Remo Senekowitsch <remo@...nzli.dev>
Cc: Rob Herring <robh@...nel.org>, Saravana Kannan <saravanak@...gle.com>,
Miguel Ojeda <ojeda@...nel.org>,
Alex Gaynor <alex.gaynor@...il.com>,
Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <benno.lossin@...ton.me>,
Andreas Hindborg <a.hindborg@...nel.org>,
Alice Ryhl <aliceryhl@...gle.com>, Trevor Gross <tmgross@...ch.edu>,
Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
"Rafael J. Wysocki" <rafael@...nel.org>,
Dirk Behme <dirk.behme@...bosch.com>, linux-kernel@...r.kernel.org,
devicetree@...r.kernel.org, rust-for-linux@...r.kernel.org
Subject: Re: [PATCH v4 9/9] samples: rust: platform: Add property read
examples
On Sun, May 04, 2025 at 07:31:54PM +0200, Remo Senekowitsch wrote:
> Add some example usage of the device property read methods for
> DT/ACPI/swnode properties.
>
> Co-developed-by: Rob Herring (Arm) <robh@...nel.org>
> Signed-off-by: Rob Herring (Arm) <robh@...nel.org>
> Signed-off-by: Remo Senekowitsch <remo@...nzli.dev>
> ---
> drivers/of/unittest-data/tests-platform.dtsi | 3 +
> samples/rust/rust_driver_platform.rs | 71 +++++++++++++++++++-
> 2 files changed, 72 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/of/unittest-data/tests-platform.dtsi b/drivers/of/unittest-data/tests-platform.dtsi
> index 4171f43cf01cc..50a51f38afb60 100644
> --- a/drivers/of/unittest-data/tests-platform.dtsi
> +++ b/drivers/of/unittest-data/tests-platform.dtsi
> @@ -37,6 +37,9 @@ dev@100 {
> test-device@2 {
> compatible = "test,rust-device";
> reg = <0x2>;
> +
> + test,u32-prop = <0xdeadbeef>;
> + test,i16-array = /bits/ 16 <1 2 (-3) (-4)>;
> };
> };
>
> diff --git a/samples/rust/rust_driver_platform.rs b/samples/rust/rust_driver_platform.rs
> index 8b42b3cfb363a..a04ff4afb1325 100644
> --- a/samples/rust/rust_driver_platform.rs
> +++ b/samples/rust/rust_driver_platform.rs
> @@ -2,7 +2,7 @@
>
> //! Rust Platform driver sample.
>
> -use kernel::{c_str, device::Core, of, platform, prelude::*, types::ARef};
> +use kernel::{c_str, device::Core, of, platform, prelude::*, str::CString, types::ARef};
>
> struct SampleDriver {
> pdev: ARef<platform::Device>,
> @@ -25,18 +25,85 @@ fn probe(
> pdev: &platform::Device<Core>,
> info: Option<&Self::IdInfo>,
> ) -> Result<Pin<KBox<Self>>> {
> + let dev = pdev.as_ref();
> +
> dev_dbg!(pdev.as_ref(), "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);
You switch to use dev here, but not for dev_dbg() above.
> }
>
> + Self::properties_parse(dev)?;
Let's just use pdev.as_ref() here too.
> +
> let drvdata = KBox::new(Self { pdev: pdev.into() }, GFP_KERNEL)?;
>
> Ok(drvdata.into())
> }
> }
>
> +impl SampleDriver {
> + fn properties_parse(dev: &kernel::device::Device) -> Result<()> {
Please refer to this as &device::Device, i.e. import kernel::device. You should
also be able to just use Result, without the generic.
> + let fwnode = dev.fwnode().ok_or(ENOENT)?;
> +
> + if let Ok(idx) =
> + fwnode.property_match_string(c_str!("compatible"), c_str!("test,rust-device"))
> + {
> + dev_info!(dev, "matched compatible string idx = {}\n", idx);
> + }
> +
> + if let Ok(str) = fwnode
> + .property_read::<CString>(c_str!("compatible"))
> + .required_by(dev)
> + {
> + dev_info!(dev, "compatible string = {:?}\n", str);
> + }
And else? Why do you ignore a potential error?
> +
> + let prop = fwnode.property_read_bool(c_str!("test,bool-prop"));
> + dev_info!(dev, "bool prop is {}\n", prop);
Let's use a consistent style for all those prints, e.g. '$name'='$value'. For
instance:
let name = c_str!("test,bool-prop");
let prop = fwnode.property_read_bool(name);
dev_info!(dev, "'{}'='{}'\n", name, prop);
> + if fwnode.property_present(c_str!("test,u32-prop")) {
> + dev_info!(dev, "'test,u32-prop' is present\n");
Given the above, I'd keep this one as it is.
> + }
> +
> + let prop = fwnode
> + .property_read::<u32>(c_str!("test,u32-optional-prop"))
> + .or(0x12);
> + dev_info!(
> + dev,
> + "'test,u32-optional-prop' is {:#x} (default = {:#x})\n",
> + prop,
> + 0x12
> + );
> +
> + // Missing property without a default will print an error
Maybe additionally add that you discard the Result intentionally in order to not
make properties_parse() fail in this case.
> + let _ = fwnode
> + .property_read::<u32>(c_str!("test,u32-required-prop"))
> + .required_by(dev);
> +
> + let prop: u32 = fwnode
> + .property_read(c_str!("test,u32-prop"))
> + .required_by(dev)?;
> + dev_info!(dev, "'test,u32-prop' is {:#x}\n", prop);
> +
> + let prop: [i16; 4] = fwnode
> + .property_read(c_str!("test,i16-array"))
> + .required_by(dev)?;
> + dev_info!(dev, "'test,i16-array' is {:?}\n", prop);
> + dev_info!(
> + dev,
> + "'test,i16-array' length is {}\n",
> + fwnode.property_count_elem::<u16>(c_str!("test,i16-array"))?,
> + );
> +
> + let prop: KVec<i16> = fwnode
> + .property_read_array_vec(c_str!("test,i16-array"), 4)?
> + .required_by(dev)?;
> + dev_info!(dev, "'test,i16-array' is KVec {:?}\n", prop);
> +
> + Ok(())
> + }
> +}
> +
> impl Drop for SampleDriver {
> fn drop(&mut self) {
> dev_dbg!(self.pdev.as_ref(), "Remove Rust Platform driver sample.\n");
> --
> 2.49.0
>
Powered by blists - more mailing lists