[RFC PATCH 5/6] ALSA: mixer_audient: Use one get and one put for all control types

From: Will Porter

Date: Wed Nov 11 2026 - 21:45:21 EST


The boolean, integer and enum get and put functions differ only in how
they convert a value between the device and the control. Replace the
six functions with snd_ctl_audient_get() and snd_ctl_audient_put().
Three helpers switch on the control type: one converts a device value
to a control value, one converts a control value back and checks its
range, and one fills a channel that has no hardware.

The locking and the order stay. Put checks every hardware channel
before it takes the lock or sends a request. Get returns -EIO for an
enum value that is not in the list.

The get functions showed an unknown write-only value as off, as the
minimum or as init, by type. Every write-only control in the tables is
a boolean whose init equals min, or an enum. So init gives the same
value for each of them: use init for all types.

No functional change for the controls in the tables.

Assisted-by: Claude:claude-fable-5-2
Assisted-by: Claude:claude-sonnet-5-5
Signed-off-by: Will Porter <mrwillporter@xxxxxxxxx>
---
sound/usb/mixer_audient.c | 284 ++++++++++++++------------------------
1 file changed, 101 insertions(+), 183 deletions(-)

diff --git a/sound/usb/mixer_audient.c b/sound/usb/mixer_audient.c
index ff7bf518743ee..edc568830366c 100644
--- a/sound/usb/mixer_audient.c
+++ b/sound/usb/mixer_audient.c
@@ -706,84 +706,6 @@ static int snd_audient_raw_set_one(struct usb_mixer_elem_info *const elem,
}


-static int snd_ctl_audient_boolean_get(struct snd_kcontrol *kctl,
- struct snd_ctl_elem_value *ctl_val)
-{
- struct usb_mixer_elem_info *const elem = snd_kcontrol_chip(kctl);
- struct usb_mixer_interface *const mixer = elem->head.mixer;
-
- const unsigned int nchan = snd_audient_kctl_channels(kctl);
- const unsigned int hwchan = snd_audient_kctl_hwchannels(kctl);
- const struct snd_audient_ctrl_type *const ctype
- = snd_audient_kctl_type(kctl);
- const u8 base = snd_audient_kctl_cidxoffs(kctl);
-
- int i;
-
- CLASS(snd_usb_lock, pm)(mixer->chip);
- if (pm.err < 0)
- return pm.err;
- /* serializes the transfers and the cache, also between gets */
- guard(mutex)(&mixer->lock);
-
- for (i = 0; i < hwchan; i++) {
- u32 buf;
- int ret;
-
- ret = snd_audient_raw_get_one(elem, ctype, i, base + i,
- &buf);
- if (ret == -ENODATA) /* unknown write-only value: off */
- buf = ctype->min.integer;
- else if (ret < 0)
- return ret;
-
- ctl_val->value.integer.value[i] = (buf == ctype->min.integer)
- ? false : true;
- }
-
- for (; i < nchan; i++)
- ctl_val->value.integer.value[i] = 0;
-
- return 0;
-}
-
-static int snd_ctl_audient_boolean_set(struct snd_kcontrol *kctl,
- struct snd_ctl_elem_value *ctl_val)
-{
- struct usb_mixer_elem_info *const elem = snd_kcontrol_chip(kctl);
- struct usb_mixer_interface *const mixer = elem->head.mixer;
-
- const unsigned int hwchan = snd_audient_kctl_hwchannels(kctl);
- const struct snd_audient_ctrl_type *const ctype
- = snd_audient_kctl_type(kctl);
- const u8 base = snd_audient_kctl_cidxoffs(kctl);
-
- int changed = 0;
- int i;
-
- CLASS(snd_usb_lock, pm)(mixer->chip);
- if (pm.err < 0)
- return pm.err;
- /* serializes the transfers and the cache, also between gets */
- guard(mutex)(&mixer->lock);
-
- for (i = 0; i < hwchan; i++) {
- u32 buf = ctl_val->value.integer.value[i] ? ctype->max.integer
- : ctype->min.integer;
- int ret;
-
- ret = snd_audient_raw_set_one(elem, ctype, i, base + i,
- buf);
- if (ret < 0)
- return ret;
-
- if (ret)
- changed = 1;
- }
-
- return changed;
-}
-
/*
* With a step size (res), an integer control counts steps from min:
* 0 ... (max - min) / res. Without one it shows the device value.
@@ -819,106 +741,104 @@ snd_audient_int_from_ctl(const struct snd_audient_ctrl_type *ctype, long val)
return ctype->res ? ctype->min.integer + val * ctype->res : val;
}

-static int snd_ctl_audient_integer_get(struct snd_kcontrol *kctl,
- struct snd_ctl_elem_value *ctl_val)
+/* The item index of a device value, or -EIO if the value is not in the list */
+static int snd_audient_enum_item(const struct snd_audient_enum *e, u32 value)
{
- struct usb_mixer_elem_info *const elem = snd_kcontrol_chip(kctl);
- struct usb_mixer_interface *const mixer = elem->head.mixer;
-
- const unsigned int nchan = snd_audient_kctl_channels(kctl);
- const unsigned int hwchan = snd_audient_kctl_hwchannels(kctl);
- const struct snd_audient_ctrl_type *const ctype
- = snd_audient_kctl_type(kctl);
- const u8 base = snd_audient_kctl_cidxoffs(kctl);
-
- int i;
+ unsigned int item;

- CLASS(snd_usb_lock, pm)(mixer->chip);
- if (pm.err < 0)
- return pm.err;
- /* serializes the transfers and the cache, also between gets */
- guard(mutex)(&mixer->lock);
+ for (item = 0; item < e->items; item++)
+ if (e->values[item] == value)
+ return item;

- for (i = 0; i < hwchan; i++) {
- s32 val;
- int ret;
+ return -EIO;
+}

- ret = snd_audient_raw_get_one(elem, ctype, i, base + i,
- &val);
- if (ret == -ENODATA) /* unknown write-only value: minimum */
- val = ctype->min.integer;
- else if (ret < 0)
- return ret;
+/*
+ * Convert a device value to the control value of hardware channel i. An
+ * enumerated value that is not in the list returns -EIO.
+ */
+static int snd_audient_value_to_ctl(const struct snd_audient_ctrl_type *ctype,
+ struct snd_ctl_elem_value *ctl_val,
+ unsigned int i, u32 raw)
+{
+ int item;

+ switch (ctype->type) {
+ case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
+ ctl_val->value.integer.value[i] = raw != ctype->min.integer;
+ break;
+ case SNDRV_CTL_ELEM_TYPE_INTEGER:
ctl_val->value.integer.value[i] =
- snd_audient_int_to_ctl(ctype, val);
- }
+ snd_audient_int_to_ctl(ctype, (s32)raw);
+ break;
+ default: /* SNDRV_CTL_ELEM_TYPE_ENUMERATED */
+ item = snd_audient_enum_item(ctype->enumeration, raw);
+ if (item < 0)
+ return item;

- for (; i < nchan; i++)
- ctl_val->value.integer.value[i] = snd_audient_int_min(ctype);
+ ctl_val->value.enumerated.item[i] = item;
+ break;
+ }

return 0;
}

