[PATCH RESEND v13 2/3] rust: leds: add Mode trait

Markus Probst
2026-04-11T15:07:31Z

Add the `led::Mode` trait to allow for other types of led class devices
in `led::LedOps`.

Signed-off-by: Markus Probst <markus.probst@posteo.de>
---
rust/kernel/led.rs | 28 ++++++++++++++++++++++++----
rust/kernel/led/normal.rs | 24 ++++++++++++++++--------
2 files changed, 40 insertions(+), 12 deletions(-)

diff --git a/rust/kernel/led.rs b/rust/kernel/led.rs
index 1fba512a804c..5035563d68a3 100644
--- a/rust/kernel/led.rs
+++ b/rust/kernel/led.rs
@@ -35,7 +35,7 @@

mod normal;

-pub use normal::Device;
+pub use normal::{Device, Normal};

/// The name of the led is determined by the driver.
pub enum Named {}
@@ -177,6 +177,7 @@ pub fn name(self, name: &'a CStr) -> Self {
/// #[vtable]
/// impl led::LedOps for MyLedOps {
/// type Bus = platform::Device<device::Bound>;
+/// type Mode = led::Normal;
/// const BLOCKING: bool = false;
/// const MAX_BRIGHTNESS: u32 = 255;
///
@@ -209,6 +210,11 @@ pub trait LedOps: Send + 'static + Sized {
#[allow(private_bounds)]
type Bus: AsBusDevice<Bound>;

+ /// The led mode to use.
+ ///
+ /// See [`Mode`].
+ type Mode: Mode;
+
/// If set true, [`LedOps::brightness_set`] and [`LedOps::blink_set`] must perform the
/// operation immediately. If set false, they must not sleep.
const BLOCKING: bool;
@@ -221,12 +227,16 @@ pub trait LedOps: Send + 'static + Sized {
fn brightness_set(
&self,
dev: &Self::Bus,
- classdev: &Device<Self>,
+ classdev: &<Self::Mode as Mode>::Device<Self>,
brightness: u32,
) -> Result<()>;

/// Gets the current brightness level.
- fn brightness_get(&self, dev: &Self::Bus, classdev: &Device<Self>) -> u32 {
+ fn brightness_get(
+ &self,
+ dev: &Self::Bus,
+ classdev: &<Self::Mode as Mode>::Device<Self>,
+ ) -> u32 {
let _ = (dev, classdev);
build_error!(VTABLE_DEFAULT_ERROR)
}
@@ -242,7 +252,7 @@ fn brightness_get(&self, dev: &Self::Bus, classdev: &Device<Self>) -> u32 {
fn blink_set(
&self,
dev: &Self::Bus,
- classdev: &Device<Self>,
+ classdev: &<Self::Mode as Mode>::Device<Self>,
delay_on: &mut usize,
delay_off: &mut usize,
) -> Result<()> {
@@ -305,6 +315,16 @@ fn try_from(value: u32) -> core::result::Result<Self, Self::Error> {
}
}

+/// The led mode.
+///
+/// Each led mode has its own led class device type with different capabilities.
+///
+/// See [`Normal`].
+pub trait Mode: private::Sealed {
+ /// The class device for the led mode.
+ type Device<T: LedOps<Mode = Self>>;
+}
+
mod private {
pub trait Sealed {}
}
diff --git a/rust/kernel/led/normal.rs b/rust/kernel/led/normal.rs
index bd239f186c64..dda247145f25 100644
--- a/rust/kernel/led/normal.rs
+++ b/rust/kernel/led/normal.rs
@@ -6,11 +6,19 @@

use super::*;

+/// The led mode for the `struct led_classdev`. Leds with this mode can only have a fixed color.
+pub enum Normal {}
+
+impl Mode for Normal {
+ type Device<T: LedOps<Mode = Self>> = Device<T>;
+}
+impl private::Sealed for Normal {}
+
/// The led class device representation.
///
/// This structure represents the Rust abstraction for a led class device.
#[pin_data(PinnedDrop)]
-pub struct Device<T: LedOps> {
+pub struct Device<T: LedOps<Mode = Normal>> {
#[pin]
ops: T,
#[pin]
@@ -19,7 +27,7 @@ pub struct Device<T: LedOps> {

impl<'a, S: DeviceBuilderState> DeviceBuilder<'a, S> {
/// Registers a new [`Device`].
- pub fn build<T: LedOps>(
+ pub fn build<T: LedOps<Mode = Normal>>(
self,
parent: &'a T::Bus,
ops: impl PinInit<T, Error> + 'a,
@@ -89,7 +97,7 @@ pub fn build<T: LedOps>(
}
}

-impl<T: LedOps> Device<T> {
+impl<T: LedOps<Mode = Normal>> Device<T> {
/// # Safety
/// `led_cdev` must be a valid pointer to a `led_classdev` embedded within a
/// `led::Device`.
@@ -107,17 +115,17 @@ fn parent(&self) -> &device::Device<Bound> {
}

// SAFETY: A `led::Device` can be unregistered from any thread.
-unsafe impl<T: LedOps + Send> Send for Device<T> {}
+unsafe impl<T: LedOps<Mode = Normal> + Send> Send for Device<T> {}

// SAFETY: `led::Device` can be shared among threads because all methods of `led::Device`
// are thread safe.
-unsafe impl<T: LedOps + Sync> Sync for Device<T> {}
+unsafe impl<T: LedOps<Mode = Normal> + Sync> Sync for Device<T> {}

-struct Adapter<T: LedOps> {
+struct Adapter<T: LedOps<Mode = Normal>> {
_p: PhantomData<T>,
}

-impl<T: LedOps> Adapter<T> {
+impl<T: LedOps<Mode = Normal>> Adapter<T> {
/// # Safety
/// `led_cdev` must be a valid pointer to a `led_classdev` embedded within a
/// `led::Device`.
@@ -203,7 +211,7 @@ impl<T: LedOps> Adapter<T> {
}

#[pinned_drop]
-impl<T: LedOps> PinnedDrop for Device<T> {
+impl<T: LedOps<Mode = Normal>> PinnedDrop for Device<T> {
fn drop(self: Pin<&mut Self>) {
let raw = self.classdev.get();
// SAFETY: The existence of `self` guarantees that `self.classdev.get()` is a pointer to a

--
2.52.0