blob: 65edd93df7de998018ff2a4ec8d9805c765b58d2 [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 */
114 { KE_IGNORE, 0xe025, { KEY_RESERVED } },
115 { 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
238 sparse_keymap_report_entry(dell_wmi_input_dev, key, 1, true);
239}
240
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000241static void dell_wmi_notify(u32 value, void *context)
242{
243 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000244 union acpi_object *obj;
Len Brownfda11e62009-12-26 23:02:24 -0500245 acpi_status status;
Pali Rohár83fc44c2014-11-11 20:21:22 +0100246 acpi_size buffer_size;
247 u16 *buffer_entry, *buffer_end;
248 int len, i;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000249
Len Brownfda11e62009-12-26 23:02:24 -0500250 status = wmi_get_event_data(value, &response);
251 if (status != AE_OK) {
Pali Rohár83fc44c2014-11-11 20:21:22 +0100252 pr_warn("bad event status 0x%x\n", status);
Len Brownfda11e62009-12-26 23:02:24 -0500253 return;
254 }
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000255
256 obj = (union acpi_object *)response.pointer;
Pali Rohár83fc44c2014-11-11 20:21:22 +0100257 if (!obj) {
258 pr_warn("no response\n");
259 return;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000260 }
Pali Rohár83fc44c2014-11-11 20:21:22 +0100261
262 if (obj->type != ACPI_TYPE_BUFFER) {
263 pr_warn("bad response type %x\n", obj->type);
264 kfree(obj);
265 return;
266 }
267
268 pr_debug("Received WMI event (%*ph)\n",
269 obj->buffer.length, obj->buffer.pointer);
270
271 buffer_entry = (u16 *)obj->buffer.pointer;
272 buffer_size = obj->buffer.length/2;
273
274 if (!dell_new_hk_type) {
275 if (buffer_size >= 3 && buffer_entry[1] == 0x0)
276 dell_wmi_process_key(buffer_entry[2]);
277 else if (buffer_size >= 2)
278 dell_wmi_process_key(buffer_entry[1]);
279 else
280 pr_info("Received unknown WMI event\n");
281 kfree(obj);
282 return;
283 }
284
285 buffer_end = buffer_entry + buffer_size;
286
Pali Rohár481fe5b2016-01-04 22:26:36 +0100287 /*
288 * BIOS/ACPI on devices with WMI interface version 0 does not clear
289 * buffer before filling it. So next time when BIOS/ACPI send WMI event
290 * which is smaller as previous then it contains garbage in buffer from
291 * previous event.
292 *
293 * BIOS/ACPI on devices with WMI interface version 1 clears buffer and
294 * sometimes send more events in buffer at one call.
295 *
296 * So to prevent reading garbage from buffer we will process only first
297 * one event on devices with WMI interface version 0.
298 */
299 if (dell_wmi_interface_version == 0 && buffer_entry < buffer_end)
300 if (buffer_end > buffer_entry + buffer_entry[0] + 1)
301 buffer_end = buffer_entry + buffer_entry[0] + 1;
302
Pali Rohár83fc44c2014-11-11 20:21:22 +0100303 while (buffer_entry < buffer_end) {
304
305 len = buffer_entry[0];
306 if (len == 0)
307 break;
308
309 len++;
310
311 if (buffer_entry + len > buffer_end) {
312 pr_warn("Invalid length of WMI event\n");
313 break;
314 }
315
316 pr_debug("Process buffer (%*ph)\n", len*2, buffer_entry);
317
318 switch (buffer_entry[1]) {
319 case 0x00:
320 for (i = 2; i < len; ++i) {
321 switch (buffer_entry[i]) {
322 case 0xe043:
323 /* NIC Link is Up */
324 pr_debug("NIC Link is Up\n");
325 break;
326 case 0xe044:
327 /* NIC Link is Down */
328 pr_debug("NIC Link is Down\n");
329 break;
330 case 0xe045:
331 /* Unknown event but defined in DSDT */
332 default:
333 /* Unknown event */
334 pr_info("Unknown WMI event type 0x00: "
335 "0x%x\n", (int)buffer_entry[i]);
336 break;
337 }
338 }
339 break;
340 case 0x10:
341 /* Keys pressed */
342 for (i = 2; i < len; ++i)
343 dell_wmi_process_key(buffer_entry[i]);
344 break;
345 case 0x11:
346 for (i = 2; i < len; ++i) {
347 switch (buffer_entry[i]) {
348 case 0xfff0:
349 /* Battery unplugged */
350 pr_debug("Battery unplugged\n");
351 break;
352 case 0xfff1:
353 /* Battery inserted */
354 pr_debug("Battery inserted\n");
355 break;
356 case 0x01e1:
357 case 0x02ea:
358 case 0x02eb:
359 case 0x02ec:
360 case 0x02f6:
361 /* Keyboard backlight level changed */
362 pr_debug("Keyboard backlight level "
363 "changed\n");
364 break;
365 default:
366 /* Unknown event */
367 pr_info("Unknown WMI event type 0x11: "
368 "0x%x\n", (int)buffer_entry[i]);
369 break;
370 }
371 }
372 break;
373 default:
374 /* Unknown event */
375 pr_info("Unknown WMI event type 0x%x\n",
376 (int)buffer_entry[1]);
377 break;
378 }
379
380 buffer_entry += len;
381
382 }
383
Anisse Astier3e9b9882009-12-04 10:10:09 +0100384 kfree(obj);
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000385}
386
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800387static bool have_scancode(u32 scancode, const struct key_entry *keymap, int len)
388{
389 int i;
390
391 for (i = 0; i < len; i++)
392 if (keymap[i].code == scancode)
393 return true;
394
395 return false;
396}
397
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800398static void __init handle_dmi_entry(const struct dmi_header *dm,
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800399
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800400 void *opaque)
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800401
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500402{
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800403 struct dell_dmi_results *results = opaque;
404 struct dell_bios_hotkey_table *table;
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800405 int hotkey_num, i, pos = 0;
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700406 struct key_entry *keymap;
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800407 int num_bios_keys;
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800408
409 if (results->err || results->keymap)
410 return; /* We already found the hotkey table. */
411
Andy Lutomirskib13de702016-02-15 08:32:35 -0800412 if (dm->type != 0xb2)
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800413 return;
414
415 table = container_of(dm, struct dell_bios_hotkey_table, header);
416
Andy Lutomirskib13de702016-02-15 08:32:35 -0800417 hotkey_num = (table->header.length -
418 sizeof(struct dell_bios_hotkey_table)) /
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800419 sizeof(struct dell_bios_keymap_entry);
Andy Lutomirskib13de702016-02-15 08:32:35 -0800420 if (hotkey_num < 1) {
421 /*
422 * Historically, dell-wmi would ignore a DMI entry of
423 * fewer than 7 bytes. Sizes between 4 and 8 bytes are
424 * nonsensical (both the header and all entries are 4
425 * bytes), so we approximate the old behavior by
426 * ignoring tables with fewer than one entry.
427 */
428 return;
429 }
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500430
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800431 keymap = kcalloc(hotkey_num + ARRAY_SIZE(dell_wmi_extra_keymap) + 1,
432 sizeof(struct key_entry), GFP_KERNEL);
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800433 if (!keymap) {
434 results->err = -ENOMEM;
435 return;
436 }
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500437
438 for (i = 0; i < hotkey_num; i++) {
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700439 const struct dell_bios_keymap_entry *bios_entry =
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800440 &table->keymap[i];
Andy Lutomirskicbc61f12015-11-30 17:02:01 -0800441
442 /* Uninitialized entries are 0 aka KEY_RESERVED. */
443 u16 keycode = (bios_entry->keycode <
444 ARRAY_SIZE(bios_to_linux_keycode)) ?
445 bios_to_linux_keycode[bios_entry->keycode] :
446 KEY_RESERVED;
447
448 /*
449 * Log if we find an entry in the DMI table that we don't
450 * understand. If this happens, we should figure out what
451 * the entry means and add it to bios_to_linux_keycode.
452 */
453 if (keycode == KEY_RESERVED) {
454 pr_info("firmware scancode 0x%x maps to unrecognized keycode 0x%x\n",
455 bios_entry->scancode, bios_entry->keycode);
456 continue;
457 }
Gabriele Mazzotta8cb8e632014-12-04 00:16:23 +0100458
459 if (keycode == KEY_KBDILLUMTOGGLE)
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800460 keymap[pos].type = KE_IGNORE;
Gabriele Mazzotta8cb8e632014-12-04 00:16:23 +0100461 else
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800462 keymap[pos].type = KE_KEY;
463 keymap[pos].code = bios_entry->scancode;
464 keymap[pos].keycode = keycode;
465
466 pos++;
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500467 }
468
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800469 num_bios_keys = pos;
470
471 for (i = 0; i < ARRAY_SIZE(dell_wmi_extra_keymap); i++) {
472 const struct key_entry *entry = &dell_wmi_extra_keymap[i];
473
474 /*
475 * Check if we've already found this scancode. This takes
476 * quadratic time, but it doesn't matter unless the list
477 * of extra keys gets very long.
478 */
479 if (!have_scancode(entry->code, keymap, num_bios_keys)) {
480 keymap[pos] = *entry;
481 pos++;
482 }
483 }
484
485 keymap[pos].type = KE_END;
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500486
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800487 results->keymap = keymap;
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500488}
489
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000490static int __init dell_wmi_input_setup(void)
491{
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800492 struct dell_dmi_results dmi_results = {};
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000493 int err;
494
495 dell_wmi_input_dev = input_allocate_device();
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000496 if (!dell_wmi_input_dev)
497 return -ENOMEM;
498
499 dell_wmi_input_dev->name = "Dell WMI hotkeys";
500 dell_wmi_input_dev->phys = "wmi/input0";
501 dell_wmi_input_dev->id.bustype = BUS_HOST;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000502
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800503 if (dmi_walk(handle_dmi_entry, &dmi_results)) {
504 /*
505 * Historically, dell-wmi ignored dmi_walk errors. A failure
506 * is certainly surprising, but it probably just indicates
507 * a very old laptop.
508 */
509 pr_warn("no DMI; using the old-style hotkey interface\n");
510 }
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700511
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800512 if (dmi_results.err) {
513 err = dmi_results.err;
514 goto err_free_dev;
515 }
516
517 if (dmi_results.keymap) {
518 dell_new_hk_type = true;
519
520 err = sparse_keymap_setup(dell_wmi_input_dev,
521 dmi_results.keymap, NULL);
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700522
523 /*
524 * Sparse keymap library makes a copy of keymap so we
525 * don't need the original one that was allocated.
526 */
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800527 kfree(dmi_results.keymap);
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700528 } else {
529 err = sparse_keymap_setup(dell_wmi_input_dev,
530 dell_wmi_legacy_keymap, NULL);
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000531 }
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700532 if (err)
533 goto err_free_dev;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000534
535 err = input_register_device(dell_wmi_input_dev);
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700536 if (err)
537 goto err_free_keymap;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000538
539 return 0;
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700540
541 err_free_keymap:
542 sparse_keymap_free(dell_wmi_input_dev);
543 err_free_dev:
544 input_free_device(dell_wmi_input_dev);
545 return err;
546}
547
548static void dell_wmi_input_destroy(void)
549{
550 sparse_keymap_free(dell_wmi_input_dev);
551 input_unregister_device(dell_wmi_input_dev);
552}
553
Pali Rohár595773a42016-01-04 22:26:35 +0100554/*
555 * Descriptor buffer is 128 byte long and contains:
556 *
557 * Name Offset Length Value
558 * Vendor Signature 0 4 "DELL"
559 * Object Signature 4 4 " WMI"
560 * WMI Interface Version 8 4 <version>
561 * WMI buffer length 12 4 4096
562 */
563static int __init dell_wmi_check_descriptor_buffer(void)
564{
565 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
566 union acpi_object *obj;
567 acpi_status status;
568 u32 *buffer;
569
570 status = wmi_query_block(DELL_DESCRIPTOR_GUID, 0, &out);
571 if (ACPI_FAILURE(status)) {
572 pr_err("Cannot read Dell descriptor buffer - %d\n", status);
573 return status;
574 }
575
576 obj = (union acpi_object *)out.pointer;
577 if (!obj) {
578 pr_err("Dell descriptor buffer is empty\n");
579 return -EINVAL;
580 }
581
582 if (obj->type != ACPI_TYPE_BUFFER) {
583 pr_err("Cannot read Dell descriptor buffer\n");
584 kfree(obj);
585 return -EINVAL;
586 }
587
588 if (obj->buffer.length != 128) {
589 pr_err("Dell descriptor buffer has invalid length (%d)\n",
590 obj->buffer.length);
591 if (obj->buffer.length < 16) {
592 kfree(obj);
593 return -EINVAL;
594 }
595 }
596
597 buffer = (u32 *)obj->buffer.pointer;
598
599 if (buffer[0] != 0x4C4C4544 && buffer[1] != 0x494D5720)
600 pr_warn("Dell descriptor buffer has invalid signature (%*ph)\n",
601 8, buffer);
602
603 if (buffer[2] != 0 && buffer[2] != 1)
604 pr_warn("Dell descriptor buffer has unknown version (%d)\n",
605 buffer[2]);
606
607 if (buffer[3] != 4096)
608 pr_warn("Dell descriptor buffer has invalid buffer length (%d)\n",
609 buffer[3]);
610
611 dell_wmi_interface_version = buffer[2];
612
613 pr_info("Detected Dell WMI interface version %u\n",
614 dell_wmi_interface_version);
615
616 kfree(obj);
617 return 0;
618}
619
Michał Kępieńe09c4d52016-03-04 14:09:08 +0100620/*
621 * According to Dell SMBIOS documentation:
622 *
623 * 17 3 Application Program Registration
624 *
625 * cbArg1 Application ID 1 = 0x00010000
626 * cbArg2 Application ID 2
627 * QUICKSET/DCP = 0x51534554 "QSET"
628 * ALS Driver = 0x416c7353 "AlsS"
629 * Latitude ON = 0x4c6f6e52 "LonR"
630 * cbArg3 Application version or revision number
631 * cbArg4 0 = Unregister application
632 * 1 = Register application
633 * cbRes1 Standard return codes (0, -1, -2)
634 */
635
636static int dell_wmi_events_set_enabled(bool enable)
637{
638 struct calling_interface_buffer *buffer;
639 int ret;
640
641 buffer = dell_smbios_get_buffer();
642 buffer->input[0] = 0x10000;
643 buffer->input[1] = 0x51534554;
644 buffer->input[3] = enable;
645 dell_smbios_send_request(17, 3);
646 ret = buffer->output[0];
647 dell_smbios_release_buffer();
648
649 return dell_smbios_error(ret);
650}
651
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000652static int __init dell_wmi_init(void)
653{
654 int err;
Len Brownabb631b2009-12-26 21:51:38 -0500655 acpi_status status;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000656
Pali Rohár595773a42016-01-04 22:26:35 +0100657 if (!wmi_has_guid(DELL_EVENT_GUID) ||
658 !wmi_has_guid(DELL_DESCRIPTOR_GUID)) {
659 pr_warn("Dell WMI GUID were not found\n");
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800660 return -ENODEV;
661 }
662
Pali Rohár595773a42016-01-04 22:26:35 +0100663 err = dell_wmi_check_descriptor_buffer();
664 if (err)
665 return err;
666
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800667 err = dell_wmi_input_setup();
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700668 if (err)
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800669 return err;
670
Len Brownabb631b2009-12-26 21:51:38 -0500671 status = wmi_install_notify_handler(DELL_EVENT_GUID,
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800672 dell_wmi_notify, NULL);
Len Brownabb631b2009-12-26 21:51:38 -0500673 if (ACPI_FAILURE(status)) {
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700674 dell_wmi_input_destroy();
Joe Percheseb889522011-03-29 15:21:37 -0700675 pr_err("Unable to register notify handler - %d\n", status);
Len Brownabb631b2009-12-26 21:51:38 -0500676 return -ENODEV;
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800677 }
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000678
Michał Kępieńe09c4d52016-03-04 14:09:08 +0100679 dmi_check_system(dell_wmi_smbios_list);
680
681 if (wmi_requires_smbios_request) {
682 err = dell_wmi_events_set_enabled(true);
683 if (err) {
684 pr_err("Failed to enable WMI events\n");
685 wmi_remove_notify_handler(DELL_EVENT_GUID);
686 dell_wmi_input_destroy();
687 return err;
688 }
689 }
690
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000691 return 0;
692}
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700693module_init(dell_wmi_init);
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000694
695static void __exit dell_wmi_exit(void)
696{
Michał Kępieńe09c4d52016-03-04 14:09:08 +0100697 if (wmi_requires_smbios_request)
698 dell_wmi_events_set_enabled(false);
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800699 wmi_remove_notify_handler(DELL_EVENT_GUID);
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700700 dell_wmi_input_destroy();
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000701}
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000702module_exit(dell_wmi_exit);