2e901d54ee51d3822a0bf15afe8057265627f983
[platform/upstream/alsa-lib.git] / src / pcm / pcm_local.h
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20  *
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <limits.h>
26 #include <sys/uio.h>
27
28 #define _snd_mask sndrv_mask
29 #define _snd_pcm_access_mask _snd_mask
30 #define _snd_pcm_format_mask _snd_mask
31 #define _snd_pcm_subformat_mask _snd_mask
32
33 #include "local.h"
34
35 #define SND_INTERVAL_INLINE
36 #include "interval.h"
37
38 #define SND_MASK_INLINE
39 #include "mask.h"
40
41 typedef enum sndrv_pcm_hw_param snd_pcm_hw_param_t;
42 #define SND_PCM_HW_PARAM_ACCESS SNDRV_PCM_HW_PARAM_ACCESS
43 #define SND_PCM_HW_PARAM_FIRST_MASK SNDRV_PCM_HW_PARAM_FIRST_MASK
44 #define SND_PCM_HW_PARAM_FORMAT SNDRV_PCM_HW_PARAM_FORMAT
45 #define SND_PCM_HW_PARAM_SUBFORMAT SNDRV_PCM_HW_PARAM_SUBFORMAT
46 #define SND_PCM_HW_PARAM_LAST_MASK SNDRV_PCM_HW_PARAM_LAST_MASK
47 #define SND_PCM_HW_PARAM_SAMPLE_BITS SNDRV_PCM_HW_PARAM_SAMPLE_BITS
48 #define SND_PCM_HW_PARAM_FIRST_INTERVAL SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
49 #define SND_PCM_HW_PARAM_FRAME_BITS SNDRV_PCM_HW_PARAM_FRAME_BITS
50 #define SND_PCM_HW_PARAM_CHANNELS SNDRV_PCM_HW_PARAM_CHANNELS
51 #define SND_PCM_HW_PARAM_RATE SNDRV_PCM_HW_PARAM_RATE
52 #define SND_PCM_HW_PARAM_PERIOD_TIME SNDRV_PCM_HW_PARAM_PERIOD_TIME
53 #define SND_PCM_HW_PARAM_PERIOD_SIZE SNDRV_PCM_HW_PARAM_PERIOD_SIZE
54 #define SND_PCM_HW_PARAM_PERIOD_BYTES SNDRV_PCM_HW_PARAM_PERIOD_BYTES
55 #define SND_PCM_HW_PARAM_PERIODS SNDRV_PCM_HW_PARAM_PERIODS
56 #define SND_PCM_HW_PARAM_BUFFER_TIME SNDRV_PCM_HW_PARAM_BUFFER_TIME
57 #define SND_PCM_HW_PARAM_BUFFER_SIZE SNDRV_PCM_HW_PARAM_BUFFER_SIZE
58 #define SND_PCM_HW_PARAM_BUFFER_BYTES SNDRV_PCM_HW_PARAM_BUFFER_BYTES
59 #define SND_PCM_HW_PARAM_TICK_TIME SNDRV_PCM_HW_PARAM_TICK_TIME
60 #define SND_PCM_HW_PARAM_LAST_INTERVAL SNDRV_PCM_HW_PARAM_LAST_INTERVAL
61 #define SND_PCM_HW_PARAM_LAST_MASK SNDRV_PCM_HW_PARAM_LAST_MASK
62 #define SND_PCM_HW_PARAM_FIRST_MASK SNDRV_PCM_HW_PARAM_FIRST_MASK
63 #define SND_PCM_HW_PARAM_LAST_INTERVAL SNDRV_PCM_HW_PARAM_LAST_INTERVAL
64 #define SND_PCM_HW_PARAM_FIRST_INTERVAL SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
65
66 /** device accepts mmaped access */
67 #define SND_PCM_INFO_MMAP SNDRV_PCM_INFO_MMAP
68 /** device accepts  mmaped access with sample resolution */
69 #define SND_PCM_INFO_MMAP_VALID SNDRV_PCM_INFO_MMAP_VALID
70 /** device is doing double buffering */
71 #define SND_PCM_INFO_DOUBLE SNDRV_PCM_INFO_DOUBLE
72 /** device transfers samples in batch */
73 #define SND_PCM_INFO_BATCH SNDRV_PCM_INFO_BATCH
74 /** device accepts interleaved samples */
75 #define SND_PCM_INFO_INTERLEAVED SNDRV_PCM_INFO_INTERLEAVED
76 /** device accepts non-interleaved samples */
77 #define SND_PCM_INFO_NONINTERLEAVED SNDRV_PCM_INFO_NONINTERLEAVED
78 /** device accepts complex sample organization */
79 #define SND_PCM_INFO_COMPLEX SNDRV_PCM_INFO_COMPLEX
80 /** device is capable block transfers */
81 #define SND_PCM_INFO_BLOCK_TRANSFER SNDRV_PCM_INFO_BLOCK_TRANSFER
82 /** device can detect DAC/ADC overrange */
83 #define SND_PCM_INFO_OVERRANGE SNDRV_PCM_INFO_OVERRANGE
84 /** device supports resume */
85 #define SND_PCM_INFO_RESUME SNDRV_PCM_INFO_RESUME
86 /** device is capable to pause */
87 #define SND_PCM_INFO_PAUSE SNDRV_PCM_INFO_PAUSE
88 /** device can do only half duplex */
89 #define SND_PCM_INFO_HALF_DUPLEX SNDRV_PCM_INFO_HALF_DUPLEX
90 /** device can do only joint duplex (same parameters) */
91 #define SND_PCM_INFO_JOINT_DUPLEX SNDRV_PCM_INFO_JOINT_DUPLEX
92 /** device can do a kind of synchronized start */
93 #define SND_PCM_INFO_SYNC_START SNDRV_PCM_INFO_SYNC_START
94 /** device can disable period wakeups */
95 #define SND_PCM_INFO_NO_PERIOD_WAKEUP SNDRV_PCM_INFO_NO_PERIOD_WAKEUP
96
97 #define SND_PCM_HW_PARAMS_NORESAMPLE SNDRV_PCM_HW_PARAMS_NORESAMPLE
98 #define SND_PCM_HW_PARAMS_EXPORT_BUFFER SNDRV_PCM_HW_PARAMS_EXPORT_BUFFER
99 #define SND_PCM_HW_PARAMS_NO_PERIOD_WAKEUP SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
100
101 #define SND_PCM_INFO_MONOTONIC  0x80000000
102
103 typedef struct _snd_pcm_rbptr {
104         snd_pcm_t *master;
105         volatile snd_pcm_uframes_t *ptr;
106         int fd;
107         off_t offset;
108         int link_dst_count;
109         snd_pcm_t **link_dst;
110         void *private_data;
111         void (*changed)(snd_pcm_t *pcm, snd_pcm_t *src);
112 } snd_pcm_rbptr_t;
113
114 typedef struct _snd_pcm_channel_info {
115         unsigned int channel;
116         void *addr;                     /* base address of channel samples */
117         unsigned int first;             /* offset to first sample in bits */
118         unsigned int step;              /* samples distance in bits */
119         enum { SND_PCM_AREA_SHM, SND_PCM_AREA_MMAP, SND_PCM_AREA_LOCAL } type;
120         union {
121                 struct {
122                         struct snd_shm_area *area;
123                         int shmid;
124                 } shm;
125                 struct {
126                         int fd;
127                         off_t offset;
128                 } mmap;
129         } u;
130         char reserved[64];
131 } snd_pcm_channel_info_t;
132
133 typedef struct {
134         int (*close)(snd_pcm_t *pcm);
135         int (*nonblock)(snd_pcm_t *pcm, int nonblock);
136         int (*async)(snd_pcm_t *pcm, int sig, pid_t pid);
137         int (*info)(snd_pcm_t *pcm, snd_pcm_info_t *info);
138         int (*hw_refine)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params);
139         int (*hw_params)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params);
140         int (*hw_free)(snd_pcm_t *pcm);
141         int (*sw_params)(snd_pcm_t *pcm, snd_pcm_sw_params_t *params);
142         int (*channel_info)(snd_pcm_t *pcm, snd_pcm_channel_info_t *info);
143         void (*dump)(snd_pcm_t *pcm, snd_output_t *out);
144         int (*mmap)(snd_pcm_t *pcm);
145         int (*munmap)(snd_pcm_t *pcm);
146 } snd_pcm_ops_t;
147
148 typedef struct {
149         int (*status)(snd_pcm_t *pcm, snd_pcm_status_t *status);
150         int (*prepare)(snd_pcm_t *pcm);
151         int (*reset)(snd_pcm_t *pcm);
152         int (*start)(snd_pcm_t *pcm);
153         int (*drop)(snd_pcm_t *pcm);
154         int (*drain)(snd_pcm_t *pcm);
155         int (*pause)(snd_pcm_t *pcm, int enable);
156         snd_pcm_state_t (*state)(snd_pcm_t *pcm);
157         int (*hwsync)(snd_pcm_t *pcm);
158         int (*delay)(snd_pcm_t *pcm, snd_pcm_sframes_t *delayp);
159         int (*resume)(snd_pcm_t *pcm);
160         int (*link)(snd_pcm_t *pcm1, snd_pcm_t *pcm2);
161         int (*link_slaves)(snd_pcm_t *pcm, snd_pcm_t *master);
162         int (*unlink)(snd_pcm_t *pcm);
163         snd_pcm_sframes_t (*rewindable)(snd_pcm_t *pcm);
164         snd_pcm_sframes_t (*rewind)(snd_pcm_t *pcm, snd_pcm_uframes_t frames);
165         snd_pcm_sframes_t (*forwardable)(snd_pcm_t *pcm);
166         snd_pcm_sframes_t (*forward)(snd_pcm_t *pcm, snd_pcm_uframes_t frames);
167         snd_pcm_sframes_t (*writei)(snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size);
168         snd_pcm_sframes_t (*writen)(snd_pcm_t *pcm, void **bufs, snd_pcm_uframes_t size);
169         snd_pcm_sframes_t (*readi)(snd_pcm_t *pcm, void *buffer, snd_pcm_uframes_t size);
170         snd_pcm_sframes_t (*readn)(snd_pcm_t *pcm, void **bufs, snd_pcm_uframes_t size);
171         snd_pcm_sframes_t (*avail_update)(snd_pcm_t *pcm);
172         snd_pcm_sframes_t (*mmap_commit)(snd_pcm_t *pcm, snd_pcm_uframes_t offset, snd_pcm_uframes_t size);
173         int (*htimestamp)(snd_pcm_t *pcm, snd_pcm_uframes_t *avail, snd_htimestamp_t *tstamp);
174         int (*poll_descriptors_count)(snd_pcm_t *pcm);
175         int (*poll_descriptors)(snd_pcm_t *pcm, struct pollfd *pfds, unsigned int space);
176         int (*poll_revents)(snd_pcm_t *pcm, struct pollfd *pfds, unsigned int nfds, unsigned short *revents);
177 } snd_pcm_fast_ops_t;
178
179 struct _snd_pcm {
180         void *open_func;
181         char *name;
182         snd_pcm_type_t type;
183         snd_pcm_stream_t stream;
184         int mode;
185         long minperiodtime;             /* in us */
186         int poll_fd_count;
187         int poll_fd;
188         unsigned short poll_events;
189         int setup: 1,
190             monotonic: 1,
191             compat: 1;
192         snd_pcm_access_t access;        /* access mode */
193         snd_pcm_format_t format;        /* SND_PCM_FORMAT_* */
194         snd_pcm_subformat_t subformat;  /* subformat */
195         unsigned int channels;          /* channels */
196         unsigned int rate;              /* rate in Hz */
197         snd_pcm_uframes_t period_size;
198         unsigned int period_time;       /* period duration */
199         snd_interval_t periods;
200         snd_pcm_tstamp_t tstamp_mode;   /* timestamp mode */
201         unsigned int period_step;
202         snd_pcm_uframes_t avail_min;    /* min avail frames for wakeup */
203         int period_event;
204         snd_pcm_uframes_t start_threshold;
205         snd_pcm_uframes_t stop_threshold;
206         snd_pcm_uframes_t silence_threshold;    /* Silence filling happens when
207                                            noise is nearest than this */
208         snd_pcm_uframes_t silence_size; /* Silence filling size */
209         snd_pcm_uframes_t boundary;     /* pointers wrap point */
210         unsigned int info;              /* Info for returned setup */
211         unsigned int msbits;            /* used most significant bits */
212         unsigned int rate_num;          /* rate numerator */
213         unsigned int rate_den;          /* rate denominator */
214         unsigned int hw_flags;          /* actual hardware flags */
215         snd_pcm_uframes_t fifo_size;    /* chip FIFO size in frames */
216         snd_pcm_uframes_t buffer_size;
217         snd_interval_t buffer_time;
218         unsigned int sample_bits;
219         unsigned int frame_bits;
220         snd_pcm_rbptr_t appl;
221         snd_pcm_rbptr_t hw;
222         snd_pcm_uframes_t min_align;
223         unsigned int mmap_rw: 1;        /* use always mmapped buffer */
224         unsigned int mmap_shadow: 1;    /* don't call actual mmap,
225                                          * use the mmaped buffer of the slave
226                                          */
227         unsigned int donot_close: 1;    /* don't close this PCM */
228         snd_pcm_channel_info_t *mmap_channels;
229         snd_pcm_channel_area_t *running_areas;
230         snd_pcm_channel_area_t *stopped_areas;
231         const snd_pcm_ops_t *ops;
232         const snd_pcm_fast_ops_t *fast_ops;
233         snd_pcm_t *op_arg;
234         snd_pcm_t *fast_op_arg;
235         void *private_data;
236         struct list_head async_handlers;
237 };
238
239 /* make local functions really local */
240 /* Grrr, these cannot be local - a bad aserver uses them!
241 #define snd_pcm_async \
242         snd1_pcm_async
243 #define snd_pcm_mmap \
244         snd1_pcm_mmap
245 #define snd_pcm_munmap \
246         snd1_pcm_munmap
247 #define snd_pcm_hw_refine \
248         snd1_pcm_hw_refine
249 */
250 #define snd_pcm_new \
251         snd1_pcm_new
252 #define snd_pcm_free \
253         snd1_pcm_free
254 #define snd_pcm_areas_from_buf \
255         snd1_pcm_areas_from_buf
256 #define snd_pcm_areas_from_bufs \
257         snd1_pcm_areas_from_bufs
258 #define snd_pcm_open_named_slave \
259         snd1_pcm_open_named_slave
260 #define snd_pcm_conf_generic_id \
261         snd1_pcm_conf_generic_id
262 #define snd_pcm_hw_open_fd \
263         snd1_pcm_hw_open_fd
264 #define snd_pcm_wait_nocheck \
265         snd1_pcm_wait_nocheck
266 #define snd_pcm_rate_get_default_converter \
267         snd1_pcm_rate_get_default_converter
268 #define snd_pcm_set_hw_ptr \
269         snd1_pcm_set_hw_ptr
270 #define snd_pcm_set_appl_ptr \
271         snd1_pcm_set_appl_ptr
272 #define snd_pcm_link_hw_ptr \
273         snd1_pcm_link_hw_ptr
274 #define snd_pcm_link_appl_ptr \
275         snd1_pcm_link_appl_ptr
276 #define snd_pcm_unlink_hw_ptr \
277         snd1_pcm_unlink_hw_ptr
278 #define snd_pcm_unlink_appl_ptr \
279         snd1_pcm_unlink_appl_ptr
280 #define snd_pcm_mmap_appl_ptr \
281         snd1_pcm_mmap_appl_ptr
282 #define snd_pcm_mmap_appl_backward \
283         snd1_pcm_mmap_appl_backward
284 #define snd_pcm_mmap_appl_forward \
285         snd1_pcm_mmap_appl_forward
286 #define snd_pcm_mmap_hw_backward \
287         snd1_pcm_mmap_hw_backward
288 #define snd_pcm_mmap_hw_forward \
289         snd1_pcm_mmap_hw_forward
290 #define snd_pcm_read_areas \
291         snd1_pcm_read_areas
292 #define snd_pcm_write_areas \
293         snd1_pcm_write_areas
294 #define snd_pcm_read_mmap \
295         snd1_pcm_read_mmap
296 #define snd_pcm_write_mmap \
297         snd1_pcm_write_mmap
298 #define snd_pcm_channel_info_shm \
299         snd1_pcm_channel_info_shm
300 #define snd_pcm_hw_refine_soft \
301         snd1_pcm_hw_refine_soft
302 #define snd_pcm_hw_refine_slave \
303         snd1_pcm_hw_refine_slave
304 #define snd_pcm_hw_params_slave \
305         snd1_pcm_hw_params_slave
306 #define snd_pcm_hw_param_refine_near \
307         snd1_pcm_hw_param_refine_near
308 #define snd_pcm_hw_param_refine_multiple \
309         snd1_pcm_hw_param_refine_multiple
310 #define snd_pcm_hw_param_empty \
311         snd1_pcm_hw_param_empty
312 #define snd_pcm_hw_param_always_eq \
313         snd1_pcm_hw_param_always_eq
314 #define snd_pcm_hw_param_never_eq \
315         snd1_pcm_hw_param_never_eq
316 #define snd_pcm_hw_param_get_mask \
317         snd1_pcm_hw_param_get_mask
318 #define snd_pcm_hw_param_get_interval \
319         snd1_pcm_hw_param_get_interval
320 #define snd_pcm_hw_param_any \
321         snd1_pcm_hw_param_any
322 #define snd_pcm_hw_param_set_integer \
323         snd1_pcm_hw_param_set_integer
324 #define snd_pcm_hw_param_set_first \
325         snd1_pcm_hw_param_set_first
326 #define snd_pcm_hw_param_set_last \
327         snd1_pcm_hw_param_set_last
328 #define snd_pcm_hw_param_set_near \
329         snd1_pcm_hw_param_set_near
330 #define snd_pcm_hw_param_set_min \
331         snd1_pcm_hw_param_set_min
332 #define snd_pcm_hw_param_set_max \
333         snd1_pcm_hw_param_set_max
334 #define snd_pcm_hw_param_set_minmax \
335         snd1_pcm_hw_param_set_minmax
336 #define snd_pcm_hw_param_set \
337         snd1_pcm_hw_param_set
338 #define snd_pcm_hw_param_set_mask \
339         snd1_pcm_hw_param_set_mask
340 #define snd_pcm_hw_param_get \
341         snd1_pcm_hw_param_get
342 #define snd_pcm_hw_param_get_min \
343         snd1_pcm_hw_param_get_min
344 #define snd_pcm_hw_param_get_max \
345         snd1_pcm_hw_param_get_max
346 #define snd_pcm_hw_param_name           \
347         snd1_pcm_hw_param_name
348
349 int snd_pcm_new(snd_pcm_t **pcmp, snd_pcm_type_t type, const char *name,
350                 snd_pcm_stream_t stream, int mode);
351 int snd_pcm_free(snd_pcm_t *pcm);
352
353 void snd_pcm_areas_from_buf(snd_pcm_t *pcm, snd_pcm_channel_area_t *areas, void *buf);
354 void snd_pcm_areas_from_bufs(snd_pcm_t *pcm, snd_pcm_channel_area_t *areas, void **bufs);
355
356 int snd_pcm_async(snd_pcm_t *pcm, int sig, pid_t pid);
357 int snd_pcm_mmap(snd_pcm_t *pcm);
358 int snd_pcm_munmap(snd_pcm_t *pcm);
359 int snd_pcm_mmap_ready(snd_pcm_t *pcm);
360 void snd_pcm_set_hw_ptr(snd_pcm_t *pcm, volatile snd_pcm_uframes_t *hw_ptr, int fd, off_t offset);
361 void snd_pcm_set_appl_ptr(snd_pcm_t *pcm, volatile snd_pcm_uframes_t *appl_ptr, int fd, off_t offset);
362 void snd_pcm_link_hw_ptr(snd_pcm_t *pcm, snd_pcm_t *slave);
363 void snd_pcm_link_appl_ptr(snd_pcm_t *pcm, snd_pcm_t *slave);
364 void snd_pcm_unlink_hw_ptr(snd_pcm_t *pcm, snd_pcm_t *slave);
365 void snd_pcm_unlink_appl_ptr(snd_pcm_t *pcm, snd_pcm_t *slave);
366 snd_pcm_sframes_t snd_pcm_mmap_appl_ptr(snd_pcm_t *pcm, off_t offset);
367 void snd_pcm_mmap_appl_backward(snd_pcm_t *pcm, snd_pcm_uframes_t frames);
368 void snd_pcm_mmap_appl_forward(snd_pcm_t *pcm, snd_pcm_uframes_t frames);
369 void snd_pcm_mmap_hw_backward(snd_pcm_t *pcm, snd_pcm_uframes_t frames);
370 void snd_pcm_mmap_hw_forward(snd_pcm_t *pcm, snd_pcm_uframes_t frames);
371
372 snd_pcm_sframes_t snd_pcm_mmap_writei(snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size);
373 snd_pcm_sframes_t snd_pcm_mmap_readi(snd_pcm_t *pcm, void *buffer, snd_pcm_uframes_t size);
374 snd_pcm_sframes_t snd_pcm_mmap_writen(snd_pcm_t *pcm, void **bufs, snd_pcm_uframes_t size);
375 snd_pcm_sframes_t snd_pcm_mmap_readn(snd_pcm_t *pcm, void **bufs, snd_pcm_uframes_t size);
376
377 typedef snd_pcm_sframes_t (*snd_pcm_xfer_areas_func_t)(snd_pcm_t *pcm, 
378                                                        const snd_pcm_channel_area_t *areas,
379                                                        snd_pcm_uframes_t offset, 
380                                                        snd_pcm_uframes_t size);
381
382 snd_pcm_sframes_t snd_pcm_read_areas(snd_pcm_t *pcm, const snd_pcm_channel_area_t *areas,
383                                      snd_pcm_uframes_t offset, snd_pcm_uframes_t size,
384                                      snd_pcm_xfer_areas_func_t func);
385 snd_pcm_sframes_t snd_pcm_write_areas(snd_pcm_t *pcm, const snd_pcm_channel_area_t *areas,
386                                       snd_pcm_uframes_t offset, snd_pcm_uframes_t size,
387                                       snd_pcm_xfer_areas_func_t func);
388 snd_pcm_sframes_t snd_pcm_read_mmap(snd_pcm_t *pcm, snd_pcm_uframes_t offset,
389                                     snd_pcm_uframes_t size);
390 snd_pcm_sframes_t snd_pcm_write_mmap(snd_pcm_t *pcm, snd_pcm_uframes_t offset,
391                                      snd_pcm_uframes_t size);
392 static inline int snd_pcm_channel_info(snd_pcm_t *pcm, snd_pcm_channel_info_t *info)
393 {
394         return pcm->ops->channel_info(pcm, info);
395 }
396 int snd_pcm_channel_info_shm(snd_pcm_t *pcm, snd_pcm_channel_info_t *info, int shmid);
397 int _snd_pcm_poll_descriptor(snd_pcm_t *pcm);
398 #define _snd_pcm_link_descriptor _snd_pcm_poll_descriptor /* FIXME */
399 #define _snd_pcm_async_descriptor _snd_pcm_poll_descriptor /* FIXME */
400
401 /* handle special error cases */
402 static inline int snd_pcm_check_error(snd_pcm_t *pcm, int err)
403 {
404         if (err == -EINTR) {
405                 switch (snd_pcm_state(pcm)) {
406                 case SND_PCM_STATE_XRUN:
407                         return -EPIPE;
408                 case SND_PCM_STATE_SUSPENDED:
409                         return -ESTRPIPE;
410                 case SND_PCM_STATE_DISCONNECTED:
411                         return -ENODEV;
412                 default:
413                         break;
414                 }
415         }
416         return err;
417 }
418
419 static inline snd_pcm_uframes_t snd_pcm_mmap_playback_avail(snd_pcm_t *pcm)
420 {
421         snd_pcm_sframes_t avail;
422         avail = *pcm->hw.ptr + pcm->buffer_size - *pcm->appl.ptr;
423         if (avail < 0)
424                 avail += pcm->boundary;
425         else if ((snd_pcm_uframes_t) avail >= pcm->boundary)
426                 avail -= pcm->boundary;
427         return avail;
428 }
429
430 static inline snd_pcm_uframes_t snd_pcm_mmap_capture_avail(snd_pcm_t *pcm)
431 {
432         snd_pcm_sframes_t avail;
433         avail = *pcm->hw.ptr - *pcm->appl.ptr;
434         if (avail < 0)
435                 avail += pcm->boundary;
436         return avail;
437 }
438
439 static inline snd_pcm_uframes_t snd_pcm_mmap_avail(snd_pcm_t *pcm)
440 {
441         if (pcm->stream == SND_PCM_STREAM_PLAYBACK)
442                 return snd_pcm_mmap_playback_avail(pcm);
443         else
444                 return snd_pcm_mmap_capture_avail(pcm);
445 }
446
447 static inline snd_pcm_sframes_t snd_pcm_mmap_playback_hw_avail(snd_pcm_t *pcm)
448 {
449         return pcm->buffer_size - snd_pcm_mmap_playback_avail(pcm);
450 }
451
452 static inline snd_pcm_sframes_t snd_pcm_mmap_capture_hw_avail(snd_pcm_t *pcm)
453 {
454         return pcm->buffer_size - snd_pcm_mmap_capture_avail(pcm);
455 }
456
457 static inline snd_pcm_sframes_t snd_pcm_mmap_hw_avail(snd_pcm_t *pcm)
458 {
459         snd_pcm_sframes_t avail;
460         avail = *pcm->hw.ptr - *pcm->appl.ptr;
461         if (pcm->stream == SND_PCM_STREAM_PLAYBACK)
462                 avail += pcm->buffer_size;
463         if (avail < 0)
464                 avail += pcm->boundary;
465         return pcm->buffer_size - avail;
466 }
467
468 static inline const snd_pcm_channel_area_t *snd_pcm_mmap_areas(snd_pcm_t *pcm)
469 {
470         if (pcm->stopped_areas &&
471             snd_pcm_state(pcm) != SND_PCM_STATE_RUNNING) 
472                 return pcm->stopped_areas;
473         return pcm->running_areas;
474 }
475
476 static inline snd_pcm_uframes_t snd_pcm_mmap_offset(snd_pcm_t *pcm)
477 {
478         assert(pcm);
479         return *pcm->appl.ptr % pcm->buffer_size;
480 }
481
482 static inline snd_pcm_uframes_t snd_pcm_mmap_hw_offset(snd_pcm_t *pcm)
483 {
484         assert(pcm);
485         return *pcm->hw.ptr % pcm->buffer_size;
486 }
487
488 static inline snd_pcm_uframes_t snd_pcm_mmap_playback_delay(snd_pcm_t *pcm)
489 {
490         return snd_pcm_mmap_playback_hw_avail(pcm);
491 }
492
493 static inline snd_pcm_uframes_t snd_pcm_mmap_capture_delay(snd_pcm_t *pcm)
494 {
495         return snd_pcm_mmap_capture_hw_avail(pcm);
496 }
497
498 static inline snd_pcm_sframes_t snd_pcm_mmap_delay(snd_pcm_t *pcm)
499 {
500         if (pcm->stream == SND_PCM_STREAM_PLAYBACK)
501                 return snd_pcm_mmap_playback_delay(pcm);
502         else
503                 return snd_pcm_mmap_capture_delay(pcm);
504 }
505
506 static inline void *snd_pcm_channel_area_addr(const snd_pcm_channel_area_t *area, snd_pcm_uframes_t offset)
507 {
508         unsigned int bitofs = area->first + area->step * offset;
509         assert(bitofs % 8 == 0);
510         return (char *) area->addr + bitofs / 8;
511 }
512
513 static inline unsigned int snd_pcm_channel_area_step(const snd_pcm_channel_area_t *area)
514 {
515         assert(area->step % 8 == 0);
516         return area->step / 8;
517 }
518
519 static inline snd_pcm_sframes_t _snd_pcm_writei(snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size)
520 {
521         return pcm->fast_ops->writei(pcm->fast_op_arg, buffer, size);
522 }
523
524 static inline snd_pcm_sframes_t _snd_pcm_writen(snd_pcm_t *pcm, void **bufs, snd_pcm_uframes_t size)
525 {
526         return pcm->fast_ops->writen(pcm->fast_op_arg, bufs, size);
527 }
528
529 static inline snd_pcm_sframes_t _snd_pcm_readi(snd_pcm_t *pcm, void *buffer, snd_pcm_uframes_t size)
530 {
531         return pcm->fast_ops->readi(pcm->fast_op_arg, buffer, size);
532 }
533
534 static inline snd_pcm_sframes_t _snd_pcm_readn(snd_pcm_t *pcm, void **bufs, snd_pcm_uframes_t size)
535 {
536         return pcm->fast_ops->readn(pcm->fast_op_arg, bufs, size);
537 }
538
539 static inline int muldiv(int a, int b, int c, int *r)
540 {
541         int64_t n = (int64_t)a * b;
542         int64_t v = n / c;
543         if (v > INT_MAX) {
544                 *r = 0;
545                 return INT_MAX;
546         }
547         if (v < INT_MIN) {
548                 *r = 0;
549                 return INT_MIN;
550         }
551         *r = n % c;
552         return v;
553 }
554
555 static inline int muldiv_down(int a, int b, int c)
556 {
557         int64_t v = (int64_t)a * b / c;
558         if (v > INT_MAX) {
559                 return INT_MAX;
560         }
561         if (v < INT_MIN) {
562                 return INT_MIN;
563         }
564         return v;
565 }
566
567 static inline int muldiv_near(int a, int b, int c)
568 {
569         int r;
570         int n = muldiv(a, b, c, &r);
571         if (r >= (c + 1) / 2)
572                 n++;
573         return n;
574 }
575
576 int snd_pcm_hw_refine(snd_pcm_t *pcm, snd_pcm_hw_params_t *params);
577 int _snd_pcm_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t *params);
578 int snd_pcm_hw_refine_soft(snd_pcm_t *pcm, snd_pcm_hw_params_t *params);
579 int snd_pcm_hw_refine_slave(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
580                             int (*cprepare)(snd_pcm_t *pcm,
581                                             snd_pcm_hw_params_t *params),
582                             int (*cchange)(snd_pcm_t *pcm,
583                                            snd_pcm_hw_params_t *params,
584                                            snd_pcm_hw_params_t *sparams),
585                             int (*sprepare)(snd_pcm_t *pcm,
586                                             snd_pcm_hw_params_t *params),
587                             int (*schange)(snd_pcm_t *pcm,
588                                            snd_pcm_hw_params_t *params,
589                                            snd_pcm_hw_params_t *sparams),
590                             int (*srefine)(snd_pcm_t *pcm,
591                                            snd_pcm_hw_params_t *sparams));
592 int snd_pcm_hw_params_slave(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
593                             int (*cchange)(snd_pcm_t *pcm,
594                                            snd_pcm_hw_params_t *params,
595                                            snd_pcm_hw_params_t *sparams),
596                             int (*sprepare)(snd_pcm_t *pcm,
597                                             snd_pcm_hw_params_t *params),
598                             int (*schange)(snd_pcm_t *pcm,
599                                            snd_pcm_hw_params_t *params,
600                                            snd_pcm_hw_params_t *sparams),
601                             int (*sparams)(snd_pcm_t *pcm,
602                                            snd_pcm_hw_params_t *sparams));
603
604
605 void _snd_pcm_hw_params_any(snd_pcm_hw_params_t *params);
606 void _snd_pcm_hw_param_set_empty(snd_pcm_hw_params_t *params,
607                                  snd_pcm_hw_param_t var);
608 int _snd_pcm_hw_param_set_interval(snd_pcm_hw_params_t *params,
609                                    snd_pcm_hw_param_t var,
610                                    const snd_interval_t *val);
611 int _snd_pcm_hw_param_set_mask(snd_pcm_hw_params_t *params,
612                            snd_pcm_hw_param_t var, const snd_mask_t *mask);
613 int _snd_pcm_hw_param_first(snd_pcm_hw_params_t *params,
614                             snd_pcm_hw_param_t var);
615 int _snd_pcm_hw_param_last(snd_pcm_hw_params_t *params,
616                            snd_pcm_hw_param_t var);
617 int _snd_pcm_hw_param_set(snd_pcm_hw_params_t *params,
618                           snd_pcm_hw_param_t var, unsigned int val, int dir);
619 static inline int _snd_pcm_hw_params_set_format(snd_pcm_hw_params_t *params,
620                                                 snd_pcm_format_t val)
621 {
622         return _snd_pcm_hw_param_set(params, SND_PCM_HW_PARAM_FORMAT,
623                                      (unsigned long) val, 0);
624 }
625
626 static inline int _snd_pcm_hw_params_set_subformat(snd_pcm_hw_params_t *params,
627                                      snd_pcm_subformat_t val)
628 {
629         return _snd_pcm_hw_param_set(params, SND_PCM_HW_PARAM_SUBFORMAT,
630                                      (unsigned long) val, 0);
631 }
632
633 int _snd_pcm_hw_param_set_min(snd_pcm_hw_params_t *params,
634                               snd_pcm_hw_param_t var, unsigned int val, int dir);
635 int _snd_pcm_hw_param_set_max(snd_pcm_hw_params_t *params,
636                               snd_pcm_hw_param_t var, unsigned int val, int dir);
637 int _snd_pcm_hw_param_set_minmax(snd_pcm_hw_params_t *params,
638                                  snd_pcm_hw_param_t var,
639                                  unsigned int min, int mindir,
640                                  unsigned int max, int maxdir);
641 int _snd_pcm_hw_param_refine(snd_pcm_hw_params_t *params,
642                              snd_pcm_hw_param_t var,
643                              const snd_pcm_hw_params_t *src);
644 int _snd_pcm_hw_params_refine(snd_pcm_hw_params_t *params,
645                               unsigned int vars,
646                               const snd_pcm_hw_params_t *src);
647 int snd_pcm_hw_param_refine_near(snd_pcm_t *pcm,
648                                  snd_pcm_hw_params_t *params,
649                                  snd_pcm_hw_param_t var,
650                                  const snd_pcm_hw_params_t *src);
651 int snd_pcm_hw_param_refine_multiple(snd_pcm_t *pcm,
652                                      snd_pcm_hw_params_t *params,
653                                      snd_pcm_hw_param_t var,
654                                      const snd_pcm_hw_params_t *src);
655 int snd_pcm_hw_param_empty(const snd_pcm_hw_params_t *params,
656                            snd_pcm_hw_param_t var);
657 int snd_pcm_hw_param_always_eq(const snd_pcm_hw_params_t *params,
658                                snd_pcm_hw_param_t var,
659                                const snd_pcm_hw_params_t *params1);
660 int snd_pcm_hw_param_never_eq(const snd_pcm_hw_params_t *params,
661                               snd_pcm_hw_param_t var,
662                               const snd_pcm_hw_params_t *params1);
663 const snd_mask_t *snd_pcm_hw_param_get_mask(const snd_pcm_hw_params_t *params,
664                                               snd_pcm_hw_param_t var);
665 const snd_interval_t *snd_pcm_hw_param_get_interval(const snd_pcm_hw_params_t *params,
666                                                       snd_pcm_hw_param_t var);
667
668 int snd_pcm_hw_param_any(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
669                          snd_pcm_hw_param_t var);
670 int snd_pcm_hw_param_set_integer(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
671                                  snd_set_mode_t mode,
672                                  snd_pcm_hw_param_t var);
673 int snd_pcm_hw_param_set_first(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
674                                snd_pcm_hw_param_t var, unsigned int *rval, int *dir);
675 int snd_pcm_hw_param_set_last(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
676                               snd_pcm_hw_param_t var, unsigned int *rval, int *dir);
677 int snd_pcm_hw_param_set_near(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
678                               snd_pcm_hw_param_t var, unsigned int *val, int *dir);
679 int snd_pcm_hw_param_set_min(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
680                              snd_set_mode_t mode,
681                              snd_pcm_hw_param_t var,
682                              unsigned int *val, int *dir);
683 int snd_pcm_hw_param_set_max(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
684                              snd_set_mode_t mode,
685                              snd_pcm_hw_param_t var, unsigned int *val, int *dir);
686 int snd_pcm_hw_param_set_minmax(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
687                                 snd_set_mode_t mode,
688                                 snd_pcm_hw_param_t var,
689                                 unsigned int *min, int *mindir,
690                                 unsigned int *max, int *maxdir);
691 int snd_pcm_hw_param_set(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
692                          snd_set_mode_t mode,
693                          snd_pcm_hw_param_t var, unsigned int val, int dir);
694 int snd_pcm_hw_param_set_mask(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
695                               snd_set_mode_t mode,
696                               snd_pcm_hw_param_t var, const snd_mask_t *mask);
697 int snd_pcm_hw_param_get(const snd_pcm_hw_params_t *params, snd_pcm_hw_param_t var,
698                          unsigned int *val, int *dir);
699 int snd_pcm_hw_param_get_min(const snd_pcm_hw_params_t *params,
700                              snd_pcm_hw_param_t var,
701                              unsigned int *val, int *dir);
702 int snd_pcm_hw_param_get_max(const snd_pcm_hw_params_t *params,
703                              snd_pcm_hw_param_t var,
704                              unsigned int *val, int *dir);
705
706 #ifdef INTERNAL
707 snd_pcm_sframes_t INTERNAL(snd_pcm_forward)(snd_pcm_t *pcm, snd_pcm_uframes_t frames);
708
709 int INTERNAL(snd_pcm_hw_params_get_access)(const snd_pcm_hw_params_t *params, snd_pcm_access_t *access);
710 int snd_pcm_hw_params_test_access(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_t access);
711 int snd_pcm_hw_params_set_access(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_t access);
712 int INTERNAL(snd_pcm_hw_params_set_access_first)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_t *access);
713 int INTERNAL(snd_pcm_hw_params_set_access_last)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_t *access);
714 int snd_pcm_hw_params_set_access_mask(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_mask_t *mask);
715 int snd_pcm_hw_params_get_access_mask(snd_pcm_hw_params_t *params, snd_pcm_access_mask_t *mask);
716
717 int INTERNAL(snd_pcm_hw_params_get_format)(const snd_pcm_hw_params_t *params, snd_pcm_format_t *val);
718 int snd_pcm_hw_params_test_format(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_t val);
719 int snd_pcm_hw_params_set_format(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_t val);
720 int INTERNAL(snd_pcm_hw_params_set_format_first)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_t *format);
721 int INTERNAL(snd_pcm_hw_params_set_format_last)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_t *format);
722 int snd_pcm_hw_params_set_format_mask(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_mask_t *mask);
723 void snd_pcm_hw_params_get_format_mask(snd_pcm_hw_params_t *params, snd_pcm_format_mask_t *mask);
724
725 int INTERNAL(snd_pcm_hw_params_get_subformat)(const snd_pcm_hw_params_t *params, snd_pcm_subformat_t *subformat);
726 int snd_pcm_hw_params_test_subformat(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_subformat_t subformat);
727 int snd_pcm_hw_params_set_subformat(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_subformat_t subformat);
728 int INTERNAL(snd_pcm_hw_params_set_subformat_first)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_subformat_t *subformat);
729 int INTERNAL(snd_pcm_hw_params_set_subformat_last)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_subformat_t *subformat);
730 int snd_pcm_hw_params_set_subformat_mask(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_subformat_mask_t *mask);
731 void snd_pcm_hw_params_get_subformat_mask(snd_pcm_hw_params_t *params, snd_pcm_subformat_mask_t *mask);
732
733 int INTERNAL(snd_pcm_hw_params_get_channels)(const snd_pcm_hw_params_t *params, unsigned int *val);
734 int INTERNAL(snd_pcm_hw_params_get_channels_min)(const snd_pcm_hw_params_t *params, unsigned int *val);
735 int INTERNAL(snd_pcm_hw_params_get_channels_max)(const snd_pcm_hw_params_t *params, unsigned int *val);
736 int snd_pcm_hw_params_test_channels(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val);
737 int snd_pcm_hw_params_set_channels(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val);
738 int snd_pcm_hw_params_set_channels_min(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val);
739 int snd_pcm_hw_params_set_channels_max(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val);
740 int snd_pcm_hw_params_set_channels_minmax(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *min, unsigned int *max);
741 int INTERNAL(snd_pcm_hw_params_set_channels_near)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val);
742 int INTERNAL(snd_pcm_hw_params_set_channels_first)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val);
743 int INTERNAL(snd_pcm_hw_params_set_channels_last)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val);
744
745 int INTERNAL(snd_pcm_hw_params_get_rate)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
746 int INTERNAL(snd_pcm_hw_params_get_rate_min)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
747 int INTERNAL(snd_pcm_hw_params_get_rate_max)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
748 int snd_pcm_hw_params_test_rate(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir);
749 int snd_pcm_hw_params_set_rate(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir);
750 int snd_pcm_hw_params_set_rate_min(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
751 int snd_pcm_hw_params_set_rate_max(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
752 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);
753 int INTERNAL(snd_pcm_hw_params_set_rate_near)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
754 int INTERNAL(snd_pcm_hw_params_set_rate_first)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
755 int INTERNAL(snd_pcm_hw_params_set_rate_last)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
756
757 int INTERNAL(snd_pcm_hw_params_get_period_time)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
758 int INTERNAL(snd_pcm_hw_params_get_period_time_min)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
759 int INTERNAL(snd_pcm_hw_params_get_period_time_max)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
760 int snd_pcm_hw_params_test_period_time(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir);
761 int snd_pcm_hw_params_set_period_time(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir);
762 int snd_pcm_hw_params_set_period_time_min(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
763 int snd_pcm_hw_params_set_period_time_max(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
764 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);
765 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);
766 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);
767 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);
768
769 int INTERNAL(snd_pcm_hw_params_get_period_size)(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *frames, int *dir);
770 int INTERNAL(snd_pcm_hw_params_get_period_size_min)(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *frames, int *dir);
771 int INTERNAL(snd_pcm_hw_params_get_period_size_max)(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *frames, int *dir);
772 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);
773 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);
774 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);
775 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);
776 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);
777 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);
778 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);
779 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);
780 int snd_pcm_hw_params_set_period_size_integer(snd_pcm_t *pcm, snd_pcm_hw_params_t *params);
781
782 int INTERNAL(snd_pcm_hw_params_get_periods)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
783 int INTERNAL(snd_pcm_hw_params_get_periods_min)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
784 int INTERNAL(snd_pcm_hw_params_get_periods_max)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
785 int snd_pcm_hw_params_test_periods(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir);
786 int snd_pcm_hw_params_set_periods(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir);
787 int snd_pcm_hw_params_set_periods_min(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
788 int snd_pcm_hw_params_set_periods_max(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
789 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);
790 int INTERNAL(snd_pcm_hw_params_set_periods_near)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
791 int INTERNAL(snd_pcm_hw_params_set_periods_first)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
792 int INTERNAL(snd_pcm_hw_params_set_periods_last)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
793 int snd_pcm_hw_params_set_periods_integer(snd_pcm_t *pcm, snd_pcm_hw_params_t *params);
794
795 int INTERNAL(snd_pcm_hw_params_get_buffer_time)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
796 int INTERNAL(snd_pcm_hw_params_get_buffer_time_min)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
797 int INTERNAL(snd_pcm_hw_params_get_buffer_time_max)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
798 int snd_pcm_hw_params_test_buffer_time(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir);
799 int snd_pcm_hw_params_set_buffer_time(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir);
800 int snd_pcm_hw_params_set_buffer_time_min(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
801 int snd_pcm_hw_params_set_buffer_time_max(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
802 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);
803 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);
804 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);
805 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);
806
807 int INTERNAL(snd_pcm_hw_params_get_buffer_size)(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val);
808 int INTERNAL(snd_pcm_hw_params_get_buffer_size_min)(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val);
809 int INTERNAL(snd_pcm_hw_params_get_buffer_size_max)(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val);
810 int snd_pcm_hw_params_test_buffer_size(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t val);
811 int snd_pcm_hw_params_set_buffer_size(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t val);
812 int snd_pcm_hw_params_set_buffer_size_min(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val);
813 int snd_pcm_hw_params_set_buffer_size_max(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val);
814 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);
815 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);
816 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);
817 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);
818
819 int snd_pcm_sw_params_set_tstamp_mode(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_tstamp_t val);
820 int INTERNAL(snd_pcm_sw_params_get_tstamp_mode)(const snd_pcm_sw_params_t *params, snd_pcm_tstamp_t *val);
821 int snd_pcm_sw_params_set_avail_min(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val);
822 int INTERNAL(snd_pcm_sw_params_get_avail_min)(const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val);
823 int snd_pcm_sw_params_set_start_threshold(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val);
824 int INTERNAL(snd_pcm_sw_params_get_start_threshold)(const snd_pcm_sw_params_t *paramsm, snd_pcm_uframes_t *val);
825 int snd_pcm_sw_params_set_stop_threshold(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val);
826 int INTERNAL(snd_pcm_sw_params_get_stop_threshold)(const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val);
827 int snd_pcm_sw_params_set_silence_threshold(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val);
828 int INTERNAL(snd_pcm_sw_params_get_silence_threshold)(const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val);
829 int snd_pcm_sw_params_set_silence_size(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val);
830 int INTERNAL(snd_pcm_sw_params_get_silence_size)(const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val);
831 #endif /* INTERNAL */
832
833 const char *snd_pcm_hw_param_name(snd_pcm_hw_param_t param);
834 void snd_pcm_hw_param_dump(const snd_pcm_hw_params_t *params,
835                            snd_pcm_hw_param_t var, snd_output_t *out);
836 #if 0
837 int snd_pcm_hw_strategy_simple_near(snd_pcm_hw_strategy_t *strategy, int order,
838                                     snd_pcm_hw_param_t var,
839                                     unsigned int best,
840                                     unsigned int mul);
841 int snd_pcm_hw_strategy_simple_choices(snd_pcm_hw_strategy_t *strategy, int order,
842                                        snd_pcm_hw_param_t var,
843                                        unsigned int count,
844                                        snd_pcm_hw_strategy_simple_choices_list_t *choices);
845 #endif
846
847 #define SCONF_MANDATORY 1
848 #define SCONF_UNCHANGED 2
849
850 int snd_pcm_slave_conf(snd_config_t *root, snd_config_t *conf,
851                        snd_config_t **pcm_conf, unsigned int count, ...);
852
853 #define SND_PCM_APPEND  (1<<8)
854
855 int snd_pcm_open_named_slave(snd_pcm_t **pcmp, const char *name,
856                              snd_config_t *root,
857                              snd_config_t *conf, snd_pcm_stream_t stream,
858                              int mode, snd_config_t *parent_conf);
859 static inline int
860 snd_pcm_open_slave(snd_pcm_t **pcmp, snd_config_t *root,
861                    snd_config_t *conf, snd_pcm_stream_t stream,
862                    int mode, snd_config_t *parent_conf)
863 {
864         return snd_pcm_open_named_slave(pcmp, NULL, root, conf, stream,
865                                         mode, parent_conf);
866 }
867 int snd_pcm_conf_generic_id(const char *id);
868
869 int snd_pcm_hw_open_fd(snd_pcm_t **pcmp, const char *name, int fd, int mmap_emulation, int sync_ptr_ioctl);
870 int __snd_pcm_mmap_emul_open(snd_pcm_t **pcmp, const char *name,
871                              snd_pcm_t *slave, int close_slave);
872
873 int snd_pcm_wait_nocheck(snd_pcm_t *pcm, int timeout);
874
875 const snd_config_t *snd_pcm_rate_get_default_converter(snd_config_t *root);
876
877 #define SND_PCM_HW_PARBIT_ACCESS        (1U << SND_PCM_HW_PARAM_ACCESS)
878 #define SND_PCM_HW_PARBIT_FORMAT        (1U << SND_PCM_HW_PARAM_FORMAT)
879 #define SND_PCM_HW_PARBIT_SUBFORMAT     (1U << SND_PCM_HW_PARAM_SUBFORMAT)
880 #define SND_PCM_HW_PARBIT_CHANNELS      (1U << SND_PCM_HW_PARAM_CHANNELS)
881 #define SND_PCM_HW_PARBIT_RATE          (1U << SND_PCM_HW_PARAM_RATE)
882 #define SND_PCM_HW_PARBIT_PERIOD_TIME   (1U << SND_PCM_HW_PARAM_PERIOD_TIME)
883 #define SND_PCM_HW_PARBIT_PERIOD_SIZE   (1U << SND_PCM_HW_PARAM_PERIOD_SIZE)
884 #define SND_PCM_HW_PARBIT_PERIODS       (1U << SND_PCM_HW_PARAM_PERIODS)
885 #define SND_PCM_HW_PARBIT_BUFFER_TIME   (1U << SND_PCM_HW_PARAM_BUFFER_TIME)
886 #define SND_PCM_HW_PARBIT_BUFFER_SIZE   (1U << SND_PCM_HW_PARAM_BUFFER_SIZE)
887 #define SND_PCM_HW_PARBIT_SAMPLE_BITS   (1U << SND_PCM_HW_PARAM_SAMPLE_BITS)
888 #define SND_PCM_HW_PARBIT_FRAME_BITS    (1U << SND_PCM_HW_PARAM_FRAME_BITS)
889 #define SND_PCM_HW_PARBIT_PERIOD_BYTES  (1U << SND_PCM_HW_PARAM_PERIOD_BYTES)
890 #define SND_PCM_HW_PARBIT_BUFFER_BYTES  (1U << SND_PCM_HW_PARAM_BUFFER_BYTES)
891 #define SND_PCM_HW_PARBIT_TICK_TIME     (1U << SND_PCM_HW_PARAM_TICK_TIME)
892
893
894 #define SND_PCM_ACCBIT_MMAP { ((1U << SND_PCM_ACCESS_MMAP_INTERLEAVED) | \
895                              (1U << SND_PCM_ACCESS_MMAP_NONINTERLEAVED) | \
896                              (1U << SND_PCM_ACCESS_MMAP_COMPLEX)) }
897 #define SND_PCM_ACCBIT_MMAPI { (1U << SND_PCM_ACCESS_MMAP_INTERLEAVED) }
898 #define SND_PCM_ACCBIT_MMAPN { (1U << SND_PCM_ACCESS_MMAP_NONINTERLEAVED) }
899 #define SND_PCM_ACCBIT_MMAPC { (1U << SND_PCM_ACCESS_MMAP_COMPLEX) }
900
901 #define SND_PCM_ACCBIT_SHM { ((1U << SND_PCM_ACCESS_MMAP_INTERLEAVED) | \
902                             (1U << SND_PCM_ACCESS_RW_INTERLEAVED) | \
903                             (1U << SND_PCM_ACCESS_MMAP_NONINTERLEAVED) | \
904                             (1U << SND_PCM_ACCESS_RW_NONINTERLEAVED)) }
905 #define SND_PCM_ACCBIT_SHMI { ((1U << SND_PCM_ACCESS_MMAP_INTERLEAVED) | \
906                              (1U << SND_PCM_ACCESS_RW_INTERLEAVED)) }
907 #define SND_PCM_ACCBIT_SHMN { ((1U << SND_PCM_ACCESS_MMAP_NONINTERLEAVED) | \
908                              (1U << SND_PCM_ACCESS_RW_NONINTERLEAVED)) }
909
910 #define SND_PCM_FMTBIT_LINEAR \
911         { ((1U << SND_PCM_FORMAT_S8) | \
912          (1U << SND_PCM_FORMAT_U8) | \
913          (1U << SND_PCM_FORMAT_S16_LE) | \
914          (1U << SND_PCM_FORMAT_S16_BE) | \
915          (1U << SND_PCM_FORMAT_U16_LE) | \
916          (1U << SND_PCM_FORMAT_U16_BE) | \
917          (1U << SND_PCM_FORMAT_S24_LE) | \
918          (1U << SND_PCM_FORMAT_S24_BE) | \
919          (1U << SND_PCM_FORMAT_U24_LE) | \
920          (1U << SND_PCM_FORMAT_U24_BE) | \
921          (1U << SND_PCM_FORMAT_S32_LE) | \
922          (1U << SND_PCM_FORMAT_S32_BE) | \
923          (1U << SND_PCM_FORMAT_U32_LE) | \
924          (1U << SND_PCM_FORMAT_U32_BE)), \
925         ((1U << (SND_PCM_FORMAT_S24_3LE - 32)) | \
926          (1U << (SND_PCM_FORMAT_U24_3LE - 32)) | \
927          (1U << (SND_PCM_FORMAT_S24_3BE - 32)) | \
928          (1U << (SND_PCM_FORMAT_U24_3BE - 32)) | \
929          (1U << (SND_PCM_FORMAT_S20_3LE - 32)) | \
930          (1U << (SND_PCM_FORMAT_U20_3LE - 32)) | \
931          (1U << (SND_PCM_FORMAT_S20_3BE - 32)) | \
932          (1U << (SND_PCM_FORMAT_U20_3BE - 32)) | \
933          (1U << (SND_PCM_FORMAT_S18_3LE - 32)) | \
934          (1U << (SND_PCM_FORMAT_U18_3LE - 32)) | \
935          (1U << (SND_PCM_FORMAT_S18_3BE - 32)) | \
936          (1U << (SND_PCM_FORMAT_U18_3BE - 32))) }
937         
938
939 #define SND_PCM_FMTBIT_FLOAT \
940         { ((1U << SND_PCM_FORMAT_FLOAT_LE) | \
941          (1U << SND_PCM_FORMAT_FLOAT_BE) | \
942          (1U << SND_PCM_FORMAT_FLOAT64_LE) | \
943          (1U << SND_PCM_FORMAT_FLOAT64_BE)) }
944
945
946 typedef union snd_tmp_float {
947         float f;
948         int32_t i;
949 } snd_tmp_float_t;
950
951 typedef union snd_tmp_double {
952         double d;
953         int64_t l;
954 } snd_tmp_double_t;
955
956 /* get the current timestamp */
957 static inline void gettimestamp(snd_htimestamp_t *tstamp, int monotonic)
958 {
959 #if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
960         if (monotonic) {
961                 clock_gettime(CLOCK_MONOTONIC, tstamp);
962         } else {
963 #endif
964                 struct timeval tv;
965
966                 gettimeofday(&tv, 0);
967                 tstamp->tv_sec = tv.tv_sec;
968                 tstamp->tv_nsec = tv.tv_usec * 1000L;
969 #if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
970         }
971 #endif
972 }