blob: 6ba6c30e5bb67723ae27f421795af51a1f8e0857 [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
26#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Matthew Garrett0b3f6102009-01-09 20:17:11 +000030#include <linux/types.h>
31#include <linux/input.h>
32#include <acpi/acpi_drivers.h>
33#include <linux/acpi.h>
34#include <linux/string.h>
Rezwanul Kabir5ea25592009-11-02 12:00:42 -050035#include <linux/dmi.h>
Matthew Garrett0b3f6102009-01-09 20:17:11 +000036
37MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
38MODULE_DESCRIPTION("Dell laptop WMI hotkeys driver");
39MODULE_LICENSE("GPL");
40
41#define DELL_EVENT_GUID "9DBB5994-A997-11DA-B012-B622A1EF5492"
42
Rezwanul Kabir5ea25592009-11-02 12:00:42 -050043static int acpi_video;
44
Matthew Garrett0b3f6102009-01-09 20:17:11 +000045MODULE_ALIAS("wmi:"DELL_EVENT_GUID);
46
47struct key_entry {
48 char type; /* See KE_* below */
49 u16 code;
50 u16 keycode;
51};
52
Mario Limonciello5cab0092009-06-10 19:40:47 +000053enum { KE_KEY, KE_SW, KE_IGNORE, KE_END };
54
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 Garrett0b3f6102009-01-09 20:17:11 +000060
Rezwanul Kabir5ea25592009-11-02 12:00:42 -050061static struct key_entry dell_legacy_wmi_keymap[] = {
Matthew Garrett0b3f6102009-01-09 20:17:11 +000062 {KE_KEY, 0xe045, KEY_PROG1},
Mario Limonciello5cab0092009-06-10 19:40:47 +000063 {KE_KEY, 0xe009, KEY_EJECTCD},
64
65 /* These also contain the brightness level at offset 6 */
66 {KE_KEY, 0xe006, KEY_BRIGHTNESSUP},
67 {KE_KEY, 0xe005, KEY_BRIGHTNESSDOWN},
68
69 /* Battery health status button */
70 {KE_KEY, 0xe007, KEY_BATTERY},
71
72 /* This is actually for all radios. Although physically a
73 * switch, the notification does not provide an indication of
74 * state and so it should be reported as a key */
75 {KE_KEY, 0xe008, KEY_WLAN},
76
77 /* The next device is at offset 6, the active devices are at
78 offset 8 and the attached devices at offset 10 */
Rezwanul Kabir5ea25592009-11-02 12:00:42 -050079 {KE_KEY, 0xe00b, KEY_SWITCHVIDEOMODE},
Mario Limonciello5cab0092009-06-10 19:40:47 +000080
81 {KE_IGNORE, 0xe00c, KEY_KBDILLUMTOGGLE},
82
83 /* BIOS error detected */
84 {KE_IGNORE, 0xe00d, KEY_RESERVED},
85
86 /* Wifi Catcher */
87 {KE_KEY, 0xe011, KEY_PROG2},
88
89 /* Ambient light sensor toggle */
90 {KE_IGNORE, 0xe013, KEY_RESERVED},
91
92 {KE_IGNORE, 0xe020, KEY_MUTE},
93 {KE_IGNORE, 0xe02e, KEY_VOLUMEDOWN},
94 {KE_IGNORE, 0xe030, KEY_VOLUMEUP},
95 {KE_IGNORE, 0xe033, KEY_KBDILLUMUP},
96 {KE_IGNORE, 0xe034, KEY_KBDILLUMDOWN},
97 {KE_IGNORE, 0xe03a, KEY_CAPSLOCK},
98 {KE_IGNORE, 0xe045, KEY_NUMLOCK},
99 {KE_IGNORE, 0xe046, KEY_SCROLLLOCK},
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000100 {KE_END, 0}
101};
102
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500103static bool dell_new_hk_type;
104
105struct dell_new_keymap_entry {
106 u16 scancode;
107 u16 keycode;
108};
109
110struct dell_hotkey_table {
111 struct dmi_header header;
112 struct dell_new_keymap_entry keymap[];
113
114};
115
116static struct key_entry *dell_new_wmi_keymap;
117
118static u16 bios_to_linux_keycode[256] = {
119
120 KEY_MEDIA, KEY_NEXTSONG, KEY_PLAYPAUSE, KEY_PREVIOUSSONG,
121 KEY_STOPCD, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
122 KEY_WWW, KEY_UNKNOWN, KEY_VOLUMEDOWN, KEY_MUTE,
123 KEY_VOLUMEUP, KEY_UNKNOWN, KEY_BATTERY, KEY_EJECTCD,
124 KEY_UNKNOWN, KEY_SLEEP, KEY_PROG1, KEY_BRIGHTNESSDOWN,
125 KEY_BRIGHTNESSUP, KEY_UNKNOWN, KEY_KBDILLUMTOGGLE,
126 KEY_UNKNOWN, KEY_SWITCHVIDEOMODE, KEY_UNKNOWN, KEY_UNKNOWN,
127 KEY_SWITCHVIDEOMODE, KEY_UNKNOWN, KEY_UNKNOWN, KEY_PROG2,
128 KEY_UNKNOWN, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
129 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
130 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
131 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
132 0, 0, 0, 0, 0, 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,
138 KEY_PROG3
139};
140
141
142static struct key_entry *dell_wmi_keymap = dell_legacy_wmi_keymap;
143
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000144static struct input_dev *dell_wmi_input_dev;
145
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800146static struct key_entry *dell_wmi_get_entry_by_scancode(unsigned int code)
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000147{
148 struct key_entry *key;
149
150 for (key = dell_wmi_keymap; key->type != KE_END; key++)
151 if (code == key->code)
152 return key;
153
154 return NULL;
155}
156
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800157static struct key_entry *dell_wmi_get_entry_by_keycode(unsigned int keycode)
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000158{
159 struct key_entry *key;
160
161 for (key = dell_wmi_keymap; key->type != KE_END; key++)
162 if (key->type == KE_KEY && keycode == key->keycode)
163 return key;
164
165 return NULL;
166}
167
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800168static int dell_wmi_getkeycode(struct input_dev *dev,
169 unsigned int scancode, unsigned int *keycode)
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000170{
171 struct key_entry *key = dell_wmi_get_entry_by_scancode(scancode);
172
173 if (key && key->type == KE_KEY) {
174 *keycode = key->keycode;
175 return 0;
176 }
177
178 return -EINVAL;
179}
180
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800181static int dell_wmi_setkeycode(struct input_dev *dev,
182 unsigned int scancode, unsigned int keycode)
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000183{
184 struct key_entry *key;
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800185 unsigned int old_keycode;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000186
187 key = dell_wmi_get_entry_by_scancode(scancode);
188 if (key && key->type == KE_KEY) {
189 old_keycode = key->keycode;
190 key->keycode = keycode;
191 set_bit(keycode, dev->keybit);
192 if (!dell_wmi_get_entry_by_keycode(old_keycode))
193 clear_bit(old_keycode, dev->keybit);
194 return 0;
195 }
196 return -EINVAL;
197}
198
199static void dell_wmi_notify(u32 value, void *context)
200{
201 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
202 static struct key_entry *key;
203 union acpi_object *obj;
Len Brownfda11e62009-12-26 23:02:24 -0500204 acpi_status status;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000205
Len Brownfda11e62009-12-26 23:02:24 -0500206 status = wmi_get_event_data(value, &response);
207 if (status != AE_OK) {
208 printk(KERN_INFO "dell-wmi: bad event status 0x%x\n", status);
209 return;
210 }
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000211
212 obj = (union acpi_object *)response.pointer;
213
214 if (obj && obj->type == ACPI_TYPE_BUFFER) {
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500215 int reported_key;
216 u16 *buffer_entry = (u16 *)obj->buffer.pointer;
217 if (dell_new_hk_type && (buffer_entry[1] != 0x10)) {
218 printk(KERN_INFO "dell-wmi: Received unknown WMI event"
219 " (0x%x)\n", buffer_entry[1]);
220 return;
221 }
222
223 if (dell_new_hk_type)
224 reported_key = (int)buffer_entry[2];
225 else
226 reported_key = (int)buffer_entry[1] & 0xffff;
227
228 key = dell_wmi_get_entry_by_scancode(reported_key);
229
230 if (!key) {
231 printk(KERN_INFO "dell-wmi: Unknown key %x pressed\n",
232 reported_key);
233 } else if ((key->keycode == KEY_BRIGHTNESSUP ||
234 key->keycode == KEY_BRIGHTNESSDOWN) && acpi_video) {
235 /* Don't report brightness notifications that will also
236 * come via ACPI */
237 return;
238 } else {
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000239 input_report_key(dell_wmi_input_dev, key->keycode, 1);
240 input_sync(dell_wmi_input_dev);
241 input_report_key(dell_wmi_input_dev, key->keycode, 0);
242 input_sync(dell_wmi_input_dev);
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500243 }
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000244 }
Anisse Astier3e9b9882009-12-04 10:10:09 +0100245 kfree(obj);
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000246}
247
Rezwanul Kabir5ea25592009-11-02 12:00:42 -0500248
249static void setup_new_hk_map(const struct dmi_header *dm)
250{
251
252 int i;
253 int hotkey_num = (dm->length-4)/sizeof(struct dell_new_keymap_entry);
254 struct dell_hotkey_table *table =
255 container_of(dm, struct dell_hotkey_table, header);
256
257 dell_new_wmi_keymap = kzalloc((hotkey_num+1) *
258 sizeof(struct key_entry), GFP_KERNEL);
259
260 for (i = 0; i < hotkey_num; i++) {
261 dell_new_wmi_keymap[i].type = KE_KEY;
262 dell_new_wmi_keymap[i].code = table->keymap[i].scancode;
263 dell_new_wmi_keymap[i].keycode =
264 (table->keymap[i].keycode > 255) ? 0 :
265 bios_to_linux_keycode[table->keymap[i].keycode];
266 }
267
268 dell_new_wmi_keymap[i].type = KE_END;
269 dell_new_wmi_keymap[i].code = 0;
270 dell_new_wmi_keymap[i].keycode = 0;
271
272 dell_wmi_keymap = dell_new_wmi_keymap;
273
274}
275
276
277static void find_hk_type(const struct dmi_header *dm, void *dummy)
278{
279
280 if ((dm->type == 0xb2) && (dm->length > 6)) {
281 dell_new_hk_type = true;
282 setup_new_hk_map(dm);
283 }
284
285}
286
287
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000288static int __init dell_wmi_input_setup(void)
289{
290 struct key_entry *key;
291 int err;
292
293 dell_wmi_input_dev = input_allocate_device();
294
295 if (!dell_wmi_input_dev)
296 return -ENOMEM;
297
298 dell_wmi_input_dev->name = "Dell WMI hotkeys";
299 dell_wmi_input_dev->phys = "wmi/input0";
300 dell_wmi_input_dev->id.bustype = BUS_HOST;
301 dell_wmi_input_dev->getkeycode = dell_wmi_getkeycode;
302 dell_wmi_input_dev->setkeycode = dell_wmi_setkeycode;
303
304 for (key = dell_wmi_keymap; key->type != KE_END; key++) {
305 switch (key->type) {
306 case KE_KEY:
307 set_bit(EV_KEY, dell_wmi_input_dev->evbit);
308 set_bit(key->keycode, dell_wmi_input_dev->keybit);
309 break;
310 case KE_SW:
311 set_bit(EV_SW, dell_wmi_input_dev->evbit);
312 set_bit(key->keycode, dell_wmi_input_dev->swbit);
313 break;
314 }
315 }
316
317 err = input_register_device(dell_wmi_input_dev);
318
319 if (err) {
320 input_free_device(dell_wmi_input_dev);
321 return err;
322 }
323
324 return 0;
325}
326
327static int __init dell_wmi_init(void)
328{
329 int err;
Len Brownabb631b2009-12-26 21:51:38 -0500330 acpi_status status;
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000331
Dmitry Torokhov7a9568f2009-12-25 11:49:35 -0800332 if (!wmi_has_guid(DELL_EVENT_GUID)) {
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000333 printk(KERN_WARNING "dell-wmi: No known WMI GUID found\n");
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800334 return -ENODEV;
335 }
336
337 dmi_walk(find_hk_type, NULL);
338 acpi_video = acpi_video_backlight_support();
339
340 err = dell_wmi_input_setup();
341 if (err)
342 return err;
343
Len Brownabb631b2009-12-26 21:51:38 -0500344 status = wmi_install_notify_handler(DELL_EVENT_GUID,
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800345 dell_wmi_notify, NULL);
Len Brownabb631b2009-12-26 21:51:38 -0500346 if (ACPI_FAILURE(status)) {
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800347 input_unregister_device(dell_wmi_input_dev);
348 printk(KERN_ERR
349 "dell-wmi: Unable to register notify handler - %d\n",
Len Brownabb631b2009-12-26 21:51:38 -0500350 status);
351 return -ENODEV;
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800352 }
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000353
354 return 0;
355}
356
357static void __exit dell_wmi_exit(void)
358{
Dmitry Torokhov1fdd4072009-12-17 22:19:42 -0800359 wmi_remove_notify_handler(DELL_EVENT_GUID);
360 input_unregister_device(dell_wmi_input_dev);
Matthew Garrett0b3f6102009-01-09 20:17:11 +0000361}
362
363module_init(dell_wmi_init);
364module_exit(dell_wmi_exit);