* [RFC PATCH v5 0/2] ALSA: usb-audio: working hardware volume for XiiSound headsets (040b:0897)
@ 2026-11-17 14:33 Pavel Artemev
2026-11-17 14:33 ` [PATCH 1/2] ALSA: usb-audio: drive XiiSound playback volume instead of skipping it Pavel Artemev
2026-11-17 14:33 ` [PATCH 2/2] ALSA: usb-audio: add MIXER_STATE_REPUSH quirk flag Pavel Artemev
0 siblings, 2 replies; 8+ messages in thread
From: Pavel Artemev @ 2026-11-17 14:33 UTC (permalink / raw)
To: Takashi Iwai; +Cc: Jaroslav Kysela, linux-sound, linux-kernel
During a two-week campaign to make a Temple (XiiSound/Weltrend
040b:0897) wireless dongle audible I captured the complete control
traffic of the device on both Linux (usbmon) and the vendor's own
Windows 10 stack (USBPcap). This cover letter summarizes what the
wire says about this chipset, because it differs from the device model
encoded in the recently merged workaround, commit ba9bccdf711a
("ALSA: usb-audio: Skip broken playback feature unit on XiiSound
chips"), which is queued for 7.5.
To be clear up front: that commit fixes the user-visible bug (the
dongle coming up silent or one-eared), nothing here is a regression
report against it, and this RFC is not asking for its immediate
removal. It proposes the alternative I ended up running locally -
keep the playback feature unit exposed and make its hardware volume
work - and offers the captures as evidence, both for this design
discussion and for whoever debugs these devices next.
What the wire shows
===================
1. The playback feature unit is a working hardware volume. Per-
channel SET_CUR writes apply deterministically and independently
(a fast pair CN1<-50 + CN2<-100 applies exactly; a lone channel
write does not disturb its sibling), the master gates both ears
symmetrically, and intermediate values are audible and graded.
The vendor's Windows driver drives this exact unit on every slider
and hardware-button step, mapped linearly from its slider percent
(10% -> raw 9, 50% -> raw 49), and every intermediate value is
audible there too.
2. The "any volume below the maximum mutes one or both channels"
behaviour that motivated the skip is host-side, in three stacked
layers:
- the unit advertises a bogus 0.00..0.39 dB range, under which
every user-space volume at or below 0 dB - the whole normal
PipeWire range, 100% included - resolves to the raw minimum,
which does silence the channel;
- the stereo control's GET_CUR returns a constant value, so the
sticky-GET sanity check classifies it broken and the probe
writes the minimum to both channels at every mixer build;
- user space applies one logical volume change as fragmented
writes (one per channel, the first carrying the stale sibling
value), and the device silently drops control writes in the
first seconds after enumeration (measured: the window closes
6.5-9 s after enumeration, with a ragged tail), so a stale
channel can survive until some later write happens to refresh
it. The per-ear asymmetry users report is exactly this stale
state, not a channel-mapping property: under wire-verified
canonical writes the attenuation is symmetric (12/12 steps).
3. The Windows driver never reads back (GET_CUR is broken, so it
cannot), and does not need to: it keeps the whole channel picture
at a single source of truth, writes every channel of a control in
one short burst - master first - and re-pushes the complete state
at every device open. That every-open re-push is what repairs
whatever the enumeration window swallowed.
One consequence worth recording: the create-time pin writes of the
merged quirk sit inside the loss window and are dropped along with
every other early write. The dongle still comes up audible because
it powers up with every volume channel at the maximum - which is what
the pin intended to enforce anyway - and the resume re-pin added in
its v3 is the part that does real work. So the merged fix is sound
in effect; only its description of the hardware is not.
The RFC
=======
Patch 1 replaces the skip-and-pin with three quirk flags for
040b:0897: the new QUIRK_FLAG_MIXER_INIT_VOLUME_MAX (initialize the
channels of volume controls to the maximum instead of the minimum,
exactly where the broken-GET minimum writes happen) plus the existing
PLAYBACK/CAPTURE LINEAR_VOL flags (linear scale instead of the bogus
dB range). Hardware volume then behaves like on Windows.
Patch 2 adds QUIRK_FLAG_MIXER_STATE_REPUSH, which replicates the
vendor's write discipline for write-lossy devices: volume and mute
puts only record values, one deferred flush writes each touched
feature unit in a single canonical burst (master first, derived from
the loudest channel), and every PCM stream open and resume re-push
the full cached state - the every-open repair the Windows stack
relies on. This is new mechanism territory (I found no prior
proposal for write batching or re-push in the archives), hence RFC.
Both ran as locally built DKMS modules - first against a 7.2 tree over
the wire-verified campaign, then successive revisions against 7.3.4 -
and in daily use; details in the commit messages.
Known limitations, stated for the record:
- Through the linear scale PipeWire applies its cubic, so the
mid-slider feel is quieter than on Windows (50% -> raw ~12).
Users who want the vendor loudness feel can set
api.alsa.soft-mixer = true - which is effectively what the merged
skip approach gives everyone. The value of this RFC is that the
choice stays with the user and the hardware volume keeps working
for stacks that do drive it.
- After a replug the device sits at its power-up state (maximum)
until the first write lands past the loss window; with patch 2 a
stream open re-pushes and heals, provided streams actually open
and close (a wedged PipeWire session that holds the device open
indefinitely defers the heal to the first user volume gesture).
- Hardware buttons keep stepping an internal device state that the
host cannot observe (no readback); neither approach - skip or
drive - syncs with the buttons.
I am happy to rework or drop either patch; the capture evidence is
offered independently of the design preference.
Changes in v5
=============
Thanks to the sashiko AI review for the v4 pass; all three findings
are addressed:
- The derived master is re-asserted by the stream-open and resume
re-pushes again. The v4 skip-equal guard starved it: once the
cached master equaled the derived value, a master write dropped
mid-session or lost across a device reset was never re-sent,
while the channels were healed. The skip now survives only at
the control's raw maximum, where it is quiet and correct at the
same time: a limit-valued write triggers the device's in-headset
limit notification even when the value is unchanged, and every
device reset leaves the master at raw max anyway. A dropped
master write below the maximum is healed by the next re-push; at
the maximum it self-evidences as a missing arrival notification
and heals at the next gesture.
- A per-channel volume put that re-claims master derivation now
also drops a still-unflushed explicit master value (the
superseded value stayed pending and the flush wrote it over the
newer channels); and only volume puts re-claim - a mute put no
longer hands the master back to derivation.
- The deferred put callback checks the mixer's disconnected flag
under the mixer lock before arming the flush timer (a put racing
the disconnect mark could re-arm after the cancels and run on
freed memory; free() cancels the work before the controls are
unregistered).
Wire-verified on the device against 7.3.4: an explicit master
superseded by a channel put inside one flush window produces the
derived burst only (the superseded value never reaches the wire); a
stream-open re-push at a non-limit volume re-asserts the master
(v4 skipped it); parked at the raw maximum the re-push keeps the
master absent; the runtime-PM paths are unchanged.
Not adopted: the straightforward reading of the first finding -
always re-assert the master - would fire the limit notification at
every stream open while the user sits at 100%, with no user action
nearby; the limit-aware re-assert keeps the healing without that
regression. Mute-put ownership and the disconnect guard have no
wire-observable path on this device (it exposes no mute kctls, and
the race window is milliseconds wide) and are verified by
inspection.
Documentation/sound/alsa-configuration.rst | 9 +
sound/usb/mixer.c | 552 ++++++++++++++++++++++++++++-
sound/usb/mixer.h | 19 +
sound/usb/mixer_maps.c | 19 -
sound/usb/mixer_quirks.c | 59 ---
sound/usb/pcm.c | 4 +
sound/usb/quirks.c | 7 +
sound/usb/usbaudio.h | 29 ++
8 files changed, 604 insertions(+), 94 deletions(-)
base-commit: f930156051f6a3dcd454ff723ca36b14d49650d2
--
2.56.1
^ permalink raw reply [flat|nested] 8+ messages in thread* [PATCH 1/2] ALSA: usb-audio: drive XiiSound playback volume instead of skipping it 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 ` Pavel Artemev 2026-11-17 14:33 ` [PATCH 2/2] ALSA: usb-audio: add MIXER_STATE_REPUSH quirk flag Pavel Artemev 1 sibling, 0 replies; 8+ messages in thread From: Pavel Artemev @ 2026-11-17 14:33 UTC (permalink / raw) To: Takashi Iwai; +Cc: Jaroslav Kysela, linux-sound, linux-kernel Replace the XiiSound (040b:0897) playback feature unit workaround from commit ba9bccdf711a ("ALSA: usb-audio: Skip broken playback feature unit on XiiSound chips") with quirk flags that keep the unit exposed and make its hardware volume work. Wire-level captures (usbmon on Linux, USBPcap on the vendor's own Windows 10 stack) show the playback feature unit is functional as a hardware volume: per-channel SET_CUR writes apply deterministically and independently (a fast pair CN1<-50 + CN2<-100 applies exactly; a lone channel write does not disturb its sibling), the master channel gates both ears symmetrically, and intermediate values are audible and graded. The vendor's Windows driver drives exactly this unit on every slider and hardware-button step, writing all three channels with plain 0..100 raw values mapped linearly from its slider. The "any volume below the maximum mutes one or both channels" behaviour that motivated the skip is host-side, not firmware: * the unit advertises a bogus 0.00..0.39 dB range, under which every user-space volume at or below 0 dB - the whole normal PipeWire range, 100% included - resolves to the raw minimum, which silences the channel; * the stereo volume control's GET_CUR returns a constant value, so the sanity check classifies it as broken and init_cur_mix_raw() writes the minimum to both channels at every mixer build; * user space applies one logical volume change as fragmented writes (one per channel, the first carrying the stale sibling value), and these devices silently drop control writes in the first seconds after enumeration, so a channel can be left behind at a stale value that no later write corrects. Enable three quirk flags for 040b:0897. The new QUIRK_FLAG_MIXER_INIT_VOLUME_MAX makes init_cur_mix_raw() initialize the channels of volume controls to the maximum instead of the minimum: it runs exactly where the broken-GET minimum writes would, supersedes them (also when range discovery is deferred with -EAGAIN and initialization repeats later from mixer_ctl_feature_info()), and touches nothing else - controls whose GET_CUR works are read, not written, and switches keep the safe unmuted minimum. The existing MIXER_PLAYBACK_LINEAR_VOL and MIXER_CAPTURE_LINEAR_VOL replace the bogus dB scale with a linear one, so user-space volumes map onto the full raw range. The controls stay writable; hardware volume then behaves like on Windows. The create-time pin writes of the previous quirk sit inside that post-enumeration write-loss window and are dropped along with every other early write; the device comes up audible after a plug only because it powers up with every volume channel at the maximum, which the explicit init reproduces if a write ever does land. No separate resume re-pin is needed: with the controls created, the standard mixer resume path restores the cached values. Testing: the same flag combination ran on the device as a locally built DKMS module against a 7.2 tree through a wire-level verification campaign: the initialization writes were observed on the bus at every mixer build, the full raw range became reachable from PipeWire (route volumes 0/13/100 for 0.0/0.5/1.00), and mute and balance track. This exact tree also ran on the device as a DKMS module against 7.3.4: the broken-GET stereo control is initialized to the maximum in place of the minimum, the other controls are read but not written, and the linear raw mapping holds (route volume restores landed as 13/100/35 on the bus for 0.5/1.0/0.7). Closes: https://github.com/alsa-project/alsa-ucm-conf/issues/760 Closes: https://github.com/linuxmint/linuxmint/issues/822 Link: https://bugzilla.kernel.org/show_bug.cgi?id=221401 Link: https://lore.kernel.org/linux-sound/20261116153521.188570-2-a@pavel.bz Assisted-by: LLM Signed-off-by: Pavel Artemev <a@pavel.bz> --- Documentation/sound/alsa-configuration.rst | 4 ++ sound/usb/mixer.c | 13 ++++- sound/usb/mixer_maps.c | 19 ------- sound/usb/mixer_quirks.c | 59 ---------------------- sound/usb/quirks.c | 5 ++ sound/usb/usbaudio.h | 11 ++++ 6 files changed, 32 insertions(+), 79 deletions(-) diff --git a/Documentation/sound/alsa-configuration.rst b/Documentation/sound/alsa-configuration.rst index 689eccf6a..ea7f5aa31 100644 --- a/Documentation/sound/alsa-configuration.rst +++ b/Documentation/sound/alsa-configuration.rst @@ -2413,6 +2413,10 @@ quirk_flags versa (same for 88200 <-> 96000 and 176400 <-> 192000). The driver sends the partner rate in SET_CUR so the device ends up running at the requested one. + * bit 34: ``mixer_init_volume_max`` + Initialize the volume control channels to the maximum value instead + of the minimum at mixer creation. For devices that silence their + audio path while a volume channel sits at the minimum value. This module supports multiple devices, autoprobe and hotplugging. diff --git a/sound/usb/mixer.c b/sound/usb/mixer.c index 95209ec00..bf1d9fbf0 100644 --- a/sound/usb/mixer.c +++ b/sound/usb/mixer.c @@ -1310,7 +1310,18 @@ static void init_cur_mix_raw(struct usb_mixer_elem_info *cval, int ch, int idx) "%d:%d: failed to get current value for ch %d (%d)\n", cval->head.id, mixer_ctrl_intf(cval->head.mixer), ch, err); - snd_usb_set_cur_mix_value(cval, ch, idx, cval->min); + /* + * QUIRK_FLAG_MIXER_INIT_VOLUME_MAX: some devices silence an audio + * path while a volume channel sits at the minimum value, so + * initialize those to the maximum instead. Only volume controls: + * for switches the minimum is the safe (unmuted) value. + */ + if ((cval->head.mixer->chip->quirk_flags & + QUIRK_FLAG_MIXER_INIT_VOLUME_MAX) && + cval->control == UAC_FU_VOLUME) + snd_usb_set_cur_mix_value(cval, ch, idx, cval->max); + else + snd_usb_set_cur_mix_value(cval, ch, idx, cval->min); } /* diff --git a/sound/usb/mixer_maps.c b/sound/usb/mixer_maps.c index 9218ca631..98b414621 100644 --- a/sound/usb/mixer_maps.c +++ b/sound/usb/mixer_maps.c @@ -374,20 +374,6 @@ static const struct usbmix_name_map dell_alc4020_map[] = { { 0 } }; -/* - * Weltrend XiiSound chips (040b:0897, e.g. Redragon H878): - * the playback feature unit is broken; its volume controls act like - * mute switches below the maximum value and the channel mapping is - * unreliable, so any hardware volume change mutes one or both - * channels. Skip the unit entirely and let userspace fall back to - * software volume; the unit is pinned to maximum/unmuted by the - * mixer quirk in mixer_quirks.c. - */ -static const struct usbmix_name_map xiisound_map[] = { - { 5, NULL }, /* playback feature unit */ - { 0 } -}; - /* * Corsair Virtuoso calls everything "Headset" without this, leading to * applications moving the sidetone control instead of the main one. @@ -804,11 +790,6 @@ static const struct usbmix_ctl_map usbmix_ctl_maps[] = { .id = USB_ID(0x1395, 0x0025), .map = sennheiser_pc8_map, }, - { - /* Weltrend XiiSound (Redragon H878) */ - .id = USB_ID(0x040b, 0x0897), - .map = xiisound_map, - }, { /* Microsoft USB Link headset */ .id = USB_ID(0x045e, 0x083c), diff --git a/sound/usb/mixer_quirks.c b/sound/usb/mixer_quirks.c index 4c7d68311..583971944 100644 --- a/sound/usb/mixer_quirks.c +++ b/sound/usb/mixer_quirks.c @@ -4603,59 +4603,6 @@ static int snd_audiofuse_controls_create(struct usb_mixer_interface *mixer) return err; } -/* - * Weltrend XiiSound chips (040b:0897, e.g. Redragon H878): - * the playback feature unit (id 5) is broken; its volume controls act like - * mute switches below the maximum value and the channel mapping is - * unreliable, so the unit is hidden from userspace entirely (see the - * quirk in mixer_maps.c) and userspace falls back to software volume. - * Pin the unit's master and channel volumes to the reported maximum - * and unmute them, so the audio path stays fully open. - */ -static int snd_xiisound_init_fu_pins(struct usb_mixer_interface *mixer) -{ - struct snd_usb_audio *chip = mixer->chip; - unsigned char buf[2]; - int ifnum, ch, err; - - if (mixer->protocol != UAC_VERSION_1) - return 0; - - ifnum = snd_usb_ctrl_intf(chip->ctrl_intf); - - /* unit 5 is the playback feature unit on these chips */ - for (ch = 0; ch <= 2; ch++) { - err = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), - UAC_GET_MAX, - USB_DIR_IN | USB_TYPE_CLASS | - USB_RECIP_INTERFACE, - (UAC_FU_VOLUME << 8) | ch, - (5 << 8) | ifnum, buf, 2); - if (err < 2) - return 0; /* no such unit/channels, nothing to pin */ - - err = snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0), - UAC_SET_CUR, - USB_DIR_OUT | USB_TYPE_CLASS | - USB_RECIP_INTERFACE, - (UAC_FU_VOLUME << 8) | ch, - (5 << 8) | ifnum, buf, 2); - if (err < 0) - return err; - - buf[0] = 0; /* unmute */ - err = snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0), - UAC_SET_CUR, - USB_DIR_OUT | USB_TYPE_CLASS | - USB_RECIP_INTERFACE, - (UAC_FU_MUTE << 8) | ch, - (5 << 8) | ifnum, buf, 1); - if (err < 0) - return err; - } - return 0; -} - int snd_usb_mixer_apply_create_quirk(struct usb_mixer_interface *mixer) { int err = 0; @@ -4669,9 +4616,6 @@ int snd_usb_mixer_apply_create_quirk(struct usb_mixer_interface *mixer) case USB_ID(0x0644, 0x8047): err = snd_us16x08_controls_create(mixer); break; - case USB_ID(0x040b, 0x0897): - err = snd_xiisound_init_fu_pins(mixer); - break; case USB_ID(0x041e, 0x3020): case USB_ID(0x041e, 0x3040): case USB_ID(0x041e, 0x3042): @@ -4863,9 +4807,6 @@ void snd_usb_mixer_resume_quirk(struct usb_mixer_interface *mixer) case USB_ID(0x0bda, 0x4014): /* Dell WD15 dock */ dell_dock_mixer_init(mixer); break; - case USB_ID(0x040b, 0x0897): /* Weltrend XiiSound (Redragon H878) */ - snd_xiisound_init_fu_pins(mixer); - break; } } diff --git a/sound/usb/quirks.c b/sound/usb/quirks.c index a4eb0d0cd..6ef6d4410 100644 --- a/sound/usb/quirks.c +++ b/sound/usb/quirks.c @@ -2502,6 +2502,10 @@ static const struct usb_audio_quirk_flags_table quirk_flags_table[] = { QUIRK_FLAG_FIXED_RATE), DEVICE_FLG(0x03f0, 0x654a, /* HP 320 FHD Webcam */ QUIRK_FLAG_GET_SAMPLE_RATE | QUIRK_FLAG_MIC_RES_16), + DEVICE_FLG(0x040b, 0x0897, /* XiiSound/Weltrend Temple and rebranded headsets */ + QUIRK_FLAG_MIXER_INIT_VOLUME_MAX | + QUIRK_FLAG_MIXER_PLAYBACK_LINEAR_VOL | + QUIRK_FLAG_MIXER_CAPTURE_LINEAR_VOL), DEVICE_FLG(0x041e, 0x3000, /* Creative SB Extigy */ QUIRK_FLAG_IGNORE_CTL_ERROR), DEVICE_FLG(0x041e, 0x324d, /* Creative Sound Blaster Play! 3 */ @@ -2979,6 +2983,7 @@ static const char *const snd_usb_audio_quirk_flag_names[] = { QUIRK_STRING_ENTRY(PLAYBACK_URB_FIXUP), QUIRK_STRING_ENTRY(ALWAYS_SET_RATE), QUIRK_STRING_ENTRY(SWAP_RATES), + QUIRK_STRING_ENTRY(MIXER_INIT_VOLUME_MAX), NULL }; diff --git a/sound/usb/usbaudio.h b/sound/usb/usbaudio.h index c6d59218d..61794fdc4 100644 --- a/sound/usb/usbaudio.h +++ b/sound/usb/usbaudio.h @@ -262,6 +262,15 @@ extern bool snd_usb_skip_validation; * The device applies a requested sample rate to the other base-rate family * (44100 <-> 48000, 88200 <-> 96000, 176400 <-> 192000). Send the partner * rate in SET_CUR so that the device runs at the requested rate. + * QUIRK_FLAG_MIXER_INIT_VOLUME_MAX + * A Feature Unit volume channel left at the minimum value silences its + * audio path on some devices; user space that does not write all + * channels of such a control would leave the device silent. Where + * GET_CUR is broken the kernel additionally discards the test values + * and initializes the channels to the minimum at every mixer build. + * Initialize the volume control channels to the maximum instead of + * the minimum; the controls remain writable and the hardware volume + * operates normally afterwards. */ enum { @@ -299,6 +308,7 @@ enum { QUIRK_TYPE_PLAYBACK_URB_FIXUP = 31, QUIRK_TYPE_ALWAYS_SET_RATE = 32, QUIRK_TYPE_SWAP_RATES = 33, + QUIRK_TYPE_MIXER_INIT_VOLUME_MAX = 34, /* Please also edit snd_usb_audio_quirk_flag_names and alsa-configuration.rst */ }; @@ -338,5 +348,6 @@ enum { #define QUIRK_FLAG_PLAYBACK_URB_FIXUP QUIRK_FLAG(PLAYBACK_URB_FIXUP) #define QUIRK_FLAG_ALWAYS_SET_RATE QUIRK_FLAG(ALWAYS_SET_RATE) #define QUIRK_FLAG_SWAP_RATES QUIRK_FLAG(SWAP_RATES) +#define QUIRK_FLAG_MIXER_INIT_VOLUME_MAX QUIRK_FLAG(MIXER_INIT_VOLUME_MAX) #endif /* __USBAUDIO_H */ base-commit: f930156051f6a3dcd454ff723ca36b14d49650d2 -- 2.56.1 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH 2/2] ALSA: usb-audio: add MIXER_STATE_REPUSH quirk flag 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 1/2] ALSA: usb-audio: drive XiiSound playback volume instead of skipping it Pavel Artemev @ 2026-11-17 14:33 ` Pavel Artemev 2026-11-17 15:04 ` sashiko-bot 1 sibling, 1 reply; 8+ messages in thread From: Pavel Artemev @ 2026-11-17 14:33 UTC (permalink / raw) To: Takashi Iwai; +Cc: Jaroslav Kysela, linux-sound, linux-kernel Some USB audio devices silently drop Feature Unit control writes in the first seconds after USB enumeration, and occasionally drop individual writes at any later time as well. When GET_CUR on such a device is broken there is no way to detect the divergence by reading back, so the state the host believes in silently drifts from the hardware state: a channel left at a stale value is never corrected. The Windows driver of one such chipset (Weltrend/XiiSound, 040b:0897) handles this by never reading back at all. USBPcap captures of it show a single source of truth for the whole channel picture, every channel of a control written in one short burst - master first, then the channels - and the complete state re-pushed at every device open. That every-open re-push is the load-bearing mechanism: it repairs whatever writes were swallowed around enumeration. Add QUIRK_FLAG_MIXER_STATE_REPUSH, which replicates that write discipline in the driver: * volume and mute puts only record values; the cache is defined as "what user space last asked for" and is never read back from the device, and never invalidated by device status notifications; * a single deferred flush (30 ms quiescence) writes each touched feature unit in one burst, master first, derived as the loudest channel - never asserted from its own possibly stale cache. The last writer owns the master: an explicit master put stops derivation for that unit until user space writes one of its volume channels again, dropping any unflushed explicit value with the ownership, so a value saved and restored by alsactl does not pin the volume forever; * a PCM stream open and a resume re-push all cached volume values through the same timer - the every-open repair the Windows stack relies on. A resume re-push also re-asserts cached mute switches, taking over the job restore_mixer_value() usually does at resume: restore runs without the mixer lock, so writing the deferred controls there could clobber values a concurrent deferred put just recorded, and would re-send a derived master from a cache that lags its channels. The flush runs under the mixer lock, like the write path of the put callback it replaces, so a put can never interleave with a burst. Re-push requests are lock-free atomic bits, because neither PM callback may block on that lock: a control write inside a flush can trigger the runtime resume whose callback then runs on the very thread holding it. Suspend marks the mixer and cancels the flush timer without waiting - a flush in flight may be blocked in runtime-PM resume, waiting for the suspend callback to finish, so waiting for it there would deadlock the PM worker against the flush worker - and a flush already running stops at the next unit, with writes that failed mid-suspend kept pending for retry. Resume requests a re-push and arms the worker without taking the lock; the worker re-checks the request bits after every run, so a request raised while it runs is never lost, and the disarm ordering against disconnect (disconnected flag under the mixer lock before the synchronizing cancel, and checked under the same lock by the deferred put callback before it arms the timer, because free() cancels the work before the controls are unregistered) keeps the teardown race-free. Only controls written through mixer_ctl_feature_put() with a volume or mute selector are deferred. Mixer unit controls share that put callback but store an input channel number in the selector field and are excluded (num_outputs is only ever set for mixer units); other feature unit controls (AGC, tone) keep the direct write path, as do the caches of non-deferred controls on notification; and read-only channels are neither written nor cached, matching the direct write path. The master is re-asserted on every flush and re-push, including unchanged values, except when it sits at the control's raw maximum: that re-send would only trigger the in-headset limit notification (nothing moved), while power-up leaves the master at raw max anyway, so the skip is quiet and correct at once. A sporadically dropped master write below raw max is therefore healed by the next re-push; at raw max it self-evidences as a missing arrival notification and heals at the next gesture. The vendor driver has no readback either. Mute switches are only written when user space changed them, or at a resume re-push: a lost re-assert can at worst fail to mute (power-up is unmuted), and mute writes during playback audibly chop the stream on these devices. Volume writes are delayed by the quiescence window, which is not perceptible for a volume control. The flag is enabled for 040b:0897, whose loss window was measured on the wire at 6.5-9 s after enumeration with a ragged tail (beep-ACKed limit-write probes at increasing delays). Testing: ran on the device as a locally built DKMS module (7.2 tree, daily use over 10 days) and wire-verified: 21 volume gestures produced 21 bursts of exactly one write per channel in the master-first order, no readbacks, no re-sends; endpoint limit notifications fire only on user arrivals at a limit; no stream chops during playback; replug, suspend/resume, balance and mute pass end to end. The same mechanism also ran on the device as a DKMS module against 7.3.4, wire-verified: volume gestures each produced exactly one burst of one write per channel in the master-first order, with no readbacks and no writes to the capture unit. The changes over the v3 revision were also wire-verified against 7.3.4: a deferred put issued while the device is runtime-suspended produces no bus traffic and its value is delivered by the resume re-push, which re-asserts the cached volume and mute controls; a stream-open re-push re-sends volumes only; an explicit master put is followed again by derivation once user space writes a volume channel of the unit; and the derived master equal to the cached value is not re-sent in that tree (no endpoint notification without a user arrival). The changes over the v4 revision are also wire-verified against 7.3.4: an explicit master put superseded by a channel put in the same flush window produces one derived burst, and the superseded explicit value never reaches the wire; a stream-open re-push at a non-limit volume re-asserts the derived master (present on the wire, where v4 skipped it); parked at the raw maximum the re-push keeps the master absent; the runtime-PM paths (zero bus traffic for a put on a suspended device, resume re-push delivering the pending gesture, mutes and mic) behave as in v4. Closes: https://github.com/alsa-project/alsa-ucm-conf/issues/760 Link: https://bugzilla.kernel.org/show_bug.cgi?id=221401 Link: https://lore.kernel.org/linux-sound/20261117124242.12237-3-a@pavel.bz Assisted-by: LLM Signed-off-by: Pavel Artemev <a@pavel.bz> --- Documentation/sound/alsa-configuration.rst | 5 + sound/usb/mixer.c | 539 ++++++++++++++++++++- sound/usb/mixer.h | 19 + sound/usb/pcm.c | 4 + sound/usb/quirks.c | 2 + sound/usb/usbaudio.h | 18 + 6 files changed, 572 insertions(+), 15 deletions(-) diff --git a/Documentation/sound/alsa-configuration.rst b/Documentation/sound/alsa-configuration.rst index ea7f5aa31..9b4b13969 100644 --- a/Documentation/sound/alsa-configuration.rst +++ b/Documentation/sound/alsa-configuration.rst @@ -2417,6 +2417,11 @@ quirk_flags Initialize the volume control channels to the maximum value instead of the minimum at mixer creation. For devices that silence their audio path while a volume channel sits at the minimum value. + * bit 35: ``mixer_state_repush`` + Defer volume and mute writes into one burst per feature unit, and + re-push the complete cached mixer state at every PCM stream open + and after resume. For devices that silently drop control writes + around enumeration and whose controls cannot be read back. This module supports multiple devices, autoprobe and hotplugging. diff --git a/sound/usb/mixer.c b/sound/usb/mixer.c index bf1d9fbf0..03d6fa3ab 100644 --- a/sound/usb/mixer.c +++ b/sound/usb/mixer.c @@ -37,6 +37,7 @@ #include <linux/usb/audio.h> #include <linux/usb/audio-v2.h> #include <linux/usb/audio-v3.h> +#include <linux/workqueue.h> #include <sound/core.h> #include <sound/control.h> @@ -584,13 +585,22 @@ static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, return snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, validx, value); } +/* read-only indication of one channel of a control (channel 0 = master); + * read-only channels are not written and not cached + */ +static unsigned int mixer_ch_readonly(const struct usb_mixer_elem_info *cval, + int channel) +{ + if (channel == 0) + return cval->master_readonly; + return cval->ch_readonly & BIT(channel - 1); +} + int snd_usb_set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel, int index, int value) { int err; - unsigned int read_only = (channel == 0) ? - cval->master_readonly : - cval->ch_readonly & BIT(channel - 1); + unsigned int read_only = mixer_ch_readonly(cval, channel); if (read_only) { usb_audio_dbg(cval->head.mixer->chip, @@ -1608,18 +1618,369 @@ static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, return 0; } +/* + * Atomic mixer write bursts (QUIRK_FLAG_MIXER_STATE_REPUSH). + * + * Some devices silently drop mixer writes issued in the first seconds + * after USB enumeration, and can drop individual writes at any later + * time as well; GET_CUR is broken on them, so the divergence cannot + * be detected by reading back. The vendor's Windows driver never + * reads either: it collects the whole channel picture at a single + * source of truth and writes every channel of a control in one short + * burst (master first), re-pushing the complete state at every device + * open. + * + * User space applies one logical volume change as multiple fragmented + * control writes (one per channel, the first carrying stale siblings), + * so the stock write-per-put path cannot produce that burst. Puts + * therefore only record values - the cache is defined as "what user + * space last asked for" and is never read back from the device - and + * a single flush timer coalesces the fragments. When it fires, each + * touched feature unit is written in the Windows order: the master, + * derived as the loudest channel, first, then the changed channels, + * one write each. A stream open requests a full-state re-push + * (healing writes lost in the post-enumeration window), merged into + * the same timer so a burst never observes a half-applied gesture + * (user space opens streams mid-gesture for UI feedback sounds). + * + * The flush runs under the mixer lock, like the write path of the put + * callback it replaces: snd_usb_set_cur_mix_value() completes its USB + * transfer before updating the cache, so an unlocked flush could + * clobber values recorded concurrently by a put. The PM callbacks + * must not block on that lock, though: a control write inside a flush + * can trigger the runtime resume whose callback then runs on the very + * thread holding it. Re-push requests are therefore lock-free atomic + * bits; whoever cannot take the lock (the resume callback) only sets + * them and arms the worker, and the worker re-checks the bits after + * every run so no request is lost. + */ + +/* quiescence window for the flush; user space applies a stereo change + * as two control writes 5..20 ms apart + */ +#define MIXER_FLUSH_DELAY msecs_to_jiffies(30) + +/* mixer->flush_flags bits */ +#define MIXER_FLUSH_FULL 0 /* re-push all cached volume values */ +#define MIXER_FLUSH_RESUME 1 /* resume: also re-assert cached mutes */ +#define MIXER_FLUSH_SUSPENDED 2 /* suspend in flight: stop writing */ + +/* flush levels latched from the flags by the worker */ +enum { + FLUSH_TOUCHED, /* only channels user space changed */ + FLUSH_FULL_STATE, /* plus every cached volume value */ + FLUSH_RESUME_STATE, /* plus every cached mute switch */ +}; + +static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol); + +static bool is_feature_volume_mute_ctl(struct usb_mixer_elem_info *cval) +{ + if (!cval->head.kctl || !cval->head.kctl->put) + return false; + /* the info callback is shared with processing unit controls, whose + * put writes through a different path and does not manage the + * shared cache; the put callback is unique to feature unit and + * mixer unit controls, which the cache does cover + */ + if (cval->head.kctl->put != mixer_ctl_feature_put) + return false; + /* mixer unit controls share that put callback but store the + * 1-based input channel in cval->control, not a UAC_FU_* selector; + * num_outputs is only ever set for mixer units, so it tells the + * two apart + */ + if (cval->num_outputs) + return false; + return cval->control == UAC_FU_VOLUME || cval->control == UAC_FU_MUTE; +} + +/* Re-send every cached channel of one control, unchanged values + * included + */ +static void repush_ctl_cached(struct usb_mixer_elem_info *cval) +{ + int c, err, idx = 0; + + if (cval->cmask) { + for (c = 0; c < MAX_CHANNELS; c++) { + if (!(cval->cmask & BIT_ULL(c))) + continue; + if (test_bit(c + 1, cval->cached)) { + err = snd_usb_set_cur_mix_value(cval, c + 1, + idx, cval->cache_val[idx]); + if (err < 0) + usb_audio_dbg(cval->head.mixer->chip, + "repush ctl %d ch %d failed (%d)\n", + cval->control, c + 1, err); + } + idx++; + } + } else { + /* master channel */ + if (test_bit(0, cval->cached)) { + err = snd_usb_set_cur_mix_value(cval, 0, 0, + *cval->cache_val); + if (err < 0) + usb_audio_dbg(cval->head.mixer->chip, + "repush ctl %d ch 0 failed (%d)\n", + cval->control, err); + } + } +} + +/* Write one control's pending (changed-since-last-flush) channels. + * Pending bits are only set together with cache_val by a put, so the + * value is written unconditionally; the cache bit may have been + * cleared under us, and snd_usb_set_cur_mix_value() sets it again. + */ +static void flush_ctl_pending(struct usb_mixer_elem_info *cval) +{ + int c, err, idx = 0; + + for (c = 0; c < MAX_CHANNELS; c++) { + if (!(cval->cmask & BIT_ULL(c))) + continue; + if (test_bit(c + 1, cval->pending)) { + clear_bit(c + 1, cval->pending); + err = snd_usb_set_cur_mix_value(cval, c + 1, + idx, cval->cache_val[idx]); + if (err < 0) { + /* keep the channel pending so the next + * flush (stream open, resume) retries it + */ + set_bit(c + 1, cval->pending); + usb_audio_dbg(cval->head.mixer->chip, + "flush ctl %d ch %d failed (%d)\n", + cval->control, c + 1, err); + } + } + idx++; + } + if (!cval->cmask && test_bit(0, cval->pending)) { + /* master channel */ + clear_bit(0, cval->pending); + err = snd_usb_set_cur_mix_value(cval, 0, 0, + *cval->cache_val); + if (err < 0) { + set_bit(0, cval->pending); + usb_audio_dbg(cval->head.mixer->chip, + "flush ctl %d ch 0 failed (%d)\n", + cval->control, err); + } + } +} + +/* + * One atomic write burst per touched feature unit, Windows-style. + * level FLUSH_FULL_STATE re-pushes every cached volume value (the + * stream-open heal); FLUSH_RESUME_STATE additionally re-asserts + * cached mute switches (restore_mixer_value() leaves them to the + * flush under this quirk, and nothing is playing yet at resume, so + * the write cannot chop a stream). Otherwise only the channels user + * space changed since the last flush are written. The master is + * written first and derived as the loudest cached channel - never + * asserted from its own (possibly stale) cache. An explicit master + * put owns the master until user space writes a volume channel of + * the unit again. + * The master is re-asserted whenever the derived value differs from + * the cached one, and unchanged as well unless it sits at the + * control's raw maximum: re-sending an unchanged raw-max value makes + * these devices emit their in-headset limit notification with no + * user action nearby, while power-up leaves the master at raw max + * anyway, so the skip is quiet and correct at the same time. A + * sporadically dropped master write is thus healed by the next + * re-push unless the value is raw max, where it self-evidences as a + * missing arrival notification and heals at the next gesture. + * Mute switches are only written when user space changed them (or at + * resume re-push): a lost re-assert can only fail to mute (power-up + * is unmuted), and mute writes during playback audibly chop the + * stream on these devices. + * + * Called with mixer->lock held. + */ +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; + + lockdep_assert_held(&mixer->lock); + + if (mixer->disconnected) + return; + + for (id = 0; id < MAX_ID_ELEMS; id++) { + if (test_bit(MIXER_FLUSH_SUSPENDED, &mixer->flush_flags)) { + /* A suspend landed mid-run: stop writing (the + * writes would race the port suspend, and a + * failed one only stays pending anyway) and + * request the strongest re-push, which the + * resume path delivers. + */ + set_bit(MIXER_FLUSH_RESUME, &mixer->flush_flags); + return; + } + master = NULL; + max_val = 0; + have_ch = false; + touched = false; + master_pending = false; + 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; + idx = 0; + for (c = 0; c < MAX_CHANNELS; c++) { + if (!(cval->cmask & BIT_ULL(c))) + continue; + if (test_bit(c + 1, cval->cached)) { + /* seed from the first cached value: + * raw volume ranges can be signed + * and all-negative + */ + if (!have_ch || + cval->cache_val[idx] > max_val) + max_val = cval->cache_val[idx]; + have_ch = true; + } + idx++; + } + } + /* 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); + if (err < 0) + usb_audio_dbg(mixer->chip, + "derive ctl %d ch 0 failed (%d)\n", + master->control, err); + } + if (master && level != FLUSH_TOUCHED && !master_pending && + (master->master_explicit || !have_ch) && + !(test_bit(0, master->cached) && + *master->cache_val == master->max)) + /* user-owned master, or a unit without per-channel + * data: re-assert the cached master value, except + * the unchanged raw maximum (same notification / + * reset-at-max reasoning as the derive above) + */ + repush_ctl_cached(master); + for_each_mixer_elem(list, mixer, id) { + cval = mixer_elem_list_to_info(list); + if (!is_feature_volume_mute_ctl(cval)) + continue; + if (cval->control == UAC_FU_VOLUME && cval->cmask && + level != FLUSH_TOUCHED) { + bitmap_zero(cval->pending, MAX_CHANNELS + 1); + repush_ctl_cached(cval); + continue; + } + if (cval->control == UAC_FU_MUTE && + level == FLUSH_RESUME_STATE) { + bitmap_zero(cval->pending, MAX_CHANNELS + 1); + repush_ctl_cached(cval); + continue; + } + if (!bitmap_empty(cval->pending, MAX_CHANNELS + 1)) + flush_ctl_pending(cval); + } + } +} + +static void snd_usb_mixer_flush_work(struct work_struct *work) +{ + struct usb_mixer_interface *mixer = + container_of(work, struct usb_mixer_interface, + flush_work.work); + int level; + + scoped_guard(mutex, &mixer->lock) { + level = FLUSH_TOUCHED; + if (test_and_clear_bit(MIXER_FLUSH_FULL, + &mixer->flush_flags)) + level = FLUSH_FULL_STATE; + if (test_and_clear_bit(MIXER_FLUSH_RESUME, + &mixer->flush_flags)) + level = FLUSH_RESUME_STATE; + if (mixer->disconnected) + return; + if (level != FLUSH_TOUCHED) + usb_audio_dbg(mixer->chip, + "re-pushing mixer state (level %d)\n", + level); + snd_usb_mixer_flush_pending(mixer, level); + } + /* A request may have arrived while we ran - from a stream open, + * or from the resume callback, which cannot take this lock (see + * the comment in snd_usb_mixer_resume()). Re-arm so it gets + * its own run; arming here is safe against teardown because the + * disconnect path cancels this work only after this function + * has returned. While suspended, the resume callback does the + * arming: re-arming here would only loop on failing writes. + */ + if (!test_bit(MIXER_FLUSH_SUSPENDED, &mixer->flush_flags) && + (test_bit(MIXER_FLUSH_FULL, &mixer->flush_flags) || + test_bit(MIXER_FLUSH_RESUME, &mixer->flush_flags))) + mod_delayed_work(system_wq, &mixer->flush_work, + MIXER_FLUSH_DELAY); +} + +/* Arm the deferred flush - unless the mixer is being torn down: + * free() cancels the work before the controls are unregistered, so + * a put must never arm the timer after the disconnect mark (which + * disconnect sets under the mixer lock the put already holds). + */ +static void mixer_arm_flush(struct usb_mixer_interface *mixer) +{ + if (!mixer->disconnected) + mod_delayed_work(system_wq, &mixer->flush_work, + MIXER_FLUSH_DELAY); +} + /* put the current value to feature/mixer unit */ static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct usb_mixer_elem_info *cval = snd_kcontrol_chip(kcontrol); + bool deferred; int max_val; int c, cnt, val, oval, err; int changed = 0; + deferred = (cval->head.mixer->chip->quirk_flags & + QUIRK_FLAG_MIXER_STATE_REPUSH) && + is_feature_volume_mute_ctl(cval); + guard(mutex)(&cval->head.mixer->lock); max_val = get_max_exposed(cval); if (cval->cmask) { + bool ch_put = false; + cnt = 0; for (c = 0; c < MAX_CHANNELS; c++) { if (!(cval->cmask & BIT(c))) @@ -1632,14 +1993,54 @@ static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, return -EINVAL; val = get_abs_value(cval, val); if (oval != val) { - err = snd_usb_set_cur_mix_value(cval, c + 1, - cnt, val); - if (err < 0) - return filter_error(cval, err); + if (deferred) { + /* record only; one flush writes the + * whole unit in a single burst. + * Read-only channels keep the + * direct-path behaviour: no write, + * no cache update. + */ + if (!mixer_ch_readonly(cval, c + 1)) { + set_bit(c + 1, cval->cached); + cval->cache_val[cnt] = val; + set_bit(c + 1, cval->pending); + mixer_arm_flush(cval->head.mixer); + ch_put = true; + } + } else { + err = snd_usb_set_cur_mix_value(cval, c + 1, + cnt, val); + if (err < 0) + return filter_error(cval, err); + } changed = 1; } cnt++; } + /* The last writer owns the master: a volume channel + * write (the way audio servers drive this unit) + * re-claims master derivation, so a master saved and + * restored by e.g. alsactl does not pin the volume + * forever. 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. + */ + if (ch_put && cval->control == UAC_FU_VOLUME) { + 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); + } + } + } } else { /* master channel */ err = snd_usb_get_cur_mix_value(cval, 0, 0, &oval); @@ -1650,9 +2051,24 @@ static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, return -EINVAL; val = get_abs_value(cval, val); if (val != oval) { - err = snd_usb_set_cur_mix_value(cval, 0, 0, val); - if (err < 0) - return filter_error(cval, err); + if (deferred) { + /* record only; one flush writes the whole + * unit in a single burst. Read-only + * masters keep the direct-path behaviour: + * no write, no cache update. + */ + if (!mixer_ch_readonly(cval, 0)) { + set_bit(0, cval->cached); + cval->cache_val[0] = val; + set_bit(0, cval->pending); + cval->master_explicit = true; + mixer_arm_flush(cval->head.mixer); + } + } else { + err = snd_usb_set_cur_mix_value(cval, 0, 0, val); + if (err < 0) + return filter_error(cval, err); + } changed = 1; } } @@ -3259,6 +3675,8 @@ static void snd_usb_mixer_free(struct usb_mixer_interface *mixer) /* kill pending URBs */ snd_usb_mixer_disconnect(mixer); + cancel_delayed_work_sync(&mixer->flush_work); + /* Unregister controls first, snd_ctl_remove() frees the element */ if (mixer->id_elems) { for (id = 0; id < MAX_ID_ELEMS; id++) { @@ -3685,8 +4103,14 @@ void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid) if (!list->is_std_info) continue; info = mixer_elem_list_to_info(list); - /* invalidate cache, so the value is read from the device */ - if (!info->get_cur_broken) + /* invalidate cache, so the value is read from the device; + * under QUIRK_FLAG_MIXER_STATE_REPUSH the deferred controls + * own their cache as the host-side truth and are never + * invalidated + */ + if (!info->get_cur_broken && + !(mixer->chip->quirk_flags & QUIRK_FLAG_MIXER_STATE_REPUSH && + is_feature_volume_mute_ctl(info))) bitmap_zero(info->cached, MAX_CHANNELS + 1); snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &list->kctl->id); @@ -3783,8 +4207,14 @@ static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer, switch (attribute) { case UAC2_CS_CUR: - /* invalidate cache, so the value is read from the device */ - if (!info->get_cur_broken) { + /* invalidate cache, so the value is read from the + * device; under QUIRK_FLAG_MIXER_STATE_REPUSH the + * deferred controls own their cache as the + * host-side truth and are never invalidated + */ + if (!info->get_cur_broken && + !(mixer->chip->quirk_flags & QUIRK_FLAG_MIXER_STATE_REPUSH && + is_feature_volume_mute_ctl(info))) { if (channel) clear_bit(channel, info->cached); else /* master channel */ @@ -3914,6 +4344,7 @@ int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif) if (!mixer) return -ENOMEM; mixer->chip = chip; + INIT_DELAYED_WORK(&mixer->flush_work, snd_usb_mixer_flush_work); mixer->ignore_ctl_error = !!(chip->quirk_flags & QUIRK_FLAG_IGNORE_CTL_ERROR); mixer->id_elems = kzalloc_objs(*mixer->id_elems, MAX_ID_ELEMS); if (!mixer->id_elems) { @@ -3976,13 +4407,51 @@ void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer) { if (mixer->disconnected) return; + /* Every mod_delayed_work() call site either holds this lock and + * re-checks ->disconnected inside it, or runs from a PM callback + * (which completes before the USB core calls driver->disconnect), + * or is the flush worker itself before it returns. Marking the + * mixer disconnected under the lock before cancelling therefore + * guarantees no flush can be armed after the cancel returns. + */ + scoped_guard(mutex, &mixer->lock) { + mixer->disconnected = true; + } + cancel_delayed_work_sync(&mixer->flush_work); if (mixer->urb) usb_kill_urb(mixer->urb); if (mixer->rc_urb) usb_kill_urb(mixer->rc_urb); if (mixer->private_free) mixer->private_free(mixer); - mixer->disconnected = true; +} + +/** + * snd_usb_mixer_stream_opened - notify mixers of a PCM stream open + * @chip: the USB audio chip + * + * Called from the PCM open path. Devices flagged + * QUIRK_FLAG_MIXER_STATE_REPUSH request a full-state re-push (the + * vendor driver's every-open write pattern, healing writes swallowed + * in the post-enumeration loss window), merged into the flush timer + * so the burst never observes a half-applied gesture. + */ +void snd_usb_mixer_stream_opened(struct snd_usb_audio *chip) +{ + struct usb_mixer_interface *mixer; + + if (!(chip->quirk_flags & QUIRK_FLAG_MIXER_STATE_REPUSH)) + return; + + list_for_each_entry(mixer, &chip->mixer_list, list) { + scoped_guard(mutex, &mixer->lock) { + if (mixer->disconnected) + continue; + set_bit(MIXER_FLUSH_FULL, &mixer->flush_flags); + mod_delayed_work(system_wq, &mixer->flush_work, + MIXER_FLUSH_DELAY); + } + } } /* stop any bus activity of a mixer */ @@ -4007,6 +4476,16 @@ static int snd_usb_mixer_activate(struct usb_mixer_interface *mixer) int snd_usb_mixer_suspend(struct usb_mixer_interface *mixer) { + /* Do not wait for an in-flight flush here: its control writes can + * be blocked in runtime-PM resume, waiting for this very suspend + * callback to finish - cancel_delayed_work_sync() would deadlock + * the PM worker against the flush worker. Cancel without waiting + * and mark the mixer suspended; a flush already in flight stops + * at the next unit (failed writes keep their values pending), + * and the resume path re-pushes the complete state. + */ + set_bit(MIXER_FLUSH_SUSPENDED, &mixer->flush_flags); + cancel_delayed_work(&mixer->flush_work); snd_usb_mixer_inactivate(mixer); if (mixer->private_suspend) mixer->private_suspend(mixer); @@ -4021,6 +4500,17 @@ static int restore_mixer_value(struct usb_mixer_elem_list *list) if (cval->val_type == USB_MIXER_BESPOKEN) return 0; + /* Deferred controls belong to the flush mechanism: restore runs + * without the mixer lock, so writing them here could clobber + * values a concurrent deferred put just recorded, and it would + * re-send a derived master from a cache that lags its channels. + * The resume path requests a re-push that re-derives and + * re-asserts all of them instead. + */ + if (cval->head.mixer->chip->quirk_flags & QUIRK_FLAG_MIXER_STATE_REPUSH && + is_feature_volume_mute_ctl(cval)) + return 0; + if (cval->cmask) { idx = 0; for (c = 0; c < MAX_CHANNELS; c++) { @@ -4067,6 +4557,25 @@ int snd_usb_mixer_resume(struct usb_mixer_interface *mixer) snd_usb_mixer_resume_quirk(mixer); + /* A suspend may have cancelled a flush that had not fired yet, + * and restore_mixer_value() leaves the deferred controls to the + * flush: request the re-push that re-derives and re-asserts all + * of them. No lock is taken here, not even trylock: a flush + * whose control write triggered this very resume holds the lock + * and must not be blocked or silently skipped - the worker + * re-checks the request bits after every run, so this never + * gets lost. Arming without the lock is safe against teardown: + * the USB core autoresumes the device before calling the + * driver's disconnect, so this callback always runs before the + * flush work is cancelled for good. + */ + if (mixer->chip->quirk_flags & QUIRK_FLAG_MIXER_STATE_REPUSH) { + clear_bit(MIXER_FLUSH_SUSPENDED, &mixer->flush_flags); + set_bit(MIXER_FLUSH_RESUME, &mixer->flush_flags); + mod_delayed_work(system_wq, &mixer->flush_work, + MIXER_FLUSH_DELAY); + } + return snd_usb_mixer_activate(mixer); } diff --git a/sound/usb/mixer.h b/sound/usb/mixer.h index c40f83285..813a1694c 100644 --- a/sound/usb/mixer.h +++ b/sound/usb/mixer.h @@ -41,6 +41,15 @@ struct usb_mixer_interface { bool disconnected; + /* atomic write bursts (QUIRK_FLAG_MIXER_STATE_REPUSH) */ + struct delayed_work flush_work; + /* flush requests, atomic bit ops only (see mixer.c): + * 0 - re-push all cached volume values at the next flush + * 1 - resume: also re-assert cached mute switches + * 2 - suspend in flight: stop writing, retry after resume + */ + unsigned long flush_flags; + void *private_data; void (*private_free)(struct usb_mixer_interface *mixer); void (*private_suspend)(struct usb_mixer_interface *mixer); @@ -95,6 +104,14 @@ struct usb_mixer_elem_info { int dBmin, dBmax; DECLARE_BITMAP(cached, MAX_CHANNELS + 1); int cache_val[MAX_CHANNELS]; + /* channels changed since the last flush + * (QUIRK_FLAG_MIXER_STATE_REPUSH); same bit layout as cached + */ + DECLARE_BITMAP(pending, MAX_CHANNELS + 1); + /* user space has put this master explicitly: stop deriving it from + * the channels (QUIRK_FLAG_MIXER_STATE_REPUSH) + */ + bool master_explicit; u8 initialized; u8 min_mute; u8 get_cur_broken; @@ -129,6 +146,8 @@ int snd_usb_mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag, int snd_usb_mixer_suspend(struct usb_mixer_interface *mixer); int snd_usb_mixer_resume(struct usb_mixer_interface *mixer); +void snd_usb_mixer_stream_opened(struct snd_usb_audio *chip); + int snd_usb_set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel, int index, int value); diff --git a/sound/usb/pcm.c b/sound/usb/pcm.c index 57a26492d..ec8ad7bd7 100644 --- a/sound/usb/pcm.c +++ b/sound/usb/pcm.c @@ -24,6 +24,7 @@ #include "power.h" #include "media.h" #include "implicit.h" +#include "mixer.h" #define SUBSTREAM_FLAG_DATA_EP_STARTED 0 #define SUBSTREAM_FLAG_SYNC_EP_STARTED 1 @@ -1271,6 +1272,9 @@ static int snd_usb_pcm_open(struct snd_pcm_substream *substream) if (ret < 0) goto err_resume; + /* may re-push the cached mixer state (write-loss recovery) */ + snd_usb_mixer_stream_opened(chip); + return 0; err_resume: diff --git a/sound/usb/quirks.c b/sound/usb/quirks.c index 6ef6d4410..5328faa3d 100644 --- a/sound/usb/quirks.c +++ b/sound/usb/quirks.c @@ -2504,6 +2504,7 @@ static const struct usb_audio_quirk_flags_table quirk_flags_table[] = { QUIRK_FLAG_GET_SAMPLE_RATE | QUIRK_FLAG_MIC_RES_16), DEVICE_FLG(0x040b, 0x0897, /* XiiSound/Weltrend Temple and rebranded headsets */ QUIRK_FLAG_MIXER_INIT_VOLUME_MAX | + QUIRK_FLAG_MIXER_STATE_REPUSH | QUIRK_FLAG_MIXER_PLAYBACK_LINEAR_VOL | QUIRK_FLAG_MIXER_CAPTURE_LINEAR_VOL), DEVICE_FLG(0x041e, 0x3000, /* Creative SB Extigy */ @@ -2984,6 +2985,7 @@ static const char *const snd_usb_audio_quirk_flag_names[] = { QUIRK_STRING_ENTRY(ALWAYS_SET_RATE), QUIRK_STRING_ENTRY(SWAP_RATES), QUIRK_STRING_ENTRY(MIXER_INIT_VOLUME_MAX), + QUIRK_STRING_ENTRY(MIXER_STATE_REPUSH), NULL }; diff --git a/sound/usb/usbaudio.h b/sound/usb/usbaudio.h index 61794fdc4..b530513a3 100644 --- a/sound/usb/usbaudio.h +++ b/sound/usb/usbaudio.h @@ -271,6 +271,22 @@ extern bool snd_usb_skip_validation; * Initialize the volume control channels to the maximum instead of * the minimum; the controls remain writable and the hardware volume * operates normally afterwards. + * QUIRK_FLAG_MIXER_STATE_REPUSH + * Some devices silently drop mixer writes issued in the first seconds + * after USB enumeration, and can drop individual writes at any later + * time as well, so the state the host believes in never reaches the + * hardware (and GET_CUR is typically broken on these devices, so the + * divergence cannot be detected by reading back). The vendor's own + * Windows driver never reads back either: it collects the whole + * channel picture at a single source of truth and writes every + * channel of a control in one short burst, re-pushing the complete + * state at every device open. Record volume and mute control writes + * instead of issuing them directly, and write each touched feature + * unit in one burst from a deferred flush (the master volume first, + * derived from the loudest channel); a PCM stream open requests a + * full-state re-push through the same timer. Under this flag the + * cache holds what user space last asked for and is never invalidated + * by device status notifications. */ enum { @@ -309,6 +325,7 @@ enum { QUIRK_TYPE_ALWAYS_SET_RATE = 32, QUIRK_TYPE_SWAP_RATES = 33, QUIRK_TYPE_MIXER_INIT_VOLUME_MAX = 34, + QUIRK_TYPE_MIXER_STATE_REPUSH = 35, /* Please also edit snd_usb_audio_quirk_flag_names and alsa-configuration.rst */ }; @@ -349,5 +366,6 @@ enum { #define QUIRK_FLAG_ALWAYS_SET_RATE QUIRK_FLAG(ALWAYS_SET_RATE) #define QUIRK_FLAG_SWAP_RATES QUIRK_FLAG(SWAP_RATES) #define QUIRK_FLAG_MIXER_INIT_VOLUME_MAX QUIRK_FLAG(MIXER_INIT_VOLUME_MAX) +#define QUIRK_FLAG_MIXER_STATE_REPUSH QUIRK_FLAG(MIXER_STATE_REPUSH) #endif /* __USBAUDIO_H */ -- 2.56.1 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH 2/2] ALSA: usb-audio: add MIXER_STATE_REPUSH quirk flag 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 0 siblings, 0 replies; 8+ messages in thread From: sashiko-bot @ 2026-11-17 15:04 UTC (permalink / raw) To: Pavel Artemev; +Cc: linux-sound Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] ALSA: usb-audio: stale master cache_val in mixer_ctl_feature_put() - [Medium] ALSA: usb-audio: master_explicit stays pinned in mixer_ctl_feature_put() -- commit 41831f8b3a2ed7b3056781d9884cd8cfd89e4001 Author: Pavel Artemev <a@pavel.bz> ALSA: usb-audio: add MIXER_STATE_REPUSH quirk flag This commit adds the QUIRK_FLAG_MIXER_STATE_REPUSH quirk flag to coalesce feature unit volume and mute writes into deferred per-unit bursts and re-push cached mixer state on PCM stream open and resume. Link: https://bugzilla.kernel.org/show_bug.cgi?id=221401 Link: https://lore.kernel.org/linux-sound/20261117124242.12237-3-a@pavel.bz > diff --git a/sound/usb/mixer.c b/sound/usb/mixer.c > index bf1d9fbf08aae..03d6fa3ab1d25 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, [ ... ] > +static void snd_usb_mixer_flush_pending(struct usb_mixer_interface *mixer, > + int level) > +{ [ ... ] > + /* 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); > + if (err < 0) > + usb_audio_dbg(mixer->chip, > + "derive ctl %d ch 0 failed (%d)\n", > + master->control, err); > + } [ ... ] > +static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, > + struct snd_ctl_elem_value *ucontrol) > +{ > struct usb_mixer_elem_info *cval = snd_kcontrol_chip(kcontrol); > + bool deferred; > int max_val; > int c, cnt, val, oval, err; > int changed = 0; > > + deferred = (cval->head.mixer->chip->quirk_flags & > + QUIRK_FLAG_MIXER_STATE_REPUSH) && > + is_feature_volume_mute_ctl(cval); > + > guard(mutex)(&cval->head.mixer->lock); > max_val = get_max_exposed(cval); > if (cval->cmask) { > + bool ch_put = false; > + > cnt = 0; > for (c = 0; c < MAX_CHANNELS; c++) { > if (!(cval->cmask & BIT(c))) > @@ -1632,14 +1993,54 @@ static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, > return -EINVAL; > val = get_abs_value(cval, val); > if (oval != val) { > - err = snd_usb_set_cur_mix_value(cval, c + 1, > - cnt, val); > - if (err < 0) > - return filter_error(cval, err); > + if (deferred) { > + /* record only; one flush writes the > + * whole unit in a single burst. > + * Read-only channels keep the > + * direct-path behaviour: no write, > + * no cache update. > + */ > + if (!mixer_ch_readonly(cval, c + 1)) { > + set_bit(c + 1, cval->cached); > + cval->cache_val[cnt] = val; > + set_bit(c + 1, cval->pending); > + mixer_arm_flush(cval->head.mixer); > + ch_put = true; > + } > + } else { > + err = snd_usb_set_cur_mix_value(cval, c + 1, > + cnt, val); > + if (err < 0) > + return filter_error(cval, err); > + } > changed = 1; > } > cnt++; > } > + /* The last writer owns the master: a volume channel > + * write (the way audio servers drive this unit) > + * re-claims master derivation, so a master saved and > + * restored by e.g. alsactl does not pin the volume > + * forever. 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. > + */ > + if (ch_put && cval->control == UAC_FU_VOLUME) { > + 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); > + } > + } > + } [Severity: Medium] Can m->master_explicit stay pinned after alsactl restore? parse_audio_feature_unit() builds the per-channel control before the master control: sound/usb/mixer.c:parse_audio_feature_unit() { ... if (ch_bits & 1) build_feature_ctl(state, _ftr, ch_bits, control, &iterm, unitid, 0); if (master_bits & BIT(i)) build_feature_ctl(state, _ftr, 0, control, &iterm, unitid, 0); ... } Because the per-channel control gets index 0 (lower numid) and the master control gets index 1 (higher numid), alsactl restore writes the channels first and the master second, leaving m->master_explicit set. If an audio server then writes a per-channel volume that already matches cval->cache_val (for example the value alsactl just restored, or cval->max after QUIRK_FLAG_MIXER_INIT_VOLUME_MAX), oval == val on every channel so ch_put remains false, m->master_explicit is never cleared, and no flush is armed to re-derive the master volume. > } else { > /* master channel */ > err = snd_usb_get_cur_mix_value(cval, 0, 0, &oval); > @@ -1650,9 +2051,24 @@ static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, > return -EINVAL; > val = get_abs_value(cval, val); > if (val != oval) { > - err = snd_usb_set_cur_mix_value(cval, 0, 0, val); > - if (err < 0) > - return filter_error(cval, err); > + if (deferred) { > + /* record only; one flush writes the whole > + * unit in a single burst. Read-only > + * masters keep the direct-path behaviour: > + * no write, no cache update. > + */ > + if (!mixer_ch_readonly(cval, 0)) { > + set_bit(0, cval->cached); > + cval->cache_val[0] = val; > + set_bit(0, cval->pending); > + cval->master_explicit = true; > + mixer_arm_flush(cval->head.mixer); > + } > + } else { > + err = snd_usb_set_cur_mix_value(cval, 0, 0, val); > + if (err < 0) > + return filter_error(cval, err); > + } > changed = 1; > } > } [Severity: Medium] When a per-channel write supersedes an unflushed master write by clearing bit 0 of m->pending, does leaving bit 0 of m->cached set with the unflushed m->cache_val[0] corrupt the master derivation check in snd_usb_mixer_flush_pending()? If the hardware master is below master->max, and user space writes master->max to the master control followed by master->max to a channel control before the flush runs, m->cache_val[0] is already updated to master->max while m->pending is cleared. snd_usb_mixer_flush_pending() then sees test_bit(0, master->cached) and *master->cache_val == max_val == master->max, so it skips the master write on that flush and on all later stream-open and resume re-pushes, leaving the hardware master volume stuck at its old value. Clearing bit 0 of m->cached whenever bit 0 of m->pending is cleared on ownership handoff would force the next flush to write the derived master value. -- Sashiko AI review · https://sashiko.dev/#/patchset/20261117143314.41853-1-a@pavel.bz?part=2 ^ permalink raw reply [flat|nested] 8+ messages in thread
* [RFC PATCH v6 0/2] ALSA: usb-audio: working hardware volume for XiiSound headsets (040b:0897)
@ 2026-11-17 18:07 Pavel Artemev
2026-11-17 18:07 ` [PATCH 2/2] ALSA: usb-audio: add MIXER_STATE_REPUSH quirk flag Pavel Artemev
0 siblings, 1 reply; 8+ messages in thread
From: Pavel Artemev @ 2026-11-17 18:07 UTC (permalink / raw)
To: Takashi Iwai; +Cc: Jaroslav Kysela, linux-sound, linux-kernel
During a two-week campaign to make a Temple (XiiSound/Weltrend
040b:0897) wireless dongle audible I captured the complete control
traffic of the device on both Linux (usbmon) and the vendor's own
Windows 10 stack (USBPcap). This cover letter summarizes what the
wire says about this chipset, because it differs from the device model
encoded in the recently merged workaround, commit ba9bccdf711a
("ALSA: usb-audio: Skip broken playback feature unit on XiiSound
chips"), which is queued for 7.5.
To be clear up front: that commit fixes the user-visible bug (the
dongle coming up silent or one-eared), nothing here is a regression
report against it, and this RFC is not asking for its immediate
removal. It proposes the alternative I ended up running locally -
keep the playback feature unit exposed and make its hardware volume
work - and offers the captures as evidence, both for this design
discussion and for whoever debugs these devices next.
What the wire shows
===================
1. The playback feature unit is a working hardware volume. Per-
channel SET_CUR writes apply deterministically and independently
(a fast pair CN1<-50 + CN2<-100 applies exactly; a lone channel
write does not disturb its sibling), the master gates both ears
symmetrically, and intermediate values are audible and graded.
The vendor's Windows driver drives this exact unit on every slider
and hardware-button step, mapped linearly from its slider percent
(10% -> raw 9, 50% -> raw 49), and every intermediate value is
audible there too.
2. The "any volume below the maximum mutes one or both channels"
behaviour that motivated the skip is host-side, in three stacked
layers:
- the unit advertises a bogus 0.00..0.39 dB range, under which
every user-space volume at or below 0 dB - the whole normal
PipeWire range, 100% included - resolves to the raw minimum,
which does silence the channel;
- the stereo control's GET_CUR returns a constant value, so the
sticky-GET sanity check classifies it broken and the probe
writes the minimum to both channels at every mixer build;
- user space applies one logical volume change as fragmented
writes (one per channel, the first carrying the stale sibling
value), and the device silently drops control writes in the
first seconds after enumeration (measured: the window closes
6.5-9 s after enumeration, with a ragged tail), so a stale
channel can survive until some later write happens to refresh
it. The per-ear asymmetry users report is exactly this stale
state, not a channel-mapping property: under wire-verified
canonical writes the attenuation is symmetric (12/12 steps).
3. The Windows driver never reads back (GET_CUR is broken, so it
cannot), and does not need to: it keeps the whole channel picture
at a single source of truth, writes every channel of a control in
one short burst - master first - and re-pushes the complete state
at every device open. That every-open re-push is what repairs
whatever the enumeration window swallowed.
One consequence worth recording: the create-time pin writes of the
merged quirk sit inside the loss window and are dropped along with
every other early write. The dongle still comes up audible because
it powers up with every volume channel at the maximum - which is what
the pin intended to enforce anyway - and the resume re-pin added in
its v3 is the part that does real work. So the merged fix is sound
in effect; only its description of the hardware is not.
The RFC
=======
Patch 1 replaces the skip-and-pin with three quirk flags for
040b:0897: the new QUIRK_FLAG_MIXER_INIT_VOLUME_MAX (initialize the
channels of volume controls to the maximum instead of the minimum,
exactly where the broken-GET minimum writes happen) plus the existing
PLAYBACK/CAPTURE LINEAR_VOL flags (linear scale instead of the bogus
dB range). Hardware volume then behaves like on Windows.
Patch 2 adds QUIRK_FLAG_MIXER_STATE_REPUSH, which replicates the
vendor's write discipline for write-lossy devices: volume and mute
puts only record values, one deferred flush writes each touched
feature unit in a single canonical burst (master first, derived from
the loudest channel), and every PCM stream open and resume re-push
the full cached state - the every-open repair the Windows stack
relies on. This is new mechanism territory (I found no prior
proposal for write batching or re-push in the archives), hence RFC.
Both ran as locally built DKMS modules - first against a 7.2 tree over
the wire-verified campaign, then successive revisions against 7.3.4 -
and in daily use; details in the commit messages.
Known limitations, stated for the record:
- Through the linear scale PipeWire applies its cubic, so the
mid-slider feel is quieter than on Windows (50% -> raw ~12).
Users who want the vendor loudness feel can set
api.alsa.soft-mixer = true - which is effectively what the merged
skip approach gives everyone. The value of this RFC is that the
choice stays with the user and the hardware volume keeps working
for stacks that do drive it.
- After a replug the device sits at its power-up state (maximum)
until the first write lands past the loss window; with patch 2 a
stream open re-pushes and heals, provided streams actually open
and close (a wedged PipeWire session that holds the device open
indefinitely defers the heal to the first user volume gesture).
- Hardware buttons keep stepping an internal device state that the
host cannot observe (no readback); neither approach - skip or
drive - syncs with the buttons.
I am happy to rework or drop either patch; the capture evidence is
offered independently of the design preference.
Changes in v6
=============
Thanks to the sashiko AI review for the v5 pass; both findings are
addressed:
- Master ownership now transfers on any volume-channel write
request, unchanged values included. v5 only re-claimed
derivation when a channel value actually changed, so a master
restored by alsactl - which restores channels before the master,
leaving the master put as the last write - stayed the owner
through the audio server's subsequent no-op channel writes, and
the re-pushes kept asserting the alsactl master instead of the
derived one until the first changed gesture.
- A channel put superseding an unflushed explicit master put now
drops the master's cache entry along with its pending bit. v5
kept the never-written value cached: when it equaled the control's
raw maximum, the limit-aware re-assert skip mistook it for device
state and swallowed every later derive - the hardware master
stayed stuck below its channels with no healer.
- A local pre-send review run of the same public prompts also
flagged smaller items, addressed here: patch 1's init-max quirk
now excludes mixer unit controls (their control selector field
stores a 1-based input pin number, which aliases UAC_FU_VOLUME -
a broken-GET mixer unit input would have been initialized to the
maximum instead of the minimum; latent, this device has no mixer
units); processing-unit selectors sharing the 0x02 value are
disclosed in the commit message as not excluded (none exist on
this device); the quirk flag documentation no longer describes
the stream-open re-push as a full-state re-push (it re-sends
cached volumes; only a resume additionally re-asserts cached
mutes) - in both the header comment and the docs; and a master
put arriving right after an ownership transfer dropped its cache
entry is now recorded unconditionally instead of read back (on
broken-GET master controls that read would fail until the next
derive re-caches). The same review confirmed the two v5
findings as fixed in code and probed the ownership-transfer
machinery for residue; the one accepted residual is that an
ownership transfer empties the master cache, so the next re-push
re-derives the master even at the raw maximum - never a lone
write, it shares the burst with the channel re-pushes.
Wire-verified on the device against 7.3.4: the v5 verification set
still holds (superseded explicit value never reaching the wire,
stream-open re-push re-asserting the master below the raw maximum,
raw-max skip, runtime-PM paths); the v6 ownership changes likewise:
a no-op volume-channel write following an explicit master put
re-claims derivation, so the stream-open re-push derives the master
instead of re-asserting the explicit value, and an explicit master
put to the raw maximum superseded by a channel put in the same flush
window produces the full derived burst including the master.
--
2.56.1
^ permalink raw reply [flat|nested] 8+ messages in thread* [PATCH 2/2] ALSA: usb-audio: add MIXER_STATE_REPUSH quirk flag 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 ` Pavel Artemev 2026-11-17 18:40 ` sashiko-bot 0 siblings, 1 reply; 8+ messages in thread From: Pavel Artemev @ 2026-11-17 18:07 UTC (permalink / raw) To: Takashi Iwai; +Cc: Jaroslav Kysela, linux-sound, linux-kernel Some USB audio devices silently drop Feature Unit control writes in the first seconds after USB enumeration, and occasionally drop individual writes at any later time as well. When GET_CUR on such a device is broken there is no way to detect the divergence by reading back, so the state the host believes in silently drifts from the hardware state: a channel left at a stale value is never corrected. The Windows driver of one such chipset (Weltrend/XiiSound, 040b:0897) handles this by never reading back at all. USBPcap captures of it show a single source of truth for the whole channel picture, every channel of a control written in one short burst - master first, then the channels - and the complete state re-pushed at every device open. That every-open re-push is the load-bearing mechanism: it repairs whatever writes were swallowed around enumeration. Add QUIRK_FLAG_MIXER_STATE_REPUSH, which replicates that write discipline in the driver: * volume and mute puts only record values; the cache is defined as "what user space last asked for" and is never read back from the device, and never invalidated by device status notifications; * a single deferred flush (30 ms quiescence) writes each touched feature unit in one burst, master first, derived as the loudest channel - never asserted from its own possibly stale cache. The last writer owns the master: an explicit master put stops derivation for that unit until user space requests a volume channel write of it again, unchanged values included (alsactl restores channels before the master, so ownership must not survive the audio server's first no-op channel write), and an unflushed explicit value is dropped with the ownership together with its cache entry, so it can neither be flushed over newer channel writes nor poison the raw-max re-assert skip. A master put arriving while the transfer dropped the cache entry is recorded unconditionally instead of read back: on broken-GET master controls that read would fail until the next derive re-caches; * a PCM stream open and a resume re-push all cached volume values through the same timer - the every-open repair the Windows stack relies on. A resume re-push also re-asserts cached mute switches, taking over the job restore_mixer_value() usually does at resume: restore runs without the mixer lock, so writing the deferred controls there could clobber values a concurrent deferred put just recorded, and would re-send a derived master from a cache that lags its channels. The flush runs under the mixer lock, like the write path of the put callback it replaces, so a put can never interleave with a burst. Re-push requests are lock-free atomic bits, because neither PM callback may block on that lock: a control write inside a flush can trigger the runtime resume whose callback then runs on the very thread holding it. Suspend marks the mixer and cancels the flush timer without waiting - a flush in flight may be blocked in runtime-PM resume, waiting for the suspend callback to finish, so waiting for it there would deadlock the PM worker against the flush worker - and a flush already running stops at the next unit, with writes that failed mid-suspend kept pending for retry. Resume requests a re-push and arms the worker without taking the lock; the worker re-checks the request bits after every run, so a request raised while it runs is never lost, and the disarm ordering against disconnect (disconnected flag under the mixer lock before the synchronizing cancel, and checked under the same lock by the deferred put callback before it arms the timer, because free() cancels the work before the controls are unregistered) keeps the teardown race-free. Only controls written through mixer_ctl_feature_put() with a volume or mute selector are deferred. Mixer unit controls share that put callback but store an input channel number in the selector field and are excluded (num_outputs is only ever set for mixer units); other feature unit controls (AGC, tone) keep the direct write path, as do the caches of non-deferred controls on notification; and read-only channels are neither written nor cached, matching the direct write path. The master is re-asserted on every flush and re-push, including unchanged values, except when it sits at the control's raw maximum: that re-send would only trigger the in-headset limit notification (nothing moved), while power-up leaves the master at raw max anyway, so the skip is quiet and correct at once. An ownership transfer empties the master cache, so the next re-push re-derives and writes the master even at the raw maximum - never as a lone write: it shares the burst with the channel re-pushes. A sporadically dropped master write below raw max is therefore healed by the next re-push; at raw max it self-evidences as a missing arrival notification and heals at the next gesture. The vendor driver has no readback either. Mute switches are only written when user space changed them, or at a resume re-push: a lost re-assert can at worst fail to mute (power-up is unmuted), and mute writes during playback audibly chop the stream on these devices. Volume writes are delayed by the quiescence window, which is not perceptible for a volume control. The flag is enabled for 040b:0897, whose loss window was measured on the wire at 6.5-9 s after enumeration with a ragged tail (beep-ACKed limit-write probes at increasing delays). Testing: ran on the device as a locally built DKMS module (7.2 tree, daily use over 10 days) and wire-verified: 21 volume gestures produced 21 bursts of exactly one write per channel in the master-first order, no readbacks, no re-sends; endpoint limit notifications fire only on user arrivals at a limit; no stream chops during playback; replug, suspend/resume, balance and mute pass end to end. The same mechanism also ran on the device as a DKMS module against 7.3.4, wire-verified: volume gestures each produced exactly one burst of one write per channel in the master-first order, with no readbacks and no writes to the capture unit. The changes over the v3 revision were also wire-verified against 7.3.4: a deferred put issued while the device is runtime-suspended produces no bus traffic and its value is delivered by the resume re-push, which re-asserts the cached volume and mute controls; a stream-open re-push re-sends volumes only; an explicit master put is followed again by derivation once user space writes a volume channel of the unit; and the derived master equal to the cached value is not re-sent in that tree (no endpoint notification without a user arrival). The changes over the v4 revision are also wire-verified against 7.3.4: an explicit master put superseded by a channel put in the same flush window produces one derived burst, and the superseded explicit value never reaches the wire; a stream-open re-push at a non-limit volume re-asserts the derived master (present on the wire, where v4 skipped it); parked at the raw maximum the re-push keeps the master absent; the runtime-PM paths (zero bus traffic for a put on a suspended device, resume re-push delivering the pending gesture, mutes and mic) behave as in v4. The changes over the v5 revision are wire-verified against 7.3.4: master ownership transfers on any volume-channel write request, unchanged values included (a no-op write after an explicit master put makes the next re-push derive the master instead of re-asserting the explicit value), an unflushed explicit master superseded by a channel put is dropped with its cache entry, so a supersession at the raw maximum still produces the full derived burst including the master, and a master put arriving while the transfer dropped the cache entry is recorded without reading the device back; the baseline gesture discipline, the raw-maximum re-assert skip and the runtime-PM paths behave as in v5. Closes: https://github.com/alsa-project/alsa-ucm-conf/issues/760 Link: https://bugzilla.kernel.org/show_bug.cgi?id=221401 Link: https://lore.kernel.org/linux-sound/20261117143314.41853-3-a@pavel.bz Assisted-by: LLM Signed-off-by: Pavel Artemev <a@pavel.bz> --- Documentation/sound/alsa-configuration.rst | 6 + sound/usb/mixer.c | 570 ++++++++++++++++++++- sound/usb/mixer.h | 19 + sound/usb/pcm.c | 4 + sound/usb/quirks.c | 2 + sound/usb/usbaudio.h | 19 + 6 files changed, 605 insertions(+), 15 deletions(-) diff --git a/Documentation/sound/alsa-configuration.rst b/Documentation/sound/alsa-configuration.rst index ea7f5aa31..4b4cdadf2 100644 --- a/Documentation/sound/alsa-configuration.rst +++ b/Documentation/sound/alsa-configuration.rst @@ -2417,6 +2417,12 @@ quirk_flags Initialize the volume control channels to the maximum value instead of the minimum at mixer creation. For devices that silence their audio path while a volume channel sits at the minimum value. + * bit 35: ``mixer_state_repush`` + Defer volume and mute writes into one burst per feature unit, and + re-push the cached volume values at every PCM stream open and + after resume (a resume additionally re-asserts cached mute + switches). For devices that silently drop control writes + around enumeration and whose controls cannot be read back. This module supports multiple devices, autoprobe and hotplugging. diff --git a/sound/usb/mixer.c b/sound/usb/mixer.c index ad96371ad..b0e799149 100644 --- a/sound/usb/mixer.c +++ b/sound/usb/mixer.c @@ -37,6 +37,7 @@ #include <linux/usb/audio.h> #include <linux/usb/audio-v2.h> #include <linux/usb/audio-v3.h> +#include <linux/workqueue.h> #include <sound/core.h> #include <sound/control.h> @@ -584,13 +585,22 @@ static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, return snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, validx, value); } +/* read-only indication of one channel of a control (channel 0 = master); + * read-only channels are not written and not cached + */ +static unsigned int mixer_ch_readonly(const struct usb_mixer_elem_info *cval, + int channel) +{ + if (channel == 0) + return cval->master_readonly; + return cval->ch_readonly & BIT(channel - 1); +} + int snd_usb_set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel, int index, int value) { int err; - unsigned int read_only = (channel == 0) ? - cval->master_readonly : - cval->ch_readonly & BIT(channel - 1); + unsigned int read_only = mixer_ch_readonly(cval, channel); if (read_only) { usb_audio_dbg(cval->head.mixer->chip, @@ -1608,18 +1618,369 @@ static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, return 0; } +/* + * Atomic mixer write bursts (QUIRK_FLAG_MIXER_STATE_REPUSH). + * + * Some devices silently drop mixer writes issued in the first seconds + * after USB enumeration, and can drop individual writes at any later + * time as well; GET_CUR is broken on them, so the divergence cannot + * be detected by reading back. The vendor's Windows driver never + * reads either: it collects the whole channel picture at a single + * source of truth and writes every channel of a control in one short + * burst (master first), re-pushing the complete state at every device + * open. + * + * User space applies one logical volume change as multiple fragmented + * control writes (one per channel, the first carrying stale siblings), + * so the stock write-per-put path cannot produce that burst. Puts + * therefore only record values - the cache is defined as "what user + * space last asked for" and is never read back from the device - and + * a single flush timer coalesces the fragments. When it fires, each + * touched feature unit is written in the Windows order: the master, + * derived as the loudest channel, first, then the changed channels, + * one write each. A stream open requests a full-state re-push + * (healing writes lost in the post-enumeration window), merged into + * the same timer so a burst never observes a half-applied gesture + * (user space opens streams mid-gesture for UI feedback sounds). + * + * The flush runs under the mixer lock, like the write path of the put + * callback it replaces: snd_usb_set_cur_mix_value() completes its USB + * transfer before updating the cache, so an unlocked flush could + * clobber values recorded concurrently by a put. The PM callbacks + * must not block on that lock, though: a control write inside a flush + * can trigger the runtime resume whose callback then runs on the very + * thread holding it. Re-push requests are therefore lock-free atomic + * bits; whoever cannot take the lock (the resume callback) only sets + * them and arms the worker, and the worker re-checks the bits after + * every run so no request is lost. + */ + +/* quiescence window for the flush; user space applies a stereo change + * as two control writes 5..20 ms apart + */ +#define MIXER_FLUSH_DELAY msecs_to_jiffies(30) + +/* mixer->flush_flags bits */ +#define MIXER_FLUSH_FULL 0 /* re-push all cached volume values */ +#define MIXER_FLUSH_RESUME 1 /* resume: also re-assert cached mutes */ +#define MIXER_FLUSH_SUSPENDED 2 /* suspend in flight: stop writing */ + +/* flush levels latched from the flags by the worker */ +enum { + FLUSH_TOUCHED, /* only channels user space changed */ + FLUSH_FULL_STATE, /* plus every cached volume value */ + FLUSH_RESUME_STATE, /* plus every cached mute switch */ +}; + +static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol); + +static bool is_feature_volume_mute_ctl(struct usb_mixer_elem_info *cval) +{ + if (!cval->head.kctl || !cval->head.kctl->put) + return false; + /* the info callback is shared with processing unit controls, whose + * put writes through a different path and does not manage the + * shared cache; the put callback is unique to feature unit and + * mixer unit controls, which the cache does cover + */ + if (cval->head.kctl->put != mixer_ctl_feature_put) + return false; + /* mixer unit controls share that put callback but store the + * 1-based input channel in cval->control, not a UAC_FU_* selector; + * num_outputs is only ever set for mixer units, so it tells the + * two apart + */ + if (cval->num_outputs) + return false; + return cval->control == UAC_FU_VOLUME || cval->control == UAC_FU_MUTE; +} + +/* Re-send every cached channel of one control, unchanged values + * included + */ +static void repush_ctl_cached(struct usb_mixer_elem_info *cval) +{ + int c, err, idx = 0; + + if (cval->cmask) { + for (c = 0; c < MAX_CHANNELS; c++) { + if (!(cval->cmask & BIT_ULL(c))) + continue; + if (test_bit(c + 1, cval->cached)) { + err = snd_usb_set_cur_mix_value(cval, c + 1, + idx, cval->cache_val[idx]); + if (err < 0) + usb_audio_dbg(cval->head.mixer->chip, + "repush ctl %d ch %d failed (%d)\n", + cval->control, c + 1, err); + } + idx++; + } + } else { + /* master channel */ + if (test_bit(0, cval->cached)) { + err = snd_usb_set_cur_mix_value(cval, 0, 0, + *cval->cache_val); + if (err < 0) + usb_audio_dbg(cval->head.mixer->chip, + "repush ctl %d ch 0 failed (%d)\n", + cval->control, err); + } + } +} + +/* Write one control's pending (changed-since-last-flush) channels. + * Pending bits are only set together with cache_val by a put, so the + * value is written unconditionally; the cache bit may have been + * cleared under us, and snd_usb_set_cur_mix_value() sets it again. + */ +static void flush_ctl_pending(struct usb_mixer_elem_info *cval) +{ + int c, err, idx = 0; + + for (c = 0; c < MAX_CHANNELS; c++) { + if (!(cval->cmask & BIT_ULL(c))) + continue; + if (test_bit(c + 1, cval->pending)) { + clear_bit(c + 1, cval->pending); + err = snd_usb_set_cur_mix_value(cval, c + 1, + idx, cval->cache_val[idx]); + if (err < 0) { + /* keep the channel pending so the next + * flush (stream open, resume) retries it + */ + set_bit(c + 1, cval->pending); + usb_audio_dbg(cval->head.mixer->chip, + "flush ctl %d ch %d failed (%d)\n", + cval->control, c + 1, err); + } + } + idx++; + } + if (!cval->cmask && test_bit(0, cval->pending)) { + /* master channel */ + clear_bit(0, cval->pending); + err = snd_usb_set_cur_mix_value(cval, 0, 0, + *cval->cache_val); + if (err < 0) { + set_bit(0, cval->pending); + usb_audio_dbg(cval->head.mixer->chip, + "flush ctl %d ch 0 failed (%d)\n", + cval->control, err); + } + } +} + +/* + * One atomic write burst per touched feature unit, Windows-style. + * level FLUSH_FULL_STATE re-pushes every cached volume value (the + * stream-open heal); FLUSH_RESUME_STATE additionally re-asserts + * cached mute switches (restore_mixer_value() leaves them to the + * flush under this quirk, and nothing is playing yet at resume, so + * the write cannot chop a stream). Otherwise only the channels user + * space changed since the last flush are written. The master is + * written first and derived as the loudest cached channel - never + * asserted from its own (possibly stale) cache. An explicit master + * put owns the master until user space requests a volume channel + * write of the unit again, unchanged values included. + * The master is re-asserted whenever the derived value differs from + * the cached one, and unchanged as well unless it sits at the + * control's raw maximum: re-sending an unchanged raw-max value makes + * these devices emit their in-headset limit notification with no + * user action nearby, while power-up leaves the master at raw max + * anyway, so the skip is quiet and correct at the same time. A + * sporadically dropped master write is thus healed by the next + * re-push unless the value is raw max, where it self-evidences as a + * missing arrival notification and heals at the next gesture. + * Mute switches are only written when user space changed them (or at + * resume re-push): a lost re-assert can only fail to mute (power-up + * is unmuted), and mute writes during playback audibly chop the + * stream on these devices. + * + * Called with mixer->lock held. + */ +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; + + lockdep_assert_held(&mixer->lock); + + if (mixer->disconnected) + return; + + for (id = 0; id < MAX_ID_ELEMS; id++) { + if (test_bit(MIXER_FLUSH_SUSPENDED, &mixer->flush_flags)) { + /* A suspend landed mid-run: stop writing (the + * writes would race the port suspend, and a + * failed one only stays pending anyway) and + * request the strongest re-push, which the + * resume path delivers. + */ + set_bit(MIXER_FLUSH_RESUME, &mixer->flush_flags); + return; + } + master = NULL; + max_val = 0; + have_ch = false; + touched = false; + master_pending = false; + 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; + idx = 0; + for (c = 0; c < MAX_CHANNELS; c++) { + if (!(cval->cmask & BIT_ULL(c))) + continue; + if (test_bit(c + 1, cval->cached)) { + /* seed from the first cached value: + * raw volume ranges can be signed + * and all-negative + */ + if (!have_ch || + cval->cache_val[idx] > max_val) + max_val = cval->cache_val[idx]; + have_ch = true; + } + idx++; + } + } + /* 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); + if (err < 0) + usb_audio_dbg(mixer->chip, + "derive ctl %d ch 0 failed (%d)\n", + master->control, err); + } + if (master && level != FLUSH_TOUCHED && !master_pending && + (master->master_explicit || !have_ch) && + !(test_bit(0, master->cached) && + *master->cache_val == master->max)) + /* user-owned master, or a unit without per-channel + * data: re-assert the cached master value, except + * the unchanged raw maximum (same notification / + * reset-at-max reasoning as the derive above) + */ + repush_ctl_cached(master); + for_each_mixer_elem(list, mixer, id) { + cval = mixer_elem_list_to_info(list); + if (!is_feature_volume_mute_ctl(cval)) + continue; + if (cval->control == UAC_FU_VOLUME && cval->cmask && + level != FLUSH_TOUCHED) { + bitmap_zero(cval->pending, MAX_CHANNELS + 1); + repush_ctl_cached(cval); + continue; + } + if (cval->control == UAC_FU_MUTE && + level == FLUSH_RESUME_STATE) { + bitmap_zero(cval->pending, MAX_CHANNELS + 1); + repush_ctl_cached(cval); + continue; + } + if (!bitmap_empty(cval->pending, MAX_CHANNELS + 1)) + flush_ctl_pending(cval); + } + } +} + +static void snd_usb_mixer_flush_work(struct work_struct *work) +{ + struct usb_mixer_interface *mixer = + container_of(work, struct usb_mixer_interface, + flush_work.work); + int level; + + scoped_guard(mutex, &mixer->lock) { + level = FLUSH_TOUCHED; + if (test_and_clear_bit(MIXER_FLUSH_FULL, + &mixer->flush_flags)) + level = FLUSH_FULL_STATE; + if (test_and_clear_bit(MIXER_FLUSH_RESUME, + &mixer->flush_flags)) + level = FLUSH_RESUME_STATE; + if (mixer->disconnected) + return; + if (level != FLUSH_TOUCHED) + usb_audio_dbg(mixer->chip, + "re-pushing mixer state (level %d)\n", + level); + snd_usb_mixer_flush_pending(mixer, level); + } + /* A request may have arrived while we ran - from a stream open, + * or from the resume callback, which cannot take this lock (see + * the comment in snd_usb_mixer_resume()). Re-arm so it gets + * its own run; arming here is safe against teardown because the + * disconnect path cancels this work only after this function + * has returned. While suspended, the resume callback does the + * arming: re-arming here would only loop on failing writes. + */ + if (!test_bit(MIXER_FLUSH_SUSPENDED, &mixer->flush_flags) && + (test_bit(MIXER_FLUSH_FULL, &mixer->flush_flags) || + test_bit(MIXER_FLUSH_RESUME, &mixer->flush_flags))) + mod_delayed_work(system_wq, &mixer->flush_work, + MIXER_FLUSH_DELAY); +} + +/* Arm the deferred flush - unless the mixer is being torn down: + * free() cancels the work before the controls are unregistered, so + * a put must never arm the timer after the disconnect mark (which + * disconnect sets under the mixer lock the put already holds). + */ +static void mixer_arm_flush(struct usb_mixer_interface *mixer) +{ + if (!mixer->disconnected) + mod_delayed_work(system_wq, &mixer->flush_work, + MIXER_FLUSH_DELAY); +} + /* put the current value to feature/mixer unit */ static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct usb_mixer_elem_info *cval = snd_kcontrol_chip(kcontrol); + bool deferred; int max_val; int c, cnt, val, oval, err; int changed = 0; + deferred = (cval->head.mixer->chip->quirk_flags & + QUIRK_FLAG_MIXER_STATE_REPUSH) && + is_feature_volume_mute_ctl(cval); + guard(mutex)(&cval->head.mixer->lock); max_val = get_max_exposed(cval); if (cval->cmask) { + bool ch_seen = false; + cnt = 0; for (c = 0; c < MAX_CHANNELS; c++) { if (!(cval->cmask & BIT(c))) @@ -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) { - err = snd_usb_set_cur_mix_value(cval, c + 1, - cnt, val); - if (err < 0) - return filter_error(cval, err); + if (deferred) { + /* record only; one flush writes the + * whole unit in a single burst. + * Read-only channels keep the + * direct-path behaviour: no write, + * no cache update. + */ + if (!mixer_ch_readonly(cval, c + 1)) { + set_bit(c + 1, cval->cached); + cval->cache_val[cnt] = val; + set_bit(c + 1, cval->pending); + mixer_arm_flush(cval->head.mixer); + } + } else { + err = snd_usb_set_cur_mix_value(cval, c + 1, + cnt, val); + if (err < 0) + return filter_error(cval, err); + } 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); + } + } + } } else { /* master channel */ + if (deferred && !test_bit(0, cval->cached)) { + /* an ownership transfer dropped the cache entry: + * the device value is unknown and, on broken-GET + * controls, unreadable - record the request + * unconditionally instead of reading it back (the + * cache holds what user space last asked for) + */ + val = ucontrol->value.integer.value[0]; + if (val < 0 || val > max_val) + return -EINVAL; + val = get_abs_value(cval, val); + if (!mixer_ch_readonly(cval, 0)) { + set_bit(0, cval->cached); + cval->cache_val[0] = val; + set_bit(0, cval->pending); + cval->master_explicit = true; + mixer_arm_flush(cval->head.mixer); + return 1; + } + } err = snd_usb_get_cur_mix_value(cval, 0, 0, &oval); if (err < 0) return filter_error(cval, err); @@ -1650,9 +2082,24 @@ static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, return -EINVAL; val = get_abs_value(cval, val); if (val != oval) { - err = snd_usb_set_cur_mix_value(cval, 0, 0, val); - if (err < 0) - return filter_error(cval, err); + if (deferred) { + /* record only; one flush writes the whole + * unit in a single burst. Read-only + * masters keep the direct-path behaviour: + * no write, no cache update. + */ + if (!mixer_ch_readonly(cval, 0)) { + set_bit(0, cval->cached); + cval->cache_val[0] = val; + set_bit(0, cval->pending); + cval->master_explicit = true; + mixer_arm_flush(cval->head.mixer); + } + } else { + err = snd_usb_set_cur_mix_value(cval, 0, 0, val); + if (err < 0) + return filter_error(cval, err); + } changed = 1; } } @@ -3259,6 +3706,8 @@ static void snd_usb_mixer_free(struct usb_mixer_interface *mixer) /* kill pending URBs */ snd_usb_mixer_disconnect(mixer); + cancel_delayed_work_sync(&mixer->flush_work); + /* Unregister controls first, snd_ctl_remove() frees the element */ if (mixer->id_elems) { for (id = 0; id < MAX_ID_ELEMS; id++) { @@ -3685,8 +4134,14 @@ void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid) if (!list->is_std_info) continue; info = mixer_elem_list_to_info(list); - /* invalidate cache, so the value is read from the device */ - if (!info->get_cur_broken) + /* invalidate cache, so the value is read from the device; + * under QUIRK_FLAG_MIXER_STATE_REPUSH the deferred controls + * own their cache as the host-side truth and are never + * invalidated + */ + if (!info->get_cur_broken && + !(mixer->chip->quirk_flags & QUIRK_FLAG_MIXER_STATE_REPUSH && + is_feature_volume_mute_ctl(info))) bitmap_zero(info->cached, MAX_CHANNELS + 1); snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &list->kctl->id); @@ -3783,8 +4238,14 @@ static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer, switch (attribute) { case UAC2_CS_CUR: - /* invalidate cache, so the value is read from the device */ - if (!info->get_cur_broken) { + /* invalidate cache, so the value is read from the + * device; under QUIRK_FLAG_MIXER_STATE_REPUSH the + * deferred controls own their cache as the + * host-side truth and are never invalidated + */ + if (!info->get_cur_broken && + !(mixer->chip->quirk_flags & QUIRK_FLAG_MIXER_STATE_REPUSH && + is_feature_volume_mute_ctl(info))) { if (channel) clear_bit(channel, info->cached); else /* master channel */ @@ -3914,6 +4375,7 @@ int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif) if (!mixer) return -ENOMEM; mixer->chip = chip; + INIT_DELAYED_WORK(&mixer->flush_work, snd_usb_mixer_flush_work); mixer->ignore_ctl_error = !!(chip->quirk_flags & QUIRK_FLAG_IGNORE_CTL_ERROR); mixer->id_elems = kzalloc_objs(*mixer->id_elems, MAX_ID_ELEMS); if (!mixer->id_elems) { @@ -3976,13 +4438,51 @@ void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer) { if (mixer->disconnected) return; + /* Every mod_delayed_work() call site either holds this lock and + * re-checks ->disconnected inside it, or runs from a PM callback + * (which completes before the USB core calls driver->disconnect), + * or is the flush worker itself before it returns. Marking the + * mixer disconnected under the lock before cancelling therefore + * guarantees no flush can be armed after the cancel returns. + */ + scoped_guard(mutex, &mixer->lock) { + mixer->disconnected = true; + } + cancel_delayed_work_sync(&mixer->flush_work); if (mixer->urb) usb_kill_urb(mixer->urb); if (mixer->rc_urb) usb_kill_urb(mixer->rc_urb); if (mixer->private_free) mixer->private_free(mixer); - mixer->disconnected = true; +} + +/** + * snd_usb_mixer_stream_opened - notify mixers of a PCM stream open + * @chip: the USB audio chip + * + * Called from the PCM open path. Devices flagged + * QUIRK_FLAG_MIXER_STATE_REPUSH request a full-state re-push (the + * vendor driver's every-open write pattern, healing writes swallowed + * in the post-enumeration loss window), merged into the flush timer + * so the burst never observes a half-applied gesture. + */ +void snd_usb_mixer_stream_opened(struct snd_usb_audio *chip) +{ + struct usb_mixer_interface *mixer; + + if (!(chip->quirk_flags & QUIRK_FLAG_MIXER_STATE_REPUSH)) + return; + + list_for_each_entry(mixer, &chip->mixer_list, list) { + scoped_guard(mutex, &mixer->lock) { + if (mixer->disconnected) + continue; + set_bit(MIXER_FLUSH_FULL, &mixer->flush_flags); + mod_delayed_work(system_wq, &mixer->flush_work, + MIXER_FLUSH_DELAY); + } + } } /* stop any bus activity of a mixer */ @@ -4007,6 +4507,16 @@ static int snd_usb_mixer_activate(struct usb_mixer_interface *mixer) int snd_usb_mixer_suspend(struct usb_mixer_interface *mixer) { + /* Do not wait for an in-flight flush here: its control writes can + * be blocked in runtime-PM resume, waiting for this very suspend + * callback to finish - cancel_delayed_work_sync() would deadlock + * the PM worker against the flush worker. Cancel without waiting + * and mark the mixer suspended; a flush already in flight stops + * at the next unit (failed writes keep their values pending), + * and the resume path re-pushes the complete state. + */ + set_bit(MIXER_FLUSH_SUSPENDED, &mixer->flush_flags); + cancel_delayed_work(&mixer->flush_work); snd_usb_mixer_inactivate(mixer); if (mixer->private_suspend) mixer->private_suspend(mixer); @@ -4021,6 +4531,17 @@ static int restore_mixer_value(struct usb_mixer_elem_list *list) if (cval->val_type == USB_MIXER_BESPOKEN) return 0; + /* Deferred controls belong to the flush mechanism: restore runs + * without the mixer lock, so writing them here could clobber + * values a concurrent deferred put just recorded, and it would + * re-send a derived master from a cache that lags its channels. + * The resume path requests a re-push that re-derives and + * re-asserts all of them instead. + */ + if (cval->head.mixer->chip->quirk_flags & QUIRK_FLAG_MIXER_STATE_REPUSH && + is_feature_volume_mute_ctl(cval)) + return 0; + if (cval->cmask) { idx = 0; for (c = 0; c < MAX_CHANNELS; c++) { @@ -4067,6 +4588,25 @@ int snd_usb_mixer_resume(struct usb_mixer_interface *mixer) snd_usb_mixer_resume_quirk(mixer); + /* A suspend may have cancelled a flush that had not fired yet, + * and restore_mixer_value() leaves the deferred controls to the + * flush: request the re-push that re-derives and re-asserts all + * of them. No lock is taken here, not even trylock: a flush + * whose control write triggered this very resume holds the lock + * and must not be blocked or silently skipped - the worker + * re-checks the request bits after every run, so this never + * gets lost. Arming without the lock is safe against teardown: + * the USB core autoresumes the device before calling the + * driver's disconnect, so this callback always runs before the + * flush work is cancelled for good. + */ + if (mixer->chip->quirk_flags & QUIRK_FLAG_MIXER_STATE_REPUSH) { + clear_bit(MIXER_FLUSH_SUSPENDED, &mixer->flush_flags); + set_bit(MIXER_FLUSH_RESUME, &mixer->flush_flags); + mod_delayed_work(system_wq, &mixer->flush_work, + MIXER_FLUSH_DELAY); + } + return snd_usb_mixer_activate(mixer); } diff --git a/sound/usb/mixer.h b/sound/usb/mixer.h index c40f83285..813a1694c 100644 --- a/sound/usb/mixer.h +++ b/sound/usb/mixer.h @@ -41,6 +41,15 @@ struct usb_mixer_interface { bool disconnected; + /* atomic write bursts (QUIRK_FLAG_MIXER_STATE_REPUSH) */ + struct delayed_work flush_work; + /* flush requests, atomic bit ops only (see mixer.c): + * 0 - re-push all cached volume values at the next flush + * 1 - resume: also re-assert cached mute switches + * 2 - suspend in flight: stop writing, retry after resume + */ + unsigned long flush_flags; + void *private_data; void (*private_free)(struct usb_mixer_interface *mixer); void (*private_suspend)(struct usb_mixer_interface *mixer); @@ -95,6 +104,14 @@ struct usb_mixer_elem_info { int dBmin, dBmax; DECLARE_BITMAP(cached, MAX_CHANNELS + 1); int cache_val[MAX_CHANNELS]; + /* channels changed since the last flush + * (QUIRK_FLAG_MIXER_STATE_REPUSH); same bit layout as cached + */ + DECLARE_BITMAP(pending, MAX_CHANNELS + 1); + /* user space has put this master explicitly: stop deriving it from + * the channels (QUIRK_FLAG_MIXER_STATE_REPUSH) + */ + bool master_explicit; u8 initialized; u8 min_mute; u8 get_cur_broken; @@ -129,6 +146,8 @@ int snd_usb_mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag, int snd_usb_mixer_suspend(struct usb_mixer_interface *mixer); int snd_usb_mixer_resume(struct usb_mixer_interface *mixer); +void snd_usb_mixer_stream_opened(struct snd_usb_audio *chip); + int snd_usb_set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel, int index, int value); diff --git a/sound/usb/pcm.c b/sound/usb/pcm.c index 57a26492d..ec8ad7bd7 100644 --- a/sound/usb/pcm.c +++ b/sound/usb/pcm.c @@ -24,6 +24,7 @@ #include "power.h" #include "media.h" #include "implicit.h" +#include "mixer.h" #define SUBSTREAM_FLAG_DATA_EP_STARTED 0 #define SUBSTREAM_FLAG_SYNC_EP_STARTED 1 @@ -1271,6 +1272,9 @@ static int snd_usb_pcm_open(struct snd_pcm_substream *substream) if (ret < 0) goto err_resume; + /* may re-push the cached mixer state (write-loss recovery) */ + snd_usb_mixer_stream_opened(chip); + return 0; err_resume: diff --git a/sound/usb/quirks.c b/sound/usb/quirks.c index 6ef6d4410..5328faa3d 100644 --- a/sound/usb/quirks.c +++ b/sound/usb/quirks.c @@ -2504,6 +2504,7 @@ static const struct usb_audio_quirk_flags_table quirk_flags_table[] = { QUIRK_FLAG_GET_SAMPLE_RATE | QUIRK_FLAG_MIC_RES_16), DEVICE_FLG(0x040b, 0x0897, /* XiiSound/Weltrend Temple and rebranded headsets */ QUIRK_FLAG_MIXER_INIT_VOLUME_MAX | + QUIRK_FLAG_MIXER_STATE_REPUSH | QUIRK_FLAG_MIXER_PLAYBACK_LINEAR_VOL | QUIRK_FLAG_MIXER_CAPTURE_LINEAR_VOL), DEVICE_FLG(0x041e, 0x3000, /* Creative SB Extigy */ @@ -2984,6 +2985,7 @@ static const char *const snd_usb_audio_quirk_flag_names[] = { QUIRK_STRING_ENTRY(ALWAYS_SET_RATE), QUIRK_STRING_ENTRY(SWAP_RATES), QUIRK_STRING_ENTRY(MIXER_INIT_VOLUME_MAX), + QUIRK_STRING_ENTRY(MIXER_STATE_REPUSH), NULL }; diff --git a/sound/usb/usbaudio.h b/sound/usb/usbaudio.h index 61794fdc4..641bc968e 100644 --- a/sound/usb/usbaudio.h +++ b/sound/usb/usbaudio.h @@ -271,6 +271,23 @@ extern bool snd_usb_skip_validation; * Initialize the volume control channels to the maximum instead of * the minimum; the controls remain writable and the hardware volume * operates normally afterwards. + * QUIRK_FLAG_MIXER_STATE_REPUSH + * Some devices silently drop mixer writes issued in the first seconds + * after USB enumeration, and can drop individual writes at any later + * time as well, so the state the host believes in never reaches the + * hardware (and GET_CUR is typically broken on these devices, so the + * divergence cannot be detected by reading back). The vendor's own + * Windows driver never reads back either: it collects the whole + * channel picture at a single source of truth and writes every + * channel of a control in one short burst, re-pushing the complete + * state at every device open. Record volume and mute control writes + * instead of issuing them directly, and write each touched feature + * unit in one burst from a deferred flush (the master volume first, + * derived from the loudest channel); a PCM stream open or a resume + * requests a re-push of the cached volume values through the same + * timer, and a resume additionally re-asserts cached mute switches. + * Under this flag the cache holds what user space last asked for and + * is never invalidated by device status notifications. */ enum { @@ -309,6 +326,7 @@ enum { QUIRK_TYPE_ALWAYS_SET_RATE = 32, QUIRK_TYPE_SWAP_RATES = 33, QUIRK_TYPE_MIXER_INIT_VOLUME_MAX = 34, + QUIRK_TYPE_MIXER_STATE_REPUSH = 35, /* Please also edit snd_usb_audio_quirk_flag_names and alsa-configuration.rst */ }; @@ -349,5 +367,6 @@ enum { #define QUIRK_FLAG_ALWAYS_SET_RATE QUIRK_FLAG(ALWAYS_SET_RATE) #define QUIRK_FLAG_SWAP_RATES QUIRK_FLAG(SWAP_RATES) #define QUIRK_FLAG_MIXER_INIT_VOLUME_MAX QUIRK_FLAG(MIXER_INIT_VOLUME_MAX) +#define QUIRK_FLAG_MIXER_STATE_REPUSH QUIRK_FLAG(MIXER_STATE_REPUSH) #endif /* __USBAUDIO_H */ -- 2.56.1 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH 2/2] ALSA: usb-audio: add MIXER_STATE_REPUSH quirk flag 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 0 siblings, 0 replies; 8+ messages in thread From: sashiko-bot @ 2026-11-17 18:40 UTC (permalink / raw) To: Pavel Artemev; +Cc: linux-sound 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 ^ permalink raw reply [flat|nested] 8+ messages in thread
* [RFC PATCH v7 0/2] ALSA: usb-audio: working hardware volume for XiiSound headsets (040b:0897)
@ 2026-11-17 19:46 Pavel Artemev
2026-11-17 19:46 ` [PATCH 2/2] ALSA: usb-audio: add MIXER_STATE_REPUSH quirk flag Pavel Artemev
0 siblings, 1 reply; 8+ messages in thread
From: Pavel Artemev @ 2026-11-17 19:46 UTC (permalink / raw)
To: Takashi Iwai; +Cc: Jaroslav Kysela, linux-sound, linux-kernel
During a two-week campaign to make a Temple (XiiSound/Weltrend
040b:0897) wireless dongle audible I captured the complete control
traffic of the device on both Linux (usbmon) and the vendor's own
Windows 10 stack (USBPcap). This cover letter summarizes what the
wire says about this chipset, because it differs from the device model
encoded in the recently merged workaround, commit ba9bccdf711a
("ALSA: usb-audio: Skip broken playback feature unit on XiiSound
chips"), which is queued for 7.5.
To be clear up front: that commit fixes the user-visible bug (the
dongle coming up silent or one-eared), nothing here is a regression
report against it, and this RFC is not asking for its immediate
removal. It proposes the alternative I ended up running locally -
keep the playback feature unit exposed and make its hardware volume
work - and offers the captures as evidence, both for this design
discussion and for whoever debugs these devices next.
What the wire shows
===================
1. The playback feature unit is a working hardware volume. Per-
channel SET_CUR writes apply deterministically and independently
(a fast pair CN1<-50 + CN2<-100 applies exactly; a lone channel
write does not disturb its sibling), the master gates both ears
symmetrically, and intermediate values are audible and graded.
The vendor's Windows driver drives this exact unit on every slider
and hardware-button step, mapped linearly from its slider percent
(10% -> raw 9, 50% -> raw 49), and every intermediate value is
audible there too.
2. The "any volume below the maximum mutes one or both channels"
behaviour that motivated the skip is host-side, in three stacked
layers:
- the unit advertises a bogus 0.00..0.39 dB range, under which
every user-space volume at or below 0 dB - the whole normal
PipeWire range, 100% included - resolves to the raw minimum,
which does silence the channel;
- the stereo control's GET_CUR returns a constant value, so the
sticky-GET sanity check classifies it broken and the probe
writes the minimum to both channels at every mixer build;
- user space applies one logical volume change as fragmented
writes (one per channel, the first carrying the stale sibling
value), and the device silently drops control writes in the
first seconds after enumeration (measured: the window closes
6.5-9 s after enumeration, with a ragged tail), so a stale
channel can survive until some later write happens to refresh
it. The per-ear asymmetry users report is exactly this stale
state, not a channel-mapping property: under wire-verified
canonical writes the attenuation is symmetric (12/12 steps).
3. The Windows driver never reads back (GET_CUR is broken, so it
cannot), and does not need to: it keeps the whole channel picture
at a single source of truth, writes every channel of a control in
one short burst - master first - and re-pushes the complete state
at every device open. That every-open re-push is what repairs
whatever the enumeration window swallowed.
One consequence worth recording: the create-time pin writes of the
merged quirk sit inside the loss window and are dropped along with
every other early write. The dongle still comes up audible because
it powers up with every volume channel at the maximum - which is what
the pin intended to enforce anyway - and the resume re-pin added in
its v3 is the part that does real work. So the merged fix is sound
in effect; only its description of the hardware is not.
The RFC
=======
Patch 1 replaces the skip-and-pin with three quirk flags for
040b:0897: the new QUIRK_FLAG_MIXER_INIT_VOLUME_MAX (initialize the
channels of volume controls to the maximum instead of the minimum,
exactly where the broken-GET minimum writes happen) plus the existing
PLAYBACK/CAPTURE LINEAR_VOL flags (linear scale instead of the bogus
dB range). Hardware volume then behaves like on Windows.
Patch 2 adds QUIRK_FLAG_MIXER_STATE_REPUSH, which replicates the
vendor's write discipline for write-lossy devices: volume and mute
puts only record values, one deferred flush writes each touched
feature unit in a single canonical burst (master first, derived from
the loudest channel), and every PCM stream open and resume re-push
the full cached state - the every-open repair the Windows stack
relies on. This is new mechanism territory (I found no prior
proposal for write batching or re-push in the archives), hence RFC.
Both ran as locally built DKMS modules - first against a 7.2 tree over
the wire-verified campaign, then successive revisions against 7.3.4 -
and in daily use; details in the commit messages.
Known limitations, stated for the record:
- Through the linear scale PipeWire applies its cubic, so the
mid-slider feel is quieter than on Windows (50% -> raw ~12).
Users who want the vendor loudness feel can set
api.alsa.soft-mixer = true - which is effectively what the merged
skip approach gives everyone. The value of this RFC is that the
choice stays with the user and the hardware volume keeps working
for stacks that do drive it.
- After a replug the device sits at its power-up state (maximum)
until the first write lands past the loss window; with patch 2 a
stream open re-pushes and heals, provided streams actually open
and close (a wedged PipeWire session that holds the device open
indefinitely defers the heal to the first user volume gesture).
- Hardware buttons keep stepping an internal device state that the
host cannot observe (no readback); neither approach - skip or
drive - syncs with the buttons.
I am happy to rework or drop either patch; the capture evidence is
offered independently of the design preference.
Changes in v7
=============
Thanks to the sashiko AI review for the v6 pass; both findings are
addressed, and both sit in the ownership-transfer hunk it reviewed:
- A volume-channel write request that re-claims master derivation
now also checks the master it takes over against the channels.
v6 handed ownership over but armed nothing: after an explicit
master put had flushed, a no-op channel write left the hardware
master at the explicit value - potentially gating both ears at
the wrong level - until the next changed write, stream open or
resume happened to re-derive it. The transfer now requests a
re-push when the master it takes over disagrees with the derived
channel maximum, so the next flush delivers the derive on its
own.
- The transfer no longer drops the cache entry of a master the
device did receive. v6 cleared it unconditionally, which both
forced a device readback (a GET_CUR, or -ENXIO where GET is
broken) for master reads until the next flush re-cached, and
defeated the raw-maximum re-assert skip for every flush that
followed a channel write: re-sending an unchanged raw-max master
makes these devices emit their in-headset limit notification, so
balance steps or stream opens while the louder channel sat at
the maximum beeped with no user arrival. Only two kinds of
entries are dropped now: never-written ones (an explicit value
superseded before its flush - kept from v6, so the skip can
never be poisoned by a value the hardware never saw) and
diverged ones (the case above); a consistent master keeps its
cache and stays quiet.
- The local pre-send review run (5 finder shards + 6 fresh-context
falsifiers, same public prompts) surfaced no new code defects in
the v7 changes; its three Low items are folded here: both
master-put record paths no longer set the derivation-ownership
flag on mute masters (a dead store - every reader filters on the
volume selector), the flag documentation states the explicit-
master ownership rule, and the testing statement now carves out
the one documented exception to "notifications only on user
arrivals". The prior-feedback shard re-verified both v6 findings
as fixed in code and the v5-era regressions as not reopened; the
concurrency lens fully fenced the one speculative teardown
question (the resume-time re-arm runs under the USB device lock
every route to mixer teardown takes first).
Wire-verified on the device against 7.3.4: the v6 verification set
still holds (baseline burst discipline, the superseded-explicit
derived burst at the raw maximum, raw-max skip, runtime-PM paths);
the v7 changes likewise: a no-op volume-channel write following a
flushed explicit master that disagrees with the channels triggers
the re-push itself - no stream open needed - with the derived master
write leading the burst; the cache entry survives consistent
transfers, so a balance step at the parked raw maximum writes only
the changed channel (no re-sent raw-max master, no endpoint
notification) and a stream-open re-push after a no-op write keeps
the master absent; and a master read after the transfer is served
from the cache with no device readback.
--
2.56.1
^ permalink raw reply [flat|nested] 8+ messages in thread* [PATCH 2/2] ALSA: usb-audio: add MIXER_STATE_REPUSH quirk flag 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 ` Pavel Artemev 2026-11-17 20:13 ` sashiko-bot 0 siblings, 1 reply; 8+ messages in thread From: Pavel Artemev @ 2026-11-17 19:46 UTC (permalink / raw) To: Takashi Iwai; +Cc: Jaroslav Kysela, linux-sound, linux-kernel Some USB audio devices silently drop Feature Unit control writes in the first seconds after USB enumeration, and occasionally drop individual writes at any later time as well. When GET_CUR on such a device is broken there is no way to detect the divergence by reading back, so the state the host believes in silently drifts from the hardware state: a channel left at a stale value is never corrected. The Windows driver of one such chipset (Weltrend/XiiSound, 040b:0897) handles this by never reading back at all. USBPcap captures of it show a single source of truth for the whole channel picture, every channel of a control written in one short burst - master first, then the channels - and the complete state re-pushed at every device open. That every-open re-push is the load-bearing mechanism: it repairs whatever writes were swallowed around enumeration. Add QUIRK_FLAG_MIXER_STATE_REPUSH, which replicates that write discipline in the driver: * volume and mute puts only record values; the cache is defined as "what user space last asked for" and is never read back from the device, and never invalidated by device status notifications; * a single deferred flush (30 ms quiescence) writes each touched feature unit in one burst, master first, derived as the loudest channel - never asserted from its own possibly stale cache. The last writer owns the master: an explicit master put stops derivation for that unit until user space requests a volume channel write of it again, unchanged values included (alsactl restores channels before the master, so ownership must not survive the audio server's first no-op channel write). An unflushed explicit value is dropped with the ownership together with its cache entry, so it can neither be flushed over newer channel writes nor poison the raw-max re-assert skip; the cache entry of a master the device did receive survives the transfer - it is the record of what the device actually plays - and is only dropped, with a full-state re-push requested, when it disagrees with the derived channel maximum, so a diverged master is re-derived by the next flush instead of waiting for a changed write or a stream open. A master put arriving while the transfer dropped the cache entry is recorded unconditionally instead of read back: on broken-GET master controls that read would fail until the next derive re-caches; * a PCM stream open and a resume re-push all cached volume values through the same timer - the every-open repair the Windows stack relies on. A resume re-push also re-asserts cached mute switches, taking over the job restore_mixer_value() usually does at resume: restore runs without the mixer lock, so writing the deferred controls there could clobber values a concurrent deferred put just recorded, and would re-send a derived master from a cache that lags its channels. The flush runs under the mixer lock, like the write path of the put callback it replaces, so a put can never interleave with a burst. Re-push requests are lock-free atomic bits, because neither PM callback may block on that lock: a control write inside a flush can trigger the runtime resume whose callback then runs on the very thread holding it. Suspend marks the mixer and cancels the flush timer without waiting - a flush in flight may be blocked in runtime-PM resume, waiting for the suspend callback to finish, so waiting for it there would deadlock the PM worker against the flush worker - and a flush already running stops at the next unit, with writes that failed mid-suspend kept pending for retry. Resume requests a re-push and arms the worker without taking the lock; the worker re-checks the request bits after every run, so a request raised while it runs is never lost, and the disarm ordering against disconnect (disconnected flag under the mixer lock before the synchronizing cancel, and checked under the same lock by the deferred put callback before it arms the timer, because free() cancels the work before the controls are unregistered) keeps the teardown race-free. Only controls written through mixer_ctl_feature_put() with a volume or mute selector are deferred. Mixer unit controls share that put callback but store an input channel number in the selector field and are excluded (num_outputs is only ever set for mixer units); other feature unit controls (AGC, tone) keep the direct write path, as do the caches of non-deferred controls on notification; and read-only channels are neither written nor cached, matching the direct write path. The master is re-asserted on every flush and re-push, including unchanged values, except when it sits at the control's raw maximum: that re-send would only trigger the in-headset limit notification (nothing moved), while power-up leaves the master at raw max anyway, so the skip is quiet and correct at once. An ownership transfer that drops the cache entry requests a re-push, so the master is re-derived and written in the next flush even at the raw maximum - never as a lone write: it shares the burst with the channel re-pushes. A sporadically dropped master write below raw max is therefore healed by the next re-push; at raw max it self-evidences as a missing arrival notification and heals when the derived value next changes. The vendor driver has no readback either. Mute switches are only written when user space changed them, or at a resume re-push: a lost re-assert can at worst fail to mute (power-up is unmuted), and mute writes during playback audibly chop the stream on these devices. Volume writes are delayed by the quiescence window, which is not perceptible for a volume control. The flag is enabled for 040b:0897, whose loss window was measured on the wire at 6.5-9 s after enumeration with a ragged tail (beep-ACKed limit-write probes at increasing delays). Testing: ran on the device as a locally built DKMS module (7.2 tree, daily use over 10 days) and wire-verified: 21 volume gestures produced 21 bursts of exactly one write per channel in the master-first order, no readbacks, no re-sends; endpoint limit notifications fire only on user arrivals at a limit in that campaign (the one exception class being the re-derived raw-max master write that supersedes an unflushed explicit value can produce, documented above); no stream chops during playback; replug, suspend/resume, balance and mute pass end to end. The same mechanism also ran on the device as a DKMS module against 7.3.4, wire-verified: volume gestures each produced exactly one burst of one write per channel in the master-first order, with no readbacks and no writes to the capture unit. The changes over the v3 revision were also wire-verified against 7.3.4: a deferred put issued while the device is runtime-suspended produces no bus traffic and its value is delivered by the resume re-push, which re-asserts the cached volume and mute controls; a stream-open re-push re-sends volumes only; an explicit master put is followed again by derivation once user space writes a volume channel of the unit; and the derived master equal to the cached value is not re-sent in that tree (no endpoint notification without a user arrival). The changes over the v4 revision are also wire-verified against 7.3.4: an explicit master put superseded by a channel put in the same flush window produces one derived burst, and the superseded explicit value never reaches the wire; a stream-open re-push at a non-limit volume re-asserts the derived master (present on the wire, where v4 skipped it); parked at the raw maximum the re-push keeps the master absent; the runtime-PM paths (zero bus traffic for a put on a suspended device, resume re-push delivering the pending gesture, mutes and mic) behave as in v4. The changes over the v5 revision are wire-verified against 7.3.4: master ownership transfers on any volume-channel write request, unchanged values included (a no-op write after an explicit master put makes the next re-push derive the master instead of re-asserting the explicit value), an unflushed explicit master superseded by a channel put is dropped with its cache entry, so a supersession at the raw maximum still produces the full derived burst including the master, and a master put arriving while the transfer dropped the cache entry is recorded without reading the device back; the baseline gesture discipline, the raw-maximum re-assert skip and the runtime-PM paths behave as in v5. The changes over the v6 revision are wire-verified against 7.3.4: the baseline gesture discipline, the superseded- explicit derived burst at the raw maximum and the runtime-PM paths behave as in v6; the v6 ownership refinements likewise: a no-op volume-channel write following a flushed explicit master that disagrees with the channels produces, without any stream open, a full-state re-push whose derived master write leads the burst (the stale master heals within one flush window); the cache entry survives consistent transfers, so a balance step while the louder channel sits at the raw maximum writes only the changed channel (no unchanged raw-max master re-send, no endpoint notification), a stream-open re-push after a no-op write at the parked raw maximum keeps the master absent, and a master read after the transfer is served from the cache with no device readback. Closes: https://github.com/alsa-project/alsa-ucm-conf/issues/760 Link: https://bugzilla.kernel.org/show_bug.cgi?id=221401 Link: https://lore.kernel.org/linux-sound/20261117180728.117915-3-a@pavel.bz Assisted-by: LLM Signed-off-by: Pavel Artemev <a@pavel.bz> --- Documentation/sound/alsa-configuration.rst | 6 + sound/usb/mixer.c | 637 ++++++++++++++++++++- sound/usb/mixer.h | 19 + sound/usb/pcm.c | 4 + sound/usb/quirks.c | 2 + sound/usb/usbaudio.h | 22 + 6 files changed, 675 insertions(+), 15 deletions(-) diff --git a/Documentation/sound/alsa-configuration.rst b/Documentation/sound/alsa-configuration.rst index ea7f5aa31..4b4cdadf2 100644 --- a/Documentation/sound/alsa-configuration.rst +++ b/Documentation/sound/alsa-configuration.rst @@ -2417,6 +2417,12 @@ quirk_flags Initialize the volume control channels to the maximum value instead of the minimum at mixer creation. For devices that silence their audio path while a volume channel sits at the minimum value. + * bit 35: ``mixer_state_repush`` + Defer volume and mute writes into one burst per feature unit, and + re-push the cached volume values at every PCM stream open and + after resume (a resume additionally re-asserts cached mute + switches). For devices that silently drop control writes + around enumeration and whose controls cannot be read back. This module supports multiple devices, autoprobe and hotplugging. diff --git a/sound/usb/mixer.c b/sound/usb/mixer.c index ad96371ad..e487ed8dd 100644 --- a/sound/usb/mixer.c +++ b/sound/usb/mixer.c @@ -37,6 +37,7 @@ #include <linux/usb/audio.h> #include <linux/usb/audio-v2.h> #include <linux/usb/audio-v3.h> +#include <linux/workqueue.h> #include <sound/core.h> #include <sound/control.h> @@ -584,13 +585,22 @@ static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, return snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, validx, value); } +/* read-only indication of one channel of a control (channel 0 = master); + * read-only channels are not written and not cached + */ +static unsigned int mixer_ch_readonly(const struct usb_mixer_elem_info *cval, + int channel) +{ + if (channel == 0) + return cval->master_readonly; + return cval->ch_readonly & BIT(channel - 1); +} + int snd_usb_set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel, int index, int value) { int err; - unsigned int read_only = (channel == 0) ? - cval->master_readonly : - cval->ch_readonly & BIT(channel - 1); + unsigned int read_only = mixer_ch_readonly(cval, channel); if (read_only) { usb_audio_dbg(cval->head.mixer->chip, @@ -1608,18 +1618,370 @@ static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, return 0; } +/* + * Atomic mixer write bursts (QUIRK_FLAG_MIXER_STATE_REPUSH). + * + * Some devices silently drop mixer writes issued in the first seconds + * after USB enumeration, and can drop individual writes at any later + * time as well; GET_CUR is broken on them, so the divergence cannot + * be detected by reading back. The vendor's Windows driver never + * reads either: it collects the whole channel picture at a single + * source of truth and writes every channel of a control in one short + * burst (master first), re-pushing the complete state at every device + * open. + * + * User space applies one logical volume change as multiple fragmented + * control writes (one per channel, the first carrying stale siblings), + * so the stock write-per-put path cannot produce that burst. Puts + * therefore only record values - the cache is defined as "what user + * space last asked for" and is never read back from the device - and + * a single flush timer coalesces the fragments. When it fires, each + * touched feature unit is written in the Windows order: the master, + * derived as the loudest channel, first, then the changed channels, + * one write each. A stream open requests a full-state re-push + * (healing writes lost in the post-enumeration window), merged into + * the same timer so a burst never observes a half-applied gesture + * (user space opens streams mid-gesture for UI feedback sounds). + * + * The flush runs under the mixer lock, like the write path of the put + * callback it replaces: snd_usb_set_cur_mix_value() completes its USB + * transfer before updating the cache, so an unlocked flush could + * clobber values recorded concurrently by a put. The PM callbacks + * must not block on that lock, though: a control write inside a flush + * can trigger the runtime resume whose callback then runs on the very + * thread holding it. Re-push requests are therefore lock-free atomic + * bits; whoever cannot take the lock (the resume callback) only sets + * them and arms the worker, and the worker re-checks the bits after + * every run so no request is lost. + */ + +/* quiescence window for the flush; user space applies a stereo change + * as two control writes 5..20 ms apart + */ +#define MIXER_FLUSH_DELAY msecs_to_jiffies(30) + +/* mixer->flush_flags bits */ +#define MIXER_FLUSH_FULL 0 /* re-push all cached volume values */ +#define MIXER_FLUSH_RESUME 1 /* resume: also re-assert cached mutes */ +#define MIXER_FLUSH_SUSPENDED 2 /* suspend in flight: stop writing */ + +/* flush levels latched from the flags by the worker */ +enum { + FLUSH_TOUCHED, /* only channels user space changed */ + FLUSH_FULL_STATE, /* plus every cached volume value */ + FLUSH_RESUME_STATE, /* plus every cached mute switch */ +}; + +static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol); + +static bool is_feature_volume_mute_ctl(struct usb_mixer_elem_info *cval) +{ + if (!cval->head.kctl || !cval->head.kctl->put) + return false; + /* the info callback is shared with processing unit controls, whose + * put writes through a different path and does not manage the + * shared cache; the put callback is unique to feature unit and + * mixer unit controls, which the cache does cover + */ + if (cval->head.kctl->put != mixer_ctl_feature_put) + return false; + /* mixer unit controls share that put callback but store the + * 1-based input channel in cval->control, not a UAC_FU_* selector; + * num_outputs is only ever set for mixer units, so it tells the + * two apart + */ + if (cval->num_outputs) + return false; + return cval->control == UAC_FU_VOLUME || cval->control == UAC_FU_MUTE; +} + +/* Re-send every cached channel of one control, unchanged values + * included + */ +static void repush_ctl_cached(struct usb_mixer_elem_info *cval) +{ + int c, err, idx = 0; + + if (cval->cmask) { + for (c = 0; c < MAX_CHANNELS; c++) { + if (!(cval->cmask & BIT_ULL(c))) + continue; + if (test_bit(c + 1, cval->cached)) { + err = snd_usb_set_cur_mix_value(cval, c + 1, + idx, cval->cache_val[idx]); + if (err < 0) + usb_audio_dbg(cval->head.mixer->chip, + "repush ctl %d ch %d failed (%d)\n", + cval->control, c + 1, err); + } + idx++; + } + } else { + /* master channel */ + if (test_bit(0, cval->cached)) { + err = snd_usb_set_cur_mix_value(cval, 0, 0, + *cval->cache_val); + if (err < 0) + usb_audio_dbg(cval->head.mixer->chip, + "repush ctl %d ch 0 failed (%d)\n", + cval->control, err); + } + } +} + +/* Write one control's pending (changed-since-last-flush) channels. + * Pending bits are only set together with cache_val by a put, so the + * value is written unconditionally; the cache bit may have been + * cleared under us, and snd_usb_set_cur_mix_value() sets it again. + */ +static void flush_ctl_pending(struct usb_mixer_elem_info *cval) +{ + int c, err, idx = 0; + + for (c = 0; c < MAX_CHANNELS; c++) { + if (!(cval->cmask & BIT_ULL(c))) + continue; + if (test_bit(c + 1, cval->pending)) { + clear_bit(c + 1, cval->pending); + err = snd_usb_set_cur_mix_value(cval, c + 1, + idx, cval->cache_val[idx]); + if (err < 0) { + /* keep the channel pending so the next + * flush (stream open, resume) retries it + */ + set_bit(c + 1, cval->pending); + usb_audio_dbg(cval->head.mixer->chip, + "flush ctl %d ch %d failed (%d)\n", + cval->control, c + 1, err); + } + } + idx++; + } + if (!cval->cmask && test_bit(0, cval->pending)) { + /* master channel */ + clear_bit(0, cval->pending); + err = snd_usb_set_cur_mix_value(cval, 0, 0, + *cval->cache_val); + if (err < 0) { + set_bit(0, cval->pending); + usb_audio_dbg(cval->head.mixer->chip, + "flush ctl %d ch 0 failed (%d)\n", + cval->control, err); + } + } +} + +/* + * One atomic write burst per touched feature unit, Windows-style. + * level FLUSH_FULL_STATE re-pushes every cached volume value (the + * stream-open heal); FLUSH_RESUME_STATE additionally re-asserts + * cached mute switches (restore_mixer_value() leaves them to the + * flush under this quirk, and nothing is playing yet at resume, so + * the write cannot chop a stream). Otherwise only the channels user + * space changed since the last flush are written. The master is + * written first and derived as the loudest cached channel - never + * asserted from its own (possibly stale) cache. An explicit master + * put owns the master until user space requests a volume channel + * write of the unit again, unchanged values included. + * The master is re-asserted whenever the derived value differs from + * the cached one, and unchanged as well unless it sits at the + * control's raw maximum: re-sending an unchanged raw-max value makes + * these devices emit their in-headset limit notification with no + * user action nearby, while power-up leaves the master at raw max + * anyway, so the skip is quiet and correct at the same time. A + * sporadically dropped master write below raw max is thus healed + * by the next re-push; at raw max it self-evidences as a missing + * arrival notification and heals when the derived value next + * changes. + * Mute switches are only written when user space changed them (or at + * resume re-push): a lost re-assert can only fail to mute (power-up + * is unmuted), and mute writes during playback audibly chop the + * stream on these devices. + * + * Called with mixer->lock held. + */ +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; + + lockdep_assert_held(&mixer->lock); + + if (mixer->disconnected) + return; + + for (id = 0; id < MAX_ID_ELEMS; id++) { + if (test_bit(MIXER_FLUSH_SUSPENDED, &mixer->flush_flags)) { + /* A suspend landed mid-run: stop writing (the + * writes would race the port suspend, and a + * failed one only stays pending anyway) and + * request the strongest re-push, which the + * resume path delivers. + */ + set_bit(MIXER_FLUSH_RESUME, &mixer->flush_flags); + return; + } + master = NULL; + max_val = 0; + have_ch = false; + touched = false; + master_pending = false; + 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; + idx = 0; + for (c = 0; c < MAX_CHANNELS; c++) { + if (!(cval->cmask & BIT_ULL(c))) + continue; + if (test_bit(c + 1, cval->cached)) { + /* seed from the first cached value: + * raw volume ranges can be signed + * and all-negative + */ + if (!have_ch || + cval->cache_val[idx] > max_val) + max_val = cval->cache_val[idx]; + have_ch = true; + } + idx++; + } + } + /* 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); + if (err < 0) + usb_audio_dbg(mixer->chip, + "derive ctl %d ch 0 failed (%d)\n", + master->control, err); + } + if (master && level != FLUSH_TOUCHED && !master_pending && + (master->master_explicit || !have_ch) && + !(test_bit(0, master->cached) && + *master->cache_val == master->max)) + /* user-owned master, or a unit without per-channel + * data: re-assert the cached master value, except + * the unchanged raw maximum (same notification / + * reset-at-max reasoning as the derive above) + */ + repush_ctl_cached(master); + for_each_mixer_elem(list, mixer, id) { + cval = mixer_elem_list_to_info(list); + if (!is_feature_volume_mute_ctl(cval)) + continue; + if (cval->control == UAC_FU_VOLUME && cval->cmask && + level != FLUSH_TOUCHED) { + bitmap_zero(cval->pending, MAX_CHANNELS + 1); + repush_ctl_cached(cval); + continue; + } + if (cval->control == UAC_FU_MUTE && + level == FLUSH_RESUME_STATE) { + bitmap_zero(cval->pending, MAX_CHANNELS + 1); + repush_ctl_cached(cval); + continue; + } + if (!bitmap_empty(cval->pending, MAX_CHANNELS + 1)) + flush_ctl_pending(cval); + } + } +} + +static void snd_usb_mixer_flush_work(struct work_struct *work) +{ + struct usb_mixer_interface *mixer = + container_of(work, struct usb_mixer_interface, + flush_work.work); + int level; + + scoped_guard(mutex, &mixer->lock) { + level = FLUSH_TOUCHED; + if (test_and_clear_bit(MIXER_FLUSH_FULL, + &mixer->flush_flags)) + level = FLUSH_FULL_STATE; + if (test_and_clear_bit(MIXER_FLUSH_RESUME, + &mixer->flush_flags)) + level = FLUSH_RESUME_STATE; + if (mixer->disconnected) + return; + if (level != FLUSH_TOUCHED) + usb_audio_dbg(mixer->chip, + "re-pushing mixer state (level %d)\n", + level); + snd_usb_mixer_flush_pending(mixer, level); + } + /* A request may have arrived while we ran - from a stream open, + * or from the resume callback, which cannot take this lock (see + * the comment in snd_usb_mixer_resume()). Re-arm so it gets + * its own run; arming here is safe against teardown because the + * disconnect path cancels this work only after this function + * has returned. While suspended, the resume callback does the + * arming: re-arming here would only loop on failing writes. + */ + if (!test_bit(MIXER_FLUSH_SUSPENDED, &mixer->flush_flags) && + (test_bit(MIXER_FLUSH_FULL, &mixer->flush_flags) || + test_bit(MIXER_FLUSH_RESUME, &mixer->flush_flags))) + mod_delayed_work(system_wq, &mixer->flush_work, + MIXER_FLUSH_DELAY); +} + +/* Arm the deferred flush - unless the mixer is being torn down: + * free() cancels the work before the controls are unregistered, so + * a put must never arm the timer after the disconnect mark (which + * disconnect sets under the mixer lock the put already holds). + */ +static void mixer_arm_flush(struct usb_mixer_interface *mixer) +{ + if (!mixer->disconnected) + mod_delayed_work(system_wq, &mixer->flush_work, + MIXER_FLUSH_DELAY); +} + /* put the current value to feature/mixer unit */ static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct usb_mixer_elem_info *cval = snd_kcontrol_chip(kcontrol); + bool deferred; int max_val; int c, cnt, val, oval, err; int changed = 0; + deferred = (cval->head.mixer->chip->quirk_flags & + QUIRK_FLAG_MIXER_STATE_REPUSH) && + is_feature_volume_mute_ctl(cval); + guard(mutex)(&cval->head.mixer->lock); max_val = get_max_exposed(cval); if (cval->cmask) { + bool ch_seen = false; + cnt = 0; for (c = 0; c < MAX_CHANNELS; c++) { if (!(cval->cmask & BIT(c))) @@ -1632,16 +1994,152 @@ static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, return -EINVAL; val = get_abs_value(cval, val); if (oval != val) { - err = snd_usb_set_cur_mix_value(cval, c + 1, - cnt, val); - if (err < 0) - return filter_error(cval, err); + if (deferred) { + /* record only; one flush writes the + * whole unit in a single burst. + * Read-only channels keep the + * direct-path behaviour: no write, + * no cache update. + */ + if (!mixer_ch_readonly(cval, c + 1)) { + set_bit(c + 1, cval->cached); + cval->cache_val[cnt] = val; + set_bit(c + 1, cval->pending); + mixer_arm_flush(cval->head.mixer); + } + } else { + err = snd_usb_set_cur_mix_value(cval, c + 1, + cnt, val); + if (err < 0) + return filter_error(cval, err); + } 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 cannot write the superseded value, and its + * cache entry follows it - or the raw-max skip in the + * flush would mistake the never-written value for + * device state and swallow every later derive. The + * cache entry of a master the device did receive + * survives the transfer - it is the record of what the + * device actually plays - and is dropped, with a + * full-state re-push requested, only when it disagrees + * with the derived channel maximum: a diverged master + * is then re-derived by the next flush instead of + * waiting for a changed write or a stream open, while + * a consistent one keeps the raw-max skip quiet. + */ + if (ch_seen) { + struct usb_mixer_elem_list *ml; + int ch_max = 0; + bool have_ch = false, re_push = false; + + for_each_mixer_elem(ml, cval->head.mixer, + cval->head.id) { + struct usb_mixer_elem_info *m = + mixer_elem_list_to_info(ml); + int ch, idx = 0; + + if (!is_feature_volume_mute_ctl(m) || + m->control != UAC_FU_VOLUME || !m->cmask) + continue; + for (ch = 0; ch < MAX_CHANNELS; ch++) { + if (!(m->cmask & BIT_ULL(ch))) + continue; + if (!test_bit(ch + 1, m->cached)) { + idx++; + continue; + } + /* seed from the first cached + * value: raw volume ranges can + * be signed and all-negative + */ + if (!have_ch || + m->cache_val[idx] > ch_max) + ch_max = m->cache_val[idx]; + have_ch = true; + idx++; + } + } + for_each_mixer_elem(ml, cval->head.mixer, + cval->head.id) { + struct usb_mixer_elem_info *m = + mixer_elem_list_to_info(ml); + bool was_explicit; + + if (!is_feature_volume_mute_ctl(m) || + m->control != UAC_FU_VOLUME || m->cmask) + continue; + was_explicit = m->master_explicit; + m->master_explicit = false; + if (test_and_clear_bit(0, m->pending)) { + /* an unflushed explicit value never + * reached the device: derive the + * master instead of writing it + */ + clear_bit(0, m->cached); + re_push = true; + } else if (was_explicit && have_ch && + test_bit(0, m->cached) && + m->cache_val[0] != ch_max) { + /* a flushed explicit value that + * disagrees with the channels: + * the device master is stale + * until something re-derives it + */ + clear_bit(0, m->cached); + re_push = true; + } + } + if (re_push) { + set_bit(MIXER_FLUSH_FULL, + &cval->head.mixer->flush_flags); + mixer_arm_flush(cval->head.mixer); + } + } } else { /* master channel */ + if (deferred && !test_bit(0, cval->cached)) { + /* an ownership transfer dropped the cache entry: + * the device value is unknown and, on broken-GET + * controls, unreadable - record the request + * unconditionally instead of reading it back (the + * cache holds what user space last asked for) + */ + val = ucontrol->value.integer.value[0]; + if (val < 0 || val > max_val) + return -EINVAL; + val = get_abs_value(cval, val); + if (!mixer_ch_readonly(cval, 0)) { + set_bit(0, cval->cached); + cval->cache_val[0] = val; + set_bit(0, cval->pending); + /* derivation ownership is a volume-master + * notion only (all readers filter on + * UAC_FU_VOLUME); a mute master records + * the same way but owns nothing + */ + if (cval->control == UAC_FU_VOLUME) + cval->master_explicit = true; + mixer_arm_flush(cval->head.mixer); + return 1; + } + } err = snd_usb_get_cur_mix_value(cval, 0, 0, &oval); if (err < 0) return filter_error(cval, err); @@ -1650,9 +2148,25 @@ static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, return -EINVAL; val = get_abs_value(cval, val); if (val != oval) { - err = snd_usb_set_cur_mix_value(cval, 0, 0, val); - if (err < 0) - return filter_error(cval, err); + if (deferred) { + /* record only; one flush writes the whole + * unit in a single burst. Read-only + * masters keep the direct-path behaviour: + * no write, no cache update. + */ + if (!mixer_ch_readonly(cval, 0)) { + set_bit(0, cval->cached); + cval->cache_val[0] = val; + set_bit(0, cval->pending); + if (cval->control == UAC_FU_VOLUME) + cval->master_explicit = true; + mixer_arm_flush(cval->head.mixer); + } + } else { + err = snd_usb_set_cur_mix_value(cval, 0, 0, val); + if (err < 0) + return filter_error(cval, err); + } changed = 1; } } @@ -3259,6 +3773,8 @@ static void snd_usb_mixer_free(struct usb_mixer_interface *mixer) /* kill pending URBs */ snd_usb_mixer_disconnect(mixer); + cancel_delayed_work_sync(&mixer->flush_work); + /* Unregister controls first, snd_ctl_remove() frees the element */ if (mixer->id_elems) { for (id = 0; id < MAX_ID_ELEMS; id++) { @@ -3685,8 +4201,14 @@ void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid) if (!list->is_std_info) continue; info = mixer_elem_list_to_info(list); - /* invalidate cache, so the value is read from the device */ - if (!info->get_cur_broken) + /* invalidate cache, so the value is read from the device; + * under QUIRK_FLAG_MIXER_STATE_REPUSH the deferred controls + * own their cache as the host-side truth and are never + * invalidated + */ + if (!info->get_cur_broken && + !(mixer->chip->quirk_flags & QUIRK_FLAG_MIXER_STATE_REPUSH && + is_feature_volume_mute_ctl(info))) bitmap_zero(info->cached, MAX_CHANNELS + 1); snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &list->kctl->id); @@ -3783,8 +4305,14 @@ static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer, switch (attribute) { case UAC2_CS_CUR: - /* invalidate cache, so the value is read from the device */ - if (!info->get_cur_broken) { + /* invalidate cache, so the value is read from the + * device; under QUIRK_FLAG_MIXER_STATE_REPUSH the + * deferred controls own their cache as the + * host-side truth and are never invalidated + */ + if (!info->get_cur_broken && + !(mixer->chip->quirk_flags & QUIRK_FLAG_MIXER_STATE_REPUSH && + is_feature_volume_mute_ctl(info))) { if (channel) clear_bit(channel, info->cached); else /* master channel */ @@ -3914,6 +4442,7 @@ int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif) if (!mixer) return -ENOMEM; mixer->chip = chip; + INIT_DELAYED_WORK(&mixer->flush_work, snd_usb_mixer_flush_work); mixer->ignore_ctl_error = !!(chip->quirk_flags & QUIRK_FLAG_IGNORE_CTL_ERROR); mixer->id_elems = kzalloc_objs(*mixer->id_elems, MAX_ID_ELEMS); if (!mixer->id_elems) { @@ -3976,13 +4505,51 @@ void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer) { if (mixer->disconnected) return; + /* Every mod_delayed_work() call site either holds this lock and + * re-checks ->disconnected inside it, or runs from a PM callback + * (which completes before the USB core calls driver->disconnect), + * or is the flush worker itself before it returns. Marking the + * mixer disconnected under the lock before cancelling therefore + * guarantees no flush can be armed after the cancel returns. + */ + scoped_guard(mutex, &mixer->lock) { + mixer->disconnected = true; + } + cancel_delayed_work_sync(&mixer->flush_work); if (mixer->urb) usb_kill_urb(mixer->urb); if (mixer->rc_urb) usb_kill_urb(mixer->rc_urb); if (mixer->private_free) mixer->private_free(mixer); - mixer->disconnected = true; +} + +/** + * snd_usb_mixer_stream_opened - notify mixers of a PCM stream open + * @chip: the USB audio chip + * + * Called from the PCM open path. Devices flagged + * QUIRK_FLAG_MIXER_STATE_REPUSH request a full-state re-push (the + * vendor driver's every-open write pattern, healing writes swallowed + * in the post-enumeration loss window), merged into the flush timer + * so the burst never observes a half-applied gesture. + */ +void snd_usb_mixer_stream_opened(struct snd_usb_audio *chip) +{ + struct usb_mixer_interface *mixer; + + if (!(chip->quirk_flags & QUIRK_FLAG_MIXER_STATE_REPUSH)) + return; + + list_for_each_entry(mixer, &chip->mixer_list, list) { + scoped_guard(mutex, &mixer->lock) { + if (mixer->disconnected) + continue; + set_bit(MIXER_FLUSH_FULL, &mixer->flush_flags); + mod_delayed_work(system_wq, &mixer->flush_work, + MIXER_FLUSH_DELAY); + } + } } /* stop any bus activity of a mixer */ @@ -4007,6 +4574,16 @@ static int snd_usb_mixer_activate(struct usb_mixer_interface *mixer) int snd_usb_mixer_suspend(struct usb_mixer_interface *mixer) { + /* Do not wait for an in-flight flush here: its control writes can + * be blocked in runtime-PM resume, waiting for this very suspend + * callback to finish - cancel_delayed_work_sync() would deadlock + * the PM worker against the flush worker. Cancel without waiting + * and mark the mixer suspended; a flush already in flight stops + * at the next unit (failed writes keep their values pending), + * and the resume path re-pushes the complete state. + */ + set_bit(MIXER_FLUSH_SUSPENDED, &mixer->flush_flags); + cancel_delayed_work(&mixer->flush_work); snd_usb_mixer_inactivate(mixer); if (mixer->private_suspend) mixer->private_suspend(mixer); @@ -4021,6 +4598,17 @@ static int restore_mixer_value(struct usb_mixer_elem_list *list) if (cval->val_type == USB_MIXER_BESPOKEN) return 0; + /* Deferred controls belong to the flush mechanism: restore runs + * without the mixer lock, so writing them here could clobber + * values a concurrent deferred put just recorded, and it would + * re-send a derived master from a cache that lags its channels. + * The resume path requests a re-push that re-derives and + * re-asserts all of them instead. + */ + if (cval->head.mixer->chip->quirk_flags & QUIRK_FLAG_MIXER_STATE_REPUSH && + is_feature_volume_mute_ctl(cval)) + return 0; + if (cval->cmask) { idx = 0; for (c = 0; c < MAX_CHANNELS; c++) { @@ -4067,6 +4655,25 @@ int snd_usb_mixer_resume(struct usb_mixer_interface *mixer) snd_usb_mixer_resume_quirk(mixer); + /* A suspend may have cancelled a flush that had not fired yet, + * and restore_mixer_value() leaves the deferred controls to the + * flush: request the re-push that re-derives and re-asserts all + * of them. No lock is taken here, not even trylock: a flush + * whose control write triggered this very resume holds the lock + * and must not be blocked or silently skipped - the worker + * re-checks the request bits after every run, so this never + * gets lost. Arming without the lock is safe against teardown: + * the USB core autoresumes the device before calling the + * driver's disconnect, so this callback always runs before the + * flush work is cancelled for good. + */ + if (mixer->chip->quirk_flags & QUIRK_FLAG_MIXER_STATE_REPUSH) { + clear_bit(MIXER_FLUSH_SUSPENDED, &mixer->flush_flags); + set_bit(MIXER_FLUSH_RESUME, &mixer->flush_flags); + mod_delayed_work(system_wq, &mixer->flush_work, + MIXER_FLUSH_DELAY); + } + return snd_usb_mixer_activate(mixer); } diff --git a/sound/usb/mixer.h b/sound/usb/mixer.h index c40f83285..813a1694c 100644 --- a/sound/usb/mixer.h +++ b/sound/usb/mixer.h @@ -41,6 +41,15 @@ struct usb_mixer_interface { bool disconnected; + /* atomic write bursts (QUIRK_FLAG_MIXER_STATE_REPUSH) */ + struct delayed_work flush_work; + /* flush requests, atomic bit ops only (see mixer.c): + * 0 - re-push all cached volume values at the next flush + * 1 - resume: also re-assert cached mute switches + * 2 - suspend in flight: stop writing, retry after resume + */ + unsigned long flush_flags; + void *private_data; void (*private_free)(struct usb_mixer_interface *mixer); void (*private_suspend)(struct usb_mixer_interface *mixer); @@ -95,6 +104,14 @@ struct usb_mixer_elem_info { int dBmin, dBmax; DECLARE_BITMAP(cached, MAX_CHANNELS + 1); int cache_val[MAX_CHANNELS]; + /* channels changed since the last flush + * (QUIRK_FLAG_MIXER_STATE_REPUSH); same bit layout as cached + */ + DECLARE_BITMAP(pending, MAX_CHANNELS + 1); + /* user space has put this master explicitly: stop deriving it from + * the channels (QUIRK_FLAG_MIXER_STATE_REPUSH) + */ + bool master_explicit; u8 initialized; u8 min_mute; u8 get_cur_broken; @@ -129,6 +146,8 @@ int snd_usb_mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag, int snd_usb_mixer_suspend(struct usb_mixer_interface *mixer); int snd_usb_mixer_resume(struct usb_mixer_interface *mixer); +void snd_usb_mixer_stream_opened(struct snd_usb_audio *chip); + int snd_usb_set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel, int index, int value); diff --git a/sound/usb/pcm.c b/sound/usb/pcm.c index 57a26492d..ec8ad7bd7 100644 --- a/sound/usb/pcm.c +++ b/sound/usb/pcm.c @@ -24,6 +24,7 @@ #include "power.h" #include "media.h" #include "implicit.h" +#include "mixer.h" #define SUBSTREAM_FLAG_DATA_EP_STARTED 0 #define SUBSTREAM_FLAG_SYNC_EP_STARTED 1 @@ -1271,6 +1272,9 @@ static int snd_usb_pcm_open(struct snd_pcm_substream *substream) if (ret < 0) goto err_resume; + /* may re-push the cached mixer state (write-loss recovery) */ + snd_usb_mixer_stream_opened(chip); + return 0; err_resume: diff --git a/sound/usb/quirks.c b/sound/usb/quirks.c index 6ef6d4410..5328faa3d 100644 --- a/sound/usb/quirks.c +++ b/sound/usb/quirks.c @@ -2504,6 +2504,7 @@ static const struct usb_audio_quirk_flags_table quirk_flags_table[] = { QUIRK_FLAG_GET_SAMPLE_RATE | QUIRK_FLAG_MIC_RES_16), DEVICE_FLG(0x040b, 0x0897, /* XiiSound/Weltrend Temple and rebranded headsets */ QUIRK_FLAG_MIXER_INIT_VOLUME_MAX | + QUIRK_FLAG_MIXER_STATE_REPUSH | QUIRK_FLAG_MIXER_PLAYBACK_LINEAR_VOL | QUIRK_FLAG_MIXER_CAPTURE_LINEAR_VOL), DEVICE_FLG(0x041e, 0x3000, /* Creative SB Extigy */ @@ -2984,6 +2985,7 @@ static const char *const snd_usb_audio_quirk_flag_names[] = { QUIRK_STRING_ENTRY(ALWAYS_SET_RATE), QUIRK_STRING_ENTRY(SWAP_RATES), QUIRK_STRING_ENTRY(MIXER_INIT_VOLUME_MAX), + QUIRK_STRING_ENTRY(MIXER_STATE_REPUSH), NULL }; diff --git a/sound/usb/usbaudio.h b/sound/usb/usbaudio.h index 61794fdc4..4d7101fa5 100644 --- a/sound/usb/usbaudio.h +++ b/sound/usb/usbaudio.h @@ -271,6 +271,26 @@ extern bool snd_usb_skip_validation; * Initialize the volume control channels to the maximum instead of * the minimum; the controls remain writable and the hardware volume * operates normally afterwards. + * QUIRK_FLAG_MIXER_STATE_REPUSH + * Some devices silently drop mixer writes issued in the first seconds + * after USB enumeration, and can drop individual writes at any later + * time as well, so the state the host believes in never reaches the + * hardware (and GET_CUR is typically broken on these devices, so the + * divergence cannot be detected by reading back). The vendor's own + * Windows driver never reads back either: it collects the whole + * channel picture at a single source of truth and writes every + * channel of a control in one short burst, re-pushing the complete + * state at every device open. Record volume and mute control writes + * instead of issuing them directly, and write each touched feature + * unit in one burst from a deferred flush (the master volume first, + * derived from the loudest channel unless user space wrote the + * master itself: an explicit master put owns the master until the + * next volume channel write of the unit); a PCM stream open or a + * resume + * requests a re-push of the cached volume values through the same + * timer, and a resume additionally re-asserts cached mute switches. + * Under this flag the cache holds what user space last asked for and + * is never invalidated by device status notifications. */ enum { @@ -309,6 +329,7 @@ enum { QUIRK_TYPE_ALWAYS_SET_RATE = 32, QUIRK_TYPE_SWAP_RATES = 33, QUIRK_TYPE_MIXER_INIT_VOLUME_MAX = 34, + QUIRK_TYPE_MIXER_STATE_REPUSH = 35, /* Please also edit snd_usb_audio_quirk_flag_names and alsa-configuration.rst */ }; @@ -349,5 +370,6 @@ enum { #define QUIRK_FLAG_ALWAYS_SET_RATE QUIRK_FLAG(ALWAYS_SET_RATE) #define QUIRK_FLAG_SWAP_RATES QUIRK_FLAG(SWAP_RATES) #define QUIRK_FLAG_MIXER_INIT_VOLUME_MAX QUIRK_FLAG(MIXER_INIT_VOLUME_MAX) +#define QUIRK_FLAG_MIXER_STATE_REPUSH QUIRK_FLAG(MIXER_STATE_REPUSH) #endif /* __USBAUDIO_H */ -- 2.56.1 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH 2/2] ALSA: usb-audio: add MIXER_STATE_REPUSH quirk flag 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 0 siblings, 0 replies; 8+ messages in thread From: sashiko-bot @ 2026-11-17 20:13 UTC (permalink / raw) To: Pavel Artemev; +Cc: linux-sound Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] ALSA: usb-audio: clearing master cache bit breaks mixer_ctl_feature_get() - [Medium] ALSA: usb-audio: mixer_ctl_feature_put() drops unchanged master ownership - [Medium] ALSA: usb-audio: snd_usb_mixer_flush_pending() loses pending state on error -- commit 44887f9cb8c6632f7e6e12e06a599b842f0a6231 Author: Pavel Artemev <a@pavel.bz> ALSA: usb-audio: add MIXER_STATE_REPUSH quirk flag This commit introduces QUIRK_FLAG_MIXER_STATE_REPUSH to coalesce feature unit volume and mute writes into deferred bursts and re-push cached values on PCM stream open and resume. Link: https://bugzilla.kernel.org/show_bug.cgi?id=221401 Link: https://lore.kernel.org/linux-sound/20261117180728.117915-3-a@pavel.bz > diff --git a/sound/usb/mixer.c b/sound/usb/mixer.c > index ad96371add1ad..e487ed8dd158d 100644 > --- a/sound/usb/mixer.c > +++ b/sound/usb/mixer.c [ ... ] > @@ -1608,18 +1618,370 @@ 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) > +{ [ ... ] > + for_each_mixer_elem(list, mixer, id) { > + cval = mixer_elem_list_to_info(list); > + if (!is_feature_volume_mute_ctl(cval)) > + continue; > + if (cval->control == UAC_FU_VOLUME && cval->cmask && > + level != FLUSH_TOUCHED) { > + bitmap_zero(cval->pending, MAX_CHANNELS + 1); > + repush_ctl_cached(cval); > + continue; > + } > + if (cval->control == UAC_FU_MUTE && > + level == FLUSH_RESUME_STATE) { > + bitmap_zero(cval->pending, MAX_CHANNELS + 1); > + repush_ctl_cached(cval); > + continue; > + } > + if (!bitmap_empty(cval->pending, MAX_CHANNELS + 1)) > + flush_ctl_pending(cval); > + } > + } > +} [Severity: Medium] Does clearing cval->pending before calling repush_ctl_cached() lose pending writes if snd_usb_set_cur_mix_value() fails? Unlike flush_ctl_pending(), repush_ctl_cached() only logs an error when a transfer fails and does not set the bit back in cval->pending. For a mute control during FLUSH_RESUME_STATE, a failed write is never retried by a subsequent FLUSH_TOUCHED or stream-open FLUSH_FULL_STATE because its pending bit was already cleared. [ ... ] > @@ -1632,16 +1994,152 @@ static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, > return -EINVAL; > val = get_abs_value(cval, val); [ ... ] > + for_each_mixer_elem(ml, cval->head.mixer, > + cval->head.id) { > + struct usb_mixer_elem_info *m = > + mixer_elem_list_to_info(ml); > + bool was_explicit; > + > + if (!is_feature_volume_mute_ctl(m) || > + m->control != UAC_FU_VOLUME || m->cmask) > + continue; > + was_explicit = m->master_explicit; > + m->master_explicit = false; > + if (test_and_clear_bit(0, m->pending)) { > + /* an unflushed explicit value never > + * reached the device: derive the > + * master instead of writing it > + */ > + clear_bit(0, m->cached); > + re_push = true; > + } else if (was_explicit && have_ch && > + test_bit(0, m->cached) && > + m->cache_val[0] != ch_max) { > + /* a flushed explicit value that > + * disagrees with the channels: > + * the device master is stale > + * until something re-derives it > + */ > + clear_bit(0, m->cached); > + re_push = true; > + } > + } [Severity: Medium] Can clearing bit 0 of m->cached here break mixer_ctl_feature_get() on the master control before the next flush completes? mixer_ctl_feature_get() calls snd_usb_get_cur_mix_value(cval, 0, 0, &val). While bit 0 of m->cached is clear (during the 30 ms flush window, or for the entire duration of runtime suspend because snd_usb_mixer_flush_pending() returns early when MIXER_FLUSH_SUSPENDED is set), reading the master control either fails with -ENXIO when m->get_cur_broken is set or issues a USB GET_CUR readback when it is not. That readback wakes a runtime-suspended device and re-sets bit 0 of m->cached; if the device returns m->max while ch_max == m->max, snd_usb_mixer_flush_pending() will then hit the raw-max skip and omit writing the derived master. > + if (re_push) { > + set_bit(MIXER_FLUSH_FULL, > + &cval->head.mixer->flush_flags); > + mixer_arm_flush(cval->head.mixer); > + } > + } > } else { > /* master channel */ > + if (deferred && !test_bit(0, cval->cached)) { [ ... ] > + } > err = snd_usb_get_cur_mix_value(cval, 0, 0, &oval); > if (err < 0) > return filter_error(cval, err); > @@ -1650,9 +2148,25 @@ static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, > return -EINVAL; > val = get_abs_value(cval, val); > if (val != oval) { > - err = snd_usb_set_cur_mix_value(cval, 0, 0, val); > - if (err < 0) > - return filter_error(cval, err); > + if (deferred) { > + /* record only; one flush writes the whole > + * unit in a single burst. Read-only > + * masters keep the direct-path behaviour: > + * no write, no cache update. > + */ > + if (!mixer_ch_readonly(cval, 0)) { > + set_bit(0, cval->cached); > + cval->cache_val[0] = val; > + set_bit(0, cval->pending); > + if (cval->control == UAC_FU_VOLUME) > + cval->master_explicit = true; > + mixer_arm_flush(cval->head.mixer); > + } > + } else { [Severity: Medium] Does gating cval->master_explicit = true on val != oval lose master ownership when an explicit master write matches the pre-flush master cache? When user space writes a new value to the volume channels while cval->master_explicit is false, cval->cache_val[0] on the master control keeps its previous value until snd_usb_mixer_flush_pending() runs. If user space then writes that previous value to the master control before the flush fires (or while the device is runtime-suspended), val == oval holds, so cval->master_explicit and bit 0 of cval->pending remain unset and the flush overwrites the master with the derived channel value. -- Sashiko AI review · https://sashiko.dev/#/patchset/20261117194630.155912-1-a@pavel.bz?part=2 ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-11-17 20:13 UTC | newest] Thread overview: 8+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 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 1/2] ALSA: usb-audio: drive XiiSound playback volume instead of skipping it 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 -- strict thread matches above, loose matches on Subject: below -- 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 2/2] ALSA: usb-audio: add MIXER_STATE_REPUSH quirk flag Pavel Artemev 2026-11-17 18:40 ` sashiko-bot 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
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox