blob: 7c05a87e7039cb2543a40aec4a66083692e7776b [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
54static inline int param_is_mask(int p)
55{
56 return (p >= SNDRV_PCM_HW_PARAM_FIRST_MASK) &&
57 (p <= SNDRV_PCM_HW_PARAM_LAST_MASK);
58}
59
60static inline int param_is_interval(int p)
61{
62 return (p >= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL) &&
63 (p <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL);
64}
65
66static inline struct snd_interval *param_to_interval(struct snd_pcm_hw_params *p, int n)
67{
68 return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]);
69}
70
71static inline struct snd_mask *param_to_mask(struct snd_pcm_hw_params *p, int n)
72{
73 return &(p->masks[n - SNDRV_PCM_HW_PARAM_FIRST_MASK]);
74}
75
76static void param_set_mask(struct snd_pcm_hw_params *p, int n, unsigned int bit)
77{
78 if (bit >= SNDRV_MASK_MAX)
79 return;
80 if (param_is_mask(n)) {
81 struct snd_mask *m = param_to_mask(p, n);
82 m->bits[0] = 0;
83 m->bits[1] = 0;
84 m->bits[bit >> 5] |= (1 << (bit & 31));
85 }
86}
87
88static void param_set_min(struct snd_pcm_hw_params *p, int n, unsigned int val)
89{
90 if (param_is_interval(n)) {
91 struct snd_interval *i = param_to_interval(p, n);
92 i->min = val;
93 }
94}
95
Simon Wilson42fc2d32012-12-03 11:18:57 -080096static unsigned int param_get_min(struct snd_pcm_hw_params *p, int n)
97{
98 if (param_is_interval(n)) {
99 struct snd_interval *i = param_to_interval(p, n);
100 return i->min;
101 }
102 return 0;
103}
104
105static unsigned int param_get_max(struct snd_pcm_hw_params *p, int n)
106{
107 if (param_is_interval(n)) {
108 struct snd_interval *i = param_to_interval(p, n);
109 return i->max;
110 }
111 return 0;
112}
113
Simon Wilsonedff7082011-06-06 15:33:34 -0700114static void param_set_int(struct snd_pcm_hw_params *p, int n, unsigned int val)
115{
116 if (param_is_interval(n)) {
117 struct snd_interval *i = param_to_interval(p, n);
118 i->min = val;
119 i->max = val;
120 i->integer = 1;
121 }
122}
123
Simon Wilsone9942c82011-10-13 13:57:25 -0700124static unsigned int param_get_int(struct snd_pcm_hw_params *p, int n)
125{
126 if (param_is_interval(n)) {
127 struct snd_interval *i = param_to_interval(p, n);
128 if (i->integer)
129 return i->max;
130 }
131 return 0;
132}
133
Simon Wilsonedff7082011-06-06 15:33:34 -0700134static void param_init(struct snd_pcm_hw_params *p)
135{
136 int n;
137
138 memset(p, 0, sizeof(*p));
139 for (n = SNDRV_PCM_HW_PARAM_FIRST_MASK;
140 n <= SNDRV_PCM_HW_PARAM_LAST_MASK; n++) {
141 struct snd_mask *m = param_to_mask(p, n);
142 m->bits[0] = ~0;
143 m->bits[1] = ~0;
144 }
145 for (n = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL;
146 n <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; n++) {
147 struct snd_interval *i = param_to_interval(p, n);
148 i->min = 0;
149 i->max = ~0;
150 }
Simon Wilson42fc2d32012-12-03 11:18:57 -0800151 p->rmask = ~0U;
152 p->cmask = 0;
153 p->info = ~0U;
Simon Wilsonedff7082011-06-06 15:33:34 -0700154}
155
156#define PCM_ERROR_MAX 128
157
158struct pcm {
159 int fd;
160 unsigned int flags;
161 int running:1;
162 int underruns;
163 unsigned int buffer_size;
Simon Wilsone9942c82011-10-13 13:57:25 -0700164 unsigned int boundary;
Simon Wilsonedff7082011-06-06 15:33:34 -0700165 char error[PCM_ERROR_MAX];
166 struct pcm_config config;
Simon Wilsondd88f132011-07-25 10:58:30 -0700167 struct snd_pcm_mmap_status *mmap_status;
168 struct snd_pcm_mmap_control *mmap_control;
169 struct snd_pcm_sync_ptr *sync_ptr;
Simon Wilsone9942c82011-10-13 13:57:25 -0700170 void *mmap_buffer;
171 unsigned int noirq_frames_per_msec;
Eric Laurent73b9c672011-10-14 11:12:24 -0700172 int wait_for_avail_min;
Simon Wilsonedff7082011-06-06 15:33:34 -0700173};
174
175unsigned int pcm_get_buffer_size(struct pcm *pcm)
176{
177 return pcm->buffer_size;
178}
179
180const char* pcm_get_error(struct pcm *pcm)
181{
182 return pcm->error;
183}
184
185static int oops(struct pcm *pcm, int e, const char *fmt, ...)
186{
187 va_list ap;
188 int sz;
189
190 va_start(ap, fmt);
191 vsnprintf(pcm->error, PCM_ERROR_MAX, fmt, ap);
192 va_end(ap);
193 sz = strlen(pcm->error);
194
195 if (errno)
196 snprintf(pcm->error + sz, PCM_ERROR_MAX - sz,
197 ": %s", strerror(e));
198 return -1;
199}
200
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700201static unsigned int pcm_format_to_alsa(enum pcm_format format)
202{
203 switch (format) {
204 case PCM_FORMAT_S32_LE:
205 return SNDRV_PCM_FORMAT_S32_LE;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800206 case PCM_FORMAT_S8:
207 return SNDRV_PCM_FORMAT_S8;
Glenn Kastend9837d02014-01-31 07:56:33 -0800208 case PCM_FORMAT_S24_3LE:
209 return SNDRV_PCM_FORMAT_S24_3LE;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800210 case PCM_FORMAT_S24_LE:
211 return SNDRV_PCM_FORMAT_S24_LE;
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700212 default:
213 case PCM_FORMAT_S16_LE:
214 return SNDRV_PCM_FORMAT_S16_LE;
215 };
216}
217
Simon Wilson36ea2d82013-07-17 11:10:45 -0700218unsigned int pcm_format_to_bits(enum pcm_format format)
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700219{
220 switch (format) {
221 case PCM_FORMAT_S32_LE:
Simon Wilson36ea2d82013-07-17 11:10:45 -0700222 case PCM_FORMAT_S24_LE:
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700223 return 32;
Glenn Kastend9837d02014-01-31 07:56:33 -0800224 case PCM_FORMAT_S24_3LE:
225 return 24;
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700226 default:
227 case PCM_FORMAT_S16_LE:
228 return 16;
229 };
230}
231
Simon Wilsone9942c82011-10-13 13:57:25 -0700232unsigned int pcm_bytes_to_frames(struct pcm *pcm, unsigned int bytes)
233{
234 return bytes / (pcm->config.channels *
235 (pcm_format_to_bits(pcm->config.format) >> 3));
236}
237
238unsigned int pcm_frames_to_bytes(struct pcm *pcm, unsigned int frames)
239{
240 return frames * pcm->config.channels *
241 (pcm_format_to_bits(pcm->config.format) >> 3);
242}
243
Simon Wilsondd88f132011-07-25 10:58:30 -0700244static int pcm_sync_ptr(struct pcm *pcm, int flags) {
245 if (pcm->sync_ptr) {
246 pcm->sync_ptr->flags = flags;
247 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SYNC_PTR, pcm->sync_ptr) < 0)
248 return -1;
249 }
250 return 0;
251}
252
253static int pcm_hw_mmap_status(struct pcm *pcm) {
254
255 if (pcm->sync_ptr)
256 return 0;
257
258 int page_size = sysconf(_SC_PAGE_SIZE);
259 pcm->mmap_status = mmap(NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED,
260 pcm->fd, SNDRV_PCM_MMAP_OFFSET_STATUS);
261 if (pcm->mmap_status == MAP_FAILED)
262 pcm->mmap_status = NULL;
263 if (!pcm->mmap_status)
264 goto mmap_error;
265
266 pcm->mmap_control = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
267 MAP_FILE | MAP_SHARED, pcm->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL);
268 if (pcm->mmap_control == MAP_FAILED)
269 pcm->mmap_control = NULL;
270 if (!pcm->mmap_control) {
271 munmap(pcm->mmap_status, page_size);
272 pcm->mmap_status = NULL;
273 goto mmap_error;
274 }
Eric Laurent73b9c672011-10-14 11:12:24 -0700275 if (pcm->flags & PCM_MMAP)
276 pcm->mmap_control->avail_min = pcm->config.avail_min;
277 else
278 pcm->mmap_control->avail_min = 1;
Simon Wilsondd88f132011-07-25 10:58:30 -0700279
280 return 0;
281
282mmap_error:
283
284 pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr));
285 if (!pcm->sync_ptr)
286 return -ENOMEM;
287 pcm->mmap_status = &pcm->sync_ptr->s.status;
288 pcm->mmap_control = &pcm->sync_ptr->c.control;
Eric Laurent73b9c672011-10-14 11:12:24 -0700289 if (pcm->flags & PCM_MMAP)
290 pcm->mmap_control->avail_min = pcm->config.avail_min;
291 else
292 pcm->mmap_control->avail_min = 1;
293
Simon Wilsondd88f132011-07-25 10:58:30 -0700294 pcm_sync_ptr(pcm, 0);
295
296 return 0;
297}
298
299static void pcm_hw_munmap_status(struct pcm *pcm) {
300 if (pcm->sync_ptr) {
301 free(pcm->sync_ptr);
302 pcm->sync_ptr = NULL;
303 } else {
304 int page_size = sysconf(_SC_PAGE_SIZE);
305 if (pcm->mmap_status)
306 munmap(pcm->mmap_status, page_size);
307 if (pcm->mmap_control)
308 munmap(pcm->mmap_control, page_size);
309 }
310 pcm->mmap_status = NULL;
311 pcm->mmap_control = NULL;
312}
313
Simon Wilsone9942c82011-10-13 13:57:25 -0700314static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
Eric Laurentc98da792013-09-16 14:31:17 -0700315 char *buf, unsigned int src_offset,
Simon Wilsone9942c82011-10-13 13:57:25 -0700316 unsigned int frames)
317{
318 int size_bytes = pcm_frames_to_bytes(pcm, frames);
319 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
320 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
321
322 /* interleaved only atm */
Eric Laurentc98da792013-09-16 14:31:17 -0700323 if (pcm->flags & PCM_IN)
324 memcpy(buf + src_offset_bytes,
325 (char*)pcm->mmap_buffer + pcm_offset_bytes,
326 size_bytes);
327 else
328 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
329 buf + src_offset_bytes,
330 size_bytes);
Simon Wilsone9942c82011-10-13 13:57:25 -0700331 return 0;
332}
333
Eric Laurentc98da792013-09-16 14:31:17 -0700334static int pcm_mmap_transfer_areas(struct pcm *pcm, char *buf,
Simon Wilsone9942c82011-10-13 13:57:25 -0700335 unsigned int offset, unsigned int size)
336{
337 void *pcm_areas;
338 int commit;
339 unsigned int pcm_offset, frames, count = 0;
340
341 while (size > 0) {
342 frames = size;
343 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
Eric Laurentc98da792013-09-16 14:31:17 -0700344 pcm_areas_copy(pcm, pcm_offset, buf, offset, frames);
Simon Wilsone9942c82011-10-13 13:57:25 -0700345 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
346 if (commit < 0) {
347 oops(pcm, commit, "failed to commit %d frames\n", frames);
348 return commit;
349 }
350
351 offset += commit;
352 count += commit;
353 size -= commit;
354 }
355 return count;
356}
357
Simon Wilsondd88f132011-07-25 10:58:30 -0700358int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
359 struct timespec *tstamp)
360{
361 int frames;
362 int rc;
363 snd_pcm_uframes_t hw_ptr;
364
365 if (!pcm_is_ready(pcm))
366 return -1;
367
368 rc = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_HWSYNC);
369 if (rc < 0)
370 return -1;
371
Simon Wilson8dd366f2011-11-17 13:16:52 -0800372 if ((pcm->mmap_status->state != PCM_STATE_RUNNING) &&
373 (pcm->mmap_status->state != PCM_STATE_DRAINING))
Simon Wilson5aed71d2011-11-16 14:45:38 -0800374 return -1;
375
Simon Wilsondd88f132011-07-25 10:58:30 -0700376 *tstamp = pcm->mmap_status->tstamp;
377 if (tstamp->tv_sec == 0 && tstamp->tv_nsec == 0)
378 return -1;
379
380 hw_ptr = pcm->mmap_status->hw_ptr;
381 if (pcm->flags & PCM_IN)
382 frames = hw_ptr - pcm->mmap_control->appl_ptr;
383 else
384 frames = hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
385
386 if (frames < 0)
Eric Laurent73b9c672011-10-14 11:12:24 -0700387 frames += pcm->boundary;
388 else if (frames > (int)pcm->boundary)
389 frames -= pcm->boundary;
Simon Wilsondd88f132011-07-25 10:58:30 -0700390
391 *avail = (unsigned int)frames;
392
393 return 0;
394}
395
Simon Wilsondaa83292012-02-28 15:26:02 -0800396int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
Simon Wilsonedff7082011-06-06 15:33:34 -0700397{
398 struct snd_xferi x;
399
400 if (pcm->flags & PCM_IN)
401 return -EINVAL;
402
Simon Wilsondaa83292012-02-28 15:26:02 -0800403 x.buf = (void*)data;
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700404 x.frames = count / (pcm->config.channels *
405 pcm_format_to_bits(pcm->config.format) / 8);
Simon Wilsonedff7082011-06-06 15:33:34 -0700406
407 for (;;) {
408 if (!pcm->running) {
409 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE))
410 return oops(pcm, errno, "cannot prepare channel");
411 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x))
412 return oops(pcm, errno, "cannot write initial data");
413 pcm->running = 1;
414 return 0;
415 }
416 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) {
417 pcm->running = 0;
418 if (errno == EPIPE) {
John Grossman673253a2012-04-03 16:37:38 -0700419 /* we failed to make our window -- try to restart if we are
420 * allowed to do so. Otherwise, simply allow the EPIPE error to
421 * propagate up to the app level */
Simon Wilsonedff7082011-06-06 15:33:34 -0700422 pcm->underruns++;
John Grossman673253a2012-04-03 16:37:38 -0700423 if (pcm->flags & PCM_NORESTART)
424 return -EPIPE;
Simon Wilsonedff7082011-06-06 15:33:34 -0700425 continue;
426 }
427 return oops(pcm, errno, "cannot write stream data");
428 }
429 return 0;
430 }
431}
432
433int pcm_read(struct pcm *pcm, void *data, unsigned int count)
434{
435 struct snd_xferi x;
436
437 if (!(pcm->flags & PCM_IN))
438 return -EINVAL;
439
440 x.buf = data;
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700441 x.frames = count / (pcm->config.channels *
442 pcm_format_to_bits(pcm->config.format) / 8);
Simon Wilsonedff7082011-06-06 15:33:34 -0700443
444 for (;;) {
445 if (!pcm->running) {
Simon Wilson85dc38f2012-05-15 17:37:19 -0700446 if (pcm_start(pcm) < 0) {
447 fprintf(stderr, "start error");
448 return -errno;
449 }
Simon Wilsonedff7082011-06-06 15:33:34 -0700450 }
451 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) {
452 pcm->running = 0;
453 if (errno == EPIPE) {
454 /* we failed to make our window -- try to restart */
455 pcm->underruns++;
456 continue;
457 }
458 return oops(pcm, errno, "cannot read stream data");
459 }
460 return 0;
461 }
462}
463
464static struct pcm bad_pcm = {
465 .fd = -1,
466};
467
Simon Wilson42fc2d32012-12-03 11:18:57 -0800468struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
469 unsigned int flags)
470{
471 struct snd_pcm_hw_params *params;
472 char fn[256];
473 int fd;
474
475 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
476 flags & PCM_IN ? 'c' : 'p');
477
478 fd = open(fn, O_RDWR);
479 if (fd < 0) {
480 fprintf(stderr, "cannot open device '%s'\n", fn);
481 goto err_open;
482 }
483
484 params = calloc(1, sizeof(struct snd_pcm_hw_params));
485 if (!params)
486 goto err_calloc;
487
488 param_init(params);
489 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
490 fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
491 goto err_hw_refine;
492 }
493
494 close(fd);
495
496 return (struct pcm_params *)params;
497
498err_hw_refine:
499 free(params);
500err_calloc:
501 close(fd);
502err_open:
503 return NULL;
504}
505
506void pcm_params_free(struct pcm_params *pcm_params)
507{
508 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
509
510 if (params)
511 free(params);
512}
513
514static int pcm_param_to_alsa(enum pcm_param param)
515{
516 switch (param) {
Andy Hunga5b44d92014-03-10 18:08:15 -0700517 case PCM_PARAM_ACCESS:
518 return SNDRV_PCM_HW_PARAM_ACCESS;
519 case PCM_PARAM_FORMAT:
520 return SNDRV_PCM_HW_PARAM_FORMAT;
521 case PCM_PARAM_SUBFORMAT:
522 return SNDRV_PCM_HW_PARAM_SUBFORMAT;
Simon Wilson42fc2d32012-12-03 11:18:57 -0800523 case PCM_PARAM_SAMPLE_BITS:
524 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
525 break;
526 case PCM_PARAM_FRAME_BITS:
527 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
528 break;
529 case PCM_PARAM_CHANNELS:
530 return SNDRV_PCM_HW_PARAM_CHANNELS;
531 break;
532 case PCM_PARAM_RATE:
533 return SNDRV_PCM_HW_PARAM_RATE;
534 break;
535 case PCM_PARAM_PERIOD_TIME:
536 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
537 break;
538 case PCM_PARAM_PERIOD_SIZE:
539 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
540 break;
541 case PCM_PARAM_PERIOD_BYTES:
542 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
543 break;
544 case PCM_PARAM_PERIODS:
545 return SNDRV_PCM_HW_PARAM_PERIODS;
546 break;
547 case PCM_PARAM_BUFFER_TIME:
548 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
549 break;
550 case PCM_PARAM_BUFFER_SIZE:
551 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
552 break;
553 case PCM_PARAM_BUFFER_BYTES:
554 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
555 break;
556 case PCM_PARAM_TICK_TIME:
557 return SNDRV_PCM_HW_PARAM_TICK_TIME;
558 break;
559
560 default:
561 return -1;
562 }
563}
564
Andy Hunga5b44d92014-03-10 18:08:15 -0700565struct pcm_mask *pcm_params_get_mask(struct pcm_params *pcm_params,
566 enum pcm_param param)
567{
568 int p;
569 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
570 if (params == NULL) {
571 return NULL;
572 }
573
574 p = pcm_param_to_alsa(param);
575 if (p < 0 || !param_is_mask(p)) {
576 return NULL;
577 }
578
579 return (struct pcm_mask *)param_to_mask(params, p);
580}
581
Simon Wilson42fc2d32012-12-03 11:18:57 -0800582unsigned int pcm_params_get_min(struct pcm_params *pcm_params,
583 enum pcm_param param)
584{
585 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
586 int p;
587
588 if (!params)
589 return 0;
590
591 p = pcm_param_to_alsa(param);
592 if (p < 0)
593 return 0;
594
595 return param_get_min(params, p);
596}
597
598unsigned int pcm_params_get_max(struct pcm_params *pcm_params,
599 enum pcm_param param)
600{
601 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
602 int p;
603
604 if (!params)
605 return 0;
606
607 p = pcm_param_to_alsa(param);
608 if (p < 0)
609 return 0;
610
611 return param_get_max(params, p);
612}
613
Simon Wilsonedff7082011-06-06 15:33:34 -0700614int pcm_close(struct pcm *pcm)
615{
616 if (pcm == &bad_pcm)
617 return 0;
618
Simon Wilsondd88f132011-07-25 10:58:30 -0700619 pcm_hw_munmap_status(pcm);
620
Simon Wilsone9942c82011-10-13 13:57:25 -0700621 if (pcm->flags & PCM_MMAP) {
622 pcm_stop(pcm);
623 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
624 }
625
Simon Wilsonedff7082011-06-06 15:33:34 -0700626 if (pcm->fd >= 0)
627 close(pcm->fd);
628 pcm->running = 0;
629 pcm->buffer_size = 0;
630 pcm->fd = -1;
Simon Wilsondd88f132011-07-25 10:58:30 -0700631 free(pcm);
Simon Wilsonedff7082011-06-06 15:33:34 -0700632 return 0;
633}
634
Simon Wilsonedff7082011-06-06 15:33:34 -0700635struct pcm *pcm_open(unsigned int card, unsigned int device,
636 unsigned int flags, struct pcm_config *config)
637{
638 struct pcm *pcm;
639 struct snd_pcm_info info;
640 struct snd_pcm_hw_params params;
641 struct snd_pcm_sw_params sparams;
642 char fn[256];
Simon Wilsondd88f132011-07-25 10:58:30 -0700643 int rc;
Simon Wilsonedff7082011-06-06 15:33:34 -0700644
645 pcm = calloc(1, sizeof(struct pcm));
646 if (!pcm || !config)
647 return &bad_pcm; /* TODO: could support default config here */
648
649 pcm->config = *config;
650
651 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
652 flags & PCM_IN ? 'c' : 'p');
653
654 pcm->flags = flags;
655 pcm->fd = open(fn, O_RDWR);
656 if (pcm->fd < 0) {
657 oops(pcm, errno, "cannot open device '%s'", fn);
658 return pcm;
659 }
660
661 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
662 oops(pcm, errno, "cannot get info");
Simon Wilsone9942c82011-10-13 13:57:25 -0700663 goto fail_close;
Simon Wilsonedff7082011-06-06 15:33:34 -0700664 }
665
666 param_init(&params);
Simon Wilsonedff7082011-06-06 15:33:34 -0700667 param_set_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT,
668 pcm_format_to_alsa(config->format));
669 param_set_mask(&params, SNDRV_PCM_HW_PARAM_SUBFORMAT,
670 SNDRV_PCM_SUBFORMAT_STD);
671 param_set_min(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
672 param_set_int(&params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
673 pcm_format_to_bits(config->format));
674 param_set_int(&params, SNDRV_PCM_HW_PARAM_FRAME_BITS,
675 pcm_format_to_bits(config->format) * config->channels);
676 param_set_int(&params, SNDRV_PCM_HW_PARAM_CHANNELS,
677 config->channels);
678 param_set_int(&params, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
679 param_set_int(&params, SNDRV_PCM_HW_PARAM_RATE, config->rate);
680
Simon Wilsone9942c82011-10-13 13:57:25 -0700681 if (flags & PCM_NOIRQ) {
682
683 if (!(flags & PCM_MMAP)) {
684 oops(pcm, -EINVAL, "noirq only currently supported with mmap().");
685 goto fail;
686 }
687
688 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
689 pcm->noirq_frames_per_msec = config->rate / 1000;
690 }
691
692 if (flags & PCM_MMAP)
693 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
694 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
695 else
696 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
697 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
698
Simon Wilsonedff7082011-06-06 15:33:34 -0700699 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
700 oops(pcm, errno, "cannot set hw params");
Simon Wilsone9942c82011-10-13 13:57:25 -0700701 goto fail_close;
Simon Wilsonedff7082011-06-06 15:33:34 -0700702 }
703
Simon Wilsone9942c82011-10-13 13:57:25 -0700704 /* get our refined hw_params */
705 config->period_size = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
706 config->period_count = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIODS);
707 pcm->buffer_size = config->period_count * config->period_size;
708
709 if (flags & PCM_MMAP) {
710 pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size),
711 PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0);
712 if (pcm->mmap_buffer == MAP_FAILED) {
713 oops(pcm, -errno, "failed to mmap buffer %d bytes\n",
714 pcm_frames_to_bytes(pcm, pcm->buffer_size));
715 goto fail_close;
716 }
717 }
718
719
Simon Wilsonedff7082011-06-06 15:33:34 -0700720 memset(&sparams, 0, sizeof(sparams));
Simon Wilsondd88f132011-07-25 10:58:30 -0700721 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
Simon Wilsonedff7082011-06-06 15:33:34 -0700722 sparams.period_step = 1;
Simon Wilsonc1239622011-07-27 15:08:34 -0700723
Eric Laurentff2e5422012-08-22 16:18:14 -0700724 if (!config->start_threshold) {
725 if (pcm->flags & PCM_IN)
726 pcm->config.start_threshold = sparams.start_threshold = 1;
727 else
728 pcm->config.start_threshold = sparams.start_threshold =
729 config->period_count * config->period_size / 2;
730 } else
Simon Wilsonc1239622011-07-27 15:08:34 -0700731 sparams.start_threshold = config->start_threshold;
732
Simon Wilsone9942c82011-10-13 13:57:25 -0700733 /* pick a high stop threshold - todo: does this need further tuning */
Eric Laurent1b32ddf2012-01-30 11:31:56 -0800734 if (!config->stop_threshold) {
735 if (pcm->flags & PCM_IN)
736 pcm->config.stop_threshold = sparams.stop_threshold =
737 config->period_count * config->period_size * 10;
738 else
739 pcm->config.stop_threshold = sparams.stop_threshold =
740 config->period_count * config->period_size;
741 }
Simon Wilsonc1239622011-07-27 15:08:34 -0700742 else
743 sparams.stop_threshold = config->stop_threshold;
744
Eric Laurent73b9c672011-10-14 11:12:24 -0700745 if (!pcm->config.avail_min) {
746 if (pcm->flags & PCM_MMAP)
747 pcm->config.avail_min = sparams.avail_min = pcm->config.period_size;
748 else
749 pcm->config.avail_min = sparams.avail_min = 1;
750 } else
751 sparams.avail_min = config->avail_min;
752
Simon Wilsonedff7082011-06-06 15:33:34 -0700753 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
754 sparams.silence_size = 0;
Simon Wilsonc1239622011-07-27 15:08:34 -0700755 sparams.silence_threshold = config->silence_threshold;
Simon Wilsone9942c82011-10-13 13:57:25 -0700756 pcm->boundary = sparams.boundary = pcm->buffer_size;
Simon Wilsonc1239622011-07-27 15:08:34 -0700757
Simon Wilsondaa83292012-02-28 15:26:02 -0800758 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
Simon Wilsone9942c82011-10-13 13:57:25 -0700759 pcm->boundary *= 2;
Simon Wilsonedff7082011-06-06 15:33:34 -0700760
761 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
762 oops(pcm, errno, "cannot set sw params");
763 goto fail;
764 }
765
Simon Wilsondd88f132011-07-25 10:58:30 -0700766 rc = pcm_hw_mmap_status(pcm);
767 if (rc < 0) {
768 oops(pcm, rc, "mmap status failed");
769 goto fail;
770 }
771
Glenn Kasten6b0a2062013-08-22 15:11:48 -0700772#ifdef SNDRV_PCM_IOCTL_TTSTAMP
773 if (pcm->flags & PCM_MONOTONIC) {
774 int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
775 rc = ioctl(pcm->fd, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
776 if (rc < 0) {
777 oops(pcm, rc, "cannot set timestamp type");
778 goto fail;
779 }
780 }
781#endif
782
Simon Wilsonedff7082011-06-06 15:33:34 -0700783 pcm->underruns = 0;
784 return pcm;
785
786fail:
Simon Wilsone9942c82011-10-13 13:57:25 -0700787 if (flags & PCM_MMAP)
788 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
789fail_close:
Simon Wilsonedff7082011-06-06 15:33:34 -0700790 close(pcm->fd);
791 pcm->fd = -1;
792 return pcm;
793}
794
795int pcm_is_ready(struct pcm *pcm)
796{
797 return pcm->fd >= 0;
798}
Simon Wilson70d77082011-06-24 11:08:10 -0700799
800int pcm_start(struct pcm *pcm)
801{
802 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
803 return oops(pcm, errno, "cannot prepare channel");
Simon Wilsone9942c82011-10-13 13:57:25 -0700804
805 if (pcm->flags & PCM_MMAP)
806 pcm_sync_ptr(pcm, 0);
807
Simon Wilson70d77082011-06-24 11:08:10 -0700808 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
809 return oops(pcm, errno, "cannot start channel");
810
Simon Wilsone9942c82011-10-13 13:57:25 -0700811 pcm->running = 1;
Simon Wilson70d77082011-06-24 11:08:10 -0700812 return 0;
813}
814
815int pcm_stop(struct pcm *pcm)
816{
817 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
818 return oops(pcm, errno, "cannot stop channel");
819
Simon Wilsone9942c82011-10-13 13:57:25 -0700820 pcm->running = 0;
Simon Wilson70d77082011-06-24 11:08:10 -0700821 return 0;
822}
823
Simon Wilsone9942c82011-10-13 13:57:25 -0700824static inline int pcm_mmap_playback_avail(struct pcm *pcm)
825{
826 int avail;
827
828 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
829
830 if (avail < 0)
831 avail += pcm->boundary;
832 else if (avail > (int)pcm->boundary)
833 avail -= pcm->boundary;
834
835 return avail;
836}
837
838static inline int pcm_mmap_capture_avail(struct pcm *pcm)
839{
840 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
841 if (avail < 0)
842 avail += pcm->boundary;
843 return avail;
844}
845
846static inline int pcm_mmap_avail(struct pcm *pcm)
847{
848 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
849 if (pcm->flags & PCM_IN)
850 return pcm_mmap_capture_avail(pcm);
851 else
852 return pcm_mmap_playback_avail(pcm);
853}
854
855static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
856{
857 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
858 appl_ptr += frames;
859
860 /* check for boundary wrap */
861 if (appl_ptr > pcm->boundary)
862 appl_ptr -= pcm->boundary;
863 pcm->mmap_control->appl_ptr = appl_ptr;
864}
865
866int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
867 unsigned int *frames)
868{
869 unsigned int continuous, copy_frames, avail;
870
871 /* return the mmap buffer */
872 *areas = pcm->mmap_buffer;
873
874 /* and the application offset in frames */
875 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
876
877 avail = pcm_mmap_avail(pcm);
878 if (avail > pcm->buffer_size)
879 avail = pcm->buffer_size;
880 continuous = pcm->buffer_size - *offset;
881
882 /* we can only copy frames if the are availabale and continuos */
883 copy_frames = *frames;
884 if (copy_frames > avail)
885 copy_frames = avail;
886 if (copy_frames > continuous)
887 copy_frames = continuous;
888 *frames = copy_frames;
889
890 return 0;
891}
892
893int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
894{
895 /* update the application pointer in userspace and kernel */
896 pcm_mmap_appl_forward(pcm, frames);
897 pcm_sync_ptr(pcm, 0);
898
899 return frames;
900}
901
902int pcm_avail_update(struct pcm *pcm)
903{
904 pcm_sync_ptr(pcm, 0);
905 return pcm_mmap_avail(pcm);
906}
907
908int pcm_state(struct pcm *pcm)
909{
910 int err = pcm_sync_ptr(pcm, 0);
911 if (err < 0)
912 return err;
913
914 return pcm->mmap_status->state;
915}
916
Eric Laurent73b9c672011-10-14 11:12:24 -0700917int pcm_set_avail_min(struct pcm *pcm, int avail_min)
918{
919 if ((~pcm->flags) & (PCM_MMAP | PCM_NOIRQ))
920 return -ENOSYS;
921
922 pcm->config.avail_min = avail_min;
923 return 0;
924}
925
Simon Wilsone9942c82011-10-13 13:57:25 -0700926int pcm_wait(struct pcm *pcm, int timeout)
927{
928 struct pollfd pfd;
Simon Wilsone9942c82011-10-13 13:57:25 -0700929 int err;
930
931 pfd.fd = pcm->fd;
932 pfd.events = POLLOUT | POLLERR | POLLNVAL;
933
934 do {
935 /* let's wait for avail or timeout */
936 err = poll(&pfd, 1, timeout);
937 if (err < 0)
938 return -errno;
939
940 /* timeout ? */
941 if (err == 0)
942 return 0;
943
944 /* have we been interrupted ? */
945 if (errno == -EINTR)
946 continue;
947
948 /* check for any errors */
949 if (pfd.revents & (POLLERR | POLLNVAL)) {
950 switch (pcm_state(pcm)) {
951 case PCM_STATE_XRUN:
952 return -EPIPE;
953 case PCM_STATE_SUSPENDED:
954 return -ESTRPIPE;
955 case PCM_STATE_DISCONNECTED:
956 return -ENODEV;
957 default:
958 return -EIO;
959 }
960 }
961 /* poll again if fd not ready for IO */
962 } while (!(pfd.revents & (POLLIN | POLLOUT)));
963
964 return 1;
965}
966
Eric Laurentc98da792013-09-16 14:31:17 -0700967int pcm_mmap_transfer(struct pcm *pcm, const void *buffer, unsigned int bytes)
Simon Wilsone9942c82011-10-13 13:57:25 -0700968{
969 int err = 0, frames, avail;
970 unsigned int offset = 0, count;
971
972 if (bytes == 0)
973 return 0;
974
975 count = pcm_bytes_to_frames(pcm, bytes);
976
977 while (count > 0) {
978
979 /* get the available space for writing new frames */
980 avail = pcm_avail_update(pcm);
981 if (avail < 0) {
982 fprintf(stderr, "cannot determine available mmap frames");
983 return err;
984 }
985
986 /* start the audio if we reach the threshold */
987 if (!pcm->running &&
988 (pcm->buffer_size - avail) >= pcm->config.start_threshold) {
989 if (pcm_start(pcm) < 0) {
990 fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n",
991 (unsigned int)pcm->mmap_status->hw_ptr,
992 (unsigned int)pcm->mmap_control->appl_ptr,
993 avail);
994 return -errno;
995 }
Eric Laurent73b9c672011-10-14 11:12:24 -0700996 pcm->wait_for_avail_min = 0;
Simon Wilsone9942c82011-10-13 13:57:25 -0700997 }
998
999 /* sleep until we have space to write new frames */
Eric Laurent73b9c672011-10-14 11:12:24 -07001000 if (pcm->running) {
1001 /* enable waiting for avail_min threshold when less frames than we have to write
1002 * are available. */
1003 if (!pcm->wait_for_avail_min && (count > (unsigned int)avail))
1004 pcm->wait_for_avail_min = 1;
Simon Wilsone9942c82011-10-13 13:57:25 -07001005
Eric Laurent73b9c672011-10-14 11:12:24 -07001006 if (pcm->wait_for_avail_min && (avail < pcm->config.avail_min)) {
1007 int time = -1;
Simon Wilsone9942c82011-10-13 13:57:25 -07001008
Eric Laurent73b9c672011-10-14 11:12:24 -07001009 /* disable waiting for avail_min threshold to allow small amounts of data to be
1010 * written without waiting as long as there is enough room in buffer. */
1011 pcm->wait_for_avail_min = 0;
1012
1013 if (pcm->flags & PCM_NOIRQ)
1014 time = (pcm->config.avail_min - avail) / pcm->noirq_frames_per_msec;
1015
1016 err = pcm_wait(pcm, time);
1017 if (err < 0) {
1018 pcm->running = 0;
1019 oops(pcm, err, "wait error: hw 0x%x app 0x%x avail 0x%x\n",
1020 (unsigned int)pcm->mmap_status->hw_ptr,
1021 (unsigned int)pcm->mmap_control->appl_ptr,
1022 avail);
1023 pcm->mmap_control->appl_ptr = 0;
1024 return err;
1025 }
1026 continue;
Simon Wilsone9942c82011-10-13 13:57:25 -07001027 }
Simon Wilsone9942c82011-10-13 13:57:25 -07001028 }
1029
1030 frames = count;
1031 if (frames > avail)
1032 frames = avail;
1033
1034 if (!frames)
1035 break;
1036
1037 /* copy frames from buffer */
Eric Laurentc98da792013-09-16 14:31:17 -07001038 frames = pcm_mmap_transfer_areas(pcm, (void *)buffer, offset, frames);
Simon Wilsone9942c82011-10-13 13:57:25 -07001039 if (frames < 0) {
1040 fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n",
1041 (unsigned int)pcm->mmap_status->hw_ptr,
1042 (unsigned int)pcm->mmap_control->appl_ptr,
1043 avail);
1044 return frames;
1045 }
1046
1047 offset += frames;
1048 count -= frames;
1049 }
1050
Simon Wilsone9942c82011-10-13 13:57:25 -07001051 return 0;
1052}
Eric Laurentc98da792013-09-16 14:31:17 -07001053
1054int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count)
1055{
1056 if ((~pcm->flags) & (PCM_OUT | PCM_MMAP))
1057 return -ENOSYS;
1058
1059 return pcm_mmap_transfer(pcm, (void *)data, count);
1060}
1061
1062int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count)
1063{
1064 if ((~pcm->flags) & (PCM_IN | PCM_MMAP))
1065 return -ENOSYS;
1066
1067 return pcm_mmap_transfer(pcm, data, count);
1068}