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: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>,
linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
platform-driver-x86@vger.kernel.org,
"Nick Dowsett" <nick42d@gmail.com>
Subject: [PATCH 4/4] rust: wmi: add Buffer and Device::invoke_method()
Date: Sun, 22 Nov 2026 12:28:17 +0800 [thread overview]
Message-ID: <20261122-rust-wmi-v1-4-63111d9e824c@gmail.com> (raw)
In-Reply-To: <20261122-rust-wmi-v1-0-63111d9e824c@gmail.com>
From: Nick Dowsett <nick42d@gmail.com>
- Adds safe \`wmi::Buffer\` RAII abstraction wrapping an owned
\`struct wmi_buffer\`.
- Implement \`Device::invoke_method()\` - safe abstraction for
\`wmidev_invoke_method\`.
- Also implement \`Sync\` for \`Device<device::Bound>\`.
Signed-off-by: Nick Dowsett <nick42d@gmail.com>
---
rust/kernel/wmi.rs | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 97 insertions(+), 1 deletion(-)
diff --git a/rust/kernel/wmi.rs b/rust/kernel/wmi.rs
index 09712cbc1c93..d97c8cb7cbf7 100644
--- a/rust/kernel/wmi.rs
+++ b/rust/kernel/wmi.rs
@@ -17,7 +17,9 @@
};
use core::{
marker::PhantomData,
- ptr::NonNull, //
+ ops::Deref,
+ ptr::NonNull,
+ slice, //
};
/// Type alias for [`driver::Registration<Adapter<T>>`], the registration of
@@ -222,6 +224,11 @@ unsafe impl Send for Device<device::Normal> {}
// - All methods on `&device::Device<Normal>` are thread safe.
unsafe impl Sync for Device<device::Normal> {}
+// SAFETY:
+// - As per `&Device<Normal>`, but `&Device<Bound>` also has the method `invoke_method()`,
+// which wraps the internally synchronised `bindings::wmidev_invoke_method()`.
+unsafe impl Sync for Device<device::Bound> {}
+
// 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
@@ -248,6 +255,70 @@ fn as_raw(&self) -> *mut bindings::wmi_device {
}
}
+impl Device<device::Bound> {
+ /// Calls `wmidev_invoke_method`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// # use kernel::{device::Bound, prelude::*, wmi};
+ /// const METHOD_ID: u32 = 0xB105;
+ /// const METHOD_ARGS: [u8; 1] = [0];
+ /// fn method_returning_u32(wdev: &wmi::Device<Bound>) -> Result<u32> {
+ /// let output: wmi::Buffer = wdev
+ /// .invoke_method(0, METHOD_ID, &METHOD_ARGS, size_of::<u32>())?;
+ /// let output_as_bytes: [u8; 4] = output
+ /// .get(..size_of::<u32>())
+ /// .and_then(|buf| buf.try_into().ok())
+ /// .ok_or(ENODATA)?;
+ /// let output_u32 = u32::from_le_bytes(output_as_bytes);
+ /// dev_dbg!(wdev.as_ref(), "WMI output: {}\n", output_u32);
+ /// Ok(output_u32)
+ /// }
+ /// ```
+ pub fn invoke_method(
+ &self,
+ instance: u8,
+ method_id: u32,
+ input: &[u8],
+ min_output_size: usize,
+ ) -> Result<Buffer> {
+ let mut out_buf = bindings::wmi_buffer::default();
+ let in_buf = bindings::wmi_buffer {
+ length: input.len(),
+ // NOTE1: Even if the input slice is empty, this will create a dangling pointer
+ // rather than a null pointer as Rust references are always non-null.
+ // NOTE2: The input parameter to wmidev_invoke_method is expected to be read-only,
+ // however, we cast to a mutable pointer here purely to meet the type definition of
+ // `wmi_buffer`.
+ data: input.as_ptr().cast_mut().cast::<c_void>(),
+ };
+ // SAFETY:
+ // - By type invariants, `self.as_raw()` points to a valid, live `wmi_device`.
+ // - `out_buf` and `in_buf` are pointers to valid, live `wmi_buffer`s.
+ // - `in_buf` contains a pointer to an allocation behind a Rust reference (which therefore
+ // lives as long as this method call) with length less than or equal to the allocation
+ // size, or contains a dangling pointer with length 0.
+ // - The allocation in `in_buf` will not be mutated - the C implementation declares the in
+ // parameter as const.
+ let status = unsafe {
+ bindings::wmidev_invoke_method(
+ self.as_raw(),
+ instance,
+ method_id,
+ &raw const in_buf,
+ &raw mut out_buf,
+ min_output_size,
+ )
+ };
+ error::to_result(status)?;
+
+ // On success, `wmidev_invoke_method` returns a `wmi_buffer`
+ // satisfying `Buffer`'s invariants, which we hand to the caller as the
+ // new owner.
+ Ok(Buffer(out_buf))
+ }
+}
impl<Ctx: device::DeviceContext> AsRef<device::Device<Ctx>> for Device<Ctx> {
fn as_ref(&self) -> &device::Device<Ctx> {
// SAFETY:
@@ -263,3 +334,28 @@ fn as_ref(&self) -> &device::Device<Ctx> {
unsafe { device::Device::from_raw(&raw mut (*self.as_raw()).dev) }
}
}
+
+/// An owned, 8-byte aligned output buffer.
+///
+/// # Invariants
+/// - `self.0.data` is a pointer to an 8-byte aligned owned buffer of length
+/// at least `self.0.length` and freeable with `kfree()`.
+pub struct Buffer(bindings::wmi_buffer);
+impl Deref for Buffer {
+ type Target = [u8];
+ fn deref(&self) -> &[u8] {
+ if self.0.length == 0 {
+ return &[];
+ }
+ // SAFETY: By the type invariant, `self.0.length != 0` guarantees `self.0.data`
+ // points to an owned buffer of length at least `self.0.length`.
+ unsafe { slice::from_raw_parts(self.0.data.cast::<u8>(), self.0.length) }
+ }
+}
+impl Drop for Buffer {
+ fn drop(&mut self) {
+ // SAFETY: By the type invariant, `self.0.data` is exclusively owned and
+ // freeable with `kfree()`.
+ unsafe { bindings::kfree(self.0.data) };
+ }
+}
--
2.56.1
prev parent reply other threads:[~2026-11-22 4:28 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-22 4:28 [PATCH 0/4] rust: wmi: add MVP of WMI abstractions Nick Dowsett via B4 Relay
2026-11-22 4:28 ` [PATCH 1/4] rust: wmi: add basic WMI device abstraction Nick Dowsett via B4 Relay
2026-11-22 4:28 ` [PATCH 2/4] rust: wmi: add DeviceId and wmi_device_table! macro Nick Dowsett via B4 Relay
2026-11-22 4:28 ` [PATCH 3/4] rust: wmi: add Driver trait, Adapter and implement RegistrationOps Nick Dowsett via B4 Relay
2026-11-22 4:28 ` Nick Dowsett via B4 Relay [this message]
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=20261122-rust-wmi-v1-4-63111d9e824c@gmail.com \
--to=devnull+nick42d.gmail.com@kernel.org \
--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=ilpo.jarvinen@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=nick42d@gmail.com \
--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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®