-
Notifications
You must be signed in to change notification settings - Fork 2
/
ideapad-wmi-fn-keys.c
173 lines (144 loc) · 4.44 KB
/
ideapad-wmi-fn-keys.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* ideapad-wmi-fn-keys.c - Ideapad WMI fn keys driver
*
* Supported models:
* - Lenovo Yoga 9 14IAP7
* - Lenovo Yoga 9 14ITL5
*
* Copyright (C) 2022 Philipp Jungkamp <[email protected]>
* Copyright (C) 2022 Ulrich Huber <[email protected]>
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/acpi.h>
#include <linux/input.h>
#include <linux/input/sparse-keymap.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/wmi.h>
#define IDEAPAD_FN_KEY_EVENT_GUID "8FC0DE0C-B4E4-43FD-B0F3-8871711C1294"
struct ideapad_wmi_private {
struct wmi_device *wmi_device;
struct input_dev *input_dev;
};
static const struct key_entry ideapad_wmi_fn_key_keymap[] = {
/* Customizable Lenovo Hotkey (Acts on Windows as macro key) ("star" with 'S' inside) */
{ KE_KEY, 0x01, { KEY_PROG1 } },
/* Disable FnLock (handled by the firmware) */
{ KE_IGNORE, 0x02 },
/* Enable FnLock (handled by the firmware) */
{ KE_IGNORE, 0x03 },
/*
* Snipping (dashed circle with scissors)
*
* Better fit would be KEY_SELECTIVE_SCREENSHOT, but:
* - Not supported by xorg-x11proto:
* https://github.com/freedesktop/xorg-x11proto/blob/master/XF86keysym.h
* - Not supported by Qt:
* https://doc.qt.io/qt-6/qt.html#Key-enum
* - Not supported by KDE:
* https://github.com/KDE/kwindowsystem/blob/9d5cf1a99f71ce2b0efd608c6899171c6ce4e25d/src/platforms/xcb/kkeyserver.cpp
*/
{ KE_KEY, 0x04, { KEY_F14 } },
/* Customizable Lenovo Hotkey ("star" with 'S' inside) (long-press) */
{ KE_KEY, 0x08, { KEY_FAVORITES } },
/* Sound profile switch */
{ KE_KEY, 0x12, { KEY_PROG2 } },
/* Dark mode toggle */
{ KE_KEY, 0x13, { KEY_PROG3 } },
/* Lenovo Support */
{ KE_KEY, 0x27, { KEY_HELP } },
/* Lenovo Virtual Background application */
{ KE_KEY, 0x28, { KEY_PROG4 } },
{ KE_END },
};
static int ideapad_wmi_input_init(struct ideapad_wmi_private *priv)
{
struct input_dev *input_dev;
int err;
input_dev = input_allocate_device();
if (!input_dev) {
return -ENOMEM;
}
input_dev->name = "Ideapad WMI Fn Keys";
input_dev->phys = IDEAPAD_FN_KEY_EVENT_GUID "/input0";
input_dev->id.bustype = BUS_HOST;
input_dev->dev.parent = &priv->wmi_device->dev;
err = sparse_keymap_setup(input_dev, ideapad_wmi_fn_key_keymap, NULL);
if (err) {
dev_err(&priv->wmi_device->dev,
"Could not set up input device keymap: %d\n", err);
goto err_free_dev;
}
err = input_register_device(input_dev);
if (err) {
dev_err(&priv->wmi_device->dev,
"Could not register input device: %d\n", err);
goto err_free_dev;
}
priv->input_dev = input_dev;
return 0;
err_free_dev:
input_free_device(input_dev);
return err;
}
static void ideapad_wmi_input_exit(struct ideapad_wmi_private *priv)
{
input_unregister_device(priv->input_dev);
priv->input_dev = NULL;
}
static void ideapad_wmi_input_report(struct ideapad_wmi_private *priv,
unsigned int scancode)
{
if (!sparse_keymap_report_event(priv->input_dev, scancode, 1, true))
pr_info("ideapad-wmi-fn-keys: Unknown scancode %x\n", scancode);
}
static int ideapad_wmi_probe(struct wmi_device *wdev, const void *ctx)
{
struct ideapad_wmi_private *priv;
int err;
priv = devm_kzalloc(&wdev->dev, sizeof(*priv), GFP_KERNEL);
if (!priv)
return -ENOMEM;
dev_set_drvdata(&wdev->dev, priv);
priv->wmi_device = wdev;
err = ideapad_wmi_input_init(priv);
if (err)
return err;
return 0;
}
static void ideapad_wmi_remove(struct wmi_device *wdev)
{
struct ideapad_wmi_private *priv = dev_get_drvdata(&wdev->dev);
ideapad_wmi_input_exit(priv);
}
static void ideapad_wmi_notify(struct wmi_device *wdev, union acpi_object *data)
{
struct ideapad_wmi_private *priv = dev_get_drvdata(&wdev->dev);
if(data->type != ACPI_TYPE_INTEGER) {
dev_warn(&priv->wmi_device->dev,
"WMI event data is not an integer\n");
return;
}
ideapad_wmi_input_report(priv, data->integer.value);
}
static const struct wmi_device_id ideapad_wmi_id_table[] = {
{ /* Special Keys on the Yoga 9 14IAP7 */
.guid_string = IDEAPAD_FN_KEY_EVENT_GUID
},
{ }
};
static struct wmi_driver ideapad_wmi_driver = {
.driver = {
.name = "ideapad-wmi-fn-keys",
},
.id_table = ideapad_wmi_id_table,
.probe = ideapad_wmi_probe,
.remove = ideapad_wmi_remove,
.notify = ideapad_wmi_notify,
};
module_wmi_driver(ideapad_wmi_driver);
MODULE_DEVICE_TABLE(wmi, ideapad_wmi_id_table);
MODULE_AUTHOR("Ulrich Huber <[email protected]>");
MODULE_DESCRIPTION("Ideapad WMI fn keys driver");
MODULE_LICENSE("GPL");