HID: intel-thc-hid: intel-ipts: support THC IPTS ports
Subsystems (3)
v10 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 operations an IPTS port 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
The first patch is new in v10 and answers the layering review: the backend no longer writes THC_M_PRT_CONTROL itself and no longer keeps a copy of the SPI base clock. The core exports thc_port_supported(), thc_port_reset_release(), thc_port_reset_hold() and thc_spi_hz_from_div(), and thc_interrupt_quiesce() stops warning when the port is already in the state that was asked for.
Changes in v10:
- One core patch plus five driver patches; no direct register access and no private SPI base clock left in intel-ipts/.
- A state machine (DOWN, RESET, CONFIG, DMA, UP) replaces the running and dma_ready flags, so teardown knows how far to unwind.
- The hardirq stays out of the status registers while a reset owns them, and the threaded handler neither acknowledges nor claims the line in that window.
- resume_noirq/restore_noirq mask the port as soon as configuration space is back, as asked in the review of 4/5.
- IRQF_SHARED|IRQF_COND_ONESHOT is used only for the legacy pin; the MSI fallback is not a shared interrupt.
- Headers are grouped and sorted, and the series is clean under checkpatch --strict.
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 operations an IPTS port
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 | 109 +- drivers/hid/intel-thc-hid/Kconfig | 14 + drivers/hid/intel-thc-hid/Makefile | 4 + drivers/hid/intel-thc-hid/intel-ipts/ipts.c | 976 ++++++++++++++++++ drivers/hid/intel-thc-hid/intel-ipts/ipts.h | 220 ++++ .../hid/intel-thc-hid/intel-ipts/pci-ipts.c | 233 +++++ .../intel-thc-hid/intel-thc/intel-thc-dev.c | 107 +- .../intel-thc-hid/intel-thc/intel-thc-dev.h | 4 + 8 files changed, 1658 insertions(+), 9 deletions(-) create mode 100644 drivers/hid/intel-thc-hid/intel-ipts/ipts.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
PATCH v10 1/6 HID: intel-thc-hid: intel-thc: expose the operations an IPTS port needs
The IPTS backend sits on the same core but cannot reach everything it needs through it: it writes the reset and port-supported bits of THC_M_PRT_CONTROL itself, and keeps a copy of the SPI base clock to turn the divisor in the configuration block into a frequency it can ask for.
Add the calls for those:
- thc_port_supported() reads the capability bit. A port with nothing behind it answers nothing on the bus, so the question is worth asking before the port is brought up.
- thc_port_reset_release() and thc_port_reset_hold() write the reset bit. It is the reset request, and reads as one while the port is out of reset, so it is written and never read-modify-written: the port has to be told either way, whatever the bit reads back.
- thc_spi_hz_from_div() applies the base clock the gears are fractions of to a divisor a device reports. Keeping it next to the gear table is the point: a frequency computed from another base lands on the wrong gear, and the table compares rounded to 100 Hz, so this has to round the same way or divisor 3 falls just short of the 41 MHz gear.
thc_interrupt_quiesce() also stops warning when the port is already in the state that was asked for. Every bring-up and teardown path calls it on both ends, so being idle at either end is the normal case, and the warning says nothing the caller can act on.
No existing signature changes, and nothing else changes for the two modes already in tree; they can move over to the new calls later.
--- .../intel-thc-hid/intel-thc/intel-thc-dev.c | 107 ++++++++++++++++-- .../intel-thc-hid/intel-thc/intel-thc-dev.h | 4 + 2 files changed, 103 insertions(+), 8 deletions(-)
drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.c +99 -8
@@ -642,16 +642,13 @@ int thc_interrupt_quiesce(const struct thc_device *dev, bool int_quiesce)u32 ctrl;int ret; +/* Every bring-up and teardown path calls this, and a port that is +* already in the state that was asked for is the normal case there: +* nothing to write, nothing to wait for, nothing to report. +*/ regmap_read (dev->thc_regmap,THC_M_PRT_CONTROL_OFFSET , &ctrl); -if (!(ctrl &THC_M_PRT_CONTROL_THC_DEVINT_QUIESCE_EN ) && !int_quiesce) { -dev_warn (dev->dev,"THC interrupt already unquiesce\n" ); +if (!!(ctrl &THC_M_PRT_CONTROL_THC_DEVINT_QUIESCE_EN ) == int_quiesce)return 0 ; - } - -if ((ctrl &THC_M_PRT_CONTROL_THC_DEVINT_QUIESCE_EN ) && int_quiesce) { -dev_warn (dev->dev,"THC interrupt already quiesce\n" ); -return 0 ; - }/* Quiesce device interrupt - Set quiesce bit and waiting for THC HW to ACK */ if (int_quiesce)
@@ -1117,6 +1114,66 @@ EXPORT_SYMBOL_NS_GPL(thc_port_select, "INTEL_THC");#define THC_SPI_FREQUENCY_3M1 3125000#define THC_SPI_FREQUENCY_3M9 3906250#define THC_SPI_FREQUENCY_5M2 5208333 +/** +* 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_release - Take the port out of reset +* +* @dev: the pointer of THC private device context +* +* The reset bit of the port control register is the reset request and reads +* one when the port is out of reset, so the bit is written and never +* read-modify-written: the port has to be told either way. +* +* Return: 0 on success, other error codes on failed +*/ +int thc_port_reset_release (struct thc_device *dev) +{ +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_release,"INTEL_THC" ); + +/** +* thc_port_reset_hold - Put the port into reset +* +* @dev: the pointer of THC private device context +* +* Return: 0 on success, other error codes on failed +*/ +int thc_port_reset_hold (struct thc_device *dev) +{ +return regmap_write_bits (dev->thc_regmap,THC_M_PRT_CONTROL_OFFSET , +THC_M_PRT_CONTROL_DEVRST ,0 ); +} +EXPORT_SYMBOL_NS_GPL (thc_port_reset_hold,"INTEL_THC" ); +#define THC_SPI_FREQUENCY_7M 7812500#define THC_SPI_FREQUENCY_10M 10416700#define THC_SPI_FREQUENCY_15M 15625000
@@ -1130,6 +1187,9 @@ EXPORT_SYMBOL_NS_GPL(thc_port_select, "INTEL_THC");#define THC_SPI_LOW_FREQUENCY THC_SPI_FREQUENCY_17M +/* Every gear above is the base clock divided by an integer. */ +#define THC_SPI_BASE_HZ 125000000 +static u8 thc_get_spi_freq_div_val (struct thc_device *dev,u32 spi_freq_val) {static const int frequency[] = {
@@ -1207,6 +1267,37 @@ static bool thc_spi_needs_half_div(u8 freq_div, u32 spi_freq_val, spi_freq_val <THC_SPI_FREQUENCY_15M ; } +/** +* thc_spi_hz_from_div - Turn a base clock divisor into a bus frequency +* +* @div: divisor, zero when the device did not provide one +* @div8: divide the result by eight as well +* +* A device that describes its bus timing as a divisor has to come back +* through here rather than through a copy of the base clock of its own: the +* gears thc_spi_read_config() programs are fractions of that clock, and a +* frequency computed from another base lands on the wrong one. +* +* Return: the frequency in Hz, or 0 for a divisor of zero +*/ +u32 thc_spi_hz_from_div (u8 div,bool div8) +{ +u32 hz; + +if (!div) +return 0 ; + + hz =THC_SPI_BASE_HZ / div; +if (div8) + hz /=8 ; + +/* Geared frequencies are compared rounded to 100 Hz: round the same +* way here, or divisor 3 lands just below the 41 MHz gear. +*/ +return DIV_ROUND_CLOSEST (hz,100 ) *100 ; +} +EXPORT_SYMBOL_NS_GPL (thc_spi_hz_from_div,"INTEL_THC" ); +/** * thc_spi_read_config - Configure SPI bus read attributes *
drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.h +4 -0
@@ -134,7 +134,11 @@ void thc_change_ltr_mode(struct thc_device *dev, u32 ltr_mode);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_reset_hold (struct thc_device *dev); +int thc_port_reset_release (struct thc_device *dev);int thc_port_select (struct thc_device *dev,enum thc_port_type port_type); +int thc_port_supported (struct thc_device *dev,bool *supported); +u32 thc_spi_hz_from_div (u8 div,bool div8);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,
[email protected]
AI reviewRe: [PATCH v10 1/6] HID: intel-thc-hid: intel-thc: expose the operations an IPTS port needs
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] HID: intel-thc: 100 Hz rounding in thc_spi_hz_from_div() misses div8 gears --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] HID: intel-thc: 100 Hz rounding in thc_spi_hz_from_div() misses div8 gears
--
commit 7cfe4283a39cc31a3e9b305786a30873184104d6
Author: Emfox Zhou <[email protected]>
HID: intel-thc-hid: intel-thc: expose the operations an IPTS port needs
This commit adds helper functions to check port support, control port reset, and convert SPI base clock divisors into bus frequencies, and stops warning in thc_interrupt_quiesce() when the port is already in the target state.
Quoted 4 lines
drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.c +0
-0
[ ... ]
Quoted 23 lines
drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.c +19
-0@@ -1207,6 +1267,37 @@ static bool thc_spi_needs_half_div(u8 freq_div, u32 spi_freq_val, spi_freq_val <THC_SPI_FREQUENCY_15M ; } +/** +* thc_spi_hz_from_div - Turn a base clock divisor into a bus frequency +* +* @div: divisor, zero when the device did not provide one +* @div8: divide the result by eight as well +* +* A device that describes its bus timing as a divisor has to come back +* through here rather than through a copy of the base clock of its own: the +* gears thc_spi_read_config() programs are fractions of that clock, and a +* frequency computed from another base lands on the wrong one. +* +* Return: the frequency in Hz, or 0 for a divisor of zero +*/ +u32 thc_spi_hz_from_div (u8 div,bool div8) +{ +u32 hz; + +if (!div) +return 0 ;
drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.c static bool thc_spi_needs_half_div(u8 freq_div, u32 spi_freq_val, · line 1298
Quoted 10 lines
drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.c +10
-0+ + hz =THC_SPI_BASE_HZ / div; +if (div8) + hz /=8 ; + +/* Geared frequencies are compared rounded to 100 Hz: round the same +* way here, or divisor 3 lands just below the 41 MHz gear. +*/ +return DIV_ROUND_CLOSEST (hz,100 ) *100 ; +}
[Severity: Medium]
Does rounding to 100 Hz in thc_spi_hz_from_div() miss the low-frequency gears when div8 is true?
Unlike the high-frequency gears, the div8 thresholds in frequency[] are defined to 1 Hz precision:
drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.c:
#define THC_SPI_FREQUENCY_2M2 2232143
#define THC_SPI_FREQUENCY_2M6 2604167
#define THC_SPI_FREQUENCY_3M1 3125000
#define THC_SPI_FREQUENCY_3M9 3906250
#define THC_SPI_FREQUENCY_5M2 5208333thc_get_spi_freq_div_val() requires spi_freq_val >= frequency[i] to select gear i. With div8 = true, div = 7 yields 2232100 Hz, which falls below THC_SPI_FREQUENCY_2M2 and causes thc_get_spi_freq_div_val() to fail with THC_SPI_FRQ_RESERVED (-EINVAL in thc_spi_read_config() and thc_spi_write_config()). Similarly, div = 3 with div8 = true yields 5208300 Hz, which misses THC_SPI_FREQUENCY_5M2 and demotes the clock to THC_SPI_FREQUENCY_3M9.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v10 2/6 HID: intel-thc-hid: intel-ipts: bring up the port
In reply to Emfox ZhouPorts on Tiger Lake, Lakefield, Alder Lake and Raptor Lake can run in IPTS mode: instead of HIDSPI the port drives the touch controller over plain SPI, and the controller answers with a 64 byte configuration block at address zero that carries the vendor and product IDs, the buffer sizes and the SPI timing it expects.
Add a port driver that does that much: select the SPI port, release the controller from reset, read the configuration block over the existing PIO path, apply the timing the controller asked for and enable the touch engine. Removal puts it back into reset.
Three things are not obvious from the register names:
- The bulk data window is at 0x1000, not at zero, and has to be set before the DMA engines run: they write there, and left unset they write into the configuration block, where the controller drops the frames without reporting an error.
- Quiescing gates the whole data path, not only the interrupt. The port stays quiesced while the block is read and the timing applied, and is released only once that is done.
- The edge detect bit belongs to the interrupt setup, but is set
here: the first configuration read is unreliable without it.
How far the bring-up got is recorded as one state rather than a flag per
step: the teardown has to undo exactly what completed, and a start that fails half way leaves the port at the last step that did. The state moves forward only while the port starts and backward only while it stops, so the checks on both paths read as the question they answer.
Meteor Lake and newer IDs are deliberately absent: those ports are already claimed by the HIDSPI driver.
--- drivers/hid/intel-thc-hid/Kconfig | 13 ++ drivers/hid/intel-thc-hid/Makefile | 4 + drivers/hid/intel-thc-hid/intel-ipts/ipts.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.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
@@ -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
@@ -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.o + ccflags-y += -I $(src)/intel-thc
drivers/hid/intel-thc-hid/intel-ipts/ipts.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
[email protected]
AI reviewRe: [PATCH v10 2/6] HID: intel-thc-hid: intel-ipts: bring up the port
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] HID: intel-thc-hid: 100 Hz rounding in thc_spi_hz_from_div() misses slow gears --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] HID: intel-thc-hid: 100 Hz rounding in thc_spi_hz_from_div() misses slow gears
--
commit 9057f6d4695018de027656c79eb1b4c3207e3b0b
Author: Emfox Zhou <[email protected]>
HID: intel-thc-hid: intel-ipts: bring up the port
This commit adds the intel-ipts PCI port driver to initialize Intel Touch Host Controller ports in IPTS mode, read the controller's configuration block over PIO, configure SPI timing, and enable the touch engine.
Quoted 5 lines
drivers/hid/intel-thc-hid/intel-ipts/ipts.c new file +0
-0
[ ... ]
Quoted 12 lines
drivers/hid/intel-thc-hid/intel-ipts/ipts.c new file +12
-0+/* The core takes the fastest gear that is not above the request. */ +static void thc_ipts_spi_from_cfg (struct thc_ipts *idev,u32 *hz,u32 *mode) +{ +u32 cfg =le32_to_cpu (idev->cfg.spi_cfg); + + *hz =thc_spi_hz_from_div (THC_IPTS_SPI_CLKDIV (cfg), + cfg &THC_IPTS_SPI_CLKDIV8 ); +if (!*hz) { +dev_warn_once (idev->dev,"controller asks for divisor 0\n" ); + *hz =THC_IPTS_SPI_MIN_HZ ; + } +
drivers/hid/intel-thc-hid/intel-ipts/ipts.c line 80
Quoted 10 lines
drivers/hid/intel-thc-hid/intel-ipts/ipts.c new file +10
-0+/* +* THC_IPTS_SPI_MIN_HZ is the slowest gear the core has, so a lower +* request would be refused: run at the floor and keep the port. +*/ +if (*hz <THC_IPTS_SPI_MIN_HZ ) { +dev_warn_once (idev->dev, +"controller asks for %u Hz, running at %u Hz\n" , + *hz,THC_IPTS_SPI_MIN_HZ ); + *hz =THC_IPTS_SPI_MIN_HZ ; + }
[Severity: Medium]
Does the 100 Hz rounding in thc_spi_hz_from_div() cause slow SPI gears with THC_IPTS_SPI_CLKDIV8 set to miss their target frequencies?
thc_spi_hz_from_div() rounds the computed frequency to the nearest 100 Hz, whereas the low-frequency gear constants in intel-thc-dev.c and THC_IPTS_SPI_MIN_HZ in ipts.h are specified to 1 Hz precision:
drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.c:
#define THC_SPI_FREQUENCY_2M2 2232143
#define THC_SPI_FREQUENCY_2M6 2604167
#define THC_SPI_FREQUENCY_3M1 3125000
#define THC_SPI_FREQUENCY_3M9 3906250
#define THC_SPI_FREQUENCY_5M2 5208333drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.c:thc_spi_hz_from_div() {
...
hz = THC_SPI_BASE_HZ / div;
if (div8)
hz /= 8;
...
return DIV_ROUND_CLOSEST(hz, 100) * 100;
}When a controller requests divisor 3 with divide-by-8 enabled, thc_spi_hz_from_div(3, true) rounds 5208333 down to 5208300. Because 5208300 is below THC_SPI_FREQUENCY_5M2 (5208333), thc_get_spi_freq_div_val() falls back to THC_SPI_FREQUENCY_3M9 (3906250 Hz, THC_SPI_FRQ_DIV_4), running the SPI bus 25% slower than requested.
Similarly, when a controller requests the valid slowest hardware gear (divisor 7 with divide-by-8 enabled), thc_spi_hz_from_div(7, true) returns 2232100, which is below THC_IPTS_SPI_MIN_HZ (2232143) and triggers a spurious warning in thc_ipts_spi_from_cfg().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v10 3/6 HID: intel-thc-hid: intel-ipts: receive reports
In reply to Emfox Zhouand decode the frames that arrive.
Interrupt remapping rejects the MSI requests these ports send: the requester ID in them is 01:05.0, neither the port's own function nor a valid DMA alias for it, so only the legacy interrupt pin works once remapping is on. MSI is the fallback for a platform that does not route that pin. The pin is shared with other functions on the PCH, and such a line only takes handlers that agree on oneshot, so the request asks for it only if a sharer uses it. The hardirq masks the port itself, so a foreign interrupt costs at most a few register reads in the thread, and is reported as not handled. It also turns away while a reset owns the status registers, and the run of the thread that finds it in flight acknowledges nothing and claims nothing: a thread that returns IRQ_HANDLED is counted as having handled the interrupt, which would hide a peer screaming on the line the port shares.
Transfer errors are recovered from a work item and not from the threaded
handler: thc_rxdma_reset() takes the bus lock, and the core's write path holds that lock while it waits for the very completion the handler delivers. The port is masked as soon as the reset is queued, and the threaded handler stops touching the status registers once the reset runs:
acknowledging a frame it is not going to read would lose the interrupt that announces it again. The thread masks the port on entry as well, because a sharer can queue a second run through IRQTF_RUNTHREAD with no hardirq of ours in between. A run that is already inside can unmask just before the wait for it returns, so the reset masks the line once more before it touches the engines, and the thread leaves the line open only when neither a reset nor a teardown is in flight.
Every frame carries a code and a length in front of the payload. The receive side decodes that header and logs each frame.
Three details of the bring-up are worth spelling out:
- The interrupt configuration has to be applied before the DMA engines are started: it programs the device interrupt type field to the invalid value, and starting the receive engine then replaces that field with the type it expects. The other order would leave the invalid value in place.
- An event that arrives while the thread runs leaves nothing behind but its status bit, so the handler drains the status registers instead of trusting a single delivery: a write completion taken for granted this way makes the next transfer wait for a completion that never comes, and the port stays unusable until it is reset.
- Bulk transfers have no address phase, so the SPI opcode is zero. The opcodes the HIDSPI path uses make this controller ignore the frames altogether.
The frame size the controller reports is bounded by the receive ring, one page per PRD entry: a port that asks for a larger buffer is refused with an explicit error instead of failing in sgl_alloc().
The reset covers the host side only and leaves the PRD table descriptors the controller rewrites in place: taking a frame puts its entry back, but a frame that is not read keeps the length the controller wrote, and the next frame that does not fit ends in a PRD overrun. intel-quickspi and intel-quicki2c recover through the same call and have the same gap. Restoring those descriptors is the core's job, not this driver's, and a separate change to intel-thc does it.
--- drivers/hid/intel-thc-hid/intel-ipts/ipts.c | 328 ++++++++++++++++++ drivers/hid/intel-thc-hid/intel-ipts/ipts.h | 24 ++ .../hid/intel-thc-hid/intel-ipts/pci-ipts.c | 50 +++ 3 files changed, 402 insertions(+)
drivers/hid/intel-thc-hid/intel-ipts/ipts.c +328 -0
@@ -4,7 +4,12 @@*/ #include <linux/delay.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 +20,19 @@/* 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 stops at the first error cause and reports the rest as a mask, so +* reading until it finds nothing can take a pass per cause plus one that +* confirms 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;
@@ -101,6 +116,291 @@ static int thc_ipts_touch_set(struct thc_ipts *idev, bool enable) &touch); } +static void thc_ipts_handle_frame (struct thc_ipts *idev,size_t len) +{ +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 ); + +dev_dbg (idev->dev,"frame code %u, %u bytes\n" , code, size); +} + +static void thc_ipts_dma_stop (struct thc_ipts *idev) +{ +if (READ_ONCE (idev->state) <THC_IPTS_DMA ) +return ; + +WRITE_ONCE (idev->state,THC_IPTS_CONFIG ); +thc_dma_unconfigure (idev->thc); +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 receive ring has one page per PRD entry and the core refuses a +* frame that needs more: bound it here, or the port fails with a bare +* allocation error from 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 ; + } + +/* +* A resume can report larger limits than the probe did, and the +* engines would then write past the end. Follow them on every start; +* the receive buffer is rounded up because the core rounds to a page. +*/ + ret =thc_ipts_resize (idev, &idev->rx_frame, &idev->buf_rx, +round_up (idev->max_rx,SZ_4K )); +if (ret) +return ret; + + 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 in flight still gets the thread: the status it drains is +* where a pending transmit completion is delivered, and a write +* waiting for that one holds the bus lock the teardown needs. A reset +* needs no such help, it masks for itself before it waits. +*/ +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 and queue the reset. The line stays open: a write that +* is already waiting for its completion needs this interrupt, and the work +* item takes the port once that 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); +} + +/* +* From a work item: the reset waits for the bus lock a pending write holds +* until the threaded handler delivers its completion. +*/ +void thc_ipts_recover (struct work_struct *work) +{ + struct thc_ipts *idev = container_of(work, struct thc_ipts, + recover_work); + +/* +* From here the status registers belong to the reset: the threaded +* handler must not acknowledge a frame it is not going to read, or the +* interrupt that would announce it again never comes. The flag alone +* is only a snapshot to a run that is already inside, so the mask and +* the wait below are what keeps that run out. +*/ +WRITE_ONCE (idev->recovering, true); +thc_interrupt_enable (idev->thc, false); +synchronize_irq (idev->irq); +/* A run that unmasked just before synchronize_irq() returned leaves +* the line open: mask it again, the write is idempotent. +*/ +thc_interrupt_enable (idev->thc, false); + +if (thc_rxdma_reset (idev->thc)) { +/* Nothing left to reset: keep the port masked until it +* restarts, and let the requests fail with -ENODEV instead of +* -EAGAIN, which would look like a reset that never ends. +*/ +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); +return ; + } + +/* The reset drops whatever the engines had queued. A run of the +* handler that sees ->recovering clear must not be turned away by +* ->recover_pending afterwards, so that one is cleared first. +*/ +WRITE_ONCE (idev->recover_seq,READ_ONCE (idev->recover_seq) +1 ); +WRITE_ONCE (idev->recover_pending, false); +WRITE_ONCE (idev->recovering, false); +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; + +/* Reports are handed to the HID core from here, and a driver callback +* that sends a request would wait for a completion this very thread +* delivers: it is recorded so the send path can tell. +*/ +WRITE_ONCE (idev->irq_thread, current); + +/* A sharer can queue a second run of this thread with no hardirq of +* ours in between: mask again, the write is idempotent. +*/ +thc_interrupt_enable (idev->thc, false); + +/* The port stays masked, so a cause that arrives meanwhile leaves only +* its status bit: read until the core reports none. +*/ +for (pass =0 ; pass <THC_IPTS_IRQ_PASSES ; pass++) { +int mask; + +/* +* The reset owns the status registers while it runs: a frame +* acknowledged here but not read is never announced again. +* Nothing is acknowledged here either, so the line is not +* claimed. +*/ +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 raises this for every frame until the +* reset takes the port: report the first one only. +*/ +if (!reported) { +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 Tx completion +* behind the error would time out under the bus lock. +*/ +continue ; + } + +/* A reset is queued or running, or the port is going away: the +* ring is not ours to read, only its status was. +*/ +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; + } + +/* The work item resets the engine and unmasks again; a frame read +* meanwhile would corrupt the ring. +*/ +if (need_recover) { +thc_ipts_request_recover (idev); +return IRQ_HANDLED ; + } + +/* A reset unmasks when it is done and a teardown masks on its own, so +* the line only stays open here while neither runs: a write already +* on the wire needs the completion this handler delivers. +*/ +if (!READ_ONCE (idev->recovering) && !READ_ONCE (idev->stopping)) +thc_interrupt_enable (idev->thc, true); + +return ret; +} +/** * thc_ipts_port_start - bring one port up * @idev: port to start
@@ -111,6 +411,9 @@ int thc_ipts_port_start(struct thc_ipts *idev)bool supported;int ret; +WRITE_ONCE (idev->stopping, false); +WRITE_ONCE (idev->recovering, false); + ret =thc_interrupt_quiesce (idev->thc, true);if (ret)return ret;
@@ -193,6 +496,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 +515,26 @@ int thc_ipts_port_start(struct thc_ipts *idev)*/ void thc_ipts_port_stop (struct thc_ipts *idev) { +/* +* Mask the line, then wait for a handler that may be holding a +* frame for the HID device which is about to go away. +*/ +WRITE_ONCE (idev->stopping, true); +thc_interrupt_enable (idev->thc, false); +synchronize_irq (idev->irq); +cancel_work_sync (&idev->recover_work); + +/* A handler that read ->stopping early may have unmasked. */ +thc_interrupt_enable (idev->thc, false); + +/* Stop the controller before the buffers it writes into go away. */ if (READ_ONCE (idev->state) >=THC_IPTS_CONFIG )thc_ipts_touch_set (idev, false);/* Forced: the bit may read as clear and still need the command. */ thc_port_reset_hold (idev->thc);thc_interrupt_quiesce (idev->thc, true); + +thc_ipts_dma_stop (idev);WRITE_ONCE (idev->state,THC_IPTS_DOWN ); }
drivers/hid/intel-thc-hid/intel-ipts/ipts.h +24 -0
@@ -7,7 +7,9 @@#include <linux/build_bug.h> #include <linux/device.h> #include <linux/interrupt.h> +#include <linux/sched.h> #include <linux/types.h> +#include <linux/wait.h> #include "intel-thc-dev.h"
@@ -116,24 +118,46 @@ 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 +* @rx_frame: buffer for one received frame, including its header +* @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; +u8 *rx_frame; +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; }; +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);
drivers/hid/intel-thc-hid/intel-ipts/pci-ipts.c +50 -0
77 lines not on this page · raw message
[email protected]
AI reviewRe: [PATCH v10 3/6] HID: intel-thc-hid: intel-ipts: receive reports
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] HID: intel-thc-hid: DMA buffer leak on reset failure in thc_ipts_recover() - [Medium] HID…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Medium] HID: intel-thc-hid: DMA buffer leak on reset failure in thc_ipts_recover()
- [Medium] HID: intel-thc-hid: intel-ipts: invalid wait context in thc_ipts_hardirq()
- [Medium] HID: intel-thc-hid: intel-ipts: IRQ left masked on thc_ipts_thread() recovery
--
commit 7673d514a40c57318bfb19e7aad1ebad5c468aba
Author: Emfox Zhou <[email protected]>
HID: intel-thc-hid: intel-ipts: receive reports
This commit adds DMA receive ring management, shared INTx / MSI interrupt handling, error recovery, and incoming frame decoding for Intel THC IPTS ports.
Quoted 4 lines
drivers/hid/intel-thc-hid/intel-ipts/ipts.c +0
-0
[ ... ]
Quoted 1 line
drivers/hid/intel-thc-hid/intel-ipts/ipts.c +0
-0@@ -101,6 +116,291 @@ static int thc_ipts_touch_set(struct thc_ipts *idev, bool enable)
[ ... ]
Quoted 9 lines
drivers/hid/intel-thc-hid/intel-ipts/ipts.c +9
-0+static void thc_ipts_dma_stop (struct thc_ipts *idev) +{ +if (READ_ONCE (idev->state) <THC_IPTS_DMA ) +return ; + +WRITE_ONCE (idev->state,THC_IPTS_CONFIG ); +thc_dma_unconfigure (idev->thc); +thc_dma_release (idev->thc); +}
[ ... ]
drivers/hid/intel-thc-hid/intel-ipts/ipts.c static int thc_ipts_touch_set(struct thc_ipts *idev, bool enable) · line 219
Quoted 13 lines
drivers/hid/intel-thc-hid/intel-ipts/ipts.c +13
-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);
[Severity: Medium]
Can thc_interrupt_enable() trigger an invalid wait context splat here?
thc_ipts_probe() registers thc_ipts_hardirq() on the legacy PCI_IRQ_INTX line with IRQF_SHARED | IRQF_COND_ONESHOT. When the line is shared with an IRQF_ONESHOT handler, __setup_irq() promotes the action to IRQF_ONESHOT, so __handle_irq_event_percpu() runs thc_ipts_hardirq() in LD_WAIT_SPIN wait context without calling lockdep_hardirq_threaded(). thc_interrupt_enable() calls regmap_write_bits() on dev->thc_regmap, which is configured in thc_regmap_cfg with .fast_io = true and without .use_raw_spinlock = true, acquiring a regular spinlock_t in hardirq context.
[ ... ]
Quoted 40 lines
drivers/hid/intel-thc-hid/intel-ipts/ipts.c +40
-0+/* +* Refuse new requests and queue the reset. The line stays open: a write that +* is already waiting for its completion needs this interrupt, and the work +* item takes the port once that 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); +} + +/* +* From a work item: the reset waits for the bus lock a pending write holds +* until the threaded handler delivers its completion. +*/ +void thc_ipts_recover (struct work_struct *work) +{ + struct thc_ipts *idev = container_of(work, struct thc_ipts, + recover_work); + +/* +* From here the status registers belong to the reset: the threaded +* handler must not acknowledge a frame it is not going to read, or the +* interrupt that would announce it again never comes. The flag alone +* is only a snapshot to a run that is already inside, so the mask and +* the wait below are what keeps that run out. +*/ +WRITE_ONCE (idev->recovering, true); +thc_interrupt_enable (idev->thc, false); +synchronize_irq (idev->irq); +/* A run that unmasked just before synchronize_irq() returned leaves +* the line open: mask it again, the write is idempotent. +*/ +thc_interrupt_enable (idev->thc, false); + +if (thc_rxdma_reset (idev->thc)) { +/* Nothing left to reset: keep the port masked until it
drivers/hid/intel-thc-hid/intel-ipts/ipts.c static int thc_ipts_touch_set(struct thc_ipts *idev, bool enable) · line 303
Quoted 10 lines
drivers/hid/intel-thc-hid/intel-ipts/ipts.c +10
-0+* restarts, and let the requests fail with -ENODEV instead of +* -EAGAIN, which would look like a reset that never ends. +*/ +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); +return ; + }
[Severity: Medium]
Does lowering idev->state to THC_IPTS_CONFIG here leak the DMA buffers when thc_rxdma_reset() fails?
thc_rxdma_reset() does not call thc_dma_release() (and returns before thc_dma_unconfigure() if thc_interrupt_quiesce() or thc_wait_for_dma_pause() fails). Once idev->state is set to THC_IPTS_CONFIG, a subsequent thc_ipts_port_stop() sees idev->state < THC_IPTS_DMA in thc_ipts_dma_stop() and returns without unconfiguring DMA or releasing the coherent PRD tables and scatter-gather buffers.
[ ... ]
Quoted 7 lines
drivers/hid/intel-thc-hid/intel-ipts/ipts.c +7
-0+/* The work item resets the engine and unmasks again; a frame read +* meanwhile would corrupt the ring. +*/ +if (need_recover) { +thc_ipts_request_recover (idev); +return IRQ_HANDLED ; + }
drivers/hid/intel-thc-hid/intel-ipts/ipts.c static int thc_ipts_touch_set(struct thc_ipts *idev, bool enable) · line 402
Quoted 10 lines
drivers/hid/intel-thc-hid/intel-ipts/ipts.c +10
-0+ +/* A reset unmasks when it is done and a teardown masks on its own, so +* the line only stays open here while neither runs: a write already +* on the wire needs the completion this handler delivers. +*/ +if (!READ_ONCE (idev->recovering) && !READ_ONCE (idev->stopping)) +thc_interrupt_enable (idev->thc, true); + +return ret; +}
[Severity: Medium]
Does returning early when need_recover is true leave the port interrupt masked while an in-flight TxDMA write is waiting for completion?
thc_ipts_hardirq() and thc_ipts_thread() mask the interrupt on entry, and the comments on thc_ipts_request_recover() and thc_ipts_recover() note that the line must stay open until an in-flight write holding thc_bus_lock receives its completion interrupt. Returning here before thc_interrupt_enable(idev->thc, true) (and masking in thc_ipts_recover() before thc_rxdma_reset() waits on thc_bus_lock) starves thc_dma_write() of its completion interrupt until its 1-second timeout expires. Commit "HID: intel-thc-hid: intel-ipts: add the HID device" later serializes thc_ipts_recover() on idev->tx_lock and removes this early return, but changes the unmask condition to check !READ_ONCE(idev->recover_pending), which is already false after thc_ipts_request_recover().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH v10 4/6 HID: intel-thc-hid: intel-ipts: add the HID device
In reply to Emfox ZhouRegister a HID device and let it own the handshake: the report descriptor is not read once at probe and kept, it is asked for over the transport whenever the HID core parses the device, and feature reports are fetched the same way. Both need a lock that keeps one frame on the wire at a time, a wait queue the answer is delivered on, and buffers for the answers. The descriptor is asked for up to six times, 300 ms each; a request that finds the port masked by a reset waits for that reset instead of spending a try.
A feature answer arrives as a frame of its own and carries only the report ID back, which does not tell it from the answer to the next request for the same report: nothing on the wire says which request a frame answers. So the request is sent once, and a timeout shuts the channel until the port is started again, the only point where controller and device are known to agree. A reset is counted: an answer it dropped returns -EIO and leaves the channel open, because that frame was never left out there. The count is taken after the request is written, so a reset that finished before the write is not taken for one that dropped this frame. Resetting the engine instead would drop a late answer and the frame the reset had queued.
The state is updated under a lock, so accepting an answer and consuming it are one step; it is a mutex, because the answer is copied in the threaded handler and can be as large as a receive frame. The waits happen outside
it: the thread that delivers an answer calls into the HID core on its way and would otherwise wait for a lock the waiter holds, and one that sends a request from there waits for a completion only it can deliver, so the send path refuses that thread and serves every other context. A second feature request while one is outstanding is refused rather than queued, for that reason and because its answer could not be told from the first one's.
Requests are refused while a recovery is in flight: the port is masked, so a write would wait out its one second timeout holding the bus lock the reset needs. Teardown refuses new requests, then waits for one already inside thc_dma_write(), which needs the interrupt the mask takes away; the mask follows that wait, and the line is waited on once more after the buffers are gone. The transmit lock is not held across synchronize_irq():
the threaded handler can issue a request of its own from a HID callback.
The threaded handler leaves the line open whenever no reset is running, teardown included: that very run is what the teardown waits for before it masks, and masking here would take away the completion a write already on the wire needs, which the teardown then waits for over the bus lock.
Input reports go straight to the input layer and the raw stream is available through hidraw, where decoding belongs: the reports carry a heatmap, and turning it into contacts needs floating point arithmetic the kernel cannot do. The controller starts streaming once user space asks for it, through a set feature request on report 5.
--- drivers/hid/intel-thc-hid/Kconfig | 3 +- drivers/hid/intel-thc-hid/intel-ipts/ipts.c | 480 +++++++++++++++++- drivers/hid/intel-thc-hid/intel-ipts/ipts.h | 56 ++ .../hid/intel-thc-hid/intel-ipts/pci-ipts.c | 3 + 4 files changed, 519 insertions(+), 23 deletions(-)
drivers/hid/intel-thc-hid/Kconfig +2 -1
@@ -48,7 +48,8 @@ config INTEL_IPTS 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. + framing. It then exposes the reports as a HID device, so that a + decoder in user space can turn them into input events. Say Y/M here if you have an Intel touch controller that runs in IPTS mode. If unsure, say N.
drivers/hid/intel-thc-hid/intel-ipts/ipts.c +458 -22
597 lines not on this page · raw message
drivers/hid/intel-thc-hid/intel-ipts/ipts.h +56 -0
90 lines not on this page · raw message
drivers/hid/intel-thc-hid/intel-ipts/pci-ipts.c +3 -0
@@ -46,6 +46,9 @@ 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);
[email protected]
AI reviewRe: [PATCH v10 4/6] HID: intel-thc-hid: intel-ipts: add the HID device
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] HID: intel-thc-hid: intel-ipts: masked IRQ on recovery starves thc_dma_write() - [Medium] HID…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] HID: intel-thc-hid: intel-ipts: masked IRQ on recovery starves thc_dma_write()
- [Medium] HID: intel-thc-hid: intel-ipts: DMA leak in thc_ipts_dma_stop() on reset error
--
commit cf6199acae63b20d55f6ee4b4578d3004cdbf95e
Author: Emfox Zhou <[email protected]>
HID: intel-thc-hid: intel-ipts: add the HID device
This commit registers a HID device for Intel THC ports operating in IPTS mode, adding report descriptor and feature report handshakes, input report forwarding, and transmit serialization across recovery and teardown.
Quoted 4 lines
drivers/hid/intel-thc-hid/intel-ipts/ipts.c +0
-0
[ ... ]
Quoted 25 lines
drivers/hid/intel-thc-hid/intel-ipts/ipts.c +9
-1@@ -274,6 +400,15 @@ void thc_ipts_recover(struct work_struct *work) struct thc_ipts *idev = container_of(work, struct thc_ipts, recover_work); +/* +* A write that is already on the wire holds this lock and needs the +* interrupt that the mask below takes away: let it finish first, or it +* times out over the bus lock the reset is waiting for. +*/ +mutex_lock (&idev->tx_lock); +WRITE_ONCE (idev->recovering, true); +mutex_unlock (&idev->tx_lock); +/* * From here the status registers belong to the reset: the threaded * handler must not acknowledge a frame it is not going to read, or the @@ -281,7 +416,6 @@ void thc_ipts_recover(struct work_struct *work)* is only a snapshot to a run that is already inside, so the mask and * the wait below are what keeps that run out. */ -WRITE_ONCE (idev->recovering, true);thc_interrupt_enable (idev->thc, false);synchronize_irq (idev->irq);/* A run that unmasked just before synchronize_irq() returned leaves
drivers/hid/intel-thc-hid/intel-ipts/ipts.c void thc_ipts_recover(struct work_struct *work) · line 438
Quoted 6 lines
drivers/hid/intel-thc-hid/intel-ipts/ipts.c +1
-0@@ -299,6 +433,7 @@ void thc_ipts_recover(struct work_struct *work)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 ; }
[Severity: Medium]
Does setting idev->state to THC_IPTS_CONFIG when thc_rxdma_reset() fails cause thc_ipts_dma_stop() to leak the DMA allocations on teardown?
drivers/hid/intel-thc-hid/intel-ipts/ipts.c:thc_ipts_dma_stop() {
if (READ_ONCE(idev->state) < THC_IPTS_DMA)
return;
WRITE_ONCE(idev->state, THC_IPTS_CONFIG);
thc_dma_unconfigure(idev->thc);
thc_dma_release(idev->thc);
}Because idev->state is lowered to THC_IPTS_CONFIG here to make thc_ipts_send() return -ENODEV, a later thc_ipts_port_stop() will see idev->state < THC_IPTS_DMA and skip both thc_dma_unconfigure() and thc_dma_release(), leaking the non-devm coherent PRD tables and mapped scatterlists.
[ ... ]
Quoted 22 lines
drivers/hid/intel-thc-hid/intel-ipts/ipts.c +7
-8@@ -383,24 +522,294 @@ irqreturn_t thc_ipts_thread(int irq, void *data) need_recover = true; } -/* The work item resets the engine and unmasks again; a frame read -* meanwhile would corrupt the ring. +/* +* The work item resets the engine in its own time; a frame read +* meanwhile would corrupt the ring, so only queue the reset. */ -if (need_recover) { +if (need_recover)thc_ipts_request_recover (idev); -return IRQ_HANDLED ; - } -/* A reset unmasks when it is done and a teardown masks on its own, so -* the line only stays open here while neither runs: a write already -* on the wire needs the completion this handler delivers. +/* +* A reset unmasks when it is done, and a teardown masks on its own +* after waiting for this very run. Leaving the line open here is what
drivers/hid/intel-thc-hid/intel-ipts/ipts.c irqreturn_t thc_ipts_thread(int irq, void *data) · line 543
Quoted 10 lines
drivers/hid/intel-thc-hid/intel-ipts/ipts.c +4
-1+* a write already on the wire needs: this handler is where its +* completion is delivered, and masking it away would make that write +* time out over the bus lock the teardown is waiting for. */ -if (!READ_ONCE (idev->recovering) && !READ_ONCE (idev->stopping)) +if (!READ_ONCE (idev->recovering) && !READ_ONCE (idev->recover_pending))thc_interrupt_enable (idev->thc, true);return ret; }
[Severity: High]
Does checking !READ_ONCE(idev->recover_pending) here leave the interrupt masked while an in-flight write in thc_dma_write() still needs it?
Sender IRQ thread kworkerthc_ipts_send()
mutex_lock(&idev->tx_lock)
thc_dma_write()
thc_ipts_thread()
thc_interrupt_enable(..., false)
...
thc_ipts_request_recover()
idev->recover_pending = true
schedule_work(...)
/* recover_pending is true:
* IRQ is left masked
*/
thc_ipts_recover()
mutex_lock(&idev->tx_lock)
wait_event_timeout(..., 1 * HZ)
<-- TxDMA IRQ masked, times out after 1sthc_ipts_recover() waits on idev->tx_lock before setting idev->recovering and masking the line so that a write already on the wire can finish. However, thc_ipts_request_recover() sets idev->recover_pending = true just above, so checking !READ_ONCE(idev->recover_pending) prevents thc_ipts_thread() from unmasking the interrupt. Any concurrent thc_ipts_send() in thc_dma_write() stalls for its 1-second timeout holding tx_lock and thc_bus_lock, and if that write was from thc_ipts_get_feature(), the resulting -ETIMEDOUT permanently latches idev->feature_off = true until the port is restarted.
Should the unmask check here test only !READ_ONCE(idev->recovering)?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
PATCH v10 5/6 HID: intel-thc-hid: intel-ipts: handle system sleep
In reply to Emfox ZhouSystem sleep takes the port down and builds it up again rather than keeping the HID device across it: the controller forgets the streaming mode user space asked it for, and hidraw offers no way to tell user space to ask again. A new HID device is what comes back, so the hidraw node and the input device are destroyed on the way down and reappear under a new minor; a daemon holding them open sees that and repeats the request.
Nothing here arms a wakeup source, so the port does not have to stay alive across sleep: the same pair serves freeze, thaw, poweroff and restore, and a controller that lost power in hibernation comes back through the full bring-up. The line is enabled again before the resume callbacks run, while the port is still down, so resume_noirq masks it as soon as the configuration space is back.
--- .../hid/intel-thc-hid/intel-ipts/pci-ipts.c | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+)
drivers/hid/intel-thc-hid/intel-ipts/pci-ipts.c +56 -0
@@ -152,6 +152,61 @@ 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 can bring the global enable back with it, as firmware +* leaves it at probe. The line is shared and level triggered, and the +* handler has nothing to mask it with until the engines are up, so +* mask it here as probe does. +*/ +thc_interrupt_enable (idev->thc, false); + + ret =thc_ipts_port_start (idev); +if (ret) +thc_ipts_port_stop (idev); + +return ret; +} + +/* +* The line is enabled again before the regular resume callbacks run, and the +* port is still down then: mask it as early as the configuration space is +* back, or a level left asserted by firmware burns the shared line down to a +* disabled interrupt. +*/ +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);
@@ -166,6 +221,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, };
PATCH v10 6/6 HID: intel-thc-hid: intel-ipts: document the IPTS mode
In reply to Emfox ZhouThe THC documentation only covers HIDSPI and HIDI2C. Ports from before Meteor Lake run in a third mode, and the intel-ipts driver is written against behaviour that is not described anywhere in tree.
Record what the driver depends on: the layout of the configuration block, the frame codes and header sizes, the bulk data window, and the order in which quiesce, interrupt configuration and the DMA engines have to be set up.
--- Documentation/hid/intel-thc-hid.rst | 109 +++++++++++++++++++++++++++- 1 file changed, 108 insertions(+), 1 deletion(-)
Documentation/hid/intel-thc-hid.rst +108 -1
@@ -590,7 +590,114 @@ 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 +====== ========================================================== +0x00 pending interrupt causes +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 +0x24 firmware version word +====== ========================================================== + +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. + +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