2 * Helper functions for indirect PCM data transfer
4 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
5 * Jaroslav Kysela <perex@suse.cz>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #ifndef __SOUND_PCM_INDIRECT_H
23 #define __SOUND_PCM_INDIRECT_H
25 #include <sound/pcm.h>
27 typedef struct sndrv_pcm_indirect
{
28 unsigned int hw_buffer_size
; /* Byte size of hardware buffer */
29 unsigned int hw_queue_size
; /* Max queue size of hw buffer (0 = buffer size) */
30 unsigned int hw_data
; /* Offset to next dst (or src) in hw ring buffer */
31 unsigned int hw_io
; /* Ring buffer hw pointer */
32 int hw_ready
; /* Bytes ready for play (or captured) in hw ring buffer */
33 unsigned int sw_buffer_size
; /* Byte size of software buffer */
34 unsigned int sw_data
; /* Offset to next dst (or src) in sw ring buffer */
35 unsigned int sw_io
; /* Current software pointer in bytes */
36 int sw_ready
; /* Bytes ready to be transferred to/from hw */
37 snd_pcm_uframes_t appl_ptr
; /* Last seen appl_ptr */
40 typedef void (*snd_pcm_indirect_copy_t
)(snd_pcm_substream_t
*substream
,
41 snd_pcm_indirect_t
*rec
, size_t bytes
);
44 * helper function for playback ack callback
47 snd_pcm_indirect_playback_transfer(snd_pcm_substream_t
*substream
,
48 snd_pcm_indirect_t
*rec
,
49 snd_pcm_indirect_copy_t copy
)
51 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
52 snd_pcm_uframes_t appl_ptr
= runtime
->control
->appl_ptr
;
53 snd_pcm_sframes_t diff
= appl_ptr
- rec
->appl_ptr
;
57 if (diff
< -(snd_pcm_sframes_t
) (runtime
->boundary
/ 2))
58 diff
+= runtime
->boundary
;
59 rec
->sw_ready
+= (int)frames_to_bytes(runtime
, diff
);
60 rec
->appl_ptr
= appl_ptr
;
62 qsize
= rec
->hw_queue_size
? rec
->hw_queue_size
: rec
->hw_buffer_size
;
63 while (rec
->hw_ready
< qsize
&& rec
->sw_ready
> 0) {
64 unsigned int hw_to_end
= rec
->hw_buffer_size
- rec
->hw_data
;
65 unsigned int sw_to_end
= rec
->sw_buffer_size
- rec
->sw_data
;
66 unsigned int bytes
= qsize
- rec
->hw_ready
;
67 if (rec
->sw_ready
< (int)bytes
)
68 bytes
= rec
->sw_ready
;
69 if (hw_to_end
< bytes
)
71 if (sw_to_end
< bytes
)
75 copy(substream
, rec
, bytes
);
76 rec
->hw_data
+= bytes
;
77 if (rec
->hw_data
== rec
->hw_buffer_size
)
79 rec
->sw_data
+= bytes
;
80 if (rec
->sw_data
== rec
->sw_buffer_size
)
82 rec
->hw_ready
+= bytes
;
83 rec
->sw_ready
-= bytes
;
88 * helper function for playback pointer callback
89 * ptr = current byte pointer
91 static inline snd_pcm_uframes_t
92 snd_pcm_indirect_playback_pointer(snd_pcm_substream_t
*substream
,
93 snd_pcm_indirect_t
*rec
, unsigned int ptr
)
95 int bytes
= ptr
- rec
->hw_io
;
97 bytes
+= rec
->hw_buffer_size
;
99 rec
->hw_ready
-= bytes
;
101 if (rec
->sw_io
>= rec
->sw_buffer_size
)
102 rec
->sw_io
-= rec
->sw_buffer_size
;
103 if (substream
->ops
->ack
)
104 substream
->ops
->ack(substream
);
105 return bytes_to_frames(substream
->runtime
, rec
->sw_io
);
110 * helper function for capture ack callback
113 snd_pcm_indirect_capture_transfer(snd_pcm_substream_t
*substream
,
114 snd_pcm_indirect_t
*rec
,
115 snd_pcm_indirect_copy_t copy
)
117 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
118 snd_pcm_uframes_t appl_ptr
= runtime
->control
->appl_ptr
;
119 snd_pcm_sframes_t diff
= appl_ptr
- rec
->appl_ptr
;
122 if (diff
< -(snd_pcm_sframes_t
) (runtime
->boundary
/ 2))
123 diff
+= runtime
->boundary
;
124 rec
->sw_ready
-= frames_to_bytes(runtime
, diff
);
125 rec
->appl_ptr
= appl_ptr
;
127 while (rec
->hw_ready
> 0 &&
128 rec
->sw_ready
< (int)rec
->sw_buffer_size
) {
129 size_t hw_to_end
= rec
->hw_buffer_size
- rec
->hw_data
;
130 size_t sw_to_end
= rec
->sw_buffer_size
- rec
->sw_data
;
131 size_t bytes
= rec
->sw_buffer_size
- rec
->sw_ready
;
132 if (rec
->hw_ready
< (int)bytes
)
133 bytes
= rec
->hw_ready
;
134 if (hw_to_end
< bytes
)
136 if (sw_to_end
< bytes
)
140 copy(substream
, rec
, bytes
);
141 rec
->hw_data
+= bytes
;
142 if ((int)rec
->hw_data
== rec
->hw_buffer_size
)
144 rec
->sw_data
+= bytes
;
145 if (rec
->sw_data
== rec
->sw_buffer_size
)
147 rec
->hw_ready
-= bytes
;
148 rec
->sw_ready
+= bytes
;
153 * helper function for capture pointer callback,
154 * ptr = current byte pointer
156 static inline snd_pcm_uframes_t
157 snd_pcm_indirect_capture_pointer(snd_pcm_substream_t
*substream
,
158 snd_pcm_indirect_t
*rec
, unsigned int ptr
)
161 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 substream
->ops
->ack(substream
);
174 return bytes_to_frames(substream
->runtime
, rec
->sw_io
);
177 #endif /* __SOUND_PCM_INDIRECT_H */