Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ALSA: firewire: digi00x: 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-4-tiwai@suse.de

+42 -58
+12 -12
sound/firewire/digi00x/digi00x-midi.c
··· 16 16 if (err < 0) 17 17 return err; 18 18 19 - mutex_lock(&dg00x->mutex); 20 - err = snd_dg00x_stream_reserve_duplex(dg00x, 0, 0, 0); 21 - if (err >= 0) { 22 - ++dg00x->substreams_counter; 23 - err = snd_dg00x_stream_start_duplex(dg00x); 24 - if (err < 0) 25 - --dg00x->substreams_counter; 19 + scoped_guard(mutex, &dg00x->mutex) { 20 + err = snd_dg00x_stream_reserve_duplex(dg00x, 0, 0, 0); 21 + if (err >= 0) { 22 + ++dg00x->substreams_counter; 23 + err = snd_dg00x_stream_start_duplex(dg00x); 24 + if (err < 0) 25 + --dg00x->substreams_counter; 26 + } 26 27 } 27 - mutex_unlock(&dg00x->mutex); 28 28 if (err < 0) 29 29 snd_dg00x_stream_lock_release(dg00x); 30 30 ··· 35 35 { 36 36 struct snd_dg00x *dg00x = substream->rmidi->private_data; 37 37 38 - mutex_lock(&dg00x->mutex); 39 - --dg00x->substreams_counter; 40 - snd_dg00x_stream_stop_duplex(dg00x); 41 - mutex_unlock(&dg00x->mutex); 38 + scoped_guard(mutex, &dg00x->mutex) { 39 + --dg00x->substreams_counter; 40 + snd_dg00x_stream_stop_duplex(dg00x); 41 + } 42 42 43 43 snd_dg00x_stream_lock_release(dg00x); 44 44 return 0;
+29 -44
sound/firewire/digi00x/digi00x-pcm.c
··· 127 127 } 128 128 } 129 129 130 - mutex_lock(&dg00x->mutex); 130 + scoped_guard(mutex, &dg00x->mutex) { 131 + // When source of clock is not internal or any stream is reserved for 132 + // transmission of PCM frames, the available sampling rate is limited 133 + // at current one. 134 + if ((clock != SND_DG00X_CLOCK_INTERNAL) || 135 + (dg00x->substreams_counter > 0 && d->events_per_period > 0)) { 136 + unsigned int frames_per_period = d->events_per_period; 137 + unsigned int frames_per_buffer = d->events_per_buffer; 138 + unsigned int rate; 131 139 132 - // When source of clock is not internal or any stream is reserved for 133 - // transmission of PCM frames, the available sampling rate is limited 134 - // at current one. 135 - if ((clock != SND_DG00X_CLOCK_INTERNAL) || 136 - (dg00x->substreams_counter > 0 && d->events_per_period > 0)) { 137 - unsigned int frames_per_period = d->events_per_period; 138 - unsigned int frames_per_buffer = d->events_per_buffer; 139 - unsigned int rate; 140 - 141 - err = snd_dg00x_stream_get_external_rate(dg00x, &rate); 142 - if (err < 0) { 143 - mutex_unlock(&dg00x->mutex); 144 - goto err_locked; 145 - } 146 - substream->runtime->hw.rate_min = rate; 147 - substream->runtime->hw.rate_max = rate; 148 - 149 - if (frames_per_period > 0) { 150 - err = snd_pcm_hw_constraint_minmax(substream->runtime, 151 - SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 152 - frames_per_period, frames_per_period); 153 - if (err < 0) { 154 - mutex_unlock(&dg00x->mutex); 140 + err = snd_dg00x_stream_get_external_rate(dg00x, &rate); 141 + if (err < 0) 155 142 goto err_locked; 156 - } 143 + substream->runtime->hw.rate_min = rate; 144 + substream->runtime->hw.rate_max = rate; 157 145 158 - err = snd_pcm_hw_constraint_minmax(substream->runtime, 159 - SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 160 - frames_per_buffer, frames_per_buffer); 161 - if (err < 0) { 162 - mutex_unlock(&dg00x->mutex); 163 - goto err_locked; 146 + if (frames_per_period > 0) { 147 + err = snd_pcm_hw_constraint_minmax(substream->runtime, 148 + SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 149 + frames_per_period, frames_per_period); 150 + if (err < 0) 151 + goto err_locked; 152 + 153 + err = snd_pcm_hw_constraint_minmax(substream->runtime, 154 + SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 155 + frames_per_buffer, frames_per_buffer); 156 + if (err < 0) 157 + goto err_locked; 164 158 } 165 159 } 166 160 } 167 - 168 - mutex_unlock(&dg00x->mutex); 169 161 170 162 snd_pcm_set_sync(substream); 171 163 ··· 187 195 unsigned int frames_per_period = params_period_size(hw_params); 188 196 unsigned int frames_per_buffer = params_buffer_size(hw_params); 189 197 190 - mutex_lock(&dg00x->mutex); 198 + guard(mutex)(&dg00x->mutex); 191 199 err = snd_dg00x_stream_reserve_duplex(dg00x, rate, 192 200 frames_per_period, frames_per_buffer); 193 201 if (err >= 0) 194 202 ++dg00x->substreams_counter; 195 - mutex_unlock(&dg00x->mutex); 196 203 } 197 204 198 205 return err; ··· 201 210 { 202 211 struct snd_dg00x *dg00x = substream->private_data; 203 212 204 - mutex_lock(&dg00x->mutex); 213 + guard(mutex)(&dg00x->mutex); 205 214 206 215 if (substream->runtime->state != SNDRV_PCM_STATE_OPEN) 207 216 --dg00x->substreams_counter; 208 217 209 218 snd_dg00x_stream_stop_duplex(dg00x); 210 - 211 - mutex_unlock(&dg00x->mutex); 212 219 213 220 return 0; 214 221 } ··· 216 227 struct snd_dg00x *dg00x = substream->private_data; 217 228 int err; 218 229 219 - mutex_lock(&dg00x->mutex); 230 + guard(mutex)(&dg00x->mutex); 220 231 221 232 err = snd_dg00x_stream_start_duplex(dg00x); 222 233 if (err >= 0) 223 234 amdtp_stream_pcm_prepare(&dg00x->tx_stream); 224 - 225 - mutex_unlock(&dg00x->mutex); 226 235 227 236 return err; 228 237 } ··· 230 243 struct snd_dg00x *dg00x = substream->private_data; 231 244 int err; 232 245 233 - mutex_lock(&dg00x->mutex); 246 + guard(mutex)(&dg00x->mutex); 234 247 235 248 err = snd_dg00x_stream_start_duplex(dg00x); 236 249 if (err >= 0) { 237 250 amdtp_stream_pcm_prepare(&dg00x->rx_stream); 238 251 amdtp_dot_reset(&dg00x->rx_stream); 239 252 } 240 - 241 - mutex_unlock(&dg00x->mutex); 242 253 243 254 return err; 244 255 }
+1 -2
sound/firewire/digi00x/digi00x.c
··· 116 116 117 117 snd_dg00x_transaction_reregister(dg00x); 118 118 119 - mutex_lock(&dg00x->mutex); 119 + guard(mutex)(&dg00x->mutex); 120 120 snd_dg00x_stream_update_duplex(dg00x); 121 - mutex_unlock(&dg00x->mutex); 122 121 } 123 122 124 123 static void snd_dg00x_remove(struct fw_unit *unit)