mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Dirk Behme <dirk.behme@de.bosch.com>
To: "Rob Herring (Arm)" <robh@kernel.org>,
	"Saravana Kannan" <saravanak@google.com>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <benno.lossin@proton.me>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Dirk Behme" <dirk.behme@gmail.com>
Cc: <devicetree@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	<rust-for-linux@vger.kernel.org>
Subject: Re: [PATCH RFC 3/3] samples: rust: platform: Add property read examples
Date: Mon, 28 Oct 2024 08:13:59 +0100	[thread overview]
Message-ID: <df1473dc-99a4-4a6c-bfc3-09c37b9883d5@de.bosch.com> (raw)
In-Reply-To: <20241025-rust-platform-dev-v1-3-0df8dcf7c20b@kernel.org>

On 25.10.2024 23:05, Rob Herring (Arm) wrote:
> Add some example usage of the device property read methods for
> DT/ACPI/swnode properties.
> 
> Signed-off-by: Rob Herring (Arm) <robh@kernel.org>


I think that we should mention that this works only with 
CONFIG_OF_UNITTEST enabled? And/or maybe wrap the whole somehow with

#[cfg(CONFIG_OF_UNITTEST)]

?

> ---
>   drivers/of/unittest-data/tests-platform.dtsi |  3 +++
>   samples/rust/rust_driver_platform.rs         | 22 ++++++++++++++++++++++
>   2 files changed, 25 insertions(+)
> 
> diff --git a/drivers/of/unittest-data/tests-platform.dtsi b/drivers/of/unittest-data/tests-platform.dtsi
> index 2caaf1c10ee6..a5369b9343b8 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 5cf4a8f86c13..95c290806862 100644
> --- a/samples/rust/rust_driver_platform.rs
> +++ b/samples/rust/rust_driver_platform.rs
> @@ -41,6 +41,28 @@ fn probe(pdev: &mut platform::Device, info: Option<&Self::IdInfo>) -> Result<Pin
>               }
>           };
>   
> +        let dev = pdev.as_ref();


Maybe move this up and use it in Danilo's part, as well? To stay consistent?

