From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.160.47 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796057121; b=P0R9+qxRo0IfUYQEEAV83JzKfdrXdXZcQg2Qebqwegx/o9BHiyezanl8svrSYwPpzh96 qARFpE+baFM0dJzV72Awu47hsyG2/RmlXkZl5hivKDD8CDdOi4BKWlEUhAhqpsvqU2Axc 5w3ufWtlSm0PFErLzVxrAAjHZJ7uCDw3BswoZLgEXmpUSqXBw2Q6bwvHEQoWAWA2zMl8J cRTTED1S7l7TYydS5PnZgVE6WPYNemOktsDf/WOn8ApkQQ1e0lZeCz1U0X3fPyRDYxdqA 6Ml2OIMSwVnLnColuZyjzl0CcBAXpOmWfzUfuwx2gZs+UEfHvBdM3vR6dDq+eXnFfJQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796057121; 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=wEQWilFpoeNBm+emrDMIuoCux0u+9HvJnBAcf/JtOaM=; b=sx/8DWLwmjv3gmaCLIkxuPBG7sUNb3NdpEp9isYDC+CSHlNJgJSUwYY/ADahtDb16FR5 3TY6Mz6pemIaBMnudC0sj6p2dsVTUrE99rokonzVrVHSN2qOmeqvAsGb8w5ksBLvuiz2U yWqLBWE/wio+cNoirgxDY/4jvFaDqxeFiE73R+KtB6FttPT3uqIpxUDF1025iuZ3p7dJy mzY5ClaqJxnpaLyX6uaehzu0/YEk3GAwNMW3OSjWDNAknwKA0ir0Fsj8WbPqjcIf/AKkx o3GFOGXvGl1g/eYOXM0jsUuG9qOdRwbiBhzb+IhRnBMVWXDOs5rcTv3gc0rSAMZZoNw== 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=ehx3aaDt; arc=none smtp.remote-ip=209.85.160.47 Received: from mail-oa1-f47.google.com (mail-oa1-f47.google.com [209.85.160.47]) (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 309E160A2600 for ; Mon, 30 Nov 2026 16:45:20 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 309E160A2600 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=ehx3aaDt Received: by mail-oa1-f47.google.com with SMTP id 586e51a60fabf-48eee44b908so5903141fac.1 for ; Mon, 30 Nov 2026 08:45:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796057119; x=1796661919; 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=wEQWilFpoeNBm+emrDMIuoCux0u+9HvJnBAcf/JtOaM=; b=ehx3aaDtFZHqzDW7WSPKZdW6GEYTGjK8rqHjmfYAze0nYU49/638DP4gYeLthurHqx jkkRH5VgJOUHHtp3odGIVz/jknVQ8FeKQG3cDaRCpQv9AmBQtP21mI0Iw1gNd08q43GI f8TO2Ti7nONYwjd9JbNn2XVBC0epO0/vfoNx2qG1+MPxKdtIj98NQZUd2cRfCISrqNLE o1V5NGH/jVtHstVQb0D7Au28gbVI6pgAfk4bAqqsgg6sLWR5DWy5MbfKVRpa8B7g5Gd9 L958+zclXzsq3oP0oqzYcWi32QZZ4YloyXMdk4Tk3JbPO6TKvG95UBNTdBxJnA/1gdDx rE0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796057119; x=1796661919; 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=wEQWilFpoeNBm+emrDMIuoCux0u+9HvJnBAcf/JtOaM=; b=0BAsuqJI+WqSNBHysdNQ1+t14RP23hpyrA5W2bHqjiMt51uSfIFdtuk+X/BBYFMHwf zTvLrStudnwXVcpCFo3LZGfokD7gJmhBvNJLO7PtZCs9+ZwXOqje79uJr5sKO2xirbaY dn9qYHhEOCyUc747geJb8hIZtSoun/z1/PP3Y0gAeXTGNtejwObT98OrM0HquEJ6oLIa wEEwye7wReTwcX8LxwTD6tsS/tFUqj3DFinCWUQebGFePHpPNaqfGJR2YKt/SfhKn/Y5 5pWi9drpHtUcrc8tDdEUlqTQZY255w4S4dvimgLEMW1LTaoUaFZijtyi9v1Qnf2fsymt 2rYA== X-Forwarded-Encrypted: i=1; AL+hErHLi96OKEaM1BQQ7qdaawz7so4IXF7gVH9962bZkvrjE0XMCe6IdDtsQbMcd++zB6BWnAJ3OJVoLN3MvCk=@vger.kernel.org X-Gm-Message-State: APuU4Z/dBaip41EsG47IVYAXyRy6sa/GsgXx2qYWNFtcfzP73t7dmJc8 cCYytkqffqtBPaNtLPXRVZ8jsvY0ho1nB2b3zo9CmNlw/Yrx0dOKJ319 X-Gm-Gg: AZFdTZeO8tQ4LI0SQMOTWjSioTfoU4yGtx1Rt9gCye4gttcPioY8E05JlzERiQFccVv HgiLSLI1ByLu8Z7sDuwjyHjz/JeL+RaupZ3JL+xxmRnm0lxB/k55kZqWTw3BhLHeUMC4l8G34s9 95M2rK9wm8gZiK7vnigMkWdMKwwHsxwAWddwCdgVrt3yGeCgEcgF4QulVkbsHI/uD4bVCpw1K8Z JVKLXhPMWr/LlHxsoAMxh/k1akRO4mTppxmGmPug31Sgd5ADmGNmRYXlvHeQzf/tDGjWPdVMoXI Swwq95+qe6dQEHdYvD9k60+OfI+XVqxpTiH0uc92Et5tRcH1JY5nUVCIWL00dMsvkrIK7GZr30g xwIMmpSLb6UCcZ0K5L1nkXFNjDgxr8pGymCT5RBgNIMQWAkEY+KGu2tIJDkUdzPWRpD34dXkwwD J4tJA0t2sEGJbx+nkDEOXy4YSZXtvP7QWbVHp/yPWPxqJjn1yHUrORxduGeN37m/7AxSDhCFxHh X1Zuju44lrWUstCWD8KWapGEOWg+4znX0fnexWTeuCN1A5fexrODnS3YLKjavM1LtY4udzxe6Cp klJKy0daEaxzC/pIMBMscW/Ct6FNbb+YfosevB8/Ue2NrQ== X-Received: by 2002:a05:6871:eb09:b0:4ad:5ca8:dbf7 with SMTP id 586e51a60fabf-4bc77f1d19emr30376455fac.0.1796057118956; Mon, 30 Nov 2026 08:45:18 -0800 (PST) Received: from localhost.localdomain ([2600:1702:4ff0:baa0:5054:ff:fe3d:4ddb]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-4bc5f7ae7dcsm28002053fac.8.2026.11.30.08.45.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 08:45:18 -0800 (PST) From: Will Porter To: Takashi Iwai , Jaroslav Kysela Cc: Christian Ruppert , linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org, Neil Andrews , Nikita Tarasov Subject: [PATCH v2 14/21] ALSA: mixer_audient: add control value caching Date: Mon, 30 Nov 2026 10:44:21 -0600 Message-ID: <20261130164428.2930348-15-mrwillporter@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261130164428.2930348-1-mrwillporter@gmail.com> References: <20261130164428.2930348-1-mrwillporter@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 From: Christian Ruppert In preparation of id14 support, add control value caching. Signed-off-by: Christian Ruppert Co-developed-by: Will Porter Signed-off-by: Will Porter --- sound/usb/mixer_audient.c | 144 +++++++++++++++++++++++++++----------- 1 file changed, 105 insertions(+), 39 deletions(-) diff --git a/sound/usb/mixer_audient.c b/sound/usb/mixer_audient.c index bdfcb7fcf531b..6c625b4734c47 100644 --- a/sound/usb/mixer_audient.c +++ b/sound/usb/mixer_audient.c @@ -40,6 +40,7 @@ static const DECLARE_TLV_DB_MINMAX_MUTE(snd_audient_tlv_mixer, -12800, 600); #define SND_AUDIENT_CTYPE_FLAGS_SIGNED BIT(0) #define SND_AUDIENT_CTYPE_FLAGS_INIT_HW BIT(1) #define SND_AUDIENT_CTYPE_FLAGS_GET_BROKEN BIT(2) +#define SND_AUDIENT_CTYPE_FLAGS_CACHE BIT(3) static const struct snd_audient_ctrl_type { snd_ctl_elem_type_t type; @@ -51,7 +52,7 @@ static const struct snd_audient_ctrl_type { union { long integer; } max; /* for booleans holds physical int value for true */ - u32 init; + u32 init; /* hardware and cache are initialized to this value */ const unsigned int *tlv; u8 unit; u8 sel; @@ -91,7 +92,8 @@ static const struct snd_audient_ctrl_type { }, [SND_AUDIENT_TYPE_MIXER] = { .type = SNDRV_CTL_ELEM_TYPE_INTEGER, - .flags = SND_AUDIENT_CTYPE_FLAGS_SIGNED, + .flags = SND_AUDIENT_CTYPE_FLAGS_SIGNED + | SND_AUDIENT_CTYPE_FLAGS_CACHE, .len = 2, .min.integer = -128 * 256, .max.integer = 6 * 256, @@ -285,14 +287,33 @@ static inline int snd_audient_set_cur(struct usb_mixer_interface *mixer, } -static int snd_audient_raw_get_one(struct usb_mixer_interface *const mixer, +static int snd_audient_raw_get_one(struct usb_mixer_elem_info *const elem, const struct snd_audient_ctrl_type *const ctype, - const u8 cidx, u32 *const value) + const unsigned int cidx, u32 *const value) { + struct usb_mixer_interface *const mixer = elem->head.mixer; int ret; __le32 buf = 0; s32 val; + if (cidx >= MAX_CHANNELS) + return -EINVAL; + + if (ctype->flags & SND_AUDIENT_CTYPE_FLAGS_CACHE) { + if (test_bit(cidx, elem->cached)) { + *value = elem->cache_val[cidx]; + + return ctype->len; + } else if (ctype->flags & SND_AUDIENT_CTYPE_FLAGS_GET_BROKEN) { + /* + * GET_BROKEN controls are write-only: an invalid + * cache entry does not trigger a GET. + */ + return -ENODATA; + } + } + + ret = snd_audient_get_cur(mixer, ctype, cidx, &buf); if (ret < 0) return ret; @@ -304,43 +325,55 @@ static int snd_audient_raw_get_one(struct usb_mixer_interface *const mixer, *value = val; + set_bit(cidx, elem->cached); + elem->cache_val[cidx] = val; + return ret; } -static int snd_audient_raw_set_one(struct usb_mixer_interface *const mixer, +static int snd_audient_raw_set_one(struct usb_mixer_elem_info *const elem, const struct snd_audient_ctrl_type *const ctype, - const u8 cidx, const u32 value) + const unsigned int cidx, const u32 value, bool force) { + struct usb_mixer_interface *const mixer = elem->head.mixer; int ret; u32 val; - if (ctype->flags & SND_AUDIENT_CTYPE_FLAGS_GET_BROKEN) { + if (cidx >= MAX_CHANNELS) + return -EINVAL; + + if (force) { val = !value; /* just make it different */ } else { - ret = snd_audient_raw_get_one(mixer, ctype, cidx, &val); - if (ret < 0) + ret = snd_audient_raw_get_one(elem, ctype, cidx, &val); + if (ret == -ENODATA) /* GET_BROKEN and not cached yet */ + val = !value; /* just make it different */ + else if (ret < 0) return ret; } - if (val != value) { + force = force || (ctype->flags & SND_AUDIENT_CTYPE_FLAGS_GET_BROKEN); + + if (force || (val != value)) { __le32 buf = cpu_to_le32(value); ret = snd_audient_set_cur(mixer, ctype, cidx, &buf); if (ret < 0) return ret; - return 1; + set_bit(cidx, elem->cached); + elem->cache_val[cidx] = value; } - return 0; + return val != value; } static int snd_ctl_audient_boolean_get(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ctl_val) { - struct usb_mixer_elem_list *const list = snd_kcontrol_chip(kctl); - struct usb_mixer_interface *const mixer = list->mixer; + 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); @@ -353,13 +386,17 @@ static int snd_ctl_audient_boolean_get(struct snd_kcontrol *kctl, 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(mixer, ctype, base + i, &buf); - if (ret < 0) + ret = snd_audient_raw_get_one(elem, ctype, 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) @@ -375,8 +412,8 @@ static int snd_ctl_audient_boolean_get(struct snd_kcontrol *kctl, static int snd_ctl_audient_boolean_set(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ctl_val) { - struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl); - struct usb_mixer_interface *mixer = list->mixer; + 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 @@ -389,13 +426,16 @@ static int snd_ctl_audient_boolean_set(struct snd_kcontrol *kctl, 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(mixer, ctype, base + i, buf); + ret = snd_audient_raw_set_one(elem, ctype, base + i, buf, + false); if (ret < 0) return ret; @@ -409,8 +449,8 @@ static int snd_ctl_audient_boolean_set(struct snd_kcontrol *kctl, static int snd_ctl_audient_integer_get(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ctl_val) { - struct usb_mixer_elem_list *const list = snd_kcontrol_chip(kctl); - struct usb_mixer_interface *const mixer = list->mixer; + 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); @@ -423,13 +463,17 @@ static int snd_ctl_audient_integer_get(struct snd_kcontrol *kctl, 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; int ret; - ret = snd_audient_raw_get_one(mixer, ctype, base + i, &val); - if (ret < 0) + ret = snd_audient_raw_get_one(elem, ctype, base + i, &val); + if (ret == -ENODATA) /* unknown write-only value: minimum */ + val = ctype->min.integer; + else if (ret < 0) return ret; if (val < ctype->min.integer) @@ -450,8 +494,8 @@ static int snd_ctl_audient_integer_get(struct snd_kcontrol *kctl, static int snd_ctl_audient_integer_set(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ctl_val) { - struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl); - struct usb_mixer_interface *mixer = list->mixer; + 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 @@ -471,12 +515,15 @@ static int snd_ctl_audient_integer_set(struct snd_kcontrol *kctl, 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 = ctl_val->value.integer.value[i]; int ret; - ret = snd_audient_raw_set_one(mixer, ctype, base + i, val); + ret = snd_audient_raw_set_one(elem, ctype, base + i, val, + false); if (ret < 0) return ret; @@ -517,6 +564,8 @@ static int snd_audient_add_ctrl(struct usb_mixer_interface *mixer, struct snd_kcontrol *kctl; struct usb_mixer_elem_info *elem; + int ret; + knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER; knew.name = ctrl->name; knew.info = snd_ctl_audient_info; @@ -524,7 +573,9 @@ static int snd_audient_add_ctrl(struct usb_mixer_interface *mixer, ctrl->hwchannels); knew.tlv.p = ctype->tlv; knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE - | SNDRV_CTL_ELEM_ACCESS_VOLATILE + | ((ctype->flags & SND_AUDIENT_CTYPE_FLAGS_CACHE) + ? 0 + : SNDRV_CTL_ELEM_ACCESS_VOLATILE) | (ctype->tlv ? SNDRV_CTL_ELEM_ACCESS_TLV_READ : 0); switch (ctype->type) { @@ -540,13 +591,30 @@ static int snd_audient_add_ctrl(struct usb_mixer_interface *mixer, return -EINVAL; } + elem = kzalloc_obj(*elem); + if (!elem) + return -ENOMEM; + + elem->head.mixer = mixer; + elem->channels = ctrl->channels; + elem->head.id = ctype->unit; + elem->control = ctype->sel; + elem->get_cur_broken = (ctype->flags & SND_AUDIENT_CTYPE_FLAGS_CACHE) + && (ctype->flags & SND_AUDIENT_CTYPE_FLAGS_GET_BROKEN); + + /* + * A failed probe transfer must not fail the card: warn and keep the + * control. + * We don't initialize the cache: This will be done implicitly when + * the control is first successfully accessed. + */ if (ctype->flags & SND_AUDIENT_CTYPE_FLAGS_INIT_HW) { int i; for (i = 0; i < ctrl->hwchannels; i++) { - int ret = snd_audient_raw_set_one(mixer, ctype, - ctrl->val_offs + i, - ctype->init); + ret = snd_audient_raw_set_one(elem, ctype, + ctrl->val_offs + i, + ctype->init, true); if (ret < 0) usb_audio_warn(mixer->chip, "%s: cannot set channel %d (%d)\n", @@ -554,22 +622,20 @@ static int snd_audient_add_ctrl(struct usb_mixer_interface *mixer, } } - elem = kzalloc_obj(*elem); - if (!elem) - return -ENOMEM; - - elem->head.mixer = mixer; - elem->channels = ctrl->channels; - kctl = snd_ctl_new1(&knew, elem); if (!kctl) { - kfree(elem); - return -ENOMEM; + ret = -ENOMEM; + goto free_elem; } kctl->private_free = snd_usb_mixer_elem_free; return snd_usb_mixer_add_control(&elem->head, kctl); + +free_elem: + kfree(elem); + + return ret; } -- 2.47.3