lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20251008181027.662616-2-markus.probst@posteo.de>
Date: Wed, 08 Oct 2025 18:10:43 +0000
From: Markus Probst <markus.probst@...teo.de>
To: 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>
Cc: Markus Probst <markus.probst@...teo.de>,
	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>,
	Alice Ryhl <aliceryhl@...gle.com>,
	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: [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

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) })
+    }
+
+    /// 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(|()| value as 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
-- 
2.49.1


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