1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * button.c - ACPI Button Driver
5 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
6 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
9 #define pr_fmt(fmt) "ACPI: button: " fmt
11 #include <linux/compiler.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/types.h>
16 #include <linux/proc_fs.h>
17 #include <linux/seq_file.h>
18 #include <linux/input.h>
19 #include <linux/slab.h>
20 #include <linux/acpi.h>
21 #include <linux/dmi.h>
22 #include <acpi/button.h>
24 #define PREFIX "ACPI: "
26 #define ACPI_BUTTON_CLASS "button"
27 #define ACPI_BUTTON_FILE_INFO "info"
28 #define ACPI_BUTTON_FILE_STATE "state"
29 #define ACPI_BUTTON_TYPE_UNKNOWN 0x00
30 #define ACPI_BUTTON_NOTIFY_STATUS 0x80
32 #define ACPI_BUTTON_SUBCLASS_POWER "power"
33 #define ACPI_BUTTON_HID_POWER "PNP0C0C"
34 #define ACPI_BUTTON_DEVICE_NAME_POWER "Power Button"
35 #define ACPI_BUTTON_TYPE_POWER 0x01
37 #define ACPI_BUTTON_SUBCLASS_SLEEP "sleep"
38 #define ACPI_BUTTON_HID_SLEEP "PNP0C0E"
39 #define ACPI_BUTTON_DEVICE_NAME_SLEEP "Sleep Button"
40 #define ACPI_BUTTON_TYPE_SLEEP 0x03
42 #define ACPI_BUTTON_SUBCLASS_LID "lid"
43 #define ACPI_BUTTON_HID_LID "PNP0C0D"
44 #define ACPI_BUTTON_DEVICE_NAME_LID "Lid Switch"
45 #define ACPI_BUTTON_TYPE_LID 0x05
48 ACPI_BUTTON_LID_INIT_IGNORE,
49 ACPI_BUTTON_LID_INIT_OPEN,
50 ACPI_BUTTON_LID_INIT_METHOD,
53 static const char * const lid_init_state_str[] = {
54 [ACPI_BUTTON_LID_INIT_IGNORE] = "ignore",
55 [ACPI_BUTTON_LID_INIT_OPEN] = "open",
56 [ACPI_BUTTON_LID_INIT_METHOD] = "method",
59 #define _COMPONENT ACPI_BUTTON_COMPONENT
60 ACPI_MODULE_NAME("button");
62 MODULE_AUTHOR("Paul Diefenbaugh");
63 MODULE_DESCRIPTION("ACPI Button Driver");
64 MODULE_LICENSE("GPL");
66 static const struct acpi_device_id button_device_ids[] = {
67 {ACPI_BUTTON_HID_LID, 0},
68 {ACPI_BUTTON_HID_SLEEP, 0},
69 {ACPI_BUTTON_HID_SLEEPF, 0},
70 {ACPI_BUTTON_HID_POWER, 0},
71 {ACPI_BUTTON_HID_POWERF, 0},
74 MODULE_DEVICE_TABLE(acpi, button_device_ids);
77 * Some devices which don't even have a lid in anyway have a broken _LID
78 * method (e.g. pointing to a floating gpio pin) causing spurious LID events.
80 static const struct dmi_system_id lid_blacklst[] = {
82 /* GP-electronic T701 */
84 DMI_MATCH(DMI_SYS_VENDOR, "Insyde"),
85 DMI_MATCH(DMI_PRODUCT_NAME, "T701"),
86 DMI_MATCH(DMI_BIOS_VERSION, "BYT70A.YNCHENG.WIN.007"),
92 static int acpi_button_add(struct acpi_device *device);
93 static int acpi_button_remove(struct acpi_device *device);
94 static void acpi_button_notify(struct acpi_device *device, u32 event);
96 #ifdef CONFIG_PM_SLEEP
97 static int acpi_button_suspend(struct device *dev);
98 static int acpi_button_resume(struct device *dev);
100 #define acpi_button_suspend NULL
101 #define acpi_button_resume NULL
103 static SIMPLE_DEV_PM_OPS(acpi_button_pm, acpi_button_suspend, acpi_button_resume);
105 static struct acpi_driver acpi_button_driver = {
107 .class = ACPI_BUTTON_CLASS,
108 .ids = button_device_ids,
110 .add = acpi_button_add,
111 .remove = acpi_button_remove,
112 .notify = acpi_button_notify,
114 .drv.pm = &acpi_button_pm,
119 struct input_dev *input;
120 char phys[32]; /* for input device */
121 unsigned long pushed;
127 static BLOCKING_NOTIFIER_HEAD(acpi_lid_notifier);
128 static struct acpi_device *lid_device;
129 static u8 lid_init_state = ACPI_BUTTON_LID_INIT_METHOD;
131 static unsigned long lid_report_interval __read_mostly = 500;
132 module_param(lid_report_interval, ulong, 0644);
133 MODULE_PARM_DESC(lid_report_interval, "Interval (ms) between lid key events");
135 /* --------------------------------------------------------------------------
137 -------------------------------------------------------------------------- */
139 static struct proc_dir_entry *acpi_button_dir;
140 static struct proc_dir_entry *acpi_lid_dir;
142 static int acpi_lid_evaluate_state(struct acpi_device *device)
144 unsigned long long lid_state;
147 status = acpi_evaluate_integer(device->handle, "_LID", NULL, &lid_state);
148 if (ACPI_FAILURE(status))
151 return lid_state ? 1 : 0;
154 static int acpi_lid_notify_state(struct acpi_device *device, int state)
156 struct acpi_button *button = acpi_driver_data(device);
162 * In lid_init_state=ignore mode, if user opens/closes lid
163 * frequently with "open" missing, and "last_time" is also updated
164 * frequently, "close" cannot be delivered to the userspace.
165 * So "last_time" is only updated after a timeout or an actual
168 if (lid_init_state != ACPI_BUTTON_LID_INIT_IGNORE ||
169 button->last_state != !!state)
174 next_report = ktime_add(button->last_time,
175 ms_to_ktime(lid_report_interval));
176 if (button->last_state == !!state &&
177 ktime_after(ktime_get(), next_report)) {
178 /* Complain the buggy firmware */
179 pr_warn_once("The lid device is not compliant to SW_LID.\n");
182 * Send the unreliable complement switch event:
184 * On most platforms, the lid device is reliable. However
185 * there are exceptions:
186 * 1. Platforms returning initial lid state as "close" by
187 * default after booting/resuming:
188 * https://bugzilla.kernel.org/show_bug.cgi?id=89211
189 * https://bugzilla.kernel.org/show_bug.cgi?id=106151
190 * 2. Platforms never reporting "open" events:
191 * https://bugzilla.kernel.org/show_bug.cgi?id=106941
192 * On these buggy platforms, the usage model of the ACPI
193 * lid device actually is:
194 * 1. The initial returning value of _LID may not be
196 * 2. The open event may not be reliable.
197 * 3. The close event is reliable.
199 * But SW_LID is typed as input switch event, the input
200 * layer checks if the event is redundant. Hence if the
201 * state is not switched, the userspace cannot see this
202 * platform triggered reliable event. By inserting a
203 * complement switch event, it then is guaranteed that the
204 * platform triggered reliable one can always be seen by
207 if (lid_init_state == ACPI_BUTTON_LID_INIT_IGNORE) {
210 * Do generate complement switch event for "close"
211 * as "close" is reliable and wrong "open" won't
212 * trigger unexpected behaviors.
213 * Do not generate complement switch event for
214 * "open" as "open" is not reliable and wrong
215 * "close" will trigger unexpected behaviors.
218 input_report_switch(button->input,
220 input_sync(button->input);
224 /* Send the platform triggered reliable event */
226 acpi_handle_debug(device->handle, "ACPI LID %s\n",
227 state ? "open" : "closed");
228 input_report_switch(button->input, SW_LID, !state);
229 input_sync(button->input);
230 button->last_state = !!state;
231 button->last_time = ktime_get();
234 ret = blocking_notifier_call_chain(&acpi_lid_notifier, state, device);
235 if (ret == NOTIFY_DONE)
236 ret = blocking_notifier_call_chain(&acpi_lid_notifier, state,
238 if (ret == NOTIFY_DONE || ret == NOTIFY_OK) {
240 * It is also regarded as success if the notifier_chain
241 * returns NOTIFY_OK or NOTIFY_DONE.
248 static int __maybe_unused acpi_button_state_seq_show(struct seq_file *seq,
251 struct acpi_device *device = seq->private;
254 state = acpi_lid_evaluate_state(device);
255 seq_printf(seq, "state: %s\n",
256 state < 0 ? "unsupported" : (state ? "open" : "closed"));
260 static int acpi_button_add_fs(struct acpi_device *device)
262 struct acpi_button *button = acpi_driver_data(device);
263 struct proc_dir_entry *entry = NULL;
266 /* procfs I/F for ACPI lid device only */
267 if (button->type != ACPI_BUTTON_TYPE_LID)
270 if (acpi_button_dir || acpi_lid_dir) {
271 printk(KERN_ERR PREFIX "More than one Lid device found!\n");
275 /* create /proc/acpi/button */
276 acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
277 if (!acpi_button_dir)
280 /* create /proc/acpi/button/lid */
281 acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
284 goto remove_button_dir;
287 /* create /proc/acpi/button/lid/LID/ */
288 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), acpi_lid_dir);
289 if (!acpi_device_dir(device)) {
294 /* create /proc/acpi/button/lid/LID/state */
295 entry = proc_create_single_data(ACPI_BUTTON_FILE_STATE, S_IRUGO,
296 acpi_device_dir(device), acpi_button_state_seq_show,
307 remove_proc_entry(acpi_device_bid(device),
309 acpi_device_dir(device) = NULL;
311 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
314 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
315 acpi_button_dir = NULL;
319 static int acpi_button_remove_fs(struct acpi_device *device)
321 struct acpi_button *button = acpi_driver_data(device);
323 if (button->type != ACPI_BUTTON_TYPE_LID)
326 remove_proc_entry(ACPI_BUTTON_FILE_STATE,
327 acpi_device_dir(device));
328 remove_proc_entry(acpi_device_bid(device),
330 acpi_device_dir(device) = NULL;
331 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
333 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
334 acpi_button_dir = NULL;
339 /* --------------------------------------------------------------------------
341 -------------------------------------------------------------------------- */
342 int acpi_lid_notifier_register(struct notifier_block *nb)
344 return blocking_notifier_chain_register(&acpi_lid_notifier, nb);
346 EXPORT_SYMBOL(acpi_lid_notifier_register);
348 int acpi_lid_notifier_unregister(struct notifier_block *nb)
350 return blocking_notifier_chain_unregister(&acpi_lid_notifier, nb);
352 EXPORT_SYMBOL(acpi_lid_notifier_unregister);
354 int acpi_lid_open(void)
359 return acpi_lid_evaluate_state(lid_device);
361 EXPORT_SYMBOL(acpi_lid_open);
363 static int acpi_lid_update_state(struct acpi_device *device,
368 state = acpi_lid_evaluate_state(device);
372 if (state && signal_wakeup)
373 acpi_pm_wakeup_event(&device->dev);
375 return acpi_lid_notify_state(device, state);
378 static void acpi_lid_initialize_state(struct acpi_device *device)
380 switch (lid_init_state) {
381 case ACPI_BUTTON_LID_INIT_OPEN:
382 (void)acpi_lid_notify_state(device, 1);
384 case ACPI_BUTTON_LID_INIT_METHOD:
385 (void)acpi_lid_update_state(device, false);
387 case ACPI_BUTTON_LID_INIT_IGNORE:
393 static void acpi_button_notify(struct acpi_device *device, u32 event)
395 struct acpi_button *button = acpi_driver_data(device);
396 struct input_dev *input;
400 case ACPI_FIXED_HARDWARE_EVENT:
401 event = ACPI_BUTTON_NOTIFY_STATUS;
403 case ACPI_BUTTON_NOTIFY_STATUS:
404 input = button->input;
405 if (button->type == ACPI_BUTTON_TYPE_LID) {
406 mutex_lock(&button->input->mutex);
407 users = button->input->users;
408 mutex_unlock(&button->input->mutex);
410 acpi_lid_update_state(device, true);
414 acpi_pm_wakeup_event(&device->dev);
415 if (button->suspended)
418 keycode = test_bit(KEY_SLEEP, input->keybit) ?
419 KEY_SLEEP : KEY_POWER;
420 input_report_key(input, keycode, 1);
422 input_report_key(input, keycode, 0);
425 acpi_bus_generate_netlink_event(
426 device->pnp.device_class,
427 dev_name(&device->dev),
428 event, ++button->pushed);
432 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
433 "Unsupported event [0x%x]\n", event));
438 #ifdef CONFIG_PM_SLEEP
439 static int acpi_button_suspend(struct device *dev)
441 struct acpi_device *device = to_acpi_device(dev);
442 struct acpi_button *button = acpi_driver_data(device);
444 button->suspended = true;
448 static int acpi_button_resume(struct device *dev)
450 struct acpi_device *device = to_acpi_device(dev);
451 struct acpi_button *button = acpi_driver_data(device);
453 button->suspended = false;
454 if (button->type == ACPI_BUTTON_TYPE_LID && button->input->users) {
455 button->last_state = !!acpi_lid_evaluate_state(device);
456 button->last_time = ktime_get();
457 acpi_lid_initialize_state(device);
463 static int acpi_lid_input_open(struct input_dev *input)
465 struct acpi_device *device = input_get_drvdata(input);
466 struct acpi_button *button = acpi_driver_data(device);
468 button->last_state = !!acpi_lid_evaluate_state(device);
469 button->last_time = ktime_get();
470 acpi_lid_initialize_state(device);
475 static int acpi_button_add(struct acpi_device *device)
477 struct acpi_button *button;
478 struct input_dev *input;
479 const char *hid = acpi_device_hid(device);
483 if (!strcmp(hid, ACPI_BUTTON_HID_LID) && dmi_check_system(lid_blacklst))
486 button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
490 device->driver_data = button;
492 button->input = input = input_allocate_device();
495 goto err_free_button;
498 name = acpi_device_name(device);
499 class = acpi_device_class(device);
501 if (!strcmp(hid, ACPI_BUTTON_HID_POWER) ||
502 !strcmp(hid, ACPI_BUTTON_HID_POWERF)) {
503 button->type = ACPI_BUTTON_TYPE_POWER;
504 strcpy(name, ACPI_BUTTON_DEVICE_NAME_POWER);
505 sprintf(class, "%s/%s",
506 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
507 } else if (!strcmp(hid, ACPI_BUTTON_HID_SLEEP) ||
508 !strcmp(hid, ACPI_BUTTON_HID_SLEEPF)) {
509 button->type = ACPI_BUTTON_TYPE_SLEEP;
510 strcpy(name, ACPI_BUTTON_DEVICE_NAME_SLEEP);
511 sprintf(class, "%s/%s",
512 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
513 } else if (!strcmp(hid, ACPI_BUTTON_HID_LID)) {
514 button->type = ACPI_BUTTON_TYPE_LID;
515 strcpy(name, ACPI_BUTTON_DEVICE_NAME_LID);
516 sprintf(class, "%s/%s",
517 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
518 input->open = acpi_lid_input_open;
520 printk(KERN_ERR PREFIX "Unsupported hid [%s]\n", hid);
525 error = acpi_button_add_fs(device);
529 snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
532 input->phys = button->phys;
533 input->id.bustype = BUS_HOST;
534 input->id.product = button->type;
535 input->dev.parent = &device->dev;
537 switch (button->type) {
538 case ACPI_BUTTON_TYPE_POWER:
539 input_set_capability(input, EV_KEY, KEY_POWER);
542 case ACPI_BUTTON_TYPE_SLEEP:
543 input_set_capability(input, EV_KEY, KEY_SLEEP);
546 case ACPI_BUTTON_TYPE_LID:
547 input_set_capability(input, EV_SW, SW_LID);
551 input_set_drvdata(input, device);
552 error = input_register_device(input);
555 if (button->type == ACPI_BUTTON_TYPE_LID) {
557 * This assumes there's only one lid device, or if there are
558 * more we only care about the last one...
563 device_init_wakeup(&device->dev, true);
564 printk(KERN_INFO PREFIX "%s [%s]\n", name, acpi_device_bid(device));
568 acpi_button_remove_fs(device);
570 input_free_device(input);
576 static int acpi_button_remove(struct acpi_device *device)
578 struct acpi_button *button = acpi_driver_data(device);
580 acpi_button_remove_fs(device);
581 input_unregister_device(button->input);
586 static int param_set_lid_init_state(const char *val,
587 const struct kernel_param *kp)
591 i = sysfs_match_string(lid_init_state_str, val);
596 pr_info("Initial lid state set to '%s'\n", lid_init_state_str[i]);
600 static int param_get_lid_init_state(char *buf, const struct kernel_param *kp)
604 for (i = 0; i < ARRAY_SIZE(lid_init_state_str); i++)
605 if (i == lid_init_state)
606 c += sprintf(buf + c, "[%s] ", lid_init_state_str[i]);
608 c += sprintf(buf + c, "%s ", lid_init_state_str[i]);
610 buf[c - 1] = '\n'; /* Replace the final space with a newline */
615 module_param_call(lid_init_state,
616 param_set_lid_init_state, param_get_lid_init_state,
618 MODULE_PARM_DESC(lid_init_state, "Behavior for reporting LID initial state");
620 static int acpi_button_register_driver(struct acpi_driver *driver)
623 * Modules such as nouveau.ko and i915.ko have a link time dependency
624 * on acpi_lid_open(), and would therefore not be loadable on ACPI
625 * capable kernels booted in non-ACPI mode if the return value of
626 * acpi_bus_register_driver() is returned from here with ACPI disabled
627 * when this driver is built as a module.
632 return acpi_bus_register_driver(driver);
635 static void acpi_button_unregister_driver(struct acpi_driver *driver)
638 acpi_bus_unregister_driver(driver);
641 module_driver(acpi_button_driver, acpi_button_register_driver,
642 acpi_button_unregister_driver);