From 52bc8b1017416d27f9a507aad434e03f6c5815f7 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Sun, 15 Nov 2026 19:25:26 +0000 Subject: [PATCH 1/6] dt-bindings: input: apple,dockchannel-hid: add the T6050 compatible apple,t6050-dockchannel-hid selects the MTP receive ring, as apple,t6041-dockchannel-hid does for the M4 Pro. A board that uses it lists the second DART stream in iommus. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- .../devicetree/bindings/input/apple,dockchannel-hid.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Documentation/devicetree/bindings/input/apple,dockchannel-hid.yaml b/Documentation/devicetree/bindings/input/apple,dockchannel-hid.yaml index b6c9e727e5a67..5ce60d94fecba 100644 --- a/Documentation/devicetree/bindings/input/apple,dockchannel-hid.yaml +++ b/Documentation/devicetree/bindings/input/apple,dockchannel-hid.yaml @@ -32,7 +32,9 @@ properties: oneOf: - const: apple,dockchannel-hid - items: - - const: apple,t6041-dockchannel-hid + - enum: + - apple,t6041-dockchannel-hid + - apple,t6050-dockchannel-hid - const: apple,dockchannel-hid reg: From dc1dcc246e1d903c36c3d6864d8929201f6965a6 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Sun, 15 Nov 2026 19:25:26 +0000 Subject: [PATCH 2/6] HID: dockchannel-hid: receive T6050 MTP reports through the receive ring On a 14-inch M5 Max (K114c) the touch screen interface sends 17872-byte and 448-byte reports about 120 times a second, also when nothing is touched. Reading them word by word from the MMIO FIFO kept the receive IRQ thread at 51 to 59 percent of one core. Register the 20 MiB receive ring for nodes that use the T6050 compatible, as the T6041 path already does with a smaller one, and keep the T6050 reset and GPIO handling. A T6050 node still has the FIFO, so if the firmware refuses the ring it warns and stays on the FIFO: the ring stays published and marked registered so a ring the firmware did accept still drains, and the sticky error is cleared so later interface starts do not fail. The T6041 ring is its only receive path, so there a refusal still fails the interface start, as before. The refusal decision is a helper in dockchannel-hid-protocol.h with three KUnit cases. A failed ring allocation at probe still fails the probe. The parser KUnit tests build their own ring and now set its size. The ring needs the second MTP DART stream, so this must land after the change that lists it. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- .../dockchannel-hid-parser-test.c | 2 + .../dockchannel-hid-protocol-test.c | 20 ++++++ .../dockchannel-hid-protocol.h | 11 ++++ drivers/hid/dockchannel-hid/dockchannel-hid.c | 61 +++++++++++++------ 4 files changed, 77 insertions(+), 17 deletions(-) diff --git a/drivers/hid/dockchannel-hid/dockchannel-hid-parser-test.c b/drivers/hid/dockchannel-hid/dockchannel-hid-parser-test.c index 64fde481641cd..f7cd70a22e957 100644 --- a/drivers/hid/dockchannel-hid/dockchannel-hid-parser-test.c +++ b/drivers/hid/dockchannel-hid/dockchannel-hid-parser-test.c @@ -210,6 +210,8 @@ static void *dchid_test_ring(struct kunit *test, u32 consumer, u32 producer) dchid->ring_read = consumer; dchid->ring_registered = true; dchid->use_ring = true; + dchid->use_rx_ring = true; + dchid->ring_size = DCHID_RING_SIZE; return dchid->ring + DCHID_RING_HEADER_SIZE; } diff --git a/drivers/hid/dockchannel-hid/dockchannel-hid-protocol-test.c b/drivers/hid/dockchannel-hid/dockchannel-hid-protocol-test.c index 2c4d18a9177a2..8af84b43d52a1 100644 --- a/drivers/hid/dockchannel-hid/dockchannel-hid-protocol-test.c +++ b/drivers/hid/dockchannel-hid/dockchannel-hid-protocol-test.c @@ -62,11 +62,31 @@ static void dchid_pm2_power_off_test(struct kunit *test) KUNIT_EXPECT_MEMEQ(test, cmd, expected, sizeof(expected)); } +static void dchid_ring_refusal_keeps_fifo_test(struct kunit *test) +{ + /* A node with an MMIO FIFO receive path of its own may fall back to it. */ + KUNIT_EXPECT_TRUE(test, dchid_ring_refused(-ETIMEDOUT, true)); +} + +static void dchid_ring_refusal_fails_start_without_fallback_test(struct kunit *test) +{ + /* The T6041 ring is its only receive path: a refusal must fail the start. */ + KUNIT_EXPECT_FALSE(test, dchid_ring_refused(-ETIMEDOUT, false)); +} + +static void dchid_ring_success_is_not_a_refusal_test(struct kunit *test) +{ + KUNIT_EXPECT_FALSE(test, dchid_ring_refused(0, true)); +} + 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_ring_refusal_keeps_fifo_test), + KUNIT_CASE(dchid_ring_refusal_fails_start_without_fallback_test), + KUNIT_CASE(dchid_ring_success_is_not_a_refusal_test), {} }; diff --git a/drivers/hid/dockchannel-hid/dockchannel-hid-protocol.h b/drivers/hid/dockchannel-hid/dockchannel-hid-protocol.h index c73c911862ac0..612cf4bf700d3 100644 --- a/drivers/hid/dockchannel-hid/dockchannel-hid-protocol.h +++ b/drivers/hid/dockchannel-hid/dockchannel-hid-protocol.h @@ -52,4 +52,15 @@ static inline void dchid_pm2_command(u8 iface, u8 state, bool has_changed, cmd[4] = has_changed; } +/* + * Whether a failed receive-ring command is to be absorbed. Only a node that + * has an MMIO FIFO receive path of its own (fifo_fallback) may fall back to + * it; the T6041 ring is its only receive path and its failure must fail the + * interface start. + */ +static inline bool dchid_ring_refused(int ret, bool fifo_fallback) +{ + return ret < 0 && fifo_fallback; +} + #endif /* _DOCKCHANNEL_HID_PROTOCOL_H */ diff --git a/drivers/hid/dockchannel-hid/dockchannel-hid.c b/drivers/hid/dockchannel-hid/dockchannel-hid.c index 9e0877ca2d496..e5db9e71cc8e7 100644 --- a/drivers/hid/dockchannel-hid/dockchannel-hid.c +++ b/drivers/hid/dockchannel-hid/dockchannel-hid.c @@ -213,9 +213,13 @@ struct dockchannel_hid { struct gpio_desc *afe_reset; struct mutex ring_mutex; + /* The T8132 transport changes reset and GPIO handling as well as receives. */ bool use_ring; + /* Receive through a host-memory ring while keeping the local reset paths. */ + bool use_rx_ring; bool ring_registered; int ring_error; + size_t ring_size; void *ring; dma_addr_t ring_dma; u32 ring_read; @@ -544,28 +548,40 @@ static int dchid_register_ring(struct dockchannel_hid *dchid) } __packed msg = { .cmd = CMD_REGISTER_RING, .addr = dchid->ring_dma, - .size = DCHID_RING_SIZE, + .size = dchid->ring_size, }; /* Observed in both macOS and standalone T8132 bringup before 0x91. */ u8 prepare[] = { CMD_HOST_STATE, HOST_STATE_AWAKE }; - int ret = 0; + int ret = 0, cmd_ret; + bool fifo_fallback = !dchid->use_ring; - if (!dchid->use_ring) + if (!dchid->use_rx_ring) return 0; mutex_lock(&dchid->ring_mutex); ret = dchid->ring_error; if (!dchid->ring_registered) { - ret = dchid_comm_cmd(dchid, prepare, sizeof(prepare)); - if (ret < 0) { - mutex_unlock(&dchid->ring_mutex); - return ret; + cmd_ret = dchid_comm_cmd(dchid, prepare, sizeof(prepare)); + if (cmd_ret >= 0) { + /* The registration ACK itself arrives through the ring. */ + dma_wmb(); + WRITE_ONCE(dchid->ring_registered, true); + cmd_ret = dchid_comm_cmd(dchid, &msg, sizeof(msg)); + dchid->ring_error = cmd_ret; + } + ret = cmd_ret; + if (dchid_ring_refused(cmd_ret, fifo_fallback)) { + /* + * Stay on the FIFO. Keep the ring marked registered, so a + * ring the firmware did accept still drains, and clear the + * sticky error so later interface starts do not fail. + */ + WRITE_ONCE(dchid->ring_registered, true); + dchid->ring_error = 0; + dev_warn(dchid->dev, + "receive ring refused (%d), staying on FIFO\n", cmd_ret); + ret = 0; } - /* The registration ACK itself arrives through the ring. */ - dma_wmb(); - WRITE_ONCE(dchid->ring_registered, true); - ret = dchid_comm_cmd(dchid, &msg, sizeof(msg)); - dchid->ring_error = ret; } mutex_unlock(&dchid->ring_mutex); return ret; @@ -1450,7 +1466,7 @@ static int dchid_dispatch(struct dockchannel_hid *dchid, struct dchid_hdr *hdr, return 0; } - if (dchid->use_ring && hdr->channel == DCHID_CHANNEL_NOTIFY) { + if (dchid->use_rx_ring && hdr->channel == DCHID_CHANNEL_NOTIFY) { /* A FIFO notification has no application subheader or report ID. */ return fifo && !hdr->length && hdr->iface == IFACE_COMM; } @@ -1496,7 +1512,7 @@ static void dchid_ring_copy(struct dockchannel_hid *dchid, void *dst, u32 offset, size_t length) { size_t first = min_t(size_t, length, - DCHID_RING_SIZE - DCHID_RING_HEADER_SIZE - offset); + dchid->ring_size - DCHID_RING_HEADER_SIZE - offset); void *data = dchid->ring + DCHID_RING_HEADER_SIZE; memcpy(dst, data + offset, first); @@ -1505,7 +1521,7 @@ static void dchid_ring_copy(struct dockchannel_hid *dchid, void *dst, static int dchid_drain_ring(struct dockchannel_hid *dchid) { - const u32 capacity = DCHID_RING_SIZE - DCHID_RING_HEADER_SIZE; + const u32 capacity = dchid->ring_size - DCHID_RING_HEADER_SIZE; u32 *indices = dchid->ring; u32 producer, consumer, available; int size; @@ -1662,6 +1678,17 @@ static int dockchannel_hid_probe(struct platform_device *pdev) mutex_init(&dchid->ring_mutex); /* The M4 Pro (J616s) MTP uses the same 26.6.2 firmware transport as T8132. */ dchid->use_ring = of_device_is_compatible(dev->of_node, "apple,t6041-dockchannel-hid"); + /* + * The T6050 MTP keeps this platform's reset and GPIO paths, but macOS gives + * its large raw touch reports a 20 MiB host read FIFO. A board opts in with + * the t6050 compatible, once its MTP DART streams 0 and 1 are described. + * Try that receive transport and fall back to the MMIO FIFO if the + * firmware refuses it. + */ + dchid->use_rx_ring = dchid->use_ring || + of_device_is_compatible(dev->of_node, "apple,t6050-dockchannel-hid"); + dchid->ring_size = of_device_is_compatible(dev->of_node, "apple,t6050-dockchannel-hid") ? + 20 * SZ_1M : DCHID_RING_SIZE; ret = dchid_bind_afe_smc(dchid); if (ret) @@ -1782,8 +1809,8 @@ static int dockchannel_hid_probe(struct platform_device *pdev) if (ret) dev_warn(dev, "Failed to set up wakeup: %d\n", ret); - if (dchid->use_ring) { - dchid->ring = dmam_alloc_coherent(dev, DCHID_RING_SIZE, &dchid->ring_dma, + if (dchid->use_rx_ring) { + dchid->ring = dmam_alloc_coherent(dev, dchid->ring_size, &dchid->ring_dma, GFP_KERNEL); if (!dchid->ring) return -ENOMEM; From e3188ea02108991efef1d5c127e469101a0d5943 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Sun, 15 Nov 2026 19:25:26 +0000 Subject: [PATCH 3/6] arm64: dts: apple: t6050-k114-k116: use the T6050 DockChannel HID compatible Select the MTP receive ring on the K114 and K116 boards, whose MTP DART streams 0 and 1 are described. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- arch/arm64/boot/dts/apple/t6050-k114-k116.dtsi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/arm64/boot/dts/apple/t6050-k114-k116.dtsi b/arch/arm64/boot/dts/apple/t6050-k114-k116.dtsi index f7cedd1a4c854..d7c327271539b 100644 --- a/arch/arm64/boot/dts/apple/t6050-k114-k116.dtsi +++ b/arch/arm64/boot/dts/apple/t6050-k114-k116.dtsi @@ -196,7 +196,7 @@ */ &mtp_dockchannel { mtp_hid: input@8000 { - compatible = "apple,dockchannel-hid"; + compatible = "apple,t6050-dockchannel-hid", "apple,dockchannel-hid"; reg = <0x8000 0x1000>, <0xc000 0x1000>, <0x0 0x1000>, <0x4000 0x1000>; reg-names = "config", "data", "rmt-config", "rmt-data"; From 7e027e381c11286d68683ba3c57580320ac80bb8 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Mon, 16 Nov 2026 12:24:31 -0600 Subject: [PATCH 4/6] HID: dockchannel-hid: retry the receive ring when host state fails When CMD_HOST_STATE failed on a node with an MMIO FIFO path, CMD_REGISTER_RING was never sent, yet the failure was treated as a refused ring: ring_registered was set and the error cleared, so no later interface start tried again and the interface stayed on the FIFO for the session. Only a refused CMD_REGISTER_RING now keeps the FIFO; a failed preparation leaves the ring unregistered, logs it and lets the next interface start retry. The decision is dchid_ring_stays_on_fifo() in the protocol header with two KUnit cases. Found in review by a K114s owner. Not booted. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- .../dockchannel-hid-protocol-test.c | 15 +++++++++++++++ .../dockchannel-hid/dockchannel-hid-protocol.h | 10 ++++++++++ drivers/hid/dockchannel-hid/dockchannel-hid.c | 16 ++++++++++++++-- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/drivers/hid/dockchannel-hid/dockchannel-hid-protocol-test.c b/drivers/hid/dockchannel-hid/dockchannel-hid-protocol-test.c index 8af84b43d52a1..613996225865c 100644 --- a/drivers/hid/dockchannel-hid/dockchannel-hid-protocol-test.c +++ b/drivers/hid/dockchannel-hid/dockchannel-hid-protocol-test.c @@ -79,6 +79,19 @@ static void dchid_ring_success_is_not_a_refusal_test(struct kunit *test) KUNIT_EXPECT_FALSE(test, dchid_ring_refused(0, true)); } +static void dchid_ring_failed_prepare_is_not_a_refusal_test(struct kunit *test) +{ + /* CMD_REGISTER_RING was never sent, so the ring is not refused: stay unregistered and retry. */ + KUNIT_EXPECT_FALSE(test, dchid_ring_stays_on_fifo(false, -ETIMEDOUT, true)); +} + +static void dchid_ring_refused_register_stays_on_fifo_test(struct kunit *test) +{ + KUNIT_EXPECT_TRUE(test, dchid_ring_stays_on_fifo(true, -ETIMEDOUT, true)); + KUNIT_EXPECT_FALSE(test, dchid_ring_stays_on_fifo(true, -ETIMEDOUT, false)); + KUNIT_EXPECT_FALSE(test, dchid_ring_stays_on_fifo(true, 0, true)); +} + static struct kunit_case dchid_protocol_test_cases[] = { KUNIT_CASE(dchid_pm1_command_test), KUNIT_CASE(dchid_pm2_will_change_test), @@ -87,6 +100,8 @@ static struct kunit_case dchid_protocol_test_cases[] = { KUNIT_CASE(dchid_ring_refusal_keeps_fifo_test), KUNIT_CASE(dchid_ring_refusal_fails_start_without_fallback_test), KUNIT_CASE(dchid_ring_success_is_not_a_refusal_test), + KUNIT_CASE(dchid_ring_failed_prepare_is_not_a_refusal_test), + KUNIT_CASE(dchid_ring_refused_register_stays_on_fifo_test), {} }; diff --git a/drivers/hid/dockchannel-hid/dockchannel-hid-protocol.h b/drivers/hid/dockchannel-hid/dockchannel-hid-protocol.h index 612cf4bf700d3..64ff7ba27e221 100644 --- a/drivers/hid/dockchannel-hid/dockchannel-hid-protocol.h +++ b/drivers/hid/dockchannel-hid/dockchannel-hid-protocol.h @@ -63,4 +63,14 @@ static inline bool dchid_ring_refused(int ret, bool fifo_fallback) return ret < 0 && fifo_fallback; } +/* + * Whether the ring stays unused for the session. Only a refused CMD_REGISTER_RING does: + * when the preparation command failed, CMD_REGISTER_RING was never sent, so nothing + * was refused and a later interface start may register the ring. + */ +static inline bool dchid_ring_stays_on_fifo(bool prepared, int ret, bool fifo_fallback) +{ + return prepared && dchid_ring_refused(ret, fifo_fallback); +} + #endif /* _DOCKCHANNEL_HID_PROTOCOL_H */ diff --git a/drivers/hid/dockchannel-hid/dockchannel-hid.c b/drivers/hid/dockchannel-hid/dockchannel-hid.c index e5db9e71cc8e7..77a73b20e3fcc 100644 --- a/drivers/hid/dockchannel-hid/dockchannel-hid.c +++ b/drivers/hid/dockchannel-hid/dockchannel-hid.c @@ -554,6 +554,7 @@ static int dchid_register_ring(struct dockchannel_hid *dchid) u8 prepare[] = { CMD_HOST_STATE, HOST_STATE_AWAKE }; int ret = 0, cmd_ret; bool fifo_fallback = !dchid->use_ring; + bool prepared; if (!dchid->use_rx_ring) return 0; @@ -562,7 +563,8 @@ static int dchid_register_ring(struct dockchannel_hid *dchid) ret = dchid->ring_error; if (!dchid->ring_registered) { cmd_ret = dchid_comm_cmd(dchid, prepare, sizeof(prepare)); - if (cmd_ret >= 0) { + prepared = cmd_ret >= 0; + if (prepared) { /* The registration ACK itself arrives through the ring. */ dma_wmb(); WRITE_ONCE(dchid->ring_registered, true); @@ -570,7 +572,7 @@ static int dchid_register_ring(struct dockchannel_hid *dchid) dchid->ring_error = cmd_ret; } ret = cmd_ret; - if (dchid_ring_refused(cmd_ret, fifo_fallback)) { + if (dchid_ring_stays_on_fifo(prepared, cmd_ret, fifo_fallback)) { /* * Stay on the FIFO. Keep the ring marked registered, so a * ring the firmware did accept still drains, and clear the @@ -581,6 +583,16 @@ static int dchid_register_ring(struct dockchannel_hid *dchid) dev_warn(dchid->dev, "receive ring refused (%d), staying on FIFO\n", cmd_ret); ret = 0; + } else if (!prepared && fifo_fallback) { + /* + * The preparation command failed and the ring was never asked for. + * The FIFO still works, so the start goes on, but the ring stays + * unregistered and the next interface start tries again. + */ + dev_warn(dchid->dev, + "receive ring not registered, host state failed (%d); using the FIFO for now\n", + cmd_ret); + ret = 0; } } mutex_unlock(&dchid->ring_mutex); From 6cfd21dcf24c7c19512b571c0cc0491bebf13744 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Sun, 15 Nov 2026 19:25:26 +0000 Subject: [PATCH 5/6] HID: dockchannel-hid: look again after a receive ring doorbell The ring is read only when a zero-length notify arrives on the FIFO, and two paths could leave a frame in the ring until the next one. dchid_dispatch() accepted the doorbell from the comm interface only, and dchid_handle_packet() drained once and re-armed the interrupt without looking again, although the drain also stops at an empty ring or an incomplete frame. Accept the doorbell from any interface. After a drain that found nothing, look once more at once; after a drain that stopped on a partial frame, look again up to two more times, 100 us apart. Only the partial-frame case sleeps: the doorbell is the only wake source and an empty ring is the common case after a doorbell that the previous drain already served, so a timer wakeup for each of those would add to the IRQ thread's load. The rechecks stop when the device is stopping, so a frame that arrives in that window of up to about 200 us is dropped at teardown. The decisions are helpers in dockchannel-hid-protocol.h with three KUnit cases, and each doorbell and drain logs its ring indices at debug level. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- .../dockchannel-hid-parser-test.c | 12 +++- .../dockchannel-hid-protocol-test.c | 35 ++++++++++ .../dockchannel-hid-protocol.h | 35 ++++++++++ drivers/hid/dockchannel-hid/dockchannel-hid.c | 65 ++++++++++++++----- 4 files changed, 130 insertions(+), 17 deletions(-) diff --git a/drivers/hid/dockchannel-hid/dockchannel-hid-parser-test.c b/drivers/hid/dockchannel-hid/dockchannel-hid-parser-test.c index f7cd70a22e957..e6c0c473e262c 100644 --- a/drivers/hid/dockchannel-hid/dockchannel-hid-parser-test.c +++ b/drivers/hid/dockchannel-hid/dockchannel-hid-parser-test.c @@ -196,6 +196,14 @@ static void dchid_unaligned_checksum_test(struct kunit *test) } } +/* dchid_drain_ring() also reports whether it stopped on a partial frame; these tests do not look at that. */ +static int dchid_test_drain(struct dockchannel_hid *dchid) +{ + bool partial; + + return dchid_drain_ring(dchid, &partial); +} + /* A ring whose next frame straddles the end of the data area. */ static void *dchid_test_ring(struct kunit *test, u32 consumer, u32 producer) { @@ -240,7 +248,7 @@ static void dchid_ring_wrap_test(struct kunit *test) memcpy(data + consumer, &frame, 12); memcpy(data, (u8 *)&frame + 12, sizeof(frame) - 12); - KUNIT_EXPECT_EQ(test, dchid_drain_ring(iface->dchid), 0); + KUNIT_EXPECT_EQ(test, dchid_test_drain(iface->dchid), 1); /* The frame arrived whole: its acknowledgment matched the pending command. */ KUNIT_EXPECT_TRUE(test, completion_done(&iface->out_complete)); KUNIT_EXPECT_EQ(test, iface->dchid->ring_read, sizeof(frame) - 12); @@ -254,7 +262,7 @@ static void dchid_ring_bad_header_test(struct kunit *test) u8 *data = dchid_test_ring(test, 0, 16); memcpy(data, &hdr, sizeof(hdr)); - KUNIT_EXPECT_EQ(test, dchid_drain_ring(iface->dchid), -EPROTO); + KUNIT_EXPECT_EQ(test, dchid_test_drain(iface->dchid), -EPROTO); KUNIT_EXPECT_EQ(test, iface->dchid->ring_read, 0U); } diff --git a/drivers/hid/dockchannel-hid/dockchannel-hid-protocol-test.c b/drivers/hid/dockchannel-hid/dockchannel-hid-protocol-test.c index 613996225865c..e35241509fa7a 100644 --- a/drivers/hid/dockchannel-hid/dockchannel-hid-protocol-test.c +++ b/drivers/hid/dockchannel-hid/dockchannel-hid-protocol-test.c @@ -79,6 +79,38 @@ static void dchid_ring_success_is_not_a_refusal_test(struct kunit *test) KUNIT_EXPECT_FALSE(test, dchid_ring_refused(0, true)); } +static void dchid_ring_doorbell_any_iface_test(struct kunit *test) +{ + /* The firmware may address the doorbell to the interface whose frame is queued. */ + KUNIT_EXPECT_TRUE(test, dchid_notify_wants_drain(true, 0, 0)); + KUNIT_EXPECT_TRUE(test, dchid_notify_wants_drain(true, 2, 0)); + KUNIT_EXPECT_TRUE(test, dchid_notify_wants_drain(true, 7, 0)); + /* A frame with a payload is not a doorbell, and ring frames never are. */ + KUNIT_EXPECT_FALSE(test, dchid_notify_wants_drain(true, 2, 4)); + KUNIT_EXPECT_FALSE(test, dchid_notify_wants_drain(false, 0, 0)); +} + +static void dchid_ring_empty_doorbell_rechecked_test(struct kunit *test) +{ + /* A doorbell that finds nothing is looked at once more, without sleeping. */ + KUNIT_EXPECT_TRUE(test, dchid_ring_retry_needed(0, false, 0)); + KUNIT_EXPECT_FALSE(test, dchid_ring_retry_needed(0, false, 1)); + KUNIT_EXPECT_EQ(test, dchid_ring_recheck_delay_us(false), 0); +} + +static void dchid_ring_partial_frame_rechecked_test(struct kunit *test) +{ + int tries; + + /* Frames drained but the next one is still being written: wait and look again. */ + for (tries = 0; tries < DCHID_RING_RECHECKS; tries++) + KUNIT_EXPECT_TRUE(test, dchid_ring_retry_needed(3, true, tries)); + KUNIT_EXPECT_FALSE(test, dchid_ring_retry_needed(3, true, DCHID_RING_RECHECKS)); + KUNIT_EXPECT_EQ(test, dchid_ring_recheck_delay_us(true), DCHID_RING_RECHECK_US); + /* A clean full drain needs no retry. */ + KUNIT_EXPECT_FALSE(test, dchid_ring_retry_needed(3, false, 0)); +} + static void dchid_ring_failed_prepare_is_not_a_refusal_test(struct kunit *test) { /* CMD_REGISTER_RING was never sent, so the ring is not refused: stay unregistered and retry. */ @@ -102,6 +134,9 @@ static struct kunit_case dchid_protocol_test_cases[] = { KUNIT_CASE(dchid_ring_success_is_not_a_refusal_test), KUNIT_CASE(dchid_ring_failed_prepare_is_not_a_refusal_test), KUNIT_CASE(dchid_ring_refused_register_stays_on_fifo_test), + KUNIT_CASE(dchid_ring_doorbell_any_iface_test), + KUNIT_CASE(dchid_ring_empty_doorbell_rechecked_test), + KUNIT_CASE(dchid_ring_partial_frame_rechecked_test), {} }; diff --git a/drivers/hid/dockchannel-hid/dockchannel-hid-protocol.h b/drivers/hid/dockchannel-hid/dockchannel-hid-protocol.h index 64ff7ba27e221..dfd46d060383d 100644 --- a/drivers/hid/dockchannel-hid/dockchannel-hid-protocol.h +++ b/drivers/hid/dockchannel-hid/dockchannel-hid-protocol.h @@ -73,4 +73,39 @@ static inline bool dchid_ring_stays_on_fifo(bool prepared, int ret, bool fifo_fa return prepared && dchid_ring_refused(ret, fifo_fallback); } +#define DCHID_IFACE_COMM 0 + +/* + * Whether a received frame is the FIFO doorbell for the receive ring: a + * zero-length NOTIFY frame on the FIFO. The firmware may address it to the + * interface whose frame was queued, so the interface is not part of the test. + */ +static inline bool dchid_notify_wants_drain(bool fifo, u8 iface, u32 length) +{ + return fifo && !length; +} + +/* + * A doorbell that finds the ring empty is looked at once more at once. One + * that stops on a frame still being written is looked at again up to + * DCHID_RING_RECHECKS times, DCHID_RING_RECHECK_US apart, before the frame is + * left for the next doorbell. Only the second case sleeps: an empty ring is + * the common case after a doorbell that the previous drain already served, and + * a timer wakeup for each of those would add to the IRQ thread's load. + */ +#define DCHID_RING_RECHECKS 2 +#define DCHID_RING_RECHECK_US 100 + +static inline bool dchid_ring_retry_needed(int frames, bool partial, int tries) +{ + if (partial) + return tries < DCHID_RING_RECHECKS; + return frames == 0 && tries < 1; +} + +static inline unsigned int dchid_ring_recheck_delay_us(bool partial) +{ + return partial ? DCHID_RING_RECHECK_US : 0; +} + #endif /* _DOCKCHANNEL_HID_PROTOCOL_H */ diff --git a/drivers/hid/dockchannel-hid/dockchannel-hid.c b/drivers/hid/dockchannel-hid/dockchannel-hid.c index 77a73b20e3fcc..c1b64e4e7bd7c 100644 --- a/drivers/hid/dockchannel-hid/dockchannel-hid.c +++ b/drivers/hid/dockchannel-hid/dockchannel-hid.c @@ -50,7 +50,7 @@ struct dchid_hdr { u16 pad; } __packed; -#define IFACE_COMM 0 +#define IFACE_COMM DCHID_IFACE_COMM #define FLAGS_GROUP GENMASK(7, 6) #define FLAGS_REQ GENMASK(5, 0) @@ -1480,7 +1480,9 @@ static int dchid_dispatch(struct dockchannel_hid *dchid, struct dchid_hdr *hdr, if (dchid->use_rx_ring && hdr->channel == DCHID_CHANNEL_NOTIFY) { /* A FIFO notification has no application subheader or report ID. */ - return fifo && !hdr->length && hdr->iface == IFACE_COMM; + dev_dbg(dchid->dev, "notify fifo=%d iface=%u length=%u\n", + fifo, hdr->iface, hdr->length); + return dchid_notify_wants_drain(fifo, hdr->iface, hdr->length); } if (hdr->iface >= MAX_INTERFACES || hdr->length < sizeof(struct dchid_subhdr)) @@ -1531,13 +1533,14 @@ static void dchid_ring_copy(struct dockchannel_hid *dchid, void *dst, memcpy(dst + first, data, length - first); } -static int dchid_drain_ring(struct dockchannel_hid *dchid) +static int dchid_drain_ring(struct dockchannel_hid *dchid, bool *partial) { const u32 capacity = dchid->ring_size - DCHID_RING_HEADER_SIZE; u32 *indices = dchid->ring; u32 producer, consumer, available; - int size; + int size, frames = 0; + *partial = false; if (!READ_ONCE(dchid->ring_registered)) { /* The firmware can notify before it has the ring address: nothing to read. */ dev_dbg(dchid->dev, "Ring notification before registration\n"); @@ -1554,15 +1557,19 @@ static int dchid_drain_ring(struct dockchannel_hid *dchid) dma_rmb(); available = producer >= consumer ? producer - consumer : capacity - consumer + producer; - if (available < sizeof(struct dchid_hdr)) - return 0; + if (available < sizeof(struct dchid_hdr)) { + *partial = !!available; + return frames; + } dchid_ring_copy(dchid, dchid->pkt_buf, consumer, sizeof(struct dchid_hdr)); size = dchid_frame_size((void *)dchid->pkt_buf); if (size < 0) return size; - if (available < size) - return 0; + if (available < size) { + *partial = true; + return frames; + } dchid_ring_copy(dchid, dchid->pkt_buf, consumer, size); dchid_dispatch(dchid, (void *)dchid->pkt_buf, dchid->pkt_buf + sizeof(struct dchid_hdr), false); @@ -1570,19 +1577,50 @@ static int dchid_drain_ring(struct dockchannel_hid *dchid) if (consumer >= capacity) consumer -= capacity; dchid->ring_read = consumer; + frames++; /* All frame reads must complete before the producer can reuse it. */ mb(); WRITE_ONCE(indices[1], consumer); cond_resched(); } - return 0; + return frames; +} + +/* + * Drain after a doorbell. The firmware can ring the doorbell before the + * producer index (or the whole frame) is visible, and the doorbell is not + * repeated: the frame would then wait for the next one. Look again a bounded + * number of times while the ring looks empty or the last frame is incomplete. + */ +static void dchid_drain_ring_rechecked(struct dockchannel_hid *dchid) +{ + int tries = 0; + bool partial = false; + unsigned int delay; + int frames; + + do { + frames = dchid_drain_ring(dchid, &partial); + if (frames < 0) { + dev_err(dchid->dev, "Receive ring failed: %d\n", frames); + return; + } + dev_dbg(dchid->dev, "drain: try %d frames %d partial %d producer %u consumer %u\n", + tries, frames, partial, + READ_ONCE(((u32 *)dchid->ring)[0]), + READ_ONCE(((u32 *)dchid->ring)[1])); + if (!dchid_ring_retry_needed(frames, partial, tries++)) + return; + delay = dchid_ring_recheck_delay_us(partial); + if (delay) + fsleep(delay); + } while (!READ_ONCE(dchid->stopping)); } static void dchid_handle_packet(void *cookie, size_t avail) { struct dockchannel_hid *dchid = cookie; struct dchid_hdr hdr; - int ret; if (READ_ONCE(dchid->stopping)) return; @@ -1600,11 +1638,8 @@ static void dchid_handle_packet(void *cookie, size_t avail) goto done; } - if (dchid_dispatch(dchid, &hdr, dchid->pkt_buf, true)) { - ret = dchid_drain_ring(dchid); - if (ret < 0) - dev_err(dchid->dev, "Receive ring failed: %d\n", ret); - } + if (dchid_dispatch(dchid, &hdr, dchid->pkt_buf, true)) + dchid_drain_ring_rechecked(dchid); done: if (!READ_ONCE(dchid->stopping)) From add2e7f1420cf75475d7a8618d6598d0b669514b Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Thu, 19 Nov 2026 11:53:20 -0600 Subject: [PATCH 6/6] HID: dockchannel-hid: drop the drain rechecks, keep the comm-interface doorbell Neither lost-wakeup path has been seen on hardware, so until a trace shows a late frame, fix the ordering rather than add rechecks: the drain already carries dma_rmb() between the producer index read and the frame reads, so a partial frame under a published index means the producer published early, which neither a barrier nor a bounded 100 us retry loop fixes. Drop dchid_drain_ring_rechecked() and its helpers and cases. The any-interface doorbell is also dropped: no second interface was seen ringing, so the notification is again taken from the comm interface only. Word the probe comment as the review asks: the T6050 MTP compatible selects the ring and its size (the 20 MiB introduced in #2015), not a board opt-in. Build check: arm64 config with CONFIG_KUNIT=y, CONFIG_HID_DOCKCHANNEL_KUNIT_TEST=y and CONFIG_HID_DOCKCHANNEL_PARSER_KUNIT_TEST=y, clang, W=1: dockchannel-hid.o and dockchannel-hid-protocol-test.o build with no warnings. Not booted. --- .../dockchannel-hid-parser-test.c | 12 +-- .../dockchannel-hid-protocol-test.c | 35 --------- .../dockchannel-hid-protocol.h | 35 --------- drivers/hid/dockchannel-hid/dockchannel-hid.c | 76 +++++-------------- 4 files changed, 23 insertions(+), 135 deletions(-) diff --git a/drivers/hid/dockchannel-hid/dockchannel-hid-parser-test.c b/drivers/hid/dockchannel-hid/dockchannel-hid-parser-test.c index e6c0c473e262c..f7cd70a22e957 100644 --- a/drivers/hid/dockchannel-hid/dockchannel-hid-parser-test.c +++ b/drivers/hid/dockchannel-hid/dockchannel-hid-parser-test.c @@ -196,14 +196,6 @@ static void dchid_unaligned_checksum_test(struct kunit *test) } } -/* dchid_drain_ring() also reports whether it stopped on a partial frame; these tests do not look at that. */ -static int dchid_test_drain(struct dockchannel_hid *dchid) -{ - bool partial; - - return dchid_drain_ring(dchid, &partial); -} - /* A ring whose next frame straddles the end of the data area. */ static void *dchid_test_ring(struct kunit *test, u32 consumer, u32 producer) { @@ -248,7 +240,7 @@ static void dchid_ring_wrap_test(struct kunit *test) memcpy(data + consumer, &frame, 12); memcpy(data, (u8 *)&frame + 12, sizeof(frame) - 12); - KUNIT_EXPECT_EQ(test, dchid_test_drain(iface->dchid), 1); + KUNIT_EXPECT_EQ(test, dchid_drain_ring(iface->dchid), 0); /* The frame arrived whole: its acknowledgment matched the pending command. */ KUNIT_EXPECT_TRUE(test, completion_done(&iface->out_complete)); KUNIT_EXPECT_EQ(test, iface->dchid->ring_read, sizeof(frame) - 12); @@ -262,7 +254,7 @@ static void dchid_ring_bad_header_test(struct kunit *test) u8 *data = dchid_test_ring(test, 0, 16); memcpy(data, &hdr, sizeof(hdr)); - KUNIT_EXPECT_EQ(test, dchid_test_drain(iface->dchid), -EPROTO); + KUNIT_EXPECT_EQ(test, dchid_drain_ring(iface->dchid), -EPROTO); KUNIT_EXPECT_EQ(test, iface->dchid->ring_read, 0U); } diff --git a/drivers/hid/dockchannel-hid/dockchannel-hid-protocol-test.c b/drivers/hid/dockchannel-hid/dockchannel-hid-protocol-test.c index e35241509fa7a..613996225865c 100644 --- a/drivers/hid/dockchannel-hid/dockchannel-hid-protocol-test.c +++ b/drivers/hid/dockchannel-hid/dockchannel-hid-protocol-test.c @@ -79,38 +79,6 @@ static void dchid_ring_success_is_not_a_refusal_test(struct kunit *test) KUNIT_EXPECT_FALSE(test, dchid_ring_refused(0, true)); } -static void dchid_ring_doorbell_any_iface_test(struct kunit *test) -{ - /* The firmware may address the doorbell to the interface whose frame is queued. */ - KUNIT_EXPECT_TRUE(test, dchid_notify_wants_drain(true, 0, 0)); - KUNIT_EXPECT_TRUE(test, dchid_notify_wants_drain(true, 2, 0)); - KUNIT_EXPECT_TRUE(test, dchid_notify_wants_drain(true, 7, 0)); - /* A frame with a payload is not a doorbell, and ring frames never are. */ - KUNIT_EXPECT_FALSE(test, dchid_notify_wants_drain(true, 2, 4)); - KUNIT_EXPECT_FALSE(test, dchid_notify_wants_drain(false, 0, 0)); -} - -static void dchid_ring_empty_doorbell_rechecked_test(struct kunit *test) -{ - /* A doorbell that finds nothing is looked at once more, without sleeping. */ - KUNIT_EXPECT_TRUE(test, dchid_ring_retry_needed(0, false, 0)); - KUNIT_EXPECT_FALSE(test, dchid_ring_retry_needed(0, false, 1)); - KUNIT_EXPECT_EQ(test, dchid_ring_recheck_delay_us(false), 0); -} - -static void dchid_ring_partial_frame_rechecked_test(struct kunit *test) -{ - int tries; - - /* Frames drained but the next one is still being written: wait and look again. */ - for (tries = 0; tries < DCHID_RING_RECHECKS; tries++) - KUNIT_EXPECT_TRUE(test, dchid_ring_retry_needed(3, true, tries)); - KUNIT_EXPECT_FALSE(test, dchid_ring_retry_needed(3, true, DCHID_RING_RECHECKS)); - KUNIT_EXPECT_EQ(test, dchid_ring_recheck_delay_us(true), DCHID_RING_RECHECK_US); - /* A clean full drain needs no retry. */ - KUNIT_EXPECT_FALSE(test, dchid_ring_retry_needed(3, false, 0)); -} - static void dchid_ring_failed_prepare_is_not_a_refusal_test(struct kunit *test) { /* CMD_REGISTER_RING was never sent, so the ring is not refused: stay unregistered and retry. */ @@ -134,9 +102,6 @@ static struct kunit_case dchid_protocol_test_cases[] = { KUNIT_CASE(dchid_ring_success_is_not_a_refusal_test), KUNIT_CASE(dchid_ring_failed_prepare_is_not_a_refusal_test), KUNIT_CASE(dchid_ring_refused_register_stays_on_fifo_test), - KUNIT_CASE(dchid_ring_doorbell_any_iface_test), - KUNIT_CASE(dchid_ring_empty_doorbell_rechecked_test), - KUNIT_CASE(dchid_ring_partial_frame_rechecked_test), {} }; diff --git a/drivers/hid/dockchannel-hid/dockchannel-hid-protocol.h b/drivers/hid/dockchannel-hid/dockchannel-hid-protocol.h index dfd46d060383d..64ff7ba27e221 100644 --- a/drivers/hid/dockchannel-hid/dockchannel-hid-protocol.h +++ b/drivers/hid/dockchannel-hid/dockchannel-hid-protocol.h @@ -73,39 +73,4 @@ static inline bool dchid_ring_stays_on_fifo(bool prepared, int ret, bool fifo_fa return prepared && dchid_ring_refused(ret, fifo_fallback); } -#define DCHID_IFACE_COMM 0 - -/* - * Whether a received frame is the FIFO doorbell for the receive ring: a - * zero-length NOTIFY frame on the FIFO. The firmware may address it to the - * interface whose frame was queued, so the interface is not part of the test. - */ -static inline bool dchid_notify_wants_drain(bool fifo, u8 iface, u32 length) -{ - return fifo && !length; -} - -/* - * A doorbell that finds the ring empty is looked at once more at once. One - * that stops on a frame still being written is looked at again up to - * DCHID_RING_RECHECKS times, DCHID_RING_RECHECK_US apart, before the frame is - * left for the next doorbell. Only the second case sleeps: an empty ring is - * the common case after a doorbell that the previous drain already served, and - * a timer wakeup for each of those would add to the IRQ thread's load. - */ -#define DCHID_RING_RECHECKS 2 -#define DCHID_RING_RECHECK_US 100 - -static inline bool dchid_ring_retry_needed(int frames, bool partial, int tries) -{ - if (partial) - return tries < DCHID_RING_RECHECKS; - return frames == 0 && tries < 1; -} - -static inline unsigned int dchid_ring_recheck_delay_us(bool partial) -{ - return partial ? DCHID_RING_RECHECK_US : 0; -} - #endif /* _DOCKCHANNEL_HID_PROTOCOL_H */ diff --git a/drivers/hid/dockchannel-hid/dockchannel-hid.c b/drivers/hid/dockchannel-hid/dockchannel-hid.c index c1b64e4e7bd7c..d1464a58d8417 100644 --- a/drivers/hid/dockchannel-hid/dockchannel-hid.c +++ b/drivers/hid/dockchannel-hid/dockchannel-hid.c @@ -50,7 +50,7 @@ struct dchid_hdr { u16 pad; } __packed; -#define IFACE_COMM DCHID_IFACE_COMM +#define IFACE_COMM 0 #define FLAGS_GROUP GENMASK(7, 6) #define FLAGS_REQ GENMASK(5, 0) @@ -1480,9 +1480,7 @@ static int dchid_dispatch(struct dockchannel_hid *dchid, struct dchid_hdr *hdr, if (dchid->use_rx_ring && hdr->channel == DCHID_CHANNEL_NOTIFY) { /* A FIFO notification has no application subheader or report ID. */ - dev_dbg(dchid->dev, "notify fifo=%d iface=%u length=%u\n", - fifo, hdr->iface, hdr->length); - return dchid_notify_wants_drain(fifo, hdr->iface, hdr->length); + return fifo && !hdr->length && hdr->iface == IFACE_COMM; } if (hdr->iface >= MAX_INTERFACES || hdr->length < sizeof(struct dchid_subhdr)) @@ -1533,14 +1531,13 @@ static void dchid_ring_copy(struct dockchannel_hid *dchid, void *dst, memcpy(dst + first, data, length - first); } -static int dchid_drain_ring(struct dockchannel_hid *dchid, bool *partial) +static int dchid_drain_ring(struct dockchannel_hid *dchid) { const u32 capacity = dchid->ring_size - DCHID_RING_HEADER_SIZE; u32 *indices = dchid->ring; u32 producer, consumer, available; - int size, frames = 0; + int size; - *partial = false; if (!READ_ONCE(dchid->ring_registered)) { /* The firmware can notify before it has the ring address: nothing to read. */ dev_dbg(dchid->dev, "Ring notification before registration\n"); @@ -1557,19 +1554,15 @@ static int dchid_drain_ring(struct dockchannel_hid *dchid, bool *partial) dma_rmb(); available = producer >= consumer ? producer - consumer : capacity - consumer + producer; - if (available < sizeof(struct dchid_hdr)) { - *partial = !!available; - return frames; - } + if (available < sizeof(struct dchid_hdr)) + return 0; dchid_ring_copy(dchid, dchid->pkt_buf, consumer, sizeof(struct dchid_hdr)); size = dchid_frame_size((void *)dchid->pkt_buf); if (size < 0) return size; - if (available < size) { - *partial = true; - return frames; - } + if (available < size) + return 0; dchid_ring_copy(dchid, dchid->pkt_buf, consumer, size); dchid_dispatch(dchid, (void *)dchid->pkt_buf, dchid->pkt_buf + sizeof(struct dchid_hdr), false); @@ -1577,50 +1570,19 @@ static int dchid_drain_ring(struct dockchannel_hid *dchid, bool *partial) if (consumer >= capacity) consumer -= capacity; dchid->ring_read = consumer; - frames++; /* All frame reads must complete before the producer can reuse it. */ mb(); WRITE_ONCE(indices[1], consumer); cond_resched(); } - return frames; -} - -/* - * Drain after a doorbell. The firmware can ring the doorbell before the - * producer index (or the whole frame) is visible, and the doorbell is not - * repeated: the frame would then wait for the next one. Look again a bounded - * number of times while the ring looks empty or the last frame is incomplete. - */ -static void dchid_drain_ring_rechecked(struct dockchannel_hid *dchid) -{ - int tries = 0; - bool partial = false; - unsigned int delay; - int frames; - - do { - frames = dchid_drain_ring(dchid, &partial); - if (frames < 0) { - dev_err(dchid->dev, "Receive ring failed: %d\n", frames); - return; - } - dev_dbg(dchid->dev, "drain: try %d frames %d partial %d producer %u consumer %u\n", - tries, frames, partial, - READ_ONCE(((u32 *)dchid->ring)[0]), - READ_ONCE(((u32 *)dchid->ring)[1])); - if (!dchid_ring_retry_needed(frames, partial, tries++)) - return; - delay = dchid_ring_recheck_delay_us(partial); - if (delay) - fsleep(delay); - } while (!READ_ONCE(dchid->stopping)); + return 0; } static void dchid_handle_packet(void *cookie, size_t avail) { struct dockchannel_hid *dchid = cookie; struct dchid_hdr hdr; + int ret; if (READ_ONCE(dchid->stopping)) return; @@ -1638,8 +1600,11 @@ static void dchid_handle_packet(void *cookie, size_t avail) goto done; } - if (dchid_dispatch(dchid, &hdr, dchid->pkt_buf, true)) - dchid_drain_ring_rechecked(dchid); + if (dchid_dispatch(dchid, &hdr, dchid->pkt_buf, true)) { + ret = dchid_drain_ring(dchid); + if (ret < 0) + dev_err(dchid->dev, "Receive ring failed: %d\n", ret); + } done: if (!READ_ONCE(dchid->stopping)) @@ -1726,11 +1691,12 @@ static int dockchannel_hid_probe(struct platform_device *pdev) /* The M4 Pro (J616s) MTP uses the same 26.6.2 firmware transport as T8132. */ dchid->use_ring = of_device_is_compatible(dev->of_node, "apple,t6041-dockchannel-hid"); /* - * The T6050 MTP keeps this platform's reset and GPIO paths, but macOS gives - * its large raw touch reports a 20 MiB host read FIFO. A board opts in with - * the t6050 compatible, once its MTP DART streams 0 and 1 are described. - * Try that receive transport and fall back to the MMIO FIFO if the - * firmware refuses it. + * The T6050 MTP keeps this platform's reset and GPIO paths, but its large + * raw touch reports need a bigger receive path: the T6050 MTP compatible + * selects the ring and its size, the 20 MiB introduced for it in #2015 + * (the T6041 path keeps its 2 MiB). The ring needs the node's MTP DART + * streams 0 and 1 described (see #2014). Try that receive transport and + * fall back to the MMIO FIFO if the firmware refuses it. */ dchid->use_rx_ring = dchid->use_ring || of_device_is_compatible(dev->of_node, "apple,t6050-dockchannel-hid");