[RFC PATCH 3/3] rust: sample driver for WMI demonstrations

Gladyshev Ilya posted 3 patches 1 month, 3 weeks ago
There is a newer version of this series
[RFC PATCH 3/3] rust: sample driver for WMI demonstrations
Posted by Gladyshev Ilya 1 month, 3 weeks ago
This driver is for RFC demonstration only. It will be removed before
real submission, however it's working and can be transformed into
sample driver
---
 drivers/platform/x86/Makefile        |  1 +
 drivers/platform/x86/redmi_wmi_rs.rs | 60 ++++++++++++++++++++++++++++
 2 files changed, 61 insertions(+)
 create mode 100644 drivers/platform/x86/redmi_wmi_rs.rs

diff --git a/drivers/platform/x86/Makefile b/drivers/platform/x86/Makefile
index d25762f7114f..3045e42618ef 100644
--- a/drivers/platform/x86/Makefile
+++ b/drivers/platform/x86/Makefile
@@ -13,6 +13,7 @@ obj-$(CONFIG_MXM_WMI)			+= mxm-wmi.o
 obj-$(CONFIG_NVIDIA_WMI_EC_BACKLIGHT)	+= nvidia-wmi-ec-backlight.o
 obj-$(CONFIG_XIAOMI_WMI)		+= xiaomi-wmi.o
 obj-$(CONFIG_REDMI_WMI)			+= redmi-wmi.o
+obj-m					+= redmi_wmi_rs.o
 obj-$(CONFIG_GIGABYTE_WMI)		+= gigabyte-wmi.o
 
 # Acer
diff --git a/drivers/platform/x86/redmi_wmi_rs.rs b/drivers/platform/x86/redmi_wmi_rs.rs
new file mode 100644
index 000000000000..65300bad022d
--- /dev/null
+++ b/drivers/platform/x86/redmi_wmi_rs.rs
@@ -0,0 +1,60 @@
+//! This is SAMPLE DRIVER
+//! It doesn't aim into upstream and serves only demonstration purposes for
+//! RFC patchset
+
+use kernel::sync::atomic::{Atomic, Relaxed};
+
+use kernel::{
+    acpi::AcpiObject,
+    device, module_wmi_driver, pr_info,
+    prelude::*,
+    wmi::{self, Device, DeviceId, Driver},
+    wmi_device_table,
+};
+
+wmi_device_table!(
+    REDMI_TABLE,
+    MODULE_REDMI_TABLE,
+    <RedmiWMIDriver as Driver>::IdInfo,
+    [(DeviceId::new(b"46C93E13-EE9B-4262-8488-563BCA757FEF"), 12)]
+);
+
+struct RedmiWMIDriver {
+    probed_val: i32,
+    invokation_cnt: Atomic<i64>,
+}
+
+#[vtable]
+impl wmi::Driver for RedmiWMIDriver {
+    type IdInfo = i32;
+
+    const TABLE: &'static dyn kernel::device_id::IdTable<DeviceId, Self::IdInfo> = &REDMI_TABLE;
+
+    fn probe(
+        _: &Device<kernel::device::Core>,
+        id_info: &Self::IdInfo,
+    ) -> impl PinInit<Self, Error> {
+        pr_info!("Rust WMI Sample Driver probed with val {id_info}\n");
+
+        Ok(Self {
+            probed_val: *id_info,
+            invokation_cnt: Atomic::new(0),
+        })
+    }
+
+    fn notify(&self, _: &Device<device::Core>, _: &AcpiObject) {
+        pr_info!(
+            "Notified driver with probed_val: {}, invokation cnt: {}",
+            self.probed_val,
+            self.invokation_cnt.fetch_add(1, Relaxed)
+        );
+    }
+}
+
+module_wmi_driver!(
+    type: RedmiWMIDriver,
+    name: "redmi_wmi_sample",
+    authors: ["Gladyshev Ilya"],
+    description: "SAMPLE DRIVER for RFC demonstration only",
+    license: "GPL v2",
+);
-- 
2.51.1.dirty