drm/amdgpu: record error code when ring test failed
[linux-2.6-block.git] / drivers / acpi / button.c
CommitLineData
1da177e4 1/*
50a4da89 2 * button.c - ACPI Button Driver
1da177e4
LT
3 *
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6 *
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
1da177e4
LT
19 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 */
21
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/init.h>
b34a8030
AS
25#include <linux/types.h>
26#include <linux/proc_fs.h>
27#include <linux/seq_file.h>
c0968f0e 28#include <linux/input.h>
5a0e3ad6 29#include <linux/slab.h>
8b48463f 30#include <linux/acpi.h>
6270da6f 31#include <acpi/button.h>
1da177e4 32
a192a958
LB
33#define PREFIX "ACPI: "
34
1da177e4 35#define ACPI_BUTTON_CLASS "button"
b34a8030
AS
36#define ACPI_BUTTON_FILE_INFO "info"
37#define ACPI_BUTTON_FILE_STATE "state"
38#define ACPI_BUTTON_TYPE_UNKNOWN 0x00
1da177e4
LT
39#define ACPI_BUTTON_NOTIFY_STATUS 0x80
40
41#define ACPI_BUTTON_SUBCLASS_POWER "power"
4be44fcd 42#define ACPI_BUTTON_HID_POWER "PNP0C0C"
d68b597c 43#define ACPI_BUTTON_DEVICE_NAME_POWER "Power Button"
1da177e4 44#define ACPI_BUTTON_TYPE_POWER 0x01
1da177e4
LT
45
46#define ACPI_BUTTON_SUBCLASS_SLEEP "sleep"
47#define ACPI_BUTTON_HID_SLEEP "PNP0C0E"
d68b597c 48#define ACPI_BUTTON_DEVICE_NAME_SLEEP "Sleep Button"
1da177e4 49#define ACPI_BUTTON_TYPE_SLEEP 0x03
1da177e4
LT
50
51#define ACPI_BUTTON_SUBCLASS_LID "lid"
52#define ACPI_BUTTON_HID_LID "PNP0C0D"
53#define ACPI_BUTTON_DEVICE_NAME_LID "Lid Switch"
54#define ACPI_BUTTON_TYPE_LID 0x05
55
3540c32a
LZ
56#define ACPI_BUTTON_LID_INIT_IGNORE 0x00
57#define ACPI_BUTTON_LID_INIT_OPEN 0x01
58#define ACPI_BUTTON_LID_INIT_METHOD 0x02
59
1da177e4 60#define _COMPONENT ACPI_BUTTON_COMPONENT
f52fd66d 61ACPI_MODULE_NAME("button");
1da177e4 62
c0968f0e 63MODULE_AUTHOR("Paul Diefenbaugh");
7cda93e0 64MODULE_DESCRIPTION("ACPI Button Driver");
1da177e4
LT
65MODULE_LICENSE("GPL");
66
1ba90e3a
TR
67static const struct acpi_device_id button_device_ids[] = {
68 {ACPI_BUTTON_HID_LID, 0},
69 {ACPI_BUTTON_HID_SLEEP, 0},
70 {ACPI_BUTTON_HID_SLEEPF, 0},
71 {ACPI_BUTTON_HID_POWER, 0},
72 {ACPI_BUTTON_HID_POWERF, 0},
73 {"", 0},
74};
75MODULE_DEVICE_TABLE(acpi, button_device_ids);
76
4be44fcd 77static int acpi_button_add(struct acpi_device *device);
51fac838 78static int acpi_button_remove(struct acpi_device *device);
373cfc36 79static void acpi_button_notify(struct acpi_device *device, u32 event);
1da177e4 80
90692404 81#ifdef CONFIG_PM_SLEEP
e71eeb2a 82static int acpi_button_suspend(struct device *dev);
1be532de 83static int acpi_button_resume(struct device *dev);
2de9fd17 84#else
e71eeb2a 85#define acpi_button_suspend NULL
2de9fd17 86#define acpi_button_resume NULL
90692404 87#endif
e71eeb2a 88static SIMPLE_DEV_PM_OPS(acpi_button_pm, acpi_button_suspend, acpi_button_resume);
1be532de 89
1da177e4 90static struct acpi_driver acpi_button_driver = {
c2b6705b 91 .name = "button",
4be44fcd 92 .class = ACPI_BUTTON_CLASS,
1ba90e3a 93 .ids = button_device_ids,
4be44fcd
LB
94 .ops = {
95 .add = acpi_button_add,
96 .remove = acpi_button_remove,
373cfc36 97 .notify = acpi_button_notify,
c0968f0e 98 },
1be532de 99 .drv.pm = &acpi_button_pm,
1da177e4
LT
100};
101
102struct acpi_button {
c0968f0e
DT
103 unsigned int type;
104 struct input_dev *input;
105 char phys[32]; /* for input device */
4be44fcd 106 unsigned long pushed;
e71eeb2a 107 bool suspended;
1da177e4
LT
108};
109
7e12715e
JB
110static BLOCKING_NOTIFIER_HEAD(acpi_lid_notifier);
111static struct acpi_device *lid_device;
3540c32a 112static u8 lid_init_state = ACPI_BUTTON_LID_INIT_METHOD;
7e12715e 113
b34a8030
AS
114/* --------------------------------------------------------------------------
115 FS Interface (/proc)
116 -------------------------------------------------------------------------- */
117
4be44fcd 118static struct proc_dir_entry *acpi_button_dir;
912b7427 119static struct proc_dir_entry *acpi_lid_dir;
4be44fcd 120
ee7e2265
LZ
121static int acpi_lid_evaluate_state(struct acpi_device *device)
122{
123 unsigned long long lid_state;
124 acpi_status status;
125
126 status = acpi_evaluate_integer(device->handle, "_LID", NULL, &lid_state);
127 if (ACPI_FAILURE(status))
128 return -ENODEV;
129
130 return lid_state ? 1 : 0;
131}
132
133static int acpi_lid_notify_state(struct acpi_device *device, int state)
134{
135 struct acpi_button *button = acpi_driver_data(device);
136 int ret;
137
138 /* input layer checks if event is redundant */
139 input_report_switch(button->input, SW_LID, !state);
140 input_sync(button->input);
141
142 if (state)
143 pm_wakeup_event(&device->dev, 0);
144
145 ret = blocking_notifier_call_chain(&acpi_lid_notifier, state, device);
146 if (ret == NOTIFY_DONE)
147 ret = blocking_notifier_call_chain(&acpi_lid_notifier, state,
148 device);
149 if (ret == NOTIFY_DONE || ret == NOTIFY_OK) {
150 /*
151 * It is also regarded as success if the notifier_chain
152 * returns NOTIFY_OK or NOTIFY_DONE.
153 */
154 ret = 0;
155 }
156 return ret;
157}
158
b34a8030
AS
159static int acpi_button_state_seq_show(struct seq_file *seq, void *offset)
160{
106c19e7 161 struct acpi_device *device = seq->private;
ee7e2265 162 int state;
b34a8030 163
ee7e2265 164 state = acpi_lid_evaluate_state(device);
c0968f0e 165 seq_printf(seq, "state: %s\n",
ee7e2265 166 state < 0 ? "unsupported" : (state ? "open" : "closed"));
d550d98d 167 return 0;
b34a8030
AS
168}
169
170static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
171{
d9dda78b 172 return single_open(file, acpi_button_state_seq_show, PDE_DATA(inode));
b34a8030
AS
173}
174
912b7427
ZR
175static const struct file_operations acpi_button_state_fops = {
176 .owner = THIS_MODULE,
177 .open = acpi_button_state_open_fs,
178 .read = seq_read,
179 .llseek = seq_lseek,
180 .release = single_release,
181};
b34a8030 182
4be44fcd 183static int acpi_button_add_fs(struct acpi_device *device)
b34a8030 184{
1bce8113 185 struct acpi_button *button = acpi_driver_data(device);
4be44fcd 186 struct proc_dir_entry *entry = NULL;
912b7427 187 int ret = 0;
b34a8030 188
912b7427
ZR
189 /* procfs I/F for ACPI lid device only */
190 if (button->type != ACPI_BUTTON_TYPE_LID)
191 return 0;
192
193 if (acpi_button_dir || acpi_lid_dir) {
194 printk(KERN_ERR PREFIX "More than one Lid device found!\n");
195 return -EEXIST;
b34a8030
AS
196 }
197
912b7427
ZR
198 /* create /proc/acpi/button */
199 acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
200 if (!acpi_button_dir)
d550d98d 201 return -ENODEV;
b34a8030 202
912b7427
ZR
203 /* create /proc/acpi/button/lid */
204 acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
205 if (!acpi_lid_dir) {
206 ret = -ENODEV;
207 goto remove_button_dir;
208 }
b34a8030 209
912b7427
ZR
210 /* create /proc/acpi/button/lid/LID/ */
211 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), acpi_lid_dir);
212 if (!acpi_device_dir(device)) {
213 ret = -ENODEV;
214 goto remove_lid_dir;
215 }
b34a8030 216
912b7427
ZR
217 /* create /proc/acpi/button/lid/LID/state */
218 entry = proc_create_data(ACPI_BUTTON_FILE_STATE,
219 S_IRUGO, acpi_device_dir(device),
220 &acpi_button_state_fops, device);
221 if (!entry) {
222 ret = -ENODEV;
223 goto remove_dev_dir;
b34a8030
AS
224 }
225
912b7427
ZR
226done:
227 return ret;
228
229remove_dev_dir:
230 remove_proc_entry(acpi_device_bid(device),
231 acpi_lid_dir);
232 acpi_device_dir(device) = NULL;
233remove_lid_dir:
234 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
e370cc86 235 acpi_lid_dir = NULL;
912b7427
ZR
236remove_button_dir:
237 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
e370cc86 238 acpi_button_dir = NULL;
912b7427 239 goto done;
b34a8030
AS
240}
241
4be44fcd 242static int acpi_button_remove_fs(struct acpi_device *device)
b34a8030 243{
c0968f0e 244 struct acpi_button *button = acpi_driver_data(device);
b34a8030 245
912b7427
ZR
246 if (button->type != ACPI_BUTTON_TYPE_LID)
247 return 0;
b34a8030 248
912b7427
ZR
249 remove_proc_entry(ACPI_BUTTON_FILE_STATE,
250 acpi_device_dir(device));
251 remove_proc_entry(acpi_device_bid(device),
252 acpi_lid_dir);
253 acpi_device_dir(device) = NULL;
254 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
e370cc86 255 acpi_lid_dir = NULL;
912b7427 256 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
e370cc86 257 acpi_button_dir = NULL;
b34a8030 258
d550d98d 259 return 0;
b34a8030
AS
260}
261
1da177e4
LT
262/* --------------------------------------------------------------------------
263 Driver Interface
264 -------------------------------------------------------------------------- */
7e12715e
JB
265int acpi_lid_notifier_register(struct notifier_block *nb)
266{
267 return blocking_notifier_chain_register(&acpi_lid_notifier, nb);
268}
269EXPORT_SYMBOL(acpi_lid_notifier_register);
270
271int acpi_lid_notifier_unregister(struct notifier_block *nb)
272{
273 return blocking_notifier_chain_unregister(&acpi_lid_notifier, nb);
274}
275EXPORT_SYMBOL(acpi_lid_notifier_unregister);
276
277int acpi_lid_open(void)
278{
2c907b72
JB
279 if (!lid_device)
280 return -ENODEV;
281
ee7e2265 282 return acpi_lid_evaluate_state(lid_device);
7e12715e
JB
283}
284EXPORT_SYMBOL(acpi_lid_open);
285
ee7e2265 286static int acpi_lid_update_state(struct acpi_device *device)
23de5d9e 287{
ee7e2265 288 int state;
50a4da89 289
ee7e2265
LZ
290 state = acpi_lid_evaluate_state(device);
291 if (state < 0)
292 return state;
7e12715e 293
ee7e2265 294 return acpi_lid_notify_state(device, state);
23de5d9e 295}
1da177e4 296
3540c32a
LZ
297static void acpi_lid_initialize_state(struct acpi_device *device)
298{
299 switch (lid_init_state) {
300 case ACPI_BUTTON_LID_INIT_OPEN:
301 (void)acpi_lid_notify_state(device, 1);
302 break;
303 case ACPI_BUTTON_LID_INIT_METHOD:
304 (void)acpi_lid_update_state(device);
305 break;
306 case ACPI_BUTTON_LID_INIT_IGNORE:
307 default:
308 break;
309 }
310}
311
373cfc36 312static void acpi_button_notify(struct acpi_device *device, u32 event)
1da177e4 313{
373cfc36 314 struct acpi_button *button = acpi_driver_data(device);
c0968f0e 315 struct input_dev *input;
1da177e4 316
1da177e4 317 switch (event) {
373cfc36
BH
318 case ACPI_FIXED_HARDWARE_EVENT:
319 event = ACPI_BUTTON_NOTIFY_STATUS;
320 /* fall through */
1da177e4 321 case ACPI_BUTTON_NOTIFY_STATUS:
c0968f0e 322 input = button->input;
c0968f0e 323 if (button->type == ACPI_BUTTON_TYPE_LID) {
ee7e2265 324 acpi_lid_update_state(device);
c0968f0e 325 } else {
e71eeb2a
RW
326 int keycode;
327
328 pm_wakeup_event(&device->dev, 0);
329 if (button->suspended)
330 break;
c0968f0e 331
e71eeb2a
RW
332 keycode = test_bit(KEY_SLEEP, input->keybit) ?
333 KEY_SLEEP : KEY_POWER;
c0968f0e
DT
334 input_report_key(input, keycode, 1);
335 input_sync(input);
336 input_report_key(input, keycode, 0);
df316e93 337 input_sync(input);
1f83511b 338
0bf6368e
LT
339 acpi_bus_generate_netlink_event(
340 device->pnp.device_class,
341 dev_name(&device->dev),
342 event, ++button->pushed);
c0968f0e 343 }
1da177e4
LT
344 break;
345 default:
346 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd 347 "Unsupported event [0x%x]\n", event));
1da177e4
LT
348 break;
349 }
1da177e4
LT
350}
351
90692404 352#ifdef CONFIG_PM_SLEEP
e71eeb2a
RW
353static int acpi_button_suspend(struct device *dev)
354{
355 struct acpi_device *device = to_acpi_device(dev);
356 struct acpi_button *button = acpi_driver_data(device);
357
358 button->suspended = true;
359 return 0;
360}
361
1be532de 362static int acpi_button_resume(struct device *dev)
23de5d9e 363{
1be532de 364 struct acpi_device *device = to_acpi_device(dev);
1bce8113 365 struct acpi_button *button = acpi_driver_data(device);
50a4da89 366
e71eeb2a 367 button->suspended = false;
3540c32a
LZ
368 if (button->type == ACPI_BUTTON_TYPE_LID)
369 acpi_lid_initialize_state(device);
23de5d9e
AS
370 return 0;
371}
90692404 372#endif
23de5d9e 373
c0968f0e
DT
374static int acpi_button_add(struct acpi_device *device)
375{
c0968f0e
DT
376 struct acpi_button *button;
377 struct input_dev *input;
620e112c
TR
378 const char *hid = acpi_device_hid(device);
379 char *name, *class;
1bce8113 380 int error;
1da177e4 381
c0968f0e 382 button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
1da177e4 383 if (!button)
d550d98d 384 return -ENOMEM;
1da177e4 385
db89b4f0 386 device->driver_data = button;
1da177e4 387
c0968f0e
DT
388 button->input = input = input_allocate_device();
389 if (!input) {
390 error = -ENOMEM;
391 goto err_free_button;
392 }
393
bf04a772
BH
394 name = acpi_device_name(device);
395 class = acpi_device_class(device);
396
d68b597c
BH
397 if (!strcmp(hid, ACPI_BUTTON_HID_POWER) ||
398 !strcmp(hid, ACPI_BUTTON_HID_POWERF)) {
1da177e4 399 button->type = ACPI_BUTTON_TYPE_POWER;
bf04a772
BH
400 strcpy(name, ACPI_BUTTON_DEVICE_NAME_POWER);
401 sprintf(class, "%s/%s",
1da177e4 402 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
d68b597c
BH
403 } else if (!strcmp(hid, ACPI_BUTTON_HID_SLEEP) ||
404 !strcmp(hid, ACPI_BUTTON_HID_SLEEPF)) {
1da177e4 405 button->type = ACPI_BUTTON_TYPE_SLEEP;
bf04a772
BH
406 strcpy(name, ACPI_BUTTON_DEVICE_NAME_SLEEP);
407 sprintf(class, "%s/%s",
1da177e4 408 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
bf04a772 409 } else if (!strcmp(hid, ACPI_BUTTON_HID_LID)) {
1da177e4 410 button->type = ACPI_BUTTON_TYPE_LID;
bf04a772
BH
411 strcpy(name, ACPI_BUTTON_DEVICE_NAME_LID);
412 sprintf(class, "%s/%s",
1da177e4 413 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
4be44fcd 414 } else {
bf04a772 415 printk(KERN_ERR PREFIX "Unsupported hid [%s]\n", hid);
c0968f0e
DT
416 error = -ENODEV;
417 goto err_free_input;
1da177e4
LT
418 }
419
c0968f0e
DT
420 error = acpi_button_add_fs(device);
421 if (error)
422 goto err_free_input;
423
bf04a772 424 snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
c0968f0e 425
bf04a772 426 input->name = name;
c0968f0e
DT
427 input->phys = button->phys;
428 input->id.bustype = BUS_HOST;
429 input->id.product = button->type;
3b34e523 430 input->dev.parent = &device->dev;
b34a8030 431
1da177e4 432 switch (button->type) {
c0968f0e 433 case ACPI_BUTTON_TYPE_POWER:
763f527b 434 input_set_capability(input, EV_KEY, KEY_POWER);
1da177e4 435 break;
c0968f0e
DT
436
437 case ACPI_BUTTON_TYPE_SLEEP:
763f527b 438 input_set_capability(input, EV_KEY, KEY_SLEEP);
1da177e4 439 break;
c0968f0e
DT
440
441 case ACPI_BUTTON_TYPE_LID:
763f527b 442 input_set_capability(input, EV_SW, SW_LID);
1da177e4
LT
443 break;
444 }
445
c0968f0e
DT
446 error = input_register_device(input);
447 if (error)
373cfc36 448 goto err_remove_fs;
7e12715e 449 if (button->type == ACPI_BUTTON_TYPE_LID) {
3540c32a 450 acpi_lid_initialize_state(device);
7e12715e
JB
451 /*
452 * This assumes there's only one lid device, or if there are
453 * more we only care about the last one...
454 */
455 lid_device = device;
456 }
1da177e4 457
bf04a772 458 printk(KERN_INFO PREFIX "%s [%s]\n", name, acpi_device_bid(device));
c0968f0e 459 return 0;
1da177e4 460
c0968f0e
DT
461 err_remove_fs:
462 acpi_button_remove_fs(device);
463 err_free_input:
464 input_free_device(input);
465 err_free_button:
466 kfree(button);
467 return error;
1da177e4
LT
468}
469
51fac838 470static int acpi_button_remove(struct acpi_device *device)
1da177e4 471{
1bce8113 472 struct acpi_button *button = acpi_driver_data(device);
1da177e4 473
4be44fcd 474 acpi_button_remove_fs(device);
c0968f0e 475 input_unregister_device(button->input);
1da177e4 476 kfree(button);
d550d98d 477 return 0;
1da177e4
LT
478}
479
3540c32a
LZ
480static int param_set_lid_init_state(const char *val, struct kernel_param *kp)
481{
482 int result = 0;
483
484 if (!strncmp(val, "open", sizeof("open") - 1)) {
485 lid_init_state = ACPI_BUTTON_LID_INIT_OPEN;
486 pr_info("Notify initial lid state as open\n");
487 } else if (!strncmp(val, "method", sizeof("method") - 1)) {
488 lid_init_state = ACPI_BUTTON_LID_INIT_METHOD;
489 pr_info("Notify initial lid state with _LID return value\n");
490 } else if (!strncmp(val, "ignore", sizeof("ignore") - 1)) {
491 lid_init_state = ACPI_BUTTON_LID_INIT_IGNORE;
492 pr_info("Do not notify initial lid state\n");
493 } else
494 result = -EINVAL;
495 return result;
496}
497
498static int param_get_lid_init_state(char *buffer, struct kernel_param *kp)
499{
500 switch (lid_init_state) {
501 case ACPI_BUTTON_LID_INIT_OPEN:
502 return sprintf(buffer, "open");
503 case ACPI_BUTTON_LID_INIT_METHOD:
504 return sprintf(buffer, "method");
505 case ACPI_BUTTON_LID_INIT_IGNORE:
506 return sprintf(buffer, "ignore");
507 default:
508 return sprintf(buffer, "invalid");
509 }
510 return 0;
511}
512
513module_param_call(lid_init_state,
514 param_set_lid_init_state, param_get_lid_init_state,
515 NULL, 0644);
516MODULE_PARM_DESC(lid_init_state, "Behavior for reporting LID initial state");
517
466e78f7 518module_acpi_driver(acpi_button_driver);