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_DOCK_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_SW
, KE_END
};
80 static struct key_entry hp_wmi_keymap
[] = {
81 {KE_SW
, 0x01, SW_DOCK
},
82 {KE_KEY
, 0x02, KEY_BRIGHTNESSUP
},
83 {KE_KEY
, 0x03, KEY_BRIGHTNESSDOWN
},
84 {KE_KEY
, 0x20e6, KEY_PROG1
},
85 {KE_KEY
, 0x2142, KEY_MEDIA
},
86 {KE_KEY
, 0x213b, KEY_INFO
},
87 {KE_KEY
, 0x231b, KEY_HELP
},
91 static struct input_dev
*hp_wmi_input_dev
;
92 static struct platform_device
*hp_wmi_platform_dev
;
94 static struct rfkill
*wifi_rfkill
;
95 static struct rfkill
*bluetooth_rfkill
;
96 static struct rfkill
*wwan_rfkill
;
98 static struct platform_driver hp_wmi_driver
= {
101 .owner
= THIS_MODULE
,
103 .probe
= hp_wmi_bios_setup
,
104 .remove
= hp_wmi_bios_remove
,
105 .resume
= hp_wmi_resume_handler
,
108 static int hp_wmi_perform_query(int query
, int write
, int value
)
110 struct bios_return bios_return
;
112 union acpi_object
*obj
;
113 struct bios_args args
= {
114 .signature
= 0x55434553,
115 .command
= write
? 0x2 : 0x1,
116 .commandtype
= query
,
117 .datasize
= write
? 0x4 : 0,
120 struct acpi_buffer input
= { sizeof(struct bios_args
), &args
};
121 struct acpi_buffer output
= { ACPI_ALLOCATE_BUFFER
, NULL
};
123 status
= wmi_evaluate_method(HPWMI_BIOS_GUID
, 0, 0x3, &input
, &output
);
125 obj
= output
.pointer
;
127 if (!obj
|| obj
->type
!= ACPI_TYPE_BUFFER
)
130 bios_return
= *((struct bios_return
*)obj
->buffer
.pointer
);
131 if (bios_return
.return_code
> 0)
132 return bios_return
.return_code
* -1;
134 return bios_return
.value
;
137 static int hp_wmi_display_state(void)
139 return hp_wmi_perform_query(HPWMI_DISPLAY_QUERY
, 0, 0);
142 static int hp_wmi_hddtemp_state(void)
144 return hp_wmi_perform_query(HPWMI_HDDTEMP_QUERY
, 0, 0);
147 static int hp_wmi_als_state(void)
149 return hp_wmi_perform_query(HPWMI_ALS_QUERY
, 0, 0);
152 static int hp_wmi_dock_state(void)
154 return hp_wmi_perform_query(HPWMI_DOCK_QUERY
, 0, 0);
157 static int hp_wmi_set_block(void *data
, bool blocked
)
159 unsigned long b
= (unsigned long) data
;
160 int query
= BIT(b
+ 8) | ((!!blocked
) << b
);
162 return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 1, query
);
165 static const struct rfkill_ops hp_wmi_rfkill_ops
= {
166 .set_block
= hp_wmi_set_block
,
169 static bool hp_wmi_wifi_state(void)
171 int wireless
= hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 0, 0);
173 if (wireless
& 0x100)
179 static bool hp_wmi_bluetooth_state(void)
181 int wireless
= hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 0, 0);
183 if (wireless
& 0x10000)
189 static bool hp_wmi_wwan_state(void)
191 int wireless
= hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 0, 0);
193 if (wireless
& 0x1000000)
199 static ssize_t
show_display(struct device
*dev
, struct device_attribute
*attr
,
202 int value
= hp_wmi_display_state();
205 return sprintf(buf
, "%d\n", value
);
208 static ssize_t
show_hddtemp(struct device
*dev
, struct device_attribute
*attr
,
211 int value
= hp_wmi_hddtemp_state();
214 return sprintf(buf
, "%d\n", value
);
217 static ssize_t
show_als(struct device
*dev
, struct device_attribute
*attr
,
220 int value
= hp_wmi_als_state();
223 return sprintf(buf
, "%d\n", value
);
226 static ssize_t
show_dock(struct device
*dev
, struct device_attribute
*attr
,
229 int value
= hp_wmi_dock_state();
232 return sprintf(buf
, "%d\n", value
);
235 static ssize_t
set_als(struct device
*dev
, struct device_attribute
*attr
,
236 const char *buf
, size_t count
)
238 u32 tmp
= simple_strtoul(buf
, NULL
, 10);
239 hp_wmi_perform_query(HPWMI_ALS_QUERY
, 1, tmp
);
243 static DEVICE_ATTR(display
, S_IRUGO
, show_display
, NULL
);
244 static DEVICE_ATTR(hddtemp
, S_IRUGO
, show_hddtemp
, NULL
);
245 static DEVICE_ATTR(als
, S_IRUGO
| S_IWUSR
, show_als
, set_als
);
246 static DEVICE_ATTR(dock
, S_IRUGO
, show_dock
, NULL
);
248 static struct key_entry
*hp_wmi_get_entry_by_scancode(int code
)
250 struct key_entry
*key
;
252 for (key
= hp_wmi_keymap
; key
->type
!= KE_END
; key
++)
253 if (code
== key
->code
)
259 static struct key_entry
*hp_wmi_get_entry_by_keycode(int keycode
)
261 struct key_entry
*key
;
263 for (key
= hp_wmi_keymap
; key
->type
!= KE_END
; key
++)
264 if (key
->type
== KE_KEY
&& keycode
== key
->keycode
)
270 static int hp_wmi_getkeycode(struct input_dev
*dev
, int scancode
, int *keycode
)
272 struct key_entry
*key
= hp_wmi_get_entry_by_scancode(scancode
);
274 if (key
&& key
->type
== KE_KEY
) {
275 *keycode
= key
->keycode
;
282 static int hp_wmi_setkeycode(struct input_dev
*dev
, int scancode
, int keycode
)
284 struct key_entry
*key
;
287 if (keycode
< 0 || keycode
> KEY_MAX
)
290 key
= hp_wmi_get_entry_by_scancode(scancode
);
291 if (key
&& key
->type
== KE_KEY
) {
292 old_keycode
= key
->keycode
;
293 key
->keycode
= keycode
;
294 set_bit(keycode
, dev
->keybit
);
295 if (!hp_wmi_get_entry_by_keycode(old_keycode
))
296 clear_bit(old_keycode
, dev
->keybit
);
303 static void hp_wmi_notify(u32 value
, void *context
)
305 struct acpi_buffer response
= { ACPI_ALLOCATE_BUFFER
, NULL
};
306 static struct key_entry
*key
;
307 union acpi_object
*obj
;
309 wmi_get_event_data(value
, &response
);
311 obj
= (union acpi_object
*)response
.pointer
;
313 if (obj
&& obj
->type
== ACPI_TYPE_BUFFER
&& obj
->buffer
.length
== 8) {
314 int eventcode
= *((u8
*) obj
->buffer
.pointer
);
315 if (eventcode
== 0x4)
316 eventcode
= hp_wmi_perform_query(HPWMI_HOTKEY_QUERY
, 0,
318 key
= hp_wmi_get_entry_by_scancode(eventcode
);
322 input_report_key(hp_wmi_input_dev
,
324 input_sync(hp_wmi_input_dev
);
325 input_report_key(hp_wmi_input_dev
,
327 input_sync(hp_wmi_input_dev
);
330 input_report_switch(hp_wmi_input_dev
,
332 hp_wmi_dock_state());
333 input_sync(hp_wmi_input_dev
);
336 } else if (eventcode
== 0x5) {
338 rfkill_set_sw_state(wifi_rfkill
,
339 hp_wmi_wifi_state());
340 if (bluetooth_rfkill
)
341 rfkill_set_sw_state(bluetooth_rfkill
,
342 hp_wmi_bluetooth_state());
344 rfkill_set_sw_state(wwan_rfkill
,
345 hp_wmi_wwan_state());
347 printk(KERN_INFO
"HP WMI: Unknown key pressed - %x\n",
350 printk(KERN_INFO
"HP WMI: Unknown response received\n");
353 static int __init
hp_wmi_input_setup(void)
355 struct key_entry
*key
;
358 hp_wmi_input_dev
= input_allocate_device();
360 hp_wmi_input_dev
->name
= "HP WMI hotkeys";
361 hp_wmi_input_dev
->phys
= "wmi/input0";
362 hp_wmi_input_dev
->id
.bustype
= BUS_HOST
;
363 hp_wmi_input_dev
->getkeycode
= hp_wmi_getkeycode
;
364 hp_wmi_input_dev
->setkeycode
= hp_wmi_setkeycode
;
366 for (key
= hp_wmi_keymap
; key
->type
!= KE_END
; key
++) {
369 set_bit(EV_KEY
, hp_wmi_input_dev
->evbit
);
370 set_bit(key
->keycode
, hp_wmi_input_dev
->keybit
);
373 set_bit(EV_SW
, hp_wmi_input_dev
->evbit
);
374 set_bit(key
->keycode
, hp_wmi_input_dev
->swbit
);
376 /* Set initial dock state */
377 input_report_switch(hp_wmi_input_dev
, key
->keycode
,
378 hp_wmi_dock_state());
379 input_sync(hp_wmi_input_dev
);
384 err
= input_register_device(hp_wmi_input_dev
);
387 input_free_device(hp_wmi_input_dev
);
394 static void cleanup_sysfs(struct platform_device
*device
)
396 device_remove_file(&device
->dev
, &dev_attr_display
);
397 device_remove_file(&device
->dev
, &dev_attr_hddtemp
);
398 device_remove_file(&device
->dev
, &dev_attr_als
);
399 device_remove_file(&device
->dev
, &dev_attr_dock
);
402 static int __init
hp_wmi_bios_setup(struct platform_device
*device
)
405 int wireless
= hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 0, 0);
407 err
= device_create_file(&device
->dev
, &dev_attr_display
);
409 goto add_sysfs_error
;
410 err
= device_create_file(&device
->dev
, &dev_attr_hddtemp
);
412 goto add_sysfs_error
;
413 err
= device_create_file(&device
->dev
, &dev_attr_als
);
415 goto add_sysfs_error
;
416 err
= device_create_file(&device
->dev
, &dev_attr_dock
);
418 goto add_sysfs_error
;
420 if (wireless
& 0x1) {
421 wifi_rfkill
= rfkill_alloc("hp-wifi", &device
->dev
,
425 err
= rfkill_register(wifi_rfkill
);
427 goto register_wifi_error
;
430 if (wireless
& 0x2) {
431 bluetooth_rfkill
= rfkill_alloc("hp-bluetooth", &device
->dev
,
432 RFKILL_TYPE_BLUETOOTH
,
435 err
= rfkill_register(bluetooth_rfkill
);
437 goto register_bluetooth_error
;
440 if (wireless
& 0x4) {
441 wwan_rfkill
= rfkill_alloc("hp-wwan", &device
->dev
,
445 err
= rfkill_register(wwan_rfkill
);
447 goto register_wwan_err
;
452 rfkill_destroy(wwan_rfkill
);
453 if (bluetooth_rfkill
)
454 rfkill_unregister(bluetooth_rfkill
);
455 register_bluetooth_error
:
456 rfkill_destroy(bluetooth_rfkill
);
458 rfkill_unregister(wifi_rfkill
);
460 rfkill_destroy(wifi_rfkill
);
462 cleanup_sysfs(device
);
466 static int __exit
hp_wmi_bios_remove(struct platform_device
*device
)
468 cleanup_sysfs(device
);
471 rfkill_unregister(wifi_rfkill
);
472 rfkill_destroy(wifi_rfkill
);
474 if (bluetooth_rfkill
) {
475 rfkill_unregister(bluetooth_rfkill
);
476 rfkill_destroy(wifi_rfkill
);
479 rfkill_unregister(wwan_rfkill
);
480 rfkill_destroy(wwan_rfkill
);
486 static int hp_wmi_resume_handler(struct platform_device
*device
)
488 struct key_entry
*key
;
491 * Docking state may have changed while suspended, so trigger
492 * an input event for the current state. As this is a switch,
493 * the input layer will only actually pass it on if the state
496 for (key
= hp_wmi_keymap
; key
->type
!= KE_END
; key
++) {
499 input_report_switch(hp_wmi_input_dev
, key
->keycode
,
500 hp_wmi_dock_state());
501 input_sync(hp_wmi_input_dev
);
509 static int __init
hp_wmi_init(void)
513 if (wmi_has_guid(HPWMI_EVENT_GUID
)) {
514 err
= wmi_install_notify_handler(HPWMI_EVENT_GUID
,
515 hp_wmi_notify
, NULL
);
517 hp_wmi_input_setup();
520 if (wmi_has_guid(HPWMI_BIOS_GUID
)) {
521 err
= platform_driver_register(&hp_wmi_driver
);
524 hp_wmi_platform_dev
= platform_device_alloc("hp-wmi", -1);
525 if (!hp_wmi_platform_dev
) {
526 platform_driver_unregister(&hp_wmi_driver
);
529 platform_device_add(hp_wmi_platform_dev
);
535 static void __exit
hp_wmi_exit(void)
537 if (wmi_has_guid(HPWMI_EVENT_GUID
)) {
538 wmi_remove_notify_handler(HPWMI_EVENT_GUID
);
539 input_unregister_device(hp_wmi_input_dev
);
541 if (hp_wmi_platform_dev
) {
542 platform_device_del(hp_wmi_platform_dev
);
543 platform_driver_unregister(&hp_wmi_driver
);
547 module_init(hp_wmi_init
);
548 module_exit(hp_wmi_exit
);