Skip to content

Commit 6024a61

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 076ebea commit 6024a61

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

drivers/power_button.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 *notused) {
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() {
37+
#ifdef KTF_ACPICA
38+
ACPI_TABLE_FADT *fadt = acpi_find_table(ACPI_SIG_FADT);
39+
40+
if (!(fadt->Flags & ACPI_FADT_POWER_BUTTON)) {
41+
printk("PWRB: Configuring ACPI 'fixed' power button handling\n");
42+
43+
if (ACPI_FAILURE(AcpiClearEvent(ACPI_EVENT_POWER_BUTTON)))
44+
panic("PWRB: Failed to clear power button event");
45+
46+
if (ACPI_FAILURE(AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
47+
button_handler, NULL)))
48+
panic("PWRB: Failed to install power button handler");
49+
}
50+
else {
51+
panic("PWRB: Non-fixed power button not implemented\n");
52+
}
53+
54+
dprintk("PWRB: Initialized Power Button\n");
55+
#else
56+
panic("PWRB: Power button without ACPICA not implemented\n");
57+
#endif
58+
59+
return true;
60+
}

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();
17+
18+
#endif /* KTF_POWER_BUTTON_H */

0 commit comments

Comments
 (0)