From patchwork Thu Sep 5 04:17:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Torokhov X-Patchwork-Id: 13791718 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id DA196CD4F57 for ; Thu, 5 Sep 2024 04:30:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=IJ0f2V5xQlnWNK4EvLWQUiJbt0aWje4MjkaawAE2fXI=; b=W7QRa96e6/G/fDQwYHZvKJ38l7 0HhYOMelaDHE8gFGafV3wuxH7qvANIg3ymqkGDEerahu6IhlzCCnreNhO1a9DeNLSjBvEKG7cPcGg c0F533n/OJ6kKTvgkMzqP/Ug5wWTOvRNe/o/NJcGE8XSlo3kGAu9ktBVfrvasKBHvPn71I4ih3I0S 80N/cH8BxdS60SnXL59MDTgsdM5pImhuzGLr9N5QbqFbo/xkQihqX4OSBnI/jR0hud7k8rqoKz9dl RdoSU6dTjTAT6mJ22L/fMpHVU2y+oTvIo6SijmeH1YY/uaGL9ZoqqDmC+blEuErCotOBD7fqty2Rf HU7IDrtw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sm48J-00000006w5A-1KIz; Thu, 05 Sep 2024 04:30:11 +0000 Received: from mail-ot1-x334.google.com ([2607:f8b0:4864:20::334]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sm3wY-00000006tRU-3dUg for linux-arm-kernel@lists.infradead.org; Thu, 05 Sep 2024 04:18:04 +0000 Received: by mail-ot1-x334.google.com with SMTP id 46e09a7af769-70f6118f1b5so284045a34.0 for ; Wed, 04 Sep 2024 21:18:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725509881; x=1726114681; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=IJ0f2V5xQlnWNK4EvLWQUiJbt0aWje4MjkaawAE2fXI=; b=WmuSmenxZC7jqWTx8F5nZhPOggEGRzQHMmzPd/PhcgSD3dibw6M3ib9KqRRfCj+tZJ BxE3DBUPGydzAtroTKT2EGu/Mux6Mm3QLIC6g79oy7OLZWB/yd106u8tkeu4PBsMn5sP qglwem6BwzOtW3gui1VN2+PG3KbcALnJ9161unEh5H8+GLCRwF0s5gxSxbp41M+fAZu2 9mGIuGnoWWujhwxKw/h+z42P2Qaffp4YrxK0bMfiYsQh8dId5pxnp321zw6bKSuULTwf kuyx4BLZTdYDHtnlGJgfbu6t201TNtD9zErjaw1drul2lD+BCX08oL8ElB/jiekUkcxx hrVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725509881; x=1726114681; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=IJ0f2V5xQlnWNK4EvLWQUiJbt0aWje4MjkaawAE2fXI=; b=t3fo2BOYA4PZKuo5uEuvGnBiwS7nz+KSNyrj0PKZ1bVY94ushdPfuuiePqq1wBZBrD If48EkPqwvM7YJ6FG/Wfv0tQes07KcUigIWs8+w5vK7DQzQsKBUyT4L5A3TuqX+KLbi1 goHd+OO4gO/Jd8SISmCBSlQs7dDN3NucvXgi5dv+XUBR9NSRDyDE1zVIzcDr5DIO3B/f 9jeGdlJZx6CUFkjABOapbSnniLaPBnBzY/v7+yet5WUJ7rNavFWSIvTg5kaGiBrsNuYp EQtqLw9xdaxdoFJUYXgKss3ktRi7SHU+7dNyHw8iWayIrskZZnb3z9i3vao83MUaUcxU Gd0w== X-Forwarded-Encrypted: i=1; AJvYcCWEkInmF63jKHE+nbrdO+bPY71Ez6552pZfQ2aWOgxcjYHTfbowNV6Xw4lNkSWRRwhLBE6FPBdIIpsfEVswxpxy@lists.infradead.org X-Gm-Message-State: AOJu0YwOcHP1WbPSD9tQ5kZvcPiDQdoR/KzqlhTNS39PNxWdCRKDAUQ3 PjpQf+uwFZrSt4m3wwYCCsmDbU+R1uLwSpSfU4fLwJHR7laYAyWjSDJyLg== X-Google-Smtp-Source: AGHT+IFfwqWs2Krwm9FKRLXZTcp8pEOgI1FCBF6/QQpcb7fZKh+2/hIYDHYeoNIAXonuZwb4W4JP/Q== X-Received: by 2002:a05:6830:2d8b:b0:710:b588:2552 with SMTP id 46e09a7af769-710b5882e6bmr5386695a34.12.1725509881402; Wed, 04 Sep 2024 21:18:01 -0700 (PDT) Received: from dtor-ws.sjc.corp.google.com ([2620:15c:9d:2:13bd:b4e:4c0f:4c37]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7d4fbd8d52esm2450216a12.32.2024.09.04.21.18.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 21:18:00 -0700 (PDT) From: Dmitry Torokhov To: linux-input@vger.kernel.org Cc: =?utf-8?q?Pali_Roh=C3=A1r?= , Helge Deller , "K. Y. Srinivasan" , Wei Liu , Dexuan Cui , Samuel Holland , Lyude Paul , Michal Simek , Hans de Goede , linux-kernel@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-sunxi@lists.linux.dev Subject: [PATCH 12/24] Input: i8042 - tease apart interrupt handler Date: Wed, 4 Sep 2024 21:17:17 -0700 Message-ID: <20240905041732.2034348-13-dmitry.torokhov@gmail.com> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog In-Reply-To: <20240905041732.2034348-1-dmitry.torokhov@gmail.com> References: <20240905041732.2034348-1-dmitry.torokhov@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240904_211802_938184_7AE55572 X-CRM114-Status: GOOD ( 25.28 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org In preparation to using guard notation when acquiring mutexes and spinlocks factor out handling of active multiplexing mode from i8042_interrupt(). Signed-off-by: Dmitry Torokhov --- drivers/input/serio/i8042.c | 139 +++++++++++++++++++++--------------- 1 file changed, 83 insertions(+), 56 deletions(-) diff --git a/drivers/input/serio/i8042.c b/drivers/input/serio/i8042.c index 8ec4872b4471..674cd155ec8f 100644 --- a/drivers/input/serio/i8042.c +++ b/drivers/input/serio/i8042.c @@ -178,7 +178,7 @@ static unsigned char i8042_suppress_kbd_ack; static struct platform_device *i8042_platform_device; static struct notifier_block i8042_kbd_bind_notifier_block; -static irqreturn_t i8042_interrupt(int irq, void *dev_id); +static bool i8042_handle_data(int irq); static bool (*i8042_platform_filter)(unsigned char data, unsigned char str, struct serio *serio); @@ -434,7 +434,7 @@ static void i8042_port_close(struct serio *serio) * See if there is any data appeared while we were messing with * port state. */ - i8042_interrupt(0, NULL); + i8042_handle_data(0); } /* @@ -518,12 +518,68 @@ static bool i8042_filter(unsigned char data, unsigned char str, } /* - * i8042_interrupt() is the most important function in this driver - - * it handles the interrupts from the i8042, and sends incoming bytes - * to the upper layers. + * i8042_handle_mux() handles case when data is coming from one of + * the multiplexed ports. It would be simple if not for quirks with + * handling errors: + * + * When MUXERR condition is signalled the data register can only contain + * 0xfd, 0xfe or 0xff if implementation follows the spec. Unfortunately + * it is not always the case. Some KBCs also report 0xfc when there is + * nothing connected to the port while others sometimes get confused which + * port the data came from and signal error leaving the data intact. They + * _do not_ revert to legacy mode (actually I've never seen KBC reverting + * to legacy mode yet, when we see one we'll add proper handling). + * Anyway, we process 0xfc, 0xfd, 0xfe and 0xff as timeouts, and for the + * rest assume that the data came from the same serio last byte + * was transmitted (if transmission happened not too long ago). */ +static int i8042_handle_mux(u8 str, u8 *data, unsigned int *dfl) +{ + static unsigned long last_transmit; + static unsigned long last_port; + unsigned int mux_port; + + mux_port = (str >> 6) & 3; + *dfl = 0; + + if (str & I8042_STR_MUXERR) { + dbg("MUX error, status is %02x, data is %02x\n", + str, *data); + + switch (*data) { + default: + if (time_before(jiffies, last_transmit + HZ/10)) { + mux_port = last_port; + break; + } + fallthrough; /* report timeout */ + case 0xfc: + case 0xfd: + case 0xfe: + *dfl = SERIO_TIMEOUT; + *data = 0xfe; + break; + case 0xff: + *dfl = SERIO_PARITY; + *data = 0xfe; + break; + } + } -static irqreturn_t i8042_interrupt(int irq, void *dev_id) + last_port = mux_port; + last_transmit = jiffies; + + return I8042_MUX_PORT_NO + mux_port; +} + +/* + * i8042_handle_data() is the most important function in this driver - + * it reads the data from the i8042, determines its destination serio + * port, and sends received byte to the upper layers. + * + * Returns true if there was data waiting, false otherwise. + */ +static bool i8042_handle_data(int irq) { struct i8042_port *port; struct serio *serio; @@ -532,63 +588,24 @@ static irqreturn_t i8042_interrupt(int irq, void *dev_id) unsigned int dfl; unsigned int port_no; bool filtered; - int ret = 1; spin_lock_irqsave(&i8042_lock, flags); str = i8042_read_status(); if (unlikely(~str & I8042_STR_OBF)) { spin_unlock_irqrestore(&i8042_lock, flags); - if (irq) - dbg("Interrupt %d, without any data\n", irq); - ret = 0; - goto out; + return false; } data = i8042_read_data(); if (i8042_mux_present && (str & I8042_STR_AUXDATA)) { - static unsigned long last_transmit; - static unsigned char last_str; - - dfl = 0; - if (str & I8042_STR_MUXERR) { - dbg("MUX error, status is %02x, data is %02x\n", - str, data); -/* - * When MUXERR condition is signalled the data register can only contain - * 0xfd, 0xfe or 0xff if implementation follows the spec. Unfortunately - * it is not always the case. Some KBCs also report 0xfc when there is - * nothing connected to the port while others sometimes get confused which - * port the data came from and signal error leaving the data intact. They - * _do not_ revert to legacy mode (actually I've never seen KBC reverting - * to legacy mode yet, when we see one we'll add proper handling). - * Anyway, we process 0xfc, 0xfd, 0xfe and 0xff as timeouts, and for the - * rest assume that the data came from the same serio last byte - * was transmitted (if transmission happened not too long ago). - */ - - switch (data) { - default: - if (time_before(jiffies, last_transmit + HZ/10)) { - str = last_str; - break; - } - fallthrough; /* report timeout */ - case 0xfc: - case 0xfd: - case 0xfe: dfl = SERIO_TIMEOUT; data = 0xfe; break; - case 0xff: dfl = SERIO_PARITY; data = 0xfe; break; - } - } - - port_no = I8042_MUX_PORT_NO + ((str >> 6) & 3); - last_str = str; - last_transmit = jiffies; + port_no = i8042_handle_mux(str, &data, &dfl); } else { - dfl = ((str & I8042_STR_PARITY) ? SERIO_PARITY : 0) | - ((str & I8042_STR_TIMEOUT && !i8042_notimeout) ? SERIO_TIMEOUT : 0); + dfl = (str & I8042_STR_PARITY) ? SERIO_PARITY : 0; + if ((str & I8042_STR_TIMEOUT) && !i8042_notimeout) + dfl |= SERIO_TIMEOUT; port_no = (str & I8042_STR_AUXDATA) ? I8042_AUX_PORT_NO : I8042_KBD_PORT_NO; @@ -609,8 +626,17 @@ static irqreturn_t i8042_interrupt(int irq, void *dev_id) if (likely(serio && !filtered)) serio_interrupt(serio, data, dfl); - out: - return IRQ_RETVAL(ret); + return true; +} + +static irqreturn_t i8042_interrupt(int irq, void *dev_id) +{ + if (unlikely(!i8042_handle_data(irq))) { + dbg("Interrupt %d, without any data\n", irq); + return IRQ_NONE; + } + + return IRQ_HANDLED; } /* @@ -1216,13 +1242,14 @@ static int i8042_controller_resume(bool s2r_wants_reset) if (i8042_mux_present) { if (i8042_set_mux_mode(true, NULL) || i8042_enable_mux_ports()) pr_warn("failed to resume active multiplexor, mouse won't work\n"); - } else if (i8042_ports[I8042_AUX_PORT_NO].serio) + } else if (i8042_ports[I8042_AUX_PORT_NO].serio) { i8042_enable_aux_port(); + } if (i8042_ports[I8042_KBD_PORT_NO].serio) i8042_enable_kbd_port(); - i8042_interrupt(0, NULL); + i8042_handle_data(0); return 0; } @@ -1253,7 +1280,7 @@ static int i8042_pm_suspend(struct device *dev) static int i8042_pm_resume_noirq(struct device *dev) { if (i8042_forcenorestore || !pm_resume_via_firmware()) - i8042_interrupt(0, NULL); + i8042_handle_data(0); return 0; } @@ -1290,7 +1317,7 @@ static int i8042_pm_resume(struct device *dev) static int i8042_pm_thaw(struct device *dev) { - i8042_interrupt(0, NULL); + i8042_handle_data(0); return 0; }