1 /*
2  *  PCM Interface - local header file
3  *  Copyright (c) 2000 by Jaroslav Kysela <perex@perex.cz>
4  *                        Abramo Bagnara <abramo@alsa-project.org>
5  *
6  *
7  *   This library is free software; you can redistribute it and/or modify
8  *   it under the terms of the GNU Lesser General Public License as
9  *   published by the Free Software Foundation; either version 2.1 of
10  *   the License, or (at your option) any later version.
11  *
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 Lesser General Public License for more details.
16  *
17  *   You should have received a copy of the GNU Lesser General Public
18  *   License along with this library; if not, write to the Free Software
19  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20  *
21  */
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <limits.h>
26 #include <sys/uio.h>
27 #include <time.h>
28 #include <sys/time.h>
29 
30 #define _snd_mask sndrv_mask
31 #define _snd_pcm_access_mask _snd_mask
32 #define _snd_pcm_format_mask _snd_mask
33 #define _snd_pcm_subformat_mask _snd_mask
34 
35 #include "local.h"
36 
37 #ifdef THREAD_SAFE_API
38 #define __USE_UNIX98 1	/* for old glibc */
39 #include <pthread.h>
40 #endif
41 
42 #define SND_INTERVAL_INLINE
43 #include "interval.h"
44 
45 #define SND_MASK_INLINE
46 #include "mask.h"
47 
48 #define SND_PCM_HW_PARAM_ACCESS SNDRV_PCM_HW_PARAM_ACCESS
49 #define SND_PCM_HW_PARAM_FIRST_MASK SNDRV_PCM_HW_PARAM_FIRST_MASK
50 #define SND_PCM_HW_PARAM_FORMAT SNDRV_PCM_HW_PARAM_FORMAT
51 #define SND_PCM_HW_PARAM_SUBFORMAT SNDRV_PCM_HW_PARAM_SUBFORMAT
52 #define SND_PCM_HW_PARAM_LAST_MASK SNDRV_PCM_HW_PARAM_LAST_MASK
53 #define SND_PCM_HW_PARAM_SAMPLE_BITS SNDRV_PCM_HW_PARAM_SAMPLE_BITS
54 #define SND_PCM_HW_PARAM_FIRST_INTERVAL SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
55 #define SND_PCM_HW_PARAM_FRAME_BITS SNDRV_PCM_HW_PARAM_FRAME_BITS
56 #define SND_PCM_HW_PARAM_CHANNELS SNDRV_PCM_HW_PARAM_CHANNELS
57 #define SND_PCM_HW_PARAM_RATE SNDRV_PCM_HW_PARAM_RATE
58 #define SND_PCM_HW_PARAM_PERIOD_TIME SNDRV_PCM_HW_PARAM_PERIOD_TIME
59 #define SND_PCM_HW_PARAM_PERIOD_SIZE SNDRV_PCM_HW_PARAM_PERIOD_SIZE
60 #define SND_PCM_HW_PARAM_PERIOD_BYTES SNDRV_PCM_HW_PARAM_PERIOD_BYTES
61 #define SND_PCM_HW_PARAM_PERIODS SNDRV_PCM_HW_PARAM_PERIODS
62 #define SND_PCM_HW_PARAM_BUFFER_TIME SNDRV_PCM_HW_PARAM_BUFFER_TIME
63 #define SND_PCM_HW_PARAM_BUFFER_SIZE SNDRV_PCM_HW_PARAM_BUFFER_SIZE
64 #define SND_PCM_HW_PARAM_BUFFER_BYTES SNDRV_PCM_HW_PARAM_BUFFER_BYTES
65 #define SND_PCM_HW_PARAM_TICK_TIME SNDRV_PCM_HW_PARAM_TICK_TIME
66 #define SND_PCM_HW_PARAM_LAST_INTERVAL SNDRV_PCM_HW_PARAM_LAST_INTERVAL
67 #define SND_PCM_HW_PARAM_LAST_MASK SNDRV_PCM_HW_PARAM_LAST_MASK
68 #define SND_PCM_HW_PARAM_FIRST_MASK SNDRV_PCM_HW_PARAM_FIRST_MASK
69 #define SND_PCM_HW_PARAM_LAST_INTERVAL SNDRV_PCM_HW_PARAM_LAST_INTERVAL
70 #define SND_PCM_HW_PARAM_FIRST_INTERVAL SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
71 
72 /** device accepts mmaped access */
73 #define SND_PCM_INFO_MMAP SNDRV_PCM_INFO_MMAP
74 /** device accepts  mmaped access with sample resolution */
75 #define SND_PCM_INFO_MMAP_VALID SNDRV_PCM_INFO_MMAP_VALID
76 /** device is doing double buffering */
77 #define SND_PCM_INFO_DOUBLE SNDRV_PCM_INFO_DOUBLE
78 /** device transfers samples in batch */
79 #define SND_PCM_INFO_BATCH SNDRV_PCM_INFO_BATCH
80 /** device accepts interleaved samples */
81 #define SND_PCM_INFO_INTERLEAVED SNDRV_PCM_INFO_INTERLEAVED
82 /** device accepts non-interleaved samples */
83 #define SND_PCM_INFO_NONINTERLEAVED SNDRV_PCM_INFO_NONINTERLEAVED
84 /** device accepts complex sample organization */
85 #define SND_PCM_INFO_COMPLEX SNDRV_PCM_INFO_COMPLEX
86 /** device is capable block transfers */
87 #define SND_PCM_INFO_BLOCK_TRANSFER SNDRV_PCM_INFO_BLOCK_TRANSFER
88 /** device can detect DAC/ADC overrange */
89 #define SND_PCM_INFO_OVERRANGE SNDRV_PCM_INFO_OVERRANGE
90 /** device supports resume */
91 #define SND_PCM_INFO_RESUME SNDRV_PCM_INFO_RESUME
92 /** device is capable to pause */
93 #define SND_PCM_INFO_PAUSE SNDRV_PCM_INFO_PAUSE
94 /** device can do only half duplex */
95 #define SND_PCM_INFO_HALF_DUPLEX SNDRV_PCM_INFO_HALF_DUPLEX
96 /** device can do only joint duplex (same parameters) */
97 #define SND_PCM_INFO_JOINT_DUPLEX SNDRV_PCM_INFO_JOINT_DUPLEX
98 /** device can do a kind of synchronized start */
99 #define SND_PCM_INFO_SYNC_START SNDRV_PCM_INFO_SYNC_START
100 /** device can disable period wakeups */
101 #define SND_PCM_INFO_NO_PERIOD_WAKEUP SNDRV_PCM_INFO_NO_PERIOD_WAKEUP
102 
103 #define SND_PCM_HW_PARAMS_NORESAMPLE SNDRV_PCM_HW_PARAMS_NORESAMPLE
104 #define SND_PCM_HW_PARAMS_EXPORT_BUFFER SNDRV_PCM_HW_PARAMS_EXPORT_BUFFER
105 #define SND_PCM_HW_PARAMS_NO_PERIOD_WAKEUP SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
106 
107 #define SND_PCM_INFO_MONOTONIC	0x80000000
108 
109 typedef struct _snd_pcm_rbptr {
110 	snd_pcm_t *master;
111 	volatile snd_pcm_uframes_t *ptr;
112 	int fd;
113 	off_t offset;
114 	int link_dst_count;
115 	snd_pcm_t **link_dst;
116 	void *private_data;
117 	void (*changed)(snd_pcm_t *pcm, snd_pcm_t *src);
118 } snd_pcm_rbptr_t;
119 
120 typedef struct _snd_pcm_channel_info {
121 	unsigned int channel;
122 	void *addr;			/* base address of channel samples */
123 	unsigned int first;		/* offset to first sample in bits */
124 	unsigned int step;		/* samples distance in bits */
125 	enum { SND_PCM_AREA_SHM, SND_PCM_AREA_MMAP, SND_PCM_AREA_LOCAL } type;
126 	union {
127 		struct {
128 			struct snd_shm_area *area;
129 			int shmid;
130 		} shm;
131 		struct {
132 			int fd;
133 			off_t offset;
134 		} mmap;
135 	} u;
136 	char reserved[64];
137 } snd_pcm_channel_info_t;
138 
139 typedef struct {
140 	int (*close)(snd_pcm_t *pcm);
141 	int (*nonblock)(snd_pcm_t *pcm, int nonblock); /* always locked */
142 	int (*async)(snd_pcm_t *pcm, int sig, pid_t pid);
143 	int (*info)(snd_pcm_t *pcm, snd_pcm_info_t *info);
144 	int (*hw_refine)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params);
145 	int (*hw_params)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params);
146 	int (*hw_free)(snd_pcm_t *pcm);
147 	int (*sw_params)(snd_pcm_t *pcm, snd_pcm_sw_params_t *params); /* always locked */
148 	int (*channel_info)(snd_pcm_t *pcm, snd_pcm_channel_info_t *info);
149 	void (*dump)(snd_pcm_t *pcm, snd_output_t *out);
150 	int (*mmap)(snd_pcm_t *pcm);
151 	int (*munmap)(snd_pcm_t *pcm);
152 	snd_pcm_chmap_query_t **(*query_chmaps)(snd_pcm_t *pcm);
153 	snd_pcm_chmap_t *(*get_chmap)(snd_pcm_t *pcm);
154 	int (*set_chmap)(snd_pcm_t *pcm, const snd_pcm_chmap_t *map);
155 } snd_pcm_ops_t;
156 
157 typedef struct {
158 	int (*status)(snd_pcm_t *pcm, snd_pcm_status_t *status); /* locked */
159 	int (*prepare)(snd_pcm_t *pcm); /* locked */
160 	int (*reset)(snd_pcm_t *pcm); /* locked */
161 	int (*start)(snd_pcm_t *pcm); /* locked */
162 	int (*drop)(snd_pcm_t *pcm); /* locked */
163 	int (*drain)(snd_pcm_t *pcm); /* need own locking */
164 	int (*pause)(snd_pcm_t *pcm, int enable); /* locked */
165 	snd_pcm_state_t (*state)(snd_pcm_t *pcm); /* locked */
166 	int (*hwsync)(snd_pcm_t *pcm); /* locked */
167 	int (*delay)(snd_pcm_t *pcm, snd_pcm_sframes_t *delayp); /* locked */
168 	int (*resume)(snd_pcm_t *pcm); /* need own locking */
169 	int (*link)(snd_pcm_t *pcm1, snd_pcm_t *pcm2);
170 	int (*link_slaves)(snd_pcm_t *pcm, snd_pcm_t *master);
171 	int (*unlink)(snd_pcm_t *pcm);
172 	snd_pcm_sframes_t (*rewindable)(snd_pcm_t *pcm); /* locked */
173 	snd_pcm_sframes_t (*rewind)(snd_pcm_t *pcm, snd_pcm_uframes_t frames); /* locked */
174 	snd_pcm_sframes_t (*forwardable)(snd_pcm_t *pcm); /* locked */
175 	snd_pcm_sframes_t (*forward)(snd_pcm_t *pcm, snd_pcm_uframes_t frames); /* locked */
176 	snd_pcm_sframes_t (*writei)(snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size); /* need own locking */
177 	snd_pcm_sframes_t (*writen)(snd_pcm_t *pcm, void **bufs, snd_pcm_uframes_t size); /* need own locking */
178 	snd_pcm_sframes_t (*readi)(snd_pcm_t *pcm, void *buffer, snd_pcm_uframes_t size); /* need own locking */
179 	snd_pcm_sframes_t (*readn)(snd_pcm_t *pcm, void **bufs, snd_pcm_uframes_t size); /* need own locking */
180 	snd_pcm_sframes_t (*avail_update)(snd_pcm_t *pcm); /* locked */
181 	snd_pcm_sframes_t (*mmap_commit)(snd_pcm_t *pcm, snd_pcm_uframes_t offset, snd_pcm_uframes_t size); /* locked */
182 	int (*htimestamp)(snd_pcm_t *pcm, snd_pcm_uframes_t *avail, snd_htimestamp_t *tstamp); /* locked */
183 	int (*poll_descriptors_count)(snd_pcm_t *pcm); /* locked */
184 	int (*poll_descriptors)(snd_pcm_t *pcm, struct pollfd *pfds, unsigned int space); /* locked */
185 	int (*poll_revents)(snd_pcm_t *pcm, struct pollfd *pfds, unsigned int nfds, unsigned short *revents); /* locked */
186 	int (*may_wait_for_avail_min)(snd_pcm_t *pcm, snd_pcm_uframes_t avail);
187 	int (*mmap_begin)(snd_pcm_t *pcm, const snd_pcm_channel_area_t **areas, snd_pcm_uframes_t *offset, snd_pcm_uframes_t *frames); /* locked */
188 } snd_pcm_fast_ops_t;
189 
190 struct _snd_pcm {
191 	void *open_func;
192 	char *name;
193 	snd_pcm_type_t type;
194 	snd_pcm_stream_t stream;
195 	int mode;
196 	long minperiodtime;		/* in us */
197 	int poll_fd_count;
198 	int poll_fd;
199 	unsigned short poll_events;
200 	int setup: 1,
201 	    compat: 1;
202 	snd_pcm_access_t access;	/* access mode */
203 	snd_pcm_format_t format;	/* SND_PCM_FORMAT_* */
204 	snd_pcm_subformat_t subformat;	/* subformat */
205 	unsigned int channels;		/* channels */
206 	unsigned int rate;		/* rate in Hz */
207 	snd_pcm_uframes_t period_size;
208 	unsigned int period_time;	/* period duration */
209 	snd_interval_t periods;
210 	snd_pcm_tstamp_t tstamp_mode;	/* timestamp mode */
211 	snd_pcm_tstamp_type_t tstamp_type;	/* timestamp type */
212 	unsigned int period_step;
213 	snd_pcm_uframes_t avail_min;	/* min avail frames for wakeup */
214 	int period_event;
215 	snd_pcm_uframes_t start_threshold;
216 	snd_pcm_uframes_t stop_threshold;
217 	snd_pcm_uframes_t silence_threshold;	/* Silence filling happens when
218 					   noise is nearest than this */
219 	snd_pcm_uframes_t silence_size;	/* Silence filling size */
220 	snd_pcm_uframes_t boundary;	/* pointers wrap point */
221 	unsigned int info;		/* Info for returned setup */
222 	unsigned int msbits;		/* used most significant bits */
223 	unsigned int rate_num;		/* rate numerator */
224 	unsigned int rate_den;		/* rate denominator */
225 	unsigned int hw_flags;		/* actual hardware flags */
226 	snd_pcm_uframes_t fifo_size;	/* chip FIFO size in frames */
227 	snd_pcm_uframes_t buffer_size;
228 	snd_interval_t buffer_time;
229 	unsigned int sample_bits;
230 	unsigned int frame_bits;
231 	snd_pcm_rbptr_t appl;
232 	snd_pcm_rbptr_t hw;
233 	snd_pcm_uframes_t min_align;
234 	unsigned int mmap_rw: 1;	/* use always mmapped buffer */
235 	unsigned int mmap_shadow: 1;	/* don't call actual mmap,
236 					 * use the mmaped buffer of the slave
237 					 */
238 	unsigned int donot_close: 1;	/* don't close this PCM */
239 	unsigned int own_state_check:1; /* plugin has own PCM state check */
240 	snd_pcm_channel_info_t *mmap_channels;
241 	snd_pcm_channel_area_t *running_areas;
242 	snd_pcm_channel_area_t *stopped_areas;
243 	const snd_pcm_ops_t *ops;
244 	const snd_pcm_fast_ops_t *fast_ops;
245 	snd_pcm_t *op_arg;
246 	snd_pcm_t *fast_op_arg;
247 	void *private_data;
248 	struct list_head async_handlers;
249 #ifdef THREAD_SAFE_API
250 	int need_lock;		/* true = this PCM (plugin) is thread-unsafe,
251 				 * thus it needs a lock.
252 				 */
253 	int lock_enabled;	/* thread-safety lock is enabled on the system;
254 				 * it's set depending on $LIBASOUND_THREAD_SAFE.
255 				 */
256 	pthread_mutex_t lock;
257 #endif
258 };
259 
260 /* make local functions really local */
261 /* Grrr, these cannot be local - a bad aserver uses them!
262 #define snd_pcm_async \
263 	snd1_pcm_async
264 #define snd_pcm_mmap \
265 	snd1_pcm_mmap
266 #define snd_pcm_munmap \
267 	snd1_pcm_munmap
268 #define snd_pcm_hw_refine \
269 	snd1_pcm_hw_refine
270 */
271 #define snd_pcm_new \
272 	snd1_pcm_new
273 #define snd_pcm_free \
274 	snd1_pcm_free
275 #define snd_pcm_areas_from_buf \
276 	snd1_pcm_areas_from_buf
277 #define snd_pcm_areas_from_bufs \
278 	snd1_pcm_areas_from_bufs
279 #define snd_pcm_open_named_slave \
280 	snd1_pcm_open_named_slave
281 #define snd_pcm_hw_open_fd \
282 	snd1_pcm_hw_open_fd
283 #define snd_pcm_wait_nocheck \
284 	snd1_pcm_wait_nocheck
285 #define snd_pcm_rate_get_default_converter \
286 	snd1_pcm_rate_get_default_converter
287 #define snd_pcm_set_hw_ptr \
288 	snd1_pcm_set_hw_ptr
289 #define snd_pcm_set_appl_ptr \
290 	snd1_pcm_set_appl_ptr
291 #define snd_pcm_link_hw_ptr \
292 	snd1_pcm_link_hw_ptr
293 #define snd_pcm_link_appl_ptr \
294 	snd1_pcm_link_appl_ptr
295 #define snd_pcm_unlink_hw_ptr \
296 	snd1_pcm_unlink_hw_ptr
297 #define snd_pcm_unlink_appl_ptr \
298 	snd1_pcm_unlink_appl_ptr
299 #define snd_pcm_mmap_appl_ptr \
300 	snd1_pcm_mmap_appl_ptr
301 #define snd_pcm_mmap_appl_backward \
302 	snd1_pcm_mmap_appl_backward
303 #define snd_pcm_mmap_appl_forward \
304 	snd1_pcm_mmap_appl_forward
305 #define snd_pcm_mmap_hw_backward \
306 	snd1_pcm_mmap_hw_backward
307 #define snd_pcm_mmap_hw_forward \
308 	snd1_pcm_mmap_hw_forward
309 #define snd_pcm_read_areas \
310 	snd1_pcm_read_areas
311 #define snd_pcm_write_areas \
312 	snd1_pcm_write_areas
313 #define snd_pcm_read_mmap \
314 	snd1_pcm_read_mmap
315 #define snd_pcm_write_mmap \
316 	snd1_pcm_write_mmap
317 #define snd_pcm_channel_info_shm \
318 	snd1_pcm_channel_info_shm
319 #define snd_pcm_hw_refine_soft \
320 	snd1_pcm_hw_refine_soft
321 #define snd_pcm_hw_refine_slave \
322 	snd1_pcm_hw_refine_slave
323 #define snd_pcm_hw_params_slave \
324 	snd1_pcm_hw_params_slave
325 #define snd_pcm_hw_param_refine_near \
326 	snd1_pcm_hw_param_refine_near
327 #define snd_pcm_hw_param_refine_multiple \
328 	snd1_pcm_hw_param_refine_multiple
329 #define snd_pcm_hw_param_empty \
330 	snd1_pcm_hw_param_empty
331 #define snd_pcm_hw_param_always_eq \
332 	snd1_pcm_hw_param_always_eq
333 #define snd_pcm_hw_param_never_eq \
334 	snd1_pcm_hw_param_never_eq
335 #define snd_pcm_hw_param_get_mask \
336 	snd1_pcm_hw_param_get_mask
337 #define snd_pcm_hw_param_get_interval \
338 	snd1_pcm_hw_param_get_interval
339 #define snd_pcm_hw_param_any \
340 	snd1_pcm_hw_param_any
341 #define snd_pcm_hw_param_set_integer \
342 	snd1_pcm_hw_param_set_integer
343 #define snd_pcm_hw_param_set_first \
344 	snd1_pcm_hw_param_set_first
345 #define snd_pcm_hw_param_set_last \
346 	snd1_pcm_hw_param_set_last
347 #define snd_pcm_hw_param_set_near \
348 	snd1_pcm_hw_param_set_near
349 #define snd_pcm_hw_param_set_min \
350 	snd1_pcm_hw_param_set_min
351 #define snd_pcm_hw_param_set_max \
352 	snd1_pcm_hw_param_set_max
353 #define snd_pcm_hw_param_set_minmax \
354 	snd1_pcm_hw_param_set_minmax
355 #define snd_pcm_hw_param_set \
356 	snd1_pcm_hw_param_set
357 #define snd_pcm_hw_param_set_mask \
358 	snd1_pcm_hw_param_set_mask
359 #define snd_pcm_hw_param_get \
360 	snd1_pcm_hw_param_get
361 #define snd_pcm_hw_param_get_min \
362 	snd1_pcm_hw_param_get_min
363 #define snd_pcm_hw_param_get_max \
364 	snd1_pcm_hw_param_get_max
365 #define snd_pcm_hw_param_name		\
366 	snd1_pcm_hw_param_name
367 
368 int snd_pcm_new(snd_pcm_t **pcmp, snd_pcm_type_t type, const char *name,
369 		snd_pcm_stream_t stream, int mode);
370 int snd_pcm_free(snd_pcm_t *pcm);
371 
372 void snd_pcm_areas_from_buf(snd_pcm_t *pcm, snd_pcm_channel_area_t *areas, void *buf);
373 void snd_pcm_areas_from_bufs(snd_pcm_t *pcm, snd_pcm_channel_area_t *areas, void **bufs);
374 
375 int snd_pcm_async(snd_pcm_t *pcm, int sig, pid_t pid);
376 int snd_pcm_mmap(snd_pcm_t *pcm);
377 int snd_pcm_munmap(snd_pcm_t *pcm);
378 int snd_pcm_mmap_ready(snd_pcm_t *pcm);
379 void snd_pcm_set_hw_ptr(snd_pcm_t *pcm, volatile snd_pcm_uframes_t *hw_ptr, int fd, off_t offset);
380 void snd_pcm_set_appl_ptr(snd_pcm_t *pcm, volatile snd_pcm_uframes_t *appl_ptr, int fd, off_t offset);
381 void snd_pcm_link_hw_ptr(snd_pcm_t *pcm, snd_pcm_t *slave);
382 void snd_pcm_link_appl_ptr(snd_pcm_t *pcm, snd_pcm_t *slave);
383 void snd_pcm_unlink_hw_ptr(snd_pcm_t *pcm, snd_pcm_t *slave);
384 void snd_pcm_unlink_appl_ptr(snd_pcm_t *pcm, snd_pcm_t *slave);
385 snd_pcm_sframes_t snd_pcm_mmap_appl_ptr(snd_pcm_t *pcm, off_t offset);
386 void snd_pcm_mmap_appl_backward(snd_pcm_t *pcm, snd_pcm_uframes_t frames);
387 void snd_pcm_mmap_appl_forward(snd_pcm_t *pcm, snd_pcm_uframes_t frames);
388 void snd_pcm_mmap_hw_backward(snd_pcm_t *pcm, snd_pcm_uframes_t frames);
389 void snd_pcm_mmap_hw_forward(snd_pcm_t *pcm, snd_pcm_uframes_t frames);
390 
391 snd_pcm_sframes_t snd_pcm_mmap_writei(snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size);
392 snd_pcm_sframes_t snd_pcm_mmap_readi(snd_pcm_t *pcm, void *buffer, snd_pcm_uframes_t size);
393 snd_pcm_sframes_t snd_pcm_mmap_writen(snd_pcm_t *pcm, void **bufs, snd_pcm_uframes_t size);
394 snd_pcm_sframes_t snd_pcm_mmap_readn(snd_pcm_t *pcm, void **bufs, snd_pcm_uframes_t size);
395 
396 typedef snd_pcm_sframes_t (*snd_pcm_xfer_areas_func_t)(snd_pcm_t *pcm,
397 						       const snd_pcm_channel_area_t *areas,
398 						       snd_pcm_uframes_t offset,
399 						       snd_pcm_uframes_t size);
400 
401 snd_pcm_sframes_t snd_pcm_read_areas(snd_pcm_t *pcm, const snd_pcm_channel_area_t *areas,
402 				     snd_pcm_uframes_t offset, snd_pcm_uframes_t size,
403 				     snd_pcm_xfer_areas_func_t func);
404 snd_pcm_sframes_t snd_pcm_write_areas(snd_pcm_t *pcm, const snd_pcm_channel_area_t *areas,
405 				      snd_pcm_uframes_t offset, snd_pcm_uframes_t size,
406 				      snd_pcm_xfer_areas_func_t func);
407 snd_pcm_sframes_t snd_pcm_read_mmap(snd_pcm_t *pcm, snd_pcm_uframes_t offset,
408 				    snd_pcm_uframes_t size);
409 snd_pcm_sframes_t snd_pcm_write_mmap(snd_pcm_t *pcm, snd_pcm_uframes_t offset,
410 				     snd_pcm_uframes_t size);
snd_pcm_channel_info(snd_pcm_t * pcm,snd_pcm_channel_info_t * info)411 static inline int snd_pcm_channel_info(snd_pcm_t *pcm, snd_pcm_channel_info_t *info)
412 {
413 	if (!pcm->ops->channel_info)
414 		return -ENOSYS;
415 	return pcm->ops->channel_info(pcm, info);
416 }
417 int snd_pcm_channel_info_shm(snd_pcm_t *pcm, snd_pcm_channel_info_t *info, int shmid);
418 int _snd_pcm_poll_descriptor(snd_pcm_t *pcm);
419 #define _snd_pcm_link_descriptor _snd_pcm_poll_descriptor /* FIXME */
420 #define _snd_pcm_async_descriptor _snd_pcm_poll_descriptor /* FIXME */
421 
422 /* locked versions */
423 int __snd_pcm_mmap_begin(snd_pcm_t *pcm, const snd_pcm_channel_area_t **areas,
424 			 snd_pcm_uframes_t *offset, snd_pcm_uframes_t *frames);
425 snd_pcm_sframes_t __snd_pcm_mmap_commit(snd_pcm_t *pcm,
426 					snd_pcm_uframes_t offset,
427 					snd_pcm_uframes_t frames);
428 int __snd_pcm_wait_in_lock(snd_pcm_t *pcm, int timeout);
429 
__snd_pcm_avail_update(snd_pcm_t * pcm)430 static inline snd_pcm_sframes_t __snd_pcm_avail_update(snd_pcm_t *pcm)
431 {
432 	if (!pcm->fast_ops->avail_update)
433 		return -ENOSYS;
434 	return pcm->fast_ops->avail_update(pcm->fast_op_arg);
435 }
436 
__snd_pcm_start(snd_pcm_t * pcm)437 static inline int __snd_pcm_start(snd_pcm_t *pcm)
438 {
439 	if (!pcm->fast_ops->start)
440 		return -ENOSYS;
441 	return pcm->fast_ops->start(pcm->fast_op_arg);
442 }
443 
__snd_pcm_state(snd_pcm_t * pcm)444 static inline snd_pcm_state_t __snd_pcm_state(snd_pcm_t *pcm)
445 {
446 	if (!pcm->fast_ops->state)
447 		return -ENOSYS;
448 	return pcm->fast_ops->state(pcm->fast_op_arg);
449 }
450 
__snd_pcm_hwsync(snd_pcm_t * pcm)451 static inline int __snd_pcm_hwsync(snd_pcm_t *pcm)
452 {
453 	if (!pcm->fast_ops->hwsync)
454 		return -ENOSYS;
455 	return pcm->fast_ops->hwsync(pcm->fast_op_arg);
456 }
457 
__snd_pcm_delay(snd_pcm_t * pcm,snd_pcm_sframes_t * delayp)458 static inline int __snd_pcm_delay(snd_pcm_t *pcm, snd_pcm_sframes_t *delayp)
459 {
460 	if (!pcm->fast_ops->delay)
461 		return -ENOSYS;
462 	return pcm->fast_ops->delay(pcm->fast_op_arg, delayp);
463 }
464 
465 /* handle special error cases */
snd_pcm_check_error(snd_pcm_t * pcm,int err)466 static inline int snd_pcm_check_error(snd_pcm_t *pcm, int err)
467 {
468 	if (err == -EINTR) {
469 		switch (__snd_pcm_state(pcm)) {
470 		case SND_PCM_STATE_XRUN:
471 			return -EPIPE;
472 		case SND_PCM_STATE_SUSPENDED:
473 			return -ESTRPIPE;
474 		case SND_PCM_STATE_DISCONNECTED:
475 			return -ENODEV;
476 		default:
477 			break;
478 		}
479 	}
480 	return err;
481 }
482 
__snd_pcm_playback_avail(snd_pcm_t * pcm,const snd_pcm_uframes_t hw_ptr,const snd_pcm_uframes_t appl_ptr)483 static inline snd_pcm_uframes_t __snd_pcm_playback_avail(snd_pcm_t *pcm,
484 							 const snd_pcm_uframes_t hw_ptr,
485 							 const snd_pcm_uframes_t appl_ptr)
486 {
487 	snd_pcm_sframes_t avail;
488 	avail = hw_ptr + pcm->buffer_size - appl_ptr;
489 	if (avail < 0)
490 		avail += pcm->boundary;
491 	else if ((snd_pcm_uframes_t) avail >= pcm->boundary)
492 		avail -= pcm->boundary;
493 	return avail;
494 }
495 
snd_pcm_mmap_playback_avail(snd_pcm_t * pcm)496 static inline snd_pcm_uframes_t snd_pcm_mmap_playback_avail(snd_pcm_t *pcm)
497 {
498 	return __snd_pcm_playback_avail(pcm, *pcm->hw.ptr, *pcm->appl.ptr);
499 }
500 
__snd_pcm_capture_avail(snd_pcm_t * pcm,const snd_pcm_uframes_t hw_ptr,const snd_pcm_uframes_t appl_ptr)501 static inline snd_pcm_uframes_t __snd_pcm_capture_avail(snd_pcm_t *pcm,
502 							const snd_pcm_uframes_t hw_ptr,
503 							const snd_pcm_uframes_t appl_ptr)
504 {
505 	snd_pcm_sframes_t avail;
506 	avail = hw_ptr - appl_ptr;
507 	if (avail < 0)
508 		avail += pcm->boundary;
509 	return avail;
510 }
511 
snd_pcm_mmap_capture_avail(snd_pcm_t * pcm)512 static inline snd_pcm_uframes_t snd_pcm_mmap_capture_avail(snd_pcm_t *pcm)
513 {
514 	return __snd_pcm_capture_avail(pcm, *pcm->hw.ptr, *pcm->appl.ptr);
515 }
516 
__snd_pcm_avail(snd_pcm_t * pcm,const snd_pcm_uframes_t hw_ptr,const snd_pcm_uframes_t appl_ptr)517 static inline snd_pcm_uframes_t __snd_pcm_avail(snd_pcm_t *pcm,
518 						const snd_pcm_uframes_t hw_ptr,
519 						const snd_pcm_uframes_t appl_ptr)
520 {
521 	if (pcm->stream == SND_PCM_STREAM_PLAYBACK)
522 		return __snd_pcm_playback_avail(pcm, hw_ptr, appl_ptr);
523 	else
524 		return __snd_pcm_capture_avail(pcm, hw_ptr, appl_ptr);
525 }
526 
snd_pcm_mmap_avail(snd_pcm_t * pcm)527 static inline snd_pcm_uframes_t snd_pcm_mmap_avail(snd_pcm_t *pcm)
528 {
529 	return __snd_pcm_avail(pcm, *pcm->hw.ptr, *pcm->appl.ptr);
530 }
531 
snd_pcm_mmap_playback_hw_avail(snd_pcm_t * pcm)532 static inline snd_pcm_sframes_t snd_pcm_mmap_playback_hw_avail(snd_pcm_t *pcm)
533 {
534 	return pcm->buffer_size - snd_pcm_mmap_playback_avail(pcm);
535 }
536 
snd_pcm_mmap_capture_hw_avail(snd_pcm_t * pcm)537 static inline snd_pcm_sframes_t snd_pcm_mmap_capture_hw_avail(snd_pcm_t *pcm)
538 {
539 	return pcm->buffer_size - snd_pcm_mmap_capture_avail(pcm);
540 }
541 
snd_pcm_mmap_hw_avail(snd_pcm_t * pcm)542 static inline snd_pcm_sframes_t snd_pcm_mmap_hw_avail(snd_pcm_t *pcm)
543 {
544 	return pcm->buffer_size - snd_pcm_mmap_avail(pcm);
545 }
546 
snd_pcm_mmap_playback_hw_rewindable(snd_pcm_t * pcm)547 static inline snd_pcm_sframes_t snd_pcm_mmap_playback_hw_rewindable(snd_pcm_t *pcm)
548 {
549 	snd_pcm_sframes_t ret = snd_pcm_mmap_playback_hw_avail(pcm);
550 	return (ret >= 0) ? ret : 0;
551 }
552 
snd_pcm_mmap_capture_hw_rewindable(snd_pcm_t * pcm)553 static inline snd_pcm_sframes_t snd_pcm_mmap_capture_hw_rewindable(snd_pcm_t *pcm)
554 {
555 	snd_pcm_sframes_t ret = snd_pcm_mmap_capture_hw_avail(pcm);
556 	return (ret >= 0) ? ret : 0;
557 }
558 
snd_pcm_mmap_hw_rewindable(snd_pcm_t * pcm)559 static inline snd_pcm_uframes_t snd_pcm_mmap_hw_rewindable(snd_pcm_t *pcm)
560 {
561 	snd_pcm_sframes_t ret = snd_pcm_mmap_hw_avail(pcm);
562 	return (ret >= 0) ? ret : 0;
563 }
564 
snd_pcm_mmap_areas(snd_pcm_t * pcm)565 static inline const snd_pcm_channel_area_t *snd_pcm_mmap_areas(snd_pcm_t *pcm)
566 {
567 	if (pcm->stopped_areas &&
568 	    __snd_pcm_state(pcm) != SND_PCM_STATE_RUNNING)
569 		return pcm->stopped_areas;
570 	return pcm->running_areas;
571 }
572 
snd_pcm_mmap_offset(snd_pcm_t * pcm)573 static inline snd_pcm_uframes_t snd_pcm_mmap_offset(snd_pcm_t *pcm)
574 {
575         assert(pcm);
576 	return *pcm->appl.ptr % pcm->buffer_size;
577 }
578 
snd_pcm_mmap_hw_offset(snd_pcm_t * pcm)579 static inline snd_pcm_uframes_t snd_pcm_mmap_hw_offset(snd_pcm_t *pcm)
580 {
581         assert(pcm);
582 	return *pcm->hw.ptr % pcm->buffer_size;
583 }
584 
snd_pcm_mmap_playback_delay(snd_pcm_t * pcm)585 static inline snd_pcm_uframes_t snd_pcm_mmap_playback_delay(snd_pcm_t *pcm)
586 {
587 	return snd_pcm_mmap_playback_hw_avail(pcm);
588 }
589 
snd_pcm_mmap_capture_delay(snd_pcm_t * pcm)590 static inline snd_pcm_uframes_t snd_pcm_mmap_capture_delay(snd_pcm_t *pcm)
591 {
592 	return snd_pcm_mmap_capture_hw_avail(pcm);
593 }
594 
snd_pcm_mmap_delay(snd_pcm_t * pcm)595 static inline snd_pcm_sframes_t snd_pcm_mmap_delay(snd_pcm_t *pcm)
596 {
597 	if (pcm->stream == SND_PCM_STREAM_PLAYBACK)
598 		return snd_pcm_mmap_playback_delay(pcm);
599 	else
600 		return snd_pcm_mmap_capture_delay(pcm);
601 }
602 
snd_pcm_channel_area_addr(const snd_pcm_channel_area_t * area,snd_pcm_uframes_t offset)603 static inline void *snd_pcm_channel_area_addr(const snd_pcm_channel_area_t *area, snd_pcm_uframes_t offset)
604 {
605 	unsigned int bitofs = area->first + area->step * offset;
606 	assert(bitofs % 8 == 0);
607 	return (char *) area->addr + bitofs / 8;
608 }
609 
snd_pcm_channel_area_step(const snd_pcm_channel_area_t * area)610 static inline unsigned int snd_pcm_channel_area_step(const snd_pcm_channel_area_t *area)
611 {
612 	assert(area->step % 8 == 0);
613 	return area->step / 8;
614 }
615 
_snd_pcm_writei(snd_pcm_t * pcm,const void * buffer,snd_pcm_uframes_t size)616 static inline snd_pcm_sframes_t _snd_pcm_writei(snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size)
617 {
618 	/* lock handled in the callback */
619 	if (!pcm->fast_ops->writei)
620 		return -ENOSYS;
621 	return pcm->fast_ops->writei(pcm->fast_op_arg, buffer, size);
622 }
623 
_snd_pcm_writen(snd_pcm_t * pcm,void ** bufs,snd_pcm_uframes_t size)624 static inline snd_pcm_sframes_t _snd_pcm_writen(snd_pcm_t *pcm, void **bufs, snd_pcm_uframes_t size)
625 {
626 	/* lock handled in the callback */
627 	if (!pcm->fast_ops->writen)
628 		return -ENOSYS;
629 	return pcm->fast_ops->writen(pcm->fast_op_arg, bufs, size);
630 }
631 
_snd_pcm_readi(snd_pcm_t * pcm,void * buffer,snd_pcm_uframes_t size)632 static inline snd_pcm_sframes_t _snd_pcm_readi(snd_pcm_t *pcm, void *buffer, snd_pcm_uframes_t size)
633 {
634 	/* lock handled in the callback */
635 	if (!pcm->fast_ops->readi)
636 		return -ENOSYS;
637 	return pcm->fast_ops->readi(pcm->fast_op_arg, buffer, size);
638 }
639 
_snd_pcm_readn(snd_pcm_t * pcm,void ** bufs,snd_pcm_uframes_t size)640 static inline snd_pcm_sframes_t _snd_pcm_readn(snd_pcm_t *pcm, void **bufs, snd_pcm_uframes_t size)
641 {
642 	/* lock handled in the callback */
643 	if (!pcm->fast_ops->readn)
644 		return -ENOSYS;
645 	return pcm->fast_ops->readn(pcm->fast_op_arg, bufs, size);
646 }
647 
muldiv(int a,int b,int c,int * r)648 static inline int muldiv(int a, int b, int c, int *r)
649 {
650 	int64_t n = (int64_t)a * b;
651 	int64_t v = n / c;
652 	if (v > INT_MAX) {
653 		*r = 0;
654 		return INT_MAX;
655 	}
656 	if (v < INT_MIN) {
657 		*r = 0;
658 		return INT_MIN;
659 	}
660 	*r = n % c;
661 	return v;
662 }
663 
muldiv_down(int a,int b,int c)664 static inline int muldiv_down(int a, int b, int c)
665 {
666 	int64_t v = (int64_t)a * b / c;
667 	if (v > INT_MAX) {
668 		return INT_MAX;
669 	}
670 	if (v < INT_MIN) {
671 		return INT_MIN;
672 	}
673 	return v;
674 }
675 
muldiv_near(int a,int b,int c)676 static inline int muldiv_near(int a, int b, int c)
677 {
678 	int r;
679 	int n = muldiv(a, b, c, &r);
680 	if (r >= (c + 1) / 2)
681 		n++;
682 	return n;
683 }
684 
685 int snd_pcm_hw_refine(snd_pcm_t *pcm, snd_pcm_hw_params_t *params);
686 int _snd_pcm_hw_params_internal(snd_pcm_t *pcm, snd_pcm_hw_params_t *params);
687 #undef _snd_pcm_hw_params
688 int snd_pcm_hw_refine_soft(snd_pcm_t *pcm, snd_pcm_hw_params_t *params);
689 int snd_pcm_hw_refine_slave(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
690 			    int (*cprepare)(snd_pcm_t *pcm,
691 					    snd_pcm_hw_params_t *params),
692 			    int (*cchange)(snd_pcm_t *pcm,
693 					   snd_pcm_hw_params_t *params,
694 					   snd_pcm_hw_params_t *sparams),
695 			    int (*sprepare)(snd_pcm_t *pcm,
696 					    snd_pcm_hw_params_t *params),
697 			    int (*schange)(snd_pcm_t *pcm,
698 					   snd_pcm_hw_params_t *params,
699 					   snd_pcm_hw_params_t *sparams),
700 			    int (*srefine)(snd_pcm_t *pcm,
701 					   snd_pcm_hw_params_t *sparams));
702 int snd_pcm_hw_params_slave(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
703 			    int (*cchange)(snd_pcm_t *pcm,
704 					   snd_pcm_hw_params_t *params,
705 					   snd_pcm_hw_params_t *sparams),
706 			    int (*sprepare)(snd_pcm_t *pcm,
707 					    snd_pcm_hw_params_t *params),
708 			    int (*schange)(snd_pcm_t *pcm,
709 					   snd_pcm_hw_params_t *params,
710 					   snd_pcm_hw_params_t *sparams),
711 			    int (*sparams)(snd_pcm_t *pcm,
712 					   snd_pcm_hw_params_t *sparams));
713 
714 
715 void _snd_pcm_hw_params_any(snd_pcm_hw_params_t *params);
716 void _snd_pcm_hw_param_set_empty(snd_pcm_hw_params_t *params,
717 				 snd_pcm_hw_param_t var);
718 int _snd_pcm_hw_param_set_interval(snd_pcm_hw_params_t *params,
719 				   snd_pcm_hw_param_t var,
720 				   const snd_interval_t *val);
721 int _snd_pcm_hw_param_set_mask(snd_pcm_hw_params_t *params,
722 			   snd_pcm_hw_param_t var, const snd_mask_t *mask);
723 int _snd_pcm_hw_param_first(snd_pcm_hw_params_t *params,
724 			    snd_pcm_hw_param_t var);
725 int _snd_pcm_hw_param_last(snd_pcm_hw_params_t *params,
726 			   snd_pcm_hw_param_t var);
727 int _snd_pcm_hw_param_set(snd_pcm_hw_params_t *params,
728 			  snd_pcm_hw_param_t var, unsigned int val, int dir);
_snd_pcm_hw_params_set_format(snd_pcm_hw_params_t * params,snd_pcm_format_t val)729 static inline int _snd_pcm_hw_params_set_format(snd_pcm_hw_params_t *params,
730 						snd_pcm_format_t val)
731 {
732 	return _snd_pcm_hw_param_set(params, SND_PCM_HW_PARAM_FORMAT,
733 				     (unsigned long) val, 0);
734 }
735 
_snd_pcm_hw_params_set_subformat(snd_pcm_hw_params_t * params,snd_pcm_subformat_t val)736 static inline int _snd_pcm_hw_params_set_subformat(snd_pcm_hw_params_t *params,
737 				     snd_pcm_subformat_t val)
738 {
739 	return _snd_pcm_hw_param_set(params, SND_PCM_HW_PARAM_SUBFORMAT,
740 				     (unsigned long) val, 0);
741 }
742 
743 int _snd_pcm_hw_param_set_min(snd_pcm_hw_params_t *params,
744 			      snd_pcm_hw_param_t var, unsigned int val, int dir);
745 int _snd_pcm_hw_param_set_max(snd_pcm_hw_params_t *params,
746 			      snd_pcm_hw_param_t var, unsigned int val, int dir);
747 int _snd_pcm_hw_param_set_minmax(snd_pcm_hw_params_t *params,
748 				 snd_pcm_hw_param_t var,
749 				 unsigned int min, int mindir,
750 				 unsigned int max, int maxdir);
751 int _snd_pcm_hw_param_refine(snd_pcm_hw_params_t *params,
752 			     snd_pcm_hw_param_t var,
753 			     const snd_pcm_hw_params_t *src);
754 int _snd_pcm_hw_params_refine(snd_pcm_hw_params_t *params,
755 			      unsigned int vars,
756 			      const snd_pcm_hw_params_t *src);
757 int snd_pcm_hw_param_refine_near(snd_pcm_t *pcm,
758 				 snd_pcm_hw_params_t *params,
759 				 snd_pcm_hw_param_t var,
760 				 const snd_pcm_hw_params_t *src);
761 int snd_pcm_hw_param_refine_multiple(snd_pcm_t *pcm,
762 				     snd_pcm_hw_params_t *params,
763 				     snd_pcm_hw_param_t var,
764 				     const snd_pcm_hw_params_t *src);
765 int snd_pcm_hw_param_empty(const snd_pcm_hw_params_t *params,
766 			   snd_pcm_hw_param_t var);
767 int snd_pcm_hw_param_always_eq(const snd_pcm_hw_params_t *params,
768 			       snd_pcm_hw_param_t var,
769 			       const snd_pcm_hw_params_t *params1);
770 int snd_pcm_hw_param_never_eq(const snd_pcm_hw_params_t *params,
771 			      snd_pcm_hw_param_t var,
772 			      const snd_pcm_hw_params_t *params1);
773 const snd_mask_t *snd_pcm_hw_param_get_mask(const snd_pcm_hw_params_t *params,
774 					      snd_pcm_hw_param_t var);
775 const snd_interval_t *snd_pcm_hw_param_get_interval(const snd_pcm_hw_params_t *params,
776 						      snd_pcm_hw_param_t var);
777 
778 int snd_pcm_hw_param_any(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
779 			 snd_pcm_hw_param_t var);
780 int snd_pcm_hw_param_set_integer(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
781 				 snd_set_mode_t mode,
782 				 snd_pcm_hw_param_t var);
783 int snd_pcm_hw_param_set_first(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
784 			       snd_pcm_hw_param_t var, unsigned int *rval, int *dir);
785 int snd_pcm_hw_param_set_last(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
786 			      snd_pcm_hw_param_t var, unsigned int *rval, int *dir);
787 int snd_pcm_hw_param_set_near(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
788 			      snd_pcm_hw_param_t var, unsigned int *val, int *dir);
789 int snd_pcm_hw_param_set_min(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
790 			     snd_set_mode_t mode,
791 			     snd_pcm_hw_param_t var,
792 			     unsigned int *val, int *dir);
793 int snd_pcm_hw_param_set_max(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
794 			     snd_set_mode_t mode,
795 			     snd_pcm_hw_param_t var, unsigned int *val, int *dir);
796 int snd_pcm_hw_param_set_minmax(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
797 				snd_set_mode_t mode,
798 				snd_pcm_hw_param_t var,
799 				unsigned int *min, int *mindir,
800 				unsigned int *max, int *maxdir);
801 int snd_pcm_hw_param_set(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
802 			 snd_set_mode_t mode,
803 			 snd_pcm_hw_param_t var, unsigned int val, int dir);
804 int snd_pcm_hw_param_set_mask(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
805 			      snd_set_mode_t mode,
806 			      snd_pcm_hw_param_t var, const snd_mask_t *mask);
807 int snd_pcm_hw_param_get(const snd_pcm_hw_params_t *params, snd_pcm_hw_param_t var,
808 			 unsigned int *val, int *dir);
809 int snd_pcm_hw_param_get_min(const snd_pcm_hw_params_t *params,
810 			     snd_pcm_hw_param_t var,
811 			     unsigned int *val, int *dir);
812 int snd_pcm_hw_param_get_max(const snd_pcm_hw_params_t *params,
813 			     snd_pcm_hw_param_t var,
814 			     unsigned int *val, int *dir);
815 
816 #ifdef INTERNAL
817 snd_pcm_sframes_t INTERNAL(snd_pcm_forward)(snd_pcm_t *pcm, snd_pcm_uframes_t frames);
818 
819 int INTERNAL(snd_pcm_hw_params_get_access)(const snd_pcm_hw_params_t *params, snd_pcm_access_t *access);
820 int snd_pcm_hw_params_test_access(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_t access);
821 int snd_pcm_hw_params_set_access(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_t access);
822 int INTERNAL(snd_pcm_hw_params_set_access_first)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_t *access);
823 int INTERNAL(snd_pcm_hw_params_set_access_last)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_t *access);
824 int snd_pcm_hw_params_set_access_mask(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_mask_t *mask);
825 int snd_pcm_hw_params_get_access_mask(snd_pcm_hw_params_t *params, snd_pcm_access_mask_t *mask);
826 
827 int INTERNAL(snd_pcm_hw_params_get_format)(const snd_pcm_hw_params_t *params, snd_pcm_format_t *val);
828 int snd_pcm_hw_params_test_format(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_t val);
829 int snd_pcm_hw_params_set_format(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_t val);
830 int INTERNAL(snd_pcm_hw_params_set_format_first)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_t *format);
831 int INTERNAL(snd_pcm_hw_params_set_format_last)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_t *format);
832 int snd_pcm_hw_params_set_format_mask(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_mask_t *mask);
833 void snd_pcm_hw_params_get_format_mask(snd_pcm_hw_params_t *params, snd_pcm_format_mask_t *mask);
834 
835 int INTERNAL(snd_pcm_hw_params_get_subformat)(const snd_pcm_hw_params_t *params, snd_pcm_subformat_t *subformat);
836 int snd_pcm_hw_params_test_subformat(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_subformat_t subformat);
837 int snd_pcm_hw_params_set_subformat(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_subformat_t subformat);
838 int INTERNAL(snd_pcm_hw_params_set_subformat_first)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_subformat_t *subformat);
839 int INTERNAL(snd_pcm_hw_params_set_subformat_last)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_subformat_t *subformat);
840 int snd_pcm_hw_params_set_subformat_mask(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_subformat_mask_t *mask);
841 void snd_pcm_hw_params_get_subformat_mask(snd_pcm_hw_params_t *params, snd_pcm_subformat_mask_t *mask);
842 
843 int INTERNAL(snd_pcm_hw_params_get_channels)(const snd_pcm_hw_params_t *params, unsigned int *val);
844 int INTERNAL(snd_pcm_hw_params_get_channels_min)(const snd_pcm_hw_params_t *params, unsigned int *val);
845 int INTERNAL(snd_pcm_hw_params_get_channels_max)(const snd_pcm_hw_params_t *params, unsigned int *val);
846 int snd_pcm_hw_params_test_channels(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val);
847 int snd_pcm_hw_params_set_channels(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val);
848 int snd_pcm_hw_params_set_channels_min(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val);
849 int snd_pcm_hw_params_set_channels_max(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val);
850 int snd_pcm_hw_params_set_channels_minmax(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *min, unsigned int *max);
851 int INTERNAL(snd_pcm_hw_params_set_channels_near)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val);
852 int INTERNAL(snd_pcm_hw_params_set_channels_first)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val);
853 int INTERNAL(snd_pcm_hw_params_set_channels_last)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val);
854 
855 int INTERNAL(snd_pcm_hw_params_get_rate)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
856 int INTERNAL(snd_pcm_hw_params_get_rate_min)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
857 int INTERNAL(snd_pcm_hw_params_get_rate_max)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
858 int snd_pcm_hw_params_test_rate(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir);
859 int snd_pcm_hw_params_set_rate(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir);
860 int snd_pcm_hw_params_set_rate_min(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
861 int snd_pcm_hw_params_set_rate_max(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
862 int snd_pcm_hw_params_set_rate_minmax(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *min, int *mindir, unsigned int *max, int *maxdir);
863 int INTERNAL(snd_pcm_hw_params_set_rate_near)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
864 int INTERNAL(snd_pcm_hw_params_set_rate_first)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
865 int INTERNAL(snd_pcm_hw_params_set_rate_last)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
866 
867 int INTERNAL(snd_pcm_hw_params_get_period_time)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
868 int INTERNAL(snd_pcm_hw_params_get_period_time_min)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
869 int INTERNAL(snd_pcm_hw_params_get_period_time_max)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
870 int snd_pcm_hw_params_test_period_time(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir);
871 int snd_pcm_hw_params_set_period_time(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir);
872 int snd_pcm_hw_params_set_period_time_min(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
873 int snd_pcm_hw_params_set_period_time_max(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
874 int snd_pcm_hw_params_set_period_time_minmax(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *min, int *mindir, unsigned int *max, int *maxdir);
875 int INTERNAL(snd_pcm_hw_params_set_period_time_near)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
876 int INTERNAL(snd_pcm_hw_params_set_period_time_first)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
877 int INTERNAL(snd_pcm_hw_params_set_period_time_last)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
878 
879 int INTERNAL(snd_pcm_hw_params_get_period_size)(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *frames, int *dir);
880 int INTERNAL(snd_pcm_hw_params_get_period_size_min)(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *frames, int *dir);
881 int INTERNAL(snd_pcm_hw_params_get_period_size_max)(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *frames, int *dir);
882 int snd_pcm_hw_params_test_period_size(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t val, int dir);
883 int snd_pcm_hw_params_set_period_size(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t val, int dir);
884 int snd_pcm_hw_params_set_period_size_min(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val, int *dir);
885 int snd_pcm_hw_params_set_period_size_max(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val, int *dir);
886 int snd_pcm_hw_params_set_period_size_minmax(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *min, int *mindir, snd_pcm_uframes_t *max, int *maxdir);
887 int INTERNAL(snd_pcm_hw_params_set_period_size_near)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val, int *dir);
888 int INTERNAL(snd_pcm_hw_params_set_period_size_first)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val, int *dir);
889 int INTERNAL(snd_pcm_hw_params_set_period_size_last)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val, int *dir);
890 int snd_pcm_hw_params_set_period_size_integer(snd_pcm_t *pcm, snd_pcm_hw_params_t *params);
891 
892 int INTERNAL(snd_pcm_hw_params_get_periods)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
893 int INTERNAL(snd_pcm_hw_params_get_periods_min)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
894 int INTERNAL(snd_pcm_hw_params_get_periods_max)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
895 int snd_pcm_hw_params_test_periods(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir);
896 int snd_pcm_hw_params_set_periods(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir);
897 int snd_pcm_hw_params_set_periods_min(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
898 int snd_pcm_hw_params_set_periods_max(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
899 int snd_pcm_hw_params_set_periods_minmax(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *min, int *mindir, unsigned int *max, int *maxdir);
900 int INTERNAL(snd_pcm_hw_params_set_periods_near)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
901 int INTERNAL(snd_pcm_hw_params_set_periods_first)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
902 int INTERNAL(snd_pcm_hw_params_set_periods_last)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
903 int snd_pcm_hw_params_set_periods_integer(snd_pcm_t *pcm, snd_pcm_hw_params_t *params);
904 
905 int INTERNAL(snd_pcm_hw_params_get_buffer_time)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
906 int INTERNAL(snd_pcm_hw_params_get_buffer_time_min)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
907 int INTERNAL(snd_pcm_hw_params_get_buffer_time_max)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
908 int snd_pcm_hw_params_test_buffer_time(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir);
909 int snd_pcm_hw_params_set_buffer_time(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir);
910 int snd_pcm_hw_params_set_buffer_time_min(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
911 int snd_pcm_hw_params_set_buffer_time_max(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
912 int snd_pcm_hw_params_set_buffer_time_minmax(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *min, int *mindir, unsigned int *max, int *maxdir);
913 int INTERNAL(snd_pcm_hw_params_set_buffer_time_near)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
914 int INTERNAL(snd_pcm_hw_params_set_buffer_time_first)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
915 int INTERNAL(snd_pcm_hw_params_set_buffer_time_last)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
916 
917 int INTERNAL(snd_pcm_hw_params_get_buffer_size)(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val);
918 int INTERNAL(snd_pcm_hw_params_get_buffer_size_min)(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val);
919 int INTERNAL(snd_pcm_hw_params_get_buffer_size_max)(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val);
920 int snd_pcm_hw_params_test_buffer_size(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t val);
921 int snd_pcm_hw_params_set_buffer_size(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t val);
922 int snd_pcm_hw_params_set_buffer_size_min(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val);
923 int snd_pcm_hw_params_set_buffer_size_max(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val);
924 int snd_pcm_hw_params_set_buffer_size_minmax(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *min, snd_pcm_uframes_t *max);
925 int INTERNAL(snd_pcm_hw_params_set_buffer_size_near)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val);
926 int INTERNAL(snd_pcm_hw_params_set_buffer_size_first)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val);
927 int INTERNAL(snd_pcm_hw_params_set_buffer_size_last)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val);
928 
929 int snd_pcm_sw_params_set_tstamp_mode(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_tstamp_t val);
930 int INTERNAL(snd_pcm_sw_params_get_tstamp_mode)(const snd_pcm_sw_params_t *params, snd_pcm_tstamp_t *val);
931 int snd_pcm_sw_params_set_avail_min(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val);
932 int INTERNAL(snd_pcm_sw_params_get_avail_min)(const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val);
933 int snd_pcm_sw_params_set_start_threshold(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val);
934 int INTERNAL(snd_pcm_sw_params_get_start_threshold)(const snd_pcm_sw_params_t *paramsm, snd_pcm_uframes_t *val);
935 int snd_pcm_sw_params_set_stop_threshold(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val);
936 int INTERNAL(snd_pcm_sw_params_get_stop_threshold)(const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val);
937 int snd_pcm_sw_params_set_silence_threshold(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val);
938 int INTERNAL(snd_pcm_sw_params_get_silence_threshold)(const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val);
939 int snd_pcm_sw_params_set_silence_size(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val);
940 int INTERNAL(snd_pcm_sw_params_get_silence_size)(const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val);
941 #endif /* INTERNAL */
942 
943 const char *snd_pcm_hw_param_name(snd_pcm_hw_param_t param);
944 void snd_pcm_hw_param_dump(const snd_pcm_hw_params_t *params,
945 			   snd_pcm_hw_param_t var, snd_output_t *out);
946 #if 0
947 int snd_pcm_hw_strategy_simple_near(snd_pcm_hw_strategy_t *strategy, int order,
948 				    snd_pcm_hw_param_t var,
949 				    unsigned int best,
950 				    unsigned int mul);
951 int snd_pcm_hw_strategy_simple_choices(snd_pcm_hw_strategy_t *strategy, int order,
952 				       snd_pcm_hw_param_t var,
953 				       unsigned int count,
954 				       snd_pcm_hw_strategy_simple_choices_list_t *choices);
955 #endif
956 
957 #define SCONF_MANDATORY	1
958 #define SCONF_UNCHANGED	2
959 
960 int snd_pcm_slave_conf(snd_config_t *root, snd_config_t *conf,
961 		       snd_config_t **pcm_conf, unsigned int count, ...);
962 
963 #define SND_PCM_APPEND	(1<<8)
964 
965 int snd_pcm_open_named_slave(snd_pcm_t **pcmp, const char *name,
966 			     snd_config_t *root,
967 			     snd_config_t *conf, snd_pcm_stream_t stream,
968 			     int mode, snd_config_t *parent_conf);
969 static inline int
snd_pcm_open_slave(snd_pcm_t ** pcmp,snd_config_t * root,snd_config_t * conf,snd_pcm_stream_t stream,int mode,snd_config_t * parent_conf)970 snd_pcm_open_slave(snd_pcm_t **pcmp, snd_config_t *root,
971 		   snd_config_t *conf, snd_pcm_stream_t stream,
972 		   int mode, snd_config_t *parent_conf)
973 {
974 	return snd_pcm_open_named_slave(pcmp, NULL, root, conf, stream,
975 					mode, parent_conf);
976 }
977 
978 #define snd_pcm_conf_generic_id(id) _snd_conf_generic_id(id)
979 
980 int snd_pcm_hw_open_fd(snd_pcm_t **pcmp, const char *name, int fd,
981 		       int sync_ptr_ioctl);
982 int __snd_pcm_mmap_emul_open(snd_pcm_t **pcmp, const char *name,
983 			     snd_pcm_t *slave, int close_slave);
984 
985 int snd_pcm_wait_nocheck(snd_pcm_t *pcm, int timeout);
986 
987 const snd_config_t *snd_pcm_rate_get_default_converter(snd_config_t *root);
988 
989 #define SND_PCM_HW_PARBIT_ACCESS	(1U << SND_PCM_HW_PARAM_ACCESS)
990 #define SND_PCM_HW_PARBIT_FORMAT	(1U << SND_PCM_HW_PARAM_FORMAT)
991 #define SND_PCM_HW_PARBIT_SUBFORMAT	(1U << SND_PCM_HW_PARAM_SUBFORMAT)
992 #define SND_PCM_HW_PARBIT_CHANNELS	(1U << SND_PCM_HW_PARAM_CHANNELS)
993 #define SND_PCM_HW_PARBIT_RATE		(1U << SND_PCM_HW_PARAM_RATE)
994 #define SND_PCM_HW_PARBIT_PERIOD_TIME	(1U << SND_PCM_HW_PARAM_PERIOD_TIME)
995 #define SND_PCM_HW_PARBIT_PERIOD_SIZE	(1U << SND_PCM_HW_PARAM_PERIOD_SIZE)
996 #define SND_PCM_HW_PARBIT_PERIODS	(1U << SND_PCM_HW_PARAM_PERIODS)
997 #define SND_PCM_HW_PARBIT_BUFFER_TIME	(1U << SND_PCM_HW_PARAM_BUFFER_TIME)
998 #define SND_PCM_HW_PARBIT_BUFFER_SIZE	(1U << SND_PCM_HW_PARAM_BUFFER_SIZE)
999 #define SND_PCM_HW_PARBIT_SAMPLE_BITS	(1U << SND_PCM_HW_PARAM_SAMPLE_BITS)
1000 #define SND_PCM_HW_PARBIT_FRAME_BITS	(1U << SND_PCM_HW_PARAM_FRAME_BITS)
1001 #define SND_PCM_HW_PARBIT_PERIOD_BYTES	(1U << SND_PCM_HW_PARAM_PERIOD_BYTES)
1002 #define SND_PCM_HW_PARBIT_BUFFER_BYTES	(1U << SND_PCM_HW_PARAM_BUFFER_BYTES)
1003 #define SND_PCM_HW_PARBIT_TICK_TIME	(1U << SND_PCM_HW_PARAM_TICK_TIME)
1004 
1005 
1006 #define SND_PCM_ACCBIT_MMAP { ((1U << SND_PCM_ACCESS_MMAP_INTERLEAVED) | \
1007 			     (1U << SND_PCM_ACCESS_MMAP_NONINTERLEAVED) | \
1008 			     (1U << SND_PCM_ACCESS_MMAP_COMPLEX)) }
1009 #define SND_PCM_ACCBIT_MMAPI { (1U << SND_PCM_ACCESS_MMAP_INTERLEAVED) }
1010 #define SND_PCM_ACCBIT_MMAPN { (1U << SND_PCM_ACCESS_MMAP_NONINTERLEAVED) }
1011 #define SND_PCM_ACCBIT_MMAPC { (1U << SND_PCM_ACCESS_MMAP_COMPLEX) }
1012 
1013 #define SND_PCM_ACCBIT_SHM { ((1U << SND_PCM_ACCESS_MMAP_INTERLEAVED) | \
1014 			    (1U << SND_PCM_ACCESS_RW_INTERLEAVED) | \
1015 			    (1U << SND_PCM_ACCESS_MMAP_NONINTERLEAVED) | \
1016 			    (1U << SND_PCM_ACCESS_RW_NONINTERLEAVED)) }
1017 #define SND_PCM_ACCBIT_SHMI { ((1U << SND_PCM_ACCESS_MMAP_INTERLEAVED) | \
1018 			     (1U << SND_PCM_ACCESS_RW_INTERLEAVED)) }
1019 #define SND_PCM_ACCBIT_SHMN { ((1U << SND_PCM_ACCESS_MMAP_NONINTERLEAVED) | \
1020 			     (1U << SND_PCM_ACCESS_RW_NONINTERLEAVED)) }
1021 
1022 #define SND_PCM_FMTBIT_LINEAR \
1023 	{ ((1U << SND_PCM_FORMAT_S8) | \
1024 	 (1U << SND_PCM_FORMAT_U8) | \
1025 	 (1U << SND_PCM_FORMAT_S16_LE) | \
1026 	 (1U << SND_PCM_FORMAT_S16_BE) | \
1027 	 (1U << SND_PCM_FORMAT_U16_LE) | \
1028 	 (1U << SND_PCM_FORMAT_U16_BE) | \
1029 	 (1U << SND_PCM_FORMAT_S20_LE) | \
1030 	 (1U << SND_PCM_FORMAT_S20_BE) | \
1031 	 (1U << SND_PCM_FORMAT_U20_LE) | \
1032 	 (1U << SND_PCM_FORMAT_U20_BE) | \
1033 	 (1U << SND_PCM_FORMAT_S24_LE) | \
1034 	 (1U << SND_PCM_FORMAT_S24_BE) | \
1035 	 (1U << SND_PCM_FORMAT_U24_LE) | \
1036 	 (1U << SND_PCM_FORMAT_U24_BE) | \
1037 	 (1U << SND_PCM_FORMAT_S32_LE) | \
1038 	 (1U << SND_PCM_FORMAT_S32_BE) | \
1039 	 (1U << SND_PCM_FORMAT_U32_LE) | \
1040 	 (1U << SND_PCM_FORMAT_U32_BE)), \
1041 	((1U << (SND_PCM_FORMAT_S24_3LE - 32)) | \
1042 	 (1U << (SND_PCM_FORMAT_U24_3LE - 32)) | \
1043 	 (1U << (SND_PCM_FORMAT_S24_3BE - 32)) | \
1044 	 (1U << (SND_PCM_FORMAT_U24_3BE - 32)) | \
1045 	 (1U << (SND_PCM_FORMAT_S20_3LE - 32)) | \
1046 	 (1U << (SND_PCM_FORMAT_U20_3LE - 32)) | \
1047 	 (1U << (SND_PCM_FORMAT_S20_3BE - 32)) | \
1048 	 (1U << (SND_PCM_FORMAT_U20_3BE - 32)) | \
1049 	 (1U << (SND_PCM_FORMAT_S18_3LE - 32)) | \
1050 	 (1U << (SND_PCM_FORMAT_U18_3LE - 32)) | \
1051 	 (1U << (SND_PCM_FORMAT_S18_3BE - 32)) | \
1052 	 (1U << (SND_PCM_FORMAT_U18_3BE - 32))) }
1053 
1054 
1055 #define SND_PCM_FMTBIT_FLOAT \
1056 	{ ((1U << SND_PCM_FORMAT_FLOAT_LE) | \
1057 	 (1U << SND_PCM_FORMAT_FLOAT_BE) | \
1058 	 (1U << SND_PCM_FORMAT_FLOAT64_LE) | \
1059 	 (1U << SND_PCM_FORMAT_FLOAT64_BE)) }
1060 
1061 
1062 typedef union snd_tmp_float {
1063 	float f;
1064 	int32_t i;
1065 } snd_tmp_float_t;
1066 
1067 typedef union snd_tmp_double {
1068 	double d;
1069 	int64_t l;
1070 } snd_tmp_double_t;
1071 
1072 /* get the current timestamp */
1073 #ifdef HAVE_CLOCK_GETTIME
gettimestamp(snd_htimestamp_t * tstamp,snd_pcm_tstamp_type_t tstamp_type)1074 static inline void gettimestamp(snd_htimestamp_t *tstamp,
1075 				snd_pcm_tstamp_type_t tstamp_type)
1076 {
1077 	clockid_t id;
1078 
1079 	switch (tstamp_type) {
1080 #ifdef CLOCK_MONOTONIC_RAW
1081 	case SND_PCM_TSTAMP_TYPE_MONOTONIC_RAW:
1082 		id = CLOCK_MONOTONIC_RAW;
1083 		break;
1084 #endif
1085 #ifdef CLOCK_MONOTONIC
1086 	case SND_PCM_TSTAMP_TYPE_MONOTONIC:
1087 		id = CLOCK_MONOTONIC;
1088 		break;
1089 #endif
1090 	default:
1091 		id = CLOCK_REALTIME;
1092 		break;
1093 	}
1094 	clock_gettime(id, tstamp);
1095 }
1096 #else /* HAVE_CLOCK_GETTIME */
gettimestamp(snd_htimestamp_t * tstamp,snd_pcm_tstamp_type_t tstamp_type)1097 static inline void gettimestamp(snd_htimestamp_t *tstamp,
1098 				snd_pcm_tstamp_type_t tstamp_type)
1099 {
1100 	struct timeval tv;
1101 
1102 	gettimeofday(&tv, 0);
1103 	tstamp->tv_sec = tv.tv_sec;
1104 	tstamp->tv_nsec = tv.tv_usec * 1000L;
1105 }
1106 #endif /* HAVE_CLOCK_GETTIME */
1107 
1108 snd_pcm_chmap_query_t **
1109 _snd_pcm_make_single_query_chmaps(const snd_pcm_chmap_t *src);
1110 snd_pcm_chmap_t *_snd_pcm_copy_chmap(const snd_pcm_chmap_t *src);
1111 snd_pcm_chmap_query_t **
1112 _snd_pcm_copy_chmap_query(snd_pcm_chmap_query_t * const *src);
1113 snd_pcm_chmap_query_t **
1114 _snd_pcm_parse_config_chmaps(snd_config_t *conf);
1115 snd_pcm_chmap_t *
1116 _snd_pcm_choose_fixed_chmap(snd_pcm_t *pcm, snd_pcm_chmap_query_t * const *maps);
1117 
1118 /* return true if the PCM stream may wait to get avail_min space */
snd_pcm_may_wait_for_avail_min(snd_pcm_t * pcm,snd_pcm_uframes_t avail)1119 static inline int snd_pcm_may_wait_for_avail_min(snd_pcm_t *pcm, snd_pcm_uframes_t avail)
1120 {
1121 	if (avail >= pcm->avail_min)
1122 		return 0;
1123 	if (pcm->fast_ops->may_wait_for_avail_min)
1124 		return pcm->fast_ops->may_wait_for_avail_min(pcm, avail);
1125 	return 1;
1126 }
1127 
1128 /* hack to access to internal period_event in snd_pcm_sw_parmams */
sw_get_period_event(const snd_pcm_sw_params_t * params)1129 static inline int sw_get_period_event(const snd_pcm_sw_params_t *params)
1130 {
1131 	return params->reserved[sizeof(params->reserved) / sizeof(params->reserved[0])- 1];
1132 }
1133 
sw_set_period_event(snd_pcm_sw_params_t * params,int val)1134 static inline void sw_set_period_event(snd_pcm_sw_params_t *params, int val)
1135 {
1136 	params->reserved[sizeof(params->reserved) / sizeof(params->reserved[0]) - 1] = val;
1137 }
1138 
1139 #define PCMINABORT(pcm) (((pcm)->mode & SND_PCM_ABORT) != 0)
1140 
1141 #ifdef THREAD_SAFE_API
1142 /*
1143  * __snd_pcm_lock() and __snd_pcm_unlock() are used to lock/unlock the plugin
1144  * forcibly even if it's declared as thread-safe.  It's needed only for some
1145  * codes that are thread-unsafe per design (e.g. snd_pcm_nonblock()).
1146  *
1147  * OTOH, snd_pcm_lock() and snd_pcm_unlock() are used to lock/unlock the plugin
1148  * in normal situations.  They do lock/unlock only when the plugin is
1149  * thread-unsafe.
1150  *
1151  * Both __snd_pcm_lock() and snd_pcm_lock() (and their unlocks) wouldn't do
1152  * any action when the whole locking is disabled via $LIBASOUND_THREAD_SAFE=0.
1153  */
__snd_pcm_lock(snd_pcm_t * pcm)1154 static inline void __snd_pcm_lock(snd_pcm_t *pcm)
1155 {
1156 	if (pcm->lock_enabled)
1157 		pthread_mutex_lock(&pcm->lock);
1158 }
__snd_pcm_unlock(snd_pcm_t * pcm)1159 static inline void __snd_pcm_unlock(snd_pcm_t *pcm)
1160 {
1161 	if (pcm->lock_enabled)
1162 		pthread_mutex_unlock(&pcm->lock);
1163 }
snd_pcm_lock(snd_pcm_t * pcm)1164 static inline void snd_pcm_lock(snd_pcm_t *pcm)
1165 {
1166 	if (pcm->lock_enabled && pcm->need_lock)
1167 		pthread_mutex_lock(&pcm->lock);
1168 }
snd_pcm_unlock(snd_pcm_t * pcm)1169 static inline void snd_pcm_unlock(snd_pcm_t *pcm)
1170 {
1171 	if (pcm->lock_enabled && pcm->need_lock)
1172 		pthread_mutex_unlock(&pcm->lock);
1173 }
1174 #else /* THREAD_SAFE_API */
1175 #define __snd_pcm_lock(pcm)		do {} while (0)
1176 #define __snd_pcm_unlock(pcm)		do {} while (0)
1177 #define snd_pcm_lock(pcm)		do {} while (0)
1178 #define snd_pcm_unlock(pcm)		do {} while (0)
1179 #endif /* THREAD_SAFE_API */
1180