Skip to contentSkip to conversation

rust/kernel: Add faux device bindings

PATCH In review Lyude Paul 2 participants 2 messages Feb 7, 2025 – Feb 10, 2025

Subsystems (2)
lore
Share & tools
lore

Lyude Paul

author
#0 lore raw

PATCH v3 1/1 rust/kernel: Add faux device bindings

This introduces a module for working with faux devices in rust, along with adding sample code to show how the API is used. Unlike other types of devices, we don't provide any hooks for device probe/removal - since these are optional for the faux API and are unnecessary in rust.

Cc Greg Kroah-Hartman <[email protected]>
Cc Maíra Canal <[email protected]>
Cc Danilo Krummrich <[email protected]>
Cc Miguel Ojeda <[email protected]>

---
V2:
* Check for NULL on return from device_create(), not a pointer error
* Rename Device to Registration to make its purpose more clear
* Drop platform device comment from crate docstring.
* Update MAINTAINERS
V3:
* Clarify safety comment around thread-safety
* Return ENODEV on failure from new()
* Add missing newline in faux driver sample
* s/crate/module/ in descriptions
* Add newline after faux module docstring
* Fix missing markdown in safety comments
* Add missing INVARIANT: comment in Registration
* s/Registration/faux::Registration/ in rust_driver_faux.rs

---
 MAINTAINERS                      |  2 +
 rust/bindings/bindings_helper.h  |  1 +
 rust/kernel/faux.rs              | 67 ++++++++++++++++++++++++++++++++
 rust/kernel/lib.rs               |  1 +
 samples/rust/Kconfig             | 10 +++++
 samples/rust/Makefile            |  1 +
 samples/rust/rust_driver_faux.rs | 29 ++++++++++++++
 7 files changed, 111 insertions(+)
 create mode 100644 rust/kernel/faux.rs
 create mode 100644 samples/rust/rust_driver_faux.rs
MAINTAINERS +2 -0
index 2f0dc98dab5fa..aa81e57b4c25c 100644
@@ -7119,8 +7119,10 @@ F:	rust/kernel/device.rs
 F:	rust/kernel/device_id.rs
 F:	rust/kernel/devres.rs
 F:	rust/kernel/driver.rs
+F:	rust/kernel/faux.rs
 F:	rust/kernel/platform.rs
 F:	samples/rust/rust_driver_platform.rs
+F:	samples/rust/rust_driver_faux.rs
 
 DRIVERS FOR OMAP ADAPTIVE VOLTAGE SCALING (AVS)
 M:	Nishanth Menon <[email protected]>
rust/bindings/bindings_helper.h +1 -0
index 55354e4dec14e..f46cf3bb70695 100644
@@ -11,6 +11,7 @@
 #include <linux/blk_types.h>
 #include <linux/blkdev.h>
 #include <linux/cred.h>
+#include <linux/device/faux.h>
 #include <linux/errname.h>
 #include <linux/ethtool.h>
 #include <linux/file.h>
rust/kernel/faux.rs new file +67 -0

67 lines not on this page · raw message

rust/kernel/lib.rs +1 -0
index 496ed32b0911a..398242f92a961 100644
@@ -46,6 +46,7 @@
 pub mod devres;
 pub mod driver;
 pub mod error;
+pub mod faux;
 #[cfg(CONFIG_RUST_FW_LOADER_ABSTRACTIONS)]
 pub mod firmware;
 pub mod fs;
samples/rust/Kconfig +10 -0
index 918dbead2c0b4..3b6eae84b2977 100644
@@ -61,6 +61,16 @@ config SAMPLE_RUST_DRIVER_PLATFORM
 
 	  If unsure, say N.
 
+config SAMPLE_RUST_DRIVER_FAUX
+	tristate "Faux Driver"
+	help
+	  This option builds the Rust Faux driver sample.
+
+	  To compile this as a module, choose M here:
+	  the module will be called rust_driver_faux.
+
+	  If unsure, say N.
+
 config SAMPLE_RUST_HOSTPROGS
 	bool "Host programs"
 	help
samples/rust/Makefile +1 -0
index 5a8ab0df0567c..0dbc6d90f1ef9 100644
@@ -6,6 +6,7 @@ obj-$(CONFIG_SAMPLE_RUST_MISC_DEVICE)		+= rust_misc_device.o
 obj-$(CONFIG_SAMPLE_RUST_PRINT)			+= rust_print.o
 obj-$(CONFIG_SAMPLE_RUST_DRIVER_PCI)		+= rust_driver_pci.o
 obj-$(CONFIG_SAMPLE_RUST_DRIVER_PLATFORM)	+= rust_driver_platform.o
+obj-$(CONFIG_SAMPLE_RUST_DRIVER_FAUX)		+= rust_driver_faux.o
 
 rust_print-y := rust_print_main.o rust_print_events.o
 
samples/rust/rust_driver_faux.rs new file +29 -0
new file mode 100644
index 0000000000000..048c6cb98b29a
@@ -0,0 +1,29 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+//! Rust faux device sample.
+
+use kernel::{c_str, faux, prelude::*, Module};
+
+module! {
+    type: SampleModule,
+    name: "rust_faux_driver",
+    author: "Lyude Paul",
+    description: "Rust faux device sample",
+    license: "GPL",
+}
+
+struct SampleModule {
+    _reg: faux::Registration,
+}
+
+impl Module for SampleModule {
+    fn init(_module: &'static ThisModule) -> Result<Self> {
+        pr_info!("Initialising Rust Faux Device Sample\n");
+
+        let reg = faux::Registration::new(c_str!("rust-faux-sample-device"))?;
+
+        dev_info!(reg.as_ref(), "Hello from faux device!\n");
+
+        Ok(Self { _reg: reg })
+    }
+}

base-commit: 808eb958781e4ebb6e9c0962af2e856767e20f45
prerequisite-patch-id: f05ff916822e21eec452f9e5b8d11339c409265a

Signature · 1 line
2.48.1
#1 lore raw
Quoted 11 lines · Lyude Paul

This introduces a module for working with faux devices in rust, along with adding sample code to show how the API is used. Unlike other types of devices, we don't provide any hooks for device probe/removal - since these are optional for the faux API and are unnecessary in rust.

Cc Greg Kroah-Hartman <[email protected]>
Cc Maíra Canal <[email protected]>
Cc Danilo Krummrich <[email protected]>
Cc Miguel Ojeda <[email protected]>
Quoted 15 lines

---
V2:
* Check for NULL on return from device_create(), not a pointer error
* Rename Device to Registration to make its purpose more clear
* Drop platform device comment from crate docstring.
* Update MAINTAINERS
V3:
* Clarify safety comment around thread-safety
* Return ENODEV on failure from new()
* Add missing newline in faux driver sample
* s/crate/module/ in descriptions
* Add newline after faux module docstring
* Fix missing markdown in safety comments
* Add missing INVARIANT: comment in Registration
* s/Registration/faux::Registration/ in rust_driver_faux.rs

Thanks, I've taken this into my series and fixed up the api where I added a "parent" pointer. I'll send it out in a bit...

thanks,

greg k-h