Re: [PATCH v7 2/4] rust: i2c: add manual I2C device creation abstractions

From: Danilo Krummrich
Date: Tue Nov 11 2025 - 03:17:18 EST


On Mon Nov 10, 2025 at 10:30 PM AEDT, Igor Korotin wrote:
> +impl I2cAdapter {
> + /// Returns the I2C Adapter index.
> + #[inline]
> + pub fn get_nr(&self) -> i32 {
> + // SAFETY: `self.as_raw` is a valid pointer to a `struct i2c_adapter`.
> + unsafe { (*self.as_raw()).nr }
> + }

Missing empty line. Also, please try to avoid the "get" prefix for functions /
methods that do not increase a reference count. What about just index()?

> + /// Gets pointer to an `i2c_adapter` by index.
> + pub fn get(index: i32) -> Result<ARef<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) }).ok_or(ENODEV)?;
> +
> + // SAFETY: `adapter` is non-null and points to a live `i2c_adapter`.
> + // `I2cAdapter` is #[repr(transparent)], so this cast is valid.
> + Ok(unsafe { (&*adapter.as_ptr().cast::<I2cAdapter<device::Normal>>()).into() })
> + }
> +}
> +
> +// SAFETY: `I2cAdapter` is a transparent wrapper of a type that doesn't depend on `I2cAdapter`'s generic
> +// argument.
> +kernel::impl_device_context_deref!(unsafe { I2cAdapter });
> +kernel::impl_device_context_into_aref!(I2cAdapter);
> +
> +// SAFETY: Instances of `I2cAdapter` are always reference-counted.
> +unsafe impl crate::types::AlwaysRefCounted for I2cAdapter {
> + fn inc_ref(&self) {
> + // SAFETY: The existence of a shared reference guarantees that the refcount is non-zero.
> + unsafe { bindings::i2c_get_adapter(self.get_nr()) };
> + }
> +
> + unsafe fn dec_ref(obj: NonNull<Self>) {
> + // SAFETY: The safety requirements guarantee that the refcount is non-zero.
> + unsafe { bindings::i2c_put_adapter(obj.as_ref().as_raw()) }
> + }
> +}
> +
> +/// 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 [`I2cBoardInfo`] 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() };

pin_init::zeroed()

> + let mut i: usize = 0;
> + while i < src.len() {
> + i2c_board_info.type_[i] = src[i];
> + i += 1;
> + }
> +
> + i2c_board_info.addr = addr;
> + Self(i2c_board_info)
> + }
> +
> + fn as_raw(&self) -> *const bindings::i2c_board_info {
> + from_ref(&self.0)
> + }
> +}