]> www.infradead.org Git - users/hch/misc.git/commitdiff
ALSA: firewire: motu: Use guard() for mutex locks
authorTakashi Iwai <tiwai@suse.de>
Thu, 28 Aug 2025 13:27:09 +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-6-tiwai@suse.de
sound/firewire/motu/motu-midi.c
sound/firewire/motu/motu-pcm.c

index eebc7e790ee2ab682ea024d6b82723252f995bd4..18c5734e999fb2d941c990c235291b954a06cb98 100644 (file)
@@ -15,18 +15,16 @@ static int midi_open(struct snd_rawmidi_substream *substream)
        if (err < 0)
                return err;
 
-       mutex_lock(&motu->mutex);
-
-       err = snd_motu_stream_reserve_duplex(motu, 0, 0, 0);
-       if (err >= 0) {
-               ++motu->substreams_counter;
-               err = snd_motu_stream_start_duplex(motu);
-               if (err < 0)
-                       --motu->substreams_counter;
+       scoped_guard(mutex, &motu->mutex) {
+               err = snd_motu_stream_reserve_duplex(motu, 0, 0, 0);
+               if (err >= 0) {
+                       ++motu->substreams_counter;
+                       err = snd_motu_stream_start_duplex(motu);
+                       if (err < 0)
+                               --motu->substreams_counter;
+               }
        }
 
-       mutex_unlock(&motu->mutex);
-
        if (err < 0)
                snd_motu_stream_lock_release(motu);
 
@@ -37,12 +35,10 @@ static int midi_close(struct snd_rawmidi_substream *substream)
 {
        struct snd_motu *motu = substream->rmidi->private_data;
 
-       mutex_lock(&motu->mutex);
-
-       --motu->substreams_counter;
-       snd_motu_stream_stop_duplex(motu);
-
-       mutex_unlock(&motu->mutex);
+       scoped_guard(mutex, &motu->mutex) {
+               --motu->substreams_counter;
+               snd_motu_stream_stop_duplex(motu);
+       }
 
        snd_motu_stream_lock_release(motu);
        return 0;
index 7b4d476af348f3f23ea7c503eea3ca8078586971..600c571edf02249998cbaa54577c55c4c8ec14df 100644 (file)
@@ -138,59 +138,56 @@ static int pcm_open(struct snd_pcm_substream *substream)
        if (err < 0)
                return err;
 
-       mutex_lock(&motu->mutex);
-
-       err = snd_motu_stream_cache_packet_formats(motu);
-       if (err < 0)
-               goto err_locked;
-
-       err = init_hw_info(motu, substream);
-       if (err < 0)
-               goto err_locked;
+       scoped_guard(mutex, &motu->mutex) {
+               err = snd_motu_stream_cache_packet_formats(motu);
+               if (err < 0)
+                       goto err_locked;
 
-       err = snd_motu_protocol_get_clock_source(motu, &src);
-       if (err < 0)
-               goto err_locked;
-
-       // 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_MOTU_CLOCK_SOURCE_INTERNAL &&
-            src != SND_MOTU_CLOCK_SOURCE_SPH) ||
-           (motu->substreams_counter > 0 && d->events_per_period > 0)) {
-               unsigned int frames_per_period = d->events_per_period;
-               unsigned int frames_per_buffer = d->events_per_buffer;
-               unsigned int rate;
-
-               err = snd_motu_protocol_get_clock_rate(motu, &rate);
+               err = init_hw_info(motu, substream);
                if (err < 0)
                        goto err_locked;
 
-               substream->runtime->hw.rate_min = rate;
-               substream->runtime->hw.rate_max = rate;
+               err = snd_motu_protocol_get_clock_source(motu, &src);
+               if (err < 0)
+                       goto err_locked;
 
-               if (frames_per_period > 0) {
-                       err = snd_pcm_hw_constraint_minmax(substream->runtime,
-                                       SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
-                                       frames_per_period, frames_per_period);
+               // 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_MOTU_CLOCK_SOURCE_INTERNAL &&
+                    src != SND_MOTU_CLOCK_SOURCE_SPH) ||
+                   (motu->substreams_counter > 0 && d->events_per_period > 0)) {
+                       unsigned int frames_per_period = d->events_per_period;
+                       unsigned int frames_per_buffer = d->events_per_buffer;
+                       unsigned int rate;
+
+                       err = snd_motu_protocol_get_clock_rate(motu, &rate);
                        if (err < 0)
                                goto err_locked;
 
-                       err = snd_pcm_hw_constraint_minmax(substream->runtime,
-                                       SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
-                                       frames_per_buffer, frames_per_buffer);
-                       if (err < 0)
-                               goto err_locked;
+                       substream->runtime->hw.rate_min = rate;
+                       substream->runtime->hw.rate_max = rate;
+
+                       if (frames_per_period > 0) {
+                               err = snd_pcm_hw_constraint_minmax(substream->runtime,
+                                                                  SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
+                                                                  frames_per_period, frames_per_period);
+                               if (err < 0)
+                                       goto err_locked;
+
+                               err = snd_pcm_hw_constraint_minmax(substream->runtime,
+                                                                  SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
+                                                                  frames_per_buffer, frames_per_buffer);
+                               if (err < 0)
+                                       goto err_locked;
+                       }
                }
        }
 
        snd_pcm_set_sync(substream);
 
-       mutex_unlock(&motu->mutex);
-
        return 0;
 err_locked:
-       mutex_unlock(&motu->mutex);
        snd_motu_stream_lock_release(motu);
        return err;
 }
@@ -215,12 +212,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(&motu->mutex);
+               guard(mutex)(&motu->mutex);
                err = snd_motu_stream_reserve_duplex(motu, rate,
                                        frames_per_period, frames_per_buffer);
                if (err >= 0)
                        ++motu->substreams_counter;
-               mutex_unlock(&motu->mutex);
        }
 
        return err;
@@ -230,15 +226,13 @@ static int pcm_hw_free(struct snd_pcm_substream *substream)
 {
        struct snd_motu *motu = substream->private_data;
 
-       mutex_lock(&motu->mutex);
+       guard(mutex)(&motu->mutex);
 
        if (substream->runtime->state != SNDRV_PCM_STATE_OPEN)
                --motu->substreams_counter;
 
        snd_motu_stream_stop_duplex(motu);
 
-       mutex_unlock(&motu->mutex);
-
        return 0;
 }
 
@@ -247,9 +241,9 @@ static int capture_prepare(struct snd_pcm_substream *substream)
        struct snd_motu *motu = substream->private_data;
        int err;
 
-       mutex_lock(&motu->mutex);
-       err = snd_motu_stream_start_duplex(motu);
-       mutex_unlock(&motu->mutex);
+       scoped_guard(mutex, &motu->mutex) {
+               err = snd_motu_stream_start_duplex(motu);
+       }
        if (err >= 0)
                amdtp_stream_pcm_prepare(&motu->tx_stream);
 
@@ -260,9 +254,9 @@ static int playback_prepare(struct snd_pcm_substream *substream)
        struct snd_motu *motu = substream->private_data;
        int err;
 
-       mutex_lock(&motu->mutex);
-       err = snd_motu_stream_start_duplex(motu);
-       mutex_unlock(&motu->mutex);
+       scoped_guard(mutex, &motu->mutex) {
+               err = snd_motu_stream_start_duplex(motu);
+       }
        if (err >= 0)
                amdtp_stream_pcm_prepare(&motu->rx_stream);