ALSA: usb-audio: working hardware volume for XiiSound headsets (040b:0897)
Subsystems (2)
cover ALSA: usb-audio: working hardware volume for XiiSound headsets (040b:0897)
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 all channels of every volume control to the maximum at mixer creation, superseding the probe-time minimum writes) 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: 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 re-pushes 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 (7.2 tree, the port here is to current sound.git master) through wire-verified end-to-end rounds - replug, suspend/resume, balance, mute, rapid gestures during playback
- 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.
Pavel Artemev (2):
ALSA: usb-audio: drive XiiSound playback volume instead of skipping it
ALSA: usb-audio: add MIXER_STATE_REPUSH quirk flag
Documentation/sound/alsa-configuration.rst | 9 + sound/usb/mixer.c | 286 +++++++++++++++++++- sound/usb/mixer.h | 10 + sound/usb/mixer_maps.c | 19 - sound/usb/mixer_quirks.c | 99 ++++------ sound/usb/pcm.c | 4 + sound/usb/quirks.c | 7 + sound/usb/usbaudio.h | 27 ++ 8 files changed, 376 insertions(+), 85 deletions(-)
base-commit: f930156051f6a3dcd454ff723ca36b14d49650d2
Signature · 1 line
2.56.1
RFC PATCH 1/2 ALSA: usb-audio: drive XiiSound playback volume instead of skipping it
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, the master channel gates both ears symmetrically, and intermediate values are audible and graded. Windows drives exactly this unit on every slider and 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 initializes all channels of every volume control to the maximum once at mixer creation (superseding the probe-time minimum writes; on the wire the sequence per channel is minimum then maximum), and 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 upstream form compiles for sound/usb without warnings and passes checkpatch, but has not been load-tested as-is.
--- Documentation/sound/alsa-configuration.rst | 4 + sound/usb/mixer_maps.c | 19 ----- sound/usb/mixer_quirks.c | 99 +++++++++------------- sound/usb/quirks.c | 5 ++ sound/usb/usbaudio.h | 11 +++ 5 files changed, 60 insertions(+), 78 deletions(-)
Documentation/sound/alsa-configuration.rst +4 -0
@@ -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 all channels of the volume controls to the maximum value + 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.
sound/usb/mixer_maps.c +0 -19
@@ -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),
sound/usb/mixer_quirks.c +40 -59
129 lines not on this page · raw message
sound/usb/quirks.c +5 -0
@@ -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 };
sound/usb/usbaudio.h +11 -0
@@ -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 all channels of the volume controls to the maximum once +* at mixer creation; 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 */
Signature · 1 line
2.56.1
[email protected]
AI reviewRe: [RFC PATCH 1/2] ALSA: usb-audio: drive XiiSound playback volume instead of skipping it
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] ALSA: usb-audio: init_volume_max quirk ignores deferred range init - [Medium] ALSA: usb-audio…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] ALSA: usb-audio: init_volume_max quirk ignores deferred range init
- [Medium] ALSA: usb-audio: 32-bit BIT() overflow in snd_usb_mixer_init_volume_max()
--
commit 2df8ef85ac853457efaa66341d89d1aca5a40143
Author: Pavel Artemev <[email protected]>
ALSA: usb-audio: drive XiiSound playback volume instead of skipping it
This commit replaces the XiiSound (040b:0897) playback feature unit skip quirk with linear volume quirk flags and a new QUIRK_FLAG_MIXER_INIT_VOLUME_MAX flag that initializes feature unit volume controls to their maximum value at mixer creation.
Quoted 4 lines
sound/usb/mixer_quirks.c +0
-0
[ ... ]
Quoted 28 lines
sound/usb/mixer_quirks.c +24
-0@@ -5218,6 +5159,41 @@ static void snd_fix_plt_name(struct snd_usb_audio *chip,usb_audio_dbg (chip,"something wrong in kctl name %s\n" , id->name); } +/* +* A Feature Unit volume channel left at the minimum value silences its +* audio path on some devices. Write the maximum volume to all channels +* once at mixer creation, so that the audio path is open; the controls +* remain writable and the hardware volume operates normally afterwards. +*/ +static void snd_usb_mixer_init_volume_max (struct usb_mixer_interface *mixer, +struct usb_mixer_elem_info *cval) +{ +int i, idx, err; + +usb_audio_info (mixer->chip,"applying init volume max quirk\n" ); + +if (!cval->cmask) { +/* master channel */ + err =snd_usb_set_cur_mix_value (cval,0 ,0 , cval->max); +if (err <0 ) +usb_audio_dbg (mixer->chip,"cannot init control %d ch 0 to max: %d\n" , + cval->control, err); +return ; + } + + idx =0 ; +for (i =0 ; i <MAX_CHANNELS ; i++) {
sound/usb/mixer_quirks.c static void snd_fix_plt_name(struct snd_usb_audio *chip, · line 5195
Quoted 10 lines
sound/usb/mixer_quirks.c +10
-0+if (!(cval->cmask &BIT (i))) +continue ; +/* per-channel writes apply independently; keep going on error */ + err =snd_usb_set_cur_mix_value (cval, i +1 , idx, cval->max); +if (err <0 ) +usb_audio_dbg (mixer->chip,"cannot init control %d ch %d to max: %d\n" , + cval->control, i +1 , err); + idx++; + } +}
[Severity: Medium]
Does BIT(i) overflow on 32-bit architectures when i reaches 32?
MAX_CHANNELS is 64 and cval->cmask is a u64, whereas BIT(i) uses unsigned long. On 32-bit kernels, shifting by i >= 32 is undefined behavior and, on architectures that mask shift counts modulo 32, matches bits 0..31 a second time for i = 32..63, sending UAC_SET_CUR requests for non-existent channels 33..64 and writing extra entries into cval->cached and cval->cache_val[]. Using BIT_ULL(i) avoids the 32-bit shift overflow.
sound/usb/mixer_quirks.c void snd_usb_mixer_fu_apply_quirk(struct usb_mixer_interface *mixer, · line 5247
Quoted 8 lines
sound/usb/mixer_quirks.c +4
-0@@ -5265,6 +5241,11 @@ void snd_usb_mixer_fu_apply_quirk(struct usb_mixer_interface *mixer,if (cval->control ==UAC_FU_VOLUME &&strstr (kctl->id.name,"Capture" ))snd_usb_mixer_fu_quirk_linear_scale (mixer, cval, kctl); +/* devices that silence a channel left at the minimum value */ +if (mixer->chip->quirk_flags &QUIRK_FLAG_MIXER_INIT_VOLUME_MAX ) +if (cval->control ==UAC_FU_VOLUME ) +snd_usb_mixer_init_volume_max (mixer, cval);
[Severity: Medium]
What happens when get_min_max_with_quirks() returns -EAGAIN during __build_feature_ctl() and defers range initialization?
__build_feature_ctl() continues past -EAGAIN with cval->initialized == 0 and the failsafe cval->max == 1, so snd_usb_mixer_init_volume_max() writes 1 to every channel and marks cval->cached. When mixer_ctl_feature_info() later runs get_min_max_with_quirks() to finish initialization, init_cur_mix_raw() either overwrites all channels with cval->min without snd_usb_mixer_init_volume_max() running again, or skips channels already marked in cval->cached and leaves their cache_val[] entries stuck at 1.
Even when probe-time range discovery succeeds on a device with brokenGET_CUR, init_cur_mix_raw() first writes cval->min to every channel beforesnd_usb_mixer_init_volume_max() writes cval->max, so if the first write
lands and the second is dropped, the channel and cache remain at cval->min.
Handling QUIRK_FLAG_MIXER_INIT_VOLUME_MAX directly in init_cur_mix_raw() (initializing to cval->max instead of cval->min, and across all channels) would cover deferred initialization and avoid the initial cval->min write.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
RFC PATCH 2/2 ALSA: usb-audio: add MIXER_STATE_REPUSH quirk flag
In reply to Pavel ArtemevSome 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:
* control puts only record values; the cache is defined as "what
user space last asked for" and is never read back from the device;
* 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, and never overriding a value user space put explicitly;
* a PCM stream open requests a full-state re-push of all cached volume values, merged into the same timer so a burst never observes a half-applied gesture (user space applies one logical volume change as multiple per-channel writes and opens streams mid-gesture for UI feedback sounds).
Mute switches are only written when user space changed them: 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. This upstream port compiles for sound/usb without warnings and passes checkpatch, but has not been load-tested as-is.
--- Documentation/sound/alsa-configuration.rst | 5 + sound/usb/mixer.c | 286 ++++++++++++++++++++- sound/usb/mixer.h | 10 + sound/usb/pcm.c | 4 + sound/usb/quirks.c | 2 + sound/usb/usbaudio.h | 16 ++ 6 files changed, 316 insertions(+), 7 deletions(-)
Documentation/sound/alsa-configuration.rst +5 -0
@@ -2417,6 +2417,11 @@ quirk_flags Initialize all channels of the volume controls to the maximum value 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. + For devices that silently drop control writes around enumeration + and whose controls cannot be read back. This module supports multiple devices, autoprobe and hotplugging.
sound/usb/mixer.c +279 -7
333 lines not on this page · raw message
sound/usb/mixer.h +10 -0
@@ -41,6 +41,10 @@ struct usb_mixer_interface {bool disconnected; +/* atomic write bursts (QUIRK_FLAG_MIXER_STATE_REPUSH) */ +struct delayed_work flush_work; +bool flush_full;/* full-state re-push requested at next flush */ +void *private_data;void (*private_free)(struct usb_mixer_interface *mixer);void (*private_suspend)(struct usb_mixer_interface *mixer);
@@ -95,6 +99,10 @@ 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 );u8 initialized;u8 min_mute;u8 get_cur_broken;
@@ -129,6 +137,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);
sound/usb/pcm.c +4 -0
@@ -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:
sound/usb/quirks.c +2 -0
@@ -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 };
sound/usb/usbaudio.h +16 -0
@@ -271,6 +271,20 @@ extern bool snd_usb_skip_validation;* Initialize all channels of the volume controls to the maximum once * at mixer creation; 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 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. */ enum {
@@ -309,6 +323,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 +364,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 */
Signature · 1 line
2.56.1
[email protected]
AI reviewRe: [RFC PATCH 2/2] ALSA: usb-audio: add MIXER_STATE_REPUSH quirk flag
Thank you for your contribution! Sashiko AI review found 7 potential issues to consider: - [High] ALSA: usb-audio: dropped non-volume writes in mixer_ctl_feature_put() - [High] ALSA: usb-audio…
Thank you for your contribution! Sashiko AI review found 7 potential issues to consider:
- [High] ALSA: usb-audio: dropped non-volume writes in mixer_ctl_feature_put()
- [High] ALSA: usb-audio: skipped master re-push in snd_usb_mixer_flush_pending()
- [High] ALSA: usb-audio: unlocked snd_usb_mixer_flush_pending() races with put()
- [High] ALSA: usb-audio: missing flush_work cancel on disconnect and suspend
- [Medium] ALSA: usb-audio: broken negative volume max in snd_usb_mixer_flush_pending()
- [Medium] ALSA: usb-audio: status interrupt clears cache under MIXER_STATE_REPUSH
- [Low] ALSA: usb-audio: 32-bit shift overflow with BIT() on u64 cmask in flush helpers
--
commit f48a8ed50544ffb0295c5ae1365eebbbf684e0b3
Author: Pavel Artemev <[email protected]>
ALSA: usb-audio: add MIXER_STATE_REPUSH quirk flag
This commit introduces QUIRK_FLAG_MIXER_STATE_REPUSH to coalesce Feature Unit volume and mute control writes into a deferred burst and re-push cached volume state on PCM stream open, and enables it for Weltrend/XiiSound (040b:0897) headsets.
Quoted 4 lines
sound/usb/mixer.c +0
-0
[ ... ]
Quoted 83 lines
sound/usb/mixer.c +79
-0@@ -1597,11 +1598,229 @@ 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). +*/ + +/* 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) + +static bool is_feature_volume_mute_ctl (struct usb_mixer_elem_info *cval) +{ +if (!cval->head.kctl || !cval->head.kctl->info) +return false; +if (cval->head.kctl->info != mixer_ctl_feature_info) +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 (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. */ +static void flush_ctl_pending (struct usb_mixer_elem_info *cval) +{
sound/usb/mixer.c static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, · line 1689
Quoted 10 lines
sound/usb/mixer.c +10
-0+int c, err, idx =0 ; + +for (c =0 ; c <MAX_CHANNELS ; c++) { +if (!(cval->cmask &BIT (c))) +continue ; +if (test_bit (c +1 , cval->pending)) { +clear_bit (c +1 , cval->pending); +if (test_bit (c +1 , cval->cached)) { + err =snd_usb_set_cur_mix_value (cval, c +1 , + idx, cval->cache_val[idx]);
[Severity: Medium]
Can a USB status interrupt clear cval->cached and cause flush_ctl_pending() to drop a pending write?
snd_usb_mixer_notify_id() and snd_usb_mixer_interrupt_v2() zero info->cached whenever !info->get_cur_broken. Boolean controls such as UAC_FU_MUTE skip check_sticky_volume_control() in get_min_max_with_quirks() and keep get_cur_broken == 0, so a status interrupt for the Feature Unit clears cval->cached; flush_ctl_pending() then clears cval->pending without writing to hardware, and later mixer_ctl_feature_get() or mixer_ctl_feature_put() calls fall back to UAC_GET_CUR readbacks.
Quoted 60 lines
sound/usb/mixer.c +60
-0+if (err <0 ) +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); +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, +"flush ctl %d ch 0 failed (%d)\n" , + cval->control, err); + } + } +} + +/* +* One atomic write burst per touched feature unit, Windows-style: +* full = re-push every cached volume value (the stream-open heal), +* otherwise only the channels user space changed since the last +* flush. The master is written first and derived as the loudest +* cached channel - never asserted from its own (possibly stale) +* cache, and never overridden when user space put it explicitly. +* Mute switches are only written when user space changed them: 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. +*/ +static void snd_usb_mixer_flush_pending (struct usb_mixer_interface *mixer, +bool full) +{ +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; + +if (mixer->disconnected) +return ; + +for (id =0 ; id <MAX_ID_ELEMS ; id++) { + 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 =
sound/usb/mixer.c static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, · line 1759
Quoted 10 lines
sound/usb/mixer.c +10
-0+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 (c))) +continue ;
[Severity: Low]
Does BIT(c) overflow a 32-bit unsigned long on 32-bit architectures when c reaches 32..63?
MAX_CHANNELS is 64 and cval->cmask is a u64 (since 16ee07bfa935 ("ALSA:
usb-audio: Extend max number of channels to 64")), so the loops in repush_ctl_cached(), flush_ctl_pending(), and snd_usb_mixer_flush_pending() need BIT_ULL(c) to test bits 32..63 without undefined shift behavior.
sound/usb/mixer.c static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, · line 1764
Quoted 5 lines
sound/usb/mixer.c +5
-0+if (test_bit (c +1 , cval->cached)) { +if (cval->cache_val[idx] > max_val) + max_val = cval->cache_val[idx]; + have_ch = true; + }
[Severity: Medium]
Does initializing max_val to 0 produce the wrong master volume when all cached channel values are negative?
cval->cache_val[] holds raw hardware values from get_abs_value(), which for standard UAC Feature Unit volume controls (USB_MIXER_S16) are signed 1/256 dB values where attenuation below 0 dB is negative. When all cached channels are negative, cval->cache_val[idx] > max_val is never true while have_ch is set to true, so max_val stays 0 and snd_usb_set_cur_mix_value() writes 0 (0 dB, or an out-of-range value if master->max < 0) to the master channel. Seeding max_val when !have_ch or initializing it to INT_MIN would avoid this.
Quoted 23 lines
sound/usb/mixer.c +23
-0+ idx++; + } + } +/* master first (the Windows burst order); a max of 0 is a +* legal volume, hence the explicit have_ch test +*/ +if (master && !master_pending && have_ch && + (touched || full) && + (!test_bit (0 , master->cached) || + *master->cache_val != max_val)) { +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 && full && !have_ch && !master_pending) +/* unit without per-channel data: own cache */ +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))
sound/usb/mixer.c static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, · line 1797
Quoted 10 lines
sound/usb/mixer.c +10
-0+continue ; +if (cval->control ==UAC_FU_VOLUME && full && + cval->cmask) { +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: High]
Doesn't the (!test_bit(0, master->cached) || *master->cache_val != max_val) check prevent the master channel from being re-pushed on PCM stream open when have_ch is true?
At mixer creation (in init_cur_mix_raw() and snd_usb_mixer_init_volume_max()) and on any flush during the post-enumeration loss window, snd_usb_set_cur_mix_value() sets bit 0 in master->cached and sets *master->cache_val = max_val even when the device drops the USB write. When a stream is opened later (full == true), *master->cache_val already equals max_val, the !have_ch fallback is skipped because have_ch is true, and the second loop skips master because master->cmask is 0, so a dropped master volume write is never healed. For the same reason, a balance change that adjusts a quieter channel without changing max_val also skips writing master at the start of the burst.
Conversely, if user space explicitly writes to the master control, flush_ctl_pending(master) clears bit 0 of master->pending on that flush, so the next stream open or channel flush sees !master_pending and *master->cache_val != max_val and overwrites the explicit master volume with max_val.
Quoted 8 lines
sound/usb/mixer.c +8
-0+ } +} + +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);
sound/usb/mixer.c static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, · line 1815
Quoted 10 lines
sound/usb/mixer.c +10
-0+bool full; + + scoped_guard(mutex, &mixer->lock) { + full = mixer->flush_full; + mixer->flush_full = false; + } +if (full) +usb_audio_info (mixer->chip,"flushing mixer state (stream open)\n" ); +snd_usb_mixer_flush_pending (mixer, full); +}
[Severity: High]
Does running snd_usb_mixer_flush_pending() without mixer->lock race with mixer_ctl_feature_put()?
kworker CPU0snd_usb_mixer_flush_work()
snd_usb_mixer_flush_pending()
flush_ctl_pending()
val = cval->cache_val[idx]
snd_usb_set_cur_mix_value(..., val)
snd_usb_mixer_set_ctl_value()
mixer_ctl_feature_put()
guard(mutex)(&mixer->lock)
cval->cache_val[idx] = new_val
set_bit(c + 1, cval->pending)
cval->cache_val[idx] = val <-- clobbers new_valWhile snd_usb_mixer_set_ctl_value() sleeps in snd_usb_ctl_msg(), a concurrent mixer_ctl_feature_put() can update cval->cache_val[idx], and when the USB transfer completes snd_usb_set_cur_mix_value() overwrites cval->cache_val[idx] with the old value. Similarly, if mixer_ctl_feature_put() sets cval->pending and updates cval->cache_val[idx] between the first loop of snd_usb_mixer_flush_pending() (which computes max_val) and the second loop (which clears cval->pending via bitmap_zero() or flush_ctl_pending()), the pending bit is cleared without updating master to the new max_val. In snd_usb_mixer_stream_opened(), calling mod_delayed_work() after dropping mixer->lock also allows an expiring flush_work to consume flush_full before the timer is extended by MIXER_FLUSH_DELAY.
sound/usb/mixer.c static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, · line 1823
Quoted 8 lines
sound/usb/mixer.c +3
-0+/* 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 = cval->head.mixer->chip->quirk_flags & +QUIRK_FLAG_MIXER_STATE_REPUSH ;
[Severity: High]
Does setting deferred without checking is_feature_volume_mute_ctl(cval) drop writes to all other controls that use mixer_ctl_feature_put()?
usb_feature_unit_ctl is used for all 12 Feature Unit control types in audio_feature_info[] (such as Auto Gain Control, Bass, Treble, and Loudness) as well as Mixer Unit controls created by build_mixer_unit_ctl(). When QUIRK_FLAG_MIXER_STATE_REPUSH is set, mixer_ctl_feature_put() defers writes for all of them into cval->pending, but snd_usb_mixer_flush_pending() skips everything rejected by is_feature_volume_mute_ctl(), so those writes never reach the hardware.
Meanwhile, build_mixer_unit_ctl() (where cval->control = in_ch + 1) and mixer_procunit_ctl in build_audio_procunit() also set kctl->info = mixer_ctl_feature_info. Any Mixer Unit control for input channels 0 or 1 or Processing/Extension Unit control with selector 1 or 2 matches is_feature_volume_mute_ctl(), and because mixer_ctl_procunit_put() writes via set_cur_ctl_value() without updating cval->cache_val, a stream-open full flush overwrites Processing Unit control 2 with its stale initial cache_val.
[ ... ]
sound/usb/mixer.c void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer) · line 4219
Quoted 3 lines
sound/usb/mixer.c +0
-0@@ -3974,6 +4218,34 @@ void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer) mixer->disconnected = true; }
[Severity: High]
Can mixer->flush_work run after usb_audio_disconnect() or during USB suspend?
snd_usb_mixer_disconnect() only sets mixer->disconnected = true, while cancel_delayed_work_sync(&mixer->flush_work) is deferred until snd_usb_mixer_free() when user space closes the last ALSA file descriptor, and mixer_ctl_feature_put() still schedules flush_work even after mixer->disconnected is set. If a device is unplugged while an ALSA device node is open, usb_audio_disconnect() returns to the USB core and struct usb_device can be freed before snd_usb_mixer_flush_work() runs and dereferences mixer->chip->dev->dev in usb_audio_info() (prior to checking mixer->disconnected in snd_usb_mixer_flush_pending()). Likewise, snd_usb_mixer_suspend() does not cancel flush_work, allowing the worker to issue control transfers while the interface is suspended.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2