Add some example usage of the device property read methods for
DT/ACPI/swnode properties.
Co-developed-by: Rob Herring (Arm) <robh@kernel.org>
Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
Signed-off-by: Remo Senekowitsch <remo@buenzli.dev>
---
drivers/of/unittest-data/tests-platform.dtsi | 3 +
samples/rust/rust_driver_platform.rs | 69 +++++++++++++++++++-
2 files changed, 69 insertions(+), 3 deletions(-)
diff --git a/drivers/of/unittest-data/tests-platform.dtsi b/drivers/of/unittest-data/tests-platform.dtsi
index 4171f43cf..50a51f38a 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 8120609e2..0284f1840 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, of, platform, prelude::*};
+use kernel::{c_str, of, platform, prelude::*, str::CString};
struct SampleDriver {
pdev: platform::Device,
@@ -22,18 +22,81 @@ impl platform::Driver for SampleDriver {
const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
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();
+
+ 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(dev)?;
+
let drvdata = KBox::new(Self { pdev: pdev.clone() }, GFP_KERNEL)?;
Ok(drvdata.into())
}
}
+impl SampleDriver {
+ fn properties_parse(dev: &kernel::device::Device) -> Result<()> {
+ if let Ok(idx) = dev.property_match_string(c_str!("compatible"), c_str!("test,rust-device"))
+ {
+ dev_info!(dev, "matched compatible string idx = {}\n", idx);
+ }
+
+ if let Ok(str) = dev
+ .property_read::<CString>(c_str!("compatible"))
+ .required()
+ {
+ dev_info!(dev, "compatible string = {:?}\n", str);
+ }
+
+ let prop = dev.property_read_bool(c_str!("test,bool-prop"));
+ dev_info!(dev, "bool prop is {}\n", prop);
+
+ if dev.property_present(c_str!("test,u32-prop")) {
+ dev_info!(dev, "'test,u32-prop' is present\n");
+ }
+
+ let prop = dev
+ .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
+ let _ = dev
+ .property_read::<u32>(c_str!("test,u32-required-prop"))
+ .required()?;
+
+ let prop: u32 = dev.property_read(c_str!("test,u32-prop")).required()?;
+ dev_info!(dev, "'test,u32-prop' is {:#x}\n", prop);
+
+ // TODO: remove or replace with u16? `Property` is not implemented for
+ // unsigned integers, as suggested by Andy Shevchenko.
+
+ let prop: [i16; 4] = dev.property_read(c_str!("test,i16-array")).required()?;
+ 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"))?,
+ );
+
+ let prop: KVec<i16> = dev
+ .property_read_array_vec(c_str!("test,i16-array"), 4)?
+ .required()?;
+ 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
Hi Remo,
On 14/04/2025 17:26, 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@kernel.org>
> Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
> Signed-off-by: Remo Senekowitsch <remo@buenzli.dev>
> ---
> drivers/of/unittest-data/tests-platform.dtsi | 3 +
> samples/rust/rust_driver_platform.rs | 69 +++++++++++++++++++-
> 2 files changed, 69 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/of/unittest-data/tests-platform.dtsi b/drivers/of/unittest-data/tests-platform.dtsi
> index 4171f43cf..50a51f38a 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)>;
Is "test,u32-required-prop" missing here? See below ...
> };
> };
>
> diff --git a/samples/rust/rust_driver_platform.rs b/samples/rust/rust_driver_platform.rs
> index 8120609e2..0284f1840 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, of, platform, prelude::*};
> +use kernel::{c_str, of, platform, prelude::*, str::CString};
>
> struct SampleDriver {
> pdev: platform::Device,
> @@ -22,18 +22,81 @@ impl platform::Driver for SampleDriver {
> const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
>
> 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();
> +
> + 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(dev)?;
> +
> let drvdata = KBox::new(Self { pdev: pdev.clone() }, GFP_KERNEL)?;
>
> Ok(drvdata.into())
> }
> }
>
> +impl SampleDriver {
> + fn properties_parse(dev: &kernel::device::Device) -> Result<()> {
> + if let Ok(idx) = dev.property_match_string(c_str!("compatible"), c_str!("test,rust-device"))
> + {
> + dev_info!(dev, "matched compatible string idx = {}\n", idx);
> + }
> +
> + if let Ok(str) = dev
> + .property_read::<CString>(c_str!("compatible"))
> + .required()
> + {
> + dev_info!(dev, "compatible string = {:?}\n", str);
> + }
> +
> + let prop = dev.property_read_bool(c_str!("test,bool-prop"));
> + dev_info!(dev, "bool prop is {}\n", prop);
> +
> + if dev.property_present(c_str!("test,u32-prop")) {
> + dev_info!(dev, "'test,u32-prop' is present\n");
> + }
> +
> + let prop = dev
> + .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
> + let _ = dev
> + .property_read::<u32>(c_str!("test,u32-required-prop"))
> + .required()?;
On x86 using above dtsi it seems it error exits here:
[ 2.178227] rust_driver_platform
testcase-data:platform-tests:test-device@2: Probed with info: '42'.
[ 2.178715] rust_driver_platform
testcase-data:platform-tests:test-device@2: matched compatible string
idx = 0
[ 2.179122] rust_driver_platform
testcase-data:platform-tests:test-device@2: compatible string =
"test,rust-device"
[ 2.179563] rust_driver_platform
testcase-data:platform-tests:test-device@2: bool prop is false
[ 2.179841] rust_driver_platform
testcase-data:platform-tests:test-device@2: 'test,u32-prop' is present
[ 2.180200] rust_driver_platform
testcase-data:platform-tests:test-device@2: 'test,u32-optional-prop' is
0x12 (default = 0x12)
[ 2.180598] rust_driver_platform
testcase-data:platform-tests:test-device@2:
/testcase-data/platform-tests/test-device@2: property
'test,u32-required-prop' is missing
[ 2.181244] rust_driver_platform
testcase-data:platform-tests:test-device@2: probe with driver
rust_driver_platform failed with error -22
I'm not sure if this is what we want?
Best regards
Dirk
> + let prop: u32 = dev.property_read(c_str!("test,u32-prop")).required()?;
> + dev_info!(dev, "'test,u32-prop' is {:#x}\n", prop);
> +
> + // TODO: remove or replace with u16? `Property` is not implemented for
> + // unsigned integers, as suggested by Andy Shevchenko.
> +
> + let prop: [i16; 4] = dev.property_read(c_str!("test,i16-array")).required()?;
> + 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"))?,
> + );
> +
> + let prop: KVec<i16> = dev
> + .property_read_array_vec(c_str!("test,i16-array"), 4)?
> + .required()?;
> + dev_info!(dev, "'test,i16-array' is KVec {:?}\n", prop);
> +
> + Ok(())
> + }
> +}
> +
On 23/04/2025 14:39, Dirk Behme wrote:
> Hi Remo,
>
> On 14/04/2025 17:26, 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@kernel.org>
>> Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
>> Signed-off-by: Remo Senekowitsch <remo@buenzli.dev>
>> ---
>> drivers/of/unittest-data/tests-platform.dtsi | 3 +
>> samples/rust/rust_driver_platform.rs | 69 +++++++++++++++++++-
>> 2 files changed, 69 insertions(+), 3 deletions(-)
>>
>> diff --git a/drivers/of/unittest-data/tests-platform.dtsi b/drivers/of/unittest-data/tests-platform.dtsi
>> index 4171f43cf..50a51f38a 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)>;
>
>
> Is "test,u32-required-prop" missing here? See below ...
Ah, no, sorry, "test,u32-required-prop" isn't there intentionally to
demonstrate the error handling on missing required properties. In this
case I would propose to just remove the '?' to make the sample code not
exit on this intended error:
diff --git a/samples/rust/rust_driver_platform.rs
b/samples/rust/rust_driver_platform.rs
index 01902956e1ca7..b6cbd721a8882 100644
--- a/samples/rust/rust_driver_platform.rs
+++ b/samples/rust/rust_driver_platform.rs
@@ -75,7 +75,7 @@ fn properties_parse(dev: &kernel::device::Device) ->
Result<()> {
// Missing property without a default will print an error
let _ = dev
.property_read::<u32>(c_str!("test,u32-required-prop"))
- .required()?;
+ .required();
let prop: u32 =
dev.property_read(c_str!("test,u32-prop")).required()?;
dev_info!(dev, "'test,u32-prop' is {:#x}\n", prop);
Best regards
Dirk
© 2016 - 2025 Red Hat, Inc.