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
60 static int __init
hp_wmi_bios_setup(struct platform_device
*device
);
61 static int __exit
hp_wmi_bios_remove(struct platform_device
*device
);
62 static int hp_wmi_resume_handler(struct device
*device
);
79 char type
; /* See KE_* below */
84 enum { KE_KEY
, KE_END
};
86 static struct key_entry hp_wmi_keymap
[] = {
87 {KE_KEY
, 0x02, KEY_BRIGHTNESSUP
},
88 {KE_KEY
, 0x03, KEY_BRIGHTNESSDOWN
},
89 {KE_KEY
, 0x20e6, KEY_PROG1
},
90 {KE_KEY
, 0x2142, KEY_MEDIA
},
91 {KE_KEY
, 0x213b, KEY_INFO
},
92 {KE_KEY
, 0x231b, KEY_HELP
},
96 static struct input_dev
*hp_wmi_input_dev
;
97 static struct platform_device
*hp_wmi_platform_dev
;
99 static struct rfkill
*wifi_rfkill
;
100 static struct rfkill
*bluetooth_rfkill
;
101 static struct rfkill
*wwan_rfkill
;
103 static const struct dev_pm_ops hp_wmi_pm_ops
= {
104 .resume
= hp_wmi_resume_handler
,
105 .restore
= hp_wmi_resume_handler
,
108 static struct platform_driver hp_wmi_driver
= {
111 .owner
= THIS_MODULE
,
112 .pm
= &hp_wmi_pm_ops
,
114 .probe
= hp_wmi_bios_setup
,
115 .remove
= hp_wmi_bios_remove
,
118 static int hp_wmi_perform_query(int query
, int write
, int value
)
120 struct bios_return bios_return
;
122 union acpi_object
*obj
;
123 struct bios_args args
= {
124 .signature
= 0x55434553,
125 .command
= write
? 0x2 : 0x1,
126 .commandtype
= query
,
127 .datasize
= write
? 0x4 : 0,
130 struct acpi_buffer input
= { sizeof(struct bios_args
), &args
};
131 struct acpi_buffer output
= { ACPI_ALLOCATE_BUFFER
, NULL
};
133 status
= wmi_evaluate_method(HPWMI_BIOS_GUID
, 0, 0x3, &input
, &output
);
135 obj
= output
.pointer
;
137 if (!obj
|| obj
->type
!= ACPI_TYPE_BUFFER
)
140 bios_return
= *((struct bios_return
*)obj
->buffer
.pointer
);
141 if (bios_return
.return_code
> 0)
142 return bios_return
.return_code
* -1;
144 return bios_return
.value
;
147 static int hp_wmi_display_state(void)
149 return hp_wmi_perform_query(HPWMI_DISPLAY_QUERY
, 0, 0);
152 static int hp_wmi_hddtemp_state(void)
154 return hp_wmi_perform_query(HPWMI_HDDTEMP_QUERY
, 0, 0);
157 static int hp_wmi_als_state(void)
159 return hp_wmi_perform_query(HPWMI_ALS_QUERY
, 0, 0);
162 static int hp_wmi_dock_state(void)
164 int ret
= hp_wmi_perform_query(HPWMI_HARDWARE_QUERY
, 0, 0);
172 static int hp_wmi_tablet_state(void)
174 int ret
= hp_wmi_perform_query(HPWMI_HARDWARE_QUERY
, 0, 0);
179 return (ret
& 0x4) ? 1 : 0;
182 static int hp_wmi_set_block(void *data
, bool blocked
)
184 enum hp_wmi_radio r
= (enum hp_wmi_radio
) data
;
185 int query
= BIT(r
+ 8) | ((!blocked
) << r
);
187 return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 1, query
);
190 static const struct rfkill_ops hp_wmi_rfkill_ops
= {
191 .set_block
= hp_wmi_set_block
,
194 static bool hp_wmi_get_sw_state(enum hp_wmi_radio r
)
196 int wireless
= hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 0, 0);
197 int mask
= 0x200 << (r
* 8);
205 static bool hp_wmi_get_hw_state(enum hp_wmi_radio r
)
207 int wireless
= hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 0, 0);
208 int mask
= 0x800 << (r
* 8);
216 static ssize_t
show_display(struct device
*dev
, struct device_attribute
*attr
,
219 int value
= hp_wmi_display_state();
222 return sprintf(buf
, "%d\n", value
);
225 static ssize_t
show_hddtemp(struct device
*dev
, struct device_attribute
*attr
,
228 int value
= hp_wmi_hddtemp_state();
231 return sprintf(buf
, "%d\n", value
);
234 static ssize_t
show_als(struct device
*dev
, struct device_attribute
*attr
,
237 int value
= hp_wmi_als_state();
240 return sprintf(buf
, "%d\n", value
);
243 static ssize_t
show_dock(struct device
*dev
, struct device_attribute
*attr
,
246 int value
= hp_wmi_dock_state();
249 return sprintf(buf
, "%d\n", value
);
252 static ssize_t
show_tablet(struct device
*dev
, struct device_attribute
*attr
,
255 int value
= hp_wmi_tablet_state();
258 return sprintf(buf
, "%d\n", value
);
261 static ssize_t
set_als(struct device
*dev
, struct device_attribute
*attr
,
262 const char *buf
, size_t count
)
264 u32 tmp
= simple_strtoul(buf
, NULL
, 10);
265 hp_wmi_perform_query(HPWMI_ALS_QUERY
, 1, tmp
);
269 static DEVICE_ATTR(display
, S_IRUGO
, show_display
, NULL
);
270 static DEVICE_ATTR(hddtemp
, S_IRUGO
, show_hddtemp
, NULL
);
271 static DEVICE_ATTR(als
, S_IRUGO
| S_IWUSR
, show_als
, set_als
);
272 static DEVICE_ATTR(dock
, S_IRUGO
, show_dock
, NULL
);
273 static DEVICE_ATTR(tablet
, S_IRUGO
, show_tablet
, NULL
);
275 static struct key_entry
*hp_wmi_get_entry_by_scancode(int code
)
277 struct key_entry
*key
;
279 for (key
= hp_wmi_keymap
; key
->type
!= KE_END
; key
++)
280 if (code
== key
->code
)
286 static struct key_entry
*hp_wmi_get_entry_by_keycode(int keycode
)
288 struct key_entry
*key
;
290 for (key
= hp_wmi_keymap
; key
->type
!= KE_END
; key
++)
291 if (key
->type
== KE_KEY
&& keycode
== key
->keycode
)
297 static int hp_wmi_getkeycode(struct input_dev
*dev
, int scancode
, int *keycode
)
299 struct key_entry
*key
= hp_wmi_get_entry_by_scancode(scancode
);
301 if (key
&& key
->type
== KE_KEY
) {
302 *keycode
= key
->keycode
;
309 static int hp_wmi_setkeycode(struct input_dev
*dev
, int scancode
, int keycode
)
311 struct key_entry
*key
;
314 if (keycode
< 0 || keycode
> KEY_MAX
)
317 key
= hp_wmi_get_entry_by_scancode(scancode
);
318 if (key
&& key
->type
== KE_KEY
) {
319 old_keycode
= key
->keycode
;
320 key
->keycode
= keycode
;
321 set_bit(keycode
, dev
->keybit
);
322 if (!hp_wmi_get_entry_by_keycode(old_keycode
))
323 clear_bit(old_keycode
, dev
->keybit
);
330 static void hp_wmi_notify(u32 value
, void *context
)
332 struct acpi_buffer response
= { ACPI_ALLOCATE_BUFFER
, NULL
};
333 static struct key_entry
*key
;
334 union acpi_object
*obj
;
337 wmi_get_event_data(value
, &response
);
339 obj
= (union acpi_object
*)response
.pointer
;
341 if (!obj
|| obj
->type
!= ACPI_TYPE_BUFFER
|| obj
->buffer
.length
!= 8) {
342 printk(KERN_INFO
"HP WMI: Unknown response received\n");
346 eventcode
= *((u8
*) obj
->buffer
.pointer
);
347 if (eventcode
== 0x4)
348 eventcode
= hp_wmi_perform_query(HPWMI_HOTKEY_QUERY
, 0,
350 key
= hp_wmi_get_entry_by_scancode(eventcode
);
354 input_report_key(hp_wmi_input_dev
,
356 input_sync(hp_wmi_input_dev
);
357 input_report_key(hp_wmi_input_dev
,
359 input_sync(hp_wmi_input_dev
);
362 } else if (eventcode
== 0x1) {
363 input_report_switch(hp_wmi_input_dev
, SW_DOCK
,
364 hp_wmi_dock_state());
365 input_report_switch(hp_wmi_input_dev
, SW_TABLET_MODE
,
366 hp_wmi_tablet_state());
367 input_sync(hp_wmi_input_dev
);
368 } else if (eventcode
== 0x5) {
370 rfkill_set_states(wifi_rfkill
,
371 hp_wmi_get_sw_state(HPWMI_WIFI
),
372 hp_wmi_get_hw_state(HPWMI_WIFI
));
373 if (bluetooth_rfkill
)
374 rfkill_set_states(bluetooth_rfkill
,
375 hp_wmi_get_sw_state(HPWMI_BLUETOOTH
),
376 hp_wmi_get_hw_state(HPWMI_BLUETOOTH
));
378 rfkill_set_states(wwan_rfkill
,
379 hp_wmi_get_sw_state(HPWMI_WWAN
),
380 hp_wmi_get_hw_state(HPWMI_WWAN
));
382 printk(KERN_INFO
"HP WMI: Unknown key pressed - %x\n",
386 static int __init
hp_wmi_input_setup(void)
388 struct key_entry
*key
;
391 hp_wmi_input_dev
= input_allocate_device();
393 hp_wmi_input_dev
->name
= "HP WMI hotkeys";
394 hp_wmi_input_dev
->phys
= "wmi/input0";
395 hp_wmi_input_dev
->id
.bustype
= BUS_HOST
;
396 hp_wmi_input_dev
->getkeycode
= hp_wmi_getkeycode
;
397 hp_wmi_input_dev
->setkeycode
= hp_wmi_setkeycode
;
399 for (key
= hp_wmi_keymap
; key
->type
!= KE_END
; key
++) {
402 set_bit(EV_KEY
, hp_wmi_input_dev
->evbit
);
403 set_bit(key
->keycode
, hp_wmi_input_dev
->keybit
);
408 set_bit(EV_SW
, hp_wmi_input_dev
->evbit
);
409 set_bit(SW_DOCK
, hp_wmi_input_dev
->swbit
);
410 set_bit(SW_TABLET_MODE
, hp_wmi_input_dev
->swbit
);
412 /* Set initial hardware state */
413 input_report_switch(hp_wmi_input_dev
, SW_DOCK
, hp_wmi_dock_state());
414 input_report_switch(hp_wmi_input_dev
, SW_TABLET_MODE
,
415 hp_wmi_tablet_state());
416 input_sync(hp_wmi_input_dev
);
418 err
= input_register_device(hp_wmi_input_dev
);
421 input_free_device(hp_wmi_input_dev
);
428 static void cleanup_sysfs(struct platform_device
*device
)
430 device_remove_file(&device
->dev
, &dev_attr_display
);
431 device_remove_file(&device
->dev
, &dev_attr_hddtemp
);
432 device_remove_file(&device
->dev
, &dev_attr_als
);
433 device_remove_file(&device
->dev
, &dev_attr_dock
);
434 device_remove_file(&device
->dev
, &dev_attr_tablet
);
437 static int __init
hp_wmi_bios_setup(struct platform_device
*device
)
440 int wireless
= hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 0, 0);
442 err
= device_create_file(&device
->dev
, &dev_attr_display
);
444 goto add_sysfs_error
;
445 err
= device_create_file(&device
->dev
, &dev_attr_hddtemp
);
447 goto add_sysfs_error
;
448 err
= device_create_file(&device
->dev
, &dev_attr_als
);
450 goto add_sysfs_error
;
451 err
= device_create_file(&device
->dev
, &dev_attr_dock
);
453 goto add_sysfs_error
;
454 err
= device_create_file(&device
->dev
, &dev_attr_tablet
);
456 goto add_sysfs_error
;
458 if (wireless
& 0x1) {
459 wifi_rfkill
= rfkill_alloc("hp-wifi", &device
->dev
,
462 (void *) HPWMI_WIFI
);
463 rfkill_init_sw_state(wifi_rfkill
,
464 hp_wmi_get_sw_state(HPWMI_WIFI
));
465 rfkill_set_hw_state(wifi_rfkill
,
466 hp_wmi_get_hw_state(HPWMI_WIFI
));
467 err
= rfkill_register(wifi_rfkill
);
469 goto register_wifi_error
;
472 if (wireless
& 0x2) {
473 bluetooth_rfkill
= rfkill_alloc("hp-bluetooth", &device
->dev
,
474 RFKILL_TYPE_BLUETOOTH
,
476 (void *) HPWMI_BLUETOOTH
);
477 rfkill_init_sw_state(bluetooth_rfkill
,
478 hp_wmi_get_sw_state(HPWMI_BLUETOOTH
));
479 rfkill_set_hw_state(bluetooth_rfkill
,
480 hp_wmi_get_hw_state(HPWMI_BLUETOOTH
));
481 err
= rfkill_register(bluetooth_rfkill
);
483 goto register_bluetooth_error
;
486 if (wireless
& 0x4) {
487 wwan_rfkill
= rfkill_alloc("hp-wwan", &device
->dev
,
490 (void *) HPWMI_WWAN
);
491 rfkill_init_sw_state(wwan_rfkill
,
492 hp_wmi_get_sw_state(HPWMI_WWAN
));
493 rfkill_set_hw_state(wwan_rfkill
,
494 hp_wmi_get_hw_state(HPWMI_WWAN
));
495 err
= rfkill_register(wwan_rfkill
);
497 goto register_wwan_err
;
502 rfkill_destroy(wwan_rfkill
);
503 if (bluetooth_rfkill
)
504 rfkill_unregister(bluetooth_rfkill
);
505 register_bluetooth_error
:
506 rfkill_destroy(bluetooth_rfkill
);
508 rfkill_unregister(wifi_rfkill
);
510 rfkill_destroy(wifi_rfkill
);
512 cleanup_sysfs(device
);
516 static int __exit
hp_wmi_bios_remove(struct platform_device
*device
)
518 cleanup_sysfs(device
);
521 rfkill_unregister(wifi_rfkill
);
522 rfkill_destroy(wifi_rfkill
);
524 if (bluetooth_rfkill
) {
525 rfkill_unregister(bluetooth_rfkill
);
526 rfkill_destroy(bluetooth_rfkill
);
529 rfkill_unregister(wwan_rfkill
);
530 rfkill_destroy(wwan_rfkill
);
536 static int hp_wmi_resume_handler(struct device
*device
)
539 * Hardware state may have changed while suspended, so trigger
540 * input events for the current state. As this is a switch,
541 * the input layer will only actually pass it on if the state
544 if (hp_wmi_input_dev
) {
545 input_report_switch(hp_wmi_input_dev
, SW_DOCK
,
546 hp_wmi_dock_state());
547 input_report_switch(hp_wmi_input_dev
, SW_TABLET_MODE
,
548 hp_wmi_tablet_state());
549 input_sync(hp_wmi_input_dev
);
553 rfkill_set_states(wifi_rfkill
,
554 hp_wmi_get_sw_state(HPWMI_WIFI
),
555 hp_wmi_get_hw_state(HPWMI_WIFI
));
556 if (bluetooth_rfkill
)
557 rfkill_set_states(bluetooth_rfkill
,
558 hp_wmi_get_sw_state(HPWMI_BLUETOOTH
),
559 hp_wmi_get_hw_state(HPWMI_BLUETOOTH
));
561 rfkill_set_states(wwan_rfkill
,
562 hp_wmi_get_sw_state(HPWMI_WWAN
),
563 hp_wmi_get_hw_state(HPWMI_WWAN
));
568 static int __init
hp_wmi_init(void)
572 if (wmi_has_guid(HPWMI_EVENT_GUID
)) {
573 err
= wmi_install_notify_handler(HPWMI_EVENT_GUID
,
574 hp_wmi_notify
, NULL
);
576 hp_wmi_input_setup();
579 if (wmi_has_guid(HPWMI_BIOS_GUID
)) {
580 err
= platform_driver_register(&hp_wmi_driver
);
583 hp_wmi_platform_dev
= platform_device_alloc("hp-wmi", -1);
584 if (!hp_wmi_platform_dev
) {
585 platform_driver_unregister(&hp_wmi_driver
);
588 platform_device_add(hp_wmi_platform_dev
);
594 static void __exit
hp_wmi_exit(void)
596 if (wmi_has_guid(HPWMI_EVENT_GUID
)) {
597 wmi_remove_notify_handler(HPWMI_EVENT_GUID
);
598 input_unregister_device(hp_wmi_input_dev
);
600 if (hp_wmi_platform_dev
) {
601 platform_device_del(hp_wmi_platform_dev
);
602 platform_driver_unregister(&hp_wmi_driver
);
606 module_init(hp_wmi_init
);
607 module_exit(hp_wmi_exit
);