From: Emfox Zhou <emfox@debian.org>
To: Even Xu <even.xu@intel.com>, Xinpeng Sun <xinpeng.sun@intel.com>,
Jiri Kosina <jikos@kernel.org>,
Benjamin Tissoires <bentiss@kernel.org>
Cc: linux-input@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v11 4/6] HID: intel-thc-hid: intel-ipts: add the HID device
Date: Thu, 3 Dec 2026 10:04:13 +0800 [thread overview]
Message-ID: <20261203020433.2631656-4-emfox@debian.org> (raw)
In-Reply-To: <cover.1796215044.git.emfox@debian.org>
Register a HID device for the port and implement its callbacks, with the
descriptor and feature handshakes in ipts-hid.c.
Signed-off-by: Emfox Zhou <emfox@debian.org>
---
drivers/hid/intel-thc-hid/Makefile | 1 +
.../hid/intel-thc-hid/intel-ipts/ipts-hid.c | 267 ++++++++++++++++++
.../intel-thc-hid/intel-ipts/ipts-protocol.c | 7 +
drivers/hid/intel-thc-hid/intel-ipts/ipts.h | 2 +
4 files changed, 277 insertions(+)
create mode 100644 drivers/hid/intel-thc-hid/intel-ipts/ipts-hid.c
diff --git a/drivers/hid/intel-thc-hid/Makefile b/drivers/hid/intel-thc-hid/Makefile
index a3d6f8b18..34e8aaeb2 100644
--- a/drivers/hid/intel-thc-hid/Makefile
+++ b/drivers/hid/intel-thc-hid/Makefile
@@ -23,5 +23,6 @@ intel-quicki2c-objs += intel-quicki2c/quicki2c-protocol.o
obj-$(CONFIG_INTEL_IPTS) += intel-ipts.o
intel-ipts-objs += intel-ipts/pci-ipts.o
intel-ipts-objs += intel-ipts/ipts-protocol.o
+intel-ipts-objs += intel-ipts/ipts-hid.o
ccflags-y += -I $(src)/intel-thc
diff --git a/drivers/hid/intel-thc-hid/intel-ipts/ipts-hid.c b/drivers/hid/intel-thc-hid/intel-ipts/ipts-hid.c
new file mode 100644
index 000000000..491029468
--- /dev/null
+++ b/drivers/hid/intel-thc-hid/intel-ipts/ipts-hid.c
@@ -0,0 +1,267 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * HID device for THC ports in IPTS mode.
+ */
+
+#include <linux/hid.h>
+#include <linux/string.h>
+
+#include "ipts.h"
+
+static bool thc_ipts_idle(const struct thc_ipts *idev)
+{
+ return !READ_ONCE(idev->recover_pending) &&
+ !READ_ONCE(idev->recovering) &&
+ !READ_ONCE(idev->stopping);
+}
+
+static int thc_ipts_hid_parse(struct hid_device *hdev)
+{
+ struct thc_ipts *idev = hdev->driver_data;
+ unsigned long timeout = msecs_to_jiffies(THC_IPTS_REQ_TIMEOUT_MS);
+ u64 zero = 0;
+ unsigned int retries = 0;
+ int ret;
+ int try = 0;
+
+ /* A descriptor from a previous start is not this one's. */
+ WRITE_ONCE(idev->have_desc, false);
+
+ while (try < THC_IPTS_REQ_TRIES) {
+ /* An answer that landed after a wait expired is taken here. */
+ if (smp_load_acquire(&idev->have_desc))
+ break;
+
+ /* The controller ignores the eight byte argument. */
+ ret = thc_ipts_send(idev, THC_IPTS_TX_GET_DESC, &zero,
+ sizeof(zero), NULL);
+ /*
+ * Both dropped the frame: try again.
+ */
+ if (ret == -EAGAIN || ret == -ETIMEDOUT) {
+ if (++retries > THC_IPTS_REQ_TRIES ||
+ !wait_event_timeout(idev->wq,
+ thc_ipts_idle(idev), timeout))
+ break;
+ continue;
+ }
+ if (ret)
+ return ret;
+
+ try++;
+ wait_event_timeout(idev->wq, READ_ONCE(idev->have_desc),
+ timeout);
+ }
+
+ /* Pairs with the release in thc_ipts_descriptor(). */
+ if (smp_load_acquire(&idev->have_desc) && idev->desc_len > 0) {
+ /* The receive buffer holds more than a descriptor may be. */
+ if (idev->desc_len > HID_MAX_DESCRIPTOR_SIZE) {
+ dev_err(idev->dev,
+ "report descriptor of %zu bytes is too large\n",
+ idev->desc_len);
+ return -EINVAL;
+ }
+
+ return hid_parse_report(hdev, idev->desc, idev->desc_len);
+ }
+
+ dev_err(idev->dev, "controller did not return a report descriptor\n");
+
+ return -ETIMEDOUT;
+}
+
+static int thc_ipts_get_feature(struct thc_ipts *idev, unsigned char reportnum,
+ u8 *buf, size_t len)
+{
+ unsigned long timeout = msecs_to_jiffies(THC_IPTS_REQ_TIMEOUT_MS);
+ /* Given by the send below, which only a frame that went out gets. */
+ unsigned int seq = 0;
+ int ret;
+
+ /*
+ * One request at a time, and the wait happens without the lock: a request
+ * from the handler thread would wait for itself.
+ */
+ mutex_lock(&idev->feature_lock);
+
+ /*
+ * The answer carries only the ID, so a late one cannot be told from the
+ * next: after a timeout the channel stays shut.
+ */
+ if (idev->feature_off) {
+ mutex_unlock(&idev->feature_lock);
+ return -EIO;
+ }
+ if (idev->feature_pending) {
+ mutex_unlock(&idev->feature_lock);
+ return -EBUSY;
+ }
+
+ WRITE_ONCE(idev->feature_done, false);
+ idev->feature_id = reportnum;
+ idev->feature_pending = true;
+ mutex_unlock(&idev->feature_lock);
+
+ /*
+ * A refusal wrote nothing: the channel stays open.
+ */
+ ret = thc_ipts_send(idev, THC_IPTS_TX_GET_FEATURE, &reportnum, 1, &seq);
+ if (ret) {
+ mutex_lock(&idev->feature_lock);
+ idev->feature_pending = false;
+
+ /*
+ * The frame may be out there: its answer cannot be told apart.
+ */
+ if (ret == -ETIMEDOUT)
+ WRITE_ONCE(idev->feature_off, true);
+ mutex_unlock(&idev->feature_lock);
+
+ if (ret == -ETIMEDOUT)
+ dev_err(idev->dev, "feature report %u not acknowledged\n",
+ reportnum);
+
+ return ret;
+ }
+
+ wait_event_timeout(idev->wq, READ_ONCE(idev->feature_done) ||
+ READ_ONCE(idev->recover_seq) != seq ||
+ READ_ONCE(idev->recover_pending) ||
+ READ_ONCE(idev->recovering) ||
+ READ_ONCE(idev->stopping), timeout);
+
+ mutex_lock(&idev->feature_lock);
+ /* An answer that landed while the wait was expiring counts too. */
+ if (!idev->feature_done) {
+ /*
+ * A reset dropped whatever was queued: nothing to wait for.
+ */
+ if (READ_ONCE(idev->recover_seq) != seq ||
+ READ_ONCE(idev->recover_pending) ||
+ READ_ONCE(idev->recovering) ||
+ READ_ONCE(idev->stopping))
+ ret = -EIO;
+ else
+ ret = -ETIMEDOUT;
+
+ idev->feature_pending = false;
+ /*
+ * Only a timeout leaves a frame out there.
+ */
+ if (ret == -ETIMEDOUT)
+ WRITE_ONCE(idev->feature_off, true);
+ mutex_unlock(&idev->feature_lock);
+
+ if (ret == -ETIMEDOUT)
+ dev_err(idev->dev, "no feature report %u\n", reportnum);
+
+ return ret;
+ }
+
+ /* The payload is the report as user space writes it: ID first. */
+ len = min(len, idev->feature_len);
+ if (len)
+ memcpy(buf, idev->feature, len);
+ idev->feature_pending = false;
+ mutex_unlock(&idev->feature_lock);
+
+ return len ?: -EPROTO;
+}
+
+static int thc_ipts_hid_raw_request(struct hid_device *hdev,
+ unsigned char reportnum, __u8 *buf,
+ size_t len, unsigned char rtype,
+ int reqtype)
+{
+ struct thc_ipts *idev = hdev->driver_data;
+ u32 code;
+ int ret;
+
+ if (!buf || !len)
+ return -EINVAL;
+
+ if (reqtype == HID_REQ_GET_REPORT && rtype == HID_FEATURE_REPORT)
+ return thc_ipts_get_feature(idev, reportnum, buf, len);
+
+ if (reqtype != HID_REQ_SET_REPORT ||
+ (rtype != HID_OUTPUT_REPORT && rtype != HID_FEATURE_REPORT))
+ return -EOPNOTSUPP;
+
+ code = rtype == HID_OUTPUT_REPORT ? THC_IPTS_TX_OUTPUT :
+ THC_IPTS_TX_SET_FEATURE;
+
+ ret = thc_ipts_send(idev, code, buf, len, NULL);
+ if (ret)
+ return ret;
+
+ /* The raw request returns a byte count, which hidraw forwards. */
+ return (int)len;
+}
+
+static int thc_ipts_hid_start(struct hid_device *hdev)
+{
+ return 0;
+}
+
+static void thc_ipts_hid_stop(struct hid_device *hdev)
+{
+}
+
+static int thc_ipts_hid_open(struct hid_device *hdev)
+{
+ return 0;
+}
+
+static void thc_ipts_hid_close(struct hid_device *hdev)
+{
+}
+
+static const struct hid_ll_driver thc_ipts_ll_driver = {
+ .start = thc_ipts_hid_start,
+ .stop = thc_ipts_hid_stop,
+ .open = thc_ipts_hid_open,
+ .close = thc_ipts_hid_close,
+ .parse = thc_ipts_hid_parse,
+ .raw_request = thc_ipts_hid_raw_request,
+};
+
+void thc_ipts_hid_remove(struct thc_ipts *idev)
+{
+ if (!READ_ONCE(idev->hid))
+ return;
+
+ hid_destroy_device(idev->hid);
+ WRITE_ONCE(idev->hid, NULL);
+}
+
+int thc_ipts_hid_add(struct thc_ipts *idev)
+{
+ struct hid_device *hdev;
+ int ret;
+
+ hdev = hid_allocate_device();
+ if (IS_ERR(hdev))
+ return PTR_ERR(hdev);
+
+ hdev->ll_driver = &thc_ipts_ll_driver;
+ hdev->bus = BUS_PCI;
+ hdev->vendor = idev->vendor;
+ hdev->product = idev->product;
+ hdev->version = le32_to_cpu(idev->cfg.revision) & 0xffff;
+ hdev->dev.parent = idev->dev;
+ hdev->driver_data = idev;
+ snprintf(hdev->name, sizeof(hdev->name), "intel-ipts %04X:%04X",
+ idev->vendor, idev->product);
+ strscpy(hdev->phys, dev_name(idev->dev), sizeof(hdev->phys));
+
+ ret = hid_add_device(hdev);
+ if (ret) {
+ hid_destroy_device(hdev);
+ return ret;
+ }
+
+ WRITE_ONCE(idev->hid, hdev);
+
+ return 0;
+}
diff --git a/drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c b/drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c
index 9ef4b3cfa..4877ad87a 100644
--- a/drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c
+++ b/drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c
@@ -629,6 +629,11 @@ int thc_ipts_port_start(struct thc_ipts *idev)
thc_interrupt_enable(idev->thc, true);
+ ret = thc_ipts_hid_add(idev);
+ if (ret)
+ return ret;
+ WRITE_ONCE(idev->state, THC_IPTS_UP);
+
return 0;
}
@@ -672,4 +677,6 @@ void thc_ipts_port_stop(struct thc_ipts *idev)
/* Wait for a handler that may still look at the ring. */
synchronize_irq(idev->irq);
thc_interrupt_enable(idev->thc, false);
+
+ thc_ipts_hid_remove(idev);
}
diff --git a/drivers/hid/intel-thc-hid/intel-ipts/ipts.h b/drivers/hid/intel-thc-hid/intel-ipts/ipts.h
index f27ad7f03..f9a09343e 100644
--- a/drivers/hid/intel-thc-hid/intel-ipts/ipts.h
+++ b/drivers/hid/intel-thc-hid/intel-ipts/ipts.h
@@ -212,6 +212,8 @@ struct thc_ipts {
int thc_ipts_send(struct thc_ipts *idev, u32 code, const void *data,
size_t len, unsigned int *seq);
+int thc_ipts_hid_add(struct thc_ipts *idev);
+void thc_ipts_hid_remove(struct thc_ipts *idev);
irqreturn_t thc_ipts_hardirq(int irq, void *data);
irqreturn_t thc_ipts_thread(int irq, void *data);
next prev parent reply other threads:[~2026-12-03 2:05 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 2:04 [PATCH v11 0/6] HID: intel-thc-hid: intel-ipts: support THC IPTS ports Emfox Zhou
2026-12-03 2:04 ` [PATCH v11 1/6] HID: intel-thc-hid: intel-thc: expose the port operations IPTS needs Emfox Zhou
2026-12-03 2:11 ` sashiko-bot
2026-12-03 2:04 ` [PATCH v11 2/6] HID: intel-thc-hid: intel-ipts: bring up the port Emfox Zhou
2026-12-03 2:15 ` sashiko-bot
2026-12-03 2:04 ` [PATCH v11 3/6] HID: intel-thc-hid: intel-ipts: receive reports Emfox Zhou
2026-12-03 2:26 ` sashiko-bot
2026-12-03 2:04 ` Emfox Zhou [this message]
2026-12-03 2:04 ` [PATCH v11 5/6] HID: intel-thc-hid: intel-ipts: handle system sleep Emfox Zhou
2026-12-03 2:04 ` [PATCH v11 6/6] HID: intel-thc-hid: intel-ipts: document the IPTS mode Emfox Zhou
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261203020433.2631656-4-emfox@debian.org \
--to=emfox@debian.org \
--cc=bentiss@kernel.org \
--cc=even.xu@intel.com \
--cc=jikos@kernel.org \
--cc=linux-input@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=xinpeng.sun@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox