From: Nick Dowsett Date: Sun, 22 Nov 2026 12:28:16 +0800 Subject: [PATCH 3/4] rust: wmi: add Driver trait, Adapter and implement RegistrationOps MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261122-rust-wmi-v1-3-63111d9e824c@gmail.com> References: <20261122-rust-wmi-v1-0-63111d9e824c@gmail.com> In-Reply-To: <20261122-rust-wmi-v1-0-63111d9e824c@gmail.com> To: Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?utf-8?q?Onur_=C3=96zkan?= , Armin Wolf Cc: =?utf-8?q?Ilpo_J=C3=A4rvinen?= , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, platform-driver-x86@vger.kernel.org, Nick Dowsett X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795321700; l=6297; i=nick42d@gmail.com; h=from:subject:message-id; bh=NBBpMJ2e+LoZ/1yJhSczt6U3XB3Ub4uia4ccHmG6ulI=; b=AADnUhHNgw1sRfdAG73isLFjmJjuIPtj7VRj86+EVDjShOpyZggn9JR4dTv9qzLSEbL7XqLxk XMBz3XJubp8A1QQ36mBWPHqzMCCkn9mJhDevn4XqRy2oalO3TvWd4Yr X-Developer-Key: i=nick42d@gmail.com; a=ed25519; pk=1uIkNexEofzEXNN3Y3QW5z8IrgjWaP0yH6Es51jxWOg= X-Endpoint-Received: by B4 Relay for nick42d@gmail.com/default with auth_id=1596 List-Id: B4 Relay Submissions - Implements the `wmi::Driver` trait. - Adds adapter implementing `driver::DriverLayout` and `driver::RegistrationOps` - `wmi::Adapter`. - Adds `wmi::Registration` type alias for convenience. Signed-off-by: Nick Dowsett --- rust/kernel/wmi.rs | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 121 insertions(+), 2 deletions(-) diff --git a/rust/kernel/wmi.rs b/rust/kernel/wmi.rs index 494797fdbf68..09712cbc1c93 100644 --- a/rust/kernel/wmi.rs +++ b/rust/kernel/wmi.rs @@ -8,15 +8,134 @@ 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>`], 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 = driver::Registration>; + +/// 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>, +/// ) -> impl PinInit, 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>, + ) -> impl PinInit, Error> + 'bound; +} + +/// Adapter to allow [`Driver`] to be used with [`driver::Registration`]. +pub struct Adapter(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 driver::DriverLayout for Adapter { + 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 driver::RegistrationOps for Adapter { + unsafe fn register( + reg: &Opaque, + 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 { + (*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); + (*reg.get()).id_table = T::ID_TABLE.as_ptr(); + (*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) { + // SAFETY: `wdrv` is a valid `wmi_driver` previously registered with `register()`. + unsafe { bindings::wmi_driver_unregister(wdrv.get()) }; + } +} + +impl Adapter { + /// 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::>>() }; + 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; @@ -133,7 +252,7 @@ impl AsRef> for Device { fn as_ref(&self) -> &device::Device { // SAFETY: // - By the type invariant on `Device`, `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`, then the reference count must be // non-zero. // - If you have a reference to a `Device`, it is either: -- 2.56.1