From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.43 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796317718; b=cj4gXklOeokZ9YPmSv+yVQwihUZtfA3N7GmVH3Udi4943HSkpfZia2RhoiOMV99Yws+8 4M7ipKtBG/P1Yth7enrxBv5Ag67AHydMYP8dOz3UiNcIpd+kPbuMev58lSXOO/iXRZZkK gkbYOYBxKf2DTIzrEjrEoHxtmTI25JRh0BlqdBjxh44oT7vqrJEXgxuVwRUgEnzv161/P Te4ENyebuBTEg2sCeXR8qXrQA/f4OTTBDbtD2XLsmh9yvFT+K+hbbA9lwo1NeuUQuQxRG ecrmr2jFFkEFv8vxvvovscarkAT4VBwKjQP/vGiuCpoiiFgRMWSWDd2YhyaDjaIQ4YA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796317718; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=U3UovuSMPjJ293LYXUjJNu4NAWbuxT4txQuQhfidIlg=; b=XhyfprKutfSw0M+chmf3NqaZQtcZ8XoRqwB69tGN8/N7h2Jnu0I9Canh48VXnzSZxENL 2gmsJf3AD/ZU3rPR7HIPWyLfLOHMOV89RLxF2JlbUIToxzDXYm9cAi0Krtp4DwBc7qGhm r1wQgDSUNgYcomqJcp5rtdxy+on4YvfwLvlF65vxZV/GaAa6QfGdTxyLRQCicQLW6vStM dMYopMjrf8OG7j1YrzuublL369CcDe5YdvEVW9+rvmplOxuiIMtwY6ZPjMEPoa/Rd0QTl rVOCOKchr0ioAZxDUdi8NIMxBbXD4cvbzNRPQopwub/CbqcC4OwOJ00XsQAc7t7pDWA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=X74O0p1c; arc=none smtp.remote-ip=74.125.82.43 Received: from mail-dl1-f43.google.com (mail-dl1-f43.google.com [74.125.82.43]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 750C8606A322 for ; Thu, 3 Dec 2026 17:08:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 750C8606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=X74O0p1c Received: by mail-dl1-f43.google.com with SMTP id a92af1059eb24-1b59e19b33bso2603551c88.1 for ; Thu, 03 Dec 2026 09:08:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796317717; x=1796922517; darn=vger.kernel.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:content-type; bh=U3UovuSMPjJ293LYXUjJNu4NAWbuxT4txQuQhfidIlg=; b=X74O0p1ckxMuP//Q/KLwO2kc96V9bcIoAsdIIHjsCDN1Tw3Fxa14Qq6d16M3KafeXp m9VzjOpNOgLAMAGRVb4mr1Dsnw+wO+G+vW8RlBZvO6r3g0gfmqb6bJ+V5MI7jQRgeJKb 5DT8piB/Ef4MivHdmwEfUSYXaTzFawmFXR6B3upDA4hNa92Z3nzU8GLLqtA8a3gnheNE DTN9j4rKhGKr5xCsK/Rmrf0DdCaWW+E9t+ZaDjNEqIwE1hX9IVFb6NkhapPbZD4HKRBc VX5ofkFuEjYfUnq+fKwnZXgwCI6e6Picr3n5ay7RksMUdWZVZuzHuKpnT3Lw/5plKHD8 IJlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796317717; x=1796922517; 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=U3UovuSMPjJ293LYXUjJNu4NAWbuxT4txQuQhfidIlg=; b=03O3stlJd1qd3MRM+Jnl8UhSsHZZmllWoWgIffm7iRPTWoCu37PAx+WayFXdkP0YpU Y1YL8guEI4gjCrl5PvB9oErLStaEg0lje4Gp7ofHCQ8MZRu306w+R+idf7HxDH0PcgMH ETVz8Ue8u+UZYuxRHvvedNWQwYWjGhxoHycuvxksq4eKEHm4Q6BT+0ktW7Xvf3HsITll NNLlLL14fGQeEvCe8qVWGbmfm1Uje9QB2GwxT/+GrE2yGN+KnjzoMAma+LBktHx7h9sb w+r00u9tbdeWRdgQ09v8g1T67ogG+yQlNYkIl3JM+HC+UdjZM0tx00KalWznFlsnua5q vXTw== X-Forwarded-Encrypted: i=1; AL+hErFcxqXRZEGAiYqlcKkOVDAaMJJ63iYnwIH5pNmn0WM1aedTCPi1iBwU0lEArBRft8ArZn54be1T0FNIk3s=@vger.kernel.org X-Gm-Message-State: APuU4Z/c3PnWqUxbCK7GZvYFaeT4evSLdttnnN/uweN03Yu5KrIYseTg itOmvRzNHYDLYdMKppZRiV4eIi5+Hi0s1X5Ix0dXYih7CrqqRccUUCgH X-Gm-Gg: AZFdTZfpgcidgTQAjopRU4Re78s245P1PNgWpZBvSTO/aDAtEr1bKGePaGJZMufl6+m qKxz9DMYnmpvIYduvCDheBHLC0xDO3JZYDtItw0UKl2fzZdnXsDaGE5HcmPdLBP4EYqYDF40NdU ApzF1gzp4vjHwPFxmyvpeZnd6hJPToCgcd4GxgaAGuGXHakdyyzH7097bI7FEmKM/jJ8NvoAGGu 6xnisRMoSUntV9irfxPVXEKuvw7xz9Z9x1WZjixXG7qaVDQgEWqZ3V52Smf1q72r9l94wlbfmtP Oy3XEQWSpzOdqvnz+LwCyzpEZKhxz5xOhbFzRoTS1bs8RWvbfwx22Y/0Sxhu7cdNViXTI8KJ04k 4nvuHdZ8pDvNHyuLSCHuVVIpZGxf4xd0fRylKZoSqvAq/c+3j6deu++hD0Tl7TlsX/kiaYjSaQs Hx5ni9PMCPvO9lRAfodElpvWTg5UDgCHKve0iLoPtk+tOGYtvyOUxK3YbA8/l197Gl02HxXlNxh Oy/M7BG7StQOsB2tRR3vLhWAFW+uts5VyjR3C78LYsuRJDyxo+NnCkV5Zf+sXRfmuLQGc1WQjB0 lezZEXWoFW5B+kocc1uRPQCVuOl61PEUojkCiYwPQ4ZRwAPYY+N8 X-Received: by 2002:a05:7022:e0b:b0:1a3:8d5f:6f93 with SMTP id a92af1059eb24-1b556882ac7mr63330502c88.17.1796317717147; Thu, 03 Dec 2026 09:08:37 -0800 (PST) Received: from localhost.localdomain ([103.149.158.169]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-1b5567ff27bsm123486453c88.6.2026.12.03.09.08.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 09:08:36 -0800 (PST) From: Sreeraj S Kurup To: perex@perex.cz, tiwai@suse.de, lgirdwood@gmail.com, broonie@kernel.org Cc: linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org, Sreeraj S Kurup Subject: [PATCH 1/3] ALSA: hda: core: Fix command creation error handling Date: Thu, 3 Dec 2026 17:05:00 +0000 Message-ID: <20261203170741.3873-2-sreekuttan2156239@gmail.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261203170741.3873-1-sreekuttan2156239@gmail.com> References: <20261203170741.3873-1-sreekuttan2156239@gmail.com> X-Mailing-List: linux-kernel@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 snd_hdac_make_cmd() returns -1 when one of the command fields is out of range. However, its return type is unsigned int, so -1 is converted to 0xffffffff. Since 0xffffffff can also be a valid encoded HDA command, callers cannot reliably distinguish an invalid command from a valid one. Change snd_hdac_make_cmd() to return an integer status and store the encoded command through an output parameter. Return -EINVAL when a command field is out of range and 0 on success. Update snd_hdac_read(), codec_read() and codec_write() to check the command creation status before executing the command. Also change codec_read() and snd_hdac_codec_read() to return an integer status and provide the codec response through an output parameter. This separates the response value from the error status and makes error handling clearer. Update snd_hdac_sync_power_state() to check the return status before using the codec response. This ensures that invalid commands are not passed to snd_hdac_exec_verb() and that codec responses are handled separately from errors. Signed-off-by: Sreeraj S Kurup --- include/sound/hdaudio.h | 2 +- sound/hda/core/device.c | 69 +++++++++++++++++++++++++++++------------ 2 files changed, 51 insertions(+), 20 deletions(-) diff --git a/include/sound/hdaudio.h b/include/sound/hdaudio.h index 04fd4794bcee..7d9c034efc1b 100644 --- a/include/sound/hdaudio.h +++ b/include/sound/hdaudio.h @@ -155,7 +155,7 @@ bool snd_hdac_is_supported_format(struct hdac_device *codec, hda_nid_t nid, unsigned int format); int snd_hdac_codec_read(struct hdac_device *hdac, hda_nid_t nid, - int flags, unsigned int verb, unsigned int parm); + int flags, unsigned int verb, unsigned int parm, unsigned int *res); int snd_hdac_codec_write(struct hdac_device *hdac, hda_nid_t nid, int flags, unsigned int verb, unsigned int parm); bool snd_hdac_check_power_state(struct hdac_device *hdac, diff --git a/sound/hda/core/device.c b/sound/hda/core/device.c index 6187ffaedbfa..d43c949af77c 100644 --- a/sound/hda/core/device.c +++ b/sound/hda/core/device.c @@ -219,27 +219,34 @@ EXPORT_SYMBOL_GPL(snd_hdac_codec_modalias); * @nid: NID to encode * @verb: verb to encode * @parm: parameter to encode + * @cmd: pointer to store the encoded command. * - * Return an encoded command verb or -1 for error. + * Return 0 on success, or a negative error code. + * The encoded command is stored in @cmd. */ -static unsigned int snd_hdac_make_cmd(struct hdac_device *codec, hda_nid_t nid, - unsigned int verb, unsigned int parm) +static int snd_hdac_make_cmd(struct hdac_device *codec, hda_nid_t nid, + unsigned int verb, unsigned int parm, + unsigned int *cmd) { u32 val, addr; + if (!cmd) + return -EINVAL; + addr = codec->addr; if ((addr & ~0xf) || (nid & ~0x7f) || (verb & ~0xfff) || (parm & ~0xffff)) { dev_err(&codec->dev, "out of range cmd %x:%x:%x:%x\n", addr, nid, verb, parm); - return -1; + return -EINVAL; } val = addr << 28; val |= (u32)nid << 20; val |= verb << 8; val |= parm; - return val; + *cmd = val; + return 0; } /** @@ -276,7 +283,12 @@ int snd_hdac_exec_verb(struct hdac_device *codec, unsigned int cmd, int snd_hdac_read(struct hdac_device *codec, hda_nid_t nid, unsigned int verb, unsigned int parm, unsigned int *res) { - unsigned int cmd = snd_hdac_make_cmd(codec, nid, verb, parm); + unsigned int cmd; + int err; + + err = snd_hdac_make_cmd(codec, nid, verb, parm, &cmd); + if (err) + return err; return snd_hdac_exec_verb(codec, cmd, 0, res); } @@ -1048,22 +1060,30 @@ bool snd_hdac_is_supported_format(struct hdac_device *codec, hda_nid_t nid, } EXPORT_SYMBOL_GPL(snd_hdac_is_supported_format); -static unsigned int codec_read(struct hdac_device *hdac, hda_nid_t nid, - int flags, unsigned int verb, unsigned int parm) +static int codec_read(struct hdac_device *hdac, hda_nid_t nid, + int flags, unsigned int verb, unsigned int parm, + unsigned int *res) { - unsigned int cmd = snd_hdac_make_cmd(hdac, nid, verb, parm); - unsigned int res; + unsigned int cmd; + int err; - if (snd_hdac_exec_verb(hdac, cmd, flags, &res)) - return -1; + err = snd_hdac_make_cmd(hdac, nid, verb, parm, &cmd); + if (err) + return err; + + return snd_hdac_exec_verb(hdac, cmd, flags, res); - return res; } static int codec_write(struct hdac_device *hdac, hda_nid_t nid, int flags, unsigned int verb, unsigned int parm) { - unsigned int cmd = snd_hdac_make_cmd(hdac, nid, verb, parm); + int err; + unsigned int cmd; + + err = snd_hdac_make_cmd(hdac, nid, verb, parm, &cmd); + if (err) + return err; return snd_hdac_exec_verb(hdac, cmd, flags, NULL); } @@ -1075,15 +1095,23 @@ static int codec_write(struct hdac_device *hdac, hda_nid_t nid, * @flags: optional bit flags * @verb: the verb to send * @parm: the parameter for the verb + * @res: pointer to store the codec response * * Send a single command and read the corresponding response. * - * Returns the obtained response value, or -1 for an error. + * Returns 0 on success, or a negative error code. */ int snd_hdac_codec_read(struct hdac_device *hdac, hda_nid_t nid, - int flags, unsigned int verb, unsigned int parm) + int flags, unsigned int verb, unsigned int parm, + unsigned int *res) { - return codec_read(hdac, nid, flags, verb, parm); + int err; + + err = codec_read(hdac, nid, flags, verb, parm, res); + if (err < 0) + return err; + + return 0; } EXPORT_SYMBOL_GPL(snd_hdac_codec_read); @@ -1145,8 +1173,11 @@ unsigned int snd_hdac_sync_power_state(struct hdac_device *codec, unsigned int state, actual_state, count; for (count = 0; count < 500; count++) { - state = snd_hdac_codec_read(codec, nid, 0, - AC_VERB_GET_POWER_STATE, 0); + if (snd_hdac_codec_read(codec, nid, 0, + AC_VERB_GET_POWER_STATE, 0, &state)) { + msleep(20); + break; + } if (state & AC_PWRST_ERROR) { msleep(20); break; -- 2.55.0