x86 platform drivers: hp-wmi Catch and log unkown event and key codes correctly
[pandora-kernel.git] / drivers / platform / x86 / 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/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>
37
38 MODULE_AUTHOR("Matthew Garrett <mjg59@srcf.ucam.org>");
39 MODULE_DESCRIPTION("HP laptop WMI hotkeys driver");
40 MODULE_LICENSE("GPL");
41
42 MODULE_ALIAS("wmi:95F24279-4D7B-4334-9387-ACCDC67EF61C");
43 MODULE_ALIAS("wmi:5FB7F034-2C63-45e9-BE91-3D44E2C707E4");
44
45 #define HPWMI_EVENT_GUID "95F24279-4D7B-4334-9387-ACCDC67EF61C"
46 #define HPWMI_BIOS_GUID "5FB7F034-2C63-45e9-BE91-3D44E2C707E4"
47
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
54
55 enum hp_wmi_radio {
56         HPWMI_WIFI = 0,
57         HPWMI_BLUETOOTH = 1,
58         HPWMI_WWAN = 2,
59 };
60
61 enum hp_wmi_event_ids {
62         HPWMI_DOCK_EVENT = 1,
63         HPWMI_BEZEL_BUTTON = 4,
64         HPWMI_WIRELESS = 5,
65 };
66
67 static int __devinit hp_wmi_bios_setup(struct platform_device *device);
68 static int __exit hp_wmi_bios_remove(struct platform_device *device);
69 static int hp_wmi_resume_handler(struct device *device);
70
71 struct bios_args {
72         u32 signature;
73         u32 command;
74         u32 commandtype;
75         u32 datasize;
76         u32 data;
77 };
78
79 struct bios_return {
80         u32 sigpass;
81         u32 return_code;
82         u32 value;
83 };
84
85 struct key_entry {
86         char type;              /* See KE_* below */
87         u16 code;
88         u16 keycode;
89 };
90
91 enum { KE_KEY, KE_END };
92
93 static struct key_entry hp_wmi_keymap[] = {
94         {KE_KEY, 0x02, KEY_BRIGHTNESSUP},
95         {KE_KEY, 0x03, KEY_BRIGHTNESSDOWN},
96         {KE_KEY, 0x20e6, KEY_PROG1},
97         {KE_KEY, 0x2142, KEY_MEDIA},
98         {KE_KEY, 0x213b, KEY_INFO},
99         {KE_KEY, 0x2169, KEY_DIRECTION},
100         {KE_KEY, 0x231b, KEY_HELP},
101         {KE_END, 0}
102 };
103
104 static struct input_dev *hp_wmi_input_dev;
105 static struct platform_device *hp_wmi_platform_dev;
106
107 static struct rfkill *wifi_rfkill;
108 static struct rfkill *bluetooth_rfkill;
109 static struct rfkill *wwan_rfkill;
110
111 static const struct dev_pm_ops hp_wmi_pm_ops = {
112         .resume  = hp_wmi_resume_handler,
113         .restore  = hp_wmi_resume_handler,
114 };
115
116 static struct platform_driver hp_wmi_driver = {
117         .driver = {
118                 .name = "hp-wmi",
119                 .owner = THIS_MODULE,
120                 .pm = &hp_wmi_pm_ops,
121         },
122         .probe = hp_wmi_bios_setup,
123         .remove = hp_wmi_bios_remove,
124 };
125
126 static int hp_wmi_perform_query(int query, int write, int value)
127 {
128         struct bios_return bios_return;
129         acpi_status status;
130         union acpi_object *obj;
131         struct bios_args args = {
132                 .signature = 0x55434553,
133                 .command = write ? 0x2 : 0x1,
134                 .commandtype = query,
135                 .datasize = write ? 0x4 : 0,
136                 .data = value,
137         };
138         struct acpi_buffer input = { sizeof(struct bios_args), &args };
139         struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
140
141         status = wmi_evaluate_method(HPWMI_BIOS_GUID, 0, 0x3, &input, &output);
142
143         obj = output.pointer;
144
145         if (!obj)
146                 return -EINVAL;
147         else if (obj->type != ACPI_TYPE_BUFFER) {
148                 kfree(obj);
149                 return -EINVAL;
150         }
151
152         bios_return = *((struct bios_return *)obj->buffer.pointer);
153         kfree(obj);
154         if (bios_return.return_code > 0)
155                 return bios_return.return_code * -1;
156         else
157                 return bios_return.value;
158 }
159
160 static int hp_wmi_display_state(void)
161 {
162         return hp_wmi_perform_query(HPWMI_DISPLAY_QUERY, 0, 0);
163 }
164
165 static int hp_wmi_hddtemp_state(void)
166 {
167         return hp_wmi_perform_query(HPWMI_HDDTEMP_QUERY, 0, 0);
168 }
169
170 static int hp_wmi_als_state(void)
171 {
172         return hp_wmi_perform_query(HPWMI_ALS_QUERY, 0, 0);
173 }
174
175 static int hp_wmi_dock_state(void)
176 {
177         int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, 0, 0);
178
179         if (ret < 0)
180                 return ret;
181
182         return ret & 0x1;
183 }
184
185 static int hp_wmi_tablet_state(void)
186 {
187         int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, 0, 0);
188
189         if (ret < 0)
190                 return ret;
191
192         return (ret & 0x4) ? 1 : 0;
193 }
194
195 static int hp_wmi_set_block(void *data, bool blocked)
196 {
197         enum hp_wmi_radio r = (enum hp_wmi_radio) data;
198         int query = BIT(r + 8) | ((!blocked) << r);
199
200         return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, query);
201 }
202
203 static const struct rfkill_ops hp_wmi_rfkill_ops = {
204         .set_block = hp_wmi_set_block,
205 };
206
207 static bool hp_wmi_get_sw_state(enum hp_wmi_radio r)
208 {
209         int wireless = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, 0);
210         int mask = 0x200 << (r * 8);
211
212         if (wireless & mask)
213                 return false;
214         else
215                 return true;
216 }
217
218 static bool hp_wmi_get_hw_state(enum hp_wmi_radio r)
219 {
220         int wireless = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, 0);
221         int mask = 0x800 << (r * 8);
222
223         if (wireless & mask)
224                 return false;
225         else
226                 return true;
227 }
228
229 static ssize_t show_display(struct device *dev, struct device_attribute *attr,
230                             char *buf)
231 {
232         int value = hp_wmi_display_state();
233         if (value < 0)
234                 return -EINVAL;
235         return sprintf(buf, "%d\n", value);
236 }
237
238 static ssize_t show_hddtemp(struct device *dev, struct device_attribute *attr,
239                             char *buf)
240 {
241         int value = hp_wmi_hddtemp_state();
242         if (value < 0)
243                 return -EINVAL;
244         return sprintf(buf, "%d\n", value);
245 }
246
247 static ssize_t show_als(struct device *dev, struct device_attribute *attr,
248                         char *buf)
249 {
250         int value = hp_wmi_als_state();
251         if (value < 0)
252                 return -EINVAL;
253         return sprintf(buf, "%d\n", value);
254 }
255
256 static ssize_t show_dock(struct device *dev, struct device_attribute *attr,
257                          char *buf)
258 {
259         int value = hp_wmi_dock_state();
260         if (value < 0)
261                 return -EINVAL;
262         return sprintf(buf, "%d\n", value);
263 }
264
265 static ssize_t show_tablet(struct device *dev, struct device_attribute *attr,
266                          char *buf)
267 {
268         int value = hp_wmi_tablet_state();
269         if (value < 0)
270                 return -EINVAL;
271         return sprintf(buf, "%d\n", value);
272 }
273
274 static ssize_t set_als(struct device *dev, struct device_attribute *attr,
275                        const char *buf, size_t count)
276 {
277         u32 tmp = simple_strtoul(buf, NULL, 10);
278         hp_wmi_perform_query(HPWMI_ALS_QUERY, 1, tmp);
279         return count;
280 }
281
282 static DEVICE_ATTR(display, S_IRUGO, show_display, NULL);
283 static DEVICE_ATTR(hddtemp, S_IRUGO, show_hddtemp, NULL);
284 static DEVICE_ATTR(als, S_IRUGO | S_IWUSR, show_als, set_als);
285 static DEVICE_ATTR(dock, S_IRUGO, show_dock, NULL);
286 static DEVICE_ATTR(tablet, S_IRUGO, show_tablet, NULL);
287
288 static struct key_entry *hp_wmi_get_entry_by_scancode(unsigned int code)
289 {
290         struct key_entry *key;
291
292         for (key = hp_wmi_keymap; key->type != KE_END; key++)
293                 if (code == key->code)
294                         return key;
295
296         return NULL;
297 }
298
299 static struct key_entry *hp_wmi_get_entry_by_keycode(unsigned int keycode)
300 {
301         struct key_entry *key;
302
303         for (key = hp_wmi_keymap; key->type != KE_END; key++)
304                 if (key->type == KE_KEY && keycode == key->keycode)
305                         return key;
306
307         return NULL;
308 }
309
310 static int hp_wmi_getkeycode(struct input_dev *dev,
311                              unsigned int scancode, unsigned int *keycode)
312 {
313         struct key_entry *key = hp_wmi_get_entry_by_scancode(scancode);
314
315         if (key && key->type == KE_KEY) {
316                 *keycode = key->keycode;
317                 return 0;
318         }
319
320         return -EINVAL;
321 }
322
323 static int hp_wmi_setkeycode(struct input_dev *dev,
324                              unsigned int scancode, unsigned int keycode)
325 {
326         struct key_entry *key;
327         unsigned int old_keycode;
328
329         key = hp_wmi_get_entry_by_scancode(scancode);
330         if (key && key->type == KE_KEY) {
331                 old_keycode = key->keycode;
332                 key->keycode = keycode;
333                 set_bit(keycode, dev->keybit);
334                 if (!hp_wmi_get_entry_by_keycode(old_keycode))
335                         clear_bit(old_keycode, dev->keybit);
336                 return 0;
337         }
338
339         return -EINVAL;
340 }
341
342 static void hp_wmi_notify(u32 value, void *context)
343 {
344         struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
345         static struct key_entry *key;
346         union acpi_object *obj;
347         int eventcode, key_code;
348         acpi_status status;
349
350         status = wmi_get_event_data(value, &response);
351         if (status != AE_OK) {
352                 printk(KERN_INFO "hp-wmi: bad event status 0x%x\n", status);
353                 return;
354         }
355
356         obj = (union acpi_object *)response.pointer;
357
358         if (!obj || obj->type != ACPI_TYPE_BUFFER || obj->buffer.length != 8) {
359                 printk(KERN_INFO "HP WMI: Unknown response received\n");
360                 kfree(obj);
361                 return;
362         }
363
364         eventcode = *((u8 *) obj->buffer.pointer);
365         kfree(obj);
366         switch (eventcode) {
367         case HPWMI_DOCK_EVENT:
368                 input_report_switch(hp_wmi_input_dev, SW_DOCK,
369                                     hp_wmi_dock_state());
370                 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
371                                     hp_wmi_tablet_state());
372                 input_sync(hp_wmi_input_dev);
373                 break;
374         case HPWMI_BEZEL_BUTTON:
375                 key_code = hp_wmi_perform_query(HPWMI_HOTKEY_QUERY, 0,
376                                                  0);
377                 key = hp_wmi_get_entry_by_scancode(key_code);
378                 if (key) {
379                         switch (key->type) {
380                         case KE_KEY:
381                                 input_report_key(hp_wmi_input_dev,
382                                                  key->keycode, 1);
383                                 input_sync(hp_wmi_input_dev);
384                                 input_report_key(hp_wmi_input_dev,
385                                                  key->keycode, 0);
386                                 input_sync(hp_wmi_input_dev);
387                                 break;
388                         }
389                 } else
390                         printk(KERN_INFO "HP WMI: Unknown key code - 0x%x\n",
391                                key_code);
392                 break;
393         case HPWMI_WIRELESS:
394                 if (wifi_rfkill)
395                         rfkill_set_states(wifi_rfkill,
396                                           hp_wmi_get_sw_state(HPWMI_WIFI),
397                                           hp_wmi_get_hw_state(HPWMI_WIFI));
398                 if (bluetooth_rfkill)
399                         rfkill_set_states(bluetooth_rfkill,
400                                           hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
401                                           hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
402                 if (wwan_rfkill)
403                         rfkill_set_states(wwan_rfkill,
404                                           hp_wmi_get_sw_state(HPWMI_WWAN),
405                                           hp_wmi_get_hw_state(HPWMI_WWAN));
406                 break;
407         default:
408                 printk(KERN_INFO "HP WMI: Unknown eventcode - %d\n",
409                        eventcode);
410                 break;
411         }
412 }
413
414 static int __init hp_wmi_input_setup(void)
415 {
416         struct key_entry *key;
417         int err;
418
419         hp_wmi_input_dev = input_allocate_device();
420
421         hp_wmi_input_dev->name = "HP WMI hotkeys";
422         hp_wmi_input_dev->phys = "wmi/input0";
423         hp_wmi_input_dev->id.bustype = BUS_HOST;
424         hp_wmi_input_dev->getkeycode = hp_wmi_getkeycode;
425         hp_wmi_input_dev->setkeycode = hp_wmi_setkeycode;
426
427         for (key = hp_wmi_keymap; key->type != KE_END; key++) {
428                 switch (key->type) {
429                 case KE_KEY:
430                         set_bit(EV_KEY, hp_wmi_input_dev->evbit);
431                         set_bit(key->keycode, hp_wmi_input_dev->keybit);
432                         break;
433                 }
434         }
435
436         set_bit(EV_SW, hp_wmi_input_dev->evbit);
437         set_bit(SW_DOCK, hp_wmi_input_dev->swbit);
438         set_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit);
439
440         /* Set initial hardware state */
441         input_report_switch(hp_wmi_input_dev, SW_DOCK, hp_wmi_dock_state());
442         input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
443                             hp_wmi_tablet_state());
444         input_sync(hp_wmi_input_dev);
445
446         err = input_register_device(hp_wmi_input_dev);
447
448         if (err) {
449                 input_free_device(hp_wmi_input_dev);
450                 return err;
451         }
452
453         return 0;
454 }
455
456 static void cleanup_sysfs(struct platform_device *device)
457 {
458         device_remove_file(&device->dev, &dev_attr_display);
459         device_remove_file(&device->dev, &dev_attr_hddtemp);
460         device_remove_file(&device->dev, &dev_attr_als);
461         device_remove_file(&device->dev, &dev_attr_dock);
462         device_remove_file(&device->dev, &dev_attr_tablet);
463 }
464
465 static int __devinit hp_wmi_bios_setup(struct platform_device *device)
466 {
467         int err;
468         int wireless = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, 0);
469
470         err = device_create_file(&device->dev, &dev_attr_display);
471         if (err)
472                 goto add_sysfs_error;
473         err = device_create_file(&device->dev, &dev_attr_hddtemp);
474         if (err)
475                 goto add_sysfs_error;
476         err = device_create_file(&device->dev, &dev_attr_als);
477         if (err)
478                 goto add_sysfs_error;
479         err = device_create_file(&device->dev, &dev_attr_dock);
480         if (err)
481                 goto add_sysfs_error;
482         err = device_create_file(&device->dev, &dev_attr_tablet);
483         if (err)
484                 goto add_sysfs_error;
485
486         if (wireless & 0x1) {
487                 wifi_rfkill = rfkill_alloc("hp-wifi", &device->dev,
488                                            RFKILL_TYPE_WLAN,
489                                            &hp_wmi_rfkill_ops,
490                                            (void *) HPWMI_WIFI);
491                 rfkill_init_sw_state(wifi_rfkill,
492                                      hp_wmi_get_sw_state(HPWMI_WIFI));
493                 rfkill_set_hw_state(wifi_rfkill,
494                                     hp_wmi_get_hw_state(HPWMI_WIFI));
495                 err = rfkill_register(wifi_rfkill);
496                 if (err)
497                         goto register_wifi_error;
498         }
499
500         if (wireless & 0x2) {
501                 bluetooth_rfkill = rfkill_alloc("hp-bluetooth", &device->dev,
502                                                 RFKILL_TYPE_BLUETOOTH,
503                                                 &hp_wmi_rfkill_ops,
504                                                 (void *) HPWMI_BLUETOOTH);
505                 rfkill_init_sw_state(bluetooth_rfkill,
506                                      hp_wmi_get_sw_state(HPWMI_BLUETOOTH));
507                 rfkill_set_hw_state(bluetooth_rfkill,
508                                     hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
509                 err = rfkill_register(bluetooth_rfkill);
510                 if (err)
511                         goto register_bluetooth_error;
512         }
513
514         if (wireless & 0x4) {
515                 wwan_rfkill = rfkill_alloc("hp-wwan", &device->dev,
516                                            RFKILL_TYPE_WWAN,
517                                            &hp_wmi_rfkill_ops,
518                                            (void *) HPWMI_WWAN);
519                 rfkill_init_sw_state(wwan_rfkill,
520                                      hp_wmi_get_sw_state(HPWMI_WWAN));
521                 rfkill_set_hw_state(wwan_rfkill,
522                                     hp_wmi_get_hw_state(HPWMI_WWAN));
523                 err = rfkill_register(wwan_rfkill);
524                 if (err)
525                         goto register_wwan_err;
526         }
527
528         return 0;
529 register_wwan_err:
530         rfkill_destroy(wwan_rfkill);
531         if (bluetooth_rfkill)
532                 rfkill_unregister(bluetooth_rfkill);
533 register_bluetooth_error:
534         rfkill_destroy(bluetooth_rfkill);
535         if (wifi_rfkill)
536                 rfkill_unregister(wifi_rfkill);
537 register_wifi_error:
538         rfkill_destroy(wifi_rfkill);
539 add_sysfs_error:
540         cleanup_sysfs(device);
541         return err;
542 }
543
544 static int __exit hp_wmi_bios_remove(struct platform_device *device)
545 {
546         cleanup_sysfs(device);
547
548         if (wifi_rfkill) {
549                 rfkill_unregister(wifi_rfkill);
550                 rfkill_destroy(wifi_rfkill);
551         }
552         if (bluetooth_rfkill) {
553                 rfkill_unregister(bluetooth_rfkill);
554                 rfkill_destroy(bluetooth_rfkill);
555         }
556         if (wwan_rfkill) {
557                 rfkill_unregister(wwan_rfkill);
558                 rfkill_destroy(wwan_rfkill);
559         }
560
561         return 0;
562 }
563
564 static int hp_wmi_resume_handler(struct device *device)
565 {
566         /*
567          * Hardware state may have changed while suspended, so trigger
568          * input events for the current state. As this is a switch,
569          * the input layer will only actually pass it on if the state
570          * changed.
571          */
572         if (hp_wmi_input_dev) {
573                 input_report_switch(hp_wmi_input_dev, SW_DOCK,
574                                     hp_wmi_dock_state());
575                 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
576                                     hp_wmi_tablet_state());
577                 input_sync(hp_wmi_input_dev);
578         }
579
580         if (wifi_rfkill)
581                 rfkill_set_states(wifi_rfkill,
582                                   hp_wmi_get_sw_state(HPWMI_WIFI),
583                                   hp_wmi_get_hw_state(HPWMI_WIFI));
584         if (bluetooth_rfkill)
585                 rfkill_set_states(bluetooth_rfkill,
586                                   hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
587                                   hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
588         if (wwan_rfkill)
589                 rfkill_set_states(wwan_rfkill,
590                                   hp_wmi_get_sw_state(HPWMI_WWAN),
591                                   hp_wmi_get_hw_state(HPWMI_WWAN));
592
593         return 0;
594 }
595
596 static int __init hp_wmi_init(void)
597 {
598         int err;
599
600         if (wmi_has_guid(HPWMI_EVENT_GUID)) {
601                 err = wmi_install_notify_handler(HPWMI_EVENT_GUID,
602                                                  hp_wmi_notify, NULL);
603                 if (ACPI_SUCCESS(err))
604                         hp_wmi_input_setup();
605         }
606
607         if (wmi_has_guid(HPWMI_BIOS_GUID)) {
608                 err = platform_driver_register(&hp_wmi_driver);
609                 if (err)
610                         return 0;
611                 hp_wmi_platform_dev = platform_device_alloc("hp-wmi", -1);
612                 if (!hp_wmi_platform_dev) {
613                         platform_driver_unregister(&hp_wmi_driver);
614                         return 0;
615                 }
616                 platform_device_add(hp_wmi_platform_dev);
617         }
618
619         return 0;
620 }
621
622 static void __exit hp_wmi_exit(void)
623 {
624         if (wmi_has_guid(HPWMI_EVENT_GUID)) {
625                 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
626                 input_unregister_device(hp_wmi_input_dev);
627         }
628         if (hp_wmi_platform_dev) {
629                 platform_device_del(hp_wmi_platform_dev);
630                 platform_driver_unregister(&hp_wmi_driver);
631         }
632 }
633
634 module_init(hp_wmi_init);
635 module_exit(hp_wmi_exit);