1 // SPDX-License-Identifier: GPL-2.0
2 /* WMI driver for Xiaomi Laptops */
4 #include <linux/acpi.h>
5 #include <linux/device.h>
6 #include <linux/input.h>
7 #include <linux/module.h>
8 #include <linux/mutex.h>
11 #include <uapi/linux/input-event-codes.h>
13 #define XIAOMI_KEY_FN_ESC_0 "A2095CCE-0491-44E7-BA27-F8ED8F88AA86"
14 #define XIAOMI_KEY_FN_ESC_1 "7BBE8E39-B486-473D-BA13-66F75C5805CD"
15 #define XIAOMI_KEY_FN_FN "409B028D-F06B-4C7C-8BBB-EE133A6BD87E"
16 #define XIAOMI_KEY_CAPSLOCK "83FE7607-053A-4644-822A-21532C621FC7"
17 #define XIAOMI_KEY_FN_F7 "76E9027C-95D0-4180-8692-DA6747DD1C2D"
19 #define XIAOMI_DEVICE(guid, key) \
20 .guid_string = (guid), \
21 .context = &(const unsigned int){key}
24 struct input_dev
*input_dev
;
25 struct mutex key_lock
; /* Protects the key event sequence */
26 unsigned int key_code
;
29 static void xiaomi_mutex_destroy(void *data
)
31 struct mutex
*lock
= data
;
36 static int xiaomi_wmi_probe(struct wmi_device
*wdev
, const void *context
)
38 struct xiaomi_wmi
*data
;
44 data
= devm_kzalloc(&wdev
->dev
, sizeof(struct xiaomi_wmi
), GFP_KERNEL
);
47 dev_set_drvdata(&wdev
->dev
, data
);
49 mutex_init(&data
->key_lock
);
50 ret
= devm_add_action_or_reset(&wdev
->dev
, xiaomi_mutex_destroy
, &data
->key_lock
);
54 data
->input_dev
= devm_input_allocate_device(&wdev
->dev
);
55 if (data
->input_dev
== NULL
)
57 data
->input_dev
->name
= "Xiaomi WMI keys";
58 data
->input_dev
->phys
= "wmi/input0";
60 data
->key_code
= *((const unsigned int *)context
);
61 set_bit(EV_KEY
, data
->input_dev
->evbit
);
62 set_bit(data
->key_code
, data
->input_dev
->keybit
);
64 return input_register_device(data
->input_dev
);
67 static void xiaomi_wmi_notify(struct wmi_device
*wdev
, union acpi_object
*dummy
)
69 struct xiaomi_wmi
*data
= dev_get_drvdata(&wdev
->dev
);
71 mutex_lock(&data
->key_lock
);
72 input_report_key(data
->input_dev
, data
->key_code
, 1);
73 input_sync(data
->input_dev
);
74 input_report_key(data
->input_dev
, data
->key_code
, 0);
75 input_sync(data
->input_dev
);
76 mutex_unlock(&data
->key_lock
);
79 static const struct wmi_device_id xiaomi_wmi_id_table
[] = {
80 // { XIAOMI_DEVICE(XIAOMI_KEY_FN_ESC_0, KEY_FN_ESC) },
81 // { XIAOMI_DEVICE(XIAOMI_KEY_FN_ESC_1, KEY_FN_ESC) },
82 { XIAOMI_DEVICE(XIAOMI_KEY_FN_FN
, KEY_PROG1
) },
83 // { XIAOMI_DEVICE(XIAOMI_KEY_CAPSLOCK, KEY_CAPSLOCK) },
84 { XIAOMI_DEVICE(XIAOMI_KEY_FN_F7
, KEY_CUT
) },
86 /* Terminating entry */
90 static struct wmi_driver xiaomi_wmi_driver
= {
94 .id_table
= xiaomi_wmi_id_table
,
95 .probe
= xiaomi_wmi_probe
,
96 .notify
= xiaomi_wmi_notify
,
99 module_wmi_driver(xiaomi_wmi_driver
);
101 MODULE_DEVICE_TABLE(wmi
, xiaomi_wmi_id_table
);
102 MODULE_AUTHOR("Mattias Jacobsson");
103 MODULE_DESCRIPTION("Xiaomi WMI driver");
104 MODULE_LICENSE("GPL v2");