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