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

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

+57 -74
+17 -22
sound/firewire/fireworks/fireworks.c
··· 188 188 { 189 189 struct snd_efw *efw = card->private_data; 190 190 191 - mutex_lock(&devices_mutex); 192 - clear_bit(efw->card_index, devices_used); 193 - mutex_unlock(&devices_mutex); 191 + scoped_guard(mutex, &devices_mutex) { 192 + clear_bit(efw->card_index, devices_used); 193 + } 194 194 195 195 snd_efw_stream_destroy_duplex(efw); 196 196 snd_efw_transaction_remove_instance(efw); ··· 207 207 int err; 208 208 209 209 // check registered cards. 210 - mutex_lock(&devices_mutex); 211 - for (card_index = 0; card_index < SNDRV_CARDS; ++card_index) { 212 - if (!test_bit(card_index, devices_used) && enable[card_index]) 213 - break; 214 - } 215 - if (card_index >= SNDRV_CARDS) { 216 - mutex_unlock(&devices_mutex); 217 - return -ENOENT; 218 - } 210 + scoped_guard(mutex, &devices_mutex) { 211 + for (card_index = 0; card_index < SNDRV_CARDS; ++card_index) { 212 + if (!test_bit(card_index, devices_used) && enable[card_index]) 213 + break; 214 + } 215 + if (card_index >= SNDRV_CARDS) 216 + return -ENOENT; 219 217 220 - err = snd_card_new(&unit->device, index[card_index], id[card_index], THIS_MODULE, 221 - sizeof(*efw), &card); 222 - if (err < 0) { 223 - mutex_unlock(&devices_mutex); 224 - return err; 218 + err = snd_card_new(&unit->device, index[card_index], id[card_index], THIS_MODULE, 219 + sizeof(*efw), &card); 220 + if (err < 0) 221 + return err; 222 + card->private_free = efw_card_free; 223 + set_bit(card_index, devices_used); 225 224 } 226 - card->private_free = efw_card_free; 227 - set_bit(card_index, devices_used); 228 - mutex_unlock(&devices_mutex); 229 225 230 226 efw = card->private_data; 231 227 efw->unit = fw_unit_get(unit); ··· 283 287 284 288 snd_efw_transaction_bus_reset(efw->unit); 285 289 286 - mutex_lock(&efw->mutex); 290 + guard(mutex)(&efw->mutex); 287 291 snd_efw_stream_update_duplex(efw); 288 - mutex_unlock(&efw->mutex); 289 292 } 290 293 291 294 static void efw_remove(struct fw_unit *unit)
+13 -14
sound/firewire/fireworks/fireworks_midi.c
··· 14 14 15 15 err = snd_efw_stream_lock_try(efw); 16 16 if (err < 0) 17 - goto end; 17 + return err; 18 18 19 - mutex_lock(&efw->mutex); 20 - err = snd_efw_stream_reserve_duplex(efw, 0, 0, 0); 21 - if (err >= 0) { 22 - ++efw->substreams_counter; 23 - err = snd_efw_stream_start_duplex(efw); 24 - if (err < 0) 25 - --efw->substreams_counter; 19 + scoped_guard(mutex, &efw->mutex) { 20 + err = snd_efw_stream_reserve_duplex(efw, 0, 0, 0); 21 + if (err >= 0) { 22 + ++efw->substreams_counter; 23 + err = snd_efw_stream_start_duplex(efw); 24 + if (err < 0) 25 + --efw->substreams_counter; 26 + } 26 27 } 27 - mutex_unlock(&efw->mutex); 28 28 if (err < 0) 29 29 snd_efw_stream_lock_release(efw); 30 - end: 31 30 return err; 32 31 } 33 32 ··· 34 35 { 35 36 struct snd_efw *efw = substream->rmidi->private_data; 36 37 37 - mutex_lock(&efw->mutex); 38 - --efw->substreams_counter; 39 - snd_efw_stream_stop_duplex(efw); 40 - mutex_unlock(&efw->mutex); 38 + scoped_guard(mutex, &efw->mutex) { 39 + --efw->substreams_counter; 40 + snd_efw_stream_stop_duplex(efw); 41 + } 41 42 42 43 snd_efw_stream_lock_release(efw); 43 44 return 0;
+27 -38
sound/firewire/fireworks/fireworks_pcm.c
··· 189 189 if (err < 0) 190 190 goto err_locked; 191 191 192 - mutex_lock(&efw->mutex); 192 + scoped_guard(mutex, &efw->mutex) { 193 + // When source of clock is not internal or any stream is reserved for 194 + // transmission of PCM frames, the available sampling rate is limited 195 + // at current one. 196 + if ((clock_source != SND_EFW_CLOCK_SOURCE_INTERNAL) || 197 + (efw->substreams_counter > 0 && d->events_per_period > 0)) { 198 + unsigned int frames_per_period = d->events_per_period; 199 + unsigned int frames_per_buffer = d->events_per_buffer; 200 + unsigned int sampling_rate; 193 201 194 - // When source of clock is not internal or any stream is reserved for 195 - // transmission of PCM frames, the available sampling rate is limited 196 - // at current one. 197 - if ((clock_source != SND_EFW_CLOCK_SOURCE_INTERNAL) || 198 - (efw->substreams_counter > 0 && d->events_per_period > 0)) { 199 - unsigned int frames_per_period = d->events_per_period; 200 - unsigned int frames_per_buffer = d->events_per_buffer; 201 - unsigned int sampling_rate; 202 - 203 - err = snd_efw_command_get_sampling_rate(efw, &sampling_rate); 204 - if (err < 0) { 205 - mutex_unlock(&efw->mutex); 206 - goto err_locked; 207 - } 208 - substream->runtime->hw.rate_min = sampling_rate; 209 - substream->runtime->hw.rate_max = sampling_rate; 210 - 211 - if (frames_per_period > 0) { 212 - err = snd_pcm_hw_constraint_minmax(substream->runtime, 213 - SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 214 - frames_per_period, frames_per_period); 215 - if (err < 0) { 216 - mutex_unlock(&efw->mutex); 202 + err = snd_efw_command_get_sampling_rate(efw, &sampling_rate); 203 + if (err < 0) 217 204 goto err_locked; 218 - } 205 + substream->runtime->hw.rate_min = sampling_rate; 206 + substream->runtime->hw.rate_max = sampling_rate; 219 207 220 - err = snd_pcm_hw_constraint_minmax(substream->runtime, 221 - SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 222 - frames_per_buffer, frames_per_buffer); 223 - if (err < 0) { 224 - mutex_unlock(&efw->mutex); 225 - goto err_locked; 208 + if (frames_per_period > 0) { 209 + err = snd_pcm_hw_constraint_minmax(substream->runtime, 210 + SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 211 + frames_per_period, frames_per_period); 212 + if (err < 0) 213 + goto err_locked; 214 + 215 + err = snd_pcm_hw_constraint_minmax(substream->runtime, 216 + SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 217 + frames_per_buffer, frames_per_buffer); 218 + if (err < 0) 219 + goto err_locked; 226 220 } 227 221 } 228 222 } 229 - 230 - mutex_unlock(&efw->mutex); 231 223 232 224 snd_pcm_set_sync(substream); 233 225 ··· 247 255 unsigned int frames_per_period = params_period_size(hw_params); 248 256 unsigned int frames_per_buffer = params_buffer_size(hw_params); 249 257 250 - mutex_lock(&efw->mutex); 258 + guard(mutex)(&efw->mutex); 251 259 err = snd_efw_stream_reserve_duplex(efw, rate, 252 260 frames_per_period, frames_per_buffer); 253 261 if (err >= 0) 254 262 ++efw->substreams_counter; 255 - mutex_unlock(&efw->mutex); 256 263 } 257 264 258 265 return err; ··· 261 270 { 262 271 struct snd_efw *efw = substream->private_data; 263 272 264 - mutex_lock(&efw->mutex); 273 + guard(mutex)(&efw->mutex); 265 274 266 275 if (substream->runtime->state != SNDRV_PCM_STATE_OPEN) 267 276 --efw->substreams_counter; 268 277 269 278 snd_efw_stream_stop_duplex(efw); 270 - 271 - mutex_unlock(&efw->mutex); 272 279 273 280 return 0; 274 281 }