Skip to content

Commit 26d87c5

Browse files
committed
ALSA: korg1212: Replace the pending stop check code with sync_stop PCM ops
The korg1212 driver has an overly complex code with the timer API to check the pending stop operation and to sync at the prepare stage. This very same thing can be achieved more simply by the PCM sync_stop ops without timer, and this patch implements it. Along with the rewrite to sync_stop ops, the flag dsp_stop_is_processed is replaced with dsp_stop_processing to indicate the stop operation is pending, not to indicate it's finished. In that way, wait_for_event() can be used more straightforwardly. Link: https://lore.kernel.org/590769506CF46967+20250414042629.63019-5-wangyuli@uniontech.com Link: https://patch.msgid.link/20250415155522.7998-1-tiwai@suse.de Signed-off-by: Takashi Iwai <tiwai@suse.de>
1 parent bdd9ea9 commit 26d87c5

1 file changed

Lines changed: 19 additions & 56 deletions

File tree

sound/pci/korg1212/korg1212.c

Lines changed: 19 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,6 @@ struct snd_korg1212 {
308308
spinlock_t lock;
309309
struct mutex open_mutex;
310310

311-
struct timer_list timer; /* timer callback for checking ack of stop request */
312-
int stop_pending_cnt; /* counter for stop pending check */
313-
314311
wait_queue_head_t wait;
315312

316313
unsigned long iomem;
@@ -382,7 +379,7 @@ struct snd_korg1212 {
382379
unsigned long totalerrorcnt; // Total Error Count
383380

384381
int dsp_is_loaded;
385-
int dsp_stop_is_processed;
382+
int dsp_stop_processing;
386383

387384
};
388385

@@ -565,52 +562,17 @@ static int snd_korg1212_Send1212Command(struct snd_korg1212 *korg1212,
565562
/* spinlock already held */
566563
static void snd_korg1212_SendStop(struct snd_korg1212 *korg1212)
567564
{
568-
if (! korg1212->stop_pending_cnt) {
569-
korg1212->sharedBufferPtr->cardCommand = 0xffffffff;
570-
/* program the timer */
571-
korg1212->stop_pending_cnt = HZ;
572-
mod_timer(&korg1212->timer, jiffies + 1);
573-
}
565+
korg1212->dsp_stop_processing = 1;
566+
korg1212->sharedBufferPtr->cardCommand = 0xffffffff;
574567
}
575568

576569
static void snd_korg1212_SendStopAndWait(struct snd_korg1212 *korg1212)
577570
{
578571
unsigned long flags;
579572
spin_lock_irqsave(&korg1212->lock, flags);
580-
korg1212->dsp_stop_is_processed = 0;
581573
snd_korg1212_SendStop(korg1212);
582574
spin_unlock_irqrestore(&korg1212->lock, flags);
583-
wait_event_timeout(korg1212->wait, korg1212->dsp_stop_is_processed, (HZ * 3) / 2);
584-
}
585-
586-
/* timer callback for checking the ack of stop request */
587-
static void snd_korg1212_timer_func(struct timer_list *t)
588-
{
589-
struct snd_korg1212 *korg1212 = from_timer(korg1212, t, timer);
590-
unsigned long flags;
591-
592-
spin_lock_irqsave(&korg1212->lock, flags);
593-
if (korg1212->sharedBufferPtr->cardCommand == 0) {
594-
/* ack'ed */
595-
korg1212->stop_pending_cnt = 0;
596-
korg1212->dsp_stop_is_processed = 1;
597-
wake_up(&korg1212->wait);
598-
K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Stop ack'ed [%s]\n",
599-
stateName[korg1212->cardState]);
600-
} else {
601-
if (--korg1212->stop_pending_cnt > 0) {
602-
/* reprogram timer */
603-
mod_timer(&korg1212->timer, jiffies + 1);
604-
} else {
605-
dev_dbg(korg1212->card->dev, "korg1212_timer_func timeout\n");
606-
korg1212->sharedBufferPtr->cardCommand = 0;
607-
korg1212->dsp_stop_is_processed = 1;
608-
wake_up(&korg1212->wait);
609-
K1212_DEBUG_PRINTK("K1212_DEBUG: Stop timeout [%s]\n",
610-
stateName[korg1212->cardState]);
611-
}
612-
}
613-
spin_unlock_irqrestore(&korg1212->lock, flags);
575+
wait_event_timeout(korg1212->wait, !korg1212->dsp_stop_processing, HZ);
614576
}
615577

616578
static int snd_korg1212_TurnOnIdleMonitor(struct snd_korg1212 *korg1212)
@@ -1135,7 +1097,9 @@ static irqreturn_t snd_korg1212_interrupt(int irq, void *dev_id)
11351097
korg1212->errorcnt++;
11361098
korg1212->totalerrorcnt++;
11371099
korg1212->sharedBufferPtr->cardCommand = 0;
1100+
korg1212->dsp_stop_processing = 0;
11381101
snd_korg1212_setCardState(korg1212, K1212_STATE_ERRORSTOP);
1102+
wake_up(&korg1212->wait);
11391103
break;
11401104

11411105
// ------------------------------------------------------------------------
@@ -1147,6 +1111,8 @@ static irqreturn_t snd_korg1212_interrupt(int irq, void *dev_id)
11471111
korg1212->irqcount, doorbellValue,
11481112
stateName[korg1212->cardState]);
11491113
korg1212->sharedBufferPtr->cardCommand = 0;
1114+
korg1212->dsp_stop_processing = 0;
1115+
wake_up(&korg1212->wait);
11501116
break;
11511117

