xref: /linux/drivers/platform/x86/msi-wmi.c (revision 5b05bb3f6c5716fab6911e12d60dd1f43ad9806a)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * MSI WMI hotkeys
4  *
5  * Copyright (C) 2009 Novell <trenn@suse.de>
6  *
7  * Most stuff taken over from hp-wmi
8  */
9 
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 
12 #include <linux/kernel.h>
13 #include <linux/input.h>
14 #include <linux/input/sparse-keymap.h>
15 #include <linux/acpi.h>
16 #include <linux/backlight.h>
17 #include <linux/slab.h>
18 #include <linux/module.h>
19 #include <acpi/video.h>
20 
21 MODULE_AUTHOR("Thomas Renninger <trenn@suse.de>");
22 MODULE_DESCRIPTION("MSI laptop WMI hotkeys driver");
23 MODULE_LICENSE("GPL");
24 
25 #define DRV_NAME "msi-wmi"
26 
27 #define MSIWMI_BIOS_GUID "551A1F84-FBDD-4125-91DB-3EA8F44F1D45"
28 #define MSIWMI_MSI_EVENT_GUID "B6F3EEF2-3D2F-49DC-9DE3-85BCE18C62F2"
29 #define MSIWMI_WIND_EVENT_GUID "5B3CC38A-40D9-7245-8AE6-1145B751BE3F"
30 
31 MODULE_ALIAS("wmi:" MSIWMI_BIOS_GUID);
32 MODULE_ALIAS("wmi:" MSIWMI_MSI_EVENT_GUID);
33 MODULE_ALIAS("wmi:" MSIWMI_WIND_EVENT_GUID);
34 
35 enum msi_scancodes {
36 	/* Generic MSI keys (not present on MSI Wind) */
37 	MSI_KEY_BRIGHTNESSUP	= 0xD0,
38 	MSI_KEY_BRIGHTNESSDOWN,
39 	MSI_KEY_VOLUMEUP,
40 	MSI_KEY_VOLUMEDOWN,
41 	MSI_KEY_MUTE,
42 	/* MSI Wind keys */
43 	WIND_KEY_TOUCHPAD	= 0x08,	/* Fn+F3 touchpad toggle */
44 	WIND_KEY_BLUETOOTH	= 0x56,	/* Fn+F11 Bluetooth toggle */
45 	WIND_KEY_CAMERA,		/* Fn+F6 webcam toggle */
46 	WIND_KEY_WLAN		= 0x5f,	/* Fn+F11 Wi-Fi toggle */
47 	WIND_KEY_TURBO,			/* Fn+F10 turbo mode toggle */
48 	WIND_KEY_ECO		= 0x69,	/* Fn+F10 ECO mode toggle */
49 	/* MSI Claw keys */
50 	CLAW_KEY_VOLUMEDOWN	= 0x21,
51 	CLAW_KEY_CENTER		= 0x29,	/* MSI M-Center main menu */
52 	CLAW_KEY_QUICK_LONG	= 0x2a,	/* MSI M-Center quick access long hold */
53 	CLAW_KEY_VOLUMEUP	= 0x32,
54 	CLAW_KEY_QUICK_SHORT	= 0x58,	/* MSI M-Center quick access short press */
55 };
56 static struct key_entry msi_wmi_keymap[] = {
57 	{ KE_KEY, MSI_KEY_BRIGHTNESSUP,		{KEY_BRIGHTNESSUP} },
58 	{ KE_KEY, MSI_KEY_BRIGHTNESSDOWN,	{KEY_BRIGHTNESSDOWN} },
59 	{ KE_KEY, MSI_KEY_VOLUMEUP,		{KEY_VOLUMEUP} },
60 	{ KE_KEY, MSI_KEY_VOLUMEDOWN,		{KEY_VOLUMEDOWN} },
61 	{ KE_KEY, MSI_KEY_MUTE,			{KEY_MUTE} },
62 
63 	/* These keys work without WMI. Ignore them to avoid double keycodes */
64 	{ KE_IGNORE, WIND_KEY_TOUCHPAD,		{KEY_TOUCHPAD_TOGGLE} },
65 	{ KE_IGNORE, WIND_KEY_BLUETOOTH,	{KEY_BLUETOOTH} },
66 	{ KE_IGNORE, WIND_KEY_CAMERA,		{KEY_CAMERA} },
67 	{ KE_IGNORE, WIND_KEY_WLAN,		{KEY_WLAN} },
68 
69 	/* These are unknown WMI events found on MSI Wind */
70 	{ KE_IGNORE, 0x00 },
71 	{ KE_IGNORE, 0x62 },
72 	{ KE_IGNORE, 0x63 },
73 
74 	/* These are MSI Wind keys that should be handled via WMI */
75 	{ KE_KEY, WIND_KEY_TURBO,		{KEY_PROG1} },
76 	{ KE_KEY, WIND_KEY_ECO,			{KEY_PROG2} },
77 
78 	/* These are MSI Claw keys, used for MSI M-Center in Windows */
79 	{ KE_KEY, CLAW_KEY_CENTER,		{KEY_F15} },
80 
81 	/* These MSI Claw keys work without WMI. Ignore them to avoid double keycodes */
82 	{ KE_IGNORE, CLAW_KEY_QUICK_SHORT },
83 	{ KE_IGNORE, CLAW_KEY_QUICK_LONG },
84 	{ KE_IGNORE, CLAW_KEY_VOLUMEUP },
85 	{ KE_IGNORE, CLAW_KEY_VOLUMEDOWN },
86 
87 	{ KE_END, 0 }
88 };
89 
90 static ktime_t last_pressed;
91 
92 static const struct {
93 	const char *guid;
94 	bool quirk_last_pressed;
95 } *event_wmi, event_wmis[] = {
96 	{ MSIWMI_MSI_EVENT_GUID, true },
97 	{ MSIWMI_WIND_EVENT_GUID, false },
98 };
99 
100 static struct backlight_device *backlight;
101 
102 static int backlight_map[] = { 0x00, 0x33, 0x66, 0x99, 0xCC, 0xFF };
103 
104 static struct input_dev *msi_wmi_input_dev;
105 
msi_wmi_query_block(int instance,int * ret)106 static int msi_wmi_query_block(int instance, int *ret)
107 {
108 	acpi_status status;
109 	union acpi_object *obj;
110 
111 	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
112 
113 	status = wmi_query_block(MSIWMI_BIOS_GUID, instance, &output);
114 	if (ACPI_FAILURE(status))
115 		return -EIO;
116 
117 	obj = output.pointer;
118 
119 	if (!obj || obj->type != ACPI_TYPE_INTEGER) {
120 		if (obj) {
121 			pr_err("query block returned object "
122 			       "type: %d - buffer length:%d\n", obj->type,
123 			       obj->type == ACPI_TYPE_BUFFER ?
124 			       obj->buffer.length : 0);
125 		}
126 		kfree(obj);
127 		return -EINVAL;
128 	}
129 	*ret = obj->integer.value;
130 	kfree(obj);
131 	return 0;
132 }
133 
msi_wmi_set_block(int instance,int value)134 static int msi_wmi_set_block(int instance, int value)
135 {
136 	acpi_status status;
137 
138 	struct acpi_buffer input = { sizeof(int), &value };
139 
140 	pr_debug("Going to set block of instance: %d - value: %d\n",
141 		 instance, value);
142 
143 	status = wmi_set_block(MSIWMI_BIOS_GUID, instance, &input);
144 
145 	return ACPI_SUCCESS(status) ? 0 : 1;
146 }
147 
bl_get(struct backlight_device * bd)148 static int bl_get(struct backlight_device *bd)
149 {
150 	int level, err, ret;
151 
152 	/* Instance 1 is "get backlight", cmp with DSDT */
153 	err = msi_wmi_query_block(1, &ret);
154 	if (err) {
155 		pr_err("Could not query backlight: %d\n", err);
156 		return -EINVAL;
157 	}
158 	pr_debug("Get: Query block returned: %d\n", ret);
159 	for (level = 0; level < ARRAY_SIZE(backlight_map); level++) {
160 		if (backlight_map[level] == ret) {
161 			pr_debug("Current backlight level: 0x%X - index: %d\n",
162 				 backlight_map[level], level);
163 			break;
164 		}
165 	}
166 	if (level == ARRAY_SIZE(backlight_map)) {
167 		pr_err("get: Invalid brightness value: 0x%X\n", ret);
168 		return -EINVAL;
169 	}
170 	return level;
171 }
172 
bl_set_status(struct backlight_device * bd)173 static int bl_set_status(struct backlight_device *bd)
174 {
175 	int bright = bd->props.brightness;
176 	if (bright >= ARRAY_SIZE(backlight_map) || bright < 0)
177 		return -EINVAL;
178 
179 	/* Instance 0 is "set backlight" */
180 	return msi_wmi_set_block(0, backlight_map[bright]);
181 }
182 
183 static const struct backlight_ops msi_backlight_ops = {
184 	.get_brightness	= bl_get,
185 	.update_status	= bl_set_status,
186 };
187 
msi_wmi_notify(union acpi_object * obj,void * context)188 static void msi_wmi_notify(union acpi_object *obj, void *context)
189 {
190 	struct key_entry *key = NULL;
191 	int eventcode = 0;
192 
193 	if (!obj)
194 		return;
195 
196 	switch (obj->type) {
197 	case ACPI_TYPE_INTEGER:
198 		eventcode = obj->integer.value;
199 		pr_debug("Eventcode: 0x%x\n", eventcode);
200 		break;
201 	case ACPI_TYPE_BUFFER:
202 		/* Field returns u8[2] here, but is u32 by spec. Allow "oversized" buffers. */
203 		if (obj->buffer.length < 2)
204 			return;
205 
206 		/* pointer[0] is key ID, pointer[1] is active state. We don't get release
207 		 * events, so ignore the active state and treat as autorelease.
208 		 */
209 		eventcode = obj->buffer.pointer[0];
210 		pr_debug("Eventcode: 0x%x\n", eventcode);
211 		break;
212 	default:
213 		pr_info("Unknown event received\n");
214 		return;
215 	}
216 
217 	key = sparse_keymap_entry_from_scancode(msi_wmi_input_dev, eventcode);
218 
219 	if (!key) {
220 		pr_info("Unknown key pressed - 0x%x\n", eventcode);
221 		return;
222 	}
223 
224 	if (event_wmi->quirk_last_pressed) {
225 		ktime_t cur = ktime_get_real();
226 		ktime_t diff = ktime_sub(cur, last_pressed);
227 		/* Ignore event if any event happened in a 50 ms
228 		 * timeframe -> Key press may result in 10-20 GPEs
229 		 */
230 		if (ktime_to_us(diff) < 1000 * 50) {
231 			pr_debug("Suppressed key event 0x%X - Last press was %lld us ago\n",
232 				 key->code, ktime_to_us(diff));
233 			return;
234 		}
235 		last_pressed = cur;
236 	}
237 
238 	/* Brightness is served via acpi video driver */
239 	if (key->type == KE_KEY &&
240 	    (backlight || (key->code == MSI_KEY_BRIGHTNESSUP ||
241 			   key->code == MSI_KEY_BRIGHTNESSDOWN)))
242 		return;
243 
244 	pr_debug("Send key: 0x%X - Input layer keycode: %d\n", key->code, key->keycode);
245 	sparse_keymap_report_entry(msi_wmi_input_dev, key, 1, true);
246 }
247 
msi_wmi_backlight_setup(void)248 static int __init msi_wmi_backlight_setup(void)
249 {
250 	int err;
251 	struct backlight_properties props;
252 
253 	memset(&props, 0, sizeof(struct backlight_properties));
254 	props.type = BACKLIGHT_PLATFORM;
255 	props.max_brightness = ARRAY_SIZE(backlight_map) - 1;
256 	backlight = backlight_device_register(DRV_NAME, NULL, NULL,
257 					      &msi_backlight_ops,
258 					      &props);
259 	if (IS_ERR(backlight))
260 		return PTR_ERR(backlight);
261 
262 	err = bl_get(NULL);
263 	if (err < 0) {
264 		backlight_device_unregister(backlight);
265 		return err;
266 	}
267 
268 	backlight->props.brightness = err;
269 
270 	return 0;
271 }
272 
msi_wmi_input_setup(void)273 static int __init msi_wmi_input_setup(void)
274 {
275 	int err;
276 
277 	msi_wmi_input_dev = input_allocate_device();
278 	if (!msi_wmi_input_dev)
279 		return -ENOMEM;
280 
281 	msi_wmi_input_dev->name = "MSI WMI hotkeys";
282 	msi_wmi_input_dev->phys = "wmi/input0";
283 	msi_wmi_input_dev->id.bustype = BUS_HOST;
284 
285 	err = sparse_keymap_setup(msi_wmi_input_dev, msi_wmi_keymap, NULL);
286 	if (err)
287 		goto err_free_dev;
288 
289 	err = input_register_device(msi_wmi_input_dev);
290 
291 	if (err)
292 		goto err_free_dev;
293 
294 	last_pressed = 0;
295 
296 	return 0;
297 
298 err_free_dev:
299 	input_free_device(msi_wmi_input_dev);
300 	return err;
301 }
302 
msi_wmi_init(void)303 static int __init msi_wmi_init(void)
304 {
305 	int err;
306 	int i;
307 
308 	for (i = 0; i < ARRAY_SIZE(event_wmis); i++) {
309 		if (!wmi_has_guid(event_wmis[i].guid))
310 			continue;
311 
312 		err = msi_wmi_input_setup();
313 		if (err) {
314 			pr_err("Unable to setup input device\n");
315 			return err;
316 		}
317 
318 		err = wmi_install_notify_handler(event_wmis[i].guid,
319 			msi_wmi_notify, NULL);
320 		if (ACPI_FAILURE(err)) {
321 			pr_err("Unable to setup WMI notify handler\n");
322 			goto err_free_input;
323 		}
324 
325 		pr_debug("Event handler installed\n");
326 		event_wmi = &event_wmis[i];
327 		break;
328 	}
329 
330 	if (wmi_has_guid(MSIWMI_BIOS_GUID) &&
331 	    acpi_video_get_backlight_type() == acpi_backlight_vendor) {
332 		err = msi_wmi_backlight_setup();
333 		if (err) {
334 			pr_err("Unable to setup backlight device\n");
335 			goto err_uninstall_handler;
336 		}
337 		pr_debug("Backlight device created\n");
338 	}
339 
340 	if (!event_wmi && !backlight) {
341 		pr_err("This machine doesn't have neither MSI-hotkeys nor backlight through WMI\n");
342 		return -ENODEV;
343 	}
344 
345 	return 0;
346 
347 err_uninstall_handler:
348 	if (event_wmi)
349 		wmi_remove_notify_handler(event_wmi->guid);
350 err_free_input:
351 	if (event_wmi)
352 		input_unregister_device(msi_wmi_input_dev);
353 	return err;
354 }
355 
msi_wmi_exit(void)356 static void __exit msi_wmi_exit(void)
357 {
358 	if (event_wmi) {
359 		wmi_remove_notify_handler(event_wmi->guid);
360 		input_unregister_device(msi_wmi_input_dev);
361 	}
362 	backlight_device_unregister(backlight);
363 }
364 
365 module_init(msi_wmi_init);
366 module_exit(msi_wmi_exit);
367