All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Pavel Artemev" <a@pavel.bz>
Cc: linux-sound@vger.kernel.org
Subject: Re: [PATCH 2/2] ALSA: usb-audio: add MIXER_STATE_REPUSH quirk flag
Date: Tue, 17 Nov 2026 18:40:56 +0000	[thread overview]
Message-ID: <sashiko-outbox-219376@kernel.org> (raw)
In-Reply-To: <20261117180728.117915-3-a@pavel.bz>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] ALSA: usb-audio: unconditional master cache clear in
  mixer_ctl_feature_put()
- [Medium] ALSA: usb-audio: missing master derive on no-op put in
  mixer_ctl_feature_put()
--

commit ea1a41cc4b1413f82eda4163fef6df317f18d1ed
Author: Pavel Artemev <a@pavel.bz>

ALSA: usb-audio: add MIXER_STATE_REPUSH quirk flag

This commit adds QUIRK_FLAG_MIXER_STATE_REPUSH to defer Feature Unit volume
and mute control writes into atomic bursts and re-push cached mixer state on
PCM stream open and resume, enabling it for Weltrend/XiiSound (040b:0897)
headsets.

Link: https://bugzilla.kernel.org/show_bug.cgi?id=221401
Link: https://lore.kernel.org/linux-sound/20261117143314.41853-3-a@pavel.bz

> diff --git a/sound/usb/mixer.c b/sound/usb/mixer.c
> index ad96371add1ad..b0e799149dc07 100644
> --- a/sound/usb/mixer.c
> +++ b/sound/usb/mixer.c

[ ... ]

> @@ -1608,18 +1618,369 @@ static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol,
>  	return 0;
>  }
>  

[ ... ]

