Drive the 2720 Flip cover panel from userspace while closed

The shipping single-panel DTB left the external GC9305 unused because
bananarchy-cover-idle only knew /dev/fb1. The compiled daemon now owns the
shared QUP5 bus while the flip is closed: it claims gpio13/21/23 through
the gpiochip ABI, replays the external firmware over /dev/spidev0.1 at the
verified 25 MHz and pushes 240x240 RGB565 frames. LID OPEN restores the mux
and releases the panel lines; activation also requires the main panel to be
DPMS-off. Polarities are config keys and live SET knobs, with
SET backend none as a rollback. /dev/fb1 keeps precedence and the two-panel
spi-mux DT stays opt-in.

Rebuilt the a1108/dejavu nokia-beatles image and pinned the verified hash
ccc8c44517a6a3b78e16e45e9dddbf89f6f04a35ee4c52246d38d89970bd13ac in
check-rebuild.sh. e2fsck -fn passes on both partitions, debugfs confirms
the four updated rootfs files byte-for-byte, and the bootfs sync, boot image
check, artifact sync, host tests and rebuild preflight all pass.

This is an intermediate checkpoint: eyes-on confirmation that the cover
lights up and several more polarity/CS fixes are still required.
This commit is contained in:
Luxferre committed 2026-11-10 19:41:57 +02:00
1 parent d402a53221
commit 40a840b49b
12 files changed
+1085 -72

No files matched your search

