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=1796263501; b=mQBhBMojnGumf8gHTb3LoKXegd8JbFua/c/UJvP0JDcaj3qhBimg498Cn51X2VOxHzNj BsKRBBQqfNc7X0wnNvG9zK0t1IEUqOgVurDBcbacHDiNF1Kh+Op+9ZRoIj28M090hif/G 4iBDYmEEUdUEoq/uVqCSuYP4Dx9pbnT1N314P9VY3l4Tyu3osPnHl02P9WOrfrNaE+9Xw rhGxVhXLk2CFiFUTEN1jYzuItp+SRFLmYxhr7v7Wo9rfpDj4OdJrikJzt0JMkq0ctH25+ tvSBjPWR5W6eepaIddceewIX/qxCZNFbSSw21d5XqVHkIvnH3RlAaZeLOb9DFoEEl2A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796263501; 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=9q7h7y+sAiwgLvdFhGSu03QP+l5Yqyirij0yOna7dpY=; b=TNt/pyqquXDOxKC9AdBcxoi3JmVMHkiJ+JmYGhzM/Mmgkx3NBmFklzcx7pBs1S5RsMy0 1RXETg68djMm9HwLZsj1wV+2ULTTsMwuuQSOfrC0q3yeSr0b5q1UFQ/kJDVR7r3Oo29n9 8XPfdfybY5KrgA66vwHw5rPzYgmqybY1vCsugA+HzDSWJ5UB0dHwckiKnHgXig0CAP1qV lly10FfHroFbo4BTtQ+enlDvKM9gM82l/rieaRY/thCtmaxugmVPAozcUCwpOIJDsWg2Q hpDLzzbyjNNBv0nD63KRV5g7lCZaUswulFI0DcTU0apha+hCSTIx2tfEI7ouVuyRdHg== 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=XHXbqO5t; 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 017A0606A333; Thu, 3 Dec 2026 02:04:59 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 017A0606A333 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=XHXbqO5t 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=9q7h7y+sAiwgLvdFhGSu03QP+l5Yqyirij0yOna7dpY=; b=XHXbqO5toeQ3m9EecUPnXXm8zg hzGYbNq/apG+0/5HJy4zlJLzE6N5fcdKPjbhJ5TuMREGKiM70WtaxixEqfMkXNSl4hF3emFQiz7FA Yot2kzdfGdbOdi8vAEE4AVukrW/ks7pAziBKpkIsg/Un7ryEa7SW8kt4ucAxIQFNB9LwI6xfKEGdP 73MqihVWpEQIOaBjJtUIrObvEcUKDZJbTyj/H6HU0QLBaQABT/V0eJ8uGPN3MLllvA2HfUSNMs2P2 HFAgb6aLY3gwD/GE/KtKVpP8zpUG2mvbWciKWz8RDFQZxNiobkwJi7BiJeASQZq2B8I6VYellR6hA mmKwrNVw==; 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 1xYwBp-001xMI-1R; Thu, 03 Dec 2026 02:04:54 +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 3/6] HID: intel-thc-hid: intel-ipts: receive reports Date: Thu, 3 Dec 2026 10:04:12 +0800 Message-ID: <20261203020433.2631656-3-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 Start the DMA engines and the port interrupt and carry frames in both directions: hand input reports to the HID device and keep descriptor and feature answers for the handshakes. Transfer errors are recovered from a work item. Signed-off-by: Emfox Zhou --- .../intel-thc-hid/intel-ipts/ipts-protocol.c | 467 +++++++++++++++++- drivers/hid/intel-thc-hid/intel-ipts/ipts.h | 83 ++++ .../hid/intel-thc-hid/intel-ipts/pci-ipts.c | 52 ++ 3 files changed, 600 insertions(+), 2 deletions(-) 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 5d3d8d400..9ef4b3cfa 100644 --- a/drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c +++ b/drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c @@ -3,8 +3,15 @@ * Transport for THC ports in IPTS mode: plain SPI with a private framing. */ +#include #include +#include +#include +#include +#include #include +#include +#include #include "intel-thc-hw.h" #include "ipts.h" @@ -15,9 +22,18 @@ /* Clock used until the controller has been read, the slowest known-good one. */ #define THC_IPTS_BOOT_SPI_HZ 7812500 +/* One page per PRD entry; a larger frame cannot be received. */ +#define THC_IPTS_RX_MAX (PRD_ENTRIES_NUM * SZ_4K) + /* Time the controller needs after reset before it answers on the bus. */ #define THC_IPTS_BOOT_MS 100 +/* + * The core reports one cause per pass: allow a pass per cause and one to + * confirm the status is drained. + */ +#define THC_IPTS_IRQ_PASSES (THC_UNKNOWN_INT + 1) + static int thc_ipts_read_cfg(struct thc_ipts *idev) { int ret, try; @@ -80,6 +96,411 @@ static int thc_ipts_touch_set(struct thc_ipts *idev, bool enable) &touch); } +/* + * Frames are built in @idev->tx_frame, so the lock is taken here rather than + * by the callers: none of them may hold it across a wait. + */ +int thc_ipts_send(struct thc_ipts *idev, u32 code, const void *data, + size_t len, unsigned int *seq) +{ + size_t padded = round_up(len, 4); + u8 *frame = idev->tx_frame; + int ret; + + /* + * A request from the handler thread would wait for a completion only that + * thread delivers: refuse it. + */ + if (current == READ_ONCE(idev->irq_thread)) + return -EBUSY; + + guard(mutex)(&idev->tx_lock); + + if (READ_ONCE(idev->stopping)) + return -ESHUTDOWN; + /* A reset is queued or running: the write would only time out. */ + if (READ_ONCE(idev->recover_pending) || READ_ONCE(idev->recovering)) + return -EAGAIN; + /* No engine to write into: waiting for a reset would not help. */ + if (READ_ONCE(idev->state) < THC_IPTS_DMA) + return -ENODEV; + if (THC_IPTS_TX_HDR_SIZE + padded > idev->max_tx) + return -EMSGSIZE; + + put_unaligned_le32(code, frame); + put_unaligned_le32((u32)len, frame + 4); + memset(frame + THC_IPTS_TX_HDR_SIZE, 0, padded); + if (len && data) + memcpy(frame + THC_IPTS_TX_HDR_SIZE, data, len); + + ret = thc_dma_write(idev->thc, frame, THC_IPTS_TX_HDR_SIZE + padded); + /* + * Counted under the lock, so a reset that takes it afterwards drops this + * frame. + */ + if (!ret && seq) + *seq = READ_ONCE(idev->recover_seq); + + return ret; +} + +static void thc_ipts_descriptor(struct thc_ipts *idev, const u8 *data, + size_t len) +{ + /* One answer per request: a late reply must not overwrite it. */ + if (len <= THC_IPTS_DESC_PREAMBLE || READ_ONCE(idev->have_desc)) + return; + + len = min(len - THC_IPTS_DESC_PREAMBLE, idev->max_rx); + memcpy(idev->desc, data + THC_IPTS_DESC_PREAMBLE, len); + idev->desc_len = len; + /* The bytes and the length have to land before the flag is seen. */ + smp_store_release(&idev->have_desc, true); + wake_up(&idev->wq); +} + +static void thc_ipts_feature(struct thc_ipts *idev, const u8 *data, + size_t len) +{ + if (!len) + return; + + /* Only a frame that arrives while a request is waiting is taken. */ + mutex_lock(&idev->feature_lock); + if (!idev->feature_pending || + (idev->feature_id && data[0] != idev->feature_id)) { + mutex_unlock(&idev->feature_lock); + return; + } + + len = min(len, idev->max_rx); + memcpy(idev->feature, data, len); + idev->feature_len = len; + WRITE_ONCE(idev->feature_done, true); + mutex_unlock(&idev->feature_lock); + + wake_up(&idev->wq); +} + +static void thc_ipts_handle_frame(struct thc_ipts *idev, size_t len) +{ + const u8 *payload = idev->rx_frame + THC_IPTS_RX_HDR_SIZE; + size_t capacity = idev->max_rx - THC_IPTS_RX_HDR_SIZE; + u32 code, size; + + if (len < THC_IPTS_RX_HDR_SIZE) + return; + + code = get_unaligned_le32(idev->rx_frame); + /* The header may claim more than what actually arrived. */ + size = min_t(u32, get_unaligned_le32(idev->rx_frame + 4), capacity); + size = min_t(u32, size, len - THC_IPTS_RX_HDR_SIZE); + + switch (code) { + case THC_IPTS_RX_INPUT: { + struct hid_device *hdev = READ_ONCE(idev->hid); + + if (hdev) + hid_safe_input_report(hdev, HID_INPUT_REPORT, + (u8 *)payload, capacity, size, 1); + break; + } + case THC_IPTS_RX_DESC: + thc_ipts_descriptor(idev, payload, size); + break; + case THC_IPTS_RX_FEATURE: + thc_ipts_feature(idev, payload, size); + break; + case THC_IPTS_RX_RESET: + dev_warn(idev->dev, "controller asked for a reset\n"); + break; + default: + dev_dbg(idev->dev, "frame code %u, %u bytes\n", code, size); + break; + } +} + +static void thc_ipts_dma_stop(struct thc_ipts *idev) +{ + /* + * A failed reset leaves the state below DMA with the tables still + * allocated. + */ + if (READ_ONCE(idev->state) >= THC_IPTS_CONFIG) + thc_dma_unconfigure(idev->thc); + + WRITE_ONCE(idev->state, THC_IPTS_CONFIG); + thc_dma_release(idev->thc); +} + +static int thc_ipts_resize(struct thc_ipts *idev, u8 **buf, size_t *have, + size_t want) +{ + u8 *p; + + if (*have >= want) + return 0; + + p = devm_krealloc(idev->dev, *buf, want, GFP_KERNEL); + if (!p) + return -ENOMEM; + + *buf = p; + *have = want; + + return 0; +} + +static int thc_ipts_dma_start(struct thc_ipts *idev) +{ + int ret; + + /* + * The core refuses frames larger than the receive ring: fail here instead + * of in sgl_alloc(). + */ + if (idev->max_rx > THC_IPTS_RX_MAX) { + dev_err(idev->dev, "receive buffer of %zu bytes is too large\n", + idev->max_rx); + return -ENODEV; + } + + /* + * Follow the limits a resume reports, or the engines write past the + * buffers. + */ + ret = thc_ipts_resize(idev, &idev->rx_frame, &idev->buf_rx, + round_up(idev->max_rx, SZ_4K)); + if (!ret) + ret = thc_ipts_resize(idev, &idev->tx_frame, &idev->buf_tx, + round_up(idev->max_tx, 4)); + if (!ret) + ret = thc_ipts_resize(idev, &idev->desc, &idev->buf_desc, + idev->max_rx); + if (!ret) + ret = thc_ipts_resize(idev, &idev->feature, &idev->buf_feat, + idev->max_rx); + if (ret) + return ret; + + /* Only RXDMA2 (in) and TXDMA (out) are used; the other two stay 0. */ + ret = thc_dma_set_max_packet_sizes(idev->thc, 0, idev->max_rx, + idev->max_tx, 0); + if (ret) + return ret; + + ret = thc_dma_allocate(idev->thc); + if (ret) + return ret; + + ret = thc_dma_configure(idev->thc); + if (ret) { + thc_dma_release(idev->thc); + return ret; + } + + return 0; +} + +irqreturn_t thc_ipts_hardirq(int irq, void *data) +{ + struct thc_ipts *idev = data; + + /* + * Nothing of ours is pending: the cause belongs to a sharer. A reset + * in flight owns the status registers and acknowledges nothing. + */ + if (READ_ONCE(idev->state) < THC_IPTS_DMA || + READ_ONCE(idev->recovering)) + return IRQ_NONE; + + /* Level triggered and shared: mask first, or it stays asserted. */ + thc_interrupt_enable(idev->thc, false); + + /* + * A teardown still needs this thread: it delivers the completion a + * pending write holds the bus lock for. + */ + return IRQ_WAKE_THREAD; +} + +static int thc_ipts_read_frames(struct thc_ipts *idev) +{ + for (;;) { + int finished = 0; + size_t len = 0; + + if (thc_rxdma_read(idev->thc, THC_RXDMA2, idev->rx_frame, + &len, &finished)) { + dev_warn_once(idev->dev, + "cannot read a frame from the engine\n"); + return -EIO; + } + + if (!len) + break; + + thc_ipts_handle_frame(idev, len); + if (finished) + break; + } + + return 0; +} + +/* + * Refuse new requests; the work item masks the port once a pending write + * is done. + */ +static void thc_ipts_request_recover(struct thc_ipts *idev) +{ + if (READ_ONCE(idev->recover_pending)) + return; + + WRITE_ONCE(idev->recover_pending, true); + schedule_work(&idev->recover_work); + /* A request waiting for the port looks at that flag. */ + wake_up(&idev->wq); +} + +/* + * The reset waits for the bus lock a pending write holds. + */ +void thc_ipts_recover(struct work_struct *work) +{ + struct thc_ipts *idev = container_of(work, struct thc_ipts, + recover_work); + + /* + * A write on the wire needs the interrupt this reset is about to mask: + * let it finish first. + */ + mutex_lock(&idev->tx_lock); + WRITE_ONCE(idev->recovering, true); + mutex_unlock(&idev->tx_lock); + + /* + * Mask, then wait out a run already inside: the status registers belong + * to the reset. + */ + thc_interrupt_enable(idev->thc, false); + synchronize_irq(idev->irq); + /* + * A run may have unmasked on its way out: mask again. + */ + thc_interrupt_enable(idev->thc, false); + + if (thc_rxdma_reset(idev->thc)) { + /* + * Nothing left to reset: stay masked and fail with -ENODEV. + */ + dev_err(idev->dev, "cannot reset the receive engine\n"); + WRITE_ONCE(idev->state, THC_IPTS_CONFIG); + WRITE_ONCE(idev->recover_seq, READ_ONCE(idev->recover_seq) + 1); + WRITE_ONCE(idev->recover_pending, false); + WRITE_ONCE(idev->recovering, false); + wake_up(&idev->wq); + return; + } + + /* + * Clear the queued flag before the running one. + */ + WRITE_ONCE(idev->recover_seq, READ_ONCE(idev->recover_seq) + 1); + WRITE_ONCE(idev->recover_pending, false); + WRITE_ONCE(idev->recovering, false); + + /* A request waiting out the reset is looking at that flag. */ + wake_up(&idev->wq); + + if (!READ_ONCE(idev->stopping)) + thc_interrupt_enable(idev->thc, true); +} + +irqreturn_t thc_ipts_thread(int irq, void *data) +{ + struct thc_ipts *idev = data; + irqreturn_t ret = IRQ_NONE; + bool need_recover = false; + bool reported = false; + int pass; + + /* + * Remember this thread: a request from it would wait for a completion + * only it delivers. + */ + WRITE_ONCE(idev->irq_thread, current); + + /* + * A sharer can queue a second run: mask again. + */ + thc_interrupt_enable(idev->thc, false); + + /* + * The port stays masked while reading: causes only leave status bits. + */ + for (pass = 0; pass < THC_IPTS_IRQ_PASSES; pass++) { + int mask; + + /* + * A reset owns the status registers: acknowledge nothing here. + */ + if (READ_ONCE(idev->recovering)) + break; + + mask = thc_interrupt_handler(idev->thc); + + if (mask == BIT(THC_UNKNOWN_INT)) + break; + + ret = IRQ_HANDLED; + if (mask & (BIT(THC_FATAL_ERR_INT) | BIT(THC_TXN_ERR_INT))) { + /* + * An overrun repeats until the reset: report the first one. + */ + if (!reported && !READ_ONCE(idev->recover_pending)) { + dev_err(idev->dev, + "transfer error, resetting the engine\n"); + reported = true; + } + /* The teardown has its own reset to wait for. */ + if (!READ_ONCE(idev->stopping)) + need_recover = true; + /* + * The core stops at the first cause; a completion behind it would time + * out. + */ + continue; + } + + /* + * A reset is queued or running: read the status, not the ring. + */ + if (need_recover || READ_ONCE(idev->recover_pending) || + READ_ONCE(idev->recovering) || + READ_ONCE(idev->stopping)) + continue; + + if (mask & BIT(THC_RXDMA2_INT) && thc_ipts_read_frames(idev)) + need_recover = true; + } + + /* + * Only queue the reset: a frame read now would corrupt the ring. + */ + if (need_recover) + thc_ipts_request_recover(idev); + + /* + * Keep the line open unless a reset is running: a write on the wire needs + * this handler to deliver its completion. + */ + if (!READ_ONCE(idev->recovering)) + thc_interrupt_enable(idev->thc, true); + + return ret; +} + /** * thc_ipts_port_start - bring one port up * @idev: port to start @@ -90,6 +511,11 @@ int thc_ipts_port_start(struct thc_ipts *idev) bool supported; int ret; + WRITE_ONCE(idev->stopping, false); + WRITE_ONCE(idev->recover_pending, false); + WRITE_ONCE(idev->recovering, false); + WRITE_ONCE(idev->feature_off, false); + ret = thc_interrupt_quiesce(idev->thc, true); if (ret) return ret; @@ -193,6 +619,16 @@ int thc_ipts_port_start(struct thc_ipts *idev) return ret; WRITE_ONCE(idev->state, THC_IPTS_CONFIG); + /* Sets the type field to invalid; RXDMA2 start writes the real one. */ + thc_interrupt_config(idev->thc); + + ret = thc_ipts_dma_start(idev); + if (ret) + return ret; + WRITE_ONCE(idev->state, THC_IPTS_DMA); + + thc_interrupt_enable(idev->thc, true); + return 0; } @@ -202,11 +638,38 @@ int thc_ipts_port_start(struct thc_ipts *idev) */ void thc_ipts_port_stop(struct thc_ipts *idev) { + /* + * Refuse requests, then wait for one inside thc_dma_write(): its + * completion comes through this handler. + */ + WRITE_ONCE(idev->stopping, true); + wake_up(&idev->wq); + mutex_lock(&idev->tx_lock); + mutex_unlock(&idev->tx_lock); + + thc_interrupt_enable(idev->thc, false); + synchronize_irq(idev->irq); + cancel_work_sync(&idev->recover_work); + + /* A run that was already in flight may have unmasked on its way out. */ + thc_interrupt_enable(idev->thc, false); + + /* Stop the controller and keep it in reset until the next start. */ if (READ_ONCE(idev->state) >= THC_IPTS_CONFIG) thc_ipts_touch_set(idev, false); - /* Keep the touch IC in reset until the next start. */ thc_port_reset(idev->thc, true); - thc_interrupt_quiesce(idev->thc, true); + + /* + * The tables are not devm-managed; release them even if the engine did + * not stop. + */ + if (thc_interrupt_quiesce(idev->thc, true)) + dev_warn(idev->dev, "engine did not stop, releasing anyway\n"); + thc_ipts_dma_stop(idev); WRITE_ONCE(idev->state, THC_IPTS_DOWN); + + /* Wait for a handler that may still look at the ring. */ + synchronize_irq(idev->irq); + thc_interrupt_enable(idev->thc, false); } diff --git a/drivers/hid/intel-thc-hid/intel-ipts/ipts.h b/drivers/hid/intel-thc-hid/intel-ipts/ipts.h index f8a55ac45..f27ad7f03 100644 --- a/drivers/hid/intel-thc-hid/intel-ipts/ipts.h +++ b/drivers/hid/intel-thc-hid/intel-ipts/ipts.h @@ -6,8 +6,13 @@ #include #include #include +#include #include +#include +#include +#include #include +#include #include "intel-thc-dev.h" @@ -38,6 +43,13 @@ #define THC_IPTS_CFG_TRIES 10 #define THC_IPTS_CFG_RETRY_MS 100 +/* A report descriptor arrives with eight bytes of preamble in front of it. */ +#define THC_IPTS_DESC_PREAMBLE 8 + +/* Retry budget for the report descriptor handshake. */ +#define THC_IPTS_REQ_TRIES 6 +#define THC_IPTS_REQ_TIMEOUT_MS 300 + #define THC_IPTS_CFG_KNOWN 40 /* The block as far as this driver reads it, padded to its full size. */ @@ -92,6 +104,17 @@ static_assert(offsetof(struct thc_ipts_cfg, device_id) == 0x14); #define THC_IPTS_RX_HDR_SIZE 64 #define THC_IPTS_TX_HDR_SIZE 8 +/* Frame codes. The same numbers mean different things per direction. */ +#define THC_IPTS_RX_INPUT 3 +#define THC_IPTS_RX_FEATURE 4 +#define THC_IPTS_RX_DESC 5 +#define THC_IPTS_RX_RESET 7 + +#define THC_IPTS_TX_SET_FEATURE 3 +#define THC_IPTS_TX_GET_FEATURE 4 +#define THC_IPTS_TX_OUTPUT 5 +#define THC_IPTS_TX_GET_DESC 7 + /** * enum thc_ipts_state - how far the port has been brought up * @THC_IPTS_DOWN: no controller, no buffers, no HID device @@ -116,24 +139,84 @@ enum thc_ipts_state { * struct thc_ipts - state of one THC port in IPTS mode * @dev: device used for resources and logging * @thc: shared THC core context + * @irq: interrupt line of the port * @cfg: configuration block last read from the controller * @vendor: vendor ID the controller reports for its HID device * @product: product ID the controller reports for its HID device * @max_rx: largest frame the controller may send, in bytes * @max_tx: largest frame the driver may send, in bytes + * @buf_rx: allocated size of @rx_frame + * @buf_tx: allocated size of @tx_frame + * @buf_desc: allocated size of @desc + * @buf_feat: allocated size of @feature + * @rx_frame: buffer for one received frame, including its header + * @tx_frame: buffer for one frame to send, including its header + * @desc: copy of the report descriptor, past its preamble + * @desc_len: length of @desc + * @have_desc: a descriptor arrived and has not been consumed yet + * @feature: buffer a feature report is collected in + * @feature_len: length of the report in @feature + * @feature_id: report ID that was asked for, checked against the answer + * @feature_pending: a feature report was asked for and has not arrived yet + * @feature_done: a feature report arrived and has not been consumed yet + * @feature_off: a request timed out and no more are taken until the port + * restarts + * @feature_lock: protects the feature report buffer and its flags + * @wq: waited on for a report descriptor or a feature report + * @tx_lock: serialises writes into @tx_frame + * @hid: HID device the reports are handed to, once it has been registered + * @recover_work: resets the receive engine after a transfer error * @state: how far the port has been brought up, see enum thc_ipts_state + * @recover_pending: a reset is wanted, refuse new requests + * @recovering: a reset is queued or running, ignore the interrupt + * @recover_seq: number of resets that ran, to tell a dropped answer apart + * @stopping: the port is being torn down, ignore the interrupt + * @irq_thread: thread the port interrupt is handled in, once it has run */ struct thc_ipts { struct device *dev; struct thc_device *thc; + int irq; struct thc_ipts_cfg cfg; u16 vendor; u16 product; size_t max_rx; size_t max_tx; + size_t buf_rx; + size_t buf_tx; + size_t buf_desc; + size_t buf_feat; + u8 *rx_frame; + u8 *tx_frame; + u8 *desc; + size_t desc_len; + bool have_desc; + u8 *feature; + size_t feature_len; + u8 feature_id; + bool feature_pending; + bool feature_done; + bool feature_off; + struct mutex feature_lock; /* feature report handshake */ + wait_queue_head_t wq; + struct mutex tx_lock; /* transmit frame */ + struct hid_device *hid; + struct work_struct recover_work; enum thc_ipts_state state; + bool recover_pending; + bool recovering; + unsigned int recover_seq; + bool stopping; + struct task_struct *irq_thread; }; +int thc_ipts_send(struct thc_ipts *idev, u32 code, const void *data, + size_t len, unsigned int *seq); + +irqreturn_t thc_ipts_hardirq(int irq, void *data); +irqreturn_t thc_ipts_thread(int irq, void *data); +void thc_ipts_recover(struct work_struct *work); + int thc_ipts_port_start(struct thc_ipts *idev); void thc_ipts_port_stop(struct thc_ipts *idev); diff --git a/drivers/hid/intel-thc-hid/intel-ipts/pci-ipts.c b/drivers/hid/intel-thc-hid/intel-ipts/pci-ipts.c index 5e826d092..746ef27e2 100644 --- a/drivers/hid/intel-thc-hid/intel-ipts/pci-ipts.c +++ b/drivers/hid/intel-thc-hid/intel-ipts/pci-ipts.c @@ -38,6 +38,7 @@ static int thc_ipts_probe(struct pci_dev *pci, const struct pci_device_id *id) { struct thc_ipts *idev; void __iomem *mem; + unsigned long flags; int ret; idev = devm_kzalloc(&pci->dev, sizeof(*idev), GFP_KERNEL); @@ -45,7 +46,11 @@ static int thc_ipts_probe(struct pci_dev *pci, const struct pci_device_id *id) return -ENOMEM; idev->dev = &pci->dev; + mutex_init(&idev->tx_lock); + mutex_init(&idev->feature_lock); + init_waitqueue_head(&idev->wq); pci_set_drvdata(pci, idev); + INIT_WORK(&idev->recover_work, thc_ipts_recover); ret = pcim_enable_device(pci); if (ret) { @@ -77,9 +82,51 @@ static int thc_ipts_probe(struct pci_dev *pci, const struct pci_device_id *id) if (IS_ERR(idev->thc)) return PTR_ERR(idev->thc); + /* + * Firmware may leave the global enable set: mask until a handler is in + * place. + */ + thc_interrupt_enable(idev->thc, false); + + /* + * Remapping rejects MSI here, so take the legacy pin; MSI only if no pin. + */ + flags = IRQF_SHARED | IRQF_COND_ONESHOT; + ret = pci_alloc_irq_vectors(pci, 1, 1, PCI_IRQ_INTX); + if (ret < 0) { + dev_warn(idev->dev, "no interrupt pin, falling back to MSI\n"); + ret = pci_alloc_irq_vectors(pci, 1, 1, + PCI_IRQ_MSI | PCI_IRQ_MSIX); + /* + * A message interrupt is neither shared nor oneshot. + */ + flags = 0; + } + if (ret < 0) { + dev_err(idev->dev, "no interrupt vector, error %d\n", ret); + return ret; + } + + idev->irq = pci_irq_vector(pci, 0); + if (idev->irq < 0) { + dev_err(idev->dev, "no interrupt number, error %d\n", + idev->irq); + return idev->irq; + } + + /* Oneshot only if a sharer has it: the hardirq masks the port. */ + ret = devm_request_threaded_irq(idev->dev, idev->irq, thc_ipts_hardirq, + thc_ipts_thread, flags, KBUILD_MODNAME, + idev); + if (ret) { + dev_err(idev->dev, "cannot get the interrupt, error %d\n", ret); + return ret; + } + ret = thc_ipts_port_start(idev); if (ret) { thc_ipts_port_stop(idev); + devm_free_irq(idev->dev, idev->irq, idev); return ret; } @@ -96,6 +143,11 @@ static void thc_ipts_remove(struct pci_dev *pci) struct thc_ipts *idev = pci_get_drvdata(pci); thc_ipts_port_stop(idev); + + /* + * Free the handler before the vector goes. + */ + devm_free_irq(idev->dev, idev->irq, idev); pci_clear_master(pci); }