ALSA: firewire: motu: Use guard() for mutex locks

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
This commit is contained in:
Takashi Iwai 2025-08-28 15:27:09 +02:00
parent 760c91a25a
commit b9ac651964
2 changed files with 56 additions and 66 deletions

View 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;

View 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;
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);
scoped_guard(mutex, &motu->mutex) {
err = snd_motu_stream_cache_packet_formats(motu);
if (err < 0)
goto err_locked;
substream->runtime->hw.rate_min = rate;
substream->runtime->hw.rate_max = rate;
err = init_hw_info(motu, substream);
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);
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);
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);