Skip to content

Commit 93b3e0f

Browse files
committed
drivers: ACPI fixed power button driver
Driver for ACPI fixed power buttons. It allows overwriting the default handler so the button can be used for custom purposes. One such purpose might be to trigger an experiment to run. Signed-off-by: Sandro Rüegge <[email protected]>
1 parent 17e7997 commit 93b3e0f

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

drivers/power_button.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <acpi_ktf.h>
2+
#include <drivers/power_button.h>
3+
#include <spinlock.h>
4+
5+
static void default_handler(void *);
6+
7+
spinlock_t pb_handler_lock = SPINLOCK_INIT;
8+
/** power button press handler */
9+
static pb_handler_t pb_handler = default_handler;
10+
/** context passed to the power button press handler */
11+
static void *pb_context = NULL;
12+
13+
static UINT32 button_handler(void *Context) {
14+
AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
15+
16+
if (spin_try_lock(&pb_handler_lock)) {
17+
if (pb_handler)
18+
pb_handler(pb_context);
19+
spin_unlock(&pb_handler_lock);
20+
}
21+
22+
return ACPI_INTERRUPT_HANDLED;
23+
}
24+
25+
static void default_handler(void *) {
26+
acpi_power_off();
27+
}
28+
29+
void pb_set_handler(pb_handler_t handler, void *context) {
30+
spin_lock(&pb_handler_lock);
31+
pb_handler = handler;
32+
pb_context = context;
33+
spin_unlock(&pb_handler_lock);
34+
}
35+
36+
bool init_power_button(const cpu_t *cpu) {
37+
ACPI_TABLE_FADT *fadt = acpi_find_table(ACPI_SIG_FADT);
38+
39+
if (!(fadt->Flags & ACPI_FADT_POWER_BUTTON)) {
40+
printk("PWRB: fixed button handler\n");
41+
42+
if (ACPI_FAILURE(AcpiClearEvent(ACPI_EVENT_POWER_BUTTON))) {
43+
panic("PWRB: Failed to clear POWER BUTTON EVENT");
44+
}
45+
if (ACPI_FAILURE(AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
46+
button_handler, NULL))) {
47+
panic("PWRB: Failed to install power button handler");
48+
}
49+
}
50+
else {
51+
panic("Could not initialize power button\n");
52+
}
53+
54+
dprintk("Initialized Power Button\n");
55+
56+
return true;
57+
}

include/drivers/power_button.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef KTF_POWER_BUTTON_H
2+
#define KTF_POWER_BUTTON_H
3+
4+
#include <cpu.h>
5+
6+
typedef void (*pb_handler_t)(void *);
7+
8+
/**
9+
* Set a handler for the power button. Useful to control experiments.
10+
*/
11+
void pb_set_handler(pb_handler_t handler, void *context);
12+
13+
/**
14+
* Initialize power button handling. Requires ACPICA library.
15+
*/
16+
extern bool init_power_button(const cpu_t *cpu);
17+
18+
#endif /* KTF_POWER_BUTTON_H */

0 commit comments

Comments
 (0)