-
Notifications
You must be signed in to change notification settings - Fork 0
/
system76_legacy.c
357 lines (295 loc) · 8.32 KB
/
system76_legacy.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
/*
* system76_legacy.c
*
* Copyright (C) 2019 Simon Doppler <dopsi@dopsi.ch>
* Copyright (C) 2017 Jeremy Soller <jeremy@system76.com>
* Copyright (C) 2014-2016 Arnoud Willemsen <mail@lynthium.com>
* Copyright (C) 2013-2015 TUXEDO Computers GmbH <tux@tuxedocomputers.com>
*
* 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 S76LEGACY_DRIVER_NAME KBUILD_MODNAME
#define pr_fmt(fmt) S76LEGACY_DRIVER_NAME ": " fmt
#include <linux/acpi.h>
#include <linux/delay.h>
#include <linux/dmi.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/i8042.h>
#include <linux/input.h>
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/leds.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/pci.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/reboot.h>
#include <linux/rfkill.h>
#include <linux/stringify.h>
#include <linux/version.h>
#include <linux/workqueue.h>
#define __S76LEGACY_PR(lvl, fmt, ...) do { pr_##lvl(fmt, ##__VA_ARGS__); } \
while (0)
#define S76LEGACY_INFO(fmt, ...) __S76LEGACY_PR(info, fmt, ##__VA_ARGS__)
#define S76LEGACY_ERROR(fmt, ...) __S76LEGACY_PR(err, fmt, ##__VA_ARGS__)
#define S76LEGACY_DEBUG(fmt, ...) __S76LEGACY_PR(debug, "[%s:%u] " fmt, \
__func__, __LINE__, ##__VA_ARGS__)
#define S76LEGACY_EVENT_GUID "ABBC0F6B-8EA1-11D1-00A0-C90629100000"
#define S76LEGACY_WMBB_GUID "ABBC0F6D-8EA1-11D1-00A0-C90629100000"
#define S76LEGACY_HAS_HWMON (defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE)))
/* method IDs for S76LEGACY_GET */
#define GET_EVENT 0x01 /* 1 */
#define DRIVER_AP_KEY (1 << 0)
#define DRIVER_AP_LED (1 << 1)
#define DRIVER_HWMON (1 << 2)
#define DRIVER_KB_LED (1 << 3)
#define DRIVER_OLED (1 << 4)
#define DRIVER_INPUT (DRIVER_AP_KEY | DRIVER_OLED)
static uint64_t driver_flags = 0;
struct platform_device *s76_platform_device;
static int s76_wmbb(u32 method_id, u32 arg, u32 *retval) {
struct acpi_buffer in = { (acpi_size) sizeof(arg), &arg };
struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
union acpi_object *obj;
acpi_status status;
u32 tmp;
S76LEGACY_DEBUG("%0#4x IN : %0#6x\n", method_id, arg);
status = wmi_evaluate_method(S76LEGACY_WMBB_GUID, 0, method_id, &in, &out);
if (unlikely(ACPI_FAILURE(status))) {
return -EIO;
}
obj = (union acpi_object *) out.pointer;
if (obj && obj->type == ACPI_TYPE_INTEGER) {
tmp = (u32) obj->integer.value;
} else {
tmp = 0;
}
S76LEGACY_DEBUG("%0#4x OUT: %0#6x (IN: %0#6x)\n", method_id, tmp, arg);
if (likely(retval)) {
*retval = tmp;
}
kfree(obj);
return 0;
}
#include "system76_legacy_ap-led.c"
#include "system76_legacy_input.c"
#include "system76_legacy_kb-led.c"
#include "system76_legacy_hwmon.c"
#include "system76_legacy_nv_hda.c"
static void s76_wmi_notify(u32 value, void *context) {
u32 event;
if (value != 0xD0) {
S76LEGACY_DEBUG("Unexpected WMI event (%0#6x)\n", value);
return;
}
s76_wmbb(GET_EVENT, 0, &event);
S76LEGACY_DEBUG("WMI event code (%x)\n", event);
switch (event) {
case 0x81:
if (driver_flags & DRIVER_KB_LED) {
kb_wmi_dec();
}
break;
case 0x82:
if (driver_flags & DRIVER_KB_LED) {
kb_wmi_inc();
}
break;
case 0x83:
if (driver_flags & DRIVER_KB_LED) {
kb_wmi_color();
}
break;
case 0x7b:
//TODO: Fn+Backspace
break;
case 0x95:
//TODO: Fn+ESC
break;
case 0x9F:
if (driver_flags & DRIVER_KB_LED) {
kb_wmi_toggle();
}
break;
case 0xD7:
if (driver_flags & DRIVER_OLED) {
s76_input_screen_wmi();
}
break;
case 0xF4:
if (driver_flags & DRIVER_AP_KEY) {
s76_input_airplane_wmi();
}
break;
case 0xFC:
// Touchpad WMI (disable)
break;
case 0xFD:
// Touchpad WMI (enable)
break;
default:
S76LEGACY_DEBUG("Unknown WMI event code (%x)\n", event);
break;
}
}
static int s76_probe(struct platform_device *dev) {
int err;
if (driver_flags & DRIVER_AP_LED) {
err = ap_led_init(&dev->dev);
if (unlikely(err)) {
S76LEGACY_ERROR("Could not register LED device\n");
}
}
if (driver_flags & DRIVER_KB_LED) {
err = kb_led_init(&dev->dev);
if (unlikely(err)) {
S76LEGACY_ERROR("Could not register LED device\n");
}
}
if (driver_flags & DRIVER_INPUT) {
err = s76_input_init(&dev->dev);
if (unlikely(err)) {
S76LEGACY_ERROR("Could not register input device\n");
}
}
#ifdef S76LEGACY_HAS_HWMON
if (driver_flags & DRIVER_HWMON) {
s76_hwmon_init(&dev->dev);
}
#endif
err = nv_hda_init(&dev->dev);
if (unlikely(err)) {
S76LEGACY_ERROR("Could not register NVIDIA audio device\n");
}
err = wmi_install_notify_handler(S76LEGACY_EVENT_GUID, s76_wmi_notify, NULL);
if (unlikely(ACPI_FAILURE(err))) {
S76LEGACY_ERROR("Could not register WMI notify handler (%0#6x)\n", err);
return -EIO;
}
// Enable hotkey support
s76_wmbb(0x46, 0, NULL);
// Enable touchpad lock
i8042_lock_chip();
i8042_command(NULL, 0x97);
i8042_unlock_chip();
return 0;
}
static int s76_remove(struct platform_device *dev) {
wmi_remove_notify_handler(S76LEGACY_EVENT_GUID);
nv_hda_exit();
#ifdef S76LEGACY_HAS_HWMON
if (driver_flags & DRIVER_HWMON) {
s76_hwmon_fini(&dev->dev);
}
#endif
if (driver_flags & DRIVER_INPUT) {
s76_input_exit();
}
if (driver_flags & DRIVER_KB_LED) {
kb_led_exit();
}
if (driver_flags & DRIVER_AP_LED) {
ap_led_exit();
}
return 0;
}
static int s76_suspend(struct platform_device *dev, pm_message_t status) {
S76LEGACY_DEBUG("s76_suspend\n");
if (driver_flags & DRIVER_KB_LED) {
kb_led_suspend();
}
return 0;
}
static int s76_resume(struct platform_device *dev) {
S76LEGACY_DEBUG("s76_resume\n");
msleep(2000);
if (driver_flags & DRIVER_AP_LED) {
ap_led_resume();
}
if (driver_flags & DRIVER_KB_LED) {
kb_led_resume();
}
// Enable hotkey support
s76_wmbb(0x46, 0, NULL);
// Enable touchpad lock
i8042_lock_chip();
i8042_command(NULL, 0x97);
i8042_unlock_chip();
return 0;
}
static struct platform_driver s76_platform_driver = {
.remove = s76_remove,
.suspend = s76_suspend,
.resume = s76_resume,
.driver = {
.name = S76LEGACY_DRIVER_NAME,
.owner = THIS_MODULE,
},
};
static int __init s76_dmi_matched(const struct dmi_system_id *id) {
S76LEGACY_INFO("Model %s found\n", id->ident);
driver_flags = (uint64_t)id->driver_data;
return 1;
}
// Devices that launched with DKMS support
#define DMI_TABLE(PRODUCT, DATA) { \
.ident = "System76 " PRODUCT, \
.matches = { \
DMI_MATCH(DMI_SYS_VENDOR, "System76"), \
DMI_MATCH(DMI_PRODUCT_VERSION, PRODUCT), \
}, \
.callback = s76_dmi_matched, \
.driver_data = (void *)(uint64_t)(DATA), \
}
static struct dmi_system_id s76_dmi_table[] __initdata = {
DMI_TABLE("oryp3", DRIVER_AP_KEY | DRIVER_AP_LED | DRIVER_HWMON | DRIVER_KB_LED),
{}
};
MODULE_DEVICE_TABLE(dmi, s76_dmi_table);
static int __init s76_init(void) {
if (!dmi_check_system(s76_dmi_table)) {
S76LEGACY_INFO("Model does not utilize this driver");
return -ENODEV;
}
if (!driver_flags) {
S76LEGACY_INFO("Driver data not defined");
return -ENODEV;
}
if (!wmi_has_guid(S76LEGACY_EVENT_GUID)) {
S76LEGACY_INFO("No known WMI event notification GUID found\n");
return -ENODEV;
}
if (!wmi_has_guid(S76LEGACY_WMBB_GUID)) {
S76LEGACY_INFO("No known WMI control method GUID found\n");
return -ENODEV;
}
s76_platform_device =
platform_create_bundle(&s76_platform_driver, s76_probe, NULL, 0, NULL, 0);
if (unlikely(IS_ERR(s76_platform_device))) {
return PTR_ERR(s76_platform_device);
}
return 0;
}
static void __exit s76_exit(void) {
platform_device_unregister(s76_platform_device);
platform_driver_unregister(&s76_platform_driver);
}
module_init(s76_init);
module_exit(s76_exit);
MODULE_AUTHOR("Simon Doppler <dopsi@dopsi.ch>");
MODULE_DESCRIPTION("System76 laptop driver for older laptops");
MODULE_LICENSE("GPL");
MODULE_VERSION("2.0.0");