Add first led driver written in Rust
Subsystems (4)
cover Add first led driver written in Rust
This patch series adds the first led driver written in Rust to the kernel.
The following changes were made:
* extends the not-yet-merged i2c patch series (see below) to include abstractions for reading and writing to i2c clients.
* add a wrapper of Vec, that allows to initialize PinInit items with the gurantee that these will never be moved.
* add basic led classdev abstractions to register and unregister leds
* add a driver for the atmega1608 microcontroller used in synology devices.
This patch series requires and is based on:
https://lore.kernel.org/rust-for-linux/[email protected]/T/#t
Atmega1608 is a programmable microcontroller. It has been preprogrammed to allow leds being controlled via the i2c bus. This driver can only be used on synology devices using this microcontroller.
Information on how to interact with it, has been gained by viewing the drivers/leds/leds-atmega1608.c file licensed under GPL found here (thats the only documentation):
https://global.synologydownload.com/download/ToolChain/Synology%20NAS%20GPL%20Source/7.2-72806/r1000/linux-4.4.x.txz
Markus Probst (4):
rust: i2c: add read and write byte data abstractions
rust: add pinned wrapper of Vec
rust: leds: add basic led classdev abstractions
leds: add driver for synology atmega1608 controlled LEDs
MAINTAINERS | 6 + drivers/leds/Kconfig | 9 + drivers/leds/Makefile | 1 + drivers/leds/leds_atmega1608.rs | 377 ++++++++++++++++++++++++++++++++ rust/kernel/alloc/kvec.rs | 86 ++++++++ rust/kernel/i2c.rs | 45 ++++ rust/kernel/led.rs | 299 +++++++++++++++++++++++++ rust/kernel/lib.rs | 1 + 8 files changed, 824 insertions(+) create mode 100644 drivers/leds/leds_atmega1608.rs create mode 100644 rust/kernel/led.rs
Signature · 1 line
2.49.1
PATCH 1/4 rust: i2c: add read and write byte data abstractions
In addition to the core abstractions, implement
* i2c::I2cClient::read_byte - read a byte from the i2c client
* i2c::I2cClient::write_byte - write a byte to the i2c client
* i2c::I2cClient::read_byte_data - read byte data from the i2c client
* i2c::I2cClient::write_byte_data - write byte data to the i2c client
* i2c::I2cClient::read_word_data - read word data from the i2c client
* i2c::I2cClient::write_word_data - write word data to the i2c client
--- rust/kernel/i2c.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+)
rust/kernel/i2c.rs +45 -0
@@ -463,6 +463,51 @@ impl<Ctx: device::DeviceContext> I2cClient<Ctx> {fn as_raw (&self) -> *mut bindings::i2c_client { self.0 .get () } + +/// Reads a byte from the i2c client +pub fn read_byte (&self) ->Result <u8 > { +// SAFETY: By the type invariant of `Self`, `self.as_raw()` is a pointer to a valid +// `struct i2c_client`. +let value =unsafe { bindings::i2c_smbus_read_byte (self.as_raw ()) }; +to_result (value).map (|()| valueas u8 ) + } + +/// Writes a byte to the i2c client +pub fn write_byte (&self, value:u8 ) ->Result <()> { +// SAFETY: By the type invariant of `Self`, `self.as_raw()` is a pointer to a valid +// `struct i2c_client`. +to_result (unsafe { bindings::i2c_smbus_write_byte (self.as_raw (), value) }) + } + +/// Reads byte data from the i2c client +pub fn read_byte_data (&self, reg:u8 ) ->Result <u8 > { +// SAFETY: By the type invariant of `Self`, `self.as_raw()` is a pointer to a valid +// `struct i2c_client`. +let value =unsafe { bindings::i2c_smbus_read_byte_data (self.as_raw (), reg) }; +to_result (value).map (|()| valueas u8 ) + } + +/// Writes byte data to the i2c client +pub fn write_byte_data (&self, reg:u8 , value:u8 ) ->Result <()> { +// SAFETY: By the type invariant of `Self`, `self.as_raw()` is a pointer to a valid +// `struct i2c_client`. +to_result (unsafe { bindings::i2c_smbus_write_byte_data (self.as_raw (), reg, value) }) + } + +/// Reads word data from the i2c client +pub fn read_word_data (&self, reg:u8 ) ->Result <u16 > { +// SAFETY: By the type invariant of `Self`, `self.as_raw()` is a pointer to a valid +// `struct i2c_client`. +let value =unsafe { bindings::i2c_smbus_read_word_data (self.as_raw (), reg) }; +to_result (value).map (|()| valueas u16 ) + } + +/// Writes word data to the i2c client +pub fn write_word_data (&self, reg:u8 , value:u16 ) ->Result <()> { +// SAFETY: By the type invariant of `Self`, `self.as_raw()` is a pointer to a valid +// `struct i2c_client`. +to_result (unsafe { bindings::i2c_smbus_write_word_data (self.as_raw (), reg, value) }) + } }// SAFETY: `I2cClient` is a transparent wrapper of a type that doesn't depend on `I2cClient`'s generic
Signature · 1 line
2.49.1
PATCH 2/4 rust: add pinned wrapper of Vec
Implement a wrapper of Vec that guarantees that its content will never be moved, unless the item implements Unpin, allowing PinInit to be initialized on the Vec.
--- rust/kernel/alloc/kvec.rs | 86 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+)
rust/kernel/alloc/kvec.rs +86 -0
@@ -21,6 +21,7 @@ slice, slice::SliceIndex, }; +use pin_init::PinInit ;mod errors;pub use self::errors::{InsertError ,PushError ,RemoveError };
@@ -109,6 +110,11 @@ pub struct Vec<T, A: Allocator> { _p: PhantomData<A>, } +/// A pinned wrapper of the [`Vec`] type. +/// +/// It is guaranteed that the contents will never be moved, unless T implements Unpin. +pub struct PinnedVec <T ,A :Allocator >(Vec <T ,A >); +/// Type alias for [`Vec`] with a [`Kmalloc`] allocator. /// /// # Examples
@@ -121,6 +127,8 @@ pub struct Vec<T, A: Allocator> {/// # Ok::<(), Error>(()) /// ``` pub type KVec <T > =Vec <T ,Kmalloc >; +/// Type alias for [`PinnedVec`] with a [`Kmalloc`] allocator. +pub type KPinnedVec <T > =PinnedVec <T ,Kmalloc >;/// Type alias for [`Vec`] with a [`Vmalloc`] allocator. ///
@@ -134,6 +142,8 @@ pub struct Vec<T, A: Allocator> {/// # Ok::<(), Error>(()) /// ``` pub type VVec <T > =Vec <T ,Vmalloc >; +/// Type alias for [`PinnedVec`] with a [`Vmalloc`] allocator. +pub type VPinnedVec <T > =PinnedVec <T ,Vmalloc >;/// Type alias for [`Vec`] with a [`KVmalloc`] allocator. ///
@@ -147,6 +157,8 @@ pub struct Vec<T, A: Allocator> {/// # Ok::<(), Error>(()) /// ``` pub type KVVec <T > =Vec <T ,KVmalloc >; +/// Type alias for [`PinnedVec`] with a [`KVmalloc`] allocator. +pub type KVPinnedVec <T > =PinnedVec <T ,KVmalloc >;// SAFETY: `Vec` is `Send` if `T` is `Send` because `Vec` owns its elements. unsafe impl <T ,A >Send for Vec <T ,A >
@@ -1294,6 +1306,80 @@ fn drop(&mut self) { } } +impl <T ,A :Allocator >PinnedVec <T ,A > { +/// Creates a new [`PinnedVec`] instance with at least the given capacity. +pub fn with_capacity (capacity:usize , flags:Flags ) ->Result <Self ,AllocError > { +Vec ::with_capacity (capacity, flags).map (Self ) + } + +/// Shortens the vector, setting the length to `len` and drops the removed values. +/// If `len` is greater than or equal to the current length, this does nothing. +/// +/// This has no effect on the capacity and will not allocate. +pub fn truncate (&mut self, len:usize ) { + self.0 .truncate (len); + } + +/// Pin-initializes P and appends it to the back of the [`Vec`] instance without reallocating. +pub fn push_pin_init <E ,P :PinInit <T ,E >>(&mut self, init:P ) ->Result <(),E > +where +E :From <PushError <P >>, + { +if self.0 .len () < self.0 .capacity () { +let spare = self.0 .spare_capacity_mut (); +// SAFETY: the length is less than the capacity, so `spare` is non-empty. +unsafe { init.__pinned_init (spare.get_unchecked_mut (0 ).as_mut_ptr ())? }; +// SAFETY: We just initialised the first spare entry, so it is safe to +// increase the length by 1. We also know that the new length is <= capacity. +unsafe { self.0 .inc_len (1 ) }; +Ok (()) + }else { +Err (E ::from (PushError (init))) + } + } + +/// Removes the last element from a vector and drops it returning true, or false if it is empty. +pub fn pop (&mut self) ->bool { +if self.is_empty () { +return false ; + } + +// SAFETY: We just checked that the length is at least one. +let ptr: *mut [T ] =unsafe { self.0 .dec_len (1 ) }; + +// SAFETY: the contract of `dec_len` guarantees that the elements in `ptr` are +// valid elements whose ownership has been transferred to the caller. +unsafe { ptr::drop_in_place (ptr) }; +true + } +} + +impl <T ,A :Allocator >Deref for PinnedVec <T ,A > { +type Target =Vec <T ,A >; + +fn deref (&self) -> &Self ::Target { + &self.0 + } +} + +impl <T :Unpin ,A :Allocator >DerefMut for PinnedVec <T ,A > { +fn deref_mut (&mut self) -> &mut Self ::Target { + &mut self.0 + } +} + +impl <T ,A :Allocator >From <Vec <T ,A >>for PinnedVec <T ,A > { +fn from (value:Vec <T ,A >) ->Self { +Self (value) + } +} + +impl <T :Unpin ,A :Allocator >From <PinnedVec <T ,A >>for Vec <T ,A > { +fn from (value:PinnedVec <T ,A >) ->Self { + value.0 + } +} +#[macros::kunit_tests(rust_kvec_kunit)] mod tests {use super ::*;
Signature · 1 line
2.49.1
PATCH 3/4 rust: leds: add basic led classdev abstractions
Implement the core abstractions needed for led class devices, including:
* led::LedHandler - the trait for handling leds, including brightness_set
* led::LedInitData - data set for the led class device
* led::LedClassDev - a safe wrapper arround led_classdev
--- rust/kernel/led.rs | 299 +++++++++++++++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 1 + 2 files changed, 300 insertions(+) create mode 100644 rust/kernel/led.rs
rust/kernel/led.rs new file +299 -0
@@ -0,0 +1,299 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Abstractions for the leds driver model. +//! +//! C header: [`include/linux/leds.h`](srctree/include/linux/leds.h) + +use core::pin::Pin ; + +use pin_init::{pin_data, pinned_drop,PinInit }; + +use crate ::{ + alloc::KBox , + container_of, + device::{property::FwNode ,Device }, + error::{code::EINVAL , from_result, to_result,Error ,Result }, + prelude::GFP_KERNEL , + str::{CStr ,CString }, + try_pin_init, + types::{ARef ,Opaque }, +}; + +/// The led class device representation. +/// +/// This structure represents the Rust abstraction for a C `struct led_classdev`. +#[pin_data( PinnedDrop )] +pub struct LedClassDev { + default_trigger:Option <CString >, + parent:Option <ARef <Device >>, + handler:KBox <dyn LedHandlerImpl >, +#[pin] + classdev:Opaque <bindings::led_classdev >, +} + +/// The led init data representation. +/// +/// This structure represents the Rust abstraction for a C `struct led_init_data`. +#[derive( Default )] +pub struct LedInitData <'a> { + fwnode:Option <&'aFwNode >, + default_label:Option <&'aCStr >, + devicename:Option <&'aCStr >, + devname_mandatory:bool , +} + +impl LedInitData <'static> { +/// Creates a new [`LedInitData`] +pub fn new () ->Self { +Self ::default () + } +} + +impl <'a>LedInitData <'a> { +/// Sets the firmware node +pub fn fwnode <'b, 'c>(self, fwnode: &'bFwNode ) ->LedInitData <'c> +where + 'a: 'c, + 'b: 'c, + { +LedInitData { + fwnode:Some (fwnode), + ..self + } + } + +/// Sets a default label +pub fn default_label <'b, 'c>(self, label: &'bCStr ) ->LedInitData <'c> +where + 'a: 'c, + 'b: 'c, + { +LedInitData { + default_label:Some (label), + ..self + } + } + +/// Sets the device name +pub fn devicename <'b, 'c>(self, devicename: &'bCStr ) ->LedInitData <'c> +where + 'a: 'c, + 'b: 'c, + { +LedInitData { + devicename:Some (devicename), + ..self + } + } + +/// Sets if a device name is mandatory +pub fn devicename_mandatory (self, mandatory:bool ) ->Self { +Self { + devname_mandatory: mandatory, + + ..self + } + } +} + +/// The led handler trait. +/// +/// # Examples +/// +///``` +/// # use kernel::{c_str, led, alloc::KBox, error::{Result, code::ENOSYS}}; +/// # use core::pin::Pin; +/// +/// struct MyHandler; +/// +/// +/// impl led::LedHandler for MyHandler { +/// const BLOCKING = false; +/// const MAX_BRIGHTNESS = 255; +/// +/// fn brightness_set(&self, _brightness: u32) -> Result<()> { +/// // Set the brightness for the led here +/// Ok(()) +/// } +/// } +/// +/// fn register_my_led() -> Result<Pin<KBox<led::LedClassDev>>> { +/// let handler = MyHandler; +/// KBox::pin_init(led::LedClassDev::new( +/// None, +/// None, +/// led::LedInitData::new() +/// .default_label(c_str!("my_led")), +/// handler, +/// )) +/// } +///``` +/// Led drivers must implement this trait in order to register and handle a [`LedClassDev`]. +pub trait LedHandler { +/// If set true, [`LedHandler::brightness_set`] and [`LedHandler::blink_set`] must not sleep +/// and perform the operation immediately. +const BLOCKING :bool ; +/// Set this to true, if [`LedHandler::blink_set`] is implemented. +const BLINK :bool =false ; +/// The max brightness level +const MAX_BRIGHTNESS :u32 ; + +/// Sets the brightness level +/// +/// See also [`LedHandler::BLOCKING`] +fn brightness_set (&self, brightness:u32 ) ->Result <()>; + +/// Activates hardware accelerated blinking. +/// +/// delays are in milliseconds. If both are zero, a sensible default should be chosen. +/// The caller should adjust the timings in that case and if it can't match the values +/// specified exactly. Setting the brightness to 0 will disable the hardware accelerated +/// blinking. +/// +/// See also [`LedHandler::BLOCKING`] +fn blink_set (&self, _delay_on: &mut usize , _delay_off: &mut usize ) ->Result <()> { +Err (EINVAL ) + } +} + +trait LedHandlerImpl { +fn brightness_set (&self, brightness:u32 ) ->Result <()>; +fn blink_set (&self, delay_on: &mut usize , delay_off: &mut usize ) ->Result <()>; +} + +impl <T :LedHandler >LedHandlerImpl for T { +fn brightness_set (&self, brightness:u32 ) ->Result <()> { +T ::brightness_set (self, brightness) + } + +fn blink_set (&self, delay_on: &mut usize , delay_off: &mut usize ) ->Result <()> { +T ::blink_set (self, delay_on, delay_off) + } +} + +// SAFETY: A `LedClassDev` can be unregistered from any thread. +unsafe impl Send for LedClassDev {} + +// SAFETY: `LedClassDev` can be shared among threads because all methods of `LedClassDev` +// are thread safe. +unsafe impl Sync for LedClassDev {} + +impl LedClassDev { +/// Registers a new led classdev. +/// +/// The [`LedClassDev`] will be unregistered and drop. +pub fn new <'a,T :LedHandler + 'static>( + parent:Option <&Device >, + default_trigger:Option <CString >, + init_data:LedInitData <'a>, + handler:T , + ) ->impl PinInit <Self ,Error > +use <'a,T > { +let parent_ptr = parent.map_or (core::ptr::null_mut (),Device ::as_raw); +let parent = parent.map (ARef ::from); +try_pin_init! (Self { + default_trigger, + parent, + handler <- { +let handler:KBox <dyn LedHandlerImpl > =KBox ::<T >::new (handler,GFP_KERNEL )?; +Ok ::<_ ,Error >(handler) + }, + classdev <-Opaque ::try_ffi_init (|ptr: *mut bindings::led_classdev | { +// SAFETY: `try_ffi_init` guarantees that `ptr` is valid for write. +unsafe { ptr.write (bindings::led_classdev { + max_brightness:T ::MAX_BRIGHTNESS , + brightness_set:T ::BLOCKING .then_some ( + brightness_setas unsafe extern "C" fn (*mut bindings::led_classdev ,u32 ) + ), + brightness_set_blocking: (!T ::BLOCKING ).then_some ( + brightness_set_blocking +as unsafe extern "C" fn (*mut bindings::led_classdev ,u32 ) ->i32 + ), + blink_set:T ::BLINK .then_some ( + blink_set +as unsafe extern "C" fn (*mut bindings::led_classdev , *mut usize , + *mut usize ) ->i32 + ), + .. bindings::led_classdev::default () + }) }; + +let mut init_data = bindings::led_init_data { + fwnode: init_data.fwnode.map_or (core::ptr::null_mut (),FwNode ::as_raw), + default_label: init_data.default_label + .map_or (core::ptr::null (),CStr ::as_char_ptr), + devicename: init_data.devicename.map_or (core::ptr::null (),CStr ::as_char_ptr), + devname_mandatory: init_data.devname_mandatory, + }; +// SAFETY: +// - `parent_ptr` is guaranteed to be a pointer to a valid `device` +// or a null pointer. +// - `ptr` is guaranteed to be a pointer to an initialized `led_classdev`. +to_result (unsafe { + bindings::led_classdev_register_ext (parent_ptr, ptr, &mut init_data) + }) + }), + }) + } +} + +extern "C" fn brightness_set (led_cdev: *mut bindings::led_classdev , brightness:u32 ) { +// SAFETY: `led_cdev` is a valid pointer to a `led_classdev` stored inside a `LedClassDev`. +let classdev =unsafe { + &*container_of! ( + led_cdev.cast ::<Opaque <bindings::led_classdev >>(), +LedClassDev , + classdev + ) + }; +let _ = classdev.handler.brightness_set (brightness); +} + +extern "C" fn brightness_set_blocking ( + led_cdev: *mut bindings::led_classdev , + brightness:u32 , +) ->i32 { +// SAFETY: `led_cdev` is a valid pointer to a `led_classdev` stored inside a `LedClassDev`. +let classdev =unsafe { + &*container_of! ( + led_cdev.cast ::<Opaque <bindings::led_classdev >>(), +LedClassDev , + classdev + ) + }; +from_result (|| { + classdev.handler.brightness_set (brightness)?; +Ok (0 ) + }) +} + +extern "C" fn blink_set ( + led_cdev: *mut bindings::led_classdev , + delay_on: *mut usize , + delay_off: *mut usize , +) ->i32 { +// SAFETY: `led_cdev` is a valid pointer to a `led_classdev` stored inside a `LedClassDev`. +let classdev =unsafe { + &*container_of! ( + led_cdev.cast ::<Opaque <bindings::led_classdev >>(), +LedClassDev , + classdev + ) + }; +from_result (|| { + classdev.handler.blink_set ( +// SAFETY: `delay_on` is guaranteed to be a valid pointer to usize +unsafe { &mut *delay_on }, +// SAFETY: `delay_on` is guaranteed to be a valid pointer to usize +unsafe { &mut *delay_off }, + )?; +Ok (0 ) + }) +} + +#[pinned_drop] +impl PinnedDrop for LedClassDev { +fn drop (self:Pin <&mut Self >) { +// SAFETY: The existence of `self` guarantees that `self.classdev` has previously been +// successfully registered with `led_classdev_register_ext` +unsafe { bindings::led_classdev_unregister (self.classdev.get ()) }; + } +}
rust/kernel/lib.rs +1 -0
@@ -97,6 +97,7 @@pub mod jump_label;#[cfg( CONFIG_KUNIT )] pub mod kunit; +pub mod led;pub mod list;pub mod miscdevice;pub mod mm;
Signature · 1 line
2.49.1
PATCH 4/4 leds: add driver for synology atmega1608 controlled LEDs
The Atmega1608 is a microcontroller used by synology devices to control leds via the i2c bus. It can handle up to 24 leds.
Successfully tested on a Synology DS923+.
--- MAINTAINERS | 6 + drivers/leds/Kconfig | 9 + drivers/leds/Makefile | 1 + drivers/leds/leds_atmega1608.rs | 377 ++++++++++++++++++++++++++++++++ 4 files changed, 393 insertions(+) create mode 100644 drivers/leds/leds_atmega1608.rs
MAINTAINERS +6 -0
@@ -24401,6 +24401,12 @@ F: drivers/dma-buf/sync_* F: include/linux/sync_file.h F: include/uapi/linux/sync_file.h +SYNOLOGY ATMEGA1608 LED CONTROLLER DRIVER +M: Markus Probst <[email protected]> +L: [email protected] +S: Maintained +F: drivers/leds/leds_atmega1608.rs + SYNOPSYS ARC ARCHITECTURE M: Vineet Gupta <[email protected]> L: [email protected]
drivers/leds/Kconfig +9 -0
@@ -323,6 +323,15 @@ config LEDS_WRAP help This option enables support for the PCEngines WRAP programmable LEDs. +config LEDS_ATMEGA1608 + tristate "LED Support for Atmega1608 used in Synology devices" + depends on LEDS_CLASS + depends on I2C + depends on RUST + help + This option enables support for the Atmega1608 microcontroller used + as led controller in synology devices. + config LEDS_COBALT_QUBE tristate "LED Support for the Cobalt Qube series front LED" depends on LEDS_CLASS
drivers/leds/Makefile +1 -0
@@ -98,6 +98,7 @@ obj-$(CONFIG_LEDS_UPBOARD) += leds-upboard.o obj-$(CONFIG_LEDS_WM831X_STATUS) += leds-wm831x-status.o obj-$(CONFIG_LEDS_WM8350) += leds-wm8350.o obj-$(CONFIG_LEDS_WRAP) += leds-wrap.o +obj-$(CONFIG_LEDS_ATMEGA1608) += leds_atmega1608.o # Kinetic ExpressWire Protocol obj-$(CONFIG_LEDS_EXPRESSWIRE) += leds-expresswire.o
drivers/leds/leds_atmega1608.rs new file +377 -0
377 lines not on this page · raw message
Signature · 1 line
2.49.1
Danilo Krummrich
maintainerRe: [PATCH 4/4] leds: add driver for synology atmega1608 controlled LEDs
(Not a full review (let's work out the dependencies first), but there's one thing that stood out to me.)
drivers/leds/leds_atmega1608.rs line 142
Quoted 7 lines · Markus Probst
drivers/leds/leds_atmega1608.rs new file +7
-0+struct Atmega1608Led { + addr:Atmega1608LedAddress , + id:Atmega1608LedId , + + client:ARef <I2cClient >, + + mode_lock:Arc <Mutex <()>>,
Mutex<()> raises an eyebrow, since a mutex that doesn't protect anything is pointless. So, I assume it is protecting some data, but in an unsound way.
drivers/leds/leds_atmega1608.rs line 310
Quoted 3 lines
drivers/leds/leds_atmega1608.rs new file +3
-0+impl Atmega1608Led { +fn update_mode (&self, mode:Atmega1608LedMode ) ->Result <Atmega1608LedMode > { +let _guard = self.mode_lock.lock ();
What exactly does the mutex protect in the code below?
Quoted 28 lines
drivers/leds/leds_atmega1608.rs new file +28
-0+ +let mut current = self + .client + .read_byte_data (self.addras u8 ) + .inspect_err (|err| { +dev_err! ( + self.client.as_ref (), +"failed to read {:#2x}: {err:?}\n" , + self.addras u8 + ); + })?; + + current = + (current & !self.id.mask ()) | (((modeas u8 ) << self.id.shift ()) & self.id.mask ()); + + self.client + .write_byte_data (self.addras u8 , current) + .inspect_err (|err| { +dev_err! ( + self.client.as_ref (), +"failed to write {:#2x}: {err:?}" , + self.addras u8 + ); + })?; + +Ok (mode) + } +}
Re: [PATCH 4/4] leds: add driver for synology atmega1608 controlled LEDs
Quoted 18 lines · Danilo Krummrich
(Not a full review (let's work out the dependencies first), but there's one
thing that stood out to me.)drivers/leds/leds_atmega1608.rs line 142
Quoted 7 lines · Markus Probst
drivers/leds/leds_atmega1608.rs new file +7
-0+struct Atmega1608Led { + addr: Atmega1608LedAddress, + id: Atmega1608LedId, + + client: ARef<I2cClient>, + + mode_lock: Arc<Mutex<()>>,Mutex<()> raises an eyebrow, since a mutex that doesn't protect anything is
pointless. So, I assume it is protecting some data, but in an unsound way.
Quoted 6 lines
Quoted 4 lines
+impl Atmega1608Led {
+ fnupdate_mode(&self, mode: Atmega1608LedMode)-> Result<Atmega1608LedMode> {
+ let_guard=self.mode_lock.lock();What exactly does the mutex protect in the code below?
Otherwise there would be a race condition. Each register has 8 bits, each led has 2 bits. If the led mode is updated at the same time with another one in the same register, it could lead to the first action being overwritten by the second.
Meaning if two actions run at the same time:
- led0 reads from the register
- led1 reads from the register
- led0 writes to the register
- led1 writes to the register (the changes for led0 have been overwritten here, as it did read the register before led0 has written to it)
Quoted 22 lines
Quoted 22 lines
drivers/leds/leds_atmega1608.rs new file +14
-0+ + let mut current = self + .client + .read_byte_data(self.addr as u8) + .inspect_err(|err| { + dev_err!( + self.client.as_ref(), + "failed to read {:#2x}: {err:?}\n", + self.addr as u8 + ); + })?; + + current = + (current & !self.id.mask()) | (((mode as u8) <<
self.id.shift()) &self.id.mask());
+
+ self.client
+ .write_byte_data(self.addr as u8, current) + .inspect_err(|err| {
+ dev_err!(
+self.client.as_ref(),
Quoted 8 lines
drivers/leds/leds_atmega1608.rs line 338
Quoted 8 lines
+ "failed to write {:#2x}: {err:?}", + self.addr as u8
+ );
+ })?;drivers/leds/leds_atmega1608.rs new file +4
-0+ + Ok(mode) + } +}
Thanks
- Markus Probst
Danilo Krummrich
maintainerRe: [PATCH 4/4] leds: add driver for synology atmega1608 controlled LEDs
(Cc: Alex)
Quoted 25 lines · Markus Probst
Quoted 18 lines · Danilo Krummrich
(Not a full review (let's work out the dependencies first), but there's one
thing that stood out to me.)drivers/leds/leds_atmega1608.rs line 142
Quoted 7 lines · Markus Probst
drivers/leds/leds_atmega1608.rs new file +7
-0+struct Atmega1608Led { + addr: Atmega1608LedAddress, + id: Atmega1608LedId, + + client: ARef<I2cClient>, + + mode_lock: Arc<Mutex<()>>,Mutex<()> raises an eyebrow, since a mutex that doesn't protect anything is
pointless. So, I assume it is protecting some data, but in an unsound way.Quoted 6 lines
Quoted 4 lines
+impl Atmega1608Led {
+ fnupdate_mode(&self, mode: Atmega1608LedMode)-> Result<Atmega1608LedMode> {
+ let_guard=self.mode_lock.lock();What exactly does the mutex protect in the code below?
Quoted 11 lines
Otherwise there would be a race condition. Each register has 8 bits, each led has 2 bits. If the led mode is updated at the same time with another one in the same register, it could lead to the first action being overwritten by the second.
Meaning if two actions run at the same time:
- led0 reads from the register
- led1 reads from the register
- led0 writes to the register
- led1 writes to the register (the changes for led0 have been overwritten here, as it did read the register before led0 has written to it)
Ok, so you need exclusive access to a register. I think this is something the register abstraction I also mentioned in [1] could support.
@Alex: Have you thought about this already?
[1] https://lore.kernel.org/rust-for-linux/[email protected]/
Alexandre Courbot
reviewerRe: [PATCH 4/4] leds: add driver for synology atmega1608 controlled LEDs
Quoted 40 lines · Danilo Krummrich
(Cc: Alex)
Quoted 25 lines · Markus Probst
Quoted 18 lines · Danilo Krummrich
(Not a full review (let's work out the dependencies first), but there's one
thing that stood out to me.)drivers/leds/leds_atmega1608.rs line 142
Quoted 7 lines · Markus Probst
drivers/leds/leds_atmega1608.rs new file +7
-0+struct Atmega1608Led { + addr: Atmega1608LedAddress, + id: Atmega1608LedId, + + client: ARef<I2cClient>, + + mode_lock: Arc<Mutex<()>>,Mutex<()> raises an eyebrow, since a mutex that doesn't protect anything is
pointless. So, I assume it is protecting some data, but in an unsound way.Quoted 6 lines
Quoted 4 lines
+impl Atmega1608Led {
+ fnupdate_mode(&self, mode: Atmega1608LedMode)-> Result<Atmega1608LedMode> {
+ let_guard=self.mode_lock.lock();What exactly does the mutex protect in the code below?
Quoted 11 lines
Otherwise there would be a race condition. Each register has 8 bits, each led has 2 bits. If the led mode is updated at the same time with another one in the same register, it could lead to the first action being overwritten by the second.
Meaning if two actions run at the same time:
- led0 reads from the register
- led1 reads from the register
- led0 writes to the register
- led1 writes to the register (the changes for led0 have been overwritten here, as it did read the register before led0 has written to it)
Quoted 6 lines
Ok, so you need exclusive access to a register. I think this is something the register abstraction I also mentioned in [1] could support.
@Alex: Have you thought about this already?
[1] https://lore.kernel.org/rust-for-linux/[email protected]/
I haven't at all. :) And indeed intuitively I couldn't say what would be
the right level to provide such concurrent access protection. When
someone reads from a register, there is no guarantee that they did so
with the intent of writing an updated value back to it, so I don't think we can e.g. hold a guard for as long as the read value exists.
Even the alter accessor does not take any lock for the duration of its closure, so I don't think it protects us. OTOH, led0 would hold the IO resource for as long as alter runs, which should prevent led1 from accessing it? If so, I'd say that's the correct way of handling such cases.
(drive-by comment: I will probably rename alter into update to match the regmap API once the dust settles with all the code moves related to register!())
Re: [PATCH 4/4] leds: add driver for synology atmega1608 controlled LEDs
In reply to Markus ProbstHi!
Quoted 2 lines
The Atmega1608 is a microcontroller used by synology devices to control leds via the i2c bus. It can handle up to 24 leds.
Ok, but driver is not really for atmega1608, but for whatever code is running there, right?
I would not mind that code being opensource.
I would not mind if we had standard interface to these so the next person can reuse the protocol.
drivers/leds/Kconfig config LEDS_WRAP · line 327
Quoted 7 lines
+++ b/drivers/leds/Kconfig
drivers/leds/Kconfig +2
-0@@ -323,6 +323,15 @@ config LEDS_WRAP help This option enables support for the PCEngines WRAP programmable LEDs. +config LEDS_ATMEGA1608 + tristate "LED Support for Atmega1608 used in Synology devices"
There's nothing Atmega specific, right?
"LED Support for Synology devices"?
drivers/leds/Kconfig config LEDS_WRAP · line 333
Quoted 6 lines
drivers/leds/Kconfig +6
-0+ depends on LEDS_CLASS + depends on I2C + depends on RUST + help + This option enables support for the Atmega1608 microcontroller used + as led controller in synology devices.
led->LEDsynology->Synology
drivers/leds/leds_atmega1608.rs line 3
Quoted 5 lines
drivers/leds/leds_atmega1608.rs new file +4
-0+++ b/drivers/leds/leds_atmega1608.rs@@ -0,0 +1,377 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Led Driver for Synology devices using Atmega1608 as Led Controller.
Led->LED
drivers/leds/leds_atmega1608.rs line 6
Quoted 2 lines
drivers/leds/leds_atmega1608.rs new file +2
-0+//! Atmega1608 is a microcontroller from Microchip Technology, it is used +//! as a led controller in synology nas devices.
led->LED
(Please fix globally).
Quoted 8 lines
drivers/leds/leds_atmega1608.rs new file +8
-0+//! # Limitations +//! +//! DIM0 (and DIM1) are shared across all leds, meaning if DIM0 is used by +//! multiple leds, these multiple leds cannot have different brightness +//! levels. The same does apply for the hardware accelerated blinking. +//! +//! In other words, for all 24 leds there can either only be one brightness other +//! than 0 and 255, or one hardware accelerated blinking delay.
drivers/leds/leds_atmega1608.rs line 34
Quoted 10 lines
drivers/leds/leds_atmega1608.rs new file +10
-0+//! +//! Furthermore the off and on delay in hardware accelerated blinking cannot +//! have different values and have to be equal. We solve this by calculating +//! the average of those numbers and use it as delay for both. The delay +//! cannot be larger than 765 ms (255*3). +//! +//! While hardware accelerated blinking is on, the led cycles between +//! the current brightness for the mode and full brightness. Because of this +//! behaviour, we hardcode the brightness value of 128 if hardware accelerated +//! blinking is used.
Maybe we should just pretend that hw acceleration does not exist? Or pretend it can only do single blinking rate.
drivers/leds/leds_atmega1608.rs line 104
Quoted 8 lines
drivers/leds/leds_atmega1608.rs new file +8
-0+ +kernel::module_i2c_driver! { +type :Atmega1608LedDriver , + name:"atmega1608" , + authors: ["Markus Probst <[email protected]>" ], + description:"Led Driver for Synology devices using Atmega1608 as Led Controller" , + license:"GPL v2" , +}
This should be called synologyLedDriver or something. We have driver for Thinkpad LEDs. We don't care what kind of hardware it runs on.
drivers/leds/leds_atmega1608.rs line 278
Quoted 13 lines
drivers/leds/leds_atmega1608.rs new file +13
-0+impl LedHandler for Atmega1608Led { +const BLOCKING :bool =true ; +const BLINK :bool =true ; +const MAX_BRIGHTNESS :u32 =255 ; + +fn brightness_set (&self, brightness:u32 ) ->Result <()> { +let brightness = u8::try_from (brightness).unwrap_or (255 ); + +let mode = self.update_mode (match brightness { +0 =>Atmega1608LedMode ::Off , +255 |254 =>Atmega1608LedMode ::On , + _ =>Atmega1608LedMode ::Dim0 , + })?;
Umm... so the hardware can only do on / off and dim. And dim is strange.
Certainly MAX_BRIGHTNESS should not be 255, but 1 (or 2 if you want to support dim mode).
One idea: Use hardware diming to have three levels. So MAX_BRIGHTNESS = 2. Then ignore hardware accelerated blinking. And you should have working and simpler driver.
Thanks and best regards,
Pavel
Signature · 2 lines
I don't work for Nazis and criminals, and neither should you. Boycott Putin, Trump, Netanyahu and Musk!
Re: [PATCH 4/4] leds: add driver for synology atmega1608 controlled LEDs
Quoted 5 lines · Pavel Machek
Hi!
Quoted 2 lines
The Atmega1608 is a microcontroller used by synology devices to control leds via the i2c bus. It can handle up to 24 leds.
Quoted 7 lines
Ok, but driver is not really for atmega1608, but for whatever code is running there, right?
I would not mind that code being opensource.
I would not mind if we had standard interface to these so the next person can reuse the protocol.
Me neither. But Synology is a company which seems to like proprietary software. They have there own Linux-based (4.4.x kernel) operating system, which is proprietary. Those devices are not intended for installing other operating systems and doing so will certainly void the warranty, but that doesn't stop me from doing it anyway.
Quoted 11 lines
drivers/leds/Kconfig config LEDS_WRAP · line 327
Quoted 9 lines
+++ b/drivers/leds/Kconfig
drivers/leds/Kconfig +2
-0@@ -323,6 +323,15 @@ config LEDS_WRAP help This option enables support for the PCEngines WRAP programmable LEDs. +config LEDS_ATMEGA1608 + tristate "LED Support for Atmega1608 used in Synology devices"There's nothing Atmega specific, right?
Not that I am aware of. The driver interacts with code running on the Atmega1608.
Quoted 2 lines
"LED Support for Synology devices"?
I don't think thats the only driver they use for controlling disk leds (although it seems the only synology specific they have added to their kernel. They also seem to use LP3943 for some of their models, according to their kernel config, which is already in the upstream kernel), so it could mislead users into thinking this driver will be useful on every synology device.
But I agree the config naming could be better.
Quoted 78 lines
drivers/leds/Kconfig config LEDS_WRAP · line 333
Quoted 7 lines
drivers/leds/Kconfig +6
-0+ depends on LEDS_CLASS + depends on I2C + depends on RUST + help + This option enables support for the Atmega1608 microcontroller used + as led controller in synology devices.
led->LEDsynology->Synologydrivers/leds/leds_atmega1608.rs line 3
Quoted 5 lines
drivers/leds/leds_atmega1608.rs new file +4
-0+++ b/drivers/leds/leds_atmega1608.rs@@ -0,0 +1,377 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Led Driver for Synology devices using Atmega1608 as Led Controller.
Led->LEDdrivers/leds/leds_atmega1608.rs line 6
Quoted 2 lines
drivers/leds/leds_atmega1608.rs new file +2
-0+//! Atmega1608 is a microcontroller from Microchip Technology, it is used +//! as a led controller in synology nas devices.
led->LED(Please fix globally).
Quoted 8 lines
drivers/leds/leds_atmega1608.rs new file +8
-0+//! # Limitations +//! +//! DIM0 (and DIM1) are shared across all leds, meaning if DIM0 is used by +//! multiple leds, these multiple leds cannot have different brightness +//! levels. The same does apply for the hardware accelerated blinking. +//! +//! In other words, for all 24 leds there can either only be one brightness other +//! than 0 and 255, or one hardware accelerated blinking delay. drivers/leds/leds_atmega1608.rs line 34
Quoted 10 lines
drivers/leds/leds_atmega1608.rs new file +10
-0+//! +//! Furthermore the off and on delay in hardware accelerated blinking cannot +//! have different values and have to be equal. We solve this by calculating +//! the average of those numbers and use it as delay for both. The delay +//! cannot be larger than 765 ms (255*3). +//! +//! While hardware accelerated blinking is on, the led cycles between +//! the current brightness for the mode and full brightness. Because of this +//! behaviour, we hardcode the brightness value of 128 if hardware accelerated +//! blinking is used. Maybe we should just pretend that hw acceleration does not exist? Or pretend it can only do single blinking rate.
drivers/leds/leds_atmega1608.rs line 104
Quoted 8 lines
drivers/leds/leds_atmega1608.rs new file +8
-0+ +kernel::module_i2c_driver! { +type :Atmega1608LedDriver , + name:"atmega1608" , + authors: ["Markus Probst <[email protected]>" ], + description:"Led Driver for Synology devices using Atmega1608 as Led Controller" , + license:"GPL v2" , +}This should be called synologyLedDriver or something. We have driver for Thinkpad LEDs. We don't care what kind of hardware it runs on.
drivers/leds/leds_atmega1608.rs line 278
Quoted 13 lines
drivers/leds/leds_atmega1608.rs new file +13
-0+impl LedHandler for Atmega1608Led { +const BLOCKING :bool =true ; +const BLINK :bool =true ; +const MAX_BRIGHTNESS :u32 =255 ; + +fn brightness_set (&self, brightness:u32 ) ->Result <()> { +let brightness = u8::try_from (brightness).unwrap_or (255 ); + +let mode = self.update_mode (match brightness { +0 =>Atmega1608LedMode ::Off , +255 |254 =>Atmega1608LedMode ::On , + _ =>Atmega1608LedMode ::Dim0 , + })?;Umm... so the hardware can only do on / off and dim. And dim is strange.
Quoted 6 lines
Certainly
MAX_BRIGHTNESSshould not be 255, but 1 (or 2 if you want to support dim mode).One idea: Use hardware diming to have three levels. So
MAX_BRIGHTNESS= 2. Then ignore hardware accelerated blinking. And you should have working and simpler driver.
Those LEDs are labeled "Disk 1", "Disk 2", "Disk 3", "Disk 4" ..., I think blinking is more important here than brightness. The main purpose of those leds is to show disk activity (blink) and issues (different color). Pretending it can only do a single blinking rate seems to be the way to go.
Another reason why brightness is less important here:
There are other leds: Status, Power, USB, Alert which are handled by a different driver (communication through uart). I am also currently working on supporting these (the driver would be in the drivers/mfd tree as it takes care of other things too. I have a working prototyp, but its far from being ready for the kernel).
Synology devices have a global brightness setting, having an impact on all the leds mentioned above (not just via the atmega1608). On my Synology NAS device (DS923+), its controlled via the '0x2e' i2c data address (outside of the atmega1608 range) on the same i2c bus, but that will propably differ per device (they have an xml config on each device, for how to control the brightness). I think this should be handled by userspace via i2c-dev (default is MAX brightness) and not by the kernel.
Also I noticed, that there isn't a "per-disk led trigger", which did actually suprise me. Just need to search "NAS" on the web and I find enough devices (not just from Synology) having a per-disk led, which proabably run Linux. There is "disk-activity", but thats for all the disks. Is there another way for linking the disk activity of one specific disk to a specific led that I missed? Otherwise I would write (in C) a led trigger for this purpose.
Thanks
- Markus Probst
Quoted 3 lines
Thanks and best regards,
Pavel
Danilo Krummrich
maintainerRe: [PATCH 3/4] rust: leds: add basic led classdev abstractions
In reply to Markus Probstrust/kernel/led.rs line 26
Quoted 5 lines · Markus Probst
rust/kernel/led.rs new file +5
-0+/// The led class device representation. +/// +/// This structure represents the Rust abstraction for a C `struct led_classdev`. +#[pin_data( PinnedDrop )] +pub struct LedClassDev {
Please just call this structure Device; drivers will refer to it as led::Device. This is for consistency with drm::Device, pwm::Device, etc.
rust/kernel/led.rs line 29
Quoted 3 lines
rust/kernel/led.rs new file +3
-0+ default_trigger: Option<CString>, + parent: Option<ARef<Device>>, + handler: KBox<dyn LedHandlerImpl>,
Those fields are already covered by the C struct led_classdev, there is no need to duplicate them.
Please have a look at how drm::Device [1] and pwm::Device [2] abstract things for reference. The general patterns for abstracting class devices should be very similar.
[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/rust/kernel/drm/device.rs
[2] https://lore.kernel.org/all/20250930-rust-next-pwm-working-fan-for-sending-v15-3-5661c3090877@samsung.com/
Re: [PATCH 3/4] rust: leds: add basic led classdev abstractions
In reply to Markus ProbstHi!
Quoted 10 lines
Implement the core abstractions needed for led class devices, including:
*
led::LedHandler- the trait for handling leds, includingbrightness_set*
led::LedInitData- data set for the led class device*
led::LedClassDev- a safe wrapper arroundled_classdevSigned-off-by Markus Probst <[email protected]>
My knowledge of Rust is not nearly good enough to understand this, but...
Pavel
Signature · 2 lines
I don't work for Nazis and criminals, and neither should you. Boycott Putin, Trump, Netanyahu and Musk!
Alice Ryhl
maintainerRe: [PATCH 2/4] rust: add pinned wrapper of Vec
In reply to Markus Probstrust/kernel/alloc/kvec.rs pub struct Vec<T, A: Allocator> { · line 116
Quoted 8 lines · Markus Probst
Implement a wrapper of Vec that guarantees that its content will never be moved, unless the item implements Unpin, allowing PinInit to be initialized on the Vec.
rust/kernel/alloc/kvec.rs +4
-0+/// A pinned wrapper of the [`Vec`] type. +/// +/// It is guaranteed that the contents will never be moved, unless T implements Unpin. +pub struct PinnedVec <T ,A :Allocator >(Vec <T ,A >);
Could we simply use Pin<Vec<T>> for this purpose? Why the new struct?
In fact, does the utility added by commit ac9eea3d08c2 ("rust: alloc:
implement Box::pin_slice()") not satisfy your needs?
Also, this thread seems mangled. Patches 2, 3, and 4 should all be replies to patch 1. Right now the thread is deeply nested.
Alice
Re: [PATCH 2/4] rust: add pinned wrapper of Vec
Quoted 6 lines · Alice Ryhl
Quoted 4 lines · Markus Probst
Implement a wrapper of Vec that guarantees that its content will never be
moved, unless the item implements Unpin, allowing PinInit to be initialized on the Vec.
Quoted 8 lines
Quoted 5 lines
rust/kernel/alloc/kvec.rs +3
-0+/// A pinned wrapper of the [`Vec`] type. +/// +/// It is guaranteed that the contents will never be moved, unless T implements Unpin.
+pub struct PinnedVec<T, A: Allocator>(Vec<T, A>);Could we simply use
Pin<Vec<T>>for this purpose? Why the new struct?
I suppose we could. I didn't take into consideration that Vec also implements Deref.
Quoted 3 lines
In fact, does the utility added by commit
ac9eea3d08c2("rust: alloc:
implementBox::pin_slice()") not satisfy your needs?
Its not as flexible as with this implementation, as items can't be dynamically pushed.
Quoted 5 lines
Also, this thread seems mangled. Patches 2, 3, and 4 should all be replies to patch 1. Right now the thread is deeply nested.
I will use --no-reply-chain in the future then with git-sendemail.
Quoted 760 lines
Alice
Quoted 757 lines
Quoted 757 lines
Quoted 757 lines
Quoted 757 lines
Quoted 757 lines
Quoted 757 lines
Quoted 757 lines
Quoted 757 lines
Quoted 757 lines
Quoted 757 lines
Quoted 757 lines
Quoted 757 lines
Alice Ryhl
maintainerRe: [PATCH 1/4] rust: i2c: add read and write byte data abstractions
In reply to Markus ProbstQuoted 44 lines · Markus Probst
In addition to the core abstractions, implement
*
i2c::I2cClient::read_byte- read a byte from the i2c client
*i2c::I2cClient::write_byte- write a byte to the i2c client*
i2c::I2cClient::read_byte_data- read byte data from the i2c client
*i2c::I2cClient::write_byte_data- write byte data to the i2c client*
i2c::I2cClient::read_word_data- read word data from the i2c client
*i2c::I2cClient::write_word_data- write word data to the i2c clientSigned-off-by Markus Probst <[email protected]>--- rust/kernel/i2c.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+)rust/kernel/i2c.rs +20
-0@@ -463,6 +463,51 @@ impl<Ctx: device::DeviceContext> I2cClient<Ctx> {fn as_raw (&self) -> *mut bindings::i2c_client { self.0 .get () } + +/// Reads a byte from the i2c client +pub fn read_byte (&self) ->Result <u8 > { +// SAFETY: By the type invariant of `Self`, `self.as_raw()` is a pointer to a valid +// `struct i2c_client`. +let value =unsafe { bindings::i2c_smbus_read_byte (self.as_raw ()) }; +to_result (value).map (|()| valueas u8 ) + } + +/// Writes a byte to the i2c client +pub fn write_byte (&self, value:u8 ) ->Result <()> { +// SAFETY: By the type invariant of `Self`, `self.as_raw()` is a pointer to a valid +// `struct i2c_client`. +to_result (unsafe { bindings::i2c_smbus_write_byte (self.as_raw (), value) }) + } + +/// Reads byte data from the i2c client +pub fn read_byte_data (&self, reg:u8 ) ->Result <u8 > { +// SAFETY: By the type invariant of `Self`, `self.as_raw()` is a pointer to a valid +// `struct i2c_client`.
rust/kernel/i2c.rs impl<Ctx: device::DeviceContext> I2cClient<Ctx> { · line 495
Quoted 10 lines
rust/kernel/i2c.rs +10
-0+let value =unsafe { bindings::i2c_smbus_read_byte_data (self.as_raw (), reg) }; +to_result (value).map (|()| valueas u8 ) + } + +/// Writes byte data to the i2c client +pub fn write_byte_data (&self, reg:u8 , value:u8 ) ->Result <()> { +// SAFETY: By the type invariant of `Self`, `self.as_raw()` is a pointer to a valid +// `struct i2c_client`. +to_result (unsafe { bindings::i2c_smbus_write_byte_data (self.as_raw (), reg, value) }) + }
I think it would be useful if the documentation for these methods has an explanation of the difference between write_byte and write_byte_data.
Alice
Danilo Krummrich
maintainerRe: [PATCH 1/4] rust: i2c: add read and write byte data abstractions
In reply to Markus ProbstQuoted 10 lines · Markus Probst
In addition to the core abstractions, implement
*
i2c::I2cClient::read_byte- read a byte from the i2c client
*i2c::I2cClient::write_byte- write a byte to the i2c client*
i2c::I2cClient::read_byte_data- read byte data from the i2c client
*i2c::I2cClient::write_byte_data- write byte data to the i2c client*
i2c::I2cClient::read_word_data- read word data from the i2c client
*i2c::I2cClient::write_word_data- write word data to the i2c client
I don't think those should be implemented to be used by drivers directly.
Instead we should generalize the Io type [1] and allow for arbitrary backends, such that we can use the register!() macro [2] to define register types for I2C devices.
Ideally, an I2C / SMBus backend should use regmap right away.
The register!() macro is currently moved into the generic infrastructure [3].
[1] https://rust.docs.kernel.org/kernel/io/struct.Io.html
[2] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/gpu/nova-core/regs/macros.rs
[3] https://lore.kernel.org/all/[email protected]/
Re: [PATCH 1/4] rust: i2c: add read and write byte data abstractions
In reply to Markus ProbstHi!
Quoted 5 lines
In addition to the core abstractions, implement
*
i2c::I2cClient::read_byte- read a byte from the i2c client
*i2c::I2cClient::write_byte- write a byte to the i2c client
Quoted 7 lines
*
i2c::I2cClient::read_byte_data- read byte data from the i2c client
*i2c::I2cClient::write_byte_data- write byte data to the i2c client*
i2c::I2cClient::read_word_data- read word data from the i2c client
*i2c::I2cClient::write_word_data- write word data to the i2c clientSigned-off-by Markus Probst <[email protected]>
Signature · 2 lines
I don't work for Nazis and criminals, and neither should you. Boycott Putin, Trump, Netanyahu and Musk!