[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <aOecnHYkBYEbUsi4@google.com>
Date: Thu, 9 Oct 2025 11:29:32 +0000
From: Alice Ryhl <aliceryhl@...gle.com>
To: Markus Probst <markus.probst@...teo.de>
Cc: Lee Jones <lee@...nel.org>, Pavel Machek <pavel@...nel.org>, Danilo Krummrich <dakr@...nel.org>,
Miguel Ojeda <ojeda@...nel.org>, Alex Gaynor <alex.gaynor@...il.com>,
Igor Korotin <igor.korotin.linux@...il.com>, Lorenzo Stoakes <lorenzo.stoakes@...cle.com>,
Vlastimil Babka <vbabka@...e.cz>, "Liam R. Howlett" <Liam.Howlett@...cle.com>,
Uladzislau Rezki <urezki@...il.com>, Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>,
bjorn3_gh@...tonmail.com, Benno Lossin <lossin@...nel.org>,
Andreas Hindborg <a.hindborg@...nel.org>, Trevor Gross <tmgross@...ch.edu>,
Daniel Almeida <daniel.almeida@...labora.com>, linux-leds@...r.kernel.org,
rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org
Subject: Re: [PATCH 1/4] rust: i2c: add read and write byte data abstractions
On Wed, Oct 08, 2025 at 06:10:43PM +0000, Markus Probst wrote:
> 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
>
> Signed-off-by: Markus Probst <markus.probst@...teo.de>
> ---
> rust/kernel/i2c.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 45 insertions(+)
>
> diff --git a/rust/kernel/i2c.rs b/rust/kernel/i2c.rs
> index 73858aecc131..57b02b65f90f 100644
> --- a/rust/kernel/i2c.rs
> +++ b/rust/kernel/i2c.rs
> @@ -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(|()| value as 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(|()| value as 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
Powered by blists - more mailing lists