blob: bc98ef95514a1770c285ed54b4913d7c41ff5ad6 [file] [log] [blame]
Hans de Goede13bb0fd2017-05-12 17:11:54 +02001/*
2 * PEAQ 2-in-1 WMI hotkey driver
3 * Copyright (C) 2017 Hans de Goede <hdegoede@redhat.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9
10#include <linux/acpi.h>
11#include <linux/input-polldev.h>
12#include <linux/kernel.h>
13#include <linux/module.h>
14
15#define PEAQ_DOLBY_BUTTON_GUID "ABBC0F6F-8EA1-11D1-00A0-C90629100000"
16#define PEAQ_DOLBY_BUTTON_METHOD_ID 5
17#define PEAQ_POLL_INTERVAL_MS 250
18#define PEAQ_POLL_IGNORE_MS 500
19#define PEAQ_POLL_MAX_MS 1000
20
21MODULE_ALIAS("wmi:"PEAQ_DOLBY_BUTTON_GUID);
22
23static unsigned int peaq_ignore_events_counter;
24static struct input_polled_dev *peaq_poll_dev;
25
26/*
27 * The Dolby button (yes really a Dolby button) causes an ACPI variable to get
28 * set on both press and release. The WMI method checks and clears that flag.
29 * So for a press + release we will get back One from the WMI method either once
30 * (if polling after the release) or twice (polling between press and release).
31 * We ignore events for 0.5s after the first event to avoid reporting 2 presses.
32 */
33static void peaq_wmi_poll(struct input_polled_dev *dev)
34{
35 union acpi_object obj;
36 acpi_status status;
37 u32 dummy = 0;
38
39 struct acpi_buffer input = { sizeof(dummy), &dummy };
40 struct acpi_buffer output = { sizeof(obj), &obj };
41
Pali Rohár2cf7bde2017-08-12 09:44:17 +020042 status = wmi_evaluate_method(PEAQ_DOLBY_BUTTON_GUID, 0,
Hans de Goede13bb0fd2017-05-12 17:11:54 +020043 PEAQ_DOLBY_BUTTON_METHOD_ID,
44 &input, &output);
45 if (ACPI_FAILURE(status))
46 return;
47
48 if (obj.type != ACPI_TYPE_INTEGER) {
49 dev_err(&peaq_poll_dev->input->dev,
50 "Error WMBC did not return an integer\n");
51 return;
52 }
53
Dan Carpenter890f6582017-07-19 16:07:19 +030054 if (peaq_ignore_events_counter && peaq_ignore_events_counter--)
Hans de Goede13bb0fd2017-05-12 17:11:54 +020055 return;
56
57 if (obj.integer.value) {
58 input_event(peaq_poll_dev->input, EV_KEY, KEY_SOUND, 1);
59 input_sync(peaq_poll_dev->input);
60 input_event(peaq_poll_dev->input, EV_KEY, KEY_SOUND, 0);
61 input_sync(peaq_poll_dev->input);
62 peaq_ignore_events_counter = max(1u,
63 PEAQ_POLL_IGNORE_MS / peaq_poll_dev->poll_interval);
64 }
65}
66
67static int __init peaq_wmi_init(void)
68{
Andy Shevchenkodfea7e12017-10-08 21:04:43 +030069 if (!wmi_has_guid(PEAQ_DOLBY_BUTTON_GUID))
Hans de Goede13bb0fd2017-05-12 17:11:54 +020070 return -ENODEV;
71
72 peaq_poll_dev = input_allocate_polled_device();
73 if (!peaq_poll_dev)
74 return -ENOMEM;
75
76 peaq_poll_dev->poll = peaq_wmi_poll;
77 peaq_poll_dev->poll_interval = PEAQ_POLL_INTERVAL_MS;
78 peaq_poll_dev->poll_interval_max = PEAQ_POLL_MAX_MS;
79 peaq_poll_dev->input->name = "PEAQ WMI hotkeys";
80 peaq_poll_dev->input->phys = "wmi/input0";
81 peaq_poll_dev->input->id.bustype = BUS_HOST;
82 input_set_capability(peaq_poll_dev->input, EV_KEY, KEY_SOUND);
83
84 return input_register_polled_device(peaq_poll_dev);
85}
86
87static void __exit peaq_wmi_exit(void)
88{
89 if (!wmi_has_guid(PEAQ_DOLBY_BUTTON_GUID))
90 return;
91
92 input_unregister_polled_device(peaq_poll_dev);
93}
94
95module_init(peaq_wmi_init);
96module_exit(peaq_wmi_exit);
97
98MODULE_DESCRIPTION("PEAQ 2-in-1 WMI hotkey driver");
99MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
100MODULE_LICENSE("GPL");