rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Danilo Krummrich" <dakr@kernel.org>
To: "Daniel Almeida" <daniel.almeida@collabora.com>
Cc: "Igor Korotin" <igor.korotin.linux@gmail.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.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>,
	rust-for-linux@vger.kernel.org
Subject: Re: [PATCH v3 2/3] rust: i2c: add manual I2C device creation abstractions
Date: Sat, 02 Aug 2025 02:26:42 +0200	[thread overview]
Message-ID: <DBRIKK3NT7EK.IIK91AWAOHDP@kernel.org> (raw)
In-Reply-To: <A5DDD914-C07D-41C6-83C4-5ED9730D8A5B@collabora.com>

On Fri Aug 1, 2025 at 7:59 PM CEST, Daniel Almeida wrote:
>> +/// The i2c adapter reference
>> +///
>> +/// This represents the Rust abstraction for a reference to an existing
>> +/// C `struct i2c_adapter`.
>> +///
>> +/// # Invariants
>> +///
>> +/// A [`I2cAdapterRef`] instance represents a valid `struct i2c_adapter` created by the C portion of
>> +/// the kernel.
>> +#[repr(transparent)]
>> +pub struct I2cAdapterRef(NonNull<bindings::i2c_adapter>);
>
> Is there a plan for an owned version? Otherwise I’d personally drop the
> “Ref” nomenclature.
>
>> +
>> +impl I2cAdapterRef {
>> +    fn as_raw(&self) -> *mut bindings::i2c_adapter {
>> +        self.0.as_ptr()
>> +    }
>> +
>> +    /// Gets pointer to an `i2c_adapter` by index.
>> +    pub fn get(index: i32) -> Option<Self> {
>> +        // SAFETY: `index` must refer to a valid I2C adapter; the kernel
>> +        // guarantees that `i2c_get_adapter(index)` returns either a valid
>> +        // pointer or NULL. `NonNull::new` guarantees the correct check.
>> +        let adapter = NonNull::new(unsafe { bindings::i2c_get_adapter(index) })?;
>> +        Some(Self(adapter))
>> +    }
>> +}
>> +
>> +impl Drop for I2cAdapterRef {
>> +    fn drop(&mut self) {
>> +        // SAFETY: This `I2cAdapterRef` was obtained from `i2c_get_adapter`,
>> +        // and calling `i2c_put_adapter` exactly once will correctly release
>> +        // the reference count in the I2C core. It is safe to call from any context
>> +        unsafe { bindings::i2c_put_adapter(self.as_raw()) }
>
> Should this implement AlwasyRefCounted?

I don't think it's necessary given the context this is used in, but it's also
not much more code to do so, and ARef<I2cAdapter> turns out much cleaner I
think.

So, yes I agree this should implement AlwasyRefCounted.

>> +    }
>> +}
>> +
>> +/// The i2c board info representation
>> +///
>> +/// This structure represents the Rust abstraction for a C `struct i2c_board_info` structure,
>> +/// which is used for manual I2C client creation.
>> +#[repr(transparent)]
>> +pub struct I2cBoardInfo(bindings::i2c_board_info);
>> +
>> +impl I2cBoardInfo {
>> +    const I2C_TYPE_SIZE: usize = 20;
>> +    /// Create a new board‐info for a kernel driver.
>> +    #[inline(always)]
>> +    pub const fn new(type_: &'static CStr, addr: u16) -> Self {
>> +        build_assert!(
>> +            type_.len_with_nul() <= Self::I2C_TYPE_SIZE,
>> +            "Type exceeds 20 bytes"
>> +        );
>> +        let src = type_.as_bytes_with_nul();
>> +        // Replace with `bindings::acpi_device_id::default()` once stabilized for `const`.
>> +        // SAFETY: FFI type is valid to be zero-initialized.
>> +        let mut i2c_board_info: bindings::i2c_board_info = unsafe { core::mem::zeroed() };
>> +        let mut i: usize = 0;
>> +        while i < src.len() {
>> +            i2c_board_info.type_[i] = src[i];
>> +            i += 1;
>> +        }
>
> Same comment as the last patch.

Still not possible unfortunately. :(

  reply	other threads:[~2025-08-02  0:26 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-01 15:37 [PATCH v3 0/3] rust: i2c: Add basic I2C driver abstractions Igor Korotin
2025-08-01 15:40 ` [PATCH v3 1/3] rust: i2c: add basic I2C device and " Igor Korotin
2025-08-01 17:14   ` Daniel Almeida
2025-08-02  0:22     ` Danilo Krummrich
2025-08-04 14:16     ` Igor Korotin
2025-08-02  0:00   ` Danilo Krummrich
2025-08-02  9:07     ` Wolfram Sang
2025-08-02 10:51       ` Danilo Krummrich
2025-08-02 12:14         ` Wolfram Sang
2025-08-02 12:41           ` Danilo Krummrich
2025-08-04 14:58     ` Igor Korotin
2025-08-04 15:17       ` Danilo Krummrich
2025-08-04 15:24         ` Miguel Ojeda
2025-08-04 15:40         ` Igor Korotin
2025-08-04 16:11           ` Wolfram Sang
2025-08-04 17:15             ` Danilo Krummrich
2025-08-04 22:01               ` Wolfram Sang
2025-08-05  8:37                 ` Igor Korotin
2025-08-05  9:28                   ` Wolfram Sang
2025-08-05 12:40                 ` Daniel Almeida
2025-08-04 17:26             ` Igor Korotin
2025-08-04 17:46               ` Miguel Ojeda
2025-08-04 21:57               ` Wolfram Sang
2025-08-15 15:40     ` Igor Korotin
2025-08-15 16:03       ` Danilo Krummrich
2025-08-15 17:04         ` Greg KH
2025-08-15 17:16           ` Danilo Krummrich
2025-08-01 15:44 ` [PATCH v3 2/3] rust: i2c: add manual I2C device creation abstractions Igor Korotin
2025-08-01 17:59   ` Daniel Almeida
2025-08-02  0:26     ` Danilo Krummrich [this message]
2025-08-04 14:38     ` Igor Korotin
2025-08-02  0:12   ` Danilo Krummrich
2025-08-01 15:45 ` [PATCH v3 3/3] samples: rust: add Rust I2C sample driver Igor Korotin
2025-08-01 18:09   ` Daniel Almeida
2025-08-04 14:43     ` Igor Korotin
2025-08-04 14:58       ` Daniel Almeida
2025-08-02  0:18   ` Danilo Krummrich
2025-08-07  8:42   ` kernel test robot
2025-08-04 15:07 ` [PATCH v3 0/3] rust: i2c: Add basic I2C driver abstractions Daniel Almeida

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=DBRIKK3NT7EK.IIK91AWAOHDP@kernel.org \
    --to=dakr@kernel.org \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=daniel.almeida@collabora.com \
    --cc=gary@garyguo.net \
    --cc=igor.korotin.linux@gmail.com \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    /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).