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 3/4] rust: wmi: add Driver trait, Adapter and implement RegistrationOps
Date: Fri, 27 Nov 2026 19:02:51 +0800 [thread overview]
Message-ID: <20261127-rust-wmi-v2-3-5bfafabd6a6e@gmail.com> (raw)
In-Reply-To: <20261127-rust-wmi-v2-0-5bfafabd6a6e@gmail.com>
- Implements the `wmi::Driver` trait.
- Adds adapter implementing `driver::DriverLayout` and
`driver::RegistrationOps` - `wmi::Adapter<T: Driver>`.
- Adds `wmi::Registration<T>` type alias for convenience.
Signed-off-by: Nick Dowsett <nick42d@gmail.com>
---
rust/kernel/wmi.rs | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 125 insertions(+), 2 deletions(-)
diff --git a/rust/kernel/wmi.rs b/rust/kernel/wmi.rs
index f7fbde29177b..235c4a2cef6a 100644
--- a/rust/kernel/wmi.rs
+++ b/rust/kernel/wmi.rs
@@ -8,15 +8,138 @@
bindings,
device,
device_id::RawDeviceId,
+ driver,
+ error,
prelude::*,
sync::aref::AlwaysRefCounted,
- types::Opaque, //
+ types::Opaque,
+ ThisModule, //
};
use core::{
marker::PhantomData,
ptr::NonNull, //
};
+/// Type alias for [`driver::Registration<Adapter<T>>`], the registration of
+/// a WMI driver wrapping a [`struct wmi_driver`].
+///
+/// When this struct is dropped the driver will be unregistered.
+///
+/// [`struct wmi_driver`]: srctree/include/linux/wmi.h
+pub type Registration<T> = driver::Registration<Adapter<T>>;
+
+/// WMI driver trait.
+///
+/// Drivers must implement this trait in order to register a WMI driver.
+///
+/// # Examples
+///
+/// ```
+/// # use kernel::{device::Core, prelude::*, wmi};
+/// struct MyDriver;
+///
+/// kernel::wmi_device_table!(
+/// MY_DRIVER_WMI_TABLE,
+/// [wmi::DeviceId::new(c"00000000-0000-0000-0000-000000000000")]
+/// );
+///
+/// impl wmi::Driver for MyDriver {
+/// type Data<'bound> = Self;
+/// const ID_TABLE: wmi::IdTable = &MY_DRIVER_WMI_TABLE;
+///
+/// fn probe<'bound>(
+/// _dev: &'bound wmi::Device<Core<'_>>,
+/// ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound {
+/// Err(ENODEV)
+/// }
+/// }
+/// ```
+pub trait Driver {
+ /// The type of the driver's bus device private data.
+ type Data<'bound>: Send + 'bound;
+ /// The table of WMI device IDs supported by the driver.
+ const ID_TABLE: IdTable;
+ /// The `probe` callback is called when the WMI driver is bound to a matching WMI device.
+ /// Allocating driver-specific data structures and initializing interfaces to other kernel
+ /// subsystems should normally be done in this function.
+ fn probe<'bound>(
+ dev: &'bound Device<device::Core<'_>>,
+ ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound;
+}
+
+/// Adapter to allow [`Driver`] to be used with [`driver::Registration`].
+pub struct Adapter<T: Driver>(T);
+
+// SAFETY:
+// - `wmi_driver` is a C struct.
+// - Since `T` is a driver, `T::Data` is by definition the type of its
+// device private data.
+// - `wmi_driver` embeds a `device_driver` at offset `DEVICE_DRIVER_OFFSET`
+// as per `core::mem::offset_of!()`.
+unsafe impl<T: Driver> driver::DriverLayout for Adapter<T> {
+ type DriverType = bindings::wmi_driver;
+ type DriverData<'bound> = T::Data<'bound>;
+ const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
+}
+
+// SAFETY: It's always valid to unregister a registered `wmi_driver`.
+unsafe impl<T: Driver> driver::RegistrationOps for Adapter<T> {
+ unsafe fn register(
+ reg: &Opaque<Self::DriverType>,
+ name: &'static CStr,
+ module: &'static ThisModule,
+ ) -> Result {
+ // SAFETY:
+ // - It's safe to set the fields of `wmi_driver` on initialization.
+ // - All set fields meet the invariants of `wmi_driver`.
+ unsafe {
+ // SAFETY: `name` is valid for the static lifetime.
+ (*reg.get()).driver.name = name.as_char_ptr();
+ // SAFETY: This callback will only be called with a pointer to a `wmi_device`
+ // valid for the lifetime of the callback.
+ (*reg.get()).probe = Some(Self::probe_callback);
+ // SAFETY: `T::ID_TABLE` is valid for the static lifetime.
+ (*reg.get()).id_table = T::ID_TABLE.as_ptr();
+ // Per the documentation, new WMI drivers should be allowed to be instantiated
+ // multiple times (see `Documentation/wmi/driver-development-guide.rst`).
+ (*reg.get()).no_singleton = true;
+ }
+
+ // SAFETY: `reg` is a valid `wmi_driver`.
+ error::to_result(unsafe { bindings::__wmi_driver_register(reg.get(), module.as_ptr()) })
+ }
+ unsafe fn unregister(wdrv: &Opaque<Self::DriverType>) {
+ // SAFETY: `wdrv` is a valid `wmi_driver` previously registered with `register()`.
+ unsafe { bindings::wmi_driver_unregister(wdrv.get()) };
+ }
+}
+
+impl<T: Driver> Adapter<T> {
+ /// C wrapper for [`Driver::probe`] function to use it as a C function pointer.
+ ///
+ /// # Safety
+ /// - The caller must ensure that `wdev` is a pointer to a `wmi_device` valid for
+ /// the lifetime of the callback.
+ unsafe extern "C" fn probe_callback(
+ wdev: *mut bindings::wmi_device,
+ _context: *const c_void,
+ ) -> c_int {
+ // SAFETY:
+ // - As per the safety requirement for the function, `wdev` is a pointer to a
+ // `struct wmi_device` valid for the lifetime of the callback.
+ // This is the definition of the Core and CoreInternal marker lifetime.
+ // - `Device` is `#[repr(transparent)]` over `struct wmi_device`.
+ let wdev = unsafe { &*wdev.cast::<Device<device::CoreInternal<'_>>>() };
+ let data = T::probe(wdev);
+ // As per the C driver subsystem documentation, the return type of a driver
+ // probe callback is 0 on success or a kernel error code on failure.
+ error::from_result(|| {
+ wdev.as_ref().set_drvdata(data)?;
+ Ok(0)
+ })
+ }
+}
+
/// IdTable type for WMI drivers.
pub type IdTable = &'static dyn crate::device_id::IdTable<DeviceId, ()>;
@@ -140,7 +263,7 @@ impl<Ctx: device::DeviceContext> AsRef<device::Device<Ctx>> for Device<Ctx> {
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`.
+ // 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:
--
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 3/4] rust: wmi: add Driver trait, Adapter and implement RegistrationOps
Date: Fri, 27 Nov 2026 19:02:51 +0800 [thread overview]
Message-ID: <20261127-rust-wmi-v2-3-5bfafabd6a6e@gmail.com> (raw)
In-Reply-To: <20261127-rust-wmi-v2-0-5bfafabd6a6e@gmail.com>
From: Nick Dowsett <nick42d@gmail.com>
- Implements the `wmi::Driver` trait.
- Adds adapter implementing `driver::DriverLayout` and
`driver::RegistrationOps` - `wmi::Adapter<T: Driver>`.
- Adds `wmi::Registration<T>` type alias for convenience.
Signed-off-by: Nick Dowsett <nick42d@gmail.com>
---
rust/kernel/wmi.rs | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 125 insertions(+), 2 deletions(-)
diff --git a/rust/kernel/wmi.rs b/rust/kernel/wmi.rs
index f7fbde29177b..235c4a2cef6a 100644
--- a/rust/kernel/wmi.rs
+++ b/rust/kernel/wmi.rs
@@ -8,15 +8,138 @@
bindings,
device,
device_id::RawDeviceId,
+ driver,
+ error,
prelude::*,
sync::aref::AlwaysRefCounted,
- types::Opaque, //
+ types::Opaque,
+ ThisModule, //
};
use core::{
marker::PhantomData,
ptr::NonNull, //
};
+/// Type alias for [`driver::Registration<Adapter<T>>`], the registration of
+/// a WMI driver wrapping a [`struct wmi_driver`].
+///
+/// When this struct is dropped the driver will be unregistered.
+///
+/// [`struct wmi_driver`]: srctree/include/linux/wmi.h
+pub type Registration<T> = driver::Registration<Adapter<T>>;
+
+/// WMI driver trait.
+///
+/// Drivers must implement this trait in order to register a WMI driver.
+///
+/// # Examples
+///
+/// ```
+/// # use kernel::{device::Core, prelude::*, wmi};
+/// struct MyDriver;
+///
+/// kernel::wmi_device_table!(
+/// MY_DRIVER_WMI_TABLE,
+/// [wmi::DeviceId::new(c"00000000-0000-0000-0000-000000000000")]
+/// );
+///
+/// impl wmi::Driver for MyDriver {
+/// type Data<'bound> = Self;
+/// const ID_TABLE: wmi::IdTable = &MY_DRIVER_WMI_TABLE;
+///
+/// fn probe<'bound>(
+/// _dev: &'bound wmi::Device<Core<'_>>,
+/// ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound {
+/// Err(ENODEV)
+/// }
+/// }
+/// ```
+pub trait Driver {
+ /// The type of the driver's bus device private data.
+ type Data<'bound>: Send + 'bound;
+ /// The table of WMI device IDs supported by the driver.
+ const ID_TABLE: IdTable;
+ /// The `probe` callback is called when the WMI driver is bound to a matching WMI device.
+ /// Allocating driver-specific data structures and initializing interfaces to other kernel
+ /// subsystems should normally be done in this function.
+ fn probe<'bound>(
+ dev: &'bound Device<device::Core<'_>>,
+ ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound;
+}
+
+/// Adapter to allow [`Driver`] to be used with [`driver::Registration`].
+pub struct Adapter<T: Driver>(T);
+
+// SAFETY:
+// - `wmi_driver` is a C struct.
+// - Since `T` is a driver, `T::Data` is by definition the type of its
+// device private data.
+// - `wmi_driver` embeds a `device_driver` at offset `DEVICE_DRIVER_OFFSET`
+// as per `core::mem::offset_of!()`.
+unsafe impl<T: Driver> driver::DriverLayout for Adapter<T> {
+ type DriverType = bindings::wmi_driver;
+ type DriverData<'bound> = T::Data<'bound>;
+ const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
+}
+
+// SAFETY: It's always valid to unregister a registered `wmi_driver`.
+unsafe impl<T: Driver> driver::RegistrationOps for Adapter<T> {
+ unsafe fn register(
+ reg: &Opaque<Self::DriverType>,
+ name: &'static CStr,
+ module: &'static ThisModule,
+ ) -> Result {
+ // SAFETY:
+ // - It's safe to set the fields of `wmi_driver` on initialization.
+ // - All set fields meet the invariants of `wmi_driver`.
+ unsafe {
+ // SAFETY: `name` is valid for the static lifetime.
+ (*reg.get()).driver.name = name.as_char_ptr();
+ // SAFETY: This callback will only be called with a pointer to a `wmi_device`
+ // valid for the lifetime of the callback.
+ (*reg.get()).probe = Some(Self::probe_callback);
+ // SAFETY: `T::ID_TABLE` is valid for the static lifetime.
+ (*reg.get()).id_table = T::ID_TABLE.as_ptr();
+ // Per the documentation, new WMI drivers should be allowed to be instantiated
+ // multiple times (see `Documentation/wmi/driver-development-guide.rst`).
+ (*reg.get()).no_singleton = true;
+ }
+
+ // SAFETY: `reg` is a valid `wmi_driver`.
+ error::to_result(unsafe { bindings::__wmi_driver_register(reg.get(), module.as_ptr()) })
+ }
+ unsafe fn unregister(wdrv: &Opaque<Self::DriverType>) {
+ // SAFETY: `wdrv` is a valid `wmi_driver` previously registered with `register()`.
+ unsafe { bindings::wmi_driver_unregister(wdrv.get()) };
+ }
+}
+
+impl<T: Driver> Adapter<T> {
+ /// C wrapper for [`Driver::probe`] function to use it as a C function pointer.
+ ///
+ /// # Safety
+ /// - The caller must ensure that `wdev` is a pointer to a `wmi_device` valid for
+ /// the lifetime of the callback.
+ unsafe extern "C" fn probe_callback(
+ wdev: *mut bindings::wmi_device,
+ _context: *const c_void,
+ ) -> c_int {
+ // SAFETY:
+ // - As per the safety requirement for the function, `wdev` is a pointer to a
+ // `struct wmi_device` valid for the lifetime of the callback.
+ // This is the definition of the Core and CoreInternal marker lifetime.
+ // - `Device` is `#[repr(transparent)]` over `struct wmi_device`.
+ let wdev = unsafe { &*wdev.cast::<Device<device::CoreInternal<'_>>>() };
+ let data = T::probe(wdev);
+ // As per the C driver subsystem documentation, the return type of a driver
+ // probe callback is 0 on success or a kernel error code on failure.
+ error::from_result(|| {
+ wdev.as_ref().set_drvdata(data)?;
+ Ok(0)
+ })
+ }
+}
+
/// IdTable type for WMI drivers.
pub type IdTable = &'static dyn crate::device_id::IdTable<DeviceId, ()>;
@@ -140,7 +263,7 @@ impl<Ctx: device::DeviceContext> AsRef<device::Device<Ctx>> for Device<Ctx> {
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`.
+ // 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:
--
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 ` [PATCH v2 1/4] rust: wmi: add basic WMI device abstraction Nick Dowsett
2026-11-27 11:02 ` 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 ` Nick Dowsett [this message]
2026-11-27 11:02 ` [PATCH v2 3/4] rust: wmi: add Driver trait, Adapter and implement RegistrationOps 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-3-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.