From nobody Tue Nov 24 11:27:41 2026 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795321704; b=WvDwyJrDgz66MKizgv5UWVmlcxdxZLdfvkQBATV7l3BcCfjokYmPHHt2gQL8dzhevG6V JQ8Dx71CYYQ04g3KZPpx9jlPjOWThtveZ2RlYYR7AQsGi96iL6gUsS0cbHzUuIz98qY6f BiWfEdNV1PXS/Uk71hZdrekBHn8A3ZEfxeSm9mtcPDvPGW0v0d5soieP91BcBuV05FTLe PLs2b0Ufe7GXwvGdaRfhFAGF95UDlt8JQ8JiC/VIvHYTZJCePzF3o+hkW+fT63Nn3hasc Kp1fEegE5AtUA88V12sWl4iQf9JlYM0pRE1i9N/XLfn7DhTkF/sA27c53kKV/N2M7Gw== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795321704; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=XQww88CE4ncPleHQjKDJrOVK2foStslcNvThU2QtO2s=; b=QulAJxLMKENkB1gYP2YYbxNN7/X9LqU6eFOCXsUpHWzCBL2KNMo70WRohvo/C1L6o0RJ G2AjwScKSFiv3W67TmJ2C53hY6rxH8zy8RErNqNLs3P/sIsc2DSgEMFTTx6Ypj0t7pP71 aOLdKFT9w3MmqyfoLX3y+5+HWC/aWagCrFH/4SRnLUu5PRnHQ7lxpt0lOlS2TBOqsfO7O LMK5a7VLylzxuk9HVZ1GaS7HXD/9I2wt4ED/UBp3znMWt1ELVueRcN/wSvdZU10j8ohoB ETta1Hyz9wL1s4MFNDdMWQxmgE1m0X+kkSjdeVLxrKMcDgZWVaI/i85uRYGHoj9Q5/Q== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=nV908r26; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ED579606A333; Sun, 22 Nov 2026 04:28:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=nV908r26 Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1795321703; b=G8cdanOL8Yx0KgZKHM9d0e8TOWEM2aZfjaMNoo8JQH8jpCtqhM7bguYy7JrOes1LFE99 qlFNloTkEJq4qJqKvo+OHRMNHmKWk14j2XU3FSD85NK56LgXEA94WF//Nuto2mjIotHUf EMlqKVHF7nDOkNMgVURdcWHpHY2UGd99dfw94LZYGR304yS+ED5QZ/qYulDVRzrNl15vm 9YaUYQxD36hKrN87LJTgDDlkLM1NB/KnOhVHTo1B8VCtM+Q68WRLPYMJs0WoEnnZfBBzA vkwO2MWVd9sBIW6SB5ezep5l/oXbhHF+RaLH6KTLr5HzzNkVzvADj2YYFGJLfjhHYoQ== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795321703; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=XQww88CE4ncPleHQjKDJrOVK2foStslcNvThU2QtO2s=; b=IHtggPR1/12AEd6XvPCkCrdYfzoCiWj50sk1zICccad7qnWblQlYpqAp+9aUKfIYCekz kEGqtbJj2h5CfHiNZEpw8c9SSrl2dA4pGwhE4lcZoSWCIK8bkZ9q5xE4dgiesGFxaGOxW Gf95OQ6DWILmu/qZqCeFZsHpFNuWRY0AqwVDyTYG/SUwZ5sjWYdECTerddJ0v3X/EugXW ZssCG6g6nXS0DAz6p3dR/r74WQaAw8T2i0tA9uJl4P3gb/vZH96OvpdP5GsOGdCDO4xH7 sGOdWdxZY8xIOMbyKC/EmkWM0vJFEu/k7Iih66vNNex6fwrJyGfBbJ/2gif3VLHW94A== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id ADD071F000C6; Sun, 22 Nov 2026 04:28:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795321703; bh=XQww88CE4ncPleHQjKDJrOVK2foStslcNvThU2QtO2s=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=nV908r26mYpXgNVOTvfdL/61cQrUNL6Zltl+dT/C17pQvp1NyKqDjaRzEijH3PLUA JEtz4JUgPH1IbXJijOi1sS03TGiXDQkyach0EOcCMBQq0gHF3vpOCOA2+E12EvDyW+ hhpJ8dCuYLQwIev0l55CrmxyEq4fX9NKJfNeluPbApequ/o2jNeoEWzmfeJnfx3zmr hx+10zmqgzBBIanetpoLeCRPTJmWyjgEhEKNUg4YHz9ZJfQ0SZo0zfUzupmZ/VNmYf U+L9XjkhpomJmYo13gEvvRjHTI9ECJtsNF9gcz6Er2WBxzsaJFScwAdcFcsdsLVQgF B+6xJze46RPzA== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 3C99F4BEAC; Sun, 22 Nov 2026 04:28:22 +0000 (UTC) From: Nick Dowsett via B4 Relay Date: Sun, 22 Nov 2026 12:28:14 +0800 Subject: [PATCH 1/4] rust: wmi: add basic WMI device abstraction X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Message-Id: <20261122-rust-wmi-v1-1-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=5566; i=nick42d@gmail.com; h=from:subject:message-id; bh=Knm+6RNcLG75vWsIc9eLMNWmCBTmuerBtZDTKnVEFGg=; b=n+pCEhCVmKsQB10xdz/YmZv8Cc7AcFnoTejFJim4VEsR8a+g4bplx3nGdD5+siFFy2/kV4JjW 8/l4ATrSiORAhXR/KB5LvDAlDS/n8mMvsAo2Vp9SuG6TgZYiRzEQfnt 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 X-Original-From: Nick Dowsett Reply-To: nick42d@gmail.com Content-Transfer-Encoding: quoted-printable From: Nick Dowsett - Adds the `kernel::wmi` module - Exposes `linux/wmi.h` to bindgen. - Adds `rust/kernel/wmi.rs` to MAINTAINERS. - Implements `wmi::Device` wrapper around `struct wmi_device`. Signed-off-by: Nick Dowsett --- MAINTAINERS | 1 + rust/bindings/bindings_helper.h | 1 + rust/kernel/lib.rs | 2 + rust/kernel/wmi.rs | 89 +++++++++++++++++++++++++++++++++++++= ++++ 4 files changed, 93 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 =20 ACRN HYPERVISOR SERVICE MODULE M: Fei Li diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helpe= r.h index 3791af0aadc7..81652d0dfed1 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -94,6 +94,7 @@ #include #include #include +#include #include #include #include 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 =3D "y")] pub mod usb; +#[cfg(CONFIG_ACPI_WMI =3D "y")] +pub mod wmi; pub mod workqueue; pub mod xarray; =20 diff --git a/rust/kernel/wmi.rs b/rust/kernel/wmi.rs new file mode 100644 index 000000000000..5f1d8c9803d6 --- /dev/null +++ b/rust/kernel/wmi.rs @@ -0,0 +1,89 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Abstractions for WMI. +//! +//! 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` 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` or `ARef>`. +/// +/// [`struct wmi_device`]: srctree/include/linux/wmi.h +#[repr(transparent)] +pub struct Device { + ptr: Opaque, + _marker: PhantomData, +} + +// SAFETY: `Device` is a transparent wrapper of `Opaque` 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` is never moved by value in R= ust. +// If sent to another thread as `ARef>`, the reference-coun= ting +// is performed in a thread-safe way. +unsafe impl Send for Device {} + +// SAFETY: +// - All that `&Device` can do is increment its reference count (t= hread-safe) +// and perform a reference conversion to `&device::Device`. +// - All methods on `&device::Device` are thread safe. +unsafe impl Sync for Device {} + +// SAFETY: +// - By the type invariant, all instances of `Device` are reference-counte= d. +// - 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 { + fn inc_ref(&self) { + self.as_ref().inc_ref(); + } + unsafe fn dec_ref(obj: NonNull) { + // CAST: `Device` is a `#[repr(transparent)]` wrapper of `Opaque`. + let wdev: *mut bindings::wmi_device =3D 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 rele= ase cleanup when it + // reaches 0. + unsafe { bindings::put_device(&raw mut (*wdev).dev) }; + } +} +impl Device { + /// Returns the raw pointer to the underlying `bindings::wmi_device`. + fn as_raw(&self) -> *mut bindings::wmi_device { + self.ptr.get() + } +} + +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`. + // - If you have a reference to a `Device`, then the referenc= e count must be + // non-zero. + // - If you have a reference to a `Device`, it is either: + // - Borrowed from an `ARef>` that cannot be drop= ped and decrement + // reference count to zero whilst the reference is held. + // - Derived from a scope (such as a callback) that guarantees t= hat reference count + // will not be reduced to zero whilst that reference is held. + unsafe { device::Device::from_raw(&raw mut (*self.as_raw()).dev) } + } +} --=20 2.56.1 From nobody Tue Nov 24 11:27:41 2026 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795321704; b=oqEXrMvLzHW7Gs27A0KzUEvSNI16wVaj11LfrQyxZao/+mv/83YKc/PJ1u38nwXaogci bANwz+5C+zv4oDDVhPN46OsAktxXYUCWdMzWGhNuBqeXL5F4Soz9bFcFrhGcgvFKyr+7f yj8jjvYtvLmyXCeDF5+oa1Su5WVkJ4ygzw2A4udPb38wpnJBuOvvY1UDKj7NBK3XHnSO4 1vMz13ddQdT9rAfdW0Po0x/HHqD1DvsTIEXZzMVYanX3rD/P8Bw9LFTti8Mqk5TTSql0k mPVOs/hp8fqJIizIr3ZA05d8YgKCpw59FcQooDcYglknOsb1ab+mk/WjWCltM2YHmjQ== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795321704; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=HXWx9Df0pY/DINSyxxO4vxfCFplqWB2ueWyslaQ3MdE=; b=R3f/q+YiTtMngSVQ2YbHeXLKbkowZGwbHQ8Q4RB1uLCudQTK9t87nwZlmzFV8JPVoZRB ItkUJFreLalV1AqCbmHrA/WOkBLHggtuWrHbM+2zDZaNz4qZ4+eIgaJHIWACIoU1H089Z F152k3FjeFq3pWJTevt6pUNCHCCykg2uyN9qIeke17tg/Sk5wh3eYf9xknsafW5y41h+S LLQ/KEvYo3FWCQKrdrc4knCxreVDrPSUsY2IqOuEbM5ehhGBucx1+9BBNoqmjt3yMJQdR cpnHhx18pfMz/2QLiq3SqLYFiI0lhrnNY73ZwgxJcI0W0Gtzwonr2HAueYWsIMSs80g== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Q7uvrMuz; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 675C7606A33E; Sun, 22 Nov 2026 04:28:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Q7uvrMuz Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1795321704; b=f+16tup7Um+a17rlUq9g/jkfxJidWEkrush5SRht66m1Z5zf3q3GYRd9SABFc6YQGZTS tWWNduPKomOUOsuPW9Ye/AI6co/vSnN3xohP2skhTbFpyIB0XWvFCa2fPSApdtQKcgbKB 6wwqxB1T2gX8kmjrwIlyaP+8+5rufKBdz90FpYZ3WqPoVbd0V3/x0q55CpO5G7ECV9qOK DFGDC7xWsG75Urtmh35lQMSY0YBS3azlEeQgoa2VwRh7PPtFd5ojYjgb8Fhk5TYeXBeQ7 x0kabrg179k6pM2wrEkDKNILde9EHxJaYgwZ3o5B/hwW33Df5WpsfojcwEcf5uGq+kA== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795321704; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=HXWx9Df0pY/DINSyxxO4vxfCFplqWB2ueWyslaQ3MdE=; b=ubcv2KCFQoaL7/5ZdX3h41C6FFyU62upV/LS9g7mBIFtBT6KKTXn80F6LXLVg6Xyl9y5 tf6RukMZmPip2rbqKVgEvlFTA8jVIgyoXan+FUrzY+ht7RrmyQIW73BKs0m/WP41VLOhj HmnHoUvPONKU3aZGvMQLAH/tcyxfwk34gqstefj4GxSFQx42fCXh5dKzzd3I/yFrgn3Xg hIEXqmlFQEftcnhzQuSLsIn2yD5+L6OVUZZljBTvsg4+kMuiNVG0Zed9XHErljsPrZHsR Frt3ySPcDotkCqynJZWpEbThtv/WjUeZzbeL7Tpa3PSOTcuJZ4LFmsNg2oRgsSq3PeQ== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id C99CC1F000CF; Sun, 22 Nov 2026 04:28:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795321704; bh=HXWx9Df0pY/DINSyxxO4vxfCFplqWB2ueWyslaQ3MdE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=Q7uvrMuzKLI2Ce3VvVcpHfxmbdA9fEBL9EHdit3qp5rwn1lbUNQOTayG+kZhFFPGX IsXXILJyXMkC3LVmDc8RcoiAZgY6tmGKaJxnLEnx8TTEY2uwNOQpbyQ446ybQ/9WrC UdRFO8wcfXgGyhSgsiVuwGXcE/qqp6+GCuCQUuUpBSsbYqoWVvsPLo4BnzTcuAliDL WUOq0MuzJCZBjMDVO0QKfphbS3X7fe9xTCWuJP9ZKN5hPoFlisd+e/0yM8Q5WsLAAv eN0Gy4XBdIo3xP0xqe036wdVr7eOm6yGqcSY+q/E1yAxYzBzhM3zCXmOhcU3p4nuxy SjxuEf7qqIVPA== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 5975D4BEEA; Sun, 22 Nov 2026 04:28:22 +0000 (UTC) From: Nick Dowsett via B4 Relay Date: Sun, 22 Nov 2026 12:28:15 +0800 Subject: [PATCH 2/4] rust: wmi: add DeviceId and wmi_device_table! macro X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Message-Id: <20261122-rust-wmi-v1-2-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=2770; i=nick42d@gmail.com; h=from:subject:message-id; bh=rN5WOMkzuWsdFdqCtGFto3bveJqWkdJKdIJZrv+wpJA=; b=HjDeKiCcpD/X/W5TCtr79VRGMnJDNbpXoWRXrRCf1L/3uC1jmyZT66TotA9kdT+Y4Q4lS2YK1 pLLqOiKX/DJB3ubFEcXPob2nyCVBHqrTSi3ciLVWbGaK+k4EgqRlpDo 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 X-Original-From: Nick Dowsett Reply-To: nick42d@gmail.com Content-Transfer-Encoding: quoted-printable From: Nick Dowsett - Add `wmi::DeviceId` wrapping `struct wmi_device_id`. - Implements `wmi::IdTable` and the `wmi_device_table!` macro. Signed-off-by: Nick Dowsett --- rust/kernel/wmi.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 57 insertions(+) diff --git a/rust/kernel/wmi.rs b/rust/kernel/wmi.rs index 5f1d8c9803d6..494797fdbf68 100644 --- a/rust/kernel/wmi.rs +++ b/rust/kernel/wmi.rs @@ -7,6 +7,8 @@ use crate::{ bindings, device, + device_id::RawDeviceId, + prelude::*, sync::aref::AlwaysRefCounted, types::Opaque, // }; @@ -15,6 +17,61 @@ ptr::NonNull, // }; =20 +/// IdTable type for WMI drivers. +pub type IdTable =3D &'static dyn crate::device_id::IdTable; + +/// A WMI device ID wrapping [`struct wmi_device_id`]. +/// +/// [`struct wmi_device_id`]: srctree/include/linux/device-id/wmi.h +/// +/// # Invariants +/// `self.0.guid_string` is a valid, null-terminated string of length [`bi= ndings::UUID_STRING_LEN`]. +#[repr(transparent)] +#[derive(Clone, Copy)] +pub struct DeviceId(bindings::wmi_device_id); +// SAFETY: `DeviceId` is a `#[repr(transparent)]` wrapper of `wmi_device_i= d`. +// `wmi_device_id` does not have any invariants, so it's safe to transmute= to it. +unsafe impl RawDeviceId for DeviceId { + type RawType =3D bindings::wmi_device_id; +} +impl DeviceId { + const WMI_UUID_LEN: usize =3D crate::num::casts::u32_as_usize(bindings= ::UUID_STRING_LEN); + /// Creates a new [`DeviceId`]. + /// + /// Fails at compile time if `guid.len()` is not [`bindings::UUID_STRI= NG_LEN`]. + #[inline(always)] + pub const fn new(guid: &'static CStr) -> Self { + let src =3D guid.to_bytes_with_nul(); + // NOTE: Function is marked `#[inline(always)]` to help compiler o= ptimise the error path. + build_assert!( + src.len() =3D=3D Self::WMI_UUID_LEN + 1, + "WMI DeviceId length incorrect" + ); + let mut id: bindings::wmi_device_id =3D pin_init::zeroed(); + // TODO: Upgrade to core::slice::copy_from_slice(src) once rustc>= =3D1.87 + let mut i =3D 0; + while i < src.len() { + id.guid_string[i] =3D src[i]; + i +=3D 1; + } + Self(id) + } +} + +/// Create a WMI [`IdTable`] with an "alias" for modpost. +#[macro_export] +macro_rules! wmi_device_table { + ($table_name:ident, [$($id:expr),* $(,)?]) =3D> { + $crate::module_device_table!( + "wmi", + $crate::wmi::DeviceId, + $table_name, + @none, + [$($id),*] + ); + }; +} + /// A WMI bus device ([`struct wmi_device`]). /// /// # Invariants --=20 2.56.1 From nobody Tue Nov 24 11:27:41 2026 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795321704; b=XcDO+iaIS1LATFKWc4kuAGqBs3GgFOYQ1VirF69pi6HayX7SEC4RHOyM+ZSGbSKRJkQH NqyA5PWvJvR1ijojjKuT6OrsfAlJwbSPUK+kXHMu1tn/Mw5n97i/eCfeXgrOfiCTt84nY rV2JrqJ75luu26wPt7RynG6uOKWeMD+ML4ypmJAum8F9VEYh1d2XGgJiNt6uOmjVJ62kv UEhH1MRGWDMm7Yl8pCAVutLRAT8bz++Dd8CbOJa2fTkNEyI4XacaNC7rRgke6VeIGDK6O v02N7qfiyKKkgU1hem4U/PaweQzg+f2ZqcsJoB+jeLWn+5celIBQxYZhvegsH+yxfLA== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795321704; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=iOB3JE5qE0FUvOsGS7IC7g4sM218MK+UDdbKg/F5LWA=; b=FSGCenX5ffkN4YQUdzXBd8ADRvySTrpTmO4sQQj1Vwpjo+9SPSoHZv77XyEkDS5Z/bzP V7lvzFL2yBV6n2JcOqGsK8iMoPZKT2Jr69hD8ctWp9/7O4BrlIF9uzSqV16kh4G0td8kI AaFUGvCAbi9te6uZ05A4A1LvYjWjCjMG9ZlF9e0xA/gkkDDJEmk4HjiMEtPaEdFLkDaWq zMAOZogxZEl41kjLkYTBxsOIVClr7e66Tg8NWfgwdSSv0znwFZCcsWscYb8T/68Ai9C8H i9zz/3Wvnwgp0ReLmvUbuqVbsc7VIEE10Qh9ibPb7YOYjEWujuwvEiHsBJlsxc0wkCw== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=eVEsVxEi; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C3762606A687; Sun, 22 Nov 2026 04:28:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=eVEsVxEi Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1795321704; b=jokUd89y+aDsrYHwkxFh0L4lqBcFh5xSwxTK47p/yskZYyL7Yo+syCrJR9meXg6TXUrB XYqwfdGSFMZaC7JRqsjNn1cp5kuY+VG+ShqK2zp2NYG0UjgiaFQY/Po6jOhznw+kQXlrf 85GWyRVepuZ6+depXxDFlwBYjFY3Z8gsdVFtQDmNTKObw6YPBNM9P3x3qK3/mRbAXUH5U 4ujxr+susiT56qZfDOTkk/E/bxRmyDhzbMcLHk0eSr7gQLI0W5w9Fk/xDDLuNviM1Th99 FGn2Lp/Pq9lDzs5YeQ+3YT5eIs9GZxt+bsxLR29mYhYYNnvlKoM+zRh3L9nq0nxOUgQ== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795321704; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=iOB3JE5qE0FUvOsGS7IC7g4sM218MK+UDdbKg/F5LWA=; b=FwGJyrJkSzDISWj3UAsc9uI1I8wnl5vaP+3M13pFbsjcEyIQfwN6zaOP/8czO279daPJ xSyfbqIIUnUwqTQpa18ASs+48W5CN4EIamAALc8EPz5uYr1X5uqtG2KiTr4W9BqNrpDgx k5mnKR4DD59dJu08N+DjfJhDsSNEXN7GoDONFhnE2TJhGSFHd1Agi3AbmPmUZscBBsXcj in/YGdN+6PWnEEiRkA4zqZBFiAnmIGCun8++1T6SR+PZx2XdMHbuQhasAiLHDpMsUxzZz zBvDKY2dIKvxtpbzNEE3p2E7iySpJ/G4+g3XFB1VBKU4qmlRB3gsOX9uljlFUzT5g9g== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id E43AD1F000CA; Sun, 22 Nov 2026 04:28:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795321704; bh=iOB3JE5qE0FUvOsGS7IC7g4sM218MK+UDdbKg/F5LWA=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=eVEsVxEiCRr2JBD/JfNNljqp+DsJ1dmdMIEeROaXUApcZgdPD/CsIm5eM5os51w6J YVzh2N9Sz5wHbknCsH3Di3wSyBDGpvGp/KmYaMM2KoMz2nee8jNCP03uXCUuWckS3a dCz9zF7VDb4oRRpkXTP+8jSEeu3vLYWO5QlBWptg1zucleV8vyRBQ48XcWpnpTzpjt 9oLLdadCha+LIdg0dw/7kxaUoUQud25HctYliIUgs/U9AOZ3qhSNsqioF0Q2C5T9ew vOwhz3AahC5RJlf0OULya4PpE/claU/3zmM0TPEtE8FckYlLUifPmPw7J5K2jciSTa 2xV4AmbYnYZuw== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 740B74BEEE; Sun, 22 Nov 2026 04:28:22 +0000 (UTC) From: Nick Dowsett via B4 Relay Date: Sun, 22 Nov 2026 12:28:16 +0800 Subject: [PATCH 3/4] rust: wmi: add Driver trait, Adapter and implement RegistrationOps X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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 X-Original-From: Nick Dowsett Reply-To: nick42d@gmail.com Content-Transfer-Encoding: quoted-printable From: Nick Dowsett - 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, // }; =20 +/// 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 =3D 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> =3D Self; +/// const ID_TABLE: wmi::IdTable =3D &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 m= atching WMI device. + /// Allocating driver-specific data structures and initializing interf= aces 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 =3D bindings::wmi_driver; + type DriverData<'bound> =3D T::Data<'bound>; + const DEVICE_DRIVER_OFFSET: usize =3D core::mem::offset_of!(Self::Driv= erType, 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 =3D 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 =3D Some(Self::probe_callback); + (*reg.get()).id_table =3D T::ID_TABLE.as_ptr(); + (*reg.get()).no_singleton =3D 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 wi= th `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 p= ointer to a + // `struct wmi_device` valid for the lifetime of the callback. + // This is the definition of the Core and CoreInternal marker li= fetime. + // - `Device` is `#[repr(transparent)]` over `struct wmi_device`. + let wdev =3D unsafe { &*wdev.cast::>>() }; + let data =3D 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 failur= e. + error::from_result(|| { + wdev.as_ref().set_drvdata(data)?; + Ok(0) + }) + } +} + /// IdTable type for WMI drivers. pub type IdTable =3D &'static dyn crate::device_id::IdTable; =20 @@ -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 referenc= e count must be // non-zero. // - If you have a reference to a `Device`, it is either: --=20 2.56.1 From nobody Tue Nov 24 11:27:41 2026 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795321705; b=sS40gd/NMyab/571fW0nCIecqIX2y17Twdg+lUNSeX2DI6obogpZCu1+00LeVOHKB9qO K6T/9TMtqV9VpXqQ20SMdEMNabaafbyRRhocTVl4dlJaL1x3OfolpPQHbaXLGAHxdijTc ZvjUY1AYs4wAYuoBWmPDLTlYsVbyaYkxr47Av/Px4Kq5gm3UKr+A3aDmc5OXOzKAGlE1Z tZ/+ZAH855Q3DFf369ZbTJqC4FovWsy4k8aVpJJd+LArBsQl4xhqXjtquZO5jt56oe0N2 Ti42BJvWYIn3yY/CPsSEe9hwt5E5yoMvfUMGT8orgGd+DomAQZTSa9fOnyMvsiiYbfg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795321705; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=qIENcXcsXDQ9ZgrTaWKLrjR+qyFqEiW4ZYJCFWa3s6c=; b=XUfdRyYvkR2nnMJfjTY/Y/5m/ToGusCr2mOTxx7wf7xVCOFv01kNqoXHEOofnxWfGd0s cKUnrcMZhWje8uKLbhnFAv5GDEK9l0Nkn2acU/0ZceCR9oH8JiKO8FLqL17xaU1lvrOmg 9UYQjBCBLow0+5RrUa+2FvGbgfnMfnJFrisnrUWz1+cZtSSuQ5D25NbqKFaKSdZEoFKz2 0auvASrZs1kLV5CCHY60Zfeavu3IJtvtoiPUK+Tkfl3E/CgcbIjlaS40/OAxE22F2e2B6 WG09Xk1qKU/0+oDKnWfB40TdC6DroUwVuTxmHOgfw4MC0idQDPyk9IAnod6yRjiJc1g== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="lhr/wpa1"; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 54AEC606A32A; Sun, 22 Nov 2026 04:28:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=lhr/wpa1 Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1795321704; b=pX3Sy7M95oyPtZySXzBKi+iHnXcYKVNH7GOkoMXZZC9oxCNKRog4fszx4ThaHJ5js0wC vBn19epApXMO06GG9b52TwH+0a2v4dUVT5/zM8T0ZJXWR8G7R+RWWDeOnu965JPEq1Ogz m/Pj9oK8dn+nMfm3A7TJI7OfPfaWF6RCeWk7hWAVy9aZIxhoIN7XZbIfCj5X/4pLjlGub FiDQHdpcVRAkdIa+pGjbXaOS3wyQHyu8IktHyMrCbN9BXpQbgLyBE6vF94C+2kqGyVV+O szKkB0qL4ueJVd560oZfMXvVFlDC4xVRTTHGVqhVP531gLuzE1PthIchNjz8SO20KUQ== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795321704; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=qIENcXcsXDQ9ZgrTaWKLrjR+qyFqEiW4ZYJCFWa3s6c=; b=nXTXCSEwwVs9MDSphN6xlle+uDyVDvz2j013ZXIJD3lhnqYdN12Pph0C2Q066Sd9W4pT /7U9zOkr6QexRy812jutDWW6s6klYRJVxcgL+Vvbo2Q2lzf1PFR/S/zdwU3GrIuiMK+We xKMWmuj8BRlOsJ/oAcd7vsEJflJrR/CRsrbYb6eUWRmmpiDO7KU6HMRd4tEsmp0Y0A2jv 0ESHq9evSJNWn0+YeLt31ITAxOjE3deYUpRbX1hZmfhWrmTL+xc9jx9wUoewPOXGJ9xG8 10AsUgSitt6L7FTZ/xsO9x6nF5SBExrfrljLhNg6IogJRL/5uV11jL1NE96SFNKx/0A== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id 127821F000D0; Sun, 22 Nov 2026 04:28:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795321704; bh=qIENcXcsXDQ9ZgrTaWKLrjR+qyFqEiW4ZYJCFWa3s6c=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=lhr/wpa1/VWDbK2vhk65OU/XHsTdUqOlVxi+Qny6FKIVigqUe0NqkR2sVXShka9Ey kfOJG7pJi3qMKHBiigOs08/EI0dgEVARDmsu2VoRw1CP72VtXTo9OB4BVVW4S7e6zc 2cQbyIojpJhSq5GNt0XYwzEFP3gk71wRMQbUchPFheORlS0iFdG38NJ+bjx3EOvPRO JJS/yNO5eEaBU52kLWgiRffkk7lpwbMVEj/+k0V0ugEVIrGPZD7IFYaDWgDuX+PEDE DJ/YUrLxVzUiqExnF02bI/HsIzzxT5nWD0nu+LLfiIoNv00FjTF+0oCQYORTdHFdxH NCE+EYsERnffQ== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 96CA54BEF2; Sun, 22 Nov 2026 04:28:22 +0000 (UTC) From: Nick Dowsett via B4 Relay Date: Sun, 22 Nov 2026 12:28:17 +0800 Subject: [PATCH 4/4] rust: wmi: add Buffer and Device::invoke_method() X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Message-Id: <20261122-rust-wmi-v1-4-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=5545; i=nick42d@gmail.com; h=from:subject:message-id; bh=VW4iRI5cDRDRluz4CYZ2xuelFH93Nr8DBnHhhhSeFjE=; b=bqfy+MYcO5ooFuXfwQE7sB0dvrGN1BN/VE0CjFx1xy5WtFBl1ZncUb6DzRL9wFhBcAItpIGXx g+PCl93MbP8CBlMo+aWVGVBPOnpQwWvowCbB8NiKblKPbRNCdM47wil 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 X-Original-From: Nick Dowsett Reply-To: nick42d@gmail.com Content-Transfer-Encoding: quoted-printable From: Nick Dowsett - 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\`. Signed-off-by: Nick Dowsett --- 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, // }; =20 /// Type alias for [`driver::Registration>`], the registration = of @@ -222,6 +224,11 @@ unsafe impl Send for Device {} // - All methods on `&device::Device` are thread safe. unsafe impl Sync for Device {} =20 +// SAFETY: +// - As per `&Device`, but `&Device` also has the method `i= nvoke_method()`, +// which wraps the internally synchronised `bindings::wmidev_invoke_meth= od()`. +unsafe impl Sync for Device {} + // SAFETY: // - By the type invariant, all instances of `Device` are reference-counte= d. // - 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 { } } =20 +impl Device { + /// Calls `wmidev_invoke_method`. + /// + /// # Examples + /// + /// ``` + /// # use kernel::{device::Bound, prelude::*, wmi}; + /// const METHOD_ID: u32 =3D 0xB105; + /// const METHOD_ARGS: [u8; 1] =3D [0]; + /// fn method_returning_u32(wdev: &wmi::Device) -> Result { + /// let output: wmi::Buffer =3D wdev + /// .invoke_method(0, METHOD_ID, &METHOD_ARGS, size_of::(= ))?; + /// let output_as_bytes: [u8; 4] =3D output + /// .get(..size_of::()) + /// .and_then(|buf| buf.try_into().ok()) + /// .ok_or(ENODATA)?; + /// let output_u32 =3D 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 { + let mut out_buf =3D bindings::wmi_buffer::default(); + let in_buf =3D 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 no= n-null. + // NOTE2: The input parameter to wmidev_invoke_method is expec= ted to be read-only, + // however, we cast to a mutable pointer here purely to meet t= he type definition of + // `wmi_buffer`. + data: input.as_ptr().cast_mut().cast::(), + }; + // 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_buffe= r`s. + // - `in_buf` contains a pointer to an allocation behind a Rust re= ference (which therefore + // lives as long as this method call) with length less than or e= qual to the allocation + // size, or contains a dangling pointer with length 0. + // - The allocation in `in_buf` will not be mutated - the C implem= entation declares the in + // parameter as const. + let status =3D 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 a= s the + // new owner. + Ok(Buffer(out_buf)) + } +} impl AsRef> for Device { fn as_ref(&self) -> &device::Device { // SAFETY: @@ -263,3 +334,28 @@ fn as_ref(&self) -> &device::Device { 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 leng= th +/// at least `self.0.length` and freeable with `kfree()`. +pub struct Buffer(bindings::wmi_buffer); +impl Deref for Buffer { + type Target =3D [u8]; + fn deref(&self) -> &[u8] { + if self.0.length =3D=3D 0 { + return &[]; + } + // SAFETY: By the type invariant, `self.0.length !=3D 0` guarantee= s `self.0.data` + // points to an owned buffer of length at least `self.0.length`. + unsafe { slice::from_raw_parts(self.0.data.cast::(), self.0.le= ngth) } + } +} +impl Drop for Buffer { + fn drop(&mut self) { + // SAFETY: By the type invariant, `self.0.data` is exclusively own= ed and + // freeable with `kfree()`. + unsafe { bindings::kfree(self.0.data) }; + } +} --=20 2.56.1