blob: f8978464df31c9a8273b93776fc1224776d71471 [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>
Andy Lutomirskibff589b2015-11-25 07:53:13 -080039#include <linux/wmi.h>
Hans de Goede18bd7692015-06-16 16:28:01 +020040#include <acpi/video.h>
Michał Kępieńe09c4d52016-03-04 14:09:08 +010041#include "dell-smbios.h"
Matthew Garrett0b3f6102009-01-09 20:17:11 +000042
43MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
Pali Rohár595773a42016-01-04 22:26:35 +010044MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
Matthew Garrett0b3f6102009-01-09 20:17:11 +000045MODULE_DESCRIPTION("Dell laptop WMI hotkeys driver");
46MODULE_LICENSE("GPL");
47
48#define DELL_EVENT_GUID "9DBB5994-A997-11DA-B012-B622A1EF5492"
Pali Rohár595773a42016-01-04 22:26:35 +010049#define DELL_DESCRIPTOR_GUID "8D9DDCBC-A997-11DA-B012-B622A1EF5492"
Matthew Garrett0b3f6102009-01-09 20:17:11 +000050
Pali Rohár595773a42016-01-04 22:26:35 +010051static u32 dell_wmi_interface_version;
Michał Kępieńe09c4d52016-03-04 14:09:08 +010052static bool wmi_requires_smbios_request;
Matthew Garrett0b3f6102009-01-09 20:17:11 +000053
54MODULE_ALIAS("wmi:"DELL_EVENT_GUID);
Pali Rohár595773a42016-01-04 22:26:35 +010055MODULE_ALIAS("wmi:"DELL_DESCRIPTOR_GUID);
Matthew Garrett0b3f6102009-01-09 20:17:11 +000056
Andy Lutomirskibff589b2015-11-25 07:53:13 -080057struct dell_wmi_priv {
58 struct input_dev *input_dev;
59};
60
Michał Kępieńe09c4d52016-03-04 14:09:08 +010061static int __init dmi_matched(const struct dmi_system_id *dmi)
62{
63 wmi_requires_smbios_request = 1;
64 return 1;
65}
66
67static const struct dmi_system_id dell_wmi_smbios_list[] __initconst = {
68 {
69 .callback = dmi_matched,
Michał Kępieńaaf3a5e2016-03-04 14:09:10 +010070 .ident = "Dell Inspiron M5110",
71 .matches = {
72 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
73 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
74 },
75 },
76 {
77 .callback = dmi_matched,
Michał Kępieńe09c4d52016-03-04 14:09:08 +010078 .ident = "Dell Vostro V131",
79 .matches = {
80 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
81 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
82 },
83 },
84 { }
85};
86
Mario Limonciello5cab0092009-06-10 19:40:47 +000087/*
Pali Roháre075b3c2016-06-15 21:49:13 +020088 * Keymap for WMI events of type 0x0000
89 *
Mario Limonciello5cab0092009-06-10 19:40:47 +000090 * Certain keys are flagged as KE_IGNORE. All of these are either
91 * notifications (rather than requests for change) or are also sent
92 * via the keyboard controller so should not be sent again.
93 */
Andy Lutomirskibff589b2015-11-25 07:53:13 -080094static const struct key_entry dell_wmi_keymap_type_0000[] = {
Seth Forsheef1566f02011-08-01 15:46:10 -050095 { KE_IGNORE, 0x003a, { KEY_CAPSLOCK } },
96
Pali Rohár6a4d2602016-06-15 21:49:11 +020097 /* Key code is followed by brightness level */
98 { KE_KEY, 0xe005, { KEY_BRIGHTNESSDOWN } },
99 { KE_KEY, 0xe006, { KEY_BRIGHTNESSUP } },
Mario Limonciello5cab0092009-06-10 19:40:47 +0000100
101 /* Battery health status button */
Pali Rohár6a4d2602016-06-15 21:49:11 +0200102 { KE_KEY, 0xe007, { KEY_BATTERY } },
Mario Limonciello5cab0092009-06-10 19:40:47 +0000103
Pali Rohár6a4d2602016-06-15 21:49:11 +0200104 /* Radio devices state change, key code is followed by other values */
Gabriele Mazzotta8f8d75e2014-12-04 00:16:21 +0100105 { KE_IGNORE, 0xe008, { KEY_RFKILL } },
Mario Limonciello5cab0092009-06-10 19:40:47 +0000106
Pali Rohár6a4d2602016-06-15 21:49:11 +0200107 { KE_KEY, 0xe009, { KEY_EJECTCD } },
Mario Limonciello5cab0092009-06-10 19:40:47 +0000108
Pali Rohár6a4d2602016-06-15 21:49:11 +0200109 /* Key code is followed by: next, active and attached devices */
110 { KE_KEY, 0xe00b, { KEY_SWITCHVIDEOMODE } },
111
112 /* Key code is followed by keyboard illumination level */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700113 { KE_IGNORE, 0xe00c, { KEY_KBDILLUMTOGGLE } },
Mario Limonciello5cab0092009-06-10 19:40:47 +0000114
115 /* BIOS error detected */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700116 { KE_IGNORE, 0xe00d, { KEY_RESERVED } },
Mario Limonciello5cab0092009-06-10 19:40:47 +0000117
Pali Rohár65a97a62016-07-27 09:34:42 +0200118 /* Battery was removed or inserted */
119 { KE_IGNORE, 0xe00e, { KEY_RESERVED } },
Pali Rohár3237a862016-06-15 21:49:12 +0200120
Mario Limonciello5cab0092009-06-10 19:40:47 +0000121 /* Wifi Catcher */
Mario Limonciello915ac052016-07-28 12:53:47 -0500122 { KE_KEY, 0xe011, { KEY_WLAN } },
Mario Limonciello5cab0092009-06-10 19:40:47 +0000123
124 /* Ambient light sensor toggle */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700125 { KE_IGNORE, 0xe013, { KEY_RESERVED } },
Mario Limonciello5cab0092009-06-10 19:40:47 +0000126
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700127 { KE_IGNORE, 0xe020, { KEY_MUTE } },
Seth Forsheef1566f02011-08-01 15:46:10 -0500128
Pali Rohár3237a862016-06-15 21:49:12 +0200129 /* Unknown, defined in ACPI DSDT */
130 /* { KE_IGNORE, 0xe023, { KEY_RESERVED } }, */
131
132 /* Untested, Dell Instant Launch key on Inspiron 7520 */
133 /* { KE_IGNORE, 0xe024, { KEY_RESERVED } }, */
134
Michał Kępieńaaf3a5e2016-03-04 14:09:10 +0100135 /* Dell Instant Launch key */
Pali Rohár6a4d2602016-06-15 21:49:11 +0200136 { KE_KEY, 0xe025, { KEY_PROG4 } },
Michał Kępieńaaf3a5e2016-03-04 14:09:10 +0100137
138 /* Audio panel key */
Seth Forsheef1566f02011-08-01 15:46:10 -0500139 { KE_IGNORE, 0xe026, { KEY_RESERVED } },
140
Alex Hungab9bb11c2016-06-17 11:44:27 +0800141 /* LCD Display On/Off Control key */
142 { KE_KEY, 0xe027, { KEY_DISPLAYTOGGLE } },
143
Pali Rohár3237a862016-06-15 21:49:12 +0200144 /* Untested, Multimedia key on Dell Vostro 3560 */
145 /* { KE_IGNORE, 0xe028, { KEY_RESERVED } }, */
146
Pali Rohár6a4d2602016-06-15 21:49:11 +0200147 /* Dell Instant Launch key */
148 { KE_KEY, 0xe029, { KEY_PROG4 } },
149
Pali Rohár3237a862016-06-15 21:49:12 +0200150 /* Untested, Windows Mobility Center button on Inspiron 7520 */
151 /* { KE_IGNORE, 0xe02a, { KEY_RESERVED } }, */
152
153 /* Unknown, defined in ACPI DSDT */
154 /* { KE_IGNORE, 0xe02b, { KEY_RESERVED } }, */
155
156 /* Untested, Dell Audio With Preset Switch button on Inspiron 7520 */
157 /* { KE_IGNORE, 0xe02c, { KEY_RESERVED } }, */
158
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700159 { KE_IGNORE, 0xe02e, { KEY_VOLUMEDOWN } },
160 { KE_IGNORE, 0xe030, { KEY_VOLUMEUP } },
161 { KE_IGNORE, 0xe033, { KEY_KBDILLUMUP } },
162 { KE_IGNORE, 0xe034, { KEY_KBDILLUMDOWN } },
163 { KE_IGNORE, 0xe03a, { KEY_CAPSLOCK } },
Pali Rohár3de59bc2016-06-15 21:49:10 +0200164
Pali Rohár3237a862016-06-15 21:49:12 +0200165 /* NIC Link is Up */
166 { KE_IGNORE, 0xe043, { KEY_RESERVED } },
167
168 /* NIC Link is Down */
169 { KE_IGNORE, 0xe044, { KEY_RESERVED } },
170
Pali Rohár3de59bc2016-06-15 21:49:10 +0200171 /*
172 * This entry is very suspicious!
173 * Originally Matthew Garrett created this dell-wmi driver specially for
174 * "button with a picture of a battery" which has event code 0xe045.
175 * Later Mario Limonciello from Dell told us that event code 0xe045 is
176 * reported by Num Lock and should be ignored because key is send also
177 * by keyboard controller.
178 * So for now we will ignore this event to prevent potential double
179 * Num Lock key press.
180 */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700181 { KE_IGNORE, 0xe045, { KEY_NUMLOCK } },
Pali Rohár3de59bc2016-06-15 21:49:10 +0200182
Pali Rohár3237a862016-06-15 21:49:12 +0200183 /* Scroll lock and also going to tablet mode on portable devices */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700184 { KE_IGNORE, 0xe046, { KEY_SCROLLLOCK } },
Pali Rohár3237a862016-06-15 21:49:12 +0200185
186 /* Untested, going from tablet mode on portable devices */
187 /* { KE_IGNORE, 0xe047, { KEY_RESERVED } }, */
188
189 /* Dell Support Center key */
190 { KE_IGNORE, 0xe06e, { KEY_RESERVED } },
191
Seth Forsheef1566f02011-08-01 15:46:10 -0500192 { KE_IGNORE, 0xe0f7, { KEY_MUTE } },
193 { KE_IGNORE, 0xe0f8, { KEY_VOLUMEDOWN } },
194 { KE_IGNORE, 0xe0f9, { KEY_VOLUMEUP } },
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000195};
196
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700197struct dell_bios_keymap_entry {
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500198 u16 scancode;
199 u16 keycode;
200};
201
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700202struct dell_bios_hotkey_table {
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500203 struct dmi_header header;
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700204 struct dell_bios_keymap_entry keymap[];
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500205
206};
207
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800208struct dell_dmi_results {
209 int err;
Pali Roháre075b3c2016-06-15 21:49:13 +0200210 int keymap_size;
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800211 struct key_entry *keymap;
212};
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500213
Andy Lutomirskicbc61f12015-11-30 17:02:01 -0800214/* Uninitialized entries here are KEY_RESERVED == 0. */
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800215static const u16 bios_to_linux_keycode[256] = {
Andy Lutomirski394cb772015-11-13 21:49:31 -0800216 [0] = KEY_MEDIA,
217 [1] = KEY_NEXTSONG,
218 [2] = KEY_PLAYPAUSE,
219 [3] = KEY_PREVIOUSSONG,
220 [4] = KEY_STOPCD,
221 [5] = KEY_UNKNOWN,
222 [6] = KEY_UNKNOWN,
223 [7] = KEY_UNKNOWN,
224 [8] = KEY_WWW,
225 [9] = KEY_UNKNOWN,
226 [10] = KEY_VOLUMEDOWN,
227 [11] = KEY_MUTE,
228 [12] = KEY_VOLUMEUP,
229 [13] = KEY_UNKNOWN,
230 [14] = KEY_BATTERY,
231 [15] = KEY_EJECTCD,
232 [16] = KEY_UNKNOWN,
233 [17] = KEY_SLEEP,
234 [18] = KEY_PROG1,
235 [19] = KEY_BRIGHTNESSDOWN,
236 [20] = KEY_BRIGHTNESSUP,
237 [21] = KEY_UNKNOWN,
238 [22] = KEY_KBDILLUMTOGGLE,
239 [23] = KEY_UNKNOWN,
240 [24] = KEY_SWITCHVIDEOMODE,
241 [25] = KEY_UNKNOWN,
242 [26] = KEY_UNKNOWN,
243 [27] = KEY_SWITCHVIDEOMODE,
244 [28] = KEY_UNKNOWN,
245 [29] = KEY_UNKNOWN,
246 [30] = KEY_PROG2,
247 [31] = KEY_UNKNOWN,
248 [32] = KEY_UNKNOWN,
249 [33] = KEY_UNKNOWN,
250 [34] = KEY_UNKNOWN,
251 [35] = KEY_UNKNOWN,
252 [36] = KEY_UNKNOWN,
253 [37] = KEY_UNKNOWN,
254 [38] = KEY_MICMUTE,
255 [255] = KEY_PROG3,
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500256};
257
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800258/*
Pali Roháre075b3c2016-06-15 21:49:13 +0200259 * Keymap for WMI events of type 0x0010
260 *
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800261 * These are applied if the 0xB2 DMI hotkey table is present and doesn't
262 * override them.
263 */
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800264static const struct key_entry dell_wmi_keymap_type_0010[] = {
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800265 /* Fn-lock */
266 { KE_IGNORE, 0x151, { KEY_RESERVED } },
267
268 /* Change keyboard illumination */
269 { KE_IGNORE, 0x152, { KEY_KBDILLUMTOGGLE } },
270
271 /*
272 * Radio disable (notify only -- there is no model for which the
273 * WMI event is supposed to trigger an action).
274 */
275 { KE_IGNORE, 0x153, { KEY_RFKILL } },
276
277 /* RGB keyboard backlight control */
278 { KE_IGNORE, 0x154, { KEY_RESERVED } },
279
Andy Lutomirskie4f2e3f2016-02-17 12:38:07 -0800280 /*
281 * Stealth mode toggle. This will "disable all lights and sounds".
282 * The action is performed by the BIOS and EC; the WMI event is just
283 * a notification. On the XPS 13 9350, this is Fn+F7, and there's
284 * a BIOS setting to enable and disable the hotkey.
285 */
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800286 { KE_IGNORE, 0x155, { KEY_RESERVED } },
Mario Limonciello5dc444b2016-07-28 12:53:48 -0500287
288 /* Rugged magnetic dock attach/detach events */
289 { KE_IGNORE, 0x156, { KEY_RESERVED } },
290 { KE_IGNORE, 0x157, { KEY_RESERVED } },
291
292 /* Rugged programmable (P1/P2/P3 keys) */
293 { KE_KEY, 0x850, { KEY_PROG1 } },
294 { KE_KEY, 0x851, { KEY_PROG2 } },
295 { KE_KEY, 0x852, { KEY_PROG3 } },
296
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800297};
298
Pali Roháre075b3c2016-06-15 21:49:13 +0200299/*
300 * Keymap for WMI events of type 0x0011
301 */
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800302static const struct key_entry dell_wmi_keymap_type_0011[] = {
Pali Roháre075b3c2016-06-15 21:49:13 +0200303 /* Battery unplugged */
304 { KE_IGNORE, 0xfff0, { KEY_RESERVED } },
305
306 /* Battery inserted */
307 { KE_IGNORE, 0xfff1, { KEY_RESERVED } },
308
309 /* Keyboard backlight level changed */
310 { KE_IGNORE, 0x01e1, { KEY_RESERVED } },
311 { KE_IGNORE, 0x02ea, { KEY_RESERVED } },
312 { KE_IGNORE, 0x02eb, { KEY_RESERVED } },
313 { KE_IGNORE, 0x02ec, { KEY_RESERVED } },
314 { KE_IGNORE, 0x02f6, { KEY_RESERVED } },
315};
316
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800317static void dell_wmi_process_key(struct wmi_device *wdev, int type, int code)
Pali Rohár83fc44c2014-11-11 20:21:22 +0100318{
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800319 struct dell_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
Pali Rohár83fc44c2014-11-11 20:21:22 +0100320 const struct key_entry *key;
321
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800322 key = sparse_keymap_entry_from_scancode(priv->input_dev,
Pali Roháre075b3c2016-06-15 21:49:13 +0200323 (type << 16) | code);
Pali Rohár83fc44c2014-11-11 20:21:22 +0100324 if (!key) {
Pali Roháre075b3c2016-06-15 21:49:13 +0200325 pr_info("Unknown key with type 0x%04x and code 0x%04x pressed\n",
326 type, code);
Pali Rohár83fc44c2014-11-11 20:21:22 +0100327 return;
328 }
329
Pali Roháre075b3c2016-06-15 21:49:13 +0200330 pr_debug("Key with type 0x%04x and code 0x%04x pressed\n", type, code);
Pali Rohár83fc44c2014-11-11 20:21:22 +0100331
332 /* Don't report brightness notifications that will also come via ACPI */
333 if ((key->keycode == KEY_BRIGHTNESSUP ||
Hans de Goede61679c72015-12-22 19:09:49 +0100334 key->keycode == KEY_BRIGHTNESSDOWN) &&
335 acpi_video_handles_brightness_key_presses())
Pali Rohár83fc44c2014-11-11 20:21:22 +0100336 return;
337
Pali Roháre075b3c2016-06-15 21:49:13 +0200338 if (type == 0x0000 && code == 0xe025 && !wmi_requires_smbios_request)
Michał Kępień13f50592016-03-04 14:09:09 +0100339 return;
340
Hans de Goede9c656b02017-03-16 11:55:35 +0100341 if (key->keycode == KEY_KBDILLUMTOGGLE)
342 dell_laptop_call_notifier(
343 DELL_LAPTOP_KBD_BACKLIGHT_BRIGHTNESS_CHANGED, NULL);
344
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800345 sparse_keymap_report_entry(priv->input_dev, key, 1, true);
Pali Rohár83fc44c2014-11-11 20:21:22 +0100346}
347
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800348static void dell_wmi_notify(struct wmi_device *wdev,
349 union acpi_object *obj)
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000350{
Pali Rohár83fc44c2014-11-11 20:21:22 +0100351 u16 *buffer_entry, *buffer_end;
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800352 acpi_size buffer_size;
Pali Rohár83fc44c2014-11-11 20:21:22 +0100353 int len, i;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000354
Pali Rohár83fc44c2014-11-11 20:21:22 +0100355 if (obj->type != ACPI_TYPE_BUFFER) {
356 pr_warn("bad response type %x\n", obj->type);
Pali Rohár83fc44c2014-11-11 20:21:22 +0100357 return;
358 }
359
360 pr_debug("Received WMI event (%*ph)\n",
361 obj->buffer.length, obj->buffer.pointer);
362
363 buffer_entry = (u16 *)obj->buffer.pointer;
364 buffer_size = obj->buffer.length/2;
Pali Rohár83fc44c2014-11-11 20:21:22 +0100365 buffer_end = buffer_entry + buffer_size;
366
Pali Rohár481fe5b2016-01-04 22:26:36 +0100367 /*
368 * BIOS/ACPI on devices with WMI interface version 0 does not clear
369 * buffer before filling it. So next time when BIOS/ACPI send WMI event
370 * which is smaller as previous then it contains garbage in buffer from
371 * previous event.
372 *
373 * BIOS/ACPI on devices with WMI interface version 1 clears buffer and
374 * sometimes send more events in buffer at one call.
375 *
376 * So to prevent reading garbage from buffer we will process only first
377 * one event on devices with WMI interface version 0.
378 */
379 if (dell_wmi_interface_version == 0 && buffer_entry < buffer_end)
380 if (buffer_end > buffer_entry + buffer_entry[0] + 1)
381 buffer_end = buffer_entry + buffer_entry[0] + 1;
382
Pali Rohár83fc44c2014-11-11 20:21:22 +0100383 while (buffer_entry < buffer_end) {
384
385 len = buffer_entry[0];
386 if (len == 0)
387 break;
388
389 len++;
390
391 if (buffer_entry + len > buffer_end) {
392 pr_warn("Invalid length of WMI event\n");
393 break;
394 }
395
396 pr_debug("Process buffer (%*ph)\n", len*2, buffer_entry);
397
398 switch (buffer_entry[1]) {
Pali Roháre075b3c2016-06-15 21:49:13 +0200399 case 0x0000: /* One key pressed or event occurred */
400 if (len > 2)
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800401 dell_wmi_process_key(wdev, 0x0000,
402 buffer_entry[2]);
Pali Roháre075b3c2016-06-15 21:49:13 +0200403 /* Other entries could contain additional information */
Pali Rohár83fc44c2014-11-11 20:21:22 +0100404 break;
Pali Roháre075b3c2016-06-15 21:49:13 +0200405 case 0x0010: /* Sequence of keys pressed */
406 case 0x0011: /* Sequence of events occurred */
Pali Rohár83fc44c2014-11-11 20:21:22 +0100407 for (i = 2; i < len; ++i)
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800408 dell_wmi_process_key(wdev, buffer_entry[1],
Pali Roháre075b3c2016-06-15 21:49:13 +0200409 buffer_entry[i]);
Pali Rohár83fc44c2014-11-11 20:21:22 +0100410 break;
Pali Roháre075b3c2016-06-15 21:49:13 +0200411 default: /* Unknown event */
Pali Rohár83fc44c2014-11-11 20:21:22 +0100412 pr_info("Unknown WMI event type 0x%x\n",
413 (int)buffer_entry[1]);
414 break;
415 }
416
417 buffer_entry += len;
418
419 }
420
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000421}
422
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800423static bool have_scancode(u32 scancode, const struct key_entry *keymap, int len)
424{
425 int i;
426
427 for (i = 0; i < len; i++)
428 if (keymap[i].code == scancode)
429 return true;
430
431 return false;
432}
433
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800434static void handle_dmi_entry(const struct dmi_header *dm, void *opaque)
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500435{
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800436 struct dell_dmi_results *results = opaque;
437 struct dell_bios_hotkey_table *table;
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800438 int hotkey_num, i, pos = 0;
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700439 struct key_entry *keymap;
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800440
441 if (results->err || results->keymap)
442 return; /* We already found the hotkey table. */
443
Andy Lutomirski074df512016-02-17 12:34:34 -0800444 /* The Dell hotkey table is type 0xB2. Scan until we find it. */
Andy Lutomirskib13de702016-02-15 08:32:35 -0800445 if (dm->type != 0xb2)
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800446 return;
447
448 table = container_of(dm, struct dell_bios_hotkey_table, header);
449
Andy Lutomirskib13de702016-02-15 08:32:35 -0800450 hotkey_num = (table->header.length -
451 sizeof(struct dell_bios_hotkey_table)) /
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800452 sizeof(struct dell_bios_keymap_entry);
Andy Lutomirskib13de702016-02-15 08:32:35 -0800453 if (hotkey_num < 1) {
454 /*
455 * Historically, dell-wmi would ignore a DMI entry of
456 * fewer than 7 bytes. Sizes between 4 and 8 bytes are
457 * nonsensical (both the header and all entries are 4
458 * bytes), so we approximate the old behavior by
459 * ignoring tables with fewer than one entry.
460 */
461 return;
462 }
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500463
Pali Roháre075b3c2016-06-15 21:49:13 +0200464 keymap = kcalloc(hotkey_num, sizeof(struct key_entry), GFP_KERNEL);
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800465 if (!keymap) {
466 results->err = -ENOMEM;
467 return;
468 }
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500469
470 for (i = 0; i < hotkey_num; i++) {
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700471 const struct dell_bios_keymap_entry *bios_entry =
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800472 &table->keymap[i];
Andy Lutomirskicbc61f12015-11-30 17:02:01 -0800473
474 /* Uninitialized entries are 0 aka KEY_RESERVED. */
475 u16 keycode = (bios_entry->keycode <
476 ARRAY_SIZE(bios_to_linux_keycode)) ?
477 bios_to_linux_keycode[bios_entry->keycode] :
478 KEY_RESERVED;
479
480 /*
481 * Log if we find an entry in the DMI table that we don't
482 * understand. If this happens, we should figure out what
483 * the entry means and add it to bios_to_linux_keycode.
484 */
485 if (keycode == KEY_RESERVED) {
486 pr_info("firmware scancode 0x%x maps to unrecognized keycode 0x%x\n",
487 bios_entry->scancode, bios_entry->keycode);
488 continue;
489 }
Gabriele Mazzotta8cb8e632014-12-04 00:16:23 +0100490
491 if (keycode == KEY_KBDILLUMTOGGLE)
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800492 keymap[pos].type = KE_IGNORE;
Gabriele Mazzotta8cb8e632014-12-04 00:16:23 +0100493 else
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800494 keymap[pos].type = KE_KEY;
495 keymap[pos].code = bios_entry->scancode;
496 keymap[pos].keycode = keycode;
497
498 pos++;
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500499 }
500
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800501 results->keymap = keymap;
Pali Roháre075b3c2016-06-15 21:49:13 +0200502 results->keymap_size = pos;
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500503}
504
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800505static int dell_wmi_input_setup(struct wmi_device *wdev)
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000506{
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800507 struct dell_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800508 struct dell_dmi_results dmi_results = {};
Pali Roháre075b3c2016-06-15 21:49:13 +0200509 struct key_entry *keymap;
510 int err, i, pos = 0;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000511
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800512 priv->input_dev = input_allocate_device();
513 if (!priv->input_dev)
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000514 return -ENOMEM;
515
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800516 priv->input_dev->name = "Dell WMI hotkeys";
517 priv->input_dev->id.bustype = BUS_HOST;
518 priv->input_dev->dev.parent = &wdev->dev;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000519
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800520 if (dmi_walk(handle_dmi_entry, &dmi_results)) {
521 /*
522 * Historically, dell-wmi ignored dmi_walk errors. A failure
523 * is certainly surprising, but it probably just indicates
524 * a very old laptop.
525 */
526 pr_warn("no DMI; using the old-style hotkey interface\n");
527 }
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700528
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800529 if (dmi_results.err) {
530 err = dmi_results.err;
531 goto err_free_dev;
532 }
533
Pali Roháre075b3c2016-06-15 21:49:13 +0200534 keymap = kcalloc(dmi_results.keymap_size +
535 ARRAY_SIZE(dell_wmi_keymap_type_0000) +
536 ARRAY_SIZE(dell_wmi_keymap_type_0010) +
537 ARRAY_SIZE(dell_wmi_keymap_type_0011) +
538 1,
539 sizeof(struct key_entry), GFP_KERNEL);
540 if (!keymap) {
541 kfree(dmi_results.keymap);
542 err = -ENOMEM;
543 goto err_free_dev;
544 }
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800545
Pali Roháre075b3c2016-06-15 21:49:13 +0200546 /* Append table with events of type 0x0010 which comes from DMI */
547 for (i = 0; i < dmi_results.keymap_size; i++) {
548 keymap[pos] = dmi_results.keymap[i];
549 keymap[pos].code |= (0x0010 << 16);
550 pos++;
551 }
552
553 kfree(dmi_results.keymap);
554
555 /* Append table with extra events of type 0x0010 which are not in DMI */
556 for (i = 0; i < ARRAY_SIZE(dell_wmi_keymap_type_0010); i++) {
557 const struct key_entry *entry = &dell_wmi_keymap_type_0010[i];
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700558
559 /*
Pali Roháre075b3c2016-06-15 21:49:13 +0200560 * Check if we've already found this scancode. This takes
561 * quadratic time, but it doesn't matter unless the list
562 * of extra keys gets very long.
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700563 */
Pali Roháre075b3c2016-06-15 21:49:13 +0200564 if (dmi_results.keymap_size &&
565 have_scancode(entry->code | (0x0010 << 16),
566 keymap, dmi_results.keymap_size)
567 )
568 continue;
569
570 keymap[pos] = *entry;
571 keymap[pos].code |= (0x0010 << 16);
572 pos++;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000573 }
Pali Roháre075b3c2016-06-15 21:49:13 +0200574
575 /* Append table with events of type 0x0011 */
576 for (i = 0; i < ARRAY_SIZE(dell_wmi_keymap_type_0011); i++) {
577 keymap[pos] = dell_wmi_keymap_type_0011[i];
578 keymap[pos].code |= (0x0011 << 16);
579 pos++;
580 }
581
582 /*
583 * Now append also table with "legacy" events of type 0x0000. Some of
584 * them are reported also on laptops which have scancodes in DMI.
585 */
586 for (i = 0; i < ARRAY_SIZE(dell_wmi_keymap_type_0000); i++) {
587 keymap[pos] = dell_wmi_keymap_type_0000[i];
588 pos++;
589 }
590
591 keymap[pos].type = KE_END;
592
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800593 err = sparse_keymap_setup(priv->input_dev, keymap, NULL);
Pali Roháre075b3c2016-06-15 21:49:13 +0200594 /*
595 * Sparse keymap library makes a copy of keymap so we don't need the
596 * original one that was allocated.
597 */
598 kfree(keymap);
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700599 if (err)
600 goto err_free_dev;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000601
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800602 err = input_register_device(priv->input_dev);
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700603 if (err)
Michał Kępień815edfe2017-03-09 13:11:41 +0100604 goto err_free_dev;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000605
606 return 0;
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700607
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700608 err_free_dev:
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800609 input_free_device(priv->input_dev);
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700610 return err;
611}
612
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800613static void dell_wmi_input_destroy(struct wmi_device *wdev)
614{
615 struct dell_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
616
617 input_unregister_device(priv->input_dev);
618}
619
Pali Rohár595773a42016-01-04 22:26:35 +0100620/*
621 * Descriptor buffer is 128 byte long and contains:
622 *
623 * Name Offset Length Value
624 * Vendor Signature 0 4 "DELL"
625 * Object Signature 4 4 " WMI"
626 * WMI Interface Version 8 4 <version>
627 * WMI buffer length 12 4 4096
628 */
629static int __init dell_wmi_check_descriptor_buffer(void)
630{
631 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
632 union acpi_object *obj;
633 acpi_status status;
634 u32 *buffer;
635
636 status = wmi_query_block(DELL_DESCRIPTOR_GUID, 0, &out);
637 if (ACPI_FAILURE(status)) {
638 pr_err("Cannot read Dell descriptor buffer - %d\n", status);
639 return status;
640 }
641
642 obj = (union acpi_object *)out.pointer;
643 if (!obj) {
644 pr_err("Dell descriptor buffer is empty\n");
645 return -EINVAL;
646 }
647
648 if (obj->type != ACPI_TYPE_BUFFER) {
649 pr_err("Cannot read Dell descriptor buffer\n");
650 kfree(obj);
651 return -EINVAL;
652 }
653
654 if (obj->buffer.length != 128) {
655 pr_err("Dell descriptor buffer has invalid length (%d)\n",
656 obj->buffer.length);
657 if (obj->buffer.length < 16) {
658 kfree(obj);
659 return -EINVAL;
660 }
661 }
662
663 buffer = (u32 *)obj->buffer.pointer;
664
665 if (buffer[0] != 0x4C4C4544 && buffer[1] != 0x494D5720)
666 pr_warn("Dell descriptor buffer has invalid signature (%*ph)\n",
667 8, buffer);
668
669 if (buffer[2] != 0 && buffer[2] != 1)
670 pr_warn("Dell descriptor buffer has unknown version (%d)\n",
671 buffer[2]);
672
673 if (buffer[3] != 4096)
674 pr_warn("Dell descriptor buffer has invalid buffer length (%d)\n",
675 buffer[3]);
676
677 dell_wmi_interface_version = buffer[2];
678
679 pr_info("Detected Dell WMI interface version %u\n",
680 dell_wmi_interface_version);
681
682 kfree(obj);
683 return 0;
684}
685
Michał Kępieńe09c4d52016-03-04 14:09:08 +0100686/*
687 * According to Dell SMBIOS documentation:
688 *
689 * 17 3 Application Program Registration
690 *
691 * cbArg1 Application ID 1 = 0x00010000
692 * cbArg2 Application ID 2
693 * QUICKSET/DCP = 0x51534554 "QSET"
694 * ALS Driver = 0x416c7353 "AlsS"
695 * Latitude ON = 0x4c6f6e52 "LonR"
696 * cbArg3 Application version or revision number
697 * cbArg4 0 = Unregister application
698 * 1 = Register application
699 * cbRes1 Standard return codes (0, -1, -2)
700 */
701
702static int dell_wmi_events_set_enabled(bool enable)
703{
704 struct calling_interface_buffer *buffer;
705 int ret;
706
707 buffer = dell_smbios_get_buffer();
708 buffer->input[0] = 0x10000;
709 buffer->input[1] = 0x51534554;
710 buffer->input[3] = enable;
711 dell_smbios_send_request(17, 3);
712 ret = buffer->output[0];
713 dell_smbios_release_buffer();
714
715 return dell_smbios_error(ret);
716}
717
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800718static int dell_wmi_probe(struct wmi_device *wdev)
719{
720 struct dell_wmi_priv *priv = devm_kzalloc(
721 &wdev->dev, sizeof(struct dell_wmi_priv), GFP_KERNEL);
722
723 dev_set_drvdata(&wdev->dev, priv);
724
725 return dell_wmi_input_setup(wdev);
726}
727
728static int dell_wmi_remove(struct wmi_device *wdev)
729{
730 dell_wmi_input_destroy(wdev);
731 return 0;
732}
733static const struct wmi_device_id dell_wmi_id_table[] = {
734 { .guid_string = DELL_EVENT_GUID },
735 { },
736};
737
738static struct wmi_driver dell_wmi_driver = {
739 .driver = {
740 .name = "dell-wmi",
741 },
742 .id_table = dell_wmi_id_table,
743 .probe = dell_wmi_probe,
744 .remove = dell_wmi_remove,
745 .notify = dell_wmi_notify,
746};
747
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000748static int __init dell_wmi_init(void)
749{
750 int err;
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800751
Pali Rohár595773a42016-01-04 22:26:35 +0100752 err = dell_wmi_check_descriptor_buffer();
753 if (err)
754 return err;
755
Michał Kępieńe09c4d52016-03-04 14:09:08 +0100756 dmi_check_system(dell_wmi_smbios_list);
757
758 if (wmi_requires_smbios_request) {
759 err = dell_wmi_events_set_enabled(true);
760 if (err) {
761 pr_err("Failed to enable WMI events\n");
Michał Kępieńe09c4d52016-03-04 14:09:08 +0100762 return err;
763 }
764 }
765
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800766 return wmi_driver_register(&dell_wmi_driver);
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000767}
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700768module_init(dell_wmi_init);
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000769
770static void __exit dell_wmi_exit(void)
771{
Michał Kępieńe09c4d52016-03-04 14:09:08 +0100772 if (wmi_requires_smbios_request)
773 dell_wmi_events_set_enabled(false);
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800774
775 wmi_driver_unregister(&dell_wmi_driver);
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000776}
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000777module_exit(dell_wmi_exit);