Fix 2720 Flip cover init, shared backlight and flip lock

This commit is contained in:
Luxferre committed 2026-11-10 23:20:13 +02:00
1 parent c939687138
commit b389120328
24 files changed
+494 -217

No files matched your search

+221 -42
View File
@@ -52,6 +52,7 @@
#define MAIN_BLANK_CFG "/home/user/.config/sxmo/idle_blank_timeout"
#define DEFAULT_BLANK_SECS 300
#define CLOCK_REDRAW_MS 30000
#define KERNEL_REDRAW_MS 1000
#define COVER_SPI_WIDTH 240
#define COVER_SPI_HEIGHT 240
#define COVER_SPI_CHUNK 2048
@@ -140,7 +141,12 @@ struct cover_hw {
char probe_desc[96];
char kernel_fb[128];
char kernel_dpms[128];
char kernel_cmd[128];
char kernel_bl[128];
char kernel_marker[128];
unsigned int kernel_bl_level;
int kernel_lit;
int kernel_owned;
int sim;
int spi_enabled;
int fd_spi;
@@ -355,6 +361,15 @@ static int write_all(int fd, const unsigned char *buffer, size_t length)
return 1;
}
static void write_cover_text(const char *path, const char *value)
{
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
if(fd < 0)
return;
(void)!write(fd, value, strlen(value));
close(fd);
}
struct state {
char lid[16];
char backend[16];
@@ -834,12 +849,18 @@ static int cover_spi_draw(struct cover_hw *hw, const struct state *state)
return 1;
}
static void cover_kernel_release(struct cover_hw *hw);
/* On an open flip the daemon must not own the shared QUP5 bus at all. The
* mux line stays claimed for the daemon's lifetime (so it is always driven
* back to the internal panel), while the DC/reset handle is released. */
static void cover_hw_release(struct cover_hw *hw)
{
unsigned char mux_value[1];
if(hw->kernel_owned) {
cover_kernel_release(hw);
return;
}
if(!hw->mux_claimed)
return;
if(hw->initialized)
@@ -857,6 +878,7 @@ static void cover_hw_release(struct cover_hw *hw)
static void cover_hw_shutdown(struct cover_hw *hw)
{
cover_hw_release(hw);
cover_kernel_release(hw);
if(!hw->sim) {
if(hw->fd_mux >= 0)
close(hw->fd_mux);
@@ -899,14 +921,14 @@ static int cover_main_panel_is_off(void)
}
/* --- Kernel-owned cover path ----------------------------------------------
* The external panel sits behind the gpio13 bus switch on the same QUP5 bus
* as the internal panel. The kernel panel-mipi-dbi-spi driver owns CS0, the
* shared gpio110 D/C line and the firmware init stream, so userspace cannot
* drive the cover with its own D/C framing. Instead, while the flip is closed
* we switch the mux to the cover, release its reset, force a DRM DPMS off/on
* cycle (which makes the kernel driver replay its init sequence onto the panel
* now on the bus) and render the cover UI into the main framebuffer. The main
* backlight and keypad stay at 0, so only the outer panel can light up.
* The KaiOS 3.10 DTS shows that both GC9305s share CS0, gpio110 D/C and the
* PMIC MPP2 PWM backlight. gpio13 selects the panel, gpio23 is the external
* reset, and gpio21 is TE (not D/C). The kernel panel-mipi-dbi-spi driver
* owns CS0 and gpio110, so the cover is driven by the driver itself:
* pulse gpio23, replay the stock 240x240 init through its debugfs command
* hook, turn the shared backlight on and flush a 240x240 /dev/fb0 damage
* rect. A marker lets bananarchy-display-power-sync leave that backlight on
* while the lid is closed and the cover is lit.
*/
static void hw_dpms(struct cover_hw *hw, const char *mode)
{
@@ -924,6 +946,108 @@ static void hw_dpms(struct cover_hw *hw, const char *mode)
}
}
static int cover_kernel_command(struct cover_hw *hw, unsigned char cmd,
const unsigned char *params,
unsigned int count)
{
char line[256];
char event[320];
size_t used;
unsigned int i;
int flags = O_WRONLY | O_CLOEXEC;
int fd;
used = (size_t)snprintf(line, sizeof(line), "%02x", cmd);
for(i = 0; i < count && used + 4 < sizeof(line); i++)
used += (size_t)snprintf(line + used, sizeof(line) - used,
" %02x", params[i]);
snprintf(event, sizeof(event), "kernel cmd %s", line);
hw_event(hw, event);
if(hw->sim)
flags |= O_TRUNC;
fd = open(hw->kernel_cmd, flags);
if(fd < 0)
return hw->sim;
if(!write_all(fd, (const unsigned char *)line, strlen(line))) {
close(fd);
return 0;
}
close(fd);
return 1;
}
static int cover_kernel_replay_firmware(struct cover_hw *hw)
{
unsigned char buf[256];
size_t len;
size_t i = 16;
FILE *handle = fopen(hw->firmware, "rb");
if(!handle)
return 0;
len = fread(buf, 1, sizeof(buf), handle);
fclose(handle);
if(len < 18 || memcmp(buf, "MIPI DBI", 8) != 0 || buf[15] != 1)
return 0;
while(i + 1 < len) {
unsigned char cmd = buf[i++];
unsigned char count = buf[i++];
if(i + count > len)
return 0;
if(cmd == 0x00 && count == 1) {
char line[64];
snprintf(line, sizeof(line), "kernel delay %u",
(unsigned int)buf[i]);
hw_event(hw, line);
if(!hw->sim)
poll(NULL, 0, (int)buf[i]);
} else if(!cover_kernel_command(hw, cmd, buf + i, count)) {
return 0;
}
i += count;
}
return i == len;
}
static void cover_kernel_reset(struct cover_hw *hw)
{
unsigned char values[2];
values[0] = (unsigned char)hw->mux_closed_level;
values[1] = (unsigned char)hw->reset_idle_level;
hw_gpio_set(hw, hw->fd_mux, values, 2);
if(!hw->sim)
poll(NULL, 0, 1);
values[1] = (unsigned char)hw->reset_assert_level;
hw_gpio_set(hw, hw->fd_mux, values, 2);
if(!hw->sim)
poll(NULL, 0, 10);
values[1] = (unsigned char)hw->reset_idle_level;
hw_gpio_set(hw, hw->fd_mux, values, 2);
if(!hw->sim)
poll(NULL, 0, 10);
hw_event(hw, "kernel reset");
}
static void cover_kernel_backlight(struct cover_hw *hw, int on)
{
char value[32];
char line[64];
unsigned int level = on ? hw->kernel_bl_level : 0;
/* Publish the marker before changing brightness: the backlight uevent runs
* bananarchy-display-power-sync, which must not force the cover dark. */
if(on)
write_cover_text(hw->kernel_marker, "1");
else
unlink(hw->kernel_marker);
snprintf(value, sizeof(value), "%u", level);
write_cover_text(hw->kernel_bl, value);
snprintf(line, sizeof(line), "kernel backlight %u", level);
hw_event(hw, line);
}
static void cover_kernel_display_off(struct cover_hw *hw)
{
cover_kernel_command(hw, 0x28, NULL, 0);
}
static int cover_kernel_claim(struct cover_hw *hw)
{
unsigned int offsets[2];
@@ -941,6 +1065,7 @@ static int cover_kernel_claim(struct cover_hw *hw)
return 0;
hw->fd_mux = fd;
hw->mux_claimed = 1;
hw->kernel_owned = 1;
values[0] = (unsigned char)hw->mux_closed_level;
values[1] = (unsigned char)hw->reset_idle_level;
return hw_gpio_set(hw, hw->fd_mux, values, 2);
@@ -949,12 +1074,9 @@ static int cover_kernel_claim(struct cover_hw *hw)
static void cover_kernel_release(struct cover_hw *hw)
{
unsigned char values[2];
/* The spidev backend owns fd_mux too; only the kernel backend may release
* it here, or its STATUS would lose the restored mux state. */
if(strcmp(hw->backend, "kernel") != 0)
return;
if(!hw->mux_claimed)
if(!hw->kernel_owned)
return;
cover_kernel_backlight(hw, 0);
values[0] = (unsigned char)hw->mux_open_level;
values[1] = (unsigned char)hw->reset_idle_level;
hw_gpio_set(hw, hw->fd_mux, values, 2);
@@ -963,9 +1085,25 @@ static void cover_kernel_release(struct cover_hw *hw)
close(hw->fd_mux);
hw->fd_mux = -1;
hw->mux_claimed = 0;
hw->kernel_owned = 0;
hw->kernel_lit = -1;
}
static void cover_bus_release(struct cover_hw *hw)
{
if(hw->kernel_owned) {
cover_kernel_release(hw);
return;
}
if(!hw->mux_claimed)
return;
cover_hw_release(hw);
if(!hw->sim && hw->fd_mux >= 0)
close(hw->fd_mux);
hw->fd_mux = -1;
hw->mux_claimed = 0;
}
static int cover_kernel_frame(struct cover_hw *hw, const struct state *state)
{
unsigned int width = COVER_SPI_WIDTH;
@@ -1024,8 +1162,17 @@ static const char *cover_kernel_draw(struct cover_hw *hw,
hw_dpms(hw, "reinit");
if(!hw->sim)
poll(NULL, 0, 180);
cover_kernel_reset(hw);
if(!cover_kernel_replay_firmware(hw))
return "none";
cover_kernel_backlight(hw, 1);
if(!hw->sim)
poll(NULL, 0, 50);
} else {
if(hw->kernel_lit > 0)
cover_kernel_display_off(hw);
hw_dpms(hw, "off");
cover_kernel_backlight(hw, 0);
}
hw->kernel_lit = lit;
}
@@ -1034,28 +1181,6 @@ static const char *cover_kernel_draw(struct cover_hw *hw,
return lit ? "kernel" : "kernel-dark";
}
/* The kernel backend keeps DPMS ON while the cover is lit (that is how the
* DRM driver flushes to it), so its safety condition is the main backlight
* being dark, not DPMS. */
static int cover_main_backlight_is_off(void)
{
const char *path = getenv("BANANARCHY_COVER_BL_PATH");
char buf[32];
ssize_t got;
int fd;
if(!path || !*path)
path = "/sys/class/backlight/backlight/brightness";
fd = open(path, O_RDONLY | O_CLOEXEC);
if(fd < 0)
return 1;
got = read(fd, buf, sizeof(buf) - 1);
close(fd);
if(got <= 0)
return 1;
buf[got] = '\0';
return atoi(buf) == 0;
}
static const char *cover_draw(struct cover_hw *hw, const char *fb_path,
const struct state *state)
{
@@ -1071,12 +1196,17 @@ static const char *cover_draw(struct cover_hw *hw, const char *fb_path,
return "idle";
}
if(strcmp(hw->backend, "none") == 0) {
cover_hw_release(hw);
cover_kernel_release(hw);
cover_bus_release(hw);
return "none";
}
if(strcmp(hw->backend, "kernel") == 0) {
if(!cover_main_backlight_is_off()) {
/* A runtime backend switch must hand over the spidev linehandle first. */
if(hw->mux_claimed && !hw->kernel_owned)
cover_bus_release(hw);
/* Take the shared bus only after the lid daemon has stopped Xorg's
* main-panel flushes. Once the cover owns it, DPMS is intentionally On
* so the kernel driver can send frames. */
if(hw->kernel_lit < 0 && !cover_main_panel_is_off()) {
cover_kernel_release(hw);
return "pending";
}
@@ -1181,9 +1311,20 @@ static void cover_hw_init(struct cover_hw *hw)
hw->panel_lines = 1;
hw->probe_index = -1;
hw->kernel_lit = -1;
safe_copy(hw->spi_path, sizeof(hw->spi_path), "/dev/spidev0.1");
safe_copy(hw->gpio_path, sizeof(hw->gpio_path), "/dev/gpiochip0");
safe_copy(hw->firmware, sizeof(hw->firmware),
"/lib/firmware/nokia,beatles-gc9305-ext-panel.bin");
safe_copy(hw->kernel_fb, sizeof(hw->kernel_fb), "/dev/fb0");
safe_copy(hw->kernel_dpms, sizeof(hw->kernel_dpms),
"/usr/bin/bananarchy-cover-dpms.sh");
safe_copy(hw->kernel_cmd, sizeof(hw->kernel_cmd),
"/sys/kernel/debug/dri/0/command");
safe_copy(hw->kernel_bl, sizeof(hw->kernel_bl),
"/sys/class/backlight/backlight/brightness");
safe_copy(hw->kernel_marker, sizeof(hw->kernel_marker),
"/run/bananarchy/cover-backlight");
hw->kernel_bl_level = 14;
safe_copy(hw->probe_desc, sizeof(hw->probe_desc), "none");
hw->fd_spi = -1;
hw->fd_chip = -1;
@@ -1193,10 +1334,25 @@ static void cover_hw_init(struct cover_hw *hw)
{
const char *fb = getenv("BANANARCHY_COVER_MAIN_FB");
const char *dp = getenv("BANANARCHY_COVER_DPMS_HELPER");
const char *cmd = getenv("BANANARCHY_COVER_KERNEL_CMD");
const char *bl = getenv("BANANARCHY_COVER_KERNEL_BL");
const char *marker = getenv("BANANARCHY_COVER_KERNEL_MARKER");
const char *level = getenv("BANANARCHY_COVER_KERNEL_BL_LEVEL");
if(fb && *fb)
safe_copy(hw->kernel_fb, sizeof(hw->kernel_fb), fb);
if(dp && *dp)
safe_copy(hw->kernel_dpms, sizeof(hw->kernel_dpms), dp);
if(cmd && *cmd)
safe_copy(hw->kernel_cmd, sizeof(hw->kernel_cmd), cmd);
if(bl && *bl)
safe_copy(hw->kernel_bl, sizeof(hw->kernel_bl), bl);
if(marker && *marker)
safe_copy(hw->kernel_marker, sizeof(hw->kernel_marker), marker);
if(level && *level) {
unsigned long parsed = strtoul(level, NULL, 10);
if(parsed > 0 && parsed <= 255)
hw->kernel_bl_level = (unsigned int)parsed;
}
}
sim = getenv("BANANARCHY_COVER_SPI_SIM");
if(sim && *sim) {
@@ -1261,6 +1417,17 @@ static void read_config(const char *path, char *socket_path, size_t splen,
safe_copy(hw->kernel_fb, sizeof(hw->kernel_fb), value);
else if(strcmp(key, "kernel_dpms_helper") == 0)
safe_copy(hw->kernel_dpms, sizeof(hw->kernel_dpms), value);
else if(strcmp(key, "kernel_command_path") == 0)
safe_copy(hw->kernel_cmd, sizeof(hw->kernel_cmd), value);
else if(strcmp(key, "kernel_backlight_path") == 0)
safe_copy(hw->kernel_bl, sizeof(hw->kernel_bl), value);
else if(strcmp(key, "kernel_backlight_marker") == 0)
safe_copy(hw->kernel_marker, sizeof(hw->kernel_marker), value);
else if(strcmp(key, "kernel_backlight_level") == 0) {
v = strtol(value, NULL, 10);
if(v > 0 && v <= 255)
hw->kernel_bl_level = (unsigned int)v;
}
else if(strcmp(key, "gpiochip_device") == 0)
safe_copy(hw->gpio_path, sizeof(hw->gpio_path), value);
else if(strcmp(key, "panel_firmware") == 0)
@@ -1455,9 +1622,15 @@ static void handle_command(char *buffer, struct state *state,
hw->word_bits = (int)bits;
else
return;
} else if(strcmp(key, "kernel_backlight_level") == 0) {
long level = strtol(value, NULL, 10);
if(level > 0 && level <= 255)
hw->kernel_bl_level = (unsigned int)level;
else
return;
} else
return;
cover_hw_release(hw);
cover_bus_release(hw);
*redraw = 1;
} else if(strcmp(command, "PROBE") == 0) {
const struct probe_case *p;
@@ -1477,7 +1650,11 @@ static void handle_command(char *buffer, struct state *state,
hw->probe_index + 1,
(int)(sizeof(probe_cases) / sizeof(probe_cases[0])),
p->bits, p->mux, p->reset, p->dc, p->speed);
cover_hw_release(hw);
/* PROBE is the legacy userspace sign-off path; it owns its own D/C. */
safe_copy(hw->backend, sizeof(hw->backend), "spidev");
hw->spi_enabled = hw->spi_path[0] && hw->gpio_path[0] &&
hw->firmware[0];
cover_bus_release(hw);
state->test = 2;
state->visible = 1;
state->last_input_ms = now_ms();
@@ -1595,7 +1772,9 @@ int main(int argc, char **argv)
/* With the cover off there is no timer at all: poll blocks until IPC. */
int timeout_ms = -1;
if(state.visible) {
long long next = CLOCK_REDRAW_MS - (now - last_draw);
long long interval = strcmp(hw.backend, "kernel") == 0 ?
KERNEL_REDRAW_MS : CLOCK_REDRAW_MS;
long long next = interval - (now - last_draw);
if(next < 1)
next = 1;
if(state.blank_ms > 0) {
@@ -1608,7 +1787,7 @@ int main(int argc, char **argv)
}
}
timeout_ms = (int)(next > 1000000 ? 1000000 : next);
if(state.visible && now - last_draw >= CLOCK_REDRAW_MS)
if(state.visible && now - last_draw >= interval)
redraw = 1;
}
if(redraw) {
+6 -4
View File
@@ -214,14 +214,16 @@ static void on_transition(int closed)
* the DPMS re-init below hits the internal panel, not the cover. */
cover_send("OPEN");
poll(NULL, 0, 80);
unlink(LOCK_FLAG);
write_session_file(state, "unlock");
/* Closing the flip locks the desktop. Opening only wakes the panel into
* the visible passcode lockscreen; it must not auto-unlock. */
write_session_file(LOCK_FLAG, "1");
write_session_file(state, "lock");
write_text_file(BL_POWER, "0");
write_sysfs_int(BRIGHTNESS, bl);
write_sysfs_int(KBD_BACKLIGHT, kbd);
run_dpms("reinit");
locker_send("CLOSE\n");
run_sxmo_state("unlock", "open");
locker_send("LOCK\n");
run_sxmo_state("lock", "open");
}
}
+1 -1
View File
@@ -1096,7 +1096,7 @@ int locker_run_main(int argc, char** argv) {
}
continue;
}
// A trusted root lid daemon clears the lock flag before sending CLOSE.
// A trusted unlock path removes the lock flag before sending CLOSE.
// If the flag is gone, release any stale locker grab/window immediately;
// otherwise ignore CLOSE so user scripts cannot dismiss the lockscreen.
if(cmd == "CLOSE" || cmd == "UNLOCK") {