x86 platform drivers: hp-wmi Add media key 0x20e8
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / hp-wmi.c
blobf3ae911cbab9a781c3bbec70a7fa53f1019e6470
1 /*
2 * HP WMI hotkeys
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
55 #define PREFIX "HP WMI: "
57 enum hp_wmi_radio {
58 HPWMI_WIFI = 0,
59 HPWMI_BLUETOOTH = 1,
60 HPWMI_WWAN = 2,
63 enum hp_wmi_event_ids {
64 HPWMI_DOCK_EVENT = 1,
65 HPWMI_BEZEL_BUTTON = 4,
66 HPWMI_WIRELESS = 5,
69 static int __devinit hp_wmi_bios_setup(struct platform_device *device);
70 static int __exit hp_wmi_bios_remove(struct platform_device *device);
71 static int hp_wmi_resume_handler(struct device *device);
73 struct bios_args {
74 u32 signature;
75 u32 command;
76 u32 commandtype;
77 u32 datasize;
78 u32 data;
81 struct bios_return {
82 u32 sigpass;
83 u32 return_code;
84 u32 value;
87 struct key_entry {
88 char type; /* See KE_* below */
89 u16 code;
90 u16 keycode;
93 enum { KE_KEY, KE_END };
95 static struct key_entry hp_wmi_keymap[] = {
96 {KE_KEY, 0x02, KEY_BRIGHTNESSUP},
97 {KE_KEY, 0x03, KEY_BRIGHTNESSDOWN},
98 {KE_KEY, 0x20e6, KEY_PROG1},
99 {KE_KEY, 0x20e8, KEY_MEDIA},
100 {KE_KEY, 0x2142, KEY_MEDIA},
101 {KE_KEY, 0x213b, KEY_INFO},
102 {KE_KEY, 0x2169, KEY_DIRECTION},
103 {KE_KEY, 0x231b, KEY_HELP},
104 {KE_END, 0}
107 static struct input_dev *hp_wmi_input_dev;
108 static struct platform_device *hp_wmi_platform_dev;
110 static struct rfkill *wifi_rfkill;
111 static struct rfkill *bluetooth_rfkill;
112 static struct rfkill *wwan_rfkill;
114 static const struct dev_pm_ops hp_wmi_pm_ops = {
115 .resume = hp_wmi_resume_handler,
116 .restore = hp_wmi_resume_handler,
119 static struct platform_driver hp_wmi_driver = {
120 .driver = {
121 .name = "hp-wmi",
122 .owner = THIS_MODULE,
123 .pm = &hp_wmi_pm_ops,
125 .probe = hp_wmi_bios_setup,
126 .remove = hp_wmi_bios_remove,
129 static int hp_wmi_perform_query(int query, int write, int value)
131 struct bios_return bios_return;
132 acpi_status status;
133 union acpi_object *obj;
134 struct bios_args args = {
135 .signature = 0x55434553,
136 .command = write ? 0x2 : 0x1,
137 .commandtype = query,
138 .datasize = write ? 0x4 : 0,
139 .data = value,
141 struct acpi_buffer input = { sizeof(struct bios_args), &args };
142 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
144 status = wmi_evaluate_method(HPWMI_BIOS_GUID, 0, 0x3, &input, &output);
146 obj = output.pointer;
148 if (!obj)
149 return -EINVAL;
150 else if (obj->type != ACPI_TYPE_BUFFER) {
151 kfree(obj);
152 return -EINVAL;
155 bios_return = *((struct bios_return *)obj->buffer.pointer);
156 kfree(obj);
157 if (bios_return.return_code > 0)
158 return bios_return.return_code * -1;
159 else
160 return bios_return.value;
163 static int hp_wmi_display_state(void)
165 return hp_wmi_perform_query(HPWMI_DISPLAY_QUERY, 0, 0);
168 static int hp_wmi_hddtemp_state(void)
170 return hp_wmi_perform_query(HPWMI_HDDTEMP_QUERY, 0, 0);
173 static int hp_wmi_als_state(void)
175 return hp_wmi_perform_query(HPWMI_ALS_QUERY, 0, 0);
178 static int hp_wmi_dock_state(void)
180 int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, 0, 0);
182 if (ret < 0)
183 return ret;
185 return ret & 0x1;
188 static int hp_wmi_tablet_state(void)
190 int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, 0, 0);
192 if (ret < 0)
193 return ret;
195 return (ret & 0x4) ? 1 : 0;
198 static int hp_wmi_set_block(void *data, bool blocked)
200 enum hp_wmi_radio r = (enum hp_wmi_radio) data;
201 int query = BIT(r + 8) | ((!blocked) << r);
203 return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, query);
206 static const struct rfkill_ops hp_wmi_rfkill_ops = {
207 .set_block = hp_wmi_set_block,
210 static bool hp_wmi_get_sw_state(enum hp_wmi_radio r)
212 int wireless = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, 0);
213 int mask = 0x200 << (r * 8);
215 if (wireless & mask)
216 return false;
217 else
218 return true;
221 static bool hp_wmi_get_hw_state(enum hp_wmi_radio r)
223 int wireless = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, 0);
224 int mask = 0x800 << (r * 8);
226 if (wireless & mask)
227 return false;
228 else
229 return true;
232 static ssize_t show_display(struct device *dev, struct device_attribute *attr,
233 char *buf)
235 int value = hp_wmi_display_state();
236 if (value < 0)
237 return -EINVAL;
238 return sprintf(buf, "%d\n", value);
241 static ssize_t show_hddtemp(struct device *dev, struct device_attribute *attr,
242 char *buf)
244 int value = hp_wmi_hddtemp_state();
245 if (value < 0)
246 return -EINVAL;
247 return sprintf(buf, "%d\n", value);
250 static ssize_t show_als(struct device *dev, struct device_attribute *attr,
251 char *buf)
253 int value = hp_wmi_als_state();
254 if (value < 0)
255 return -EINVAL;
256 return sprintf(buf, "%d\n", value);
259 static ssize_t show_dock(struct device *dev, struct device_attribute *attr,
260 char *buf)
262 int value = hp_wmi_dock_state();
263 if (value < 0)
264 return -EINVAL;
265 return sprintf(buf, "%d\n", value);
268 static ssize_t show_tablet(struct device *dev, struct device_attribute *attr,
269 char *buf)
271 int value = hp_wmi_tablet_state();
272 if (value < 0)
273 return -EINVAL;
274 return sprintf(buf, "%d\n", value);
277 static ssize_t set_als(struct device *dev, struct device_attribute *attr,
278 const char *buf, size_t count)
280 u32 tmp = simple_strtoul(buf, NULL, 10);
281 hp_wmi_perform_query(HPWMI_ALS_QUERY, 1, tmp);
282 return count;
285 static DEVICE_ATTR(display, S_IRUGO, show_display, NULL);
286 static DEVICE_ATTR(hddtemp, S_IRUGO, show_hddtemp, NULL);
287 static DEVICE_ATTR(als, S_IRUGO | S_IWUSR, show_als, set_als);
288 static DEVICE_ATTR(dock, S_IRUGO, show_dock, NULL);
289 static DEVICE_ATTR(tablet, S_IRUGO, show_tablet, NULL);
291 static struct key_entry *hp_wmi_get_entry_by_scancode(unsigned int code)
293 struct key_entry *key;
295 for (key = hp_wmi_keymap; key->type != KE_END; key++)
296 if (code == key->code)
297 return key;
299 return NULL;
302 static struct key_entry *hp_wmi_get_entry_by_keycode(unsigned int keycode)
304 struct key_entry *key;
306 for (key = hp_wmi_keymap; key->type != KE_END; key++)
307 if (key->type == KE_KEY && keycode == key->keycode)
308 return key;
310 return NULL;
313 static int hp_wmi_getkeycode(struct input_dev *dev,
314 unsigned int scancode, unsigned int *keycode)
316 struct key_entry *key = hp_wmi_get_entry_by_scancode(scancode);
318 if (key && key->type == KE_KEY) {
319 *keycode = key->keycode;
320 return 0;
323 return -EINVAL;
326 static int hp_wmi_setkeycode(struct input_dev *dev,
327 unsigned int scancode, unsigned int keycode)
329 struct key_entry *key;
330 unsigned int old_keycode;
332 key = hp_wmi_get_entry_by_scancode(scancode);
333 if (key && key->type == KE_KEY) {
334 old_keycode = key->keycode;
335 key->keycode = keycode;
336 set_bit(keycode, dev->keybit);
337 if (!hp_wmi_get_entry_by_keycode(old_keycode))
338 clear_bit(old_keycode, dev->keybit);
339 return 0;
342 return -EINVAL;
345 static void hp_wmi_notify(u32 value, void *context)
347 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
348 static struct key_entry *key;
349 union acpi_object *obj;
350 int eventcode, key_code;
351 acpi_status status;
353 status = wmi_get_event_data(value, &response);
354 if (status != AE_OK) {
355 printk(KERN_INFO PREFIX "bad event status 0x%x\n", status);
356 return;
359 obj = (union acpi_object *)response.pointer;
361 if (!obj || obj->type != ACPI_TYPE_BUFFER || obj->buffer.length != 8) {
362 printk(KERN_INFO PREFIX "Unknown response received\n");
363 kfree(obj);
364 return;
367 eventcode = *((u8 *) obj->buffer.pointer);
368 kfree(obj);
369 switch (eventcode) {
370 case HPWMI_DOCK_EVENT:
371 input_report_switch(hp_wmi_input_dev, SW_DOCK,
372 hp_wmi_dock_state());
373 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
374 hp_wmi_tablet_state());
375 input_sync(hp_wmi_input_dev);
376 break;
377 case HPWMI_BEZEL_BUTTON:
378 key_code = hp_wmi_perform_query(HPWMI_HOTKEY_QUERY, 0,
380 key = hp_wmi_get_entry_by_scancode(key_code);
381 if (key) {
382 switch (key->type) {
383 case KE_KEY:
384 input_report_key(hp_wmi_input_dev,
385 key->keycode, 1);
386 input_sync(hp_wmi_input_dev);
387 input_report_key(hp_wmi_input_dev,
388 key->keycode, 0);
389 input_sync(hp_wmi_input_dev);
390 break;
392 } else
393 printk(KERN_INFO PREFIX "Unknown key code - 0x%x\n",
394 key_code);
395 break;
396 case HPWMI_WIRELESS:
397 if (wifi_rfkill)
398 rfkill_set_states(wifi_rfkill,
399 hp_wmi_get_sw_state(HPWMI_WIFI),
400 hp_wmi_get_hw_state(HPWMI_WIFI));
401 if (bluetooth_rfkill)
402 rfkill_set_states(bluetooth_rfkill,
403 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
404 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
405 if (wwan_rfkill)
406 rfkill_set_states(wwan_rfkill,
407 hp_wmi_get_sw_state(HPWMI_WWAN),
408 hp_wmi_get_hw_state(HPWMI_WWAN));
409 break;
410 default:
411 printk(KERN_INFO PREFIX "Unknown eventcode - %d\n",
412 eventcode);
413 break;
417 static int __init hp_wmi_input_setup(void)
419 struct key_entry *key;
420 int err;
422 hp_wmi_input_dev = input_allocate_device();
424 hp_wmi_input_dev->name = "HP WMI hotkeys";
425 hp_wmi_input_dev->phys = "wmi/input0";
426 hp_wmi_input_dev->id.bustype = BUS_HOST;
427 hp_wmi_input_dev->getkeycode = hp_wmi_getkeycode;
428 hp_wmi_input_dev->setkeycode = hp_wmi_setkeycode;
430 for (key = hp_wmi_keymap; key->type != KE_END; key++) {
431 switch (key->type) {
432 case KE_KEY:
433 set_bit(EV_KEY, hp_wmi_input_dev->evbit);
434 set_bit(key->keycode, hp_wmi_input_dev->keybit);
435 break;
439 set_bit(EV_SW, hp_wmi_input_dev->evbit);
440 set_bit(SW_DOCK, hp_wmi_input_dev->swbit);
441 set_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit);
443 /* Set initial hardware state */
444 input_report_switch(hp_wmi_input_dev, SW_DOCK, hp_wmi_dock_state());
445 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
446 hp_wmi_tablet_state());
447 input_sync(hp_wmi_input_dev);
449 err = input_register_device(hp_wmi_input_dev);
451 if (err) {
452 input_free_device(hp_wmi_input_dev);
453 return err;
456 return 0;
459 static void cleanup_sysfs(struct platform_device *device)
461 device_remove_file(&device->dev, &dev_attr_display);
462 device_remove_file(&device->dev, &dev_attr_hddtemp);
463 device_remove_file(&device->dev, &dev_attr_als);
464 device_remove_file(&device->dev, &dev_attr_dock);
465 device_remove_file(&device->dev, &dev_attr_tablet);
468 static int __devinit hp_wmi_bios_setup(struct platform_device *device)
470 int err;
471 int wireless = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, 0);
473 err = device_create_file(&device->dev, &dev_attr_display);
474 if (err)
475 goto add_sysfs_error;
476 err = device_create_file(&device->dev, &dev_attr_hddtemp);
477 if (err)
478 goto add_sysfs_error;
479 err = device_create_file(&device->dev, &dev_attr_als);
480 if (err)
481 goto add_sysfs_error;
482 err = device_create_file(&device->dev, &dev_attr_dock);
483 if (err)
484 goto add_sysfs_error;
485 err = device_create_file(&device->dev, &dev_attr_tablet);
486 if (err)
487 goto add_sysfs_error;
489 if (wireless & 0x1) {
490 wifi_rfkill = rfkill_alloc("hp-wifi", &device->dev,
491 RFKILL_TYPE_WLAN,
492 &hp_wmi_rfkill_ops,
493 (void *) HPWMI_WIFI);
494 rfkill_init_sw_state(wifi_rfkill,
495 hp_wmi_get_sw_state(HPWMI_WIFI));
496 rfkill_set_hw_state(wifi_rfkill,
497 hp_wmi_get_hw_state(HPWMI_WIFI));
498 err = rfkill_register(wifi_rfkill);
499 if (err)
500 goto register_wifi_error;
503 if (wireless & 0x2) {
504 bluetooth_rfkill = rfkill_alloc("hp-bluetooth", &device->dev,
505 RFKILL_TYPE_BLUETOOTH,
506 &hp_wmi_rfkill_ops,
507 (void *) HPWMI_BLUETOOTH);
508 rfkill_init_sw_state(bluetooth_rfkill,
509 hp_wmi_get_sw_state(HPWMI_BLUETOOTH));
510 rfkill_set_hw_state(bluetooth_rfkill,
511 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
512 err = rfkill_register(bluetooth_rfkill);
513 if (err)
514 goto register_bluetooth_error;
517 if (wireless & 0x4) {
518 wwan_rfkill = rfkill_alloc("hp-wwan", &device->dev,
519 RFKILL_TYPE_WWAN,
520 &hp_wmi_rfkill_ops,
521 (void *) HPWMI_WWAN);
522 rfkill_init_sw_state(wwan_rfkill,
523 hp_wmi_get_sw_state(HPWMI_WWAN));
524 rfkill_set_hw_state(wwan_rfkill,
525 hp_wmi_get_hw_state(HPWMI_WWAN));
526 err = rfkill_register(wwan_rfkill);
527 if (err)
528 goto register_wwan_err;
531 return 0;
532 register_wwan_err:
533 rfkill_destroy(wwan_rfkill);
534 if (bluetooth_rfkill)
535 rfkill_unregister(bluetooth_rfkill);
536 register_bluetooth_error:
537 rfkill_destroy(bluetooth_rfkill);
538 if (wifi_rfkill)
539 rfkill_unregister(wifi_rfkill);
540 register_wifi_error:
541 rfkill_destroy(wifi_rfkill);
542 add_sysfs_error:
543 cleanup_sysfs(device);
544 return err;
547 static int __exit hp_wmi_bios_remove(struct platform_device *device)
549 cleanup_sysfs(device);
551 if (wifi_rfkill) {
552 rfkill_unregister(wifi_rfkill);
553 rfkill_destroy(wifi_rfkill);
555 if (bluetooth_rfkill) {
556 rfkill_unregister(bluetooth_rfkill);
557 rfkill_destroy(bluetooth_rfkill);
559 if (wwan_rfkill) {
560 rfkill_unregister(wwan_rfkill);
561 rfkill_destroy(wwan_rfkill);
564 return 0;
567 static int hp_wmi_resume_handler(struct device *device)
570 * Hardware state may have changed while suspended, so trigger
571 * input events for the current state. As this is a switch,
572 * the input layer will only actually pass it on if the state
573 * changed.
575 if (hp_wmi_input_dev) {
576 input_report_switch(hp_wmi_input_dev, SW_DOCK,
577 hp_wmi_dock_state());
578 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
579 hp_wmi_tablet_state());
580 input_sync(hp_wmi_input_dev);
583 if (wifi_rfkill)
584 rfkill_set_states(wifi_rfkill,
585 hp_wmi_get_sw_state(HPWMI_WIFI),
586 hp_wmi_get_hw_state(HPWMI_WIFI));
587 if (bluetooth_rfkill)
588 rfkill_set_states(bluetooth_rfkill,
589 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
590 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
591 if (wwan_rfkill)
592 rfkill_set_states(wwan_rfkill,
593 hp_wmi_get_sw_state(HPWMI_WWAN),
594 hp_wmi_get_hw_state(HPWMI_WWAN));
596 return 0;
599 static int __init hp_wmi_init(void)
601 int err;
603 if (wmi_has_guid(HPWMI_EVENT_GUID)) {
604 err = wmi_install_notify_handler(HPWMI_EVENT_GUID,
605 hp_wmi_notify, NULL);
606 if (ACPI_SUCCESS(err))
607 hp_wmi_input_setup();
610 if (wmi_has_guid(HPWMI_BIOS_GUID)) {
611 err = platform_driver_register(&hp_wmi_driver);
612 if (err)
613 return 0;
614 hp_wmi_platform_dev = platform_device_alloc("hp-wmi", -1);
615 if (!hp_wmi_platform_dev) {
616 platform_driver_unregister(&hp_wmi_driver);
617 return 0;
619 platform_device_add(hp_wmi_platform_dev);
622 return 0;
625 static void __exit hp_wmi_exit(void)
627 if (wmi_has_guid(HPWMI_EVENT_GUID)) {
628 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
629 input_unregister_device(hp_wmi_input_dev);
631 if (hp_wmi_platform_dev) {
632 platform_device_del(hp_wmi_platform_dev);
633 platform_driver_unregister(&hp_wmi_driver);
637 module_init(hp_wmi_init);
638 module_exit(hp_wmi_exit);