All of lore.kernel.org
 help / color / mirror / Atom feed
From: Igor Korotin <igor.korotin.linux@gmail.com>
To: Miguel Ojeda <ojeda@kernel.org>,
	Alex Gaynor <alex.gaynor@gmail.com>,
	Wolfram Sang <wsa+renesas@sang-engineering.com>
Cc: "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>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Viresh Kumar" <viresh.kumar@linaro.org>,
	"Asahi Lina" <lina+kernel@asahilina.net>,
	"Wedson Almeida Filho" <wedsonaf@gmail.com>,
	"Alex Hung" <alex.hung@amd.com>,
	"Tamir Duberstein" <tamird@gmail.com>,
	"Xiangfei Ding" <dingxiangfei2009@gmail.com>,
	linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
	linux-i2c@vger.kernel.org
Subject: [PATCH v2 4/4] samples: rust: add Rust manual I2C device creation sample
Date: Fri,  4 Jul 2025 16:43:41 +0100	[thread overview]
Message-ID: <20250704154345.1198721-1-igor.korotin.linux@gmail.com> (raw)
In-Reply-To: <20250704153332.1193214-1-igor.korotin.linux@gmail.com>

Add a new `rust_device_i2c` sample, showing how to create I2C device
on a certain `I2CAdapterRef` using `I2cBoardInfo`. Demonstrates
automatic unregister of such I2C device when driver is unloaded

Signed-off-by: Igor Korotin <igor.korotin.linux@gmail.com>
---
 MAINTAINERS                     |  1 +
 samples/rust/Kconfig            | 13 +++++++++
 samples/rust/Makefile           |  1 +
 samples/rust/rust_device_i2c.rs | 50 +++++++++++++++++++++++++++++++++
 4 files changed, 65 insertions(+)
 create mode 100644 samples/rust/rust_device_i2c.rs

diff --git a/MAINTAINERS b/MAINTAINERS
index 82b469b8ecb9..23bab3c8e1ef 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -11365,6 +11365,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_device_i2c.rs
 F:	samples/rust/rust_driver_i2c.rs
 
 I2C SUBSYSTEM HOST DRIVERS
diff --git a/samples/rust/Kconfig b/samples/rust/Kconfig
index 55aeb12cd7f7..394618aaf5ef 100644
--- a/samples/rust/Kconfig
+++ b/samples/rust/Kconfig
@@ -62,6 +62,18 @@ config SAMPLE_RUST_DMA
 
 	  If unsure, say N.
 
+config SAMPLE_RUST_DEVICE_I2C
+	tristate "Manual I2C Device"
+	depends on I2C && I2C_CHARDEV
+	help
+	  This option builds the Rust I2C device manual creation
+	  sample.
+
+	  To compile this as a module, choose M here:
+	  the module will be called rust_device_i2c.
+
+	  If unsure, say N.
+
 config SAMPLE_RUST_DRIVER_I2C
 	tristate "I2C Driver"
 	depends on I2C
@@ -124,3 +136,4 @@ config SAMPLE_RUST_HOSTPROGS
 	  If unsure, say N.
 
 endif # SAMPLES_RUST
+
diff --git a/samples/rust/Makefile b/samples/rust/Makefile
index 141d8f078248..ee830da1a9d2 100644
--- a/samples/rust/Makefile
+++ b/samples/rust/Makefile
@@ -5,6 +5,7 @@ obj-$(CONFIG_SAMPLE_RUST_MINIMAL)		+= rust_minimal.o
 obj-$(CONFIG_SAMPLE_RUST_MISC_DEVICE)		+= rust_misc_device.o
 obj-$(CONFIG_SAMPLE_RUST_PRINT)			+= rust_print.o
 obj-$(CONFIG_SAMPLE_RUST_DMA)			+= rust_dma.o
+obj-$(CONFIG_SAMPLE_RUST_DEVICE_I2C)		+= rust_device_i2c.o
 obj-$(CONFIG_SAMPLE_RUST_DRIVER_I2C)		+= rust_driver_i2c.o
 obj-$(CONFIG_SAMPLE_RUST_DRIVER_PCI)		+= rust_driver_pci.o
 obj-$(CONFIG_SAMPLE_RUST_DRIVER_PLATFORM)	+= rust_driver_platform.o
