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