Add some example usage of the device property read methods for
DT/ACPI/swnode properties.
Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
---
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();
+ 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"));
+ 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()
+ );
+
let drvdata = KBox::new(Self { pdev: pdev.clone() }, GFP_KERNEL)?;
Ok(drvdata.into())
--
2.45.2
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()) >
© 2016 - 2024 Red Hat, Inc.