Files
bananarchy/scripts/test-keypad-debouncer.py
T

310 lines
11 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
"""Offline verification for the system-core keypad debouncer and DT/X11 settings."""
import os
import re
import shutil
import subprocess
import sys
import tempfile
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PATCH_0014 = os.path.join(BASE_DIR, "patches/kernel/0014-input-core-keypad-debouncer.patch")
BUILD_KERNEL = os.path.join(BASE_DIR, "scripts/build-kernel-audio.sh")
SYSMAP = os.path.join(BASE_DIR, "prebuilt/kernel-6.12.1-msm8916/System.map")
POWERSAVE = os.path.join(BASE_DIR, "overlay/rootfs/etc/local.d/10-power-save.start")
X11_SESSION = os.path.join(BASE_DIR, "overlay/rootfs/usr/bin/bananarchy-x11-session.sh")
DTS_EXPECT = {
"devices/nokia-argon/qcom-msm8905-nokia-argon.dts": (0x32, 0x12, 0x19, 2),
"devices/nokia-sparkler/qcom-msm8909-nokia-sparkler.dts": (0x32, 0x12, 0x19, 1),
"devices/nokia-sparkler/qcom-msm8909-nokia-leo.dts": (0x32, 0x12, 0x19, 1),
"devices/nokia-beatles/qcom-msm8905-nokia-beatles.dts": (0x32, 0x0e, 0x14, 2),
"devices/nokia-beatles/qcom-msm8905-nokia-beatles-cover.dts": (0x32, 0x0e, 0x14, 2),
"devices/nokia-coconut/qcom-msm8905-nokia-coconut.dts": (0x32, 0x0e, 0x14, 2),
}
def read_text(path):
with open(path, "r", encoding="utf-8") as handle:
return handle.read()
def fail(msg):
sys.stderr.write(f"[fail] {msg}\n")
return 1
def extract_debouncer_code(patch_text):
lines = []
in_block = False
for raw in patch_text.splitlines():
if raw.startswith("+static unsigned int ba_key_debounce_ms"):
in_block = True
if in_block:
if not raw.startswith("+"):
break
lines.append(raw[1:])
code = "\n".join(lines) + "\n"
if "ba_filter_key_bounce" not in code or "ba_clear_key_debounce_dev" not in code:
raise AssertionError("failed to extract debouncer C implementation from 0014")
return code
def run_c_debouncer_harness(patch_text):
gcc = shutil.which("gcc")
if not gcc:
print("[skip] host gcc unavailable; skipping compiled C debouncer behavioral test")
return
extracted = extract_debouncer_code(patch_text)
harness = r"""
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int64_t s64;
typedef int64_t ktime_t;
typedef int spinlock_t;
#define DEFINE_SPINLOCK(x) spinlock_t x = 0
#define DEFINE_MUTEX(x) int x = 0
#define READ_ONCE(x) (x)
#define module_param_named(a, b, c, d)
#define MODULE_PARM_DESC(a, b)
#define NSEC_PER_MSEC 1000000LL
#define BUS_HOST 0x19
#define BUS_USB 0x03
#define BUS_BLUETOOTH 0x05
#define BUS_VIRTUAL 0x06
#define EV_SYN 0x00
#define EV_KEY 0x01
#define EV_MSC 0x04
#define MSC_SCAN 0x04
#define BTN_MISC 0x100
#define BTN_LEFT 0x110
#define KEY_OK 0x160
#define KEY_1 2
#define KEY_2 3
#define KEY_POWER 116
#define KEY_PICKUP_PHONE 0x1bd
#define KEY_NUMERIC_STAR 0x20a
#define KEY_MAX 0x2ff
static ktime_t mock_now_ns = 0;
static inline ktime_t ktime_get(void) { return mock_now_ns; }
static inline s64 ktime_to_ns(ktime_t k) { return k; }
static inline ktime_t ktime_add_ns(ktime_t k, s64 ns) { return k + ns; }
static inline s64 ktime_sub(ktime_t a, ktime_t b) { return a - b; }
static inline bool ktime_before(ktime_t a, ktime_t b) { return a < b; }
static inline bool ktime_after(ktime_t a, ktime_t b) { return a > b; }
#define spin_lock_irqsave(l, f) do { (void)(l); (f) = 0; } while (0)
#define spin_unlock_irqrestore(l, f) do { (void)(l); (void)(f); } while (0)
#define min(a, b) ((a) < (b) ? (a) : (b))
struct input_value {
uint16_t type;
uint16_t code;
int32_t value;
};
struct input_dev {
struct { uint16_t bustype; } id;
unsigned long key[(KEY_MAX + 63) / 64];
struct input_value vals[64];
unsigned int num_vals;
};
static inline bool test_bit(unsigned int nr, const unsigned long *addr) {
return (addr[nr / 64] >> (nr % 64)) & 1UL;
}
static inline void set_bit(unsigned int nr, unsigned long *addr) {
addr[nr / 64] |= (1UL << (nr % 64));
}
static inline void clear_bit(unsigned int nr, unsigned long *addr) {
addr[nr / 64] &= ~(1UL << (nr % 64));
}
""" + extracted.replace("static DEFINE_MUTEX(input_mutex);\n", "") + r"""
static int feed_key(struct input_dev *dev, int64_t ms, uint32_t scancode,
unsigned int code, int value) {
mock_now_ns = ms * NSEC_PER_MSEC;
if (dev->num_vals + 2 > 64) {
dev->num_vals = 0;
}
dev->vals[dev->num_vals++] = (struct input_value){ EV_MSC, MSC_SCAN, (int32_t)scancode };
if (ba_filter_key_bounce(dev, code, value)) {
return 0;
}
if (value) set_bit(code, dev->key);
else clear_bit(code, dev->key);
dev->vals[dev->num_vals++] = (struct input_value){ EV_KEY, (uint16_t)code, value };
return 1;
}
int main(void) {
struct input_dev kpd = {0}, pwr = {0}, bt = {0}, uinp = {0}, mouse = {0};
kpd.id.bustype = BUS_HOST;
pwr.id.bustype = BUS_HOST;
bt.id.bustype = BUS_BLUETOOTH;
uinp.id.bustype = BUS_VIRTUAL;
mouse.id.bustype = BUS_USB;
ba_key_debounce_ms = 55;
/* 1. Immediate genuine press and release pass with 0 ms delay. */
if (!feed_key(&kpd, 1000, 1, KEY_1, 1) || !feed_key(&kpd, 1080, 1, KEY_1, 0)) {
fprintf(stderr, "genuine press/release dropped\n");
return 1;
}
/* 2. Dome chatter after release (bounce Down@1105 -> bounce Up@1112) must drop and pop MSC_SCAN. */
kpd.num_vals = 0;
if (feed_key(&kpd, 1105, 1, KEY_1, 1) || kpd.num_vals != 0) {
fprintf(stderr, "post-release bounce press not suppressed or MSC_SCAN left (%u)\n", kpd.num_vals);
return 2;
}
if (feed_key(&kpd, 1112, 1, KEY_1, 0) || kpd.num_vals != 0) {
fprintf(stderr, "suppressed bounce release not suppressed or MSC_SCAN left (%u)\n", kpd.num_vals);
return 3;
}
/* 3. Multi-ring chatter still suppressed inside extended settle, then genuine tap@1210 passes. */
if (feed_key(&kpd, 1132, 1, KEY_1, 1) || feed_key(&kpd, 1138, 1, KEY_1, 0)) {
fprintf(stderr, "secondary chatter ring passed\n");
return 4;
}
if (!feed_key(&kpd, 1210, 1, KEY_1, 1) || !feed_key(&kpd, 1270, 1, KEY_1, 0)) {
fprintf(stderr, "genuine second tap after debounce window dropped\n");
return 5;
}
/* 4. Two different physical keys pressed 5 ms apart both pass immediately. */
if (!feed_key(&kpd, 2000, 1, KEY_1, 1) || !feed_key(&kpd, 2005, 2, KEY_2, 1) ||
!feed_key(&kpd, 2040, 1, KEY_1, 0) || !feed_key(&kpd, 2045, 2, KEY_2, 0)) {
fprintf(stderr, "adjacent distinct key suppressed\n");
return 6;
}
/* 4b. High Nokia phone keycodes (> KEY_OK: Call 0x1bd, * 0x20a) DO get debounced. */
if (!feed_key(&kpd, 2500, 9, KEY_NUMERIC_STAR, 1) ||
!feed_key(&kpd, 2560, 9, KEY_NUMERIC_STAR, 0) ||
feed_key(&kpd, 2580, 9, KEY_NUMERIC_STAR, 1)) {
fprintf(stderr, "KEY_NUMERIC_STAR was not debounced\n");
return 7;
}
/* 4c. Separate built-in PMIC pwrkey input_dev debounces KEY_POWER independently. */
if (!feed_key(&pwr, 2600, 0, KEY_POWER, 1) ||
!feed_key(&pwr, 2640, 0, KEY_POWER, 0) ||
feed_key(&pwr, 2660, 0, KEY_POWER, 1)) {
fprintf(stderr, "KEY_POWER chatter on pwrkey dev was not debounced\n");
return 12;
}
/* 5. BUS_BLUETOOTH, BUS_VIRTUAL, BUS_USB and BTN_LEFT bypass debouncing completely. */
if (!feed_key(&bt, 3000, 1, KEY_1, 1) || !feed_key(&bt, 3010, 1, KEY_1, 0) ||
!feed_key(&bt, 3015, 1, KEY_1, 1)) {
fprintf(stderr, "Bluetooth keyboard was unexpectedly debounced\n");
return 8;
}
if (!feed_key(&uinp, 3000, 1, KEY_POWER, 1) || !feed_key(&uinp, 3010, 1, KEY_POWER, 0) ||
!feed_key(&uinp, 3015, 1, KEY_POWER, 1)) {
fprintf(stderr, "Virtual uinput device was unexpectedly debounced\n");
return 9;
}
if (!feed_key(&mouse, 3000, 0, BTN_LEFT, 1) || !feed_key(&mouse, 3005, 0, BTN_LEFT, 0) ||
!feed_key(&mouse, 3010, 0, BTN_LEFT, 1)) {
fprintf(stderr, "Pointer button BTN_LEFT was unexpectedly debounced\n");
return 10;
}
/* 6. Unregister cleanup releases slots matching device. */
ba_clear_key_debounce_dev(&kpd);
for (int i = 0; i < BA_KEY_DEBOUNCE_SLOTS; i++) {
if (ba_key_slots[i].dev == &kpd) {
fprintf(stderr, "ba_clear_key_debounce_dev leaked slot %d\n", i);
return 11;
}
}
return 0;
}
"""
with tempfile.TemporaryDirectory(prefix="ba_debounce_") as td:
src = os.path.join(td, "test_debounce.c")
exe = os.path.join(td, "test_debounce")
with open(src, "w", encoding="utf-8") as handle:
handle.write(harness)
subprocess.run([gcc, "-Wall", "-Wextra", "-Werror", "-O2", src, "-o", exe], check=True)
subprocess.run([exe], check=True)
def main():
if not os.path.isfile(PATCH_0014):
return fail("missing patches/kernel/0014-input-core-keypad-debouncer.patch")
patch_txt = read_text(PATCH_0014)
for tok in (
"ba_filter_key_bounce",
"ba_pop_trailing_msc_scan",
"ba_clear_key_debounce_dev",
"key_debounce_ms",
"drivers/input/misc/pm8941-pwrkey.c",
"pwrkey->sw_debounce_end_time",
"45000U",
):
if tok not in patch_txt:
return fail(f"0014 patch missing {tok}")
if "a/include/linux/input.h" in patch_txt or "b/include/linux/input.h" in patch_txt:
return fail("0014 patch must not modify include/linux/input.h (module ABI invariant)")
build_txt = read_text(BUILD_KERNEL)
if "0014-input-core-keypad-debouncer.patch" not in build_txt or \
"ba_filter_key_bounce" not in build_txt:
return fail("scripts/build-kernel-audio.sh missing 0014 patch guard")
if os.path.isfile(SYSMAP):
sysmap = read_text(SYSMAP)
for sym in ("ba_key_debounce_ms", "ba_key_slots"):
if sym not in sysmap:
return fail(f"System.map missing debouncer symbol {sym}")
run_c_debouncer_harness(patch_txt)
dtc = shutil.which("dtc")
for rel, (want_scan, want_db, want_gk_db, want_gk_cnt) in DTS_EXPECT.items():
dts_path = os.path.join(BASE_DIR, rel)
txt = read_text(dts_path)
if "col-scan-delay-ms" in txt:
return fail(f"{rel} still uses unsupported col-scan-delay-ms property")
if f"col-scan-delay-us = <0x{want_scan:02x}>;" not in txt:
return fail(f"{rel} missing col-scan-delay-us = <0x{want_scan:02x}>;")
if f"debounce-delay-ms = <0x{want_db:02x}>;" not in txt:
return fail(f"{rel} missing debounce-delay-ms = <0x{want_db:02x}>;")
if txt.count(f"debounce-interval = <0x{want_gk_db:02x}>;") < want_gk_cnt:
return fail(f"{rel} missing expected gpio-keys debounce-interval = <0x{want_gk_db:02x}>;")
dtb_path = dts_path[:-4] + ".dtb"
if dtc and os.path.isfile(dtb_path):
dec = subprocess.check_output([dtc, "-q", "-I", "dtb", "-O", "dts", dtb_path], text=True)
if "col-scan-delay-ms" in dec:
return fail(f"{dtb_path} still contains col-scan-delay-ms")
if f"col-scan-delay-us = <0x{want_scan:02x}>;" not in dec:
return fail(f"{dtb_path} missing col-scan-delay-us = <0x{want_scan:02x}>;")
ps_txt = read_text(POWERSAVE)
if "/sys/module/input_core/parameters/key_debounce_ms" not in ps_txt or \
"nokia,(argon|sparkler|leo)" not in ps_txt or \
"/etc/bananarchy/key-debounce.conf" not in ps_txt:
return fail("10-power-save.start missing per-target key_debounce_ms tuning")
x11_txt = read_text(X11_SESSION)
if "xset r rate 300 25" not in x11_txt:
return fail("bananarchy-x11-session.sh must set xset r rate 300 25")
print("[ok] 0014 input_core debouncer ABI/C harness, DT scan/debounce, runtime tuning, and X11 rate passed")
return 0
if __name__ == "__main__":
sys.exit(main())