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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/slab.h> |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 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> |
Rezwanul Kabir | 5ea2559 | 2009-11-02 12:00:42 -0500 | [diff] [blame] | 35 | #include <linux/dmi.h> |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 36 | |
| 37 | MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>"); |
| 38 | MODULE_DESCRIPTION("Dell laptop WMI hotkeys driver"); |
| 39 | MODULE_LICENSE("GPL"); |
| 40 | |
| 41 | #define DELL_EVENT_GUID "9DBB5994-A997-11DA-B012-B622A1EF5492" |
| 42 | |
Rezwanul Kabir | 5ea2559 | 2009-11-02 12:00:42 -0500 | [diff] [blame] | 43 | static int acpi_video; |
| 44 | |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 45 | MODULE_ALIAS("wmi:"DELL_EVENT_GUID); |
| 46 | |
| 47 | struct key_entry { |
| 48 | char type; /* See KE_* below */ |
| 49 | u16 code; |
| 50 | u16 keycode; |
| 51 | }; |
| 52 | |
Mario Limonciello | 5cab009 | 2009-06-10 19:40:47 +0000 | [diff] [blame] | 53 | enum { 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 Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 60 | |
Rezwanul Kabir | 5ea2559 | 2009-11-02 12:00:42 -0500 | [diff] [blame] | 61 | static struct key_entry dell_legacy_wmi_keymap[] = { |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 62 | {KE_KEY, 0xe045, KEY_PROG1}, |
Mario Limonciello | 5cab009 | 2009-06-10 19:40:47 +0000 | [diff] [blame] | 63 | {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 Kabir | 5ea2559 | 2009-11-02 12:00:42 -0500 | [diff] [blame] | 79 | {KE_KEY, 0xe00b, KEY_SWITCHVIDEOMODE}, |
Mario Limonciello | 5cab009 | 2009-06-10 19:40:47 +0000 | [diff] [blame] | 80 | |
| 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 Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 100 | {KE_END, 0} |
| 101 | }; |
| 102 | |
Rezwanul Kabir | 5ea2559 | 2009-11-02 12:00:42 -0500 | [diff] [blame] | 103 | static bool dell_new_hk_type; |
| 104 | |
| 105 | struct dell_new_keymap_entry { |
| 106 | u16 scancode; |
| 107 | u16 keycode; |
| 108 | }; |
| 109 | |
| 110 | struct dell_hotkey_table { |
| 111 | struct dmi_header header; |
| 112 | struct dell_new_keymap_entry keymap[]; |
| 113 | |
| 114 | }; |
| 115 | |
| 116 | static struct key_entry *dell_new_wmi_keymap; |
| 117 | |
| 118 | static 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 | |
| 142 | static struct key_entry *dell_wmi_keymap = dell_legacy_wmi_keymap; |
| 143 | |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 144 | static struct input_dev *dell_wmi_input_dev; |
| 145 | |
Dmitry Torokhov | 58b9399 | 2010-03-08 22:37:10 -0800 | [diff] [blame] | 146 | static struct key_entry *dell_wmi_get_entry_by_scancode(unsigned int code) |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 147 | { |
| 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 Torokhov | 58b9399 | 2010-03-08 22:37:10 -0800 | [diff] [blame] | 157 | static struct key_entry *dell_wmi_get_entry_by_keycode(unsigned int keycode) |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 158 | { |
| 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 Torokhov | 58b9399 | 2010-03-08 22:37:10 -0800 | [diff] [blame] | 168 | static int dell_wmi_getkeycode(struct input_dev *dev, |
| 169 | unsigned int scancode, unsigned int *keycode) |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 170 | { |
| 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 Torokhov | 58b9399 | 2010-03-08 22:37:10 -0800 | [diff] [blame] | 181 | static int dell_wmi_setkeycode(struct input_dev *dev, |
| 182 | unsigned int scancode, unsigned int keycode) |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 183 | { |
| 184 | struct key_entry *key; |
Dmitry Torokhov | 58b9399 | 2010-03-08 22:37:10 -0800 | [diff] [blame] | 185 | unsigned int old_keycode; |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 186 | |
| 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 | |
| 199 | static 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 Brown | fda11e6 | 2009-12-26 23:02:24 -0500 | [diff] [blame] | 204 | acpi_status status; |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 205 | |
Len Brown | fda11e6 | 2009-12-26 23:02:24 -0500 | [diff] [blame] | 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); |
| 209 | return; |
| 210 | } |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 211 | |
| 212 | obj = (union acpi_object *)response.pointer; |
| 213 | |
| 214 | if (obj && obj->type == ACPI_TYPE_BUFFER) { |
Rezwanul Kabir | 5ea2559 | 2009-11-02 12:00:42 -0500 | [diff] [blame] | 215 | 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 Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 239 | 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 Kabir | 5ea2559 | 2009-11-02 12:00:42 -0500 | [diff] [blame] | 243 | } |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 244 | } |
Anisse Astier | 3e9b988 | 2009-12-04 10:10:09 +0100 | [diff] [blame] | 245 | kfree(obj); |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Rezwanul Kabir | 5ea2559 | 2009-11-02 12:00:42 -0500 | [diff] [blame] | 248 | |
| 249 | static 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 | |
| 277 | static 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 Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 288 | static 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 | |
| 327 | static int __init dell_wmi_init(void) |
| 328 | { |
| 329 | int err; |
Len Brown | abb631b | 2009-12-26 21:51:38 -0500 | [diff] [blame] | 330 | acpi_status status; |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 331 | |
Dmitry Torokhov | 7a9568f | 2009-12-25 11:49:35 -0800 | [diff] [blame] | 332 | if (!wmi_has_guid(DELL_EVENT_GUID)) { |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 333 | printk(KERN_WARNING "dell-wmi: No known WMI GUID found\n"); |
Dmitry Torokhov | 1fdd407 | 2009-12-17 22:19:42 -0800 | [diff] [blame] | 334 | 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 Brown | abb631b | 2009-12-26 21:51:38 -0500 | [diff] [blame] | 344 | status = wmi_install_notify_handler(DELL_EVENT_GUID, |
Dmitry Torokhov | 1fdd407 | 2009-12-17 22:19:42 -0800 | [diff] [blame] | 345 | dell_wmi_notify, NULL); |
Len Brown | abb631b | 2009-12-26 21:51:38 -0500 | [diff] [blame] | 346 | if (ACPI_FAILURE(status)) { |
Dmitry Torokhov | 1fdd407 | 2009-12-17 22:19:42 -0800 | [diff] [blame] | 347 | input_unregister_device(dell_wmi_input_dev); |
| 348 | printk(KERN_ERR |
| 349 | "dell-wmi: Unable to register notify handler - %d\n", |
Len Brown | abb631b | 2009-12-26 21:51:38 -0500 | [diff] [blame] | 350 | status); |
| 351 | return -ENODEV; |
Dmitry Torokhov | 1fdd407 | 2009-12-17 22:19:42 -0800 | [diff] [blame] | 352 | } |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 353 | |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | static void __exit dell_wmi_exit(void) |
| 358 | { |
Dmitry Torokhov | 1fdd407 | 2009-12-17 22:19:42 -0800 | [diff] [blame] | 359 | wmi_remove_notify_handler(DELL_EVENT_GUID); |
| 360 | input_unregister_device(dell_wmi_input_dev); |
Matthew Garrett | 0b3f610 | 2009-01-09 20:17:11 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | module_init(dell_wmi_init); |
| 364 | module_exit(dell_wmi_exit); |