From nobody Sun Feb 8 17:21:48 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6AB1F355022; Sat, 31 Jan 2026 14:12:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769868773; cv=none; b=bOYzBFL4o9yprmyXu+plkUFKhjsQclB3DPV7YHf9nzjwTjdkAXYlCBEXxqgexR9wrqx5Lmybm78MYFUxAj+Yu7GBfNirQZ7TrJ0Q/oMrrPARPuG254B69lBAcgpi5NnCBjLcvKlMCbYkSrbI2T2jYXGTgCh0Ip+uUp76Qvh8ZwY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769868773; c=relaxed/simple; bh=pN7PB1pCh1IWJygKI2Vu4CWLqsurcsYp3Gs/ABkgY6I=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=LhUBk2tZFGxp3MoLlrKgOHaM8rHupkn9/F+Q1+nQ8zEK6YAl55xD7I+5R6mLWoiTN3P7tLhOxbrR3ahPiEivifDFXPMpVp2jQI6a5sBVyWHlEGWQcGfqF7cJtgEDEphgWvAWshi/iiS8N9hWsBN3dAluTqY+iudTOoe4ydPrPRE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=EgYYYSV9; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="EgYYYSV9" Received: by smtp.kernel.org (Postfix) with ESMTPS id 263E5C2BCB4; Sat, 31 Jan 2026 14:12:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1769868773; bh=pN7PB1pCh1IWJygKI2Vu4CWLqsurcsYp3Gs/ABkgY6I=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=EgYYYSV91B/xdRpxt4pz2j4nC/1je+kvqwwvxqKSUHi0ByiLoaqcVXMQQvkJkEGQW 4Rv7+zu7aj2U4ZFY+85ukwyr6R6zrMhxgwFT3Rius36Dt0hg5FS25+ftQAoj9/1k2u f8q+ObbdS1xTmQZAaFGSR7KXPPgKeOvyPaDNJ+deJNXlcq+yRkvtz3UFXE6459QEc4 SVDZXZMood/0QgA7r5VM16MADi4d3D6ph7JcKTOHsy2iqVh19Tq6xteC3/MkOvAg9S qGlLcLUM3eIf7dSeYzg7QLY6uMnjxmIN2g4kPvAZJpBzvVm0mNIfltTsaCX7EcVIfs aU7X/KM78sPrw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1BB35D79770; Sat, 31 Jan 2026 14:12:53 +0000 (UTC) From: Igor Korotin via B4 Relay Date: Sat, 31 Jan 2026 14:12:47 +0000 Subject: [PATCH 5/5] samples: rust: add Rust I2C adapter registration sample 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 Message-Id: <20260131-i2c-adapter-v1-5-5a436e34cd1a@gmail.com> References: <20260131-i2c-adapter-v1-0-5a436e34cd1a@gmail.com> In-Reply-To: <20260131-i2c-adapter-v1-0-5a436e34cd1a@gmail.com> To: Danilo Krummrich , Daniel Almeida , Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Wolfram Sang Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-i2c@vger.kernel.org, markus.probst@posteo.de, Igor Korotin X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1769868772; l=7280; i=igor.korotin.linux@gmail.com; s=20260125; h=from:subject:message-id; bh=UoiJITbB7wSdqa0+19oS9e0ghILI5njDjJv+rjpQTNo=; b=OJMA6QqqxEoPFMUi/s6s6vUTrFKJwRrspekSjPnBxBTRXVy2rYv7XsxvuwQ2sLrxN9izdsPPP eVuBxGsChbWCKUGKyTtelRLz2VjTCBrCm7z+Z8P954G66OF/8b0QJBK X-Developer-Key: i=igor.korotin.linux@gmail.com; a=ed25519; pk=SjZ//wxnynZgJJgTqANyU10A7hdQQdDXLvXIr+zWroE= X-Endpoint-Received: by B4 Relay for igor.korotin.linux@gmail.com/20260125 with auth_id=615 X-Original-From: Igor Korotin Reply-To: igor.korotin.linux@gmail.com From: Igor Korotin Add a new `rust_i2c_adapter` sample, showing how to create a new i2c adapter using `i2c::adapter::Registration` Signed-off-by: Igor Korotin --- MAINTAINERS | 3 +- samples/rust/Kconfig | 12 +++ samples/rust/Makefile | 1 + samples/rust/rust_i2c_adapter.rs | 170 +++++++++++++++++++++++++++++++++++= ++++ 4 files changed, 184 insertions(+), 2 deletions(-) diff --git a/MAINTAINERS b/MAINTAINERS index 60662984176d..bae13f1f9a23 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -11888,8 +11888,7 @@ L: rust-for-linux@vger.kernel.org S: Maintained F: rust/i2c/* F: rust/kernel/i2c.rs -F: samples/rust/rust_driver_i2c.rs -F: samples/rust/rust_i2c_client.rs +F: samples/rust/rust_*i2c*.rs =20 I2C SUBSYSTEM HOST DRIVERS M: Andi Shyti diff --git a/samples/rust/Kconfig b/samples/rust/Kconfig index 3efa51bfc8ef..a5157b50e27c 100644 --- a/samples/rust/Kconfig +++ b/samples/rust/Kconfig @@ -107,6 +107,18 @@ config SAMPLE_RUST_I2C_CLIENT =20 If unsure, say N. =20 +config SAMPLE_RUST_I2C_ADAPTER + tristate "I2C Adapter Registration" + depends on I2C=3Dy + help + This option builds the Rust I2C adapter manual creation + sample. + + To compile this as a module, choose M here: + the module will be called rust_i2c_adapter. + + 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 f65885d1d62b..7026eb46ae50 100644 --- a/samples/rust/Makefile +++ b/samples/rust/Makefile @@ -9,6 +9,7 @@ obj-$(CONFIG_SAMPLE_RUST_DEBUGFS_SCOPED) +=3D rust_debugfs_= scoped.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_I2C_CLIENT) +=3D rust_i2c_client.o +obj-$(CONFIG_SAMPLE_RUST_I2C_ADAPTER) +=3D rust_i2c_adapter.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_USB) +=3D rust_driver_usb.o diff --git a/samples/rust/rust_i2c_adapter.rs b/samples/rust/rust_i2c_adapt= er.rs new file mode 100644 index 000000000000..542766e27d6f --- /dev/null +++ b/samples/rust/rust_i2c_adapter.rs @@ -0,0 +1,170 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Rust I2C adapter registration sample. +//! +//! An I2C adapter in Rust cannot exist on its own. To register a new I2C = adapter, +//! it must be bound to a parent device. In this sample driver, a platform= device +//! is used as the parent. + +//! ACPI match table test +//! +//! This demonstrates how to test an ACPI-based Rust I2C adapter registrat= ion driver +//! using QEMU with a custom SSDT. +//! +//! Steps: +//! +//! 1. **Create an SSDT source file** (`ssdt.dsl`) with the following cont= ent: +//! +//! ```asl +//! DefinitionBlock ("", "SSDT", 2, "TEST", "VIRTACPI", 0x00000001) +//! { +//! Scope (\_SB) +//! { +//! Device (T432) +//! { +//! Name (_HID, "LNUXBEEF") // ACPI hardware ID to match +//! Name (_UID, 1) +//! Name (_STA, 0x0F) // Device present, enabled +//! Name (_CRS, ResourceTemplate () +//! { +//! Memory32Fixed (ReadWrite, 0xFED00000, 0x1000) +//! }) +//! } +//! } +//! } +//! ``` +//! +//! 2. **Compile the table**: +//! +//! ```sh +//! iasl -tc ssdt.dsl +//! ``` +//! +//! This generates `ssdt.aml` +//! +//! 3. **Run QEMU** with the compiled AML file: +//! +//! ```sh +//! qemu-system-x86_64 -m 512M \ +//! -enable-kvm \ +//! -kernel path/to/bzImage \ +//! -append "root=3D/dev/sda console=3DttyS0" \ +//! -hda rootfs.img \ +//! -serial stdio \ +//! -acpitable file=3Dssdt.aml +//! ``` +//! +//! Requirements: +//! - The `rust_i2c_adapter` must be present either: +//! - built directly into the kernel (`bzImage`), or +//! - available as a `.ko` file and loadable from `rootfs.img` +//! +//! 4. **Verify it worked** by checking `dmesg`: +//! +//! ``` +//! rust_i2c_adapter LNUXBEEF:00: Probe Rust I2C Adapter registration = sample. +//! ``` +//! + +use kernel::{ + acpi, + device, + devres::Devres, + i2c::adapter::{ + I2cAdapter, + I2cAdapterOptions, + Registration, // + }, + i2c::algo::{ + flags, // + I2cAlgorithm, + I2cFlags, + I2cSmbusData, + }, + platform, + prelude::*, + sync::aref::ARef, // +}; + +#[pin_data] +struct SampleDriver { + parent_dev: ARef, + #[pin] + i2c_adap: Devres>, +} + +struct SampleDevice {} + +#[vtable] +impl I2cAlgorithm for SampleDevice { + fn smbus_xfer( + _adap: &I2cAdapter, + _addr: u16, + _flags: u16, + _read_write: u8, + _command: u8, + _size: usize, + _data: &I2cSmbusData, + ) -> Result { + dev_info!(_adap.as_ref(), "SMBus xfer: request handler called"); + Ok(()) + } + + fn functionality(_adap: &I2cAdapter) -> I2cFlags { + flags::I2C_FUNC_SMBUS_READ_BYTE | flags::I2C_FUNC_SMBUS_WRITE_BYTE + } +} + +kernel::acpi_device_table!( + ACPI_TABLE, + MODULE_ACPI_TABLE, + ::IdInfo, + [(acpi::DeviceId::new(c"LNUXBEEF"), ())] +); + +impl platform::Driver for SampleDriver { + type IdInfo =3D (); + + const ACPI_ID_TABLE: Option> =3D Some(&ACP= I_TABLE); + + fn probe( + pdev: &platform::Device, + _id_info: Option<&Self::IdInfo>, + ) -> impl PinInit { + pin_init::pin_init_scope(move || { + dev_info!( + pdev.as_ref(), + "Probe Rust I2C Adapter registration sample.\n" + ); + + Ok(try_pin_init!(Self { + parent_dev: pdev.into(), + i2c_adap <- { + let name =3D I2cAdapterOptions{ name: c"rust_i2c_adapt= er"}; + Registration::register(pdev.as_ref(), name) + }, + })) + }) + } + + fn unbind(pdev: &platform::Device, _this: Pin<&Self>) { + dev_info!( + pdev.as_ref(), + "Unbind start: Rust I2C Adapter registration sample.\n" + ); + // The i2c_adap (Devres>) will be autom= atically + // dropped here, which will call i2c_del_adapter() in its Drop impl + dev_info!( + pdev.as_ref(), + "Unbind complete: Rust I2C Adapter registration sample.\n" + ); + } +} + +kernel::module_platform_driver! { + type: SampleDriver, + name: "rust_i2c_adapter", + authors: ["Igor Korotin"], + description: "Sample I2C Adapter registration", + license: "GPL", +} --=20 2.43.0