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
);
72 char type
; /* See KE_* below */
77 enum { KE_KEY
, KE_SW
, KE_END
};
79 static struct key_entry hp_wmi_keymap
[] = {
80 {KE_SW
, 0x01, SW_DOCK
},
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
,
106 static int hp_wmi_perform_query(int query
, int write
, int value
)
108 struct bios_return bios_return
;
110 union acpi_object
*obj
;
111 struct bios_args args
= {
112 .signature
= 0x55434553,
113 .command
= write
? 0x2 : 0x1,
114 .commandtype
= query
,
115 .datasize
= write
? 0x4 : 0,
118 struct acpi_buffer input
= { sizeof(struct bios_args
), &args
};
119 struct acpi_buffer output
= { ACPI_ALLOCATE_BUFFER
, NULL
};
121 status
= wmi_evaluate_method(HPWMI_BIOS_GUID
, 0, 0x3, &input
, &output
);
123 obj
= output
.pointer
;
125 if (!obj
|| obj
->type
!= ACPI_TYPE_BUFFER
)
128 bios_return
= *((struct bios_return
*)obj
->buffer
.pointer
);
129 if (bios_return
.return_code
> 0)
130 return bios_return
.return_code
* -1;
132 return bios_return
.value
;
135 static int hp_wmi_display_state(void)
137 return hp_wmi_perform_query(HPWMI_DISPLAY_QUERY
, 0, 0);
140 static int hp_wmi_hddtemp_state(void)
142 return hp_wmi_perform_query(HPWMI_HDDTEMP_QUERY
, 0, 0);
145 static int hp_wmi_als_state(void)
147 return hp_wmi_perform_query(HPWMI_ALS_QUERY
, 0, 0);
150 static int hp_wmi_dock_state(void)
152 return hp_wmi_perform_query(HPWMI_DOCK_QUERY
, 0, 0);
155 static int hp_wmi_wifi_set(void *data
, enum rfkill_state state
)
158 return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 1, 0x101);
160 return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 1, 0x100);
163 static int hp_wmi_bluetooth_set(void *data
, enum rfkill_state state
)
166 return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 1, 0x202);
168 return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 1, 0x200);
171 static int hp_wmi_wwan_set(void *data
, enum rfkill_state state
)
174 return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 1, 0x404);
176 return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 1, 0x400);
179 static int hp_wmi_wifi_state(void)
181 int wireless
= hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 0, 0);
183 if (wireless
& 0x100)
184 return RFKILL_STATE_UNBLOCKED
;
186 return RFKILL_STATE_SOFT_BLOCKED
;
189 static int hp_wmi_bluetooth_state(void)
191 int wireless
= hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 0, 0);
193 if (wireless
& 0x10000)
194 return RFKILL_STATE_UNBLOCKED
;
196 return RFKILL_STATE_SOFT_BLOCKED
;
199 static int hp_wmi_wwan_state(void)
201 int wireless
= hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 0, 0);
203 if (wireless
& 0x1000000)
204 return RFKILL_STATE_UNBLOCKED
;
206 return RFKILL_STATE_SOFT_BLOCKED
;
209 static ssize_t
show_display(struct device
*dev
, struct device_attribute
*attr
,
212 int value
= hp_wmi_display_state();
215 return sprintf(buf
, "%d\n", value
);
218 static ssize_t
show_hddtemp(struct device
*dev
, struct device_attribute
*attr
,
221 int value
= hp_wmi_hddtemp_state();
224 return sprintf(buf
, "%d\n", value
);
227 static ssize_t
show_als(struct device
*dev
, struct device_attribute
*attr
,
230 int value
= hp_wmi_als_state();
233 return sprintf(buf
, "%d\n", value
);
236 static ssize_t
show_dock(struct device
*dev
, struct device_attribute
*attr
,
239 int value
= hp_wmi_dock_state();
242 return sprintf(buf
, "%d\n", value
);
245 static ssize_t
set_als(struct device
*dev
, struct device_attribute
*attr
,
246 const char *buf
, size_t count
)
248 u32 tmp
= simple_strtoul(buf
, NULL
, 10);
249 hp_wmi_perform_query(HPWMI_ALS_QUERY
, 1, tmp
);
253 static DEVICE_ATTR(display
, S_IRUGO
, show_display
, NULL
);
254 static DEVICE_ATTR(hddtemp
, S_IRUGO
, show_hddtemp
, NULL
);
255 static DEVICE_ATTR(als
, S_IRUGO
| S_IWUSR
, show_als
, set_als
);
256 static DEVICE_ATTR(dock
, S_IRUGO
, show_dock
, NULL
);
258 static struct key_entry
*hp_wmi_get_entry_by_scancode(int code
)
260 struct key_entry
*key
;
262 for (key
= hp_wmi_keymap
; key
->type
!= KE_END
; key
++)
263 if (code
== key
->code
)
269 static struct key_entry
*hp_wmi_get_entry_by_keycode(int keycode
)
271 struct key_entry
*key
;
273 for (key
= hp_wmi_keymap
; key
->type
!= KE_END
; key
++)
274 if (key
->type
== KE_KEY
&& keycode
== key
->keycode
)
280 static int hp_wmi_getkeycode(struct input_dev
*dev
, int scancode
, int *keycode
)
282 struct key_entry
*key
= hp_wmi_get_entry_by_scancode(scancode
);
284 if (key
&& key
->type
== KE_KEY
) {
285 *keycode
= key
->keycode
;
292 static int hp_wmi_setkeycode(struct input_dev
*dev
, int scancode
, int keycode
)
294 struct key_entry
*key
;
297 if (keycode
< 0 || keycode
> KEY_MAX
)
300 key
= hp_wmi_get_entry_by_scancode(scancode
);
301 if (key
&& key
->type
== KE_KEY
) {
302 old_keycode
= key
->keycode
;
303 key
->keycode
= keycode
;
304 set_bit(keycode
, dev
->keybit
);
305 if (!hp_wmi_get_entry_by_keycode(old_keycode
))
306 clear_bit(old_keycode
, dev
->keybit
);
313 static void hp_wmi_notify(u32 value
, void *context
)
315 struct acpi_buffer response
= { ACPI_ALLOCATE_BUFFER
, NULL
};
316 static struct key_entry
*key
;
317 union acpi_object
*obj
;
319 wmi_get_event_data(value
, &response
);
321 obj
= (union acpi_object
*)response
.pointer
;
323 if (obj
&& obj
->type
== ACPI_TYPE_BUFFER
&& obj
->buffer
.length
== 8) {
324 int eventcode
= *((u8
*) obj
->buffer
.pointer
);
325 if (eventcode
== 0x4)
326 eventcode
= hp_wmi_perform_query(HPWMI_HOTKEY_QUERY
, 0,
328 key
= hp_wmi_get_entry_by_scancode(eventcode
);
332 input_report_key(hp_wmi_input_dev
,
334 input_sync(hp_wmi_input_dev
);
335 input_report_key(hp_wmi_input_dev
,
337 input_sync(hp_wmi_input_dev
);
340 input_report_switch(hp_wmi_input_dev
,
342 hp_wmi_dock_state());
343 input_sync(hp_wmi_input_dev
);
346 } else if (eventcode
== 0x5) {
348 rfkill_force_state(wifi_rfkill
,
349 hp_wmi_wifi_state());
350 if (bluetooth_rfkill
)
351 rfkill_force_state(bluetooth_rfkill
,
352 hp_wmi_bluetooth_state());
354 rfkill_force_state(wwan_rfkill
,
355 hp_wmi_wwan_state());
357 printk(KERN_INFO
"HP WMI: Unknown key pressed - %x\n",
360 printk(KERN_INFO
"HP WMI: Unknown response received\n");
363 static int __init
hp_wmi_input_setup(void)
365 struct key_entry
*key
;
368 hp_wmi_input_dev
= input_allocate_device();
370 hp_wmi_input_dev
->name
= "HP WMI hotkeys";
371 hp_wmi_input_dev
->phys
= "wmi/input0";
372 hp_wmi_input_dev
->id
.bustype
= BUS_HOST
;
373 hp_wmi_input_dev
->getkeycode
= hp_wmi_getkeycode
;
374 hp_wmi_input_dev
->setkeycode
= hp_wmi_setkeycode
;
376 for (key
= hp_wmi_keymap
; key
->type
!= KE_END
; key
++) {
379 set_bit(EV_KEY
, hp_wmi_input_dev
->evbit
);
380 set_bit(key
->keycode
, hp_wmi_input_dev
->keybit
);
383 set_bit(EV_SW
, hp_wmi_input_dev
->evbit
);
384 set_bit(key
->keycode
, hp_wmi_input_dev
->swbit
);
386 /* Set initial dock state */
387 input_report_switch(hp_wmi_input_dev
, key
->keycode
,
388 hp_wmi_dock_state());
389 input_sync(hp_wmi_input_dev
);
394 err
= input_register_device(hp_wmi_input_dev
);
397 input_free_device(hp_wmi_input_dev
);
404 static void cleanup_sysfs(struct platform_device
*device
)
406 device_remove_file(&device
->dev
, &dev_attr_display
);
407 device_remove_file(&device
->dev
, &dev_attr_hddtemp
);
408 device_remove_file(&device
->dev
, &dev_attr_als
);
409 device_remove_file(&device
->dev
, &dev_attr_dock
);
412 static int __init
hp_wmi_bios_setup(struct platform_device
*device
)
415 int wireless
= hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 0, 0);
417 err
= device_create_file(&device
->dev
, &dev_attr_display
);
419 goto add_sysfs_error
;
420 err
= device_create_file(&device
->dev
, &dev_attr_hddtemp
);
422 goto add_sysfs_error
;
423 err
= device_create_file(&device
->dev
, &dev_attr_als
);
425 goto add_sysfs_error
;
426 err
= device_create_file(&device
->dev
, &dev_attr_dock
);
428 goto add_sysfs_error
;
430 if (wireless
& 0x1) {
431 wifi_rfkill
= rfkill_allocate(&device
->dev
, RFKILL_TYPE_WLAN
);
432 wifi_rfkill
->name
= "hp-wifi";
433 wifi_rfkill
->state
= hp_wmi_wifi_state();
434 wifi_rfkill
->toggle_radio
= hp_wmi_wifi_set
;
435 wifi_rfkill
->user_claim_unsupported
= 1;
436 err
= rfkill_register(wifi_rfkill
);
438 goto add_sysfs_error
;
441 if (wireless
& 0x2) {
442 bluetooth_rfkill
= rfkill_allocate(&device
->dev
,
443 RFKILL_TYPE_BLUETOOTH
);
444 bluetooth_rfkill
->name
= "hp-bluetooth";
445 bluetooth_rfkill
->state
= hp_wmi_bluetooth_state();
446 bluetooth_rfkill
->toggle_radio
= hp_wmi_bluetooth_set
;
447 bluetooth_rfkill
->user_claim_unsupported
= 1;
448 err
= rfkill_register(bluetooth_rfkill
);
450 goto register_bluetooth_error
;
453 if (wireless
& 0x4) {
454 wwan_rfkill
= rfkill_allocate(&device
->dev
, RFKILL_TYPE_WWAN
);
455 wwan_rfkill
->name
= "hp-wwan";
456 wwan_rfkill
->state
= hp_wmi_wwan_state();
457 wwan_rfkill
->toggle_radio
= hp_wmi_wwan_set
;
458 wwan_rfkill
->user_claim_unsupported
= 1;
459 err
= rfkill_register(wwan_rfkill
);
461 goto register_wwan_err
;
466 if (bluetooth_rfkill
)
467 rfkill_unregister(bluetooth_rfkill
);
468 register_bluetooth_error
:
470 rfkill_unregister(wifi_rfkill
);
472 cleanup_sysfs(device
);
476 static int __exit
hp_wmi_bios_remove(struct platform_device
*device
)
478 cleanup_sysfs(device
);
481 rfkill_unregister(wifi_rfkill
);
482 if (bluetooth_rfkill
)
483 rfkill_unregister(bluetooth_rfkill
);
485 rfkill_unregister(wwan_rfkill
);
490 static int __init
hp_wmi_init(void)
494 if (wmi_has_guid(HPWMI_EVENT_GUID
)) {
495 err
= wmi_install_notify_handler(HPWMI_EVENT_GUID
,
496 hp_wmi_notify
, NULL
);
498 hp_wmi_input_setup();
501 if (wmi_has_guid(HPWMI_BIOS_GUID
)) {
502 err
= platform_driver_register(&hp_wmi_driver
);
505 hp_wmi_platform_dev
= platform_device_alloc("hp-wmi", -1);
506 if (!hp_wmi_platform_dev
) {
507 platform_driver_unregister(&hp_wmi_driver
);
510 platform_device_add(hp_wmi_platform_dev
);
516 static void __exit
hp_wmi_exit(void)
518 if (wmi_has_guid(HPWMI_EVENT_GUID
)) {
519 wmi_remove_notify_handler(HPWMI_EVENT_GUID
);
520 input_unregister_device(hp_wmi_input_dev
);
522 if (hp_wmi_platform_dev
) {
523 platform_device_del(hp_wmi_platform_dev
);
524 platform_driver_unregister(&hp_wmi_driver
);
528 module_init(hp_wmi_init
);
529 module_exit(hp_wmi_exit
);