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
55 #define PREFIX "HP WMI: "
63 enum hp_wmi_event_ids
{
65 HPWMI_BEZEL_BUTTON
= 4,
69 static int __devinit
hp_wmi_bios_setup(struct platform_device
*device
);
70 static int __exit
hp_wmi_bios_remove(struct platform_device
*device
);
71 static int hp_wmi_resume_handler(struct device
*device
);
88 char type
; /* See KE_* below */
93 enum { KE_KEY
, KE_END
};
95 static struct key_entry hp_wmi_keymap
[] = {
96 {KE_KEY
, 0x02, KEY_BRIGHTNESSUP
},
97 {KE_KEY
, 0x03, KEY_BRIGHTNESSDOWN
},
98 {KE_KEY
, 0x20e6, KEY_PROG1
},
99 {KE_KEY
, 0x20e8, KEY_MEDIA
},
100 {KE_KEY
, 0x2142, KEY_MEDIA
},
101 {KE_KEY
, 0x213b, KEY_INFO
},
102 {KE_KEY
, 0x2169, KEY_DIRECTION
},
103 {KE_KEY
, 0x231b, KEY_HELP
},
107 static struct input_dev
*hp_wmi_input_dev
;
108 static struct platform_device
*hp_wmi_platform_dev
;
110 static struct rfkill
*wifi_rfkill
;
111 static struct rfkill
*bluetooth_rfkill
;
112 static struct rfkill
*wwan_rfkill
;
114 static const struct dev_pm_ops hp_wmi_pm_ops
= {
115 .resume
= hp_wmi_resume_handler
,
116 .restore
= hp_wmi_resume_handler
,
119 static struct platform_driver hp_wmi_driver
= {
122 .owner
= THIS_MODULE
,
123 .pm
= &hp_wmi_pm_ops
,
125 .probe
= hp_wmi_bios_setup
,
126 .remove
= hp_wmi_bios_remove
,
129 static int hp_wmi_perform_query(int query
, int write
, int value
)
131 struct bios_return bios_return
;
133 union acpi_object
*obj
;
134 struct bios_args args
= {
135 .signature
= 0x55434553,
136 .command
= write
? 0x2 : 0x1,
137 .commandtype
= query
,
138 .datasize
= write
? 0x4 : 0,
141 struct acpi_buffer input
= { sizeof(struct bios_args
), &args
};
142 struct acpi_buffer output
= { ACPI_ALLOCATE_BUFFER
, NULL
};
144 status
= wmi_evaluate_method(HPWMI_BIOS_GUID
, 0, 0x3, &input
, &output
);
146 obj
= output
.pointer
;
150 else if (obj
->type
!= ACPI_TYPE_BUFFER
) {
155 bios_return
= *((struct bios_return
*)obj
->buffer
.pointer
);
157 if (bios_return
.return_code
> 0)
158 return bios_return
.return_code
* -1;
160 return bios_return
.value
;
163 static int hp_wmi_display_state(void)
165 return hp_wmi_perform_query(HPWMI_DISPLAY_QUERY
, 0, 0);
168 static int hp_wmi_hddtemp_state(void)
170 return hp_wmi_perform_query(HPWMI_HDDTEMP_QUERY
, 0, 0);
173 static int hp_wmi_als_state(void)
175 return hp_wmi_perform_query(HPWMI_ALS_QUERY
, 0, 0);
178 static int hp_wmi_dock_state(void)
180 int ret
= hp_wmi_perform_query(HPWMI_HARDWARE_QUERY
, 0, 0);
188 static int hp_wmi_tablet_state(void)
190 int ret
= hp_wmi_perform_query(HPWMI_HARDWARE_QUERY
, 0, 0);
195 return (ret
& 0x4) ? 1 : 0;
198 static int hp_wmi_set_block(void *data
, bool blocked
)
200 enum hp_wmi_radio r
= (enum hp_wmi_radio
) data
;
201 int query
= BIT(r
+ 8) | ((!blocked
) << r
);
203 return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 1, query
);
206 static const struct rfkill_ops hp_wmi_rfkill_ops
= {
207 .set_block
= hp_wmi_set_block
,
210 static bool hp_wmi_get_sw_state(enum hp_wmi_radio r
)
212 int wireless
= hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 0, 0);
213 int mask
= 0x200 << (r
* 8);
221 static bool hp_wmi_get_hw_state(enum hp_wmi_radio r
)
223 int wireless
= hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 0, 0);
224 int mask
= 0x800 << (r
* 8);
232 static ssize_t
show_display(struct device
*dev
, struct device_attribute
*attr
,
235 int value
= hp_wmi_display_state();
238 return sprintf(buf
, "%d\n", value
);
241 static ssize_t
show_hddtemp(struct device
*dev
, struct device_attribute
*attr
,
244 int value
= hp_wmi_hddtemp_state();
247 return sprintf(buf
, "%d\n", value
);
250 static ssize_t
show_als(struct device
*dev
, struct device_attribute
*attr
,
253 int value
= hp_wmi_als_state();
256 return sprintf(buf
, "%d\n", value
);
259 static ssize_t
show_dock(struct device
*dev
, struct device_attribute
*attr
,
262 int value
= hp_wmi_dock_state();
265 return sprintf(buf
, "%d\n", value
);
268 static ssize_t
show_tablet(struct device
*dev
, struct device_attribute
*attr
,
271 int value
= hp_wmi_tablet_state();
274 return sprintf(buf
, "%d\n", value
);
277 static ssize_t
set_als(struct device
*dev
, struct device_attribute
*attr
,
278 const char *buf
, size_t count
)
280 u32 tmp
= simple_strtoul(buf
, NULL
, 10);
281 hp_wmi_perform_query(HPWMI_ALS_QUERY
, 1, tmp
);
285 static DEVICE_ATTR(display
, S_IRUGO
, show_display
, NULL
);
286 static DEVICE_ATTR(hddtemp
, S_IRUGO
, show_hddtemp
, NULL
);
287 static DEVICE_ATTR(als
, S_IRUGO
| S_IWUSR
, show_als
, set_als
);
288 static DEVICE_ATTR(dock
, S_IRUGO
, show_dock
, NULL
);
289 static DEVICE_ATTR(tablet
, S_IRUGO
, show_tablet
, NULL
);
291 static struct key_entry
*hp_wmi_get_entry_by_scancode(unsigned int code
)
293 struct key_entry
*key
;
295 for (key
= hp_wmi_keymap
; key
->type
!= KE_END
; key
++)
296 if (code
== key
->code
)
302 static struct key_entry
*hp_wmi_get_entry_by_keycode(unsigned int keycode
)
304 struct key_entry
*key
;
306 for (key
= hp_wmi_keymap
; key
->type
!= KE_END
; key
++)
307 if (key
->type
== KE_KEY
&& keycode
== key
->keycode
)
313 static int hp_wmi_getkeycode(struct input_dev
*dev
,
314 unsigned int scancode
, unsigned int *keycode
)
316 struct key_entry
*key
= hp_wmi_get_entry_by_scancode(scancode
);
318 if (key
&& key
->type
== KE_KEY
) {
319 *keycode
= key
->keycode
;
326 static int hp_wmi_setkeycode(struct input_dev
*dev
,
327 unsigned int scancode
, unsigned int keycode
)
329 struct key_entry
*key
;
330 unsigned int old_keycode
;
332 key
= hp_wmi_get_entry_by_scancode(scancode
);
333 if (key
&& key
->type
== KE_KEY
) {
334 old_keycode
= key
->keycode
;
335 key
->keycode
= keycode
;
336 set_bit(keycode
, dev
->keybit
);
337 if (!hp_wmi_get_entry_by_keycode(old_keycode
))
338 clear_bit(old_keycode
, dev
->keybit
);
345 static void hp_wmi_notify(u32 value
, void *context
)
347 struct acpi_buffer response
= { ACPI_ALLOCATE_BUFFER
, NULL
};
348 static struct key_entry
*key
;
349 union acpi_object
*obj
;
350 int eventcode
, key_code
;
353 status
= wmi_get_event_data(value
, &response
);
354 if (status
!= AE_OK
) {
355 printk(KERN_INFO PREFIX
"bad event status 0x%x\n", status
);
359 obj
= (union acpi_object
*)response
.pointer
;
361 if (!obj
|| obj
->type
!= ACPI_TYPE_BUFFER
|| obj
->buffer
.length
!= 8) {
362 printk(KERN_INFO PREFIX
"Unknown response received\n");
367 eventcode
= *((u8
*) obj
->buffer
.pointer
);
370 case HPWMI_DOCK_EVENT
:
371 input_report_switch(hp_wmi_input_dev
, SW_DOCK
,
372 hp_wmi_dock_state());
373 input_report_switch(hp_wmi_input_dev
, SW_TABLET_MODE
,
374 hp_wmi_tablet_state());
375 input_sync(hp_wmi_input_dev
);
377 case HPWMI_BEZEL_BUTTON
:
378 key_code
= hp_wmi_perform_query(HPWMI_HOTKEY_QUERY
, 0,
380 key
= hp_wmi_get_entry_by_scancode(key_code
);
384 input_report_key(hp_wmi_input_dev
,
386 input_sync(hp_wmi_input_dev
);
387 input_report_key(hp_wmi_input_dev
,
389 input_sync(hp_wmi_input_dev
);
393 printk(KERN_INFO PREFIX
"Unknown key code - 0x%x\n",
398 rfkill_set_states(wifi_rfkill
,
399 hp_wmi_get_sw_state(HPWMI_WIFI
),
400 hp_wmi_get_hw_state(HPWMI_WIFI
));
401 if (bluetooth_rfkill
)
402 rfkill_set_states(bluetooth_rfkill
,
403 hp_wmi_get_sw_state(HPWMI_BLUETOOTH
),
404 hp_wmi_get_hw_state(HPWMI_BLUETOOTH
));
406 rfkill_set_states(wwan_rfkill
,
407 hp_wmi_get_sw_state(HPWMI_WWAN
),
408 hp_wmi_get_hw_state(HPWMI_WWAN
));
411 printk(KERN_INFO PREFIX
"Unknown eventcode - %d\n",
417 static int __init
hp_wmi_input_setup(void)
419 struct key_entry
*key
;
422 hp_wmi_input_dev
= input_allocate_device();
424 hp_wmi_input_dev
->name
= "HP WMI hotkeys";
425 hp_wmi_input_dev
->phys
= "wmi/input0";
426 hp_wmi_input_dev
->id
.bustype
= BUS_HOST
;
427 hp_wmi_input_dev
->getkeycode
= hp_wmi_getkeycode
;
428 hp_wmi_input_dev
->setkeycode
= hp_wmi_setkeycode
;
430 for (key
= hp_wmi_keymap
; key
->type
!= KE_END
; key
++) {
433 set_bit(EV_KEY
, hp_wmi_input_dev
->evbit
);
434 set_bit(key
->keycode
, hp_wmi_input_dev
->keybit
);
439 set_bit(EV_SW
, hp_wmi_input_dev
->evbit
);
440 set_bit(SW_DOCK
, hp_wmi_input_dev
->swbit
);
441 set_bit(SW_TABLET_MODE
, hp_wmi_input_dev
->swbit
);
443 /* Set initial hardware state */
444 input_report_switch(hp_wmi_input_dev
, SW_DOCK
, hp_wmi_dock_state());
445 input_report_switch(hp_wmi_input_dev
, SW_TABLET_MODE
,
446 hp_wmi_tablet_state());
447 input_sync(hp_wmi_input_dev
);
449 err
= input_register_device(hp_wmi_input_dev
);
452 input_free_device(hp_wmi_input_dev
);
459 static void cleanup_sysfs(struct platform_device
*device
)
461 device_remove_file(&device
->dev
, &dev_attr_display
);
462 device_remove_file(&device
->dev
, &dev_attr_hddtemp
);
463 device_remove_file(&device
->dev
, &dev_attr_als
);
464 device_remove_file(&device
->dev
, &dev_attr_dock
);
465 device_remove_file(&device
->dev
, &dev_attr_tablet
);
468 static int __devinit
hp_wmi_bios_setup(struct platform_device
*device
)
471 int wireless
= hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 0, 0);
473 err
= device_create_file(&device
->dev
, &dev_attr_display
);
475 goto add_sysfs_error
;
476 err
= device_create_file(&device
->dev
, &dev_attr_hddtemp
);
478 goto add_sysfs_error
;
479 err
= device_create_file(&device
->dev
, &dev_attr_als
);
481 goto add_sysfs_error
;
482 err
= device_create_file(&device
->dev
, &dev_attr_dock
);
484 goto add_sysfs_error
;
485 err
= device_create_file(&device
->dev
, &dev_attr_tablet
);
487 goto add_sysfs_error
;
489 if (wireless
& 0x1) {
490 wifi_rfkill
= rfkill_alloc("hp-wifi", &device
->dev
,
493 (void *) HPWMI_WIFI
);
494 rfkill_init_sw_state(wifi_rfkill
,
495 hp_wmi_get_sw_state(HPWMI_WIFI
));
496 rfkill_set_hw_state(wifi_rfkill
,
497 hp_wmi_get_hw_state(HPWMI_WIFI
));
498 err
= rfkill_register(wifi_rfkill
);
500 goto register_wifi_error
;
503 if (wireless
& 0x2) {
504 bluetooth_rfkill
= rfkill_alloc("hp-bluetooth", &device
->dev
,
505 RFKILL_TYPE_BLUETOOTH
,
507 (void *) HPWMI_BLUETOOTH
);
508 rfkill_init_sw_state(bluetooth_rfkill
,
509 hp_wmi_get_sw_state(HPWMI_BLUETOOTH
));
510 rfkill_set_hw_state(bluetooth_rfkill
,
511 hp_wmi_get_hw_state(HPWMI_BLUETOOTH
));
512 err
= rfkill_register(bluetooth_rfkill
);
514 goto register_bluetooth_error
;
517 if (wireless
& 0x4) {
518 wwan_rfkill
= rfkill_alloc("hp-wwan", &device
->dev
,
521 (void *) HPWMI_WWAN
);
522 rfkill_init_sw_state(wwan_rfkill
,
523 hp_wmi_get_sw_state(HPWMI_WWAN
));
524 rfkill_set_hw_state(wwan_rfkill
,
525 hp_wmi_get_hw_state(HPWMI_WWAN
));
526 err
= rfkill_register(wwan_rfkill
);
528 goto register_wwan_err
;
533 rfkill_destroy(wwan_rfkill
);
534 if (bluetooth_rfkill
)
535 rfkill_unregister(bluetooth_rfkill
);
536 register_bluetooth_error
:
537 rfkill_destroy(bluetooth_rfkill
);
539 rfkill_unregister(wifi_rfkill
);
541 rfkill_destroy(wifi_rfkill
);
543 cleanup_sysfs(device
);
547 static int __exit
hp_wmi_bios_remove(struct platform_device
*device
)
549 cleanup_sysfs(device
);
552 rfkill_unregister(wifi_rfkill
);
553 rfkill_destroy(wifi_rfkill
);
555 if (bluetooth_rfkill
) {
556 rfkill_unregister(bluetooth_rfkill
);
557 rfkill_destroy(bluetooth_rfkill
);
560 rfkill_unregister(wwan_rfkill
);
561 rfkill_destroy(wwan_rfkill
);
567 static int hp_wmi_resume_handler(struct device
*device
)
570 * Hardware state may have changed while suspended, so trigger
571 * input events for the current state. As this is a switch,
572 * the input layer will only actually pass it on if the state
575 if (hp_wmi_input_dev
) {
576 input_report_switch(hp_wmi_input_dev
, SW_DOCK
,
577 hp_wmi_dock_state());
578 input_report_switch(hp_wmi_input_dev
, SW_TABLET_MODE
,
579 hp_wmi_tablet_state());
580 input_sync(hp_wmi_input_dev
);
584 rfkill_set_states(wifi_rfkill
,
585 hp_wmi_get_sw_state(HPWMI_WIFI
),
586 hp_wmi_get_hw_state(HPWMI_WIFI
));
587 if (bluetooth_rfkill
)
588 rfkill_set_states(bluetooth_rfkill
,
589 hp_wmi_get_sw_state(HPWMI_BLUETOOTH
),
590 hp_wmi_get_hw_state(HPWMI_BLUETOOTH
));
592 rfkill_set_states(wwan_rfkill
,
593 hp_wmi_get_sw_state(HPWMI_WWAN
),
594 hp_wmi_get_hw_state(HPWMI_WWAN
));
599 static int __init
hp_wmi_init(void)
603 if (wmi_has_guid(HPWMI_EVENT_GUID
)) {
604 err
= wmi_install_notify_handler(HPWMI_EVENT_GUID
,
605 hp_wmi_notify
, NULL
);
606 if (ACPI_SUCCESS(err
))
607 hp_wmi_input_setup();
610 if (wmi_has_guid(HPWMI_BIOS_GUID
)) {
611 err
= platform_driver_register(&hp_wmi_driver
);
614 hp_wmi_platform_dev
= platform_device_alloc("hp-wmi", -1);
615 if (!hp_wmi_platform_dev
) {
616 platform_driver_unregister(&hp_wmi_driver
);
619 platform_device_add(hp_wmi_platform_dev
);
625 static void __exit
hp_wmi_exit(void)
627 if (wmi_has_guid(HPWMI_EVENT_GUID
)) {
628 wmi_remove_notify_handler(HPWMI_EVENT_GUID
);
629 input_unregister_device(hp_wmi_input_dev
);
631 if (hp_wmi_platform_dev
) {
632 platform_device_del(hp_wmi_platform_dev
);
633 platform_driver_unregister(&hp_wmi_driver
);
637 module_init(hp_wmi_init
);
638 module_exit(hp_wmi_exit
);