blob: bdf483babb4388936c982dc0f602caf84a235672 [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>
5 *
6 * Portions based on wistron_btns.c:
7 * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
8 * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
9 * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
Joe Percheseb889522011-03-29 15:21:37 -070026#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
Matthew Garrett0b3f6102009-01-09 20:17:11 +000028#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Matthew Garrett0b3f6102009-01-09 20:17:11 +000032#include <linux/types.h>
33#include <linux/input.h>
Dmitry Torokhov890a7c82010-08-04 22:30:08 -070034#include <linux/input/sparse-keymap.h>
Matthew Garrett0b3f6102009-01-09 20:17:11 +000035#include <linux/acpi.h>
36#include <linux/string.h>
Rezwanul Kabir5ea25592009-11-02 12:00:42 -050037#include <linux/dmi.h>
Matthew Garrett0b3f6102009-01-09 20:17:11 +000038
39MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
40MODULE_DESCRIPTION("Dell laptop WMI hotkeys driver");
41MODULE_LICENSE("GPL");
42
43#define DELL_EVENT_GUID "9DBB5994-A997-11DA-B012-B622A1EF5492"
44
Rezwanul Kabir5ea25592009-11-02 12:00:42 -050045static int acpi_video;
46
Matthew Garrett0b3f6102009-01-09 20:17:11 +000047MODULE_ALIAS("wmi:"DELL_EVENT_GUID);
48
Mario Limonciello5cab0092009-06-10 19:40:47 +000049/*
50 * Certain keys are flagged as KE_IGNORE. All of these are either
51 * notifications (rather than requests for change) or are also sent
52 * via the keyboard controller so should not be sent again.
53 */
Matthew Garrett0b3f6102009-01-09 20:17:11 +000054
Dmitry Torokhov890a7c82010-08-04 22:30:08 -070055static const struct key_entry dell_wmi_legacy_keymap[] __initconst = {
Seth Forsheef1566f02011-08-01 15:46:10 -050056 { KE_IGNORE, 0x003a, { KEY_CAPSLOCK } },
57
Dmitry Torokhov890a7c82010-08-04 22:30:08 -070058 { KE_KEY, 0xe045, { KEY_PROG1 } },
59 { KE_KEY, 0xe009, { KEY_EJECTCD } },
Mario Limonciello5cab0092009-06-10 19:40:47 +000060
61 /* These also contain the brightness level at offset 6 */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -070062 { KE_KEY, 0xe006, { KEY_BRIGHTNESSUP } },
63 { KE_KEY, 0xe005, { KEY_BRIGHTNESSDOWN } },
Mario Limonciello5cab0092009-06-10 19:40:47 +000064
65 /* Battery health status button */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -070066 { KE_KEY, 0xe007, { KEY_BATTERY } },
Mario Limonciello5cab0092009-06-10 19:40:47 +000067
68 /* This is actually for all radios. Although physically a
69 * switch, the notification does not provide an indication of
70 * state and so it should be reported as a key */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -070071 { KE_KEY, 0xe008, { KEY_WLAN } },
Mario Limonciello5cab0092009-06-10 19:40:47 +000072
73 /* The next device is at offset 6, the active devices are at
74 offset 8 and the attached devices at offset 10 */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -070075 { KE_KEY, 0xe00b, { KEY_SWITCHVIDEOMODE } },
Mario Limonciello5cab0092009-06-10 19:40:47 +000076
Dmitry Torokhov890a7c82010-08-04 22:30:08 -070077 { KE_IGNORE, 0xe00c, { KEY_KBDILLUMTOGGLE } },
Mario Limonciello5cab0092009-06-10 19:40:47 +000078
79 /* BIOS error detected */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -070080 { KE_IGNORE, 0xe00d, { KEY_RESERVED } },
Mario Limonciello5cab0092009-06-10 19:40:47 +000081
82 /* Wifi Catcher */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -070083 { KE_KEY, 0xe011, {KEY_PROG2 } },
Mario Limonciello5cab0092009-06-10 19:40:47 +000084
85 /* Ambient light sensor toggle */
Dmitry Torokhov890a7c82010-08-04 22:30:08 -070086 { KE_IGNORE, 0xe013, { KEY_RESERVED } },
Mario Limonciello5cab0092009-06-10 19:40:47 +000087
Dmitry Torokhov890a7c82010-08-04 22:30:08 -070088 { KE_IGNORE, 0xe020, { KEY_MUTE } },
Seth Forsheef1566f02011-08-01 15:46:10 -050089
90 /* Shortcut and audio panel keys */
91 { KE_IGNORE, 0xe025, { KEY_RESERVED } },
92 { KE_IGNORE, 0xe026, { KEY_RESERVED } },
93
Dmitry Torokhov890a7c82010-08-04 22:30:08 -070094 { KE_IGNORE, 0xe02e, { KEY_VOLUMEDOWN } },
95 { KE_IGNORE, 0xe030, { KEY_VOLUMEUP } },
96 { KE_IGNORE, 0xe033, { KEY_KBDILLUMUP } },
97 { KE_IGNORE, 0xe034, { KEY_KBDILLUMDOWN } },
98 { KE_IGNORE, 0xe03a, { KEY_CAPSLOCK } },
99 { KE_IGNORE, 0xe045, { KEY_NUMLOCK } },
100 { KE_IGNORE, 0xe046, { KEY_SCROLLLOCK } },
Seth Forsheef1566f02011-08-01 15:46:10 -0500101 { KE_IGNORE, 0xe0f7, { KEY_MUTE } },
102 { KE_IGNORE, 0xe0f8, { KEY_VOLUMEDOWN } },
103 { KE_IGNORE, 0xe0f9, { KEY_VOLUMEUP } },
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700104 { KE_END, 0 }
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000105};
106
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500107static bool dell_new_hk_type;
108
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700109struct dell_bios_keymap_entry {
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500110 u16 scancode;
111 u16 keycode;
112};
113
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700114struct dell_bios_hotkey_table {
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500115 struct dmi_header header;
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700116 struct dell_bios_keymap_entry keymap[];
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500117
118};
119
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700120static const struct dell_bios_hotkey_table *dell_bios_hotkey_table;
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500121
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700122static const u16 bios_to_linux_keycode[256] __initconst = {
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500123
124 KEY_MEDIA, KEY_NEXTSONG, KEY_PLAYPAUSE, KEY_PREVIOUSSONG,
125 KEY_STOPCD, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
126 KEY_WWW, KEY_UNKNOWN, KEY_VOLUMEDOWN, KEY_MUTE,
127 KEY_VOLUMEUP, KEY_UNKNOWN, KEY_BATTERY, KEY_EJECTCD,
128 KEY_UNKNOWN, KEY_SLEEP, KEY_PROG1, KEY_BRIGHTNESSDOWN,
129 KEY_BRIGHTNESSUP, KEY_UNKNOWN, KEY_KBDILLUMTOGGLE,
130 KEY_UNKNOWN, KEY_SWITCHVIDEOMODE, KEY_UNKNOWN, KEY_UNKNOWN,
131 KEY_SWITCHVIDEOMODE, KEY_UNKNOWN, KEY_UNKNOWN, KEY_PROG2,
132 KEY_UNKNOWN, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
133 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
134 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
135 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
136 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
137 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
138 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
139 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
140 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
141 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
142 KEY_PROG3
143};
144
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000145static struct input_dev *dell_wmi_input_dev;
146
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000147static void dell_wmi_notify(u32 value, void *context)
148{
149 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000150 union acpi_object *obj;
Len Brownfda11e62009-12-26 23:02:24 -0500151 acpi_status status;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000152
Len Brownfda11e62009-12-26 23:02:24 -0500153 status = wmi_get_event_data(value, &response);
154 if (status != AE_OK) {
Joe Percheseb889522011-03-29 15:21:37 -0700155 pr_info("bad event status 0x%x\n", status);
Len Brownfda11e62009-12-26 23:02:24 -0500156 return;
157 }
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000158
159 obj = (union acpi_object *)response.pointer;
160
161 if (obj && obj->type == ACPI_TYPE_BUFFER) {
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700162 const struct key_entry *key;
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500163 int reported_key;
164 u16 *buffer_entry = (u16 *)obj->buffer.pointer;
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700165
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500166 if (dell_new_hk_type && (buffer_entry[1] != 0x10)) {
Joe Percheseb889522011-03-29 15:21:37 -0700167 pr_info("Received unknown WMI event (0x%x)\n",
168 buffer_entry[1]);
Yong Wanga0624a92010-04-05 21:09:40 +0800169 kfree(obj);
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500170 return;
171 }
172
Islam Amerd5164db2010-06-24 13:39:47 -0400173 if (dell_new_hk_type || buffer_entry[1] == 0x0)
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500174 reported_key = (int)buffer_entry[2];
175 else
176 reported_key = (int)buffer_entry[1] & 0xffff;
177
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700178 key = sparse_keymap_entry_from_scancode(dell_wmi_input_dev,
179 reported_key);
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500180 if (!key) {
Joe Percheseb889522011-03-29 15:21:37 -0700181 pr_info("Unknown key %x pressed\n", reported_key);
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500182 } else if ((key->keycode == KEY_BRIGHTNESSUP ||
183 key->keycode == KEY_BRIGHTNESSDOWN) && acpi_video) {
184 /* Don't report brightness notifications that will also
185 * come via ACPI */
Yong Wanga0624a92010-04-05 21:09:40 +0800186 ;
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500187 } else {
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700188 sparse_keymap_report_entry(dell_wmi_input_dev, key,
189 1, true);
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500190 }
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000191 }
Anisse Astier3e9b9882009-12-04 10:10:09 +0100192 kfree(obj);
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000193}
194
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700195static const struct key_entry * __init dell_wmi_prepare_new_keymap(void)
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500196{
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700197 int hotkey_num = (dell_bios_hotkey_table->header.length - 4) /
198 sizeof(struct dell_bios_keymap_entry);
199 struct key_entry *keymap;
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500200 int i;
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500201
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700202 keymap = kcalloc(hotkey_num + 1, sizeof(struct key_entry), GFP_KERNEL);
203 if (!keymap)
204 return NULL;
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500205
206 for (i = 0; i < hotkey_num; i++) {
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700207 const struct dell_bios_keymap_entry *bios_entry =
208 &dell_bios_hotkey_table->keymap[i];
209 keymap[i].type = KE_KEY;
210 keymap[i].code = bios_entry->scancode;
211 keymap[i].keycode = bios_entry->keycode < 256 ?
212 bios_to_linux_keycode[bios_entry->keycode] :
213 KEY_RESERVED;
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500214 }
215
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700216 keymap[hotkey_num].type = KE_END;
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500217
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700218 return keymap;
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500219}
220
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000221static int __init dell_wmi_input_setup(void)
222{
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000223 int err;
224
225 dell_wmi_input_dev = input_allocate_device();
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000226 if (!dell_wmi_input_dev)
227 return -ENOMEM;
228
229 dell_wmi_input_dev->name = "Dell WMI hotkeys";
230 dell_wmi_input_dev->phys = "wmi/input0";
231 dell_wmi_input_dev->id.bustype = BUS_HOST;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000232
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700233 if (dell_new_hk_type) {
234 const struct key_entry *keymap = dell_wmi_prepare_new_keymap();
235 if (!keymap) {
236 err = -ENOMEM;
237 goto err_free_dev;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000238 }
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700239
240 err = sparse_keymap_setup(dell_wmi_input_dev, keymap, NULL);
241
242 /*
243 * Sparse keymap library makes a copy of keymap so we
244 * don't need the original one that was allocated.
245 */
246 kfree(keymap);
247 } else {
248 err = sparse_keymap_setup(dell_wmi_input_dev,
249 dell_wmi_legacy_keymap, NULL);
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000250 }
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700251 if (err)
252 goto err_free_dev;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000253
254 err = input_register_device(dell_wmi_input_dev);
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700255 if (err)
256 goto err_free_keymap;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000257
258 return 0;
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700259
260 err_free_keymap:
261 sparse_keymap_free(dell_wmi_input_dev);
262 err_free_dev:
263 input_free_device(dell_wmi_input_dev);
264 return err;
265}
266
267static void dell_wmi_input_destroy(void)
268{
269 sparse_keymap_free(dell_wmi_input_dev);
270 input_unregister_device(dell_wmi_input_dev);
271}
272
273static void __init find_hk_type(const struct dmi_header *dm, void *dummy)
274{
275 if (dm->type == 0xb2 && dm->length > 6) {
276 dell_new_hk_type = true;
277 dell_bios_hotkey_table =
278 container_of(dm, struct dell_bios_hotkey_table, header);
279 }
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000280}
281
282static int __init dell_wmi_init(void)
283{
284 int err;
Len Brownabb631b2009-12-26 21:51:38 -0500285 acpi_status status;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000286
Dmitry Torokhov7a9568f2009-12-25 11:49:35 -0800287 if (!wmi_has_guid(DELL_EVENT_GUID)) {
Joe Percheseb889522011-03-29 15:21:37 -0700288 pr_warn("No known WMI GUID found\n");
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800289 return -ENODEV;
290 }
291
292 dmi_walk(find_hk_type, NULL);
293 acpi_video = acpi_video_backlight_support();
294
295 err = dell_wmi_input_setup();
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700296 if (err)
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800297 return err;
298
Len Brownabb631b2009-12-26 21:51:38 -0500299 status = wmi_install_notify_handler(DELL_EVENT_GUID,
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800300 dell_wmi_notify, NULL);
Len Brownabb631b2009-12-26 21:51:38 -0500301 if (ACPI_FAILURE(status)) {
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700302 dell_wmi_input_destroy();
Joe Percheseb889522011-03-29 15:21:37 -0700303 pr_err("Unable to register notify handler - %d\n", status);
Len Brownabb631b2009-12-26 21:51:38 -0500304 return -ENODEV;
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800305 }
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000306
307 return 0;
308}
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700309module_init(dell_wmi_init);
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000310
311static void __exit dell_wmi_exit(void)
312{
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800313 wmi_remove_notify_handler(DELL_EVENT_GUID);
Dmitry Torokhov890a7c82010-08-04 22:30:08 -0700314 dell_wmi_input_destroy();
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000315}
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000316module_exit(dell_wmi_exit);