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/platform_device.h>
34 #include <linux/acpi.h>
35 #include <linux/rfkill.h>
36 #include <linux/string.h>
38 MODULE_AUTHOR("Matthew Garrett <mjg59@srcf.ucam.org>");
39 MODULE_DESCRIPTION("HP laptop WMI hotkeys driver");
40 MODULE_LICENSE("GPL");
42 MODULE_ALIAS("wmi:95F24279-4D7B-4334-9387-ACCDC67EF61C");
43 MODULE_ALIAS("wmi:5FB7F034-2C63-45e9-BE91-3D44E2C707E4");
45 #define HPWMI_EVENT_GUID "95F24279-4D7B-4334-9387-ACCDC67EF61C"
46 #define HPWMI_BIOS_GUID "5FB7F034-2C63-45e9-BE91-3D44E2C707E4"
48 #define HPWMI_DISPLAY_QUERY 0x1
49 #define HPWMI_HDDTEMP_QUERY 0x2
50 #define HPWMI_ALS_QUERY 0x3
51 #define HPWMI_HARDWARE_QUERY 0x4
52 #define HPWMI_WIRELESS_QUERY 0x5
53 #define HPWMI_HOTKEY_QUERY 0xc
61 enum hp_wmi_event_ids {
63 HPWMI_BEZEL_BUTTON = 4,
67 static int __devinit hp_wmi_bios_setup(struct platform_device *device);
68 static int __exit hp_wmi_bios_remove(struct platform_device *device);
69 static int hp_wmi_resume_handler(struct device *device);
86 char type; /* See KE_* below */
91 enum { KE_KEY, KE_END };
93 static struct key_entry hp_wmi_keymap[] = {
94 {KE_KEY, 0x02, KEY_BRIGHTNESSUP},
95 {KE_KEY, 0x03, KEY_BRIGHTNESSDOWN},
96 {KE_KEY, 0x20e6, KEY_PROG1},
97 {KE_KEY, 0x2142, KEY_MEDIA},
98 {KE_KEY, 0x213b, KEY_INFO},
99 {KE_KEY, 0x2169, KEY_DIRECTION},
100 {KE_KEY, 0x231b, KEY_HELP},
104 static struct input_dev *hp_wmi_input_dev;
105 static struct platform_device *hp_wmi_platform_dev;
107 static struct rfkill *wifi_rfkill;
108 static struct rfkill *bluetooth_rfkill;
109 static struct rfkill *wwan_rfkill;
111 static const struct dev_pm_ops hp_wmi_pm_ops = {
112 .resume = hp_wmi_resume_handler,
113 .restore = hp_wmi_resume_handler,
116 static struct platform_driver hp_wmi_driver = {
119 .owner = THIS_MODULE,
120 .pm = &hp_wmi_pm_ops,
122 .probe = hp_wmi_bios_setup,
123 .remove = hp_wmi_bios_remove,
126 static int hp_wmi_perform_query(int query, int write, int value)
128 struct bios_return bios_return;
130 union acpi_object *obj;
131 struct bios_args args = {
132 .signature = 0x55434553,
133 .command = write ? 0x2 : 0x1,
134 .commandtype = query,
135 .datasize = write ? 0x4 : 0,
138 struct acpi_buffer input = { sizeof(struct bios_args), &args };
139 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
141 status = wmi_evaluate_method(HPWMI_BIOS_GUID, 0, 0x3, &input, &output);
143 obj = output.pointer;
147 else if (obj->type != ACPI_TYPE_BUFFER) {
152 bios_return = *((struct bios_return *)obj->buffer.pointer);
154 if (bios_return.return_code > 0)
155 return bios_return.return_code * -1;
157 return bios_return.value;
160 static int hp_wmi_display_state(void)
162 return hp_wmi_perform_query(HPWMI_DISPLAY_QUERY, 0, 0);
165 static int hp_wmi_hddtemp_state(void)
167 return hp_wmi_perform_query(HPWMI_HDDTEMP_QUERY, 0, 0);
170 static int hp_wmi_als_state(void)
172 return hp_wmi_perform_query(HPWMI_ALS_QUERY, 0, 0);
175 static int hp_wmi_dock_state(void)
177 int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, 0, 0);
185 static int hp_wmi_tablet_state(void)
187 int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, 0, 0);
192 return (ret & 0x4) ? 1 : 0;
195 static int hp_wmi_set_block(void *data, bool blocked)
197 enum hp_wmi_radio r = (enum hp_wmi_radio) data;
198 int query = BIT(r + 8) | ((!blocked) << r);
200 return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, query);
203 static const struct rfkill_ops hp_wmi_rfkill_ops = {
204 .set_block = hp_wmi_set_block,
207 static bool hp_wmi_get_sw_state(enum hp_wmi_radio r)
209 int wireless = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, 0);
210 int mask = 0x200 << (r * 8);
218 static bool hp_wmi_get_hw_state(enum hp_wmi_radio r)
220 int wireless = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, 0);
221 int mask = 0x800 << (r * 8);
229 static ssize_t show_display(struct device *dev, struct device_attribute *attr,
232 int value = hp_wmi_display_state();
235 return sprintf(buf, "%d\n", value);
238 static ssize_t show_hddtemp(struct device *dev, struct device_attribute *attr,
241 int value = hp_wmi_hddtemp_state();
244 return sprintf(buf, "%d\n", value);
247 static ssize_t show_als(struct device *dev, struct device_attribute *attr,
250 int value = hp_wmi_als_state();
253 return sprintf(buf, "%d\n", value);
256 static ssize_t show_dock(struct device *dev, struct device_attribute *attr,
259 int value = hp_wmi_dock_state();
262 return sprintf(buf, "%d\n", value);
265 static ssize_t show_tablet(struct device *dev, struct device_attribute *attr,
268 int value = hp_wmi_tablet_state();
271 return sprintf(buf, "%d\n", value);
274 static ssize_t set_als(struct device *dev, struct device_attribute *attr,
275 const char *buf, size_t count)
277 u32 tmp = simple_strtoul(buf, NULL, 10);
278 hp_wmi_perform_query(HPWMI_ALS_QUERY, 1, tmp);
282 static DEVICE_ATTR(display, S_IRUGO, show_display, NULL);
283 static DEVICE_ATTR(hddtemp, S_IRUGO, show_hddtemp, NULL);
284 static DEVICE_ATTR(als, S_IRUGO | S_IWUSR, show_als, set_als);
285 static DEVICE_ATTR(dock, S_IRUGO, show_dock, NULL);
286 static DEVICE_ATTR(tablet, S_IRUGO, show_tablet, NULL);
288 static struct key_entry *hp_wmi_get_entry_by_scancode(unsigned int code)
290 struct key_entry *key;
292 for (key = hp_wmi_keymap; key->type != KE_END; key++)
293 if (code == key->code)
299 static struct key_entry *hp_wmi_get_entry_by_keycode(unsigned int keycode)
301 struct key_entry *key;
303 for (key = hp_wmi_keymap; key->type != KE_END; key++)
304 if (key->type == KE_KEY && keycode == key->keycode)
310 static int hp_wmi_getkeycode(struct input_dev *dev,
311 unsigned int scancode, unsigned int *keycode)
313 struct key_entry *key = hp_wmi_get_entry_by_scancode(scancode);
315 if (key && key->type == KE_KEY) {
316 *keycode = key->keycode;
323 static int hp_wmi_setkeycode(struct input_dev *dev,
324 unsigned int scancode, unsigned int keycode)
326 struct key_entry *key;
327 unsigned int old_keycode;
329 key = hp_wmi_get_entry_by_scancode(scancode);
330 if (key && key->type == KE_KEY) {
331 old_keycode = key->keycode;
332 key->keycode = keycode;
333 set_bit(keycode, dev->keybit);
334 if (!hp_wmi_get_entry_by_keycode(old_keycode))
335 clear_bit(old_keycode, dev->keybit);
342 static void hp_wmi_notify(u32 value, void *context)
344 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
345 static struct key_entry *key;
346 union acpi_object *obj;
347 int eventcode, key_code;
350 status = wmi_get_event_data(value, &response);
351 if (status != AE_OK) {
352 printk(KERN_INFO "hp-wmi: bad event status 0x%x\n", status);
356 obj = (union acpi_object *)response.pointer;
358 if (!obj || obj->type != ACPI_TYPE_BUFFER || obj->buffer.length != 8) {
359 printk(KERN_INFO "HP WMI: Unknown response received\n");
364 eventcode = *((u8 *) obj->buffer.pointer);
367 case HPWMI_DOCK_EVENT:
368 input_report_switch(hp_wmi_input_dev, SW_DOCK,
369 hp_wmi_dock_state());
370 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
371 hp_wmi_tablet_state());
372 input_sync(hp_wmi_input_dev);
374 case HPWMI_BEZEL_BUTTON:
375 key_code = hp_wmi_perform_query(HPWMI_HOTKEY_QUERY, 0,
377 key = hp_wmi_get_entry_by_scancode(key_code);
381 input_report_key(hp_wmi_input_dev,
383 input_sync(hp_wmi_input_dev);
384 input_report_key(hp_wmi_input_dev,
386 input_sync(hp_wmi_input_dev);
390 printk(KERN_INFO "HP WMI: Unknown key code - 0x%x\n",
395 rfkill_set_states(wifi_rfkill,
396 hp_wmi_get_sw_state(HPWMI_WIFI),
397 hp_wmi_get_hw_state(HPWMI_WIFI));
398 if (bluetooth_rfkill)
399 rfkill_set_states(bluetooth_rfkill,
400 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
401 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
403 rfkill_set_states(wwan_rfkill,
404 hp_wmi_get_sw_state(HPWMI_WWAN),
405 hp_wmi_get_hw_state(HPWMI_WWAN));
408 printk(KERN_INFO "HP WMI: Unknown eventcode - %d\n",
414 static int __init hp_wmi_input_setup(void)
416 struct key_entry *key;
419 hp_wmi_input_dev = input_allocate_device();
421 hp_wmi_input_dev->name = "HP WMI hotkeys";
422 hp_wmi_input_dev->phys = "wmi/input0";
423 hp_wmi_input_dev->id.bustype = BUS_HOST;
424 hp_wmi_input_dev->getkeycode = hp_wmi_getkeycode;
425 hp_wmi_input_dev->setkeycode = hp_wmi_setkeycode;
427 for (key = hp_wmi_keymap; key->type != KE_END; key++) {
430 set_bit(EV_KEY, hp_wmi_input_dev->evbit);
431 set_bit(key->keycode, hp_wmi_input_dev->keybit);
436 set_bit(EV_SW, hp_wmi_input_dev->evbit);
437 set_bit(SW_DOCK, hp_wmi_input_dev->swbit);
438 set_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit);
440 /* Set initial hardware state */
441 input_report_switch(hp_wmi_input_dev, SW_DOCK, hp_wmi_dock_state());
442 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
443 hp_wmi_tablet_state());
444 input_sync(hp_wmi_input_dev);
446 err = input_register_device(hp_wmi_input_dev);
449 input_free_device(hp_wmi_input_dev);
456 static void cleanup_sysfs(struct platform_device *device)
458 device_remove_file(&device->dev, &dev_attr_display);
459 device_remove_file(&device->dev, &dev_attr_hddtemp);
460 device_remove_file(&device->dev, &dev_attr_als);
461 device_remove_file(&device->dev, &dev_attr_dock);
462 device_remove_file(&device->dev, &dev_attr_tablet);
465 static int __devinit hp_wmi_bios_setup(struct platform_device *device)
468 int wireless = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, 0);
470 err = device_create_file(&device->dev, &dev_attr_display);
472 goto add_sysfs_error;
473 err = device_create_file(&device->dev, &dev_attr_hddtemp);
475 goto add_sysfs_error;
476 err = device_create_file(&device->dev, &dev_attr_als);
478 goto add_sysfs_error;
479 err = device_create_file(&device->dev, &dev_attr_dock);
481 goto add_sysfs_error;
482 err = device_create_file(&device->dev, &dev_attr_tablet);
484 goto add_sysfs_error;
486 if (wireless & 0x1) {
487 wifi_rfkill = rfkill_alloc("hp-wifi", &device->dev,
490 (void *) HPWMI_WIFI);
491 rfkill_init_sw_state(wifi_rfkill,
492 hp_wmi_get_sw_state(HPWMI_WIFI));
493 rfkill_set_hw_state(wifi_rfkill,
494 hp_wmi_get_hw_state(HPWMI_WIFI));
495 err = rfkill_register(wifi_rfkill);
497 goto register_wifi_error;
500 if (wireless & 0x2) {
501 bluetooth_rfkill = rfkill_alloc("hp-bluetooth", &device->dev,
502 RFKILL_TYPE_BLUETOOTH,
504 (void *) HPWMI_BLUETOOTH);
505 rfkill_init_sw_state(bluetooth_rfkill,
506 hp_wmi_get_sw_state(HPWMI_BLUETOOTH));
507 rfkill_set_hw_state(bluetooth_rfkill,
508 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
509 err = rfkill_register(bluetooth_rfkill);
511 goto register_bluetooth_error;
514 if (wireless & 0x4) {
515 wwan_rfkill = rfkill_alloc("hp-wwan", &device->dev,
518 (void *) HPWMI_WWAN);
519 rfkill_init_sw_state(wwan_rfkill,
520 hp_wmi_get_sw_state(HPWMI_WWAN));
521 rfkill_set_hw_state(wwan_rfkill,
522 hp_wmi_get_hw_state(HPWMI_WWAN));
523 err = rfkill_register(wwan_rfkill);
525 goto register_wwan_err;
530 rfkill_destroy(wwan_rfkill);
531 if (bluetooth_rfkill)
532 rfkill_unregister(bluetooth_rfkill);
533 register_bluetooth_error:
534 rfkill_destroy(bluetooth_rfkill);
536 rfkill_unregister(wifi_rfkill);
538 rfkill_destroy(wifi_rfkill);
540 cleanup_sysfs(device);
544 static int __exit hp_wmi_bios_remove(struct platform_device *device)
546 cleanup_sysfs(device);
549 rfkill_unregister(wifi_rfkill);
550 rfkill_destroy(wifi_rfkill);
552 if (bluetooth_rfkill) {
553 rfkill_unregister(bluetooth_rfkill);
554 rfkill_destroy(bluetooth_rfkill);
557 rfkill_unregister(wwan_rfkill);
558 rfkill_destroy(wwan_rfkill);
564 static int hp_wmi_resume_handler(struct device *device)
567 * Hardware state may have changed while suspended, so trigger
568 * input events for the current state. As this is a switch,
569 * the input layer will only actually pass it on if the state
572 if (hp_wmi_input_dev) {
573 input_report_switch(hp_wmi_input_dev, SW_DOCK,
574 hp_wmi_dock_state());
575 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
576 hp_wmi_tablet_state());
577 input_sync(hp_wmi_input_dev);
581 rfkill_set_states(wifi_rfkill,
582 hp_wmi_get_sw_state(HPWMI_WIFI),
583 hp_wmi_get_hw_state(HPWMI_WIFI));
584 if (bluetooth_rfkill)
585 rfkill_set_states(bluetooth_rfkill,
586 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
587 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
589 rfkill_set_states(wwan_rfkill,
590 hp_wmi_get_sw_state(HPWMI_WWAN),
591 hp_wmi_get_hw_state(HPWMI_WWAN));
596 static int __init hp_wmi_init(void)
600 if (wmi_has_guid(HPWMI_EVENT_GUID)) {
601 err = wmi_install_notify_handler(HPWMI_EVENT_GUID,
602 hp_wmi_notify, NULL);
603 if (ACPI_SUCCESS(err))
604 hp_wmi_input_setup();
607 if (wmi_has_guid(HPWMI_BIOS_GUID)) {
608 err = platform_driver_register(&hp_wmi_driver);
611 hp_wmi_platform_dev = platform_device_alloc("hp-wmi", -1);
612 if (!hp_wmi_platform_dev) {
613 platform_driver_unregister(&hp_wmi_driver);
616 platform_device_add(hp_wmi_platform_dev);
622 static void __exit hp_wmi_exit(void)
624 if (wmi_has_guid(HPWMI_EVENT_GUID)) {
625 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
626 input_unregister_device(hp_wmi_input_dev);
628 if (hp_wmi_platform_dev) {
629 platform_device_del(hp_wmi_platform_dev);
630 platform_driver_unregister(&hp_wmi_driver);
634 module_init(hp_wmi_init);
635 module_exit(hp_wmi_exit);