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

ALSA: usb-audio: Replace manual mutex/spinlock with guard()

This is another code cleanup by replacing the manual mutex or spinlock
with guard() macros. usb_audio_disconnect() is slightly refactored
(split to another function) to apply guard() cleanly, but the rest are
rather straightforward conversions.

No functional changes but only code refactoring.

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

+148 -216
+22 -17
sound/usb/card.c
··· 900 900 901 901 /* check whether it's already registered */ 902 902 chip = NULL; 903 - mutex_lock(&register_mutex); 903 + guard(mutex)(&register_mutex); 904 904 for (i = 0; i < SNDRV_CARDS; i++) { 905 905 if (usb_chip[i] && usb_chip[i]->dev == dev) { 906 906 if (atomic_read(&usb_chip[i]->shutdown)) { ··· 1015 1015 1016 1016 if (platform_ops && platform_ops->connect_cb) 1017 1017 platform_ops->connect_cb(chip); 1018 - mutex_unlock(&register_mutex); 1019 1018 1020 1019 return 0; 1021 1020 ··· 1032 1033 if (!chip->num_interfaces) 1033 1034 snd_card_free(chip->card); 1034 1035 } 1035 - mutex_unlock(&register_mutex); 1036 1036 return err; 1037 1037 } 1038 1038 ··· 1039 1041 * we need to take care of counter, since disconnection can be called also 1040 1042 * many times as well as usb_audio_probe(). 1041 1043 */ 1042 - static void usb_audio_disconnect(struct usb_interface *intf) 1044 + static bool __usb_audio_disconnect(struct usb_interface *intf, 1045 + struct snd_usb_audio *chip, 1046 + struct snd_card *card) 1043 1047 { 1044 - struct snd_usb_audio *chip = usb_get_intfdata(intf); 1045 - struct snd_card *card; 1046 1048 struct list_head *p; 1047 1049 1048 - if (chip == USB_AUDIO_IFACE_UNUSED) 1049 - return; 1050 + guard(mutex)(&register_mutex); 1050 1051 1051 - card = chip->card; 1052 - 1053 - mutex_lock(&register_mutex); 1054 1052 if (platform_ops && platform_ops->disconnect_cb) 1055 1053 platform_ops->disconnect_cb(chip); 1056 1054 ··· 1092 1098 usb_enable_autosuspend(interface_to_usbdev(intf)); 1093 1099 1094 1100 chip->num_interfaces--; 1095 - if (chip->num_interfaces <= 0) { 1096 - usb_chip[chip->index] = NULL; 1097 - mutex_unlock(&register_mutex); 1101 + if (chip->num_interfaces > 0) 1102 + return false; 1103 + 1104 + usb_chip[chip->index] = NULL; 1105 + return true; 1106 + } 1107 + 1108 + static void usb_audio_disconnect(struct usb_interface *intf) 1109 + { 1110 + struct snd_usb_audio *chip = usb_get_intfdata(intf); 1111 + struct snd_card *card; 1112 + 1113 + if (chip == USB_AUDIO_IFACE_UNUSED) 1114 + return; 1115 + 1116 + card = chip->card; 1117 + if (__usb_audio_disconnect(intf, chip, card)) 1098 1118 snd_card_free_when_closed(card); 1099 - } else { 1100 - mutex_unlock(&register_mutex); 1101 - } 1102 1119 } 1103 1120 1104 1121 /* lock the shutdown (disconnect) task and autoresume */
+46 -78
sound/usb/endpoint.c
··· 163 163 static int slave_next_packet_size(struct snd_usb_endpoint *ep, 164 164 unsigned int avail) 165 165 { 166 - unsigned long flags; 167 166 unsigned int phase; 168 167 int ret; 169 168 170 169 if (ep->fill_max) 171 170 return ep->maxframesize; 172 171 173 - spin_lock_irqsave(&ep->lock, flags); 172 + guard(spinlock_irqsave)(&ep->lock); 174 173 phase = (ep->phase & 0xffff) + (ep->freqm << ep->datainterval); 175 174 ret = min(phase >> 16, ep->maxframesize); 176 175 if (avail && ret >= avail) 177 176 ret = -EAGAIN; 178 177 else 179 178 ep->phase = phase; 180 - spin_unlock_irqrestore(&ep->lock, flags); 181 - 182 179 return ret; 183 180 } 184 181 ··· 437 440 static void push_back_to_ready_list(struct snd_usb_endpoint *ep, 438 441 struct snd_urb_ctx *ctx) 439 442 { 440 - unsigned long flags; 441 - 442 - spin_lock_irqsave(&ep->lock, flags); 443 + guard(spinlock_irqsave)(&ep->lock); 443 444 list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs); 444 - spin_unlock_irqrestore(&ep->lock, flags); 445 445 } 446 446 447 447 /* ··· 460 466 bool implicit_fb = snd_usb_endpoint_implicit_feedback_sink(ep); 461 467 462 468 while (ep_state_running(ep)) { 463 - 464 - unsigned long flags; 465 469 struct snd_usb_packet_info *packet; 466 470 struct snd_urb_ctx *ctx = NULL; 467 471 int err, i; 468 472 469 - spin_lock_irqsave(&ep->lock, flags); 470 - if ((!implicit_fb || ep->next_packet_queued > 0) && 471 - !list_empty(&ep->ready_playback_urbs)) { 472 - /* take URB out of FIFO */ 473 - ctx = list_first_entry(&ep->ready_playback_urbs, 474 - struct snd_urb_ctx, ready_list); 475 - list_del_init(&ctx->ready_list); 476 - if (implicit_fb) 477 - packet = next_packet_fifo_dequeue(ep); 473 + scoped_guard(spinlock_irqsave, &ep->lock) { 474 + if ((!implicit_fb || ep->next_packet_queued > 0) && 475 + !list_empty(&ep->ready_playback_urbs)) { 476 + /* take URB out of FIFO */ 477 + ctx = list_first_entry(&ep->ready_playback_urbs, 478 + struct snd_urb_ctx, ready_list); 479 + list_del_init(&ctx->ready_list); 480 + if (implicit_fb) 481 + packet = next_packet_fifo_dequeue(ep); 482 + } 478 483 } 479 - spin_unlock_irqrestore(&ep->lock, flags); 480 484 481 485 if (ctx == NULL) 482 486 break; ··· 760 768 const struct audioformat *fp, 761 769 const struct snd_pcm_hw_params *params) 762 770 { 763 - bool ret; 764 - 765 - mutex_lock(&chip->mutex); 766 - ret = endpoint_compatible(ep, fp, params); 767 - mutex_unlock(&chip->mutex); 768 - return ret; 771 + guard(mutex)(&chip->mutex); 772 + return endpoint_compatible(ep, fp, params); 769 773 } 770 774 771 775 /* ··· 787 799 struct snd_usb_endpoint *ep; 788 800 int ep_num = is_sync_ep ? fp->sync_ep : fp->endpoint; 789 801 790 - mutex_lock(&chip->mutex); 802 + guard(mutex)(&chip->mutex); 791 803 ep = snd_usb_get_endpoint(chip, ep_num); 792 804 if (!ep) { 793 805 usb_audio_err(chip, "Cannot find EP 0x%x to open\n", ep_num); 794 - goto unlock; 806 + return NULL; 795 807 } 796 808 797 809 if (!ep->opened) { ··· 808 820 ep_num, ep->iface, ep->altsetting, ep->ep_idx); 809 821 810 822 ep->iface_ref = iface_ref_find(chip, ep->iface); 811 - if (!ep->iface_ref) { 812 - ep = NULL; 813 - goto unlock; 814 - } 823 + if (!ep->iface_ref) 824 + return NULL; 815 825 816 826 if (fp->protocol != UAC_VERSION_1) { 817 827 ep->clock_ref = clock_ref_find(chip, fp->clock); 818 - if (!ep->clock_ref) { 819 - ep = NULL; 820 - goto unlock; 821 - } 828 + if (!ep->clock_ref) 829 + return NULL; 822 830 ep->clock_ref->opened++; 823 831 } 824 832 ··· 843 859 ep->implicit_fb_sync); 844 860 845 861 } else { 846 - if (WARN_ON(!ep->iface_ref)) { 847 - ep = NULL; 848 - goto unlock; 849 - } 862 + if (WARN_ON(!ep->iface_ref)) 863 + return NULL; 850 864 851 865 if (!endpoint_compatible(ep, fp, params)) { 852 866 usb_audio_err(chip, "Incompatible EP setup for 0x%x\n", 853 867 ep_num); 854 - ep = NULL; 855 - goto unlock; 868 + return NULL; 856 869 } 857 870 858 871 usb_audio_dbg(chip, "Reopened EP 0x%x (count %d)\n", ··· 860 879 ep->iface_ref->need_setup = true; 861 880 862 881 ep->opened++; 863 - 864 - unlock: 865 - mutex_unlock(&chip->mutex); 866 882 return ep; 867 883 } 868 884 ··· 942 964 void snd_usb_endpoint_close(struct snd_usb_audio *chip, 943 965 struct snd_usb_endpoint *ep) 944 966 { 945 - mutex_lock(&chip->mutex); 967 + guard(mutex)(&chip->mutex); 946 968 usb_audio_dbg(chip, "Closing EP 0x%x (count %d)\n", 947 969 ep->ep_num, ep->opened); 948 970 ··· 963 985 ep->clock_ref = NULL; 964 986 usb_audio_dbg(chip, "EP 0x%x closed\n", ep->ep_num); 965 987 } 966 - mutex_unlock(&chip->mutex); 967 988 } 968 989 969 990 /* Prepare for suspening EP, called from the main suspend handler */ ··· 1024 1047 static int stop_urbs(struct snd_usb_endpoint *ep, bool force, bool keep_pending) 1025 1048 { 1026 1049 unsigned int i; 1027 - unsigned long flags; 1028 1050 1029 1051 if (!force && atomic_read(&ep->running)) 1030 1052 return -EBUSY; ··· 1031 1055 if (!ep_state_update(ep, EP_STATE_RUNNING, EP_STATE_STOPPING)) 1032 1056 return 0; 1033 1057 1034 - spin_lock_irqsave(&ep->lock, flags); 1035 - INIT_LIST_HEAD(&ep->ready_playback_urbs); 1036 - ep->next_packet_head = 0; 1037 - ep->next_packet_queued = 0; 1038 - spin_unlock_irqrestore(&ep->lock, flags); 1058 + scoped_guard(spinlock_irqsave, &ep->lock) { 1059 + INIT_LIST_HEAD(&ep->ready_playback_urbs); 1060 + ep->next_packet_head = 0; 1061 + ep->next_packet_queued = 0; 1062 + } 1039 1063 1040 1064 if (keep_pending) 1041 1065 return 0; ··· 1336 1360 struct snd_usb_endpoint *ep) 1337 1361 { 1338 1362 const struct audioformat *fmt = ep->cur_audiofmt; 1339 - int err = 0; 1363 + int err; 1340 1364 1341 - mutex_lock(&chip->mutex); 1365 + guard(mutex)(&chip->mutex); 1342 1366 if (!ep->need_setup) 1343 - goto unlock; 1367 + return 0; 1344 1368 1345 1369 /* release old buffers, if any */ 1346 1370 err = release_urbs(ep, false); 1347 1371 if (err < 0) 1348 - goto unlock; 1372 + return err; 1349 1373 1350 1374 ep->datainterval = fmt->datainterval; 1351 1375 ep->maxpacksize = fmt->maxpacksize; ··· 1383 1407 usb_audio_dbg(chip, "Set up %d URBS, ret=%d\n", ep->nurbs, err); 1384 1408 1385 1409 if (err < 0) 1386 - goto unlock; 1410 + return err; 1387 1411 1388 1412 /* some unit conversions in runtime */ 1389 1413 ep->maxframesize = ep->maxpacksize / ep->cur_frame_bytes; ··· 1395 1419 err = 0; 1396 1420 } 1397 1421 1398 - unlock: 1399 - mutex_unlock(&chip->mutex); 1400 1422 return err; 1401 1423 } 1402 1424 ··· 1441 1467 bool iface_first; 1442 1468 int err = 0; 1443 1469 1444 - mutex_lock(&chip->mutex); 1470 + guard(mutex)(&chip->mutex); 1445 1471 if (WARN_ON(!ep->iface_ref)) 1446 - goto unlock; 1472 + return 0; 1447 1473 if (!ep->need_prepare) 1448 - goto unlock; 1474 + return 0; 1449 1475 1450 1476 /* If the interface has been already set up, just set EP parameters */ 1451 1477 if (!ep->iface_ref->need_setup) { ··· 1455 1481 if (ep->cur_audiofmt->protocol == UAC_VERSION_1) { 1456 1482 err = init_sample_rate(chip, ep); 1457 1483 if (err < 0) 1458 - goto unlock; 1484 + return err; 1459 1485 } 1460 1486 goto done; 1461 1487 } ··· 1473 1499 if (iface_first) { 1474 1500 err = endpoint_set_interface(chip, ep, true); 1475 1501 if (err < 0) 1476 - goto unlock; 1502 + return err; 1477 1503 } 1478 1504 1479 1505 err = snd_usb_init_pitch(chip, ep->cur_audiofmt); 1480 1506 if (err < 0) 1481 - goto unlock; 1507 + return err; 1482 1508 1483 1509 err = init_sample_rate(chip, ep); 1484 1510 if (err < 0) 1485 - goto unlock; 1511 + return err; 1486 1512 1487 1513 err = snd_usb_select_mode_quirk(chip, ep->cur_audiofmt); 1488 1514 if (err < 0) 1489 - goto unlock; 1515 + return err; 1490 1516 1491 1517 /* for UAC2/3, enable the interface altset here at last */ 1492 1518 if (!iface_first) { 1493 1519 err = endpoint_set_interface(chip, ep, true); 1494 1520 if (err < 0) 1495 - goto unlock; 1521 + return err; 1496 1522 } 1497 1523 1498 1524 ep->iface_ref->need_setup = false; 1499 1525 1500 1526 done: 1501 1527 ep->need_prepare = false; 1502 - err = 1; 1503 - 1504 - unlock: 1505 - mutex_unlock(&chip->mutex); 1506 - return err; 1528 + return 1; 1507 1529 } 1508 1530 EXPORT_SYMBOL_GPL(snd_usb_endpoint_prepare); 1509 1531 ··· 1511 1541 1512 1542 if (!clock) 1513 1543 return 0; 1514 - mutex_lock(&chip->mutex); 1544 + guard(mutex)(&chip->mutex); 1515 1545 list_for_each_entry(ref, &chip->clock_ref_list, list) { 1516 1546 if (ref->clock == clock) { 1517 1547 rate = ref->rate; 1518 1548 break; 1519 1549 } 1520 1550 } 1521 - mutex_unlock(&chip->mutex); 1522 1551 return rate; 1523 1552 } 1524 1553 ··· 1867 1898 * If the frequency looks valid, set it. 1868 1899 * This value is referred to in prepare_playback_urb(). 1869 1900 */ 1870 - spin_lock_irqsave(&ep->lock, flags); 1901 + guard(spinlock_irqsave)(&ep->lock); 1871 1902 ep->freqm = f; 1872 - spin_unlock_irqrestore(&ep->lock, flags); 1873 1903 } else { 1874 1904 /* 1875 1905 * Out of range; maybe the shift value is wrong.
+2 -4
sound/usb/media.c
··· 140 140 if (!mctl) 141 141 return 0; 142 142 143 - mutex_lock(&mctl->media_dev->graph_mutex); 143 + guard(mutex)(&mctl->media_dev->graph_mutex); 144 144 if (mctl->media_dev->enable_source) 145 145 ret = mctl->media_dev->enable_source(&mctl->media_entity, 146 146 &mctl->media_pipe); 147 - mutex_unlock(&mctl->media_dev->graph_mutex); 148 147 return ret; 149 148 } 150 149 ··· 154 155 if (!mctl) 155 156 return; 156 157 157 - mutex_lock(&mctl->media_dev->graph_mutex); 158 + guard(mutex)(&mctl->media_dev->graph_mutex); 158 159 if (mctl->media_dev->disable_source) 159 160 mctl->media_dev->disable_source(&mctl->media_entity); 160 - mutex_unlock(&mctl->media_dev->graph_mutex); 161 161 } 162 162 163 163 static int snd_media_mixer_init(struct snd_usb_audio *chip)
+25 -41
sound/usb/midi.c
··· 265 265 struct out_urb_context *context = urb->context; 266 266 struct snd_usb_midi_out_endpoint *ep = context->ep; 267 267 unsigned int urb_index; 268 - unsigned long flags; 269 268 270 - spin_lock_irqsave(&ep->buffer_lock, flags); 271 - urb_index = context - ep->urbs; 272 - ep->active_urbs &= ~(1 << urb_index); 273 - if (unlikely(ep->drain_urbs)) { 274 - ep->drain_urbs &= ~(1 << urb_index); 275 - wake_up(&ep->drain_wait); 269 + scoped_guard(spinlock_irqsave, &ep->buffer_lock) { 270 + urb_index = context - ep->urbs; 271 + ep->active_urbs &= ~(1 << urb_index); 272 + if (unlikely(ep->drain_urbs)) { 273 + ep->drain_urbs &= ~(1 << urb_index); 274 + wake_up(&ep->drain_wait); 275 + } 276 276 } 277 - spin_unlock_irqrestore(&ep->buffer_lock, flags); 278 277 if (urb->status < 0) { 279 278 int err = snd_usbmidi_urb_error(urb); 280 279 if (err < 0) { ··· 294 295 { 295 296 unsigned int urb_index; 296 297 struct urb *urb; 297 - unsigned long flags; 298 298 299 - spin_lock_irqsave(&ep->buffer_lock, flags); 300 - if (ep->umidi->disconnected) { 301 - spin_unlock_irqrestore(&ep->buffer_lock, flags); 299 + guard(spinlock_irqsave)(&ep->buffer_lock); 300 + if (ep->umidi->disconnected) 302 301 return; 303 - } 304 302 305 303 urb_index = ep->next_urb; 306 304 for (;;) { ··· 321 325 break; 322 326 } 323 327 ep->next_urb = urb_index; 324 - spin_unlock_irqrestore(&ep->buffer_lock, flags); 325 328 } 326 329 327 330 static void snd_usbmidi_out_work(struct work_struct *work) ··· 337 342 struct snd_usb_midi *umidi = timer_container_of(umidi, t, error_timer); 338 343 unsigned int i, j; 339 344 340 - spin_lock(&umidi->disc_lock); 345 + guard(spinlock)(&umidi->disc_lock); 341 346 if (umidi->disconnected) { 342 - spin_unlock(&umidi->disc_lock); 343 347 return; 344 348 } 345 349 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { ··· 355 361 if (umidi->endpoints[i].out) 356 362 snd_usbmidi_do_output(umidi->endpoints[i].out); 357 363 } 358 - spin_unlock(&umidi->disc_lock); 359 364 } 360 365 361 366 /* helper function to send static data that may not DMA-able */ ··· 1141 1148 struct snd_usb_midi *umidi = substream->rmidi->private_data; 1142 1149 struct snd_kcontrol *ctl; 1143 1150 1144 - down_read(&umidi->disc_rwsem); 1145 - if (umidi->disconnected) { 1146 - up_read(&umidi->disc_rwsem); 1151 + guard(rwsem_read)(&umidi->disc_rwsem); 1152 + if (umidi->disconnected) 1147 1153 return open ? -ENODEV : 0; 1148 - } 1149 1154 1150 - mutex_lock(&umidi->mutex); 1155 + guard(mutex)(&umidi->mutex); 1151 1156 if (open) { 1152 1157 if (!umidi->opened[0] && !umidi->opened[1]) { 1153 1158 if (umidi->roland_load_ctl) { ··· 1174 1183 } 1175 1184 } 1176 1185 } 1177 - mutex_unlock(&umidi->mutex); 1178 - up_read(&umidi->disc_rwsem); 1179 1186 return 0; 1180 1187 } 1181 1188 ··· 1537 1548 * a timer may submit an URB. To reliably break the cycle 1538 1549 * a flag under lock must be used 1539 1550 */ 1540 - down_write(&umidi->disc_rwsem); 1541 - spin_lock_irq(&umidi->disc_lock); 1542 - umidi->disconnected = 1; 1543 - spin_unlock_irq(&umidi->disc_lock); 1544 - up_write(&umidi->disc_rwsem); 1551 + scoped_guard(rwsem_write, &umidi->disc_rwsem) { 1552 + guard(spinlock_irq)(&umidi->disc_lock); 1553 + umidi->disconnected = 1; 1554 + } 1545 1555 1546 1556 timer_shutdown_sync(&umidi->error_timer); 1547 1557 ··· 2082 2094 2083 2095 if (value->value.enumerated.item[0] > 1) 2084 2096 return -EINVAL; 2085 - mutex_lock(&umidi->mutex); 2097 + guard(mutex)(&umidi->mutex); 2086 2098 changed = value->value.enumerated.item[0] != kcontrol->private_value; 2087 2099 if (changed) 2088 2100 kcontrol->private_value = value->value.enumerated.item[0]; 2089 - mutex_unlock(&umidi->mutex); 2090 2101 return changed; 2091 2102 } 2092 2103 ··· 2435 2448 struct snd_usb_midi_in_endpoint *ep) 2436 2449 { 2437 2450 unsigned int i; 2438 - unsigned long flags; 2439 2451 2440 2452 if (!ep) 2441 2453 return; 2442 2454 for (i = 0; i < INPUT_URBS; ++i) { 2443 2455 struct urb *urb = ep->urbs[i]; 2444 - spin_lock_irqsave(&umidi->disc_lock, flags); 2445 - if (!atomic_read(&urb->use_count)) { 2446 - urb->dev = ep->umidi->dev; 2447 - snd_usbmidi_submit_urb(urb, GFP_ATOMIC); 2456 + scoped_guard(spinlock_irqsave, &umidi->disc_lock) { 2457 + if (!atomic_read(&urb->use_count)) { 2458 + urb->dev = ep->umidi->dev; 2459 + snd_usbmidi_submit_urb(urb, GFP_ATOMIC); 2460 + } 2448 2461 } 2449 - spin_unlock_irqrestore(&umidi->disc_lock, flags); 2450 2462 } 2451 2463 } 2452 2464 ··· 2474 2488 struct snd_usb_midi *umidi; 2475 2489 2476 2490 umidi = list_entry(p, struct snd_usb_midi, list); 2477 - mutex_lock(&umidi->mutex); 2491 + guard(mutex)(&umidi->mutex); 2478 2492 snd_usbmidi_input_stop(p); 2479 - mutex_unlock(&umidi->mutex); 2480 2493 } 2481 2494 EXPORT_SYMBOL(snd_usbmidi_suspend); 2482 2495 ··· 2487 2502 struct snd_usb_midi *umidi; 2488 2503 2489 2504 umidi = list_entry(p, struct snd_usb_midi, list); 2490 - mutex_lock(&umidi->mutex); 2505 + guard(mutex)(&umidi->mutex); 2491 2506 snd_usbmidi_input_start(p); 2492 - mutex_unlock(&umidi->mutex); 2493 2507 } 2494 2508 EXPORT_SYMBOL(snd_usbmidi_resume); 2495 2509
+4 -12
sound/usb/midi2.c
··· 160 160 { 161 161 struct snd_usb_midi2_urb *ctx = urb->context; 162 162 struct snd_usb_midi2_endpoint *ep = ctx->ep; 163 - unsigned long flags; 164 163 165 - spin_lock_irqsave(&ep->lock, flags); 164 + guard(spinlock_irqsave)(&ep->lock); 166 165 set_bit(ctx->index, &ep->urb_free); 167 166 if (urb->status >= 0 && atomic_read(&ep->running)) 168 167 submit_output_urbs_locked(ep); 169 168 if (ep->urb_free == ep->urb_free_mask) 170 169 wake_up(&ep->wait); 171 - spin_unlock_irqrestore(&ep->lock, flags); 172 170 } 173 171 174 172 /* prepare for input submission: just set the buffer length */ ··· 187 189 { 188 190 struct snd_usb_midi2_urb *ctx = urb->context; 189 191 struct snd_usb_midi2_endpoint *ep = ctx->ep; 190 - unsigned long flags; 191 192 int len; 192 193 193 - spin_lock_irqsave(&ep->lock, flags); 194 + guard(spinlock_irqsave)(&ep->lock); 194 195 if (ep->disconnected || urb->status < 0) 195 196 goto dequeue; 196 197 len = urb->actual_length; ··· 205 208 submit_input_urbs_locked(ep); 206 209 if (ep->urb_free == ep->urb_free_mask) 207 210 wake_up(&ep->wait); 208 - spin_unlock_irqrestore(&ep->lock, flags); 209 211 } 210 212 211 213 /* URB submission helper; for both direction */ 212 214 static void submit_io_urbs(struct snd_usb_midi2_endpoint *ep) 213 215 { 214 - unsigned long flags; 215 - 216 216 if (!ep) 217 217 return; 218 - spin_lock_irqsave(&ep->lock, flags); 218 + guard(spinlock_irqsave)(&ep->lock); 219 219 if (ep->direction == STR_IN) 220 220 submit_input_urbs_locked(ep); 221 221 else 222 222 submit_output_urbs_locked(ep); 223 - spin_unlock_irqrestore(&ep->lock, flags); 224 223 } 225 224 226 225 /* kill URBs for close, suspend and disconnect */ ··· 241 248 { 242 249 if (!ep) 243 250 return; 244 - spin_lock_irq(&ep->lock); 251 + guard(spinlock_irq)(&ep->lock); 245 252 atomic_set(&ep->running, 0); 246 253 wait_event_lock_irq_timeout(ep->wait, 247 254 ep->disconnected || 248 255 ep->urb_free == ep->urb_free_mask, 249 256 ep->lock, msecs_to_jiffies(500)); 250 - spin_unlock_irq(&ep->lock); 251 257 } 252 258 253 259 /* release URBs for an EP */
+13 -22
sound/usb/mixer_s1810c.c
··· 338 338 struct s1810_mixer_state *private = mixer->private_data; 339 339 u32 field = 0; 340 340 u32 ctl_idx = (u32) (kctl->private_value & 0xFF); 341 - int ret = 0; 341 + int ret; 342 342 343 - mutex_lock(&private->usb_mutex); 343 + guard(mutex)(&private->usb_mutex); 344 344 ret = snd_sc1810c_get_status_field(chip->dev, &field, 345 345 ctl_idx, &private->seqnum); 346 346 if (ret < 0) 347 - goto unlock; 347 + return ret; 348 348 349 349 *state = field; 350 - unlock: 351 - mutex_unlock(&private->usb_mutex); 352 - return ret ? ret : 0; 350 + return ret; 353 351 } 354 352 355 353 /* ··· 364 366 u32 pval = (u32) kctl->private_value; 365 367 u32 ctl_id = (pval >> 8) & 0xFF; 366 368 u32 ctl_val = (pval >> 16) & 0x1; 367 - int ret = 0; 368 369 369 - mutex_lock(&private->usb_mutex); 370 - ret = snd_s1810c_send_ctl_packet(chip->dev, 0, 0, 0, ctl_id, ctl_val); 371 - mutex_unlock(&private->usb_mutex); 372 - return ret; 370 + guard(mutex)(&private->usb_mutex); 371 + return snd_s1810c_send_ctl_packet(chip->dev, 0, 0, 0, ctl_id, ctl_val); 373 372 } 374 373 375 374 /* Generic get/set/init functions for switch controls */ ··· 381 386 u32 pval = (u32) kctl->private_value; 382 387 u32 ctl_idx = pval & 0xFF; 383 388 u32 state = 0; 384 - int ret = 0; 389 + int ret; 385 390 386 - mutex_lock(&private->data_mutex); 391 + guard(mutex)(&private->data_mutex); 387 392 ret = snd_s1810c_get_switch_state(mixer, kctl, &state); 388 393 if (ret < 0) 389 - goto unlock; 394 + return ret; 390 395 391 396 switch (ctl_idx) { 392 397 case SC1810C_STATE_LINE_SW: ··· 397 402 ctl_elem->value.integer.value[0] = (long)state; 398 403 } 399 404 400 - unlock: 401 - mutex_unlock(&private->data_mutex); 402 - return (ret < 0) ? ret : 0; 405 + return 0; 403 406 } 404 407 405 408 static int ··· 413 420 u32 newval = 0; 414 421 int ret = 0; 415 422 416 - mutex_lock(&private->data_mutex); 423 + guard(mutex)(&private->data_mutex); 417 424 ret = snd_s1810c_get_switch_state(mixer, kctl, &curval); 418 425 if (ret < 0) 419 - goto unlock; 426 + return ret; 420 427 421 428 switch (ctl_idx) { 422 429 case SC1810C_STATE_LINE_SW: ··· 428 435 } 429 436 430 437 if (curval == newval) 431 - goto unlock; 438 + return 0; 432 439 433 440 kctl->private_value &= ~(0x1 << 16); 434 441 kctl->private_value |= (unsigned int)(newval & 0x1) << 16; 435 442 ret = snd_s1810c_set_switch_state(mixer, kctl); 436 443 437 - unlock: 438 - mutex_unlock(&private->data_mutex); 439 444 return (ret < 0) ? 0 : 1; 440 445 } 441 446
+1 -2
sound/usb/mixer_us16x08.c
··· 152 152 unsigned char *buf, int size) 153 153 { 154 154 155 - mutex_lock(&chip->mutex); 155 + guard(mutex)(&chip->mutex); 156 156 snd_usb_ctl_msg(chip->dev, 157 157 usb_rcvctrlpipe(chip->dev, 0), 158 158 SND_US16X08_URB_METER_REQUEST, 159 159 SND_US16X08_URB_METER_REQUESTTYPE, 0, 0, buf, size); 160 - mutex_unlock(&chip->mutex); 161 160 return 0; 162 161 } 163 162
+34 -38
sound/usb/pcm.c
··· 77 77 78 78 if (atomic_read(&subs->stream->chip->shutdown)) 79 79 return SNDRV_PCM_POS_XRUN; 80 - spin_lock(&subs->lock); 81 - hwptr_done = subs->hwptr_done; 82 - runtime->delay = snd_usb_pcm_delay(subs, runtime); 83 - spin_unlock(&subs->lock); 80 + scoped_guard(spinlock, &subs->lock) { 81 + hwptr_done = subs->hwptr_done; 82 + runtime->delay = snd_usb_pcm_delay(subs, runtime); 83 + } 84 84 return bytes_to_frames(runtime, hwptr_done); 85 85 } 86 86 ··· 560 560 subs->sync_endpoint); 561 561 } 562 562 563 - mutex_lock(&chip->mutex); 564 - subs->cur_audiofmt = fmt; 565 - mutex_unlock(&chip->mutex); 563 + scoped_guard(mutex, &chip->mutex) { 564 + subs->cur_audiofmt = fmt; 565 + } 566 566 567 567 if (!subs->data_endpoint->need_setup) 568 568 goto unlock; ··· 611 611 struct snd_usb_audio *chip = subs->stream->chip; 612 612 613 613 snd_media_stop_pipeline(subs); 614 - mutex_lock(&chip->mutex); 615 - subs->cur_audiofmt = NULL; 616 - mutex_unlock(&chip->mutex); 614 + scoped_guard(mutex, &chip->mutex) { 615 + subs->cur_audiofmt = NULL; 616 + } 617 617 if (!snd_usb_lock_shutdown(chip)) { 618 618 if (stop_endpoints(subs, false)) 619 619 sync_pending_stops(subs); ··· 1244 1244 struct snd_usb_audio *chip = subs->stream->chip; 1245 1245 int ret; 1246 1246 1247 - mutex_lock(&chip->mutex); 1248 - if (subs->opened) { 1249 - mutex_unlock(&chip->mutex); 1250 - return -EBUSY; 1247 + scoped_guard(mutex, &chip->mutex) { 1248 + if (subs->opened) 1249 + return -EBUSY; 1250 + subs->opened = 1; 1251 1251 } 1252 - subs->opened = 1; 1253 - mutex_unlock(&chip->mutex); 1254 1252 1255 1253 runtime->hw = snd_usb_hardware; 1256 1254 /* need an explicit sync to catch applptr update in low-latency mode */ ··· 1279 1281 err_resume: 1280 1282 snd_usb_autosuspend(subs->stream->chip); 1281 1283 err_open: 1282 - mutex_lock(&chip->mutex); 1283 - subs->opened = 0; 1284 - mutex_unlock(&chip->mutex); 1284 + scoped_guard(mutex, &chip->mutex) { 1285 + subs->opened = 0; 1286 + } 1285 1287 1286 1288 return ret; 1287 1289 } ··· 1305 1307 1306 1308 subs->pcm_substream = NULL; 1307 1309 snd_usb_autosuspend(subs->stream->chip); 1308 - mutex_lock(&chip->mutex); 1309 - subs->opened = 0; 1310 - mutex_unlock(&chip->mutex); 1310 + scoped_guard(mutex, &chip->mutex) { 1311 + subs->opened = 0; 1312 + } 1311 1313 1312 1314 return 0; 1313 1315 } ··· 1323 1325 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime; 1324 1326 unsigned int stride, frames, bytes, oldptr; 1325 1327 int i, period_elapsed = 0; 1326 - unsigned long flags; 1327 1328 unsigned char *cp; 1328 1329 int current_frame_number; 1329 1330 ··· 1355 1358 oldbytes, bytes); 1356 1359 } 1357 1360 /* update the current pointer */ 1358 - spin_lock_irqsave(&subs->lock, flags); 1359 - oldptr = subs->hwptr_done; 1360 - subs->hwptr_done += bytes; 1361 - if (subs->hwptr_done >= subs->buffer_bytes) 1362 - subs->hwptr_done -= subs->buffer_bytes; 1363 - frames = (bytes + (oldptr % stride)) / stride; 1364 - subs->transfer_done += frames; 1365 - if (subs->transfer_done >= runtime->period_size) { 1366 - subs->transfer_done -= runtime->period_size; 1367 - period_elapsed = 1; 1361 + scoped_guard(spinlock_irqsave, &subs->lock) { 1362 + oldptr = subs->hwptr_done; 1363 + subs->hwptr_done += bytes; 1364 + if (subs->hwptr_done >= subs->buffer_bytes) 1365 + subs->hwptr_done -= subs->buffer_bytes; 1366 + frames = (bytes + (oldptr % stride)) / stride; 1367 + subs->transfer_done += frames; 1368 + if (subs->transfer_done >= runtime->period_size) { 1369 + subs->transfer_done -= runtime->period_size; 1370 + period_elapsed = 1; 1371 + } 1372 + 1373 + /* realign last_frame_number */ 1374 + subs->last_frame_number = current_frame_number; 1368 1375 } 1369 - 1370 - /* realign last_frame_number */ 1371 - subs->last_frame_number = current_frame_number; 1372 - 1373 - spin_unlock_irqrestore(&subs->lock, flags); 1374 1376 /* copy a data chunk */ 1375 1377 if (oldptr + bytes > subs->buffer_bytes) { 1376 1378 unsigned int bytes1 = subs->buffer_bytes - oldptr;
+1 -2
sound/usb/proc.c
··· 193 193 struct snd_usb_substream *subs, 194 194 struct snd_info_buffer *buffer) 195 195 { 196 - mutex_lock(&chip->mutex); 196 + guard(mutex)(&chip->mutex); 197 197 if (subs->running) { 198 198 snd_iprintf(buffer, " Status: Running\n"); 199 199 if (subs->cur_audiofmt) { ··· 204 204 } else { 205 205 snd_iprintf(buffer, " Status: Stop\n"); 206 206 } 207 - mutex_unlock(&chip->mutex); 208 207 } 209 208 210 209 static void proc_pcm_format_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)