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 <linux/platform_device.h>
33 #include <linux/acpi.h>
34 #include <linux/rfkill.h>
35 #include <linux/string.h>
37 MODULE_AUTHOR("Matthew Garrett <mjg59@srcf.ucam.org>");
38 MODULE_DESCRIPTION("HP laptop WMI hotkeys driver");
39 MODULE_LICENSE("GPL");
41 MODULE_ALIAS("wmi:95F24279-4D7B-4334-9387-ACCDC67EF61C");
42 MODULE_ALIAS("wmi:5FB7F034-2C63-45e9-BE91-3D44E2C707E4");
44 #define HPWMI_EVENT_GUID "95F24279-4D7B-4334-9387-ACCDC67EF61C"
45 #define HPWMI_BIOS_GUID "5FB7F034-2C63-45e9-BE91-3D44E2C707E4"
47 #define HPWMI_DISPLAY_QUERY 0x1
48 #define HPWMI_HDDTEMP_QUERY 0x2
49 #define HPWMI_ALS_QUERY 0x3
50 #define HPWMI_HARDWARE_QUERY 0x4
51 #define HPWMI_WIRELESS_QUERY 0x5
52 #define HPWMI_HOTKEY_QUERY 0xc
54 #define PREFIX "HP WMI: "
55 #define UNIMP "Unimplemented "
63 enum hp_wmi_event_ids {
66 HPWMI_SMART_ADAPTER = 3,
67 HPWMI_BEZEL_BUTTON = 4,
69 HPWMI_CPU_BATTERY_THROTTLE = 6,
70 HPWMI_LOCK_SWITCH = 7,
73 static int __devinit hp_wmi_bios_setup(struct platform_device *device);
74 static int __exit hp_wmi_bios_remove(struct platform_device *device);
75 static int hp_wmi_resume_handler(struct device *device);
91 char type; /* See KE_* below */
96 enum { KE_KEY, KE_END };
98 static struct key_entry hp_wmi_keymap[] = {
99 {KE_KEY, 0x02, KEY_BRIGHTNESSUP},
100 {KE_KEY, 0x03, KEY_BRIGHTNESSDOWN},
101 {KE_KEY, 0x20e6, KEY_PROG1},
102 {KE_KEY, 0x20e8, KEY_MEDIA},
103 {KE_KEY, 0x2142, KEY_MEDIA},
104 {KE_KEY, 0x213b, KEY_INFO},
105 {KE_KEY, 0x2169, KEY_DIRECTION},
106 {KE_KEY, 0x231b, KEY_HELP},
110 static struct input_dev *hp_wmi_input_dev;
111 static struct platform_device *hp_wmi_platform_dev;
113 static struct rfkill *wifi_rfkill;
114 static struct rfkill *bluetooth_rfkill;
115 static struct rfkill *wwan_rfkill;
117 static const struct dev_pm_ops hp_wmi_pm_ops = {
118 .resume = hp_wmi_resume_handler,
119 .restore = hp_wmi_resume_handler,
122 static struct platform_driver hp_wmi_driver = {
125 .owner = THIS_MODULE,
126 .pm = &hp_wmi_pm_ops,
128 .probe = hp_wmi_bios_setup,
129 .remove = hp_wmi_bios_remove,
133 * hp_wmi_perform_query
135 * query: The commandtype -> What should be queried
136 * write: The command -> 0 read, 1 write, 3 ODM specific
137 * buffer: Buffer used as input and/or output
138 * buffersize: Size of buffer
140 * returns zero on success
141 * an HP WMI query specific error code (which is positive)
142 * -EINVAL if the query was not successful at all
143 * -EINVAL if the output buffer size exceeds buffersize
145 * Note: The buffersize must at least be the maximum of the input and output
146 * size. E.g. Battery info query (0x7) is defined to have 1 byte input
147 * and 128 byte output. The caller would do:
148 * buffer = kzalloc(128, GFP_KERNEL);
149 * ret = hp_wmi_perform_query(0x7, 0, buffer, 128)
151 static int hp_wmi_perform_query(int query, int write, char *buffer,
154 struct bios_return bios_return;
156 union acpi_object *obj;
157 struct bios_args args = {
158 .signature = 0x55434553,
159 .command = write ? 0x2 : 0x1,
160 .commandtype = query,
161 .datasize = buffersize,
164 struct acpi_buffer input = { sizeof(struct bios_args), &args };
165 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
167 status = wmi_evaluate_method(HPWMI_BIOS_GUID, 0, 0x3, &input, &output);
169 obj = output.pointer;
173 else if (obj->type != ACPI_TYPE_BUFFER) {
178 bios_return = *((struct bios_return *)obj->buffer.pointer);
180 if (bios_return.return_code) {
181 printk(KERN_WARNING PREFIX "Query %d returned %d\n", query,
182 bios_return.return_code);
184 return bios_return.return_code;
186 if (obj->buffer.length - sizeof(bios_return) > buffersize) {
191 memset(buffer, 0, buffersize);
193 ((char *)obj->buffer.pointer) + sizeof(struct bios_return),
194 obj->buffer.length - sizeof(bios_return));
199 static int hp_wmi_display_state(void)
202 int ret = hp_wmi_perform_query(HPWMI_DISPLAY_QUERY, 0, (char *)&state,
209 static int hp_wmi_hddtemp_state(void)
212 int ret = hp_wmi_perform_query(HPWMI_HDDTEMP_QUERY, 0, (char *)&state,
219 static int hp_wmi_als_state(void)
222 int ret = hp_wmi_perform_query(HPWMI_ALS_QUERY, 0, (char *)&state,
229 static int hp_wmi_dock_state(void)
232 int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, 0, (char *)&state,
241 static int hp_wmi_tablet_state(void)
244 int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, 0, (char *)&state,
249 return (state & 0x4) ? 1 : 0;
252 static int hp_wmi_set_block(void *data, bool blocked)
254 enum hp_wmi_radio r = (enum hp_wmi_radio) data;
255 int query = BIT(r + 8) | ((!blocked) << r);
258 ret = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1,
259 (char *)&query, sizeof(query));
265 static const struct rfkill_ops hp_wmi_rfkill_ops = {
266 .set_block = hp_wmi_set_block,
269 static bool hp_wmi_get_sw_state(enum hp_wmi_radio r)
273 hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0,
274 (char *)&wireless, sizeof(wireless));
275 /* TBD: Pass error */
277 mask = 0x200 << (r * 8);
285 static bool hp_wmi_get_hw_state(enum hp_wmi_radio r)
289 hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0,
290 (char *)&wireless, sizeof(wireless));
291 /* TBD: Pass error */
293 mask = 0x800 << (r * 8);
301 static ssize_t show_display(struct device *dev, struct device_attribute *attr,
304 int value = hp_wmi_display_state();
307 return sprintf(buf, "%d\n", value);
310 static ssize_t show_hddtemp(struct device *dev, struct device_attribute *attr,
313 int value = hp_wmi_hddtemp_state();
316 return sprintf(buf, "%d\n", value);
319 static ssize_t show_als(struct device *dev, struct device_attribute *attr,
322 int value = hp_wmi_als_state();
325 return sprintf(buf, "%d\n", value);
328 static ssize_t show_dock(struct device *dev, struct device_attribute *attr,
331 int value = hp_wmi_dock_state();
334 return sprintf(buf, "%d\n", value);
337 static ssize_t show_tablet(struct device *dev, struct device_attribute *attr,
340 int value = hp_wmi_tablet_state();
343 return sprintf(buf, "%d\n", value);
346 static ssize_t set_als(struct device *dev, struct device_attribute *attr,
347 const char *buf, size_t count)
349 u32 tmp = simple_strtoul(buf, NULL, 10);
350 int ret = hp_wmi_perform_query(HPWMI_ALS_QUERY, 1, (char *)&tmp,
358 static DEVICE_ATTR(display, S_IRUGO, show_display, NULL);
359 static DEVICE_ATTR(hddtemp, S_IRUGO, show_hddtemp, NULL);
360 static DEVICE_ATTR(als, S_IRUGO | S_IWUSR, show_als, set_als);
361 static DEVICE_ATTR(dock, S_IRUGO, show_dock, NULL);
362 static DEVICE_ATTR(tablet, S_IRUGO, show_tablet, NULL);
364 static struct key_entry *hp_wmi_get_entry_by_scancode(unsigned int code)
366 struct key_entry *key;
368 for (key = hp_wmi_keymap; key->type != KE_END; key++)
369 if (code == key->code)
375 static struct key_entry *hp_wmi_get_entry_by_keycode(unsigned int keycode)
377 struct key_entry *key;
379 for (key = hp_wmi_keymap; key->type != KE_END; key++)
380 if (key->type == KE_KEY && keycode == key->keycode)
386 static int hp_wmi_getkeycode(struct input_dev *dev,
387 unsigned int scancode, unsigned int *keycode)
389 struct key_entry *key = hp_wmi_get_entry_by_scancode(scancode);
391 if (key && key->type == KE_KEY) {
392 *keycode = key->keycode;
399 static int hp_wmi_setkeycode(struct input_dev *dev,
400 unsigned int scancode, unsigned int keycode)
402 struct key_entry *key;
403 unsigned int old_keycode;
405 key = hp_wmi_get_entry_by_scancode(scancode);
406 if (key && key->type == KE_KEY) {
407 old_keycode = key->keycode;
408 key->keycode = keycode;
409 set_bit(keycode, dev->keybit);
410 if (!hp_wmi_get_entry_by_keycode(old_keycode))
411 clear_bit(old_keycode, dev->keybit);
418 static void hp_wmi_notify(u32 value, void *context)
420 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
421 static struct key_entry *key;
422 union acpi_object *obj;
423 u32 event_id, event_data;
428 status = wmi_get_event_data(value, &response);
429 if (status != AE_OK) {
430 printk(KERN_INFO PREFIX "bad event status 0x%x\n", status);
434 obj = (union acpi_object *)response.pointer;
438 if (obj->type != ACPI_TYPE_BUFFER) {
439 printk(KERN_INFO "hp-wmi: Unknown response received %d\n",
446 * Depending on ACPI version the concatenation of id and event data
447 * inside _WED function will result in a 8 or 16 byte buffer.
449 location = (u32 *)obj->buffer.pointer;
450 if (obj->buffer.length == 8) {
451 event_id = *location;
452 event_data = *(location + 1);
453 } else if (obj->buffer.length == 16) {
454 event_id = *location;
455 event_data = *(location + 2);
457 printk(KERN_INFO "hp-wmi: Unknown buffer length %d\n",
465 case HPWMI_DOCK_EVENT:
466 input_report_switch(hp_wmi_input_dev, SW_DOCK,
467 hp_wmi_dock_state());
468 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
469 hp_wmi_tablet_state());
470 input_sync(hp_wmi_input_dev);
474 case HPWMI_SMART_ADAPTER:
476 case HPWMI_BEZEL_BUTTON:
477 ret = hp_wmi_perform_query(HPWMI_HOTKEY_QUERY, 0,
482 key = hp_wmi_get_entry_by_scancode(key_code);
486 input_report_key(hp_wmi_input_dev,
488 input_sync(hp_wmi_input_dev);
489 input_report_key(hp_wmi_input_dev,
491 input_sync(hp_wmi_input_dev);
495 printk(KERN_INFO PREFIX "Unknown key code - 0x%x\n",
500 rfkill_set_states(wifi_rfkill,
501 hp_wmi_get_sw_state(HPWMI_WIFI),
502 hp_wmi_get_hw_state(HPWMI_WIFI));
503 if (bluetooth_rfkill)
504 rfkill_set_states(bluetooth_rfkill,
505 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
506 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
508 rfkill_set_states(wwan_rfkill,
509 hp_wmi_get_sw_state(HPWMI_WWAN),
510 hp_wmi_get_hw_state(HPWMI_WWAN));
512 case HPWMI_CPU_BATTERY_THROTTLE:
513 printk(KERN_INFO PREFIX UNIMP "CPU throttle because of 3 Cell"
514 " battery event detected\n");
516 case HPWMI_LOCK_SWITCH:
519 printk(KERN_INFO PREFIX "Unknown event_id - %d - 0x%x\n",
520 event_id, event_data);
525 static int __init hp_wmi_input_setup(void)
527 struct key_entry *key;
530 hp_wmi_input_dev = input_allocate_device();
531 if (!hp_wmi_input_dev)
534 hp_wmi_input_dev->name = "HP WMI hotkeys";
535 hp_wmi_input_dev->phys = "wmi/input0";
536 hp_wmi_input_dev->id.bustype = BUS_HOST;
537 hp_wmi_input_dev->getkeycode = hp_wmi_getkeycode;
538 hp_wmi_input_dev->setkeycode = hp_wmi_setkeycode;
540 for (key = hp_wmi_keymap; key->type != KE_END; key++) {
543 set_bit(EV_KEY, hp_wmi_input_dev->evbit);
544 set_bit(key->keycode, hp_wmi_input_dev->keybit);
549 set_bit(EV_SW, hp_wmi_input_dev->evbit);
550 set_bit(SW_DOCK, hp_wmi_input_dev->swbit);
551 set_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit);
553 /* Set initial hardware state */
554 input_report_switch(hp_wmi_input_dev, SW_DOCK, hp_wmi_dock_state());
555 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
556 hp_wmi_tablet_state());
557 input_sync(hp_wmi_input_dev);
559 err = input_register_device(hp_wmi_input_dev);
562 input_free_device(hp_wmi_input_dev);
569 static void cleanup_sysfs(struct platform_device *device)
571 device_remove_file(&device->dev, &dev_attr_display);
572 device_remove_file(&device->dev, &dev_attr_hddtemp);
573 device_remove_file(&device->dev, &dev_attr_als);
574 device_remove_file(&device->dev, &dev_attr_dock);
575 device_remove_file(&device->dev, &dev_attr_tablet);
578 static int __devinit hp_wmi_bios_setup(struct platform_device *device)
583 err = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, (char *)&wireless,
588 err = device_create_file(&device->dev, &dev_attr_display);
590 goto add_sysfs_error;
591 err = device_create_file(&device->dev, &dev_attr_hddtemp);
593 goto add_sysfs_error;
594 err = device_create_file(&device->dev, &dev_attr_als);
596 goto add_sysfs_error;
597 err = device_create_file(&device->dev, &dev_attr_dock);
599 goto add_sysfs_error;
600 err = device_create_file(&device->dev, &dev_attr_tablet);
602 goto add_sysfs_error;
604 if (wireless & 0x1) {
605 wifi_rfkill = rfkill_alloc("hp-wifi", &device->dev,
608 (void *) HPWMI_WIFI);
609 rfkill_init_sw_state(wifi_rfkill,
610 hp_wmi_get_sw_state(HPWMI_WIFI));
611 rfkill_set_hw_state(wifi_rfkill,
612 hp_wmi_get_hw_state(HPWMI_WIFI));
613 err = rfkill_register(wifi_rfkill);
615 goto register_wifi_error;
618 if (wireless & 0x2) {
619 bluetooth_rfkill = rfkill_alloc("hp-bluetooth", &device->dev,
620 RFKILL_TYPE_BLUETOOTH,
622 (void *) HPWMI_BLUETOOTH);
623 rfkill_init_sw_state(bluetooth_rfkill,
624 hp_wmi_get_sw_state(HPWMI_BLUETOOTH));
625 rfkill_set_hw_state(bluetooth_rfkill,
626 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
627 err = rfkill_register(bluetooth_rfkill);
629 goto register_bluetooth_error;
632 if (wireless & 0x4) {
633 wwan_rfkill = rfkill_alloc("hp-wwan", &device->dev,
636 (void *) HPWMI_WWAN);
637 rfkill_init_sw_state(wwan_rfkill,
638 hp_wmi_get_sw_state(HPWMI_WWAN));
639 rfkill_set_hw_state(wwan_rfkill,
640 hp_wmi_get_hw_state(HPWMI_WWAN));
641 err = rfkill_register(wwan_rfkill);
643 goto register_wwan_err;
648 rfkill_destroy(wwan_rfkill);
649 if (bluetooth_rfkill)
650 rfkill_unregister(bluetooth_rfkill);
651 register_bluetooth_error:
652 rfkill_destroy(bluetooth_rfkill);
654 rfkill_unregister(wifi_rfkill);
656 rfkill_destroy(wifi_rfkill);
658 cleanup_sysfs(device);
662 static int __exit hp_wmi_bios_remove(struct platform_device *device)
664 cleanup_sysfs(device);
667 rfkill_unregister(wifi_rfkill);
668 rfkill_destroy(wifi_rfkill);
670 if (bluetooth_rfkill) {
671 rfkill_unregister(bluetooth_rfkill);
672 rfkill_destroy(bluetooth_rfkill);
675 rfkill_unregister(wwan_rfkill);
676 rfkill_destroy(wwan_rfkill);
682 static int hp_wmi_resume_handler(struct device *device)
685 * Hardware state may have changed while suspended, so trigger
686 * input events for the current state. As this is a switch,
687 * the input layer will only actually pass it on if the state
690 if (hp_wmi_input_dev) {
691 input_report_switch(hp_wmi_input_dev, SW_DOCK,
692 hp_wmi_dock_state());
693 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
694 hp_wmi_tablet_state());
695 input_sync(hp_wmi_input_dev);
699 rfkill_set_states(wifi_rfkill,
700 hp_wmi_get_sw_state(HPWMI_WIFI),
701 hp_wmi_get_hw_state(HPWMI_WIFI));
702 if (bluetooth_rfkill)
703 rfkill_set_states(bluetooth_rfkill,
704 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
705 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
707 rfkill_set_states(wwan_rfkill,
708 hp_wmi_get_sw_state(HPWMI_WWAN),
709 hp_wmi_get_hw_state(HPWMI_WWAN));
714 static int __init hp_wmi_init(void)
717 int event_capable = wmi_has_guid(HPWMI_EVENT_GUID);
718 int bios_capable = wmi_has_guid(HPWMI_BIOS_GUID);
721 err = wmi_install_notify_handler(HPWMI_EVENT_GUID,
722 hp_wmi_notify, NULL);
723 if (ACPI_FAILURE(err))
725 err = hp_wmi_input_setup();
727 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
733 err = platform_driver_register(&hp_wmi_driver);
736 hp_wmi_platform_dev = platform_device_alloc("hp-wmi", -1);
737 if (!hp_wmi_platform_dev) {
739 goto err_device_alloc;
741 err = platform_device_add(hp_wmi_platform_dev);
746 if (!bios_capable && !event_capable)
752 platform_device_put(hp_wmi_platform_dev);
754 platform_driver_unregister(&hp_wmi_driver);
756 if (wmi_has_guid(HPWMI_EVENT_GUID)) {
757 input_unregister_device(hp_wmi_input_dev);
758 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
764 static void __exit hp_wmi_exit(void)
766 if (wmi_has_guid(HPWMI_EVENT_GUID)) {
767 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
768 input_unregister_device(hp_wmi_input_dev);
770 if (hp_wmi_platform_dev) {
771 platform_device_unregister(hp_wmi_platform_dev);
772 platform_driver_unregister(&hp_wmi_driver);
776 module_init(hp_wmi_init);
777 module_exit(hp_wmi_exit);