code style scripts/checkpatch.pl (linux-3.9-rc1) formatting
[linux-2.6.32.60-moxart.git] / drivers / platform / x86 / hp-wmi.c
blobdeb53b55a41fe64c9e8c69eaea0f729c357b0292
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/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
54 static int __init hp_wmi_bios_setup(struct platform_device *device);
55 static int __exit hp_wmi_bios_remove(struct platform_device *device);
56 static int hp_wmi_resume_handler(struct device *device);
58 struct bios_args {
59 u32 signature;
60 u32 command;
61 u32 commandtype;
62 u32 datasize;
63 u32 data;
66 struct bios_return {
67 u32 sigpass;
68 u32 return_code;
69 u32 value;
72 struct key_entry {
73 char type; /* See KE_* below */
74 u16 code;
75 u16 keycode;
78 enum { KE_KEY, KE_END };
80 static struct key_entry hp_wmi_keymap[] = {
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},
87 {KE_END, 0}
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 dev_pm_ops hp_wmi_pm_ops = {
98 .resume = hp_wmi_resume_handler,
99 .restore = hp_wmi_resume_handler,
102 static struct platform_driver hp_wmi_driver = {
103 .driver = {
104 .name = "hp-wmi",
105 .owner = THIS_MODULE,
106 .pm = &hp_wmi_pm_ops,
108 .probe = hp_wmi_bios_setup,
109 .remove = hp_wmi_bios_remove,
112 static int hp_wmi_perform_query(int query, int write, int value)
114 struct bios_return bios_return;
115 acpi_status status;
116 union acpi_object *obj;
117 struct bios_args args = {
118 .signature = 0x55434553,
119 .command = write ? 0x2 : 0x1,
120 .commandtype = query,
121 .datasize = write ? 0x4 : 0,
122 .data = value,
124 struct acpi_buffer input = { sizeof(struct bios_args), &args };
125 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
127 status = wmi_evaluate_method(HPWMI_BIOS_GUID, 0, 0x3, &input, &output);
129 obj = output.pointer;
131 if (!obj || obj->type != ACPI_TYPE_BUFFER)
132 return -EINVAL;
134 bios_return = *((struct bios_return *)obj->buffer.pointer);
135 if (bios_return.return_code > 0)
136 return bios_return.return_code * -1;
137 else
138 return bios_return.value;
141 static int hp_wmi_display_state(void)
143 return hp_wmi_perform_query(HPWMI_DISPLAY_QUERY, 0, 0);
146 static int hp_wmi_hddtemp_state(void)
148 return hp_wmi_perform_query(HPWMI_HDDTEMP_QUERY, 0, 0);
151 static int hp_wmi_als_state(void)
153 return hp_wmi_perform_query(HPWMI_ALS_QUERY, 0, 0);
156 static int hp_wmi_dock_state(void)
158 int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, 0, 0);
160 if (ret < 0)
161 return ret;
163 return ret & 0x1;
166 static int hp_wmi_tablet_state(void)
168 int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, 0, 0);
170 if (ret < 0)
171 return ret;
173 return (ret & 0x4) ? 1 : 0;
176 static int hp_wmi_set_block(void *data, bool blocked)
178 unsigned long b = (unsigned long) data;
179 int query = BIT(b + 8) | ((!blocked) << b);
181 return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, query);
184 static const struct rfkill_ops hp_wmi_rfkill_ops = {
185 .set_block = hp_wmi_set_block,
188 static bool hp_wmi_wifi_state(void)
190 int wireless = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, 0);
192 if (wireless & 0x100)
193 return false;
194 else
195 return true;
198 static bool hp_wmi_bluetooth_state(void)
200 int wireless = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, 0);
202 if (wireless & 0x10000)
203 return false;
204 else
205 return true;
208 static bool hp_wmi_wwan_state(void)
210 int wireless = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, 0);
212 if (wireless & 0x1000000)
213 return false;
214 else
215 return true;
218 static ssize_t show_display(struct device *dev, struct device_attribute *attr,
219 char *buf)
221 int value = hp_wmi_display_state();
222 if (value < 0)
223 return -EINVAL;
224 return sprintf(buf, "%d\n", value);
227 static ssize_t show_hddtemp(struct device *dev, struct device_attribute *attr,
228 char *buf)
230 int value = hp_wmi_hddtemp_state();
231 if (value < 0)
232 return -EINVAL;
233 return sprintf(buf, "%d\n", value);
236 static ssize_t show_als(struct device *dev, struct device_attribute *attr,
237 char *buf)
239 int value = hp_wmi_als_state();
240 if (value < 0)
241 return -EINVAL;
242 return sprintf(buf, "%d\n", value);
245 static ssize_t show_dock(struct device *dev, struct device_attribute *attr,
246 char *buf)
248 int value = hp_wmi_dock_state();
249 if (value < 0)
250 return -EINVAL;
251 return sprintf(buf, "%d\n", value);
254 static ssize_t show_tablet(struct device *dev, struct device_attribute *attr,
255 char *buf)
257 int value = hp_wmi_tablet_state();
258 if (value < 0)
259 return -EINVAL;
260 return sprintf(buf, "%d\n", value);
263 static ssize_t set_als(struct device *dev, struct device_attribute *attr,
264 const char *buf, size_t count)
266 u32 tmp = simple_strtoul(buf, NULL, 10);
267 hp_wmi_perform_query(HPWMI_ALS_QUERY, 1, tmp);
268 return count;
271 static DEVICE_ATTR(display, S_IRUGO, show_display, NULL);
272 static DEVICE_ATTR(hddtemp, S_IRUGO, show_hddtemp, NULL);
273 static DEVICE_ATTR(als, S_IRUGO | S_IWUSR, show_als, set_als);
274 static DEVICE_ATTR(dock, S_IRUGO, show_dock, NULL);
275 static DEVICE_ATTR(tablet, S_IRUGO, show_tablet, NULL);
277 static struct key_entry *hp_wmi_get_entry_by_scancode(int code)
279 struct key_entry *key;
281 for (key = hp_wmi_keymap; key->type != KE_END; key++)
282 if (code == key->code)
283 return key;
285 return NULL;
288 static struct key_entry *hp_wmi_get_entry_by_keycode(int keycode)
290 struct key_entry *key;
292 for (key = hp_wmi_keymap; key->type != KE_END; key++)
293 if (key->type == KE_KEY && keycode == key->keycode)
294 return key;
296 return NULL;
299 static int hp_wmi_getkeycode(struct input_dev *dev, int scancode, int *keycode)
301 struct key_entry *key = hp_wmi_get_entry_by_scancode(scancode);
303 if (key && key->type == KE_KEY) {
304 *keycode = key->keycode;
305 return 0;
308 return -EINVAL;
311 static int hp_wmi_setkeycode(struct input_dev *dev, int scancode, int keycode)
313 struct key_entry *key;
314 int old_keycode;
316 if (keycode < 0 || keycode > KEY_MAX)
317 return -EINVAL;
319 key = hp_wmi_get_entry_by_scancode(scancode);
320 if (key && key->type == KE_KEY) {
321 old_keycode = key->keycode;
322 key->keycode = keycode;
323 set_bit(keycode, dev->keybit);
324 if (!hp_wmi_get_entry_by_keycode(old_keycode))
325 clear_bit(old_keycode, dev->keybit);
326 return 0;
329 return -EINVAL;
332 static void hp_wmi_notify(u32 value, void *context)
334 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
335 static struct key_entry *key;
336 union acpi_object *obj;
337 acpi_status status;
339 status = wmi_get_event_data(value, &response);
340 if (status != AE_OK) {
341 printk(KERN_INFO "hp-wmi: bad event status 0x%x\n", status);
342 return;
345 obj = (union acpi_object *)response.pointer;
347 if (obj && obj->type == ACPI_TYPE_BUFFER && obj->buffer.length == 8) {
348 int eventcode = *((u8 *) obj->buffer.pointer);
349 if (eventcode == 0x4)
350 eventcode = hp_wmi_perform_query(HPWMI_HOTKEY_QUERY, 0,
352 key = hp_wmi_get_entry_by_scancode(eventcode);
353 if (key) {
354 switch (key->type) {
355 case KE_KEY:
356 input_report_key(hp_wmi_input_dev,
357 key->keycode, 1);
358 input_sync(hp_wmi_input_dev);
359 input_report_key(hp_wmi_input_dev,
360 key->keycode, 0);
361 input_sync(hp_wmi_input_dev);
362 break;
364 } else if (eventcode == 0x1) {
365 input_report_switch(hp_wmi_input_dev, SW_DOCK,
366 hp_wmi_dock_state());
367 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
368 hp_wmi_tablet_state());
369 input_sync(hp_wmi_input_dev);
370 } else if (eventcode == 0x5) {
371 if (wifi_rfkill)
372 rfkill_set_sw_state(wifi_rfkill,
373 hp_wmi_wifi_state());
374 if (bluetooth_rfkill)
375 rfkill_set_sw_state(bluetooth_rfkill,
376 hp_wmi_bluetooth_state());
377 if (wwan_rfkill)
378 rfkill_set_sw_state(wwan_rfkill,
379 hp_wmi_wwan_state());
380 } else
381 printk(KERN_INFO "HP WMI: Unknown key pressed - %x\n",
382 eventcode);
383 } else
384 printk(KERN_INFO "HP WMI: Unknown response received\n");
386 kfree(obj);
389 static int __init hp_wmi_input_setup(void)
391 struct key_entry *key;
392 int err;
394 hp_wmi_input_dev = input_allocate_device();
396 hp_wmi_input_dev->name = "HP WMI hotkeys";
397 hp_wmi_input_dev->phys = "wmi/input0";
398 hp_wmi_input_dev->id.bustype = BUS_HOST;
399 hp_wmi_input_dev->getkeycode = hp_wmi_getkeycode;
400 hp_wmi_input_dev->setkeycode = hp_wmi_setkeycode;
402 for (key = hp_wmi_keymap; key->type != KE_END; key++) {
403 switch (key->type) {
404 case KE_KEY:
405 set_bit(EV_KEY, hp_wmi_input_dev->evbit);
406 set_bit(key->keycode, hp_wmi_input_dev->keybit);
407 break;
411 set_bit(EV_SW, hp_wmi_input_dev->evbit);
412 set_bit(SW_DOCK, hp_wmi_input_dev->swbit);
413 set_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit);
415 /* Set initial hardware state */
416 input_report_switch(hp_wmi_input_dev, SW_DOCK, hp_wmi_dock_state());
417 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
418 hp_wmi_tablet_state());
419 input_sync(hp_wmi_input_dev);
421 err = input_register_device(hp_wmi_input_dev);
423 if (err) {
424 input_free_device(hp_wmi_input_dev);
425 return err;
428 return 0;
431 static void cleanup_sysfs(struct platform_device *device)
433 device_remove_file(&device->dev, &dev_attr_display);
434 device_remove_file(&device->dev, &dev_attr_hddtemp);
435 device_remove_file(&device->dev, &dev_attr_als);
436 device_remove_file(&device->dev, &dev_attr_dock);
437 device_remove_file(&device->dev, &dev_attr_tablet);
440 static int __init hp_wmi_bios_setup(struct platform_device *device)
442 int err;
443 int wireless = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, 0);
445 err = device_create_file(&device->dev, &dev_attr_display);
446 if (err)
447 goto add_sysfs_error;
448 err = device_create_file(&device->dev, &dev_attr_hddtemp);
449 if (err)
450 goto add_sysfs_error;
451 err = device_create_file(&device->dev, &dev_attr_als);
452 if (err)
453 goto add_sysfs_error;
454 err = device_create_file(&device->dev, &dev_attr_dock);
455 if (err)
456 goto add_sysfs_error;
457 err = device_create_file(&device->dev, &dev_attr_tablet);
458 if (err)
459 goto add_sysfs_error;
461 if (wireless & 0x1) {
462 wifi_rfkill = rfkill_alloc("hp-wifi", &device->dev,
463 RFKILL_TYPE_WLAN,
464 &hp_wmi_rfkill_ops,
465 (void *) 0);
466 err = rfkill_register(wifi_rfkill);
467 if (err)
468 goto register_wifi_error;
471 if (wireless & 0x2) {
472 bluetooth_rfkill = rfkill_alloc("hp-bluetooth", &device->dev,
473 RFKILL_TYPE_BLUETOOTH,
474 &hp_wmi_rfkill_ops,
475 (void *) 1);
476 err = rfkill_register(bluetooth_rfkill);
477 if (err)
478 goto register_bluetooth_error;
481 if (wireless & 0x4) {
482 wwan_rfkill = rfkill_alloc("hp-wwan", &device->dev,
483 RFKILL_TYPE_WWAN,
484 &hp_wmi_rfkill_ops,
485 (void *) 2);
486 err = rfkill_register(wwan_rfkill);
487 if (err)
488 goto register_wwan_err;
491 return 0;
492 register_wwan_err:
493 rfkill_destroy(wwan_rfkill);
494 if (bluetooth_rfkill)
495 rfkill_unregister(bluetooth_rfkill);
496 register_bluetooth_error:
497 rfkill_destroy(bluetooth_rfkill);
498 if (wifi_rfkill)
499 rfkill_unregister(wifi_rfkill);
500 register_wifi_error:
501 rfkill_destroy(wifi_rfkill);
502 add_sysfs_error:
503 cleanup_sysfs(device);
504 return err;
507 static int __exit hp_wmi_bios_remove(struct platform_device *device)
509 cleanup_sysfs(device);
511 if (wifi_rfkill) {
512 rfkill_unregister(wifi_rfkill);
513 rfkill_destroy(wifi_rfkill);
515 if (bluetooth_rfkill) {
516 rfkill_unregister(bluetooth_rfkill);
517 rfkill_destroy(bluetooth_rfkill);
519 if (wwan_rfkill) {
520 rfkill_unregister(wwan_rfkill);
521 rfkill_destroy(wwan_rfkill);
524 return 0;
527 static int hp_wmi_resume_handler(struct device *device)
530 * Hardware state may have changed while suspended, so trigger
531 * input events for the current state. As this is a switch,
532 * the input layer will only actually pass it on if the state
533 * changed.
535 if (hp_wmi_input_dev) {
536 input_report_switch(hp_wmi_input_dev, SW_DOCK,
537 hp_wmi_dock_state());
538 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
539 hp_wmi_tablet_state());
540 input_sync(hp_wmi_input_dev);
543 return 0;
546 static int __init hp_wmi_init(void)
548 int err;
550 if (wmi_has_guid(HPWMI_EVENT_GUID)) {
551 err = wmi_install_notify_handler(HPWMI_EVENT_GUID,
552 hp_wmi_notify, NULL);
553 if (!err)
554 hp_wmi_input_setup();
557 if (wmi_has_guid(HPWMI_BIOS_GUID)) {
558 err = platform_driver_register(&hp_wmi_driver);
559 if (err)
560 return 0;
561 hp_wmi_platform_dev = platform_device_alloc("hp-wmi", -1);
562 if (!hp_wmi_platform_dev) {
563 platform_driver_unregister(&hp_wmi_driver);
564 return 0;
566 platform_device_add(hp_wmi_platform_dev);
569 return 0;
572 static void __exit hp_wmi_exit(void)
574 if (wmi_has_guid(HPWMI_EVENT_GUID)) {
575 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
576 input_unregister_device(hp_wmi_input_dev);
578 if (hp_wmi_platform_dev) {
579 platform_device_del(hp_wmi_platform_dev);
580 platform_driver_unregister(&hp_wmi_driver);
584 module_init(hp_wmi_init);
585 module_exit(hp_wmi_exit);