diff --git a/samples/rust/rust_device_i2c.rs b/samples/rust/rust_device_i2c.rs
new file mode 100644
index 000000000000..a056736b1b97
--- /dev/null
+++ b/samples/rust/rust_device_i2c.rs
@@ -0,0 +1,50 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Rust I2C DeviceOwned usage sample.
+//!
+//! This sample driver manually creates i2c_client using I2C board info
+//! and pointer to I2C Adapter structure.
+//!
+//! For reproduction of the scenario one should compile kernel with i2c-dev and i2c-stub
+//! modules enabled. f
+
+use kernel::{c_str, device::Core, i2c, prelude::*};
+
+struct SampleDriver {
+    _owned: i2c::DeviceOwned<Core>,
+}
+
+// SAFETY: SampleDriver contains only one field `owned: DeviceOwned<Core>`,
+// which is initialized in `init()` and dropped on module unload.
+// There is no interior mutability or concurrent access to its contents
+// (all I²C operations happen in single-threaded init/drop contexts),
+// so it is safe to share &SampleDriver across threads.
+unsafe impl Sync for SampleDriver {}
+
+const BOARD_INFO: i2c::I2cBoardInfo = i2c::I2cBoardInfo::new(c_str!("rust_driver_i2c"), 0x30);
+
+impl kernel::Module for SampleDriver {
+    fn init(_module: &'static ThisModule) -> Result<Self> {
+        pr_debug!("Probe Rust I2C device sample.\n");
+
+        let adapter = i2c::I2cAdapterRef::get(0).ok_or(EINVAL)?;
+
+        let device = i2c::DeviceOwned::<Core>::new(&adapter, &BOARD_INFO).ok_or(EINVAL)?;
+
+        Ok(Self { _owned: device })
+    }
+}
+
+impl Drop for SampleDriver {
+    fn drop(&mut self) {
+        pr_debug!("Drop Rust I2C device sample.\n");
+    }
+}
+
+kernel::prelude::module! {
+    type:SampleDriver,
+    name:"rust_device_i2c",
+    authors:["Igor Korotin"],
+    description:"Rust I2C device manual creation driver ",
+    license:"GPL v2",
+}
-- 
2.43.0


  parent reply	other threads:[~2025-07-04 15:45 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-04 15:33 [PATCH v2 0/4] rust: i2c: Add basic I2C driver abstractions Igor Korotin
2025-07-04 15:36 ` [PATCH v2 1/4] rust: i2c: add basic I2C device and " Igor Korotin
2025-07-04 20:16   ` Danilo Krummrich
2025-07-07 10:28     ` Igor Korotin
2025-07-07 10:47       ` Miguel Ojeda
2025-07-07 11:23         ` Igor Korotin
2025-07-10 14:04     ` Igor Korotin
2025-07-10 14:46       ` Danilo Krummrich
2025-07-04 15:39 ` [PATCH v2 2/4] rust: i2c: add manual I2C device creation abstractions Igor Korotin
2025-07-04 19:54   ` Danilo Krummrich
2025-07-07 10:35     ` Igor Korotin
2025-07-07 11:20     ` Igor Korotin
2025-07-07 12:02       ` Danilo Krummrich
2025-07-07 14:31         ` Igor Korotin
2025-07-07 14:39           ` Danilo Krummrich
2025-07-04 15:41 ` [PATCH v2 3/4] samples: rust: add Rust I2C sample driver Igor Korotin
2025-07-04 15:43 ` Igor Korotin [this message]
2025-07-04 19:58   ` [PATCH v2 4/4] samples: rust: add Rust manual I2C device creation sample Danilo Krummrich

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=20250704154345.1198721-1-igor.korotin.linux@gmail.com \
    --to=igor.korotin.linux@gmail.com \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=alex.hung@amd.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=dingxiangfei2009@gmail.com \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=lina+kernel@asahilina.net \
    --cc=linux-i2c@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tamird@gmail.com \
    --cc=tmgross@umich.edu \
    --cc=viresh.kumar@linaro.org \
    --cc=wedsonaf@gmail.com \
    --cc=wsa+renesas@sang-engineering.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.