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
;
89 bytes
+= rec
->hw_buffer_size
;
91 rec
->hw_ready
-= bytes
;
93 if (rec
->sw_io
>= rec
->sw_buffer_size
)
94 rec
->sw_io
-= rec
->sw_buffer_size
;
95 if (substream
->ops
->ack
) {
96 err
= substream
->ops
->ack(substream
);
98 return SNDRV_PCM_POS_XRUN
;
100 return bytes_to_frames(substream
->runtime
, rec
->sw_io
);
105 * helper function for capture ack callback
108 snd_pcm_indirect_capture_transfer(struct snd_pcm_substream
*substream
,
109 struct snd_pcm_indirect
*rec
,
110 snd_pcm_indirect_copy_t copy
)
112 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
113 snd_pcm_uframes_t appl_ptr
= runtime
->control
->appl_ptr
;
114 snd_pcm_sframes_t diff
= appl_ptr
- rec
->appl_ptr
;
117 if (diff
< -(snd_pcm_sframes_t
) (runtime
->boundary
/ 2))
118 diff
+= runtime
->boundary
;
121 rec
->sw_ready
-= frames_to_bytes(runtime
, diff
);
122 rec
->appl_ptr
= appl_ptr
;
124 while (rec
->hw_ready
> 0 &&
125 rec
->sw_ready
< (int)rec
->sw_buffer_size
) {
126 size_t hw_to_end
= rec
->hw_buffer_size
- rec
->hw_data
;
127 size_t sw_to_end
= rec
->sw_buffer_size
- rec
->sw_data
;
128 size_t bytes
= rec
->sw_buffer_size
- rec
->sw_ready
;
129 if (rec
->hw_ready
< (int)bytes
)
130 bytes
= rec
->hw_ready
;
131 if (hw_to_end
< bytes
)
133 if (sw_to_end
< bytes
)
137 copy(substream
, rec
, bytes
);
138 rec
->hw_data
+= bytes
;
139 if ((int)rec
->hw_data
== rec
->hw_buffer_size
)
141 rec
->sw_data
+= bytes
;
142 if (rec
->sw_data
== rec
->sw_buffer_size
)
144 rec
->hw_ready
-= bytes
;
145 rec
->sw_ready
+= bytes
;
151 * helper function for capture pointer callback,
152 * ptr = current byte pointer
154 static inline snd_pcm_uframes_t
155 snd_pcm_indirect_capture_pointer(struct snd_pcm_substream
*substream
,
156 struct snd_pcm_indirect
*rec
, unsigned int ptr
)
159 int bytes
= ptr
- rec
->hw_io
;
163 bytes
+= rec
->hw_buffer_size
;
165 rec
->hw_ready
+= bytes
;
166 qsize
= rec
->hw_queue_size
? rec
->hw_queue_size
: rec
->hw_buffer_size
;
167 if (rec
->hw_ready
> qsize
)
168 return SNDRV_PCM_POS_XRUN
;
170 if (rec
->sw_io
>= rec
->sw_buffer_size
)
171 rec
->sw_io
-= rec
->sw_buffer_size
;
172 if (substream
->ops
->ack
) {
173 err
= substream
->ops
->ack(substream
);
175 return SNDRV_PCM_POS_XRUN
;
177 return bytes_to_frames(substream
->runtime
, rec
->sw_io
);
180 #endif /* __SOUND_PCM_INDIRECT_H */