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