From 565fc0e3da9bf0ccea99eb4386a4890cdba56134 Mon Sep 17 00:00:00 2001 From: dvdli Date: Wed, 9 Dec 2020 15:45:04 +0800 Subject: fix mmap-related functions' bugs 1. sync hw ptr before calculating the avail 2. return zero when reading or writing successfully --- src/pcm.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/pcm.c b/src/pcm.c index 17775d5..169d982 100644 --- a/src/pcm.c +++ b/src/pcm.c @@ -1242,6 +1242,7 @@ static inline int pcm_mmap_capture_avail(struct pcm *pcm) int pcm_mmap_avail(struct pcm *pcm) { + pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC); if (pcm->flags & PCM_IN) return pcm_mmap_capture_avail(pcm); else @@ -1552,8 +1553,14 @@ int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count) if ((~pcm->flags) & (PCM_OUT | PCM_MMAP)) return -ENOSYS; - return pcm_mmap_transfer(pcm, (void *)data, - pcm_bytes_to_frames(pcm, count)); + unsigned int frames = pcm_bytes_to_frames(pcm, count); + int res = pcm_mmap_transfer(pcm, (void *) data, frames); + + if (res < 0) { + return res; + } + + return (unsigned int) res == frames ? 0 : -EIO; } int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count) @@ -1561,7 +1568,14 @@ int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count) if ((~pcm->flags) & (PCM_IN | PCM_MMAP)) return -ENOSYS; - return pcm_mmap_transfer(pcm, data, pcm_bytes_to_frames(pcm, count)); + unsigned int frames = pcm_bytes_to_frames(pcm, count); + int res = pcm_mmap_transfer(pcm, data, frames); + + if (res < 0) { + return res; + } + + return (unsigned int) res == frames ? 0 : -EIO; } /* Returns current read/write position in the mmap buffer with associated time stamp. */ -- cgit v1.2.3