From nobody Sat Oct 11 08:30:54 2025 Received: from mout-p-202.mailbox.org (mout-p-202.mailbox.org [80.241.56.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 57E8826AAA3; Wed, 11 Jun 2025 10:30:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.241.56.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749637806; cv=none; b=AGczJSjh6FlI1BMG1kLiEbCVduRAHqyuGt4okxWz43OZLzg0noZDkjc8oAk6MOr9WwQyEr7yvD0QTGsPsaPa4BzRwymdX5mBCuduVPXoJFNiksu7X7N6PaHVN0d0vL+r3a3D9fGdtjVlAFwM7NBVnPPeDmef1Lp7RZVkaOZvDdQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749637806; c=relaxed/simple; bh=R/L1yz/OFtaKsGYyJS6vXs8rb7mCAM3V5P4qgQyt1eg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=u2kXIMM8/Mvc9J0dHZe8cwVKlwV387VWHKL+o2d1jW8pFKJR42tUIm1vFqEtz6pVLWC/yE0XMDIZqQbaV7xq0EZjIzsy4TpdbAOr1Qsz+NfoAV5TNLefPd+joNPbkB334mkN2DPw2PNBAuvoUsYsriropVY8ad5yubixrJuLkfs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=buenzli.dev; spf=pass smtp.mailfrom=buenzli.dev; arc=none smtp.client-ip=80.241.56.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=buenzli.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=buenzli.dev Received: from smtp202.mailbox.org (smtp202.mailbox.org [IPv6:2001:67c:2050:b231:465::202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-202.mailbox.org (Postfix) with ESMTPS id 4bHMNn6Z3sz9tQS; Wed, 11 Jun 2025 12:30:01 +0200 (CEST) From: Remo Senekowitsch To: Rob Herring , Saravana Kannan , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Greg Kroah-Hartman , "Rafael J. Wysocki" , Dirk Behme , Remo Senekowitsch Cc: linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, rust-for-linux@vger.kernel.org Subject: [PATCH v8 9/9] samples: rust: platform: Add property read examples Date: Wed, 11 Jun 2025 12:29:08 +0200 Message-ID: <20250611102908.212514-10-remo@buenzli.dev> In-Reply-To: <20250611102908.212514-1-remo@buenzli.dev> References: <20250611102908.212514-1-remo@buenzli.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: 4bHMNn6Z3sz9tQS Content-Type: text/plain; charset="utf-8" Add some example usage of the device property read methods for DT/ACPI/swnode properties. Tested-by: Dirk Behme Co-developed-by: Rob Herring (Arm) Signed-off-by: Rob Herring (Arm) Signed-off-by: Remo Senekowitsch --- drivers/of/unittest-data/tests-platform.dtsi | 3 + samples/rust/rust_driver_platform.rs | 60 +++++++++++++++++++- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/drivers/of/unittest-data/tests-platform.dtsi b/drivers/of/unit= test-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 =3D "test,rust-device"; reg =3D <0x2>; + + test,u32-prop =3D <0xdeadbeef>; + test,i16-array =3D /bits/ 16 <1 2 (-3) (-4)>; }; }; =20 diff --git a/samples/rust/rust_driver_platform.rs b/samples/rust/rust_drive= r_platform.rs index 8b42b3cfb363a..c0abf78d0683b 100644 --- a/samples/rust/rust_driver_platform.rs +++ b/samples/rust/rust_driver_platform.rs @@ -2,7 +2,14 @@ =20 //! Rust Platform driver sample. =20 -use kernel::{c_str, device::Core, of, platform, prelude::*, types::ARef}; +use kernel::{ + c_str, + device::{self, Core}, + of, platform, + prelude::*, + str::CString, + types::ARef, +}; =20 struct SampleDriver { pdev: ARef, @@ -31,12 +38,63 @@ fn probe( dev_info!(pdev.as_ref(), "Probed with info: '{}'.\n", info.0); } =20 + Self::properties_parse(pdev.as_ref())?; + let drvdata =3D KBox::new(Self { pdev: pdev.into() }, GFP_KERNEL)?; =20 Ok(drvdata.into()) } } =20 +impl SampleDriver { + fn properties_parse(dev: &device::Device) -> Result { + let fwnode =3D dev.fwnode().ok_or(ENOENT)?; + + if let Ok(idx) =3D + fwnode.property_match_string(c_str!("compatible"), c_str!("tes= t,rust-device")) + { + dev_info!(dev, "matched compatible string idx =3D {}\n", idx); + } + + let name =3D c_str!("compatible"); + let prop =3D fwnode.property_read::(name).required_by(dev= )?; + dev_info!(dev, "'{name}'=3D'{prop:?}'\n"); + + let name =3D c_str!("test,bool-prop"); + let prop =3D fwnode.property_read_bool(c_str!("test,bool-prop")); + dev_info!(dev, "'{name}'=3D'{prop}'\n"); + + if fwnode.property_present(c_str!("test,u32-prop")) { + dev_info!(dev, "'test,u32-prop' is present\n"); + } + + let name =3D c_str!("test,u32-optional-prop"); + let prop =3D fwnode.property_read::(name).or(0x12); + dev_info!(dev, "'{name}'=3D'{prop:#x}' (default =3D 0x12)\n",); + + // A missing required property will print an error. Discard the er= ror to + // prevent properties_parse from failing in that case. + let name =3D c_str!("test,u32-required-prop"); + let _ =3D fwnode.property_read::(name).required_by(dev); + + let name =3D c_str!("test,u32-prop"); + let prop: u32 =3D fwnode.property_read(name).required_by(dev)?; + dev_info!(dev, "'{name}'=3D'{prop:#x}'\n"); + + let name =3D c_str!("test,i16-array"); + let prop: [i16; 4] =3D fwnode.property_read(name).required_by(dev)= ?; + dev_info!(dev, "'{name}'=3D'{prop:?}'\n"); + let len =3D fwnode.property_count_elem::(name)?; + dev_info!(dev, "'{name}' length is {len}\n",); + + let name =3D c_str!("test,i16-array"); + let prop: KVec =3D fwnode.property_read_array_vec(name, 4)?.r= equired_by(dev)?; + dev_info!(dev, "'{name}'=3D'{prop:?}' (KVec)\n"); + + Ok(()) + } +} + impl Drop for SampleDriver { fn drop(&mut self) { dev_dbg!(self.pdev.as_ref(), "Remove Rust Platform driver sample.\= n"); --=20 2.49.0