blob: 6d85839107f92050fd55ec7672a0bf78569b12d9 [file] [log] [blame]
Simon Wilsonedff7082011-06-06 15:33:34 -07001/* pcm.c
2**
3** Copyright 2011, The Android Open Source Project
4**
5** Redistribution and use in source and binary forms, with or without
6** modification, are permitted provided that the following conditions are met:
7** * Redistributions of source code must retain the above copyright
8** notice, this list of conditions and the following disclaimer.
9** * Redistributions in binary form must reproduce the above copyright
10** notice, this list of conditions and the following disclaimer in the
11** documentation and/or other materials provided with the distribution.
12** * Neither the name of The Android Open Source Project nor the names of
13** its contributors may be used to endorse or promote products derived
14** from this software without specific prior written permission.
15**
16** THIS SOFTWARE IS PROVIDED BY The Android Open Source Project ``AS IS'' AND
17** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19** ARE DISCLAIMED. IN NO EVENT SHALL The Android Open Source Project BE LIABLE
20** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26** DAMAGE.
27*/
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <fcntl.h>
32#include <stdarg.h>
33#include <string.h>
34#include <errno.h>
35#include <unistd.h>
Simon Wilsone9942c82011-10-13 13:57:25 -070036#include <poll.h>
Simon Wilsonedff7082011-06-06 15:33:34 -070037
38#include <sys/ioctl.h>
39#include <sys/mman.h>
40#include <sys/time.h>
Simon Wilsone9942c82011-10-13 13:57:25 -070041#include <limits.h>
Simon Wilsonedff7082011-06-06 15:33:34 -070042
43#include <linux/ioctl.h>
44#define __force
45#define __bitwise
46#define __user
47#include <sound/asound.h>
48
49#include <tinyalsa/asoundlib.h>
50
51#define PARAM_MAX SNDRV_PCM_HW_PARAM_LAST_INTERVAL
Simon Wilsone9942c82011-10-13 13:57:25 -070052#define SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP (1<<2)
Simon Wilsonedff7082011-06-06 15:33:34 -070053
Andy Hung70530a62014-03-26 18:29:07 -070054/* Logs information into a string; follows snprintf() in that
55 * offset may be greater than size, and though no characters are copied
56 * into string, characters are still counted into offset. */
57#define STRLOG(string, offset, size, ...) \
58 do { int temp, clipoffset = offset > size ? size : offset; \
59 temp = snprintf(string + clipoffset, size - clipoffset, __VA_ARGS__); \
60 if (temp > 0) offset += temp; } while (0)
61
62#ifndef ARRAY_SIZE
63#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
64#endif
65
66/* refer to SNDRV_PCM_ACCESS_##index in sound/asound.h. */
67static const char * const access_lookup[] = {
68 "MMAP_INTERLEAVED",
69 "MMAP_NONINTERLEAVED",
70 "MMAP_COMPLEX",
71 "RW_INTERLEAVED",
72 "RW_NONINTERLEAVED",
73};
74
75/* refer to SNDRV_PCM_FORMAT_##index in sound/asound.h. */
76static const char * const format_lookup[] = {
77 /*[0] =*/ "S8",
78 "U8",
79 "S16_LE",
80 "S16_BE",
81 "U16_LE",
82 "U16_BE",
83 "S24_LE",
84 "S24_BE",
85 "U24_LE",
86 "U24_BE",
87 "S32_LE",
88 "S32_BE",
89 "U32_LE",
90 "U32_BE",
91 "FLOAT_LE",
92 "FLOAT_BE",
93 "FLOAT64_LE",
94 "FLOAT64_BE",
95 "IEC958_SUBFRAME_LE",
96 "IEC958_SUBFRAME_BE",
97 "MU_LAW",
98 "A_LAW",
99 "IMA_ADPCM",
100 "MPEG",
101 /*[24] =*/ "GSM",
102 /* gap */
103 [31] = "SPECIAL",
104 "S24_3LE",
105 "S24_3BE",
106 "U24_3LE",
107 "U24_3BE",
108 "S20_3LE",
109 "S20_3BE",
110 "U20_3LE",
111 "U20_3BE",
112 "S18_3LE",
113 "S18_3BE",
114 "U18_3LE",
115 /*[43] =*/ "U18_3BE",
116#if 0
117 /* recent additions, may not be present on local asound.h */
118 "G723_24",
119 "G723_24_1B",
120 "G723_40",
121 "G723_40_1B",
122 "DSD_U8",
123 "DSD_U16_LE",
124#endif
125};
126
127/* refer to SNDRV_PCM_SUBFORMAT_##index in sound/asound.h. */
128static const char * const subformat_lookup[] = {
129 "STD",
130};
131
Simon Wilsonedff7082011-06-06 15:33:34 -0700132static inline int param_is_mask(int p)
133{
134 return (p >= SNDRV_PCM_HW_PARAM_FIRST_MASK) &&
135 (p <= SNDRV_PCM_HW_PARAM_LAST_MASK);
136}
137
138static inline int param_is_interval(int p)
139{
140 return (p >= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL) &&
141 (p <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL);
142}
143
144static inline struct snd_interval *param_to_interval(struct snd_pcm_hw_params *p, int n)
145{
146 return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]);
147}
148
149static inline struct snd_mask *param_to_mask(struct snd_pcm_hw_params *p, int n)
150{
151 return &(p->masks[n - SNDRV_PCM_HW_PARAM_FIRST_MASK]);
152}
153
154static void param_set_mask(struct snd_pcm_hw_params *p, int n, unsigned int bit)
155{
156 if (bit >= SNDRV_MASK_MAX)
157 return;
158 if (param_is_mask(n)) {
159 struct snd_mask *m = param_to_mask(p, n);
160 m->bits[0] = 0;
161 m->bits[1] = 0;
162 m->bits[bit >> 5] |= (1 << (bit & 31));
163 }
164}
165
166static void param_set_min(struct snd_pcm_hw_params *p, int n, unsigned int val)
167{
168 if (param_is_interval(n)) {
169 struct snd_interval *i = param_to_interval(p, n);
170 i->min = val;
171 }
172}
173
Simon Wilson42fc2d32012-12-03 11:18:57 -0800174static unsigned int param_get_min(struct snd_pcm_hw_params *p, int n)
175{
176 if (param_is_interval(n)) {
177 struct snd_interval *i = param_to_interval(p, n);
178 return i->min;
179 }
180 return 0;
181}
182
Paul McLeanb25ece42014-03-21 15:27:34 -0700183static void param_set_max(struct snd_pcm_hw_params *p, int n, unsigned int val)
184{
185 if (param_is_interval(n)) {
186 struct snd_interval *i = param_to_interval(p, n);
187 i->max = val;
188 }
189}
190
Simon Wilson42fc2d32012-12-03 11:18:57 -0800191static unsigned int param_get_max(struct snd_pcm_hw_params *p, int n)
192{
193 if (param_is_interval(n)) {
194 struct snd_interval *i = param_to_interval(p, n);
195 return i->max;
196 }
197 return 0;
198}
199
Simon Wilsonedff7082011-06-06 15:33:34 -0700200static void param_set_int(struct snd_pcm_hw_params *p, int n, unsigned int val)
201{
202 if (param_is_interval(n)) {
203 struct snd_interval *i = param_to_interval(p, n);
204 i->min = val;
205 i->max = val;
206 i->integer = 1;
207 }
208}
209
Simon Wilsone9942c82011-10-13 13:57:25 -0700210static unsigned int param_get_int(struct snd_pcm_hw_params *p, int n)
211{
212 if (param_is_interval(n)) {
213 struct snd_interval *i = param_to_interval(p, n);
214 if (i->integer)
215 return i->max;
216 }
217 return 0;
218}
219
Simon Wilsonedff7082011-06-06 15:33:34 -0700220static void param_init(struct snd_pcm_hw_params *p)
221{
222 int n;
223
224 memset(p, 0, sizeof(*p));
225 for (n = SNDRV_PCM_HW_PARAM_FIRST_MASK;
226 n <= SNDRV_PCM_HW_PARAM_LAST_MASK; n++) {
227 struct snd_mask *m = param_to_mask(p, n);
228 m->bits[0] = ~0;
229 m->bits[1] = ~0;
230 }
231 for (n = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL;
232 n <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; n++) {
233 struct snd_interval *i = param_to_interval(p, n);
234 i->min = 0;
235 i->max = ~0;
236 }
Simon Wilson42fc2d32012-12-03 11:18:57 -0800237 p->rmask = ~0U;
238 p->cmask = 0;
239 p->info = ~0U;
Simon Wilsonedff7082011-06-06 15:33:34 -0700240}
241
242#define PCM_ERROR_MAX 128
243
244struct pcm {
245 int fd;
246 unsigned int flags;
247 int running:1;
248 int underruns;
249 unsigned int buffer_size;
Simon Wilsone9942c82011-10-13 13:57:25 -0700250 unsigned int boundary;
Simon Wilsonedff7082011-06-06 15:33:34 -0700251 char error[PCM_ERROR_MAX];
252 struct pcm_config config;
Simon Wilsondd88f132011-07-25 10:58:30 -0700253 struct snd_pcm_mmap_status *mmap_status;
254 struct snd_pcm_mmap_control *mmap_control;
255 struct snd_pcm_sync_ptr *sync_ptr;
Simon Wilsone9942c82011-10-13 13:57:25 -0700256 void *mmap_buffer;
257 unsigned int noirq_frames_per_msec;
Eric Laurent73b9c672011-10-14 11:12:24 -0700258 int wait_for_avail_min;
Simon Wilsonedff7082011-06-06 15:33:34 -0700259};
260
261unsigned int pcm_get_buffer_size(struct pcm *pcm)
262{
263 return pcm->buffer_size;
264}
265
266const char* pcm_get_error(struct pcm *pcm)
267{
268 return pcm->error;
269}
270
271static int oops(struct pcm *pcm, int e, const char *fmt, ...)
272{
273 va_list ap;
274 int sz;
275
276 va_start(ap, fmt);
277 vsnprintf(pcm->error, PCM_ERROR_MAX, fmt, ap);
278 va_end(ap);
279 sz = strlen(pcm->error);
280
281 if (errno)
282 snprintf(pcm->error + sz, PCM_ERROR_MAX - sz,
283 ": %s", strerror(e));
284 return -1;
285}
286
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700287static unsigned int pcm_format_to_alsa(enum pcm_format format)
288{
289 switch (format) {
290 case PCM_FORMAT_S32_LE:
291 return SNDRV_PCM_FORMAT_S32_LE;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800292 case PCM_FORMAT_S8:
293 return SNDRV_PCM_FORMAT_S8;
Glenn Kastend9837d02014-01-31 07:56:33 -0800294 case PCM_FORMAT_S24_3LE:
295 return SNDRV_PCM_FORMAT_S24_3LE;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800296 case PCM_FORMAT_S24_LE:
297 return SNDRV_PCM_FORMAT_S24_LE;
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700298 default:
299 case PCM_FORMAT_S16_LE:
300 return SNDRV_PCM_FORMAT_S16_LE;
301 };
302}
303
Simon Wilson36ea2d82013-07-17 11:10:45 -0700304unsigned int pcm_format_to_bits(enum pcm_format format)
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700305{
306 switch (format) {
307 case PCM_FORMAT_S32_LE:
Simon Wilson36ea2d82013-07-17 11:10:45 -0700308 case PCM_FORMAT_S24_LE:
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700309 return 32;
Glenn Kastend9837d02014-01-31 07:56:33 -0800310 case PCM_FORMAT_S24_3LE:
311 return 24;
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700312 default:
313 case PCM_FORMAT_S16_LE:
314 return 16;
315 };
316}
317
Simon Wilsone9942c82011-10-13 13:57:25 -0700318unsigned int pcm_bytes_to_frames(struct pcm *pcm, unsigned int bytes)
319{
320 return bytes / (pcm->config.channels *
321 (pcm_format_to_bits(pcm->config.format) >> 3));
322}
323
324unsigned int pcm_frames_to_bytes(struct pcm *pcm, unsigned int frames)
325{
326 return frames * pcm->config.channels *
327 (pcm_format_to_bits(pcm->config.format) >> 3);
328}
329
Simon Wilsondd88f132011-07-25 10:58:30 -0700330static int pcm_sync_ptr(struct pcm *pcm, int flags) {
331 if (pcm->sync_ptr) {
332 pcm->sync_ptr->flags = flags;
333 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SYNC_PTR, pcm->sync_ptr) < 0)
334 return -1;
335 }
336 return 0;
337}
338
339static int pcm_hw_mmap_status(struct pcm *pcm) {
340
341 if (pcm->sync_ptr)
342 return 0;
343
344 int page_size = sysconf(_SC_PAGE_SIZE);
345 pcm->mmap_status = mmap(NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED,
346 pcm->fd, SNDRV_PCM_MMAP_OFFSET_STATUS);
347 if (pcm->mmap_status == MAP_FAILED)
348 pcm->mmap_status = NULL;
349 if (!pcm->mmap_status)
350 goto mmap_error;
351
352 pcm->mmap_control = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
353 MAP_FILE | MAP_SHARED, pcm->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL);
354 if (pcm->mmap_control == MAP_FAILED)
355 pcm->mmap_control = NULL;
356 if (!pcm->mmap_control) {
357 munmap(pcm->mmap_status, page_size);
358 pcm->mmap_status = NULL;
359 goto mmap_error;
360 }
Eric Laurent73b9c672011-10-14 11:12:24 -0700361 if (pcm->flags & PCM_MMAP)
362 pcm->mmap_control->avail_min = pcm->config.avail_min;
363 else
364 pcm->mmap_control->avail_min = 1;
Simon Wilsondd88f132011-07-25 10:58:30 -0700365
366 return 0;
367
368mmap_error:
369
370 pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr));
371 if (!pcm->sync_ptr)
372 return -ENOMEM;
373 pcm->mmap_status = &pcm->sync_ptr->s.status;
374 pcm->mmap_control = &pcm->sync_ptr->c.control;
Eric Laurent73b9c672011-10-14 11:12:24 -0700375 if (pcm->flags & PCM_MMAP)
376 pcm->mmap_control->avail_min = pcm->config.avail_min;
377 else
378 pcm->mmap_control->avail_min = 1;
379
Simon Wilsondd88f132011-07-25 10:58:30 -0700380 pcm_sync_ptr(pcm, 0);
381
382 return 0;
383}
384
385static void pcm_hw_munmap_status(struct pcm *pcm) {
386 if (pcm->sync_ptr) {
387 free(pcm->sync_ptr);
388 pcm->sync_ptr = NULL;
389 } else {
390 int page_size = sysconf(_SC_PAGE_SIZE);
391 if (pcm->mmap_status)
392 munmap(pcm->mmap_status, page_size);
393 if (pcm->mmap_control)
394 munmap(pcm->mmap_control, page_size);
395 }
396 pcm->mmap_status = NULL;
397 pcm->mmap_control = NULL;
398}
399
Simon Wilsone9942c82011-10-13 13:57:25 -0700400static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
Eric Laurentc98da792013-09-16 14:31:17 -0700401 char *buf, unsigned int src_offset,
Simon Wilsone9942c82011-10-13 13:57:25 -0700402 unsigned int frames)
403{
404 int size_bytes = pcm_frames_to_bytes(pcm, frames);
405 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
406 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
407
408 /* interleaved only atm */
Eric Laurentc98da792013-09-16 14:31:17 -0700409 if (pcm->flags & PCM_IN)
410 memcpy(buf + src_offset_bytes,
411 (char*)pcm->mmap_buffer + pcm_offset_bytes,
412 size_bytes);
413 else
414 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
415 buf + src_offset_bytes,
416 size_bytes);
Simon Wilsone9942c82011-10-13 13:57:25 -0700417 return 0;
418}
419
Eric Laurentc98da792013-09-16 14:31:17 -0700420static int pcm_mmap_transfer_areas(struct pcm *pcm, char *buf,
Simon Wilsone9942c82011-10-13 13:57:25 -0700421 unsigned int offset, unsigned int size)
422{
423 void *pcm_areas;
424 int commit;
425 unsigned int pcm_offset, frames, count = 0;
426
427 while (size > 0) {
428 frames = size;
429 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
Eric Laurentc98da792013-09-16 14:31:17 -0700430 pcm_areas_copy(pcm, pcm_offset, buf, offset, frames);
Simon Wilsone9942c82011-10-13 13:57:25 -0700431 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
432 if (commit < 0) {
433 oops(pcm, commit, "failed to commit %d frames\n", frames);
434 return commit;
435 }
436
437 offset += commit;
438 count += commit;
439 size -= commit;
440 }
441 return count;
442}
443
Simon Wilsondd88f132011-07-25 10:58:30 -0700444int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
445 struct timespec *tstamp)
446{
447 int frames;
448 int rc;
449 snd_pcm_uframes_t hw_ptr;
450
451 if (!pcm_is_ready(pcm))
452 return -1;
453
454 rc = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_HWSYNC);
455 if (rc < 0)
456 return -1;
457
Simon Wilson8dd366f2011-11-17 13:16:52 -0800458 if ((pcm->mmap_status->state != PCM_STATE_RUNNING) &&
459 (pcm->mmap_status->state != PCM_STATE_DRAINING))
Simon Wilson5aed71d2011-11-16 14:45:38 -0800460 return -1;
461
Simon Wilsondd88f132011-07-25 10:58:30 -0700462 *tstamp = pcm->mmap_status->tstamp;
463 if (tstamp->tv_sec == 0 && tstamp->tv_nsec == 0)
464 return -1;
465
466 hw_ptr = pcm->mmap_status->hw_ptr;
467 if (pcm->flags & PCM_IN)
468 frames = hw_ptr - pcm->mmap_control->appl_ptr;
469 else
470 frames = hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
471
472 if (frames < 0)
Eric Laurent73b9c672011-10-14 11:12:24 -0700473 frames += pcm->boundary;
474 else if (frames > (int)pcm->boundary)
475 frames -= pcm->boundary;
Simon Wilsondd88f132011-07-25 10:58:30 -0700476
477 *avail = (unsigned int)frames;
478
479 return 0;
480}
481
Simon Wilsondaa83292012-02-28 15:26:02 -0800482int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
Simon Wilsonedff7082011-06-06 15:33:34 -0700483{
484 struct snd_xferi x;
485
486 if (pcm->flags & PCM_IN)
487 return -EINVAL;
488
Simon Wilsondaa83292012-02-28 15:26:02 -0800489 x.buf = (void*)data;
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700490 x.frames = count / (pcm->config.channels *
491 pcm_format_to_bits(pcm->config.format) / 8);
Simon Wilsonedff7082011-06-06 15:33:34 -0700492
493 for (;;) {
494 if (!pcm->running) {
495 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE))
496 return oops(pcm, errno, "cannot prepare channel");
497 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x))
498 return oops(pcm, errno, "cannot write initial data");
499 pcm->running = 1;
500 return 0;
501 }
502 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) {
503 pcm->running = 0;
504 if (errno == EPIPE) {
John Grossman673253a2012-04-03 16:37:38 -0700505 /* we failed to make our window -- try to restart if we are
506 * allowed to do so. Otherwise, simply allow the EPIPE error to
507 * propagate up to the app level */
Simon Wilsonedff7082011-06-06 15:33:34 -0700508 pcm->underruns++;
John Grossman673253a2012-04-03 16:37:38 -0700509 if (pcm->flags & PCM_NORESTART)
510 return -EPIPE;
Simon Wilsonedff7082011-06-06 15:33:34 -0700511 continue;
512 }
513 return oops(pcm, errno, "cannot write stream data");
514 }
515 return 0;
516 }
517}
518
519int pcm_read(struct pcm *pcm, void *data, unsigned int count)
520{
521 struct snd_xferi x;
522
523 if (!(pcm->flags & PCM_IN))
524 return -EINVAL;
525
526 x.buf = data;
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700527 x.frames = count / (pcm->config.channels *
528 pcm_format_to_bits(pcm->config.format) / 8);
Simon Wilsonedff7082011-06-06 15:33:34 -0700529
530 for (;;) {
531 if (!pcm->running) {
Simon Wilson85dc38f2012-05-15 17:37:19 -0700532 if (pcm_start(pcm) < 0) {
533 fprintf(stderr, "start error");
534 return -errno;
535 }
Simon Wilsonedff7082011-06-06 15:33:34 -0700536 }
537 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) {
538 pcm->running = 0;
539 if (errno == EPIPE) {
540 /* we failed to make our window -- try to restart */
541 pcm->underruns++;
542 continue;
543 }
544 return oops(pcm, errno, "cannot read stream data");
545 }
546 return 0;
547 }
548}
549
550static struct pcm bad_pcm = {
551 .fd = -1,
552};
553
Simon Wilson42fc2d32012-12-03 11:18:57 -0800554struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
555 unsigned int flags)
556{
557 struct snd_pcm_hw_params *params;
558 char fn[256];
559 int fd;
560
561 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
562 flags & PCM_IN ? 'c' : 'p');
563
564 fd = open(fn, O_RDWR);
565 if (fd < 0) {
566 fprintf(stderr, "cannot open device '%s'\n", fn);
567 goto err_open;
568 }
569
570 params = calloc(1, sizeof(struct snd_pcm_hw_params));
571 if (!params)
572 goto err_calloc;
573
574 param_init(params);
575 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
576 fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
577 goto err_hw_refine;
578 }
579
580 close(fd);
581
582 return (struct pcm_params *)params;
583
584err_hw_refine:
585 free(params);
586err_calloc:
587 close(fd);
588err_open:
589 return NULL;
590}
591
592void pcm_params_free(struct pcm_params *pcm_params)
593{
594 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
595
596 if (params)
597 free(params);
598}
599
600static int pcm_param_to_alsa(enum pcm_param param)
601{
602 switch (param) {
Andy Hunga5b44d92014-03-10 18:08:15 -0700603 case PCM_PARAM_ACCESS:
604 return SNDRV_PCM_HW_PARAM_ACCESS;
605 case PCM_PARAM_FORMAT:
606 return SNDRV_PCM_HW_PARAM_FORMAT;
607 case PCM_PARAM_SUBFORMAT:
608 return SNDRV_PCM_HW_PARAM_SUBFORMAT;
Simon Wilson42fc2d32012-12-03 11:18:57 -0800609 case PCM_PARAM_SAMPLE_BITS:
610 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
611 break;
612 case PCM_PARAM_FRAME_BITS:
613 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
614 break;
615 case PCM_PARAM_CHANNELS:
616 return SNDRV_PCM_HW_PARAM_CHANNELS;
617 break;
618 case PCM_PARAM_RATE:
619 return SNDRV_PCM_HW_PARAM_RATE;
620 break;
621 case PCM_PARAM_PERIOD_TIME:
622 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
623 break;
624 case PCM_PARAM_PERIOD_SIZE:
625 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
626 break;
627 case PCM_PARAM_PERIOD_BYTES:
628 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
629 break;
630 case PCM_PARAM_PERIODS:
631 return SNDRV_PCM_HW_PARAM_PERIODS;
632 break;
633 case PCM_PARAM_BUFFER_TIME:
634 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
635 break;
636 case PCM_PARAM_BUFFER_SIZE:
637 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
638 break;
639 case PCM_PARAM_BUFFER_BYTES:
640 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
641 break;
642 case PCM_PARAM_TICK_TIME:
643 return SNDRV_PCM_HW_PARAM_TICK_TIME;
644 break;
645
646 default:
647 return -1;
648 }
649}
650
Andy Hunga5b44d92014-03-10 18:08:15 -0700651struct pcm_mask *pcm_params_get_mask(struct pcm_params *pcm_params,
652 enum pcm_param param)
653{
654 int p;
655 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
656 if (params == NULL) {
657 return NULL;
658 }
659
660 p = pcm_param_to_alsa(param);
661 if (p < 0 || !param_is_mask(p)) {
662 return NULL;
663 }
664
665 return (struct pcm_mask *)param_to_mask(params, p);
666}
667
Simon Wilson42fc2d32012-12-03 11:18:57 -0800668unsigned int pcm_params_get_min(struct pcm_params *pcm_params,
669 enum pcm_param param)
670{
671 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
672 int p;
673
674 if (!params)
675 return 0;
676
677 p = pcm_param_to_alsa(param);
678 if (p < 0)
679 return 0;
680
681 return param_get_min(params, p);
682}
683
Paul McLeanb25ece42014-03-21 15:27:34 -0700684void pcm_params_set_min(struct pcm_params *pcm_params,
685 enum pcm_param param, unsigned int val)
686{
687 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
688 int p;
689
690 if (!params)
691 return;
692
693 p = pcm_param_to_alsa(param);
694 if (p < 0)
695 return;
696
697 param_set_min(params, p, val);
698}
699
Simon Wilson42fc2d32012-12-03 11:18:57 -0800700unsigned int pcm_params_get_max(struct pcm_params *pcm_params,
701 enum pcm_param param)
702{
703 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
704 int p;
705
706 if (!params)
707 return 0;
708
709 p = pcm_param_to_alsa(param);
710 if (p < 0)
711 return 0;
712
713 return param_get_max(params, p);
714}
715
Paul McLeanb25ece42014-03-21 15:27:34 -0700716void pcm_params_set_max(struct pcm_params *pcm_params,
717 enum pcm_param param, unsigned int val)
718{
719 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
720 int p;
721
722 if (!params)
723 return;
724
725 p = pcm_param_to_alsa(param);
726 if (p < 0)
727 return;
728
729 param_set_max(params, p, val);
730}
731
Andy Hung70530a62014-03-26 18:29:07 -0700732static int pcm_mask_test(struct pcm_mask *m, unsigned int index)
733{
734 const unsigned int bitshift = 5; /* for 32 bit integer */
735 const unsigned int bitmask = (1 << bitshift) - 1;
736 unsigned int element;
737
738 element = index >> bitshift;
739 if (element >= ARRAY_SIZE(m->bits))
740 return 0; /* for safety, but should never occur */
741 return (m->bits[element] >> (index & bitmask)) & 1;
742}
743
744static int pcm_mask_to_string(struct pcm_mask *m, char *string, unsigned int size,
745 char *mask_name,
746 const char * const *bit_array_name, size_t bit_array_size)
747{
748 unsigned int i;
749 unsigned int offset = 0;
750
751 if (m == NULL)
752 return 0;
753 if (bit_array_size < 32) {
754 STRLOG(string, offset, size, "%12s:\t%#08x\n", mask_name, m->bits[0]);
755 } else { /* spans two or more bitfields, print with an array index */
756 for (i = 0; i < (bit_array_size + 31) >> 5; ++i) {
757 STRLOG(string, offset, size, "%9s[%d]:\t%#08x\n",
758 mask_name, i, m->bits[i]);
759 }
760 }
761 for (i = 0; i < bit_array_size; ++i) {
762 if (pcm_mask_test(m, i)) {
763 STRLOG(string, offset, size, "%12s \t%s\n", "", bit_array_name[i]);
764 }
765 }
766 return offset;
767}
768
769int pcm_params_to_string(struct pcm_params *params, char *string, unsigned int size)
770{
771 struct pcm_mask *m;
772 unsigned int min, max;
773 unsigned int clipoffset, offset;
774
775 m = pcm_params_get_mask(params, PCM_PARAM_ACCESS);
776 offset = pcm_mask_to_string(m, string, size,
777 "Access", access_lookup, ARRAY_SIZE(access_lookup));
778 m = pcm_params_get_mask(params, PCM_PARAM_FORMAT);
779 clipoffset = offset > size ? size : offset;
780 offset += pcm_mask_to_string(m, string + clipoffset, size - clipoffset,
781 "Format", format_lookup, ARRAY_SIZE(format_lookup));
782 m = pcm_params_get_mask(params, PCM_PARAM_SUBFORMAT);
783 clipoffset = offset > size ? size : offset;
784 offset += pcm_mask_to_string(m, string + clipoffset, size - clipoffset,
785 "Subformat", subformat_lookup, ARRAY_SIZE(subformat_lookup));
786 min = pcm_params_get_min(params, PCM_PARAM_RATE);
787 max = pcm_params_get_max(params, PCM_PARAM_RATE);
788 STRLOG(string, offset, size, " Rate:\tmin=%uHz\tmax=%uHz\n", min, max);
789 min = pcm_params_get_min(params, PCM_PARAM_CHANNELS);
790 max = pcm_params_get_max(params, PCM_PARAM_CHANNELS);
791 STRLOG(string, offset, size, " Channels:\tmin=%u\t\tmax=%u\n", min, max);
792 min = pcm_params_get_min(params, PCM_PARAM_SAMPLE_BITS);
793 max = pcm_params_get_max(params, PCM_PARAM_SAMPLE_BITS);
794 STRLOG(string, offset, size, " Sample bits:\tmin=%u\t\tmax=%u\n", min, max);
795 min = pcm_params_get_min(params, PCM_PARAM_PERIOD_SIZE);
796 max = pcm_params_get_max(params, PCM_PARAM_PERIOD_SIZE);
797 STRLOG(string, offset, size, " Period size:\tmin=%u\t\tmax=%u\n", min, max);
798 min = pcm_params_get_min(params, PCM_PARAM_PERIODS);
799 max = pcm_params_get_max(params, PCM_PARAM_PERIODS);
800 STRLOG(string, offset, size, "Period count:\tmin=%u\t\tmax=%u\n", min, max);
801 return offset;
802}
803
804int pcm_params_format_test(struct pcm_params *params, enum pcm_format format)
805{
806 unsigned int alsa_format = pcm_format_to_alsa(format);
807
808 if (alsa_format == SNDRV_PCM_FORMAT_S16_LE && format != PCM_FORMAT_S16_LE)
809 return 0; /* caution: format not recognized is equivalent to S16_LE */
810 return pcm_mask_test(pcm_params_get_mask(params, PCM_PARAM_FORMAT), alsa_format);
811}
812
Simon Wilsonedff7082011-06-06 15:33:34 -0700813int pcm_close(struct pcm *pcm)
814{
815 if (pcm == &bad_pcm)
816 return 0;
817
Simon Wilsondd88f132011-07-25 10:58:30 -0700818 pcm_hw_munmap_status(pcm);
819
Simon Wilsone9942c82011-10-13 13:57:25 -0700820 if (pcm->flags & PCM_MMAP) {
821 pcm_stop(pcm);
822 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
823 }
824
Simon Wilsonedff7082011-06-06 15:33:34 -0700825 if (pcm->fd >= 0)
826 close(pcm->fd);
827 pcm->running = 0;
828 pcm->buffer_size = 0;
829 pcm->fd = -1;
Simon Wilsondd88f132011-07-25 10:58:30 -0700830 free(pcm);
Simon Wilsonedff7082011-06-06 15:33:34 -0700831 return 0;
832}
833
Simon Wilsonedff7082011-06-06 15:33:34 -0700834struct pcm *pcm_open(unsigned int card, unsigned int device,
835 unsigned int flags, struct pcm_config *config)
836{
837 struct pcm *pcm;
838 struct snd_pcm_info info;
839 struct snd_pcm_hw_params params;
840 struct snd_pcm_sw_params sparams;
841 char fn[256];
Simon Wilsondd88f132011-07-25 10:58:30 -0700842 int rc;
Simon Wilsonedff7082011-06-06 15:33:34 -0700843
844 pcm = calloc(1, sizeof(struct pcm));
845 if (!pcm || !config)
846 return &bad_pcm; /* TODO: could support default config here */
847
848 pcm->config = *config;
849
850 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
851 flags & PCM_IN ? 'c' : 'p');
852
853 pcm->flags = flags;
854 pcm->fd = open(fn, O_RDWR);
855 if (pcm->fd < 0) {
856 oops(pcm, errno, "cannot open device '%s'", fn);
857 return pcm;
858 }
859
860 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
861 oops(pcm, errno, "cannot get info");
Simon Wilsone9942c82011-10-13 13:57:25 -0700862 goto fail_close;
Simon Wilsonedff7082011-06-06 15:33:34 -0700863 }
864
865 param_init(&params);
Simon Wilsonedff7082011-06-06 15:33:34 -0700866 param_set_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT,
867 pcm_format_to_alsa(config->format));
868 param_set_mask(&params, SNDRV_PCM_HW_PARAM_SUBFORMAT,
869 SNDRV_PCM_SUBFORMAT_STD);
870 param_set_min(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
871 param_set_int(&params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
872 pcm_format_to_bits(config->format));
873 param_set_int(&params, SNDRV_PCM_HW_PARAM_FRAME_BITS,
874 pcm_format_to_bits(config->format) * config->channels);
875 param_set_int(&params, SNDRV_PCM_HW_PARAM_CHANNELS,
876 config->channels);
877 param_set_int(&params, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
878 param_set_int(&params, SNDRV_PCM_HW_PARAM_RATE, config->rate);
879
Simon Wilsone9942c82011-10-13 13:57:25 -0700880 if (flags & PCM_NOIRQ) {
Simon Wilsone9942c82011-10-13 13:57:25 -0700881 if (!(flags & PCM_MMAP)) {
882 oops(pcm, -EINVAL, "noirq only currently supported with mmap().");
883 goto fail;
884 }
885
886 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
887 pcm->noirq_frames_per_msec = config->rate / 1000;
888 }
889
890 if (flags & PCM_MMAP)
891 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
Paul McLeanb25ece42014-03-21 15:27:34 -0700892 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
Simon Wilsone9942c82011-10-13 13:57:25 -0700893 else
894 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
Paul McLeanb25ece42014-03-21 15:27:34 -0700895 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
Simon Wilsone9942c82011-10-13 13:57:25 -0700896
Simon Wilsonedff7082011-06-06 15:33:34 -0700897 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
898 oops(pcm, errno, "cannot set hw params");
Simon Wilsone9942c82011-10-13 13:57:25 -0700899 goto fail_close;
Simon Wilsonedff7082011-06-06 15:33:34 -0700900 }
901
Simon Wilsone9942c82011-10-13 13:57:25 -0700902 /* get our refined hw_params */
903 config->period_size = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
904 config->period_count = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIODS);
905 pcm->buffer_size = config->period_count * config->period_size;
906
907 if (flags & PCM_MMAP) {
908 pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size),
909 PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0);
910 if (pcm->mmap_buffer == MAP_FAILED) {
911 oops(pcm, -errno, "failed to mmap buffer %d bytes\n",
912 pcm_frames_to_bytes(pcm, pcm->buffer_size));
913 goto fail_close;
914 }
915 }
916
Simon Wilsonedff7082011-06-06 15:33:34 -0700917 memset(&sparams, 0, sizeof(sparams));
Simon Wilsondd88f132011-07-25 10:58:30 -0700918 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
Simon Wilsonedff7082011-06-06 15:33:34 -0700919 sparams.period_step = 1;
Simon Wilsonc1239622011-07-27 15:08:34 -0700920
Eric Laurentff2e5422012-08-22 16:18:14 -0700921 if (!config->start_threshold) {
922 if (pcm->flags & PCM_IN)
923 pcm->config.start_threshold = sparams.start_threshold = 1;
924 else
925 pcm->config.start_threshold = sparams.start_threshold =
926 config->period_count * config->period_size / 2;
927 } else
Simon Wilsonc1239622011-07-27 15:08:34 -0700928 sparams.start_threshold = config->start_threshold;
929
Simon Wilsone9942c82011-10-13 13:57:25 -0700930 /* pick a high stop threshold - todo: does this need further tuning */
Eric Laurent1b32ddf2012-01-30 11:31:56 -0800931 if (!config->stop_threshold) {
932 if (pcm->flags & PCM_IN)
933 pcm->config.stop_threshold = sparams.stop_threshold =
934 config->period_count * config->period_size * 10;
935 else
936 pcm->config.stop_threshold = sparams.stop_threshold =
937 config->period_count * config->period_size;
938 }
Simon Wilsonc1239622011-07-27 15:08:34 -0700939 else
940 sparams.stop_threshold = config->stop_threshold;
941
Eric Laurent73b9c672011-10-14 11:12:24 -0700942 if (!pcm->config.avail_min) {
943 if (pcm->flags & PCM_MMAP)
944 pcm->config.avail_min = sparams.avail_min = pcm->config.period_size;
945 else
946 pcm->config.avail_min = sparams.avail_min = 1;
947 } else
948 sparams.avail_min = config->avail_min;
949
Simon Wilsonedff7082011-06-06 15:33:34 -0700950 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
951 sparams.silence_size = 0;
Simon Wilsonc1239622011-07-27 15:08:34 -0700952 sparams.silence_threshold = config->silence_threshold;
Simon Wilsone9942c82011-10-13 13:57:25 -0700953 pcm->boundary = sparams.boundary = pcm->buffer_size;
Simon Wilsonc1239622011-07-27 15:08:34 -0700954
Simon Wilsondaa83292012-02-28 15:26:02 -0800955 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
Paul McLeanb25ece42014-03-21 15:27:34 -0700956 pcm->boundary *= 2;
Simon Wilsonedff7082011-06-06 15:33:34 -0700957
958 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
959 oops(pcm, errno, "cannot set sw params");
960 goto fail;
961 }
962
Simon Wilsondd88f132011-07-25 10:58:30 -0700963 rc = pcm_hw_mmap_status(pcm);
964 if (rc < 0) {
965 oops(pcm, rc, "mmap status failed");
966 goto fail;
967 }
968
Glenn Kasten6b0a2062013-08-22 15:11:48 -0700969#ifdef SNDRV_PCM_IOCTL_TTSTAMP
970 if (pcm->flags & PCM_MONOTONIC) {
971 int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
972 rc = ioctl(pcm->fd, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
973 if (rc < 0) {
974 oops(pcm, rc, "cannot set timestamp type");
975 goto fail;
976 }
977 }
978#endif
979
Simon Wilsonedff7082011-06-06 15:33:34 -0700980 pcm->underruns = 0;
981 return pcm;
982
983fail:
Simon Wilsone9942c82011-10-13 13:57:25 -0700984 if (flags & PCM_MMAP)
985 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
986fail_close:
Simon Wilsonedff7082011-06-06 15:33:34 -0700987 close(pcm->fd);
988 pcm->fd = -1;
989 return pcm;
990}
991
992int pcm_is_ready(struct pcm *pcm)
993{
994 return pcm->fd >= 0;
995}
Simon Wilson70d77082011-06-24 11:08:10 -0700996
997int pcm_start(struct pcm *pcm)
998{
999 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
1000 return oops(pcm, errno, "cannot prepare channel");
Simon Wilsone9942c82011-10-13 13:57:25 -07001001
1002 if (pcm->flags & PCM_MMAP)
1003 pcm_sync_ptr(pcm, 0);
1004
Simon Wilson70d77082011-06-24 11:08:10 -07001005 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
1006 return oops(pcm, errno, "cannot start channel");
1007
Simon Wilsone9942c82011-10-13 13:57:25 -07001008 pcm->running = 1;
Simon Wilson70d77082011-06-24 11:08:10 -07001009 return 0;
1010}
1011
1012int pcm_stop(struct pcm *pcm)
1013{
1014 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
1015 return oops(pcm, errno, "cannot stop channel");
1016
Simon Wilsone9942c82011-10-13 13:57:25 -07001017 pcm->running = 0;
Simon Wilson70d77082011-06-24 11:08:10 -07001018 return 0;
1019}
1020
Simon Wilsone9942c82011-10-13 13:57:25 -07001021static inline int pcm_mmap_playback_avail(struct pcm *pcm)
1022{
1023 int avail;
1024
1025 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
1026
1027 if (avail < 0)
1028 avail += pcm->boundary;
1029 else if (avail > (int)pcm->boundary)
1030 avail -= pcm->boundary;
1031
1032 return avail;
1033}
1034
1035static inline int pcm_mmap_capture_avail(struct pcm *pcm)
1036{
1037 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
1038 if (avail < 0)
1039 avail += pcm->boundary;
1040 return avail;
1041}
1042
1043static inline int pcm_mmap_avail(struct pcm *pcm)
1044{
1045 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
1046 if (pcm->flags & PCM_IN)
1047 return pcm_mmap_capture_avail(pcm);
1048 else
1049 return pcm_mmap_playback_avail(pcm);
1050}
1051
1052static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
1053{
1054 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
1055 appl_ptr += frames;
1056
1057 /* check for boundary wrap */
1058 if (appl_ptr > pcm->boundary)
1059 appl_ptr -= pcm->boundary;
1060 pcm->mmap_control->appl_ptr = appl_ptr;
1061}
1062
1063int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
1064 unsigned int *frames)
1065{
1066 unsigned int continuous, copy_frames, avail;
1067
1068 /* return the mmap buffer */
1069 *areas = pcm->mmap_buffer;
1070
1071 /* and the application offset in frames */
1072 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
1073
1074 avail = pcm_mmap_avail(pcm);
1075 if (avail > pcm->buffer_size)
1076 avail = pcm->buffer_size;
1077 continuous = pcm->buffer_size - *offset;
1078
1079 /* we can only copy frames if the are availabale and continuos */
1080 copy_frames = *frames;
1081 if (copy_frames > avail)
1082 copy_frames = avail;
1083 if (copy_frames > continuous)
1084 copy_frames = continuous;
1085 *frames = copy_frames;
1086
1087 return 0;
1088}
1089
1090int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
1091{
1092 /* update the application pointer in userspace and kernel */
1093 pcm_mmap_appl_forward(pcm, frames);
1094 pcm_sync_ptr(pcm, 0);
1095
1096 return frames;
1097}
1098
1099int pcm_avail_update(struct pcm *pcm)
1100{
1101 pcm_sync_ptr(pcm, 0);
1102 return pcm_mmap_avail(pcm);
1103}
1104
1105int pcm_state(struct pcm *pcm)
1106{
1107 int err = pcm_sync_ptr(pcm, 0);
1108 if (err < 0)
1109 return err;
1110
1111 return pcm->mmap_status->state;
1112}
1113
Eric Laurent73b9c672011-10-14 11:12:24 -07001114int pcm_set_avail_min(struct pcm *pcm, int avail_min)
1115{
1116 if ((~pcm->flags) & (PCM_MMAP | PCM_NOIRQ))
1117 return -ENOSYS;
1118
1119 pcm->config.avail_min = avail_min;
1120 return 0;
1121}
1122
Simon Wilsone9942c82011-10-13 13:57:25 -07001123int pcm_wait(struct pcm *pcm, int timeout)
1124{
1125 struct pollfd pfd;
Simon Wilsone9942c82011-10-13 13:57:25 -07001126 int err;
1127
1128 pfd.fd = pcm->fd;
1129 pfd.events = POLLOUT | POLLERR | POLLNVAL;
1130
1131 do {
1132 /* let's wait for avail or timeout */
1133 err = poll(&pfd, 1, timeout);
1134 if (err < 0)
1135 return -errno;
1136
1137 /* timeout ? */
1138 if (err == 0)
1139 return 0;
1140
1141 /* have we been interrupted ? */
1142 if (errno == -EINTR)
1143 continue;
1144
1145 /* check for any errors */
1146 if (pfd.revents & (POLLERR | POLLNVAL)) {
1147 switch (pcm_state(pcm)) {
1148 case PCM_STATE_XRUN:
1149 return -EPIPE;
1150 case PCM_STATE_SUSPENDED:
1151 return -ESTRPIPE;
1152 case PCM_STATE_DISCONNECTED:
1153 return -ENODEV;
1154 default:
1155 return -EIO;
1156 }
1157 }
1158 /* poll again if fd not ready for IO */
1159 } while (!(pfd.revents & (POLLIN | POLLOUT)));
1160
1161 return 1;
1162}
1163
Eric Laurentc98da792013-09-16 14:31:17 -07001164int pcm_mmap_transfer(struct pcm *pcm, const void *buffer, unsigned int bytes)
Simon Wilsone9942c82011-10-13 13:57:25 -07001165{
1166 int err = 0, frames, avail;
1167 unsigned int offset = 0, count;
1168
1169 if (bytes == 0)
1170 return 0;
1171
1172 count = pcm_bytes_to_frames(pcm, bytes);
1173
1174 while (count > 0) {
1175
1176 /* get the available space for writing new frames */
1177 avail = pcm_avail_update(pcm);
1178 if (avail < 0) {
1179 fprintf(stderr, "cannot determine available mmap frames");
1180 return err;
1181 }
1182
1183 /* start the audio if we reach the threshold */
1184 if (!pcm->running &&
1185 (pcm->buffer_size - avail) >= pcm->config.start_threshold) {
1186 if (pcm_start(pcm) < 0) {
1187 fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n",
1188 (unsigned int)pcm->mmap_status->hw_ptr,
1189 (unsigned int)pcm->mmap_control->appl_ptr,
1190 avail);
1191 return -errno;
1192 }
Eric Laurent73b9c672011-10-14 11:12:24 -07001193 pcm->wait_for_avail_min = 0;
Simon Wilsone9942c82011-10-13 13:57:25 -07001194 }
1195
1196 /* sleep until we have space to write new frames */
Eric Laurent73b9c672011-10-14 11:12:24 -07001197 if (pcm->running) {
1198 /* enable waiting for avail_min threshold when less frames than we have to write
1199 * are available. */
1200 if (!pcm->wait_for_avail_min && (count > (unsigned int)avail))
1201 pcm->wait_for_avail_min = 1;
Simon Wilsone9942c82011-10-13 13:57:25 -07001202
Eric Laurent73b9c672011-10-14 11:12:24 -07001203 if (pcm->wait_for_avail_min && (avail < pcm->config.avail_min)) {
1204 int time = -1;
Simon Wilsone9942c82011-10-13 13:57:25 -07001205
Eric Laurent73b9c672011-10-14 11:12:24 -07001206 /* disable waiting for avail_min threshold to allow small amounts of data to be
1207 * written without waiting as long as there is enough room in buffer. */
1208 pcm->wait_for_avail_min = 0;
1209
1210 if (pcm->flags & PCM_NOIRQ)
1211 time = (pcm->config.avail_min - avail) / pcm->noirq_frames_per_msec;
1212
1213 err = pcm_wait(pcm, time);
1214 if (err < 0) {
1215 pcm->running = 0;
1216 oops(pcm, err, "wait error: hw 0x%x app 0x%x avail 0x%x\n",
1217 (unsigned int)pcm->mmap_status->hw_ptr,
1218 (unsigned int)pcm->mmap_control->appl_ptr,
1219 avail);
1220 pcm->mmap_control->appl_ptr = 0;
1221 return err;
1222 }
1223 continue;
Simon Wilsone9942c82011-10-13 13:57:25 -07001224 }
Simon Wilsone9942c82011-10-13 13:57:25 -07001225 }
1226
1227 frames = count;
1228 if (frames > avail)
1229 frames = avail;
1230
1231 if (!frames)
1232 break;
1233
1234 /* copy frames from buffer */
Eric Laurentc98da792013-09-16 14:31:17 -07001235 frames = pcm_mmap_transfer_areas(pcm, (void *)buffer, offset, frames);
Simon Wilsone9942c82011-10-13 13:57:25 -07001236 if (frames < 0) {
1237 fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n",
1238 (unsigned int)pcm->mmap_status->hw_ptr,
1239 (unsigned int)pcm->mmap_control->appl_ptr,
1240 avail);
1241 return frames;
1242 }
1243
1244 offset += frames;
1245 count -= frames;
1246 }
1247
Simon Wilsone9942c82011-10-13 13:57:25 -07001248 return 0;
1249}
Eric Laurentc98da792013-09-16 14:31:17 -07001250
1251int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count)
1252{
1253 if ((~pcm->flags) & (PCM_OUT | PCM_MMAP))
1254 return -ENOSYS;
1255
1256 return pcm_mmap_transfer(pcm, (void *)data, count);
1257}
1258
1259int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count)
1260{
1261 if ((~pcm->flags) & (PCM_IN | PCM_MMAP))
1262 return -ENOSYS;
1263
1264 return pcm_mmap_transfer(pcm, data, count);
1265}