1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Helper functions for indirect PCM data transfer
5 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
6 * Jaroslav Kysela <perex@perex.cz>
9 #ifndef __SOUND_PCM_INDIRECT_H
10 #define __SOUND_PCM_INDIRECT_H
12 #include <sound/pcm.h>
14 struct snd_pcm_indirect {
15 unsigned int hw_buffer_size; /* Byte size of hardware buffer */
16 unsigned int hw_queue_size; /* Max queue size of hw buffer (0 = buffer size) */
17 unsigned int hw_data; /* Offset to next dst (or src) in hw ring buffer */
18 unsigned int hw_io; /* Ring buffer hw pointer */
19 int hw_ready; /* Bytes ready for play (or captured) in hw ring buffer */
20 unsigned int sw_buffer_size; /* Byte size of software buffer */
21 unsigned int sw_data; /* Offset to next dst (or src) in sw ring buffer */
22 unsigned int sw_io; /* Current software pointer in bytes */
23 int sw_ready; /* Bytes ready to be transferred to/from hw */
24 snd_pcm_uframes_t appl_ptr; /* Last seen appl_ptr */
27 typedef void (*snd_pcm_indirect_copy_t)(struct snd_pcm_substream *substream,
28 struct snd_pcm_indirect *rec, size_t bytes);
31 * helper function for playback ack callback
34 snd_pcm_indirect_playback_transfer(struct snd_pcm_substream *substream,
35 struct snd_pcm_indirect *rec,
36 snd_pcm_indirect_copy_t copy)
38 struct snd_pcm_runtime *runtime = substream->runtime;
39 snd_pcm_uframes_t appl_ptr = runtime->control->appl_ptr;
40 snd_pcm_sframes_t diff = appl_ptr - rec->appl_ptr;
44 if (diff < -(snd_pcm_sframes_t) (runtime->boundary / 2))
45 diff += runtime->boundary;
48 rec->sw_ready += (int)frames_to_bytes(runtime, diff);
49 rec->appl_ptr = appl_ptr;
51 qsize = rec->hw_queue_size ? rec->hw_queue_size : rec->hw_buffer_size;
52 while (rec->hw_ready < qsize && rec->sw_ready > 0) {
53 unsigned int hw_to_end = rec->hw_buffer_size - rec->hw_data;
54 unsigned int sw_to_end = rec->sw_buffer_size - rec->sw_data;
55 unsigned int bytes = qsize - rec->hw_ready;
56 if (rec->sw_ready < (int)bytes)
57 bytes = rec->sw_ready;
58 if (hw_to_end < bytes)
60 if (sw_to_end < bytes)
64 copy(substream, rec, bytes);
65 rec->hw_data += bytes;
66 if (rec->hw_data == rec->hw_buffer_size)
68 rec->sw_data += bytes;
69 if (rec->sw_data == rec->sw_buffer_size)
71 rec->hw_ready += bytes;
72 rec->sw_ready -= bytes;
78 * helper function for playback pointer callback
79 * ptr = current byte pointer
81 static inline snd_pcm_uframes_t
82 snd_pcm_indirect_playback_pointer(struct snd_pcm_substream *substream,
83 struct snd_pcm_indirect *rec, unsigned int ptr)
85 int bytes = ptr - rec->hw_io;
87 bytes += rec->hw_buffer_size;
89 rec->hw_ready -= bytes;
91 if (rec->sw_io >= rec->sw_buffer_size)
92 rec->sw_io -= rec->sw_buffer_size;
93 if (substream->ops->ack)
94 substream->ops->ack(substream);
95 return bytes_to_frames(substream->runtime, rec->sw_io);
100 * helper function for capture ack callback
103 snd_pcm_indirect_capture_transfer(struct snd_pcm_substream *substream,
104 struct snd_pcm_indirect *rec,
105 snd_pcm_indirect_copy_t copy)
107 struct snd_pcm_runtime *runtime = substream->runtime;
108 snd_pcm_uframes_t appl_ptr = runtime->control->appl_ptr;
109 snd_pcm_sframes_t diff = appl_ptr - rec->appl_ptr;
112 if (diff < -(snd_pcm_sframes_t) (runtime->boundary / 2))
113 diff += runtime->boundary;
116 rec->sw_ready -= frames_to_bytes(runtime, diff);
117 rec->appl_ptr = appl_ptr;
119 while (rec->hw_ready > 0 &&
120 rec->sw_ready < (int)rec->sw_buffer_size) {
121 size_t hw_to_end = rec->hw_buffer_size - rec->hw_data;
122 size_t sw_to_end = rec->sw_buffer_size - rec->sw_data;
123 size_t bytes = rec->sw_buffer_size - rec->sw_ready;
124 if (rec->hw_ready < (int)bytes)
125 bytes = rec->hw_ready;
126 if (hw_to_end < bytes)
128 if (sw_to_end < bytes)
132 copy(substream, rec, bytes);
133 rec->hw_data += bytes;
134 if ((int)rec->hw_data == rec->hw_buffer_size)
136 rec->sw_data += bytes;
137 if (rec->sw_data == rec->sw_buffer_size)
139 rec->hw_ready -= bytes;
140 rec->sw_ready += bytes;
146 * helper function for capture pointer callback,
147 * ptr = current byte pointer
149 static inline snd_pcm_uframes_t
150 snd_pcm_indirect_capture_pointer(struct snd_pcm_substream *substream,
151 struct snd_pcm_indirect *rec, unsigned int ptr)
154 int bytes = ptr - rec->hw_io;
156 bytes += rec->hw_buffer_size;
158 rec->hw_ready += bytes;
159 qsize = rec->hw_queue_size ? rec->hw_queue_size : rec->hw_buffer_size;
160 if (rec->hw_ready > qsize)
161 return SNDRV_PCM_POS_XRUN;
163 if (rec->sw_io >= rec->sw_buffer_size)
164 rec->sw_io -= rec->sw_buffer_size;
165 if (substream->ops->ack)
166 substream->ops->ack(substream);
167 return bytes_to_frames(substream->runtime, rec->sw_io);
170 #endif /* __SOUND_PCM_INDIRECT_H */