From: Danilo Krummrich <dakr@kernel.org>
To: Remo Senekowitsch <remo@buenzli.dev>
Cc: "Rob Herring" <robh@kernel.org>,
"Saravana Kannan" <saravanak@google.com>,
"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>,
"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
"Rafael J. Wysocki" <rafael@kernel.org>,
"Dirk Behme" <dirk.behme@de.bosch.com>,
linux-kernel@vger.kernel.org, devicetree@vger.kernel.org,
rust-for-linux@vger.kernel.org
Subject: Re: [PATCH v3 2/7] rust: property: Enable printing fwnode name and path
Date: Fri, 25 Apr 2025 17:48:31 +0200 [thread overview]
Message-ID: <aAuuz8BfATawK5oL@pollux> (raw)
In-Reply-To: <20250425150130.13917-3-remo@buenzli.dev>
On Fri, Apr 25, 2025 at 05:01:25PM +0200, Remo Senekowitsch wrote:
> Add two new public methods `display_name` and `display_path` to
> `FwNode`. They can be used by driver authors for logging purposes. In
> addition, they will be used by core property abstractions for automatic
> logging, for example when a driver attempts to read a required but
> missing property.
>
> Signed-off-by: Remo Senekowitsch <remo@buenzli.dev>
> ---
> rust/kernel/device/property.rs | 78 ++++++++++++++++++++++++++++++++++
> 1 file changed, 78 insertions(+)
>
> diff --git a/rust/kernel/device/property.rs b/rust/kernel/device/property.rs
> index d89715f7d..28850aa3b 100644
> --- a/rust/kernel/device/property.rs
> +++ b/rust/kernel/device/property.rs
> @@ -49,6 +49,84 @@ pub(crate) fn as_raw(&self) -> *mut bindings::fwnode_handle {
> self.0.get()
> }
>
> + /// Returns an object that implements [`Display`](core::fmt::Display) for
> + /// printing the name of a node.
> + pub fn display_name(&self) -> impl core::fmt::Display + '_ {
> + struct FwNodeDisplayName<'a>(&'a FwNode);
> +
> + impl core::fmt::Display for FwNodeDisplayName<'_> {
> + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
> + // SAFETY: self is valid by its type invariant
> + let name = unsafe { bindings::fwnode_get_name(self.0.as_raw()) };
> + if name.is_null() {
> + return Ok(());
> + }
> + // SAFETY: fwnode_get_name returns null or a valid C string and
> + // name is not null
> + let name = unsafe { CStr::from_char_ptr(name) };
> + write!(f, "{name}")
> + }
> + }
> +
> + FwNodeDisplayName(self)
> + }
> +
> + /// Returns an object that implements [`Display`](core::fmt::Display) for
> + /// printing the full path of a node.
> + pub fn display_path(&self) -> impl core::fmt::Display + '_ {
> + struct FwNodeDisplayPath<'a>(&'a FwNode);
> +
> + impl core::fmt::Display for FwNodeDisplayPath<'_> {
> + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
> + // The logic here is the same as the one in lib/vsprintf.c
> + // (fwnode_full_name_string).
> +
> + let num_parents = unsafe { bindings::fwnode_count_parents(self.0.as_raw()) };
> +
> + for depth in (0..=num_parents).rev() {
> + let fwnode = if depth == 0 {
> + self.0.as_raw()
> + } else {
> + // SAFETY: `self.0.as_raw()` is valid
> + unsafe { bindings::fwnode_get_nth_parent(self.0.as_raw(), depth) }
> + };
> +
> + // SAFETY: fwnode is valid, it is either `self.0.as_raw()` or
> + // the return value of `bindings::fwnode_get_nth_parent` which
> + // returns a valid pointer to a fwnode_handle if the provided
> + // depth is within the valid range, which we know to be true.
> + let prefix = unsafe { bindings::fwnode_get_name_prefix(fwnode) };
> + if !prefix.is_null() {
> + // SAFETY: fwnode_get_name_prefix returns null or a
> + // valid C string
> + let prefix = unsafe { CStr::from_char_ptr(prefix) };
> + write!(f, "{prefix}")?;
> + }
> + // SAFETY: fwnode is valid for the reasons stated above
> + let name = unsafe { bindings::fwnode_get_name(fwnode) };
> + if !name.is_null() {
> + // SAFETY: fwnode_get_name returns null or a valid
> + // C string
> + let name = unsafe { CStr::from_char_ptr(name) };
> + write!(f, "{name}")?;
> + }
I think you should be able to just call
FwNodeDisplayName(self).fmt(f)?
for this part, which saves you the duplicated code.
next prev parent reply other threads:[~2025-04-25 15:48 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-25 15:01 [PATCH v3 0/7] More Rust bindings for device property reads Remo Senekowitsch
2025-04-25 15:01 ` [PATCH v3 1/7] rust: property: Move property_present to separate file Remo Senekowitsch
2025-04-25 15:25 ` Danilo Krummrich
2025-04-30 6:14 ` Dirk Behme
2025-04-25 15:01 ` [PATCH v3 2/7] rust: property: Enable printing fwnode name and path Remo Senekowitsch
2025-04-25 15:48 ` Danilo Krummrich [this message]
2025-04-30 7:44 ` Dirk Behme
2025-04-25 15:01 ` [PATCH v3 3/7] rust: property: Introduce PropertyGuard Remo Senekowitsch
2025-04-25 15:35 ` Danilo Krummrich
2025-04-26 6:19 ` Dirk Behme
2025-04-26 10:15 ` Danilo Krummrich
2025-04-26 11:08 ` Remo Senekowitsch
2025-04-26 14:19 ` Danilo Krummrich
2025-04-26 14:35 ` Dirk Behme
2025-04-26 15:02 ` Danilo Krummrich
2025-04-26 21:50 ` Remo Senekowitsch
2025-04-27 22:12 ` John Hubbard
2025-04-28 20:18 ` Rob Herring
2025-04-28 20:25 ` John Hubbard
2025-04-28 21:10 ` Rob Herring
2025-04-27 6:11 ` Dirk Behme
2025-04-27 12:23 ` Danilo Krummrich
2025-04-28 5:03 ` Dirk Behme
2025-04-28 16:09 ` Danilo Krummrich
2025-04-28 20:48 ` Rob Herring
2025-04-28 21:21 ` Danilo Krummrich
2025-04-28 21:50 ` Remo Senekowitsch
2025-04-29 8:50 ` Danilo Krummrich
2025-04-25 15:01 ` [PATCH v3 4/7] rust: property: Add bindings for reading device properties Remo Senekowitsch
2025-04-25 15:01 ` [PATCH v3 5/7] rust: property: Add child accessor and iterator Remo Senekowitsch
2025-04-30 6:26 ` Dirk Behme
2025-04-25 15:01 ` [PATCH v3 6/7] rust: property: Add property_get_reference_args Remo Senekowitsch
2025-04-25 15:01 ` [PATCH v3 7/7] samples: rust: platform: Add property read examples Remo Senekowitsch
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=aAuuz8BfATawK5oL@pollux \
--to=dakr@kernel.org \
--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=devicetree@vger.kernel.org \
--cc=dirk.behme@de.bosch.com \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ojeda@kernel.org \
--cc=rafael@kernel.org \
--cc=remo@buenzli.dev \
--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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.