11521118
default:
@@ -1535,6 +1501,14 @@ static int snd_korg1212_hw_params(struct snd_pcm_substream *substream,
15351501
return 0;
15361502
}
15371503

1504+
static int snd_korg1212_sync_stop(struct snd_pcm_substream *substream)
1505+
{
1506+
struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1507+
1508+
wait_event_timeout(korg1212->wait, !korg1212->dsp_stop_processing, HZ);
1509+
return 0;
1510+
}
1511+
15381512
static int snd_korg1212_prepare(struct snd_pcm_substream *substream)
15391513
{
15401514
struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
@@ -1544,19 +1518,7 @@ static int snd_korg1212_prepare(struct snd_pcm_substream *substream)
15441518
stateName[korg1212->cardState]);
15451519

15461520
spin_lock_irq(&korg1212->lock);
1547-
1548-
/* FIXME: we should wait for ack! */
1549-
if (korg1212->stop_pending_cnt > 0) {
1550-
K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_prepare - Stop is pending... [%s]\n",
1551-
stateName[korg1212->cardState]);
1552-
spin_unlock_irq(&korg1212->lock);
1553-
return -EAGAIN;
1554-
/*
1555-
korg1212->sharedBufferPtr->cardCommand = 0;
1556-
del_timer(&korg1212->timer);
1557-
korg1212->stop_pending_cnt = 0;
1558-
*/
1559-
}
1521+
korg1212->dsp_stop_processing = 0;
15601522

15611523
rc = snd_korg1212_SetupForPlay(korg1212);
15621524

@@ -1668,6 +1630,7 @@ static const struct snd_pcm_ops snd_korg1212_playback_ops = {
16681630
.hw_params = snd_korg1212_hw_params,
16691631
.prepare = snd_korg1212_prepare,
16701632
.trigger = snd_korg1212_trigger,
1633+
.sync_stop = snd_korg1212_sync_stop,
16711634
.pointer = snd_korg1212_playback_pointer,
16721635
.copy = snd_korg1212_playback_copy,
16731636
.fill_silence = snd_korg1212_playback_silence,
@@ -1680,6 +1643,7 @@ static const struct snd_pcm_ops snd_korg1212_capture_ops = {
16801643
.hw_params = snd_korg1212_hw_params,
16811644
.prepare = snd_korg1212_prepare,
16821645
.trigger = snd_korg1212_trigger,
1646+
.sync_stop = snd_korg1212_sync_stop,
16831647
.pointer = snd_korg1212_capture_pointer,
16841648
.copy = snd_korg1212_capture_copy,
16851649
};
@@ -2086,7 +2050,6 @@ static int snd_korg1212_create(struct snd_card *card, struct pci_dev *pci)
20862050
init_waitqueue_head(&korg1212->wait);
20872051
spin_lock_init(&korg1212->lock);
20882052
mutex_init(&korg1212->open_mutex);
2089-
timer_setup(&korg1212->timer, snd_korg1212_timer_func, 0);
20902053

20912054
korg1212->irq = -1;
20922055
korg1212->clkSource = K1212_CLKIDX_Local;

0 commit comments

Comments
 (0)