blob: f6925f16c4a2ab27cf5c518d6ca4380872079f43 [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
Len Browna192a952009-07-28 16:45:54 -040024#define PREFIX "ACPI: "
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#define ACPI_BUTTON_CLASS "button"
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -040027#define ACPI_BUTTON_FILE_INFO "info"
28#define ACPI_BUTTON_FILE_STATE "state"
29#define ACPI_BUTTON_TYPE_UNKNOWN 0x00
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#define ACPI_BUTTON_NOTIFY_STATUS 0x80
31
32#define ACPI_BUTTON_SUBCLASS_POWER "power"
Len Brown4be44fc2005-08-05 00:44:28 -040033#define ACPI_BUTTON_HID_POWER "PNP0C0C"
Bjorn Helgaasd68b5972009-04-08 15:40:04 +000034#define ACPI_BUTTON_DEVICE_NAME_POWER "Power Button"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#define ACPI_BUTTON_TYPE_POWER 0x01
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#define ACPI_BUTTON_SUBCLASS_SLEEP "sleep"
38#define ACPI_BUTTON_HID_SLEEP "PNP0C0E"
Bjorn Helgaasd68b5972009-04-08 15:40:04 +000039#define ACPI_BUTTON_DEVICE_NAME_SLEEP "Sleep Button"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#define ACPI_BUTTON_TYPE_SLEEP 0x03
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
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
46
Hans de Goede065bd4d2019-10-26 22:24:31 +020047enum {
48 ACPI_BUTTON_LID_INIT_IGNORE,
49 ACPI_BUTTON_LID_INIT_OPEN,
50 ACPI_BUTTON_LID_INIT_METHOD,
Hans de Goede593681e2019-10-26 22:24:32 +020051 ACPI_BUTTON_LID_INIT_DISABLED,
Hans de Goede065bd4d2019-10-26 22:24:31 +020052};
53
54static const char * const lid_init_state_str[] = {
55 [ACPI_BUTTON_LID_INIT_IGNORE] = "ignore",
56 [ACPI_BUTTON_LID_INIT_OPEN] = "open",
57 [ACPI_BUTTON_LID_INIT_METHOD] = "method",
Hans de Goede593681e2019-10-26 22:24:32 +020058 [ACPI_BUTTON_LID_INIT_DISABLED] = "disabled",
Hans de Goede065bd4d2019-10-26 22:24:31 +020059};
Lv Zheng3540c322016-06-01 18:10:48 +080060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define _COMPONENT ACPI_BUTTON_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050062ACPI_MODULE_NAME("button");
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Dmitry Torokhovc0968f02006-11-09 00:40:13 -050064MODULE_AUTHOR("Paul Diefenbaugh");
Len Brown7cda93e2007-02-12 23:50:02 -050065MODULE_DESCRIPTION("ACPI Button Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070066MODULE_LICENSE("GPL");
67
Thomas Renninger1ba90e32007-07-23 14:44:41 +020068static const struct acpi_device_id button_device_ids[] = {
69 {ACPI_BUTTON_HID_LID, 0},
70 {ACPI_BUTTON_HID_SLEEP, 0},
71 {ACPI_BUTTON_HID_SLEEPF, 0},
72 {ACPI_BUTTON_HID_POWER, 0},
73 {ACPI_BUTTON_HID_POWERF, 0},
74 {"", 0},
75};
76MODULE_DEVICE_TABLE(acpi, button_device_ids);
77
Hans de Goeded7cd0822019-10-26 22:24:33 +020078/* Please keep this list sorted alphabetically by vendor and model */
79static const struct dmi_system_id dmi_lid_quirks[] = {
Hans de Goede9e811e12017-11-22 16:06:12 +010080 {
Hans de Goede00e25032019-10-26 22:24:35 +020081 /*
Hans de Goede90ed9c62019-11-18 16:35:56 +010082 * Acer Switch 10 SW5-012. _LID method messes with home and
83 * power button GPIO IRQ settings causing an interrupt storm on
84 * both GPIOs. This is unfixable without a DSDT override, so we
85 * have to disable the lid-switch functionality altogether :|
86 */
87 .matches = {
88 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
89 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire SW5-012"),
90 },
91 .driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_DISABLED,
92 },
93 {
94 /*
Hans de Goede00e25032019-10-26 22:24:35 +020095 * Asus T200TA, _LID keeps reporting closed after every second
96 * openening of the lid. Causing immediate re-suspend after
97 * opening every other open. Using LID_INIT_OPEN fixes this.
98 */
99 .matches = {
100 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
101 DMI_MATCH(DMI_PRODUCT_NAME, "T200TA"),
102 },
103 .driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_OPEN,
104 },
105 {
Hans de Goeded7cd0822019-10-26 22:24:33 +0200106 /* GP-electronic T701, _LID method points to a floating GPIO */
Hans de Goede9e811e12017-11-22 16:06:12 +0100107 .matches = {
108 DMI_MATCH(DMI_SYS_VENDOR, "Insyde"),
109 DMI_MATCH(DMI_PRODUCT_NAME, "T701"),
110 DMI_MATCH(DMI_BIOS_VERSION, "BYT70A.YNCHENG.WIN.007"),
111 },
Hans de Goeded7cd0822019-10-26 22:24:33 +0200112 .driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_DISABLED,
Hans de Goede9e811e12017-11-22 16:06:12 +0100113 },
Hans de Goede932e1ba2019-10-26 22:24:34 +0200114 {
115 /*
116 * Medion Akoya E2215T, notification of the LID device only
117 * happens on close, not on open and _LID always returns closed.
118 */
119 .matches = {
120 DMI_MATCH(DMI_SYS_VENDOR, "MEDION"),
121 DMI_MATCH(DMI_PRODUCT_NAME, "E2215T MD60198"),
122 },
123 .driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_OPEN,
124 },
Jason Ekstrand05289042020-01-02 14:27:54 -0600125 {
126 /*
127 * Razer Blade Stealth 13 late 2019, notification of the LID device
128 * only happens on close, not on open and _LID always returns closed.
129 */
130 .matches = {
131 DMI_MATCH(DMI_SYS_VENDOR, "Razer"),
132 DMI_MATCH(DMI_PRODUCT_NAME, "Razer Blade Stealth 13 Late 2019"),
133 },
134 .driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_OPEN,
135 },
Hans de Goede9e811e12017-11-22 16:06:12 +0100136 {}
137};
138
Len Brown4be44fc2005-08-05 00:44:28 -0400139static int acpi_button_add(struct acpi_device *device);
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100140static int acpi_button_remove(struct acpi_device *device);
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000141static void acpi_button_notify(struct acpi_device *device, u32 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200143#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockie71eeb22014-07-23 00:59:04 +0200144static int acpi_button_suspend(struct device *dev);
Rafael J. Wysocki1be532d2012-06-27 23:26:51 +0200145static int acpi_button_resume(struct device *dev);
Shuah Khan2de9fd12014-02-12 20:19:07 -0700146#else
Rafael J. Wysockie71eeb22014-07-23 00:59:04 +0200147#define acpi_button_suspend NULL
Shuah Khan2de9fd12014-02-12 20:19:07 -0700148#define acpi_button_resume NULL
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200149#endif
Rafael J. Wysockie71eeb22014-07-23 00:59:04 +0200150static SIMPLE_DEV_PM_OPS(acpi_button_pm, acpi_button_suspend, acpi_button_resume);
Rafael J. Wysocki1be532d2012-06-27 23:26:51 +0200151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152static struct acpi_driver acpi_button_driver = {
Len Brownc2b67052007-02-12 23:33:40 -0500153 .name = "button",
Len Brown4be44fc2005-08-05 00:44:28 -0400154 .class = ACPI_BUTTON_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200155 .ids = button_device_ids,
Len Brown4be44fc2005-08-05 00:44:28 -0400156 .ops = {
157 .add = acpi_button_add,
158 .remove = acpi_button_remove,
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000159 .notify = acpi_button_notify,
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500160 },
Rafael J. Wysocki1be532d2012-06-27 23:26:51 +0200161 .drv.pm = &acpi_button_pm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162};
163
164struct acpi_button {
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500165 unsigned int type;
166 struct input_dev *input;
167 char phys[32]; /* for input device */
Len Brown4be44fc2005-08-05 00:44:28 -0400168 unsigned long pushed;
Lv Zhengdfa46c52016-08-17 16:22:58 +0800169 int last_state;
170 ktime_t last_time;
Rafael J. Wysockie71eeb22014-07-23 00:59:04 +0200171 bool suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172};
173
Jesse Barnes7e127152009-09-10 15:28:02 -0700174static struct acpi_device *lid_device;
Hans de Goeded7cd0822019-10-26 22:24:33 +0200175static long lid_init_state = -1;
Jesse Barnes7e127152009-09-10 15:28:02 -0700176
Lv Zhengdfa46c52016-08-17 16:22:58 +0800177static unsigned long lid_report_interval __read_mostly = 500;
178module_param(lid_report_interval, ulong, 0644);
179MODULE_PARM_DESC(lid_report_interval, "Interval (ms) between lid key events");
180
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400181/* --------------------------------------------------------------------------
182 FS Interface (/proc)
183 -------------------------------------------------------------------------- */
184
Len Brown4be44fc2005-08-05 00:44:28 -0400185static struct proc_dir_entry *acpi_button_dir;
Zhang Rui912b7422011-03-23 10:21:40 +0800186static struct proc_dir_entry *acpi_lid_dir;
Len Brown4be44fc2005-08-05 00:44:28 -0400187
Lv Zhengee7e2262016-06-01 18:10:42 +0800188static int acpi_lid_evaluate_state(struct acpi_device *device)
189{
190 unsigned long long lid_state;
191 acpi_status status;
192
193 status = acpi_evaluate_integer(device->handle, "_LID", NULL, &lid_state);
194 if (ACPI_FAILURE(status))
195 return -ENODEV;
196
197 return lid_state ? 1 : 0;
198}
199
200static int acpi_lid_notify_state(struct acpi_device *device, int state)
201{
202 struct acpi_button *button = acpi_driver_data(device);
Lv Zhengdfa46c52016-08-17 16:22:58 +0800203 ktime_t next_report;
204 bool do_update;
Lv Zhengee7e2262016-06-01 18:10:42 +0800205
Lv Zhengdfa46c52016-08-17 16:22:58 +0800206 /*
207 * In lid_init_state=ignore mode, if user opens/closes lid
208 * frequently with "open" missing, and "last_time" is also updated
209 * frequently, "close" cannot be delivered to the userspace.
210 * So "last_time" is only updated after a timeout or an actual
211 * switch.
212 */
213 if (lid_init_state != ACPI_BUTTON_LID_INIT_IGNORE ||
214 button->last_state != !!state)
215 do_update = true;
216 else
217 do_update = false;
218
219 next_report = ktime_add(button->last_time,
220 ms_to_ktime(lid_report_interval));
221 if (button->last_state == !!state &&
222 ktime_after(ktime_get(), next_report)) {
223 /* Complain the buggy firmware */
224 pr_warn_once("The lid device is not compliant to SW_LID.\n");
225
226 /*
227 * Send the unreliable complement switch event:
228 *
229 * On most platforms, the lid device is reliable. However
230 * there are exceptions:
231 * 1. Platforms returning initial lid state as "close" by
232 * default after booting/resuming:
233 * https://bugzilla.kernel.org/show_bug.cgi?id=89211
234 * https://bugzilla.kernel.org/show_bug.cgi?id=106151
235 * 2. Platforms never reporting "open" events:
236 * https://bugzilla.kernel.org/show_bug.cgi?id=106941
237 * On these buggy platforms, the usage model of the ACPI
238 * lid device actually is:
239 * 1. The initial returning value of _LID may not be
240 * reliable.
241 * 2. The open event may not be reliable.
242 * 3. The close event is reliable.
243 *
244 * But SW_LID is typed as input switch event, the input
245 * layer checks if the event is redundant. Hence if the
246 * state is not switched, the userspace cannot see this
247 * platform triggered reliable event. By inserting a
248 * complement switch event, it then is guaranteed that the
249 * platform triggered reliable one can always be seen by
250 * the userspace.
251 */
252 if (lid_init_state == ACPI_BUTTON_LID_INIT_IGNORE) {
253 do_update = true;
254 /*
255 * Do generate complement switch event for "close"
256 * as "close" is reliable and wrong "open" won't
257 * trigger unexpected behaviors.
258 * Do not generate complement switch event for
259 * "open" as "open" is not reliable and wrong
260 * "close" will trigger unexpected behaviors.
261 */
262 if (!state) {
263 input_report_switch(button->input,
264 SW_LID, state);
265 input_sync(button->input);
266 }
267 }
268 }
269 /* Send the platform triggered reliable event */
270 if (do_update) {
Hans de Goedeae35d652017-11-22 16:06:11 +0100271 acpi_handle_debug(device->handle, "ACPI LID %s\n",
272 state ? "open" : "closed");
Lv Zhengdfa46c52016-08-17 16:22:58 +0800273 input_report_switch(button->input, SW_LID, !state);
274 input_sync(button->input);
275 button->last_state = !!state;
276 button->last_time = ktime_get();
277 }
Lv Zhengee7e2262016-06-01 18:10:42 +0800278
Hans de Goedee346d0c2019-10-26 22:24:36 +0200279 return 0;
Lv Zhengee7e2262016-06-01 18:10:42 +0800280}
281
Randy Dunlap2c4c2a72018-07-07 08:25:01 -0700282static int __maybe_unused acpi_button_state_seq_show(struct seq_file *seq,
283 void *offset)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400284{
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000285 struct acpi_device *device = seq->private;
Lv Zhengee7e2262016-06-01 18:10:42 +0800286 int state;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400287
Lv Zhengee7e2262016-06-01 18:10:42 +0800288 state = acpi_lid_evaluate_state(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500289 seq_printf(seq, "state: %s\n",
Lv Zhengee7e2262016-06-01 18:10:42 +0800290 state < 0 ? "unsupported" : (state ? "open" : "closed"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400291 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400292}
293
Len Brown4be44fc2005-08-05 00:44:28 -0400294static int acpi_button_add_fs(struct acpi_device *device)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400295{
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000296 struct acpi_button *button = acpi_driver_data(device);
Len Brown4be44fc2005-08-05 00:44:28 -0400297 struct proc_dir_entry *entry = NULL;
Zhang Rui912b7422011-03-23 10:21:40 +0800298 int ret = 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400299
Zhang Rui912b7422011-03-23 10:21:40 +0800300 /* procfs I/F for ACPI lid device only */
301 if (button->type != ACPI_BUTTON_TYPE_LID)
302 return 0;
303
304 if (acpi_button_dir || acpi_lid_dir) {
305 printk(KERN_ERR PREFIX "More than one Lid device found!\n");
306 return -EEXIST;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400307 }
308
Zhang Rui912b7422011-03-23 10:21:40 +0800309 /* create /proc/acpi/button */
310 acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
311 if (!acpi_button_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400312 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400313
Zhang Rui912b7422011-03-23 10:21:40 +0800314 /* create /proc/acpi/button/lid */
315 acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
316 if (!acpi_lid_dir) {
317 ret = -ENODEV;
318 goto remove_button_dir;
319 }
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400320
Zhang Rui912b7422011-03-23 10:21:40 +0800321 /* create /proc/acpi/button/lid/LID/ */
322 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), acpi_lid_dir);
323 if (!acpi_device_dir(device)) {
324 ret = -ENODEV;
325 goto remove_lid_dir;
326 }
327
328 /* create /proc/acpi/button/lid/LID/state */
Christoph Hellwig3f3942a2018-05-15 15:57:23 +0200329 entry = proc_create_single_data(ACPI_BUTTON_FILE_STATE, S_IRUGO,
330 acpi_device_dir(device), acpi_button_state_seq_show,
331 device);
Zhang Rui912b7422011-03-23 10:21:40 +0800332 if (!entry) {
333 ret = -ENODEV;
334 goto remove_dev_dir;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400335 }
336
Zhang Rui912b7422011-03-23 10:21:40 +0800337done:
338 return ret;
339
340remove_dev_dir:
341 remove_proc_entry(acpi_device_bid(device),
342 acpi_lid_dir);
343 acpi_device_dir(device) = NULL;
344remove_lid_dir:
345 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
Benjamin Tissoirese370cc82016-07-29 17:08:41 +0200346 acpi_lid_dir = NULL;
Zhang Rui912b7422011-03-23 10:21:40 +0800347remove_button_dir:
348 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
Benjamin Tissoirese370cc82016-07-29 17:08:41 +0200349 acpi_button_dir = NULL;
Zhang Rui912b7422011-03-23 10:21:40 +0800350 goto done;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400351}
352
Len Brown4be44fc2005-08-05 00:44:28 -0400353static int acpi_button_remove_fs(struct acpi_device *device)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400354{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500355 struct acpi_button *button = acpi_driver_data(device);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400356
Zhang Rui912b7422011-03-23 10:21:40 +0800357 if (button->type != ACPI_BUTTON_TYPE_LID)
358 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400359
Zhang Rui912b7422011-03-23 10:21:40 +0800360 remove_proc_entry(ACPI_BUTTON_FILE_STATE,
361 acpi_device_dir(device));
362 remove_proc_entry(acpi_device_bid(device),
363 acpi_lid_dir);
364 acpi_device_dir(device) = NULL;
365 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
Benjamin Tissoirese370cc82016-07-29 17:08:41 +0200366 acpi_lid_dir = NULL;
Zhang Rui912b7422011-03-23 10:21:40 +0800367 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
Benjamin Tissoirese370cc82016-07-29 17:08:41 +0200368 acpi_button_dir = NULL;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400369
Patrick Mocheld550d982006-06-27 00:41:40 -0400370 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400371}
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373/* --------------------------------------------------------------------------
374 Driver Interface
375 -------------------------------------------------------------------------- */
Jesse Barnes7e127152009-09-10 15:28:02 -0700376int acpi_lid_open(void)
377{
Jesse Barnes2c907b72009-10-07 14:39:46 -0700378 if (!lid_device)
379 return -ENODEV;
380
Lv Zhengee7e2262016-06-01 18:10:42 +0800381 return acpi_lid_evaluate_state(lid_device);
Jesse Barnes7e127152009-09-10 15:28:02 -0700382}
383EXPORT_SYMBOL(acpi_lid_open);
384
Ravi Chandra Sadineni7c058c72018-06-27 10:55:02 -0700385static int acpi_lid_update_state(struct acpi_device *device,
386 bool signal_wakeup)
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400387{
Lv Zhengee7e2262016-06-01 18:10:42 +0800388 int state;
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400389
Lv Zhengee7e2262016-06-01 18:10:42 +0800390 state = acpi_lid_evaluate_state(device);
391 if (state < 0)
392 return state;
Bjorn Helgaas50a4da82009-04-08 15:39:38 +0000393
Ravi Chandra Sadineni7c058c72018-06-27 10:55:02 -0700394 if (state && signal_wakeup)
395 acpi_pm_wakeup_event(&device->dev);
396
Lv Zhengee7e2262016-06-01 18:10:42 +0800397 return acpi_lid_notify_state(device, state);
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400398}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Lv Zheng3540c322016-06-01 18:10:48 +0800400static void acpi_lid_initialize_state(struct acpi_device *device)
401{
402 switch (lid_init_state) {
403 case ACPI_BUTTON_LID_INIT_OPEN:
404 (void)acpi_lid_notify_state(device, 1);
405 break;
Lv Zhengf369fdf2017-05-09 15:02:22 +0800406 case ACPI_BUTTON_LID_INIT_METHOD:
Ravi Chandra Sadineni7c058c72018-06-27 10:55:02 -0700407 (void)acpi_lid_update_state(device, false);
Lv Zhengf369fdf2017-05-09 15:02:22 +0800408 break;
Lv Zheng3540c322016-06-01 18:10:48 +0800409 case ACPI_BUTTON_LID_INIT_IGNORE:
410 default:
411 break;
412 }
413}
414
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000415static void acpi_button_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000417 struct acpi_button *button = acpi_driver_data(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500418 struct input_dev *input;
Hans de Goede84d3f6b2017-09-11 16:07:06 +0200419 int users;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 switch (event) {
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000422 case ACPI_FIXED_HARDWARE_EVENT:
423 event = ACPI_BUTTON_NOTIFY_STATUS;
424 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 case ACPI_BUTTON_NOTIFY_STATUS:
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500426 input = button->input;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500427 if (button->type == ACPI_BUTTON_TYPE_LID) {
Hans de Goede84d3f6b2017-09-11 16:07:06 +0200428 mutex_lock(&button->input->mutex);
429 users = button->input->users;
430 mutex_unlock(&button->input->mutex);
431 if (users)
Ravi Chandra Sadineni7c058c72018-06-27 10:55:02 -0700432 acpi_lid_update_state(device, true);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500433 } else {
Rafael J. Wysockie71eeb22014-07-23 00:59:04 +0200434 int keycode;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500435
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200436 acpi_pm_wakeup_event(&device->dev);
Rafael J. Wysockie71eeb22014-07-23 00:59:04 +0200437 if (button->suspended)
438 break;
439
440 keycode = test_bit(KEY_SLEEP, input->keybit) ?
441 KEY_SLEEP : KEY_POWER;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500442 input_report_key(input, keycode, 1);
443 input_sync(input);
444 input_report_key(input, keycode, 0);
Guillem Joverdf316e92008-10-24 00:28:33 +0300445 input_sync(input);
Rafael J. Wysocki1f835112011-01-06 23:36:01 +0100446
Lan Tianyu0bf63682014-03-15 13:37:13 -0400447 acpi_bus_generate_netlink_event(
448 device->pnp.device_class,
449 dev_name(&device->dev),
450 event, ++button->pushed);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 break;
453 default:
454 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400455 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 break;
457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200460#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockie71eeb22014-07-23 00:59:04 +0200461static int acpi_button_suspend(struct device *dev)
462{
463 struct acpi_device *device = to_acpi_device(dev);
464 struct acpi_button *button = acpi_driver_data(device);
465
466 button->suspended = true;
467 return 0;
468}
469
Rafael J. Wysocki1be532d2012-06-27 23:26:51 +0200470static int acpi_button_resume(struct device *dev)
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400471{
Rafael J. Wysocki1be532d2012-06-27 23:26:51 +0200472 struct acpi_device *device = to_acpi_device(dev);
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000473 struct acpi_button *button = acpi_driver_data(device);
Bjorn Helgaas50a4da82009-04-08 15:39:38 +0000474
Rafael J. Wysockie71eeb22014-07-23 00:59:04 +0200475 button->suspended = false;
Zhang Rui13e96212019-04-02 21:38:32 +0800476 if (button->type == ACPI_BUTTON_TYPE_LID && button->input->users) {
477 button->last_state = !!acpi_lid_evaluate_state(device);
478 button->last_time = ktime_get();
Lv Zheng3540c322016-06-01 18:10:48 +0800479 acpi_lid_initialize_state(device);
Zhang Rui13e96212019-04-02 21:38:32 +0800480 }
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400481 return 0;
482}
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200483#endif
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400484
Hans de Goede84d3f6b2017-09-11 16:07:06 +0200485static int acpi_lid_input_open(struct input_dev *input)
486{
487 struct acpi_device *device = input_get_drvdata(input);
488 struct acpi_button *button = acpi_driver_data(device);
489
490 button->last_state = !!acpi_lid_evaluate_state(device);
491 button->last_time = ktime_get();
492 acpi_lid_initialize_state(device);
493
494 return 0;
495}
496
Len Brown4be44fc2005-08-05 00:44:28 -0400497static int acpi_button_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500499 struct acpi_button *button;
500 struct input_dev *input;
Thomas Renninger620e1122010-10-01 10:54:00 +0200501 const char *hid = acpi_device_hid(device);
502 char *name, *class;
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000503 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Hans de Goede593681e2019-10-26 22:24:32 +0200505 if (!strcmp(hid, ACPI_BUTTON_HID_LID) &&
Hans de Goeded7cd0822019-10-26 22:24:33 +0200506 lid_init_state == ACPI_BUTTON_LID_INIT_DISABLED)
Hans de Goede9e811e12017-11-22 16:06:12 +0100507 return -ENODEV;
508
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500509 button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (!button)
Patrick Mocheld550d982006-06-27 00:41:40 -0400511 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700513 device->driver_data = button;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500515 button->input = input = input_allocate_device();
516 if (!input) {
517 error = -ENOMEM;
518 goto err_free_button;
519 }
520
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000521 name = acpi_device_name(device);
522 class = acpi_device_class(device);
523
Bjorn Helgaasd68b5972009-04-08 15:40:04 +0000524 if (!strcmp(hid, ACPI_BUTTON_HID_POWER) ||
525 !strcmp(hid, ACPI_BUTTON_HID_POWERF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 button->type = ACPI_BUTTON_TYPE_POWER;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000527 strcpy(name, ACPI_BUTTON_DEVICE_NAME_POWER);
528 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
Bjorn Helgaasd68b5972009-04-08 15:40:04 +0000530 } else if (!strcmp(hid, ACPI_BUTTON_HID_SLEEP) ||
531 !strcmp(hid, ACPI_BUTTON_HID_SLEEPF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 button->type = ACPI_BUTTON_TYPE_SLEEP;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000533 strcpy(name, ACPI_BUTTON_DEVICE_NAME_SLEEP);
534 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000536 } else if (!strcmp(hid, ACPI_BUTTON_HID_LID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 button->type = ACPI_BUTTON_TYPE_LID;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000538 strcpy(name, ACPI_BUTTON_DEVICE_NAME_LID);
539 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
Hans de Goede84d3f6b2017-09-11 16:07:06 +0200541 input->open = acpi_lid_input_open;
Len Brown4be44fc2005-08-05 00:44:28 -0400542 } else {
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000543 printk(KERN_ERR PREFIX "Unsupported hid [%s]\n", hid);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500544 error = -ENODEV;
545 goto err_free_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
547
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500548 error = acpi_button_add_fs(device);
549 if (error)
550 goto err_free_input;
551
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000552 snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500553
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000554 input->name = name;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500555 input->phys = button->phys;
556 input->id.bustype = BUS_HOST;
557 input->id.product = button->type;
Andrey Borzenkov3b34e522008-03-04 15:06:35 -0800558 input->dev.parent = &device->dev;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 switch (button->type) {
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500561 case ACPI_BUTTON_TYPE_POWER:
Lan Tianyu763f5272013-09-12 03:32:03 -0400562 input_set_capability(input, EV_KEY, KEY_POWER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 break;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500564
565 case ACPI_BUTTON_TYPE_SLEEP:
Lan Tianyu763f5272013-09-12 03:32:03 -0400566 input_set_capability(input, EV_KEY, KEY_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 break;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500568
569 case ACPI_BUTTON_TYPE_LID:
Lan Tianyu763f5272013-09-12 03:32:03 -0400570 input_set_capability(input, EV_SW, SW_LID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 break;
572 }
573
Hans de Goede84d3f6b2017-09-11 16:07:06 +0200574 input_set_drvdata(input, device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500575 error = input_register_device(input);
576 if (error)
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000577 goto err_remove_fs;
Jesse Barnes7e127152009-09-10 15:28:02 -0700578 if (button->type == ACPI_BUTTON_TYPE_LID) {
Jesse Barnes7e127152009-09-10 15:28:02 -0700579 /*
580 * This assumes there's only one lid device, or if there are
581 * more we only care about the last one...
582 */
583 lid_device = device;
584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200586 device_init_wakeup(&device->dev, true);
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000587 printk(KERN_INFO PREFIX "%s [%s]\n", name, acpi_device_bid(device));
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500588 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500590 err_remove_fs:
591 acpi_button_remove_fs(device);
592 err_free_input:
593 input_free_device(input);
594 err_free_button:
595 kfree(button);
596 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100599static int acpi_button_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000601 struct acpi_button *button = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Len Brown4be44fc2005-08-05 00:44:28 -0400603 acpi_button_remove_fs(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500604 input_unregister_device(button->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 kfree(button);
Patrick Mocheld550d982006-06-27 00:41:40 -0400606 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
Kees Cooke4dca7b2017-10-17 19:04:42 -0700609static int param_set_lid_init_state(const char *val,
610 const struct kernel_param *kp)
Lv Zheng3540c322016-06-01 18:10:48 +0800611{
Hans de Goede065bd4d2019-10-26 22:24:31 +0200612 int i;
Lv Zheng3540c322016-06-01 18:10:48 +0800613
Hans de Goede065bd4d2019-10-26 22:24:31 +0200614 i = sysfs_match_string(lid_init_state_str, val);
615 if (i < 0)
616 return i;
617
618 lid_init_state = i;
619 pr_info("Initial lid state set to '%s'\n", lid_init_state_str[i]);
620 return 0;
Lv Zheng3540c322016-06-01 18:10:48 +0800621}
622
Hans de Goede065bd4d2019-10-26 22:24:31 +0200623static int param_get_lid_init_state(char *buf, const struct kernel_param *kp)
Lv Zheng3540c322016-06-01 18:10:48 +0800624{
Hans de Goede065bd4d2019-10-26 22:24:31 +0200625 int i, c = 0;
626
627 for (i = 0; i < ARRAY_SIZE(lid_init_state_str); i++)
628 if (i == lid_init_state)
629 c += sprintf(buf + c, "[%s] ", lid_init_state_str[i]);
630 else
631 c += sprintf(buf + c, "%s ", lid_init_state_str[i]);
632
633 buf[c - 1] = '\n'; /* Replace the final space with a newline */
634
635 return c;
Lv Zheng3540c322016-06-01 18:10:48 +0800636}
637
638module_param_call(lid_init_state,
639 param_set_lid_init_state, param_get_lid_init_state,
640 NULL, 0644);
641MODULE_PARM_DESC(lid_init_state, "Behavior for reporting LID initial state");
642
Ard Biesheuvelac1e55b2018-04-23 11:16:56 +0200643static int acpi_button_register_driver(struct acpi_driver *driver)
644{
Hans de Goeded7cd0822019-10-26 22:24:33 +0200645 const struct dmi_system_id *dmi_id;
646
647 if (lid_init_state == -1) {
648 dmi_id = dmi_first_match(dmi_lid_quirks);
649 if (dmi_id)
650 lid_init_state = (long)dmi_id->driver_data;
651 else
652 lid_init_state = ACPI_BUTTON_LID_INIT_METHOD;
653 }
654
Ard Biesheuvelac1e55b2018-04-23 11:16:56 +0200655 /*
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.
661 */
662 if (acpi_disabled)
663 return 0;
664
665 return acpi_bus_register_driver(driver);
666}
667
668static void acpi_button_unregister_driver(struct acpi_driver *driver)
669{
670 if (!acpi_disabled)
671 acpi_bus_unregister_driver(driver);
672}
673
674module_driver(acpi_button_driver, acpi_button_register_driver,
675 acpi_button_unregister_driver);