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/input/sparse-keymap.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
55 #define PREFIX "HP WMI: "
56 #define UNIMP "Unimplemented "
64 enum hp_wmi_event_ids {
67 HPWMI_SMART_ADAPTER = 3,
68 HPWMI_BEZEL_BUTTON = 4,
70 HPWMI_CPU_BATTERY_THROTTLE = 6,
71 HPWMI_LOCK_SWITCH = 7,
74 static int __devinit hp_wmi_bios_setup(struct platform_device *device);
75 static int __exit hp_wmi_bios_remove(struct platform_device *device);
76 static int hp_wmi_resume_handler(struct device *device);
92 enum hp_return_value {
93 HPWMI_RET_WRONG_SIGNATURE = 0x02,
94 HPWMI_RET_UNKNOWN_COMMAND = 0x03,
95 HPWMI_RET_UNKNOWN_CMDTYPE = 0x04,
96 HPWMI_RET_INVALID_PARAMETERS = 0x05,
99 static const struct key_entry hp_wmi_keymap[] = {
100 { KE_KEY, 0x02, { KEY_BRIGHTNESSUP } },
101 { KE_KEY, 0x03, { KEY_BRIGHTNESSDOWN } },
102 { KE_KEY, 0x20e6, { KEY_PROG1 } },
103 { KE_KEY, 0x20e8, { KEY_MEDIA } },
104 { KE_KEY, 0x2142, { KEY_MEDIA } },
105 { KE_KEY, 0x213b, { KEY_INFO } },
106 { KE_KEY, 0x2169, { KEY_DIRECTION } },
107 { KE_KEY, 0x231b, { KEY_HELP } },
111 static struct input_dev *hp_wmi_input_dev;
112 static struct platform_device *hp_wmi_platform_dev;
114 static struct rfkill *wifi_rfkill;
115 static struct rfkill *bluetooth_rfkill;
116 static struct rfkill *wwan_rfkill;
118 static const struct dev_pm_ops hp_wmi_pm_ops = {
119 .resume = hp_wmi_resume_handler,
120 .restore = hp_wmi_resume_handler,
123 static struct platform_driver hp_wmi_driver = {
126 .owner = THIS_MODULE,
127 .pm = &hp_wmi_pm_ops,
129 .probe = hp_wmi_bios_setup,
130 .remove = hp_wmi_bios_remove,
134 * hp_wmi_perform_query
136 * query: The commandtype -> What should be queried
137 * write: The command -> 0 read, 1 write, 3 ODM specific
138 * buffer: Buffer used as input and/or output
139 * buffersize: Size of buffer
141 * returns zero on success
142 * an HP WMI query specific error code (which is positive)
143 * -EINVAL if the query was not successful at all
144 * -EINVAL if the output buffer size exceeds buffersize
146 * Note: The buffersize must at least be the maximum of the input and output
147 * size. E.g. Battery info query (0x7) is defined to have 1 byte input
148 * and 128 byte output. The caller would do:
149 * buffer = kzalloc(128, GFP_KERNEL);
150 * ret = hp_wmi_perform_query(0x7, 0, buffer, 128)
152 static int hp_wmi_perform_query(int query, int write, u32 *buffer,
155 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 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 if (bios_return.return_code != HPWMI_RET_UNKNOWN_CMDTYPE)
182 printk(KERN_WARNING PREFIX "query 0x%x returned "
184 query, bios_return.return_code);
186 return bios_return.return_code;
189 memcpy(buffer, &bios_return.value, sizeof(bios_return.value));
195 static int hp_wmi_display_state(void)
198 int ret = hp_wmi_perform_query(HPWMI_DISPLAY_QUERY, 0, &state,
205 static int hp_wmi_hddtemp_state(void)
208 int ret = hp_wmi_perform_query(HPWMI_HDDTEMP_QUERY, 0, &state,
215 static int hp_wmi_als_state(void)
218 int ret = hp_wmi_perform_query(HPWMI_ALS_QUERY, 0, &state,
225 static int hp_wmi_dock_state(void)
228 int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, 0, &state,
237 static int hp_wmi_tablet_state(void)
240 int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, 0, &state,
245 return (state & 0x4) ? 1 : 0;
248 static int hp_wmi_set_block(void *data, bool blocked)
250 enum hp_wmi_radio r = (enum hp_wmi_radio) data;
251 int query = BIT(r + 8) | ((!blocked) << r);
254 ret = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1,
255 &query, sizeof(query));
261 static const struct rfkill_ops hp_wmi_rfkill_ops = {
262 .set_block = hp_wmi_set_block,
265 static bool hp_wmi_get_sw_state(enum hp_wmi_radio r)
269 hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0,
270 &wireless, sizeof(wireless));
271 /* TBD: Pass error */
273 mask = 0x200 << (r * 8);
281 static bool hp_wmi_get_hw_state(enum hp_wmi_radio r)
285 hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0,
286 &wireless, sizeof(wireless));
287 /* TBD: Pass error */
289 mask = 0x800 << (r * 8);
297 static ssize_t show_display(struct device *dev, struct device_attribute *attr,
300 int value = hp_wmi_display_state();
303 return sprintf(buf, "%d\n", value);
306 static ssize_t show_hddtemp(struct device *dev, struct device_attribute *attr,
309 int value = hp_wmi_hddtemp_state();
312 return sprintf(buf, "%d\n", value);
315 static ssize_t show_als(struct device *dev, struct device_attribute *attr,
318 int value = hp_wmi_als_state();
321 return sprintf(buf, "%d\n", value);
324 static ssize_t show_dock(struct device *dev, struct device_attribute *attr,
327 int value = hp_wmi_dock_state();
330 return sprintf(buf, "%d\n", value);
333 static ssize_t show_tablet(struct device *dev, struct device_attribute *attr,
336 int value = hp_wmi_tablet_state();
339 return sprintf(buf, "%d\n", value);
342 static ssize_t set_als(struct device *dev, struct device_attribute *attr,
343 const char *buf, size_t count)
345 u32 tmp = simple_strtoul(buf, NULL, 10);
346 int ret = hp_wmi_perform_query(HPWMI_ALS_QUERY, 1, &tmp,
354 static DEVICE_ATTR(display, S_IRUGO, show_display, NULL);
355 static DEVICE_ATTR(hddtemp, S_IRUGO, show_hddtemp, NULL);
356 static DEVICE_ATTR(als, S_IRUGO | S_IWUSR, show_als, set_als);
357 static DEVICE_ATTR(dock, S_IRUGO, show_dock, NULL);
358 static DEVICE_ATTR(tablet, S_IRUGO, show_tablet, NULL);
360 static void hp_wmi_notify(u32 value, void *context)
362 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
363 union acpi_object *obj;
364 u32 event_id, event_data;
365 int key_code = 0, ret;
369 status = wmi_get_event_data(value, &response);
370 if (status != AE_OK) {
371 printk(KERN_INFO PREFIX "bad event status 0x%x\n", status);
375 obj = (union acpi_object *)response.pointer;
379 if (obj->type != ACPI_TYPE_BUFFER) {
380 printk(KERN_INFO "hp-wmi: Unknown response received %d\n",
387 * Depending on ACPI version the concatenation of id and event data
388 * inside _WED function will result in a 8 or 16 byte buffer.
390 location = (u32 *)obj->buffer.pointer;
391 if (obj->buffer.length == 8) {
392 event_id = *location;
393 event_data = *(location + 1);
394 } else if (obj->buffer.length == 16) {
395 event_id = *location;
396 event_data = *(location + 2);
398 printk(KERN_INFO "hp-wmi: Unknown buffer length %d\n",
406 case HPWMI_DOCK_EVENT:
407 input_report_switch(hp_wmi_input_dev, SW_DOCK,
408 hp_wmi_dock_state());
409 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
410 hp_wmi_tablet_state());
411 input_sync(hp_wmi_input_dev);
415 case HPWMI_SMART_ADAPTER:
417 case HPWMI_BEZEL_BUTTON:
418 ret = hp_wmi_perform_query(HPWMI_HOTKEY_QUERY, 0,
424 if (!sparse_keymap_report_event(hp_wmi_input_dev,
426 printk(KERN_INFO PREFIX "Unknown key code - 0x%x\n",
431 rfkill_set_states(wifi_rfkill,
432 hp_wmi_get_sw_state(HPWMI_WIFI),
433 hp_wmi_get_hw_state(HPWMI_WIFI));
434 if (bluetooth_rfkill)
435 rfkill_set_states(bluetooth_rfkill,
436 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
437 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
439 rfkill_set_states(wwan_rfkill,
440 hp_wmi_get_sw_state(HPWMI_WWAN),
441 hp_wmi_get_hw_state(HPWMI_WWAN));
443 case HPWMI_CPU_BATTERY_THROTTLE:
444 printk(KERN_INFO PREFIX UNIMP "CPU throttle because of 3 Cell"
445 " battery event detected\n");
447 case HPWMI_LOCK_SWITCH:
450 printk(KERN_INFO PREFIX "Unknown event_id - %d - 0x%x\n",
451 event_id, event_data);
456 static int __init hp_wmi_input_setup(void)
461 hp_wmi_input_dev = input_allocate_device();
462 if (!hp_wmi_input_dev)
465 hp_wmi_input_dev->name = "HP WMI hotkeys";
466 hp_wmi_input_dev->phys = "wmi/input0";
467 hp_wmi_input_dev->id.bustype = BUS_HOST;
469 __set_bit(EV_SW, hp_wmi_input_dev->evbit);
470 __set_bit(SW_DOCK, hp_wmi_input_dev->swbit);
471 __set_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit);
473 err = sparse_keymap_setup(hp_wmi_input_dev, hp_wmi_keymap, NULL);
477 /* Set initial hardware state */
478 input_report_switch(hp_wmi_input_dev, SW_DOCK, hp_wmi_dock_state());
479 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
480 hp_wmi_tablet_state());
481 input_sync(hp_wmi_input_dev);
483 status = wmi_install_notify_handler(HPWMI_EVENT_GUID, hp_wmi_notify, NULL);
484 if (ACPI_FAILURE(status)) {
486 goto err_free_keymap;
489 err = input_register_device(hp_wmi_input_dev);
491 goto err_uninstall_notifier;
495 err_uninstall_notifier:
496 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
498 sparse_keymap_free(hp_wmi_input_dev);
500 input_free_device(hp_wmi_input_dev);
504 static void hp_wmi_input_destroy(void)
506 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
507 sparse_keymap_free(hp_wmi_input_dev);
508 input_unregister_device(hp_wmi_input_dev);
511 static void cleanup_sysfs(struct platform_device *device)
513 device_remove_file(&device->dev, &dev_attr_display);
514 device_remove_file(&device->dev, &dev_attr_hddtemp);
515 device_remove_file(&device->dev, &dev_attr_als);
516 device_remove_file(&device->dev, &dev_attr_dock);
517 device_remove_file(&device->dev, &dev_attr_tablet);
520 static int __devinit hp_wmi_bios_setup(struct platform_device *device)
525 err = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, &wireless,
530 err = device_create_file(&device->dev, &dev_attr_display);
532 goto add_sysfs_error;
533 err = device_create_file(&device->dev, &dev_attr_hddtemp);
535 goto add_sysfs_error;
536 err = device_create_file(&device->dev, &dev_attr_als);
538 goto add_sysfs_error;
539 err = device_create_file(&device->dev, &dev_attr_dock);
541 goto add_sysfs_error;
542 err = device_create_file(&device->dev, &dev_attr_tablet);
544 goto add_sysfs_error;
546 if (wireless & 0x1) {
547 wifi_rfkill = rfkill_alloc("hp-wifi", &device->dev,
550 (void *) HPWMI_WIFI);
551 rfkill_init_sw_state(wifi_rfkill,
552 hp_wmi_get_sw_state(HPWMI_WIFI));
553 rfkill_set_hw_state(wifi_rfkill,
554 hp_wmi_get_hw_state(HPWMI_WIFI));
555 err = rfkill_register(wifi_rfkill);
557 goto register_wifi_error;
560 if (wireless & 0x2) {
561 bluetooth_rfkill = rfkill_alloc("hp-bluetooth", &device->dev,
562 RFKILL_TYPE_BLUETOOTH,
564 (void *) HPWMI_BLUETOOTH);
565 rfkill_init_sw_state(bluetooth_rfkill,
566 hp_wmi_get_sw_state(HPWMI_BLUETOOTH));
567 rfkill_set_hw_state(bluetooth_rfkill,
568 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
569 err = rfkill_register(bluetooth_rfkill);
571 goto register_bluetooth_error;
574 if (wireless & 0x4) {
575 wwan_rfkill = rfkill_alloc("hp-wwan", &device->dev,
578 (void *) HPWMI_WWAN);
579 rfkill_init_sw_state(wwan_rfkill,
580 hp_wmi_get_sw_state(HPWMI_WWAN));
581 rfkill_set_hw_state(wwan_rfkill,
582 hp_wmi_get_hw_state(HPWMI_WWAN));
583 err = rfkill_register(wwan_rfkill);
585 goto register_wwan_err;
590 rfkill_destroy(wwan_rfkill);
591 if (bluetooth_rfkill)
592 rfkill_unregister(bluetooth_rfkill);
593 register_bluetooth_error:
594 rfkill_destroy(bluetooth_rfkill);
596 rfkill_unregister(wifi_rfkill);
598 rfkill_destroy(wifi_rfkill);
600 cleanup_sysfs(device);
604 static int __exit hp_wmi_bios_remove(struct platform_device *device)
606 cleanup_sysfs(device);
609 rfkill_unregister(wifi_rfkill);
610 rfkill_destroy(wifi_rfkill);
612 if (bluetooth_rfkill) {
613 rfkill_unregister(bluetooth_rfkill);
614 rfkill_destroy(bluetooth_rfkill);
617 rfkill_unregister(wwan_rfkill);
618 rfkill_destroy(wwan_rfkill);
624 static int hp_wmi_resume_handler(struct device *device)
627 * Hardware state may have changed while suspended, so trigger
628 * input events for the current state. As this is a switch,
629 * the input layer will only actually pass it on if the state
632 if (hp_wmi_input_dev) {
633 input_report_switch(hp_wmi_input_dev, SW_DOCK,
634 hp_wmi_dock_state());
635 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
636 hp_wmi_tablet_state());
637 input_sync(hp_wmi_input_dev);
641 rfkill_set_states(wifi_rfkill,
642 hp_wmi_get_sw_state(HPWMI_WIFI),
643 hp_wmi_get_hw_state(HPWMI_WIFI));
644 if (bluetooth_rfkill)
645 rfkill_set_states(bluetooth_rfkill,
646 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
647 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
649 rfkill_set_states(wwan_rfkill,
650 hp_wmi_get_sw_state(HPWMI_WWAN),
651 hp_wmi_get_hw_state(HPWMI_WWAN));
656 static int __init hp_wmi_init(void)
659 int event_capable = wmi_has_guid(HPWMI_EVENT_GUID);
660 int bios_capable = wmi_has_guid(HPWMI_BIOS_GUID);
663 err = hp_wmi_input_setup();
669 err = platform_driver_register(&hp_wmi_driver);
672 hp_wmi_platform_dev = platform_device_alloc("hp-wmi", -1);
673 if (!hp_wmi_platform_dev) {
675 goto err_device_alloc;
677 err = platform_device_add(hp_wmi_platform_dev);
682 if (!bios_capable && !event_capable)
688 platform_device_put(hp_wmi_platform_dev);
690 platform_driver_unregister(&hp_wmi_driver);
693 hp_wmi_input_destroy();
698 static void __exit hp_wmi_exit(void)
700 if (wmi_has_guid(HPWMI_EVENT_GUID))
701 hp_wmi_input_destroy();
703 if (hp_wmi_platform_dev) {
704 platform_device_unregister(hp_wmi_platform_dev);
705 platform_driver_unregister(&hp_wmi_driver);
709 module_init(hp_wmi_init);
710 module_exit(hp_wmi_exit);