From b781812c9e866b190b6a44087265e84acfd07b2e Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 27 Nov 2026 20:01:48 +1100 Subject: [PATCH] HID: dockchannel-hid: let a TP26 trackpad click wake the system dockchannel-hid wakes the system on a keyboard report or on a multi-touch report with the trackpad clicked, and recognises the click only in the MTP report (ID 0x75, button at byte 23). Newer trackpad firmware ("SensingAlgsTrackpad26C", the frames hid-magicmouse calls TP26, on the M5 MacBook Pros) sends no report ID: its data frames are a list of stream records, and the click comes as a 32-bit word in the button stream. Such a click never woke the system; only the keyboard did. Recognise the TP26 button stream too. dchid_tp26_clicked() walks the records of a data frame as hid-magicmouse does and reports the button word's bit 0. Touches still do not wake the system. Add KUnit cases for the helper: a click, no click, another stream with the bit set, a record that runs past the frame, a non-data frame, a non-TP26 frame and a short frame. Signed-off-by: Ryan Murray --- .../dockchannel-hid-protocol-test.c | 53 +++++++++++++++++++ .../dockchannel-hid-protocol.h | 38 +++++++++++++ drivers/hid/dockchannel-hid/dockchannel-hid.c | 13 +++-- 3 files changed, 99 insertions(+), 5 deletions(-) diff --git a/drivers/hid/dockchannel-hid/dockchannel-hid-protocol-test.c b/drivers/hid/dockchannel-hid/dockchannel-hid-protocol-test.c index 47f91ca6ee493..36854bdb0dae6 100644 --- a/drivers/hid/dockchannel-hid/dockchannel-hid-protocol-test.c +++ b/drivers/hid/dockchannel-hid/dockchannel-hid-protocol-test.c @@ -93,12 +93,65 @@ static void dchid_pm1_fallback_test(struct kunit *test) false, false, true)); } +/* A TP26 data frame: header, one record of @id carrying the 32-bit @word. */ +static size_t dchid_tp26_frame(u8 *buf, const u8 *id, u32 word, u16 len) +{ + size_t pos = DCHID_TP26_HEADER_SIZE; + + memset(buf, 0, DCHID_TP26_HEADER_SIZE); + buf[0] = 0x5a; + buf[1] = 0x01; + buf[2] = 2; + buf[pos] = 0x02; + memcpy(buf + pos + 1, id, 16); + buf[pos + 17] = 0x02; + put_unaligned_le16(len, buf + pos + 20); + put_unaligned_le32(word, buf + pos + DCHID_TP26_RECORD_HDR); + return pos + DCHID_TP26_RECORD_HDR + 4; +} + +static void dchid_tp26_clicked_test(struct kunit *test) +{ + static const u8 contacts_id[16] = { + 0x91, 0x62, 0x0e, 0x00, 0x8a, 0x5d, 0x81, 0xd5, + 0x7e, 0xde, 0x54, 0xa8, 0x63, 0x1f, 0xdf, 0x17, + }; + u8 buf[64]; + size_t n; + + n = dchid_tp26_frame(buf, dchid_tp26_button_id, 1, 4); + KUNIT_EXPECT_TRUE(test, dchid_tp26_clicked(buf, n)); + + n = dchid_tp26_frame(buf, dchid_tp26_button_id, 0, 4); + KUNIT_EXPECT_FALSE(test, dchid_tp26_clicked(buf, n)); + + /* Another stream with the bit set is not a click */ + n = dchid_tp26_frame(buf, contacts_id, 1, 4); + KUNIT_EXPECT_FALSE(test, dchid_tp26_clicked(buf, n)); + + /* A record that runs past the frame */ + n = dchid_tp26_frame(buf, dchid_tp26_button_id, 1, 40); + KUNIT_EXPECT_FALSE(test, dchid_tp26_clicked(buf, n)); + + /* Not a data frame, or not TP26 at all */ + n = dchid_tp26_frame(buf, dchid_tp26_button_id, 1, 4); + buf[2] = 1; + KUNIT_EXPECT_FALSE(test, dchid_tp26_clicked(buf, n)); + buf[2] = 2; + buf[0] = 0x75; + KUNIT_EXPECT_FALSE(test, dchid_tp26_clicked(buf, n)); + + /* Too short for the header */ + KUNIT_EXPECT_FALSE(test, dchid_tp26_clicked(buf, DCHID_TP26_HEADER_SIZE - 1)); +} + static struct kunit_case dchid_protocol_test_cases[] = { KUNIT_CASE(dchid_pm1_command_test), KUNIT_CASE(dchid_pm2_will_change_test), KUNIT_CASE(dchid_pm2_has_changed_test), KUNIT_CASE(dchid_pm2_power_off_test), KUNIT_CASE(dchid_pm1_fallback_test), + KUNIT_CASE(dchid_tp26_clicked_test), {} }; diff --git a/drivers/hid/dockchannel-hid/dockchannel-hid-protocol.h b/drivers/hid/dockchannel-hid/dockchannel-hid-protocol.h index 96d3c07f1cc18..d58e30c00a0c9 100644 --- a/drivers/hid/dockchannel-hid/dockchannel-hid-protocol.h +++ b/drivers/hid/dockchannel-hid/dockchannel-hid-protocol.h @@ -8,6 +8,7 @@ #include #include #include +#include /* * Interface power commands go to the comm interface as SET_REPORT feature @@ -65,4 +66,41 @@ static inline void dchid_pm2_command(u8 iface, u8 state, bool has_changed, cmd[4] = has_changed; } +/* + * Newer trackpad firmware ("SensingAlgsTrackpad26C", hid-magicmouse's TP26 + * frames) sends no report ID: a frame is 0x5a 0x01, a frame type (2 for data), + * a header to offset 31, then records of 0x02, a 16-byte stream id, 0x02, two + * type bytes, a little-endian 16-bit length and the payload. The button + * stream's payload is a 32-bit word with bit 0 set while the pad is clicked. + */ +#define DCHID_TP26_HEADER_SIZE 31 +#define DCHID_TP26_RECORD_HDR 22 + +static const u8 dchid_tp26_button_id[16] = { + 0x91, 0x62, 0x0e, 0x00, 0x8a, 0x5d, 0x81, 0xd5, + 0xc2, 0x87, 0x93, 0x63, 0x28, 0x29, 0xc8, 0xbf, +}; + +/* Whether a TP26 data frame carries a button stream record that says clicked. */ +static inline bool dchid_tp26_clicked(const u8 *data, size_t size) +{ + size_t pos; + + if (size < DCHID_TP26_HEADER_SIZE || data[0] != 0x5a || data[1] != 0x01 || + data[2] != 2) + return false; + for (pos = DCHID_TP26_HEADER_SIZE; pos + DCHID_TP26_RECORD_HDR <= size;) { + u16 len = get_unaligned_le16(data + pos + 20); + + if (data[pos] != 0x02 || data[pos + 17] != 0x02 || + len > size - pos - DCHID_TP26_RECORD_HDR) + return false; + if (len >= 4 && !memcmp(data + pos + 1, dchid_tp26_button_id, + sizeof(dchid_tp26_button_id))) + return get_unaligned_le32(data + pos + DCHID_TP26_RECORD_HDR) & 1; + pos += DCHID_TP26_RECORD_HDR + len; + } + return false; +} + #endif /* _DOCKCHANNEL_HID_PROTOCOL_H */ diff --git a/drivers/hid/dockchannel-hid/dockchannel-hid.c b/drivers/hid/dockchannel-hid/dockchannel-hid.c index 4a622ceced571..7291c0d4b21fd 100644 --- a/drivers/hid/dockchannel-hid/dockchannel-hid.c +++ b/drivers/hid/dockchannel-hid/dockchannel-hid.c @@ -1511,8 +1511,9 @@ static void dchid_handle_ack(struct dchid_iface *iface, struct dchid_hdr *hdr, v /* * Whether a report should wake the system: any keyboard report, and a - * multi-touch report only while the trackpad is clicked. Touches, the - * accelerometer and the other interfaces do not wake it. + * multi-touch report only while the trackpad is clicked, in the MTP report or + * in a TP26 frame's button stream. Touches, the accelerometer and the other + * interfaces do not wake it. */ static bool dchid_wake_report(struct dchid_iface *iface, void *data) { @@ -1523,9 +1524,11 @@ static bool dchid_wake_report(struct dchid_iface *iface, void *data) return false; if (!strcmp(iface->name, "keyboard")) return shdr->length > 0; - return !strcmp(iface->name, "multi-touch") && - shdr->length > MT_REPORT_BUTTON && payload[0] == MT_REPORT_ID && - (payload[MT_REPORT_BUTTON] & 1); + if (strcmp(iface->name, "multi-touch")) + return false; + if (shdr->length > MT_REPORT_BUTTON && payload[0] == MT_REPORT_ID) + return payload[MT_REPORT_BUTTON] & 1; + return dchid_tp26_clicked(payload, shdr->length); } /* Return the complete frame size, or reject a lost framing boundary. */