* [PATCH v7 0/2] HID: intel-thc-hid: PRD tables under a drain
@ 2026-11-17 10:54 Emfox Zhou
2026-11-17 10:54 ` [PATCH v7 1/2] HID: intel-thc-hid: intel-thc: restore PRD table capacity Emfox Zhou
2026-11-17 10:54 ` [PATCH v7 2/2] HID: intel-thc-hid: quickspi, quicki2c: skip an empty read Emfox Zhou
0 siblings, 2 replies; 3+ messages in thread
From: Emfox Zhou @ 2026-11-17 10:54 UTC (permalink / raw)
To: Even Xu, Xinpeng Sun, Jiri Kosina, Benjamin Tissoires
Cc: linux-input, linux-kernel
The PRD tables lose capacity when a frame is left unread, and a drain can
reach a ring that a reset already rewound. Rebuild the tables before the
engine that reads them starts, hold every path that stops an engine or
moves its pointers under the lock the drain takes, and stop the drain from
treating an empty read as a frame.
Patch 1 does the ring side in the core. Patch 2 fixes the two callers that
go by the buffer instead of the length they were given.
Changes in v7:
- The two Fixes: trailers the second patch was missing, pointed out in
review.
Link to v6: https://lore.kernel.org/r/20261117102711.270303-1-emfox@debian.org
Emfox Zhou (2):
HID: intel-thc-hid: intel-thc: restore PRD table capacity
HID: intel-thc-hid: quickspi, quicki2c: skip an empty read
.../intel-quicki2c/pci-quicki2c.c | 3 +
.../intel-quickspi/pci-quickspi.c | 3 +
.../intel-thc-hid/intel-thc/intel-thc-dma.c | 89 ++++++++++++++++++-
.../intel-thc-hid/intel-thc/intel-thc-dma.h | 8 ++
4 files changed, 99 insertions(+), 4 deletions(-)
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v7 1/2] HID: intel-thc-hid: intel-thc: restore PRD table capacity
2026-11-17 10:54 [PATCH v7 0/2] HID: intel-thc-hid: PRD tables under a drain Emfox Zhou
@ 2026-11-17 10:54 ` Emfox Zhou
2026-11-17 10:54 ` [PATCH v7 2/2] HID: intel-thc-hid: quickspi, quicki2c: skip an empty read Emfox Zhou
1 sibling, 0 replies; 3+ messages in thread
From: Emfox Zhou @ 2026-11-17 10:54 UTC (permalink / raw)
To: Even Xu, Xinpeng Sun, Jiri Kosina, Benjamin Tissoires
Cc: linux-input, linux-kernel
The controller writes the frame length into the PRD entry it filled and
marks the end of the frame there; only a read puts the capacity back.
thc_reset_dma_settings() rewinds the read pointers but not those lengths,
and stops the engines: a frame still queued when it runs is dropped, the
next frame that does not fit runs past the end of its entry, and the
controller reports a buffer overrun, a transfer error that leads back
into the same reset with the table still short.
Rebuild a table before the engine that reads it is started: on probe,
resume and the RxDMA reset, on the SWDMA start once that engine is idle,
and where an SWDMA round trip restarts the receive engine.
A drain in the threaded handler reads a table without the bus lock. The
rebuild, the arming, and every path that stops an engine or moves its
pointers run without that lock as well, so a drain can land in the middle
of any of them and read an entry of a ring that was already rewound. Give
the tables a mutex taken by all of them, inside the bus lock where the
caller holds it; no path takes the two in the other order.
The rewind leaves the read pointer at the start and the write pointer
where the last frame left it, so the ring reads as non-empty while the
engine is stopped and a drain would take an entry that was never written.
Record whether an engine runs, and read the ring only while it is set: the
flag is published under that same lock, after the control register is read
back, so a drain sees either a stopped engine or an armed one.
thc_dma_read() clears *read_len when it takes nothing, because callers
read that even when the read succeeds, and thc_copy_sgls_to_prd() returns
early for a channel whose tables are gone.
Fixes: a688404b2e20 ("HID: intel-thc-hid: intel-thc: Add THC DMA interfaces")
Signed-off-by: Emfox Zhou <emfox@debian.org>
---
.../intel-thc-hid/intel-thc/intel-thc-dma.c | 89 ++++++++++++++++++-
.../intel-thc-hid/intel-thc/intel-thc-dma.h | 8 ++
2 files changed, 93 insertions(+), 4 deletions(-)
diff --git a/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.c b/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.c
index c80ef8c20..944db6150 100644
--- a/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.c
+++ b/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.c
@@ -30,10 +30,20 @@ static void dma_set_start_bit(struct thc_device *dev,
struct thc_dma_configuration *dma_config)
{
u32 ctrl, mask, mbits, data, offset;
+ bool reads_ring;
if (!dma_config->is_enabled)
return;
+ /*
+ * Arm and publish under the lock the read path takes: a ring that was
+ * just rewound still reads as non-empty, so a reader in between would
+ * copy an entry the engine never wrote. TxDMA reads no ring.
+ */
+ reads_ring = dma_config->dma_channel != THC_TXDMA;
+ if (reads_ring)
+ mutex_lock(&dev->dma_ctx->prd_lock);
+
switch (dma_config->dma_channel) {
case THC_RXDMA1:
case THC_RXDMA2:
@@ -92,6 +102,17 @@ static void dma_set_start_bit(struct thc_device *dev,
default:
break;
}
+
+ if (reads_ring) {
+ /*
+ * The mutex orders the flag, not the write to the device:
+ * read the register back so the arming lands first.
+ */
+ regmap_read(dev->thc_regmap, dma_config->dma_cntrl, &ctrl);
+
+ WRITE_ONCE(dma_config->is_started, true);
+ mutex_unlock(&dev->dma_ctx->prd_lock);
+ }
}
static void dma_set_prd_control(struct thc_device *dev, u8 entry_count, u8 cb_depth,
@@ -211,12 +232,27 @@ static void thc_copy_sgls_to_prd(struct thc_device *dev,
{
unsigned int i;
+ guard(mutex)(&dev->dma_ctx->prd_lock);
+
+ if (!config->is_enabled || !config->prd_tbls)
+ return;
+
memset(config->prd_tbls, 0, array_size(PRD_TABLE_SIZE, config->prd_tbl_num));
for (i = 0; i < config->prd_tbl_num; i++)
thc_copy_one_sgl_to_prd(dev, config, i);
}
+/*
+ * A frame that is never read leaves its entry short, and the engine reads
+ * the table again on every start: put the capacity back.
+ */
+static void thc_rebuild_rx_prd(struct thc_device *dev)
+{
+ thc_copy_sgls_to_prd(dev, &dev->dma_ctx->dma_config[THC_RXDMA1]);
+ thc_copy_sgls_to_prd(dev, &dev->dma_ctx->dma_config[THC_RXDMA2]);
+}
+
static int setup_dma_buffers(struct thc_device *dev,
struct thc_dma_configuration *config,
enum dma_data_direction dir)
@@ -266,6 +302,15 @@ static int setup_dma_buffers(struct thc_device *dev,
static void thc_reset_dma_settings(struct thc_device *dev)
{
+ struct thc_dma_context *dma_ctx = dev->dma_ctx;
+ int chan;
+
+ guard(mutex)(&dma_ctx->prd_lock);
+
+ /* The rewind below discards the queue, so no read may start. */
+ for (chan = 0; chan < MAX_THC_DMA_CHANNEL; chan++)
+ WRITE_ONCE(dma_ctx->dma_config[chan].is_started, false);
+
/* Stop all DMA channels and reset DMA read pointers */
regmap_write_bits(dev->thc_regmap, THC_M_PRT_READ_DMA_CNTRL_1_OFFSET,
THC_M_PRT_READ_DMA_CNTRL_START, 0);
@@ -327,6 +372,7 @@ struct thc_dma_context *thc_dma_init(struct thc_device *dev)
return NULL;
dev->dma_ctx = dma_ctx;
+ mutex_init(&dma_ctx->prd_lock);
dma_ctx->dma_config[THC_RXDMA1].dma_channel = THC_RXDMA1;
dma_ctx->dma_config[THC_RXDMA2].dma_channel = THC_RXDMA2;
@@ -441,6 +487,8 @@ void thc_dma_release(struct thc_device *dev)
{
int chan;
+ guard(mutex)(&dev->dma_ctx->prd_lock);
+
for (chan = 0; chan < MAX_THC_DMA_CHANNEL; chan++)
release_dma_buffers(dev, &dev->dma_ctx->dma_config[chan]);
}
@@ -484,13 +532,15 @@ int thc_dma_configure(struct thc_device *dev)
struct thc_dma_context *dma_ctx = dev->dma_ctx;
int chan;
- thc_reset_dma_settings(dev);
-
if (!dma_ctx) {
dev_err_once(dev->dev, "Cannot do DMA configure because DMA context is NULL\n");
return -EINVAL;
}
+ thc_reset_dma_settings(dev);
+
+ thc_rebuild_rx_prd(dev);
+
for (chan = 0; chan < MAX_THC_DMA_CHANNEL; chan++) {
dma_set_prd_base_addr(dev,
dma_ctx->dma_config[chan].prd_tbls_dma_handle,
@@ -519,7 +569,10 @@ void thc_dma_unconfigure(struct thc_device *dev)
{
int chan;
+ guard(mutex)(&dev->dma_ctx->prd_lock);
+
for (chan = 0; chan < MAX_THC_DMA_CHANNEL; chan++) {
+ WRITE_ONCE(dev->dma_ctx->dma_config[chan].is_started, false);
dma_set_prd_base_addr(dev, 0, &dev->dma_ctx->dma_config[chan]);
dma_clear_prd_control(dev, &dev->dma_ctx->dma_config[chan]);
}
@@ -541,7 +594,13 @@ static int thc_wait_for_dma_pause(struct thc_device *dev, enum thc_dma_channel c
((channel == THC_RXDMA2) ? THC_M_PRT_READ_DMA_CNTRL_2_OFFSET :
THC_M_PRT_READ_DMA_CNTRL_SW_OFFSET);
- regmap_write_bits(dev->thc_regmap, ctrl_reg, THC_M_PRT_READ_DMA_CNTRL_START, 0);
+ {
+ guard(mutex)(&dev->dma_ctx->prd_lock);
+
+ WRITE_ONCE(dev->dma_ctx->dma_config[channel].is_started, false);
+ regmap_write_bits(dev->thc_regmap, ctrl_reg,
+ THC_M_PRT_READ_DMA_CNTRL_START, 0);
+ }
sts_reg = (channel == THC_RXDMA1) ? THC_M_PRT_READ_DMA_INT_STS_1_OFFSET :
((channel == THC_RXDMA2) ? THC_M_PRT_READ_DMA_INT_STS_2_OFFSET :
@@ -685,6 +744,21 @@ static int thc_dma_read(struct thc_device *dev,
u8 read_ptr, write_ptr, prd_table_index;
int status;
+ /* The caller may look at this when nothing was read. */
+ *read_len = 0;
+
+ guard(mutex)(&dev->dma_ctx->prd_lock);
+
+ /*
+ * Nothing in the ring is ours until the engine is started: after a
+ * reset the pointers alone say nothing about what was written.
+ */
+ if (!READ_ONCE(read_config->is_started)) {
+ if (read_finished)
+ *read_finished = 1;
+ return 0;
+ }
+
if (!is_dma_buf_empty(dev, read_config, &read_ptr, &write_ptr)) {
prd_table_index = write_ptr & THC_POINTER_MASK;
@@ -765,11 +839,16 @@ static int thc_swdma_read_start(struct thc_device *dev, void *write_buff,
if (ret)
return ret;
- if (thc_wait_for_dma_pause(dev, THC_RXDMA1) || thc_wait_for_dma_pause(dev, THC_RXDMA2))
+ if (thc_wait_for_dma_pause(dev, THC_RXDMA1) ||
+ thc_wait_for_dma_pause(dev, THC_RXDMA2) ||
+ thc_wait_for_dma_pause(dev, THC_SWDMA))
return -EIO;
thc_reset_dma_settings(dev);
+ /* A transfer that timed out left its frame length in the entry. */
+ thc_copy_sgls_to_prd(dev, &dev->dma_ctx->dma_config[THC_SWDMA]);
+
/*
* Max input size control feature is only available for RxDMA, it must keep disabled
* during SWDMA operation, and restore to previous state after SWDMA is done.
@@ -854,6 +933,8 @@ static int thc_swdma_read_completion(struct thc_device *dev)
thc_reset_dma_settings(dev);
+ thc_rebuild_rx_prd(dev);
+
dma_set_start_bit(dev, &dev->dma_ctx->dma_config[THC_RXDMA2]);
ret = thc_interrupt_quiesce(dev, false);
diff --git a/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.h b/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.h
index 715423453..d05d16ea1 100644
--- a/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.h
+++ b/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.h
@@ -6,6 +6,7 @@
#include <linux/bits.h>
#include <linux/dma-mapping.h>
+#include <linux/mutex.h>
#include <linux/sizes.h>
#include <linux/time64.h>
#include <linux/types.h>
@@ -89,6 +90,7 @@ struct thc_prd_table {
* @dma_channel: DMA channel for current DMA configuration
* @prd_tbls_dma_handle: DMA buffer handle
* @dir: Direction of DMA for this config
+ * @is_started: The engine is running, so the ring may be read
* @prd_tbls: PRD tables for current DMA
* @sgls: Array of pointers to scatter-gather lists
* @sgls_nent_pages: Number of pages per scatter-gather list
@@ -105,6 +107,7 @@ struct thc_dma_configuration {
dma_addr_t prd_tbls_dma_handle;
enum dma_data_direction dir;
bool is_enabled;
+ bool is_started;
struct thc_prd_table *prd_tbls;
struct scatterlist *sgls[PRD_TABLES_NUM];
@@ -123,6 +126,8 @@ struct thc_dma_configuration {
* struct thc_dma_context - THC DMA context
* @thc_dma_configuration: Array of all THC Channel configures
* @use_write_interrupts: Indicate TxDMA using interrupt or polling
+ * @prd_lock: Keeps the DMA read path off the tables while they are
+ * rebuilt, armed, stopped or released
* @rx_max_size_en: Temp flag to indicate THC I2C Rx max input size control feature
* enabled or not, only be used during SWDMA operation.
* @rx_int_delay_en: Temp flag to indicate THC I2C Rx interrupt delay feature
@@ -132,6 +137,9 @@ struct thc_dma_context {
struct thc_dma_configuration dma_config[MAX_THC_DMA_CHANNEL];
u8 use_write_interrupts;
+ /* Taken inside the bus lock, never the other way round. */
+ struct mutex prd_lock;
+
bool rx_max_size_en;
bool rx_int_delay_en;
};
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v7 2/2] HID: intel-thc-hid: quickspi, quicki2c: skip an empty read
2026-11-17 10:54 [PATCH v7 0/2] HID: intel-thc-hid: PRD tables under a drain Emfox Zhou
2026-11-17 10:54 ` [PATCH v7 1/2] HID: intel-thc-hid: intel-thc: restore PRD table capacity Emfox Zhou
@ 2026-11-17 10:54 ` Emfox Zhou
1 sibling, 0 replies; 3+ messages in thread
From: Emfox Zhou @ 2026-11-17 10:54 UTC (permalink / raw)
To: Even Xu, Xinpeng Sun, Jiri Kosina, Benjamin Tissoires
Cc: linux-input, linux-kernel
A drain ends when the ring holds no frame, and thc_rxdma_read() reports
that as a successful read of zero bytes. Both handlers go by the buffer
instead of the length: quickspi hands the zero on and its own length check
then rejects the frame still left in the buffer, while quicki2c reads
pkt->len from the previous frame and sends that report again.
Tell the two cases apart by the length. A frame with an empty payload is
still a frame, and quicki2c needs that one for its reset ack.
Fixes: 4138f21115ae ("HID: intel-thc-hid: intel-quickspi: Complete THC QuickSPI driver")
Fixes: 66b59bfce6d9 ("HID: intel-thc-hid: intel-quicki2c: Complete THC QuickI2C driver")
Signed-off-by: Emfox Zhou <emfox@debian.org>
---
drivers/hid/intel-thc-hid/intel-quicki2c/pci-quicki2c.c | 3 +++
drivers/hid/intel-thc-hid/intel-quickspi/pci-quickspi.c | 3 +++
2 files changed, 6 insertions(+)
diff --git a/drivers/hid/intel-thc-hid/intel-quicki2c/pci-quicki2c.c b/drivers/hid/intel-thc-hid/intel-quicki2c/pci-quicki2c.c
index 29319b622..8e3f7f3f6 100644
--- a/drivers/hid/intel-thc-hid/intel-quicki2c/pci-quicki2c.c
+++ b/drivers/hid/intel-thc-hid/intel-quicki2c/pci-quicki2c.c
@@ -298,6 +298,9 @@ static int handle_input_report(struct quicki2c_device *qcdev)
if (ret)
return ret;
+ if (!report_len)
+ continue;
+
if (!pkt->len) {
if (qcdev->state == QUICKI2C_RESETING) {
qcdev->reset_ack = true;
diff --git a/drivers/hid/intel-thc-hid/intel-quickspi/pci-quickspi.c b/drivers/hid/intel-thc-hid/intel-quickspi/pci-quickspi.c
index bf49d8ce5..812f743f6 100644
--- a/drivers/hid/intel-thc-hid/intel-quickspi/pci-quickspi.c
+++ b/drivers/hid/intel-thc-hid/intel-quickspi/pci-quickspi.c
@@ -331,6 +331,9 @@ static irqreturn_t quickspi_irq_thread_handler(int irq, void *dev_id)
goto end;
}
+ if (!input_len)
+ continue;
+
quickspi_handle_input_data(qsdev, input_len);
}
}
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-17 10:54 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-17 10:54 [PATCH v7 0/2] HID: intel-thc-hid: PRD tables under a drain Emfox Zhou
2026-11-17 10:54 ` [PATCH v7 1/2] HID: intel-thc-hid: intel-thc: restore PRD table capacity Emfox Zhou
2026-11-17 10:54 ` [PATCH v7 2/2] HID: intel-thc-hid: quickspi, quicki2c: skip an empty read Emfox Zhou
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®