blob: 3ea959ecd635828c23cedc6c2d1c5ee86a28e173 [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,
65 .ident = "Dell Vostro V131",
66 .matches = {
67 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
68 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
69 },
70 },
71 { }
72};
73
Mario Limonciello5cab0092009-06-10 19:40:47 +000074/*
75 * Certain keys are flagged as KE_IGNORE. All of these are either
76 * notifications (rather than requests for change) or are also sent
77 * via the keyboard controller so should not be sent again.
78 */
Matthew Garrett0b3f6102009-01-09 20:17:11 +000079
Dmitry Torokhov890a7c82010-08-04 22:30:08 -070080static const struct key_entry dell_wmi_legacy_keymap[] __initconst = {
Seth Forsheef1566f02011-08-01 15:46:10 -050081 { KE_IGNORE, 0x003a, { KEY_CAPSLOCK } },
82
Dmitry Torokhov890a7c82010-08-04 22:30:08 -070083 { KE_KEY, 0xe045, { KEY_PROG1 } },
84 { KE_KEY, 0xe009, { KEY_EJECTCD } },
Mario Limonciello5cab0092009-06-10 19:40:47 +000085
86 /* These also contain the brightness level at offset 6 */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -070087 { KE_KEY, 0xe006, { KEY_BRIGHTNESSUP } },
88 { KE_KEY, 0xe005, { KEY_BRIGHTNESSDOWN } },
Mario Limonciello5cab0092009-06-10 19:40:47 +000089
90 /* Battery health status button */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -070091 { KE_KEY, 0xe007, { KEY_BATTERY } },
Mario Limonciello5cab0092009-06-10 19:40:47 +000092
Gabriele Mazzotta8f8d75e2014-12-04 00:16:21 +010093 /* Radio devices state change */
94 { KE_IGNORE, 0xe008, { KEY_RFKILL } },
Mario Limonciello5cab0092009-06-10 19:40:47 +000095
96 /* The next device is at offset 6, the active devices are at
97 offset 8 and the attached devices at offset 10 */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -070098 { KE_KEY, 0xe00b, { KEY_SWITCHVIDEOMODE } },
Mario Limonciello5cab0092009-06-10 19:40:47 +000099
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700100 { KE_IGNORE, 0xe00c, { KEY_KBDILLUMTOGGLE } },
Mario Limonciello5cab0092009-06-10 19:40:47 +0000101
102 /* BIOS error detected */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700103 { KE_IGNORE, 0xe00d, { KEY_RESERVED } },
Mario Limonciello5cab0092009-06-10 19:40:47 +0000104
105 /* Wifi Catcher */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700106 { KE_KEY, 0xe011, {KEY_PROG2 } },
Mario Limonciello5cab0092009-06-10 19:40:47 +0000107
108 /* Ambient light sensor toggle */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700109 { KE_IGNORE, 0xe013, { KEY_RESERVED } },
Mario Limonciello5cab0092009-06-10 19:40:47 +0000110
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700111 { KE_IGNORE, 0xe020, { KEY_MUTE } },
Seth Forsheef1566f02011-08-01 15:46:10 -0500112
113 /* Shortcut and audio panel keys */
Michał Kępień13f50592016-03-04 14:09:09 +0100114 { KE_KEY, 0xe025, { KEY_PROG4 } },
Seth Forsheef1566f02011-08-01 15:46:10 -0500115 { KE_IGNORE, 0xe026, { KEY_RESERVED } },
116
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700117 { KE_IGNORE, 0xe02e, { KEY_VOLUMEDOWN } },
118 { KE_IGNORE, 0xe030, { KEY_VOLUMEUP } },
119 { KE_IGNORE, 0xe033, { KEY_KBDILLUMUP } },
120 { KE_IGNORE, 0xe034, { KEY_KBDILLUMDOWN } },
121 { KE_IGNORE, 0xe03a, { KEY_CAPSLOCK } },
122 { KE_IGNORE, 0xe045, { KEY_NUMLOCK } },
123 { KE_IGNORE, 0xe046, { KEY_SCROLLLOCK } },
Seth Forsheef1566f02011-08-01 15:46:10 -0500124 { KE_IGNORE, 0xe0f7, { KEY_MUTE } },
125 { KE_IGNORE, 0xe0f8, { KEY_VOLUMEDOWN } },
126 { KE_IGNORE, 0xe0f9, { KEY_VOLUMEUP } },
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700127 { KE_END, 0 }
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000128};
129
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500130static bool dell_new_hk_type;
131
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700132struct dell_bios_keymap_entry {
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500133 u16 scancode;
134 u16 keycode;
135};
136
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700137struct dell_bios_hotkey_table {
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500138 struct dmi_header header;
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700139 struct dell_bios_keymap_entry keymap[];
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500140
141};
142
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800143struct dell_dmi_results {
144 int err;
145 struct key_entry *keymap;
146};
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500147
Andy Lutomirskicbc61f12015-11-30 17:02:01 -0800148/* Uninitialized entries here are KEY_RESERVED == 0. */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700149static const u16 bios_to_linux_keycode[256] __initconst = {
Andy Lutomirski394cb772015-11-13 21:49:31 -0800150 [0] = KEY_MEDIA,
151 [1] = KEY_NEXTSONG,
152 [2] = KEY_PLAYPAUSE,
153 [3] = KEY_PREVIOUSSONG,
154 [4] = KEY_STOPCD,
155 [5] = KEY_UNKNOWN,
156 [6] = KEY_UNKNOWN,
157 [7] = KEY_UNKNOWN,
158 [8] = KEY_WWW,
159 [9] = KEY_UNKNOWN,
160 [10] = KEY_VOLUMEDOWN,
161 [11] = KEY_MUTE,
162 [12] = KEY_VOLUMEUP,
163 [13] = KEY_UNKNOWN,
164 [14] = KEY_BATTERY,
165 [15] = KEY_EJECTCD,
166 [16] = KEY_UNKNOWN,
167 [17] = KEY_SLEEP,
168 [18] = KEY_PROG1,
169 [19] = KEY_BRIGHTNESSDOWN,
170 [20] = KEY_BRIGHTNESSUP,
171 [21] = KEY_UNKNOWN,
172 [22] = KEY_KBDILLUMTOGGLE,
173 [23] = KEY_UNKNOWN,
174 [24] = KEY_SWITCHVIDEOMODE,
175 [25] = KEY_UNKNOWN,
176 [26] = KEY_UNKNOWN,
177 [27] = KEY_SWITCHVIDEOMODE,
178 [28] = KEY_UNKNOWN,
179 [29] = KEY_UNKNOWN,
180 [30] = KEY_PROG2,
181 [31] = KEY_UNKNOWN,
182 [32] = KEY_UNKNOWN,
183 [33] = KEY_UNKNOWN,
184 [34] = KEY_UNKNOWN,
185 [35] = KEY_UNKNOWN,
186 [36] = KEY_UNKNOWN,
187 [37] = KEY_UNKNOWN,
188 [38] = KEY_MICMUTE,
189 [255] = KEY_PROG3,
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500190};
191
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800192/*
193 * These are applied if the 0xB2 DMI hotkey table is present and doesn't
194 * override them.
195 */
196static const struct key_entry dell_wmi_extra_keymap[] __initconst = {
197 /* Fn-lock */
198 { KE_IGNORE, 0x151, { KEY_RESERVED } },
199
200 /* Change keyboard illumination */
201 { KE_IGNORE, 0x152, { KEY_KBDILLUMTOGGLE } },
202
203 /*
204 * Radio disable (notify only -- there is no model for which the
205 * WMI event is supposed to trigger an action).
206 */
207 { KE_IGNORE, 0x153, { KEY_RFKILL } },
208
209 /* RGB keyboard backlight control */
210 { KE_IGNORE, 0x154, { KEY_RESERVED } },
211
212 /* Stealth mode toggle */
213 { KE_IGNORE, 0x155, { KEY_RESERVED } },
214};
215
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000216static struct input_dev *dell_wmi_input_dev;
217
Pali Rohár83fc44c2014-11-11 20:21:22 +0100218static void dell_wmi_process_key(int reported_key)
219{
220 const struct key_entry *key;
221
222 key = sparse_keymap_entry_from_scancode(dell_wmi_input_dev,
223 reported_key);
224 if (!key) {
Andy Lutomirskicbc61f12015-11-30 17:02:01 -0800225 pr_info("Unknown key with scancode 0x%x pressed\n",
226 reported_key);
Pali Rohár83fc44c2014-11-11 20:21:22 +0100227 return;
228 }
229
230 pr_debug("Key %x pressed\n", reported_key);
231
232 /* Don't report brightness notifications that will also come via ACPI */
233 if ((key->keycode == KEY_BRIGHTNESSUP ||
Hans de Goede61679c72015-12-22 19:09:49 +0100234 key->keycode == KEY_BRIGHTNESSDOWN) &&
235 acpi_video_handles_brightness_key_presses())
Pali Rohár83fc44c2014-11-11 20:21:22 +0100236 return;
237
Michał Kępień13f50592016-03-04 14:09:09 +0100238 if (reported_key == 0xe025 && !wmi_requires_smbios_request)
239 return;
240
Pali Rohár83fc44c2014-11-11 20:21:22 +0100241 sparse_keymap_report_entry(dell_wmi_input_dev, key, 1, true);
242}
243
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000244static void dell_wmi_notify(u32 value, void *context)
245{
246 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000247 union acpi_object *obj;
Len Brownfda11e62009-12-26 23:02:24 -0500248 acpi_status status;
Pali Rohár83fc44c2014-11-11 20:21:22 +0100249 acpi_size buffer_size;
250 u16 *buffer_entry, *buffer_end;
251 int len, i;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000252
Len Brownfda11e62009-12-26 23:02:24 -0500253 status = wmi_get_event_data(value, &response);
254 if (status != AE_OK) {
Pali Rohár83fc44c2014-11-11 20:21:22 +0100255 pr_warn("bad event status 0x%x\n", status);
Len Brownfda11e62009-12-26 23:02:24 -0500256 return;
257 }
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000258
259 obj = (union acpi_object *)response.pointer;
Pali Rohár83fc44c2014-11-11 20:21:22 +0100260 if (!obj) {
261 pr_warn("no response\n");
262 return;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000263 }
Pali Rohár83fc44c2014-11-11 20:21:22 +0100264
265 if (obj->type != ACPI_TYPE_BUFFER) {
266 pr_warn("bad response type %x\n", obj->type);
267 kfree(obj);
268 return;
269 }
270
271 pr_debug("Received WMI event (%*ph)\n",
272 obj->buffer.length, obj->buffer.pointer);
273
274 buffer_entry = (u16 *)obj->buffer.pointer;
275 buffer_size = obj->buffer.length/2;
276
277 if (!dell_new_hk_type) {
278 if (buffer_size >= 3 && buffer_entry[1] == 0x0)
279 dell_wmi_process_key(buffer_entry[2]);
280 else if (buffer_size >= 2)
281 dell_wmi_process_key(buffer_entry[1]);
282 else
283 pr_info("Received unknown WMI event\n");
284 kfree(obj);
285 return;
286 }
287
288 buffer_end = buffer_entry + buffer_size;
289
Pali Rohár481fe5b2016-01-04 22:26:36 +0100290 /*
291 * BIOS/ACPI on devices with WMI interface version 0 does not clear
292 * buffer before filling it. So next time when BIOS/ACPI send WMI event
293 * which is smaller as previous then it contains garbage in buffer from
294 * previous event.
295 *
296 * BIOS/ACPI on devices with WMI interface version 1 clears buffer and
297 * sometimes send more events in buffer at one call.
298 *
299 * So to prevent reading garbage from buffer we will process only first
300 * one event on devices with WMI interface version 0.
301 */
302 if (dell_wmi_interface_version == 0 && buffer_entry < buffer_end)
303 if (buffer_end > buffer_entry + buffer_entry[0] + 1)
304 buffer_end = buffer_entry + buffer_entry[0] + 1;
305
Pali Rohár83fc44c2014-11-11 20:21:22 +0100306 while (buffer_entry < buffer_end) {
307
308 len = buffer_entry[0];
309 if (len == 0)
310 break;
311
312 len++;
313
314 if (buffer_entry + len > buffer_end) {
315 pr_warn("Invalid length of WMI event\n");
316 break;
317 }
318
319 pr_debug("Process buffer (%*ph)\n", len*2, buffer_entry);
320
321 switch (buffer_entry[1]) {
322 case 0x00:
323 for (i = 2; i < len; ++i) {
324 switch (buffer_entry[i]) {
325 case 0xe043:
326 /* NIC Link is Up */
327 pr_debug("NIC Link is Up\n");
328 break;
329 case 0xe044:
330 /* NIC Link is Down */
331 pr_debug("NIC Link is Down\n");
332 break;
333 case 0xe045:
334 /* Unknown event but defined in DSDT */
335 default:
336 /* Unknown event */
337 pr_info("Unknown WMI event type 0x00: "
338 "0x%x\n", (int)buffer_entry[i]);
339 break;
340 }
341 }
342 break;
343 case 0x10:
344 /* Keys pressed */
345 for (i = 2; i < len; ++i)
346 dell_wmi_process_key(buffer_entry[i]);
347 break;
348 case 0x11:
349 for (i = 2; i < len; ++i) {
350 switch (buffer_entry[i]) {
351 case 0xfff0:
352 /* Battery unplugged */
353 pr_debug("Battery unplugged\n");
354 break;
355 case 0xfff1:
356 /* Battery inserted */
357 pr_debug("Battery inserted\n");
358 break;
359 case 0x01e1:
360 case 0x02ea:
361 case 0x02eb:
362 case 0x02ec:
363 case 0x02f6:
364 /* Keyboard backlight level changed */
365 pr_debug("Keyboard backlight level "
366 "changed\n");
367 break;
368 default:
369 /* Unknown event */
370 pr_info("Unknown WMI event type 0x11: "
371 "0x%x\n", (int)buffer_entry[i]);
372 break;
373 }
374 }
375 break;
376 default:
377 /* Unknown event */
378 pr_info("Unknown WMI event type 0x%x\n",
379 (int)buffer_entry[1]);
380 break;
381 }
382
383 buffer_entry += len;
384
385 }
386
Anisse Astier3e9b9882009-12-04 10:10:09 +0100387 kfree(obj);
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000388}
389
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800390static bool have_scancode(u32 scancode, const struct key_entry *keymap, int len)
391{
392 int i;
393
394 for (i = 0; i < len; i++)
395 if (keymap[i].code == scancode)
396 return true;
397
398 return false;
399}
400
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800401static void __init handle_dmi_entry(const struct dmi_header *dm,
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800402
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800403 void *opaque)
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800404
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500405{
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800406 struct dell_dmi_results *results = opaque;
407 struct dell_bios_hotkey_table *table;
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800408 int hotkey_num, i, pos = 0;
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700409 struct key_entry *keymap;
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800410 int num_bios_keys;
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800411
412 if (results->err || results->keymap)
413 return; /* We already found the hotkey table. */
414
Andy Lutomirskib13de702016-02-15 08:32:35 -0800415 if (dm->type != 0xb2)
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800416 return;
417
418 table = container_of(dm, struct dell_bios_hotkey_table, header);
419
Andy Lutomirskib13de702016-02-15 08:32:35 -0800420 hotkey_num = (table->header.length -
421 sizeof(struct dell_bios_hotkey_table)) /
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800422 sizeof(struct dell_bios_keymap_entry);
Andy Lutomirskib13de702016-02-15 08:32:35 -0800423 if (hotkey_num < 1) {
424 /*
425 * Historically, dell-wmi would ignore a DMI entry of
426 * fewer than 7 bytes. Sizes between 4 and 8 bytes are
427 * nonsensical (both the header and all entries are 4
428 * bytes), so we approximate the old behavior by
429 * ignoring tables with fewer than one entry.
430 */
431 return;
432 }
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500433
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800434 keymap = kcalloc(hotkey_num + ARRAY_SIZE(dell_wmi_extra_keymap) + 1,
435 sizeof(struct key_entry), GFP_KERNEL);
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800436 if (!keymap) {
437 results->err = -ENOMEM;
438 return;
439 }
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500440
441 for (i = 0; i < hotkey_num; i++) {
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700442 const struct dell_bios_keymap_entry *bios_entry =
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800443 &table->keymap[i];
Andy Lutomirskicbc61f12015-11-30 17:02:01 -0800444
445 /* Uninitialized entries are 0 aka KEY_RESERVED. */
446 u16 keycode = (bios_entry->keycode <
447 ARRAY_SIZE(bios_to_linux_keycode)) ?
448 bios_to_linux_keycode[bios_entry->keycode] :
449 KEY_RESERVED;
450
451 /*
452 * Log if we find an entry in the DMI table that we don't
453 * understand. If this happens, we should figure out what
454 * the entry means and add it to bios_to_linux_keycode.
455 */
456 if (keycode == KEY_RESERVED) {
457 pr_info("firmware scancode 0x%x maps to unrecognized keycode 0x%x\n",
458 bios_entry->scancode, bios_entry->keycode);
459 continue;
460 }
Gabriele Mazzotta8cb8e632014-12-04 00:16:23 +0100461
462 if (keycode == KEY_KBDILLUMTOGGLE)
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800463 keymap[pos].type = KE_IGNORE;
Gabriele Mazzotta8cb8e632014-12-04 00:16:23 +0100464 else
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800465 keymap[pos].type = KE_KEY;
466 keymap[pos].code = bios_entry->scancode;
467 keymap[pos].keycode = keycode;
468
469 pos++;
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500470 }
471
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800472 num_bios_keys = pos;
473
474 for (i = 0; i < ARRAY_SIZE(dell_wmi_extra_keymap); i++) {
475 const struct key_entry *entry = &dell_wmi_extra_keymap[i];
476
477 /*
478 * Check if we've already found this scancode. This takes
479 * quadratic time, but it doesn't matter unless the list
480 * of extra keys gets very long.
481 */
482 if (!have_scancode(entry->code, keymap, num_bios_keys)) {
483 keymap[pos] = *entry;
484 pos++;
485 }
486 }
487
488 keymap[pos].type = KE_END;
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500489
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800490 results->keymap = keymap;
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500491}
492
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000493static int __init dell_wmi_input_setup(void)
494{
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800495 struct dell_dmi_results dmi_results = {};
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000496 int err;
497
498 dell_wmi_input_dev = input_allocate_device();
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000499 if (!dell_wmi_input_dev)
500 return -ENOMEM;
501
502 dell_wmi_input_dev->name = "Dell WMI hotkeys";
503 dell_wmi_input_dev->phys = "wmi/input0";
504 dell_wmi_input_dev->id.bustype = BUS_HOST;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000505
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800506 if (dmi_walk(handle_dmi_entry, &dmi_results)) {
507 /*
508 * Historically, dell-wmi ignored dmi_walk errors. A failure
509 * is certainly surprising, but it probably just indicates
510 * a very old laptop.
511 */
512 pr_warn("no DMI; using the old-style hotkey interface\n");
513 }
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700514
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800515 if (dmi_results.err) {
516 err = dmi_results.err;
517 goto err_free_dev;
518 }
519
520 if (dmi_results.keymap) {
521 dell_new_hk_type = true;
522
523 err = sparse_keymap_setup(dell_wmi_input_dev,
524 dmi_results.keymap, NULL);
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700525
526 /*
527 * Sparse keymap library makes a copy of keymap so we
528 * don't need the original one that was allocated.
529 */
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800530 kfree(dmi_results.keymap);
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700531 } else {
532 err = sparse_keymap_setup(dell_wmi_input_dev,
533 dell_wmi_legacy_keymap, NULL);
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000534 }
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700535 if (err)
536 goto err_free_dev;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000537
538 err = input_register_device(dell_wmi_input_dev);
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700539 if (err)
540 goto err_free_keymap;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000541
542 return 0;
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700543
544 err_free_keymap:
545 sparse_keymap_free(dell_wmi_input_dev);
546 err_free_dev:
547 input_free_device(dell_wmi_input_dev);
548 return err;
549}
550
551static void dell_wmi_input_destroy(void)
552{
553 sparse_keymap_free(dell_wmi_input_dev);
554 input_unregister_device(dell_wmi_input_dev);
555}
556
Pali Rohár595773a42016-01-04 22:26:35 +0100557/*
558 * Descriptor buffer is 128 byte long and contains:
559 *
560 * Name Offset Length Value
561 * Vendor Signature 0 4 "DELL"
562 * Object Signature 4 4 " WMI"
563 * WMI Interface Version 8 4 <version>
564 * WMI buffer length 12 4 4096
565 */
566static int __init dell_wmi_check_descriptor_buffer(void)
567{
568 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
569 union acpi_object *obj;
570 acpi_status status;
571 u32 *buffer;
572
573 status = wmi_query_block(DELL_DESCRIPTOR_GUID, 0, &out);
574 if (ACPI_FAILURE(status)) {
575 pr_err("Cannot read Dell descriptor buffer - %d\n", status);
576 return status;
577 }
578
579 obj = (union acpi_object *)out.pointer;
580 if (!obj) {
581 pr_err("Dell descriptor buffer is empty\n");
582 return -EINVAL;
583 }
584
585 if (obj->type != ACPI_TYPE_BUFFER) {
586 pr_err("Cannot read Dell descriptor buffer\n");
587 kfree(obj);
588 return -EINVAL;
589 }
590
591 if (obj->buffer.length != 128) {
592 pr_err("Dell descriptor buffer has invalid length (%d)\n",
593 obj->buffer.length);
594 if (obj->buffer.length < 16) {
595 kfree(obj);
596 return -EINVAL;
597 }
598 }
599
600 buffer = (u32 *)obj->buffer.pointer;
601
602 if (buffer[0] != 0x4C4C4544 && buffer[1] != 0x494D5720)
603 pr_warn("Dell descriptor buffer has invalid signature (%*ph)\n",
604 8, buffer);
605
606 if (buffer[2] != 0 && buffer[2] != 1)
607 pr_warn("Dell descriptor buffer has unknown version (%d)\n",
608 buffer[2]);
609
610 if (buffer[3] != 4096)
611 pr_warn("Dell descriptor buffer has invalid buffer length (%d)\n",
612 buffer[3]);
613
614 dell_wmi_interface_version = buffer[2];
615
616 pr_info("Detected Dell WMI interface version %u\n",
617 dell_wmi_interface_version);
618
619 kfree(obj);
620 return 0;
621}
622
Michał Kępieńe09c4d52016-03-04 14:09:08 +0100623/*
624 * According to Dell SMBIOS documentation:
625 *
626 * 17 3 Application Program Registration
627 *
628 * cbArg1 Application ID 1 = 0x00010000
629 * cbArg2 Application ID 2
630 * QUICKSET/DCP = 0x51534554 "QSET"
631 * ALS Driver = 0x416c7353 "AlsS"
632 * Latitude ON = 0x4c6f6e52 "LonR"
633 * cbArg3 Application version or revision number
634 * cbArg4 0 = Unregister application
635 * 1 = Register application
636 * cbRes1 Standard return codes (0, -1, -2)
637 */
638
639static int dell_wmi_events_set_enabled(bool enable)
640{
641 struct calling_interface_buffer *buffer;
642 int ret;
643
644 buffer = dell_smbios_get_buffer();
645 buffer->input[0] = 0x10000;
646 buffer->input[1] = 0x51534554;
647 buffer->input[3] = enable;
648 dell_smbios_send_request(17, 3);
649 ret = buffer->output[0];
650 dell_smbios_release_buffer();
651
652 return dell_smbios_error(ret);
653}
654
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000655static int __init dell_wmi_init(void)
656{
657 int err;
Len Brownabb631b2009-12-26 21:51:38 -0500658 acpi_status status;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000659
Pali Rohár595773a42016-01-04 22:26:35 +0100660 if (!wmi_has_guid(DELL_EVENT_GUID) ||
661 !wmi_has_guid(DELL_DESCRIPTOR_GUID)) {
662 pr_warn("Dell WMI GUID were not found\n");
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800663 return -ENODEV;
664 }
665
Pali Rohár595773a42016-01-04 22:26:35 +0100666 err = dell_wmi_check_descriptor_buffer();
667 if (err)
668 return err;
669
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800670 err = dell_wmi_input_setup();
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700671 if (err)
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800672 return err;
673
Len Brownabb631b2009-12-26 21:51:38 -0500674 status = wmi_install_notify_handler(DELL_EVENT_GUID,
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800675 dell_wmi_notify, NULL);
Len Brownabb631b2009-12-26 21:51:38 -0500676 if (ACPI_FAILURE(status)) {
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700677 dell_wmi_input_destroy();
Joe Percheseb889522011-03-29 15:21:37 -0700678 pr_err("Unable to register notify handler - %d\n", status);
Len Brownabb631b2009-12-26 21:51:38 -0500679 return -ENODEV;
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800680 }
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000681
Michał Kępieńe09c4d52016-03-04 14:09:08 +0100682 dmi_check_system(dell_wmi_smbios_list);
683
684 if (wmi_requires_smbios_request) {
685 err = dell_wmi_events_set_enabled(true);
686 if (err) {
687 pr_err("Failed to enable WMI events\n");
688 wmi_remove_notify_handler(DELL_EVENT_GUID);
689 dell_wmi_input_destroy();
690 return err;
691 }
692 }
693
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000694 return 0;
695}
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700696module_init(dell_wmi_init);
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000697
698static void __exit dell_wmi_exit(void)
699{
Michał Kępieńe09c4d52016-03-04 14:09:08 +0100700 if (wmi_requires_smbios_request)
701 dell_wmi_events_set_enabled(false);
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800702 wmi_remove_notify_handler(DELL_EVENT_GUID);
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700703 dell_wmi_input_destroy();
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000704}
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000705module_exit(dell_wmi_exit);