blob: 1f9b9a4c38c7d2fa4734a27c883b303e20700db0 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Bjorn Helgaas50a4da82009-04-08 15:39:38 +00003 * button.c - ACPI Button Driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
6 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
Vincent Legollb6aeab42017-05-20 20:38:13 +02009#define pr_fmt(fmt) "ACPI: button: " fmt
Lv Zhengdfa46c52016-08-17 16:22:58 +080010
Randy Dunlap2c4c2a72018-07-07 08:25:01 -070011#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/init.h>
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -040015#include <linux/types.h>
16#include <linux/proc_fs.h>
17#include <linux/seq_file.h>
Dmitry Torokhovc0968f02006-11-09 00:40:13 -050018#include <linux/input.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Lv Zheng8b484632013-12-03 08:49:16 +080020#include <linux/acpi.h>
Hans de Goede9e811e12017-11-22 16:06:12 +010021#include <linux/dmi.h>
Andy Shevchenko6270da62013-03-11 09:17:04 +000022#include <acpi/button.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#define ACPI_BUTTON_CLASS "button"
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -040025#define ACPI_BUTTON_FILE_STATE "state"
26#define ACPI_BUTTON_TYPE_UNKNOWN 0x00
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#define ACPI_BUTTON_NOTIFY_STATUS 0x80
28
29#define ACPI_BUTTON_SUBCLASS_POWER "power"
Bjorn Helgaasd68b5972009-04-08 15:40:04 +000030#define ACPI_BUTTON_DEVICE_NAME_POWER "Power Button"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#define ACPI_BUTTON_TYPE_POWER 0x01
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#define ACPI_BUTTON_SUBCLASS_SLEEP "sleep"
Bjorn Helgaasd68b5972009-04-08 15:40:04 +000034#define ACPI_BUTTON_DEVICE_NAME_SLEEP "Sleep Button"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#define ACPI_BUTTON_TYPE_SLEEP 0x03
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#define ACPI_BUTTON_SUBCLASS_LID "lid"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#define ACPI_BUTTON_DEVICE_NAME_LID "Lid Switch"
39#define ACPI_BUTTON_TYPE_LID 0x05
40
Hans de Goede065bd4d2019-10-26 22:24:31 +020041enum {
42 ACPI_BUTTON_LID_INIT_IGNORE,
43 ACPI_BUTTON_LID_INIT_OPEN,
44 ACPI_BUTTON_LID_INIT_METHOD,
Hans de Goede593681e2019-10-26 22:24:32 +020045 ACPI_BUTTON_LID_INIT_DISABLED,
Hans de Goede065bd4d2019-10-26 22:24:31 +020046};
47
48static const char * const lid_init_state_str[] = {
49 [ACPI_BUTTON_LID_INIT_IGNORE] = "ignore",
50 [ACPI_BUTTON_LID_INIT_OPEN] = "open",
51 [ACPI_BUTTON_LID_INIT_METHOD] = "method",
Hans de Goede593681e2019-10-26 22:24:32 +020052 [ACPI_BUTTON_LID_INIT_DISABLED] = "disabled",
Hans de Goede065bd4d2019-10-26 22:24:31 +020053};
Lv Zheng3540c322016-06-01 18:10:48 +080054
Dmitry Torokhovc0968f02006-11-09 00:40:13 -050055MODULE_AUTHOR("Paul Diefenbaugh");
Len Brown7cda93e2007-02-12 23:50:02 -050056MODULE_DESCRIPTION("ACPI Button Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070057MODULE_LICENSE("GPL");
58
Thomas Renninger1ba90e32007-07-23 14:44:41 +020059static const struct acpi_device_id button_device_ids[] = {
60 {ACPI_BUTTON_HID_LID, 0},
61 {ACPI_BUTTON_HID_SLEEP, 0},
62 {ACPI_BUTTON_HID_SLEEPF, 0},
63 {ACPI_BUTTON_HID_POWER, 0},
64 {ACPI_BUTTON_HID_POWERF, 0},
65 {"", 0},
66};
67MODULE_DEVICE_TABLE(acpi, button_device_ids);
68
Hans de Goeded7cd0822019-10-26 22:24:33 +020069/* Please keep this list sorted alphabetically by vendor and model */
70static const struct dmi_system_id dmi_lid_quirks[] = {
Hans de Goede9e811e12017-11-22 16:06:12 +010071 {
Hans de Goeded7cd0822019-10-26 22:24:33 +020072 /* GP-electronic T701, _LID method points to a floating GPIO */
Hans de Goede9e811e12017-11-22 16:06:12 +010073 .matches = {
74 DMI_MATCH(DMI_SYS_VENDOR, "Insyde"),
75 DMI_MATCH(DMI_PRODUCT_NAME, "T701"),
76 DMI_MATCH(DMI_BIOS_VERSION, "BYT70A.YNCHENG.WIN.007"),
77 },
Hans de Goeded7cd0822019-10-26 22:24:33 +020078 .driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_DISABLED,
Hans de Goede9e811e12017-11-22 16:06:12 +010079 },
Hans de Goede932e1ba2019-10-26 22:24:34 +020080 {
81 /*
Ulrich Huber1a20d402021-08-24 14:57:44 +020082 * Lenovo Yoga 9 14ITL5, initial notification of the LID device
83 * never happens.
84 */
85 .matches = {
86 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
87 DMI_MATCH(DMI_PRODUCT_NAME, "82BG"),
88 },
89 .driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_OPEN,
90 },
91 {
92 /*
Hans de Goede932e1ba2019-10-26 22:24:34 +020093 * Medion Akoya E2215T, notification of the LID device only
94 * happens on close, not on open and _LID always returns closed.
95 */
96 .matches = {
97 DMI_MATCH(DMI_SYS_VENDOR, "MEDION"),
Hans de Goede7daaa062020-11-07 14:32:54 +010098 DMI_MATCH(DMI_PRODUCT_NAME, "E2215T"),
99 },
100 .driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_OPEN,
101 },
102 {
103 /*
104 * Medion Akoya E2228T, notification of the LID device only
105 * happens on close, not on open and _LID always returns closed.
106 */
107 .matches = {
108 DMI_MATCH(DMI_SYS_VENDOR, "MEDION"),
109 DMI_MATCH(DMI_PRODUCT_NAME, "E2228T"),
Hans de Goede932e1ba2019-10-26 22:24:34 +0200110 },
111 .driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_OPEN,
112 },
Jason Ekstrand05289042020-01-02 14:27:54 -0600113 {
114 /*
115 * Razer Blade Stealth 13 late 2019, notification of the LID device
116 * only happens on close, not on open and _LID always returns closed.
117 */
118 .matches = {
119 DMI_MATCH(DMI_SYS_VENDOR, "Razer"),
120 DMI_MATCH(DMI_PRODUCT_NAME, "Razer Blade Stealth 13 Late 2019"),
121 },
122 .driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_OPEN,
123 },
Hans de Goede9e811e12017-11-22 16:06:12 +0100124 {}
125};
126
Len Brown4be44fc2005-08-05 00:44:28 -0400127static int acpi_button_add(struct acpi_device *device);
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100128static int acpi_button_remove(struct acpi_device *device);
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000129static void acpi_button_notify(struct acpi_device *device, u32 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200131#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockie71eeb22014-07-23 00:59:04 +0200132static int acpi_button_suspend(struct device *dev);
Rafael J. Wysocki1be532d2012-06-27 23:26:51 +0200133static int acpi_button_resume(struct device *dev);
Shuah Khan2de9fd12014-02-12 20:19:07 -0700134#else
Rafael J. Wysockie71eeb22014-07-23 00:59:04 +0200135#define acpi_button_suspend NULL
Shuah Khan2de9fd12014-02-12 20:19:07 -0700136#define acpi_button_resume NULL
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200137#endif
Rafael J. Wysockie71eeb22014-07-23 00:59:04 +0200138static SIMPLE_DEV_PM_OPS(acpi_button_pm, acpi_button_suspend, acpi_button_resume);
Rafael J. Wysocki1be532d2012-06-27 23:26:51 +0200139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140static struct acpi_driver acpi_button_driver = {
Len Brownc2b67052007-02-12 23:33:40 -0500141 .name = "button",
Len Brown4be44fc2005-08-05 00:44:28 -0400142 .class = ACPI_BUTTON_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200143 .ids = button_device_ids,
Len Brown4be44fc2005-08-05 00:44:28 -0400144 .ops = {
145 .add = acpi_button_add,
146 .remove = acpi_button_remove,
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000147 .notify = acpi_button_notify,
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500148 },
Rafael J. Wysocki1be532d2012-06-27 23:26:51 +0200149 .drv.pm = &acpi_button_pm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150};
151
152struct acpi_button {
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500153 unsigned int type;
154 struct input_dev *input;
155 char phys[32]; /* for input device */
Len Brown4be44fc2005-08-05 00:44:28 -0400156 unsigned long pushed;
Lv Zhengdfa46c52016-08-17 16:22:58 +0800157 int last_state;
158 ktime_t last_time;
Rafael J. Wysockie71eeb22014-07-23 00:59:04 +0200159 bool suspended;
dmitry.torokhov@gmail.com21988a82020-10-04 22:11:25 -0700160 bool lid_state_initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161};
162
Jesse Barnes7e127152009-09-10 15:28:02 -0700163static struct acpi_device *lid_device;
Hans de Goeded7cd0822019-10-26 22:24:33 +0200164static long lid_init_state = -1;
Jesse Barnes7e127152009-09-10 15:28:02 -0700165
Lv Zhengdfa46c52016-08-17 16:22:58 +0800166static unsigned long lid_report_interval __read_mostly = 500;
167module_param(lid_report_interval, ulong, 0644);
168MODULE_PARM_DESC(lid_report_interval, "Interval (ms) between lid key events");
169
Xiaofei Taneffbe642021-03-27 20:08:19 +0800170/* FS Interface (/proc) */
Len Brown4be44fc2005-08-05 00:44:28 -0400171static struct proc_dir_entry *acpi_button_dir;
Zhang Rui912b7422011-03-23 10:21:40 +0800172static struct proc_dir_entry *acpi_lid_dir;
Len Brown4be44fc2005-08-05 00:44:28 -0400173
Lv Zhengee7e2262016-06-01 18:10:42 +0800174static int acpi_lid_evaluate_state(struct acpi_device *device)
175{
176 unsigned long long lid_state;
177 acpi_status status;
178
179 status = acpi_evaluate_integer(device->handle, "_LID", NULL, &lid_state);
180 if (ACPI_FAILURE(status))
181 return -ENODEV;
182
183 return lid_state ? 1 : 0;
184}
185
186static int acpi_lid_notify_state(struct acpi_device *device, int state)
187{
188 struct acpi_button *button = acpi_driver_data(device);
Lv Zhengdfa46c52016-08-17 16:22:58 +0800189 ktime_t next_report;
190 bool do_update;
Lv Zhengee7e2262016-06-01 18:10:42 +0800191
Lv Zhengdfa46c52016-08-17 16:22:58 +0800192 /*
193 * In lid_init_state=ignore mode, if user opens/closes lid
194 * frequently with "open" missing, and "last_time" is also updated
195 * frequently, "close" cannot be delivered to the userspace.
196 * So "last_time" is only updated after a timeout or an actual
197 * switch.
198 */
199 if (lid_init_state != ACPI_BUTTON_LID_INIT_IGNORE ||
200 button->last_state != !!state)
201 do_update = true;
202 else
203 do_update = false;
204
205 next_report = ktime_add(button->last_time,
206 ms_to_ktime(lid_report_interval));
207 if (button->last_state == !!state &&
208 ktime_after(ktime_get(), next_report)) {
209 /* Complain the buggy firmware */
210 pr_warn_once("The lid device is not compliant to SW_LID.\n");
211
212 /*
213 * Send the unreliable complement switch event:
214 *
215 * On most platforms, the lid device is reliable. However
216 * there are exceptions:
217 * 1. Platforms returning initial lid state as "close" by
218 * default after booting/resuming:
219 * https://bugzilla.kernel.org/show_bug.cgi?id=89211
220 * https://bugzilla.kernel.org/show_bug.cgi?id=106151
221 * 2. Platforms never reporting "open" events:
222 * https://bugzilla.kernel.org/show_bug.cgi?id=106941
223 * On these buggy platforms, the usage model of the ACPI
224 * lid device actually is:
225 * 1. The initial returning value of _LID may not be
226 * reliable.
227 * 2. The open event may not be reliable.
228 * 3. The close event is reliable.
229 *
230 * But SW_LID is typed as input switch event, the input
231 * layer checks if the event is redundant. Hence if the
232 * state is not switched, the userspace cannot see this
233 * platform triggered reliable event. By inserting a
234 * complement switch event, it then is guaranteed that the
235 * platform triggered reliable one can always be seen by
236 * the userspace.
237 */
238 if (lid_init_state == ACPI_BUTTON_LID_INIT_IGNORE) {
239 do_update = true;
240 /*
241 * Do generate complement switch event for "close"
242 * as "close" is reliable and wrong "open" won't
243 * trigger unexpected behaviors.
244 * Do not generate complement switch event for
245 * "open" as "open" is not reliable and wrong
246 * "close" will trigger unexpected behaviors.
247 */
248 if (!state) {
249 input_report_switch(button->input,
250 SW_LID, state);
251 input_sync(button->input);
252 }
253 }
254 }
255 /* Send the platform triggered reliable event */
256 if (do_update) {
Hans de Goedeae35d652017-11-22 16:06:11 +0100257 acpi_handle_debug(device->handle, "ACPI LID %s\n",
258 state ? "open" : "closed");
Lv Zhengdfa46c52016-08-17 16:22:58 +0800259 input_report_switch(button->input, SW_LID, !state);
260 input_sync(button->input);
261 button->last_state = !!state;
262 button->last_time = ktime_get();
263 }
Lv Zhengee7e2262016-06-01 18:10:42 +0800264
Hans de Goedee346d0c2019-10-26 22:24:36 +0200265 return 0;
Lv Zhengee7e2262016-06-01 18:10:42 +0800266}
267
Randy Dunlap2c4c2a72018-07-07 08:25:01 -0700268static int __maybe_unused acpi_button_state_seq_show(struct seq_file *seq,
269 void *offset)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400270{
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000271 struct acpi_device *device = seq->private;
Lv Zhengee7e2262016-06-01 18:10:42 +0800272 int state;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400273
Lv Zhengee7e2262016-06-01 18:10:42 +0800274 state = acpi_lid_evaluate_state(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500275 seq_printf(seq, "state: %s\n",
Lv Zhengee7e2262016-06-01 18:10:42 +0800276 state < 0 ? "unsupported" : (state ? "open" : "closed"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400277 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400278}
279
Len Brown4be44fc2005-08-05 00:44:28 -0400280static int acpi_button_add_fs(struct acpi_device *device)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400281{
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000282 struct acpi_button *button = acpi_driver_data(device);
Len Brown4be44fc2005-08-05 00:44:28 -0400283 struct proc_dir_entry *entry = NULL;
Zhang Rui912b7422011-03-23 10:21:40 +0800284 int ret = 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400285
Zhang Rui912b7422011-03-23 10:21:40 +0800286 /* procfs I/F for ACPI lid device only */
287 if (button->type != ACPI_BUTTON_TYPE_LID)
288 return 0;
289
290 if (acpi_button_dir || acpi_lid_dir) {
Rafael J. Wysocki411e3212021-02-03 19:46:14 +0100291 pr_info("More than one Lid device found!\n");
Zhang Rui912b7422011-03-23 10:21:40 +0800292 return -EEXIST;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400293 }
294
Zhang Rui912b7422011-03-23 10:21:40 +0800295 /* create /proc/acpi/button */
296 acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
297 if (!acpi_button_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400298 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400299
Zhang Rui912b7422011-03-23 10:21:40 +0800300 /* create /proc/acpi/button/lid */
301 acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
302 if (!acpi_lid_dir) {
303 ret = -ENODEV;
304 goto remove_button_dir;
305 }
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400306
Zhang Rui912b7422011-03-23 10:21:40 +0800307 /* create /proc/acpi/button/lid/LID/ */
308 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), acpi_lid_dir);
309 if (!acpi_device_dir(device)) {
310 ret = -ENODEV;
311 goto remove_lid_dir;
312 }
313
314 /* create /proc/acpi/button/lid/LID/state */
Christoph Hellwig3f3942a2018-05-15 15:57:23 +0200315 entry = proc_create_single_data(ACPI_BUTTON_FILE_STATE, S_IRUGO,
316 acpi_device_dir(device), acpi_button_state_seq_show,
317 device);
Zhang Rui912b7422011-03-23 10:21:40 +0800318 if (!entry) {
319 ret = -ENODEV;
320 goto remove_dev_dir;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400321 }
322
Zhang Rui912b7422011-03-23 10:21:40 +0800323done:
324 return ret;
325
326remove_dev_dir:
327 remove_proc_entry(acpi_device_bid(device),
328 acpi_lid_dir);
329 acpi_device_dir(device) = NULL;
330remove_lid_dir:
331 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
Benjamin Tissoirese370cc82016-07-29 17:08:41 +0200332 acpi_lid_dir = NULL;
Zhang Rui912b7422011-03-23 10:21:40 +0800333remove_button_dir:
334 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
Benjamin Tissoirese370cc82016-07-29 17:08:41 +0200335 acpi_button_dir = NULL;
Zhang Rui912b7422011-03-23 10:21:40 +0800336 goto done;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400337}
338
Len Brown4be44fc2005-08-05 00:44:28 -0400339static int acpi_button_remove_fs(struct acpi_device *device)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400340{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500341 struct acpi_button *button = acpi_driver_data(device);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400342
Zhang Rui912b7422011-03-23 10:21:40 +0800343 if (button->type != ACPI_BUTTON_TYPE_LID)
344 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400345
Zhang Rui912b7422011-03-23 10:21:40 +0800346 remove_proc_entry(ACPI_BUTTON_FILE_STATE,
347 acpi_device_dir(device));
348 remove_proc_entry(acpi_device_bid(device),
349 acpi_lid_dir);
350 acpi_device_dir(device) = NULL;
351 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
Benjamin Tissoirese370cc82016-07-29 17:08:41 +0200352 acpi_lid_dir = NULL;
Zhang Rui912b7422011-03-23 10:21:40 +0800353 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
Benjamin Tissoirese370cc82016-07-29 17:08:41 +0200354 acpi_button_dir = NULL;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400355
Patrick Mocheld550d982006-06-27 00:41:40 -0400356 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400357}
358
Xiaofei Taneffbe642021-03-27 20:08:19 +0800359/* Driver Interface */
Jesse Barnes7e127152009-09-10 15:28:02 -0700360int acpi_lid_open(void)
361{
Jesse Barnes2c907b72009-10-07 14:39:46 -0700362 if (!lid_device)
363 return -ENODEV;
364
Lv Zhengee7e2262016-06-01 18:10:42 +0800365 return acpi_lid_evaluate_state(lid_device);
Jesse Barnes7e127152009-09-10 15:28:02 -0700366}
367EXPORT_SYMBOL(acpi_lid_open);
368
Ravi Chandra Sadineni7c058c72018-06-27 10:55:02 -0700369static int acpi_lid_update_state(struct acpi_device *device,
370 bool signal_wakeup)
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400371{
Lv Zhengee7e2262016-06-01 18:10:42 +0800372 int state;
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400373
Lv Zhengee7e2262016-06-01 18:10:42 +0800374 state = acpi_lid_evaluate_state(device);
375 if (state < 0)
376 return state;
Bjorn Helgaas50a4da82009-04-08 15:39:38 +0000377
Ravi Chandra Sadineni7c058c72018-06-27 10:55:02 -0700378 if (state && signal_wakeup)
379 acpi_pm_wakeup_event(&device->dev);
380
Lv Zhengee7e2262016-06-01 18:10:42 +0800381 return acpi_lid_notify_state(device, state);
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400382}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Lv Zheng3540c322016-06-01 18:10:48 +0800384static void acpi_lid_initialize_state(struct acpi_device *device)
385{
dmitry.torokhov@gmail.com21988a82020-10-04 22:11:25 -0700386 struct acpi_button *button = acpi_driver_data(device);
387
Lv Zheng3540c322016-06-01 18:10:48 +0800388 switch (lid_init_state) {
389 case ACPI_BUTTON_LID_INIT_OPEN:
390 (void)acpi_lid_notify_state(device, 1);
391 break;
Lv Zhengf369fdf2017-05-09 15:02:22 +0800392 case ACPI_BUTTON_LID_INIT_METHOD:
Ravi Chandra Sadineni7c058c72018-06-27 10:55:02 -0700393 (void)acpi_lid_update_state(device, false);
Lv Zhengf369fdf2017-05-09 15:02:22 +0800394 break;
Lv Zheng3540c322016-06-01 18:10:48 +0800395 case ACPI_BUTTON_LID_INIT_IGNORE:
396 default:
397 break;
398 }
dmitry.torokhov@gmail.com21988a82020-10-04 22:11:25 -0700399
400 button->lid_state_initialized = true;
Lv Zheng3540c322016-06-01 18:10:48 +0800401}
402
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000403static void acpi_button_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000405 struct acpi_button *button = acpi_driver_data(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500406 struct input_dev *input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 switch (event) {
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000409 case ACPI_FIXED_HARDWARE_EVENT:
410 event = ACPI_BUTTON_NOTIFY_STATUS;
Gustavo A. R. Silva57d2dd42020-07-07 15:09:37 -0500411 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 case ACPI_BUTTON_NOTIFY_STATUS:
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500413 input = button->input;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500414 if (button->type == ACPI_BUTTON_TYPE_LID) {
dmitry.torokhov@gmail.com21988a82020-10-04 22:11:25 -0700415 if (button->lid_state_initialized)
Ravi Chandra Sadineni7c058c72018-06-27 10:55:02 -0700416 acpi_lid_update_state(device, true);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500417 } else {
Rafael J. Wysockie71eeb22014-07-23 00:59:04 +0200418 int keycode;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500419
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200420 acpi_pm_wakeup_event(&device->dev);
Rafael J. Wysockie71eeb22014-07-23 00:59:04 +0200421 if (button->suspended)
422 break;
423
424 keycode = test_bit(KEY_SLEEP, input->keybit) ?
425 KEY_SLEEP : KEY_POWER;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500426 input_report_key(input, keycode, 1);
427 input_sync(input);
428 input_report_key(input, keycode, 0);
Guillem Joverdf316e92008-10-24 00:28:33 +0300429 input_sync(input);
Rafael J. Wysocki1f835112011-01-06 23:36:01 +0100430
Lan Tianyu0bf63682014-03-15 13:37:13 -0400431 acpi_bus_generate_netlink_event(
432 device->pnp.device_class,
433 dev_name(&device->dev),
434 event, ++button->pushed);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 break;
437 default:
Rafael J. Wysocki411e3212021-02-03 19:46:14 +0100438 acpi_handle_debug(device->handle, "Unsupported event [0x%x]\n",
439 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 break;
441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200444#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockie71eeb22014-07-23 00:59:04 +0200445static int acpi_button_suspend(struct device *dev)
446{
447 struct acpi_device *device = to_acpi_device(dev);
448 struct acpi_button *button = acpi_driver_data(device);
449
450 button->suspended = true;
451 return 0;
452}
453
Rafael J. Wysocki1be532d2012-06-27 23:26:51 +0200454static int acpi_button_resume(struct device *dev)
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400455{
Rafael J. Wysocki1be532d2012-06-27 23:26:51 +0200456 struct acpi_device *device = to_acpi_device(dev);
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000457 struct acpi_button *button = acpi_driver_data(device);
Bjorn Helgaas50a4da82009-04-08 15:39:38 +0000458
Rafael J. Wysockie71eeb22014-07-23 00:59:04 +0200459 button->suspended = false;
dmitry.torokhov@gmail.com21988a82020-10-04 22:11:25 -0700460 if (button->type == ACPI_BUTTON_TYPE_LID) {
Zhang Rui13e96212019-04-02 21:38:32 +0800461 button->last_state = !!acpi_lid_evaluate_state(device);
462 button->last_time = ktime_get();
Lv Zheng3540c322016-06-01 18:10:48 +0800463 acpi_lid_initialize_state(device);
Zhang Rui13e96212019-04-02 21:38:32 +0800464 }
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400465 return 0;
466}
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200467#endif
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400468
Hans de Goede84d3f6b2017-09-11 16:07:06 +0200469static int acpi_lid_input_open(struct input_dev *input)
470{
471 struct acpi_device *device = input_get_drvdata(input);
472 struct acpi_button *button = acpi_driver_data(device);
473
474 button->last_state = !!acpi_lid_evaluate_state(device);
475 button->last_time = ktime_get();
476 acpi_lid_initialize_state(device);
477
478 return 0;
479}
480
Len Brown4be44fc2005-08-05 00:44:28 -0400481static int acpi_button_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500483 struct acpi_button *button;
484 struct input_dev *input;
Thomas Renninger620e1122010-10-01 10:54:00 +0200485 const char *hid = acpi_device_hid(device);
486 char *name, *class;
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000487 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Hans de Goede593681e2019-10-26 22:24:32 +0200489 if (!strcmp(hid, ACPI_BUTTON_HID_LID) &&
Hans de Goeded7cd0822019-10-26 22:24:33 +0200490 lid_init_state == ACPI_BUTTON_LID_INIT_DISABLED)
Hans de Goede9e811e12017-11-22 16:06:12 +0100491 return -ENODEV;
492
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500493 button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (!button)
Patrick Mocheld550d982006-06-27 00:41:40 -0400495 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700497 device->driver_data = button;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500499 button->input = input = input_allocate_device();
500 if (!input) {
501 error = -ENOMEM;
502 goto err_free_button;
503 }
504
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000505 name = acpi_device_name(device);
506 class = acpi_device_class(device);
507
Bjorn Helgaasd68b5972009-04-08 15:40:04 +0000508 if (!strcmp(hid, ACPI_BUTTON_HID_POWER) ||
509 !strcmp(hid, ACPI_BUTTON_HID_POWERF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 button->type = ACPI_BUTTON_TYPE_POWER;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000511 strcpy(name, ACPI_BUTTON_DEVICE_NAME_POWER);
512 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
Bjorn Helgaasd68b5972009-04-08 15:40:04 +0000514 } else if (!strcmp(hid, ACPI_BUTTON_HID_SLEEP) ||
515 !strcmp(hid, ACPI_BUTTON_HID_SLEEPF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 button->type = ACPI_BUTTON_TYPE_SLEEP;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000517 strcpy(name, ACPI_BUTTON_DEVICE_NAME_SLEEP);
518 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000520 } else if (!strcmp(hid, ACPI_BUTTON_HID_LID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 button->type = ACPI_BUTTON_TYPE_LID;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000522 strcpy(name, ACPI_BUTTON_DEVICE_NAME_LID);
523 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
Hans de Goede84d3f6b2017-09-11 16:07:06 +0200525 input->open = acpi_lid_input_open;
Len Brown4be44fc2005-08-05 00:44:28 -0400526 } else {
Rafael J. Wysocki411e3212021-02-03 19:46:14 +0100527 pr_info("Unsupported hid [%s]\n", hid);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500528 error = -ENODEV;
529 goto err_free_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
531
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500532 error = acpi_button_add_fs(device);
533 if (error)
534 goto err_free_input;
535
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000536 snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500537
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000538 input->name = name;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500539 input->phys = button->phys;
540 input->id.bustype = BUS_HOST;
541 input->id.product = button->type;
Andrey Borzenkov3b34e522008-03-04 15:06:35 -0800542 input->dev.parent = &device->dev;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 switch (button->type) {
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500545 case ACPI_BUTTON_TYPE_POWER:
Lan Tianyu763f5272013-09-12 03:32:03 -0400546 input_set_capability(input, EV_KEY, KEY_POWER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 break;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500548
549 case ACPI_BUTTON_TYPE_SLEEP:
Lan Tianyu763f5272013-09-12 03:32:03 -0400550 input_set_capability(input, EV_KEY, KEY_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 break;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500552
553 case ACPI_BUTTON_TYPE_LID:
Lan Tianyu763f5272013-09-12 03:32:03 -0400554 input_set_capability(input, EV_SW, SW_LID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 break;
556 }
557
Hans de Goede84d3f6b2017-09-11 16:07:06 +0200558 input_set_drvdata(input, device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500559 error = input_register_device(input);
560 if (error)
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000561 goto err_remove_fs;
Jesse Barnes7e127152009-09-10 15:28:02 -0700562 if (button->type == ACPI_BUTTON_TYPE_LID) {
Jesse Barnes7e127152009-09-10 15:28:02 -0700563 /*
564 * This assumes there's only one lid device, or if there are
565 * more we only care about the last one...
566 */
567 lid_device = device;
568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200570 device_init_wakeup(&device->dev, true);
Rafael J. Wysocki411e3212021-02-03 19:46:14 +0100571 pr_info("%s [%s]\n", name, acpi_device_bid(device));
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500572 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500574 err_remove_fs:
575 acpi_button_remove_fs(device);
576 err_free_input:
577 input_free_device(input);
578 err_free_button:
579 kfree(button);
580 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100583static int acpi_button_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000585 struct acpi_button *button = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Len Brown4be44fc2005-08-05 00:44:28 -0400587 acpi_button_remove_fs(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500588 input_unregister_device(button->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 kfree(button);
Patrick Mocheld550d982006-06-27 00:41:40 -0400590 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
Kees Cooke4dca7b2017-10-17 19:04:42 -0700593static int param_set_lid_init_state(const char *val,
594 const struct kernel_param *kp)
Lv Zheng3540c322016-06-01 18:10:48 +0800595{
Hans de Goede065bd4d2019-10-26 22:24:31 +0200596 int i;
Lv Zheng3540c322016-06-01 18:10:48 +0800597
Hans de Goede065bd4d2019-10-26 22:24:31 +0200598 i = sysfs_match_string(lid_init_state_str, val);
599 if (i < 0)
600 return i;
601
602 lid_init_state = i;
603 pr_info("Initial lid state set to '%s'\n", lid_init_state_str[i]);
604 return 0;
Lv Zheng3540c322016-06-01 18:10:48 +0800605}
606
Hans de Goede065bd4d2019-10-26 22:24:31 +0200607static int param_get_lid_init_state(char *buf, const struct kernel_param *kp)
Lv Zheng3540c322016-06-01 18:10:48 +0800608{
Hans de Goede065bd4d2019-10-26 22:24:31 +0200609 int i, c = 0;
610
611 for (i = 0; i < ARRAY_SIZE(lid_init_state_str); i++)
612 if (i == lid_init_state)
613 c += sprintf(buf + c, "[%s] ", lid_init_state_str[i]);
614 else
615 c += sprintf(buf + c, "%s ", lid_init_state_str[i]);
616
617 buf[c - 1] = '\n'; /* Replace the final space with a newline */
618
619 return c;
Lv Zheng3540c322016-06-01 18:10:48 +0800620}
621
622module_param_call(lid_init_state,
623 param_set_lid_init_state, param_get_lid_init_state,
624 NULL, 0644);
625MODULE_PARM_DESC(lid_init_state, "Behavior for reporting LID initial state");
626
Ard Biesheuvelac1e55b2018-04-23 11:16:56 +0200627static int acpi_button_register_driver(struct acpi_driver *driver)
628{
Hans de Goeded7cd0822019-10-26 22:24:33 +0200629 const struct dmi_system_id *dmi_id;
630
631 if (lid_init_state == -1) {
632 dmi_id = dmi_first_match(dmi_lid_quirks);
633 if (dmi_id)
634 lid_init_state = (long)dmi_id->driver_data;
635 else
636 lid_init_state = ACPI_BUTTON_LID_INIT_METHOD;
637 }
638
Ard Biesheuvelac1e55b2018-04-23 11:16:56 +0200639 /*
640 * Modules such as nouveau.ko and i915.ko have a link time dependency
641 * on acpi_lid_open(), and would therefore not be loadable on ACPI
642 * capable kernels booted in non-ACPI mode if the return value of
643 * acpi_bus_register_driver() is returned from here with ACPI disabled
644 * when this driver is built as a module.
645 */
646 if (acpi_disabled)
647 return 0;
648
649 return acpi_bus_register_driver(driver);
650}
651
652static void acpi_button_unregister_driver(struct acpi_driver *driver)
653{
654 if (!acpi_disabled)
655 acpi_bus_unregister_driver(driver);
656}
657
658module_driver(acpi_button_driver, acpi_button_register_driver,
659 acpi_button_unregister_driver);