From nobody Wed Oct 8 14:18:09 2025 Received: from mail-wr1-f50.google.com (mail-wr1-f50.google.com [209.85.221.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 353E94A33; Thu, 26 Jun 2025 17:51:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750960277; cv=none; b=DjFngbowEutLddRuclnNF5ACS4U3taB8EChb/z1E4QquE3uRpl/FZWJ29QKjZ+lfc2xDHu6l12MeezFWR7D6Sl/0zAynxblb5sK5/jx2uZZZeon75rT63uPzcP1iHxzg3ZTZtw5FaHYktFIa2ilM0bJo+Hvp0B44VcxfPrVzyQc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750960277; c=relaxed/simple; bh=m+5bxqvtqiUl+MzKFWjIbbXmZnyTdE8VBXhAm+HV5rE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=saL0/a5S4GzW0kIytpxpgyfY5f/gWQ4F5HXB6VJZTP3Mv5/QQxAwfVwtagYhM6N1tymziBYPvycLRls7boWVi3SmoBtqeuCLCOCxHNNG+wvROkfClwJCELqAYc2XeurbfNCrn214LlsAB3LBi+XPApTh2SDjlNRxZDGb+GD/5Ic= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=eR1eDAeq; arc=none smtp.client-ip=209.85.221.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="eR1eDAeq" Received: by mail-wr1-f50.google.com with SMTP id ffacd0b85a97d-3a54700a46eso676198f8f.1; Thu, 26 Jun 2025 10:51:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1750960273; x=1751565073; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=yubyZEl+6Y9yV3wuoYfKBENEcakJXXUBssVHsQ4xmUI=; b=eR1eDAeqdDWpF0XTBKpmMo+KjQTJk+yBs682o6LM9gBqRz3IjhtnOP5W4duitbFDso U4pLUdPc5BaAYjxpsa2hOQQL5NbjZkooV+7/yDbSWPDQ1+y3Tba8xWLHqKyxFSOMGPFh scpbiIPBNvBZK9rbA8tcCa9UAd9S6V4hYiSqcoAsAjaQY0vLFnQu5kpWEWdasOAFDHZB 0mTTTjhPIHV9ClF4mBHYh4syVz9igfb235hcE5wNl6B9lvBt6+utbsnsdYsFFTJfdu9y f4atgE6dx5S51+Rv7ylbytbSlDK1dCzfRMnI323FG2THyjuY/UjVQ8POKGMGDtLYurTe YcjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750960273; x=1751565073; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=yubyZEl+6Y9yV3wuoYfKBENEcakJXXUBssVHsQ4xmUI=; b=OV8X+2HxkQG7RNKvLRlN5oNyvz9XYFQ7k1V8bgPNjDbKvDX+Q+naoWiTCGwp6qCy6q FOIqocvE5S8JC0kEj1jwt9NBqtFPKCwaeo5NFn74JLQwWaROasYVJmh+po8PDIsXnAcN 8Mnr2G9his1B7SzVkOUiaG4RHxjALBPWUop1gB8xUEIMGc5r5/BvT5afELZPAbLIRnS2 4/AExD0fqWM2dkAzmyDGP4AzJ4xgV9213GbvddBPXl2LLbrGKGduEHjlwgxerf4R8qDo odY+o8baI5kZ1gB8bWQZqB20fynMdfB6+kLuukYbJhL5mAXryMNhAep7GqzklqI1oOko ZoUA== X-Forwarded-Encrypted: i=1; AJvYcCUDx3HicbvhX6su9jkQYGyvzMIQqdxtu5Oblt7rUcuneYAO7/k10fVAS6S9zb+mxVmyXdZ6GfbX2KKxM1Lf@vger.kernel.org, AJvYcCVTUn0yb5zv4oJjHZ7RnKEMjlzWM5oOE+F4Y5MExNJOrA5FT3enL1gMbL96xgiQDEenxp9voZpOZL0=@vger.kernel.org, AJvYcCXQFqHUSz4Ax2wSmfppnYKn2GebQgEYiNpEyniG7Vlxuk4U6juwPRxGI18ce7RU13Qu9SBsuidVF+ef/2P+eO8=@vger.kernel.org X-Gm-Message-State: AOJu0YziC4caHCi2tW6hRx5kI3AiYbuywBidy1oI2ev7fLnrbmTLXtSJ F7W3ImK2WefIBc0izog9teGtXrbjPOmSmnZND112yUAWX/8SXOaTtfZF X-Gm-Gg: ASbGncuA5/gfLeI/xN7mCQqlLmfHFvA3Nkp3rEToNWgvvLIvTMOnJAWGG8fU7/W7xRQ 6kFdhVJPv4YwRRkh1l2hQ7Ee9LC8yqmTxRi0iC9V1u3agjNXN7Azqgzwiu94HCy6B2ZwWDzQJSE tD+LXMji31a7HNmsZoL/C5nYdINUjoqAKv4xrR0msSWihOZAIB3KUQDzbsXfE/z1roCEr23jnUX NL+c8ipDVbRA+43+wFw+RiCFalfIMfqC4A+h5Ei7LUF877h4qpKjEvcsqazN9kE77SOGxB4u3w9 UpXfmMCeOMopX/JNvFM2blr5Mjk7CpaYGdxcO509TxemcdCMkdx6hT08r4iOlMiNOzObsMeNLAO psBPsMJ+G9ToBDO/q4SbwlyFgU9YL2zoikiQQcOU= X-Google-Smtp-Source: AGHT+IEIqXyptV5h+15bPfY6BmHXuM5kuOXLpvYhm1Y1Adfg4eZIpMuZD6JZBEpJ/O6aFYrHvy21WA== X-Received: by 2002:a5d:5f49:0:b0:3a5:5270:a52c with SMTP id ffacd0b85a97d-3a8f302a206mr455969f8f.0.1750960273111; Thu, 26 Jun 2025 10:51:13 -0700 (PDT) Received: from igor-korotin-Precision-Tower-3620.airspan.com ([188.39.32.4]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3a892e52a26sm485966f8f.51.2025.06.26.10.51.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Jun 2025 10:51:12 -0700 (PDT) Sender: Igor Korotin From: Igor Korotin To: Miguel Ojeda , Alex Gaynor , Wolfram Sang Cc: Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Greg Kroah-Hartman , Viresh Kumar , Asahi Lina , Wedson Almeida Filho , Alex Hung , Tamir Duberstein , Xiangfei Ding , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-i2c@vger.kernel.org Subject: [PATCH v1 1/4] rust: i2c: add basic I2C device and driver abstractions Date: Thu, 26 Jun 2025 18:49:10 +0100 Message-ID: <20250626174910.905816-1-igor.korotin.linux@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250626174623.904917-1-igor.korotin.linux@gmail.com> References: <20250626174623.904917-1-igor.korotin.linux@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Implement the core abstractions needed for I2C drivers, including: * `i2c::Driver` =E2=80=94 the trait drivers must implement, including `prob= e` * `i2c::Device` =E2=80=94 a safe wrapper around `struct i2c_client` * `i2c::Adapter` =E2=80=94 implements `driver::RegistrationOps` to hook int= o the generic `driver::Registration` machinery * `i2c::DeviceId` =E2=80=94 a `RawDeviceId` implementation for I2C device I= Ds Signed-off-by: Igor Korotin --- MAINTAINERS | 2 + rust/bindings/bindings_helper.h | 1 + rust/helpers/helpers.c | 1 + rust/helpers/i2c.c | 15 ++ rust/kernel/i2c.rs | 363 ++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 2 + 6 files changed, 384 insertions(+) create mode 100644 rust/helpers/i2c.c create mode 100644 rust/kernel/i2c.rs diff --git a/MAINTAINERS b/MAINTAINERS index 68d2a32759ec..2b7a24586c19 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -11362,6 +11362,8 @@ F: include/linux/i2c-smbus.h F: include/linux/i2c.h F: include/uapi/linux/i2c-*.h F: include/uapi/linux/i2c.h +F: rust/helpers/i2c.c +F: rust/kernel/i2c.rs =20 I2C SUBSYSTEM HOST DRIVERS M: Andi Shyti diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helpe= r.h index 8cbb660e2ec2..0c7b3cbb8918 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -52,6 +52,7 @@ #include #include #include +#include #include #include #include diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c index 393ad201befb..63afd35205da 100644 --- a/rust/helpers/helpers.c +++ b/rust/helpers/helpers.c @@ -23,6 +23,7 @@ #include "drm.c" #include "err.c" #include "fs.c" +#include "i2c.c" #include "io.c" #include "jump_label.c" #include "kunit.c" diff --git a/rust/helpers/i2c.c b/rust/helpers/i2c.c new file mode 100644 index 000000000000..5f384f8f560e --- /dev/null +++ b/rust/helpers/i2c.c @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include + +void* rust_helper_i2c_get_clientdata(struct i2c_client *client) +{ + return i2c_get_clientdata(client); +} + +void rust_helper_i2c_set_clientdata(struct i2c_client *client, void *data) +{ + i2c_set_clientdata(client, data); +} + + diff --git a/rust/kernel/i2c.rs b/rust/kernel/i2c.rs new file mode 100644 index 000000000000..051f4f2bd38a --- /dev/null +++ b/rust/kernel/i2c.rs @@ -0,0 +1,363 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! I2C Driver subsystem + +// I2C Driver abstractions. +use crate::{ + container_of, device, + device_id::RawDeviceId, + driver, + error::*, + of, + prelude::*, + types::{ForeignOwnable, Opaque}, +}; + +use core::{ + marker::PhantomData, + ptr::{addr_of_mut, NonNull}, +}; + +/// An I2C device id table. +#[repr(transparent)] +#[derive(Clone, Copy)] +pub struct DeviceId(bindings::i2c_device_id); + +impl DeviceId { + const I2C_NAME_SIZE: usize =3D 20; + + /// Create a new device id from an I2C 'id' string. + pub const fn new(id: &[u8; N]) -> Self { + build_assert!(N <=3D Self::I2C_NAME_SIZE); + // Replace with `bindings::i2c_device_id::default()` once stabiliz= ed for `const`. + // SAFETY: FFI type is valid to be zero-initialized. + let mut i2c: bindings::i2c_device_id =3D unsafe { core::mem::zeroe= d() }; + let mut i =3D 0; + while i < N { + i2c.name[i] =3D id[i]; + i +=3D 1; + } + + Self(i2c) + } +} + +// SAFETY: +// * `DeviceId` is a `#[repr(transparent)` wrapper of `i2c_device_id` and = does not add +// additional invariants, so it's safe to transmute to `RawType`. +// * `DRIVER_DATA_OFFSET` is the offset to the `driver_data` field. +unsafe impl RawDeviceId for DeviceId { + type RawType =3D bindings::i2c_device_id; + + const DRIVER_DATA_OFFSET: usize =3D core::mem::offset_of!(bindings::i2= c_device_id, driver_data); + + fn index(&self) -> usize { + self.0.driver_data as _ + } +} + +/// IdTable type for I2C +pub type IdTable =3D &'static dyn kernel::device_id::IdTable; + +/// Create a I2C `IdTable` with its alias for modpost. +#[macro_export] +macro_rules! i2c_device_table { + ($table_name:ident, $module_table_name:ident, $id_info_type: ty, $tabl= e_data: expr) =3D> { + const $table_name: $crate::device_id::IdArray< + $crate::i2c::DeviceId, + $id_info_type, + { $table_data.len() }, + > =3D $crate::device_id::IdArray::new($table_data); + + $crate::module_device_table!("i2c", $module_table_name, $table_nam= e); + }; +} + +/// An adapter for the registration of I2C drivers. +pub struct Adapter(T); + +// SAFETY: A call to `unregister` for a given instance of `RegType` is gua= ranteed to be valid if +// a preceding call to `register` has been successful. +unsafe impl driver::RegistrationOps for Adapter { + type RegType =3D bindings::i2c_driver; + + unsafe fn register( + pdrv: &Opaque, + name: &'static CStr, + module: &'static ThisModule, + ) -> Result { + let i2c_table =3D match T::I2C_ID_TABLE { + Some(table) =3D> table.as_ptr(), + None =3D> core::ptr::null(), + }; + + let of_table =3D match T::OF_ID_TABLE { + Some(table) =3D> table.as_ptr(), + None =3D> core::ptr::null(), + }; + + // SAFETY: It's safe to set the fields of `struct i2c_client` on i= nitialization. + unsafe { + (*pdrv.get()).driver.name =3D name.as_char_ptr(); + (*pdrv.get()).probe =3D Some(Self::probe_callback); + (*pdrv.get()).remove =3D Some(Self::remove_callback); + (*pdrv.get()).shutdown =3D Some(Self::shutdown_callback); + (*pdrv.get()).id_table =3D i2c_table; + (*pdrv.get()).driver.of_match_table =3D of_table; + } + + // SAFETY: `pdrv` is guaranteed to be a valid `RegType`. + to_result(unsafe { bindings::i2c_register_driver(module.0, pdrv.ge= t()) }) + } + + unsafe fn unregister(pdrv: &Opaque) { + // SAFETY: `pdrv` is guaranteed to be a valid `RegType`. + unsafe { bindings::i2c_del_driver(pdrv.get()) } + } +} + +impl Adapter { + extern "C" fn probe_callback(pdev: *mut bindings::i2c_client) -> kerne= l::ffi::c_int { + // SAFETY: The I2C bus only ever calls the probe callback with a v= alid pointer to a + // `struct i2c_client`. + // + // INVARIANT: `pdev` is valid for the duration of `probe_callback(= )`. + let pdev =3D unsafe { &*pdev.cast::>() }; + + let info =3D + Self::i2c_id_info(pdev).or_else(|| ::= id_info(pdev.as_ref())); + + match T::probe(pdev, info) { + Ok(data) =3D> { + unsafe { bindings::i2c_set_clientdata(pdev.as_raw(), data.= into_foreign() as _) }; + } + Err(err) =3D> return Error::to_errno(err), + } + + 0 + } + + extern "C" fn remove_callback(pdev: *mut bindings::i2c_client) { + // SAFETY: `pdev` is a valid pointer to a `struct i2c_client`. + let ptr =3D unsafe { bindings::i2c_get_clientdata(pdev) }.cast(); + + // SAFETY: `remove_callback` is only ever called after a successfu= l call to + // `probe_callback`, hence it's guaranteed that `ptr` points to a = valid and initialized + // `KBox` pointer created through `KBox::into_foreign`. + let _ =3D unsafe { KBox::::from_foreign(ptr) }; + } + + extern "C" fn shutdown_callback(pdev: *mut bindings::i2c_client) { + let pdev =3D unsafe { &*pdev.cast::>() }; + + T::shutdown(pdev); + } + + /// The [`i2c::IdTable`] of the corresponding driver. + fn i2c_id_table() -> Option::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 i= n the [`i2c::IdTable`]. + fn i2c_id_info(dev: &Device) -> Option<&'static ::IdInfo> { + let table =3D Self::i2c_id_table()?; + + // SAFETY: + // - `table` has static lifetime, hence it's valid for read, + // - `dev` is guaranteed to be valid while it's alive, and so is `= pdev.as_ref().as_raw()`. + let raw_id =3D unsafe { bindings::i2c_match_id(table.as_ptr(), dev= .as_raw()) }; + + if raw_id.is_null() { + None + } else { + // SAFETY: `DeviceId` is a `#[repr(transparent)` wrapper of `s= truct i2c_device_id` and + // does not add additional invariants, so it's safe to transmu= te. + let id =3D unsafe { &*raw_id.cast::() }; + + Some(table.info(::i= ndex(id))) + } + } +} + +impl driver::Adapter for Adapter { + type IdInfo =3D T::IdInfo; + + fn of_id_table() -> Option> { + T::OF_ID_TABLE + } +} + +/// Declares a kernel module that exposes a single i2c driver. +/// +/// # Examples +/// +/// ```ignore +/// kernel::module_i2c_driver! { +/// type: MyDriver, +/// name: "Module name", +/// authors: ["Author name"], +/// description: "Description", +/// license: "GPL v2", +/// } +/// ``` +#[macro_export] +macro_rules! module_i2c_driver { + ($($f:tt)*) =3D> { + $crate::module_driver!(, $crate::i2c::Adapter, { $($f)* }); + }; +} + +/// The i2c driver trait. +/// +/// Drivers must implement this trait in order to get a i2c driver registe= red. +/// +/// # Example +/// +///``` +/// # use kernel::{bindings, c_str, device::Core, i2c, of}; +/// +/// struct MyDriver; +/// +/// kernel::i2c_device_table!( +/// I2C_TABLE, +/// MODULE_I2C_TABLE, +/// ::IdInfo, +/// [ +/// (i2c::DeviceId::new(b"test"), ()) +/// ] +/// ); +/// +/// kernel::of_device_table!( +/// OF_TABLE, +/// MODULE_OF_TABLE, +/// ::IdInfo, +/// [ +/// (of::DeviceId::new(c_str!("test,device")), ()) +/// ] +/// ); +/// +/// impl i2c::Driver for MyDriver { +/// type IdInfo =3D (); +/// const I2C_ID_TABLE: Option> =3D Some(&I= 2C_TABLE); +/// const OF_ID_TABLE: Option> =3D Some(&OF_= TABLE); +/// +/// fn probe( +/// _pdev: &i2c::Device, +/// _id_info: Option<&Self::IdInfo>, +/// ) -> Result>> { +/// Err(ENODEV) +/// } +/// +/// fn shutdown(_pdev: &i2c::Device) { +/// } +/// } +///``` +pub trait Driver: Send { + /// The type holding information about each device id supported by the= driver. + // TODO: Use `associated_type_defaults` once stabilized: + // + // ``` + // type IdInfo: 'static =3D (); + // ``` + type IdInfo: 'static; + + /// The table of device ids supported by the driver. + const I2C_ID_TABLE: Option> =3D None; + + /// The table of OF device ids supported by the driver. + const OF_ID_TABLE: Option> =3D None; + + /// I2C driver probe. + /// + /// Called when a new i2c device is added or discovered. + /// Implementers should attempt to initialize the device here. + fn probe(dev: &Device, id_info: Option<&Self::IdInfo>) + -> Result>>; + + /// I2C driver shutdown + /// + /// Called when + fn shutdown(dev: &Device) { + let _ =3D dev; + } +} + +/// The i2c client representation. +/// +/// This structure represents the Rust abstraction for a C `struct i2c_cli= ent`. The +/// implementation abstracts the usage of an already existing C `struct i2= c_client` within Rust +/// code that we get passed from the C side. +/// +/// # Invariants +/// +/// A [`Device`] instance represents a valid `struct i2c_client` created b= y the C portion of +/// the kernel. +#[repr(transparent)] +pub struct Device( + Opaque, + PhantomData, +); + +impl Device { + fn as_raw(&self) -> *mut bindings::i2c_client { + self.0.get() + } +} + +// SAFETY: `Device` is a transparent wrapper of a type that doesn't depend= on `Device`'s generic +// argument. +kernel::impl_device_context_deref!(unsafe { Device }); +kernel::impl_device_context_into_aref!(Device); + +// SAFETY: Instances of `Device` are always reference-counted. +unsafe impl crate::types::AlwaysRefCounted for Device { + fn inc_ref(&self) { + // SAFETY: The existence of a shared reference guarantees that the= refcount is non-zero. + unsafe { bindings::get_device(self.as_ref().as_raw()) }; + } + + unsafe fn dec_ref(obj: NonNull) { + // SAFETY: The safety requirements guarantee that the refcount is = non-zero. + unsafe { bindings::put_device(addr_of_mut!((*obj.as_ref().as_raw()= ).dev)) } + } +} + +impl AsRef> for Device { + fn as_ref(&self) -> &device::Device { + // SAFETY: By the type invariant of `Self`, `self.as_raw()` is a p= ointer to a valid + // `struct i2c_client`. + let dev =3D unsafe { addr_of_mut!((*self.as_raw()).dev) }; + + // SAFETY: `dev` points to a valid `struct device`. + unsafe { device::Device::as_ref(dev) } + } +} + +impl TryFrom<&device::Device> for &Device= { + type Error =3D kernel::error::Error; + + fn try_from(dev: &device::Device) -> Result { + // SAFETY: By the type invariant of `Device`, `dev.as_raw()` is a = valid pointer to a + // `struct device`. + if unsafe { bindings::i2c_verify_client(dev.as_raw()).is_null() } { + return Err(EINVAL); + } + + // SAFETY: We've just verified that the type of `dev` equals to + // `bindings::i2c_client_type`, hence `dev` must be embedded in a = valid + // `struct i2c_client` as guaranteed by the corresponding C code. + let pdev =3D unsafe { container_of!(dev.as_raw(), bindings::i2c_cl= ient, dev) }; + + // SAFETY: `pdev` is a valid pointer to a `struct i2c_client`. + Ok(unsafe { &*pdev.cast() }) + } +} + +// SAFETY: A `Device` is always reference-counted and can be released from= any thread. +unsafe impl Send for Device {} + +// SAFETY: `Device` can be shared among threads because all methods of `De= vice` +// (i.e. `Device) are thread safe. +unsafe impl Sync for Device {} diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 6b4774b2b1c3..30a0fc20dfee 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -78,6 +78,8 @@ #[cfg(CONFIG_RUST_FW_LOADER_ABSTRACTIONS)] pub mod firmware; pub mod fs; +#[cfg(CONFIG_I2C)] +pub mod i2c; pub mod init; pub mod io; pub mod ioctl; --=20 2.43.0 From nobody Wed Oct 8 14:18:09 2025 Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4725D2ECD3B; Thu, 26 Jun 2025 17:53:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750960404; cv=none; b=dN8/kw7vyWEM13aoyKyYm4S5r9Z4vCfXFvaio8bf2uzj3D7FyGSK522B7C3wATBPL6SwtxutzNuS5G7jyRgW0+jM0XBkqmEzy/4lWsWTwzsrHX3inGNg4IZTPh4n9ZlKjh/ZObLR1RJ+JNtUKkFq9xGr/rnehfrAH4cjmc9qig8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750960404; c=relaxed/simple; bh=NQGCH21Cr3TGgbGw7hzS5ZmYtxDnvykL1nv8YPps/uw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ymd4IUzQuqGiIq4NMDlqOrGWzkSyF1suHcYQbNHgyF/DR2aAvElhFup+ci+aYs7YM6FiPOzgQk4cWfYHDGVTI55th6fkK/KY/hq701wHHTUwhwq7bo0prB7uEQM0mCjcPrP5WRK78Hrc6goou7XqneAu3GaD5kfzJPaoMHP0p2g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=fYe+84dF; arc=none smtp.client-ip=209.85.128.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="fYe+84dF" Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-4538bc1cffdso7745955e9.0; Thu, 26 Jun 2025 10:53:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1750960400; x=1751565200; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=ORxdFmBgJBVNfDZeWld7nnhWsBM7PFaTDPy5u2ssuGg=; b=fYe+84dFwY25M1qxXAkhqq4Fqw7kfDpuCtbcjX9TFs/ENW7u5VC2ISqvFhXbAnCq6T WkEO2tRAJy/criqEpcWfu5P/2Rt78O1YhvFjN1DfsZfYkO2m6NrNXJw4YO3xUjyFpgra ep+HBdWsWI9C4Zq51mJzt/WYBiKRrICSquGcu3lOVliSxy2pVAWHkr0hBC6HfDXcxKES G+BWDG4UdCkJASbsCcibwnD9ohP47S1VJ9SixD5YTvnCa1PYmMV0a0RRDCfCa7KO2ose +ilyD9lAtRgPNvKZkusovu3xPwtMj3ZTfs5Qod7D53isgyV9sBj1rrFm01l8ptKiofWf ZrWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750960400; x=1751565200; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=ORxdFmBgJBVNfDZeWld7nnhWsBM7PFaTDPy5u2ssuGg=; b=CJD6fnPlVeQKxnLwMiwNW2BqR4JeY2vZVSFEWtdZ4f97pUDzCTxxmbS1c+9xl85CWN NBt7X5PBVzIjDnQToSJzaEA7Ni249gSYu0I0JOY6gWQK1b7J6YoWMwkMUIOO0VP9EQxI OiW01QKqnNb2gBy1vSW1qVUt9sJrs7Bkm8HB9aEbtoW4cLpZ2cV4lcVd4BwshQ9/7K5A DWjhZt9UYjpSdxcg2LeLF4WSvMm7mKAgwGg4KHhpg4ubg4vE4UOQJDf7IpPBWCcshzU7 N3ATU+4++4aZekZpXqkHANyw/Bz8sT5TrYCIqOZsFJNUKYxISLbCDkjSg55x7ilCOquv BLkg== X-Forwarded-Encrypted: i=1; AJvYcCWA0PmeFMzGhCI8e0YsPL+l4VqKZA7yXNuE/99mPbs0gabaJdth/YKrPFIXWRB9NvETQXYRU/1NIX45e9NYphU=@vger.kernel.org, AJvYcCWEIi7COrmclFgvzHkRVY6hLJ0k7Ewy0HvKqRdGekiXQUkJ0T4xLaYkHkiPB5naYBbUND2+BigOJimvHT7G@vger.kernel.org, AJvYcCXfYCXIUp2xN2OGPlmW0kRe0uh40vT9WfOuz9mcA9CyWw0JzCYt09ylVSWr233VMHFwzwzGyRklDeU=@vger.kernel.org X-Gm-Message-State: AOJu0Yym+5ksAVQa0WGEvwR2cJD/HnErlm+Wt3pCF/BhJOaZZc4HtchJ TR9oFwajwwDf61opGcYM7GjvEl5gWfxeAGkhYcJ9RcDB/JLxlvCcvshW X-Gm-Gg: ASbGncvmchU4AAjnm/TNnzyvEaNP7F8/F4Zr3mSHi8F6cG+FRLeCuIWGLvMSaT1n7Ya ftiDIsfumKOmrMf0zYwGEkdx7P3HmF0alPj5N1flDuo0zYXUbMpQYRszyRdJz4rKf2NyILcg0JQ JAiuxF5cDnCk8v3tH6LuhwC+3hF7WcjGfvLmfc3uYw31Sbb9UFSw7RczWXmG5Fpi6uNgLX/YosI +LMQ1ZzFZPhxc0QmqknGakskCwqL5NHG+jk1Em4WbLFNCU1rZ2iF53dob7HEdSRO4PSE2MvQ/nj ncy0qBGw+yGCXFearYtZtV6yITvUt8VGF5aciG0605KK0CbhT1BhqLOrEHRvDAlkm2qiXkK+5dv nWh7pr7aEvh+25+YOUn4Da8lqer92tZOrs3Ya X-Google-Smtp-Source: AGHT+IEmOlq90hh0Tao5zeGkk5g+LoHlubiwo3zM4KNNDyLJweA8fdHr5wHeWASnKc06/3KhQ4do+A== X-Received: by 2002:a05:600c:4f12:b0:450:c20d:64c3 with SMTP id 5b1f17b1804b1-4538ee835a4mr2612385e9.18.1750960400346; Thu, 26 Jun 2025 10:53:20 -0700 (PDT) Received: from igor-korotin-Precision-Tower-3620.airspan.com ([188.39.32.4]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3a892e6280bsm487731f8f.94.2025.06.26.10.53.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Jun 2025 10:53:19 -0700 (PDT) Sender: Igor Korotin From: Igor Korotin To: Miguel Ojeda , Alex Gaynor , Wolfram Sang Cc: Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Greg Kroah-Hartman , Viresh Kumar , Asahi Lina , Wedson Almeida Filho , Alex Hung , Tamir Duberstein , Xiangfei Ding , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-i2c@vger.kernel.org Subject: [PATCH v1 2/4] samples: rust: add Rust I2C sample driver Date: Thu, 26 Jun 2025 18:51:17 +0100 Message-ID: <20250626175117.906567-1-igor.korotin.linux@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250626174623.904917-1-igor.korotin.linux@gmail.com> References: <20250626174623.904917-1-igor.korotin.linux@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a new `rust_driver_i2c` sample, showing how to bind an I2C client in Rust via legacy I2C-ID or OF compatible tables. Signed-off-by: Igor Korotin --- MAINTAINERS | 1 + samples/rust/Kconfig | 11 ++++++ samples/rust/Makefile | 1 + samples/rust/rust_driver_i2c.rs | 61 +++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 samples/rust/rust_driver_i2c.rs diff --git a/MAINTAINERS b/MAINTAINERS index 2b7a24586c19..a64570dda05e 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -11364,6 +11364,7 @@ F: include/uapi/linux/i2c-*.h F: include/uapi/linux/i2c.h F: rust/helpers/i2c.c F: rust/kernel/i2c.rs +F: samples/rust/rust_driver_i2c.rs =20 I2C SUBSYSTEM HOST DRIVERS M: Andi Shyti diff --git a/samples/rust/Kconfig b/samples/rust/Kconfig index 7f7371a004ee..55aeb12cd7f7 100644 --- a/samples/rust/Kconfig +++ b/samples/rust/Kconfig @@ -62,6 +62,17 @@ config SAMPLE_RUST_DMA =20 If unsure, say N. =20 +config SAMPLE_RUST_DRIVER_I2C + tristate "I2C Driver" + depends on I2C + help + This option builds the Rust I2C driver sample. + + To compile this as a module, choose M here: + the module will be called rust_driver_i2c. + + If unsure, say N. + config SAMPLE_RUST_DRIVER_PCI tristate "PCI Driver" depends on PCI diff --git a/samples/rust/Makefile b/samples/rust/Makefile index bd2faad63b4f..141d8f078248 100644 --- a/samples/rust/Makefile +++ b/samples/rust/Makefile @@ -5,6 +5,7 @@ obj-$(CONFIG_SAMPLE_RUST_MINIMAL) +=3D rust_minimal.o obj-$(CONFIG_SAMPLE_RUST_MISC_DEVICE) +=3D rust_misc_device.o obj-$(CONFIG_SAMPLE_RUST_PRINT) +=3D rust_print.o obj-$(CONFIG_SAMPLE_RUST_DMA) +=3D rust_dma.o +obj-$(CONFIG_SAMPLE_RUST_DRIVER_I2C) +=3D rust_driver_i2c.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_PCI) +=3D rust_driver_pci.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_PLATFORM) +=3D rust_driver_platform.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_FAUX) +=3D rust_driver_faux.o diff --git a/samples/rust/rust_driver_i2c.rs b/samples/rust/rust_driver_i2c= .rs new file mode 100644 index 000000000000..7c5def930fe0 --- /dev/null +++ b/samples/rust/rust_driver_i2c.rs @@ -0,0 +1,61 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Rust I2C driver sample. + +use kernel::{c_str, device::Core, i2c, of, prelude::*, types::ARef}; + +struct SampleDriver { + pdev: ARef, +} + +kernel::i2c_device_table! { + I2C_TABLE, + MODULE_I2C_TABLE, + ::IdInfo, + [(i2c::DeviceId::new(b"rust_driver_i2c"), 0)] +} + +kernel::of_device_table! { + OF_TABLE, + MODULE_OF_TABLE, + ::IdInfo, + [(of::DeviceId::new(c_str!("test,rust_driver_i2c")), 0)] +} + +impl i2c::Driver for SampleDriver { + type IdInfo =3D u32; + + const I2C_ID_TABLE: Option> =3D Some(&I2C_T= ABLE); + const OF_ID_TABLE: Option> =3D Some(&OF_TABL= E); + + fn probe(pdev: &i2c::Device, info: Option<&Self::IdInfo>) -> Res= ult>> { + let dev =3D pdev.as_ref(); + + dev_dbg!(dev, "Probe Rust I2C driver sample.\n"); + + if let Some(info) =3D info { + dev_info!(dev, "Probed with info: '{}'.\n", info); + } + + let drvdata =3D KBox::new(Self { pdev: pdev.into() }, GFP_KERNEL)?; + + Ok(drvdata.into()) + } + fn shutdown(pdev: &i2c::Device) { + dev_dbg!(pdev.as_ref(), "Shutdown Rust I2C driver sample.\n"); + } +} + +impl Drop for SampleDriver { + fn drop(&mut self) { + dev_dbg!(self.pdev.as_ref(), "Remove Rust I2C driver sample.\n"); + } +} + +kernel::module_i2c_driver! { + type: SampleDriver, + name: "rust_driver_i2c", + authors: ["Igor Korotin"], + description: "Rust I2C driver", + license: "GPL v2", +} --=20 2.43.0 From nobody Wed Oct 8 14:18:09 2025 Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DE8EA4A33; Thu, 26 Jun 2025 17:55:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750960530; cv=none; b=U41dJbek1k3CTzSpjJ4HNrxTmAreTS2cI1W8osAweKIzJ9eyiZGygSsyvOrIZWmkpffumN0NonZHQ3eL07zS8sUTkrKCpjDIq1w1y13Bz/CCKDJ/jE+kzZO2BYHsgyUdUMgV4tKm24ti19W9CDpkL4S/kqtT/2s0F6iyuDUEkJU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750960530; c=relaxed/simple; bh=Is4pl56zzII6m8npRKkdjZoguWllIfYhpTW2lmD+PAA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=unyOuW3//kCNetWHW7NC990I2Tw+Db/gQTT5MdtTe+DuJKNqJ1TE1yaB8jBcjXIXvKK+vDkTVZpO4h4Y36CNBdlL7KM3/BoEndFw4bE9IsDLfEvJdhtdY4Po0SNTznsIzcyhYgteQMQawWNmZ7Y/RWvByKb74BJek9qj5Ts6U+M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ADAAlFvd; arc=none smtp.client-ip=209.85.128.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ADAAlFvd" Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-450ce3a2dd5so9937135e9.3; Thu, 26 Jun 2025 10:55:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1750960527; x=1751565327; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=RIDI78zT6DRLAysPKVHAZFjsgIf6iaU9mJj8Cm/tcZE=; b=ADAAlFvdbepKDvMJ+B6CBHj9jzRYErFPHoyBo3qro5RQpce+VsGsdgVMMleJZs0eV6 mcxucvx6Q9I5nlXStUwzTrnXQL5k6pobsmSx3wBrOX5EShgec98H/vQtNn+mhZF49L93 Gx7VcMk8GRXfxv631tmqUWZZ0avk1aWlFHJJiVYnTdGfq3ewxaTvhj90njeUU57uDEtO BK176HPfWMAkYZeZIzokRBdk+R1oeRHcd4jMSIIqNUAKnc1VUDq+Sne9FbUJ+TwZ/xlN iJcL1Hgwc99ZSAzsdC5aINJ61FnsPGEBvdUT9CKajfJblWRJ8UYMKfs5w945xTKumiYB KWWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750960527; x=1751565327; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=RIDI78zT6DRLAysPKVHAZFjsgIf6iaU9mJj8Cm/tcZE=; b=V35889sTSv45TcND3O6OOKaMQUSvufR3P/7IUXYQ7ouSDvGpmJC+KgeXUBdFI2pd88 eD1kQOzM6FJEjsYEbYLVL9UogLZhS3fuQHWdYlfCiQkx4FBL98eYn0FTrwsMYIdAI73w JYYm7GevtVLFB7w3zuiDHjVcY5BlobP45PjPxeQoXY45hRi0fHKdK/DRhcAIoRyDL0M1 llhw8Gn6UJoZNFq5xnvS7BHElHQuteMtUNg0NpswS5jRxirjR7jZElaZmKIvwCt769iz e+lGfJMm3qtTADfsyDdgE8kyry/PF4BBiRdKWWOic9MRJUpvqBLePh/dkpyrlpBq9Qev J9aw== X-Forwarded-Encrypted: i=1; AJvYcCVXM8rINlpWLmRtOVw5dUwbZa/ykGAhaNG1s01+iIGyb11rxJogFw90ju21mnI+SfESNlhs41PR3zj3j12P@vger.kernel.org, AJvYcCWoea385DyLfgzVIVfTR6Vs50d3NtJkdkgU/yxJjm7NnUahEM+eG++L4sOF+LqqkIX5tQ/AUY7b0kX+Ol4MgMA=@vger.kernel.org, AJvYcCXB520DAB5YHnwKRQ4Qu9lNr60c/k6twNy/tkTfWc7R1zz24XSNpmiyFhwDQGyr4gS8UKdkAXmYquw=@vger.kernel.org X-Gm-Message-State: AOJu0YySJBtDGNnsvZb4/2MFwXPw21lYJw1blCK8fnEIcjXbj5e7YVOS ryqaREDmejw0CX4drCknwpkR2JtlLRVGo6JSMSunpIED6+iBaLcd+t8K X-Gm-Gg: ASbGncu8zHLqqBpsJ8Zrkxt8M2nhwOSZqpdHtC0A1EOzGEV2xWl+Ceb4mhMae7Ef2u4 Gnesi1vKwwFINL7VV2jGXXeYLK9FYBAXEetwqzl4Dtem5XI0b59N749DMlJALlYtutTNDv0kDlx AaRuyjvQRCvsOIT6zBuiPSu6U0r/mjpivl52yyipfx26NgK13e7jVjwsZibTtmMkV0JZzeUBKfo sJ6F508cgVUHjrakBfMf93+/kYYgurueDPI20EwXKsh6tAnVs9XXAMxG0YzwrAhmkmmV29sU+Ym 2fo/0B2sL4I+kLr7jHaN5ovTvGWnpu0Ua0K5717OJtsLZ5UuUR2moAIXjODE2rKDdV8BIoFwaf+ Z+07U+wmqxr+23d2i4qghLGfMESZm2bxdSjDnnX2bdWvyGzI= X-Google-Smtp-Source: AGHT+IEeKzojehoFo40SoZr9b7MARsdE7+Lp7wM9/muxivPRyuPVFMClAKht6pT/YY7xBqPqPC3GIg== X-Received: by 2002:a05:6000:280d:b0:3a5:27ba:479c with SMTP id ffacd0b85a97d-3a8fe5b1478mr318595f8f.43.1750960527188; Thu, 26 Jun 2025 10:55:27 -0700 (PDT) Received: from igor-korotin-Precision-Tower-3620.airspan.com ([188.39.32.4]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3a892e52c23sm496813f8f.52.2025.06.26.10.55.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Jun 2025 10:55:26 -0700 (PDT) Sender: Igor Korotin From: Igor Korotin To: Miguel Ojeda , Alex Gaynor , Wolfram Sang Cc: Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Greg Kroah-Hartman , Viresh Kumar , Asahi Lina , Wedson Almeida Filho , Alex Hung , Tamir Duberstein , Xiangfei Ding , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-i2c@vger.kernel.org Subject: [PATCH v1 3/4] rust: i2c: ACPI ID-table support for I2C abstractions Date: Thu, 26 Jun 2025 18:53:24 +0100 Message-ID: <20250626175324.908172-1-igor.korotin.linux@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250626174623.904917-1-igor.korotin.linux@gmail.com> References: <20250626174623.904917-1-igor.korotin.linux@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Populate driver.acpi_match_table from T::ACPI_ID_TABLE (defaults to None). Depends-on: <20250620152425.285683-1-igor.korotin.linux@gmail.com> Signed-off-by: Igor Korotin --- rust/kernel/i2c.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/rust/kernel/i2c.rs b/rust/kernel/i2c.rs index 051f4f2bd38a..c242f1d3921f 100644 --- a/rust/kernel/i2c.rs +++ b/rust/kernel/i2c.rs @@ -4,7 +4,7 @@ =20 // I2C Driver abstractions. use crate::{ - container_of, device, + acpi, container_of, device, device_id::RawDeviceId, driver, error::*, @@ -96,6 +96,11 @@ unsafe fn register( None =3D> core::ptr::null(), }; =20 + let acpi_table =3D match T::ACPI_ID_TABLE { + Some(table) =3D> table.as_ptr(), + None =3D> core::ptr::null(), + }; + // SAFETY: It's safe to set the fields of `struct i2c_client` on i= nitialization. unsafe { (*pdrv.get()).driver.name =3D name.as_char_ptr(); @@ -104,6 +109,7 @@ unsafe fn register( (*pdrv.get()).shutdown =3D Some(Self::shutdown_callback); (*pdrv.get()).id_table =3D i2c_table; (*pdrv.get()).driver.of_match_table =3D of_table; + (*pdrv.get()).driver.acpi_match_table =3D acpi_table; } =20 // SAFETY: `pdrv` is guaranteed to be a valid `RegType`. @@ -187,6 +193,10 @@ impl driver::Adapter for Adapter<= T> { fn of_id_table() -> Option> { T::OF_ID_TABLE } + + fn acpi_id_table() -> Option> { + T::ACPI_ID_TABLE + } } =20 /// Declares a kernel module that exposes a single i2c driver. @@ -216,10 +226,19 @@ macro_rules! module_i2c_driver { /// # Example /// ///``` -/// # use kernel::{bindings, c_str, device::Core, i2c, of}; +/// # use kernel::{acpi, bindings, c_str, device::Core, i2c, of}; /// /// struct MyDriver; /// +/// kernel::acpi_device_table!( +/// ACPI_TABLE, +/// MODULE_ACPI_TABLE, +/// ::IdInfo, +/// [ +/// (acpi::DeviceId::new(b"TST0001"), ()) +/// ] +/// ); +/// /// kernel::i2c_device_table!( /// I2C_TABLE, /// MODULE_I2C_TABLE, @@ -242,6 +261,7 @@ macro_rules! module_i2c_driver { /// type IdInfo =3D (); /// const I2C_ID_TABLE: Option> =3D Some(&I= 2C_TABLE); /// const OF_ID_TABLE: Option> =3D Some(&OF_= TABLE); +/// const ACPI_ID_TABLE: Option> =3D Some(= &ACPI_TABLE); /// /// fn probe( /// _pdev: &i2c::Device, @@ -269,6 +289,9 @@ pub trait Driver: Send { /// The table of OF device ids supported by the driver. const OF_ID_TABLE: Option> =3D None; =20 + /// The table of ACPI device ids supported by the driver. + const ACPI_ID_TABLE: Option> =3D None; + /// I2C driver probe. /// /// Called when a new i2c device is added or discovered. --=20 2.43.0 From nobody Wed Oct 8 14:18:09 2025 Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CF6C24A33; Thu, 26 Jun 2025 17:57:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750960658; cv=none; b=GzZdpuurolmTMCafaqY2VFX1tp/kiYx5EZT4Tfel/2gGFmk1yyzWAMdOSzgYUukL3lyYo3DRcGIR4cAbxxv3RylH57XEi8F5mVhYz0fOZGCNSC3U2M7D7NshazhllwkGkzozoTsmLZMiP0OAGNORJLC+BZtKJ9Y5IwWT7AnVysc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750960658; c=relaxed/simple; bh=nAsTYlNlsKoQoRIBxmtqCsE8i3UtjyaiMo3nR3fH1kY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nrKx5qcsYtIsCCx+xmRfi0XOkZhE4TTMQylIXzUsDgAns0hw/g5SLrwmGYyAPbWwFU7ul5aBjHItqcKvkBrDjegHP4rr5A81zLH9SIIFzXDSHnSbVSCp7Vlq+pOD94pc9y0dvH0bw4M9B7M3FgxAz3fIAusIE+OuOLAZnc4wDTI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=lN52fw08; arc=none smtp.client-ip=209.85.128.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="lN52fw08" Received: by mail-wm1-f46.google.com with SMTP id 5b1f17b1804b1-453398e90e9so9327865e9.1; Thu, 26 Jun 2025 10:57:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1750960655; x=1751565455; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=A/JMMKJYJJnaalmSUe6TK9/m1Tf4VynpgQ2NjLenDxQ=; b=lN52fw08dhj42h8qKvJO91vxnJSTy0RnXJryfL1HI7iUtaxoYSP1lHCicWxlIQgrf2 q9fBXt0uPY8SoM9QxSsg790hgNtt1mAjCw5Di6Y/wD6ikpTGk2lyNxiQk43CzIR5UpX1 EJ+bY+q2m5Oh+lRB32viB7d7h8sD3hZIJS2+cIbBfBL5CdHBTwBs2e9RuxvATFZHSEEU ivxugxXEoC6D0cbbuozfqJNuguz4udgYuVvw/F54wbpRDXhdA2ZB++B2jQwU5WONrIdQ JW/7hhndmqMZUd1VRS9Pr0ZMeyLzQsjUxNDPyjj2ivre5ZXb+ZPH9cthJkdpL+bU/lLK kJDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750960655; x=1751565455; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=A/JMMKJYJJnaalmSUe6TK9/m1Tf4VynpgQ2NjLenDxQ=; b=UTGv5FaAJNByQTxJoAZrZB8v8S1C37W40D3Qg7A6WycrkYCMDuufQZpXqbdpnF2Jw5 SBenm/rlFnq+RYwc/RypKzvKjIoyPgPOcdatIZl3VcSuPwIJrtPg0N99SSpFe22ZcufJ gwoNJYX5LMGtYfS2V1qydzqDhg3S5jcOwXOnTbAWUeEpGGHluU6sJ7wIWHVQXvbQPzbP Zfu+MvLXDM3n+vpKydysnowWjcdwS0JgnFguUbOig0p42RWuIz14sOHLMcYqtF7x+if4 YlgtkJJI9BwM7zkBYekPABwxnkW7JQxq2ggW4HkD5Z16J63VHKOb6mtPz7G7c0NKAThX /O5Q== X-Forwarded-Encrypted: i=1; AJvYcCV+PyblhOoknqoo0xa/S9qCeaTHnz8HszKlT9f3Yk+7fIxw4/fUYFZ0o50Ll5k1AQN4p/5xC0hbN8BhebOce/Q=@vger.kernel.org, AJvYcCXDaI2T7fBgoHgw4M9ooeaBX11IsCT25DSqgr4q8dXZliQjmsGjK7COqCptwulG3ubF3DqKpfh2sIeyfByS@vger.kernel.org, AJvYcCXv+dy//4K7pp4Vy1OW3teOz/T1sPimDMKeJLuE3dxx1lk/tb5QNpPAKKqVPgnl+p887InQeoTd8/g=@vger.kernel.org X-Gm-Message-State: AOJu0Yy6pa4DW2ygoTvDReGMEgoRBA+Ja+2htyweSrEBDmqW0Jiu3PSG sPHVDtcoZVHK7JHUgCXnWAY/ZNy4kMLDJLEw78QmFlCCuRB8gYWNafa1 X-Gm-Gg: ASbGnctnatFmZ3KXY+5J0T0Sij6amEJPvwQqNlXuwPXp72MMARrNGhxMojNs1lbI1hs f/t9LT9Nufri5mmHxqm55R08HTi7ZsfDS3VJfCe0/um6ZEsYMTqp4Nhm6LKsWSUVwYYrwqolsps QvDG2po2scqcUnaEiUd67Zh2sejngGbBD/pw907nEvxhA82a3jWbzqMjy6eIfmx4ftUK6zwNFB4 c8kNvelvCcHsj9898djK0jN43bBViTUk6+AsrkOlWahre6b4sQJqa4Q/G9VuuBdo6vmUYDt51n5 DR/bNaocMsSKL35CAxsnj+9arZp2apy6IlmgEUMp8Htqu01OkOFeSIk4GyHhGJ/LbljQa2+9j1E aEajiUkF86+3MASBeEWum1g0IiC53vhvh+0/TiqP5QguzbxU= X-Google-Smtp-Source: AGHT+IF+L5+lFftiKxjMEYMZ+2kfG8sBKVLw/AlWLt7CRtqKdtY0gU2Rq1RqhRFd2kLjCo44YjAIlg== X-Received: by 2002:a05:600c:6745:b0:43c:f513:9591 with SMTP id 5b1f17b1804b1-4538ee41d76mr2736045e9.14.1750960654976; Thu, 26 Jun 2025 10:57:34 -0700 (PDT) Received: from igor-korotin-Precision-Tower-3620.airspan.com ([188.39.32.4]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-453823ba553sm55198365e9.31.2025.06.26.10.57.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Jun 2025 10:57:34 -0700 (PDT) Sender: Igor Korotin From: Igor Korotin To: Miguel Ojeda , Alex Gaynor , Wolfram Sang Cc: Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Greg Kroah-Hartman , Viresh Kumar , Asahi Lina , Wedson Almeida Filho , Alex Hung , Tamir Duberstein , Xiangfei Ding , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-i2c@vger.kernel.org Subject: [PATCH v1 4/4] samples: rust: show ACPI ID-table in I2C sample driver Date: Thu, 26 Jun 2025 18:55:32 +0100 Message-ID: <20250626175532.909732-1-igor.korotin.linux@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250626174623.904917-1-igor.korotin.linux@gmail.com> References: <20250626174623.904917-1-igor.korotin.linux@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add an `acpi_device_table!` example and set `SampleDriver::ACPI_ID_TABLE` to demonstrate ACPI usage. Depends-on: <20250620152425.285683-1-igor.korotin.linux@gmail.com> Signed-off-by: Igor Korotin --- samples/rust/rust_driver_i2c.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/samples/rust/rust_driver_i2c.rs b/samples/rust/rust_driver_i2c= .rs index 7c5def930fe0..98bda18fd098 100644 --- a/samples/rust/rust_driver_i2c.rs +++ b/samples/rust/rust_driver_i2c.rs @@ -2,12 +2,19 @@ =20 //! Rust I2C driver sample. =20 -use kernel::{c_str, device::Core, i2c, of, prelude::*, types::ARef}; +use kernel::{acpi, c_str, device::Core, i2c, of, prelude::*, types::ARef}; =20 struct SampleDriver { pdev: ARef, } =20 +kernel::acpi_device_table! { + ACPI_TABLE, + MODULE_ACPI_TABLE, + ::IdInfo, + [(acpi::DeviceId::new(b"TST0001"), 0)] +} + kernel::i2c_device_table! { I2C_TABLE, MODULE_I2C_TABLE, @@ -25,6 +32,7 @@ struct SampleDriver { impl i2c::Driver for SampleDriver { type IdInfo =3D u32; =20 + const ACPI_ID_TABLE: Option> =3D Some(&ACP= I_TABLE); const I2C_ID_TABLE: Option> =3D Some(&I2C_T= ABLE); const OF_ID_TABLE: Option> =3D Some(&OF_TABL= E); =20 --=20 2.43.0