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 static const struct key_entry hp_wmi_keymap[] = {
93 { KE_KEY, 0x02, { KEY_BRIGHTNESSUP } },
94 { KE_KEY, 0x03, { KEY_BRIGHTNESSDOWN } },
95 { KE_KEY, 0x20e6, { KEY_PROG1 } },
96 { KE_KEY, 0x20e8, { KEY_MEDIA } },
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,
127 * hp_wmi_perform_query
129 * query: The commandtype -> What should be queried
130 * write: The command -> 0 read, 1 write, 3 ODM specific
131 * buffer: Buffer used as input and/or output
132 * buffersize: Size of buffer
134 * returns zero on success
135 * an HP WMI query specific error code (which is positive)
136 * -EINVAL if the query was not successful at all
137 * -EINVAL if the output buffer size exceeds buffersize
139 * Note: The buffersize must at least be the maximum of the input and output
140 * size. E.g. Battery info query (0x7) is defined to have 1 byte input
141 * and 128 byte output. The caller would do:
142 * buffer = kzalloc(128, GFP_KERNEL);
143 * ret = hp_wmi_perform_query(0x7, 0, buffer, 128)
145 static int hp_wmi_perform_query(int query, int write, u32 *buffer,
148 struct bios_return bios_return;
150 union acpi_object *obj;
151 struct bios_args args = {
152 .signature = 0x55434553,
153 .command = write ? 0x2 : 0x1,
154 .commandtype = query,
155 .datasize = buffersize,
158 struct acpi_buffer input = { sizeof(struct bios_args), &args };
159 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
161 status = wmi_evaluate_method(HPWMI_BIOS_GUID, 0, 0x3, &input, &output);
163 obj = output.pointer;
167 else if (obj->type != ACPI_TYPE_BUFFER) {
172 bios_return = *((struct bios_return *)obj->buffer.pointer);
174 memcpy(buffer, &bios_return.value, sizeof(bios_return.value));
180 static int hp_wmi_display_state(void)
183 int ret = hp_wmi_perform_query(HPWMI_DISPLAY_QUERY, 0, &state,
190 static int hp_wmi_hddtemp_state(void)
193 int ret = hp_wmi_perform_query(HPWMI_HDDTEMP_QUERY, 0, &state,
200 static int hp_wmi_als_state(void)
203 int ret = hp_wmi_perform_query(HPWMI_ALS_QUERY, 0, &state,
210 static int hp_wmi_dock_state(void)
213 int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, 0, &state,
222 static int hp_wmi_tablet_state(void)
225 int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, 0, &state,
230 return (state & 0x4) ? 1 : 0;
233 static int hp_wmi_set_block(void *data, bool blocked)
235 enum hp_wmi_radio r = (enum hp_wmi_radio) data;
236 int query = BIT(r + 8) | ((!blocked) << r);
239 ret = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1,
240 &query, sizeof(query));
246 static const struct rfkill_ops hp_wmi_rfkill_ops = {
247 .set_block = hp_wmi_set_block,
250 static bool hp_wmi_get_sw_state(enum hp_wmi_radio r)
254 hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0,
255 &wireless, sizeof(wireless));
256 /* TBD: Pass error */
258 mask = 0x200 << (r * 8);
266 static bool hp_wmi_get_hw_state(enum hp_wmi_radio r)
270 hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0,
271 &wireless, sizeof(wireless));
272 /* TBD: Pass error */
274 mask = 0x800 << (r * 8);
282 static ssize_t show_display(struct device *dev, struct device_attribute *attr,
285 int value = hp_wmi_display_state();
288 return sprintf(buf, "%d\n", value);
291 static ssize_t show_hddtemp(struct device *dev, struct device_attribute *attr,
294 int value = hp_wmi_hddtemp_state();
297 return sprintf(buf, "%d\n", value);
300 static ssize_t show_als(struct device *dev, struct device_attribute *attr,
303 int value = hp_wmi_als_state();
306 return sprintf(buf, "%d\n", value);
309 static ssize_t show_dock(struct device *dev, struct device_attribute *attr,
312 int value = hp_wmi_dock_state();
315 return sprintf(buf, "%d\n", value);
318 static ssize_t show_tablet(struct device *dev, struct device_attribute *attr,
321 int value = hp_wmi_tablet_state();
324 return sprintf(buf, "%d\n", value);
327 static ssize_t set_als(struct device *dev, struct device_attribute *attr,
328 const char *buf, size_t count)
330 u32 tmp = simple_strtoul(buf, NULL, 10);
331 int ret = hp_wmi_perform_query(HPWMI_ALS_QUERY, 1, &tmp,
339 static DEVICE_ATTR(display, S_IRUGO, show_display, NULL);
340 static DEVICE_ATTR(hddtemp, S_IRUGO, show_hddtemp, NULL);
341 static DEVICE_ATTR(als, S_IRUGO | S_IWUSR, show_als, set_als);
342 static DEVICE_ATTR(dock, S_IRUGO, show_dock, NULL);
343 static DEVICE_ATTR(tablet, S_IRUGO, show_tablet, NULL);
345 static void hp_wmi_notify(u32 value, void *context)
347 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
348 union acpi_object *obj;
349 u32 event_id, event_data;
350 int key_code = 0, ret;
354 status = wmi_get_event_data(value, &response);
355 if (status != AE_OK) {
356 printk(KERN_INFO PREFIX "bad event status 0x%x\n", status);
360 obj = (union acpi_object *)response.pointer;
364 if (obj->type != ACPI_TYPE_BUFFER) {
365 printk(KERN_INFO "hp-wmi: Unknown response received %d\n",
372 * Depending on ACPI version the concatenation of id and event data
373 * inside _WED function will result in a 8 or 16 byte buffer.
375 location = (u32 *)obj->buffer.pointer;
376 if (obj->buffer.length == 8) {
377 event_id = *location;
378 event_data = *(location + 1);
379 } else if (obj->buffer.length == 16) {
380 event_id = *location;
381 event_data = *(location + 2);
383 printk(KERN_INFO "hp-wmi: Unknown buffer length %d\n",
391 case HPWMI_DOCK_EVENT:
392 input_report_switch(hp_wmi_input_dev, SW_DOCK,
393 hp_wmi_dock_state());
394 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
395 hp_wmi_tablet_state());
396 input_sync(hp_wmi_input_dev);
400 case HPWMI_SMART_ADAPTER:
402 case HPWMI_BEZEL_BUTTON:
403 ret = hp_wmi_perform_query(HPWMI_HOTKEY_QUERY, 0,
409 if (!sparse_keymap_report_event(hp_wmi_input_dev,
411 printk(KERN_INFO PREFIX "Unknown key code - 0x%x\n",
416 rfkill_set_states(wifi_rfkill,
417 hp_wmi_get_sw_state(HPWMI_WIFI),
418 hp_wmi_get_hw_state(HPWMI_WIFI));
419 if (bluetooth_rfkill)
420 rfkill_set_states(bluetooth_rfkill,
421 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
422 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
424 rfkill_set_states(wwan_rfkill,
425 hp_wmi_get_sw_state(HPWMI_WWAN),
426 hp_wmi_get_hw_state(HPWMI_WWAN));
428 case HPWMI_CPU_BATTERY_THROTTLE:
429 printk(KERN_INFO PREFIX UNIMP "CPU throttle because of 3 Cell"
430 " battery event detected\n");
432 case HPWMI_LOCK_SWITCH:
435 printk(KERN_INFO PREFIX "Unknown event_id - %d - 0x%x\n",
436 event_id, event_data);
441 static int __init hp_wmi_input_setup(void)
446 hp_wmi_input_dev = input_allocate_device();
447 if (!hp_wmi_input_dev)
450 hp_wmi_input_dev->name = "HP WMI hotkeys";
451 hp_wmi_input_dev->phys = "wmi/input0";
452 hp_wmi_input_dev->id.bustype = BUS_HOST;
454 __set_bit(EV_SW, hp_wmi_input_dev->evbit);
455 __set_bit(SW_DOCK, hp_wmi_input_dev->swbit);
456 __set_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit);
458 err = sparse_keymap_setup(hp_wmi_input_dev, hp_wmi_keymap, NULL);
462 /* Set initial hardware state */
463 input_report_switch(hp_wmi_input_dev, SW_DOCK, hp_wmi_dock_state());
464 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
465 hp_wmi_tablet_state());
466 input_sync(hp_wmi_input_dev);
468 status = wmi_install_notify_handler(HPWMI_EVENT_GUID, hp_wmi_notify, NULL);
469 if (ACPI_FAILURE(status)) {
471 goto err_free_keymap;
474 err = input_register_device(hp_wmi_input_dev);
476 goto err_uninstall_notifier;
480 err_uninstall_notifier:
481 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
483 sparse_keymap_free(hp_wmi_input_dev);
485 input_free_device(hp_wmi_input_dev);
489 static void hp_wmi_input_destroy(void)
491 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
492 sparse_keymap_free(hp_wmi_input_dev);
493 input_unregister_device(hp_wmi_input_dev);
496 static void cleanup_sysfs(struct platform_device *device)
498 device_remove_file(&device->dev, &dev_attr_display);
499 device_remove_file(&device->dev, &dev_attr_hddtemp);
500 device_remove_file(&device->dev, &dev_attr_als);
501 device_remove_file(&device->dev, &dev_attr_dock);
502 device_remove_file(&device->dev, &dev_attr_tablet);
505 static int __devinit hp_wmi_bios_setup(struct platform_device *device)
510 err = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, &wireless,
515 err = device_create_file(&device->dev, &dev_attr_display);
517 goto add_sysfs_error;
518 err = device_create_file(&device->dev, &dev_attr_hddtemp);
520 goto add_sysfs_error;
521 err = device_create_file(&device->dev, &dev_attr_als);
523 goto add_sysfs_error;
524 err = device_create_file(&device->dev, &dev_attr_dock);
526 goto add_sysfs_error;
527 err = device_create_file(&device->dev, &dev_attr_tablet);
529 goto add_sysfs_error;
531 if (wireless & 0x1) {
532 wifi_rfkill = rfkill_alloc("hp-wifi", &device->dev,
535 (void *) HPWMI_WIFI);
536 rfkill_init_sw_state(wifi_rfkill,
537 hp_wmi_get_sw_state(HPWMI_WIFI));
538 rfkill_set_hw_state(wifi_rfkill,
539 hp_wmi_get_hw_state(HPWMI_WIFI));
540 err = rfkill_register(wifi_rfkill);
542 goto register_wifi_error;
545 if (wireless & 0x2) {
546 bluetooth_rfkill = rfkill_alloc("hp-bluetooth", &device->dev,
547 RFKILL_TYPE_BLUETOOTH,
549 (void *) HPWMI_BLUETOOTH);
550 rfkill_init_sw_state(bluetooth_rfkill,
551 hp_wmi_get_sw_state(HPWMI_BLUETOOTH));
552 rfkill_set_hw_state(bluetooth_rfkill,
553 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
554 err = rfkill_register(bluetooth_rfkill);
556 goto register_bluetooth_error;
559 if (wireless & 0x4) {
560 wwan_rfkill = rfkill_alloc("hp-wwan", &device->dev,
563 (void *) HPWMI_WWAN);
564 rfkill_init_sw_state(wwan_rfkill,
565 hp_wmi_get_sw_state(HPWMI_WWAN));
566 rfkill_set_hw_state(wwan_rfkill,
567 hp_wmi_get_hw_state(HPWMI_WWAN));
568 err = rfkill_register(wwan_rfkill);
570 goto register_wwan_err;
575 rfkill_destroy(wwan_rfkill);
576 if (bluetooth_rfkill)
577 rfkill_unregister(bluetooth_rfkill);
578 register_bluetooth_error:
579 rfkill_destroy(bluetooth_rfkill);
581 rfkill_unregister(wifi_rfkill);
583 rfkill_destroy(wifi_rfkill);
585 cleanup_sysfs(device);
589 static int __exit hp_wmi_bios_remove(struct platform_device *device)
591 cleanup_sysfs(device);
594 rfkill_unregister(wifi_rfkill);
595 rfkill_destroy(wifi_rfkill);
597 if (bluetooth_rfkill) {
598 rfkill_unregister(bluetooth_rfkill);
599 rfkill_destroy(bluetooth_rfkill);
602 rfkill_unregister(wwan_rfkill);
603 rfkill_destroy(wwan_rfkill);
609 static int hp_wmi_resume_handler(struct device *device)
612 * Hardware state may have changed while suspended, so trigger
613 * input events for the current state. As this is a switch,
614 * the input layer will only actually pass it on if the state
617 if (hp_wmi_input_dev) {
618 input_report_switch(hp_wmi_input_dev, SW_DOCK,
619 hp_wmi_dock_state());
620 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
621 hp_wmi_tablet_state());
622 input_sync(hp_wmi_input_dev);
626 rfkill_set_states(wifi_rfkill,
627 hp_wmi_get_sw_state(HPWMI_WIFI),
628 hp_wmi_get_hw_state(HPWMI_WIFI));
629 if (bluetooth_rfkill)
630 rfkill_set_states(bluetooth_rfkill,
631 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
632 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
634 rfkill_set_states(wwan_rfkill,
635 hp_wmi_get_sw_state(HPWMI_WWAN),
636 hp_wmi_get_hw_state(HPWMI_WWAN));
641 static int __init hp_wmi_init(void)
644 int event_capable = wmi_has_guid(HPWMI_EVENT_GUID);
645 int bios_capable = wmi_has_guid(HPWMI_BIOS_GUID);
648 err = hp_wmi_input_setup();
654 err = platform_driver_register(&hp_wmi_driver);
657 hp_wmi_platform_dev = platform_device_alloc("hp-wmi", -1);
658 if (!hp_wmi_platform_dev) {
660 goto err_device_alloc;
662 err = platform_device_add(hp_wmi_platform_dev);
667 if (!bios_capable && !event_capable)
673 platform_device_put(hp_wmi_platform_dev);
675 platform_driver_unregister(&hp_wmi_driver);
678 hp_wmi_input_destroy();
683 static void __exit hp_wmi_exit(void)
685 if (wmi_has_guid(HPWMI_EVENT_GUID))
686 hp_wmi_input_destroy();
688 if (hp_wmi_platform_dev) {
689 platform_device_unregister(hp_wmi_platform_dev);
690 platform_driver_unregister(&hp_wmi_driver);
694 module_init(hp_wmi_init);
695 module_exit(hp_wmi_exit);