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
53 static int __init
hp_wmi_bios_setup(struct platform_device
*device
);
54 static int __exit
hp_wmi_bios_remove(struct platform_device
*device
);
71 char type
; /* See KE_* below */
76 enum { KE_KEY
, KE_SW
, KE_END
};
78 static struct key_entry hp_wmi_keymap
[] = {
79 {KE_SW
, 0x01, SW_DOCK
},
80 {KE_KEY
, 0x02, KEY_BRIGHTNESSUP
},
81 {KE_KEY
, 0x03, KEY_BRIGHTNESSDOWN
},
82 {KE_KEY
, 0x04, KEY_HELP
},
86 static struct input_dev
*hp_wmi_input_dev
;
87 static struct platform_device
*hp_wmi_platform_dev
;
89 static struct rfkill
*wifi_rfkill
;
90 static struct rfkill
*bluetooth_rfkill
;
91 static struct rfkill
*wwan_rfkill
;
93 static struct platform_driver hp_wmi_driver
= {
98 .probe
= hp_wmi_bios_setup
,
99 .remove
= hp_wmi_bios_remove
,
102 static int hp_wmi_perform_query(int query
, int write
, int value
)
104 struct bios_return bios_return
;
106 union acpi_object
*obj
;
107 struct bios_args args
= {
108 .signature
= 0x55434553,
109 .command
= write
? 0x2 : 0x1,
110 .commandtype
= query
,
111 .datasize
= write
? 0x4 : 0,
114 struct acpi_buffer input
= { sizeof(struct bios_args
), &args
};
115 struct acpi_buffer output
= { ACPI_ALLOCATE_BUFFER
, NULL
};
117 status
= wmi_evaluate_method(HPWMI_BIOS_GUID
, 0, 0x3, &input
, &output
);
119 obj
= output
.pointer
;
121 if (!obj
|| obj
->type
!= ACPI_TYPE_BUFFER
)
124 bios_return
= *((struct bios_return
*)obj
->buffer
.pointer
);
125 if (bios_return
.return_code
> 0)
126 return bios_return
.return_code
* -1;
128 return bios_return
.value
;
131 static int hp_wmi_display_state(void)
133 return hp_wmi_perform_query(HPWMI_DISPLAY_QUERY
, 0, 0);
136 static int hp_wmi_hddtemp_state(void)
138 return hp_wmi_perform_query(HPWMI_HDDTEMP_QUERY
, 0, 0);
141 static int hp_wmi_als_state(void)
143 return hp_wmi_perform_query(HPWMI_ALS_QUERY
, 0, 0);
146 static int hp_wmi_dock_state(void)
148 return hp_wmi_perform_query(HPWMI_DOCK_QUERY
, 0, 0);
151 static int hp_wmi_wifi_set(void *data
, enum rfkill_state state
)
154 return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 1, 0x101);
156 return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 1, 0x100);
159 static int hp_wmi_bluetooth_set(void *data
, enum rfkill_state state
)
162 return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 1, 0x202);
164 return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 1, 0x200);
167 static int hp_wmi_wwan_set(void *data
, enum rfkill_state state
)
170 return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 1, 0x404);
172 return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 1, 0x400);
175 static int hp_wmi_wifi_state(void)
177 int wireless
= hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 0, 0);
179 if (wireless
& 0x100)
185 static int hp_wmi_bluetooth_state(void)
187 int wireless
= hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 0, 0);
189 if (wireless
& 0x10000)
195 static int hp_wmi_wwan_state(void)
197 int wireless
= hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 0, 0);
199 if (wireless
& 0x1000000)
205 static ssize_t
show_display(struct device
*dev
, struct device_attribute
*attr
,
208 int value
= hp_wmi_display_state();
211 return sprintf(buf
, "%d\n", value
);
214 static ssize_t
show_hddtemp(struct device
*dev
, struct device_attribute
*attr
,
217 int value
= hp_wmi_hddtemp_state();
220 return sprintf(buf
, "%d\n", value
);
223 static ssize_t
show_als(struct device
*dev
, struct device_attribute
*attr
,
226 int value
= hp_wmi_als_state();
229 return sprintf(buf
, "%d\n", value
);
232 static ssize_t
show_dock(struct device
*dev
, struct device_attribute
*attr
,
235 int value
= hp_wmi_dock_state();
238 return sprintf(buf
, "%d\n", value
);
241 static ssize_t
set_als(struct device
*dev
, struct device_attribute
*attr
,
242 const char *buf
, size_t count
)
244 u32 tmp
= simple_strtoul(buf
, NULL
, 10);
245 hp_wmi_perform_query(HPWMI_ALS_QUERY
, 1, tmp
);
249 static DEVICE_ATTR(display
, S_IRUGO
, show_display
, NULL
);
250 static DEVICE_ATTR(hddtemp
, S_IRUGO
, show_hddtemp
, NULL
);
251 static DEVICE_ATTR(als
, S_IRUGO
| S_IWUSR
, show_als
, set_als
);
252 static DEVICE_ATTR(dock
, S_IRUGO
, show_dock
, NULL
);
254 static struct key_entry
*hp_wmi_get_entry_by_scancode(int code
)
256 struct key_entry
*key
;
258 for (key
= hp_wmi_keymap
; key
->type
!= KE_END
; key
++)
259 if (code
== key
->code
)
265 static struct key_entry
*hp_wmi_get_entry_by_keycode(int keycode
)
267 struct key_entry
*key
;
269 for (key
= hp_wmi_keymap
; key
->type
!= KE_END
; key
++)
270 if (key
->type
== KE_KEY
&& keycode
== key
->keycode
)
276 static int hp_wmi_getkeycode(struct input_dev
*dev
, int scancode
, int *keycode
)
278 struct key_entry
*key
= hp_wmi_get_entry_by_scancode(scancode
);
280 if (key
&& key
->type
== KE_KEY
) {
281 *keycode
= key
->keycode
;
288 static int hp_wmi_setkeycode(struct input_dev
*dev
, int scancode
, int keycode
)
290 struct key_entry
*key
;
293 if (keycode
< 0 || keycode
> KEY_MAX
)
296 key
= hp_wmi_get_entry_by_scancode(scancode
);
297 if (key
&& key
->type
== KE_KEY
) {
298 old_keycode
= key
->keycode
;
299 key
->keycode
= keycode
;
300 set_bit(keycode
, dev
->keybit
);
301 if (!hp_wmi_get_entry_by_keycode(old_keycode
))
302 clear_bit(old_keycode
, dev
->keybit
);
309 void hp_wmi_notify(u32 value
, void *context
)
311 struct acpi_buffer response
= { ACPI_ALLOCATE_BUFFER
, NULL
};
312 static struct key_entry
*key
;
313 union acpi_object
*obj
;
315 wmi_get_event_data(value
, &response
);
317 obj
= (union acpi_object
*)response
.pointer
;
319 if (obj
&& obj
->type
== ACPI_TYPE_BUFFER
&& obj
->buffer
.length
== 8) {
320 int eventcode
= *((u8
*) obj
->buffer
.pointer
);
321 key
= hp_wmi_get_entry_by_scancode(eventcode
);
325 input_report_key(hp_wmi_input_dev
,
327 input_sync(hp_wmi_input_dev
);
328 input_report_key(hp_wmi_input_dev
,
330 input_sync(hp_wmi_input_dev
);
333 input_report_switch(hp_wmi_input_dev
,
335 hp_wmi_dock_state());
336 input_sync(hp_wmi_input_dev
);
339 } else if (eventcode
== 0x5) {
341 wifi_rfkill
->state
= hp_wmi_wifi_state();
342 if (bluetooth_rfkill
)
343 bluetooth_rfkill
->state
=
344 hp_wmi_bluetooth_state();
346 wwan_rfkill
->state
= hp_wmi_wwan_state();
348 printk(KERN_INFO
"HP WMI: Unknown key pressed - %x\n",
351 printk(KERN_INFO
"HP WMI: Unknown response received\n");
354 static int __init
hp_wmi_input_setup(void)
356 struct key_entry
*key
;
359 hp_wmi_input_dev
= input_allocate_device();
361 hp_wmi_input_dev
->name
= "HP WMI hotkeys";
362 hp_wmi_input_dev
->phys
= "wmi/input0";
363 hp_wmi_input_dev
->id
.bustype
= BUS_HOST
;
364 hp_wmi_input_dev
->getkeycode
= hp_wmi_getkeycode
;
365 hp_wmi_input_dev
->setkeycode
= hp_wmi_setkeycode
;
367 for (key
= hp_wmi_keymap
; key
->type
!= KE_END
; key
++) {
370 set_bit(EV_KEY
, hp_wmi_input_dev
->evbit
);
371 set_bit(key
->keycode
, hp_wmi_input_dev
->keybit
);
374 set_bit(EV_SW
, hp_wmi_input_dev
->evbit
);
375 set_bit(key
->keycode
, hp_wmi_input_dev
->swbit
);
380 err
= input_register_device(hp_wmi_input_dev
);
383 input_free_device(hp_wmi_input_dev
);
390 static void cleanup_sysfs(struct platform_device
*device
)
392 device_remove_file(&device
->dev
, &dev_attr_display
);
393 device_remove_file(&device
->dev
, &dev_attr_hddtemp
);
394 device_remove_file(&device
->dev
, &dev_attr_als
);
395 device_remove_file(&device
->dev
, &dev_attr_dock
);
398 static int __init
hp_wmi_bios_setup(struct platform_device
*device
)
402 err
= device_create_file(&device
->dev
, &dev_attr_display
);
404 goto add_sysfs_error
;
405 err
= device_create_file(&device
->dev
, &dev_attr_hddtemp
);
407 goto add_sysfs_error
;
408 err
= device_create_file(&device
->dev
, &dev_attr_als
);
410 goto add_sysfs_error
;
411 err
= device_create_file(&device
->dev
, &dev_attr_dock
);
413 goto add_sysfs_error
;
415 wifi_rfkill
= rfkill_allocate(&device
->dev
, RFKILL_TYPE_WLAN
);
416 wifi_rfkill
->name
= "hp-wifi";
417 wifi_rfkill
->state
= hp_wmi_wifi_state();
418 wifi_rfkill
->toggle_radio
= hp_wmi_wifi_set
;
419 wifi_rfkill
->user_claim_unsupported
= 1;
421 bluetooth_rfkill
= rfkill_allocate(&device
->dev
,
422 RFKILL_TYPE_BLUETOOTH
);
423 bluetooth_rfkill
->name
= "hp-bluetooth";
424 bluetooth_rfkill
->state
= hp_wmi_bluetooth_state();
425 bluetooth_rfkill
->toggle_radio
= hp_wmi_bluetooth_set
;
426 bluetooth_rfkill
->user_claim_unsupported
= 1;
428 wwan_rfkill
= rfkill_allocate(&device
->dev
, RFKILL_TYPE_WIMAX
);
429 wwan_rfkill
->name
= "hp-wwan";
430 wwan_rfkill
->state
= hp_wmi_wwan_state();
431 wwan_rfkill
->toggle_radio
= hp_wmi_wwan_set
;
432 wwan_rfkill
->user_claim_unsupported
= 1;
434 rfkill_register(wifi_rfkill
);
435 rfkill_register(bluetooth_rfkill
);
436 rfkill_register(wwan_rfkill
);
440 cleanup_sysfs(device
);
444 static int __exit
hp_wmi_bios_remove(struct platform_device
*device
)
446 cleanup_sysfs(device
);
448 rfkill_unregister(wifi_rfkill
);
449 rfkill_unregister(bluetooth_rfkill
);
450 rfkill_unregister(wwan_rfkill
);
455 static int __init
hp_wmi_init(void)
459 if (wmi_has_guid(HPWMI_EVENT_GUID
)) {
460 err
= wmi_install_notify_handler(HPWMI_EVENT_GUID
,
461 hp_wmi_notify
, NULL
);
463 hp_wmi_input_setup();
466 if (wmi_has_guid(HPWMI_BIOS_GUID
)) {
467 err
= platform_driver_register(&hp_wmi_driver
);
470 hp_wmi_platform_dev
= platform_device_alloc("hp-wmi", -1);
471 if (!hp_wmi_platform_dev
) {
472 platform_driver_unregister(&hp_wmi_driver
);
475 platform_device_add(hp_wmi_platform_dev
);
481 static void __exit
hp_wmi_exit(void)
483 if (wmi_has_guid(HPWMI_EVENT_GUID
)) {
484 wmi_remove_notify_handler(HPWMI_EVENT_GUID
);
485 input_unregister_device(hp_wmi_input_dev
);
487 if (hp_wmi_platform_dev
) {
488 platform_device_del(hp_wmi_platform_dev
);
489 platform_driver_unregister(&hp_wmi_driver
);
493 module_init(hp_wmi_init
);
494 module_exit(hp_wmi_exit
);