-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem76_legacy_input.c
189 lines (145 loc) · 4.77 KB
/
system76_legacy_input.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
/*
* input.c
*
* Copyright (C) 2019 Simon Doppler <[email protected]>
* Copyright (C) 2017 Jeremy Soller <[email protected]>
* Copyright (C) 2014-2016 Arnoud Willemsen <[email protected]>
* Copyright (C) 2013-2015 TUXEDO Computers GmbH <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define AIRPLANE_KEY KEY_WLAN
#define SCREEN_KEY KEY_SCREENLOCK
static struct input_dev *s76_input_device;
static DEFINE_MUTEX(s76_input_report_mutex);
#define POLL_FREQ_MIN 1
#define POLL_FREQ_MAX 20
#define POLL_FREQ_DEFAULT 5
static int param_set_poll_freq(const char *val, const struct kernel_param *kp)
{
int ret;
ret = param_set_byte(val, kp);
if (!ret)
*((unsigned char *) kp->arg) = clamp_t(unsigned char,
*((unsigned char *) kp->arg),
POLL_FREQ_MIN, POLL_FREQ_MAX);
return ret;
}
static const struct kernel_param_ops param_ops_poll_freq = {
.set = param_set_poll_freq,
.get = param_get_byte,
};
static unsigned char param_poll_freq = POLL_FREQ_DEFAULT;
#define param_check_poll_freq param_check_byte
module_param_named(poll_freq, param_poll_freq, poll_freq, S_IRUSR);
MODULE_PARM_DESC(poll_freq, "Set polling frequency");
static struct task_struct *s76_input_polling_task;
static void s76_input_key(unsigned int code) {
S76LEGACY_DEBUG("Send key %x\n", code);
mutex_lock(&s76_input_report_mutex);
input_report_key(s76_input_device, code, 1);
input_sync(s76_input_device);
input_report_key(s76_input_device, code, 0);
input_sync(s76_input_device);
mutex_unlock(&s76_input_report_mutex);
}
static int s76_input_polling_thread(void *data) {
S76LEGACY_DEBUG("Polling thread started (PID: %i), polling at %i Hz\n",
current->pid, param_poll_freq);
while (!kthread_should_stop()) {
u8 byte;
ec_read(0xDB, &byte);
if (byte & 0x40) {
ec_write(0xDB, byte & ~0x40);
S76LEGACY_DEBUG("Airplane-Mode Hotkey pressed (EC)\n");
s76_input_key(AIRPLANE_KEY);
}
msleep_interruptible(1000 / param_poll_freq);
}
S76LEGACY_DEBUG("Polling thread exiting\n");
return 0;
}
static void s76_input_airplane_wmi(void) {
S76LEGACY_DEBUG("Airplane-Mode Hotkey pressed (WMI)\n");
if (s76_input_polling_task) {
S76LEGACY_DEBUG("Stopping polling thread\n");
kthread_stop(s76_input_polling_task);
s76_input_polling_task = NULL;
}
s76_input_key(AIRPLANE_KEY);
}
static void s76_input_screen_wmi(void) {
S76LEGACY_DEBUG("Screen Hotkey pressed (WMI)\n");
s76_input_key(SCREEN_KEY);
}
static int s76_input_open(struct input_dev *dev) {
if (driver_flags & DRIVER_AP_KEY) {
s76_input_polling_task = kthread_run(
s76_input_polling_thread,
NULL, "system76_legacy-polld");
}
if (unlikely(IS_ERR(s76_input_polling_task))) {
s76_input_polling_task = NULL;
S76LEGACY_ERROR("Could not create polling thread\n");
return PTR_ERR(s76_input_polling_task);
}
return 0;
}
static void s76_input_close(struct input_dev *dev) {
if (unlikely(IS_ERR_OR_NULL(s76_input_polling_task))) {
return;
}
kthread_stop(s76_input_polling_task);
s76_input_polling_task = NULL;
}
static int __init s76_input_init(struct device *dev) {
int err;
u8 byte;
s76_input_device = input_allocate_device();
if (unlikely(!s76_input_device)) {
S76LEGACY_ERROR("Error allocating input device\n");
return -ENOMEM;
}
s76_input_device->name = "System76 (legacy) Hotkeys";
s76_input_device->phys = "system76_legacy/input0";
s76_input_device->id.bustype = BUS_HOST;
s76_input_device->dev.parent = dev;
set_bit(EV_KEY, s76_input_device->evbit);
if (driver_flags & DRIVER_AP_KEY) {
set_bit(AIRPLANE_KEY, s76_input_device->keybit);
ec_read(0xDB, &byte);
ec_write(0xDB, byte & ~0x40);
}
if (driver_flags & DRIVER_OLED) {
set_bit(SCREEN_KEY, s76_input_device->keybit);
}
s76_input_device->open = s76_input_open;
s76_input_device->close = s76_input_close;
err = input_register_device(s76_input_device);
if (unlikely(err)) {
S76LEGACY_ERROR("Error registering input device\n");
goto err_free_input_device;
}
return 0;
err_free_input_device:
input_free_device(s76_input_device);
return err;
}
static void __exit s76_input_exit(void) {
if (unlikely(!s76_input_device)) {
return;
}
input_unregister_device(s76_input_device);
s76_input_device = NULL;
}