> +static void snd_usb_mixer_flush_pending(struct usb_mixer_interface *mixer,
> +					int level)
> +{
> +	struct usb_mixer_elem_list *list;
> +	struct usb_mixer_elem_info *cval, *master;
> +	int id, c, idx, max_val;
> +	bool have_ch, touched, master_pending;

[ ... ]

> +		for_each_mixer_elem(list, mixer, id) {
> +			cval = mixer_elem_list_to_info(list);
> +			if (!is_feature_volume_mute_ctl(cval) ||
> +			    cval->control != UAC_FU_VOLUME)
> +				continue;
> +			if (!cval->cmask) {
> +				if (!master) {
> +					master = cval;
> +					master_pending =
> +						test_bit(0, cval->pending);
> +				}
> +				continue;
> +			}
> +			if (!bitmap_empty(cval->pending, MAX_CHANNELS + 1))
> +				touched = true;

[ ... ]

> +		}
> +		/* master first (the Windows burst order).  Re-asserted
> +		 * even unchanged, unless the value is the control's raw
> +		 * maximum: that re-send would only trigger the in-headset
> +		 * limit notification, and the device sits at raw max
> +		 * after every reset anyway.
> +		 */
> +		if (master && !master_pending && !master->master_explicit &&
> +		    have_ch &&
> +		    (touched || level != FLUSH_TOUCHED) &&
> +		    (!test_bit(0, master->cached) ||
> +		     *master->cache_val != max_val ||
> +		     max_val != master->max)) {
> +			int err = snd_usb_set_cur_mix_value(master, 0, 0,
> +							    max_val);

[Severity: Medium]
Does reclaiming master ownership on a no-op channel write fail to derive and
write the master volume?

When an explicit master put is followed by a per-channel volume write whose
values match the cached channels (oval == val), mixer_ctl_feature_put() sets
ch_seen = true and clears m->master_explicit, bit 0 of m->pending, and bit 0
of m->cached, but never sets any bit in cval->pending or calls
mixer_arm_flush().  If the explicit master put had already flushed, no flush
is armed to update the hardware master to the derived channel volume.  If the
explicit master put was still pending in the flush window,
snd_usb_mixer_flush_pending() runs at FLUSH_TOUCHED with both master_pending
and touched false, so the (touched || level != FLUSH_TOUCHED) check skips
deriving the master altogether and leaves hardware state stale and bit 0 of
master->cached cleared until a later stream open or resume.

[ ... ]

> @@ -1632,16 +1993,87 @@ static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol,
>  				return -EINVAL;
>  			val = get_abs_value(cval, val);
>  			if (oval != val) {

[ ... ]

>  				changed = 1;
>  			}
> +			/* any volume-channel write request - unchanged
> +			 * values included - claims master ownership (see
> +			 * below); read-only channels are not user-driven
> +			 */
> +			if (deferred && cval->control == UAC_FU_VOLUME &&
> +			    !mixer_ch_readonly(cval, c + 1))
> +				ch_seen = true;
>  			cnt++;
>  		}
> +		/* The last writer owns the master: a volume channel
> +		 * write request (the way audio servers drive this unit;
> +		 * alsactl restores channels before the master, so the
> +		 * master put would otherwise own every boot until the
> +		 * first changed gesture) re-claims master derivation.
> +		 * An unflushed explicit master value is dropped along
> +		 * with the ownership: the pending bit is cleared so the
> +		 * flush re-derives the master instead of writing the
> +		 * superseded value, and the cached bit follows it - or
> +		 * the raw-max skip in the flush would mistake the
> +		 * never-written value for device state and swallow
> +		 * every later derive.
> +		 */
> +		if (ch_seen) {
> +			struct usb_mixer_elem_list *ml;
> +
> +			for_each_mixer_elem(ml, cval->head.mixer,
> +					    cval->head.id) {
> +				struct usb_mixer_elem_info *m =
> +					mixer_elem_list_to_info(ml);
> +
> +				if (is_feature_volume_mute_ctl(m) &&
> +				    m->control == UAC_FU_VOLUME && !m->cmask) {
> +					m->master_explicit = false;
> +					clear_bit(0, m->pending);
> +					clear_bit(0, m->cached);
> +				}
> +			}
> +		}

[Severity: Medium]
Does clearing bit 0 of m->cached unconditionally on every per-channel volume
write break master reads and the raw-maximum re-assert skip when
m->master_explicit was already false?

Because clear_bit(0, m->cached) runs on every channel put (including no-op
writes that do not arm a flush, or before the 30 ms flush fires), any
user-space read of the master control via mixer_ctl_feature_get() ->
snd_usb_get_cur_mix_value() while bit 0 is cleared either fails with -ENXIO
when get_cur_broken is set or issues a USB GET_CUR readback when it is not:

sound/usb/mixer.c:snd_usb_get_cur_mix_value() {
	if (test_bit(channel, cval->cached)) {
		*value = cval->cache_val[index];
		return 0;
	}

	/* The current value is always provided by the cache after initialization. */
	if (cval->get_cur_broken)
		return -ENXIO;

	err = get_cur_mix_raw(cval, channel, value);
    ...
}

In addition, dropping bit 0 of master->cached when no explicit master
ownership transfer took place forces !test_bit(0, master->cached) to be true
in snd_usb_mixer_flush_pending(), bypassing the max_val != master->max check
and re-sending master->max on every channel burst (such as adjusting balance
on the quieter channel while the louder channel remains at master->max) or on
the next stream-open re-push after a no-op channel write.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117180728.117915-1-a@pavel.bz?part=2

  reply	other threads:[~2026-11-17 18:40 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 18:07 [RFC PATCH v6 0/2] ALSA: usb-audio: working hardware volume for XiiSound headsets (040b:0897) Pavel Artemev
2026-11-17 18:07 ` [PATCH 1/2] ALSA: usb-audio: drive XiiSound playback volume instead of skipping it Pavel Artemev
2026-11-17 18:07 ` [PATCH 2/2] ALSA: usb-audio: add MIXER_STATE_REPUSH quirk flag Pavel Artemev
2026-11-17 18:40   ` sashiko-bot [this message]
  -- strict thread matches above, loose matches on Subject: below --
2026-11-17 19:46 [RFC PATCH v7 0/2] ALSA: usb-audio: working hardware volume for XiiSound headsets (040b:0897) Pavel Artemev
2026-11-17 19:46 ` [PATCH 2/2] ALSA: usb-audio: add MIXER_STATE_REPUSH quirk flag Pavel Artemev
2026-11-17 20:13   ` sashiko-bot
2026-11-17 14:33 [RFC PATCH v5 0/2] ALSA: usb-audio: working hardware volume for XiiSound headsets (040b:0897) Pavel Artemev
2026-11-17 14:33 ` [PATCH 2/2] ALSA: usb-audio: add MIXER_STATE_REPUSH quirk flag Pavel Artemev
2026-11-17 15:04   ` sashiko-bot

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=sashiko-outbox-219376@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=a@pavel.bz \
    --cc=linux-sound@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.