blob: 8a64c7967753c472e66b7f5de6344a81c3545a37 [file] [log] [blame]
Matthew Garrett0b3f6102009-01-09 20:17:11 +00001/*
2 * Dell WMI hotkeys
3 *
4 * Copyright (C) 2008 Red Hat <mjg@redhat.com>
Pali Rohár595773a42016-01-04 22:26:35 +01005 * Copyright (C) 2014-2015 Pali Rohár <pali.rohar@gmail.com>
Matthew Garrett0b3f6102009-01-09 20:17:11 +00006 *
7 * Portions based on wistron_btns.c:
8 * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
9 * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
10 * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26
Joe Percheseb889522011-03-29 15:21:37 -070027#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28
Matthew Garrett0b3f6102009-01-09 20:17:11 +000029#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Matthew Garrett0b3f6102009-01-09 20:17:11 +000033#include <linux/types.h>
34#include <linux/input.h>
Dmitry Torokhov890a7c82010-08-04 22:30:08 -070035#include <linux/input/sparse-keymap.h>
Matthew Garrett0b3f6102009-01-09 20:17:11 +000036#include <linux/acpi.h>
37#include <linux/string.h>
Rezwanul Kabir5ea25592009-11-02 12:00:42 -050038#include <linux/dmi.h>
Hans de Goede18bd7692015-06-16 16:28:01 +020039#include <acpi/video.h>
Michał Kępieńe09c4d52016-03-04 14:09:08 +010040#include "dell-smbios.h"
Matthew Garrett0b3f6102009-01-09 20:17:11 +000041
42MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
Pali Rohár595773a42016-01-04 22:26:35 +010043MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
Matthew Garrett0b3f6102009-01-09 20:17:11 +000044MODULE_DESCRIPTION("Dell laptop WMI hotkeys driver");
45MODULE_LICENSE("GPL");
46
47#define DELL_EVENT_GUID "9DBB5994-A997-11DA-B012-B622A1EF5492"
Pali Rohár595773a42016-01-04 22:26:35 +010048#define DELL_DESCRIPTOR_GUID "8D9DDCBC-A997-11DA-B012-B622A1EF5492"
Matthew Garrett0b3f6102009-01-09 20:17:11 +000049
Pali Rohár595773a42016-01-04 22:26:35 +010050static u32 dell_wmi_interface_version;
Michał Kępieńe09c4d52016-03-04 14:09:08 +010051static bool wmi_requires_smbios_request;
Matthew Garrett0b3f6102009-01-09 20:17:11 +000052
53MODULE_ALIAS("wmi:"DELL_EVENT_GUID);
Pali Rohár595773a42016-01-04 22:26:35 +010054MODULE_ALIAS("wmi:"DELL_DESCRIPTOR_GUID);
Matthew Garrett0b3f6102009-01-09 20:17:11 +000055
Michał Kępieńe09c4d52016-03-04 14:09:08 +010056static int __init dmi_matched(const struct dmi_system_id *dmi)
57{
58 wmi_requires_smbios_request = 1;
59 return 1;
60}
61
62static const struct dmi_system_id dell_wmi_smbios_list[] __initconst = {
63 {
64 .callback = dmi_matched,
Michał Kępieńaaf3a5e2016-03-04 14:09:10 +010065 .ident = "Dell Inspiron M5110",
66 .matches = {
67 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
68 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
69 },
70 },
71 {
72 .callback = dmi_matched,
Michał Kępieńe09c4d52016-03-04 14:09:08 +010073 .ident = "Dell Vostro V131",
74 .matches = {
75 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
76 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
77 },
78 },
79 { }
80};
81
Mario Limonciello5cab0092009-06-10 19:40:47 +000082/*
Pali Roháre075b3c2016-06-15 21:49:13 +020083 * Keymap for WMI events of type 0x0000
84 *
Mario Limonciello5cab0092009-06-10 19:40:47 +000085 * Certain keys are flagged as KE_IGNORE. All of these are either
86 * notifications (rather than requests for change) or are also sent
87 * via the keyboard controller so should not be sent again.
88 */
Pali Roháre075b3c2016-06-15 21:49:13 +020089static const struct key_entry dell_wmi_keymap_type_0000[] __initconst = {
Seth Forsheef1566f02011-08-01 15:46:10 -050090 { KE_IGNORE, 0x003a, { KEY_CAPSLOCK } },
91
Pali Rohár6a4d2602016-06-15 21:49:11 +020092 /* Key code is followed by brightness level */
93 { KE_KEY, 0xe005, { KEY_BRIGHTNESSDOWN } },
94 { KE_KEY, 0xe006, { KEY_BRIGHTNESSUP } },
Mario Limonciello5cab0092009-06-10 19:40:47 +000095
96 /* Battery health status button */
Pali Rohár6a4d2602016-06-15 21:49:11 +020097 { KE_KEY, 0xe007, { KEY_BATTERY } },
Mario Limonciello5cab0092009-06-10 19:40:47 +000098
Pali Rohár6a4d2602016-06-15 21:49:11 +020099 /* Radio devices state change, key code is followed by other values */
Gabriele Mazzotta8f8d75e2014-12-04 00:16:21 +0100100 { KE_IGNORE, 0xe008, { KEY_RFKILL } },
Mario Limonciello5cab0092009-06-10 19:40:47 +0000101
Pali Rohár6a4d2602016-06-15 21:49:11 +0200102 { KE_KEY, 0xe009, { KEY_EJECTCD } },
Mario Limonciello5cab0092009-06-10 19:40:47 +0000103
Pali Rohár6a4d2602016-06-15 21:49:11 +0200104 /* Key code is followed by: next, active and attached devices */
105 { KE_KEY, 0xe00b, { KEY_SWITCHVIDEOMODE } },
106
107 /* Key code is followed by keyboard illumination level */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700108 { KE_IGNORE, 0xe00c, { KEY_KBDILLUMTOGGLE } },
Mario Limonciello5cab0092009-06-10 19:40:47 +0000109
110 /* BIOS error detected */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700111 { KE_IGNORE, 0xe00d, { KEY_RESERVED } },
Mario Limonciello5cab0092009-06-10 19:40:47 +0000112
Pali Rohár65a97a62016-07-27 09:34:42 +0200113 /* Battery was removed or inserted */
114 { KE_IGNORE, 0xe00e, { KEY_RESERVED } },
Pali Rohár3237a862016-06-15 21:49:12 +0200115
Mario Limonciello5cab0092009-06-10 19:40:47 +0000116 /* Wifi Catcher */
Mario Limonciello915ac052016-07-28 12:53:47 -0500117 { KE_KEY, 0xe011, { KEY_WLAN } },
Mario Limonciello5cab0092009-06-10 19:40:47 +0000118
119 /* Ambient light sensor toggle */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700120 { KE_IGNORE, 0xe013, { KEY_RESERVED } },
Mario Limonciello5cab0092009-06-10 19:40:47 +0000121
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700122 { KE_IGNORE, 0xe020, { KEY_MUTE } },
Seth Forsheef1566f02011-08-01 15:46:10 -0500123
Pali Rohár3237a862016-06-15 21:49:12 +0200124 /* Unknown, defined in ACPI DSDT */
125 /* { KE_IGNORE, 0xe023, { KEY_RESERVED } }, */
126
127 /* Untested, Dell Instant Launch key on Inspiron 7520 */
128 /* { KE_IGNORE, 0xe024, { KEY_RESERVED } }, */
129
Michał Kępieńaaf3a5e2016-03-04 14:09:10 +0100130 /* Dell Instant Launch key */
Pali Rohár6a4d2602016-06-15 21:49:11 +0200131 { KE_KEY, 0xe025, { KEY_PROG4 } },
Michał Kępieńaaf3a5e2016-03-04 14:09:10 +0100132
133 /* Audio panel key */
Seth Forsheef1566f02011-08-01 15:46:10 -0500134 { KE_IGNORE, 0xe026, { KEY_RESERVED } },
135
Alex Hungab9bb11c2016-06-17 11:44:27 +0800136 /* LCD Display On/Off Control key */
137 { KE_KEY, 0xe027, { KEY_DISPLAYTOGGLE } },
138
Pali Rohár3237a862016-06-15 21:49:12 +0200139 /* Untested, Multimedia key on Dell Vostro 3560 */
140 /* { KE_IGNORE, 0xe028, { KEY_RESERVED } }, */
141
Pali Rohár6a4d2602016-06-15 21:49:11 +0200142 /* Dell Instant Launch key */
143 { KE_KEY, 0xe029, { KEY_PROG4 } },
144
Pali Rohár3237a862016-06-15 21:49:12 +0200145 /* Untested, Windows Mobility Center button on Inspiron 7520 */
146 /* { KE_IGNORE, 0xe02a, { KEY_RESERVED } }, */
147
148 /* Unknown, defined in ACPI DSDT */
149 /* { KE_IGNORE, 0xe02b, { KEY_RESERVED } }, */
150
151 /* Untested, Dell Audio With Preset Switch button on Inspiron 7520 */
152 /* { KE_IGNORE, 0xe02c, { KEY_RESERVED } }, */
153
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700154 { KE_IGNORE, 0xe02e, { KEY_VOLUMEDOWN } },
155 { KE_IGNORE, 0xe030, { KEY_VOLUMEUP } },
156 { KE_IGNORE, 0xe033, { KEY_KBDILLUMUP } },
157 { KE_IGNORE, 0xe034, { KEY_KBDILLUMDOWN } },
158 { KE_IGNORE, 0xe03a, { KEY_CAPSLOCK } },
Pali Rohár3de59bc2016-06-15 21:49:10 +0200159
Pali Rohár3237a862016-06-15 21:49:12 +0200160 /* NIC Link is Up */
161 { KE_IGNORE, 0xe043, { KEY_RESERVED } },
162
163 /* NIC Link is Down */
164 { KE_IGNORE, 0xe044, { KEY_RESERVED } },
165
Pali Rohár3de59bc2016-06-15 21:49:10 +0200166 /*
167 * This entry is very suspicious!
168 * Originally Matthew Garrett created this dell-wmi driver specially for
169 * "button with a picture of a battery" which has event code 0xe045.
170 * Later Mario Limonciello from Dell told us that event code 0xe045 is
171 * reported by Num Lock and should be ignored because key is send also
172 * by keyboard controller.
173 * So for now we will ignore this event to prevent potential double
174 * Num Lock key press.
175 */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700176 { KE_IGNORE, 0xe045, { KEY_NUMLOCK } },
Pali Rohár3de59bc2016-06-15 21:49:10 +0200177
Pali Rohár3237a862016-06-15 21:49:12 +0200178 /* Scroll lock and also going to tablet mode on portable devices */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700179 { KE_IGNORE, 0xe046, { KEY_SCROLLLOCK } },
Pali Rohár3237a862016-06-15 21:49:12 +0200180
181 /* Untested, going from tablet mode on portable devices */
182 /* { KE_IGNORE, 0xe047, { KEY_RESERVED } }, */
183
184 /* Dell Support Center key */
185 { KE_IGNORE, 0xe06e, { KEY_RESERVED } },
186
Seth Forsheef1566f02011-08-01 15:46:10 -0500187 { KE_IGNORE, 0xe0f7, { KEY_MUTE } },
188 { KE_IGNORE, 0xe0f8, { KEY_VOLUMEDOWN } },
189 { KE_IGNORE, 0xe0f9, { KEY_VOLUMEUP } },
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000190};
191
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700192struct dell_bios_keymap_entry {
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500193 u16 scancode;
194 u16 keycode;
195};
196
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700197struct dell_bios_hotkey_table {
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500198 struct dmi_header header;
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700199 struct dell_bios_keymap_entry keymap[];
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500200
201};
202
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800203struct dell_dmi_results {
204 int err;
Pali Roháre075b3c2016-06-15 21:49:13 +0200205 int keymap_size;
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800206 struct key_entry *keymap;
207};
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500208
Andy Lutomirskicbc61f12015-11-30 17:02:01 -0800209/* Uninitialized entries here are KEY_RESERVED == 0. */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700210static const u16 bios_to_linux_keycode[256] __initconst = {
Andy Lutomirski394cb772015-11-13 21:49:31 -0800211 [0] = KEY_MEDIA,
212 [1] = KEY_NEXTSONG,
213 [2] = KEY_PLAYPAUSE,
214 [3] = KEY_PREVIOUSSONG,
215 [4] = KEY_STOPCD,
216 [5] = KEY_UNKNOWN,
217 [6] = KEY_UNKNOWN,
218 [7] = KEY_UNKNOWN,
219 [8] = KEY_WWW,
220 [9] = KEY_UNKNOWN,
221 [10] = KEY_VOLUMEDOWN,
222 [11] = KEY_MUTE,
223 [12] = KEY_VOLUMEUP,
224 [13] = KEY_UNKNOWN,
225 [14] = KEY_BATTERY,
226 [15] = KEY_EJECTCD,
227 [16] = KEY_UNKNOWN,
228 [17] = KEY_SLEEP,
229 [18] = KEY_PROG1,
230 [19] = KEY_BRIGHTNESSDOWN,
231 [20] = KEY_BRIGHTNESSUP,
232 [21] = KEY_UNKNOWN,
233 [22] = KEY_KBDILLUMTOGGLE,
234 [23] = KEY_UNKNOWN,
235 [24] = KEY_SWITCHVIDEOMODE,
236 [25] = KEY_UNKNOWN,
237 [26] = KEY_UNKNOWN,
238 [27] = KEY_SWITCHVIDEOMODE,
239 [28] = KEY_UNKNOWN,
240 [29] = KEY_UNKNOWN,
241 [30] = KEY_PROG2,
242 [31] = KEY_UNKNOWN,
243 [32] = KEY_UNKNOWN,
244 [33] = KEY_UNKNOWN,
245 [34] = KEY_UNKNOWN,
246 [35] = KEY_UNKNOWN,
247 [36] = KEY_UNKNOWN,
248 [37] = KEY_UNKNOWN,
249 [38] = KEY_MICMUTE,
250 [255] = KEY_PROG3,
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500251};
252
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800253/*
Pali Roháre075b3c2016-06-15 21:49:13 +0200254 * Keymap for WMI events of type 0x0010
255 *
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800256 * These are applied if the 0xB2 DMI hotkey table is present and doesn't
257 * override them.
258 */
Pali Roháre075b3c2016-06-15 21:49:13 +0200259static const struct key_entry dell_wmi_keymap_type_0010[] __initconst = {
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800260 /* Fn-lock */
261 { KE_IGNORE, 0x151, { KEY_RESERVED } },
262
263 /* Change keyboard illumination */
264 { KE_IGNORE, 0x152, { KEY_KBDILLUMTOGGLE } },
265
266 /*
267 * Radio disable (notify only -- there is no model for which the
268 * WMI event is supposed to trigger an action).
269 */
270 { KE_IGNORE, 0x153, { KEY_RFKILL } },
271
272 /* RGB keyboard backlight control */
273 { KE_IGNORE, 0x154, { KEY_RESERVED } },
274
275 /* Stealth mode toggle */
276 { KE_IGNORE, 0x155, { KEY_RESERVED } },
Mario Limonciello5dc444b2016-07-28 12:53:48 -0500277
278 /* Rugged magnetic dock attach/detach events */
279 { KE_IGNORE, 0x156, { KEY_RESERVED } },
280 { KE_IGNORE, 0x157, { KEY_RESERVED } },
281
282 /* Rugged programmable (P1/P2/P3 keys) */
283 { KE_KEY, 0x850, { KEY_PROG1 } },
284 { KE_KEY, 0x851, { KEY_PROG2 } },
285 { KE_KEY, 0x852, { KEY_PROG3 } },
286
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800287};
288
Pali Roháre075b3c2016-06-15 21:49:13 +0200289/*
290 * Keymap for WMI events of type 0x0011
291 */
292static const struct key_entry dell_wmi_keymap_type_0011[] __initconst = {
293 /* Battery unplugged */
294 { KE_IGNORE, 0xfff0, { KEY_RESERVED } },
295
296 /* Battery inserted */
297 { KE_IGNORE, 0xfff1, { KEY_RESERVED } },
298
299 /* Keyboard backlight level changed */
300 { KE_IGNORE, 0x01e1, { KEY_RESERVED } },
301 { KE_IGNORE, 0x02ea, { KEY_RESERVED } },
302 { KE_IGNORE, 0x02eb, { KEY_RESERVED } },
303 { KE_IGNORE, 0x02ec, { KEY_RESERVED } },
304 { KE_IGNORE, 0x02f6, { KEY_RESERVED } },
305};
306
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000307static struct input_dev *dell_wmi_input_dev;
308
Pali Roháre075b3c2016-06-15 21:49:13 +0200309static void dell_wmi_process_key(int type, int code)
Pali Rohár83fc44c2014-11-11 20:21:22 +0100310{
311 const struct key_entry *key;
312
313 key = sparse_keymap_entry_from_scancode(dell_wmi_input_dev,
Pali Roháre075b3c2016-06-15 21:49:13 +0200314 (type << 16) | code);
Pali Rohár83fc44c2014-11-11 20:21:22 +0100315 if (!key) {
Pali Roháre075b3c2016-06-15 21:49:13 +0200316 pr_info("Unknown key with type 0x%04x and code 0x%04x pressed\n",
317 type, code);
Pali Rohár83fc44c2014-11-11 20:21:22 +0100318 return;
319 }
320
Pali Roháre075b3c2016-06-15 21:49:13 +0200321 pr_debug("Key with type 0x%04x and code 0x%04x pressed\n", type, code);
Pali Rohár83fc44c2014-11-11 20:21:22 +0100322
323 /* Don't report brightness notifications that will also come via ACPI */
324 if ((key->keycode == KEY_BRIGHTNESSUP ||
Hans de Goede61679c72015-12-22 19:09:49 +0100325 key->keycode == KEY_BRIGHTNESSDOWN) &&
326 acpi_video_handles_brightness_key_presses())
Pali Rohár83fc44c2014-11-11 20:21:22 +0100327 return;
328
Pali Roháre075b3c2016-06-15 21:49:13 +0200329 if (type == 0x0000 && code == 0xe025 && !wmi_requires_smbios_request)
Michał Kępień13f50592016-03-04 14:09:09 +0100330 return;
331
Hans de Goede9c656b02017-03-16 11:55:35 +0100332 if (key->keycode == KEY_KBDILLUMTOGGLE)
333 dell_laptop_call_notifier(
334 DELL_LAPTOP_KBD_BACKLIGHT_BRIGHTNESS_CHANGED, NULL);
335
Pali Rohár83fc44c2014-11-11 20:21:22 +0100336 sparse_keymap_report_entry(dell_wmi_input_dev, key, 1, true);
337}
338
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000339static void dell_wmi_notify(u32 value, void *context)
340{
341 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000342 union acpi_object *obj;
Len Brownfda11e62009-12-26 23:02:24 -0500343 acpi_status status;
Pali Rohár83fc44c2014-11-11 20:21:22 +0100344 acpi_size buffer_size;
345 u16 *buffer_entry, *buffer_end;
346 int len, i;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000347
Len Brownfda11e62009-12-26 23:02:24 -0500348 status = wmi_get_event_data(value, &response);
349 if (status != AE_OK) {
Pali Rohár83fc44c2014-11-11 20:21:22 +0100350 pr_warn("bad event status 0x%x\n", status);
Len Brownfda11e62009-12-26 23:02:24 -0500351 return;
352 }
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000353
354 obj = (union acpi_object *)response.pointer;
Pali Rohár83fc44c2014-11-11 20:21:22 +0100355 if (!obj) {
356 pr_warn("no response\n");
357 return;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000358 }
Pali Rohár83fc44c2014-11-11 20:21:22 +0100359
360 if (obj->type != ACPI_TYPE_BUFFER) {
361 pr_warn("bad response type %x\n", obj->type);
362 kfree(obj);
363 return;
364 }
365
366 pr_debug("Received WMI event (%*ph)\n",
367 obj->buffer.length, obj->buffer.pointer);
368
369 buffer_entry = (u16 *)obj->buffer.pointer;
370 buffer_size = obj->buffer.length/2;
Pali Rohár83fc44c2014-11-11 20:21:22 +0100371 buffer_end = buffer_entry + buffer_size;
372
Pali Rohár481fe5b2016-01-04 22:26:36 +0100373 /*
374 * BIOS/ACPI on devices with WMI interface version 0 does not clear
375 * buffer before filling it. So next time when BIOS/ACPI send WMI event
376 * which is smaller as previous then it contains garbage in buffer from
377 * previous event.
378 *
379 * BIOS/ACPI on devices with WMI interface version 1 clears buffer and
380 * sometimes send more events in buffer at one call.
381 *
382 * So to prevent reading garbage from buffer we will process only first
383 * one event on devices with WMI interface version 0.
384 */
385 if (dell_wmi_interface_version == 0 && buffer_entry < buffer_end)
386 if (buffer_end > buffer_entry + buffer_entry[0] + 1)
387 buffer_end = buffer_entry + buffer_entry[0] + 1;
388
Pali Rohár83fc44c2014-11-11 20:21:22 +0100389 while (buffer_entry < buffer_end) {
390
391 len = buffer_entry[0];
392 if (len == 0)
393 break;
394
395 len++;
396
397 if (buffer_entry + len > buffer_end) {
398 pr_warn("Invalid length of WMI event\n");
399 break;
400 }
401
402 pr_debug("Process buffer (%*ph)\n", len*2, buffer_entry);
403
404 switch (buffer_entry[1]) {
Pali Roháre075b3c2016-06-15 21:49:13 +0200405 case 0x0000: /* One key pressed or event occurred */
406 if (len > 2)
407 dell_wmi_process_key(0x0000, buffer_entry[2]);
408 /* Other entries could contain additional information */
Pali Rohár83fc44c2014-11-11 20:21:22 +0100409 break;
Pali Roháre075b3c2016-06-15 21:49:13 +0200410 case 0x0010: /* Sequence of keys pressed */
411 case 0x0011: /* Sequence of events occurred */
Pali Rohár83fc44c2014-11-11 20:21:22 +0100412 for (i = 2; i < len; ++i)
Pali Roháre075b3c2016-06-15 21:49:13 +0200413 dell_wmi_process_key(buffer_entry[1],
414 buffer_entry[i]);
Pali Rohár83fc44c2014-11-11 20:21:22 +0100415 break;
Pali Roháre075b3c2016-06-15 21:49:13 +0200416 default: /* Unknown event */
Pali Rohár83fc44c2014-11-11 20:21:22 +0100417 pr_info("Unknown WMI event type 0x%x\n",
418 (int)buffer_entry[1]);
419 break;
420 }
421
422 buffer_entry += len;
423
424 }
425
Anisse Astier3e9b9882009-12-04 10:10:09 +0100426 kfree(obj);
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000427}
428
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800429static bool have_scancode(u32 scancode, const struct key_entry *keymap, int len)
430{
431 int i;
432
433 for (i = 0; i < len; i++)
434 if (keymap[i].code == scancode)
435 return true;
436
437 return false;
438}
439
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800440static void __init handle_dmi_entry(const struct dmi_header *dm,
441 void *opaque)
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800442
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500443{
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800444 struct dell_dmi_results *results = opaque;
445 struct dell_bios_hotkey_table *table;
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800446 int hotkey_num, i, pos = 0;
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700447 struct key_entry *keymap;
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800448
449 if (results->err || results->keymap)
450 return; /* We already found the hotkey table. */
451
Andy Lutomirskib13de702016-02-15 08:32:35 -0800452 if (dm->type != 0xb2)
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800453 return;
454
455 table = container_of(dm, struct dell_bios_hotkey_table, header);
456
Andy Lutomirskib13de702016-02-15 08:32:35 -0800457 hotkey_num = (table->header.length -
458 sizeof(struct dell_bios_hotkey_table)) /
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800459 sizeof(struct dell_bios_keymap_entry);
Andy Lutomirskib13de702016-02-15 08:32:35 -0800460 if (hotkey_num < 1) {
461 /*
462 * Historically, dell-wmi would ignore a DMI entry of
463 * fewer than 7 bytes. Sizes between 4 and 8 bytes are
464 * nonsensical (both the header and all entries are 4
465 * bytes), so we approximate the old behavior by
466 * ignoring tables with fewer than one entry.
467 */
468 return;
469 }
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500470
Pali Roháre075b3c2016-06-15 21:49:13 +0200471 keymap = kcalloc(hotkey_num, sizeof(struct key_entry), GFP_KERNEL);
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800472 if (!keymap) {
473 results->err = -ENOMEM;
474 return;
475 }
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500476
477 for (i = 0; i < hotkey_num; i++) {
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700478 const struct dell_bios_keymap_entry *bios_entry =
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800479 &table->keymap[i];
Andy Lutomirskicbc61f12015-11-30 17:02:01 -0800480
481 /* Uninitialized entries are 0 aka KEY_RESERVED. */
482 u16 keycode = (bios_entry->keycode <
483 ARRAY_SIZE(bios_to_linux_keycode)) ?
484 bios_to_linux_keycode[bios_entry->keycode] :
485 KEY_RESERVED;
486
487 /*
488 * Log if we find an entry in the DMI table that we don't
489 * understand. If this happens, we should figure out what
490 * the entry means and add it to bios_to_linux_keycode.
491 */
492 if (keycode == KEY_RESERVED) {
493 pr_info("firmware scancode 0x%x maps to unrecognized keycode 0x%x\n",
494 bios_entry->scancode, bios_entry->keycode);
495 continue;
496 }
Gabriele Mazzotta8cb8e632014-12-04 00:16:23 +0100497
498 if (keycode == KEY_KBDILLUMTOGGLE)
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800499 keymap[pos].type = KE_IGNORE;
Gabriele Mazzotta8cb8e632014-12-04 00:16:23 +0100500 else
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800501 keymap[pos].type = KE_KEY;
502 keymap[pos].code = bios_entry->scancode;
503 keymap[pos].keycode = keycode;
504
505 pos++;
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500506 }
507
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800508 results->keymap = keymap;
Pali Roháre075b3c2016-06-15 21:49:13 +0200509 results->keymap_size = pos;
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500510}
511
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000512static int __init dell_wmi_input_setup(void)
513{
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800514 struct dell_dmi_results dmi_results = {};
Pali Roháre075b3c2016-06-15 21:49:13 +0200515 struct key_entry *keymap;
516 int err, i, pos = 0;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000517
518 dell_wmi_input_dev = input_allocate_device();
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000519 if (!dell_wmi_input_dev)
520 return -ENOMEM;
521
522 dell_wmi_input_dev->name = "Dell WMI hotkeys";
523 dell_wmi_input_dev->phys = "wmi/input0";
524 dell_wmi_input_dev->id.bustype = BUS_HOST;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000525
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800526 if (dmi_walk(handle_dmi_entry, &dmi_results)) {
527 /*
528 * Historically, dell-wmi ignored dmi_walk errors. A failure
529 * is certainly surprising, but it probably just indicates
530 * a very old laptop.
531 */
532 pr_warn("no DMI; using the old-style hotkey interface\n");
533 }
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700534
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800535 if (dmi_results.err) {
536 err = dmi_results.err;
537 goto err_free_dev;
538 }
539
Pali Roháre075b3c2016-06-15 21:49:13 +0200540 keymap = kcalloc(dmi_results.keymap_size +
541 ARRAY_SIZE(dell_wmi_keymap_type_0000) +
542 ARRAY_SIZE(dell_wmi_keymap_type_0010) +
543 ARRAY_SIZE(dell_wmi_keymap_type_0011) +
544 1,
545 sizeof(struct key_entry), GFP_KERNEL);
546 if (!keymap) {
547 kfree(dmi_results.keymap);
548 err = -ENOMEM;
549 goto err_free_dev;
550 }
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800551
Pali Roháre075b3c2016-06-15 21:49:13 +0200552 /* Append table with events of type 0x0010 which comes from DMI */
553 for (i = 0; i < dmi_results.keymap_size; i++) {
554 keymap[pos] = dmi_results.keymap[i];
555 keymap[pos].code |= (0x0010 << 16);
556 pos++;
557 }
558
559 kfree(dmi_results.keymap);
560
561 /* Append table with extra events of type 0x0010 which are not in DMI */
562 for (i = 0; i < ARRAY_SIZE(dell_wmi_keymap_type_0010); i++) {
563 const struct key_entry *entry = &dell_wmi_keymap_type_0010[i];
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700564
565 /*
Pali Roháre075b3c2016-06-15 21:49:13 +0200566 * Check if we've already found this scancode. This takes
567 * quadratic time, but it doesn't matter unless the list
568 * of extra keys gets very long.
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700569 */
Pali Roháre075b3c2016-06-15 21:49:13 +0200570 if (dmi_results.keymap_size &&
571 have_scancode(entry->code | (0x0010 << 16),
572 keymap, dmi_results.keymap_size)
573 )
574 continue;
575
576 keymap[pos] = *entry;
577 keymap[pos].code |= (0x0010 << 16);
578 pos++;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000579 }
Pali Roháre075b3c2016-06-15 21:49:13 +0200580
581 /* Append table with events of type 0x0011 */
582 for (i = 0; i < ARRAY_SIZE(dell_wmi_keymap_type_0011); i++) {
583 keymap[pos] = dell_wmi_keymap_type_0011[i];
584 keymap[pos].code |= (0x0011 << 16);
585 pos++;
586 }
587
588 /*
589 * Now append also table with "legacy" events of type 0x0000. Some of
590 * them are reported also on laptops which have scancodes in DMI.
591 */
592 for (i = 0; i < ARRAY_SIZE(dell_wmi_keymap_type_0000); i++) {
593 keymap[pos] = dell_wmi_keymap_type_0000[i];
594 pos++;
595 }
596
597 keymap[pos].type = KE_END;
598
599 err = sparse_keymap_setup(dell_wmi_input_dev, keymap, NULL);
600 /*
601 * Sparse keymap library makes a copy of keymap so we don't need the
602 * original one that was allocated.
603 */
604 kfree(keymap);
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700605 if (err)
606 goto err_free_dev;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000607
608 err = input_register_device(dell_wmi_input_dev);
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700609 if (err)
Michał Kępień815edfe2017-03-09 13:11:41 +0100610 goto err_free_dev;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000611
612 return 0;
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700613
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700614 err_free_dev:
615 input_free_device(dell_wmi_input_dev);
616 return err;
617}
618
Pali Rohár595773a42016-01-04 22:26:35 +0100619/*
620 * Descriptor buffer is 128 byte long and contains:
621 *
622 * Name Offset Length Value
623 * Vendor Signature 0 4 "DELL"
624 * Object Signature 4 4 " WMI"
625 * WMI Interface Version 8 4 <version>
626 * WMI buffer length 12 4 4096
627 */
628static int __init dell_wmi_check_descriptor_buffer(void)
629{
630 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
631 union acpi_object *obj;
632 acpi_status status;
633 u32 *buffer;
634
635 status = wmi_query_block(DELL_DESCRIPTOR_GUID, 0, &out);
636 if (ACPI_FAILURE(status)) {
637 pr_err("Cannot read Dell descriptor buffer - %d\n", status);
638 return status;
639 }
640
641 obj = (union acpi_object *)out.pointer;
642 if (!obj) {
643 pr_err("Dell descriptor buffer is empty\n");
644 return -EINVAL;
645 }
646
647 if (obj->type != ACPI_TYPE_BUFFER) {
648 pr_err("Cannot read Dell descriptor buffer\n");
649 kfree(obj);
650 return -EINVAL;
651 }
652
653 if (obj->buffer.length != 128) {
654 pr_err("Dell descriptor buffer has invalid length (%d)\n",
655 obj->buffer.length);
656 if (obj->buffer.length < 16) {
657 kfree(obj);
658 return -EINVAL;
659 }
660 }
661
662 buffer = (u32 *)obj->buffer.pointer;
663
664 if (buffer[0] != 0x4C4C4544 && buffer[1] != 0x494D5720)
665 pr_warn("Dell descriptor buffer has invalid signature (%*ph)\n",
666 8, buffer);
667
668 if (buffer[2] != 0 && buffer[2] != 1)
669 pr_warn("Dell descriptor buffer has unknown version (%d)\n",
670 buffer[2]);
671
672 if (buffer[3] != 4096)
673 pr_warn("Dell descriptor buffer has invalid buffer length (%d)\n",
674 buffer[3]);
675
676 dell_wmi_interface_version = buffer[2];
677
678 pr_info("Detected Dell WMI interface version %u\n",
679 dell_wmi_interface_version);
680
681 kfree(obj);
682 return 0;
683}
684
Michał Kępieńe09c4d52016-03-04 14:09:08 +0100685/*
686 * According to Dell SMBIOS documentation:
687 *
688 * 17 3 Application Program Registration
689 *
690 * cbArg1 Application ID 1 = 0x00010000
691 * cbArg2 Application ID 2
692 * QUICKSET/DCP = 0x51534554 "QSET"
693 * ALS Driver = 0x416c7353 "AlsS"
694 * Latitude ON = 0x4c6f6e52 "LonR"
695 * cbArg3 Application version or revision number
696 * cbArg4 0 = Unregister application
697 * 1 = Register application
698 * cbRes1 Standard return codes (0, -1, -2)
699 */
700
701static int dell_wmi_events_set_enabled(bool enable)
702{
703 struct calling_interface_buffer *buffer;
704 int ret;
705
706 buffer = dell_smbios_get_buffer();
707 buffer->input[0] = 0x10000;
708 buffer->input[1] = 0x51534554;
709 buffer->input[3] = enable;
710 dell_smbios_send_request(17, 3);
711 ret = buffer->output[0];
712 dell_smbios_release_buffer();
713
714 return dell_smbios_error(ret);
715}
716
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000717static int __init dell_wmi_init(void)
718{
719 int err;
Len Brownabb631b2009-12-26 21:51:38 -0500720 acpi_status status;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000721
Pali Rohár595773a42016-01-04 22:26:35 +0100722 if (!wmi_has_guid(DELL_EVENT_GUID) ||
723 !wmi_has_guid(DELL_DESCRIPTOR_GUID)) {
724 pr_warn("Dell WMI GUID were not found\n");
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800725 return -ENODEV;
726 }
727
Pali Rohár595773a42016-01-04 22:26:35 +0100728 err = dell_wmi_check_descriptor_buffer();
729 if (err)
730 return err;
731
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800732 err = dell_wmi_input_setup();
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700733 if (err)
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800734 return err;
735
Len Brownabb631b2009-12-26 21:51:38 -0500736 status = wmi_install_notify_handler(DELL_EVENT_GUID,
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800737 dell_wmi_notify, NULL);
Len Brownabb631b2009-12-26 21:51:38 -0500738 if (ACPI_FAILURE(status)) {
Michał Kępień815edfe2017-03-09 13:11:41 +0100739 input_unregister_device(dell_wmi_input_dev);
Joe Percheseb889522011-03-29 15:21:37 -0700740 pr_err("Unable to register notify handler - %d\n", status);
Len Brownabb631b2009-12-26 21:51:38 -0500741 return -ENODEV;
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800742 }
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000743
Michał Kępieńe09c4d52016-03-04 14:09:08 +0100744 dmi_check_system(dell_wmi_smbios_list);
745
746 if (wmi_requires_smbios_request) {
747 err = dell_wmi_events_set_enabled(true);
748 if (err) {
749 pr_err("Failed to enable WMI events\n");
750 wmi_remove_notify_handler(DELL_EVENT_GUID);
Michał Kępień815edfe2017-03-09 13:11:41 +0100751 input_unregister_device(dell_wmi_input_dev);
Michał Kępieńe09c4d52016-03-04 14:09:08 +0100752 return err;
753 }
754 }
755
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000756 return 0;
757}
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700758module_init(dell_wmi_init);
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000759
760static void __exit dell_wmi_exit(void)
761{
Michał Kępieńe09c4d52016-03-04 14:09:08 +0100762 if (wmi_requires_smbios_request)
763 dell_wmi_events_set_enabled(false);
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800764 wmi_remove_notify_handler(DELL_EVENT_GUID);
Michał Kępień815edfe2017-03-09 13:11:41 +0100765 input_unregister_device(dell_wmi_input_dev);
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000766}
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000767module_exit(dell_wmi_exit);