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 int acpi_button_add(struct acpi_device *device);
70 static int acpi_button_remove(struct acpi_device *device, int type);
71 static int acpi_button_info_open_fs(struct inode *inode, struct file *file);
72 static int acpi_button_state_open_fs(struct inode *inode, struct file *file);
74 static struct acpi_driver acpi_button_driver = {
76 .class = ACPI_BUTTON_CLASS,
77 .ids = "button_power,button_sleep,PNP0C0D,PNP0C0C,PNP0C0E",
79 .add = acpi_button_add,
80 .remove = acpi_button_remove,
85 struct acpi_device *device; /* Fixed button kludge */
87 struct input_dev *input;
88 char phys[32]; /* for input device */
92 static const struct file_operations acpi_button_info_fops = {
93 .open = acpi_button_info_open_fs,
96 .release = single_release,
99 static const struct file_operations acpi_button_state_fops = {
100 .open = acpi_button_state_open_fs,
103 .release = single_release,
106 /* --------------------------------------------------------------------------
108 -------------------------------------------------------------------------- */
110 static struct proc_dir_entry *acpi_button_dir;
112 static int acpi_button_info_seq_show(struct seq_file *seq, void *offset)
114 struct acpi_button *button = seq->private;
116 if (!button || !button->device)
119 seq_printf(seq, "type: %s\n",
120 acpi_device_name(button->device));
125 static int acpi_button_info_open_fs(struct inode *inode, struct file *file)
127 return single_open(file, acpi_button_info_seq_show, PDE(inode)->data);
130 static int acpi_button_state_seq_show(struct seq_file *seq, void *offset)
132 struct acpi_button *button = seq->private;
136 if (!button || !button->device)
139 status = acpi_evaluate_integer(button->device->handle, "_LID", NULL, &state);
140 seq_printf(seq, "state: %s\n",
141 ACPI_FAILURE(status) ? "unsupported" :
142 (state ? "open" : "closed"));
146 static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
148 return single_open(file, acpi_button_state_seq_show, PDE(inode)->data);
151 static struct proc_dir_entry *acpi_power_dir;
152 static struct proc_dir_entry *acpi_sleep_dir;
153 static struct proc_dir_entry *acpi_lid_dir;
155 static int acpi_button_add_fs(struct acpi_device *device)
157 struct proc_dir_entry *entry = NULL;
158 struct acpi_button *button;
160 if (!device || !acpi_driver_data(device))
163 button = acpi_driver_data(device);
165 switch (button->type) {
166 case ACPI_BUTTON_TYPE_POWER:
167 case ACPI_BUTTON_TYPE_POWERF:
169 acpi_power_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_POWER,
171 entry = acpi_power_dir;
173 case ACPI_BUTTON_TYPE_SLEEP:
174 case ACPI_BUTTON_TYPE_SLEEPF:
176 acpi_sleep_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_SLEEP,
178 entry = acpi_sleep_dir;
180 case ACPI_BUTTON_TYPE_LID:
182 acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID,
184 entry = acpi_lid_dir;
190 entry->owner = THIS_MODULE;
192 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), entry);
193 if (!acpi_device_dir(device))
195 acpi_device_dir(device)->owner = THIS_MODULE;
198 entry = create_proc_entry(ACPI_BUTTON_FILE_INFO,
199 S_IRUGO, acpi_device_dir(device));
203 entry->proc_fops = &acpi_button_info_fops;
204 entry->data = acpi_driver_data(device);
205 entry->owner = THIS_MODULE;
208 /* show lid state [R] */
209 if (button->type == ACPI_BUTTON_TYPE_LID) {
210 entry = create_proc_entry(ACPI_BUTTON_FILE_STATE,
211 S_IRUGO, acpi_device_dir(device));
215 entry->proc_fops = &acpi_button_state_fops;
216 entry->data = acpi_driver_data(device);
217 entry->owner = THIS_MODULE;
224 static int acpi_button_remove_fs(struct acpi_device *device)
226 struct acpi_button *button = acpi_driver_data(device);
228 if (acpi_device_dir(device)) {
229 if (button->type == ACPI_BUTTON_TYPE_LID)
230 remove_proc_entry(ACPI_BUTTON_FILE_STATE,
231 acpi_device_dir(device));
232 remove_proc_entry(ACPI_BUTTON_FILE_INFO,
233 acpi_device_dir(device));
235 remove_proc_entry(acpi_device_bid(device),
236 acpi_device_dir(device)->parent);
237 acpi_device_dir(device) = NULL;
243 /* --------------------------------------------------------------------------
245 -------------------------------------------------------------------------- */
247 static void acpi_button_notify(acpi_handle handle, u32 event, void *data)
249 struct acpi_button *button = data;
250 struct input_dev *input;
252 if (!button || !button->device)
256 case ACPI_BUTTON_NOTIFY_STATUS:
257 input = button->input;
259 if (button->type == ACPI_BUTTON_TYPE_LID) {
260 struct acpi_handle *handle = button->device->handle;
263 if (!ACPI_FAILURE(acpi_evaluate_integer(handle, "_LID",
265 input_report_switch(input, SW_LID, !state);
268 int keycode = test_bit(KEY_SLEEP, input->keybit) ?
269 KEY_SLEEP : KEY_POWER;
271 input_report_key(input, keycode, 1);
273 input_report_key(input, keycode, 0);
277 acpi_bus_generate_event(button->device, event,
281 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
282 "Unsupported event [0x%x]\n", event));
289 static acpi_status acpi_button_notify_fixed(void *data)
291 struct acpi_button *button = data;
294 return AE_BAD_PARAMETER;
296 acpi_button_notify(button->device->handle, ACPI_BUTTON_NOTIFY_STATUS, button);
301 static int acpi_button_install_notify_handlers(struct acpi_button *button)
305 switch (button->type) {
306 case ACPI_BUTTON_TYPE_POWERF:
308 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
309 acpi_button_notify_fixed,
312 case ACPI_BUTTON_TYPE_SLEEPF:
314 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
315 acpi_button_notify_fixed,
319 status = acpi_install_notify_handler(button->device->handle,
326 return ACPI_FAILURE(status) ? -ENODEV : 0;
329 static void acpi_button_remove_notify_handlers(struct acpi_button *button)
331 switch (button->type) {
332 case ACPI_BUTTON_TYPE_POWERF:
333 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
334 acpi_button_notify_fixed);
336 case ACPI_BUTTON_TYPE_SLEEPF:
337 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
338 acpi_button_notify_fixed);
341 acpi_remove_notify_handler(button->device->handle,
348 static int acpi_button_add(struct acpi_device *device)
351 struct acpi_button *button;
352 struct input_dev *input;
357 button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
361 button->device = device;
362 acpi_driver_data(device) = button;
364 button->input = input = input_allocate_device();
367 goto err_free_button;
371 * Determine the button type (via hid), as fixed-feature buttons
372 * need to be handled a bit differently than generic-space.
374 if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWER)) {
375 button->type = ACPI_BUTTON_TYPE_POWER;
376 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_POWER);
377 sprintf(acpi_device_class(device), "%s/%s",
378 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
379 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWERF)) {
380 button->type = ACPI_BUTTON_TYPE_POWERF;
381 strcpy(acpi_device_name(device),
382 ACPI_BUTTON_DEVICE_NAME_POWERF);
383 sprintf(acpi_device_class(device), "%s/%s",
384 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
385 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEP)) {
386 button->type = ACPI_BUTTON_TYPE_SLEEP;
387 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_SLEEP);
388 sprintf(acpi_device_class(device), "%s/%s",
389 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
390 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEPF)) {
391 button->type = ACPI_BUTTON_TYPE_SLEEPF;
392 strcpy(acpi_device_name(device),
393 ACPI_BUTTON_DEVICE_NAME_SLEEPF);
394 sprintf(acpi_device_class(device), "%s/%s",
395 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
396 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_LID)) {
397 button->type = ACPI_BUTTON_TYPE_LID;
398 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_LID);
399 sprintf(acpi_device_class(device), "%s/%s",
400 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
402 printk(KERN_ERR PREFIX "Unsupported hid [%s]\n",
403 acpi_device_hid(device));
408 error = acpi_button_add_fs(device);
412 error = acpi_button_install_notify_handlers(button);
416 snprintf(button->phys, sizeof(button->phys),
417 "%s/button/input0", acpi_device_hid(device));
419 input->name = acpi_device_name(device);
420 input->phys = button->phys;
421 input->id.bustype = BUS_HOST;
422 input->id.product = button->type;
424 switch (button->type) {
425 case ACPI_BUTTON_TYPE_POWER:
426 case ACPI_BUTTON_TYPE_POWERF:
427 input->evbit[0] = BIT(EV_KEY);
428 set_bit(KEY_POWER, input->keybit);
431 case ACPI_BUTTON_TYPE_SLEEP:
432 case ACPI_BUTTON_TYPE_SLEEPF:
433 input->evbit[0] = BIT(EV_KEY);
434 set_bit(KEY_SLEEP, input->keybit);
437 case ACPI_BUTTON_TYPE_LID:
438 input->evbit[0] = BIT(EV_SW);
439 set_bit(SW_LID, input->swbit);
443 error = input_register_device(input);
445 goto err_remove_handlers;
447 if (device->wakeup.flags.valid) {
448 /* Button's GPE is run-wake GPE */
449 acpi_set_gpe_type(device->wakeup.gpe_device,
450 device->wakeup.gpe_number,
451 ACPI_GPE_TYPE_WAKE_RUN);
452 acpi_enable_gpe(device->wakeup.gpe_device,
453 device->wakeup.gpe_number, ACPI_NOT_ISR);
454 device->wakeup.state.enabled = 1;
457 printk(KERN_INFO PREFIX "%s [%s]\n",
458 acpi_device_name(device), acpi_device_bid(device));
463 acpi_button_remove_notify_handlers(button);
465 acpi_button_remove_fs(device);
467 input_free_device(input);
473 static int acpi_button_remove(struct acpi_device *device, int type)
475 struct acpi_button *button;
477 if (!device || !acpi_driver_data(device))
480 button = acpi_driver_data(device);
482 acpi_button_remove_notify_handlers(button);
483 acpi_button_remove_fs(device);
484 input_unregister_device(button->input);
490 static int __init acpi_button_init(void)
494 acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
495 if (!acpi_button_dir)
497 acpi_button_dir->owner = THIS_MODULE;
498 result = acpi_bus_register_driver(&acpi_button_driver);
500 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
507 static void __exit acpi_button_exit(void)
509 acpi_bus_unregister_driver(&acpi_button_driver);
512 remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER, acpi_button_dir);
514 remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP, acpi_button_dir);
516 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
517 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
520 module_init(acpi_button_init);
521 module_exit(acpi_button_exit);