1 // SPDX-License-Identifier: GPL-2.0
2 /* WMI driver for Xiaomi Laptops */
3
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>
9 #include <linux/wmi.h>
10
11 #include <uapi/linux/input-event-codes.h>
12
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"
18
19 #define XIAOMI_DEVICE(guid, key) \
20 .guid_string = (guid), \
21 .context = &(const unsigned int){key}
22
23 struct xiaomi_wmi {
24 struct input_dev *input_dev;
25 struct mutex key_lock; /* Protects the key event sequence */
26 unsigned int key_code;
27 };
28
xiaomi_mutex_destroy(void * data)29 static void xiaomi_mutex_destroy(void *data)
30 {
31 struct mutex *lock = data;
32
33 mutex_destroy(lock);
34 }
35
xiaomi_wmi_probe(struct wmi_device * wdev,const void * context)36 static int xiaomi_wmi_probe(struct wmi_device *wdev, const void *context)
37 {
38 struct xiaomi_wmi *data;
39 int ret;
40
41 if (!context)
42 return -EINVAL;
43
44 data = devm_kzalloc(&wdev->dev, sizeof(struct xiaomi_wmi), GFP_KERNEL);
45 if (data == NULL)
46 return -ENOMEM;
47 dev_set_drvdata(&wdev->dev, data);
48
49 mutex_init(&data->key_lock);
50 ret = devm_add_action_or_reset(&wdev->dev, xiaomi_mutex_destroy, &data->key_lock);
51 if (ret < 0)
52 return ret;
53
54 data->input_dev = devm_input_allocate_device(&wdev->dev);
55 if (data->input_dev == NULL)
56 return -ENOMEM;
57 data->input_dev->name = "Xiaomi WMI keys";
58 data->input_dev->phys = "wmi/input0";
59
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);
63
64 return input_register_device(data->input_dev);
65 }
66
xiaomi_wmi_notify(struct wmi_device * wdev,union acpi_object * dummy)67 static void xiaomi_wmi_notify(struct wmi_device *wdev, union acpi_object *dummy)
68 {
69 struct xiaomi_wmi *data = dev_get_drvdata(&wdev->dev);
70
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);
77 }
78
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) },
85
86 /* Terminating entry */
87 { }
88 };
89
90 static struct wmi_driver xiaomi_wmi_driver = {
91 .driver = {
92 .name = "xiaomi-wmi",
93 },
94 .id_table = xiaomi_wmi_id_table,
95 .probe = xiaomi_wmi_probe,
96 .notify = xiaomi_wmi_notify,
97 .no_singleton = true,
98 };
99 module_wmi_driver(xiaomi_wmi_driver);
100
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");
105