dell-wmi: Update code for processing WMI events
[deliverable/linux.git] / drivers / platform / x86 / dell-wmi.c
CommitLineData
0b3f6109
MG
1/*
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
eb889524
JP
26#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
0b3f6109
MG
28#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/init.h>
5a0e3ad6 31#include <linux/slab.h>
0b3f6109
MG
32#include <linux/types.h>
33#include <linux/input.h>
890a7c8e 34#include <linux/input/sparse-keymap.h>
0b3f6109
MG
35#include <linux/acpi.h>
36#include <linux/string.h>
5ea25597 37#include <linux/dmi.h>
0b3f6109
MG
38
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
5ea25597
RK
45static int acpi_video;
46
0b3f6109
MG
47MODULE_ALIAS("wmi:"DELL_EVENT_GUID);
48
5cab0098
ML
49/*
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 */
0b3f6109 54
890a7c8e 55static const struct key_entry dell_wmi_legacy_keymap[] __initconst = {
f1566f0d
SF
56 { KE_IGNORE, 0x003a, { KEY_CAPSLOCK } },
57
890a7c8e
DT
58 { KE_KEY, 0xe045, { KEY_PROG1 } },
59 { KE_KEY, 0xe009, { KEY_EJECTCD } },
5cab0098
ML
60
61 /* These also contain the brightness level at offset 6 */
890a7c8e
DT
62 { KE_KEY, 0xe006, { KEY_BRIGHTNESSUP } },
63 { KE_KEY, 0xe005, { KEY_BRIGHTNESSDOWN } },
5cab0098
ML
64
65 /* Battery health status button */
890a7c8e 66 { KE_KEY, 0xe007, { KEY_BATTERY } },
5cab0098
ML
67
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 */
890a7c8e 71 { KE_KEY, 0xe008, { KEY_WLAN } },
5cab0098
ML
72
73 /* The next device is at offset 6, the active devices are at
74 offset 8 and the attached devices at offset 10 */
890a7c8e 75 { KE_KEY, 0xe00b, { KEY_SWITCHVIDEOMODE } },
5cab0098 76
890a7c8e 77 { KE_IGNORE, 0xe00c, { KEY_KBDILLUMTOGGLE } },
5cab0098
ML
78
79 /* BIOS error detected */
890a7c8e 80 { KE_IGNORE, 0xe00d, { KEY_RESERVED } },
5cab0098
ML
81
82 /* Wifi Catcher */
890a7c8e 83 { KE_KEY, 0xe011, {KEY_PROG2 } },
5cab0098
ML
84
85 /* Ambient light sensor toggle */
890a7c8e
DT
86 { KE_IGNORE, 0xe013, { KEY_RESERVED } },
87
88 { KE_IGNORE, 0xe020, { KEY_MUTE } },
f1566f0d
SF
89
90 /* Shortcut and audio panel keys */
91 { KE_IGNORE, 0xe025, { KEY_RESERVED } },
92 { KE_IGNORE, 0xe026, { KEY_RESERVED } },
93
890a7c8e
DT
94 { 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 } },
f1566f0d
SF
101 { KE_IGNORE, 0xe0f7, { KEY_MUTE } },
102 { KE_IGNORE, 0xe0f8, { KEY_VOLUMEDOWN } },
103 { KE_IGNORE, 0xe0f9, { KEY_VOLUMEUP } },
890a7c8e 104 { KE_END, 0 }
0b3f6109
MG
105};
106
5ea25597
RK
107static bool dell_new_hk_type;
108
890a7c8e 109struct dell_bios_keymap_entry {
5ea25597
RK
110 u16 scancode;
111 u16 keycode;
112};
113
890a7c8e 114struct dell_bios_hotkey_table {
5ea25597 115 struct dmi_header header;
890a7c8e 116 struct dell_bios_keymap_entry keymap[];
5ea25597
RK
117
118};
119
890a7c8e 120static const struct dell_bios_hotkey_table *dell_bios_hotkey_table;
5ea25597 121
890a7c8e 122static const u16 bios_to_linux_keycode[256] __initconst = {
5ea25597
RK
123
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,
cfb743bf
AH
132 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
133 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_MICMUTE,
5ea25597
RK
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, 0, 0,
cfb743bf
AH
142 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
143 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_PROG3
5ea25597
RK
144};
145
0b3f6109
MG
146static struct input_dev *dell_wmi_input_dev;
147
83fc44c3
PR
148static void dell_wmi_process_key(int reported_key)
149{
150 const struct key_entry *key;
151
152 key = sparse_keymap_entry_from_scancode(dell_wmi_input_dev,
153 reported_key);
154 if (!key) {
155 pr_info("Unknown key %x pressed\n", reported_key);
156 return;
157 }
158
159 pr_debug("Key %x pressed\n", reported_key);
160
161 /* Don't report brightness notifications that will also come via ACPI */
162 if ((key->keycode == KEY_BRIGHTNESSUP ||
163 key->keycode == KEY_BRIGHTNESSDOWN) && acpi_video)
164 return;
165
166 sparse_keymap_report_entry(dell_wmi_input_dev, key, 1, true);
167}
168
0b3f6109
MG
169static void dell_wmi_notify(u32 value, void *context)
170{
171 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
0b3f6109 172 union acpi_object *obj;
fda11e61 173 acpi_status status;
83fc44c3
PR
174 acpi_size buffer_size;
175 u16 *buffer_entry, *buffer_end;
176 int len, i;
0b3f6109 177
fda11e61
LB
178 status = wmi_get_event_data(value, &response);
179 if (status != AE_OK) {
83fc44c3 180 pr_warn("bad event status 0x%x\n", status);
fda11e61
LB
181 return;
182 }
0b3f6109
MG
183
184 obj = (union acpi_object *)response.pointer;
83fc44c3
PR
185 if (!obj) {
186 pr_warn("no response\n");
187 return;
188 }
0b3f6109 189
83fc44c3
PR
190 if (obj->type != ACPI_TYPE_BUFFER) {
191 pr_warn("bad response type %x\n", obj->type);
192 kfree(obj);
193 return;
194 }
195
196 pr_debug("Received WMI event (%*ph)\n",
197 obj->buffer.length, obj->buffer.pointer);
5ea25597 198
83fc44c3
PR
199 buffer_entry = (u16 *)obj->buffer.pointer;
200 buffer_size = obj->buffer.length/2;
201
202 if (!dell_new_hk_type) {
203 if (buffer_size >= 3 && buffer_entry[1] == 0x0)
204 dell_wmi_process_key(buffer_entry[2]);
a666b6ff 205 else if (buffer_size >= 2)
83fc44c3
PR
206 dell_wmi_process_key(buffer_entry[1]);
207 else
a666b6ff 208 pr_info("Received unknown WMI event\n");
83fc44c3
PR
209 kfree(obj);
210 return;
211 }
212
213 buffer_end = buffer_entry + buffer_size;
214
215 while (buffer_entry < buffer_end) {
216
217 len = buffer_entry[0];
218 if (len == 0)
219 break;
220
221 len++;
222
223 if (buffer_entry + len > buffer_end) {
224 pr_warn("Invalid length of WMI event\n");
225 break;
a666b6ff 226 }
5ea25597 227
83fc44c3
PR
228 pr_debug("Process buffer (%*ph)\n", len*2, buffer_entry);
229
230 switch (buffer_entry[1]) {
231 case 0x00:
232 for (i = 2; i < len; ++i) {
233 switch (buffer_entry[i]) {
234 case 0xe043:
235 /* NIC Link is Up */
236 pr_debug("NIC Link is Up\n");
237 break;
238 case 0xe044:
239 /* NIC Link is Down */
240 pr_debug("NIC Link is Down\n");
241 break;
242 case 0xe045:
243 /* Unknown event but defined in DSDT */
244 default:
245 /* Unknown event */
246 pr_info("Unknown WMI event type 0x00: "
247 "0x%x\n", (int)buffer_entry[i]);
248 break;
249 }
250 }
251 break;
252 case 0x10:
253 /* Keys pressed */
254 for (i = 2; i < len; ++i)
255 dell_wmi_process_key(buffer_entry[i]);
256 break;
257 case 0x11:
258 for (i = 2; i < len; ++i) {
259 switch (buffer_entry[i]) {
260 case 0xfff0:
261 /* Battery unplugged */
262 pr_debug("Battery unplugged\n");
263 break;
264 case 0xfff1:
265 /* Battery inserted */
266 pr_debug("Battery inserted\n");
267 break;
268 case 0x01e1:
269 case 0x02ea:
270 case 0x02eb:
271 case 0x02ec:
272 case 0x02f6:
273 /* Keyboard backlight level changed */
274 pr_debug("Keyboard backlight level "
275 "changed\n");
276 break;
277 default:
278 /* Unknown event */
279 pr_info("Unknown WMI event type 0x11: "
280 "0x%x\n", (int)buffer_entry[i]);
281 break;
282 }
283 }
284 break;
285 default:
286 /* Unknown event */
287 pr_info("Unknown WMI event type 0x%x\n",
288 (int)buffer_entry[1]);
289 break;
5ea25597 290 }
83fc44c3
PR
291
292 buffer_entry += len;
293
0b3f6109 294 }
83fc44c3 295
3e9b988e 296 kfree(obj);
0b3f6109
MG
297}
298
890a7c8e 299static const struct key_entry * __init dell_wmi_prepare_new_keymap(void)
5ea25597 300{
890a7c8e
DT
301 int hotkey_num = (dell_bios_hotkey_table->header.length - 4) /
302 sizeof(struct dell_bios_keymap_entry);
303 struct key_entry *keymap;
5ea25597 304 int i;
5ea25597 305
890a7c8e
DT
306 keymap = kcalloc(hotkey_num + 1, sizeof(struct key_entry), GFP_KERNEL);
307 if (!keymap)
308 return NULL;
5ea25597
RK
309
310 for (i = 0; i < hotkey_num; i++) {
890a7c8e
DT
311 const struct dell_bios_keymap_entry *bios_entry =
312 &dell_bios_hotkey_table->keymap[i];
313 keymap[i].type = KE_KEY;
314 keymap[i].code = bios_entry->scancode;
315 keymap[i].keycode = bios_entry->keycode < 256 ?
316 bios_to_linux_keycode[bios_entry->keycode] :
317 KEY_RESERVED;
5ea25597
RK
318 }
319
890a7c8e 320 keymap[hotkey_num].type = KE_END;
5ea25597 321
890a7c8e 322 return keymap;
5ea25597
RK
323}
324
0b3f6109
MG
325static int __init dell_wmi_input_setup(void)
326{
0b3f6109
MG
327 int err;
328
329 dell_wmi_input_dev = input_allocate_device();
0b3f6109
MG
330 if (!dell_wmi_input_dev)
331 return -ENOMEM;
332
333 dell_wmi_input_dev->name = "Dell WMI hotkeys";
334 dell_wmi_input_dev->phys = "wmi/input0";
335 dell_wmi_input_dev->id.bustype = BUS_HOST;
890a7c8e
DT
336
337 if (dell_new_hk_type) {
338 const struct key_entry *keymap = dell_wmi_prepare_new_keymap();
339 if (!keymap) {
340 err = -ENOMEM;
341 goto err_free_dev;
0b3f6109 342 }
0b3f6109 343
890a7c8e 344 err = sparse_keymap_setup(dell_wmi_input_dev, keymap, NULL);
0b3f6109 345
890a7c8e
DT
346 /*
347 * Sparse keymap library makes a copy of keymap so we
348 * don't need the original one that was allocated.
349 */
350 kfree(keymap);
351 } else {
352 err = sparse_keymap_setup(dell_wmi_input_dev,
353 dell_wmi_legacy_keymap, NULL);
0b3f6109 354 }
890a7c8e
DT
355 if (err)
356 goto err_free_dev;
357
358 err = input_register_device(dell_wmi_input_dev);
359 if (err)
360 goto err_free_keymap;
0b3f6109
MG
361
362 return 0;
890a7c8e
DT
363
364 err_free_keymap:
365 sparse_keymap_free(dell_wmi_input_dev);
366 err_free_dev:
367 input_free_device(dell_wmi_input_dev);
368 return err;
369}
370
371static void dell_wmi_input_destroy(void)
372{
373 sparse_keymap_free(dell_wmi_input_dev);
374 input_unregister_device(dell_wmi_input_dev);
375}
376
377static void __init find_hk_type(const struct dmi_header *dm, void *dummy)
378{
379 if (dm->type == 0xb2 && dm->length > 6) {
380 dell_new_hk_type = true;
381 dell_bios_hotkey_table =
382 container_of(dm, struct dell_bios_hotkey_table, header);
383 }
0b3f6109
MG
384}
385
386static int __init dell_wmi_init(void)
387{
388 int err;
abb631bf 389 acpi_status status;
0b3f6109 390
7a9568f5 391 if (!wmi_has_guid(DELL_EVENT_GUID)) {
eb889524 392 pr_warn("No known WMI GUID found\n");
1fdd407f
DT
393 return -ENODEV;
394 }
5ea25597 395
1fdd407f
DT
396 dmi_walk(find_hk_type, NULL);
397 acpi_video = acpi_video_backlight_support();
0b3f6109 398
1fdd407f 399 err = dell_wmi_input_setup();
890a7c8e 400 if (err)
1fdd407f 401 return err;
5ea25597 402
abb631bf 403 status = wmi_install_notify_handler(DELL_EVENT_GUID,
1fdd407f 404 dell_wmi_notify, NULL);
abb631bf 405 if (ACPI_FAILURE(status)) {
890a7c8e 406 dell_wmi_input_destroy();
eb889524 407 pr_err("Unable to register notify handler - %d\n", status);
abb631bf 408 return -ENODEV;
1fdd407f 409 }
0b3f6109
MG
410
411 return 0;
412}
890a7c8e 413module_init(dell_wmi_init);
0b3f6109
MG
414
415static void __exit dell_wmi_exit(void)
416{
1fdd407f 417 wmi_remove_notify_handler(DELL_EVENT_GUID);
890a7c8e 418 dell_wmi_input_destroy();
0b3f6109 419}
0b3f6109 420module_exit(dell_wmi_exit);
This page took 0.540863 seconds and 5 git commands to generate.