From 7b3452727292d0a7a34a60f363b6c6bbd5150bc6 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Wed, 11 Nov 2026 23:11:40 +0000 Subject: [PATCH 1/9] HID: apple-mtp-kiwi: add the MTP touch screen driver The touch screen of the 14 inch M5 Max MacBook Pro (K114c) sits behind the kiwi interface of the dockchannel MTP. The interface has a 29-byte HID descriptor with one opaque input item and no description of the contacts. The chip reports a raw 82 column by 54 row capacitance image every 8.3 ms and does no contact detection itself. Claim the interface by name (hid-apple and hid-magicmouse already refuse BUS_HOST Apple interfaces that are not a keyboard or mouse), keep the raw pipe on hidraw, and register a direct multi-touch input device with 10 slots over the 3024x1986 panel. The driver: - clamps the Report Size of the descriptor, which is 0x80000 bits and above the 16384 that hid-core accepts, in report_fixup, - skips the whole-image shift frames the chip sends every 2 s and removes a per-row mean from the others, - keeps a fixed-point baseline per cell (fast up when a finger left, slow drift otherwise), - takes 3x3 local maxima 250 counts below baseline, centroids them and tracks them frame to frame with a 2 frame miss debounce. Row 0 of the sensor is the bottom edge of the panel, so y is mirrored; this is a fixed fact of the hardware, there is no option for it. The grid and the panel size are K114 only, and frames that are not 17861 bytes long are ignored. Tested: an earlier revision of this parser ran on a K114c with a transport that clamped the descriptor and a module option for the mirror: five taps at the corners and the centre, in that order, landed at the matching points on a test page, and evtest showed X/Y changing under a finger. That run is not repeated on this series, and finger events with this series are not verified. The clamp in this patch is checked in a userland test only. The firmware load and power sequence of the interface are in a separate patch. Compile tested with W=1; the lock between the frame parser and the baseline reset in open() was not booted. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- MAINTAINERS | 5 + arch/arm64/configs/asahi.config | 1 + drivers/hid/Kconfig | 12 + drivers/hid/Makefile | 1 + drivers/hid/hid-apple-mtp-kiwi.c | 432 +++++++++++++++++++++++++++++++ 5 files changed, 451 insertions(+) create mode 100644 drivers/hid/hid-apple-mtp-kiwi.c diff --git a/MAINTAINERS b/MAINTAINERS index 3f919f3e327f3..2dacd2a7ea3ff 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -2064,6 +2064,11 @@ F: drivers/gpu/drm/apple/dcp-fabric* F: tools/testing/selftests/drivers/gpu/apple-afkv2/ F: tools/testing/selftests/drivers/gpu/apple-dcp/ +APPLE MTP TOUCH SCREEN (KIWI) DRIVER +M: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> +S: Maintained +F: drivers/hid/hid-apple-mtp-kiwi.c + APPLE PCIE CONTROLLER DRIVER M: Marc Zyngier L: linux-pci@vger.kernel.org diff --git a/arch/arm64/configs/asahi.config b/arch/arm64/configs/asahi.config index 8195cd4e4a230..f83ed0a1f5ea0 100644 --- a/arch/arm64/configs/asahi.config +++ b/arch/arm64/configs/asahi.config @@ -103,6 +103,7 @@ CONFIG_SND_SOC_TAS2770=m CONFIG_HID_APPLE=m CONFIG_HID_MAGICMOUSE=m CONFIG_HID_APPLE_MTP_HAPTIC=m +CONFIG_HID_APPLE_MTP_KIWI=m CONFIG_SERIAL_SAMSUNG=y CONFIG_SERIAL_SAMSUNG_CONSOLE=y CONFIG_HID_DOCKCHANNEL=m diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig index 6f956d056b87c..70926a6303c72 100644 --- a/drivers/hid/Kconfig +++ b/drivers/hid/Kconfig @@ -156,6 +156,18 @@ 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 touch screen (kiwi) support" + depends on HID + help + Support for the touch screen of the 14 inch M5 Max MacBook Pro + (K114c). The driver turns the raw capacitance image of the touch + interface into multi-touch contacts. The sensor grid and the panel + size are fixed in the driver, and it ignores frames of any other size. + + This driver can also be built as a module. If so, it will be 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 new file mode 100644 index 0000000000000..1a87b4e18db04 --- /dev/null +++ b/drivers/hid/hid-apple-mtp-kiwi.c @@ -0,0 +1,432 @@ +// 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: + * + * 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, slightly different per row). Frames shifted by more than 200 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 +#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_FRAME_LEN 17861 +#define KIWI_MAX_REPORT_SIZE 16384 /* hid-core rejects a larger Report Size */ +#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 200 /* mean shift of the 2 s reference frame is about 900 */ +#define KIWI_GLITCH_MAX 10 /* skipped frames in a row before the baseline is retaken */ +#define KIWI_MISS_MAX 2 +#define KIWI_MATCH_D2 (4 * KIWI_FP * 4 * KIWI_FP) + +#define KIWI_THRESHOLD 250 /* counts below baseline that make a touch */ + +struct kiwi_contact { + bool on; + int miss; + int x, y; /* 1/KIWI_FP cell, centre of cell 0 is KIWI_FP / 2 */ + int size; +}; + +struct kiwi { + struct input_dev *input; + /* Serialises the frame parser with the baseline reset in open() */ + spinlock_t lock; + bool have_base; + int bad; + 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"); +} + +/* + * 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 (big reference frame). + * Besides the two big reference frames (+658 and +902 counts) every 2 s there is a + * -445 and a -126 count frame: the whole image moves, so remove the mean. + */ +static bool kiwi_load(struct kiwi *k, const u8 *data) +{ + s64 sum = 0; + s32 common; + int i, r, c; + + if (!k->have_base) { + for (i = 0; i < KIWI_CELLS; i++) + k->base[i] = get_unaligned_le16(data + KIWI_HDR_LEN + 2 * i) << 8; + k->have_base = true; + k->bad = 0; + 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 */ + 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); + 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); + } + } + return true; +} + +/* Track the baseline: fast up when a finger left, slow drift otherwise. */ +static void kiwi_adapt(struct kiwi *k) +{ + int i; + + for (i = 0; i < KIWI_CELLS; i++) { + int s = k->sig[i]; + + 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; +} + +static void kiwi_report(struct kiwi *k) +{ + struct input_dev *input = k->input; + int j; + + for (j = 0; j < KIWI_MAX_SLOTS; j++) { + struct kiwi_contact *c = &k->cont[j]; + int x, y; + + 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, j; + + for (i = 0; i < n; i++) { + j = kiwi_find(k, &found[i], &claimed); + if (j < 0) + continue; + claimed |= BIT(j); + k->cont[j] = found[i]; + 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; + } + for (i = 0; i < n; i++) { + if (!found[i].on) + continue; + for (j = 0; j < KIWI_MAX_SLOTS && k->cont[j].on; j++) + ; + if (j < KIWI_MAX_SLOTS) + k->cont[j] = found[i]; + } +} + +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; + for (r = 0; r < KIWI_ROWS && n < KIWI_MAX_SLOTS; r++) { + for (c = 0; c < KIWI_COLS && n < KIWI_MAX_SLOTS; c++) { + if (k->sig[r * KIWI_COLS + c] >= KIWI_THRESHOLD && kiwi_is_peak(k, r, c)) + kiwi_centroid(k, r, c, &found[n++]); + } + } + kiwi_adapt(k); + kiwi_track(k, found, n); + kiwi_report(k); +} + +/* + * The descriptor declares the whole frame as one item of 0x80000 bits. + * hid-core refuses a Report Size above 16384, so clamp it: the driver reads the + * raw frames and never looks at the field layout. + */ +static const __u8 *kiwi_report_fixup(struct hid_device *hdev, __u8 *rdesc, + unsigned int *rsize) +{ + unsigned int i = 0; + + while (i < *rsize) { + u8 prefix = rdesc[i]; + unsigned int n = prefix & 3, j; + u32 val = 0; + + if (prefix == 0xfe) { /* long item */ + if (i + 1 >= *rsize) + break; + i += 3 + rdesc[i + 1]; + continue; + } + if (n == 3) + n = 4; + if (i + 1 + n > *rsize) + break; + if ((prefix & 0xfc) == 0x74 && n >= 2) { /* Global: Report Size */ + for (j = 0; j < n; j++) + val |= (u32)rdesc[i + 1 + j] << (8 * j); + if (val > KIWI_MAX_REPORT_SIZE) { + hid_info(hdev, "clamping Report Size %u to %u\n", + val, KIWI_MAX_REPORT_SIZE); + for (j = 0; j < n; j++) + rdesc[i + 1 + j] = (KIWI_MAX_REPORT_SIZE >> (8 * j)) & 0xff; + } + } + i += 1 + n; + } + return rdesc; +} + +static int kiwi_raw_event(struct hid_device *hdev, struct hid_report *report, + u8 *data, int size) +{ + struct kiwi *k = hid_get_drvdata(hdev); + unsigned long flags; + + if (size != KIWI_FRAME_LEN) { + hid_warn_once(hdev, "unexpected frame length %d, expected %d\n", + size, KIWI_FRAME_LEN); + return 0; + } + if (data[0] == KIWI_MAGIC) { + spin_lock_irqsave(&k->lock, flags); + kiwi_frame(k, data); + spin_unlock_irqrestore(&k->lock, flags); + } + return 0; +} + +static int kiwi_input_open(struct input_dev *input) +{ + struct kiwi *k = input_get_drvdata(input); + unsigned long flags; + + spin_lock_irqsave(&k->lock, flags); + k->have_base = false; + spin_unlock_irqrestore(&k->lock, flags); + 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; + spin_lock_init(&kiwi->lock); + 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; + + return hid_hw_start(hdev, HID_CONNECT_HIDRAW); +} + +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, + .report_fixup = kiwi_report_fixup, + .raw_event = kiwi_raw_event, +}; +module_hid_driver(kiwi_driver); + +MODULE_DESCRIPTION("Apple MTP touch-screen (kiwi) interface"); +MODULE_LICENSE("GPL"); From e2e4daebe57625f80a0540d12bd67dc4089f2f58 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Wed, 11 Nov 2026 23:11:41 +0000 Subject: [PATCH 2/9] HID: dockchannel-hid: bring up the kiwi (touch screen) interface The touch screen ("kiwi") of the 14 inch M5 Max MacBook Pro rides the same dockchannel MTP as the multi-touch interface and announces itself the same way, but it owns its reset and power lines instead of sharing the AFE reset, and its 1.3 MB firmware image is written by the MTP bootloader over the touch IC SPI, which takes seconds rather than the 1 s the comm timeout allows. - A power method 2 interface may decode its own reset and power lines from its device node; without its own reset line it keeps falling back to the shared AFE reset, so existing multi-touch boards are unchanged. - Announced GPIOs are tracked per interface (the MTP announces clock_24MHz_enable, display_sync, power and reset for kiwi) instead of failing on the second one, and a GPIO command is matched to its announced id. The kiwi clock and display_sync pads are pad functions in the device tree, not host GPIOs: they are left unset and a command for them is refused. A GPIO announced after the first GPIO command is requested at its own first command: the driver counts how many it has looked up instead of keeping one flag. - While the kiwi firmware load is running, comm commands use a 10 s timeout, and the kiwi interface waits 10 s for its ready message. - The kiwi start sequence, taken only when kiwi has a reset line of its own, power-cycles the chip with a reset pulse (the reset line is active low in the device tree), settles 50 ms, sends the firmware and then runs the normal power method 2 transitions. The power line is raised on the way up and left alone on the way down. The sequence is fixed code, not tunables. It needs the device tree side (the kiwi node, its reset and power lines and the two pad functions), which is a separate patch. The image installed as apple/tpkiwifw-k114c.bin fails to load at its operations 26, 29 and 30, which are optional property placeholders (address 0, not required). The verified runs used an image with those three operations removed, so whatever installs the firmware needs to drop them. Compile tested against this tree with W=1. The start sequence, the timeouts and the pad function settings are verified on hardware on a 7.2.11 based tree. Booted on a K114c on this base with the first revision of this series: the interface binds, reports ready with status 0 and streams 17861-byte frames at about 123 Hz. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- drivers/hid/dockchannel-hid/dockchannel-hid.c | 226 ++++++++++++++---- 1 file changed, 175 insertions(+), 51 deletions(-) diff --git a/drivers/hid/dockchannel-hid/dockchannel-hid.c b/drivers/hid/dockchannel-hid/dockchannel-hid.c index 9e0877ca2d496..17b05ae4da936 100644 --- a/drivers/hid/dockchannel-hid/dockchannel-hid.c +++ b/drivers/hid/dockchannel-hid/dockchannel-hid.c @@ -29,8 +29,12 @@ #define COMMAND_TIMEOUT_MS 1000 #define START_TIMEOUT_MS 2000 +/* The MTP bootloader writes the 1.3 MB kiwi image over the touch IC SPI: seconds, not 1 s. */ +#define KIWI_CMD_TIMEOUT_MS 10000 +#define KIWI_START_TIMEOUT_MS 10000 #define MAX_INTERFACES 16 +#define MAX_IFACE_GPIOS 4 /* Complete frame, including the largest aligned body and its checksum. */ #define MAX_PKT_SIZE (8 + 0xfffc + 4) @@ -180,8 +184,15 @@ struct dchid_iface { size_t hid_desc_len; struct gpio_desc *gpio; - char gpio_name[MAX_GPIO_NAME]; - int gpio_id; + /* + * The MTP of the K114 boards announces several GPIOs per interface + * (kiwi: clock_24MHz_enable, display_sync, power, reset). Track them all. + */ + struct gpio_desc *gpios[MAX_IFACE_GPIOS]; + char gpio_names[MAX_IFACE_GPIOS][MAX_GPIO_NAME]; + int gpio_ids[MAX_IFACE_GPIOS]; + int gpio_count; + unsigned int gpios_requested; /* entries of gpios[] already looked up */ bool gpio_announced; struct mutex out_mutex; @@ -195,6 +206,10 @@ struct dchid_iface { u32 keyboard_layout_id; u32 power_method; bool firmware_managed_reset; + + /* Power method 2: the line that resets this interface, and its optional supply. */ + struct gpio_desc *reset; + struct gpio_desc *power; }; struct dockchannel_hid { @@ -203,6 +218,8 @@ struct dockchannel_hid { struct device_link *helper_link; struct apple_smc *afe_smc; bool stopping; + /* The kiwi firmware load is running: comm commands get the long timeout */ + bool kiwi_loading; bool inherited_mtp; /* Between the system sleep suspend and resume callbacks */ bool suspended; @@ -253,6 +270,7 @@ dchid_get_interface(struct dockchannel_hid *dchid, int index, const char *name) u32 power_method = DCHID_POWER_METHOD_1; bool firmware_managed_reset = false; struct device_node *of_node = NULL; + struct gpio_desc *reset = NULL, *power = NULL; struct dchid_iface *iface; if (READ_ONCE(dchid->stopping)) @@ -294,11 +312,34 @@ dchid_get_interface(struct dockchannel_hid *dchid, int index, const char *name) power_method, name); goto err_put_node; } - if (power_method == DCHID_POWER_METHOD_2 && - !firmware_managed_reset && !dchid->afe_reset) { - dev_err(dchid->dev, "Power method 2 for %s needs the AFE reset line\n", - name); - goto err_put_node; + if (power_method == DCHID_POWER_METHOD_2 && !firmware_managed_reset) { + struct fwnode_handle *fwnode = of_fwnode_handle(of_node); + + /* An interface with its own reset line (kiwi) skips the AFE one. */ + reset = devm_fwnode_gpiod_get(dchid->dev, fwnode, "apple,reset", + GPIOD_OUT_LOW, "dchid-reset"); + if (IS_ERR(reset)) { + if (PTR_ERR(reset) != -ENOENT) { + dev_err(dchid->dev, "Reset line of %s: %pe\n", name, reset); + goto err_put_node; + } + reset = dchid->afe_reset; + } + if (!reset) { + dev_err(dchid->dev, "Power method 2 for %s needs a reset line\n", + name); + goto err_put_node; + } + + power = devm_fwnode_gpiod_get(dchid->dev, fwnode, "apple,power", + GPIOD_OUT_HIGH, "dchid-power"); + if (IS_ERR(power)) { + if (PTR_ERR(power) != -ENOENT) { + dev_err(dchid->dev, "Power line of %s: %pe\n", name, power); + goto err_put_node; + } + power = NULL; + } } } @@ -314,6 +355,8 @@ dchid_get_interface(struct dockchannel_hid *dchid, int index, const char *name) iface->of_node = of_node; iface->power_method = power_method; iface->firmware_managed_reset = firmware_managed_reset; + iface->reset = reset; + iface->power = power; iface->out_report = -1; init_completion(&iface->out_complete); init_completion(&iface->ready); @@ -427,6 +470,7 @@ static int dchid_send(struct dchid_iface *iface, u32 flags, void *msg, size_t si static int dchid_cmd(struct dchid_iface *iface, u32 type, u32 req, void *data, size_t size, void *resp_buf, size_t resp_size) { + unsigned int timeout_ms; int ret; int report_id = *(u8*)data; @@ -443,7 +487,9 @@ static int dchid_cmd(struct dchid_iface *iface, u32 type, u32 req, if (ret < 0) goto done; - if (!wait_for_completion_timeout(&iface->out_complete, msecs_to_jiffies(COMMAND_TIMEOUT_MS))) { + timeout_ms = READ_ONCE(iface->dchid->kiwi_loading) ? + KIWI_CMD_TIMEOUT_MS : COMMAND_TIMEOUT_MS; + if (!wait_for_completion_timeout(&iface->out_complete, msecs_to_jiffies(timeout_ms))) { dev_err(iface->dchid->dev, "output report 0x%x to iface %d (%s) timed out\n", report_id, iface->index, iface->name); ret = -ETIMEDOUT; @@ -571,11 +617,21 @@ static int dchid_register_ring(struct dockchannel_hid *dchid) return ret; } +/* Drive an interface's own reset line, and its supply on the way up, to the OFF or ON state. */ +static int dchid_set_own_lines(struct dchid_iface *iface, u8 state) +{ + bool on = state == DCHID_POWER_STATE_ON; + + if (on && iface->power) + gpiod_set_value_cansleep(iface->power, on); + return gpiod_set_value_cansleep(iface->reset, !on); +} + /* - * Power method 2: announce the transition, drive the AFE reset line to the - * new state and confirm the transition. The reset is asserted for a power-off - * and held for 50 ms, and released for a power-on and given 50 ms to settle - * before the transition is confirmed. + * Power method 2: announce the transition, drive the interface's reset line + * to the new state and confirm the transition. The reset is asserted for a + * power-off and held for 50 ms, and released for a power-on before the + * transition is confirmed. */ static int dchid_pm2_set_power(struct dchid_iface *iface, u8 state) { @@ -595,21 +651,19 @@ static int dchid_pm2_set_power(struct dchid_iface *iface, u8 state) } else { bool off = state == DCHID_POWER_STATE_OFF; - ret = gpiod_set_value_cansleep(dchid->afe_reset, off); + ret = dchid_set_own_lines(iface, state); if (ret < 0) { - dev_err(dchid->dev, "%s: failed to %s the AFE reset: %d\n", + dev_err(dchid->dev, "%s: failed to %s the reset line: %d\n", iface->name, off ? "assert" : "release", ret); return ret; } - dev_dbg(dchid->dev, "%s: AFE reset %s\n", iface->name, - off ? "asserted" : "released"); /* - * The sequence validated on J716s (M5 Pro, MTP firmware - * 6320.86.1, "Touch MT ready" on 3 of 3 boots) also waits - * 50 ms after the release before HasChanged, as the - * firmware-managed path above does. + * The kiwi sequence was verified with a settle after power-off + * only; the shared AFE reset (multi-touch, sequence validated + * on J716s) settles after every transition. */ - fsleep(50 * USEC_PER_MSEC); + if (off || iface->reset == dchid->afe_reset) + fsleep(50 * USEC_PER_MSEC); } dchid_pm2_command(iface->index, state, true, msg); @@ -705,7 +759,7 @@ static int dchid_get_firmware(struct dchid_iface *iface, void **firmware, size_t static int dchid_request_gpio(struct dchid_iface *iface) { char prop_name[MAX_GPIO_NAME + 16]; - int ret; + int ret, g; if (iface->firmware_managed_reset) return -EINVAL; @@ -715,30 +769,54 @@ static int dchid_request_gpio(struct dchid_iface *iface) return -EOPNOTSUPP; } - if (iface->gpio) + if (iface->gpio_count && iface->gpios_requested == iface->gpio_count) + return 0; + if (!iface->gpio_count && iface->gpio) return 0; - dev_dbg(iface->dchid->dev, "Requesting GPIO %s#%d: %s\n", - iface->name, iface->gpio_id, iface->gpio_name); + for (g = iface->gpios_requested; g < iface->gpio_count; g++) { + const char *name = iface->gpio_names[g]; - /* A power method 2 interface holds its reset line from probe on. */ - if (iface->power_method == DCHID_POWER_METHOD_2 && - !strcmp(iface->gpio_name, "afe-reset")) { - iface->gpio = iface->dchid->afe_reset; - return 0; - } + dev_dbg(iface->dchid->dev, "Requesting GPIO %s#%d: %s\n", + iface->name, iface->gpio_ids[g], name); - snprintf(prop_name, sizeof(prop_name), "apple,%s", iface->gpio_name); + /* + * A power method 2 multi-touch holds its AFE reset line from + * probe on; the pm2 transitions drive it, not this path. + */ + if (iface->power_method == DCHID_POWER_METHOD_2 && + !strcmp(name, "afe-reset")) { + iface->gpios[g] = iface->dchid->afe_reset; + if (g == 0) + iface->gpio = iface->dchid->afe_reset; + continue; + } - iface->gpio = devm_gpiod_get_index(iface->dchid->dev, prop_name, 0, GPIOD_OUT_LOW); + if (iface->gpios[g]) + continue; - if (IS_ERR(iface->gpio)) { - ret = PTR_ERR(iface->gpio); - iface->gpio = NULL; - dev_err(iface->dchid->dev, "Failed to request GPIO %s-gpios: %d\n", - prop_name, ret); - return ret; + snprintf(prop_name, sizeof(prop_name), "apple,%s", name); + iface->gpios[g] = devm_gpiod_get_index(iface->dchid->dev, prop_name, + 0, GPIOD_OUT_LOW); + if (IS_ERR(iface->gpios[g])) { + ret = PTR_ERR(iface->gpios[g]); + iface->gpios[g] = NULL; + /* + * A line the device tree does not wire as a host GPIO (the + * kiwi clock and display_sync pads are pad functions) is + * left unset; a command for it is refused. + */ + if (ret == -ENOENT) + continue; + dev_err(iface->dchid->dev, + "Failed to request GPIO %s-gpios: %d\n", + prop_name, ret); + return ret; + } + if (g == 0) + iface->gpio = iface->gpios[g]; } + iface->gpios_requested = iface->gpio_count; return 0; } @@ -747,6 +825,7 @@ static int dchid_start_interface(struct dchid_iface *iface) { void *fw; size_t size; + bool kiwi_own; int ret; if (iface->starting) { @@ -754,6 +833,12 @@ static int dchid_start_interface(struct dchid_iface *iface) return -EINPROGRESS; } + /* Only a kiwi with a reset line of its own may power-cycle it; the AFE reset is shared. */ + kiwi_own = !strcmp(iface->name, "kiwi") && + iface->power_method == DCHID_POWER_METHOD_2 && + !iface->firmware_managed_reset && + iface->reset != iface->dchid->afe_reset; + dev_dbg(iface->dchid->dev, "Starting interface %s\n", iface->name); iface->starting = true; @@ -773,13 +858,30 @@ static int dchid_start_interface(struct dchid_iface *iface) goto err; } - /* Only multi-touch has firmware */ + /* Only multi-touch and kiwi have firmware */ if (fw && size) { + /* + * The kiwi chip needs a full power cycle with a reset pulse + * before its firmware goes out (its reset line is active low + * in the device tree). + */ + if (kiwi_own) { + WRITE_ONCE(iface->dchid->kiwi_loading, true); + ret = dchid_set_own_lines(iface, DCHID_POWER_STATE_OFF); + if (ret < 0) + goto err; + fsleep(50 * USEC_PER_MSEC); + ret = dchid_set_own_lines(iface, DCHID_POWER_STATE_ON); + if (ret < 0) + goto err; + fsleep(50 * USEC_PER_MSEC); + } /* Send firmware to the device */ dev_dbg(iface->dchid->dev, "Sending firmware for %s\n", iface->name); /* Retained MTP must see AFE reset throughout firmware upload. */ if (iface->dchid->inherited_mtp && + !kiwi_own && iface->power_method == DCHID_POWER_METHOD_2) { if (iface->firmware_managed_reset) { ret = -EINVAL; @@ -799,8 +901,11 @@ static int dchid_start_interface(struct dchid_iface *iface) /* After loading firmware, multi-touch needs a reset */ dev_dbg(iface->dchid->dev, "Resetting %s\n", iface->name); if (iface->power_method == DCHID_POWER_METHOD_2) { - /* The retained session is already off while reset is asserted. */ - if (!iface->dchid->inherited_mtp) { + /* + * The retained session is already off while reset is asserted; + * kiwi always power-cycles its own lines. + */ + if (!iface->dchid->inherited_mtp || kiwi_own) { ret = dchid_pm2_set_power(iface, DCHID_POWER_STATE_OFF); if (ret < 0) goto err; @@ -816,9 +921,13 @@ static int dchid_start_interface(struct dchid_iface *iface) } } + if (kiwi_own) + WRITE_ONCE(iface->dchid->kiwi_loading, false); return 0; err: + if (kiwi_own) + WRITE_ONCE(iface->dchid->kiwi_loading, false); iface->starting = false; return ret; } @@ -849,6 +958,7 @@ static void dchid_stop(struct hid_device *hdev) static int dchid_open(struct hid_device *hdev) { struct dchid_iface *iface = hdev->driver_data; + unsigned int timeout_ms; int ret; if (READ_ONCE(iface->dchid->stopping)) @@ -859,7 +969,9 @@ static int dchid_open(struct hid_device *hdev) if (ret < 0) return ret; - if (!wait_for_completion_timeout(&iface->ready, msecs_to_jiffies(START_TIMEOUT_MS))) { + timeout_ms = !strcmp(iface->name, "kiwi") ? + KIWI_START_TIMEOUT_MS : START_TIMEOUT_MS; + if (!wait_for_completion_timeout(&iface->ready, msecs_to_jiffies(timeout_ms))) { dev_err(iface->dchid->dev, "iface %s start timed out\n", iface->name); return -ETIMEDOUT; } @@ -1160,14 +1272,16 @@ static void dchid_handle_init(struct dockchannel_hid *dchid, void *data, size_t break; } - if (iface->gpio_announced) { + if (iface->gpio_count >= MAX_IFACE_GPIOS) { dev_err(dchid->dev, - "Cannot request more than one GPIO per interface!\n"); + "Too many GPIO requests for %s\n", iface->name); break; } - strscpy(iface->gpio_name, req->name, MAX_GPIO_NAME); - iface->gpio_id = req->id; + strscpy(iface->gpio_names[iface->gpio_count], req->name, + MAX_GPIO_NAME); + iface->gpio_ids[iface->gpio_count] = req->id; + iface->gpio_count++; iface->gpio_announced = true; break; } @@ -1214,6 +1328,9 @@ static void dchid_handle_init(struct dockchannel_hid *dchid, void *data, size_t static void dchid_handle_gpio(struct dockchannel_hid *dchid, void *data, size_t length) { + struct gpio_desc *cmd_gpio = NULL; + bool found = false; + unsigned int g; struct dchid_gpio_cmd *cmd = data; struct dchid_iface *iface; u32 retcode = 0xe000f00d; /* Give it a random Apple-style error code */ @@ -1236,7 +1353,14 @@ static void dchid_handle_gpio(struct dockchannel_hid *dchid, void *data, size_t if (dchid_request_gpio(iface) < 0) goto err; - if (!iface->gpio || cmd->gpio != iface->gpio_id) { + for (g = 0; g < iface->gpio_count; g++) { + if (iface->gpio_ids[g] == cmd->gpio) { + cmd_gpio = iface->gpios[g]; + found = true; + break; + } + } + if (!found || !cmd_gpio) { dev_err(dchid->dev, "Got GPIO command for bad GPIO %s#%d\n", iface->name, cmd->gpio); goto err; @@ -1247,13 +1371,13 @@ static void dchid_handle_gpio(struct dockchannel_hid *dchid, void *data, size_t switch (cmd->cmd) { case 3: /* Pulse. */ - gpiod_set_value_cansleep(iface->gpio, 1); + gpiod_set_value_cansleep(cmd_gpio, 1); msleep(10); /* Random guess... */ - gpiod_set_value_cansleep(iface->gpio, 0); + gpiod_set_value_cansleep(cmd_gpio, 0); retcode = 0; break; default: - dev_err(dchid->dev, "Unknown GPIO command %d\n", cmd->cmd ); + dev_err(dchid->dev, "Unknown GPIO command %d\n", cmd->cmd); break; } From b1e78d5fcc993ca3d9688e408282c13f44fef768 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Wed, 11 Nov 2026 23:22:25 +0000 Subject: [PATCH 3/9] dt-bindings: input: apple,dockchannel-hid: document the kiwi interface properties The kiwi (touch screen) interface of the dockchannel MTP owns its power sequencing lines instead of sharing the multi-touch analog front end reset. Document apple,power-method, apple,reset-gpios and apple,power-gpios on the kiwi child node, mirroring how the multi-touch child documents its power method. Without apple,reset-gpios the driver falls back to the shared AFE reset of the parent and does not power-cycle the touchscreen itself; the text says so. The 24 MHz clock and display sync pads are pad functions selected by the transport node pinctrl, not GPIO properties, so they need no binding entries. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- .../bindings/input/apple,dockchannel-hid.yaml | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Documentation/devicetree/bindings/input/apple,dockchannel-hid.yaml b/Documentation/devicetree/bindings/input/apple,dockchannel-hid.yaml index b6c9e727e5a67..61a3f48ea491e 100644 --- a/Documentation/devicetree/bindings/input/apple,dockchannel-hid.yaml +++ b/Documentation/devicetree/bindings/input/apple,dockchannel-hid.yaml @@ -241,6 +241,33 @@ properties: Name of the touchscreen firmware that the host sends to the coprocessor when the interface is first opened. + apple,power-method: + $ref: /schemas/types.yaml#/definitions/uint32 + enum: [1, 2] + default: 1 + description: + The power sequencing protocol of the coprocessor firmware, as + described above. With power method 2 the touchscreen owns its + lines, so apple,reset-gpios should be given. Without it the driver + falls back to the shared apple,afe-reset-gpios of the parent and + does not power-cycle the touchscreen itself. + + apple,reset-gpios: + maxItems: 1 + description: + Reset line of the touchscreen, described with the level that + asserts the reset as its active level. Unlike the multi-touch + analog front end reset the touchscreen owns the line, and with + power method 2 the host drives it itself during the start + sequence instead of using the shared reset of the parent. + + apple,power-gpios: + maxItems: 1 + description: + Optional supply enable line of the touchscreen, raised to power + the interface on and lowered to power it off during power + method 2 transitions. + mtp: type: object additionalProperties: false From a7b2febb963a3610ee5e7792bbaa00b80eb174a5 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Thu, 12 Nov 2026 02:10:33 +0000 Subject: [PATCH 4/9] arm64: dts: apple: t6050-k114c: enable the kiwi touch screen interface The touch screen of the 14 inch MacBook Pro with the M5 Max rides the dockchannel MTP as its own "kiwi" interface. It sequences its own power: with power method 2 the host drives the touchscreen reset (nub GPIO 25, active low) and its optional supply enable (nub GPIO 26), instead of the shared multi-touch AFE reset on the SMC. Enable the shared always-on NUB GPIO power domain and controller the way J714s does, with its seven interrupts, mux the 24 MHz clock (nub pin 22) and display sync (AP pin 11) pads through the transport node pinctrl, and enable the interface with its k114c firmware name. The reset polarity, the GPIO numbers and the two pad function values (pin 22 and pin 11, function 1) are those of the device tree that gave touch on the hardware on a kernel based on v7.2.11. Booted on a K114c on an earlier base with the first revision of this series: the interface binds, reports ready with status 0 and streams 17861-byte frames at about 123 Hz. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- .../arm64/boot/dts/apple/t6050-k114-k116.dtsi | 11 +++--- arch/arm64/boot/dts/apple/t6050-k114c.dts | 38 +++++++++++++++++++ 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/arch/arm64/boot/dts/apple/t6050-k114-k116.dtsi b/arch/arm64/boot/dts/apple/t6050-k114-k116.dtsi index f7cedd1a4c854..2fa146288fbfe 100644 --- a/arch/arm64/boot/dts/apple/t6050-k114-k116.dtsi +++ b/arch/arm64/boot/dts/apple/t6050-k114-k116.dtsi @@ -223,11 +223,12 @@ /* * The touchscreen (ADT mtp-transport/kiwi, image-tag "fsat"). macOS * sends it firmware through the same CBOR bootloader as the trackpad. - * Its power, reset, 24 MHz clock and display-sync lines are SoC GPIOs - * (nub-gpio0 0x1a, 0x19, 0x16; gpio0 0xb), with an SMC power reference - * (pKW4('rCIO', 0x20000)) and the TSRC clock source. How that load and - * the report format map onto this transport is still being worked - * out, so the interface stays off until it has been tested. + * Its power (nub-gpio0 0x1a) and reset (0x19) lines are GPIOs; the 24 + * MHz clock (nub-gpio0 0x16) and display-sync (gpio0 0xb) pads are pad + * functions, muxed by the transport node's pinctrl-0. The ADT also + * names an SMC power reference (pKW4('rCIO', 0x20000)) and the TSRC + * clock source. A board enables the interface once its settings have + * been tested (the K114c so far). */ kiwi: kiwi { status = "disabled"; diff --git a/arch/arm64/boot/dts/apple/t6050-k114c.dts b/arch/arm64/boot/dts/apple/t6050-k114c.dts index 13ea865ba58e1..3112915bc719d 100644 --- a/arch/arm64/boot/dts/apple/t6050-k114c.dts +++ b/arch/arm64/boot/dts/apple/t6050-k114c.dts @@ -12,6 +12,7 @@ /* Defined before the includes: t6050-max.dtsi guards the ATC2 router on it. */ #define T6050_K114C #include "t6050-k114-k116.dtsi" +#include / { compatible = "apple,k114c", "apple,t6050", "apple,arm-platform"; @@ -581,3 +582,40 @@ <&ps_atc2_cio_usb>, <&ps_atc2_cio_auss>, <&ps_atc2_pcie>; apple,pcie-tunnel = <&acio2_pcie_tunnel>; }; + +/* + * The touchscreen (kiwi). It sequences its own power: power method 2 with + * its own reset line (nub GPIO 25, active low) and supply enable (nub GPIO + * 26), not the shared multi-touch AFE reset. The GPIO numbers, pin values + * and polarities are those of a device tree that gave touch on this machine. + */ +&ps_nub_gpio { + status = "okay"; +}; + +&nub_gpio0 { + status = "okay"; + + kiwi_clk24_pins: kiwi-clk24-pins { + pinmux = ; + }; +}; + +&pinctrl_ap { + kiwi_dsync_pins: kiwi-dsync-pins { + pinmux = ; + }; +}; + +&mtp_hid { + pinctrl-0 = <&kiwi_clk24_pins>, <&kiwi_dsync_pins>; + pinctrl-names = "default"; +}; + +&kiwi { + firmware-name = "apple/tpkiwifw-k114c.bin"; + apple,power-method = <2>; + apple,reset-gpios = <&nub_gpio0 25 GPIO_ACTIVE_LOW>; + apple,power-gpios = <&nub_gpio0 26 GPIO_ACTIVE_HIGH>; + status = "okay"; +}; From 124d387ca23e6dda107aeadce01aea2f3d937065 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Wed, 18 Nov 2026 15:23:34 -0600 Subject: [PATCH 5/9] HID: apple-mtp-kiwi: suppress phantom top-edge contacts at rest The K114c touch panel can leave a few top-edge capacitance cells biased low after baseline adaptation. The tracker treated those cells as fingers and emitted MT events at the menu bar (ABS_X about 36) with nobody touching: 245 events in 45 s at rest on one boot, 2232 events in 15 s on another (K114c, 2026-11-18). Add hold and stillness drops, a hold-map so a dropped phantom cannot re-seed with a fresh tracking id, and edge guarding. Debug output goes through dev_dbg on the input device. Tested on a K114c at rest: the previous driver produced bursts of contacts every few seconds; with this one loaded, zero contacts were reported over about 28 minutes across a reboot. Replayed tap, press-and-hold and drag captures still produce their contacts. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- drivers/hid/hid-apple-mtp-kiwi.c | 597 ++++++++++++++++++++++++++----- 1 file changed, 511 insertions(+), 86 deletions(-) diff --git a/drivers/hid/hid-apple-mtp-kiwi.c b/drivers/hid/hid-apple-mtp-kiwi.c index 1a87b4e18db04..9eaca6bfd0c19 100644 --- a/drivers/hid/hid-apple-mtp-kiwi.c +++ b/drivers/hid/hid-apple-mtp-kiwi.c @@ -12,7 +12,7 @@ * * 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: + * 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) @@ -24,19 +24,18 @@ * 8917.. further sections that are not decoded yet * * Every 2 s four whole-image shifts arrive (about +658, +902, -445 and -126 - * counts, slightly different per row). Frames shifted by more than 200 counts - * are skipped and a per-row mean is removed from the rest. The raw image has no + * 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 -#include #include #include @@ -47,8 +46,6 @@ #define KIWI_ROWS 54 #define KIWI_CELLS (KIWI_COLS * KIWI_ROWS) #define KIWI_MIN_FRAME (KIWI_HDR_LEN + 2 * KIWI_CELLS) -#define KIWI_FRAME_LEN 17861 -#define KIWI_MAX_REPORT_SIZE 16384 /* hid-core rejects a larger Report Size */ #define KIWI_MAGIC 0x5a #define KIWI_MAX_SLOTS 10 /* eDP-1 panel of the k114c, 3024x1986. */ @@ -57,26 +54,77 @@ /* Panel is about 10 units per mm. */ #define KIWI_RES 10 #define KIWI_FP 256 -#define KIWI_GLITCH 200 /* mean shift of the 2 s reference frame is about 900 */ -#define KIWI_GLITCH_MAX 10 /* skipped frames in a row before the baseline is retaken */ +/* counts: reference frames shift the mean by 137 to 899, idle frames by under 10 */ +#define KIWI_GLITCH 64 +/* counts: two ordinary frames differ less than this in every cell */ +#define KIWI_SAME 200 +/* skipped frames in a row before the baseline is retaken */ +#define KIWI_GLITCH_MAX 10 #define KIWI_MISS_MAX 2 +/* the static periphery outliers all live inside this ring */ +#define KIWI_GATE_RING 3 #define KIWI_MATCH_D2 (4 * KIWI_FP * 4 * KIWI_FP) -#define KIWI_THRESHOLD 250 /* counts below baseline that make a touch */ +/* counts below baseline that make a touch */ +#define KIWI_THRESHOLD 250 +/* 45 s at 120 Hz: a static contact past this is a phantom */ +#define KIWI_HOLD_FRAMES 5400 +/* shortcut: 6 cells from the drop, raise if a wider blob re-arms past the mark */ +#define KIWI_SUP_REACH 6 +/* 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_LIFT_FRAMES 240 +/* 1 s: a contact this old whose level never breathed is a phantom */ +#define KIWI_STATIC_FRAMES 120 +/* counts: real signal breathes far more (measured 854 on a real touch) */ +#define KIWI_STILL_COUNTS 40 +/* per-row mean shift that marks a whole-image reference frame */ +#define KIWI_ROW_SHIFT 64 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; - /* Serialises the frame parser with the baseline reset in open() */ - spinlock_t lock; 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 */ + /* every drop masks the blob extent (2): a neighbour cell must not re-arm */ + u8 sup_rad; + /* hold-dropped extents: every hold drop marks its blob; the map lifts + * only on a sustained quiet run + */ + u8 sup_map[(KIWI_CELLS + 7) / 8]; + bool sup_map_on; + u16 sup_map_clear; + bool had; /* a contact just ended here: its hold time carries over */ + /* the last end was a stillness drop: a carry must not inherit lively */ + bool last_still; + 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]; @@ -88,24 +136,125 @@ 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 (big reference frame). - * Besides the two big reference frames (+658 and +902 counts) every 2 s there is a - * -445 and a -126 count frame: the whole image moves, so remove the mean. + * 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) { - for (i = 0; i < KIWI_CELLS; i++) - k->base[i] = get_unaligned_le16(data + KIWI_HDR_LEN + 2 * i) << 8; - k->have_base = true; - k->bad = 0; + kiwi_take_base(k, data); return false; } for (i = 0; i < KIWI_CELLS; i++) { @@ -120,29 +269,81 @@ static bool kiwi_load(struct kiwi *k, const u8 *data) } 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. */ +/* 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) { - int i; + 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) @@ -163,7 +364,7 @@ static bool kiwi_is_peak(struct kiwi *k, int r, int c) 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) + if (((dr < 0) || (!dr && dc < 0)) ? n >= s : n > s) return false; } } @@ -215,15 +416,132 @@ static int kiwi_find(struct kiwi *k, const struct kiwi_contact *c, u32 *claimed) 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 bool kiwi_sup_near(const struct kiwi *k, int row, int col) +{ + int a, b; + + for (a = -1; a <= 1; a++) + for (b = -1; b <= 1; b++) { + int r2 = row + a, c2 = col + 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))) + return true; + } + return false; +} + +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; + + 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; + if (kiwi_sup_near(k, rr, cc)) + 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; + 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 */ + dev_dbg(&k->input->dev, + "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 */ + dev_dbg(&k->input->dev, + "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) @@ -244,27 +562,159 @@ static void kiwi_report(struct kiwi *k) static void kiwi_track(struct kiwi *k, struct kiwi_contact *found, int n) { u32 claimed = 0; - int i, j; + 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); - k->cont[j] = found[i]; + { + 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; + /* liveliness is sticky: the claim copy must not wipe it */ + k->cont[j].lively = lv; + 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; + dev_dbg(&k->input->dev, + "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) + 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++; + } + dev_dbg(&k->input->dev, + "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) + if (j < KIWI_MAX_SLOTS) { + dev_dbg(&k->input->dev, + "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; + } } } @@ -275,10 +725,19 @@ static void kiwi_frame(struct kiwi *k, const u8 *data) 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++) { - if (k->sig[r * KIWI_COLS + c] >= KIWI_THRESHOLD && kiwi_is_peak(k, r, c)) - kiwi_centroid(k, r, c, &found[n++]); + /* 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); @@ -286,73 +745,26 @@ static void kiwi_frame(struct kiwi *k, const u8 *data) kiwi_report(k); } -/* - * The descriptor declares the whole frame as one item of 0x80000 bits. - * hid-core refuses a Report Size above 16384, so clamp it: the driver reads the - * raw frames and never looks at the field layout. - */ -static const __u8 *kiwi_report_fixup(struct hid_device *hdev, __u8 *rdesc, - unsigned int *rsize) -{ - unsigned int i = 0; - - while (i < *rsize) { - u8 prefix = rdesc[i]; - unsigned int n = prefix & 3, j; - u32 val = 0; - - if (prefix == 0xfe) { /* long item */ - if (i + 1 >= *rsize) - break; - i += 3 + rdesc[i + 1]; - continue; - } - if (n == 3) - n = 4; - if (i + 1 + n > *rsize) - break; - if ((prefix & 0xfc) == 0x74 && n >= 2) { /* Global: Report Size */ - for (j = 0; j < n; j++) - val |= (u32)rdesc[i + 1 + j] << (8 * j); - if (val > KIWI_MAX_REPORT_SIZE) { - hid_info(hdev, "clamping Report Size %u to %u\n", - val, KIWI_MAX_REPORT_SIZE); - for (j = 0; j < n; j++) - rdesc[i + 1 + j] = (KIWI_MAX_REPORT_SIZE >> (8 * j)) & 0xff; - } - } - i += 1 + n; - } - return rdesc; -} - static int kiwi_raw_event(struct hid_device *hdev, struct hid_report *report, u8 *data, int size) { struct kiwi *k = hid_get_drvdata(hdev); - unsigned long flags; - if (size != KIWI_FRAME_LEN) { - hid_warn_once(hdev, "unexpected frame length %d, expected %d\n", - size, KIWI_FRAME_LEN); - return 0; - } - if (data[0] == KIWI_MAGIC) { - spin_lock_irqsave(&k->lock, flags); + if (size >= KIWI_MIN_FRAME && data[0] == KIWI_MAGIC) kiwi_frame(k, data); - spin_unlock_irqrestore(&k->lock, flags); - } return 0; } static int kiwi_input_open(struct input_dev *input) { struct kiwi *k = input_get_drvdata(input); - unsigned long flags; - spin_lock_irqsave(&k->lock, flags); k->have_base = false; - spin_unlock_irqrestore(&k->lock, flags); + 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)); } @@ -395,7 +807,6 @@ static int kiwi_probe(struct hid_device *hdev, const struct hid_device_id *id) kiwi = devm_kzalloc(&hdev->dev, sizeof(*kiwi), GFP_KERNEL); if (!kiwi) return -ENOMEM; - spin_lock_init(&kiwi->lock); hid_set_drvdata(hdev, kiwi); ret = hid_parse(hdev); @@ -409,9 +820,23 @@ static int kiwi_probe(struct hid_device *hdev, const struct hid_device_id *id) 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) }, { } @@ -423,7 +848,7 @@ static struct hid_driver kiwi_driver = { .id_table = kiwi_devices, .match = kiwi_match, .probe = kiwi_probe, - .report_fixup = kiwi_report_fixup, + .remove = kiwi_remove, .raw_event = kiwi_raw_event, }; module_hid_driver(kiwi_driver); From c8564b84d96a6d2f1f85dd35a59b584ce4bf42d6 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Thu, 19 Nov 2026 19:06:43 -0600 Subject: [PATCH 6/9] dt-bindings: input: describe DockChannel touchscreen geometry Reuse the common touchscreen properties for input range and size. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- .../bindings/input/apple,dockchannel-hid.yaml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Documentation/devicetree/bindings/input/apple,dockchannel-hid.yaml b/Documentation/devicetree/bindings/input/apple,dockchannel-hid.yaml index 61a3f48ea491e..585dd72bc5937 100644 --- a/Documentation/devicetree/bindings/input/apple,dockchannel-hid.yaml +++ b/Documentation/devicetree/bindings/input/apple,dockchannel-hid.yaml @@ -268,6 +268,16 @@ properties: the interface on and lowered to power it off during power method 2 transitions. + touchscreen-size-x: + $ref: touchscreen/touchscreen.yaml#/properties/touchscreen-size-x + + touchscreen-size-y: + $ref: touchscreen/touchscreen.yaml#/properties/touchscreen-size-y + + touchscreen-x-mm: true + + touchscreen-y-mm: true + mtp: type: object additionalProperties: false From 7a262df0306daa9a9397919a1e87bde453b5a9f2 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Thu, 19 Nov 2026 19:06:50 -0600 Subject: [PATCH 7/9] HID: use DT geometry and power method for DockChannel touch Pass the interface firmware node to its HID child. Refuse a touchscreen without a valid range and physical size. Use power method 2 and a dedicated reset for line ownership instead of the interface name. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- drivers/hid/dockchannel-hid/dockchannel-hid.c | 46 +++++++++---------- drivers/hid/hid-apple-mtp-kiwi.c | 37 +++++++++------ 2 files changed, 47 insertions(+), 36 deletions(-) diff --git a/drivers/hid/dockchannel-hid/dockchannel-hid.c b/drivers/hid/dockchannel-hid/dockchannel-hid.c index 17b05ae4da936..bea2845ab2c74 100644 --- a/drivers/hid/dockchannel-hid/dockchannel-hid.c +++ b/drivers/hid/dockchannel-hid/dockchannel-hid.c @@ -29,9 +29,9 @@ #define COMMAND_TIMEOUT_MS 1000 #define START_TIMEOUT_MS 2000 -/* The MTP bootloader writes the 1.3 MB kiwi image over the touch IC SPI: seconds, not 1 s. */ -#define KIWI_CMD_TIMEOUT_MS 10000 -#define KIWI_START_TIMEOUT_MS 10000 +/* The MTP bootloader needs longer for a firmware image written over SPI. */ +#define PM2_CMD_TIMEOUT_MS 10000 +#define PM2_START_TIMEOUT_MS 10000 #define MAX_INTERFACES 16 #define MAX_IFACE_GPIOS 4 @@ -218,8 +218,8 @@ struct dockchannel_hid { struct device_link *helper_link; struct apple_smc *afe_smc; bool stopping; - /* The kiwi firmware load is running: comm commands get the long timeout */ - bool kiwi_loading; + /* Commands share the extended timeout during a power method 2 upload. */ + bool slow_fw_loading; bool inherited_mtp; /* Between the system sleep suspend and resume callbacks */ bool suspended; @@ -487,8 +487,8 @@ static int dchid_cmd(struct dchid_iface *iface, u32 type, u32 req, if (ret < 0) goto done; - timeout_ms = READ_ONCE(iface->dchid->kiwi_loading) ? - KIWI_CMD_TIMEOUT_MS : COMMAND_TIMEOUT_MS; + timeout_ms = READ_ONCE(iface->dchid->slow_fw_loading) ? + PM2_CMD_TIMEOUT_MS : COMMAND_TIMEOUT_MS; if (!wait_for_completion_timeout(&iface->out_complete, msecs_to_jiffies(timeout_ms))) { dev_err(iface->dchid->dev, "output report 0x%x to iface %d (%s) timed out\n", report_id, iface->index, iface->name); @@ -825,7 +825,7 @@ static int dchid_start_interface(struct dchid_iface *iface) { void *fw; size_t size; - bool kiwi_own; + bool own_lines; int ret; if (iface->starting) { @@ -833,11 +833,10 @@ static int dchid_start_interface(struct dchid_iface *iface) return -EINPROGRESS; } - /* Only a kiwi with a reset line of its own may power-cycle it; the AFE reset is shared. */ - kiwi_own = !strcmp(iface->name, "kiwi") && - iface->power_method == DCHID_POWER_METHOD_2 && - !iface->firmware_managed_reset && - iface->reset != iface->dchid->afe_reset; + /* Only an interface with its own reset line may power-cycle it. */ + own_lines = iface->power_method == DCHID_POWER_METHOD_2 && + !iface->firmware_managed_reset && + iface->reset != iface->dchid->afe_reset; dev_dbg(iface->dchid->dev, "Starting interface %s\n", iface->name); @@ -865,8 +864,8 @@ static int dchid_start_interface(struct dchid_iface *iface) * before its firmware goes out (its reset line is active low * in the device tree). */ - if (kiwi_own) { - WRITE_ONCE(iface->dchid->kiwi_loading, true); + if (own_lines) { + WRITE_ONCE(iface->dchid->slow_fw_loading, true); ret = dchid_set_own_lines(iface, DCHID_POWER_STATE_OFF); if (ret < 0) goto err; @@ -881,7 +880,7 @@ static int dchid_start_interface(struct dchid_iface *iface) dev_dbg(iface->dchid->dev, "Sending firmware for %s\n", iface->name); /* Retained MTP must see AFE reset throughout firmware upload. */ if (iface->dchid->inherited_mtp && - !kiwi_own && + !own_lines && iface->power_method == DCHID_POWER_METHOD_2) { if (iface->firmware_managed_reset) { ret = -EINVAL; @@ -905,7 +904,7 @@ static int dchid_start_interface(struct dchid_iface *iface) * The retained session is already off while reset is asserted; * kiwi always power-cycles its own lines. */ - if (!iface->dchid->inherited_mtp || kiwi_own) { + if (!iface->dchid->inherited_mtp || own_lines) { ret = dchid_pm2_set_power(iface, DCHID_POWER_STATE_OFF); if (ret < 0) goto err; @@ -921,13 +920,13 @@ static int dchid_start_interface(struct dchid_iface *iface) } } - if (kiwi_own) - WRITE_ONCE(iface->dchid->kiwi_loading, false); + if (own_lines) + WRITE_ONCE(iface->dchid->slow_fw_loading, false); return 0; err: - if (kiwi_own) - WRITE_ONCE(iface->dchid->kiwi_loading, false); + if (own_lines) + WRITE_ONCE(iface->dchid->slow_fw_loading, false); iface->starting = false; return ret; } @@ -969,8 +968,8 @@ static int dchid_open(struct hid_device *hdev) if (ret < 0) return ret; - timeout_ms = !strcmp(iface->name, "kiwi") ? - KIWI_START_TIMEOUT_MS : START_TIMEOUT_MS; + timeout_ms = iface->power_method == DCHID_POWER_METHOD_2 ? + PM2_START_TIMEOUT_MS : START_TIMEOUT_MS; if (!wait_for_completion_timeout(&iface->ready, msecs_to_jiffies(timeout_ms))) { dev_err(iface->dchid->dev, "iface %s start timed out\n", iface->name); return -ETIMEDOUT; @@ -1137,6 +1136,7 @@ static void dchid_create_interface_work(struct work_struct *ws) } hid->dev.parent = iface->dchid->dev; + device_set_node(&hid->dev, of_fwnode_handle(iface->of_node)); hid->driver_data = iface; iface->hid = hid; diff --git a/drivers/hid/hid-apple-mtp-kiwi.c b/drivers/hid/hid-apple-mtp-kiwi.c index 9eaca6bfd0c19..995e00d2b86f4 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 @@ -48,11 +49,6 @@ #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 /* counts: reference frames shift the mean by 137 to 899, idle frames by under 10 */ #define KIWI_GLITCH 64 @@ -96,6 +92,8 @@ struct kiwi_contact { struct kiwi { struct input_dev *input; + int size_x, size_y; + int res_x, res_y; 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 */ @@ -546,11 +544,11 @@ static void kiwi_report(struct kiwi *k) 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); + x = div_s64((s64)c->x * k->size_x, KIWI_COLS * KIWI_FP); + y = div_s64((s64)c->y * k->size_y, 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); + x = clamp(x, 0, k->size_x - 1); + y = clamp(k->size_y - 1 - y, 0, k->size_y - 1); 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); @@ -789,10 +787,10 @@ static struct input_dev *kiwi_alloc_input(struct hid_device *hdev, struct kiwi * 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_POSITION_X, 0, k->size_x - 1, 0, 0); + input_set_abs_params(input, ABS_MT_POSITION_Y, 0, k->size_y - 1, 0, 0); + input_abs_set_res(input, ABS_MT_POSITION_X, k->res_x); + input_abs_set_res(input, ABS_MT_POSITION_Y, k->res_y); 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; @@ -802,11 +800,24 @@ static struct input_dev *kiwi_alloc_input(struct hid_device *hdev, struct kiwi * static int kiwi_probe(struct hid_device *hdev, const struct hid_device_id *id) { struct kiwi *kiwi; + u32 size_x, size_y, mm_x, mm_y; int ret; + if (device_property_read_u32(&hdev->dev, "touchscreen-size-x", &size_x) || + device_property_read_u32(&hdev->dev, "touchscreen-size-y", &size_y) || + device_property_read_u32(&hdev->dev, "touchscreen-x-mm", &mm_x) || + device_property_read_u32(&hdev->dev, "touchscreen-y-mm", &mm_y) || + size_x < 2 || size_x > INT_MAX || size_y < 2 || size_y > INT_MAX || + !mm_x || mm_x > size_x || !mm_y || mm_y > size_y) + return dev_err_probe(&hdev->dev, -EINVAL, "invalid touchscreen geometry\n"); + kiwi = devm_kzalloc(&hdev->dev, sizeof(*kiwi), GFP_KERNEL); if (!kiwi) return -ENOMEM; + kiwi->size_x = size_x; + kiwi->size_y = size_y; + kiwi->res_x = DIV_ROUND_CLOSEST(size_x, mm_x); + kiwi->res_y = DIV_ROUND_CLOSEST(size_y, mm_y); hid_set_drvdata(hdev, kiwi); ret = hid_parse(hdev); From b6993a22c845fe44e0d99edc5c7a766f194c074c Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Thu, 19 Nov 2026 19:06:59 -0600 Subject: [PATCH 8/9] arm64: dts: apple: describe the K114c touchscreen size The 3024-by-1986 panel has 254 pixels per inch, which yields 302 by 199 mm rounded to whole millimeters. The DTB exposes all four values. Link: https://support.apple.com/en-us/148301 Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- arch/arm64/boot/dts/apple/t6050-k114c.dts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/arm64/boot/dts/apple/t6050-k114c.dts b/arch/arm64/boot/dts/apple/t6050-k114c.dts index 3112915bc719d..c57f3638a386e 100644 --- a/arch/arm64/boot/dts/apple/t6050-k114c.dts +++ b/arch/arm64/boot/dts/apple/t6050-k114c.dts @@ -617,5 +617,9 @@ apple,power-method = <2>; apple,reset-gpios = <&nub_gpio0 25 GPIO_ACTIVE_LOW>; apple,power-gpios = <&nub_gpio0 26 GPIO_ACTIVE_HIGH>; + touchscreen-size-x = <3024>; + touchscreen-size-y = <1986>; + touchscreen-x-mm = <302>; + touchscreen-y-mm = <199>; status = "okay"; }; From 06d61d6700f3707bfdacc89c1fcc3fdc7b9e2907 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Thu, 19 Nov 2026 20:15:40 -0600 Subject: [PATCH 9/9] HID: apple-mtp-kiwi: accept only the measured frame size Only the 17,861-byte frame matches the measured Kiwi layout. Reject other sizes before decoding, without matching on a machine ID. The extracted report callback rejects 17,860 and accepts 17,861 bytes. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- drivers/hid/hid-apple-mtp-kiwi.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/hid/hid-apple-mtp-kiwi.c b/drivers/hid/hid-apple-mtp-kiwi.c index 995e00d2b86f4..963df2b64e91b 100644 --- a/drivers/hid/hid-apple-mtp-kiwi.c +++ b/drivers/hid/hid-apple-mtp-kiwi.c @@ -46,7 +46,7 @@ #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 #define KIWI_FP 256 @@ -748,7 +748,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; }