[PATCH 5/7] rust: reset: add SharedResetControl
From: Daniel Almeida
Date: Thu Nov 26 2026 - 11:06:45 EST
A reset line can be shared between devices. The reset core then counts
the deasserts on the line, and only the first deassert and the last
assert act on it.
Add SharedResetControl for such lines, with get() and get_optional().
It has two states, Unknown and Deasserted, which correspond to Disabled
and Enabled of Regulator<T>. An assert returns it to Unknown, since
other users may keep the line deasserted.
Like ExclusiveResetControl, it borrows the bound device it was obtained
from, which ties it to the binding as devm_reset_control_get_shared()
does in C.
Nothing uses SharedResetControl yet.
Signed-off-by: Daniel Almeida <daniel.almeida@xxxxxxxxxxxxx>
---
rust/helpers/reset.c | 13 ++++
rust/kernel/reset.rs | 206 +++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 219 insertions(+)
diff --git a/rust/helpers/reset.c b/rust/helpers/reset.c
index 435f2c7c815d..9c106d8b6de1 100644
--- a/rust/helpers/reset.c
+++ b/rust/helpers/reset.c
@@ -15,6 +15,19 @@ rust_helper_reset_control_get_optional_exclusive(struct device *dev,
return reset_control_get_optional_exclusive(dev, id);
}
+__rust_helper struct reset_control *
+rust_helper_reset_control_get_shared(struct device *dev, const char *id)
+{
+ return reset_control_get_shared(dev, id);
+}
+
+__rust_helper struct reset_control *
+rust_helper_reset_control_get_optional_shared(struct device *dev,
+ const char *id)
+{
+ return reset_control_get_optional_shared(dev, id);
+}
+
#ifndef CONFIG_RESET_CONTROLLER
__rust_helper struct reset_control *
rust_helper_fwnode_reset_control_array_get(struct fwnode_handle *fwnode,
diff --git a/rust/kernel/reset.rs b/rust/kernel/reset.rs
index 3bbd226825d8..4d241ec6e549 100644
--- a/rust/kernel/reset.rs
+++ b/rust/kernel/reset.rs
@@ -16,6 +16,9 @@
//! time, with [`ExclusiveResetControl::assert_guard`]. The [`AssertedGuard`] that it returns
//! deasserts the line when dropped, and the control stays in the [`Unknown`] state.
//!
+//! If the reset line is shared with other devices, the driver asks for a [`SharedResetControl`]
+//! instead.
+//!
//! 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.
@@ -67,6 +70,9 @@ pub trait ResetState: private::Sealed {
///
/// Dropping a reset control in this state asserts the line. An error from that assert is
/// discarded.
+///
+/// For a [`SharedResetControl`], the line may still be deasserted after that assert, as described
+/// in [`SharedResetControl::assert`].
pub struct Deasserted;
impl ResetState for Unknown {
@@ -81,6 +87,15 @@ impl ResetState for Deasserted {
const ASSERT_ON_DROP: bool = true;
}
+/// A trait representing the states a [`SharedResetControl`] can be in.
+///
+/// [`Asserted`] is not one of them, since the line may still be deasserted after a
+/// [`SharedResetControl`] is asserted.
+pub trait SharedResetState: ResetState {}
+
+impl SharedResetState for Unknown {}
+impl SharedResetState for Deasserted {}
+
/// An error that can occur when trying to move a reset control between states.
pub struct Error<T> {
/// The error that occurred.
@@ -505,3 +520,194 @@ fn drop(&mut self) {
unsafe { bindings::reset_control_deassert(self.reset.inner) };
}
}
+
+/// A shared reset control of a device.
+///
+/// Shared means that other devices may use the same reset line. The deasserts on the line are
+/// counted, and only the first deassert and the last assert act on it. A [`SharedResetControl`]
+/// can only be asserted after it was deasserted, as the C API requires.
+///
+/// A reset line cannot be held by a [`SharedResetControl`] and by an [`ExclusiveResetControl`] at
+/// the same time.
+///
+/// The reset control is released when the [`SharedResetControl`] is dropped. The lifetime `'a` is
+/// that of the reference to the bound device it was obtained from, so a [`SharedResetControl`]
+/// cannot outlive the binding of that device to its driver.
+///
+/// # Examples
+///
+/// Keeping a shared reset line deasserted:
+///
+/// ```
+/// use kernel::{
+/// device::{
+/// Bound,
+/// Device, //
+/// },
+/// reset::{
+/// Deasserted,
+/// SharedResetControl, //
+/// },
+/// };
+///
+/// struct Data<'a> {
+/// // Dropping `Data` asserts the reset control again.
+/// reset: SharedResetControl<'a, Deasserted>,
+/// }
+///
+/// fn probe<'a>(dev: &'a Device<Bound>) -> Result<Data<'a>> {
+/// let reset = SharedResetControl::get(dev, None)?.deassert()?;
+///
+/// Ok(Data { reset })
+/// }
+/// ```
+///
+/// # Invariants
+///
+/// `inner` is a pointer obtained from a successful call to one of the following:
+///
+/// - [`reset_control_get_shared()`]
+/// - [`reset_control_get_optional_shared()`]
+///
+/// It is treated as an opaque token that may only be accessed using C API methods (e.g., it may be
+/// `NULL` if the C API returns `NULL`). The [`SharedResetControl`] owns the reference that the
+/// call returned.
+///
+/// [`reset_control_get_shared()`]:
+/// https://docs.kernel.org/driver-api/reset.html#c.reset_control_get_shared
+/// [`reset_control_get_optional_shared()`]:
+/// https://docs.kernel.org/driver-api/reset.html#c.reset_control_get_optional_shared
+pub struct SharedResetControl<'a, State: SharedResetState> {
+ inner: *mut bindings::reset_control,
+ _phantom: PhantomData<(&'a Device<Bound>, State)>,
+}
+
+// SAFETY: A reset control can be used and released from any thread.
+unsafe impl<State: SharedResetState> Send for SharedResetControl<'_, State> {}
+
+// SAFETY: A `SharedResetControl` has no `&self` methods, so references to it can be shared between
+// threads.
+unsafe impl<State: SharedResetState> Sync for SharedResetControl<'_, State> {}
+
+impl<'a> SharedResetControl<'a, Unknown> {
+ /// Gets the reset line `name` of `dev`, or its first reset line if `name` is `None`.
+ ///
+ /// Equivalent to the kernel's [`reset_control_get_shared()`].
+ ///
+ /// [`reset_control_get_shared()`]:
+ /// https://docs.kernel.org/driver-api/reset.html#c.reset_control_get_shared
+ #[inline]
+ pub fn get(dev: &'a Device<Bound>, name: Option<&CStr>) -> Result<Self> {
+ let id = name.map_or(ptr::null(), |n| n.as_char_ptr());
+
+ // SAFETY: `dev` is a valid device, and `id` is `NULL` or a valid C string.
+ let inner = unsafe { bindings::reset_control_get_shared(dev.as_raw(), id) };
+ let inner = from_err_ptr(inner)?;
+
+ // INVARIANT: `inner` comes from a successful call to `reset_control_get_shared()`, and the
+ // returned value owns the reference from that call.
+ Ok(Self {
+ inner,
+ _phantom: PhantomData,
+ })
+ }
+
+ /// Gets the reset line `name` of `dev`, if there is one.
+ ///
+ /// Behaves the same as [`Self::get`], except when the device has no such reset line or
+ /// `CONFIG_RESET_CONTROLLER` is disabled. In those cases, it returns a [`SharedResetControl`]
+ /// on which every operation succeeds and does nothing.
+ ///
+ /// Equivalent to the kernel's [`reset_control_get_optional_shared()`].
+ ///
+ /// [`reset_control_get_optional_shared()`]:
+ /// https://docs.kernel.org/driver-api/reset.html#c.reset_control_get_optional_shared
+ #[inline]
+ pub fn get_optional(dev: &'a Device<Bound>, name: Option<&CStr>) -> Result<Self> {
+ let id = name.map_or(ptr::null(), |n| n.as_char_ptr());
+
+ // SAFETY: `dev` is a valid device, and `id` is `NULL` or a valid C string.
+ let inner = unsafe { bindings::reset_control_get_optional_shared(dev.as_raw(), id) };
+ let inner = from_err_ptr(inner)?;
+
+ // INVARIANT: `inner` comes from a successful call to
+ // `reset_control_get_optional_shared()`, and the returned value owns the reference from
+ // that call.
+ Ok(Self {
+ inner,
+ _phantom: PhantomData,
+ })
+ }
+
+ /// Deasserts the reset line and moves the [`SharedResetControl`] to the [`Deasserted`] state.
+ ///
+ /// 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<SharedResetControl<'a, Deasserted>, Error<Self>> {
+ // INVARIANT: The reference that `self` owns moves to the value that this returns.
+ let reset = ManuallyDrop::new(self);
+
+ // SAFETY: By the type invariants, `reset.inner` is a pointer that a C getter returned, and
+ // `reset` owns its reference.
+ to_result(unsafe { bindings::reset_control_deassert(reset.inner) })
+ .map(|()| SharedResetControl {
+ inner: reset.inner,
+ _phantom: PhantomData,
+ })
+ .map_err(|error| Error {
+ error,
+ reset: ManuallyDrop::into_inner(reset),
+ })
+ }
+}
+
+impl<'a> SharedResetControl<'a, Deasserted> {
+ /// Asserts the reset line and moves the [`SharedResetControl`] to the [`Unknown`] state.
+ ///
+ /// The line may still be deasserted afterwards, for as long as other users keep it so.
+ ///
+ /// If this fails, the [`SharedResetControl`] is returned in the [`Unknown`] state as well.
+ ///
+ /// Equivalent to 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(
+ self,
+ ) -> Result<SharedResetControl<'a, Unknown>, Error<SharedResetControl<'a, Unknown>>> {
+ // INVARIANT: The reference that `self` owns moves to `reset`.
+ let this = ManuallyDrop::new(self);
+ let reset = SharedResetControl {
+ inner: this.inner,
+ _phantom: PhantomData,
+ };
+
+ // An assert that fails may already have given the deassert back, so `reset` is `Unknown`
+ // in both cases.
+ //
+ // SAFETY: By the type invariants, `reset.inner` is a pointer that a C getter returned, and
+ // `reset` owns its reference.
+ match to_result(unsafe { bindings::reset_control_assert(reset.inner) }) {
+ Ok(()) => Ok(reset),
+ Err(error) => Err(Error { error, reset }),
+ }
+ }
+}
+
+impl<State: SharedResetState> Drop for SharedResetControl<'_, State> {
+ fn drop(&mut self) {
+ if State::ASSERT_ON_DROP {
+ // SAFETY: By the type invariants, `self.inner` is a pointer that a C getter returned,
+ // and `self` owns its reference.
+ unsafe { bindings::reset_control_assert(self.inner) };
+ }
+
+ // SAFETY: By the type invariants, `self` owns the reference that the C getter returned,
+ // so it is safe to relinquish it now.
+ unsafe { bindings::reset_control_put(self.inner) };
+ }
+}
--
2.56.0