Rust LED Driver Abstractions and Lenovo SE10 Driver with DMI and I/O Port Support
Subsystems (3)
v2 cover Rust LED Driver Abstractions and Lenovo SE10 Driver with DMI and I/O Port Support
This patch series introduces Rust LED abstractions and provides a driver for the Lenovo ThinkEdge SE10 using those abstractions.
1. DMI Abstraction:
- Introduced Rust macros and types for DMI-based system matching, providing functionality similar to the MODULE_DEVICE_TABLE(dmi, x) macro in C. This abstraction allows system-specific matching using DMI fields.
2. LED Abstractions:
- Introduced a generic LED abstraction that supports on/off control, brightness levels, and hardware blinking.
- The abstraction is implemented using a vtable, with a struct that holds the generic driver data and is pinned for direct LED registration.
3. I/O Port Abstractions:
- Added abstractions for hardware I/O port memory access, enabling the driver to interact with hardware ports.
4. LED Driver for Lenovo ThinkEdge SE10:
- A driver is implemented for the red LED on the front panel of the Lenovo ThinkEdge SE10, utilizing the previously introduced LED abstractions, I/O port abstractions, and DMI matching.
- The driver supports on/off control and a WWAN hardware trigger.
Additionally, the LED abstraction has been prepared to support multicolor LEDs in the future. While this functionality is planned, it has not yet been implemented, as I do not have a consumer for it at this time.
Would it make sense to add the Rust DMI abstractions to the existing DMI/SMBIOS SUPPORT entry in the MAINTAINERS file?
This series depends on rust: time: Introduce Delta type by FUJITA Tomonori[1].
I send this previously without the SE10 driver as an RFC[2], and now have a driver to consume the APIs.
[1]: https://lore.kernel.org/rust-for-linux/[email protected]/
[2]: https://lore.kernel.org/rust-for-linux/[email protected]/
Fiona Behrens (5):
rust: dmi: Add abstractions for DMI
rust: leds: Add Rust bindings for LED subsystem
rust: leds: Add hardware trigger support for hardware-controlled LEDs
rust: add I/O port abstractions with resource management
leds: leds_lenovo_se10: LED driver for Lenovo SE10 platform
MAINTAINERS | 2 + drivers/leds/Kconfig | 10 + drivers/leds/Makefile | 1 + drivers/leds/leds_lenovo_se10.rs | 132 ++++++++ rust/bindings/bindings_helper.h | 1 + rust/helpers/helpers.c | 1 + rust/helpers/ioport.c | 42 +++ rust/kernel/dmi.rs | 533 +++++++++++++++++++++++++++++++ rust/kernel/ioport.rs | 169 ++++++++++ rust/kernel/leds.rs | 416 ++++++++++++++++++++++++ rust/kernel/leds/triggers.rs | 128 ++++++++ rust/kernel/lib.rs | 6 + rust/kernel/time.rs | 7 + 13 files changed, 1448 insertions(+) create mode 100644 drivers/leds/leds_lenovo_se10.rs create mode 100644 rust/helpers/ioport.c create mode 100644 rust/kernel/dmi.rs create mode 100644 rust/kernel/ioport.rs create mode 100644 rust/kernel/leds.rs create mode 100644 rust/kernel/leds/triggers.rs
Signature · 1 line
2.47.0
PATCH v2 1/5 rust: dmi: Add abstractions for DMI
Introduce Rust macros and types to support DMI-based system matching, providing functionality similar to the MODULE_DEVICE_TABLE(dmi, x) macro in C.
- Add the dmi_system_id! macro for defining system identifiers and matching specific DMI fields.
- Add the dmi_device_table! macro, which allows module aliases to be available after a full build when compiled as a module.
- Define the Field enum for DMI field IDs, ensuring compatibility with existing C bindings.
These abstractions enable writing Rust kernel drivers that rely on DMI data for system-specific behavior and autoloading.
--- rust/bindings/bindings_helper.h | 1 + rust/kernel/dmi.rs | 533 ++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 2 + 3 files changed, 536 insertions(+) create mode 100644 rust/kernel/dmi.rs
rust/bindings/bindings_helper.h +1 -0
@@ -11,6 +11,7 @@#include <linux/blk_types.h> #include <linux/blkdev.h> #include <linux/cred.h> +#include <linux/dmi.h> #include <linux/errname.h> #include <linux/ethtool.h> #include <linux/file.h>
rust/kernel/dmi.rs new file +533 -0
533 lines not on this page · raw message
rust/kernel/lib.rs +2 -0
@@ -35,6 +35,8 @@mod build_assert;pub mod cred;pub mod device; +#[cfg( CONFIG_DMI )] +pub mod dmi;pub mod error;#[cfg( CONFIG_RUST_FW_LOADER_ABSTRACTIONS )] pub mod firmware;
Signature · 1 line
2.47.0
PATCH v2 2/5 rust: leds: Add Rust bindings for LED subsystem
In reply to Fiona BehrensThis patch introduces Rust support for the LED subsystem, adding abstractions for the led_classdev struct and implementing core LED operations such as brightness_set, brightness_get, and blink_set via a vtable trait.
A Color enum is defined to represent various LED colors, and a LedConfig struct is used to configure LED properties such as name and color. The Led struct wraps the C led_classdev and ensures proper memory management by being pinned and automatically unregistered when dropped.
Support for a devm register variant is not included in this patch.
--- MAINTAINERS | 1 + rust/kernel/leds.rs | 341 ++++++++++++++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 2 + rust/kernel/time.rs | 7 + 4 files changed, 351 insertions(+) create mode 100644 rust/kernel/leds.rs
MAINTAINERS +1 -0
@@ -13019,6 +13019,7 @@ F: Documentation/leds/ F: drivers/leds/ F: include/dt-bindings/leds/ F: include/linux/leds.h +F: rust/kernel/leds.rs LEGO MINDSTORMS EV3 R: David Lechner <[email protected]>
rust/kernel/leds.rs new file +341 -0
341 lines not on this page · raw message
rust/kernel/lib.rs +2 -0
@@ -46,6 +46,8 @@pub mod jump_label;#[cfg( CONFIG_KUNIT )] pub mod kunit; +#[cfg( CONFIG_NEW_LEDS )] +pub mod leds;pub mod list;pub mod miscdevice;#[cfg( CONFIG_NET )]
rust/kernel/time.rs +7 -0
@@ -143,4 +143,11 @@ pub fn as_nanos(self) -> i64 {pub fn as_micros_ceil (self) ->i64 { self.as_nanos ().saturating_add (NSEC_PER_USEC -1 ) /NSEC_PER_USEC } + +/// Return the smallest number of milliseconds greater than or equal +/// to the value in the `Delta`. +#[inline] +pub fn as_millis_ceil (self) ->i64 { + self.as_nanos ().saturating_add (NSEC_PER_MSEC -1 ) /NSEC_PER_MSEC + } }
Signature · 1 line
2.47.0
Re: [PATCH v2 2/5] rust: leds: Add Rust bindings for LED subsystem
Hi Fiona,
Thanks for this! I noticed a procedural thing (the last diff chunk, please see below), so I took the chance to give a few quick surface-level comments I noticed while scrolling, mostly on docs :)
Quoted 2 lines · Fiona Behrens
+ /// Get String representation of the Color.
[String], [Color]
i.e. please format, and use intra-doc links where possible/reasonable.
Quoted 1 line
+ // SAFETY: pointer from above, valid for static lifetime.
In general, try to explain why, not just what, in the safety comments, i.e. why it is valid for a static lifetime? e.g. does the C API guarantee it?
rust/kernel/leds.rs line 57
Quoted 3 lines
rust/kernel/leds.rs new file +3
-0+/// Get String representation of the Color as rust type. +#[inline] +pub fn name (&self) ->Option <&'staticstr > {
"Rust type"
However, I am not sure what it is trying to say. I would have thought it is a custom newtype of something or perhaps something strange is going on, but I guess you are referring to str vs. CStr? In general, I don't think we need to say "as a Rust type" -- it may confuse more than help.
Quoted 1 line
+ // SAFETY: name from C name array which is valid UTF-8.
What guarantees this? i.e. I imagine you looked into all the cases from the static table, so I would for instance say: "All values in the C name array are valid UTF-8." or similar (perhaps mention which array, so that people can e.g. grep).
rust/kernel/leds.rs line 68
Quoted 5 lines
rust/kernel/leds.rs new file +5
-0+impl core::fmt::Display for Color { +fn fmt (&self, f: &mut core::fmt::Formatter <'_>) -> core::fmt::Result { + f.write_str (self.name ().unwrap_or ("Invalid color" )) + } +}
Can this happen? If not, should Color have an # Invariant and would we need the Option<...> for names then?
In any case, here, shouldn't the error be bubbled up?
Quoted 1 line
+ /// Tris to convert a u32 into a [
Color].
Typo: Tries
Format: [u32]
There are also other typos ("brightnes", "Activae") -- I recommend checkpatch.pl with the --codespell flag which may help catching some of these.
rust/kernel/leds.rs line 250
Quoted 4 lines
rust/kernel/leds.rs new file +4
-0+/// Get the LED brightness level. +fn brightness_get (_this: &mut Self ::This ) ->u8 { +crate ::build_error (crate ::error::VTABLE_DEFAULT_ERROR ) + }
Please use the macro instead (see 15f2f9313a39 ("rust: use the build_error! macro, not the hidden function")). Soon we will have it in the prelude too (see 4401565fe92b ("rust: add build_error! to the prelude")), by the way.
rust/kernel/leds.rs line 312
Quoted 2 lines
rust/kernel/leds.rs new file +2
-0+// SAFETY: By the safety requirement of this function `delay_on` is pointing to a valid `c_ulong`. +let on =Delta ::from_millis (unsafe { *delay_on }as i64 );
I didn't look into most comments, but e.g. I noticed this one does not look correct -- the safety requirements for this function do not mention anything about delay_on, no?
Quoted 5 lines
rust/kernel/time.rs +0
-0
rust/kernel/time.rs pub fn as_nanos(self) -> i64 { · line 152
Quoted 10 lines
rust/kernel/time.rs +7
-0@@ -143,4 +143,11 @@ pub fn as_nanos(self) -> i64 {pub fn as_micros_ceil (self) ->i64 { self.as_nanos ().saturating_add (NSEC_PER_USEC -1 ) /NSEC_PER_USEC } + +/// Return the smallest number of milliseconds greater than or equal +/// to the value in the `Delta`. +#[inline] +pub fn as_millis_ceil (self) ->i64 { + self.as_nanos ().saturating_add (NSEC_PER_MSEC -1 ) /NSEC_PER_MSEC + }
This should probably be its own patch, with Cc to the timekeeping maintainers etc.
Cheers,
Miguel
PATCH v2 3/5 rust: leds: Add hardware trigger support for hardware-controlled LEDs
In reply to Fiona BehrensAdds abstraction for hardware trigger support in LEDs, enabling LEDs to be controlled by external hardware events.
An Arc is embedded within the led_classdev to manage the lifecycle of the hardware trigger, ensuring proper reference counting and cleanup when the LED is dropped.
--- MAINTAINERS | 1 + rust/kernel/leds.rs | 95 +++++++++++++++++++++++--- rust/kernel/leds/triggers.rs | 128 +++++++++++++++++++++++++++++++++++ 3 files changed, 214 insertions(+), 10 deletions(-) create mode 100644 rust/kernel/leds/triggers.rs
MAINTAINERS +1 -0
@@ -13020,6 +13020,7 @@ F: drivers/leds/ F: include/dt-bindings/leds/ F: include/linux/leds.h F: rust/kernel/leds.rs +F: rust/kernel/leds/ LEGO MINDSTORMS EV3 R: David Lechner <[email protected]>
rust/kernel/leds.rs +85 -10
134 lines not on this page · raw message
rust/kernel/leds/triggers.rs new file +128 -0
128 lines not on this page · raw message
Signature · 1 line
2.47.0
Re: [PATCH v2 3/5] rust: leds: Add hardware trigger support for hardware-controlled LEDs
Quoted 186 lines · Fiona Behrens
Adds abstraction for hardware trigger support in LEDs, enabling LEDs to be controlled by external hardware events.
An
Arcis embedded within theled_classdevto manage the lifecycle of the hardware trigger, ensuring proper reference counting and cleanup when the LED is dropped.Signed-off-by Fiona Behrens <[email protected]>--- MAINTAINERS | 1 + rust/kernel/leds.rs | 95 +++++++++++++++++++++++--- rust/kernel/leds/triggers.rs | 128 +++++++++++++++++++++++++++++++++++ 3 files changed, 214 insertions(+), 10 deletions(-) create mode 100644 rust/kernel/leds/triggers.rsMAINTAINERS +1
-0@@ -13020,6 +13020,7 @@ F: drivers/leds/ F: include/dt-bindings/leds/ F: include/linux/leds.h F: rust/kernel/leds.rs +F: rust/kernel/leds/ LEGO MINDSTORMS EV3 R: David Lechner <[email protected]>rust/kernel/leds.rs +85
-10@@ -10,9 +10,14 @@use crate ::error::from_result;use crate ::ffi::c_ulong;use crate ::prelude::*; +#[cfg( CONFIG_LEDS_TRIGGERS )] +use crate ::sync::Arc ;use crate ::time::Delta ;use crate ::types::Opaque ; +#[cfg( CONFIG_LEDS_TRIGGERS )] +pub mod triggers; +/// Color of an LED. #[allow(missing_docs)] #[derive( Copy , Clone )] @@ -110,12 +115,34 @@ fn try_from(value: u32) -> Result<Self, Self::Error> { }/// Data used for led registration. -#[derive( Clone )] -pub struct LedConfig <'name> { +pub struct LedConfig <'name,T > {/// Name to give the led. pub name:Option <&'nameCStr >,/// Color of the LED. pub color:Color , +/// Private data of the LED. +pub data:T , + +/// Default trigger name. +pub default_trigger:Option <&'staticCStr >, +/// Hardware trigger. +/// +/// Setting this to some also defaults the default trigger to this hardware trigger. +/// Use `default_trigger: Some("none")` to overwrite this. +#[cfg( CONFIG_LEDS_TRIGGERS )] +pub hardware_trigger:Option <Arc <triggers::Hardware <T >>>, +} + +impl <'name,T >LedConfig <'name,T > { +/// Create a new LedConfig +pub fn new (color:Color , data:T ) ->Self { +Self { + color, + data, +// SAFETY: all other fields are valid with zeroes. + ..unsafe { core::mem::zeroed () } + } + } }/// A Led backed by a C `struct led_classdev`, additionally offering @@ -141,8 +168,7 @@ impl<T> Led<T>#[cfg( CONFIG_LEDS_CLASS )] pub fn register <'a>( device:Option <&'aDevice >, - config: &'aLedConfig <'a>, - data:T , + config:LedConfig <'a,T >, ) ->impl PinInit <Self ,Error > + 'awhere T : 'a,@@ -188,14 +214,46 @@ pub fn register<'a>(unsafe { ptr::write (set_fn_ptr,Some (blink_set ::<T >)) }; } +#[cfg( CONFIG_LEDS_TRIGGERS )] +if let Some (trigger) = config.hardware_trigger { +let trigger = trigger.into_raw (); +// SAFETY: `place` is pointing to a live allocation. +let trigger_type_ptr =unsafe { ptr::addr_of_mut!((*place).trigger_type) }; +// SAFETY: `trigger` is a valid pointer +let hw_trigger =unsafe { ptr::addr_of!((*trigger).hw_type) }; +// SAFETY: `trigger_type_ptr` points to a valid allocation and we have exclusive access. +unsafe { ptr::write (trigger_type_ptr, hw_trigger.cast_mut ().cast ()) }; + +// SAFETY: trigger points to a valid hardware trigger struct. +let trigger_name_ptr =unsafe {Opaque ::raw_get (ptr::addr_of!( (*trigger).trigger)) }; +// SAFETY: trigger points to a valid hardware trigger struct. +let trigger_name_ptr =unsafe { (*trigger_name_ptr).name }; +// SAFETY: `place` is pointing to a live allocation. +let default_trigger_ptr =unsafe { ptr::addr_of_mut!((*place).default_trigger) }; +// SAFETY: `default_trigger_ptr` points to a valid allocation and we have exclusive access. +unsafe { ptr::write (default_trigger_ptr, trigger_name_ptr) }; + +// SAFETY: `place` is pointing to a live allocation. +let hw_ctrl_trigger_ptr =unsafe { ptr::addr_of_mut!((*place).hw_control_trigger) }; +// SAFETY: `hw_ctrl_trigger_ptr` points to a valid allocation and we have exclusive access. +unsafe { ptr::write (hw_ctrl_trigger_ptr, trigger_name_ptr) }; + } + +// After hw trigger impl, to overwrite default trigger +if let Some (default_trigger) = config.default_trigger { +// SAFETY: `place` is pointing to a live allocation. +let default_trigger_ptr =unsafe { ptr::addr_of_mut!((*place).default_trigger) }; +// SAFETY: `default_trigger_ptr` points to a valid allocation and we have exclusive access. +unsafe { ptr::write (default_trigger_ptr, default_trigger.as_char_ptr ()) }; + } -let dev = device.map (|dev| dev.as_raw ()).unwrap_or (ptr::null_mut ()); -// SAFETY: `place` is a pointer to a live allocation of `bindings::led_classdev`. -crate ::error::to_result (unsafe { - bindings::led_classdev_register_ext (dev, place, ptr::null_mut ()) - }) +let dev = device.map (|dev| dev.as_raw ()).unwrap_or (ptr::null_mut ()); +// SAFETY: `place` is a pointer to a live allocation of `bindings::led_classdev`. +crate ::error::to_result (unsafe { + bindings::led_classdev_register_ext (dev, place, ptr::null_mut ()) + }) }), - data: data, + data: config.data, }) } }@@ -220,6 +278,23 @@ fn drop(self: Pin<&mut Self>) {unsafe { bindings::led_classdev_unregister (self.led.get ()) } + +// drop trigger if there is a hw trigger defined. +#[cfg( CONFIG_LEDS_TRIGGERS )] + { +// SAFETY: `self.led` is a valid led. +let hw_trigger_type = +unsafe { ptr::read (ptr::addr_of!((*self.led.get ()).trigger_type)) }; +if !hw_trigger_type.is_null () { +// SAFETY: hw_trigger_type is a valid and non null pointer into a Hardware trigger. +let hw_trigger_type =unsafe { +crate ::container_of!(hw_trigger_type, triggers::Hardware <T >, hw_type) + }; +// SAFETY: `hw_trigger_type` is a valid pointer that came from an arc. +let hw_trigger_type =unsafe {Arc ::from_raw (hw_trigger_type) }; +drop (hw_trigger_type); + } + } } }rust/kernel/leds/triggers.rs new file +10
-0@@ -0,0 +1,128 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! LED trigger abstractions. + +use core::marker::PhantomData ; +use core::ptr; + +use crate ::error::{from_result, to_result}; +use crate ::prelude::*; +use crate ::types::Opaque ;
rust/kernel/leds/triggers.rs line 20
Quoted 10 lines
rust/kernel/leds/triggers.rs new file +10
-0+ +use super ::FromLedClassdev ; + +/// LED Hardware trigger. +/// +/// Used to impement a hardware operation mode for an LED. +#[pin_data( PinnedDrop )] +pub struct Hardware <T > { +#[pin] +pub (crate ) hw_type:Opaque <bindings::led_hw_trigger_type >,
This should probably be called trigger_type instead of hw_type, as it is in the C version of the code.
Quoted 67 lines
rust/kernel/leds/triggers.rs new file +67
-0+#[pin] + pub(crate) trigger: Opaque<bindings::led_trigger>, + _t: PhantomData<T>, +} + +impl <T >Hardware <T > +where +T :HardwareOperations , +{ +/// Register a new hardware Trigger with a given name. +pub fn register (name: &'staticCStr ) ->impl PinInit <Self ,Error > { +try_pin_init! (Self { +// SAFETY: `led_hw_trigger_type` is valid with all zeroes. + hw_type:Opaque ::new (unsafe { core::mem::zeroed () }), + trigger <-Opaque ::try_ffi_init (move |place: *mut bindings::led_trigger | { +// SAFETY: `place` is a pointer to a live allocation, so erasing is valid. +unsafe { place.write_bytes (0 ,1 ) }; + +// Add name +// SAFETY: `place` is pointing to a live allocation, so the deref is safe. +let name_ptr =unsafe { ptr::addr_of_mut!((*place).name) }; +// SAFETY: `name_ptr` points to a valid allocation and we have exclusive access. +unsafe { ptr::write (name_ptr, name.as_char_ptr ()) }; + +// Add fn pointers +// SAFETY: `place` is pointing to a live allocation, so the deref is safe. +let activate_fn_ptr: *mut Option <_ > =unsafe { ptr::addr_of_mut!((*place).activate) }; +// SAFETY: `activate_fn_ptr` points to a valid allocation and we have exclusive access. +unsafe { ptr::write (activate_fn_ptr,Some (trigger_activate ::<T >)) }; + +if T ::HAS_DEACTIVATE { +// SAFETY: `place` is pointing to a live allocation, so the deref is safe. +let deactivate_fn_ptr: *mut Option <_ > =unsafe { ptr::addr_of_mut!((*place).deactivate) }; +// SAFETY: `deactivate_fn_ptr` points to a valid allocation and we have exclusive access. +unsafe { ptr::write (deactivate_fn_ptr,Some (trigger_deactivate ::<T >)) }; + } + +// Add hardware trigger +// SAFETY: `place` is pointing to a live allocation, so the deref is safe. +let trigger_type_ptr =unsafe { ptr::addr_of_mut!((*place).trigger_type) }; +// SAFETY: `place` is pointing to a live allocation, so the deref is safe. +let trigger_type =unsafe {crate ::container_of!(place,Self , trigger).cast_mut () }; +// SAFETY: `trigger_type` is pointing to a live allocation of Self. +let trigger_type =unsafe { ptr::addr_of!((*trigger_type).hw_type) }; +// SAFETY: `trigger_type_ptr` points to a valid allocation and we have exclusive access. +unsafe { ptr::write (trigger_type_ptr,Opaque ::raw_get (trigger_type)) }; + +// SAFETY: ffi call, `place` is sufficently filled with data at this point +to_result (unsafe { + bindings::led_trigger_register (place) + }) + }), + _t:PhantomData , + }) + } +} + +#[pinned_drop] +impl <T >PinnedDrop for Hardware <T > { +fn drop (self:Pin <&mut Self >) { +// SAFETY: trigger is pointing to a live and registered allocation +unsafe { + bindings::led_trigger_unregister (self.trigger.get ()); + } + } +} +
rust/kernel/leds/triggers.rs line 97
Quoted 10 lines
rust/kernel/leds/triggers.rs new file +10
-0+/// Operations for the Hardware trigger +#[macros::vtable] +pub trait HardwareOperations :super ::Operations { +/// Activate the hardware trigger. +fn activate (this: &mut Self ::This ) ->Result ; +/// Deactivate the hardware trigger. +fn deactivate (_this: &mut Self ::This ) { +crate ::build_error (crate ::error::VTABLE_DEFAULT_ERROR ) + } +}
This looks as if you are doing a Rust binding for struct led_trigger. But you keep calling it Hardware trigger, which makes me thing that you are confused about what is a LED trigger and what is a hardware trigger.
Why do you keep putting "Hardware" into the names of these symbols?
I fear that you may be confused about some stuff here. In order to determine whether this is the case, could you answer the following questions please?
- What is the purpose of struct led_hw_trigger_type?
- What is the purpose of the dummy member of this struct? What value should be assigned to it?
- If a LED class device (LED cdev) has the trigger_type member set to NULL, which LED triggers will be listed in the sysfs trigger file for this LED cdev? And which triggers will be listed if the trigger_type member is not NULL?
- Why does both struct led_classdev and struct led_trigger have the trigger_type member?
Quoted 34 lines
rust/kernel/leds/triggers.rs new file +30
-0+/// `trigger_activate` function pointer +/// +/// # Safety +/// +/// `led_cdev` must be passed by the corresponding callback in `led_trigger`. +unsafe extern "C" fn trigger_activate <T >(led_cdev: *mut bindings::led_classdev ) ->i32 +where +T :HardwareOperations , +{ +from_result (|| { +// SAFETY: By the safety requirement of this function `led_cdev` is embedded inside a `T::This<T>`. +let led =unsafe { &mut *(T ::This ::led_container_of (led_cdev.cast ())) }; +T ::activate (led)?; +Ok (0 ) + }) +} + +/// `trigger_deactivate` function pointer +/// +/// # Safety +/// +/// `led_cdev` must be passed by the corresponding callback in `led_trigger`. +unsafe extern "C" fn trigger_deactivate <T >(led_cdev: *mut bindings::led_classdev ) +where +T :HardwareOperations , +{ +// SAFETY: By the safety requirement of this function `led_cdev` is embedded inside a `T::This<T>`. +let led =unsafe { &mut *(T ::This ::led_container_of (led_cdev.cast ())) }; +T ::deactivate (led) +}Signature · 1 line
2.47.0
Re: [PATCH v2 3/5] rust: leds: Add hardware trigger support for hardware-controlled LEDs
Hi,
Quoted 284 lines · Marek Behún
Quoted 186 lines · Fiona Behrens
Adds abstraction for hardware trigger support in LEDs, enabling LEDs to be controlled by external hardware events.
An
Arcis embedded within theled_classdevto manage the lifecycle of the hardware trigger, ensuring proper reference counting and cleanup when the LED is dropped.Signed-off-by Fiona Behrens <[email protected]>--- MAINTAINERS | 1 + rust/kernel/leds.rs | 95 +++++++++++++++++++++++--- rust/kernel/leds/triggers.rs | 128 +++++++++++++++++++++++++++++++++++ 3 files changed, 214 insertions(+), 10 deletions(-) create mode 100644 rust/kernel/leds/triggers.rsMAINTAINERS +1
-0@@ -13020,6 +13020,7 @@ F: drivers/leds/ F: include/dt-bindings/leds/ F: include/linux/leds.h F: rust/kernel/leds.rs +F: rust/kernel/leds/ LEGO MINDSTORMS EV3 R: David Lechner <[email protected]>rust/kernel/leds.rs +85
-10@@ -10,9 +10,14 @@use crate ::error::from_result;use crate ::ffi::c_ulong;use crate ::prelude::*; +#[cfg( CONFIG_LEDS_TRIGGERS )] +use crate ::sync::Arc ;use crate ::time::Delta ;use crate ::types::Opaque ; +#[cfg( CONFIG_LEDS_TRIGGERS )] +pub mod triggers; +/// Color of an LED. #[allow(missing_docs)] #[derive( Copy , Clone )] @@ -110,12 +115,34 @@ fn try_from(value: u32) -> Result<Self, Self::Error> { }/// Data used for led registration. -#[derive( Clone )] -pub struct LedConfig <'name> { +pub struct LedConfig <'name,T > {/// Name to give the led. pub name:Option <&'nameCStr >,/// Color of the LED. pub color:Color , +/// Private data of the LED. +pub data:T , + +/// Default trigger name. +pub default_trigger:Option <&'staticCStr >, +/// Hardware trigger. +/// +/// Setting this to some also defaults the default trigger to this hardware trigger. +/// Use `default_trigger: Some("none")` to overwrite this. +#[cfg( CONFIG_LEDS_TRIGGERS )] +pub hardware_trigger:Option <Arc <triggers::Hardware <T >>>, +} + +impl <'name,T >LedConfig <'name,T > { +/// Create a new LedConfig +pub fn new (color:Color , data:T ) ->Self { +Self { + color, + data, +// SAFETY: all other fields are valid with zeroes. + ..unsafe { core::mem::zeroed () } + } + } }/// A Led backed by a C `struct led_classdev`, additionally offering @@ -141,8 +168,7 @@ impl<T> Led<T>#[cfg( CONFIG_LEDS_CLASS )] pub fn register <'a>( device:Option <&'aDevice >, - config: &'aLedConfig <'a>, - data:T , + config:LedConfig <'a,T >, ) ->impl PinInit <Self ,Error > + 'awhere T : 'a,@@ -188,14 +214,46 @@ pub fn register<'a>(unsafe { ptr::write (set_fn_ptr,Some (blink_set ::<T >)) }; } +#[cfg( CONFIG_LEDS_TRIGGERS )] +if let Some (trigger) = config.hardware_trigger { +let trigger = trigger.into_raw (); +// SAFETY: `place` is pointing to a live allocation. +let trigger_type_ptr =unsafe { ptr::addr_of_mut!((*place).trigger_type) }; +// SAFETY: `trigger` is a valid pointer +let hw_trigger =unsafe { ptr::addr_of!((*trigger).hw_type) }; +// SAFETY: `trigger_type_ptr` points to a valid allocation and we have exclusive access. +unsafe { ptr::write (trigger_type_ptr, hw_trigger.cast_mut ().cast ()) }; + +// SAFETY: trigger points to a valid hardware trigger struct. +let trigger_name_ptr =unsafe {Opaque ::raw_get (ptr::addr_of!( (*trigger).trigger)) }; +// SAFETY: trigger points to a valid hardware trigger struct. +let trigger_name_ptr =unsafe { (*trigger_name_ptr).name }; +// SAFETY: `place` is pointing to a live allocation. +let default_trigger_ptr =unsafe { ptr::addr_of_mut!((*place).default_trigger) }; +// SAFETY: `default_trigger_ptr` points to a valid allocation and we have exclusive access. +unsafe { ptr::write (default_trigger_ptr, trigger_name_ptr) }; + +// SAFETY: `place` is pointing to a live allocation. +let hw_ctrl_trigger_ptr =unsafe { ptr::addr_of_mut!((*place).hw_control_trigger) }; +// SAFETY: `hw_ctrl_trigger_ptr` points to a valid allocation and we have exclusive access. +unsafe { ptr::write (hw_ctrl_trigger_ptr, trigger_name_ptr) }; + } + +// After hw trigger impl, to overwrite default trigger +if let Some (default_trigger) = config.default_trigger { +// SAFETY: `place` is pointing to a live allocation. +let default_trigger_ptr =unsafe { ptr::addr_of_mut!((*place).default_trigger) }; +// SAFETY: `default_trigger_ptr` points to a valid allocation and we have exclusive access. +unsafe { ptr::write (default_trigger_ptr, default_trigger.as_char_ptr ()) }; + } -let dev = device.map (|dev| dev.as_raw ()).unwrap_or (ptr::null_mut ()); -// SAFETY: `place` is a pointer to a live allocation of `bindings::led_classdev`. -crate ::error::to_result (unsafe { - bindings::led_classdev_register_ext (dev, place, ptr::null_mut ()) - }) +let dev = device.map (|dev| dev.as_raw ()).unwrap_or (ptr::null_mut ()); +// SAFETY: `place` is a pointer to a live allocation of `bindings::led_classdev`. +crate ::error::to_result (unsafe { + bindings::led_classdev_register_ext (dev, place, ptr::null_mut ()) + }) }), - data: data, + data: config.data, }) } }@@ -220,6 +278,23 @@ fn drop(self: Pin<&mut Self>) {unsafe { bindings::led_classdev_unregister (self.led.get ()) } + +// drop trigger if there is a hw trigger defined. +#[cfg( CONFIG_LEDS_TRIGGERS )] + { +// SAFETY: `self.led` is a valid led. +let hw_trigger_type = +unsafe { ptr::read (ptr::addr_of!((*self.led.get ()).trigger_type)) }; +if !hw_trigger_type.is_null () { +// SAFETY: hw_trigger_type is a valid and non null pointer into a Hardware trigger. +let hw_trigger_type =unsafe { +crate ::container_of!(hw_trigger_type, triggers::Hardware <T >, hw_type) + }; +// SAFETY: `hw_trigger_type` is a valid pointer that came from an arc. +let hw_trigger_type =unsafe {Arc ::from_raw (hw_trigger_type) }; +drop (hw_trigger_type); + } + } } }rust/kernel/leds/triggers.rs new file +10
-0@@ -0,0 +1,128 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! LED trigger abstractions. + +use core::marker::PhantomData ; +use core::ptr; + +use crate ::error::{from_result, to_result}; +use crate ::prelude::*; +use crate ::types::Opaque ;rust/kernel/leds/triggers.rs line 20
Quoted 10 lines
rust/kernel/leds/triggers.rs new file +10
-0+ +use super ::FromLedClassdev ; + +/// LED Hardware trigger. +/// +/// Used to impement a hardware operation mode for an LED. +#[pin_data( PinnedDrop )] +pub struct Hardware <T > { +#[pin] +pub (crate ) hw_type:Opaque <bindings::led_hw_trigger_type >,This should probably be called
trigger_typeinstead ofhw_type, as it is in the C version of the code.Quoted 67 lines
rust/kernel/leds/triggers.rs new file +67
-0+#[pin] + pub(crate) trigger: Opaque<bindings::led_trigger>, + _t: PhantomData<T>, +} + +impl <T >Hardware <T > +where +T :HardwareOperations , +{ +/// Register a new hardware Trigger with a given name. +pub fn register (name: &'staticCStr ) ->impl PinInit <Self ,Error > { +try_pin_init! (Self { +// SAFETY: `led_hw_trigger_type` is valid with all zeroes. + hw_type:Opaque ::new (unsafe { core::mem::zeroed () }), + trigger <-Opaque ::try_ffi_init (move |place: *mut bindings::led_trigger | { +// SAFETY: `place` is a pointer to a live allocation, so erasing is valid. +unsafe { place.write_bytes (0 ,1 ) }; + +// Add name +// SAFETY: `place` is pointing to a live allocation, so the deref is safe. +let name_ptr =unsafe { ptr::addr_of_mut!((*place).name) }; +// SAFETY: `name_ptr` points to a valid allocation and we have exclusive access. +unsafe { ptr::write (name_ptr, name.as_char_ptr ()) }; + +// Add fn pointers +// SAFETY: `place` is pointing to a live allocation, so the deref is safe. +let activate_fn_ptr: *mut Option <_ > =unsafe { ptr::addr_of_mut!((*place).activate) }; +// SAFETY: `activate_fn_ptr` points to a valid allocation and we have exclusive access. +unsafe { ptr::write (activate_fn_ptr,Some (trigger_activate ::<T >)) }; + +if T ::HAS_DEACTIVATE { +// SAFETY: `place` is pointing to a live allocation, so the deref is safe. +let deactivate_fn_ptr: *mut Option <_ > =unsafe { ptr::addr_of_mut!((*place).deactivate) }; +// SAFETY: `deactivate_fn_ptr` points to a valid allocation and we have exclusive access. +unsafe { ptr::write (deactivate_fn_ptr,Some (trigger_deactivate ::<T >)) }; + } + +// Add hardware trigger +// SAFETY: `place` is pointing to a live allocation, so the deref is safe. +let trigger_type_ptr =unsafe { ptr::addr_of_mut!((*place).trigger_type) }; +// SAFETY: `place` is pointing to a live allocation, so the deref is safe. +let trigger_type =unsafe {crate ::container_of!(place,Self , trigger).cast_mut () }; +// SAFETY: `trigger_type` is pointing to a live allocation of Self. +let trigger_type =unsafe { ptr::addr_of!((*trigger_type).hw_type) }; +// SAFETY: `trigger_type_ptr` points to a valid allocation and we have exclusive access. +unsafe { ptr::write (trigger_type_ptr,Opaque ::raw_get (trigger_type)) }; + +// SAFETY: ffi call, `place` is sufficently filled with data at this point +to_result (unsafe { + bindings::led_trigger_register (place) + }) + }), + _t:PhantomData , + }) + } +} + +#[pinned_drop] +impl <T >PinnedDrop for Hardware <T > { +fn drop (self:Pin <&mut Self >) { +// SAFETY: trigger is pointing to a live and registered allocation +unsafe { + bindings::led_trigger_unregister (self.trigger.get ()); + } + } +} +rust/kernel/leds/triggers.rs line 97
Quoted 10 lines
rust/kernel/leds/triggers.rs new file +10
-0+/// Operations for the Hardware trigger +#[macros::vtable] +pub trait HardwareOperations :super ::Operations { +/// Activate the hardware trigger. +fn activate (this: &mut Self ::This ) ->Result ; +/// Deactivate the hardware trigger. +fn deactivate (_this: &mut Self ::This ) { +crate ::build_error (crate ::error::VTABLE_DEFAULT_ERROR ) + } +}This looks as if you are doing a Rust binding for struct
led_trigger. But you keep calling it Hardware trigger, which makes me thing that you are confused about what is a LED trigger and what is a hardware trigger.
Quoted 1 line
Why do you keep putting "Hardware" into the names of these symbols?
The idea was to create a abstraction specific to writing a hardware trigger (or my understanding of what that is) and deal with the other trigger types later, to more separate the things on the rust side with e.g. the vtables for those.
But my understanding might be wrong.
(My broad understanding is what I did in the SE10 driver later, to tell the hardware to not present the LED to the kernel, but some other hardware wiring to a hardware thing that then drives the LED)
Quoted 4 lines
I fear that you may be confused about some stuff here. In order to determine whether this is the case, could you answer the following questions please?
That might be right, thanks for trying to clear it up if that is the case.
Quoted 1 line
- What is the purpose of
struct led_hw_trigger_type?
Marking a led that it has a private trigger that gives control of the LED to some hardware driver.
Quoted 2 lines
- What is the purpose of the
dummymember of this struct? What value should be assigned to it?
From my understanding this is just to give the struct a size, so that it has a unique address in memory so the pointer value can be compared.
Quoted 4 lines
- If a LED class device (LED cdev) has the
trigger_typemember set to NULL, which LED triggers will be listed in the sysfstriggerfile for this LED cdev? And which triggers will be listed if thetrigger_typemember is not NULL?
For null all generic triggers will be listed, and for some value all generic plus the specific trigger.
Quoted 2 lines
- Why does both
struct led_classdevandstruct led_triggerhave thetrigger_typemember?
led_classdev has it to declare that it does have a led private trigger mode, and the led_trigger has it so the activate/deactive functions can be found.
My research so far into how triggers work was mostly so that I can use the wwan module trigger on the SE10 board I have here, and therefore I did not look into how to write a generic led trigger usable on more then a specific led.
Thanks a lot for clearing up possible misunderstandings, Fiona
Quoted 35 lines
Quoted 35 lines
rust/kernel/leds/triggers.rs new file +30
-0+/// `trigger_activate` function pointer +/// +/// # Safety +/// +/// `led_cdev` must be passed by the corresponding callback in `led_trigger`. +unsafe extern "C" fn trigger_activate <T >(led_cdev: *mut bindings::led_classdev ) ->i32 +where +T :HardwareOperations , +{ +from_result (|| { +// SAFETY: By the safety requirement of this function `led_cdev` is embedded inside a `T::This<T>`. +let led =unsafe { &mut *(T ::This ::led_container_of (led_cdev.cast ())) }; +T ::activate (led)?; +Ok (0 ) + }) +} + +/// `trigger_deactivate` function pointer +/// +/// # Safety +/// +/// `led_cdev` must be passed by the corresponding callback in `led_trigger`. +unsafe extern "C" fn trigger_deactivate <T >(led_cdev: *mut bindings::led_classdev ) +where +T :HardwareOperations , +{ +// SAFETY: By the safety requirement of this function `led_cdev` is embedded inside a `T::This<T>`. +let led =unsafe { &mut *(T ::This ::led_container_of (led_cdev.cast ())) }; +T ::deactivate (led) +}Signature · 1 line
2.47.0
Re: [PATCH v2 3/5] rust: leds: Add hardware trigger support for hardware-controlled LEDs
Quoted 6 lines · Fiona Behrens
Quoted 6 lines
This looks as if you are doing a Rust binding for struct
led_trigger. But you keep calling it Hardware trigger, which makes me thing that you are confused about what is a LED trigger and what is a hardware trigger.
Quoted 7 lines
Quoted 1 line
Why do you keep putting "Hardware" into the names of these symbols?
The idea was to create a abstraction specific to writing a hardware trigger (or my understanding of what that is) and deal with the other trigger types later, to more separate the things on the rust side with e.g. the vtables for those.
But my understanding might be wrong.(My broad understanding is what I did in the SE10 driver later, to tell the hardware to not present the LED to the kernel, but some other hardware wiring to a hardware thing that then drives the LED)
Looking at patch 5, you do:
#[vtable]
#[cfg(CONFIG_LEDS_TRIGGERS)]
impl triggers::HardwareOperations for LedSE10 {
fn activate(this: &mut Self::This) -> Result {
this.data.send_cmd(LedCommand::Trigger)
}
}I think this naming "triggers::HardwareOperations" may cause confusion in the future. I think that what you implement here should be called LED Private Trigger, or something derived from that.
Using the work "hardware" may lead people to think that it means the other mechanism, wherein we offload software LED triggers to hardware (which is implemented via the hw_control_* members of
struct led_classdev).
Quoted 18 lines
Quoted 5 lines
I fear that you may be confused about some stuff here. In order to determine whether this is the case, could you answer the following questions please?
That might be right, thanks for trying to clear it up if that is the case.
Quoted 1 line
- What is the purpose of
struct led_hw_trigger_type?Marking a led that it has a private trigger that gives control of the LED to some hardware driver.
Quoted 2 lines
- What is the purpose of the
dummymember of this struct? What value should be assigned to it?From my understanding this is just to give the struct a size, so that it has a unique address in memory so the pointer value can be compared.
Quoted 4 lines
- If a LED class device (LED cdev) has the
trigger_typemember set to NULL, which LED triggers will be listed in the sysfstriggerfile for this LED cdev? And which triggers will be listed if thetrigger_typemember is not NULL?For null all generic triggers will be listed, and for some value all generic plus the specific trigger.
Quoted 8 lines
Quoted 2 lines
- Why does both
struct led_classdevandstruct led_triggerhave thetrigger_typemember?
led_classdevhas it to declare that it does have a led private trigger mode, and theled_triggerhas it so the activate/deactive functions can be found.My research so far into how triggers work was mostly so that I can use the wwan module trigger on the SE10 board I have here, and therefore I did not look into how to write a generic led trigger usable on more then a specific led.
Thanks a lot for clearing up possible misunderstandings, Fiona
OK it seems that you do indeed understand these.
PATCH v2 4/5 rust: add I/O port abstractions with resource management
In reply to Fiona BehrensThis patch introduces abstractions for working with I/O port resources, including the Resource and Region types. These abstractions facilitate interaction with ioport_resource, enabling safe management of resource reservations and memory accesses.
Additionally, helper functions such as outb, outw, and outl have been provided to write values to these resources, with matching read functions such as inb, inw, and inl for accessing the port memory.
--- rust/helpers/helpers.c | 1 + rust/helpers/ioport.c | 42 ++++++++++ rust/kernel/ioport.rs | 169 +++++++++++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 2 + 4 files changed, 214 insertions(+) create mode 100644 rust/helpers/ioport.c create mode 100644 rust/kernel/ioport.rs
rust/helpers/helpers.c +1 -0
@@ -14,6 +14,7 @@#include "cred.c" #include "err.c" #include "fs.c" +#include "ioport.c" #include "jump_label.c" #include "kunit.c" #include "mutex.c"
rust/helpers/ioport.c new file +42 -0
42 lines not on this page · raw message
rust/kernel/ioport.rs new file +169 -0
169 lines not on this page · raw message
rust/kernel/lib.rs +2 -0
@@ -43,6 +43,8 @@pub mod fs;pub mod init;pub mod ioctl; +#[cfg( CONFIG_HAS_IOPORT )] +pub mod ioport;pub mod jump_label;#[cfg( CONFIG_KUNIT )] pub mod kunit;
Signature · 1 line
2.47.0
Daniel Almeida
reviewerRe: [PATCH v2 4/5] rust: add I/O port abstractions with resource management
Hi Fiona,
Quoted 105 lines · Fiona Behrens
This patch introduces abstractions for working with I/O port resources, including the
ResourceandRegiontypes. These abstractions facilitate interaction withioport_resource, enabling safe management of resource reservations and memory accesses.Additionally, helper functions such as
outb,outw, andoutlhave been provided to write values to these resources, with matching read functions such asinb,inw, andinlfor accessing the port memory.Signed-off-by Fiona Behrens <[email protected]>---
rust/helpers/helpers.c | 1 + rust/helpers/ioport.c | 42 ++++++++++ rust/kernel/ioport.rs | 169 +++++++++++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 2 + 4 files changed, 214 insertions(+)create mode 100644
rust/helpers/ioport.c
create mode 100644rust/kernel/ioport.rsrust/helpers/helpers.c +1
-0@@ -14,6 +14,7 @@#include "cred.c" #include "err.c" #include "fs.c" +#include "ioport.c" #include "jump_label.c" #include "kunit.c" #include "mutex.c" rust/helpers/ioport.c new file +42
-0@@ -0,0 +1,42 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/ioport.h> +#include <linux/io.h> + +struct resource *rust_helper_request_region (resource_size_t start, +resource_size_t n,const char *name) +{ +return request_region (start, n, name); +} + +struct resource *rust_helper_request_muxed_region (resource_size_t start, +resource_size_t n, +const char *name) +{ +return request_muxed_region (start, n, name); +} + +void rust_helper_release_region (resource_size_t start,resource_size_t n) +{ +release_region (start, n); +} + +#define define_rust_helper_out (name, type) \ + void rust_helper_##name(type value, unsigned long addr) \ + { \ + (name)(value, addr); \ + } + +define_rust_helper_out (outb, u8); +define_rust_helper_out (outw, u16); +define_rust_helper_out (outl, u32); + +#define define_rust_helper_in (name, type) \ + type rust_helper_##name(unsigned long addr) \ + { \ + return (name)(addr); \ + } + +define_rust_helper_in (inb, u8); +define_rust_helper_in (inw, u16); +define_rust_helper_in (inl, u32);rust/kernel/ioport.rs new file +18
-0@@ -0,0 +1,169 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Abstractions of routines for detecting, reserving and +//! allocating system resources. +//! +//! C header: [`include/linux/ioport.h`](srctree/include/linux/ioport.h) + +use core::ops::Deref ; +use core::ptr; + +use crate ::prelude::*; +use crate ::types::Opaque ; + +/// Resource Size type. +/// This is a type alias to `u64` +/// depending on the config option `CONFIG_PHYS_ADDR_T_64BIT`. +#[cfg( CONFIG_PHYS_ADDR_T_64BIT )] +pub type ResourceSize =u64 ;
rust/kernel/ioport.rs line 28
Quoted 10 lines
rust/kernel/ioport.rs new file +10
-0+ +/// Resource Size type. +/// This is a type alias to `u32` +/// depending on the config option `CONFIG_PHYS_ADDR_T_64BIT`. +#[cfg(not( CONFIG_PHYS_ADDR_T_64BIT ))] +pub type ResourceSize =u32 ; + +/// Resource node. +#[repr(transparent)] +pub struct Resource (Opaque <bindings::resource >);
This is a conflict with [0]
[0]: https://lore.kernel.org/rust-for-linux/[email protected]/
— Daniel
Re: [PATCH v2 4/5] rust: add I/O port abstractions with resource management
Hi,
Quoted 111 lines · Daniel Almeida
Hi Fiona,
Quoted 109 lines · Fiona Behrens
This patch introduces abstractions for working with I/O port resources, including the
ResourceandRegiontypes. These abstractions facilitate interaction withioport_resource, enabling safe management of resource reservations and memory accesses.Additionally, helper functions such as
outb,outw, andoutlhave been provided to write values to these resources, with matching read functions such asinb,inw, andinlfor accessing the port memory.Signed-off-by Fiona Behrens <[email protected]>---
rust/helpers/helpers.c | 1 + rust/helpers/ioport.c | 42 ++++++++++ rust/kernel/ioport.rs | 169 +++++++++++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 2 + 4 files changed, 214 insertions(+)create mode 100644
rust/helpers/ioport.c
create mode 100644rust/kernel/ioport.rsrust/helpers/helpers.c +1
-0@@ -14,6 +14,7 @@#include "cred.c" #include "err.c" #include "fs.c" +#include "ioport.c" #include "jump_label.c" #include "kunit.c" #include "mutex.c" rust/helpers/ioport.c new file +42
-0@@ -0,0 +1,42 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/ioport.h> +#include <linux/io.h> + +struct resource *rust_helper_request_region (resource_size_t start, +resource_size_t n,const char *name) +{ +return request_region (start, n, name); +} + +struct resource *rust_helper_request_muxed_region (resource_size_t start, +resource_size_t n, +const char *name) +{ +return request_muxed_region (start, n, name); +} + +void rust_helper_release_region (resource_size_t start,resource_size_t n) +{ +release_region (start, n); +} + +#define define_rust_helper_out (name, type) \ + void rust_helper_##name(type value, unsigned long addr) \ + { \ + (name)(value, addr); \ + } + +define_rust_helper_out (outb, u8); +define_rust_helper_out (outw, u16); +define_rust_helper_out (outl, u32); + +#define define_rust_helper_in (name, type) \ + type rust_helper_##name(unsigned long addr) \ + { \ + return (name)(addr); \ + } + +define_rust_helper_in (inb, u8); +define_rust_helper_in (inw, u16); +define_rust_helper_in (inl, u32);rust/kernel/ioport.rs new file +22
-0@@ -0,0 +1,169 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Abstractions of routines for detecting, reserving and +//! allocating system resources. +//! +//! C header: [`include/linux/ioport.h`](srctree/include/linux/ioport.h) + +use core::ops::Deref ; +use core::ptr; + +use crate ::prelude::*; +use crate ::types::Opaque ; + +/// Resource Size type. +/// This is a type alias to `u64` +/// depending on the config option `CONFIG_PHYS_ADDR_T_64BIT`. +#[cfg( CONFIG_PHYS_ADDR_T_64BIT )] +pub type ResourceSize =u64 ; + +/// Resource Size type. +/// This is a type alias to `u32` +/// depending on the config option `CONFIG_PHYS_ADDR_T_64BIT`.
Quoted 8 lines
rust/kernel/ioport.rs line 28
Quoted 6 lines
rust/kernel/ioport.rs new file +6
-0+#[cfg(not( CONFIG_PHYS_ADDR_T_64BIT ))] +pub type ResourceSize =u32 ; + +/// Resource node. +#[repr(transparent)] +pub struct Resource (Opaque <bindings::resource >);This is a conflict with [0]
Ah missed that, only saw the IO abstraction in the same series but it did not work for my use case, and then somehow missed this, will rework to use that as base in the next version
Thanks
Fiona
Quoted 4 lines
[0]: https://lore.kernel.org/rust-for-linux/[email protected]/
— Daniel
PATCH v2 5/5 leds: leds_lenovo_se10: LED driver for Lenovo SE10 platform
In reply to Fiona BehrensAdd driver for the Lenovo ThinkEdge SE10 LED.
This driver supports controlling the red LED located on the front panel of the Lenovo SE10 hardware. Additionally, it supports the hardware-triggered functionality of the LED, which by default is tied to the WWAN trigger.
The driver is written in Rust and adds basic LED support for the SE10 platform.
--- drivers/leds/Kconfig | 10 +++ drivers/leds/Makefile | 1 + drivers/leds/leds_lenovo_se10.rs | 132 +++++++++++++++++++++++++++++++ 3 files changed, 143 insertions(+) create mode 100644 drivers/leds/leds_lenovo_se10.rs
drivers/leds/Kconfig +10 -0
@@ -223,6 +223,16 @@ config LEDS_TURRIS_OMNIA side of CZ.NIC's Turris Omnia router. There are 12 RGB LEDs on the front panel. +config LEDS_LENOVO_SE10 + tristate "LED support for Lenovo ThinkEdge SE10" + depends on RUST + depends on (X86 && DMI) || COMPILE_TEST + depends on HAS_IOPORT + imply LEDS_TRIGGERS + help + This option enables basic support for the LED found on the front of + Lenovo's SE10 ThinkEdge. There is one user controlable LED on the front panel. + config LEDS_LM3530 tristate "LCD Backlight driver for LM3530" depends on LEDS_CLASS
drivers/leds/Makefile +1 -0
@@ -37,6 +37,7 @@ obj-$(CONFIG_LEDS_IP30) += leds-ip30.o obj-$(CONFIG_LEDS_IPAQ_MICRO) += leds-ipaq-micro.o obj-$(CONFIG_LEDS_IS31FL319X) += leds-is31fl319x.o obj-$(CONFIG_LEDS_IS31FL32XX) += leds-is31fl32xx.o +obj-$(CONFIG_LEDS_LENOVO_SE10) += leds_lenovo_se10.o obj-$(CONFIG_LEDS_LM3530) += leds-lm3530.o obj-$(CONFIG_LEDS_LM3532) += leds-lm3532.o obj-$(CONFIG_LEDS_LM3533) += leds-lm3533.o
drivers/leds/leds_lenovo_se10.rs new file +132 -0
132 lines not on this page · raw message
Signature · 1 line
2.47.0
Re: [PATCH v2 5/5] leds: leds_lenovo_se10: LED driver for Lenovo SE10 platform
Hi Fiona,
Quoted 6 lines · Fiona Behrens
Add driver for the Lenovo ThinkEdge SE10 LED.
This driver supports controlling the red LED located on the front panel of the Lenovo SE10 hardware. Additionally, it supports the hardware-triggered functionality of the LED, which by default is tied to the WWAN trigger.
Quoted 7 lines
The driver is written in Rust and adds basic LED support for the SE10 platform.
Signed-off-by Fiona Behrens <[email protected]>--- drivers/leds/Kconfig | 10 +++ drivers/leds/Makefile | 1 + drivers/leds/leds_lenovo_se10.rs | 132 +++++++++++++++++++++++++++++++
All the other files are called leds-<name>. Should this be leds-lenovo-se10.rs?
Quoted 30 lines
3 files changed, 143 insertions(+) create mode 100644 drivers/leds/leds_lenovo_se10.rsdrivers/leds/Kconfig +9
-0@@ -223,6 +223,16 @@ config LEDS_TURRIS_OMNIA side of CZ.NIC's Turris Omnia router. There are 12 RGB LEDs on the front panel. +config LEDS_LENOVO_SE10 + tristate "LED support for Lenovo ThinkEdge SE10" + depends on RUST + depends on (X86 && DMI) || COMPILE_TEST + depends on HAS_IOPORT + imply LEDS_TRIGGERS + help + This option enables basic support for the LED found on the front of + Lenovo's SE10 ThinkEdge. There is one user controlable LED on thefront panel.
drivers/leds/Kconfig +1
-0+ config LEDS_LM3530 tristate "LCD Backlight driver for LM3530" depends on LEDS_CLASSdrivers/leds/Makefile +0
-0
drivers/leds/Makefile obj-$(CONFIG_LEDS_IP30) += leds-ip30.o · line 40
Quoted 4 lines
drivers/leds/Makefile +1
-0@@ -37,6 +37,7 @@ obj-$(CONFIG_LEDS_IP30) += leds-ip30.o obj-$(CONFIG_LEDS_IPAQ_MICRO) += leds-ipaq-micro.o obj-$(CONFIG_LEDS_IS31FL319X) += leds-is31fl319x.o obj-$(CONFIG_LEDS_IS31FL32XX) += leds-is31fl32xx.o +obj-$(CONFIG_LEDS_LENOVO_SE10) += leds_lenovo_se10.o
Same note above on name formatting.
Quoted 46 lines
drivers/leds/Makefile +0
-0obj-$(CONFIG_LEDS_LM3530) += leds-lm3530.o obj-$(CONFIG_LEDS_LM3532) += leds-lm3532.o obj-$(CONFIG_LEDS_LM3533) += leds-lm3533.odrivers/leds/leds_lenovo_se10.rs new file +36
-0@@ -0,0 +1,132 @@ +// SPDX-License-Identifier: GPL-2.0 +//! LED driver for Lenovo ThinkEdge SE10. + +use kernel::ioport::{Region ,ResourceSize }; +#[cfg( CONFIG_LEDS_TRIGGERS )] +use kernel::leds::triggers; +use kernel::leds::{Led ,LedConfig ,Operations }; +use kernel::prelude::*; +use kernel::time::Delta ; +use kernel::{c_str, dmi_device_table}; + +module! { +type :SE10 , + name:"leds_lenovo_se10" , + author:"Fiona Behrens <[email protected]>" , + description:"LED driver for Lenovo ThinkEdge SE10" , + license:"GPL" , +} + +dmi_device_table! (5 ,SE10_DMI_TABLE , [ +"LENOVO-SE10" : [SysVendor :"LENOVO" ,ProductName :"12NH" ], +"LENOVO-SE10" : [SysVendor :"LENOVO" ,ProductName :"12NJ" ], +"LENOVO-SE10" : [SysVendor :"LENOVO" ,ProductName :"12NK" ], +"LENOVO-SE10" : [SysVendor :"LENOVO" ,ProductName :"12NL" ], +"LENOVO-SE10" : [SysVendor :"LENOVO" ,ProductName :"12NM" ], +]); + +struct SE10 { +/// Led registration + _led:Pin <KBox <Led <LedSE10 >>>, +} + +impl kernel::Module for SE10 { +fn init (_module: &'staticThisModule ) ->Result <Self > { +if SE10_DMI_TABLE .check_system ().is_none () { +return Err (ENODEV );
drivers/leds/leds_lenovo_se10.rs line 46
Quoted 10 lines
drivers/leds/leds_lenovo_se10.rs new file +10
-0+ } + + let led = KBox::try_pin_init( + Led::register( + None, + LedConfig { + name: Some(c_str!("platform:red:user")), + #[cfg(CONFIG_LEDS_TRIGGERS)] + hardware_trigger: Some(kernel::sync::Arc::pin_init( + triggers::Hardware::register(c_str!("wwan")),
I was curious as to why the "wwan" in here.
Quoted 90 lines
drivers/leds/leds_lenovo_se10.rs new file +36
-0+ GFP_KERNEL, + )?), + ..LedConfig ::new (kernel::leds::Color ::Red ,LedSE10 ) + }, + ), + GFP_KERNEL, + )?; + +Ok (Self { _led: led }) + } +} + +/// Valid led commands. +#[repr( u8 )] +#[allow(missing_docs)] +enum LedCommand { +#[cfg( CONFIG_LEDS_TRIGGERS )] +Trigger =0xB2 , +Off =0xB3 , +On =0xB4 , +Blink =0xB5 , +} + +struct LedSE10 ; + +impl LedSE10 { +/// Base address of the command port. +const CMD_PORT :ResourceSize =0x6C ; +/// Length of the command port. +const CMD_LEN :ResourceSize =1 ; +/// Blink duration the hardware supports. +const HW_DURATION :Delta =Delta ::from_millis (1000 ); + +/// Request led region. +fn request_cmd_region (&self) ->Result <Region <'static>> { + Region::request_muxed(Self::CMD_PORT, Self::CMD_LEN,c_str!("leds_lenovo_se10"))
drivers/leds/leds_lenovo_se10.rs new file +34
-0+ .ok_or(EBUSY) + } + + /// Send command. + fn send_cmd(&self, cmd: LedCommand) -> Result { + let region = self.request_cmd_region()?; + region.outb(cmd as u8, 0); + Ok(()) + } +} + +#[vtable] +impl Operations for LedSE10 { + type This = Led<LedSE10>; + + const MAX_BRIGHTNESS: u8 = 1; + + fn brightness_set(this: &mut Self::This, brightness: u8) { + if let Err(e) = if brightness == 0 { + this.data.send_cmd(LedCommand::Off) + } else { + this.data.send_cmd(LedCommand::On) + } { + pr_warn!("Failed to set led: {e:?}\n)") + } + } + + fn blink_set( + this: &mut Self::This, + delay_on: Delta, + delay_off: Delta, + ) -> Result<(Delta, Delta)> { + if !(delay_on.is_zero() && delay_off.is_zero() + || delay_on == Self::HW_DURATION && delay_off ==
Self::HW_DURATION)drivers/leds/leds_lenovo_se10.rs new file +16
-0+ { +return Err (EINVAL ); + } + + this.data.send_cmd (LedCommand ::Blink )?; +Ok ((Self ::HW_DURATION ,Self ::HW_DURATION )) + } +} + +#[vtable] +#[cfg( CONFIG_LEDS_TRIGGERS )] +impl triggers::HardwareOperations for LedSE10 { +fn activate (this: &mut Self ::This ) ->Result { + this.data.send_cmd (LedCommand ::Trigger ) + } +}Signature · 1 line
2.47.0
I don't have the competence to review the rust code I'm afraid - so my limited feedback above is the best I can do. Not sure it's really worth a reviewed-by tag, but I did read the code and learnt a little about rust in the process (which was fun).
I did test your changes on my SE10 system and it works well.
Thanks!
Mark
Re: [PATCH v2 5/5] leds: leds_lenovo_se10: LED driver for Lenovo SE10 platform
Hi,
Quoted 11 lines · Mark Pearson
Hi Fiona,
Quoted 8 lines · Fiona Behrens
Add driver for the Lenovo ThinkEdge SE10 LED.
This driver supports controlling the red LED located on the front panel of the Lenovo SE10 hardware. Additionally, it supports the hardware-triggered functionality of the LED, which by default is tied to the WWAN trigger.
The driver is written in Rust and adds basic LED support for the SE10 platform.
Quoted 7 lines
Quoted 5 lines
Signed-off-by Fiona Behrens <[email protected]>--- drivers/leds/Kconfig | 10 +++ drivers/leds/Makefile | 1 + drivers/leds/leds_lenovo_se10.rs | 132 +++++++++++++++++++++++++++++++All the other files are called leds-<name>. Should this be
leds-lenovo-se10.rs?
This does not work with rust, as the rust makefile converts this filename to a rust crate name, and those crate name cannot have dashes in them. Not sure if we should fix this to hold to the file name conventions, maybe something for @Miguel to decide
Quoted 88 lines
Quoted 31 lines
3 files changed, 143 insertions(+) create mode 100644 drivers/leds/leds_lenovo_se10.rsdrivers/leds/Kconfig +9
-0@@ -223,6 +223,16 @@ config LEDS_TURRIS_OMNIA side of CZ.NIC's Turris Omnia router. There are 12 RGB LEDs on the front panel. +config LEDS_LENOVO_SE10 + tristate "LED support for Lenovo ThinkEdge SE10" + depends on RUST + depends on (X86 && DMI) || COMPILE_TEST + depends on HAS_IOPORT + imply LEDS_TRIGGERS + help + This option enables basic support for the LED found on the front of + Lenovo's SE10 ThinkEdge. There is one user controlable LED on thefront panel.
drivers/leds/Kconfig +1
-0+ config LEDS_LM3530 tristate "LCD Backlight driver for LM3530" depends on LEDS_CLASSdrivers/leds/Makefile +0
-0drivers/leds/Makefile obj-$(CONFIG_LEDS_IP30) += leds-ip30.o · line 40
Quoted 4 lines
drivers/leds/Makefile +1
-0@@ -37,6 +37,7 @@ obj-$(CONFIG_LEDS_IP30) += leds-ip30.o obj-$(CONFIG_LEDS_IPAQ_MICRO) += leds-ipaq-micro.o obj-$(CONFIG_LEDS_IS31FL319X) += leds-is31fl319x.o obj-$(CONFIG_LEDS_IS31FL32XX) += leds-is31fl32xx.o +obj-$(CONFIG_LEDS_LENOVO_SE10) += leds_lenovo_se10.oSame note above on name formatting.
Quoted 50 lines
drivers/leds/Makefile +0
-0obj-$(CONFIG_LEDS_LM3530) += leds-lm3530.o obj-$(CONFIG_LEDS_LM3532) += leds-lm3532.o obj-$(CONFIG_LEDS_LM3533) += leds-lm3533.odrivers/leds/leds_lenovo_se10.rs new file +40
-0@@ -0,0 +1,132 @@ +// SPDX-License-Identifier: GPL-2.0 +//! LED driver for Lenovo ThinkEdge SE10. + +use kernel::ioport::{Region, ResourceSize}; +#[cfg(CONFIG_LEDS_TRIGGERS)] +use kernel::leds::triggers; +use kernel::leds::{Led, LedConfig, Operations}; +use kernel::prelude::*; +use kernel::time::Delta; +use kernel::{c_str, dmi_device_table}; + +module! { + type: SE10, + name: "leds_lenovo_se10", + author: "Fiona Behrens <[email protected]>", + description: "LED driver for Lenovo ThinkEdge SE10", + license: "GPL", +} + +dmi_device_table!(5, SE10_DMI_TABLE, [ + "LENOVO-SE10": [SysVendor: "LENOVO", ProductName: "12NH"], + "LENOVO-SE10": [SysVendor: "LENOVO", ProductName: "12NJ"], + "LENOVO-SE10": [SysVendor: "LENOVO", ProductName: "12NK"], + "LENOVO-SE10": [SysVendor: "LENOVO", ProductName: "12NL"], + "LENOVO-SE10": [SysVendor: "LENOVO", ProductName: "12NM"], +]); + +struct SE10 { + /// Led registration + _led: Pin<KBox<Led<LedSE10>>>, +} + +impl kernel::Module for SE10 { + fn init(_module: &'static ThisModule) -> Result<Self> { + if SE10_DMI_TABLE.check_system().is_none() { + return Err(ENODEV); + } + + let led = KBox::try_pin_init( + Led::register(
Quoted 8 lines
drivers/leds/leds_lenovo_se10.rs line 46
Quoted 6 lines
drivers/leds/leds_lenovo_se10.rs new file +6
-0+ None, + LedConfig { + name: Some(c_str!("platform:red:user")), + #[cfg(CONFIG_LEDS_TRIGGERS)] + hardware_trigger: Some(kernel::sync::Arc::pin_init( + triggers::Hardware::register(c_str!("wwan")),I was curious as to why the "wwan" in here.
This is the hardware trigger, as to the documentation I found from Lenovo the trigger mode gives hardware control to the wwan module if installed in the hardware.
Quoted 94 lines
Quoted 91 lines
drivers/leds/leds_lenovo_se10.rs new file +36
-0+ GFP_KERNEL, + )?), + ..LedConfig ::new (kernel::leds::Color ::Red ,LedSE10 ) + }, + ), + GFP_KERNEL, + )?; + +Ok (Self { _led: led }) + } +} + +/// Valid led commands. +#[repr( u8 )] +#[allow(missing_docs)] +enum LedCommand { +#[cfg( CONFIG_LEDS_TRIGGERS )] +Trigger =0xB2 , +Off =0xB3 , +On =0xB4 , +Blink =0xB5 , +} + +struct LedSE10 ; + +impl LedSE10 { +/// Base address of the command port. +const CMD_PORT :ResourceSize =0x6C ; +/// Length of the command port. +const CMD_LEN :ResourceSize =1 ; +/// Blink duration the hardware supports. +const HW_DURATION :Delta =Delta ::from_millis (1000 ); + +/// Request led region. +fn request_cmd_region (&self) ->Result <Region <'static>> { + Region::request_muxed(Self::CMD_PORT, Self::CMD_LEN,c_str!("leds_lenovo_se10"))
drivers/leds/leds_lenovo_se10.rs new file +34
-0+ .ok_or(EBUSY) + } + + /// Send command. + fn send_cmd(&self, cmd: LedCommand) -> Result { + let region = self.request_cmd_region()?; + region.outb(cmd as u8, 0); + Ok(()) + } +} + +#[vtable] +impl Operations for LedSE10 { + type This = Led<LedSE10>; + + const MAX_BRIGHTNESS: u8 = 1; + + fn brightness_set(this: &mut Self::This, brightness: u8) { + if let Err(e) = if brightness == 0 { + this.data.send_cmd(LedCommand::Off) + } else { + this.data.send_cmd(LedCommand::On) + } { + pr_warn!("Failed to set led: {e:?}\n)") + } + } + + fn blink_set( + this: &mut Self::This, + delay_on: Delta, + delay_off: Delta, + ) -> Result<(Delta, Delta)> { + if !(delay_on.is_zero() && delay_off.is_zero() + || delay_on == Self::HW_DURATION && delay_off ==
Self::HW_DURATION)drivers/leds/leds_lenovo_se10.rs new file +16
-0+ { +return Err (EINVAL ); + } + + this.data.send_cmd (LedCommand ::Blink )?; +Ok ((Self ::HW_DURATION ,Self ::HW_DURATION )) + } +} + +#[vtable] +#[cfg( CONFIG_LEDS_TRIGGERS )] +impl triggers::HardwareOperations for LedSE10 { +fn activate (this: &mut Self ::This ) ->Result { + this.data.send_cmd (LedCommand ::Trigger ) + } +}Signature · 1 line
2.47.0I don't have the competence to review the rust code I'm afraid - so my limited feedback above is the best I can do. Not sure it's really worth a reviewed-by tag, but I did read the code and learnt a little about rust in the process (which was fun).
Quoted 2 lines
I did test your changes on my SE10 system and it works well.
Tested-by Mark Pearson <[email protected]>
Thanks a lot,
Fiona
Quoted 3 lines
Thanks!
Mark
Re: [PATCH v2 5/5] leds: leds_lenovo_se10: LED driver for Lenovo SE10 platform
Hi
Quoted 21 lines · Fiona Behrens
Hi,
Quoted 17 lines · Mark Pearson
Hi Fiona,
Quoted 13 lines · Fiona Behrens
Add driver for the Lenovo ThinkEdge SE10 LED.
This driver supports controlling the red LED located on the front panel of the Lenovo SE10 hardware. Additionally, it supports the hardware-triggered functionality of the LED, which by default is tied to the WWAN trigger.
The driver is written in Rust and adds basic LED support for the SE10 platform.
Signed-off-by Fiona Behrens <[email protected]>--- drivers/leds/Kconfig | 10 +++ drivers/leds/Makefile | 1 + drivers/leds/leds_lenovo_se10.rs | 132 +++++++++++++++++++++++++++++++
Quoted 7 lines
Quoted 1 line
All the other files are called leds-<name>. Should this be
leds-lenovo-se10.rs?This does not work with rust, as the rust makefile converts this filename to a rust crate name, and those crate name cannot have dashes in them.
Not sure if we should fix this to hold to the file name conventions, maybe something for @Miguel to decide
Ah - thanks for the clarification (and to Miguel in the follow up)
Quoted 98 lines
Quoted 89 lines
Quoted 32 lines
3 files changed, 143 insertions(+) create mode 100644 drivers/leds/leds_lenovo_se10.rsdrivers/leds/Kconfig +9
-0@@ -223,6 +223,16 @@ config LEDS_TURRIS_OMNIA side of CZ.NIC's Turris Omnia router. There are 12 RGB LEDs on the front panel. +config LEDS_LENOVO_SE10 + tristate "LED support for Lenovo ThinkEdge SE10" + depends on RUST + depends on (X86 && DMI) || COMPILE_TEST + depends on HAS_IOPORT + imply LEDS_TRIGGERS + help + This option enables basic support for the LED found on the front of + Lenovo's SE10 ThinkEdge. There is one user controlable LED on thefront panel.
drivers/leds/Kconfig +1
-0+ config LEDS_LM3530 tristate "LCD Backlight driver for LM3530" depends on LEDS_CLASSdrivers/leds/Makefile +0
-0drivers/leds/Makefile obj-$(CONFIG_LEDS_IP30) += leds-ip30.o · line 40
Quoted 4 lines
drivers/leds/Makefile +1
-0@@ -37,6 +37,7 @@ obj-$(CONFIG_LEDS_IP30) += leds-ip30.o obj-$(CONFIG_LEDS_IPAQ_MICRO) += leds-ipaq-micro.o obj-$(CONFIG_LEDS_IS31FL319X) += leds-is31fl319x.o obj-$(CONFIG_LEDS_IS31FL32XX) += leds-is31fl32xx.o +obj-$(CONFIG_LEDS_LENOVO_SE10) += leds_lenovo_se10.oSame note above on name formatting.
Quoted 50 lines
drivers/leds/Makefile +0
-0obj-$(CONFIG_LEDS_LM3530) += leds-lm3530.o obj-$(CONFIG_LEDS_LM3532) += leds-lm3532.o obj-$(CONFIG_LEDS_LM3533) += leds-lm3533.odrivers/leds/leds_lenovo_se10.rs new file +40
-0@@ -0,0 +1,132 @@ +// SPDX-License-Identifier: GPL-2.0 +//! LED driver for Lenovo ThinkEdge SE10. + +use kernel::ioport::{Region, ResourceSize}; +#[cfg(CONFIG_LEDS_TRIGGERS)] +use kernel::leds::triggers; +use kernel::leds::{Led, LedConfig, Operations}; +use kernel::prelude::*; +use kernel::time::Delta; +use kernel::{c_str, dmi_device_table}; + +module! { + type: SE10, + name: "leds_lenovo_se10", + author: "Fiona Behrens <[email protected]>", + description: "LED driver for Lenovo ThinkEdge SE10", + license: "GPL", +} + +dmi_device_table!(5, SE10_DMI_TABLE, [ + "LENOVO-SE10": [SysVendor: "LENOVO", ProductName: "12NH"], + "LENOVO-SE10": [SysVendor: "LENOVO", ProductName: "12NJ"], + "LENOVO-SE10": [SysVendor: "LENOVO", ProductName: "12NK"], + "LENOVO-SE10": [SysVendor: "LENOVO", ProductName: "12NL"], + "LENOVO-SE10": [SysVendor: "LENOVO", ProductName: "12NM"], +]); + +struct SE10 { + /// Led registration + _led: Pin<KBox<Led<LedSE10>>>, +} + +impl kernel::Module for SE10 { + fn init(_module: &'static ThisModule) -> Result<Self> { + if SE10_DMI_TABLE.check_system().is_none() { + return Err(ENODEV); + } + + let led = KBox::try_pin_init( + Led::register(Quoted 8 lines
drivers/leds/leds_lenovo_se10.rs line 46
Quoted 6 lines
drivers/leds/leds_lenovo_se10.rs new file +6
-0+ None, + LedConfig { + name: Some(c_str!("platform:red:user")), + #[cfg(CONFIG_LEDS_TRIGGERS)] + hardware_trigger: Some(kernel::sync::Arc::pin_init( + triggers::Hardware::register(c_str!("wwan")),I was curious as to why the "wwan" in here.
Quoted 4 lines
This is the hardware trigger, as to the documentation I found from Lenovo the trigger mode gives hardware control to the wwan module if installed in the hardware.
Ah - I should probably have known that :) All good.
Mark
Re: [PATCH v2 5/5] leds: leds_lenovo_se10: LED driver for Lenovo SE10 platform
In reply to Mark PearsonQuoted 2 lines · Mark Pearson
All the other files are called leds-<name>. Should this be
leds-lenovo-se10.rs?
Rust requires underscores for the crate name, so we started with just using the same name for both since it is simpler and to avoid ending up with the usual mix of dashes/underscores in the future.
But we can allow them if subsystems think it is important to maintain consistency within their folder etc.
Cheers,
Miguel
Re: [PATCH v2 5/5] leds: leds_lenovo_se10: LED driver for Lenovo SE10 platform
In reply to Fiona BehrensQuoted 91 lines · Fiona Behrens
Add driver for the Lenovo ThinkEdge SE10 LED.
This driver supports controlling the red LED located on the front panel of the Lenovo SE10 hardware. Additionally, it supports the hardware-triggered functionality of the LED, which by default is tied to the WWAN trigger.
The driver is written in Rust and adds basic LED support for the SE10 platform.
Signed-off-by Fiona Behrens <[email protected]>--- drivers/leds/Kconfig | 10 +++ drivers/leds/Makefile | 1 + drivers/leds/leds_lenovo_se10.rs | 132 +++++++++++++++++++++++++++++++ 3 files changed, 143 insertions(+) create mode 100644 drivers/leds/leds_lenovo_se10.rsdrivers/leds/Kconfig +10
-0@@ -223,6 +223,16 @@ config LEDS_TURRIS_OMNIA side of CZ.NIC's Turris Omnia router. There are 12 RGB LEDs on the front panel. +config LEDS_LENOVO_SE10 + tristate "LED support for Lenovo ThinkEdge SE10" + depends on RUST + depends on (X86 && DMI) || COMPILE_TEST + depends on HAS_IOPORT + imply LEDS_TRIGGERS + help + This option enables basic support for the LED found on the front of + Lenovo's SE10 ThinkEdge. There is one user controlable LED on the front panel. + config LEDS_LM3530 tristate "LCD Backlight driver for LM3530" depends on LEDS_CLASSdrivers/leds/Makefile +1
-0@@ -37,6 +37,7 @@ obj-$(CONFIG_LEDS_IP30) += leds-ip30.o obj-$(CONFIG_LEDS_IPAQ_MICRO) += leds-ipaq-micro.o obj-$(CONFIG_LEDS_IS31FL319X) += leds-is31fl319x.o obj-$(CONFIG_LEDS_IS31FL32XX) += leds-is31fl32xx.o +obj-$(CONFIG_LEDS_LENOVO_SE10) += leds_lenovo_se10.o obj-$(CONFIG_LEDS_LM3530) += leds-lm3530.o obj-$(CONFIG_LEDS_LM3532) += leds-lm3532.o obj-$(CONFIG_LEDS_LM3533) += leds-lm3533.odrivers/leds/leds_lenovo_se10.rs new file +36
-0@@ -0,0 +1,132 @@ +// SPDX-License-Identifier: GPL-2.0 +//! LED driver for Lenovo ThinkEdge SE10. + +use kernel::ioport::{Region ,ResourceSize }; +#[cfg( CONFIG_LEDS_TRIGGERS )] +use kernel::leds::triggers; +use kernel::leds::{Led ,LedConfig ,Operations }; +use kernel::prelude::*; +use kernel::time::Delta ; +use kernel::{c_str, dmi_device_table}; + +module! { +type :SE10 , + name:"leds_lenovo_se10" , + author:"Fiona Behrens <[email protected]>" , + description:"LED driver for Lenovo ThinkEdge SE10" , + license:"GPL" , +} + +dmi_device_table! (5 ,SE10_DMI_TABLE , [ +"LENOVO-SE10" : [SysVendor :"LENOVO" ,ProductName :"12NH" ], +"LENOVO-SE10" : [SysVendor :"LENOVO" ,ProductName :"12NJ" ], +"LENOVO-SE10" : [SysVendor :"LENOVO" ,ProductName :"12NK" ], +"LENOVO-SE10" : [SysVendor :"LENOVO" ,ProductName :"12NL" ], +"LENOVO-SE10" : [SysVendor :"LENOVO" ,ProductName :"12NM" ], +]); + +struct SE10 { +/// Led registration + _led:Pin <KBox <Led <LedSE10 >>>, +} + +impl kernel::Module for SE10 { +fn init (_module: &'staticThisModule ) ->Result <Self > { +if SE10_DMI_TABLE .check_system ().is_none () { +return Err (ENODEV );
drivers/leds/leds_lenovo_se10.rs line 46
Quoted 10 lines
drivers/leds/leds_lenovo_se10.rs new file +10
-0+ } + + let led = KBox::try_pin_init( + Led::register( + None, + LedConfig { + name: Some(c_str!("platform:red:user")), + #[cfg(CONFIG_LEDS_TRIGGERS)] + hardware_trigger: Some(kernel::sync::Arc::pin_init( + triggers::Hardware::register(c_str!("wwan")),
There are currently two LED drivers utilizing the led_hw_trigger_type mechanism to make certain triggers available only for certain LEDs:
- the leds-cros_ec.c driver, which registers the trigger under the name "chromeos-auto", to suggest that activating the trigger on this LED will make it blink automatically by hardware and that it is ChromeOS specific,
- the leds-turris-omnia.c driver, which registers the trigger under the name "omnia-mcu", to suggest that activating the trigger will make the LED blinking be controller by the MCU on Turris Omnia.
Using the name "wwan" for this trigger is too general. In the future someone may want to create a software "wwan" trigger that will be available for any LED class device, for example...
Please change the name of this LED-private trigger.
Quoted 75 lines
drivers/leds/leds_lenovo_se10.rs new file +75
-0+ GFP_KERNEL, + )?), + ..LedConfig ::new (kernel::leds::Color ::Red ,LedSE10 ) + }, + ), + GFP_KERNEL, + )?; + +Ok (Self { _led: led }) + } +} + +/// Valid led commands. +#[repr( u8 )] +#[allow(missing_docs)] +enum LedCommand { +#[cfg( CONFIG_LEDS_TRIGGERS )] +Trigger =0xB2 , +Off =0xB3 , +On =0xB4 , +Blink =0xB5 , +} + +struct LedSE10 ; + +impl LedSE10 { +/// Base address of the command port. +const CMD_PORT :ResourceSize =0x6C ; +/// Length of the command port. +const CMD_LEN :ResourceSize =1 ; +/// Blink duration the hardware supports. +const HW_DURATION :Delta =Delta ::from_millis (1000 ); + +/// Request led region. +fn request_cmd_region (&self) ->Result <Region <'static>> { +Region ::request_muxed (Self ::CMD_PORT ,Self ::CMD_LEN ,c_str! ("leds_lenovo_se10" )) + .ok_or (EBUSY ) + } + +/// Send command. +fn send_cmd (&self, cmd:LedCommand ) ->Result { +let region = self.request_cmd_region ()?; + region.outb (cmdas u8 ,0 ); +Ok (()) + } +} + +#[vtable] +impl Operations for LedSE10 { +type This =Led <LedSE10 >; + +const MAX_BRIGHTNESS :u8 =1 ; + +fn brightness_set (this: &mut Self ::This , brightness:u8 ) { +if let Err (e) =if brightness ==0 { + this.data.send_cmd (LedCommand ::Off ) + }else { + this.data.send_cmd (LedCommand ::On ) + } { +pr_warn! ("Failed to set led: {e:?}\n)" ) + } + } + +fn blink_set ( + this: &mut Self ::This , + delay_on:Delta , + delay_off:Delta , + ) ->Result <(Delta ,Delta )> { +if !(delay_on.is_zero () && delay_off.is_zero () + || delay_on ==Self ::HW_DURATION && delay_off ==Self ::HW_DURATION ) + { +return Err (EINVAL ); + } + + this.data.send_cmd (LedCommand ::Blink )?;
drivers/leds/leds_lenovo_se10.rs line 131
Quoted 10 lines
drivers/leds/leds_lenovo_se10.rs new file +10
-0+Ok ((Self ::HW_DURATION ,Self ::HW_DURATION )) + } +} + +#[vtable] +#[cfg( CONFIG_LEDS_TRIGGERS )] +impl triggers::HardwareOperations for LedSE10 { +fn activate (this: &mut Self ::This ) ->Result { + this.data.send_cmd (LedCommand ::Trigger ) + }
No deactivation method for the trigger? NACK.
The driver must implement the deactivation method, since LED core always allows disabling LED triggers. See led-trigger.c function led_trigger_write(): if "none" is written to the sysfs trigger file, the trigger is removed and the trigger file will afterwards report that no trigger is activated on the LED.
Since you did not implement the deactivation method, this will result in the system thinking that no LED trigger is selected on the LED, but in fact your LED's blinking will still be controlled by hardware.
Marek