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