Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / platform / x86 / dell-wmi-aio.c
blobc7b7f1e403fb9a0d372f2dfef16e7fb5868b8231
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * WMI hotkeys support for Dell All-In-One series
4 */
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/types.h>
12 #include <linux/input.h>
13 #include <linux/input/sparse-keymap.h>
14 #include <linux/acpi.h>
15 #include <linux/string.h>
17 MODULE_DESCRIPTION("WMI hotkeys driver for Dell All-In-One series");
18 MODULE_LICENSE("GPL");
20 #define EVENT_GUID1 "284A0E6B-380E-472A-921F-E52786257FB4"
21 #define EVENT_GUID2 "02314822-307C-4F66-BF0E-48AEAEB26CC8"
23 struct dell_wmi_event {
24 u16 length;
25 /* 0x000: A hot key pressed or an event occurred
26 * 0x00F: A sequence of hot keys are pressed */
27 u16 type;
28 u16 event[];
31 static const char *dell_wmi_aio_guids[] = {
32 EVENT_GUID1,
33 EVENT_GUID2,
34 NULL
37 MODULE_ALIAS("wmi:"EVENT_GUID1);
38 MODULE_ALIAS("wmi:"EVENT_GUID2);
40 static const struct key_entry dell_wmi_aio_keymap[] = {
41 { KE_KEY, 0xc0, { KEY_VOLUMEUP } },
42 { KE_KEY, 0xc1, { KEY_VOLUMEDOWN } },
43 { KE_KEY, 0xe030, { KEY_VOLUMEUP } },
44 { KE_KEY, 0xe02e, { KEY_VOLUMEDOWN } },
45 { KE_KEY, 0xe020, { KEY_MUTE } },
46 { KE_KEY, 0xe027, { KEY_DISPLAYTOGGLE } },
47 { KE_KEY, 0xe006, { KEY_BRIGHTNESSUP } },
48 { KE_KEY, 0xe005, { KEY_BRIGHTNESSDOWN } },
49 { KE_KEY, 0xe00b, { KEY_SWITCHVIDEOMODE } },
50 { KE_END, 0 }
53 static struct input_dev *dell_wmi_aio_input_dev;
56 * The new WMI event data format will follow the dell_wmi_event structure
57 * So, we will check if the buffer matches the format
59 static bool dell_wmi_aio_event_check(u8 *buffer, int length)
61 struct dell_wmi_event *event = (struct dell_wmi_event *)buffer;
63 if (event == NULL || length < 6)
64 return false;
66 if ((event->type == 0 || event->type == 0xf) &&
67 event->length >= 2)
68 return true;
70 return false;
73 static void dell_wmi_aio_notify(u32 value, void *context)
75 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
76 union acpi_object *obj;
77 struct dell_wmi_event *event;
78 acpi_status status;
80 status = wmi_get_event_data(value, &response);
81 if (status != AE_OK) {
82 pr_info("bad event status 0x%x\n", status);
83 return;
86 obj = (union acpi_object *)response.pointer;
87 if (obj) {
88 unsigned int scancode = 0;
90 switch (obj->type) {
91 case ACPI_TYPE_INTEGER:
92 /* Most All-In-One correctly return integer scancode */
93 scancode = obj->integer.value;
94 sparse_keymap_report_event(dell_wmi_aio_input_dev,
95 scancode, 1, true);
96 break;
97 case ACPI_TYPE_BUFFER:
98 if (dell_wmi_aio_event_check(obj->buffer.pointer,
99 obj->buffer.length)) {
100 event = (struct dell_wmi_event *)
101 obj->buffer.pointer;
102 scancode = event->event[0];
103 } else {
104 /* Broken machines return the scancode in a
105 buffer */
106 if (obj->buffer.pointer &&
107 obj->buffer.length > 0)
108 scancode = obj->buffer.pointer[0];
110 if (scancode)
111 sparse_keymap_report_event(
112 dell_wmi_aio_input_dev,
113 scancode, 1, true);
114 break;
117 kfree(obj);
120 static int __init dell_wmi_aio_input_setup(void)
122 int err;
124 dell_wmi_aio_input_dev = input_allocate_device();
126 if (!dell_wmi_aio_input_dev)
127 return -ENOMEM;
129 dell_wmi_aio_input_dev->name = "Dell AIO WMI hotkeys";
130 dell_wmi_aio_input_dev->phys = "wmi/input0";
131 dell_wmi_aio_input_dev->id.bustype = BUS_HOST;
133 err = sparse_keymap_setup(dell_wmi_aio_input_dev,
134 dell_wmi_aio_keymap, NULL);
135 if (err) {
136 pr_err("Unable to setup input device keymap\n");
137 goto err_free_dev;
139 err = input_register_device(dell_wmi_aio_input_dev);
140 if (err) {
141 pr_info("Unable to register input device\n");
142 goto err_free_dev;
144 return 0;
146 err_free_dev:
147 input_free_device(dell_wmi_aio_input_dev);
148 return err;
151 static const char *dell_wmi_aio_find(void)
153 int i;
155 for (i = 0; dell_wmi_aio_guids[i] != NULL; i++)
156 if (wmi_has_guid(dell_wmi_aio_guids[i]))
157 return dell_wmi_aio_guids[i];
159 return NULL;
162 static int __init dell_wmi_aio_init(void)
164 int err;
165 const char *guid;
167 guid = dell_wmi_aio_find();
168 if (!guid) {
169 pr_warn("No known WMI GUID found\n");
170 return -ENXIO;
173 err = dell_wmi_aio_input_setup();
174 if (err)
175 return err;
177 err = wmi_install_notify_handler(guid, dell_wmi_aio_notify, NULL);
178 if (err) {
179 pr_err("Unable to register notify handler - %d\n", err);
180 input_unregister_device(dell_wmi_aio_input_dev);
181 return err;
184 return 0;
187 static void __exit dell_wmi_aio_exit(void)
189 const char *guid;
191 guid = dell_wmi_aio_find();
192 wmi_remove_notify_handler(guid);
193 input_unregister_device(dell_wmi_aio_input_dev);
196 module_init(dell_wmi_aio_init);
197 module_exit(dell_wmi_aio_exit);