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/types.h>
30 #include <linux/input.h>
31 #include <acpi/acpi_drivers.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 static int __init
hp_wmi_bios_setup(struct platform_device
*device
);
55 static int __exit
hp_wmi_bios_remove(struct platform_device
*device
);
56 static int hp_wmi_resume_handler(struct platform_device
*device
);
73 char type
; /* See KE_* below */
78 enum { KE_KEY
, KE_END
};
80 static struct key_entry hp_wmi_keymap
[] = {
81 {KE_KEY
, 0x02, KEY_BRIGHTNESSUP
},
82 {KE_KEY
, 0x03, KEY_BRIGHTNESSDOWN
},
83 {KE_KEY
, 0x20e6, KEY_PROG1
},
84 {KE_KEY
, 0x2142, KEY_MEDIA
},
85 {KE_KEY
, 0x213b, KEY_INFO
},
86 {KE_KEY
, 0x231b, KEY_HELP
},
90 static struct input_dev
*hp_wmi_input_dev
;
91 static struct platform_device
*hp_wmi_platform_dev
;
93 static struct rfkill
*wifi_rfkill
;
94 static struct rfkill
*bluetooth_rfkill
;
95 static struct rfkill
*wwan_rfkill
;
97 static struct platform_driver hp_wmi_driver
= {
100 .owner
= THIS_MODULE
,
102 .probe
= hp_wmi_bios_setup
,
103 .remove
= hp_wmi_bios_remove
,
104 .resume
= hp_wmi_resume_handler
,
107 static int hp_wmi_perform_query(int query
, int write
, int value
)
109 struct bios_return bios_return
;
111 union acpi_object
*obj
;
112 struct bios_args args
= {
113 .signature
= 0x55434553,
114 .command
= write
? 0x2 : 0x1,
115 .commandtype
= query
,
116 .datasize
= write
? 0x4 : 0,
119 struct acpi_buffer input
= { sizeof(struct bios_args
), &args
};
120 struct acpi_buffer output
= { ACPI_ALLOCATE_BUFFER
, NULL
};
122 status
= wmi_evaluate_method(HPWMI_BIOS_GUID
, 0, 0x3, &input
, &output
);
124 obj
= output
.pointer
;
126 if (!obj
|| obj
->type
!= ACPI_TYPE_BUFFER
)
129 bios_return
= *((struct bios_return
*)obj
->buffer
.pointer
);
130 if (bios_return
.return_code
> 0)
131 return bios_return
.return_code
* -1;
133 return bios_return
.value
;
136 static int hp_wmi_display_state(void)
138 return hp_wmi_perform_query(HPWMI_DISPLAY_QUERY
, 0, 0);
141 static int hp_wmi_hddtemp_state(void)
143 return hp_wmi_perform_query(HPWMI_HDDTEMP_QUERY
, 0, 0);
146 static int hp_wmi_als_state(void)
148 return hp_wmi_perform_query(HPWMI_ALS_QUERY
, 0, 0);
151 static int hp_wmi_dock_state(void)
153 int ret
= hp_wmi_perform_query(HPWMI_HARDWARE_QUERY
, 0, 0);
161 static int hp_wmi_tablet_state(void)
163 int ret
= hp_wmi_perform_query(HPWMI_HARDWARE_QUERY
, 0, 0);
168 return (ret
& 0x4) ? 1 : 0;
171 static int hp_wmi_set_block(void *data
, bool blocked
)
173 unsigned long b
= (unsigned long) data
;
174 int query
= BIT(b
+ 8) | ((!!blocked
) << b
);
176 return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 1, query
);
179 static const struct rfkill_ops hp_wmi_rfkill_ops
= {
180 .set_block
= hp_wmi_set_block
,
183 static bool hp_wmi_wifi_state(void)
185 int wireless
= hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 0, 0);
187 if (wireless
& 0x100)
193 static bool hp_wmi_bluetooth_state(void)
195 int wireless
= hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 0, 0);
197 if (wireless
& 0x10000)
203 static bool hp_wmi_wwan_state(void)
205 int wireless
= hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 0, 0);
207 if (wireless
& 0x1000000)
213 static ssize_t
show_display(struct device
*dev
, struct device_attribute
*attr
,
216 int value
= hp_wmi_display_state();
219 return sprintf(buf
, "%d\n", value
);
222 static ssize_t
show_hddtemp(struct device
*dev
, struct device_attribute
*attr
,
225 int value
= hp_wmi_hddtemp_state();
228 return sprintf(buf
, "%d\n", value
);
231 static ssize_t
show_als(struct device
*dev
, struct device_attribute
*attr
,
234 int value
= hp_wmi_als_state();
237 return sprintf(buf
, "%d\n", value
);
240 static ssize_t
show_dock(struct device
*dev
, struct device_attribute
*attr
,
243 int value
= hp_wmi_dock_state();
246 return sprintf(buf
, "%d\n", value
);
249 static ssize_t
show_tablet(struct device
*dev
, struct device_attribute
*attr
,
252 int value
= hp_wmi_tablet_state();
255 return sprintf(buf
, "%d\n", value
);
258 static ssize_t
set_als(struct device
*dev
, struct device_attribute
*attr
,
259 const char *buf
, size_t count
)
261 u32 tmp
= simple_strtoul(buf
, NULL
, 10);
262 hp_wmi_perform_query(HPWMI_ALS_QUERY
, 1, tmp
);
266 static DEVICE_ATTR(display
, S_IRUGO
, show_display
, NULL
);
267 static DEVICE_ATTR(hddtemp
, S_IRUGO
, show_hddtemp
, NULL
);
268 static DEVICE_ATTR(als
, S_IRUGO
| S_IWUSR
, show_als
, set_als
);
269 static DEVICE_ATTR(dock
, S_IRUGO
, show_dock
, NULL
);
270 static DEVICE_ATTR(tablet
, S_IRUGO
, show_tablet
, NULL
);
272 static struct key_entry
*hp_wmi_get_entry_by_scancode(int code
)
274 struct key_entry
*key
;
276 for (key
= hp_wmi_keymap
; key
->type
!= KE_END
; key
++)
277 if (code
== key
->code
)
283 static struct key_entry
*hp_wmi_get_entry_by_keycode(int keycode
)
285 struct key_entry
*key
;
287 for (key
= hp_wmi_keymap
; key
->type
!= KE_END
; key
++)
288 if (key
->type
== KE_KEY
&& keycode
== key
->keycode
)
294 static int hp_wmi_getkeycode(struct input_dev
*dev
, int scancode
, int *keycode
)
296 struct key_entry
*key
= hp_wmi_get_entry_by_scancode(scancode
);
298 if (key
&& key
->type
== KE_KEY
) {
299 *keycode
= key
->keycode
;
306 static int hp_wmi_setkeycode(struct input_dev
*dev
, int scancode
, int keycode
)
308 struct key_entry
*key
;
311 if (keycode
< 0 || keycode
> KEY_MAX
)
314 key
= hp_wmi_get_entry_by_scancode(scancode
);
315 if (key
&& key
->type
== KE_KEY
) {
316 old_keycode
= key
->keycode
;
317 key
->keycode
= keycode
;
318 set_bit(keycode
, dev
->keybit
);
319 if (!hp_wmi_get_entry_by_keycode(old_keycode
))
320 clear_bit(old_keycode
, dev
->keybit
);
327 static void hp_wmi_notify(u32 value
, void *context
)
329 struct acpi_buffer response
= { ACPI_ALLOCATE_BUFFER
, NULL
};
330 static struct key_entry
*key
;
331 union acpi_object
*obj
;
333 wmi_get_event_data(value
, &response
);
335 obj
= (union acpi_object
*)response
.pointer
;
337 if (obj
&& obj
->type
== ACPI_TYPE_BUFFER
&& obj
->buffer
.length
== 8) {
338 int eventcode
= *((u8
*) obj
->buffer
.pointer
);
339 if (eventcode
== 0x4)
340 eventcode
= hp_wmi_perform_query(HPWMI_HOTKEY_QUERY
, 0,
342 key
= hp_wmi_get_entry_by_scancode(eventcode
);
346 input_report_key(hp_wmi_input_dev
,
348 input_sync(hp_wmi_input_dev
);
349 input_report_key(hp_wmi_input_dev
,
351 input_sync(hp_wmi_input_dev
);
354 } else if (eventcode
== 0x1) {
355 input_report_switch(hp_wmi_input_dev
, SW_DOCK
,
356 hp_wmi_dock_state());
357 input_report_switch(hp_wmi_input_dev
, SW_TABLET_MODE
,
358 hp_wmi_tablet_state());
359 input_sync(hp_wmi_input_dev
);
360 } else if (eventcode
== 0x5) {
362 rfkill_set_sw_state(wifi_rfkill
,
363 hp_wmi_wifi_state());
364 if (bluetooth_rfkill
)
365 rfkill_set_sw_state(bluetooth_rfkill
,
366 hp_wmi_bluetooth_state());
368 rfkill_set_sw_state(wwan_rfkill
,
369 hp_wmi_wwan_state());
371 printk(KERN_INFO
"HP WMI: Unknown key pressed - %x\n",
374 printk(KERN_INFO
"HP WMI: Unknown response received\n");
377 static int __init
hp_wmi_input_setup(void)
379 struct key_entry
*key
;
382 hp_wmi_input_dev
= input_allocate_device();
384 hp_wmi_input_dev
->name
= "HP WMI hotkeys";
385 hp_wmi_input_dev
->phys
= "wmi/input0";
386 hp_wmi_input_dev
->id
.bustype
= BUS_HOST
;
387 hp_wmi_input_dev
->getkeycode
= hp_wmi_getkeycode
;
388 hp_wmi_input_dev
->setkeycode
= hp_wmi_setkeycode
;
390 for (key
= hp_wmi_keymap
; key
->type
!= KE_END
; key
++) {
393 set_bit(EV_KEY
, hp_wmi_input_dev
->evbit
);
394 set_bit(key
->keycode
, hp_wmi_input_dev
->keybit
);
399 set_bit(EV_SW
, hp_wmi_input_dev
->evbit
);
400 set_bit(SW_DOCK
, hp_wmi_input_dev
->swbit
);
401 set_bit(SW_TABLET_MODE
, hp_wmi_input_dev
->swbit
);
403 /* Set initial hardware state */
404 input_report_switch(hp_wmi_input_dev
, SW_DOCK
, hp_wmi_dock_state());
405 input_report_switch(hp_wmi_input_dev
, SW_TABLET_MODE
,
406 hp_wmi_tablet_state());
407 input_sync(hp_wmi_input_dev
);
409 err
= input_register_device(hp_wmi_input_dev
);
412 input_free_device(hp_wmi_input_dev
);
419 static void cleanup_sysfs(struct platform_device
*device
)
421 device_remove_file(&device
->dev
, &dev_attr_display
);
422 device_remove_file(&device
->dev
, &dev_attr_hddtemp
);
423 device_remove_file(&device
->dev
, &dev_attr_als
);
424 device_remove_file(&device
->dev
, &dev_attr_dock
);
425 device_remove_file(&device
->dev
, &dev_attr_tablet
);
428 static int __init
hp_wmi_bios_setup(struct platform_device
*device
)
431 int wireless
= hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 0, 0);
433 err
= device_create_file(&device
->dev
, &dev_attr_display
);
435 goto add_sysfs_error
;
436 err
= device_create_file(&device
->dev
, &dev_attr_hddtemp
);
438 goto add_sysfs_error
;
439 err
= device_create_file(&device
->dev
, &dev_attr_als
);
441 goto add_sysfs_error
;
442 err
= device_create_file(&device
->dev
, &dev_attr_dock
);
444 goto add_sysfs_error
;
445 err
= device_create_file(&device
->dev
, &dev_attr_tablet
);
447 goto add_sysfs_error
;
449 if (wireless
& 0x1) {
450 wifi_rfkill
= rfkill_alloc("hp-wifi", &device
->dev
,
454 err
= rfkill_register(wifi_rfkill
);
456 goto register_wifi_error
;
459 if (wireless
& 0x2) {
460 bluetooth_rfkill
= rfkill_alloc("hp-bluetooth", &device
->dev
,
461 RFKILL_TYPE_BLUETOOTH
,
464 err
= rfkill_register(bluetooth_rfkill
);
466 goto register_bluetooth_error
;
469 if (wireless
& 0x4) {
470 wwan_rfkill
= rfkill_alloc("hp-wwan", &device
->dev
,
474 err
= rfkill_register(wwan_rfkill
);
476 goto register_wwan_err
;
481 rfkill_destroy(wwan_rfkill
);
482 if (bluetooth_rfkill
)
483 rfkill_unregister(bluetooth_rfkill
);
484 register_bluetooth_error
:
485 rfkill_destroy(bluetooth_rfkill
);
487 rfkill_unregister(wifi_rfkill
);
489 rfkill_destroy(wifi_rfkill
);
491 cleanup_sysfs(device
);
495 static int __exit
hp_wmi_bios_remove(struct platform_device
*device
)
497 cleanup_sysfs(device
);
500 rfkill_unregister(wifi_rfkill
);
501 rfkill_destroy(wifi_rfkill
);
503 if (bluetooth_rfkill
) {
504 rfkill_unregister(bluetooth_rfkill
);
505 rfkill_destroy(wifi_rfkill
);
508 rfkill_unregister(wwan_rfkill
);
509 rfkill_destroy(wwan_rfkill
);
515 static int hp_wmi_resume_handler(struct platform_device
*device
)
518 * Hardware state may have changed while suspended, so trigger
519 * input events for the current state. As this is a switch,
520 * the input layer will only actually pass it on if the state
524 input_report_switch(hp_wmi_input_dev
, SW_DOCK
, hp_wmi_dock_state());
525 input_report_switch(hp_wmi_input_dev
, SW_TABLET_MODE
,
526 hp_wmi_tablet_state());
527 input_sync(hp_wmi_input_dev
);
532 static int __init
hp_wmi_init(void)
536 if (wmi_has_guid(HPWMI_EVENT_GUID
)) {
537 err
= wmi_install_notify_handler(HPWMI_EVENT_GUID
,
538 hp_wmi_notify
, NULL
);
540 hp_wmi_input_setup();
543 if (wmi_has_guid(HPWMI_BIOS_GUID
)) {
544 err
= platform_driver_register(&hp_wmi_driver
);
547 hp_wmi_platform_dev
= platform_device_alloc("hp-wmi", -1);
548 if (!hp_wmi_platform_dev
) {
549 platform_driver_unregister(&hp_wmi_driver
);
552 platform_device_add(hp_wmi_platform_dev
);
558 static void __exit
hp_wmi_exit(void)
560 if (wmi_has_guid(HPWMI_EVENT_GUID
)) {
561 wmi_remove_notify_handler(HPWMI_EVENT_GUID
);
562 input_unregister_device(hp_wmi_input_dev
);
564 if (hp_wmi_platform_dev
) {
565 platform_device_del(hp_wmi_platform_dev
);
566 platform_driver_unregister(&hp_wmi_driver
);
570 module_init(hp_wmi_init
);
571 module_exit(hp_wmi_exit
);