+642 -55
View File
@@ -11,6 +11,14 @@
* default 300 s, 0 = never) blanks the cover again when no button arrives;
* - all other events (network, SMS, battery, calls, theme) only update the
* frame while the cover is visible, so an idle daemon stays at 0% CPU.
*
* The 2720's two GC9305 panels share one QUP5 SPI bus through a gpio13 mux.
* Registering both panels in the DT darkens the main panel, so the shipping
* DTB keeps one panel and this daemon drives the external panel from userspace
* on /dev/spidev0.1 ONLY while the flip is closed (the main panel is blanked
* and DPMS-off then). On open it restores the mux to the internal panel and
* releases every GPIO/SPIDEV handle before Xorg can flush another frame.
* If the opt-in two-panel DT is installed, /dev/fb1 takes precedence instead.
*/
#include <ctype.h>
#include <poll.h>
@@ -44,6 +52,89 @@
#define MAIN_BLANK_CFG "/home/user/.config/sxmo/idle_blank_timeout"
#define DEFAULT_BLANK_SECS 300
#define CLOCK_REDRAW_MS 30000
#define COVER_SPI_WIDTH 240
#define COVER_SPI_HEIGHT 240
#define COVER_SPI_CHUNK 2048
#define MAIN_DPMS_PATH "/sys/class/drm/card0-SPI-1/dpms"
/* Self-contained linux/gpio.h (v1 ABI) and linux/spi/spidev.h UAPI values.
* The armv7 cross sysroot does not always ship these headers. The layouts are
* fixed by the kernel ABI; a compile-time size check guards the cross build. */
#define BA_GPIO_IOC_MAGIC 0xb4u
#define BA_IOC_WR 1u
#define BA_IOC_RDWR 3u
#define BA_IOC(dir, type, nr, size) \
(((unsigned int)(dir) << 30) | ((unsigned int)(type) << 8) | \
((unsigned int)(nr) << 0) | ((unsigned int)(size) << 16))
#define BA_SPI_IOC_MESSAGE(n) \
BA_IOC(BA_IOC_WR, 'k', 0u, (unsigned int)(n) * 32u)
#define BA_SPI_IOC_WR_MODE BA_IOC(BA_IOC_WR, 'k', 1u, 1u)
#define BA_SPI_IOC_WR_BITS BA_IOC(BA_IOC_WR, 'k', 3u, 1u)
#define BA_SPI_IOC_WR_SPEED BA_IOC(BA_IOC_WR, 'k', 4u, 4u)
#define BA_GPIO_GET_LINEHANDLE BA_IOC(BA_IOC_RDWR, BA_GPIO_IOC_MAGIC, 3u, 364u)
#define BA_GPIO_GET_VALUES BA_IOC(BA_IOC_RDWR, BA_GPIO_IOC_MAGIC, 8u, 64u)
#define BA_GPIO_SET_VALUES BA_IOC(BA_IOC_RDWR, BA_GPIO_IOC_MAGIC, 9u, 64u)
#define BA_GPIOHANDLE_REQUEST_OUTPUT (1u << 1)
struct ba_gpiohandle_request {
uint32_t lineoffsets[64];
uint32_t flags;
uint8_t default_values[64];
char consumer_label[32];
uint32_t lines;
int32_t fd;
};
struct ba_gpiohandle_data {
uint8_t values[64];
};
struct ba_spi_ioc_transfer {
uint64_t tx_buf;
uint64_t rx_buf;
uint32_t len;
uint32_t speed_hz;
uint16_t delay_usecs;
uint8_t bits_per_word;
uint8_t cs_change;
uint8_t tx_nbits;
uint8_t rx_nbits;
uint8_t word_delay_usecs;
uint8_t pad;
};
_Static_assert(sizeof(struct ba_gpiohandle_request) == 364,
"gpiochip v1 linehandle ABI size");
_Static_assert(sizeof(struct ba_spi_ioc_transfer) == 32,
"spidev transfer ABI size");
struct cover_hw {
char backend[16];
char spi_path[128];
char gpio_path[128];
char firmware[128];
char sim_path[128];
unsigned int mux_gpio;
unsigned int dc_gpio;
unsigned int reset_gpio;
int mux_closed_level;
int mux_open_level;
int dc_command_level;
int dc_data_level;
int reset_assert_level;
int reset_idle_level;
unsigned int speed_hz;
int sim;
int spi_enabled;
int fd_spi;
int fd_chip;
int fd_mux;
int fd_panel;
int fd_sim;
int mux_claimed;
int claimed;
int initialized;
};
struct glyph {
char ch;
@@ -272,6 +363,55 @@ static void set_backlight(const struct state *state, int on)
close(fd);
}
static void render_frame(struct frame *fb, const struct state *state)
{
char clock[16];
char date[32];
char battery[32];
time_t now = time(NULL);
struct tm tm;
unsigned int width = fb->width;
unsigned int height = fb->height;
localtime_r(&now, &tm);
strftime(clock, sizeof(clock), "%H:%M", &tm);
strftime(date, sizeof(date), "%d.%m.%Y", &tm);
if(state->test == 2) {
frame_rect(fb, 0, 0, (int)width / 4, (int)height,
rgb888(250, 40, 40));
frame_rect(fb, (int)width / 4, 0, (int)width / 2, (int)height,
rgb888(40, 230, 80));
frame_rect(fb, (int)width / 2, 0, (int)width * 3 / 4, (int)height,
rgb888(60, 110, 250));
frame_rect(fb, (int)width * 3 / 4, 0, (int)width, (int)height,
rgb888(245, 245, 245));
} else if(state->test == 3) {
frame_rect(fb, 0, 0, (int)width, (int)height,
rgb888(245, 245, 245));
} else if(!cover_is_lit(state)) {
frame_rect(fb, 0, 0, (int)width, (int)height, rgb888(0, 0, 0));
} else {
frame_rect(fb, 0, 0, (int)width, (int)height, rgb888(8, 12, 26));
frame_rect(fb, 0, 0, (int)width, 3, rgb888(250, 210, 60));
frame_text(fb, "BANANARCHY", 12, 26, 2, rgb888(250, 210, 60));
frame_text(fb, clock, 20, (int)height / 2 - 20,
width >= 220 ? 5 : 3, rgb888(235, 240, 248));
frame_text(fb, date, 30, (int)height / 2 + 40, 2,
rgb888(90, 150, 240));
/* Compact network / battery indicators, pushed by the event bridge. */
if(state->network[0])
frame_text(fb, state->network, 6, (int)height - 44, 1,
rgb888(120, 230, 160));
if(state->battery >= 0) {
snprintf(battery, sizeof(battery), "BAT %d%%", state->battery);
frame_text(fb, battery, 6, (int)height - 26, 1,
rgb888(250, 210, 60));
}
if(state->message[0])
frame_text(fb, state->message, 6, (int)height - 12, 1,
rgb888(235, 240, 248));
}
}
static int draw_frame(const char *fb_path, const struct state *state)
{
int fd = open(fb_path, O_RDWR | O_CLOEXEC);
@@ -282,11 +422,6 @@ static int draw_frame(const char *fb_path, const struct state *state)
size_t size;
unsigned char *mem;
struct frame fb;
char clock[16];
char date[32];
char battery[32];
time_t now = time(NULL);
struct tm tm;
if(fd < 0)
return 0;
if(!fb_geometry(fb_path, &width, &height, &bpp, &stride)) {
@@ -304,44 +439,7 @@ static int draw_frame(const char *fb_path, const struct state *state)
fb.height = height;
fb.stride = stride;
fb.bpp = bpp;
localtime_r(&now, &tm);
strftime(clock, sizeof(clock), "%H:%M", &tm);
strftime(date, sizeof(date), "%d.%m.%Y", &tm);
if(state->test == 2) {
frame_rect(&fb, 0, 0, (int)width / 4, (int)height,
rgb888(250, 40, 40));
frame_rect(&fb, (int)width / 4, 0, (int)width / 2, (int)height,
rgb888(40, 230, 80));
frame_rect(&fb, (int)width / 2, 0, (int)width * 3 / 4, (int)height,
rgb888(60, 110, 250));
frame_rect(&fb, (int)width * 3 / 4, 0, (int)width, (int)height,
rgb888(245, 245, 245));
} else if(state->test == 3) {
frame_rect(&fb, 0, 0, (int)width, (int)height,
rgb888(245, 245, 245));
} else if(!cover_is_lit(state)) {
frame_rect(&fb, 0, 0, (int)width, (int)height, rgb888(0, 0, 0));
} else {
frame_rect(&fb, 0, 0, (int)width, (int)height, rgb888(8, 12, 26));
frame_rect(&fb, 0, 0, (int)width, 3, rgb888(250, 210, 60));
frame_text(&fb, "BANANARCHY", 12, 26, 2, rgb888(250, 210, 60));
frame_text(&fb, clock, 20, (int)height / 2 - 20,
width >= 220 ? 5 : 3, rgb888(235, 240, 248));
frame_text(&fb, date, 30, (int)height / 2 + 40, 2,
rgb888(90, 150, 240));
/* Compact network / battery indicators, pushed by the event bridge. */
if(state->network[0])
frame_text(&fb, state->network, 6, (int)height - 44, 1,
rgb888(120, 230, 160));
if(state->battery >= 0) {
snprintf(battery, sizeof(battery), "BAT %d%%", state->battery);
frame_text(&fb, battery, 6, (int)height - 26, 1,
rgb888(250, 210, 60));
}
if(state->message[0])
frame_text(&fb, state->message, 6, (int)height - 12, 1,
rgb888(235, 240, 248));
}
render_frame(&fb, state);
if(lseek(fd, 0, SEEK_SET) < 0 || !write_all(fd, mem, size)) {
free(mem);
close(fd);
@@ -359,6 +457,373 @@ static int draw_frame(const char *fb_path, const struct state *state)
return 1;
}
/* --- Closed-flip-only userspace GC9305 backend (/dev/spidev0.1) --------- */
static void hw_event(struct cover_hw *hw, const char *line)
{
if(hw->fd_sim < 0)
return;
(void)!write(hw->fd_sim, line, strlen(line));
(void)!write(hw->fd_sim, "\n", 1);
}
static int hw_gpio_set(struct cover_hw *hw, int fd,
const unsigned char *values, unsigned int count)
{
struct ba_gpiohandle_data data;
char line[64];
unsigned int i;
snprintf(line, sizeof(line), "gpio");
for(i = 0; i < count; i++)
snprintf(line + strlen(line), sizeof(line) - strlen(line),
" %u", values[i]);
hw_event(hw, line);
if(hw->sim)
return 1;
memset(&data, 0, sizeof(data));
for(i = 0; i < count && i < sizeof(data.values); i++)
data.values[i] = values[i];
return ioctl(fd, BA_GPIO_SET_VALUES, &data) >= 0;
}
static int hw_gpio_request(struct cover_hw *hw, const unsigned int *offsets,
const unsigned char *defaults, unsigned int count)
{
struct ba_gpiohandle_request req;
unsigned int i;
if(hw->sim) {
hw_event(hw, "claim");
return 1;
}
if(hw->fd_chip < 0)
return -1;
memset(&req, 0, sizeof(req));
for(i = 0; i < count && i < 64; i++) {
req.lineoffsets[i] = offsets[i];
req.default_values[i] = defaults[i];
}
req.flags = BA_GPIOHANDLE_REQUEST_OUTPUT;
snprintf(req.consumer_label, sizeof(req.consumer_label),
"bananarchy-cover");
req.lines = count;
req.fd = -1;
if(ioctl(hw->fd_chip, BA_GPIO_GET_LINEHANDLE, &req) < 0)
return -1;
hw_event(hw, "claim");
return req.fd;
}
static int hw_xfer(struct cover_hw *hw, const unsigned char *tx, size_t len,
int cs_change)
{
struct ba_spi_ioc_transfer tr;
if(hw->sim)
return 1;
memset(&tr, 0, sizeof(tr));
tr.tx_buf = (uint64_t)(uintptr_t)tx;
tr.len = (uint32_t)len;
tr.bits_per_word = 8;
tr.cs_change = (uint8_t)(cs_change ? 1 : 0);
return ioctl(hw->fd_spi, BA_SPI_IOC_MESSAGE(1), &tr) >= 0;
}
static int hw_dc(struct cover_hw *hw, int level)
{
unsigned char values[2];
values[0] = (unsigned char)level;
values[1] = (unsigned char)hw->reset_idle_level;
return hw_gpio_set(hw, hw->fd_panel, values, 2);
}
static int hw_command(struct cover_hw *hw, unsigned char cmd,
const unsigned char *params, size_t n)
{
char line[64];
snprintf(line, sizeof(line), "cmd %02x %zu", cmd, n);
hw_event(hw, line);
if(hw->sim)
return 1;
if(!hw_dc(hw, hw->dc_command_level))
return 0;
if(!hw_xfer(hw, &cmd, 1, n ? 0 : 1))
return 0;
if(!n)
return 1;
if(!hw_dc(hw, hw->dc_data_level))
return 0;
while(n) {
size_t chunk = n > COVER_SPI_CHUNK ? COVER_SPI_CHUNK : n;
if(!hw_xfer(hw, params, chunk, chunk == n))
return 0;
params += chunk;
n -= chunk;
}
return 1;
}
static int hw_init_panel(struct cover_hw *hw)
{
unsigned char blob[4096];
unsigned char blank[2] = {0, 0};
size_t len = 0;
size_t p;
ssize_t got;
int fd = open(hw->firmware, O_RDONLY | O_CLOEXEC);
if(fd < 0)
return 0;
got = read(fd, blob, sizeof(blob));
close(fd);
if(got < 17)
return 0;
len = (size_t)got;
if(memcmp(blob, "MIPI DBI", 8) != 0)
return 0;
p = 16;
while(p + 2 <= len) {
unsigned char cmd = blob[p];
size_t n = blob[p + 1];
p += 2;
if(p + n > len)
break;
if(cmd == 0x00 && n == 1) {
/* Vendor "delay ms" record. Host simulation skips the wait. */
if(!hw->sim)
poll(NULL, 0, (int)blob[p] > 0 ? (int)blob[p] : 1);
} else if(cmd == 0x2c && n == 0) {
/* Memory-write marker: every command gets its own CS framing, so a
* bare 0x2c is not part of the init sequence. */
hw_event(hw, "skip 2c");
} else {
hw_command(hw, cmd, n ? blob + p : blank, n);
}
p += n;
}
return 1;
}
static int hw_activate(struct cover_hw *hw)
{
unsigned int mux_offset[1];
unsigned int panel_offsets[2];
unsigned char mux_default[1];
unsigned char panel_default[2];
unsigned char values[2];
int fd;
if(!hw->spi_enabled)
return 0;
if(!hw->sim && hw->fd_chip < 0) {
hw->fd_chip = open(hw->gpio_path, O_RDWR | O_CLOEXEC);
if(hw->fd_chip < 0)
return 0;
}
if(!hw->mux_claimed) {
mux_offset[0] = hw->mux_gpio;
mux_default[0] = (unsigned char)hw->mux_open_level;
fd = hw_gpio_request(hw, mux_offset, mux_default, 1);
if(fd < 0)
return 0;
hw->fd_mux = fd;
hw->mux_claimed = 1;
}
{
unsigned char mux_value[1];
mux_value[0] = (unsigned char)hw->mux_closed_level;
hw_gpio_set(hw, hw->fd_mux, mux_value, 1);
}
if(!hw->claimed) {
panel_offsets[0] = hw->dc_gpio;
panel_offsets[1] = hw->reset_gpio;
panel_default[0] = (unsigned char)hw->dc_data_level;
panel_default[1] = (unsigned char)hw->reset_idle_level;
fd = hw_gpio_request(hw, panel_offsets, panel_default, 2);
if(fd < 0)
return 0;
hw->fd_panel = fd;
hw->claimed = 1;
hw->initialized = 0;
if(hw->fd_spi < 0 && !hw->sim) {
unsigned char mode = 0;
unsigned char bits = 8;
uint32_t speed = hw->speed_hz;
hw->fd_spi = open(hw->spi_path, O_RDWR | O_CLOEXEC);
if(hw->fd_spi < 0)
return 0;
if(ioctl(hw->fd_spi, BA_SPI_IOC_WR_MODE, &mode) < 0 ||
ioctl(hw->fd_spi, BA_SPI_IOC_WR_BITS, &bits) < 0 ||
ioctl(hw->fd_spi, BA_SPI_IOC_WR_SPEED, &speed) < 0)
return 0;
}
}
if(!hw->initialized) {
/* Reset pulse on the external panel, then replay its firmware stream. */
values[0] = (unsigned char)hw->dc_data_level;
values[1] = (unsigned char)hw->reset_assert_level;
hw_gpio_set(hw, hw->fd_panel, values, 2);
if(!hw->sim)
poll(NULL, 0, 20);
values[1] = (unsigned char)hw->reset_idle_level;
hw_gpio_set(hw, hw->fd_panel, values, 2);
if(!hw->sim)
poll(NULL, 0, 150);
if(!hw_init_panel(hw))
return 0;
hw->initialized = 1;
}
return 1;
}
static int hw_frame(struct cover_hw *hw, const struct frame *fb, int lit)
{
unsigned char cmd = 0x2c;
size_t size = (size_t)fb->stride * fb->height;
size_t left = size;
const unsigned char *pixels = fb->pixels;
char line[96];
snprintf(line, sizeof(line), "frame lit=%d size=%zu", lit, size);
hw_event(hw, line);
if(hw->sim)
return 1;
if(!hw_dc(hw, hw->dc_command_level))
return 0;
if(!hw_xfer(hw, &cmd, 1, 0))
return 0;
if(!hw_dc(hw, hw->dc_data_level))
return 0;
while(left) {
size_t chunk = left > COVER_SPI_CHUNK ? COVER_SPI_CHUNK : left;
if(!hw_xfer(hw, pixels, chunk, chunk == left))
return 0;
pixels += chunk;
left -= chunk;
}
return 1;
}
static int cover_spi_draw(struct cover_hw *hw, const struct state *state)
{
size_t size = COVER_SPI_WIDTH * COVER_SPI_HEIGHT * 2;
unsigned char *mem;
struct frame fb;
int lit = cover_is_lit(state);
if(!hw_activate(hw))
return 0;
mem = calloc(1, size);
if(!mem)
return 0;
fb.pixels = mem;
fb.width = COVER_SPI_WIDTH;
fb.height = COVER_SPI_HEIGHT;
fb.stride = COVER_SPI_WIDTH * 2;
fb.bpp = 16;
render_frame(&fb, state);
if(lit)
hw_command(hw, 0x29, NULL, 0);
if(!hw_frame(hw, &fb, lit)) {
free(mem);
return 0;
}
if(!lit)
hw_command(hw, 0x28, NULL, 0);
free(mem);
return 1;
}
/* 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->mux_claimed)
return;
if(hw->initialized)
hw_command(hw, 0x28, NULL, 0);
mux_value[0] = (unsigned char)hw->mux_open_level;
hw_gpio_set(hw, hw->fd_mux, mux_value, 1);
hw_event(hw, "release");
if(hw->claimed && !hw->sim)
close(hw->fd_panel);
hw->fd_panel = -1;
hw->claimed = 0;
hw->initialized = 0;
}
static void cover_hw_shutdown(struct cover_hw *hw)
{
cover_hw_release(hw);
if(!hw->sim) {
if(hw->fd_mux >= 0)
close(hw->fd_mux);
if(hw->fd_spi >= 0)
close(hw->fd_spi);
if(hw->fd_chip >= 0)
close(hw->fd_chip);
}
hw->fd_mux = -1;
hw->fd_spi = -1;
hw->fd_chip = -1;
if(hw->fd_sim >= 0) {
hw_event(hw, "close");
close(hw->fd_sim);
hw->fd_sim = -1;
}
hw->mux_claimed = 0;
}
/* The cover may only own QUP5 while the main panel is DPMS-off. This is the
* real closed-flip state and also rejects a transient lid-state flap at lid
* daemon startup, when Xorg is still flushing frames to the internal panel. */
static int cover_main_panel_is_off(void)
{
const char *path = getenv("BANANARCHY_COVER_DPMS_PATH");
char buf[16];
ssize_t got;
int fd;
if(!path || !*path)
path = MAIN_DPMS_PATH;
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 strncmp(buf, "Off", 3) == 0;
}
static const char *cover_draw(struct cover_hw *hw, const char *fb_path,
const struct state *state)
{
int flip_open = strcmp(state->lid, "open") == 0;
int fb_present = access(fb_path, W_OK) == 0;
int prefer_fb = strcmp(hw->backend, "spidev") != 0 &&
strcmp(hw->backend, "none") != 0 && fb_present;
if(flip_open) {
cover_hw_release(hw);
if(prefer_fb)
return draw_frame(fb_path, state) ? "fb" : "none";
return "idle";
}
if(strcmp(hw->backend, "none") == 0) {
cover_hw_release(hw);
return "none";
}
if(prefer_fb) {
cover_hw_release(hw);
return draw_frame(fb_path, state) ? "fb" : "none";
}
if(hw->spi_enabled) {
if(!cover_main_panel_is_off()) {
cover_hw_release(hw);
return "pending";
}
return cover_spi_draw(hw, state) ? "spidev" : "none";
}
cover_hw_release(hw);
return "none";
}
static void mkdir_p(const char *path)
{
char buf[256];
@@ -414,8 +879,50 @@ static long long blank_timeout_ms(const char *configured)
return (long long)secs * 1000LL;
}
static int config_level(const char *value, int fallback)
{
long v = strtol(value, NULL, 10);
if(v < 0 || v > 1)
return fallback;
return (int)v;
}
static void cover_hw_init(struct cover_hw *hw)
{
const char *sim;
memset(hw, 0, sizeof(*hw));
safe_copy(hw->backend, sizeof(hw->backend), "auto");
hw->mux_gpio = 13;
hw->dc_gpio = 21;
hw->reset_gpio = 23;
/* The live phone holds gpio13 low while the 240x320 main panel is active,
* so low selects the internal panel and high must select the cover. */
hw->mux_open_level = 0;
hw->mux_closed_level = 1;
hw->dc_command_level = 0;
hw->dc_data_level = 1;
hw->reset_assert_level = 0;
hw->reset_idle_level = 1;
hw->speed_hz = 25000000;
hw->fd_spi = -1;
hw->fd_chip = -1;
hw->fd_mux = -1;
hw->fd_panel = -1;
hw->fd_sim = -1;
sim = getenv("BANANARCHY_COVER_SPI_SIM");
if(sim && *sim) {
safe_copy(hw->sim_path, sizeof(hw->sim_path), sim);
hw->fd_sim = open(sim, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
if(hw->fd_sim >= 0)
hw->sim = 1;
else
hw->sim_path[0] = '\0';
}
}
static void read_config(const char *path, char *socket_path, size_t splen,
char *fb_path, size_t fblen, struct state *state)
char *fb_path, size_t fblen, struct state *state,
struct cover_hw *hw)
{
char line[256];
char blank[32];
@@ -434,6 +941,7 @@ static void read_config(const char *path, char *socket_path, size_t splen,
char *eq;
char *key;
char *value;
long v;
if(comment)
*comment = '\0';
eq = strchr(line, '=');
@@ -451,14 +959,60 @@ static void read_config(const char *path, char *socket_path, size_t splen,
else if(strcmp(key, "backlight_path") == 0)
safe_copy(state->backlight_path, sizeof(state->backlight_path), value);
else if(strcmp(key, "brightness") == 0) {
long v = strtol(value, NULL, 10);
v = strtol(value, NULL, 10);
if(v > 0 && v <= 255)
state->backlight_level = (int)v;
} else if(strcmp(key, "blank_timeout") == 0)
safe_copy(blank, sizeof(blank), value);
else if(strcmp(key, "backend") == 0)
safe_copy(hw->backend, sizeof(hw->backend), value);
else if(strcmp(key, "spi_device") == 0)
safe_copy(hw->spi_path, sizeof(hw->spi_path), value);
else if(strcmp(key, "gpiochip_device") == 0)
safe_copy(hw->gpio_path, sizeof(hw->gpio_path), value);
else if(strcmp(key, "panel_firmware") == 0)
safe_copy(hw->firmware, sizeof(hw->firmware), value);
else if(strcmp(key, "spi_mux_gpio") == 0) {
v = strtol(value, NULL, 10);
if(v >= 0 && v < 1024)
hw->mux_gpio = (unsigned int)v;
} else if(strcmp(key, "spi_dc_gpio") == 0) {
v = strtol(value, NULL, 10);
if(v >= 0 && v < 1024)
hw->dc_gpio = (unsigned int)v;
} else if(strcmp(key, "spi_reset_gpio") == 0) {
v = strtol(value, NULL, 10);
if(v >= 0 && v < 1024)
hw->reset_gpio = (unsigned int)v;
} else if(strcmp(key, "mux_closed_level") == 0)
hw->mux_closed_level = config_level(value, hw->mux_closed_level);
else if(strcmp(key, "mux_open_level") == 0)
hw->mux_open_level = config_level(value, hw->mux_open_level);
else if(strcmp(key, "dc_command_level") == 0)
hw->dc_command_level = config_level(value, hw->dc_command_level);
else if(strcmp(key, "dc_data_level") == 0)
hw->dc_data_level = config_level(value, hw->dc_data_level);
else if(strcmp(key, "reset_assert_level") == 0)
hw->reset_assert_level = config_level(value, hw->reset_assert_level);
else if(strcmp(key, "reset_idle_level") == 0)
hw->reset_idle_level = config_level(value, hw->reset_idle_level);
else if(strcmp(key, "spi_speed_hz") == 0) {
v = strtol(value, NULL, 10);
if(v >= 1000000 && v <= 50000000)
hw->speed_hz = (unsigned int)v;
}
}
fclose(handle);
state->blank_ms = blank_timeout_ms(blank);
if(strcmp(hw->backend, "none") == 0 || strcmp(hw->backend, "fb") == 0 ||
!hw->spi_path[0] || !hw->gpio_path[0] || !hw->firmware[0])
return;
if(!hw->sim) {
hw->fd_chip = open(hw->gpio_path, O_RDWR | O_CLOEXEC);
if(hw->fd_chip < 0)
return;
}
hw->spi_enabled = 1;
}
static void lower_string(char *value)
@@ -492,7 +1046,8 @@ static int test_value(const char *mode)
return 0;
}
static void handle_command(char *buffer, struct state *state, int *redraw)
static void handle_command(char *buffer, struct state *state,
struct cover_hw *hw, int *redraw)
{
char command[32];
char *save = NULL;
@@ -551,8 +1106,36 @@ static void handle_command(char *buffer, struct state *state, int *redraw)
} else if(strcmp(command, "SET") == 0) {
char *key = strtok_r(NULL, " \t\r\n", &save);
char *value = strtok_r(NULL, " \t\r\n", &save);
if(key && value && strcmp(key, "enabled") == 0)
if(!key || !value)
return;
if(strcmp(key, "enabled") == 0) {
state->enabled = (int)strtol(value, NULL, 10);
*redraw = 1;
return;
}
/* Runtime polarity knobs and a one-command rollback for the physical
* sign-off. Releasing forces the next draw to re-init the panel. */
if(strcmp(key, "backend") == 0) {
safe_copy(hw->backend, sizeof(hw->backend), value);
hw->spi_enabled =
strcmp(hw->backend, "none") != 0 && strcmp(hw->backend, "fb") != 0 &&
hw->spi_path[0] && hw->gpio_path[0] && hw->firmware[0];
} else if(strcmp(key, "mux_closed_level") == 0)
hw->mux_closed_level = config_level(value, hw->mux_closed_level);
else if(strcmp(key, "mux_open_level") == 0)
hw->mux_open_level = config_level(value, hw->mux_open_level);
else if(strcmp(key, "dc_command_level") == 0)
hw->dc_command_level = config_level(value, hw->dc_command_level);
else if(strcmp(key, "dc_data_level") == 0)
hw->dc_data_level = config_level(value, hw->dc_data_level);
else if(strcmp(key, "reset_assert_level") == 0)
hw->reset_assert_level = config_level(value, hw->reset_assert_level);
else if(strcmp(key, "reset_idle_level") == 0)
hw->reset_idle_level = config_level(value, hw->reset_idle_level);
else
return;
cover_hw_release(hw);
*redraw = 1;
} else if(strcmp(command, "TEST") == 0) {
char *mode = strtok_r(NULL, " \t\r\n", &save);
if(mode)
@@ -576,7 +1159,7 @@ static void handle_command(char *buffer, struct state *state, int *redraw)
}
static void reply_for(char *buffer, const struct state *state,
char *reply, size_t len)
const struct cover_hw *hw, char *reply, size_t len)
{
char command[32];
char *save = NULL;
@@ -592,10 +1175,11 @@ static void reply_for(char *buffer, const struct state *state,
} else if(strcmp(command, "STATUS") == 0) {
snprintf(reply, len,
"OK lid=%s backend=%s enabled=%d visible=%d blank_ms=%lld "
"battery=%d call=%s sms=%d network=%s\n",
"battery=%d call=%s sms=%d network=%s spi=%d mux=%s\n",
state->lid, state->backend, state->enabled, state->visible,
state->blank_ms, state->battery, state->call, state->sms,
state->network);
state->network, hw->spi_enabled,
hw->mux_claimed ? (hw->claimed ? "closed" : "open") : "free");
} else if(strcmp(command, "LID") == 0) {
snprintf(reply, len, "OK lid=%s\n", state->lid);
} else if(strcmp(command, "TEST") == 0) {
@@ -615,6 +1199,7 @@ int main(int argc, char **argv)
struct sockaddr_un local;
int server;
struct state state;
struct cover_hw hw;
long long last_draw = 0;
int redraw = 1;
if(lock_fd < 0)
@@ -629,8 +1214,9 @@ int main(int argc, char **argv)
snprintf(state.call, sizeof(state.call), "none");
state.enabled = 1;
state.battery = -1;
cover_hw_init(&hw);
read_config(config, socket_path, sizeof(socket_path),
fb_path, sizeof(fb_path), &state);
fb_path, sizeof(fb_path), &state, &hw);
mkdir_p(socket_path);
unlink(socket_path);
server = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
@@ -649,7 +1235,7 @@ int main(int argc, char **argv)
struct pollfd pfd;
char buffer[512];
char original[512];
char reply[256];
char reply[512];
struct sockaddr_un peer;
socklen_t peer_len = sizeof(peer);
ssize_t nread;
@@ -676,7 +1262,7 @@ int main(int argc, char **argv)
if(redraw) {
set_backlight(&state, cover_is_lit(&state));
snprintf(state.backend, sizeof(state.backend), "%s",
draw_frame(fb_path, &state) ? "fb" : "none");
cover_draw(&hw, fb_path, &state));
last_draw = now;
redraw = 0;
}
@@ -689,12 +1275,13 @@ int main(int argc, char **argv)
if(nread > 0) {
buffer[nread] = '\0';
snprintf(original, sizeof(original), "%s", buffer);
handle_command(buffer, &state, &redraw);
reply_for(original, &state, reply, sizeof(reply));
handle_command(buffer, &state, &hw, &redraw);
reply_for(original, &state, &hw, reply, sizeof(reply));
sendto(server, reply, strlen(reply), 0,
(struct sockaddr *)&peer, peer_len);
if(strncasecmp(original, "QUIT", 4) == 0) {
set_backlight(&state, 0);
cover_hw_shutdown(&hw);
close(server);
unlink(socket_path);
return 0;