--- a/samples/rust/rust_driver_platform.rs
+++ b/samples/rust/rust_driver_platform.rs
@@ -26,25 +26,25 @@ impl platform::Driver for SampleDriver {

      fn probe(pdev: &mut platform::Device, info: Option<&Self::IdInfo>) 
-> Result<Pin<KBox<Self>>> {
          dev_dbg!(pdev.as_ref(), "Probe Rust Platform driver sample.\n");
+        let dev = pdev.as_ref();

          match (Self::of_match_device(pdev), info) {
              (Some(id), Some(info)) => {
                  dev_info!(
-                    pdev.as_ref(),
+                    dev,
                      "Probed by OF compatible match: '{}' with info: 
'{}'.\n",
                      id.compatible(),
                      info.0
                  );
              }
              _ => {
-                dev_info!(pdev.as_ref(), "Probed by name.\n");
+                dev_info!(dev, "Probed by name.\n");
              }
          };

-        let dev = pdev.as_ref();
          if let Ok(idx) = 
dev.property_match_string(c_str!("compatible"), c_str!("test,rust-device"))
          {
-            dev_info!(pdev.as_ref(), "matched compatible string idx = 
{}\n", idx);
+            dev_info!(dev, "matched compatible string idx = {}\n", idx);
          }

          let prop = dev.property_read_bool(c_str!("test,bool-prop"));
@@ -56,12 +56,10 @@ fn probe(pdev: &mut platform::Device, info: 
Option<&Self::IdInfo>) -> Result<Pin


> +        if let Ok(idx) = dev.property_match_string(c_str!("compatible"), c_str!("test,rust-device"))
> +        {
> +            dev_info!(pdev.as_ref(), "matched compatible string idx = {}\n", idx);
> +        }
> +
> +        let prop = dev.property_read_bool(c_str!("test,bool-prop"));

I stopped reading here with "hey, "test,bool-prop" isn't in the 
tests-platform.dtsi above, no?". Until I realized that this is 
intentional to get back false. So whats about adding a comment like

// Intentionally check for an non-existent property to get back false

?


> +        dev_info!(dev, "bool prop is {}\n", prop);
> +
> +        let _prop = dev.property_read::<u32>(c_str!("test,u32-prop"))?;
> +        let prop: u32 = dev.property_read(c_str!("test,u32-prop"))?;
> +        dev_info!(dev, "'test,u32-prop' is {:#x}\n", prop);
> +
> +        let prop: [i16; 4] = dev.property_read_array(c_str!("test,i16-array"))?;
> +        dev_info!(dev, "'test,i16-array' is {:?}\n", prop);
> +        dev_info!(
> +            dev,
> +            "'test,i16-array' length is {}\n",
> +            dev.property_count_elem::<u16>(c_str!("test,i16-array"))
> +                .unwrap()


In the error case unwrap() (or expect()) will result in panic(). Besides 
some very rare cases I don't see a reason why device drivers should 
panic. Esp. not if reading some array length from the device tree fails 
;) So I don't think we should encourage using unwrap() or expect() in 
drivers by using them even in example code. Which most probably will be 
copied quite often, then ;)

What's about anything like this instead?

          let prop: [i16; 4] = 
dev.property_read_array(c_str!("test,i16-array"))?;
          dev_info!(dev, "'test,i16-array' is {:?}\n", prop);
-        dev_info!(
-            dev,
-            "'test,i16-array' length is {}\n",
-            dev.property_count_elem::<u16>(c_str!("test,i16-array"))
-                .unwrap()
-        );
+        let len = dev.property_count_elem::<u16>(c_str!("test,i16-array"));
+        if let Ok(length) = len {
+            dev_info!(dev, "'test,i16-array' length is {}\n", length);
+        }

          let drvdata = KBox::new(Self { pdev: pdev.clone() }, GFP_KERNEL)?;

Or if we want a reasonable error message in the error case, as well, we 
could switch to a match, instead.

Best regards

Dirk



> +        );
> +
>           let drvdata = KBox::new(Self { pdev: pdev.clone() }, GFP_KERNEL)?;
>   
>           Ok(drvdata.into())
> 


  reply	other threads:[~2024-10-28  7:14 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-25 21:05 [PATCH RFC 0/3] Initial rust bindings for device property reads Rob Herring (Arm)
2024-10-25 21:05 ` [PATCH RFC 1/3] of: unittest: Add a platform device node for rust platform driver sample Rob Herring (Arm)
2024-10-28  7:11   ` Dirk Behme
2024-10-25 21:05 ` [PATCH RFC 2/3] rust: Add bindings for device properties Rob Herring (Arm)
2024-10-25 21:12   ` Alex Gaynor
2024-10-27 22:07     ` Rob Herring
2024-10-28 22:24     ` Rob Herring
2024-10-29  2:08       ` Alex Gaynor
2024-10-28  7:12   ` Dirk Behme
2024-10-29 14:16   ` Alice Ryhl
2024-10-29 17:57     ` Rob Herring
2024-10-29 18:29       ` Miguel Ojeda
2024-10-29 18:30         ` Miguel Ojeda
2024-10-29 18:48       ` Alice Ryhl
2024-10-29 18:57         ` Miguel Ojeda
2024-10-29 19:35           ` Rob Herring
2024-10-29 22:05             ` Rob Herring
2024-10-30  8:09               ` Alice Ryhl
2024-10-30  8:15             ` Alice Ryhl
2024-10-30 14:05               ` Rob Herring
2024-10-30 15:43                 ` Alice Ryhl
2024-10-30 16:03                 ` Dirk Behme
2024-10-30 16:47                   ` Rob Herring
2024-10-31  7:19                     ` Dirk Behme
2024-11-15  6:39                     ` Dirk Behme
2024-10-25 21:05 ` [PATCH RFC 3/3] samples: rust: platform: Add property read examples Rob Herring (Arm)
2024-10-28  7:13   ` Dirk Behme [this message]
2024-10-28  7:11 ` [PATCH RFC 0/3] Initial rust bindings for device property reads Dirk Behme

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=df1473dc-99a4-4a6c-bfc3-09c37b9883d5@de.bosch.com \
    --to=dirk.behme@de.bosch.com \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=dirk.behme@gmail.com \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rafael@kernel.org \
    --cc=robh@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=saravanak@google.com \
    --cc=tmgross@umich.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®