Commit d7ca3a71 authored by Takashi Iwai's avatar Takashi Iwai Committed by Greg Kroah-Hartman

staging: bcm2835-audio: Operate non-atomic PCM ops

This is the most significant part in the patch series.

The bcm2835-audio driver used to queue the commands to vc04 core via
workqueue, but basically the whole accesses to vc04 core are done in
the sleepable context, including the callback calls.  In such a case,
rewriting the code using non-atomic PCM ops will simplify the logic a
lot.

This patch does it: all workqueue are gone and each former-work
implementation is now directly called from PCM ops like trigger and
write transfer.

Along with it, the DMA position updater, bcm2835_playback_fifo(), was
also rewritten to use a simpler logic.  Now it handles the XRUN and
draining properly by calling snd_pcm_stop() conditionally.

The current position is kept in atomic_t value so that it can be read
concurrently from the pointer callback.

Also, the bcm2835_audio_instance object is allocated at the beginning
of bcm2835_audio_open().  This makes the resource management clearer.
Signed-off-by: default avatarTakashi Iwai <tiwai@suse.de>
Tested-by: default avatarStefan Wahren <stefan.wahren@i2se.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 0307363a
...@@ -11,7 +11,8 @@ ...@@ -11,7 +11,8 @@
/* hardware definition */ /* hardware definition */
static const struct snd_pcm_hardware snd_bcm2835_playback_hw = { static const struct snd_pcm_hardware snd_bcm2835_playback_hw = {
.info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER | .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID), SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
SNDRV_PCM_INFO_DRAIN_TRIGGER),
.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE, .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
.rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000, .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
.rate_min = 8000, .rate_min = 8000,
...@@ -27,7 +28,8 @@ static const struct snd_pcm_hardware snd_bcm2835_playback_hw = { ...@@ -27,7 +28,8 @@ static const struct snd_pcm_hardware snd_bcm2835_playback_hw = {
static const struct snd_pcm_hardware snd_bcm2835_playback_spdif_hw = { static const struct snd_pcm_hardware snd_bcm2835_playback_spdif_hw = {
.info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER | .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID), SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
SNDRV_PCM_INFO_DRAIN_TRIGGER),
.formats = SNDRV_PCM_FMTBIT_S16_LE, .formats = SNDRV_PCM_FMTBIT_S16_LE,
.rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_44100 | .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_44100 |
SNDRV_PCM_RATE_48000, SNDRV_PCM_RATE_48000,
...@@ -47,42 +49,34 @@ static void snd_bcm2835_playback_free(struct snd_pcm_runtime *runtime) ...@@ -47,42 +49,34 @@ static void snd_bcm2835_playback_free(struct snd_pcm_runtime *runtime)
kfree(runtime->private_data); kfree(runtime->private_data);
} }
void bcm2835_playback_fifo(struct bcm2835_alsa_stream *alsa_stream) void bcm2835_playback_fifo(struct bcm2835_alsa_stream *alsa_stream,
unsigned int bytes)
{ {
unsigned int consumed = 0; struct snd_pcm_substream *substream = alsa_stream->substream;
int new_period = 0; unsigned int pos;
audio_info("alsa_stream=%p substream=%p\n", alsa_stream, if (!alsa_stream->period_size)
alsa_stream ? alsa_stream->substream : 0); return;
consumed = bcm2835_audio_retrieve_buffers(alsa_stream); if (bytes >= alsa_stream->buffer_size) {
snd_pcm_stream_lock(substream);
/* We get called only if playback was triggered, So, the number of buffers we retrieve in snd_pcm_stop(substream,
* each iteration are the buffers that have been played out already alsa_stream->draining ?
*/ SNDRV_PCM_STATE_SETUP :
SNDRV_PCM_STATE_XRUN);
if (alsa_stream->period_size) { snd_pcm_stream_unlock(substream);
if ((alsa_stream->pos / alsa_stream->period_size) != return;
((alsa_stream->pos + consumed) / alsa_stream->period_size))
new_period = 1;
}
audio_debug("updating pos cur: %d + %d max:%d period_bytes:%d, hw_ptr: %d new_period:%d\n",
alsa_stream->pos,
consumed,
alsa_stream->buffer_size,
(int) (alsa_stream->period_size * alsa_stream->substream->runtime->periods),
frames_to_bytes(alsa_stream->substream->runtime, alsa_stream->substream->runtime->status->hw_ptr),
new_period);
if (alsa_stream->buffer_size) {
alsa_stream->pos += consumed & ~(1 << 30);
alsa_stream->pos %= alsa_stream->buffer_size;
} }
if (alsa_stream->substream) { pos = atomic_read(&alsa_stream->pos);
if (new_period) pos += bytes;
snd_pcm_period_elapsed(alsa_stream->substream); pos %= alsa_stream->buffer_size;
} else { atomic_set(&alsa_stream->pos, pos);
audio_warning(" unexpected NULL substream\n");
alsa_stream->period_offset += bytes;
if (alsa_stream->period_offset >= alsa_stream->period_size) {
alsa_stream->period_offset %= alsa_stream->period_size;
snd_pcm_period_elapsed(substream);
} }
} }
...@@ -246,7 +240,8 @@ static int snd_bcm2835_pcm_prepare(struct snd_pcm_substream *substream) ...@@ -246,7 +240,8 @@ static int snd_bcm2835_pcm_prepare(struct snd_pcm_substream *substream)
alsa_stream->buffer_size = snd_pcm_lib_buffer_bytes(substream); alsa_stream->buffer_size = snd_pcm_lib_buffer_bytes(substream);
alsa_stream->period_size = snd_pcm_lib_period_bytes(substream); alsa_stream->period_size = snd_pcm_lib_period_bytes(substream);
alsa_stream->pos = 0; atomic_set(&alsa_stream->pos, 0);
alsa_stream->period_offset = 0;
alsa_stream->draining = false; alsa_stream->draining = false;
return 0; return 0;
...@@ -283,7 +278,7 @@ static int snd_bcm2835_pcm_trigger(struct snd_pcm_substream *substream, int cmd) ...@@ -283,7 +278,7 @@ static int snd_bcm2835_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
return bcm2835_audio_start(alsa_stream); return bcm2835_audio_start(alsa_stream);
case SNDRV_PCM_TRIGGER_DRAIN: case SNDRV_PCM_TRIGGER_DRAIN:
alsa_stream->draining = true; alsa_stream->draining = true;
return 0; return bcm2835_audio_drain(alsa_stream);
case SNDRV_PCM_TRIGGER_STOP: case SNDRV_PCM_TRIGGER_STOP:
return bcm2835_audio_stop(alsa_stream); return bcm2835_audio_stop(alsa_stream);
default: default:
...@@ -300,7 +295,7 @@ snd_bcm2835_pcm_pointer(struct snd_pcm_substream *substream) ...@@ -300,7 +295,7 @@ snd_bcm2835_pcm_pointer(struct snd_pcm_substream *substream)
return snd_pcm_indirect_playback_pointer(substream, return snd_pcm_indirect_playback_pointer(substream,
&alsa_stream->pcm_indirect, &alsa_stream->pcm_indirect,
alsa_stream->pos); atomic_read(&alsa_stream->pos));
} }
/* operators */ /* operators */
...@@ -338,6 +333,7 @@ int snd_bcm2835_new_pcm(struct bcm2835_chip *chip, u32 numchannels) ...@@ -338,6 +333,7 @@ int snd_bcm2835_new_pcm(struct bcm2835_chip *chip, u32 numchannels)
if (err < 0) if (err < 0)
return err; return err;
pcm->private_data = chip; pcm->private_data = chip;
pcm->nonatomic = true;
strcpy(pcm->name, "bcm2835 ALSA"); strcpy(pcm->name, "bcm2835 ALSA");
chip->pcm = pcm; chip->pcm = pcm;
chip->dest = AUDIO_DEST_AUTO; chip->dest = AUDIO_DEST_AUTO;
...@@ -367,6 +363,7 @@ int snd_bcm2835_new_spdif_pcm(struct bcm2835_chip *chip) ...@@ -367,6 +363,7 @@ int snd_bcm2835_new_spdif_pcm(struct bcm2835_chip *chip)
return err; return err;
pcm->private_data = chip; pcm->private_data = chip;
pcm->nonatomic = true;
strcpy(pcm->name, "bcm2835 IEC958/HDMI"); strcpy(pcm->name, "bcm2835 IEC958/HDMI");
chip->pcm_spdif = pcm; chip->pcm_spdif = pcm;
snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
...@@ -395,6 +392,7 @@ int snd_bcm2835_new_simple_pcm(struct bcm2835_chip *chip, ...@@ -395,6 +392,7 @@ int snd_bcm2835_new_simple_pcm(struct bcm2835_chip *chip,
return err; return err;
pcm->private_data = chip; pcm->private_data = chip;
pcm->nonatomic = true;
strcpy(pcm->name, name); strcpy(pcm->name, name);
chip->pcm = pcm; chip->pcm = pcm;
chip->dest = route; chip->dest = route;
......
...@@ -121,13 +121,12 @@ struct bcm2835_alsa_stream { ...@@ -121,13 +121,12 @@ struct bcm2835_alsa_stream {
int draining; int draining;
unsigned int pos; atomic_t pos;
unsigned int period_offset;
unsigned int buffer_size; unsigned int buffer_size;
unsigned int period_size; unsigned int period_size;
atomic_t retrieved;
struct bcm2835_audio_instance *instance; struct bcm2835_audio_instance *instance;
struct workqueue_struct *my_wq;
int idx; int idx;
}; };
...@@ -152,11 +151,13 @@ int bcm2835_audio_set_params(struct bcm2835_alsa_stream *alsa_stream, ...@@ -152,11 +151,13 @@ int bcm2835_audio_set_params(struct bcm2835_alsa_stream *alsa_stream,
unsigned int bps); unsigned int bps);
int bcm2835_audio_start(struct bcm2835_alsa_stream *alsa_stream); int bcm2835_audio_start(struct bcm2835_alsa_stream *alsa_stream);
int bcm2835_audio_stop(struct bcm2835_alsa_stream *alsa_stream); int bcm2835_audio_stop(struct bcm2835_alsa_stream *alsa_stream);
int bcm2835_audio_drain(struct bcm2835_alsa_stream *alsa_stream);
int bcm2835_audio_set_ctls(struct bcm2835_alsa_stream *alsa_stream); int bcm2835_audio_set_ctls(struct bcm2835_alsa_stream *alsa_stream);
int bcm2835_audio_write(struct bcm2835_alsa_stream *alsa_stream, int bcm2835_audio_write(struct bcm2835_alsa_stream *alsa_stream,
unsigned int count, unsigned int count,
void *src); void *src);
void bcm2835_playback_fifo(struct bcm2835_alsa_stream *alsa_stream); void bcm2835_playback_fifo(struct bcm2835_alsa_stream *alsa_stream,
unsigned int size);
unsigned int bcm2835_audio_retrieve_buffers(struct bcm2835_alsa_stream *alsa_stream); unsigned int bcm2835_audio_retrieve_buffers(struct bcm2835_alsa_stream *alsa_stream);
#endif /* __SOUND_ARM_BCM2835_H */ #endif /* __SOUND_ARM_BCM2835_H */
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