Linux 4.19.133
[linux/fpc-iii.git] / drivers / acpi / button.c
blobd5c19e25ddf593802402c3613d3699185faec77f
1 /*
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 #define pr_fmt(fmt) "ACPI: button: " fmt
24 #include <linux/compiler.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/types.h>
29 #include <linux/proc_fs.h>
30 #include <linux/seq_file.h>
31 #include <linux/input.h>
32 #include <linux/slab.h>
33 #include <linux/acpi.h>
34 #include <linux/dmi.h>
35 #include <acpi/button.h>
37 #define PREFIX "ACPI: "
39 #define ACPI_BUTTON_CLASS "button"
40 #define ACPI_BUTTON_FILE_INFO "info"
41 #define ACPI_BUTTON_FILE_STATE "state"
42 #define ACPI_BUTTON_TYPE_UNKNOWN 0x00
43 #define ACPI_BUTTON_NOTIFY_STATUS 0x80
45 #define ACPI_BUTTON_SUBCLASS_POWER "power"
46 #define ACPI_BUTTON_HID_POWER "PNP0C0C"
47 #define ACPI_BUTTON_DEVICE_NAME_POWER "Power Button"
48 #define ACPI_BUTTON_TYPE_POWER 0x01
50 #define ACPI_BUTTON_SUBCLASS_SLEEP "sleep"
51 #define ACPI_BUTTON_HID_SLEEP "PNP0C0E"
52 #define ACPI_BUTTON_DEVICE_NAME_SLEEP "Sleep Button"
53 #define ACPI_BUTTON_TYPE_SLEEP 0x03
55 #define ACPI_BUTTON_SUBCLASS_LID "lid"
56 #define ACPI_BUTTON_HID_LID "PNP0C0D"
57 #define ACPI_BUTTON_DEVICE_NAME_LID "Lid Switch"
58 #define ACPI_BUTTON_TYPE_LID 0x05
60 #define ACPI_BUTTON_LID_INIT_IGNORE 0x00
61 #define ACPI_BUTTON_LID_INIT_OPEN 0x01
62 #define ACPI_BUTTON_LID_INIT_METHOD 0x02
64 #define _COMPONENT ACPI_BUTTON_COMPONENT
65 ACPI_MODULE_NAME("button");
67 MODULE_AUTHOR("Paul Diefenbaugh");
68 MODULE_DESCRIPTION("ACPI Button Driver");
69 MODULE_LICENSE("GPL");
71 static const struct acpi_device_id button_device_ids[] = {
72 {ACPI_BUTTON_HID_LID, 0},
73 {ACPI_BUTTON_HID_SLEEP, 0},
74 {ACPI_BUTTON_HID_SLEEPF, 0},
75 {ACPI_BUTTON_HID_POWER, 0},
76 {ACPI_BUTTON_HID_POWERF, 0},
77 {"", 0},
79 MODULE_DEVICE_TABLE(acpi, button_device_ids);
82 * Some devices which don't even have a lid in anyway have a broken _LID
83 * method (e.g. pointing to a floating gpio pin) causing spurious LID events.
85 static const struct dmi_system_id lid_blacklst[] = {
87 /* GP-electronic T701 */
88 .matches = {
89 DMI_MATCH(DMI_SYS_VENDOR, "Insyde"),
90 DMI_MATCH(DMI_PRODUCT_NAME, "T701"),
91 DMI_MATCH(DMI_BIOS_VERSION, "BYT70A.YNCHENG.WIN.007"),
96 * Medion Akoya E2215T, notification of the LID device only
97 * happens on close, not on open and _LID always returns closed.
99 .matches = {
100 DMI_MATCH(DMI_SYS_VENDOR, "MEDION"),
101 DMI_MATCH(DMI_PRODUCT_NAME, "E2215T MD60198"),
103 .driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_OPEN,
107 * Razer Blade Stealth 13 late 2019, notification of the LID device
108 * only happens on close, not on open and _LID always returns closed.
110 .matches = {
111 DMI_MATCH(DMI_SYS_VENDOR, "Razer"),
112 DMI_MATCH(DMI_PRODUCT_NAME, "Razer Blade Stealth 13 Late 2019"),
114 .driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_OPEN,
119 static int acpi_button_add(struct acpi_device *device);
120 static int acpi_button_remove(struct acpi_device *device);
121 static void acpi_button_notify(struct acpi_device *device, u32 event);
123 #ifdef CONFIG_PM_SLEEP
124 static int acpi_button_suspend(struct device *dev);
125 static int acpi_button_resume(struct device *dev);
126 #else
127 #define acpi_button_suspend NULL
128 #define acpi_button_resume NULL
129 #endif
130 static SIMPLE_DEV_PM_OPS(acpi_button_pm, acpi_button_suspend, acpi_button_resume);
132 static struct acpi_driver acpi_button_driver = {
133 .name = "button",
134 .class = ACPI_BUTTON_CLASS,
135 .ids = button_device_ids,
136 .ops = {
137 .add = acpi_button_add,
138 .remove = acpi_button_remove,
139 .notify = acpi_button_notify,
141 .drv.pm = &acpi_button_pm,
144 struct acpi_button {
145 unsigned int type;
146 struct input_dev *input;
147 char phys[32]; /* for input device */
148 unsigned long pushed;
149 int last_state;
150 ktime_t last_time;
151 bool suspended;
154 static BLOCKING_NOTIFIER_HEAD(acpi_lid_notifier);
155 static struct acpi_device *lid_device;
156 static u8 lid_init_state = ACPI_BUTTON_LID_INIT_METHOD;
158 static unsigned long lid_report_interval __read_mostly = 500;
159 module_param(lid_report_interval, ulong, 0644);
160 MODULE_PARM_DESC(lid_report_interval, "Interval (ms) between lid key events");
162 /* --------------------------------------------------------------------------
163 FS Interface (/proc)
164 -------------------------------------------------------------------------- */
166 static struct proc_dir_entry *acpi_button_dir;
167 static struct proc_dir_entry *acpi_lid_dir;
169 static int acpi_lid_evaluate_state(struct acpi_device *device)
171 unsigned long long lid_state;
172 acpi_status status;
174 status = acpi_evaluate_integer(device->handle, "_LID", NULL, &lid_state);
175 if (ACPI_FAILURE(status))
176 return -ENODEV;
178 return lid_state ? 1 : 0;
181 static int acpi_lid_notify_state(struct acpi_device *device, int state)
183 struct acpi_button *button = acpi_driver_data(device);
184 int ret;
185 ktime_t next_report;
186 bool do_update;
189 * In lid_init_state=ignore mode, if user opens/closes lid
190 * frequently with "open" missing, and "last_time" is also updated
191 * frequently, "close" cannot be delivered to the userspace.
192 * So "last_time" is only updated after a timeout or an actual
193 * switch.
195 if (lid_init_state != ACPI_BUTTON_LID_INIT_IGNORE ||
196 button->last_state != !!state)
197 do_update = true;
198 else
199 do_update = false;
201 next_report = ktime_add(button->last_time,
202 ms_to_ktime(lid_report_interval));
203 if (button->last_state == !!state &&
204 ktime_after(ktime_get(), next_report)) {
205 /* Complain the buggy firmware */
206 pr_warn_once("The lid device is not compliant to SW_LID.\n");
209 * Send the unreliable complement switch event:
211 * On most platforms, the lid device is reliable. However
212 * there are exceptions:
213 * 1. Platforms returning initial lid state as "close" by
214 * default after booting/resuming:
215 * https://bugzilla.kernel.org/show_bug.cgi?id=89211
216 * https://bugzilla.kernel.org/show_bug.cgi?id=106151
217 * 2. Platforms never reporting "open" events:
218 * https://bugzilla.kernel.org/show_bug.cgi?id=106941
219 * On these buggy platforms, the usage model of the ACPI
220 * lid device actually is:
221 * 1. The initial returning value of _LID may not be
222 * reliable.
223 * 2. The open event may not be reliable.
224 * 3. The close event is reliable.
226 * But SW_LID is typed as input switch event, the input
227 * layer checks if the event is redundant. Hence if the
228 * state is not switched, the userspace cannot see this
229 * platform triggered reliable event. By inserting a
230 * complement switch event, it then is guaranteed that the
231 * platform triggered reliable one can always be seen by
232 * the userspace.
234 if (lid_init_state == ACPI_BUTTON_LID_INIT_IGNORE) {
235 do_update = true;
237 * Do generate complement switch event for "close"
238 * as "close" is reliable and wrong "open" won't
239 * trigger unexpected behaviors.
240 * Do not generate complement switch event for
241 * "open" as "open" is not reliable and wrong
242 * "close" will trigger unexpected behaviors.
244 if (!state) {
245 input_report_switch(button->input,
246 SW_LID, state);
247 input_sync(button->input);
251 /* Send the platform triggered reliable event */
252 if (do_update) {
253 acpi_handle_debug(device->handle, "ACPI LID %s\n",
254 state ? "open" : "closed");
255 input_report_switch(button->input, SW_LID, !state);
256 input_sync(button->input);
257 button->last_state = !!state;
258 button->last_time = ktime_get();
261 ret = blocking_notifier_call_chain(&acpi_lid_notifier, state, device);
262 if (ret == NOTIFY_DONE)
263 ret = blocking_notifier_call_chain(&acpi_lid_notifier, state,
264 device);
265 if (ret == NOTIFY_DONE || ret == NOTIFY_OK) {
267 * It is also regarded as success if the notifier_chain
268 * returns NOTIFY_OK or NOTIFY_DONE.
270 ret = 0;
272 return ret;
275 static int __maybe_unused acpi_button_state_seq_show(struct seq_file *seq,
276 void *offset)
278 struct acpi_device *device = seq->private;
279 int state;
281 state = acpi_lid_evaluate_state(device);
282 seq_printf(seq, "state: %s\n",
283 state < 0 ? "unsupported" : (state ? "open" : "closed"));
284 return 0;
287 static int acpi_button_add_fs(struct acpi_device *device)
289 struct acpi_button *button = acpi_driver_data(device);
290 struct proc_dir_entry *entry = NULL;
291 int ret = 0;
293 /* procfs I/F for ACPI lid device only */
294 if (button->type != ACPI_BUTTON_TYPE_LID)
295 return 0;
297 if (acpi_button_dir || acpi_lid_dir) {
298 printk(KERN_ERR PREFIX "More than one Lid device found!\n");
299 return -EEXIST;
302 /* create /proc/acpi/button */
303 acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
304 if (!acpi_button_dir)
305 return -ENODEV;
307 /* create /proc/acpi/button/lid */
308 acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
309 if (!acpi_lid_dir) {
310 ret = -ENODEV;
311 goto remove_button_dir;
314 /* create /proc/acpi/button/lid/LID/ */
315 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), acpi_lid_dir);
316 if (!acpi_device_dir(device)) {
317 ret = -ENODEV;
318 goto remove_lid_dir;
321 /* create /proc/acpi/button/lid/LID/state */
322 entry = proc_create_single_data(ACPI_BUTTON_FILE_STATE, S_IRUGO,
323 acpi_device_dir(device), acpi_button_state_seq_show,
324 device);
325 if (!entry) {
326 ret = -ENODEV;
327 goto remove_dev_dir;
330 done:
331 return ret;
333 remove_dev_dir:
334 remove_proc_entry(acpi_device_bid(device),
335 acpi_lid_dir);
336 acpi_device_dir(device) = NULL;
337 remove_lid_dir:
338 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
339 acpi_lid_dir = NULL;
340 remove_button_dir:
341 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
342 acpi_button_dir = NULL;
343 goto done;
346 static int acpi_button_remove_fs(struct acpi_device *device)
348 struct acpi_button *button = acpi_driver_data(device);
350 if (button->type != ACPI_BUTTON_TYPE_LID)
351 return 0;
353 remove_proc_entry(ACPI_BUTTON_FILE_STATE,
354 acpi_device_dir(device));
355 remove_proc_entry(acpi_device_bid(device),
356 acpi_lid_dir);
357 acpi_device_dir(device) = NULL;
358 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
359 acpi_lid_dir = NULL;
360 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
361 acpi_button_dir = NULL;
363 return 0;
366 /* --------------------------------------------------------------------------
367 Driver Interface
368 -------------------------------------------------------------------------- */
369 int acpi_lid_notifier_register(struct notifier_block *nb)
371 return blocking_notifier_chain_register(&acpi_lid_notifier, nb);
373 EXPORT_SYMBOL(acpi_lid_notifier_register);
375 int acpi_lid_notifier_unregister(struct notifier_block *nb)
377 return blocking_notifier_chain_unregister(&acpi_lid_notifier, nb);
379 EXPORT_SYMBOL(acpi_lid_notifier_unregister);
381 int acpi_lid_open(void)
383 if (!lid_device)
384 return -ENODEV;
386 return acpi_lid_evaluate_state(lid_device);
388 EXPORT_SYMBOL(acpi_lid_open);
390 static int acpi_lid_update_state(struct acpi_device *device,
391 bool signal_wakeup)
393 int state;
395 state = acpi_lid_evaluate_state(device);
396 if (state < 0)
397 return state;
399 if (state && signal_wakeup)
400 acpi_pm_wakeup_event(&device->dev);
402 return acpi_lid_notify_state(device, state);
405 static void acpi_lid_initialize_state(struct acpi_device *device)
407 switch (lid_init_state) {
408 case ACPI_BUTTON_LID_INIT_OPEN:
409 (void)acpi_lid_notify_state(device, 1);
410 break;
411 case ACPI_BUTTON_LID_INIT_METHOD:
412 (void)acpi_lid_update_state(device, false);
413 break;
414 case ACPI_BUTTON_LID_INIT_IGNORE:
415 default:
416 break;
420 static void acpi_button_notify(struct acpi_device *device, u32 event)
422 struct acpi_button *button = acpi_driver_data(device);
423 struct input_dev *input;
424 int users;
426 switch (event) {
427 case ACPI_FIXED_HARDWARE_EVENT:
428 event = ACPI_BUTTON_NOTIFY_STATUS;
429 /* fall through */
430 case ACPI_BUTTON_NOTIFY_STATUS:
431 input = button->input;
432 if (button->type == ACPI_BUTTON_TYPE_LID) {
433 mutex_lock(&button->input->mutex);
434 users = button->input->users;
435 mutex_unlock(&button->input->mutex);
436 if (users)
437 acpi_lid_update_state(device, true);
438 } else {
439 int keycode;
441 acpi_pm_wakeup_event(&device->dev);
442 if (button->suspended)
443 break;
445 keycode = test_bit(KEY_SLEEP, input->keybit) ?
446 KEY_SLEEP : KEY_POWER;
447 input_report_key(input, keycode, 1);
448 input_sync(input);
449 input_report_key(input, keycode, 0);
450 input_sync(input);
452 acpi_bus_generate_netlink_event(
453 device->pnp.device_class,
454 dev_name(&device->dev),
455 event, ++button->pushed);
457 break;
458 default:
459 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
460 "Unsupported event [0x%x]\n", event));
461 break;
465 #ifdef CONFIG_PM_SLEEP
466 static int acpi_button_suspend(struct device *dev)
468 struct acpi_device *device = to_acpi_device(dev);
469 struct acpi_button *button = acpi_driver_data(device);
471 button->suspended = true;
472 return 0;
475 static int acpi_button_resume(struct device *dev)
477 struct acpi_device *device = to_acpi_device(dev);
478 struct acpi_button *button = acpi_driver_data(device);
480 button->suspended = false;
481 if (button->type == ACPI_BUTTON_TYPE_LID && button->input->users) {
482 button->last_state = !!acpi_lid_evaluate_state(device);
483 button->last_time = ktime_get();
484 acpi_lid_initialize_state(device);
486 return 0;
488 #endif
490 static int acpi_lid_input_open(struct input_dev *input)
492 struct acpi_device *device = input_get_drvdata(input);
493 struct acpi_button *button = acpi_driver_data(device);
495 button->last_state = !!acpi_lid_evaluate_state(device);
496 button->last_time = ktime_get();
497 acpi_lid_initialize_state(device);
499 return 0;
502 static int acpi_button_add(struct acpi_device *device)
504 struct acpi_button *button;
505 struct input_dev *input;
506 const char *hid = acpi_device_hid(device);
507 char *name, *class;
508 int error;
510 if (!strcmp(hid, ACPI_BUTTON_HID_LID) && dmi_check_system(lid_blacklst))
511 return -ENODEV;
513 button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
514 if (!button)
515 return -ENOMEM;
517 device->driver_data = button;
519 button->input = input = input_allocate_device();
520 if (!input) {
521 error = -ENOMEM;
522 goto err_free_button;
525 name = acpi_device_name(device);
526 class = acpi_device_class(device);
528 if (!strcmp(hid, ACPI_BUTTON_HID_POWER) ||
529 !strcmp(hid, ACPI_BUTTON_HID_POWERF)) {
530 button->type = ACPI_BUTTON_TYPE_POWER;
531 strcpy(name, ACPI_BUTTON_DEVICE_NAME_POWER);
532 sprintf(class, "%s/%s",
533 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
534 } else if (!strcmp(hid, ACPI_BUTTON_HID_SLEEP) ||
535 !strcmp(hid, ACPI_BUTTON_HID_SLEEPF)) {
536 button->type = ACPI_BUTTON_TYPE_SLEEP;
537 strcpy(name, ACPI_BUTTON_DEVICE_NAME_SLEEP);
538 sprintf(class, "%s/%s",
539 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
540 } else if (!strcmp(hid, ACPI_BUTTON_HID_LID)) {
541 button->type = ACPI_BUTTON_TYPE_LID;
542 strcpy(name, ACPI_BUTTON_DEVICE_NAME_LID);
543 sprintf(class, "%s/%s",
544 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
545 input->open = acpi_lid_input_open;
546 } else {
547 printk(KERN_ERR PREFIX "Unsupported hid [%s]\n", hid);
548 error = -ENODEV;
549 goto err_free_input;
552 error = acpi_button_add_fs(device);
553 if (error)
554 goto err_free_input;
556 snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
558 input->name = name;
559 input->phys = button->phys;
560 input->id.bustype = BUS_HOST;
561 input->id.product = button->type;
562 input->dev.parent = &device->dev;
564 switch (button->type) {
565 case ACPI_BUTTON_TYPE_POWER:
566 input_set_capability(input, EV_KEY, KEY_POWER);
567 break;
569 case ACPI_BUTTON_TYPE_SLEEP:
570 input_set_capability(input, EV_KEY, KEY_SLEEP);
571 break;
573 case ACPI_BUTTON_TYPE_LID:
574 input_set_capability(input, EV_SW, SW_LID);
575 break;
578 input_set_drvdata(input, device);
579 error = input_register_device(input);
580 if (error)
581 goto err_remove_fs;
582 if (button->type == ACPI_BUTTON_TYPE_LID) {
584 * This assumes there's only one lid device, or if there are
585 * more we only care about the last one...
587 lid_device = device;
590 device_init_wakeup(&device->dev, true);
591 printk(KERN_INFO PREFIX "%s [%s]\n", name, acpi_device_bid(device));
592 return 0;
594 err_remove_fs:
595 acpi_button_remove_fs(device);
596 err_free_input:
597 input_free_device(input);
598 err_free_button:
599 kfree(button);
600 return error;
603 static int acpi_button_remove(struct acpi_device *device)
605 struct acpi_button *button = acpi_driver_data(device);
607 acpi_button_remove_fs(device);
608 input_unregister_device(button->input);
609 kfree(button);
610 return 0;
613 static int param_set_lid_init_state(const char *val,
614 const struct kernel_param *kp)
616 int result = 0;
618 if (!strncmp(val, "open", sizeof("open") - 1)) {
619 lid_init_state = ACPI_BUTTON_LID_INIT_OPEN;
620 pr_info("Notify initial lid state as open\n");
621 } else if (!strncmp(val, "method", sizeof("method") - 1)) {
622 lid_init_state = ACPI_BUTTON_LID_INIT_METHOD;
623 pr_info("Notify initial lid state with _LID return value\n");
624 } else if (!strncmp(val, "ignore", sizeof("ignore") - 1)) {
625 lid_init_state = ACPI_BUTTON_LID_INIT_IGNORE;
626 pr_info("Do not notify initial lid state\n");
627 } else
628 result = -EINVAL;
629 return result;
632 static int param_get_lid_init_state(char *buffer,
633 const struct kernel_param *kp)
635 switch (lid_init_state) {
636 case ACPI_BUTTON_LID_INIT_OPEN:
637 return sprintf(buffer, "open");
638 case ACPI_BUTTON_LID_INIT_METHOD:
639 return sprintf(buffer, "method");
640 case ACPI_BUTTON_LID_INIT_IGNORE:
641 return sprintf(buffer, "ignore");
642 default:
643 return sprintf(buffer, "invalid");
645 return 0;
648 module_param_call(lid_init_state,
649 param_set_lid_init_state, param_get_lid_init_state,
650 NULL, 0644);
651 MODULE_PARM_DESC(lid_init_state, "Behavior for reporting LID initial state");
653 static int acpi_button_register_driver(struct acpi_driver *driver)
656 * Modules such as nouveau.ko and i915.ko have a link time dependency
657 * on acpi_lid_open(), and would therefore not be loadable on ACPI
658 * capable kernels booted in non-ACPI mode if the return value of
659 * acpi_bus_register_driver() is returned from here with ACPI disabled
660 * when this driver is built as a module.
662 if (acpi_disabled)
663 return 0;
665 return acpi_bus_register_driver(driver);
668 static void acpi_button_unregister_driver(struct acpi_driver *driver)
670 if (!acpi_disabled)
671 acpi_bus_unregister_driver(driver);
674 module_driver(acpi_button_driver, acpi_button_register_driver,
675 acpi_button_unregister_driver);