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
, KBD_LED_OFF_TOKEN
, { KEY_RESERVED
} },
315 { KE_IGNORE
, KBD_LED_ON_TOKEN
, { KEY_RESERVED
} },
316 { KE_IGNORE
, KBD_LED_AUTO_TOKEN
, { KEY_RESERVED
} },
317 { KE_IGNORE
, KBD_LED_AUTO_25_TOKEN
, { KEY_RESERVED
} },
318 { KE_IGNORE
, KBD_LED_AUTO_50_TOKEN
, { KEY_RESERVED
} },
319 { KE_IGNORE
, KBD_LED_AUTO_75_TOKEN
, { KEY_RESERVED
} },
320 { KE_IGNORE
, KBD_LED_AUTO_100_TOKEN
, { KEY_RESERVED
} },
323 static void dell_wmi_process_key(struct wmi_device
*wdev
, int type
, int code
)
325 struct dell_wmi_priv
*priv
= dev_get_drvdata(&wdev
->dev
);
326 const struct key_entry
*key
;
328 key
= sparse_keymap_entry_from_scancode(priv
->input_dev
,
329 (type
<< 16) | code
);
331 pr_info("Unknown key with type 0x%04x and code 0x%04x pressed\n",
336 pr_debug("Key with type 0x%04x and code 0x%04x pressed\n", type
, code
);
338 /* Don't report brightness notifications that will also come via ACPI */
339 if ((key
->keycode
== KEY_BRIGHTNESSUP
||
340 key
->keycode
== KEY_BRIGHTNESSDOWN
) &&
341 acpi_video_handles_brightness_key_presses())
344 if (type
== 0x0000 && code
== 0xe025 && !wmi_requires_smbios_request
)
347 if (key
->keycode
== KEY_KBDILLUMTOGGLE
)
348 dell_laptop_call_notifier(
349 DELL_LAPTOP_KBD_BACKLIGHT_BRIGHTNESS_CHANGED
, NULL
);
351 sparse_keymap_report_entry(priv
->input_dev
, key
, 1, true);
354 static void dell_wmi_notify(struct wmi_device
*wdev
,
355 union acpi_object
*obj
)
357 struct dell_wmi_priv
*priv
= dev_get_drvdata(&wdev
->dev
);
358 u16
*buffer_entry
, *buffer_end
;
359 acpi_size buffer_size
;
362 if (obj
->type
!= ACPI_TYPE_BUFFER
) {
363 pr_warn("bad response type %x\n", obj
->type
);
367 pr_debug("Received WMI event (%*ph)\n",
368 obj
->buffer
.length
, obj
->buffer
.pointer
);
370 buffer_entry
= (u16
*)obj
->buffer
.pointer
;
371 buffer_size
= obj
->buffer
.length
/2;
372 buffer_end
= buffer_entry
+ buffer_size
;
375 * BIOS/ACPI on devices with WMI interface version 0 does not clear
376 * buffer before filling it. So next time when BIOS/ACPI send WMI event
377 * which is smaller as previous then it contains garbage in buffer from
380 * BIOS/ACPI on devices with WMI interface version 1 clears buffer and
381 * sometimes send more events in buffer at one call.
383 * So to prevent reading garbage from buffer we will process only first
384 * one event on devices with WMI interface version 0.
386 if (priv
->interface_version
== 0 && buffer_entry
< buffer_end
)
387 if (buffer_end
> buffer_entry
+ buffer_entry
[0] + 1)
388 buffer_end
= buffer_entry
+ buffer_entry
[0] + 1;
390 while (buffer_entry
< buffer_end
) {
392 len
= buffer_entry
[0];
398 if (buffer_entry
+ len
> buffer_end
) {
399 pr_warn("Invalid length of WMI event\n");
403 pr_debug("Process buffer (%*ph)\n", len
*2, buffer_entry
);
405 switch (buffer_entry
[1]) {
406 case 0x0000: /* One key pressed or event occurred */
408 dell_wmi_process_key(wdev
, 0x0000,
410 /* Other entries could contain additional information */
412 case 0x0010: /* Sequence of keys pressed */
413 case 0x0011: /* Sequence of events occurred */
414 for (i
= 2; i
< len
; ++i
)
415 dell_wmi_process_key(wdev
, buffer_entry
[1],
418 default: /* Unknown event */
419 pr_info("Unknown WMI event type 0x%x\n",
420 (int)buffer_entry
[1]);
430 static bool have_scancode(u32 scancode
, const struct key_entry
*keymap
, int len
)
434 for (i
= 0; i
< len
; i
++)
435 if (keymap
[i
].code
== scancode
)
441 static void handle_dmi_entry(const struct dmi_header
*dm
, void *opaque
)
443 struct dell_dmi_results
*results
= opaque
;
444 struct dell_bios_hotkey_table
*table
;
445 int hotkey_num
, i
, pos
= 0;
446 struct key_entry
*keymap
;
448 if (results
->err
|| results
->keymap
)
449 return; /* We already found the hotkey table. */
451 /* The Dell hotkey table is type 0xB2. Scan until we find it. */
452 if (dm
->type
!= 0xb2)
455 table
= container_of(dm
, struct dell_bios_hotkey_table
, header
);
457 hotkey_num
= (table
->header
.length
-
458 sizeof(struct dell_bios_hotkey_table
)) /
459 sizeof(struct dell_bios_keymap_entry
);
460 if (hotkey_num
< 1) {
462 * Historically, dell-wmi would ignore a DMI entry of
463 * fewer than 7 bytes. Sizes between 4 and 8 bytes are
464 * nonsensical (both the header and all entries are 4
465 * bytes), so we approximate the old behavior by
466 * ignoring tables with fewer than one entry.
471 keymap
= kcalloc(hotkey_num
, sizeof(struct key_entry
), GFP_KERNEL
);
473 results
->err
= -ENOMEM
;
477 for (i
= 0; i
< hotkey_num
; i
++) {
478 const struct dell_bios_keymap_entry
*bios_entry
=
481 /* Uninitialized entries are 0 aka KEY_RESERVED. */
482 u16 keycode
= (bios_entry
->keycode
<
483 ARRAY_SIZE(bios_to_linux_keycode
)) ?
484 bios_to_linux_keycode
[bios_entry
->keycode
] :
488 * Log if we find an entry in the DMI table that we don't
489 * understand. If this happens, we should figure out what
490 * the entry means and add it to bios_to_linux_keycode.
492 if (keycode
== KEY_RESERVED
) {
493 pr_info("firmware scancode 0x%x maps to unrecognized keycode 0x%x\n",
494 bios_entry
->scancode
, bios_entry
->keycode
);
498 if (keycode
== KEY_KBDILLUMTOGGLE
)
499 keymap
[pos
].type
= KE_IGNORE
;
501 keymap
[pos
].type
= KE_KEY
;
502 keymap
[pos
].code
= bios_entry
->scancode
;
503 keymap
[pos
].keycode
= keycode
;
508 results
->keymap
= keymap
;
509 results
->keymap_size
= pos
;
512 static int dell_wmi_input_setup(struct wmi_device
*wdev
)
514 struct dell_wmi_priv
*priv
= dev_get_drvdata(&wdev
->dev
);
515 struct dell_dmi_results dmi_results
= {};
516 struct key_entry
*keymap
;
519 priv
->input_dev
= input_allocate_device();
520 if (!priv
->input_dev
)
523 priv
->input_dev
->name
= "Dell WMI hotkeys";
524 priv
->input_dev
->id
.bustype
= BUS_HOST
;
525 priv
->input_dev
->dev
.parent
= &wdev
->dev
;
527 if (dmi_walk(handle_dmi_entry
, &dmi_results
)) {
529 * Historically, dell-wmi ignored dmi_walk errors. A failure
530 * is certainly surprising, but it probably just indicates
533 pr_warn("no DMI; using the old-style hotkey interface\n");
536 if (dmi_results
.err
) {
537 err
= dmi_results
.err
;
541 keymap
= kcalloc(dmi_results
.keymap_size
+
542 ARRAY_SIZE(dell_wmi_keymap_type_0000
) +
543 ARRAY_SIZE(dell_wmi_keymap_type_0010
) +
544 ARRAY_SIZE(dell_wmi_keymap_type_0011
) +
546 sizeof(struct key_entry
), GFP_KERNEL
);
548 kfree(dmi_results
.keymap
);
553 /* Append table with events of type 0x0010 which comes from DMI */
554 for (i
= 0; i
< dmi_results
.keymap_size
; i
++) {
555 keymap
[pos
] = dmi_results
.keymap
[i
];
556 keymap
[pos
].code
|= (0x0010 << 16);
560 kfree(dmi_results
.keymap
);
562 /* Append table with extra events of type 0x0010 which are not in DMI */
563 for (i
= 0; i
< ARRAY_SIZE(dell_wmi_keymap_type_0010
); i
++) {
564 const struct key_entry
*entry
= &dell_wmi_keymap_type_0010
[i
];
567 * Check if we've already found this scancode. This takes
568 * quadratic time, but it doesn't matter unless the list
569 * of extra keys gets very long.
571 if (dmi_results
.keymap_size
&&
572 have_scancode(entry
->code
| (0x0010 << 16),
573 keymap
, dmi_results
.keymap_size
)
577 keymap
[pos
] = *entry
;
578 keymap
[pos
].code
|= (0x0010 << 16);
582 /* Append table with events of type 0x0011 */
583 for (i
= 0; i
< ARRAY_SIZE(dell_wmi_keymap_type_0011
); i
++) {
584 keymap
[pos
] = dell_wmi_keymap_type_0011
[i
];
585 keymap
[pos
].code
|= (0x0011 << 16);
590 * Now append also table with "legacy" events of type 0x0000. Some of
591 * them are reported also on laptops which have scancodes in DMI.
593 for (i
= 0; i
< ARRAY_SIZE(dell_wmi_keymap_type_0000
); i
++) {
594 keymap
[pos
] = dell_wmi_keymap_type_0000
[i
];
598 keymap
[pos
].type
= KE_END
;
600 err
= sparse_keymap_setup(priv
->input_dev
, keymap
, NULL
);
602 * Sparse keymap library makes a copy of keymap so we don't need the
603 * original one that was allocated.
609 err
= input_register_device(priv
->input_dev
);
616 input_free_device(priv
->input_dev
);
620 static void dell_wmi_input_destroy(struct wmi_device
*wdev
)
622 struct dell_wmi_priv
*priv
= dev_get_drvdata(&wdev
->dev
);
624 input_unregister_device(priv
->input_dev
);
628 * According to Dell SMBIOS documentation:
630 * 17 3 Application Program Registration
632 * cbArg1 Application ID 1 = 0x00010000
633 * cbArg2 Application ID 2
634 * QUICKSET/DCP = 0x51534554 "QSET"
635 * ALS Driver = 0x416c7353 "AlsS"
636 * Latitude ON = 0x4c6f6e52 "LonR"
637 * cbArg3 Application version or revision number
638 * cbArg4 0 = Unregister application
639 * 1 = Register application
640 * cbRes1 Standard return codes (0, -1, -2)
643 static int dell_wmi_events_set_enabled(bool enable
)
645 struct calling_interface_buffer
*buffer
;
648 buffer
= kzalloc(sizeof(struct calling_interface_buffer
), GFP_KERNEL
);
651 buffer
->cmd_class
= CLASS_INFO
;
652 buffer
->cmd_select
= SELECT_APP_REGISTRATION
;
653 buffer
->input
[0] = 0x10000;
654 buffer
->input
[1] = 0x51534554;
655 buffer
->input
[3] = enable
;
656 ret
= dell_smbios_call(buffer
);
658 ret
= buffer
->output
[0];
661 return dell_smbios_error(ret
);
664 static int dell_wmi_probe(struct wmi_device
*wdev
, const void *context
)
666 struct dell_wmi_priv
*priv
;
669 ret
= dell_wmi_get_descriptor_valid();
674 &wdev
->dev
, sizeof(struct dell_wmi_priv
), GFP_KERNEL
);
677 dev_set_drvdata(&wdev
->dev
, priv
);
679 if (!dell_wmi_get_interface_version(&priv
->interface_version
))
680 return -EPROBE_DEFER
;
682 return dell_wmi_input_setup(wdev
);
685 static int dell_wmi_remove(struct wmi_device
*wdev
)
687 dell_wmi_input_destroy(wdev
);
690 static const struct wmi_device_id dell_wmi_id_table
[] = {
691 { .guid_string
= DELL_EVENT_GUID
},
695 static struct wmi_driver dell_wmi_driver
= {
699 .id_table
= dell_wmi_id_table
,
700 .probe
= dell_wmi_probe
,
701 .remove
= dell_wmi_remove
,
702 .notify
= dell_wmi_notify
,
705 static int __init
dell_wmi_init(void)
709 dmi_check_system(dell_wmi_smbios_list
);
711 if (wmi_requires_smbios_request
) {
712 err
= dell_wmi_events_set_enabled(true);
714 pr_err("Failed to enable WMI events\n");
719 return wmi_driver_register(&dell_wmi_driver
);
721 late_initcall(dell_wmi_init
);
723 static void __exit
dell_wmi_exit(void)
725 if (wmi_requires_smbios_request
)
726 dell_wmi_events_set_enabled(false);
728 wmi_driver_unregister(&dell_wmi_driver
);
730 module_exit(dell_wmi_exit
);
732 MODULE_DEVICE_TABLE(wmi
, dell_wmi_id_table
);