Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Dell WMI hotkeys |
| 3 | * |
| 4 | * Copyright (C) 2008 Red Hat <mjg@redhat.com> |
Pali Rohár | 595773a4 | 2016-01-04 22:26:35 +0100 | [diff] [blame] | 5 | * Copyright (C) 2014-2015 Pali Rohár <pali.rohar@gmail.com> |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 6 | * |
| 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 Perches | eb88952 | 2011-03-29 15:21:37 -0700 | [diff] [blame] | 27 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 28 | |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 29 | #include <linux/kernel.h> |
| 30 | #include <linux/module.h> |
| 31 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 32 | #include <linux/slab.h> |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 33 | #include <linux/types.h> |
| 34 | #include <linux/input.h> |
Dmitry Torokhov | 890a7c8 | 2010-08-04 22:30:08 -0700 | [diff] [blame] | 35 | #include <linux/input/sparse-keymap.h> |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 36 | #include <linux/acpi.h> |
| 37 | #include <linux/string.h> |
Rezwanul Kabir | 5ea2559 | 2009-11-02 12:00:42 -0500 | [diff] [blame] | 38 | #include <linux/dmi.h> |
Hans de Goede | 18bd769 | 2015-06-16 16:28:01 +0200 | [diff] [blame] | 39 | #include <acpi/video.h> |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 40 | |
| 41 | MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>"); |
Pali Rohár | 595773a4 | 2016-01-04 22:26:35 +0100 | [diff] [blame] | 42 | MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>"); |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 43 | MODULE_DESCRIPTION("Dell laptop WMI hotkeys driver"); |
| 44 | MODULE_LICENSE("GPL"); |
| 45 | |
| 46 | #define DELL_EVENT_GUID "9DBB5994-A997-11DA-B012-B622A1EF5492" |
Pali Rohár | 595773a4 | 2016-01-04 22:26:35 +0100 | [diff] [blame] | 47 | #define DELL_DESCRIPTOR_GUID "8D9DDCBC-A997-11DA-B012-B622A1EF5492" |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 48 | |
Rezwanul Kabir | 5ea2559 | 2009-11-02 12:00:42 -0500 | [diff] [blame] | 49 | static int acpi_video; |
Pali Rohár | 595773a4 | 2016-01-04 22:26:35 +0100 | [diff] [blame] | 50 | static u32 dell_wmi_interface_version; |
Rezwanul Kabir | 5ea2559 | 2009-11-02 12:00:42 -0500 | [diff] [blame] | 51 | |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 52 | MODULE_ALIAS("wmi:"DELL_EVENT_GUID); |
Pali Rohár | 595773a4 | 2016-01-04 22:26:35 +0100 | [diff] [blame] | 53 | MODULE_ALIAS("wmi:"DELL_DESCRIPTOR_GUID); |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 54 | |
Mario Limonciello | 5cab009 | 2009-06-10 19:40:47 +0000 | [diff] [blame] | 55 | /* |
| 56 | * Certain keys are flagged as KE_IGNORE. All of these are either |
| 57 | * notifications (rather than requests for change) or are also sent |
| 58 | * via the keyboard controller so should not be sent again. |
| 59 | */ |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 60 | |
Dmitry Torokhov | 890a7c8 | 2010-08-04 22:30:08 -0700 | [diff] [blame] | 61 | static const struct key_entry dell_wmi_legacy_keymap[] __initconst = { |
Seth Forshee | f1566f0 | 2011-08-01 15:46:10 -0500 | [diff] [blame] | 62 | { KE_IGNORE, 0x003a, { KEY_CAPSLOCK } }, |
| 63 | |
Dmitry Torokhov | 890a7c8 | 2010-08-04 22:30:08 -0700 | [diff] [blame] | 64 | { KE_KEY, 0xe045, { KEY_PROG1 } }, |
| 65 | { KE_KEY, 0xe009, { KEY_EJECTCD } }, |
Mario Limonciello | 5cab009 | 2009-06-10 19:40:47 +0000 | [diff] [blame] | 66 | |
| 67 | /* These also contain the brightness level at offset 6 */ |
Dmitry Torokhov | 890a7c8 | 2010-08-04 22:30:08 -0700 | [diff] [blame] | 68 | { KE_KEY, 0xe006, { KEY_BRIGHTNESSUP } }, |
| 69 | { KE_KEY, 0xe005, { KEY_BRIGHTNESSDOWN } }, |
Mario Limonciello | 5cab009 | 2009-06-10 19:40:47 +0000 | [diff] [blame] | 70 | |
| 71 | /* Battery health status button */ |
Dmitry Torokhov | 890a7c8 | 2010-08-04 22:30:08 -0700 | [diff] [blame] | 72 | { KE_KEY, 0xe007, { KEY_BATTERY } }, |
Mario Limonciello | 5cab009 | 2009-06-10 19:40:47 +0000 | [diff] [blame] | 73 | |
Gabriele Mazzotta | 8f8d75e | 2014-12-04 00:16:21 +0100 | [diff] [blame] | 74 | /* Radio devices state change */ |
| 75 | { KE_IGNORE, 0xe008, { KEY_RFKILL } }, |
Mario Limonciello | 5cab009 | 2009-06-10 19:40:47 +0000 | [diff] [blame] | 76 | |
| 77 | /* The next device is at offset 6, the active devices are at |
| 78 | offset 8 and the attached devices at offset 10 */ |
Dmitry Torokhov | 890a7c8 | 2010-08-04 22:30:08 -0700 | [diff] [blame] | 79 | { KE_KEY, 0xe00b, { KEY_SWITCHVIDEOMODE } }, |
Mario Limonciello | 5cab009 | 2009-06-10 19:40:47 +0000 | [diff] [blame] | 80 | |
Dmitry Torokhov | 890a7c8 | 2010-08-04 22:30:08 -0700 | [diff] [blame] | 81 | { KE_IGNORE, 0xe00c, { KEY_KBDILLUMTOGGLE } }, |
Mario Limonciello | 5cab009 | 2009-06-10 19:40:47 +0000 | [diff] [blame] | 82 | |
| 83 | /* BIOS error detected */ |
Dmitry Torokhov | 890a7c8 | 2010-08-04 22:30:08 -0700 | [diff] [blame] | 84 | { KE_IGNORE, 0xe00d, { KEY_RESERVED } }, |
Mario Limonciello | 5cab009 | 2009-06-10 19:40:47 +0000 | [diff] [blame] | 85 | |
| 86 | /* Wifi Catcher */ |
Dmitry Torokhov | 890a7c8 | 2010-08-04 22:30:08 -0700 | [diff] [blame] | 87 | { KE_KEY, 0xe011, {KEY_PROG2 } }, |
Mario Limonciello | 5cab009 | 2009-06-10 19:40:47 +0000 | [diff] [blame] | 88 | |
| 89 | /* Ambient light sensor toggle */ |
Dmitry Torokhov | 890a7c8 | 2010-08-04 22:30:08 -0700 | [diff] [blame] | 90 | { KE_IGNORE, 0xe013, { KEY_RESERVED } }, |
Mario Limonciello | 5cab009 | 2009-06-10 19:40:47 +0000 | [diff] [blame] | 91 | |
Dmitry Torokhov | 890a7c8 | 2010-08-04 22:30:08 -0700 | [diff] [blame] | 92 | { KE_IGNORE, 0xe020, { KEY_MUTE } }, |
Seth Forshee | f1566f0 | 2011-08-01 15:46:10 -0500 | [diff] [blame] | 93 | |
| 94 | /* Shortcut and audio panel keys */ |
| 95 | { KE_IGNORE, 0xe025, { KEY_RESERVED } }, |
| 96 | { KE_IGNORE, 0xe026, { KEY_RESERVED } }, |
| 97 | |
Dmitry Torokhov | 890a7c8 | 2010-08-04 22:30:08 -0700 | [diff] [blame] | 98 | { KE_IGNORE, 0xe02e, { KEY_VOLUMEDOWN } }, |
| 99 | { KE_IGNORE, 0xe030, { KEY_VOLUMEUP } }, |
| 100 | { KE_IGNORE, 0xe033, { KEY_KBDILLUMUP } }, |
| 101 | { KE_IGNORE, 0xe034, { KEY_KBDILLUMDOWN } }, |
| 102 | { KE_IGNORE, 0xe03a, { KEY_CAPSLOCK } }, |
| 103 | { KE_IGNORE, 0xe045, { KEY_NUMLOCK } }, |
| 104 | { KE_IGNORE, 0xe046, { KEY_SCROLLLOCK } }, |
Seth Forshee | f1566f0 | 2011-08-01 15:46:10 -0500 | [diff] [blame] | 105 | { KE_IGNORE, 0xe0f7, { KEY_MUTE } }, |
| 106 | { KE_IGNORE, 0xe0f8, { KEY_VOLUMEDOWN } }, |
| 107 | { KE_IGNORE, 0xe0f9, { KEY_VOLUMEUP } }, |
Dmitry Torokhov | 890a7c8 | 2010-08-04 22:30:08 -0700 | [diff] [blame] | 108 | { KE_END, 0 } |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
Rezwanul Kabir | 5ea2559 | 2009-11-02 12:00:42 -0500 | [diff] [blame] | 111 | static bool dell_new_hk_type; |
| 112 | |
Dmitry Torokhov | 890a7c8 | 2010-08-04 22:30:08 -0700 | [diff] [blame] | 113 | struct dell_bios_keymap_entry { |
Rezwanul Kabir | 5ea2559 | 2009-11-02 12:00:42 -0500 | [diff] [blame] | 114 | u16 scancode; |
| 115 | u16 keycode; |
| 116 | }; |
| 117 | |
Dmitry Torokhov | 890a7c8 | 2010-08-04 22:30:08 -0700 | [diff] [blame] | 118 | struct dell_bios_hotkey_table { |
Rezwanul Kabir | 5ea2559 | 2009-11-02 12:00:42 -0500 | [diff] [blame] | 119 | struct dmi_header header; |
Dmitry Torokhov | 890a7c8 | 2010-08-04 22:30:08 -0700 | [diff] [blame] | 120 | struct dell_bios_keymap_entry keymap[]; |
Rezwanul Kabir | 5ea2559 | 2009-11-02 12:00:42 -0500 | [diff] [blame] | 121 | |
| 122 | }; |
| 123 | |
Dmitry Torokhov | 890a7c8 | 2010-08-04 22:30:08 -0700 | [diff] [blame] | 124 | static const struct dell_bios_hotkey_table *dell_bios_hotkey_table; |
Rezwanul Kabir | 5ea2559 | 2009-11-02 12:00:42 -0500 | [diff] [blame] | 125 | |
Andy Lutomirski | cbc61f1 | 2015-11-30 17:02:01 -0800 | [diff] [blame] | 126 | /* Uninitialized entries here are KEY_RESERVED == 0. */ |
Dmitry Torokhov | 890a7c8 | 2010-08-04 22:30:08 -0700 | [diff] [blame] | 127 | static const u16 bios_to_linux_keycode[256] __initconst = { |
Andy Lutomirski | 394cb77 | 2015-11-13 21:49:31 -0800 | [diff] [blame] | 128 | [0] = KEY_MEDIA, |
| 129 | [1] = KEY_NEXTSONG, |
| 130 | [2] = KEY_PLAYPAUSE, |
| 131 | [3] = KEY_PREVIOUSSONG, |
| 132 | [4] = KEY_STOPCD, |
| 133 | [5] = KEY_UNKNOWN, |
| 134 | [6] = KEY_UNKNOWN, |
| 135 | [7] = KEY_UNKNOWN, |
| 136 | [8] = KEY_WWW, |
| 137 | [9] = KEY_UNKNOWN, |
| 138 | [10] = KEY_VOLUMEDOWN, |
| 139 | [11] = KEY_MUTE, |
| 140 | [12] = KEY_VOLUMEUP, |
| 141 | [13] = KEY_UNKNOWN, |
| 142 | [14] = KEY_BATTERY, |
| 143 | [15] = KEY_EJECTCD, |
| 144 | [16] = KEY_UNKNOWN, |
| 145 | [17] = KEY_SLEEP, |
| 146 | [18] = KEY_PROG1, |
| 147 | [19] = KEY_BRIGHTNESSDOWN, |
| 148 | [20] = KEY_BRIGHTNESSUP, |
| 149 | [21] = KEY_UNKNOWN, |
| 150 | [22] = KEY_KBDILLUMTOGGLE, |
| 151 | [23] = KEY_UNKNOWN, |
| 152 | [24] = KEY_SWITCHVIDEOMODE, |
| 153 | [25] = KEY_UNKNOWN, |
| 154 | [26] = KEY_UNKNOWN, |
| 155 | [27] = KEY_SWITCHVIDEOMODE, |
| 156 | [28] = KEY_UNKNOWN, |
| 157 | [29] = KEY_UNKNOWN, |
| 158 | [30] = KEY_PROG2, |
| 159 | [31] = KEY_UNKNOWN, |
| 160 | [32] = KEY_UNKNOWN, |
| 161 | [33] = KEY_UNKNOWN, |
| 162 | [34] = KEY_UNKNOWN, |
| 163 | [35] = KEY_UNKNOWN, |
| 164 | [36] = KEY_UNKNOWN, |
| 165 | [37] = KEY_UNKNOWN, |
| 166 | [38] = KEY_MICMUTE, |
| 167 | [255] = KEY_PROG3, |
Rezwanul Kabir | 5ea2559 | 2009-11-02 12:00:42 -0500 | [diff] [blame] | 168 | }; |
| 169 | |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 170 | static struct input_dev *dell_wmi_input_dev; |
| 171 | |
Pali Rohár | 83fc44c | 2014-11-11 20:21:22 +0100 | [diff] [blame] | 172 | static void dell_wmi_process_key(int reported_key) |
| 173 | { |
| 174 | const struct key_entry *key; |
| 175 | |
| 176 | key = sparse_keymap_entry_from_scancode(dell_wmi_input_dev, |
| 177 | reported_key); |
| 178 | if (!key) { |
Andy Lutomirski | cbc61f1 | 2015-11-30 17:02:01 -0800 | [diff] [blame] | 179 | pr_info("Unknown key with scancode 0x%x pressed\n", |
| 180 | reported_key); |
Pali Rohár | 83fc44c | 2014-11-11 20:21:22 +0100 | [diff] [blame] | 181 | return; |
| 182 | } |
| 183 | |
| 184 | pr_debug("Key %x pressed\n", reported_key); |
| 185 | |
| 186 | /* Don't report brightness notifications that will also come via ACPI */ |
| 187 | if ((key->keycode == KEY_BRIGHTNESSUP || |
| 188 | key->keycode == KEY_BRIGHTNESSDOWN) && acpi_video) |
| 189 | return; |
| 190 | |
| 191 | sparse_keymap_report_entry(dell_wmi_input_dev, key, 1, true); |
| 192 | } |
| 193 | |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 194 | static void dell_wmi_notify(u32 value, void *context) |
| 195 | { |
| 196 | struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL }; |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 197 | union acpi_object *obj; |
Len Brown | fda11e6 | 2009-12-26 23:02:24 -0500 | [diff] [blame] | 198 | acpi_status status; |
Pali Rohár | 83fc44c | 2014-11-11 20:21:22 +0100 | [diff] [blame] | 199 | acpi_size buffer_size; |
| 200 | u16 *buffer_entry, *buffer_end; |
| 201 | int len, i; |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 202 | |
Len Brown | fda11e6 | 2009-12-26 23:02:24 -0500 | [diff] [blame] | 203 | status = wmi_get_event_data(value, &response); |
| 204 | if (status != AE_OK) { |
Pali Rohár | 83fc44c | 2014-11-11 20:21:22 +0100 | [diff] [blame] | 205 | pr_warn("bad event status 0x%x\n", status); |
Len Brown | fda11e6 | 2009-12-26 23:02:24 -0500 | [diff] [blame] | 206 | return; |
| 207 | } |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 208 | |
| 209 | obj = (union acpi_object *)response.pointer; |
Pali Rohár | 83fc44c | 2014-11-11 20:21:22 +0100 | [diff] [blame] | 210 | if (!obj) { |
| 211 | pr_warn("no response\n"); |
| 212 | return; |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 213 | } |
Pali Rohár | 83fc44c | 2014-11-11 20:21:22 +0100 | [diff] [blame] | 214 | |
| 215 | if (obj->type != ACPI_TYPE_BUFFER) { |
| 216 | pr_warn("bad response type %x\n", obj->type); |
| 217 | kfree(obj); |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | pr_debug("Received WMI event (%*ph)\n", |
| 222 | obj->buffer.length, obj->buffer.pointer); |
| 223 | |
| 224 | buffer_entry = (u16 *)obj->buffer.pointer; |
| 225 | buffer_size = obj->buffer.length/2; |
| 226 | |
| 227 | if (!dell_new_hk_type) { |
| 228 | if (buffer_size >= 3 && buffer_entry[1] == 0x0) |
| 229 | dell_wmi_process_key(buffer_entry[2]); |
| 230 | else if (buffer_size >= 2) |
| 231 | dell_wmi_process_key(buffer_entry[1]); |
| 232 | else |
| 233 | pr_info("Received unknown WMI event\n"); |
| 234 | kfree(obj); |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | buffer_end = buffer_entry + buffer_size; |
| 239 | |
Pali Rohár | 481fe5b | 2016-01-04 22:26:36 +0100 | [diff] [blame^] | 240 | /* |
| 241 | * BIOS/ACPI on devices with WMI interface version 0 does not clear |
| 242 | * buffer before filling it. So next time when BIOS/ACPI send WMI event |
| 243 | * which is smaller as previous then it contains garbage in buffer from |
| 244 | * previous event. |
| 245 | * |
| 246 | * BIOS/ACPI on devices with WMI interface version 1 clears buffer and |
| 247 | * sometimes send more events in buffer at one call. |
| 248 | * |
| 249 | * So to prevent reading garbage from buffer we will process only first |
| 250 | * one event on devices with WMI interface version 0. |
| 251 | */ |
| 252 | if (dell_wmi_interface_version == 0 && buffer_entry < buffer_end) |
| 253 | if (buffer_end > buffer_entry + buffer_entry[0] + 1) |
| 254 | buffer_end = buffer_entry + buffer_entry[0] + 1; |
| 255 | |
Pali Rohár | 83fc44c | 2014-11-11 20:21:22 +0100 | [diff] [blame] | 256 | while (buffer_entry < buffer_end) { |
| 257 | |
| 258 | len = buffer_entry[0]; |
| 259 | if (len == 0) |
| 260 | break; |
| 261 | |
| 262 | len++; |
| 263 | |
| 264 | if (buffer_entry + len > buffer_end) { |
| 265 | pr_warn("Invalid length of WMI event\n"); |
| 266 | break; |
| 267 | } |
| 268 | |
| 269 | pr_debug("Process buffer (%*ph)\n", len*2, buffer_entry); |
| 270 | |
| 271 | switch (buffer_entry[1]) { |
| 272 | case 0x00: |
| 273 | for (i = 2; i < len; ++i) { |
| 274 | switch (buffer_entry[i]) { |
| 275 | case 0xe043: |
| 276 | /* NIC Link is Up */ |
| 277 | pr_debug("NIC Link is Up\n"); |
| 278 | break; |
| 279 | case 0xe044: |
| 280 | /* NIC Link is Down */ |
| 281 | pr_debug("NIC Link is Down\n"); |
| 282 | break; |
| 283 | case 0xe045: |
| 284 | /* Unknown event but defined in DSDT */ |
| 285 | default: |
| 286 | /* Unknown event */ |
| 287 | pr_info("Unknown WMI event type 0x00: " |
| 288 | "0x%x\n", (int)buffer_entry[i]); |
| 289 | break; |
| 290 | } |
| 291 | } |
| 292 | break; |
| 293 | case 0x10: |
| 294 | /* Keys pressed */ |
| 295 | for (i = 2; i < len; ++i) |
| 296 | dell_wmi_process_key(buffer_entry[i]); |
| 297 | break; |
| 298 | case 0x11: |
| 299 | for (i = 2; i < len; ++i) { |
| 300 | switch (buffer_entry[i]) { |
| 301 | case 0xfff0: |
| 302 | /* Battery unplugged */ |
| 303 | pr_debug("Battery unplugged\n"); |
| 304 | break; |
| 305 | case 0xfff1: |
| 306 | /* Battery inserted */ |
| 307 | pr_debug("Battery inserted\n"); |
| 308 | break; |
| 309 | case 0x01e1: |
| 310 | case 0x02ea: |
| 311 | case 0x02eb: |
| 312 | case 0x02ec: |
| 313 | case 0x02f6: |
| 314 | /* Keyboard backlight level changed */ |
| 315 | pr_debug("Keyboard backlight level " |
| 316 | "changed\n"); |
| 317 | break; |
| 318 | default: |
| 319 | /* Unknown event */ |
| 320 | pr_info("Unknown WMI event type 0x11: " |
| 321 | "0x%x\n", (int)buffer_entry[i]); |
| 322 | break; |
| 323 | } |
| 324 | } |
| 325 | break; |
| 326 | default: |
| 327 | /* Unknown event */ |
| 328 | pr_info("Unknown WMI event type 0x%x\n", |
| 329 | (int)buffer_entry[1]); |
| 330 | break; |
| 331 | } |
| 332 | |
| 333 | buffer_entry += len; |
| 334 | |
| 335 | } |
| 336 | |
Anisse Astier | 3e9b988 | 2009-12-04 10:10:09 +0100 | [diff] [blame] | 337 | kfree(obj); |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Dmitry Torokhov | 890a7c8 | 2010-08-04 22:30:08 -0700 | [diff] [blame] | 340 | static const struct key_entry * __init dell_wmi_prepare_new_keymap(void) |
Rezwanul Kabir | 5ea2559 | 2009-11-02 12:00:42 -0500 | [diff] [blame] | 341 | { |
Dmitry Torokhov | 890a7c8 | 2010-08-04 22:30:08 -0700 | [diff] [blame] | 342 | int hotkey_num = (dell_bios_hotkey_table->header.length - 4) / |
| 343 | sizeof(struct dell_bios_keymap_entry); |
| 344 | struct key_entry *keymap; |
Rezwanul Kabir | 5ea2559 | 2009-11-02 12:00:42 -0500 | [diff] [blame] | 345 | int i; |
Rezwanul Kabir | 5ea2559 | 2009-11-02 12:00:42 -0500 | [diff] [blame] | 346 | |
Dmitry Torokhov | 890a7c8 | 2010-08-04 22:30:08 -0700 | [diff] [blame] | 347 | keymap = kcalloc(hotkey_num + 1, sizeof(struct key_entry), GFP_KERNEL); |
| 348 | if (!keymap) |
| 349 | return NULL; |
Rezwanul Kabir | 5ea2559 | 2009-11-02 12:00:42 -0500 | [diff] [blame] | 350 | |
| 351 | for (i = 0; i < hotkey_num; i++) { |
Dmitry Torokhov | 890a7c8 | 2010-08-04 22:30:08 -0700 | [diff] [blame] | 352 | const struct dell_bios_keymap_entry *bios_entry = |
| 353 | &dell_bios_hotkey_table->keymap[i]; |
Andy Lutomirski | cbc61f1 | 2015-11-30 17:02:01 -0800 | [diff] [blame] | 354 | |
| 355 | /* Uninitialized entries are 0 aka KEY_RESERVED. */ |
| 356 | u16 keycode = (bios_entry->keycode < |
| 357 | ARRAY_SIZE(bios_to_linux_keycode)) ? |
| 358 | bios_to_linux_keycode[bios_entry->keycode] : |
| 359 | KEY_RESERVED; |
| 360 | |
| 361 | /* |
| 362 | * Log if we find an entry in the DMI table that we don't |
| 363 | * understand. If this happens, we should figure out what |
| 364 | * the entry means and add it to bios_to_linux_keycode. |
| 365 | */ |
| 366 | if (keycode == KEY_RESERVED) { |
| 367 | pr_info("firmware scancode 0x%x maps to unrecognized keycode 0x%x\n", |
| 368 | bios_entry->scancode, bios_entry->keycode); |
| 369 | continue; |
| 370 | } |
Gabriele Mazzotta | 8cb8e63 | 2014-12-04 00:16:23 +0100 | [diff] [blame] | 371 | |
| 372 | if (keycode == KEY_KBDILLUMTOGGLE) |
| 373 | keymap[i].type = KE_IGNORE; |
| 374 | else |
| 375 | keymap[i].type = KE_KEY; |
| 376 | keymap[i].code = bios_entry->scancode; |
| 377 | keymap[i].keycode = keycode; |
Rezwanul Kabir | 5ea2559 | 2009-11-02 12:00:42 -0500 | [diff] [blame] | 378 | } |
| 379 | |
Dmitry Torokhov | 890a7c8 | 2010-08-04 22:30:08 -0700 | [diff] [blame] | 380 | keymap[hotkey_num].type = KE_END; |
Rezwanul Kabir | 5ea2559 | 2009-11-02 12:00:42 -0500 | [diff] [blame] | 381 | |
Dmitry Torokhov | 890a7c8 | 2010-08-04 22:30:08 -0700 | [diff] [blame] | 382 | return keymap; |
Rezwanul Kabir | 5ea2559 | 2009-11-02 12:00:42 -0500 | [diff] [blame] | 383 | } |
| 384 | |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 385 | static int __init dell_wmi_input_setup(void) |
| 386 | { |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 387 | int err; |
| 388 | |
| 389 | dell_wmi_input_dev = input_allocate_device(); |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 390 | if (!dell_wmi_input_dev) |
| 391 | return -ENOMEM; |
| 392 | |
| 393 | dell_wmi_input_dev->name = "Dell WMI hotkeys"; |
| 394 | dell_wmi_input_dev->phys = "wmi/input0"; |
| 395 | dell_wmi_input_dev->id.bustype = BUS_HOST; |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 396 | |
Dmitry Torokhov | 890a7c8 | 2010-08-04 22:30:08 -0700 | [diff] [blame] | 397 | if (dell_new_hk_type) { |
| 398 | const struct key_entry *keymap = dell_wmi_prepare_new_keymap(); |
| 399 | if (!keymap) { |
| 400 | err = -ENOMEM; |
| 401 | goto err_free_dev; |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 402 | } |
Dmitry Torokhov | 890a7c8 | 2010-08-04 22:30:08 -0700 | [diff] [blame] | 403 | |
| 404 | err = sparse_keymap_setup(dell_wmi_input_dev, keymap, NULL); |
| 405 | |
| 406 | /* |
| 407 | * Sparse keymap library makes a copy of keymap so we |
| 408 | * don't need the original one that was allocated. |
| 409 | */ |
| 410 | kfree(keymap); |
| 411 | } else { |
| 412 | err = sparse_keymap_setup(dell_wmi_input_dev, |
| 413 | dell_wmi_legacy_keymap, NULL); |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 414 | } |
Dmitry Torokhov | 890a7c8 | 2010-08-04 22:30:08 -0700 | [diff] [blame] | 415 | if (err) |
| 416 | goto err_free_dev; |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 417 | |
| 418 | err = input_register_device(dell_wmi_input_dev); |
Dmitry Torokhov | 890a7c8 | 2010-08-04 22:30:08 -0700 | [diff] [blame] | 419 | if (err) |
| 420 | goto err_free_keymap; |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 421 | |
| 422 | return 0; |
Dmitry Torokhov | 890a7c8 | 2010-08-04 22:30:08 -0700 | [diff] [blame] | 423 | |
| 424 | err_free_keymap: |
| 425 | sparse_keymap_free(dell_wmi_input_dev); |
| 426 | err_free_dev: |
| 427 | input_free_device(dell_wmi_input_dev); |
| 428 | return err; |
| 429 | } |
| 430 | |
| 431 | static void dell_wmi_input_destroy(void) |
| 432 | { |
| 433 | sparse_keymap_free(dell_wmi_input_dev); |
| 434 | input_unregister_device(dell_wmi_input_dev); |
| 435 | } |
| 436 | |
| 437 | static void __init find_hk_type(const struct dmi_header *dm, void *dummy) |
| 438 | { |
| 439 | if (dm->type == 0xb2 && dm->length > 6) { |
| 440 | dell_new_hk_type = true; |
| 441 | dell_bios_hotkey_table = |
| 442 | container_of(dm, struct dell_bios_hotkey_table, header); |
| 443 | } |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Pali Rohár | 595773a4 | 2016-01-04 22:26:35 +0100 | [diff] [blame] | 446 | /* |
| 447 | * Descriptor buffer is 128 byte long and contains: |
| 448 | * |
| 449 | * Name Offset Length Value |
| 450 | * Vendor Signature 0 4 "DELL" |
| 451 | * Object Signature 4 4 " WMI" |
| 452 | * WMI Interface Version 8 4 <version> |
| 453 | * WMI buffer length 12 4 4096 |
| 454 | */ |
| 455 | static int __init dell_wmi_check_descriptor_buffer(void) |
| 456 | { |
| 457 | struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 458 | union acpi_object *obj; |
| 459 | acpi_status status; |
| 460 | u32 *buffer; |
| 461 | |
| 462 | status = wmi_query_block(DELL_DESCRIPTOR_GUID, 0, &out); |
| 463 | if (ACPI_FAILURE(status)) { |
| 464 | pr_err("Cannot read Dell descriptor buffer - %d\n", status); |
| 465 | return status; |
| 466 | } |
| 467 | |
| 468 | obj = (union acpi_object *)out.pointer; |
| 469 | if (!obj) { |
| 470 | pr_err("Dell descriptor buffer is empty\n"); |
| 471 | return -EINVAL; |
| 472 | } |
| 473 | |
| 474 | if (obj->type != ACPI_TYPE_BUFFER) { |
| 475 | pr_err("Cannot read Dell descriptor buffer\n"); |
| 476 | kfree(obj); |
| 477 | return -EINVAL; |
| 478 | } |
| 479 | |
| 480 | if (obj->buffer.length != 128) { |
| 481 | pr_err("Dell descriptor buffer has invalid length (%d)\n", |
| 482 | obj->buffer.length); |
| 483 | if (obj->buffer.length < 16) { |
| 484 | kfree(obj); |
| 485 | return -EINVAL; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | buffer = (u32 *)obj->buffer.pointer; |
| 490 | |
| 491 | if (buffer[0] != 0x4C4C4544 && buffer[1] != 0x494D5720) |
| 492 | pr_warn("Dell descriptor buffer has invalid signature (%*ph)\n", |
| 493 | 8, buffer); |
| 494 | |
| 495 | if (buffer[2] != 0 && buffer[2] != 1) |
| 496 | pr_warn("Dell descriptor buffer has unknown version (%d)\n", |
| 497 | buffer[2]); |
| 498 | |
| 499 | if (buffer[3] != 4096) |
| 500 | pr_warn("Dell descriptor buffer has invalid buffer length (%d)\n", |
| 501 | buffer[3]); |
| 502 | |
| 503 | dell_wmi_interface_version = buffer[2]; |
| 504 | |
| 505 | pr_info("Detected Dell WMI interface version %u\n", |
| 506 | dell_wmi_interface_version); |
| 507 | |
| 508 | kfree(obj); |
| 509 | return 0; |
| 510 | } |
| 511 | |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 512 | static int __init dell_wmi_init(void) |
| 513 | { |
| 514 | int err; |
Len Brown | abb631b | 2009-12-26 21:51:38 -0500 | [diff] [blame] | 515 | acpi_status status; |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 516 | |
Pali Rohár | 595773a4 | 2016-01-04 22:26:35 +0100 | [diff] [blame] | 517 | if (!wmi_has_guid(DELL_EVENT_GUID) || |
| 518 | !wmi_has_guid(DELL_DESCRIPTOR_GUID)) { |
| 519 | pr_warn("Dell WMI GUID were not found\n"); |
Dmitry Torokhov | 1fdd407 | 2009-12-17 22:19:42 -0800 | [diff] [blame] | 520 | return -ENODEV; |
| 521 | } |
| 522 | |
Pali Rohár | 595773a4 | 2016-01-04 22:26:35 +0100 | [diff] [blame] | 523 | err = dell_wmi_check_descriptor_buffer(); |
| 524 | if (err) |
| 525 | return err; |
| 526 | |
Dmitry Torokhov | 1fdd407 | 2009-12-17 22:19:42 -0800 | [diff] [blame] | 527 | dmi_walk(find_hk_type, NULL); |
Hans de Goede | 18bd769 | 2015-06-16 16:28:01 +0200 | [diff] [blame] | 528 | acpi_video = acpi_video_get_backlight_type() != acpi_backlight_vendor; |
Dmitry Torokhov | 1fdd407 | 2009-12-17 22:19:42 -0800 | [diff] [blame] | 529 | |
| 530 | err = dell_wmi_input_setup(); |
Dmitry Torokhov | 890a7c8 | 2010-08-04 22:30:08 -0700 | [diff] [blame] | 531 | if (err) |
Dmitry Torokhov | 1fdd407 | 2009-12-17 22:19:42 -0800 | [diff] [blame] | 532 | return err; |
| 533 | |
Len Brown | abb631b | 2009-12-26 21:51:38 -0500 | [diff] [blame] | 534 | status = wmi_install_notify_handler(DELL_EVENT_GUID, |
Dmitry Torokhov | 1fdd407 | 2009-12-17 22:19:42 -0800 | [diff] [blame] | 535 | dell_wmi_notify, NULL); |
Len Brown | abb631b | 2009-12-26 21:51:38 -0500 | [diff] [blame] | 536 | if (ACPI_FAILURE(status)) { |
Dmitry Torokhov | 890a7c8 | 2010-08-04 22:30:08 -0700 | [diff] [blame] | 537 | dell_wmi_input_destroy(); |
Joe Perches | eb88952 | 2011-03-29 15:21:37 -0700 | [diff] [blame] | 538 | pr_err("Unable to register notify handler - %d\n", status); |
Len Brown | abb631b | 2009-12-26 21:51:38 -0500 | [diff] [blame] | 539 | return -ENODEV; |
Dmitry Torokhov | 1fdd407 | 2009-12-17 22:19:42 -0800 | [diff] [blame] | 540 | } |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 541 | |
| 542 | return 0; |
| 543 | } |
Dmitry Torokhov | 890a7c8 | 2010-08-04 22:30:08 -0700 | [diff] [blame] | 544 | module_init(dell_wmi_init); |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 545 | |
| 546 | static void __exit dell_wmi_exit(void) |
| 547 | { |
Dmitry Torokhov | 1fdd407 | 2009-12-17 22:19:42 -0800 | [diff] [blame] | 548 | wmi_remove_notify_handler(DELL_EVENT_GUID); |
Dmitry Torokhov | 890a7c8 | 2010-08-04 22:30:08 -0700 | [diff] [blame] | 549 | dell_wmi_input_destroy(); |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 550 | } |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 551 | module_exit(dell_wmi_exit); |