mirror of
https://github.com/torvalds/linux.git
synced 2026-01-12 00:42:35 +08:00
ALSA: 6fire: Clean ups with guard()
Simple code cleanups with the guard() for spinlock and mutex. No functional changes. Link: https://patch.msgid.link/20250811082231.31498-1-tiwai@suse.de Signed-off-by: Takashi Iwai <tiwai@suse.de>
This commit is contained in:
@@ -88,24 +88,22 @@ static int usb6fire_chip_probe(struct usb_interface *intf,
|
||||
struct snd_card *card = NULL;
|
||||
|
||||
/* look if we already serve this card and return if so */
|
||||
mutex_lock(®ister_mutex);
|
||||
for (i = 0; i < SNDRV_CARDS; i++) {
|
||||
if (devices[i] == device) {
|
||||
if (chips[i])
|
||||
chips[i]->intf_count++;
|
||||
usb_set_intfdata(intf, chips[i]);
|
||||
mutex_unlock(®ister_mutex);
|
||||
return 0;
|
||||
} else if (!devices[i] && regidx < 0)
|
||||
regidx = i;
|
||||
scoped_guard(mutex, ®ister_mutex) {
|
||||
for (i = 0; i < SNDRV_CARDS; i++) {
|
||||
if (devices[i] == device) {
|
||||
if (chips[i])
|
||||
chips[i]->intf_count++;
|
||||
usb_set_intfdata(intf, chips[i]);
|
||||
return 0;
|
||||
} else if (!devices[i] && regidx < 0)
|
||||
regidx = i;
|
||||
}
|
||||
if (regidx < 0) {
|
||||
dev_err(&intf->dev, "too many cards registered.\n");
|
||||
return -ENODEV;
|
||||
}
|
||||
devices[regidx] = device;
|
||||
}
|
||||
if (regidx < 0) {
|
||||
mutex_unlock(®ister_mutex);
|
||||
dev_err(&intf->dev, "too many cards registered.\n");
|
||||
return -ENODEV;
|
||||
}
|
||||
devices[regidx] = device;
|
||||
mutex_unlock(®ister_mutex);
|
||||
|
||||
/* check, if firmware is present on device, upload it if not */
|
||||
ret = usb6fire_fw_init(intf);
|
||||
@@ -175,10 +173,10 @@ static void usb6fire_chip_disconnect(struct usb_interface *intf)
|
||||
if (chip) { /* if !chip, fw upload has been performed */
|
||||
chip->intf_count--;
|
||||
if (!chip->intf_count) {
|
||||
mutex_lock(®ister_mutex);
|
||||
devices[chip->regidx] = NULL;
|
||||
chips[chip->regidx] = NULL;
|
||||
mutex_unlock(®ister_mutex);
|
||||
scoped_guard(mutex, ®ister_mutex) {
|
||||
devices[chip->regidx] = NULL;
|
||||
chips[chip->regidx] = NULL;
|
||||
}
|
||||
|
||||
chip->shutdown = true;
|
||||
usb6fire_chip_abort(chip);
|
||||
|
||||
@@ -23,9 +23,8 @@ static void usb6fire_midi_out_handler(struct urb *urb)
|
||||
{
|
||||
struct midi_runtime *rt = urb->context;
|
||||
int ret;
|
||||
unsigned long flags;
|
||||
|
||||
spin_lock_irqsave(&rt->out_lock, flags);
|
||||
guard(spinlock_irqsave)(&rt->out_lock);
|
||||
|
||||
if (rt->out) {
|
||||
ret = snd_rawmidi_transmit(rt->out, rt->out_buffer + 4,
|
||||
@@ -43,18 +42,14 @@ static void usb6fire_midi_out_handler(struct urb *urb)
|
||||
} else /* no more data to transmit */
|
||||
rt->out = NULL;
|
||||
}
|
||||
spin_unlock_irqrestore(&rt->out_lock, flags);
|
||||
}
|
||||
|
||||
static void usb6fire_midi_in_received(
|
||||
struct midi_runtime *rt, u8 *data, int length)
|
||||
{
|
||||
unsigned long flags;
|
||||
|
||||
spin_lock_irqsave(&rt->in_lock, flags);
|
||||
guard(spinlock_irqsave)(&rt->in_lock);
|
||||
if (rt->in)
|
||||
snd_rawmidi_receive(rt->in, data, length);
|
||||
spin_unlock_irqrestore(&rt->in_lock, flags);
|
||||
}
|
||||
|
||||
static int usb6fire_midi_out_open(struct snd_rawmidi_substream *alsa_sub)
|
||||
@@ -73,14 +68,11 @@ static void usb6fire_midi_out_trigger(
|
||||
struct midi_runtime *rt = alsa_sub->rmidi->private_data;
|
||||
struct urb *urb = &rt->out_urb;
|
||||
__s8 ret;
|
||||
unsigned long flags;
|
||||
|
||||
spin_lock_irqsave(&rt->out_lock, flags);
|
||||
guard(spinlock_irqsave)(&rt->out_lock);
|
||||
if (up) { /* start transfer */
|
||||
if (rt->out) { /* we are already transmitting so just return */
|
||||
spin_unlock_irqrestore(&rt->out_lock, flags);
|
||||
if (rt->out) /* we are already transmitting so just return */
|
||||
return;
|
||||
}
|
||||
|
||||
ret = snd_rawmidi_transmit(alsa_sub, rt->out_buffer + 4,
|
||||
MIDI_BUFSIZE - 4);
|
||||
@@ -99,7 +91,6 @@ static void usb6fire_midi_out_trigger(
|
||||
}
|
||||
} else if (rt->out == alsa_sub)
|
||||
rt->out = NULL;
|
||||
spin_unlock_irqrestore(&rt->out_lock, flags);
|
||||
}
|
||||
|
||||
static void usb6fire_midi_out_drain(struct snd_rawmidi_substream *alsa_sub)
|
||||
@@ -125,14 +116,12 @@ static void usb6fire_midi_in_trigger(
|
||||
struct snd_rawmidi_substream *alsa_sub, int up)
|
||||
{
|
||||
struct midi_runtime *rt = alsa_sub->rmidi->private_data;
|
||||
unsigned long flags;
|
||||
|
||||
spin_lock_irqsave(&rt->in_lock, flags);
|
||||
guard(spinlock_irqsave)(&rt->in_lock);
|
||||
if (up)
|
||||
rt->in = alsa_sub;
|
||||
else
|
||||
rt->in = NULL;
|
||||
spin_unlock_irqrestore(&rt->in_lock, flags);
|
||||
}
|
||||
|
||||
static const struct snd_rawmidi_ops out_ops = {
|
||||
|
||||
@@ -289,7 +289,7 @@ static void usb6fire_pcm_in_urb_handler(struct urb *usb_urb)
|
||||
struct pcm_urb *out_urb = in_urb->peer;
|
||||
struct pcm_runtime *rt = in_urb->chip->pcm;
|
||||
struct pcm_substream *sub;
|
||||
unsigned long flags;
|
||||
bool period_elapsed;
|
||||
int total_length = 0;
|
||||
int frame_count;
|
||||
int frame;
|
||||
@@ -313,17 +313,18 @@ static void usb6fire_pcm_in_urb_handler(struct urb *usb_urb)
|
||||
|
||||
/* receive our capture data */
|
||||
sub = &rt->capture;
|
||||
spin_lock_irqsave(&sub->lock, flags);
|
||||
if (sub->active) {
|
||||
usb6fire_pcm_capture(sub, in_urb);
|
||||
if (sub->period_off >= sub->instance->runtime->period_size) {
|
||||
sub->period_off %= sub->instance->runtime->period_size;
|
||||
spin_unlock_irqrestore(&sub->lock, flags);
|
||||
snd_pcm_period_elapsed(sub->instance);
|
||||
} else
|
||||
spin_unlock_irqrestore(&sub->lock, flags);
|
||||
} else
|
||||
spin_unlock_irqrestore(&sub->lock, flags);
|
||||
period_elapsed = false;
|
||||
scoped_guard(spinlock_irqsave, &sub->lock) {
|
||||
if (sub->active) {
|
||||
usb6fire_pcm_capture(sub, in_urb);
|
||||
if (sub->period_off >= sub->instance->runtime->period_size) {
|
||||
sub->period_off %= sub->instance->runtime->period_size;
|
||||
period_elapsed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (period_elapsed)
|
||||
snd_pcm_period_elapsed(sub->instance);
|
||||
|
||||
/* setup out urb structure */
|
||||
for (i = 0; i < PCM_N_PACKETS_PER_URB; i++) {
|
||||
@@ -338,17 +339,18 @@ static void usb6fire_pcm_in_urb_handler(struct urb *usb_urb)
|
||||
|
||||
/* now send our playback data (if a free out urb was found) */
|
||||
sub = &rt->playback;
|
||||
spin_lock_irqsave(&sub->lock, flags);
|
||||
if (sub->active) {
|
||||
usb6fire_pcm_playback(sub, out_urb);
|
||||
if (sub->period_off >= sub->instance->runtime->period_size) {
|
||||
sub->period_off %= sub->instance->runtime->period_size;
|
||||
spin_unlock_irqrestore(&sub->lock, flags);
|
||||
snd_pcm_period_elapsed(sub->instance);
|
||||
} else
|
||||
spin_unlock_irqrestore(&sub->lock, flags);
|
||||
} else
|
||||
spin_unlock_irqrestore(&sub->lock, flags);
|
||||
period_elapsed = false;
|
||||
scoped_guard(spinlock_irqsave, &sub->lock) {
|
||||
if (sub->active) {
|
||||
usb6fire_pcm_playback(sub, out_urb);
|
||||
if (sub->period_off >= sub->instance->runtime->period_size) {
|
||||
sub->period_off %= sub->instance->runtime->period_size;
|
||||
period_elapsed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (period_elapsed)
|
||||
snd_pcm_period_elapsed(sub->instance);
|
||||
|
||||
/* setup the 4th byte of each sample (0x40 for analog channels) */
|
||||
dest = out_urb->buffer;
|
||||
@@ -392,7 +394,7 @@ static int usb6fire_pcm_open(struct snd_pcm_substream *alsa_sub)
|
||||
if (rt->panic)
|
||||
return -EPIPE;
|
||||
|
||||
mutex_lock(&rt->stream_mutex);
|
||||
guard(mutex)(&rt->stream_mutex);
|
||||
alsa_rt->hw = pcm_hw;
|
||||
|
||||
if (alsa_sub->stream == SNDRV_PCM_STREAM_PLAYBACK) {
|
||||
@@ -408,14 +410,12 @@ static int usb6fire_pcm_open(struct snd_pcm_substream *alsa_sub)
|
||||
}
|
||||
|
||||
if (!sub) {
|
||||
mutex_unlock(&rt->stream_mutex);
|
||||
dev_err(&rt->chip->dev->dev, "invalid stream type.\n");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
sub->instance = alsa_sub;
|
||||
sub->active = false;
|
||||
mutex_unlock(&rt->stream_mutex);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -423,18 +423,17 @@ static int usb6fire_pcm_close(struct snd_pcm_substream *alsa_sub)
|
||||
{
|
||||
struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
|
||||
struct pcm_substream *sub = usb6fire_pcm_get_substream(alsa_sub);
|
||||
unsigned long flags;
|
||||
|
||||
if (rt->panic)
|
||||
return 0;
|
||||
|
||||
mutex_lock(&rt->stream_mutex);
|
||||
guard(mutex)(&rt->stream_mutex);
|
||||
if (sub) {
|
||||
/* deactivate substream */
|
||||
spin_lock_irqsave(&sub->lock, flags);
|
||||
sub->instance = NULL;
|
||||
sub->active = false;
|
||||
spin_unlock_irqrestore(&sub->lock, flags);
|
||||
scoped_guard(spinlock_irqsave, &sub->lock) {
|
||||
sub->instance = NULL;
|
||||
sub->active = false;
|
||||
}
|
||||
|
||||
/* all substreams closed? if so, stop streaming */
|
||||
if (!rt->playback.instance && !rt->capture.instance) {
|
||||
@@ -442,7 +441,6 @@ static int usb6fire_pcm_close(struct snd_pcm_substream *alsa_sub)
|
||||
rt->rate = ARRAY_SIZE(rates);
|
||||
}
|
||||
}
|
||||
mutex_unlock(&rt->stream_mutex);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -458,7 +456,7 @@ static int usb6fire_pcm_prepare(struct snd_pcm_substream *alsa_sub)
|
||||
if (!sub)
|
||||
return -ENODEV;
|
||||
|
||||
mutex_lock(&rt->stream_mutex);
|
||||
guard(mutex)(&rt->stream_mutex);
|
||||
sub->dma_off = 0;
|
||||
sub->period_off = 0;
|
||||
|
||||
@@ -467,7 +465,6 @@ static int usb6fire_pcm_prepare(struct snd_pcm_substream *alsa_sub)
|
||||
if (alsa_rt->rate == rates[rt->rate])
|
||||
break;
|
||||
if (rt->rate == ARRAY_SIZE(rates)) {
|
||||
mutex_unlock(&rt->stream_mutex);
|
||||
dev_err(&rt->chip->dev->dev,
|
||||
"invalid rate %d in prepare.\n",
|
||||
alsa_rt->rate);
|
||||
@@ -475,19 +472,15 @@ static int usb6fire_pcm_prepare(struct snd_pcm_substream *alsa_sub)
|
||||
}
|
||||
|
||||
ret = usb6fire_pcm_set_rate(rt);
|
||||
if (ret) {
|
||||
mutex_unlock(&rt->stream_mutex);
|
||||
if (ret)
|
||||
return ret;
|
||||
}
|
||||
ret = usb6fire_pcm_stream_start(rt);
|
||||
if (ret) {
|
||||
mutex_unlock(&rt->stream_mutex);
|
||||
dev_err(&rt->chip->dev->dev,
|
||||
"could not start pcm stream.\n");
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
mutex_unlock(&rt->stream_mutex);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -495,26 +488,22 @@ static int usb6fire_pcm_trigger(struct snd_pcm_substream *alsa_sub, int cmd)
|
||||
{
|
||||
struct pcm_substream *sub = usb6fire_pcm_get_substream(alsa_sub);
|
||||
struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
|
||||
unsigned long flags;
|
||||
|
||||
if (rt->panic)
|
||||
return -EPIPE;
|
||||
if (!sub)
|
||||
return -ENODEV;
|
||||
|
||||
guard(spinlock_irqsave)(&sub->lock);
|
||||
switch (cmd) {
|
||||
case SNDRV_PCM_TRIGGER_START:
|
||||
case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
|
||||
spin_lock_irqsave(&sub->lock, flags);
|
||||
sub->active = true;
|
||||
spin_unlock_irqrestore(&sub->lock, flags);
|
||||
return 0;
|
||||
|
||||
case SNDRV_PCM_TRIGGER_STOP:
|
||||
case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
|
||||
spin_lock_irqsave(&sub->lock, flags);
|
||||
sub->active = false;
|
||||
spin_unlock_irqrestore(&sub->lock, flags);
|
||||
return 0;
|
||||
|
||||
default:
|
||||
@@ -527,15 +516,13 @@ static snd_pcm_uframes_t usb6fire_pcm_pointer(
|
||||
{
|
||||
struct pcm_substream *sub = usb6fire_pcm_get_substream(alsa_sub);
|
||||
struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
|
||||
unsigned long flags;
|
||||
snd_pcm_uframes_t ret;
|
||||
|
||||
if (rt->panic || !sub)
|
||||
return SNDRV_PCM_POS_XRUN;
|
||||
|
||||
spin_lock_irqsave(&sub->lock, flags);
|
||||
guard(spinlock_irqsave)(&sub->lock);
|
||||
ret = sub->dma_off;
|
||||
spin_unlock_irqrestore(&sub->lock, flags);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user