xref: /linux/drivers/acpi/button.c (revision f16c512a07b954aa3b0b3c580e7abb5dd4f5e703)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  button.c - ACPI Button Driver
4  *
5  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
6  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7  */
8 
9 #define pr_fmt(fmt) "ACPI: button: " fmt
10 
11 #include <linux/compiler.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/types.h>
16 #include <linux/proc_fs.h>
17 #include <linux/seq_file.h>
18 #include <linux/input.h>
19 #include <linux/slab.h>
20 #include <linux/acpi.h>
21 #include <linux/dmi.h>
22 #include <linux/platform_device.h>
23 #include <acpi/button.h>
24 
25 #define ACPI_BUTTON_CLASS		"button"
26 #define ACPI_BUTTON_FILE_STATE		"state"
27 #define ACPI_BUTTON_TYPE_UNKNOWN	0x00
28 #define ACPI_BUTTON_NOTIFY_WAKE		0x02
29 #define ACPI_BUTTON_NOTIFY_STATUS	0x80
30 
31 #define ACPI_BUTTON_CLASS_POWER		"button/power"
32 #define ACPI_BUTTON_DEVICE_NAME_POWER	"Power Button"
33 #define ACPI_BUTTON_TYPE_POWER		0x01
34 
35 #define ACPI_BUTTON_CLASS_SLEEP		"button/sleep"
36 #define ACPI_BUTTON_DEVICE_NAME_SLEEP	"Sleep Button"
37 #define ACPI_BUTTON_TYPE_SLEEP		0x03
38 
39 #define ACPI_BUTTON_CLASS_LID		"button/lid"
40 #define ACPI_BUTTON_SUBCLASS_LID	"lid"
41 #define ACPI_BUTTON_DEVICE_NAME_LID	"Lid Switch"
42 #define ACPI_BUTTON_TYPE_LID		0x05
43 
44 enum {
45 	ACPI_BUTTON_LID_INIT_IGNORE,
46 	ACPI_BUTTON_LID_INIT_OPEN,
47 	ACPI_BUTTON_LID_INIT_METHOD,
48 	ACPI_BUTTON_LID_INIT_DISABLED,
49 };
50 
51 static const char * const lid_init_state_str[] = {
52 	[ACPI_BUTTON_LID_INIT_IGNORE]		= "ignore",
53 	[ACPI_BUTTON_LID_INIT_OPEN]		= "open",
54 	[ACPI_BUTTON_LID_INIT_METHOD]		= "method",
55 	[ACPI_BUTTON_LID_INIT_DISABLED]		= "disabled",
56 };
57 
58 MODULE_AUTHOR("Paul Diefenbaugh");
59 MODULE_DESCRIPTION("ACPI Button Driver");
60 MODULE_LICENSE("GPL");
61 
62 static const struct acpi_device_id button_device_ids[] = {
63 	{ACPI_BUTTON_HID_LID, ACPI_BUTTON_TYPE_LID},
64 	{ACPI_BUTTON_HID_SLEEP, ACPI_BUTTON_TYPE_SLEEP},
65 	{ACPI_BUTTON_HID_SLEEPF, ACPI_BUTTON_TYPE_SLEEP},
66 	{ACPI_BUTTON_HID_POWER, ACPI_BUTTON_TYPE_POWER},
67 	{ACPI_BUTTON_HID_POWERF, ACPI_BUTTON_TYPE_POWER},
68 	{"", 0},
69 };
70 MODULE_DEVICE_TABLE(acpi, button_device_ids);
71 
72 /* Please keep this list sorted alphabetically by vendor and model */
73 static const struct dmi_system_id dmi_lid_quirks[] = {
74 	{
75 		/* GP-electronic T701, _LID method points to a floating GPIO */
76 		.matches = {
77 			DMI_MATCH(DMI_SYS_VENDOR, "Insyde"),
78 			DMI_MATCH(DMI_PRODUCT_NAME, "T701"),
79 			DMI_MATCH(DMI_BIOS_VERSION, "BYT70A.YNCHENG.WIN.007"),
80 		},
81 		.driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_DISABLED,
82 	},
83 	{
84 		/* Nextbook Ares 8A tablet, _LID device always reports lid closed */
85 		.matches = {
86 			DMI_MATCH(DMI_SYS_VENDOR, "Insyde"),
87 			DMI_MATCH(DMI_PRODUCT_NAME, "CherryTrail"),
88 			DMI_MATCH(DMI_BIOS_VERSION, "M882"),
89 		},
90 		.driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_DISABLED,
91 	},
92 	{
93 		/*
94 		 * Lenovo Yoga 9 14ITL5, initial notification of the LID device
95 		 * never happens.
96 		 */
97 		.matches = {
98 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
99 			DMI_MATCH(DMI_PRODUCT_NAME, "82BG"),
100 		},
101 		.driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_OPEN,
102 	},
103 	{
104 		/*
105 		 * Medion Akoya E2215T, notification of the LID device only
106 		 * happens on close, not on open and _LID always returns closed.
107 		 */
108 		.matches = {
109 			DMI_MATCH(DMI_SYS_VENDOR, "MEDION"),
110 			DMI_MATCH(DMI_PRODUCT_NAME, "E2215T"),
111 		},
112 		.driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_OPEN,
113 	},
114 	{
115 		/*
116 		 * Medion Akoya E2228T, 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, "E2228T"),
122 		},
123 		.driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_OPEN,
124 	},
125 	{
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 	},
136 	{
137 		/*
138 		 * Razer Blade Pro 17 early 2020, notification of the LID device
139 		 * only happens on close, not on open and _LID keeps returning closed.
140 		 */
141 		.matches = {
142 			DMI_MATCH(DMI_SYS_VENDOR, "Razer"),
143 			DMI_MATCH(DMI_PRODUCT_NAME, "Blade Pro 17 (Early 2020) - RZ09-0329"),
144 		},
145 		.driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_OPEN,
146 	},
147 	{
148 		/*
149 		 * Razer Blade Stealth 13 early 2020, when the lid is opened
150 		 * while suspended the open notification is lost and _LID keeps
151 		 * returning closed after resume, causing spurious re-suspends.
152 		 */
153 		.matches = {
154 			DMI_MATCH(DMI_SYS_VENDOR, "Razer"),
155 			DMI_MATCH(DMI_PRODUCT_NAME, "Blade Stealth 13 (Early 2020) - RZ09-0310"),
156 		},
157 		.driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_OPEN,
158 	},
159 	{
160 		/*
161 		 * Samsung galaxybook2 ,initial _LID device notification returns
162 		 * lid closed.
163 		 */
164 		.matches = {
165 			DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
166 			DMI_MATCH(DMI_PRODUCT_NAME, "750XED"),
167 		},
168 		.driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_OPEN,
169 	},
170 	{}
171 };
172 
173 static int acpi_button_probe(struct platform_device *pdev);
174 static void acpi_button_remove(struct platform_device *pdev);
175 
176 #ifdef CONFIG_PM_SLEEP
177 static int acpi_button_suspend(struct device *dev);
178 static int acpi_button_resume(struct device *dev);
179 #else
180 #define acpi_button_suspend NULL
181 #define acpi_button_resume NULL
182 #endif
183 static SIMPLE_DEV_PM_OPS(acpi_button_pm, acpi_button_suspend, acpi_button_resume);
184 
185 static struct platform_driver acpi_button_driver = {
186 	.probe = acpi_button_probe,
187 	.remove = acpi_button_remove,
188 	.driver = {
189 		.name = "acpi-button",
190 		.acpi_match_table = button_device_ids,
191 		.pm = &acpi_button_pm,
192 	},
193 };
194 
195 struct acpi_button {
196 	struct acpi_device *adev;
197 	struct device *dev;		/* physical button device */
198 	unsigned int type;
199 	struct input_dev *input;
200 	const char *class;		/* for netlink messages */
201 	char phys[32];			/* for input device */
202 	unsigned long pushed;
203 	bool last_state;
204 	ktime_t last_time;
205 	bool suspended;
206 	bool lid_state_initialized;
207 	bool gpe_enabled;
208 };
209 
210 static long lid_init_state = -1;
211 
212 static unsigned long lid_report_interval __read_mostly = 500;
213 module_param(lid_report_interval, ulong, 0644);
214 MODULE_PARM_DESC(lid_report_interval, "Interval (ms) between lid key events");
215 
216 /* FS Interface (/proc) */
217 static struct proc_dir_entry *acpi_button_dir;
218 static struct proc_dir_entry *acpi_lid_dir;
219 
220 static int acpi_lid_evaluate_state(acpi_handle lid_handle)
221 {
222 	unsigned long long lid_state;
223 	acpi_status status;
224 
225 	status = acpi_evaluate_integer(lid_handle, "_LID", NULL, &lid_state);
226 	if (ACPI_FAILURE(status))
227 		return -ENODEV;
228 
229 	return !!lid_state;
230 }
231 
232 static void acpi_lid_notify_state(struct acpi_button *button, bool state)
233 {
234 	struct acpi_device *device = button->adev;
235 	ktime_t next_report;
236 	bool do_update;
237 
238 	/*
239 	 * In lid_init_state=ignore mode, if user opens/closes lid
240 	 * frequently with "open" missing, and "last_time" is also updated
241 	 * frequently, "close" cannot be delivered to the userspace.
242 	 * So "last_time" is only updated after a timeout or an actual
243 	 * switch.
244 	 */
245 	do_update = lid_init_state != ACPI_BUTTON_LID_INIT_IGNORE ||
246 			button->last_state != state;
247 	next_report = ktime_add(button->last_time,
248 				ms_to_ktime(lid_report_interval));
249 	if (button->last_state == state &&
250 	    ktime_after(ktime_get(), next_report)) {
251 		/* Complain about the buggy firmware. */
252 		pr_warn_once(FW_BUG "Unexpected lid state reported by firmware\n");
253 
254 		/*
255 		 * Send the unreliable complement switch event:
256 		 *
257 		 * On most platforms, the lid device is reliable. However
258 		 * there are exceptions:
259 		 * 1. Platforms returning initial lid state as "close" by
260 		 *    default after booting/resuming:
261 		 *     https://bugzilla.kernel.org/show_bug.cgi?id=89211
262 		 *     https://bugzilla.kernel.org/show_bug.cgi?id=106151
263 		 * 2. Platforms never reporting "open" events:
264 		 *     https://bugzilla.kernel.org/show_bug.cgi?id=106941
265 		 * On these buggy platforms, the usage model of the ACPI
266 		 * lid device actually is:
267 		 * 1. The initial returning value of _LID may not be
268 		 *    reliable.
269 		 * 2. The open event may not be reliable.
270 		 * 3. The close event is reliable.
271 		 *
272 		 * But SW_LID is typed as input switch event, the input
273 		 * layer checks if the event is redundant. Hence if the
274 		 * state is not switched, the userspace cannot see this
275 		 * platform triggered reliable event. By inserting a
276 		 * complement switch event, it then is guaranteed that the
277 		 * platform triggered reliable one can always be seen by
278 		 * the userspace.
279 		 */
280 		if (lid_init_state == ACPI_BUTTON_LID_INIT_IGNORE) {
281 			do_update = true;
282 			/*
283 			 * Do generate complement switch event for "close"
284 			 * as "close" is reliable and wrong "open" won't
285 			 * trigger unexpected behaviors.
286 			 * Do not generate complement switch event for
287 			 * "open" as "open" is not reliable and wrong
288 			 * "close" will trigger unexpected behaviors.
289 			 */
290 			if (!state) {
291 				input_report_switch(button->input,
292 						    SW_LID, state);
293 				input_sync(button->input);
294 			}
295 		}
296 	}
297 	/* Send the platform triggered reliable event */
298 	if (do_update) {
299 		acpi_handle_debug(device->handle, "ACPI LID %s\n",
300 				  state ? "open" : "closed");
301 		input_report_switch(button->input, SW_LID, !state);
302 		input_sync(button->input);
303 		button->last_state = state;
304 		button->last_time = ktime_get();
305 	}
306 }
307 
308 static int __maybe_unused acpi_button_state_seq_show(struct seq_file *seq,
309 						     void *offset)
310 {
311 	struct acpi_button *button = seq->private;
312 	int state;
313 
314 	state = acpi_lid_evaluate_state(button->adev->handle);
315 	seq_printf(seq, "state:      %s\n",
316 		   state < 0 ? "unsupported" : (state ? "open" : "closed"));
317 	return 0;
318 }
319 
320 static int acpi_lid_add_fs(struct acpi_button *button)
321 {
322 	struct acpi_device *device = button->adev;
323 	struct proc_dir_entry *entry = NULL;
324 
325 	if (acpi_button_dir || acpi_lid_dir) {
326 		pr_info("More than one Lid device found!\n");
327 		return -EEXIST;
328 	}
329 
330 	/* create /proc/acpi/button */
331 	acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
332 	if (!acpi_button_dir)
333 		return -ENODEV;
334 
335 	/* create /proc/acpi/button/lid */
336 	acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
337 	if (!acpi_lid_dir)
338 		goto remove_button_dir;
339 
340 	/* create /proc/acpi/button/lid/LID/ */
341 	acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), acpi_lid_dir);
342 	if (!acpi_device_dir(device))
343 		goto remove_lid_dir;
344 
345 	/* create /proc/acpi/button/lid/LID/state */
346 	entry = proc_create_single_data(ACPI_BUTTON_FILE_STATE, S_IRUGO,
347 			acpi_device_dir(device), acpi_button_state_seq_show,
348 			button);
349 	if (!entry)
350 		goto remove_dev_dir;
351 
352 	return 0;
353 
354 remove_dev_dir:
355 	remove_proc_entry(acpi_device_bid(device), acpi_lid_dir);
356 	acpi_device_dir(device) = NULL;
357 remove_lid_dir:
358 	remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
359 	acpi_lid_dir = NULL;
360 remove_button_dir:
361 	remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
362 	acpi_button_dir = NULL;
363 	return -ENODEV;
364 }
365 
366 static void acpi_lid_remove_fs(void *data)
367 {
368 	struct acpi_button *button = data;
369 	struct acpi_device *device = button->adev;
370 
371 	remove_proc_entry(ACPI_BUTTON_FILE_STATE,
372 			  acpi_device_dir(device));
373 	remove_proc_entry(acpi_device_bid(device),
374 			  acpi_lid_dir);
375 	acpi_device_dir(device) = NULL;
376 	remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
377 	acpi_lid_dir = NULL;
378 	remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
379 	acpi_button_dir = NULL;
380 }
381 
382 static int devm_acpi_lid_add_fs(struct device *dev, struct acpi_button *button)
383 {
384 	int ret;
385 
386 	ret = acpi_lid_add_fs(button);
387 	if (ret)
388 		return ret;
389 
390 	return devm_add_action_or_reset(dev, acpi_lid_remove_fs, button);
391 }
392 
393 static acpi_handle saved_lid_handle;
394 static DEFINE_MUTEX(acpi_lid_lock);
395 
396 static void acpi_lid_save(struct acpi_device *adev)
397 {
398 	guard(mutex)(&acpi_lid_lock);
399 
400 	saved_lid_handle = adev->handle;
401 }
402 
403 static void acpi_lid_forget(struct acpi_device *adev)
404 {
405 	guard(mutex)(&acpi_lid_lock);
406 
407 	if (saved_lid_handle == adev->handle)
408 		saved_lid_handle = NULL;
409 }
410 
411 /* Driver Interface */
412 int acpi_lid_open(void)
413 {
414 	guard(mutex)(&acpi_lid_lock);
415 
416 	if (!saved_lid_handle)
417 		return -ENODEV;
418 
419 	return acpi_lid_evaluate_state(saved_lid_handle);
420 }
421 EXPORT_SYMBOL(acpi_lid_open);
422 
423 static void acpi_lid_update_state(struct acpi_button *button, bool signal_wakeup)
424 {
425 	int state;
426 
427 	state = acpi_lid_evaluate_state(button->adev->handle);
428 	if (state < 0)
429 		return;
430 
431 	if (state && signal_wakeup)
432 		acpi_pm_wakeup_event(button->dev);
433 
434 	acpi_lid_notify_state(button, state);
435 }
436 
437 static void acpi_lid_initialize_state(struct acpi_button *button)
438 {
439 	switch (lid_init_state) {
440 	case ACPI_BUTTON_LID_INIT_OPEN:
441 		acpi_lid_notify_state(button, true);
442 		break;
443 	case ACPI_BUTTON_LID_INIT_METHOD:
444 		acpi_lid_update_state(button, false);
445 		break;
446 	case ACPI_BUTTON_LID_INIT_IGNORE:
447 	default:
448 		break;
449 	}
450 
451 	button->lid_state_initialized = true;
452 }
453 
454 static void acpi_lid_notify(acpi_handle handle, u32 event, void *data)
455 {
456 	struct acpi_button *button = data;
457 	struct acpi_device *device = button->adev;
458 
459 	if (event != ACPI_BUTTON_NOTIFY_STATUS) {
460 		acpi_handle_debug(device->handle, "Unsupported event [0x%x]\n",
461 				  event);
462 		return;
463 	}
464 
465 	if (!button->lid_state_initialized)
466 		return;
467 
468 	acpi_lid_update_state(button, true);
469 }
470 
471 static void acpi_button_notify(acpi_handle handle, u32 event, void *data)
472 {
473 	struct acpi_button *button = data;
474 	struct acpi_device *device = button->adev;
475 	struct input_dev *input;
476 	int keycode;
477 
478 	switch (event) {
479 	case ACPI_BUTTON_NOTIFY_STATUS:
480 		break;
481 	case ACPI_BUTTON_NOTIFY_WAKE:
482 		break;
483 	default:
484 		acpi_handle_debug(device->handle, "Unsupported event [0x%x]\n",
485 				  event);
486 		return;
487 	}
488 
489 	acpi_pm_wakeup_event(button->dev);
490 
491 	if (button->suspended || event == ACPI_BUTTON_NOTIFY_WAKE)
492 		return;
493 
494 	input = button->input;
495 	keycode = test_bit(KEY_SLEEP, input->keybit) ? KEY_SLEEP : KEY_POWER;
496 
497 	input_report_key(input, keycode, 1);
498 	input_sync(input);
499 	input_report_key(input, keycode, 0);
500 	input_sync(input);
501 
502 	acpi_bus_generate_netlink_event(button->class, dev_name(&device->dev),
503 					event, ++button->pushed);
504 }
505 
506 static void acpi_button_notify_run(void *data)
507 {
508 	acpi_button_notify(NULL, ACPI_BUTTON_NOTIFY_STATUS, data);
509 }
510 
511 static u32 acpi_button_event(void *data)
512 {
513 	acpi_os_execute(OSL_NOTIFY_HANDLER, acpi_button_notify_run, data);
514 	return ACPI_INTERRUPT_HANDLED;
515 }
516 
517 #ifdef CONFIG_PM_SLEEP
518 static int acpi_button_suspend(struct device *dev)
519 {
520 	struct acpi_button *button = dev_get_drvdata(dev);
521 
522 	button->suspended = true;
523 	return 0;
524 }
525 
526 static int acpi_button_resume(struct device *dev)
527 {
528 	struct acpi_button *button = dev_get_drvdata(dev);
529 	struct input_dev *input;
530 
531 	button->suspended = false;
532 	if (button->type == ACPI_BUTTON_TYPE_LID) {
533 		button->last_state = !!acpi_lid_evaluate_state(ACPI_HANDLE(dev));
534 		button->last_time = ktime_get();
535 		acpi_lid_initialize_state(button);
536 	}
537 
538 	if (button->type == ACPI_BUTTON_TYPE_POWER) {
539 		input = button->input;
540 		input_report_key(input, KEY_WAKEUP, 1);
541 		input_sync(input);
542 		input_report_key(input, KEY_WAKEUP, 0);
543 		input_sync(input);
544 	}
545 	return 0;
546 }
547 #endif
548 
549 static int acpi_lid_input_open(struct input_dev *input)
550 {
551 	struct acpi_button *button = input_get_drvdata(input);
552 
553 	button->last_state = !!acpi_lid_evaluate_state(button->adev->handle);
554 	button->last_time = ktime_get();
555 	acpi_lid_initialize_state(button);
556 
557 	return 0;
558 }
559 
560 static acpi_notify_handler acpi_button_notify_handler(struct acpi_button *button)
561 {
562 	if (button->type == ACPI_BUTTON_TYPE_LID)
563 		return acpi_lid_notify;
564 
565 	return acpi_button_notify;
566 }
567 
568 static void acpi_button_wakeup_cleanup(void *data)
569 {
570 	device_init_wakeup(data, false);
571 }
572 
573 static int devm_acpi_button_init_wakeup(struct device *dev)
574 {
575 	device_init_wakeup(dev, true);
576 	return devm_add_action_or_reset(dev, acpi_button_wakeup_cleanup, dev);
577 }
578 
579 static void acpi_button_remove_event_handler(void *data)
580 {
581 	struct acpi_button *button = data;
582 	struct acpi_device *adev = button->adev;
583 
584 	switch (adev->device_type) {
585 	case ACPI_BUS_TYPE_POWER_BUTTON:
586 		acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
587 						acpi_button_event);
588 		break;
589 
590 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
591 		acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
592 						acpi_button_event);
593 		break;
594 
595 	default:
596 		if (button->gpe_enabled) {
597 			dev_dbg(button->dev, "Disabling ACPI GPE%02llx\n",
598 				adev->wakeup.gpe_number);
599 			acpi_disable_gpe(adev->wakeup.gpe_device,
600 					 adev->wakeup.gpe_number);
601 		}
602 		acpi_remove_notify_handler(adev->handle, ACPI_ALL_NOTIFY,
603 					   acpi_button_notify_handler(button));
604 		break;
605 	}
606 	acpi_os_wait_events_complete();
607 }
608 
609 static int acpi_button_add_fixed_event_handler(u32 event,
610 					       struct acpi_button *button)
611 {
612 	acpi_status status;
613 
614 	status = acpi_install_fixed_event_handler(event, acpi_button_event, button);
615 	if (ACPI_FAILURE(status))
616 		return -ENODEV;
617 
618 	return 0;
619 }
620 
621 static int acpi_button_add_event_handler(struct acpi_button *button)
622 {
623 	struct acpi_device *adev = button->adev;
624 	acpi_status status;
625 
626 	if (adev->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
627 		return acpi_button_add_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
628 							   button);
629 
630 	if (adev->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
631 		return acpi_button_add_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
632 							   button);
633 
634 	status = acpi_install_notify_handler(adev->handle, ACPI_ALL_NOTIFY,
635 					     acpi_button_notify_handler(button),
636 					     button);
637 	if (ACPI_FAILURE(status))
638 		return -ENODEV;
639 
640 	if (!adev->wakeup.flags.valid)
641 		return 0;
642 
643 	/*
644 	 * If the wakeup GPE has a handler method, enable it in case it is also
645 	 * used for signaling runtime events.
646 	 */
647 	status = acpi_enable_gpe_cond(adev->wakeup.gpe_device,
648 				      adev->wakeup.gpe_number,
649 				      ACPI_GPE_DISPATCH_METHOD);
650 	button->gpe_enabled = ACPI_SUCCESS(status);
651 	if (button->gpe_enabled)
652 		dev_dbg(button->dev, "Enabled ACPI GPE%02llx\n",
653 			adev->wakeup.gpe_number);
654 
655 	return 0;
656 }
657 
658 static int devm_acpi_button_add_event_handler(struct device *dev,
659 					      struct acpi_button *button)
660 {
661 	int ret;
662 
663 	ret = acpi_button_add_event_handler(button);
664 	if (ret)
665 		return ret;
666 
667 	return devm_add_action_or_reset(dev, acpi_button_remove_event_handler,
668 					button);
669 }
670 
671 static int acpi_button_probe(struct platform_device *pdev)
672 {
673 	struct device *dev = &pdev->dev;
674 	struct acpi_device *device = ACPI_COMPANION(dev);
675 	const struct acpi_device_id *id;
676 	struct acpi_button *button;
677 	struct input_dev *input;
678 	u8 button_type;
679 	int error = 0;
680 
681 	id = acpi_match_acpi_device(button_device_ids, device);
682 	if (!id || strcmp(acpi_device_hid(device), id->id))
683 		return dev_err_probe(dev, -ENODEV, "Unsupported device\n");
684 
685 	button_type = id->driver_data;
686 	if (button_type == ACPI_BUTTON_TYPE_LID &&
687 	    lid_init_state == ACPI_BUTTON_LID_INIT_DISABLED)
688 		return -ENODEV;
689 
690 	button = devm_kzalloc(dev, sizeof(*button), GFP_KERNEL);
691 	if (!button)
692 		return -ENOMEM;
693 
694 	platform_set_drvdata(pdev, button);
695 
696 	button->dev = dev;
697 	button->adev = device;
698 	input = devm_input_allocate_device(dev);
699 	if (!input)
700 		return -ENOMEM;
701 
702 	button->input = input;
703 	button->type = button_type;
704 
705 	switch (button_type) {
706 	case ACPI_BUTTON_TYPE_LID:
707 		button->class = ACPI_BUTTON_CLASS_LID;
708 
709 		input->name = ACPI_BUTTON_DEVICE_NAME_LID;
710 		input_set_capability(input, EV_SW, SW_LID);
711 		input->open = acpi_lid_input_open;
712 
713 		error = devm_acpi_lid_add_fs(dev, button);
714 		if (error)
715 			return error;
716 
717 		break;
718 
719 	case ACPI_BUTTON_TYPE_POWER:
720 		button->class = ACPI_BUTTON_CLASS_POWER;
721 
722 		input->name = ACPI_BUTTON_DEVICE_NAME_POWER;
723 		input_set_capability(input, EV_KEY, KEY_POWER);
724 		input_set_capability(input, EV_KEY, KEY_WAKEUP);
725 		break;
726 
727 	case ACPI_BUTTON_TYPE_SLEEP:
728 		button->class = ACPI_BUTTON_CLASS_SLEEP;
729 
730 		input->name = ACPI_BUTTON_DEVICE_NAME_SLEEP;
731 		input_set_capability(input, EV_KEY, KEY_SLEEP);
732 		break;
733 
734 	default:
735 		return dev_err_probe(dev, -ENODEV, "Unrecognized button type\n");
736 	}
737 
738 	snprintf(button->phys, sizeof(button->phys), "%s/button/input0",
739 		 acpi_device_hid(device));
740 
741 	input->phys = button->phys;
742 	input->id.bustype = BUS_HOST;
743 	input->id.product = button_type;
744 
745 	input_set_drvdata(input, button);
746 	error = input_register_device(input);
747 	if (error)
748 		return error;
749 
750 	error = devm_acpi_button_init_wakeup(dev);
751 	if (error)
752 		return error;
753 
754 	error = devm_acpi_button_add_event_handler(dev, button);
755 	if (error)
756 		return error;
757 
758 	if (button_type == ACPI_BUTTON_TYPE_LID) {
759 		/*
760 		 * This assumes there's only one lid device, or if there are
761 		 * more we only care about the last one...
762 		 */
763 		acpi_lid_save(device);
764 	}
765 
766 	pr_info("%s [%s]\n", input->name, acpi_device_bid(device));
767 
768 	return 0;
769 }
770 
771 static void acpi_button_remove(struct platform_device *pdev)
772 {
773 	struct acpi_button *button = platform_get_drvdata(pdev);
774 
775 	if (button->type == ACPI_BUTTON_TYPE_LID)
776 		acpi_lid_forget(button->adev);
777 }
778 
779 static int param_set_lid_init_state(const char *val,
780 				    const struct kernel_param *kp)
781 {
782 	int i;
783 
784 	i = sysfs_match_string(lid_init_state_str, val);
785 	if (i < 0)
786 		return i;
787 
788 	lid_init_state = i;
789 	pr_info("Initial lid state set to '%s'\n", lid_init_state_str[i]);
790 	return 0;
791 }
792 
793 static int param_get_lid_init_state(char *buf, const struct kernel_param *kp)
794 {
795 	int i, c = 0;
796 
797 	for (i = 0; i < ARRAY_SIZE(lid_init_state_str); i++)
798 		if (i == lid_init_state)
799 			c += sprintf(buf + c, "[%s] ", lid_init_state_str[i]);
800 		else
801 			c += sprintf(buf + c, "%s ", lid_init_state_str[i]);
802 
803 	buf[c - 1] = '\n'; /* Replace the final space with a newline */
804 
805 	return c;
806 }
807 
808 module_param_call(lid_init_state,
809 		  param_set_lid_init_state, param_get_lid_init_state,
810 		  NULL, 0644);
811 MODULE_PARM_DESC(lid_init_state, "Behavior for reporting LID initial state");
812 
813 static int __init acpi_button_init(void)
814 {
815 	const struct dmi_system_id *dmi_id;
816 
817 	if (lid_init_state == -1) {
818 		dmi_id = dmi_first_match(dmi_lid_quirks);
819 		if (dmi_id)
820 			lid_init_state = (long)dmi_id->driver_data;
821 		else
822 			lid_init_state = ACPI_BUTTON_LID_INIT_METHOD;
823 	}
824 
825 	/*
826 	 * Modules such as nouveau.ko and i915.ko have a link time dependency
827 	 * on acpi_lid_open(), and would therefore not be loadable on ACPI
828 	 * capable kernels booted in non-ACPI mode if the return value of
829 	 * platform_driver_register() is returned from here with ACPI disabled
830 	 * when this driver is built as a module.
831 	 */
832 	if (acpi_disabled)
833 		return 0;
834 
835 	return platform_driver_register(&acpi_button_driver);
836 }
837 
838 static void __exit acpi_button_exit(void)
839 {
840 	if (!acpi_disabled)
841 		platform_driver_unregister(&acpi_button_driver);
842 }
843 
844 module_init(acpi_button_init);
845 module_exit(acpi_button_exit);
846