Merge branch 'core/xen' into x86/xen
[safe/jmp/linux-2.6] / drivers / misc / hp-wmi.c
1 /*
2  * HP WMI hotkeys
3  *
4  * Copyright (C) 2008 Red Hat <mjg@redhat.com>
5  *
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>
10  *
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.
15  *
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.
20  *
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
24  */
25
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>
36
37 MODULE_AUTHOR("Matthew Garrett <mjg59@srcf.ucam.org>");
38 MODULE_DESCRIPTION("HP laptop WMI hotkeys driver");
39 MODULE_LICENSE("GPL");
40
41 MODULE_ALIAS("wmi:95F24279-4D7B-4334-9387-ACCDC67EF61C");
42 MODULE_ALIAS("wmi:5FB7F034-2C63-45e9-BE91-3D44E2C707E4");
43
44 #define HPWMI_EVENT_GUID "95F24279-4D7B-4334-9387-ACCDC67EF61C"
45 #define HPWMI_BIOS_GUID "5FB7F034-2C63-45e9-BE91-3D44E2C707E4"
46
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
52 #define HPWMI_HOTKEY_QUERY 0xc
53
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
57 struct bios_args {
58         u32 signature;
59         u32 command;
60         u32 commandtype;
61         u32 datasize;
62         u32 data;
63 };
64
65 struct bios_return {
66         u32 sigpass;
67         u32 return_code;
68         u32 value;
69 };
70
71 struct key_entry {
72         char type;              /* See KE_* below */
73         u16 code;
74         u16 keycode;
75 };
76
77 enum { KE_KEY, KE_SW, KE_END };
78
79 static struct key_entry hp_wmi_keymap[] = {
80         {KE_SW, 0x01, SW_DOCK},
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, 0x231b, KEY_HELP},
86         {KE_END, 0}
87 };
88
89 static struct input_dev *hp_wmi_input_dev;
90 static struct platform_device *hp_wmi_platform_dev;
91
92 static struct rfkill *wifi_rfkill;
93 static struct rfkill *bluetooth_rfkill;
94 static struct rfkill *wwan_rfkill;
95
96 static struct platform_driver hp_wmi_driver = {
97         .driver = {
98                    .name = "hp-wmi",
99                    .owner = THIS_MODULE,
100         },
101         .probe = hp_wmi_bios_setup,
102         .remove = hp_wmi_bios_remove,
103 };
104
105 static int hp_wmi_perform_query(int query, int write, int value)
106 {
107         struct bios_return bios_return;
108         acpi_status status;
109         union acpi_object *obj;
110         struct bios_args args = {
111                 .signature = 0x55434553,
112                 .command = write ? 0x2 : 0x1,
113                 .commandtype = query,
114                 .datasize = write ? 0x4 : 0,
115                 .data = value,
116         };
117         struct acpi_buffer input = { sizeof(struct bios_args), &args };
118         struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
119
120         status = wmi_evaluate_method(HPWMI_BIOS_GUID, 0, 0x3, &input, &output);
121
122         obj = output.pointer;
123
124         if (!obj || obj->type != ACPI_TYPE_BUFFER)
125                 return -EINVAL;
126
127         bios_return = *((struct bios_return *)obj->buffer.pointer);
128         if (bios_return.return_code > 0)
129                 return bios_return.return_code * -1;
130         else
131                 return bios_return.value;
132 }
133
134 static int hp_wmi_display_state(void)
135 {
136         return hp_wmi_perform_query(HPWMI_DISPLAY_QUERY, 0, 0);
137 }
138
139 static int hp_wmi_hddtemp_state(void)
140 {
141         return hp_wmi_perform_query(HPWMI_HDDTEMP_QUERY, 0, 0);
142 }
143
144 static int hp_wmi_als_state(void)
145 {
146         return hp_wmi_perform_query(HPWMI_ALS_QUERY, 0, 0);
147 }
148
149 static int hp_wmi_dock_state(void)
150 {
151         return hp_wmi_perform_query(HPWMI_DOCK_QUERY, 0, 0);
152 }
153
154 static int hp_wmi_wifi_set(void *data, enum rfkill_state state)
155 {
156         if (state)
157                 return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, 0x101);
158         else
159                 return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, 0x100);
160 }
161
162 static int hp_wmi_bluetooth_set(void *data, enum rfkill_state state)
163 {
164         if (state)
165                 return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, 0x202);
166         else
167                 return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, 0x200);
168 }
169
170 static int hp_wmi_wwan_set(void *data, enum rfkill_state state)
171 {
172         if (state)
173                 return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, 0x404);
174         else
175                 return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, 0x400);
176 }
177
178 static int hp_wmi_wifi_state(void)
179 {
180         int wireless = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, 0);
181
182         if (wireless & 0x100)
183                 return RFKILL_STATE_UNBLOCKED;
184         else
185                 return RFKILL_STATE_SOFT_BLOCKED;
186 }
187
188 static int hp_wmi_bluetooth_state(void)
189 {
190         int wireless = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, 0);
191
192         if (wireless & 0x10000)
193                 return RFKILL_STATE_UNBLOCKED;
194         else
195                 return RFKILL_STATE_SOFT_BLOCKED;
196 }
197
198 static int hp_wmi_wwan_state(void)
199 {
200         int wireless = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, 0);
201
202         if (wireless & 0x1000000)
203                 return RFKILL_STATE_UNBLOCKED;
204         else
205                 return RFKILL_STATE_SOFT_BLOCKED;
206 }
207
208 static ssize_t show_display(struct device *dev, struct device_attribute *attr,
209                             char *buf)
210 {
211         int value = hp_wmi_display_state();
212         if (value < 0)
213                 return -EINVAL;
214         return sprintf(buf, "%d\n", value);
215 }
216
217 static ssize_t show_hddtemp(struct device *dev, struct device_attribute *attr,
218                             char *buf)
219 {
220         int value = hp_wmi_hddtemp_state();
221         if (value < 0)
222                 return -EINVAL;
223         return sprintf(buf, "%d\n", value);
224 }
225
226 static ssize_t show_als(struct device *dev, struct device_attribute *attr,
227                         char *buf)
228 {
229         int value = hp_wmi_als_state();
230         if (value < 0)
231                 return -EINVAL;
232         return sprintf(buf, "%d\n", value);
233 }
234
235 static ssize_t show_dock(struct device *dev, struct device_attribute *attr,
236                          char *buf)
237 {
238         int value = hp_wmi_dock_state();
239         if (value < 0)
240                 return -EINVAL;
241         return sprintf(buf, "%d\n", value);
242 }
243
244 static ssize_t set_als(struct device *dev, struct device_attribute *attr,
245                        const char *buf, size_t count)
246 {
247         u32 tmp = simple_strtoul(buf, NULL, 10);
248         hp_wmi_perform_query(HPWMI_ALS_QUERY, 1, tmp);
249         return count;
250 }
251
252 static DEVICE_ATTR(display, S_IRUGO, show_display, NULL);
253 static DEVICE_ATTR(hddtemp, S_IRUGO, show_hddtemp, NULL);
254 static DEVICE_ATTR(als, S_IRUGO | S_IWUSR, show_als, set_als);
255 static DEVICE_ATTR(dock, S_IRUGO, show_dock, NULL);
256
257 static struct key_entry *hp_wmi_get_entry_by_scancode(int code)
258 {
259         struct key_entry *key;
260
261         for (key = hp_wmi_keymap; key->type != KE_END; key++)
262                 if (code == key->code)
263                         return key;
264
265         return NULL;
266 }
267
268 static struct key_entry *hp_wmi_get_entry_by_keycode(int keycode)
269 {
270         struct key_entry *key;
271
272         for (key = hp_wmi_keymap; key->type != KE_END; key++)
273                 if (key->type == KE_KEY && keycode == key->keycode)
274                         return key;
275
276         return NULL;
277 }
278
279 static int hp_wmi_getkeycode(struct input_dev *dev, int scancode, int *keycode)
280 {
281         struct key_entry *key = hp_wmi_get_entry_by_scancode(scancode);
282
283         if (key && key->type == KE_KEY) {
284                 *keycode = key->keycode;
285                 return 0;
286         }
287
288         return -EINVAL;
289 }
290
291 static int hp_wmi_setkeycode(struct input_dev *dev, int scancode, int keycode)
292 {
293         struct key_entry *key;
294         int old_keycode;
295
296         if (keycode < 0 || keycode > KEY_MAX)
297                 return -EINVAL;
298
299         key = hp_wmi_get_entry_by_scancode(scancode);
300         if (key && key->type == KE_KEY) {
301                 old_keycode = key->keycode;
302                 key->keycode = keycode;
303                 set_bit(keycode, dev->keybit);
304                 if (!hp_wmi_get_entry_by_keycode(old_keycode))
305                         clear_bit(old_keycode, dev->keybit);
306                 return 0;
307         }
308
309         return -EINVAL;
310 }
311
312 void hp_wmi_notify(u32 value, void *context)
313 {
314         struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
315         static struct key_entry *key;
316         union acpi_object *obj;
317
318         wmi_get_event_data(value, &response);
319
320         obj = (union acpi_object *)response.pointer;
321
322         if (obj && obj->type == ACPI_TYPE_BUFFER && obj->buffer.length == 8) {
323                 int eventcode = *((u8 *) obj->buffer.pointer);
324                 if (eventcode == 0x4)
325                         eventcode = hp_wmi_perform_query(HPWMI_HOTKEY_QUERY, 0,
326                                                          0);
327                 key = hp_wmi_get_entry_by_scancode(eventcode);
328                 if (key) {
329                         switch (key->type) {
330                         case KE_KEY:
331                                 input_report_key(hp_wmi_input_dev,
332                                                  key->keycode, 1);
333                                 input_sync(hp_wmi_input_dev);
334                                 input_report_key(hp_wmi_input_dev,
335                                                  key->keycode, 0);
336                                 input_sync(hp_wmi_input_dev);
337                                 break;
338                         case KE_SW:
339                                 input_report_switch(hp_wmi_input_dev,
340                                                     key->keycode,
341                                                     hp_wmi_dock_state());
342                                 input_sync(hp_wmi_input_dev);
343                                 break;
344                         }
345                 } else if (eventcode == 0x5) {
346                         if (wifi_rfkill)
347                                 rfkill_force_state(wifi_rfkill,
348                                                    hp_wmi_wifi_state());
349                         if (bluetooth_rfkill)
350                                 rfkill_force_state(bluetooth_rfkill,
351                                                    hp_wmi_bluetooth_state());
352                         if (wwan_rfkill)
353                                 rfkill_force_state(wwan_rfkill,
354                                                    hp_wmi_wwan_state());
355                 } else
356                         printk(KERN_INFO "HP WMI: Unknown key pressed - %x\n",
357                                eventcode);
358         } else
359                 printk(KERN_INFO "HP WMI: Unknown response received\n");
360 }
361
362 static int __init hp_wmi_input_setup(void)
363 {
364         struct key_entry *key;
365         int err;
366
367         hp_wmi_input_dev = input_allocate_device();
368
369         hp_wmi_input_dev->name = "HP WMI hotkeys";
370         hp_wmi_input_dev->phys = "wmi/input0";
371         hp_wmi_input_dev->id.bustype = BUS_HOST;
372         hp_wmi_input_dev->getkeycode = hp_wmi_getkeycode;
373         hp_wmi_input_dev->setkeycode = hp_wmi_setkeycode;
374
375         for (key = hp_wmi_keymap; key->type != KE_END; key++) {
376                 switch (key->type) {
377                 case KE_KEY:
378                         set_bit(EV_KEY, hp_wmi_input_dev->evbit);
379                         set_bit(key->keycode, hp_wmi_input_dev->keybit);
380                         break;
381                 case KE_SW:
382                         set_bit(EV_SW, hp_wmi_input_dev->evbit);
383                         set_bit(key->keycode, hp_wmi_input_dev->swbit);
384                         break;
385                 }
386         }
387
388         err = input_register_device(hp_wmi_input_dev);
389
390         if (err) {
391                 input_free_device(hp_wmi_input_dev);
392                 return err;
393         }
394
395         return 0;
396 }
397
398 static void cleanup_sysfs(struct platform_device *device)
399 {
400         device_remove_file(&device->dev, &dev_attr_display);
401         device_remove_file(&device->dev, &dev_attr_hddtemp);
402         device_remove_file(&device->dev, &dev_attr_als);
403         device_remove_file(&device->dev, &dev_attr_dock);
404 }
405
406 static int __init hp_wmi_bios_setup(struct platform_device *device)
407 {
408         int err;
409         int wireless = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, 0);
410
411         err = device_create_file(&device->dev, &dev_attr_display);
412         if (err)
413                 goto add_sysfs_error;
414         err = device_create_file(&device->dev, &dev_attr_hddtemp);
415         if (err)
416                 goto add_sysfs_error;
417         err = device_create_file(&device->dev, &dev_attr_als);
418         if (err)
419                 goto add_sysfs_error;
420         err = device_create_file(&device->dev, &dev_attr_dock);
421         if (err)
422                 goto add_sysfs_error;
423
424         if (wireless & 0x1) {
425                 wifi_rfkill = rfkill_allocate(&device->dev, RFKILL_TYPE_WLAN);
426                 wifi_rfkill->name = "hp-wifi";
427                 wifi_rfkill->state = hp_wmi_wifi_state();
428                 wifi_rfkill->toggle_radio = hp_wmi_wifi_set;
429                 wifi_rfkill->user_claim_unsupported = 1;
430                 rfkill_register(wifi_rfkill);
431         }
432
433         if (wireless & 0x2) {
434                 bluetooth_rfkill = rfkill_allocate(&device->dev,
435                                                    RFKILL_TYPE_BLUETOOTH);
436                 bluetooth_rfkill->name = "hp-bluetooth";
437                 bluetooth_rfkill->state = hp_wmi_bluetooth_state();
438                 bluetooth_rfkill->toggle_radio = hp_wmi_bluetooth_set;
439                 bluetooth_rfkill->user_claim_unsupported = 1;
440                 rfkill_register(bluetooth_rfkill);
441         }
442
443         if (wireless & 0x4) {
444                 wwan_rfkill = rfkill_allocate(&device->dev, RFKILL_TYPE_WWAN);
445                 wwan_rfkill->name = "hp-wwan";
446                 wwan_rfkill->state = hp_wmi_wwan_state();
447                 wwan_rfkill->toggle_radio = hp_wmi_wwan_set;
448                 wwan_rfkill->user_claim_unsupported = 1;
449                 rfkill_register(wwan_rfkill);
450         }
451
452         return 0;
453 add_sysfs_error:
454         cleanup_sysfs(device);
455         return err;
456 }
457
458 static int __exit hp_wmi_bios_remove(struct platform_device *device)
459 {
460         cleanup_sysfs(device);
461
462         if (wifi_rfkill)
463                 rfkill_unregister(wifi_rfkill);
464         if (bluetooth_rfkill)
465                 rfkill_unregister(bluetooth_rfkill);
466         if (wwan_rfkill)
467                 rfkill_unregister(wwan_rfkill);
468
469         return 0;
470 }
471
472 static int __init hp_wmi_init(void)
473 {
474         int err;
475
476         if (wmi_has_guid(HPWMI_EVENT_GUID)) {
477                 err = wmi_install_notify_handler(HPWMI_EVENT_GUID,
478                                                  hp_wmi_notify, NULL);
479                 if (!err)
480                         hp_wmi_input_setup();
481         }
482
483         if (wmi_has_guid(HPWMI_BIOS_GUID)) {
484                 err = platform_driver_register(&hp_wmi_driver);
485                 if (err)
486                         return 0;
487                 hp_wmi_platform_dev = platform_device_alloc("hp-wmi", -1);
488                 if (!hp_wmi_platform_dev) {
489                         platform_driver_unregister(&hp_wmi_driver);
490                         return 0;
491                 }
492                 platform_device_add(hp_wmi_platform_dev);
493         }
494
495         return 0;
496 }
497
498 static void __exit hp_wmi_exit(void)
499 {
500         if (wmi_has_guid(HPWMI_EVENT_GUID)) {
501                 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
502                 input_unregister_device(hp_wmi_input_dev);
503         }
504         if (hp_wmi_platform_dev) {
505                 platform_device_del(hp_wmi_platform_dev);
506                 platform_driver_unregister(&hp_wmi_driver);
507         }
508 }
509
510 module_init(hp_wmi_init);
511 module_exit(hp_wmi_exit);