[PATCH] ALSA: sparc: cs4231: Require an integer number of periods

From: Stian Halseth

Date: Tue Dec 01 2026 - 16:15:54 EST


The DMA engine is fed one period at a time. snd_cs4231_advance_dma()
wraps after runtime->periods periods. Nothing requires the buffer to
be a whole number of periods. When it is not, the DMA ring ends before
the buffer does. The frames in the tail are never played. On capture,
the tail is never filled.

Any application that sets its own buffer and period sizes can hit
this. Buffer and period times can also round to such sizes. A minimal
case:

aplay -D hw:0 -t raw -f S16_BE -c 2 -r 48000 \
--buffer-size=8192 --period-size=2016 /dev/zero

The DMA wraps after 8064 frames, but ALSA counts each wrap as a full
pass of 8192 frames. On a Sun Ultra 5, hw_ptr in the substream's proc
status advances 48781 frames/s, against 48020 with 2048-frame periods.
With music the skipped frames are heard as distortion. This was found
with Audacious, which picks such sizes by default on this card.

Add the integer periods constraint, as other drivers that program DMA
per period do.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Closes: https://github.com/sparclinux/issues/issues/134
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Stian Halseth <stian@xxxxxx>
---
sound/sparc/cs4231.c | 8 ++++++++
1 file changed, 8 insertions(+)

diff --git a/sound/sparc/cs4231.c b/sound/sparc/cs4231.c
index 0489eac16..a2b414318 100644
--- a/sound/sparc/cs4231.c
+++ b/sound/sparc/cs4231.c
@@ -1094,6 +1094,10 @@ static int snd_cs4231_playback_open(struct snd_pcm_substream *substream)

runtime->hw = snd_cs4231_playback;

+ err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
+ if (err < 0)
+ return err;
+
err = snd_cs4231_open(chip, CS4231_MODE_PLAY);
if (err < 0)
return err;
@@ -1113,6 +1117,10 @@ static int snd_cs4231_capture_open(struct snd_pcm_substream *substream)

runtime->hw = snd_cs4231_capture;

+ err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
+ if (err < 0)
+ return err;
+
err = snd_cs4231_open(chip, CS4231_MODE_RECORD);
if (err < 0)
return err;
--
2.43.0