From: Nick Dowsett <nick42d@gmail.com>
To: "Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>, "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>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Tamir Duberstein" <tamird@kernel.org>,
"Alexandre Courbot" <acourbot@nvidia.com>,
"Onur Özkan" <work@onurozkan.dev>, "Armin Wolf" <W_Armin@gmx.de>
Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
platform-driver-x86@vger.kernel.org,
Nick Dowsett <nick42d@gmail.com>
Subject: [PATCH v2 1/4] rust: wmi: add basic WMI device abstraction
Date: Fri, 27 Nov 2026 19:02:49 +0800 [thread overview]
Message-ID: <20261127-rust-wmi-v2-1-5bfafabd6a6e@gmail.com> (raw)
In-Reply-To: <20261127-rust-wmi-v2-0-5bfafabd6a6e@gmail.com>
- Adds the `kernel::wmi` module
- Exposes `linux/wmi.h` to bindgen.
- Adds `rust/kernel/wmi.rs` to MAINTAINERS.
- Implements `wmi::Device<Ctx>` wrapper around `struct wmi_device`.
Signed-off-by: Nick Dowsett <nick42d@gmail.com>
---
MAINTAINERS | 1 +
rust/bindings/bindings_helper.h | 1 +
rust/kernel/lib.rs | 2 +
rust/kernel/wmi.rs | 93 +++++++++++++++++++++++++++++++++++++++++
4 files changed, 97 insertions(+)
diff --git a/MAINTAINERS b/MAINTAINERS
index 44540a1eb2b8..f58a44003d78 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -422,6 +422,7 @@ F: Documentation/driver-api/wmi.rst
F: Documentation/wmi/
F: drivers/platform/wmi/
F: include/uapi/linux/wmi.h
+F: rust/kernel/wmi.rs
ACRN HYPERVISOR SERVICE MODULE
M: Fei Li <fei1.li@intel.com>
diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h
index 3791af0aadc7..81652d0dfed1 100644
--- a/rust/bindings/bindings_helper.h
+++ b/rust/bindings/bindings_helper.h
@@ -94,6 +94,7 @@
#include <linux/tracepoint.h>
#include <linux/usb.h>
#include <linux/wait.h>
+#include <linux/wmi.h>
#include <linux/workqueue.h>
#include <linux/xarray.h>
#include <net/genetlink.h>
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index 1ab41129d557..b9713bd16313 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -147,6 +147,8 @@
pub mod uaccess;
#[cfg(CONFIG_USB = "y")]
pub mod usb;
+#[cfg(CONFIG_ACPI_WMI = "y")]
+pub mod wmi;
pub mod workqueue;
pub mod xarray;
diff --git a/rust/kernel/wmi.rs b/rust/kernel/wmi.rs
new file mode 100644
index 000000000000..f821bf35220e
--- /dev/null
+++ b/rust/kernel/wmi.rs
@@ -0,0 +1,93 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Abstractions for WMI (Windows Management Instrumentation).
+//!
+//! C header: [`include/linux/wmi.h`](srctree/include/linux/wmi.h)
+
+use crate::{
+ bindings,
+ device,
+ sync::aref::AlwaysRefCounted,
+ types::Opaque, //
+};
+use core::{
+ marker::PhantomData,
+ ptr::NonNull, //
+};
+
+/// A WMI bus device ([`struct wmi_device`]).
+///
+/// # Invariants
+/// - `Device` wraps an in-place `Opaque<bindings::wmi_device>` allocated and always
+/// reference-counted in a thread-safe way by the C subsystem.
+/// - Instances are never constructed or moved by value in Rust, they are only accessed behind
+/// `&Device<Ctx>` or `ARef<Device<device::Normal>>`.
+///
+/// [`struct wmi_device`]: srctree/include/linux/wmi.h
+#[repr(transparent)]
+pub struct Device<Ctx: device::DeviceContext = device::Normal> {
+ ptr: Opaque<bindings::wmi_device>,
+ _marker: PhantomData<Ctx>,
+}
+
+// SAFETY: `Device<Ctx>` is a transparent wrapper of `Opaque<bindings::wmi_device>` whose
+// representation does not depend on `Ctx`.
+crate::impl_device_context_deref!(unsafe { Device });
+crate::impl_device_context_into_aref!(Device);
+
+// SAFETY: By the invariant, `Device<Normal>` is never moved by value in Rust.
+// If sent to another thread as `ARef<Device<Normal>>`, the reference-counting
+// is performed in a thread-safe way.
+unsafe impl Send for Device<device::Normal> {}
+
+// SAFETY:
+// - All that `&Device<Normal>` can do is increment its reference count (thread-safe)
+// and perform a reference conversion to `&device::Device<Normal>`.
+// - All methods on `&device::Device<Normal>` are thread safe.
+unsafe impl Sync for Device<device::Normal> {}
+
+// SAFETY:
+// - By the type invariant, all instances of `Device` are reference-counted.
+// - Increments to the reference count keep the object alive in memory at least
+// until matching decrements (`dec_ref` via `put_device`) are performed.
+unsafe impl AlwaysRefCounted for Device<device::Normal> {
+ #[inline]
+ fn inc_ref(&self) {
+ self.as_ref().inc_ref();
+ }
+ #[inline]
+ unsafe fn dec_ref(obj: NonNull<Self>) {
+ // CAST: `Device` is a `#[repr(transparent)]` wrapper of `Opaque<bindings::wmi_device>`.
+ let wdev: *mut bindings::wmi_device = obj.cast().as_ptr();
+ // SAFETY:
+ // - The safety requirements of `dec_ref` guarantee that `obj` is a valid pointer with a
+ // non-zero reference count.
+ // - `put_device` safely decrements the refcount and triggers release cleanup when it
+ // reaches 0.
+ unsafe { bindings::put_device(&raw mut (*wdev).dev) };
+ }
+}
+impl<Ctx: device::DeviceContext> Device<Ctx> {
+ /// Returns the raw pointer to the underlying `bindings::wmi_device`.
+ #[inline]
+ fn as_raw(&self) -> *mut bindings::wmi_device {
+ self.ptr.get()
+ }
+}
+
+impl<Ctx: device::DeviceContext> AsRef<device::Device<Ctx>> for Device<Ctx> {
+ #[inline]
+ fn as_ref(&self) -> &device::Device<Ctx> {
+ // SAFETY:
+ // - By the type invariant on `Device<Ctx>`, `self.as_raw()` is a valid pointer to
+ // a `wmi_device which embeds a `device`.
+ // - If you have a reference to a `Device<Ctx>`, then the reference count must be
+ // non-zero.
+ // - If you have a reference to a `Device<Ctx>`, it is either:
+ // - Borrowed from an `ARef<Device<Normal>>` that cannot be dropped and decrement
+ // reference count to zero whilst the reference is held.
+ // - Derived from a scope (such as a callback) that guarantees that reference count
+ // will not be reduced to zero whilst that reference is held.
+ unsafe { device::Device::from_raw(&raw mut (*self.as_raw()).dev) }
+ }
+}
--
2.56.1
WARNING: multiple messages have this Message-ID (diff)
From: Nick Dowsett via B4 Relay <devnull+nick42d.gmail.com@kernel.org>
To: "Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>, "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>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Tamir Duberstein" <tamird@kernel.org>,
"Alexandre Courbot" <acourbot@nvidia.com>,
"Onur Özkan" <work@onurozkan.dev>, "Armin Wolf" <W_Armin@gmx.de>
Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
platform-driver-x86@vger.kernel.org,
Nick Dowsett <nick42d@gmail.com>
Subject: [PATCH v2 1/4] rust: wmi: add basic WMI device abstraction
Date: Fri, 27 Nov 2026 19:02:49 +0800 [thread overview]
Message-ID: <20261127-rust-wmi-v2-1-5bfafabd6a6e@gmail.com> (raw)
In-Reply-To: <20261127-rust-wmi-v2-0-5bfafabd6a6e@gmail.com>
From: Nick Dowsett <nick42d@gmail.com>
- Adds the `kernel::wmi` module
- Exposes `linux/wmi.h` to bindgen.
- Adds `rust/kernel/wmi.rs` to MAINTAINERS.
- Implements `wmi::Device<Ctx>` wrapper around `struct wmi_device`.
Signed-off-by: Nick Dowsett <nick42d@gmail.com>
---
MAINTAINERS | 1 +
rust/bindings/bindings_helper.h | 1 +
rust/kernel/lib.rs | 2 +
rust/kernel/wmi.rs | 93 +++++++++++++++++++++++++++++++++++++++++
4 files changed, 97 insertions(+)
diff --git a/MAINTAINERS b/MAINTAINERS
index 44540a1eb2b8..f58a44003d78 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -422,6 +422,7 @@ F: Documentation/driver-api/wmi.rst
F: Documentation/wmi/
F: drivers/platform/wmi/
F: include/uapi/linux/wmi.h
+F: rust/kernel/wmi.rs
ACRN HYPERVISOR SERVICE MODULE
M: Fei Li <fei1.li@intel.com>
diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h
index 3791af0aadc7..81652d0dfed1 100644
--- a/rust/bindings/bindings_helper.h
+++ b/rust/bindings/bindings_helper.h
@@ -94,6 +94,7 @@
#include <linux/tracepoint.h>
#include <linux/usb.h>
#include <linux/wait.h>
+#include <linux/wmi.h>
#include <linux/workqueue.h>
#include <linux/xarray.h>
#include <net/genetlink.h>
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index 1ab41129d557..b9713bd16313 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -147,6 +147,8 @@
pub mod uaccess;
#[cfg(CONFIG_USB = "y")]
pub mod usb;
+#[cfg(CONFIG_ACPI_WMI = "y")]
+pub mod wmi;
pub mod workqueue;
pub mod xarray;
diff --git a/rust/kernel/wmi.rs b/rust/kernel/wmi.rs
new file mode 100644
index 000000000000..f821bf35220e
--- /dev/null
+++ b/rust/kernel/wmi.rs
@@ -0,0 +1,93 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Abstractions for WMI (Windows Management Instrumentation).
+//!
+//! C header: [`include/linux/wmi.h`](srctree/include/linux/wmi.h)
+
+use crate::{
+ bindings,
+ device,
+ sync::aref::AlwaysRefCounted,
+ types::Opaque, //
+};
+use core::{
+ marker::PhantomData,
+ ptr::NonNull, //
+};
+
+/// A WMI bus device ([`struct wmi_device`]).
+///
+/// # Invariants
+/// - `Device` wraps an in-place `Opaque<bindings::wmi_device>` allocated and always
+/// reference-counted in a thread-safe way by the C subsystem.
+/// - Instances are never constructed or moved by value in Rust, they are only accessed behind
+/// `&Device<Ctx>` or `ARef<Device<device::Normal>>`.
+///
+/// [`struct wmi_device`]: srctree/include/linux/wmi.h
+#[repr(transparent)]
+pub struct Device<Ctx: device::DeviceContext = device::Normal> {
+ ptr: Opaque<bindings::wmi_device>,
+ _marker: PhantomData<Ctx>,
+}
+
+// SAFETY: `Device<Ctx>` is a transparent wrapper of `Opaque<bindings::wmi_device>` whose
+// representation does not depend on `Ctx`.
+crate::impl_device_context_deref!(unsafe { Device });
+crate::impl_device_context_into_aref!(Device);
+
+// SAFETY: By the invariant, `Device<Normal>` is never moved by value in Rust.
+// If sent to another thread as `ARef<Device<Normal>>`, the reference-counting
+// is performed in a thread-safe way.
+unsafe impl Send for Device<device::Normal> {}
+
+// SAFETY:
+// - All that `&Device<Normal>` can do is increment its reference count (thread-safe)
+// and perform a reference conversion to `&device::Device<Normal>`.
+// - All methods on `&device::Device<Normal>` are thread safe.
+unsafe impl Sync for Device<device::Normal> {}
+
+// SAFETY:
+// - By the type invariant, all instances of `Device` are reference-counted.
+// - Increments to the reference count keep the object alive in memory at least
+// until matching decrements (`dec_ref` via `put_device`) are performed.
+unsafe impl AlwaysRefCounted for Device<device::Normal> {
+ #[inline]
+ fn inc_ref(&self) {
+ self.as_ref().inc_ref();
+ }
+ #[inline]
+ unsafe fn dec_ref(obj: NonNull<Self>) {
+ // CAST: `Device` is a `#[repr(transparent)]` wrapper of `Opaque<bindings::wmi_device>`.
+ let wdev: *mut bindings::wmi_device = obj.cast().as_ptr();
+ // SAFETY:
+ // - The safety requirements of `dec_ref` guarantee that `obj` is a valid pointer with a
+ // non-zero reference count.
+ // - `put_device` safely decrements the refcount and triggers release cleanup when it
+ // reaches 0.
+ unsafe { bindings::put_device(&raw mut (*wdev).dev) };
+ }
+}
+impl<Ctx: device::DeviceContext> Device<Ctx> {
+ /// Returns the raw pointer to the underlying `bindings::wmi_device`.
+ #[inline]
+ fn as_raw(&self) -> *mut bindings::wmi_device {
+ self.ptr.get()
+ }
+}
+
+impl<Ctx: device::DeviceContext> AsRef<device::Device<Ctx>> for Device<Ctx> {
+ #[inline]
+ fn as_ref(&self) -> &device::Device<Ctx> {
+ // SAFETY:
+ // - By the type invariant on `Device<Ctx>`, `self.as_raw()` is a valid pointer to
+ // a `wmi_device which embeds a `device`.
+ // - If you have a reference to a `Device<Ctx>`, then the reference count must be
+ // non-zero.
+ // - If you have a reference to a `Device<Ctx>`, it is either:
+ // - Borrowed from an `ARef<Device<Normal>>` that cannot be dropped and decrement
+ // reference count to zero whilst the reference is held.
+ // - Derived from a scope (such as a callback) that guarantees that reference count
+ // will not be reduced to zero whilst that reference is held.
+ unsafe { device::Device::from_raw(&raw mut (*self.as_raw()).dev) }
+ }
+}
--
2.56.1
next prev parent reply other threads:[~2026-11-27 11:02 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-27 11:02 [PATCH v2 0/4] rust: wmi: add ACPI WMI bus abstractions Nick Dowsett
2026-11-27 11:02 ` Nick Dowsett via B4 Relay
2026-11-27 11:02 ` Nick Dowsett [this message]
2026-11-27 11:02 ` [PATCH v2 1/4] rust: wmi: add basic WMI device abstraction Nick Dowsett via B4 Relay
2026-11-27 11:02 ` [PATCH v2 2/4] rust: wmi: add DeviceId and wmi_device_table! macro Nick Dowsett
2026-11-27 11:02 ` Nick Dowsett via B4 Relay
2026-11-27 11:02 ` [PATCH v2 3/4] rust: wmi: add Driver trait, Adapter and implement RegistrationOps Nick Dowsett
2026-11-27 11:02 ` Nick Dowsett via B4 Relay
2026-11-28 15:57 ` Armin Wolf
2026-11-29 1:32 ` Nick Dowsett
2026-11-27 11:02 ` [PATCH v2 4/4] rust: wmi: add Buffer and Device::invoke_method() Nick Dowsett
2026-11-27 11:02 ` Nick Dowsett via B4 Relay
2026-11-28 16:05 ` Armin Wolf
2026-11-29 3:08 ` Nick Dowsett
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=20261127-rust-wmi-v2-1-5bfafabd6a6e@gmail.com \
--to=nick42d@gmail.com \
--cc=W_Armin@gmx.de \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=platform-driver-x86@vger.kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=work@onurozkan.dev \
/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.