blob: 2842e9ea27fadb2b6ac2d8c98ae21e4fe5a8720d [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) {
517 case PCM_PARAM_SAMPLE_BITS:
518 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
519 break;
520 case PCM_PARAM_FRAME_BITS:
521 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
522 break;
523 case PCM_PARAM_CHANNELS:
524 return SNDRV_PCM_HW_PARAM_CHANNELS;
525 break;
526 case PCM_PARAM_RATE:
527 return SNDRV_PCM_HW_PARAM_RATE;
528 break;
529 case PCM_PARAM_PERIOD_TIME:
530 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
531 break;
532 case PCM_PARAM_PERIOD_SIZE:
533 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
534 break;
535 case PCM_PARAM_PERIOD_BYTES:
536 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
537 break;
538 case PCM_PARAM_PERIODS:
539 return SNDRV_PCM_HW_PARAM_PERIODS;
540 break;
541 case PCM_PARAM_BUFFER_TIME:
542 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
543 break;
544 case PCM_PARAM_BUFFER_SIZE:
545 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
546 break;
547 case PCM_PARAM_BUFFER_BYTES:
548 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
549 break;
550 case PCM_PARAM_TICK_TIME:
551 return SNDRV_PCM_HW_PARAM_TICK_TIME;
552 break;
553
554 default:
555 return -1;
556 }
557}
558
559unsigned int pcm_params_get_min(struct pcm_params *pcm_params,
560 enum pcm_param param)
561{
562 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
563 int p;
564
565 if (!params)
566 return 0;
567
568 p = pcm_param_to_alsa(param);
569 if (p < 0)
570 return 0;
571
572 return param_get_min(params, p);
573}
574
575unsigned int pcm_params_get_max(struct pcm_params *pcm_params,
576 enum pcm_param param)
577{
578 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
579 int p;
580
581 if (!params)
582 return 0;
583
584 p = pcm_param_to_alsa(param);
585 if (p < 0)
586 return 0;
587
588 return param_get_max(params, p);
589}
590
Simon Wilsonedff7082011-06-06 15:33:34 -0700591int pcm_close(struct pcm *pcm)
592{
593 if (pcm == &bad_pcm)
594 return 0;
595
Simon Wilsondd88f132011-07-25 10:58:30 -0700596 pcm_hw_munmap_status(pcm);
597
Simon Wilsone9942c82011-10-13 13:57:25 -0700598 if (pcm->flags & PCM_MMAP) {
599 pcm_stop(pcm);
600 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
601 }
602
Simon Wilsonedff7082011-06-06 15:33:34 -0700603 if (pcm->fd >= 0)
604 close(pcm->fd);
605 pcm->running = 0;
606 pcm->buffer_size = 0;
607 pcm->fd = -1;
Simon Wilsondd88f132011-07-25 10:58:30 -0700608 free(pcm);
Simon Wilsonedff7082011-06-06 15:33:34 -0700609 return 0;
610}
611
Simon Wilsonedff7082011-06-06 15:33:34 -0700612struct pcm *pcm_open(unsigned int card, unsigned int device,
613 unsigned int flags, struct pcm_config *config)
614{
615 struct pcm *pcm;
616 struct snd_pcm_info info;
617 struct snd_pcm_hw_params params;
618 struct snd_pcm_sw_params sparams;
619 char fn[256];
Simon Wilsondd88f132011-07-25 10:58:30 -0700620 int rc;
Simon Wilsonedff7082011-06-06 15:33:34 -0700621
622 pcm = calloc(1, sizeof(struct pcm));
623 if (!pcm || !config)
624 return &bad_pcm; /* TODO: could support default config here */
625
626 pcm->config = *config;
627
628 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
629 flags & PCM_IN ? 'c' : 'p');
630
631 pcm->flags = flags;
632 pcm->fd = open(fn, O_RDWR);
633 if (pcm->fd < 0) {
634 oops(pcm, errno, "cannot open device '%s'", fn);
635 return pcm;
636 }
637
638 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
639 oops(pcm, errno, "cannot get info");
Simon Wilsone9942c82011-10-13 13:57:25 -0700640 goto fail_close;
Simon Wilsonedff7082011-06-06 15:33:34 -0700641 }
642
643 param_init(&params);
Simon Wilsonedff7082011-06-06 15:33:34 -0700644 param_set_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT,
645 pcm_format_to_alsa(config->format));
646 param_set_mask(&params, SNDRV_PCM_HW_PARAM_SUBFORMAT,
647 SNDRV_PCM_SUBFORMAT_STD);
648 param_set_min(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
649 param_set_int(&params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
650 pcm_format_to_bits(config->format));
651 param_set_int(&params, SNDRV_PCM_HW_PARAM_FRAME_BITS,
652 pcm_format_to_bits(config->format) * config->channels);
653 param_set_int(&params, SNDRV_PCM_HW_PARAM_CHANNELS,
654 config->channels);
655 param_set_int(&params, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
656 param_set_int(&params, SNDRV_PCM_HW_PARAM_RATE, config->rate);
657
Simon Wilsone9942c82011-10-13 13:57:25 -0700658 if (flags & PCM_NOIRQ) {
659
660 if (!(flags & PCM_MMAP)) {
661 oops(pcm, -EINVAL, "noirq only currently supported with mmap().");
662 goto fail;
663 }
664
665 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
666 pcm->noirq_frames_per_msec = config->rate / 1000;
667 }
668
669 if (flags & PCM_MMAP)
670 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
671 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
672 else
673 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
674 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
675
Simon Wilsonedff7082011-06-06 15:33:34 -0700676 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
677 oops(pcm, errno, "cannot set hw params");
Simon Wilsone9942c82011-10-13 13:57:25 -0700678 goto fail_close;
Simon Wilsonedff7082011-06-06 15:33:34 -0700679 }
680
Simon Wilsone9942c82011-10-13 13:57:25 -0700681 /* get our refined hw_params */
682 config->period_size = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
683 config->period_count = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIODS);
684 pcm->buffer_size = config->period_count * config->period_size;
685
686 if (flags & PCM_MMAP) {
687 pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size),
688 PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0);
689 if (pcm->mmap_buffer == MAP_FAILED) {
690 oops(pcm, -errno, "failed to mmap buffer %d bytes\n",
691 pcm_frames_to_bytes(pcm, pcm->buffer_size));
692 goto fail_close;
693 }
694 }
695
696
Simon Wilsonedff7082011-06-06 15:33:34 -0700697 memset(&sparams, 0, sizeof(sparams));
Simon Wilsondd88f132011-07-25 10:58:30 -0700698 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
Simon Wilsonedff7082011-06-06 15:33:34 -0700699 sparams.period_step = 1;
Simon Wilsonc1239622011-07-27 15:08:34 -0700700
Eric Laurentff2e5422012-08-22 16:18:14 -0700701 if (!config->start_threshold) {
702 if (pcm->flags & PCM_IN)
703 pcm->config.start_threshold = sparams.start_threshold = 1;
704 else
705 pcm->config.start_threshold = sparams.start_threshold =
706 config->period_count * config->period_size / 2;
707 } else
Simon Wilsonc1239622011-07-27 15:08:34 -0700708 sparams.start_threshold = config->start_threshold;
709
Simon Wilsone9942c82011-10-13 13:57:25 -0700710 /* pick a high stop threshold - todo: does this need further tuning */
Eric Laurent1b32ddf2012-01-30 11:31:56 -0800711 if (!config->stop_threshold) {
712 if (pcm->flags & PCM_IN)
713 pcm->config.stop_threshold = sparams.stop_threshold =
714 config->period_count * config->period_size * 10;
715 else
716 pcm->config.stop_threshold = sparams.stop_threshold =
717 config->period_count * config->period_size;
718 }
Simon Wilsonc1239622011-07-27 15:08:34 -0700719 else
720 sparams.stop_threshold = config->stop_threshold;
721
Eric Laurent73b9c672011-10-14 11:12:24 -0700722 if (!pcm->config.avail_min) {
723 if (pcm->flags & PCM_MMAP)
724 pcm->config.avail_min = sparams.avail_min = pcm->config.period_size;
725 else
726 pcm->config.avail_min = sparams.avail_min = 1;
727 } else
728 sparams.avail_min = config->avail_min;
729
Simon Wilsonedff7082011-06-06 15:33:34 -0700730 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
731 sparams.silence_size = 0;
Simon Wilsonc1239622011-07-27 15:08:34 -0700732 sparams.silence_threshold = config->silence_threshold;
Simon Wilsone9942c82011-10-13 13:57:25 -0700733 pcm->boundary = sparams.boundary = pcm->buffer_size;
Simon Wilsonc1239622011-07-27 15:08:34 -0700734
Simon Wilsondaa83292012-02-28 15:26:02 -0800735 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
Simon Wilsone9942c82011-10-13 13:57:25 -0700736 pcm->boundary *= 2;
Simon Wilsonedff7082011-06-06 15:33:34 -0700737
738 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
739 oops(pcm, errno, "cannot set sw params");
740 goto fail;
741 }
742
Simon Wilsondd88f132011-07-25 10:58:30 -0700743 rc = pcm_hw_mmap_status(pcm);
744 if (rc < 0) {
745 oops(pcm, rc, "mmap status failed");
746 goto fail;
747 }
748
Glenn Kasten6b0a2062013-08-22 15:11:48 -0700749#ifdef SNDRV_PCM_IOCTL_TTSTAMP
750 if (pcm->flags & PCM_MONOTONIC) {
751 int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
752 rc = ioctl(pcm->fd, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
753 if (rc < 0) {
754 oops(pcm, rc, "cannot set timestamp type");
755 goto fail;
756 }
757 }
758#endif
759
Simon Wilsonedff7082011-06-06 15:33:34 -0700760 pcm->underruns = 0;
761 return pcm;
762
763fail:
Simon Wilsone9942c82011-10-13 13:57:25 -0700764 if (flags & PCM_MMAP)
765 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
766fail_close:
Simon Wilsonedff7082011-06-06 15:33:34 -0700767 close(pcm->fd);
768 pcm->fd = -1;
769 return pcm;
770}
771
772int pcm_is_ready(struct pcm *pcm)
773{
774 return pcm->fd >= 0;
775}
Simon Wilson70d77082011-06-24 11:08:10 -0700776
777int pcm_start(struct pcm *pcm)
778{
779 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
780 return oops(pcm, errno, "cannot prepare channel");
Simon Wilsone9942c82011-10-13 13:57:25 -0700781
782 if (pcm->flags & PCM_MMAP)
783 pcm_sync_ptr(pcm, 0);
784
Simon Wilson70d77082011-06-24 11:08:10 -0700785 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
786 return oops(pcm, errno, "cannot start channel");
787
Simon Wilsone9942c82011-10-13 13:57:25 -0700788 pcm->running = 1;
Simon Wilson70d77082011-06-24 11:08:10 -0700789 return 0;
790}
791
792int pcm_stop(struct pcm *pcm)
793{
794 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
795 return oops(pcm, errno, "cannot stop channel");
796
Simon Wilsone9942c82011-10-13 13:57:25 -0700797 pcm->running = 0;
Simon Wilson70d77082011-06-24 11:08:10 -0700798 return 0;
799}
800
Simon Wilsone9942c82011-10-13 13:57:25 -0700801static inline int pcm_mmap_playback_avail(struct pcm *pcm)
802{
803 int avail;
804
805 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
806
807 if (avail < 0)
808 avail += pcm->boundary;
809 else if (avail > (int)pcm->boundary)
810 avail -= pcm->boundary;
811
812 return avail;
813}
814
815static inline int pcm_mmap_capture_avail(struct pcm *pcm)
816{
817 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
818 if (avail < 0)
819 avail += pcm->boundary;
820 return avail;
821}
822
823static inline int pcm_mmap_avail(struct pcm *pcm)
824{
825 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
826 if (pcm->flags & PCM_IN)
827 return pcm_mmap_capture_avail(pcm);
828 else
829 return pcm_mmap_playback_avail(pcm);
830}
831
832static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
833{
834 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
835 appl_ptr += frames;
836
837 /* check for boundary wrap */
838 if (appl_ptr > pcm->boundary)
839 appl_ptr -= pcm->boundary;
840 pcm->mmap_control->appl_ptr = appl_ptr;
841}
842
843int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
844 unsigned int *frames)
845{
846 unsigned int continuous, copy_frames, avail;
847
848 /* return the mmap buffer */
849 *areas = pcm->mmap_buffer;
850
851 /* and the application offset in frames */
852 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
853
854 avail = pcm_mmap_avail(pcm);
855 if (avail > pcm->buffer_size)
856 avail = pcm->buffer_size;
857 continuous = pcm->buffer_size - *offset;
858
859 /* we can only copy frames if the are availabale and continuos */
860 copy_frames = *frames;
861 if (copy_frames > avail)
862 copy_frames = avail;
863 if (copy_frames > continuous)
864 copy_frames = continuous;
865 *frames = copy_frames;
866
867 return 0;
868}
869
870int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
871{
872 /* update the application pointer in userspace and kernel */
873 pcm_mmap_appl_forward(pcm, frames);
874 pcm_sync_ptr(pcm, 0);
875
876 return frames;
877}
878
879int pcm_avail_update(struct pcm *pcm)
880{
881 pcm_sync_ptr(pcm, 0);
882 return pcm_mmap_avail(pcm);
883}
884
885int pcm_state(struct pcm *pcm)
886{
887 int err = pcm_sync_ptr(pcm, 0);
888 if (err < 0)
889 return err;
890
891 return pcm->mmap_status->state;
892}
893
Eric Laurent73b9c672011-10-14 11:12:24 -0700894int pcm_set_avail_min(struct pcm *pcm, int avail_min)
895{
896 if ((~pcm->flags) & (PCM_MMAP | PCM_NOIRQ))
897 return -ENOSYS;
898
899 pcm->config.avail_min = avail_min;
900 return 0;
901}
902
Simon Wilsone9942c82011-10-13 13:57:25 -0700903int pcm_wait(struct pcm *pcm, int timeout)
904{
905 struct pollfd pfd;
Simon Wilsone9942c82011-10-13 13:57:25 -0700906 int err;
907
908 pfd.fd = pcm->fd;
909 pfd.events = POLLOUT | POLLERR | POLLNVAL;
910
911 do {
912 /* let's wait for avail or timeout */
913 err = poll(&pfd, 1, timeout);
914 if (err < 0)
915 return -errno;
916
917 /* timeout ? */
918 if (err == 0)
919 return 0;
920
921 /* have we been interrupted ? */
922 if (errno == -EINTR)
923 continue;
924
925 /* check for any errors */
926 if (pfd.revents & (POLLERR | POLLNVAL)) {
927 switch (pcm_state(pcm)) {
928 case PCM_STATE_XRUN:
929 return -EPIPE;
930 case PCM_STATE_SUSPENDED:
931 return -ESTRPIPE;
932 case PCM_STATE_DISCONNECTED:
933 return -ENODEV;
934 default:
935 return -EIO;
936 }
937 }
938 /* poll again if fd not ready for IO */
939 } while (!(pfd.revents & (POLLIN | POLLOUT)));
940
941 return 1;
942}
943
Eric Laurentc98da792013-09-16 14:31:17 -0700944int pcm_mmap_transfer(struct pcm *pcm, const void *buffer, unsigned int bytes)
Simon Wilsone9942c82011-10-13 13:57:25 -0700945{
946 int err = 0, frames, avail;
947 unsigned int offset = 0, count;
948
949 if (bytes == 0)
950 return 0;
951
952 count = pcm_bytes_to_frames(pcm, bytes);
953
954 while (count > 0) {
955
956 /* get the available space for writing new frames */
957 avail = pcm_avail_update(pcm);
958 if (avail < 0) {
959 fprintf(stderr, "cannot determine available mmap frames");
960 return err;
961 }
962
963 /* start the audio if we reach the threshold */
964 if (!pcm->running &&
965 (pcm->buffer_size - avail) >= pcm->config.start_threshold) {
966 if (pcm_start(pcm) < 0) {
967 fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n",
968 (unsigned int)pcm->mmap_status->hw_ptr,
969 (unsigned int)pcm->mmap_control->appl_ptr,
970 avail);
971 return -errno;
972 }
Eric Laurent73b9c672011-10-14 11:12:24 -0700973 pcm->wait_for_avail_min = 0;
Simon Wilsone9942c82011-10-13 13:57:25 -0700974 }
975
976 /* sleep until we have space to write new frames */
Eric Laurent73b9c672011-10-14 11:12:24 -0700977 if (pcm->running) {
978 /* enable waiting for avail_min threshold when less frames than we have to write
979 * are available. */
980 if (!pcm->wait_for_avail_min && (count > (unsigned int)avail))
981 pcm->wait_for_avail_min = 1;
Simon Wilsone9942c82011-10-13 13:57:25 -0700982
Eric Laurent73b9c672011-10-14 11:12:24 -0700983 if (pcm->wait_for_avail_min && (avail < pcm->config.avail_min)) {
984 int time = -1;
Simon Wilsone9942c82011-10-13 13:57:25 -0700985
Eric Laurent73b9c672011-10-14 11:12:24 -0700986 /* disable waiting for avail_min threshold to allow small amounts of data to be
987 * written without waiting as long as there is enough room in buffer. */
988 pcm->wait_for_avail_min = 0;
989
990 if (pcm->flags & PCM_NOIRQ)
991 time = (pcm->config.avail_min - avail) / pcm->noirq_frames_per_msec;
992
993 err = pcm_wait(pcm, time);
994 if (err < 0) {
995 pcm->running = 0;
996 oops(pcm, err, "wait error: hw 0x%x app 0x%x avail 0x%x\n",
997 (unsigned int)pcm->mmap_status->hw_ptr,
998 (unsigned int)pcm->mmap_control->appl_ptr,
999 avail);
1000 pcm->mmap_control->appl_ptr = 0;
1001 return err;
1002 }
1003 continue;
Simon Wilsone9942c82011-10-13 13:57:25 -07001004 }
Simon Wilsone9942c82011-10-13 13:57:25 -07001005 }
1006
1007 frames = count;
1008 if (frames > avail)
1009 frames = avail;
1010
1011 if (!frames)
1012 break;
1013
1014 /* copy frames from buffer */
Eric Laurentc98da792013-09-16 14:31:17 -07001015 frames = pcm_mmap_transfer_areas(pcm, (void *)buffer, offset, frames);
Simon Wilsone9942c82011-10-13 13:57:25 -07001016 if (frames < 0) {
1017 fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n",
1018 (unsigned int)pcm->mmap_status->hw_ptr,
1019 (unsigned int)pcm->mmap_control->appl_ptr,
1020 avail);
1021 return frames;
1022 }
1023
1024 offset += frames;
1025 count -= frames;
1026 }
1027
Simon Wilsone9942c82011-10-13 13:57:25 -07001028 return 0;
1029}
Eric Laurentc98da792013-09-16 14:31:17 -07001030
1031int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count)
1032{
1033 if ((~pcm->flags) & (PCM_OUT | PCM_MMAP))
1034 return -ENOSYS;
1035
1036 return pcm_mmap_transfer(pcm, (void *)data, count);
1037}
1038
1039int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count)
1040{
1041 if ((~pcm->flags) & (PCM_IN | PCM_MMAP))
1042 return -ENOSYS;
1043
1044 return pcm_mmap_transfer(pcm, data, count);
1045}