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 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/types.h>
26 #include <linux/proc_fs.h>
27 #include <linux/seq_file.h>
28 #include <linux/input.h>
29 #include <linux/slab.h>
30 #include <linux/acpi.h>
31 #include <acpi/button.h>
33 #define PREFIX "ACPI: "
35 #define ACPI_BUTTON_CLASS "button"
36 #define ACPI_BUTTON_FILE_INFO "info"
37 #define ACPI_BUTTON_FILE_STATE "state"
38 #define ACPI_BUTTON_TYPE_UNKNOWN 0x00
39 #define ACPI_BUTTON_NOTIFY_STATUS 0x80
41 #define ACPI_BUTTON_SUBCLASS_POWER "power"
42 #define ACPI_BUTTON_HID_POWER "PNP0C0C"
43 #define ACPI_BUTTON_DEVICE_NAME_POWER "Power Button"
44 #define ACPI_BUTTON_TYPE_POWER 0x01
46 #define ACPI_BUTTON_SUBCLASS_SLEEP "sleep"
47 #define ACPI_BUTTON_HID_SLEEP "PNP0C0E"
48 #define ACPI_BUTTON_DEVICE_NAME_SLEEP "Sleep Button"
49 #define ACPI_BUTTON_TYPE_SLEEP 0x03
51 #define ACPI_BUTTON_SUBCLASS_LID "lid"
52 #define ACPI_BUTTON_HID_LID "PNP0C0D"
53 #define ACPI_BUTTON_DEVICE_NAME_LID "Lid Switch"
54 #define ACPI_BUTTON_TYPE_LID 0x05
56 #define ACPI_BUTTON_LID_INIT_IGNORE 0x00
57 #define ACPI_BUTTON_LID_INIT_OPEN 0x01
58 #define ACPI_BUTTON_LID_INIT_METHOD 0x02
60 #define _COMPONENT ACPI_BUTTON_COMPONENT
61 ACPI_MODULE_NAME("button");
63 MODULE_AUTHOR("Paul Diefenbaugh");
64 MODULE_DESCRIPTION("ACPI Button Driver");
65 MODULE_LICENSE("GPL");
67 static const struct acpi_device_id button_device_ids[] = {
68 {ACPI_BUTTON_HID_LID, 0},
69 {ACPI_BUTTON_HID_SLEEP, 0},
70 {ACPI_BUTTON_HID_SLEEPF, 0},
71 {ACPI_BUTTON_HID_POWER, 0},
72 {ACPI_BUTTON_HID_POWERF, 0},
75 MODULE_DEVICE_TABLE(acpi, button_device_ids);
77 static int acpi_button_add(struct acpi_device *device);
78 static int acpi_button_remove(struct acpi_device *device);
79 static void acpi_button_notify(struct acpi_device *device, u32 event);
81 #ifdef CONFIG_PM_SLEEP
82 static int acpi_button_suspend(struct device *dev);
83 static int acpi_button_resume(struct device *dev);
85 #define acpi_button_suspend NULL
86 #define acpi_button_resume NULL
88 static SIMPLE_DEV_PM_OPS(acpi_button_pm, acpi_button_suspend, acpi_button_resume);
90 static struct acpi_driver acpi_button_driver = {
92 .class = ACPI_BUTTON_CLASS,
93 .ids = button_device_ids,
95 .add = acpi_button_add,
96 .remove = acpi_button_remove,
97 .notify = acpi_button_notify,
99 .drv.pm = &acpi_button_pm,
104 struct input_dev *input;
105 char phys[32]; /* for input device */
106 unsigned long pushed;
110 static BLOCKING_NOTIFIER_HEAD(acpi_lid_notifier);
111 static struct acpi_device *lid_device;
112 static u8 lid_init_state = ACPI_BUTTON_LID_INIT_METHOD;
114 /* --------------------------------------------------------------------------
116 -------------------------------------------------------------------------- */
118 static struct proc_dir_entry *acpi_button_dir;
119 static struct proc_dir_entry *acpi_lid_dir;
121 static int acpi_lid_evaluate_state(struct acpi_device *device)
123 unsigned long long lid_state;
126 status = acpi_evaluate_integer(device->handle, "_LID", NULL, &lid_state);
127 if (ACPI_FAILURE(status))
130 return lid_state ? 1 : 0;
133 static int acpi_lid_notify_state(struct acpi_device *device, int state)
135 struct acpi_button *button = acpi_driver_data(device);
138 /* input layer checks if event is redundant */
139 input_report_switch(button->input, SW_LID, !state);
140 input_sync(button->input);
143 pm_wakeup_event(&device->dev, 0);
145 ret = blocking_notifier_call_chain(&acpi_lid_notifier, state, device);
146 if (ret == NOTIFY_DONE)
147 ret = blocking_notifier_call_chain(&acpi_lid_notifier, state,
149 if (ret == NOTIFY_DONE || ret == NOTIFY_OK) {
151 * It is also regarded as success if the notifier_chain
152 * returns NOTIFY_OK or NOTIFY_DONE.
159 static int acpi_button_state_seq_show(struct seq_file *seq, void *offset)
161 struct acpi_device *device = seq->private;
164 state = acpi_lid_evaluate_state(device);
165 seq_printf(seq, "state: %s\n",
166 state < 0 ? "unsupported" : (state ? "open" : "closed"));
170 static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
172 return single_open(file, acpi_button_state_seq_show, PDE_DATA(inode));
175 static const struct file_operations acpi_button_state_fops = {
176 .owner = THIS_MODULE,
177 .open = acpi_button_state_open_fs,
180 .release = single_release,
183 static int acpi_button_add_fs(struct acpi_device *device)
185 struct acpi_button *button = acpi_driver_data(device);
186 struct proc_dir_entry *entry = NULL;
189 /* procfs I/F for ACPI lid device only */
190 if (button->type != ACPI_BUTTON_TYPE_LID)
193 if (acpi_button_dir || acpi_lid_dir) {
194 printk(KERN_ERR PREFIX "More than one Lid device found!\n");
198 /* create /proc/acpi/button */
199 acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
200 if (!acpi_button_dir)
203 /* create /proc/acpi/button/lid */
204 acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
207 goto remove_button_dir;
210 /* create /proc/acpi/button/lid/LID/ */
211 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), acpi_lid_dir);
212 if (!acpi_device_dir(device)) {
217 /* create /proc/acpi/button/lid/LID/state */
218 entry = proc_create_data(ACPI_BUTTON_FILE_STATE,
219 S_IRUGO, acpi_device_dir(device),
220 &acpi_button_state_fops, device);
230 remove_proc_entry(acpi_device_bid(device),
232 acpi_device_dir(device) = NULL;
234 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
236 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
240 static int acpi_button_remove_fs(struct acpi_device *device)
242 struct acpi_button *button = acpi_driver_data(device);
244 if (button->type != ACPI_BUTTON_TYPE_LID)
247 remove_proc_entry(ACPI_BUTTON_FILE_STATE,
248 acpi_device_dir(device));
249 remove_proc_entry(acpi_device_bid(device),
251 acpi_device_dir(device) = NULL;
252 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
253 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
258 /* --------------------------------------------------------------------------
260 -------------------------------------------------------------------------- */
261 int acpi_lid_notifier_register(struct notifier_block *nb)
263 return blocking_notifier_chain_register(&acpi_lid_notifier, nb);
265 EXPORT_SYMBOL(acpi_lid_notifier_register);
267 int acpi_lid_notifier_unregister(struct notifier_block *nb)
269 return blocking_notifier_chain_unregister(&acpi_lid_notifier, nb);
271 EXPORT_SYMBOL(acpi_lid_notifier_unregister);
273 int acpi_lid_open(void)
278 return acpi_lid_evaluate_state(lid_device);
280 EXPORT_SYMBOL(acpi_lid_open);
282 static int acpi_lid_update_state(struct acpi_device *device)
286 state = acpi_lid_evaluate_state(device);
290 return acpi_lid_notify_state(device, state);
293 static void acpi_lid_initialize_state(struct acpi_device *device)
295 switch (lid_init_state) {
296 case ACPI_BUTTON_LID_INIT_OPEN:
297 (void)acpi_lid_notify_state(device, 1);
299 case ACPI_BUTTON_LID_INIT_METHOD:
300 (void)acpi_lid_update_state(device);
302 case ACPI_BUTTON_LID_INIT_IGNORE:
308 static void acpi_button_notify(struct acpi_device *device, u32 event)
310 struct acpi_button *button = acpi_driver_data(device);
311 struct input_dev *input;
314 case ACPI_FIXED_HARDWARE_EVENT:
315 event = ACPI_BUTTON_NOTIFY_STATUS;
317 case ACPI_BUTTON_NOTIFY_STATUS:
318 input = button->input;
319 if (button->type == ACPI_BUTTON_TYPE_LID) {
320 acpi_lid_update_state(device);
324 pm_wakeup_event(&device->dev, 0);
325 if (button->suspended)
328 keycode = test_bit(KEY_SLEEP, input->keybit) ?
329 KEY_SLEEP : KEY_POWER;
330 input_report_key(input, keycode, 1);
332 input_report_key(input, keycode, 0);
335 acpi_bus_generate_netlink_event(
336 device->pnp.device_class,
337 dev_name(&device->dev),
338 event, ++button->pushed);
342 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
343 "Unsupported event [0x%x]\n", event));
348 #ifdef CONFIG_PM_SLEEP
349 static int acpi_button_suspend(struct device *dev)
351 struct acpi_device *device = to_acpi_device(dev);
352 struct acpi_button *button = acpi_driver_data(device);
354 button->suspended = true;
358 static int acpi_button_resume(struct device *dev)
360 struct acpi_device *device = to_acpi_device(dev);
361 struct acpi_button *button = acpi_driver_data(device);
363 button->suspended = false;
364 if (button->type == ACPI_BUTTON_TYPE_LID)
365 acpi_lid_initialize_state(device);
370 static int acpi_button_add(struct acpi_device *device)
372 struct acpi_button *button;
373 struct input_dev *input;
374 const char *hid = acpi_device_hid(device);
378 button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
382 device->driver_data = button;
384 button->input = input = input_allocate_device();
387 goto err_free_button;
390 name = acpi_device_name(device);
391 class = acpi_device_class(device);
393 if (!strcmp(hid, ACPI_BUTTON_HID_POWER) ||
394 !strcmp(hid, ACPI_BUTTON_HID_POWERF)) {
395 button->type = ACPI_BUTTON_TYPE_POWER;
396 strcpy(name, ACPI_BUTTON_DEVICE_NAME_POWER);
397 sprintf(class, "%s/%s",
398 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
399 } else if (!strcmp(hid, ACPI_BUTTON_HID_SLEEP) ||
400 !strcmp(hid, ACPI_BUTTON_HID_SLEEPF)) {
401 button->type = ACPI_BUTTON_TYPE_SLEEP;
402 strcpy(name, ACPI_BUTTON_DEVICE_NAME_SLEEP);
403 sprintf(class, "%s/%s",
404 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
405 } else if (!strcmp(hid, ACPI_BUTTON_HID_LID)) {
406 button->type = ACPI_BUTTON_TYPE_LID;
407 strcpy(name, ACPI_BUTTON_DEVICE_NAME_LID);
408 sprintf(class, "%s/%s",
409 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
411 printk(KERN_ERR PREFIX "Unsupported hid [%s]\n", hid);
416 error = acpi_button_add_fs(device);
420 snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
423 input->phys = button->phys;
424 input->id.bustype = BUS_HOST;
425 input->id.product = button->type;
426 input->dev.parent = &device->dev;
428 switch (button->type) {
429 case ACPI_BUTTON_TYPE_POWER:
430 input_set_capability(input, EV_KEY, KEY_POWER);
433 case ACPI_BUTTON_TYPE_SLEEP:
434 input_set_capability(input, EV_KEY, KEY_SLEEP);
437 case ACPI_BUTTON_TYPE_LID:
438 input_set_capability(input, EV_SW, SW_LID);
442 error = input_register_device(input);
445 if (button->type == ACPI_BUTTON_TYPE_LID) {
446 acpi_lid_initialize_state(device);
448 * This assumes there's only one lid device, or if there are
449 * more we only care about the last one...
454 printk(KERN_INFO PREFIX "%s [%s]\n", name, acpi_device_bid(device));
458 acpi_button_remove_fs(device);
460 input_free_device(input);
466 static int acpi_button_remove(struct acpi_device *device)
468 struct acpi_button *button = acpi_driver_data(device);
470 acpi_button_remove_fs(device);
471 input_unregister_device(button->input);
476 static int param_set_lid_init_state(const char *val, struct kernel_param *kp)
480 if (!strncmp(val, "open", sizeof("open") - 1)) {
481 lid_init_state = ACPI_BUTTON_LID_INIT_OPEN;
482 pr_info("Notify initial lid state as open\n");
483 } else if (!strncmp(val, "method", sizeof("method") - 1)) {
484 lid_init_state = ACPI_BUTTON_LID_INIT_METHOD;
485 pr_info("Notify initial lid state with _LID return value\n");
486 } else if (!strncmp(val, "ignore", sizeof("ignore") - 1)) {
487 lid_init_state = ACPI_BUTTON_LID_INIT_IGNORE;
488 pr_info("Do not notify initial lid state\n");
494 static int param_get_lid_init_state(char *buffer, struct kernel_param *kp)
496 switch (lid_init_state) {
497 case ACPI_BUTTON_LID_INIT_OPEN:
498 return sprintf(buffer, "open");
499 case ACPI_BUTTON_LID_INIT_METHOD:
500 return sprintf(buffer, "method");
501 case ACPI_BUTTON_LID_INIT_IGNORE:
502 return sprintf(buffer, "ignore");
504 return sprintf(buffer, "invalid");
509 module_param_call(lid_init_state,
510 param_set_lid_init_state, param_get_lid_init_state,
512 MODULE_PARM_DESC(lid_init_state, "Behavior for reporting LID initial state");
514 module_acpi_driver(acpi_button_driver);