xref: /linux/include/sound/pcm-indirect.h (revision 0146878c)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Helper functions for indirect PCM data transfer
4  *
5  *  Copyright (c) by Takashi Iwai <tiwai@suse.de>
6  *                   Jaroslav Kysela <perex@perex.cz>
7  */
8 
9 #ifndef __SOUND_PCM_INDIRECT_H
10 #define __SOUND_PCM_INDIRECT_H
11 
12 #include <sound/pcm.h>
13 
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 */
25 };
26 
27 typedef void (*snd_pcm_indirect_copy_t)(struct snd_pcm_substream *substream,
28 					struct snd_pcm_indirect *rec, size_t bytes);
29 
30 /*
31  * helper function for playback ack callback
32  */
33 static inline int
snd_pcm_indirect_playback_transfer(struct snd_pcm_substream * substream,struct snd_pcm_indirect * rec,snd_pcm_indirect_copy_t copy)34 snd_pcm_indirect_playback_transfer(struct snd_pcm_substream *substream,
35 				   struct snd_pcm_indirect *rec,
36 				   snd_pcm_indirect_copy_t copy)
37 {
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;
41 	int qsize;
42 
43 	if (diff) {
44 		if (diff < -(snd_pcm_sframes_t) (runtime->boundary / 2))
45 			diff += runtime->boundary;
46 		if (diff < 0)
47 			return -EPIPE;
48 		rec->sw_ready += (int)frames_to_bytes(runtime, diff);
49 		rec->appl_ptr = appl_ptr;
50 	}
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)
59 			bytes = hw_to_end;
60 		if (sw_to_end < bytes)
61 			bytes = sw_to_end;
62 		if (! bytes)
63 			break;
64 		copy(substream, rec, bytes);
65 		rec->hw_data += bytes;
66 		if (rec->hw_data == rec->hw_buffer_size)
67 			rec->hw_data = 0;
68 		rec->sw_data += bytes;
69 		if (rec->sw_data == rec->sw_buffer_size)
70 			rec->sw_data = 0;
71 		rec->hw_ready += bytes;
72 		rec->sw_ready -= bytes;
73 	}
74 	return 0;
75 }
76 
77 /*
78  * helper function for playback pointer callback
79  * ptr = current byte pointer
80  */
81 static inline snd_pcm_uframes_t
snd_pcm_indirect_playback_pointer(struct snd_pcm_substream * substream,struct snd_pcm_indirect * rec,unsigned int ptr)82 snd_pcm_indirect_playback_pointer(struct snd_pcm_substream *substream,
83 				  struct snd_pcm_indirect *rec, unsigned int ptr)
84 {
85 	int bytes = ptr - rec->hw_io;
86 	int err;
87 
88 	if (bytes < 0)
89 		bytes += rec->hw_buffer_size;
90 	rec->hw_io = ptr;
91 	rec->hw_ready -= bytes;
92 	rec->sw_io += 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);
97 		if (err == -EPIPE)
98 			return SNDRV_PCM_POS_XRUN;
99 	}
100 	return bytes_to_frames(substream->runtime, rec->sw_io);
101 }
102 
103 
104 /*
105  * helper function for capture ack callback
106  */
107 static inline int
snd_pcm_indirect_capture_transfer(struct snd_pcm_substream * substream,struct snd_pcm_indirect * rec,snd_pcm_indirect_copy_t copy)108 snd_pcm_indirect_capture_transfer(struct snd_pcm_substream *substream,
109 				  struct snd_pcm_indirect *rec,
110 				  snd_pcm_indirect_copy_t copy)
111 {
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;
115 
116 	if (diff) {
117 		if (diff < -(snd_pcm_sframes_t) (runtime->boundary / 2))
118 			diff += runtime->boundary;
119 		if (diff < 0)
120 			return -EPIPE;
121 		rec->sw_ready -= frames_to_bytes(runtime, diff);
122 		rec->appl_ptr = appl_ptr;
123 	}
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)
132 			bytes = hw_to_end;
133 		if (sw_to_end < bytes)
134 			bytes = sw_to_end;
135 		if (! bytes)
136 			break;
137 		copy(substream, rec, bytes);
138 		rec->hw_data += bytes;
139 		if ((int)rec->hw_data == rec->hw_buffer_size)
140 			rec->hw_data = 0;
141 		rec->sw_data += bytes;
142 		if (rec->sw_data == rec->sw_buffer_size)
143 			rec->sw_data = 0;
144 		rec->hw_ready -= bytes;
145 		rec->sw_ready += bytes;
146 	}
147 	return 0;
148 }
149 
150 /*
151  * helper function for capture pointer callback,
152  * ptr = current byte pointer
153  */
154 static inline snd_pcm_uframes_t
snd_pcm_indirect_capture_pointer(struct snd_pcm_substream * substream,struct snd_pcm_indirect * rec,unsigned int ptr)155 snd_pcm_indirect_capture_pointer(struct snd_pcm_substream *substream,
156 				 struct snd_pcm_indirect *rec, unsigned int ptr)
157 {
158 	int qsize;
159 	int bytes = ptr - rec->hw_io;
160 	int err;
161 
162 	if (bytes < 0)
163 		bytes += rec->hw_buffer_size;
164 	rec->hw_io = ptr;
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;
169 	rec->sw_io += bytes;
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);
174 		if (err == -EPIPE)
175 			return SNDRV_PCM_POS_XRUN;
176 	}
177 	return bytes_to_frames(substream->runtime, rec->sw_io);
178 }
179 
180 #endif /* __SOUND_PCM_INDIRECT_H */
181