From 9635b914e2ccb5b432defba00f4677ca31f44846 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Tue, 17 Nov 2026 10:47:56 +0000 Subject: [PATCH 1/3] HID: apple-mtp-kiwi: a hold drop keeps the whole lit blob dead Why: the suppression map was a box on the centroid. On the original always-lit capture the centroid sits on the blob's corner, so the far edge stays unmarked and re-arms one frame after the drop (borns 5985 and 5995). The drop itself is correct: the first contact ends at born+5399. What: the drop marks that box and then every still-lit cell that touches it, out to 6 cells. The region stays dead until 240 consecutive frames below the release level. A short dip does not clear it, and the single-cell guard does not clear while the map is holding. A real lift still frees the cells. Proof: holdrebirth, the original 8000-frame always-lit capture, is red at 3c5c18e0 (4 contacts, borns 583, 594, 5985, 5995) and green here (exactly 583 and 594). breatheretap stays 2 contacts, both bounded. lifteretap is a lift, a quiet run, and a new touch on the same cells (borns 189 and 1074). A reach-0 mutant of the grow goes red again. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> (cherry picked from commit e26025adb80fffb7c3b0f5ca15f12bd65e3fd357) --- .../hid/apple-mtp-kiwi-replay/kiwi-synth.py | 216 +++++ drivers/hid/apple-mtp-kiwi-replay/replay.c | 330 +++++++ .../apple-mtp-kiwi-replay/run-kiwi-replay.sh | 87 ++ drivers/hid/hid-apple-mtp-kiwi.c | 810 ++++++++++++++++++ 4 files changed, 1443 insertions(+) create mode 100755 drivers/hid/apple-mtp-kiwi-replay/kiwi-synth.py create mode 100644 drivers/hid/apple-mtp-kiwi-replay/replay.c create mode 100755 drivers/hid/apple-mtp-kiwi-replay/run-kiwi-replay.sh create mode 100644 drivers/hid/hid-apple-mtp-kiwi.c diff --git a/drivers/hid/apple-mtp-kiwi-replay/kiwi-synth.py b/drivers/hid/apple-mtp-kiwi-replay/kiwi-synth.py new file mode 100755 index 0000000000000..16ae29655a140 --- /dev/null +++ b/drivers/hid/apple-mtp-kiwi-replay/kiwi-synth.py @@ -0,0 +1,216 @@ +#!/usr/bin/env python3 +"""Synthetic kiwi captures for the replay harness, same line format as the real ones. + +kiwi-synth.py OUT.frames MODE + idle : three 2 s cycles (720 frames, 120 Hz), references +658 +902 -445 -126 with + per-row lean and noisier top rows. Expect no contact ever. + stuckedge : idle plus cells (52..53, 0..1) held 400 counts low from frame 200 on, + mimicking the 16:12 phantom: baseline first, then the top edge sticks. + stuckmid : cells (25..27, 40..42) held 400 counts low from frame 200 on: the hold + rule case away from the edge ring. + wander : a breathing blob (plus or minus 200 counts) that hops one cell every + 90 frames from frame 200 on: lively and moving, so only the 45 s + hold rule can judge it. + flat2s : a perfectly flat 400-count blob at the centre for 2 s from frame 200: + the M5 menu-open phantom shape (static, no ramp, no breathing). + tap : idle plus a wobbling fingertip near the panel centre for 24 frames. + tapedge : same tap but near the top edge, so the edge guard is exercised by a + finger-like blob that must STILL be reported after confirmation. + presshold : a real 39 s breathing touch, a lift, then a genuine new press at the + same cell 60 frames later held to the end: the new press must inherit + nothing and stay reported. + bootfinger: a flat 5x5 blob 400 counts low at the centre from frame 0 through + frame 400: a finger already resting on the glass at input open. + The baseline must not absorb it: the blob must be reported. + bootfinger2 : the same boot finger, a lift at 400, 200 idle frames, then a tap + at the same spot (600) and a tap on a different row (648). + bootfinger3 : the same but the second tap is on the SAME rows as the boot + finger: measures whether the absorbed bake scars those rows. + breathelong : a stationary tent blob ramping to 400 counts over 600 frames + (the breathing starts after the ramp: +-39 sine, period 1 s) + at the centre to the end of an 8000-frame run: + lively by breathing, zero drift, so only the hold rule ends it. + The suite runs it with --expect bound: the hold rule must drop + it at about frame born+5400 and keep every contact bounded. + breatheretap : the breathelong blob, lifted at 6600 for 2.5 s, then the + same finger taps again at 6900: after a real lift the map must + free the cells so the tap registers (2 contacts, both bounded). + bootfinger4 : the same boot finger and lift, then a stuckmid-style flat + 400-count blob on the SAME rows from frame 500: at the absorbed + bake the blob cancels the scar and is never seen (shallow or + equal-depth touches on those rows are invisible all session). +""" +import math +import sys +import random + +HDR, COLS, ROWS = 61, 82, 54 +CELLS = COLS * ROWS +FRAME = 61 + 2 * CELLS + 8944 # 17861, like the real reports + +path, mode = sys.argv[1], sys.argv[2] +rng = random.Random(11) +nframes = 8000 if mode in ("breathelong", "holdrebirth") else 7200 if mode in ("presshold", "breatheretap") else 1400 if mode == "lifteretap" else 720 +refs = [658, 902, -445, -126] # whole-image shifts, one per 0.5 s +idle = [[2500 + rng.randint(-30, 30) for _ in range(COLS)] for _ in range(ROWS)] +for r in range(ROWS - 4, ROWS): # top rows noisier + for c in range(COLS): + idle[r][c] = 2500 + rng.randint(-60, 60) + + +def shift_for(f, r): + if f % 60: # the real chip sends one reference frame, then 59 ordinary ones + return 0 + k = (f // 60) % 4 + base = refs[k] + row_lean = (r - 26) * (2 if base > 0 else -2) + if r >= ROWS - 4: # top-edge rows move hundreds more on reference frames + row_lean += (300 if base > 0 else -300) + return base + row_lean + + +def cell_frame(f): + drift = 3 * f // (130 * 120) + img = [[idle[r][c] + rng.randint(-5, 5) + drift + shift_for(f, r) for c in range(COLS)] + for r in range(ROWS)] + if mode in ("stuckedge",) and f >= 200: + for r in (52, 53): + for c in (0, 1): + img[r][c] -= 400 + if mode in ("stuckmid",) and f >= 200: + for r in (25, 26, 27): + for c in (40, 41, 42): + img[r][c] -= 400 + if mode in ("tap", "tapedge") and 100 <= f < 124: + cr, cc = (4, 2) if mode == "tapedge" else (26, 41) + jr, jc = rng.randint(-1, 1), rng.randint(-1, 1) + ramp = min(f - 100, 123 - f, 5) * 160 + breath = rng.randint(-70, 70) + for r in range(cr - 2, cr + 3): + for c in range(cc - 2, cc + 3): + if 0 <= r < ROWS and 0 <= c < COLS: + d = max(abs(r - cr), abs(c - cc)) + img[r][c] -= (1000 if d == 0 else 600) * ramp // 800 + img[r][c] -= 10 * abs(jr) + 10 * abs(jc) + breath + if mode == "wander" and f >= 200: + cr, cc = 26 + (f // 90) % 2, 41 + ((f // 90) // 2) % 2 + breath = rng.randint(-200, 200) + for r in (cr - 1, cr, cr + 1): + for c in (cc - 1, cc, cc + 1): + if 0 <= r < ROWS and 0 <= c < COLS: + d = max(abs(r - cr), abs(c - cc)) + img[r][c] -= 400 if d == 0 else 250 + img[r][c] -= breath // 3 + if mode == "presshold" and 200 <= f < 4900: + cr, cc = 26, 41 + breath = rng.randint(-200, 200) + for r in (cr - 1, cr, cr + 1): + for c in (cc - 1, cc, cc + 1): + if 0 <= r < ROWS and 0 <= c < COLS: + d = max(abs(r - cr), abs(c - cc)) + img[r][c] -= 400 if d == 0 else 250 + img[r][c] -= breath // 3 + if mode == "presshold" and f >= 4960: + cr, cc = 26, 41 + jr, jc = rng.randint(-1, 1), rng.randint(-1, 1) + breath = rng.randint(-200, 200) + for r in range(cr - 2, cr + 3): + for c in range(cc - 2, cc + 3): + if 0 <= r < ROWS and 0 <= c < COLS: + d = max(abs(r - cr), abs(c - cc)) + img[r][c] -= (1000 if d == 0 else 600) if d <= 2 else 0 + img[r][c] -= 10 * abs(jr) + 10 * abs(jc) + breath + if mode == "flat2s" and 200 <= f < 440: + for r in (25, 26, 27): + for c in (40, 41, 42): + img[r][c] -= 400 + if mode == "holdrebirth" and f >= 600: + # the original always-lit capture: a flat 5x5 from frame 600, breathing + # +-80. At the parent this is 4 contacts; the two after the drop must die. + base = 400 + int(80 * math.sin(2 * math.pi * f / 120)) + for r in range(24, 29): + for c in range(39, 44): + img[r][c] -= base + if mode == "lifteretap": + # a finger, a real lift, quiet past the lift window, then the same + # cells again. The second touch must register. + if 200 <= f < 800 or 1100 <= f < 1180: + base = 400 + int(39 * math.sin(2 * math.pi * f / 120)) + for r in range(24, 29): + for c in range(39, 44): + img[r][c] -= base * (3 - abs(r - 26)) * (3 - abs(c - 41)) // 9 + if mode == "breathelong" and f >= 1: + base = 600 + int(39 * math.sin(2 * math.pi * f / 120)) + for r in range(24, 29): + for c in range(39, 44): + img[r][c] -= base * (3 - abs(r - 26)) * (3 - abs(c - 41)) // 9 + if mode == "breatheretap" and f >= 1 and not (6600 <= f < 6900): + ramp = min(f, 600) + base = ramp * 400 // 600 + (int(39 * math.sin(2 * math.pi * f / 120)) if f > 600 else 0) + for r in range(24, 29): + for c in range(39, 44): + img[r][c] -= base * (3 - abs(r - 26)) * (3 - abs(c - 41)) // 9 + if mode == "bootfinger" and f < 400: + for r in range(24, 29): + for c in range(39, 44): + img[r][c] -= 400 + if mode == "bootfinger4" and f < 400: + for r in range(24, 29): + for c in range(39, 44): + img[r][c] -= 400 + if mode == "bootfinger4" and f >= 500: + for r in range(24, 29): + for c in range(39, 44): + img[r][c] -= 400 + if mode in ("bootfinger2", "bootfinger3") and f < 400: + for r in range(24, 29): + for c in range(39, 44): + img[r][c] -= 400 + if mode in ("bootfinger2", "bootfinger3") and 600 <= f < 624: + jr, jc = rng.randint(-1, 1), rng.randint(-1, 1) + ramp = min(f - 600, 623 - f, 5) * 160 + breath = rng.randint(-70, 70) + for r in range(24, 29): + for c in range(39, 44): + d = max(abs(r - 26), abs(c - 41)) + img[r][c] -= (1000 if d == 0 else 600) * ramp // 800 + img[r][c] -= 10 * abs(jr) + 10 * abs(jc) + breath + if mode == "bootfinger2" and 648 <= f < 672: + jr, jc = rng.randint(-1, 1), rng.randint(-1, 1) + ramp = min(f - 648, 671 - f, 5) * 160 + breath = rng.randint(-70, 70) + for r in range(10, 15): + for c in range(60, 65): + d = max(abs(r - 12), abs(c - 62)) + img[r][c] -= (1000 if d == 0 else 600) * ramp // 800 + img[r][c] -= 10 * abs(jr) + 10 * abs(jc) + breath + if mode == "bootfinger3" and 648 <= f < 672: + jr, jc = rng.randint(-1, 1), rng.randint(-1, 1) + ramp = min(f - 648, 671 - f, 5) * 160 + breath = rng.randint(-70, 70) + for r in range(24, 29): + for c in range(60, 65): + d = max(abs(r - 26), abs(c - 62)) + img[r][c] -= (1000 if d == 0 else 600) * ramp // 800 + img[r][c] -= 10 * abs(jr) + 10 * abs(jc) + breath + return img + + +with open(path, "w") as fh: + for f in range(nframes): + raw = bytearray(FRAME) + raw[0:7] = bytes([0x5A, 0x01, 0x02, 0x00, 0x20, 0x18, 0x00]) + raw[7:11] = (f * 8333).to_bytes(4, "little") + raw[15:17] = (f & 0xFFFF).to_bytes(2, "little") + raw[23] = f & 0xFF + raw[24] = (f >> 8) & 0xFF + img = cell_frame(f) + i = HDR + for r in range(ROWS): + for c in range(COLS): + v = max(0, min(65535, img[r][c])) + raw[i] = v & 0xFF + raw[i + 1] = v >> 8 + i += 2 + fh.write(f"{f / 120.0:.4f} len={FRAME} dev=0 {raw.hex()}\n") +print(path, mode, nframes) diff --git a/drivers/hid/apple-mtp-kiwi-replay/replay.c b/drivers/hid/apple-mtp-kiwi-replay/replay.c new file mode 100644 index 0000000000000..31365113daae0 --- /dev/null +++ b/drivers/hid/apple-mtp-kiwi-replay/replay.c @@ -0,0 +1,330 @@ +/* kiwi-replay.c : replay the real hid-apple-mtp-kiwi.c frame logic over every + * capture and start phase, and fail on any reported contact with no finger present. + * + * Build (on mesa-xbuild): + * cc -O2 -I -include linux-shim.h -DKIWI_DRV='"/path/driver.c"' replay.c -o kiwi-replay + * Run: + * kiwi-replay --expect none cap.frames [...] fail on ANY contact + * kiwi-replay --expect touch cap.frames fail on never-moving or over-hold contacts + * kiwi-replay --expect bound cap.frames every contact must end near the hold bound + * kiwi-replay --expect one cap.frames exactly one contact, bounded (the breather) + * --phases N (default 240), --loop N (replay N times back to back) + */ +#include "linux-shim.h" +#define HARNESS_STATIC_FRAMES 120 +#define HARNESS_STATIC_GRACE 60 +#include KIWI_DRV + +struct kiwi_contact_rec { + int slot, x0, y0, x1, y1; + long frames, born; + bool moved_ever; +}; + +struct kiwi_input_rec kiwi_rec; + +/* per-cell-cluster total contact time: a phantom may exist only inside the + * grace window; more than that in one spot means it persists */ +#define CLUSTER(px, py) ((((px) / 80) << 16) | ((py) / 80)) + +static int rebuild(struct kiwi_contact_rec *out, int maxout) +{ + struct { bool open; struct kiwi_contact_rec c; } st[4] = {0}; + int n = 0, i; + for (i = 0; i < kiwi_rec.n; i++) { + struct kiwi_rec_event *e = &kiwi_rec.ev[i]; + if (e->slot < 0 || e->slot >= 4) + continue; + if (!e->on) { + if (st[e->slot].open && n < maxout) + out[n++] = st[e->slot].c; + st[e->slot].open = false; + continue; + } + if (e->x >= 0 || e->y >= 0 || e->size >= 0) { + struct kiwi_contact_rec *c = &st[e->slot].c; + if (!st[e->slot].open) { + st[e->slot].open = true; + c->slot = e->slot; + c->x0 = c->x1 = 1 << 30; /* position arrives with the first ABS event */ + c->y0 = c->y1 = 1 << 30; + c->frames = 0; + c->born = e->frameno; + c->moved_ever = false; + } else if (c->x1 == 1 << 30) { + c->x0 = c->x1 = e->x >= 0 ? e->x : c->x1; + c->y0 = c->y1 = e->y >= 0 ? e->y : c->y1; + } else { + if (e->x >= 0) + c->frames++; /* one X per report: count frames, not events */ + if (e->x >= 0 && (e->x != c->x1 || e->y != c->y1)) + c->moved_ever = true; + if (e->x >= 0) c->x1 = e->x; + if (e->y >= 0) c->y1 = e->y; + } + } + } + for (i = 0; i < 4; i++) /* contacts still down at capture end count too */ + if (st[i].open && n < maxout) + out[n++] = st[i].c; + return n; +} + +static long cluster_time(struct kiwi_contact_rec *out, int n) +{ + long t[4096] = {0}; + long worst = 0; + int i; + + for (i = 0; i < n; i++) { + unsigned b = CLUSTER(out[i].x0, out[i].y0) & 4095; + t[b] += out[i].frames; + if (t[b] > worst) + worst = t[b]; + } + return worst; +} + +static int parse_capture(const char *path, u8 **frames, int maxframes) +{ + FILE *f = fopen(path, "r"); + char *line = NULL; + size_t cap = 0; + ssize_t llen; + int n = 0; + if (!f) { perror(path); exit(2); } + while ((llen = getline(&line, &cap, f)) > 0 && n < maxframes) { + char *hex = strrchr(line, ' '); + int blen = 0, i; + u8 *raw; + if (!hex || !hex[1]) + continue; + blen = (int)((llen - (hex + 1 - line)) / 2); + if (blen < KIWI_MIN_FRAME) + continue; + raw = malloc(KIWI_MIN_FRAME); + for (i = 0; i < KIWI_MIN_FRAME; i++) { + unsigned b; + if (sscanf(hex + 1 + 2 * i, "%2x", &b) != 1) { blen = 0; break; } + raw[i] = (u8)b; + } + if (blen && raw[0] == KIWI_MAGIC) + frames[n++] = raw; + else + free(raw); + } + free(line); + fclose(f); + return n; +} + +static void run_phase(u8 **frames, int n, int phase, int loops) +{ + struct kiwi k; + struct input_dev input; + int i, l; + memset(&k, 0, sizeof(k)); + memset(&kiwi_rec, 0, sizeof(kiwi_rec)); /* slot_on starts all-off */ + k.input = &input; + for (l = 0; l < loops; l++) + for (i = 0; i < n; i++) + kiwi_frame(&k, frames[(phase + i) % n]); +} + +int main(int argc, char **argv) +{ + const char *expect = NULL, *taps_spec = NULL; + int tap_min = 15; + int tap_w[8], ntap_w = 0; + int phases = 240, loops = 1, argi; + static u8 *frames[60000]; + int fails = 0; + + for (argi = 1; argi < argc; argi++) { + if (!strcmp(argv[argi], "--expect")) expect = argv[++argi]; + else if (!strcmp(argv[argi], "--phases")) phases = atoi(argv[++argi]); + else if (!strcmp(argv[argi], "--loop")) loops = atoi(argv[++argi]); + else if (!strcmp(argv[argi], "--taps")) taps_spec = argv[++argi]; + else if (!strcmp(argv[argi], "--tap-min")) tap_min = atoi(argv[++argi]); + else break; + } + for (; argi < argc; argi++) { + const char *path = argv[argi]; + int n = parse_capture(path, frames, 60000); + int p, contacts_total = 0; + bool press_sustained = false; + bool press_churn = false; + if (!n) { printf("cap=%s SKIP no frames\n", path); continue; } + if (phases > n) phases = n; + for (p = 0; p < phases; p++) { + static struct kiwi_contact_rec out[256]; + int m, ci; + run_phase(frames, n, p, loops); + m = rebuild(out, 256); + contacts_total += m; + if (expect && !strcmp(expect, "press")) + for (ci = 0; ci < m; ci++) { + if (out[ci].born >= (long)n * 2 / 3 && out[ci].frames >= 600 && + out[ci].born + out[ci].frames >= n - 240) + press_sustained = true; + /* churn: a late contact that died well before the end means the + * press was dropped and re-armed instead of held */ + if (out[ci].born >= (long)n * 2 / 3 && + out[ci].born + out[ci].frames < n - 600) + press_churn = true; + } + + /* taps contract: each named window must hold a full-length contact and + * the idle stretch before the first tap must stay silent. Used by the + * bootfinger captures: a finger rested on the glass at boot must not + * scar the rows its later taps land on. */ + if (expect && !strcmp(expect, "taps") && taps_spec) { + const char *p2 = taps_spec; + int twi = 0; + + while (*p2 && twi < 8) { + tap_w[twi++] = atoi(p2); + while (*p2 && *p2 != ':' && *p2 != ',') + p2++; + if (*p2 == ':') { + tap_w[twi++] = atoi(p2 + 1); + while (*p2 && *p2 != ',') + p2++; + } else { + tap_w[twi] = tap_w[twi - 1] + 24; + twi++; + } + if (*p2 == ',') + p2++; + } + ntap_w = twi / 2; + /* the first pair names the silent idle stretch after the lift; + * a contact that reaches into a tap window is that tap's lead */ + for (ci = 0; ci < m; ci++) { + bool tap_lead = false; + int pair; + + if (out[ci].born < tap_w[0] || out[ci].born >= tap_w[1]) + continue; + for (pair = 1; pair < ntap_w; pair++) + if (out[ci].born + out[ci].frames >= tap_w[pair * 2] && + out[ci].born <= tap_w[pair * 2 + 1] + 40) + tap_lead = true; + if (!tap_lead) { + fails++; + fails++; + fprintf(stderr, "cap=%s FAIL ghost-in-idle (contact born %ld inside the silent window)\n", + path, out[ci].born); + break; + } + } + for (twi = 1; twi < ntap_w; twi++) { + int a = tap_w[twi * 2], b = tap_w[twi * 2 + 1]; + bool full = false; + + for (ci = 0; ci < m; ci++) + if (out[ci].frames >= tap_min && + out[ci].born <= b + 40 && out[ci].born + out[ci].frames >= a) + full = true; + if (!full) { + fails++; + fprintf(stderr, "cap=%s FAIL tap-%d-short (window %d..%d below %d frames)\n", + path, twi, a, b, tap_min); + } + } + } + if (expect && (strcmp(expect, "press") == 0 || strcmp(expect, "touch") == 0) && + getenv("PRESS_TRACE")) + for (ci = 0; ci < m; ci++) + fprintf(stderr, "press-trace born=%ld frames=%ld end=%ld\n", + out[ci].born, out[ci].frames, out[ci].born + out[ci].frames); + { + long worst = cluster_time(out, m); + if (expect && !strcmp(expect, "none") && worst > HARNESS_HOLD_FRAMES * 3 / 2) { + fails++; + fprintf(stderr, "cap=%s phase=%d FAIL phantom-persists cluster_frames=%ld\n", + path, p, worst); + } + } + /* "reborn:,,...": the contact list must be exactly the + * named borns, in order - nothing more (a rebirth after the + * hold drop), nothing fewer (the touch must register). */ + if (expect && !strncmp(expect, "reborn:", 7)) { + long want[8]; + int nw = 0, ni; + const char *q = expect + 7; + + while (*q && nw < 8) { + want[nw++] = strtol(q, (char **)&q, 10); + if (*q == ',') + q++; + } + { + bool bad = m != nw; + + for (ni = 0; ni < m && ni < nw; ni++) + if (out[ni].born != want[ni]) + bad = true; + if (bad) + fails++; + if (bad) + fprintf(stderr, + "cap=%s phase=%d FAIL reborn-list want=%s got=%u borns=%ld,%ld,%ld,%ld\n", + path, p, expect + 7, m, m > 0 ? out[0].born : -1, + m > 1 ? out[1].born : -1, m > 2 ? out[2].born : -1, + m > 3 ? out[3].born : -1); + } + } + /* "one": the stationary breather must produce exactly one + * bounded contact: the hold rule ends it and the sticky + * suppression keeps the still-present blob from re-arming. */ + if (expect && !strcmp(expect, "one") && m != 1) { + fails++; + fprintf(stderr, "cap=%s phase=%d FAIL expected-one-contact got=%u\n", + path, p, m); + } + for (ci = 0; ci < m; ci++) { + struct kiwi_contact_rec *c = &out[ci]; + /* "bound"/"one": every contact must end near the hold bound. + * The drop lands a few frames past born+KIWI_HOLD_FRAMES (the + * anchor lags; measured 12 on the breather), so the check + * allows 200 frames of slack. */ + bool bnd = expect && (!strcmp(expect, "bound") || + !strcmp(expect, "one")); + bool held = c->frames > (bnd ? HARNESS_HOLD_FRAMES + 200 : + HARNESS_HOLD_FRAMES); + bool statik = expect && !strcmp(expect, "flat") && + c->frames > HARNESS_STATIC_FRAMES + HARNESS_STATIC_GRACE; + bool strict = expect && !strcmp(expect, "strict"); + bool pressmode = expect && !strcmp(expect, "press"); + bool fail = pressmode ? false : bnd ? held : held || statik || strict; + if (getenv("KIWI_DUMP")) + fprintf(stderr, "cap=%s phase=%d contact slot=%d (%d,%d)..(%d,%d) frames=%ld born=%ld\n", + path, p, c->slot, c->x0, c->y0, c->x1, c->y1, c->frames, c->born); + if (fail) { + fails++; + if (fails <= 40) + fprintf(stderr, "cap=%s phase=%d FAIL %s slot=%d (%d,%d)..(%d,%d) frames=%ld born=%ld\n", + path, p, strict ? "phantom" : held ? "held-too-long" : "static-phantom", + c->slot, c->x0, c->y0, c->x1, c->y1, c->frames, c->born); + } + } + } + if (kiwi_rec.dropped) { + fails++; + fprintf(stderr, "cap=%s FAIL evidence-incomplete (recorder buffer exhausted)\n", path); + } + if (expect && !strcmp(expect, "press") && (!press_sustained || press_churn)) { + fails++; + fprintf(stderr, "cap=%s FAIL press-not-sustained (no contact born past two thirds of the capture lives 5 s)\n", path); + } + if (expect && !strcmp(expect, "touch") && contacts_total == 0) { + fails++; + fprintf(stderr, "cap=%s FAIL finger-never-detected\n", path); + } + printf("cap=%s frames=%d phases=%d contacts=%d %s\n", path, n, phases, + contacts_total, fails ? "FAIL" : "PASS"); + while (n--) free(frames[n]); + } + return fails ? 1 : 0; +} diff --git a/drivers/hid/apple-mtp-kiwi-replay/run-kiwi-replay.sh b/drivers/hid/apple-mtp-kiwi-replay/run-kiwi-replay.sh new file mode 100755 index 0000000000000..537c54c076bf2 --- /dev/null +++ b/drivers/hid/apple-mtp-kiwi-replay/run-kiwi-replay.sh @@ -0,0 +1,87 @@ +#!/usr/bin/env bash +# kiwi replay harness: build and run against a given driver file. +# usage: run-kiwi-replay.sh /path/to/hid-apple-mtp-kiwi.c [OUTDIR] +# works from the committed tree dir (drivers/hid/apple-mtp-kiwi-replay) and +# from the scratch layout (kiwi/replay with a sibling shim/); real captures +# come from $CAPS when that directory exists, else only the synthetics run. +set -e +DRV=$1 +OUT=${2:-/tmp/kiwi-replay} +H="$(cd "$(dirname "$0")" && pwd)" +if [ -f "$H/replay.c" ]; then + R=$H +else + R=$H/replay +fi +if [ -f "$H/linux-shim.h" ]; then + SHIM=$H +elif [ -f "$H/shim/linux-shim.h" ]; then + SHIM=$H/shim +elif [ -f "$H/../shim/linux-shim.h" ]; then + SHIM=$H/../shim +else + echo "cannot locate replay.c under $H" >&2 + exit 2 +fi +CAPS=${CAPS:-$H/../kiwi-caps} +[ -d "$CAPS" ] || CAPS=$H/../../kiwi-caps +mkdir -p "$OUT" +python3 "$R/kiwi-synth.py" "$OUT/stuckedge.frames" stuckedge >/dev/null +python3 "$R/kiwi-synth.py" "$OUT/idlecycle.frames" idle >/dev/null +python3 "$R/kiwi-synth.py" "$OUT/tap.frames" tap >/dev/null +python3 "$R/kiwi-synth.py" "$OUT/tapedge.frames" tapedge >/dev/null +python3 "$R/kiwi-synth.py" "$OUT/stuckmid.frames" stuckmid >/dev/null +python3 "$R/kiwi-synth.py" "$OUT/flat2s.frames" flat2s >/dev/null +python3 "$R/kiwi-synth.py" "$OUT/bootfinger.frames" bootfinger >/dev/null +python3 "$R/kiwi-synth.py" "$OUT/breathelong.frames" breathelong >/dev/null +python3 "$R/kiwi-synth.py" "$OUT/breatheretap.frames" breatheretap >/dev/null +python3 "$R/kiwi-synth.py" "$OUT/holdrebirth.frames" holdrebirth >/dev/null +python3 "$R/kiwi-synth.py" "$OUT/lifteretap.frames" lifteretap >/dev/null +python3 "$R/kiwi-synth.py" "$OUT/bootfinger2.frames" bootfinger2 >/dev/null +python3 "$R/kiwi-synth.py" "$OUT/bootfinger4.frames" bootfinger4 >/dev/null +python3 "$R/kiwi-synth.py" "$OUT/bootfinger3.frames" bootfinger3 >/dev/null +python3 "$R/kiwi-synth.py" "$OUT/wander.frames" wander >/dev/null +python3 "$R/kiwi-synth.py" "$OUT/presshold.frames" presshold >/dev/null +cc -O2 -I"$SHIM" -include linux-shim.h -DKIWI_DRV="\"$DRV\"" "$R/replay.c" -o "$OUT/kiwi-replay" +echo "== driver: $DRV" +if [ -d "$CAPS" ]; then + echo "== real captures (regression: no phantom ever; the real finger stays detected)" + for cap in "$CAPS"/kiwi-full.frames "$CAPS"/kiwi-watch3.frames; do + "$OUT/kiwi-replay" --expect strict --phases 240 "$cap" || true + done + "$OUT/kiwi-replay" --expect touch --phases 240 "$CAPS"/kiwi-watch2-final.frames || true +else + echo "== real captures skipped (no $CAPS)" +fi +echo "== synthetic idle (must report nothing)" +"$OUT/kiwi-replay" --expect none --phases 60 --loop 40 "$OUT/idlecycle.frames" || true +echo "== synthetic stuck top-edge cells (the 16:12 phantom: RED until hardened)" +"$OUT/kiwi-replay" --expect strict --phases 60 --loop 40 "$OUT/stuckedge.frames" || true +echo "== synthetic stuck centre cells away from the edge (hold rule: RED until hardened)" +"$OUT/kiwi-replay" --expect none --phases 60 --loop 40 "$OUT/stuckmid.frames" || true +echo "== synthetic finger on the glass at boot (must be reported, not learned into the baseline)" +"$OUT/kiwi-replay" --expect touch --phases 1 --loop 1 "$OUT/bootfinger.frames" || true +echo "== synthetic finger on the glass at boot, lift, idle, then two taps (lifted finger leaves no ghost)" +"$OUT/kiwi-replay" --expect touch --phases 1 --loop 1 "$OUT/bootfinger2.frames" || true +echo "== synthetic flat blob on the boot-finger rows after the lift (the scar test: it must be seen)" +"$OUT/kiwi-replay" --expect touch --phases 1 --loop 1 "$OUT/bootfinger4.frames" || true +echo "== synthetic taps after a boot finger on the same rows (no scar: full-length taps, silent idle)" +"$OUT/kiwi-replay" --expect taps --taps 410:590,600:624,648:672 --tap-min 15 --phases 1 --loop 1 "$OUT/bootfinger3.frames" || true +echo "== synthetic stationary breather (the hold rule must drop it exactly once; a still-present blob must not re-arm)" +"$OUT/kiwi-replay" --expect one --phases 1 --loop 1 "$OUT/breathelong.frames" || true +echo "== synthetic always-lit breather (hold drops exactly 583 and 594; zero births after the drop)" +"$OUT/kiwi-replay" --expect reborn:583,594 --phases 1 --loop 1 "$OUT/holdrebirth.frames" || true +echo "== synthetic lift, full quiet, same-spot touch again (the lift must free the cells)" +"$OUT/kiwi-replay" --expect reborn:189,1074 --phases 1 --loop 1 "$OUT/lifteretap.frames" || true +echo "== synthetic breather that lifts and taps again (after a real lift the tap must register)" +"$OUT/kiwi-replay" --expect bound --phases 1 --loop 1 "$OUT/breatheretap.frames" || true +echo "== synthetic wandering breathing blob (a live touch by contract: it must stay reported)" +"$OUT/kiwi-replay" --expect touch --phases 60 --loop 40 "$OUT/wander.frames" || true +echo "== synthetic flat 2 s centre blob (the menu-open shape: RED until the static rule)" +"$OUT/kiwi-replay" --expect flat --phases 60 --loop 40 "$OUT/flat2s.frames" || true +echo "== synthetic press after a long touch (a fresh press must inherit nothing: RED until the carry gap check)" +"$OUT/kiwi-replay" --expect press --phases 1 --loop 1 "$OUT/presshold.frames" || true +echo "== synthetic finger taps (must stay detected and not over-hold)" +"$OUT/kiwi-replay" --expect touch --phases 60 --loop 5 "$OUT/tap.frames" || true +"$OUT/kiwi-replay" --expect touch --phases 60 --loop 5 "$OUT/tapedge.frames" || true +echo "== done (exit reflects failures; each block above is judged by its own PASS/FAIL lines)" diff --git a/drivers/hid/hid-apple-mtp-kiwi.c b/drivers/hid/hid-apple-mtp-kiwi.c new file mode 100644 index 0000000000000..58100d263c9ba --- /dev/null +++ b/drivers/hid/hid-apple-mtp-kiwi.c @@ -0,0 +1,810 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Apple MTP touch-screen (kiwi) interface. + * + * The kiwi interface of the dockchannel MTP carries its touches as a raw + * report pipe. No other HID driver may claim it: the Apple wildcard entries of + * hid-apple and hid-magicmouse make hid-generic stand off from every + * BUS_HOST Apple device, and both reject the interface in probe. This driver + * claims it by name, keeps the pipe on hidraw, and registers a direct + * multi-touch input device. The interface is started (firmware, reset) when + * the first user opens hidraw or the input device. + * + * The 29-byte descriptor declares one opaque Digitizer/TouchScreen input blob + * (16384 bits), so the finger layout is not described by HID. Observed layout + * of the 17861-byte report (see the pull request description): + * + * 0..60 header. Byte 0 is 0x5a, bytes 1..6 are 01 02 00 20 18 00, + * bytes 7..10 are a u32 LE microsecond timer (8333 us per frame) + * 61..8916 54 rows x 82 columns of u16 LE raw capacitance, row major, + * row 0 at the bottom edge of the panel, + * idle level about 2500 (noise about 25 to 50 counts). A finger + * lowers the cells under it by 600..1300 counts. One cell is + * about 36.9 px on the 3024x1986 panel in both directions. + * 8917.. further sections that are not decoded yet + * + * Every 2 s four whole-image shifts arrive (about +658, +902, -445 and -126 + * counts), each between two ordinary frames. A few cells on the top edge + * (rows 52 and 53, columns 0, 41 and 81) move by 260 to 880 counts more than + * the rest in each of them. Frames shifted by more than 64 counts are skipped + * and a per-row mean is removed from the rest. The raw image has no + * baseline, so the driver keeps one per cell, picks local maxima of + * (baseline - raw), and centroids 3x3. + */ + +#include +#include +#include +#include +#include +#include + +#include "hid-ids.h" + +#define KIWI_HDR_LEN 61 +#define KIWI_COLS 82 +#define KIWI_ROWS 54 +#define KIWI_CELLS (KIWI_COLS * KIWI_ROWS) +#define KIWI_MIN_FRAME (KIWI_HDR_LEN + 2 * KIWI_CELLS) +#define KIWI_MAGIC 0x5a +#define KIWI_MAX_SLOTS 10 +/* eDP-1 panel of the k114c, 3024x1986. */ +#define KIWI_MAX_X 3023 +#define KIWI_MAX_Y 1985 +/* Panel is about 10 units per mm. */ +#define KIWI_RES 10 +#define KIWI_FP 256 +#define KIWI_GLITCH 64 /* counts: reference frames shift the mean by 137 to 899, idle frames by under 10 */ +#define KIWI_SAME 200 /* counts: two ordinary frames differ less than this in every cell */ +#define KIWI_GLITCH_MAX 10 /* skipped frames in a row before the baseline is retaken */ +#define KIWI_MISS_MAX 2 +#define KIWI_GATE_RING 3 /* the static periphery outliers all live inside this ring */ +#define KIWI_MATCH_D2 (4 * KIWI_FP * 4 * KIWI_FP) + +#define KIWI_THRESHOLD 250 /* counts below baseline that make a touch */ +#define KIWI_HOLD_FRAMES 5400 /* 45 s at 120 Hz: a static contact past this is a phantom */ +#define KIWI_SUP_REACH 6 /* shortcut: 6 cells from the drop, raise if a wider blob re-arms past the mark */ +#define KIWI_LIFT_FRAMES 240 /* 2 s of quiet before a hold-dropped region re-arms: the breather's fp dips under the mask for a few frames at a time, a real lift stays quiet far longer */ +#define KIWI_STATIC_FRAMES 120 /* 1 s: a contact this old whose level never breathed is a phantom */ +#define KIWI_STILL_COUNTS 40 /* counts: real signal breathes far more (measured 854 on a real touch) */ +#define KIWI_ROW_SHIFT 64 /* per-row mean shift that marks a whole-image reference frame */ + +#ifdef KIWI_DEBUG +#define KDBG(...) fprintf(stderr, __VA_ARGS__) +#else +#define KDBG(...) do {} while (0) +#endif + +struct kiwi_contact { + bool on; + int miss; + int x, y; /* 1/KIWI_FP cell, centre of cell 0 is KIWI_FP / 2 */ + int size; + u32 born; /* accepted-frame stamp of the first sight of this blob */ + int bx, by; /* birth position: net drift past a cell over a sane hold = phantom */ + s16 b_sig; /* signal at the birth cell when born: flatness marks a stuck cell */ + bool lively; /* the birth-cell signal moved more than KIWI_STILL_COUNTS */ +}; + +struct kiwi { + struct input_dev *input; + bool have_base; + int rowmean[KIWI_ROWS]; /* per-row mean of the last frame, pre-removal */ + u8 cand_hot[(KIWI_CELLS + 7) / 8]; /* hot-cell map of the first baseline candidate */ + bool have_hot; + u64 drop_ring, drop_hold, drop_still; /* per-rule drop counters, debugfs */ + struct dentry *dbg; + int bad; + int agree; /* agreeing pairs chained toward a baseline */ + u32 now; /* accepted-frame counter, for the hold rule */ + bool sup; + s16 sup_r, sup_c; /* cell of the last hold-dropped phantom: re-arm guard */ + u8 sup_rad; /* every drop masks the blob extent (2): a neighbour cell must not re-arm */ + u8 sup_map[(KIWI_CELLS + 7) / 8]; /* hold-dropped extents: every hold drop marks its blob; the map lifts only on a sustained quiet run */ + bool sup_map_on; + u16 sup_map_clear; + bool had; /* a contact just ended here: its hold time carries over */ + bool last_still; /* the last end was a stillness drop: a carry must not inherit lively */ + s16 last_r, last_c; + u32 last_born; + int last_bx, last_by; + s16 last_bsig; + u32 last_end; /* frame of the last contact end: carry only across a miss-length gap */ + u8 cand[2 * KIWI_CELLS]; /* baseline candidate: raw frame, little endian */ + int cand_age; + s16 sig[KIWI_CELLS]; /* baseline minus raw, positive under a finger */ + s32 base[KIWI_CELLS]; + s32 fp[KIWI_CELLS]; + struct kiwi_contact cont[KIWI_MAX_SLOTS]; +}; + +static bool kiwi_match(struct hid_device *hdev, bool ignore_special_drivers) +{ + return hdev->bus == BUS_HOST && !strcmp(hdev->name, "Apple MTP kiwi"); +} + +/* + * The chip sends four reference frames in every 2 s cycle, each between two + * ordinary frames. They differ from the ordinary frames and from each other, so + * a frame is a baseline only if the frame two later matches it. + */ +/* Mark cells that lift more than a touch threshold off their row mean. The + * panel's static periphery outliers land in this set on every frame, so a + * frame is a baseline candidate only when its hot set matches the first one: + * a live touch adds hot cells, static texture does not. */ +static void kiwi_hot_set(const u8 *cells, u8 *hot) +{ + int r, c, i; + + memset(hot, 0, (KIWI_CELLS + 7) / 8); + for (r = 0; r < KIWI_ROWS; r++) { + s64 row = 0; + int mean; + + for (c = 0; c < KIWI_COLS; c++) + row += get_unaligned_le16(cells + 2 * (r * KIWI_COLS + c)); + mean = (int)div_s64(row, KIWI_COLS); + for (c = 0; c < KIWI_COLS; c++) { + i = r * KIWI_COLS + c; + if (abs((int)get_unaligned_le16(cells + 2 * i) - mean) > KIWI_THRESHOLD) + hot[i / 8] |= (u8)(1 << (i % 8)); + } + } +} + +static void kiwi_take_base(struct kiwi *k, const u8 *data) +{ + const u8 *cells = data + KIWI_HDR_LEN; + int i, r; + + if (!k->have_hot) { + kiwi_hot_set(cells, k->cand_hot); + k->have_hot = true; + } else { + u8 hot[(KIWI_CELLS + 7) / 8]; + + kiwi_hot_set(cells, hot); + if (memcmp(hot, k->cand_hot, sizeof(hot)) != 0) + return; /* a touch is down: keep the stale signal, wait for a quiet frame */ + } + if (k->cand_age == 0) { + memcpy(k->cand, cells, sizeof(k->cand)); + k->cand_age = 1; + return; + } + if (k->cand_age == 1) { + k->cand_age = 2; + return; + } + k->cand_age = 0; + for (i = 0; i < KIWI_CELLS; i++) + if (abs(get_unaligned_le16(cells + 2 * i) - get_unaligned_le16(k->cand + 2 * i)) > KIWI_SAME) { + k->agree = 0; /* a broken chain restarts from the first pair */ + return; + } + if (++k->agree < 2) { /* baseline only from agreeing frames: two pairs in a row */ + memcpy(k->cand, cells, sizeof(k->cand)); + k->cand_age = 1; + return; + } + k->agree = 0; + /* Bake every cell from its raw value, except interior hot cells: the + * panel's static periphery outliers are hot in every frame and must bake, + * but a finger resting on the glass at open is also hot, and baking it + * would eat a real touch for the whole session. Interior hot cells bake + * at their row's quiet level instead, so they read as a touch from the + * first frame; a dead-still interior block is then bounded by the static + * rule like any other flat phantom. */ + for (r = 0; r < KIWI_ROWS; r++) { + s64 quiet = 0; + int qn = 0, c, i2; + + for (c = 0; c < KIWI_COLS; c++) { + i2 = r * KIWI_COLS + c; + if (!(k->cand_hot[i2 / 8] & (u8)(1 << (i2 % 8)))) { + quiet += get_unaligned_le16(cells + 2 * i2); + qn++; + } + } + if (qn) + quiet = div_s64(quiet, qn); + for (c = 0; c < KIWI_COLS; c++) { + i2 = r * KIWI_COLS + c; + if ((k->cand_hot[i2 / 8] & (u8)(1 << (i2 % 8))) && + c >= KIWI_GATE_RING && c < KIWI_COLS - KIWI_GATE_RING && + r >= KIWI_GATE_RING && r < KIWI_ROWS - KIWI_GATE_RING) + k->base[i2] = (s32)quiet << 8; + else + k->base[i2] = get_unaligned_le16(cells + 2 * i2) << 8; + } + } + k->have_base = true; + k->bad = 0; +} + +/* + * Fixed point: base and fp are in 1/256 count. fp is baseline minus raw with the + * frame-wide shift removed, positive under a finger. + * Returns false for a frame that must be skipped: no baseline yet, or one of the + * four reference frames of the 2 s cycle. None may reach the baseline or the + * peak search: a baseline taken from the -126 frame, or a -126 frame passed + * through, turns the top edge cells into a held touch. + */ +static bool kiwi_load(struct kiwi *k, const u8 *data) +{ + s64 sum = 0; + s32 common; + int rowmax; + int i, r, c; + + if (!k->have_base) { + kiwi_take_base(k, data); + return false; + } + for (i = 0; i < KIWI_CELLS; i++) { + k->fp[i] = k->base[i] - (get_unaligned_le16(data + KIWI_HDR_LEN + 2 * i) << 8); + sum += k->fp[i]; + } + common = div_s64(sum, KIWI_CELLS); + if (abs(common >> 8) > KIWI_GLITCH) { + if (++k->bad >= KIWI_GLITCH_MAX) + k->have_base = false; + return false; + } + k->bad = 0; + /* the shift differs a little from row to row, so remove it per row */ + rowmax = 0; + for (r = 0; r < KIWI_ROWS; r++) { + s64 row = 0; + + for (c = 0; c < KIWI_COLS; c++) + row += k->fp[r * KIWI_COLS + c]; + common = div_s64(row, KIWI_COLS); + k->rowmean[r] = (int)common; + if (abs(common >> 8) > rowmax) + rowmax = abs(common >> 8); + for (c = 0; c < KIWI_COLS; c++) { + i = r * KIWI_COLS + c; + k->fp[i] -= common; + k->sig[i] = clamp(k->fp[i] >> 8, -32768, 32767); + } + } + if (rowmax > KIWI_ROW_SHIFT) { + /* a frame we cannot trust for contacts. A touch also leaves its rows + * a few tens of counts off (the slow adapt integrates the touch's row + * pressure); if skipped frames never healed, a lift would strand the + * row above the skip bound and blind the next touch. Heal mildly + * shifted rows here; true reference frames (hundreds of counts) and + * hot rows stay untouched. */ + for (r = 0; r < KIWI_ROWS; r++) { + int step, rr, cc, worst = 0; + + if (abs(k->rowmean[r] >> 8) > KIWI_ROW_SHIFT + 32) + continue; + /* heal only rows with no touch-strength cell: a lit blob is the + * very pressure that drifted the row, and healing under it just + * chases the blob */ + for (rr = 0; rr < KIWI_COLS; rr++) { + cc = k->sig[r * KIWI_COLS + rr]; + if (cc < 0) + cc = -cc; + if (cc > worst) + worst = cc; + } + if (worst > KIWI_THRESHOLD) + continue; + step = k->rowmean[r] >> 10; + for (c = 0; c < KIWI_COLS; c++) + k->base[r * KIWI_COLS + c] -= step; + } + if (++k->bad >= KIWI_GLITCH_MAX) + k->have_base = false; + return false; + } + return true; +} + +/* Track the baseline: fast up when a finger left, slow drift otherwise. + * Rows with a touch-strength cell never adapt: their idle cells would chase + * the touch's row pressure and strand the row when the touch lifts. */ +static void kiwi_adapt(struct kiwi *k) +{ + bool touched[KIWI_ROWS]; + int i, r, c; + + for (r = 0; r < KIWI_ROWS; r++) { + touched[r] = false; + for (c = 0; c < KIWI_COLS; c++) + if (abs(k->sig[r * KIWI_COLS + c]) > KIWI_THRESHOLD / 2) { + touched[r] = true; + break; + } + } + for (i = 0; i < KIWI_CELLS; i++) { + int s = k->sig[i]; + + if (touched[i / KIWI_COLS]) + continue; + if (s < -100) + k->base[i] -= k->fp[i] >> 3; + else if (s < 100) + k->base[i] -= k->fp[i] >> 10; + } +} + +static bool kiwi_is_peak(struct kiwi *k, int r, int c) +{ + int s = k->sig[r * KIWI_COLS + c]; + int dr, dc; + + for (dr = -1; dr <= 1; dr++) { + for (dc = -1; dc <= 1; dc++) { + int rr = r + dr, cc = c + dc, n; + + if ((!dr && !dc) || rr < 0 || rr >= KIWI_ROWS || cc < 0 || cc >= KIWI_COLS) + continue; + n = k->sig[rr * KIWI_COLS + cc]; + /* earlier in raster order must be strictly lower: one peak per plateau */ + if (((dr < 0) || (!dr && dc < 0)) ? n >= s : n > s) + return false; + } + } + return true; +} + +static void kiwi_centroid(struct kiwi *k, int r, int c, struct kiwi_contact *out) +{ + s64 w = 0, wx = 0, wy = 0; + int dr, dc, cells = 0; + + for (dr = -1; dr <= 1; dr++) { + for (dc = -1; dc <= 1; dc++) { + int rr = r + dr, cc = c + dc, s; + + if (rr < 0 || rr >= KIWI_ROWS || cc < 0 || cc >= KIWI_COLS) + continue; + s = k->sig[rr * KIWI_COLS + cc]; + if (s <= KIWI_THRESHOLD / 4) + continue; + w += s; + wx += (s64)s * cc; + wy += (s64)s * rr; + cells++; + } + } + out->x = (int)div64_s64(wx * KIWI_FP, w) + KIWI_FP / 2; + out->y = (int)div64_s64(wy * KIWI_FP, w) + KIWI_FP / 2; + out->size = cells; + out->on = true; + out->miss = 0; +} + +static int kiwi_find(struct kiwi *k, const struct kiwi_contact *c, u32 *claimed) +{ + int best = -1, best_d2 = KIWI_MATCH_D2, j; + + for (j = 0; j < KIWI_MAX_SLOTS; j++) { + int dx = k->cont[j].x - c->x, dy = k->cont[j].y - c->y, d2; + + if (!k->cont[j].on || (*claimed & BIT(j))) + continue; + d2 = dx * dx + dy * dy; + if (d2 < best_d2) { + best_d2 = d2; + best = j; + } + } + return best; +} + +/* mark the dropped blob: the centroid box, then every still-lit cell that + * touches it. A box on the centroid leaves the far edge of a wide blob + * unmarked, and that edge re-arms a frame later. The map is the only clock: + * it lifts after KIWI_LIFT_FRAMES of quiet, not on a dip and not on a timer. */ +static void kiwi_sup_mark(struct kiwi *k, int row, int col) +{ + int dr, dc, pass; + + for (dr = -2; dr <= 2; dr++) + for (dc = -2; dc <= 2; dc++) { + int rr = row + dr, cc = col + dc, i; + + if (rr < 0 || rr >= KIWI_ROWS || cc < 0 || cc >= KIWI_COLS) + continue; + i = rr * KIWI_COLS + cc; + k->sup_map[i / 8] |= (u8)(1 << (i % 8)); + } + /* grow into the lit blob, one ring per pass, bounded by KIWI_SUP_REACH */ + for (pass = 0; pass < KIWI_SUP_REACH; pass++) + for (dr = -KIWI_SUP_REACH; dr <= KIWI_SUP_REACH; dr++) + for (dc = -KIWI_SUP_REACH; dc <= KIWI_SUP_REACH; dc++) { + int rr = row + dr, cc = col + dc, i, a, b, near = 0; + + if (rr < 0 || rr >= KIWI_ROWS || cc < 0 || cc >= KIWI_COLS) + continue; + i = rr * KIWI_COLS + cc; + if (k->sup_map[i / 8] & (u8)(1 << (i % 8))) + continue; + if (k->sig[i] < KIWI_THRESHOLD / 2) + continue; + for (a = -1; a <= 1 && !near; a++) + for (b = -1; b <= 1; b++) { + int r2 = rr + a, c2 = cc + b, i2; + + if (!a && !b) + continue; + if (r2 < 0 || r2 >= KIWI_ROWS || + c2 < 0 || c2 >= KIWI_COLS) + continue; + i2 = r2 * KIWI_COLS + c2; + if (k->sup_map[i2 / 8] & (u8)(1 << (i2 % 8))) + near = 1; + } + if (near) + k->sup_map[i / 8] |= (u8)(1 << (i % 8)); + } + k->sup_map_on = true; + k->sup_map_clear = 0; +} + +static void kiwi_report(struct kiwi *k) +{ + struct input_dev *input = k->input; + int j, j2; + + for (j = 0; j < KIWI_MAX_SLOTS; j++) { + struct kiwi_contact *c = &k->cont[j]; + int x, y; + + if (c->on && k->now - c->born >= KIWI_STATIC_FRAMES && !c->lively) { + /* a 1 s old contact whose birth cell never breathed is a stuck cell */ + KDBG("f%u static-drop slot%d cell(row%d,col%d)\n", k->now, j, + c->y / KIWI_FP, c->x / KIWI_FP); + k->drop_still++; + c->on = false; + k->sup = true; + k->sup_rad = 2; + k->sup_r = clamp(c->y / KIWI_FP, 0, KIWI_ROWS - 1); + k->sup_c = clamp(c->x / KIWI_FP, 0, KIWI_COLS - 1); + /* an old flat contact dying of stillness is the stuck blob at + * the end of its hold life: mark the hold map, or its fp dips + * seed a fresh clock within frames. Young still drops keep + * today's burst semantics exactly. */ + if (k->now - c->born > KIWI_HOLD_FRAMES - KIWI_LIFT_FRAMES) { + kiwi_sup_mark(k, c->y / KIWI_FP, c->x / KIWI_FP); + for (j2 = 0; j2 < KIWI_MAX_SLOTS; j2++) + if (k->cont[j2].on && j2 != j && + abs(k->cont[j2].y / KIWI_FP - k->sup_r) <= k->sup_rad && + abs(k->cont[j2].x / KIWI_FP - k->sup_c) <= k->sup_rad) + k->cont[j2].on = false; /* one blob, one contact */ + } + k->had = true; + k->last_r = k->sup_r; + k->last_c = k->sup_c; + k->last_born = c->born; + k->last_bx = c->bx; + k->last_by = c->by; + k->last_bsig = c->b_sig; + k->last_end = k->now; + k->last_still = true; + } else if (c->on && k->now - c->born > KIWI_HOLD_FRAMES && + abs(c->x - c->bx) + abs(c->y - c->by) < 3 * KIWI_FP) { + /* a contact that outlives a sane hold in one spot is a phantom */ + KDBG("f%u hold-drop slot%d cell(row%d,col%d) drift(%d,%d)\n", k->now, j, + c->y / KIWI_FP, c->x / KIWI_FP, (c->y - c->by) / KIWI_FP, (c->x - c->bx) / KIWI_FP); + k->drop_hold++; + c->on = false; + k->sup = true; + k->sup_rad = 2; /* the dropped blob still spans its cells; mask them all */ + k->sup_r = clamp(c->y / KIWI_FP, 0, KIWI_ROWS - 1); + k->sup_c = clamp(c->x / KIWI_FP, 0, KIWI_COLS - 1); + kiwi_sup_mark(k, c->y / KIWI_FP, c->x / KIWI_FP); + for (j2 = 0; j2 < KIWI_MAX_SLOTS; j2++) + if (k->cont[j2].on && j2 != j && + abs(k->cont[j2].y / KIWI_FP - k->sup_r) <= k->sup_rad && + abs(k->cont[j2].x / KIWI_FP - k->sup_c) <= k->sup_rad) + k->cont[j2].on = false; /* one blob, one contact */ + k->had = true; + k->last_r = k->sup_r; + k->last_c = k->sup_c; + k->last_born = c->born; + k->last_bx = c->bx; + k->last_by = c->by; + k->last_end = k->now; + } + input_mt_slot(input, j); + input_mt_report_slot_state(input, MT_TOOL_FINGER, c->on); + if (!c->on) + continue; + x = div_s64((s64)c->x * (KIWI_MAX_X + 1), KIWI_COLS * KIWI_FP); + y = div_s64((s64)c->y * (KIWI_MAX_Y + 1), KIWI_ROWS * KIWI_FP); + /* Row 0 is the bottom edge of the panel. */ + x = clamp(x, 0, KIWI_MAX_X); + y = clamp(KIWI_MAX_Y - y, 0, KIWI_MAX_Y); + input_report_abs(input, ABS_MT_POSITION_X, x); + input_report_abs(input, ABS_MT_POSITION_Y, y); + input_report_abs(input, ABS_MT_TOUCH_MAJOR, c->size * 16); + } + input_mt_sync_frame(input); + input_sync(input); +} + +static void kiwi_track(struct kiwi *k, struct kiwi_contact *found, int n) +{ + u32 claimed = 0; + int i, i2, j, r, c; + + for (i = 0; i < n; i++) { + j = kiwi_find(k, &found[i], &claimed); + if (j < 0) + continue; + claimed |= BIT(j); + { + u32 b = k->cont[j].born; + int bx = k->cont[j].bx, by = k->cont[j].by; + s16 bs = k->cont[j].b_sig; + bool lv = k->cont[j].lively; + + k->cont[j] = found[i]; + k->cont[j].born = b; + k->cont[j].bx = bx; + k->cont[j].by = by; + k->cont[j].b_sig = bs; + k->cont[j].lively = lv; /* liveliness is sticky: the claim copy must not wipe it */ + if (!k->cont[j].lively) { + int d = k->sig[(by / KIWI_FP) * KIWI_COLS + (bx / KIWI_FP)] - bs; + + if (d < 0) + d = -d; + if (d > KIWI_STILL_COUNTS) { + k->cont[j].lively = true; + KDBG("f%u lively slot%d d%d\n", k->now, j, d); + } + } + } + found[i].on = false; /* consumed */ + } + for (j = 0; j < KIWI_MAX_SLOTS; j++) { + if (k->cont[j].on && !(claimed & BIT(j)) && ++k->cont[j].miss > KIWI_MISS_MAX) { + k->cont[j].on = false; + /* a contact near the hold age that dips out misses into its + * own drop: mark the hold map here, or the same blob seeds a + * fresh slot with a fresh clock one frame later. The window + * opens one lift-run early: measured on the breather, its + * last dip lands 2 frames shy of the full hold age. */ + if (k->now - k->cont[j].born > KIWI_HOLD_FRAMES - KIWI_LIFT_FRAMES) { + k->sup = true; + k->sup_rad = 2; + k->sup_r = clamp(k->cont[j].y / KIWI_FP, 0, KIWI_ROWS - 1); + k->sup_c = clamp(k->cont[j].x / KIWI_FP, 0, KIWI_COLS - 1); + kiwi_sup_mark(k, k->cont[j].y / KIWI_FP, k->cont[j].x / KIWI_FP); + k->drop_hold++; + } + KDBG("f%u miss-drop slot%d cell(row%d,col%d) held%u\n", k->now, j, + k->cont[j].y / KIWI_FP, k->cont[j].x / KIWI_FP, k->now - k->cont[j].born); + k->had = true; + k->last_r = clamp(k->cont[j].y / KIWI_FP, 0, KIWI_ROWS - 1); + k->last_c = clamp(k->cont[j].x / KIWI_FP, 0, KIWI_COLS - 1); + k->last_born = k->cont[j].born; + k->last_bx = k->cont[j].bx; + k->last_by = k->cont[j].by; + k->last_bsig = k->cont[j].b_sig; + k->last_end = k->now; + k->last_still = false; + } + } + for (i = 0; i < n; i++) { + if (!found[i].on) + continue; + for (j = 0; j < KIWI_MAX_SLOTS; j++) { + int dx = k->cont[j].x - found[i].x, dy = k->cont[j].y - found[i].y; + + if (k->cont[j].on && dx * dx + dy * dy < KIWI_MATCH_D2) { + found[i].on = false; /* already tracked: one blob, one contact */ + break; + } + } + if (!found[i].on) + continue; + r = found[i].x / KIWI_FP; + c = found[i].y / KIWI_FP; + if (k->sup && abs(c - k->sup_r) <= k->sup_rad && abs(r - k->sup_c) <= k->sup_rad) + continue; /* the dropped phantom's cell is still lit */ + i2 = c * KIWI_COLS + r; + if (k->sup_map_on && (k->sup_map[i2 / 8] & (u8)(1 << (i2 % 8)))) + continue; /* a hold-dropped region: silent until it lifts */ + for (j = 0; j < KIWI_MAX_SLOTS && k->cont[j].on; j++) + ; + if (j < KIWI_MAX_SLOTS) { + KDBG("f%u open slot%d cell(row%d,col%d) carry%d\n", k->now, j, c, r, + k->had && abs(c - k->last_r) <= 2 && abs(r - k->last_c) <= 2); + k->cont[j] = found[i]; + if (k->had && abs(c - k->last_r) <= 2 && abs(r - k->last_c) <= 2 && + k->now - k->last_born <= KIWI_HOLD_FRAMES + KIWI_MISS_MAX && + k->now - k->last_end <= KIWI_MISS_MAX) { + k->cont[j].born = k->last_born; /* same hold, still going */ + k->cont[j].bx = k->last_bx; + k->cont[j].by = k->last_by; + k->cont[j].b_sig = k->last_bsig; + /* a carried chain has earned its breath, unless the chain + * just died of stillness: the same flat blob lit a neighbour */ + k->cont[j].lively = !k->last_still; + } else { + int i2; + + k->cont[j].born = k->now; + k->cont[j].bx = found[i].x; + k->cont[j].by = found[i].y; + k->cont[j].lively = false; + i2 = (k->cont[j].by / KIWI_FP) * KIWI_COLS + (k->cont[j].bx / KIWI_FP); + k->cont[j].b_sig = (k->cont[j].by >= 0 && k->cont[j].by / KIWI_FP < KIWI_ROWS && + k->cont[j].bx >= 0 && k->cont[j].bx / KIWI_FP < KIWI_COLS) + ? k->sig[i2] : 0; + } + } + } + /* the guard clears once its cell goes quiet, so a real finger works again */ + if (k->sup && !k->sup_map_on) { + i = k->sup_r * KIWI_COLS + k->sup_c; + if (k->sup_r < 0 || k->sup_r >= KIWI_ROWS || k->sup_c < 0 || k->sup_c >= KIWI_COLS || + k->sig[i] < KIWI_THRESHOLD / 2) + k->sup = false; + } + /* the hold map lifts only after a sustained quiet run: the blob's fp + * dips cannot re-arm a dropped region with a fresh clock, while a real + * lift (quiet well past KIWI_LIFT_FRAMES) frees the cells for the next + * touch */ + if (k->sup_map_on) { + bool lit = false; + + for (i2 = 0; i2 < KIWI_CELLS && !lit; i2++) + if ((k->sup_map[i2 / 8] & (u8)(1 << (i2 % 8))) && + k->sig[i2] >= KIWI_THRESHOLD / 2) + lit = true; + if (lit) { + k->sup_map_clear = 0; + } else if (++k->sup_map_clear >= KIWI_LIFT_FRAMES) { + memset(k->sup_map, 0, sizeof(k->sup_map)); + k->sup_map_on = false; + k->sup_map_clear = 0; + k->sup = false; + } + } +} + +static void kiwi_frame(struct kiwi *k, const u8 *data) +{ + struct kiwi_contact found[KIWI_MAX_SLOTS]; + int r, c, n = 0; + + if (!kiwi_load(k, data)) + return; + k->now++; + + for (r = 0; r < KIWI_ROWS && n < KIWI_MAX_SLOTS; r++) { + for (c = 0; c < KIWI_COLS && n < KIWI_MAX_SLOTS; c++) { + /* the 2-cell edge ring is where stuck-cell phantoms live; a real + * finger is never centred that close to the bezel */ + if (k->sig[r * KIWI_COLS + c] >= KIWI_THRESHOLD && kiwi_is_peak(k, r, c)) { + if (r > 1 && r < KIWI_ROWS - 2 && c > 1 && c < KIWI_COLS - 2) + kiwi_centroid(k, r, c, &found[n++]); + else + k->drop_ring++; /* the 2-cell edge ring rejected a peak */ + } + } + } + kiwi_adapt(k); + kiwi_track(k, found, n); + kiwi_report(k); +} + +static int kiwi_raw_event(struct hid_device *hdev, struct hid_report *report, + u8 *data, int size) +{ + struct kiwi *k = hid_get_drvdata(hdev); + + if (size >= KIWI_MIN_FRAME && data[0] == KIWI_MAGIC) + kiwi_frame(k, data); + return 0; +} + +static int kiwi_input_open(struct input_dev *input) +{ + struct kiwi *k = input_get_drvdata(input); + + k->have_base = false; + k->cand_age = 0; + k->agree = 0; + k->now = 0; + k->sup = false; + k->had = false; + return hid_hw_open(to_hid_device(input->dev.parent)); +} + +static void kiwi_input_close(struct input_dev *input) +{ + hid_hw_close(to_hid_device(input->dev.parent)); +} + +static struct input_dev *kiwi_alloc_input(struct hid_device *hdev, struct kiwi *k) +{ + struct input_dev *input = devm_input_allocate_device(&hdev->dev); + + if (!input) + return NULL; + input->name = "Apple MTP touch screen"; + input->id.bustype = hdev->bus; + input->id.vendor = hdev->vendor; + input->id.product = hdev->product; + input->phys = hdev->phys; + input->dev.parent = &hdev->dev; + input->open = kiwi_input_open; + input->close = kiwi_input_close; + input_set_drvdata(input, k); + __set_bit(INPUT_PROP_DIRECT, input->propbit); + input_set_abs_params(input, ABS_MT_POSITION_X, 0, KIWI_MAX_X, 0, 0); + input_set_abs_params(input, ABS_MT_POSITION_Y, 0, KIWI_MAX_Y, 0, 0); + input_abs_set_res(input, ABS_MT_POSITION_X, KIWI_RES); + input_abs_set_res(input, ABS_MT_POSITION_Y, KIWI_RES); + input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0); + if (input_mt_init_slots(input, KIWI_MAX_SLOTS, INPUT_MT_DIRECT)) + return NULL; + return input; +} + +static int kiwi_probe(struct hid_device *hdev, const struct hid_device_id *id) +{ + struct kiwi *kiwi; + int ret; + + kiwi = devm_kzalloc(&hdev->dev, sizeof(*kiwi), GFP_KERNEL); + if (!kiwi) + return -ENOMEM; + hid_set_drvdata(hdev, kiwi); + + ret = hid_parse(hdev); + if (ret) + return ret; + + kiwi->input = kiwi_alloc_input(hdev, kiwi); + if (!kiwi->input) + return -ENOMEM; + ret = input_register_device(kiwi->input); + if (ret) + return ret; + + kiwi->dbg = debugfs_create_dir("hid-apple-mtp-kiwi", NULL); + debugfs_create_u64("drop_edge_ring", 0444, kiwi->dbg, &kiwi->drop_ring); + debugfs_create_u64("drop_hold", 0444, kiwi->dbg, &kiwi->drop_hold); + debugfs_create_u64("drop_still", 0444, kiwi->dbg, &kiwi->drop_still); + + return hid_hw_start(hdev, HID_CONNECT_HIDRAW); +} + +static void kiwi_remove(struct hid_device *hdev) +{ + struct kiwi *k = hid_get_drvdata(hdev); + + debugfs_remove_recursive(k->dbg); + input_unregister_device(k->input); + hid_hw_stop(hdev); +} + +static const struct hid_device_id kiwi_devices[] = { + { HID_DEVICE(BUS_HOST, HID_GROUP_ANY, HOST_VENDOR_ID_APPLE, HID_ANY_ID) }, + { } +}; +MODULE_DEVICE_TABLE(hid, kiwi_devices); + +static struct hid_driver kiwi_driver = { + .name = "hid-apple-mtp-kiwi", + .id_table = kiwi_devices, + .match = kiwi_match, + .probe = kiwi_probe, + .remove = kiwi_remove, + .raw_event = kiwi_raw_event, +}; +module_hid_driver(kiwi_driver); + +MODULE_DESCRIPTION("Apple MTP touch-screen (kiwi) interface"); +MODULE_LICENSE("GPL"); From c5725a2467229839df6f031368867512354ad898 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Tue, 17 Nov 2026 10:58:23 +0000 Subject: [PATCH 2/3] HID: apple-mtp-kiwi: M6 reach-0 mutant in the mutation suite Why: the hold-rebirth fix lives or dies with the blob grow; a mutant that drops it must go red on holdrebirth, and does (the old 4 borns). Also generates the holdrebirth capture for that check. Proof: full mutation run: M1, M4, M5, M6 red; M3 idle PASS; M0 unhardened baseline red on stuckedge as before. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> (cherry picked from commit 95769b13c323c8ebe3b8aa9e5bab765dacd2e28c) --- .../run-kiwi-mutations.sh | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100755 drivers/hid/apple-mtp-kiwi-replay/run-kiwi-mutations.sh diff --git a/drivers/hid/apple-mtp-kiwi-replay/run-kiwi-mutations.sh b/drivers/hid/apple-mtp-kiwi-replay/run-kiwi-mutations.sh new file mode 100755 index 0000000000000..792dc0267431b --- /dev/null +++ b/drivers/hid/apple-mtp-kiwi-replay/run-kiwi-mutations.sh @@ -0,0 +1,57 @@ +#!/usr/bin/env bash +# mutation check for the kiwi hardening: each guard removed must turn its +# synthetic capture RED again in the replay harness. +# usage: run-kiwi-mutations.sh (env: RUN for outputs, BASE for the frozen +# unhardened driver; captures are generated into $RUN) +set -e +H="$(cd "$(dirname "$0")" && pwd)" +if [ -f "$H/replay.c" ]; then + S=$H +else + S=$H/replay +fi +if [ -f "$H/linux-shim.h" ]; then + SHIM=$H +elif [ -f "$H/shim/linux-shim.h" ]; then + SHIM=$H/shim +elif [ -f "$H/../shim/linux-shim.h" ]; then + SHIM=$H/../shim +else + echo "cannot locate replay.c under $H" >&2 + exit 2 +fi +R=${R:-$(cd "$H/../.." && pwd)/kiwi} +DRIVE=${DRIVE:-$R/drv/hid-apple-mtp-kiwi.c} +[ -f "$DRIVE" ] || DRIVE=$(cd "$H/.." && pwd)/hid-apple-mtp-kiwi.c +RUN=${RUN:-/tmp/kiwi-mut} +BASE=${BASE:-$H/baseline2.c} # frozen unhardened driver, sha in the log +mkdir -p "$RUN" +[ -f "$BASE" ] || { echo "frozen baseline driver missing: $BASE" >&2; exit 2; } +for m in stuckedge wander flat2s idlecycle presshold holdrebirth; do + [ -f "$RUN/$m.frames" ] || python3 "$S/kiwi-synth.py" "$RUN/$m.frames" "$m" >/dev/null +done +cc -O2 -I"$SHIM" -include linux-shim.h -DKIWI_DRV="\"$BASE\"" "$S/replay.c" -o "$RUN/kiwi-replay-base" +echo "== M0 baseline (unhardened): stuckedge must FAIL" +"$RUN/kiwi-replay-base" --expect strict --phases 60 --loop 40 "$RUN/stuckedge.frames" 2>/dev/null | tail -n 1 || true + +echo "== M1 edge-ring guard removed: stuckedge must FAIL again" +sed 's/if (r > 1 \&\& r < KIWI_ROWS - 2 \&\& c > 1 \&\& c < KIWI_COLS - 2)/if (1)/' "$DRIVE" > "$RUN/m1.c" +cc -O2 -I"$SHIM" -include linux-shim.h -DKIWI_DRV="\"$RUN/m1.c\"" "$S/replay.c" -o "$RUN/kiwi-replay-m1" +"$RUN/kiwi-replay-m1" --expect strict --phases 60 --loop 40 "$RUN/stuckedge.frames" 2>/dev/null | tail -n 1 || true +echo "== M2 hold rule disabled: defense in depth only (the static rule already bounds the blob, so no behavioral delta is observable)" +echo "== M4 static rule disabled: flat2s must FAIL again" +sed 's/#define KIWI_STATIC_FRAMES\t120/#define KIWI_STATIC_FRAMES\t0x7fffffff/' "$DRIVE" > "$RUN/m4.c" +cc -O2 -I"$SHIM" -include linux-shim.h -DKIWI_DRV="\"$RUN/m4.c\"" "$S/replay.c" -o "$RUN/kiwi-replay-m4" +"$RUN/kiwi-replay-m4" --expect flat --phases 60 --loop 40 "$RUN/flat2s.frames" 2>/dev/null | tail -n 1 || true +echo "== M3 agreeing-pair chain dropped (accept on first pair): idle must still PASS" +sed 's/if (++k->agree < 2) {/if (0) {/' "$DRIVE" > "$RUN/m3.c" +cc -O2 -I"$SHIM" -include linux-shim.h -DKIWI_DRV="\"$RUN/m3.c\"" "$S/replay.c" -o "$RUN/kiwi-replay-m3" +"$RUN/kiwi-replay-m3" --expect none --phases 60 --loop 40 "$RUN/idlecycle.frames" 2>/dev/null | tail -n 1 || true +echo "== M5 carry gap check removed: presshold must FAIL again" +sed 's/k->now - k->last_end <= KIWI_MISS_MAX) {/k->now - k->last_end <= KIWI_HOLD_FRAMES + KIWI_MISS_MAX) {/' "$DRIVE" > "$RUN/m5.c" +cc -O2 -I"$SHIM" -include linux-shim.h -DKIWI_DRV="\"$RUN/m5.c\"" "$S/replay.c" -o "$RUN/kiwi-replay-m5" +"$RUN/kiwi-replay-m5" --expect press --phases 1 --loop 1 "$RUN/presshold.frames" 2>/dev/null | tail -n 1 || true +echo "== M6 blob grow removed (reach 0): holdrebirth must FAIL again, the old 4 borns" +sed 's/#define KIWI_SUP_REACH\t\t6/#define KIWI_SUP_REACH\t\t0/' "$DRIVE" > "$RUN/m6.c" +cc -O2 -I"$SHIM" -include linux-shim.h -DKIWI_DRV="\"$RUN/m6.c\"" "$S/replay.c" -o "$RUN/kiwi-replay-m6" +"$RUN/kiwi-replay-m6" --expect reborn:583,594 --phases 1 --loop 1 "$RUN/holdrebirth.frames" 2>/dev/null | tail -n 1 || true From 20ce1d1bb5c80d03a1508134de96de168580c8ca Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Thu, 19 Nov 2026 19:03:05 -0600 Subject: [PATCH 3/3] HID: build the K114c Kiwi driver and bound its frame format Add Kconfig and Makefile entries for the in-tree module. Only bind on K114c, and reject frame lengths outside its measured format. Move the host replay sources into tools/testing. Keep their existing optional capture and mutation inputs. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- drivers/hid/Kconfig | 10 ++++++++++ drivers/hid/Makefile | 1 + drivers/hid/hid-apple-mtp-kiwi.c | 10 ++++++---- .../selftests}/hid/apple-mtp-kiwi-replay/kiwi-synth.py | 0 .../selftests}/hid/apple-mtp-kiwi-replay/replay.c | 0 .../hid/apple-mtp-kiwi-replay/run-kiwi-mutations.sh | 6 ++---- .../hid/apple-mtp-kiwi-replay/run-kiwi-replay.sh | 6 ++---- 7 files changed, 21 insertions(+), 12 deletions(-) rename {drivers => tools/testing/selftests}/hid/apple-mtp-kiwi-replay/kiwi-synth.py (100%) rename {drivers => tools/testing/selftests}/hid/apple-mtp-kiwi-replay/replay.c (100%) rename {drivers => tools/testing/selftests}/hid/apple-mtp-kiwi-replay/run-kiwi-mutations.sh (94%) rename {drivers => tools/testing/selftests}/hid/apple-mtp-kiwi-replay/run-kiwi-replay.sh (94%) diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig index 6f956d056b87c..8e12ebebef27d 100644 --- a/drivers/hid/Kconfig +++ b/drivers/hid/Kconfig @@ -156,6 +156,16 @@ config HID_APPLE_MTP_HAPTIC This driver can also be built as a module. If so, it will be called hid-apple-mtp-haptic. +config HID_APPLE_MTP_KIWI + tristate "Apple MTP Kiwi touchscreen" + depends on HID_DOCKCHANNEL + help + Support the K114c touchscreen on the DockChannel HID transport. + Decode the K114c sensor grid and report direct multitouch events + through the input subsystem. Other board layouts are not supported. + + The module is called hid-apple-mtp-kiwi. + config HID_APPLEIR tristate "Apple infrared receiver" depends on (USB_HID) diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile index 3efe8ee1caddb..ba4942205143d 100644 --- a/drivers/hid/Makefile +++ b/drivers/hid/Makefile @@ -30,6 +30,7 @@ obj-$(CONFIG_HID_ALPS) += hid-alps.o obj-$(CONFIG_HID_ACRUX) += hid-axff.o obj-$(CONFIG_HID_APPLE) += hid-apple.o obj-$(CONFIG_HID_APPLE_MTP_HAPTIC) += hid-apple-mtp-haptic.o +obj-$(CONFIG_HID_APPLE_MTP_KIWI) += hid-apple-mtp-kiwi.o obj-$(CONFIG_HID_APPLEIR) += hid-appleir.o obj-$(CONFIG_HID_APPLETB_BL) += hid-appletb-bl.o obj-$(CONFIG_HID_APPLETB_KBD) += hid-appletb-kbd.o diff --git a/drivers/hid/hid-apple-mtp-kiwi.c b/drivers/hid/hid-apple-mtp-kiwi.c index 58100d263c9ba..83a20e93a6a3a 100644 --- a/drivers/hid/hid-apple-mtp-kiwi.c +++ b/drivers/hid/hid-apple-mtp-kiwi.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #include @@ -45,10 +46,10 @@ #define KIWI_COLS 82 #define KIWI_ROWS 54 #define KIWI_CELLS (KIWI_COLS * KIWI_ROWS) -#define KIWI_MIN_FRAME (KIWI_HDR_LEN + 2 * KIWI_CELLS) +#define KIWI_FRAME_SIZE 17861 #define KIWI_MAGIC 0x5a #define KIWI_MAX_SLOTS 10 -/* eDP-1 panel of the k114c, 3024x1986. */ +/* K114c is the only qualified panel and frame format. */ #define KIWI_MAX_X 3023 #define KIWI_MAX_Y 1985 /* Panel is about 10 units per mm. */ @@ -120,7 +121,8 @@ struct kiwi { static bool kiwi_match(struct hid_device *hdev, bool ignore_special_drivers) { - return hdev->bus == BUS_HOST && !strcmp(hdev->name, "Apple MTP kiwi"); + return hdev->bus == BUS_HOST && !strcmp(hdev->name, "Apple MTP kiwi") && + of_machine_is_compatible("apple,k114c"); } /* @@ -703,7 +705,7 @@ static int kiwi_raw_event(struct hid_device *hdev, struct hid_report *report, { struct kiwi *k = hid_get_drvdata(hdev); - if (size >= KIWI_MIN_FRAME && data[0] == KIWI_MAGIC) + if (size == KIWI_FRAME_SIZE && data[0] == KIWI_MAGIC) kiwi_frame(k, data); return 0; } diff --git a/drivers/hid/apple-mtp-kiwi-replay/kiwi-synth.py b/tools/testing/selftests/hid/apple-mtp-kiwi-replay/kiwi-synth.py similarity index 100% rename from drivers/hid/apple-mtp-kiwi-replay/kiwi-synth.py rename to tools/testing/selftests/hid/apple-mtp-kiwi-replay/kiwi-synth.py diff --git a/drivers/hid/apple-mtp-kiwi-replay/replay.c b/tools/testing/selftests/hid/apple-mtp-kiwi-replay/replay.c similarity index 100% rename from drivers/hid/apple-mtp-kiwi-replay/replay.c rename to tools/testing/selftests/hid/apple-mtp-kiwi-replay/replay.c diff --git a/drivers/hid/apple-mtp-kiwi-replay/run-kiwi-mutations.sh b/tools/testing/selftests/hid/apple-mtp-kiwi-replay/run-kiwi-mutations.sh similarity index 94% rename from drivers/hid/apple-mtp-kiwi-replay/run-kiwi-mutations.sh rename to tools/testing/selftests/hid/apple-mtp-kiwi-replay/run-kiwi-mutations.sh index 792dc0267431b..115987325026e 100755 --- a/drivers/hid/apple-mtp-kiwi-replay/run-kiwi-mutations.sh +++ b/tools/testing/selftests/hid/apple-mtp-kiwi-replay/run-kiwi-mutations.sh @@ -17,12 +17,10 @@ elif [ -f "$H/shim/linux-shim.h" ]; then elif [ -f "$H/../shim/linux-shim.h" ]; then SHIM=$H/../shim else - echo "cannot locate replay.c under $H" >&2 + echo "cannot locate linux-shim.h near $H" >&2 exit 2 fi -R=${R:-$(cd "$H/../.." && pwd)/kiwi} -DRIVE=${DRIVE:-$R/drv/hid-apple-mtp-kiwi.c} -[ -f "$DRIVE" ] || DRIVE=$(cd "$H/.." && pwd)/hid-apple-mtp-kiwi.c +DRIVE=${DRIVE:-$(cd "$H/../../../../.." && pwd)/drivers/hid/hid-apple-mtp-kiwi.c} RUN=${RUN:-/tmp/kiwi-mut} BASE=${BASE:-$H/baseline2.c} # frozen unhardened driver, sha in the log mkdir -p "$RUN" diff --git a/drivers/hid/apple-mtp-kiwi-replay/run-kiwi-replay.sh b/tools/testing/selftests/hid/apple-mtp-kiwi-replay/run-kiwi-replay.sh similarity index 94% rename from drivers/hid/apple-mtp-kiwi-replay/run-kiwi-replay.sh rename to tools/testing/selftests/hid/apple-mtp-kiwi-replay/run-kiwi-replay.sh index 537c54c076bf2..d4fff51560323 100755 --- a/drivers/hid/apple-mtp-kiwi-replay/run-kiwi-replay.sh +++ b/tools/testing/selftests/hid/apple-mtp-kiwi-replay/run-kiwi-replay.sh @@ -1,9 +1,7 @@ #!/usr/bin/env bash # kiwi replay harness: build and run against a given driver file. # usage: run-kiwi-replay.sh /path/to/hid-apple-mtp-kiwi.c [OUTDIR] -# works from the committed tree dir (drivers/hid/apple-mtp-kiwi-replay) and -# from the scratch layout (kiwi/replay with a sibling shim/); real captures -# come from $CAPS when that directory exists, else only the synthetics run. +# Use CAPS to supply real captures. Without it, run synthetic captures only. set -e DRV=$1 OUT=${2:-/tmp/kiwi-replay} @@ -20,7 +18,7 @@ elif [ -f "$H/shim/linux-shim.h" ]; then elif [ -f "$H/../shim/linux-shim.h" ]; then SHIM=$H/../shim else - echo "cannot locate replay.c under $H" >&2 + echo "cannot locate linux-shim.h near $H" >&2 exit 2 fi CAPS=${CAPS:-$H/../kiwi-caps}