]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/platform/x86/dell-wmi.c
platform-drivers-x86: samsung-q10: make dmi_check_callback return 1
[mirror_ubuntu-hirsute-kernel.git] / drivers / platform / x86 / dell-wmi.c
CommitLineData
0b3f6109
MG
1/*
2 * Dell WMI hotkeys
3 *
4 * Copyright (C) 2008 Red Hat <mjg@redhat.com>
5 *
6 * Portions based on wistron_btns.c:
7 * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
8 * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
9 * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
eb889524
JP
26#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
0b3f6109
MG
28#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/init.h>
5a0e3ad6 31#include <linux/slab.h>
0b3f6109
MG
32#include <linux/types.h>
33#include <linux/input.h>
890a7c8e 34#include <linux/input/sparse-keymap.h>
0b3f6109
MG
35#include <acpi/acpi_drivers.h>
36#include <linux/acpi.h>
37#include <linux/string.h>
5ea25597 38#include <linux/dmi.h>
0b3f6109
MG
39
40MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
41MODULE_DESCRIPTION("Dell laptop WMI hotkeys driver");
42MODULE_LICENSE("GPL");
43
44#define DELL_EVENT_GUID "9DBB5994-A997-11DA-B012-B622A1EF5492"
45
5ea25597
RK
46static int acpi_video;
47
0b3f6109
MG
48MODULE_ALIAS("wmi:"DELL_EVENT_GUID);
49
5cab0098
ML
50/*
51 * Certain keys are flagged as KE_IGNORE. All of these are either
52 * notifications (rather than requests for change) or are also sent
53 * via the keyboard controller so should not be sent again.
54 */
0b3f6109 55
890a7c8e
DT
56static const struct key_entry dell_wmi_legacy_keymap[] __initconst = {
57 { KE_KEY, 0xe045, { KEY_PROG1 } },
58 { KE_KEY, 0xe009, { KEY_EJECTCD } },
5cab0098
ML
59
60 /* These also contain the brightness level at offset 6 */
890a7c8e
DT
61 { KE_KEY, 0xe006, { KEY_BRIGHTNESSUP } },
62 { KE_KEY, 0xe005, { KEY_BRIGHTNESSDOWN } },
5cab0098
ML
63
64 /* Battery health status button */
890a7c8e 65 { KE_KEY, 0xe007, { KEY_BATTERY } },
5cab0098
ML
66
67 /* This is actually for all radios. Although physically a
68 * switch, the notification does not provide an indication of
69 * state and so it should be reported as a key */
890a7c8e 70 { KE_KEY, 0xe008, { KEY_WLAN } },
5cab0098
ML
71
72 /* The next device is at offset 6, the active devices are at
73 offset 8 and the attached devices at offset 10 */
890a7c8e 74 { KE_KEY, 0xe00b, { KEY_SWITCHVIDEOMODE } },
5cab0098 75
890a7c8e 76 { KE_IGNORE, 0xe00c, { KEY_KBDILLUMTOGGLE } },
5cab0098
ML
77
78 /* BIOS error detected */
890a7c8e 79 { KE_IGNORE, 0xe00d, { KEY_RESERVED } },
5cab0098
ML
80
81 /* Wifi Catcher */
890a7c8e 82 { KE_KEY, 0xe011, {KEY_PROG2 } },
5cab0098
ML
83
84 /* Ambient light sensor toggle */
890a7c8e
DT
85 { KE_IGNORE, 0xe013, { KEY_RESERVED } },
86
87 { KE_IGNORE, 0xe020, { KEY_MUTE } },
88 { KE_IGNORE, 0xe02e, { KEY_VOLUMEDOWN } },
89 { KE_IGNORE, 0xe030, { KEY_VOLUMEUP } },
90 { KE_IGNORE, 0xe033, { KEY_KBDILLUMUP } },
91 { KE_IGNORE, 0xe034, { KEY_KBDILLUMDOWN } },
92 { KE_IGNORE, 0xe03a, { KEY_CAPSLOCK } },
93 { KE_IGNORE, 0xe045, { KEY_NUMLOCK } },
94 { KE_IGNORE, 0xe046, { KEY_SCROLLLOCK } },
95 { KE_END, 0 }
0b3f6109
MG
96};
97
5ea25597
RK
98static bool dell_new_hk_type;
99
890a7c8e 100struct dell_bios_keymap_entry {
5ea25597
RK
101 u16 scancode;
102 u16 keycode;
103};
104
890a7c8e 105struct dell_bios_hotkey_table {
5ea25597 106 struct dmi_header header;
890a7c8e 107 struct dell_bios_keymap_entry keymap[];
5ea25597
RK
108
109};
110
890a7c8e 111static const struct dell_bios_hotkey_table *dell_bios_hotkey_table;
5ea25597 112
890a7c8e 113static const u16 bios_to_linux_keycode[256] __initconst = {
5ea25597
RK
114
115 KEY_MEDIA, KEY_NEXTSONG, KEY_PLAYPAUSE, KEY_PREVIOUSSONG,
116 KEY_STOPCD, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
117 KEY_WWW, KEY_UNKNOWN, KEY_VOLUMEDOWN, KEY_MUTE,
118 KEY_VOLUMEUP, KEY_UNKNOWN, KEY_BATTERY, KEY_EJECTCD,
119 KEY_UNKNOWN, KEY_SLEEP, KEY_PROG1, KEY_BRIGHTNESSDOWN,
120 KEY_BRIGHTNESSUP, KEY_UNKNOWN, KEY_KBDILLUMTOGGLE,
121 KEY_UNKNOWN, KEY_SWITCHVIDEOMODE, KEY_UNKNOWN, KEY_UNKNOWN,
122 KEY_SWITCHVIDEOMODE, KEY_UNKNOWN, KEY_UNKNOWN, KEY_PROG2,
123 KEY_UNKNOWN, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
124 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
125 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
126 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
127 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
128 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
129 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
130 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
131 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
132 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
133 KEY_PROG3
134};
135
0b3f6109
MG
136static struct input_dev *dell_wmi_input_dev;
137
0b3f6109
MG
138static void dell_wmi_notify(u32 value, void *context)
139{
140 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
0b3f6109 141 union acpi_object *obj;
fda11e61 142 acpi_status status;
0b3f6109 143
fda11e61
LB
144 status = wmi_get_event_data(value, &response);
145 if (status != AE_OK) {
eb889524 146 pr_info("bad event status 0x%x\n", status);
fda11e61
LB
147 return;
148 }
0b3f6109
MG
149
150 obj = (union acpi_object *)response.pointer;
151
152 if (obj && obj->type == ACPI_TYPE_BUFFER) {
890a7c8e 153 const struct key_entry *key;
5ea25597
RK
154 int reported_key;
155 u16 *buffer_entry = (u16 *)obj->buffer.pointer;
890a7c8e 156
5ea25597 157 if (dell_new_hk_type && (buffer_entry[1] != 0x10)) {
eb889524
JP
158 pr_info("Received unknown WMI event (0x%x)\n",
159 buffer_entry[1]);
a0624a90 160 kfree(obj);
5ea25597
RK
161 return;
162 }
163
d5164dbf 164 if (dell_new_hk_type || buffer_entry[1] == 0x0)
5ea25597
RK
165 reported_key = (int)buffer_entry[2];
166 else
167 reported_key = (int)buffer_entry[1] & 0xffff;
168
890a7c8e
DT
169 key = sparse_keymap_entry_from_scancode(dell_wmi_input_dev,
170 reported_key);
5ea25597 171 if (!key) {
eb889524 172 pr_info("Unknown key %x pressed\n", reported_key);
5ea25597
RK
173 } else if ((key->keycode == KEY_BRIGHTNESSUP ||
174 key->keycode == KEY_BRIGHTNESSDOWN) && acpi_video) {
175 /* Don't report brightness notifications that will also
176 * come via ACPI */
a0624a90 177 ;
5ea25597 178 } else {
890a7c8e
DT
179 sparse_keymap_report_entry(dell_wmi_input_dev, key,
180 1, true);
5ea25597 181 }
0b3f6109 182 }
3e9b988e 183 kfree(obj);
0b3f6109
MG
184}
185
890a7c8e 186static const struct key_entry * __init dell_wmi_prepare_new_keymap(void)
5ea25597 187{
890a7c8e
DT
188 int hotkey_num = (dell_bios_hotkey_table->header.length - 4) /
189 sizeof(struct dell_bios_keymap_entry);
190 struct key_entry *keymap;
5ea25597 191 int i;
5ea25597 192
890a7c8e
DT
193 keymap = kcalloc(hotkey_num + 1, sizeof(struct key_entry), GFP_KERNEL);
194 if (!keymap)
195 return NULL;
5ea25597
RK
196
197 for (i = 0; i < hotkey_num; i++) {
890a7c8e
DT
198 const struct dell_bios_keymap_entry *bios_entry =
199 &dell_bios_hotkey_table->keymap[i];
200 keymap[i].type = KE_KEY;
201 keymap[i].code = bios_entry->scancode;
202 keymap[i].keycode = bios_entry->keycode < 256 ?
203 bios_to_linux_keycode[bios_entry->keycode] :
204 KEY_RESERVED;
5ea25597
RK
205 }
206
890a7c8e 207 keymap[hotkey_num].type = KE_END;
5ea25597 208
890a7c8e 209 return keymap;
5ea25597
RK
210}
211
0b3f6109
MG
212static int __init dell_wmi_input_setup(void)
213{
0b3f6109
MG
214 int err;
215
216 dell_wmi_input_dev = input_allocate_device();
0b3f6109
MG
217 if (!dell_wmi_input_dev)
218 return -ENOMEM;
219
220 dell_wmi_input_dev->name = "Dell WMI hotkeys";
221 dell_wmi_input_dev->phys = "wmi/input0";
222 dell_wmi_input_dev->id.bustype = BUS_HOST;
890a7c8e
DT
223
224 if (dell_new_hk_type) {
225 const struct key_entry *keymap = dell_wmi_prepare_new_keymap();
226 if (!keymap) {
227 err = -ENOMEM;
228 goto err_free_dev;
0b3f6109 229 }
0b3f6109 230
890a7c8e 231 err = sparse_keymap_setup(dell_wmi_input_dev, keymap, NULL);
0b3f6109 232
890a7c8e
DT
233 /*
234 * Sparse keymap library makes a copy of keymap so we
235 * don't need the original one that was allocated.
236 */
237 kfree(keymap);
238 } else {
239 err = sparse_keymap_setup(dell_wmi_input_dev,
240 dell_wmi_legacy_keymap, NULL);
0b3f6109 241 }
890a7c8e
DT
242 if (err)
243 goto err_free_dev;
244
245 err = input_register_device(dell_wmi_input_dev);
246 if (err)
247 goto err_free_keymap;
0b3f6109
MG
248
249 return 0;
890a7c8e
DT
250
251 err_free_keymap:
252 sparse_keymap_free(dell_wmi_input_dev);
253 err_free_dev:
254 input_free_device(dell_wmi_input_dev);
255 return err;
256}
257
258static void dell_wmi_input_destroy(void)
259{
260 sparse_keymap_free(dell_wmi_input_dev);
261 input_unregister_device(dell_wmi_input_dev);
262}
263
264static void __init find_hk_type(const struct dmi_header *dm, void *dummy)
265{
266 if (dm->type == 0xb2 && dm->length > 6) {
267 dell_new_hk_type = true;
268 dell_bios_hotkey_table =
269 container_of(dm, struct dell_bios_hotkey_table, header);
270 }
0b3f6109
MG
271}
272
273static int __init dell_wmi_init(void)
274{
275 int err;
abb631bf 276 acpi_status status;
0b3f6109 277
7a9568f5 278 if (!wmi_has_guid(DELL_EVENT_GUID)) {
eb889524 279 pr_warn("No known WMI GUID found\n");
1fdd407f
DT
280 return -ENODEV;
281 }
5ea25597 282
1fdd407f
DT
283 dmi_walk(find_hk_type, NULL);
284 acpi_video = acpi_video_backlight_support();
0b3f6109 285
1fdd407f 286 err = dell_wmi_input_setup();
890a7c8e 287 if (err)
1fdd407f 288 return err;
5ea25597 289
abb631bf 290 status = wmi_install_notify_handler(DELL_EVENT_GUID,
1fdd407f 291 dell_wmi_notify, NULL);
abb631bf 292 if (ACPI_FAILURE(status)) {
890a7c8e 293 dell_wmi_input_destroy();
eb889524 294 pr_err("Unable to register notify handler - %d\n", status);
abb631bf 295 return -ENODEV;
1fdd407f 296 }
0b3f6109
MG
297
298 return 0;
299}
890a7c8e 300module_init(dell_wmi_init);
0b3f6109
MG
301
302static void __exit dell_wmi_exit(void)
303{
1fdd407f 304 wmi_remove_notify_handler(DELL_EVENT_GUID);
890a7c8e 305 dell_wmi_input_destroy();
0b3f6109 306}
0b3f6109 307module_exit(dell_wmi_exit);