Rust for Linux List
 help / color / mirror / Atom feed
From: Muchamad Coirul Anwar <muchamadcoirulanwar@gmail.com>
To: Jonathan Cameron <jic23@kernel.org>
Cc: linux-iio@vger.kernel.org, rust-for-linux@vger.kernel.org,
	linux-kernel@vger.kernel.org, Miguel Ojeda <ojeda@kernel.org>,
	Igor Korotin <igor.korotin.linux@gmail.com>,
	Brandon Saint-John <branstj@gmail.com>,
	Muchamad Coirul Anwar <muchamadcoirulanwar@gmail.com>
Subject: [RFC PATCH v3 1/4] i2c: rust: implement kernel::io::Io trait for I2cClient
Date: Sun, 24 May 2026 20:28:20 +0700	[thread overview]
Message-ID: <20260524132824.54918-2-muchamadcoirulanwar@gmail.com> (raw)
In-Reply-To: <20260524132824.54918-1-muchamadcoirulanwar@gmail.com>

Implement the Io trait for I2cClient per the agreed-upon direction
for I2C register access abstractions. This provides try_read8() and
try_read16() with automatic offset validation via io_addr().

I2cClient now implements IoCapable<u8> and IoCapable<u16> with
maxsize=256 (SMBus command byte range 0x00-0xFF).

Link: https://lore.kernel.org/rust-for-linux/20260131-i2c-adapter-v1-4-5a436e34cd1a@gmail.com/
Signed-off-by: Muchamad Coirul Anwar <muchamadcoirulanwar@gmail.com>
---
 rust/kernel/i2c.rs | 76 +++++++++++++++++++++++++++++++---------------
 1 file changed, 51 insertions(+), 25 deletions(-)

diff --git a/rust/kernel/i2c.rs b/rust/kernel/i2c.rs
index 6eaea1158fda..cdbef6cfa344 100644
--- a/rust/kernel/i2c.rs
+++ b/rust/kernel/i2c.rs
@@ -14,6 +14,7 @@
     devres::Devres,
     driver,
     error::*,
+    io::{Io, IoCapable},
     of,
     prelude::*,
     sync::aref::{
@@ -477,30 +478,6 @@ 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`.
@@ -614,5 +591,54 @@ fn drop(&mut self) {
 unsafe impl Send for Registration {}
 
 // SAFETY: `Registration` offers no interior mutability (no mutation through &self
-// and no mutable access is exposed)
+// and no mutable access is exposed).
 unsafe impl Sync for Registration {}
+
+impl<Ctx: device::DeviceContext> IoCapable<u8> for I2cClient<Ctx> {}
+impl<Ctx: device::DeviceContext> IoCapable<u16> for I2cClient<Ctx> {}
+
+impl<Ctx: device::DeviceContext> Io for I2cClient<Ctx> {
+    #[inline]
+    fn addr(&self) -> usize {
+        0
+    }
+
+    #[inline]
+    fn maxsize(&self) -> usize {
+        256
+    }
+
+    #[inline]
+    fn try_read8(&self, offset: usize) -> Result<u8>
+    where
+        Self: IoCapable<u8>,
+    {
+        let reg = self.io_addr::<u8>(offset)? as u8;
+        // SAFETY: `self.as_raw()` returns a valid pointer to a `struct i2c_client`
+        // as guaranteed by the type invariant of `I2cClient`. `reg` is bounds-checked
+        // by `io_addr()` above (offset + 1 <= 256).
+        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)
+        }
+    }
+
+    #[inline]
+    fn try_read16(&self, offset: usize) -> Result<u16>
+    where
+        Self: IoCapable<u16>,
+    {
+        let reg = self.io_addr::<u16>(offset)? as u8;
+        // SAFETY: `self.as_raw()` returns a valid pointer to a `struct i2c_client`
+        // as guaranteed by the type invariant of `I2cClient`. `reg` is bounds-checked
+        // by `io_addr()` above (offset + 2 <= 256).
+        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)
+        }
+    }
+}
-- 
2.50.0


  reply	other threads:[~2026-05-24 13:28 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-24 13:28 [RFC PATCH v3 0/4] iio: position: add Rust driver for ams AS5600 Muchamad Coirul Anwar
2026-05-24 13:28 ` Muchamad Coirul Anwar [this message]
2026-05-28 15:25   ` [RFC PATCH v3 1/4] i2c: rust: implement kernel::io::Io trait for I2cClient Jonathan Cameron
2026-06-01  7:58     ` Muchamad Coirul Anwar
2026-06-01  9:05       ` Jonathan Cameron
2026-06-02  8:11         ` Muchamad Coirul Anwar
2026-06-02 11:59           ` Jonathan Cameron
2026-05-24 13:28 ` [RFC PATCH v3 2/4] rust: add minimal IIO subsystem abstractions Muchamad Coirul Anwar
2026-05-28 16:09   ` Jonathan Cameron
2026-06-01  8:30     ` Muchamad Coirul Anwar
2026-06-01  9:10       ` Jonathan Cameron
2026-05-24 13:28 ` [RFC PATCH v3 3/4] iio: position: add Rust driver for ams AS5600 Muchamad Coirul Anwar
2026-05-28 16:08   ` Jonathan Cameron
2026-06-01 11:11     ` Muchamad Coirul Anwar
2026-06-02 12:06       ` Jonathan Cameron
2026-06-03  8:52         ` Muchamad Coirul Anwar
2026-05-29  5:37   ` Brandon Saint-John
2026-06-01 11:33     ` Muchamad Coirul Anwar
2026-05-24 13:28 ` [RFC PATCH v3 4/4] iio: position: as5600: add Kconfig and Makefile entries Muchamad Coirul Anwar
2026-05-28 16:09   ` Jonathan Cameron
2026-06-01  8:00     ` Muchamad Coirul Anwar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260524132824.54918-2-muchamadcoirulanwar@gmail.com \
    --to=muchamadcoirulanwar@gmail.com \
    --cc=branstj@gmail.com \
    --cc=igor.korotin.linux@gmail.com \
    --cc=jic23@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox