]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
ALSA: usb-audio: Add helper function for volume range checks
authorRong Zhang <i@rong.moe>
Tue, 3 Mar 2026 19:47:57 +0000 (03:47 +0800)
committerTakashi Iwai <tiwai@suse.de>
Wed, 4 Mar 2026 11:05:57 +0000 (12:05 +0100)
When a potentially insane volume range is found, the volume control
parameters will be printed in WARN level instead of DEBUG level.
Currently, it's done by emitting a open-coded usb_audio_warn() in the
corresponding check.

The following changes are about to add more checks against volumen
ranges. As the first step, extract the current check logic into a helper
function to improve readability.

No functional change intended.

Signed-off-by: Rong Zhang <i@rong.moe>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
Link: https://patch.msgid.link/20260303194805.266158-3-i@rong.moe
sound/usb/mixer.c

index df0d3df9c7ece33c6bb6e3b38f95a56f544d460b..f52ca0d7e6653c405232301bca96a415946c159d 100644 (file)
@@ -1660,6 +1660,27 @@ static const struct usb_feature_control_info *get_feature_control_info(int contr
        return NULL;
 }
 
+static bool check_insane_volume_range(struct usb_mixer_interface *mixer,
+                                     struct snd_kcontrol *kctl,
+                                     struct usb_mixer_elem_info *cval)
+{
+       int range = (cval->max - cval->min) / cval->res;
+
+       /*
+        * Are there devices with volume range more than 255? I use a bit more
+        * to be sure. 384 is a resolution magic number found on Logitech
+        * devices. It will definitively catch all buggy Logitech devices.
+        */
+       if (range > 384) {
+               usb_audio_warn(mixer->chip,
+                              "Warning! Unlikely big volume range (=%u), cval->res is probably wrong.",
+                              range);
+               return true;
+       }
+
+       return false;
+}
+
 static void __build_feature_ctl(struct usb_mixer_interface *mixer,
                                const struct usbmix_name_map *imap,
                                unsigned int ctl_mask, int control,
@@ -1673,7 +1694,6 @@ static void __build_feature_ctl(struct usb_mixer_interface *mixer,
        struct snd_kcontrol *kctl;
        struct usb_mixer_elem_info *cval;
        const struct usbmix_name_map *map;
-       unsigned int range;
 
        if (control == UAC_FU_GRAPHIC_EQUALIZER) {
                /* FIXME: not supported yet */
@@ -1811,25 +1831,16 @@ static void __build_feature_ctl(struct usb_mixer_interface *mixer,
 
        snd_usb_mixer_fu_apply_quirk(mixer, cval, unitid, kctl);
 
-       range = (cval->max - cval->min) / cval->res;
-       /*
-        * Are there devices with volume range more than 255? I use a bit more
-        * to be sure. 384 is a resolution magic number found on Logitech
-        * devices. It will definitively catch all buggy Logitech devices.
-        */
-       if (range > 384) {
-               usb_audio_warn(mixer->chip,
-                              "Warning! Unlikely big volume range (=%u), cval->res is probably wrong.",
-                              range);
-               usb_audio_warn(mixer->chip,
-                              "[%d] FU [%s] ch = %d, val = %d/%d/%d",
+       if (check_insane_volume_range(mixer, kctl, cval)) {
+               usb_audio_warn(mixer->chip, "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
                               cval->head.id, kctl->id.name, cval->channels,
                               cval->min, cval->max, cval->res);
+       } else {
+               usb_audio_dbg(mixer->chip, "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
+                             cval->head.id, kctl->id.name, cval->channels,
+                             cval->min, cval->max, cval->res);
        }
 
-       usb_audio_dbg(mixer->chip, "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
-                     cval->head.id, kctl->id.name, cval->channels,
-                     cval->min, cval->max, cval->res);
        snd_usb_mixer_add_control(&cval->head, kctl);
 }