4 * Copyright (C) 2008 Red Hat <mjg@redhat.com>
5 * Copyright (C) 2014-2015 Pali Rohár <pali.rohar@gmail.com>
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>
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.
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.
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
27 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/slab.h>
33 #include <linux/types.h>
34 #include <linux/input.h>
35 #include <linux/input/sparse-keymap.h>
36 #include <linux/acpi.h>
37 #include <linux/string.h>
38 #include <linux/dmi.h>
39 #include <linux/wmi.h>
40 #include <acpi/video.h>
41 #include "dell-smbios.h"
42 #include "dell-wmi-descriptor.h"
44 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
45 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
46 MODULE_DESCRIPTION("Dell laptop WMI hotkeys driver");
47 MODULE_LICENSE("GPL");
49 #define DELL_EVENT_GUID "9DBB5994-A997-11DA-B012-B622A1EF5492"
51 static bool wmi_requires_smbios_request
;
53 MODULE_ALIAS("wmi:"DELL_EVENT_GUID
);
55 struct dell_wmi_priv
{
56 struct input_dev
*input_dev
;
57 u32 interface_version
;
60 static int __init
dmi_matched(const struct dmi_system_id
*dmi
)
62 wmi_requires_smbios_request
= 1;
66 static const struct dmi_system_id dell_wmi_smbios_list
[] __initconst
= {
68 .callback
= dmi_matched
,
69 .ident
= "Dell Inspiron M5110",
71 DMI_MATCH(DMI_SYS_VENDOR
, "Dell Inc."),
72 DMI_MATCH(DMI_PRODUCT_NAME
, "Inspiron M5110"),
76 .callback
= dmi_matched
,
77 .ident
= "Dell Vostro V131",
79 DMI_MATCH(DMI_SYS_VENDOR
, "Dell Inc."),
80 DMI_MATCH(DMI_PRODUCT_NAME
, "Vostro V131"),
87 * Keymap for WMI events of type 0x0000
89 * 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.
93 static const struct key_entry dell_wmi_keymap_type_0000
[] = {
94 { KE_IGNORE
, 0x003a, { KEY_CAPSLOCK
} },
96 /* Key code is followed by brightness level */
97 { KE_KEY
, 0xe005, { KEY_BRIGHTNESSDOWN
} },
98 { KE_KEY
, 0xe006, { KEY_BRIGHTNESSUP
} },
100 /* Battery health status button */
101 { KE_KEY
, 0xe007, { KEY_BATTERY
} },
103 /* Radio devices state change, key code is followed by other values */
104 { KE_IGNORE
, 0xe008, { KEY_RFKILL
} },
106 { KE_KEY
, 0xe009, { KEY_EJECTCD
} },
108 /* Key code is followed by: next, active and attached devices */
109 { KE_KEY
, 0xe00b, { KEY_SWITCHVIDEOMODE
} },
111 /* Key code is followed by keyboard illumination level */
112 { KE_IGNORE
, 0xe00c, { KEY_KBDILLUMTOGGLE
} },
114 /* BIOS error detected */
115 { KE_IGNORE
, 0xe00d, { KEY_RESERVED
} },
117 /* Battery was removed or inserted */
118 { KE_IGNORE
, 0xe00e, { KEY_RESERVED
} },
121 { KE_KEY
, 0xe011, { KEY_WLAN
} },
123 /* Ambient light sensor toggle */
124 { KE_IGNORE
, 0xe013, { KEY_RESERVED
} },
126 { KE_IGNORE
, 0xe020, { KEY_MUTE
} },
128 /* Unknown, defined in ACPI DSDT */
129 /* { KE_IGNORE, 0xe023, { KEY_RESERVED } }, */
131 /* Untested, Dell Instant Launch key on Inspiron 7520 */
132 /* { KE_IGNORE, 0xe024, { KEY_RESERVED } }, */
134 /* Dell Instant Launch key */
135 { KE_KEY
, 0xe025, { KEY_PROG4
} },
137 /* Audio panel key */
138 { KE_IGNORE
, 0xe026, { KEY_RESERVED
} },
140 /* LCD Display On/Off Control key */
141 { KE_KEY
, 0xe027, { KEY_DISPLAYTOGGLE
} },
143 /* Untested, Multimedia key on Dell Vostro 3560 */
144 /* { KE_IGNORE, 0xe028, { KEY_RESERVED } }, */
146 /* Dell Instant Launch key */
147 { KE_KEY
, 0xe029, { KEY_PROG4
} },
149 /* Untested, Windows Mobility Center button on Inspiron 7520 */
150 /* { KE_IGNORE, 0xe02a, { KEY_RESERVED } }, */
152 /* Unknown, defined in ACPI DSDT */
153 /* { KE_IGNORE, 0xe02b, { KEY_RESERVED } }, */
155 /* Untested, Dell Audio With Preset Switch button on Inspiron 7520 */
156 /* { KE_IGNORE, 0xe02c, { KEY_RESERVED } }, */
158 { 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
} },
165 { KE_IGNORE
, 0xe043, { KEY_RESERVED
} },
167 /* NIC Link is Down */
168 { KE_IGNORE
, 0xe044, { KEY_RESERVED
} },
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.
180 { KE_IGNORE
, 0xe045, { KEY_NUMLOCK
} },
182 /* Scroll lock and also going to tablet mode on portable devices */
183 { KE_IGNORE
, 0xe046, { KEY_SCROLLLOCK
} },
185 /* Untested, going from tablet mode on portable devices */
186 /* { KE_IGNORE, 0xe047, { KEY_RESERVED } }, */
188 /* Dell Support Center key */
189 { KE_IGNORE
, 0xe06e, { KEY_RESERVED
} },
191 { KE_IGNORE
, 0xe0f7, { KEY_MUTE
} },
192 { KE_IGNORE
, 0xe0f8, { KEY_VOLUMEDOWN
} },
193 { KE_IGNORE
, 0xe0f9, { KEY_VOLUMEUP
} },
196 struct dell_bios_keymap_entry
{
201 struct dell_bios_hotkey_table
{
202 struct dmi_header header
;
203 struct dell_bios_keymap_entry keymap
[];
207 struct dell_dmi_results
{
210 struct key_entry
*keymap
;
213 /* Uninitialized entries here are KEY_RESERVED == 0. */
214 static const u16 bios_to_linux_keycode
[256] = {
218 [3] = KEY_PREVIOUSSONG
,
225 [10] = KEY_VOLUMEDOWN
,
234 [19] = KEY_BRIGHTNESSDOWN
,
235 [20] = KEY_BRIGHTNESSUP
,
236 [21] = KEY_BRIGHTNESS_AUTO
,
237 [22] = KEY_KBDILLUMTOGGLE
,
239 [24] = KEY_SWITCHVIDEOMODE
,
242 [27] = KEY_SWITCHVIDEOMODE
,
258 * Keymap for WMI events of type 0x0010
260 * These are applied if the 0xB2 DMI hotkey table is present and doesn't
263 static const struct key_entry dell_wmi_keymap_type_0010
[] = {
264 /* Fn-lock switched to function keys */
265 { KE_IGNORE
, 0x0, { KEY_RESERVED
} },
267 /* Fn-lock switched to multimedia keys */
268 { KE_IGNORE
, 0x1, { KEY_RESERVED
} },
271 { KE_KEY
, 0x150, { KEY_MICMUTE
} },
274 { KE_IGNORE
, 0x151, { KEY_RESERVED
} },
276 /* Change keyboard illumination */
277 { KE_IGNORE
, 0x152, { KEY_KBDILLUMTOGGLE
} },
280 * Radio disable (notify only -- there is no model for which the
281 * WMI event is supposed to trigger an action).
283 { KE_IGNORE
, 0x153, { KEY_RFKILL
} },
285 /* RGB keyboard backlight control */
286 { KE_IGNORE
, 0x154, { KEY_RESERVED
} },
289 * Stealth mode toggle. This will "disable all lights and sounds".
290 * The action is performed by the BIOS and EC; the WMI event is just
291 * a notification. On the XPS 13 9350, this is Fn+F7, and there's
292 * a BIOS setting to enable and disable the hotkey.
294 { KE_IGNORE
, 0x155, { KEY_RESERVED
} },
296 /* Rugged magnetic dock attach/detach events */
297 { KE_IGNORE
, 0x156, { KEY_RESERVED
} },
298 { KE_IGNORE
, 0x157, { KEY_RESERVED
} },
300 /* Rugged programmable (P1/P2/P3 keys) */
301 { KE_KEY
, 0x850, { KEY_PROG1
} },
302 { KE_KEY
, 0x851, { KEY_PROG2
} },
303 { KE_KEY
, 0x852, { KEY_PROG3
} },
306 * Radio disable (notify only -- there is no model for which the
307 * WMI event is supposed to trigger an action).
309 { KE_IGNORE
, 0xe008, { KEY_RFKILL
} },
312 { KE_IGNORE
, 0xe035, { KEY_RESERVED
} },
316 * Keymap for WMI events of type 0x0011
318 static const struct key_entry dell_wmi_keymap_type_0011
[] = {
319 /* Battery unplugged */
320 { KE_IGNORE
, 0xfff0, { KEY_RESERVED
} },
322 /* Battery inserted */
323 { KE_IGNORE
, 0xfff1, { KEY_RESERVED
} },
325 /* Keyboard backlight level changed */
326 { KE_IGNORE
, 0x01e1, { KEY_RESERVED
} },
327 { KE_IGNORE
, 0x02ea, { KEY_RESERVED
} },
328 { KE_IGNORE
, 0x02eb, { KEY_RESERVED
} },
329 { KE_IGNORE
, 0x02ec, { KEY_RESERVED
} },
330 { KE_IGNORE
, 0x02f6, { KEY_RESERVED
} },
333 static void dell_wmi_process_key(struct wmi_device
*wdev
, int type
, int code
)
335 struct dell_wmi_priv
*priv
= dev_get_drvdata(&wdev
->dev
);
336 const struct key_entry
*key
;
338 key
= sparse_keymap_entry_from_scancode(priv
->input_dev
,
339 (type
<< 16) | code
);
341 pr_info("Unknown key with type 0x%04x and code 0x%04x pressed\n",
346 pr_debug("Key with type 0x%04x and code 0x%04x pressed\n", type
, code
);
348 /* Don't report brightness notifications that will also come via ACPI */
349 if ((key
->keycode
== KEY_BRIGHTNESSUP
||
350 key
->keycode
== KEY_BRIGHTNESSDOWN
) &&
351 acpi_video_handles_brightness_key_presses())
354 if (type
== 0x0000 && code
== 0xe025 && !wmi_requires_smbios_request
)
357 if (key
->keycode
== KEY_KBDILLUMTOGGLE
)
358 dell_laptop_call_notifier(
359 DELL_LAPTOP_KBD_BACKLIGHT_BRIGHTNESS_CHANGED
, NULL
);
361 sparse_keymap_report_entry(priv
->input_dev
, key
, 1, true);
364 static void dell_wmi_notify(struct wmi_device
*wdev
,
365 union acpi_object
*obj
)
367 struct dell_wmi_priv
*priv
= dev_get_drvdata(&wdev
->dev
);
368 u16
*buffer_entry
, *buffer_end
;
369 acpi_size buffer_size
;
372 if (obj
->type
!= ACPI_TYPE_BUFFER
) {
373 pr_warn("bad response type %x\n", obj
->type
);
377 pr_debug("Received WMI event (%*ph)\n",
378 obj
->buffer
.length
, obj
->buffer
.pointer
);
380 buffer_entry
= (u16
*)obj
->buffer
.pointer
;
381 buffer_size
= obj
->buffer
.length
/2;
382 buffer_end
= buffer_entry
+ buffer_size
;
385 * BIOS/ACPI on devices with WMI interface version 0 does not clear
386 * buffer before filling it. So next time when BIOS/ACPI send WMI event
387 * which is smaller as previous then it contains garbage in buffer from
390 * BIOS/ACPI on devices with WMI interface version 1 clears buffer and
391 * sometimes send more events in buffer at one call.
393 * So to prevent reading garbage from buffer we will process only first
394 * one event on devices with WMI interface version 0.
396 if (priv
->interface_version
== 0 && buffer_entry
< buffer_end
)
397 if (buffer_end
> buffer_entry
+ buffer_entry
[0] + 1)
398 buffer_end
= buffer_entry
+ buffer_entry
[0] + 1;
400 while (buffer_entry
< buffer_end
) {
402 len
= buffer_entry
[0];
408 if (buffer_entry
+ len
> buffer_end
) {
409 pr_warn("Invalid length of WMI event\n");
413 pr_debug("Process buffer (%*ph)\n", len
*2, buffer_entry
);
415 switch (buffer_entry
[1]) {
416 case 0x0000: /* One key pressed or event occurred */
418 dell_wmi_process_key(wdev
, 0x0000,
420 /* Other entries could contain additional information */
422 case 0x0010: /* Sequence of keys pressed */
423 case 0x0011: /* Sequence of events occurred */
424 for (i
= 2; i
< len
; ++i
)
425 dell_wmi_process_key(wdev
, buffer_entry
[1],
428 default: /* Unknown event */
429 pr_info("Unknown WMI event type 0x%x\n",
430 (int)buffer_entry
[1]);
440 static bool have_scancode(u32 scancode
, const struct key_entry
*keymap
, int len
)
444 for (i
= 0; i
< len
; i
++)
445 if (keymap
[i
].code
== scancode
)
451 static void handle_dmi_entry(const struct dmi_header
*dm
, void *opaque
)
453 struct dell_dmi_results
*results
= opaque
;
454 struct dell_bios_hotkey_table
*table
;
455 int hotkey_num
, i
, pos
= 0;
456 struct key_entry
*keymap
;
458 if (results
->err
|| results
->keymap
)
459 return; /* We already found the hotkey table. */
461 /* The Dell hotkey table is type 0xB2. Scan until we find it. */
462 if (dm
->type
!= 0xb2)
465 table
= container_of(dm
, struct dell_bios_hotkey_table
, header
);
467 hotkey_num
= (table
->header
.length
-
468 sizeof(struct dell_bios_hotkey_table
)) /
469 sizeof(struct dell_bios_keymap_entry
);
470 if (hotkey_num
< 1) {
472 * Historically, dell-wmi would ignore a DMI entry of
473 * fewer than 7 bytes. Sizes between 4 and 8 bytes are
474 * nonsensical (both the header and all entries are 4
475 * bytes), so we approximate the old behavior by
476 * ignoring tables with fewer than one entry.
481 keymap
= kcalloc(hotkey_num
, sizeof(struct key_entry
), GFP_KERNEL
);
483 results
->err
= -ENOMEM
;
487 for (i
= 0; i
< hotkey_num
; i
++) {
488 const struct dell_bios_keymap_entry
*bios_entry
=
491 /* Uninitialized entries are 0 aka KEY_RESERVED. */
492 u16 keycode
= (bios_entry
->keycode
<
493 ARRAY_SIZE(bios_to_linux_keycode
)) ?
494 bios_to_linux_keycode
[bios_entry
->keycode
] :
498 * Log if we find an entry in the DMI table that we don't
499 * understand. If this happens, we should figure out what
500 * the entry means and add it to bios_to_linux_keycode.
502 if (keycode
== KEY_RESERVED
) {
503 pr_info("firmware scancode 0x%x maps to unrecognized keycode 0x%x\n",
504 bios_entry
->scancode
, bios_entry
->keycode
);
508 if (keycode
== KEY_KBDILLUMTOGGLE
)
509 keymap
[pos
].type
= KE_IGNORE
;
511 keymap
[pos
].type
= KE_KEY
;
512 keymap
[pos
].code
= bios_entry
->scancode
;
513 keymap
[pos
].keycode
= keycode
;
518 results
->keymap
= keymap
;
519 results
->keymap_size
= pos
;
522 static int dell_wmi_input_setup(struct wmi_device
*wdev
)
524 struct dell_wmi_priv
*priv
= dev_get_drvdata(&wdev
->dev
);
525 struct dell_dmi_results dmi_results
= {};
526 struct key_entry
*keymap
;
529 priv
->input_dev
= input_allocate_device();
530 if (!priv
->input_dev
)
533 priv
->input_dev
->name
= "Dell WMI hotkeys";
534 priv
->input_dev
->id
.bustype
= BUS_HOST
;
535 priv
->input_dev
->dev
.parent
= &wdev
->dev
;
537 if (dmi_walk(handle_dmi_entry
, &dmi_results
)) {
539 * Historically, dell-wmi ignored dmi_walk errors. A failure
540 * is certainly surprising, but it probably just indicates
543 pr_warn("no DMI; using the old-style hotkey interface\n");
546 if (dmi_results
.err
) {
547 err
= dmi_results
.err
;
551 keymap
= kcalloc(dmi_results
.keymap_size
+
552 ARRAY_SIZE(dell_wmi_keymap_type_0000
) +
553 ARRAY_SIZE(dell_wmi_keymap_type_0010
) +
554 ARRAY_SIZE(dell_wmi_keymap_type_0011
) +
556 sizeof(struct key_entry
), GFP_KERNEL
);
558 kfree(dmi_results
.keymap
);
563 /* Append table with events of type 0x0010 which comes from DMI */
564 for (i
= 0; i
< dmi_results
.keymap_size
; i
++) {
565 keymap
[pos
] = dmi_results
.keymap
[i
];
566 keymap
[pos
].code
|= (0x0010 << 16);
570 kfree(dmi_results
.keymap
);
572 /* Append table with extra events of type 0x0010 which are not in DMI */
573 for (i
= 0; i
< ARRAY_SIZE(dell_wmi_keymap_type_0010
); i
++) {
574 const struct key_entry
*entry
= &dell_wmi_keymap_type_0010
[i
];
577 * Check if we've already found this scancode. This takes
578 * quadratic time, but it doesn't matter unless the list
579 * of extra keys gets very long.
581 if (dmi_results
.keymap_size
&&
582 have_scancode(entry
->code
| (0x0010 << 16),
583 keymap
, dmi_results
.keymap_size
)
587 keymap
[pos
] = *entry
;
588 keymap
[pos
].code
|= (0x0010 << 16);
592 /* Append table with events of type 0x0011 */
593 for (i
= 0; i
< ARRAY_SIZE(dell_wmi_keymap_type_0011
); i
++) {
594 keymap
[pos
] = dell_wmi_keymap_type_0011
[i
];
595 keymap
[pos
].code
|= (0x0011 << 16);
600 * Now append also table with "legacy" events of type 0x0000. Some of
601 * them are reported also on laptops which have scancodes in DMI.
603 for (i
= 0; i
< ARRAY_SIZE(dell_wmi_keymap_type_0000
); i
++) {
604 keymap
[pos
] = dell_wmi_keymap_type_0000
[i
];
608 keymap
[pos
].type
= KE_END
;
610 err
= sparse_keymap_setup(priv
->input_dev
, keymap
, NULL
);
612 * Sparse keymap library makes a copy of keymap so we don't need the
613 * original one that was allocated.
619 err
= input_register_device(priv
->input_dev
);
626 input_free_device(priv
->input_dev
);
630 static void dell_wmi_input_destroy(struct wmi_device
*wdev
)
632 struct dell_wmi_priv
*priv
= dev_get_drvdata(&wdev
->dev
);
634 input_unregister_device(priv
->input_dev
);
638 * According to Dell SMBIOS documentation:
640 * 17 3 Application Program Registration
642 * cbArg1 Application ID 1 = 0x00010000
643 * cbArg2 Application ID 2
644 * QUICKSET/DCP = 0x51534554 "QSET"
645 * ALS Driver = 0x416c7353 "AlsS"
646 * Latitude ON = 0x4c6f6e52 "LonR"
647 * cbArg3 Application version or revision number
648 * cbArg4 0 = Unregister application
649 * 1 = Register application
650 * cbRes1 Standard return codes (0, -1, -2)
653 static int dell_wmi_events_set_enabled(bool enable
)
655 struct calling_interface_buffer
*buffer
;
658 buffer
= kzalloc(sizeof(struct calling_interface_buffer
), GFP_KERNEL
);
661 buffer
->cmd_class
= CLASS_INFO
;
662 buffer
->cmd_select
= SELECT_APP_REGISTRATION
;
663 buffer
->input
[0] = 0x10000;
664 buffer
->input
[1] = 0x51534554;
665 buffer
->input
[3] = enable
;
666 ret
= dell_smbios_call(buffer
);
668 ret
= buffer
->output
[0];
671 return dell_smbios_error(ret
);
674 static int dell_wmi_probe(struct wmi_device
*wdev
)
676 struct dell_wmi_priv
*priv
;
679 ret
= dell_wmi_get_descriptor_valid();
684 &wdev
->dev
, sizeof(struct dell_wmi_priv
), GFP_KERNEL
);
687 dev_set_drvdata(&wdev
->dev
, priv
);
689 if (!dell_wmi_get_interface_version(&priv
->interface_version
))
690 return -EPROBE_DEFER
;
692 return dell_wmi_input_setup(wdev
);
695 static int dell_wmi_remove(struct wmi_device
*wdev
)
697 dell_wmi_input_destroy(wdev
);
700 static const struct wmi_device_id dell_wmi_id_table
[] = {
701 { .guid_string
= DELL_EVENT_GUID
},
705 static struct wmi_driver dell_wmi_driver
= {
709 .id_table
= dell_wmi_id_table
,
710 .probe
= dell_wmi_probe
,
711 .remove
= dell_wmi_remove
,
712 .notify
= dell_wmi_notify
,
715 static int __init
dell_wmi_init(void)
719 dmi_check_system(dell_wmi_smbios_list
);
721 if (wmi_requires_smbios_request
) {
722 err
= dell_wmi_events_set_enabled(true);
724 pr_err("Failed to enable WMI events\n");
729 return wmi_driver_register(&dell_wmi_driver
);
731 late_initcall(dell_wmi_init
);
733 static void __exit
dell_wmi_exit(void)
735 if (wmi_requires_smbios_request
)
736 dell_wmi_events_set_enabled(false);
738 wmi_driver_unregister(&dell_wmi_driver
);
740 module_exit(dell_wmi_exit
);