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/slab.h>
30 #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>
35 #include <linux/dmi.h>
37 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
38 MODULE_DESCRIPTION("Dell laptop WMI hotkeys driver");
39 MODULE_LICENSE("GPL");
41 #define DELL_EVENT_GUID "9DBB5994-A997-11DA-B012-B622A1EF5492"
43 static int acpi_video
;
45 MODULE_ALIAS("wmi:"DELL_EVENT_GUID
);
48 char type
; /* See KE_* below */
53 enum { KE_KEY
, KE_SW
, KE_IGNORE
, KE_END
};
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.
61 static struct key_entry dell_legacy_wmi_keymap
[] = {
62 {KE_KEY
, 0xe045, KEY_PROG1
},
63 {KE_KEY
, 0xe009, KEY_EJECTCD
},
65 /* These also contain the brightness level at offset 6 */
66 {KE_KEY
, 0xe006, KEY_BRIGHTNESSUP
},
67 {KE_KEY
, 0xe005, KEY_BRIGHTNESSDOWN
},
69 /* Battery health status button */
70 {KE_KEY
, 0xe007, KEY_BATTERY
},
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
},
77 /* The next device is at offset 6, the active devices are at
78 offset 8 and the attached devices at offset 10 */
79 {KE_KEY
, 0xe00b, KEY_SWITCHVIDEOMODE
},
81 {KE_IGNORE
, 0xe00c, KEY_KBDILLUMTOGGLE
},
83 /* BIOS error detected */
84 {KE_IGNORE
, 0xe00d, KEY_RESERVED
},
87 {KE_KEY
, 0xe011, KEY_PROG2
},
89 /* Ambient light sensor toggle */
90 {KE_IGNORE
, 0xe013, KEY_RESERVED
},
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
},
103 static bool dell_new_hk_type
;
105 struct dell_new_keymap_entry
{
110 struct dell_hotkey_table
{
111 struct dmi_header header
;
112 struct dell_new_keymap_entry keymap
[];
116 static struct key_entry
*dell_new_wmi_keymap
;
118 static u16 bios_to_linux_keycode
[256] = {
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,
142 static struct key_entry
*dell_wmi_keymap
= dell_legacy_wmi_keymap
;
144 static struct input_dev
*dell_wmi_input_dev
;
146 static struct key_entry
*dell_wmi_get_entry_by_scancode(unsigned int code
)
148 struct key_entry
*key
;
150 for (key
= dell_wmi_keymap
; key
->type
!= KE_END
; key
++)
151 if (code
== key
->code
)
157 static struct key_entry
*dell_wmi_get_entry_by_keycode(unsigned int keycode
)
159 struct key_entry
*key
;
161 for (key
= dell_wmi_keymap
; key
->type
!= KE_END
; key
++)
162 if (key
->type
== KE_KEY
&& keycode
== key
->keycode
)
168 static int dell_wmi_getkeycode(struct input_dev
*dev
,
169 unsigned int scancode
, unsigned int *keycode
)
171 struct key_entry
*key
= dell_wmi_get_entry_by_scancode(scancode
);
173 if (key
&& key
->type
== KE_KEY
) {
174 *keycode
= key
->keycode
;
181 static int dell_wmi_setkeycode(struct input_dev
*dev
,
182 unsigned int scancode
, unsigned int keycode
)
184 struct key_entry
*key
;
185 unsigned int old_keycode
;
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
);
199 static void dell_wmi_notify(u32 value
, void *context
)
201 struct acpi_buffer response
= { ACPI_ALLOCATE_BUFFER
, NULL
};
202 static struct key_entry
*key
;
203 union acpi_object
*obj
;
206 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
);
212 obj
= (union acpi_object
*)response
.pointer
;
214 if (obj
&& obj
->type
== ACPI_TYPE_BUFFER
) {
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]);
224 if (dell_new_hk_type
)
225 reported_key
= (int)buffer_entry
[2];
227 reported_key
= (int)buffer_entry
[1] & 0xffff;
229 key
= dell_wmi_get_entry_by_scancode(reported_key
);
232 printk(KERN_INFO
"dell-wmi: Unknown key %x pressed\n",
234 } else if ((key
->keycode
== KEY_BRIGHTNESSUP
||
235 key
->keycode
== KEY_BRIGHTNESSDOWN
) && acpi_video
) {
236 /* Don't report brightness notifications that will also
240 input_report_key(dell_wmi_input_dev
, key
->keycode
, 1);
241 input_sync(dell_wmi_input_dev
);
242 input_report_key(dell_wmi_input_dev
, key
->keycode
, 0);
243 input_sync(dell_wmi_input_dev
);
250 static void setup_new_hk_map(const struct dmi_header
*dm
)
254 int hotkey_num
= (dm
->length
-4)/sizeof(struct dell_new_keymap_entry
);
255 struct dell_hotkey_table
*table
=
256 container_of(dm
, struct dell_hotkey_table
, header
);
258 dell_new_wmi_keymap
= kzalloc((hotkey_num
+1) *
259 sizeof(struct key_entry
), GFP_KERNEL
);
261 for (i
= 0; i
< hotkey_num
; i
++) {
262 dell_new_wmi_keymap
[i
].type
= KE_KEY
;
263 dell_new_wmi_keymap
[i
].code
= table
->keymap
[i
].scancode
;
264 dell_new_wmi_keymap
[i
].keycode
=
265 (table
->keymap
[i
].keycode
> 255) ? 0 :
266 bios_to_linux_keycode
[table
->keymap
[i
].keycode
];
269 dell_new_wmi_keymap
[i
].type
= KE_END
;
270 dell_new_wmi_keymap
[i
].code
= 0;
271 dell_new_wmi_keymap
[i
].keycode
= 0;
273 dell_wmi_keymap
= dell_new_wmi_keymap
;
278 static void find_hk_type(const struct dmi_header
*dm
, void *dummy
)
281 if ((dm
->type
== 0xb2) && (dm
->length
> 6)) {
282 dell_new_hk_type
= true;
283 setup_new_hk_map(dm
);
289 static int __init
dell_wmi_input_setup(void)
291 struct key_entry
*key
;
294 dell_wmi_input_dev
= input_allocate_device();
296 if (!dell_wmi_input_dev
)
299 dell_wmi_input_dev
->name
= "Dell WMI hotkeys";
300 dell_wmi_input_dev
->phys
= "wmi/input0";
301 dell_wmi_input_dev
->id
.bustype
= BUS_HOST
;
302 dell_wmi_input_dev
->getkeycode
= dell_wmi_getkeycode
;
303 dell_wmi_input_dev
->setkeycode
= dell_wmi_setkeycode
;
305 for (key
= dell_wmi_keymap
; key
->type
!= KE_END
; key
++) {
308 set_bit(EV_KEY
, dell_wmi_input_dev
->evbit
);
309 set_bit(key
->keycode
, dell_wmi_input_dev
->keybit
);
312 set_bit(EV_SW
, dell_wmi_input_dev
->evbit
);
313 set_bit(key
->keycode
, dell_wmi_input_dev
->swbit
);
318 err
= input_register_device(dell_wmi_input_dev
);
321 input_free_device(dell_wmi_input_dev
);
328 static int __init
dell_wmi_init(void)
333 if (!wmi_has_guid(DELL_EVENT_GUID
)) {
334 printk(KERN_WARNING
"dell-wmi: No known WMI GUID found\n");
338 dmi_walk(find_hk_type
, NULL
);
339 acpi_video
= acpi_video_backlight_support();
341 err
= dell_wmi_input_setup();
345 status
= wmi_install_notify_handler(DELL_EVENT_GUID
,
346 dell_wmi_notify
, NULL
);
347 if (ACPI_FAILURE(status
)) {
348 input_unregister_device(dell_wmi_input_dev
);
350 "dell-wmi: Unable to register notify handler - %d\n",
358 static void __exit
dell_wmi_exit(void)
360 wmi_remove_notify_handler(DELL_EVENT_GUID
);
361 input_unregister_device(dell_wmi_input_dev
);
364 module_init(dell_wmi_init
);
365 module_exit(dell_wmi_exit
);