mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sreeraj S Kurup <sreekuttan2156239@gmail.com>
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 <sreekuttan2156239@gmail.com>
Subject: [PATCH 1/3] ALSA: hda: core: Fix command creation error handling
Date: Thu,  3 Dec 2026 17:05:00 +0000	[thread overview]
Message-ID: <20261203170741.3873-2-sreekuttan2156239@gmail.com> (raw)
In-Reply-To: <20261203170741.3873-1-sreekuttan2156239@gmail.com>

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 <sreekuttan2156239@gmail.com>
---
 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


  reply	other threads:[~2026-12-03 17:08 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 17:04 [PATCH 0/3] ALSA: hda: Separate codec read status from response Sreeraj S Kurup
2026-12-03 17:05 ` Sreeraj S Kurup [this message]
2026-12-03 17:05 ` [PATCH 2/3] ALSA: hda: hdmi_chmap: Update HDMI channel map codec read callers Sreeraj S Kurup
2026-12-03 17:05 ` [PATCH 3/3] ASoC: codecs: hdac_hdmi: Update codec read API callers Sreeraj S Kurup
2026-12-03 17:33 ` [PATCH 0/3] ALSA: hda: Separate codec read status from response Takashi Iwai
2026-12-03 18:10   ` Sreeraj S Kurup
2026-12-03 18:31   ` Sreeraj S Kurup

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261203170741.3873-2-sreekuttan2156239@gmail.com \
    --to=sreekuttan2156239@gmail.com \
    --cc=broonie@kernel.org \
    --cc=lgirdwood@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=perex@perex.cz \
    --cc=tiwai@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®