From: Daniel Almeida <daniel.almeida@collabora.com>
To: "Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Tamir Duberstein" <tamird@kernel.org>,
"Alexandre Courbot" <acourbot@nvidia.com>,
"Onur Özkan" <work@onurozkan.dev>,
"Philipp Zabel" <p.zabel@pengutronix.de>
Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
dri-devel@lists.freedesktop.org,
Detlev Casanova <detlev.casanova@collabora.com>,
Nicolas Dufresne <nicolas.dufresne@collabora.com>
Subject: [PATCH 4/7] rust: reset: add AssertedGuard
Date: Thu, 26 Nov 2026 13:04:31 -0300 [thread overview]
Message-ID: <20261126-b4-rust-reset-v1-4-9c4c3d029080@collabora.com> (raw)
In-Reply-To: <20261126-b4-rust-reset-v1-0-9c4c3d029080@collabora.com>
Some drivers hold their device in reset for a short time while they
are bound.
Add ExclusiveResetControl::assert_guard() as a convenience for them. It
asserts the reset line of an Unknown control and returns an
AssertedGuard that deasserts it when dropped, so the control stays in
the Unknown state.
The guard borrows the control mutably because, unlike regulators,
exclusive resets do not have an "enabled" count. Therefore, allowing
more than one guard at a time would not work, as dropping one of them
would invalidate the other while it is still alive.
Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
rust/kernel/reset.rs | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 82 insertions(+)
diff --git a/rust/kernel/reset.rs b/rust/kernel/reset.rs
index 7ca83f2d6abf..3bbd226825d8 100644
--- a/rust/kernel/reset.rs
+++ b/rust/kernel/reset.rs
@@ -12,6 +12,10 @@
//! A driver can also get a single reset control for all the reset lines in the `resets`
//! property of its device. Its operations then act on all of those lines.
//!
+//! An [`ExclusiveResetControl`] in the [`Unknown`] state can also assert its line for a limited
+//! time, with [`ExclusiveResetControl::assert_guard`]. The [`AssertedGuard`] that it returns
+//! deasserts the line when dropped, and the control stays in the [`Unknown`] state.
+//!
//! C header: [`include/linux/reset.h`](srctree/include/linux/reset.h)
//!
//! See [Reset controller API](https://docs.kernel.org/driver-api/reset.html) for more information.
@@ -381,6 +385,24 @@ pub fn assert(self) -> Result<ExclusiveResetControl<'a, Asserted>, Error<Self>>
pub fn deassert(self) -> Result<ExclusiveResetControl<'a, Deasserted>, Error<Self>> {
self.deassert_internal()
}
+
+ /// Asserts the reset line and returns an [`AssertedGuard`], which deasserts it when dropped.
+ ///
+ /// The [`ExclusiveResetControl`] stays in the [`Unknown`] state, and it cannot be used while
+ /// the guard exists.
+ ///
+ /// The line is asserted with the kernel's [`reset_control_assert()`].
+ ///
+ /// [`reset_control_assert()`]:
+ /// https://docs.kernel.org/driver-api/reset.html#c.reset_control_assert
+ #[inline]
+ pub fn assert_guard(&mut self) -> Result<AssertedGuard<'_, 'a>> {
+ // SAFETY: By the type invariants, `self.inner` is a pointer that a C getter returned, and
+ // `self` owns its reference.
+ to_result(unsafe { bindings::reset_control_assert(self.inner) })?;
+
+ Ok(AssertedGuard { reset: self })
+ }
}
impl<'a> ExclusiveResetControl<'a, Asserted> {
@@ -423,3 +445,63 @@ fn drop(&mut self) {
unsafe { bindings::reset_control_put(self.inner) };
}
}
+
+/// A guard returned by [`ExclusiveResetControl::assert_guard`], which deasserts the reset line when
+/// dropped.
+///
+/// An error from the deassert done on drop is discarded. [`AssertedGuard::deassert`] deasserts the
+/// line and returns the error.
+///
+/// # Examples
+///
+/// Holding a device in reset for a limited time:
+///
+/// ```
+/// use kernel::reset::{
+/// ExclusiveResetControl,
+/// Unknown, //
+/// };
+///
+/// # fn restart_clocks() -> Result { Ok(()) }
+/// fn reset_device(reset: &mut ExclusiveResetControl<'_, Unknown>) -> Result {
+/// let asserted = reset.assert_guard()?;
+///
+/// // If this fails, dropping `asserted` deasserts the line.
+/// restart_clocks()?;
+///
+/// asserted.deassert()
+/// }
+/// ```
+#[must_use = "the reset line is deasserted immediately when the guard is unused"]
+pub struct AssertedGuard<'r, 'a> {
+ reset: &'r mut ExclusiveResetControl<'a, Unknown>,
+}
+
+impl<'r, 'a> AssertedGuard<'r, 'a> {
+ /// Deasserts the reset line and consumes the guard.
+ ///
+ /// The only difference from dropping the guard is that this returns an error from the
+ /// deassert, which a drop discards.
+ ///
+ /// Equivalent to the kernel's [`reset_control_deassert()`].
+ ///
+ /// [`reset_control_deassert()`]:
+ /// https://docs.kernel.org/driver-api/reset.html#c.reset_control_deassert
+ #[inline]
+ pub fn deassert(self) -> Result {
+ // Skip `drop()`, which would deassert the line a second time.
+ let guard = ManuallyDrop::new(self);
+
+ // SAFETY: By the type invariants of `ExclusiveResetControl`, `guard.reset.inner` is a
+ // pointer that a C getter returned, and `guard.reset` owns its reference.
+ to_result(unsafe { bindings::reset_control_deassert(guard.reset.inner) })
+ }
+}
+
+impl<'r, 'a> Drop for AssertedGuard<'r, 'a> {
+ fn drop(&mut self) {
+ // SAFETY: By the type invariants of `ExclusiveResetControl`, `self.reset.inner` is a
+ // pointer that a C getter returned, and `self.reset` owns its reference.
+ unsafe { bindings::reset_control_deassert(self.reset.inner) };
+ }
+}
--
2.56.0
next prev parent reply other threads:[~2026-11-26 16:06 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 16:04 [PATCH 0/7] rust: reset: add reset control abstractions Daniel Almeida
2026-11-26 16:04 ` [PATCH 1/7] rust: reset: add reset control helpers Daniel Almeida
2026-11-26 16:04 ` [PATCH 2/7] rust: reset: add ExclusiveResetControl Daniel Almeida
2026-11-26 16:04 ` [PATCH 3/7] rust: reset: add ExclusiveResetControl::get_array() and get_array_optional() Daniel Almeida
2026-11-26 16:04 ` Daniel Almeida [this message]
2026-11-26 16:04 ` [PATCH 5/7] rust: reset: add SharedResetControl Daniel Almeida
2026-11-26 19:14 ` Alice Ryhl
2026-11-26 16:04 ` [PATCH 6/7] rust: reset: add SharedResetControl::get_array() and get_array_optional() Daniel Almeida
2026-11-26 16:04 ` [PATCH 7/7] rust: reset: add devm_deassert() and devm_deassert_optional() Daniel Almeida
2026-11-26 16:33 ` [PATCH 0/7] rust: reset: add reset control abstractions Nicolas Dufresne
2026-11-26 18:02 ` Daniel Almeida
2026-11-26 18:11 ` Nicolas Dufresne
2026-11-26 18:36 ` Daniel Almeida
2026-11-26 18:46 ` Nicolas Dufresne
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=20261126-b4-rust-reset-v1-4-9c4c3d029080@collabora.com \
--to=daniel.almeida@collabora.com \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=detlev.casanova@collabora.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=nicolas.dufresne@collabora.com \
--cc=ojeda@kernel.org \
--cc=p.zabel@pengutronix.de \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=work@onurozkan.dev \
/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®