ACPI: delete some defaults from ACPI Kconfig
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / acpi / button.c
CommitLineData
1da177e4
LT
1/*
2 * acpi_button.c - ACPI Button Driver ($Revision: 30 $)
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 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 *
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 */
25
26#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/init.h>
b34a8030
AS
29#include <linux/types.h>
30#include <linux/proc_fs.h>
31#include <linux/seq_file.h>
1da177e4
LT
32#include <acpi/acpi_bus.h>
33#include <acpi/acpi_drivers.h>
34
1da177e4
LT
35#define ACPI_BUTTON_COMPONENT 0x00080000
36#define ACPI_BUTTON_DRIVER_NAME "ACPI Button Driver"
37#define ACPI_BUTTON_CLASS "button"
b34a8030
AS
38#define ACPI_BUTTON_FILE_INFO "info"
39#define ACPI_BUTTON_FILE_STATE "state"
40#define ACPI_BUTTON_TYPE_UNKNOWN 0x00
1da177e4
LT
41#define ACPI_BUTTON_NOTIFY_STATUS 0x80
42
43#define ACPI_BUTTON_SUBCLASS_POWER "power"
4be44fcd 44#define ACPI_BUTTON_HID_POWER "PNP0C0C"
1da177e4
LT
45#define ACPI_BUTTON_DEVICE_NAME_POWER "Power Button (CM)"
46#define ACPI_BUTTON_DEVICE_NAME_POWERF "Power Button (FF)"
47#define ACPI_BUTTON_TYPE_POWER 0x01
48#define ACPI_BUTTON_TYPE_POWERF 0x02
49
50#define ACPI_BUTTON_SUBCLASS_SLEEP "sleep"
51#define ACPI_BUTTON_HID_SLEEP "PNP0C0E"
52#define ACPI_BUTTON_DEVICE_NAME_SLEEP "Sleep Button (CM)"
53#define ACPI_BUTTON_DEVICE_NAME_SLEEPF "Sleep Button (FF)"
54#define ACPI_BUTTON_TYPE_SLEEP 0x03
55#define ACPI_BUTTON_TYPE_SLEEPF 0x04
56
57#define ACPI_BUTTON_SUBCLASS_LID "lid"
58#define ACPI_BUTTON_HID_LID "PNP0C0D"
59#define ACPI_BUTTON_DEVICE_NAME_LID "Lid Switch"
60#define ACPI_BUTTON_TYPE_LID 0x05
61
62#define _COMPONENT ACPI_BUTTON_COMPONENT
4be44fcd 63ACPI_MODULE_NAME("acpi_button")
1da177e4 64
4be44fcd 65 MODULE_AUTHOR("Paul Diefenbaugh");
1da177e4
LT
66MODULE_DESCRIPTION(ACPI_BUTTON_DRIVER_NAME);
67MODULE_LICENSE("GPL");
68
4be44fcd
LB
69static int acpi_button_add(struct acpi_device *device);
70static int acpi_button_remove(struct acpi_device *device, int type);
b34a8030
AS
71static int acpi_button_info_open_fs(struct inode *inode, struct file *file);
72static int acpi_button_state_open_fs(struct inode *inode, struct file *file);
1da177e4
LT
73
74static struct acpi_driver acpi_button_driver = {
4be44fcd
LB
75 .name = ACPI_BUTTON_DRIVER_NAME,
76 .class = ACPI_BUTTON_CLASS,
77 .ids = "ACPI_FPB,ACPI_FSB,PNP0C0D,PNP0C0C,PNP0C0E",
78 .ops = {
79 .add = acpi_button_add,
80 .remove = acpi_button_remove,
81 },
1da177e4
LT
82};
83
84struct acpi_button {
4be44fcd
LB
85 struct acpi_device *device; /* Fixed button kludge */
86 u8 type;
87 unsigned long pushed;
1da177e4
LT
88};
89
b34a8030 90static struct file_operations acpi_button_info_fops = {
4be44fcd
LB
91 .open = acpi_button_info_open_fs,
92 .read = seq_read,
93 .llseek = seq_lseek,
94 .release = single_release,
b34a8030
AS
95};
96
97static struct file_operations acpi_button_state_fops = {
4be44fcd
LB
98 .open = acpi_button_state_open_fs,
99 .read = seq_read,
100 .llseek = seq_lseek,
101 .release = single_release,
b34a8030 102};
4be44fcd 103
b34a8030
AS
104/* --------------------------------------------------------------------------
105 FS Interface (/proc)
106 -------------------------------------------------------------------------- */
107
4be44fcd 108static struct proc_dir_entry *acpi_button_dir;
b34a8030
AS
109
110static int acpi_button_info_seq_show(struct seq_file *seq, void *offset)
111{
4be44fcd 112 struct acpi_button *button = (struct acpi_button *)seq->private;
b34a8030 113
b34a8030
AS
114
115 if (!button || !button->device)
d550d98d 116 return 0;
b34a8030 117
4be44fcd
LB
118 seq_printf(seq, "type: %s\n",
119 acpi_device_name(button->device));
b34a8030 120
d550d98d 121 return 0;
b34a8030
AS
122}
123
124static int acpi_button_info_open_fs(struct inode *inode, struct file *file)
125{
126 return single_open(file, acpi_button_info_seq_show, PDE(inode)->data);
127}
4be44fcd 128
b34a8030
AS
129static int acpi_button_state_seq_show(struct seq_file *seq, void *offset)
130{
4be44fcd
LB
131 struct acpi_button *button = (struct acpi_button *)seq->private;
132 acpi_status status;
133 unsigned long state;
b34a8030 134
b34a8030
AS
135
136 if (!button || !button->device)
d550d98d 137 return 0;
b34a8030 138
27b1d3e8 139 status = acpi_evaluate_integer(button->device->handle, "_LID", NULL, &state);
b34a8030
AS
140 if (ACPI_FAILURE(status)) {
141 seq_printf(seq, "state: unsupported\n");
4be44fcd
LB
142 } else {
143 seq_printf(seq, "state: %s\n",
144 (state ? "open" : "closed"));
b34a8030
AS
145 }
146
d550d98d 147 return 0;
b34a8030
AS
148}
149
150static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
151{
152 return single_open(file, acpi_button_state_seq_show, PDE(inode)->data);
153}
154
155static struct proc_dir_entry *acpi_power_dir;
156static struct proc_dir_entry *acpi_sleep_dir;
157static struct proc_dir_entry *acpi_lid_dir;
158
4be44fcd 159static int acpi_button_add_fs(struct acpi_device *device)
b34a8030 160{
4be44fcd
LB
161 struct proc_dir_entry *entry = NULL;
162 struct acpi_button *button = NULL;
b34a8030 163
b34a8030
AS
164
165 if (!device || !acpi_driver_data(device))
d550d98d 166 return -EINVAL;
b34a8030
AS
167
168 button = acpi_driver_data(device);
169
170 switch (button->type) {
171 case ACPI_BUTTON_TYPE_POWER:
172 case ACPI_BUTTON_TYPE_POWERF:
173 if (!acpi_power_dir)
4be44fcd
LB
174 acpi_power_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_POWER,
175 acpi_button_dir);
b34a8030
AS
176 entry = acpi_power_dir;
177 break;
178 case ACPI_BUTTON_TYPE_SLEEP:
179 case ACPI_BUTTON_TYPE_SLEEPF:
180 if (!acpi_sleep_dir)
4be44fcd
LB
181 acpi_sleep_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_SLEEP,
182 acpi_button_dir);
b34a8030
AS
183 entry = acpi_sleep_dir;
184 break;
185 case ACPI_BUTTON_TYPE_LID:
186 if (!acpi_lid_dir)
4be44fcd
LB
187 acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID,
188 acpi_button_dir);
b34a8030
AS
189 entry = acpi_lid_dir;
190 break;
191 }
192
193 if (!entry)
d550d98d 194 return -ENODEV;
b34a8030
AS
195 entry->owner = THIS_MODULE;
196
197 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), entry);
198 if (!acpi_device_dir(device))
d550d98d 199 return -ENODEV;
b34a8030
AS
200 acpi_device_dir(device)->owner = THIS_MODULE;
201
202 /* 'info' [R] */
203 entry = create_proc_entry(ACPI_BUTTON_FILE_INFO,
4be44fcd 204 S_IRUGO, acpi_device_dir(device));
b34a8030 205 if (!entry)
d550d98d 206 return -ENODEV;
b34a8030
AS
207 else {
208 entry->proc_fops = &acpi_button_info_fops;
209 entry->data = acpi_driver_data(device);
210 entry->owner = THIS_MODULE;
211 }
212
213 /* show lid state [R] */
214 if (button->type == ACPI_BUTTON_TYPE_LID) {
215 entry = create_proc_entry(ACPI_BUTTON_FILE_STATE,
4be44fcd 216 S_IRUGO, acpi_device_dir(device));
b34a8030 217 if (!entry)
a6fc6720 218 return -ENODEV;
b34a8030
AS
219 else {
220 entry->proc_fops = &acpi_button_state_fops;
221 entry->data = acpi_driver_data(device);
222 entry->owner = THIS_MODULE;
223 }
224 }
225
d550d98d 226 return 0;
b34a8030
AS
227}
228
4be44fcd 229static int acpi_button_remove_fs(struct acpi_device *device)
b34a8030 230{
4be44fcd 231 struct acpi_button *button = NULL;
b34a8030 232
b34a8030
AS
233
234 button = acpi_driver_data(device);
235 if (acpi_device_dir(device)) {
236 if (button->type == ACPI_BUTTON_TYPE_LID)
237 remove_proc_entry(ACPI_BUTTON_FILE_STATE,
4be44fcd 238 acpi_device_dir(device));
b34a8030 239 remove_proc_entry(ACPI_BUTTON_FILE_INFO,
4be44fcd 240 acpi_device_dir(device));
b34a8030
AS
241
242 remove_proc_entry(acpi_device_bid(device),
4be44fcd 243 acpi_device_dir(device)->parent);
b34a8030
AS
244 acpi_device_dir(device) = NULL;
245 }
246
d550d98d 247 return 0;
b34a8030
AS
248}
249
1da177e4
LT
250/* --------------------------------------------------------------------------
251 Driver Interface
252 -------------------------------------------------------------------------- */
253
4be44fcd 254static void acpi_button_notify(acpi_handle handle, u32 event, void *data)
1da177e4 255{
4be44fcd 256 struct acpi_button *button = (struct acpi_button *)data;
1da177e4 257
1da177e4
LT
258
259 if (!button || !button->device)
d550d98d 260 return;
1da177e4
LT
261
262 switch (event) {
263 case ACPI_BUTTON_NOTIFY_STATUS:
4be44fcd
LB
264 acpi_bus_generate_event(button->device, event,
265 ++button->pushed);
1da177e4
LT
266 break;
267 default:
268 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd 269 "Unsupported event [0x%x]\n", event));
1da177e4
LT
270 break;
271 }
272
d550d98d 273 return;
1da177e4
LT
274}
275
4be44fcd 276static acpi_status acpi_button_notify_fixed(void *data)
1da177e4 277{
4be44fcd
LB
278 struct acpi_button *button = (struct acpi_button *)data;
279
1da177e4 280
b34a8030 281 if (!button)
d550d98d 282 return AE_BAD_PARAMETER;
1da177e4 283
27b1d3e8 284 acpi_button_notify(button->device->handle, ACPI_BUTTON_NOTIFY_STATUS, button);
1da177e4 285
d550d98d 286 return AE_OK;
1da177e4
LT
287}
288
4be44fcd 289static int acpi_button_add(struct acpi_device *device)
1da177e4 290{
4be44fcd
LB
291 int result = 0;
292 acpi_status status = AE_OK;
293 struct acpi_button *button = NULL;
1da177e4 294
1da177e4
LT
295
296 if (!device)
d550d98d 297 return -EINVAL;
1da177e4
LT
298
299 button = kmalloc(sizeof(struct acpi_button), GFP_KERNEL);
300 if (!button)
d550d98d 301 return -ENOMEM;
1da177e4
LT
302 memset(button, 0, sizeof(struct acpi_button));
303
304 button->device = device;
1da177e4
LT
305 acpi_driver_data(device) = button;
306
307 /*
308 * Determine the button type (via hid), as fixed-feature buttons
309 * need to be handled a bit differently than generic-space.
310 */
311 if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWER)) {
312 button->type = ACPI_BUTTON_TYPE_POWER;
4be44fcd
LB
313 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_POWER);
314 sprintf(acpi_device_class(device), "%s/%s",
1da177e4 315 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
4be44fcd 316 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWERF)) {
1da177e4
LT
317 button->type = ACPI_BUTTON_TYPE_POWERF;
318 strcpy(acpi_device_name(device),
4be44fcd
LB
319 ACPI_BUTTON_DEVICE_NAME_POWERF);
320 sprintf(acpi_device_class(device), "%s/%s",
1da177e4 321 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
4be44fcd 322 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEP)) {
1da177e4 323 button->type = ACPI_BUTTON_TYPE_SLEEP;
4be44fcd
LB
324 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_SLEEP);
325 sprintf(acpi_device_class(device), "%s/%s",
1da177e4 326 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
4be44fcd 327 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEPF)) {
1da177e4
LT
328 button->type = ACPI_BUTTON_TYPE_SLEEPF;
329 strcpy(acpi_device_name(device),
4be44fcd
LB
330 ACPI_BUTTON_DEVICE_NAME_SLEEPF);
331 sprintf(acpi_device_class(device), "%s/%s",
1da177e4 332 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
4be44fcd 333 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_LID)) {
1da177e4 334 button->type = ACPI_BUTTON_TYPE_LID;
4be44fcd
LB
335 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_LID);
336 sprintf(acpi_device_class(device), "%s/%s",
1da177e4 337 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
4be44fcd 338 } else {
6468463a
LB
339 printk(KERN_ERR PREFIX "Unsupported hid [%s]\n",
340 acpi_device_hid(device));
1da177e4
LT
341 result = -ENODEV;
342 goto end;
343 }
344
b34a8030
AS
345 result = acpi_button_add_fs(device);
346 if (result)
347 goto end;
348
1da177e4
LT
349 switch (button->type) {
350 case ACPI_BUTTON_TYPE_POWERF:
4be44fcd
LB
351 status =
352 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
353 acpi_button_notify_fixed,
354 button);
1da177e4
LT
355 break;
356 case ACPI_BUTTON_TYPE_SLEEPF:
4be44fcd
LB
357 status =
358 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
359 acpi_button_notify_fixed,
360 button);
1da177e4
LT
361 break;
362 default:
27b1d3e8 363 status = acpi_install_notify_handler(device->handle,
4be44fcd
LB
364 ACPI_DEVICE_NOTIFY,
365 acpi_button_notify,
366 button);
1da177e4
LT
367 break;
368 }
369
370 if (ACPI_FAILURE(status)) {
1da177e4
LT
371 result = -ENODEV;
372 goto end;
373 }
374
375 if (device->wakeup.flags.valid) {
376 /* Button's GPE is run-wake GPE */
4be44fcd
LB
377 acpi_set_gpe_type(device->wakeup.gpe_device,
378 device->wakeup.gpe_number,
379 ACPI_GPE_TYPE_WAKE_RUN);
380 acpi_enable_gpe(device->wakeup.gpe_device,
381 device->wakeup.gpe_number, ACPI_NOT_ISR);
1da177e4
LT
382 device->wakeup.state.enabled = 1;
383 }
384
4be44fcd
LB
385 printk(KERN_INFO PREFIX "%s [%s]\n",
386 acpi_device_name(device), acpi_device_bid(device));
1da177e4 387
4be44fcd 388 end:
1da177e4 389 if (result) {
b34a8030 390 acpi_button_remove_fs(device);
1da177e4
LT
391 kfree(button);
392 }
393
d550d98d 394 return result;
1da177e4
LT
395}
396
4be44fcd 397static int acpi_button_remove(struct acpi_device *device, int type)
1da177e4 398{
4be44fcd
LB
399 acpi_status status = 0;
400 struct acpi_button *button = NULL;
1da177e4 401
1da177e4
LT
402
403 if (!device || !acpi_driver_data(device))
d550d98d 404 return -EINVAL;
1da177e4
LT
405
406 button = acpi_driver_data(device);
407
408 /* Unregister for device notifications. */
409 switch (button->type) {
410 case ACPI_BUTTON_TYPE_POWERF:
4be44fcd
LB
411 status =
412 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
413 acpi_button_notify_fixed);
1da177e4
LT
414 break;
415 case ACPI_BUTTON_TYPE_SLEEPF:
4be44fcd
LB
416 status =
417 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
418 acpi_button_notify_fixed);
1da177e4
LT
419 break;
420 default:
27b1d3e8 421 status = acpi_remove_notify_handler(device->handle,
4be44fcd
LB
422 ACPI_DEVICE_NOTIFY,
423 acpi_button_notify);
1da177e4
LT
424 break;
425 }
426
4be44fcd 427 acpi_button_remove_fs(device);
b34a8030 428
1da177e4
LT
429 kfree(button);
430
d550d98d 431 return 0;
1da177e4
LT
432}
433
4be44fcd 434static int __init acpi_button_init(void)
1da177e4 435{
4be44fcd 436 int result = 0;
1da177e4 437
1da177e4 438
b34a8030
AS
439 acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
440 if (!acpi_button_dir)
d550d98d 441 return -ENODEV;
b34a8030 442 acpi_button_dir->owner = THIS_MODULE;
1da177e4
LT
443 result = acpi_bus_register_driver(&acpi_button_driver);
444 if (result < 0) {
b34a8030 445 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
d550d98d 446 return -ENODEV;
1da177e4
LT
447 }
448
d550d98d 449 return 0;
1da177e4
LT
450}
451
4be44fcd 452static void __exit acpi_button_exit(void)
1da177e4 453{
1da177e4
LT
454
455 acpi_bus_unregister_driver(&acpi_button_driver);
456
4be44fcd 457 if (acpi_power_dir)
b34a8030
AS
458 remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER, acpi_button_dir);
459 if (acpi_sleep_dir)
460 remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP, acpi_button_dir);
461 if (acpi_lid_dir)
462 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
463 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
464
d550d98d 465 return;
1da177e4
LT
466}
467
1da177e4
LT
468module_init(acpi_button_init);
469module_exit(acpi_button_exit);