Commit d20b9b20 authored by Takashi Iwai's avatar Takashi Iwai Committed by Jaroslav Kysela

[ALSA] aica - fix behaviour in poor resource conditions

Fix behaviour in poor resource conditions.  With this new patch,
tidied with indent and with SNDRV_PCM_TRIGGER_STOP being used.
Signed-off by: Adrian McMenamin <adrian@mcmen.demon.co.uk>
Signed-off-by: default avatarTakashi Iwai <tiwai@suse.de>
Signed-off-by: default avatarJaroslav Kysela <perex@suse.cz>
parent 43bbb6cc
...@@ -64,12 +64,6 @@ module_param(enable, bool, 0644); ...@@ -64,12 +64,6 @@ module_param(enable, bool, 0644);
MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard."); MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
/* Use workqueue */ /* Use workqueue */
static struct spu_work_holder {
struct work_struct spu_dma_work;
void *sspointer;
} spu_working;
static struct workqueue_struct *aica_queue; static struct workqueue_struct *aica_queue;
/* Simple platform device */ /* Simple platform device */
...@@ -100,9 +94,9 @@ static void spu_write_wait(void) ...@@ -100,9 +94,9 @@ static void spu_write_wait(void)
break; break;
/* To ensure hardware failure doesn't wedge kernel */ /* To ensure hardware failure doesn't wedge kernel */
time_count++; time_count++;
if (time_count > 0x10000) if (time_count > 0x10000) {
{ snd_printk
snd_printk("WARNING: G2 FIFO appears to be blocked.\n"); ("WARNING: G2 FIFO appears to be blocked.\n");
break; break;
} }
} }
...@@ -226,11 +220,11 @@ static int aica_dma_transfer(int channels, int buffer_size, ...@@ -226,11 +220,11 @@ static int aica_dma_transfer(int channels, int buffer_size,
runtime = substream->runtime; runtime = substream->runtime;
for (q = 0; q < channels; q++) { for (q = 0; q < channels; q++) {
err = dma_xfer(AICA_DMA_CHANNEL, err = dma_xfer(AICA_DMA_CHANNEL,
(unsigned long)(runtime->dma_area + (unsigned long) (runtime->dma_area +
(AICA_BUFFER_SIZE * q) / (AICA_BUFFER_SIZE * q) /
channels + channels +
AICA_PERIOD_SIZE * AICA_PERIOD_SIZE *
period_offset), period_offset),
AICA_CHANNEL0_OFFSET + q * CHANNEL_OFFSET + AICA_CHANNEL0_OFFSET + q * CHANNEL_OFFSET +
AICA_PERIOD_SIZE * period_offset, AICA_PERIOD_SIZE * period_offset,
buffer_size / channels, AICA_DMA_MODE); buffer_size / channels, AICA_DMA_MODE);
...@@ -244,26 +238,25 @@ static int aica_dma_transfer(int channels, int buffer_size, ...@@ -244,26 +238,25 @@ static int aica_dma_transfer(int channels, int buffer_size,
static void startup_aica(struct snd_card_aica *dreamcastcard) static void startup_aica(struct snd_card_aica *dreamcastcard)
{ {
spu_memload(AICA_CHANNEL0_CONTROL_OFFSET, spu_memload(AICA_CHANNEL0_CONTROL_OFFSET,
dreamcastcard->channel, dreamcastcard->channel, sizeof(struct aica_channel));
sizeof(struct aica_channel));
aica_chn_start(); aica_chn_start();
} }
static void run_spu_dma(struct work_struct *work) static void run_spu_dma(struct work_struct *work)
{ {
int buffer_size; int buffer_size;
struct snd_pcm_substream *substream;
struct snd_pcm_runtime *runtime; struct snd_pcm_runtime *runtime;
struct snd_card_aica *dreamcastcard; struct snd_card_aica *dreamcastcard;
struct spu_work_holder *holder = container_of(work, struct spu_work_holder, spu_dma_work); dreamcastcard =
substream = holder-> sspointer; container_of(work, struct snd_card_aica, spu_dma_work);
dreamcastcard = substream->pcm->private_data; runtime = dreamcastcard->substream->runtime;
runtime = substream->runtime;
if (unlikely(dreamcastcard->dma_check == 0)) { if (unlikely(dreamcastcard->dma_check == 0)) {
buffer_size = frames_to_bytes(runtime, runtime->buffer_size); buffer_size =
frames_to_bytes(runtime, runtime->buffer_size);
if (runtime->channels > 1) if (runtime->channels > 1)
dreamcastcard->channel->flags |= 0x01; dreamcastcard->channel->flags |= 0x01;
aica_dma_transfer(runtime->channels, buffer_size, substream); aica_dma_transfer(runtime->channels, buffer_size,
dreamcastcard->substream);
startup_aica(dreamcastcard); startup_aica(dreamcastcard);
dreamcastcard->clicks = dreamcastcard->clicks =
buffer_size / (AICA_PERIOD_SIZE * runtime->channels); buffer_size / (AICA_PERIOD_SIZE * runtime->channels);
...@@ -271,13 +264,11 @@ static void run_spu_dma(struct work_struct *work) ...@@ -271,13 +264,11 @@ static void run_spu_dma(struct work_struct *work)
} else { } else {
aica_dma_transfer(runtime->channels, aica_dma_transfer(runtime->channels,
AICA_PERIOD_SIZE * runtime->channels, AICA_PERIOD_SIZE * runtime->channels,
substream); dreamcastcard->substream);
snd_pcm_period_elapsed(dreamcastcard->substream); snd_pcm_period_elapsed(dreamcastcard->substream);
dreamcastcard->clicks++; dreamcastcard->clicks++;
if (unlikely(dreamcastcard->clicks >= AICA_PERIOD_NUMBER)) if (unlikely(dreamcastcard->clicks >= AICA_PERIOD_NUMBER))
{
dreamcastcard->clicks %= AICA_PERIOD_NUMBER; dreamcastcard->clicks %= AICA_PERIOD_NUMBER;
}
mod_timer(&dreamcastcard->timer, jiffies + 1); mod_timer(&dreamcastcard->timer, jiffies + 1);
} }
} }
...@@ -289,7 +280,7 @@ static void aica_period_elapsed(unsigned long timer_var) ...@@ -289,7 +280,7 @@ static void aica_period_elapsed(unsigned long timer_var)
struct snd_pcm_runtime *runtime; struct snd_pcm_runtime *runtime;
struct snd_pcm_substream *substream; struct snd_pcm_substream *substream;
struct snd_card_aica *dreamcastcard; struct snd_card_aica *dreamcastcard;
substream = (struct snd_pcm_substream *)timer_var; substream = (struct snd_pcm_substream *) timer_var;
runtime = substream->runtime; runtime = substream->runtime;
dreamcastcard = substream->pcm->private_data; dreamcastcard = substream->pcm->private_data;
/* Have we played out an additional period? */ /* Have we played out an additional period? */
...@@ -307,27 +298,24 @@ static void aica_period_elapsed(unsigned long timer_var) ...@@ -307,27 +298,24 @@ static void aica_period_elapsed(unsigned long timer_var)
dreamcastcard->current_period = play_period; dreamcastcard->current_period = play_period;
if (unlikely(dreamcastcard->dma_check == 0)) if (unlikely(dreamcastcard->dma_check == 0))
dreamcastcard->dma_check = 1; dreamcastcard->dma_check = 1;
queue_work(aica_queue, &(spu_working.spu_dma_work)); queue_work(aica_queue, &(dreamcastcard->spu_dma_work));
} }
static void spu_begin_dma(struct snd_pcm_substream *substream) static void spu_begin_dma(struct snd_pcm_substream *substream)
{ {
/* Must be atomic */
struct snd_card_aica *dreamcastcard; struct snd_card_aica *dreamcastcard;
struct snd_pcm_runtime *runtime; struct snd_pcm_runtime *runtime;
runtime = substream->runtime; runtime = substream->runtime;
dreamcastcard = substream->pcm->private_data; dreamcastcard = substream->pcm->private_data;
/* Use queue to do the heavy lifting */ /*get the queue to do the work */
spu_working.sspointer = substream; queue_work(aica_queue, &(dreamcastcard->spu_dma_work));
INIT_WORK(&(spu_working.spu_dma_work), run_spu_dma);
queue_work(aica_queue, &(spu_working.spu_dma_work));
/* Timer may already be running */ /* Timer may already be running */
if (unlikely(dreamcastcard->timer.data)) { if (unlikely(dreamcastcard->timer.data)) {
mod_timer(&dreamcastcard->timer, jiffies + 4); mod_timer(&dreamcastcard->timer, jiffies + 4);
return; return;
} }
init_timer(&(dreamcastcard->timer)); init_timer(&(dreamcastcard->timer));
dreamcastcard->timer.data = (unsigned long)substream; dreamcastcard->timer.data = (unsigned long) substream;
dreamcastcard->timer.function = aica_period_elapsed; dreamcastcard->timer.function = aica_period_elapsed;
dreamcastcard->timer.expires = jiffies + 4; dreamcastcard->timer.expires = jiffies + 4;
add_timer(&(dreamcastcard->timer)); add_timer(&(dreamcastcard->timer));
...@@ -366,7 +354,9 @@ static int snd_aicapcm_pcm_close(struct snd_pcm_substream ...@@ -366,7 +354,9 @@ static int snd_aicapcm_pcm_close(struct snd_pcm_substream
*substream) *substream)
{ {
struct snd_card_aica *dreamcastcard = substream->pcm->private_data; struct snd_card_aica *dreamcastcard = substream->pcm->private_data;
del_timer(&dreamcastcard->timer); flush_workqueue(aica_queue);
if (dreamcastcard->timer.data)
del_timer(&dreamcastcard->timer);
kfree(dreamcastcard->channel); kfree(dreamcastcard->channel);
spu_disable(); spu_disable();
return 0; return 0;
...@@ -385,7 +375,8 @@ static int snd_aicapcm_pcm_hw_params(struct snd_pcm_substream ...@@ -385,7 +375,8 @@ static int snd_aicapcm_pcm_hw_params(struct snd_pcm_substream
{ {
/* Allocate a DMA buffer using ALSA built-ins */ /* Allocate a DMA buffer using ALSA built-ins */
return return
snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params)); snd_pcm_lib_malloc_pages(substream,
params_buffer_bytes(hw_params));
} }
static int snd_aicapcm_pcm_prepare(struct snd_pcm_substream static int snd_aicapcm_pcm_prepare(struct snd_pcm_substream
...@@ -402,15 +393,11 @@ static int snd_aicapcm_pcm_prepare(struct snd_pcm_substream ...@@ -402,15 +393,11 @@ static int snd_aicapcm_pcm_prepare(struct snd_pcm_substream
static int snd_aicapcm_pcm_trigger(struct snd_pcm_substream static int snd_aicapcm_pcm_trigger(struct snd_pcm_substream
*substream, int cmd) *substream, int cmd)
{ {
struct snd_card_aica *dreamcastcard;
switch (cmd) { switch (cmd) {
case SNDRV_PCM_TRIGGER_START: case SNDRV_PCM_TRIGGER_START:
spu_begin_dma(substream); spu_begin_dma(substream);
break; break;
case SNDRV_PCM_TRIGGER_STOP: case SNDRV_PCM_TRIGGER_STOP:
dreamcastcard = substream->pcm->private_data;
if (dreamcastcard->timer.data)
del_timer(&dreamcastcard->timer);
aica_chn_halt(); aica_chn_halt();
break; break;
default: default:
...@@ -444,7 +431,8 @@ static int __init snd_aicapcmchip(struct snd_card_aica ...@@ -444,7 +431,8 @@ static int __init snd_aicapcmchip(struct snd_card_aica
int err; int err;
/* AICA has no capture ability */ /* AICA has no capture ability */
err = err =
snd_pcm_new(dreamcastcard->card, "AICA PCM", pcm_index, 1, 0, &pcm); snd_pcm_new(dreamcastcard->card, "AICA PCM", pcm_index, 1, 0,
&pcm);
if (unlikely(err < 0)) if (unlikely(err < 0))
return err; return err;
pcm->private_data = dreamcastcard; pcm->private_data = dreamcastcard;
...@@ -524,9 +512,7 @@ static int aica_pcmvolume_put(struct snd_kcontrol *kcontrol, ...@@ -524,9 +512,7 @@ static int aica_pcmvolume_put(struct snd_kcontrol *kcontrol,
dreamcastcard->channel->vol = ucontrol->value.integer.value[0]; dreamcastcard->channel->vol = ucontrol->value.integer.value[0];
dreamcastcard->master_volume = ucontrol->value.integer.value[0]; dreamcastcard->master_volume = ucontrol->value.integer.value[0];
spu_memload(AICA_CHANNEL0_CONTROL_OFFSET, spu_memload(AICA_CHANNEL0_CONTROL_OFFSET,
dreamcastcard->channel, dreamcastcard->channel, sizeof(struct aica_channel));
sizeof(struct aica_channel));
return 1; return 1;
} }
...@@ -610,6 +596,8 @@ static int __init snd_aica_probe(struct platform_device *devptr) ...@@ -610,6 +596,8 @@ static int __init snd_aica_probe(struct platform_device *devptr)
strcpy(dreamcastcard->card->shortname, SND_AICA_DRIVER); strcpy(dreamcastcard->card->shortname, SND_AICA_DRIVER);
strcpy(dreamcastcard->card->longname, strcpy(dreamcastcard->card->longname,
"Yamaha AICA Super Intelligent Sound Processor for SEGA Dreamcast"); "Yamaha AICA Super Intelligent Sound Processor for SEGA Dreamcast");
/* Prepare to use the queue */
INIT_WORK(&(dreamcastcard->spu_dma_work), run_spu_dma);
/* Load the PCM 'chip' */ /* Load the PCM 'chip' */
err = snd_aicapcmchip(dreamcastcard, 0); err = snd_aicapcmchip(dreamcastcard, 0);
if (unlikely(err < 0)) if (unlikely(err < 0))
...@@ -663,8 +651,10 @@ static int __init aica_init(void) ...@@ -663,8 +651,10 @@ static int __init aica_init(void)
static void __exit aica_exit(void) static void __exit aica_exit(void)
{ {
/* Destroy the aica kernel thread */ /* Destroy the aica kernel thread *
destroy_workqueue(aica_queue); * being extra cautious to check if it exists*/
if (likely(aica_queue))
destroy_workqueue(aica_queue);
platform_device_unregister(pd); platform_device_unregister(pd);
platform_driver_unregister(&snd_aica_driver); platform_driver_unregister(&snd_aica_driver);
/* Kill any sound still playing and reset ARM7 to safe state */ /* Kill any sound still playing and reset ARM7 to safe state */
......
...@@ -69,6 +69,7 @@ struct aica_channel { ...@@ -69,6 +69,7 @@ struct aica_channel {
}; };
struct snd_card_aica { struct snd_card_aica {
struct work_struct spu_dma_work;
struct snd_card *card; struct snd_card *card;
struct aica_channel *channel; struct aica_channel *channel;
struct snd_pcm_substream *substream; struct snd_pcm_substream *substream;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment