1 // SPDX-License-Identifier: GPL-2.0
2 /* WMI driver for Xiaomi Laptops */
3
4 #include <linux/acpi.h>
5 #include <linux/input.h>
6 #include <linux/module.h>
7 #include <linux/wmi.h>
8
9 #include <uapi/linux/input-event-codes.h>
10
11 #define XIAOMI_KEY_FN_ESC_0 "A2095CCE-0491-44E7-BA27-F8ED8F88AA86"
12 #define XIAOMI_KEY_FN_ESC_1 "7BBE8E39-B486-473D-BA13-66F75C5805CD"
13 #define XIAOMI_KEY_FN_FN "409B028D-F06B-4C7C-8BBB-EE133A6BD87E"
14 #define XIAOMI_KEY_CAPSLOCK "83FE7607-053A-4644-822A-21532C621FC7"
15 #define XIAOMI_KEY_FN_F7 "76E9027C-95D0-4180-8692-DA6747DD1C2D"
16
17 #define XIAOMI_DEVICE(guid, key) \
18 .guid_string = (guid), \
19 .context = &(const unsigned int){key}
20
21 struct xiaomi_wmi {
22 struct input_dev *input_dev;
23 unsigned int key_code;
24 };
25
xiaomi_wmi_probe(struct wmi_device * wdev,const void * context)26 static int xiaomi_wmi_probe(struct wmi_device *wdev, const void *context)
27 {
28 struct xiaomi_wmi *data;
29
30 if (wdev == NULL || context == NULL)
31 return -EINVAL;
32
33 data = devm_kzalloc(&wdev->dev, sizeof(struct xiaomi_wmi), GFP_KERNEL);
34 if (data == NULL)
35 return -ENOMEM;
36 dev_set_drvdata(&wdev->dev, data);
37
38 data->input_dev = devm_input_allocate_device(&wdev->dev);
39 if (data->input_dev == NULL)
40 return -ENOMEM;
41 data->input_dev->name = "Xiaomi WMI keys";
42 data->input_dev->phys = "wmi/input0";
43
44 data->key_code = *((const unsigned int *)context);
45 set_bit(EV_KEY, data->input_dev->evbit);
46 set_bit(data->key_code, data->input_dev->keybit);
47
48 return input_register_device(data->input_dev);
49 }
50
xiaomi_wmi_notify(struct wmi_device * wdev,union acpi_object * dummy)51 static void xiaomi_wmi_notify(struct wmi_device *wdev, union acpi_object *dummy)
52 {
53 struct xiaomi_wmi *data;
54
55 if (wdev == NULL)
56 return;
57
58 data = dev_get_drvdata(&wdev->dev);
59 if (data == NULL)
60 return;
61
62 input_report_key(data->input_dev, data->key_code, 1);
63 input_sync(data->input_dev);
64 input_report_key(data->input_dev, data->key_code, 0);
65 input_sync(data->input_dev);
66 }
67
68 static const struct wmi_device_id xiaomi_wmi_id_table[] = {
69 // { XIAOMI_DEVICE(XIAOMI_KEY_FN_ESC_0, KEY_FN_ESC) },
70 // { XIAOMI_DEVICE(XIAOMI_KEY_FN_ESC_1, KEY_FN_ESC) },
71 { XIAOMI_DEVICE(XIAOMI_KEY_FN_FN, KEY_PROG1) },
72 // { XIAOMI_DEVICE(XIAOMI_KEY_CAPSLOCK, KEY_CAPSLOCK) },
73 { XIAOMI_DEVICE(XIAOMI_KEY_FN_F7, KEY_CUT) },
74
75 /* Terminating entry */
76 { }
77 };
78
79 static struct wmi_driver xiaomi_wmi_driver = {
80 .driver = {
81 .name = "xiaomi-wmi",
82 },
83 .id_table = xiaomi_wmi_id_table,
84 .probe = xiaomi_wmi_probe,
85 .notify = xiaomi_wmi_notify,
86 };
87 module_wmi_driver(xiaomi_wmi_driver);
88
89 MODULE_DEVICE_TABLE(wmi, xiaomi_wmi_id_table);
90 MODULE_AUTHOR("Mattias Jacobsson");
91 MODULE_DESCRIPTION("Xiaomi WMI driver");
92 MODULE_LICENSE("GPL v2");
93