sgi-xp: isolate xpc_vars_part structure to sn2 only
[linux-2.6/kvm.git] / drivers / misc / hp-wmi.c
blob1dbcbcb323a28e9e9679c3026982b74d7338fd9a
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_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);
56 struct bios_args {
57 u32 signature;
58 u32 command;
59 u32 commandtype;
60 u32 datasize;
61 u32 data;
64 struct bios_return {
65 u32 sigpass;
66 u32 return_code;
67 u32 value;
70 struct key_entry {
71 char type; /* See KE_* below */
72 u8 code;
73 u16 keycode;
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},
83 {KE_END, 0}
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 = {
94 .driver = {
95 .name = "hp-wmi",
96 .owner = THIS_MODULE,
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;
105 acpi_status status;
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,
112 .data = value,
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)
122 return -EINVAL;
124 bios_return = *((struct bios_return *)obj->buffer.pointer);
125 if (bios_return.return_code > 0)
126 return bios_return.return_code * -1;
127 else
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)
153 if (state)
154 return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, 0x101);
155 else
156 return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, 0x100);
159 static int hp_wmi_bluetooth_set(void *data, enum rfkill_state state)
161 if (state)
162 return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, 0x202);
163 else
164 return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, 0x200);
167 static int hp_wmi_wwan_set(void *data, enum rfkill_state state)
169 if (state)
170 return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, 0x404);
171 else
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)
180 return 1;
181 else
182 return 0;
185 static int hp_wmi_bluetooth_state(void)
187 int wireless = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, 0);
189 if (wireless & 0x10000)
190 return 1;
191 else
192 return 0;
195 static int hp_wmi_wwan_state(void)
197 int wireless = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, 0);
199 if (wireless & 0x1000000)
200 return 1;
201 else
202 return 0;
205 static ssize_t show_display(struct device *dev, struct device_attribute *attr,
206 char *buf)
208 int value = hp_wmi_display_state();
209 if (value < 0)
210 return -EINVAL;
211 return sprintf(buf, "%d\n", value);
214 static ssize_t show_hddtemp(struct device *dev, struct device_attribute *attr,
215 char *buf)
217 int value = hp_wmi_hddtemp_state();
218 if (value < 0)
219 return -EINVAL;
220 return sprintf(buf, "%d\n", value);
223 static ssize_t show_als(struct device *dev, struct device_attribute *attr,
224 char *buf)
226 int value = hp_wmi_als_state();
227 if (value < 0)
228 return -EINVAL;
229 return sprintf(buf, "%d\n", value);
232 static ssize_t show_dock(struct device *dev, struct device_attribute *attr,
233 char *buf)
235 int value = hp_wmi_dock_state();
236 if (value < 0)
237 return -EINVAL;
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);
246 return count;
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)
260 return key;
262 return NULL;
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)
271 return key;
273 return NULL;
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;
282 return 0;
285 return -EINVAL;
288 static int hp_wmi_setkeycode(struct input_dev *dev, int scancode, int keycode)
290 struct key_entry *key;
291 int old_keycode;
293 if (keycode < 0 || keycode > KEY_MAX)
294 return -EINVAL;
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);
303 return 0;
306 return -EINVAL;
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);
322 if (key) {
323 switch (key->type) {
324 case KE_KEY:
325 input_report_key(hp_wmi_input_dev,
326 key->keycode, 1);
327 input_sync(hp_wmi_input_dev);
328 input_report_key(hp_wmi_input_dev,
329 key->keycode, 0);
330 input_sync(hp_wmi_input_dev);
331 break;
332 case KE_SW:
333 input_report_switch(hp_wmi_input_dev,
334 key->keycode,
335 hp_wmi_dock_state());
336 input_sync(hp_wmi_input_dev);
337 break;
339 } else if (eventcode == 0x5) {
340 if (wifi_rfkill)
341 wifi_rfkill->state = hp_wmi_wifi_state();
342 if (bluetooth_rfkill)
343 bluetooth_rfkill->state =
344 hp_wmi_bluetooth_state();
345 if (wwan_rfkill)
346 wwan_rfkill->state = hp_wmi_wwan_state();
347 } else
348 printk(KERN_INFO "HP WMI: Unknown key pressed - %x\n",
349 eventcode);
350 } else
351 printk(KERN_INFO "HP WMI: Unknown response received\n");
354 static int __init hp_wmi_input_setup(void)
356 struct key_entry *key;
357 int err;
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++) {
368 switch (key->type) {
369 case KE_KEY:
370 set_bit(EV_KEY, hp_wmi_input_dev->evbit);
371 set_bit(key->keycode, hp_wmi_input_dev->keybit);
372 break;
373 case KE_SW:
374 set_bit(EV_SW, hp_wmi_input_dev->evbit);
375 set_bit(key->keycode, hp_wmi_input_dev->swbit);
376 break;
380 err = input_register_device(hp_wmi_input_dev);
382 if (err) {
383 input_free_device(hp_wmi_input_dev);
384 return err;
387 return 0;
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)
400 int err;
402 err = device_create_file(&device->dev, &dev_attr_display);
403 if (err)
404 goto add_sysfs_error;
405 err = device_create_file(&device->dev, &dev_attr_hddtemp);
406 if (err)
407 goto add_sysfs_error;
408 err = device_create_file(&device->dev, &dev_attr_als);
409 if (err)
410 goto add_sysfs_error;
411 err = device_create_file(&device->dev, &dev_attr_dock);
412 if (err)
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);
438 return 0;
439 add_sysfs_error:
440 cleanup_sysfs(device);
441 return err;
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);
452 return 0;
455 static int __init hp_wmi_init(void)
457 int err;
459 if (wmi_has_guid(HPWMI_EVENT_GUID)) {
460 err = wmi_install_notify_handler(HPWMI_EVENT_GUID,
461 hp_wmi_notify, NULL);
462 if (!err)
463 hp_wmi_input_setup();
466 if (wmi_has_guid(HPWMI_BIOS_GUID)) {
467 err = platform_driver_register(&hp_wmi_driver);
468 if (err)
469 return 0;
470 hp_wmi_platform_dev = platform_device_alloc("hp-wmi", -1);
471 if (!hp_wmi_platform_dev) {
472 platform_driver_unregister(&hp_wmi_driver);
473 return 0;
475 platform_device_add(hp_wmi_platform_dev);
478 return 0;
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);