Re: [RFC PATCH 2/3] rust: introduce WMI abstractions
From: Gladyshev Ilya
Date: Thu Dec 25 2025 - 15:47:57 EST
On 12/25/25 21:06, Armin Wolf wrote:
+// SAFETY: A call to `unregister` for a given instance of `RegType`
is guaranteed to be valid if
+// a preceding call to `register` has been successful.
+unsafe impl<T: Driver + 'static> driver::RegistrationOps for
Adapter<T> {
+ type RegType = bindings::wmi_driver;
+
+ unsafe fn register(
+ wdrv: &Opaque<Self::RegType>,
+ name: &'static CStr,
+ module: &'static ThisModule,
+ ) -> Result {
+ macro_rules! map_callback {
+ ($flag:ident -> $callback:ident) => {
+ if T::$flag {
+ Some(Self::$callback)
+ } else {
+ None
+ }
+ };
+ }
+
+ // SAFETY: It's safe to set the fields of `struct wmi_driver`
on initialization.
+ unsafe {
+ (*wdrv.get()).driver.name = name.as_char_ptr();
+ (*wdrv.get()).driver.probe_type =
bindings::probe_type_PROBE_PREFER_ASYNCHRONOUS;
+ (*wdrv.get()).id_table = T::TABLE.as_ptr();
+ (*wdrv.get()).probe = map_callback!(HAS_PROBE ->
probe_callback);
+ (*wdrv.get()).notify = map_callback!(HAS_NOTIFY ->
notify_callback);
I think it should be possible to handle WMI drivers
requiring .no_notify_data to be set. Is there
a way to declare the WMI event data passed to the notify() callback as
optional? If yes, then i suggest
that we always set .no_notify_data and simply require the WMI drivers
themselves to verify that a given
WMI event does contain additional event data.
Yes, I can change the notify API to receive Optional<&AcpiBuffer>
instead of &AcpiBuffer, so every driver will be forced to verify payload
existence by Rust's type system.
IIRC, casting raw (C) pointer to Optional<&T> is actually a no-op in
Rust, since NULL is automatically mapped to None (empty Optional), so it
will be a zero-cost typesystem win :)