mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andrew Davis <afd@ti.com>
To: "Igor Korotin" <igor.korotin@linux.dev>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Onur Özkan" <work@onurozkan.dev>
Cc: <rust-for-linux@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	"Andrew Davis" <afd@ti.com>
Subject: [RFC PATCH] rust: i2c: Remove use of bindings::i2c_match_id
Date: Wed, 11 Nov 2026 14:52:04 -0600	[thread overview]
Message-ID: <20261111205204.481923-1-afd@ti.com> (raw)

The C function i2c_match_id() is slated for removal, remove use of this
from Rust I2C by directly finding the matching ID for I2C clients.

Signed-off-by: Andrew Davis <afd@ti.com>
---

Hello all,

Looking for some help on this one. I'm working on removing i2c_match_id()
from the kernel for various reasons, and ran into the issue of it being
used by Rust through a binding.

Wanted to try my hand at some Rust code and so tried to recreate this
simple ~7 line C function in Rust. Didn't go so well, tried writing it
by hand and got something that built, but a quick AI code review told me
every single line I wrote was wrong :D After some more fixing this is what
I ended up with. Seems to be correct but having a hard time believing this
mess I came up with is really how Rust should be written. So any suggestions
or hints on a better way to do this would be very welcome!

Thanks,
Andrew

 rust/kernel/i2c.rs | 33 ++++++++++++++++++++++++++++-----
 1 file changed, 28 insertions(+), 5 deletions(-)

diff --git a/rust/kernel/i2c.rs b/rust/kernel/i2c.rs
index b4ff25c830fc0..acf96887253c8 100644
--- a/rust/kernel/i2c.rs
+++ b/rust/kernel/i2c.rs
@@ -197,12 +197,35 @@ fn i2c_id_info(dev: &I2cClient) -> Option<&'static <Self as driver::Adapter>::Id
         let table = Self::i2c_id_table()?;
 
         // SAFETY:
-        // - `table` has static lifetime, hence it's valid for reads
         // - `dev` is guaranteed to be valid while it's alive, and so is `dev.as_raw()`.
-        let raw_id = unsafe { bindings::i2c_match_id(table.as_ptr(), dev.as_raw()) };
-
-        if raw_id.is_null() {
-            return None;
+        // - `name` is filled with `strscpy()` when the client is created, so it is NUL-terminated,
+        //    and it is not modified for the lifetime of the client.
+        let client_name = unsafe { CStr::from_char_ptr((*dev.as_raw()).name.as_ptr()) };
+
+        // Get the raw pointer to the start of the C array
+        // It is never null as it points at the start of a static `IdArray`.
+        let mut raw_id = table.as_ptr();
+
+        // Iterate until hitting the sentinel entry or matching the client name
+        loop {
+            // SAFETY: `raw_id` points to an entry of `table`, which is terminated by a zeroed
+            // sentinel entry. We never advance past the sentinel (see below), so `raw_id` is
+            // valid for reads. `name` is NUL-terminated for every entry, including the
+            // sentinel, which is all zeroes.
+            let id_name = unsafe { CStr::from_char_ptr((*raw_id).name.as_ptr()) };
+
+            // An empty name is the sentinel: no match.
+            if id_name.is_empty() {
+                return None;
+            }
+
+            if id_name == client_name {
+                break;
+            }
+
+            // SAFETY: the current entry is not the sentinel, so the next entry is still
+            // within `table`.
+            raw_id = unsafe { raw_id.add(1) };
         }
 
         // SAFETY: `DeviceId` is a `#[repr(transparent)` wrapper of `struct i2c_device_id` and
-- 
2.43.0


                 reply	other threads:[~2026-11-11 20:53 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261111205204.481923-1-afd@ti.com \
    --to=afd@ti.com \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=gary@garyguo.net \
    --cc=igor.korotin@linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=work@onurozkan.dev \
    /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

all inboxes | Powered by JetHome®