From nobody Thu Oct 2 02:15:16 2025 Received: from gloria.sntech.de (gloria.sntech.de [185.11.138.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C3CF778F59 for ; Tue, 23 Sep 2025 16:09:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.11.138.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758643751; cv=none; b=Zg4fA+gPNkaBYZqGyLpklfpGaPeBCBzkR9hZtgCukKLXxlkI2LEpfe8pS9Hx2Lmf7Cxy0I7eMIJF2DNKvADgP4nEMXR0SlR0tFqlte8hxz/xO4LqBz7gGGw+MOjYqOJNs69L6cg7hlxnkB8B0KfAzPbyg4/6q3n+u/kQUZnKXs4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758643751; c=relaxed/simple; bh=nXovU8Z9lcvw9h3dBvRi7sK4wfZTfh+iEEWHXqhJ3Zs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gE0tdL7aWfgR0PI72YWiYcCCLaNrDOqV5Tb2Zl007wMsvpj/Sj+OglHcoLCVLB9W3ojZR+Rzye1goyWQMKzPL2kA7AF8dy9FveWvqold+x2PHgDUQ9F8fMVltj9R4nbYlx8u54P0X6g+7DB9ZzrGt6/TxxiAtEgVRkJn3jNfx6k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=sntech.de; spf=pass smtp.mailfrom=sntech.de; dkim=pass (2048-bit key) header.d=sntech.de header.i=@sntech.de header.b=hrJgptjG; arc=none smtp.client-ip=185.11.138.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=sntech.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sntech.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=sntech.de header.i=@sntech.de header.b="hrJgptjG" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sntech.de; s=gloria202408; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-Type; bh=/P2H1HQIzFcvBqE1RaZPIVfxAOIj1n2GfQ8xkB+a48M=; b=hrJgptjGe9W5aKrd1enuA+sSmn MKx1/Ul2LJhp3H/0IFkIMuZ4nwauMdgCGnEWABx3ndmV20WisphFQiTniPZzJGmKCcVkMHJmlT8QY ureCAY3MA457mlFwaqJh60GKQYvOZWEEJtTXYoC7EPd+cctMFKrLZm3xLwYg7gP7w+1Zl2UbVtW+y Kelhw6TYv07lO58n0yGBGwPgbpifXFHXZGFhdii6vvp4vPTwpqhn1bMs7KODJi6nT3SZYsN9vQl8w w2ZakftmKyiw5UnZ51sqTnKh4HTYTdTnUXpU3RlZ3FBNL1DqB4d0KQUZO5Xyl70Lzpg14/ELvmNcb ZMvs8PfA==; Received: from i53875a1e.versanet.de ([83.135.90.30] helo=localhost.localdomain) by gloria.sntech.de with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1v15Zi-0004vp-Vi; Tue, 23 Sep 2025 18:09:07 +0200 From: Heiko Stuebner To: lee@kernel.org Cc: linux-kernel@vger.kernel.org, heiko@sntech.de Subject: [PATCH 1/4] mfd: qnap-mcu: Calculate the checksum on the actual number of bytes received Date: Tue, 23 Sep 2025 18:08:50 +0200 Message-ID: <20250923160853.1925674-2-heiko@sntech.de> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250923160853.1925674-1-heiko@sntech.de> References: <20250923160853.1925674-1-heiko@sntech.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In the case of an error message, the number of received bytes can be less than originally expected but still contain a valid message. If the transfer itself ended in an error we would exit earlier already. So calculate the checksum on the number of received bytes and not the number of expected bytes. Signed-off-by: Heiko Stuebner --- drivers/mfd/qnap-mcu.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/mfd/qnap-mcu.c b/drivers/mfd/qnap-mcu.c index 2be429a50611..e3210541ec56 100644 --- a/drivers/mfd/qnap-mcu.c +++ b/drivers/mfd/qnap-mcu.c @@ -175,8 +175,8 @@ int qnap_mcu_exec(struct qnap_mcu *mcu, return -ETIMEDOUT; } =20 - crc =3D qnap_mcu_csum(rx, reply_data_size); - if (crc !=3D rx[reply_data_size]) { + crc =3D qnap_mcu_csum(rx, reply->received - QNAP_MCU_CHECKSUM_SIZE); + if (crc !=3D rx[reply->received - QNAP_MCU_CHECKSUM_SIZE]) { dev_err(&mcu->serdev->dev, "Invalid Checksum received\n"); return -EIO; } --=20 2.47.2 From nobody Thu Oct 2 02:15:16 2025 Received: from gloria.sntech.de (gloria.sntech.de [185.11.138.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4B7D52E2679 for ; Tue, 23 Sep 2025 16:09:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.11.138.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758643751; cv=none; b=gp5pcC1+n7EzKWYYoXyaziUEpRZcehlNxWKgTGy3AcUHTknlSA30Dxg0JjpkrIztovGi4L/55R/dviF/PMO2HnfTbxpB51rvcR2EYM6wnXJcuQesQwQRhVl4YfCb7+hcrUzIXAUuuQtV9BHDrJNGB7C9AgjbvhY5tJq2n+DPLbQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758643751; c=relaxed/simple; bh=N+Ft/PmrtbS1c2vl4AHC6AfYZVStrjBDS5Fxlyfgs08=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=IhCQGylvhmjbOlYc+JQeJp7x2R/4+fAjiExoTRmvNhpHXuIxSYpsH9MAoX3P8P46jgJDgopNApalt+l4M+AOe1EM/JGOUa9UM3pIEAI8Yr8+TWIDJB209USizrwaIAk+T6mKvCBP1OZ/TqBAEt3bVR8GvXKmT9S6uGQk/nUkzQc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=sntech.de; spf=pass smtp.mailfrom=sntech.de; dkim=pass (2048-bit key) header.d=sntech.de header.i=@sntech.de header.b=XBO6kneD; arc=none smtp.client-ip=185.11.138.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=sntech.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sntech.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=sntech.de header.i=@sntech.de header.b="XBO6kneD" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sntech.de; s=gloria202408; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-Type; bh=nQYxBu/Q09Up2UjAV3Yg5ScyfmPTkQa5TKMKUGdESZo=; b=XBO6kneDxyPXIz3mq/G6EWlO/2 p9hiVyAKvjd7otWEKBp22ZS0G0urwjXx9WjrybDKbboIxffGWOrMwEi4nsRzAF8gTjV1wHr5cEv6/ 1HbBiUqWN+UwgR9qR+BrQwgcpYDijAz9DE1tE2/9+vsz1PVbdJF12GSmgQEm1lrr3vL0c6tpJk0YI Y9JQgSuGvVL41s3kD+HRsx0zXk+UbAFUfCZn3ihOsmto5Ca4Uss5sqCCbYL1XLVbSOp89GU8Gh/1+ IoYdn75apiU+OyOcNbv0tzch4UMU2CgdmqmKH9jTldGgjQMux6QjlIT1zcllkDaMd92DiikYtEov9 D760RK9w==; Received: from i53875a1e.versanet.de ([83.135.90.30] helo=localhost.localdomain) by gloria.sntech.de with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1v15Zj-0004vp-6V; Tue, 23 Sep 2025 18:09:07 +0200 From: Heiko Stuebner To: lee@kernel.org Cc: linux-kernel@vger.kernel.org, heiko@sntech.de Subject: [PATCH 2/4] mfd: qnap-mcu: Use EPROTO in stead of EIO on checksum errors Date: Tue, 23 Sep 2025 18:08:51 +0200 Message-ID: <20250923160853.1925674-3-heiko@sntech.de> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250923160853.1925674-1-heiko@sntech.de> References: <20250923160853.1925674-1-heiko@sntech.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" EPROTO stands for protocol error and a lot of driver already use it to designate errors in the sent or received data from a peripheral. So use it in the qnap-mcu as well for checksum errors. Signed-off-by: Heiko Stuebner --- drivers/mfd/qnap-mcu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/mfd/qnap-mcu.c b/drivers/mfd/qnap-mcu.c index e3210541ec56..b4b630f7d413 100644 --- a/drivers/mfd/qnap-mcu.c +++ b/drivers/mfd/qnap-mcu.c @@ -178,7 +178,7 @@ int qnap_mcu_exec(struct qnap_mcu *mcu, crc =3D qnap_mcu_csum(rx, reply->received - QNAP_MCU_CHECKSUM_SIZE); if (crc !=3D rx[reply->received - QNAP_MCU_CHECKSUM_SIZE]) { dev_err(&mcu->serdev->dev, "Invalid Checksum received\n"); - return -EIO; + return -EPROTO; } =20 memcpy(reply_data, rx, reply_data_size); --=20 2.47.2 From nobody Thu Oct 2 02:15:16 2025 Received: from gloria.sntech.de (gloria.sntech.de [185.11.138.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4B7592E1F01 for ; Tue, 23 Sep 2025 16:09:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.11.138.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758643752; cv=none; b=RrRJm+cKkwLFJlp6P6At0oMuZ7J4I3Ju0oa9XZBdTYmox6GFA40Lvp96jOk1Zuz5OYqKBaCT6pPG/9m6Ut0tTRgnGU8VGbTOE8nN7iYTHzd+95nkKMQLrurcq4hU5y8kTzotETISQ7ak0M3fhv06QxFuxJ23Hd1EFa0GUGNRHLE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758643752; c=relaxed/simple; bh=DY/Ofcn/Ow8r+LCBGOumGnV6d/7pc2frhjwOPeU7DJ0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OaBkkngjkXubMURPeAa+SrFqkDrLe1djkjTUEZqK3jCGGuTgxEKXP1flST/5nzqDfJLYMmrhbZHpsMveGIDS0pFFxpXl/SM/7uyB4YoYoI5W9y8TDqL+ftYb1JAx3bF8DZ2JyBhxsePg3OCCmSJ1MYQZJ8ITvqpKwDuIjveQ91s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=sntech.de; spf=pass smtp.mailfrom=sntech.de; dkim=pass (2048-bit key) header.d=sntech.de header.i=@sntech.de header.b=KdyLdjmg; arc=none smtp.client-ip=185.11.138.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=sntech.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sntech.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=sntech.de header.i=@sntech.de header.b="KdyLdjmg" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sntech.de; s=gloria202408; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-Type; bh=YXITm8DEAzGdardV/rwB/49K9rMVAUg6IGseVbSkN+U=; b=KdyLdjmg7bs8ftzQi7VcHMjS+B d436hzdTJdbT1ntJ3V3AqbsrtY9bC1/Aay21ZyhbJsz5z2oSLmdS7CDRzoOzwKZOk6l2qYhVRFy5O BiG5wciEWmyEU3cG0tNUiyRfQBYRrVY5ptGs4QviqkHc9Y9RpXrHo9xCclYawqoHXKo2A2dor2VE9 OyiTEN8RAdZwRVHnJYhKUTJYm+nz4c9IcBOyJs/q+nXcR0UOf240kqqX86uEQRrBm6tGlbso8hOkA M9y2qXDPQSVelQigEDbw3l7fAfMX+2PbR1soHUhIWV8ybTzMsYIv5PXMguZz9zamgqPNfb8jAdpwb 2//lnDEA==; Received: from i53875a1e.versanet.de ([83.135.90.30] helo=localhost.localdomain) by gloria.sntech.de with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1v15Zj-0004vp-DZ; Tue, 23 Sep 2025 18:09:07 +0200 From: Heiko Stuebner To: lee@kernel.org Cc: linux-kernel@vger.kernel.org, heiko@sntech.de Subject: [PATCH 3/4] mfd: qnap-mcu: Move checksum verification to its own function Date: Tue, 23 Sep 2025 18:08:52 +0200 Message-ID: <20250923160853.1925674-4-heiko@sntech.de> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250923160853.1925674-1-heiko@sntech.de> References: <20250923160853.1925674-1-heiko@sntech.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" We'll need the checksum check in a second place in the future, so move the verification code to a separate function. Signed-off-by: Heiko Stuebner --- drivers/mfd/qnap-mcu.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/drivers/mfd/qnap-mcu.c b/drivers/mfd/qnap-mcu.c index b4b630f7d413..cd836bdd44a8 100644 --- a/drivers/mfd/qnap-mcu.c +++ b/drivers/mfd/qnap-mcu.c @@ -78,6 +78,13 @@ static u8 qnap_mcu_csum(const u8 *buf, size_t size) return csum; } =20 +static bool qnap_mcu_verify_checksum(const u8 *buf, size_t size) +{ + u8 crc =3D qnap_mcu_csum(buf, size - QNAP_MCU_CHECKSUM_SIZE); + + return crc =3D=3D buf[size - QNAP_MCU_CHECKSUM_SIZE]; +} + static int qnap_mcu_write(struct qnap_mcu *mcu, const u8 *data, u8 data_si= ze) { unsigned char tx[QNAP_MCU_TX_BUFFER_SIZE]; @@ -150,7 +157,6 @@ int qnap_mcu_exec(struct qnap_mcu *mcu, size_t length =3D reply_data_size + QNAP_MCU_CHECKSUM_SIZE; struct qnap_mcu_reply *reply =3D &mcu->reply; int ret =3D 0; - u8 crc; =20 if (length > sizeof(rx)) { dev_err(&mcu->serdev->dev, "expected data too big for receive buffer"); @@ -175,8 +181,7 @@ int qnap_mcu_exec(struct qnap_mcu *mcu, return -ETIMEDOUT; } =20 - crc =3D qnap_mcu_csum(rx, reply->received - QNAP_MCU_CHECKSUM_SIZE); - if (crc !=3D rx[reply->received - QNAP_MCU_CHECKSUM_SIZE]) { + if (!qnap_mcu_verify_checksum(rx, reply->received)) { dev_err(&mcu->serdev->dev, "Invalid Checksum received\n"); return -EPROTO; } --=20 2.47.2 From nobody Thu Oct 2 02:15:16 2025 Received: from gloria.sntech.de (gloria.sntech.de [185.11.138.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 956A324A069 for ; Tue, 23 Sep 2025 16:09:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.11.138.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758643751; cv=none; b=ZD6mhv1mAXVLf0qcNCPqX+sN14vVk1gy9oA84YtsVdCcgd3Sc1mtUjSJNYBOOjuenkO4iTDtedG1RWUB5Yxz2ZtorUzvvqNOAd97YJ1DEmarRsVJw2eDTdnntg6jteoETrAfXyTyTj1RaFegFiIh641lixKOLe1e+paJFhXi2U0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758643751; c=relaxed/simple; bh=twIJTEcCMTy0Py/D0BH4AIzZpT9lz7OmtGrhpXbLpOk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Fcpq3Ht+dlh6AQD0zZ4a0tgnytC0PX/cJ9BY+V8wY04TFhvFd5uCQ4kJnDRyL4UXiIDHpzHcYlud9f213+o7QSJMX3BppWwpRfpKcQbkoOX3bawv14dkLcF0oVQHLuj7Gne9z3PDnKuiHX3hfo6iFkBR8dImpQ7mzxEBgQPKYT8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=sntech.de; spf=pass smtp.mailfrom=sntech.de; dkim=pass (2048-bit key) header.d=sntech.de header.i=@sntech.de header.b=lipjEu70; arc=none smtp.client-ip=185.11.138.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=sntech.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sntech.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=sntech.de header.i=@sntech.de header.b="lipjEu70" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sntech.de; s=gloria202408; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-Type; bh=QL8dkAR/dAKVEaSl3fM2kEqgsUQIAz7/pBW3yRVmCNg=; b=lipjEu708ETMYWdXFONd5xOHdO Hv33OmFbNElZNLJM3I/tdhmCgb15bkfivkD57bS36eogqWVYi3jZ3MWj6mqnlYsqjYGzhSFLjx3a8 SO0VYDn6vAFa+DRQFzKfSFDE5pfUg1qeOQ8LZO+g+aVeGwDN9vIJcTMtzm4jrsqqkTMME4r4TV7gQ 0QB0/7KL9maOmJKQ9miLdV/q9bbBnFmItgYvm/AEcYQ8f+Br3V1H4QCHShOzdKJ9LebvSUzk0+CaB ZcXt5sU1rtN2e9zu0+Lf5bMUHEDlFOlSDISj9klk74K+CHiJgKKuVpPSnZPGQeRra+MfH6bR9NSuB f5Q2O9Uw==; Received: from i53875a1e.versanet.de ([83.135.90.30] helo=localhost.localdomain) by gloria.sntech.de with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1v15Zj-0004vp-Kf; Tue, 23 Sep 2025 18:09:07 +0200 From: Heiko Stuebner To: lee@kernel.org Cc: linux-kernel@vger.kernel.org, heiko@sntech.de Subject: [PATCH 4/4] mfd: qnap-mcu: Add proper error handling for command errors Date: Tue, 23 Sep 2025 18:08:53 +0200 Message-ID: <20250923160853.1925674-5-heiko@sntech.de> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250923160853.1925674-1-heiko@sntech.de> References: <20250923160853.1925674-1-heiko@sntech.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Further investigation revealed that the MCU in QNAP devices may return two error states. One "@8" for a checksum error in the submitted command and one "@9" for any generic (and sadly unspecified) error. These error codes with 2 data character can of course also be shorter then the expected reply length for the submitted command, so we'll need to check the received data for error codes and exit the receive portion early in that case. Signed-off-by: Heiko Stuebner --- drivers/mfd/qnap-mcu.c | 44 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/drivers/mfd/qnap-mcu.c b/drivers/mfd/qnap-mcu.c index cd836bdd44a8..fb78609a8433 100644 --- a/drivers/mfd/qnap-mcu.c +++ b/drivers/mfd/qnap-mcu.c @@ -19,6 +19,7 @@ /* The longest command found so far is 5 bytes long */ #define QNAP_MCU_MAX_CMD_SIZE 5 #define QNAP_MCU_MAX_DATA_SIZE 36 +#define QNAP_MCU_ERROR_SIZE 2 #define QNAP_MCU_CHECKSUM_SIZE 1 =20 #define QNAP_MCU_RX_BUFFER_SIZE \ @@ -103,6 +104,24 @@ static int qnap_mcu_write(struct qnap_mcu *mcu, const = u8 *data, u8 data_size) return serdev_device_write(mcu->serdev, tx, length, HZ); } =20 +static bool qnap_mcu_reply_is_generic_error(unsigned char *buf, size_t siz= e) +{ + return (size =3D=3D QNAP_MCU_ERROR_SIZE + QNAP_MCU_CHECKSUM_SIZE && + buf[0] =3D=3D '@' && buf[1] =3D=3D '9'); +} + +static bool qnap_mcu_reply_is_checksum_error(unsigned char *buf, size_t si= ze) +{ + return (size =3D=3D QNAP_MCU_ERROR_SIZE + QNAP_MCU_CHECKSUM_SIZE && + buf[0] =3D=3D '@' && buf[1] =3D=3D '8'); +} + +static bool qnap_mcu_reply_is_any_error(unsigned char *buf, size_t size) +{ + return (size =3D=3D QNAP_MCU_ERROR_SIZE + QNAP_MCU_CHECKSUM_SIZE && + buf[0] =3D=3D '@' && (buf[1] =3D=3D '8' || buf[1] =3D=3D '9')); +} + static size_t qnap_mcu_receive_buf(struct serdev_device *serdev, const u8 = *buf, size_t size) { struct device *dev =3D &serdev->dev; @@ -136,6 +155,19 @@ static size_t qnap_mcu_receive_buf(struct serdev_devic= e *serdev, const u8 *buf, } } =20 + /* + * We received everything the uart had to offer for now. + * Check for a possible error reply in the received data. + */ + if (reply->received =3D=3D QNAP_MCU_ERROR_SIZE + QNAP_MCU_CHECKSUM_SIZE && + qnap_mcu_verify_checksum(reply->data, reply->received) && + qnap_mcu_reply_is_any_error(reply->data, reply->received)) { + /* The reply was an error code, we're done */ + reply->length =3D 0; + + complete(&reply->done); + } + /* * The only way to get out of the above loop and end up here * is through consuming all of the supplied data, so here we @@ -182,10 +214,20 @@ int qnap_mcu_exec(struct qnap_mcu *mcu, } =20 if (!qnap_mcu_verify_checksum(rx, reply->received)) { - dev_err(&mcu->serdev->dev, "Invalid Checksum received\n"); + dev_err(&mcu->serdev->dev, "Invalid Checksum received from controller\n"= ); + return -EPROTO; + } + + if (qnap_mcu_reply_is_checksum_error(rx, reply->received)) { + dev_err(&mcu->serdev->dev, "Controller received invalid Checksum\n"); return -EPROTO; } =20 + if (qnap_mcu_reply_is_generic_error(rx, reply->received)) { + dev_err(&mcu->serdev->dev, "Generic error received from controller\n"); + return -EIO; + } + memcpy(reply_data, rx, reply_data_size); =20 return 0; --=20 2.47.2