Matthew Garrett | 62ec30d | 2008-07-25 01:45:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * HP 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/platform_device.h> |
| 33 | #include <linux/acpi.h> |
| 34 | #include <linux/rfkill.h> |
| 35 | #include <linux/string.h> |
| 36 | |
| 37 | MODULE_AUTHOR("Matthew Garrett <mjg59@srcf.ucam.org>"); |
| 38 | MODULE_DESCRIPTION("HP laptop WMI hotkeys driver"); |
| 39 | MODULE_LICENSE("GPL"); |
| 40 | |
| 41 | MODULE_ALIAS("wmi:95F24279-4D7B-4334-9387-ACCDC67EF61C"); |
| 42 | MODULE_ALIAS("wmi:5FB7F034-2C63-45e9-BE91-3D44E2C707E4"); |
| 43 | |
| 44 | #define HPWMI_EVENT_GUID "95F24279-4D7B-4334-9387-ACCDC67EF61C" |
| 45 | #define HPWMI_BIOS_GUID "5FB7F034-2C63-45e9-BE91-3D44E2C707E4" |
| 46 | |
| 47 | #define HPWMI_DISPLAY_QUERY 0x1 |
| 48 | #define HPWMI_HDDTEMP_QUERY 0x2 |
| 49 | #define HPWMI_ALS_QUERY 0x3 |
| 50 | #define HPWMI_DOCK_QUERY 0x4 |
| 51 | #define HPWMI_WIRELESS_QUERY 0x5 |
| 52 | |
| 53 | static int __init hp_wmi_bios_setup(struct platform_device *device); |
| 54 | static int __exit hp_wmi_bios_remove(struct platform_device *device); |
| 55 | |
| 56 | struct bios_args { |
| 57 | u32 signature; |
| 58 | u32 command; |
| 59 | u32 commandtype; |
| 60 | u32 datasize; |
| 61 | u32 data; |
| 62 | }; |
| 63 | |
| 64 | struct bios_return { |
| 65 | u32 sigpass; |
| 66 | u32 return_code; |
| 67 | u32 value; |
| 68 | }; |
| 69 | |
| 70 | struct key_entry { |
| 71 | char type; /* See KE_* below */ |
| 72 | u8 code; |
| 73 | u16 keycode; |
| 74 | }; |
| 75 | |
| 76 | enum { KE_KEY, KE_SW, KE_END }; |
| 77 | |
| 78 | static struct key_entry hp_wmi_keymap[] = { |
| 79 | {KE_SW, 0x01, SW_DOCK}, |
| 80 | {KE_KEY, 0x02, KEY_BRIGHTNESSUP}, |
| 81 | {KE_KEY, 0x03, KEY_BRIGHTNESSDOWN}, |
| 82 | {KE_KEY, 0x04, KEY_HELP}, |
| 83 | {KE_END, 0} |
| 84 | }; |
| 85 | |
| 86 | static struct input_dev *hp_wmi_input_dev; |
| 87 | static struct platform_device *hp_wmi_platform_dev; |
| 88 | |
| 89 | static struct rfkill *wifi_rfkill; |
| 90 | static struct rfkill *bluetooth_rfkill; |
| 91 | static struct rfkill *wwan_rfkill; |
| 92 | |
| 93 | static struct platform_driver hp_wmi_driver = { |
| 94 | .driver = { |
| 95 | .name = "hp-wmi", |
| 96 | .owner = THIS_MODULE, |
| 97 | }, |
| 98 | .probe = hp_wmi_bios_setup, |
| 99 | .remove = hp_wmi_bios_remove, |
| 100 | }; |
| 101 | |
| 102 | static int hp_wmi_perform_query(int query, int write, int value) |
| 103 | { |
| 104 | struct bios_return bios_return; |
| 105 | acpi_status status; |
| 106 | union acpi_object *obj; |
| 107 | struct bios_args args = { |
| 108 | .signature = 0x55434553, |
| 109 | .command = write ? 0x2 : 0x1, |
| 110 | .commandtype = query, |
| 111 | .datasize = write ? 0x4 : 0, |
| 112 | .data = value, |
| 113 | }; |
| 114 | struct acpi_buffer input = { sizeof(struct bios_args), &args }; |
| 115 | struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 116 | |
| 117 | status = wmi_evaluate_method(HPWMI_BIOS_GUID, 0, 0x3, &input, &output); |
| 118 | |
| 119 | obj = output.pointer; |
| 120 | |
| 121 | if (!obj || obj->type != ACPI_TYPE_BUFFER) |
| 122 | return -EINVAL; |
| 123 | |
| 124 | bios_return = *((struct bios_return *)obj->buffer.pointer); |
| 125 | if (bios_return.return_code > 0) |
| 126 | return bios_return.return_code * -1; |
| 127 | else |
| 128 | return bios_return.value; |
| 129 | } |
| 130 | |
| 131 | static int hp_wmi_display_state(void) |
| 132 | { |
| 133 | return hp_wmi_perform_query(HPWMI_DISPLAY_QUERY, 0, 0); |
| 134 | } |
| 135 | |
| 136 | static int hp_wmi_hddtemp_state(void) |
| 137 | { |
| 138 | return hp_wmi_perform_query(HPWMI_HDDTEMP_QUERY, 0, 0); |
| 139 | } |
| 140 | |
| 141 | static int hp_wmi_als_state(void) |
| 142 | { |
| 143 | return hp_wmi_perform_query(HPWMI_ALS_QUERY, 0, 0); |
| 144 | } |
| 145 | |
| 146 | static int hp_wmi_dock_state(void) |
| 147 | { |
| 148 | return hp_wmi_perform_query(HPWMI_DOCK_QUERY, 0, 0); |
| 149 | } |
| 150 | |
| 151 | static int hp_wmi_wifi_set(void *data, enum rfkill_state state) |
| 152 | { |
| 153 | if (state) |
| 154 | return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, 0x101); |
| 155 | else |
| 156 | return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, 0x100); |
| 157 | } |
| 158 | |
| 159 | static int hp_wmi_bluetooth_set(void *data, enum rfkill_state state) |
| 160 | { |
| 161 | if (state) |
| 162 | return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, 0x202); |
| 163 | else |
| 164 | return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, 0x200); |
| 165 | } |
| 166 | |
| 167 | static int hp_wmi_wwan_set(void *data, enum rfkill_state state) |
| 168 | { |
| 169 | if (state) |
| 170 | return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, 0x404); |
| 171 | else |
| 172 | return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, 0x400); |
| 173 | } |
| 174 | |
| 175 | static int hp_wmi_wifi_state(void) |
| 176 | { |
| 177 | int wireless = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, 0); |
| 178 | |
| 179 | if (wireless & 0x100) |
| 180 | return 1; |
| 181 | else |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static int hp_wmi_bluetooth_state(void) |
| 186 | { |
| 187 | int wireless = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, 0); |
| 188 | |
| 189 | if (wireless & 0x10000) |
| 190 | return 1; |
| 191 | else |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | static int hp_wmi_wwan_state(void) |
| 196 | { |
| 197 | int wireless = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, 0); |
| 198 | |
| 199 | if (wireless & 0x1000000) |
| 200 | return 1; |
| 201 | else |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | static ssize_t show_display(struct device *dev, struct device_attribute *attr, |
| 206 | char *buf) |
| 207 | { |
| 208 | int value = hp_wmi_display_state(); |
| 209 | if (value < 0) |
| 210 | return -EINVAL; |
| 211 | return sprintf(buf, "%d\n", value); |
| 212 | } |
| 213 | |
| 214 | static ssize_t show_hddtemp(struct device *dev, struct device_attribute *attr, |
| 215 | char *buf) |
| 216 | { |
| 217 | int value = hp_wmi_hddtemp_state(); |
| 218 | if (value < 0) |
| 219 | return -EINVAL; |
| 220 | return sprintf(buf, "%d\n", value); |
| 221 | } |
| 222 | |
| 223 | static ssize_t show_als(struct device *dev, struct device_attribute *attr, |
| 224 | char *buf) |
| 225 | { |
| 226 | int value = hp_wmi_als_state(); |
| 227 | if (value < 0) |
| 228 | return -EINVAL; |
| 229 | return sprintf(buf, "%d\n", value); |
| 230 | } |
| 231 | |
| 232 | static ssize_t show_dock(struct device *dev, struct device_attribute *attr, |
| 233 | char *buf) |
| 234 | { |
| 235 | int value = hp_wmi_dock_state(); |
| 236 | if (value < 0) |
| 237 | return -EINVAL; |
| 238 | return sprintf(buf, "%d\n", value); |
| 239 | } |
| 240 | |
| 241 | static ssize_t set_als(struct device *dev, struct device_attribute *attr, |
| 242 | const char *buf, size_t count) |
| 243 | { |
| 244 | u32 tmp = simple_strtoul(buf, NULL, 10); |
| 245 | hp_wmi_perform_query(HPWMI_ALS_QUERY, 1, tmp); |
| 246 | return count; |
| 247 | } |
| 248 | |
| 249 | static DEVICE_ATTR(display, S_IRUGO, show_display, NULL); |
| 250 | static DEVICE_ATTR(hddtemp, S_IRUGO, show_hddtemp, NULL); |
| 251 | static DEVICE_ATTR(als, S_IRUGO | S_IWUSR, show_als, set_als); |
| 252 | static DEVICE_ATTR(dock, S_IRUGO, show_dock, NULL); |
| 253 | |
| 254 | static struct key_entry *hp_wmi_get_entry_by_scancode(int code) |
| 255 | { |
| 256 | struct key_entry *key; |
| 257 | |
| 258 | for (key = hp_wmi_keymap; key->type != KE_END; key++) |
| 259 | if (code == key->code) |
| 260 | return key; |
| 261 | |
| 262 | return NULL; |
| 263 | } |
| 264 | |
| 265 | static struct key_entry *hp_wmi_get_entry_by_keycode(int keycode) |
| 266 | { |
| 267 | struct key_entry *key; |
| 268 | |
| 269 | for (key = hp_wmi_keymap; key->type != KE_END; key++) |
| 270 | if (key->type == KE_KEY && keycode == key->keycode) |
| 271 | return key; |
| 272 | |
| 273 | return NULL; |
| 274 | } |
| 275 | |
| 276 | static int hp_wmi_getkeycode(struct input_dev *dev, int scancode, int *keycode) |
| 277 | { |
| 278 | struct key_entry *key = hp_wmi_get_entry_by_scancode(scancode); |
| 279 | |
| 280 | if (key && key->type == KE_KEY) { |
| 281 | *keycode = key->keycode; |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | return -EINVAL; |
| 286 | } |
| 287 | |
| 288 | static int hp_wmi_setkeycode(struct input_dev *dev, int scancode, int keycode) |
| 289 | { |
| 290 | struct key_entry *key; |
| 291 | int old_keycode; |
| 292 | |
| 293 | if (keycode < 0 || keycode > KEY_MAX) |
| 294 | return -EINVAL; |
| 295 | |
| 296 | key = hp_wmi_get_entry_by_scancode(scancode); |
| 297 | if (key && key->type == KE_KEY) { |
| 298 | old_keycode = key->keycode; |
| 299 | key->keycode = keycode; |
| 300 | set_bit(keycode, dev->keybit); |
| 301 | if (!hp_wmi_get_entry_by_keycode(old_keycode)) |
| 302 | clear_bit(old_keycode, dev->keybit); |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | return -EINVAL; |
| 307 | } |
| 308 | |
| 309 | void hp_wmi_notify(u32 value, void *context) |
| 310 | { |
| 311 | struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 312 | static struct key_entry *key; |
| 313 | union acpi_object *obj; |
| 314 | |
| 315 | wmi_get_event_data(value, &response); |
| 316 | |
| 317 | obj = (union acpi_object *)response.pointer; |
| 318 | |
| 319 | if (obj && obj->type == ACPI_TYPE_BUFFER && obj->buffer.length == 8) { |
| 320 | int eventcode = *((u8 *) obj->buffer.pointer); |
| 321 | key = hp_wmi_get_entry_by_scancode(eventcode); |
| 322 | if (key) { |
| 323 | switch (key->type) { |
| 324 | case KE_KEY: |
| 325 | input_report_key(hp_wmi_input_dev, |
| 326 | key->keycode, 1); |
| 327 | input_sync(hp_wmi_input_dev); |
| 328 | input_report_key(hp_wmi_input_dev, |
| 329 | key->keycode, 0); |
| 330 | input_sync(hp_wmi_input_dev); |
| 331 | break; |
| 332 | case KE_SW: |
| 333 | input_report_switch(hp_wmi_input_dev, |
| 334 | key->keycode, |
| 335 | hp_wmi_dock_state()); |
| 336 | input_sync(hp_wmi_input_dev); |
| 337 | break; |
| 338 | } |
| 339 | } else if (eventcode == 0x5) { |
| 340 | if (wifi_rfkill) |
| 341 | wifi_rfkill->state = hp_wmi_wifi_state(); |
| 342 | if (bluetooth_rfkill) |
| 343 | bluetooth_rfkill->state = |
| 344 | hp_wmi_bluetooth_state(); |
| 345 | if (wwan_rfkill) |
| 346 | wwan_rfkill->state = hp_wmi_wwan_state(); |
| 347 | } else |
| 348 | printk(KERN_INFO "HP WMI: Unknown key pressed - %x\n", |
| 349 | eventcode); |
| 350 | } else |
| 351 | printk(KERN_INFO "HP WMI: Unknown response received\n"); |
| 352 | } |
| 353 | |
| 354 | static int __init hp_wmi_input_setup(void) |
| 355 | { |
| 356 | struct key_entry *key; |
| 357 | int err; |
| 358 | |
| 359 | hp_wmi_input_dev = input_allocate_device(); |
| 360 | |
| 361 | hp_wmi_input_dev->name = "HP WMI hotkeys"; |
| 362 | hp_wmi_input_dev->phys = "wmi/input0"; |
| 363 | hp_wmi_input_dev->id.bustype = BUS_HOST; |
| 364 | hp_wmi_input_dev->getkeycode = hp_wmi_getkeycode; |
| 365 | hp_wmi_input_dev->setkeycode = hp_wmi_setkeycode; |
| 366 | |
| 367 | for (key = hp_wmi_keymap; key->type != KE_END; key++) { |
| 368 | switch (key->type) { |
| 369 | case KE_KEY: |
| 370 | set_bit(EV_KEY, hp_wmi_input_dev->evbit); |
| 371 | set_bit(key->keycode, hp_wmi_input_dev->keybit); |
| 372 | break; |
| 373 | case KE_SW: |
| 374 | set_bit(EV_SW, hp_wmi_input_dev->evbit); |
| 375 | set_bit(key->keycode, hp_wmi_input_dev->swbit); |
| 376 | break; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | err = input_register_device(hp_wmi_input_dev); |
| 381 | |
| 382 | if (err) { |
| 383 | input_free_device(hp_wmi_input_dev); |
| 384 | return err; |
| 385 | } |
| 386 | |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | static void cleanup_sysfs(struct platform_device *device) |
| 391 | { |
| 392 | device_remove_file(&device->dev, &dev_attr_display); |
| 393 | device_remove_file(&device->dev, &dev_attr_hddtemp); |
| 394 | device_remove_file(&device->dev, &dev_attr_als); |
| 395 | device_remove_file(&device->dev, &dev_attr_dock); |
| 396 | } |
| 397 | |
| 398 | static int __init hp_wmi_bios_setup(struct platform_device *device) |
| 399 | { |
| 400 | int err; |
| 401 | |
| 402 | err = device_create_file(&device->dev, &dev_attr_display); |
| 403 | if (err) |
| 404 | goto add_sysfs_error; |
| 405 | err = device_create_file(&device->dev, &dev_attr_hddtemp); |
| 406 | if (err) |
| 407 | goto add_sysfs_error; |
| 408 | err = device_create_file(&device->dev, &dev_attr_als); |
| 409 | if (err) |
| 410 | goto add_sysfs_error; |
| 411 | err = device_create_file(&device->dev, &dev_attr_dock); |
| 412 | if (err) |
| 413 | goto add_sysfs_error; |
| 414 | |
| 415 | wifi_rfkill = rfkill_allocate(&device->dev, RFKILL_TYPE_WLAN); |
| 416 | wifi_rfkill->name = "hp-wifi"; |
| 417 | wifi_rfkill->state = hp_wmi_wifi_state(); |
| 418 | wifi_rfkill->toggle_radio = hp_wmi_wifi_set; |
| 419 | wifi_rfkill->user_claim_unsupported = 1; |
| 420 | |
| 421 | bluetooth_rfkill = rfkill_allocate(&device->dev, |
| 422 | RFKILL_TYPE_BLUETOOTH); |
| 423 | bluetooth_rfkill->name = "hp-bluetooth"; |
| 424 | bluetooth_rfkill->state = hp_wmi_bluetooth_state(); |
| 425 | bluetooth_rfkill->toggle_radio = hp_wmi_bluetooth_set; |
| 426 | bluetooth_rfkill->user_claim_unsupported = 1; |
| 427 | |
| 428 | wwan_rfkill = rfkill_allocate(&device->dev, RFKILL_TYPE_WIMAX); |
| 429 | wwan_rfkill->name = "hp-wwan"; |
| 430 | wwan_rfkill->state = hp_wmi_wwan_state(); |
| 431 | wwan_rfkill->toggle_radio = hp_wmi_wwan_set; |
| 432 | wwan_rfkill->user_claim_unsupported = 1; |
| 433 | |
| 434 | rfkill_register(wifi_rfkill); |
| 435 | rfkill_register(bluetooth_rfkill); |
| 436 | rfkill_register(wwan_rfkill); |
| 437 | |
| 438 | return 0; |
| 439 | add_sysfs_error: |
| 440 | cleanup_sysfs(device); |
| 441 | return err; |
| 442 | } |
| 443 | |
| 444 | static int __exit hp_wmi_bios_remove(struct platform_device *device) |
| 445 | { |
| 446 | cleanup_sysfs(device); |
| 447 | |
| 448 | rfkill_unregister(wifi_rfkill); |
| 449 | rfkill_unregister(bluetooth_rfkill); |
| 450 | rfkill_unregister(wwan_rfkill); |
| 451 | |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | static int __init hp_wmi_init(void) |
| 456 | { |
| 457 | int err; |
| 458 | |
| 459 | if (wmi_has_guid(HPWMI_EVENT_GUID)) { |
| 460 | err = wmi_install_notify_handler(HPWMI_EVENT_GUID, |
| 461 | hp_wmi_notify, NULL); |
| 462 | if (!err) |
| 463 | hp_wmi_input_setup(); |
| 464 | } |
| 465 | |
| 466 | if (wmi_has_guid(HPWMI_BIOS_GUID)) { |
| 467 | err = platform_driver_register(&hp_wmi_driver); |
| 468 | if (err) |
| 469 | return 0; |
| 470 | hp_wmi_platform_dev = platform_device_alloc("hp-wmi", -1); |
| 471 | if (!hp_wmi_platform_dev) { |
| 472 | platform_driver_unregister(&hp_wmi_driver); |
| 473 | return 0; |
| 474 | } |
| 475 | platform_device_add(hp_wmi_platform_dev); |
| 476 | } |
| 477 | |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | static void __exit hp_wmi_exit(void) |
| 482 | { |
| 483 | if (wmi_has_guid(HPWMI_EVENT_GUID)) { |
| 484 | wmi_remove_notify_handler(HPWMI_EVENT_GUID); |
| 485 | input_unregister_device(hp_wmi_input_dev); |
| 486 | } |
| 487 | if (hp_wmi_platform_dev) { |
| 488 | platform_device_del(hp_wmi_platform_dev); |
| 489 | platform_driver_unregister(&hp_wmi_driver); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | module_init(hp_wmi_init); |
| 494 | module_exit(hp_wmi_exit); |