Add WMI driver for Redmibook keyboard.
Subsystems (3)
foxido
maintainerThis is my first interaction with mailing lists in general and upstream linux development in particular, so I apologize for all mistakes in advance. Please correct me, if you see some "behaviour"-kinda errors
--
foxido
Quoted 232 lines · Gladyshev Ilya
This driver implements support for various Fn keys (like Cut) and Xiaomi specific AI button.
Signed-off-by Gladyshev Ilya <[email protected]>--- MAINTAINERS | 6 ++ drivers/platform/x86/Kconfig | 10 ++ drivers/platform/x86/Makefile | 1 + drivers/platform/x86/redmi-wmi.c | 164 +++++++++++++++++++++++++++++++ 4 files changed, 181 insertions(+)create mode 100644
drivers/platform/x86/redmi-wmi.cMAINTAINERS +6
-0@@ -20965,6 +20965,12 @@ S: Maintained T: git https://github.com/pkshih/rtw.git F: drivers/net/wireless/realtek/rtw89/ +REDMIBOOK WMI DRIVERS +M: Gladyshev Ilya <[email protected]> +L: [email protected] +S: Maintained +F: drivers/platform/x86/redmi-wmi.c + REDPINE WIRELESS DRIVER L: [email protected] S: Orphandrivers/platform/x86/Kconfig +10
-0@@ -109,6 +109,16 @@ config XIAOMI_WMI To compile this driver as a module, choose M here: the module will be called xiaomi-wmi. +config REDMI_WMI + tristate "Redmibook WMI key driver" + depends on ACPI_WMI + depends on INPUT + help + Say Y here if you want to support WMI-based keys on Redmibooks. + + To compile this driver as a module, choose M here: the module will + be called redmi-wmi. + config GIGABYTE_WMI tristate "Gigabyte WMI temperature driver" depends on ACPI_WMIdrivers/platform/x86/Makefile +1
-0@@ -13,6 +13,7 @@ obj-$(CONFIG_HUAWEI_WMI) += huawei-wmi.o 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-$(CONFIG_GIGABYTE_WMI) += gigabyte-wmi.o # Acerdrivers/platform/x86/redmi-wmi.c new file +164
-0@@ -0,0 +1,164 @@ +// SPDX-License-Identifier: GPL-2.0 +/* WMI driver for Xiaomi Redmibooks */ + +#include <linux/acpi.h> +#include <linux/device.h> +#include <linux/input.h> +#include <linux/module.h> +#include <linux/mutex.h> +#include <linux/wmi.h> + +#include <uapi/linux/input-event-codes.h> + +#define WMI_REDMIBOOK_KEYBOARD_EVENT_GUID "46c93e13-ee9b-4262-8488-563bca757fef" + +/* Supported WMI keys ... */ +#define ACPI_CUT_PAYLOAD 0x00000201 +#define ACPI_ALL_APPS_PAYLOAD 0x00000301 +#define ACPI_SETUP_PAYLOAD 0x00001b01 +#define ACPI_CST_KEY_PRESS_PAYLOAD 0x00011801 +#define ACPI_CST_KEY_RELEASE_PAYLOAD 0x00011901 + +/* ... and their mappings */ +#define WMI_CUT_KEY KEY_PROG1 +#define WMI_ALL_APPS_KEY KEY_ALL_APPLICATIONS +#define WMI_SETUP_KEY KEY_SETUP +#define WMI_CST_KEY KEY_ASSISTANT + +/* Keyboard backlight key (not supported yet) */ +#define BACKLIGHT_LEVEL_0_PAYLOAD 0x00000501 +#define BACKLIGHT_LEVEL_1_PAYLOAD 0x00800501 +#define BACKLIGHT_LEVEL_2_PAYLOAD 0x00050501 +#define BACKLIGHT_LEVEL_3_PAYLOAD 0x000a0501 + +struct redmi_wmi { +struct input_dev *input_dev; +/* Protects the key event sequence */ +struct mutex key_lock; +}; + +static void redmi_mutex_destroy (void *data) +{ +struct mutex *lock = data; + +mutex_destroy (lock); +} + +static int redmi_wmi_probe (struct wmi_device *wdev,const void *context) +{ +struct redmi_wmi *data; +int ret; + +/* Init dev */ + data =devm_kzalloc (&wdev->dev,sizeof (struct redmi_wmi ),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + +dev_set_drvdata (&wdev->dev, data); + +/* Init mutex & setup destroy at exit */ +mutex_init (&data->key_lock); + ret =devm_add_action_or_reset (&wdev->dev, redmi_mutex_destroy, &data->key_lock); +if (ret <0 ) +return ret; + +/* Setup input device */ + data->input_dev =devm_input_allocate_device (&wdev->dev); +if (!data->input_dev) +return -ENOMEM ; + data->input_dev->name ="Redmibook WMI keys" ; + data->input_dev->phys ="wmi/input0" ; + +set_bit (EV_KEY , data->input_dev->evbit); + +/* "Cut" key*/ +set_bit (WMI_CUT_KEY , data->input_dev->keybit); +/* "All apps" key*/ +set_bit (WMI_ALL_APPS_KEY , data->input_dev->keybit); +/* "Settings" key */ +set_bit (WMI_SETUP_KEY , data->input_dev->keybit); +/* Custom (AI?) key */ +set_bit (WMI_CST_KEY , data->input_dev->keybit); + +return input_register_device (data->input_dev); +} + +static void press_and_release_key (struct input_dev *dev,unsigned int code) +{ +input_report_key (dev, code,1 ); +input_sync (dev); +input_report_key (dev, code,0 ); +input_sync (dev); +} + +static void redmi_wmi_notify (struct wmi_device *wdev,union acpi_object *obj) +{ +struct redmi_wmi *data =dev_get_drvdata (&wdev->dev); + +if (obj->type !=ACPI_TYPE_BUFFER ) { +dev_err (&wdev->dev,"Bad response type %u\n" , obj->type); +return ; + } + +if (obj->buffer.length <4 ) { +dev_err (&wdev->dev,"Invalid buffer length %u\n" , obj->buffer.length); +return ; + } + +/* For linearizability */ +guard (mutex)(&data->key_lock); + +u32 payload = ((u32 *)obj->buffer.pointer)[0 ]; + +switch (payload) { +case ACPI_CUT_PAYLOAD : +press_and_release_key (data->input_dev,WMI_CUT_KEY ); +break ; +case ACPI_ALL_APPS_PAYLOAD : +press_and_release_key (data->input_dev,WMI_ALL_APPS_KEY ); +break ; +case ACPI_SETUP_PAYLOAD : +press_and_release_key (data->input_dev,WMI_SETUP_KEY ); +break ; +case ACPI_CST_KEY_PRESS_PAYLOAD : +input_report_key (data->input_dev,WMI_CST_KEY ,1 ); +input_sync (data->input_dev); +break ; +case ACPI_CST_KEY_RELEASE_PAYLOAD : +input_report_key (data->input_dev,WMI_CST_KEY ,0 ); +input_sync (data->input_dev); +break ; +case BACKLIGHT_LEVEL_0_PAYLOAD : +case BACKLIGHT_LEVEL_1_PAYLOAD : +case BACKLIGHT_LEVEL_2_PAYLOAD : +case BACKLIGHT_LEVEL_3_PAYLOAD : +pr_debug ("keyboard backlight WMI event, no action" ); +break ; +default : +pr_debug ("unsupported Redmibook WMI event with 4byte payload %u" , payload); +break ; + } +} + +static const struct wmi_device_id redmi_wmi_id_table[] = { + { .guid_string =WMI_REDMIBOOK_KEYBOARD_EVENT_GUID }, +/* Terminating entry */ + { } +}; + +static struct wmi_driver redmi_wmi_driver = { + .driver = { + .name ="redmi-wmi" , + .probe_type =PROBE_PREFER_ASYNCHRONOUS + }, + .id_table = redmi_wmi_id_table, + .probe = redmi_wmi_probe, + .notify = redmi_wmi_notify, + .no_singleton = true, +}; +module_wmi_driver (redmi_wmi_driver); + +MODULE_DEVICE_TABLE (wmi, redmi_wmi_id_table); +MODULE_AUTHOR ("Gladyshev Ilya <[email protected]>" ); +MODULE_DESCRIPTION ("Redmibook WMI driver" ); +MODULE_LICENSE ("GPL" );
Gladyshev Ilya
maintainerPATCH 1/1 Add WMI driver for Redmibook keyboard.
This driver implements support for various Fn keys (like Cut) and Xiaomi specific AI button.
--- MAINTAINERS | 6 ++ drivers/platform/x86/Kconfig | 10 ++ drivers/platform/x86/Makefile | 1 + drivers/platform/x86/redmi-wmi.c | 164 +++++++++++++++++++++++++++++++ 4 files changed, 181 insertions(+) create mode 100644 drivers/platform/x86/redmi-wmi.c
MAINTAINERS +6 -0
@@ -20965,6 +20965,12 @@ S: Maintained T: git https://github.com/pkshih/rtw.git F: drivers/net/wireless/realtek/rtw89/ +REDMIBOOK WMI DRIVERS +M: Gladyshev Ilya <[email protected]> +L: [email protected] +S: Maintained +F: drivers/platform/x86/redmi-wmi.c + REDPINE WIRELESS DRIVER L: [email protected] S: Orphan
drivers/platform/x86/Kconfig +10 -0
@@ -109,6 +109,16 @@ config XIAOMI_WMI To compile this driver as a module, choose M here: the module will be called xiaomi-wmi. +config REDMI_WMI + tristate "Redmibook WMI key driver" + depends on ACPI_WMI + depends on INPUT + help + Say Y here if you want to support WMI-based keys on Redmibooks. + + To compile this driver as a module, choose M here: the module will + be called redmi-wmi. + config GIGABYTE_WMI tristate "Gigabyte WMI temperature driver" depends on ACPI_WMI
drivers/platform/x86/Makefile +1 -0
@@ -13,6 +13,7 @@ obj-$(CONFIG_HUAWEI_WMI) += huawei-wmi.o 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-$(CONFIG_GIGABYTE_WMI) += gigabyte-wmi.o # Acer
drivers/platform/x86/redmi-wmi.c new file +164 -0
164 lines not on this page · raw message
Signature · 1 line
2.50.0
Am 28.07.25 um 00:38 schrieb foxido:
Quoted 1 line
This is my first interaction with mailing lists in general and upstream linux development in particular, so I apologize for all mistakes in advance. Please correct me, if you see some "behaviour"-kinda errors
It OK to make mistakes, we all have been there :)
You can take a look at https://subspace.kernel.org/etiquette.html for more information.
Thanks,
Armin Wolf
Quoted 243 lines
Signature · 1 line
foxidoQuoted 238 lines · Gladyshev Ilya
This driver implements support for various Fn keys (like Cut) and Xiaomi specific AI button.
Signed-off-by Gladyshev Ilya <[email protected]>---
MAINTAINERS | 6 ++ drivers/platform/x86/Kconfig | 10 ++ drivers/platform/x86/Makefile | 1 +drivers/platform/x86/redmi-wmi.c| 164 +++++++++++++++++++++++++++++++4 files changed, 181 insertions(+)create mode 100644
drivers/platform/x86/redmi-wmi.cMAINTAINERS +0
-0@@ -20965,6 +20965,12 @@ S: MaintainedT: git https://github.com/pkshih/rtw.git
F:drivers/net/wireless/realtek/rtw89/
+REDMIBOOK WMI DRIVERSMAINTAINERS +5
-0+M: Gladyshev Ilya <[email protected]> +L: [email protected] +S: Maintained +F: drivers/platform/x86/redmi-wmi.c +REDPINE WIRELESS DRIVER
L: [email protected]
S: Orphandrivers/platform/x86/Kconfig +0
-0@@ -109,6 +109,16 @@ config XIAOMI_WMITo compile this driver as a module, choose M here: the module will be called xiaomi-wmi.
+configREDMI_WMI
+ tristate "Redmibook WMI key driver"
+ depends onACPI_WMI
+ depends on INPUT
+ help
+ Say Y here if you want to support WMI-based keys on Redmibooks. +
+ To compile this driver as a module, choose M here: the module will
+ be called redmi-wmi.
+
configGIGABYTE_WMI
tristate "Gigabyte WMI temperature driver"
depends onACPI_WMIdrivers/platform/x86/Makefile +0
-0@@ -13,6 +13,7 @@ obj-$(CONFIG_HUAWEI_WMI) += huawei-wmi.oobj-$(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-$(CONFIG_GIGABYTE_WMI) += gigabyte-wmi.o
# Acerdrivers/platform/x86/redmi-wmi.c new file +13
-0@@ -0,0 +1,164 @@ +// SPDX-License-Identifier: GPL-2.0 +/* WMI driver for Xiaomi Redmibooks */ + +#include <linux/acpi.h> +#include <linux/device.h> +#include <linux/input.h> +#include <linux/module.h> +#include <linux/mutex.h> +#include <linux/wmi.h> + +#include <uapi/linux/input-event-codes.h> + +#define WMI_REDMIBOOK_KEYBOARD_EVENT_GUID "46c93e13-ee9b-4262-8488-563bca757fef"
drivers/platform/x86/redmi-wmi.c new file +34
-0+ +/* Supported WMI keys ... */ +#define ACPI_CUT_PAYLOAD 0x00000201 +#define ACPI_ALL_APPS_PAYLOAD 0x00000301 +#define ACPI_SETUP_PAYLOAD 0x00001b01 +#define ACPI_CST_KEY_PRESS_PAYLOAD 0x00011801 +#define ACPI_CST_KEY_RELEASE_PAYLOAD 0x00011901 + +/* ... and their mappings */ +#define WMI_CUT_KEY KEY_PROG1 +#define WMI_ALL_APPS_KEY KEY_ALL_APPLICATIONS +#define WMI_SETUP_KEY KEY_SETUP +#define WMI_CST_KEY KEY_ASSISTANT + +/* Keyboard backlight key (not supported yet) */ +#define BACKLIGHT_LEVEL_0_PAYLOAD 0x00000501 +#define BACKLIGHT_LEVEL_1_PAYLOAD 0x00800501 +#define BACKLIGHT_LEVEL_2_PAYLOAD 0x00050501 +#define BACKLIGHT_LEVEL_3_PAYLOAD 0x000a0501 + +struct redmi_wmi { + struct input_dev *input_dev; + /* Protects the key event sequence */ + struct mutex key_lock; +}; + +static void redmi_mutex_destroy (void *data) +{ + struct mutex *lock = data; + + mutex_destroy(lock); +} + +static int redmi_wmi_probe(struct wmi_device *wdev, const void*context)
drivers/platform/x86/redmi-wmi.c new file +6
-0+{ + struct redmi_wmi *data; + int ret; + + /* Init dev */ + data = devm_kzalloc(&wdev->dev, sizeof(struct redmi_wmi),
GFP_KERNEL);
+ if (!data)
+ return-ENOMEM;drivers/platform/x86/redmi-wmi.c new file +6
-0+ + dev_set_drvdata(&wdev->dev, data); + + /* Init mutex & setup destroy at exit */ + mutex_init(&data->key_lock); + ret = devm_add_action_or_reset(&wdev->dev, redmi_mutex_destroy,
&data->key_lock);
+ if (ret < 0)
+ return ret;drivers/platform/x86/redmi-wmi.c new file +23
-0+ + /* Setup input device */ + data->input_dev = devm_input_allocate_device(&wdev->dev); + if (!data->input_dev) + return -ENOMEM; + data->input_dev->name = "Redmibook WMI keys"; + data->input_dev->phys = "wmi/input0"; + + set_bit(EV_KEY, data->input_dev->evbit); + + /* "Cut" key*/ + set_bit(WMI_CUT_KEY, data->input_dev->keybit); + /* "All apps" key*/ + set_bit(WMI_ALL_APPS_KEY, data->input_dev->keybit); + /* "Settings" key */ + set_bit(WMI_SETUP_KEY, data->input_dev->keybit); + /* Custom (AI?) key */ + set_bit(WMI_CST_KEY, data->input_dev->keybit); + + return input_register_device(data->input_dev); +} + +static void press_and_release_key(struct input_dev *dev, unsignedint code)
drivers/platform/x86/redmi-wmi.c new file +8
-0+{ + input_report_key(dev, code, 1); + input_sync(dev); + input_report_key(dev, code, 0); + input_sync(dev); +} + +static void redmi_wmi_notify(struct wmi_device *wdev, union
acpi_object*obj)drivers/platform/x86/redmi-wmi.c new file +10
-0+{ + struct redmi_wmi *data = dev_get_drvdata(&wdev->dev); + + if (obj->type != ACPI_TYPE_BUFFER) { + dev_err(&wdev->dev, "Bad response type %u\n", obj->type); + return; + } + + if (obj->buffer.length < 4) { + dev_err(&wdev->dev, "Invalid buffer length %u\n",
obj->buffer.length);
+ return;
+ }drivers/platform/x86/redmi-wmi.c new file +32
-0+ + /* For linearizability */ + guard(mutex)(&data->key_lock); + + u32 payload = ((u32 *)obj->buffer.pointer)[0]; + + switch (payload) { + case ACPI_CUT_PAYLOAD: + press_and_release_key(data->input_dev, WMI_CUT_KEY); + break; + case ACPI_ALL_APPS_PAYLOAD: + press_and_release_key(data->input_dev, WMI_ALL_APPS_KEY); + break; + case ACPI_SETUP_PAYLOAD: + press_and_release_key(data->input_dev, WMI_SETUP_KEY); + break; + case ACPI_CST_KEY_PRESS_PAYLOAD: + input_report_key(data->input_dev, WMI_CST_KEY, 1); + input_sync(data->input_dev); + break; + case ACPI_CST_KEY_RELEASE_PAYLOAD: + input_report_key(data->input_dev, WMI_CST_KEY, 0); + input_sync(data->input_dev); + break; + case BACKLIGHT_LEVEL_0_PAYLOAD: + case BACKLIGHT_LEVEL_1_PAYLOAD: + case BACKLIGHT_LEVEL_2_PAYLOAD: + case BACKLIGHT_LEVEL_3_PAYLOAD: + pr_debug("keyboard backlight WMI event, no action"); + break; + default: + pr_debug("unsupported Redmibook WMI event with 4byte payload%u", payload);
+ break;
+ }drivers/platform/x86/redmi-wmi.c new file +24
-0+} + +static const struct wmi_device_id redmi_wmi_id_table[] = { + { .guid_string = WMI_REDMIBOOK_KEYBOARD_EVENT_GUID }, + /* Terminating entry */ + { } +}; + +static struct wmi_driver redmi_wmi_driver = { + .driver = { + .name = "redmi-wmi", + .probe_type = PROBE_PREFER_ASYNCHRONOUS + }, + .id_table = redmi_wmi_id_table, + .probe = redmi_wmi_probe, + .notify = redmi_wmi_notify, + .no_singleton = true, +}; +module_wmi_driver (redmi_wmi_driver); + +MODULE_DEVICE_TABLE (wmi, redmi_wmi_id_table); +MODULE_AUTHOR ("Gladyshev Ilya <[email protected]>" ); +MODULE_DESCRIPTION ("Redmibook WMI driver" ); +MODULE_LICENSE ("GPL" );
Am 28.07.25 um 00:34 schrieb Gladyshev Ilya:
Quoted 2 lines
This driver implements support for various Fn keys (like Cut) and Xiaomi specific AI button.
Interesting, i was just talking with another person about implementing a WMI event driver for the exact same WMI event device. I CCed the person involved in the discussion so that he can test this driver on his device as well.
All in all the driver looks promising, but there are still things that need to be improved before we can include this driver in the mainline kernel. For details see below.
Also please CC the linux input mailing list in the future so that they can give feedback as well.
Quoted 31 lines
Signed-off-by Gladyshev Ilya <[email protected]>--- MAINTAINERS | 6 ++ drivers/platform/x86/Kconfig | 10 ++ drivers/platform/x86/Makefile | 1 + drivers/platform/x86/redmi-wmi.c | 164 +++++++++++++++++++++++++++++++ 4 files changed, 181 insertions(+)create mode 100644
drivers/platform/x86/redmi-wmi.cMAINTAINERS +6
-0@@ -20965,6 +20965,12 @@ S: Maintained T: git https://github.com/pkshih/rtw.git F: drivers/net/wireless/realtek/rtw89/ +REDMIBOOK WMI DRIVERS +M: Gladyshev Ilya <[email protected]> +L: [email protected] +S: Maintained +F: drivers/platform/x86/redmi-wmi.c + REDPINE WIRELESS DRIVER L: [email protected] S: Orphandrivers/platform/x86/Kconfig +0
-0
drivers/platform/x86/Kconfig config XIAOMI_WMI · line 117
Quoted 9 lines
drivers/platform/x86/Kconfig +6
-0@@ -109,6 +109,16 @@ config XIAOMI_WMI To compile this driver as a module, choose M here: the module will be called xiaomi-wmi. +config REDMI_WMI + tristate "Redmibook WMI key driver" + depends on ACPI_WMI + depends on INPUT + help + Say Y here if you want to support WMI-based keys on Redmibooks.
"Say Y here if you want support for WMI-based hotkey events on Xiaomi Redmi devices."
Quoted 42 lines
drivers/platform/x86/Kconfig +4
-0+ + To compile this driver as a module, choose M here: the module will + be called redmi-wmi. + config GIGABYTE_WMI tristate "Gigabyte WMI temperature driver" depends on ACPI_WMIdrivers/platform/x86/Makefile +1
-0@@ -13,6 +13,7 @@ obj-$(CONFIG_HUAWEI_WMI) += huawei-wmi.o 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-$(CONFIG_GIGABYTE_WMI) += gigabyte-wmi.o # Acerdrivers/platform/x86/redmi-wmi.c new file +17
-0@@ -0,0 +1,164 @@ +// SPDX-License-Identifier: GPL-2.0 +/* WMI driver for Xiaomi Redmibooks */ + +#include <linux/acpi.h> +#include <linux/device.h> +#include <linux/input.h> +#include <linux/module.h> +#include <linux/mutex.h> +#include <linux/wmi.h> + +#include <uapi/linux/input-event-codes.h> + +#define WMI_REDMIBOOK_KEYBOARD_EVENT_GUID "46c93e13-ee9b-4262-8488-563bca757fef" + +/* Supported WMI keys ... */ +#define ACPI_CUT_PAYLOAD 0x00000201 +#define ACPI_ALL_APPS_PAYLOAD 0x00000301
drivers/platform/x86/redmi-wmi.c line 27
Quoted 10 lines
drivers/platform/x86/redmi-wmi.c new file +10
-0+#define ACPI_SETUP_PAYLOAD 0x00001b01 +#define ACPI_CST_KEY_PRESS_PAYLOAD 0x00011801 +#define ACPI_CST_KEY_RELEASE_PAYLOAD 0x00011901 + +/* ... and their mappings */ +#define WMI_CUT_KEY KEY_PROG1 +#define WMI_ALL_APPS_KEY KEY_ALL_APPLICATIONS +#define WMI_SETUP_KEY KEY_SETUP +#define WMI_CST_KEY KEY_ASSISTANT +
Why not using sparse-keymap for this?
Quoted 16 lines
drivers/platform/x86/redmi-wmi.c new file +16
-0+/* Keyboard backlight key (not supported yet) */ +#define BACKLIGHT_LEVEL_0_PAYLOAD 0x00000501 +#define BACKLIGHT_LEVEL_1_PAYLOAD 0x00800501 +#define BACKLIGHT_LEVEL_2_PAYLOAD 0x00050501 +#define BACKLIGHT_LEVEL_3_PAYLOAD 0x000a0501 + +struct redmi_wmi { +struct input_dev *input_dev; +/* Protects the key event sequence */ +struct mutex key_lock; +}; + +static void redmi_mutex_destroy (void *data) +{ +struct mutex *lock = data; +
drivers/platform/x86/redmi-wmi.c line 53
Quoted 10 lines
drivers/platform/x86/redmi-wmi.c new file +10
-0+mutex_destroy (lock); +} + +static int redmi_wmi_probe (struct wmi_device *wdev,const void *context) +{ +struct redmi_wmi *data; +int ret; + +/* Init dev */ + data =devm_kzalloc (&wdev->dev,sizeof (struct redmi_wmi ),GFP_KERNEL );
sizeof(struct redmi_wmi) -> sizeof(*data)
drivers/platform/x86/redmi-wmi.c line 63
Quoted 10 lines
drivers/platform/x86/redmi-wmi.c new file +10
-0+if (!data) +return -ENOMEM ; + +dev_set_drvdata (&wdev->dev, data); + +/* Init mutex & setup destroy at exit */ +mutex_init (&data->key_lock); + ret =devm_add_action_or_reset (&wdev->dev, redmi_mutex_destroy, &data->key_lock); +if (ret <0 ) +return ret;
Please use devm_mutex_init() instead. You can also drop the comment then.
Quoted 8 lines
drivers/platform/x86/redmi-wmi.c new file +8
-0+ +/* Setup input device */ + data->input_dev =devm_input_allocate_device (&wdev->dev); +if (!data->input_dev) +return -ENOMEM ; + data->input_dev->name ="Redmibook WMI keys" ; + data->input_dev->phys ="wmi/input0" ; +
drivers/platform/x86/redmi-wmi.c line 81
Quoted 10 lines
drivers/platform/x86/redmi-wmi.c new file +10
-0+set_bit (EV_KEY , data->input_dev->evbit); + +/* "Cut" key*/ +set_bit (WMI_CUT_KEY , data->input_dev->keybit); +/* "All apps" key*/ +set_bit (WMI_ALL_APPS_KEY , data->input_dev->keybit); +/* "Settings" key */ +set_bit (WMI_SETUP_KEY , data->input_dev->keybit); +/* Custom (AI?) key */ +set_bit (WMI_CST_KEY , data->input_dev->keybit);
Please use sparse-keymap for setting up all of this.
drivers/platform/x86/redmi-wmi.c line 92
Quoted 11 lines
drivers/platform/x86/redmi-wmi.c new file +11
-0+ +return input_register_device (data->input_dev); +} + +static void press_and_release_key (struct input_dev *dev,unsigned int code) +{ +input_report_key (dev, code,1 ); +input_sync (dev); +input_report_key (dev, code,0 ); +input_sync (dev); +}
Using sparse-keymap would allow you to drop this function and instead rely on the autorelease functionality of sparse-keymap instead.
Quoted 4 lines
drivers/platform/x86/redmi-wmi.c new file +4
-0+ +static void redmi_wmi_notify (struct wmi_device *wdev,union acpi_object *obj) +{ +struct redmi_wmi *data =dev_get_drvdata (&wdev->dev);
drivers/platform/x86/redmi-wmi.c line 106
Quoted 10 lines
drivers/platform/x86/redmi-wmi.c new file +10
-0+ +if (obj->type !=ACPI_TYPE_BUFFER ) { +dev_err (&wdev->dev,"Bad response type %u\n" , obj->type); +return ; + } + +if (obj->buffer.length <4 ) { +dev_err (&wdev->dev,"Invalid buffer length %u\n" , obj->buffer.length); +return ; + }
The MOF description of the WMI event looks like this:
class WMIEvent : __ExtrinsicEvent {
};
[WMI, Dynamic, Provider("WmiProv"), Locale("MS\\0x40A"), Description("Root/WMI/HID_EVENT20"), guid("{46c93e13-ee9b-4262-8488-563bca757fef}")] class HID_EVENT20 : WmiEvent {
[key, read] string InstanceName;
[read] boolean Active;
[WmiDataId(1), read, write, Description("Package Data")] uint8 EventDetail[32];
};
As you can see the buffer should contain at least 32 bytes. Please check this even when you are only using the first 4 bytes as some WMI events might return fewer than 32 bytes of data in order to signal a firmware error.
drivers/platform/x86/redmi-wmi.c line 111
Quoted 5 lines
drivers/platform/x86/redmi-wmi.c new file +5
-0+ +/* For linearizability */ +guard (mutex)(&data->key_lock); + +u32 payload = ((u32 *)obj->buffer.pointer)[0 ];
Please use get_unaligned_le32() from linux/unaligned.h as the buffer might not be properly aligned for 32 bit integers.
Quoted 17 lines
drivers/platform/x86/redmi-wmi.c new file +17
-0+ +switch (payload) { +case ACPI_CUT_PAYLOAD : +press_and_release_key (data->input_dev,WMI_CUT_KEY ); +break ; +case ACPI_ALL_APPS_PAYLOAD : +press_and_release_key (data->input_dev,WMI_ALL_APPS_KEY ); +break ; +case ACPI_SETUP_PAYLOAD : +press_and_release_key (data->input_dev,WMI_SETUP_KEY ); +break ; +case ACPI_CST_KEY_PRESS_PAYLOAD : +input_report_key (data->input_dev,WMI_CST_KEY ,1 ); +input_sync (data->input_dev); +break ; +case ACPI_CST_KEY_RELEASE_PAYLOAD : +input_report_key (data->input_dev,WMI_CST_KEY ,0 );
drivers/platform/x86/redmi-wmi.c line 138
Quoted 10 lines
drivers/platform/x86/redmi-wmi.c new file +10
-0+input_sync (data->input_dev); +break ; +case BACKLIGHT_LEVEL_0_PAYLOAD : +case BACKLIGHT_LEVEL_1_PAYLOAD : +case BACKLIGHT_LEVEL_2_PAYLOAD : +case BACKLIGHT_LEVEL_3_PAYLOAD : +pr_debug ("keyboard backlight WMI event, no action" ); +break ; +default : +pr_debug ("unsupported Redmibook WMI event with 4byte payload %u" , payload);
Please use dev_dbg() here. Also using sparse-keymap would allow you to skip all of this and instead let the input subsystem do the matching.
drivers/platform/x86/redmi-wmi.c line 145
Quoted 7 lines
drivers/platform/x86/redmi-wmi.c new file +7
-0+break ; + } +} + +static const struct wmi_device_id redmi_wmi_id_table[] = { + { .guid_string =WMI_REDMIBOOK_KEYBOARD_EVENT_GUID }, +/* Terminating entry */
Pointless commit, please remove.
Thanks,
Armin Wolf
Quoted 19 lines
drivers/platform/x86/redmi-wmi.c new file +19
-0+ { } +}; + +static struct wmi_driver redmi_wmi_driver = { + .driver = { + .name ="redmi-wmi" , + .probe_type =PROBE_PREFER_ASYNCHRONOUS + }, + .id_table = redmi_wmi_id_table, + .probe = redmi_wmi_probe, + .notify = redmi_wmi_notify, + .no_singleton = true, +}; +module_wmi_driver (redmi_wmi_driver); + +MODULE_DEVICE_TABLE (wmi, redmi_wmi_id_table); +MODULE_AUTHOR ("Gladyshev Ilya <[email protected]>" ); +MODULE_DESCRIPTION ("Redmibook WMI driver" ); +MODULE_LICENSE ("GPL" );
Gladyshev Ilya
maintainerQuoted 10 lines · Armin Wolf
Am 28.07.25 um 00:34 schrieb Gladyshev Ilya:
Quoted 2 lines
This driver implements support for various Fn keys (like Cut) and Xiaomi specific AI button.
Interesting, i was just talking with another person about implementing a WMI event driver for the exact same WMI event device. I CCed the person involved in the discussion so that he can test this driver on his device as well.
All in all the driver looks promising, but there are still things that need to be improved before we can include this driver in the mainline kernel. For details see below.
Thanks for your feedback, will fix in v2. However, I have small question: do I still need a mutex for linearizability if I implement driver via sparse-keymap? I've copied mutex from xiaomi-wmi, but as I looked up not all WMI keyboard drivers use it (fujitsu-laptop, acer-wmi).
--
Gladyshev Ilya
Quoted 1 line · On Tue, Jul 29, 2025 at 12:47:57AM +0300 Armin Wolf wrote:
"Say Y here if you want support for WMI-based hotkey events on Xiaomi Redmi devices."
I think it should be "Xiaomi Redmibook" instead. "Xiaomi Redmi" is associated with mobiles devices. See Google Images for example of this:
[1] and [2].
[1]: https://www.google.com/search?q=xiaomi+redmi&udm=2
[2]: https://www.google.com/search?q=xiaomi+redmibook&udm=2
Signature · 1 line
Nikita Krasnov
Am 29.07.25 um 10:37 schrieb Gladyshev Ilya:
Quoted 6 lines · Armin Wolf
Quoted 5 lines
Am 28.07.25 um 00:34 schrieb Gladyshev Ilya:
Quoted 2 lines
This driver implements support for various Fn keys (like Cut) and Xiaomi specific AI button.
Quoted 7 lines
Quoted 5 lines
Interesting, i was just talking with another person about implementing a WMI event driver for the exact same WMI event device. I CCed the person involved in the discussion so that he can test this driver on his device as well.
All in all the driver looks promising, but there are still things that need to be improved before we can include this driver in the mainline kernel. For details see below.Thanks for your feedback, will fix in v2. However, I have small question: do I still need a mutex for linearizability if I implement driver via sparse-keymap? I've copied mutex from xiaomi-wmi, but as I looked up not all WMI keyboard drivers use it (fujitsu-laptop, acer-wmi).
Yes, the mutex is still necessary unless the event handler itself is already protected by some sort of locking. Many older WMI driver are not using a mutex for legacy reasons, but new drivers should.
Thanks,
Armin Wolf
Quoted 3 lines
Signature · 1 line
Gladyshev Ilya