From: Igor Korotin <igor.korotin.linux@gmail.com>
To: Daniel Almeida <daniel.almeida@collabora.com>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Wolfram Sang" <wsa+renesas@sang-engineering.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"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>,
"Danilo Krummrich" <dakr@kernel.org>,
"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
"Viresh Kumar" <viresh.kumar@linaro.org>,
"Asahi Lina" <lina+kernel@asahilina.net>,
"Wedson Almeida Filho" <wedsonaf@gmail.com>,
"Alex Hung" <alex.hung@amd.com>,
"Tamir Duberstein" <tamird@gmail.com>,
"Xiangfei Ding" <dingxiangfei2009@gmail.com>,
linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
linux-i2c@vger.kernel.org
Subject: Re: [PATCH v4 1/3] rust: i2c: add basic I2C device and driver abstractions
Date: Tue, 9 Sep 2025 18:19:03 +0100 [thread overview]
Message-ID: <655ca23c-1fe6-498a-80b8-1b75044d9db3@gmail.com> (raw)
In-Reply-To: <CB269793-D165-4D22-95E5-F978C1ECC79E@collabora.com>
Hi Daniel
On 8/27/2025 7:37 PM, Daniel Almeida wrote:
>> +
>> + let i2c_table = match T::I2C_ID_TABLE {
>> + Some(table) => table.as_ptr(),
>> + None => core::ptr::null(),
>> + };
>> +
>> + let of_table = match T::OF_ID_TABLE {
>> + Some(table) => table.as_ptr(),
>> + None => core::ptr::null(),
>> + };
>> +
>> + let acpi_table = match T::ACPI_ID_TABLE {
>> + Some(table) => table.as_ptr(),
>> + None => core::ptr::null(),
>> + };
>> +
>> + // SAFETY: It's safe to set the fields of `struct i2c_client` on initialization.
>> + unsafe {
>> + (*idrv.get()).driver.name = name.as_char_ptr();
>> + (*idrv.get()).probe = Some(Self::probe_callback);
>> + (*idrv.get()).remove = Some(Self::remove_callback);
>> + (*idrv.get()).shutdown = Some(Self::shutdown_callback);
>> + (*idrv.get()).id_table = i2c_table;
>> + (*idrv.get()).driver.of_match_table = of_table;
>> + (*idrv.get()).driver.acpi_match_table = acpi_table;
>> + }
>> +
>> + // SAFETY: `idrv` is guaranteed to be a valid `RegType`.
>> + to_result(unsafe { bindings::i2c_register_driver(module.0, idrv.get()) })
>> + }
>> +
>> + unsafe fn unregister(idrv: &Opaque<Self::RegType>) {
>> + // SAFETY: `idrv` is guaranteed to be a valid `RegType`.
>> + unsafe { bindings::i2c_del_driver(idrv.get()) }
>> + }
>> +}
>> +
>> +impl<T: Driver + 'static> Adapter<T> {
>> + extern "C" fn probe_callback(idev: *mut bindings::i2c_client) -> kernel::ffi::c_int {
>> + // SAFETY: The I2C bus only ever calls the probe callback with a valid pointer to a
>> + // `struct i2c_client`.
>> + //
>> + // INVARIANT: `idev` is valid for the duration of `probe_callback()`.
>> + let idev = unsafe { &*idev.cast::<I2cClient<device::CoreInternal>>() };
>> +
>> + let info =
>> + Self::i2c_id_info(idev).or_else(|| <Self as driver::Adapter>::id_info(idev.as_ref()));
>
> I wonder if these should be private member functions?
>
>> +
>> + from_result(|| {
>> + let data = T::probe(idev, info)?;
>> +
>> + idev.as_ref().set_drvdata(data);
>> + Ok(0)
>> + })
>> + }
>> +
>> + extern "C" fn remove_callback(idev: *mut bindings::i2c_client) {
>> + // SAFETY: `idev` is a valid pointer to a `struct i2c_client`.
>> + let idev = unsafe { &*idev.cast::<I2cClient<device::CoreInternal>>() };
>
>> +
>> + // SAFETY: `remove_callback` is only ever called after a successful call to
>> + // `probe_callback`, hence it's guaranteed that `I2cClient::set_drvdata()` has been called
>> + // and stored a `Pin<KBox<T>>`.
>> + drop(unsafe { idev.as_ref().drvdata_obtain::<Pin<KBox<T>>>() });
>> + }
>> +
>> + extern "C" fn shutdown_callback(idev: *mut bindings::i2c_client) {
>> + // SAFETY: `shutdown_callback` is only ever called for a valid `idev`
>> + let idev = unsafe { &*idev.cast::<I2cClient<device::Core>>() };
>> +
>> + T::shutdown(idev);
>> + }
>> +
>> + /// The [`i2c::IdTable`] of the corresponding driver.
>> + fn i2c_id_table() -> Option<IdTable<<Self as driver::Adapter>::IdInfo>> {
>> + T::I2C_ID_TABLE
>> + }
>> +
>> + /// Returns the driver's private data from the matching entry in the [`i2c::IdTable`], if any.
>> + ///
>> + /// If this returns `None`, it means there is no match with an entry in the [`i2c::IdTable`].
>> + fn i2c_id_info(dev: &I2cClient) -> Option<&'static <Self as driver::Adapter>::IdInfo> {
>
> Again, perhaps a private member function? I’m trying to simplify the syntax here.
Can you, please, kindly clarify what do you mean? If a function is not
pub/pub(crate),
it is a private function.
Thanks,
Igor
next prev parent reply other threads:[~2025-09-09 17:19 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-20 15:14 [PATCH v4 0/3] rust: i2c: Add basic I2C driver abstractions Igor Korotin
2025-08-20 15:19 ` [PATCH v4 1/3] rust: i2c: add basic I2C device and " Igor Korotin
2025-08-27 18:37 ` Daniel Almeida
2025-09-09 17:19 ` Igor Korotin [this message]
2025-09-09 17:53 ` Daniel Almeida
2025-09-09 18:19 ` Danilo Krummrich
2025-09-09 18:22 ` Daniel Almeida
2025-08-20 15:21 ` [PATCH v4 2/3] rust: i2c: add manual I2C device creation abstractions Igor Korotin
2025-08-27 19:23 ` Daniel Almeida
2025-08-20 15:23 ` [PATCH v4 3/3] samples: rust: add Rust I2C sample driver Igor Korotin
2025-08-27 19:38 ` Daniel Almeida
2025-09-09 17:55 ` Igor Korotin
2025-09-09 18:34 ` Danilo Krummrich
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=655ca23c-1fe6-498a-80b8-1b75044d9db3@gmail.com \
--to=igor.korotin.linux@gmail.com \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=alex.hung@amd.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=dingxiangfei2009@gmail.com \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=lina+kernel@asahilina.net \
--cc=linux-i2c@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@gmail.com \
--cc=tmgross@umich.edu \
--cc=viresh.kumar@linaro.org \
--cc=wedsonaf@gmail.com \
--cc=wsa+renesas@sang-engineering.com \
/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;
as well as URLs for NNTP newsgroup(s).