blob: b3b99709f824804b967aaf83cca97dd750e70126 [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/*
83 * Certain keys are flagged as KE_IGNORE. All of these are either
84 * notifications (rather than requests for change) or are also sent
85 * via the keyboard controller so should not be sent again.
86 */
Matthew Garrett0b3f6102009-01-09 20:17:11 +000087
Dmitry Torokhov890a7c82010-08-04 22:30:08 -070088static const struct key_entry dell_wmi_legacy_keymap[] __initconst = {
Seth Forsheef1566f02011-08-01 15:46:10 -050089 { KE_IGNORE, 0x003a, { KEY_CAPSLOCK } },
90
Pali Rohár6a4d2602016-06-15 21:49:11 +020091 /* Key code is followed by brightness level */
92 { KE_KEY, 0xe005, { KEY_BRIGHTNESSDOWN } },
93 { KE_KEY, 0xe006, { KEY_BRIGHTNESSUP } },
Mario Limonciello5cab0092009-06-10 19:40:47 +000094
95 /* Battery health status button */
Pali Rohár6a4d2602016-06-15 21:49:11 +020096 { KE_KEY, 0xe007, { KEY_BATTERY } },
Mario Limonciello5cab0092009-06-10 19:40:47 +000097
Pali Rohár6a4d2602016-06-15 21:49:11 +020098 /* Radio devices state change, key code is followed by other values */
Gabriele Mazzotta8f8d75e2014-12-04 00:16:21 +010099 { KE_IGNORE, 0xe008, { KEY_RFKILL } },
Mario Limonciello5cab0092009-06-10 19:40:47 +0000100
Pali Rohár6a4d2602016-06-15 21:49:11 +0200101 { KE_KEY, 0xe009, { KEY_EJECTCD } },
Mario Limonciello5cab0092009-06-10 19:40:47 +0000102
Pali Rohár6a4d2602016-06-15 21:49:11 +0200103 /* Key code is followed by: next, active and attached devices */
104 { KE_KEY, 0xe00b, { KEY_SWITCHVIDEOMODE } },
105
106 /* Key code is followed by keyboard illumination level */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700107 { KE_IGNORE, 0xe00c, { KEY_KBDILLUMTOGGLE } },
Mario Limonciello5cab0092009-06-10 19:40:47 +0000108
109 /* BIOS error detected */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700110 { KE_IGNORE, 0xe00d, { KEY_RESERVED } },
Mario Limonciello5cab0092009-06-10 19:40:47 +0000111
112 /* Wifi Catcher */
Pali Rohár6a4d2602016-06-15 21:49:11 +0200113 { KE_KEY, 0xe011, { KEY_PROG2 } },
Mario Limonciello5cab0092009-06-10 19:40:47 +0000114
115 /* Ambient light sensor toggle */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700116 { KE_IGNORE, 0xe013, { KEY_RESERVED } },
Mario Limonciello5cab0092009-06-10 19:40:47 +0000117
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700118 { KE_IGNORE, 0xe020, { KEY_MUTE } },
Seth Forsheef1566f02011-08-01 15:46:10 -0500119
Michał Kępieńaaf3a5e2016-03-04 14:09:10 +0100120 /* Dell Instant Launch key */
Pali Rohár6a4d2602016-06-15 21:49:11 +0200121 { KE_KEY, 0xe025, { KEY_PROG4 } },
Michał Kępieńaaf3a5e2016-03-04 14:09:10 +0100122
123 /* Audio panel key */
Seth Forsheef1566f02011-08-01 15:46:10 -0500124 { KE_IGNORE, 0xe026, { KEY_RESERVED } },
125
Pali Rohár6a4d2602016-06-15 21:49:11 +0200126 /* Dell Instant Launch key */
127 { KE_KEY, 0xe029, { KEY_PROG4 } },
128
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700129 { KE_IGNORE, 0xe02e, { KEY_VOLUMEDOWN } },
130 { KE_IGNORE, 0xe030, { KEY_VOLUMEUP } },
131 { KE_IGNORE, 0xe033, { KEY_KBDILLUMUP } },
132 { KE_IGNORE, 0xe034, { KEY_KBDILLUMDOWN } },
133 { KE_IGNORE, 0xe03a, { KEY_CAPSLOCK } },
Pali Rohár3de59bc2016-06-15 21:49:10 +0200134
135 /*
136 * This entry is very suspicious!
137 * Originally Matthew Garrett created this dell-wmi driver specially for
138 * "button with a picture of a battery" which has event code 0xe045.
139 * Later Mario Limonciello from Dell told us that event code 0xe045 is
140 * reported by Num Lock and should be ignored because key is send also
141 * by keyboard controller.
142 * So for now we will ignore this event to prevent potential double
143 * Num Lock key press.
144 */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700145 { KE_IGNORE, 0xe045, { KEY_NUMLOCK } },
Pali Rohár3de59bc2016-06-15 21:49:10 +0200146
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700147 { KE_IGNORE, 0xe046, { KEY_SCROLLLOCK } },
Seth Forsheef1566f02011-08-01 15:46:10 -0500148 { KE_IGNORE, 0xe0f7, { KEY_MUTE } },
149 { KE_IGNORE, 0xe0f8, { KEY_VOLUMEDOWN } },
150 { KE_IGNORE, 0xe0f9, { KEY_VOLUMEUP } },
Pali Rohár6a4d2602016-06-15 21:49:11 +0200151
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700152 { KE_END, 0 }
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000153};
154
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500155static bool dell_new_hk_type;
156
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700157struct dell_bios_keymap_entry {
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500158 u16 scancode;
159 u16 keycode;
160};
161
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700162struct dell_bios_hotkey_table {
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500163 struct dmi_header header;
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700164 struct dell_bios_keymap_entry keymap[];
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500165
166};
167
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800168struct dell_dmi_results {
169 int err;
170 struct key_entry *keymap;
171};
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500172
Andy Lutomirskicbc61f12015-11-30 17:02:01 -0800173/* Uninitialized entries here are KEY_RESERVED == 0. */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700174static const u16 bios_to_linux_keycode[256] __initconst = {
Andy Lutomirski394cb772015-11-13 21:49:31 -0800175 [0] = KEY_MEDIA,
176 [1] = KEY_NEXTSONG,
177 [2] = KEY_PLAYPAUSE,
178 [3] = KEY_PREVIOUSSONG,
179 [4] = KEY_STOPCD,
180 [5] = KEY_UNKNOWN,
181 [6] = KEY_UNKNOWN,
182 [7] = KEY_UNKNOWN,
183 [8] = KEY_WWW,
184 [9] = KEY_UNKNOWN,
185 [10] = KEY_VOLUMEDOWN,
186 [11] = KEY_MUTE,
187 [12] = KEY_VOLUMEUP,
188 [13] = KEY_UNKNOWN,
189 [14] = KEY_BATTERY,
190 [15] = KEY_EJECTCD,
191 [16] = KEY_UNKNOWN,
192 [17] = KEY_SLEEP,
193 [18] = KEY_PROG1,
194 [19] = KEY_BRIGHTNESSDOWN,
195 [20] = KEY_BRIGHTNESSUP,
196 [21] = KEY_UNKNOWN,
197 [22] = KEY_KBDILLUMTOGGLE,
198 [23] = KEY_UNKNOWN,
199 [24] = KEY_SWITCHVIDEOMODE,
200 [25] = KEY_UNKNOWN,
201 [26] = KEY_UNKNOWN,
202 [27] = KEY_SWITCHVIDEOMODE,
203 [28] = KEY_UNKNOWN,
204 [29] = KEY_UNKNOWN,
205 [30] = KEY_PROG2,
206 [31] = KEY_UNKNOWN,
207 [32] = KEY_UNKNOWN,
208 [33] = KEY_UNKNOWN,
209 [34] = KEY_UNKNOWN,
210 [35] = KEY_UNKNOWN,
211 [36] = KEY_UNKNOWN,
212 [37] = KEY_UNKNOWN,
213 [38] = KEY_MICMUTE,
214 [255] = KEY_PROG3,
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500215};
216
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800217/*
218 * These are applied if the 0xB2 DMI hotkey table is present and doesn't
219 * override them.
220 */
221static const struct key_entry dell_wmi_extra_keymap[] __initconst = {
222 /* Fn-lock */
223 { KE_IGNORE, 0x151, { KEY_RESERVED } },
224
225 /* Change keyboard illumination */
226 { KE_IGNORE, 0x152, { KEY_KBDILLUMTOGGLE } },
227
228 /*
229 * Radio disable (notify only -- there is no model for which the
230 * WMI event is supposed to trigger an action).
231 */
232 { KE_IGNORE, 0x153, { KEY_RFKILL } },
233
234 /* RGB keyboard backlight control */
235 { KE_IGNORE, 0x154, { KEY_RESERVED } },
236
237 /* Stealth mode toggle */
238 { KE_IGNORE, 0x155, { KEY_RESERVED } },
239};
240
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000241static struct input_dev *dell_wmi_input_dev;
242
Pali Rohár83fc44c2014-11-11 20:21:22 +0100243static void dell_wmi_process_key(int reported_key)
244{
245 const struct key_entry *key;
246
247 key = sparse_keymap_entry_from_scancode(dell_wmi_input_dev,
248 reported_key);
249 if (!key) {
Andy Lutomirskicbc61f12015-11-30 17:02:01 -0800250 pr_info("Unknown key with scancode 0x%x pressed\n",
251 reported_key);
Pali Rohár83fc44c2014-11-11 20:21:22 +0100252 return;
253 }
254
255 pr_debug("Key %x pressed\n", reported_key);
256
257 /* Don't report brightness notifications that will also come via ACPI */
258 if ((key->keycode == KEY_BRIGHTNESSUP ||
Hans de Goede61679c72015-12-22 19:09:49 +0100259 key->keycode == KEY_BRIGHTNESSDOWN) &&
260 acpi_video_handles_brightness_key_presses())
Pali Rohár83fc44c2014-11-11 20:21:22 +0100261 return;
262
Michał Kępień13f50592016-03-04 14:09:09 +0100263 if (reported_key == 0xe025 && !wmi_requires_smbios_request)
264 return;
265
Pali Rohár83fc44c2014-11-11 20:21:22 +0100266 sparse_keymap_report_entry(dell_wmi_input_dev, key, 1, true);
267}
268
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000269static void dell_wmi_notify(u32 value, void *context)
270{
271 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000272 union acpi_object *obj;
Len Brownfda11e62009-12-26 23:02:24 -0500273 acpi_status status;
Pali Rohár83fc44c2014-11-11 20:21:22 +0100274 acpi_size buffer_size;
275 u16 *buffer_entry, *buffer_end;
276 int len, i;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000277
Len Brownfda11e62009-12-26 23:02:24 -0500278 status = wmi_get_event_data(value, &response);
279 if (status != AE_OK) {
Pali Rohár83fc44c2014-11-11 20:21:22 +0100280 pr_warn("bad event status 0x%x\n", status);
Len Brownfda11e62009-12-26 23:02:24 -0500281 return;
282 }
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000283
284 obj = (union acpi_object *)response.pointer;
Pali Rohár83fc44c2014-11-11 20:21:22 +0100285 if (!obj) {
286 pr_warn("no response\n");
287 return;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000288 }
Pali Rohár83fc44c2014-11-11 20:21:22 +0100289
290 if (obj->type != ACPI_TYPE_BUFFER) {
291 pr_warn("bad response type %x\n", obj->type);
292 kfree(obj);
293 return;
294 }
295
296 pr_debug("Received WMI event (%*ph)\n",
297 obj->buffer.length, obj->buffer.pointer);
298
299 buffer_entry = (u16 *)obj->buffer.pointer;
300 buffer_size = obj->buffer.length/2;
301
302 if (!dell_new_hk_type) {
303 if (buffer_size >= 3 && buffer_entry[1] == 0x0)
304 dell_wmi_process_key(buffer_entry[2]);
305 else if (buffer_size >= 2)
306 dell_wmi_process_key(buffer_entry[1]);
307 else
308 pr_info("Received unknown WMI event\n");
309 kfree(obj);
310 return;
311 }
312
313 buffer_end = buffer_entry + buffer_size;
314
Pali Rohár481fe5b2016-01-04 22:26:36 +0100315 /*
316 * BIOS/ACPI on devices with WMI interface version 0 does not clear
317 * buffer before filling it. So next time when BIOS/ACPI send WMI event
318 * which is smaller as previous then it contains garbage in buffer from
319 * previous event.
320 *
321 * BIOS/ACPI on devices with WMI interface version 1 clears buffer and
322 * sometimes send more events in buffer at one call.
323 *
324 * So to prevent reading garbage from buffer we will process only first
325 * one event on devices with WMI interface version 0.
326 */
327 if (dell_wmi_interface_version == 0 && buffer_entry < buffer_end)
328 if (buffer_end > buffer_entry + buffer_entry[0] + 1)
329 buffer_end = buffer_entry + buffer_entry[0] + 1;
330
Pali Rohár83fc44c2014-11-11 20:21:22 +0100331 while (buffer_entry < buffer_end) {
332
333 len = buffer_entry[0];
334 if (len == 0)
335 break;
336
337 len++;
338
339 if (buffer_entry + len > buffer_end) {
340 pr_warn("Invalid length of WMI event\n");
341 break;
342 }
343
344 pr_debug("Process buffer (%*ph)\n", len*2, buffer_entry);
345
346 switch (buffer_entry[1]) {
347 case 0x00:
348 for (i = 2; i < len; ++i) {
349 switch (buffer_entry[i]) {
350 case 0xe043:
351 /* NIC Link is Up */
352 pr_debug("NIC Link is Up\n");
353 break;
354 case 0xe044:
355 /* NIC Link is Down */
356 pr_debug("NIC Link is Down\n");
357 break;
358 case 0xe045:
359 /* Unknown event but defined in DSDT */
360 default:
361 /* Unknown event */
362 pr_info("Unknown WMI event type 0x00: "
363 "0x%x\n", (int)buffer_entry[i]);
364 break;
365 }
366 }
367 break;
368 case 0x10:
369 /* Keys pressed */
370 for (i = 2; i < len; ++i)
371 dell_wmi_process_key(buffer_entry[i]);
372 break;
373 case 0x11:
374 for (i = 2; i < len; ++i) {
375 switch (buffer_entry[i]) {
376 case 0xfff0:
377 /* Battery unplugged */
378 pr_debug("Battery unplugged\n");
379 break;
380 case 0xfff1:
381 /* Battery inserted */
382 pr_debug("Battery inserted\n");
383 break;
384 case 0x01e1:
385 case 0x02ea:
386 case 0x02eb:
387 case 0x02ec:
388 case 0x02f6:
389 /* Keyboard backlight level changed */
390 pr_debug("Keyboard backlight level "
391 "changed\n");
392 break;
393 default:
394 /* Unknown event */
395 pr_info("Unknown WMI event type 0x11: "
396 "0x%x\n", (int)buffer_entry[i]);
397 break;
398 }
399 }
400 break;
401 default:
402 /* Unknown event */
403 pr_info("Unknown WMI event type 0x%x\n",
404 (int)buffer_entry[1]);
405 break;
406 }
407
408 buffer_entry += len;
409
410 }
411
Anisse Astier3e9b9882009-12-04 10:10:09 +0100412 kfree(obj);
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000413}
414
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800415static bool have_scancode(u32 scancode, const struct key_entry *keymap, int len)
416{
417 int i;
418
419 for (i = 0; i < len; i++)
420 if (keymap[i].code == scancode)
421 return true;
422
423 return false;
424}
425
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800426static void __init handle_dmi_entry(const struct dmi_header *dm,
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800427
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800428 void *opaque)
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800429
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500430{
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800431 struct dell_dmi_results *results = opaque;
432 struct dell_bios_hotkey_table *table;
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800433 int hotkey_num, i, pos = 0;
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700434 struct key_entry *keymap;
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800435 int num_bios_keys;
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800436
437 if (results->err || results->keymap)
438 return; /* We already found the hotkey table. */
439
Andy Lutomirskib13de702016-02-15 08:32:35 -0800440 if (dm->type != 0xb2)
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800441 return;
442
443 table = container_of(dm, struct dell_bios_hotkey_table, header);
444
Andy Lutomirskib13de702016-02-15 08:32:35 -0800445 hotkey_num = (table->header.length -
446 sizeof(struct dell_bios_hotkey_table)) /
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800447 sizeof(struct dell_bios_keymap_entry);
Andy Lutomirskib13de702016-02-15 08:32:35 -0800448 if (hotkey_num < 1) {
449 /*
450 * Historically, dell-wmi would ignore a DMI entry of
451 * fewer than 7 bytes. Sizes between 4 and 8 bytes are
452 * nonsensical (both the header and all entries are 4
453 * bytes), so we approximate the old behavior by
454 * ignoring tables with fewer than one entry.
455 */
456 return;
457 }
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500458
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800459 keymap = kcalloc(hotkey_num + ARRAY_SIZE(dell_wmi_extra_keymap) + 1,
460 sizeof(struct key_entry), GFP_KERNEL);
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800461 if (!keymap) {
462 results->err = -ENOMEM;
463 return;
464 }
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500465
466 for (i = 0; i < hotkey_num; i++) {
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700467 const struct dell_bios_keymap_entry *bios_entry =
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800468 &table->keymap[i];
Andy Lutomirskicbc61f12015-11-30 17:02:01 -0800469
470 /* Uninitialized entries are 0 aka KEY_RESERVED. */
471 u16 keycode = (bios_entry->keycode <
472 ARRAY_SIZE(bios_to_linux_keycode)) ?
473 bios_to_linux_keycode[bios_entry->keycode] :
474 KEY_RESERVED;
475
476 /*
477 * Log if we find an entry in the DMI table that we don't
478 * understand. If this happens, we should figure out what
479 * the entry means and add it to bios_to_linux_keycode.
480 */
481 if (keycode == KEY_RESERVED) {
482 pr_info("firmware scancode 0x%x maps to unrecognized keycode 0x%x\n",
483 bios_entry->scancode, bios_entry->keycode);
484 continue;
485 }
Gabriele Mazzotta8cb8e632014-12-04 00:16:23 +0100486
487 if (keycode == KEY_KBDILLUMTOGGLE)
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800488 keymap[pos].type = KE_IGNORE;
Gabriele Mazzotta8cb8e632014-12-04 00:16:23 +0100489 else
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800490 keymap[pos].type = KE_KEY;
491 keymap[pos].code = bios_entry->scancode;
492 keymap[pos].keycode = keycode;
493
494 pos++;
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500495 }
496
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800497 num_bios_keys = pos;
498
499 for (i = 0; i < ARRAY_SIZE(dell_wmi_extra_keymap); i++) {
500 const struct key_entry *entry = &dell_wmi_extra_keymap[i];
501
502 /*
503 * Check if we've already found this scancode. This takes
504 * quadratic time, but it doesn't matter unless the list
505 * of extra keys gets very long.
506 */
507 if (!have_scancode(entry->code, keymap, num_bios_keys)) {
508 keymap[pos] = *entry;
509 pos++;
510 }
511 }
512
513 keymap[pos].type = KE_END;
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500514
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800515 results->keymap = keymap;
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500516}
517
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000518static int __init dell_wmi_input_setup(void)
519{
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800520 struct dell_dmi_results dmi_results = {};
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000521 int err;
522
523 dell_wmi_input_dev = input_allocate_device();
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000524 if (!dell_wmi_input_dev)
525 return -ENOMEM;
526
527 dell_wmi_input_dev->name = "Dell WMI hotkeys";
528 dell_wmi_input_dev->phys = "wmi/input0";
529 dell_wmi_input_dev->id.bustype = BUS_HOST;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000530
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800531 if (dmi_walk(handle_dmi_entry, &dmi_results)) {
532 /*
533 * Historically, dell-wmi ignored dmi_walk errors. A failure
534 * is certainly surprising, but it probably just indicates
535 * a very old laptop.
536 */
537 pr_warn("no DMI; using the old-style hotkey interface\n");
538 }
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700539
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800540 if (dmi_results.err) {
541 err = dmi_results.err;
542 goto err_free_dev;
543 }
544
545 if (dmi_results.keymap) {
546 dell_new_hk_type = true;
547
548 err = sparse_keymap_setup(dell_wmi_input_dev,
549 dmi_results.keymap, NULL);
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700550
551 /*
552 * Sparse keymap library makes a copy of keymap so we
553 * don't need the original one that was allocated.
554 */
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800555 kfree(dmi_results.keymap);
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700556 } else {
557 err = sparse_keymap_setup(dell_wmi_input_dev,
558 dell_wmi_legacy_keymap, NULL);
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000559 }
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700560 if (err)
561 goto err_free_dev;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000562
563 err = input_register_device(dell_wmi_input_dev);
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700564 if (err)
565 goto err_free_keymap;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000566
567 return 0;
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700568
569 err_free_keymap:
570 sparse_keymap_free(dell_wmi_input_dev);
571 err_free_dev:
572 input_free_device(dell_wmi_input_dev);
573 return err;
574}
575
576static void dell_wmi_input_destroy(void)
577{
578 sparse_keymap_free(dell_wmi_input_dev);
579 input_unregister_device(dell_wmi_input_dev);
580}
581
Pali Rohár595773a42016-01-04 22:26:35 +0100582/*
583 * Descriptor buffer is 128 byte long and contains:
584 *
585 * Name Offset Length Value
586 * Vendor Signature 0 4 "DELL"
587 * Object Signature 4 4 " WMI"
588 * WMI Interface Version 8 4 <version>
589 * WMI buffer length 12 4 4096
590 */
591static int __init dell_wmi_check_descriptor_buffer(void)
592{
593 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
594 union acpi_object *obj;
595 acpi_status status;
596 u32 *buffer;
597
598 status = wmi_query_block(DELL_DESCRIPTOR_GUID, 0, &out);
599 if (ACPI_FAILURE(status)) {
600 pr_err("Cannot read Dell descriptor buffer - %d\n", status);
601 return status;
602 }
603
604 obj = (union acpi_object *)out.pointer;
605 if (!obj) {
606 pr_err("Dell descriptor buffer is empty\n");
607 return -EINVAL;
608 }
609
610 if (obj->type != ACPI_TYPE_BUFFER) {
611 pr_err("Cannot read Dell descriptor buffer\n");
612 kfree(obj);
613 return -EINVAL;
614 }
615
616 if (obj->buffer.length != 128) {
617 pr_err("Dell descriptor buffer has invalid length (%d)\n",
618 obj->buffer.length);
619 if (obj->buffer.length < 16) {
620 kfree(obj);
621 return -EINVAL;
622 }
623 }
624
625 buffer = (u32 *)obj->buffer.pointer;
626
627 if (buffer[0] != 0x4C4C4544 && buffer[1] != 0x494D5720)
628 pr_warn("Dell descriptor buffer has invalid signature (%*ph)\n",
629 8, buffer);
630
631 if (buffer[2] != 0 && buffer[2] != 1)
632 pr_warn("Dell descriptor buffer has unknown version (%d)\n",
633 buffer[2]);
634
635 if (buffer[3] != 4096)
636 pr_warn("Dell descriptor buffer has invalid buffer length (%d)\n",
637 buffer[3]);
638
639 dell_wmi_interface_version = buffer[2];
640
641 pr_info("Detected Dell WMI interface version %u\n",
642 dell_wmi_interface_version);
643
644 kfree(obj);
645 return 0;
646}
647
Michał Kępieńe09c4d52016-03-04 14:09:08 +0100648/*
649 * According to Dell SMBIOS documentation:
650 *
651 * 17 3 Application Program Registration
652 *
653 * cbArg1 Application ID 1 = 0x00010000
654 * cbArg2 Application ID 2
655 * QUICKSET/DCP = 0x51534554 "QSET"
656 * ALS Driver = 0x416c7353 "AlsS"
657 * Latitude ON = 0x4c6f6e52 "LonR"
658 * cbArg3 Application version or revision number
659 * cbArg4 0 = Unregister application
660 * 1 = Register application
661 * cbRes1 Standard return codes (0, -1, -2)
662 */
663
664static int dell_wmi_events_set_enabled(bool enable)
665{
666 struct calling_interface_buffer *buffer;
667 int ret;
668
669 buffer = dell_smbios_get_buffer();
670 buffer->input[0] = 0x10000;
671 buffer->input[1] = 0x51534554;
672 buffer->input[3] = enable;
673 dell_smbios_send_request(17, 3);
674 ret = buffer->output[0];
675 dell_smbios_release_buffer();
676
677 return dell_smbios_error(ret);
678}
679
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000680static int __init dell_wmi_init(void)
681{
682 int err;
Len Brownabb631b2009-12-26 21:51:38 -0500683 acpi_status status;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000684
Pali Rohár595773a42016-01-04 22:26:35 +0100685 if (!wmi_has_guid(DELL_EVENT_GUID) ||
686 !wmi_has_guid(DELL_DESCRIPTOR_GUID)) {
687 pr_warn("Dell WMI GUID were not found\n");
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800688 return -ENODEV;
689 }
690
Pali Rohár595773a42016-01-04 22:26:35 +0100691 err = dell_wmi_check_descriptor_buffer();
692 if (err)
693 return err;
694
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800695 err = dell_wmi_input_setup();
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700696 if (err)
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800697 return err;
698
Len Brownabb631b2009-12-26 21:51:38 -0500699 status = wmi_install_notify_handler(DELL_EVENT_GUID,
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800700 dell_wmi_notify, NULL);
Len Brownabb631b2009-12-26 21:51:38 -0500701 if (ACPI_FAILURE(status)) {
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700702 dell_wmi_input_destroy();
Joe Percheseb889522011-03-29 15:21:37 -0700703 pr_err("Unable to register notify handler - %d\n", status);
Len Brownabb631b2009-12-26 21:51:38 -0500704 return -ENODEV;
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800705 }
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000706
Michał Kępieńe09c4d52016-03-04 14:09:08 +0100707 dmi_check_system(dell_wmi_smbios_list);
708
709 if (wmi_requires_smbios_request) {
710 err = dell_wmi_events_set_enabled(true);
711 if (err) {
712 pr_err("Failed to enable WMI events\n");
713 wmi_remove_notify_handler(DELL_EVENT_GUID);
714 dell_wmi_input_destroy();
715 return err;
716 }
717 }
718
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000719 return 0;
720}
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700721module_init(dell_wmi_init);
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000722
723static void __exit dell_wmi_exit(void)
724{
Michał Kępieńe09c4d52016-03-04 14:09:08 +0100725 if (wmi_requires_smbios_request)
726 dell_wmi_events_set_enabled(false);
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800727 wmi_remove_notify_handler(DELL_EVENT_GUID);
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700728 dell_wmi_input_destroy();
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000729}
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000730module_exit(dell_wmi_exit);