[PATCH 1/3] ALSA: hda: core: Fix command creation error handling

From: Sreeraj S Kurup

Date: Thu Dec 03 2026 - 12:08:43 EST


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@xxxxxxxxx>
---
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