Skip to contentSkip to conversation

Add spi-hid transport driver

PATCH Superseded Jingyuan Liang 6 participants 57 messages Oct 9 – Nov 23

Subsystems (6)

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v5 cover Add spi-hid transport driver

This series picks up the spi-hid driver work originally started by Microsoft. The patch breakdown has been modified and the implementation has been refactored to address upstream feedback and testing issues. We are submitting this as a new series while keeping the original sign-off chain to reflect the history.

Same as the original series, there is a change to HID documentation, some HID core changes to support a SPI device, the SPI HID transport driver, and HID over SPI Device Tree binding. We have added the HID over SPI ACPI support, power management, panel follower, and quirks for Ilitek touch controllers.

Original authors: Jarrett Schultz <[email protected]>,
Dmitry Antipov <[email protected]>

---
Changes in v5:
- Fix request/response races; match GET_FEATURE responses by report ID
- Reset the device on response timeout; wait uninterruptibly
- Validate device descriptor and report lengths; fix buffer sizing
- Honor rtype in raw_request(); fix GET_REPORT off-by-one
- Use hid_safe_input_report(); forward input reports only while open
- Disable IRQ around hid_device teardown; fix a sparse warning
- Use disable_work_sync() for reset_work in remove()
- Cap trace copies at 64 bytes; cacheline-align read approval buffers
- ACPI: select RESET_CONTROLLER, use device_reset_optional()
- OF & DT: read opcodes as u8, use dev_err_probe(), require reg
- PM: cancel reset_work on suspend, wait for reset before
reset_resume(), fix IRQ imbalance on resume failure
- Panel follower: fix regulator/IRQ imbalance; depend on DRM || !DRM
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Extended io_lock scope to protect the shid->hid pointer lifecycle
against races with the IRQ handler
- Cacheline-aligned DMA buffers, enforced 4-byte alignment
- Added error rollback for failed suspend/resume transitions
- Moved IRQ request to probe with IRQF_NO_AUTOEN (disabled by default)
- DT Bindings & OF Driver:
- Required a device-specific compatible string in the schema.
- Added description to explain why opcodes and addresses properties
need to be defined in the schema
- Added spi-peripheral-props.yaml reference and switched to unevaluatedProperties: false.
- Added fallback to default timing parameters in OF driver if match
data is missing.
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- Add io_lock init
- Relocate tracepoints to drivers/hid/spi-hid/ and fix tracepoint macros
- Add tracepoints for sync, error handling, reset, and report processing
- Clean up internal includes and fix Makefile CFLAGS
- Add more details in v2 changelog
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Clean up DT bindings: fix formatting and remove timing and flags properties
- Update DT binding example: use a device-specific compatible and drop reset_assert
- Simplify ACPI/OF match tables by removing ACPI_PTR/of_match_ptr
- Refactor OF driver to use match data for timing parameters instead
of DT properties
- Switch to fsleep() for delays in ACPI and OF drivers
- Drop patch 12 as it is vendor specific
- Add a lock to fix input/output concurrency race
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Angela Czubak (2):
HID: spi-hid: add transport driver skeleton for HID over SPI bus
HID: spi-hid: add ACPI support for HID over SPI

Jarrett Schultz (3):
Documentation: Correction in HID output_report callback description.
HID: Add BUS_SPI support and define HID_SPI_DEVICE macro
HID: spi-hid: add device tree support for HID over SPI

Jingyuan Liang (6):
HID: spi-hid: add spi-hid driver HID layer
HID: spi-hid: add HID SPI protocol implementation
HID: spi-hid: add spi_hid traces
dt-bindings: input: Document hid-over-spi DT schema
HID: spi-hid: add power management implementation
HID: spi-hid: add panel follower support

 .../devicetree/bindings/input/hid-over-spi.yaml    |  130 ++
 Documentation/hid/hid-transport.rst                |    4 +-
 drivers/hid/Kconfig                                |    2 +
 drivers/hid/Makefile                               |    2 +
 drivers/hid/hid-core.c                             |    3 +
 drivers/hid/spi-hid/Kconfig                        |   50 +
 drivers/hid/spi-hid/Makefile                       |   12 +
 drivers/hid/spi-hid/spi-hid-acpi.c                 |  262 +++
 drivers/hid/spi-hid/spi-hid-core.c                 | 1754 ++++++++++++++++++++
 drivers/hid/spi-hid/spi-hid-core.h                 |  118 ++
 drivers/hid/spi-hid/spi-hid-of.c                   |  243 +++
 drivers/hid/spi-hid/spi-hid-trace.h                |  184 ++
 drivers/hid/spi-hid/spi-hid.h                      |   46 +
 include/linux/hid.h                                |    2 +
 14 files changed, 2810 insertions(+), 2 deletions(-)

---
base-commit: ee9c669f9bf5fd2c24206746ded9382fe810df89
change-id: 20260212-send-upstream-75f6fd9ed92e

Best regards,

Signature · 1 line
Jingyuan Liang <[email protected]>
#1 lore raw

PATCH v5 1/11 Documentation: Correction in HID output_report callback description.

From: Jarrett Schultz <[email protected]>

Originally output_report callback was described as must-be asynchronous, but that is not the case in some implementations, namely i2c-hid. Correct the documentation to say that it may be asynchronous.

---
 Documentation/hid/hid-transport.rst | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Documentation/hid/hid-transport.rst +2 -2
index 6f1692da296c..2008cf432af1 100644
@@ -327,8 +327,8 @@ The available HID callbacks are:
 
    Send raw output report via intr channel. Used by some HID device drivers
    which require high throughput for outgoing requests on the intr channel. This
-   must not cause SET_REPORT calls! This must be implemented as asynchronous
-   output report on the intr channel!
+   must not cause SET_REPORT calls! This call might be asynchronous, so the
+   caller should not expect an immediate response!
 
    ::
 
Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
#2 lore raw

Re: [PATCH v5 01/11] Documentation: Correction in HID output_report callback description.

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] hid: documentation incorrectly relaxes asynchronous requirement for output_report --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] hid: documentation incorrectly relaxes asynchronous requirement for output_report --

commit 6d8fa62fc058c7f5630a13cc92c85b7867bc9869
Author: Jarrett Schultz <[email protected]>

Documentation: Correction in HID output_report callback description.

This commit updates the documentation for the HID output_report callback. It changes the description to state that the callback might be asynchronous rather than strictly requiring it, citing i2c-hid as an example.

Documentation/hid/hid-transport.rst The available HID callbacks are: · line 331

Quoted 12 lines
Documentation/hid/hid-transport.rst +2 -2
index 6f1692da296cf..2008cf432af12 100644
@@ -327,8 +327,8 @@ The available HID callbacks are:
 
    Send raw output report via intr channel. Used by some HID device drivers
    which require high throughput for outgoing requests on the intr channel. This
-   must not cause SET_REPORT calls! This must be implemented as asynchronous
-   output report on the intr channel!
+   must not cause SET_REPORT calls! This call might be asynchronous, so the
+   caller should not expect an immediate response!

[Severity: Medium]
Does this documentation change contradict the strict architectural guarantees of the HID subsystem?

The HID subsystem design rules mandate that the output_report callback must be asynchronous to prevent latency spikes, performance degradation, or deadlocks on the high-throughput interrupt channel.

By stating that the call "might be asynchronous", could this inadvertently mislead future transport driver authors into implementing blocking, synchronous callbacks where they are not permitted?

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

Re: [PATCH v5 01/11] Documentation: Correction in HID output_report callback description.

Quoted 35 lines · On Fri, Oct 9, 2026 at 3:29 PM <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] hid: documentation incorrectly relaxes asynchronous requirement for output_report --

commit 6d8fa62fc058c7f5630a13cc92c85b7867bc9869
Author: Jarrett Schultz <[email protected]>

Documentation: Correction in HID output_report callback description.

This commit updates the documentation for the HID output_report callback. It changes the description to state that the callback might be asynchronous rather than strictly requiring it, citing i2c-hid as an example.

Documentation/hid/hid-transport.rst The available HID callbacks are: · line 331

Quoted 12 lines
Documentation/hid/hid-transport.rst +2 -2
index 6f1692da296cf..2008cf432af12 100644
@@ -327,8 +327,8 @@ The available HID callbacks are:
 
    Send raw output report via intr channel. Used by some HID device drivers
    which require high throughput for outgoing requests on the intr channel. This
-   must not cause SET_REPORT calls! This must be implemented as asynchronous
-   output report on the intr channel!
+   must not cause SET_REPORT calls! This call might be asynchronous, so the
+   caller should not expect an immediate response!

[Severity: Medium]
Does this documentation change contradict the strict architectural guarantees of the HID subsystem?

The HID subsystem design rules mandate that the output_report callback must be asynchronous to prevent latency spikes, performance degradation, or deadlocks on the high-throughput interrupt channel.

Quoted 6 lines

By stating that the call "might be asynchronous", could this inadvertently mislead future transport driver authors into implementing blocking, synchronous callbacks where they are not permitted?

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

The "must be asynchronous" rule doesn't match the in-tree transports. usbhid and i2c-hid both block in output_report(), and hid-input calls it from a workqueue as a "synchronous output report". This patch only brings the documentation in line with existing behavior; the actual requirements don't change.

#4 lore raw

Re: [PATCH v5 01/11] Documentation: Correction in HID output_report callback description.

In reply to Jingyuan Liang
Quoted 9 lines · Jingyuan Liang

From: Jarrett Schultz <[email protected]>

Originally output_report callback was described as must-be asynchronous, but that is not the case in some implementations, namely i2c-hid. Correct the documentation to say that it may be asynchronous.

No, you cannot test documentation. Do not add fake tags when they simply do not apply.

Best regards,
Krzysztof

#5 lore raw

Re: [PATCH v5 01/11] Documentation: Correction in HID output_report callback description.

Quoted 12 lines · Krzysztof Kozlowski
Quoted 9 lines · Jingyuan Liang

From: Jarrett Schultz <[email protected]>

Originally output_report callback was described as must-be asynchronous, but that is not the case in some implementations, namely i2c-hid. Correct the documentation to say that it may be asynchronous.

Quoted 7 lines

No, you cannot test documentation. Do not add fake tags when they simply do not apply.

Best regards,
Krzysztof

Thank you. I will drop the tested-by tag in v6.

#6 lore raw

PATCH v5 2/11 HID: Add BUS_SPI support and define HID_SPI_DEVICE macro

In reply to Jingyuan Liang

From: Jarrett Schultz <[email protected]>

If connecting a hid_device with bus field indicating BUS_SPI print out "SPI" in the debug print.

Macro sets the bus field to BUS_SPI and uses arguments to set vendor product fields.

---
 drivers/hid/hid-core.c | 3 +++
 include/linux/hid.h    | 2 ++
 2 files changed, 5 insertions(+)
drivers/hid/hid-core.c +3 -0
index a3ff0514f9cd..1b9dbb7515ad 100644
@@ -2373,6 +2373,9 @@ int hid_connect(struct hid_device *hdev, unsigned int connect_mask)
 	case BUS_I2C:
 		bus = "I2C";
 		break;
+	case BUS_SPI:
+		bus = "SPI";
+		break;
 	case BUS_SDW:
 		bus = "SOUNDWIRE";
 		break;
