-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathevents.c
400 lines (313 loc) · 8.95 KB
/
events.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// SPDX-License-Identifier: GPL-2.0
#include "pr.h"
#include <acpi/video.h>
#include <dt-bindings/leds/common.h>
#include <linux/acpi.h>
#include <linux/init.h>
#include <linux/input.h>
#include <linux/input/sparse-keymap.h>
#include <linux/leds.h>
#include <linux/version.h>
#include "events.h"
#include "misc.h"
#include "pdev.h"
#include "wmi.h"
/* ========================================================================== */
#define KBD_BL_LED_SUFFIX ":" LED_FUNCTION_KBD_BACKLIGHT
/* ========================================================================== */
static struct {
const char *guid;
bool handler_installed;
} qc71_wmi_event_guids[] = {
{ .guid = QC71_WMI_EVENT_70_GUID },
{ .guid = QC71_WMI_EVENT_71_GUID },
{ .guid = QC71_WMI_EVENT_72_GUID },
};
static const struct key_entry qc71_wmi_hotkeys[] = {
/* reported via keyboard controller */
{ KE_IGNORE, 0x01, { KEY_CAPSLOCK }},
{ KE_IGNORE, 0x02, { KEY_NUMLOCK }},
{ KE_IGNORE, 0x03, { KEY_SCROLLLOCK }},
/* reported via "video bus" */
{ KE_IGNORE, 0x14, { KEY_BRIGHTNESSUP }},
{ KE_IGNORE, 0x15, { KEY_BRIGHTNESSDOWN }},
/* reported in automatic mode when rfkill state changes */
{ KE_SW, 0x1a, {.sw = { SW_RFKILL_ALL, 1 }}},
{ KE_SW, 0x1b, {.sw = { SW_RFKILL_ALL, 0 }}},
/* reported via keyboard controller */
{ KE_IGNORE, 0x35, { KEY_MUTE }},
{ KE_IGNORE, 0x36, { KEY_VOLUMEDOWN }},
{ KE_IGNORE, 0x37, { KEY_VOLUMEUP }},
/*
* not reported by other means when in manual mode,
* handled automatically when it automatic mode
*/
{ KE_KEY, 0xa4, { KEY_RFKILL }},
{ KE_KEY, 0xb1, { KEY_KBDILLUMDOWN }},
{ KE_KEY, 0xb2, { KEY_KBDILLUMUP }},
{ KE_KEY, 0xb8, { KEY_FN_ESC }},
{ KE_END }
};
/* ========================================================================== */
static struct input_dev *qc71_input_dev;
/* ========================================================================== */
static void toggle_fn_lock_from_event_handler(void)
{
int status = qc71_fn_lock_get_state();
if (status < 0)
return;
/* seemingly the returned status in the WMI event handler is not the current */
pr_info("setting Fn lock state from %d to %d\n", !status, status);
qc71_fn_lock_set_state(status);
}
#if IS_ENABLED(CONFIG_LEDS_BRIGHTNESS_HW_CHANGED)
extern struct rw_semaphore leds_list_lock;
extern struct list_head leds_list;
static void emit_keyboard_led_hw_changed(void)
{
struct led_classdev *led;
if (down_read_killable(&leds_list_lock))
return;
list_for_each_entry (led, &leds_list, node) {
size_t name_length;
const char *suffix;
if (!(led->flags & LED_BRIGHT_HW_CHANGED))
continue;
name_length = strlen(led->name);
if (name_length < strlen(KBD_BL_LED_SUFFIX))
continue;
suffix = led->name + name_length - strlen(KBD_BL_LED_SUFFIX);
if (strcmp(suffix, KBD_BL_LED_SUFFIX) == 0) {
if (mutex_lock_interruptible(&led->led_access))
break;
if (led_update_brightness(led) >= 0)
led_classdev_notify_brightness_hw_changed(led, led->brightness);
mutex_unlock(&led->led_access);
break;
}
}
up_read(&leds_list_lock);
}
#else
static inline void emit_keyboard_led_hw_changed(void)
{ }
#endif
static void process_event_72(const union acpi_object *obj)
{
bool do_report = true;
if (obj->type != ACPI_TYPE_INTEGER)
return;
switch (obj->integer.value) {
/* caps lock */
case 1:
pr_info("caps lock\n");
break;
/* num lock */
case 2:
pr_info("num lock\n");
break;
/* scroll lock */
case 3:
pr_info("scroll lock\n");
break;
/* touchpad on */
case 4:
pr_info("touchpad on\n");
break;
/* touchpad off */
case 5:
pr_info("touchpad off\n");
break;
/* increase screen brightness */
case 20:
pr_info("increase screen brightness\n");
/* do_report = !acpi_video_handles_brightness_key_presses() */
break;
/* decrease screen brightness */
case 21:
pr_info("decrease screen brightness\n");
/* do_report = !acpi_video_handles_brightness_key_presses() */
break;
/* radio on */
case 26:
/* triggered in automatic mode when the rfkill hotkey is pressed */
pr_info("radio on\n");
break;
/* radio off */
case 27:
/* triggered in automatic mode when the rfkill hotkey is pressed */
pr_info("radio off\n");
break;
/* mute/unmute */
case 53:
pr_info("toggle mute\n");
break;
/* decrease volume */
case 54:
pr_info("decrease volume\n");
break;
/* increase volume */
case 55:
pr_info("increase volume\n");
break;
case 57:
pr_info("lightbar on\n");
break;
case 58:
pr_info("lightbar off\n");
break;
/* enable super key (win key) lock */
case 64:
pr_info("enable super key lock\n");
break;
/* decrease volume */
case 65:
pr_info("disable super key lock\n");
break;
/* enable/disable airplane mode */
case 164:
pr_info("toggle airplane mode\n");
break;
/* super key (win key) lock state changed */
case 165:
pr_info("super key lock state changed\n");
sysfs_notify(&qc71_platform_dev->dev.kobj, NULL, "super_key_lock");
break;
case 166:
pr_info("lightbar state changed\n");
break;
/* fan boost state changed */
case 167:
pr_info("fan boost state changed\n");
break;
/* charger unplugged/plugged in */
case 171:
pr_info("AC plugged/unplugged\n");
break;
/* perf mode button pressed */
case 176:
pr_info("change perf mode\n");
/* TODO: should it be handled here? */
break;
/* increase keyboard backlight */
case 177:
pr_info("keyboard backlight decrease\n");
/* TODO: should it be handled here? */
break;
/* decrease keyboard backlight */
case 178:
pr_info("keyboard backlight increase\n");
/* TODO: should it be handled here? */
break;
/* toggle Fn lock (Fn+ESC)*/
case 184:
pr_info("toggle Fn lock\n");
toggle_fn_lock_from_event_handler();
sysfs_notify(&qc71_platform_dev->dev.kobj, NULL, "fn_lock");
break;
/* keyboard backlight brightness changed */
case 240:
pr_info("keyboard backlight changed\n");
emit_keyboard_led_hw_changed();
break;
default:
pr_warn("unknown code: %u\n", (unsigned int) obj->integer.value);
break;
}
if (do_report && qc71_input_dev)
sparse_keymap_report_event(qc71_input_dev,
obj->integer.value, 1, true);
}
static void process_event(const union acpi_object *obj, const char *guid)
{
pr_info("guid=%s obj=%p\n", guid, obj);
if (!obj)
return;
pr_info("obj->type = %d\n", (int) obj->type);
if (obj->type == ACPI_TYPE_INTEGER) {
pr_info("int = %u\n", (unsigned int) obj->integer.value);
} else if (obj->type == ACPI_TYPE_STRING) {
pr_info("string = '%s'\n", obj->string.pointer);
} else if (obj->type == ACPI_TYPE_BUFFER) {
pr_info("buffer = %u %*ph", obj->buffer.length,
(int) obj->buffer.length, (void *) obj->buffer.pointer);
}
if (strcmp(guid, QC71_WMI_EVENT_72_GUID) == 0)
process_event_72(obj);
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 12, 0)
static void qc71_wmi_event_handler(union acpi_object *obj, void *context)
{
process_event(obj, context);
}
#else
static void qc71_wmi_event_handler(u32 value, void *context)
{
struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
acpi_status status;
status = wmi_get_event_data(value, &response);
if (ACPI_FAILURE(status)) {
pr_err("bad WMI event status: %#010x\n", (unsigned int) status);
return;
}
process_event(response.pointer, context);
kfree(response.pointer);
}
#endif
static int __init setup_input_dev(void)
{
int err = 0;
qc71_input_dev = input_allocate_device();
if (!qc71_input_dev)
return -ENOMEM;
qc71_input_dev->name = "QC71 laptop input device";
qc71_input_dev->phys = "qc71_laptop/input0";
qc71_input_dev->id.bustype = BUS_HOST;
qc71_input_dev->dev.parent = &qc71_platform_dev->dev;
err = sparse_keymap_setup(qc71_input_dev, qc71_wmi_hotkeys, NULL);
if (err)
goto err_free_device;
err = qc71_rfkill_get_wifi_state();
if (err >= 0)
input_report_switch(qc71_input_dev, SW_RFKILL_ALL, err);
else
input_report_switch(qc71_input_dev, SW_RFKILL_ALL, 1);
err = input_register_device(qc71_input_dev);
if (err)
goto err_free_device;
return err;
err_free_device:
input_free_device(qc71_input_dev);
qc71_input_dev = NULL;
return err;
}
/* ========================================================================== */
int __init qc71_wmi_events_setup(void)
{
int err = 0, i;
(void) setup_input_dev();
for (i = 0; i < ARRAY_SIZE(qc71_wmi_event_guids); i++) {
const char *guid = qc71_wmi_event_guids[i].guid;
acpi_status status =
wmi_install_notify_handler(guid, qc71_wmi_event_handler, (void *) guid);
if (ACPI_FAILURE(status)) {
pr_warn("could not install WMI notify handler for '%s': [%#010lx] %s\n",
guid, (unsigned long) status, acpi_format_exception(status));
} else {
qc71_wmi_event_guids[i].handler_installed = true;
}
}
return err;
}
void qc71_wmi_events_cleanup(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(qc71_wmi_event_guids); i++) {
if (qc71_wmi_event_guids[i].handler_installed) {
wmi_remove_notify_handler(qc71_wmi_event_guids[i].guid);
qc71_wmi_event_guids[i].handler_installed = false;
}
}
if (qc71_input_dev)
input_unregister_device(qc71_input_dev);
}