Re: [RFC PATCH v2 1/4] i2c: rust: add smbus_read_byte_data and smbus_read_word_data

Igor Korotin
2026-05-04T10:29:24Z

Hello Muchamad

On 4/29/2026 2:22 PM, Muchamad Coirul Anwar wrote:
> Signed-off-by: Muchamad Coirul Anwar <muchamadcoirulanwar@gmail.com>
> ---
> rust/kernel/i2c.rs | 24 ++++++++++++++++++++++++
> 1 file changed, 24 insertions(+)
>
> diff --git a/rust/kernel/i2c.rs b/rust/kernel/i2c.rs
> index 7b908f0c5a58..6eaea1158fda 100644
> --- a/rust/kernel/i2c.rs
> +++ b/rust/kernel/i2c.rs
> @@ -477,6 +477,30 @@ impl<Ctx: device::DeviceContext> I2cClient<Ctx> {
> fn as_raw(&self) -> *mut bindings::i2c_client {
> self.0.get()
> }
> +
> + /// Reads a single byte from a register via SMBus.
> + pub fn smbus_read_byte_data(&self, reg: u8) -> Result<u8> {
> + // SAFETY: `self.as_raw()` is a valid pointer to a `struct i2c_client`
> + // by the type invariant of `I2cClient`.
> + let ret = unsafe { bindings::i2c_smbus_read_byte_data(self.as_raw(), reg) };
> + if ret < 0 {
> + Err(Error::from_errno(ret))
> + } else {
> + Ok(ret as u8)
> + }
> + }
> +
> + /// Reads a 16-bit word from a register via SMBus.
> + pub fn smbus_read_word_data(&self, reg: u8) -> Result<u16> {
> + // SAFETY: `self.as_raw()` is a valid pointer to a `struct i2c_client`
> + // by the type invariant of `I2cClient`.
> + let ret = unsafe { bindings::i2c_smbus_read_word_data(self.as_raw(), reg) };
> + if ret < 0 {
> + Err(Error::from_errno(ret))
> + } else {
> + Ok(ret as u16)
> + }
> + }
> }
>
> // SAFETY: `I2cClient` is a transparent wrapper of `struct i2c_client`.

Thanks for the patch. However, we've previously agreed [1] that
I2cClient should implement the IO trait [2] rather than adding
standalone methods like these. This patch would need to be reworked in
that direction.

If you'd like to take that on, feel free to submit a patch series
implementing the IO trait instead.

Cheers
Igor

[1]
https://lore.kernel.org/rust-for-linux/20260131-i2c-adapter-v1-4-5a436e34cd1a@gmail.com/

[2]
https://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git/commit/?h=driver-core-testing&id=121d87b28e1d9061d3aaa156c43a627d3cb5e620