| Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 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 |  | 
|  | 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 |  | 
|  | 35 | MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>"); | 
|  | 36 | MODULE_DESCRIPTION("Dell laptop WMI hotkeys driver"); | 
|  | 37 | MODULE_LICENSE("GPL"); | 
|  | 38 |  | 
|  | 39 | #define DELL_EVENT_GUID "9DBB5994-A997-11DA-B012-B622A1EF5492" | 
|  | 40 |  | 
|  | 41 | MODULE_ALIAS("wmi:"DELL_EVENT_GUID); | 
|  | 42 |  | 
|  | 43 | struct key_entry { | 
|  | 44 | char type;		/* See KE_* below */ | 
|  | 45 | u16 code; | 
|  | 46 | u16 keycode; | 
|  | 47 | }; | 
|  | 48 |  | 
| Mario Limonciello | 5cab009 | 2009-06-10 19:40:47 +0000 | [diff] [blame] | 49 | enum { KE_KEY, KE_SW, KE_IGNORE, KE_END }; | 
|  | 50 |  | 
|  | 51 | /* | 
|  | 52 | * Certain keys are flagged as KE_IGNORE. All of these are either | 
|  | 53 | * notifications (rather than requests for change) or are also sent | 
|  | 54 | * via the keyboard controller so should not be sent again. | 
|  | 55 | */ | 
| Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 56 |  | 
|  | 57 | static struct key_entry dell_wmi_keymap[] = { | 
|  | 58 | {KE_KEY, 0xe045, KEY_PROG1}, | 
| Mario Limonciello | 5cab009 | 2009-06-10 19:40:47 +0000 | [diff] [blame] | 59 | {KE_KEY, 0xe009, KEY_EJECTCD}, | 
|  | 60 |  | 
|  | 61 | /* These also contain the brightness level at offset 6 */ | 
|  | 62 | {KE_KEY, 0xe006, KEY_BRIGHTNESSUP}, | 
|  | 63 | {KE_KEY, 0xe005, KEY_BRIGHTNESSDOWN}, | 
|  | 64 |  | 
|  | 65 | /* Battery health status button */ | 
|  | 66 | {KE_KEY, 0xe007, KEY_BATTERY}, | 
|  | 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 */ | 
|  | 71 | {KE_KEY, 0xe008, KEY_WLAN}, | 
|  | 72 |  | 
|  | 73 | /* The next device is at offset 6, the active devices are at | 
|  | 74 | offset 8 and the attached devices at offset 10 */ | 
|  | 75 | {KE_KEY, 0xe00b, KEY_DISPLAYTOGGLE}, | 
|  | 76 |  | 
|  | 77 | {KE_IGNORE, 0xe00c, KEY_KBDILLUMTOGGLE}, | 
|  | 78 |  | 
|  | 79 | /* BIOS error detected */ | 
|  | 80 | {KE_IGNORE, 0xe00d, KEY_RESERVED}, | 
|  | 81 |  | 
|  | 82 | /* Wifi Catcher */ | 
|  | 83 | {KE_KEY, 0xe011, KEY_PROG2}, | 
|  | 84 |  | 
|  | 85 | /* Ambient light sensor toggle */ | 
|  | 86 | {KE_IGNORE, 0xe013, KEY_RESERVED}, | 
|  | 87 |  | 
|  | 88 | {KE_IGNORE, 0xe020, KEY_MUTE}, | 
|  | 89 | {KE_IGNORE, 0xe02e, KEY_VOLUMEDOWN}, | 
|  | 90 | {KE_IGNORE, 0xe030, KEY_VOLUMEUP}, | 
|  | 91 | {KE_IGNORE, 0xe033, KEY_KBDILLUMUP}, | 
|  | 92 | {KE_IGNORE, 0xe034, KEY_KBDILLUMDOWN}, | 
|  | 93 | {KE_IGNORE, 0xe03a, KEY_CAPSLOCK}, | 
|  | 94 | {KE_IGNORE, 0xe045, KEY_NUMLOCK}, | 
|  | 95 | {KE_IGNORE, 0xe046, KEY_SCROLLLOCK}, | 
| Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 96 | {KE_END, 0} | 
|  | 97 | }; | 
|  | 98 |  | 
|  | 99 | static struct input_dev *dell_wmi_input_dev; | 
|  | 100 |  | 
|  | 101 | static struct key_entry *dell_wmi_get_entry_by_scancode(int code) | 
|  | 102 | { | 
|  | 103 | struct key_entry *key; | 
|  | 104 |  | 
|  | 105 | for (key = dell_wmi_keymap; key->type != KE_END; key++) | 
|  | 106 | if (code == key->code) | 
|  | 107 | return key; | 
|  | 108 |  | 
|  | 109 | return NULL; | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | static struct key_entry *dell_wmi_get_entry_by_keycode(int keycode) | 
|  | 113 | { | 
|  | 114 | struct key_entry *key; | 
|  | 115 |  | 
|  | 116 | for (key = dell_wmi_keymap; key->type != KE_END; key++) | 
|  | 117 | if (key->type == KE_KEY && keycode == key->keycode) | 
|  | 118 | return key; | 
|  | 119 |  | 
|  | 120 | return NULL; | 
|  | 121 | } | 
|  | 122 |  | 
|  | 123 | static int dell_wmi_getkeycode(struct input_dev *dev, int scancode, | 
|  | 124 | int *keycode) | 
|  | 125 | { | 
|  | 126 | struct key_entry *key = dell_wmi_get_entry_by_scancode(scancode); | 
|  | 127 |  | 
|  | 128 | if (key && key->type == KE_KEY) { | 
|  | 129 | *keycode = key->keycode; | 
|  | 130 | return 0; | 
|  | 131 | } | 
|  | 132 |  | 
|  | 133 | return -EINVAL; | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | static int dell_wmi_setkeycode(struct input_dev *dev, int scancode, int keycode) | 
|  | 137 | { | 
|  | 138 | struct key_entry *key; | 
|  | 139 | int old_keycode; | 
|  | 140 |  | 
|  | 141 | if (keycode < 0 || keycode > KEY_MAX) | 
|  | 142 | return -EINVAL; | 
|  | 143 |  | 
|  | 144 | key = dell_wmi_get_entry_by_scancode(scancode); | 
|  | 145 | if (key && key->type == KE_KEY) { | 
|  | 146 | old_keycode = key->keycode; | 
|  | 147 | key->keycode = keycode; | 
|  | 148 | set_bit(keycode, dev->keybit); | 
|  | 149 | if (!dell_wmi_get_entry_by_keycode(old_keycode)) | 
|  | 150 | clear_bit(old_keycode, dev->keybit); | 
|  | 151 | return 0; | 
|  | 152 | } | 
|  | 153 | return -EINVAL; | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | static void dell_wmi_notify(u32 value, void *context) | 
|  | 157 | { | 
|  | 158 | struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL }; | 
|  | 159 | static struct key_entry *key; | 
|  | 160 | union acpi_object *obj; | 
|  | 161 |  | 
|  | 162 | wmi_get_event_data(value, &response); | 
|  | 163 |  | 
|  | 164 | obj = (union acpi_object *)response.pointer; | 
|  | 165 |  | 
|  | 166 | if (obj && obj->type == ACPI_TYPE_BUFFER) { | 
|  | 167 | int *buffer = (int *)obj->buffer.pointer; | 
| Mario Limonciello | 75d71c4 | 2009-06-10 19:40:46 +0000 | [diff] [blame] | 168 | /* | 
|  | 169 | *  The upper bytes of the event may contain | 
|  | 170 | *  additional information, so mask them off for the | 
|  | 171 | *  scancode lookup | 
|  | 172 | */ | 
|  | 173 | key = dell_wmi_get_entry_by_scancode(buffer[1] & 0xFFFF); | 
| Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 174 | if (key) { | 
|  | 175 | input_report_key(dell_wmi_input_dev, key->keycode, 1); | 
|  | 176 | input_sync(dell_wmi_input_dev); | 
|  | 177 | input_report_key(dell_wmi_input_dev, key->keycode, 0); | 
|  | 178 | input_sync(dell_wmi_input_dev); | 
| Matthew Garrett | db18b04 | 2009-06-10 19:40:48 +0000 | [diff] [blame] | 179 | } else if (buffer[1] & 0xFFFF) | 
| Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 180 | printk(KERN_INFO "dell-wmi: Unknown key %x pressed\n", | 
| Matthew Garrett | db18b04 | 2009-06-10 19:40:48 +0000 | [diff] [blame] | 181 | buffer[1] & 0xFFFF); | 
| Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 182 | } | 
|  | 183 | } | 
|  | 184 |  | 
|  | 185 | static int __init dell_wmi_input_setup(void) | 
|  | 186 | { | 
|  | 187 | struct key_entry *key; | 
|  | 188 | int err; | 
|  | 189 |  | 
|  | 190 | dell_wmi_input_dev = input_allocate_device(); | 
|  | 191 |  | 
|  | 192 | if (!dell_wmi_input_dev) | 
|  | 193 | return -ENOMEM; | 
|  | 194 |  | 
|  | 195 | dell_wmi_input_dev->name = "Dell WMI hotkeys"; | 
|  | 196 | dell_wmi_input_dev->phys = "wmi/input0"; | 
|  | 197 | dell_wmi_input_dev->id.bustype = BUS_HOST; | 
|  | 198 | dell_wmi_input_dev->getkeycode = dell_wmi_getkeycode; | 
|  | 199 | dell_wmi_input_dev->setkeycode = dell_wmi_setkeycode; | 
|  | 200 |  | 
|  | 201 | for (key = dell_wmi_keymap; key->type != KE_END; key++) { | 
|  | 202 | switch (key->type) { | 
|  | 203 | case KE_KEY: | 
|  | 204 | set_bit(EV_KEY, dell_wmi_input_dev->evbit); | 
|  | 205 | set_bit(key->keycode, dell_wmi_input_dev->keybit); | 
|  | 206 | break; | 
|  | 207 | case KE_SW: | 
|  | 208 | set_bit(EV_SW, dell_wmi_input_dev->evbit); | 
|  | 209 | set_bit(key->keycode, dell_wmi_input_dev->swbit); | 
|  | 210 | break; | 
|  | 211 | } | 
|  | 212 | } | 
|  | 213 |  | 
|  | 214 | err = input_register_device(dell_wmi_input_dev); | 
|  | 215 |  | 
|  | 216 | if (err) { | 
|  | 217 | input_free_device(dell_wmi_input_dev); | 
|  | 218 | return err; | 
|  | 219 | } | 
|  | 220 |  | 
|  | 221 | return 0; | 
|  | 222 | } | 
|  | 223 |  | 
|  | 224 | static int __init dell_wmi_init(void) | 
|  | 225 | { | 
|  | 226 | int err; | 
|  | 227 |  | 
|  | 228 | if (wmi_has_guid(DELL_EVENT_GUID)) { | 
|  | 229 | err = dell_wmi_input_setup(); | 
|  | 230 |  | 
|  | 231 | if (err) | 
|  | 232 | return err; | 
|  | 233 |  | 
|  | 234 | err = wmi_install_notify_handler(DELL_EVENT_GUID, | 
|  | 235 | dell_wmi_notify, NULL); | 
|  | 236 | if (err) { | 
|  | 237 | input_unregister_device(dell_wmi_input_dev); | 
|  | 238 | printk(KERN_ERR "dell-wmi: Unable to register" | 
|  | 239 | " notify handler - %d\n", err); | 
|  | 240 | return err; | 
|  | 241 | } | 
|  | 242 |  | 
|  | 243 | } else | 
|  | 244 | printk(KERN_WARNING "dell-wmi: No known WMI GUID found\n"); | 
|  | 245 |  | 
|  | 246 | return 0; | 
|  | 247 | } | 
|  | 248 |  | 
|  | 249 | static void __exit dell_wmi_exit(void) | 
|  | 250 | { | 
|  | 251 | if (wmi_has_guid(DELL_EVENT_GUID)) { | 
|  | 252 | wmi_remove_notify_handler(DELL_EVENT_GUID); | 
|  | 253 | input_unregister_device(dell_wmi_input_dev); | 
|  | 254 | } | 
|  | 255 | } | 
|  | 256 |  | 
|  | 257 | module_init(dell_wmi_init); | 
|  | 258 | module_exit(dell_wmi_exit); |