Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=82.195.75.108 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796263507; b=ro0Ed+/NUy9ONPnFniihDDqT4yyiOMQK0mfiXjLxxcJ27yZD5YqtBmZtV0fJa2bWhfqp uzZ4YlxRgsphlCU3iw4iWtZEFHadxf9Ux2XPaSAmLiMLsXkuYwKqUHg3QxQNHx0+BsVdY o8dSX+GjD8JBzTkUbL23kP+BP2DmqnGmCGILAjwzGTSXBzSSXirZibYSag8DvhNz2fCkY PjrcWZJOx9eHFA+/i4wj/OiB6ext0x/KPsYQ1t0EzfnnOeyCTO2FeuyJekU7GsJ8iXMtw tpIQgCl59f7G+n7BwdqNKosLJkl3XTPf/yviczeMldcTP0p8XOlkLDU211S1ZGhB9GA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796263507; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-Debian-User; bh=58wwGa8fG79fMTVAiD+3AGapUXnv1NNLOhrEh1vsa7Q=; b=kaFJMXQGewiixHHJCWyxDxEEQuccDqFCtH4ElPedZrSb+QVuKhRohD0XxRzg5ZuJiwn0 Q8+PwaX2VBBcmlI4U7yt3SZeUOwEIO5jUwBc2a8maCX2Mtb4ap28EFv/SW7fBTHInevkx LnNUK+6MAfqafh9DmDsZYQzNxefXOG+tcWxR21o32DAMEbB0Nb1J6zRg0M9uBwuL0YTOj PRLneImDRhZK38MNFM5nVKVcHkK2N2+IT/yHRsQxHceslSm59gneRUZpjiq+HIdIbFZtv +twCZ1GiQH9T69Vw/IoKO8eKJ9MLi6lhfMxTkDp3NESAxGvH3597sw1JIDWtzj2+CoQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=debian.org; spf=pass smtp.mailfrom=debian.org; dkim=pass header.d=debian.org header.i=@debian.org header.a=rsa-sha256 header.s=smtpauto.stravinsky header.b=XXw96ras; arc=none smtp.remote-ip=82.195.75.108 Received: from stravinsky.debian.org (stravinsky.debian.org [82.195.75.108]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 09A13606A682; Thu, 3 Dec 2026 02:05:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 09A13606A682 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=debian.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=debian.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=debian.org header.i=@debian.org header.a=rsa-sha256 header.s=smtpauto.stravinsky header.b=XXw96ras DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debian.org; s=smtpauto.stravinsky; h=X-Debian-User:Content-Transfer-Encoding:MIME-Version :References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Reply-To: Content-Type:Content-ID:Content-Description; bh=58wwGa8fG79fMTVAiD+3AGapUXnv1NNLOhrEh1vsa7Q=; b=XXw96rasRVf/W97ADUXZZAqBDl 4FgUR/rRtPYkjg+jeGXs3nS/IJ4IkhZR3WEs31sboCRX5wURYzDCBDd03gMMy7FX+tELHwlPwjNAJ Kd7x+NaqSg014QFIY9onhZvkfwqHzhjlLuKjiVDUSfp6CT96mQeA9TeoMU/Oho/BK0MtPqXltpBhD 5IP4o4WibjBsvV+qMVQRTskDLp2fUU0Ks99UTdWXxwsYdXuNHppnEVPJIrgNfvvfKZAEgEKA4YSwU I7FUKKwenBVe6Cqz/MwlBlXUDMv2I9tErC+eu16BaXiN44FWcl1MfqqE7uJ+rIFGwSBGzcb0tfLzC XR/Tkpug==; Received: from authenticated-user by stravinsky.debian.org with esmtpsa (TLS1.3:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.96) (envelope-from ) id 1xYwBx-001xMR-1U; Thu, 03 Dec 2026 02:05:02 +0000 From: Emfox Zhou To: Even Xu , Xinpeng Sun , Jiri Kosina , Benjamin Tissoires 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 Message-ID: <20261203020433.2631656-4-emfox@debian.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: References: X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Debian-User: emfox 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 --- 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 +#include + +#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);