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/slab.h>
30 #include <linux/types.h>
31 #include <linux/input.h>
32 #include <acpi/acpi_drivers.h>
33 #include <linux/platform_device.h>
34 #include <linux/acpi.h>
35 #include <linux/rfkill.h>
36 #include <linux/string.h>
38 MODULE_AUTHOR("Matthew Garrett <mjg59@srcf.ucam.org>");
39 MODULE_DESCRIPTION("HP laptop WMI hotkeys driver");
40 MODULE_LICENSE("GPL");
42 MODULE_ALIAS("wmi:95F24279-4D7B-4334-9387-ACCDC67EF61C");
43 MODULE_ALIAS("wmi:5FB7F034-2C63-45e9-BE91-3D44E2C707E4");
45 #define HPWMI_EVENT_GUID "95F24279-4D7B-4334-9387-ACCDC67EF61C"
46 #define HPWMI_BIOS_GUID "5FB7F034-2C63-45e9-BE91-3D44E2C707E4"
48 #define HPWMI_DISPLAY_QUERY 0x1
49 #define HPWMI_HDDTEMP_QUERY 0x2
50 #define HPWMI_ALS_QUERY 0x3
51 #define HPWMI_HARDWARE_QUERY 0x4
52 #define HPWMI_WIRELESS_QUERY 0x5
53 #define HPWMI_HOTKEY_QUERY 0xc
61 static int __devinit
hp_wmi_bios_setup(struct platform_device
*device
);
62 static int __exit
hp_wmi_bios_remove(struct platform_device
*device
);
63 static int hp_wmi_resume_handler(struct device
*device
);
80 char type
; /* See KE_* below */
85 enum { KE_KEY
, KE_END
};
87 static struct key_entry hp_wmi_keymap
[] = {
88 {KE_KEY
, 0x02, KEY_BRIGHTNESSUP
},
89 {KE_KEY
, 0x03, KEY_BRIGHTNESSDOWN
},
90 {KE_KEY
, 0x20e6, KEY_PROG1
},
91 {KE_KEY
, 0x2142, KEY_MEDIA
},
92 {KE_KEY
, 0x213b, KEY_INFO
},
93 {KE_KEY
, 0x2169, KEY_DIRECTION
},
94 {KE_KEY
, 0x231b, KEY_HELP
},
98 static struct input_dev
*hp_wmi_input_dev
;
99 static struct platform_device
*hp_wmi_platform_dev
;
101 static struct rfkill
*wifi_rfkill
;
102 static struct rfkill
*bluetooth_rfkill
;
103 static struct rfkill
*wwan_rfkill
;
105 static const struct dev_pm_ops hp_wmi_pm_ops
= {
106 .resume
= hp_wmi_resume_handler
,
107 .restore
= hp_wmi_resume_handler
,
110 static struct platform_driver hp_wmi_driver
= {
113 .owner
= THIS_MODULE
,
114 .pm
= &hp_wmi_pm_ops
,
116 .probe
= hp_wmi_bios_setup
,
117 .remove
= hp_wmi_bios_remove
,
120 static int hp_wmi_perform_query(int query
, int write
, int value
)
122 struct bios_return bios_return
;
124 union acpi_object
*obj
;
125 struct bios_args args
= {
126 .signature
= 0x55434553,
127 .command
= write
? 0x2 : 0x1,
128 .commandtype
= query
,
129 .datasize
= write
? 0x4 : 0,
132 struct acpi_buffer input
= { sizeof(struct bios_args
), &args
};
133 struct acpi_buffer output
= { ACPI_ALLOCATE_BUFFER
, NULL
};
135 status
= wmi_evaluate_method(HPWMI_BIOS_GUID
, 0, 0x3, &input
, &output
);
137 obj
= output
.pointer
;
141 else if (obj
->type
!= ACPI_TYPE_BUFFER
) {
146 bios_return
= *((struct bios_return
*)obj
->buffer
.pointer
);
148 if (bios_return
.return_code
> 0)
149 return bios_return
.return_code
* -1;
151 return bios_return
.value
;
154 static int hp_wmi_display_state(void)
156 return hp_wmi_perform_query(HPWMI_DISPLAY_QUERY
, 0, 0);
159 static int hp_wmi_hddtemp_state(void)
161 return hp_wmi_perform_query(HPWMI_HDDTEMP_QUERY
, 0, 0);
164 static int hp_wmi_als_state(void)
166 return hp_wmi_perform_query(HPWMI_ALS_QUERY
, 0, 0);
169 static int hp_wmi_dock_state(void)
171 int ret
= hp_wmi_perform_query(HPWMI_HARDWARE_QUERY
, 0, 0);
179 static int hp_wmi_tablet_state(void)
181 int ret
= hp_wmi_perform_query(HPWMI_HARDWARE_QUERY
, 0, 0);
186 return (ret
& 0x4) ? 1 : 0;
189 static int hp_wmi_set_block(void *data
, bool blocked
)
191 enum hp_wmi_radio r
= (enum hp_wmi_radio
) data
;
192 int query
= BIT(r
+ 8) | ((!blocked
) << r
);
194 return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 1, query
);
197 static const struct rfkill_ops hp_wmi_rfkill_ops
= {
198 .set_block
= hp_wmi_set_block
,
201 static bool hp_wmi_get_sw_state(enum hp_wmi_radio r
)
203 int wireless
= hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 0, 0);
204 int mask
= 0x200 << (r
* 8);
212 static bool hp_wmi_get_hw_state(enum hp_wmi_radio r
)
214 int wireless
= hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 0, 0);
215 int mask
= 0x800 << (r
* 8);
223 static ssize_t
show_display(struct device
*dev
, struct device_attribute
*attr
,
226 int value
= hp_wmi_display_state();
229 return sprintf(buf
, "%d\n", value
);
232 static ssize_t
show_hddtemp(struct device
*dev
, struct device_attribute
*attr
,
235 int value
= hp_wmi_hddtemp_state();
238 return sprintf(buf
, "%d\n", value
);
241 static ssize_t
show_als(struct device
*dev
, struct device_attribute
*attr
,
244 int value
= hp_wmi_als_state();
247 return sprintf(buf
, "%d\n", value
);
250 static ssize_t
show_dock(struct device
*dev
, struct device_attribute
*attr
,
253 int value
= hp_wmi_dock_state();
256 return sprintf(buf
, "%d\n", value
);
259 static ssize_t
show_tablet(struct device
*dev
, struct device_attribute
*attr
,
262 int value
= hp_wmi_tablet_state();
265 return sprintf(buf
, "%d\n", value
);
268 static ssize_t
set_als(struct device
*dev
, struct device_attribute
*attr
,
269 const char *buf
, size_t count
)
271 u32 tmp
= simple_strtoul(buf
, NULL
, 10);
272 hp_wmi_perform_query(HPWMI_ALS_QUERY
, 1, tmp
);
276 static DEVICE_ATTR(display
, S_IRUGO
, show_display
, NULL
);
277 static DEVICE_ATTR(hddtemp
, S_IRUGO
, show_hddtemp
, NULL
);
278 static DEVICE_ATTR(als
, S_IRUGO
| S_IWUSR
, show_als
, set_als
);
279 static DEVICE_ATTR(dock
, S_IRUGO
, show_dock
, NULL
);
280 static DEVICE_ATTR(tablet
, S_IRUGO
, show_tablet
, NULL
);
282 static struct key_entry
*hp_wmi_get_entry_by_scancode(unsigned int code
)
284 struct key_entry
*key
;
286 for (key
= hp_wmi_keymap
; key
->type
!= KE_END
; key
++)
287 if (code
== key
->code
)
293 static struct key_entry
*hp_wmi_get_entry_by_keycode(unsigned int keycode
)
295 struct key_entry
*key
;
297 for (key
= hp_wmi_keymap
; key
->type
!= KE_END
; key
++)
298 if (key
->type
== KE_KEY
&& keycode
== key
->keycode
)
304 static int hp_wmi_getkeycode(struct input_dev
*dev
,
305 unsigned int scancode
, unsigned int *keycode
)
307 struct key_entry
*key
= hp_wmi_get_entry_by_scancode(scancode
);
309 if (key
&& key
->type
== KE_KEY
) {
310 *keycode
= key
->keycode
;
317 static int hp_wmi_setkeycode(struct input_dev
*dev
,
318 unsigned int scancode
, unsigned int keycode
)
320 struct key_entry
*key
;
321 unsigned int old_keycode
;
323 key
= hp_wmi_get_entry_by_scancode(scancode
);
324 if (key
&& key
->type
== KE_KEY
) {
325 old_keycode
= key
->keycode
;
326 key
->keycode
= keycode
;
327 set_bit(keycode
, dev
->keybit
);
328 if (!hp_wmi_get_entry_by_keycode(old_keycode
))
329 clear_bit(old_keycode
, dev
->keybit
);
336 static void hp_wmi_notify(u32 value
, void *context
)
338 struct acpi_buffer response
= { ACPI_ALLOCATE_BUFFER
, NULL
};
339 static struct key_entry
*key
;
340 union acpi_object
*obj
;
344 status
= wmi_get_event_data(value
, &response
);
345 if (status
!= AE_OK
) {
346 printk(KERN_INFO
"hp-wmi: bad event status 0x%x\n", status
);
350 obj
= (union acpi_object
*)response
.pointer
;
352 if (!obj
|| obj
->type
!= ACPI_TYPE_BUFFER
|| obj
->buffer
.length
!= 8) {
353 printk(KERN_INFO
"HP WMI: Unknown response received\n");
358 eventcode
= *((u8
*) obj
->buffer
.pointer
);
360 if (eventcode
== 0x4)
361 eventcode
= hp_wmi_perform_query(HPWMI_HOTKEY_QUERY
, 0,
363 key
= hp_wmi_get_entry_by_scancode(eventcode
);
367 input_report_key(hp_wmi_input_dev
,
369 input_sync(hp_wmi_input_dev
);
370 input_report_key(hp_wmi_input_dev
,
372 input_sync(hp_wmi_input_dev
);
375 } else if (eventcode
== 0x1) {
376 input_report_switch(hp_wmi_input_dev
, SW_DOCK
,
377 hp_wmi_dock_state());
378 input_report_switch(hp_wmi_input_dev
, SW_TABLET_MODE
,
379 hp_wmi_tablet_state());
380 input_sync(hp_wmi_input_dev
);
381 } else if (eventcode
== 0x5) {
383 rfkill_set_states(wifi_rfkill
,
384 hp_wmi_get_sw_state(HPWMI_WIFI
),
385 hp_wmi_get_hw_state(HPWMI_WIFI
));
386 if (bluetooth_rfkill
)
387 rfkill_set_states(bluetooth_rfkill
,
388 hp_wmi_get_sw_state(HPWMI_BLUETOOTH
),
389 hp_wmi_get_hw_state(HPWMI_BLUETOOTH
));
391 rfkill_set_states(wwan_rfkill
,
392 hp_wmi_get_sw_state(HPWMI_WWAN
),
393 hp_wmi_get_hw_state(HPWMI_WWAN
));
395 printk(KERN_INFO
"HP WMI: Unknown key pressed - %x\n",
399 static int __init
hp_wmi_input_setup(void)
401 struct key_entry
*key
;
404 hp_wmi_input_dev
= input_allocate_device();
406 hp_wmi_input_dev
->name
= "HP WMI hotkeys";
407 hp_wmi_input_dev
->phys
= "wmi/input0";
408 hp_wmi_input_dev
->id
.bustype
= BUS_HOST
;
409 hp_wmi_input_dev
->getkeycode
= hp_wmi_getkeycode
;
410 hp_wmi_input_dev
->setkeycode
= hp_wmi_setkeycode
;
412 for (key
= hp_wmi_keymap
; key
->type
!= KE_END
; key
++) {
415 set_bit(EV_KEY
, hp_wmi_input_dev
->evbit
);
416 set_bit(key
->keycode
, hp_wmi_input_dev
->keybit
);
421 set_bit(EV_SW
, hp_wmi_input_dev
->evbit
);
422 set_bit(SW_DOCK
, hp_wmi_input_dev
->swbit
);
423 set_bit(SW_TABLET_MODE
, hp_wmi_input_dev
->swbit
);
425 /* Set initial hardware state */
426 input_report_switch(hp_wmi_input_dev
, SW_DOCK
, hp_wmi_dock_state());
427 input_report_switch(hp_wmi_input_dev
, SW_TABLET_MODE
,
428 hp_wmi_tablet_state());
429 input_sync(hp_wmi_input_dev
);
431 err
= input_register_device(hp_wmi_input_dev
);
434 input_free_device(hp_wmi_input_dev
);
441 static void cleanup_sysfs(struct platform_device
*device
)
443 device_remove_file(&device
->dev
, &dev_attr_display
);
444 device_remove_file(&device
->dev
, &dev_attr_hddtemp
);
445 device_remove_file(&device
->dev
, &dev_attr_als
);
446 device_remove_file(&device
->dev
, &dev_attr_dock
);
447 device_remove_file(&device
->dev
, &dev_attr_tablet
);
450 static int __devinit
hp_wmi_bios_setup(struct platform_device
*device
)
453 int wireless
= hp_wmi_perform_query(HPWMI_WIRELESS_QUERY
, 0, 0);
455 err
= device_create_file(&device
->dev
, &dev_attr_display
);
457 goto add_sysfs_error
;
458 err
= device_create_file(&device
->dev
, &dev_attr_hddtemp
);
460 goto add_sysfs_error
;
461 err
= device_create_file(&device
->dev
, &dev_attr_als
);
463 goto add_sysfs_error
;
464 err
= device_create_file(&device
->dev
, &dev_attr_dock
);
466 goto add_sysfs_error
;
467 err
= device_create_file(&device
->dev
, &dev_attr_tablet
);
469 goto add_sysfs_error
;
471 if (wireless
& 0x1) {
472 wifi_rfkill
= rfkill_alloc("hp-wifi", &device
->dev
,
475 (void *) HPWMI_WIFI
);
476 rfkill_init_sw_state(wifi_rfkill
,
477 hp_wmi_get_sw_state(HPWMI_WIFI
));
478 rfkill_set_hw_state(wifi_rfkill
,
479 hp_wmi_get_hw_state(HPWMI_WIFI
));
480 err
= rfkill_register(wifi_rfkill
);
482 goto register_wifi_error
;
485 if (wireless
& 0x2) {
486 bluetooth_rfkill
= rfkill_alloc("hp-bluetooth", &device
->dev
,
487 RFKILL_TYPE_BLUETOOTH
,
489 (void *) HPWMI_BLUETOOTH
);
490 rfkill_init_sw_state(bluetooth_rfkill
,
491 hp_wmi_get_sw_state(HPWMI_BLUETOOTH
));
492 rfkill_set_hw_state(bluetooth_rfkill
,
493 hp_wmi_get_hw_state(HPWMI_BLUETOOTH
));
494 err
= rfkill_register(bluetooth_rfkill
);
496 goto register_bluetooth_error
;
499 if (wireless
& 0x4) {
500 wwan_rfkill
= rfkill_alloc("hp-wwan", &device
->dev
,
503 (void *) HPWMI_WWAN
);
504 rfkill_init_sw_state(wwan_rfkill
,
505 hp_wmi_get_sw_state(HPWMI_WWAN
));
506 rfkill_set_hw_state(wwan_rfkill
,
507 hp_wmi_get_hw_state(HPWMI_WWAN
));
508 err
= rfkill_register(wwan_rfkill
);
510 goto register_wwan_err
;
515 rfkill_destroy(wwan_rfkill
);
516 if (bluetooth_rfkill
)
517 rfkill_unregister(bluetooth_rfkill
);
518 register_bluetooth_error
:
519 rfkill_destroy(bluetooth_rfkill
);
521 rfkill_unregister(wifi_rfkill
);
523 rfkill_destroy(wifi_rfkill
);
525 cleanup_sysfs(device
);
529 static int __exit
hp_wmi_bios_remove(struct platform_device
*device
)
531 cleanup_sysfs(device
);
534 rfkill_unregister(wifi_rfkill
);
535 rfkill_destroy(wifi_rfkill
);
537 if (bluetooth_rfkill
) {
538 rfkill_unregister(bluetooth_rfkill
);
539 rfkill_destroy(bluetooth_rfkill
);
542 rfkill_unregister(wwan_rfkill
);
543 rfkill_destroy(wwan_rfkill
);
549 static int hp_wmi_resume_handler(struct device
*device
)
552 * Hardware state may have changed while suspended, so trigger
553 * input events for the current state. As this is a switch,
554 * the input layer will only actually pass it on if the state
557 if (hp_wmi_input_dev
) {
558 input_report_switch(hp_wmi_input_dev
, SW_DOCK
,
559 hp_wmi_dock_state());
560 input_report_switch(hp_wmi_input_dev
, SW_TABLET_MODE
,
561 hp_wmi_tablet_state());
562 input_sync(hp_wmi_input_dev
);
566 rfkill_set_states(wifi_rfkill
,
567 hp_wmi_get_sw_state(HPWMI_WIFI
),
568 hp_wmi_get_hw_state(HPWMI_WIFI
));
569 if (bluetooth_rfkill
)
570 rfkill_set_states(bluetooth_rfkill
,
571 hp_wmi_get_sw_state(HPWMI_BLUETOOTH
),
572 hp_wmi_get_hw_state(HPWMI_BLUETOOTH
));
574 rfkill_set_states(wwan_rfkill
,
575 hp_wmi_get_sw_state(HPWMI_WWAN
),
576 hp_wmi_get_hw_state(HPWMI_WWAN
));
581 static int __init
hp_wmi_init(void)
585 if (wmi_has_guid(HPWMI_EVENT_GUID
)) {
586 err
= wmi_install_notify_handler(HPWMI_EVENT_GUID
,
587 hp_wmi_notify
, NULL
);
588 if (ACPI_SUCCESS(err
))
589 hp_wmi_input_setup();
592 if (wmi_has_guid(HPWMI_BIOS_GUID
)) {
593 err
= platform_driver_register(&hp_wmi_driver
);
596 hp_wmi_platform_dev
= platform_device_alloc("hp-wmi", -1);
597 if (!hp_wmi_platform_dev
) {
598 platform_driver_unregister(&hp_wmi_driver
);
601 platform_device_add(hp_wmi_platform_dev
);
607 static void __exit
hp_wmi_exit(void)
609 if (wmi_has_guid(HPWMI_EVENT_GUID
)) {
610 wmi_remove_notify_handler(HPWMI_EVENT_GUID
);
611 input_unregister_device(hp_wmi_input_dev
);
613 if (hp_wmi_platform_dev
) {
614 platform_device_del(hp_wmi_platform_dev
);
615 platform_driver_unregister(&hp_wmi_driver
);
619 module_init(hp_wmi_init
);
620 module_exit(hp_wmi_exit
);