[PATCH] sound: Add a generic result header

From: Sreeraj S Kurup

Date: Fri Dec 04 2026 - 06:42:18 EST


Add include/sound/snd_result.h to provide a common result
structure for sound APIs.

Some sound APIs return unsigned int values but also use
negative error codes to report failures. Since the return
type is unsigned int, negative error codes are converted
to unsigned values. This makes it hard to tell whether
the returned value is an error or a valid result.

For example, returning -1 from an unsigned int function
produces 0xffffffff, which could also be a valid value
returned by the function. This makes it difficult to
handle errors when the full range of unsigned values
can be used.

The new result structure uses a type field to identify
what kind of value is stored. It supports error codes,
signed integers, unsigned integers and pointers. Helper
functions are provided to create each type of result.

This allows sound APIs to return errors and different
kinds of values without using special values to indicate
errors.

More result types and their corresponding helper
functions can be added later as needed by other
sound APIs.

Signed-off-by: Sreeraj S Kurup <sreekuttan2156239@xxxxxxxxx>
---
include/sound/snd_result.h | 61 ++++++++++++++++++++++++++++++++++++++
1 file changed, 61 insertions(+)
create mode 100644 include/sound/snd_result.h

diff --git a/include/sound/snd_result.h b/include/sound/snd_result.h
new file mode 100644
index 000000000000..61503675e7fb
--- /dev/null
+++ b/include/sound/snd_result.h
@@ -0,0 +1,61 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Generic result type and helpers for sound APIs
+ * that need to return an error code or a value of
+ * a different type.
+ *
+ */
+
+#ifndef __SOUND_RESULT_H
+#define __SOUND_RESULT_H
+
+enum snd_result_type {
+ SND_RESULT_ERR,
+ SND_RESULT_INT,
+ SND_RESULT_UINT,
+ SND_RESULT_PTR,
+};
+
+struct snd_result {
+ enum snd_result_type type;
+ union {
+ int err;
+ int i;
+ unsigned int u;
+ void *ptr;
+ };
+};
+
+static inline struct snd_result snd_result_err(int err)
+{
+ return (struct snd_result) {
+ .type = SND_RESULT_ERR,
+ .err = err,
+ };
+}
+
+static inline struct snd_result snd_result_int(int value)
+{
+ return (struct snd_result) {
+ .type = SND_RESULT_INT,
+ .i = value,
+ };
+}
+
+static inline struct snd_result snd_result_uint(unsigned int value)
+{
+ return (struct snd_result) {
+ .type = SND_RESULT_UINT,
+ .u = value,
+ };
+}
+
+static inline struct snd_result snd_result_ptr(void *value)
+{
+ return (struct snd_result) {
+ .type = SND_RESULT_PTR,
+ .ptr = value,
+ };
+}
+
+#endif
--
2.55.0