blob: 8d102195a3927fa4bf76f4230d069d6038414923 [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"
Mario Limonciello92b8c542017-11-01 14:25:27 -050042#include "dell-wmi-descriptor.h"
Matthew Garrett0b3f6102009-01-09 20:17:11 +000043
44MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
Pali Rohár595773a42016-01-04 22:26:35 +010045MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
Matthew Garrett0b3f6102009-01-09 20:17:11 +000046MODULE_DESCRIPTION("Dell laptop WMI hotkeys driver");
47MODULE_LICENSE("GPL");
48
49#define DELL_EVENT_GUID "9DBB5994-A997-11DA-B012-B622A1EF5492"
50
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);
54
Andy Lutomirskibff589b2015-11-25 07:53:13 -080055struct dell_wmi_priv {
56 struct input_dev *input_dev;
Andy Lutomirski00ebbeb2017-08-01 08:37:27 -070057 u32 interface_version;
Andy Lutomirskibff589b2015-11-25 07:53:13 -080058};
59
Michał Kępieńe09c4d52016-03-04 14:09:08 +010060static int __init dmi_matched(const struct dmi_system_id *dmi)
61{
62 wmi_requires_smbios_request = 1;
63 return 1;
64}
65
66static const struct dmi_system_id dell_wmi_smbios_list[] __initconst = {
67 {
68 .callback = dmi_matched,
Michał Kępieńaaf3a5e2016-03-04 14:09:10 +010069 .ident = "Dell Inspiron M5110",
70 .matches = {
71 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
72 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
73 },
74 },
75 {
76 .callback = dmi_matched,
Michał Kępieńe09c4d52016-03-04 14:09:08 +010077 .ident = "Dell Vostro V131",
78 .matches = {
79 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
80 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
81 },
82 },
83 { }
84};
85
Mario Limonciello5cab0092009-06-10 19:40:47 +000086/*
Pali Roháre075b3c2016-06-15 21:49:13 +020087 * Keymap for WMI events of type 0x0000
88 *
Mario Limonciello5cab0092009-06-10 19:40:47 +000089 * Certain keys are flagged as KE_IGNORE. All of these are either
90 * notifications (rather than requests for change) or are also sent
91 * via the keyboard controller so should not be sent again.
92 */
Andy Lutomirskibff589b2015-11-25 07:53:13 -080093static const struct key_entry dell_wmi_keymap_type_0000[] = {
Seth Forsheef1566f02011-08-01 15:46:10 -050094 { KE_IGNORE, 0x003a, { KEY_CAPSLOCK } },
95
Pali Rohár6a4d2602016-06-15 21:49:11 +020096 /* Key code is followed by brightness level */
97 { KE_KEY, 0xe005, { KEY_BRIGHTNESSDOWN } },
98 { KE_KEY, 0xe006, { KEY_BRIGHTNESSUP } },
Mario Limonciello5cab0092009-06-10 19:40:47 +000099
100 /* Battery health status button */
Pali Rohár6a4d2602016-06-15 21:49:11 +0200101 { KE_KEY, 0xe007, { KEY_BATTERY } },
Mario Limonciello5cab0092009-06-10 19:40:47 +0000102
Pali Rohár6a4d2602016-06-15 21:49:11 +0200103 /* Radio devices state change, key code is followed by other values */
Gabriele Mazzotta8f8d75e2014-12-04 00:16:21 +0100104 { KE_IGNORE, 0xe008, { KEY_RFKILL } },
Mario Limonciello5cab0092009-06-10 19:40:47 +0000105
Pali Rohár6a4d2602016-06-15 21:49:11 +0200106 { KE_KEY, 0xe009, { KEY_EJECTCD } },
Mario Limonciello5cab0092009-06-10 19:40:47 +0000107
Pali Rohár6a4d2602016-06-15 21:49:11 +0200108 /* Key code is followed by: next, active and attached devices */
109 { KE_KEY, 0xe00b, { KEY_SWITCHVIDEOMODE } },
110
111 /* Key code is followed by keyboard illumination level */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700112 { KE_IGNORE, 0xe00c, { KEY_KBDILLUMTOGGLE } },
Mario Limonciello5cab0092009-06-10 19:40:47 +0000113
114 /* BIOS error detected */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700115 { KE_IGNORE, 0xe00d, { KEY_RESERVED } },
Mario Limonciello5cab0092009-06-10 19:40:47 +0000116
Pali Rohár65a97a62016-07-27 09:34:42 +0200117 /* Battery was removed or inserted */
118 { KE_IGNORE, 0xe00e, { KEY_RESERVED } },
Pali Rohár3237a862016-06-15 21:49:12 +0200119
Mario Limonciello5cab0092009-06-10 19:40:47 +0000120 /* Wifi Catcher */
Mario Limonciello915ac052016-07-28 12:53:47 -0500121 { KE_KEY, 0xe011, { KEY_WLAN } },
Mario Limonciello5cab0092009-06-10 19:40:47 +0000122
123 /* Ambient light sensor toggle */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700124 { KE_IGNORE, 0xe013, { KEY_RESERVED } },
Mario Limonciello5cab0092009-06-10 19:40:47 +0000125
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700126 { KE_IGNORE, 0xe020, { KEY_MUTE } },
Seth Forsheef1566f02011-08-01 15:46:10 -0500127
Pali Rohár3237a862016-06-15 21:49:12 +0200128 /* Unknown, defined in ACPI DSDT */
129 /* { KE_IGNORE, 0xe023, { KEY_RESERVED } }, */
130
131 /* Untested, Dell Instant Launch key on Inspiron 7520 */
132 /* { KE_IGNORE, 0xe024, { KEY_RESERVED } }, */
133
Michał Kępieńaaf3a5e2016-03-04 14:09:10 +0100134 /* Dell Instant Launch key */
Pali Rohár6a4d2602016-06-15 21:49:11 +0200135 { KE_KEY, 0xe025, { KEY_PROG4 } },
Michał Kępieńaaf3a5e2016-03-04 14:09:10 +0100136
137 /* Audio panel key */
Seth Forsheef1566f02011-08-01 15:46:10 -0500138 { KE_IGNORE, 0xe026, { KEY_RESERVED } },
139
Alex Hungab9bb11c2016-06-17 11:44:27 +0800140 /* LCD Display On/Off Control key */
141 { KE_KEY, 0xe027, { KEY_DISPLAYTOGGLE } },
142
Pali Rohár3237a862016-06-15 21:49:12 +0200143 /* Untested, Multimedia key on Dell Vostro 3560 */
144 /* { KE_IGNORE, 0xe028, { KEY_RESERVED } }, */
145
Pali Rohár6a4d2602016-06-15 21:49:11 +0200146 /* Dell Instant Launch key */
147 { KE_KEY, 0xe029, { KEY_PROG4 } },
148
Pali Rohár3237a862016-06-15 21:49:12 +0200149 /* Untested, Windows Mobility Center button on Inspiron 7520 */
150 /* { KE_IGNORE, 0xe02a, { KEY_RESERVED } }, */
151
152 /* Unknown, defined in ACPI DSDT */
153 /* { KE_IGNORE, 0xe02b, { KEY_RESERVED } }, */
154
155 /* Untested, Dell Audio With Preset Switch button on Inspiron 7520 */
156 /* { KE_IGNORE, 0xe02c, { KEY_RESERVED } }, */
157
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700158 { KE_IGNORE, 0xe02e, { KEY_VOLUMEDOWN } },
159 { KE_IGNORE, 0xe030, { KEY_VOLUMEUP } },
160 { KE_IGNORE, 0xe033, { KEY_KBDILLUMUP } },
161 { KE_IGNORE, 0xe034, { KEY_KBDILLUMDOWN } },
162 { KE_IGNORE, 0xe03a, { KEY_CAPSLOCK } },
Pali Rohár3de59bc2016-06-15 21:49:10 +0200163
Pali Rohár3237a862016-06-15 21:49:12 +0200164 /* NIC Link is Up */
165 { KE_IGNORE, 0xe043, { KEY_RESERVED } },
166
167 /* NIC Link is Down */
168 { KE_IGNORE, 0xe044, { KEY_RESERVED } },
169
Pali Rohár3de59bc2016-06-15 21:49:10 +0200170 /*
171 * This entry is very suspicious!
172 * Originally Matthew Garrett created this dell-wmi driver specially for
173 * "button with a picture of a battery" which has event code 0xe045.
174 * Later Mario Limonciello from Dell told us that event code 0xe045 is
175 * reported by Num Lock and should be ignored because key is send also
176 * by keyboard controller.
177 * So for now we will ignore this event to prevent potential double
178 * Num Lock key press.
179 */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700180 { KE_IGNORE, 0xe045, { KEY_NUMLOCK } },
Pali Rohár3de59bc2016-06-15 21:49:10 +0200181
Pali Rohár3237a862016-06-15 21:49:12 +0200182 /* Scroll lock and also going to tablet mode on portable devices */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700183 { KE_IGNORE, 0xe046, { KEY_SCROLLLOCK } },
Pali Rohár3237a862016-06-15 21:49:12 +0200184
185 /* Untested, going from tablet mode on portable devices */
186 /* { KE_IGNORE, 0xe047, { KEY_RESERVED } }, */
187
188 /* Dell Support Center key */
189 { KE_IGNORE, 0xe06e, { KEY_RESERVED } },
190
Seth Forsheef1566f02011-08-01 15:46:10 -0500191 { KE_IGNORE, 0xe0f7, { KEY_MUTE } },
192 { KE_IGNORE, 0xe0f8, { KEY_VOLUMEDOWN } },
193 { KE_IGNORE, 0xe0f9, { KEY_VOLUMEUP } },
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000194};
195
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700196struct dell_bios_keymap_entry {
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500197 u16 scancode;
198 u16 keycode;
199};
200
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700201struct dell_bios_hotkey_table {
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500202 struct dmi_header header;
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700203 struct dell_bios_keymap_entry keymap[];
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500204
205};
206
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800207struct dell_dmi_results {
208 int err;
Pali Roháre075b3c2016-06-15 21:49:13 +0200209 int keymap_size;
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800210 struct key_entry *keymap;
211};
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500212
Andy Lutomirskicbc61f12015-11-30 17:02:01 -0800213/* Uninitialized entries here are KEY_RESERVED == 0. */
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800214static const u16 bios_to_linux_keycode[256] = {
Andy Lutomirski394cb772015-11-13 21:49:31 -0800215 [0] = KEY_MEDIA,
216 [1] = KEY_NEXTSONG,
217 [2] = KEY_PLAYPAUSE,
218 [3] = KEY_PREVIOUSSONG,
219 [4] = KEY_STOPCD,
220 [5] = KEY_UNKNOWN,
221 [6] = KEY_UNKNOWN,
222 [7] = KEY_UNKNOWN,
223 [8] = KEY_WWW,
224 [9] = KEY_UNKNOWN,
225 [10] = KEY_VOLUMEDOWN,
226 [11] = KEY_MUTE,
227 [12] = KEY_VOLUMEUP,
228 [13] = KEY_UNKNOWN,
229 [14] = KEY_BATTERY,
230 [15] = KEY_EJECTCD,
231 [16] = KEY_UNKNOWN,
232 [17] = KEY_SLEEP,
233 [18] = KEY_PROG1,
234 [19] = KEY_BRIGHTNESSDOWN,
235 [20] = KEY_BRIGHTNESSUP,
236 [21] = KEY_UNKNOWN,
237 [22] = KEY_KBDILLUMTOGGLE,
238 [23] = KEY_UNKNOWN,
239 [24] = KEY_SWITCHVIDEOMODE,
240 [25] = KEY_UNKNOWN,
241 [26] = KEY_UNKNOWN,
242 [27] = KEY_SWITCHVIDEOMODE,
243 [28] = KEY_UNKNOWN,
244 [29] = KEY_UNKNOWN,
245 [30] = KEY_PROG2,
246 [31] = KEY_UNKNOWN,
247 [32] = KEY_UNKNOWN,
248 [33] = KEY_UNKNOWN,
249 [34] = KEY_UNKNOWN,
250 [35] = KEY_UNKNOWN,
251 [36] = KEY_UNKNOWN,
252 [37] = KEY_UNKNOWN,
253 [38] = KEY_MICMUTE,
254 [255] = KEY_PROG3,
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500255};
256
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800257/*
Pali Roháre075b3c2016-06-15 21:49:13 +0200258 * Keymap for WMI events of type 0x0010
259 *
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800260 * These are applied if the 0xB2 DMI hotkey table is present and doesn't
261 * override them.
262 */
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800263static const struct key_entry dell_wmi_keymap_type_0010[] = {
Shih-Yuan Lee (FourDollars)842b8542017-12-19 17:39:49 +0800264 /* Mic mute */
265 { KE_KEY, 0x150, { KEY_MICMUTE } },
266
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800267 /* Fn-lock */
268 { KE_IGNORE, 0x151, { KEY_RESERVED } },
269
270 /* Change keyboard illumination */
271 { KE_IGNORE, 0x152, { KEY_KBDILLUMTOGGLE } },
272
273 /*
274 * Radio disable (notify only -- there is no model for which the
275 * WMI event is supposed to trigger an action).
276 */
277 { KE_IGNORE, 0x153, { KEY_RFKILL } },
278
279 /* RGB keyboard backlight control */
280 { KE_IGNORE, 0x154, { KEY_RESERVED } },
281
Andy Lutomirskie4f2e3f2016-02-17 12:38:07 -0800282 /*
283 * Stealth mode toggle. This will "disable all lights and sounds".
284 * The action is performed by the BIOS and EC; the WMI event is just
285 * a notification. On the XPS 13 9350, this is Fn+F7, and there's
286 * a BIOS setting to enable and disable the hotkey.
287 */
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800288 { KE_IGNORE, 0x155, { KEY_RESERVED } },
Mario Limonciello5dc444b2016-07-28 12:53:48 -0500289
290 /* Rugged magnetic dock attach/detach events */
291 { KE_IGNORE, 0x156, { KEY_RESERVED } },
292 { KE_IGNORE, 0x157, { KEY_RESERVED } },
293
294 /* Rugged programmable (P1/P2/P3 keys) */
295 { KE_KEY, 0x850, { KEY_PROG1 } },
296 { KE_KEY, 0x851, { KEY_PROG2 } },
297 { KE_KEY, 0x852, { KEY_PROG3 } },
298
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800299};
300
Pali Roháre075b3c2016-06-15 21:49:13 +0200301/*
302 * Keymap for WMI events of type 0x0011
303 */
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800304static const struct key_entry dell_wmi_keymap_type_0011[] = {
Pali Roháre075b3c2016-06-15 21:49:13 +0200305 /* Battery unplugged */
306 { KE_IGNORE, 0xfff0, { KEY_RESERVED } },
307
308 /* Battery inserted */
309 { KE_IGNORE, 0xfff1, { KEY_RESERVED } },
310
311 /* Keyboard backlight level changed */
312 { KE_IGNORE, 0x01e1, { KEY_RESERVED } },
313 { KE_IGNORE, 0x02ea, { KEY_RESERVED } },
314 { KE_IGNORE, 0x02eb, { KEY_RESERVED } },
315 { KE_IGNORE, 0x02ec, { KEY_RESERVED } },
316 { KE_IGNORE, 0x02f6, { KEY_RESERVED } },
317};
318
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800319static void dell_wmi_process_key(struct wmi_device *wdev, int type, int code)
Pali Rohár83fc44c2014-11-11 20:21:22 +0100320{
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800321 struct dell_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
Pali Rohár83fc44c2014-11-11 20:21:22 +0100322 const struct key_entry *key;
323
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800324 key = sparse_keymap_entry_from_scancode(priv->input_dev,
Pali Roháre075b3c2016-06-15 21:49:13 +0200325 (type << 16) | code);
Pali Rohár83fc44c2014-11-11 20:21:22 +0100326 if (!key) {
Pali Roháre075b3c2016-06-15 21:49:13 +0200327 pr_info("Unknown key with type 0x%04x and code 0x%04x pressed\n",
328 type, code);
Pali Rohár83fc44c2014-11-11 20:21:22 +0100329 return;
330 }
331
Pali Roháre075b3c2016-06-15 21:49:13 +0200332 pr_debug("Key with type 0x%04x and code 0x%04x pressed\n", type, code);
Pali Rohár83fc44c2014-11-11 20:21:22 +0100333
334 /* Don't report brightness notifications that will also come via ACPI */
335 if ((key->keycode == KEY_BRIGHTNESSUP ||
Hans de Goede61679c72015-12-22 19:09:49 +0100336 key->keycode == KEY_BRIGHTNESSDOWN) &&
337 acpi_video_handles_brightness_key_presses())
Pali Rohár83fc44c2014-11-11 20:21:22 +0100338 return;
339
Pali Roháre075b3c2016-06-15 21:49:13 +0200340 if (type == 0x0000 && code == 0xe025 && !wmi_requires_smbios_request)
Michał Kępień13f50592016-03-04 14:09:09 +0100341 return;
342
Hans de Goede9c656b02017-03-16 11:55:35 +0100343 if (key->keycode == KEY_KBDILLUMTOGGLE)
344 dell_laptop_call_notifier(
345 DELL_LAPTOP_KBD_BACKLIGHT_BRIGHTNESS_CHANGED, NULL);
346
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800347 sparse_keymap_report_entry(priv->input_dev, key, 1, true);
Pali Rohár83fc44c2014-11-11 20:21:22 +0100348}
349
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800350static void dell_wmi_notify(struct wmi_device *wdev,
351 union acpi_object *obj)
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000352{
Andy Lutomirski00ebbeb2017-08-01 08:37:27 -0700353 struct dell_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
Pali Rohár83fc44c2014-11-11 20:21:22 +0100354 u16 *buffer_entry, *buffer_end;
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800355 acpi_size buffer_size;
Pali Rohár83fc44c2014-11-11 20:21:22 +0100356 int len, i;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000357
Pali Rohár83fc44c2014-11-11 20:21:22 +0100358 if (obj->type != ACPI_TYPE_BUFFER) {
359 pr_warn("bad response type %x\n", obj->type);
Pali Rohár83fc44c2014-11-11 20:21:22 +0100360 return;
361 }
362
363 pr_debug("Received WMI event (%*ph)\n",
364 obj->buffer.length, obj->buffer.pointer);
365
366 buffer_entry = (u16 *)obj->buffer.pointer;
367 buffer_size = obj->buffer.length/2;
Pali Rohár83fc44c2014-11-11 20:21:22 +0100368 buffer_end = buffer_entry + buffer_size;
369
Pali Rohár481fe5b2016-01-04 22:26:36 +0100370 /*
371 * BIOS/ACPI on devices with WMI interface version 0 does not clear
372 * buffer before filling it. So next time when BIOS/ACPI send WMI event
373 * which is smaller as previous then it contains garbage in buffer from
374 * previous event.
375 *
376 * BIOS/ACPI on devices with WMI interface version 1 clears buffer and
377 * sometimes send more events in buffer at one call.
378 *
379 * So to prevent reading garbage from buffer we will process only first
380 * one event on devices with WMI interface version 0.
381 */
Andy Lutomirski00ebbeb2017-08-01 08:37:27 -0700382 if (priv->interface_version == 0 && buffer_entry < buffer_end)
Pali Rohár481fe5b2016-01-04 22:26:36 +0100383 if (buffer_end > buffer_entry + buffer_entry[0] + 1)
384 buffer_end = buffer_entry + buffer_entry[0] + 1;
385
Pali Rohár83fc44c2014-11-11 20:21:22 +0100386 while (buffer_entry < buffer_end) {
387
388 len = buffer_entry[0];
389 if (len == 0)
390 break;
391
392 len++;
393
394 if (buffer_entry + len > buffer_end) {
395 pr_warn("Invalid length of WMI event\n");
396 break;
397 }
398
399 pr_debug("Process buffer (%*ph)\n", len*2, buffer_entry);
400
401 switch (buffer_entry[1]) {
Pali Roháre075b3c2016-06-15 21:49:13 +0200402 case 0x0000: /* One key pressed or event occurred */
403 if (len > 2)
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800404 dell_wmi_process_key(wdev, 0x0000,
405 buffer_entry[2]);
Pali Roháre075b3c2016-06-15 21:49:13 +0200406 /* Other entries could contain additional information */
Pali Rohár83fc44c2014-11-11 20:21:22 +0100407 break;
Pali Roháre075b3c2016-06-15 21:49:13 +0200408 case 0x0010: /* Sequence of keys pressed */
409 case 0x0011: /* Sequence of events occurred */
Pali Rohár83fc44c2014-11-11 20:21:22 +0100410 for (i = 2; i < len; ++i)
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800411 dell_wmi_process_key(wdev, buffer_entry[1],
Pali Roháre075b3c2016-06-15 21:49:13 +0200412 buffer_entry[i]);
Pali Rohár83fc44c2014-11-11 20:21:22 +0100413 break;
Pali Roháre075b3c2016-06-15 21:49:13 +0200414 default: /* Unknown event */
Pali Rohár83fc44c2014-11-11 20:21:22 +0100415 pr_info("Unknown WMI event type 0x%x\n",
416 (int)buffer_entry[1]);
417 break;
418 }
419
420 buffer_entry += len;
421
422 }
423
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000424}
425
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800426static bool have_scancode(u32 scancode, const struct key_entry *keymap, int len)
427{
428 int i;
429
430 for (i = 0; i < len; i++)
431 if (keymap[i].code == scancode)
432 return true;
433
434 return false;
435}
436
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800437static void handle_dmi_entry(const struct dmi_header *dm, void *opaque)
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500438{
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800439 struct dell_dmi_results *results = opaque;
440 struct dell_bios_hotkey_table *table;
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800441 int hotkey_num, i, pos = 0;
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700442 struct key_entry *keymap;
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800443
444 if (results->err || results->keymap)
445 return; /* We already found the hotkey table. */
446
Andy Lutomirski074df512016-02-17 12:34:34 -0800447 /* The Dell hotkey table is type 0xB2. Scan until we find it. */
Andy Lutomirskib13de702016-02-15 08:32:35 -0800448 if (dm->type != 0xb2)
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800449 return;
450
451 table = container_of(dm, struct dell_bios_hotkey_table, header);
452
Andy Lutomirskib13de702016-02-15 08:32:35 -0800453 hotkey_num = (table->header.length -
454 sizeof(struct dell_bios_hotkey_table)) /
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800455 sizeof(struct dell_bios_keymap_entry);
Andy Lutomirskib13de702016-02-15 08:32:35 -0800456 if (hotkey_num < 1) {
457 /*
458 * Historically, dell-wmi would ignore a DMI entry of
459 * fewer than 7 bytes. Sizes between 4 and 8 bytes are
460 * nonsensical (both the header and all entries are 4
461 * bytes), so we approximate the old behavior by
462 * ignoring tables with fewer than one entry.
463 */
464 return;
465 }
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500466
Pali Roháre075b3c2016-06-15 21:49:13 +0200467 keymap = kcalloc(hotkey_num, sizeof(struct key_entry), GFP_KERNEL);
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800468 if (!keymap) {
469 results->err = -ENOMEM;
470 return;
471 }
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500472
473 for (i = 0; i < hotkey_num; i++) {
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700474 const struct dell_bios_keymap_entry *bios_entry =
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800475 &table->keymap[i];
Andy Lutomirskicbc61f12015-11-30 17:02:01 -0800476
477 /* Uninitialized entries are 0 aka KEY_RESERVED. */
478 u16 keycode = (bios_entry->keycode <
479 ARRAY_SIZE(bios_to_linux_keycode)) ?
480 bios_to_linux_keycode[bios_entry->keycode] :
481 KEY_RESERVED;
482
483 /*
484 * Log if we find an entry in the DMI table that we don't
485 * understand. If this happens, we should figure out what
486 * the entry means and add it to bios_to_linux_keycode.
487 */
488 if (keycode == KEY_RESERVED) {
489 pr_info("firmware scancode 0x%x maps to unrecognized keycode 0x%x\n",
490 bios_entry->scancode, bios_entry->keycode);
491 continue;
492 }
Gabriele Mazzotta8cb8e632014-12-04 00:16:23 +0100493
494 if (keycode == KEY_KBDILLUMTOGGLE)
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800495 keymap[pos].type = KE_IGNORE;
Gabriele Mazzotta8cb8e632014-12-04 00:16:23 +0100496 else
Andy Lutomirskia464afb2016-02-15 08:32:36 -0800497 keymap[pos].type = KE_KEY;
498 keymap[pos].code = bios_entry->scancode;
499 keymap[pos].keycode = keycode;
500
501 pos++;
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500502 }
503
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800504 results->keymap = keymap;
Pali Roháre075b3c2016-06-15 21:49:13 +0200505 results->keymap_size = pos;
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500506}
507
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800508static int dell_wmi_input_setup(struct wmi_device *wdev)
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000509{
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800510 struct dell_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800511 struct dell_dmi_results dmi_results = {};
Pali Roháre075b3c2016-06-15 21:49:13 +0200512 struct key_entry *keymap;
513 int err, i, pos = 0;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000514
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800515 priv->input_dev = input_allocate_device();
516 if (!priv->input_dev)
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000517 return -ENOMEM;
518
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800519 priv->input_dev->name = "Dell WMI hotkeys";
520 priv->input_dev->id.bustype = BUS_HOST;
521 priv->input_dev->dev.parent = &wdev->dev;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000522
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800523 if (dmi_walk(handle_dmi_entry, &dmi_results)) {
524 /*
525 * Historically, dell-wmi ignored dmi_walk errors. A failure
526 * is certainly surprising, but it probably just indicates
527 * a very old laptop.
528 */
529 pr_warn("no DMI; using the old-style hotkey interface\n");
530 }
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700531
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800532 if (dmi_results.err) {
533 err = dmi_results.err;
534 goto err_free_dev;
535 }
536
Pali Roháre075b3c2016-06-15 21:49:13 +0200537 keymap = kcalloc(dmi_results.keymap_size +
538 ARRAY_SIZE(dell_wmi_keymap_type_0000) +
539 ARRAY_SIZE(dell_wmi_keymap_type_0010) +
540 ARRAY_SIZE(dell_wmi_keymap_type_0011) +
541 1,
542 sizeof(struct key_entry), GFP_KERNEL);
543 if (!keymap) {
544 kfree(dmi_results.keymap);
545 err = -ENOMEM;
546 goto err_free_dev;
547 }
Andy Lutomirski18b6f802016-02-15 08:32:33 -0800548
Pali Roháre075b3c2016-06-15 21:49:13 +0200549 /* Append table with events of type 0x0010 which comes from DMI */
550 for (i = 0; i < dmi_results.keymap_size; i++) {
551 keymap[pos] = dmi_results.keymap[i];
552 keymap[pos].code |= (0x0010 << 16);
553 pos++;
554 }
555
556 kfree(dmi_results.keymap);
557
558 /* Append table with extra events of type 0x0010 which are not in DMI */
559 for (i = 0; i < ARRAY_SIZE(dell_wmi_keymap_type_0010); i++) {
560 const struct key_entry *entry = &dell_wmi_keymap_type_0010[i];
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700561
562 /*
Pali Roháre075b3c2016-06-15 21:49:13 +0200563 * Check if we've already found this scancode. This takes
564 * quadratic time, but it doesn't matter unless the list
565 * of extra keys gets very long.
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700566 */
Pali Roháre075b3c2016-06-15 21:49:13 +0200567 if (dmi_results.keymap_size &&
568 have_scancode(entry->code | (0x0010 << 16),
569 keymap, dmi_results.keymap_size)
570 )
571 continue;
572
573 keymap[pos] = *entry;
574 keymap[pos].code |= (0x0010 << 16);
575 pos++;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000576 }
Pali Roháre075b3c2016-06-15 21:49:13 +0200577
578 /* Append table with events of type 0x0011 */
579 for (i = 0; i < ARRAY_SIZE(dell_wmi_keymap_type_0011); i++) {
580 keymap[pos] = dell_wmi_keymap_type_0011[i];
581 keymap[pos].code |= (0x0011 << 16);
582 pos++;
583 }
584
585 /*
586 * Now append also table with "legacy" events of type 0x0000. Some of
587 * them are reported also on laptops which have scancodes in DMI.
588 */
589 for (i = 0; i < ARRAY_SIZE(dell_wmi_keymap_type_0000); i++) {
590 keymap[pos] = dell_wmi_keymap_type_0000[i];
591 pos++;
592 }
593
594 keymap[pos].type = KE_END;
595
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800596 err = sparse_keymap_setup(priv->input_dev, keymap, NULL);
Pali Roháre075b3c2016-06-15 21:49:13 +0200597 /*
598 * Sparse keymap library makes a copy of keymap so we don't need the
599 * original one that was allocated.
600 */
601 kfree(keymap);
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700602 if (err)
603 goto err_free_dev;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000604
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800605 err = input_register_device(priv->input_dev);
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700606 if (err)
Michał Kępień815edfe2017-03-09 13:11:41 +0100607 goto err_free_dev;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000608
609 return 0;
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700610
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700611 err_free_dev:
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800612 input_free_device(priv->input_dev);
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700613 return err;
614}
615
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800616static void dell_wmi_input_destroy(struct wmi_device *wdev)
617{
618 struct dell_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
619
620 input_unregister_device(priv->input_dev);
621}
622
Pali Rohár595773a42016-01-04 22:26:35 +0100623/*
Michał Kępieńe09c4d52016-03-04 14:09:08 +0100624 * 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
Mario Limonciello549b4932017-11-01 14:25:31 -0500644 buffer = kzalloc(sizeof(struct calling_interface_buffer), GFP_KERNEL);
Dan Carpenter91c73e82017-12-11 13:54:27 +0300645 if (!buffer)
646 return -ENOMEM;
Mario Limonciello549b4932017-11-01 14:25:31 -0500647 buffer->cmd_class = CLASS_INFO;
648 buffer->cmd_select = SELECT_APP_REGISTRATION;
Michał Kępieńe09c4d52016-03-04 14:09:08 +0100649 buffer->input[0] = 0x10000;
650 buffer->input[1] = 0x51534554;
651 buffer->input[3] = enable;
Mario Limonciello549b4932017-11-01 14:25:31 -0500652 ret = dell_smbios_call(buffer);
653 if (ret == 0)
654 ret = buffer->output[0];
655 kfree(buffer);
Michał Kępieńe09c4d52016-03-04 14:09:08 +0100656
657 return dell_smbios_error(ret);
658}
659
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800660static int dell_wmi_probe(struct wmi_device *wdev)
661{
Andy Lutomirski00ebbeb2017-08-01 08:37:27 -0700662 struct dell_wmi_priv *priv;
Mario Limonciello868b8d32017-11-09 11:49:10 -0600663 int ret;
Mario Limonciello92b8c542017-11-01 14:25:27 -0500664
Mario Limonciello868b8d32017-11-09 11:49:10 -0600665 ret = dell_wmi_get_descriptor_valid();
666 if (ret)
667 return ret;
Andy Lutomirskibaa54802017-08-01 08:37:26 -0700668
Andy Lutomirski00ebbeb2017-08-01 08:37:27 -0700669 priv = devm_kzalloc(
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800670 &wdev->dev, sizeof(struct dell_wmi_priv), GFP_KERNEL);
Andy Lutomirski00ebbeb2017-08-01 08:37:27 -0700671 if (!priv)
672 return -ENOMEM;
673 dev_set_drvdata(&wdev->dev, priv);
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800674
Mario Limonciello92b8c542017-11-01 14:25:27 -0500675 if (!dell_wmi_get_interface_version(&priv->interface_version))
676 return -EPROBE_DEFER;
Andy Lutomirskibaa54802017-08-01 08:37:26 -0700677
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800678 return dell_wmi_input_setup(wdev);
679}
680
681static int dell_wmi_remove(struct wmi_device *wdev)
682{
683 dell_wmi_input_destroy(wdev);
684 return 0;
685}
686static const struct wmi_device_id dell_wmi_id_table[] = {
687 { .guid_string = DELL_EVENT_GUID },
688 { },
689};
690
691static struct wmi_driver dell_wmi_driver = {
692 .driver = {
693 .name = "dell-wmi",
694 },
695 .id_table = dell_wmi_id_table,
696 .probe = dell_wmi_probe,
697 .remove = dell_wmi_remove,
698 .notify = dell_wmi_notify,
699};
700
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000701static int __init dell_wmi_init(void)
702{
703 int err;
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800704
Michał Kępieńe09c4d52016-03-04 14:09:08 +0100705 dmi_check_system(dell_wmi_smbios_list);
706
707 if (wmi_requires_smbios_request) {
708 err = dell_wmi_events_set_enabled(true);
709 if (err) {
710 pr_err("Failed to enable WMI events\n");
Michał Kępieńe09c4d52016-03-04 14:09:08 +0100711 return err;
712 }
713 }
714
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800715 return wmi_driver_register(&dell_wmi_driver);
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000716}
Darren Hart (VMware)49368c12018-03-12 23:28:00 -0700717late_initcall(dell_wmi_init);
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000718
719static void __exit dell_wmi_exit(void)
720{
Michał Kępieńe09c4d52016-03-04 14:09:08 +0100721 if (wmi_requires_smbios_request)
722 dell_wmi_events_set_enabled(false);
Andy Lutomirskibff589b2015-11-25 07:53:13 -0800723
724 wmi_driver_unregister(&dell_wmi_driver);
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000725}
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000726module_exit(dell_wmi_exit);