1#include <acpi/acpi.h>
2#include <console/printf.h>
3#include <err.h>
4#include <uacpi/event.h>
5#include <uacpi/sleep.h>
6
7#include "uacpi/status.h"
8#include <types/types.h>
9
10static int system_shutdown(void) {
11 uacpi_status ret = uacpi_prepare_for_sleep_state(UACPI_SLEEP_STATE_S5);
12 if (uacpi_unlikely_error(ret)) {
13 printf(format: "failed to prepare for sleep: %s", uacpi_status_to_string(ret));
14 return ERR_IO;
15 }
16
17 asm volatile("cli");
18
19 ret = uacpi_enter_sleep_state(UACPI_SLEEP_STATE_S5);
20 if (uacpi_unlikely_error(ret)) {
21 printf(format: "failed to enter sleep: %s", uacpi_status_to_string(ret));
22 return ERR_IO;
23 }
24
25 return 0;
26}
27
28static uacpi_interrupt_ret handle_power_button(uacpi_handle ctx) {
29 cc_ignore(ctx);
30 system_shutdown();
31 return UACPI_INTERRUPT_HANDLED;
32}
33
34int power_button_init(void) {
35 uacpi_status ret = uacpi_install_fixed_event_handler(
36 event: UACPI_FIXED_EVENT_POWER_BUTTON, handler: handle_power_button, UACPI_NULL);
37 if (uacpi_unlikely_error(ret)) {
38 printf(format: "failed to install power button event callback: %s",
39 uacpi_status_to_string(ret));
40 return -ERR_NO_DEV;
41 }
42
43 return 0;
44}
45