1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 2008 Red Hat <mjg@redhat.com>
6 * Copyright (C) 2014-2015 Pali Rohár <pali.rohar@gmail.com>
8 * Portions based on wistron_btns.c:
9 * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
10 * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
11 * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include <linux/types.h>
21 #include <linux/input.h>
22 #include <linux/input/sparse-keymap.h>
23 #include <linux/acpi.h>
24 #include <linux/string.h>
25 #include <linux/dmi.h>
26 #include <linux/wmi.h>
27 #include <acpi/video.h>
28 #include "dell-smbios.h"
29 #include "dell-wmi-descriptor.h"
31 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
32 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
33 MODULE_DESCRIPTION("Dell laptop WMI hotkeys driver");
34 MODULE_LICENSE("GPL");
36 #define DELL_EVENT_GUID "9DBB5994-A997-11DA-B012-B622A1EF5492"
38 static bool wmi_requires_smbios_request;
40 struct dell_wmi_priv {
41 struct input_dev *input_dev;
42 u32 interface_version;
45 static int __init dmi_matched(const struct dmi_system_id *dmi)
47 wmi_requires_smbios_request = 1;
51 static const struct dmi_system_id dell_wmi_smbios_list[] __initconst = {
53 .callback = dmi_matched,
54 .ident = "Dell Inspiron M5110",
56 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
57 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
61 .callback = dmi_matched,
62 .ident = "Dell Vostro V131",
64 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
65 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
72 * Keymap for WMI events of type 0x0000
74 * Certain keys are flagged as KE_IGNORE. All of these are either
75 * notifications (rather than requests for change) or are also sent
76 * via the keyboard controller so should not be sent again.
78 static const struct key_entry dell_wmi_keymap_type_0000[] = {
79 { KE_IGNORE, 0x003a, { KEY_CAPSLOCK } },
81 /* Key code is followed by brightness level */
82 { KE_KEY, 0xe005, { KEY_BRIGHTNESSDOWN } },
83 { KE_KEY, 0xe006, { KEY_BRIGHTNESSUP } },
85 /* Battery health status button */
86 { KE_KEY, 0xe007, { KEY_BATTERY } },
88 /* Radio devices state change, key code is followed by other values */
89 { KE_IGNORE, 0xe008, { KEY_RFKILL } },
91 { KE_KEY, 0xe009, { KEY_EJECTCD } },
93 /* Key code is followed by: next, active and attached devices */
94 { KE_KEY, 0xe00b, { KEY_SWITCHVIDEOMODE } },
96 /* Key code is followed by keyboard illumination level */
97 { KE_IGNORE, 0xe00c, { KEY_KBDILLUMTOGGLE } },
99 /* BIOS error detected */
100 { KE_IGNORE, 0xe00d, { KEY_RESERVED } },
102 /* Battery was removed or inserted */
103 { KE_IGNORE, 0xe00e, { KEY_RESERVED } },
106 { KE_KEY, 0xe011, { KEY_WLAN } },
108 /* Ambient light sensor toggle */
109 { KE_IGNORE, 0xe013, { KEY_RESERVED } },
111 { KE_IGNORE, 0xe020, { KEY_MUTE } },
113 /* Unknown, defined in ACPI DSDT */
114 /* { KE_IGNORE, 0xe023, { KEY_RESERVED } }, */
116 /* Untested, Dell Instant Launch key on Inspiron 7520 */
117 /* { KE_IGNORE, 0xe024, { KEY_RESERVED } }, */
119 /* Dell Instant Launch key */
120 { KE_KEY, 0xe025, { KEY_PROG4 } },
122 /* Audio panel key */
123 { KE_IGNORE, 0xe026, { KEY_RESERVED } },
125 /* LCD Display On/Off Control key */
126 { KE_KEY, 0xe027, { KEY_DISPLAYTOGGLE } },
128 /* Untested, Multimedia key on Dell Vostro 3560 */
129 /* { KE_IGNORE, 0xe028, { KEY_RESERVED } }, */
131 /* Dell Instant Launch key */
132 { KE_KEY, 0xe029, { KEY_PROG4 } },
134 /* Untested, Windows Mobility Center button on Inspiron 7520 */
135 /* { KE_IGNORE, 0xe02a, { KEY_RESERVED } }, */
137 /* Unknown, defined in ACPI DSDT */
138 /* { KE_IGNORE, 0xe02b, { KEY_RESERVED } }, */
140 /* Untested, Dell Audio With Preset Switch button on Inspiron 7520 */
141 /* { KE_IGNORE, 0xe02c, { KEY_RESERVED } }, */
143 { KE_IGNORE, 0xe02e, { KEY_VOLUMEDOWN } },
144 { KE_IGNORE, 0xe030, { KEY_VOLUMEUP } },
145 { KE_IGNORE, 0xe033, { KEY_KBDILLUMUP } },
146 { KE_IGNORE, 0xe034, { KEY_KBDILLUMDOWN } },
147 { KE_IGNORE, 0xe03a, { KEY_CAPSLOCK } },
150 { KE_IGNORE, 0xe043, { KEY_RESERVED } },
152 /* NIC Link is Down */
153 { KE_IGNORE, 0xe044, { KEY_RESERVED } },
156 * This entry is very suspicious!
157 * Originally Matthew Garrett created this dell-wmi driver specially for
158 * "button with a picture of a battery" which has event code 0xe045.
159 * Later Mario Limonciello from Dell told us that event code 0xe045 is
160 * reported by Num Lock and should be ignored because key is send also
161 * by keyboard controller.
162 * So for now we will ignore this event to prevent potential double
163 * Num Lock key press.
165 { KE_IGNORE, 0xe045, { KEY_NUMLOCK } },
167 /* Scroll lock and also going to tablet mode on portable devices */
168 { KE_IGNORE, 0xe046, { KEY_SCROLLLOCK } },
170 /* Untested, going from tablet mode on portable devices */
171 /* { KE_IGNORE, 0xe047, { KEY_RESERVED } }, */
173 /* Dell Support Center key */
174 { KE_IGNORE, 0xe06e, { KEY_RESERVED } },
176 { KE_IGNORE, 0xe0f7, { KEY_MUTE } },
177 { KE_IGNORE, 0xe0f8, { KEY_VOLUMEDOWN } },
178 { KE_IGNORE, 0xe0f9, { KEY_VOLUMEUP } },
181 struct dell_bios_keymap_entry {
186 struct dell_bios_hotkey_table {
187 struct dmi_header header;
188 struct dell_bios_keymap_entry keymap[];
192 struct dell_dmi_results {
195 struct key_entry *keymap;
198 /* Uninitialized entries here are KEY_RESERVED == 0. */
199 static const u16 bios_to_linux_keycode[256] = {
203 [3] = KEY_PREVIOUSSONG,
210 [10] = KEY_VOLUMEDOWN,
219 [19] = KEY_BRIGHTNESSDOWN,
220 [20] = KEY_BRIGHTNESSUP,
221 [21] = KEY_BRIGHTNESS_AUTO,
222 [22] = KEY_KBDILLUMTOGGLE,
224 [24] = KEY_SWITCHVIDEOMODE,
227 [27] = KEY_SWITCHVIDEOMODE,
243 * Keymap for WMI events of type 0x0010
245 * These are applied if the 0xB2 DMI hotkey table is present and doesn't
248 static const struct key_entry dell_wmi_keymap_type_0010[] = {
249 /* Fn-lock switched to function keys */
250 { KE_IGNORE, 0x0, { KEY_RESERVED } },
252 /* Fn-lock switched to multimedia keys */
253 { KE_IGNORE, 0x1, { KEY_RESERVED } },
255 /* Keyboard backlight change notification */
256 { KE_IGNORE, 0x3f, { KEY_RESERVED } },
259 { KE_KEY, 0x150, { KEY_MICMUTE } },
262 { KE_IGNORE, 0x151, { KEY_RESERVED } },
264 /* Change keyboard illumination */
265 { KE_IGNORE, 0x152, { KEY_KBDILLUMTOGGLE } },
268 * Radio disable (notify only -- there is no model for which the
269 * WMI event is supposed to trigger an action).
271 { KE_IGNORE, 0x153, { KEY_RFKILL } },
273 /* RGB keyboard backlight control */
274 { KE_IGNORE, 0x154, { KEY_RESERVED } },
277 * Stealth mode toggle. This will "disable all lights and sounds".
278 * The action is performed by the BIOS and EC; the WMI event is just
279 * a notification. On the XPS 13 9350, this is Fn+F7, and there's
280 * a BIOS setting to enable and disable the hotkey.
282 { KE_IGNORE, 0x155, { KEY_RESERVED } },
284 /* Rugged magnetic dock attach/detach events */
285 { KE_IGNORE, 0x156, { KEY_RESERVED } },
286 { KE_IGNORE, 0x157, { KEY_RESERVED } },
288 /* Rugged programmable (P1/P2/P3 keys) */
289 { KE_KEY, 0x850, { KEY_PROG1 } },
290 { KE_KEY, 0x851, { KEY_PROG2 } },
291 { KE_KEY, 0x852, { KEY_PROG3 } },
294 * Radio disable (notify only -- there is no model for which the
295 * WMI event is supposed to trigger an action).
297 { KE_IGNORE, 0xe008, { KEY_RFKILL } },
300 { KE_IGNORE, 0xe035, { KEY_RESERVED } },
304 * Keymap for WMI events of type 0x0011
306 static const struct key_entry dell_wmi_keymap_type_0011[] = {
307 /* Battery unplugged */
308 { KE_IGNORE, 0xfff0, { KEY_RESERVED } },
310 /* Battery inserted */
311 { KE_IGNORE, 0xfff1, { KEY_RESERVED } },
313 /* Keyboard backlight level changed */
314 { KE_IGNORE, 0x01e1, { KEY_RESERVED } },
315 { KE_IGNORE, 0x02ea, { KEY_RESERVED } },
316 { KE_IGNORE, 0x02eb, { KEY_RESERVED } },
317 { KE_IGNORE, 0x02ec, { KEY_RESERVED } },
318 { KE_IGNORE, 0x02f6, { KEY_RESERVED } },
321 static void dell_wmi_process_key(struct wmi_device *wdev, int type, int code)
323 struct dell_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
324 const struct key_entry *key;
326 key = sparse_keymap_entry_from_scancode(priv->input_dev,
327 (type << 16) | code);
329 pr_info("Unknown key with type 0x%04x and code 0x%04x pressed\n",
334 pr_debug("Key with type 0x%04x and code 0x%04x pressed\n", type, code);
336 /* Don't report brightness notifications that will also come via ACPI */
337 if ((key->keycode == KEY_BRIGHTNESSUP ||
338 key->keycode == KEY_BRIGHTNESSDOWN) &&
339 acpi_video_handles_brightness_key_presses())
342 if (type == 0x0000 && code == 0xe025 && !wmi_requires_smbios_request)
345 if (key->keycode == KEY_KBDILLUMTOGGLE)
346 dell_laptop_call_notifier(
347 DELL_LAPTOP_KBD_BACKLIGHT_BRIGHTNESS_CHANGED, NULL);
349 sparse_keymap_report_entry(priv->input_dev, key, 1, true);
352 static void dell_wmi_notify(struct wmi_device *wdev,
353 union acpi_object *obj)
355 struct dell_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
356 u16 *buffer_entry, *buffer_end;
357 acpi_size buffer_size;
360 if (obj->type != ACPI_TYPE_BUFFER) {
361 pr_warn("bad response type %x\n", obj->type);
365 pr_debug("Received WMI event (%*ph)\n",
366 obj->buffer.length, obj->buffer.pointer);
368 buffer_entry = (u16 *)obj->buffer.pointer;
369 buffer_size = obj->buffer.length/2;
370 buffer_end = buffer_entry + buffer_size;
373 * BIOS/ACPI on devices with WMI interface version 0 does not clear
374 * buffer before filling it. So next time when BIOS/ACPI send WMI event
375 * which is smaller as previous then it contains garbage in buffer from
378 * BIOS/ACPI on devices with WMI interface version 1 clears buffer and
379 * sometimes send more events in buffer at one call.
381 * So to prevent reading garbage from buffer we will process only first
382 * one event on devices with WMI interface version 0.
384 if (priv->interface_version == 0 && buffer_entry < buffer_end)
385 if (buffer_end > buffer_entry + buffer_entry[0] + 1)
386 buffer_end = buffer_entry + buffer_entry[0] + 1;
388 while (buffer_entry < buffer_end) {
390 len = buffer_entry[0];
396 if (buffer_entry + len > buffer_end) {
397 pr_warn("Invalid length of WMI event\n");
401 pr_debug("Process buffer (%*ph)\n", len*2, buffer_entry);
403 switch (buffer_entry[1]) {
404 case 0x0000: /* One key pressed or event occurred */
406 dell_wmi_process_key(wdev, 0x0000,
408 /* Other entries could contain additional information */
410 case 0x0010: /* Sequence of keys pressed */
411 case 0x0011: /* Sequence of events occurred */
412 for (i = 2; i < len; ++i)
413 dell_wmi_process_key(wdev, buffer_entry[1],
416 default: /* Unknown event */
417 pr_info("Unknown WMI event type 0x%x\n",
418 (int)buffer_entry[1]);
428 static bool have_scancode(u32 scancode, const struct key_entry *keymap, int len)
432 for (i = 0; i < len; i++)
433 if (keymap[i].code == scancode)
439 static void handle_dmi_entry(const struct dmi_header *dm, void *opaque)
441 struct dell_dmi_results *results = opaque;
442 struct dell_bios_hotkey_table *table;
443 int hotkey_num, i, pos = 0;
444 struct key_entry *keymap;
446 if (results->err || results->keymap)
447 return; /* We already found the hotkey table. */
449 /* The Dell hotkey table is type 0xB2. Scan until we find it. */
450 if (dm->type != 0xb2)
453 table = container_of(dm, struct dell_bios_hotkey_table, header);
455 hotkey_num = (table->header.length -
456 sizeof(struct dell_bios_hotkey_table)) /
457 sizeof(struct dell_bios_keymap_entry);
458 if (hotkey_num < 1) {
460 * Historically, dell-wmi would ignore a DMI entry of
461 * fewer than 7 bytes. Sizes between 4 and 8 bytes are
462 * nonsensical (both the header and all entries are 4
463 * bytes), so we approximate the old behavior by
464 * ignoring tables with fewer than one entry.
469 keymap = kcalloc(hotkey_num, sizeof(struct key_entry), GFP_KERNEL);
471 results->err = -ENOMEM;
475 for (i = 0; i < hotkey_num; i++) {
476 const struct dell_bios_keymap_entry *bios_entry =
479 /* Uninitialized entries are 0 aka KEY_RESERVED. */
480 u16 keycode = (bios_entry->keycode <
481 ARRAY_SIZE(bios_to_linux_keycode)) ?
482 bios_to_linux_keycode[bios_entry->keycode] :
486 * Log if we find an entry in the DMI table that we don't
487 * understand. If this happens, we should figure out what
488 * the entry means and add it to bios_to_linux_keycode.
490 if (keycode == KEY_RESERVED) {
491 pr_info("firmware scancode 0x%x maps to unrecognized keycode 0x%x\n",
492 bios_entry->scancode, bios_entry->keycode);
496 if (keycode == KEY_KBDILLUMTOGGLE)
497 keymap[pos].type = KE_IGNORE;
499 keymap[pos].type = KE_KEY;
500 keymap[pos].code = bios_entry->scancode;
501 keymap[pos].keycode = keycode;
506 results->keymap = keymap;
507 results->keymap_size = pos;
510 static int dell_wmi_input_setup(struct wmi_device *wdev)
512 struct dell_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
513 struct dell_dmi_results dmi_results = {};
514 struct key_entry *keymap;
517 priv->input_dev = input_allocate_device();
518 if (!priv->input_dev)
521 priv->input_dev->name = "Dell WMI hotkeys";
522 priv->input_dev->id.bustype = BUS_HOST;
523 priv->input_dev->dev.parent = &wdev->dev;
525 if (dmi_walk(handle_dmi_entry, &dmi_results)) {
527 * Historically, dell-wmi ignored dmi_walk errors. A failure
528 * is certainly surprising, but it probably just indicates
531 pr_warn("no DMI; using the old-style hotkey interface\n");
534 if (dmi_results.err) {
535 err = dmi_results.err;
539 keymap = kcalloc(dmi_results.keymap_size +
540 ARRAY_SIZE(dell_wmi_keymap_type_0000) +
541 ARRAY_SIZE(dell_wmi_keymap_type_0010) +
542 ARRAY_SIZE(dell_wmi_keymap_type_0011) +
544 sizeof(struct key_entry), GFP_KERNEL);
546 kfree(dmi_results.keymap);
551 /* Append table with events of type 0x0010 which comes from DMI */
552 for (i = 0; i < dmi_results.keymap_size; i++) {
553 keymap[pos] = dmi_results.keymap[i];
554 keymap[pos].code |= (0x0010 << 16);
558 kfree(dmi_results.keymap);
560 /* Append table with extra events of type 0x0010 which are not in DMI */
561 for (i = 0; i < ARRAY_SIZE(dell_wmi_keymap_type_0010); i++) {
562 const struct key_entry *entry = &dell_wmi_keymap_type_0010[i];
565 * Check if we've already found this scancode. This takes
566 * quadratic time, but it doesn't matter unless the list
567 * of extra keys gets very long.
569 if (dmi_results.keymap_size &&
570 have_scancode(entry->code | (0x0010 << 16),
571 keymap, dmi_results.keymap_size)
575 keymap[pos] = *entry;
576 keymap[pos].code |= (0x0010 << 16);
580 /* Append table with events of type 0x0011 */
581 for (i = 0; i < ARRAY_SIZE(dell_wmi_keymap_type_0011); i++) {
582 keymap[pos] = dell_wmi_keymap_type_0011[i];
583 keymap[pos].code |= (0x0011 << 16);
588 * Now append also table with "legacy" events of type 0x0000. Some of
589 * them are reported also on laptops which have scancodes in DMI.
591 for (i = 0; i < ARRAY_SIZE(dell_wmi_keymap_type_0000); i++) {
592 keymap[pos] = dell_wmi_keymap_type_0000[i];
596 keymap[pos].type = KE_END;
598 err = sparse_keymap_setup(priv->input_dev, keymap, NULL);
600 * Sparse keymap library makes a copy of keymap so we don't need the
601 * original one that was allocated.
607 err = input_register_device(priv->input_dev);
614 input_free_device(priv->input_dev);
618 static void dell_wmi_input_destroy(struct wmi_device *wdev)
620 struct dell_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
622 input_unregister_device(priv->input_dev);
626 * According to Dell SMBIOS documentation:
628 * 17 3 Application Program Registration
630 * cbArg1 Application ID 1 = 0x00010000
631 * cbArg2 Application ID 2
632 * QUICKSET/DCP = 0x51534554 "QSET"
633 * ALS Driver = 0x416c7353 "AlsS"
634 * Latitude ON = 0x4c6f6e52 "LonR"
635 * cbArg3 Application version or revision number
636 * cbArg4 0 = Unregister application
637 * 1 = Register application
638 * cbRes1 Standard return codes (0, -1, -2)
641 static int dell_wmi_events_set_enabled(bool enable)
643 struct calling_interface_buffer *buffer;
646 buffer = kzalloc(sizeof(struct calling_interface_buffer), GFP_KERNEL);
649 buffer->cmd_class = CLASS_INFO;
650 buffer->cmd_select = SELECT_APP_REGISTRATION;
651 buffer->input[0] = 0x10000;
652 buffer->input[1] = 0x51534554;
653 buffer->input[3] = enable;
654 ret = dell_smbios_call(buffer);
656 ret = buffer->output[0];
659 return dell_smbios_error(ret);
662 static int dell_wmi_probe(struct wmi_device *wdev, const void *context)
664 struct dell_wmi_priv *priv;
667 ret = dell_wmi_get_descriptor_valid();
672 &wdev->dev, sizeof(struct dell_wmi_priv), GFP_KERNEL);
675 dev_set_drvdata(&wdev->dev, priv);
677 if (!dell_wmi_get_interface_version(&priv->interface_version))
678 return -EPROBE_DEFER;
680 return dell_wmi_input_setup(wdev);
683 static int dell_wmi_remove(struct wmi_device *wdev)
685 dell_wmi_input_destroy(wdev);
688 static const struct wmi_device_id dell_wmi_id_table[] = {
689 { .guid_string = DELL_EVENT_GUID },
693 static struct wmi_driver dell_wmi_driver = {
697 .id_table = dell_wmi_id_table,
698 .probe = dell_wmi_probe,
699 .remove = dell_wmi_remove,
700 .notify = dell_wmi_notify,
703 static int __init dell_wmi_init(void)
707 dmi_check_system(dell_wmi_smbios_list);
709 if (wmi_requires_smbios_request) {
710 err = dell_wmi_events_set_enabled(true);
712 pr_err("Failed to enable WMI events\n");
717 return wmi_driver_register(&dell_wmi_driver);
719 late_initcall(dell_wmi_init);
721 static void __exit dell_wmi_exit(void)
723 if (wmi_requires_smbios_request)
724 dell_wmi_events_set_enabled(false);
726 wmi_driver_unregister(&dell_wmi_driver);
728 module_exit(dell_wmi_exit);
730 MODULE_DEVICE_TABLE(wmi, dell_wmi_id_table);