include/linux/hid.h +2 -0
index 8d17b741638c..45f713fe9982 100644
@@ -821,6 +821,8 @@ struct hid_descriptor {
 	.bus = BUS_BLUETOOTH, .vendor = (ven), .product = (prod)
 #define HID_I2C_DEVICE(ven, prod)				\
 	.bus = BUS_I2C, .vendor = (ven), .product = (prod)
+#define HID_SPI_DEVICE(ven, prod)				\
+	.bus = BUS_SPI, .vendor = (ven), .product = (prod)
 
 #define HID_REPORT_ID(rep) \
 	.report_type = (rep)
Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
#7 lore raw

PATCH v5 3/11 HID: spi-hid: add transport driver skeleton for HID over SPI bus

In reply to Jingyuan Liang

From: Angela Czubak <[email protected]>

Create spi-hid folder and add Kconfig and Makefile for spi-hid driver. Add basic device structure, definitions, and probe/remove functions.

---
 drivers/hid/Kconfig                |   2 +
 drivers/hid/Makefile               |   2 +
 drivers/hid/spi-hid/Kconfig        |  15 +++
 drivers/hid/spi-hid/Makefile       |   9 ++
 drivers/hid/spi-hid/spi-hid-core.c | 213 +++++++++++++++++++++++++++++++++++++
 5 files changed, 241 insertions(+)
drivers/hid/Kconfig +2 -0
index c43e824428f0..17a42301875c 100644
@@ -1527,6 +1527,8 @@ source "drivers/hid/bpf/Kconfig"
 
 source "drivers/hid/i2c-hid/Kconfig"
 
+source "drivers/hid/spi-hid/Kconfig"
+
 source "drivers/hid/intel-ish-hid/Kconfig"
 
 source "drivers/hid/amd-sfh-hid/Kconfig"
drivers/hid/Makefile +2 -0
index 48a863b245ee..b5a7ef7f208f 100644
@@ -177,6 +177,8 @@ obj-$(CONFIG_USB_KBD)		+= usbhid/
 
 obj-$(CONFIG_I2C_HID_CORE)	+= i2c-hid/
 
+obj-$(CONFIG_SPI_HID_CORE)	+= spi-hid/
+
 obj-$(CONFIG_INTEL_ISH_HID)	+= intel-ish-hid/
 
 obj-$(CONFIG_AMD_SFH_HID)       += amd-sfh-hid/
drivers/hid/spi-hid/Kconfig new file +15 -0
new file mode 100644
index 000000000000..836fdefe8345
@@ -0,0 +1,15 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Copyright (c) 2021 Microsoft Corporation
+#
+
+menuconfig SPI_HID
+	tristate "SPI HID support"
+	default y
+	depends on SPI
+
+if SPI_HID
+
+config SPI_HID_CORE
+	tristate
+endif
drivers/hid/spi-hid/Makefile new file +9 -0
new file mode 100644
index 000000000000..92e24cddbfc2
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Makefile for the SPI HID input drivers
+#
+# Copyright (c) 2021 Microsoft Corporation
+#
+
+obj-$(CONFIG_SPI_HID_CORE)	+= spi-hid.o
+spi-hid-objs 			= spi-hid-core.o
drivers/hid/spi-hid/spi-hid-core.c new file +213 -0

213 lines not on this page · raw message

Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
#8 lore raw

PATCH v5 4/11 HID: spi-hid: add spi-hid driver HID layer

In reply to Jingyuan Liang

Add HID low level driver callbacks to register SPI as a HID driver, and an external touch device as a HID device.

---
 drivers/hid/spi-hid/spi-hid-core.c | 592 +++++++++++++++++++++++++++++++++++++
 1 file changed, 592 insertions(+)
drivers/hid/spi-hid/spi-hid-core.c +592 -0
index 02a7608c4b88..fca7a44eeb9f 100644
@@ -20,13 +20,69 @@
  *  Copyright (c) 2006-2010 Jiri Kosina
  */
 
+#include <linux/completion.h>
+#include <linux/crc32.h>
 #include <linux/device.h>
+#include <linux/err.h>
 #include <linux/hid.h>
 #include <linux/hid-over-spi.h>
 #include <linux/interrupt.h>
+#include <linux/jiffies.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/slab.h>
 #include <linux/spi/spi.h>
+#include <linux/string.h>
+#include <linux/sysfs.h>
+#include <linux/unaligned.h>
+
+#define SPI_HID_OUTPUT_REPORT_CONTENT_ID_DESC_REQUEST	0x00
+
+#define SPI_HID_RESP_TIMEOUT	1000
+
+/* Protocol message size constants */
+#define SPI_HID_OUTPUT_HEADER_LEN		8
+
+/* flags */
+/*
+ * ready flag indicates that the FW is ready to accept commands and
+ * requests. The FW becomes ready after sending the report descriptor.
+ */
+#define SPI_HID_READY	0
+
+/* Raw input buffer with data from the bus */
+struct spi_hid_input_buf {
+	u8 header[HIDSPI_INPUT_HEADER_SIZE];
+	u8 body[HIDSPI_INPUT_BODY_HEADER_SIZE];
+	u8 content[];
+};
+
+/* Raw output report buffer to be put on the bus */
+struct spi_hid_output_buf {
+	u8 header[SPI_HID_OUTPUT_HEADER_LEN];
+	u8 content[];
+};
+
+/* Data necessary to send an output report */
+struct spi_hid_output_report {
+	u8 report_type;
+	u16 content_length;
+	u8 content_id;
+	u8 *content;
+};
+
+/* Processed data from a device descriptor */
+struct spi_hid_device_descriptor {
+	u16 hid_version;
+	u16 report_descriptor_length;
+	u16 max_input_length;
+	u16 max_output_length;
+	u16 max_fragment_length;
+	u16 vendor_id;
+	u16 product_id;
+	u16 version_id;
+	u8 no_output_report_ack;
+};
 
 /* struct spi_hid_conf - Conf provided to the core */
 struct spi_hid_conf {
@@ -61,8 +117,26 @@ struct spi_hid {
 	struct spihid_ops	*ops;
 	struct spi_hid_conf	*conf;
 
+	struct spi_hid_device_descriptor desc;	/* HID device descriptor. */
+	struct spi_hid_output_buf *output;	/* Output buffer. */
+	struct spi_hid_input_buf *input;	/* Input buffer. */
+	struct spi_hid_input_buf *response;	/* Response buffer. */
+
+	u16 response_length;
+	u16 bufsize;
+
 	enum hidspi_power_state power_state;
 
+	u8 reset_attempts;	/* The number of reset attempts. */
+
+	unsigned long flags;	/* device flags. */
+
+	/* Control lock to make sure one output transaction at a time. */
+	struct mutex output_lock;
+	struct completion output_done;
+
+	u32 report_descriptor_crc32;	/* HID report descriptor crc32 checksum. */
+
 	u32 regulator_error_count;
 	int regulator_last_error;
 	u32 bus_error_count;
@@ -70,6 +144,33 @@ struct spi_hid {
 	u32 dir_count;	/* device initiated reset count. */
 };
 
+static struct hid_ll_driver spi_hid_ll_driver;
+
+static void spi_hid_populate_output_header(u8 *buf,
+					   const struct spi_hid_conf *conf,
+					   const struct spi_hid_output_report *report)
+{
+	buf[0] = conf->write_opcode;
+	put_unaligned_be24(conf->output_report_address, &buf[1]);
+	buf[4] = report->report_type;
+	put_unaligned_le16(report->content_length, &buf[5]);
+	buf[7] = report->content_id;
+}
+
+static int spi_hid_output(struct spi_hid *shid, const void *buf, u16 length)
+{
+	int error;
+
+	error = spi_write(shid->spi, buf, length);
+
+	if (error) {
+		shid->bus_error_count++;
+		shid->bus_last_error = error;
+	}
+
+	return error;
+}
+
 static const char *spi_hid_power_mode_string(enum hidspi_power_state power_state)
 {
 	switch (power_state) {
@@ -84,11 +185,484 @@ static const char *spi_hid_power_mode_string(enum hidspi_power_state power_state
 	}
 }
 
+static void spi_hid_stop_hid(struct spi_hid *shid)
+{
+	struct hid_device *hid = shid->hid;
+
+	shid->hid = NULL;
+	clear_bit(SPI_HID_READY, &shid->flags);
+
+	if (hid)
+		hid_destroy_device(hid);
+}
+
+static int __spi_hid_send_output_report(struct spi_hid *shid,
+					struct spi_hid_output_report *report)
+{
+	struct spi_hid_output_buf *buf = shid->output;
+	struct device *dev = &shid->spi->dev;
+	u16 report_length;
+	u16 padded_length;
+	u8 padding;
+	int error;
+
+	if (report->content_length > shid->desc.max_output_length ||
+	    report->content_length > shid->bufsize) {
+		dev_err(dev, "Output report too big, content_length 0x%x\n",
+			report->content_length);
+		return -E2BIG;
+	}
+
+	spi_hid_populate_output_header(buf->header, shid->conf, report);
+
+	if (report->content_length)
+		memcpy(&buf->content, report->content, report->content_length);
+
+	report_length = sizeof(buf->header) + report->content_length;
+	padded_length = round_up(report_length, 4);
+	padding = padded_length - report_length;
+	memset(&buf->content[report->content_length], 0, padding);
+
+	error = spi_hid_output(shid, buf, padded_length);
+	if (error)
+		dev_err(dev, "Failed output transfer: %d\n", error);
+
+	return error;
+}
+
+static int spi_hid_send_output_report(struct spi_hid *shid,
+				      struct spi_hid_output_report *report)
+{
+	guard(mutex)(&shid->output_lock);
+	return __spi_hid_send_output_report(shid, report);
+}
+
+static int __spi_hid_sync_request(struct spi_hid *shid,
+				  struct spi_hid_output_report *report)
+{
+	struct device *dev = &shid->spi->dev;
+	int error;
+
+	reinit_completion(&shid->output_done);
+
+	error = __spi_hid_send_output_report(shid, report);
+	if (error)
+		return error;
+
+	error = wait_for_completion_interruptible_timeout(&shid->output_done,
+							  msecs_to_jiffies(SPI_HID_RESP_TIMEOUT));
+	if (error == 0) {
+		dev_err(dev, "Response timed out\n");
+		return -ETIMEDOUT;
+	}
+	if (error < 0)
+		return error;
+
+	return 0;
+}
+
+static int spi_hid_sync_request(struct spi_hid *shid,
+				struct spi_hid_output_report *report)
+{
+	guard(mutex)(&shid->output_lock);
+	return __spi_hid_sync_request(shid, report);
+}
+
+/*
+ * This function returns the length of the report descriptor, or a negative
+ * error code if something went wrong.
+ */
+static int spi_hid_report_descriptor_request(struct spi_hid *shid)
+{
+	struct device *dev = &shid->spi->dev;
+	struct spi_hid_output_report report = {
+		.report_type = REPORT_DESCRIPTOR,
+		.content_length = 0,
+		.content_id = SPI_HID_OUTPUT_REPORT_CONTENT_ID_DESC_REQUEST,
+		.content = NULL,
+	};
+	int ret;
+
+	ret =  spi_hid_sync_request(shid, &report);
+	if (ret) {
+		dev_err(dev,
+			"Expected report descriptor not received: %d\n", ret);
+		return ret;
+	}
+
+	ret = shid->response_length;
+	if (ret != shid->desc.report_descriptor_length) {
+		ret = min_t(unsigned int, ret, shid->desc.report_descriptor_length);
+		dev_err(dev, "Received report descriptor length doesn't match device descriptor field, using min of the two: %d\n",
+			ret);
+	}
+
+	return ret;
+}
+
+static int spi_hid_create_device(struct spi_hid *shid)
+{
+	struct hid_device *hid;
+	struct device *dev = &shid->spi->dev;
+	int error;
+
+	hid = hid_allocate_device();
+	error = PTR_ERR_OR_ZERO(hid);
+	if (error) {
+		dev_err(dev, "Failed to allocate hid device: %d\n", error);
+		return error;
+	}
+
+	hid->driver_data = shid->spi;
+	hid->ll_driver = &spi_hid_ll_driver;
+	hid->dev.parent = &shid->spi->dev;
+	hid->bus = BUS_SPI;
+	hid->version = shid->desc.hid_version;
+	hid->vendor = shid->desc.vendor_id;
+	hid->product = shid->desc.product_id;
+
+	snprintf(hid->name, sizeof(hid->name), "spi %04X:%04X",
+		 hid->vendor, hid->product);
+	strscpy(hid->phys, dev_name(&shid->spi->dev), sizeof(hid->phys));
+
+	shid->hid = hid;
+
+	error = hid_add_device(hid);
+	if (error) {
+		dev_err(dev, "Failed to add hid device: %d\n", error);
+		/*
+		 * We likely got here because report descriptor request timed
+		 * out. Let's disconnect and destroy the hid_device structure.
+		 */
+		spi_hid_stop_hid(shid);
+		return error;
+	}
+
+	return 0;
+}
+
+static int spi_hid_get_request(struct spi_hid *shid, u8 content_id,
+			       u8 *buf, size_t len)
+{
+	struct device *dev = &shid->spi->dev;
+	struct spi_hid_output_report report = {
+		.report_type = GET_FEATURE,
+		.content_length = 0,
+		.content_id = content_id,
+		.content = NULL,
+	};
+	u16 content_len;
+	int error, ret;
+
+	/* buf[0] holds the report ID, so there must be room for at least that. */
+	if (!len)
+		return -EINVAL;
+
+	guard(mutex)(&shid->output_lock);
+
+	error = __spi_hid_sync_request(shid, &report);
+	if (error) {
+		dev_err(dev,
+			"Expected get request response not received! Error %d\n",
+			error);
+		return error;
+	}
+
+	/* Content Length counts only the payload, not the Content ID. */
+	content_len = get_unaligned_le16(&shid->response->body[1]);
+	if (content_len > shid->bufsize) {
+		dev_err(dev, "Get report response too big: %u > %u\n",
+			content_len, shid->bufsize);
+		return -EPROTO;
+	}
+
+	/* buf[0] holds the report ID, the payload follows. */
+	ret = min_t(size_t, len - 1, content_len);
+	buf[0] = shid->response->body[3];
+	memcpy(&buf[1], shid->response->content, ret);
+
+	return ret + 1;
+}
+
+static int spi_hid_set_request(struct spi_hid *shid, u8 *arg_buf, u16 arg_len,
+			       u8 content_id)
+{
+	struct spi_hid_output_report report = {
+		.report_type = SET_FEATURE,
+		.content_length = arg_len,
+		.content_id = content_id,
+		.content = arg_buf,
+	};
+
+	return spi_hid_sync_request(shid, &report);
+}
+
+/* This is a placeholder. Will be implemented in the next patch. */
 static irqreturn_t spi_hid_dev_irq(int irq, void *_shid)
 {
 	return IRQ_HANDLED;
 }
 
+static int spi_hid_alloc_buffers(struct spi_hid *shid, size_t report_size)
+{
+	struct device *dev = &shid->spi->dev;
+	int inbufsize = round_up(sizeof(shid->input->header) +
+				 sizeof(shid->input->body) + report_size, 4);
+	int outbufsize = round_up(sizeof(shid->output->header) + report_size, 4);
+	void *tmp;
+
+	tmp = devm_krealloc(dev, shid->output, outbufsize, GFP_KERNEL | __GFP_ZERO);
+	if (!tmp)
+		return -ENOMEM;
+	shid->output = tmp;
+
+	tmp = devm_krealloc(dev, shid->input, inbufsize, GFP_KERNEL | __GFP_ZERO);
+	if (!tmp)
+		return -ENOMEM;
+	shid->input = tmp;
+
+	tmp = devm_krealloc(dev, shid->response, inbufsize, GFP_KERNEL | __GFP_ZERO);
+	if (!tmp)
+		return -ENOMEM;
+	shid->response = tmp;
+
+	if (!shid->output || !shid->input || !shid->response)
+		return -ENOMEM;
+
+	shid->bufsize = report_size;
+
+	return 0;
+}
+
+static int spi_hid_get_report_length(struct hid_report *report)
+{
+	return DIV_ROUND_UP(report->size, 8) +
+		report->device->report_enum[report->type].numbered + 2;
+}
+
+/*
+ * Traverse the supplied list of reports and find the longest
+ */
+static void spi_hid_find_max_report(struct hid_device *hid, u32 type,
+				    u16 *max)
+{
+	struct hid_report *report;
+	u16 size;
+
+	/*
+	 * We should not rely on wMaxInputLength, as some devices may set it to
+	 * a wrong length.
+	 */
+	list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
+		size = spi_hid_get_report_length(report);
+		if (*max < size)
+			*max = size;
+	}
+}
+
+/* hid_ll_driver interface functions */
+
+static int spi_hid_ll_start(struct hid_device *hid)
+{
+	struct spi_device *spi = hid->driver_data;
+	struct spi_hid *shid = spi_get_drvdata(spi);
+	int error = 0;
+	/*
+	 * HID_MIN_BUFFER_SIZE is the minimum transport buffer size, not a
+	 * requirement on the device's report sizes. Use it as a floor so
+	 * devices with small reports are still supported.
+	 */
+	u16 bufsize = HID_MIN_BUFFER_SIZE;
+
+	spi_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
+	spi_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
+	spi_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
+
+	if (bufsize > shid->bufsize) {
+		guard(disable_irq)(&shid->spi->irq);
+
+		error = spi_hid_alloc_buffers(shid, bufsize);
+		if (error)
+			return error;
+	}
+
+	return 0;
+}
+
+static void spi_hid_ll_stop(struct hid_device *hid)
+{
+	hid->claimed = 0;
+}
+
+static int spi_hid_ll_open(struct hid_device *hid)
+{
+	return 0;
+}
+
+static void spi_hid_ll_close(struct hid_device *hid)
+{
+	struct spi_device *spi = hid->driver_data;
+	struct spi_hid *shid = spi_get_drvdata(spi);
+
+	shid->reset_attempts = 0;
+}
+
+static int spi_hid_ll_power(struct hid_device *hid, int level)
+{
+	struct spi_device *spi = hid->driver_data;
+	struct spi_hid *shid = spi_get_drvdata(spi);
+	int error = 0;
+
+	guard(mutex)(&shid->output_lock);
+	if (!shid->hid)
+		error = -ENODEV;
+
+	return error;
+}
+
+static int spi_hid_ll_parse(struct hid_device *hid)
+{
+	struct spi_device *spi = hid->driver_data;
+	struct spi_hid *shid = spi_get_drvdata(spi);
+	struct device *dev = &spi->dev;
+	unsigned int rsize = shid->desc.report_descriptor_length;
+	int error, len;
+
+	if (rsize > HID_MAX_DESCRIPTOR_SIZE) {
+		dev_err(dev,
+			"Report descriptor size %d is greater than HID_MAX_DESCRIPTOR_SIZE %d\n",
+			rsize, HID_MAX_DESCRIPTOR_SIZE);
+		return -EINVAL;
+	}
+
+	if (rsize > shid->bufsize) {
+		error = spi_hid_alloc_buffers(shid, rsize);
+		if (error)
+			return error;
+	}
+
+	len = spi_hid_report_descriptor_request(shid);
+	if (len < 0) {
+		dev_err(dev, "Report descriptor request failed, %d\n", len);
+		return len;
+	}
+
+	/*
+	 * FIXME: below call returning 0 doesn't mean that the report descriptor
+	 * is good. We might be caching a crc32 of a corrupted r. d. or who
+	 * knows what the FW sent. Need to have a feedback loop about r. d.
+	 * being ok and only then cache it.
+	 */
+	error = hid_parse_report(hid, (u8 *)shid->response->content, len);
+	if (error) {
+		dev_err(dev, "failed parsing report: %d\n", error);
+		return error;
+	}
+	shid->report_descriptor_crc32 = crc32_le(0,
+						 (unsigned char const *)shid->response->content,
+						 len);
+
+	set_bit(SPI_HID_READY, &shid->flags);
+
+	return 0;
+}
+
+static int spi_hid_ll_output_report(struct hid_device *hid, __u8 *buf,
+				    size_t len)
+{
+	struct spi_device *spi = hid->driver_data;
+	struct spi_hid *shid = spi_get_drvdata(spi);
+	struct device *dev = &spi->dev;
+	struct spi_hid_output_report report = {
+		.report_type = OUTPUT_REPORT,
+		.content_length = len - 1,
+		.content_id = buf[0],
+		.content = &buf[1],
+	};
+	int error;
+
+	if (!test_bit(SPI_HID_READY, &shid->flags)) {
+		dev_err(dev, "%s called in unready state\n", __func__);
+		return -ENODEV;
+	}
+
+	if (shid->desc.no_output_report_ack)
+		error = spi_hid_send_output_report(shid, &report);
+	else
+		error = spi_hid_sync_request(shid, &report);
+
+	if (error) {
+		dev_err(dev, "failed to send output report\n");
+		return error;
+	}
+
+	return len;
+}
+
+static int spi_hid_ll_raw_request(struct hid_device *hid,
+				  unsigned char reportnum, __u8 *buf,
+				  size_t len, unsigned char rtype, int reqtype)
+{
+	struct spi_device *spi = hid->driver_data;
+	struct spi_hid *shid = spi_get_drvdata(spi);
+	struct device *dev = &spi->dev;
+	int ret;
+
+	switch (reqtype) {
+	case HID_REQ_SET_REPORT:
+		if (buf[0] != reportnum) {
+			dev_err(dev, "report id mismatch\n");
+			return -EINVAL;
+		}
+
+		if (rtype == HID_OUTPUT_REPORT)
+			return spi_hid_ll_output_report(hid, buf, len);
+
+		if (rtype != HID_FEATURE_REPORT)
+			return -EINVAL;
+
+		ret = spi_hid_set_request(shid, &buf[1], len - 1,
+					  reportnum);
+		if (ret) {
+			dev_err(dev, "failed to set report\n");
+			return ret;
+		}
+
+		ret = len;
+		break;
+	case HID_REQ_GET_REPORT:
+		/*
+		 * Only feature reports for now: GET_INPUT_REPORT is not
+		 * supported.
+		 */
+		if (rtype != HID_FEATURE_REPORT)
+			return -EOPNOTSUPP;
+
+		ret = spi_hid_get_request(shid, reportnum, buf, len);
+		if (ret < 0) {
+			dev_err(dev, "failed to get report\n");
+			return ret;
+		}
+		break;
+	default:
+		dev_err(dev, "invalid request type\n");
+		return -EIO;
+	}
+
+	return ret;
+}
+
+static struct hid_ll_driver spi_hid_ll_driver = {
+	.start = spi_hid_ll_start,
+	.stop = spi_hid_ll_stop,
+	.open = spi_hid_ll_open,
+	.close = spi_hid_ll_close,
+	.power = spi_hid_ll_power,
+	.parse = spi_hid_ll_parse,
+	.output_report = spi_hid_ll_output_report,
+	.raw_request = spi_hid_ll_raw_request,
+};
+
 static ssize_t bus_error_count_show(struct device *dev,
 				    struct device_attribute *attr, char *buf)
 {
@@ -159,6 +733,18 @@ int spi_hid_core_probe(struct spi_device *spi, struct spihid_ops *ops,
 
 	spi_set_drvdata(spi, shid);
 
+	mutex_init(&shid->output_lock);
+	init_completion(&shid->output_done);
+
+	/*
+	 * we need to allocate the buffer without knowing the maximum
+	 * size of the reports. Let's use HID_MAX_DESCRIPTOR_SIZE, then we do the
+	 * real computation later.
+	 */
+	error = spi_hid_alloc_buffers(shid, HID_MAX_DESCRIPTOR_SIZE);
+	if (error)
+		return error;
+
 	/*
 	 * At the end of probe we initialize the device:
 	 *   0) assert reset, bias the interrupt line
@@ -191,6 +777,10 @@ int spi_hid_core_probe(struct spi_device *spi, struct spihid_ops *ops,
 	dev_dbg(dev, "%s: d3 -> %s\n", __func__,
 		spi_hid_power_mode_string(shid->power_state));
 
+	error = spi_hid_create_device(shid);
+	if (error)
+		return error;
+
 	return 0;
 }
 EXPORT_SYMBOL_GPL(spi_hid_core_probe);
@@ -201,6 +791,8 @@ void spi_hid_core_remove(struct spi_device *spi)
 	struct device *dev = &spi->dev;
 	int error;
 
+	spi_hid_stop_hid(shid);
+
 	shid->ops->assert_reset(shid->ops);
 	error = shid->ops->power_down(shid->ops);
 	if (error)
Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
#9 lore raw

Re: [PATCH v5 04/11] HID: spi-hid: add spi-hid driver HID layer

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] hid: output_report callback sleeps in atomic context --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] hid: output_report callback sleeps in atomic context
--

commit 954a3684cea1bad33a85c00ff6725f7553c75027
Author: Jingyuan Liang <[email protected]>

HID: spi-hid: add spi-hid driver HID layer

Add HID low level driver callbacks to register SPI as a HID driver, and an external touch device as a HID device.

Quoted 4 lines
drivers/hid/spi-hid/spi-hid-core.c +0 -0
index 02a7608c4b882..fca7a44eeb9f9 100644

[ ... ]

Quoted 37 lines
drivers/hid/spi-hid/spi-hid-core.c +37 -0
+static int spi_hid_send_output_report(struct spi_hid *shid,
+				      struct spi_hid_output_report *report)
+{
+	guard(mutex)(&shid->output_lock);
+	return __spi_hid_send_output_report(shid, report);
+}
+
+static int __spi_hid_sync_request(struct spi_hid *shid,
+				  struct spi_hid_output_report *report)
+{
+	struct device *dev = &shid->spi->dev;
+	int error;
+
+	reinit_completion(&shid->output_done);
+
+	error = __spi_hid_send_output_report(shid, report);
+	if (error)
+		return error;
+
+	error = wait_for_completion_interruptible_timeout(&shid->output_done,
+							  msecs_to_jiffies(SPI_HID_RESP_TIMEOUT));
+	if (error == 0) {
+		dev_err(dev, "Response timed out\n");
+		return -ETIMEDOUT;
+	}
+	if (error < 0)
+		return error;
+
+	return 0;
+}
+
+static int spi_hid_sync_request(struct spi_hid *shid,
+				struct spi_hid_output_report *report)
+{
+	guard(mutex)(&shid->output_lock);
+	return __spi_hid_sync_request(shid, report);
+}

[ ... ]

Quoted 13 lines
drivers/hid/spi-hid/spi-hid-core.c +13 -0
+static int spi_hid_ll_output_report(struct hid_device *hid, __u8 *buf,
+				    size_t len)
+{
+	struct spi_device *spi = hid->driver_data;
+	struct spi_hid *shid = spi_get_drvdata(spi);
+	struct device *dev = &spi->dev;
+	struct spi_hid_output_report report = {
+		.report_type = OUTPUT_REPORT,
+		.content_length = len - 1,
+		.content_id = buf[0],
+		.content = &buf[1],
+	};
+	int error;

drivers/hid/spi-hid/spi-hid-core.c static const char *spi_hid_power_mode_string(enum hidspi_power_state power_state · line 592

Quoted 10 lines
drivers/hid/spi-hid/spi-hid-core.c +10 -0
+
+	if (!test_bit(SPI_HID_READY, &shid->flags)) {
+		dev_err(dev, "%s called in unready state\n", __func__);
+		return -ENODEV;
+	}
+
+	if (shid->desc.no_output_report_ack)
+		error = spi_hid_send_output_report(shid, &report);
+	else
+		error = spi_hid_sync_request(shid, &report);

[Severity: High]
Can this code sleep in an atomic context?

The HID subsystem requires the output_report callback to be asynchronous because it can be invoked from atomic contexts, such as softirqs for LED triggers or force feedback.

Both spi_hid_send_output_report() and spi_hid_sync_request() acquire a mutex, which will sleep. Furthermore, spi_hid_sync_request() blocks by calling wait_for_completion_interruptible_timeout() in __spi_hid_sync_request() while waiting for the response.

Will calling these synchronous primitives from the output_report callback lead to a scheduling while atomic BUG and cause a kernel panic?

Quoted 8 lines
drivers/hid/spi-hid/spi-hid-core.c +8 -0
+
+	if (error) {
+		dev_err(dev, "failed to send output report\n");
+		return error;
+	}
+
+	return len;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#10 lore raw

Re: [PATCH v5 04/11] HID: spi-hid: add spi-hid driver HID layer

Quoted 95 lines · On Fri, Oct 9, 2026 at 3:42 PM <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] hid: output_report callback sleeps in atomic context
--

commit 954a3684cea1bad33a85c00ff6725f7553c75027
Author: Jingyuan Liang <[email protected]>

HID: spi-hid: add spi-hid driver HID layer

Add HID low level driver callbacks to register SPI as a HID driver, and an external touch device as a HID device.

Quoted 4 lines
drivers/hid/spi-hid/spi-hid-core.c +0 -0
index 02a7608c4b882..fca7a44eeb9f9 100644

[ ... ]

Quoted 37 lines
drivers/hid/spi-hid/spi-hid-core.c +37 -0
+static int spi_hid_send_output_report(struct spi_hid *shid,
+                                   struct spi_hid_output_report *report)
+{
+     guard(mutex)(&shid->output_lock);
+     return __spi_hid_send_output_report(shid, report);
+}
+
+static int __spi_hid_sync_request(struct spi_hid *shid,
+                               struct spi_hid_output_report *report)
+{
+     struct device *dev = &shid->spi->dev;
+     int error;
+
+     reinit_completion(&shid->output_done);
+
+     error = __spi_hid_send_output_report(shid, report);
+     if (error)
+             return error;
+
+     error = wait_for_completion_interruptible_timeout(&shid->output_done,
+                                                       msecs_to_jiffies(SPI_HID_RESP_TIMEOUT));
+     if (error == 0) {
+             dev_err(dev, "Response timed out\n");
+             return -ETIMEDOUT;
+     }
+     if (error < 0)
+             return error;
+
+     return 0;
+}
+
+static int spi_hid_sync_request(struct spi_hid *shid,
+                             struct spi_hid_output_report *report)
+{
+     guard(mutex)(&shid->output_lock);
+     return __spi_hid_sync_request(shid, report);
+}

[ ... ]

Quoted 13 lines
drivers/hid/spi-hid/spi-hid-core.c +13 -0
+static int spi_hid_ll_output_report(struct hid_device *hid, __u8 *buf,
+                                 size_t len)
+{
+     struct spi_device *spi = hid->driver_data;
+     struct spi_hid *shid = spi_get_drvdata(spi);
+     struct device *dev = &spi->dev;
+     struct spi_hid_output_report report = {
+             .report_type = OUTPUT_REPORT,
+             .content_length = len - 1,
+             .content_id = buf[0],
+             .content = &buf[1],
+     };
+     int error;

drivers/hid/spi-hid/spi-hid-core.c static const char *spi_hid_power_mode_string(enum hidspi_power_state power_state · line 592

Quoted 10 lines
drivers/hid/spi-hid/spi-hid-core.c +10 -0
+
+     if (!test_bit(SPI_HID_READY, &shid->flags)) {
+             dev_err(dev, "%s called in unready state\n", __func__);
+             return -ENODEV;
+     }
+
+     if (shid->desc.no_output_report_ack)
+             error = spi_hid_send_output_report(shid, &report);
+     else
+             error = spi_hid_sync_request(shid, &report);

[Severity: High]
Can this code sleep in an atomic context?

The HID subsystem requires the output_report callback to be asynchronous because it can be invoked from atomic contexts, such as softirqs for LED triggers or force feedback.

Both spi_hid_send_output_report() and spi_hid_sync_request() acquire a mutex, which will sleep. Furthermore, spi_hid_sync_request() blocks by calling wait_for_completion_interruptible_timeout() in __spi_hid_sync_request() while waiting for the response.

Will calling these synchronous primitives from the output_report callback lead to a scheduling while atomic BUG and cause a kernel panic?

Quoted 11 lines

drivers/hid/spi-hid/spi-hid-core.c static const char *spi_hid_power_mode_string(enum hidspi_power_state power_state · line 600

Quoted 8 lines
drivers/hid/spi-hid/spi-hid-core.c +8 -0
+
+     if (error) {
+             dev_err(dev, "failed to send output report\n");
+             return error;
+     }
+
+     return len;
+}

--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4

Not really. HID core doesn't call ->output_report() from atomic context (LED updates are sent from a workqueue), and as noted in my reply on the documentation patch, USB-HID and I2C-HID already sleep in this callback too.

#11 lore raw

PATCH v5 5/11 HID: spi-hid: add HID SPI protocol implementation

In reply to Jingyuan Liang

This driver follows HID Over SPI Protocol Specification 1.0 available at
https://www.microsoft.com/en-us/download/details.aspx?id=103325. The initial version of the driver does not support: 1) multi-fragment input reports, 2) sending GET_INPUT and COMMAND output report types and processing their respective acknowledge input reports, and 3) device sleep power state.

---
 drivers/hid/spi-hid/spi-hid-core.c | 777 +++++++++++++++++++++++++++++++++++--
 1 file changed, 743 insertions(+), 34 deletions(-)
drivers/hid/spi-hid/spi-hid-core.c +743 -34
index fca7a44eeb9f..447ba178d5bf 100644
@@ -20,14 +20,20 @@
  *  Copyright (c) 2006-2010 Jiri Kosina
  */
 
+#include <linux/cache.h>
 #include <linux/completion.h>
 #include <linux/crc32.h>
 #include <linux/device.h>
+#include <linux/dma-mapping.h>
 #include <linux/err.h>
 #include <linux/hid.h>
 #include <linux/hid-over-spi.h>
+#include <linux/input.h>
 #include <linux/interrupt.h>
+#include <linux/irq.h>
 #include <linux/jiffies.h>
+#include <linux/kernel.h>
+#include <linux/list.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
 #include <linux/slab.h>
@@ -35,12 +41,22 @@
 #include <linux/string.h>
 #include <linux/sysfs.h>
 #include <linux/unaligned.h>
+#include <linux/wait.h>
+#include <linux/workqueue.h>
+
+/* Protocol constants */
+#define SPI_HID_READ_APPROVAL_CONSTANT		0xff
+#define SPI_HID_INPUT_HEADER_SYNC_BYTE		0x5a
+#define SPI_HID_INPUT_HEADER_VERSION		0x03
+#define SPI_HID_SUPPORTED_VERSION		0x0300
 
 #define SPI_HID_OUTPUT_REPORT_CONTENT_ID_DESC_REQUEST	0x00
 
-#define SPI_HID_RESP_TIMEOUT	1000
+#define SPI_HID_MAX_RESET_ATTEMPTS	3
+#define SPI_HID_RESP_TIMEOUT		1000
 
 /* Protocol message size constants */
+#define SPI_HID_READ_APPROVAL_LEN		5
 #define SPI_HID_OUTPUT_HEADER_LEN		8
 
 /* flags */
@@ -49,6 +65,26 @@
  * requests. The FW becomes ready after sending the report descriptor.
  */
 #define SPI_HID_READY	0
+/*
+ * refresh_in_progress is set to true while the refresh_device worker
+ * thread is destroying and recreating the hidraw device. When this flag
+ * is set to true, input reports are dropped.
+ */
+#define SPI_HID_REFRESH_IN_PROGRESS	1
+/*
+ * reset_pending indicates that the device is being reset. When this flag
+ * is set to true, garbage interrupts triggered during reset will be
+ * dropped and will not cause error handling.
+ */
+#define SPI_HID_RESET_PENDING	2
+#define SPI_HID_RESET_RESPONSE	3
+#define SPI_HID_CREATE_DEVICE	4
+#define SPI_HID_ERROR	5
+/*
+ * started is set while the HID device is open, i.e. between ll_open and
+ * ll_close. Input reports are only forwarded while this flag is set.
+ */
+#define SPI_HID_STARTED	6
 
 /* Raw input buffer with data from the bus */
 struct spi_hid_input_buf {
@@ -57,6 +93,22 @@ struct spi_hid_input_buf {
 	u8 content[];
 };
 
+/* Processed data from input report header */
+struct spi_hid_input_header {
+	u8 version;
+	u16 report_length;
+	u8 last_fragment_flag;
+	u8 sync_const;
+};
+
+/* Processed data from an input report */
+struct spi_hid_input_report {
+	u8 report_type;
+	u16 content_length;
+	u8 content_id;
+	u8 *content;
+};
+
 /* Raw output report buffer to be put on the bus */
 struct spi_hid_output_buf {
 	u8 header[SPI_HID_OUTPUT_HEADER_LEN];
@@ -114,6 +166,9 @@ struct spi_hid {
 	struct spi_device	*spi;	/* spi device. */
 	struct hid_device	*hid;	/* pointer to corresponding HID dev. */
 
+	struct spi_transfer	input_transfer[2];	/* Transfer buffer for read and write. */
+	struct spi_message	input_message;	/* used to execute a sequence of spi transfers. */
+
 	struct spihid_ops	*ops;
 	struct spi_hid_conf	*conf;
 
@@ -124,6 +179,10 @@ struct spi_hid {
 
 	u16 response_length;
 	u16 bufsize;
+	/* Response type awaited by a sync request, 0 if none. Protected by io_lock. */
+	u8 expected_response;
+	/* Content ID of that request. Protected by io_lock. */
+	u8 expected_content_id;
 
 	enum hidspi_power_state power_state;
 
@@ -131,8 +190,28 @@ struct spi_hid {
 
 	unsigned long flags;	/* device flags. */
 
-	/* Control lock to make sure one output transaction at a time. */
+	struct work_struct reset_work;
+
+	/*
+	 * Serializes request/response transactions: held from sending a
+	 * request until its response in shid->response has been consumed.
+	 * Taken before io_lock.
+	 */
 	struct mutex output_lock;
+	/* Power lock to make sure one power state change at a time. */
+	struct mutex power_lock;
+	/*
+	 * Serializes SPI bus transfers (output writes vs. IRQ-thread reads) and
+	 * protects shid->expected_response, updates to shid->desc and
+	 * publication of shid->hid. Held only briefly and never while waiting
+	 * for a response, since the IRQ thread needs it to deliver that
+	 * response. Nests inside output_lock.
+	 *
+	 * It does not keep the hid_device alive: the IRQ must be quiesced with
+	 * disable_irq() before the hid_device is destroyed.
+	 */
+	struct mutex io_lock;
+
 	struct completion output_done;
 
 	u32 report_descriptor_crc32;	/* HID report descriptor crc32 checksum. */
@@ -142,10 +221,67 @@ struct spi_hid {
 	u32 bus_error_count;
 	int bus_last_error;
 	u32 dir_count;	/* device initiated reset count. */
+
+	/* DMA-safe transfer buffers */
+	u8 read_approval_header[SPI_HID_READ_APPROVAL_LEN] ____cacheline_aligned;
+	u8 read_approval_body[SPI_HID_READ_APPROVAL_LEN];
 };
 
 static struct hid_ll_driver spi_hid_ll_driver;
 
+static void spi_hid_populate_read_approvals(const struct spi_hid_conf *conf,
+					    u8 *header_buf, u8 *body_buf)
+{
+	header_buf[0] = conf->read_opcode;
+	put_unaligned_be24(conf->input_report_header_address, &header_buf[1]);
+	header_buf[4] = SPI_HID_READ_APPROVAL_CONSTANT;
+
+	body_buf[0] = conf->read_opcode;
+	put_unaligned_be24(conf->input_report_body_address, &body_buf[1]);
+	body_buf[4] = SPI_HID_READ_APPROVAL_CONSTANT;
+}
+
+static void spi_hid_parse_dev_desc(const struct hidspi_dev_descriptor *raw,
+				   struct spi_hid_device_descriptor *desc)
+{
+	desc->hid_version = le16_to_cpu(raw->bcd_ver);
+	desc->report_descriptor_length = le16_to_cpu(raw->rep_desc_len);
+	desc->max_input_length = le16_to_cpu(raw->max_input_len);
+	desc->max_output_length = le16_to_cpu(raw->max_output_len);
+
+	/* FIXME: multi-fragment not supported, field below not used */
+	desc->max_fragment_length = le16_to_cpu(raw->max_frag_len);
+
+	desc->vendor_id = le16_to_cpu(raw->vendor_id);
+	desc->product_id = le16_to_cpu(raw->product_id);
+	desc->version_id = le16_to_cpu(raw->version_id);
+	desc->no_output_report_ack = le16_to_cpu(raw->flags) & BIT(0);
+}
+
+static void spi_hid_populate_input_header(const u8 *buf,
+					  struct spi_hid_input_header *header)
+{
+	header->version            = buf[0] & 0xf;
+	header->report_length      = (get_unaligned_le16(&buf[1]) & 0x3fff) * 4;
+	header->last_fragment_flag = (buf[2] & 0x40) >> 6;
+	header->sync_const         = buf[3];
+}
+
+static void spi_hid_populate_input_body(const u8 *buf,
+					struct spi_hid_input_report *body)
+{
+	body->report_type = buf[0];
+	body->content_length = get_unaligned_le16(&buf[1]);
+	body->content_id = buf[3];
+}
+
+static void spi_hid_input_report_prepare(struct spi_hid_input_buf *buf,
+					 struct spi_hid_input_report *report)
+{
+	spi_hid_populate_input_body(buf->body, report);
+	report->content = buf->content;
+}
+
 static void spi_hid_populate_output_header(u8 *buf,
 					   const struct spi_hid_conf *conf,
 					   const struct spi_hid_output_report *report)
@@ -157,6 +293,33 @@ static void spi_hid_populate_output_header(u8 *buf,
 	buf[7] = report->content_id;
 }
 
+static int spi_hid_input_sync(struct spi_hid *shid, void *buf, u16 length,
+			      bool is_header)
+{
+	int error;
+
+	shid->input_transfer[0].tx_buf = is_header ?
+					 shid->read_approval_header :
+					 shid->read_approval_body;
+	shid->input_transfer[0].len = SPI_HID_READ_APPROVAL_LEN;
+
+	shid->input_transfer[1].rx_buf = buf;
+	shid->input_transfer[1].len = length;
+
+	spi_message_init_with_transfers(&shid->input_message,
+					shid->input_transfer, 2);
+
+	error = spi_sync(shid->spi, &shid->input_message);
+	if (error) {
+		dev_err(&shid->spi->dev, "Error starting sync transfer: %d\n", error);
+		shid->bus_error_count++;
+		shid->bus_last_error = error;
+		return error;
+	}
+
+	return 0;
+}
+
 static int spi_hid_output(struct spi_hid *shid, const void *buf, u16 length)
 {
 	int error;
@@ -187,17 +350,94 @@ static const char *spi_hid_power_mode_string(enum hidspi_power_state power_state
 
 static void spi_hid_stop_hid(struct spi_hid *shid)
 {
-	struct hid_device *hid = shid->hid;
+	struct hid_device *hid;
 
-	shid->hid = NULL;
-	clear_bit(SPI_HID_READY, &shid->flags);
+	scoped_guard(mutex, &shid->io_lock) {
+		hid = shid->hid;
+		shid->hid = NULL;
+		clear_bit(SPI_HID_READY, &shid->flags);
+	}
 
 	if (hid)
 		hid_destroy_device(hid);
 }
 
+static void spi_hid_error_handler(struct spi_hid *shid)
+{
+	struct device *dev = &shid->spi->dev;
+	int error;
+
+	guard(mutex)(&shid->power_lock);
+	if (shid->power_state == HIDSPI_OFF)
+		return;
+
+	disable_irq(shid->spi->irq);
+
+	if (shid->reset_attempts++ >= SPI_HID_MAX_RESET_ATTEMPTS) {
+		dev_err(dev, "unresponsive device, aborting\n");
+		spi_hid_stop_hid(shid);
+		shid->ops->assert_reset(shid->ops);
+		error = shid->ops->power_down(shid->ops);
+		if (error) {
+			dev_err(dev, "failed to disable regulator\n");
+			shid->regulator_error_count++;
+			shid->regulator_last_error = error;
+		}
+		shid->power_state = HIDSPI_OFF;
+		/* Dead device: leave the IRQ permanently disabled. */
+		return;
+	}
+
+	clear_bit(SPI_HID_READY, &shid->flags);
+	set_bit(SPI_HID_RESET_PENDING, &shid->flags);
+
+	shid->ops->assert_reset(shid->ops);
+
+	shid->power_state = HIDSPI_OFF;
+
+	/*
+	 * We want to cancel pending reset work as the device is being reset
+	 * to recover from an error. cancel_work_sync will put us in a deadlock
+	 * because this function is scheduled in 'reset_work' and we should
+	 * avoid waiting for itself.
+	 */
+	cancel_work(&shid->reset_work);
+
+	shid->ops->sleep_minimal_reset_delay(shid->ops);
+
+	shid->power_state = HIDSPI_ON;
+
+	shid->ops->deassert_reset(shid->ops);
+
+	enable_irq(shid->spi->irq);
+}
+
+/* Map an output report type to the response type the device sends back. */
+static u8 spi_hid_response_type(u8 report_type)
+{
+	switch (report_type) {
+	case DEVICE_DESCRIPTOR:
+		return DEVICE_DESCRIPTOR_RESPONSE;
+	case REPORT_DESCRIPTOR:
+		return REPORT_DESCRIPTOR_RESPONSE;
+	case SET_FEATURE:
+		return SET_FEATURE_RESPONSE;
+	case GET_FEATURE:
+		return GET_FEATURE_RESPONSE;
+	case OUTPUT_REPORT:
+		return OUTPUT_REPORT_RESPONSE;
+	default:
+		return 0;
+	}
+}
+
+/*
+ * If expected_response is non-zero, arm the response tracking under io_lock
+ * before the write so that the IRQ path only accepts a response of that type.
+ */
 static int __spi_hid_send_output_report(struct spi_hid *shid,
-					struct spi_hid_output_report *report)
+					struct spi_hid_output_report *report,
+					u8 expected_response)
 {
 	struct spi_hid_output_buf *buf = shid->output;
 	struct device *dev = &shid->spi->dev;
@@ -206,6 +446,14 @@ static int __spi_hid_send_output_report(struct spi_hid *shid,
 	u8 padding;
 	int error;
 
+	lockdep_assert_held(&shid->output_lock);
+
+	/* While not READY, only (re)init descriptor requests may be sent. */
+	if (report->report_type != DEVICE_DESCRIPTOR &&
+	    report->report_type != REPORT_DESCRIPTOR &&
+	    !test_bit(SPI_HID_READY, &shid->flags))
+		return -ENODEV;
+
 	if (report->content_length > shid->desc.max_output_length ||
 	    report->content_length > shid->bufsize) {
 		dev_err(dev, "Output report too big, content_length 0x%x\n",
@@ -213,6 +461,7 @@ static int __spi_hid_send_output_report(struct spi_hid *shid,
 		return -E2BIG;
 	}
 
+	guard(mutex)(&shid->io_lock);
 	spi_hid_populate_output_header(buf->header, shid->conf, report);
 
 	if (report->content_length)
@@ -223,9 +472,17 @@ static int __spi_hid_send_output_report(struct spi_hid *shid,
 	padding = padded_length - report_length;
 	memset(&buf->content[report->content_length], 0, padding);
 
+	if (expected_response) {
+		reinit_completion(&shid->output_done);
+		shid->expected_response = expected_response;
+		shid->expected_content_id = report->content_id;
+	}
+
 	error = spi_hid_output(shid, buf, padded_length);
-	if (error)
+	if (error) {
 		dev_err(dev, "Failed output transfer: %d\n", error);
+		shid->expected_response = 0;
+	}
 
 	return error;
 }
@@ -234,7 +491,7 @@ static int spi_hid_send_output_report(struct spi_hid *shid,
 				      struct spi_hid_output_report *report)
 {
 	guard(mutex)(&shid->output_lock);
-	return __spi_hid_send_output_report(shid, report);
+	return __spi_hid_send_output_report(shid, report, 0);
 }
 
 static int __spi_hid_sync_request(struct spi_hid *shid,
@@ -243,22 +500,26 @@ static int __spi_hid_sync_request(struct spi_hid *shid,
 	struct device *dev = &shid->spi->dev;
 	int error;
 
-	reinit_completion(&shid->output_done);
-
-	error = __spi_hid_send_output_report(shid, report);
+	error = __spi_hid_send_output_report(shid, report,
+					     spi_hid_response_type(report->report_type));
 	if (error)
 		return error;
 
-	error = wait_for_completion_interruptible_timeout(&shid->output_done,
-							  msecs_to_jiffies(SPI_HID_RESP_TIMEOUT));
-	if (error == 0) {
-		dev_err(dev, "Response timed out\n");
-		return -ETIMEDOUT;
+	if (wait_for_completion_timeout(&shid->output_done,
+					msecs_to_jiffies(SPI_HID_RESP_TIMEOUT)))
+		return 0;
+
+	/* Drop late responses and block new HID requests until the reset. */
+	scoped_guard(mutex, &shid->io_lock) {
+		shid->expected_response = 0;
+		clear_bit(SPI_HID_READY, &shid->flags);
 	}
-	if (error < 0)
-		return error;
 
-	return 0;
+	dev_err(dev, "Response timed out\n");
+	/* The device may still answer later: resync it with a reset. */
+	set_bit(SPI_HID_ERROR, &shid->flags);
+	schedule_work(&shid->reset_work);
+	return -ETIMEDOUT;
 }
 
 static int spi_hid_sync_request(struct spi_hid *shid,
@@ -268,9 +529,174 @@ static int spi_hid_sync_request(struct spi_hid *shid,
 	return __spi_hid_sync_request(shid, report);
 }
 
+/*
+ * Handle the reset response from the FW by sending a request for the device
+ * descriptor.
+ */
+static void spi_hid_reset_response(struct spi_hid *shid)
+{
+	struct device *dev = &shid->spi->dev;
+	struct spi_hid_output_report report = {
+		.report_type = DEVICE_DESCRIPTOR,
+		.content_length = 0x0,
+		.content_id = SPI_HID_OUTPUT_REPORT_CONTENT_ID_DESC_REQUEST,
+		.content = NULL,
+	};
+	int error;
+
+	if (test_bit(SPI_HID_READY, &shid->flags)) {
+		dev_err(dev, "Spontaneous FW reset!\n");
+		clear_bit(SPI_HID_READY, &shid->flags);
+		shid->dir_count++;
+	}
+
+	if (shid->power_state == HIDSPI_OFF)
+		return;
+
+	error = spi_hid_sync_request(shid, &report);
+	if (error) {
+		dev_WARN_ONCE(dev, true,
+			      "Failed to send device descriptor request: %d\n", error);
+		set_bit(SPI_HID_ERROR, &shid->flags);
+		schedule_work(&shid->reset_work);
+	}
+}
+
+static int spi_hid_input_report_handler(struct spi_hid *shid,
+					struct spi_hid_input_buf *buf)
+{
+	struct device *dev = &shid->spi->dev;
+	struct hid_device *hid;
+	struct spi_hid_input_report r;
+	int error = 0;
+
+	scoped_guard(mutex, &shid->io_lock) {
+		if (!test_bit(SPI_HID_READY, &shid->flags) ||
+		    !test_bit(SPI_HID_STARTED, &shid->flags) ||
+		    test_bit(SPI_HID_REFRESH_IN_PROGRESS, &shid->flags) || !shid->hid) {
+			dev_dbg(dev, "HID not ready (flags 0x%lx), dropping input report\n",
+				shid->flags);
+			return 0;
+		}
+
+		hid = shid->hid;
+		spi_hid_input_report_prepare(buf, &r);
+	}
+
+	/*
+	 * Safe after dropping io_lock: hid is only freed, and bufsize only
+	 * changed, after disable_irq(), which waits for this handler. The
+	 * buffer is the content ID byte at r.content - 1 plus bufsize bytes.
+	 */
+	error = hid_safe_input_report(hid, HID_INPUT_REPORT, r.content - 1,
+				      shid->bufsize + 1,
+				      r.content_length + 1, 1);
+
+	if (error == -ENODEV || error == -EBUSY) {
+		dev_err(dev, "ignoring report --> %d\n", error);
+		return 0;
+	} else if (error) {
+		dev_err(dev, "Bad input report: %d\n", error);
+	}
+
+	return error;
+}
+
+/*
+ * Validate a device descriptor response and, if valid, publish it to
+ * shid->desc and request device creation.
+ */
+static int spi_hid_dev_desc_response(struct spi_hid *shid,
+				     struct spi_hid_input_report *body)
+{
+	struct hidspi_dev_descriptor *raw =
+		(struct hidspi_dev_descriptor *)shid->input->content;
+	struct device *dev = &shid->spi->dev;
+
+	/* Validate device descriptor length before parsing */
+	if (body->content_length != HIDSPI_DEVICE_DESCRIPTOR_SIZE) {
+		dev_err(dev, "Invalid content length %d, expected %zu\n",
+			body->content_length, HIDSPI_DEVICE_DESCRIPTOR_SIZE);
+		return -EPROTO;
+	}
+
+	if (le16_to_cpu(raw->dev_desc_len) != HIDSPI_DEVICE_DESCRIPTOR_SIZE) {
+		dev_err(dev, "Invalid wDeviceDescLength %d, expected %zu\n",
+			le16_to_cpu(raw->dev_desc_len),
+			HIDSPI_DEVICE_DESCRIPTOR_SIZE);
+		return -EPROTO;
+	}
+
+	if (le16_to_cpu(raw->bcd_ver) != SPI_HID_SUPPORTED_VERSION) {
+		dev_err(dev, "Unsupported device descriptor version %4x\n",
+			le16_to_cpu(raw->bcd_ver));
+		return -EPROTONOSUPPORT;
+	}
+
+	/* Fail before reset_attempts is cleared so resets stay bounded. */
+	if (!le16_to_cpu(raw->rep_desc_len) ||
+	    le16_to_cpu(raw->rep_desc_len) > HID_MAX_DESCRIPTOR_SIZE) {
+		dev_err(dev, "Invalid wReportDescLength %d, max %d\n",
+			le16_to_cpu(raw->rep_desc_len),
+			HID_MAX_DESCRIPTOR_SIZE);
+		return -EPROTO;
+	}
+
+	spi_hid_parse_dev_desc(raw, &shid->desc);
+
+	/* Reset attempts at every device descriptor fetch */
+	shid->reset_attempts = 0;
+	set_bit(SPI_HID_CREATE_DEVICE, &shid->flags);
+	schedule_work(&shid->reset_work);
+
+	return 0;
+}
+
+static int spi_hid_response_handler(struct spi_hid *shid,
+				    struct spi_hid_input_report *body)
+{
+	int error = 0;
+
+	guard(mutex)(&shid->io_lock);
+
+	/*
+	 * Drop late or unsolicited responses. With no request IDs, also match
+	 * the report ID for GET_FEATURE so it can't get another report's data.
+	 */
+	if (!shid->expected_response ||
+	    body->report_type != shid->expected_response ||
+	    (body->report_type == GET_FEATURE_RESPONSE &&
+	     body->content_id != shid->expected_content_id)) {
+		dev_err(&shid->spi->dev, "Unexpected response report 0x%x\n",
+			body->report_type);
+		return 0;
+	}
+	shid->expected_response = 0;
+
+	shid->response_length = body->content_length;
+	if (body->report_type == REPORT_DESCRIPTOR_RESPONSE ||
+	    body->report_type == GET_FEATURE_RESPONSE) {
+		memcpy(shid->response->body, shid->input->body,
+		       sizeof(shid->input->body));
+		memcpy(shid->response->content, shid->input->content,
+		       body->content_length);
+	} else if (body->report_type == DEVICE_DESCRIPTOR_RESPONSE) {
+		/*
+		 * Publish the descriptor before complete() so reset_work never
+		 * sees a partially written shid->desc.
+		 */
+		error = spi_hid_dev_desc_response(shid, body);
+	}
+
+	/* Wake the waiter even on error so it doesn't wait for the timeout. */
+	complete(&shid->output_done);
+
+	return error;
+}
+
 /*
  * This function returns the length of the report descriptor, or a negative
- * error code if something went wrong.
+ * error code if something went wrong. Caller must hold output_lock.
  */
 static int spi_hid_report_descriptor_request(struct spi_hid *shid)
 {
@@ -283,10 +709,14 @@ static int spi_hid_report_descriptor_request(struct spi_hid *shid)
 	};
 	int ret;
 
-	ret =  spi_hid_sync_request(shid, &report);
+	lockdep_assert_held(&shid->output_lock);
+
+	ret = __spi_hid_sync_request(shid, &report);
 	if (ret) {
 		dev_err(dev,
 			"Expected report descriptor not received: %d\n", ret);
+		set_bit(SPI_HID_ERROR, &shid->flags);
+		schedule_work(&shid->reset_work);
 		return ret;
 	}
 
@@ -325,7 +755,9 @@ static int spi_hid_create_device(struct spi_hid *shid)
 		 hid->vendor, hid->product);
 	strscpy(hid->phys, dev_name(&shid->spi->dev), sizeof(hid->phys));
 
-	shid->hid = hid;
+	scoped_guard(mutex, &shid->io_lock) {
+		shid->hid = hid;
+	}
 
 	error = hid_add_device(hid);
 	if (error) {
@@ -334,13 +766,204 @@ static int spi_hid_create_device(struct spi_hid *shid)
 		 * We likely got here because report descriptor request timed
 		 * out. Let's disconnect and destroy the hid_device structure.
 		 */
-		spi_hid_stop_hid(shid);
+		scoped_guard(disable_irq, &shid->spi->irq)
+			spi_hid_stop_hid(shid);
 		return error;
 	}
 
 	return 0;
 }
 
+static void spi_hid_refresh_device(struct spi_hid *shid)
+{
+	struct device *dev = &shid->spi->dev;
+	u32 new_crc32 = 0;
+	int error = 0;
+
+	/* Keep shid->response stable until the CRC is computed. */
+	scoped_guard(mutex, &shid->output_lock) {
+		error = spi_hid_report_descriptor_request(shid);
+		if (error < 0) {
+			dev_err(dev,
+				"%s: failed report descriptor request: %d\n",
+				__func__, error);
+			return;
+		}
+		new_crc32 = crc32_le(0, (unsigned char const *)shid->response->content,
+				     (size_t)error);
+	}
+
+	/* Same report descriptor, so no need to create a new hid device. */
+	if (new_crc32 == shid->report_descriptor_crc32) {
+		set_bit(SPI_HID_READY, &shid->flags);
+		return;
+	}
+
+	shid->report_descriptor_crc32 = new_crc32;
+
+	set_bit(SPI_HID_REFRESH_IN_PROGRESS, &shid->flags);
+
+	/*
+	 * Disable the IRQ only for tear-down: creation needs it to receive
+	 * the report descriptor. REFRESH_IN_PROGRESS drops input meanwhile.
+	 */
+	scoped_guard(disable_irq, &shid->spi->irq) {
+		spi_hid_stop_hid(shid);
+	}
+
+	error = spi_hid_create_device(shid);
+	clear_bit(SPI_HID_REFRESH_IN_PROGRESS, &shid->flags);
+
+	if (error)
+		dev_err(dev, "%s: Failed to create hid device: %d\n", __func__, error);
+}
+
+static void spi_hid_reset_work(struct work_struct *work)
+{
+	struct spi_hid *shid =
+		container_of(work, struct spi_hid, reset_work);
+	struct device *dev = &shid->spi->dev;
+	int error = 0;
+	bool resched = false;
+
+	if (test_and_clear_bit(SPI_HID_RESET_RESPONSE, &shid->flags)) {
+		spi_hid_reset_response(shid);
+		resched = true;
+	} else if (test_and_clear_bit(SPI_HID_CREATE_DEVICE, &shid->flags)) {
+		guard(mutex)(&shid->power_lock);
+		if (shid->power_state != HIDSPI_OFF) {
+			if (!shid->hid) {
+				error = spi_hid_create_device(shid);
+				if (error) {
+					dev_err(dev, "%s: Failed to create hid device: %d\n",
+						__func__, error);
+				}
+			} else {
+				spi_hid_refresh_device(shid);
+			}
+		} else {
+			dev_err(dev, "%s: Powered off, returning\n", __func__);
+		}
+		resched = true;
+	} else if (test_and_clear_bit(SPI_HID_ERROR, &shid->flags)) {
+		spi_hid_error_handler(shid);
+	}
+
+	/*
+	 * If other flags are still pending, safely reschedule ourselves
+	 * to process them in the next workqueue cycle.
+	 */
+	if (resched && (shid->flags & (BIT(SPI_HID_RESET_RESPONSE) |
+				       BIT(SPI_HID_CREATE_DEVICE) |
+				       BIT(SPI_HID_ERROR)))) {
+		schedule_work(&shid->reset_work);
+	}
+}
+
+static int spi_hid_process_input_report(struct spi_hid *shid,
+					struct spi_hid_input_buf *buf)
+{
+	struct spi_hid_input_header header;
+	struct spi_hid_input_report body;
+	struct device *dev = &shid->spi->dev;
+
+	spi_hid_populate_input_header(buf->header, &header);
+	spi_hid_input_report_prepare(buf, &body);
+
+	if (HIDSPI_INPUT_BODY_SIZE(body.content_length) > header.report_length) {
+		dev_err(dev, "Bad body length %zu > %u\n",
+			HIDSPI_INPUT_BODY_SIZE(body.content_length),
+			header.report_length);
+		return -EPROTO;
+	}
+
+	switch (body.report_type) {
+	case DATA:
+		return spi_hid_input_report_handler(shid, buf);
+	case RESET_RESPONSE:
+		clear_bit(SPI_HID_RESET_PENDING, &shid->flags);
+		set_bit(SPI_HID_RESET_RESPONSE, &shid->flags);
+		schedule_work(&shid->reset_work);
+		break;
+	case DEVICE_DESCRIPTOR_RESPONSE:
+		return spi_hid_response_handler(shid, &body);
+	case OUTPUT_REPORT_RESPONSE:
+		if (shid->desc.no_output_report_ack) {
+			dev_err(dev, "Unexpected output report response\n");
+			break;
+		}
+		fallthrough;
+	case GET_FEATURE_RESPONSE:
+	case SET_FEATURE_RESPONSE:
+	case REPORT_DESCRIPTOR_RESPONSE:
+		spi_hid_response_handler(shid, &body);
+		break;
+	/*
+	 * FIXME: sending GET_INPUT and COMMAND reports not supported, thus
+	 * throw away responses to those, they should never come.
+	 */
+	case GET_INPUT_REPORT_RESPONSE:
+	case COMMAND_RESPONSE:
+		dev_err(dev, "Not a supported report type: 0x%x\n",
+			body.report_type);
+		break;
+	default:
+		dev_err(dev, "Unknown input report: 0x%x\n", body.report_type);
+		return -EPROTO;
+	}
+
+	return 0;
+}
+
+static int spi_hid_bus_validate_header(struct spi_hid *shid,
+				       struct spi_hid_input_header *header)
+{
+	struct device *dev = &shid->spi->dev;
+	/*
+	 * Body capacity of shid->input, matching spi_hid_alloc_buffers().
+	 * shid->bufsize only changes with the IRQ disabled, so it is stable here.
+	 */
+	u32 max_body = round_up(sizeof(shid->input->header) +
+				sizeof(shid->input->body) + shid->bufsize, 4) -
+		       sizeof(shid->input->header);
+
+	if (header->version != SPI_HID_INPUT_HEADER_VERSION) {
+		dev_err(dev, "Unknown input report version (v 0x%x)\n",
+			header->version);
+		return -EINVAL;
+	}
+
+	/*
+	 * report_length is device-provided and is used as the size of the body
+	 * transfer, so it must never exceed the input buffer.
+	 */
+	if (header->report_length > max_body) {
+		dev_err(dev, "Input report too big: %u > %u\n",
+			header->report_length, max_body);
+		return -EMSGSIZE;
+	}
+
+	if (shid->desc.max_input_length != 0 &&
+	    header->report_length > shid->desc.max_input_length) {
+		dev_err(dev, "Input report body size %u > max expected of %u\n",
+			header->report_length, shid->desc.max_input_length);
+		return -EMSGSIZE;
+	}
+
+	if (header->last_fragment_flag != 1) {
+		dev_err(dev, "Multi-fragment reports not supported\n");
+		return -EOPNOTSUPP;
+	}
+
+	if (header->sync_const != SPI_HID_INPUT_HEADER_SYNC_BYTE) {
+		dev_err(dev, "Invalid input report sync constant (0x%x)\n",
+			header->sync_const);
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
 static int spi_hid_get_request(struct spi_hid *shid, u8 content_id,
 			       u8 *buf, size_t len)
 {
@@ -365,6 +988,8 @@ static int spi_hid_get_request(struct spi_hid *shid, u8 content_id,
 		dev_err(dev,
 			"Expected get request response not received! Error %d\n",
 			error);
+		set_bit(SPI_HID_ERROR, &shid->flags);
+		schedule_work(&shid->reset_work);
 		return error;
 	}
 
@@ -397,9 +1022,81 @@ static int spi_hid_set_request(struct spi_hid *shid, u8 *arg_buf, u16 arg_len,
 	return spi_hid_sync_request(shid, &report);
 }
 
-/* This is a placeholder. Will be implemented in the next patch. */
+/* Schedule a reset to recover from an error in the IRQ handler. */
+static irqreturn_t spi_hid_irq_error(struct spi_hid *shid)
+{
+	set_bit(SPI_HID_ERROR, &shid->flags);
+	schedule_work(&shid->reset_work);
+
+	return IRQ_HANDLED;
+}
+
 static irqreturn_t spi_hid_dev_irq(int irq, void *_shid)
 {
+	struct spi_hid *shid = _shid;
+	struct device *dev = &shid->spi->dev;
+	struct spi_hid_input_header header;
+	int error = 0;
+
+	scoped_guard(mutex, &shid->io_lock) {
+		if (shid->power_state == HIDSPI_OFF) {
+			dev_warn(dev, "Device is off, ignoring interrupt\n");
+			return IRQ_NONE;
+		}
+
+		error = spi_hid_input_sync(shid, shid->input->header,
+					   sizeof(shid->input->header), true);
+		if (error) {
+			dev_err(dev, "Failed to transfer header: %d\n", error);
+			return spi_hid_irq_error(shid);
+		}
+
+		if (shid->input_message.status < 0) {
+			dev_warn(dev, "Error reading header: %d\n",
+				 shid->input_message.status);
+			shid->bus_error_count++;
+			shid->bus_last_error = shid->input_message.status;
+			return spi_hid_irq_error(shid);
+		}
+
+		spi_hid_populate_input_header(shid->input->header, &header);
+
+		error = spi_hid_bus_validate_header(shid, &header);
+		if (error) {
+			if (!test_bit(SPI_HID_RESET_PENDING, &shid->flags)) {
+				dev_err(dev, "Failed to validate header: %d\n", error);
+				print_hex_dump(KERN_ERR, "spi_hid: header buffer: ",
+					       DUMP_PREFIX_NONE, 16, 1, shid->input->header,
+					       sizeof(shid->input->header), false);
+				shid->bus_error_count++;
+				shid->bus_last_error = error;
+				return spi_hid_irq_error(shid);
+			}
+			return IRQ_HANDLED;
+		}
+
+		error = spi_hid_input_sync(shid, shid->input->body, header.report_length,
+					   false);
+		if (error) {
+			dev_err(dev, "Failed to transfer body: %d\n", error);
+			return spi_hid_irq_error(shid);
+		}
+
+		if (shid->input_message.status < 0) {
+			dev_warn(dev, "Error reading body: %d\n",
+				 shid->input_message.status);
+			shid->bus_error_count++;
+			shid->bus_last_error = shid->input_message.status;
+			return spi_hid_irq_error(shid);
+		}
+	}
+
+	error = spi_hid_process_input_report(shid, shid->input);
+	if (error) {
+		dev_err(dev, "Failed to process input report: %d\n", error);
+		return spi_hid_irq_error(shid);
+	}
+
 	return IRQ_HANDLED;
 }
 
@@ -496,6 +1193,10 @@ static void spi_hid_ll_stop(struct hid_device *hid)
 
 static int spi_hid_ll_open(struct hid_device *hid)
 {
+	struct spi_device *spi = hid->driver_data;
+	struct spi_hid *shid = spi_get_drvdata(spi);
+
+	set_bit(SPI_HID_STARTED, &shid->flags);
 	return 0;
 }
 
@@ -504,6 +1205,7 @@ static void spi_hid_ll_close(struct hid_device *hid)
 	struct spi_device *spi = hid->driver_data;
 	struct spi_hid *shid = spi_get_drvdata(spi);
 
+	clear_bit(SPI_HID_STARTED, &shid->flags);
 	shid->reset_attempts = 0;
 }
 
@@ -535,11 +1237,8 @@ static int spi_hid_ll_parse(struct hid_device *hid)
 		return -EINVAL;
 	}
 
-	if (rsize > shid->bufsize) {
-		error = spi_hid_alloc_buffers(shid, rsize);
-		if (error)
-			return error;
-	}
+	/* Keep shid->response stable until done. */
+	guard(mutex)(&shid->output_lock);
 
 	len = spi_hid_report_descriptor_request(shid);
 	if (len < 0) {
@@ -730,12 +1429,21 @@ int spi_hid_core_probe(struct spi_device *spi, struct spihid_ops *ops,
 	shid->power_state = HIDSPI_ON;
 	shid->ops = ops;
 	shid->conf = conf;
+	set_bit(SPI_HID_RESET_PENDING, &shid->flags);
 
 	spi_set_drvdata(spi, shid);
 
+	/* Using now populated conf let's pre-calculate the read approvals */
+	spi_hid_populate_read_approvals(shid->conf, shid->read_approval_header,
+					shid->read_approval_body);
+
 	mutex_init(&shid->output_lock);
+	mutex_init(&shid->power_lock);
+	mutex_init(&shid->io_lock);
 	init_completion(&shid->output_done);
 
+	INIT_WORK(&shid->reset_work, spi_hid_reset_work);
+
 	/*
 	 * we need to allocate the buffer without knowing the maximum
 	 * size of the reports. Let's use HID_MAX_DESCRIPTOR_SIZE, then we do the
@@ -760,7 +1468,7 @@ int spi_hid_core_probe(struct spi_device *spi, struct spihid_ops *ops,
 	shid->ops->sleep_minimal_reset_delay(shid->ops);
 
 	error = devm_request_threaded_irq(dev, spi->irq, NULL, spi_hid_dev_irq,
-					  IRQF_ONESHOT, dev_name(&spi->dev), shid);
+					  IRQF_ONESHOT | IRQF_NO_AUTOEN, dev_name(&spi->dev), shid);
 	if (error) {
 		dev_err(dev, "%s: unable to request threaded IRQ\n", __func__);
 		return error;
@@ -774,13 +1482,11 @@ int spi_hid_core_probe(struct spi_device *spi, struct spihid_ops *ops,
 
 	shid->ops->deassert_reset(shid->ops);
 
+	enable_irq(spi->irq);
+
 	dev_dbg(dev, "%s: d3 -> %s\n", __func__,
 		spi_hid_power_mode_string(shid->power_state));
 
-	error = spi_hid_create_device(shid);
-	if (error)
-		return error;
-
 	return 0;
 }
 EXPORT_SYMBOL_GPL(spi_hid_core_probe);
@@ -791,6 +1497,9 @@ void spi_hid_core_remove(struct spi_device *spi)
 	struct device *dev = &spi->dev;
 	int error;
 
+	disable_irq(spi->irq);
+	disable_work_sync(&shid->reset_work);
+
 	spi_hid_stop_hid(shid);
 
 	shid->ops->assert_reset(shid->ops);
Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
#12 lore raw

Re: [PATCH v5 05/11] HID: spi-hid: add HID SPI protocol implementation

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] hid: sleeping in atomic context via synchronous output_report - [High] spi: dma: unaligned DMA…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] hid: sleeping in atomic context via synchronous output_report
- [High] spi: dma: unaligned DMA transfer buffers cause memory corruption
- [High] hid: spi-hid: use-after-free of transport buffers on concurrent realloc
- [Medium] hid: spi-hid: SPI_HID_REFRESH_IN_PROGRESS breaks hid_device_io_start() during probe --

commit 4647f83267b811c038d6b541aee03ea186664da4
Author: Jingyuan Liang <[email protected]>
Subject: HID: spi-hid: add HID SPI protocol implementation

This commit adds a new driver for the HID over SPI protocol. It supports basic input and output reports but omits features like multi-fragment reports and power states.

Quoted 3 lines
drivers/hid/spi-hid/spi-hid-core.c +0 -0

[ ... ]

drivers/hid/spi-hid/spi-hid-core.c static void spi_hid_populate_output_header(u8 *buf, · line 306

Quoted 11 lines
drivers/hid/spi-hid/spi-hid-core.c +11 -0
+static int spi_hid_input_sync(struct spi_hid *shid, void *buf, u16 length,
+			      bool is_header)
+{
+	int error;
+
+	shid->input_transfer[0].tx_buf = is_header ?
+					 shid->read_approval_header :
+					 shid->read_approval_body;
+	shid->input_transfer[0].len = SPI_HID_READ_APPROVAL_LEN;
+
+	shid->input_transfer[1].rx_buf = buf;

[Severity: High]
Could this cause memory corruption on non-cache-coherent architectures?

The read_approval_body is a 5-byte array that immediately follows read_approval_header in struct spi_hid. Since it starts at an unaligned 5-byte offset, passing it as tx_buf to the DMA sync path violates architecture DMA API rules.

The rx_buf parameter also receives unaligned memory when buf is passed as shid->input->body later in the IRQ handler, which starts 4 bytes after the dynamically allocated input header. This can trigger CONFIG_DMA_API_DEBUG warnings or corrupt adjacent memory.

[ ... ]

drivers/hid/spi-hid/spi-hid-core.c

Quoted 12 lines
drivers/hid/spi-hid/spi-hid-core.c +12 -0
+static int __spi_hid_send_output_report(struct spi_hid *shid,
+					struct spi_hid_output_report *report,
+					u8 expected_response)
+{
+	struct spi_hid_output_buf *buf = shid->output;
+	struct device *dev = &shid->spi->dev;
+	u16 report_length;
+	u16 padded_length;
+	u8 padding;
+	int error;
+
+	lockdep_assert_held(&shid->output_lock);

[Severity: High]
Is there a potential use-after-free here if the output buffer is reallocated concurrently?

When the HID core calls spi_hid_ll_start() dynamically from userspace or BPF, it expands the transport buffers by calling spi_hid_alloc_buffers() if the report sizes have increased. This reallocation uses devm_krealloc() to free shid->output and replace it.

However, spi_hid_ll_start() does not appear to acquire shid->output_lock before reallocating. If a spontaneous device reset interrupt schedules reset_work on a global worker thread:

spi_hid_reset_work()
spi_hid_reset_response()

    spi_hid_sync_request()
      __spi_hid_send_output_report()

This path can execute concurrently, accessing the freed shid->output buffer under the lock, leading to a use-after-free when writing the header and payload.

[ ... ]

Quoted 5 lines
drivers/hid/spi-hid/spi-hid-core.c +5 -0
+static int spi_hid_send_output_report(struct spi_hid *shid,
+				      struct spi_hid_output_report *report)
+{
+	guard(mutex)(&shid->output_lock);
+	return __spi_hid_send_output_report(shid, report, 0);

drivers/hid/spi-hid/spi-hid-core.c

Quoted 10 lines
drivers/hid/spi-hid/spi-hid-core.c +10 -0
+}
+
+static int __spi_hid_sync_request(struct spi_hid *shid,
+				  struct spi_hid_output_report *report)
+{
+	struct device *dev = &shid->spi->dev;
+	int error;
+
+	error = __spi_hid_send_output_report(shid, report,
+					     spi_hid_response_type(report->report_type));

[Severity: High]
Does this sleeping lock violation trigger scheduling while atomic warnings?

According to the HID subsystem guidelines, the low-level output_report callback is used for high-throughput data on the interrupt channel and must be asynchronous.

When spi_hid_ll_output_report() calls spi_hid_send_output_report() or spi_hid_sync_request(), these helpers immediately acquire shid->output_lock (a sleeping mutex) and perform a synchronous, blocking SPI transfer. Since the callback can be invoked from atomic contexts (like updating LED states from an input event handler), sleeping here may cause panics.

[ ... ]

Quoted 6 lines
drivers/hid/spi-hid/spi-hid-core.c +6 -0
+static int spi_hid_input_report_handler(struct spi_hid *shid,
+					struct spi_hid_input_buf *buf)
+{
+	struct device *dev = &shid->spi->dev;
+	struct hid_device *hid;
+	struct spi_hid_input_report r;

drivers/hid/spi-hid/spi-hid-core.c static int spi_hid_sync_request(struct spi_hid *shid, · line 580

Quoted 10 lines
drivers/hid/spi-hid/spi-hid-core.c +10 -0
+	int error = 0;
+
+	scoped_guard(mutex, &shid->io_lock) {
+		if (!test_bit(SPI_HID_READY, &shid->flags) ||
+		    !test_bit(SPI_HID_STARTED, &shid->flags) ||
+		    test_bit(SPI_HID_REFRESH_IN_PROGRESS, &shid->flags) || !shid->hid) {
+			dev_dbg(dev, "HID not ready (flags 0x%lx), dropping input report\n",
+				shid->flags);
+			return 0;
+		}

[Severity: Medium]
Will this prevent drivers from receiving input reports during probe?

When spi_hid_refresh_device() is executed, it sets the SPI_HID_REFRESH_IN_PROGRESS flag before tearing down the old HID device and creating the new one:

spi_hid_refresh_device()
set_bit(SPI_HID_REFRESH_IN_PROGRESS, ...)
spi_hid_create_device()

    hid_add_device()
      -> driver probe()

During this entire time, the input report handler will drop all incoming reports. This conflicts with the HID core API contract, which explicitly allows a driver to receive inputs during its probe phase by calling hid_device_io_start(). A driver that requires a firmware handshake or data during probe could hang or fail upon device refresh.

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

Re: [PATCH v5 05/11] HID: spi-hid: add HID SPI protocol implementation

Quoted 40 lines · On Fri, Oct 9, 2026 at 3:42 PM <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] hid: sleeping in atomic context via synchronous output_report
- [High] spi: dma: unaligned DMA transfer buffers cause memory corruption
- [High] hid: spi-hid: use-after-free of transport buffers on concurrent realloc
- [Medium] hid: spi-hid: SPI_HID_REFRESH_IN_PROGRESS breaks hid_device_io_start() during probe --

commit 4647f83267b811c038d6b541aee03ea186664da4
Author: Jingyuan Liang <[email protected]>
Subject: HID: spi-hid: add HID SPI protocol implementation

This commit adds a new driver for the HID over SPI protocol. It supports basic input and output reports but omits features like multi-fragment reports and power states.

Quoted 3 lines
drivers/hid/spi-hid/spi-hid-core.c +0 -0

[ ... ]

drivers/hid/spi-hid/spi-hid-core.c static void spi_hid_populate_output_header(u8 *buf, · line 306

Quoted 11 lines
drivers/hid/spi-hid/spi-hid-core.c +11 -0
+static int spi_hid_input_sync(struct spi_hid *shid, void *buf, u16 length,
+                           bool is_header)
+{
+     int error;
+
+     shid->input_transfer[0].tx_buf = is_header ?
+                                      shid->read_approval_header :
+                                      shid->read_approval_body;
+     shid->input_transfer[0].len = SPI_HID_READ_APPROVAL_LEN;
+
+     shid->input_transfer[1].rx_buf = buf;

[Severity: High]
Could this cause memory corruption on non-cache-coherent architectures?

The read_approval_body is a 5-byte array that immediately follows read_approval_header in struct spi_hid. Since it starts at an unaligned 5-byte offset, passing it as tx_buf to the DMA sync path violates architecture DMA API rules.

Quoted 4 lines

The rx_buf parameter also receives unaligned memory when buf is passed as shid->input->body later in the IRQ handler, which starts 4 bytes after the dynamically allocated input header. This can trigger CONFIG_DMA_API_DEBUG warnings or corrupt adjacent memory.

I don't think so. The read approvals are TX-only and never change after probe, and the RX body only shares a cacheline with the input header, which isn't touched during the body transfer.

Quoted 28 lines

[ ... ]

drivers/hid/spi-hid/spi-hid-core.c

Quoted 12 lines
drivers/hid/spi-hid/spi-hid-core.c +12 -0
+static int __spi_hid_send_output_report(struct spi_hid *shid,
+                                     struct spi_hid_output_report *report,
+                                     u8 expected_response)
+{
+     struct spi_hid_output_buf *buf = shid->output;
+     struct device *dev = &shid->spi->dev;
+     u16 report_length;
+     u16 padded_length;
+     u8 padding;
+     int error;
+
+     lockdep_assert_held(&shid->output_lock);

[Severity: High]
Is there a potential use-after-free here if the output buffer is reallocated concurrently?

When the HID core calls spi_hid_ll_start() dynamically from userspace or BPF, it expands the transport buffers by calling spi_hid_alloc_buffers() if the report sizes have increased. This reallocation uses devm_krealloc() to free shid->output and replace it.

However, spi_hid_ll_start() does not appear to acquire shid->output_lock before reallocating. If a spontaneous device reset interrupt schedules reset_work on a global worker thread:

Quoted 8 lines

spi_hid_reset_work()
spi_hid_reset_response()

    spi_hid_sync_request()
      __spi_hid_send_output_report()

This path can execute concurrently, accessing the freed shid->output buffer under the lock, leading to a use-after-free when writing the header and payload.

Thank you. I will fix it in v6.

Quoted 25 lines

[ ... ]

Quoted 5 lines
drivers/hid/spi-hid/spi-hid-core.c +5 -0
+static int spi_hid_send_output_report(struct spi_hid *shid,
+                                   struct spi_hid_output_report *report)
+{
+     guard(mutex)(&shid->output_lock);
+     return __spi_hid_send_output_report(shid, report, 0);

drivers/hid/spi-hid/spi-hid-core.c

Quoted 10 lines
drivers/hid/spi-hid/spi-hid-core.c +10 -0
+}
+
+static int __spi_hid_sync_request(struct spi_hid *shid,
+                               struct spi_hid_output_report *report)
+{
+     struct device *dev = &shid->spi->dev;
+     int error;
+
+     error = __spi_hid_send_output_report(shid, report,
+                                          spi_hid_response_type(report->report_type));

[Severity: High]
Does this sleeping lock violation trigger scheduling while atomic warnings?

According to the HID subsystem guidelines, the low-level output_report callback is used for high-throughput data on the interrupt channel and must be asynchronous.

Quoted 5 lines

When spi_hid_ll_output_report() calls spi_hid_send_output_report() or spi_hid_sync_request(), these helpers immediately acquire shid->output_lock (a sleeping mutex) and perform a synchronous, blocking SPI transfer. Since the callback can be invoked from atomic contexts (like updating LED states from an input event handler), sleeping here may cause panics.

No. hid-input defers LED updates from input events to a workqueue, so ->output_report() isn't called from atomic context there, and USB-HID and I2C-HID already sleep in this callback too.

Quoted 29 lines

[ ... ]

Quoted 6 lines
drivers/hid/spi-hid/spi-hid-core.c +6 -0
+static int spi_hid_input_report_handler(struct spi_hid *shid,
+                                     struct spi_hid_input_buf *buf)
+{
+     struct device *dev = &shid->spi->dev;
+     struct hid_device *hid;
+     struct spi_hid_input_report r;

drivers/hid/spi-hid/spi-hid-core.c static int spi_hid_sync_request(struct spi_hid *shid, · line 580

Quoted 10 lines
drivers/hid/spi-hid/spi-hid-core.c +10 -0
+     int error = 0;
+
+     scoped_guard(mutex, &shid->io_lock) {
+             if (!test_bit(SPI_HID_READY, &shid->flags) ||
+                 !test_bit(SPI_HID_STARTED, &shid->flags) ||
+                 test_bit(SPI_HID_REFRESH_IN_PROGRESS, &shid->flags) || !shid->hid) {
+                     dev_dbg(dev, "HID not ready (flags 0x%lx), dropping input report\n",
+                             shid->flags);
+                     return 0;
+             }

[Severity: Medium]
Will this prevent drivers from receiving input reports during probe?

When spi_hid_refresh_device() is executed, it sets the SPI_HID_REFRESH_IN_PROGRESS flag before tearing down the old HID device and creating the new one:

spi_hid_refresh_device()
set_bit(SPI_HID_REFRESH_IN_PROGRESS, ...)
spi_hid_create_device()

Quoted 8 lines
    hid_add_device()
      -> driver probe()

During this entire time, the input report handler will drop all incoming reports. This conflicts with the HID core API contract, which explicitly allows a driver to receive inputs during its probe phase by calling hid_device_io_start(). A driver that requires a firmware handshake or data during probe could hang or fail upon device refresh.

Thank you. I will fix it in v6.

Quoted 3 lines

--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5

#14 lore raw

PATCH v5 6/11 HID: spi-hid: add spi_hid traces

In reply to Jingyuan Liang

Add traces for the purpose of debugging spi_hid driver.

---
 drivers/hid/spi-hid/Makefile        |   1 +
 drivers/hid/spi-hid/spi-hid-core.c  | 132 ++++++++------------------
 drivers/hid/spi-hid/spi-hid-core.h  | 109 +++++++++++++++++++++
 drivers/hid/spi-hid/spi-hid-trace.h | 184 ++++++++++++++++++++++++++++++++++++
 4 files changed, 334 insertions(+), 92 deletions(-)
drivers/hid/spi-hid/Makefile +1 -0
index 92e24cddbfc2..733e006df56e 100644
@@ -7,3 +7,4 @@
 
 obj-$(CONFIG_SPI_HID_CORE)	+= spi-hid.o
 spi-hid-objs 			= spi-hid-core.o
+CFLAGS_spi-hid-core.o 		:= -I$(src)
drivers/hid/spi-hid/spi-hid-core.c +40 -92

228 lines not on this page · raw message

drivers/hid/spi-hid/spi-hid-core.h new file +109 -0

109 lines not on this page · raw message

drivers/hid/spi-hid/spi-hid-trace.h new file +184 -0

184 lines not on this page · raw message

Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
#15 lore raw

PATCH v5 7/11 HID: spi-hid: add ACPI support for HID over SPI

In reply to Jingyuan Liang

From: Angela Czubak <[email protected]>

Detect SPI HID devices described in ACPI.

---
 drivers/hid/spi-hid/Kconfig        |  16 +++
 drivers/hid/spi-hid/Makefile       |   1 +
 drivers/hid/spi-hid/spi-hid-acpi.c | 261 +++++++++++++++++++++++++++++++++++++
 drivers/hid/spi-hid/spi-hid-core.c |  48 +++----
 drivers/hid/spi-hid/spi-hid.h      |  45 +++++++
 5 files changed, 344 insertions(+), 27 deletions(-)
drivers/hid/spi-hid/Kconfig +16 -0
index 836fdefe8345..303ada03a1bf 100644
@@ -10,6 +10,22 @@ menuconfig SPI_HID
 
 if SPI_HID
 
+config SPI_HID_ACPI
+	tristate "HID over SPI transport layer ACPI driver"
+	depends on ACPI
+	select RESET_CONTROLLER
+	select SPI_HID_CORE
+	help
+	  Say Y here if you use a keyboard, a touchpad, a touchscreen, or any
+	  other HID based devices which are connected to your computer via SPI.
+	  This driver supports ACPI-based systems.
+
+	  If unsure, say N.
+
+	  This support is also available as a module.  If so, the module
+	  will be called spi-hid-acpi. It will also build/depend on the
+	  module spi-hid.
+
 config SPI_HID_CORE
 	tristate
 endif
drivers/hid/spi-hid/Makefile +1 -0
index 733e006df56e..3ca326602643 100644
@@ -8,3 +8,4 @@
 obj-$(CONFIG_SPI_HID_CORE)	+= spi-hid.o
 spi-hid-objs 			= spi-hid-core.o
 CFLAGS_spi-hid-core.o 		:= -I$(src)
+obj-$(CONFIG_SPI_HID_ACPI)	+= spi-hid-acpi.o
drivers/hid/spi-hid/spi-hid-acpi.c new file +261 -0

261 lines not on this page · raw message

drivers/hid/spi-hid/spi-hid-core.c +21 -27

73 lines not on this page · raw message

drivers/hid/spi-hid/spi-hid.h new file +45 -0

45 lines not on this page · raw message

Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
#16 lore raw

Re: [PATCH v5 07/11] HID: spi-hid: add ACPI support for HID over SPI

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] spi-hid: data race on bus_error_count in spi_hid_error_handler --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] spi-hid: data race on bus_error_count in spi_hid_error_handler
--

commit a262e333d16eb58c8aa593f81c7497790c2b01b7
Author: Angela Czubak <[email protected]>

HID: spi-hid: add ACPI support for HID over SPI

Detect SPI HID devices described in ACPI.

Quoted 4 lines
drivers/hid/spi-hid/spi-hid-core.c +0 -0
index 83678e6835818..f4e9b4e1294ed 100644

[ ... ]

drivers/hid/spi-hid/spi-hid-core.c static void spi_hid_error_handler(struct spi_hid *shid) · line 310

Quoted 10 lines
drivers/hid/spi-hid/spi-hid-core.c +5 -1
@@ -327,8 +303,21 @@ static void spi_hid_error_handler(struct spi_hid *shid)
 
 	shid->power_state = HIDSPI_ON;
 
-	shid->ops->deassert_reset(shid->ops);
+	error = shid->ops->deassert_reset(shid->ops);
+	if (error) {
+		dev_err(dev, "failed to deassert reset: %d\n", error);
+		shid->bus_error_count++;
+		shid->bus_last_error = error;
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

[Severity: Low]
Does this introduce a data race with other updaters of bus_error_count?

Userspace sending an output report via __spi_hid_send_output_report() checks SPI_HID_READY locklessly. If SPI_HID_READY is read as 1 just before spi_hid_error_handler() clears it, the output report path proceeds to acquire io_lock and invoke spi_hid_output(), which updates bus_error_count on error (under io_lock).

Because spi_hid_error_handler() updates bus_error_count here without holding io_lock, both threads can execute shid->bus_error_count++ concurrently, which can lead to lost increments or torn reads via sysfs.

Quoted 12 lines
drivers/hid/spi-hid/spi-hid-core.c +9 -0
+		/*
+		 * Without a successful reset the device will not send a reset
+		 * response, so no IRQ would trigger another recovery attempt.
+		 * Retry from here; reset_attempts bounds the number of tries.
+		 */
+		set_bit(SPI_HID_ERROR, &shid->flags);
+		schedule_work(&shid->reset_work);
+	}
 
+	/* Balance disable_irq() above; a retry disables it again. */
 	enable_irq(shid->spi->irq);
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
#17 lore raw

Re: [PATCH v5 07/11] HID: spi-hid: add ACPI support for HID over SPI

Quoted 40 lines · On Fri, Oct 9, 2026 at 3:41 PM <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] spi-hid: data race on bus_error_count in spi_hid_error_handler
--

commit a262e333d16eb58c8aa593f81c7497790c2b01b7
Author: Angela Czubak <[email protected]>

HID: spi-hid: add ACPI support for HID over SPI

Detect SPI HID devices described in ACPI.

Quoted 4 lines
drivers/hid/spi-hid/spi-hid-core.c +0 -0
index 83678e6835818..f4e9b4e1294ed 100644

[ ... ]

drivers/hid/spi-hid/spi-hid-core.c static void spi_hid_error_handler(struct spi_hid *shid) · line 310

Quoted 10 lines
drivers/hid/spi-hid/spi-hid-core.c +5 -1
@@ -327,8 +303,21 @@ static void spi_hid_error_handler(struct spi_hid *shid)
 
      shid->power_state = HIDSPI_ON;
 
-     shid->ops->deassert_reset(shid->ops);
+     error = shid->ops->deassert_reset(shid->ops);
+     if (error) {
+             dev_err(dev, "failed to deassert reset: %d\n", error);
+             shid->bus_error_count++;
+             shid->bus_last_error = error;
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

[Severity: Low]
Does this introduce a data race with other updaters of bus_error_count?

Userspace sending an output report via __spi_hid_send_output_report() checks SPI_HID_READY locklessly. If SPI_HID_READY is read as 1 just before spi_hid_error_handler() clears it, the output report path proceeds to acquire io_lock and invoke spi_hid_output(), which updates bus_error_count on error (under io_lock).

Quoted 3 lines

Because spi_hid_error_handler() updates bus_error_count here without holding io_lock, both threads can execute shid->bus_error_count++ concurrently, which can lead to lost increments or torn reads via sysfs.

This is harmless in practice: bus_error_count and bus_last_error are only debug counters shown in sysfs, and losing an increment would need two failures at the same moment.

Quoted 16 lines

drivers/hid/spi-hid/spi-hid-core.c static void spi_hid_error_handler(struct spi_hid *shid) · line 322

Quoted 13 lines
drivers/hid/spi-hid/spi-hid-core.c +9 -0
+             /*
+              * Without a successful reset the device will not send a reset
+              * response, so no IRQ would trigger another recovery attempt.
+              * Retry from here; reset_attempts bounds the number of tries.
+              */
+             set_bit(SPI_HID_ERROR, &shid->flags);
+             schedule_work(&shid->reset_work);
+     }
 
+     /* Balance disable_irq() above; a retry disables it again. */
      enable_irq(shid->spi->irq);
 }

--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7

#18 lore raw

PATCH v5 8/11 HID: spi-hid: add device tree support for HID over SPI

In reply to Jingyuan Liang

From: Jarrett Schultz <[email protected]>

Detect SPI HID devices described in Device Tree.

---
 drivers/hid/spi-hid/Kconfig      |  15 +++
 drivers/hid/spi-hid/Makefile     |   1 +
 drivers/hid/spi-hid/spi-hid-of.c | 242 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 258 insertions(+)
drivers/hid/spi-hid/Kconfig +15 -0
index 303ada03a1bf..63f202975ac3 100644
@@ -26,6 +26,21 @@ config SPI_HID_ACPI
 	  will be called spi-hid-acpi. It will also build/depend on the
 	  module spi-hid.
 
+config SPI_HID_OF
+	tristate "HID over SPI transport layer Open Firmware driver"
+	depends on OF
+	select SPI_HID_CORE
+	help
+	  Say Y here if you use a keyboard, a touchpad, a touchscreen, or any
+	  other HID based devices which are connected to your computer via SPI.
+	  This driver supports Open Firmware (Device Tree)-based systems.
+
+	  If unsure, say N.
+
+	  This support is also available as a module.  If so, the module
+	  will be called spi-hid-of. It will also build/depend on the
+	  module spi-hid.
+
 config SPI_HID_CORE
 	tristate
 endif
drivers/hid/spi-hid/Makefile +1 -0
index 3ca326602643..31192e71edae 100644
@@ -9,3 +9,4 @@ obj-$(CONFIG_SPI_HID_CORE)	+= spi-hid.o
 spi-hid-objs 			= spi-hid-core.o
 CFLAGS_spi-hid-core.o 		:= -I$(src)
 obj-$(CONFIG_SPI_HID_ACPI)	+= spi-hid-acpi.o
+obj-$(CONFIG_SPI_HID_OF)	+= spi-hid-of.o
drivers/hid/spi-hid/spi-hid-of.c new file +242 -0

242 lines not on this page · raw message

Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
#19 lore raw

Re: [PATCH v5 08/11] HID: spi-hid: add device tree support for HID over SPI

Hello,

drivers/hid/spi-hid/spi-hid-of.c line 32

Quoted 3 lines · Jingyuan Liang
drivers/hid/spi-hid/spi-hid-of.c new file +2 -0
+#include <linux/mod_devicetable.h>
[...]
+#include <linux/spi/spi.h>

Drop <linux/mod_devicetable.h>, the spi header already provides the needed *_device_id with less dependencies.

drivers/hid/spi-hid/spi-hid-of.c line 214

Quoted 3 lines
drivers/hid/spi-hid/spi-hid-of.c new file +3 -0
+static const struct of_device_id spi_hid_of_match[] = {
+	{ .compatible = "hid-over-spi", .data = &timing_data },
+	{}

{ } please

drivers/hid/spi-hid/spi-hid-of.c line 222

Quoted 8 lines
drivers/hid/spi-hid/spi-hid-of.c new file +8 -0
+};
+MODULE_DEVICE_TABLE(of, spi_hid_of_match);
+
+static const struct spi_device_id spi_hid_of_id_table[] = {
+	{ "hid", 0 },
+	{ "hid-over-spi", 0 },
+	{ }
+};

Please use named initializers and drop the unused zeros.

drivers/hid/spi-hid/spi-hid-of.c line 235

Quoted 13 lines
drivers/hid/spi-hid/spi-hid-of.c new file +13 -0
+MODULE_DEVICE_TABLE(spi, spi_hid_of_id_table);
+
+static struct spi_driver spi_hid_of_driver = {
+	.driver = {
+		.name	= "spi_hid_of",
+		.owner	= THIS_MODULE,
+		.of_match_table = spi_hid_of_match,
+		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
+		.dev_groups = spi_hid_groups,
+	},
+	.probe		= spi_hid_of_probe,
+	.remove		= spi_hid_core_remove,
+	.id_table	= spi_hid_of_id_table,

Very inconsistent alignment of =, if you ask me, just use a single space before a =.

Quoted 1 line

+};

Best regards
Uwe

#20 lore raw

Re: [PATCH v5 08/11] HID: spi-hid: add device tree support for HID over SPI

Quoted 10 lines · Uwe Kleine-König

Hello,

drivers/hid/spi-hid/spi-hid-of.c line 32

Quoted 3 lines · Jingyuan Liang
drivers/hid/spi-hid/spi-hid-of.c new file +2 -0
+#include <linux/mod_devicetable.h>
[...]
+#include <linux/spi/spi.h>

Drop <linux/mod_devicetable.h>, the spi header already provides the needed *_device_id with less dependencies.

Thank you. I will drop it in v6.

Quoted 6 lines

drivers/hid/spi-hid/spi-hid-of.c line 214

Quoted 4 lines
drivers/hid/spi-hid/spi-hid-of.c new file +3 -0
+static const struct of_device_id spi_hid_of_match[] = {
+     { .compatible = "hid-over-spi", .data = &timing_data },
+     {}

{ } please

Will fix it in v6.

Quoted 11 lines

drivers/hid/spi-hid/spi-hid-of.c line 222

Quoted 9 lines
drivers/hid/spi-hid/spi-hid-of.c new file +8 -0
+};
+MODULE_DEVICE_TABLE(of, spi_hid_of_match);
+
+static const struct spi_device_id spi_hid_of_id_table[] = {
+     { "hid", 0 },
+     { "hid-over-spi", 0 },
+     { }
+};

Please use named initializers and drop the unused zeros.

Thank you. Will fix it in v6.

Quoted 17 lines
Quoted 4 lines
drivers/hid/spi-hid/spi-hid-of.c new file +3 -0
+MODULE_DEVICE_TABLE(spi, spi_hid_of_id_table);
+
+static struct spi_driver spi_hid_of_driver = {

drivers/hid/spi-hid/spi-hid-of.c line 235

Quoted 10 lines
drivers/hid/spi-hid/spi-hid-of.c new file +10 -0
+     .driver = {
+             .name   = "spi_hid_of",
+             .owner  = THIS_MODULE,
+             .of_match_table = spi_hid_of_match,
+             .probe_type = PROBE_PREFER_ASYNCHRONOUS,
+             .dev_groups = spi_hid_groups,
+     },
+     .probe          = spi_hid_of_probe,
+     .remove         = spi_hid_core_remove,
+     .id_table       = spi_hid_of_id_table,

Very inconsistent alignment of =, if you ask me, just use a single space before a =.

Will do in v6, thanks.

Quoted 5 lines
Quoted 2 lines

+};

Best regards
Uwe

#21 lore raw

PATCH v5 9/11 dt-bindings: input: Document hid-over-spi DT schema

In reply to Jingyuan Liang

Documentation describes the required and optional properties for implementing Device Tree for a Microsoft G6 Touch Digitizer that supports HID over SPI Protocol 1.0 specification.

The properties are common to HID over SPI.

---
 .../devicetree/bindings/input/hid-over-spi.yaml    | 130 +++++++++++++++++++++
 1 file changed, 130 insertions(+)
Documentation/devicetree/bindings/input/hid-over-spi.yaml new file +130 -0
new file mode 100644
index 000000000000..2a5fa0868a09
@@ -0,0 +1,130 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/input/hid-over-spi.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: HID over SPI Devices
+
+maintainers:
+  - Benjamin Tissoires <[email protected]>
+  - Jiri Kosina <[email protected]>
+  - Jingyuan Liang <[email protected]>
+
+description: |+
+  HID over SPI provides support for various Human Interface Devices over the
+  SPI bus. These devices can be for example touchpads, keyboards, touch screens
+  or sensors.
+
+  The specification has been written by Microsoft and is currently available
+  here: https://www.microsoft.com/en-us/download/details.aspx?id=103325
+
+  The HID over SPI specification does not define fixed values for the
+  read/write opcodes and the input/output report addresses. The host
+  retrieves them from platform firmware (ACPI _DSM), and they differ
+  between devices. These properties provide the same values on Device
+  Tree systems, so devices that fully comply with the specification work
+  with the "hid-over-spi" fallback compatible.
+
+allOf:
+  - $ref: /schemas/input/touchscreen/touchscreen.yaml#
+  - $ref: /schemas/spi/spi-peripheral-props.yaml#
+
+properties:
+  compatible:
+    items:
+      - enum:
+          - microsoft,g6-touch-digitizer
+      - const: hid-over-spi
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  reset-gpios:
+    maxItems: 1
+    description:
+      GPIO specifier for the device's reset line. The HID over SPI
+      specification requires a dedicated, host-driven, active-low reset
+      line. The line must be flagged with GPIO_ACTIVE_LOW.
+
+  vdd-supply:
+    description:
+      Regulator for the VDD supply voltage.
+
+  input-report-header-address:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    minimum: 0
+    maximum: 0xffffff
+    description:
+      A value to be included in the Read Approval packet, listing an address of
+      the input report header to be put on the SPI bus. This address has 24
+      bits.
+
+  input-report-body-address:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    minimum: 0
+    maximum: 0xffffff
+    description:
+      A value to be included in the Read Approval packet, listing an address of
+      the input report body to be put on the SPI bus. This address has 24 bits.
+
+  output-report-address:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    minimum: 0
+    maximum: 0xffffff
+    description:
+      A value to be included in the Output Report sent by the host, listing an
+      address where the output report on the SPI bus is to be written to. This
+      address has 24 bits.
+
+  read-opcode:
+    $ref: /schemas/types.yaml#/definitions/uint8
+    description:
+      Value to be used in Read Approval packets. 1 byte.
+
+  write-opcode:
+    $ref: /schemas/types.yaml#/definitions/uint8
+    description:
+      Value to be used in Output Report packets. 1 byte.
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - reset-gpios
+  - vdd-supply
+  - input-report-header-address
+  - input-report-body-address
+  - output-report-address
+  - read-opcode
+  - write-opcode
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+    #include <dt-bindings/gpio/gpio.h>
+
+    spi {
+      #address-cells = <1>;
+      #size-cells = <0>;
+
+      hid@0 {
+        compatible = "microsoft,g6-touch-digitizer", "hid-over-spi";
+        reg = <0x0>;
+        interrupts-extended = <&gpio 42 IRQ_TYPE_EDGE_FALLING>;
+        reset-gpios = <&gpio 27 GPIO_ACTIVE_LOW>;
+        vdd-supply = <&pm8350c_l3>;
+        pinctrl-names = "default";
+        pinctrl-0 = <&ts_d6_int_bias>;
+        input-report-header-address = <0x1000>;
+        input-report-body-address = <0x1004>;
+        output-report-address = <0x2000>;
+        read-opcode = /bits/ 8 <0x0b>;
+        write-opcode = /bits/ 8 <0x02>;
+      };
+    };
Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
#22 lore raw

Re: [PATCH v5 09/11] dt-bindings: input: Document hid-over-spi DT schema

Quoted 9 lines · Jingyuan Liang

Documentation describes the required and optional properties for implementing Device Tree for a Microsoft G6 Touch Digitizer that supports HID over SPI Protocol 1.0 specification.

The properties are common to HID over SPI.

No, fake test.

Best regards,
Krzysztof

#23 lore raw

Re: [PATCH v5 09/11] dt-bindings: input: Document hid-over-spi DT schema

Quoted 6 lines · Krzysztof Kozlowski
Quoted 4 lines · Jingyuan Liang

Documentation describes the required and optional properties for implementing Device Tree for a Microsoft G6 Touch Digitizer that supports HID over SPI Protocol 1.0 specification.

Quoted 7 lines
Quoted 5 lines

The properties are common to HID over SPI.

No, fake test.

Will drop the tested-by tag in v6, thanks.

Quoted 3 lines

Best regards,
Krzysztof

#24 lore raw

PATCH v5 10/11 HID: spi-hid: add power management implementation

In reply to Jingyuan Liang

Implement HID over SPI driver power management callbacks.

---
 drivers/hid/spi-hid/spi-hid-acpi.c |   1 +
 drivers/hid/spi-hid/spi-hid-core.c | 185 +++++++++++++++++++++++++++++++++++++
 drivers/hid/spi-hid/spi-hid-core.h |   2 +
 drivers/hid/spi-hid/spi-hid-of.c   |   1 +
 drivers/hid/spi-hid/spi-hid.h      |   1 +
 5 files changed, 190 insertions(+)
drivers/hid/spi-hid/spi-hid-acpi.c +1 -0
index 31ca3fa336cd..a52cafe86c05 100644
@@ -246,6 +246,7 @@ static struct spi_driver spi_hid_acpi_driver = {
 	.driver = {
 		.name	= "spi_hid_acpi",
 		.owner	= THIS_MODULE,
+		.pm	= &spi_hid_core_pm,
 		.acpi_match_table = spi_hid_acpi_match,
 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
 		.dev_groups = spi_hid_groups,
drivers/hid/spi-hid/spi-hid-core.c +185 -0

239 lines not on this page · raw message

drivers/hid/spi-hid/spi-hid-core.h +2 -0
index 1d8356f787f3..0b3248aedfce 100644
@@ -92,6 +92,8 @@ struct spi_hid {
 	struct mutex io_lock;
 
 	struct completion output_done;
+	/* Completed once the device descriptor is received after a reset. */
+	struct completion reset_done;
 
 	u32 report_descriptor_crc32;	/* HID report descriptor crc32 checksum. */
 
drivers/hid/spi-hid/spi-hid-of.c +1 -0
index 78d0530d2473..605edecc4f7e 100644
@@ -226,6 +226,7 @@ static struct spi_driver spi_hid_of_driver = {
 	.driver = {
 		.name	= "spi_hid_of",
 		.owner	= THIS_MODULE,
+		.pm	= &spi_hid_core_pm,
 		.of_match_table = spi_hid_of_match,
 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
 		.dev_groups = spi_hid_groups,
drivers/hid/spi-hid/spi-hid.h +1 -0
index f5a5f4d54beb..17b2fdf192ed 100644
@@ -41,5 +41,6 @@ int spi_hid_core_probe(struct spi_device *spi, struct spihid_ops *ops,
 void spi_hid_core_remove(struct spi_device *spi);
 
 extern const struct attribute_group *spi_hid_groups[];
+extern const struct dev_pm_ops spi_hid_core_pm;
 
 #endif /* SPI_HID_H */
Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
#25 lore raw

Re: [PATCH v5 10/11] HID: spi-hid: add power management implementation

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] spi-hid: hid_driver_reset_resume() races with error recovery during resume --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] spi-hid: hid_driver_reset_resume() races with error recovery during resume --

commit e43c402ebe88a8dd98c0bab5a81f64d825faf560
Author: Jingyuan Liang <[email protected]>

HID: spi-hid: add power management implementation

This commit implements power management callbacks for the HID over SPI driver. It introduces suspend and resume operations, handling device power state and reset coordination during system transitions.

Quoted 4 lines
drivers/hid/spi-hid/spi-hid-core.c +0 -0
index f4e9b4e1294ed..66bfc251bd915 100644

[ ... ]

Quoted 8 lines
drivers/hid/spi-hid/spi-hid-core.c +8 -0
+static int spi_hid_resume(struct spi_hid *shid)
+{
+	int error;
+	struct device *dev = &shid->spi->dev;
+	bool was_reset = false;
+
+	guard(mutex)(&shid->power_lock);
+	if (test_bit(SPI_HID_DEAD, &shid->flags)) {

[ ... ]

Quoted 5 lines
drivers/hid/spi-hid/spi-hid-core.c +5 -0
+	enable_irq(shid->spi->irq);
+
+	/*
+	 * For devices that may wake up, process work that was pending when
+	 * suspend cancelled reset_work. Otherwise suspend already dropped it.

drivers/hid/spi-hid/spi-hid-core.c static const char *spi_hid_power_mode_string(enum hidspi_power_state power_state · line 385

Quoted 10 lines
drivers/hid/spi-hid/spi-hid-core.c +10 -0
+	 */
+	if (test_bit(SPI_HID_RESET_RESPONSE, &shid->flags) ||
+	    test_bit(SPI_HID_CREATE_DEVICE, &shid->flags) ||
+	    test_bit(SPI_HID_ERROR, &shid->flags))
+		schedule_work(&shid->reset_work);
+
+	if (shid->hid) {
+		if (was_reset)
+			spi_hid_wait_for_reset(shid);
+		error = hid_driver_reset_resume(shid->hid);

[Severity: Medium]
Does hid_driver_reset_resume() race with error recovery here?

In spi_hid_resume() (drivers/hid/spi-hid/spi-hid-core.c), the power_lock is acquired at the beginning of the function and held until it returns.

If a wakeup-capable device suspends while an error recovery is pending (the SPI_HID_ERROR flag is set), spi_hid_suspend() cancels reset_work and leaves the device powered on.

During system resume, spi_hid_resume() reschedules reset_work to handle the pending error. However, because the device was left powered on, was_reset is false, causing spi_hid_resume() to skip spi_hid_wait_for_reset().

It then immediately calls hid_driver_reset_resume() while still holding power_lock. Because reset_work requires power_lock to execute spi_hid_error_handler(), the scheduled error recovery is blocked until spi_hid_resume() returns.

Consequently, hid_driver_reset_resume() attempts to communicate with the device synchronously while it is physically still in an unresponsive error state, which will time out or fail, aborting the device resume.

Quoted 8 lines
drivers/hid/spi-hid/spi-hid-core.c +8 -0
+		if (error) {
+			dev_err(dev, "%s: failed to reset resume hid driver: %d\n",
+				__func__, error);
+			return error;
+		}
+	}
+	return 0;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
#26 lore raw

Re: [PATCH v5 10/11] HID: spi-hid: add power management implementation

Quoted 58 lines · On Fri, Oct 9, 2026 at 3:43 PM <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] spi-hid: hid_driver_reset_resume() races with error recovery during resume --

commit e43c402ebe88a8dd98c0bab5a81f64d825faf560
Author: Jingyuan Liang <[email protected]>

HID: spi-hid: add power management implementation

This commit implements power management callbacks for the HID over SPI driver. It introduces suspend and resume operations, handling device power state and reset coordination during system transitions.

Quoted 4 lines
drivers/hid/spi-hid/spi-hid-core.c +0 -0
index f4e9b4e1294ed..66bfc251bd915 100644

[ ... ]

Quoted 8 lines
drivers/hid/spi-hid/spi-hid-core.c +8 -0
+static int spi_hid_resume(struct spi_hid *shid)
+{
+     int error;
+     struct device *dev = &shid->spi->dev;
+     bool was_reset = false;
+
+     guard(mutex)(&shid->power_lock);
+     if (test_bit(SPI_HID_DEAD, &shid->flags)) {

[ ... ]

Quoted 5 lines
drivers/hid/spi-hid/spi-hid-core.c +5 -0
+     enable_irq(shid->spi->irq);
+
+     /*
+      * For devices that may wake up, process work that was pending when
+      * suspend cancelled reset_work. Otherwise suspend already dropped it.

drivers/hid/spi-hid/spi-hid-core.c static const char *spi_hid_power_mode_string(enum hidspi_power_state power_state · line 385

Quoted 10 lines
drivers/hid/spi-hid/spi-hid-core.c +10 -0
+      */
+     if (test_bit(SPI_HID_RESET_RESPONSE, &shid->flags) ||
+         test_bit(SPI_HID_CREATE_DEVICE, &shid->flags) ||
+         test_bit(SPI_HID_ERROR, &shid->flags))
+             schedule_work(&shid->reset_work);
+
+     if (shid->hid) {
+             if (was_reset)
+                     spi_hid_wait_for_reset(shid);
+             error = hid_driver_reset_resume(shid->hid);

[Severity: Medium]
Does hid_driver_reset_resume() race with error recovery here?

In spi_hid_resume() (drivers/hid/spi-hid/spi-hid-core.c), the power_lock is acquired at the beginning of the function and held until it returns.

If a wakeup-capable device suspends while an error recovery is pending (the SPI_HID_ERROR flag is set), spi_hid_suspend() cancels reset_work and leaves the device powered on.

During system resume, spi_hid_resume() reschedules reset_work to handle the pending error. However, because the device was left powered on, was_reset is false, causing spi_hid_resume() to skip spi_hid_wait_for_reset().

Quoted 8 lines

It then immediately calls hid_driver_reset_resume() while still holding power_lock. Because reset_work requires power_lock to execute spi_hid_error_handler(), the scheduled error recovery is blocked until spi_hid_resume() returns.

Consequently, hid_driver_reset_resume() attempts to communicate with the device synchronously while it is physically still in an unresponsive error state, which will time out or fail, aborting the device resume.

This needs an error to be pending exactly at suspend on a wakeup-enabled device. Error recovery still runs right after resume, so I'd rather keep the resume path simple.

Quoted 12 lines

drivers/hid/spi-hid/spi-hid-core.c static const char *spi_hid_power_mode_string(enum hidspi_power_state power_state · line 393

Quoted 9 lines
drivers/hid/spi-hid/spi-hid-core.c +8 -0
+             if (error) {
+                     dev_err(dev, "%s: failed to reset resume hid driver: %d\n",
+                             __func__, error);
+                     return error;
+             }
+     }
+     return 0;
+}

--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10

#27 lore raw

PATCH v5 11/11 HID: spi-hid: add panel follower support

In reply to Jingyuan Liang

Add support to spi-hid to be a panel follower.

---
 drivers/hid/spi-hid/Kconfig        |   4 +
 drivers/hid/spi-hid/spi-hid-core.c | 213 ++++++++++++++++++++++++++++---------
 drivers/hid/spi-hid/spi-hid-core.h |   7 ++
 3 files changed, 174 insertions(+), 50 deletions(-)
drivers/hid/spi-hid/Kconfig +4 -0
index 63f202975ac3..656dbda1c5d8 100644
@@ -13,6 +13,7 @@ if SPI_HID
 config SPI_HID_ACPI
 	tristate "HID over SPI transport layer ACPI driver"
 	depends on ACPI
+	depends on DRM || !DRM
 	select RESET_CONTROLLER
 	select SPI_HID_CORE
 	help
@@ -29,6 +30,7 @@ config SPI_HID_ACPI
 config SPI_HID_OF
 	tristate "HID over SPI transport layer Open Firmware driver"
 	depends on OF
+	depends on DRM || !DRM
 	select SPI_HID_CORE
 	help
 	  Say Y here if you use a keyboard, a touchpad, a touchscreen, or any
@@ -43,4 +45,6 @@ config SPI_HID_OF
 
 config SPI_HID_CORE
 	tristate
+	# We need to call into panel code so if DRM=m, this can't be 'y'
+	depends on DRM || !DRM
 endif
drivers/hid/spi-hid/spi-hid-core.c +163 -50

292 lines not on this page · raw message

drivers/hid/spi-hid/spi-hid-core.h +7 -0
index 0b3248aedfce..631897dd9e8a 100644
@@ -10,6 +10,8 @@
 #include <linux/hid-over-spi.h>
 #include <linux/spi/spi.h>
 
+#include <drm/drm_panel.h>
+
 /* Protocol message size constants */
 #define SPI_HID_READ_APPROVAL_LEN		5
 #define SPI_HID_OUTPUT_HEADER_LEN		8
@@ -56,6 +58,10 @@ struct spi_hid {
 	struct spi_hid_input_buf *input;	/* Input buffer. */
 	struct spi_hid_input_buf *response;	/* Response buffer. */
 
+	struct drm_panel_follower panel_follower;
+	bool	is_panel_follower;
+	bool	panel_follower_work_finished;
+
 	u16 response_length;
 	u16 bufsize;
 	/* Response type awaited by a sync request, 0 if none. Protected by io_lock. */
@@ -70,6 +76,7 @@ struct spi_hid {
 	unsigned long flags;	/* device flags. */
 
 	struct work_struct reset_work;
+	struct work_struct panel_follower_work;
 
 	/*
 	 * Serializes request/response transactions: held from sending a
Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
#28 lore raw

Re: [PATCH v5 11/11] HID: spi-hid: add panel follower support

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] spi-hid: Inconsistent wake IRQ configuration for panel followers --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] spi-hid: Inconsistent wake IRQ configuration for panel followers
--

commit 4c8d0ec463c5dbd300c392f3f924992096f8ebc1
Author: Jingyuan Liang <[email protected]>

HID: spi-hid: add panel follower support

This commit adds support to the spi-hid transport driver to act as a panel follower, allowing its power state to be sequenced with a display panel.

Quoted 3 lines
drivers/hid/spi-hid/spi-hid-core.c +0 -0

[ ... ]

Quoted 10 lines
drivers/hid/spi-hid/spi-hid-core.c +0 -6
@@ -1577,21 +1676,17 @@ int spi_hid_core_probe(struct spi_device *spi, struct spihid_ops *ops,
 		}
 	}
 
-	error = shid->ops->power_up(shid->ops);
-	if (error) {
-		dev_err(dev, "%s: could not power up\n", __func__);
-		return error;
-	}
-

drivers/hid/spi-hid/spi-hid-core.c int spi_hid_core_probe(struct spi_device *spi, struct spihid_ops *ops, · line 1683

Quoted 10 lines
drivers/hid/spi-hid/spi-hid-core.c +5 -5
-	error = shid->ops->deassert_reset(shid->ops);
-	if (error) {
-		dev_err(dev, "%s: failed to deassert reset: %d\n", __func__, error);
-		shid->ops->power_down(shid->ops);
-		return error;
+	if (shid->is_panel_follower) {
+		error = spi_hid_register_panel_follower(shid);
+		if (error)
+			return dev_err_probe(dev, error,
+					     "Failed to register panel follower\n");

[Severity: Low]
Does this sequence leave a dangling, inactive wake IRQ attached to the device when it is a panel follower?

In spi_hid_core_probe() just prior to this block, the code checks device_can_wakeup(dev) and unconditionally allocates the wake IRQ via devm_pm_set_wake_irq().

However, if the device is a panel follower, it calls spi_hid_register_panel_follower() here. Inside that function, it checks device_can_wakeup(dev) again, warns the user, and forcibly disables the capability:

    if (device_can_wakeup(dev)) {
        dev_warn(dev, "Can't wakeup if following panel\n");
        device_set_wakeup_capable(dev, false);
    }

Because the capability is disabled after the IRQ is already requested, it appears to waste resources by allocating an IRQ that is immediately rendered unusable. Should the panel follower check and the potential clearing of the wakeup capability occur before the wake IRQ is requested?

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

Re: [PATCH v5 11/11] HID: spi-hid: add panel follower support

Quoted 57 lines · On Fri, Oct 9, 2026 at 3:36 PM <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] spi-hid: Inconsistent wake IRQ configuration for panel followers
--

commit 4c8d0ec463c5dbd300c392f3f924992096f8ebc1
Author: Jingyuan Liang <[email protected]>

HID: spi-hid: add panel follower support

This commit adds support to the spi-hid transport driver to act as a panel follower, allowing its power state to be sequenced with a display panel.

Quoted 3 lines
drivers/hid/spi-hid/spi-hid-core.c +0 -0

[ ... ]

Quoted 10 lines
drivers/hid/spi-hid/spi-hid-core.c +0 -6
@@ -1577,21 +1676,17 @@ int spi_hid_core_probe(struct spi_device *spi, struct spihid_ops *ops,
              }
      }
 
-     error = shid->ops->power_up(shid->ops);
-     if (error) {
-             dev_err(dev, "%s: could not power up\n", __func__);
-             return error;
-     }
-

drivers/hid/spi-hid/spi-hid-core.c int spi_hid_core_probe(struct spi_device *spi, struct spihid_ops *ops, · line 1683

Quoted 10 lines
drivers/hid/spi-hid/spi-hid-core.c +5 -5
-     error = shid->ops->deassert_reset(shid->ops);
-     if (error) {
-             dev_err(dev, "%s: failed to deassert reset: %d\n", __func__, error);
-             shid->ops->power_down(shid->ops);
-             return error;
+     if (shid->is_panel_follower) {
+             error = spi_hid_register_panel_follower(shid);
+             if (error)
+                     return dev_err_probe(dev, error,
+                                          "Failed to register panel follower\n");

[Severity: Low]
Does this sequence leave a dangling, inactive wake IRQ attached to the device when it is a panel follower?

In spi_hid_core_probe() just prior to this block, the code checks device_can_wakeup(dev) and unconditionally allocates the wake IRQ via devm_pm_set_wake_irq().

However, if the device is a panel follower, it calls spi_hid_register_panel_follower() here. Inside that function, it checks device_can_wakeup(dev) again, warns the user, and forcibly disables the capability:

    if (device_can_wakeup(dev)) {
        dev_warn(dev, "Can't wakeup if following panel\n");
        device_set_wakeup_capable(dev, false);
    }
Quoted 4 lines

Because the capability is disabled after the IRQ is already requested, it appears to waste resources by allocating an IRQ that is immediately rendered unusable. Should the panel follower check and the potential clearing of the wakeup capability occur before the wake IRQ is requested?

This is harmless: devm_pm_set_wake_irq() doesn't request a new IRQ, it only marks the existing one as the wake IRQ, and the PM core only arms it when wakeup is enabled. i2c-hid has the same sequence (the I2C core sets the wake IRQ before the panel follower check).

Quoted 3 lines

--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11

#30 lore raw

cover HID: spi-hid: handle reset assertion and resume handshake failures

In reply to Jingyuan Liang

Hi Jingyuan,

I applied the complete v5 series to its stated base and found two remaining error paths:

- Initialization continues after assert_reset() fails, powering up the device and enabling its IRQ while returning success.
- The resume reset wait only logs a missing device descriptor, so the HID reset_resume callback can run without a completed handshake.

Propagate the assertion error, and return -ETIMEDOUT from the reset wait while scheduling the existing bounded recovery worker. Skip the HID resume callback on that timeout, including the failed-suspend rollback path, where the original suspend error remains the return value. The PM core can therefore report an incomplete resume, while the worker may still recover the device asynchronously.

The actual driver functions were exercised with mock firmware/IRQ/HID interfaces under ASan/UBSan. The SL7 integration's SPI-HID core, OF and ACPI objects compile with W=1 on ARM64. The full integration boots and works in normal use on SL7. Logs from two s2idle cycles show the touchpad being re-enumerated and iptsd reconnecting automatically; the transport and regulator error counters remain zero.

Based on your v5:
https://lore.kernel.org/all/[email protected]/
Upstream base: ee9c669f9bf5fd2c24206746ded9382fe810df89.

Best regards,
Jizhou Tong

fQwQf (2):
HID: spi-hid: propagate reset assertion errors during initialization
HID: spi-hid: report a missing reset handshake on resume

 drivers/hid/spi-hid/spi-hid-core.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

base-commit: ee9c669f9bf5fd2c24206746ded9382fe810df89
prerequisite-patch-id: b401890d766019f0677a572f18bb389cd8827721
prerequisite-patch-id: beaf91cb29f3a735559a3251092ac0ac17810b10
prerequisite-patch-id: 909ee816b83e0bc8bb4b560f8c7cc0c8109c4516
prerequisite-patch-id: 8b60145a9a853a32881af27181c26dff0e82a33e
prerequisite-patch-id: bcb36384a391e48222f0924b0c571fb0eac19ae7
prerequisite-patch-id: 816dfccf7d35ea041c0d7c08c7bfbe061a45c7dc
prerequisite-patch-id: 67a41d4cb6bfa41248d89a8f967d4414cff2da2e
prerequisite-patch-id: d0bf155ff4d70a3aeb88ec0232f04a4b2edb3598
prerequisite-patch-id: f18d9032e5ed5713beb37527dba225ec79589b31
prerequisite-patch-id: 854affdd16efb5696f9ed1d2a8cdb50dababe724
prerequisite-patch-id: 248c986cba937716c739b804039376ac3823f618

#31 lore raw

v2 cover HID: spi-hid: handle reset assertion and resume handshake failures

Hi Jingyuan,

I applied the complete v5 series to its stated base and found two remaining error paths:

- Initialization continues after assert_reset() fails, powering up the device and enabling its IRQ while returning success.
- The resume reset wait only logs a missing device descriptor, so the HID reset_resume callback can run without a completed handshake.

Propagate the assertion error, and return -ETIMEDOUT from the reset wait while scheduling the existing bounded recovery worker. Skip the HID resume callback on that timeout, including the failed-suspend rollback path, where the original suspend error remains the return value. The PM core can therefore report an incomplete resume, while the worker may still recover the device asynchronously.

The actual driver functions were exercised with mock firmware/IRQ/HID interfaces under ASan/UBSan. The SL7 integration's SPI-HID core, OF and ACPI objects compile with W=1 on ARM64. The full integration boots and works in normal use on SL7. Logs from two s2idle cycles show the touchpad being re-enumerated and iptsd reconnecting automatically; the transport and regulator error counters remain zero.

Based on your v5:
https://lore.kernel.org/all/[email protected]/
Upstream base: ee9c669f9bf5fd2c24206746ded9382fe810df89.

Changes in v2:
- Explain how the reset assertion error propagates through probe.
- No code changes from v1.

v1: https://lore.kernel.org/all/010601a126bfa3ae-858150e8-f0a7-4bae-9c37-d82180d8128e-000000@ap-northeast-1.amazonses.com/

Best regards,
Jizhou Tong

Jizhou Tong (2):
HID: spi-hid: propagate reset assertion errors during initialization
HID: spi-hid: report a missing reset handshake on resume

 drivers/hid/spi-hid/spi-hid-core.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

base-commit: ee9c669f9bf5fd2c24206746ded9382fe810df89
prerequisite-patch-id: b401890d766019f0677a572f18bb389cd8827721
prerequisite-patch-id: beaf91cb29f3a735559a3251092ac0ac17810b10
prerequisite-patch-id: 909ee816b83e0bc8bb4b560f8c7cc0c8109c4516
prerequisite-patch-id: 8b60145a9a853a32881af27181c26dff0e82a33e
prerequisite-patch-id: bcb36384a391e48222f0924b0c571fb0eac19ae7
prerequisite-patch-id: 816dfccf7d35ea041c0d7c08c7bfbe061a45c7dc
prerequisite-patch-id: 67a41d4cb6bfa41248d89a8f967d4414cff2da2e
prerequisite-patch-id: d0bf155ff4d70a3aeb88ec0232f04a4b2edb3598
prerequisite-patch-id: f18d9032e5ed5713beb37527dba225ec79589b31
prerequisite-patch-id: 854affdd16efb5696f9ed1d2a8cdb50dababe724
prerequisite-patch-id: 248c986cba937716c739b804039376ac3823f618

Signature · 1 line
2.53.0
#32 lore raw

PATCH v2 1/2 HID: spi-hid: propagate reset assertion errors during initialization

Stop initialization if the platform cannot assert reset. Powering up and enabling the IRQ after a failed reset assertion can leave the device in an unknown state while probe reports success.

Return the platform error unchanged from spi_hid_dev_init(). The non-panel-follower probe path propagates it through spi_hid_core_probe() to the OF or ACPI frontend, failing probe before power-up or IRQ enablement.

---
 drivers/hid/spi-hid/spi-hid-core.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/hid/spi-hid/spi-hid-core.c +5 -1
index 106e92b71ffb62c05a1c10b975759d71440b47b0..1aae7863e87de766444e7eae392335e4b1daa85e 100644
@@ -1524,7 +1524,11 @@ static int spi_hid_dev_init(struct spi_hid *shid)
 
 	guard(mutex)(&shid->power_lock);
 
-	shid->ops->assert_reset(shid->ops);
+	error = shid->ops->assert_reset(shid->ops);
+	if (error) {
+		dev_err(dev, "failed to assert reset: %d\n", error);
+		return error;
+	}
 
 	shid->ops->sleep_minimal_reset_delay(shid->ops);
 
Signature · 1 line
2.53.0
#33 lore raw

PATCH v2 2/2 HID: spi-hid: report a missing reset handshake on resume

In reply to Jizhou Tong

Return -ETIMEDOUT when the device descriptor does not arrive after reset. Do not call the HID reset_resume callback before the device can accept its requests. Clear READY and schedule the existing bounded recovery worker on timeout. Apply the same rule when undoing a failed suspend, retaining the original suspend error.

Report the incomplete device resume to the PM core instead of returning success. The scheduled worker may still recover the device asynchronously.

---
 drivers/hid/spi-hid/spi-hid-core.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
drivers/hid/spi-hid/spi-hid-core.c +13 -6
index 1aae7863e87de766444e7eae392335e4b1daa85e..0046dfdd82677bb55c5a0b6742cdfaa8fec224c6 100644
@@ -255,7 +255,7 @@ static const char *spi_hid_power_mode_string(enum hidspi_power_state power_state
  * requests. Set READY here: the report descriptor check that normally sets it
  * needs power_lock, which callers hold.
  */
-static void spi_hid_wait_for_reset(struct spi_hid *shid)
+static int spi_hid_wait_for_reset(struct spi_hid *shid)
 {
 	unsigned long timeout = msecs_to_jiffies(SPI_HID_RESET_TIMEOUT +
 						 SPI_HID_RESP_TIMEOUT);
@@ -263,10 +263,14 @@ static void spi_hid_wait_for_reset(struct spi_hid *shid)
 	if (!wait_for_completion_timeout(&shid->reset_done, timeout)) {
 		dev_warn(&shid->spi->dev,
 			 "Device descriptor not received after reset\n");
-		return;
+		clear_bit(SPI_HID_READY, &shid->flags);
+		set_bit(SPI_HID_ERROR, &shid->flags);
+		schedule_work(&shid->reset_work);
+		return -ETIMEDOUT;
 	}
 
 	set_bit(SPI_HID_READY, &shid->flags);
+	return 0;
 }
 
 static int spi_hid_suspend(struct spi_hid *shid)
@@ -326,8 +330,8 @@ static int spi_hid_suspend(struct spi_hid *shid)
 				shid->ops->deassert_reset(shid->ops);
 				enable_irq(shid->spi->irq);
 				if (shid->hid) {
-					spi_hid_wait_for_reset(shid);
-					hid_driver_reset_resume(shid->hid);
+					if (!spi_hid_wait_for_reset(shid))
+						hid_driver_reset_resume(shid->hid);
 				}
 				return error;
 			}
@@ -387,8 +391,11 @@ static int spi_hid_resume(struct spi_hid *shid)
 		schedule_work(&shid->reset_work);
 
 	if (shid->hid) {
-		if (was_reset)
-			spi_hid_wait_for_reset(shid);
+		if (was_reset) {
+			error = spi_hid_wait_for_reset(shid);
+			if (error)
+				return error;
+		}
 		error = hid_driver_reset_resume(shid->hid);
 		if (error) {
 			dev_err(dev, "%s: failed to reset resume hid driver: %d\n",
Signature · 1 line
2.53.0
#34 lore raw

v3 cover HID: spi-hid: propagate reset GPIO errors

In reply to Jizhou Tong

Hi Jingyuan,

These two fixes apply on top of your v7 series [1]. Initialization still ignores a failed reset assertion. Resume also ignores assertion and deassertion failures, so it can report success with the device held in reset. Propagate these errors while preserving the IRQ balance and using the existing recovery worker after a deassertion failure.

The resume handshake timeout fix from my v2 is already in v7 and is not repeated here. The second patch now addresses only the remaining GPIO error paths.

The driver is in the downstream SL7 kernel I have been using with working touchpad input and suspend/resume. Fault injection covers initialization, assertion and deassertion errors, IRQ balance, and recovery scheduling. The core, ACPI and OF modules build with W=1 against the prepared SL7 kernel build. Hardware testing used the complete downstream SL7 stack, including the QSPI transport and controller changes.

Changes since v2:
- Rebase on v7 and drop the already-incorporated handshake fix.
- Check reset GPIO errors on resume and retain v7's recovery behavior.

[1] https://lore.kernel.org/all/[email protected]/

Jizhou Tong (2):
HID: spi-hid: propagate reset assertion errors during initialization
HID: spi-hid: propagate reset GPIO errors from resume

 drivers/hid/spi-hid/spi-hid-core.c | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)

base-commit: ee9c669f9bf5fd2c24206746ded9382fe810df89
prerequisite-patch-id: b401890d766019f0677a572f18bb389cd8827721
prerequisite-patch-id: beaf91cb29f3a735559a3251092ac0ac17810b10
prerequisite-patch-id: e7eba48cebafeee52eae204c950d7c4f8386f0bd
prerequisite-patch-id: 41ca1ba441d0246d5bf6a4befb26b1c65a596b95
prerequisite-patch-id: 84023dc857708fbb763a889ac060acc7d4387264
prerequisite-patch-id: f9b381e783ceb6b608c27e3b6299c591ac9be769
prerequisite-patch-id: ae5c9959b2923b37a5a650f04e93c662b9e10592
prerequisite-patch-id: 44a8965a58c0bfb0d5cf758790f407d136451956
prerequisite-patch-id: 207558a5e93c1074b4dc1b568d70d62a9ffe542b
prerequisite-patch-id: 59919db0f4e3fbaa1908a0b311fcd53837c5aac3
prerequisite-patch-id: b3706d31329e8a5829627fd0eb795c24aeb389cd

#35 lore raw

PATCH v3 1/2 HID: spi-hid: propagate reset assertion errors during initialization

Stop initialization before power-up or IRQ enablement if asserting reset fails. Return the platform error through the core probe and frontend. This initialization path still ignores the assertion error in v7.

---
 drivers/hid/spi-hid/spi-hid-core.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/hid/spi-hid/spi-hid-core.c +5 -1
index 496b33cdf3..fd64d2d2cb 100644
@@ -1468,7 +1468,11 @@ static int spi_hid_dev_init(struct spi_hid *shid)
 
 	guard(mutex)(&shid->power_lock);
 
-	shid->ops->assert_reset(shid->ops);
+	error = shid->ops->assert_reset(shid->ops);
+	if (error) {
+		dev_err(dev, "failed to assert reset: %d\n", error);
+		return error;
+	}
 
 	shid->ops->sleep_minimal_reset_delay(shid->ops);
 
#36 lore raw

PATCH v3 2/2 HID: spi-hid: propagate reset GPIO errors from resume

In reply to Jizhou Tong

Stop before powering up if reset assertion fails, balancing the IRQ disabled by suspend. If reset deassertion fails after power-up, return that error and queue the existing recovery worker. Otherwise resume may report success although the device remains held in reset.

---
 drivers/hid/spi-hid/spi-hid-core.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
drivers/hid/spi-hid/spi-hid-core.c +14 -4
index fd64d2d2cb..f2b2106600 100644
@@ -316,7 +316,7 @@ static int spi_hid_suspend(struct spi_hid *shid)
 
 static int spi_hid_resume(struct spi_hid *shid)
 {
-	int error;
+	int error = 0;
 	struct device *dev = &shid->spi->dev;
 	bool was_reset = false;
 
@@ -333,7 +333,12 @@ static int spi_hid_resume(struct spi_hid *shid)
 	 * while the device is off.
 	 */
 	if (shid->power_state == HIDSPI_OFF) {
-		shid->ops->assert_reset(shid->ops);
+		error = shid->ops->assert_reset(shid->ops);
+		if (error) {
+			dev_err(dev, "failed to assert reset: %d\n", error);
+			enable_irq(shid->spi->irq);
+			return error;
+		}
 
 		shid->ops->sleep_minimal_reset_delay(shid->ops);
 
@@ -347,11 +352,13 @@ static int spi_hid_resume(struct spi_hid *shid)
 		shid->power_state = HIDSPI_ON;
 		reinit_completion(&shid->reset_done);
 		error = shid->ops->deassert_reset(shid->ops);
-		if (error)
+		if (error) {
 			dev_err(dev, "%s: failed to deassert reset: %d\n",
 				__func__, error);
-		else
+			set_bit(SPI_HID_ERROR, &shid->flags);
+		} else {
 			was_reset = true;
+		}
 	}
 
 	enable_irq(shid->spi->irq);
@@ -365,6 +372,9 @@ static int spi_hid_resume(struct spi_hid *shid)
 	    test_bit(SPI_HID_ERROR, &shid->flags))
 		schedule_work(&shid->reset_work);
 
+	if (error)
+		return error;
+
 	if (shid->hid) {
 		if (was_reset && spi_hid_wait_for_reset(shid)) {
 			/* No reset response: let recovery retry. */
#37 lore raw

cover HID: spi-hid: add Romulus13 quad-SPI support on v5

In reply to Jingyuan Liang

Hi Jingyuan and Dale,

This ports my Surface Laptop 7 (Romulus13) additions onto the complete v5 SPI-HID series, preserving v5's request serialization, response matching and IRQ/PM model. It follows the two error-handling fixes sent separately in this thread.

The main addition is a spi-mem transport for the touchpad's 1-4-4 reads and writes. Reads use eight dummy clocks; writes supply the opcode and address separately from the report data. Dedicated aligned TX/RX regions keep streaming DMA away from protocol parsing buffers. The managed allocation is overallocated, with its base kept separately from the explicitly ARCH_DMA_MINALIGN-aligned view. Single-lane transfers retain spi_sync()/spi_write(), including their existing short-write behavior.

A spi_mem_driver can bind controllers without mem_ops. For supported operations, spi_mem_exec_op() falls back to ordinary SPI messages when mem_ops are absent; this driver's single-lane path calls the SPI helpers directly. The SPI_MEM build dependency is still required.

The series also adds bounded DATA fragmentation and the Romulus timing, opcodes, binding and reset behavior. On this device, a reset loses the heatmap mode selected by iptsd while leaving the report descriptor unchanged. Recreating the HID device after descriptor refresh lets hidraw consumers reconnect and select that mode again. Other devices retain v5's CRC-based reuse behavior. Romulus still waits for the reset handshake and schedules recovery on timeout; READY and the old HID resume callback are deferred until re-probe, including failed-suspend rollback.

Software tests compile actual driver functions under ASan/UBSan,
covering transport phases/errors, 65,536 header lengths, 5,048 fragment
combinations and resume/descriptor-refresh paths, including missing
Romulus handshakes and failed-suspend rollback. Both RFC 3/6 and the
complete series compile with W=1 on ARM64 against the stated v5 base.
The allocator tests include five DMA alignments with storage aligned to
only eight bytes, resize failures and capacity rollback. Short writes are accepted on single-lane SPI and rejected on quad SPI.

The previous revision of this v5 integration boots and works in normal use on SL7. Across two logged s2idle cycles, the touchpad was re-enumerated and iptsd reconnected automatically, with READY and zero transport or regulator errors afterward. This revision's allocation, short-write and Romulus handshake changes have build and software-test coverage.

The local test kernel retains the downstream GENI/GPI QSPI controller and Romulus DT integration. Those dependencies are outside this HID series. This RFC is for transport/interface review; in particular, I would appreciate feedback on registering both frontends through spi-mem (which adds a SPI_MEM dependency), and on the device-specific HID rebind after reset.

Base: the v5 series plus the two error-handling fixes.
https://lore.kernel.org/all/[email protected]/

Best regards,
Jizhou Tong

fQwQf (6):
HID: spi-hid: isolate DMA transfers from protocol buffers
HID: spi-hid: add spi-mem quad-SPI transfers
HID: spi-hid: assemble bounded fragmented input reports
dt-bindings: input: describe the Romulus13 QSPI touchpad
HID: spi-hid: support the Romulus13 QSPI touchpad
HID: spi-hid: expose readiness for reset diagnostics

 .../ABI/testing/sysfs-bus-spi-devices-spi-hid |  30 +++
 .../input/microsoft,romulus13-touchpad.yaml   |  86 ++++++++
 MAINTAINERS                                   |   7 +
 drivers/hid/spi-hid/Kconfig                   |   2 +-
 drivers/hid/spi-hid/spi-hid-acpi.c            |  28 +--
 drivers/hid/spi-hid/spi-hid-core.c            | 207 ++++++++++++++----
 drivers/hid/spi-hid/spi-hid-core.h            |   8 +
 drivers/hid/spi-hid/spi-hid-of.c              |  55 +++--
 drivers/hid/spi-hid/spi-hid.h                 |   8 +-
 9 files changed, 355 insertions(+), 76 deletions(-)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-spi-devices-spi-hid
 create mode 100644 Documentation/devicetree/bindings/input/microsoft,romulus13-touchpad.yaml

base-commit: ee9c669f9bf5fd2c24206746ded9382fe810df89
prerequisite-patch-id: b401890d766019f0677a572f18bb389cd8827721
prerequisite-patch-id: beaf91cb29f3a735559a3251092ac0ac17810b10
prerequisite-patch-id: 909ee816b83e0bc8bb4b560f8c7cc0c8109c4516
prerequisite-patch-id: 8b60145a9a853a32881af27181c26dff0e82a33e
prerequisite-patch-id: bcb36384a391e48222f0924b0c571fb0eac19ae7
prerequisite-patch-id: 816dfccf7d35ea041c0d7c08c7bfbe061a45c7dc
prerequisite-patch-id: 67a41d4cb6bfa41248d89a8f967d4414cff2da2e
prerequisite-patch-id: d0bf155ff4d70a3aeb88ec0232f04a4b2edb3598
prerequisite-patch-id: f18d9032e5ed5713beb37527dba225ec79589b31
prerequisite-patch-id: 854affdd16efb5696f9ed1d2a8cdb50dababe724
prerequisite-patch-id: 248c986cba937716c739b804039376ac3823f618
prerequisite-patch-id: 24df0c559e4b401af9acd4bc7bad99dd4b4724d4
prerequisite-patch-id: 2827fd11c245e7f91c73506b9c7c392b44f8d9f3

#38 lore raw

v2 cover HID: spi-hid: add Romulus13 quad-SPI support on v5

Hi Jingyuan and Dale,

This ports my Surface Laptop 7 (Romulus13) additions onto the complete v5 SPI-HID series, preserving v5's request serialization, response matching and IRQ/PM model. It is based on the two error-handling fixes submitted separately.

The main addition is a spi-mem transport for the touchpad's 1-4-4 reads and writes. Reads use eight dummy clocks; writes supply the opcode and address separately from the report data. Dedicated aligned TX/RX regions keep streaming DMA away from protocol parsing buffers. The managed allocation is overallocated, with its base kept separately from the explicitly ARCH_DMA_MINALIGN-aligned view. Single-lane transfers retain spi_sync()/spi_write(), including their existing short-write behavior.

Controllers without mem_ops remain supported; patch 2/6 describes the fallback behavior and SPI_MEM dependency.

The series also adds bounded DATA fragmentation and the Romulus timing, opcodes, binding and reset behavior. On this device, a reset loses the heatmap mode selected by iptsd while leaving the report descriptor unchanged. Recreating the HID device after descriptor refresh lets hidraw consumers reconnect and select that mode again. Other devices retain v5's CRC-based reuse behavior. Romulus still waits for the reset handshake and schedules recovery on timeout; READY and the old HID resume callback are deferred until re-probe, including failed-suspend rollback.

Software tests compile actual driver functions under ASan/UBSan, covering transport phases/errors, 65,536 header lengths, 5,048 fragment combinations and resume/descriptor-refresh paths, including missing Romulus handshakes and failed-suspend rollback. Both RFC 3/6 and the complete series compile with W=1 on ARM64 against the stated v5 base. The allocator tests include five DMA alignments with storage aligned to only eight bytes, resize failures and capacity rollback. Short writes are accepted on single-lane SPI and rejected on quad SPI.

The previous revision of this v5 integration boots and works in normal use on SL7. Across two logged s2idle cycles, the touchpad was re-enumerated and iptsd reconnected automatically, with READY and zero transport or regulator errors afterward. This revision's allocation, short-write and Romulus handshake changes have build and software-test coverage.

The local test kernel retains the downstream GENI/GPI QSPI controller and Romulus DT integration. Those dependencies are outside this HID series. This RFC is for transport/interface review; in particular, I would appreciate feedback on registering both frontends through spi-mem (which adds a SPI_MEM dependency), and on the device-specific HID rebind after reset.

Base: the v5 series plus the two error-handling fixes.
https://lore.kernel.org/all/[email protected]/

Changes in v2:
- Use a designated initializer for the new Romulus SPI ID, following Uwe's review of the v5 OF frontend.
- No functional changes from v1.

v1: https://lore.kernel.org/all/010601a126bfbf4d-27ca6d8c-0c4e-4191-a92d-6dc7d3fbac60-000000@ap-northeast-1.amazonses.com/

OF frontend review:
https://lore.kernel.org/all/aszWnnc6RR9d5SYw@monoceros/

Best regards,
Jizhou Tong

Jizhou Tong (6):
HID: spi-hid: isolate DMA transfers from protocol buffers
HID: spi-hid: add spi-mem quad-SPI transfers
HID: spi-hid: assemble bounded fragmented input reports
dt-bindings: input: describe the Romulus13 QSPI touchpad
HID: spi-hid: support the Romulus13 QSPI touchpad
HID: spi-hid: expose readiness for reset diagnostics

 .../ABI/testing/sysfs-bus-spi-devices-spi-hid |  30 +++
 .../input/microsoft,romulus13-touchpad.yaml   |  86 ++++++++
 MAINTAINERS                                   |   7 +
 drivers/hid/spi-hid/Kconfig                   |   2 +-
 drivers/hid/spi-hid/spi-hid-acpi.c            |  28 +--
 drivers/hid/spi-hid/spi-hid-core.c            | 207 ++++++++++++++----
 drivers/hid/spi-hid/spi-hid-core.h            |   8 +
 drivers/hid/spi-hid/spi-hid-of.c              |  55 +++--
 drivers/hid/spi-hid/spi-hid.h                 |   8 +-
 9 files changed, 355 insertions(+), 76 deletions(-)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-spi-devices-spi-hid
 create mode 100644 Documentation/devicetree/bindings/input/microsoft,romulus13-touchpad.yaml

base-commit: ee9c669f9bf5fd2c24206746ded9382fe810df89
prerequisite-patch-id: b401890d766019f0677a572f18bb389cd8827721
prerequisite-patch-id: beaf91cb29f3a735559a3251092ac0ac17810b10
prerequisite-patch-id: 909ee816b83e0bc8bb4b560f8c7cc0c8109c4516
prerequisite-patch-id: 8b60145a9a853a32881af27181c26dff0e82a33e
prerequisite-patch-id: bcb36384a391e48222f0924b0c571fb0eac19ae7
prerequisite-patch-id: 816dfccf7d35ea041c0d7c08c7bfbe061a45c7dc
prerequisite-patch-id: 67a41d4cb6bfa41248d89a8f967d4414cff2da2e
prerequisite-patch-id: d0bf155ff4d70a3aeb88ec0232f04a4b2edb3598
prerequisite-patch-id: f18d9032e5ed5713beb37527dba225ec79589b31
prerequisite-patch-id: 854affdd16efb5696f9ed1d2a8cdb50dababe724
prerequisite-patch-id: 248c986cba937716c739b804039376ac3823f618
prerequisite-patch-id: 24df0c559e4b401af9acd4bc7bad99dd4b4724d4
prerequisite-patch-id: 2827fd11c245e7f91c73506b9c7c392b44f8d9f3

Signature · 1 line
2.53.0
#39 lore raw

RFC PATCH v2 1/6 HID: spi-hid: isolate DMA transfers from protocol buffers

The body starts four bytes into the input buffer. Receiving DMA directly into it can share a cache line with the header and does not meet controllers that require aligned DMA buffers. Use separate aligned TX and RX regions under io_lock, and copy received data only after a successful transfer. Keep protocol parsing buffers away from streaming DMA.

Overallocate the managed storage and align the DMA view explicitly to ARCH_DMA_MINALIGN, keeping the allocation base for realloc/free. Publish the new transfer capacity only after allocation succeeds.

---
 drivers/hid/spi-hid/spi-hid-core.c | 33 ++++++++++++++++++++++++------
 drivers/hid/spi-hid/spi-hid-core.h |  3 +++
 2 files changed, 30 insertions(+), 6 deletions(-)
drivers/hid/spi-hid/spi-hid-core.c +27 -6
index 0046dfdd82677bb55c5a0b6742cdfaa8fec224c6..65a1cd2c72e8025f50a188e17afd2fa5b359abb1 100644
@@ -193,14 +193,19 @@ static void spi_hid_populate_output_header(u8 *buf,
 static int spi_hid_input_sync(struct spi_hid *shid, void *buf, u16 length,
 			      bool is_header)
 {
+	u8 *rx = shid->xfer_buf + ARCH_DMA_MINALIGN;
 	int error;
 
-	shid->input_transfer[0].tx_buf = is_header ?
-					 shid->read_approval_header :
-					 shid->read_approval_body;
+	if (length > shid->xfer_size - ARCH_DMA_MINALIGN)
+		return -EMSGSIZE;
+
+	memcpy(shid->xfer_buf, is_header ? shid->read_approval_header :
+					 shid->read_approval_body,
+	       SPI_HID_READ_APPROVAL_LEN);
+	shid->input_transfer[0].tx_buf = shid->xfer_buf;
 	shid->input_transfer[0].len = SPI_HID_READ_APPROVAL_LEN;
 
-	shid->input_transfer[1].rx_buf = buf;
+	shid->input_transfer[1].rx_buf = rx;
 	shid->input_transfer[1].len = length;
 
 	spi_message_init_with_transfers(&shid->input_message,
@@ -219,6 +224,7 @@ static int spi_hid_input_sync(struct spi_hid *shid, void *buf, u16 length,
 		return error;
 	}
 
+	memcpy(buf, rx, length);
 	return 0;
 }
 
@@ -226,7 +232,11 @@ static int spi_hid_output(struct spi_hid *shid, const void *buf, u16 length)
 {
 	int error;
 
-	error = spi_write(shid->spi, buf, length);
+	if (length > shid->xfer_size)
+		return -EMSGSIZE;
+
+	memcpy(shid->xfer_buf, buf, length);
+	error = spi_write(shid->spi, shid->xfer_buf, length);
 
 	if (error) {
 		shid->bus_error_count++;
@@ -1210,6 +1220,7 @@ static int spi_hid_alloc_buffers(struct spi_hid *shid, size_t report_size)
 	int inbufsize = round_up(sizeof(shid->input->header) +
 				 sizeof(shid->input->body) + report_size, 4);
 	int outbufsize = round_up(sizeof(shid->output->header) + report_size, 4);
+	size_t xfer_size;
 	void *tmp;
 
 	tmp = devm_krealloc(dev, shid->output, outbufsize, GFP_KERNEL | __GFP_ZERO);
@@ -1227,8 +1238,18 @@ static int spi_hid_alloc_buffers(struct spi_hid *shid, size_t report_size)
 		return -ENOMEM;
 	shid->response = tmp;
 
-	if (!shid->output || !shid->input || !shid->response)
+	/* io_lock serializes the separate, DMA-aligned TX and RX regions. */
+	xfer_size = ALIGN(max(inbufsize, outbufsize), ARCH_DMA_MINALIGN) +
+		    ARCH_DMA_MINALIGN;
+	/* Keep ownership separate from the explicitly aligned DMA view. */
+	tmp = devm_krealloc(dev, shid->xfer_alloc,
+			    xfer_size + ARCH_DMA_MINALIGN - 1,
+			    GFP_KERNEL | __GFP_ZERO);
+	if (!tmp)
 		return -ENOMEM;
+	shid->xfer_alloc = tmp;
+	shid->xfer_buf = PTR_ALIGN(tmp, ARCH_DMA_MINALIGN);
+	shid->xfer_size = xfer_size;
 
 	shid->bufsize = report_size;
 
drivers/hid/spi-hid/spi-hid-core.h +3 -0
index 631897dd9e8a2f7a7c0ca29a5ae6e79e83e12579..9cbe2e57400443bd68056057d08f97c39c403a70 100644
@@ -44,6 +44,9 @@ struct spi_hid_device_descriptor {
 
 /* Driver context */
 struct spi_hid {
+	void *xfer_alloc;
+	u8 *xfer_buf;
+	size_t xfer_size;
 	struct spi_device	*spi;	/* spi device. */
 	struct hid_device	*hid;	/* pointer to corresponding HID dev. */
 
Signature · 1 line
2.53.0
#40 lore raw

RFC PATCH v2 2/6 HID: spi-hid: add spi-mem quad-SPI transfers

In reply to Jizhou Tong

Use explicit opcode, address, dummy and data phases for the 1-4-4 reads and 1-4-4 writes used by the Romulus touchpad. Four quad dummy bytes provide the eight read dummy clocks. The output data phase excludes the opcode and address already supplied to spi-mem.

Register the frontends through spi-mem so controllers exposing memory operations can execute the transactions. Single-lane devices retain their ordinary spi_sync/spi_write transfers. Reject bus-width combinations this transport cannot encode.

Only quad writes require the four-byte opcode/address prefix; preserve the existing short-write behavior for single-lane SPI.

Controllers do not need mem_ops to bind these frontends. Single-lane transfers bypass spi_mem_exec_op(); for supported memory operations, spi-mem can fall back to ordinary SPI messages when mem_ops are absent. The new SPI_MEM Kconfig dependency remains required.

---
 drivers/hid/spi-hid/Kconfig        |  2 +-
 drivers/hid/spi-hid/spi-hid-acpi.c | 28 ++++++-----
 drivers/hid/spi-hid/spi-hid-core.c | 80 ++++++++++++++++++++++--------
 drivers/hid/spi-hid/spi-hid-core.h |  1 +
 drivers/hid/spi-hid/spi-hid-of.c   | 35 ++++++-------
 drivers/hid/spi-hid/spi-hid.h      |  6 +--
 6 files changed, 96 insertions(+), 56 deletions(-)
drivers/hid/spi-hid/Kconfig +1 -1
index 656dbda1c5d873e5bc6c87dda1fbe81460425d30..c84da18ab82819b019cd58a2d3eb9a7b0f6417b0 100644
@@ -6,7 +6,7 @@
 menuconfig SPI_HID
 	tristate "SPI HID support"
 	default y
-	depends on SPI
+	depends on SPI && SPI_MEM
 
 if SPI_HID
 
drivers/hid/spi-hid/spi-hid-acpi.c +15 -13
index a52cafe86c052f0ab8c4761c43355969eb689157..790342073e6f53d61a193429094e965c7819aeda 100644
@@ -194,8 +194,9 @@ static void spi_hid_acpi_sleep_minimal_reset_delay(struct spihid_ops *ops)
 	fsleep(1000 * conf->minimal_reset_delay_ms);
 }
 
-static int spi_hid_acpi_probe(struct spi_device *spi)
+static int spi_hid_acpi_probe(struct spi_mem *mem)
 {
+	struct spi_device *spi = mem->spi;
 	struct device *dev = &spi->dev;
 	struct acpi_device *adev;
 	struct spi_hid_acpi_config *config;
@@ -232,7 +233,7 @@ static int spi_hid_acpi_probe(struct spi_device *spi)
 		return error;
 	}
 
-	return spi_hid_core_probe(spi, &config->ops, &config->property_conf);
+	return spi_hid_core_probe(mem, &config->ops, &config->property_conf);
 }
 
 static const struct acpi_device_id spi_hid_acpi_match[] = {
@@ -242,20 +243,21 @@ static const struct acpi_device_id spi_hid_acpi_match[] = {
 };
 MODULE_DEVICE_TABLE(acpi, spi_hid_acpi_match);
 
-static struct spi_driver spi_hid_acpi_driver = {
-	.driver = {
-		.name	= "spi_hid_acpi",
-		.owner	= THIS_MODULE,
-		.pm	= &spi_hid_core_pm,
-		.acpi_match_table = spi_hid_acpi_match,
-		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
-		.dev_groups = spi_hid_groups,
+static struct spi_mem_driver spi_hid_acpi_driver = {
+	.spidrv = {
+		.driver = {
+			.name = "spi_hid_acpi",
+			.pm = &spi_hid_core_pm,
+			.acpi_match_table = spi_hid_acpi_match,
+			.probe_type = PROBE_PREFER_ASYNCHRONOUS,
+			.dev_groups = spi_hid_groups,
+		},
 	},
-	.probe		= spi_hid_acpi_probe,
-	.remove		= spi_hid_core_remove,
+	.probe = spi_hid_acpi_probe,
+	.remove = spi_hid_core_remove,
 };
 
-module_spi_driver(spi_hid_acpi_driver);
+module_spi_mem_driver(spi_hid_acpi_driver);
 
 MODULE_DESCRIPTION("HID over SPI ACPI transport driver");
 MODULE_AUTHOR("Angela Czubak <[email protected]>");
drivers/hid/spi-hid/spi-hid-core.c +60 -20

202 lines not on this page · raw message

drivers/hid/spi-hid/spi-hid-core.h +1 -0
index 9cbe2e57400443bd68056057d08f97c39c403a70..554288dd17631edc1a503755b04170c04501bcb7 100644
@@ -44,6 +44,7 @@ struct spi_hid_device_descriptor {
 
 /* Driver context */
 struct spi_hid {
+	struct spi_mem *mem;
 	void *xfer_alloc;
 	u8 *xfer_buf;
 	size_t xfer_size;
drivers/hid/spi-hid/spi-hid-of.c +16 -19
index 605edecc4f7ee0423c37f0960cd4974b04ab387c..51bcb1a1b2a85480044c6514890f790e2930f523 100644
@@ -175,8 +175,9 @@ static void spi_hid_of_sleep_minimal_reset_delay(struct spihid_ops *ops)
 	fsleep(1000 * conf->timing_data->minimal_reset_delay_ms);
 }
 
-static int spi_hid_of_probe(struct spi_device *spi)
+static int spi_hid_of_probe(struct spi_mem *mem)
 {
+	struct spi_device *spi = mem->spi;
 	struct device *dev = &spi->dev;
 	struct spi_hid_of_config *config;
 	int error;
@@ -197,16 +198,11 @@ static int spi_hid_of_probe(struct spi_device *spi)
 	if (!config->timing_data)
 		config->timing_data = &timing_data;
 
-	/*
-	 * FIXME: multi-SPI not supported. Once it is, derive the
-	 * HID-over-SPI flags from spi->mode.
-	 */
-
 	error = spi_hid_of_populate_config(config, dev);
 	if (error)
 		return dev_err_probe(dev, error, "Unable to populate config data\n");
 
-	return spi_hid_core_probe(spi, &config->ops, &config->property_conf);
+	return spi_hid_core_probe(mem, &config->ops, &config->property_conf);
 }
 
 static const struct of_device_id spi_hid_of_match[] = {
@@ -222,21 +218,22 @@ static const struct spi_device_id spi_hid_of_id_table[] = {
 };
 MODULE_DEVICE_TABLE(spi, spi_hid_of_id_table);
 
-static struct spi_driver spi_hid_of_driver = {
-	.driver = {
-		.name	= "spi_hid_of",
-		.owner	= THIS_MODULE,
-		.pm	= &spi_hid_core_pm,
-		.of_match_table = spi_hid_of_match,
-		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
-		.dev_groups = spi_hid_groups,
+static struct spi_mem_driver spi_hid_of_driver = {
+	.spidrv = {
+		.driver = {
+			.name = "spi_hid_of",
+			.pm = &spi_hid_core_pm,
+			.of_match_table = spi_hid_of_match,
+			.probe_type = PROBE_PREFER_ASYNCHRONOUS,
+			.dev_groups = spi_hid_groups,
+		},
+		.id_table = spi_hid_of_id_table,
 	},
-	.probe		= spi_hid_of_probe,
-	.remove		= spi_hid_core_remove,
-	.id_table	= spi_hid_of_id_table,
+	.probe = spi_hid_of_probe,
+	.remove = spi_hid_core_remove,
 };
 
-module_spi_driver(spi_hid_of_driver);
+module_spi_mem_driver(spi_hid_of_driver);
 
 MODULE_DESCRIPTION("HID over SPI OF transport driver");
 MODULE_AUTHOR("Dmitry Antipov <[email protected]>");
drivers/hid/spi-hid/spi-hid.h +3 -3
index 17b2fdf192ed2cf40db0638874112c72595fdd6d..dd3a851777a22eefa9005dc6f7a18bf62c5ac6b3 100644
@@ -7,7 +7,7 @@
 #ifndef SPI_HID_H
 #define SPI_HID_H
 
-#include <linux/spi/spi.h>
+#include <linux/spi/spi-mem.h>
 #include <linux/sysfs.h>
 
 /* struct spi_hid_conf - Conf provided to the core */
@@ -35,10 +35,10 @@ struct spihid_ops {
 	void (*sleep_minimal_reset_delay)(struct spihid_ops *ops);
 };
 
-int spi_hid_core_probe(struct spi_device *spi, struct spihid_ops *ops,
+int spi_hid_core_probe(struct spi_mem *mem, struct spihid_ops *ops,
 		       struct spi_hid_conf *conf);
 
-void spi_hid_core_remove(struct spi_device *spi);
+int spi_hid_core_remove(struct spi_mem *mem);
 
 extern const struct attribute_group *spi_hid_groups[];
 extern const struct dev_pm_ops spi_hid_core_pm;
Signature · 1 line
2.53.0
#41 lore raw

RFC PATCH v2 3/6 HID: spi-hid: assemble bounded fragmented input reports

In reply to Jizhou Tong

Keep fragment lengths separate from complete HID report lengths. Assemble DATA reports into a dedicated buffer, enforce the declared content length and final padding, and reject fragmented command responses. Discard incomplete reports after a one-second gap or during error recovery and suspend.

Require a body header before parsing and bound allocations to 8 KiB. Preserve the v5 request serialization and response matching.

Use the existing max_fragment_length descriptor field for each fragment; max_input_length describes a complete input report and remains distinct.

---
 drivers/hid/spi-hid/spi-hid-core.c | 70 +++++++++++++++++++++++++-----
 drivers/hid/spi-hid/spi-hid-core.h |  4 ++
 2 files changed, 63 insertions(+), 11 deletions(-)
drivers/hid/spi-hid/spi-hid-core.c +59 -11
index 9d02c156fcd641e9c944c0dfe08b5aad57a97f6a..2778d147b202ab663928a851198eca8fe628b16a 100644
@@ -146,7 +146,6 @@ static void spi_hid_parse_dev_desc(const struct hidspi_dev_descriptor *raw,
 	desc->max_input_length = le16_to_cpu(raw->max_input_len);
 	desc->max_output_length = le16_to_cpu(raw->max_output_len);
 
-	/* FIXME: multi-fragment not supported, field below not used */
 	desc->max_fragment_length = le16_to_cpu(raw->max_frag_len);
 
 	desc->vendor_id = le16_to_cpu(raw->vendor_id);
@@ -335,6 +334,7 @@ static int spi_hid_suspend(struct spi_hid *shid)
 	 */
 	disable_irq(shid->spi->irq);
 	cancel_work_sync(&shid->reset_work);
+	shid->fragment_len = 0;
 
 	guard(mutex)(&shid->power_lock);
 	if (!device_may_wakeup(dev)) {
@@ -468,6 +468,7 @@ static void spi_hid_error_handler(struct spi_hid *shid)
 		return;
 
 	disable_irq(shid->spi->irq);
+	shid->fragment_len = 0;
 
 	if (shid->reset_attempts++ >= SPI_HID_MAX_RESET_ATTEMPTS) {
 		dev_err(dev, "unresponsive device, aborting\n");
@@ -1039,6 +1040,48 @@ static int spi_hid_process_input_report(struct spi_hid *shid,
 	return 0;
 }
 
+/* Only DATA reports may be fragmented; command responses remain atomic. */
+static int spi_hid_accept_fragment(struct spi_hid *shid,
+				   const struct spi_hid_input_header *header)
+{
+	struct spi_hid_input_report body;
+	u16 len = header->report_length;
+	unsigned int total;
+	int error;
+
+	if (shid->fragment_len &&
+	    time_after(jiffies, shid->fragment_time + msecs_to_jiffies(1000)))
+		goto invalid;
+	if (!shid->fragment_len) {
+		if (header->last_fragment_flag)
+			return spi_hid_process_input_report(shid, shid->input);
+		spi_hid_populate_input_body(shid->input->body, &body);
+		if (body.report_type != DATA || body.content_length > shid->bufsize)
+			goto invalid;
+		shid->fragment_expected = HIDSPI_INPUT_BODY_SIZE(body.content_length);
+	}
+	total = shid->fragment_len + len;
+	if (total > round_up(shid->bufsize + HIDSPI_INPUT_BODY_HEADER_SIZE, 4) ||
+	    (!header->last_fragment_flag && total >= shid->fragment_expected) ||
+	    (header->last_fragment_flag && total != ALIGN(shid->fragment_expected, 4)))
+		goto invalid;
+	memcpy((u8 *)shid->fragment + offsetof(struct spi_hid_input_buf, body) +
+	       shid->fragment_len, shid->input->body, len);
+	shid->fragment_len = total;
+	shid->fragment_time = jiffies;
+	if (!header->last_fragment_flag)
+		return 0;
+
+	memcpy(shid->fragment->header, shid->input->header, HIDSPI_INPUT_HEADER_SIZE);
+	put_unaligned_le16((total / 4) | BIT(14), &shid->fragment->header[1]);
+	error = spi_hid_process_input_report(shid, shid->fragment);
+	shid->fragment_len = 0;
+	return error;
+invalid:
+	shid->fragment_len = 0;
+	return -EPROTO;
+}
+
 static int spi_hid_bus_validate_header(struct spi_hid *shid,
 				       struct spi_hid_input_header *header)
 {
@@ -1061,24 +1104,20 @@ static int spi_hid_bus_validate_header(struct spi_hid *shid,
 	 * report_length is device-provided and is used as the size of the body
 	 * transfer, so it must never exceed the input buffer.
 	 */
-	if (header->report_length > max_body) {
+	if (header->report_length < HIDSPI_INPUT_BODY_HEADER_SIZE ||
+	    header->report_length > max_body) {
 		dev_err(dev, "Input report too big: %u > %u\n",
 			header->report_length, max_body);
 		return -EMSGSIZE;
 	}
 
-	if (shid->desc.max_input_length != 0 &&
-	    header->report_length > shid->desc.max_input_length) {
+	if (shid->desc.max_fragment_length &&
+	    header->report_length > shid->desc.max_fragment_length) {
 		dev_err(dev, "Input report body size %u > max expected of %u\n",
-			header->report_length, shid->desc.max_input_length);
+			header->report_length, shid->desc.max_fragment_length);
 		return -EMSGSIZE;
 	}
 
-	if (header->last_fragment_flag != 1) {
-		dev_err(dev, "Multi-fragment reports not supported\n");
-		return -EOPNOTSUPP;
-	}
-
 	if (header->sync_const != SPI_HID_INPUT_HEADER_SYNC_BYTE) {
 		dev_err(dev, "Invalid input report sync constant (0x%x)\n",
 			header->sync_const);
@@ -1149,6 +1188,7 @@ static int spi_hid_set_request(struct spi_hid *shid, u8 *arg_buf, u16 arg_len,
 /* Schedule a reset to recover from an error in the IRQ handler. */
 static irqreturn_t spi_hid_irq_error(struct spi_hid *shid)
 {
+	shid->fragment_len = 0;
 	set_bit(SPI_HID_ERROR, &shid->flags);
 	schedule_work(&shid->reset_work);
 
@@ -1231,7 +1271,7 @@ static irqreturn_t spi_hid_dev_irq(int irq, void *_shid)
 		}
 	}
 
-	error = spi_hid_process_input_report(shid, shid->input);
+	error = spi_hid_accept_fragment(shid, &header);
 	if (error) {
 		dev_err(dev, "Failed to process input report: %d\n", error);
 		return spi_hid_irq_error(shid);
@@ -1249,6 +1289,9 @@ static int spi_hid_alloc_buffers(struct spi_hid *shid, size_t report_size)
 	size_t xfer_size;
 	void *tmp;
 
+	if (report_size > SZ_8K)
+		return -EMSGSIZE;
+
 	tmp = devm_krealloc(dev, shid->output, outbufsize, GFP_KERNEL | __GFP_ZERO);
 	if (!tmp)
 		return -ENOMEM;
@@ -1264,6 +1307,11 @@ static int spi_hid_alloc_buffers(struct spi_hid *shid, size_t report_size)
 		return -ENOMEM;
 	shid->response = tmp;
 
+	tmp = devm_krealloc(dev, shid->fragment, inbufsize, GFP_KERNEL | __GFP_ZERO);
+	if (!tmp)
+		return -ENOMEM;
+	shid->fragment = tmp;
+
 	/* io_lock serializes the separate, DMA-aligned TX and RX regions. */
 	xfer_size = ALIGN(max(inbufsize, outbufsize), ARCH_DMA_MINALIGN) +
 		    ARCH_DMA_MINALIGN;
drivers/hid/spi-hid/spi-hid-core.h +4 -0
index 554288dd17631edc1a503755b04170c04501bcb7..635d66daaf456cdb65c57146758ea7c64a1537f7 100644
@@ -61,6 +61,10 @@ struct spi_hid {
 	struct spi_hid_output_buf *output;	/* Output buffer. */
 	struct spi_hid_input_buf *input;	/* Input buffer. */
 	struct spi_hid_input_buf *response;	/* Response buffer. */
+	struct spi_hid_input_buf *fragment;
+	u16 fragment_len;
+	u16 fragment_expected;
+	unsigned long fragment_time;
 
 	struct drm_panel_follower panel_follower;
 	bool	is_panel_follower;
Signature · 1 line
2.53.0
#42 lore raw

RFC PATCH v2 4/6 dt-bindings: input: describe the Romulus13 QSPI touchpad

In reply to Jizhou Tong

Describe the fixed-address HID-over-SPI touchpad on Surface Laptop 7 13.8-inch, with quad address/data transfers, an active-low reset and a 5 V supply.

---
 .../input/microsoft,romulus13-touchpad.yaml   | 86 +++++++++++++++++++
 1 file changed, 86 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/input/microsoft,romulus13-touchpad.yaml
Documentation/devicetree/bindings/input/microsoft,romulus13-touchpad.yaml new file +86 -0
new file mode 100644
index 0000000000000000000000000000000000000000..88e4664c56e7fb1c3f9f5949a3ded2f9d7070604
@@ -0,0 +1,86 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/input/microsoft,romulus13-touchpad.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Microsoft Surface Laptop 7 13.8-inch QSPI touchpad
+
+maintainers:
+  - Jizhou Tong <[email protected]>
+
+description:
+  The touchpad on the 13.8-inch Surface Laptop 7 uses the HID SPI v3
+  protocol over a quad SPI connection. Its supply and board reset states
+  must be controlled during initialization and recovery.
+
+allOf:
+  - $ref: /schemas/spi/spi-peripheral-props.yaml#
+
+properties:
+  compatible:
+    const: microsoft,romulus13-touchpad
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  vdd-supply:
+    description: Touchpad 5 V supply.
+
+  spi-max-frequency:
+    maximum: 20000000
+
+  spi-rx-bus-width:
+    items:
+      - const: 4
+
+  spi-tx-bus-width:
+    items:
+      - const: 4
+
+  reset-gpios:
+    maxItems: 1
+    description: Active-low touchpad reset.
+
+  pinctrl-0: true
+
+  pinctrl-names:
+    items:
+      - const: default
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - vdd-supply
+  - spi-rx-bus-width
+  - spi-tx-bus-width
+  - reset-gpios
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+    #include <dt-bindings/gpio/gpio.h>
+
+    spi {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        touchpad@0 {
+            compatible = "microsoft,romulus13-touchpad";
+            reg = <0>;
+            spi-max-frequency = <20000000>;
+            spi-rx-bus-width = <4>;
+            spi-tx-bus-width = <4>;
+            interrupts-extended = <&tlmm 3 IRQ_TYPE_LEVEL_LOW>;
+            vdd-supply = <&vreg_ts_5p0>;
+            reset-gpios = <&tlmm 120 GPIO_ACTIVE_LOW>;
+            pinctrl-0 = <&touchpad_reset>;
+            pinctrl-names = "default";
+        };
+    };
Signature · 1 line
2.53.0
#43 lore raw

Re: [RFC PATCH v2 4/6] dt-bindings: input: describe the Romulus13 QSPI touchpad

Quoted 5 lines · Jizhou Tong

Describe the fixed-address HID-over-SPI touchpad on Surface Laptop 7 13.8-inch, with quad address/data transfers, an active-low reset and a 5 V supply.

With the changes to the hid-over-spi series that this is based on, can this device now go into hid-over-spi.yaml?

Quoted 54 lines
---
 .../input/microsoft,romulus13-touchpad.yaml   | 86 +++++++++++++++++++
 1 file changed, 86 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/input/microsoft,romulus13-touchpad.yaml
Documentation/devicetree/bindings/input/microsoft,romulus13-touchpad.yaml new file +43 -0
new file mode 100644
index 0000000000000000000000000000000000000000..88e4664c56e7fb1c3f9f5949a3ded2f9d7070604
@@ -0,0 +1,86 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/input/microsoft,romulus13-touchpad.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Microsoft Surface Laptop 7 13.8-inch QSPI touchpad
+
+maintainers:
+  - Jizhou Tong <[email protected]>
+
+description:
+  The touchpad on the 13.8-inch Surface Laptop 7 uses the HID SPI v3
+  protocol over a quad SPI connection. Its supply and board reset states
+  must be controlled during initialization and recovery.
+
+allOf:
+  - $ref: /schemas/spi/spi-peripheral-props.yaml#
+
+properties:
+  compatible:
+    const: microsoft,romulus13-touchpad
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  vdd-supply:
+    description: Touchpad 5 V supply.
+
+  spi-max-frequency:
+    maximum: 20000000
+
+  spi-rx-bus-width:
+    items:
+      - const: 4
+
+  spi-tx-bus-width:
+    items:
+      - const: 4
+

Documentation/devicetree/bindings/input/microsoft,romulus13-touchpad.yaml line 52

Quoted 10 lines
Documentation/devicetree/bindings/input/microsoft,romulus13-touchpad.yaml new file +9 -0
+  reset-gpios:
+    maxItems: 1
+    description: Active-low touchpad reset.
+
 
+  pinctrl-0: true
+
+  pinctrl-names:
+    items:
+      - const: default

Are these even something that needs to be added? I am pretty sure pinctrl stuff is allowed in all nodes without being cited explicitly.

Looks okay to me other than these two things.

pw-bot: changes-requested

Thanks,
Conor.

Quoted 37 lines
Documentation/devicetree/bindings/input/microsoft,romulus13-touchpad.yaml new file +34 -0
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - vdd-supply
+  - spi-rx-bus-width
+  - spi-tx-bus-width
+  - reset-gpios
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+    #include <dt-bindings/gpio/gpio.h>
+
+    spi {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        touchpad@0 {
+            compatible = "microsoft,romulus13-touchpad";
+            reg = <0>;
+            spi-max-frequency = <20000000>;
+            spi-rx-bus-width = <4>;
+            spi-tx-bus-width = <4>;
+            interrupts-extended = <&tlmm 3 IRQ_TYPE_LEVEL_LOW>;
+            vdd-supply = <&vreg_ts_5p0>;
+            reset-gpios = <&tlmm 120 GPIO_ACTIVE_LOW>;
+            pinctrl-0 = <&touchpad_reset>;
+            pinctrl-names = "default";
+        };
+    };
Signature · 1 line
2.53.0
#44 lore raw

RFC PATCH v2 5/6 HID: spi-hid: support the Romulus13 QSPI touchpad

In reply to Jizhou Tong

Provide the verified Romulus opcodes, register addresses and power/reset delays. Reset loses the heatmap mode selected by iptsd, even if the report descriptor is unchanged. Recreate the HID device after the descriptor refresh so hidraw consumers reconnect and initialize that mode again.

Wait for the device descriptor handshake after a power-cycle, including on Romulus, so a missing response still triggers the bounded recovery worker and returns -ETIMEDOUT. Defer READY and the old HID resume callback until re-probe on this device. Apply the same policy when rolling back a failed suspend, retaining the original suspend error. Keep the v5 CRC-based refresh and resume behavior for other hardware.

---
 drivers/hid/spi-hid/spi-hid-core.c | 19 +++++++++++++------
 drivers/hid/spi-hid/spi-hid-of.c   | 20 ++++++++++++++++++++
 drivers/hid/spi-hid/spi-hid.h      |  2 ++
 3 files changed, 35 insertions(+), 6 deletions(-)
drivers/hid/spi-hid/spi-hid-core.c +13 -6
index 2778d147b202ab663928a851198eca8fe628b16a..bd27bc3ea34c5fa65a903dd204800dbdafe3a7bc 100644
@@ -287,8 +287,9 @@ static const char *spi_hid_power_mode_string(enum hidspi_power_state power_state
 
 /*
  * Wait for the device descriptor response after reset; the device then takes
- * requests. Set READY here: the report descriptor check that normally sets it
- * needs power_lock, which callers hold.
+ * requests. Set READY here for devices that reuse their HID instance: the
+ * report descriptor check needs power_lock, which callers hold. Devices that
+ * rebind after reset become ready when their new HID instance is parsed.
  */
 static int spi_hid_wait_for_reset(struct spi_hid *shid)
 {
@@ -304,7 +305,8 @@ static int spi_hid_wait_for_reset(struct spi_hid *shid)
 		return -ETIMEDOUT;
 	}
 
-	set_bit(SPI_HID_READY, &shid->flags);
+	if (!shid->conf->rebind_after_reset)
+		set_bit(SPI_HID_READY, &shid->flags);
 	return 0;
 }
 
@@ -366,7 +368,8 @@ static int spi_hid_suspend(struct spi_hid *shid)
 				shid->ops->deassert_reset(shid->ops);
 				enable_irq(shid->spi->irq);
 				if (shid->hid) {
-					if (!spi_hid_wait_for_reset(shid))
+					if (!spi_hid_wait_for_reset(shid) &&
+					    !shid->conf->rebind_after_reset)
 						hid_driver_reset_resume(shid->hid);
 				}
 				return error;
@@ -431,6 +434,9 @@ static int spi_hid_resume(struct spi_hid *shid)
 			error = spi_hid_wait_for_reset(shid);
 			if (error)
 				return error;
+			/* Refresh will recreate the HID device after the handshake. */
+			if (shid->conf->rebind_after_reset)
+				return 0;
 		}
 		error = hid_driver_reset_resume(shid->hid);
 		if (error) {
@@ -916,8 +922,9 @@ static void spi_hid_refresh_device(struct spi_hid *shid)
 				     (size_t)error);
 	}
 
-	/* Same report descriptor, so no need to create a new hid device. */
-	if (new_crc32 == shid->report_descriptor_crc32) {
+	/* Some devices lose userspace-selected modes even with the same CRC. */
+	if (new_crc32 == shid->report_descriptor_crc32 &&
+	    !shid->conf->rebind_after_reset) {
 		set_bit(SPI_HID_READY, &shid->flags);
 		return;
 	}
drivers/hid/spi-hid/spi-hid-of.c +20 -0
index 51bcb1a1b2a85480044c6514890f790e2930f523..cec149adb505b2e09f210bfb010cb37f618184b1 100644
@@ -56,12 +56,29 @@ static const struct spi_hid_timing_data timing_data = {
 	.minimal_reset_delay_ms = 100,
 };
 
+static const struct spi_hid_timing_data romulus_timing = {
+	.post_power_on_delay_ms = 1000,
+	.minimal_reset_delay_ms = 300,
+};
+
 static int spi_hid_of_populate_config(struct spi_hid_of_config *conf,
 				      struct device *dev)
 {
 	int error;
 	u32 val;
 
+	if (device_is_compatible(dev, "microsoft,romulus13-touchpad")) {
+		conf->property_conf = (struct spi_hid_conf) {
+			.input_report_header_address = 0x1000,
+			.input_report_body_address = 0x1004,
+			.output_report_address = 0x2000,
+			.read_opcode = 0xeb,
+			.write_opcode = 0xe2,
+			.rebind_after_reset = true,
+		};
+		goto power;
+	}
+
 	error = device_property_read_u32(dev, "input-report-header-address",
 					 &val);
 	if (error) {
@@ -98,6 +115,7 @@ static int spi_hid_of_populate_config(struct spi_hid_of_config *conf,
 		return -ENODEV;
 	}
 
+power:
 	conf->supply = devm_regulator_get(dev, "vdd");
 	if (IS_ERR(conf->supply))
 		return dev_err_probe(dev, PTR_ERR(conf->supply),
@@ -206,12 +224,14 @@ static int spi_hid_of_probe(struct spi_mem *mem)
 }
 
 static const struct of_device_id spi_hid_of_match[] = {
+	{ .compatible = "microsoft,romulus13-touchpad", .data = &romulus_timing },
 	{ .compatible = "hid-over-spi", .data = &timing_data },
 	{}
 };
 MODULE_DEVICE_TABLE(of, spi_hid_of_match);
 
 static const struct spi_device_id spi_hid_of_id_table[] = {
+	{ .name = "romulus13-touchpad" },
 	{ "hid", 0 },
 	{ "hid-over-spi", 0 },
 	{ }
drivers/hid/spi-hid/spi-hid.h +2 -0
index dd3a851777a22eefa9005dc6f7a18bf62c5ac6b3..ccbb4b9d3aeefa759a7a824ea28f3f9a5450b3b3 100644
@@ -17,6 +17,8 @@ struct spi_hid_conf {
 	u32 output_report_address;
 	u8 read_opcode;
 	u8 write_opcode;
+	/* Reconnect hidraw clients when reset loses device-specific state. */
+	bool rebind_after_reset;
 };
 
 /**
Signature · 1 line
2.53.0
#45 lore raw

RFC PATCH v2 6/6 HID: spi-hid: expose readiness for reset diagnostics

In reply to Jizhou Tong

Retain the read-only readiness status used to correlate descriptor refresh and hidraw reconnection with iptsd logs. Document it alongside the existing bus, regulator and device-reset counters.

Limit the Romulus maintenance entry to its binding and ABI documentation.

---
 .../ABI/testing/sysfs-bus-spi-devices-spi-hid | 30 +++++++++++++++++++
 MAINTAINERS                                   |  7 +++++
 drivers/hid/spi-hid/spi-hid-core.c            | 11 +++++++
 3 files changed, 48 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-spi-devices-spi-hid
Documentation/ABI/testing/sysfs-bus-spi-devices-spi-hid new file +30 -0
new file mode 100644
index 0000000000000000000000000000000000000000..5c55748a082e028a2fb7a0aadbde5d92eed50950
@@ -0,0 +1,30 @@
+What:		/sys/bus/spi/devices/.../ready
+Date:		October 2026
+Contact:	Jizhou Tong <[email protected]>
+Description:
+		Read-only generic HID SPI readiness: "ready" or "not ready".
+		The device must be ready and opened before input is delivered.
+
+What:		/sys/bus/spi/devices/.../bus_error_count
+Date:		October 2026
+Contact:	Jizhou Tong <[email protected]>
+Description:
+		Read-only pair "COUNT (ERROR)". COUNT accumulates transport
+		failures and invalid input headers since bind and may wrap.
+		ERROR is the last such negative errno. The two fields are
+		sampled independently. Unlike the previous downstream SL7
+		driver, a successful transfer does not clear this counter.
+
+What:		/sys/bus/spi/devices/.../regulator_error_count
+Date:		October 2026
+Contact:	Jizhou Tong <[email protected]>
+Description:
+		Read-only pair "COUNT (ERROR)" of accumulated regulator
+		failures and the last errno. Fields are sampled independently.
+
+What:		/sys/bus/spi/devices/.../device_initiated_reset_count
+Date:		October 2026
+Contact:	Jizhou Tong <[email protected]>
+Description:
+		Read-only unsigned count of reset responses processed while
+		the generic HID ready flag was set. Starts at zero on bind.
MAINTAINERS +7 -0
index c52ec2d7d3c12ace1f6e2f6012a32844bd8f364e..858ec47345400b0cf751e7c8cc13140b462c701a 100644
@@ -11626,6 +11626,13 @@ L:	[email protected]
 S:	Maintained
 F:	drivers/hid/hid-nvidia-shield.c
 
+HID OVER SPI ROMULUS13 INTEGRATION
+M:	Jizhou Tong <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/ABI/testing/sysfs-bus-spi-devices-spi-hid
+F:	Documentation/devicetree/bindings/input/microsoft,romulus13-touchpad.yaml
+
 HID PHOENIX RC FLIGHT CONTROLLER
 M:	Marcus Folkesson <[email protected]>
 L:	[email protected]
drivers/hid/spi-hid/spi-hid-core.c +11 -0
index bd27bc3ea34c5fa65a903dd204800dbdafe3a7bc..167ee347588fd6248c647e8d970324e96d99d8a8 100644
@@ -1568,6 +1568,16 @@ static struct hid_ll_driver spi_hid_ll_driver = {
 	.raw_request = spi_hid_ll_raw_request,
 };
 
+static ssize_t ready_show(struct device *dev,
+			  struct device_attribute *attr, char *buf)
+{
+	struct spi_hid *shid = spi_mem_get_drvdata(dev_get_drvdata(dev));
+
+	return sysfs_emit(buf, "%s\n", test_bit(SPI_HID_READY, &shid->flags) ?
+			  "ready" : "not ready");
+}
+static DEVICE_ATTR_RO(ready);
+
 static ssize_t bus_error_count_show(struct device *dev,
 				    struct device_attribute *attr, char *buf)
 {
@@ -1601,6 +1611,7 @@ static ssize_t device_initiated_reset_count_show(struct device *dev,
 static DEVICE_ATTR_RO(device_initiated_reset_count);
 
 static struct attribute *spi_hid_attrs[] = {
+	&dev_attr_ready.attr,
 	&dev_attr_bus_error_count.attr,
 	&dev_attr_regulator_error_count.attr,
 	&dev_attr_device_initiated_reset_count.attr,
Signature · 1 line
2.53.0
#46 lore raw

v3 cover HID: spi-hid: add quad-SPI support for Romulus13

In reply to Jizhou Tong

Hi Jingyuan, Dale,

This RFC adds quad-SPI support for the Surface Laptop 7 13.8-inch (Romulus13) touchpad to Jingyuan's v7 driver [1], on top of the two reset GPIO error fixes sent separately.

It adds aligned transport buffers, spi-mem 1-4-4 transfers, bounded input fragment assembly, and Romulus match data. After a device reset, Romulus recreates the HID device so iptsd reapplies the touchpad mode without restarting iptsd. Single-lane devices retain their ordinary SPI transfers.

Following Conor's review [2], the compatible and quad-width constraints now extend hid-over-spi.yaml. There is no separate Romulus binding or redundant declaration of the globally allowed pinctrl properties.

The final driver source matches the SPI-HID source in my tested downstream SL7 kernel. Touchpad input and suspend/resume work there with the downstream Qualcomm GENI/GPI QSPI controller and board changes. Those dependencies are being prepared separately from this HID transport RFC.

The core, ACPI and OF modules build with W=1 against the prepared SL7 kernel build. Software tests cover header and fragment bounds, DMA buffer alignment and allocation failures, SPI/QSPI transfer phases, reset errors, and resume/re-probe ordering. The common binding passes schema validation and compiled positive/negative examples, including both IRQ spellings.

Changes since v2:
- Rebase onto v7's compatible data, descriptor and PM handling.
- Drop the readiness/error-counter ABI; keep v7's removal of counters.
- Extend the common binding and remove redundant pinctrl declarations.
- Keep quad-only output framing checks out of the single-lane path.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/20261028-punctured-flagpole-c7ec4cfc4609@spud/

Jizhou Tong (5):
HID: spi-hid: isolate DMA transfers from protocol buffers
HID: spi-hid: add spi-mem quad-SPI transfers
HID: spi-hid: assemble bounded fragmented input reports
dt-bindings: input: hid-over-spi: add the Surface Laptop 7 touchpad
HID: spi-hid: support the Romulus13 QSPI touchpad

 .../bindings/input/hid-over-spi.yaml          |  40 ++++
 drivers/hid/spi-hid/Kconfig                   |   2 +-
 drivers/hid/spi-hid/spi-hid-acpi.c            |  26 +--
 drivers/hid/spi-hid/spi-hid-core.c            | 183 +++++++++++++++---
 drivers/hid/spi-hid/spi-hid-core.h            |   8 +
 drivers/hid/spi-hid/spi-hid-of.c              |  44 +++--
 drivers/hid/spi-hid/spi-hid.h                 |   8 +-
 7 files changed, 249 insertions(+), 62 deletions(-)

base-commit: ee9c669f9bf5fd2c24206746ded9382fe810df89
prerequisite-patch-id: b401890d766019f0677a572f18bb389cd8827721
prerequisite-patch-id: beaf91cb29f3a735559a3251092ac0ac17810b10
prerequisite-patch-id: e7eba48cebafeee52eae204c950d7c4f8386f0bd
prerequisite-patch-id: 41ca1ba441d0246d5bf6a4befb26b1c65a596b95
prerequisite-patch-id: 84023dc857708fbb763a889ac060acc7d4387264
prerequisite-patch-id: f9b381e783ceb6b608c27e3b6299c591ac9be769
prerequisite-patch-id: ae5c9959b2923b37a5a650f04e93c662b9e10592
prerequisite-patch-id: 44a8965a58c0bfb0d5cf758790f407d136451956
prerequisite-patch-id: 207558a5e93c1074b4dc1b568d70d62a9ffe542b
prerequisite-patch-id: 59919db0f4e3fbaa1908a0b311fcd53837c5aac3
prerequisite-patch-id: b3706d31329e8a5829627fd0eb795c24aeb389cd
prerequisite-patch-id: 24df0c559e4b401af9acd4bc7bad99dd4b4724d4
prerequisite-patch-id: 3163173e775b674a9f6b8b5f683e0a522e2ea4a0

#47 lore raw

RFC PATCH v3 1/5 HID: spi-hid: isolate DMA transfers from protocol buffers

SPI controllers may DMA-map the transfer buffers. Pointers into the packed protocol buffers do not guarantee DMA alignment or isolation from adjacent data during cache maintenance.

Allocate a separate transfer buffer with explicitly aligned TX and RX regions, and copy protocol data into and out of it under io_lock. Keep the original allocation pointer for managed ownership and publish the new capacity only after allocation succeeds.

---
 drivers/hid/spi-hid/spi-hid-core.c | 33 ++++++++++++++++++++++++------
 drivers/hid/spi-hid/spi-hid-core.h |  3 +++
 2 files changed, 30 insertions(+), 6 deletions(-)
drivers/hid/spi-hid/spi-hid-core.c +27 -6
index f2b2106600..db04322517 100644
@@ -193,14 +193,19 @@ static void spi_hid_populate_output_header(u8 *buf,
 static int spi_hid_input_sync(struct spi_hid *shid, void *buf, u16 length,
 			      bool is_header)
 {
+	u8 *rx = shid->xfer_buf + ARCH_DMA_MINALIGN;
 	int error;
 
-	shid->input_transfer[0].tx_buf = is_header ?
-					 shid->read_approval_header :
-					 shid->read_approval_body;
+	if (length > shid->xfer_size - ARCH_DMA_MINALIGN)
+		return -EMSGSIZE;
+
+	memcpy(shid->xfer_buf, is_header ? shid->read_approval_header :
+					 shid->read_approval_body,
+	       SPI_HID_READ_APPROVAL_LEN);
+	shid->input_transfer[0].tx_buf = shid->xfer_buf;
 	shid->input_transfer[0].len = SPI_HID_READ_APPROVAL_LEN;
 
-	shid->input_transfer[1].rx_buf = buf;
+	shid->input_transfer[1].rx_buf = rx;
 	shid->input_transfer[1].len = length;
 
 	spi_message_init_with_transfers(&shid->input_message,
@@ -217,12 +222,17 @@ static int spi_hid_input_sync(struct spi_hid *shid, void *buf, u16 length,
 		return error;
 	}
 
+	memcpy(buf, rx, length);
 	return 0;
 }
 
 static int spi_hid_output(struct spi_hid *shid, const void *buf, u16 length)
 {
-	return spi_write(shid->spi, buf, length);
+	if (length > shid->xfer_size)
+		return -EMSGSIZE;
+
+	memcpy(shid->xfer_buf, buf, length);
+	return spi_write(shid->spi, shid->xfer_buf, length);
 }
 
 static const char *spi_hid_power_mode_string(enum hidspi_power_state power_state)
@@ -1203,6 +1213,7 @@ static int spi_hid_alloc_buffers(struct spi_hid *shid, size_t report_size)
 	int inbufsize = round_up(sizeof(shid->input->header) +
 				 sizeof(shid->input->body) + report_size, 4);
 	int outbufsize = round_up(sizeof(shid->output->header) + report_size, 4);
+	size_t xfer_size;
 	void *tmp;
 
 	tmp = devm_krealloc(dev, shid->output, outbufsize, GFP_KERNEL | __GFP_ZERO);
@@ -1220,8 +1231,18 @@ static int spi_hid_alloc_buffers(struct spi_hid *shid, size_t report_size)
 		return -ENOMEM;
 	shid->response = tmp;
 
-	if (!shid->output || !shid->input || !shid->response)
+	/* io_lock serializes the separate, DMA-aligned TX and RX regions. */
+	xfer_size = ALIGN(max(inbufsize, outbufsize), ARCH_DMA_MINALIGN) +
+		    ARCH_DMA_MINALIGN;
+	/* Keep ownership separate from the explicitly aligned DMA view. */
+	tmp = devm_krealloc(dev, shid->xfer_alloc,
+			    xfer_size + ARCH_DMA_MINALIGN - 1,
+			    GFP_KERNEL | __GFP_ZERO);
+	if (!tmp)
 		return -ENOMEM;
+	shid->xfer_alloc = tmp;
+	shid->xfer_buf = PTR_ALIGN(tmp, ARCH_DMA_MINALIGN);
+	shid->xfer_size = xfer_size;
 
 	shid->bufsize = report_size;
 
drivers/hid/spi-hid/spi-hid-core.h +3 -0
index 28e9880ad4..3419472844 100644
@@ -44,6 +44,9 @@ struct spi_hid_device_descriptor {
 
 /* Driver context */
 struct spi_hid {
+	void *xfer_alloc;
+	u8 *xfer_buf;
+	size_t xfer_size;
 	struct spi_device	*spi;	/* spi device. */
 	struct hid_device	*hid;	/* pointer to corresponding HID dev. */
 
#48 lore raw

Re: [RFC PATCH v3 1/5] HID: spi-hid: isolate DMA transfers from protocol buffers

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] HID: spi-hid: TX/RX overlap in spi_hid_input_sync() if ARCH_DMA_MINALIGN < 5 --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] HID: spi-hid: TX/RX overlap in spi_hid_input_sync() if ARCH_DMA_MINALIGN < 5 --

commit 62a7cf26838a2cb27b111ff47187ee064977f44a
Author: Jizhou Tong <[email protected]>

HID: spi-hid: isolate DMA transfers from protocol buffers

This commit allocates a separate DMA-aligned transfer buffer in struct spi_hid and copies TX and RX protocol data through it under io_lock to avoid DMA mapping unaligned or adjacent protocol buffers.

Quoted 15 lines
drivers/hid/spi-hid/spi-hid-core.c +2 -3
index f2b21066003cb..db0432251732e 100644
@@ -193,14 +193,19 @@ static void spi_hid_populate_output_header(u8 *buf,
 static int spi_hid_input_sync(struct spi_hid *shid, void *buf, u16 length,
 			      bool is_header)
 {
+	u8 *rx = shid->xfer_buf + ARCH_DMA_MINALIGN;
 	int error;
 
-	shid->input_transfer[0].tx_buf = is_header ?
-					 shid->read_approval_header :
-					 shid->read_approval_body;
+	if (length > shid->xfer_size - ARCH_DMA_MINALIGN)

drivers/hid/spi-hid/spi-hid-core.c static void spi_hid_populate_output_header(u8 *buf, · line 208

Quoted 10 lines
drivers/hid/spi-hid/spi-hid-core.c +7 -1
+		return -EMSGSIZE;
+
+	memcpy(shid->xfer_buf, is_header ? shid->read_approval_header :
+					 shid->read_approval_body,
+	       SPI_HID_READ_APPROVAL_LEN);
+	shid->input_transfer[0].tx_buf = shid->xfer_buf;
 	shid->input_transfer[0].len = SPI_HID_READ_APPROVAL_LEN;
 
-	shid->input_transfer[1].rx_buf = buf;
+	shid->input_transfer[1].rx_buf = rx;

[Severity: Medium]
Can shid->input_transfer[0].tx_buf and shid->input_transfer[1].rx_buf overlap in spi_hid_input_sync() when ARCH_DMA_MINALIGN is smaller than SPI_HID_READ_APPROVAL_LEN?

In drivers/hid/spi-hid/spi-hid-core.h, SPI_HID_READ_APPROVAL_LEN is 5:

#define SPI_HID_READ_APPROVAL_LEN 5

In include/linux/cache.h, architectures that do not define ARCH_DMA_MINALIGN fall back to __alignof__(unsigned long long):

#ifdef ARCH_DMA_MINALIGN
#define ARCH_HAS_DMA_MINALIGN
#else
#define ARCH_DMA_MINALIGN __alignof__(unsigned long long) #endif

On 32-bit architectures where 64-bit integers have 4-byte or 2-byte alignment (such as OpenRISC or m68k), ARCH_DMA_MINALIGN evaluates to 4 or 2.

When spi_hid_dev_irq() calls spi_hid_input_sync(), the 5-byte read approval is copied to shid->xfer_buf[0..4] for input_transfer[0].tx_buf, while rx for input_transfer[1].rx_buf is placed at shid->xfer_buf + ARCH_DMA_MINALIGN. If ARCH_DMA_MINALIGN is 4, both transfers in the same spi_message share shid->xfer_buf[4] (and spi_hid_alloc_buffers() only reserves a single ARCH_DMA_MINALIGN unit for the TX region in shid->xfer_size).

Should spi_hid_alloc_buffers() and spi_hid_input_sync() use ALIGN(SPI_HID_READ_APPROVAL_LEN, ARCH_DMA_MINALIGN) for the TX region size and the rx offset instead of bare ARCH_DMA_MINALIGN?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/010601a1ad274a9c-ca2fc8f5-d3fb-435d-8572-c0d3eb701649-000000@ap-northeast-1.amazonses.com?part=1
#49 lore raw

Re: [RFC PATCH v3 1/5] HID: spi-hid: isolate DMA transfers from protocol buffers

Hi,

The overlap is real when ARCH_DMA_MINALIGN is smaller than the five-byte read approval. I reproduced it with the actual input-transfer function using alignment values of two and four. My earlier tests started at eight and missed these cases.

The correction below rounds the full approval length up to the DMA alignment and uses that value for the RX offset, bounds check and allocation. It is an incremental diff against v3. I have also folded it into patch 1 of a local v4 draft and will collect further review before resending the complete series.

The actual allocator and single-lane/quad transfer functions now pass ASan/UBSan tests at alignments 2, 4, 8, 16, 32, 64, 128 and 256. These check non-overlap, exact RX capacity, allocation failures and rollback. The v3 allocator and transfer path fail the new small-alignment tests. The SPI-HID core, ACPI and OF modules build with W=1 on ARM64.

The 128-byte layout used on my SL7 is unchanged.

---

drivers/hid/spi-hid/spi-hid-core.c +4 -3
index 901c8d9ace..d0ef3e7106 100644
@@ -67,6 +67,7 @@
 /* Protocol message size constants */
 #define SPI_HID_READ_APPROVAL_LEN		5
 #define SPI_HID_OUTPUT_HEADER_LEN		8
+#define SPI_HID_RX_OFFSET	ALIGN(SPI_HID_READ_APPROVAL_LEN, ARCH_DMA_MINALIGN)
 
 /* flags */
 /*
@@ -192,10 +193,10 @@ static void spi_hid_populate_output_header(u8 *buf,
 static int spi_hid_input_sync(struct spi_hid *shid, void *buf, u16 length,
 			      bool is_header)
 {
-	u8 *rx = shid->xfer_buf + ARCH_DMA_MINALIGN;
+	u8 *rx = shid->xfer_buf + SPI_HID_RX_OFFSET;
 	int error;
 
-	if (length > shid->xfer_size - ARCH_DMA_MINALIGN)
+	if (length > shid->xfer_size - SPI_HID_RX_OFFSET)
 		return -EMSGSIZE;
 
 	memcpy(shid->xfer_buf, is_header ? shid->read_approval_header :
@@ -1322,7 +1323,7 @@ static int spi_hid_alloc_buffers(struct spi_hid *shid, size_t report_size)
 
 	/* io_lock serializes the separate, DMA-aligned TX and RX regions. */
 	xfer_size = ALIGN(max(inbufsize, outbufsize), ARCH_DMA_MINALIGN) +
-		    ARCH_DMA_MINALIGN;
+		    SPI_HID_RX_OFFSET;
 	/* Keep ownership separate from the explicitly aligned DMA view. */
 	tmp = devm_krealloc(dev, shid->xfer_alloc,
 			    xfer_size + ARCH_DMA_MINALIGN - 1,
#50 lore raw

RFC PATCH v3 2/5 HID: spi-hid: add spi-mem quad-SPI transfers

In reply to Jizhou Tong

The Romulus13 touchpad uses single-lane opcodes followed by quad-lane address and data phases. Describe these as spi-mem operations so the controller receives the command, address, dummy and data phases explicitly.

Register both frontends through spi-mem while preserving ordinary SPI transfers for single-lane devices. Controllers without mem_ops can use the spi-mem fallback. Reject unsupported bus-width combinations and require the four-byte opcode/address prefix only for quad output.

---
 drivers/hid/spi-hid/Kconfig        |  2 +-
 drivers/hid/spi-hid/spi-hid-acpi.c | 26 +++++-----
 drivers/hid/spi-hid/spi-hid-core.c | 77 +++++++++++++++++++++++-------
 drivers/hid/spi-hid/spi-hid-core.h |  1 +
 drivers/hid/spi-hid/spi-hid-of.c   | 29 +++++------
 drivers/hid/spi-hid/spi-hid.h      |  6 +--
 6 files changed, 92 insertions(+), 49 deletions(-)
drivers/hid/spi-hid/Kconfig +1 -1
index 656dbda1c5..c84da18ab8 100644
@@ -6,7 +6,7 @@
 menuconfig SPI_HID
 	tristate "SPI HID support"
 	default y
-	depends on SPI
+	depends on SPI && SPI_MEM
 
 if SPI_HID
 
drivers/hid/spi-hid/spi-hid-acpi.c +14 -12
index ea291ca269..72e74da304 100644
@@ -194,8 +194,9 @@ static void spi_hid_acpi_sleep_minimal_reset_delay(struct spihid_ops *ops)
 	fsleep(1000 * conf->minimal_reset_delay_ms);
 }
 
-static int spi_hid_acpi_probe(struct spi_device *spi)
+static int spi_hid_acpi_probe(struct spi_mem *mem)
 {
+	struct spi_device *spi = mem->spi;
 	struct device *dev = &spi->dev;
 	struct acpi_device *adev;
 	struct spi_hid_acpi_config *config;
@@ -232,7 +233,7 @@ static int spi_hid_acpi_probe(struct spi_device *spi)
 		return error;
 	}
 
-	return spi_hid_core_probe(spi, &config->ops, &config->property_conf);
+	return spi_hid_core_probe(mem, &config->ops, &config->property_conf);
 }
 
 static const struct acpi_device_id spi_hid_acpi_match[] = {
@@ -242,19 +243,20 @@ static const struct acpi_device_id spi_hid_acpi_match[] = {
 };
 MODULE_DEVICE_TABLE(acpi, spi_hid_acpi_match);
 
-static struct spi_driver spi_hid_acpi_driver = {
-	.driver = {
-		.name	= "spi_hid_acpi",
-		.owner	= THIS_MODULE,
-		.pm	= &spi_hid_core_pm,
-		.acpi_match_table = spi_hid_acpi_match,
-		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
+static struct spi_mem_driver spi_hid_acpi_driver = {
+	.spidrv = {
+		.driver = {
+			.name = "spi_hid_acpi",
+			.pm = &spi_hid_core_pm,
+			.acpi_match_table = spi_hid_acpi_match,
+			.probe_type = PROBE_PREFER_ASYNCHRONOUS,
+		},
 	},
-	.probe		= spi_hid_acpi_probe,
-	.remove		= spi_hid_core_remove,
+	.probe = spi_hid_acpi_probe,
+	.remove = spi_hid_core_remove,
 };
 
-module_spi_driver(spi_hid_acpi_driver);
+module_spi_mem_driver(spi_hid_acpi_driver);
 
 MODULE_DESCRIPTION("HID over SPI ACPI transport driver");
 MODULE_AUTHOR("Angela Czubak <[email protected]>");
drivers/hid/spi-hid/spi-hid-core.c +60 -17

181 lines not on this page · raw message

drivers/hid/spi-hid/spi-hid-core.h +1 -0
index 3419472844..768d900bc7 100644
@@ -44,6 +44,7 @@ struct spi_hid_device_descriptor {
 
 /* Driver context */
 struct spi_hid {
+	struct spi_mem *mem;
 	void *xfer_alloc;
 	u8 *xfer_buf;
 	size_t xfer_size;
drivers/hid/spi-hid/spi-hid-of.c +13 -16
index 6de9affbeb..5148b6b1dd 100644
@@ -142,8 +142,9 @@ static void spi_hid_of_sleep_minimal_reset_delay(struct spihid_ops *ops)
 	fsleep(1000 * conf->data.minimal_reset_delay_ms);
 }
 
-static int spi_hid_of_probe(struct spi_device *spi)
+static int spi_hid_of_probe(struct spi_mem *mem)
 {
+	struct spi_device *spi = mem->spi;
 	struct device *dev = &spi->dev;
 	const struct spi_hid_of_data *data;
 	struct spi_hid_of_config *config;
@@ -167,16 +168,11 @@ static int spi_hid_of_probe(struct spi_device *spi)
 
 	config->data = *data;
 
-	/*
-	 * FIXME: multi-SPI not supported. Once it is, derive the
-	 * HID-over-SPI flags from spi->mode.
-	 */
-
 	error = spi_hid_of_populate_config(config, dev);
 	if (error)
 		return dev_err_probe(dev, error, "Unable to populate config data\n");
 
-	return spi_hid_core_probe(spi, &config->ops, &config->data.conf);
+	return spi_hid_core_probe(mem, &config->ops, &config->data.conf);
 }
 
 static const struct of_device_id spi_hid_of_match[] = {
@@ -191,20 +187,21 @@ static const struct spi_device_id spi_hid_of_id_table[] = {
 };
 MODULE_DEVICE_TABLE(spi, spi_hid_of_id_table);
 
-static struct spi_driver spi_hid_of_driver = {
-	.driver = {
-		.name = "spi_hid_of",
-		.owner = THIS_MODULE,
-		.pm = &spi_hid_core_pm,
-		.of_match_table = spi_hid_of_match,
-		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
+static struct spi_mem_driver spi_hid_of_driver = {
+	.spidrv = {
+		.driver = {
+			.name = "spi_hid_of",
+			.pm = &spi_hid_core_pm,
+			.of_match_table = spi_hid_of_match,
+			.probe_type = PROBE_PREFER_ASYNCHRONOUS,
+		},
+		.id_table = spi_hid_of_id_table,
 	},
 	.probe = spi_hid_of_probe,
 	.remove = spi_hid_core_remove,
-	.id_table = spi_hid_of_id_table,
 };
 
-module_spi_driver(spi_hid_of_driver);
+module_spi_mem_driver(spi_hid_of_driver);
 
 MODULE_DESCRIPTION("HID over SPI OF transport driver");
 MODULE_AUTHOR("Dmitry Antipov <[email protected]>");
drivers/hid/spi-hid/spi-hid.h +3 -3
index 95acb28452..e87d1cf05e 100644
@@ -7,7 +7,7 @@
 #ifndef SPI_HID_H
 #define SPI_HID_H
 
-#include <linux/spi/spi.h>
+#include <linux/spi/spi-mem.h>
 #include <linux/sysfs.h>
 
 /* struct spi_hid_conf - Conf provided to the core */
@@ -35,10 +35,10 @@ struct spihid_ops {
 	void (*sleep_minimal_reset_delay)(struct spihid_ops *ops);
 };
 
-int spi_hid_core_probe(struct spi_device *spi, struct spihid_ops *ops,
+int spi_hid_core_probe(struct spi_mem *mem, struct spihid_ops *ops,
 		       struct spi_hid_conf *conf);
 
-void spi_hid_core_remove(struct spi_device *spi);
+int spi_hid_core_remove(struct spi_mem *mem);
 
 extern const struct dev_pm_ops spi_hid_core_pm;
 
#51 lore raw

RFC PATCH v3 3/5 HID: spi-hid: assemble bounded fragmented input reports

In reply to Jizhou Tong

The driver rejects every report split across multiple wire fragments. Assemble DATA fragments before passing a complete report to the HID layer, checking the declared length, final padding and inter-fragment timeout. Command responses remain atomic.

Limit report_size to 8 KiB before allocating buffers and discard partial reports on suspend and recovery. Preserve minimum-body validation and check each wire fragment against max_fragment_length independently of the complete input-report size.

---
 drivers/hid/spi-hid/spi-hid-core.c | 64 +++++++++++++++++++++++++++---
 drivers/hid/spi-hid/spi-hid-core.h |  4 ++
 2 files changed, 63 insertions(+), 5 deletions(-)
drivers/hid/spi-hid/spi-hid-core.c +59 -5
index 53da68c705..ef5fb11f23 100644
@@ -146,7 +146,6 @@ static void spi_hid_parse_dev_desc(const struct hidspi_dev_descriptor *raw,
 	desc->max_input_length = le16_to_cpu(raw->max_input_len);
 	desc->max_output_length = le16_to_cpu(raw->max_output_len);
 
-	/* FIXME: multi-fragment not supported, field below not used */
 	desc->max_fragment_length = le16_to_cpu(raw->max_frag_len);
 
 	desc->vendor_id = le16_to_cpu(raw->vendor_id);
@@ -330,6 +329,7 @@ static int spi_hid_suspend(struct spi_hid *shid)
 
 	/* disable_irq() waits for a running IRQ thread. */
 	disable_irq(shid->spi->irq);
+	shid->fragment_len = 0;
 
 	guard(mutex)(&shid->power_lock);
 	if (!device_may_wakeup(dev)) {
@@ -461,6 +461,7 @@ static void spi_hid_error_handler(struct spi_hid *shid)
 		return;
 
 	disable_irq(shid->spi->irq);
+	shid->fragment_len = 0;
 
 	if (shid->reset_attempts++ >= SPI_HID_MAX_RESET_ATTEMPTS) {
 		dev_err(dev, "unresponsive device, aborting\n");
@@ -1035,6 +1036,48 @@ static int spi_hid_process_input_report(struct spi_hid *shid,
 	return 0;
 }
 
+/* Only DATA reports may be fragmented; command responses remain atomic. */
+static int spi_hid_accept_fragment(struct spi_hid *shid,
+				   const struct spi_hid_input_header *header)
+{
+	struct spi_hid_input_report body;
+	u16 len = header->report_length;
+	unsigned int total;
+	int error;
+
+	if (shid->fragment_len &&
+	    time_after(jiffies, shid->fragment_time + msecs_to_jiffies(1000)))
+		goto invalid;
+	if (!shid->fragment_len) {
+		if (header->last_fragment_flag)
+			return spi_hid_process_input_report(shid, shid->input);
+		spi_hid_populate_input_body(shid->input->body, &body);
+		if (body.report_type != DATA || body.content_length > shid->bufsize)
+			goto invalid;
+		shid->fragment_expected = HIDSPI_INPUT_BODY_SIZE(body.content_length);
+	}
+	total = shid->fragment_len + len;
+	if (total > round_up(shid->bufsize + HIDSPI_INPUT_BODY_HEADER_SIZE, 4) ||
+	    (!header->last_fragment_flag && total >= shid->fragment_expected) ||
+	    (header->last_fragment_flag && total != ALIGN(shid->fragment_expected, 4)))
+		goto invalid;
+	memcpy((u8 *)shid->fragment + offsetof(struct spi_hid_input_buf, body) +
+	       shid->fragment_len, shid->input->body, len);
+	shid->fragment_len = total;
+	shid->fragment_time = jiffies;
+	if (!header->last_fragment_flag)
+		return 0;
+
+	memcpy(shid->fragment->header, shid->input->header, HIDSPI_INPUT_HEADER_SIZE);
+	put_unaligned_le16((total / 4) | BIT(14), &shid->fragment->header[1]);
+	error = spi_hid_process_input_report(shid, shid->fragment);
+	shid->fragment_len = 0;
+	return error;
+invalid:
+	shid->fragment_len = 0;
+	return -EPROTO;
+}
+
 static int spi_hid_bus_validate_header(struct spi_hid *shid,
 				       struct spi_hid_input_header *header)
 {
@@ -1070,9 +1113,11 @@ static int spi_hid_bus_validate_header(struct spi_hid *shid,
 		return -EMSGSIZE;
 	}
 
-	if (header->last_fragment_flag != 1) {
-		dev_err(dev, "Multi-fragment reports not supported\n");
-		return -EOPNOTSUPP;
+	if (shid->desc.max_fragment_length &&
+	    header->report_length > shid->desc.max_fragment_length) {
+		dev_err(dev, "Input fragment size %u > max expected of %u\n",
+			header->report_length, shid->desc.max_fragment_length);
+		return -EMSGSIZE;
 	}
 
 	if (header->sync_const != SPI_HID_INPUT_HEADER_SYNC_BYTE) {
@@ -1152,6 +1197,7 @@ static int spi_hid_set_request(struct spi_hid *shid, u8 *arg_buf, u16 arg_len,
 /* Schedule a reset to recover from an error in the IRQ handler. */
 static irqreturn_t spi_hid_irq_error(struct spi_hid *shid)
 {
+	shid->fragment_len = 0;
 	set_bit(SPI_HID_ERROR, &shid->flags);
 	schedule_work(&shid->reset_work);
 
@@ -1228,7 +1274,7 @@ static irqreturn_t spi_hid_dev_irq(int irq, void *_shid)
 		}
 	}
 
-	error = spi_hid_process_input_report(shid, shid->input);
+	error = spi_hid_accept_fragment(shid, &header);
 	if (error) {
 		dev_err(dev, "Failed to process input report: %d\n", error);
 		return spi_hid_irq_error(shid);
@@ -1246,6 +1292,9 @@ static int spi_hid_alloc_buffers(struct spi_hid *shid, size_t report_size)
 	size_t xfer_size;
 	void *tmp;
 
+	if (report_size > SZ_8K)
+		return -EMSGSIZE;
+
 	tmp = devm_krealloc(dev, shid->output, outbufsize, GFP_KERNEL | __GFP_ZERO);
 	if (!tmp)
 		return -ENOMEM;
@@ -1261,6 +1310,11 @@ static int spi_hid_alloc_buffers(struct spi_hid *shid, size_t report_size)
 		return -ENOMEM;
 	shid->response = tmp;
 
+	tmp = devm_krealloc(dev, shid->fragment, inbufsize, GFP_KERNEL | __GFP_ZERO);
+	if (!tmp)
+		return -ENOMEM;
+	shid->fragment = tmp;
+
 	/* io_lock serializes the separate, DMA-aligned TX and RX regions. */
 	xfer_size = ALIGN(max(inbufsize, outbufsize), ARCH_DMA_MINALIGN) +
 		    ARCH_DMA_MINALIGN;
drivers/hid/spi-hid/spi-hid-core.h +4 -0
index 768d900bc7..a65a764d0b 100644
@@ -61,6 +61,10 @@ struct spi_hid {
 	struct spi_hid_output_buf *output;	/* Output buffer. */
 	struct spi_hid_input_buf *input;	/* Input buffer. */
 	struct spi_hid_input_buf *response;	/* Response buffer. */
+	struct spi_hid_input_buf *fragment;
+	u16 fragment_len;
+	u16 fragment_expected;
+	unsigned long fragment_time;
 
 	struct drm_panel_follower panel_follower;
 	bool	is_panel_follower;
#52 lore raw

RFC PATCH v3 4/5 dt-bindings: input: hid-over-spi: add the Surface Laptop 7 touchpad

In reply to Jizhou Tong

The Surface Laptop 7 13.8-inch (Romulus13) touchpad uses HID-over-SPI with quad address/data transfers. Add its compatible to the common binding, with both bus widths fixed to four and a 20 MHz clock limit. The existing supply and active-low reset properties cover this device.

Keep the G6 digitizer constraints unchanged. Pinctrl properties are already permitted by the common schemas and need no local definitions.

---
 .../bindings/input/hid-over-spi.yaml          | 40 +++++++++++++++++++
 1 file changed, 40 insertions(+)
Documentation/devicetree/bindings/input/hid-over-spi.yaml +40 -0
index bed0746f00..ec8c72493d 100644
@@ -22,11 +22,30 @@ description: |+
 allOf:
   - $ref: /schemas/input/touchscreen/touchscreen.yaml#
   - $ref: /schemas/spi/spi-peripheral-props.yaml#
+  - if:
+      properties:
+        compatible:
+          contains:
+            const: microsoft,romulus13-touchpad
+    then:
+      properties:
+        spi-max-frequency:
+          maximum: 20000000
+        spi-rx-bus-width:
+          items:
+            - const: 4
+        spi-tx-bus-width:
+          items:
+            - const: 4
+      required:
+        - spi-rx-bus-width
+        - spi-tx-bus-width
 
 properties:
   compatible:
     enum:
       - microsoft,g6-touch-digitizer
+      - microsoft,romulus13-touchpad
 
   reg:
     maxItems: 1
@@ -73,3 +92,24 @@ examples:
         pinctrl-0 = <&ts_d6_int_bias>;
       };
     };
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+    #include <dt-bindings/gpio/gpio.h>
+
+    spi {
+      #address-cells = <1>;
+      #size-cells = <0>;
+
+      touchpad@0 {
+        compatible = "microsoft,romulus13-touchpad";
+        reg = <0>;
+        spi-max-frequency = <20000000>;
+        spi-rx-bus-width = <4>;
+        spi-tx-bus-width = <4>;
+        interrupts-extended = <&tlmm 3 IRQ_TYPE_LEVEL_LOW>;
+        vdd-supply = <&vreg_ts_5p0>;
+        reset-gpios = <&tlmm 120 GPIO_ACTIVE_LOW>;
+        pinctrl-0 = <&touchpad_reset>;
+        pinctrl-names = "default";
+      };
+    };
#53 lore raw

Re: [RFC PATCH v3 4/5] dt-bindings: input: hid-over-spi: add the Surface Laptop 7 touchpad

Quoted 23 lines · Jizhou Tong

The Surface Laptop 7 13.8-inch (Romulus13) touchpad uses HID-over-SPI with quad address/data transfers. Add its compatible to the common binding, with both bus widths fixed to four and a 20 MHz clock limit. The existing supply and active-low reset properties cover this device.

Keep the G6 digitizer constraints unchanged. Pinctrl properties are already permitted by the common schemas and need no local definitions.

---
 .../bindings/input/hid-over-spi.yaml          | 40 +++++++++++++++++++
 1 file changed, 40 insertions(+)
Documentation/devicetree/bindings/input/hid-over-spi.yaml +2 -0
index bed0746f00..ec8c72493d 100644
@@ -22,11 +22,30 @@ description: |+
 allOf:
   - $ref: /schemas/input/touchscreen/touchscreen.yaml#
   - $ref: /schemas/spi/spi-peripheral-props.yaml#
+  - if:
+      properties:

Documentation/devicetree/bindings/input/hid-over-spi.yaml description: |+ · line 36

Quoted 10 lines
Documentation/devicetree/bindings/input/hid-over-spi.yaml +10 -0
+        compatible:
+          contains:
+            const: microsoft,romulus13-touchpad
+    then:
+      properties:
+        spi-max-frequency:
+          maximum: 20000000
+        spi-rx-bus-width:
+          items:
+            - const: 4

Should this not just be "const: 4", since the property is not an array?

Cheers,
Conor.

Quoted 40 lines
Documentation/devicetree/bindings/input/hid-over-spi.yaml +28 -0
+        spi-tx-bus-width:
+          items:
+            - const: 4
+      required:
+        - spi-rx-bus-width
+        - spi-tx-bus-width
 
 properties:
   compatible:
     enum:
       - microsoft,g6-touch-digitizer
+      - microsoft,romulus13-touchpad
 
   reg:
     maxItems: 1
@@ -73,3 +92,24 @@ examples:
         pinctrl-0 = <&ts_d6_int_bias>;
       };
     };
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+    #include <dt-bindings/gpio/gpio.h>
+
+    spi {
+      #address-cells = <1>;
+      #size-cells = <0>;
+
+      touchpad@0 {
+        compatible = "microsoft,romulus13-touchpad";
+        reg = <0>;
+        spi-max-frequency = <20000000>;
+        spi-rx-bus-width = <4>;
+        spi-tx-bus-width = <4>;
+        interrupts-extended = <&tlmm 3 IRQ_TYPE_LEVEL_LOW>;
+        vdd-supply = <&vreg_ts_5p0>;
+        reset-gpios = <&tlmm 120 GPIO_ACTIVE_LOW>;
+        pinctrl-0 = <&touchpad_reset>;
+        pinctrl-names = "default";
+      };
+    };
#54 lore raw

Re: [RFC PATCH v3 4/5] dt-bindings: input: hid-over-spi: add the Surface Laptop 7 touchpad

Hi Conor,

I rechecked this: spi-peripheral-props.yaml defines both bus-width properties as uint32-array, including in current mainline [1]. With dt-schema 2026.6, changing these constraints to plain "const: 4" makes the valid example fail with "spi-rx-bus-width: 4 was expected" (and likewise for TX).

The one-element items form passes the examples and rejects missing, incorrect and multiple widths. I have kept it for now; is there a preferred form for this array constraint?

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/devicetree/bindings/spi/spi-peripheral-props.yaml

Thanks,
Jizhou

Acked-by
#55 lore raw

Re: [RFC PATCH v3 4/5] dt-bindings: input: hid-over-spi: add the Surface Laptop 7 touchpad

Quoted 8 lines · Jizhou Tong

Hi Conor,

I rechecked this: spi-peripheral-props.yaml defines both bus-width properties as uint32-array, including in current mainline [1]. With dt-schema 2026.6, changing these constraints to plain "const: 4" makes the valid example fail with "spi-rx-bus-width: 4 was expected" (and likewise for TX).

Quoted 5 lines

The one-element items form passes the examples and rejects missing, incorrect and multiple widths. I have kept it for now; is there a preferred form for this array constraint?

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/devicetree/bindings/spi/spi-peripheral-props.yaml

Hmm, I;'d swear I looked it up before leaving my comment, but clearly I am wrong and it is an array. Sorry about that.

pw-bot: not-applicable

#56 lore raw

RFC PATCH v3 5/5 HID: spi-hid: support the Romulus13 QSPI touchpad

In reply to Jizhou Tong

Add the Romulus13 touchpad's protocol identifiers and power timing to the per-compatible data.

A reset loses its touchpad mode even when the descriptor CRC is unchanged. Recreate the HID device after reset so hidraw clients such as iptsd reopen it and reapply the mode without restarting the service.

Wait for the reset response before scheduling the refresh worker and delay READY until the replacement HID device has been parsed. Preserve the existing timeout recovery, locking and reset-attempt handling.

---
 drivers/hid/spi-hid/spi-hid-core.c | 15 ++++++++++-----
 drivers/hid/spi-hid/spi-hid-of.c   | 15 +++++++++++++++
 drivers/hid/spi-hid/spi-hid.h      |  2 ++
 3 files changed, 27 insertions(+), 5 deletions(-)
drivers/hid/spi-hid/spi-hid-core.c +10 -5
index ef5fb11f23..901c8d9ace 100644
@@ -280,8 +280,8 @@ static const char *spi_hid_power_mode_string(enum hidspi_power_state power_state
 
 /*
  * Wait for the device descriptor response after reset; the device then takes
- * requests. Set READY here: the report descriptor check that normally sets it
- * needs power_lock, which callers hold.
+ * requests. Set READY here for devices that reuse their HID instance. Devices
+ * that rebind after reset become ready when the new HID instance is parsed.
  */
 static int spi_hid_wait_for_reset(struct spi_hid *shid)
 {
@@ -294,7 +294,8 @@ static int spi_hid_wait_for_reset(struct spi_hid *shid)
 		return -ETIMEDOUT;
 	}
 
-	set_bit(SPI_HID_READY, &shid->flags);
+	if (!shid->conf->rebind_after_reset)
+		set_bit(SPI_HID_READY, &shid->flags);
 	return 0;
 }
 
@@ -422,6 +423,9 @@ static int spi_hid_resume(struct spi_hid *shid)
 			schedule_work(&shid->reset_work);
 			return -ETIMEDOUT;
 		}
+		/* Refresh recreates the HID device after a successful handshake. */
+		if (was_reset && shid->conf->rebind_after_reset)
+			return 0;
 		/* HID requests would fail while the device is not ready. */
 		if (!test_bit(SPI_HID_READY, &shid->flags))
 			return 0;
@@ -910,8 +914,9 @@ static void spi_hid_refresh_device(struct spi_hid *shid)
 				     (size_t)error);
 	}
 
-	/* Same report descriptor, so no need to create a new hid device. */
-	if (new_crc32 == shid->report_descriptor_crc32) {
+	/* Some devices lose userspace-selected modes even with the same CRC. */
+	if (new_crc32 == shid->report_descriptor_crc32 &&
+	    !shid->conf->rebind_after_reset) {
 		shid->reset_attempts = 0;
 		set_bit(SPI_HID_READY, &shid->flags);
 		return;
drivers/hid/spi-hid/spi-hid-of.c +15 -0
index 5148b6b1dd..7d3ce1fd89 100644
@@ -62,6 +62,19 @@ static const struct spi_hid_of_data microsoft_g6_data = {
 	.minimal_reset_delay_ms = 100,
 };
 
+static const struct spi_hid_of_data romulus13_data = {
+	.conf = {
+		.input_report_header_address = 0x1000,
+		.input_report_body_address = 0x1004,
+		.output_report_address = 0x2000,
+		.read_opcode = 0xeb,
+		.write_opcode = 0xe2,
+		.rebind_after_reset = true,
+	},
+	.post_power_on_delay_ms = 1000,
+	.minimal_reset_delay_ms = 300,
+};
+
 static int spi_hid_of_populate_config(struct spi_hid_of_config *conf,
 				      struct device *dev)
 {
@@ -176,12 +189,14 @@ static int spi_hid_of_probe(struct spi_mem *mem)
 }
 
 static const struct of_device_id spi_hid_of_match[] = {
+	{ .compatible = "microsoft,romulus13-touchpad", .data = &romulus13_data },
 	{ .compatible = "microsoft,g6-touch-digitizer", .data = &microsoft_g6_data },
 	{ }
 };
 MODULE_DEVICE_TABLE(of, spi_hid_of_match);
 
 static const struct spi_device_id spi_hid_of_id_table[] = {
+	{ .name = "romulus13-touchpad", .driver_data = (kernel_ulong_t)&romulus13_data },
 	{ .name = "g6-touch-digitizer", .driver_data = (kernel_ulong_t)&microsoft_g6_data },
 	{ }
 };
drivers/hid/spi-hid/spi-hid.h +2 -0
index e87d1cf05e..2b2f62c837 100644
@@ -17,6 +17,8 @@ struct spi_hid_conf {
 	u32 output_report_address;
 	u8 read_opcode;
 	u8 write_opcode;
+	/* Reconnect hidraw clients when reset loses device-specific state. */
+	bool rebind_after_reset;
 };
 
 /**