blob: f25bd336113bd6f3676cd9f2be849cbd15821316 [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 /*
82 * Medion Akoya E2215T, notification of the LID device only
83 * happens on close, not on open and _LID always returns closed.
84 */
85 .matches = {
86 DMI_MATCH(DMI_SYS_VENDOR, "MEDION"),
Hans de Goede7daaa062020-11-07 14:32:54 +010087 DMI_MATCH(DMI_PRODUCT_NAME, "E2215T"),
88 },
89 .driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_OPEN,
90 },
91 {
92 /*
93 * Medion Akoya E2228T, 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"),
98 DMI_MATCH(DMI_PRODUCT_NAME, "E2228T"),
Hans de Goede932e1ba2019-10-26 22:24:34 +020099 },
100 .driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_OPEN,
101 },
Jason Ekstrand05289042020-01-02 14:27:54 -0600102 {
103 /*
104 * Razer Blade Stealth 13 late 2019, notification of the LID device
105 * only happens on close, not on open and _LID always returns closed.
106 */
107 .matches = {
108 DMI_MATCH(DMI_SYS_VENDOR, "Razer"),
109 DMI_MATCH(DMI_PRODUCT_NAME, "Razer Blade Stealth 13 Late 2019"),
110 },
111 .driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_OPEN,
112 },
Hans de Goede9e811e12017-11-22 16:06:12 +0100113 {}
114};
115
Len Brown4be44fc2005-08-05 00:44:28 -0400116static int acpi_button_add(struct acpi_device *device);
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100117static int acpi_button_remove(struct acpi_device *device);
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000118static void acpi_button_notify(struct acpi_device *device, u32 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200120#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockie71eeb22014-07-23 00:59:04 +0200121static int acpi_button_suspend(struct device *dev);
Rafael J. Wysocki1be532d2012-06-27 23:26:51 +0200122static int acpi_button_resume(struct device *dev);
Shuah Khan2de9fd12014-02-12 20:19:07 -0700123#else
Rafael J. Wysockie71eeb22014-07-23 00:59:04 +0200124#define acpi_button_suspend NULL
Shuah Khan2de9fd12014-02-12 20:19:07 -0700125#define acpi_button_resume NULL
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200126#endif
Rafael J. Wysockie71eeb22014-07-23 00:59:04 +0200127static SIMPLE_DEV_PM_OPS(acpi_button_pm, acpi_button_suspend, acpi_button_resume);
Rafael J. Wysocki1be532d2012-06-27 23:26:51 +0200128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129static struct acpi_driver acpi_button_driver = {
Len Brownc2b67052007-02-12 23:33:40 -0500130 .name = "button",
Len Brown4be44fc2005-08-05 00:44:28 -0400131 .class = ACPI_BUTTON_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200132 .ids = button_device_ids,
Len Brown4be44fc2005-08-05 00:44:28 -0400133 .ops = {
134 .add = acpi_button_add,
135 .remove = acpi_button_remove,
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000136 .notify = acpi_button_notify,
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500137 },
Rafael J. Wysocki1be532d2012-06-27 23:26:51 +0200138 .drv.pm = &acpi_button_pm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139};
140
141struct acpi_button {
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500142 unsigned int type;
143 struct input_dev *input;
144 char phys[32]; /* for input device */
Len Brown4be44fc2005-08-05 00:44:28 -0400145 unsigned long pushed;
Lv Zhengdfa46c52016-08-17 16:22:58 +0800146 int last_state;
147 ktime_t last_time;
Rafael J. Wysockie71eeb22014-07-23 00:59:04 +0200148 bool suspended;
dmitry.torokhov@gmail.com21988a82020-10-04 22:11:25 -0700149 bool lid_state_initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150};
151
Jesse Barnes7e127152009-09-10 15:28:02 -0700152static struct acpi_device *lid_device;
Hans de Goeded7cd0822019-10-26 22:24:33 +0200153static long lid_init_state = -1;
Jesse Barnes7e127152009-09-10 15:28:02 -0700154
Lv Zhengdfa46c52016-08-17 16:22:58 +0800155static unsigned long lid_report_interval __read_mostly = 500;
156module_param(lid_report_interval, ulong, 0644);
157MODULE_PARM_DESC(lid_report_interval, "Interval (ms) between lid key events");
158
Xiaofei Taneffbe642021-03-27 20:08:19 +0800159/* FS Interface (/proc) */
Len Brown4be44fc2005-08-05 00:44:28 -0400160static struct proc_dir_entry *acpi_button_dir;
Zhang Rui912b7422011-03-23 10:21:40 +0800161static struct proc_dir_entry *acpi_lid_dir;
Len Brown4be44fc2005-08-05 00:44:28 -0400162
Lv Zhengee7e2262016-06-01 18:10:42 +0800163static int acpi_lid_evaluate_state(struct acpi_device *device)
164{
165 unsigned long long lid_state;
166 acpi_status status;
167
168 status = acpi_evaluate_integer(device->handle, "_LID", NULL, &lid_state);
169 if (ACPI_FAILURE(status))
170 return -ENODEV;
171
172 return lid_state ? 1 : 0;
173}
174
175static int acpi_lid_notify_state(struct acpi_device *device, int state)
176{
177 struct acpi_button *button = acpi_driver_data(device);
Lv Zhengdfa46c52016-08-17 16:22:58 +0800178 ktime_t next_report;
179 bool do_update;
Lv Zhengee7e2262016-06-01 18:10:42 +0800180
Lv Zhengdfa46c52016-08-17 16:22:58 +0800181 /*
182 * In lid_init_state=ignore mode, if user opens/closes lid
183 * frequently with "open" missing, and "last_time" is also updated
184 * frequently, "close" cannot be delivered to the userspace.
185 * So "last_time" is only updated after a timeout or an actual
186 * switch.
187 */
188 if (lid_init_state != ACPI_BUTTON_LID_INIT_IGNORE ||
189 button->last_state != !!state)
190 do_update = true;
191 else
192 do_update = false;
193
194 next_report = ktime_add(button->last_time,
195 ms_to_ktime(lid_report_interval));
196 if (button->last_state == !!state &&
197 ktime_after(ktime_get(), next_report)) {
198 /* Complain the buggy firmware */
199 pr_warn_once("The lid device is not compliant to SW_LID.\n");
200
201 /*
202 * Send the unreliable complement switch event:
203 *
204 * On most platforms, the lid device is reliable. However
205 * there are exceptions:
206 * 1. Platforms returning initial lid state as "close" by
207 * default after booting/resuming:
208 * https://bugzilla.kernel.org/show_bug.cgi?id=89211
209 * https://bugzilla.kernel.org/show_bug.cgi?id=106151
210 * 2. Platforms never reporting "open" events:
211 * https://bugzilla.kernel.org/show_bug.cgi?id=106941
212 * On these buggy platforms, the usage model of the ACPI
213 * lid device actually is:
214 * 1. The initial returning value of _LID may not be
215 * reliable.
216 * 2. The open event may not be reliable.
217 * 3. The close event is reliable.
218 *
219 * But SW_LID is typed as input switch event, the input
220 * layer checks if the event is redundant. Hence if the
221 * state is not switched, the userspace cannot see this
222 * platform triggered reliable event. By inserting a
223 * complement switch event, it then is guaranteed that the
224 * platform triggered reliable one can always be seen by
225 * the userspace.
226 */
227 if (lid_init_state == ACPI_BUTTON_LID_INIT_IGNORE) {
228 do_update = true;
229 /*
230 * Do generate complement switch event for "close"
231 * as "close" is reliable and wrong "open" won't
232 * trigger unexpected behaviors.
233 * Do not generate complement switch event for
234 * "open" as "open" is not reliable and wrong
235 * "close" will trigger unexpected behaviors.
236 */
237 if (!state) {
238 input_report_switch(button->input,
239 SW_LID, state);
240 input_sync(button->input);
241 }
242 }
243 }
244 /* Send the platform triggered reliable event */
245 if (do_update) {
Hans de Goedeae35d652017-11-22 16:06:11 +0100246 acpi_handle_debug(device->handle, "ACPI LID %s\n",
247 state ? "open" : "closed");
Lv Zhengdfa46c52016-08-17 16:22:58 +0800248 input_report_switch(button->input, SW_LID, !state);
249 input_sync(button->input);
250 button->last_state = !!state;
251 button->last_time = ktime_get();
252 }
Lv Zhengee7e2262016-06-01 18:10:42 +0800253
Hans de Goedee346d0c2019-10-26 22:24:36 +0200254 return 0;
Lv Zhengee7e2262016-06-01 18:10:42 +0800255}
256
Randy Dunlap2c4c2a72018-07-07 08:25:01 -0700257static int __maybe_unused acpi_button_state_seq_show(struct seq_file *seq,
258 void *offset)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400259{
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000260 struct acpi_device *device = seq->private;
Lv Zhengee7e2262016-06-01 18:10:42 +0800261 int state;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400262
Lv Zhengee7e2262016-06-01 18:10:42 +0800263 state = acpi_lid_evaluate_state(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500264 seq_printf(seq, "state: %s\n",
Lv Zhengee7e2262016-06-01 18:10:42 +0800265 state < 0 ? "unsupported" : (state ? "open" : "closed"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400266 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400267}
268
Len Brown4be44fc2005-08-05 00:44:28 -0400269static int acpi_button_add_fs(struct acpi_device *device)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400270{
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000271 struct acpi_button *button = acpi_driver_data(device);
Len Brown4be44fc2005-08-05 00:44:28 -0400272 struct proc_dir_entry *entry = NULL;
Zhang Rui912b7422011-03-23 10:21:40 +0800273 int ret = 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400274
Zhang Rui912b7422011-03-23 10:21:40 +0800275 /* procfs I/F for ACPI lid device only */
276 if (button->type != ACPI_BUTTON_TYPE_LID)
277 return 0;
278
279 if (acpi_button_dir || acpi_lid_dir) {
Rafael J. Wysocki411e3212021-02-03 19:46:14 +0100280 pr_info("More than one Lid device found!\n");
Zhang Rui912b7422011-03-23 10:21:40 +0800281 return -EEXIST;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400282 }
283
Zhang Rui912b7422011-03-23 10:21:40 +0800284 /* create /proc/acpi/button */
285 acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
286 if (!acpi_button_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400287 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400288
Zhang Rui912b7422011-03-23 10:21:40 +0800289 /* create /proc/acpi/button/lid */
290 acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
291 if (!acpi_lid_dir) {
292 ret = -ENODEV;
293 goto remove_button_dir;
294 }
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400295
Zhang Rui912b7422011-03-23 10:21:40 +0800296 /* create /proc/acpi/button/lid/LID/ */
297 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), acpi_lid_dir);
298 if (!acpi_device_dir(device)) {
299 ret = -ENODEV;
300 goto remove_lid_dir;
301 }
302
303 /* create /proc/acpi/button/lid/LID/state */
Christoph Hellwig3f3942a2018-05-15 15:57:23 +0200304 entry = proc_create_single_data(ACPI_BUTTON_FILE_STATE, S_IRUGO,
305 acpi_device_dir(device), acpi_button_state_seq_show,
306 device);
Zhang Rui912b7422011-03-23 10:21:40 +0800307 if (!entry) {
308 ret = -ENODEV;
309 goto remove_dev_dir;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400310 }
311
Zhang Rui912b7422011-03-23 10:21:40 +0800312done:
313 return ret;
314
315remove_dev_dir:
316 remove_proc_entry(acpi_device_bid(device),
317 acpi_lid_dir);
318 acpi_device_dir(device) = NULL;
319remove_lid_dir:
320 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
Benjamin Tissoirese370cc82016-07-29 17:08:41 +0200321 acpi_lid_dir = NULL;
Zhang Rui912b7422011-03-23 10:21:40 +0800322remove_button_dir:
323 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
Benjamin Tissoirese370cc82016-07-29 17:08:41 +0200324 acpi_button_dir = NULL;
Zhang Rui912b7422011-03-23 10:21:40 +0800325 goto done;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400326}
327
Len Brown4be44fc2005-08-05 00:44:28 -0400328static int acpi_button_remove_fs(struct acpi_device *device)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400329{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500330 struct acpi_button *button = acpi_driver_data(device);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400331
Zhang Rui912b7422011-03-23 10:21:40 +0800332 if (button->type != ACPI_BUTTON_TYPE_LID)
333 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400334
Zhang Rui912b7422011-03-23 10:21:40 +0800335 remove_proc_entry(ACPI_BUTTON_FILE_STATE,
336 acpi_device_dir(device));
337 remove_proc_entry(acpi_device_bid(device),
338 acpi_lid_dir);
339 acpi_device_dir(device) = NULL;
340 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
Benjamin Tissoirese370cc82016-07-29 17:08:41 +0200341 acpi_lid_dir = NULL;
Zhang Rui912b7422011-03-23 10:21:40 +0800342 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
Benjamin Tissoirese370cc82016-07-29 17:08:41 +0200343 acpi_button_dir = NULL;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400344
Patrick Mocheld550d982006-06-27 00:41:40 -0400345 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400346}
347
Xiaofei Taneffbe642021-03-27 20:08:19 +0800348/* Driver Interface */
Jesse Barnes7e127152009-09-10 15:28:02 -0700349int acpi_lid_open(void)
350{
Jesse Barnes2c907b72009-10-07 14:39:46 -0700351 if (!lid_device)
352 return -ENODEV;
353
Lv Zhengee7e2262016-06-01 18:10:42 +0800354 return acpi_lid_evaluate_state(lid_device);
Jesse Barnes7e127152009-09-10 15:28:02 -0700355}
356EXPORT_SYMBOL(acpi_lid_open);
357
Ravi Chandra Sadineni7c058c7c2018-06-27 10:55:02 -0700358static int acpi_lid_update_state(struct acpi_device *device,
359 bool signal_wakeup)
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400360{
Lv Zhengee7e2262016-06-01 18:10:42 +0800361 int state;
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400362
Lv Zhengee7e2262016-06-01 18:10:42 +0800363 state = acpi_lid_evaluate_state(device);
364 if (state < 0)
365 return state;
Bjorn Helgaas50a4da82009-04-08 15:39:38 +0000366
Ravi Chandra Sadineni7c058c7c2018-06-27 10:55:02 -0700367 if (state && signal_wakeup)
368 acpi_pm_wakeup_event(&device->dev);
369
Lv Zhengee7e2262016-06-01 18:10:42 +0800370 return acpi_lid_notify_state(device, state);
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400371}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Lv Zheng3540c322016-06-01 18:10:48 +0800373static void acpi_lid_initialize_state(struct acpi_device *device)
374{
dmitry.torokhov@gmail.com21988a82020-10-04 22:11:25 -0700375 struct acpi_button *button = acpi_driver_data(device);
376
Lv Zheng3540c322016-06-01 18:10:48 +0800377 switch (lid_init_state) {
378 case ACPI_BUTTON_LID_INIT_OPEN:
379 (void)acpi_lid_notify_state(device, 1);
380 break;
Lv Zhengf369fdf2017-05-09 15:02:22 +0800381 case ACPI_BUTTON_LID_INIT_METHOD:
Ravi Chandra Sadineni7c058c7c2018-06-27 10:55:02 -0700382 (void)acpi_lid_update_state(device, false);
Lv Zhengf369fdf2017-05-09 15:02:22 +0800383 break;
Lv Zheng3540c322016-06-01 18:10:48 +0800384 case ACPI_BUTTON_LID_INIT_IGNORE:
385 default:
386 break;
387 }
dmitry.torokhov@gmail.com21988a82020-10-04 22:11:25 -0700388
389 button->lid_state_initialized = true;
Lv Zheng3540c322016-06-01 18:10:48 +0800390}
391
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000392static void acpi_button_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000394 struct acpi_button *button = acpi_driver_data(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500395 struct input_dev *input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 switch (event) {
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000398 case ACPI_FIXED_HARDWARE_EVENT:
399 event = ACPI_BUTTON_NOTIFY_STATUS;
Gustavo A. R. Silva57d2dd42020-07-07 15:09:37 -0500400 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 case ACPI_BUTTON_NOTIFY_STATUS:
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500402 input = button->input;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500403 if (button->type == ACPI_BUTTON_TYPE_LID) {
dmitry.torokhov@gmail.com21988a82020-10-04 22:11:25 -0700404 if (button->lid_state_initialized)
Ravi Chandra Sadineni7c058c7c2018-06-27 10:55:02 -0700405 acpi_lid_update_state(device, true);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500406 } else {
Rafael J. Wysockie71eeb22014-07-23 00:59:04 +0200407 int keycode;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500408
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200409 acpi_pm_wakeup_event(&device->dev);
Rafael J. Wysockie71eeb22014-07-23 00:59:04 +0200410 if (button->suspended)
411 break;
412
413 keycode = test_bit(KEY_SLEEP, input->keybit) ?
414 KEY_SLEEP : KEY_POWER;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500415 input_report_key(input, keycode, 1);
416 input_sync(input);
417 input_report_key(input, keycode, 0);
Guillem Joverdf316e92008-10-24 00:28:33 +0300418 input_sync(input);
Rafael J. Wysocki1f835112011-01-06 23:36:01 +0100419
Lan Tianyu0bf63682014-03-15 13:37:13 -0400420 acpi_bus_generate_netlink_event(
421 device->pnp.device_class,
422 dev_name(&device->dev),
423 event, ++button->pushed);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 break;
426 default:
Rafael J. Wysocki411e3212021-02-03 19:46:14 +0100427 acpi_handle_debug(device->handle, "Unsupported event [0x%x]\n",
428 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 break;
430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200433#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockie71eeb22014-07-23 00:59:04 +0200434static int acpi_button_suspend(struct device *dev)
435{
436 struct acpi_device *device = to_acpi_device(dev);
437 struct acpi_button *button = acpi_driver_data(device);
438
439 button->suspended = true;
440 return 0;
441}
442
Rafael J. Wysocki1be532d2012-06-27 23:26:51 +0200443static int acpi_button_resume(struct device *dev)
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400444{
Rafael J. Wysocki1be532d2012-06-27 23:26:51 +0200445 struct acpi_device *device = to_acpi_device(dev);
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000446 struct acpi_button *button = acpi_driver_data(device);
Bjorn Helgaas50a4da82009-04-08 15:39:38 +0000447
Rafael J. Wysockie71eeb22014-07-23 00:59:04 +0200448 button->suspended = false;
dmitry.torokhov@gmail.com21988a82020-10-04 22:11:25 -0700449 if (button->type == ACPI_BUTTON_TYPE_LID) {
Zhang Rui13e96212019-04-02 21:38:32 +0800450 button->last_state = !!acpi_lid_evaluate_state(device);
451 button->last_time = ktime_get();
Lv Zheng3540c322016-06-01 18:10:48 +0800452 acpi_lid_initialize_state(device);
Zhang Rui13e96212019-04-02 21:38:32 +0800453 }
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400454 return 0;
455}
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200456#endif
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400457
Hans de Goede84d3f6b2017-09-11 16:07:06 +0200458static int acpi_lid_input_open(struct input_dev *input)
459{
460 struct acpi_device *device = input_get_drvdata(input);
461 struct acpi_button *button = acpi_driver_data(device);
462
463 button->last_state = !!acpi_lid_evaluate_state(device);
464 button->last_time = ktime_get();
465 acpi_lid_initialize_state(device);
466
467 return 0;
468}
469
Len Brown4be44fc2005-08-05 00:44:28 -0400470static int acpi_button_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500472 struct acpi_button *button;
473 struct input_dev *input;
Thomas Renninger620e1122010-10-01 10:54:00 +0200474 const char *hid = acpi_device_hid(device);
475 char *name, *class;
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000476 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Hans de Goede593681e2019-10-26 22:24:32 +0200478 if (!strcmp(hid, ACPI_BUTTON_HID_LID) &&
Hans de Goeded7cd0822019-10-26 22:24:33 +0200479 lid_init_state == ACPI_BUTTON_LID_INIT_DISABLED)
Hans de Goede9e811e12017-11-22 16:06:12 +0100480 return -ENODEV;
481
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500482 button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (!button)
Patrick Mocheld550d982006-06-27 00:41:40 -0400484 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700486 device->driver_data = button;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500488 button->input = input = input_allocate_device();
489 if (!input) {
490 error = -ENOMEM;
491 goto err_free_button;
492 }
493
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000494 name = acpi_device_name(device);
495 class = acpi_device_class(device);
496
Bjorn Helgaasd68b5972009-04-08 15:40:04 +0000497 if (!strcmp(hid, ACPI_BUTTON_HID_POWER) ||
498 !strcmp(hid, ACPI_BUTTON_HID_POWERF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 button->type = ACPI_BUTTON_TYPE_POWER;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000500 strcpy(name, ACPI_BUTTON_DEVICE_NAME_POWER);
501 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
Bjorn Helgaasd68b5972009-04-08 15:40:04 +0000503 } else if (!strcmp(hid, ACPI_BUTTON_HID_SLEEP) ||
504 !strcmp(hid, ACPI_BUTTON_HID_SLEEPF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 button->type = ACPI_BUTTON_TYPE_SLEEP;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000506 strcpy(name, ACPI_BUTTON_DEVICE_NAME_SLEEP);
507 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000509 } else if (!strcmp(hid, ACPI_BUTTON_HID_LID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 button->type = ACPI_BUTTON_TYPE_LID;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000511 strcpy(name, ACPI_BUTTON_DEVICE_NAME_LID);
512 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
Hans de Goede84d3f6b2017-09-11 16:07:06 +0200514 input->open = acpi_lid_input_open;
Len Brown4be44fc2005-08-05 00:44:28 -0400515 } else {
Rafael J. Wysocki411e3212021-02-03 19:46:14 +0100516 pr_info("Unsupported hid [%s]\n", hid);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500517 error = -ENODEV;
518 goto err_free_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500521 error = acpi_button_add_fs(device);
522 if (error)
523 goto err_free_input;
524
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000525 snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500526
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000527 input->name = name;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500528 input->phys = button->phys;
529 input->id.bustype = BUS_HOST;
530 input->id.product = button->type;
Andrey Borzenkov3b34e522008-03-04 15:06:35 -0800531 input->dev.parent = &device->dev;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 switch (button->type) {
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500534 case ACPI_BUTTON_TYPE_POWER:
Lan Tianyu763f5272013-09-12 03:32:03 -0400535 input_set_capability(input, EV_KEY, KEY_POWER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 break;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500537
538 case ACPI_BUTTON_TYPE_SLEEP:
Lan Tianyu763f5272013-09-12 03:32:03 -0400539 input_set_capability(input, EV_KEY, KEY_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 break;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500541
542 case ACPI_BUTTON_TYPE_LID:
Lan Tianyu763f5272013-09-12 03:32:03 -0400543 input_set_capability(input, EV_SW, SW_LID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 break;
545 }
546
Hans de Goede84d3f6b2017-09-11 16:07:06 +0200547 input_set_drvdata(input, device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500548 error = input_register_device(input);
549 if (error)
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000550 goto err_remove_fs;
Jesse Barnes7e127152009-09-10 15:28:02 -0700551 if (button->type == ACPI_BUTTON_TYPE_LID) {
Jesse Barnes7e127152009-09-10 15:28:02 -0700552 /*
553 * This assumes there's only one lid device, or if there are
554 * more we only care about the last one...
555 */
556 lid_device = device;
557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200559 device_init_wakeup(&device->dev, true);
Rafael J. Wysocki411e3212021-02-03 19:46:14 +0100560 pr_info("%s [%s]\n", name, acpi_device_bid(device));
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500561 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500563 err_remove_fs:
564 acpi_button_remove_fs(device);
565 err_free_input:
566 input_free_device(input);
567 err_free_button:
568 kfree(button);
569 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100572static int acpi_button_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000574 struct acpi_button *button = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Len Brown4be44fc2005-08-05 00:44:28 -0400576 acpi_button_remove_fs(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500577 input_unregister_device(button->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 kfree(button);
Patrick Mocheld550d982006-06-27 00:41:40 -0400579 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
Kees Cooke4dca7b2017-10-17 19:04:42 -0700582static int param_set_lid_init_state(const char *val,
583 const struct kernel_param *kp)
Lv Zheng3540c322016-06-01 18:10:48 +0800584{
Hans de Goede065bd4d2019-10-26 22:24:31 +0200585 int i;
Lv Zheng3540c322016-06-01 18:10:48 +0800586
Hans de Goede065bd4d2019-10-26 22:24:31 +0200587 i = sysfs_match_string(lid_init_state_str, val);
588 if (i < 0)
589 return i;
590
591 lid_init_state = i;
592 pr_info("Initial lid state set to '%s'\n", lid_init_state_str[i]);
593 return 0;
Lv Zheng3540c322016-06-01 18:10:48 +0800594}
595
Hans de Goede065bd4d2019-10-26 22:24:31 +0200596static int param_get_lid_init_state(char *buf, const struct kernel_param *kp)
Lv Zheng3540c322016-06-01 18:10:48 +0800597{
Hans de Goede065bd4d2019-10-26 22:24:31 +0200598 int i, c = 0;
599
600 for (i = 0; i < ARRAY_SIZE(lid_init_state_str); i++)
601 if (i == lid_init_state)
602 c += sprintf(buf + c, "[%s] ", lid_init_state_str[i]);
603 else
604 c += sprintf(buf + c, "%s ", lid_init_state_str[i]);
605
606 buf[c - 1] = '\n'; /* Replace the final space with a newline */
607
608 return c;
Lv Zheng3540c322016-06-01 18:10:48 +0800609}
610
611module_param_call(lid_init_state,
612 param_set_lid_init_state, param_get_lid_init_state,
613 NULL, 0644);
614MODULE_PARM_DESC(lid_init_state, "Behavior for reporting LID initial state");
615
Ard Biesheuvelac1e55b2018-04-23 11:16:56 +0200616static int acpi_button_register_driver(struct acpi_driver *driver)
617{
Hans de Goeded7cd0822019-10-26 22:24:33 +0200618 const struct dmi_system_id *dmi_id;
619
620 if (lid_init_state == -1) {
621 dmi_id = dmi_first_match(dmi_lid_quirks);
622 if (dmi_id)
623 lid_init_state = (long)dmi_id->driver_data;
624 else
625 lid_init_state = ACPI_BUTTON_LID_INIT_METHOD;
626 }
627
Ard Biesheuvelac1e55b2018-04-23 11:16:56 +0200628 /*
629 * Modules such as nouveau.ko and i915.ko have a link time dependency
630 * on acpi_lid_open(), and would therefore not be loadable on ACPI
631 * capable kernels booted in non-ACPI mode if the return value of
632 * acpi_bus_register_driver() is returned from here with ACPI disabled
633 * when this driver is built as a module.
634 */
635 if (acpi_disabled)
636 return 0;
637
638 return acpi_bus_register_driver(driver);
639}
640
641static void acpi_button_unregister_driver(struct acpi_driver *driver)
642{
643 if (!acpi_disabled)
644 acpi_bus_unregister_driver(driver);
645}
646
647module_driver(acpi_button_driver, acpi_button_register_driver,
648 acpi_button_unregister_driver);