Re: [PATCH v7 1/4] rust: i2c: add basic I2C device and driver abstractions
From: "Danilo Krummrich" <dakr@kernel.org>
Date: 2025-11-11 08:10:25
Also in:
linux-i2c, lkml
On Mon Nov 10, 2025 at 10:25 PM AEDT, Igor Korotin wrote:
quoted hunk ↗ jump to hunk
diff --git a/rust/kernel/i2c.rs b/rust/kernel/i2c.rs new file mode 100644 index 000000000000..41ef7c65c555 --- /dev/null +++ b/rust/kernel/i2c.rs@@ -0,0 +1,425 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! I2C Driver subsystem + +// I2C Driver abstractions. +use crate::{ + acpi, container_of, device, + device_id::{RawDeviceId, RawDeviceIdIndex}, + driver, + error::*, + of, + prelude::*, + types::{AlwaysRefCounted, Opaque}, +}; + +use core::{marker::PhantomData, ptr::NonNull};
Please use kernel "vertical style" [1]. [1] https://docs.kernel.org/rust/coding-guidelines.html#imports Please also run ./scripts/checkpatch.pl on all patches, there are a few warnings to address.
+
+/// An I2C device id table.
+#[repr(transparent)]
+#[derive(Clone, Copy)]
+pub struct DeviceId(bindings::i2c_device_id);
+
+impl DeviceId {
+ const I2C_NAME_SIZE: usize = 20;
+
+ /// Create a new device id from an I2C 'id' string.
+ #[inline(always)]
+ pub const fn new(id: &'static CStr) -> Self {
+ build_assert!(
+ id.len_with_nul() <= Self::I2C_NAME_SIZE,
+ "ID exceeds 20 bytes"
+ );
+ let src = id.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: bindings::i2c_device_id = unsafe { core::mem::zeroed() };You can use pin_init::zeroed() for this.
+ let mut i = 0;
+ while i < src.len() {
+ i2c.name[i] = src[i];
+ i += 1;
+ }
+
+ Self(i2c)
+ }
+}