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

ALSA: ua101: Use guard() for mutex locks

Replace the manual mutex lock/unlock pairs with guard() for code
simplification.

Only code refactoring, and no behavior change.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
Link: https://patch.msgid.link/20250829150724.6886-2-tiwai@suse.de

+34 -51
+34 -51
sound/usb/misc/ua101.c
··· 658 658 DIV_ROUND_CLOSEST(ua->rate, ua->packets_per_second); 659 659 substream->runtime->delay = substream->runtime->hw.fifo_size; 660 660 661 - mutex_lock(&ua->mutex); 661 + guard(mutex)(&ua->mutex); 662 662 err = start_usb_capture(ua); 663 663 if (err >= 0) 664 664 set_bit(ALSA_CAPTURE_OPEN, &ua->states); 665 - mutex_unlock(&ua->mutex); 666 665 return err; 667 666 } 668 667 ··· 678 679 DIV_ROUND_CLOSEST(ua->rate * ua->playback.queue_length, 679 680 ua->packets_per_second); 680 681 681 - mutex_lock(&ua->mutex); 682 + guard(mutex)(&ua->mutex); 682 683 err = start_usb_capture(ua); 683 684 if (err < 0) 684 - goto error; 685 + return err; 685 686 err = start_usb_playback(ua); 686 687 if (err < 0) { 687 688 if (!test_bit(ALSA_CAPTURE_OPEN, &ua->states)) 688 689 stop_usb_capture(ua); 689 - goto error; 690 + return err; 690 691 } 691 692 set_bit(ALSA_PLAYBACK_OPEN, &ua->states); 692 - error: 693 - mutex_unlock(&ua->mutex); 694 - return err; 693 + return 0; 695 694 } 696 695 697 696 static int capture_pcm_close(struct snd_pcm_substream *substream) 698 697 { 699 698 struct ua101 *ua = substream->private_data; 700 699 701 - mutex_lock(&ua->mutex); 700 + guard(mutex)(&ua->mutex); 702 701 clear_bit(ALSA_CAPTURE_OPEN, &ua->states); 703 702 if (!test_bit(ALSA_PLAYBACK_OPEN, &ua->states)) 704 703 stop_usb_capture(ua); 705 - mutex_unlock(&ua->mutex); 706 704 return 0; 707 705 } 708 706 ··· 707 711 { 708 712 struct ua101 *ua = substream->private_data; 709 713 710 - mutex_lock(&ua->mutex); 714 + guard(mutex)(&ua->mutex); 711 715 stop_usb_playback(ua); 712 716 clear_bit(ALSA_PLAYBACK_OPEN, &ua->states); 713 717 if (!test_bit(ALSA_CAPTURE_OPEN, &ua->states)) 714 718 stop_usb_capture(ua); 715 - mutex_unlock(&ua->mutex); 716 719 return 0; 717 720 } 718 721 ··· 719 724 struct snd_pcm_hw_params *hw_params) 720 725 { 721 726 struct ua101 *ua = substream->private_data; 722 - int err; 723 727 724 - mutex_lock(&ua->mutex); 725 - err = start_usb_capture(ua); 726 - mutex_unlock(&ua->mutex); 727 - return err; 728 + guard(mutex)(&ua->mutex); 729 + return start_usb_capture(ua); 728 730 } 729 731 730 732 static int playback_pcm_hw_params(struct snd_pcm_substream *substream, ··· 730 738 struct ua101 *ua = substream->private_data; 731 739 int err; 732 740 733 - mutex_lock(&ua->mutex); 741 + guard(mutex)(&ua->mutex); 734 742 err = start_usb_capture(ua); 735 743 if (err >= 0) 736 744 err = start_usb_playback(ua); 737 - mutex_unlock(&ua->mutex); 738 745 return err; 739 746 } 740 747 ··· 742 751 struct ua101 *ua = substream->private_data; 743 752 int err; 744 753 745 - mutex_lock(&ua->mutex); 746 - err = start_usb_capture(ua); 747 - mutex_unlock(&ua->mutex); 754 + scoped_guard(mutex, &ua->mutex) { 755 + err = start_usb_capture(ua); 756 + } 748 757 if (err < 0) 749 758 return err; 750 759 ··· 772 781 struct ua101 *ua = substream->private_data; 773 782 int err; 774 783 775 - mutex_lock(&ua->mutex); 776 - err = start_usb_capture(ua); 777 - if (err >= 0) 778 - err = start_usb_playback(ua); 779 - mutex_unlock(&ua->mutex); 784 + scoped_guard(mutex, &ua->mutex) { 785 + err = start_usb_capture(ua); 786 + if (err >= 0) 787 + err = start_usb_playback(ua); 788 + } 780 789 if (err < 0) 781 790 return err; 782 791 ··· 1118 1127 unsigned int i; 1119 1128 struct usb_interface *intf; 1120 1129 1121 - mutex_lock(&ua->mutex); 1122 - free_stream_urbs(&ua->capture); 1123 - free_stream_urbs(&ua->playback); 1124 - mutex_unlock(&ua->mutex); 1130 + scoped_guard(mutex, &ua->mutex) { 1131 + free_stream_urbs(&ua->capture); 1132 + free_stream_urbs(&ua->playback); 1133 + } 1125 1134 free_stream_buffers(ua, &ua->capture); 1126 1135 free_stream_buffers(ua, &ua->playback); 1127 1136 1128 1137 for (i = 0; i < ARRAY_SIZE(ua->intf); ++i) { 1129 - mutex_lock(&ua->mutex); 1130 - intf = ua->intf[i]; 1131 - ua->intf[i] = NULL; 1132 - mutex_unlock(&ua->mutex); 1138 + scoped_guard(mutex, &ua->mutex) { 1139 + intf = ua->intf[i]; 1140 + ua->intf[i] = NULL; 1141 + } 1133 1142 if (intf) { 1134 1143 usb_set_intfdata(intf, NULL); 1135 1144 if (intf != interface) ··· 1183 1192 intf_numbers[is_ua1000][0]) 1184 1193 return -ENODEV; 1185 1194 1186 - mutex_lock(&devices_mutex); 1195 + guard(mutex)(&devices_mutex); 1187 1196 1188 1197 for (card_index = 0; card_index < SNDRV_CARDS; ++card_index) 1189 1198 if (enable[card_index] && !(devices_used & (1 << card_index))) 1190 1199 break; 1191 - if (card_index >= SNDRV_CARDS) { 1192 - mutex_unlock(&devices_mutex); 1200 + if (card_index >= SNDRV_CARDS) 1193 1201 return -ENOENT; 1194 - } 1195 1202 err = snd_card_new(&interface->dev, 1196 1203 index[card_index], id[card_index], THIS_MODULE, 1197 1204 sizeof(*ua), &card); 1198 - if (err < 0) { 1199 - mutex_unlock(&devices_mutex); 1205 + if (err < 0) 1200 1206 return err; 1201 - } 1202 1207 card->private_free = ua101_card_free; 1203 1208 ua = card->private_data; 1204 1209 ua->dev = interface_to_usbdev(interface); ··· 1277 1290 usb_set_intfdata(interface, ua); 1278 1291 devices_used |= 1 << card_index; 1279 1292 1280 - mutex_unlock(&devices_mutex); 1281 1293 return 0; 1282 1294 1283 1295 probe_error: 1284 1296 free_usb_related_resources(ua, interface); 1285 1297 snd_card_free(card); 1286 - mutex_unlock(&devices_mutex); 1287 1298 return err; 1288 1299 } 1289 1300 ··· 1293 1308 if (!ua) 1294 1309 return; 1295 1310 1296 - mutex_lock(&devices_mutex); 1311 + guard(mutex)(&devices_mutex); 1297 1312 1298 1313 set_bit(DISCONNECTED, &ua->states); 1299 1314 wake_up(&ua->rate_feedback_wait); ··· 1306 1321 snd_usbmidi_disconnect(midi); 1307 1322 abort_alsa_playback(ua); 1308 1323 abort_alsa_capture(ua); 1309 - mutex_lock(&ua->mutex); 1310 - stop_usb_playback(ua); 1311 - stop_usb_capture(ua); 1312 - mutex_unlock(&ua->mutex); 1324 + scoped_guard(mutex, &ua->mutex) { 1325 + stop_usb_playback(ua); 1326 + stop_usb_capture(ua); 1327 + } 1313 1328 1314 1329 free_usb_related_resources(ua, interface); 1315 1330 1316 1331 devices_used &= ~(1 << ua->card_index); 1317 1332 1318 1333 snd_card_free_when_closed(ua->card); 1319 - 1320 - mutex_unlock(&devices_mutex); 1321 1334 } 1322 1335 1323 1336 static const struct usb_device_id ua101_ids[] = {