-static int snd_ctl_audient_integer_set(struct snd_kcontrol *kctl,
- struct snd_ctl_elem_value *ctl_val)
+/* The control value of a channel without hardware: off, minimum, item 0 */
+static void snd_audient_empty_ctl(const struct snd_audient_ctrl_type *ctype,
+ struct snd_ctl_elem_value *ctl_val,
+ unsigned int i)
{
- struct usb_mixer_elem_info *const elem = snd_kcontrol_chip(kctl);
- struct usb_mixer_interface *mixer = elem->head.mixer;
-
- const unsigned int hwchan = snd_audient_kctl_hwchannels(kctl);
- const struct snd_audient_ctrl_type *const ctype
- = snd_audient_kctl_type(kctl);
- const u8 base = snd_audient_kctl_cidxoffs(kctl);
-
- int changed = 0;
- int i;
+ switch (ctype->type) {
+ case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
+ ctl_val->value.integer.value[i] = 0;
+ break;
+ case SNDRV_CTL_ELEM_TYPE_INTEGER:
+ ctl_val->value.integer.value[i] = snd_audient_int_min(ctype);
+ break;
+ default: /* SNDRV_CTL_ELEM_TYPE_ENUMERATED */
+ ctl_val->value.enumerated.item[i] = 0;
+ break;
+ }
+}

