From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f173.google.com (mail-pg1-f173.google.com [209.85.215.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4063739E197 for ; Mon, 20 Jul 2026 10:14:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.173 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784542493; cv=none; b=C0lHfllctqnlHKqA+2x9ELTlCdjzJfW7zQFGJDj3mg4S9eGbBnvDEUZwKmytBFL/uj3AJ25b+o6MmxxPe5HcRL7OJDZry68yU0DXEY3cdtEsF1AG/RVhNP7e5GyrsA0aNPi+dzf5RNZUMq9TGHsuHTnNu7W4NECntFsoM6QClaU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784542493; c=relaxed/simple; bh=zBFAtScCtJz6J3LkuunkpMbvy3Vysw16nLyJL24kSuA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=WxuAQtORD4VE8Q+Hlw+GT38WvvkiV4g3M2Qc0FZ/QE6gUiYV7PPJOOF9qIJvlKjUwAH6TOnGLnxRNkRRRrjo1Jac0Ifxf+QWLGTClSfsGvr6PRzzJ5U8Q6KC4wCaKF1lsRYfYvxZU4Wm6qH+Afi2ilWLMYU5ucuP7mEkqegqTDQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=debian.org; spf=pass smtp.mailfrom=gmail.com; arc=none smtp.client-ip=209.85.215.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=debian.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pg1-f173.google.com with SMTP id 41be03b00d2f7-ca80d708489so2389398a12.1 for ; Mon, 20 Jul 2026 03:14:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784542487; x=1785147287; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=1BlyvrkmFv1hwh49RhEdK1f4LtTECBvTt9R0UVOVdkA=; b=GQH7Fp24WY8W3GKaVlcdbZwKX7vp8JG2DszrbagB5eefRDiby3Z8tqbczo1808AXaL N++25FTqaQ+lYZ5DGjHI74gnB0MXllEMmOPNSxuyqTl4+uK8et97NyGhFCAuf3s2nf9D efJfz6o6Jqa9kUjUfgWTNo4v9FDDv7tSggcx1y8osOJ6UQNFj3MdG809fhv/j7MFYn8G rBkEzCVjxExPHxE0UT7VtN/5FFicv+JiwCBC3w0Rjf4+2ZvFxbtoVXM5xcrOlYy9t+Rg Sth+a3Hwa5riYKg1VeoRPCzPuGopdZ7wZUZFzjaBuBWWpzZI8XG6JFVx0IBurA8JifP5 y6JQ== X-Forwarded-Encrypted: i=1; AHgh+RqYTAdb2dYb44viuKyk2dx3qT5CYWrh5zCRbN73TdgH874d3fLx8/0gDUFecvxop1yg+J5lFbQo1PWLmz8=@vger.kernel.org X-Gm-Message-State: AOJu0YwWmE4Z7F36SAmKKlc85l9Ll5E3hRIXk8ooNbu4MD9tN9cpQHg4 NiX6PMQkSzfGRUYxYS/E342G0BJW08yCWFvNkb4SBKWSr1z0Umbp6nX72L1+KAmUFA== X-Gm-Gg: AfdE7cnq53IGkjmMj0k11JrtnPerz5EqjoD7XbnMV98lDvZNHTiqwtNdPOiteTCEpPq pFQ9AM85Xl1UZHGn83Q7iMEC34qUZcHx+DzjBA8pzln2ocs72EImUWJQ1AmYi8Ow74mum8t6jje W57BHjVhsIL308rMH3IZrUQx/XcEul6F6zkStyUai1kiR11eHLNsx9S4ZWnbnYlNibDrCFp0DpY Ehw4UV3S9kdU4Wk5g8/pycCkOiuJEFmyS4ImNC8+zfV+t7YaLDsrDYeCiUV8wUlzvkJMa+qgZJo WpdTx6+6ROxz9WWedIfSXvoCp0NHlFO+3YrWpl9CTY8WxGyIBbKkzHijHGU2MYZnwVsDwxALpBM 2BXw1YvMJBQkLXl+uKNjUHxT+Bpd5X8rQ0rLrid2uyI6qCGaweI+VZvV82HWXts5a+osJlwpUbF 0wSRalmwJd+RE6u2gxVGUvAaZZb8Tu9kIqukkiLRED017t/XSS8pG7OdDGlSUtdcs11iW9xrQPs To8Upxk1q84vL2kvw== X-Received: by 2002:a05:6a20:258a:b0:3bf:aa54:4cbc with SMTP id adf61e73a8af0-3c38dbb8287mr19933105637.26.1784542486563; Mon, 20 Jul 2026 03:14:46 -0700 (PDT) Received: from penguin.tail0a1999.ts.net (61-228-16-100.dynamic-ip.hinet.net. [61.228.16.100]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-cb517f94c31sm4411321a12.13.2026.07.20.03.14.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Jul 2026 03:14:46 -0700 (PDT) From: Shih-Yuan Lee To: Dmitry Torokhov Cc: Mark Brown , linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, Shih-Yuan Lee Subject: [PATCH v2 3/5] Input: applespi - register touchpad synchronously in probe Date: Mon, 20 Jul 2026 18:14:33 +0800 Message-Id: <20260720101435.13612-4-fourdollars@debian.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20260720101435.13612-1-fourdollars@debian.org> References: <20260720101435.13612-1-fourdollars@debian.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Touchpad registration is currently deferred to an asynchronous worker applespi_worker(). This asynchronous registration introduces race conditions if debugfs or other properties are accessed before the worker completes, or if the driver is unbound while the worker is active. Remove the workqueue and the asynchronous worker. Perform touchpad registration synchronously during driver probe. Wait up to 3 seconds for the touchpad information command packet response using wait_event_timeout(). If the response times out, log a warning and fallback to keyboard-only mode. If registration fails, gracefully unwind GPE handlers and wait for outstanding SPI transactions to complete. To prevent data races between the interrupt handler and the probe thread, protect the 'have_tp_info' flag and 'rcvd_tp_info' structure under the cmd_msg_lock in applespi_handle_cmd_response(). Signed-off-by: Shih-Yuan Lee --- drivers/input/keyboard/applespi.c | 52 +++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/drivers/input/keyboard/applespi.c b/drivers/input/keyboard/applespi.c index a8f8d5370e95..42b7f87ef2cd 100644 --- a/drivers/input/keyboard/applespi.c +++ b/drivers/input/keyboard/applespi.c @@ -427,7 +427,7 @@ struct applespi_data { } spi_complete[2]; bool cancel_spi; - struct work_struct work; + bool have_tp_info; struct touchpad_info_protocol rcvd_tp_info; struct dentry *debugfs_root; @@ -1388,26 +1388,20 @@ applespi_register_touchpad_device(struct applespi_data *applespi, return 0; } -static void applespi_worker(struct work_struct *work) -{ - struct applespi_data *applespi = - container_of(work, struct applespi_data, work); - - applespi_register_touchpad_device(applespi, &applespi->rcvd_tp_info); -} - static void applespi_handle_cmd_response(struct applespi_data *applespi, struct spi_packet *packet, struct message *message) { + unsigned long flags; + if (packet->device == PACKET_DEV_INFO && le16_to_cpu(message->type) == 0x1020) { - /* - * We're not allowed to sleep here, but registering an input - * device can sleep. - */ + spin_lock_irqsave(&applespi->cmd_msg_lock, flags); applespi->rcvd_tp_info = message->tp_info; - schedule_work(&applespi->work); + applespi->have_tp_info = true; + spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags); + + wake_up_all(&applespi->wait_queue); return; } @@ -1675,6 +1669,7 @@ static int applespi_probe(struct spi_device *spi) acpi_handle spi_handle = ACPI_HANDLE(&spi->dev); acpi_status acpi_sts; int sts, i; + unsigned long flags; unsigned long long gpe, usb_status; /* check if the USB interface is present and enabled already */ @@ -1692,8 +1687,6 @@ static int applespi_probe(struct spi_device *spi) applespi->spi = spi; - INIT_WORK(&applespi->work, applespi_worker); - /* store the driver data */ spi_set_drvdata(spi, applespi); @@ -1821,6 +1814,20 @@ static int applespi_probe(struct spi_device *spi) /* trigger touchpad setup */ applespi_init(applespi, false); + /* set up the touchpad as a separate input device if info is received */ + sts = wait_event_timeout(applespi->wait_queue, + READ_ONCE(applespi->have_tp_info), + msecs_to_jiffies(3000)); + if (!sts) { + dev_warn(&applespi->spi->dev, + "Timed out waiting for touchpad info, continuing keyboard-only\n"); + } else { + sts = applespi_register_touchpad_device(applespi, + &applespi->rcvd_tp_info); + if (sts) + goto cancel_spi; + } + /* * By default this device is not enabled for wakeup; but USB keyboards * generally are, so the expectation is that by default the keyboard @@ -1853,6 +1860,19 @@ static int applespi_probe(struct spi_device *spi) &applespi_tp_dim_fops); return 0; + +cancel_spi: + acpi_disable_gpe(NULL, applespi->gpe); + acpi_remove_gpe_handler(NULL, applespi->gpe, applespi_notify); + + spin_lock_irqsave(&applespi->cmd_msg_lock, flags); + applespi->cancel_spi = true; + wait_event_lock_irq(applespi->wait_queue, + !applespi_async_outstanding(applespi), + applespi->cmd_msg_lock); + spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags); + + return sts; } static void applespi_drain_writes(struct applespi_data *applespi) -- 2.39.5