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_CLASS "button"
37 #define ACPI_BUTTON_FILE_INFO "info"
38 #define ACPI_BUTTON_FILE_STATE "state"
39 #define ACPI_BUTTON_TYPE_UNKNOWN 0x00
40 #define ACPI_BUTTON_NOTIFY_STATUS 0x80
42 #define ACPI_BUTTON_SUBCLASS_POWER "power"
43 #define ACPI_BUTTON_HID_POWER "PNP0C0C"
44 #define ACPI_BUTTON_DEVICE_NAME_POWER "Power Button (CM)"
45 #define ACPI_BUTTON_DEVICE_NAME_POWERF "Power Button (FF)"
46 #define ACPI_BUTTON_TYPE_POWER 0x01
47 #define ACPI_BUTTON_TYPE_POWERF 0x02
49 #define ACPI_BUTTON_SUBCLASS_SLEEP "sleep"
50 #define ACPI_BUTTON_HID_SLEEP "PNP0C0E"
51 #define ACPI_BUTTON_DEVICE_NAME_SLEEP "Sleep Button (CM)"
52 #define ACPI_BUTTON_DEVICE_NAME_SLEEPF "Sleep Button (FF)"
53 #define ACPI_BUTTON_TYPE_SLEEP 0x03
54 #define ACPI_BUTTON_TYPE_SLEEPF 0x04
56 #define ACPI_BUTTON_SUBCLASS_LID "lid"
57 #define ACPI_BUTTON_HID_LID "PNP0C0D"
58 #define ACPI_BUTTON_DEVICE_NAME_LID "Lid Switch"
59 #define ACPI_BUTTON_TYPE_LID 0x05
61 #define _COMPONENT ACPI_BUTTON_COMPONENT
62 ACPI_MODULE_NAME("button");
64 MODULE_AUTHOR("Paul Diefenbaugh");
65 MODULE_DESCRIPTION("ACPI Button Driver");
66 MODULE_LICENSE("GPL");
68 static const struct acpi_device_id button_device_ids[] = {
69 {ACPI_BUTTON_HID_LID, 0},
70 {ACPI_BUTTON_HID_SLEEP, 0},
71 {ACPI_BUTTON_HID_SLEEPF, 0},
72 {ACPI_BUTTON_HID_POWER, 0},
73 {ACPI_BUTTON_HID_POWERF, 0},
76 MODULE_DEVICE_TABLE(acpi, button_device_ids);
78 static int acpi_button_add(struct acpi_device *device);
79 static int acpi_button_remove(struct acpi_device *device, int type);
80 static int acpi_button_resume(struct acpi_device *device);
81 static int acpi_button_info_open_fs(struct inode *inode, struct file *file);
82 static int acpi_button_state_open_fs(struct inode *inode, struct file *file);
84 static struct acpi_driver acpi_button_driver = {
86 .class = ACPI_BUTTON_CLASS,
87 .ids = button_device_ids,
89 .add = acpi_button_add,
90 .resume = acpi_button_resume,
91 .remove = acpi_button_remove,
96 struct acpi_device *device; /* Fixed button kludge */
98 struct input_dev *input;
99 char phys[32]; /* for input device */
100 unsigned long pushed;
103 static const struct file_operations acpi_button_info_fops = {
104 .owner = THIS_MODULE,
105 .open = acpi_button_info_open_fs,
108 .release = single_release,
111 static const struct file_operations acpi_button_state_fops = {
112 .owner = THIS_MODULE,
113 .open = acpi_button_state_open_fs,
116 .release = single_release,
119 /* --------------------------------------------------------------------------
121 -------------------------------------------------------------------------- */
123 static struct proc_dir_entry *acpi_button_dir;
125 static int acpi_button_info_seq_show(struct seq_file *seq, void *offset)
127 struct acpi_button *button = seq->private;
129 if (!button || !button->device)
132 seq_printf(seq, "type: %s\n",
133 acpi_device_name(button->device));
138 static int acpi_button_info_open_fs(struct inode *inode, struct file *file)
140 return single_open(file, acpi_button_info_seq_show, PDE(inode)->data);
143 static int acpi_button_state_seq_show(struct seq_file *seq, void *offset)
145 struct acpi_button *button = seq->private;
147 unsigned long long state;
149 if (!button || !button->device)
152 status = acpi_evaluate_integer(button->device->handle, "_LID", NULL, &state);
153 seq_printf(seq, "state: %s\n",
154 ACPI_FAILURE(status) ? "unsupported" :
155 (state ? "open" : "closed"));
159 static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
161 return single_open(file, acpi_button_state_seq_show, PDE(inode)->data);
164 static struct proc_dir_entry *acpi_power_dir;
165 static struct proc_dir_entry *acpi_sleep_dir;
166 static struct proc_dir_entry *acpi_lid_dir;
168 static int acpi_button_add_fs(struct acpi_device *device)
170 struct proc_dir_entry *entry = NULL;
171 struct acpi_button *button;
173 if (!device || !acpi_driver_data(device))
176 button = acpi_driver_data(device);
178 switch (button->type) {
179 case ACPI_BUTTON_TYPE_POWER:
180 case ACPI_BUTTON_TYPE_POWERF:
182 acpi_power_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_POWER,
184 entry = acpi_power_dir;
186 case ACPI_BUTTON_TYPE_SLEEP:
187 case ACPI_BUTTON_TYPE_SLEEPF:
189 acpi_sleep_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_SLEEP,
191 entry = acpi_sleep_dir;
193 case ACPI_BUTTON_TYPE_LID:
195 acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID,
197 entry = acpi_lid_dir;
204 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), entry);
205 if (!acpi_device_dir(device))
209 entry = proc_create_data(ACPI_BUTTON_FILE_INFO,
210 S_IRUGO, acpi_device_dir(device),
211 &acpi_button_info_fops,
212 acpi_driver_data(device));
216 /* show lid state [R] */
217 if (button->type == ACPI_BUTTON_TYPE_LID) {
218 entry = proc_create_data(ACPI_BUTTON_FILE_STATE,
219 S_IRUGO, acpi_device_dir(device),
220 &acpi_button_state_fops,
221 acpi_driver_data(device));
229 static int acpi_button_remove_fs(struct acpi_device *device)
231 struct acpi_button *button = acpi_driver_data(device);
233 if (acpi_device_dir(device)) {
234 if (button->type == ACPI_BUTTON_TYPE_LID)
235 remove_proc_entry(ACPI_BUTTON_FILE_STATE,
236 acpi_device_dir(device));
237 remove_proc_entry(ACPI_BUTTON_FILE_INFO,
238 acpi_device_dir(device));
240 remove_proc_entry(acpi_device_bid(device),
241 acpi_device_dir(device)->parent);
242 acpi_device_dir(device) = NULL;
248 /* --------------------------------------------------------------------------
250 -------------------------------------------------------------------------- */
251 static int acpi_lid_send_state(struct acpi_button *button)
253 unsigned long long state;
256 status = acpi_evaluate_integer(button->device->handle, "_LID", NULL,
258 if (ACPI_FAILURE(status))
260 /* input layer checks if event is redundant */
261 input_report_switch(button->input, SW_LID, !state);
262 input_sync(button->input);
266 static void acpi_button_notify(acpi_handle handle, u32 event, void *data)
268 struct acpi_button *button = data;
269 struct input_dev *input;
271 if (!button || !button->device)
275 case ACPI_BUTTON_NOTIFY_STATUS:
276 input = button->input;
277 if (button->type == ACPI_BUTTON_TYPE_LID) {
278 acpi_lid_send_state(button);
280 int keycode = test_bit(KEY_SLEEP, input->keybit) ?
281 KEY_SLEEP : KEY_POWER;
283 input_report_key(input, keycode, 1);
285 input_report_key(input, keycode, 0);
289 acpi_bus_generate_proc_event(button->device, event,
293 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
294 "Unsupported event [0x%x]\n", event));
301 static acpi_status acpi_button_notify_fixed(void *data)
303 struct acpi_button *button = data;
306 return AE_BAD_PARAMETER;
308 acpi_button_notify(button->device->handle, ACPI_BUTTON_NOTIFY_STATUS, button);
313 static int acpi_button_install_notify_handlers(struct acpi_button *button)
317 switch (button->type) {
318 case ACPI_BUTTON_TYPE_POWERF:
320 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
321 acpi_button_notify_fixed,
324 case ACPI_BUTTON_TYPE_SLEEPF:
326 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
327 acpi_button_notify_fixed,
331 status = acpi_install_notify_handler(button->device->handle,
338 return ACPI_FAILURE(status) ? -ENODEV : 0;
341 static int acpi_button_resume(struct acpi_device *device)
343 struct acpi_button *button;
346 button = acpi_driver_data(device);
347 if (button && button->type == ACPI_BUTTON_TYPE_LID)
348 return acpi_lid_send_state(button);
352 static void acpi_button_remove_notify_handlers(struct acpi_button *button)
354 switch (button->type) {
355 case ACPI_BUTTON_TYPE_POWERF:
356 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
357 acpi_button_notify_fixed);
359 case ACPI_BUTTON_TYPE_SLEEPF:
360 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
361 acpi_button_notify_fixed);
364 acpi_remove_notify_handler(button->device->handle,
371 static int acpi_button_add(struct acpi_device *device)
374 struct acpi_button *button;
375 struct input_dev *input;
380 button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
384 button->device = device;
385 device->driver_data = button;
387 button->input = input = input_allocate_device();
390 goto err_free_button;
394 * Determine the button type (via hid), as fixed-feature buttons
395 * need to be handled a bit differently than generic-space.
397 if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWER)) {
398 button->type = ACPI_BUTTON_TYPE_POWER;
399 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_POWER);
400 sprintf(acpi_device_class(device), "%s/%s",
401 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
402 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWERF)) {
403 button->type = ACPI_BUTTON_TYPE_POWERF;
404 strcpy(acpi_device_name(device),
405 ACPI_BUTTON_DEVICE_NAME_POWERF);
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_SLEEP)) {
409 button->type = ACPI_BUTTON_TYPE_SLEEP;
410 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_SLEEP);
411 sprintf(acpi_device_class(device), "%s/%s",
412 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
413 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEPF)) {
414 button->type = ACPI_BUTTON_TYPE_SLEEPF;
415 strcpy(acpi_device_name(device),
416 ACPI_BUTTON_DEVICE_NAME_SLEEPF);
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_LID)) {
420 button->type = ACPI_BUTTON_TYPE_LID;
421 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_LID);
422 sprintf(acpi_device_class(device), "%s/%s",
423 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
425 printk(KERN_ERR PREFIX "Unsupported hid [%s]\n",
426 acpi_device_hid(device));
431 error = acpi_button_add_fs(device);
435 error = acpi_button_install_notify_handlers(button);
439 snprintf(button->phys, sizeof(button->phys),
440 "%s/button/input0", acpi_device_hid(device));
442 input->name = acpi_device_name(device);
443 input->phys = button->phys;
444 input->id.bustype = BUS_HOST;
445 input->id.product = button->type;
446 input->dev.parent = &device->dev;
448 switch (button->type) {
449 case ACPI_BUTTON_TYPE_POWER:
450 case ACPI_BUTTON_TYPE_POWERF:
451 input->evbit[0] = BIT_MASK(EV_KEY);
452 set_bit(KEY_POWER, input->keybit);
455 case ACPI_BUTTON_TYPE_SLEEP:
456 case ACPI_BUTTON_TYPE_SLEEPF:
457 input->evbit[0] = BIT_MASK(EV_KEY);
458 set_bit(KEY_SLEEP, input->keybit);
461 case ACPI_BUTTON_TYPE_LID:
462 input->evbit[0] = BIT_MASK(EV_SW);
463 set_bit(SW_LID, input->swbit);
467 error = input_register_device(input);
469 goto err_remove_handlers;
470 if (button->type == ACPI_BUTTON_TYPE_LID)
471 acpi_lid_send_state(button);
473 if (device->wakeup.flags.valid) {
474 /* Button's GPE is run-wake GPE */
475 acpi_set_gpe_type(device->wakeup.gpe_device,
476 device->wakeup.gpe_number,
477 ACPI_GPE_TYPE_WAKE_RUN);
478 acpi_enable_gpe(device->wakeup.gpe_device,
479 device->wakeup.gpe_number);
480 device->wakeup.state.enabled = 1;
483 printk(KERN_INFO PREFIX "%s [%s]\n",
484 acpi_device_name(device), acpi_device_bid(device));
489 acpi_button_remove_notify_handlers(button);
491 acpi_button_remove_fs(device);
493 input_free_device(input);
499 static int acpi_button_remove(struct acpi_device *device, int type)
501 struct acpi_button *button;
503 if (!device || !acpi_driver_data(device))
506 button = acpi_driver_data(device);
508 acpi_button_remove_notify_handlers(button);
509 acpi_button_remove_fs(device);
510 input_unregister_device(button->input);
516 static int __init acpi_button_init(void)
520 acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
521 if (!acpi_button_dir)
523 result = acpi_bus_register_driver(&acpi_button_driver);
525 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
532 static void __exit acpi_button_exit(void)
534 acpi_bus_unregister_driver(&acpi_button_driver);
537 remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER, acpi_button_dir);
539 remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP, acpi_button_dir);
541 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
542 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
545 module_init(acpi_button_init);
546 module_exit(acpi_button_exit);