m68k: use KBUILD_DEFCONFIG
[linux-2.6/verdex.git] / drivers / acpi / button.c
blob6c5da83cdb6807be36db10253bd42c2c332c0402
1 /*
2 * acpi_button.c - ACPI Button Driver ($Revision: 30 $)
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/types.h>
30 #include <linux/proc_fs.h>
31 #include <linux/seq_file.h>
32 #include <linux/input.h>
33 #include <acpi/acpi_bus.h>
34 #include <acpi/acpi_drivers.h>
36 #define ACPI_BUTTON_COMPONENT 0x00080000
37 #define ACPI_BUTTON_CLASS "button"
38 #define ACPI_BUTTON_FILE_INFO "info"
39 #define ACPI_BUTTON_FILE_STATE "state"
40 #define ACPI_BUTTON_TYPE_UNKNOWN 0x00
41 #define ACPI_BUTTON_NOTIFY_STATUS 0x80
43 #define ACPI_BUTTON_SUBCLASS_POWER "power"
44 #define ACPI_BUTTON_HID_POWER "PNP0C0C"
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
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
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
62 #define _COMPONENT ACPI_BUTTON_COMPONENT
63 ACPI_MODULE_NAME("button");
65 MODULE_AUTHOR("Paul Diefenbaugh");
66 MODULE_DESCRIPTION("ACPI Button Driver");
67 MODULE_LICENSE("GPL");
69 static const struct acpi_device_id button_device_ids[] = {
70 {ACPI_BUTTON_HID_LID, 0},
71 {ACPI_BUTTON_HID_SLEEP, 0},
72 {ACPI_BUTTON_HID_SLEEPF, 0},
73 {ACPI_BUTTON_HID_POWER, 0},
74 {ACPI_BUTTON_HID_POWERF, 0},
75 {"", 0},
77 MODULE_DEVICE_TABLE(acpi, button_device_ids);
79 static int acpi_button_add(struct acpi_device *device);
80 static int acpi_button_remove(struct acpi_device *device, int type);
81 static int acpi_button_resume(struct acpi_device *device);
82 static int acpi_button_info_open_fs(struct inode *inode, struct file *file);
83 static int acpi_button_state_open_fs(struct inode *inode, struct file *file);
85 static struct acpi_driver acpi_button_driver = {
86 .name = "button",
87 .class = ACPI_BUTTON_CLASS,
88 .ids = button_device_ids,
89 .ops = {
90 .add = acpi_button_add,
91 .resume = acpi_button_resume,
92 .remove = acpi_button_remove,
96 struct acpi_button {
97 struct acpi_device *device; /* Fixed button kludge */
98 unsigned int type;
99 struct input_dev *input;
100 char phys[32]; /* for input device */
101 unsigned long pushed;
104 static const struct file_operations acpi_button_info_fops = {
105 .open = acpi_button_info_open_fs,
106 .read = seq_read,
107 .llseek = seq_lseek,
108 .release = single_release,
111 static const struct file_operations acpi_button_state_fops = {
112 .open = acpi_button_state_open_fs,
113 .read = seq_read,
114 .llseek = seq_lseek,
115 .release = single_release,
118 /* --------------------------------------------------------------------------
119 FS Interface (/proc)
120 -------------------------------------------------------------------------- */
122 static struct proc_dir_entry *acpi_button_dir;
124 static int acpi_button_info_seq_show(struct seq_file *seq, void *offset)
126 struct acpi_button *button = seq->private;
128 if (!button || !button->device)
129 return 0;
131 seq_printf(seq, "type: %s\n",
132 acpi_device_name(button->device));
134 return 0;
137 static int acpi_button_info_open_fs(struct inode *inode, struct file *file)
139 return single_open(file, acpi_button_info_seq_show, PDE(inode)->data);
142 static int acpi_button_state_seq_show(struct seq_file *seq, void *offset)
144 struct acpi_button *button = seq->private;
145 acpi_status status;
146 unsigned long state;
148 if (!button || !button->device)
149 return 0;
151 status = acpi_evaluate_integer(button->device->handle, "_LID", NULL, &state);
152 seq_printf(seq, "state: %s\n",
153 ACPI_FAILURE(status) ? "unsupported" :
154 (state ? "open" : "closed"));
155 return 0;
158 static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
160 return single_open(file, acpi_button_state_seq_show, PDE(inode)->data);
163 static struct proc_dir_entry *acpi_power_dir;
164 static struct proc_dir_entry *acpi_sleep_dir;
165 static struct proc_dir_entry *acpi_lid_dir;
167 static int acpi_button_add_fs(struct acpi_device *device)
169 struct proc_dir_entry *entry = NULL;
170 struct acpi_button *button;
172 if (!device || !acpi_driver_data(device))
173 return -EINVAL;
175 button = acpi_driver_data(device);
177 switch (button->type) {
178 case ACPI_BUTTON_TYPE_POWER:
179 case ACPI_BUTTON_TYPE_POWERF:
180 if (!acpi_power_dir)
181 acpi_power_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_POWER,
182 acpi_button_dir);
183 entry = acpi_power_dir;
184 break;
185 case ACPI_BUTTON_TYPE_SLEEP:
186 case ACPI_BUTTON_TYPE_SLEEPF:
187 if (!acpi_sleep_dir)
188 acpi_sleep_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_SLEEP,
189 acpi_button_dir);
190 entry = acpi_sleep_dir;
191 break;
192 case ACPI_BUTTON_TYPE_LID:
193 if (!acpi_lid_dir)
194 acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID,
195 acpi_button_dir);
196 entry = acpi_lid_dir;
197 break;
200 if (!entry)
201 return -ENODEV;
202 entry->owner = THIS_MODULE;
204 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), entry);
205 if (!acpi_device_dir(device))
206 return -ENODEV;
207 acpi_device_dir(device)->owner = THIS_MODULE;
209 /* 'info' [R] */
210 entry = create_proc_entry(ACPI_BUTTON_FILE_INFO,
211 S_IRUGO, acpi_device_dir(device));
212 if (!entry)
213 return -ENODEV;
214 else {
215 entry->proc_fops = &acpi_button_info_fops;
216 entry->data = acpi_driver_data(device);
217 entry->owner = THIS_MODULE;
220 /* show lid state [R] */
221 if (button->type == ACPI_BUTTON_TYPE_LID) {
222 entry = create_proc_entry(ACPI_BUTTON_FILE_STATE,
223 S_IRUGO, acpi_device_dir(device));
224 if (!entry)
225 return -ENODEV;
226 else {
227 entry->proc_fops = &acpi_button_state_fops;
228 entry->data = acpi_driver_data(device);
229 entry->owner = THIS_MODULE;
233 return 0;
236 static int acpi_button_remove_fs(struct acpi_device *device)
238 struct acpi_button *button = acpi_driver_data(device);
240 if (acpi_device_dir(device)) {
241 if (button->type == ACPI_BUTTON_TYPE_LID)
242 remove_proc_entry(ACPI_BUTTON_FILE_STATE,
243 acpi_device_dir(device));
244 remove_proc_entry(ACPI_BUTTON_FILE_INFO,
245 acpi_device_dir(device));
247 remove_proc_entry(acpi_device_bid(device),
248 acpi_device_dir(device)->parent);
249 acpi_device_dir(device) = NULL;
252 return 0;
255 /* --------------------------------------------------------------------------
256 Driver Interface
257 -------------------------------------------------------------------------- */
258 static int acpi_lid_send_state(struct acpi_button *button)
260 unsigned long state;
261 acpi_status status;
263 status = acpi_evaluate_integer(button->device->handle, "_LID", NULL,
264 &state);
265 if (ACPI_FAILURE(status))
266 return -ENODEV;
267 /* input layer checks if event is redundant */
268 input_report_switch(button->input, SW_LID, !state);
269 return 0;
272 static void acpi_button_notify(acpi_handle handle, u32 event, void *data)
274 struct acpi_button *button = data;
275 struct input_dev *input;
277 if (!button || !button->device)
278 return;
280 switch (event) {
281 case ACPI_BUTTON_NOTIFY_STATUS:
282 input = button->input;
283 if (button->type == ACPI_BUTTON_TYPE_LID) {
284 acpi_lid_send_state(button);
285 } else {
286 int keycode = test_bit(KEY_SLEEP, input->keybit) ?
287 KEY_SLEEP : KEY_POWER;
289 input_report_key(input, keycode, 1);
290 input_sync(input);
291 input_report_key(input, keycode, 0);
293 input_sync(input);
295 acpi_bus_generate_proc_event(button->device, event,
296 ++button->pushed);
297 break;
298 default:
299 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
300 "Unsupported event [0x%x]\n", event));
301 break;
304 return;
307 static acpi_status acpi_button_notify_fixed(void *data)
309 struct acpi_button *button = data;
311 if (!button)
312 return AE_BAD_PARAMETER;
314 acpi_button_notify(button->device->handle, ACPI_BUTTON_NOTIFY_STATUS, button);
316 return AE_OK;
319 static int acpi_button_install_notify_handlers(struct acpi_button *button)
321 acpi_status status;
323 switch (button->type) {
324 case ACPI_BUTTON_TYPE_POWERF:
325 status =
326 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
327 acpi_button_notify_fixed,
328 button);
329 break;
330 case ACPI_BUTTON_TYPE_SLEEPF:
331 status =
332 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
333 acpi_button_notify_fixed,
334 button);
335 break;
336 default:
337 status = acpi_install_notify_handler(button->device->handle,
338 ACPI_DEVICE_NOTIFY,
339 acpi_button_notify,
340 button);
341 break;
344 return ACPI_FAILURE(status) ? -ENODEV : 0;
347 static int acpi_button_resume(struct acpi_device *device)
349 struct acpi_button *button;
350 if (!device)
351 return -EINVAL;
352 button = acpi_driver_data(device);
353 if (button && button->type == ACPI_BUTTON_TYPE_LID)
354 return acpi_lid_send_state(button);
355 return 0;
358 static void acpi_button_remove_notify_handlers(struct acpi_button *button)
360 switch (button->type) {
361 case ACPI_BUTTON_TYPE_POWERF:
362 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
363 acpi_button_notify_fixed);
364 break;
365 case ACPI_BUTTON_TYPE_SLEEPF:
366 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
367 acpi_button_notify_fixed);
368 break;
369 default:
370 acpi_remove_notify_handler(button->device->handle,
371 ACPI_DEVICE_NOTIFY,
372 acpi_button_notify);
373 break;
377 static int acpi_button_add(struct acpi_device *device)
379 int error;
380 struct acpi_button *button;
381 struct input_dev *input;
383 if (!device)
384 return -EINVAL;
386 button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
387 if (!button)
388 return -ENOMEM;
390 button->device = device;
391 acpi_driver_data(device) = button;
393 button->input = input = input_allocate_device();
394 if (!input) {
395 error = -ENOMEM;
396 goto err_free_button;
400 * Determine the button type (via hid), as fixed-feature buttons
401 * need to be handled a bit differently than generic-space.
403 if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWER)) {
404 button->type = ACPI_BUTTON_TYPE_POWER;
405 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_POWER);
406 sprintf(acpi_device_class(device), "%s/%s",
407 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
408 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWERF)) {
409 button->type = ACPI_BUTTON_TYPE_POWERF;
410 strcpy(acpi_device_name(device),
411 ACPI_BUTTON_DEVICE_NAME_POWERF);
412 sprintf(acpi_device_class(device), "%s/%s",
413 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
414 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEP)) {
415 button->type = ACPI_BUTTON_TYPE_SLEEP;
416 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_SLEEP);
417 sprintf(acpi_device_class(device), "%s/%s",
418 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
419 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEPF)) {
420 button->type = ACPI_BUTTON_TYPE_SLEEPF;
421 strcpy(acpi_device_name(device),
422 ACPI_BUTTON_DEVICE_NAME_SLEEPF);
423 sprintf(acpi_device_class(device), "%s/%s",
424 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
425 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_LID)) {
426 button->type = ACPI_BUTTON_TYPE_LID;
427 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_LID);
428 sprintf(acpi_device_class(device), "%s/%s",
429 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
430 } else {
431 printk(KERN_ERR PREFIX "Unsupported hid [%s]\n",
432 acpi_device_hid(device));
433 error = -ENODEV;
434 goto err_free_input;
437 error = acpi_button_add_fs(device);
438 if (error)
439 goto err_free_input;
441 error = acpi_button_install_notify_handlers(button);
442 if (error)
443 goto err_remove_fs;
445 snprintf(button->phys, sizeof(button->phys),
446 "%s/button/input0", acpi_device_hid(device));
448 input->name = acpi_device_name(device);
449 input->phys = button->phys;
450 input->id.bustype = BUS_HOST;
451 input->id.product = button->type;
452 input->dev.parent = &device->dev;
454 switch (button->type) {
455 case ACPI_BUTTON_TYPE_POWER:
456 case ACPI_BUTTON_TYPE_POWERF:
457 input->evbit[0] = BIT_MASK(EV_KEY);
458 set_bit(KEY_POWER, input->keybit);
459 break;
461 case ACPI_BUTTON_TYPE_SLEEP:
462 case ACPI_BUTTON_TYPE_SLEEPF:
463 input->evbit[0] = BIT_MASK(EV_KEY);
464 set_bit(KEY_SLEEP, input->keybit);
465 break;
467 case ACPI_BUTTON_TYPE_LID:
468 input->evbit[0] = BIT_MASK(EV_SW);
469 set_bit(SW_LID, input->swbit);
470 break;
473 error = input_register_device(input);
474 if (error)
475 goto err_remove_handlers;
476 if (button->type == ACPI_BUTTON_TYPE_LID)
477 acpi_lid_send_state(button);
479 if (device->wakeup.flags.valid) {
480 /* Button's GPE is run-wake GPE */
481 acpi_set_gpe_type(device->wakeup.gpe_device,
482 device->wakeup.gpe_number,
483 ACPI_GPE_TYPE_WAKE_RUN);
484 acpi_enable_gpe(device->wakeup.gpe_device,
485 device->wakeup.gpe_number, ACPI_NOT_ISR);
486 device->wakeup.state.enabled = 1;
489 printk(KERN_INFO PREFIX "%s [%s]\n",
490 acpi_device_name(device), acpi_device_bid(device));
492 return 0;
494 err_remove_handlers:
495 acpi_button_remove_notify_handlers(button);
496 err_remove_fs:
497 acpi_button_remove_fs(device);
498 err_free_input:
499 input_free_device(input);
500 err_free_button:
501 kfree(button);
502 return error;
505 static int acpi_button_remove(struct acpi_device *device, int type)
507 struct acpi_button *button;
509 if (!device || !acpi_driver_data(device))
510 return -EINVAL;
512 button = acpi_driver_data(device);
514 acpi_button_remove_notify_handlers(button);
515 acpi_button_remove_fs(device);
516 input_unregister_device(button->input);
517 kfree(button);
519 return 0;
522 static int __init acpi_button_init(void)
524 int result;
526 acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
527 if (!acpi_button_dir)
528 return -ENODEV;
529 acpi_button_dir->owner = THIS_MODULE;
530 result = acpi_bus_register_driver(&acpi_button_driver);
531 if (result < 0) {
532 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
533 return -ENODEV;
536 return 0;
539 static void __exit acpi_button_exit(void)
541 acpi_bus_unregister_driver(&acpi_button_driver);
543 if (acpi_power_dir)
544 remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER, acpi_button_dir);
545 if (acpi_sleep_dir)
546 remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP, acpi_button_dir);
547 if (acpi_lid_dir)
548 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
549 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
552 module_init(acpi_button_init);
553 module_exit(acpi_button_exit);