Skip to contentSkip to conversation

HID: intel-thc-hid: intel-ipts: support THC IPTS ports

PATCH New Emfox Zhou 2 participants 10 messages Dec 3

Subsystems (3)
lore
Share & tools
lore

Emfox Zhou

author
#0 lore raw

v11 cover HID: intel-thc-hid: intel-ipts: support THC IPTS ports

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/[email protected]/

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/[email protected]/

[PATCH v8] HID: intel-thc-hid: intel-thc: restore PRD table capacity
https://lore.kernel.org/linux-input/[email protected]/

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

author
#1 lore raw

PATCH v11 1/6 HID: intel-thc-hid: intel-thc: expose the port operations IPTS needs

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.

---
 .../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(+)
drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.c +58 -0
index 03d3b60b3..c7d495915 100644
@@ -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
drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.h +2 -0
index be8a9605d..c34611e78 100644
@@ -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,
author
#3 lore raw

PATCH v11 2/6 HID: intel-thc-hid: intel-ipts: bring up the port

In reply to Emfox Zhou

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.

---
 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
drivers/hid/intel-thc-hid/Kconfig +13 -0
index 9d74e53b8..40e3ce198 100644
@@ -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
drivers/hid/intel-thc-hid/Makefile +4 -0
index f1182253b..a3d6f8b18 100644
@@ -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
drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c new file +212 -0

212 lines not on this page · raw message

drivers/hid/intel-thc-hid/intel-ipts/ipts.h new file +140 -0

140 lines not on this page · raw message

drivers/hid/intel-thc-hid/intel-ipts/pci-ipts.c new file +124 -0

124 lines not on this page · raw message

author
#5 lore raw

PATCH v11 3/6 HID: intel-thc-hid: intel-ipts: receive reports

In reply to Emfox Zhou

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.

---
 .../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(-)
drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c +465 -2
index 5d3d8d400..9ef4b3cfa 100644
@@ -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);
 }
drivers/hid/intel-thc-hid/intel-ipts/ipts.h +83 -0

127 lines not on this page · raw message

drivers/hid/intel-thc-hid/intel-ipts/pci-ipts.c +52 -0

80 lines not on this page · raw message

author
#7 lore raw

PATCH v11 4/6 HID: intel-thc-hid: intel-ipts: add the HID device

In reply to Emfox Zhou

Register a HID device for the port and implement its callbacks, with the descriptor and feature handshakes in ipts-hid.c.

---
 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
drivers/hid/intel-thc-hid/Makefile +1 -0
index a3d6f8b18..34e8aaeb2 100644
@@ -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
drivers/hid/intel-thc-hid/intel-ipts/ipts-hid.c new file +267 -0

267 lines not on this page · raw message

drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c +7 -0
index 9ef4b3cfa..4877ad87a 100644
@@ -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);
 }
drivers/hid/intel-thc-hid/intel-ipts/ipts.h +2 -0
index f27ad7f03..f9a09343e 100644
@@ -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);
author
#8 lore raw

PATCH v11 5/6 HID: intel-thc-hid: intel-ipts: handle system sleep

In reply to Emfox Zhou

Take the port down before system sleep and bring it back up after resume.

---
 .../hid/intel-thc-hid/intel-ipts/pci-ipts.c   | 52 +++++++++++++++++++
 1 file changed, 52 insertions(+)
drivers/hid/intel-thc-hid/intel-ipts/pci-ipts.c +52 -0
index 746ef27e2..5da8cbf78 100644
@@ -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,
 };
author
#9 lore raw

PATCH v11 6/6 HID: intel-thc-hid: intel-ipts: document the IPTS mode

In reply to Emfox Zhou

Document the IPTS mode: configuration block, frame layout, data window, enabling order and driver structure.

---
 Documentation/hid/intel-thc-hid.rst | 136 +++++++++++++++++++++++++++-
 1 file changed, 135 insertions(+), 1 deletion(-)
Documentation/hid/intel-thc-hid.rst +135 -1
index 8b378c57b..5ae93a5dc 100644
@@ -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
#2 lore raw

Re: [PATCH v11 1/6] HID: intel-thc-hid: intel-thc: expose the port operations IPTS needs

In reply to Emfox Zhou
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 --

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 <[email protected]>

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.

Quoted 5 lines
drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.c +0 -0
index 03d3b60b35cf1..c7d4959153e94 100644
@@ -1111,6 +1111,64 @@ int thc_port_select(struct thc_device *dev, enum thc_port_type port_type)

[ ... ]

Quoted 20 lines
drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.c +20 -0
+/**
+ * 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);

drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.c int thc_port_select(struct thc_device *dev, enum thc_port_type port_type) · line 1170

Quoted 10 lines
drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.c +10 -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

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#4 lore raw

Re: [PATCH v11 2/6] HID: intel-thc-hid: intel-ipts: bring up the port

In reply to Emfox Zhou
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 --

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 <[email protected]>

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.

Quoted 5 lines
drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c new file +0 -0
new file mode 100644
index 0000000000000..5d3d8d4005b8f

[ ... ]

Quoted 19 lines
drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c new file +19 -0
+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;

[ ... ]

Quoted 6 lines
drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c new file +6 -0
+	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);

[ ... ]

Quoted 16 lines
drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c new file +16 -0
+	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
+ */

drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c line 212

Quoted 10 lines
drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c new file +10 -0
+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().

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#6 lore raw

Re: [PATCH v11 3/6] HID: intel-thc-hid: intel-ipts: receive reports

In reply to Emfox Zhou
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…

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 <[email protected]>

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.

Quoted 4 lines
drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c +0 -0
index 5d3d8d4005b8f..9ef4b3cfa733c 100644

[ ... ]

Quoted 11 lines
drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c +11 -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;

drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c static int thc_ipts_touch_set(struct thc_ipts *idev, bool enable) · line 325

Quoted 10 lines
drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c +10 -0
+
+	/* 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,
};

[ ... ]

Quoted 124 lines
drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c +124 -0
+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);

drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c static int thc_ipts_touch_set(struct thc_ipts *idev, bool enable) · line 502

Quoted 10 lines
drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c +10 -0
+
+	/*
+	 * 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.

[ ... ]

Quoted 31 lines
drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c +21 -2
@@ -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.

drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c int thc_ipts_port_start(struct thc_ipts *idev) · line 675

Quoted 10 lines
drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c +8 -0
+	 */
+	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.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3