ALSA: usb-audio: improve module param quirk_flags
Subsystems (2)
v6 cover ALSA: usb-audio: improve module param quirk_flags
As an implementation of what has been discussed previously[1].
1. https://lore.kernel.org/all/[email protected]/
---
Changes in v6:
- Apply review commens, details corrected and wording revised
- Link to v5: https://lore.kernel.org/r/[email protected]
Changes in v5:
- Apply review comments. Thanks a lot, Takashi Iwai!
- Link to v4: https://lore.kernel.org/r/[email protected]
Changes in v4:
- Split basic parse and dynamic change
- Drop usage of linked list
- Link to v3: https://lore.kernel.org/r/[email protected]
Changes in v3:
- Instead of a new param, improve the existed one.
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Cleaned up some internal rebase confusion, sorry for that
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Cryolitia PukNgae (4):
ALSA: usb-audio: add two-way convert between name and bit for QUIRK_FLAG_*
ALSA: usb-audio: improve module param quirk_flags
ALSA: usb-audio: make param quirk_flags change-able in runtime
ALSA: doc: improved docs about quirk_flags in snd-usb-audio
Documentation/sound/alsa-configuration.rst | 108 ++++++++++++------ sound/usb/card.c | 55 +++++++-- sound/usb/quirks.c | 173 ++++++++++++++++++++++++++++- sound/usb/quirks.h | 11 +- sound/usb/usbaudio.h | 84 +++++++++----- 5 files changed, 359 insertions(+), 72 deletions(-)
---
base-commit: 4c421c40c8b30ab7aae1edc7f7e294fcd33fc186
change-id: 20250910-sound-a91c86c92dba
Best regards,
Signature · 1 line
Cryolitia PukNgae <[email protected]>
PATCH v6 1/4 ALSA: usb-audio: add two-way convert between name and bit for QUIRK_FLAG_*
Define quirk flags enum and its name.
Add helper for converting between quirk flags' name and bit.
- snd_usb_quirk_flag_find_name()
- snd_usb_quirk_flags_from_name()
Add helper for printing debug: log snd_usb_apply_flag_dbg()
--- sound/usb/quirks.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++--- sound/usb/quirks.h | 7 +++++ sound/usb/usbaudio.h | 84 ++++++++++++++++++++++++++++++++++++---------------- 3 files changed, 144 insertions(+), 30 deletions(-)
sound/usb/quirks.c +79 -4
@@ -2446,6 +2446,84 @@ static const struct usb_audio_quirk_flags_table quirk_flags_table[] = { {}/* terminator */ }; +#define QUIRK_STRING_ENTRY (x) \ + [QUIRK_TYPE_ ## x] = __stringify(x) + +static const char *const snd_usb_audio_quirk_flag_names[] = { +QUIRK_STRING_ENTRY (GET_SAMPLE_RATE ), +QUIRK_STRING_ENTRY (SHARE_MEDIA_DEVICE ), +QUIRK_STRING_ENTRY (ALIGN_TRANSFER ), +QUIRK_STRING_ENTRY (TX_LENGTH ), +QUIRK_STRING_ENTRY (PLAYBACK_FIRST ), +QUIRK_STRING_ENTRY (SKIP_CLOCK_SELECTOR ), +QUIRK_STRING_ENTRY (IGNORE_CLOCK_SOURCE ), +QUIRK_STRING_ENTRY (ITF_USB_DSD_DAC ), +QUIRK_STRING_ENTRY (CTL_MSG_DELAY ), +QUIRK_STRING_ENTRY (CTL_MSG_DELAY_1M ), +QUIRK_STRING_ENTRY (CTL_MSG_DELAY_5M ), +QUIRK_STRING_ENTRY (IFACE_DELAY ), +QUIRK_STRING_ENTRY (VALIDATE_RATES ), +QUIRK_STRING_ENTRY (DISABLE_AUTOSUSPEND ), +QUIRK_STRING_ENTRY (IGNORE_CTL_ERROR ), +QUIRK_STRING_ENTRY (DSD_RAW ), +QUIRK_STRING_ENTRY (SET_IFACE_FIRST ), +QUIRK_STRING_ENTRY (GENERIC_IMPLICIT_FB ), +QUIRK_STRING_ENTRY (SKIP_IMPLICIT_FB ), +QUIRK_STRING_ENTRY (IFACE_SKIP_CLOSE ), +QUIRK_STRING_ENTRY (FORCE_IFACE_RESET ), +QUIRK_STRING_ENTRY (FIXED_RATE ), +QUIRK_STRING_ENTRY (MIC_RES_16 ), +QUIRK_STRING_ENTRY (MIC_RES_384 ), +QUIRK_STRING_ENTRY (MIXER_PLAYBACK_MIN_MUTE ), +QUIRK_STRING_ENTRY (MIXER_CAPTURE_MIN_MUTE ), +NULL +}; + +const char *snd_usb_quirk_flag_find_name (unsigned long index) +{ +if (index >=ARRAY_SIZE (snd_usb_audio_quirk_flag_names)) +return NULL ; + +return snd_usb_audio_quirk_flag_names[index]; +} + +u32 snd_usb_quirk_flags_from_name (const char *name) +{ +int i; + +if (!name || !*name) +return 0 ; + +for (i =0 ; snd_usb_audio_quirk_flag_names[i]; i++) { +if (strcasecmp (name, snd_usb_audio_quirk_flag_names[i]) ==0 ) +return BIT_U32 (i); + } + +return 0 ; +} + +void snd_usb_apply_flag_dbg (const char *reason, +struct snd_usb_audio *chip, +unsigned long flag) +{ +unsigned long bit; + +for_each_set_bit (bit, &flag,BYTES_TO_BITS (sizeof (flag))) { +const char *name = snd_usb_audio_quirk_flag_names[bit]; + +if (name) +usb_audio_dbg (chip, +"From %s apply quirk flag %s for device %04x:%04x\n" , + reason, name,USB_ID_VENDOR (chip->usb_id), +USB_ID_PRODUCT (chip->usb_id)); +else +usb_audio_warn (chip, +"From %s apply unknown quirk flag 0x%lx for device %04x:%04x\n" , + reason, bit,USB_ID_VENDOR (chip->usb_id), +USB_ID_PRODUCT (chip->usb_id)); + } +} +void snd_usb_init_quirk_flags (struct snd_usb_audio *chip) {const struct usb_audio_quirk_flags_table *p;
@@ -2454,10 +2532,7 @@ void snd_usb_init_quirk_flags(struct snd_usb_audio *chip)if (chip->usb_id == p->id || (!USB_ID_PRODUCT (p->id) &&USB_ID_VENDOR (chip->usb_id) ==USB_ID_VENDOR (p->id))) { -usb_audio_dbg (chip, -"Set quirk_flags 0x%x for device %04x:%04x\n" , - p->flags,USB_ID_VENDOR (chip->usb_id), -USB_ID_PRODUCT (chip->usb_id)); +snd_usb_apply_flag_dbg ("builtin table" , chip, p->flags); chip->quirk_flags |= p->flags;return ; }
sound/usb/quirks.h +7 -0
@@ -48,6 +48,13 @@ void snd_usb_audioformat_attributes_quirk(struct snd_usb_audio *chip,struct audioformat *fp, int stream); +void snd_usb_apply_flag_dbg (const char *reason, +struct snd_usb_audio *chip, +unsigned long flag); +void snd_usb_init_quirk_flags (struct snd_usb_audio *chip); +const char *snd_usb_quirk_flag_find_name (unsigned long flag); +u32 snd_usb_quirk_flags_from_name (const char *name); +#endif /* __USBAUDIO_QUIRKS_H */
sound/usb/usbaudio.h +58 -26
89 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v6 2/4 ALSA: usb-audio: improve module param quirk_flags
In reply to Cryolitia PukNgaeIt accepts strings like VID:PID:quirk_flag_name1|quirk_flag_name2;... from now on, so that we can use it to debug USB audio devices more intuitive and flexible. The compatibility of previous form is kept.
--- sound/usb/card.c | 32 +++++++++++++++---- sound/usb/quirks.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++- sound/usb/quirks.h | 4 ++- 3 files changed, 118 insertions(+), 8 deletions(-)
sound/usb/card.c +26 -6
@@ -73,7 +73,7 @@ static bool lowlatency = true;static char *quirk_alias[SNDRV_CARDS ];static char *delayed_register[SNDRV_CARDS ];static bool implicit_fb[SNDRV_CARDS ]; -static unsigned int quirk_flags[SNDRV_CARDS ]; +static char *quirk_flags[SNDRV_CARDS ];bool snd_usb_use_vmalloc = true;bool snd_usb_skip_validation;
@@ -103,7 +103,7 @@ module_param_array(delayed_register, charp, NULL, 0444);MODULE_PARM_DESC (delayed_register,"Quirk for delayed registration, given by id:iface, e.g. 0123abcd:4." );module_param_array (implicit_fb, bool,NULL ,0444 );MODULE_PARM_DESC (implicit_fb,"Apply generic implicit feedback sync mode." ); -module_param_array (quirk_flags, uint,NULL ,0444 ); +module_param_array (quirk_flags, charp,NULL ,0444 );MODULE_PARM_DESC (quirk_flags,"Driver quirk bit flags." );module_param_named (use_vmalloc, snd_usb_use_vmalloc, bool,0444 );MODULE_PARM_DESC (use_vmalloc,"Use vmalloc for PCM intermediate buffers (default: yes)." );
@@ -692,6 +692,29 @@ static void usb_audio_make_longname(struct usb_device *dev, } } +static void snd_usb_init_quirk_flags (int idx,struct snd_usb_audio *chip) +{ +size_t i; + +/* old style option found: the position-based integer value */ +if (quirk_flags[idx] && + !kstrtou32 (quirk_flags[idx],0 , &chip->quirk_flags)) { +snd_usb_apply_flag_dbg ("module param" , chip, chip->quirk_flags); +return ; + } + +/* take the default quirk from the quirk table */ +snd_usb_init_quirk_flags_table (chip); + +/* add or correct quirk bits from options */ +for (i =0 ; i <ARRAY_SIZE (quirk_flags); i++) { +if (!quirk_flags[i] || !*quirk_flags[i]) +break ; + +snd_usb_init_quirk_flags_parse_string (chip, quirk_flags[i]); + } +} +/* * create a chip instance and set its names. */
@@ -750,10 +773,7 @@ static int snd_usb_audio_create(struct usb_interface *intf,INIT_LIST_HEAD (&chip->midi_v2_list);INIT_LIST_HEAD (&chip->mixer_list); -if (quirk_flags[idx]) - chip->quirk_flags = quirk_flags[idx]; -else -snd_usb_init_quirk_flags (chip); +snd_usb_init_quirk_flags (idx, chip); card->private_free = snd_usb_audio_free;
sound/usb/quirks.c +89 -1
99 lines not on this page · raw message
sound/usb/quirks.h +3 -1
@@ -52,7 +52,9 @@ void snd_usb_apply_flag_dbg(const char *reason,struct snd_usb_audio *chip, unsigned long flag); -void snd_usb_init_quirk_flags (struct snd_usb_audio *chip); +void snd_usb_init_quirk_flags_table (struct snd_usb_audio *chip); +void snd_usb_init_quirk_flags_parse_string (struct snd_usb_audio *chip, +const char *str);const char *snd_usb_quirk_flag_find_name (unsigned long flag);u32 snd_usb_quirk_flags_from_name (const char *name);
Signature · 1 line
2.51.0
PATCH v6 3/4 ALSA: usb-audio: make param quirk_flags change-able in runtime
In reply to Cryolitia PukNgaeChange its permision from 0644 to 0444, and add runtime processing.
Developers now can change it during sysfs, without rebooting, for debugging new buggy devices.
--- sound/usb/card.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-)
sound/usb/card.c +23 -2
@@ -103,13 +103,32 @@ module_param_array(delayed_register, charp, NULL, 0444);MODULE_PARM_DESC (delayed_register,"Quirk for delayed registration, given by id:iface, e.g. 0123abcd:4." );module_param_array (implicit_fb, bool,NULL ,0444 );MODULE_PARM_DESC (implicit_fb,"Apply generic implicit feedback sync mode." ); -module_param_array (quirk_flags, charp,NULL ,0444 ); -MODULE_PARM_DESC (quirk_flags,"Driver quirk bit flags." );module_param_named (use_vmalloc, snd_usb_use_vmalloc, bool,0444 );MODULE_PARM_DESC (use_vmalloc,"Use vmalloc for PCM intermediate buffers (default: yes)." );module_param_named (skip_validation, snd_usb_skip_validation, bool,0444 );MODULE_PARM_DESC (skip_validation,"Skip unit descriptor validation (default: no)." ); +/* protects quirk_flags */ +static DEFINE_MUTEX (quirk_flags_mutex); + +static int param_set_quirkp (const char *val, +const struct kernel_param *kp) +{ +guard (mutex)(&quirk_flags_mutex); +return param_set_charp (val, kp); +} + +static const struct kernel_param_ops param_ops_quirkp = { + .set = param_set_quirkp, + .get = param_get_charp, + .free = param_free_charp, +}; + +#define param_check_quirkp param_check_charp + +module_param_array (quirk_flags, quirkp,NULL ,0644 ); +MODULE_PARM_DESC (quirk_flags,"Add/modify USB audio quirks" ); +/* * we keep the snd_usb_audio_t instances by ourselves for merging * the all interfaces on the same card as one sound device.
@@ -696,6 +715,8 @@ static void snd_usb_init_quirk_flags(int idx, struct snd_usb_audio *chip) {size_t i; +guard (mutex)(&quirk_flags_mutex); +/* old style option found: the position-based integer value */ if (quirk_flags[idx] && !kstrtou32 (quirk_flags[idx],0 , &chip->quirk_flags)) {
Signature · 1 line
2.51.0
PATCH v6 4/4 ALSA: doc: improved docs about quirk_flags in snd-usb-audio
In reply to Cryolitia PukNgaeJust briefly described about the option's change and its new usage.
--- Documentation/sound/alsa-configuration.rst | 108 ++++++++++++++++++++--------- 1 file changed, 75 insertions(+), 33 deletions(-)
Documentation/sound/alsa-configuration.rst +75 -33
@@ -2297,39 +2297,81 @@ skip_validation of the unit descriptor instead of a driver probe error, so that we can check its details. quirk_flags - Contains the bit flags for various device specific workarounds. - Applied to the corresponding card index. - - * bit 0: Skip reading sample rate for devices - * bit 1: Create Media Controller API entries - * bit 2: Allow alignment on audio sub-slot at transfer - * bit 3: Add length specifier to transfers - * bit 4: Start playback stream at first in implement feedback mode - * bit 5: Skip clock selector setup - * bit 6: Ignore errors from clock source search - * bit 7: Indicates ITF-USB DSD based DACs - * bit 8: Add a delay of 20ms at each control message handling - * bit 9: Add a delay of 1-2ms at each control message handling - * bit 10: Add a delay of 5-6ms at each control message handling - * bit 11: Add a delay of 50ms at each interface setup - * bit 12: Perform sample rate validations at probe - * bit 13: Disable runtime PM autosuspend - * bit 14: Ignore errors for mixer access - * bit 15: Support generic DSD raw U32_BE format - * bit 16: Set up the interface at first like UAC1 - * bit 17: Apply the generic implicit feedback sync mode - * bit 18: Don't apply implicit feedback sync mode - * bit 19: Don't closed interface during setting sample rate - * bit 20: Force an interface reset whenever stopping & restarting - a stream - * bit 21: Do not set PCM rate (frequency) when only one rate is - available for the given endpoint. - * bit 22: Set the fixed resolution 16 for Mic Capture Volume - * bit 23: Set the fixed resolution 384 for Mic Capture Volume - * bit 24: Set minimum volume control value as mute for devices - where the lowest playback value represents muted state instead - of minimum audible volume - * bit 25: Be similar to bit 24 but for capture streams + The option provides a refined and flexible control for applying quirk + flags. It allows to specify the quirk flags for each device, and can + be modified dynamically via sysfs. + The old usage accepts an array of integers, each of which applies quirk + flags on the device in the order of probing. + E.g., ``quirk_flags=0x01,0x02`` applies get_sample_rate to the first + device, and share_media_device to the second device. + The new usage accepts a string in the format of + ``VID1:PID1:FLAGS1;VID2:PID2:FLAGS2;...``, where ``VIDx`` and ``PIDx`` + specify the device, and ``FLAGSx`` specify the flags to be applied. + ``VIDx`` and ``PIDx`` are 4-digit hexadecimal numbers, and can be + specified as ``*`` to match any value. ``FLAGSx`` can be a set of + flags given by name, separated by ``|``, or a hexadecimal number + representing the bit flags. The available flag names are listed below. + An exclamation mark can be prefixed to a flag name to negate the flag. + For example, ``1234:abcd:mixer_playback_min_mute|!ignore_ctl_error;*:*:0x01;`` + applies the ``mixer_playback_min_mute`` flag and clears the + ``ignore_ctl_error`` flag for the device 1234:abcd, and applies the + ``skip_sample_rate`` flag for all devices. + + * bit 0: ``get_sample_rate`` + Skip reading sample rate for devices + * bit 1: ``share_media_device`` + Create Media Controller API entries + * bit 2: ``align_transfer`` + Allow alignment on audio sub-slot at transfer + * bit 3: ``tx_length`` + Add length specifier to transfers + * bit 4: ``playback_first`` + Start playback stream at first in implement feedback mode + * bit 5: ``skip_clock_selector`` + Skip clock selector setup + * bit 6: ``ignore_clock_source`` + Ignore errors from clock source search + * bit 7: ``itf_usb_dsd_dac`` + Indicates ITF-USB DSD-based DACs + * bit 8: ``ctl_msg_delay`` + Add a delay of 20ms at each control message handling + * bit 9: ``ctl_msg_delay_1m`` + Add a delay of 1-2ms at each control message handling + * bit 10: ``ctl_msg_delay_5m`` + Add a delay of 5-6ms at each control message handling + * bit 11: ``iface_delay`` + Add a delay of 50ms at each interface setup + * bit 12: ``validate_rates`` + Perform sample rate validations at probe + * bit 13: ``disable_autosuspend`` + Disable runtime PM autosuspend + * bit 14: ``ignore_ctl_error`` + Ignore errors for mixer access + * bit 15: ``dsd_raw`` + Support generic DSD raw U32_BE format + * bit 16: ``set_iface_first`` + Set up the interface at first like UAC1 + * bit 17: ``generic_implicit_fb`` + Apply the generic implicit feedback sync mode + * bit 18: ``skip_implicit_fb`` + Don't apply implicit feedback sync mode + * bit 19: ``iface_skip_close`` + Don't close interface during setting sample rate + * bit 20: ``force_iface_reset`` + Force an interface reset whenever stopping & restarting a stream + * bit 21: ``fixed_rate`` + Do not set PCM rate (frequency) when only one rate is available + for the given endpoint + * bit 22: ``mic_res_16`` + Set the fixed resolution 16 for Mic Capture Volume + * bit 23: ``mic_res_384`` + Set the fixed resolution 384 for Mic Capture Volume + * bit 24: ``mixer_playback_min_mute`` + Set minimum volume control value as mute for devices where the + lowest playback value represents muted state instead of minimum + audible volume + * bit 25: ``mixer_capture_min_mute`` + Similar to bit 24 but for capture streams This module supports multiple devices, autoprobe and hotplugging.
Signature · 1 line
2.51.0
Quoted 28 lines · On Sun, 28 Sep 2025 05:07:58 +0200, Cryolitia PukNgae via B4 Relay wrote:
As an implementation of what has been discussed previously[1].
1. https://lore.kernel.org/all/[email protected]/
Signed-off-by Cryolitia PukNgae <[email protected]>---
Changes in v6:
- Apply review commens, details corrected and wording revised
- Link to v5: https://lore.kernel.org/r/[email protected]Changes in v5:
- Apply review comments. Thanks a lot, Takashi Iwai!
- Link to v4: https://lore.kernel.org/r/[email protected]Changes in v4:
- Split basic parse and dynamic change
- Drop usage of linked list
- Link to v3: https://lore.kernel.org/r/[email protected]Changes in v3:
- Instead of a new param, improve the existed one.
- Link to v2: https://lore.kernel.org/r/[email protected]Changes in v2:
- Cleaned up some internal rebase confusion, sorry for that
- Link to v1: https://lore.kernel.org/r/[email protected]
Quoted 6 lines
---
Cryolitia PukNgae (4):
ALSA: usb-audio: add two-way convert between name and bit for QUIRK_FLAG_*
ALSA: usb-audio: improve module paramquirk_flags
ALSA: usb-audio: make paramquirk_flagschange-able in runtime
ALSA: doc: improved docs aboutquirk_flagsin snd-usb-audio
Now applied all four patches. Thanks!
Takashi