* [PATCH v11 0/6] HID: intel-thc-hid: intel-ipts: support THC IPTS ports
@ 2026-12-03 2:04 Emfox Zhou
2026-12-03 2:04 ` [PATCH v11 1/6] HID: intel-thc-hid: intel-thc: expose the port operations IPTS needs Emfox Zhou
` (5 more replies)
0 siblings, 6 replies; 10+ messages in thread
From: Emfox Zhou @ 2026-12-03 2:04 UTC (permalink / raw)
To: Even Xu, Xinpeng Sun, Jiri Kosina, Benjamin Tissoires
Cc: linux-input, linux-kernel
The THC ports on Tiger Lake, Lakefield, Alder Lake and Raptor Lake can
run in IPTS mode: plain SPI with a private framing instead of HIDSPI,
which is all intel-thc-hid handles today. This adds a backend for them
next to intel-quickspi and intel-quicki2c, on top of the same core.
1/6 expose the port operations IPTS needs (core)
2/6 bring up the port
3/6 receive reports
4/6 add the HID device
5/6 handle system sleep
6/6 document the IPTS mode
Changes in v11:
- The two reset calls are one thc_port_reset(hold): the start asserts
the line, waits and releases it, the stop keeps it asserted. DEVRST
is zero for asserted and one for released, as the THC datasheet
describes it.
- thc_spi_hz_from_div() is gone, and so is the clamp around it: the
driver converts the divisor the configuration block reports on its
own and lets thc_get_spi_freq_div_val() pick the gear. A Surface Pro 8
reports 0x09e351e8, divisor 4, 31.25 MHz.
- The quiesce warning stays: a call that changes nothing is worth
knowing about.
- ipts.c is split into ipts-protocol.c and ipts-hid.c, the way
quickspi and quicki2c are laid out.
- Commit messages are shorter; the design detail they carried moved to
Documentation/hid/intel-thc-hid.rst, which gains a section on the
driver structure.
- Comments follow the kernel style across the series.
- Also from our own review: the interrupt is left open whenever no reset
is running, and a run that finds a reset in flight claims nothing, so a
transmit completion already on the wire is still delivered; the tables
of a failed reset are freed on the next start or stop, and the SPI
divisor is rounded up before the gear is picked.
v10: https://lore.kernel.org/linux-input/cover.1795794049.git.emfox@debian.org/
This is based on the two intel-thc fix series below, applied in this order:
[PATCH v3 0/5] HID: intel-thc-hid: intel-thc: DMA lifetime fixes
https://lore.kernel.org/linux-input/cover.1795791500.git.emfox@debian.org/
[PATCH v8] HID: intel-thc-hid: intel-thc: restore PRD table capacity
https://lore.kernel.org/linux-input/20261127234500.424242-1-emfox@debian.org/
The first gives the DMA engines a lifetime, the second keeps a PRD table
from overrunning after a reset; the recovery path here relies on both,
and they overlap in intel-thc-dma.c, so apply them in that order.
The base-commit below is the tree those two series are based on. The six
patches here apply on top of them, not to the base alone: 1/6 extends the
core file the first series rewrites.
The hardware facts this driver relies on - port IDs, the configuration
block layout, frame codes and header sizes - were confirmed on a Surface
Pro 8 with a 045e:0c37 controller.
Emfox Zhou (6):
HID: intel-thc-hid: intel-thc: expose the port operations IPTS needs
HID: intel-thc-hid: intel-ipts: bring up the port
HID: intel-thc-hid: intel-ipts: receive reports
HID: intel-thc-hid: intel-ipts: add the HID device
HID: intel-thc-hid: intel-ipts: handle system sleep
HID: intel-thc-hid: intel-ipts: document the IPTS mode
Documentation/hid/intel-thc-hid.rst | 136 +++-
drivers/hid/intel-thc-hid/Kconfig | 13 +
drivers/hid/intel-thc-hid/Makefile | 5 +
.../hid/intel-thc-hid/intel-ipts/ipts-hid.c | 267 +++++++
.../intel-thc-hid/intel-ipts/ipts-protocol.c | 682 ++++++++++++++++++
drivers/hid/intel-thc-hid/intel-ipts/ipts.h | 225 ++++++
.../hid/intel-thc-hid/intel-ipts/pci-ipts.c | 228 ++++++
.../intel-thc-hid/intel-thc/intel-thc-dev.c | 58 ++
.../intel-thc-hid/intel-thc/intel-thc-dev.h | 2 +
9 files changed, 1615 insertions(+), 1 deletion(-)
create mode 100644 drivers/hid/intel-thc-hid/intel-ipts/ipts-hid.c
create mode 100644 drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c
create mode 100644 drivers/hid/intel-thc-hid/intel-ipts/ipts.h
create mode 100644 drivers/hid/intel-thc-hid/intel-ipts/pci-ipts.c
base-commit: 4f048c56c2697000105a75c28b918ae76f11fcef
prerequisite-patch-id: 1921888f4500ace81255e73142f40b89e4a75fd2
prerequisite-patch-id: e87bd84cb62f9d122e80b0636ec02103ee4eef96
prerequisite-patch-id: 682e6eeea6eb7792c7b2f00b14226cb9a3d1c56c
prerequisite-patch-id: efd51e0ca2778c5028ba030ddfaee6da5a82622d
prerequisite-patch-id: 1fa0e32622986734adb7ce7040400f169bc26f2b
prerequisite-patch-id: 06a128cb2c3db1f80116b314d11d69cda0d4dce0
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v11 1/6] HID: intel-thc-hid: intel-thc: expose the port operations IPTS needs
2026-12-03 2:04 [PATCH v11 0/6] HID: intel-thc-hid: intel-ipts: support THC IPTS ports Emfox Zhou
@ 2026-12-03 2:04 ` Emfox Zhou
2026-12-03 2:11 ` sashiko-bot
2026-12-03 2:04 ` [PATCH v11 2/6] HID: intel-thc-hid: intel-ipts: bring up the port Emfox Zhou
` (4 subsequent siblings)
5 siblings, 1 reply; 10+ messages in thread
From: Emfox Zhou @ 2026-12-03 2:04 UTC (permalink / raw)
To: Even Xu, Xinpeng Sun, Jiri Kosina, Benjamin Tissoires
Cc: linux-input, linux-kernel
Add thc_port_supported() and thc_port_reset(), so a backend can read the
port capability bit and drive the touch controller reset pin without
writing THC_M_PRT_CONTROL itself.
Signed-off-by: Emfox Zhou <emfox@debian.org>
---
.../intel-thc-hid/intel-thc/intel-thc-dev.c | 58 +++++++++++++++++++
.../intel-thc-hid/intel-thc/intel-thc-dev.h | 2 +
2 files changed, 60 insertions(+)
diff --git a/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.c b/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.c
index 03d3b60b3..c7d495915 100644
--- a/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.c
+++ b/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.c
@@ -1111,6 +1111,64 @@ int thc_port_select(struct thc_device *dev, enum thc_port_type port_type)
}
EXPORT_SYMBOL_NS_GPL(thc_port_select, "INTEL_THC");
+/**
+ * thc_port_supported - Tell whether the port has a touch device interface
+ *
+ * @dev: the pointer of THC private device context
+ * @supported: set to whether the port can drive a device at all
+ *
+ * Reads the capability bit of the port control register. A port that is not
+ * supported answers nothing on the bus, so this is worth asking before the
+ * port is brought up.
+ *
+ * Return: 0 on success, other error codes on failed
+ */
+int thc_port_supported(struct thc_device *dev, bool *supported)
+{
+ u32 ctrl;
+ int ret;
+
+ ret = regmap_read(dev->thc_regmap, THC_M_PRT_CONTROL_OFFSET, &ctrl);
+ if (ret)
+ return ret;
+
+ *supported = ctrl & THC_M_PRT_CONTROL_PORT_SUPPORTED;
+
+ return 0;
+}
+EXPORT_SYMBOL_NS_GPL(thc_port_supported, "INTEL_THC");
+
+/**
+ * thc_port_reset - Drive the reset line of the touch IC
+ *
+ * @dev: the pointer of THC private device context
+ * @hold: keep the touch IC in reset instead of releasing it
+ *
+ * The reset bit of the port control register drives the reset pin of the
+ * touch IC: zero asserts the reset, one releases it. Firmware may leave
+ * the pin released, so a start asserts it and then lets it go rather than
+ * assume it was held.
+ *
+ * Return: 0 on success, other error codes on failed
+ */
+int thc_port_reset(struct thc_device *dev, bool hold)
+{
+ int ret;
+
+ /* DEVRST: 0 asserts the reset, 1 releases it. */
+ ret = regmap_write_bits(dev->thc_regmap, THC_M_PRT_CONTROL_OFFSET,
+ THC_M_PRT_CONTROL_DEVRST, 0);
+ if (ret || hold)
+ return ret;
+
+ usleep_range(1000, 2000);
+
+ return regmap_write_bits(dev->thc_regmap, THC_M_PRT_CONTROL_OFFSET,
+ THC_M_PRT_CONTROL_DEVRST,
+ THC_M_PRT_CONTROL_DEVRST);
+}
+EXPORT_SYMBOL_NS_GPL(thc_port_reset, "INTEL_THC");
+
/* 125 MHz / (8 * N), with the low frequency bit set. */
#define THC_SPI_FREQUENCY_2M2 2232143
#define THC_SPI_FREQUENCY_2M6 2604167
diff --git a/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.h b/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.h
index be8a9605d..c34611e78 100644
--- a/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.h
+++ b/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.h
@@ -135,6 +135,8 @@ void thc_ltr_unconfig(struct thc_device *dev);
u32 thc_int_cause_read(struct thc_device *dev);
int thc_interrupt_handler(struct thc_device *dev);
int thc_port_select(struct thc_device *dev, enum thc_port_type port_type);
+int thc_port_reset(struct thc_device *dev, bool hold);
+int thc_port_supported(struct thc_device *dev, bool *supported);
int thc_spi_read_config(struct thc_device *dev, u32 spi_freq_val,
u32 io_mode, u32 opcode, u32 spi_rd_mps);
int thc_spi_write_config(struct thc_device *dev, u32 spi_freq_val,
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH v11 2/6] HID: intel-thc-hid: intel-ipts: bring up the port
2026-12-03 2:04 [PATCH v11 0/6] HID: intel-thc-hid: intel-ipts: support THC IPTS ports Emfox Zhou
2026-12-03 2:04 ` [PATCH v11 1/6] HID: intel-thc-hid: intel-thc: expose the port operations IPTS needs Emfox Zhou
@ 2026-12-03 2:04 ` Emfox Zhou
2026-12-03 2:15 ` sashiko-bot
2026-12-03 2:04 ` [PATCH v11 3/6] HID: intel-thc-hid: intel-ipts: receive reports Emfox Zhou
` (3 subsequent siblings)
5 siblings, 1 reply; 10+ messages in thread
From: Emfox Zhou @ 2026-12-03 2:04 UTC (permalink / raw)
To: Even Xu, Xinpeng Sun, Jiri Kosina, Benjamin Tissoires
Cc: linux-input, linux-kernel
Add the port driver: select the SPI port, reset the controller, read its
configuration block over PIO, apply the SPI timing it reports and enable
the touch engine. Teardown holds the controller in reset.
Signed-off-by: Emfox Zhou <emfox@debian.org>
---
drivers/hid/intel-thc-hid/Kconfig | 13 ++
drivers/hid/intel-thc-hid/Makefile | 4 +
.../intel-thc-hid/intel-ipts/ipts-protocol.c | 212 ++++++++++++++++++
drivers/hid/intel-thc-hid/intel-ipts/ipts.h | 140 ++++++++++++
.../hid/intel-thc-hid/intel-ipts/pci-ipts.c | 124 ++++++++++
5 files changed, 493 insertions(+)
create mode 100644 drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c
create mode 100644 drivers/hid/intel-thc-hid/intel-ipts/ipts.h
create mode 100644 drivers/hid/intel-thc-hid/intel-ipts/pci-ipts.c
diff --git a/drivers/hid/intel-thc-hid/Kconfig b/drivers/hid/intel-thc-hid/Kconfig
index 9d74e53b8..40e3ce198 100644
--- a/drivers/hid/intel-thc-hid/Kconfig
+++ b/drivers/hid/intel-thc-hid/Kconfig
@@ -40,4 +40,17 @@ config INTEL_QUICKI2C
Say Y/M here if you want to support Intel QuickI2C. If unsure, say N.
+config INTEL_IPTS
+ tristate "Intel Touch Host Controller IPTS mode"
+ depends on INTEL_THC_HID
+ help
+ Support for THC ports in IPTS mode. Instead of HIDSPI these ports
+ drive the touch controller over plain SPI and carry its reports in a
+ private framing. This driver brings such a port up and reads the
+ controller's configuration block, which describes the report
+ framing.
+
+ Say Y/M here if you have an Intel touch controller that runs in IPTS
+ mode. If unsure, say N.
+
endmenu
diff --git a/drivers/hid/intel-thc-hid/Makefile b/drivers/hid/intel-thc-hid/Makefile
index f1182253b..a3d6f8b18 100644
--- a/drivers/hid/intel-thc-hid/Makefile
+++ b/drivers/hid/intel-thc-hid/Makefile
@@ -20,4 +20,8 @@ intel-quicki2c-objs += intel-quicki2c/pci-quicki2c.o
intel-quicki2c-objs += intel-quicki2c/quicki2c-hid.o
intel-quicki2c-objs += intel-quicki2c/quicki2c-protocol.o
+obj-$(CONFIG_INTEL_IPTS) += intel-ipts.o
+intel-ipts-objs += intel-ipts/pci-ipts.o
+intel-ipts-objs += intel-ipts/ipts-protocol.o
+
ccflags-y += -I $(src)/intel-thc
diff --git a/drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c b/drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c
new file mode 100644
index 000000000..5d3d8d400
--- /dev/null
+++ b/drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c
@@ -0,0 +1,212 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Transport for THC ports in IPTS mode: plain SPI with a private framing.
+ */
+
+#include <linux/delay.h>
+#include <linux/string.h>
+
+#include "intel-thc-hw.h"
+#include "ipts.h"
+
+/* Bulk transfers have no address phase, so the SPI opcode is zero. */
+#define THC_IPTS_SPI_OPCODE 0
+
+/* Clock used until the controller has been read, the slowest known-good one. */
+#define THC_IPTS_BOOT_SPI_HZ 7812500
+
+/* Time the controller needs after reset before it answers on the bus. */
+#define THC_IPTS_BOOT_MS 100
+
+static int thc_ipts_read_cfg(struct thc_ipts *idev)
+{
+ int ret, try;
+
+ for (try = 0; try < THC_IPTS_CFG_TRIES; try++) {
+ u32 got = 0;
+
+ memset(&idev->cfg, 0, sizeof(idev->cfg));
+ ret = thc_tic_pio_read(idev->thc, 0, THC_IPTS_CFG_SIZE, &got,
+ (u32 *)&idev->cfg);
+ if (!ret && got == THC_IPTS_CFG_SIZE &&
+ le32_to_cpu(idev->cfg.device_id) == THC_IPTS_TIC_ID)
+ return 0;
+
+ if (try + 1 < THC_IPTS_CFG_TRIES)
+ msleep(THC_IPTS_CFG_RETRY_MS);
+ }
+
+ dev_err(idev->dev,
+ "controller did not answer, last id 0x%08x, error %d\n",
+ le32_to_cpu(idev->cfg.device_id), ret ?: -EIO);
+
+ return ret ?: -EIO;
+}
+
+static int thc_ipts_spi_set(struct thc_ipts *idev, u32 hz, u32 mode)
+{
+ int ret;
+
+ /* No multi-packet transfers in this mode, so both sizes stay zero. */
+ ret = thc_spi_read_config(idev->thc, hz, mode, THC_IPTS_SPI_OPCODE, 0);
+ if (ret)
+ return ret;
+
+ return thc_spi_write_config(idev->thc, hz, mode, THC_IPTS_SPI_OPCODE,
+ 0, 0);
+}
+
+static u32 thc_ipts_spi_mode(u32 cfg)
+{
+ if (cfg & THC_IPTS_SPI_QUAD)
+ return THC_QUAD_IO;
+ if (cfg & THC_IPTS_SPI_DUAL)
+ return THC_DUAL_IO;
+
+ return THC_SINGLE_IO;
+}
+
+static int thc_ipts_touch_set(struct thc_ipts *idev, bool enable)
+{
+ u32 touch = le32_to_cpu(idev->cfg.touch_cfg);
+
+ touch &= ~(THC_IPTS_TOUCH_POWER_MASK | THC_IPTS_TOUCH_ENABLE |
+ THC_IPTS_TOUCH_HID_REPORT);
+ touch |= THC_IPTS_TOUCH_POWER(enable ? THC_IPTS_TOUCH_POWER_ON : 0);
+ if (enable)
+ touch |= THC_IPTS_TOUCH_ENABLE | THC_IPTS_TOUCH_HID_REPORT;
+
+ return thc_tic_pio_write(idev->thc, THC_IPTS_CFG_TOUCH, sizeof(touch),
+ &touch);
+}
+
+/**
+ * thc_ipts_port_start - bring one port up
+ * @idev: port to start
+ */
+int thc_ipts_port_start(struct thc_ipts *idev)
+{
+ u32 hz, mode, cfg, div;
+ bool supported;
+ int ret;
+
+ ret = thc_interrupt_quiesce(idev->thc, true);
+ if (ret)
+ return ret;
+
+ ret = thc_port_select(idev->thc, THC_PORT_TYPE_SPI);
+ if (ret)
+ return ret;
+
+ ret = thc_port_supported(idev->thc, &supported);
+ if (ret)
+ return ret;
+ if (!supported)
+ return -ENODEV;
+
+ /* Without this the engines write into the configuration block. */
+ thc_spi_input_output_address_config(idev->thc, 0, THC_IPTS_BULK_ADDR,
+ THC_IPTS_BULK_ADDR);
+
+ /*
+ * Firmware may leave the engines armed with its PRD base: stop them
+ * before the controller pushes frames into stale pages.
+ */
+ thc_dma_unconfigure(idev->thc);
+ /*
+ * Free tables a failed reset left behind.
+ */
+ thc_dma_release(idev->thc);
+
+ ret = thc_ipts_spi_set(idev, THC_IPTS_BOOT_SPI_HZ, THC_SINGLE_IO);
+ if (ret)
+ return ret;
+
+ /* The first configuration read is unreliable without edge detect. */
+ thc_int_trigger_type_select(idev->thc, true);
+
+ /*
+ * Firmware may leave the line released: assert and release it before
+ * asking the controller anything.
+ */
+ ret = thc_port_reset(idev->thc, false);
+ if (ret)
+ return ret;
+
+ /* Out of reset from here: a teardown has to put it back. */
+ WRITE_ONCE(idev->state, THC_IPTS_RESET);
+
+ msleep(THC_IPTS_BOOT_MS);
+
+ ret = thc_ipts_read_cfg(idev);
+ if (ret)
+ return ret;
+
+ idev->vendor = le16_to_cpu(idev->cfg.vendor);
+ idev->product = le16_to_cpu(idev->cfg.product);
+ idev->max_rx = THC_IPTS_RX_BUF_SIZE(le32_to_cpu(idev->cfg.buf_sizes));
+ idev->max_tx = THC_IPTS_TX_BUF_SIZE(le32_to_cpu(idev->cfg.buf_sizes));
+ /* TX_BUF_SIZE() never goes below 64, so only the rx half can fail. */
+ if (idev->max_rx <= THC_IPTS_RX_HDR_SIZE) {
+ dev_err(idev->dev, "receive buffer of %zu bytes is too small\n",
+ idev->max_rx);
+ return -ENODEV;
+ }
+ /* The configuration block reports the bus timing as a divisor. */
+ cfg = le32_to_cpu(idev->cfg.spi_cfg);
+ div = THC_IPTS_SPI_CLKDIV(cfg);
+ if (div) {
+ hz = THC_IPTS_SPI_BASE_HZ / div;
+ if (cfg & THC_IPTS_SPI_CLKDIV8)
+ hz /= 8;
+ /*
+ * Round up: the gear table is rounded to 100 Hz and takes the entry not
+ * above the request.
+ */
+ hz = DIV_ROUND_UP(hz, 100) * 100;
+ } else {
+ hz = 0;
+ }
+ mode = thc_ipts_spi_mode(cfg);
+ if (!hz) {
+ dev_warn_once(idev->dev, "controller reports divisor 0\n");
+ hz = THC_IPTS_BOOT_SPI_HZ;
+ mode = THC_SINGLE_IO;
+ }
+
+ ret = thc_ipts_spi_set(idev, hz, mode);
+ if (ret) {
+ dev_warn(idev->dev,
+ "cannot follow the SPI timing, going slow\n");
+ ret = thc_ipts_spi_set(idev, THC_IPTS_BOOT_SPI_HZ,
+ THC_SINGLE_IO);
+ if (ret)
+ return ret;
+ }
+
+ ret = thc_interrupt_quiesce(idev->thc, false);
+ if (ret)
+ return ret;
+
+ ret = thc_ipts_touch_set(idev, true);
+ if (ret)
+ return ret;
+ WRITE_ONCE(idev->state, THC_IPTS_CONFIG);
+
+ return 0;
+}
+
+/**
+ * thc_ipts_port_stop - tear one port down
+ * @idev: port to stop
+ */
+void thc_ipts_port_stop(struct thc_ipts *idev)
+{
+ if (READ_ONCE(idev->state) >= THC_IPTS_CONFIG)
+ thc_ipts_touch_set(idev, false);
+
+ /* Keep the touch IC in reset until the next start. */
+ thc_port_reset(idev->thc, true);
+ thc_interrupt_quiesce(idev->thc, true);
+ WRITE_ONCE(idev->state, THC_IPTS_DOWN);
+}
diff --git a/drivers/hid/intel-thc-hid/intel-ipts/ipts.h b/drivers/hid/intel-thc-hid/intel-ipts/ipts.h
new file mode 100644
index 000000000..f8a55ac45
--- /dev/null
+++ b/drivers/hid/intel-thc-hid/intel-ipts/ipts.h
@@ -0,0 +1,140 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _THC_IPTS_H_
+#define _THC_IPTS_H_
+
+#include <linux/bitfield.h>
+#include <linux/bits.h>
+#include <linux/build_bug.h>
+#include <linux/device.h>
+#include <linux/interrupt.h>
+#include <linux/types.h>
+
+#include "intel-thc-dev.h"
+
+/* Configuration block the controller keeps at address zero. */
+#define THC_IPTS_CFG_SIZE 64
+#define THC_IPTS_CFG_TOUCH 0x0c
+#define THC_IPTS_TIC_ID 0x43495424 /* "$TIC" */
+
+/* Bulk data window. The DMA engines have to be pointed here, not at zero. */
+#define THC_IPTS_BULK_ADDR 0x1000
+
+#define PCI_DEVICE_ID_INTEL_THC_LKF_PORT1 0x98d0
+#define PCI_DEVICE_ID_INTEL_THC_LKF_PORT2 0x98d1
+#define PCI_DEVICE_ID_INTEL_THC_TGL_LP_PORT1 0xa0d0
+#define PCI_DEVICE_ID_INTEL_THC_TGL_LP_PORT2 0xa0d1
+#define PCI_DEVICE_ID_INTEL_THC_TGL_H_PORT1 0x43d0
+#define PCI_DEVICE_ID_INTEL_THC_TGL_H_PORT2 0x43d1
+#define PCI_DEVICE_ID_INTEL_THC_ADL_S_PORT1 0x7ad8
+#define PCI_DEVICE_ID_INTEL_THC_ADL_S_PORT2 0x7ad9
+#define PCI_DEVICE_ID_INTEL_THC_ADL_P_PORT1 0x51d0
+#define PCI_DEVICE_ID_INTEL_THC_ADL_P_PORT2 0x51d1
+#define PCI_DEVICE_ID_INTEL_THC_ADL_M_PORT1 0x54d0
+#define PCI_DEVICE_ID_INTEL_THC_ADL_M_PORT2 0x54d1
+#define PCI_DEVICE_ID_INTEL_THC_RPL_S_PORT1 0x7a58
+#define PCI_DEVICE_ID_INTEL_THC_RPL_S_PORT2 0x7a59
+
+/* The block fills in while the controller starts, so $TIC is retried. */
+#define THC_IPTS_CFG_TRIES 10
+#define THC_IPTS_CFG_RETRY_MS 100
+
+#define THC_IPTS_CFG_KNOWN 40
+
+/* The block as far as this driver reads it, padded to its full size. */
+struct thc_ipts_cfg {
+ __le32 irq_cause;
+ __le32 error; /* protocol error flags, not used */
+ __le32 buf_sizes; /* Rx/Tx sizes, see *_BUF_SIZE() */
+ __le32 touch_cfg; /* touch engine control, writable */
+ __le32 touch_state; /* touch engine state, not used */
+ __le32 device_id; /* THC_IPTS_TIC_ID after a good read */
+ __le32 spi_cfg;
+ __le16 vendor;
+ __le16 product;
+ __le32 revision;
+ __le32 fw_version; /* changes on every reset, not a version */
+ u8 rest[THC_IPTS_CFG_SIZE - THC_IPTS_CFG_KNOWN];
+};
+
+static_assert(sizeof(struct thc_ipts_cfg) == THC_IPTS_CFG_SIZE);
+
+/* The core is handed the struct itself, so the offsets are part of the deal. */
+static_assert(offsetof(struct thc_ipts_cfg, buf_sizes) == 0x08);
+static_assert(offsetof(struct thc_ipts_cfg, touch_cfg) == 0x0c);
+static_assert(offsetof(struct thc_ipts_cfg, spi_cfg) == 0x18);
+static_assert(offsetof(struct thc_ipts_cfg, vendor) == 0x1c);
+static_assert(offsetof(struct thc_ipts_cfg, product) == 0x1e);
+static_assert(offsetof(struct thc_ipts_cfg, revision) == 0x20);
+static_assert(offsetof(struct thc_ipts_cfg, device_id) == 0x14);
+
+/* Buffer sizes are stored as (size / 64) - 1, Rx in bits 13:0, Tx in 23:14. */
+#define THC_IPTS_DMA_UNIT 64
+#define THC_IPTS_RX_BUF_SIZE(val) \
+ ((size_t)(FIELD_GET(GENMASK(13, 0), (val)) + 1) * THC_IPTS_DMA_UNIT)
+#define THC_IPTS_TX_BUF_SIZE(val) \
+ ((size_t)(FIELD_GET(GENMASK(23, 14), (val)) + 1) * THC_IPTS_DMA_UNIT)
+
+#define THC_IPTS_TOUCH_ENABLE BIT(0)
+#define THC_IPTS_TOUCH_HID_REPORT BIT(2)
+#define THC_IPTS_TOUCH_POWER_ON 3
+#define THC_IPTS_TOUCH_POWER_MASK GENMASK(5, 3)
+#define THC_IPTS_TOUCH_POWER(val) \
+ FIELD_PREP(THC_IPTS_TOUCH_POWER_MASK, (val))
+
+#define THC_IPTS_SPI_CLKDIV(val) FIELD_GET(GENMASK(3, 1), (val))
+#define THC_IPTS_SPI_CLKDIV8 BIT(4)
+#define THC_IPTS_SPI_DUAL BIT(6)
+#define THC_IPTS_SPI_QUAD BIT(7)
+/* Base clock of the SPI gears; the divisor is relative to it. */
+#define THC_IPTS_SPI_BASE_HZ 125000000
+
+/* Code and length in front of the payload; the long one is inbound. */
+#define THC_IPTS_RX_HDR_SIZE 64
+#define THC_IPTS_TX_HDR_SIZE 8
+
+/**
+ * enum thc_ipts_state - how far the port has been brought up
+ * @THC_IPTS_DOWN: no controller, no buffers, no HID device
+ * @THC_IPTS_RESET: out of reset, configuration not read yet
+ * @THC_IPTS_CONFIG: configuration read, engines not configured
+ * @THC_IPTS_DMA: DMA engines configured and may be written to
+ * @THC_IPTS_UP: HID device registered
+ *
+ * The steps only move forward while the port starts and only backward while
+ * it stops. A failure therefore leaves the port at the last step that
+ * completed, and the teardown knows how far to unwind.
+ */
+enum thc_ipts_state {
+ THC_IPTS_DOWN,
+ THC_IPTS_RESET,
+ THC_IPTS_CONFIG,
+ THC_IPTS_DMA,
+ THC_IPTS_UP,
+};
+
+/**
+ * struct thc_ipts - state of one THC port in IPTS mode
+ * @dev: device used for resources and logging
+ * @thc: shared THC core context
+ * @cfg: configuration block last read from the controller
+ * @vendor: vendor ID the controller reports for its HID device
+ * @product: product ID the controller reports for its HID device
+ * @max_rx: largest frame the controller may send, in bytes
+ * @max_tx: largest frame the driver may send, in bytes
+ * @state: how far the port has been brought up, see enum thc_ipts_state
+ */
+struct thc_ipts {
+ struct device *dev;
+ struct thc_device *thc;
+ struct thc_ipts_cfg cfg;
+ u16 vendor;
+ u16 product;
+ size_t max_rx;
+ size_t max_tx;
+ enum thc_ipts_state state;
+};
+
+int thc_ipts_port_start(struct thc_ipts *idev);
+void thc_ipts_port_stop(struct thc_ipts *idev);
+
+#endif /* _THC_IPTS_H_ */
diff --git a/drivers/hid/intel-thc-hid/intel-ipts/pci-ipts.c b/drivers/hid/intel-thc-hid/intel-ipts/pci-ipts.c
new file mode 100644
index 000000000..5e826d092
--- /dev/null
+++ b/drivers/hid/intel-thc-hid/intel-ipts/pci-ipts.c
@@ -0,0 +1,124 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * PCI glue for the Intel Touch Host Controller in IPTS mode. Ports that
+ * the HIDSPI driver already claims are deliberately left out of the ID table.
+ */
+
+#include <linux/dma-mapping.h>
+#include <linux/module.h>
+#include <linux/pci.h>
+#include <linux/workqueue.h>
+
+#include "ipts.h"
+
+static const struct pci_device_id thc_ipts_pci_ids[] = {
+ /* Lakefield */
+ {PCI_DEVICE_DATA(INTEL, THC_LKF_PORT1, NULL)},
+ {PCI_DEVICE_DATA(INTEL, THC_LKF_PORT2, NULL)},
+ /* Tiger Lake */
+ {PCI_DEVICE_DATA(INTEL, THC_TGL_LP_PORT1, NULL)},
+ {PCI_DEVICE_DATA(INTEL, THC_TGL_LP_PORT2, NULL)},
+ {PCI_DEVICE_DATA(INTEL, THC_TGL_H_PORT1, NULL)},
+ {PCI_DEVICE_DATA(INTEL, THC_TGL_H_PORT2, NULL)},
+ /* Alder Lake */
+ {PCI_DEVICE_DATA(INTEL, THC_ADL_S_PORT1, NULL)},
+ {PCI_DEVICE_DATA(INTEL, THC_ADL_S_PORT2, NULL)},
+ {PCI_DEVICE_DATA(INTEL, THC_ADL_P_PORT1, NULL)},
+ {PCI_DEVICE_DATA(INTEL, THC_ADL_P_PORT2, NULL)},
+ {PCI_DEVICE_DATA(INTEL, THC_ADL_M_PORT1, NULL)},
+ {PCI_DEVICE_DATA(INTEL, THC_ADL_M_PORT2, NULL)},
+ /* Raptor Lake */
+ {PCI_DEVICE_DATA(INTEL, THC_RPL_S_PORT1, NULL)},
+ {PCI_DEVICE_DATA(INTEL, THC_RPL_S_PORT2, NULL)},
+ {}
+};
+MODULE_DEVICE_TABLE(pci, thc_ipts_pci_ids);
+
+static int thc_ipts_probe(struct pci_dev *pci, const struct pci_device_id *id)
+{
+ struct thc_ipts *idev;
+ void __iomem *mem;
+ int ret;
+
+ idev = devm_kzalloc(&pci->dev, sizeof(*idev), GFP_KERNEL);
+ if (!idev)
+ return -ENOMEM;
+
+ idev->dev = &pci->dev;
+ pci_set_drvdata(pci, idev);
+
+ ret = pcim_enable_device(pci);
+ if (ret) {
+ dev_err(idev->dev,
+ "cannot enable the PCI function, error %d\n", ret);
+ return ret;
+ }
+
+ pci_set_master(pci);
+
+ mem = pcim_iomap_region(pci, 0, KBUILD_MODNAME);
+ ret = PTR_ERR_OR_ZERO(mem);
+ if (ret) {
+ dev_err(idev->dev, "cannot map the register space, error %d\n",
+ ret);
+ return ret;
+ }
+
+ ret = dma_set_mask_and_coherent(idev->dev, DMA_BIT_MASK(64));
+ if (ret)
+ ret = dma_set_mask_and_coherent(idev->dev, DMA_BIT_MASK(32));
+ if (ret) {
+ dev_err(idev->dev, "no usable DMA configuration, error %d\n",
+ ret);
+ return ret;
+ }
+
+ idev->thc = thc_dev_init(idev->dev, mem);
+ if (IS_ERR(idev->thc))
+ return PTR_ERR(idev->thc);
+
+ ret = thc_ipts_port_start(idev);
+ if (ret) {
+ thc_ipts_port_stop(idev);
+ return ret;
+ }
+
+ dev_info(idev->dev, "controller %04x:%04x rev %u\n",
+ idev->vendor, idev->product, le32_to_cpu(idev->cfg.revision));
+ dev_dbg(idev->dev, "spi cfg 0x%08x, rx %zu tx %zu bytes\n",
+ le32_to_cpu(idev->cfg.spi_cfg), idev->max_rx, idev->max_tx);
+
+ return 0;
+}
+
+static void thc_ipts_remove(struct pci_dev *pci)
+{
+ struct thc_ipts *idev = pci_get_drvdata(pci);
+
+ thc_ipts_port_stop(idev);
+ pci_clear_master(pci);
+}
+
+static void thc_ipts_shutdown(struct pci_dev *pci)
+{
+ struct thc_ipts *idev = pci_get_drvdata(pci);
+
+ if (idev)
+ thc_ipts_port_stop(idev);
+}
+
+static struct pci_driver thc_ipts_driver = {
+ .name = "intel-ipts",
+ .id_table = thc_ipts_pci_ids,
+ .probe = thc_ipts_probe,
+ .remove = thc_ipts_remove,
+ .shutdown = thc_ipts_shutdown,
+ /* Bring-up waits for the controller, up to a few seconds. */
+ .driver.probe_type = PROBE_PREFER_ASYNCHRONOUS,
+};
+module_pci_driver(thc_ipts_driver);
+
+MODULE_AUTHOR("Emfox Zhou <emfox@debian.org>");
+MODULE_DESCRIPTION("IPTS transport for the Intel Touch Host Controller");
+MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS("INTEL_THC");
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH v11 3/6] HID: intel-thc-hid: intel-ipts: receive reports
2026-12-03 2:04 [PATCH v11 0/6] HID: intel-thc-hid: intel-ipts: support THC IPTS ports Emfox Zhou
2026-12-03 2:04 ` [PATCH v11 1/6] HID: intel-thc-hid: intel-thc: expose the port operations IPTS needs Emfox Zhou
2026-12-03 2:04 ` [PATCH v11 2/6] HID: intel-thc-hid: intel-ipts: bring up the port Emfox Zhou
@ 2026-12-03 2:04 ` Emfox Zhou
2026-12-03 2:26 ` sashiko-bot
2026-12-03 2:04 ` [PATCH v11 4/6] HID: intel-thc-hid: intel-ipts: add the HID device Emfox Zhou
` (2 subsequent siblings)
5 siblings, 1 reply; 10+ messages in thread
From: Emfox Zhou @ 2026-12-03 2:04 UTC (permalink / raw)
To: Even Xu, Xinpeng Sun, Jiri Kosina, Benjamin Tissoires
Cc: linux-input, linux-kernel
Start the DMA engines and the port interrupt and carry frames in both
directions: hand input reports to the HID device and keep descriptor and
feature answers for the handshakes. Transfer errors are recovered from
a work item.
Signed-off-by: Emfox Zhou <emfox@debian.org>
---
.../intel-thc-hid/intel-ipts/ipts-protocol.c | 467 +++++++++++++++++-
drivers/hid/intel-thc-hid/intel-ipts/ipts.h | 83 ++++
.../hid/intel-thc-hid/intel-ipts/pci-ipts.c | 52 ++
3 files changed, 600 insertions(+), 2 deletions(-)
diff --git a/drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c b/drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c
index 5d3d8d400..9ef4b3cfa 100644
--- a/drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c
+++ b/drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c
@@ -3,8 +3,15 @@
* Transport for THC ports in IPTS mode: plain SPI with a private framing.
*/
+#include <linux/cleanup.h>
#include <linux/delay.h>
+#include <linux/hid.h>
+#include <linux/interrupt.h>
+#include <linux/minmax.h>
+#include <linux/sizes.h>
#include <linux/string.h>
+#include <linux/unaligned.h>
+#include <linux/workqueue.h>
#include "intel-thc-hw.h"
#include "ipts.h"
@@ -15,9 +22,18 @@
/* Clock used until the controller has been read, the slowest known-good one. */
#define THC_IPTS_BOOT_SPI_HZ 7812500
+/* One page per PRD entry; a larger frame cannot be received. */
+#define THC_IPTS_RX_MAX (PRD_ENTRIES_NUM * SZ_4K)
+
/* Time the controller needs after reset before it answers on the bus. */
#define THC_IPTS_BOOT_MS 100
+/*
+ * The core reports one cause per pass: allow a pass per cause and one to
+ * confirm the status is drained.
+ */
+#define THC_IPTS_IRQ_PASSES (THC_UNKNOWN_INT + 1)
+
static int thc_ipts_read_cfg(struct thc_ipts *idev)
{
int ret, try;
@@ -80,6 +96,411 @@ static int thc_ipts_touch_set(struct thc_ipts *idev, bool enable)
&touch);
}
+/*
+ * Frames are built in @idev->tx_frame, so the lock is taken here rather than
+ * by the callers: none of them may hold it across a wait.
+ */
+int thc_ipts_send(struct thc_ipts *idev, u32 code, const void *data,
+ size_t len, unsigned int *seq)
+{
+ size_t padded = round_up(len, 4);
+ u8 *frame = idev->tx_frame;
+ int ret;
+
+ /*
+ * A request from the handler thread would wait for a completion only that
+ * thread delivers: refuse it.
+ */
+ if (current == READ_ONCE(idev->irq_thread))
+ return -EBUSY;
+
+ guard(mutex)(&idev->tx_lock);
+
+ if (READ_ONCE(idev->stopping))
+ return -ESHUTDOWN;
+ /* A reset is queued or running: the write would only time out. */
+ if (READ_ONCE(idev->recover_pending) || READ_ONCE(idev->recovering))
+ return -EAGAIN;
+ /* No engine to write into: waiting for a reset would not help. */
+ if (READ_ONCE(idev->state) < THC_IPTS_DMA)
+ return -ENODEV;
+ if (THC_IPTS_TX_HDR_SIZE + padded > idev->max_tx)
+ return -EMSGSIZE;
+
+ put_unaligned_le32(code, frame);
+ put_unaligned_le32((u32)len, frame + 4);
+ memset(frame + THC_IPTS_TX_HDR_SIZE, 0, padded);
+ if (len && data)
+ memcpy(frame + THC_IPTS_TX_HDR_SIZE, data, len);
+
+ ret = thc_dma_write(idev->thc, frame, THC_IPTS_TX_HDR_SIZE + padded);
+ /*
+ * Counted under the lock, so a reset that takes it afterwards drops this
+ * frame.
+ */
+ if (!ret && seq)
+ *seq = READ_ONCE(idev->recover_seq);
+
+ return ret;
+}
+
+static void thc_ipts_descriptor(struct thc_ipts *idev, const u8 *data,
+ size_t len)
+{
+ /* One answer per request: a late reply must not overwrite it. */
+ if (len <= THC_IPTS_DESC_PREAMBLE || READ_ONCE(idev->have_desc))
+ return;
+
+ len = min(len - THC_IPTS_DESC_PREAMBLE, idev->max_rx);
+ memcpy(idev->desc, data + THC_IPTS_DESC_PREAMBLE, len);
+ idev->desc_len = len;
+ /* The bytes and the length have to land before the flag is seen. */
+ smp_store_release(&idev->have_desc, true);
+ wake_up(&idev->wq);
+}
+
+static void thc_ipts_feature(struct thc_ipts *idev, const u8 *data,
+ size_t len)
+{
+ if (!len)
+ return;
+
+ /* Only a frame that arrives while a request is waiting is taken. */
+ mutex_lock(&idev->feature_lock);
+ if (!idev->feature_pending ||
+ (idev->feature_id && data[0] != idev->feature_id)) {
+ mutex_unlock(&idev->feature_lock);
+ return;
+ }
+
+ len = min(len, idev->max_rx);
+ memcpy(idev->feature, data, len);
+ idev->feature_len = len;
+ WRITE_ONCE(idev->feature_done, true);
+ mutex_unlock(&idev->feature_lock);
+
+ wake_up(&idev->wq);
+}
+
+static void thc_ipts_handle_frame(struct thc_ipts *idev, size_t len)
+{
+ const u8 *payload = idev->rx_frame + THC_IPTS_RX_HDR_SIZE;
+ size_t capacity = idev->max_rx - THC_IPTS_RX_HDR_SIZE;
+ u32 code, size;
+
+ if (len < THC_IPTS_RX_HDR_SIZE)
+ return;
+
+ code = get_unaligned_le32(idev->rx_frame);
+ /* The header may claim more than what actually arrived. */
+ size = min_t(u32, get_unaligned_le32(idev->rx_frame + 4), capacity);
+ size = min_t(u32, size, len - THC_IPTS_RX_HDR_SIZE);
+
+ switch (code) {
+ case THC_IPTS_RX_INPUT: {
+ struct hid_device *hdev = READ_ONCE(idev->hid);
+
+ if (hdev)
+ hid_safe_input_report(hdev, HID_INPUT_REPORT,
+ (u8 *)payload, capacity, size, 1);
+ break;
+ }
+ case THC_IPTS_RX_DESC:
+ thc_ipts_descriptor(idev, payload, size);
+ break;
+ case THC_IPTS_RX_FEATURE:
+ thc_ipts_feature(idev, payload, size);
+ break;
+ case THC_IPTS_RX_RESET:
+ dev_warn(idev->dev, "controller asked for a reset\n");
+ break;
+ default:
+ dev_dbg(idev->dev, "frame code %u, %u bytes\n", code, size);
+ break;
+ }
+}
+
+static void thc_ipts_dma_stop(struct thc_ipts *idev)
+{
+ /*
+ * A failed reset leaves the state below DMA with the tables still
+ * allocated.
+ */
+ if (READ_ONCE(idev->state) >= THC_IPTS_CONFIG)
+ thc_dma_unconfigure(idev->thc);
+
+ WRITE_ONCE(idev->state, THC_IPTS_CONFIG);
+ thc_dma_release(idev->thc);
+}
+
+static int thc_ipts_resize(struct thc_ipts *idev, u8 **buf, size_t *have,
+ size_t want)
+{
+ u8 *p;
+
+ if (*have >= want)
+ return 0;
+
+ p = devm_krealloc(idev->dev, *buf, want, GFP_KERNEL);
+ if (!p)
+ return -ENOMEM;
+
+ *buf = p;
+ *have = want;
+
+ return 0;
+}
+
+static int thc_ipts_dma_start(struct thc_ipts *idev)
+{
+ int ret;
+
+ /*
+ * The core refuses frames larger than the receive ring: fail here instead
+ * of in sgl_alloc().
+ */
+ if (idev->max_rx > THC_IPTS_RX_MAX) {
+ dev_err(idev->dev, "receive buffer of %zu bytes is too large\n",
+ idev->max_rx);
+ return -ENODEV;
+ }
+
+ /*
+ * Follow the limits a resume reports, or the engines write past the
+ * buffers.
+ */
+ ret = thc_ipts_resize(idev, &idev->rx_frame, &idev->buf_rx,
+ round_up(idev->max_rx, SZ_4K));
+ if (!ret)
+ ret = thc_ipts_resize(idev, &idev->tx_frame, &idev->buf_tx,
+ round_up(idev->max_tx, 4));
+ if (!ret)
+ ret = thc_ipts_resize(idev, &idev->desc, &idev->buf_desc,
+ idev->max_rx);
+ if (!ret)
+ ret = thc_ipts_resize(idev, &idev->feature, &idev->buf_feat,
+ idev->max_rx);
+ if (ret)
+ return ret;
+
+ /* Only RXDMA2 (in) and TXDMA (out) are used; the other two stay 0. */
+ ret = thc_dma_set_max_packet_sizes(idev->thc, 0, idev->max_rx,
+ idev->max_tx, 0);
+ if (ret)
+ return ret;
+
+ ret = thc_dma_allocate(idev->thc);
+ if (ret)
+ return ret;
+
+ ret = thc_dma_configure(idev->thc);
+ if (ret) {
+ thc_dma_release(idev->thc);
+ return ret;
+ }
+
+ return 0;
+}
+
+irqreturn_t thc_ipts_hardirq(int irq, void *data)
+{
+ struct thc_ipts *idev = data;
+
+ /*
+ * Nothing of ours is pending: the cause belongs to a sharer. A reset
+ * in flight owns the status registers and acknowledges nothing.
+ */
+ if (READ_ONCE(idev->state) < THC_IPTS_DMA ||
+ READ_ONCE(idev->recovering))
+ return IRQ_NONE;
+
+ /* Level triggered and shared: mask first, or it stays asserted. */
+ thc_interrupt_enable(idev->thc, false);
+
+ /*
+ * A teardown still needs this thread: it delivers the completion a
+ * pending write holds the bus lock for.
+ */
+ return IRQ_WAKE_THREAD;
+}
+
+static int thc_ipts_read_frames(struct thc_ipts *idev)
+{
+ for (;;) {
+ int finished = 0;
+ size_t len = 0;
+
+ if (thc_rxdma_read(idev->thc, THC_RXDMA2, idev->rx_frame,
+ &len, &finished)) {
+ dev_warn_once(idev->dev,
+ "cannot read a frame from the engine\n");
+ return -EIO;
+ }
+
+ if (!len)
+ break;
+
+ thc_ipts_handle_frame(idev, len);
+ if (finished)
+ break;
+ }
+
+ return 0;
+}
+
+/*
+ * Refuse new requests; the work item masks the port once a pending write
+ * is done.
+ */
+static void thc_ipts_request_recover(struct thc_ipts *idev)
+{
+ if (READ_ONCE(idev->recover_pending))
+ return;
+
+ WRITE_ONCE(idev->recover_pending, true);
+ schedule_work(&idev->recover_work);
+ /* A request waiting for the port looks at that flag. */
+ wake_up(&idev->wq);
+}
+
+/*
+ * The reset waits for the bus lock a pending write holds.
+ */
+void thc_ipts_recover(struct work_struct *work)
+{
+ struct thc_ipts *idev = container_of(work, struct thc_ipts,
+ recover_work);
+
+ /*
+ * A write on the wire needs the interrupt this reset is about to mask:
+ * let it finish first.
+ */
+ mutex_lock(&idev->tx_lock);
+ WRITE_ONCE(idev->recovering, true);
+ mutex_unlock(&idev->tx_lock);
+
+ /*
+ * Mask, then wait out a run already inside: the status registers belong
+ * to the reset.
+ */
+ thc_interrupt_enable(idev->thc, false);
+ synchronize_irq(idev->irq);
+ /*
+ * A run may have unmasked on its way out: mask again.
+ */
+ thc_interrupt_enable(idev->thc, false);
+
+ if (thc_rxdma_reset(idev->thc)) {
+ /*
+ * Nothing left to reset: stay masked and fail with -ENODEV.
+ */
+ dev_err(idev->dev, "cannot reset the receive engine\n");
+ WRITE_ONCE(idev->state, THC_IPTS_CONFIG);
+ WRITE_ONCE(idev->recover_seq, READ_ONCE(idev->recover_seq) + 1);
+ WRITE_ONCE(idev->recover_pending, false);
+ WRITE_ONCE(idev->recovering, false);
+ wake_up(&idev->wq);
+ return;
+ }
+
+ /*
+ * Clear the queued flag before the running one.
+ */
+ WRITE_ONCE(idev->recover_seq, READ_ONCE(idev->recover_seq) + 1);
+ WRITE_ONCE(idev->recover_pending, false);
+ WRITE_ONCE(idev->recovering, false);
+
+ /* A request waiting out the reset is looking at that flag. */
+ wake_up(&idev->wq);
+
+ if (!READ_ONCE(idev->stopping))
+ thc_interrupt_enable(idev->thc, true);
+}
+
+irqreturn_t thc_ipts_thread(int irq, void *data)
+{
+ struct thc_ipts *idev = data;
+ irqreturn_t ret = IRQ_NONE;
+ bool need_recover = false;
+ bool reported = false;
+ int pass;
+
+ /*
+ * Remember this thread: a request from it would wait for a completion
+ * only it delivers.
+ */
+ WRITE_ONCE(idev->irq_thread, current);
+
+ /*
+ * A sharer can queue a second run: mask again.
+ */
+ thc_interrupt_enable(idev->thc, false);
+
+ /*
+ * The port stays masked while reading: causes only leave status bits.
+ */
+ for (pass = 0; pass < THC_IPTS_IRQ_PASSES; pass++) {
+ int mask;
+
+ /*
+ * A reset owns the status registers: acknowledge nothing here.
+ */
+ if (READ_ONCE(idev->recovering))
+ break;
+
+ mask = thc_interrupt_handler(idev->thc);
+
+ if (mask == BIT(THC_UNKNOWN_INT))
+ break;
+
+ ret = IRQ_HANDLED;
+ if (mask & (BIT(THC_FATAL_ERR_INT) | BIT(THC_TXN_ERR_INT))) {
+ /*
+ * An overrun repeats until the reset: report the first one.
+ */
+ if (!reported && !READ_ONCE(idev->recover_pending)) {
+ dev_err(idev->dev,
+ "transfer error, resetting the engine\n");
+ reported = true;
+ }
+ /* The teardown has its own reset to wait for. */
+ if (!READ_ONCE(idev->stopping))
+ need_recover = true;
+ /*
+ * The core stops at the first cause; a completion behind it would time
+ * out.
+ */
+ continue;
+ }
+
+ /*
+ * A reset is queued or running: read the status, not the ring.
+ */
+ if (need_recover || READ_ONCE(idev->recover_pending) ||
+ READ_ONCE(idev->recovering) ||
+ READ_ONCE(idev->stopping))
+ continue;
+
+ if (mask & BIT(THC_RXDMA2_INT) && thc_ipts_read_frames(idev))
+ need_recover = true;
+ }
+
+ /*
+ * Only queue the reset: a frame read now would corrupt the ring.
+ */
+ if (need_recover)
+ thc_ipts_request_recover(idev);
+
+ /*
+ * Keep the line open unless a reset is running: a write on the wire needs
+ * this handler to deliver its completion.
+ */
+ if (!READ_ONCE(idev->recovering))
+ thc_interrupt_enable(idev->thc, true);
+
+ return ret;
+}
+
/**
* thc_ipts_port_start - bring one port up
* @idev: port to start
@@ -90,6 +511,11 @@ int thc_ipts_port_start(struct thc_ipts *idev)
bool supported;
int ret;
+ WRITE_ONCE(idev->stopping, false);
+ WRITE_ONCE(idev->recover_pending, false);
+ WRITE_ONCE(idev->recovering, false);
+ WRITE_ONCE(idev->feature_off, false);
+
ret = thc_interrupt_quiesce(idev->thc, true);
if (ret)
return ret;
@@ -193,6 +619,16 @@ int thc_ipts_port_start(struct thc_ipts *idev)
return ret;
WRITE_ONCE(idev->state, THC_IPTS_CONFIG);
+ /* Sets the type field to invalid; RXDMA2 start writes the real one. */
+ thc_interrupt_config(idev->thc);
+
+ ret = thc_ipts_dma_start(idev);
+ if (ret)
+ return ret;
+ WRITE_ONCE(idev->state, THC_IPTS_DMA);
+
+ thc_interrupt_enable(idev->thc, true);
+
return 0;
}
@@ -202,11 +638,38 @@ int thc_ipts_port_start(struct thc_ipts *idev)
*/
void thc_ipts_port_stop(struct thc_ipts *idev)
{
+ /*
+ * Refuse requests, then wait for one inside thc_dma_write(): its
+ * completion comes through this handler.
+ */
+ WRITE_ONCE(idev->stopping, true);
+ wake_up(&idev->wq);
+ mutex_lock(&idev->tx_lock);
+ mutex_unlock(&idev->tx_lock);
+
+ thc_interrupt_enable(idev->thc, false);
+ synchronize_irq(idev->irq);
+ cancel_work_sync(&idev->recover_work);
+
+ /* A run that was already in flight may have unmasked on its way out. */
+ thc_interrupt_enable(idev->thc, false);
+
+ /* Stop the controller and keep it in reset until the next start. */
if (READ_ONCE(idev->state) >= THC_IPTS_CONFIG)
thc_ipts_touch_set(idev, false);
- /* Keep the touch IC in reset until the next start. */
thc_port_reset(idev->thc, true);
- thc_interrupt_quiesce(idev->thc, true);
+
+ /*
+ * The tables are not devm-managed; release them even if the engine did
+ * not stop.
+ */
+ if (thc_interrupt_quiesce(idev->thc, true))
+ dev_warn(idev->dev, "engine did not stop, releasing anyway\n");
+ thc_ipts_dma_stop(idev);
WRITE_ONCE(idev->state, THC_IPTS_DOWN);
+
+ /* Wait for a handler that may still look at the ring. */
+ synchronize_irq(idev->irq);
+ thc_interrupt_enable(idev->thc, false);
}
diff --git a/drivers/hid/intel-thc-hid/intel-ipts/ipts.h b/drivers/hid/intel-thc-hid/intel-ipts/ipts.h
index f8a55ac45..f27ad7f03 100644
--- a/drivers/hid/intel-thc-hid/intel-ipts/ipts.h
+++ b/drivers/hid/intel-thc-hid/intel-ipts/ipts.h
@@ -6,8 +6,13 @@
#include <linux/bits.h>
#include <linux/build_bug.h>
#include <linux/device.h>
+#include <linux/hid.h>
#include <linux/interrupt.h>
+#include <linux/mutex.h>
+#include <linux/sched.h>
+#include <linux/spinlock.h>
#include <linux/types.h>
+#include <linux/wait.h>
#include "intel-thc-dev.h"
@@ -38,6 +43,13 @@
#define THC_IPTS_CFG_TRIES 10
#define THC_IPTS_CFG_RETRY_MS 100
+/* A report descriptor arrives with eight bytes of preamble in front of it. */
+#define THC_IPTS_DESC_PREAMBLE 8
+
+/* Retry budget for the report descriptor handshake. */
+#define THC_IPTS_REQ_TRIES 6
+#define THC_IPTS_REQ_TIMEOUT_MS 300
+
#define THC_IPTS_CFG_KNOWN 40
/* The block as far as this driver reads it, padded to its full size. */
@@ -92,6 +104,17 @@ static_assert(offsetof(struct thc_ipts_cfg, device_id) == 0x14);
#define THC_IPTS_RX_HDR_SIZE 64
#define THC_IPTS_TX_HDR_SIZE 8
+/* Frame codes. The same numbers mean different things per direction. */
+#define THC_IPTS_RX_INPUT 3
+#define THC_IPTS_RX_FEATURE 4
+#define THC_IPTS_RX_DESC 5
+#define THC_IPTS_RX_RESET 7
+
+#define THC_IPTS_TX_SET_FEATURE 3
+#define THC_IPTS_TX_GET_FEATURE 4
+#define THC_IPTS_TX_OUTPUT 5
+#define THC_IPTS_TX_GET_DESC 7
+
/**
* enum thc_ipts_state - how far the port has been brought up
* @THC_IPTS_DOWN: no controller, no buffers, no HID device
@@ -116,24 +139,84 @@ enum thc_ipts_state {
* struct thc_ipts - state of one THC port in IPTS mode
* @dev: device used for resources and logging
* @thc: shared THC core context
+ * @irq: interrupt line of the port
* @cfg: configuration block last read from the controller
* @vendor: vendor ID the controller reports for its HID device
* @product: product ID the controller reports for its HID device
* @max_rx: largest frame the controller may send, in bytes
* @max_tx: largest frame the driver may send, in bytes
+ * @buf_rx: allocated size of @rx_frame
+ * @buf_tx: allocated size of @tx_frame
+ * @buf_desc: allocated size of @desc
+ * @buf_feat: allocated size of @feature
+ * @rx_frame: buffer for one received frame, including its header
+ * @tx_frame: buffer for one frame to send, including its header
+ * @desc: copy of the report descriptor, past its preamble
+ * @desc_len: length of @desc
+ * @have_desc: a descriptor arrived and has not been consumed yet
+ * @feature: buffer a feature report is collected in
+ * @feature_len: length of the report in @feature
+ * @feature_id: report ID that was asked for, checked against the answer
+ * @feature_pending: a feature report was asked for and has not arrived yet
+ * @feature_done: a feature report arrived and has not been consumed yet
+ * @feature_off: a request timed out and no more are taken until the port
+ * restarts
+ * @feature_lock: protects the feature report buffer and its flags
+ * @wq: waited on for a report descriptor or a feature report
+ * @tx_lock: serialises writes into @tx_frame
+ * @hid: HID device the reports are handed to, once it has been registered
+ * @recover_work: resets the receive engine after a transfer error
* @state: how far the port has been brought up, see enum thc_ipts_state
+ * @recover_pending: a reset is wanted, refuse new requests
+ * @recovering: a reset is queued or running, ignore the interrupt
+ * @recover_seq: number of resets that ran, to tell a dropped answer apart
+ * @stopping: the port is being torn down, ignore the interrupt
+ * @irq_thread: thread the port interrupt is handled in, once it has run
*/
struct thc_ipts {
struct device *dev;
struct thc_device *thc;
+ int irq;
struct thc_ipts_cfg cfg;
u16 vendor;
u16 product;
size_t max_rx;
size_t max_tx;
+ size_t buf_rx;
+ size_t buf_tx;
+ size_t buf_desc;
+ size_t buf_feat;
+ u8 *rx_frame;
+ u8 *tx_frame;
+ u8 *desc;
+ size_t desc_len;
+ bool have_desc;
+ u8 *feature;
+ size_t feature_len;
+ u8 feature_id;
+ bool feature_pending;
+ bool feature_done;
+ bool feature_off;
+ struct mutex feature_lock; /* feature report handshake */
+ wait_queue_head_t wq;
+ struct mutex tx_lock; /* transmit frame */
+ struct hid_device *hid;
+ struct work_struct recover_work;
enum thc_ipts_state state;
+ bool recover_pending;
+ bool recovering;
+ unsigned int recover_seq;
+ bool stopping;
+ struct task_struct *irq_thread;
};
+int thc_ipts_send(struct thc_ipts *idev, u32 code, const void *data,
+ size_t len, unsigned int *seq);
+
+irqreturn_t thc_ipts_hardirq(int irq, void *data);
+irqreturn_t thc_ipts_thread(int irq, void *data);
+void thc_ipts_recover(struct work_struct *work);
+
int thc_ipts_port_start(struct thc_ipts *idev);
void thc_ipts_port_stop(struct thc_ipts *idev);
diff --git a/drivers/hid/intel-thc-hid/intel-ipts/pci-ipts.c b/drivers/hid/intel-thc-hid/intel-ipts/pci-ipts.c
index 5e826d092..746ef27e2 100644
--- a/drivers/hid/intel-thc-hid/intel-ipts/pci-ipts.c
+++ b/drivers/hid/intel-thc-hid/intel-ipts/pci-ipts.c
@@ -38,6 +38,7 @@ static int thc_ipts_probe(struct pci_dev *pci, const struct pci_device_id *id)
{
struct thc_ipts *idev;
void __iomem *mem;
+ unsigned long flags;
int ret;
idev = devm_kzalloc(&pci->dev, sizeof(*idev), GFP_KERNEL);
@@ -45,7 +46,11 @@ static int thc_ipts_probe(struct pci_dev *pci, const struct pci_device_id *id)
return -ENOMEM;
idev->dev = &pci->dev;
+ mutex_init(&idev->tx_lock);
+ mutex_init(&idev->feature_lock);
+ init_waitqueue_head(&idev->wq);
pci_set_drvdata(pci, idev);
+ INIT_WORK(&idev->recover_work, thc_ipts_recover);
ret = pcim_enable_device(pci);
if (ret) {
@@ -77,9 +82,51 @@ static int thc_ipts_probe(struct pci_dev *pci, const struct pci_device_id *id)
if (IS_ERR(idev->thc))
return PTR_ERR(idev->thc);
+ /*
+ * Firmware may leave the global enable set: mask until a handler is in
+ * place.
+ */
+ thc_interrupt_enable(idev->thc, false);
+
+ /*
+ * Remapping rejects MSI here, so take the legacy pin; MSI only if no pin.
+ */
+ flags = IRQF_SHARED | IRQF_COND_ONESHOT;
+ ret = pci_alloc_irq_vectors(pci, 1, 1, PCI_IRQ_INTX);
+ if (ret < 0) {
+ dev_warn(idev->dev, "no interrupt pin, falling back to MSI\n");
+ ret = pci_alloc_irq_vectors(pci, 1, 1,
+ PCI_IRQ_MSI | PCI_IRQ_MSIX);
+ /*
+ * A message interrupt is neither shared nor oneshot.
+ */
+ flags = 0;
+ }
+ if (ret < 0) {
+ dev_err(idev->dev, "no interrupt vector, error %d\n", ret);
+ return ret;
+ }
+
+ idev->irq = pci_irq_vector(pci, 0);
+ if (idev->irq < 0) {
+ dev_err(idev->dev, "no interrupt number, error %d\n",
+ idev->irq);
+ return idev->irq;
+ }
+
+ /* Oneshot only if a sharer has it: the hardirq masks the port. */
+ ret = devm_request_threaded_irq(idev->dev, idev->irq, thc_ipts_hardirq,
+ thc_ipts_thread, flags, KBUILD_MODNAME,
+ idev);
+ if (ret) {
+ dev_err(idev->dev, "cannot get the interrupt, error %d\n", ret);
+ return ret;
+ }
+
ret = thc_ipts_port_start(idev);
if (ret) {
thc_ipts_port_stop(idev);
+ devm_free_irq(idev->dev, idev->irq, idev);
return ret;
}
@@ -96,6 +143,11 @@ static void thc_ipts_remove(struct pci_dev *pci)
struct thc_ipts *idev = pci_get_drvdata(pci);
thc_ipts_port_stop(idev);
+
+ /*
+ * Free the handler before the vector goes.
+ */
+ devm_free_irq(idev->dev, idev->irq, idev);
pci_clear_master(pci);
}
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH v11 4/6] HID: intel-thc-hid: intel-ipts: add the HID device
2026-12-03 2:04 [PATCH v11 0/6] HID: intel-thc-hid: intel-ipts: support THC IPTS ports Emfox Zhou
` (2 preceding siblings ...)
2026-12-03 2:04 ` [PATCH v11 3/6] HID: intel-thc-hid: intel-ipts: receive reports Emfox Zhou
@ 2026-12-03 2:04 ` Emfox Zhou
2026-12-03 2:04 ` [PATCH v11 5/6] HID: intel-thc-hid: intel-ipts: handle system sleep Emfox Zhou
2026-12-03 2:04 ` [PATCH v11 6/6] HID: intel-thc-hid: intel-ipts: document the IPTS mode Emfox Zhou
5 siblings, 0 replies; 10+ messages in thread
From: Emfox Zhou @ 2026-12-03 2:04 UTC (permalink / raw)
To: Even Xu, Xinpeng Sun, Jiri Kosina, Benjamin Tissoires
Cc: linux-input, linux-kernel
Register a HID device for the port and implement its callbacks, with the
descriptor and feature handshakes in ipts-hid.c.
Signed-off-by: Emfox Zhou <emfox@debian.org>
---
drivers/hid/intel-thc-hid/Makefile | 1 +
.../hid/intel-thc-hid/intel-ipts/ipts-hid.c | 267 ++++++++++++++++++
.../intel-thc-hid/intel-ipts/ipts-protocol.c | 7 +
drivers/hid/intel-thc-hid/intel-ipts/ipts.h | 2 +
4 files changed, 277 insertions(+)
create mode 100644 drivers/hid/intel-thc-hid/intel-ipts/ipts-hid.c
diff --git a/drivers/hid/intel-thc-hid/Makefile b/drivers/hid/intel-thc-hid/Makefile
index a3d6f8b18..34e8aaeb2 100644
--- a/drivers/hid/intel-thc-hid/Makefile
+++ b/drivers/hid/intel-thc-hid/Makefile
@@ -23,5 +23,6 @@ intel-quicki2c-objs += intel-quicki2c/quicki2c-protocol.o
obj-$(CONFIG_INTEL_IPTS) += intel-ipts.o
intel-ipts-objs += intel-ipts/pci-ipts.o
intel-ipts-objs += intel-ipts/ipts-protocol.o
+intel-ipts-objs += intel-ipts/ipts-hid.o
ccflags-y += -I $(src)/intel-thc
diff --git a/drivers/hid/intel-thc-hid/intel-ipts/ipts-hid.c b/drivers/hid/intel-thc-hid/intel-ipts/ipts-hid.c
new file mode 100644
index 000000000..491029468
--- /dev/null
+++ b/drivers/hid/intel-thc-hid/intel-ipts/ipts-hid.c
@@ -0,0 +1,267 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * HID device for THC ports in IPTS mode.
+ */
+
+#include <linux/hid.h>
+#include <linux/string.h>
+
+#include "ipts.h"
+
+static bool thc_ipts_idle(const struct thc_ipts *idev)
+{
+ return !READ_ONCE(idev->recover_pending) &&
+ !READ_ONCE(idev->recovering) &&
+ !READ_ONCE(idev->stopping);
+}
+
+static int thc_ipts_hid_parse(struct hid_device *hdev)
+{
+ struct thc_ipts *idev = hdev->driver_data;
+ unsigned long timeout = msecs_to_jiffies(THC_IPTS_REQ_TIMEOUT_MS);
+ u64 zero = 0;
+ unsigned int retries = 0;
+ int ret;
+ int try = 0;
+
+ /* A descriptor from a previous start is not this one's. */
+ WRITE_ONCE(idev->have_desc, false);
+
+ while (try < THC_IPTS_REQ_TRIES) {
+ /* An answer that landed after a wait expired is taken here. */
+ if (smp_load_acquire(&idev->have_desc))
+ break;
+
+ /* The controller ignores the eight byte argument. */
+ ret = thc_ipts_send(idev, THC_IPTS_TX_GET_DESC, &zero,
+ sizeof(zero), NULL);
+ /*
+ * Both dropped the frame: try again.
+ */
+ if (ret == -EAGAIN || ret == -ETIMEDOUT) {
+ if (++retries > THC_IPTS_REQ_TRIES ||
+ !wait_event_timeout(idev->wq,
+ thc_ipts_idle(idev), timeout))
+ break;
+ continue;
+ }
+ if (ret)
+ return ret;
+
+ try++;
+ wait_event_timeout(idev->wq, READ_ONCE(idev->have_desc),
+ timeout);
+ }
+
+ /* Pairs with the release in thc_ipts_descriptor(). */
+ if (smp_load_acquire(&idev->have_desc) && idev->desc_len > 0) {
+ /* The receive buffer holds more than a descriptor may be. */
+ if (idev->desc_len > HID_MAX_DESCRIPTOR_SIZE) {
+ dev_err(idev->dev,
+ "report descriptor of %zu bytes is too large\n",
+ idev->desc_len);
+ return -EINVAL;
+ }
+
+ return hid_parse_report(hdev, idev->desc, idev->desc_len);
+ }
+
+ dev_err(idev->dev, "controller did not return a report descriptor\n");
+
+ return -ETIMEDOUT;
+}
+
+static int thc_ipts_get_feature(struct thc_ipts *idev, unsigned char reportnum,
+ u8 *buf, size_t len)
+{
+ unsigned long timeout = msecs_to_jiffies(THC_IPTS_REQ_TIMEOUT_MS);
+ /* Given by the send below, which only a frame that went out gets. */
+ unsigned int seq = 0;
+ int ret;
+
+ /*
+ * One request at a time, and the wait happens without the lock: a request
+ * from the handler thread would wait for itself.
+ */
+ mutex_lock(&idev->feature_lock);
+
+ /*
+ * The answer carries only the ID, so a late one cannot be told from the
+ * next: after a timeout the channel stays shut.
+ */
+ if (idev->feature_off) {
+ mutex_unlock(&idev->feature_lock);
+ return -EIO;
+ }
+ if (idev->feature_pending) {
+ mutex_unlock(&idev->feature_lock);
+ return -EBUSY;
+ }
+
+ WRITE_ONCE(idev->feature_done, false);
+ idev->feature_id = reportnum;
+ idev->feature_pending = true;
+ mutex_unlock(&idev->feature_lock);
+
+ /*
+ * A refusal wrote nothing: the channel stays open.
+ */
+ ret = thc_ipts_send(idev, THC_IPTS_TX_GET_FEATURE, &reportnum, 1, &seq);
+ if (ret) {
+ mutex_lock(&idev->feature_lock);
+ idev->feature_pending = false;
+
+ /*
+ * The frame may be out there: its answer cannot be told apart.
+ */
+ if (ret == -ETIMEDOUT)
+ WRITE_ONCE(idev->feature_off, true);
+ mutex_unlock(&idev->feature_lock);
+
+ if (ret == -ETIMEDOUT)
+ dev_err(idev->dev, "feature report %u not acknowledged\n",
+ reportnum);
+
+ return ret;
+ }
+
+ wait_event_timeout(idev->wq, READ_ONCE(idev->feature_done) ||
+ READ_ONCE(idev->recover_seq) != seq ||
+ READ_ONCE(idev->recover_pending) ||
+ READ_ONCE(idev->recovering) ||
+ READ_ONCE(idev->stopping), timeout);
+
+ mutex_lock(&idev->feature_lock);
+ /* An answer that landed while the wait was expiring counts too. */
+ if (!idev->feature_done) {
+ /*
+ * A reset dropped whatever was queued: nothing to wait for.
+ */
+ if (READ_ONCE(idev->recover_seq) != seq ||
+ READ_ONCE(idev->recover_pending) ||
+ READ_ONCE(idev->recovering) ||
+ READ_ONCE(idev->stopping))
+ ret = -EIO;
+ else
+ ret = -ETIMEDOUT;
+
+ idev->feature_pending = false;
+ /*
+ * Only a timeout leaves a frame out there.
+ */
+ if (ret == -ETIMEDOUT)
+ WRITE_ONCE(idev->feature_off, true);
+ mutex_unlock(&idev->feature_lock);
+
+ if (ret == -ETIMEDOUT)
+ dev_err(idev->dev, "no feature report %u\n", reportnum);
+
+ return ret;
+ }
+
+ /* The payload is the report as user space writes it: ID first. */
+ len = min(len, idev->feature_len);
+ if (len)
+ memcpy(buf, idev->feature, len);
+ idev->feature_pending = false;
+ mutex_unlock(&idev->feature_lock);
+
+ return len ?: -EPROTO;
+}
+
+static int thc_ipts_hid_raw_request(struct hid_device *hdev,
+ unsigned char reportnum, __u8 *buf,
+ size_t len, unsigned char rtype,
+ int reqtype)
+{
+ struct thc_ipts *idev = hdev->driver_data;
+ u32 code;
+ int ret;
+
+ if (!buf || !len)
+ return -EINVAL;
+
+ if (reqtype == HID_REQ_GET_REPORT && rtype == HID_FEATURE_REPORT)
+ return thc_ipts_get_feature(idev, reportnum, buf, len);
+
+ if (reqtype != HID_REQ_SET_REPORT ||
+ (rtype != HID_OUTPUT_REPORT && rtype != HID_FEATURE_REPORT))
+ return -EOPNOTSUPP;
+
+ code = rtype == HID_OUTPUT_REPORT ? THC_IPTS_TX_OUTPUT :
+ THC_IPTS_TX_SET_FEATURE;
+
+ ret = thc_ipts_send(idev, code, buf, len, NULL);
+ if (ret)
+ return ret;
+
+ /* The raw request returns a byte count, which hidraw forwards. */
+ return (int)len;
+}
+
+static int thc_ipts_hid_start(struct hid_device *hdev)
+{
+ return 0;
+}
+
+static void thc_ipts_hid_stop(struct hid_device *hdev)
+{
+}
+
+static int thc_ipts_hid_open(struct hid_device *hdev)
+{
+ return 0;
+}
+
+static void thc_ipts_hid_close(struct hid_device *hdev)
+{
+}
+
+static const struct hid_ll_driver thc_ipts_ll_driver = {
+ .start = thc_ipts_hid_start,
+ .stop = thc_ipts_hid_stop,
+ .open = thc_ipts_hid_open,
+ .close = thc_ipts_hid_close,
+ .parse = thc_ipts_hid_parse,
+ .raw_request = thc_ipts_hid_raw_request,
+};
+
+void thc_ipts_hid_remove(struct thc_ipts *idev)
+{
+ if (!READ_ONCE(idev->hid))
+ return;
+
+ hid_destroy_device(idev->hid);
+ WRITE_ONCE(idev->hid, NULL);
+}
+
+int thc_ipts_hid_add(struct thc_ipts *idev)
+{
+ struct hid_device *hdev;
+ int ret;
+
+ hdev = hid_allocate_device();
+ if (IS_ERR(hdev))
+ return PTR_ERR(hdev);
+
+ hdev->ll_driver = &thc_ipts_ll_driver;
+ hdev->bus = BUS_PCI;
+ hdev->vendor = idev->vendor;
+ hdev->product = idev->product;
+ hdev->version = le32_to_cpu(idev->cfg.revision) & 0xffff;
+ hdev->dev.parent = idev->dev;
+ hdev->driver_data = idev;
+ snprintf(hdev->name, sizeof(hdev->name), "intel-ipts %04X:%04X",
+ idev->vendor, idev->product);
+ strscpy(hdev->phys, dev_name(idev->dev), sizeof(hdev->phys));
+
+ ret = hid_add_device(hdev);
+ if (ret) {
+ hid_destroy_device(hdev);
+ return ret;
+ }
+
+ WRITE_ONCE(idev->hid, hdev);
+
+ return 0;
+}
diff --git a/drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c b/drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c
index 9ef4b3cfa..4877ad87a 100644
--- a/drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c
+++ b/drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c
@@ -629,6 +629,11 @@ int thc_ipts_port_start(struct thc_ipts *idev)
thc_interrupt_enable(idev->thc, true);
+ ret = thc_ipts_hid_add(idev);
+ if (ret)
+ return ret;
+ WRITE_ONCE(idev->state, THC_IPTS_UP);
+
return 0;
}
@@ -672,4 +677,6 @@ void thc_ipts_port_stop(struct thc_ipts *idev)
/* Wait for a handler that may still look at the ring. */
synchronize_irq(idev->irq);
thc_interrupt_enable(idev->thc, false);
+
+ thc_ipts_hid_remove(idev);
}
diff --git a/drivers/hid/intel-thc-hid/intel-ipts/ipts.h b/drivers/hid/intel-thc-hid/intel-ipts/ipts.h
index f27ad7f03..f9a09343e 100644
--- a/drivers/hid/intel-thc-hid/intel-ipts/ipts.h
+++ b/drivers/hid/intel-thc-hid/intel-ipts/ipts.h
@@ -212,6 +212,8 @@ struct thc_ipts {
int thc_ipts_send(struct thc_ipts *idev, u32 code, const void *data,
size_t len, unsigned int *seq);
+int thc_ipts_hid_add(struct thc_ipts *idev);
+void thc_ipts_hid_remove(struct thc_ipts *idev);
irqreturn_t thc_ipts_hardirq(int irq, void *data);
irqreturn_t thc_ipts_thread(int irq, void *data);
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH v11 5/6] HID: intel-thc-hid: intel-ipts: handle system sleep
2026-12-03 2:04 [PATCH v11 0/6] HID: intel-thc-hid: intel-ipts: support THC IPTS ports Emfox Zhou
` (3 preceding siblings ...)
2026-12-03 2:04 ` [PATCH v11 4/6] HID: intel-thc-hid: intel-ipts: add the HID device Emfox Zhou
@ 2026-12-03 2:04 ` Emfox Zhou
2026-12-03 2:04 ` [PATCH v11 6/6] HID: intel-thc-hid: intel-ipts: document the IPTS mode Emfox Zhou
5 siblings, 0 replies; 10+ messages in thread
From: Emfox Zhou @ 2026-12-03 2:04 UTC (permalink / raw)
To: Even Xu, Xinpeng Sun, Jiri Kosina, Benjamin Tissoires
Cc: linux-input, linux-kernel
Take the port down before system sleep and bring it back up after
resume.
Signed-off-by: Emfox Zhou <emfox@debian.org>
---
.../hid/intel-thc-hid/intel-ipts/pci-ipts.c | 52 +++++++++++++++++++
1 file changed, 52 insertions(+)
diff --git a/drivers/hid/intel-thc-hid/intel-ipts/pci-ipts.c b/drivers/hid/intel-thc-hid/intel-ipts/pci-ipts.c
index 746ef27e2..5da8cbf78 100644
--- a/drivers/hid/intel-thc-hid/intel-ipts/pci-ipts.c
+++ b/drivers/hid/intel-thc-hid/intel-ipts/pci-ipts.c
@@ -151,6 +151,57 @@ static void thc_ipts_remove(struct pci_dev *pci)
pci_clear_master(pci);
}
+static int thc_ipts_suspend(struct device *dev)
+{
+ struct thc_ipts *idev = dev_get_drvdata(dev);
+
+ thc_ipts_port_stop(idev);
+
+ return 0;
+}
+
+static int thc_ipts_resume(struct device *dev)
+{
+ struct thc_ipts *idev = dev_get_drvdata(dev);
+ int ret;
+
+ /*
+ * A restore brings the global enable back while the port is down: mask
+ * before starting.
+ */
+ thc_interrupt_enable(idev->thc, false);
+
+ ret = thc_ipts_port_start(idev);
+ if (ret)
+ thc_ipts_port_stop(idev);
+
+ return ret;
+}
+
+/*
+ * Mask as soon as configuration space is back: the line may already be
+ * asserted.
+ */
+static int thc_ipts_resume_noirq(struct device *dev)
+{
+ struct thc_ipts *idev = dev_get_drvdata(dev);
+
+ thc_interrupt_enable(idev->thc, false);
+
+ return 0;
+}
+
+static const struct dev_pm_ops thc_ipts_pm_ops = {
+ .suspend = thc_ipts_suspend,
+ .resume = thc_ipts_resume,
+ .freeze = thc_ipts_suspend,
+ .thaw = thc_ipts_resume,
+ .poweroff = thc_ipts_suspend,
+ .restore = thc_ipts_resume,
+ .resume_noirq = thc_ipts_resume_noirq,
+ .restore_noirq = thc_ipts_resume_noirq,
+};
+
static void thc_ipts_shutdown(struct pci_dev *pci)
{
struct thc_ipts *idev = pci_get_drvdata(pci);
@@ -165,6 +216,7 @@ static struct pci_driver thc_ipts_driver = {
.probe = thc_ipts_probe,
.remove = thc_ipts_remove,
.shutdown = thc_ipts_shutdown,
+ .driver.pm = pm_sleep_ptr(&thc_ipts_pm_ops),
/* Bring-up waits for the controller, up to a few seconds. */
.driver.probe_type = PROBE_PREFER_ASYNCHRONOUS,
};
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH v11 6/6] HID: intel-thc-hid: intel-ipts: document the IPTS mode
2026-12-03 2:04 [PATCH v11 0/6] HID: intel-thc-hid: intel-ipts: support THC IPTS ports Emfox Zhou
` (4 preceding siblings ...)
2026-12-03 2:04 ` [PATCH v11 5/6] HID: intel-thc-hid: intel-ipts: handle system sleep Emfox Zhou
@ 2026-12-03 2:04 ` Emfox Zhou
5 siblings, 0 replies; 10+ messages in thread
From: Emfox Zhou @ 2026-12-03 2:04 UTC (permalink / raw)
To: Even Xu, Xinpeng Sun, Jiri Kosina, Benjamin Tissoires
Cc: linux-input, linux-kernel
Document the IPTS mode: configuration block, frame layout, data window,
enabling order and driver structure.
Signed-off-by: Emfox Zhou <emfox@debian.org>
---
Documentation/hid/intel-thc-hid.rst | 136 +++++++++++++++++++++++++++-
1 file changed, 135 insertions(+), 1 deletion(-)
diff --git a/Documentation/hid/intel-thc-hid.rst b/Documentation/hid/intel-thc-hid.rst
index 8b378c57b..5ae93a5dc 100644
--- a/Documentation/hid/intel-thc-hid.rst
+++ b/Documentation/hid/intel-thc-hid.rst
@@ -590,7 +590,141 @@ To debug THC, event tracing mechanism is used. To enable debug logs::
echo 1 > /sys/kernel/debug/tracing/events/intel_thc/enable
cat /sys/kernel/debug/tracing/trace
-7. Reference
+7. IPTS Mode
+============
+
+THC also supports IPTS mode, the private Intel Precise Touch and Stylus
+protocol. The port drives the touch controller over plain SPI and moves the
+data through the same DMA engines, but the framing is private and the
+parameters come from the controller itself instead of ACPI. The
+``intel-ipts`` module handles ports in that mode, which is what Tiger Lake,
+Lakefield, Alder Lake and Raptor Lake hardware use.
+
+This section describes how the driver uses those ports. It is not a
+specification: everything below comes from observing the controller and is
+limited to what this driver relies on.
+
+7.1 Configuration Block
+-----------------------
+
+At SPI address 0 the controller exposes a 64 byte block. The driver reads it
+over the PIO path after releasing the controller from reset, and retries
+until the tag ``$TIC`` (``0x43495424``) appears, because the block is only
+partially filled while the controller is still starting.
+
+Two steps come before that read: the DMA engines are unconfigured first, so
+engines the firmware left armed do not keep writing through the PRD base it
+had mapped, and the interrupt trigger is switched to edge, without which the
+first read is unreliable.
+
+Fields the driver uses:
+
+====== ==========================================================
+Offset Meaning
+====== ==========================================================
+0x08 buffer sizes, ``(size / 64) - 1``; Rx bits 13:0, Tx 23:14
+0x0c touch engine control: enable BIT(0), HID reports BIT(2),
+ power state GENMASK(5, 3)
+0x14 controller tag, ``$TIC``
+0x18 SPI timing: clock divisor GENMASK(3, 1), divide-by-8 and
+ dual/quad bits
+0x1c vendor and product ID reported to HID
+0x20 product revision
+====== ==========================================================
+
+The driver enables the touch engine with reports and full power, and clears
+it again when the port is shut down.
+
+The offsets above, the frame codes and the header sizes below are compiled
+into ``ipts.h``; a ``static_assert()`` on the offsets the driver reads makes
+a mismatch a build failure rather than a quiet misread.
+
+7.2 Frames
+----------
+
+Both directions carry a code and a length in front of the payload. Frames
+from the controller use a 64 byte header, frames to it an 8 byte header, and
+only the latter has the payload padded to a four byte boundary.
+
+==== ========================== =======================
+Code Received from controller Sent to controller
+==== ========================== =======================
+3 input report set feature
+4 feature report get feature
+5 report descriptor output report
+7 reset request get report descriptor
+==== ========================== =======================
+
+A report descriptor comes back with eight bytes of preamble that the driver
+skips.
+
+7.3 Data Window
+---------------
+
+Bulk transfers go through address ``0x1000`` of the controller, not through
+address 0. The DMA engines have to be pointed there explicitly, otherwise
+they write into the configuration block and the controller drops the frames
+without reporting an error. The input header address is zero: that is where
+the controller writes the interrupt cause word, at the start of the
+configuration block.
+
+7.4 Enabling and Flow Control
+-----------------------------
+
+The port has to be released from quiesce before the driver enables the
+touch engine: quiescing holds back the device interrupt and the data path,
+not the configuration access, which is why the block above is read while the
+port is still quiesced. The interrupt configuration programs the interrupt
+type field in the device to the invalid value, and starting the receive
+engine then replaces that field with the type it expects, so the two have to
+happen in that order.
+
+The controller starts streaming once user space asks for it, through a set
+feature request on report 5. The driver does not do that on its own; the
+daemon that opens hidraw sends it.
+
+7.5 What Is Left to User Space
+------------------------------
+
+The reports carry heatmap data, and turning that into contacts is a
+substantial image-processing job that already exists in user space. The
+driver therefore stops at the HID layer: the report descriptor comes from
+the controller and is not written by the driver, the report stream is
+available through hidraw, and ``iptsd`` decodes it into input events.
+
+System sleep tears the port down and brings it back, so the hidraw node is
+destroyed and re-created: a daemon has to reopen it after resume.
+
+7.6 Driver Structure
+--------------------
+
+The driver is split the way the HIDSPI and HIDI2C backends are:
+``ipts-protocol.c`` owns the port, the configuration block, the DMA
+engines, the interrupt and the frames, including the frames that carry
+the report descriptor and the feature reports; ``ipts-hid.c`` owns the
+HID device and the callbacks that ask for them; ``pci-ipts.c`` is the
+PCI glue.
+
+Bring-up is one sequence: quiesce the data path, select the port, program
+the bulk data window, switch the interrupt trigger to edge, start with the
+slow SPI timing, pulse the controller reset, read the configuration block,
+apply the timing it reports, configure the DMA engines and then enable the
+interrupt and register the HID device. Quiescing gates the whole data
+path, not only the interrupt, so the port stays quiesced until the block
+has been read and the timing applied.
+
+A transfer error is recovered from a work item and not from the threaded
+handler: resetting the receive engine takes the bus lock, which the write
+path holds while it waits for the completion the handler delivers. The
+work item masks the port before it touches the engine.
+
+The report descriptor and each feature report are asked for over the
+transport and arrive as frames of their own. One request is kept on the
+wire at a time; a request that times out shuts the feature channel until
+the port is restarted, because nothing on the wire tells a late answer
+from the next one's.
+
+8. Reference
============
- HIDSPI: https://download.microsoft.com/download/c/a/0/ca07aef3-3e10-4022-b1e9-c98cea99465d/HidSpiProtocolSpec.pdf
- HIDI2C: https://download.microsoft.com/download/7/d/d/7dd44bb7-2a7a-4505-ac1c-7227d3d96d5b/hid-over-i2c-protocol-spec-v1-0.docx
^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH v11 1/6] HID: intel-thc-hid: intel-thc: expose the port operations IPTS needs
2026-12-03 2:04 ` [PATCH v11 1/6] HID: intel-thc-hid: intel-thc: expose the port operations IPTS needs Emfox Zhou
@ 2026-12-03 2:11 ` sashiko-bot
0 siblings, 0 replies; 10+ messages in thread
From: sashiko-bot @ 2026-12-03 2:11 UTC (permalink / raw)
To: Emfox Zhou; +Cc: linux-input, dmitry.torokhov
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] HID: intel-thc: thc_port_reset() misnamed for Touch IC reset
operation
--
commit a15fdd771faccc457564c8d773289aea962cca03
Author: Emfox Zhou <emfox@debian.org>
HID: intel-thc-hid: intel-thc: expose the port operations IPTS needs
This commit adds and exports thc_port_supported() and thc_port_reset() so
clients can check the port capability bit and drive the touch controller
reset pin via THC_M_PRT_CONTROL.
> diff --git a/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.c b/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.c
> index 03d3b60b35cf1..c7d4959153e94 100644
> --- a/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.c
> +++ b/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.c
> @@ -1111,6 +1111,64 @@ int thc_port_select(struct thc_device *dev, enum thc_port_type port_type)
[ ... ]
> +/**
> + * thc_port_reset - Drive the reset line of the touch IC
> + *
> + * @dev: the pointer of THC private device context
> + * @hold: keep the touch IC in reset instead of releasing it
> + *
> + * The reset bit of the port control register drives the reset pin of the
> + * touch IC: zero asserts the reset, one releases it. Firmware may leave
> + * the pin released, so a start asserts it and then lets it go rather than
> + * assume it was held.
> + *
> + * Return: 0 on success, other error codes on failed
> + */
> +int thc_port_reset(struct thc_device *dev, bool hold)
> +{
> + int ret;
> +
> + /* DEVRST: 0 asserts the reset, 1 releases it. */
> + ret = regmap_write_bits(dev->thc_regmap, THC_M_PRT_CONTROL_OFFSET,
> + THC_M_PRT_CONTROL_DEVRST, 0);
> + if (ret || hold)
> + return ret;
> +
> + usleep_range(1000, 2000);
> +
> + return regmap_write_bits(dev->thc_regmap, THC_M_PRT_CONTROL_OFFSET,
> + THC_M_PRT_CONTROL_DEVRST,
> + THC_M_PRT_CONTROL_DEVRST);
> +}
> +EXPORT_SYMBOL_NS_GPL(thc_port_reset, "INTEL_THC");
[Severity: Low]
As reported by Xu, Even on v10 [1], should thc_port_reset() be named
thc_tic_reset() instead?
THC_M_PRT_CONTROL_DEVRST drives the external Touch IC (TIC) reset pin
rather than resetting the THC port itself (which has its own soft-reset
bit THC_M_PRT_CONTROL_TSFTRST), and existing Touch IC helpers in this file
use the thc_tic_ prefix (thc_tic_pio_read(), thc_tic_pio_write(),
thc_tic_pio_write_and_read()).
[1] https://lore.kernel.org/r/SJ0PR11MB7704E6861DD87DC5483395B5F44C2@SJ0PR11MB7704.namprd11.prod.outlook.com
--
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796215044.git.emfox@debian.org?part=1
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH v11 2/6] HID: intel-thc-hid: intel-ipts: bring up the port
2026-12-03 2:04 ` [PATCH v11 2/6] HID: intel-thc-hid: intel-ipts: bring up the port Emfox Zhou
@ 2026-12-03 2:15 ` sashiko-bot
0 siblings, 0 replies; 10+ messages in thread
From: sashiko-bot @ 2026-12-03 2:15 UTC (permalink / raw)
To: Emfox Zhou; +Cc: linux-input, dmitry.torokhov
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] HID: intel-thc-hid: unchecked state in thc_ipts_port_stop()
on unwind
--
commit 663780af800a067405da9392130d2229f813c4d6
Author: Emfox Zhou <emfox@debian.org>
HID: intel-thc-hid: intel-ipts: bring up the port
This commit adds the Intel Touch Host Controller IPTS mode PCI driver and
protocol support to select the SPI port, reset the controller, read its
configuration block over PIO, apply SPI timing, and enable the touch engine.
> diff --git a/drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c b/drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c
> new file mode 100644
> index 0000000000000..5d3d8d4005b8f
> --- /dev/null
> +++ b/drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c
[ ... ]
> +int thc_ipts_port_start(struct thc_ipts *idev)
> +{
> + u32 hz, mode, cfg, div;
> + bool supported;
> + int ret;
> +
> + ret = thc_interrupt_quiesce(idev->thc, true);
> + if (ret)
> + return ret;
> +
> + ret = thc_port_select(idev->thc, THC_PORT_TYPE_SPI);
> + if (ret)
> + return ret;
> +
> + ret = thc_port_supported(idev->thc, &supported);
> + if (ret)
> + return ret;
> + if (!supported)
> + return -ENODEV;
[ ... ]
> + ret = thc_port_reset(idev->thc, false);
> + if (ret)
> + return ret;
> +
> + /* Out of reset from here: a teardown has to put it back. */
> + WRITE_ONCE(idev->state, THC_IPTS_RESET);
[ ... ]
> + ret = thc_interrupt_quiesce(idev->thc, false);
> + if (ret)
> + return ret;
> +
> + ret = thc_ipts_touch_set(idev, true);
> + if (ret)
> + return ret;
> + WRITE_ONCE(idev->state, THC_IPTS_CONFIG);
> +
> + return 0;
> +}
> +
> +/**
> + * thc_ipts_port_stop - tear one port down
> + * @idev: port to stop
> + */
> +void thc_ipts_port_stop(struct thc_ipts *idev)
> +{
> + if (READ_ONCE(idev->state) >= THC_IPTS_CONFIG)
> + thc_ipts_touch_set(idev, false);
> +
> + /* Keep the touch IC in reset until the next start. */
> + thc_port_reset(idev->thc, true);
> + thc_interrupt_quiesce(idev->thc, true);
> + WRITE_ONCE(idev->state, THC_IPTS_DOWN);
> +}
[Severity: Medium]
Should thc_ipts_port_stop() check idev->state before calling
thc_port_reset() and thc_interrupt_quiesce()?
thc_ipts_probe() calls thc_ipts_port_stop() whenever thc_ipts_port_start()
fails. If thc_ipts_port_start() returns early before releasing reset
(such as -ENODEV when !supported), idev->state is still THC_IPTS_DOWN, yet
thc_ipts_port_stop() still asserts DEVRST on the port. Similarly, because
thc_ipts_port_start() quiesces interrupts on entry and only unquiesces
them right before THC_IPTS_CONFIG, any failure in between causes
thc_ipts_port_stop() to call thc_interrupt_quiesce(idev->thc, true) on an
already-quiesced controller, triggering the "THC interrupt already
quiesce" dev_warn() in thc_interrupt_quiesce().
--
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796215044.git.emfox@debian.org?part=2
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH v11 3/6] HID: intel-thc-hid: intel-ipts: receive reports
2026-12-03 2:04 ` [PATCH v11 3/6] HID: intel-thc-hid: intel-ipts: receive reports Emfox Zhou
@ 2026-12-03 2:26 ` sashiko-bot
0 siblings, 0 replies; 10+ messages in thread
From: sashiko-bot @ 2026-12-03 2:26 UTC (permalink / raw)
To: Emfox Zhou; +Cc: linux-input, dmitry.torokhov
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] HID: intel-thc-hid: intel-ipts: IRQ storm and livelock in error
recovery
- [High] HID: intel-thc-hid: intel-ipts: race with IRQ and recovery in
thc_ipts_port_stop()
- [Medium] HID: intel-thc-hid: intel-ipts: invalid wait context in
thc_ipts_hardirq()
--
commit 660d26f61a97da206cffff0edd97b71c4183c0c7
Author: Emfox Zhou <emfox@debian.org>
HID: intel-thc-hid: intel-ipts: receive reports
This commit adds DMA configuration, threaded interrupt handling, frame
transmission and reception, and error recovery work to the Intel IPTS
transport driver.
> diff --git a/drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c b/drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c
> index 5d3d8d4005b8f..9ef4b3cfa733c 100644
> --- a/drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c
> +++ b/drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c
[ ... ]
> +irqreturn_t thc_ipts_hardirq(int irq, void *data)
> +{
> + struct thc_ipts *idev = data;
> +
> + /*
> + * Nothing of ours is pending: the cause belongs to a sharer. A reset
> + * in flight owns the status registers and acknowledges nothing.
> + */
> + if (READ_ONCE(idev->state) < THC_IPTS_DMA ||
> + READ_ONCE(idev->recovering))
> + return IRQ_NONE;
> +
> + /* Level triggered and shared: mask first, or it stays asserted. */\
> + thc_interrupt_enable(idev->thc, false);
> +
> + /*
> + * A teardown still needs this thread: it delivers the completion a
> + * pending write holds the bus lock for.
> + */
> + return IRQ_WAKE_THREAD;
> +}
[Severity: Medium]
Can calling thc_interrupt_enable() from thc_ipts_hardirq() trigger an
invalid wait context warning under CONFIG_PROVE_RAW_LOCK_NESTING or sleep
in hardirq context on PREEMPT_RT?
thc_ipts_probe() requests a legacy PCI_IRQ_INTX vector with IRQF_SHARED |
IRQF_COND_ONESHOT. When the line is shared with another handler that uses
IRQF_ONESHOT, __setup_irq() propagates IRQF_ONESHOT to this action, so
__handle_irq_event_percpu() does not mark the hardirq as threaded for
lockdep and leaves the wait context at LD_WAIT_SPIN.
thc_interrupt_enable() then calls regmap_write_bits() on dev->thc_regmap,
which is configured without .use_raw_spinlock = true and therefore takes a
non-raw spinlock_t:
drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.c:
static const struct regmap_config thc_regmap_cfg = {
...
.cache_type = REGCACHE_NONE,
.fast_io = true,
};
[ ... ]
> +void thc_ipts_recover(struct work_struct *work)
> +{
> + struct thc_ipts *idev = container_of(work, struct thc_ipts,
> + recover_work);
> +
> + /*
> + * A write on the wire needs the interrupt this reset is about to mask:
> + * let it finish first.
> + */
> + mutex_lock(&idev->tx_lock);
> + WRITE_ONCE(idev->recovering, true);
> + mutex_unlock(&idev->tx_lock);
> +
> + /*
> + * Mask, then wait out a run already inside: the status registers belong
> + * to the reset.
> + */
> + thc_interrupt_enable(idev->thc, false);
> + synchronize_irq(idev->irq);
> + /*
> + * A run may have unmasked on its way out: mask again.
> + */
> + thc_interrupt_enable(idev->thc, false);
> +
> + if (thc_rxdma_reset(idev->thc)) {
> + /*
> + * Nothing left to reset: stay masked and fail with -ENODEV.
> + */
> + dev_err(idev->dev, "cannot reset the receive engine\n");
> + WRITE_ONCE(idev->state, THC_IPTS_CONFIG);
> + WRITE_ONCE(idev->recover_seq, READ_ONCE(idev->recover_seq) + 1);
> + WRITE_ONCE(idev->recover_pending, false);
> + WRITE_ONCE(idev->recovering, false);
> + wake_up(&idev->wq);
> + return;
> + }
> +
> + /*
> + * Clear the queued flag before the running one.
> + */
> + WRITE_ONCE(idev->recover_seq, READ_ONCE(idev->recover_seq) + 1);
> + WRITE_ONCE(idev->recover_pending, false);
> + WRITE_ONCE(idev->recovering, false);
> +
> + /* A request waiting out the reset is looking at that flag. */
> + wake_up(&idev->wq);
> +
> + if (!READ_ONCE(idev->stopping))
> + thc_interrupt_enable(idev->thc, true);
> +}
> +
> +irqreturn_t thc_ipts_thread(int irq, void *data)
> +{
> + struct thc_ipts *idev = data;
> + irqreturn_t ret = IRQ_NONE;
> + bool need_recover = false;
> + bool reported = false;
> + int pass;
> +
> + /*
> + * Remember this thread: a request from it would wait for a completion
> + * only it delivers.
> + */
> + WRITE_ONCE(idev->irq_thread, current);
> +
> + /*
> + * A sharer can queue a second run: mask again.
> + */
> + thc_interrupt_enable(idev->thc, false);
> +
> + /*
> + * The port stays masked while reading: causes only leave status bits.
> + */
> + for (pass = 0; pass < THC_IPTS_IRQ_PASSES; pass++) {
> + int mask;
> +
> + /*
> + * A reset owns the status registers: acknowledge nothing here.
> + */
> + if (READ_ONCE(idev->recovering))
> + break;
> +
> + mask = thc_interrupt_handler(idev->thc);
> +
> + if (mask == BIT(THC_UNKNOWN_INT))
> + break;
> +
> + ret = IRQ_HANDLED;
> + if (mask & (BIT(THC_FATAL_ERR_INT) | BIT(THC_TXN_ERR_INT))) {
> + /*
> + * An overrun repeats until the reset: report the first one.
> + */
> + if (!reported && !READ_ONCE(idev->recover_pending)) {
> + dev_err(idev->dev,
> + "transfer error, resetting the engine\n");
> + reported = true;
> + }
> + /* The teardown has its own reset to wait for. */\
> + if (!READ_ONCE(idev->stopping))
> + need_recover = true;
> + /*
> + * The core stops at the first cause; a completion behind it would time
> + * out.
> + */
> + continue;
> + }
> +
> + /*
> + * A reset is queued or running: read the status, not the ring.
> + */
> + if (need_recover || READ_ONCE(idev->recover_pending) ||
> + READ_ONCE(idev->recovering) ||
> + READ_ONCE(idev->stopping))
> + continue;
> +
> + if (mask & BIT(THC_RXDMA2_INT) && thc_ipts_read_frames(idev))
> + need_recover = true;
> + }
> +
> + /*
> + * Only queue the reset: a frame read now would corrupt the ring.
> + */
> + if (need_recover)
> + thc_ipts_request_recover(idev);
> +
> + /*
> + * Keep the line open unless a reset is running: a write on the wire needs
> + * this handler to deliver its completion.
> + */
> + if (!READ_ONCE(idev->recovering))
> + thc_interrupt_enable(idev->thc, true);
> +
> + return ret;
> +}
[Severity: High]
Can error recovery trigger a level-triggered hardirq storm or starve
recover_work behind the threaded IRQ handler?
kworker <hardirq> / irq/thc_ipts
thc_ipts_thread()
need_recover = true
thc_ipts_request_recover()
recover_pending = true
schedule_work(&recover_work)
if (!idev->recovering)
thc_interrupt_enable(..., true)
thc_ipts_recover()
WRITE_ONCE(idev->recovering, true)
<hardirq> (overrun still asserted)
thc_ipts_hardirq()
if (idev->recovering)
return IRQ_NONE;
<hardirq> (line stays asserted)
thc_ipts_hardirq()
return IRQ_NONE; <-- IRQ storm
thc_interrupt_enable(..., false)
First, thc_ipts_thread() re-enables hardware interrupts on exit whenever
idev->recovering is still false, even after queuing recover_work with
need_recover / recover_pending set. Because an RxDMA overrun or stall
persists in hardware until thc_rxdma_reset() runs, unmasking the port
immediately re-triggers the interrupt and wakes the SCHED_FIFO IRQ thread
again (or spins while thc_ipts_recover() waits on idev->tx_lock),
preempting and starving the SCHED_NORMAL system_percpu_wq worker on the
same CPU. Also, continuing across all 10 passes of THC_IPTS_IRQ_PASSES on
a repeating overrun does not reach the TxDMA completion check in
thc_interrupt_handler(), because thc_interrupt_handler() returns early on
THC_TXN_ERR_INT and logs an unratelimited dev_err() on each pass.
Second, thc_ipts_recover() sets idev->recovering = true while hardware
interrupts are still enabled in THC_M_PRT_INT_EN_OFFSET (or while a
concurrent thc_ipts_thread() is about to call thc_interrupt_enable(thc,
true)). If the level-triggered INTx line asserts before
thc_interrupt_enable(idev->thc, false) runs, thc_ipts_hardirq() sees
idev->recovering == true and returns IRQ_NONE without masking the port,
re-entering thc_ipts_hardirq() until the spurious IRQ detector disables
the shared interrupt line.
[ ... ]
> @@ -202,11 +638,38 @@ int thc_ipts_port_start(struct thc_ipts *idev)
> */
> void thc_ipts_port_stop(struct thc_ipts *idev)
> {
> + /*
> + * Refuse requests, then wait for one inside thc_dma_write(): its
> + * completion comes through this handler.
> + */
> + WRITE_ONCE(idev->stopping, true);
> + wake_up(&idev->wq);
> + mutex_lock(&idev->tx_lock);
> + mutex_unlock(&idev->tx_lock);
> +
> + thc_interrupt_enable(idev->thc, false);
> + synchronize_irq(idev->irq);
> + cancel_work_sync(&idev->recover_work);
> +
> + /* A run that was already in flight may have unmasked on its way out. */\
> + thc_interrupt_enable(idev->thc, false);
> +
> + /* Stop the controller and keep it in reset until the next start. */\
> if (READ_ONCE(idev->state) >= THC_IPTS_CONFIG)
> thc_ipts_touch_set(idev, false);
>
> - /* Keep the touch IC in reset until the next start. */
> thc_port_reset(idev->thc, true);
> - thc_interrupt_quiesce(idev->thc, true);
> +
> + /*
> + * The tables are not devm-managed; release them even if the engine did
> + * not stop.
> + */
> + if (thc_interrupt_quiesce(idev->thc, true))
> + dev_warn(idev->dev, "engine did not stop, releasing anyway\n");
> + thc_ipts_dma_stop(idev);
> WRITE_ONCE(idev->state, THC_IPTS_DOWN);
> +
> + /* Wait for a handler that may still look at the ring. */
> + synchronize_irq(idev->irq);
> + thc_interrupt_enable(idev->thc, false);
> }
[Severity: High]
Can thc_ipts_thread() and thc_ipts_recover() race with teardown in
thc_ipts_port_stop() after idev->tx_lock has been drained?
CPU0 CPU1 (shared INTx / IRQ thread)
thc_ipts_port_stop()
WRITE_ONCE(idev->stopping, true)
mutex_lock(&idev->tx_lock)
mutex_unlock(&idev->tx_lock)
thc_interrupt_enable(..., false)
synchronize_irq(idev->irq)
cancel_work_sync(&recover_work)
thc_interrupt_enable(..., false)
<hardirq> (sharer on INTx line)
thc_ipts_hardirq()
/* state == THC_IPTS_DMA */
return IRQ_WAKE_THREAD;
thc_ipts_touch_set(..., false)
thc_port_reset(..., true)
thc_ipts_thread()
thc_interrupt_handler(idev->thc)
if (!idev->recovering)
thc_interrupt_enable(..., true)
thc_ipts_dma_stop(idev) <-- concurrent MMIO & IRQ re-enabled
Once mutex_unlock(&idev->tx_lock) completes, no write is in flight, yet
idev->state remains THC_IPTS_DMA until thc_ipts_dma_stop() and
idev->recovering is false. Because the INTx line is requested with
IRQF_SHARED, any interrupt from a device sharing the line causes
thc_ipts_hardirq() to return IRQ_WAKE_THREAD. thc_ipts_thread() also
unmasks hardware interrupts on exit when idev->recovering is false (even
when idev->stopping is true), so a run finishing during the first
synchronize_irq() can re-enable interrupts before the second
thc_interrupt_enable(idev->thc, false), which has no synchronize_irq()
after it.
In addition, if recover_work is already running when cancel_work_sync() is
called, thc_ipts_recover() does not check idev->stopping before calling
thc_rxdma_reset(idev->thc), which resets and restarts RxDMA2 and
unquiesces device interrupts in the middle of teardown.
--
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796215044.git.emfox@debian.org?part=3
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2026-12-03 2:26 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 2:04 [PATCH v11 0/6] HID: intel-thc-hid: intel-ipts: support THC IPTS ports Emfox Zhou
2026-12-03 2:04 ` [PATCH v11 1/6] HID: intel-thc-hid: intel-thc: expose the port operations IPTS needs Emfox Zhou
2026-12-03 2:11 ` sashiko-bot
2026-12-03 2:04 ` [PATCH v11 2/6] HID: intel-thc-hid: intel-ipts: bring up the port Emfox Zhou
2026-12-03 2:15 ` sashiko-bot
2026-12-03 2:04 ` [PATCH v11 3/6] HID: intel-thc-hid: intel-ipts: receive reports Emfox Zhou
2026-12-03 2:26 ` sashiko-bot
2026-12-03 2:04 ` [PATCH v11 4/6] HID: intel-thc-hid: intel-ipts: add the HID device Emfox Zhou
2026-12-03 2:04 ` [PATCH v11 5/6] HID: intel-thc-hid: intel-ipts: handle system sleep Emfox Zhou
2026-12-03 2:04 ` [PATCH v11 6/6] HID: intel-thc-hid: intel-ipts: document the IPTS mode Emfox Zhou
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.