blob: b8ad055078cf3fec5458604b597dcfb3590d0a5e [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ár3237a862016-06-15 21:49:12 +0200113 /* Unknown, defined in ACPI DSDT */
114 /* { KE_IGNORE, 0xe00e, { KEY_RESERVED } }, */
115
Mario Limonciello5cab0092009-06-10 19:40:47 +0000116 /* Wifi Catcher */
Pali Rohár6a4d2602016-06-15 21:49:11 +0200117 { KE_KEY, 0xe011, { KEY_PROG2 } },
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
Pali Rohár3237a862016-06-15 21:49:12 +0200136 /* Untested, Multimedia key on Dell Vostro 3560 */
137 /* { KE_IGNORE, 0xe028, { KEY_RESERVED } }, */
138
Pali Rohár6a4d2602016-06-15 21:49:11 +0200139 /* Dell Instant Launch key */
140 { KE_KEY, 0xe029, { KEY_PROG4 } },
141
Pali Rohár3237a862016-06-15 21:49:12 +0200142 /* Untested, Windows Mobility Center button on Inspiron 7520 */
143 /* { KE_IGNORE, 0xe02a, { KEY_RESERVED } }, */
144
145 /* Unknown, defined in ACPI DSDT */
146 /* { KE_IGNORE, 0xe02b, { KEY_RESERVED } }, */
147
148 /* Untested, Dell Audio With Preset Switch button on Inspiron 7520 */
149 /* { KE_IGNORE, 0xe02c, { KEY_RESERVED } }, */
150
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700151 { KE_IGNORE, 0xe02e, { KEY_VOLUMEDOWN } },
152 { KE_IGNORE, 0xe030, { KEY_VOLUMEUP } },
153 { KE_IGNORE, 0xe033, { KEY_KBDILLUMUP } },
154 { KE_IGNORE, 0xe034, { KEY_KBDILLUMDOWN } },
155 { KE_IGNORE, 0xe03a, { KEY_CAPSLOCK } },
Pali Rohár3de59bc2016-06-15 21:49:10 +0200156
Pali Rohár3237a862016-06-15 21:49:12 +0200157 /* NIC Link is Up */
158 { KE_IGNORE, 0xe043, { KEY_RESERVED } },
159
160 /* NIC Link is Down */
161 { KE_IGNORE, 0xe044, { KEY_RESERVED } },
162
Pali Rohár3de59bc2016-06-15 21:49:10 +0200163 /*
164 * This entry is very suspicious!
165 * Originally Matthew Garrett created this dell-wmi driver specially for
166 * "button with a picture of a battery" which has event code 0xe045.
167 * Later Mario Limonciello from Dell told us that event code 0xe045 is
168 * reported by Num Lock and should be ignored because key is send also
169 * by keyboard controller.
170 * So for now we will ignore this event to prevent potential double
171 * Num Lock key press.
172 */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700173 { KE_IGNORE, 0xe045, { KEY_NUMLOCK } },
Pali Rohár3de59bc2016-06-15 21:49:10 +0200174
Pali Rohár3237a862016-06-15 21:49:12 +0200175 /* Scroll lock and also going to tablet mode on portable devices */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700176 { KE_IGNORE, 0xe046, { KEY_SCROLLLOCK } },
Pali Rohár3237a862016-06-15 21:49:12 +0200177
178 /* Untested, going from tablet mode on portable devices */
179 /* { KE_IGNORE, 0xe047, { KEY_RESERVED } }, */
180
181 /* Dell Support Center key */
182 { KE_IGNORE, 0xe06e, { KEY_RESERVED } },
183
Seth Forsheef1566f02011-08-01 15:46:10 -0500184 { KE_IGNORE, 0xe0f7, { KEY_MUTE } },
185 { KE_IGNORE, 0xe0f8, { KEY_VOLUMEDOWN } },
186 { KE_IGNORE, 0xe0f9, { KEY_VOLUMEUP } },
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000187};
188
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700189struct dell_bios_keymap_entry {
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500190 u16 scancode;
191 u16 keycode;
192};
193
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700194struct dell_bios_hotkey_table {
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500195 struct dmi_header header;
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700196 struct dell_bios_keymap_entry keymap[];
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500197
198};
199
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800200struct dell_dmi_results {
201 int err;
Pali Roháre075b3c2016-06-15 21:49:13 +0200202 int keymap_size;
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800203 struct key_entry *keymap;
204};
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500205
Andy Lutomirskicbc61f12015-11-30 17:02:01 -0800206/* Uninitialized entries here are KEY_RESERVED == 0. */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700207static const u16 bios_to_linux_keycode[256] __initconst = {
Andy Lutomirski394cb772015-11-13 21:49:31 -0800208 [0] = KEY_MEDIA,
209 [1] = KEY_NEXTSONG,
210 [2] = KEY_PLAYPAUSE,
211 [3] = KEY_PREVIOUSSONG,
212 [4] = KEY_STOPCD,
213 [5] = KEY_UNKNOWN,
214 [6] = KEY_UNKNOWN,
215 [7] = KEY_UNKNOWN,
216 [8] = KEY_WWW,
217 [9] = KEY_UNKNOWN,
218 [10] = KEY_VOLUMEDOWN,
219 [11] = KEY_MUTE,
220 [12] = KEY_VOLUMEUP,
221 [13] = KEY_UNKNOWN,
222 [14] = KEY_BATTERY,
223 [15] = KEY_EJECTCD,
224 [16] = KEY_UNKNOWN,
225 [17] = KEY_SLEEP,
226 [18] = KEY_PROG1,
227 [19] = KEY_BRIGHTNESSDOWN,
228 [20] = KEY_BRIGHTNESSUP,
229 [21] = KEY_UNKNOWN,
230 [22] = KEY_KBDILLUMTOGGLE,
231 [23] = KEY_UNKNOWN,
232 [24] = KEY_SWITCHVIDEOMODE,
233 [25] = KEY_UNKNOWN,
234 [26] = KEY_UNKNOWN,
235 [27] = KEY_SWITCHVIDEOMODE,
236 [28] = KEY_UNKNOWN,
237 [29] = KEY_UNKNOWN,
238 [30] = KEY_PROG2,
239 [31] = KEY_UNKNOWN,
240 [32] = KEY_UNKNOWN,
241 [33] = KEY_UNKNOWN,
242 [34] = KEY_UNKNOWN,
243 [35] = KEY_UNKNOWN,
244 [36] = KEY_UNKNOWN,
245 [37] = KEY_UNKNOWN,
246 [38] = KEY_MICMUTE,
247 [255] = KEY_PROG3,
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500248};
249
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800250/*
Pali Roháre075b3c2016-06-15 21:49:13 +0200251 * Keymap for WMI events of type 0x0010
252 *
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800253 * These are applied if the 0xB2 DMI hotkey table is present and doesn't
254 * override them.
255 */
Pali Roháre075b3c2016-06-15 21:49:13 +0200256static const struct key_entry dell_wmi_keymap_type_0010[] __initconst = {
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800257 /* Fn-lock */
258 { KE_IGNORE, 0x151, { KEY_RESERVED } },
259
260 /* Change keyboard illumination */
261 { KE_IGNORE, 0x152, { KEY_KBDILLUMTOGGLE } },
262
263 /*
264 * Radio disable (notify only -- there is no model for which the
265 * WMI event is supposed to trigger an action).
266 */
267 { KE_IGNORE, 0x153, { KEY_RFKILL } },
268
269 /* RGB keyboard backlight control */
270 { KE_IGNORE, 0x154, { KEY_RESERVED } },
271
272 /* Stealth mode toggle */
273 { KE_IGNORE, 0x155, { KEY_RESERVED } },
274};
275
Pali Roháre075b3c2016-06-15 21:49:13 +0200276/*
277 * Keymap for WMI events of type 0x0011
278 */
279static const struct key_entry dell_wmi_keymap_type_0011[] __initconst = {
280 /* Battery unplugged */
281 { KE_IGNORE, 0xfff0, { KEY_RESERVED } },
282
283 /* Battery inserted */
284 { KE_IGNORE, 0xfff1, { KEY_RESERVED } },
285
286 /* Keyboard backlight level changed */
287 { KE_IGNORE, 0x01e1, { KEY_RESERVED } },
288 { KE_IGNORE, 0x02ea, { KEY_RESERVED } },
289 { KE_IGNORE, 0x02eb, { KEY_RESERVED } },
290 { KE_IGNORE, 0x02ec, { KEY_RESERVED } },
291 { KE_IGNORE, 0x02f6, { KEY_RESERVED } },
292};
293
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000294static struct input_dev *dell_wmi_input_dev;
295
Pali Roháre075b3c2016-06-15 21:49:13 +0200296static void dell_wmi_process_key(int type, int code)
Pali Rohár83fc44c2014-11-11 20:21:22 +0100297{
298 const struct key_entry *key;
299
300 key = sparse_keymap_entry_from_scancode(dell_wmi_input_dev,
Pali Roháre075b3c2016-06-15 21:49:13 +0200301 (type << 16) | code);
Pali Rohár83fc44c2014-11-11 20:21:22 +0100302 if (!key) {
Pali Roháre075b3c2016-06-15 21:49:13 +0200303 pr_info("Unknown key with type 0x%04x and code 0x%04x pressed\n",
304 type, code);
Pali Rohár83fc44c2014-11-11 20:21:22 +0100305 return;
306 }
307
Pali Roháre075b3c2016-06-15 21:49:13 +0200308 pr_debug("Key with type 0x%04x and code 0x%04x pressed\n", type, code);
Pali Rohár83fc44c2014-11-11 20:21:22 +0100309
310 /* Don't report brightness notifications that will also come via ACPI */
311 if ((key->keycode == KEY_BRIGHTNESSUP ||
Hans de Goede61679c72015-12-22 19:09:49 +0100312 key->keycode == KEY_BRIGHTNESSDOWN) &&
313 acpi_video_handles_brightness_key_presses())
Pali Rohár83fc44c2014-11-11 20:21:22 +0100314 return;
315
Pali Roháre075b3c2016-06-15 21:49:13 +0200316 if (type == 0x0000 && code == 0xe025 && !wmi_requires_smbios_request)
Michał Kępień13f50592016-03-04 14:09:09 +0100317 return;
318
Pali Rohár83fc44c2014-11-11 20:21:22 +0100319 sparse_keymap_report_entry(dell_wmi_input_dev, key, 1, true);
320}
321
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000322static void dell_wmi_notify(u32 value, void *context)
323{
324 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000325 union acpi_object *obj;
Len Brownfda11e62009-12-26 23:02:24 -0500326 acpi_status status;
Pali Rohár83fc44c2014-11-11 20:21:22 +0100327 acpi_size buffer_size;
328 u16 *buffer_entry, *buffer_end;
329 int len, i;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000330
Len Brownfda11e62009-12-26 23:02:24 -0500331 status = wmi_get_event_data(value, &response);
332 if (status != AE_OK) {
Pali Rohár83fc44c2014-11-11 20:21:22 +0100333 pr_warn("bad event status 0x%x\n", status);
Len Brownfda11e62009-12-26 23:02:24 -0500334 return;
335 }
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000336
337 obj = (union acpi_object *)response.pointer;
Pali Rohár83fc44c2014-11-11 20:21:22 +0100338 if (!obj) {
339 pr_warn("no response\n");
340 return;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000341 }
Pali Rohár83fc44c2014-11-11 20:21:22 +0100342
343 if (obj->type != ACPI_TYPE_BUFFER) {
344 pr_warn("bad response type %x\n", obj->type);
345 kfree(obj);
346 return;
347 }
348
349 pr_debug("Received WMI event (%*ph)\n",
350 obj->buffer.length, obj->buffer.pointer);
351
352 buffer_entry = (u16 *)obj->buffer.pointer;
353 buffer_size = obj->buffer.length/2;
Pali Rohár83fc44c2014-11-11 20:21:22 +0100354 buffer_end = buffer_entry + buffer_size;
355
Pali Rohár481fe5b2016-01-04 22:26:36 +0100356 /*
357 * BIOS/ACPI on devices with WMI interface version 0 does not clear
358 * buffer before filling it. So next time when BIOS/ACPI send WMI event
359 * which is smaller as previous then it contains garbage in buffer from
360 * previous event.
361 *
362 * BIOS/ACPI on devices with WMI interface version 1 clears buffer and
363 * sometimes send more events in buffer at one call.
364 *
365 * So to prevent reading garbage from buffer we will process only first
366 * one event on devices with WMI interface version 0.
367 */
368 if (dell_wmi_interface_version == 0 && buffer_entry < buffer_end)
369 if (buffer_end > buffer_entry + buffer_entry[0] + 1)
370 buffer_end = buffer_entry + buffer_entry[0] + 1;
371
Pali Rohár83fc44c2014-11-11 20:21:22 +0100372 while (buffer_entry < buffer_end) {
373
374 len = buffer_entry[0];
375 if (len == 0)
376 break;
377
378 len++;
379
380 if (buffer_entry + len > buffer_end) {
381 pr_warn("Invalid length of WMI event\n");
382 break;
383 }
384
385 pr_debug("Process buffer (%*ph)\n", len*2, buffer_entry);
386
387 switch (buffer_entry[1]) {
Pali Roháre075b3c2016-06-15 21:49:13 +0200388 case 0x0000: /* One key pressed or event occurred */
389 if (len > 2)
390 dell_wmi_process_key(0x0000, buffer_entry[2]);
391 /* Other entries could contain additional information */
Pali Rohár83fc44c2014-11-11 20:21:22 +0100392 break;
Pali Roháre075b3c2016-06-15 21:49:13 +0200393 case 0x0010: /* Sequence of keys pressed */
394 case 0x0011: /* Sequence of events occurred */
Pali Rohár83fc44c2014-11-11 20:21:22 +0100395 for (i = 2; i < len; ++i)
Pali Roháre075b3c2016-06-15 21:49:13 +0200396 dell_wmi_process_key(buffer_entry[1],
397 buffer_entry[i]);
Pali Rohár83fc44c2014-11-11 20:21:22 +0100398 break;
Pali Roháre075b3c2016-06-15 21:49:13 +0200399 default: /* Unknown event */
Pali Rohár83fc44c2014-11-11 20:21:22 +0100400 pr_info("Unknown WMI event type 0x%x\n",
401 (int)buffer_entry[1]);
402 break;
403 }
404
405 buffer_entry += len;
406
407 }
408
Anisse Astier3e9b9882009-12-04 10:10:09 +0100409 kfree(obj);
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000410}
411
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800412static bool have_scancode(u32 scancode, const struct key_entry *keymap, int len)
413{
414 int i;
415
416 for (i = 0; i < len; i++)
417 if (keymap[i].code == scancode)
418 return true;
419
420 return false;
421}
422
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800423static void __init handle_dmi_entry(const struct dmi_header *dm,
424 void *opaque)
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800425
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500426{
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800427 struct dell_dmi_results *results = opaque;
428 struct dell_bios_hotkey_table *table;
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800429 int hotkey_num, i, pos = 0;
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700430 struct key_entry *keymap;
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800431
432 if (results->err || results->keymap)
433 return; /* We already found the hotkey table. */
434
Andy Lutomirskib13de702016-02-15 08:32:35 -0800435 if (dm->type != 0xb2)
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800436 return;
437
438 table = container_of(dm, struct dell_bios_hotkey_table, header);
439
Andy Lutomirskib13de702016-02-15 08:32:35 -0800440 hotkey_num = (table->header.length -
441 sizeof(struct dell_bios_hotkey_table)) /
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800442 sizeof(struct dell_bios_keymap_entry);
Andy Lutomirskib13de702016-02-15 08:32:35 -0800443 if (hotkey_num < 1) {
444 /*
445 * Historically, dell-wmi would ignore a DMI entry of
446 * fewer than 7 bytes. Sizes between 4 and 8 bytes are
447 * nonsensical (both the header and all entries are 4
448 * bytes), so we approximate the old behavior by
449 * ignoring tables with fewer than one entry.
450 */
451 return;
452 }
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500453
Pali Roháre075b3c2016-06-15 21:49:13 +0200454 keymap = kcalloc(hotkey_num, sizeof(struct key_entry), GFP_KERNEL);
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800455 if (!keymap) {
456 results->err = -ENOMEM;
457 return;
458 }
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500459
460 for (i = 0; i < hotkey_num; i++) {
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700461 const struct dell_bios_keymap_entry *bios_entry =
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800462 &table->keymap[i];
Andy Lutomirskicbc61f12015-11-30 17:02:01 -0800463
464 /* Uninitialized entries are 0 aka KEY_RESERVED. */
465 u16 keycode = (bios_entry->keycode <
466 ARRAY_SIZE(bios_to_linux_keycode)) ?
467 bios_to_linux_keycode[bios_entry->keycode] :
468 KEY_RESERVED;
469
470 /*
471 * Log if we find an entry in the DMI table that we don't
472 * understand. If this happens, we should figure out what
473 * the entry means and add it to bios_to_linux_keycode.
474 */
475 if (keycode == KEY_RESERVED) {
476 pr_info("firmware scancode 0x%x maps to unrecognized keycode 0x%x\n",
477 bios_entry->scancode, bios_entry->keycode);
478 continue;
479 }
Gabriele Mazzotta8cb8e632014-12-04 00:16:23 +0100480
481 if (keycode == KEY_KBDILLUMTOGGLE)
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800482 keymap[pos].type = KE_IGNORE;
Gabriele Mazzotta8cb8e632014-12-04 00:16:23 +0100483 else
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800484 keymap[pos].type = KE_KEY;
485 keymap[pos].code = bios_entry->scancode;
486 keymap[pos].keycode = keycode;
487
488 pos++;
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500489 }
490
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800491 results->keymap = keymap;
Pali Roháre075b3c2016-06-15 21:49:13 +0200492 results->keymap_size = pos;
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500493}
494
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000495static int __init dell_wmi_input_setup(void)
496{
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800497 struct dell_dmi_results dmi_results = {};
Pali Roháre075b3c2016-06-15 21:49:13 +0200498 struct key_entry *keymap;
499 int err, i, pos = 0;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000500
501 dell_wmi_input_dev = input_allocate_device();
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000502 if (!dell_wmi_input_dev)
503 return -ENOMEM;
504
505 dell_wmi_input_dev->name = "Dell WMI hotkeys";
506 dell_wmi_input_dev->phys = "wmi/input0";
507 dell_wmi_input_dev->id.bustype = BUS_HOST;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000508
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800509 if (dmi_walk(handle_dmi_entry, &dmi_results)) {
510 /*
511 * Historically, dell-wmi ignored dmi_walk errors. A failure
512 * is certainly surprising, but it probably just indicates
513 * a very old laptop.
514 */
515 pr_warn("no DMI; using the old-style hotkey interface\n");
516 }
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700517
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800518 if (dmi_results.err) {
519 err = dmi_results.err;
520 goto err_free_dev;
521 }
522
Pali Roháre075b3c2016-06-15 21:49:13 +0200523 keymap = kcalloc(dmi_results.keymap_size +
524 ARRAY_SIZE(dell_wmi_keymap_type_0000) +
525 ARRAY_SIZE(dell_wmi_keymap_type_0010) +
526 ARRAY_SIZE(dell_wmi_keymap_type_0011) +
527 1,
528 sizeof(struct key_entry), GFP_KERNEL);
529 if (!keymap) {
530 kfree(dmi_results.keymap);
531 err = -ENOMEM;
532 goto err_free_dev;
533 }
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800534
Pali Roháre075b3c2016-06-15 21:49:13 +0200535 /* Append table with events of type 0x0010 which comes from DMI */
536 for (i = 0; i < dmi_results.keymap_size; i++) {
537 keymap[pos] = dmi_results.keymap[i];
538 keymap[pos].code |= (0x0010 << 16);
539 pos++;
540 }
541
542 kfree(dmi_results.keymap);
543
544 /* Append table with extra events of type 0x0010 which are not in DMI */
545 for (i = 0; i < ARRAY_SIZE(dell_wmi_keymap_type_0010); i++) {
546 const struct key_entry *entry = &dell_wmi_keymap_type_0010[i];
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700547
548 /*
Pali Roháre075b3c2016-06-15 21:49:13 +0200549 * Check if we've already found this scancode. This takes
550 * quadratic time, but it doesn't matter unless the list
551 * of extra keys gets very long.
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700552 */
Pali Roháre075b3c2016-06-15 21:49:13 +0200553 if (dmi_results.keymap_size &&
554 have_scancode(entry->code | (0x0010 << 16),
555 keymap, dmi_results.keymap_size)
556 )
557 continue;
558
559 keymap[pos] = *entry;
560 keymap[pos].code |= (0x0010 << 16);
561 pos++;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000562 }
Pali Roháre075b3c2016-06-15 21:49:13 +0200563
564 /* Append table with events of type 0x0011 */
565 for (i = 0; i < ARRAY_SIZE(dell_wmi_keymap_type_0011); i++) {
566 keymap[pos] = dell_wmi_keymap_type_0011[i];
567 keymap[pos].code |= (0x0011 << 16);
568 pos++;
569 }
570
571 /*
572 * Now append also table with "legacy" events of type 0x0000. Some of
573 * them are reported also on laptops which have scancodes in DMI.
574 */
575 for (i = 0; i < ARRAY_SIZE(dell_wmi_keymap_type_0000); i++) {
576 keymap[pos] = dell_wmi_keymap_type_0000[i];
577 pos++;
578 }
579
580 keymap[pos].type = KE_END;
581
582 err = sparse_keymap_setup(dell_wmi_input_dev, keymap, NULL);
583 /*
584 * Sparse keymap library makes a copy of keymap so we don't need the
585 * original one that was allocated.
586 */
587 kfree(keymap);
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700588 if (err)
589 goto err_free_dev;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000590
591 err = input_register_device(dell_wmi_input_dev);
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700592 if (err)
593 goto err_free_keymap;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000594
595 return 0;
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700596
597 err_free_keymap:
598 sparse_keymap_free(dell_wmi_input_dev);
599 err_free_dev:
600 input_free_device(dell_wmi_input_dev);
601 return err;
602}
603
604static void dell_wmi_input_destroy(void)
605{
606 sparse_keymap_free(dell_wmi_input_dev);
607 input_unregister_device(dell_wmi_input_dev);
608}
609
Pali Rohár595773a42016-01-04 22:26:35 +0100610/*
611 * Descriptor buffer is 128 byte long and contains:
612 *
613 * Name Offset Length Value
614 * Vendor Signature 0 4 "DELL"
615 * Object Signature 4 4 " WMI"
616 * WMI Interface Version 8 4 <version>
617 * WMI buffer length 12 4 4096
618 */
619static int __init dell_wmi_check_descriptor_buffer(void)
620{
621 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
622 union acpi_object *obj;
623 acpi_status status;
624 u32 *buffer;
625
626 status = wmi_query_block(DELL_DESCRIPTOR_GUID, 0, &out);
627 if (ACPI_FAILURE(status)) {
628 pr_err("Cannot read Dell descriptor buffer - %d\n", status);
629 return status;
630 }
631
632 obj = (union acpi_object *)out.pointer;
633 if (!obj) {
634 pr_err("Dell descriptor buffer is empty\n");
635 return -EINVAL;
636 }
637
638 if (obj->type != ACPI_TYPE_BUFFER) {
639 pr_err("Cannot read Dell descriptor buffer\n");
640 kfree(obj);
641 return -EINVAL;
642 }
643
644 if (obj->buffer.length != 128) {
645 pr_err("Dell descriptor buffer has invalid length (%d)\n",
646 obj->buffer.length);
647 if (obj->buffer.length < 16) {
648 kfree(obj);
649 return -EINVAL;
650 }
651 }
652
653 buffer = (u32 *)obj->buffer.pointer;
654
655 if (buffer[0] != 0x4C4C4544 && buffer[1] != 0x494D5720)
656 pr_warn("Dell descriptor buffer has invalid signature (%*ph)\n",
657 8, buffer);
658
659 if (buffer[2] != 0 && buffer[2] != 1)
660 pr_warn("Dell descriptor buffer has unknown version (%d)\n",
661 buffer[2]);
662
663 if (buffer[3] != 4096)
664 pr_warn("Dell descriptor buffer has invalid buffer length (%d)\n",
665 buffer[3]);
666
667 dell_wmi_interface_version = buffer[2];
668
669 pr_info("Detected Dell WMI interface version %u\n",
670 dell_wmi_interface_version);
671
672 kfree(obj);
673 return 0;
674}
675
Michał Kępieńe09c4d52016-03-04 14:09:08 +0100676/*
677 * According to Dell SMBIOS documentation:
678 *
679 * 17 3 Application Program Registration
680 *
681 * cbArg1 Application ID 1 = 0x00010000
682 * cbArg2 Application ID 2
683 * QUICKSET/DCP = 0x51534554 "QSET"
684 * ALS Driver = 0x416c7353 "AlsS"
685 * Latitude ON = 0x4c6f6e52 "LonR"
686 * cbArg3 Application version or revision number
687 * cbArg4 0 = Unregister application
688 * 1 = Register application
689 * cbRes1 Standard return codes (0, -1, -2)
690 */
691
692static int dell_wmi_events_set_enabled(bool enable)
693{
694 struct calling_interface_buffer *buffer;
695 int ret;
696
697 buffer = dell_smbios_get_buffer();
698 buffer->input[0] = 0x10000;
699 buffer->input[1] = 0x51534554;
700 buffer->input[3] = enable;
701 dell_smbios_send_request(17, 3);
702 ret = buffer->output[0];
703 dell_smbios_release_buffer();
704
705 return dell_smbios_error(ret);
706}
707
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000708static int __init dell_wmi_init(void)
709{
710 int err;
Len Brownabb631b2009-12-26 21:51:38 -0500711 acpi_status status;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000712
Pali Rohár595773a42016-01-04 22:26:35 +0100713 if (!wmi_has_guid(DELL_EVENT_GUID) ||
714 !wmi_has_guid(DELL_DESCRIPTOR_GUID)) {
715 pr_warn("Dell WMI GUID were not found\n");
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800716 return -ENODEV;
717 }
718
Pali Rohár595773a42016-01-04 22:26:35 +0100719 err = dell_wmi_check_descriptor_buffer();
720 if (err)
721 return err;
722
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800723 err = dell_wmi_input_setup();
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700724 if (err)
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800725 return err;
726
Len Brownabb631b2009-12-26 21:51:38 -0500727 status = wmi_install_notify_handler(DELL_EVENT_GUID,
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800728 dell_wmi_notify, NULL);
Len Brownabb631b2009-12-26 21:51:38 -0500729 if (ACPI_FAILURE(status)) {
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700730 dell_wmi_input_destroy();
Joe Percheseb889522011-03-29 15:21:37 -0700731 pr_err("Unable to register notify handler - %d\n", status);
Len Brownabb631b2009-12-26 21:51:38 -0500732 return -ENODEV;
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800733 }
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000734
Michał Kępieńe09c4d52016-03-04 14:09:08 +0100735 dmi_check_system(dell_wmi_smbios_list);
736
737 if (wmi_requires_smbios_request) {
738 err = dell_wmi_events_set_enabled(true);
739 if (err) {
740 pr_err("Failed to enable WMI events\n");
741 wmi_remove_notify_handler(DELL_EVENT_GUID);
742 dell_wmi_input_destroy();
743 return err;
744 }
745 }
746
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000747 return 0;
748}
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700749module_init(dell_wmi_init);
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000750
751static void __exit dell_wmi_exit(void)
752{
Michał Kępieńe09c4d52016-03-04 14:09:08 +0100753 if (wmi_requires_smbios_request)
754 dell_wmi_events_set_enabled(false);
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800755 wmi_remove_notify_handler(DELL_EVENT_GUID);
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700756 dell_wmi_input_destroy();
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000757}
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000758module_exit(dell_wmi_exit);