xref: /linux/drivers/acpi/button.c (revision 8fde5d1d47f69db6082dfa34500c27f8485389a5)
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 device *dev;		/* physical button device */
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 	bool gpe_enabled;
183 };
184 
185 static struct acpi_device *lid_device;
186 static long lid_init_state = -1;
187 
188 static unsigned long lid_report_interval __read_mostly = 500;
189 module_param(lid_report_interval, ulong, 0644);
190 MODULE_PARM_DESC(lid_report_interval, "Interval (ms) between lid key events");
191 
192 /* FS Interface (/proc) */
193 static struct proc_dir_entry *acpi_button_dir;
194 static struct proc_dir_entry *acpi_lid_dir;
195 
acpi_lid_evaluate_state(struct acpi_device * device)196 static int acpi_lid_evaluate_state(struct acpi_device *device)
197 {
198 	unsigned long long lid_state;
199 	acpi_status status;
200 
201 	status = acpi_evaluate_integer(device->handle, "_LID", NULL, &lid_state);
202 	if (ACPI_FAILURE(status))
203 		return -ENODEV;
204 
205 	return lid_state ? 1 : 0;
206 }
207 
acpi_lid_notify_state(struct acpi_button * button,int state)208 static int acpi_lid_notify_state(struct acpi_button *button, int state)
209 {
210 	struct acpi_device *device = button->adev;
211 	ktime_t next_report;
212 	bool do_update;
213 
214 	/*
215 	 * In lid_init_state=ignore mode, if user opens/closes lid
216 	 * frequently with "open" missing, and "last_time" is also updated
217 	 * frequently, "close" cannot be delivered to the userspace.
218 	 * So "last_time" is only updated after a timeout or an actual
219 	 * switch.
220 	 */
221 	if (lid_init_state != ACPI_BUTTON_LID_INIT_IGNORE ||
222 	    button->last_state != !!state)
223 		do_update = true;
224 	else
225 		do_update = false;
226 
227 	next_report = ktime_add(button->last_time,
228 				ms_to_ktime(lid_report_interval));
229 	if (button->last_state == !!state &&
230 	    ktime_after(ktime_get(), next_report)) {
231 		/* Complain the buggy firmware */
232 		pr_warn_once("The lid device is not compliant to SW_LID.\n");
233 
234 		/*
235 		 * Send the unreliable complement switch event:
236 		 *
237 		 * On most platforms, the lid device is reliable. However
238 		 * there are exceptions:
239 		 * 1. Platforms returning initial lid state as "close" by
240 		 *    default after booting/resuming:
241 		 *     https://bugzilla.kernel.org/show_bug.cgi?id=89211
242 		 *     https://bugzilla.kernel.org/show_bug.cgi?id=106151
243 		 * 2. Platforms never reporting "open" events:
244 		 *     https://bugzilla.kernel.org/show_bug.cgi?id=106941
245 		 * On these buggy platforms, the usage model of the ACPI
246 		 * lid device actually is:
247 		 * 1. The initial returning value of _LID may not be
248 		 *    reliable.
249 		 * 2. The open event may not be reliable.
250 		 * 3. The close event is reliable.
251 		 *
252 		 * But SW_LID is typed as input switch event, the input
253 		 * layer checks if the event is redundant. Hence if the
254 		 * state is not switched, the userspace cannot see this
255 		 * platform triggered reliable event. By inserting a
256 		 * complement switch event, it then is guaranteed that the
257 		 * platform triggered reliable one can always be seen by
258 		 * the userspace.
259 		 */
260 		if (lid_init_state == ACPI_BUTTON_LID_INIT_IGNORE) {
261 			do_update = true;
262 			/*
263 			 * Do generate complement switch event for "close"
264 			 * as "close" is reliable and wrong "open" won't
265 			 * trigger unexpected behaviors.
266 			 * Do not generate complement switch event for
267 			 * "open" as "open" is not reliable and wrong
268 			 * "close" will trigger unexpected behaviors.
269 			 */
270 			if (!state) {
271 				input_report_switch(button->input,
272 						    SW_LID, state);
273 				input_sync(button->input);
274 			}
275 		}
276 	}
277 	/* Send the platform triggered reliable event */
278 	if (do_update) {
279 		acpi_handle_debug(device->handle, "ACPI LID %s\n",
280 				  state ? "open" : "closed");
281 		input_report_switch(button->input, SW_LID, !state);
282 		input_sync(button->input);
283 		button->last_state = !!state;
284 		button->last_time = ktime_get();
285 	}
286 
287 	return 0;
288 }
289 
acpi_button_state_seq_show(struct seq_file * seq,void * offset)290 static int __maybe_unused acpi_button_state_seq_show(struct seq_file *seq,
291 						     void *offset)
292 {
293 	struct acpi_button *button = seq->private;
294 	int state;
295 
296 	state = acpi_lid_evaluate_state(button->adev);
297 	seq_printf(seq, "state:      %s\n",
298 		   state < 0 ? "unsupported" : (state ? "open" : "closed"));
299 	return 0;
300 }
301 
acpi_button_add_fs(struct acpi_button * button)302 static int acpi_button_add_fs(struct acpi_button *button)
303 {
304 	struct acpi_device *device = button->adev;
305 	struct proc_dir_entry *entry = NULL;
306 	int ret = 0;
307 
308 	/* procfs I/F for ACPI lid device only */
309 	if (button->type != ACPI_BUTTON_TYPE_LID)
310 		return 0;
311 
312 	if (acpi_button_dir || acpi_lid_dir) {
313 		pr_info("More than one Lid device found!\n");
314 		return -EEXIST;
315 	}
316 
317 	/* create /proc/acpi/button */
318 	acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
319 	if (!acpi_button_dir)
320 		return -ENODEV;
321 
322 	/* create /proc/acpi/button/lid */
323 	acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
324 	if (!acpi_lid_dir) {
325 		ret = -ENODEV;
326 		goto remove_button_dir;
327 	}
328 
329 	/* create /proc/acpi/button/lid/LID/ */
330 	acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), acpi_lid_dir);
331 	if (!acpi_device_dir(device)) {
332 		ret = -ENODEV;
333 		goto remove_lid_dir;
334 	}
335 
336 	/* create /proc/acpi/button/lid/LID/state */
337 	entry = proc_create_single_data(ACPI_BUTTON_FILE_STATE, S_IRUGO,
338 			acpi_device_dir(device), acpi_button_state_seq_show,
339 			button);
340 	if (!entry) {
341 		ret = -ENODEV;
342 		goto remove_dev_dir;
343 	}
344 
345 done:
346 	return ret;
347 
348 remove_dev_dir:
349 	remove_proc_entry(acpi_device_bid(device),
350 			  acpi_lid_dir);
351 	acpi_device_dir(device) = NULL;
352 remove_lid_dir:
353 	remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
354 	acpi_lid_dir = NULL;
355 remove_button_dir:
356 	remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
357 	acpi_button_dir = NULL;
358 	goto done;
359 }
360 
acpi_button_remove_fs(struct acpi_button * button)361 static int acpi_button_remove_fs(struct acpi_button *button)
362 {
363 	struct acpi_device *device = button->adev;
364 
365 	if (button->type != ACPI_BUTTON_TYPE_LID)
366 		return 0;
367 
368 	remove_proc_entry(ACPI_BUTTON_FILE_STATE,
369 			  acpi_device_dir(device));
370 	remove_proc_entry(acpi_device_bid(device),
371 			  acpi_lid_dir);
372 	acpi_device_dir(device) = NULL;
373 	remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
374 	acpi_lid_dir = NULL;
375 	remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
376 	acpi_button_dir = NULL;
377 
378 	return 0;
379 }
380 
381 /* Driver Interface */
acpi_lid_open(void)382 int acpi_lid_open(void)
383 {
384 	if (!lid_device)
385 		return -ENODEV;
386 
387 	return acpi_lid_evaluate_state(lid_device);
388 }
389 EXPORT_SYMBOL(acpi_lid_open);
390 
acpi_lid_update_state(struct acpi_button * button,bool signal_wakeup)391 static int acpi_lid_update_state(struct acpi_button *button,
392 				 bool signal_wakeup)
393 {
394 	struct acpi_device *device = button->adev;
395 	int state;
396 
397 	state = acpi_lid_evaluate_state(device);
398 	if (state < 0)
399 		return state;
400 
401 	if (state && signal_wakeup)
402 		acpi_pm_wakeup_event(button->dev);
403 
404 	return acpi_lid_notify_state(button, state);
405 }
406 
acpi_lid_initialize_state(struct acpi_button * button)407 static void acpi_lid_initialize_state(struct acpi_button *button)
408 {
409 	switch (lid_init_state) {
410 	case ACPI_BUTTON_LID_INIT_OPEN:
411 		(void)acpi_lid_notify_state(button, 1);
412 		break;
413 	case ACPI_BUTTON_LID_INIT_METHOD:
414 		(void)acpi_lid_update_state(button, false);
415 		break;
416 	case ACPI_BUTTON_LID_INIT_IGNORE:
417 	default:
418 		break;
419 	}
420 
421 	button->lid_state_initialized = true;
422 }
423 
acpi_lid_notify(acpi_handle handle,u32 event,void * data)424 static void acpi_lid_notify(acpi_handle handle, u32 event, void *data)
425 {
426 	struct acpi_button *button = data;
427 	struct acpi_device *device = button->adev;
428 
429 	if (event != ACPI_BUTTON_NOTIFY_STATUS) {
430 		acpi_handle_debug(device->handle, "Unsupported event [0x%x]\n",
431 				  event);
432 		return;
433 	}
434 
435 	if (!button->lid_state_initialized)
436 		return;
437 
438 	acpi_lid_update_state(button, true);
439 }
440 
acpi_button_notify(acpi_handle handle,u32 event,void * data)441 static void acpi_button_notify(acpi_handle handle, u32 event, void *data)
442 {
443 	struct acpi_button *button = data;
444 	struct acpi_device *device = button->adev;
445 	struct input_dev *input;
446 	int keycode;
447 
448 	switch (event) {
449 	case ACPI_BUTTON_NOTIFY_STATUS:
450 		break;
451 	case ACPI_BUTTON_NOTIFY_WAKE:
452 		break;
453 	default:
454 		acpi_handle_debug(device->handle, "Unsupported event [0x%x]\n",
455 				  event);
456 		return;
457 	}
458 
459 	acpi_pm_wakeup_event(button->dev);
460 
461 	if (button->suspended || event == ACPI_BUTTON_NOTIFY_WAKE)
462 		return;
463 
464 	input = button->input;
465 	keycode = test_bit(KEY_SLEEP, input->keybit) ? KEY_SLEEP : KEY_POWER;
466 
467 	input_report_key(input, keycode, 1);
468 	input_sync(input);
469 	input_report_key(input, keycode, 0);
470 	input_sync(input);
471 
472 	acpi_bus_generate_netlink_event(acpi_device_class(device),
473 					dev_name(&device->dev),
474 					event, ++button->pushed);
475 }
476 
acpi_button_notify_run(void * data)477 static void acpi_button_notify_run(void *data)
478 {
479 	acpi_button_notify(NULL, ACPI_BUTTON_NOTIFY_STATUS, data);
480 }
481 
acpi_button_event(void * data)482 static u32 acpi_button_event(void *data)
483 {
484 	acpi_os_execute(OSL_NOTIFY_HANDLER, acpi_button_notify_run, data);
485 	return ACPI_INTERRUPT_HANDLED;
486 }
487 
488 #ifdef CONFIG_PM_SLEEP
acpi_button_suspend(struct device * dev)489 static int acpi_button_suspend(struct device *dev)
490 {
491 	struct acpi_button *button = dev_get_drvdata(dev);
492 
493 	button->suspended = true;
494 	return 0;
495 }
496 
acpi_button_resume(struct device * dev)497 static int acpi_button_resume(struct device *dev)
498 {
499 	struct acpi_button *button = dev_get_drvdata(dev);
500 	struct acpi_device *device = ACPI_COMPANION(dev);
501 	struct input_dev *input;
502 
503 	button->suspended = false;
504 	if (button->type == ACPI_BUTTON_TYPE_LID) {
505 		button->last_state = !!acpi_lid_evaluate_state(device);
506 		button->last_time = ktime_get();
507 		acpi_lid_initialize_state(button);
508 	}
509 
510 	if (button->type == ACPI_BUTTON_TYPE_POWER) {
511 		input = button->input;
512 		input_report_key(input, KEY_WAKEUP, 1);
513 		input_sync(input);
514 		input_report_key(input, KEY_WAKEUP, 0);
515 		input_sync(input);
516 	}
517 	return 0;
518 }
519 #endif
520 
acpi_lid_input_open(struct input_dev * input)521 static int acpi_lid_input_open(struct input_dev *input)
522 {
523 	struct acpi_button *button = input_get_drvdata(input);
524 	struct acpi_device *device = button->adev;
525 
526 	button->last_state = !!acpi_lid_evaluate_state(device);
527 	button->last_time = ktime_get();
528 	acpi_lid_initialize_state(button);
529 
530 	return 0;
531 }
532 
acpi_button_probe(struct platform_device * pdev)533 static int acpi_button_probe(struct platform_device *pdev)
534 {
535 	acpi_notify_handler handler;
536 	struct acpi_device *device;
537 	struct acpi_button *button;
538 	struct input_dev *input;
539 	acpi_status status;
540 	char *name, *class;
541 	const char *hid;
542 	int error = 0;
543 
544 	device = ACPI_COMPANION(&pdev->dev);
545 	if (!device)
546 		return -ENODEV;
547 
548 	hid = acpi_device_hid(device);
549 	if (!strcmp(hid, ACPI_BUTTON_HID_LID) &&
550 	     lid_init_state == ACPI_BUTTON_LID_INIT_DISABLED)
551 		return -ENODEV;
552 
553 	button = kzalloc_obj(struct acpi_button);
554 	if (!button)
555 		return -ENOMEM;
556 
557 	platform_set_drvdata(pdev, button);
558 
559 	button->dev = &pdev->dev;
560 	button->adev = device;
561 	button->input = input = input_allocate_device();
562 	if (!input) {
563 		error = -ENOMEM;
564 		goto err_free_button;
565 	}
566 
567 	class = acpi_device_class(device);
568 
569 	if (!strcmp(hid, ACPI_BUTTON_HID_POWER) ||
570 	    !strcmp(hid, ACPI_BUTTON_HID_POWERF)) {
571 		button->type = ACPI_BUTTON_TYPE_POWER;
572 		handler = acpi_button_notify;
573 		name = ACPI_BUTTON_DEVICE_NAME_POWER;
574 		sprintf(class, "%s/%s",
575 			ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
576 	} else if (!strcmp(hid, ACPI_BUTTON_HID_SLEEP) ||
577 		   !strcmp(hid, ACPI_BUTTON_HID_SLEEPF)) {
578 		button->type = ACPI_BUTTON_TYPE_SLEEP;
579 		handler = acpi_button_notify;
580 		name = ACPI_BUTTON_DEVICE_NAME_SLEEP;
581 		sprintf(class, "%s/%s",
582 			ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
583 	} else if (!strcmp(hid, ACPI_BUTTON_HID_LID)) {
584 		button->type = ACPI_BUTTON_TYPE_LID;
585 		handler = acpi_lid_notify;
586 		name = ACPI_BUTTON_DEVICE_NAME_LID;
587 		sprintf(class, "%s/%s",
588 			ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
589 		input->open = acpi_lid_input_open;
590 	} else {
591 		pr_info("Unsupported hid [%s]\n", hid);
592 		error = -ENODEV;
593 	}
594 
595 	if (!error)
596 		error = acpi_button_add_fs(button);
597 
598 	if (error) {
599 		input_free_device(input);
600 		goto err_free_button;
601 	}
602 
603 	snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
604 
605 	input->name = name;
606 	input->phys = button->phys;
607 	input->id.bustype = BUS_HOST;
608 	input->id.product = button->type;
609 	input->dev.parent = &pdev->dev;
610 
611 	switch (button->type) {
612 	case ACPI_BUTTON_TYPE_POWER:
613 		input_set_capability(input, EV_KEY, KEY_POWER);
614 		input_set_capability(input, EV_KEY, KEY_WAKEUP);
615 		break;
616 
617 	case ACPI_BUTTON_TYPE_SLEEP:
618 		input_set_capability(input, EV_KEY, KEY_SLEEP);
619 		break;
620 
621 	case ACPI_BUTTON_TYPE_LID:
622 		input_set_capability(input, EV_SW, SW_LID);
623 		break;
624 	}
625 
626 	input_set_drvdata(input, button);
627 	error = input_register_device(input);
628 	if (error) {
629 		input_free_device(input);
630 		goto err_remove_fs;
631 	}
632 
633 	device_init_wakeup(button->dev, true);
634 
635 	switch (device->device_type) {
636 	case ACPI_BUS_TYPE_POWER_BUTTON:
637 		status = acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
638 							  acpi_button_event,
639 							  button);
640 		break;
641 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
642 		status = acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
643 							  acpi_button_event,
644 							  button);
645 		break;
646 	default:
647 		status = acpi_install_notify_handler(device->handle,
648 						     ACPI_ALL_NOTIFY, handler,
649 						     button);
650 		if (ACPI_SUCCESS(status) && device->wakeup.flags.valid) {
651 			acpi_status st;
652 
653 			/*
654 			 * If the wakeup GPE has a handler method, enable it in
655 			 * case it is also used for signaling runtime events.
656 			 */
657 			st = acpi_enable_gpe_cond(device->wakeup.gpe_device,
658 						   device->wakeup.gpe_number,
659 						   ACPI_GPE_DISPATCH_METHOD);
660 			button->gpe_enabled = ACPI_SUCCESS(st);
661 			if (button->gpe_enabled)
662 				dev_dbg(button->dev, "Enabled ACPI GPE%02llx\n",
663 					device->wakeup.gpe_number);
664 		}
665 		break;
666 	}
667 	if (ACPI_FAILURE(status)) {
668 		error = -ENODEV;
669 		goto err_input_unregister;
670 	}
671 
672 	if (button->type == ACPI_BUTTON_TYPE_LID) {
673 		/*
674 		 * This assumes there's only one lid device, or if there are
675 		 * more we only care about the last one...
676 		 */
677 		lid_device = device;
678 	}
679 
680 	pr_info("%s [%s]\n", name, acpi_device_bid(device));
681 	return 0;
682 
683 err_input_unregister:
684 	device_init_wakeup(button->dev, false);
685 	input_unregister_device(input);
686 err_remove_fs:
687 	acpi_button_remove_fs(button);
688 err_free_button:
689 	kfree(button);
690 	memset(acpi_device_class(device), 0, sizeof(acpi_device_class));
691 	return error;
692 }
693 
acpi_button_remove(struct platform_device * pdev)694 static void acpi_button_remove(struct platform_device *pdev)
695 {
696 	struct acpi_button *button = platform_get_drvdata(pdev);
697 	struct acpi_device *adev = button->adev;
698 
699 	switch (adev->device_type) {
700 	case ACPI_BUS_TYPE_POWER_BUTTON:
701 		acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
702 						acpi_button_event);
703 		break;
704 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
705 		acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
706 						acpi_button_event);
707 		break;
708 	default:
709 		if (button->gpe_enabled) {
710 			dev_dbg(button->dev, "Disabling ACPI GPE%02llx\n",
711 				adev->wakeup.gpe_number);
712 			acpi_disable_gpe(adev->wakeup.gpe_device,
713 					 adev->wakeup.gpe_number);
714 		}
715 		acpi_remove_notify_handler(adev->handle, ACPI_ALL_NOTIFY,
716 					   button->type == ACPI_BUTTON_TYPE_LID ?
717 						acpi_lid_notify :
718 						acpi_button_notify);
719 		break;
720 	}
721 	acpi_os_wait_events_complete();
722 
723 	device_init_wakeup(button->dev, false);
724 
725 	acpi_button_remove_fs(button);
726 	input_unregister_device(button->input);
727 	kfree(button);
728 
729 	memset(acpi_device_class(adev), 0, sizeof(acpi_device_class));
730 }
731 
param_set_lid_init_state(const char * val,const struct kernel_param * kp)732 static int param_set_lid_init_state(const char *val,
733 				    const struct kernel_param *kp)
734 {
735 	int i;
736 
737 	i = sysfs_match_string(lid_init_state_str, val);
738 	if (i < 0)
739 		return i;
740 
741 	lid_init_state = i;
742 	pr_info("Initial lid state set to '%s'\n", lid_init_state_str[i]);
743 	return 0;
744 }
745 
param_get_lid_init_state(char * buf,const struct kernel_param * kp)746 static int param_get_lid_init_state(char *buf, const struct kernel_param *kp)
747 {
748 	int i, c = 0;
749 
750 	for (i = 0; i < ARRAY_SIZE(lid_init_state_str); i++)
751 		if (i == lid_init_state)
752 			c += sprintf(buf + c, "[%s] ", lid_init_state_str[i]);
753 		else
754 			c += sprintf(buf + c, "%s ", lid_init_state_str[i]);
755 
756 	buf[c - 1] = '\n'; /* Replace the final space with a newline */
757 
758 	return c;
759 }
760 
761 module_param_call(lid_init_state,
762 		  param_set_lid_init_state, param_get_lid_init_state,
763 		  NULL, 0644);
764 MODULE_PARM_DESC(lid_init_state, "Behavior for reporting LID initial state");
765 
acpi_button_init(void)766 static int __init acpi_button_init(void)
767 {
768 	const struct dmi_system_id *dmi_id;
769 
770 	if (lid_init_state == -1) {
771 		dmi_id = dmi_first_match(dmi_lid_quirks);
772 		if (dmi_id)
773 			lid_init_state = (long)dmi_id->driver_data;
774 		else
775 			lid_init_state = ACPI_BUTTON_LID_INIT_METHOD;
776 	}
777 
778 	/*
779 	 * Modules such as nouveau.ko and i915.ko have a link time dependency
780 	 * on acpi_lid_open(), and would therefore not be loadable on ACPI
781 	 * capable kernels booted in non-ACPI mode if the return value of
782 	 * platform_driver_register() is returned from here with ACPI disabled
783 	 * when this driver is built as a module.
784 	 */
785 	if (acpi_disabled)
786 		return 0;
787 
788 	return platform_driver_register(&acpi_button_driver);
789 }
790 
acpi_button_exit(void)791 static void __exit acpi_button_exit(void)
792 {
793 	if (!acpi_disabled)
794 		platform_driver_unregister(&acpi_button_driver);
795 }
796 
797 module_init(acpi_button_init);
798 module_exit(acpi_button_exit);
799