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

ALSA: ua101: Use guard() for spin locks

Clean up the code using guard() for spin locks.

Merely code refactoring, and no behavior change.

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

+77 -88
+77 -88
sound/usb/misc/ua101.c
··· 171 171 { 172 172 struct ua101_urb *urb = (struct ua101_urb *)usb_urb; 173 173 struct ua101 *ua = urb->urb.context; 174 - unsigned long flags; 175 174 176 175 if (unlikely(urb->urb.status == -ENOENT || /* unlinked */ 177 176 urb->urb.status == -ENODEV || /* device removed */ ··· 183 184 184 185 if (test_bit(USB_PLAYBACK_RUNNING, &ua->states)) { 185 186 /* append URB to FIFO */ 186 - spin_lock_irqsave(&ua->lock, flags); 187 + guard(spinlock_irqsave)(&ua->lock); 187 188 list_add_tail(&urb->ready_list, &ua->ready_playback_urbs); 188 189 if (ua->rate_feedback_count > 0) 189 190 queue_work(system_highpri_wq, &ua->playback_work); 190 191 ua->playback.substream->runtime->delay -= 191 192 urb->urb.iso_frame_desc[0].length / 192 193 ua->playback.frame_bytes; 193 - spin_unlock_irqrestore(&ua->lock, flags); 194 194 } 195 195 } 196 196 ··· 247 249 static void playback_work(struct work_struct *work) 248 250 { 249 251 struct ua101 *ua = container_of(work, struct ua101, playback_work); 250 - unsigned long flags; 251 252 unsigned int frames; 252 253 struct ua101_urb *urb; 253 254 bool do_period_elapsed = false; ··· 266 269 * submitting playback URBs is possible as long as both FIFOs are 267 270 * nonempty. 268 271 */ 269 - spin_lock_irqsave(&ua->lock, flags); 270 - while (ua->rate_feedback_count > 0 && 271 - !list_empty(&ua->ready_playback_urbs)) { 272 - /* take packet size out of FIFO */ 273 - frames = ua->rate_feedback[ua->rate_feedback_start]; 274 - add_with_wraparound(ua, &ua->rate_feedback_start, 1); 275 - ua->rate_feedback_count--; 272 + scoped_guard(spinlock_irqsave, &ua->lock) { 273 + while (ua->rate_feedback_count > 0 && 274 + !list_empty(&ua->ready_playback_urbs)) { 275 + /* take packet size out of FIFO */ 276 + frames = ua->rate_feedback[ua->rate_feedback_start]; 277 + add_with_wraparound(ua, &ua->rate_feedback_start, 1); 278 + ua->rate_feedback_count--; 276 279 277 - /* take URB out of FIFO */ 278 - urb = list_first_entry(&ua->ready_playback_urbs, 279 - struct ua101_urb, ready_list); 280 - list_del(&urb->ready_list); 280 + /* take URB out of FIFO */ 281 + urb = list_first_entry(&ua->ready_playback_urbs, 282 + struct ua101_urb, ready_list); 283 + list_del(&urb->ready_list); 281 284 282 - /* fill packet with data or silence */ 283 - urb->urb.iso_frame_desc[0].length = 284 - frames * ua->playback.frame_bytes; 285 - if (test_bit(ALSA_PLAYBACK_RUNNING, &ua->states)) 286 - do_period_elapsed |= copy_playback_data(&ua->playback, 287 - &urb->urb, 288 - frames); 289 - else 290 - memset(urb->urb.transfer_buffer, 0, 291 - urb->urb.iso_frame_desc[0].length); 285 + /* fill packet with data or silence */ 286 + urb->urb.iso_frame_desc[0].length = 287 + frames * ua->playback.frame_bytes; 288 + if (test_bit(ALSA_PLAYBACK_RUNNING, &ua->states)) 289 + do_period_elapsed |= copy_playback_data(&ua->playback, 290 + &urb->urb, 291 + frames); 292 + else 293 + memset(urb->urb.transfer_buffer, 0, 294 + urb->urb.iso_frame_desc[0].length); 292 295 293 - /* and off you go ... */ 294 - err = usb_submit_urb(&urb->urb, GFP_ATOMIC); 295 - if (unlikely(err < 0)) { 296 - spin_unlock_irqrestore(&ua->lock, flags); 297 - abort_usb_playback(ua); 298 - abort_alsa_playback(ua); 299 - dev_err(&ua->dev->dev, "USB request error %d: %s\n", 300 - err, usb_error_string(err)); 301 - return; 296 + /* and off you go ... */ 297 + err = usb_submit_urb(&urb->urb, GFP_ATOMIC); 298 + if (unlikely(err < 0)) { 299 + abort_usb_playback(ua); 300 + abort_alsa_playback(ua); 301 + dev_err(&ua->dev->dev, "USB request error %d: %s\n", 302 + err, usb_error_string(err)); 303 + return; 304 + } 305 + ua->playback.substream->runtime->delay += frames; 302 306 } 303 - ua->playback.substream->runtime->delay += frames; 304 307 } 305 - spin_unlock_irqrestore(&ua->lock, flags); 308 + 306 309 if (do_period_elapsed) 307 310 snd_pcm_period_elapsed(ua->playback.substream); 308 311 } ··· 344 347 { 345 348 struct ua101 *ua = urb->context; 346 349 struct ua101_stream *stream = &ua->capture; 347 - unsigned long flags; 348 350 unsigned int frames, write_ptr; 349 351 bool do_period_elapsed; 350 352 int err; ··· 360 364 else 361 365 frames = 0; 362 366 363 - spin_lock_irqsave(&ua->lock, flags); 367 + scoped_guard(spinlock_irqsave, &ua->lock) { 364 368 365 - if (frames > 0 && test_bit(ALSA_CAPTURE_RUNNING, &ua->states)) 366 - do_period_elapsed = copy_capture_data(stream, urb, frames); 367 - else 368 - do_period_elapsed = false; 369 + if (frames > 0 && test_bit(ALSA_CAPTURE_RUNNING, &ua->states)) 370 + do_period_elapsed = copy_capture_data(stream, urb, frames); 371 + else 372 + do_period_elapsed = false; 369 373 370 - if (test_bit(USB_CAPTURE_RUNNING, &ua->states)) { 371 - err = usb_submit_urb(urb, GFP_ATOMIC); 372 - if (unlikely(err < 0)) { 373 - spin_unlock_irqrestore(&ua->lock, flags); 374 - dev_err(&ua->dev->dev, "USB request error %d: %s\n", 375 - err, usb_error_string(err)); 376 - goto stream_stopped; 374 + if (test_bit(USB_CAPTURE_RUNNING, &ua->states)) { 375 + err = usb_submit_urb(urb, GFP_ATOMIC); 376 + if (unlikely(err < 0)) { 377 + dev_err(&ua->dev->dev, "USB request error %d: %s\n", 378 + err, usb_error_string(err)); 379 + goto stream_stopped; 380 + } 381 + 382 + /* append packet size to FIFO */ 383 + write_ptr = ua->rate_feedback_start; 384 + add_with_wraparound(ua, &write_ptr, ua->rate_feedback_count); 385 + ua->rate_feedback[write_ptr] = frames; 386 + if (ua->rate_feedback_count < ua->playback.queue_length) { 387 + ua->rate_feedback_count++; 388 + if (ua->rate_feedback_count == 389 + ua->playback.queue_length) 390 + wake_up(&ua->rate_feedback_wait); 391 + } else { 392 + /* 393 + * Ring buffer overflow; this happens when the playback 394 + * stream is not running. Throw away the oldest entry, 395 + * so that the playback stream, when it starts, sees 396 + * the most recent packet sizes. 397 + */ 398 + add_with_wraparound(ua, &ua->rate_feedback_start, 1); 399 + } 400 + if (test_bit(USB_PLAYBACK_RUNNING, &ua->states) && 401 + !list_empty(&ua->ready_playback_urbs)) 402 + queue_work(system_highpri_wq, &ua->playback_work); 377 403 } 378 - 379 - /* append packet size to FIFO */ 380 - write_ptr = ua->rate_feedback_start; 381 - add_with_wraparound(ua, &write_ptr, ua->rate_feedback_count); 382 - ua->rate_feedback[write_ptr] = frames; 383 - if (ua->rate_feedback_count < ua->playback.queue_length) { 384 - ua->rate_feedback_count++; 385 - if (ua->rate_feedback_count == 386 - ua->playback.queue_length) 387 - wake_up(&ua->rate_feedback_wait); 388 - } else { 389 - /* 390 - * Ring buffer overflow; this happens when the playback 391 - * stream is not running. Throw away the oldest entry, 392 - * so that the playback stream, when it starts, sees 393 - * the most recent packet sizes. 394 - */ 395 - add_with_wraparound(ua, &ua->rate_feedback_start, 1); 396 - } 397 - if (test_bit(USB_PLAYBACK_RUNNING, &ua->states) && 398 - !list_empty(&ua->ready_playback_urbs)) 399 - queue_work(system_highpri_wq, &ua->playback_work); 400 404 } 401 - 402 - spin_unlock_irqrestore(&ua->lock, flags); 403 405 404 406 if (do_period_elapsed) 405 407 snd_pcm_period_elapsed(stream->substream); ··· 552 558 clear_bit(PLAYBACK_URB_COMPLETED, &ua->states); 553 559 ua->playback.urbs[0]->urb.complete = 554 560 first_playback_urb_complete; 555 - spin_lock_irq(&ua->lock); 556 - INIT_LIST_HEAD(&ua->ready_playback_urbs); 557 - spin_unlock_irq(&ua->lock); 561 + scoped_guard(spinlock_irq, &ua->lock) { 562 + INIT_LIST_HEAD(&ua->ready_playback_urbs); 563 + } 558 564 559 565 /* 560 566 * We submit the initial URBs all at once, so we have to wait for the ··· 575 581 576 582 for (i = 0; i < ua->playback.queue_length; ++i) { 577 583 /* all initial URBs contain silence */ 578 - spin_lock_irq(&ua->lock); 579 - frames = ua->rate_feedback[ua->rate_feedback_start]; 580 - add_with_wraparound(ua, &ua->rate_feedback_start, 1); 581 - ua->rate_feedback_count--; 582 - spin_unlock_irq(&ua->lock); 584 + scoped_guard(spinlock_irq, &ua->lock) { 585 + frames = ua->rate_feedback[ua->rate_feedback_start]; 586 + add_with_wraparound(ua, &ua->rate_feedback_start, 1); 587 + ua->rate_feedback_count--; 588 + } 583 589 urb = &ua->playback.urbs[i]->urb; 584 590 urb->iso_frame_desc[0].length = 585 591 frames * ua->playback.frame_bytes; ··· 828 834 static inline snd_pcm_uframes_t ua101_pcm_pointer(struct ua101 *ua, 829 835 struct ua101_stream *stream) 830 836 { 831 - unsigned long flags; 832 - unsigned int pos; 833 - 834 - spin_lock_irqsave(&ua->lock, flags); 835 - pos = stream->buffer_pos; 836 - spin_unlock_irqrestore(&ua->lock, flags); 837 - return pos; 837 + guard(spinlock_irqsave)(&ua->lock); 838 + return stream->buffer_pos; 838 839 } 839 840 840 841 static snd_pcm_uframes_t capture_pcm_pointer(struct snd_pcm_substream *subs)