]> www.infradead.org Git - users/hch/misc.git/commitdiff
ALSA: firewire: fireface: Use guard() for mutex locks
authorTakashi Iwai <tiwai@suse.de>
Thu, 28 Aug 2025 13:27:12 +0000 (15:27 +0200)
committerTakashi Iwai <tiwai@suse.de>
Sat, 30 Aug 2025 08:02:21 +0000 (10:02 +0200)
Replace the manual mutex lock/unlock pairs with guard() for code
simplification.

Only code refactoring, and no behavior change.

Reviewed-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
Link: https://patch.msgid.link/20250828132802.9032-9-tiwai@suse.de
sound/firewire/fireface/ff-pcm.c

index 63457d24a288d8ffd0b8385dc8bd8c2bc8ad87e8..7ad8204fbfe8bc92c7d1b385cc151d906f1d9426 100644 (file)
@@ -156,56 +156,49 @@ static int pcm_open(struct snd_pcm_substream *substream)
        if (err < 0)
                goto release_lock;
 
-       mutex_lock(&ff->mutex);
-
-       // When source of clock is not internal or any stream is reserved for
-       // transmission of PCM frames, the available sampling rate is limited
-       // at current one.
-       if (src != SND_FF_CLOCK_SRC_INTERNAL) {
-               for (i = 0; i < CIP_SFC_COUNT; ++i) {
-                       if (amdtp_rate_table[i] == rate)
-                               break;
-               }
-
-               // The unit is configured at sampling frequency which packet
-               // streaming engine can't support.
-               if (i >= CIP_SFC_COUNT) {
-                       mutex_unlock(&ff->mutex);
-                       err = -EIO;
-                       goto release_lock;
-               }
-
-               substream->runtime->hw.rate_min = rate;
-               substream->runtime->hw.rate_max = rate;
-       } else {
-               if (ff->substreams_counter > 0) {
-                       unsigned int frames_per_period = d->events_per_period;
-                       unsigned int frames_per_buffer = d->events_per_buffer;
-
-                       rate = amdtp_rate_table[ff->rx_stream.sfc];
-                       substream->runtime->hw.rate_min = rate;
-                       substream->runtime->hw.rate_max = rate;
+       scoped_guard(mutex, &ff->mutex) {
+               // When source of clock is not internal or any stream is reserved for
+               // transmission of PCM frames, the available sampling rate is limited
+               // at current one.
+               if (src != SND_FF_CLOCK_SRC_INTERNAL) {
+                       for (i = 0; i < CIP_SFC_COUNT; ++i) {
+                               if (amdtp_rate_table[i] == rate)
+                                       break;
+                       }
 
-                       err = snd_pcm_hw_constraint_minmax(substream->runtime,
-                                       SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
-                                       frames_per_period, frames_per_period);
-                       if (err < 0) {
-                               mutex_unlock(&ff->mutex);
+                       // The unit is configured at sampling frequency which packet
+                       // streaming engine can't support.
+                       if (i >= CIP_SFC_COUNT) {
+                               err = -EIO;
                                goto release_lock;
                        }
 
-                       err = snd_pcm_hw_constraint_minmax(substream->runtime,
-                                       SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
-                                       frames_per_buffer, frames_per_buffer);
-                       if (err < 0) {
-                               mutex_unlock(&ff->mutex);
-                               goto release_lock;
+                       substream->runtime->hw.rate_min = rate;
+                       substream->runtime->hw.rate_max = rate;
+               } else {
+                       if (ff->substreams_counter > 0) {
+                               unsigned int frames_per_period = d->events_per_period;
+                               unsigned int frames_per_buffer = d->events_per_buffer;
+
+                               rate = amdtp_rate_table[ff->rx_stream.sfc];
+                               substream->runtime->hw.rate_min = rate;
+                               substream->runtime->hw.rate_max = rate;
+
+                               err = snd_pcm_hw_constraint_minmax(substream->runtime,
+                                                                  SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
+                                                                  frames_per_period, frames_per_period);
+                               if (err < 0)
+                                       goto release_lock;
+
+                               err = snd_pcm_hw_constraint_minmax(substream->runtime,
+                                                                  SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
+                                                                  frames_per_buffer, frames_per_buffer);
+                               if (err < 0)
+                                       goto release_lock;
                        }
                }
        }
 
-       mutex_unlock(&ff->mutex);
-
        snd_pcm_set_sync(substream);
 
        return 0;
@@ -235,12 +228,11 @@ static int pcm_hw_params(struct snd_pcm_substream *substream,
                unsigned int frames_per_period = params_period_size(hw_params);
                unsigned int frames_per_buffer = params_buffer_size(hw_params);
 
-               mutex_lock(&ff->mutex);
+               guard(mutex)(&ff->mutex);
                err = snd_ff_stream_reserve_duplex(ff, rate, frames_per_period,
                                                   frames_per_buffer);
                if (err >= 0)
                        ++ff->substreams_counter;
-               mutex_unlock(&ff->mutex);
        }
 
        return err;
@@ -250,15 +242,13 @@ static int pcm_hw_free(struct snd_pcm_substream *substream)
 {
        struct snd_ff *ff = substream->private_data;
 
-       mutex_lock(&ff->mutex);
+       guard(mutex)(&ff->mutex);
 
        if (substream->runtime->state != SNDRV_PCM_STATE_OPEN)
                --ff->substreams_counter;
 
        snd_ff_stream_stop_duplex(ff);
 
-       mutex_unlock(&ff->mutex);
-
        return 0;
 }
 
@@ -268,14 +258,12 @@ static int pcm_capture_prepare(struct snd_pcm_substream *substream)
        struct snd_pcm_runtime *runtime = substream->runtime;
        int err;
 
-       mutex_lock(&ff->mutex);
+       guard(mutex)(&ff->mutex);
 
        err = snd_ff_stream_start_duplex(ff, runtime->rate);
        if (err >= 0)
                amdtp_stream_pcm_prepare(&ff->tx_stream);
 
-       mutex_unlock(&ff->mutex);
-
        return err;
 }
 
@@ -285,14 +273,12 @@ static int pcm_playback_prepare(struct snd_pcm_substream *substream)
        struct snd_pcm_runtime *runtime = substream->runtime;
        int err;
 
-       mutex_lock(&ff->mutex);
+       guard(mutex)(&ff->mutex);
 
        err = snd_ff_stream_start_duplex(ff, runtime->rate);
        if (err >= 0)
                amdtp_stream_pcm_prepare(&ff->rx_stream);
 
-       mutex_unlock(&ff->mutex);
-
        return err;
 }