2 * button.c - ACPI Button Driver
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"
45 #define ACPI_BUTTON_TYPE_POWER 0x01
47 #define ACPI_BUTTON_SUBCLASS_SLEEP "sleep"
48 #define ACPI_BUTTON_HID_SLEEP "PNP0C0E"
49 #define ACPI_BUTTON_DEVICE_NAME_SLEEP "Sleep Button"
50 #define ACPI_BUTTON_TYPE_SLEEP 0x03
52 #define ACPI_BUTTON_SUBCLASS_LID "lid"
53 #define ACPI_BUTTON_HID_LID "PNP0C0D"
54 #define ACPI_BUTTON_DEVICE_NAME_LID "Lid Switch"
55 #define ACPI_BUTTON_TYPE_LID 0x05
57 #define _COMPONENT ACPI_BUTTON_COMPONENT
58 ACPI_MODULE_NAME("button");
60 MODULE_AUTHOR("Paul Diefenbaugh");
61 MODULE_DESCRIPTION("ACPI Button Driver");
62 MODULE_LICENSE("GPL");
64 static const struct acpi_device_id button_device_ids[] = {
65 {ACPI_BUTTON_HID_LID, 0},
66 {ACPI_BUTTON_HID_SLEEP, 0},
67 {ACPI_BUTTON_HID_SLEEPF, 0},
68 {ACPI_BUTTON_HID_POWER, 0},
69 {ACPI_BUTTON_HID_POWERF, 0},
72 MODULE_DEVICE_TABLE(acpi, button_device_ids);
74 static int acpi_button_add(struct acpi_device *device);
75 static int acpi_button_remove(struct acpi_device *device, int type);
76 static int acpi_button_resume(struct acpi_device *device);
77 static void acpi_button_notify(struct acpi_device *device, u32 event);
78 static int acpi_button_info_open_fs(struct inode *inode, struct file *file);
79 static int acpi_button_state_open_fs(struct inode *inode, struct file *file);
81 static struct acpi_driver acpi_button_driver = {
83 .class = ACPI_BUTTON_CLASS,
84 .ids = button_device_ids,
86 .add = acpi_button_add,
87 .resume = acpi_button_resume,
88 .remove = acpi_button_remove,
89 .notify = acpi_button_notify,
95 struct input_dev *input;
96 char phys[32]; /* for input device */
100 static const struct file_operations acpi_button_info_fops = {
101 .owner = THIS_MODULE,
102 .open = acpi_button_info_open_fs,
105 .release = single_release,
108 static const struct file_operations acpi_button_state_fops = {
109 .owner = THIS_MODULE,
110 .open = acpi_button_state_open_fs,
113 .release = single_release,
116 static BLOCKING_NOTIFIER_HEAD(acpi_lid_notifier);
117 static struct acpi_device *lid_device;
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_device *device = seq->private;
129 seq_printf(seq, "type: %s\n",
130 acpi_device_name(device));
134 static int acpi_button_info_open_fs(struct inode *inode, struct file *file)
136 return single_open(file, acpi_button_info_seq_show, PDE(inode)->data);
139 static int acpi_button_state_seq_show(struct seq_file *seq, void *offset)
141 struct acpi_device *device = seq->private;
143 unsigned long long state;
145 status = acpi_evaluate_integer(device->handle, "_LID", NULL, &state);
146 seq_printf(seq, "state: %s\n",
147 ACPI_FAILURE(status) ? "unsupported" :
148 (state ? "open" : "closed"));
152 static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
154 return single_open(file, acpi_button_state_seq_show, PDE(inode)->data);
157 static struct proc_dir_entry *acpi_power_dir;
158 static struct proc_dir_entry *acpi_sleep_dir;
159 static struct proc_dir_entry *acpi_lid_dir;
161 static int acpi_button_add_fs(struct acpi_device *device)
163 struct acpi_button *button = acpi_driver_data(device);
164 struct proc_dir_entry *entry = NULL;
166 switch (button->type) {
167 case ACPI_BUTTON_TYPE_POWER:
169 acpi_power_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_POWER,
171 entry = acpi_power_dir;
173 case ACPI_BUTTON_TYPE_SLEEP:
175 acpi_sleep_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_SLEEP,
177 entry = acpi_sleep_dir;
179 case ACPI_BUTTON_TYPE_LID:
181 acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID,
183 entry = acpi_lid_dir;
190 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), entry);
191 if (!acpi_device_dir(device))
195 entry = proc_create_data(ACPI_BUTTON_FILE_INFO,
196 S_IRUGO, acpi_device_dir(device),
197 &acpi_button_info_fops, device);
201 /* show lid state [R] */
202 if (button->type == ACPI_BUTTON_TYPE_LID) {
203 entry = proc_create_data(ACPI_BUTTON_FILE_STATE,
204 S_IRUGO, acpi_device_dir(device),
205 &acpi_button_state_fops, device);
213 static int acpi_button_remove_fs(struct acpi_device *device)
215 struct acpi_button *button = acpi_driver_data(device);
217 if (acpi_device_dir(device)) {
218 if (button->type == ACPI_BUTTON_TYPE_LID)
219 remove_proc_entry(ACPI_BUTTON_FILE_STATE,
220 acpi_device_dir(device));
221 remove_proc_entry(ACPI_BUTTON_FILE_INFO,
222 acpi_device_dir(device));
224 remove_proc_entry(acpi_device_bid(device),
225 acpi_device_dir(device)->parent);
226 acpi_device_dir(device) = NULL;
232 /* --------------------------------------------------------------------------
234 -------------------------------------------------------------------------- */
235 int acpi_lid_notifier_register(struct notifier_block *nb)
237 return blocking_notifier_chain_register(&acpi_lid_notifier, nb);
239 EXPORT_SYMBOL(acpi_lid_notifier_register);
241 int acpi_lid_notifier_unregister(struct notifier_block *nb)
243 return blocking_notifier_chain_unregister(&acpi_lid_notifier, nb);
245 EXPORT_SYMBOL(acpi_lid_notifier_unregister);
247 int acpi_lid_open(void)
250 unsigned long long state;
252 status = acpi_evaluate_integer(lid_device->handle, "_LID", NULL,
254 if (ACPI_FAILURE(status))
259 EXPORT_SYMBOL(acpi_lid_open);
261 static int acpi_lid_send_state(struct acpi_device *device)
263 struct acpi_button *button = acpi_driver_data(device);
264 unsigned long long state;
268 status = acpi_evaluate_integer(device->handle, "_LID", NULL, &state);
269 if (ACPI_FAILURE(status))
272 /* input layer checks if event is redundant */
273 input_report_switch(button->input, SW_LID, !state);
274 input_sync(button->input);
276 ret = blocking_notifier_call_chain(&acpi_lid_notifier, state, device);
277 if (ret == NOTIFY_DONE)
278 ret = blocking_notifier_call_chain(&acpi_lid_notifier, state,
283 static void acpi_button_notify(struct acpi_device *device, u32 event)
285 struct acpi_button *button = acpi_driver_data(device);
286 struct input_dev *input;
289 case ACPI_FIXED_HARDWARE_EVENT:
290 event = ACPI_BUTTON_NOTIFY_STATUS;
292 case ACPI_BUTTON_NOTIFY_STATUS:
293 input = button->input;
294 if (button->type == ACPI_BUTTON_TYPE_LID) {
295 acpi_lid_send_state(device);
297 int keycode = test_bit(KEY_SLEEP, input->keybit) ?
298 KEY_SLEEP : KEY_POWER;
300 input_report_key(input, keycode, 1);
302 input_report_key(input, keycode, 0);
306 acpi_bus_generate_proc_event(device, event, ++button->pushed);
309 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
310 "Unsupported event [0x%x]\n", event));
315 static int acpi_button_resume(struct acpi_device *device)
317 struct acpi_button *button = acpi_driver_data(device);
319 if (button->type == ACPI_BUTTON_TYPE_LID)
320 return acpi_lid_send_state(device);
324 static int acpi_button_add(struct acpi_device *device)
326 struct acpi_button *button;
327 struct input_dev *input;
328 char *hid, *name, *class;
331 button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
335 device->driver_data = button;
337 button->input = input = input_allocate_device();
340 goto err_free_button;
343 hid = acpi_device_hid(device);
344 name = acpi_device_name(device);
345 class = acpi_device_class(device);
347 if (!strcmp(hid, ACPI_BUTTON_HID_POWER) ||
348 !strcmp(hid, ACPI_BUTTON_HID_POWERF)) {
349 button->type = ACPI_BUTTON_TYPE_POWER;
350 strcpy(name, ACPI_BUTTON_DEVICE_NAME_POWER);
351 sprintf(class, "%s/%s",
352 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
353 } else if (!strcmp(hid, ACPI_BUTTON_HID_SLEEP) ||
354 !strcmp(hid, ACPI_BUTTON_HID_SLEEPF)) {
355 button->type = ACPI_BUTTON_TYPE_SLEEP;
356 strcpy(name, ACPI_BUTTON_DEVICE_NAME_SLEEP);
357 sprintf(class, "%s/%s",
358 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
359 } else if (!strcmp(hid, ACPI_BUTTON_HID_LID)) {
360 button->type = ACPI_BUTTON_TYPE_LID;
361 strcpy(name, ACPI_BUTTON_DEVICE_NAME_LID);
362 sprintf(class, "%s/%s",
363 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
365 printk(KERN_ERR PREFIX "Unsupported hid [%s]\n", hid);
370 error = acpi_button_add_fs(device);
374 snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
377 input->phys = button->phys;
378 input->id.bustype = BUS_HOST;
379 input->id.product = button->type;
380 input->dev.parent = &device->dev;
382 switch (button->type) {
383 case ACPI_BUTTON_TYPE_POWER:
384 input->evbit[0] = BIT_MASK(EV_KEY);
385 set_bit(KEY_POWER, input->keybit);
388 case ACPI_BUTTON_TYPE_SLEEP:
389 input->evbit[0] = BIT_MASK(EV_KEY);
390 set_bit(KEY_SLEEP, input->keybit);
393 case ACPI_BUTTON_TYPE_LID:
394 input->evbit[0] = BIT_MASK(EV_SW);
395 set_bit(SW_LID, input->swbit);
399 error = input_register_device(input);
402 if (button->type == ACPI_BUTTON_TYPE_LID) {
403 acpi_lid_send_state(device);
405 * This assumes there's only one lid device, or if there are
406 * more we only care about the last one...
411 if (device->wakeup.flags.valid) {
412 /* Button's GPE is run-wake GPE */
413 acpi_set_gpe_type(device->wakeup.gpe_device,
414 device->wakeup.gpe_number,
415 ACPI_GPE_TYPE_WAKE_RUN);
416 acpi_enable_gpe(device->wakeup.gpe_device,
417 device->wakeup.gpe_number);
418 device->wakeup.state.enabled = 1;
421 printk(KERN_INFO PREFIX "%s [%s]\n", name, acpi_device_bid(device));
425 acpi_button_remove_fs(device);
427 input_free_device(input);
433 static int acpi_button_remove(struct acpi_device *device, int type)
435 struct acpi_button *button = acpi_driver_data(device);
437 acpi_button_remove_fs(device);
438 input_unregister_device(button->input);
443 static int __init acpi_button_init(void)
447 acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
448 if (!acpi_button_dir)
451 result = acpi_bus_register_driver(&acpi_button_driver);
453 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
460 static void __exit acpi_button_exit(void)
462 acpi_bus_unregister_driver(&acpi_button_driver);
465 remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER, acpi_button_dir);
467 remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP, acpi_button_dir);
469 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
470 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
473 module_init(acpi_button_init);
474 module_exit(acpi_button_exit);