4 * Copyright (C) 2008 Red Hat <mjg@redhat.com>
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>
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.
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.
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
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/types.h>
30 #include <linux/input.h>
31 #include <acpi/acpi_drivers.h>
32 #include <linux/acpi.h>
33 #include <linux/string.h>
34 #include <linux/dmi.h>
36 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
37 MODULE_DESCRIPTION("Dell laptop WMI hotkeys driver");
38 MODULE_LICENSE("GPL");
40 #define DELL_EVENT_GUID "9DBB5994-A997-11DA-B012-B622A1EF5492"
42 static int acpi_video
;
44 MODULE_ALIAS("wmi:"DELL_EVENT_GUID
);
47 char type
; /* See KE_* below */
52 enum { KE_KEY
, KE_SW
, KE_IGNORE
, KE_END
};
55 * Certain keys are flagged as KE_IGNORE. All of these are either
56 * notifications (rather than requests for change) or are also sent
57 * via the keyboard controller so should not be sent again.
60 static struct key_entry dell_legacy_wmi_keymap
[] = {
61 {KE_KEY
, 0xe045, KEY_PROG1
},
62 {KE_KEY
, 0xe009, KEY_EJECTCD
},
64 /* These also contain the brightness level at offset 6 */
65 {KE_KEY
, 0xe006, KEY_BRIGHTNESSUP
},
66 {KE_KEY
, 0xe005, KEY_BRIGHTNESSDOWN
},
68 /* Battery health status button */
69 {KE_KEY
, 0xe007, KEY_BATTERY
},
71 /* This is actually for all radios. Although physically a
72 * switch, the notification does not provide an indication of
73 * state and so it should be reported as a key */
74 {KE_KEY
, 0xe008, KEY_WLAN
},
76 /* The next device is at offset 6, the active devices are at
77 offset 8 and the attached devices at offset 10 */
78 {KE_KEY
, 0xe00b, KEY_SWITCHVIDEOMODE
},
80 {KE_IGNORE
, 0xe00c, KEY_KBDILLUMTOGGLE
},
82 /* BIOS error detected */
83 {KE_IGNORE
, 0xe00d, KEY_RESERVED
},
86 {KE_KEY
, 0xe011, KEY_PROG2
},
88 /* Ambient light sensor toggle */
89 {KE_IGNORE
, 0xe013, KEY_RESERVED
},
91 {KE_IGNORE
, 0xe020, KEY_MUTE
},
92 {KE_IGNORE
, 0xe02e, KEY_VOLUMEDOWN
},
93 {KE_IGNORE
, 0xe030, KEY_VOLUMEUP
},
94 {KE_IGNORE
, 0xe033, KEY_KBDILLUMUP
},
95 {KE_IGNORE
, 0xe034, KEY_KBDILLUMDOWN
},
96 {KE_IGNORE
, 0xe03a, KEY_CAPSLOCK
},
97 {KE_IGNORE
, 0xe045, KEY_NUMLOCK
},
98 {KE_IGNORE
, 0xe046, KEY_SCROLLLOCK
},
102 static bool dell_new_hk_type
;
104 struct dell_new_keymap_entry
{
109 struct dell_hotkey_table
{
110 struct dmi_header header
;
111 struct dell_new_keymap_entry keymap
[];
115 static struct key_entry
*dell_new_wmi_keymap
;
117 static u16 bios_to_linux_keycode
[256] = {
119 KEY_MEDIA
, KEY_NEXTSONG
, KEY_PLAYPAUSE
, KEY_PREVIOUSSONG
,
120 KEY_STOPCD
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
121 KEY_WWW
, KEY_UNKNOWN
, KEY_VOLUMEDOWN
, KEY_MUTE
,
122 KEY_VOLUMEUP
, KEY_UNKNOWN
, KEY_BATTERY
, KEY_EJECTCD
,
123 KEY_UNKNOWN
, KEY_SLEEP
, KEY_PROG1
, KEY_BRIGHTNESSDOWN
,
124 KEY_BRIGHTNESSUP
, KEY_UNKNOWN
, KEY_KBDILLUMTOGGLE
,
125 KEY_UNKNOWN
, KEY_SWITCHVIDEOMODE
, KEY_UNKNOWN
, KEY_UNKNOWN
,
126 KEY_SWITCHVIDEOMODE
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_PROG2
,
127 KEY_UNKNOWN
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
128 0, 0, 0, 0, 0, 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,
141 static struct key_entry
*dell_wmi_keymap
= dell_legacy_wmi_keymap
;
143 static struct input_dev
*dell_wmi_input_dev
;
145 static struct key_entry
*dell_wmi_get_entry_by_scancode(unsigned int code
)
147 struct key_entry
*key
;
149 for (key
= dell_wmi_keymap
; key
->type
!= KE_END
; key
++)
150 if (code
== key
->code
)
156 static struct key_entry
*dell_wmi_get_entry_by_keycode(unsigned int keycode
)
158 struct key_entry
*key
;
160 for (key
= dell_wmi_keymap
; key
->type
!= KE_END
; key
++)
161 if (key
->type
== KE_KEY
&& keycode
== key
->keycode
)
167 static int dell_wmi_getkeycode(struct input_dev
*dev
,
168 unsigned int scancode
, unsigned int *keycode
)
170 struct key_entry
*key
= dell_wmi_get_entry_by_scancode(scancode
);
172 if (key
&& key
->type
== KE_KEY
) {
173 *keycode
= key
->keycode
;
180 static int dell_wmi_setkeycode(struct input_dev
*dev
,
181 unsigned int scancode
, unsigned int keycode
)
183 struct key_entry
*key
;
184 unsigned int old_keycode
;
186 key
= dell_wmi_get_entry_by_scancode(scancode
);
187 if (key
&& key
->type
== KE_KEY
) {
188 old_keycode
= key
->keycode
;
189 key
->keycode
= keycode
;
190 set_bit(keycode
, dev
->keybit
);
191 if (!dell_wmi_get_entry_by_keycode(old_keycode
))
192 clear_bit(old_keycode
, dev
->keybit
);
198 static void dell_wmi_notify(u32 value
, void *context
)
200 struct acpi_buffer response
= { ACPI_ALLOCATE_BUFFER
, NULL
};
201 static struct key_entry
*key
;
202 union acpi_object
*obj
;
205 status
= wmi_get_event_data(value
, &response
);
206 if (status
!= AE_OK
) {
207 printk(KERN_INFO
"dell-wmi: bad event status 0x%x\n", status
);
211 obj
= (union acpi_object
*)response
.pointer
;
213 if (obj
&& obj
->type
== ACPI_TYPE_BUFFER
) {
215 u16
*buffer_entry
= (u16
*)obj
->buffer
.pointer
;
216 if (dell_new_hk_type
&& (buffer_entry
[1] != 0x10)) {
217 printk(KERN_INFO
"dell-wmi: Received unknown WMI event"
218 " (0x%x)\n", buffer_entry
[1]);
222 if (dell_new_hk_type
)
223 reported_key
= (int)buffer_entry
[2];
225 reported_key
= (int)buffer_entry
[1] & 0xffff;
227 key
= dell_wmi_get_entry_by_scancode(reported_key
);
230 printk(KERN_INFO
"dell-wmi: Unknown key %x pressed\n",
232 } else if ((key
->keycode
== KEY_BRIGHTNESSUP
||
233 key
->keycode
== KEY_BRIGHTNESSDOWN
) && acpi_video
) {
234 /* Don't report brightness notifications that will also
238 input_report_key(dell_wmi_input_dev
, key
->keycode
, 1);
239 input_sync(dell_wmi_input_dev
);
240 input_report_key(dell_wmi_input_dev
, key
->keycode
, 0);
241 input_sync(dell_wmi_input_dev
);
248 static void setup_new_hk_map(const struct dmi_header
*dm
)
252 int hotkey_num
= (dm
->length
-4)/sizeof(struct dell_new_keymap_entry
);
253 struct dell_hotkey_table
*table
=
254 container_of(dm
, struct dell_hotkey_table
, header
);
256 dell_new_wmi_keymap
= kzalloc((hotkey_num
+1) *
257 sizeof(struct key_entry
), GFP_KERNEL
);
259 for (i
= 0; i
< hotkey_num
; i
++) {
260 dell_new_wmi_keymap
[i
].type
= KE_KEY
;
261 dell_new_wmi_keymap
[i
].code
= table
->keymap
[i
].scancode
;
262 dell_new_wmi_keymap
[i
].keycode
=
263 (table
->keymap
[i
].keycode
> 255) ? 0 :
264 bios_to_linux_keycode
[table
->keymap
[i
].keycode
];
267 dell_new_wmi_keymap
[i
].type
= KE_END
;
268 dell_new_wmi_keymap
[i
].code
= 0;
269 dell_new_wmi_keymap
[i
].keycode
= 0;
271 dell_wmi_keymap
= dell_new_wmi_keymap
;
276 static void find_hk_type(const struct dmi_header
*dm
, void *dummy
)
279 if ((dm
->type
== 0xb2) && (dm
->length
> 6)) {
280 dell_new_hk_type
= true;
281 setup_new_hk_map(dm
);
287 static int __init
dell_wmi_input_setup(void)
289 struct key_entry
*key
;
292 dell_wmi_input_dev
= input_allocate_device();
294 if (!dell_wmi_input_dev
)
297 dell_wmi_input_dev
->name
= "Dell WMI hotkeys";
298 dell_wmi_input_dev
->phys
= "wmi/input0";
299 dell_wmi_input_dev
->id
.bustype
= BUS_HOST
;
300 dell_wmi_input_dev
->getkeycode
= dell_wmi_getkeycode
;
301 dell_wmi_input_dev
->setkeycode
= dell_wmi_setkeycode
;
303 for (key
= dell_wmi_keymap
; key
->type
!= KE_END
; key
++) {
306 set_bit(EV_KEY
, dell_wmi_input_dev
->evbit
);
307 set_bit(key
->keycode
, dell_wmi_input_dev
->keybit
);
310 set_bit(EV_SW
, dell_wmi_input_dev
->evbit
);
311 set_bit(key
->keycode
, dell_wmi_input_dev
->swbit
);
316 err
= input_register_device(dell_wmi_input_dev
);
319 input_free_device(dell_wmi_input_dev
);
326 static int __init
dell_wmi_init(void)
331 if (!wmi_has_guid(DELL_EVENT_GUID
)) {
332 printk(KERN_WARNING
"dell-wmi: No known WMI GUID found\n");
336 dmi_walk(find_hk_type
, NULL
);
337 acpi_video
= acpi_video_backlight_support();
339 err
= dell_wmi_input_setup();
343 status
= wmi_install_notify_handler(DELL_EVENT_GUID
,
344 dell_wmi_notify
, NULL
);
345 if (ACPI_FAILURE(status
)) {
346 input_unregister_device(dell_wmi_input_dev
);
348 "dell-wmi: Unable to register notify handler - %d\n",
356 static void __exit
dell_wmi_exit(void)
358 wmi_remove_notify_handler(DELL_EVENT_GUID
);
359 input_unregister_device(dell_wmi_input_dev
);
362 module_init(dell_wmi_init
);
363 module_exit(dell_wmi_exit
);