- for (i = 0; i < hwchan; i++) {
- long val = ctl_val->value.integer.value[i];
+/*
+ * Convert the control value of hardware channel i to a device value. A
+ * value out of range returns -EINVAL; a boolean has no range error.
+ */
+static int snd_audient_value_from_ctl(const struct snd_audient_ctrl_type *ctype,
+ const struct snd_ctl_elem_value *ctl_val,
+ unsigned int i, u32 *raw)
+{
+ unsigned int item;
+ long val;

+ switch (ctype->type) {
+ case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
+ *raw = ctl_val->value.integer.value[i] ? ctype->max.integer
+ : ctype->min.integer;
+ break;
+ case SNDRV_CTL_ELEM_TYPE_INTEGER:
+ val = ctl_val->value.integer.value[i];
if (val < snd_audient_int_min(ctype) ||
val > snd_audient_int_max(ctype))
return -EINVAL;
- }

- CLASS(snd_usb_lock, pm)(mixer->chip);
- if (pm.err < 0)
- return pm.err;
- /* serializes the transfers and the cache, also between gets */
- guard(mutex)(&mixer->lock);
-
- for (i = 0; i < hwchan; i++) {
- s32 val = snd_audient_int_from_ctl(ctype,
- ctl_val->value.integer.value[i]);
- int ret;
-
- ret = snd_audient_raw_set_one(elem, ctype, i, base + i,
- val);
- if (ret < 0)
- return ret;
+ *raw = (s32)snd_audient_int_from_ctl(ctype, val);
+ break;
+ default: /* SNDRV_CTL_ELEM_TYPE_ENUMERATED */
+ item = ctl_val->value.enumerated.item[i];
+ if (item >= ctype->enumeration->items)
+ return -EINVAL;

- if (ret)
- changed = 1;
+ *raw = ctype->enumeration->values[item];
+ break;
}

- return changed;
-}
-
-/* The item index of a device value, or -EIO if the value is not in the list */
-static int snd_audient_enum_item(const struct snd_audient_enum *e, u32 value)
-{
- unsigned int item;
-
- for (item = 0; item < e->items; item++)
- if (e->values[item] == value)
- return item;
-
- return -EIO;
+ return 0;
}

-static int snd_ctl_audient_enum_get(struct snd_kcontrol *kctl,
- struct snd_ctl_elem_value *ctl_val)
+static int snd_ctl_audient_get(struct snd_kcontrol *kctl,
+ struct snd_ctl_elem_value *ctl_val)
{
struct usb_mixer_elem_info *const elem = snd_kcontrol_chip(kctl);
struct usb_mixer_interface *const mixer = elem->head.mixer;
@@ -938,32 +858,28 @@ static int snd_ctl_audient_enum_get(struct snd_kcontrol *kctl,
guard(mutex)(&mixer->lock);

for (i = 0; i < hwchan; i++) {
- u32 val;
- int item;
+ u32 raw;
int ret;

- ret = snd_audient_raw_get_one(elem, ctype, i, base + i,
- &val);
+ ret = snd_audient_raw_get_one(elem, ctype, i, base + i, &raw);
if (ret == -ENODATA) /* unknown write-only value: init */
- val = ctype->init;
+ raw = ctype->init;
else if (ret < 0)
return ret;

- item = snd_audient_enum_item(ctype->enumeration, val);
- if (item < 0)
- return item;
-
- ctl_val->value.enumerated.item[i] = item;
+ ret = snd_audient_value_to_ctl(ctype, ctl_val, i, raw);
+ if (ret < 0)
+ return ret;
}

for (; i < nchan; i++)
- ctl_val->value.enumerated.item[i] = 0;
+ snd_audient_empty_ctl(ctype, ctl_val, i);

return 0;
}

-static int snd_ctl_audient_enum_set(struct snd_kcontrol *kctl,
- struct snd_ctl_elem_value *ctl_val)
+static int snd_ctl_audient_put(struct snd_kcontrol *kctl,
+ struct snd_ctl_elem_value *ctl_val)
{
struct usb_mixer_elem_info *const elem = snd_kcontrol_chip(kctl);
struct usb_mixer_interface *const mixer = elem->head.mixer;
@@ -971,15 +887,19 @@ static int snd_ctl_audient_enum_set(struct snd_kcontrol *kctl,
const unsigned int hwchan = snd_audient_kctl_hwchannels(kctl);
const struct snd_audient_ctrl_type *const ctype
= snd_audient_kctl_type(kctl);
- const struct snd_audient_enum *const e = ctype->enumeration;
const u8 base = snd_audient_kctl_cidxoffs(kctl);

unsigned int i;
int changed = 0;

- for (i = 0; i < hwchan; i++)
- if (ctl_val->value.enumerated.item[i] >= e->items)
- return -EINVAL;
+ for (i = 0; i < hwchan; i++) {
+ u32 raw;
+ int ret;
+
+ ret = snd_audient_value_from_ctl(ctype, ctl_val, i, &raw);
+ if (ret < 0)
+ return ret;
+ }

CLASS(snd_usb_lock, pm)(mixer->chip);
if (pm.err < 0)
@@ -988,11 +908,14 @@ static int snd_ctl_audient_enum_set(struct snd_kcontrol *kctl,
guard(mutex)(&mixer->lock);

for (i = 0; i < hwchan; i++) {
- const unsigned int item = ctl_val->value.enumerated.item[i];
+ u32 raw;
int ret;

- ret = snd_audient_raw_set_one(elem, ctype, i, base + i,
- e->values[item]);
+ ret = snd_audient_value_from_ctl(ctype, ctl_val, i, &raw);
+ if (ret < 0)
+ return ret;
+
+ ret = snd_audient_raw_set_one(elem, ctype, i, base + i, raw);
if (ret < 0)
return ret;

@@ -1057,21 +980,16 @@ static int snd_audient_add_ctrl(struct usb_mixer_interface *mixer,

switch (ctype->type) {
case SNDRV_CTL_ELEM_TYPE_INTEGER:
- knew.get = snd_ctl_audient_integer_get;
- knew.put = snd_ctl_audient_integer_set;
- break;
case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
- knew.get = snd_ctl_audient_boolean_get;
- knew.put = snd_ctl_audient_boolean_set;
- break;
case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
- knew.get = snd_ctl_audient_enum_get;
- knew.put = snd_ctl_audient_enum_set;
break;
default:
return -EINVAL;
}

+ knew.get = snd_ctl_audient_get;
+ knew.put = snd_ctl_audient_put;
+
if (ctrl->hwchannels > MAX_CHANNELS)
return -EINVAL;

--
2.47.3